sdio.c 51 KB

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