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