sdio.c 44 KB

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