sdio.c 44 KB

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