PageRenderTime 4849ms CodeModel.GetById 42ms RepoModel.GetById 2ms app.codeStats 0ms

/drivers/net/wireless/mwifiex/sdio.c

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