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

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mwifiex_sdio_probe_of
  2. mwifiex_sdio_probe
  3. mwifiex_sdio_resume
  4. mwifiex_write_reg_locked
  5. mwifiex_write_reg
  6. mwifiex_read_reg
  7. mwifiex_write_data_sync
  8. mwifiex_read_data_sync
  9. mwifiex_sdio_read_fw_status
  10. mwifiex_check_fw_status
  11. mwifiex_check_winner_status
  12. mwifiex_sdio_remove
  13. mwifiex_sdio_suspend
  14. mwifiex_sdio_coredump
  15. mwifiex_pm_wakeup_card
  16. mwifiex_pm_wakeup_card_complete
  17. mwifiex_sdio_dnld_fw
  18. mwifiex_init_sdio_new_mode
  19. mwifiex_init_sdio_ioport
  20. mwifiex_write_data_to_card
  21. mwifiex_get_rd_port
  22. mwifiex_get_wr_port_data
  23. mwifiex_sdio_poll_card_status
  24. mwifiex_sdio_disable_host_int
  25. mwifiex_interrupt_status
  26. mwifiex_sdio_interrupt
  27. mwifiex_sdio_enable_host_int
  28. mwifiex_sdio_card_to_host
  29. mwifiex_prog_fw_w_helper
  30. mwifiex_deaggr_sdio_pkt
  31. mwifiex_decode_rx_packet
  32. mwifiex_sdio_card_to_host_mp_aggr
  33. mwifiex_process_int_status
  34. mwifiex_host_to_card_mp_aggr
  35. mwifiex_sdio_host_to_card
  36. mwifiex_alloc_sdio_mpa_buffers
  37. mwifiex_unregister_dev
  38. mwifiex_register_dev
  39. mwifiex_init_sdio
  40. mwifiex_cleanup_mpa_buf
  41. mwifiex_cleanup_sdio
  42. mwifiex_update_mp_end_port
  43. mwifiex_sdio_card_reset_work
  44. mwifiex_sdio_rdwr_firmware
  45. mwifiex_sdio_fw_dump
  46. mwifiex_sdio_generic_fw_dump
  47. mwifiex_sdio_device_dump_work
  48. mwifiex_sdio_work
  49. mwifiex_sdio_card_reset
  50. mwifiex_sdio_device_dump
  51. mwifiex_sdio_reg_dump
  52. mwifiex_sdio_up_dev

   1 /*
   2  * Marvell Wireless LAN device driver: SDIO specific handling
   3  *
   4  * Copyright (C) 2011-2014, 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 
  20 #include <linux/firmware.h>
  21 
  22 #include "decl.h"
  23 #include "ioctl.h"
  24 #include "util.h"
  25 #include "fw.h"
  26 #include "main.h"
  27 #include "wmm.h"
  28 #include "11n.h"
  29 #include "sdio.h"
  30 
  31 
  32 #define SDIO_VERSION    "1.0"
  33 
  34 static void mwifiex_sdio_work(struct work_struct *work);
  35 
  36 static struct mwifiex_if_ops sdio_ops;
  37 
  38 static struct memory_type_mapping generic_mem_type_map[] = {
  39         {"DUMP", NULL, 0, 0xDD},
  40 };
  41 
  42 static struct memory_type_mapping mem_type_mapping_tbl[] = {
  43         {"ITCM", NULL, 0, 0xF0},
  44         {"DTCM", NULL, 0, 0xF1},
  45         {"SQRAM", NULL, 0, 0xF2},
  46         {"APU", NULL, 0, 0xF3},
  47         {"CIU", NULL, 0, 0xF4},
  48         {"ICU", NULL, 0, 0xF5},
  49         {"MAC", NULL, 0, 0xF6},
  50         {"EXT7", NULL, 0, 0xF7},
  51         {"EXT8", NULL, 0, 0xF8},
  52         {"EXT9", NULL, 0, 0xF9},
  53         {"EXT10", NULL, 0, 0xFA},
  54         {"EXT11", NULL, 0, 0xFB},
  55         {"EXT12", NULL, 0, 0xFC},
  56         {"EXT13", NULL, 0, 0xFD},
  57         {"EXTLAST", NULL, 0, 0xFE},
  58 };
  59 
  60 static const struct of_device_id mwifiex_sdio_of_match_table[] = {
  61         { .compatible = "marvell,sd8897" },
  62         { .compatible = "marvell,sd8997" },
  63         { }
  64 };
  65 
  66 /* This function parse device tree node using mmc subnode devicetree API.
  67  * The device node is saved in card->plt_of_node.
  68  * if the device tree node exist and include interrupts attributes, this
  69  * function will also request platform specific wakeup interrupt.
  70  */
  71 static int mwifiex_sdio_probe_of(struct device *dev)
  72 {
  73         if (!of_match_node(mwifiex_sdio_of_match_table, dev->of_node)) {
  74                 dev_err(dev, "required compatible string missing\n");
  75                 return -EINVAL;
  76         }
  77 
  78         return 0;
  79 }
  80 
  81 /*
  82  * SDIO probe.
  83  *
  84  * This function probes an mwifiex device and registers it. It allocates
  85  * the card structure, enables SDIO function number and initiates the
  86  * device registration and initialization procedure by adding a logical
  87  * interface.
  88  */
  89 static int
  90 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
  91 {
  92         int ret;
  93         struct sdio_mmc_card *card = NULL;
  94 
  95         pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
  96                  func->vendor, func->device, func->class, func->num);
  97 
  98         card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
  99         if (!card)
 100                 return -ENOMEM;
 101 
 102         init_completion(&card->fw_done);
 103 
 104         card->func = func;
 105 
 106         func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
 107 
 108         if (id->driver_data) {
 109                 struct mwifiex_sdio_device *data = (void *)id->driver_data;
 110 
 111                 card->firmware = data->firmware;
 112                 card->reg = data->reg;
 113                 card->max_ports = data->max_ports;
 114                 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
 115                 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
 116                 card->has_control_mask = data->has_control_mask;
 117                 card->tx_buf_size = data->tx_buf_size;
 118                 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
 119                 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
 120                 card->can_dump_fw = data->can_dump_fw;
 121                 card->fw_dump_enh = data->fw_dump_enh;
 122                 card->can_auto_tdls = data->can_auto_tdls;
 123                 card->can_ext_scan = data->can_ext_scan;
 124                 INIT_WORK(&card->work, mwifiex_sdio_work);
 125         }
 126 
 127         sdio_claim_host(func);
 128         ret = sdio_enable_func(func);
 129         sdio_release_host(func);
 130 
 131         if (ret) {
 132                 dev_err(&func->dev, "failed to enable function\n");
 133                 return ret;
 134         }
 135 
 136         /* device tree node parsing and platform specific configuration*/
 137         if (func->dev.of_node) {
 138                 ret = mwifiex_sdio_probe_of(&func->dev);
 139                 if (ret)
 140                         goto err_disable;
 141         }
 142 
 143         ret = mwifiex_add_card(card, &card->fw_done, &sdio_ops,
 144                                MWIFIEX_SDIO, &func->dev);
 145         if (ret) {
 146                 dev_err(&func->dev, "add card failed\n");
 147                 goto err_disable;
 148         }
 149 
 150         return 0;
 151 
 152 err_disable:
 153         sdio_claim_host(func);
 154         sdio_disable_func(func);
 155         sdio_release_host(func);
 156 
 157         return ret;
 158 }
 159 
 160 /*
 161  * SDIO resume.
 162  *
 163  * Kernel needs to suspend all functions separately. Therefore all
 164  * registered functions must have drivers with suspend and resume
 165  * methods. Failing that the kernel simply removes the whole card.
 166  *
 167  * If already not resumed, this function turns on the traffic and
 168  * sends a host sleep cancel request to the firmware.
 169  */
 170 static int mwifiex_sdio_resume(struct device *dev)
 171 {
 172         struct sdio_func *func = dev_to_sdio_func(dev);
 173         struct sdio_mmc_card *card;
 174         struct mwifiex_adapter *adapter;
 175 
 176         card = sdio_get_drvdata(func);
 177         if (!card || !card->adapter) {
 178                 dev_err(dev, "resume: invalid card or adapter\n");
 179                 return 0;
 180         }
 181 
 182         adapter = card->adapter;
 183 
 184         if (!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
 185                 mwifiex_dbg(adapter, WARN,
 186                             "device already resumed\n");
 187                 return 0;
 188         }
 189 
 190         clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
 191 
 192         /* Disable Host Sleep */
 193         mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
 194                           MWIFIEX_SYNC_CMD);
 195 
 196         mwifiex_disable_wake(adapter);
 197 
 198         return 0;
 199 }
 200 
 201 /* Write data into SDIO card register. Caller claims SDIO device. */
 202 static int
 203 mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
 204 {
 205         int ret = -1;
 206 
 207         sdio_writeb(func, data, reg, &ret);
 208         return ret;
 209 }
 210 
 211 /* This function writes data into SDIO card register.
 212  */
 213 static int
 214 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
 215 {
 216         struct sdio_mmc_card *card = adapter->card;
 217         int ret;
 218 
 219         sdio_claim_host(card->func);
 220         ret = mwifiex_write_reg_locked(card->func, reg, data);
 221         sdio_release_host(card->func);
 222 
 223         return ret;
 224 }
 225 
 226 /* This function reads data from SDIO card register.
 227  */
 228 static int
 229 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
 230 {
 231         struct sdio_mmc_card *card = adapter->card;
 232         int ret = -1;
 233         u8 val;
 234 
 235         sdio_claim_host(card->func);
 236         val = sdio_readb(card->func, reg, &ret);
 237         sdio_release_host(card->func);
 238 
 239         *data = val;
 240 
 241         return ret;
 242 }
 243 
 244 /* This function writes multiple data into SDIO card memory.
 245  *
 246  * This does not work in suspended mode.
 247  */
 248 static int
 249 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
 250                         u8 *buffer, u32 pkt_len, u32 port)
 251 {
 252         struct sdio_mmc_card *card = adapter->card;
 253         int ret;
 254         u8 blk_mode =
 255                 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
 256         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
 257         u32 blk_cnt =
 258                 (blk_mode ==
 259                  BLOCK_MODE) ? (pkt_len /
 260                                 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
 261         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
 262 
 263         if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
 264                 mwifiex_dbg(adapter, ERROR,
 265                             "%s: not allowed while suspended\n", __func__);
 266                 return -1;
 267         }
 268 
 269         sdio_claim_host(card->func);
 270 
 271         ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
 272 
 273         sdio_release_host(card->func);
 274 
 275         return ret;
 276 }
 277 
 278 /* This function reads multiple data from SDIO card memory.
 279  */
 280 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
 281                                   u32 len, u32 port, u8 claim)
 282 {
 283         struct sdio_mmc_card *card = adapter->card;
 284         int ret;
 285         u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
 286                        : BLOCK_MODE;
 287         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
 288         u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
 289                         : len;
 290         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
 291 
 292         if (claim)
 293                 sdio_claim_host(card->func);
 294 
 295         ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
 296 
 297         if (claim)
 298                 sdio_release_host(card->func);
 299 
 300         return ret;
 301 }
 302 
 303 /* This function reads the firmware status.
 304  */
 305 static int
 306 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
 307 {
 308         struct sdio_mmc_card *card = adapter->card;
 309         const struct mwifiex_sdio_card_reg *reg = card->reg;
 310         u8 fws0, fws1;
 311 
 312         if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
 313                 return -1;
 314 
 315         if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
 316                 return -1;
 317 
 318         *dat = (u16)((fws1 << 8) | fws0);
 319         return 0;
 320 }
 321 
 322 /* This function checks the firmware status in card.
 323  */
 324 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
 325                                    u32 poll_num)
 326 {
 327         int ret = 0;
 328         u16 firmware_stat;
 329         u32 tries;
 330 
 331         for (tries = 0; tries < poll_num; tries++) {
 332                 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
 333                 if (ret)
 334                         continue;
 335                 if (firmware_stat == FIRMWARE_READY_SDIO) {
 336                         ret = 0;
 337                         break;
 338                 }
 339 
 340                 msleep(100);
 341                 ret = -1;
 342         }
 343 
 344         return ret;
 345 }
 346 
 347 /* This function checks if WLAN is the winner.
 348  */
 349 static int mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
 350 {
 351         int ret = 0;
 352         u8 winner = 0;
 353         struct sdio_mmc_card *card = adapter->card;
 354 
 355         if (mwifiex_read_reg(adapter, card->reg->status_reg_0, &winner))
 356                 return -1;
 357 
 358         if (winner)
 359                 adapter->winner = 0;
 360         else
 361                 adapter->winner = 1;
 362 
 363         return ret;
 364 }
 365 
 366 /*
 367  * SDIO remove.
 368  *
 369  * This function removes the interface and frees up the card structure.
 370  */
 371 static void
 372 mwifiex_sdio_remove(struct sdio_func *func)
 373 {
 374         struct sdio_mmc_card *card;
 375         struct mwifiex_adapter *adapter;
 376         struct mwifiex_private *priv;
 377         int ret = 0;
 378         u16 firmware_stat;
 379 
 380         card = sdio_get_drvdata(func);
 381         if (!card)
 382                 return;
 383 
 384         wait_for_completion(&card->fw_done);
 385 
 386         adapter = card->adapter;
 387         if (!adapter || !adapter->priv_num)
 388                 return;
 389 
 390         mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
 391 
 392         ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
 393         if (!ret && firmware_stat == FIRMWARE_READY_SDIO &&
 394             !adapter->mfg_mode) {
 395                 mwifiex_deauthenticate_all(adapter);
 396 
 397                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
 398                 mwifiex_disable_auto_ds(priv);
 399                 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
 400         }
 401 
 402         mwifiex_remove_card(adapter);
 403 }
 404 
 405 /*
 406  * SDIO suspend.
 407  *
 408  * Kernel needs to suspend all functions separately. Therefore all
 409  * registered functions must have drivers with suspend and resume
 410  * methods. Failing that the kernel simply removes the whole card.
 411  *
 412  * If already not suspended, this function allocates and sends a host
 413  * sleep activate request to the firmware and turns off the traffic.
 414  */
 415 static int mwifiex_sdio_suspend(struct device *dev)
 416 {
 417         struct sdio_func *func = dev_to_sdio_func(dev);
 418         struct sdio_mmc_card *card;
 419         struct mwifiex_adapter *adapter;
 420         mmc_pm_flag_t pm_flag = 0;
 421         int ret = 0;
 422 
 423         pm_flag = sdio_get_host_pm_caps(func);
 424         pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
 425                  sdio_func_id(func), pm_flag);
 426         if (!(pm_flag & MMC_PM_KEEP_POWER)) {
 427                 dev_err(dev, "%s: cannot remain alive while host is"
 428                         " suspended\n", sdio_func_id(func));
 429                 return -ENOSYS;
 430         }
 431 
 432         card = sdio_get_drvdata(func);
 433         if (!card) {
 434                 dev_err(dev, "suspend: invalid card\n");
 435                 return 0;
 436         }
 437 
 438         /* Might still be loading firmware */
 439         wait_for_completion(&card->fw_done);
 440 
 441         adapter = card->adapter;
 442         if (!adapter) {
 443                 dev_err(dev, "adapter is not valid\n");
 444                 return 0;
 445         }
 446 
 447         if (!adapter->is_up)
 448                 return -EBUSY;
 449 
 450         mwifiex_enable_wake(adapter);
 451 
 452         /* Enable the Host Sleep */
 453         if (!mwifiex_enable_hs(adapter)) {
 454                 mwifiex_dbg(adapter, ERROR,
 455                             "cmd: failed to suspend\n");
 456                 clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
 457                 mwifiex_disable_wake(adapter);
 458                 return -EFAULT;
 459         }
 460 
 461         mwifiex_dbg(adapter, INFO,
 462                     "cmd: suspend with MMC_PM_KEEP_POWER\n");
 463         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
 464 
 465         /* Indicate device suspended */
 466         set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
 467         clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
 468 
 469         return ret;
 470 }
 471 
 472 static void mwifiex_sdio_coredump(struct device *dev)
 473 {
 474         struct sdio_func *func = dev_to_sdio_func(dev);
 475         struct sdio_mmc_card *card;
 476 
 477         card = sdio_get_drvdata(func);
 478         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
 479                               &card->work_flags))
 480                 schedule_work(&card->work);
 481 }
 482 
 483 /* Device ID for SD8786 */
 484 #define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
 485 /* Device ID for SD8787 */
 486 #define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
 487 /* Device ID for SD8797 */
 488 #define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
 489 /* Device ID for SD8897 */
 490 #define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
 491 /* Device ID for SD8887 */
 492 #define SDIO_DEVICE_ID_MARVELL_8887   (0x9135)
 493 /* Device ID for SD8801 */
 494 #define SDIO_DEVICE_ID_MARVELL_8801   (0x9139)
 495 /* Device ID for SD8977 */
 496 #define SDIO_DEVICE_ID_MARVELL_8977   (0x9145)
 497 /* Device ID for SD8987 */
 498 #define SDIO_DEVICE_ID_MARVELL_8987   (0x9149)
 499 /* Device ID for SD8997 */
 500 #define SDIO_DEVICE_ID_MARVELL_8997   (0x9141)
 501 
 502 
 503 /* WLAN IDs */
 504 static const struct sdio_device_id mwifiex_ids[] = {
 505         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
 506                 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
 507         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
 508                 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
 509         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
 510                 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
 511         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
 512                 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
 513         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
 514                 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
 515         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
 516                 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
 517         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977),
 518                 .driver_data = (unsigned long)&mwifiex_sdio_sd8977},
 519         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987),
 520                 .driver_data = (unsigned long)&mwifiex_sdio_sd8987},
 521         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
 522                 .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
 523         {},
 524 };
 525 
 526 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
 527 
 528 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
 529         .suspend = mwifiex_sdio_suspend,
 530         .resume = mwifiex_sdio_resume,
 531 };
 532 
 533 static struct sdio_driver mwifiex_sdio = {
 534         .name = "mwifiex_sdio",
 535         .id_table = mwifiex_ids,
 536         .probe = mwifiex_sdio_probe,
 537         .remove = mwifiex_sdio_remove,
 538         .drv = {
 539                 .owner = THIS_MODULE,
 540                 .coredump = mwifiex_sdio_coredump,
 541                 .pm = &mwifiex_sdio_pm_ops,
 542         }
 543 };
 544 
 545 /*
 546  * This function wakes up the card.
 547  *
 548  * A host power up command is written to the card configuration
 549  * register to wake up the card.
 550  */
 551 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
 552 {
 553         mwifiex_dbg(adapter, EVENT,
 554                     "event: wakeup device...\n");
 555 
 556         return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
 557 }
 558 
 559 /*
 560  * This function is called after the card has woken up.
 561  *
 562  * The card configuration register is reset.
 563  */
 564 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
 565 {
 566         mwifiex_dbg(adapter, EVENT,
 567                     "cmd: wakeup device completed\n");
 568 
 569         return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
 570 }
 571 
 572 static int mwifiex_sdio_dnld_fw(struct mwifiex_adapter *adapter,
 573                         struct mwifiex_fw_image *fw)
 574 {
 575         struct sdio_mmc_card *card = adapter->card;
 576         int ret;
 577 
 578         sdio_claim_host(card->func);
 579         ret = mwifiex_dnld_fw(adapter, fw);
 580         sdio_release_host(card->func);
 581 
 582         return ret;
 583 }
 584 
 585 /*
 586  * This function is used to initialize IO ports for the
 587  * chipsets supporting SDIO new mode eg SD8897.
 588  */
 589 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
 590 {
 591         u8 reg;
 592         struct sdio_mmc_card *card = adapter->card;
 593 
 594         adapter->ioport = MEM_PORT;
 595 
 596         /* enable sdio new mode */
 597         if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
 598                 return -1;
 599         if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
 600                               reg | CMD53_NEW_MODE))
 601                 return -1;
 602 
 603         /* Configure cmd port and enable reading rx length from the register */
 604         if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
 605                 return -1;
 606         if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
 607                               reg | CMD_PORT_RD_LEN_EN))
 608                 return -1;
 609 
 610         /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
 611          * completed
 612          */
 613         if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
 614                 return -1;
 615         if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
 616                               reg | CMD_PORT_AUTO_EN))
 617                 return -1;
 618 
 619         return 0;
 620 }
 621 
 622 /* This function initializes the IO ports.
 623  *
 624  * The following operations are performed -
 625  *      - Read the IO ports (0, 1 and 2)
 626  *      - Set host interrupt Reset-To-Read to clear
 627  *      - Set auto re-enable interrupt
 628  */
 629 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
 630 {
 631         u8 reg;
 632         struct sdio_mmc_card *card = adapter->card;
 633 
 634         adapter->ioport = 0;
 635 
 636         if (card->supports_sdio_new_mode) {
 637                 if (mwifiex_init_sdio_new_mode(adapter))
 638                         return -1;
 639                 goto cont;
 640         }
 641 
 642         /* Read the IO port */
 643         if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
 644                 adapter->ioport |= (reg & 0xff);
 645         else
 646                 return -1;
 647 
 648         if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
 649                 adapter->ioport |= ((reg & 0xff) << 8);
 650         else
 651                 return -1;
 652 
 653         if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
 654                 adapter->ioport |= ((reg & 0xff) << 16);
 655         else
 656                 return -1;
 657 cont:
 658         mwifiex_dbg(adapter, INFO,
 659                     "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
 660 
 661         /* Set Host interrupt reset to read to clear */
 662         if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
 663                 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
 664                                   reg | card->reg->sdio_int_mask);
 665         else
 666                 return -1;
 667 
 668         /* Dnld/Upld ready set to auto reset */
 669         if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
 670                 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
 671                                   reg | AUTO_RE_ENABLE_INT);
 672         else
 673                 return -1;
 674 
 675         return 0;
 676 }
 677 
 678 /*
 679  * This function sends data to the card.
 680  */
 681 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
 682                                       u8 *payload, u32 pkt_len, u32 port)
 683 {
 684         u32 i = 0;
 685         int ret;
 686 
 687         do {
 688                 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
 689                 if (ret) {
 690                         i++;
 691                         mwifiex_dbg(adapter, ERROR,
 692                                     "host_to_card, write iomem\t"
 693                                     "(%d) failed: %d\n", i, ret);
 694                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
 695                                 mwifiex_dbg(adapter, ERROR,
 696                                             "write CFG reg failed\n");
 697 
 698                         ret = -1;
 699                         if (i > MAX_WRITE_IOMEM_RETRY)
 700                                 return ret;
 701                 }
 702         } while (ret == -1);
 703 
 704         return ret;
 705 }
 706 
 707 /*
 708  * This function gets the read port.
 709  *
 710  * If control port bit is set in MP read bitmap, the control port
 711  * is returned, otherwise the current read port is returned and
 712  * the value is increased (provided it does not reach the maximum
 713  * limit, in which case it is reset to 1)
 714  */
 715 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
 716 {
 717         struct sdio_mmc_card *card = adapter->card;
 718         const struct mwifiex_sdio_card_reg *reg = card->reg;
 719         u32 rd_bitmap = card->mp_rd_bitmap;
 720 
 721         mwifiex_dbg(adapter, DATA,
 722                     "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
 723 
 724         if (card->supports_sdio_new_mode) {
 725                 if (!(rd_bitmap & reg->data_port_mask))
 726                         return -1;
 727         } else {
 728                 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
 729                         return -1;
 730         }
 731 
 732         if ((card->has_control_mask) &&
 733             (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
 734                 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
 735                 *port = CTRL_PORT;
 736                 mwifiex_dbg(adapter, DATA,
 737                             "data: port=%d mp_rd_bitmap=0x%08x\n",
 738                             *port, card->mp_rd_bitmap);
 739                 return 0;
 740         }
 741 
 742         if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
 743                 return -1;
 744 
 745         /* We are now handling the SDIO data ports */
 746         card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
 747         *port = card->curr_rd_port;
 748 
 749         if (++card->curr_rd_port == card->max_ports)
 750                 card->curr_rd_port = reg->start_rd_port;
 751 
 752         mwifiex_dbg(adapter, DATA,
 753                     "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
 754                     *port, rd_bitmap, card->mp_rd_bitmap);
 755 
 756         return 0;
 757 }
 758 
 759 /*
 760  * This function gets the write port for data.
 761  *
 762  * The current write port is returned if available and the value is
 763  * increased (provided it does not reach the maximum limit, in which
 764  * case it is reset to 1)
 765  */
 766 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
 767 {
 768         struct sdio_mmc_card *card = adapter->card;
 769         const struct mwifiex_sdio_card_reg *reg = card->reg;
 770         u32 wr_bitmap = card->mp_wr_bitmap;
 771 
 772         mwifiex_dbg(adapter, DATA,
 773                     "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
 774 
 775         if (!(wr_bitmap & card->mp_data_port_mask)) {
 776                 adapter->data_sent = true;
 777                 return -EBUSY;
 778         }
 779 
 780         if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
 781                 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
 782                 *port = card->curr_wr_port;
 783                 if (++card->curr_wr_port == card->mp_end_port)
 784                         card->curr_wr_port = reg->start_wr_port;
 785         } else {
 786                 adapter->data_sent = true;
 787                 return -EBUSY;
 788         }
 789 
 790         if ((card->has_control_mask) && (*port == CTRL_PORT)) {
 791                 mwifiex_dbg(adapter, ERROR,
 792                             "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
 793                             *port, card->curr_wr_port, wr_bitmap,
 794                             card->mp_wr_bitmap);
 795                 return -1;
 796         }
 797 
 798         mwifiex_dbg(adapter, DATA,
 799                     "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
 800                     *port, wr_bitmap, card->mp_wr_bitmap);
 801 
 802         return 0;
 803 }
 804 
 805 /*
 806  * This function polls the card status.
 807  */
 808 static int
 809 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
 810 {
 811         struct sdio_mmc_card *card = adapter->card;
 812         u32 tries;
 813         u8 cs;
 814 
 815         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
 816                 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
 817                         break;
 818                 else if ((cs & bits) == bits)
 819                         return 0;
 820 
 821                 usleep_range(10, 20);
 822         }
 823 
 824         mwifiex_dbg(adapter, ERROR,
 825                     "poll card status failed, tries = %d\n", tries);
 826 
 827         return -1;
 828 }
 829 
 830 /*
 831  * This function disables the host interrupt.
 832  *
 833  * The host interrupt mask is read, the disable bit is reset and
 834  * written back to the card host interrupt mask register.
 835  */
 836 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
 837 {
 838         struct sdio_mmc_card *card = adapter->card;
 839         struct sdio_func *func = card->func;
 840 
 841         sdio_claim_host(func);
 842         mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
 843         sdio_release_irq(func);
 844         sdio_release_host(func);
 845 }
 846 
 847 /*
 848  * This function reads the interrupt status from card.
 849  */
 850 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
 851 {
 852         struct sdio_mmc_card *card = adapter->card;
 853         u8 sdio_ireg;
 854         unsigned long flags;
 855 
 856         if (mwifiex_read_data_sync(adapter, card->mp_regs,
 857                                    card->reg->max_mp_regs,
 858                                    REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
 859                 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
 860                 return;
 861         }
 862 
 863         sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
 864         if (sdio_ireg) {
 865                 /*
 866                  * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
 867                  * For SDIO new mode CMD port interrupts
 868                  *      DN_LD_CMD_PORT_HOST_INT_STATUS and/or
 869                  *      UP_LD_CMD_PORT_HOST_INT_STATUS
 870                  * Clear the interrupt status register
 871                  */
 872                 mwifiex_dbg(adapter, INTR,
 873                             "int: sdio_ireg = %#x\n", sdio_ireg);
 874                 spin_lock_irqsave(&adapter->int_lock, flags);
 875                 adapter->int_status |= sdio_ireg;
 876                 spin_unlock_irqrestore(&adapter->int_lock, flags);
 877         }
 878 }
 879 
 880 /*
 881  * SDIO interrupt handler.
 882  *
 883  * This function reads the interrupt status from firmware and handles
 884  * the interrupt in current thread (ksdioirqd) right away.
 885  */
 886 static void
 887 mwifiex_sdio_interrupt(struct sdio_func *func)
 888 {
 889         struct mwifiex_adapter *adapter;
 890         struct sdio_mmc_card *card;
 891 
 892         card = sdio_get_drvdata(func);
 893         if (!card || !card->adapter) {
 894                 pr_err("int: func=%p card=%p adapter=%p\n",
 895                        func, card, card ? card->adapter : NULL);
 896                 return;
 897         }
 898         adapter = card->adapter;
 899 
 900         if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
 901                 adapter->ps_state = PS_STATE_AWAKE;
 902 
 903         mwifiex_interrupt_status(adapter);
 904         mwifiex_main_process(adapter);
 905 }
 906 
 907 /*
 908  * This function enables the host interrupt.
 909  *
 910  * The host interrupt enable mask is written to the card
 911  * host interrupt mask register.
 912  */
 913 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
 914 {
 915         struct sdio_mmc_card *card = adapter->card;
 916         struct sdio_func *func = card->func;
 917         int ret;
 918 
 919         sdio_claim_host(func);
 920 
 921         /* Request the SDIO IRQ */
 922         ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
 923         if (ret) {
 924                 mwifiex_dbg(adapter, ERROR,
 925                             "claim irq failed: ret=%d\n", ret);
 926                 goto out;
 927         }
 928 
 929         /* Simply write the mask to the register */
 930         ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
 931                                        card->reg->host_int_enable);
 932         if (ret) {
 933                 mwifiex_dbg(adapter, ERROR,
 934                             "enable host interrupt failed\n");
 935                 sdio_release_irq(func);
 936         }
 937 
 938 out:
 939         sdio_release_host(func);
 940         return ret;
 941 }
 942 
 943 /*
 944  * This function sends a data buffer to the card.
 945  */
 946 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
 947                                      u32 *type, u8 *buffer,
 948                                      u32 npayload, u32 ioport)
 949 {
 950         int ret;
 951         u32 nb;
 952 
 953         if (!buffer) {
 954                 mwifiex_dbg(adapter, ERROR,
 955                             "%s: buffer is NULL\n", __func__);
 956                 return -1;
 957         }
 958 
 959         ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
 960 
 961         if (ret) {
 962                 mwifiex_dbg(adapter, ERROR,
 963                             "%s: read iomem failed: %d\n", __func__,
 964                         ret);
 965                 return -1;
 966         }
 967 
 968         nb = get_unaligned_le16((buffer));
 969         if (nb > npayload) {
 970                 mwifiex_dbg(adapter, ERROR,
 971                             "%s: invalid packet, nb=%d npayload=%d\n",
 972                             __func__, nb, npayload);
 973                 return -1;
 974         }
 975 
 976         *type = get_unaligned_le16((buffer + 2));
 977 
 978         return ret;
 979 }
 980 
 981 /*
 982  * This function downloads the firmware to the card.
 983  *
 984  * Firmware is downloaded to the card in blocks. Every block download
 985  * is tested for CRC errors, and retried a number of times before
 986  * returning failure.
 987  */
 988 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
 989                                     struct mwifiex_fw_image *fw)
 990 {
 991         struct sdio_mmc_card *card = adapter->card;
 992         const struct mwifiex_sdio_card_reg *reg = card->reg;
 993         int ret;
 994         u8 *firmware = fw->fw_buf;
 995         u32 firmware_len = fw->fw_len;
 996         u32 offset = 0;
 997         u8 base0, base1;
 998         u8 *fwbuf;
 999         u16 len = 0;
1000         u32 txlen, tx_blocks = 0, tries;
1001         u32 i = 0;
1002 
1003         if (!firmware_len) {
1004                 mwifiex_dbg(adapter, ERROR,
1005                             "firmware image not found! Terminating download\n");
1006                 return -1;
1007         }
1008 
1009         mwifiex_dbg(adapter, INFO,
1010                     "info: downloading FW image (%d bytes)\n",
1011                     firmware_len);
1012 
1013         /* Assume that the allocated buffer is 8-byte aligned */
1014         fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
1015         if (!fwbuf)
1016                 return -ENOMEM;
1017 
1018         sdio_claim_host(card->func);
1019 
1020         /* Perform firmware data transfer */
1021         do {
1022                 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
1023                    bits */
1024                 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
1025                                                     DN_LD_CARD_RDY);
1026                 if (ret) {
1027                         mwifiex_dbg(adapter, ERROR,
1028                                     "FW download with helper:\t"
1029                                     "poll status timeout @ %d\n", offset);
1030                         goto done;
1031                 }
1032 
1033                 /* More data? */
1034                 if (offset >= firmware_len)
1035                         break;
1036 
1037                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1038                         ret = mwifiex_read_reg(adapter, reg->base_0_reg,
1039                                                &base0);
1040                         if (ret) {
1041                                 mwifiex_dbg(adapter, ERROR,
1042                                             "dev BASE0 register read failed:\t"
1043                                             "base0=%#04X(%d). Terminating dnld\n",
1044                                             base0, base0);
1045                                 goto done;
1046                         }
1047                         ret = mwifiex_read_reg(adapter, reg->base_1_reg,
1048                                                &base1);
1049                         if (ret) {
1050                                 mwifiex_dbg(adapter, ERROR,
1051                                             "dev BASE1 register read failed:\t"
1052                                             "base1=%#04X(%d). Terminating dnld\n",
1053                                             base1, base1);
1054                                 goto done;
1055                         }
1056                         len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
1057 
1058                         if (len)
1059                                 break;
1060 
1061                         usleep_range(10, 20);
1062                 }
1063 
1064                 if (!len) {
1065                         break;
1066                 } else if (len > MWIFIEX_UPLD_SIZE) {
1067                         mwifiex_dbg(adapter, ERROR,
1068                                     "FW dnld failed @ %d, invalid length %d\n",
1069                                     offset, len);
1070                         ret = -1;
1071                         goto done;
1072                 }
1073 
1074                 txlen = len;
1075 
1076                 if (len & BIT(0)) {
1077                         i++;
1078                         if (i > MAX_WRITE_IOMEM_RETRY) {
1079                                 mwifiex_dbg(adapter, ERROR,
1080                                             "FW dnld failed @ %d, over max retry\n",
1081                                             offset);
1082                                 ret = -1;
1083                                 goto done;
1084                         }
1085                         mwifiex_dbg(adapter, ERROR,
1086                                     "CRC indicated by the helper:\t"
1087                                     "len = 0x%04X, txlen = %d\n", len, txlen);
1088                         len &= ~BIT(0);
1089                         /* Setting this to 0 to resend from same offset */
1090                         txlen = 0;
1091                 } else {
1092                         i = 0;
1093 
1094                         /* Set blocksize to transfer - checking for last
1095                            block */
1096                         if (firmware_len - offset < txlen)
1097                                 txlen = firmware_len - offset;
1098 
1099                         tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
1100                                     / MWIFIEX_SDIO_BLOCK_SIZE;
1101 
1102                         /* Copy payload to buffer */
1103                         memmove(fwbuf, &firmware[offset], txlen);
1104                 }
1105 
1106                 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1107                                               MWIFIEX_SDIO_BLOCK_SIZE,
1108                                               adapter->ioport);
1109                 if (ret) {
1110                         mwifiex_dbg(adapter, ERROR,
1111                                     "FW download, write iomem (%d) failed @ %d\n",
1112                                     i, offset);
1113                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1114                                 mwifiex_dbg(adapter, ERROR,
1115                                             "write CFG reg failed\n");
1116 
1117                         ret = -1;
1118                         goto done;
1119                 }
1120 
1121                 offset += txlen;
1122         } while (true);
1123 
1124         mwifiex_dbg(adapter, MSG,
1125                     "info: FW download over, size %d bytes\n", offset);
1126 
1127         ret = 0;
1128 done:
1129         sdio_release_host(card->func);
1130         kfree(fwbuf);
1131         return ret;
1132 }
1133 
1134 /*
1135  * This function decode sdio aggreation pkt.
1136  *
1137  * Based on the the data block size and pkt_len,
1138  * skb data will be decoded to few packets.
1139  */
1140 static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1141                                     struct sk_buff *skb)
1142 {
1143         u32 total_pkt_len, pkt_len;
1144         struct sk_buff *skb_deaggr;
1145         u16 blk_size;
1146         u8 blk_num;
1147         u8 *data;
1148 
1149         data = skb->data;
1150         total_pkt_len = skb->len;
1151 
1152         while (total_pkt_len >= (SDIO_HEADER_OFFSET + adapter->intf_hdr_len)) {
1153                 if (total_pkt_len < adapter->sdio_rx_block_size)
1154                         break;
1155                 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1156                 blk_size = adapter->sdio_rx_block_size * blk_num;
1157                 if (blk_size > total_pkt_len) {
1158                         mwifiex_dbg(adapter, ERROR,
1159                                     "%s: error in blk_size,\t"
1160                                     "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1161                                     __func__, blk_num, blk_size, total_pkt_len);
1162                         break;
1163                 }
1164                 pkt_len = get_unaligned_le16((data +
1165                                              SDIO_HEADER_OFFSET));
1166                 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
1167                         mwifiex_dbg(adapter, ERROR,
1168                                     "%s: error in pkt_len,\t"
1169                                     "pkt_len=%d, blk_size=%d\n",
1170                                     __func__, pkt_len, blk_size);
1171                         break;
1172                 }
1173 
1174                 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len, GFP_KERNEL);
1175                 if (!skb_deaggr)
1176                         break;
1177                 skb_put(skb_deaggr, pkt_len);
1178                 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1179                 skb_pull(skb_deaggr, adapter->intf_hdr_len);
1180 
1181                 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1182                 data += blk_size;
1183                 total_pkt_len -= blk_size;
1184         }
1185 }
1186 
1187 /*
1188  * This function decodes a received packet.
1189  *
1190  * Based on the type, the packet is treated as either a data, or
1191  * a command response, or an event, and the correct handler
1192  * function is invoked.
1193  */
1194 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1195                                     struct sk_buff *skb, u32 upld_typ)
1196 {
1197         u8 *cmd_buf;
1198         u16 pkt_len;
1199         struct mwifiex_rxinfo *rx_info;
1200 
1201         pkt_len = get_unaligned_le16(skb->data);
1202 
1203         if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1204                 skb_trim(skb, pkt_len);
1205                 skb_pull(skb, adapter->intf_hdr_len);
1206         }
1207 
1208         switch (upld_typ) {
1209         case MWIFIEX_TYPE_AGGR_DATA:
1210                 mwifiex_dbg(adapter, INFO,
1211                             "info: --- Rx: Aggr Data packet ---\n");
1212                 rx_info = MWIFIEX_SKB_RXCB(skb);
1213                 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1214                 if (adapter->rx_work_enabled) {
1215                         skb_queue_tail(&adapter->rx_data_q, skb);
1216                         atomic_inc(&adapter->rx_pending);
1217                         adapter->data_received = true;
1218                 } else {
1219                         mwifiex_deaggr_sdio_pkt(adapter, skb);
1220                         dev_kfree_skb_any(skb);
1221                 }
1222                 break;
1223 
1224         case MWIFIEX_TYPE_DATA:
1225                 mwifiex_dbg(adapter, DATA,
1226                             "info: --- Rx: Data packet ---\n");
1227                 if (adapter->rx_work_enabled) {
1228                         skb_queue_tail(&adapter->rx_data_q, skb);
1229                         adapter->data_received = true;
1230                         atomic_inc(&adapter->rx_pending);
1231                 } else {
1232                         mwifiex_handle_rx_packet(adapter, skb);
1233                 }
1234                 break;
1235 
1236         case MWIFIEX_TYPE_CMD:
1237                 mwifiex_dbg(adapter, CMD,
1238                             "info: --- Rx: Cmd Response ---\n");
1239                 /* take care of curr_cmd = NULL case */
1240                 if (!adapter->curr_cmd) {
1241                         cmd_buf = adapter->upld_buf;
1242 
1243                         if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1244                                 mwifiex_process_sleep_confirm_resp(adapter,
1245                                                                    skb->data,
1246                                                                    skb->len);
1247 
1248                         memcpy(cmd_buf, skb->data,
1249                                min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1250                                      skb->len));
1251 
1252                         dev_kfree_skb_any(skb);
1253                 } else {
1254                         adapter->cmd_resp_received = true;
1255                         adapter->curr_cmd->resp_skb = skb;
1256                 }
1257                 break;
1258 
1259         case MWIFIEX_TYPE_EVENT:
1260                 mwifiex_dbg(adapter, EVENT,
1261                             "info: --- Rx: Event ---\n");
1262                 adapter->event_cause = get_unaligned_le32(skb->data);
1263 
1264                 if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1265                         memcpy(adapter->event_body,
1266                                skb->data + MWIFIEX_EVENT_HEADER_LEN,
1267                                skb->len);
1268 
1269                 /* event cause has been saved to adapter->event_cause */
1270                 adapter->event_received = true;
1271                 adapter->event_skb = skb;
1272 
1273                 break;
1274 
1275         default:
1276                 mwifiex_dbg(adapter, ERROR,
1277                             "unknown upload type %#x\n", upld_typ);
1278                 dev_kfree_skb_any(skb);
1279                 break;
1280         }
1281 
1282         return 0;
1283 }
1284 
1285 /*
1286  * This function transfers received packets from card to driver, performing
1287  * aggregation if required.
1288  *
1289  * For data received on control port, or if aggregation is disabled, the
1290  * received buffers are uploaded as separate packets. However, if aggregation
1291  * is enabled and required, the buffers are copied onto an aggregation buffer,
1292  * provided there is space left, processed and finally uploaded.
1293  */
1294 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1295                                              u16 rx_len, u8 port)
1296 {
1297         struct sdio_mmc_card *card = adapter->card;
1298         s32 f_do_rx_aggr = 0;
1299         s32 f_do_rx_cur = 0;
1300         s32 f_aggr_cur = 0;
1301         s32 f_post_aggr_cur = 0;
1302         struct sk_buff *skb_deaggr;
1303         struct sk_buff *skb = NULL;
1304         u32 pkt_len, pkt_type, mport, pind;
1305         u8 *curr_ptr;
1306 
1307         if ((card->has_control_mask) && (port == CTRL_PORT)) {
1308                 /* Read the command Resp without aggr */
1309                 mwifiex_dbg(adapter, CMD,
1310                             "info: %s: no aggregation for cmd\t"
1311                             "response\n", __func__);
1312 
1313                 f_do_rx_cur = 1;
1314                 goto rx_curr_single;
1315         }
1316 
1317         if (!card->mpa_rx.enabled) {
1318                 mwifiex_dbg(adapter, WARN,
1319                             "info: %s: rx aggregation disabled\n",
1320                             __func__);
1321 
1322                 f_do_rx_cur = 1;
1323                 goto rx_curr_single;
1324         }
1325 
1326         if ((!card->has_control_mask && (card->mp_rd_bitmap &
1327                                          card->reg->data_port_mask)) ||
1328             (card->has_control_mask && (card->mp_rd_bitmap &
1329                                         (~((u32) CTRL_PORT_MASK))))) {
1330                 /* Some more data RX pending */
1331                 mwifiex_dbg(adapter, INFO,
1332                             "info: %s: not last packet\n", __func__);
1333 
1334                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1335                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1336                                 f_aggr_cur = 1;
1337                         } else {
1338                                 /* No room in Aggr buf, do rx aggr now */
1339                                 f_do_rx_aggr = 1;
1340                                 f_post_aggr_cur = 1;
1341                         }
1342                 } else {
1343                         /* Rx aggr not in progress */
1344                         f_aggr_cur = 1;
1345                 }
1346 
1347         } else {
1348                 /* No more data RX pending */
1349                 mwifiex_dbg(adapter, INFO,
1350                             "info: %s: last packet\n", __func__);
1351 
1352                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1353                         f_do_rx_aggr = 1;
1354                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
1355                                 f_aggr_cur = 1;
1356                         else
1357                                 /* No room in Aggr buf, do rx aggr now */
1358                                 f_do_rx_cur = 1;
1359                 } else {
1360                         f_do_rx_cur = 1;
1361                 }
1362         }
1363 
1364         if (f_aggr_cur) {
1365                 mwifiex_dbg(adapter, INFO,
1366                             "info: current packet aggregation\n");
1367                 /* Curr pkt can be aggregated */
1368                 mp_rx_aggr_setup(card, rx_len, port);
1369 
1370                 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1371                     mp_rx_aggr_port_limit_reached(card)) {
1372                         mwifiex_dbg(adapter, INFO,
1373                                     "info: %s: aggregated packet\t"
1374                                     "limit reached\n", __func__);
1375                         /* No more pkts allowed in Aggr buf, rx it */
1376                         f_do_rx_aggr = 1;
1377                 }
1378         }
1379 
1380         if (f_do_rx_aggr) {
1381                 /* do aggr RX now */
1382                 mwifiex_dbg(adapter, DATA,
1383                             "info: do_rx_aggr: num of packets: %d\n",
1384                             card->mpa_rx.pkt_cnt);
1385 
1386                 if (card->supports_sdio_new_mode) {
1387                         int i;
1388                         u32 port_count;
1389 
1390                         for (i = 0, port_count = 0; i < card->max_ports; i++)
1391                                 if (card->mpa_rx.ports & BIT(i))
1392                                         port_count++;
1393 
1394                         /* Reading data from "start_port + 0" to "start_port +
1395                          * port_count -1", so decrease the count by 1
1396                          */
1397                         port_count--;
1398                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1399                                  (port_count << 8)) + card->mpa_rx.start_port;
1400                 } else {
1401                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1402                                  (card->mpa_rx.ports << 4)) +
1403                                  card->mpa_rx.start_port;
1404                 }
1405 
1406                 if (card->mpa_rx.pkt_cnt == 1)
1407                         mport = adapter->ioport + card->mpa_rx.start_port;
1408 
1409                 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1410                                            card->mpa_rx.buf_len, mport, 1))
1411                         goto error;
1412 
1413                 curr_ptr = card->mpa_rx.buf;
1414 
1415                 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1416                         u32 *len_arr = card->mpa_rx.len_arr;
1417 
1418                         /* get curr PKT len & type */
1419                         pkt_len = get_unaligned_le16(&curr_ptr[0]);
1420                         pkt_type = get_unaligned_le16(&curr_ptr[2]);
1421 
1422                         /* copy pkt to deaggr buf */
1423                         skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1424                                                                  GFP_KERNEL);
1425                         if (!skb_deaggr) {
1426                                 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1427                                             "drop pkt len=%d type=%d\n",
1428                                             pkt_len, pkt_type);
1429                                 curr_ptr += len_arr[pind];
1430                                 continue;
1431                         }
1432 
1433                         skb_put(skb_deaggr, len_arr[pind]);
1434 
1435                         if ((pkt_type == MWIFIEX_TYPE_DATA ||
1436                              (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1437                               adapter->sdio_rx_aggr_enable)) &&
1438                             (pkt_len <= len_arr[pind])) {
1439 
1440                                 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1441 
1442                                 skb_trim(skb_deaggr, pkt_len);
1443 
1444                                 /* Process de-aggr packet */
1445                                 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1446                                                          pkt_type);
1447                         } else {
1448                                 mwifiex_dbg(adapter, ERROR,
1449                                             "drop wrong aggr pkt:\t"
1450                                             "sdio_single_port_rx_aggr=%d\t"
1451                                             "type=%d len=%d max_len=%d\n",
1452                                             adapter->sdio_rx_aggr_enable,
1453                                             pkt_type, pkt_len, len_arr[pind]);
1454                                 dev_kfree_skb_any(skb_deaggr);
1455                         }
1456                         curr_ptr += len_arr[pind];
1457                 }
1458                 MP_RX_AGGR_BUF_RESET(card);
1459         }
1460 
1461 rx_curr_single:
1462         if (f_do_rx_cur) {
1463                 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1464                             port, rx_len);
1465 
1466                 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1467                 if (!skb) {
1468                         mwifiex_dbg(adapter, ERROR,
1469                                     "single skb allocated fail,\t"
1470                                     "drop pkt port=%d len=%d\n", port, rx_len);
1471                         if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1472                                                       card->mpa_rx.buf, rx_len,
1473                                                       adapter->ioport + port))
1474                                 goto error;
1475                         return 0;
1476                 }
1477 
1478                 skb_put(skb, rx_len);
1479 
1480                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1481                                               skb->data, skb->len,
1482                                               adapter->ioport + port))
1483                         goto error;
1484                 if (!adapter->sdio_rx_aggr_enable &&
1485                     pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1486                         mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1487                                     "current SDIO RX Aggr not enabled\n",
1488                                     pkt_type);
1489                         dev_kfree_skb_any(skb);
1490                         return 0;
1491                 }
1492 
1493                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1494         }
1495         if (f_post_aggr_cur) {
1496                 mwifiex_dbg(adapter, INFO,
1497                             "info: current packet aggregation\n");
1498                 /* Curr pkt can be aggregated */
1499                 mp_rx_aggr_setup(card, rx_len, port);
1500         }
1501 
1502         return 0;
1503 error:
1504         if (MP_RX_AGGR_IN_PROGRESS(card))
1505                 MP_RX_AGGR_BUF_RESET(card);
1506 
1507         if (f_do_rx_cur && skb)
1508                 /* Single transfer pending. Free curr buff also */
1509                 dev_kfree_skb_any(skb);
1510 
1511         return -1;
1512 }
1513 
1514 /*
1515  * This function checks the current interrupt status.
1516  *
1517  * The following interrupts are checked and handled by this function -
1518  *      - Data sent
1519  *      - Command sent
1520  *      - Packets received
1521  *
1522  * Since the firmware does not generate download ready interrupt if the
1523  * port updated is command port only, command sent interrupt checking
1524  * should be done manually, and for every SDIO interrupt.
1525  *
1526  * In case of Rx packets received, the packets are uploaded from card to
1527  * host and processed accordingly.
1528  */
1529 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1530 {
1531         struct sdio_mmc_card *card = adapter->card;
1532         const struct mwifiex_sdio_card_reg *reg = card->reg;
1533         int ret = 0;
1534         u8 sdio_ireg;
1535         struct sk_buff *skb;
1536         u8 port = CTRL_PORT;
1537         u32 len_reg_l, len_reg_u;
1538         u32 rx_blocks;
1539         u16 rx_len;
1540         unsigned long flags;
1541         u32 bitmap;
1542         u8 cr;
1543 
1544         spin_lock_irqsave(&adapter->int_lock, flags);
1545         sdio_ireg = adapter->int_status;
1546         adapter->int_status = 0;
1547         spin_unlock_irqrestore(&adapter->int_lock, flags);
1548 
1549         if (!sdio_ireg)
1550                 return ret;
1551 
1552         /* Following interrupt is only for SDIO new mode */
1553         if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1554                 adapter->cmd_sent = false;
1555 
1556         /* Following interrupt is only for SDIO new mode */
1557         if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1558                 u32 pkt_type;
1559 
1560                 /* read the len of control packet */
1561                 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1562                 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
1563                 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1564                 if (rx_len <= adapter->intf_hdr_len ||
1565                     (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1566                      MWIFIEX_RX_DATA_BUF_SIZE)
1567                         return -1;
1568                 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1569                 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1570 
1571                 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL);
1572                 if (!skb)
1573                         return -1;
1574 
1575                 skb_put(skb, rx_len);
1576 
1577                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1578                                               skb->len, adapter->ioport |
1579                                                         CMD_PORT_SLCT)) {
1580                         mwifiex_dbg(adapter, ERROR,
1581                                     "%s: failed to card_to_host", __func__);
1582                         dev_kfree_skb_any(skb);
1583                         goto term_cmd;
1584                 }
1585 
1586                 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1587                     (pkt_type != MWIFIEX_TYPE_EVENT))
1588                         mwifiex_dbg(adapter, ERROR,
1589                                     "%s:Received wrong packet on cmd port",
1590                                     __func__);
1591 
1592                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1593         }
1594 
1595         if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1596                 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1597                 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1598                 if (card->supports_sdio_new_mode) {
1599                         bitmap |=
1600                                 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1601                         bitmap |=
1602                                 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1603                 }
1604                 card->mp_wr_bitmap = bitmap;
1605 
1606                 mwifiex_dbg(adapter, INTR,
1607                             "int: DNLD: wr_bitmap=0x%x\n",
1608                             card->mp_wr_bitmap);
1609                 if (adapter->data_sent &&
1610                     (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1611                         mwifiex_dbg(adapter, INTR,
1612                                     "info:  <--- Tx DONE Interrupt --->\n");
1613                         adapter->data_sent = false;
1614                 }
1615         }
1616 
1617         /* As firmware will not generate download ready interrupt if the port
1618            updated is command port only, cmd_sent should be done for any SDIO
1619            interrupt. */
1620         if (card->has_control_mask && adapter->cmd_sent) {
1621                 /* Check if firmware has attach buffer at command port and
1622                    update just that in wr_bit_map. */
1623                 card->mp_wr_bitmap |=
1624                         (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1625                 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1626                         adapter->cmd_sent = false;
1627         }
1628 
1629         mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1630                     adapter->cmd_sent, adapter->data_sent);
1631         if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1632                 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1633                 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1634                 if (card->supports_sdio_new_mode) {
1635                         bitmap |=
1636                                 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1637                         bitmap |=
1638                                 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1639                 }
1640                 card->mp_rd_bitmap = bitmap;
1641                 mwifiex_dbg(adapter, INTR,
1642                             "int: UPLD: rd_bitmap=0x%x\n",
1643                             card->mp_rd_bitmap);
1644 
1645                 while (true) {
1646                         ret = mwifiex_get_rd_port(adapter, &port);
1647                         if (ret) {
1648                                 mwifiex_dbg(adapter, INFO,
1649                                             "info: no more rd_port available\n");
1650                                 break;
1651                         }
1652                         len_reg_l = reg->rd_len_p0_l + (port << 1);
1653                         len_reg_u = reg->rd_len_p0_u + (port << 1);
1654                         rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1655                         rx_len |= (u16) card->mp_regs[len_reg_l];
1656                         mwifiex_dbg(adapter, INFO,
1657                                     "info: RX: port=%d rx_len=%u\n",
1658                                     port, rx_len);
1659                         rx_blocks =
1660                                 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1661                                  1) / MWIFIEX_SDIO_BLOCK_SIZE;
1662                         if (rx_len <= adapter->intf_hdr_len ||
1663                             (card->mpa_rx.enabled &&
1664                              ((rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1665                               card->mpa_rx.buf_size))) {
1666                                 mwifiex_dbg(adapter, ERROR,
1667                                             "invalid rx_len=%d\n",
1668                                             rx_len);
1669                                 return -1;
1670                         }
1671 
1672                         rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1673                         mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1674                                     rx_len);
1675 
1676                         if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1677                                                               port)) {
1678                                 mwifiex_dbg(adapter, ERROR,
1679                                             "card_to_host_mpa failed: int status=%#x\n",
1680                                             sdio_ireg);
1681                                 goto term_cmd;
1682                         }
1683                 }
1684         }
1685 
1686         return 0;
1687 
1688 term_cmd:
1689         /* terminate cmd */
1690         if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1691                 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1692         else
1693                 mwifiex_dbg(adapter, INFO,
1694                             "info: CFG reg val = %d\n", cr);
1695 
1696         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1697                 mwifiex_dbg(adapter, ERROR,
1698                             "write CFG reg failed\n");
1699         else
1700                 mwifiex_dbg(adapter, INFO, "info: write success\n");
1701 
1702         if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1703                 mwifiex_dbg(adapter, ERROR,
1704                             "read CFG reg failed\n");
1705         else
1706                 mwifiex_dbg(adapter, INFO,
1707                             "info: CFG reg val =%x\n", cr);
1708 
1709         return -1;
1710 }
1711 
1712 /*
1713  * This function aggregates transmission buffers in driver and downloads
1714  * the aggregated packet to card.
1715  *
1716  * The individual packets are aggregated by copying into an aggregation
1717  * buffer and then downloaded to the card. Previous unsent packets in the
1718  * aggregation buffer are pre-copied first before new packets are added.
1719  * Aggregation is done till there is space left in the aggregation buffer,
1720  * or till new packets are available.
1721  *
1722  * The function will only download the packet to the card when aggregation
1723  * stops, otherwise it will just aggregate the packet in aggregation buffer
1724  * and return.
1725  */
1726 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1727                                         u8 *payload, u32 pkt_len, u32 port,
1728                                         u32 next_pkt_len)
1729 {
1730         struct sdio_mmc_card *card = adapter->card;
1731         int ret = 0;
1732         s32 f_send_aggr_buf = 0;
1733         s32 f_send_cur_buf = 0;
1734         s32 f_precopy_cur_buf = 0;
1735         s32 f_postcopy_cur_buf = 0;
1736         u32 mport;
1737         int index;
1738 
1739         if (!card->mpa_tx.enabled ||
1740             (card->has_control_mask && (port == CTRL_PORT)) ||
1741             (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1742                 mwifiex_dbg(adapter, WARN,
1743                             "info: %s: tx aggregation disabled\n",
1744                             __func__);
1745 
1746                 f_send_cur_buf = 1;
1747                 goto tx_curr_single;
1748         }
1749 
1750         if (next_pkt_len) {
1751                 /* More pkt in TX queue */
1752                 mwifiex_dbg(adapter, INFO,
1753                             "info: %s: more packets in queue.\n",
1754                             __func__);
1755 
1756                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1757                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1758                                 f_precopy_cur_buf = 1;
1759 
1760                                 if (!(card->mp_wr_bitmap &
1761                                       (1 << card->curr_wr_port)) ||
1762                                     !MP_TX_AGGR_BUF_HAS_ROOM(
1763                                             card, pkt_len + next_pkt_len))
1764                                         f_send_aggr_buf = 1;
1765                         } else {
1766                                 /* No room in Aggr buf, send it */
1767                                 f_send_aggr_buf = 1;
1768 
1769                                 if (!(card->mp_wr_bitmap &
1770                                       (1 << card->curr_wr_port)))
1771                                         f_send_cur_buf = 1;
1772                                 else
1773                                         f_postcopy_cur_buf = 1;
1774                         }
1775                 } else {
1776                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1777                             (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1778                                 f_precopy_cur_buf = 1;
1779                         else
1780                                 f_send_cur_buf = 1;
1781                 }
1782         } else {
1783                 /* Last pkt in TX queue */
1784                 mwifiex_dbg(adapter, INFO,
1785                             "info: %s: Last packet in Tx Queue.\n",
1786                             __func__);
1787 
1788                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1789                         /* some packs in Aggr buf already */
1790                         f_send_aggr_buf = 1;
1791 
1792                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1793                                 f_precopy_cur_buf = 1;
1794                         else
1795                                 /* No room in Aggr buf, send it */
1796                                 f_send_cur_buf = 1;
1797                 } else {
1798                         f_send_cur_buf = 1;
1799                 }
1800         }
1801 
1802         if (f_precopy_cur_buf) {
1803                 mwifiex_dbg(adapter, DATA,
1804                             "data: %s: precopy current buffer\n",
1805                             __func__);
1806                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1807 
1808                 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1809                     mp_tx_aggr_port_limit_reached(card))
1810                         /* No more pkts allowed in Aggr buf, send it */
1811                         f_send_aggr_buf = 1;
1812         }
1813 
1814         if (f_send_aggr_buf) {
1815                 mwifiex_dbg(adapter, DATA,
1816                             "data: %s: send aggr buffer: %d %d\n",
1817                             __func__, card->mpa_tx.start_port,
1818                             card->mpa_tx.ports);
1819                 if (card->supports_sdio_new_mode) {
1820                         u32 port_count;
1821                         int i;
1822 
1823                         for (i = 0, port_count = 0; i < card->max_ports; i++)
1824                                 if (card->mpa_tx.ports & BIT(i))
1825                                         port_count++;
1826 
1827                         /* Writing data from "start_port + 0" to "start_port +
1828                          * port_count -1", so decrease the count by 1
1829                          */
1830                         port_count--;
1831                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1832                                  (port_count << 8)) + card->mpa_tx.start_port;
1833                 } else {
1834                         mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1835                                  (card->mpa_tx.ports << 4)) +
1836                                  card->mpa_tx.start_port;
1837                 }
1838 
1839                 if (card->mpa_tx.pkt_cnt == 1)
1840                         mport = adapter->ioport + card->mpa_tx.start_port;
1841 
1842                 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1843                                                  card->mpa_tx.buf_len, mport);
1844 
1845                 /* Save the last multi port tx aggreagation info to debug log */
1846                 index = adapter->dbg.last_sdio_mp_index;
1847                 index = (index + 1) % MWIFIEX_DBG_SDIO_MP_NUM;
1848                 adapter->dbg.last_sdio_mp_index = index;
1849                 adapter->dbg.last_mp_wr_ports[index] = mport;
1850                 adapter->dbg.last_mp_wr_bitmap[index] = card->mp_wr_bitmap;
1851                 adapter->dbg.last_mp_wr_len[index] = card->mpa_tx.buf_len;
1852                 adapter->dbg.last_mp_curr_wr_port[index] = card->curr_wr_port;
1853 
1854                 MP_TX_AGGR_BUF_RESET(card);
1855         }
1856 
1857 tx_curr_single:
1858         if (f_send_cur_buf) {
1859                 mwifiex_dbg(adapter, DATA,
1860                             "data: %s: send current buffer %d\n",
1861                             __func__, port);
1862                 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1863                                                  adapter->ioport + port);
1864         }
1865 
1866         if (f_postcopy_cur_buf) {
1867                 mwifiex_dbg(adapter, DATA,
1868                             "data: %s: postcopy current buffer\n",
1869                             __func__);
1870                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1871         }
1872 
1873         return ret;
1874 }
1875 
1876 /*
1877  * This function downloads data from driver to card.
1878  *
1879  * Both commands and data packets are transferred to the card by this
1880  * function.
1881  *
1882  * This function adds the SDIO specific header to the front of the buffer
1883  * before transferring. The header contains the length of the packet and
1884  * the type. The firmware handles the packets based upon this set type.
1885  */
1886 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1887                                      u8 type, struct sk_buff *skb,
1888                                      struct mwifiex_tx_param *tx_param)
1889 {
1890         struct sdio_mmc_card *card = adapter->card;
1891         int ret;
1892         u32 buf_block_len;
1893         u32 blk_size;
1894         u32 port = CTRL_PORT;
1895         u8 *payload = (u8 *)skb->data;
1896         u32 pkt_len = skb->len;
1897 
1898         /* Allocate buffer and copy payload */
1899         blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1900         buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1901         put_unaligned_le16((u16)pkt_len, payload + 0);
1902         put_unaligned_le16((u32)type, payload + 2);
1903 
1904 
1905         /*
1906          * This is SDIO specific header
1907          *  u16 length,
1908          *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1909          *  MWIFIEX_TYPE_EVENT = 3)
1910          */
1911         if (type == MWIFIEX_TYPE_DATA) {
1912                 ret = mwifiex_get_wr_port_data(adapter, &port);
1913                 if (ret) {
1914                         mwifiex_dbg(adapter, ERROR,
1915                                     "%s: no wr_port available\n",
1916                                     __func__);
1917                         return ret;
1918                 }
1919         } else {
1920                 adapter->cmd_sent = true;
1921                 /* Type must be MWIFIEX_TYPE_CMD */
1922 
1923                 if (pkt_len <= adapter->intf_hdr_len ||
1924                     pkt_len > MWIFIEX_UPLD_SIZE)
1925                         mwifiex_dbg(adapter, ERROR,
1926                                     "%s: payload=%p, nb=%d\n",
1927                                     __func__, payload, pkt_len);
1928 
1929                 if (card->supports_sdio_new_mode)
1930                         port = CMD_PORT_SLCT;
1931         }
1932 
1933         /* Transfer data to card */
1934         pkt_len = buf_block_len * blk_size;
1935 
1936         if (tx_param)
1937                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1938                                                    port, tx_param->next_pkt_len
1939                                                    );
1940         else
1941                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1942                                                    port, 0);
1943 
1944         if (ret) {
1945                 if (type == MWIFIEX_TYPE_CMD)
1946                         adapter->cmd_sent = false;
1947                 if (type == MWIFIEX_TYPE_DATA) {
1948                         adapter->data_sent = false;
1949                         /* restore curr_wr_port in error cases */
1950                         card->curr_wr_port = port;
1951                         card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1952                 }
1953         } else {
1954                 if (type == MWIFIEX_TYPE_DATA) {
1955                         if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1956                                 adapter->data_sent = true;
1957                         else
1958                                 adapter->data_sent = false;
1959                 }
1960         }
1961 
1962         return ret;
1963 }
1964 
1965 /*
1966  * This function allocates the MPA Tx and Rx buffers.
1967  */
1968 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1969                                    u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1970 {
1971         struct sdio_mmc_card *card = adapter->card;
1972         u32 rx_buf_size;
1973         int ret = 0;
1974 
1975         card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1976         if (!card->mpa_tx.buf) {
1977                 ret = -1;
1978                 goto error;
1979         }
1980 
1981         card->mpa_tx.buf_size = mpa_tx_buf_size;
1982 
1983         rx_buf_size = max_t(u32, mpa_rx_buf_size,
1984                             (u32)SDIO_MAX_AGGR_BUF_SIZE);
1985         card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
1986         if (!card->mpa_rx.buf) {
1987                 ret = -1;
1988                 goto error;
1989         }
1990 
1991         card->mpa_rx.buf_size = rx_buf_size;
1992 
1993 error:
1994         if (ret) {
1995                 kfree(card->mpa_tx.buf);
1996                 kfree(card->mpa_rx.buf);
1997                 card->mpa_tx.buf_size = 0;
1998                 card->mpa_rx.buf_size = 0;
1999         }
2000 
2001         return ret;
2002 }
2003 
2004 /*
2005  * This function unregisters the SDIO device.
2006  *
2007  * The SDIO IRQ is released, the function is disabled and driver
2008  * data is set to null.
2009  */
2010 static void
2011 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2012 {
2013         struct sdio_mmc_card *card = adapter->card;
2014 
2015         if (adapter->card) {
2016                 card->adapter = NULL;
2017                 sdio_claim_host(card->func);
2018                 sdio_disable_func(card->func);
2019                 sdio_release_host(card->func);
2020         }
2021 }
2022 
2023 /*
2024  * This function registers the SDIO device.
2025  *
2026  * SDIO IRQ is claimed, block size is set and driver data is initialized.
2027  */
2028 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2029 {
2030         int ret;
2031         struct sdio_mmc_card *card = adapter->card;
2032         struct sdio_func *func = card->func;
2033 
2034         /* save adapter pointer in card */
2035         card->adapter = adapter;
2036         adapter->tx_buf_size = card->tx_buf_size;
2037 
2038         sdio_claim_host(func);
2039 
2040         /* Set block size */
2041         ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2042         sdio_release_host(func);
2043         if (ret) {
2044                 mwifiex_dbg(adapter, ERROR,
2045                             "cannot set SDIO block size\n");
2046                 return ret;
2047         }
2048 
2049         strcpy(adapter->fw_name, card->firmware);
2050         if (card->fw_dump_enh) {
2051                 adapter->mem_type_mapping_tbl = generic_mem_type_map;
2052                 adapter->num_mem_types = 1;
2053         } else {
2054                 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2055                 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2056         }
2057 
2058         return 0;
2059 }
2060 
2061 /*
2062  * This function initializes the SDIO driver.
2063  *
2064  * The following initializations steps are followed -
2065  *      - Read the Host interrupt status register to acknowledge
2066  *        the first interrupt got from bootloader
2067  *      - Disable host interrupt mask register
2068  *      - Get SDIO port
2069  *      - Initialize SDIO variables in card
2070  *      - Allocate MP registers
2071  *      - Allocate MPA Tx and Rx buffers
2072  */
2073 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
2074 {
2075         struct sdio_mmc_card *card = adapter->card;
2076         const struct mwifiex_sdio_card_reg *reg = card->reg;
2077         int ret;
2078         u8 sdio_ireg;
2079 
2080         sdio_set_drvdata(card->func, card);
2081 
2082         /*
2083          * Read the host_int_status_reg for ACK the first interrupt got
2084          * from the bootloader. If we don't do this we get a interrupt
2085          * as soon as we register the irq.
2086          */
2087         mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2088 
2089         /* Get SDIO ioport */
2090         mwifiex_init_sdio_ioport(adapter);
2091 
2092         /* Initialize SDIO variables in card */
2093         card->mp_rd_bitmap = 0;
2094         card->mp_wr_bitmap = 0;
2095         card->curr_rd_port = reg->start_rd_port;
2096         card->curr_wr_port = reg->start_wr_port;
2097 
2098         card->mp_data_port_mask = reg->data_port_mask;
2099 
2100         card->mpa_tx.buf_len = 0;
2101         card->mpa_tx.pkt_cnt = 0;
2102         card->mpa_tx.start_port = 0;
2103 
2104         card->mpa_tx.enabled = 1;
2105         card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2106 
2107         card->mpa_rx.buf_len = 0;
2108         card->mpa_rx.pkt_cnt = 0;
2109         card->mpa_rx.start_port = 0;
2110 
2111         card->mpa_rx.enabled = 1;
2112         card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2113 
2114         /* Allocate buffers for SDIO MP-A */
2115         card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2116         if (!card->mp_regs)
2117                 return -ENOMEM;
2118 
2119         /* Allocate skb pointer buffers */
2120         card->mpa_rx.skb_arr = kcalloc(card->mp_agg_pkt_limit, sizeof(void *),
2121                                        GFP_KERNEL);
2122         if (!card->mpa_rx.skb_arr) {
2123                 kfree(card->mp_regs);
2124                 return -ENOMEM;
2125         }
2126 
2127         card->mpa_rx.len_arr = kcalloc(card->mp_agg_pkt_limit,
2128                                        sizeof(*card->mpa_rx.len_arr),
2129                                        GFP_KERNEL);
2130         if (!card->mpa_rx.len_arr) {
2131                 kfree(card->mp_regs);
2132                 kfree(card->mpa_rx.skb_arr);
2133                 return -ENOMEM;
2134         }
2135 
2136         ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2137                                              card->mp_tx_agg_buf_size,
2138                                              card->mp_rx_agg_buf_size);
2139 
2140         /* Allocate 32k MPA Tx/Rx buffers if 64k memory allocation fails */
2141         if (ret && (card->mp_tx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX ||
2142                     card->mp_rx_agg_buf_size == MWIFIEX_MP_AGGR_BUF_SIZE_MAX)) {
2143                 /* Disable rx single port aggregation */
2144                 adapter->host_disable_sdio_rx_aggr = true;
2145 
2146                 ret = mwifiex_alloc_sdio_mpa_buffers
2147                         (adapter, MWIFIEX_MP_AGGR_BUF_SIZE_32K,
2148                          MWIFIEX_MP_AGGR_BUF_SIZE_32K);
2149                 if (ret) {
2150                         /* Disable multi port aggregation */
2151                         card->mpa_tx.enabled = 0;
2152                         card->mpa_rx.enabled = 0;
2153                 }
2154         }
2155 
2156         adapter->auto_tdls = card->can_auto_tdls;
2157         adapter->ext_scan = card->can_ext_scan;
2158         return 0;
2159 }
2160 
2161 /*
2162  * This function resets the MPA Tx and Rx buffers.
2163  */
2164 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2165 {
2166         struct sdio_mmc_card *card = adapter->card;
2167 
2168         MP_TX_AGGR_BUF_RESET(card);
2169         MP_RX_AGGR_BUF_RESET(card);
2170 }
2171 
2172 /*
2173  * This function cleans up the allocated card buffers.
2174  *
2175  * The following are freed by this function -
2176  *      - MP registers
2177  *      - MPA Tx buffer
2178  *      - MPA Rx buffer
2179  */
2180 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2181 {
2182         struct sdio_mmc_card *card = adapter->card;
2183 
2184         cancel_work_sync(&card->work);
2185 
2186         kfree(card->mp_regs);
2187         kfree(card->mpa_rx.skb_arr);
2188         kfree(card->mpa_rx.len_arr);
2189         kfree(card->mpa_tx.buf);
2190         kfree(card->mpa_rx.buf);
2191 }
2192 
2193 /*
2194  * This function updates the MP end port in card.
2195  */
2196 static void
2197 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2198 {
2199         struct sdio_mmc_card *card = adapter->card;
2200         const struct mwifiex_sdio_card_reg *reg = card->reg;
2201         int i;
2202 
2203         card->mp_end_port = port;
2204 
2205         card->mp_data_port_mask = reg->data_port_mask;
2206 
2207         if (reg->start_wr_port) {
2208                 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2209                         card->mp_data_port_mask &=
2210                                         ~(1 << (card->max_ports - i));
2211         }
2212 
2213         card->curr_wr_port = reg->start_wr_port;
2214 
2215         mwifiex_dbg(adapter, CMD,
2216                     "cmd: mp_end_port %d, data port mask 0x%x\n",
2217                     port, card->mp_data_port_mask);
2218 }
2219 
2220 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2221 {
2222         struct sdio_mmc_card *card = adapter->card;
2223         struct sdio_func *func = card->func;
2224         int ret;
2225 
2226         /* Prepare the adapter for the reset. */
2227         mwifiex_shutdown_sw(adapter);
2228         clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &card->work_flags);
2229         clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags);
2230 
2231         /* Run a HW reset of the SDIO interface. */
2232         sdio_claim_host(func);
2233         ret = mmc_hw_reset(func->card->host);
2234         sdio_release_host(func);
2235 
2236         switch (ret) {
2237         case 1:
2238                 dev_dbg(&func->dev, "SDIO HW reset asynchronous\n");
2239                 complete_all(adapter->fw_done);
2240                 break;
2241         case 0:
2242                 ret = mwifiex_reinit_sw(adapter);
2243                 if (ret)
2244                         dev_err(&func->dev, "reinit failed: %d\n", ret);
2245                 break;
2246         default:
2247                 dev_err(&func->dev, "SDIO HW reset failed: %d\n", ret);
2248                 break;
2249         }
2250 }
2251 
2252 /* This function read/write firmware */
2253 static enum
2254 rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2255                                        u8 doneflag)
2256 {
2257         struct sdio_mmc_card *card = adapter->card;
2258         int ret, tries;
2259         u8 ctrl_data = 0;
2260 
2261         sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2262                     card->reg->fw_dump_ctrl, &ret);
2263         if (ret) {
2264                 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
2265                 return RDWR_STATUS_FAILURE;
2266         }
2267         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2268                 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2269                                        &ret);
2270                 if (ret) {
2271                         mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2272                         return RDWR_STATUS_FAILURE;
2273                 }
2274                 if (ctrl_data == FW_DUMP_DONE)
2275                         break;
2276                 if (doneflag && ctrl_data == doneflag)
2277                         return RDWR_STATUS_DONE;
2278                 if (ctrl_data != card->reg->fw_dump_host_ready) {
2279                         mwifiex_dbg(adapter, WARN,
2280                                     "The ctrl reg was changed, re-try again\n");
2281                         sdio_writeb(card->func, card->reg->fw_dump_host_ready,
2282                                     card->reg->fw_dump_ctrl, &ret);
2283                         if (ret) {
2284                                 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2285                                 return RDWR_STATUS_FAILURE;
2286                         }
2287                 }
2288                 usleep_range(100, 200);
2289         }
2290         if (ctrl_data == card->reg->fw_dump_host_ready) {
2291                 mwifiex_dbg(adapter, ERROR,
2292                             "Fail to pull ctrl_data\n");
2293                 return RDWR_STATUS_FAILURE;
2294         }
2295 
2296         return RDWR_STATUS_SUCCESS;
2297 }
2298 
2299 /* This function dump firmware memory to file */
2300 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2301 {
2302         struct sdio_mmc_card *card = adapter->card;
2303         int ret = 0;
2304         unsigned int reg, reg_start, reg_end;
2305         u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2306         enum rdwr_status stat;
2307         u32 memory_size;
2308 
2309         if (!card->can_dump_fw)
2310                 return;
2311 
2312         for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2313                 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2314 
2315                 if (entry->mem_ptr) {
2316                         vfree(entry->mem_ptr);
2317                         entry->mem_ptr = NULL;
2318                 }
2319                 entry->mem_size = 0;
2320         }
2321 
2322         mwifiex_pm_wakeup_card(adapter);
2323         sdio_claim_host(card->func);
2324 
2325         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2326 
2327         stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2328         if (stat == RDWR_STATUS_FAILURE)
2329                 goto done;
2330 
2331         reg = card->reg->fw_dump_start;
2332         /* Read the number of the memories which will dump */
2333         dump_num = sdio_readb(card->func, reg, &ret);
2334         if (ret) {
2335                 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2336                 goto done;
2337         }
2338 
2339         /* Read the length of every memory which will dump */
2340         for (idx = 0; idx < dump_num; idx++) {
2341                 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2342 
2343                 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2344                 if (stat == RDWR_STATUS_FAILURE)
2345                         goto done;
2346 
2347                 memory_size = 0;
2348                 reg = card->reg->fw_dump_start;
2349                 for (i = 0; i < 4; i++) {
2350                         read_reg = sdio_readb(card->func, reg, &ret);
2351                         if (ret) {
2352                                 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2353                                 goto done;
2354                         }
2355                         memory_size |= (read_reg << i*8);
2356                         reg++;
2357                 }
2358 
2359                 if (memory_size == 0) {
2360                         mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2361                         ret = mwifiex_write_reg(adapter,
2362                                                 card->reg->fw_dump_ctrl,
2363                                                 FW_DUMP_READ_DONE);
2364                         if (ret) {
2365                                 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2366                                 return;
2367                         }
2368                         break;
2369                 }
2370 
2371                 mwifiex_dbg(adapter, DUMP,
2372                             "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2373                 entry->mem_ptr = vmalloc(memory_size + 1);
2374                 entry->mem_size = memory_size;
2375                 if (!entry->mem_ptr) {
2376                         mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2377                                     entry->mem_name);
2378                         goto done;
2379                 }
2380                 dbg_ptr = entry->mem_ptr;
2381                 end_ptr = dbg_ptr + memory_size;
2382 
2383                 doneflag = entry->done_flag;
2384                 mwifiex_dbg(adapter, DUMP,
2385                             "Start %s output, please wait...\n",
2386                             entry->mem_name);
2387 
2388                 do {
2389                         stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2390                         if (stat == RDWR_STATUS_FAILURE)
2391                                 goto done;
2392 
2393                         reg_start = card->reg->fw_dump_start;
2394                         reg_end = card->reg->fw_dump_end;
2395                         for (reg = reg_start; reg <= reg_end; reg++) {
2396                                 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2397                                 if (ret) {
2398                                         mwifiex_dbg(adapter, ERROR,
2399                                                     "SDIO read err\n");
2400                                         goto done;
2401                                 }
2402                                 if (dbg_ptr < end_ptr)
2403                                         dbg_ptr++;
2404                                 else
2405                                         mwifiex_dbg(adapter, ERROR,
2406                                                     "Allocated buf not enough\n");
2407                         }
2408 
2409                         if (stat != RDWR_STATUS_DONE)
2410                                 continue;
2411 
2412                         mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2413                                     entry->mem_name, dbg_ptr - entry->mem_ptr);
2414                         break;
2415                 } while (1);
2416         }
2417         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2418 
2419 done:
2420         sdio_release_host(card->func);
2421 }
2422 
2423 static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
2424 {
2425         struct sdio_mmc_card *card = adapter->card;
2426         struct memory_type_mapping *entry = &generic_mem_type_map[0];
2427         unsigned int reg, reg_start, reg_end;
2428         u8 start_flag = 0, done_flag = 0;
2429         u8 *dbg_ptr, *end_ptr;
2430         enum rdwr_status stat;
2431         int ret = -1, tries;
2432 
2433         if (!card->fw_dump_enh)
2434                 return;
2435 
2436         if (entry->mem_ptr) {
2437                 vfree(entry->mem_ptr);
2438                 entry->mem_ptr = NULL;
2439         }
2440         entry->mem_size = 0;
2441 
2442         mwifiex_pm_wakeup_card(adapter);
2443         sdio_claim_host(card->func);
2444 
2445         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2446 
2447         stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2448         if (stat == RDWR_STATUS_FAILURE)
2449                 goto done;
2450 
2451         reg_start = card->reg->fw_dump_start;
2452         reg_end = card->reg->fw_dump_end;
2453         for (reg = reg_start; reg <= reg_end; reg++) {
2454                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2455                         start_flag = sdio_readb(card->func, reg, &ret);
2456                         if (ret) {
2457                                 mwifiex_dbg(adapter, ERROR,
2458                                             "SDIO read err\n");
2459                                 goto done;
2460                         }
2461                         if (start_flag == 0)
2462                                 break;
2463                         if (tries == MAX_POLL_TRIES) {
2464                                 mwifiex_dbg(adapter, ERROR,
2465                                             "FW not ready to dump\n");
2466                                 ret = -1;
2467                                 goto done;
2468                         }
2469                 }
2470                 usleep_range(100, 200);
2471         }
2472 
2473         entry->mem_ptr = vmalloc(0xf0000 + 1);
2474         if (!entry->mem_ptr) {
2475                 ret = -1;
2476                 goto done;
2477         }
2478         dbg_ptr = entry->mem_ptr;
2479         entry->mem_size = 0xf0000;
2480         end_ptr = dbg_ptr + entry->mem_size;
2481 
2482         done_flag = entry->done_flag;
2483         mwifiex_dbg(adapter, DUMP,
2484                     "Start %s output, please wait...\n", entry->mem_name);
2485 
2486         while (true) {
2487                 stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
2488                 if (stat == RDWR_STATUS_FAILURE)
2489                         goto done;
2490                 for (reg = reg_start; reg <= reg_end; reg++) {
2491                         *dbg_ptr = sdio_readb(card->func, reg, &ret);
2492                         if (ret) {
2493                                 mwifiex_dbg(adapter, ERROR,
2494                                             "SDIO read err\n");
2495                                 goto done;
2496                         }
2497                         dbg_ptr++;
2498                         if (dbg_ptr >= end_ptr) {
2499                                 u8 *tmp_ptr;
2500 
2501                                 tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
2502                                 if (!tmp_ptr)
2503                                         goto done;
2504 
2505                                 memcpy(tmp_ptr, entry->mem_ptr,
2506                                        entry->mem_size);
2507                                 vfree(entry->mem_ptr);
2508                                 entry->mem_ptr = tmp_ptr;
2509                                 tmp_ptr = NULL;
2510                                 dbg_ptr = entry->mem_ptr + entry->mem_size;
2511                                 entry->mem_size += 0x4000;
2512                                 end_ptr = entry->mem_ptr + entry->mem_size;
2513                         }
2514                 }
2515                 if (stat == RDWR_STATUS_DONE) {
2516                         entry->mem_size = dbg_ptr - entry->mem_ptr;
2517                         mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
2518                                     entry->mem_name, entry->mem_size);
2519                         ret = 0;
2520                         break;
2521                 }
2522         }
2523         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2524 
2525 done:
2526         if (ret) {
2527                 mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
2528                 if (entry->mem_ptr) {
2529                         vfree(entry->mem_ptr);
2530                         entry->mem_ptr = NULL;
2531                 }
2532                 entry->mem_size = 0;
2533         }
2534         sdio_release_host(card->func);
2535 }
2536 
2537 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2538 {
2539         struct sdio_mmc_card *card = adapter->card;
2540 
2541         adapter->devdump_data = vzalloc(MWIFIEX_FW_DUMP_SIZE);
2542         if (!adapter->devdump_data) {
2543                 mwifiex_dbg(adapter, ERROR,
2544                             "vzalloc devdump data failure!\n");
2545                 return;
2546         }
2547 
2548         mwifiex_drv_info_dump(adapter);
2549         if (card->fw_dump_enh)
2550                 mwifiex_sdio_generic_fw_dump(adapter);
2551         else
2552                 mwifiex_sdio_fw_dump(adapter);
2553         mwifiex_prepare_fw_dump_info(adapter);
2554         mwifiex_upload_device_dump(adapter);
2555 }
2556 
2557 static void mwifiex_sdio_work(struct work_struct *work)
2558 {
2559         struct sdio_mmc_card *card =
2560                 container_of(work, struct sdio_mmc_card, work);
2561 
2562         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2563                                &card->work_flags))
2564                 mwifiex_sdio_device_dump_work(card->adapter);
2565         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2566                                &card->work_flags))
2567                 mwifiex_sdio_card_reset_work(card->adapter);
2568 }
2569 
2570 /* This function resets the card */
2571 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2572 {
2573         struct sdio_mmc_card *card = adapter->card;
2574 
2575         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags))
2576                 schedule_work(&card->work);
2577 }
2578 
2579 /* This function dumps FW information */
2580 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2581 {
2582         struct sdio_mmc_card *card = adapter->card;
2583 
2584         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2585                               &card->work_flags))
2586                 schedule_work(&card->work);
2587 }
2588 
2589 /* Function to dump SDIO function registers and SDIO scratch registers in case
2590  * of FW crash
2591  */
2592 static int
2593 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2594 {
2595         char *p = drv_buf;
2596         struct sdio_mmc_card *cardp = adapter->card;
2597         int ret = 0;
2598         u8 count, func, data, index = 0, size = 0;
2599         u8 reg, reg_start, reg_end;
2600         char buf[256], *ptr;
2601 
2602         if (!p)
2603                 return 0;
2604 
2605         mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2606 
2607         mwifiex_pm_wakeup_card(adapter);
2608 
2609         sdio_claim_host(cardp->func);
2610 
2611         for (count = 0; count < 5; count++) {
2612                 memset(buf, 0, sizeof(buf));
2613                 ptr = buf;
2614 
2615                 switch (count) {
2616                 case 0:
2617                         /* Read the registers of SDIO function0 */
2618                         func = count;
2619                         reg_start = 0;
2620                         reg_end = 9;
2621                         break;
2622                 case 1:
2623                         /* Read the registers of SDIO function1 */
2624                         func = count;
2625                         reg_start = cardp->reg->func1_dump_reg_start;
2626                         reg_end = cardp->reg->func1_dump_reg_end;
2627                         break;
2628                 case 2:
2629                         index = 0;
2630                         func = 1;
2631                         reg_start = cardp->reg->func1_spec_reg_table[index++];
2632                         size = cardp->reg->func1_spec_reg_num;
2633                         reg_end = cardp->reg->func1_spec_reg_table[size-1];
2634                         break;
2635                 default:
2636                         /* Read the scratch registers of SDIO function1 */
2637                         if (count == 4)
2638                                 mdelay(100);
2639                         func = 1;
2640                         reg_start = cardp->reg->func1_scratch_reg;
2641                         reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2642                 }
2643 
2644                 if (count != 2)
2645                         ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2646                                        func, reg_start, reg_end);
2647                 else
2648                         ptr += sprintf(ptr, "SDIO Func%d: ", func);
2649 
2650                 for (reg = reg_start; reg <= reg_end;) {
2651                         if (func == 0)
2652                                 data = sdio_f0_readb(cardp->func, reg, &ret);
2653                         else
2654                                 data = sdio_readb(cardp->func, reg, &ret);
2655 
2656                         if (count == 2)
2657                                 ptr += sprintf(ptr, "(%#x) ", reg);
2658                         if (!ret) {
2659                                 ptr += sprintf(ptr, "%02x ", data);
2660                         } else {
2661                                 ptr += sprintf(ptr, "ERR");
2662                                 break;
2663                         }
2664 
2665                         if (count == 2 && reg < reg_end)
2666                                 reg = cardp->reg->func1_spec_reg_table[index++];
2667                         else
2668                                 reg++;
2669                 }
2670 
2671                 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2672                 p += sprintf(p, "%s\n", buf);
2673         }
2674 
2675         sdio_release_host(cardp->func);
2676 
2677         mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2678 
2679         return p - drv_buf;
2680 }
2681 
2682 /* sdio device/function initialization, code is extracted
2683  * from init_if handler and register_dev handler.
2684  */
2685 static void mwifiex_sdio_up_dev(struct mwifiex_adapter *adapter)
2686 {
2687         struct sdio_mmc_card *card = adapter->card;
2688         u8 sdio_ireg;
2689 
2690         sdio_claim_host(card->func);
2691         sdio_enable_func(card->func);
2692         sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
2693         sdio_release_host(card->func);
2694 
2695         /* tx_buf_size might be changed to 3584 by firmware during
2696          * data transfer, we will reset to default size.
2697          */
2698         adapter->tx_buf_size = card->tx_buf_size;
2699 
2700         /* Read the host_int_status_reg for ACK the first interrupt got
2701          * from the bootloader. If we don't do this we get a interrupt
2702          * as soon as we register the irq.
2703          */
2704         mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2705 
2706         mwifiex_init_sdio_ioport(adapter);
2707 }
2708 
2709 static struct mwifiex_if_ops sdio_ops = {
2710         .init_if = mwifiex_init_sdio,
2711         .cleanup_if = mwifiex_cleanup_sdio,
2712         .check_fw_status = mwifiex_check_fw_status,
2713         .check_winner_status = mwifiex_check_winner_status,
2714         .prog_fw = mwifiex_prog_fw_w_helper,
2715         .register_dev = mwifiex_register_dev,
2716         .unregister_dev = mwifiex_unregister_dev,
2717         .enable_int = mwifiex_sdio_enable_host_int,
2718         .disable_int = mwifiex_sdio_disable_host_int,
2719         .process_int_status = mwifiex_process_int_status,
2720         .host_to_card = mwifiex_sdio_host_to_card,
2721         .wakeup = mwifiex_pm_wakeup_card,
2722         .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2723 
2724         /* SDIO specific */
2725         .update_mp_end_port = mwifiex_update_mp_end_port,
2726         .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2727         .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2728         .event_complete = mwifiex_sdio_event_complete,
2729         .dnld_fw = mwifiex_sdio_dnld_fw,
2730         .card_reset = mwifiex_sdio_card_reset,
2731         .reg_dump = mwifiex_sdio_reg_dump,
2732         .device_dump = mwifiex_sdio_device_dump,
2733         .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2734         .up_dev = mwifiex_sdio_up_dev,
2735 };
2736 
2737 module_driver(mwifiex_sdio, sdio_register_driver, sdio_unregister_driver);
2738 
2739 MODULE_AUTHOR("Marvell International Ltd.");
2740 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2741 MODULE_VERSION(SDIO_VERSION);
2742 MODULE_LICENSE("GPL v2");
2743 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2744 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2745 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2746 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2747 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
2748 MODULE_FIRMWARE(SD8977_DEFAULT_FW_NAME);
2749 MODULE_FIRMWARE(SD8987_DEFAULT_FW_NAME);
2750 MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);

/* [<][>][^][v][top][bottom][index][help] */