sdio.c 45 KB

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