sdio.c 43 KB

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