sdio.c 44 KB

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