sdio.c 45 KB

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