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