sdio.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  1. /*
  2. * Marvell Wireless LAN device driver: SDIO specific handling
  3. *
  4. * Copyright (C) 2011-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include <linux/firmware.h>
  20. #include "decl.h"
  21. #include "ioctl.h"
  22. #include "util.h"
  23. #include "fw.h"
  24. #include "main.h"
  25. #include "wmm.h"
  26. #include "11n.h"
  27. #include "sdio.h"
  28. #define SDIO_VERSION "1.0"
  29. /* The mwifiex_sdio_remove() callback function is called when
  30. * user removes this module from kernel space or ejects
  31. * the card from the slot. The driver handles these 2 cases
  32. * differently.
  33. * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
  34. * HS_CANCEL etc.) are sent to the firmware.
  35. * If the card is removed, there is no need to send these command.
  36. *
  37. * The variable 'user_rmmod' is used to distinguish these two
  38. * scenarios. This flag is initialized as FALSE in case the card
  39. * is removed, and will be set to TRUE for module removal when
  40. * module_exit function is called.
  41. */
  42. static u8 user_rmmod;
  43. static struct mwifiex_if_ops sdio_ops;
  44. static unsigned long iface_work_flags;
  45. static struct semaphore add_remove_card_sem;
  46. static struct memory_type_mapping generic_mem_type_map[] = {
  47. {"DUMP", NULL, 0, 0xDD},
  48. };
  49. static struct memory_type_mapping mem_type_mapping_tbl[] = {
  50. {"ITCM", NULL, 0, 0xF0},
  51. {"DTCM", NULL, 0, 0xF1},
  52. {"SQRAM", NULL, 0, 0xF2},
  53. {"APU", NULL, 0, 0xF3},
  54. {"CIU", NULL, 0, 0xF4},
  55. {"ICU", NULL, 0, 0xF5},
  56. {"MAC", NULL, 0, 0xF6},
  57. {"EXT7", NULL, 0, 0xF7},
  58. {"EXT8", NULL, 0, 0xF8},
  59. {"EXT9", NULL, 0, 0xF9},
  60. {"EXT10", NULL, 0, 0xFA},
  61. {"EXT11", NULL, 0, 0xFB},
  62. {"EXT12", NULL, 0, 0xFC},
  63. {"EXT13", NULL, 0, 0xFD},
  64. {"EXTLAST", NULL, 0, 0xFE},
  65. };
  66. /*
  67. * SDIO probe.
  68. *
  69. * This function probes an mwifiex device and registers it. It allocates
  70. * the card structure, enables SDIO function number and initiates the
  71. * device registration and initialization procedure by adding a logical
  72. * interface.
  73. */
  74. static int
  75. mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
  76. {
  77. int ret;
  78. struct sdio_mmc_card *card = NULL;
  79. pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
  80. func->vendor, func->device, func->class, func->num);
  81. card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
  82. if (!card)
  83. return -ENOMEM;
  84. card->func = func;
  85. card->device_id = id;
  86. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  87. if (id->driver_data) {
  88. struct mwifiex_sdio_device *data = (void *)id->driver_data;
  89. card->firmware = data->firmware;
  90. card->reg = data->reg;
  91. card->max_ports = data->max_ports;
  92. card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
  93. card->supports_sdio_new_mode = data->supports_sdio_new_mode;
  94. card->has_control_mask = data->has_control_mask;
  95. card->tx_buf_size = data->tx_buf_size;
  96. card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
  97. card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
  98. card->can_dump_fw = data->can_dump_fw;
  99. card->fw_dump_enh = data->fw_dump_enh;
  100. card->can_auto_tdls = data->can_auto_tdls;
  101. card->can_ext_scan = data->can_ext_scan;
  102. }
  103. sdio_claim_host(func);
  104. ret = sdio_enable_func(func);
  105. sdio_release_host(func);
  106. if (ret) {
  107. pr_err("%s: failed to enable function\n", __func__);
  108. kfree(card);
  109. return -EIO;
  110. }
  111. if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
  112. MWIFIEX_SDIO)) {
  113. pr_err("%s: add card failed\n", __func__);
  114. kfree(card);
  115. sdio_claim_host(func);
  116. ret = sdio_disable_func(func);
  117. sdio_release_host(func);
  118. ret = -1;
  119. }
  120. return ret;
  121. }
  122. /*
  123. * SDIO resume.
  124. *
  125. * Kernel needs to suspend all functions separately. Therefore all
  126. * registered functions must have drivers with suspend and resume
  127. * methods. Failing that the kernel simply removes the whole card.
  128. *
  129. * If already not resumed, this function turns on the traffic and
  130. * sends a host sleep cancel request to the firmware.
  131. */
  132. static int mwifiex_sdio_resume(struct device *dev)
  133. {
  134. struct sdio_func *func = dev_to_sdio_func(dev);
  135. struct sdio_mmc_card *card;
  136. struct mwifiex_adapter *adapter;
  137. mmc_pm_flag_t pm_flag = 0;
  138. if (func) {
  139. pm_flag = sdio_get_host_pm_caps(func);
  140. card = sdio_get_drvdata(func);
  141. if (!card || !card->adapter) {
  142. pr_err("resume: invalid card or adapter\n");
  143. return 0;
  144. }
  145. } else {
  146. pr_err("resume: sdio_func is not specified\n");
  147. return 0;
  148. }
  149. adapter = card->adapter;
  150. if (!adapter->is_suspended) {
  151. mwifiex_dbg(adapter, WARN,
  152. "device already resumed\n");
  153. return 0;
  154. }
  155. adapter->is_suspended = false;
  156. /* Disable Host Sleep */
  157. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  158. MWIFIEX_ASYNC_CMD);
  159. return 0;
  160. }
  161. /*
  162. * SDIO remove.
  163. *
  164. * This function removes the interface and frees up the card structure.
  165. */
  166. static void
  167. mwifiex_sdio_remove(struct sdio_func *func)
  168. {
  169. struct sdio_mmc_card *card;
  170. struct mwifiex_adapter *adapter;
  171. struct mwifiex_private *priv;
  172. card = sdio_get_drvdata(func);
  173. if (!card)
  174. return;
  175. adapter = card->adapter;
  176. if (!adapter || !adapter->priv_num)
  177. return;
  178. mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
  179. if (user_rmmod) {
  180. if (adapter->is_suspended)
  181. mwifiex_sdio_resume(adapter->dev);
  182. mwifiex_deauthenticate_all(adapter);
  183. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  184. mwifiex_disable_auto_ds(priv);
  185. mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
  186. }
  187. mwifiex_remove_card(card->adapter, &add_remove_card_sem);
  188. }
  189. /*
  190. * SDIO suspend.
  191. *
  192. * Kernel needs to suspend all functions separately. Therefore all
  193. * registered functions must have drivers with suspend and resume
  194. * methods. Failing that the kernel simply removes the whole card.
  195. *
  196. * If already not suspended, this function allocates and sends a host
  197. * sleep activate request to the firmware and turns off the traffic.
  198. */
  199. static int mwifiex_sdio_suspend(struct device *dev)
  200. {
  201. struct sdio_func *func = dev_to_sdio_func(dev);
  202. struct sdio_mmc_card *card;
  203. struct mwifiex_adapter *adapter;
  204. mmc_pm_flag_t pm_flag = 0;
  205. int ret = 0;
  206. if (func) {
  207. pm_flag = sdio_get_host_pm_caps(func);
  208. pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
  209. sdio_func_id(func), pm_flag);
  210. if (!(pm_flag & MMC_PM_KEEP_POWER)) {
  211. pr_err("%s: cannot remain alive while host is"
  212. " suspended\n", sdio_func_id(func));
  213. return -ENOSYS;
  214. }
  215. card = sdio_get_drvdata(func);
  216. if (!card || !card->adapter) {
  217. pr_err("suspend: invalid card or adapter\n");
  218. return 0;
  219. }
  220. } else {
  221. pr_err("suspend: sdio_func is not specified\n");
  222. return 0;
  223. }
  224. adapter = card->adapter;
  225. /* Enable the Host Sleep */
  226. if (!mwifiex_enable_hs(adapter)) {
  227. mwifiex_dbg(adapter, ERROR,
  228. "cmd: failed to suspend\n");
  229. adapter->hs_enabling = false;
  230. return -EFAULT;
  231. }
  232. mwifiex_dbg(adapter, INFO,
  233. "cmd: suspend with MMC_PM_KEEP_POWER\n");
  234. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  235. /* Indicate device suspended */
  236. adapter->is_suspended = true;
  237. adapter->hs_enabling = false;
  238. return ret;
  239. }
  240. /* Device ID for SD8786 */
  241. #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
  242. /* Device ID for SD8787 */
  243. #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
  244. /* Device ID for SD8797 */
  245. #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
  246. /* Device ID for SD8897 */
  247. #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
  248. /* Device ID for SD8887 */
  249. #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
  250. /* Device ID for SD8801 */
  251. #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
  252. /* Device ID for SD8997 */
  253. #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
  254. /* WLAN IDs */
  255. static const struct sdio_device_id mwifiex_ids[] = {
  256. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
  257. .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
  258. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
  259. .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
  260. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
  261. .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
  262. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
  263. .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
  264. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
  265. .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
  266. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
  267. .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
  268. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
  269. .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
  270. {},
  271. };
  272. MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
  273. static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
  274. .suspend = mwifiex_sdio_suspend,
  275. .resume = mwifiex_sdio_resume,
  276. };
  277. static struct sdio_driver mwifiex_sdio = {
  278. .name = "mwifiex_sdio",
  279. .id_table = mwifiex_ids,
  280. .probe = mwifiex_sdio_probe,
  281. .remove = mwifiex_sdio_remove,
  282. .drv = {
  283. .owner = THIS_MODULE,
  284. .pm = &mwifiex_sdio_pm_ops,
  285. }
  286. };
  287. /* Write data into SDIO card register. Caller claims SDIO device. */
  288. static int
  289. mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
  290. {
  291. int ret = -1;
  292. sdio_writeb(func, data, reg, &ret);
  293. return ret;
  294. }
  295. /*
  296. * This function writes data into SDIO card register.
  297. */
  298. static int
  299. mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
  300. {
  301. struct sdio_mmc_card *card = adapter->card;
  302. int ret;
  303. sdio_claim_host(card->func);
  304. ret = mwifiex_write_reg_locked(card->func, reg, data);
  305. sdio_release_host(card->func);
  306. return ret;
  307. }
  308. /*
  309. * This function reads data from SDIO card register.
  310. */
  311. static int
  312. mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
  313. {
  314. struct sdio_mmc_card *card = adapter->card;
  315. int ret = -1;
  316. u8 val;
  317. sdio_claim_host(card->func);
  318. val = sdio_readb(card->func, reg, &ret);
  319. sdio_release_host(card->func);
  320. *data = val;
  321. return ret;
  322. }
  323. /*
  324. * This function writes multiple data into SDIO card memory.
  325. *
  326. * This does not work in suspended mode.
  327. */
  328. static int
  329. mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
  330. u8 *buffer, u32 pkt_len, u32 port)
  331. {
  332. struct sdio_mmc_card *card = adapter->card;
  333. int ret;
  334. u8 blk_mode =
  335. (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
  336. u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
  337. u32 blk_cnt =
  338. (blk_mode ==
  339. BLOCK_MODE) ? (pkt_len /
  340. MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
  341. u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
  342. if (adapter->is_suspended) {
  343. mwifiex_dbg(adapter, ERROR,
  344. "%s: not allowed while suspended\n", __func__);
  345. return -1;
  346. }
  347. sdio_claim_host(card->func);
  348. ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
  349. sdio_release_host(card->func);
  350. return ret;
  351. }
  352. /*
  353. * This function reads multiple data from SDIO card memory.
  354. */
  355. static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
  356. u32 len, u32 port, u8 claim)
  357. {
  358. struct sdio_mmc_card *card = adapter->card;
  359. int ret;
  360. u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
  361. : BLOCK_MODE;
  362. u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
  363. u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
  364. : len;
  365. u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
  366. if (claim)
  367. sdio_claim_host(card->func);
  368. ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
  369. if (claim)
  370. sdio_release_host(card->func);
  371. return ret;
  372. }
  373. /*
  374. * This function wakes up the card.
  375. *
  376. * A host power up command is written to the card configuration
  377. * register to wake up the card.
  378. */
  379. static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
  380. {
  381. mwifiex_dbg(adapter, EVENT,
  382. "event: wakeup device...\n");
  383. return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
  384. }
  385. /*
  386. * This function is called after the card has woken up.
  387. *
  388. * The card configuration register is reset.
  389. */
  390. static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
  391. {
  392. mwifiex_dbg(adapter, EVENT,
  393. "cmd: wakeup device completed\n");
  394. return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
  395. }
  396. /*
  397. * This function is used to initialize IO ports for the
  398. * chipsets supporting SDIO new mode eg SD8897.
  399. */
  400. static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
  401. {
  402. u8 reg;
  403. struct sdio_mmc_card *card = adapter->card;
  404. adapter->ioport = MEM_PORT;
  405. /* enable sdio new mode */
  406. if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
  407. return -1;
  408. if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
  409. reg | CMD53_NEW_MODE))
  410. return -1;
  411. /* Configure cmd port and enable reading rx length from the register */
  412. if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
  413. return -1;
  414. if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
  415. reg | CMD_PORT_RD_LEN_EN))
  416. return -1;
  417. /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
  418. * completed
  419. */
  420. if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
  421. return -1;
  422. if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
  423. reg | CMD_PORT_AUTO_EN))
  424. return -1;
  425. return 0;
  426. }
  427. /* This function initializes the IO ports.
  428. *
  429. * The following operations are performed -
  430. * - Read the IO ports (0, 1 and 2)
  431. * - Set host interrupt Reset-To-Read to clear
  432. * - Set auto re-enable interrupt
  433. */
  434. static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
  435. {
  436. u8 reg;
  437. struct sdio_mmc_card *card = adapter->card;
  438. adapter->ioport = 0;
  439. if (card->supports_sdio_new_mode) {
  440. if (mwifiex_init_sdio_new_mode(adapter))
  441. return -1;
  442. goto cont;
  443. }
  444. /* Read the IO port */
  445. if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
  446. adapter->ioport |= (reg & 0xff);
  447. else
  448. return -1;
  449. if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
  450. adapter->ioport |= ((reg & 0xff) << 8);
  451. else
  452. return -1;
  453. if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
  454. adapter->ioport |= ((reg & 0xff) << 16);
  455. else
  456. return -1;
  457. cont:
  458. mwifiex_dbg(adapter, INFO,
  459. "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
  460. /* Set Host interrupt reset to read to clear */
  461. if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
  462. mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
  463. reg | card->reg->sdio_int_mask);
  464. else
  465. return -1;
  466. /* Dnld/Upld ready set to auto reset */
  467. if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
  468. mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
  469. reg | AUTO_RE_ENABLE_INT);
  470. else
  471. return -1;
  472. return 0;
  473. }
  474. /*
  475. * This function sends data to the card.
  476. */
  477. static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
  478. u8 *payload, u32 pkt_len, u32 port)
  479. {
  480. u32 i = 0;
  481. int ret;
  482. do {
  483. ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
  484. if (ret) {
  485. i++;
  486. mwifiex_dbg(adapter, ERROR,
  487. "host_to_card, write iomem\t"
  488. "(%d) failed: %d\n", i, ret);
  489. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  490. mwifiex_dbg(adapter, ERROR,
  491. "write CFG reg failed\n");
  492. ret = -1;
  493. if (i > MAX_WRITE_IOMEM_RETRY)
  494. return ret;
  495. }
  496. } while (ret == -1);
  497. return ret;
  498. }
  499. /*
  500. * This function gets the read port.
  501. *
  502. * If control port bit is set in MP read bitmap, the control port
  503. * is returned, otherwise the current read port is returned and
  504. * the value is increased (provided it does not reach the maximum
  505. * limit, in which case it is reset to 1)
  506. */
  507. static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
  508. {
  509. struct sdio_mmc_card *card = adapter->card;
  510. const struct mwifiex_sdio_card_reg *reg = card->reg;
  511. u32 rd_bitmap = card->mp_rd_bitmap;
  512. mwifiex_dbg(adapter, DATA,
  513. "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
  514. if (card->supports_sdio_new_mode) {
  515. if (!(rd_bitmap & reg->data_port_mask))
  516. return -1;
  517. } else {
  518. if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
  519. return -1;
  520. }
  521. if ((card->has_control_mask) &&
  522. (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
  523. card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
  524. *port = CTRL_PORT;
  525. mwifiex_dbg(adapter, DATA,
  526. "data: port=%d mp_rd_bitmap=0x%08x\n",
  527. *port, card->mp_rd_bitmap);
  528. return 0;
  529. }
  530. if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
  531. return -1;
  532. /* We are now handling the SDIO data ports */
  533. card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
  534. *port = card->curr_rd_port;
  535. if (++card->curr_rd_port == card->max_ports)
  536. card->curr_rd_port = reg->start_rd_port;
  537. mwifiex_dbg(adapter, DATA,
  538. "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
  539. *port, rd_bitmap, card->mp_rd_bitmap);
  540. return 0;
  541. }
  542. /*
  543. * This function gets the write port for data.
  544. *
  545. * The current write port is returned if available and the value is
  546. * increased (provided it does not reach the maximum limit, in which
  547. * case it is reset to 1)
  548. */
  549. static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
  550. {
  551. struct sdio_mmc_card *card = adapter->card;
  552. const struct mwifiex_sdio_card_reg *reg = card->reg;
  553. u32 wr_bitmap = card->mp_wr_bitmap;
  554. mwifiex_dbg(adapter, DATA,
  555. "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
  556. if (!(wr_bitmap & card->mp_data_port_mask)) {
  557. adapter->data_sent = true;
  558. return -EBUSY;
  559. }
  560. if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
  561. card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
  562. *port = card->curr_wr_port;
  563. if (++card->curr_wr_port == card->mp_end_port)
  564. card->curr_wr_port = reg->start_wr_port;
  565. } else {
  566. adapter->data_sent = true;
  567. return -EBUSY;
  568. }
  569. if ((card->has_control_mask) && (*port == CTRL_PORT)) {
  570. mwifiex_dbg(adapter, ERROR,
  571. "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
  572. *port, card->curr_wr_port, wr_bitmap,
  573. card->mp_wr_bitmap);
  574. return -1;
  575. }
  576. mwifiex_dbg(adapter, DATA,
  577. "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
  578. *port, wr_bitmap, card->mp_wr_bitmap);
  579. return 0;
  580. }
  581. /*
  582. * This function polls the card status.
  583. */
  584. static int
  585. mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
  586. {
  587. struct sdio_mmc_card *card = adapter->card;
  588. u32 tries;
  589. u8 cs;
  590. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  591. if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
  592. break;
  593. else if ((cs & bits) == bits)
  594. return 0;
  595. usleep_range(10, 20);
  596. }
  597. mwifiex_dbg(adapter, ERROR,
  598. "poll card status failed, tries = %d\n", tries);
  599. return -1;
  600. }
  601. /*
  602. * This function reads the firmware status.
  603. */
  604. static int
  605. mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
  606. {
  607. struct sdio_mmc_card *card = adapter->card;
  608. const struct mwifiex_sdio_card_reg *reg = card->reg;
  609. u8 fws0, fws1;
  610. if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
  611. return -1;
  612. if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
  613. return -1;
  614. *dat = (u16) ((fws1 << 8) | fws0);
  615. return 0;
  616. }
  617. /*
  618. * This function disables the host interrupt.
  619. *
  620. * The host interrupt mask is read, the disable bit is reset and
  621. * written back to the card host interrupt mask register.
  622. */
  623. static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
  624. {
  625. struct sdio_mmc_card *card = adapter->card;
  626. struct sdio_func *func = card->func;
  627. sdio_claim_host(func);
  628. mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
  629. sdio_release_irq(func);
  630. sdio_release_host(func);
  631. }
  632. /*
  633. * This function reads the interrupt status from card.
  634. */
  635. static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
  636. {
  637. struct sdio_mmc_card *card = adapter->card;
  638. u8 sdio_ireg;
  639. unsigned long flags;
  640. if (mwifiex_read_data_sync(adapter, card->mp_regs,
  641. card->reg->max_mp_regs,
  642. REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
  643. mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
  644. return;
  645. }
  646. sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
  647. if (sdio_ireg) {
  648. /*
  649. * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  650. * For SDIO new mode CMD port interrupts
  651. * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
  652. * UP_LD_CMD_PORT_HOST_INT_STATUS
  653. * Clear the interrupt status register
  654. */
  655. mwifiex_dbg(adapter, INTR,
  656. "int: sdio_ireg = %#x\n", sdio_ireg);
  657. spin_lock_irqsave(&adapter->int_lock, flags);
  658. adapter->int_status |= sdio_ireg;
  659. spin_unlock_irqrestore(&adapter->int_lock, flags);
  660. }
  661. }
  662. /*
  663. * SDIO interrupt handler.
  664. *
  665. * This function reads the interrupt status from firmware and handles
  666. * the interrupt in current thread (ksdioirqd) right away.
  667. */
  668. static void
  669. mwifiex_sdio_interrupt(struct sdio_func *func)
  670. {
  671. struct mwifiex_adapter *adapter;
  672. struct sdio_mmc_card *card;
  673. card = sdio_get_drvdata(func);
  674. if (!card || !card->adapter) {
  675. pr_debug("int: func=%p card=%p adapter=%p\n",
  676. func, card, card ? card->adapter : NULL);
  677. return;
  678. }
  679. adapter = card->adapter;
  680. if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
  681. adapter->ps_state = PS_STATE_AWAKE;
  682. mwifiex_interrupt_status(adapter);
  683. mwifiex_main_process(adapter);
  684. }
  685. /*
  686. * This function enables the host interrupt.
  687. *
  688. * The host interrupt enable mask is written to the card
  689. * host interrupt mask register.
  690. */
  691. static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
  692. {
  693. struct sdio_mmc_card *card = adapter->card;
  694. struct sdio_func *func = card->func;
  695. int ret;
  696. sdio_claim_host(func);
  697. /* Request the SDIO IRQ */
  698. ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
  699. if (ret) {
  700. mwifiex_dbg(adapter, ERROR,
  701. "claim irq failed: ret=%d\n", ret);
  702. goto out;
  703. }
  704. /* Simply write the mask to the register */
  705. ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
  706. card->reg->host_int_enable);
  707. if (ret) {
  708. mwifiex_dbg(adapter, ERROR,
  709. "enable host interrupt failed\n");
  710. sdio_release_irq(func);
  711. }
  712. out:
  713. sdio_release_host(func);
  714. return ret;
  715. }
  716. /*
  717. * This function sends a data buffer to the card.
  718. */
  719. static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
  720. u32 *type, u8 *buffer,
  721. u32 npayload, u32 ioport)
  722. {
  723. int ret;
  724. u32 nb;
  725. if (!buffer) {
  726. mwifiex_dbg(adapter, ERROR,
  727. "%s: buffer is NULL\n", __func__);
  728. return -1;
  729. }
  730. ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
  731. if (ret) {
  732. mwifiex_dbg(adapter, ERROR,
  733. "%s: read iomem failed: %d\n", __func__,
  734. ret);
  735. return -1;
  736. }
  737. nb = le16_to_cpu(*(__le16 *) (buffer));
  738. if (nb > npayload) {
  739. mwifiex_dbg(adapter, ERROR,
  740. "%s: invalid packet, nb=%d npayload=%d\n",
  741. __func__, nb, npayload);
  742. return -1;
  743. }
  744. *type = le16_to_cpu(*(__le16 *) (buffer + 2));
  745. return ret;
  746. }
  747. /*
  748. * This function downloads the firmware to the card.
  749. *
  750. * Firmware is downloaded to the card in blocks. Every block download
  751. * is tested for CRC errors, and retried a number of times before
  752. * returning failure.
  753. */
  754. static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
  755. struct mwifiex_fw_image *fw)
  756. {
  757. struct sdio_mmc_card *card = adapter->card;
  758. const struct mwifiex_sdio_card_reg *reg = card->reg;
  759. int ret;
  760. u8 *firmware = fw->fw_buf;
  761. u32 firmware_len = fw->fw_len;
  762. u32 offset = 0;
  763. u8 base0, base1;
  764. u8 *fwbuf;
  765. u16 len = 0;
  766. u32 txlen, tx_blocks = 0, tries;
  767. u32 i = 0;
  768. if (!firmware_len) {
  769. mwifiex_dbg(adapter, ERROR,
  770. "firmware image not found! Terminating download\n");
  771. return -1;
  772. }
  773. mwifiex_dbg(adapter, INFO,
  774. "info: downloading FW image (%d bytes)\n",
  775. firmware_len);
  776. /* Assume that the allocated buffer is 8-byte aligned */
  777. fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
  778. if (!fwbuf)
  779. return -ENOMEM;
  780. sdio_claim_host(card->func);
  781. /* Perform firmware data transfer */
  782. do {
  783. /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
  784. bits */
  785. ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
  786. DN_LD_CARD_RDY);
  787. if (ret) {
  788. mwifiex_dbg(adapter, ERROR,
  789. "FW download with helper:\t"
  790. "poll status timeout @ %d\n", offset);
  791. goto done;
  792. }
  793. /* More data? */
  794. if (offset >= firmware_len)
  795. break;
  796. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  797. ret = mwifiex_read_reg(adapter, reg->base_0_reg,
  798. &base0);
  799. if (ret) {
  800. mwifiex_dbg(adapter, ERROR,
  801. "dev BASE0 register read failed:\t"
  802. "base0=%#04X(%d). Terminating dnld\n",
  803. base0, base0);
  804. goto done;
  805. }
  806. ret = mwifiex_read_reg(adapter, reg->base_1_reg,
  807. &base1);
  808. if (ret) {
  809. mwifiex_dbg(adapter, ERROR,
  810. "dev BASE1 register read failed:\t"
  811. "base1=%#04X(%d). Terminating dnld\n",
  812. base1, base1);
  813. goto done;
  814. }
  815. len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
  816. if (len)
  817. break;
  818. usleep_range(10, 20);
  819. }
  820. if (!len) {
  821. break;
  822. } else if (len > MWIFIEX_UPLD_SIZE) {
  823. mwifiex_dbg(adapter, ERROR,
  824. "FW dnld failed @ %d, invalid length %d\n",
  825. offset, len);
  826. ret = -1;
  827. goto done;
  828. }
  829. txlen = len;
  830. if (len & BIT(0)) {
  831. i++;
  832. if (i > MAX_WRITE_IOMEM_RETRY) {
  833. mwifiex_dbg(adapter, ERROR,
  834. "FW dnld failed @ %d, over max retry\n",
  835. offset);
  836. ret = -1;
  837. goto done;
  838. }
  839. mwifiex_dbg(adapter, ERROR,
  840. "CRC indicated by the helper:\t"
  841. "len = 0x%04X, txlen = %d\n", len, txlen);
  842. len &= ~BIT(0);
  843. /* Setting this to 0 to resend from same offset */
  844. txlen = 0;
  845. } else {
  846. i = 0;
  847. /* Set blocksize to transfer - checking for last
  848. block */
  849. if (firmware_len - offset < txlen)
  850. txlen = firmware_len - offset;
  851. tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
  852. / MWIFIEX_SDIO_BLOCK_SIZE;
  853. /* Copy payload to buffer */
  854. memmove(fwbuf, &firmware[offset], txlen);
  855. }
  856. ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
  857. MWIFIEX_SDIO_BLOCK_SIZE,
  858. adapter->ioport);
  859. if (ret) {
  860. mwifiex_dbg(adapter, ERROR,
  861. "FW download, write iomem (%d) failed @ %d\n",
  862. i, offset);
  863. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  864. mwifiex_dbg(adapter, ERROR,
  865. "write CFG reg failed\n");
  866. ret = -1;
  867. goto done;
  868. }
  869. offset += txlen;
  870. } while (true);
  871. sdio_release_host(card->func);
  872. mwifiex_dbg(adapter, MSG,
  873. "info: FW download over, size %d bytes\n", offset);
  874. ret = 0;
  875. done:
  876. kfree(fwbuf);
  877. return ret;
  878. }
  879. /*
  880. * This function checks the firmware status in card.
  881. *
  882. * The winner interface is also determined by this function.
  883. */
  884. static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
  885. u32 poll_num)
  886. {
  887. struct sdio_mmc_card *card = adapter->card;
  888. int ret = 0;
  889. u16 firmware_stat;
  890. u32 tries;
  891. u8 winner_status;
  892. /* Wait for firmware initialization event */
  893. for (tries = 0; tries < poll_num; tries++) {
  894. ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
  895. if (ret)
  896. continue;
  897. if (firmware_stat == FIRMWARE_READY_SDIO) {
  898. ret = 0;
  899. break;
  900. } else {
  901. msleep(100);
  902. ret = -1;
  903. }
  904. }
  905. if (ret) {
  906. if (mwifiex_read_reg
  907. (adapter, card->reg->status_reg_0, &winner_status))
  908. winner_status = 0;
  909. if (winner_status)
  910. adapter->winner = 0;
  911. else
  912. adapter->winner = 1;
  913. }
  914. return ret;
  915. }
  916. /*
  917. * This function decode sdio aggreation pkt.
  918. *
  919. * Based on the the data block size and pkt_len,
  920. * skb data will be decoded to few packets.
  921. */
  922. static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
  923. struct sk_buff *skb)
  924. {
  925. u32 total_pkt_len, pkt_len;
  926. struct sk_buff *skb_deaggr;
  927. u32 pkt_type;
  928. u16 blk_size;
  929. u8 blk_num;
  930. u8 *data;
  931. data = skb->data;
  932. total_pkt_len = skb->len;
  933. while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
  934. if (total_pkt_len < adapter->sdio_rx_block_size)
  935. break;
  936. blk_num = *(data + BLOCK_NUMBER_OFFSET);
  937. blk_size = adapter->sdio_rx_block_size * blk_num;
  938. if (blk_size > total_pkt_len) {
  939. mwifiex_dbg(adapter, ERROR,
  940. "%s: error in blk_size,\t"
  941. "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
  942. __func__, blk_num, blk_size, total_pkt_len);
  943. break;
  944. }
  945. pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
  946. pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
  947. 2));
  948. if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
  949. mwifiex_dbg(adapter, ERROR,
  950. "%s: error in pkt_len,\t"
  951. "pkt_len=%d, blk_size=%d\n",
  952. __func__, pkt_len, blk_size);
  953. break;
  954. }
  955. skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len,
  956. GFP_KERNEL | GFP_DMA);
  957. if (!skb_deaggr)
  958. break;
  959. skb_put(skb_deaggr, pkt_len);
  960. memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
  961. skb_pull(skb_deaggr, INTF_HEADER_LEN);
  962. mwifiex_handle_rx_packet(adapter, skb_deaggr);
  963. data += blk_size;
  964. total_pkt_len -= blk_size;
  965. }
  966. }
  967. /*
  968. * This function decodes a received packet.
  969. *
  970. * Based on the type, the packet is treated as either a data, or
  971. * a command response, or an event, and the correct handler
  972. * function is invoked.
  973. */
  974. static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
  975. struct sk_buff *skb, u32 upld_typ)
  976. {
  977. u8 *cmd_buf;
  978. __le16 *curr_ptr = (__le16 *)skb->data;
  979. u16 pkt_len = le16_to_cpu(*curr_ptr);
  980. struct mwifiex_rxinfo *rx_info;
  981. if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
  982. skb_trim(skb, pkt_len);
  983. skb_pull(skb, INTF_HEADER_LEN);
  984. }
  985. switch (upld_typ) {
  986. case MWIFIEX_TYPE_AGGR_DATA:
  987. mwifiex_dbg(adapter, INFO,
  988. "info: --- Rx: Aggr Data packet ---\n");
  989. rx_info = MWIFIEX_SKB_RXCB(skb);
  990. rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
  991. if (adapter->rx_work_enabled) {
  992. skb_queue_tail(&adapter->rx_data_q, skb);
  993. atomic_inc(&adapter->rx_pending);
  994. adapter->data_received = true;
  995. } else {
  996. mwifiex_deaggr_sdio_pkt(adapter, skb);
  997. dev_kfree_skb_any(skb);
  998. }
  999. break;
  1000. case MWIFIEX_TYPE_DATA:
  1001. mwifiex_dbg(adapter, DATA,
  1002. "info: --- Rx: Data packet ---\n");
  1003. if (adapter->rx_work_enabled) {
  1004. skb_queue_tail(&adapter->rx_data_q, skb);
  1005. adapter->data_received = true;
  1006. atomic_inc(&adapter->rx_pending);
  1007. } else {
  1008. mwifiex_handle_rx_packet(adapter, skb);
  1009. }
  1010. break;
  1011. case MWIFIEX_TYPE_CMD:
  1012. mwifiex_dbg(adapter, CMD,
  1013. "info: --- Rx: Cmd Response ---\n");
  1014. /* take care of curr_cmd = NULL case */
  1015. if (!adapter->curr_cmd) {
  1016. cmd_buf = adapter->upld_buf;
  1017. if (adapter->ps_state == PS_STATE_SLEEP_CFM)
  1018. mwifiex_process_sleep_confirm_resp(adapter,
  1019. skb->data,
  1020. skb->len);
  1021. memcpy(cmd_buf, skb->data,
  1022. min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
  1023. skb->len));
  1024. dev_kfree_skb_any(skb);
  1025. } else {
  1026. adapter->cmd_resp_received = true;
  1027. adapter->curr_cmd->resp_skb = skb;
  1028. }
  1029. break;
  1030. case MWIFIEX_TYPE_EVENT:
  1031. mwifiex_dbg(adapter, EVENT,
  1032. "info: --- Rx: Event ---\n");
  1033. adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
  1034. if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
  1035. memcpy(adapter->event_body,
  1036. skb->data + MWIFIEX_EVENT_HEADER_LEN,
  1037. skb->len);
  1038. /* event cause has been saved to adapter->event_cause */
  1039. adapter->event_received = true;
  1040. adapter->event_skb = skb;
  1041. break;
  1042. default:
  1043. mwifiex_dbg(adapter, ERROR,
  1044. "unknown upload type %#x\n", upld_typ);
  1045. dev_kfree_skb_any(skb);
  1046. break;
  1047. }
  1048. return 0;
  1049. }
  1050. /*
  1051. * This function transfers received packets from card to driver, performing
  1052. * aggregation if required.
  1053. *
  1054. * For data received on control port, or if aggregation is disabled, the
  1055. * received buffers are uploaded as separate packets. However, if aggregation
  1056. * is enabled and required, the buffers are copied onto an aggregation buffer,
  1057. * provided there is space left, processed and finally uploaded.
  1058. */
  1059. static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
  1060. u16 rx_len, u8 port)
  1061. {
  1062. struct sdio_mmc_card *card = adapter->card;
  1063. s32 f_do_rx_aggr = 0;
  1064. s32 f_do_rx_cur = 0;
  1065. s32 f_aggr_cur = 0;
  1066. s32 f_post_aggr_cur = 0;
  1067. struct sk_buff *skb_deaggr;
  1068. struct sk_buff *skb = NULL;
  1069. u32 pkt_len, pkt_type, mport, pind;
  1070. u8 *curr_ptr;
  1071. if ((card->has_control_mask) && (port == CTRL_PORT)) {
  1072. /* Read the command Resp without aggr */
  1073. mwifiex_dbg(adapter, CMD,
  1074. "info: %s: no aggregation for cmd\t"
  1075. "response\n", __func__);
  1076. f_do_rx_cur = 1;
  1077. goto rx_curr_single;
  1078. }
  1079. if (!card->mpa_rx.enabled) {
  1080. mwifiex_dbg(adapter, WARN,
  1081. "info: %s: rx aggregation disabled\n",
  1082. __func__);
  1083. f_do_rx_cur = 1;
  1084. goto rx_curr_single;
  1085. }
  1086. if ((!card->has_control_mask && (card->mp_rd_bitmap &
  1087. card->reg->data_port_mask)) ||
  1088. (card->has_control_mask && (card->mp_rd_bitmap &
  1089. (~((u32) CTRL_PORT_MASK))))) {
  1090. /* Some more data RX pending */
  1091. mwifiex_dbg(adapter, INFO,
  1092. "info: %s: not last packet\n", __func__);
  1093. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  1094. if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
  1095. f_aggr_cur = 1;
  1096. } else {
  1097. /* No room in Aggr buf, do rx aggr now */
  1098. f_do_rx_aggr = 1;
  1099. f_post_aggr_cur = 1;
  1100. }
  1101. } else {
  1102. /* Rx aggr not in progress */
  1103. f_aggr_cur = 1;
  1104. }
  1105. } else {
  1106. /* No more data RX pending */
  1107. mwifiex_dbg(adapter, INFO,
  1108. "info: %s: last packet\n", __func__);
  1109. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  1110. f_do_rx_aggr = 1;
  1111. if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
  1112. f_aggr_cur = 1;
  1113. else
  1114. /* No room in Aggr buf, do rx aggr now */
  1115. f_do_rx_cur = 1;
  1116. } else {
  1117. f_do_rx_cur = 1;
  1118. }
  1119. }
  1120. if (f_aggr_cur) {
  1121. mwifiex_dbg(adapter, INFO,
  1122. "info: current packet aggregation\n");
  1123. /* Curr pkt can be aggregated */
  1124. mp_rx_aggr_setup(card, rx_len, port);
  1125. if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
  1126. mp_rx_aggr_port_limit_reached(card)) {
  1127. mwifiex_dbg(adapter, INFO,
  1128. "info: %s: aggregated packet\t"
  1129. "limit reached\n", __func__);
  1130. /* No more pkts allowed in Aggr buf, rx it */
  1131. f_do_rx_aggr = 1;
  1132. }
  1133. }
  1134. if (f_do_rx_aggr) {
  1135. /* do aggr RX now */
  1136. mwifiex_dbg(adapter, DATA,
  1137. "info: do_rx_aggr: num of packets: %d\n",
  1138. card->mpa_rx.pkt_cnt);
  1139. if (card->supports_sdio_new_mode) {
  1140. int i;
  1141. u32 port_count;
  1142. for (i = 0, port_count = 0; i < card->max_ports; i++)
  1143. if (card->mpa_rx.ports & BIT(i))
  1144. port_count++;
  1145. /* Reading data from "start_port + 0" to "start_port +
  1146. * port_count -1", so decrease the count by 1
  1147. */
  1148. port_count--;
  1149. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1150. (port_count << 8)) + card->mpa_rx.start_port;
  1151. } else {
  1152. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1153. (card->mpa_rx.ports << 4)) +
  1154. card->mpa_rx.start_port;
  1155. }
  1156. if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
  1157. card->mpa_rx.buf_len, mport, 1))
  1158. goto error;
  1159. curr_ptr = card->mpa_rx.buf;
  1160. for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
  1161. u32 *len_arr = card->mpa_rx.len_arr;
  1162. /* get curr PKT len & type */
  1163. pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
  1164. pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
  1165. /* copy pkt to deaggr buf */
  1166. skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
  1167. GFP_KERNEL |
  1168. GFP_DMA);
  1169. if (!skb_deaggr) {
  1170. mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
  1171. "drop pkt len=%d type=%d\n",
  1172. pkt_len, pkt_type);
  1173. curr_ptr += len_arr[pind];
  1174. continue;
  1175. }
  1176. skb_put(skb_deaggr, len_arr[pind]);
  1177. if ((pkt_type == MWIFIEX_TYPE_DATA ||
  1178. (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
  1179. adapter->sdio_rx_aggr_enable)) &&
  1180. (pkt_len <= len_arr[pind])) {
  1181. memcpy(skb_deaggr->data, curr_ptr, pkt_len);
  1182. skb_trim(skb_deaggr, pkt_len);
  1183. /* Process de-aggr packet */
  1184. mwifiex_decode_rx_packet(adapter, skb_deaggr,
  1185. pkt_type);
  1186. } else {
  1187. mwifiex_dbg(adapter, ERROR,
  1188. "drop wrong aggr pkt:\t"
  1189. "sdio_single_port_rx_aggr=%d\t"
  1190. "type=%d len=%d max_len=%d\n",
  1191. adapter->sdio_rx_aggr_enable,
  1192. pkt_type, pkt_len, len_arr[pind]);
  1193. dev_kfree_skb_any(skb_deaggr);
  1194. }
  1195. curr_ptr += len_arr[pind];
  1196. }
  1197. MP_RX_AGGR_BUF_RESET(card);
  1198. }
  1199. rx_curr_single:
  1200. if (f_do_rx_cur) {
  1201. mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
  1202. port, rx_len);
  1203. skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
  1204. if (!skb) {
  1205. mwifiex_dbg(adapter, ERROR,
  1206. "single skb allocated fail,\t"
  1207. "drop pkt port=%d len=%d\n", port, rx_len);
  1208. if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
  1209. card->mpa_rx.buf, rx_len,
  1210. adapter->ioport + port))
  1211. goto error;
  1212. return 0;
  1213. }
  1214. skb_put(skb, rx_len);
  1215. if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
  1216. skb->data, skb->len,
  1217. adapter->ioport + port))
  1218. goto error;
  1219. if (!adapter->sdio_rx_aggr_enable &&
  1220. pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
  1221. mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
  1222. "current SDIO RX Aggr not enabled\n",
  1223. pkt_type);
  1224. dev_kfree_skb_any(skb);
  1225. return 0;
  1226. }
  1227. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  1228. }
  1229. if (f_post_aggr_cur) {
  1230. mwifiex_dbg(adapter, INFO,
  1231. "info: current packet aggregation\n");
  1232. /* Curr pkt can be aggregated */
  1233. mp_rx_aggr_setup(card, rx_len, port);
  1234. }
  1235. return 0;
  1236. error:
  1237. if (MP_RX_AGGR_IN_PROGRESS(card))
  1238. MP_RX_AGGR_BUF_RESET(card);
  1239. if (f_do_rx_cur && skb)
  1240. /* Single transfer pending. Free curr buff also */
  1241. dev_kfree_skb_any(skb);
  1242. return -1;
  1243. }
  1244. /*
  1245. * This function checks the current interrupt status.
  1246. *
  1247. * The following interrupts are checked and handled by this function -
  1248. * - Data sent
  1249. * - Command sent
  1250. * - Packets received
  1251. *
  1252. * Since the firmware does not generate download ready interrupt if the
  1253. * port updated is command port only, command sent interrupt checking
  1254. * should be done manually, and for every SDIO interrupt.
  1255. *
  1256. * In case of Rx packets received, the packets are uploaded from card to
  1257. * host and processed accordingly.
  1258. */
  1259. static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
  1260. {
  1261. struct sdio_mmc_card *card = adapter->card;
  1262. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1263. int ret = 0;
  1264. u8 sdio_ireg;
  1265. struct sk_buff *skb;
  1266. u8 port = CTRL_PORT;
  1267. u32 len_reg_l, len_reg_u;
  1268. u32 rx_blocks;
  1269. u16 rx_len;
  1270. unsigned long flags;
  1271. u32 bitmap;
  1272. u8 cr;
  1273. spin_lock_irqsave(&adapter->int_lock, flags);
  1274. sdio_ireg = adapter->int_status;
  1275. adapter->int_status = 0;
  1276. spin_unlock_irqrestore(&adapter->int_lock, flags);
  1277. if (!sdio_ireg)
  1278. return ret;
  1279. /* Following interrupt is only for SDIO new mode */
  1280. if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
  1281. adapter->cmd_sent = false;
  1282. /* Following interrupt is only for SDIO new mode */
  1283. if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
  1284. u32 pkt_type;
  1285. /* read the len of control packet */
  1286. rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
  1287. rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
  1288. rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
  1289. if (rx_len <= INTF_HEADER_LEN ||
  1290. (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1291. MWIFIEX_RX_DATA_BUF_SIZE)
  1292. return -1;
  1293. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1294. mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
  1295. skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
  1296. if (!skb)
  1297. return -1;
  1298. skb_put(skb, rx_len);
  1299. if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
  1300. skb->len, adapter->ioport |
  1301. CMD_PORT_SLCT)) {
  1302. mwifiex_dbg(adapter, ERROR,
  1303. "%s: failed to card_to_host", __func__);
  1304. dev_kfree_skb_any(skb);
  1305. goto term_cmd;
  1306. }
  1307. if ((pkt_type != MWIFIEX_TYPE_CMD) &&
  1308. (pkt_type != MWIFIEX_TYPE_EVENT))
  1309. mwifiex_dbg(adapter, ERROR,
  1310. "%s:Received wrong packet on cmd port",
  1311. __func__);
  1312. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  1313. }
  1314. if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
  1315. bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
  1316. bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
  1317. if (card->supports_sdio_new_mode) {
  1318. bitmap |=
  1319. ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
  1320. bitmap |=
  1321. ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
  1322. }
  1323. card->mp_wr_bitmap = bitmap;
  1324. mwifiex_dbg(adapter, INTR,
  1325. "int: DNLD: wr_bitmap=0x%x\n",
  1326. card->mp_wr_bitmap);
  1327. if (adapter->data_sent &&
  1328. (card->mp_wr_bitmap & card->mp_data_port_mask)) {
  1329. mwifiex_dbg(adapter, INTR,
  1330. "info: <--- Tx DONE Interrupt --->\n");
  1331. adapter->data_sent = false;
  1332. }
  1333. }
  1334. /* As firmware will not generate download ready interrupt if the port
  1335. updated is command port only, cmd_sent should be done for any SDIO
  1336. interrupt. */
  1337. if (card->has_control_mask && adapter->cmd_sent) {
  1338. /* Check if firmware has attach buffer at command port and
  1339. update just that in wr_bit_map. */
  1340. card->mp_wr_bitmap |=
  1341. (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
  1342. if (card->mp_wr_bitmap & CTRL_PORT_MASK)
  1343. adapter->cmd_sent = false;
  1344. }
  1345. mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
  1346. adapter->cmd_sent, adapter->data_sent);
  1347. if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
  1348. bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
  1349. bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
  1350. if (card->supports_sdio_new_mode) {
  1351. bitmap |=
  1352. ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
  1353. bitmap |=
  1354. ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
  1355. }
  1356. card->mp_rd_bitmap = bitmap;
  1357. mwifiex_dbg(adapter, INTR,
  1358. "int: UPLD: rd_bitmap=0x%x\n",
  1359. card->mp_rd_bitmap);
  1360. while (true) {
  1361. ret = mwifiex_get_rd_port(adapter, &port);
  1362. if (ret) {
  1363. mwifiex_dbg(adapter, INFO,
  1364. "info: no more rd_port available\n");
  1365. break;
  1366. }
  1367. len_reg_l = reg->rd_len_p0_l + (port << 1);
  1368. len_reg_u = reg->rd_len_p0_u + (port << 1);
  1369. rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
  1370. rx_len |= (u16) card->mp_regs[len_reg_l];
  1371. mwifiex_dbg(adapter, INFO,
  1372. "info: RX: port=%d rx_len=%u\n",
  1373. port, rx_len);
  1374. rx_blocks =
  1375. (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
  1376. 1) / MWIFIEX_SDIO_BLOCK_SIZE;
  1377. if (rx_len <= INTF_HEADER_LEN ||
  1378. (card->mpa_rx.enabled &&
  1379. ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1380. card->mpa_rx.buf_size))) {
  1381. mwifiex_dbg(adapter, ERROR,
  1382. "invalid rx_len=%d\n",
  1383. rx_len);
  1384. return -1;
  1385. }
  1386. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1387. mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
  1388. rx_len);
  1389. if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
  1390. port)) {
  1391. mwifiex_dbg(adapter, ERROR,
  1392. "card_to_host_mpa failed: int status=%#x\n",
  1393. sdio_ireg);
  1394. goto term_cmd;
  1395. }
  1396. }
  1397. }
  1398. return 0;
  1399. term_cmd:
  1400. /* terminate cmd */
  1401. if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
  1402. mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
  1403. else
  1404. mwifiex_dbg(adapter, INFO,
  1405. "info: CFG reg val = %d\n", cr);
  1406. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
  1407. mwifiex_dbg(adapter, ERROR,
  1408. "write CFG reg failed\n");
  1409. else
  1410. mwifiex_dbg(adapter, INFO, "info: write success\n");
  1411. if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
  1412. mwifiex_dbg(adapter, ERROR,
  1413. "read CFG reg failed\n");
  1414. else
  1415. mwifiex_dbg(adapter, INFO,
  1416. "info: CFG reg val =%x\n", cr);
  1417. return -1;
  1418. }
  1419. /*
  1420. * This function aggregates transmission buffers in driver and downloads
  1421. * the aggregated packet to card.
  1422. *
  1423. * The individual packets are aggregated by copying into an aggregation
  1424. * buffer and then downloaded to the card. Previous unsent packets in the
  1425. * aggregation buffer are pre-copied first before new packets are added.
  1426. * Aggregation is done till there is space left in the aggregation buffer,
  1427. * or till new packets are available.
  1428. *
  1429. * The function will only download the packet to the card when aggregation
  1430. * stops, otherwise it will just aggregate the packet in aggregation buffer
  1431. * and return.
  1432. */
  1433. static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
  1434. u8 *payload, u32 pkt_len, u32 port,
  1435. u32 next_pkt_len)
  1436. {
  1437. struct sdio_mmc_card *card = adapter->card;
  1438. int ret = 0;
  1439. s32 f_send_aggr_buf = 0;
  1440. s32 f_send_cur_buf = 0;
  1441. s32 f_precopy_cur_buf = 0;
  1442. s32 f_postcopy_cur_buf = 0;
  1443. u32 mport;
  1444. if (!card->mpa_tx.enabled ||
  1445. (card->has_control_mask && (port == CTRL_PORT)) ||
  1446. (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
  1447. mwifiex_dbg(adapter, WARN,
  1448. "info: %s: tx aggregation disabled\n",
  1449. __func__);
  1450. f_send_cur_buf = 1;
  1451. goto tx_curr_single;
  1452. }
  1453. if (next_pkt_len) {
  1454. /* More pkt in TX queue */
  1455. mwifiex_dbg(adapter, INFO,
  1456. "info: %s: more packets in queue.\n",
  1457. __func__);
  1458. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1459. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
  1460. f_precopy_cur_buf = 1;
  1461. if (!(card->mp_wr_bitmap &
  1462. (1 << card->curr_wr_port)) ||
  1463. !MP_TX_AGGR_BUF_HAS_ROOM(
  1464. card, pkt_len + next_pkt_len))
  1465. f_send_aggr_buf = 1;
  1466. } else {
  1467. /* No room in Aggr buf, send it */
  1468. f_send_aggr_buf = 1;
  1469. if (!(card->mp_wr_bitmap &
  1470. (1 << card->curr_wr_port)))
  1471. f_send_cur_buf = 1;
  1472. else
  1473. f_postcopy_cur_buf = 1;
  1474. }
  1475. } else {
  1476. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
  1477. (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1478. f_precopy_cur_buf = 1;
  1479. else
  1480. f_send_cur_buf = 1;
  1481. }
  1482. } else {
  1483. /* Last pkt in TX queue */
  1484. mwifiex_dbg(adapter, INFO,
  1485. "info: %s: Last packet in Tx Queue.\n",
  1486. __func__);
  1487. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1488. /* some packs in Aggr buf already */
  1489. f_send_aggr_buf = 1;
  1490. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
  1491. f_precopy_cur_buf = 1;
  1492. else
  1493. /* No room in Aggr buf, send it */
  1494. f_send_cur_buf = 1;
  1495. } else {
  1496. f_send_cur_buf = 1;
  1497. }
  1498. }
  1499. if (f_precopy_cur_buf) {
  1500. mwifiex_dbg(adapter, DATA,
  1501. "data: %s: precopy current buffer\n",
  1502. __func__);
  1503. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1504. if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
  1505. mp_tx_aggr_port_limit_reached(card))
  1506. /* No more pkts allowed in Aggr buf, send it */
  1507. f_send_aggr_buf = 1;
  1508. }
  1509. if (f_send_aggr_buf) {
  1510. mwifiex_dbg(adapter, DATA,
  1511. "data: %s: send aggr buffer: %d %d\n",
  1512. __func__, card->mpa_tx.start_port,
  1513. card->mpa_tx.ports);
  1514. if (card->supports_sdio_new_mode) {
  1515. u32 port_count;
  1516. int i;
  1517. for (i = 0, port_count = 0; i < card->max_ports; i++)
  1518. if (card->mpa_tx.ports & BIT(i))
  1519. port_count++;
  1520. /* Writing data from "start_port + 0" to "start_port +
  1521. * port_count -1", so decrease the count by 1
  1522. */
  1523. port_count--;
  1524. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1525. (port_count << 8)) + card->mpa_tx.start_port;
  1526. } else {
  1527. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1528. (card->mpa_tx.ports << 4)) +
  1529. card->mpa_tx.start_port;
  1530. }
  1531. ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
  1532. card->mpa_tx.buf_len, mport);
  1533. MP_TX_AGGR_BUF_RESET(card);
  1534. }
  1535. tx_curr_single:
  1536. if (f_send_cur_buf) {
  1537. mwifiex_dbg(adapter, DATA,
  1538. "data: %s: send current buffer %d\n",
  1539. __func__, port);
  1540. ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
  1541. adapter->ioport + port);
  1542. }
  1543. if (f_postcopy_cur_buf) {
  1544. mwifiex_dbg(adapter, DATA,
  1545. "data: %s: postcopy current buffer\n",
  1546. __func__);
  1547. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1548. }
  1549. return ret;
  1550. }
  1551. /*
  1552. * This function downloads data from driver to card.
  1553. *
  1554. * Both commands and data packets are transferred to the card by this
  1555. * function.
  1556. *
  1557. * This function adds the SDIO specific header to the front of the buffer
  1558. * before transferring. The header contains the length of the packet and
  1559. * the type. The firmware handles the packets based upon this set type.
  1560. */
  1561. static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
  1562. u8 type, struct sk_buff *skb,
  1563. struct mwifiex_tx_param *tx_param)
  1564. {
  1565. struct sdio_mmc_card *card = adapter->card;
  1566. int ret;
  1567. u32 buf_block_len;
  1568. u32 blk_size;
  1569. u32 port = CTRL_PORT;
  1570. u8 *payload = (u8 *)skb->data;
  1571. u32 pkt_len = skb->len;
  1572. /* Allocate buffer and copy payload */
  1573. blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
  1574. buf_block_len = (pkt_len + blk_size - 1) / blk_size;
  1575. *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
  1576. *(__le16 *)&payload[2] = cpu_to_le16(type);
  1577. /*
  1578. * This is SDIO specific header
  1579. * u16 length,
  1580. * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
  1581. * MWIFIEX_TYPE_EVENT = 3)
  1582. */
  1583. if (type == MWIFIEX_TYPE_DATA) {
  1584. ret = mwifiex_get_wr_port_data(adapter, &port);
  1585. if (ret) {
  1586. mwifiex_dbg(adapter, ERROR,
  1587. "%s: no wr_port available\n",
  1588. __func__);
  1589. return ret;
  1590. }
  1591. } else {
  1592. adapter->cmd_sent = true;
  1593. /* Type must be MWIFIEX_TYPE_CMD */
  1594. if (pkt_len <= INTF_HEADER_LEN ||
  1595. pkt_len > MWIFIEX_UPLD_SIZE)
  1596. mwifiex_dbg(adapter, ERROR,
  1597. "%s: payload=%p, nb=%d\n",
  1598. __func__, payload, pkt_len);
  1599. if (card->supports_sdio_new_mode)
  1600. port = CMD_PORT_SLCT;
  1601. }
  1602. /* Transfer data to card */
  1603. pkt_len = buf_block_len * blk_size;
  1604. if (tx_param)
  1605. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1606. port, tx_param->next_pkt_len
  1607. );
  1608. else
  1609. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1610. port, 0);
  1611. if (ret) {
  1612. if (type == MWIFIEX_TYPE_CMD)
  1613. adapter->cmd_sent = false;
  1614. if (type == MWIFIEX_TYPE_DATA) {
  1615. adapter->data_sent = false;
  1616. /* restore curr_wr_port in error cases */
  1617. card->curr_wr_port = port;
  1618. card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
  1619. }
  1620. } else {
  1621. if (type == MWIFIEX_TYPE_DATA) {
  1622. if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1623. adapter->data_sent = true;
  1624. else
  1625. adapter->data_sent = false;
  1626. }
  1627. }
  1628. return ret;
  1629. }
  1630. /*
  1631. * This function allocates the MPA Tx and Rx buffers.
  1632. */
  1633. static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
  1634. u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
  1635. {
  1636. struct sdio_mmc_card *card = adapter->card;
  1637. u32 rx_buf_size;
  1638. int ret = 0;
  1639. card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
  1640. if (!card->mpa_tx.buf) {
  1641. ret = -1;
  1642. goto error;
  1643. }
  1644. card->mpa_tx.buf_size = mpa_tx_buf_size;
  1645. rx_buf_size = max_t(u32, mpa_rx_buf_size,
  1646. (u32)SDIO_MAX_AGGR_BUF_SIZE);
  1647. card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
  1648. if (!card->mpa_rx.buf) {
  1649. ret = -1;
  1650. goto error;
  1651. }
  1652. card->mpa_rx.buf_size = rx_buf_size;
  1653. error:
  1654. if (ret) {
  1655. kfree(card->mpa_tx.buf);
  1656. kfree(card->mpa_rx.buf);
  1657. card->mpa_tx.buf_size = 0;
  1658. card->mpa_rx.buf_size = 0;
  1659. }
  1660. return ret;
  1661. }
  1662. /*
  1663. * This function unregisters the SDIO device.
  1664. *
  1665. * The SDIO IRQ is released, the function is disabled and driver
  1666. * data is set to null.
  1667. */
  1668. static void
  1669. mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  1670. {
  1671. struct sdio_mmc_card *card = adapter->card;
  1672. if (adapter->card) {
  1673. sdio_claim_host(card->func);
  1674. sdio_disable_func(card->func);
  1675. sdio_release_host(card->func);
  1676. }
  1677. }
  1678. /*
  1679. * This function registers the SDIO device.
  1680. *
  1681. * SDIO IRQ is claimed, block size is set and driver data is initialized.
  1682. */
  1683. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  1684. {
  1685. int ret;
  1686. struct sdio_mmc_card *card = adapter->card;
  1687. struct sdio_func *func = card->func;
  1688. /* save adapter pointer in card */
  1689. card->adapter = adapter;
  1690. adapter->tx_buf_size = card->tx_buf_size;
  1691. sdio_claim_host(func);
  1692. /* Set block size */
  1693. ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
  1694. sdio_release_host(func);
  1695. if (ret) {
  1696. mwifiex_dbg(adapter, ERROR,
  1697. "cannot set SDIO block size\n");
  1698. return ret;
  1699. }
  1700. adapter->dev = &func->dev;
  1701. strcpy(adapter->fw_name, card->firmware);
  1702. if (card->fw_dump_enh) {
  1703. adapter->mem_type_mapping_tbl = generic_mem_type_map;
  1704. adapter->num_mem_types = 1;
  1705. } else {
  1706. adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
  1707. adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
  1708. }
  1709. return 0;
  1710. }
  1711. /*
  1712. * This function initializes the SDIO driver.
  1713. *
  1714. * The following initializations steps are followed -
  1715. * - Read the Host interrupt status register to acknowledge
  1716. * the first interrupt got from bootloader
  1717. * - Disable host interrupt mask register
  1718. * - Get SDIO port
  1719. * - Initialize SDIO variables in card
  1720. * - Allocate MP registers
  1721. * - Allocate MPA Tx and Rx buffers
  1722. */
  1723. static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
  1724. {
  1725. struct sdio_mmc_card *card = adapter->card;
  1726. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1727. int ret;
  1728. u8 sdio_ireg;
  1729. sdio_set_drvdata(card->func, card);
  1730. /*
  1731. * Read the host_int_status_reg for ACK the first interrupt got
  1732. * from the bootloader. If we don't do this we get a interrupt
  1733. * as soon as we register the irq.
  1734. */
  1735. mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
  1736. /* Get SDIO ioport */
  1737. mwifiex_init_sdio_ioport(adapter);
  1738. /* Initialize SDIO variables in card */
  1739. card->mp_rd_bitmap = 0;
  1740. card->mp_wr_bitmap = 0;
  1741. card->curr_rd_port = reg->start_rd_port;
  1742. card->curr_wr_port = reg->start_wr_port;
  1743. card->mp_data_port_mask = reg->data_port_mask;
  1744. card->mpa_tx.buf_len = 0;
  1745. card->mpa_tx.pkt_cnt = 0;
  1746. card->mpa_tx.start_port = 0;
  1747. card->mpa_tx.enabled = 1;
  1748. card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
  1749. card->mpa_rx.buf_len = 0;
  1750. card->mpa_rx.pkt_cnt = 0;
  1751. card->mpa_rx.start_port = 0;
  1752. card->mpa_rx.enabled = 1;
  1753. card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
  1754. /* Allocate buffers for SDIO MP-A */
  1755. card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
  1756. if (!card->mp_regs)
  1757. return -ENOMEM;
  1758. /* Allocate skb pointer buffers */
  1759. card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
  1760. card->mp_agg_pkt_limit, GFP_KERNEL);
  1761. card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
  1762. card->mp_agg_pkt_limit, GFP_KERNEL);
  1763. ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
  1764. card->mp_tx_agg_buf_size,
  1765. card->mp_rx_agg_buf_size);
  1766. /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
  1767. if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
  1768. card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
  1769. /* Disable rx single port aggregation */
  1770. adapter->host_disable_sdio_rx_aggr = true;
  1771. ret = mwifiex_alloc_sdio_mpa_buffers
  1772. (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
  1773. MWIFIEX_MP_AGGR_BUF_SIZE_32K);
  1774. if (ret) {
  1775. /* Disable multi port aggregation */
  1776. card->mpa_tx.enabled = 0;
  1777. card->mpa_rx.enabled = 0;
  1778. }
  1779. }
  1780. adapter->auto_tdls = card->can_auto_tdls;
  1781. adapter->ext_scan = card->can_ext_scan;
  1782. return 0;
  1783. }
  1784. /*
  1785. * This function resets the MPA Tx and Rx buffers.
  1786. */
  1787. static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
  1788. {
  1789. struct sdio_mmc_card *card = adapter->card;
  1790. MP_TX_AGGR_BUF_RESET(card);
  1791. MP_RX_AGGR_BUF_RESET(card);
  1792. }
  1793. /*
  1794. * This function cleans up the allocated card buffers.
  1795. *
  1796. * The following are freed by this function -
  1797. * - MP registers
  1798. * - MPA Tx buffer
  1799. * - MPA Rx buffer
  1800. */
  1801. static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
  1802. {
  1803. struct sdio_mmc_card *card = adapter->card;
  1804. kfree(card->mp_regs);
  1805. kfree(card->mpa_rx.skb_arr);
  1806. kfree(card->mpa_rx.len_arr);
  1807. kfree(card->mpa_tx.buf);
  1808. kfree(card->mpa_rx.buf);
  1809. sdio_set_drvdata(card->func, NULL);
  1810. kfree(card);
  1811. }
  1812. /*
  1813. * This function updates the MP end port in card.
  1814. */
  1815. static void
  1816. mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
  1817. {
  1818. struct sdio_mmc_card *card = adapter->card;
  1819. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1820. int i;
  1821. card->mp_end_port = port;
  1822. card->mp_data_port_mask = reg->data_port_mask;
  1823. if (reg->start_wr_port) {
  1824. for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
  1825. card->mp_data_port_mask &=
  1826. ~(1 << (card->max_ports - i));
  1827. }
  1828. card->curr_wr_port = reg->start_wr_port;
  1829. mwifiex_dbg(adapter, CMD,
  1830. "cmd: mp_end_port %d, data port mask 0x%x\n",
  1831. port, card->mp_data_port_mask);
  1832. }
  1833. static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
  1834. {
  1835. struct sdio_func *func = card->func;
  1836. const struct sdio_device_id *device_id = card->device_id;
  1837. /* TODO mmc_hw_reset does not require destroying and re-probing the
  1838. * whole adapter. Hence there was no need to for this rube-goldberg
  1839. * design to reload the fw from an external workqueue. If we don't
  1840. * destroy the adapter we could reload the fw from
  1841. * mwifiex_main_work_queue directly.
  1842. * The real difficulty with fw reset is to restore all the user
  1843. * settings applied through ioctl. By destroying and recreating the
  1844. * adapter, we take the easy way out, since we rely on user space to
  1845. * restore them. We assume that user space will treat the new
  1846. * incarnation of the adapter(interfaces) as if they had been just
  1847. * discovered and initializes them from scratch.
  1848. */
  1849. mwifiex_sdio_remove(func);
  1850. /* power cycle the adapter */
  1851. sdio_claim_host(func);
  1852. mmc_hw_reset(func->card->host);
  1853. sdio_release_host(func);
  1854. mwifiex_sdio_probe(func, device_id);
  1855. }
  1856. static struct mwifiex_adapter *save_adapter;
  1857. static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
  1858. {
  1859. struct sdio_mmc_card *card = adapter->card;
  1860. /* TODO card pointer is unprotected. If the adapter is removed
  1861. * physically, sdio core might trigger mwifiex_sdio_remove, before this
  1862. * workqueue is run, which will destroy the adapter struct. When this
  1863. * workqueue eventually exceutes it will dereference an invalid adapter
  1864. * pointer
  1865. */
  1866. mwifiex_recreate_adapter(card);
  1867. }
  1868. /* This function read/write firmware */
  1869. static enum
  1870. rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
  1871. u8 doneflag)
  1872. {
  1873. struct sdio_mmc_card *card = adapter->card;
  1874. int ret, tries;
  1875. u8 ctrl_data = 0;
  1876. sdio_writeb(card->func, card->reg->fw_dump_host_ready,
  1877. card->reg->fw_dump_ctrl, &ret);
  1878. if (ret) {
  1879. mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
  1880. return RDWR_STATUS_FAILURE;
  1881. }
  1882. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  1883. ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
  1884. &ret);
  1885. if (ret) {
  1886. mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
  1887. return RDWR_STATUS_FAILURE;
  1888. }
  1889. if (ctrl_data == FW_DUMP_DONE)
  1890. break;
  1891. if (doneflag && ctrl_data == doneflag)
  1892. return RDWR_STATUS_DONE;
  1893. if (ctrl_data != card->reg->fw_dump_host_ready) {
  1894. mwifiex_dbg(adapter, WARN,
  1895. "The ctrl reg was changed, re-try again\n");
  1896. sdio_writeb(card->func, card->reg->fw_dump_host_ready,
  1897. card->reg->fw_dump_ctrl, &ret);
  1898. if (ret) {
  1899. mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
  1900. return RDWR_STATUS_FAILURE;
  1901. }
  1902. }
  1903. usleep_range(100, 200);
  1904. }
  1905. if (ctrl_data == card->reg->fw_dump_host_ready) {
  1906. mwifiex_dbg(adapter, ERROR,
  1907. "Fail to pull ctrl_data\n");
  1908. return RDWR_STATUS_FAILURE;
  1909. }
  1910. return RDWR_STATUS_SUCCESS;
  1911. }
  1912. /* This function dump firmware memory to file */
  1913. static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
  1914. {
  1915. struct sdio_mmc_card *card = adapter->card;
  1916. int ret = 0;
  1917. unsigned int reg, reg_start, reg_end;
  1918. u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
  1919. enum rdwr_status stat;
  1920. u32 memory_size;
  1921. if (!card->can_dump_fw)
  1922. return;
  1923. for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
  1924. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1925. if (entry->mem_ptr) {
  1926. vfree(entry->mem_ptr);
  1927. entry->mem_ptr = NULL;
  1928. }
  1929. entry->mem_size = 0;
  1930. }
  1931. mwifiex_pm_wakeup_card(adapter);
  1932. sdio_claim_host(card->func);
  1933. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
  1934. stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
  1935. if (stat == RDWR_STATUS_FAILURE)
  1936. goto done;
  1937. reg = card->reg->fw_dump_start;
  1938. /* Read the number of the memories which will dump */
  1939. dump_num = sdio_readb(card->func, reg, &ret);
  1940. if (ret) {
  1941. mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
  1942. goto done;
  1943. }
  1944. /* Read the length of every memory which will dump */
  1945. for (idx = 0; idx < dump_num; idx++) {
  1946. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1947. stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
  1948. if (stat == RDWR_STATUS_FAILURE)
  1949. goto done;
  1950. memory_size = 0;
  1951. reg = card->reg->fw_dump_start;
  1952. for (i = 0; i < 4; i++) {
  1953. read_reg = sdio_readb(card->func, reg, &ret);
  1954. if (ret) {
  1955. mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
  1956. goto done;
  1957. }
  1958. memory_size |= (read_reg << i*8);
  1959. reg++;
  1960. }
  1961. if (memory_size == 0) {
  1962. mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
  1963. ret = mwifiex_write_reg(adapter,
  1964. card->reg->fw_dump_ctrl,
  1965. FW_DUMP_READ_DONE);
  1966. if (ret) {
  1967. mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
  1968. return;
  1969. }
  1970. break;
  1971. }
  1972. mwifiex_dbg(adapter, DUMP,
  1973. "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
  1974. entry->mem_ptr = vmalloc(memory_size + 1);
  1975. entry->mem_size = memory_size;
  1976. if (!entry->mem_ptr) {
  1977. mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
  1978. entry->mem_name);
  1979. goto done;
  1980. }
  1981. dbg_ptr = entry->mem_ptr;
  1982. end_ptr = dbg_ptr + memory_size;
  1983. doneflag = entry->done_flag;
  1984. mwifiex_dbg(adapter, DUMP,
  1985. "Start %s output, please wait...\n",
  1986. entry->mem_name);
  1987. do {
  1988. stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
  1989. if (stat == RDWR_STATUS_FAILURE)
  1990. goto done;
  1991. reg_start = card->reg->fw_dump_start;
  1992. reg_end = card->reg->fw_dump_end;
  1993. for (reg = reg_start; reg <= reg_end; reg++) {
  1994. *dbg_ptr = sdio_readb(card->func, reg, &ret);
  1995. if (ret) {
  1996. mwifiex_dbg(adapter, ERROR,
  1997. "SDIO read err\n");
  1998. goto done;
  1999. }
  2000. if (dbg_ptr < end_ptr)
  2001. dbg_ptr++;
  2002. else
  2003. mwifiex_dbg(adapter, ERROR,
  2004. "Allocated buf not enough\n");
  2005. }
  2006. if (stat != RDWR_STATUS_DONE)
  2007. continue;
  2008. mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
  2009. entry->mem_name, dbg_ptr - entry->mem_ptr);
  2010. break;
  2011. } while (1);
  2012. }
  2013. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
  2014. done:
  2015. sdio_release_host(card->func);
  2016. }
  2017. static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
  2018. {
  2019. struct sdio_mmc_card *card = adapter->card;
  2020. struct memory_type_mapping *entry = &generic_mem_type_map[0];
  2021. unsigned int reg, reg_start, reg_end;
  2022. u8 start_flag = 0, done_flag = 0;
  2023. u8 *dbg_ptr, *end_ptr;
  2024. enum rdwr_status stat;
  2025. int ret = -1, tries;
  2026. if (!card->fw_dump_enh)
  2027. return;
  2028. if (entry->mem_ptr) {
  2029. vfree(entry->mem_ptr);
  2030. entry->mem_ptr = NULL;
  2031. }
  2032. entry->mem_size = 0;
  2033. mwifiex_pm_wakeup_card(adapter);
  2034. sdio_claim_host(card->func);
  2035. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
  2036. stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
  2037. if (stat == RDWR_STATUS_FAILURE)
  2038. goto done;
  2039. reg_start = card->reg->fw_dump_start;
  2040. reg_end = card->reg->fw_dump_end;
  2041. for (reg = reg_start; reg <= reg_end; reg++) {
  2042. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  2043. start_flag = sdio_readb(card->func, reg, &ret);
  2044. if (ret) {
  2045. mwifiex_dbg(adapter, ERROR,
  2046. "SDIO read err\n");
  2047. goto done;
  2048. }
  2049. if (start_flag == 0)
  2050. break;
  2051. if (tries == MAX_POLL_TRIES) {
  2052. mwifiex_dbg(adapter, ERROR,
  2053. "FW not ready to dump\n");
  2054. ret = -1;
  2055. goto done;
  2056. }
  2057. }
  2058. usleep_range(100, 200);
  2059. }
  2060. entry->mem_ptr = vmalloc(0xf0000 + 1);
  2061. if (!entry->mem_ptr) {
  2062. ret = -1;
  2063. goto done;
  2064. }
  2065. dbg_ptr = entry->mem_ptr;
  2066. entry->mem_size = 0xf0000;
  2067. end_ptr = dbg_ptr + entry->mem_size;
  2068. done_flag = entry->done_flag;
  2069. mwifiex_dbg(adapter, DUMP,
  2070. "Start %s output, please wait...\n", entry->mem_name);
  2071. while (true) {
  2072. stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
  2073. if (stat == RDWR_STATUS_FAILURE)
  2074. goto done;
  2075. for (reg = reg_start; reg <= reg_end; reg++) {
  2076. *dbg_ptr = sdio_readb(card->func, reg, &ret);
  2077. if (ret) {
  2078. mwifiex_dbg(adapter, ERROR,
  2079. "SDIO read err\n");
  2080. goto done;
  2081. }
  2082. dbg_ptr++;
  2083. if (dbg_ptr >= end_ptr) {
  2084. u8 *tmp_ptr;
  2085. tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
  2086. if (!tmp_ptr)
  2087. goto done;
  2088. memcpy(tmp_ptr, entry->mem_ptr,
  2089. entry->mem_size);
  2090. vfree(entry->mem_ptr);
  2091. entry->mem_ptr = tmp_ptr;
  2092. tmp_ptr = NULL;
  2093. dbg_ptr = entry->mem_ptr + entry->mem_size;
  2094. entry->mem_size += 0x4000;
  2095. end_ptr = entry->mem_ptr + entry->mem_size;
  2096. }
  2097. }
  2098. if (stat == RDWR_STATUS_DONE) {
  2099. entry->mem_size = dbg_ptr - entry->mem_ptr;
  2100. mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
  2101. entry->mem_name, entry->mem_size);
  2102. ret = 0;
  2103. break;
  2104. }
  2105. }
  2106. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
  2107. done:
  2108. if (ret) {
  2109. mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
  2110. if (entry->mem_ptr) {
  2111. vfree(entry->mem_ptr);
  2112. entry->mem_ptr = NULL;
  2113. }
  2114. entry->mem_size = 0;
  2115. }
  2116. sdio_release_host(card->func);
  2117. }
  2118. static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
  2119. {
  2120. struct sdio_mmc_card *card = adapter->card;
  2121. mwifiex_drv_info_dump(adapter);
  2122. if (card->fw_dump_enh)
  2123. mwifiex_sdio_generic_fw_dump(adapter);
  2124. else
  2125. mwifiex_sdio_fw_dump(adapter);
  2126. mwifiex_upload_device_dump(adapter);
  2127. }
  2128. static void mwifiex_sdio_work(struct work_struct *work)
  2129. {
  2130. if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
  2131. &iface_work_flags))
  2132. mwifiex_sdio_device_dump_work(save_adapter);
  2133. if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
  2134. &iface_work_flags))
  2135. mwifiex_sdio_card_reset_work(save_adapter);
  2136. }
  2137. static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
  2138. /* This function resets the card */
  2139. static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
  2140. {
  2141. save_adapter = adapter;
  2142. if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
  2143. return;
  2144. set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
  2145. schedule_work(&sdio_work);
  2146. }
  2147. /* This function dumps FW information */
  2148. static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
  2149. {
  2150. save_adapter = adapter;
  2151. if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
  2152. return;
  2153. set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
  2154. schedule_work(&sdio_work);
  2155. }
  2156. /* Function to dump SDIO function registers and SDIO scratch registers in case
  2157. * of FW crash
  2158. */
  2159. static int
  2160. mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
  2161. {
  2162. char *p = drv_buf;
  2163. struct sdio_mmc_card *cardp = adapter->card;
  2164. int ret = 0;
  2165. u8 count, func, data, index = 0, size = 0;
  2166. u8 reg, reg_start, reg_end;
  2167. char buf[256], *ptr;
  2168. if (!p)
  2169. return 0;
  2170. mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
  2171. mwifiex_pm_wakeup_card(adapter);
  2172. sdio_claim_host(cardp->func);
  2173. for (count = 0; count < 5; count++) {
  2174. memset(buf, 0, sizeof(buf));
  2175. ptr = buf;
  2176. switch (count) {
  2177. case 0:
  2178. /* Read the registers of SDIO function0 */
  2179. func = count;
  2180. reg_start = 0;
  2181. reg_end = 9;
  2182. break;
  2183. case 1:
  2184. /* Read the registers of SDIO function1 */
  2185. func = count;
  2186. reg_start = cardp->reg->func1_dump_reg_start;
  2187. reg_end = cardp->reg->func1_dump_reg_end;
  2188. break;
  2189. case 2:
  2190. index = 0;
  2191. func = 1;
  2192. reg_start = cardp->reg->func1_spec_reg_table[index++];
  2193. size = cardp->reg->func1_spec_reg_num;
  2194. reg_end = cardp->reg->func1_spec_reg_table[size-1];
  2195. break;
  2196. default:
  2197. /* Read the scratch registers of SDIO function1 */
  2198. if (count == 4)
  2199. mdelay(100);
  2200. func = 1;
  2201. reg_start = cardp->reg->func1_scratch_reg;
  2202. reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
  2203. }
  2204. if (count != 2)
  2205. ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
  2206. func, reg_start, reg_end);
  2207. else
  2208. ptr += sprintf(ptr, "SDIO Func%d: ", func);
  2209. for (reg = reg_start; reg <= reg_end;) {
  2210. if (func == 0)
  2211. data = sdio_f0_readb(cardp->func, reg, &ret);
  2212. else
  2213. data = sdio_readb(cardp->func, reg, &ret);
  2214. if (count == 2)
  2215. ptr += sprintf(ptr, "(%#x) ", reg);
  2216. if (!ret) {
  2217. ptr += sprintf(ptr, "%02x ", data);
  2218. } else {
  2219. ptr += sprintf(ptr, "ERR");
  2220. break;
  2221. }
  2222. if (count == 2 && reg < reg_end)
  2223. reg = cardp->reg->func1_spec_reg_table[index++];
  2224. else
  2225. reg++;
  2226. }
  2227. mwifiex_dbg(adapter, MSG, "%s\n", buf);
  2228. p += sprintf(p, "%s\n", buf);
  2229. }
  2230. sdio_release_host(cardp->func);
  2231. mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
  2232. return p - drv_buf;
  2233. }
  2234. static struct mwifiex_if_ops sdio_ops = {
  2235. .init_if = mwifiex_init_sdio,
  2236. .cleanup_if = mwifiex_cleanup_sdio,
  2237. .check_fw_status = mwifiex_check_fw_status,
  2238. .prog_fw = mwifiex_prog_fw_w_helper,
  2239. .register_dev = mwifiex_register_dev,
  2240. .unregister_dev = mwifiex_unregister_dev,
  2241. .enable_int = mwifiex_sdio_enable_host_int,
  2242. .disable_int = mwifiex_sdio_disable_host_int,
  2243. .process_int_status = mwifiex_process_int_status,
  2244. .host_to_card = mwifiex_sdio_host_to_card,
  2245. .wakeup = mwifiex_pm_wakeup_card,
  2246. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  2247. /* SDIO specific */
  2248. .update_mp_end_port = mwifiex_update_mp_end_port,
  2249. .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
  2250. .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
  2251. .event_complete = mwifiex_sdio_event_complete,
  2252. .card_reset = mwifiex_sdio_card_reset,
  2253. .reg_dump = mwifiex_sdio_reg_dump,
  2254. .device_dump = mwifiex_sdio_device_dump,
  2255. .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
  2256. };
  2257. /*
  2258. * This function initializes the SDIO driver.
  2259. *
  2260. * This initiates the semaphore and registers the device with
  2261. * SDIO bus.
  2262. */
  2263. static int
  2264. mwifiex_sdio_init_module(void)
  2265. {
  2266. sema_init(&add_remove_card_sem, 1);
  2267. /* Clear the flag in case user removes the card. */
  2268. user_rmmod = 0;
  2269. return sdio_register_driver(&mwifiex_sdio);
  2270. }
  2271. /*
  2272. * This function cleans up the SDIO driver.
  2273. *
  2274. * The following major steps are followed for cleanup -
  2275. * - Resume the device if its suspended
  2276. * - Disconnect the device if connected
  2277. * - Shutdown the firmware
  2278. * - Unregister the device from SDIO bus.
  2279. */
  2280. static void
  2281. mwifiex_sdio_cleanup_module(void)
  2282. {
  2283. if (!down_interruptible(&add_remove_card_sem))
  2284. up(&add_remove_card_sem);
  2285. /* Set the flag as user is removing this module. */
  2286. user_rmmod = 1;
  2287. cancel_work_sync(&sdio_work);
  2288. sdio_unregister_driver(&mwifiex_sdio);
  2289. }
  2290. module_init(mwifiex_sdio_init_module);
  2291. module_exit(mwifiex_sdio_cleanup_module);
  2292. MODULE_AUTHOR("Marvell International Ltd.");
  2293. MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
  2294. MODULE_VERSION(SDIO_VERSION);
  2295. MODULE_LICENSE("GPL v2");
  2296. MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
  2297. MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
  2298. MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
  2299. MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
  2300. MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
  2301. MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);