sdio.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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 i;
  138. int ret = 0;
  139. if (func) {
  140. pm_flag = sdio_get_host_pm_caps(func);
  141. pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
  142. sdio_func_id(func), pm_flag);
  143. if (!(pm_flag & MMC_PM_KEEP_POWER)) {
  144. pr_err("%s: cannot remain alive while host is"
  145. " suspended\n", sdio_func_id(func));
  146. return -ENOSYS;
  147. }
  148. card = sdio_get_drvdata(func);
  149. if (!card || !card->adapter) {
  150. pr_err("suspend: invalid card or adapter\n");
  151. return 0;
  152. }
  153. } else {
  154. pr_err("suspend: sdio_func is not specified\n");
  155. return 0;
  156. }
  157. adapter = card->adapter;
  158. /* Enable the Host Sleep */
  159. if (!mwifiex_enable_hs(adapter)) {
  160. dev_err(adapter->dev, "cmd: failed to suspend\n");
  161. return -EFAULT;
  162. }
  163. dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
  164. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  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. return -ENOMEM;
  600. /* Perform firmware data transfer */
  601. do {
  602. /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
  603. bits */
  604. ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
  605. DN_LD_CARD_RDY);
  606. if (ret) {
  607. dev_err(adapter->dev, "FW download with helper:"
  608. " poll status timeout @ %d\n", offset);
  609. goto done;
  610. }
  611. /* More data? */
  612. if (offset >= firmware_len)
  613. break;
  614. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  615. ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
  616. &base0);
  617. if (ret) {
  618. dev_err(adapter->dev,
  619. "dev BASE0 register read failed: "
  620. "base0=%#04X(%d). Terminating dnld\n",
  621. base0, base0);
  622. goto done;
  623. }
  624. ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
  625. &base1);
  626. if (ret) {
  627. dev_err(adapter->dev,
  628. "dev BASE1 register read failed: "
  629. "base1=%#04X(%d). Terminating dnld\n",
  630. base1, base1);
  631. goto done;
  632. }
  633. len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
  634. if (len)
  635. break;
  636. usleep_range(10, 20);
  637. }
  638. if (!len) {
  639. break;
  640. } else if (len > MWIFIEX_UPLD_SIZE) {
  641. dev_err(adapter->dev,
  642. "FW dnld failed @ %d, invalid length %d\n",
  643. offset, len);
  644. ret = -1;
  645. goto done;
  646. }
  647. txlen = len;
  648. if (len & BIT(0)) {
  649. i++;
  650. if (i > MAX_WRITE_IOMEM_RETRY) {
  651. dev_err(adapter->dev,
  652. "FW dnld failed @ %d, over max retry\n",
  653. 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 - 1)
  669. / 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,
  678. "FW download, write iomem (%d) failed @ %d\n",
  679. i, offset);
  680. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  681. dev_err(adapter->dev, "write CFG reg failed\n");
  682. ret = -1;
  683. goto done;
  684. }
  685. offset += txlen;
  686. } while (true);
  687. dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
  688. offset);
  689. ret = 0;
  690. done:
  691. kfree(fwbuf);
  692. return ret;
  693. }
  694. /*
  695. * This function checks the firmware status in card.
  696. *
  697. * The winner interface is also determined by this function.
  698. */
  699. static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
  700. u32 poll_num)
  701. {
  702. int ret = 0;
  703. u16 firmware_stat;
  704. u32 tries;
  705. u32 winner_status;
  706. /* Wait for firmware initialization event */
  707. for (tries = 0; tries < poll_num; tries++) {
  708. ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
  709. if (ret)
  710. continue;
  711. if (firmware_stat == FIRMWARE_READY_SDIO) {
  712. ret = 0;
  713. break;
  714. } else {
  715. mdelay(100);
  716. ret = -1;
  717. }
  718. }
  719. if (ret) {
  720. if (mwifiex_read_reg
  721. (adapter, CARD_FW_STATUS0_REG, &winner_status))
  722. winner_status = 0;
  723. if (winner_status)
  724. adapter->winner = 0;
  725. else
  726. adapter->winner = 1;
  727. }
  728. return ret;
  729. }
  730. /*
  731. * This function reads the interrupt status from card.
  732. */
  733. static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
  734. {
  735. struct sdio_mmc_card *card = adapter->card;
  736. u32 sdio_ireg;
  737. unsigned long flags;
  738. if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
  739. REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
  740. 0)) {
  741. dev_err(adapter->dev, "read mp_regs failed\n");
  742. return;
  743. }
  744. sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
  745. if (sdio_ireg) {
  746. /*
  747. * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  748. * Clear the interrupt status register
  749. */
  750. dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
  751. spin_lock_irqsave(&adapter->int_lock, flags);
  752. adapter->int_status |= sdio_ireg;
  753. spin_unlock_irqrestore(&adapter->int_lock, flags);
  754. }
  755. }
  756. /*
  757. * SDIO interrupt handler.
  758. *
  759. * This function reads the interrupt status from firmware and handles
  760. * the interrupt in current thread (ksdioirqd) right away.
  761. */
  762. static void
  763. mwifiex_sdio_interrupt(struct sdio_func *func)
  764. {
  765. struct mwifiex_adapter *adapter;
  766. struct sdio_mmc_card *card;
  767. card = sdio_get_drvdata(func);
  768. if (!card || !card->adapter) {
  769. pr_debug("int: func=%p card=%p adapter=%p\n",
  770. func, card, card ? card->adapter : NULL);
  771. return;
  772. }
  773. adapter = card->adapter;
  774. if (adapter->surprise_removed)
  775. return;
  776. if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
  777. adapter->ps_state = PS_STATE_AWAKE;
  778. mwifiex_interrupt_status(adapter);
  779. mwifiex_main_process(adapter);
  780. }
  781. /*
  782. * This function decodes a received packet.
  783. *
  784. * Based on the type, the packet is treated as either a data, or
  785. * a command response, or an event, and the correct handler
  786. * function is invoked.
  787. */
  788. static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
  789. struct sk_buff *skb, u32 upld_typ)
  790. {
  791. u8 *cmd_buf;
  792. skb_pull(skb, INTF_HEADER_LEN);
  793. switch (upld_typ) {
  794. case MWIFIEX_TYPE_DATA:
  795. dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
  796. mwifiex_handle_rx_packet(adapter, skb);
  797. break;
  798. case MWIFIEX_TYPE_CMD:
  799. dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
  800. /* take care of curr_cmd = NULL case */
  801. if (!adapter->curr_cmd) {
  802. cmd_buf = adapter->upld_buf;
  803. if (adapter->ps_state == PS_STATE_SLEEP_CFM)
  804. mwifiex_process_sleep_confirm_resp(adapter,
  805. skb->data,
  806. skb->len);
  807. memcpy(cmd_buf, skb->data,
  808. min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
  809. skb->len));
  810. dev_kfree_skb_any(skb);
  811. } else {
  812. adapter->cmd_resp_received = true;
  813. adapter->curr_cmd->resp_skb = skb;
  814. }
  815. break;
  816. case MWIFIEX_TYPE_EVENT:
  817. dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
  818. adapter->event_cause = *(u32 *) skb->data;
  819. if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
  820. memcpy(adapter->event_body,
  821. skb->data + MWIFIEX_EVENT_HEADER_LEN,
  822. skb->len);
  823. /* event cause has been saved to adapter->event_cause */
  824. adapter->event_received = true;
  825. adapter->event_skb = skb;
  826. break;
  827. default:
  828. dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
  829. dev_kfree_skb_any(skb);
  830. break;
  831. }
  832. return 0;
  833. }
  834. /*
  835. * This function transfers received packets from card to driver, performing
  836. * aggregation if required.
  837. *
  838. * For data received on control port, or if aggregation is disabled, the
  839. * received buffers are uploaded as separate packets. However, if aggregation
  840. * is enabled and required, the buffers are copied onto an aggregation buffer,
  841. * provided there is space left, processed and finally uploaded.
  842. */
  843. static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
  844. struct sk_buff *skb, u8 port)
  845. {
  846. struct sdio_mmc_card *card = adapter->card;
  847. s32 f_do_rx_aggr = 0;
  848. s32 f_do_rx_cur = 0;
  849. s32 f_aggr_cur = 0;
  850. struct sk_buff *skb_deaggr;
  851. u32 pind;
  852. u32 pkt_len, pkt_type = 0;
  853. u8 *curr_ptr;
  854. u32 rx_len = skb->len;
  855. if (port == CTRL_PORT) {
  856. /* Read the command Resp without aggr */
  857. dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
  858. "response\n", __func__);
  859. f_do_rx_cur = 1;
  860. goto rx_curr_single;
  861. }
  862. if (!card->mpa_rx.enabled) {
  863. dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
  864. __func__);
  865. f_do_rx_cur = 1;
  866. goto rx_curr_single;
  867. }
  868. if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
  869. /* Some more data RX pending */
  870. dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
  871. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  872. if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
  873. f_aggr_cur = 1;
  874. } else {
  875. /* No room in Aggr buf, do rx aggr now */
  876. f_do_rx_aggr = 1;
  877. f_do_rx_cur = 1;
  878. }
  879. } else {
  880. /* Rx aggr not in progress */
  881. f_aggr_cur = 1;
  882. }
  883. } else {
  884. /* No more data RX pending */
  885. dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
  886. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  887. f_do_rx_aggr = 1;
  888. if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
  889. f_aggr_cur = 1;
  890. else
  891. /* No room in Aggr buf, do rx aggr now */
  892. f_do_rx_cur = 1;
  893. } else {
  894. f_do_rx_cur = 1;
  895. }
  896. }
  897. if (f_aggr_cur) {
  898. dev_dbg(adapter->dev, "info: current packet aggregation\n");
  899. /* Curr pkt can be aggregated */
  900. MP_RX_AGGR_SETUP(card, skb, port);
  901. if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
  902. MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
  903. dev_dbg(adapter->dev, "info: %s: aggregated packet "
  904. "limit reached\n", __func__);
  905. /* No more pkts allowed in Aggr buf, rx it */
  906. f_do_rx_aggr = 1;
  907. }
  908. }
  909. if (f_do_rx_aggr) {
  910. /* do aggr RX now */
  911. dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
  912. card->mpa_rx.pkt_cnt);
  913. if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
  914. card->mpa_rx.buf_len,
  915. (adapter->ioport | 0x1000 |
  916. (card->mpa_rx.ports << 4)) +
  917. card->mpa_rx.start_port, 1))
  918. goto error;
  919. curr_ptr = card->mpa_rx.buf;
  920. for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
  921. /* get curr PKT len & type */
  922. pkt_len = *(u16 *) &curr_ptr[0];
  923. pkt_type = *(u16 *) &curr_ptr[2];
  924. /* copy pkt to deaggr buf */
  925. skb_deaggr = card->mpa_rx.skb_arr[pind];
  926. if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
  927. card->mpa_rx.len_arr[pind])) {
  928. memcpy(skb_deaggr->data, curr_ptr, pkt_len);
  929. skb_trim(skb_deaggr, pkt_len);
  930. /* Process de-aggr packet */
  931. mwifiex_decode_rx_packet(adapter, skb_deaggr,
  932. pkt_type);
  933. } else {
  934. dev_err(adapter->dev, "wrong aggr pkt:"
  935. " type=%d len=%d max_len=%d\n",
  936. pkt_type, pkt_len,
  937. card->mpa_rx.len_arr[pind]);
  938. dev_kfree_skb_any(skb_deaggr);
  939. }
  940. curr_ptr += card->mpa_rx.len_arr[pind];
  941. }
  942. MP_RX_AGGR_BUF_RESET(card);
  943. }
  944. rx_curr_single:
  945. if (f_do_rx_cur) {
  946. dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
  947. port, rx_len);
  948. if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
  949. skb->data, skb->len,
  950. adapter->ioport + port))
  951. goto error;
  952. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  953. }
  954. return 0;
  955. error:
  956. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  957. /* Multiport-aggregation transfer failed - cleanup */
  958. for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
  959. /* copy pkt to deaggr buf */
  960. skb_deaggr = card->mpa_rx.skb_arr[pind];
  961. dev_kfree_skb_any(skb_deaggr);
  962. }
  963. MP_RX_AGGR_BUF_RESET(card);
  964. }
  965. if (f_do_rx_cur)
  966. /* Single transfer pending. Free curr buff also */
  967. dev_kfree_skb_any(skb);
  968. return -1;
  969. }
  970. /*
  971. * This function checks the current interrupt status.
  972. *
  973. * The following interrupts are checked and handled by this function -
  974. * - Data sent
  975. * - Command sent
  976. * - Packets received
  977. *
  978. * Since the firmware does not generate download ready interrupt if the
  979. * port updated is command port only, command sent interrupt checking
  980. * should be done manually, and for every SDIO interrupt.
  981. *
  982. * In case of Rx packets received, the packets are uploaded from card to
  983. * host and processed accordingly.
  984. */
  985. static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
  986. {
  987. struct sdio_mmc_card *card = adapter->card;
  988. int ret = 0;
  989. u8 sdio_ireg;
  990. struct sk_buff *skb;
  991. u8 port = CTRL_PORT;
  992. u32 len_reg_l, len_reg_u;
  993. u32 rx_blocks;
  994. u16 rx_len;
  995. unsigned long flags;
  996. spin_lock_irqsave(&adapter->int_lock, flags);
  997. sdio_ireg = adapter->int_status;
  998. adapter->int_status = 0;
  999. spin_unlock_irqrestore(&adapter->int_lock, flags);
  1000. if (!sdio_ireg)
  1001. return ret;
  1002. if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
  1003. card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
  1004. card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
  1005. dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
  1006. card->mp_wr_bitmap);
  1007. if (adapter->data_sent &&
  1008. (card->mp_wr_bitmap & card->mp_data_port_mask)) {
  1009. dev_dbg(adapter->dev,
  1010. "info: <--- Tx DONE Interrupt --->\n");
  1011. adapter->data_sent = false;
  1012. }
  1013. }
  1014. /* As firmware will not generate download ready interrupt if the port
  1015. updated is command port only, cmd_sent should be done for any SDIO
  1016. interrupt. */
  1017. if (adapter->cmd_sent) {
  1018. /* Check if firmware has attach buffer at command port and
  1019. update just that in wr_bit_map. */
  1020. card->mp_wr_bitmap |=
  1021. (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
  1022. if (card->mp_wr_bitmap & CTRL_PORT_MASK)
  1023. adapter->cmd_sent = false;
  1024. }
  1025. dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
  1026. adapter->cmd_sent, adapter->data_sent);
  1027. if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
  1028. card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
  1029. card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
  1030. dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
  1031. card->mp_rd_bitmap);
  1032. while (true) {
  1033. ret = mwifiex_get_rd_port(adapter, &port);
  1034. if (ret) {
  1035. dev_dbg(adapter->dev,
  1036. "info: no more rd_port available\n");
  1037. break;
  1038. }
  1039. len_reg_l = RD_LEN_P0_L + (port << 1);
  1040. len_reg_u = RD_LEN_P0_U + (port << 1);
  1041. rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
  1042. rx_len |= (u16) card->mp_regs[len_reg_l];
  1043. dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
  1044. port, rx_len);
  1045. rx_blocks =
  1046. (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
  1047. 1) / MWIFIEX_SDIO_BLOCK_SIZE;
  1048. if (rx_len <= INTF_HEADER_LEN ||
  1049. (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1050. MWIFIEX_RX_DATA_BUF_SIZE) {
  1051. dev_err(adapter->dev, "invalid rx_len=%d\n",
  1052. rx_len);
  1053. return -1;
  1054. }
  1055. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1056. skb = dev_alloc_skb(rx_len);
  1057. if (!skb) {
  1058. dev_err(adapter->dev, "%s: failed to alloc skb",
  1059. __func__);
  1060. return -1;
  1061. }
  1062. skb_put(skb, rx_len);
  1063. dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
  1064. rx_len, skb->len);
  1065. if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
  1066. port)) {
  1067. u32 cr = 0;
  1068. dev_err(adapter->dev, "card_to_host_mpa failed:"
  1069. " int status=%#x\n", sdio_ireg);
  1070. if (mwifiex_read_reg(adapter,
  1071. CONFIGURATION_REG, &cr))
  1072. dev_err(adapter->dev,
  1073. "read CFG reg failed\n");
  1074. dev_dbg(adapter->dev,
  1075. "info: CFG reg val = %d\n", cr);
  1076. if (mwifiex_write_reg(adapter,
  1077. CONFIGURATION_REG,
  1078. (cr | 0x04)))
  1079. dev_err(adapter->dev,
  1080. "write CFG reg failed\n");
  1081. dev_dbg(adapter->dev, "info: write success\n");
  1082. if (mwifiex_read_reg(adapter,
  1083. CONFIGURATION_REG, &cr))
  1084. dev_err(adapter->dev,
  1085. "read CFG reg failed\n");
  1086. dev_dbg(adapter->dev,
  1087. "info: CFG reg val =%x\n", cr);
  1088. return -1;
  1089. }
  1090. }
  1091. }
  1092. return 0;
  1093. }
  1094. /*
  1095. * This function aggregates transmission buffers in driver and downloads
  1096. * the aggregated packet to card.
  1097. *
  1098. * The individual packets are aggregated by copying into an aggregation
  1099. * buffer and then downloaded to the card. Previous unsent packets in the
  1100. * aggregation buffer are pre-copied first before new packets are added.
  1101. * Aggregation is done till there is space left in the aggregation buffer,
  1102. * or till new packets are available.
  1103. *
  1104. * The function will only download the packet to the card when aggregation
  1105. * stops, otherwise it will just aggregate the packet in aggregation buffer
  1106. * and return.
  1107. */
  1108. static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
  1109. u8 *payload, u32 pkt_len, u8 port,
  1110. u32 next_pkt_len)
  1111. {
  1112. struct sdio_mmc_card *card = adapter->card;
  1113. int ret = 0;
  1114. s32 f_send_aggr_buf = 0;
  1115. s32 f_send_cur_buf = 0;
  1116. s32 f_precopy_cur_buf = 0;
  1117. s32 f_postcopy_cur_buf = 0;
  1118. if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
  1119. dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
  1120. __func__);
  1121. f_send_cur_buf = 1;
  1122. goto tx_curr_single;
  1123. }
  1124. if (next_pkt_len) {
  1125. /* More pkt in TX queue */
  1126. dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
  1127. __func__);
  1128. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1129. if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
  1130. MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
  1131. f_precopy_cur_buf = 1;
  1132. if (!(card->mp_wr_bitmap &
  1133. (1 << card->curr_wr_port)) ||
  1134. !MP_TX_AGGR_BUF_HAS_ROOM(
  1135. card, pkt_len + next_pkt_len))
  1136. f_send_aggr_buf = 1;
  1137. } else {
  1138. /* No room in Aggr buf, send it */
  1139. f_send_aggr_buf = 1;
  1140. if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
  1141. !(card->mp_wr_bitmap &
  1142. (1 << card->curr_wr_port)))
  1143. f_send_cur_buf = 1;
  1144. else
  1145. f_postcopy_cur_buf = 1;
  1146. }
  1147. } else {
  1148. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
  1149. (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1150. f_precopy_cur_buf = 1;
  1151. else
  1152. f_send_cur_buf = 1;
  1153. }
  1154. } else {
  1155. /* Last pkt in TX queue */
  1156. dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
  1157. __func__);
  1158. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1159. /* some packs in Aggr buf already */
  1160. f_send_aggr_buf = 1;
  1161. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
  1162. f_precopy_cur_buf = 1;
  1163. else
  1164. /* No room in Aggr buf, send it */
  1165. f_send_cur_buf = 1;
  1166. } else {
  1167. f_send_cur_buf = 1;
  1168. }
  1169. }
  1170. if (f_precopy_cur_buf) {
  1171. dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
  1172. __func__);
  1173. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1174. if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
  1175. MP_TX_AGGR_PORT_LIMIT_REACHED(card))
  1176. /* No more pkts allowed in Aggr buf, send it */
  1177. f_send_aggr_buf = 1;
  1178. }
  1179. if (f_send_aggr_buf) {
  1180. dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
  1181. __func__,
  1182. card->mpa_tx.start_port, card->mpa_tx.ports);
  1183. ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
  1184. card->mpa_tx.buf_len,
  1185. (adapter->ioport | 0x1000 |
  1186. (card->mpa_tx.ports << 4)) +
  1187. card->mpa_tx.start_port);
  1188. MP_TX_AGGR_BUF_RESET(card);
  1189. }
  1190. tx_curr_single:
  1191. if (f_send_cur_buf) {
  1192. dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
  1193. __func__, port);
  1194. ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
  1195. adapter->ioport + port);
  1196. }
  1197. if (f_postcopy_cur_buf) {
  1198. dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
  1199. __func__);
  1200. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1201. }
  1202. return ret;
  1203. }
  1204. /*
  1205. * This function downloads data from driver to card.
  1206. *
  1207. * Both commands and data packets are transferred to the card by this
  1208. * function.
  1209. *
  1210. * This function adds the SDIO specific header to the front of the buffer
  1211. * before transferring. The header contains the length of the packet and
  1212. * the type. The firmware handles the packets based upon this set type.
  1213. */
  1214. static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
  1215. u8 type, struct sk_buff *skb,
  1216. struct mwifiex_tx_param *tx_param)
  1217. {
  1218. struct sdio_mmc_card *card = adapter->card;
  1219. int ret;
  1220. u32 buf_block_len;
  1221. u32 blk_size;
  1222. u8 port = CTRL_PORT;
  1223. u8 *payload = (u8 *)skb->data;
  1224. u32 pkt_len = skb->len;
  1225. /* Allocate buffer and copy payload */
  1226. blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
  1227. buf_block_len = (pkt_len + blk_size - 1) / blk_size;
  1228. *(u16 *) &payload[0] = (u16) pkt_len;
  1229. *(u16 *) &payload[2] = type;
  1230. /*
  1231. * This is SDIO specific header
  1232. * u16 length,
  1233. * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
  1234. * MWIFIEX_TYPE_EVENT = 3)
  1235. */
  1236. if (type == MWIFIEX_TYPE_DATA) {
  1237. ret = mwifiex_get_wr_port_data(adapter, &port);
  1238. if (ret) {
  1239. dev_err(adapter->dev, "%s: no wr_port available\n",
  1240. __func__);
  1241. return ret;
  1242. }
  1243. } else {
  1244. adapter->cmd_sent = true;
  1245. /* Type must be MWIFIEX_TYPE_CMD */
  1246. if (pkt_len <= INTF_HEADER_LEN ||
  1247. pkt_len > MWIFIEX_UPLD_SIZE)
  1248. dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
  1249. __func__, payload, pkt_len);
  1250. }
  1251. /* Transfer data to card */
  1252. pkt_len = buf_block_len * blk_size;
  1253. if (tx_param)
  1254. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1255. port, tx_param->next_pkt_len
  1256. );
  1257. else
  1258. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1259. port, 0);
  1260. if (ret) {
  1261. if (type == MWIFIEX_TYPE_CMD)
  1262. adapter->cmd_sent = false;
  1263. if (type == MWIFIEX_TYPE_DATA)
  1264. adapter->data_sent = false;
  1265. } else {
  1266. if (type == MWIFIEX_TYPE_DATA) {
  1267. if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1268. adapter->data_sent = true;
  1269. else
  1270. adapter->data_sent = false;
  1271. }
  1272. }
  1273. return ret;
  1274. }
  1275. /*
  1276. * This function allocates the MPA Tx and Rx buffers.
  1277. */
  1278. static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
  1279. u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
  1280. {
  1281. struct sdio_mmc_card *card = adapter->card;
  1282. int ret = 0;
  1283. card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
  1284. if (!card->mpa_tx.buf) {
  1285. ret = -1;
  1286. goto error;
  1287. }
  1288. card->mpa_tx.buf_size = mpa_tx_buf_size;
  1289. card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
  1290. if (!card->mpa_rx.buf) {
  1291. ret = -1;
  1292. goto error;
  1293. }
  1294. card->mpa_rx.buf_size = mpa_rx_buf_size;
  1295. error:
  1296. if (ret) {
  1297. kfree(card->mpa_tx.buf);
  1298. kfree(card->mpa_rx.buf);
  1299. }
  1300. return ret;
  1301. }
  1302. /*
  1303. * This function unregisters the SDIO device.
  1304. *
  1305. * The SDIO IRQ is released, the function is disabled and driver
  1306. * data is set to null.
  1307. */
  1308. static void
  1309. mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  1310. {
  1311. struct sdio_mmc_card *card = adapter->card;
  1312. if (adapter->card) {
  1313. /* Release the SDIO IRQ */
  1314. sdio_claim_host(card->func);
  1315. sdio_release_irq(card->func);
  1316. sdio_disable_func(card->func);
  1317. sdio_release_host(card->func);
  1318. sdio_set_drvdata(card->func, NULL);
  1319. }
  1320. }
  1321. /*
  1322. * This function registers the SDIO device.
  1323. *
  1324. * SDIO IRQ is claimed, block size is set and driver data is initialized.
  1325. */
  1326. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  1327. {
  1328. int ret = 0;
  1329. struct sdio_mmc_card *card = adapter->card;
  1330. struct sdio_func *func = card->func;
  1331. /* save adapter pointer in card */
  1332. card->adapter = adapter;
  1333. sdio_claim_host(func);
  1334. /* Request the SDIO IRQ */
  1335. ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
  1336. if (ret) {
  1337. pr_err("claim irq failed: ret=%d\n", ret);
  1338. goto disable_func;
  1339. }
  1340. /* Set block size */
  1341. ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
  1342. if (ret) {
  1343. pr_err("cannot set SDIO block size\n");
  1344. ret = -1;
  1345. goto release_irq;
  1346. }
  1347. sdio_release_host(func);
  1348. sdio_set_drvdata(func, card);
  1349. adapter->dev = &func->dev;
  1350. switch (func->device) {
  1351. case SDIO_DEVICE_ID_MARVELL_8786:
  1352. strcpy(adapter->fw_name, SD8786_DEFAULT_FW_NAME);
  1353. break;
  1354. case SDIO_DEVICE_ID_MARVELL_8797:
  1355. strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
  1356. break;
  1357. case SDIO_DEVICE_ID_MARVELL_8787:
  1358. default:
  1359. strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
  1360. break;
  1361. }
  1362. return 0;
  1363. release_irq:
  1364. sdio_release_irq(func);
  1365. disable_func:
  1366. sdio_disable_func(func);
  1367. sdio_release_host(func);
  1368. adapter->card = NULL;
  1369. return -1;
  1370. }
  1371. /*
  1372. * This function initializes the SDIO driver.
  1373. *
  1374. * The following initializations steps are followed -
  1375. * - Read the Host interrupt status register to acknowledge
  1376. * the first interrupt got from bootloader
  1377. * - Disable host interrupt mask register
  1378. * - Get SDIO port
  1379. * - Initialize SDIO variables in card
  1380. * - Allocate MP registers
  1381. * - Allocate MPA Tx and Rx buffers
  1382. */
  1383. static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
  1384. {
  1385. struct sdio_mmc_card *card = adapter->card;
  1386. int ret;
  1387. u32 sdio_ireg;
  1388. /*
  1389. * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
  1390. * from the bootloader. If we don't do this we get a interrupt
  1391. * as soon as we register the irq.
  1392. */
  1393. mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
  1394. /* Disable host interrupt mask register for SDIO */
  1395. mwifiex_sdio_disable_host_int(adapter);
  1396. /* Get SDIO ioport */
  1397. mwifiex_init_sdio_ioport(adapter);
  1398. /* Initialize SDIO variables in card */
  1399. card->mp_rd_bitmap = 0;
  1400. card->mp_wr_bitmap = 0;
  1401. card->curr_rd_port = 1;
  1402. card->curr_wr_port = 1;
  1403. card->mp_data_port_mask = DATA_PORT_MASK;
  1404. card->mpa_tx.buf_len = 0;
  1405. card->mpa_tx.pkt_cnt = 0;
  1406. card->mpa_tx.start_port = 0;
  1407. card->mpa_tx.enabled = 1;
  1408. card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
  1409. card->mpa_rx.buf_len = 0;
  1410. card->mpa_rx.pkt_cnt = 0;
  1411. card->mpa_rx.start_port = 0;
  1412. card->mpa_rx.enabled = 1;
  1413. card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
  1414. /* Allocate buffers for SDIO MP-A */
  1415. card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
  1416. if (!card->mp_regs)
  1417. return -ENOMEM;
  1418. ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
  1419. SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
  1420. SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
  1421. if (ret) {
  1422. dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
  1423. kfree(card->mp_regs);
  1424. return -1;
  1425. }
  1426. return ret;
  1427. }
  1428. /*
  1429. * This function resets the MPA Tx and Rx buffers.
  1430. */
  1431. static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
  1432. {
  1433. struct sdio_mmc_card *card = adapter->card;
  1434. MP_TX_AGGR_BUF_RESET(card);
  1435. MP_RX_AGGR_BUF_RESET(card);
  1436. }
  1437. /*
  1438. * This function cleans up the allocated card buffers.
  1439. *
  1440. * The following are freed by this function -
  1441. * - MP registers
  1442. * - MPA Tx buffer
  1443. * - MPA Rx buffer
  1444. */
  1445. static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
  1446. {
  1447. struct sdio_mmc_card *card = adapter->card;
  1448. kfree(card->mp_regs);
  1449. kfree(card->mpa_tx.buf);
  1450. kfree(card->mpa_rx.buf);
  1451. }
  1452. /*
  1453. * This function updates the MP end port in card.
  1454. */
  1455. static void
  1456. mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
  1457. {
  1458. struct sdio_mmc_card *card = adapter->card;
  1459. int i;
  1460. card->mp_end_port = port;
  1461. card->mp_data_port_mask = DATA_PORT_MASK;
  1462. for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
  1463. card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
  1464. card->curr_wr_port = 1;
  1465. dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
  1466. port, card->mp_data_port_mask);
  1467. }
  1468. static struct mmc_host *reset_host;
  1469. static void sdio_card_reset_worker(struct work_struct *work)
  1470. {
  1471. /* The actual reset operation must be run outside of driver thread.
  1472. * This is because mmc_remove_host() will cause the device to be
  1473. * instantly destroyed, and the driver then needs to end its thread,
  1474. * leading to a deadlock.
  1475. *
  1476. * We run it in a totally independent workqueue.
  1477. */
  1478. pr_err("Resetting card...\n");
  1479. mmc_remove_host(reset_host);
  1480. /* 20ms delay is based on experiment with sdhci controller */
  1481. mdelay(20);
  1482. mmc_add_host(reset_host);
  1483. }
  1484. static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
  1485. /* This function resets the card */
  1486. static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
  1487. {
  1488. struct sdio_mmc_card *card = adapter->card;
  1489. if (work_pending(&card_reset_work))
  1490. return;
  1491. reset_host = card->func->card->host;
  1492. schedule_work(&card_reset_work);
  1493. }
  1494. static struct mwifiex_if_ops sdio_ops = {
  1495. .init_if = mwifiex_init_sdio,
  1496. .cleanup_if = mwifiex_cleanup_sdio,
  1497. .check_fw_status = mwifiex_check_fw_status,
  1498. .prog_fw = mwifiex_prog_fw_w_helper,
  1499. .register_dev = mwifiex_register_dev,
  1500. .unregister_dev = mwifiex_unregister_dev,
  1501. .enable_int = mwifiex_sdio_enable_host_int,
  1502. .process_int_status = mwifiex_process_int_status,
  1503. .host_to_card = mwifiex_sdio_host_to_card,
  1504. .wakeup = mwifiex_pm_wakeup_card,
  1505. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  1506. /* SDIO specific */
  1507. .update_mp_end_port = mwifiex_update_mp_end_port,
  1508. .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
  1509. .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
  1510. .event_complete = mwifiex_sdio_event_complete,
  1511. .card_reset = mwifiex_sdio_card_reset,
  1512. };
  1513. /*
  1514. * This function initializes the SDIO driver.
  1515. *
  1516. * This initiates the semaphore and registers the device with
  1517. * SDIO bus.
  1518. */
  1519. static int
  1520. mwifiex_sdio_init_module(void)
  1521. {
  1522. sema_init(&add_remove_card_sem, 1);
  1523. /* Clear the flag in case user removes the card. */
  1524. user_rmmod = 0;
  1525. return sdio_register_driver(&mwifiex_sdio);
  1526. }
  1527. /*
  1528. * This function cleans up the SDIO driver.
  1529. *
  1530. * The following major steps are followed for cleanup -
  1531. * - Resume the device if its suspended
  1532. * - Disconnect the device if connected
  1533. * - Shutdown the firmware
  1534. * - Unregister the device from SDIO bus.
  1535. */
  1536. static void
  1537. mwifiex_sdio_cleanup_module(void)
  1538. {
  1539. if (!down_interruptible(&add_remove_card_sem))
  1540. up(&add_remove_card_sem);
  1541. /* Set the flag as user is removing this module. */
  1542. user_rmmod = 1;
  1543. cancel_work_sync(&card_reset_work);
  1544. sdio_unregister_driver(&mwifiex_sdio);
  1545. }
  1546. module_init(mwifiex_sdio_init_module);
  1547. module_exit(mwifiex_sdio_cleanup_module);
  1548. MODULE_AUTHOR("Marvell International Ltd.");
  1549. MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
  1550. MODULE_VERSION(SDIO_VERSION);
  1551. MODULE_LICENSE("GPL v2");
  1552. MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
  1553. MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
  1554. MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);