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