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