sdio.c 45 KB

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