sdio.c 45 KB

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