sdio.c 51 KB

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