sdio.c 45 KB

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