linux/drivers/net/wireless/mwifiex/sdio.c
<<
>>
Prefs
   1/*
   2 * Marvell Wireless LAN device driver: SDIO specific handling
   3 *
   4 * Copyright (C) 2011, Marvell International Ltd.
   5 *
   6 * This software file (the "File") is distributed by Marvell International
   7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
   8 * (the "License").  You may use, redistribute and/or modify this File in
   9 * accordance with the terms and conditions of the License, a copy of which
  10 * is available by writing to the Free Software Foundation, Inc.,
  11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13 *
  14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
  17 * this warranty disclaimer.
  18 */
  19
  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/* The mwifiex_sdio_remove() callback function is called when
  35 * user removes this module from kernel space or ejects
  36 * the card from the slot. The driver handles these 2 cases
  37 * differently.
  38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
  39 * HS_CANCEL etc.) are sent to the firmware.
  40 * If the card is removed, there is no need to send these command.
  41 *
  42 * The variable 'user_rmmod' is used to distinguish these two
  43 * scenarios. This flag is initialized as FALSE in case the card
  44 * is removed, and will be set to TRUE for module removal when
  45 * module_exit function is called.
  46 */
  47static u8 user_rmmod;
  48
  49static struct mwifiex_if_ops sdio_ops;
  50
  51static struct semaphore add_remove_card_sem;
  52
  53/*
  54 * SDIO probe.
  55 *
  56 * This function probes an mwifiex device and registers it. It allocates
  57 * the card structure, enables SDIO function number and initiates the
  58 * device registration and initialization procedure by adding a logical
  59 * interface.
  60 */
  61static int
  62mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
  63{
  64        int ret;
  65        struct sdio_mmc_card *card = NULL;
  66
  67        pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
  68                 func->vendor, func->device, func->class, func->num);
  69
  70        card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
  71        if (!card)
  72                return -ENOMEM;
  73
  74        card->func = func;
  75
  76        func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  77
  78        if (id->driver_data) {
  79                struct mwifiex_sdio_device *data = (void *)id->driver_data;
  80
  81                card->firmware = data->firmware;
  82                card->reg = data->reg;
  83                card->max_ports = data->max_ports;
  84                card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
  85                card->supports_sdio_new_mode = data->supports_sdio_new_mode;
  86                card->has_control_mask = data->has_control_mask;
  87        }
  88
  89        sdio_claim_host(func);
  90        ret = sdio_enable_func(func);
  91        sdio_release_host(func);
  92
  93        if (ret) {
  94                pr_err("%s: failed to enable function\n", __func__);
  95                kfree(card);
  96                return -EIO;
  97        }
  98
  99        if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
 100                             MWIFIEX_SDIO)) {
 101                pr_err("%s: add card failed\n", __func__);
 102                kfree(card);
 103                sdio_claim_host(func);
 104                ret = sdio_disable_func(func);
 105                sdio_release_host(func);
 106                ret = -1;
 107        }
 108
 109        return ret;
 110}
 111
 112/*
 113 * SDIO resume.
 114 *
 115 * Kernel needs to suspend all functions separately. Therefore all
 116 * registered functions must have drivers with suspend and resume
 117 * methods. Failing that the kernel simply removes the whole card.
 118 *
 119 * If already not resumed, this function turns on the traffic and
 120 * sends a host sleep cancel request to the firmware.
 121 */
 122static int mwifiex_sdio_resume(struct device *dev)
 123{
 124        struct sdio_func *func = dev_to_sdio_func(dev);
 125        struct sdio_mmc_card *card;
 126        struct mwifiex_adapter *adapter;
 127        mmc_pm_flag_t pm_flag = 0;
 128
 129        if (func) {
 130                pm_flag = sdio_get_host_pm_caps(func);
 131                card = sdio_get_drvdata(func);
 132                if (!card || !card->adapter) {
 133                        pr_err("resume: invalid card or adapter\n");
 134                        return 0;
 135                }
 136        } else {
 137                pr_err("resume: sdio_func is not specified\n");
 138                return 0;
 139        }
 140
 141        adapter = card->adapter;
 142
 143        if (!adapter->is_suspended) {
 144                dev_warn(adapter->dev, "device already resumed\n");
 145                return 0;
 146        }
 147
 148        adapter->is_suspended = false;
 149
 150        /* Disable Host Sleep */
 151        mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
 152                          MWIFIEX_ASYNC_CMD);
 153
 154        return 0;
 155}
 156
 157/*
 158 * SDIO remove.
 159 *
 160 * This function removes the interface and frees up the card structure.
 161 */
 162static void
 163mwifiex_sdio_remove(struct sdio_func *func)
 164{
 165        struct sdio_mmc_card *card;
 166        struct mwifiex_adapter *adapter;
 167        struct mwifiex_private *priv;
 168        int i;
 169
 170        pr_debug("info: SDIO func num=%d\n", func->num);
 171
 172        card = sdio_get_drvdata(func);
 173        if (!card)
 174                return;
 175
 176        adapter = card->adapter;
 177        if (!adapter || !adapter->priv_num)
 178                return;
 179
 180        /* In case driver is removed when asynchronous FW load is in progress */
 181        wait_for_completion(&adapter->fw_load);
 182
 183        if (user_rmmod) {
 184                if (adapter->is_suspended)
 185                        mwifiex_sdio_resume(adapter->dev);
 186
 187                for (i = 0; i < adapter->priv_num; i++)
 188                        if ((GET_BSS_ROLE(adapter->priv[i]) ==
 189                                                MWIFIEX_BSS_ROLE_STA) &&
 190                            adapter->priv[i]->media_connected)
 191                                mwifiex_deauthenticate(adapter->priv[i], NULL);
 192
 193                priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
 194                mwifiex_disable_auto_ds(priv);
 195                mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
 196        }
 197
 198        mwifiex_remove_card(card->adapter, &add_remove_card_sem);
 199}
 200
 201/*
 202 * SDIO suspend.
 203 *
 204 * Kernel needs to suspend all functions separately. Therefore all
 205 * registered functions must have drivers with suspend and resume
 206 * methods. Failing that the kernel simply removes the whole card.
 207 *
 208 * If already not suspended, this function allocates and sends a host
 209 * sleep activate request to the firmware and turns off the traffic.
 210 */
 211static int mwifiex_sdio_suspend(struct device *dev)
 212{
 213        struct sdio_func *func = dev_to_sdio_func(dev);
 214        struct sdio_mmc_card *card;
 215        struct mwifiex_adapter *adapter;
 216        mmc_pm_flag_t pm_flag = 0;
 217        int ret = 0;
 218
 219        if (func) {
 220                pm_flag = sdio_get_host_pm_caps(func);
 221                pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
 222                         sdio_func_id(func), pm_flag);
 223                if (!(pm_flag & MMC_PM_KEEP_POWER)) {
 224                        pr_err("%s: cannot remain alive while host is"
 225                                " suspended\n", sdio_func_id(func));
 226                        return -ENOSYS;
 227                }
 228
 229                card = sdio_get_drvdata(func);
 230                if (!card || !card->adapter) {
 231                        pr_err("suspend: invalid card or adapter\n");
 232                        return 0;
 233                }
 234        } else {
 235                pr_err("suspend: sdio_func is not specified\n");
 236                return 0;
 237        }
 238
 239        adapter = card->adapter;
 240
 241        /* Enable the Host Sleep */
 242        if (!mwifiex_enable_hs(adapter)) {
 243                dev_err(adapter->dev, "cmd: failed to suspend\n");
 244                return -EFAULT;
 245        }
 246
 247        dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
 248        ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
 249
 250        /* Indicate device suspended */
 251        adapter->is_suspended = true;
 252
 253        return ret;
 254}
 255
 256/* Device ID for SD8786 */
 257#define SDIO_DEVICE_ID_MARVELL_8786   (0x9116)
 258/* Device ID for SD8787 */
 259#define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
 260/* Device ID for SD8797 */
 261#define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
 262/* Device ID for SD8897 */
 263#define SDIO_DEVICE_ID_MARVELL_8897   (0x912d)
 264
 265/* WLAN IDs */
 266static const struct sdio_device_id mwifiex_ids[] = {
 267        {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
 268                .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
 269        {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
 270                .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
 271        {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
 272                .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
 273        {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
 274                .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
 275        {},
 276};
 277
 278MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
 279
 280static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
 281        .suspend = mwifiex_sdio_suspend,
 282        .resume = mwifiex_sdio_resume,
 283};
 284
 285static struct sdio_driver mwifiex_sdio = {
 286        .name = "mwifiex_sdio",
 287        .id_table = mwifiex_ids,
 288        .probe = mwifiex_sdio_probe,
 289        .remove = mwifiex_sdio_remove,
 290        .drv = {
 291                .owner = THIS_MODULE,
 292                .pm = &mwifiex_sdio_pm_ops,
 293        }
 294};
 295
 296/* Write data into SDIO card register. Caller claims SDIO device. */
 297static int
 298mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
 299{
 300        int ret = -1;
 301        sdio_writeb(func, data, reg, &ret);
 302        return ret;
 303}
 304
 305/*
 306 * This function writes data into SDIO card register.
 307 */
 308static int
 309mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
 310{
 311        struct sdio_mmc_card *card = adapter->card;
 312        int ret;
 313
 314        sdio_claim_host(card->func);
 315        ret = mwifiex_write_reg_locked(card->func, reg, data);
 316        sdio_release_host(card->func);
 317
 318        return ret;
 319}
 320
 321/*
 322 * This function reads data from SDIO card register.
 323 */
 324static int
 325mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
 326{
 327        struct sdio_mmc_card *card = adapter->card;
 328        int ret = -1;
 329        u8 val;
 330
 331        sdio_claim_host(card->func);
 332        val = sdio_readb(card->func, reg, &ret);
 333        sdio_release_host(card->func);
 334
 335        *data = val;
 336
 337        return ret;
 338}
 339
 340/*
 341 * This function writes multiple data into SDIO card memory.
 342 *
 343 * This does not work in suspended mode.
 344 */
 345static int
 346mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
 347                        u8 *buffer, u32 pkt_len, u32 port)
 348{
 349        struct sdio_mmc_card *card = adapter->card;
 350        int ret;
 351        u8 blk_mode =
 352                (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
 353        u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
 354        u32 blk_cnt =
 355                (blk_mode ==
 356                 BLOCK_MODE) ? (pkt_len /
 357                                MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
 358        u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
 359
 360        if (adapter->is_suspended) {
 361                dev_err(adapter->dev,
 362                        "%s: not allowed while suspended\n", __func__);
 363                return -1;
 364        }
 365
 366        sdio_claim_host(card->func);
 367
 368        ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
 369
 370        sdio_release_host(card->func);
 371
 372        return ret;
 373}
 374
 375/*
 376 * This function reads multiple data from SDIO card memory.
 377 */
 378static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
 379                                  u32 len, u32 port, u8 claim)
 380{
 381        struct sdio_mmc_card *card = adapter->card;
 382        int ret;
 383        u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
 384                       : BLOCK_MODE;
 385        u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
 386        u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
 387                        : len;
 388        u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
 389
 390        if (claim)
 391                sdio_claim_host(card->func);
 392
 393        ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
 394
 395        if (claim)
 396                sdio_release_host(card->func);
 397
 398        return ret;
 399}
 400
 401/*
 402 * This function wakes up the card.
 403 *
 404 * A host power up command is written to the card configuration
 405 * register to wake up the card.
 406 */
 407static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
 408{
 409        dev_dbg(adapter->dev, "event: wakeup device...\n");
 410
 411        return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
 412}
 413
 414/*
 415 * This function is called after the card has woken up.
 416 *
 417 * The card configuration register is reset.
 418 */
 419static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
 420{
 421        dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
 422
 423        return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
 424}
 425
 426/*
 427 * This function is used to initialize IO ports for the
 428 * chipsets supporting SDIO new mode eg SD8897.
 429 */
 430static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
 431{
 432        u8 reg;
 433
 434        adapter->ioport = MEM_PORT;
 435
 436        /* enable sdio new mode */
 437        if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
 438                return -1;
 439        if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
 440                              reg | CMD53_NEW_MODE))
 441                return -1;
 442
 443        /* Configure cmd port and enable reading rx length from the register */
 444        if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
 445                return -1;
 446        if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
 447                return -1;
 448
 449        /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
 450         * completed
 451         */
 452        if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
 453                return -1;
 454        if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
 455                return -1;
 456
 457        return 0;
 458}
 459
 460/* This function initializes the IO ports.
 461 *
 462 * The following operations are performed -
 463 *      - Read the IO ports (0, 1 and 2)
 464 *      - Set host interrupt Reset-To-Read to clear
 465 *      - Set auto re-enable interrupt
 466 */
 467static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
 468{
 469        u8 reg;
 470        struct sdio_mmc_card *card = adapter->card;
 471
 472        adapter->ioport = 0;
 473
 474        if (card->supports_sdio_new_mode) {
 475                if (mwifiex_init_sdio_new_mode(adapter))
 476                        return -1;
 477                goto cont;
 478        }
 479
 480        /* Read the IO port */
 481        if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
 482                adapter->ioport |= (reg & 0xff);
 483        else
 484                return -1;
 485
 486        if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
 487                adapter->ioport |= ((reg & 0xff) << 8);
 488        else
 489                return -1;
 490
 491        if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
 492                adapter->ioport |= ((reg & 0xff) << 16);
 493        else
 494                return -1;
 495cont:
 496        pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
 497
 498        /* Set Host interrupt reset to read to clear */
 499        if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
 500                mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
 501                                  reg | card->reg->sdio_int_mask);
 502        else
 503                return -1;
 504
 505        /* Dnld/Upld ready set to auto reset */
 506        if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
 507                mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
 508                                  reg | AUTO_RE_ENABLE_INT);
 509        else
 510                return -1;
 511
 512        return 0;
 513}
 514
 515/*
 516 * This function sends data to the card.
 517 */
 518static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
 519                                      u8 *payload, u32 pkt_len, u32 port)
 520{
 521        u32 i = 0;
 522        int ret;
 523
 524        do {
 525                ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
 526                if (ret) {
 527                        i++;
 528                        dev_err(adapter->dev, "host_to_card, write iomem"
 529                                        " (%d) failed: %d\n", i, ret);
 530                        if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
 531                                dev_err(adapter->dev, "write CFG reg failed\n");
 532
 533                        ret = -1;
 534                        if (i > MAX_WRITE_IOMEM_RETRY)
 535                                return ret;
 536                }
 537        } while (ret == -1);
 538
 539        return ret;
 540}
 541
 542/*
 543 * This function gets the read port.
 544 *
 545 * If control port bit is set in MP read bitmap, the control port
 546 * is returned, otherwise the current read port is returned and
 547 * the value is increased (provided it does not reach the maximum
 548 * limit, in which case it is reset to 1)
 549 */
 550static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
 551{
 552        struct sdio_mmc_card *card = adapter->card;
 553        const struct mwifiex_sdio_card_reg *reg = card->reg;
 554        u32 rd_bitmap = card->mp_rd_bitmap;
 555
 556        dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
 557
 558        if (card->supports_sdio_new_mode) {
 559                if (!(rd_bitmap & reg->data_port_mask))
 560                        return -1;
 561        } else {
 562                if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
 563                        return -1;
 564        }
 565
 566        if ((card->has_control_mask) &&
 567            (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
 568                card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
 569                *port = CTRL_PORT;
 570                dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
 571                        *port, card->mp_rd_bitmap);
 572                return 0;
 573        }
 574
 575        if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
 576                return -1;
 577
 578        /* We are now handling the SDIO data ports */
 579        card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
 580        *port = card->curr_rd_port;
 581
 582        if (++card->curr_rd_port == card->max_ports)
 583                card->curr_rd_port = reg->start_rd_port;
 584
 585        dev_dbg(adapter->dev,
 586                "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
 587                *port, rd_bitmap, card->mp_rd_bitmap);
 588
 589        return 0;
 590}
 591
 592/*
 593 * This function gets the write port for data.
 594 *
 595 * The current write port is returned if available and the value is
 596 * increased (provided it does not reach the maximum limit, in which
 597 * case it is reset to 1)
 598 */
 599static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
 600{
 601        struct sdio_mmc_card *card = adapter->card;
 602        const struct mwifiex_sdio_card_reg *reg = card->reg;
 603        u32 wr_bitmap = card->mp_wr_bitmap;
 604
 605        dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
 606
 607        if (card->supports_sdio_new_mode &&
 608            !(wr_bitmap & reg->data_port_mask)) {
 609                adapter->data_sent = true;
 610                return -EBUSY;
 611        } else if (!card->supports_sdio_new_mode &&
 612                   !(wr_bitmap & card->mp_data_port_mask)) {
 613                return -1;
 614        }
 615
 616        if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
 617                card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
 618                *port = card->curr_wr_port;
 619                if (((card->supports_sdio_new_mode) &&
 620                     (++card->curr_wr_port == card->max_ports)) ||
 621                    ((!card->supports_sdio_new_mode) &&
 622                     (++card->curr_wr_port == card->mp_end_port)))
 623                        card->curr_wr_port = reg->start_wr_port;
 624        } else {
 625                adapter->data_sent = true;
 626                return -EBUSY;
 627        }
 628
 629        if ((card->has_control_mask) && (*port == CTRL_PORT)) {
 630                dev_err(adapter->dev,
 631                        "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
 632                        *port, card->curr_wr_port, wr_bitmap,
 633                        card->mp_wr_bitmap);
 634                return -1;
 635        }
 636
 637        dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
 638                *port, wr_bitmap, card->mp_wr_bitmap);
 639
 640        return 0;
 641}
 642
 643/*
 644 * This function polls the card status.
 645 */
 646static int
 647mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
 648{
 649        struct sdio_mmc_card *card = adapter->card;
 650        u32 tries;
 651        u8 cs;
 652
 653        for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
 654                if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
 655                        break;
 656                else if ((cs & bits) == bits)
 657                        return 0;
 658
 659                usleep_range(10, 20);
 660        }
 661
 662        dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
 663
 664        return -1;
 665}
 666
 667/*
 668 * This function reads the firmware status.
 669 */
 670static int
 671mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
 672{
 673        struct sdio_mmc_card *card = adapter->card;
 674        const struct mwifiex_sdio_card_reg *reg = card->reg;
 675        u8 fws0, fws1;
 676
 677        if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
 678                return -1;
 679
 680        if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
 681                return -1;
 682
 683        *dat = (u16) ((fws1 << 8) | fws0);
 684
 685        return 0;
 686}
 687
 688/*
 689 * This function disables the host interrupt.
 690 *
 691 * The host interrupt mask is read, the disable bit is reset and
 692 * written back to the card host interrupt mask register.
 693 */
 694static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
 695{
 696        struct sdio_mmc_card *card = adapter->card;
 697        struct sdio_func *func = card->func;
 698
 699        sdio_claim_host(func);
 700        mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
 701        sdio_release_irq(func);
 702        sdio_release_host(func);
 703}
 704
 705/*
 706 * This function reads the interrupt status from card.
 707 */
 708static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
 709{
 710        struct sdio_mmc_card *card = adapter->card;
 711        u8 sdio_ireg;
 712        unsigned long flags;
 713
 714        if (mwifiex_read_data_sync(adapter, card->mp_regs,
 715                                   card->reg->max_mp_regs,
 716                                   REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
 717                dev_err(adapter->dev, "read mp_regs failed\n");
 718                return;
 719        }
 720
 721        sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
 722        if (sdio_ireg) {
 723                /*
 724                 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
 725                 * For SDIO new mode CMD port interrupts
 726                 *      DN_LD_CMD_PORT_HOST_INT_STATUS and/or
 727                 *      UP_LD_CMD_PORT_HOST_INT_STATUS
 728                 * Clear the interrupt status register
 729                 */
 730                dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
 731                spin_lock_irqsave(&adapter->int_lock, flags);
 732                adapter->int_status |= sdio_ireg;
 733                spin_unlock_irqrestore(&adapter->int_lock, flags);
 734        }
 735}
 736
 737/*
 738 * SDIO interrupt handler.
 739 *
 740 * This function reads the interrupt status from firmware and handles
 741 * the interrupt in current thread (ksdioirqd) right away.
 742 */
 743static void
 744mwifiex_sdio_interrupt(struct sdio_func *func)
 745{
 746        struct mwifiex_adapter *adapter;
 747        struct sdio_mmc_card *card;
 748
 749        card = sdio_get_drvdata(func);
 750        if (!card || !card->adapter) {
 751                pr_debug("int: func=%p card=%p adapter=%p\n",
 752                         func, card, card ? card->adapter : NULL);
 753                return;
 754        }
 755        adapter = card->adapter;
 756
 757        if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
 758                adapter->ps_state = PS_STATE_AWAKE;
 759
 760        mwifiex_interrupt_status(adapter);
 761        mwifiex_main_process(adapter);
 762}
 763
 764/*
 765 * This function enables the host interrupt.
 766 *
 767 * The host interrupt enable mask is written to the card
 768 * host interrupt mask register.
 769 */
 770static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
 771{
 772        struct sdio_mmc_card *card = adapter->card;
 773        struct sdio_func *func = card->func;
 774        int ret;
 775
 776        sdio_claim_host(func);
 777
 778        /* Request the SDIO IRQ */
 779        ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
 780        if (ret) {
 781                dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
 782                goto out;
 783        }
 784
 785        /* Simply write the mask to the register */
 786        ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
 787                                       card->reg->host_int_enable);
 788        if (ret) {
 789                dev_err(adapter->dev, "enable host interrupt failed\n");
 790                sdio_release_irq(func);
 791        }
 792
 793out:
 794        sdio_release_host(func);
 795        return ret;
 796}
 797
 798/*
 799 * This function sends a data buffer to the card.
 800 */
 801static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
 802                                     u32 *type, u8 *buffer,
 803                                     u32 npayload, u32 ioport)
 804{
 805        int ret;
 806        u32 nb;
 807
 808        if (!buffer) {
 809                dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
 810                return -1;
 811        }
 812
 813        ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
 814
 815        if (ret) {
 816                dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
 817                        ret);
 818                return -1;
 819        }
 820
 821        nb = le16_to_cpu(*(__le16 *) (buffer));
 822        if (nb > npayload) {
 823                dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
 824                        __func__, nb, npayload);
 825                return -1;
 826        }
 827
 828        *type = le16_to_cpu(*(__le16 *) (buffer + 2));
 829
 830        return ret;
 831}
 832
 833/*
 834 * This function downloads the firmware to the card.
 835 *
 836 * Firmware is downloaded to the card in blocks. Every block download
 837 * is tested for CRC errors, and retried a number of times before
 838 * returning failure.
 839 */
 840static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
 841                                    struct mwifiex_fw_image *fw)
 842{
 843        struct sdio_mmc_card *card = adapter->card;
 844        const struct mwifiex_sdio_card_reg *reg = card->reg;
 845        int ret;
 846        u8 *firmware = fw->fw_buf;
 847        u32 firmware_len = fw->fw_len;
 848        u32 offset = 0;
 849        u8 base0, base1;
 850        u8 *fwbuf;
 851        u16 len = 0;
 852        u32 txlen, tx_blocks = 0, tries;
 853        u32 i = 0;
 854
 855        if (!firmware_len) {
 856                dev_err(adapter->dev,
 857                        "firmware image not found! Terminating download\n");
 858                return -1;
 859        }
 860
 861        dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
 862                firmware_len);
 863
 864        /* Assume that the allocated buffer is 8-byte aligned */
 865        fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
 866        if (!fwbuf)
 867                return -ENOMEM;
 868
 869        /* Perform firmware data transfer */
 870        do {
 871                /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
 872                   bits */
 873                ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
 874                                                    DN_LD_CARD_RDY);
 875                if (ret) {
 876                        dev_err(adapter->dev, "FW download with helper:"
 877                                " poll status timeout @ %d\n", offset);
 878                        goto done;
 879                }
 880
 881                /* More data? */
 882                if (offset >= firmware_len)
 883                        break;
 884
 885                for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
 886                        ret = mwifiex_read_reg(adapter, reg->base_0_reg,
 887                                               &base0);
 888                        if (ret) {
 889                                dev_err(adapter->dev,
 890                                        "dev BASE0 register read failed: "
 891                                        "base0=%#04X(%d). Terminating dnld\n",
 892                                        base0, base0);
 893                                goto done;
 894                        }
 895                        ret = mwifiex_read_reg(adapter, reg->base_1_reg,
 896                                               &base1);
 897                        if (ret) {
 898                                dev_err(adapter->dev,
 899                                        "dev BASE1 register read failed: "
 900                                        "base1=%#04X(%d). Terminating dnld\n",
 901                                        base1, base1);
 902                                goto done;
 903                        }
 904                        len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
 905
 906                        if (len)
 907                                break;
 908
 909                        usleep_range(10, 20);
 910                }
 911
 912                if (!len) {
 913                        break;
 914                } else if (len > MWIFIEX_UPLD_SIZE) {
 915                        dev_err(adapter->dev,
 916                                "FW dnld failed @ %d, invalid length %d\n",
 917                                offset, len);
 918                        ret = -1;
 919                        goto done;
 920                }
 921
 922                txlen = len;
 923
 924                if (len & BIT(0)) {
 925                        i++;
 926                        if (i > MAX_WRITE_IOMEM_RETRY) {
 927                                dev_err(adapter->dev,
 928                                        "FW dnld failed @ %d, over max retry\n",
 929                                        offset);
 930                                ret = -1;
 931                                goto done;
 932                        }
 933                        dev_err(adapter->dev, "CRC indicated by the helper:"
 934                                " len = 0x%04X, txlen = %d\n", len, txlen);
 935                        len &= ~BIT(0);
 936                        /* Setting this to 0 to resend from same offset */
 937                        txlen = 0;
 938                } else {
 939                        i = 0;
 940
 941                        /* Set blocksize to transfer - checking for last
 942                           block */
 943                        if (firmware_len - offset < txlen)
 944                                txlen = firmware_len - offset;
 945
 946                        tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
 947                                    / MWIFIEX_SDIO_BLOCK_SIZE;
 948
 949                        /* Copy payload to buffer */
 950                        memmove(fwbuf, &firmware[offset], txlen);
 951                }
 952
 953                ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
 954                                              MWIFIEX_SDIO_BLOCK_SIZE,
 955                                              adapter->ioport);
 956                if (ret) {
 957                        dev_err(adapter->dev,
 958                                "FW download, write iomem (%d) failed @ %d\n",
 959                                i, offset);
 960                        if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
 961                                dev_err(adapter->dev, "write CFG reg failed\n");
 962
 963                        ret = -1;
 964                        goto done;
 965                }
 966
 967                offset += txlen;
 968        } while (true);
 969
 970        dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
 971                offset);
 972
 973        ret = 0;
 974done:
 975        kfree(fwbuf);
 976        return ret;
 977}
 978
 979/*
 980 * This function checks the firmware status in card.
 981 *
 982 * The winner interface is also determined by this function.
 983 */
 984static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
 985                                   u32 poll_num)
 986{
 987        struct sdio_mmc_card *card = adapter->card;
 988        int ret = 0;
 989        u16 firmware_stat;
 990        u32 tries;
 991        u8 winner_status;
 992
 993        /* Wait for firmware initialization event */
 994        for (tries = 0; tries < poll_num; tries++) {
 995                ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
 996                if (ret)
 997                        continue;
 998                if (firmware_stat == FIRMWARE_READY_SDIO) {
 999                        ret = 0;
1000                        break;
1001                } else {
1002                        msleep(100);
1003                        ret = -1;
1004                }
1005        }
1006
1007        if (ret) {
1008                if (mwifiex_read_reg
1009                    (adapter, card->reg->status_reg_0, &winner_status))
1010                        winner_status = 0;
1011
1012                if (winner_status)
1013                        adapter->winner = 0;
1014                else
1015                        adapter->winner = 1;
1016        }
1017        return ret;
1018}
1019
1020/*
1021 * This function decodes a received packet.
1022 *
1023 * Based on the type, the packet is treated as either a data, or
1024 * a command response, or an event, and the correct handler
1025 * function is invoked.
1026 */
1027static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1028                                    struct sk_buff *skb, u32 upld_typ)
1029{
1030        u8 *cmd_buf;
1031        __le16 *curr_ptr = (__le16 *)skb->data;
1032        u16 pkt_len = le16_to_cpu(*curr_ptr);
1033
1034        skb_trim(skb, pkt_len);
1035        skb_pull(skb, INTF_HEADER_LEN);
1036
1037        switch (upld_typ) {
1038        case MWIFIEX_TYPE_DATA:
1039                dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1040                mwifiex_handle_rx_packet(adapter, skb);
1041                break;
1042
1043        case MWIFIEX_TYPE_CMD:
1044                dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1045                /* take care of curr_cmd = NULL case */
1046                if (!adapter->curr_cmd) {
1047                        cmd_buf = adapter->upld_buf;
1048
1049                        if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1050                                mwifiex_process_sleep_confirm_resp(adapter,
1051                                                                   skb->data,
1052                                                                   skb->len);
1053
1054                        memcpy(cmd_buf, skb->data,
1055                               min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1056                                     skb->len));
1057
1058                        dev_kfree_skb_any(skb);
1059                } else {
1060                        adapter->cmd_resp_received = true;
1061                        adapter->curr_cmd->resp_skb = skb;
1062                }
1063                break;
1064
1065        case MWIFIEX_TYPE_EVENT:
1066                dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
1067                adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1068
1069                if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
1070                        memcpy(adapter->event_body,
1071                               skb->data + MWIFIEX_EVENT_HEADER_LEN,
1072                               skb->len);
1073
1074                /* event cause has been saved to adapter->event_cause */
1075                adapter->event_received = true;
1076                adapter->event_skb = skb;
1077
1078                break;
1079
1080        default:
1081                dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1082                dev_kfree_skb_any(skb);
1083                break;
1084        }
1085
1086        return 0;
1087}
1088
1089/*
1090 * This function transfers received packets from card to driver, performing
1091 * aggregation if required.
1092 *
1093 * For data received on control port, or if aggregation is disabled, the
1094 * received buffers are uploaded as separate packets. However, if aggregation
1095 * is enabled and required, the buffers are copied onto an aggregation buffer,
1096 * provided there is space left, processed and finally uploaded.
1097 */
1098static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1099                                             struct sk_buff *skb, u8 port)
1100{
1101        struct sdio_mmc_card *card = adapter->card;
1102        s32 f_do_rx_aggr = 0;
1103        s32 f_do_rx_cur = 0;
1104        s32 f_aggr_cur = 0;
1105        struct sk_buff *skb_deaggr;
1106        u32 pind;
1107        u32 pkt_len, pkt_type, mport;
1108        u8 *curr_ptr;
1109        u32 rx_len = skb->len;
1110
1111        if ((card->has_control_mask) && (port == CTRL_PORT)) {
1112                /* Read the command Resp without aggr */
1113                dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1114                        "response\n", __func__);
1115
1116                f_do_rx_cur = 1;
1117                goto rx_curr_single;
1118        }
1119
1120        if (!card->mpa_rx.enabled) {
1121                dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1122                        __func__);
1123
1124                f_do_rx_cur = 1;
1125                goto rx_curr_single;
1126        }
1127
1128        if ((!card->has_control_mask && (card->mp_rd_bitmap &
1129                                         card->reg->data_port_mask)) ||
1130            (card->has_control_mask && (card->mp_rd_bitmap &
1131                                        (~((u32) CTRL_PORT_MASK))))) {
1132                /* Some more data RX pending */
1133                dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1134
1135                if (MP_RX_AGGR_IN_PROGRESS(card)) {
1136                        if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1137                                f_aggr_cur = 1;
1138                        } else {
1139                                /* No room in Aggr buf, do rx aggr now */
1140                                f_do_rx_aggr = 1;
1141                                f_do_rx_cur = 1;
1142                        }
1143                } else {
1144                        /* Rx aggr not in progress */
1145                        f_aggr_cur = 1;
1146                }
1147
1148        } else {
1149                /* No more data RX pending */
1150                dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1151
1152                if (MP_RX_AGGR_IN_PROGRESS(card)) {
1153                        f_do_rx_aggr = 1;
1154                        if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1155                                f_aggr_cur = 1;
1156                        else
1157                                /* No room in Aggr buf, do rx aggr now */
1158                                f_do_rx_cur = 1;
1159                } else {
1160                        f_do_rx_cur = 1;
1161                }
1162        }
1163
1164        if (f_aggr_cur) {
1165                dev_dbg(adapter->dev, "info: current packet aggregation\n");
1166                /* Curr pkt can be aggregated */
1167                mp_rx_aggr_setup(card, skb, port);
1168
1169                if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1170                    mp_rx_aggr_port_limit_reached(card)) {
1171                        dev_dbg(adapter->dev, "info: %s: aggregated packet "
1172                                "limit reached\n", __func__);
1173                        /* No more pkts allowed in Aggr buf, rx it */
1174                        f_do_rx_aggr = 1;
1175                }
1176        }
1177
1178        if (f_do_rx_aggr) {
1179                /* do aggr RX now */
1180                dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1181                        card->mpa_rx.pkt_cnt);
1182
1183                if (card->supports_sdio_new_mode) {
1184                        int i;
1185                        u32 port_count;
1186
1187                        for (i = 0, port_count = 0; i < card->max_ports; i++)
1188                                if (card->mpa_rx.ports & BIT(i))
1189                                        port_count++;
1190
1191                        /* Reading data from "start_port + 0" to "start_port +
1192                         * port_count -1", so decrease the count by 1
1193                         */
1194                        port_count--;
1195                        mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1196                                 (port_count << 8)) + card->mpa_rx.start_port;
1197                } else {
1198                        mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1199                                 (card->mpa_rx.ports << 4)) +
1200                                 card->mpa_rx.start_port;
1201                }
1202
1203                if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1204                                           card->mpa_rx.buf_len, mport, 1))
1205                        goto error;
1206
1207                curr_ptr = card->mpa_rx.buf;
1208
1209                for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1210
1211                        /* get curr PKT len & type */
1212                        pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1213                        pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1214
1215                        /* copy pkt to deaggr buf */
1216                        skb_deaggr = card->mpa_rx.skb_arr[pind];
1217
1218                        if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1219                                         card->mpa_rx.len_arr[pind])) {
1220
1221                                memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1222
1223                                skb_trim(skb_deaggr, pkt_len);
1224
1225                                /* Process de-aggr packet */
1226                                mwifiex_decode_rx_packet(adapter, skb_deaggr,
1227                                                         pkt_type);
1228                        } else {
1229                                dev_err(adapter->dev, "wrong aggr pkt:"
1230                                        " type=%d len=%d max_len=%d\n",
1231                                        pkt_type, pkt_len,
1232                                        card->mpa_rx.len_arr[pind]);
1233                                dev_kfree_skb_any(skb_deaggr);
1234                        }
1235                        curr_ptr += card->mpa_rx.len_arr[pind];
1236                }
1237                MP_RX_AGGR_BUF_RESET(card);
1238        }
1239
1240rx_curr_single:
1241        if (f_do_rx_cur) {
1242                dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1243                        port, rx_len);
1244
1245                if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1246                                              skb->data, skb->len,
1247                                              adapter->ioport + port))
1248                        goto error;
1249
1250                mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1251        }
1252
1253        return 0;
1254
1255error:
1256        if (MP_RX_AGGR_IN_PROGRESS(card)) {
1257                /* Multiport-aggregation transfer failed - cleanup */
1258                for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1259                        /* copy pkt to deaggr buf */
1260                        skb_deaggr = card->mpa_rx.skb_arr[pind];
1261                        dev_kfree_skb_any(skb_deaggr);
1262                }
1263                MP_RX_AGGR_BUF_RESET(card);
1264        }
1265
1266        if (f_do_rx_cur)
1267                /* Single transfer pending. Free curr buff also */
1268                dev_kfree_skb_any(skb);
1269
1270        return -1;
1271}
1272
1273/*
1274 * This function checks the current interrupt status.
1275 *
1276 * The following interrupts are checked and handled by this function -
1277 *      - Data sent
1278 *      - Command sent
1279 *      - Packets received
1280 *
1281 * Since the firmware does not generate download ready interrupt if the
1282 * port updated is command port only, command sent interrupt checking
1283 * should be done manually, and for every SDIO interrupt.
1284 *
1285 * In case of Rx packets received, the packets are uploaded from card to
1286 * host and processed accordingly.
1287 */
1288static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1289{
1290        struct sdio_mmc_card *card = adapter->card;
1291        const struct mwifiex_sdio_card_reg *reg = card->reg;
1292        int ret = 0;
1293        u8 sdio_ireg;
1294        struct sk_buff *skb;
1295        u8 port = CTRL_PORT;
1296        u32 len_reg_l, len_reg_u;
1297        u32 rx_blocks;
1298        u16 rx_len;
1299        unsigned long flags;
1300        u32 bitmap;
1301        u8 cr;
1302
1303        spin_lock_irqsave(&adapter->int_lock, flags);
1304        sdio_ireg = adapter->int_status;
1305        adapter->int_status = 0;
1306        spin_unlock_irqrestore(&adapter->int_lock, flags);
1307
1308        if (!sdio_ireg)
1309                return ret;
1310
1311        /* Following interrupt is only for SDIO new mode */
1312        if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1313                adapter->cmd_sent = false;
1314
1315        /* Following interrupt is only for SDIO new mode */
1316        if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1317                u32 pkt_type;
1318
1319                /* read the len of control packet */
1320                rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1321                rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1322                rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1323                if (rx_len <= INTF_HEADER_LEN ||
1324                    (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1325                     MWIFIEX_RX_DATA_BUF_SIZE)
1326                        return -1;
1327                rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1328
1329                skb = dev_alloc_skb(rx_len);
1330                if (!skb)
1331                        return -1;
1332
1333                skb_put(skb, rx_len);
1334
1335                if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1336                                              skb->len, adapter->ioport |
1337                                                        CMD_PORT_SLCT)) {
1338                        dev_err(adapter->dev,
1339                                "%s: failed to card_to_host", __func__);
1340                        dev_kfree_skb_any(skb);
1341                        goto term_cmd;
1342                }
1343
1344                if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1345                    (pkt_type != MWIFIEX_TYPE_EVENT))
1346                        dev_err(adapter->dev,
1347                                "%s:Received wrong packet on cmd port",
1348                                __func__);
1349
1350                mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1351        }
1352
1353        if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1354                bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1355                bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1356                if (card->supports_sdio_new_mode) {
1357                        bitmap |=
1358                                ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1359                        bitmap |=
1360                                ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1361                }
1362                card->mp_wr_bitmap = bitmap;
1363
1364                dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
1365                        card->mp_wr_bitmap);
1366                if (adapter->data_sent &&
1367                    (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1368                        dev_dbg(adapter->dev,
1369                                "info:  <--- Tx DONE Interrupt --->\n");
1370                        adapter->data_sent = false;
1371                }
1372        }
1373
1374        /* As firmware will not generate download ready interrupt if the port
1375           updated is command port only, cmd_sent should be done for any SDIO
1376           interrupt. */
1377        if (card->has_control_mask && adapter->cmd_sent) {
1378                /* Check if firmware has attach buffer at command port and
1379                   update just that in wr_bit_map. */
1380                card->mp_wr_bitmap |=
1381                        (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1382                if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1383                        adapter->cmd_sent = false;
1384        }
1385
1386        dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1387                adapter->cmd_sent, adapter->data_sent);
1388        if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1389                bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1390                bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1391                if (card->supports_sdio_new_mode) {
1392                        bitmap |=
1393                                ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1394                        bitmap |=
1395                                ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1396                }
1397                card->mp_rd_bitmap = bitmap;
1398                dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
1399                        card->mp_rd_bitmap);
1400
1401                while (true) {
1402                        ret = mwifiex_get_rd_port(adapter, &port);
1403                        if (ret) {
1404                                dev_dbg(adapter->dev,
1405                                        "info: no more rd_port available\n");
1406                                break;
1407                        }
1408                        len_reg_l = reg->rd_len_p0_l + (port << 1);
1409                        len_reg_u = reg->rd_len_p0_u + (port << 1);
1410                        rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1411                        rx_len |= (u16) card->mp_regs[len_reg_l];
1412                        dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1413                                port, rx_len);
1414                        rx_blocks =
1415                                (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1416                                 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1417                        if (rx_len <= INTF_HEADER_LEN ||
1418                            (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1419                             MWIFIEX_RX_DATA_BUF_SIZE) {
1420                                dev_err(adapter->dev, "invalid rx_len=%d\n",
1421                                        rx_len);
1422                                return -1;
1423                        }
1424                        rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1425
1426                        skb = dev_alloc_skb(rx_len);
1427
1428                        if (!skb) {
1429                                dev_err(adapter->dev, "%s: failed to alloc skb",
1430                                        __func__);
1431                                return -1;
1432                        }
1433
1434                        skb_put(skb, rx_len);
1435
1436                        dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1437                                rx_len, skb->len);
1438
1439                        if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1440                                                              port)) {
1441                                dev_err(adapter->dev, "card_to_host_mpa failed:"
1442                                        " int status=%#x\n", sdio_ireg);
1443                                goto term_cmd;
1444                        }
1445                }
1446        }
1447
1448        return 0;
1449
1450term_cmd:
1451        /* terminate cmd */
1452        if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1453                dev_err(adapter->dev, "read CFG reg failed\n");
1454        else
1455                dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1456
1457        if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1458                dev_err(adapter->dev, "write CFG reg failed\n");
1459        else
1460                dev_dbg(adapter->dev, "info: write success\n");
1461
1462        if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1463                dev_err(adapter->dev, "read CFG reg failed\n");
1464        else
1465                dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1466
1467        return -1;
1468}
1469
1470/*
1471 * This function aggregates transmission buffers in driver and downloads
1472 * the aggregated packet to card.
1473 *
1474 * The individual packets are aggregated by copying into an aggregation
1475 * buffer and then downloaded to the card. Previous unsent packets in the
1476 * aggregation buffer are pre-copied first before new packets are added.
1477 * Aggregation is done till there is space left in the aggregation buffer,
1478 * or till new packets are available.
1479 *
1480 * The function will only download the packet to the card when aggregation
1481 * stops, otherwise it will just aggregate the packet in aggregation buffer
1482 * and return.
1483 */
1484static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1485                                        u8 *payload, u32 pkt_len, u32 port,
1486                                        u32 next_pkt_len)
1487{
1488        struct sdio_mmc_card *card = adapter->card;
1489        int ret = 0;
1490        s32 f_send_aggr_buf = 0;
1491        s32 f_send_cur_buf = 0;
1492        s32 f_precopy_cur_buf = 0;
1493        s32 f_postcopy_cur_buf = 0;
1494        u32 mport;
1495
1496        if (!card->mpa_tx.enabled ||
1497            (card->has_control_mask && (port == CTRL_PORT)) ||
1498            (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1499                dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1500                        __func__);
1501
1502                f_send_cur_buf = 1;
1503                goto tx_curr_single;
1504        }
1505
1506        if (next_pkt_len) {
1507                /* More pkt in TX queue */
1508                dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1509                        __func__);
1510
1511                if (MP_TX_AGGR_IN_PROGRESS(card)) {
1512                        if (!mp_tx_aggr_port_limit_reached(card) &&
1513                            MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1514                                f_precopy_cur_buf = 1;
1515
1516                                if (!(card->mp_wr_bitmap &
1517                                      (1 << card->curr_wr_port)) ||
1518                                    !MP_TX_AGGR_BUF_HAS_ROOM(
1519                                            card, pkt_len + next_pkt_len))
1520                                        f_send_aggr_buf = 1;
1521                        } else {
1522                                /* No room in Aggr buf, send it */
1523                                f_send_aggr_buf = 1;
1524
1525                                if (mp_tx_aggr_port_limit_reached(card) ||
1526                                    !(card->mp_wr_bitmap &
1527                                      (1 << card->curr_wr_port)))
1528                                        f_send_cur_buf = 1;
1529                                else
1530                                        f_postcopy_cur_buf = 1;
1531                        }
1532                } else {
1533                        if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1534                            (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1535                                f_precopy_cur_buf = 1;
1536                        else
1537                                f_send_cur_buf = 1;
1538                }
1539        } else {
1540                /* Last pkt in TX queue */
1541                dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1542                        __func__);
1543
1544                if (MP_TX_AGGR_IN_PROGRESS(card)) {
1545                        /* some packs in Aggr buf already */
1546                        f_send_aggr_buf = 1;
1547
1548                        if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1549                                f_precopy_cur_buf = 1;
1550                        else
1551                                /* No room in Aggr buf, send it */
1552                                f_send_cur_buf = 1;
1553                } else {
1554                        f_send_cur_buf = 1;
1555                }
1556        }
1557
1558        if (f_precopy_cur_buf) {
1559                dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1560                        __func__);
1561                MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1562
1563                if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1564                    mp_tx_aggr_port_limit_reached(card))
1565                        /* No more pkts allowed in Aggr buf, send it */
1566                        f_send_aggr_buf = 1;
1567        }
1568
1569        if (f_send_aggr_buf) {
1570                dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1571                        __func__,
1572                                card->mpa_tx.start_port, card->mpa_tx.ports);
1573                if (card->supports_sdio_new_mode) {
1574                        u32 port_count;
1575                        int i;
1576
1577                        for (i = 0, port_count = 0; i < card->max_ports; i++)
1578                                if (card->mpa_tx.ports & BIT(i))
1579                                        port_count++;
1580
1581                        /* Writing data from "start_port + 0" to "start_port +
1582                         * port_count -1", so decrease the count by 1
1583                         */
1584                        port_count--;
1585                        mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1586                                 (port_count << 8)) + card->mpa_tx.start_port;
1587                } else {
1588                        mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1589                                 (card->mpa_tx.ports << 4)) +
1590                                 card->mpa_tx.start_port;
1591                }
1592
1593                ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1594                                                 card->mpa_tx.buf_len, mport);
1595
1596                MP_TX_AGGR_BUF_RESET(card);
1597        }
1598
1599tx_curr_single:
1600        if (f_send_cur_buf) {
1601                dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1602                        __func__, port);
1603                ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1604                                                 adapter->ioport + port);
1605        }
1606
1607        if (f_postcopy_cur_buf) {
1608                dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1609                        __func__);
1610                MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1611        }
1612
1613        return ret;
1614}
1615
1616/*
1617 * This function downloads data from driver to card.
1618 *
1619 * Both commands and data packets are transferred to the card by this
1620 * function.
1621 *
1622 * This function adds the SDIO specific header to the front of the buffer
1623 * before transferring. The header contains the length of the packet and
1624 * the type. The firmware handles the packets based upon this set type.
1625 */
1626static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1627                                     u8 type, struct sk_buff *skb,
1628                                     struct mwifiex_tx_param *tx_param)
1629{
1630        struct sdio_mmc_card *card = adapter->card;
1631        int ret;
1632        u32 buf_block_len;
1633        u32 blk_size;
1634        u32 port = CTRL_PORT;
1635        u8 *payload = (u8 *)skb->data;
1636        u32 pkt_len = skb->len;
1637
1638        /* Allocate buffer and copy payload */
1639        blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1640        buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1641        *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1642        *(__le16 *)&payload[2] = cpu_to_le16(type);
1643
1644        /*
1645         * This is SDIO specific header
1646         *  u16 length,
1647         *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1648         *  MWIFIEX_TYPE_EVENT = 3)
1649         */
1650        if (type == MWIFIEX_TYPE_DATA) {
1651                ret = mwifiex_get_wr_port_data(adapter, &port);
1652                if (ret) {
1653                        dev_err(adapter->dev, "%s: no wr_port available\n",
1654                                __func__);
1655                        return ret;
1656                }
1657        } else {
1658                adapter->cmd_sent = true;
1659                /* Type must be MWIFIEX_TYPE_CMD */
1660
1661                if (pkt_len <= INTF_HEADER_LEN ||
1662                    pkt_len > MWIFIEX_UPLD_SIZE)
1663                        dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1664                                __func__, payload, pkt_len);
1665
1666                if (card->supports_sdio_new_mode)
1667                        port = CMD_PORT_SLCT;
1668        }
1669
1670        /* Transfer data to card */
1671        pkt_len = buf_block_len * blk_size;
1672
1673        if (tx_param)
1674                ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1675                                                   port, tx_param->next_pkt_len
1676                                                   );
1677        else
1678                ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1679                                                   port, 0);
1680
1681        if (ret) {
1682                if (type == MWIFIEX_TYPE_CMD)
1683                        adapter->cmd_sent = false;
1684                if (type == MWIFIEX_TYPE_DATA)
1685                        adapter->data_sent = false;
1686        } else {
1687                if (type == MWIFIEX_TYPE_DATA) {
1688                        if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1689                                adapter->data_sent = true;
1690                        else
1691                                adapter->data_sent = false;
1692                }
1693        }
1694
1695        return ret;
1696}
1697
1698/*
1699 * This function allocates the MPA Tx and Rx buffers.
1700 */
1701static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1702                                   u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1703{
1704        struct sdio_mmc_card *card = adapter->card;
1705        int ret = 0;
1706
1707        card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1708        if (!card->mpa_tx.buf) {
1709                ret = -1;
1710                goto error;
1711        }
1712
1713        card->mpa_tx.buf_size = mpa_tx_buf_size;
1714
1715        card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1716        if (!card->mpa_rx.buf) {
1717                ret = -1;
1718                goto error;
1719        }
1720
1721        card->mpa_rx.buf_size = mpa_rx_buf_size;
1722
1723error:
1724        if (ret) {
1725                kfree(card->mpa_tx.buf);
1726                kfree(card->mpa_rx.buf);
1727        }
1728
1729        return ret;
1730}
1731
1732/*
1733 * This function unregisters the SDIO device.
1734 *
1735 * The SDIO IRQ is released, the function is disabled and driver
1736 * data is set to null.
1737 */
1738static void
1739mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1740{
1741        struct sdio_mmc_card *card = adapter->card;
1742
1743        if (adapter->card) {
1744                sdio_claim_host(card->func);
1745                sdio_disable_func(card->func);
1746                sdio_release_host(card->func);
1747        }
1748}
1749
1750/*
1751 * This function registers the SDIO device.
1752 *
1753 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1754 */
1755static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1756{
1757        int ret;
1758        struct sdio_mmc_card *card = adapter->card;
1759        struct sdio_func *func = card->func;
1760
1761        /* save adapter pointer in card */
1762        card->adapter = adapter;
1763
1764        sdio_claim_host(func);
1765
1766        /* Set block size */
1767        ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1768        sdio_release_host(func);
1769        if (ret) {
1770                pr_err("cannot set SDIO block size\n");
1771                return ret;
1772        }
1773
1774
1775        adapter->dev = &func->dev;
1776
1777        strcpy(adapter->fw_name, card->firmware);
1778
1779        return 0;
1780}
1781
1782/*
1783 * This function initializes the SDIO driver.
1784 *
1785 * The following initializations steps are followed -
1786 *      - Read the Host interrupt status register to acknowledge
1787 *        the first interrupt got from bootloader
1788 *      - Disable host interrupt mask register
1789 *      - Get SDIO port
1790 *      - Initialize SDIO variables in card
1791 *      - Allocate MP registers
1792 *      - Allocate MPA Tx and Rx buffers
1793 */
1794static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1795{
1796        struct sdio_mmc_card *card = adapter->card;
1797        const struct mwifiex_sdio_card_reg *reg = card->reg;
1798        int ret;
1799        u8 sdio_ireg;
1800
1801        sdio_set_drvdata(card->func, card);
1802
1803        /*
1804         * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1805         * from the bootloader. If we don't do this we get a interrupt
1806         * as soon as we register the irq.
1807         */
1808        mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1809
1810        /* Get SDIO ioport */
1811        mwifiex_init_sdio_ioport(adapter);
1812
1813        /* Initialize SDIO variables in card */
1814        card->mp_rd_bitmap = 0;
1815        card->mp_wr_bitmap = 0;
1816        card->curr_rd_port = reg->start_rd_port;
1817        card->curr_wr_port = reg->start_wr_port;
1818
1819        card->mp_data_port_mask = reg->data_port_mask;
1820
1821        card->mpa_tx.buf_len = 0;
1822        card->mpa_tx.pkt_cnt = 0;
1823        card->mpa_tx.start_port = 0;
1824
1825        card->mpa_tx.enabled = 1;
1826        card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1827
1828        card->mpa_rx.buf_len = 0;
1829        card->mpa_rx.pkt_cnt = 0;
1830        card->mpa_rx.start_port = 0;
1831
1832        card->mpa_rx.enabled = 1;
1833        card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
1834
1835        /* Allocate buffers for SDIO MP-A */
1836        card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
1837        if (!card->mp_regs)
1838                return -ENOMEM;
1839
1840        /* Allocate skb pointer buffers */
1841        card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1842                                       card->mp_agg_pkt_limit, GFP_KERNEL);
1843        card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1844                                       card->mp_agg_pkt_limit, GFP_KERNEL);
1845        ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1846                                             SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1847                                             SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1848        if (ret) {
1849                dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1850                kfree(card->mp_regs);
1851                return -1;
1852        }
1853
1854        return ret;
1855}
1856
1857/*
1858 * This function resets the MPA Tx and Rx buffers.
1859 */
1860static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1861{
1862        struct sdio_mmc_card *card = adapter->card;
1863
1864        MP_TX_AGGR_BUF_RESET(card);
1865        MP_RX_AGGR_BUF_RESET(card);
1866}
1867
1868/*
1869 * This function cleans up the allocated card buffers.
1870 *
1871 * The following are freed by this function -
1872 *      - MP registers
1873 *      - MPA Tx buffer
1874 *      - MPA Rx buffer
1875 */
1876static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1877{
1878        struct sdio_mmc_card *card = adapter->card;
1879
1880        kfree(card->mp_regs);
1881        kfree(card->mpa_rx.skb_arr);
1882        kfree(card->mpa_rx.len_arr);
1883        kfree(card->mpa_tx.buf);
1884        kfree(card->mpa_rx.buf);
1885        sdio_set_drvdata(card->func, NULL);
1886        kfree(card);
1887}
1888
1889/*
1890 * This function updates the MP end port in card.
1891 */
1892static void
1893mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1894{
1895        struct sdio_mmc_card *card = adapter->card;
1896        const struct mwifiex_sdio_card_reg *reg = card->reg;
1897        int i;
1898
1899        card->mp_end_port = port;
1900
1901        card->mp_data_port_mask = reg->data_port_mask;
1902
1903        if (reg->start_wr_port) {
1904                for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1905                        card->mp_data_port_mask &=
1906                                        ~(1 << (card->max_ports - i));
1907        }
1908
1909        card->curr_wr_port = reg->start_wr_port;
1910
1911        dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1912                port, card->mp_data_port_mask);
1913}
1914
1915static struct mmc_host *reset_host;
1916static void sdio_card_reset_worker(struct work_struct *work)
1917{
1918        struct mmc_host *target = reset_host;
1919
1920        /* The actual reset operation must be run outside of driver thread.
1921         * This is because mmc_remove_host() will cause the device to be
1922         * instantly destroyed, and the driver then needs to end its thread,
1923         * leading to a deadlock.
1924         *
1925         * We run it in a totally independent workqueue.
1926         */
1927
1928        pr_err("Resetting card...\n");
1929        mmc_remove_host(target);
1930        /* 20ms delay is based on experiment with sdhci controller */
1931        mdelay(20);
1932        mmc_add_host(target);
1933}
1934static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1935
1936/* This function resets the card */
1937static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1938{
1939        struct sdio_mmc_card *card = adapter->card;
1940
1941        reset_host = card->func->card->host;
1942        schedule_work(&card_reset_work);
1943}
1944
1945static struct mwifiex_if_ops sdio_ops = {
1946        .init_if = mwifiex_init_sdio,
1947        .cleanup_if = mwifiex_cleanup_sdio,
1948        .check_fw_status = mwifiex_check_fw_status,
1949        .prog_fw = mwifiex_prog_fw_w_helper,
1950        .register_dev = mwifiex_register_dev,
1951        .unregister_dev = mwifiex_unregister_dev,
1952        .enable_int = mwifiex_sdio_enable_host_int,
1953        .disable_int = mwifiex_sdio_disable_host_int,
1954        .process_int_status = mwifiex_process_int_status,
1955        .host_to_card = mwifiex_sdio_host_to_card,
1956        .wakeup = mwifiex_pm_wakeup_card,
1957        .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1958
1959        /* SDIO specific */
1960        .update_mp_end_port = mwifiex_update_mp_end_port,
1961        .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1962        .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1963        .event_complete = mwifiex_sdio_event_complete,
1964        .card_reset = mwifiex_sdio_card_reset,
1965};
1966
1967/*
1968 * This function initializes the SDIO driver.
1969 *
1970 * This initiates the semaphore and registers the device with
1971 * SDIO bus.
1972 */
1973static int
1974mwifiex_sdio_init_module(void)
1975{
1976        sema_init(&add_remove_card_sem, 1);
1977
1978        /* Clear the flag in case user removes the card. */
1979        user_rmmod = 0;
1980
1981        return sdio_register_driver(&mwifiex_sdio);
1982}
1983
1984/*
1985 * This function cleans up the SDIO driver.
1986 *
1987 * The following major steps are followed for cleanup -
1988 *      - Resume the device if its suspended
1989 *      - Disconnect the device if connected
1990 *      - Shutdown the firmware
1991 *      - Unregister the device from SDIO bus.
1992 */
1993static void
1994mwifiex_sdio_cleanup_module(void)
1995{
1996        if (!down_interruptible(&add_remove_card_sem))
1997                up(&add_remove_card_sem);
1998
1999        /* Set the flag as user is removing this module. */
2000        user_rmmod = 1;
2001
2002        cancel_work_sync(&card_reset_work);
2003        sdio_unregister_driver(&mwifiex_sdio);
2004}
2005
2006module_init(mwifiex_sdio_init_module);
2007module_exit(mwifiex_sdio_cleanup_module);
2008
2009MODULE_AUTHOR("Marvell International Ltd.");
2010MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2011MODULE_VERSION(SDIO_VERSION);
2012MODULE_LICENSE("GPL v2");
2013MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2014MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2015MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2016MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2017
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.