sdio.c 45 KB

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