linux/drivers/net/wireless/marvell/mwifiex/main.c
<<
>>
Prefs
   1/*
   2 * Marvell Wireless LAN device driver: major functions
   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/suspend.h>
  21
  22#include "main.h"
  23#include "wmm.h"
  24#include "cfg80211.h"
  25#include "11n.h"
  26
  27#define VERSION "1.0"
  28#define MFG_FIRMWARE    "mwifiex_mfg.bin"
  29
  30static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
  31module_param(debug_mask, uint, 0);
  32MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
  33
  34const char driver_version[] = "mwifiex " VERSION " (%s) ";
  35static char *cal_data_cfg;
  36module_param(cal_data_cfg, charp, 0);
  37
  38static unsigned short driver_mode;
  39module_param(driver_mode, ushort, 0);
  40MODULE_PARM_DESC(driver_mode,
  41                 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
  42
  43bool mfg_mode;
  44module_param(mfg_mode, bool, 0);
  45MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
  46
  47bool aggr_ctrl;
  48module_param(aggr_ctrl, bool, 0000);
  49MODULE_PARM_DESC(aggr_ctrl, "usb tx aggregation enable:1, disable:0");
  50
  51/*
  52 * This function registers the device and performs all the necessary
  53 * initializations.
  54 *
  55 * The following initialization operations are performed -
  56 *      - Allocate adapter structure
  57 *      - Save interface specific operations table in adapter
  58 *      - Call interface specific initialization routine
  59 *      - Allocate private structures
  60 *      - Set default adapter structure parameters
  61 *      - Initialize locks
  62 *
  63 * In case of any errors during inittialization, this function also ensures
  64 * proper cleanup before exiting.
  65 */
  66static int mwifiex_register(void *card, struct device *dev,
  67                            struct mwifiex_if_ops *if_ops, void **padapter)
  68{
  69        struct mwifiex_adapter *adapter;
  70        int i;
  71
  72        adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
  73        if (!adapter)
  74                return -ENOMEM;
  75
  76        *padapter = adapter;
  77        adapter->dev = dev;
  78        adapter->card = card;
  79
  80        /* Save interface specific operations in adapter */
  81        memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
  82        adapter->debug_mask = debug_mask;
  83
  84        /* card specific initialization has been deferred until now .. */
  85        if (adapter->if_ops.init_if)
  86                if (adapter->if_ops.init_if(adapter))
  87                        goto error;
  88
  89        adapter->priv_num = 0;
  90
  91        for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
  92                /* Allocate memory for private structure */
  93                adapter->priv[i] =
  94                        kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
  95                if (!adapter->priv[i])
  96                        goto error;
  97
  98                adapter->priv[i]->adapter = adapter;
  99                adapter->priv_num++;
 100        }
 101        mwifiex_init_lock_list(adapter);
 102
 103        setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
 104                    (unsigned long)adapter);
 105
 106        return 0;
 107
 108error:
 109        mwifiex_dbg(adapter, ERROR,
 110                    "info: leave mwifiex_register with error\n");
 111
 112        for (i = 0; i < adapter->priv_num; i++)
 113                kfree(adapter->priv[i]);
 114
 115        kfree(adapter);
 116
 117        return -1;
 118}
 119
 120/*
 121 * This function unregisters the device and performs all the necessary
 122 * cleanups.
 123 *
 124 * The following cleanup operations are performed -
 125 *      - Free the timers
 126 *      - Free beacon buffers
 127 *      - Free private structures
 128 *      - Free adapter structure
 129 */
 130static int mwifiex_unregister(struct mwifiex_adapter *adapter)
 131{
 132        s32 i;
 133
 134        if (adapter->if_ops.cleanup_if)
 135                adapter->if_ops.cleanup_if(adapter);
 136
 137        del_timer_sync(&adapter->cmd_timer);
 138
 139        /* Free private structures */
 140        for (i = 0; i < adapter->priv_num; i++) {
 141                if (adapter->priv[i]) {
 142                        mwifiex_free_curr_bcn(adapter->priv[i]);
 143                        kfree(adapter->priv[i]);
 144                }
 145        }
 146
 147        if (adapter->nd_info) {
 148                for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
 149                        kfree(adapter->nd_info->matches[i]);
 150                kfree(adapter->nd_info);
 151                adapter->nd_info = NULL;
 152        }
 153
 154        kfree(adapter->regd);
 155
 156        kfree(adapter);
 157        return 0;
 158}
 159
 160void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
 161{
 162        unsigned long flags;
 163
 164        spin_lock_irqsave(&adapter->main_proc_lock, flags);
 165        if (adapter->mwifiex_processing) {
 166                adapter->more_task_flag = true;
 167                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 168        } else {
 169                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 170                queue_work(adapter->workqueue, &adapter->main_work);
 171        }
 172}
 173EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
 174
 175static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
 176{
 177        unsigned long flags;
 178
 179        spin_lock_irqsave(&adapter->rx_proc_lock, flags);
 180        if (adapter->rx_processing) {
 181                spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 182        } else {
 183                spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 184                queue_work(adapter->rx_workqueue, &adapter->rx_work);
 185        }
 186}
 187
 188static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
 189{
 190        unsigned long flags;
 191        struct sk_buff *skb;
 192        struct mwifiex_rxinfo *rx_info;
 193
 194        spin_lock_irqsave(&adapter->rx_proc_lock, flags);
 195        if (adapter->rx_processing || adapter->rx_locked) {
 196                spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 197                goto exit_rx_proc;
 198        } else {
 199                adapter->rx_processing = true;
 200                spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 201        }
 202
 203        /* Check for Rx data */
 204        while ((skb = skb_dequeue(&adapter->rx_data_q))) {
 205                atomic_dec(&adapter->rx_pending);
 206                if ((adapter->delay_main_work ||
 207                     adapter->iface_type == MWIFIEX_USB) &&
 208                    (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
 209                        if (adapter->if_ops.submit_rem_rx_urbs)
 210                                adapter->if_ops.submit_rem_rx_urbs(adapter);
 211                        adapter->delay_main_work = false;
 212                        mwifiex_queue_main_work(adapter);
 213                }
 214                rx_info = MWIFIEX_SKB_RXCB(skb);
 215                if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
 216                        if (adapter->if_ops.deaggr_pkt)
 217                                adapter->if_ops.deaggr_pkt(adapter, skb);
 218                        dev_kfree_skb_any(skb);
 219                } else {
 220                        mwifiex_handle_rx_packet(adapter, skb);
 221                }
 222        }
 223        spin_lock_irqsave(&adapter->rx_proc_lock, flags);
 224        adapter->rx_processing = false;
 225        spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
 226
 227exit_rx_proc:
 228        return 0;
 229}
 230
 231/*
 232 * The main process.
 233 *
 234 * This function is the main procedure of the driver and handles various driver
 235 * operations. It runs in a loop and provides the core functionalities.
 236 *
 237 * The main responsibilities of this function are -
 238 *      - Ensure concurrency control
 239 *      - Handle pending interrupts and call interrupt handlers
 240 *      - Wake up the card if required
 241 *      - Handle command responses and call response handlers
 242 *      - Handle events and call event handlers
 243 *      - Execute pending commands
 244 *      - Transmit pending data packets
 245 */
 246int mwifiex_main_process(struct mwifiex_adapter *adapter)
 247{
 248        int ret = 0;
 249        unsigned long flags;
 250
 251        spin_lock_irqsave(&adapter->main_proc_lock, flags);
 252
 253        /* Check if already processing */
 254        if (adapter->mwifiex_processing || adapter->main_locked) {
 255                adapter->more_task_flag = true;
 256                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 257                return 0;
 258        } else {
 259                adapter->mwifiex_processing = true;
 260                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 261        }
 262process_start:
 263        do {
 264                if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
 265                        break;
 266
 267                /* For non-USB interfaces, If we process interrupts first, it
 268                 * would increase RX pending even further. Avoid this by
 269                 * checking if rx_pending has crossed high threshold and
 270                 * schedule rx work queue and then process interrupts.
 271                 * For USB interface, there are no interrupts. We already have
 272                 * HIGH_RX_PENDING check in usb.c
 273                 */
 274                if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
 275                    adapter->iface_type != MWIFIEX_USB) {
 276                        adapter->delay_main_work = true;
 277                        mwifiex_queue_rx_work(adapter);
 278                        break;
 279                }
 280
 281                /* Handle pending interrupt if any */
 282                if (adapter->int_status) {
 283                        if (adapter->hs_activated)
 284                                mwifiex_process_hs_config(adapter);
 285                        if (adapter->if_ops.process_int_status)
 286                                adapter->if_ops.process_int_status(adapter);
 287                }
 288
 289                if (adapter->rx_work_enabled && adapter->data_received)
 290                        mwifiex_queue_rx_work(adapter);
 291
 292                /* Need to wake up the card ? */
 293                if ((adapter->ps_state == PS_STATE_SLEEP) &&
 294                    (adapter->pm_wakeup_card_req &&
 295                     !adapter->pm_wakeup_fw_try) &&
 296                    (is_command_pending(adapter) ||
 297                     !skb_queue_empty(&adapter->tx_data_q) ||
 298                     !mwifiex_bypass_txlist_empty(adapter) ||
 299                     !mwifiex_wmm_lists_empty(adapter))) {
 300                        adapter->pm_wakeup_fw_try = true;
 301                        mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
 302                        adapter->if_ops.wakeup(adapter);
 303                        continue;
 304                }
 305
 306                if (IS_CARD_RX_RCVD(adapter)) {
 307                        adapter->data_received = false;
 308                        adapter->pm_wakeup_fw_try = false;
 309                        del_timer(&adapter->wakeup_timer);
 310                        if (adapter->ps_state == PS_STATE_SLEEP)
 311                                adapter->ps_state = PS_STATE_AWAKE;
 312                } else {
 313                        /* We have tried to wakeup the card already */
 314                        if (adapter->pm_wakeup_fw_try)
 315                                break;
 316                        if (adapter->ps_state == PS_STATE_PRE_SLEEP)
 317                                mwifiex_check_ps_cond(adapter);
 318
 319                        if (adapter->ps_state != PS_STATE_AWAKE)
 320                                break;
 321                        if (adapter->tx_lock_flag) {
 322                                if (adapter->iface_type == MWIFIEX_USB) {
 323                                        if (!adapter->usb_mc_setup)
 324                                                break;
 325                                } else
 326                                        break;
 327                        }
 328
 329                        if ((!adapter->scan_chan_gap_enabled &&
 330                             adapter->scan_processing) || adapter->data_sent ||
 331                             mwifiex_is_tdls_chan_switching
 332                             (mwifiex_get_priv(adapter,
 333                                               MWIFIEX_BSS_ROLE_STA)) ||
 334                            (mwifiex_wmm_lists_empty(adapter) &&
 335                             mwifiex_bypass_txlist_empty(adapter) &&
 336                             skb_queue_empty(&adapter->tx_data_q))) {
 337                                if (adapter->cmd_sent || adapter->curr_cmd ||
 338                                        !mwifiex_is_send_cmd_allowed
 339                                                (mwifiex_get_priv(adapter,
 340                                                MWIFIEX_BSS_ROLE_STA)) ||
 341                                    (!is_command_pending(adapter)))
 342                                        break;
 343                        }
 344                }
 345
 346                /* Check for event */
 347                if (adapter->event_received) {
 348                        adapter->event_received = false;
 349                        mwifiex_process_event(adapter);
 350                }
 351
 352                /* Check for Cmd Resp */
 353                if (adapter->cmd_resp_received) {
 354                        adapter->cmd_resp_received = false;
 355                        mwifiex_process_cmdresp(adapter);
 356
 357                        /* call mwifiex back when init_fw is done */
 358                        if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
 359                                adapter->hw_status = MWIFIEX_HW_STATUS_READY;
 360                                mwifiex_init_fw_complete(adapter);
 361                        }
 362                }
 363
 364                /* Check if we need to confirm Sleep Request
 365                   received previously */
 366                if (adapter->ps_state == PS_STATE_PRE_SLEEP)
 367                        mwifiex_check_ps_cond(adapter);
 368
 369                /* * The ps_state may have been changed during processing of
 370                 * Sleep Request event.
 371                 */
 372                if ((adapter->ps_state == PS_STATE_SLEEP) ||
 373                    (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
 374                    (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
 375                        continue;
 376                }
 377
 378                if (adapter->tx_lock_flag) {
 379                        if (adapter->iface_type == MWIFIEX_USB) {
 380                                if (!adapter->usb_mc_setup)
 381                                        continue;
 382                        } else
 383                                continue;
 384                }
 385
 386                if (!adapter->cmd_sent && !adapter->curr_cmd &&
 387                    mwifiex_is_send_cmd_allowed
 388                    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
 389                        if (mwifiex_exec_next_cmd(adapter) == -1) {
 390                                ret = -1;
 391                                break;
 392                        }
 393                }
 394
 395                /** If USB Multi channel setup ongoing,
 396                 *  wait for ready to tx data.
 397                 */
 398                if (adapter->iface_type == MWIFIEX_USB &&
 399                    adapter->usb_mc_setup)
 400                        continue;
 401
 402                if ((adapter->scan_chan_gap_enabled ||
 403                     !adapter->scan_processing) &&
 404                    !adapter->data_sent &&
 405                    !skb_queue_empty(&adapter->tx_data_q)) {
 406                        mwifiex_process_tx_queue(adapter);
 407                        if (adapter->hs_activated) {
 408                                adapter->is_hs_configured = false;
 409                                mwifiex_hs_activated_event
 410                                        (mwifiex_get_priv
 411                                        (adapter, MWIFIEX_BSS_ROLE_ANY),
 412                                        false);
 413                        }
 414                }
 415
 416                if ((adapter->scan_chan_gap_enabled ||
 417                     !adapter->scan_processing) &&
 418                    !adapter->data_sent &&
 419                    !mwifiex_bypass_txlist_empty(adapter) &&
 420                    !mwifiex_is_tdls_chan_switching
 421                        (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
 422                        mwifiex_process_bypass_tx(adapter);
 423                        if (adapter->hs_activated) {
 424                                adapter->is_hs_configured = false;
 425                                mwifiex_hs_activated_event
 426                                        (mwifiex_get_priv
 427                                         (adapter, MWIFIEX_BSS_ROLE_ANY),
 428                                         false);
 429                        }
 430                }
 431
 432                if ((adapter->scan_chan_gap_enabled ||
 433                     !adapter->scan_processing) &&
 434                    !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
 435                    !mwifiex_is_tdls_chan_switching
 436                        (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
 437                        mwifiex_wmm_process_tx(adapter);
 438                        if (adapter->hs_activated) {
 439                                adapter->is_hs_configured = false;
 440                                mwifiex_hs_activated_event
 441                                        (mwifiex_get_priv
 442                                         (adapter, MWIFIEX_BSS_ROLE_ANY),
 443                                         false);
 444                        }
 445                }
 446
 447                if (adapter->delay_null_pkt && !adapter->cmd_sent &&
 448                    !adapter->curr_cmd && !is_command_pending(adapter) &&
 449                    (mwifiex_wmm_lists_empty(adapter) &&
 450                     mwifiex_bypass_txlist_empty(adapter) &&
 451                     skb_queue_empty(&adapter->tx_data_q))) {
 452                        if (!mwifiex_send_null_packet
 453                            (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
 454                             MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
 455                             MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
 456                                adapter->delay_null_pkt = false;
 457                                adapter->ps_state = PS_STATE_SLEEP;
 458                        }
 459                        break;
 460                }
 461        } while (true);
 462
 463        spin_lock_irqsave(&adapter->main_proc_lock, flags);
 464        if (adapter->more_task_flag) {
 465                adapter->more_task_flag = false;
 466                spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 467                goto process_start;
 468        }
 469        adapter->mwifiex_processing = false;
 470        spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
 471
 472        return ret;
 473}
 474EXPORT_SYMBOL_GPL(mwifiex_main_process);
 475
 476/*
 477 * This function frees the adapter structure.
 478 *
 479 * Additionally, this closes the netlink socket, frees the timers
 480 * and private structures.
 481 */
 482static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
 483{
 484        if (!adapter) {
 485                pr_err("%s: adapter is NULL\n", __func__);
 486                return;
 487        }
 488
 489        mwifiex_unregister(adapter);
 490        pr_debug("info: %s: free adapter\n", __func__);
 491}
 492
 493/*
 494 * This function cancels all works in the queue and destroys
 495 * the main workqueue.
 496 */
 497static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
 498{
 499        if (adapter->workqueue) {
 500                flush_workqueue(adapter->workqueue);
 501                destroy_workqueue(adapter->workqueue);
 502                adapter->workqueue = NULL;
 503        }
 504
 505        if (adapter->rx_workqueue) {
 506                flush_workqueue(adapter->rx_workqueue);
 507                destroy_workqueue(adapter->rx_workqueue);
 508                adapter->rx_workqueue = NULL;
 509        }
 510}
 511
 512/*
 513 * This function gets firmware and initializes it.
 514 *
 515 * The main initialization steps followed are -
 516 *      - Download the correct firmware to card
 517 *      - Issue the init commands to firmware
 518 */
 519static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
 520{
 521        int ret;
 522        char fmt[64];
 523        struct mwifiex_adapter *adapter = context;
 524        struct mwifiex_fw_image fw;
 525        bool init_failed = false;
 526        struct wireless_dev *wdev;
 527        struct completion *fw_done = adapter->fw_done;
 528
 529        if (!firmware) {
 530                mwifiex_dbg(adapter, ERROR,
 531                            "Failed to get firmware %s\n", adapter->fw_name);
 532                goto err_dnld_fw;
 533        }
 534
 535        memset(&fw, 0, sizeof(struct mwifiex_fw_image));
 536        adapter->firmware = firmware;
 537        fw.fw_buf = (u8 *) adapter->firmware->data;
 538        fw.fw_len = adapter->firmware->size;
 539
 540        if (adapter->if_ops.dnld_fw) {
 541                ret = adapter->if_ops.dnld_fw(adapter, &fw);
 542        } else {
 543                ret = mwifiex_dnld_fw(adapter, &fw);
 544        }
 545
 546        if (ret == -1)
 547                goto err_dnld_fw;
 548
 549        mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
 550
 551        if (cal_data_cfg) {
 552                if ((request_firmware(&adapter->cal_data, cal_data_cfg,
 553                                      adapter->dev)) < 0)
 554                        mwifiex_dbg(adapter, ERROR,
 555                                    "Cal data request_firmware() failed\n");
 556        }
 557
 558        /* enable host interrupt after fw dnld is successful */
 559        if (adapter->if_ops.enable_int) {
 560                if (adapter->if_ops.enable_int(adapter))
 561                        goto err_dnld_fw;
 562        }
 563
 564        adapter->init_wait_q_woken = false;
 565        ret = mwifiex_init_fw(adapter);
 566        if (ret == -1) {
 567                goto err_init_fw;
 568        } else if (!ret) {
 569                adapter->hw_status = MWIFIEX_HW_STATUS_READY;
 570                goto done;
 571        }
 572        /* Wait for mwifiex_init to complete */
 573        if (!adapter->mfg_mode) {
 574                wait_event_interruptible(adapter->init_wait_q,
 575                                         adapter->init_wait_q_woken);
 576                if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
 577                        goto err_init_fw;
 578        }
 579
 580        if (!adapter->wiphy) {
 581                if (mwifiex_register_cfg80211(adapter)) {
 582                        mwifiex_dbg(adapter, ERROR,
 583                                    "cannot register with cfg80211\n");
 584                        goto err_init_fw;
 585                }
 586        }
 587
 588        if (mwifiex_init_channel_scan_gap(adapter)) {
 589                mwifiex_dbg(adapter, ERROR,
 590                            "could not init channel stats table\n");
 591                goto err_init_chan_scan;
 592        }
 593
 594        if (driver_mode) {
 595                driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
 596                driver_mode |= MWIFIEX_DRIVER_MODE_STA;
 597        }
 598
 599        rtnl_lock();
 600        /* Create station interface by default */
 601        wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
 602                                        NL80211_IFTYPE_STATION, NULL);
 603        if (IS_ERR(wdev)) {
 604                mwifiex_dbg(adapter, ERROR,
 605                            "cannot create default STA interface\n");
 606                rtnl_unlock();
 607                goto err_add_intf;
 608        }
 609
 610        if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
 611                wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
 612                                                NL80211_IFTYPE_AP, NULL);
 613                if (IS_ERR(wdev)) {
 614                        mwifiex_dbg(adapter, ERROR,
 615                                    "cannot create AP interface\n");
 616                        rtnl_unlock();
 617                        goto err_add_intf;
 618                }
 619        }
 620
 621        if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
 622                wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
 623                                                NL80211_IFTYPE_P2P_CLIENT, NULL);
 624                if (IS_ERR(wdev)) {
 625                        mwifiex_dbg(adapter, ERROR,
 626                                    "cannot create p2p client interface\n");
 627                        rtnl_unlock();
 628                        goto err_add_intf;
 629                }
 630        }
 631        rtnl_unlock();
 632
 633        mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
 634        mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
 635        goto done;
 636
 637err_add_intf:
 638        vfree(adapter->chan_stats);
 639err_init_chan_scan:
 640        wiphy_unregister(adapter->wiphy);
 641        wiphy_free(adapter->wiphy);
 642err_init_fw:
 643        if (adapter->if_ops.disable_int)
 644                adapter->if_ops.disable_int(adapter);
 645err_dnld_fw:
 646        mwifiex_dbg(adapter, ERROR,
 647                    "info: %s: unregister device\n", __func__);
 648        if (adapter->if_ops.unregister_dev)
 649                adapter->if_ops.unregister_dev(adapter);
 650
 651        adapter->surprise_removed = true;
 652        mwifiex_terminate_workqueue(adapter);
 653
 654        if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
 655                pr_debug("info: %s: shutdown mwifiex\n", __func__);
 656                mwifiex_shutdown_drv(adapter);
 657                mwifiex_free_cmd_buffers(adapter);
 658        }
 659
 660        init_failed = true;
 661done:
 662        if (adapter->cal_data) {
 663                release_firmware(adapter->cal_data);
 664                adapter->cal_data = NULL;
 665        }
 666        if (adapter->firmware) {
 667                release_firmware(adapter->firmware);
 668                adapter->firmware = NULL;
 669        }
 670        if (init_failed) {
 671                if (adapter->irq_wakeup >= 0)
 672                        device_init_wakeup(adapter->dev, false);
 673                mwifiex_free_adapter(adapter);
 674        }
 675        /* Tell all current and future waiters we're finished */
 676        complete_all(fw_done);
 677
 678        return init_failed ? -EIO : 0;
 679}
 680
 681static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
 682{
 683        _mwifiex_fw_dpc(firmware, context);
 684}
 685
 686/*
 687 * This function gets the firmware and (if called asynchronously) kicks off the
 688 * HW init when done.
 689 */
 690static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
 691                              bool req_fw_nowait)
 692{
 693        int ret;
 694
 695        /* Override default firmware with manufacturing one if
 696         * manufacturing mode is enabled
 697         */
 698        if (mfg_mode) {
 699                if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
 700                            sizeof(adapter->fw_name)) >=
 701                            sizeof(adapter->fw_name)) {
 702                        pr_err("%s: fw_name too long!\n", __func__);
 703                        return -1;
 704                }
 705        }
 706
 707        if (req_fw_nowait) {
 708                ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
 709                                              adapter->dev, GFP_KERNEL, adapter,
 710                                              mwifiex_fw_dpc);
 711        } else {
 712                ret = request_firmware(&adapter->firmware,
 713                                       adapter->fw_name,
 714                                       adapter->dev);
 715        }
 716
 717        if (ret < 0)
 718                mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
 719                            req_fw_nowait ? "_nowait" : "", ret);
 720        return ret;
 721}
 722
 723/*
 724 * CFG802.11 network device handler for open.
 725 *
 726 * Starts the data queue.
 727 */
 728static int
 729mwifiex_open(struct net_device *dev)
 730{
 731        netif_carrier_off(dev);
 732
 733        return 0;
 734}
 735
 736/*
 737 * CFG802.11 network device handler for close.
 738 */
 739static int
 740mwifiex_close(struct net_device *dev)
 741{
 742        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 743
 744        if (priv->scan_request) {
 745                struct cfg80211_scan_info info = {
 746                        .aborted = true,
 747                };
 748
 749                mwifiex_dbg(priv->adapter, INFO,
 750                            "aborting scan on ndo_stop\n");
 751                cfg80211_scan_done(priv->scan_request, &info);
 752                priv->scan_request = NULL;
 753                priv->scan_aborting = true;
 754        }
 755
 756        if (priv->sched_scanning) {
 757                mwifiex_dbg(priv->adapter, INFO,
 758                            "aborting bgscan on ndo_stop\n");
 759                mwifiex_stop_bg_scan(priv);
 760                cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
 761        }
 762
 763        return 0;
 764}
 765
 766static bool
 767mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
 768                        struct sk_buff *skb)
 769{
 770        struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
 771
 772        if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
 773            mwifiex_is_skb_mgmt_frame(skb) ||
 774            (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
 775             ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
 776             (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
 777                mwifiex_dbg(priv->adapter, DATA,
 778                            "bypass txqueue; eth type %#x, mgmt %d\n",
 779                             ntohs(eth_hdr->h_proto),
 780                             mwifiex_is_skb_mgmt_frame(skb));
 781                return true;
 782        }
 783
 784        return false;
 785}
 786/*
 787 * Add buffer into wmm tx queue and queue work to transmit it.
 788 */
 789int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
 790{
 791        struct netdev_queue *txq;
 792        int index = mwifiex_1d_to_wmm_queue[skb->priority];
 793
 794        if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
 795                txq = netdev_get_tx_queue(priv->netdev, index);
 796                if (!netif_tx_queue_stopped(txq)) {
 797                        netif_tx_stop_queue(txq);
 798                        mwifiex_dbg(priv->adapter, DATA,
 799                                    "stop queue: %d\n", index);
 800                }
 801        }
 802
 803        if (mwifiex_bypass_tx_queue(priv, skb)) {
 804                atomic_inc(&priv->adapter->tx_pending);
 805                atomic_inc(&priv->adapter->bypass_tx_pending);
 806                mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
 807         } else {
 808                atomic_inc(&priv->adapter->tx_pending);
 809                mwifiex_wmm_add_buf_txqueue(priv, skb);
 810         }
 811
 812        mwifiex_queue_main_work(priv->adapter);
 813
 814        return 0;
 815}
 816
 817struct sk_buff *
 818mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
 819                                struct sk_buff *skb, u8 flag, u64 *cookie)
 820{
 821        struct sk_buff *orig_skb = skb;
 822        struct mwifiex_txinfo *tx_info, *orig_tx_info;
 823
 824        skb = skb_clone(skb, GFP_ATOMIC);
 825        if (skb) {
 826                unsigned long flags;
 827                int id;
 828
 829                spin_lock_irqsave(&priv->ack_status_lock, flags);
 830                id = idr_alloc(&priv->ack_status_frames, orig_skb,
 831                               1, 0x10, GFP_ATOMIC);
 832                spin_unlock_irqrestore(&priv->ack_status_lock, flags);
 833
 834                if (id >= 0) {
 835                        tx_info = MWIFIEX_SKB_TXCB(skb);
 836                        tx_info->ack_frame_id = id;
 837                        tx_info->flags |= flag;
 838                        orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
 839                        orig_tx_info->ack_frame_id = id;
 840                        orig_tx_info->flags |= flag;
 841
 842                        if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
 843                                orig_tx_info->cookie = *cookie;
 844
 845                } else if (skb_shared(skb)) {
 846                        kfree_skb(orig_skb);
 847                } else {
 848                        kfree_skb(skb);
 849                        skb = orig_skb;
 850                }
 851        } else {
 852                /* couldn't clone -- lose tx status ... */
 853                skb = orig_skb;
 854        }
 855
 856        return skb;
 857}
 858
 859/*
 860 * CFG802.11 network device handler for data transmission.
 861 */
 862static int
 863mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
 864{
 865        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 866        struct sk_buff *new_skb;
 867        struct mwifiex_txinfo *tx_info;
 868        bool multicast;
 869
 870        mwifiex_dbg(priv->adapter, DATA,
 871                    "data: %lu BSS(%d-%d): Data <= kernel\n",
 872                    jiffies, priv->bss_type, priv->bss_num);
 873
 874        if (priv->adapter->surprise_removed) {
 875                kfree_skb(skb);
 876                priv->stats.tx_dropped++;
 877                return 0;
 878        }
 879        if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
 880                mwifiex_dbg(priv->adapter, ERROR,
 881                            "Tx: bad skb len %d\n", skb->len);
 882                kfree_skb(skb);
 883                priv->stats.tx_dropped++;
 884                return 0;
 885        }
 886        if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
 887                mwifiex_dbg(priv->adapter, DATA,
 888                            "data: Tx: insufficient skb headroom %d\n",
 889                            skb_headroom(skb));
 890                /* Insufficient skb headroom - allocate a new skb */
 891                new_skb =
 892                        skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
 893                if (unlikely(!new_skb)) {
 894                        mwifiex_dbg(priv->adapter, ERROR,
 895                                    "Tx: cannot alloca new_skb\n");
 896                        kfree_skb(skb);
 897                        priv->stats.tx_dropped++;
 898                        return 0;
 899                }
 900                kfree_skb(skb);
 901                skb = new_skb;
 902                mwifiex_dbg(priv->adapter, INFO,
 903                            "info: new skb headroomd %d\n",
 904                            skb_headroom(skb));
 905        }
 906
 907        tx_info = MWIFIEX_SKB_TXCB(skb);
 908        memset(tx_info, 0, sizeof(*tx_info));
 909        tx_info->bss_num = priv->bss_num;
 910        tx_info->bss_type = priv->bss_type;
 911        tx_info->pkt_len = skb->len;
 912
 913        multicast = is_multicast_ether_addr(skb->data);
 914
 915        if (unlikely(!multicast && skb->sk &&
 916                     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
 917                     priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
 918                skb = mwifiex_clone_skb_for_tx_status(priv,
 919                                                      skb,
 920                                        MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
 921
 922        /* Record the current time the packet was queued; used to
 923         * determine the amount of time the packet was queued in
 924         * the driver before it was sent to the firmware.
 925         * The delay is then sent along with the packet to the
 926         * firmware for aggregate delay calculation for stats and
 927         * MSDU lifetime expiry.
 928         */
 929        __net_timestamp(skb);
 930
 931        if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
 932            priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
 933            !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
 934                if (priv->adapter->auto_tdls && priv->check_tdls_tx)
 935                        mwifiex_tdls_check_tx(priv, skb);
 936        }
 937
 938        mwifiex_queue_tx_pkt(priv, skb);
 939
 940        return 0;
 941}
 942
 943int mwifiex_set_mac_address(struct mwifiex_private *priv,
 944                            struct net_device *dev)
 945{
 946        int ret;
 947        u64 mac_addr;
 948
 949        if (priv->bss_type != MWIFIEX_BSS_TYPE_P2P)
 950                goto done;
 951
 952        mac_addr = ether_addr_to_u64(priv->curr_addr);
 953        mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
 954        u64_to_ether_addr(mac_addr, priv->curr_addr);
 955
 956        /* Send request to firmware */
 957        ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
 958                               HostCmd_ACT_GEN_SET, 0, NULL, true);
 959
 960        if (ret) {
 961                mwifiex_dbg(priv->adapter, ERROR,
 962                            "set mac address failed: ret=%d\n", ret);
 963                return ret;
 964        }
 965
 966done:
 967        memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
 968        return 0;
 969}
 970
 971/* CFG802.11 network device handler for setting MAC address.
 972 */
 973static int
 974mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
 975{
 976        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 977        struct sockaddr *hw_addr = addr;
 978
 979        memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
 980        return mwifiex_set_mac_address(priv, dev);
 981}
 982
 983/*
 984 * CFG802.11 network device handler for setting multicast list.
 985 */
 986static void mwifiex_set_multicast_list(struct net_device *dev)
 987{
 988        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
 989        struct mwifiex_multicast_list mcast_list;
 990
 991        if (dev->flags & IFF_PROMISC) {
 992                mcast_list.mode = MWIFIEX_PROMISC_MODE;
 993        } else if (dev->flags & IFF_ALLMULTI ||
 994                   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
 995                mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
 996        } else {
 997                mcast_list.mode = MWIFIEX_MULTICAST_MODE;
 998                mcast_list.num_multicast_addr =
 999                        mwifiex_copy_mcast_addr(&mcast_list, dev);
1000        }
1001        mwifiex_request_set_multicast_list(priv, &mcast_list);
1002}
1003
1004/*
1005 * CFG802.11 network device handler for transmission timeout.
1006 */
1007static void
1008mwifiex_tx_timeout(struct net_device *dev)
1009{
1010        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1011
1012        priv->num_tx_timeout++;
1013        priv->tx_timeout_cnt++;
1014        mwifiex_dbg(priv->adapter, ERROR,
1015                    "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1016                    jiffies, priv->tx_timeout_cnt, priv->bss_type,
1017                    priv->bss_num);
1018        mwifiex_set_trans_start(dev);
1019
1020        if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1021            priv->adapter->if_ops.card_reset) {
1022                mwifiex_dbg(priv->adapter, ERROR,
1023                            "tx_timeout_cnt exceeds threshold.\t"
1024                            "Triggering card reset!\n");
1025                priv->adapter->if_ops.card_reset(priv->adapter);
1026        }
1027}
1028
1029void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1030{
1031        struct usb_card_rec *card = adapter->card;
1032        struct mwifiex_private *priv;
1033        u16 tx_buf_size;
1034        int i, ret;
1035
1036        card->mc_resync_flag = true;
1037        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1038                if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1039                        mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1040                        return;
1041                }
1042        }
1043
1044        card->mc_resync_flag = false;
1045        tx_buf_size = 0xffff;
1046        priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1047        ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1048                               HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1049        if (ret)
1050                mwifiex_dbg(adapter, ERROR,
1051                            "send reconfig tx buf size cmd err\n");
1052}
1053EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1054
1055int mwifiex_drv_info_dump(struct mwifiex_adapter *adapter, void **drv_info)
1056{
1057        void *p;
1058        char drv_version[64];
1059        struct usb_card_rec *cardp;
1060        struct sdio_mmc_card *sdio_card;
1061        struct mwifiex_private *priv;
1062        int i, idx;
1063        struct netdev_queue *txq;
1064        struct mwifiex_debug_info *debug_info;
1065        void *drv_info_dump;
1066
1067        mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
1068
1069        /* memory allocate here should be free in mwifiex_upload_device_dump*/
1070        drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
1071
1072        if (!drv_info_dump)
1073                return 0;
1074
1075        p = (char *)(drv_info_dump);
1076        p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1077
1078        mwifiex_drv_get_driver_version(adapter, drv_version,
1079                                       sizeof(drv_version) - 1);
1080        p += sprintf(p, "driver_version = %s\n", drv_version);
1081
1082        if (adapter->iface_type == MWIFIEX_USB) {
1083                cardp = (struct usb_card_rec *)adapter->card;
1084                p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1085                             atomic_read(&cardp->tx_cmd_urb_pending));
1086                p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1087                             atomic_read(&cardp->port[0].tx_data_urb_pending));
1088                p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1089                             atomic_read(&cardp->port[1].tx_data_urb_pending));
1090                p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1091                             atomic_read(&cardp->rx_cmd_urb_pending));
1092                p += sprintf(p, "rx_data_urb_pending = %d\n",
1093                             atomic_read(&cardp->rx_data_urb_pending));
1094        }
1095
1096        p += sprintf(p, "tx_pending = %d\n",
1097                     atomic_read(&adapter->tx_pending));
1098        p += sprintf(p, "rx_pending = %d\n",
1099                     atomic_read(&adapter->rx_pending));
1100
1101        if (adapter->iface_type == MWIFIEX_SDIO) {
1102                sdio_card = (struct sdio_mmc_card *)adapter->card;
1103                p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1104                             sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1105                p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1106                             sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1107        }
1108
1109        for (i = 0; i < adapter->priv_num; i++) {
1110                if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1111                        continue;
1112                priv = adapter->priv[i];
1113                p += sprintf(p, "\n[interface  : \"%s\"]\n",
1114                             priv->netdev->name);
1115                p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1116                             atomic_read(&priv->wmm_tx_pending[0]));
1117                p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1118                             atomic_read(&priv->wmm_tx_pending[1]));
1119                p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1120                             atomic_read(&priv->wmm_tx_pending[2]));
1121                p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1122                             atomic_read(&priv->wmm_tx_pending[3]));
1123                p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1124                             "Disconnected" : "Connected");
1125                p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1126                             ? "on" : "off"));
1127                for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1128                        txq = netdev_get_tx_queue(priv->netdev, idx);
1129                        p += sprintf(p, "tx queue %d:%s  ", idx,
1130                                     netif_tx_queue_stopped(txq) ?
1131                                     "stopped" : "started");
1132                }
1133                p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1134                             priv->netdev->name, priv->num_tx_timeout);
1135        }
1136
1137        if (adapter->iface_type == MWIFIEX_SDIO ||
1138            adapter->iface_type == MWIFIEX_PCIE) {
1139                p += sprintf(p, "\n=== %s register dump===\n",
1140                             adapter->iface_type == MWIFIEX_SDIO ?
1141                                                        "SDIO" : "PCIE");
1142                if (adapter->if_ops.reg_dump)
1143                        p += adapter->if_ops.reg_dump(adapter, p);
1144        }
1145        p += sprintf(p, "\n=== more debug information\n");
1146        debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1147        if (debug_info) {
1148                for (i = 0; i < adapter->priv_num; i++) {
1149                        if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1150                                continue;
1151                        priv = adapter->priv[i];
1152                        mwifiex_get_debug_info(priv, debug_info);
1153                        p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1154                        break;
1155                }
1156                kfree(debug_info);
1157        }
1158
1159        mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1160        *drv_info = drv_info_dump;
1161        return p - drv_info_dump;
1162}
1163EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1164
1165void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter, void *drv_info,
1166                                int drv_info_size)
1167{
1168        u8 idx, *dump_data, *fw_dump_ptr;
1169        u32 dump_len;
1170
1171        dump_len = (strlen("========Start dump driverinfo========\n") +
1172                       drv_info_size +
1173                       strlen("\n========End dump========\n"));
1174
1175        for (idx = 0; idx < adapter->num_mem_types; idx++) {
1176                struct memory_type_mapping *entry =
1177                                &adapter->mem_type_mapping_tbl[idx];
1178
1179                if (entry->mem_ptr) {
1180                        dump_len += (strlen("========Start dump ") +
1181                                        strlen(entry->mem_name) +
1182                                        strlen("========\n") +
1183                                        (entry->mem_size + 1) +
1184                                        strlen("\n========End dump========\n"));
1185                }
1186        }
1187
1188        dump_data = vzalloc(dump_len + 1);
1189        if (!dump_data)
1190                goto done;
1191
1192        fw_dump_ptr = dump_data;
1193
1194        /* Dump all the memory data into single file, a userspace script will
1195         * be used to split all the memory data to multiple files
1196         */
1197        mwifiex_dbg(adapter, MSG,
1198                    "== mwifiex dump information to /sys/class/devcoredump start");
1199
1200        strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
1201        fw_dump_ptr += strlen("========Start dump driverinfo========\n");
1202        memcpy(fw_dump_ptr, drv_info, drv_info_size);
1203        fw_dump_ptr += drv_info_size;
1204        strcpy(fw_dump_ptr, "\n========End dump========\n");
1205        fw_dump_ptr += strlen("\n========End dump========\n");
1206
1207        for (idx = 0; idx < adapter->num_mem_types; idx++) {
1208                struct memory_type_mapping *entry =
1209                                        &adapter->mem_type_mapping_tbl[idx];
1210
1211                if (entry->mem_ptr) {
1212                        strcpy(fw_dump_ptr, "========Start dump ");
1213                        fw_dump_ptr += strlen("========Start dump ");
1214
1215                        strcpy(fw_dump_ptr, entry->mem_name);
1216                        fw_dump_ptr += strlen(entry->mem_name);
1217
1218                        strcpy(fw_dump_ptr, "========\n");
1219                        fw_dump_ptr += strlen("========\n");
1220
1221                        memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1222                        fw_dump_ptr += entry->mem_size;
1223
1224                        strcpy(fw_dump_ptr, "\n========End dump========\n");
1225                        fw_dump_ptr += strlen("\n========End dump========\n");
1226                }
1227        }
1228
1229        /* device dump data will be free in device coredump release function
1230         * after 5 min
1231         */
1232        dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
1233        mwifiex_dbg(adapter, MSG,
1234                    "== mwifiex dump information to /sys/class/devcoredump end");
1235
1236done:
1237        for (idx = 0; idx < adapter->num_mem_types; idx++) {
1238                struct memory_type_mapping *entry =
1239                        &adapter->mem_type_mapping_tbl[idx];
1240
1241                vfree(entry->mem_ptr);
1242                entry->mem_ptr = NULL;
1243                entry->mem_size = 0;
1244        }
1245
1246        vfree(drv_info);
1247}
1248EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1249
1250/*
1251 * CFG802.11 network device handler for statistics retrieval.
1252 */
1253static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1254{
1255        struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1256
1257        return &priv->stats;
1258}
1259
1260static u16
1261mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1262                                void *accel_priv, select_queue_fallback_t fallback)
1263{
1264        skb->priority = cfg80211_classify8021d(skb, NULL);
1265        return mwifiex_1d_to_wmm_queue[skb->priority];
1266}
1267
1268/* Network device handlers */
1269static const struct net_device_ops mwifiex_netdev_ops = {
1270        .ndo_open = mwifiex_open,
1271        .ndo_stop = mwifiex_close,
1272        .ndo_start_xmit = mwifiex_hard_start_xmit,
1273        .ndo_set_mac_address = mwifiex_ndo_set_mac_address,
1274        .ndo_validate_addr = eth_validate_addr,
1275        .ndo_tx_timeout = mwifiex_tx_timeout,
1276        .ndo_get_stats = mwifiex_get_stats,
1277        .ndo_set_rx_mode = mwifiex_set_multicast_list,
1278        .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1279};
1280
1281/*
1282 * This function initializes the private structure parameters.
1283 *
1284 * The following wait queues are initialized -
1285 *      - IOCTL wait queue
1286 *      - Command wait queue
1287 *      - Statistics wait queue
1288 *
1289 * ...and the following default parameters are set -
1290 *      - Current key index     : Set to 0
1291 *      - Rate index            : Set to auto
1292 *      - Media connected       : Set to disconnected
1293 *      - Adhoc link sensed     : Set to false
1294 *      - Nick name             : Set to null
1295 *      - Number of Tx timeout  : Set to 0
1296 *      - Device address        : Set to current address
1297 *      - Rx histogram statistc : Set to 0
1298 *
1299 * In addition, the CFG80211 work queue is also created.
1300 */
1301void mwifiex_init_priv_params(struct mwifiex_private *priv,
1302                              struct net_device *dev)
1303{
1304        dev->netdev_ops = &mwifiex_netdev_ops;
1305        dev->needs_free_netdev = true;
1306        /* Initialize private structure */
1307        priv->current_key_index = 0;
1308        priv->media_connected = false;
1309        memset(priv->mgmt_ie, 0,
1310               sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1311        priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1312        priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1313        priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1314        priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1315        priv->num_tx_timeout = 0;
1316        ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1317
1318        if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1319            GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1320                priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1321                if (priv->hist_data)
1322                        mwifiex_hist_data_reset(priv);
1323        }
1324}
1325
1326/*
1327 * This function check if command is pending.
1328 */
1329int is_command_pending(struct mwifiex_adapter *adapter)
1330{
1331        unsigned long flags;
1332        int is_cmd_pend_q_empty;
1333
1334        spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1335        is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1336        spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1337
1338        return !is_cmd_pend_q_empty;
1339}
1340
1341/*
1342 * This is the RX work queue function.
1343 *
1344 * It handles the RX operations.
1345 */
1346static void mwifiex_rx_work_queue(struct work_struct *work)
1347{
1348        struct mwifiex_adapter *adapter =
1349                container_of(work, struct mwifiex_adapter, rx_work);
1350
1351        if (adapter->surprise_removed)
1352                return;
1353        mwifiex_process_rx(adapter);
1354}
1355
1356/*
1357 * This is the main work queue function.
1358 *
1359 * It handles the main process, which in turn handles the complete
1360 * driver operations.
1361 */
1362static void mwifiex_main_work_queue(struct work_struct *work)
1363{
1364        struct mwifiex_adapter *adapter =
1365                container_of(work, struct mwifiex_adapter, main_work);
1366
1367        if (adapter->surprise_removed)
1368                return;
1369        mwifiex_main_process(adapter);
1370}
1371
1372/* Common teardown code used for both device removal and reset */
1373static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
1374{
1375        struct mwifiex_private *priv;
1376        int i;
1377
1378        /* We can no longer handle interrupts once we start doing the teardown
1379         * below.
1380         */
1381        if (adapter->if_ops.disable_int)
1382                adapter->if_ops.disable_int(adapter);
1383
1384        adapter->surprise_removed = true;
1385        mwifiex_terminate_workqueue(adapter);
1386        adapter->int_status = 0;
1387
1388        /* Stop data */
1389        for (i = 0; i < adapter->priv_num; i++) {
1390                priv = adapter->priv[i];
1391                if (priv && priv->netdev) {
1392                        mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1393                        if (netif_carrier_ok(priv->netdev))
1394                                netif_carrier_off(priv->netdev);
1395                        netif_device_detach(priv->netdev);
1396                }
1397        }
1398
1399        mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
1400        mwifiex_shutdown_drv(adapter);
1401        mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1402
1403        if (atomic_read(&adapter->rx_pending) ||
1404            atomic_read(&adapter->tx_pending) ||
1405            atomic_read(&adapter->cmd_pending)) {
1406                mwifiex_dbg(adapter, ERROR,
1407                            "rx_pending=%d, tx_pending=%d,\t"
1408                            "cmd_pending=%d\n",
1409                            atomic_read(&adapter->rx_pending),
1410                            atomic_read(&adapter->tx_pending),
1411                            atomic_read(&adapter->cmd_pending));
1412        }
1413
1414        for (i = 0; i < adapter->priv_num; i++) {
1415                priv = adapter->priv[i];
1416                if (!priv)
1417                        continue;
1418                rtnl_lock();
1419                if (priv->netdev &&
1420                    priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1421                        mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1422                rtnl_unlock();
1423        }
1424
1425        wiphy_unregister(adapter->wiphy);
1426        wiphy_free(adapter->wiphy);
1427        adapter->wiphy = NULL;
1428
1429        vfree(adapter->chan_stats);
1430        mwifiex_free_cmd_buffers(adapter);
1431}
1432
1433/*
1434 * This function gets called during PCIe function level reset.
1435 */
1436int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
1437{
1438        struct mwifiex_private *priv;
1439
1440        if (!adapter)
1441                return 0;
1442
1443        wait_for_completion(adapter->fw_done);
1444        /* Caller should ensure we aren't suspending while this happens */
1445        reinit_completion(adapter->fw_done);
1446
1447        priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1448        mwifiex_deauthenticate(priv, NULL);
1449
1450        mwifiex_uninit_sw(adapter);
1451
1452        if (adapter->if_ops.down_dev)
1453                adapter->if_ops.down_dev(adapter);
1454
1455        return 0;
1456}
1457EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
1458
1459/* This function gets called during PCIe function level reset. Required
1460 * code is extracted from mwifiex_add_card()
1461 */
1462int
1463mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
1464{
1465        int ret;
1466
1467        mwifiex_init_lock_list(adapter);
1468        if (adapter->if_ops.up_dev)
1469                adapter->if_ops.up_dev(adapter);
1470
1471        adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1472        adapter->surprise_removed = false;
1473        init_waitqueue_head(&adapter->init_wait_q);
1474        adapter->is_suspended = false;
1475        adapter->hs_activated = false;
1476        adapter->is_cmd_timedout = 0;
1477        init_waitqueue_head(&adapter->hs_activate_wait_q);
1478        init_waitqueue_head(&adapter->cmd_wait_q.wait);
1479        adapter->cmd_wait_q.status = 0;
1480        adapter->scan_wait_q_woken = false;
1481
1482        if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1483                adapter->rx_work_enabled = true;
1484
1485        adapter->workqueue =
1486                alloc_workqueue("MWIFIEX_WORK_QUEUE",
1487                                WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1488        if (!adapter->workqueue)
1489                goto err_kmalloc;
1490
1491        INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1492
1493        if (adapter->rx_work_enabled) {
1494                adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1495                                                        WQ_HIGHPRI |
1496                                                        WQ_MEM_RECLAIM |
1497                                                        WQ_UNBOUND, 1);
1498                if (!adapter->rx_workqueue)
1499                        goto err_kmalloc;
1500                INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1501        }
1502
1503        /* Register the device. Fill up the private data structure with
1504         * relevant information from the card. Some code extracted from
1505         * mwifiex_register_dev()
1506         */
1507        mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
1508
1509        if (mwifiex_init_hw_fw(adapter, false)) {
1510                mwifiex_dbg(adapter, ERROR,
1511                            "%s: firmware init failed\n", __func__);
1512                goto err_init_fw;
1513        }
1514
1515        /* _mwifiex_fw_dpc() does its own cleanup */
1516        ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1517        if (ret) {
1518                pr_err("Failed to bring up adapter: %d\n", ret);
1519                return ret;
1520        }
1521        mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
1522
1523        return 0;
1524
1525err_init_fw:
1526        mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1527        if (adapter->if_ops.unregister_dev)
1528                adapter->if_ops.unregister_dev(adapter);
1529
1530err_kmalloc:
1531        adapter->surprise_removed = true;
1532        mwifiex_terminate_workqueue(adapter);
1533        if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1534                mwifiex_dbg(adapter, ERROR,
1535                            "info: %s: shutdown mwifiex\n", __func__);
1536                mwifiex_shutdown_drv(adapter);
1537                mwifiex_free_cmd_buffers(adapter);
1538        }
1539
1540        complete_all(adapter->fw_done);
1541        mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1542
1543        return -1;
1544}
1545EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
1546
1547static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1548{
1549        struct mwifiex_adapter *adapter = priv;
1550
1551        dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1552        adapter->wake_by_wifi = true;
1553        disable_irq_nosync(irq);
1554
1555        /* Notify PM core we are wakeup source */
1556        pm_wakeup_event(adapter->dev, 0);
1557        pm_system_wakeup();
1558
1559        return IRQ_HANDLED;
1560}
1561
1562static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1563{
1564        int ret;
1565        struct device *dev = adapter->dev;
1566
1567        if (!dev->of_node)
1568                goto err_exit;
1569
1570        adapter->dt_node = dev->of_node;
1571        adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1572        if (!adapter->irq_wakeup) {
1573                dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1574                goto err_exit;
1575        }
1576
1577        ret = devm_request_irq(dev, adapter->irq_wakeup,
1578                               mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1579                               "wifi_wake", adapter);
1580        if (ret) {
1581                dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1582                        adapter->irq_wakeup, ret);
1583                goto err_exit;
1584        }
1585
1586        disable_irq(adapter->irq_wakeup);
1587        if (device_init_wakeup(dev, true)) {
1588                dev_err(dev, "fail to init wakeup for mwifiex\n");
1589                goto err_exit;
1590        }
1591        return;
1592
1593err_exit:
1594        adapter->irq_wakeup = -1;
1595}
1596
1597/*
1598 * This function adds the card.
1599 *
1600 * This function follows the following major steps to set up the device -
1601 *      - Initialize software. This includes probing the card, registering
1602 *        the interface operations table, and allocating/initializing the
1603 *        adapter structure
1604 *      - Set up the netlink socket
1605 *      - Create and start the main work queue
1606 *      - Register the device
1607 *      - Initialize firmware and hardware
1608 *      - Add logical interfaces
1609 */
1610int
1611mwifiex_add_card(void *card, struct completion *fw_done,
1612                 struct mwifiex_if_ops *if_ops, u8 iface_type,
1613                 struct device *dev)
1614{
1615        struct mwifiex_adapter *adapter;
1616
1617        if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
1618                pr_err("%s: software init failed\n", __func__);
1619                goto err_init_sw;
1620        }
1621
1622        mwifiex_probe_of(adapter);
1623
1624        adapter->iface_type = iface_type;
1625        adapter->fw_done = fw_done;
1626
1627        adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1628        adapter->surprise_removed = false;
1629        init_waitqueue_head(&adapter->init_wait_q);
1630        adapter->is_suspended = false;
1631        adapter->hs_activated = false;
1632        init_waitqueue_head(&adapter->hs_activate_wait_q);
1633        init_waitqueue_head(&adapter->cmd_wait_q.wait);
1634        adapter->cmd_wait_q.status = 0;
1635        adapter->scan_wait_q_woken = false;
1636
1637        if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1638                adapter->rx_work_enabled = true;
1639
1640        adapter->workqueue =
1641                alloc_workqueue("MWIFIEX_WORK_QUEUE",
1642                                WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1643        if (!adapter->workqueue)
1644                goto err_kmalloc;
1645
1646        INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1647
1648        if (adapter->rx_work_enabled) {
1649                adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1650                                                        WQ_HIGHPRI |
1651                                                        WQ_MEM_RECLAIM |
1652                                                        WQ_UNBOUND, 1);
1653                if (!adapter->rx_workqueue)
1654                        goto err_kmalloc;
1655
1656                INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1657        }
1658
1659        /* Register the device. Fill up the private data structure with relevant
1660           information from the card. */
1661        if (adapter->if_ops.register_dev(adapter)) {
1662                pr_err("%s: failed to register mwifiex device\n", __func__);
1663                goto err_registerdev;
1664        }
1665
1666        if (mwifiex_init_hw_fw(adapter, true)) {
1667                pr_err("%s: firmware init failed\n", __func__);
1668                goto err_init_fw;
1669        }
1670
1671        return 0;
1672
1673err_init_fw:
1674        pr_debug("info: %s: unregister device\n", __func__);
1675        if (adapter->if_ops.unregister_dev)
1676                adapter->if_ops.unregister_dev(adapter);
1677err_registerdev:
1678        adapter->surprise_removed = true;
1679        mwifiex_terminate_workqueue(adapter);
1680        if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1681                pr_debug("info: %s: shutdown mwifiex\n", __func__);
1682                mwifiex_shutdown_drv(adapter);
1683                mwifiex_free_cmd_buffers(adapter);
1684        }
1685err_kmalloc:
1686        if (adapter->irq_wakeup >= 0)
1687                device_init_wakeup(adapter->dev, false);
1688        mwifiex_free_adapter(adapter);
1689
1690err_init_sw:
1691
1692        return -1;
1693}
1694EXPORT_SYMBOL_GPL(mwifiex_add_card);
1695
1696/*
1697 * This function removes the card.
1698 *
1699 * This function follows the following major steps to remove the device -
1700 *      - Stop data traffic
1701 *      - Shutdown firmware
1702 *      - Remove the logical interfaces
1703 *      - Terminate the work queue
1704 *      - Unregister the device
1705 *      - Free the adapter structure
1706 */
1707int mwifiex_remove_card(struct mwifiex_adapter *adapter)
1708{
1709        if (!adapter)
1710                return 0;
1711
1712        mwifiex_uninit_sw(adapter);
1713
1714        if (adapter->irq_wakeup >= 0)
1715                device_init_wakeup(adapter->dev, false);
1716
1717        /* Unregister device */
1718        mwifiex_dbg(adapter, INFO,
1719                    "info: unregister device\n");
1720        if (adapter->if_ops.unregister_dev)
1721                adapter->if_ops.unregister_dev(adapter);
1722        /* Free adapter structure */
1723        mwifiex_dbg(adapter, INFO,
1724                    "info: free adapter\n");
1725        mwifiex_free_adapter(adapter);
1726
1727        return 0;
1728}
1729EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1730
1731void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1732                  const char *fmt, ...)
1733{
1734        struct va_format vaf;
1735        va_list args;
1736
1737        if (!(adapter->debug_mask & mask))
1738                return;
1739
1740        va_start(args, fmt);
1741
1742        vaf.fmt = fmt;
1743        vaf.va = &args;
1744
1745        if (adapter->dev)
1746                dev_info(adapter->dev, "%pV", &vaf);
1747        else
1748                pr_info("%pV", &vaf);
1749
1750        va_end(args);
1751}
1752EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1753
1754/*
1755 * This function initializes the module.
1756 *
1757 * The debug FS is also initialized if configured.
1758 */
1759static int
1760mwifiex_init_module(void)
1761{
1762#ifdef CONFIG_DEBUG_FS
1763        mwifiex_debugfs_init();
1764#endif
1765        return 0;
1766}
1767
1768/*
1769 * This function cleans up the module.
1770 *
1771 * The debug FS is removed if available.
1772 */
1773static void
1774mwifiex_cleanup_module(void)
1775{
1776#ifdef CONFIG_DEBUG_FS
1777        mwifiex_debugfs_remove();
1778#endif
1779}
1780
1781module_init(mwifiex_init_module);
1782module_exit(mwifiex_cleanup_module);
1783
1784MODULE_AUTHOR("Marvell International Ltd.");
1785MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1786MODULE_VERSION(VERSION);
1787MODULE_LICENSE("GPL v2");
1788