sdio.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  1. /*
  2. * Marvell Wireless LAN device driver: SDIO specific handling
  3. *
  4. * Copyright (C) 2011, 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 struct semaphore add_remove_card_sem;
  45. static int mwifiex_sdio_resume(struct device *dev);
  46. static void mwifiex_sdio_interrupt(struct sdio_func *func);
  47. /*
  48. * SDIO probe.
  49. *
  50. * This function probes an mwifiex device and registers it. It allocates
  51. * the card structure, enables SDIO function number and initiates the
  52. * device registration and initialization procedure by adding a logical
  53. * interface.
  54. */
  55. static int
  56. mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
  57. {
  58. int ret;
  59. struct sdio_mmc_card *card = NULL;
  60. pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
  61. func->vendor, func->device, func->class, func->num);
  62. card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
  63. if (!card)
  64. return -ENOMEM;
  65. card->func = func;
  66. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  67. if (id->driver_data) {
  68. struct mwifiex_sdio_device *data = (void *)id->driver_data;
  69. card->firmware = data->firmware;
  70. card->reg = data->reg;
  71. card->max_ports = data->max_ports;
  72. card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
  73. card->supports_sdio_new_mode = data->supports_sdio_new_mode;
  74. card->has_control_mask = data->has_control_mask;
  75. }
  76. sdio_claim_host(func);
  77. ret = sdio_enable_func(func);
  78. sdio_release_host(func);
  79. if (ret) {
  80. pr_err("%s: failed to enable function\n", __func__);
  81. kfree(card);
  82. return -EIO;
  83. }
  84. if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
  85. MWIFIEX_SDIO)) {
  86. pr_err("%s: add card failed\n", __func__);
  87. kfree(card);
  88. sdio_claim_host(func);
  89. ret = sdio_disable_func(func);
  90. sdio_release_host(func);
  91. ret = -1;
  92. }
  93. return ret;
  94. }
  95. /*
  96. * SDIO remove.
  97. *
  98. * This function removes the interface and frees up the card structure.
  99. */
  100. static void
  101. mwifiex_sdio_remove(struct sdio_func *func)
  102. {
  103. struct sdio_mmc_card *card;
  104. struct mwifiex_adapter *adapter;
  105. struct mwifiex_private *priv;
  106. int i;
  107. pr_debug("info: SDIO func num=%d\n", func->num);
  108. card = sdio_get_drvdata(func);
  109. if (!card)
  110. return;
  111. adapter = card->adapter;
  112. if (!adapter || !adapter->priv_num)
  113. return;
  114. /* In case driver is removed when asynchronous FW load is in progress */
  115. wait_for_completion(&adapter->fw_load);
  116. if (user_rmmod) {
  117. if (adapter->is_suspended)
  118. mwifiex_sdio_resume(adapter->dev);
  119. for (i = 0; i < adapter->priv_num; i++)
  120. if ((GET_BSS_ROLE(adapter->priv[i]) ==
  121. MWIFIEX_BSS_ROLE_STA) &&
  122. adapter->priv[i]->media_connected)
  123. mwifiex_deauthenticate(adapter->priv[i], NULL);
  124. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  125. mwifiex_disable_auto_ds(priv);
  126. mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
  127. }
  128. mwifiex_remove_card(card->adapter, &add_remove_card_sem);
  129. kfree(card);
  130. }
  131. /*
  132. * SDIO suspend.
  133. *
  134. * Kernel needs to suspend all functions separately. Therefore all
  135. * registered functions must have drivers with suspend and resume
  136. * methods. Failing that the kernel simply removes the whole card.
  137. *
  138. * If already not suspended, this function allocates and sends a host
  139. * sleep activate request to the firmware and turns off the traffic.
  140. */
  141. static int mwifiex_sdio_suspend(struct device *dev)
  142. {
  143. struct sdio_func *func = dev_to_sdio_func(dev);
  144. struct sdio_mmc_card *card;
  145. struct mwifiex_adapter *adapter;
  146. mmc_pm_flag_t pm_flag = 0;
  147. int ret = 0;
  148. if (func) {
  149. pm_flag = sdio_get_host_pm_caps(func);
  150. pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
  151. sdio_func_id(func), pm_flag);
  152. if (!(pm_flag & MMC_PM_KEEP_POWER)) {
  153. pr_err("%s: cannot remain alive while host is"
  154. " suspended\n", sdio_func_id(func));
  155. return -ENOSYS;
  156. }
  157. card = sdio_get_drvdata(func);
  158. if (!card || !card->adapter) {
  159. pr_err("suspend: invalid card or adapter\n");
  160. return 0;
  161. }
  162. } else {
  163. pr_err("suspend: sdio_func is not specified\n");
  164. return 0;
  165. }
  166. adapter = card->adapter;
  167. /* Enable the Host Sleep */
  168. if (!mwifiex_enable_hs(adapter)) {
  169. dev_err(adapter->dev, "cmd: failed to suspend\n");
  170. return -EFAULT;
  171. }
  172. dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
  173. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  174. /* Indicate device suspended */
  175. adapter->is_suspended = true;
  176. return ret;
  177. }
  178. /*
  179. * SDIO resume.
  180. *
  181. * Kernel needs to suspend all functions separately. Therefore all
  182. * registered functions must have drivers with suspend and resume
  183. * methods. Failing that the kernel simply removes the whole card.
  184. *
  185. * If already not resumed, this function turns on the traffic and
  186. * sends a host sleep cancel request to the firmware.
  187. */
  188. static int mwifiex_sdio_resume(struct device *dev)
  189. {
  190. struct sdio_func *func = dev_to_sdio_func(dev);
  191. struct sdio_mmc_card *card;
  192. struct mwifiex_adapter *adapter;
  193. mmc_pm_flag_t pm_flag = 0;
  194. if (func) {
  195. pm_flag = sdio_get_host_pm_caps(func);
  196. card = sdio_get_drvdata(func);
  197. if (!card || !card->adapter) {
  198. pr_err("resume: invalid card or adapter\n");
  199. return 0;
  200. }
  201. } else {
  202. pr_err("resume: sdio_func is not specified\n");
  203. return 0;
  204. }
  205. adapter = card->adapter;
  206. if (!adapter->is_suspended) {
  207. dev_warn(adapter->dev, "device already resumed\n");
  208. return 0;
  209. }
  210. adapter->is_suspended = false;
  211. /* Disable Host Sleep */
  212. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  213. MWIFIEX_ASYNC_CMD);
  214. return 0;
  215. }
  216. /* Device ID for SD8786 */
  217. #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
  218. /* Device ID for SD8787 */
  219. #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
  220. /* Device ID for SD8797 */
  221. #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
  222. /* Device ID for SD8897 */
  223. #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
  224. /* WLAN IDs */
  225. static const struct sdio_device_id mwifiex_ids[] = {
  226. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
  227. .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
  228. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
  229. .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
  230. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
  231. .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
  232. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
  233. .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
  234. {},
  235. };
  236. MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
  237. static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
  238. .suspend = mwifiex_sdio_suspend,
  239. .resume = mwifiex_sdio_resume,
  240. };
  241. static struct sdio_driver mwifiex_sdio = {
  242. .name = "mwifiex_sdio",
  243. .id_table = mwifiex_ids,
  244. .probe = mwifiex_sdio_probe,
  245. .remove = mwifiex_sdio_remove,
  246. .drv = {
  247. .owner = THIS_MODULE,
  248. .pm = &mwifiex_sdio_pm_ops,
  249. }
  250. };
  251. /* Write data into SDIO card register. Caller claims SDIO device. */
  252. static int
  253. mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
  254. {
  255. int ret = -1;
  256. sdio_writeb(func, data, reg, &ret);
  257. return ret;
  258. }
  259. /*
  260. * This function writes data into SDIO card register.
  261. */
  262. static int
  263. mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
  264. {
  265. struct sdio_mmc_card *card = adapter->card;
  266. int ret;
  267. sdio_claim_host(card->func);
  268. ret = mwifiex_write_reg_locked(card->func, reg, data);
  269. sdio_release_host(card->func);
  270. return ret;
  271. }
  272. /*
  273. * This function reads data from SDIO card register.
  274. */
  275. static int
  276. mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
  277. {
  278. struct sdio_mmc_card *card = adapter->card;
  279. int ret = -1;
  280. u8 val;
  281. sdio_claim_host(card->func);
  282. val = sdio_readb(card->func, reg, &ret);
  283. sdio_release_host(card->func);
  284. *data = val;
  285. return ret;
  286. }
  287. /*
  288. * This function writes multiple data into SDIO card memory.
  289. *
  290. * This does not work in suspended mode.
  291. */
  292. static int
  293. mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
  294. u8 *buffer, u32 pkt_len, u32 port)
  295. {
  296. struct sdio_mmc_card *card = adapter->card;
  297. int ret;
  298. u8 blk_mode =
  299. (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
  300. u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
  301. u32 blk_cnt =
  302. (blk_mode ==
  303. BLOCK_MODE) ? (pkt_len /
  304. MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
  305. u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
  306. if (adapter->is_suspended) {
  307. dev_err(adapter->dev,
  308. "%s: not allowed while suspended\n", __func__);
  309. return -1;
  310. }
  311. sdio_claim_host(card->func);
  312. ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
  313. sdio_release_host(card->func);
  314. return ret;
  315. }
  316. /*
  317. * This function reads multiple data from SDIO card memory.
  318. */
  319. static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
  320. u32 len, u32 port, u8 claim)
  321. {
  322. struct sdio_mmc_card *card = adapter->card;
  323. int ret;
  324. u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
  325. : BLOCK_MODE;
  326. u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
  327. u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
  328. : len;
  329. u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
  330. if (claim)
  331. sdio_claim_host(card->func);
  332. ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
  333. if (claim)
  334. sdio_release_host(card->func);
  335. return ret;
  336. }
  337. /*
  338. * This function wakes up the card.
  339. *
  340. * A host power up command is written to the card configuration
  341. * register to wake up the card.
  342. */
  343. static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
  344. {
  345. dev_dbg(adapter->dev, "event: wakeup device...\n");
  346. return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
  347. }
  348. /*
  349. * This function is called after the card has woken up.
  350. *
  351. * The card configuration register is reset.
  352. */
  353. static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
  354. {
  355. dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
  356. return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
  357. }
  358. /*
  359. * This function is used to initialize IO ports for the
  360. * chipsets supporting SDIO new mode eg SD8897.
  361. */
  362. static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
  363. {
  364. u8 reg;
  365. adapter->ioport = MEM_PORT;
  366. /* enable sdio new mode */
  367. if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
  368. return -1;
  369. if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
  370. reg | CMD53_NEW_MODE))
  371. return -1;
  372. /* Configure cmd port and enable reading rx length from the register */
  373. if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
  374. return -1;
  375. if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
  376. return -1;
  377. /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
  378. * completed
  379. */
  380. if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
  381. return -1;
  382. if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
  383. return -1;
  384. return 0;
  385. }
  386. /* This function initializes the IO ports.
  387. *
  388. * The following operations are performed -
  389. * - Read the IO ports (0, 1 and 2)
  390. * - Set host interrupt Reset-To-Read to clear
  391. * - Set auto re-enable interrupt
  392. */
  393. static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
  394. {
  395. u8 reg;
  396. struct sdio_mmc_card *card = adapter->card;
  397. adapter->ioport = 0;
  398. if (card->supports_sdio_new_mode) {
  399. if (mwifiex_init_sdio_new_mode(adapter))
  400. return -1;
  401. goto cont;
  402. }
  403. /* Read the IO port */
  404. if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
  405. adapter->ioport |= (reg & 0xff);
  406. else
  407. return -1;
  408. if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
  409. adapter->ioport |= ((reg & 0xff) << 8);
  410. else
  411. return -1;
  412. if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
  413. adapter->ioport |= ((reg & 0xff) << 16);
  414. else
  415. return -1;
  416. cont:
  417. pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
  418. /* Set Host interrupt reset to read to clear */
  419. if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
  420. mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
  421. reg | card->reg->sdio_int_mask);
  422. else
  423. return -1;
  424. /* Dnld/Upld ready set to auto reset */
  425. if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
  426. mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
  427. reg | AUTO_RE_ENABLE_INT);
  428. else
  429. return -1;
  430. return 0;
  431. }
  432. /*
  433. * This function sends data to the card.
  434. */
  435. static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
  436. u8 *payload, u32 pkt_len, u32 port)
  437. {
  438. u32 i = 0;
  439. int ret;
  440. do {
  441. ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
  442. if (ret) {
  443. i++;
  444. dev_err(adapter->dev, "host_to_card, write iomem"
  445. " (%d) failed: %d\n", i, ret);
  446. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  447. dev_err(adapter->dev, "write CFG reg failed\n");
  448. ret = -1;
  449. if (i > MAX_WRITE_IOMEM_RETRY)
  450. return ret;
  451. }
  452. } while (ret == -1);
  453. return ret;
  454. }
  455. /*
  456. * This function gets the read port.
  457. *
  458. * If control port bit is set in MP read bitmap, the control port
  459. * is returned, otherwise the current read port is returned and
  460. * the value is increased (provided it does not reach the maximum
  461. * limit, in which case it is reset to 1)
  462. */
  463. static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
  464. {
  465. struct sdio_mmc_card *card = adapter->card;
  466. const struct mwifiex_sdio_card_reg *reg = card->reg;
  467. u32 rd_bitmap = card->mp_rd_bitmap;
  468. dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
  469. if (card->supports_sdio_new_mode) {
  470. if (!(rd_bitmap & reg->data_port_mask))
  471. return -1;
  472. } else {
  473. if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
  474. return -1;
  475. }
  476. if ((card->has_control_mask) &&
  477. (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
  478. card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
  479. *port = CTRL_PORT;
  480. dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
  481. *port, card->mp_rd_bitmap);
  482. return 0;
  483. }
  484. if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
  485. return -1;
  486. /* We are now handling the SDIO data ports */
  487. card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
  488. *port = card->curr_rd_port;
  489. if (++card->curr_rd_port == card->max_ports)
  490. card->curr_rd_port = reg->start_rd_port;
  491. dev_dbg(adapter->dev,
  492. "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
  493. *port, rd_bitmap, card->mp_rd_bitmap);
  494. return 0;
  495. }
  496. /*
  497. * This function gets the write port for data.
  498. *
  499. * The current write port is returned if available and the value is
  500. * increased (provided it does not reach the maximum limit, in which
  501. * case it is reset to 1)
  502. */
  503. static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
  504. {
  505. struct sdio_mmc_card *card = adapter->card;
  506. const struct mwifiex_sdio_card_reg *reg = card->reg;
  507. u32 wr_bitmap = card->mp_wr_bitmap;
  508. dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
  509. if (card->supports_sdio_new_mode &&
  510. !(wr_bitmap & reg->data_port_mask)) {
  511. adapter->data_sent = true;
  512. return -EBUSY;
  513. } else if (!card->supports_sdio_new_mode &&
  514. !(wr_bitmap & card->mp_data_port_mask)) {
  515. return -1;
  516. }
  517. if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
  518. card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
  519. *port = card->curr_wr_port;
  520. if (((card->supports_sdio_new_mode) &&
  521. (++card->curr_wr_port == card->max_ports)) ||
  522. ((!card->supports_sdio_new_mode) &&
  523. (++card->curr_wr_port == card->mp_end_port)))
  524. card->curr_wr_port = reg->start_wr_port;
  525. } else {
  526. adapter->data_sent = true;
  527. return -EBUSY;
  528. }
  529. if ((card->has_control_mask) && (*port == CTRL_PORT)) {
  530. dev_err(adapter->dev,
  531. "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
  532. *port, card->curr_wr_port, wr_bitmap,
  533. card->mp_wr_bitmap);
  534. return -1;
  535. }
  536. dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
  537. *port, wr_bitmap, card->mp_wr_bitmap);
  538. return 0;
  539. }
  540. /*
  541. * This function polls the card status.
  542. */
  543. static int
  544. mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
  545. {
  546. struct sdio_mmc_card *card = adapter->card;
  547. u32 tries;
  548. u8 cs;
  549. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  550. if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
  551. break;
  552. else if ((cs & bits) == bits)
  553. return 0;
  554. usleep_range(10, 20);
  555. }
  556. dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
  557. return -1;
  558. }
  559. /*
  560. * This function reads the firmware status.
  561. */
  562. static int
  563. mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
  564. {
  565. struct sdio_mmc_card *card = adapter->card;
  566. const struct mwifiex_sdio_card_reg *reg = card->reg;
  567. u8 fws0, fws1;
  568. if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
  569. return -1;
  570. if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
  571. return -1;
  572. *dat = (u16) ((fws1 << 8) | fws0);
  573. return 0;
  574. }
  575. /*
  576. * This function disables the host interrupt.
  577. *
  578. * The host interrupt mask is read, the disable bit is reset and
  579. * written back to the card host interrupt mask register.
  580. */
  581. static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
  582. {
  583. struct sdio_mmc_card *card = adapter->card;
  584. struct sdio_func *func = card->func;
  585. sdio_claim_host(func);
  586. mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
  587. sdio_release_irq(func);
  588. sdio_release_host(func);
  589. }
  590. /*
  591. * This function enables the host interrupt.
  592. *
  593. * The host interrupt enable mask is written to the card
  594. * host interrupt mask register.
  595. */
  596. static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
  597. {
  598. struct sdio_mmc_card *card = adapter->card;
  599. struct sdio_func *func = card->func;
  600. int ret;
  601. sdio_claim_host(func);
  602. /* Request the SDIO IRQ */
  603. ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
  604. if (ret) {
  605. dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
  606. goto out;
  607. }
  608. /* Simply write the mask to the register */
  609. ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
  610. card->reg->host_int_enable);
  611. if (ret) {
  612. dev_err(adapter->dev, "enable host interrupt failed\n");
  613. sdio_release_irq(func);
  614. }
  615. out:
  616. sdio_release_host(func);
  617. return ret;
  618. }
  619. /*
  620. * This function sends a data buffer to the card.
  621. */
  622. static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
  623. u32 *type, u8 *buffer,
  624. u32 npayload, u32 ioport)
  625. {
  626. int ret;
  627. u32 nb;
  628. if (!buffer) {
  629. dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
  630. return -1;
  631. }
  632. ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
  633. if (ret) {
  634. dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
  635. ret);
  636. return -1;
  637. }
  638. nb = le16_to_cpu(*(__le16 *) (buffer));
  639. if (nb > npayload) {
  640. dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
  641. __func__, nb, npayload);
  642. return -1;
  643. }
  644. *type = le16_to_cpu(*(__le16 *) (buffer + 2));
  645. return ret;
  646. }
  647. /*
  648. * This function downloads the firmware to the card.
  649. *
  650. * Firmware is downloaded to the card in blocks. Every block download
  651. * is tested for CRC errors, and retried a number of times before
  652. * returning failure.
  653. */
  654. static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
  655. struct mwifiex_fw_image *fw)
  656. {
  657. struct sdio_mmc_card *card = adapter->card;
  658. const struct mwifiex_sdio_card_reg *reg = card->reg;
  659. int ret;
  660. u8 *firmware = fw->fw_buf;
  661. u32 firmware_len = fw->fw_len;
  662. u32 offset = 0;
  663. u8 base0, base1;
  664. u8 *fwbuf;
  665. u16 len = 0;
  666. u32 txlen, tx_blocks = 0, tries;
  667. u32 i = 0;
  668. if (!firmware_len) {
  669. dev_err(adapter->dev,
  670. "firmware image not found! Terminating download\n");
  671. return -1;
  672. }
  673. dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
  674. firmware_len);
  675. /* Assume that the allocated buffer is 8-byte aligned */
  676. fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
  677. if (!fwbuf)
  678. return -ENOMEM;
  679. /* Perform firmware data transfer */
  680. do {
  681. /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
  682. bits */
  683. ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
  684. DN_LD_CARD_RDY);
  685. if (ret) {
  686. dev_err(adapter->dev, "FW download with helper:"
  687. " poll status timeout @ %d\n", offset);
  688. goto done;
  689. }
  690. /* More data? */
  691. if (offset >= firmware_len)
  692. break;
  693. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  694. ret = mwifiex_read_reg(adapter, reg->base_0_reg,
  695. &base0);
  696. if (ret) {
  697. dev_err(adapter->dev,
  698. "dev BASE0 register read failed: "
  699. "base0=%#04X(%d). Terminating dnld\n",
  700. base0, base0);
  701. goto done;
  702. }
  703. ret = mwifiex_read_reg(adapter, reg->base_1_reg,
  704. &base1);
  705. if (ret) {
  706. dev_err(adapter->dev,
  707. "dev BASE1 register read failed: "
  708. "base1=%#04X(%d). Terminating dnld\n",
  709. base1, base1);
  710. goto done;
  711. }
  712. len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
  713. if (len)
  714. break;
  715. usleep_range(10, 20);
  716. }
  717. if (!len) {
  718. break;
  719. } else if (len > MWIFIEX_UPLD_SIZE) {
  720. dev_err(adapter->dev,
  721. "FW dnld failed @ %d, invalid length %d\n",
  722. offset, len);
  723. ret = -1;
  724. goto done;
  725. }
  726. txlen = len;
  727. if (len & BIT(0)) {
  728. i++;
  729. if (i > MAX_WRITE_IOMEM_RETRY) {
  730. dev_err(adapter->dev,
  731. "FW dnld failed @ %d, over max retry\n",
  732. offset);
  733. ret = -1;
  734. goto done;
  735. }
  736. dev_err(adapter->dev, "CRC indicated by the helper:"
  737. " len = 0x%04X, txlen = %d\n", len, txlen);
  738. len &= ~BIT(0);
  739. /* Setting this to 0 to resend from same offset */
  740. txlen = 0;
  741. } else {
  742. i = 0;
  743. /* Set blocksize to transfer - checking for last
  744. block */
  745. if (firmware_len - offset < txlen)
  746. txlen = firmware_len - offset;
  747. tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
  748. / MWIFIEX_SDIO_BLOCK_SIZE;
  749. /* Copy payload to buffer */
  750. memmove(fwbuf, &firmware[offset], txlen);
  751. }
  752. ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
  753. MWIFIEX_SDIO_BLOCK_SIZE,
  754. adapter->ioport);
  755. if (ret) {
  756. dev_err(adapter->dev,
  757. "FW download, write iomem (%d) failed @ %d\n",
  758. i, offset);
  759. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  760. dev_err(adapter->dev, "write CFG reg failed\n");
  761. ret = -1;
  762. goto done;
  763. }
  764. offset += txlen;
  765. } while (true);
  766. dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
  767. offset);
  768. ret = 0;
  769. done:
  770. kfree(fwbuf);
  771. return ret;
  772. }
  773. /*
  774. * This function checks the firmware status in card.
  775. *
  776. * The winner interface is also determined by this function.
  777. */
  778. static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
  779. u32 poll_num)
  780. {
  781. struct sdio_mmc_card *card = adapter->card;
  782. int ret = 0;
  783. u16 firmware_stat;
  784. u32 tries;
  785. u8 winner_status;
  786. /* Wait for firmware initialization event */
  787. for (tries = 0; tries < poll_num; tries++) {
  788. ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
  789. if (ret)
  790. continue;
  791. if (firmware_stat == FIRMWARE_READY_SDIO) {
  792. ret = 0;
  793. break;
  794. } else {
  795. mdelay(100);
  796. ret = -1;
  797. }
  798. }
  799. if (ret) {
  800. if (mwifiex_read_reg
  801. (adapter, card->reg->status_reg_0, &winner_status))
  802. winner_status = 0;
  803. if (winner_status)
  804. adapter->winner = 0;
  805. else
  806. adapter->winner = 1;
  807. }
  808. return ret;
  809. }
  810. /*
  811. * This function reads the interrupt status from card.
  812. */
  813. static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
  814. {
  815. struct sdio_mmc_card *card = adapter->card;
  816. u8 sdio_ireg;
  817. unsigned long flags;
  818. if (mwifiex_read_data_sync(adapter, card->mp_regs,
  819. card->reg->max_mp_regs,
  820. REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
  821. dev_err(adapter->dev, "read mp_regs failed\n");
  822. return;
  823. }
  824. sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
  825. if (sdio_ireg) {
  826. /*
  827. * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  828. * For SDIO new mode CMD port interrupts
  829. * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
  830. * UP_LD_CMD_PORT_HOST_INT_STATUS
  831. * Clear the interrupt status register
  832. */
  833. dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
  834. spin_lock_irqsave(&adapter->int_lock, flags);
  835. adapter->int_status |= sdio_ireg;
  836. spin_unlock_irqrestore(&adapter->int_lock, flags);
  837. }
  838. }
  839. /*
  840. * SDIO interrupt handler.
  841. *
  842. * This function reads the interrupt status from firmware and handles
  843. * the interrupt in current thread (ksdioirqd) right away.
  844. */
  845. static void
  846. mwifiex_sdio_interrupt(struct sdio_func *func)
  847. {
  848. struct mwifiex_adapter *adapter;
  849. struct sdio_mmc_card *card;
  850. card = sdio_get_drvdata(func);
  851. if (!card || !card->adapter) {
  852. pr_debug("int: func=%p card=%p adapter=%p\n",
  853. func, card, card ? card->adapter : NULL);
  854. return;
  855. }
  856. adapter = card->adapter;
  857. if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
  858. adapter->ps_state = PS_STATE_AWAKE;
  859. mwifiex_interrupt_status(adapter);
  860. mwifiex_main_process(adapter);
  861. }
  862. /*
  863. * This function decodes a received packet.
  864. *
  865. * Based on the type, the packet is treated as either a data, or
  866. * a command response, or an event, and the correct handler
  867. * function is invoked.
  868. */
  869. static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
  870. struct sk_buff *skb, u32 upld_typ)
  871. {
  872. u8 *cmd_buf;
  873. skb_pull(skb, INTF_HEADER_LEN);
  874. switch (upld_typ) {
  875. case MWIFIEX_TYPE_DATA:
  876. dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
  877. mwifiex_handle_rx_packet(adapter, skb);
  878. break;
  879. case MWIFIEX_TYPE_CMD:
  880. dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
  881. /* take care of curr_cmd = NULL case */
  882. if (!adapter->curr_cmd) {
  883. cmd_buf = adapter->upld_buf;
  884. if (adapter->ps_state == PS_STATE_SLEEP_CFM)
  885. mwifiex_process_sleep_confirm_resp(adapter,
  886. skb->data,
  887. skb->len);
  888. memcpy(cmd_buf, skb->data,
  889. min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
  890. skb->len));
  891. dev_kfree_skb_any(skb);
  892. } else {
  893. adapter->cmd_resp_received = true;
  894. adapter->curr_cmd->resp_skb = skb;
  895. }
  896. break;
  897. case MWIFIEX_TYPE_EVENT:
  898. dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
  899. adapter->event_cause = *(u32 *) skb->data;
  900. if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
  901. memcpy(adapter->event_body,
  902. skb->data + MWIFIEX_EVENT_HEADER_LEN,
  903. skb->len);
  904. /* event cause has been saved to adapter->event_cause */
  905. adapter->event_received = true;
  906. adapter->event_skb = skb;
  907. break;
  908. default:
  909. dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
  910. dev_kfree_skb_any(skb);
  911. break;
  912. }
  913. return 0;
  914. }
  915. /*
  916. * This function transfers received packets from card to driver, performing
  917. * aggregation if required.
  918. *
  919. * For data received on control port, or if aggregation is disabled, the
  920. * received buffers are uploaded as separate packets. However, if aggregation
  921. * is enabled and required, the buffers are copied onto an aggregation buffer,
  922. * provided there is space left, processed and finally uploaded.
  923. */
  924. static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
  925. struct sk_buff *skb, u8 port)
  926. {
  927. struct sdio_mmc_card *card = adapter->card;
  928. s32 f_do_rx_aggr = 0;
  929. s32 f_do_rx_cur = 0;
  930. s32 f_aggr_cur = 0;
  931. struct sk_buff *skb_deaggr;
  932. u32 pind;
  933. u32 pkt_len, pkt_type, mport;
  934. u8 *curr_ptr;
  935. u32 rx_len = skb->len;
  936. if ((card->has_control_mask) && (port == CTRL_PORT)) {
  937. /* Read the command Resp without aggr */
  938. dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
  939. "response\n", __func__);
  940. f_do_rx_cur = 1;
  941. goto rx_curr_single;
  942. }
  943. if (!card->mpa_rx.enabled) {
  944. dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
  945. __func__);
  946. f_do_rx_cur = 1;
  947. goto rx_curr_single;
  948. }
  949. if ((!card->has_control_mask && (card->mp_rd_bitmap &
  950. card->reg->data_port_mask)) ||
  951. (card->has_control_mask && (card->mp_rd_bitmap &
  952. (~((u32) CTRL_PORT_MASK))))) {
  953. /* Some more data RX pending */
  954. dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
  955. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  956. if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
  957. f_aggr_cur = 1;
  958. } else {
  959. /* No room in Aggr buf, do rx aggr now */
  960. f_do_rx_aggr = 1;
  961. f_do_rx_cur = 1;
  962. }
  963. } else {
  964. /* Rx aggr not in progress */
  965. f_aggr_cur = 1;
  966. }
  967. } else {
  968. /* No more data RX pending */
  969. dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
  970. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  971. f_do_rx_aggr = 1;
  972. if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
  973. f_aggr_cur = 1;
  974. else
  975. /* No room in Aggr buf, do rx aggr now */
  976. f_do_rx_cur = 1;
  977. } else {
  978. f_do_rx_cur = 1;
  979. }
  980. }
  981. if (f_aggr_cur) {
  982. dev_dbg(adapter->dev, "info: current packet aggregation\n");
  983. /* Curr pkt can be aggregated */
  984. mp_rx_aggr_setup(card, skb, port);
  985. if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
  986. mp_rx_aggr_port_limit_reached(card)) {
  987. dev_dbg(adapter->dev, "info: %s: aggregated packet "
  988. "limit reached\n", __func__);
  989. /* No more pkts allowed in Aggr buf, rx it */
  990. f_do_rx_aggr = 1;
  991. }
  992. }
  993. if (f_do_rx_aggr) {
  994. /* do aggr RX now */
  995. dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
  996. card->mpa_rx.pkt_cnt);
  997. if (card->supports_sdio_new_mode) {
  998. int i;
  999. u32 port_count;
  1000. for (i = 0, port_count = 0; i < card->max_ports; i++)
  1001. if (card->mpa_rx.ports & BIT(i))
  1002. port_count++;
  1003. /* Reading data from "start_port + 0" to "start_port +
  1004. * port_count -1", so decrease the count by 1
  1005. */
  1006. port_count--;
  1007. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1008. (port_count << 8)) + card->mpa_rx.start_port;
  1009. } else {
  1010. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1011. (card->mpa_rx.ports << 4)) +
  1012. card->mpa_rx.start_port;
  1013. }
  1014. if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
  1015. card->mpa_rx.buf_len, mport, 1))
  1016. goto error;
  1017. curr_ptr = card->mpa_rx.buf;
  1018. for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
  1019. /* get curr PKT len & type */
  1020. pkt_len = *(u16 *) &curr_ptr[0];
  1021. pkt_type = *(u16 *) &curr_ptr[2];
  1022. /* copy pkt to deaggr buf */
  1023. skb_deaggr = card->mpa_rx.skb_arr[pind];
  1024. if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
  1025. card->mpa_rx.len_arr[pind])) {
  1026. memcpy(skb_deaggr->data, curr_ptr, pkt_len);
  1027. skb_trim(skb_deaggr, pkt_len);
  1028. /* Process de-aggr packet */
  1029. mwifiex_decode_rx_packet(adapter, skb_deaggr,
  1030. pkt_type);
  1031. } else {
  1032. dev_err(adapter->dev, "wrong aggr pkt:"
  1033. " type=%d len=%d max_len=%d\n",
  1034. pkt_type, pkt_len,
  1035. card->mpa_rx.len_arr[pind]);
  1036. dev_kfree_skb_any(skb_deaggr);
  1037. }
  1038. curr_ptr += card->mpa_rx.len_arr[pind];
  1039. }
  1040. MP_RX_AGGR_BUF_RESET(card);
  1041. }
  1042. rx_curr_single:
  1043. if (f_do_rx_cur) {
  1044. dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
  1045. port, rx_len);
  1046. if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
  1047. skb->data, skb->len,
  1048. adapter->ioport + port))
  1049. goto error;
  1050. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  1051. }
  1052. return 0;
  1053. error:
  1054. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  1055. /* Multiport-aggregation transfer failed - cleanup */
  1056. for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
  1057. /* copy pkt to deaggr buf */
  1058. skb_deaggr = card->mpa_rx.skb_arr[pind];
  1059. dev_kfree_skb_any(skb_deaggr);
  1060. }
  1061. MP_RX_AGGR_BUF_RESET(card);
  1062. }
  1063. if (f_do_rx_cur)
  1064. /* Single transfer pending. Free curr buff also */
  1065. dev_kfree_skb_any(skb);
  1066. return -1;
  1067. }
  1068. /*
  1069. * This function checks the current interrupt status.
  1070. *
  1071. * The following interrupts are checked and handled by this function -
  1072. * - Data sent
  1073. * - Command sent
  1074. * - Packets received
  1075. *
  1076. * Since the firmware does not generate download ready interrupt if the
  1077. * port updated is command port only, command sent interrupt checking
  1078. * should be done manually, and for every SDIO interrupt.
  1079. *
  1080. * In case of Rx packets received, the packets are uploaded from card to
  1081. * host and processed accordingly.
  1082. */
  1083. static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
  1084. {
  1085. struct sdio_mmc_card *card = adapter->card;
  1086. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1087. int ret = 0;
  1088. u8 sdio_ireg;
  1089. struct sk_buff *skb;
  1090. u8 port = CTRL_PORT;
  1091. u32 len_reg_l, len_reg_u;
  1092. u32 rx_blocks;
  1093. u16 rx_len;
  1094. unsigned long flags;
  1095. u32 bitmap;
  1096. u8 cr;
  1097. spin_lock_irqsave(&adapter->int_lock, flags);
  1098. sdio_ireg = adapter->int_status;
  1099. adapter->int_status = 0;
  1100. spin_unlock_irqrestore(&adapter->int_lock, flags);
  1101. if (!sdio_ireg)
  1102. return ret;
  1103. /* Following interrupt is only for SDIO new mode */
  1104. if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
  1105. adapter->cmd_sent = false;
  1106. /* Following interrupt is only for SDIO new mode */
  1107. if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
  1108. u32 pkt_type;
  1109. /* read the len of control packet */
  1110. rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
  1111. rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
  1112. rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
  1113. if (rx_len <= INTF_HEADER_LEN ||
  1114. (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1115. MWIFIEX_RX_DATA_BUF_SIZE)
  1116. return -1;
  1117. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1118. skb = dev_alloc_skb(rx_len);
  1119. if (!skb)
  1120. return -1;
  1121. skb_put(skb, rx_len);
  1122. if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
  1123. skb->len, adapter->ioport |
  1124. CMD_PORT_SLCT)) {
  1125. dev_err(adapter->dev,
  1126. "%s: failed to card_to_host", __func__);
  1127. dev_kfree_skb_any(skb);
  1128. goto term_cmd;
  1129. }
  1130. if ((pkt_type != MWIFIEX_TYPE_CMD) &&
  1131. (pkt_type != MWIFIEX_TYPE_EVENT))
  1132. dev_err(adapter->dev,
  1133. "%s:Received wrong packet on cmd port",
  1134. __func__);
  1135. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  1136. }
  1137. if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
  1138. bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
  1139. bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
  1140. if (card->supports_sdio_new_mode) {
  1141. bitmap |=
  1142. ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
  1143. bitmap |=
  1144. ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
  1145. }
  1146. card->mp_wr_bitmap = bitmap;
  1147. dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
  1148. card->mp_wr_bitmap);
  1149. if (adapter->data_sent &&
  1150. (card->mp_wr_bitmap & card->mp_data_port_mask)) {
  1151. dev_dbg(adapter->dev,
  1152. "info: <--- Tx DONE Interrupt --->\n");
  1153. adapter->data_sent = false;
  1154. }
  1155. }
  1156. /* As firmware will not generate download ready interrupt if the port
  1157. updated is command port only, cmd_sent should be done for any SDIO
  1158. interrupt. */
  1159. if (card->has_control_mask && adapter->cmd_sent) {
  1160. /* Check if firmware has attach buffer at command port and
  1161. update just that in wr_bit_map. */
  1162. card->mp_wr_bitmap |=
  1163. (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
  1164. if (card->mp_wr_bitmap & CTRL_PORT_MASK)
  1165. adapter->cmd_sent = false;
  1166. }
  1167. dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
  1168. adapter->cmd_sent, adapter->data_sent);
  1169. if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
  1170. bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
  1171. bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
  1172. if (card->supports_sdio_new_mode) {
  1173. bitmap |=
  1174. ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
  1175. bitmap |=
  1176. ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
  1177. }
  1178. card->mp_rd_bitmap = bitmap;
  1179. dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
  1180. card->mp_rd_bitmap);
  1181. while (true) {
  1182. ret = mwifiex_get_rd_port(adapter, &port);
  1183. if (ret) {
  1184. dev_dbg(adapter->dev,
  1185. "info: no more rd_port available\n");
  1186. break;
  1187. }
  1188. len_reg_l = reg->rd_len_p0_l + (port << 1);
  1189. len_reg_u = reg->rd_len_p0_u + (port << 1);
  1190. rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
  1191. rx_len |= (u16) card->mp_regs[len_reg_l];
  1192. dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
  1193. port, rx_len);
  1194. rx_blocks =
  1195. (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
  1196. 1) / MWIFIEX_SDIO_BLOCK_SIZE;
  1197. if (rx_len <= INTF_HEADER_LEN ||
  1198. (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1199. MWIFIEX_RX_DATA_BUF_SIZE) {
  1200. dev_err(adapter->dev, "invalid rx_len=%d\n",
  1201. rx_len);
  1202. return -1;
  1203. }
  1204. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1205. skb = dev_alloc_skb(rx_len);
  1206. if (!skb) {
  1207. dev_err(adapter->dev, "%s: failed to alloc skb",
  1208. __func__);
  1209. return -1;
  1210. }
  1211. skb_put(skb, rx_len);
  1212. dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
  1213. rx_len, skb->len);
  1214. if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
  1215. port)) {
  1216. dev_err(adapter->dev, "card_to_host_mpa failed:"
  1217. " int status=%#x\n", sdio_ireg);
  1218. goto term_cmd;
  1219. }
  1220. }
  1221. }
  1222. return 0;
  1223. term_cmd:
  1224. /* terminate cmd */
  1225. if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
  1226. dev_err(adapter->dev, "read CFG reg failed\n");
  1227. else
  1228. dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
  1229. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
  1230. dev_err(adapter->dev, "write CFG reg failed\n");
  1231. else
  1232. dev_dbg(adapter->dev, "info: write success\n");
  1233. if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
  1234. dev_err(adapter->dev, "read CFG reg failed\n");
  1235. else
  1236. dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
  1237. return -1;
  1238. }
  1239. /*
  1240. * This function aggregates transmission buffers in driver and downloads
  1241. * the aggregated packet to card.
  1242. *
  1243. * The individual packets are aggregated by copying into an aggregation
  1244. * buffer and then downloaded to the card. Previous unsent packets in the
  1245. * aggregation buffer are pre-copied first before new packets are added.
  1246. * Aggregation is done till there is space left in the aggregation buffer,
  1247. * or till new packets are available.
  1248. *
  1249. * The function will only download the packet to the card when aggregation
  1250. * stops, otherwise it will just aggregate the packet in aggregation buffer
  1251. * and return.
  1252. */
  1253. static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
  1254. u8 *payload, u32 pkt_len, u32 port,
  1255. u32 next_pkt_len)
  1256. {
  1257. struct sdio_mmc_card *card = adapter->card;
  1258. int ret = 0;
  1259. s32 f_send_aggr_buf = 0;
  1260. s32 f_send_cur_buf = 0;
  1261. s32 f_precopy_cur_buf = 0;
  1262. s32 f_postcopy_cur_buf = 0;
  1263. u32 mport;
  1264. if (!card->mpa_tx.enabled ||
  1265. (card->has_control_mask && (port == CTRL_PORT)) ||
  1266. (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
  1267. dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
  1268. __func__);
  1269. f_send_cur_buf = 1;
  1270. goto tx_curr_single;
  1271. }
  1272. if (next_pkt_len) {
  1273. /* More pkt in TX queue */
  1274. dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
  1275. __func__);
  1276. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1277. if (!mp_tx_aggr_port_limit_reached(card) &&
  1278. MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
  1279. f_precopy_cur_buf = 1;
  1280. if (!(card->mp_wr_bitmap &
  1281. (1 << card->curr_wr_port)) ||
  1282. !MP_TX_AGGR_BUF_HAS_ROOM(
  1283. card, pkt_len + next_pkt_len))
  1284. f_send_aggr_buf = 1;
  1285. } else {
  1286. /* No room in Aggr buf, send it */
  1287. f_send_aggr_buf = 1;
  1288. if (mp_tx_aggr_port_limit_reached(card) ||
  1289. !(card->mp_wr_bitmap &
  1290. (1 << card->curr_wr_port)))
  1291. f_send_cur_buf = 1;
  1292. else
  1293. f_postcopy_cur_buf = 1;
  1294. }
  1295. } else {
  1296. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
  1297. (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1298. f_precopy_cur_buf = 1;
  1299. else
  1300. f_send_cur_buf = 1;
  1301. }
  1302. } else {
  1303. /* Last pkt in TX queue */
  1304. dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
  1305. __func__);
  1306. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1307. /* some packs in Aggr buf already */
  1308. f_send_aggr_buf = 1;
  1309. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
  1310. f_precopy_cur_buf = 1;
  1311. else
  1312. /* No room in Aggr buf, send it */
  1313. f_send_cur_buf = 1;
  1314. } else {
  1315. f_send_cur_buf = 1;
  1316. }
  1317. }
  1318. if (f_precopy_cur_buf) {
  1319. dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
  1320. __func__);
  1321. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1322. if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
  1323. mp_tx_aggr_port_limit_reached(card))
  1324. /* No more pkts allowed in Aggr buf, send it */
  1325. f_send_aggr_buf = 1;
  1326. }
  1327. if (f_send_aggr_buf) {
  1328. dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
  1329. __func__,
  1330. card->mpa_tx.start_port, card->mpa_tx.ports);
  1331. if (card->supports_sdio_new_mode) {
  1332. u32 port_count;
  1333. int i;
  1334. for (i = 0, port_count = 0; i < card->max_ports; i++)
  1335. if (card->mpa_tx.ports & BIT(i))
  1336. port_count++;
  1337. /* Writing data from "start_port + 0" to "start_port +
  1338. * port_count -1", so decrease the count by 1
  1339. */
  1340. port_count--;
  1341. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1342. (port_count << 8)) + card->mpa_tx.start_port;
  1343. } else {
  1344. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1345. (card->mpa_tx.ports << 4)) +
  1346. card->mpa_tx.start_port;
  1347. }
  1348. ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
  1349. card->mpa_tx.buf_len, mport);
  1350. MP_TX_AGGR_BUF_RESET(card);
  1351. }
  1352. tx_curr_single:
  1353. if (f_send_cur_buf) {
  1354. dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
  1355. __func__, port);
  1356. ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
  1357. adapter->ioport + port);
  1358. }
  1359. if (f_postcopy_cur_buf) {
  1360. dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
  1361. __func__);
  1362. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1363. }
  1364. return ret;
  1365. }
  1366. /*
  1367. * This function downloads data from driver to card.
  1368. *
  1369. * Both commands and data packets are transferred to the card by this
  1370. * function.
  1371. *
  1372. * This function adds the SDIO specific header to the front of the buffer
  1373. * before transferring. The header contains the length of the packet and
  1374. * the type. The firmware handles the packets based upon this set type.
  1375. */
  1376. static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
  1377. u8 type, struct sk_buff *skb,
  1378. struct mwifiex_tx_param *tx_param)
  1379. {
  1380. struct sdio_mmc_card *card = adapter->card;
  1381. int ret;
  1382. u32 buf_block_len;
  1383. u32 blk_size;
  1384. u32 port = CTRL_PORT;
  1385. u8 *payload = (u8 *)skb->data;
  1386. u32 pkt_len = skb->len;
  1387. /* Allocate buffer and copy payload */
  1388. blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
  1389. buf_block_len = (pkt_len + blk_size - 1) / blk_size;
  1390. *(u16 *) &payload[0] = (u16) pkt_len;
  1391. *(u16 *) &payload[2] = type;
  1392. /*
  1393. * This is SDIO specific header
  1394. * u16 length,
  1395. * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
  1396. * MWIFIEX_TYPE_EVENT = 3)
  1397. */
  1398. if (type == MWIFIEX_TYPE_DATA) {
  1399. ret = mwifiex_get_wr_port_data(adapter, &port);
  1400. if (ret) {
  1401. dev_err(adapter->dev, "%s: no wr_port available\n",
  1402. __func__);
  1403. return ret;
  1404. }
  1405. } else {
  1406. adapter->cmd_sent = true;
  1407. /* Type must be MWIFIEX_TYPE_CMD */
  1408. if (pkt_len <= INTF_HEADER_LEN ||
  1409. pkt_len > MWIFIEX_UPLD_SIZE)
  1410. dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
  1411. __func__, payload, pkt_len);
  1412. if (card->supports_sdio_new_mode)
  1413. port = CMD_PORT_SLCT;
  1414. }
  1415. /* Transfer data to card */
  1416. pkt_len = buf_block_len * blk_size;
  1417. if (tx_param)
  1418. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1419. port, tx_param->next_pkt_len
  1420. );
  1421. else
  1422. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1423. port, 0);
  1424. if (ret) {
  1425. if (type == MWIFIEX_TYPE_CMD)
  1426. adapter->cmd_sent = false;
  1427. if (type == MWIFIEX_TYPE_DATA)
  1428. adapter->data_sent = false;
  1429. } else {
  1430. if (type == MWIFIEX_TYPE_DATA) {
  1431. if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1432. adapter->data_sent = true;
  1433. else
  1434. adapter->data_sent = false;
  1435. }
  1436. }
  1437. return ret;
  1438. }
  1439. /*
  1440. * This function allocates the MPA Tx and Rx buffers.
  1441. */
  1442. static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
  1443. u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
  1444. {
  1445. struct sdio_mmc_card *card = adapter->card;
  1446. int ret = 0;
  1447. card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
  1448. if (!card->mpa_tx.buf) {
  1449. ret = -1;
  1450. goto error;
  1451. }
  1452. card->mpa_tx.buf_size = mpa_tx_buf_size;
  1453. card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
  1454. if (!card->mpa_rx.buf) {
  1455. ret = -1;
  1456. goto error;
  1457. }
  1458. card->mpa_rx.buf_size = mpa_rx_buf_size;
  1459. error:
  1460. if (ret) {
  1461. kfree(card->mpa_tx.buf);
  1462. kfree(card->mpa_rx.buf);
  1463. }
  1464. return ret;
  1465. }
  1466. /*
  1467. * This function unregisters the SDIO device.
  1468. *
  1469. * The SDIO IRQ is released, the function is disabled and driver
  1470. * data is set to null.
  1471. */
  1472. static void
  1473. mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  1474. {
  1475. struct sdio_mmc_card *card = adapter->card;
  1476. if (adapter->card) {
  1477. sdio_claim_host(card->func);
  1478. sdio_disable_func(card->func);
  1479. sdio_release_host(card->func);
  1480. sdio_set_drvdata(card->func, NULL);
  1481. }
  1482. }
  1483. /*
  1484. * This function registers the SDIO device.
  1485. *
  1486. * SDIO IRQ is claimed, block size is set and driver data is initialized.
  1487. */
  1488. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  1489. {
  1490. int ret;
  1491. struct sdio_mmc_card *card = adapter->card;
  1492. struct sdio_func *func = card->func;
  1493. /* save adapter pointer in card */
  1494. card->adapter = adapter;
  1495. sdio_claim_host(func);
  1496. /* Set block size */
  1497. ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
  1498. sdio_release_host(func);
  1499. if (ret) {
  1500. pr_err("cannot set SDIO block size\n");
  1501. return ret;
  1502. }
  1503. sdio_set_drvdata(func, card);
  1504. adapter->dev = &func->dev;
  1505. strcpy(adapter->fw_name, card->firmware);
  1506. return 0;
  1507. }
  1508. /*
  1509. * This function initializes the SDIO driver.
  1510. *
  1511. * The following initializations steps are followed -
  1512. * - Read the Host interrupt status register to acknowledge
  1513. * the first interrupt got from bootloader
  1514. * - Disable host interrupt mask register
  1515. * - Get SDIO port
  1516. * - Initialize SDIO variables in card
  1517. * - Allocate MP registers
  1518. * - Allocate MPA Tx and Rx buffers
  1519. */
  1520. static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
  1521. {
  1522. struct sdio_mmc_card *card = adapter->card;
  1523. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1524. int ret;
  1525. u8 sdio_ireg;
  1526. /*
  1527. * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
  1528. * from the bootloader. If we don't do this we get a interrupt
  1529. * as soon as we register the irq.
  1530. */
  1531. mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
  1532. /* Get SDIO ioport */
  1533. mwifiex_init_sdio_ioport(adapter);
  1534. /* Initialize SDIO variables in card */
  1535. card->mp_rd_bitmap = 0;
  1536. card->mp_wr_bitmap = 0;
  1537. card->curr_rd_port = reg->start_rd_port;
  1538. card->curr_wr_port = reg->start_wr_port;
  1539. card->mp_data_port_mask = reg->data_port_mask;
  1540. card->mpa_tx.buf_len = 0;
  1541. card->mpa_tx.pkt_cnt = 0;
  1542. card->mpa_tx.start_port = 0;
  1543. card->mpa_tx.enabled = 1;
  1544. card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
  1545. card->mpa_rx.buf_len = 0;
  1546. card->mpa_rx.pkt_cnt = 0;
  1547. card->mpa_rx.start_port = 0;
  1548. card->mpa_rx.enabled = 1;
  1549. card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
  1550. /* Allocate buffers for SDIO MP-A */
  1551. card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
  1552. if (!card->mp_regs)
  1553. return -ENOMEM;
  1554. /* Allocate skb pointer buffers */
  1555. card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
  1556. card->mp_agg_pkt_limit, GFP_KERNEL);
  1557. card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
  1558. card->mp_agg_pkt_limit, GFP_KERNEL);
  1559. ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
  1560. SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
  1561. SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
  1562. if (ret) {
  1563. dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
  1564. kfree(card->mp_regs);
  1565. return -1;
  1566. }
  1567. return ret;
  1568. }
  1569. /*
  1570. * This function resets the MPA Tx and Rx buffers.
  1571. */
  1572. static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
  1573. {
  1574. struct sdio_mmc_card *card = adapter->card;
  1575. MP_TX_AGGR_BUF_RESET(card);
  1576. MP_RX_AGGR_BUF_RESET(card);
  1577. }
  1578. /*
  1579. * This function cleans up the allocated card buffers.
  1580. *
  1581. * The following are freed by this function -
  1582. * - MP registers
  1583. * - MPA Tx buffer
  1584. * - MPA Rx buffer
  1585. */
  1586. static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
  1587. {
  1588. struct sdio_mmc_card *card = adapter->card;
  1589. kfree(card->mp_regs);
  1590. kfree(card->mpa_rx.skb_arr);
  1591. kfree(card->mpa_rx.len_arr);
  1592. kfree(card->mpa_tx.buf);
  1593. kfree(card->mpa_rx.buf);
  1594. }
  1595. /*
  1596. * This function updates the MP end port in card.
  1597. */
  1598. static void
  1599. mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
  1600. {
  1601. struct sdio_mmc_card *card = adapter->card;
  1602. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1603. int i;
  1604. card->mp_end_port = port;
  1605. card->mp_data_port_mask = reg->data_port_mask;
  1606. if (reg->start_wr_port) {
  1607. for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
  1608. card->mp_data_port_mask &=
  1609. ~(1 << (card->max_ports - i));
  1610. }
  1611. card->curr_wr_port = reg->start_wr_port;
  1612. dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
  1613. port, card->mp_data_port_mask);
  1614. }
  1615. static struct mmc_host *reset_host;
  1616. static void sdio_card_reset_worker(struct work_struct *work)
  1617. {
  1618. struct mmc_host *target = reset_host;
  1619. /* The actual reset operation must be run outside of driver thread.
  1620. * This is because mmc_remove_host() will cause the device to be
  1621. * instantly destroyed, and the driver then needs to end its thread,
  1622. * leading to a deadlock.
  1623. *
  1624. * We run it in a totally independent workqueue.
  1625. */
  1626. pr_err("Resetting card...\n");
  1627. mmc_remove_host(target);
  1628. /* 20ms delay is based on experiment with sdhci controller */
  1629. mdelay(20);
  1630. mmc_add_host(target);
  1631. }
  1632. static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
  1633. /* This function resets the card */
  1634. static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
  1635. {
  1636. struct sdio_mmc_card *card = adapter->card;
  1637. reset_host = card->func->card->host;
  1638. schedule_work(&card_reset_work);
  1639. }
  1640. static struct mwifiex_if_ops sdio_ops = {
  1641. .init_if = mwifiex_init_sdio,
  1642. .cleanup_if = mwifiex_cleanup_sdio,
  1643. .check_fw_status = mwifiex_check_fw_status,
  1644. .prog_fw = mwifiex_prog_fw_w_helper,
  1645. .register_dev = mwifiex_register_dev,
  1646. .unregister_dev = mwifiex_unregister_dev,
  1647. .enable_int = mwifiex_sdio_enable_host_int,
  1648. .disable_int = mwifiex_sdio_disable_host_int,
  1649. .process_int_status = mwifiex_process_int_status,
  1650. .host_to_card = mwifiex_sdio_host_to_card,
  1651. .wakeup = mwifiex_pm_wakeup_card,
  1652. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  1653. /* SDIO specific */
  1654. .update_mp_end_port = mwifiex_update_mp_end_port,
  1655. .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
  1656. .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
  1657. .event_complete = mwifiex_sdio_event_complete,
  1658. .card_reset = mwifiex_sdio_card_reset,
  1659. };
  1660. /*
  1661. * This function initializes the SDIO driver.
  1662. *
  1663. * This initiates the semaphore and registers the device with
  1664. * SDIO bus.
  1665. */
  1666. static int
  1667. mwifiex_sdio_init_module(void)
  1668. {
  1669. sema_init(&add_remove_card_sem, 1);
  1670. /* Clear the flag in case user removes the card. */
  1671. user_rmmod = 0;
  1672. return sdio_register_driver(&mwifiex_sdio);
  1673. }
  1674. /*
  1675. * This function cleans up the SDIO driver.
  1676. *
  1677. * The following major steps are followed for cleanup -
  1678. * - Resume the device if its suspended
  1679. * - Disconnect the device if connected
  1680. * - Shutdown the firmware
  1681. * - Unregister the device from SDIO bus.
  1682. */
  1683. static void
  1684. mwifiex_sdio_cleanup_module(void)
  1685. {
  1686. if (!down_interruptible(&add_remove_card_sem))
  1687. up(&add_remove_card_sem);
  1688. /* Set the flag as user is removing this module. */
  1689. user_rmmod = 1;
  1690. cancel_work_sync(&card_reset_work);
  1691. sdio_unregister_driver(&mwifiex_sdio);
  1692. }
  1693. module_init(mwifiex_sdio_init_module);
  1694. module_exit(mwifiex_sdio_cleanup_module);
  1695. MODULE_AUTHOR("Marvell International Ltd.");
  1696. MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
  1697. MODULE_VERSION(SDIO_VERSION);
  1698. MODULE_LICENSE("GPL v2");
  1699. MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
  1700. MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
  1701. MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
  1702. MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);