sdio.c 44 KB

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