mwifiex: add support for FW memory read/write operations
[linux-2.6-block.git] / drivers / net / wireless / 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"
26
27const char driver_version[] = "mwifiex " VERSION " (%s) ";
388ec385
AK
28static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
5e6e3a92 30
0013c7ce
AP
31static unsigned short driver_mode;
32module_param(driver_mode, ushort, 0);
33MODULE_PARM_DESC(driver_mode,
34 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
35
5e6e3a92
BZ
36/*
37 * This function registers the device and performs all the necessary
38 * initializations.
39 *
40 * The following initialization operations are performed -
41 * - Allocate adapter structure
42 * - Save interface specific operations table in adapter
43 * - Call interface specific initialization routine
44 * - Allocate private structures
45 * - Set default adapter structure parameters
46 * - Initialize locks
47 *
48 * In case of any errors during inittialization, this function also ensures
49 * proper cleanup before exiting.
50 */
51static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
287546df 52 void **padapter)
5e6e3a92 53{
2be7859f
AK
54 struct mwifiex_adapter *adapter;
55 int i;
5e6e3a92
BZ
56
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
5e6e3a92 58 if (!adapter)
b53575ec 59 return -ENOMEM;
5e6e3a92 60
287546df 61 *padapter = adapter;
5e6e3a92
BZ
62 adapter->card = card;
63
64 /* Save interface specific operations in adapter */
65 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
66
67 /* card specific initialization has been deferred until now .. */
4daffe35
AK
68 if (adapter->if_ops.init_if)
69 if (adapter->if_ops.init_if(adapter))
70 goto error;
5e6e3a92
BZ
71
72 adapter->priv_num = 0;
5e6e3a92 73
64b05e2f
AP
74 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
75 /* Allocate memory for private structure */
76 adapter->priv[i] =
77 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
78 if (!adapter->priv[i])
79 goto error;
93a1df48 80
64b05e2f 81 adapter->priv[i]->adapter = adapter;
64b05e2f
AP
82 adapter->priv_num++;
83 }
44b815c6 84 mwifiex_init_lock_list(adapter);
5e6e3a92 85
c6c33e77
JL
86 setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
87 (unsigned long)adapter);
5e6e3a92 88
5e6e3a92
BZ
89 return 0;
90
91error:
92 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
93
93a1df48 94 for (i = 0; i < adapter->priv_num; i++)
5e6e3a92 95 kfree(adapter->priv[i]);
93a1df48 96
5e6e3a92
BZ
97 kfree(adapter);
98
99 return -1;
100}
101
102/*
103 * This function unregisters the device and performs all the necessary
104 * cleanups.
105 *
106 * The following cleanup operations are performed -
107 * - Free the timers
108 * - Free beacon buffers
109 * - Free private structures
110 * - Free adapter structure
111 */
112static int mwifiex_unregister(struct mwifiex_adapter *adapter)
113{
270e58e8 114 s32 i;
5e6e3a92 115
4cfda67d
AK
116 if (adapter->if_ops.cleanup_if)
117 adapter->if_ops.cleanup_if(adapter);
118
629873f2 119 del_timer_sync(&adapter->cmd_timer);
5e6e3a92
BZ
120
121 /* Free private structures */
122 for (i = 0; i < adapter->priv_num; i++) {
123 if (adapter->priv[i]) {
124 mwifiex_free_curr_bcn(adapter->priv[i]);
125 kfree(adapter->priv[i]);
126 }
127 }
128
bf354433 129 vfree(adapter->chan_stats);
5e6e3a92
BZ
130 kfree(adapter);
131 return 0;
132}
133
b2713f67
SL
134void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
135{
136 unsigned long flags;
137
138 spin_lock_irqsave(&adapter->main_proc_lock, flags);
139 if (adapter->mwifiex_processing) {
140 adapter->more_task_flag = true;
141 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
142 } else {
143 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
144 queue_work(adapter->workqueue, &adapter->main_work);
145 }
146}
147EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
148
149static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
150{
151 unsigned long flags;
152
153 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
154 if (adapter->rx_processing) {
155 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
156 } else {
157 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
158 queue_work(adapter->rx_workqueue, &adapter->rx_work);
159 }
160}
161
6e251174
AP
162static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
163{
164 unsigned long flags;
165 struct sk_buff *skb;
92263a84 166 struct mwifiex_rxinfo *rx_info;
6e251174
AP
167
168 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
169 if (adapter->rx_processing || adapter->rx_locked) {
170 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
171 goto exit_rx_proc;
172 } else {
173 adapter->rx_processing = true;
174 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
175 }
176
177 /* Check for Rx data */
178 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
179 atomic_dec(&adapter->rx_pending);
381e9fff
AK
180 if ((adapter->delay_main_work ||
181 adapter->iface_type == MWIFIEX_USB) &&
b43a0d9d 182 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
cf6a64fd
AK
183 if (adapter->if_ops.submit_rem_rx_urbs)
184 adapter->if_ops.submit_rem_rx_urbs(adapter);
6e251174 185 adapter->delay_main_work = false;
b2713f67 186 mwifiex_queue_main_work(adapter);
6e251174 187 }
92263a84
ZL
188 rx_info = MWIFIEX_SKB_RXCB(skb);
189 if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
190 if (adapter->if_ops.deaggr_pkt)
191 adapter->if_ops.deaggr_pkt(adapter, skb);
192 dev_kfree_skb_any(skb);
193 } else {
194 mwifiex_handle_rx_packet(adapter, skb);
6e251174 195 }
6e251174
AP
196 }
197 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
198 adapter->rx_processing = false;
199 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
200
6e251174
AP
201exit_rx_proc:
202 return 0;
203}
204
5e6e3a92
BZ
205/*
206 * The main process.
207 *
208 * This function is the main procedure of the driver and handles various driver
209 * operations. It runs in a loop and provides the core functionalities.
210 *
211 * The main responsibilities of this function are -
212 * - Ensure concurrency control
213 * - Handle pending interrupts and call interrupt handlers
214 * - Wake up the card if required
215 * - Handle command responses and call response handlers
216 * - Handle events and call event handlers
217 * - Execute pending commands
218 * - Transmit pending data packets
219 */
220int mwifiex_main_process(struct mwifiex_adapter *adapter)
221{
222 int ret = 0;
223 unsigned long flags;
224
225 spin_lock_irqsave(&adapter->main_proc_lock, flags);
226
227 /* Check if already processing */
a9adbcb3 228 if (adapter->mwifiex_processing || adapter->main_locked) {
04c7b363 229 adapter->more_task_flag = true;
5e6e3a92
BZ
230 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
231 goto exit_main_proc;
232 } else {
233 adapter->mwifiex_processing = true;
91457eaa 234 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
5e6e3a92
BZ
235 }
236process_start:
237 do {
238 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
239 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
240 break;
241
381e9fff
AK
242 /* For non-USB interfaces, If we process interrupts first, it
243 * would increase RX pending even further. Avoid this by
244 * checking if rx_pending has crossed high threshold and
245 * schedule rx work queue and then process interrupts.
246 * For USB interface, there are no interrupts. We already have
247 * HIGH_RX_PENDING check in usb.c
6e251174 248 */
381e9fff
AK
249 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
250 adapter->iface_type != MWIFIEX_USB) {
6e251174 251 adapter->delay_main_work = true;
b2713f67 252 mwifiex_queue_rx_work(adapter);
6e251174
AP
253 break;
254 }
255
5e6e3a92
BZ
256 /* Handle pending interrupt if any */
257 if (adapter->int_status) {
258 if (adapter->hs_activated)
259 mwifiex_process_hs_config(adapter);
4daffe35
AK
260 if (adapter->if_ops.process_int_status)
261 adapter->if_ops.process_int_status(adapter);
5e6e3a92
BZ
262 }
263
6e251174 264 if (adapter->rx_work_enabled && adapter->data_received)
b2713f67 265 mwifiex_queue_rx_work(adapter);
6e251174 266
5e6e3a92
BZ
267 /* Need to wake up the card ? */
268 if ((adapter->ps_state == PS_STATE_SLEEP) &&
269 (adapter->pm_wakeup_card_req &&
270 !adapter->pm_wakeup_fw_try) &&
f57c1edc 271 (is_command_pending(adapter) ||
e35000ea 272 !skb_queue_empty(&adapter->tx_data_q) ||
f57c1edc 273 !mwifiex_wmm_lists_empty(adapter))) {
5e6e3a92 274 adapter->pm_wakeup_fw_try = true;
4636187d 275 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
5e6e3a92
BZ
276 adapter->if_ops.wakeup(adapter);
277 continue;
278 }
4daffe35 279
5e6e3a92 280 if (IS_CARD_RX_RCVD(adapter)) {
6e251174 281 adapter->data_received = false;
5e6e3a92 282 adapter->pm_wakeup_fw_try = false;
6e9344fd 283 del_timer(&adapter->wakeup_timer);
5e6e3a92
BZ
284 if (adapter->ps_state == PS_STATE_SLEEP)
285 adapter->ps_state = PS_STATE_AWAKE;
286 } else {
287 /* We have tried to wakeup the card already */
288 if (adapter->pm_wakeup_fw_try)
289 break;
290 if (adapter->ps_state != PS_STATE_AWAKE ||
291 adapter->tx_lock_flag)
292 break;
293
5ec39efa 294 if ((!adapter->scan_chan_gap_enabled &&
5ec39efa 295 adapter->scan_processing) || adapter->data_sent ||
e35000ea
ZL
296 (mwifiex_wmm_lists_empty(adapter) &&
297 skb_queue_empty(&adapter->tx_data_q))) {
f57c1edc
YAP
298 if (adapter->cmd_sent || adapter->curr_cmd ||
299 (!is_command_pending(adapter)))
5e6e3a92
BZ
300 break;
301 }
302 }
303
20474129
AK
304 /* Check for event */
305 if (adapter->event_received) {
306 adapter->event_received = false;
307 mwifiex_process_event(adapter);
308 }
309
5e6e3a92
BZ
310 /* Check for Cmd Resp */
311 if (adapter->cmd_resp_received) {
312 adapter->cmd_resp_received = false;
313 mwifiex_process_cmdresp(adapter);
314
315 /* call mwifiex back when init_fw is done */
316 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
317 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
318 mwifiex_init_fw_complete(adapter);
319 }
320 }
321
5e6e3a92
BZ
322 /* Check if we need to confirm Sleep Request
323 received previously */
324 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
325 if (!adapter->cmd_sent && !adapter->curr_cmd)
326 mwifiex_check_ps_cond(adapter);
327 }
328
329 /* * The ps_state may have been changed during processing of
330 * Sleep Request event.
331 */
f57c1edc
YAP
332 if ((adapter->ps_state == PS_STATE_SLEEP) ||
333 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
334 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
04c7b363 335 adapter->tx_lock_flag){
5e6e3a92 336 continue;
04c7b363 337 }
5e6e3a92
BZ
338
339 if (!adapter->cmd_sent && !adapter->curr_cmd) {
340 if (mwifiex_exec_next_cmd(adapter) == -1) {
341 ret = -1;
342 break;
343 }
344 }
345
e35000ea
ZL
346 if ((adapter->scan_chan_gap_enabled ||
347 !adapter->scan_processing) &&
348 !adapter->data_sent &&
349 !skb_queue_empty(&adapter->tx_data_q)) {
350 mwifiex_process_tx_queue(adapter);
351 if (adapter->hs_activated) {
352 adapter->is_hs_configured = false;
353 mwifiex_hs_activated_event
354 (mwifiex_get_priv
355 (adapter, MWIFIEX_BSS_ROLE_ANY),
356 false);
357 }
358 }
359
5ec39efa 360 if ((adapter->scan_chan_gap_enabled ||
d8d91253 361 !adapter->scan_processing) &&
3249ba73 362 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
363 mwifiex_wmm_process_tx(adapter);
364 if (adapter->hs_activated) {
365 adapter->is_hs_configured = false;
366 mwifiex_hs_activated_event
367 (mwifiex_get_priv
368 (adapter, MWIFIEX_BSS_ROLE_ANY),
369 false);
370 }
371 }
372
373 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
f57c1edc 374 !adapter->curr_cmd && !is_command_pending(adapter) &&
e35000ea
ZL
375 (mwifiex_wmm_lists_empty(adapter) &&
376 skb_queue_empty(&adapter->tx_data_q))) {
5e6e3a92
BZ
377 if (!mwifiex_send_null_packet
378 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
379 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
380 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
381 adapter->delay_null_pkt = false;
382 adapter->ps_state = PS_STATE_SLEEP;
383 }
384 break;
385 }
386 } while (true);
387
453b0c3f 388 spin_lock_irqsave(&adapter->main_proc_lock, flags);
91457eaa
CL
389 if (adapter->more_task_flag) {
390 adapter->more_task_flag = false;
391 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
5e6e3a92 392 goto process_start;
91457eaa 393 }
5e6e3a92
BZ
394 adapter->mwifiex_processing = false;
395 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
396
397exit_main_proc:
398 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
399 mwifiex_shutdown_drv(adapter);
400 return ret;
401}
601216e1 402EXPORT_SYMBOL_GPL(mwifiex_main_process);
5e6e3a92 403
5e6e3a92
BZ
404/*
405 * This function frees the adapter structure.
406 *
407 * Additionally, this closes the netlink socket, frees the timers
408 * and private structures.
409 */
410static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
411{
412 if (!adapter) {
413 pr_err("%s: adapter is NULL\n", __func__);
414 return;
415 }
416
417 mwifiex_unregister(adapter);
418 pr_debug("info: %s: free adapter\n", __func__);
419}
420
6b41f941
AK
421/*
422 * This function cancels all works in the queue and destroys
423 * the main workqueue.
424 */
425static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
426{
427 flush_workqueue(adapter->workqueue);
428 destroy_workqueue(adapter->workqueue);
429 adapter->workqueue = NULL;
6e251174
AP
430
431 if (adapter->rx_workqueue) {
432 flush_workqueue(adapter->rx_workqueue);
433 destroy_workqueue(adapter->rx_workqueue);
434 adapter->rx_workqueue = NULL;
435 }
6b41f941
AK
436}
437
5e6e3a92 438/*
59a4cc25 439 * This function gets firmware and initializes it.
5e6e3a92
BZ
440 *
441 * The main initialization steps followed are -
442 * - Download the correct firmware to card
5e6e3a92
BZ
443 * - Issue the init commands to firmware
444 */
59a4cc25 445static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
5e6e3a92 446{
bec568ff 447 int ret;
59a4cc25
AK
448 char fmt[64];
449 struct mwifiex_private *priv;
450 struct mwifiex_adapter *adapter = context;
5e6e3a92 451 struct mwifiex_fw_image fw;
c3afd99f
AK
452 struct semaphore *sem = adapter->card_sem;
453 bool init_failed = false;
68b76e99 454 struct wireless_dev *wdev;
5e6e3a92 455
59a4cc25
AK
456 if (!firmware) {
457 dev_err(adapter->dev,
458 "Failed to get firmware %s\n", adapter->fw_name);
6b41f941 459 goto err_dnld_fw;
5e6e3a92 460 }
59a4cc25
AK
461
462 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
463 adapter->firmware = firmware;
5e6e3a92
BZ
464 fw.fw_buf = (u8 *) adapter->firmware->data;
465 fw.fw_len = adapter->firmware->size;
466
4daffe35
AK
467 if (adapter->if_ops.dnld_fw)
468 ret = adapter->if_ops.dnld_fw(adapter, &fw);
469 else
470 ret = mwifiex_dnld_fw(adapter, &fw);
5e6e3a92 471 if (ret == -1)
6b41f941 472 goto err_dnld_fw;
5e6e3a92
BZ
473
474 dev_notice(adapter->dev, "WLAN FW is active\n");
475
388ec385
AK
476 if (cal_data_cfg) {
477 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
478 adapter->dev)) < 0)
479 dev_err(adapter->dev,
480 "Cal data request_firmware() failed\n");
481 }
482
232fde06 483 /* enable host interrupt after fw dnld is successful */
6b41f941
AK
484 if (adapter->if_ops.enable_int) {
485 if (adapter->if_ops.enable_int(adapter))
486 goto err_dnld_fw;
487 }
232fde06 488
5e6e3a92
BZ
489 adapter->init_wait_q_woken = false;
490 ret = mwifiex_init_fw(adapter);
491 if (ret == -1) {
6b41f941 492 goto err_init_fw;
5e6e3a92
BZ
493 } else if (!ret) {
494 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
495 goto done;
496 }
497 /* Wait for mwifiex_init to complete */
498 wait_event_interruptible(adapter->init_wait_q,
499 adapter->init_wait_q_woken);
59a4cc25 500 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
6b41f941 501 goto err_init_fw;
59a4cc25 502
d6bffe8b
AP
503 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
504 if (mwifiex_register_cfg80211(adapter)) {
59a4cc25 505 dev_err(adapter->dev, "cannot register with cfg80211\n");
d1af2943 506 goto err_init_fw;
59a4cc25
AK
507 }
508
bf354433
AP
509 if (mwifiex_init_channel_scan_gap(adapter)) {
510 dev_err(adapter->dev, "could not init channel stats table\n");
511 goto err_init_fw;
512 }
513
0013c7ce
AP
514 if (driver_mode) {
515 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
516 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
517 }
518
59a4cc25
AK
519 rtnl_lock();
520 /* Create station interface by default */
6bab2e19 521 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
68b76e99
AK
522 NL80211_IFTYPE_STATION, NULL, NULL);
523 if (IS_ERR(wdev)) {
59a4cc25 524 dev_err(adapter->dev, "cannot create default STA interface\n");
bec568ff 525 rtnl_unlock();
59a4cc25 526 goto err_add_intf;
5e6e3a92 527 }
0013c7ce
AP
528
529 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
6bab2e19 530 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
0013c7ce
AP
531 NL80211_IFTYPE_AP, NULL, NULL);
532 if (IS_ERR(wdev)) {
533 dev_err(adapter->dev, "cannot create AP interface\n");
534 rtnl_unlock();
535 goto err_add_intf;
536 }
537 }
538
539 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
6bab2e19 540 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
0013c7ce
AP
541 NL80211_IFTYPE_P2P_CLIENT, NULL,
542 NULL);
543 if (IS_ERR(wdev)) {
544 dev_err(adapter->dev,
545 "cannot create p2p client interface\n");
546 rtnl_unlock();
547 goto err_add_intf;
548 }
549 }
59a4cc25
AK
550 rtnl_unlock();
551
552 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
553 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
554 goto done;
5e6e3a92 555
59a4cc25 556err_add_intf:
6b41f941
AK
557 wiphy_unregister(adapter->wiphy);
558 wiphy_free(adapter->wiphy);
59a4cc25 559err_init_fw:
232fde06
DD
560 if (adapter->if_ops.disable_int)
561 adapter->if_ops.disable_int(adapter);
6b41f941 562err_dnld_fw:
59a4cc25 563 pr_debug("info: %s: unregister device\n", __func__);
6b41f941
AK
564 if (adapter->if_ops.unregister_dev)
565 adapter->if_ops.unregister_dev(adapter);
566
7197325b 567 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
6b41f941
AK
568 pr_debug("info: %s: shutdown mwifiex\n", __func__);
569 adapter->init_wait_q_woken = false;
570
571 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
572 wait_event_interruptible(adapter->init_wait_q,
573 adapter->init_wait_q_woken);
574 }
575 adapter->surprise_removed = true;
576 mwifiex_terminate_workqueue(adapter);
c3afd99f 577 init_failed = true;
5e6e3a92 578done:
388ec385
AK
579 if (adapter->cal_data) {
580 release_firmware(adapter->cal_data);
581 adapter->cal_data = NULL;
582 }
c3afd99f
AK
583 if (adapter->firmware) {
584 release_firmware(adapter->firmware);
585 adapter->firmware = NULL;
586 }
c3afd99f
AK
587 if (init_failed)
588 mwifiex_free_adapter(adapter);
589 up(sem);
59a4cc25
AK
590 return;
591}
592
593/*
594 * This function initializes the hardware and gets firmware.
595 */
596static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
597{
598 int ret;
599
59a4cc25
AK
600 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
601 adapter->dev, GFP_KERNEL, adapter,
602 mwifiex_fw_dpc);
603 if (ret < 0)
604 dev_err(adapter->dev,
605 "request_firmware_nowait() returned error %d\n", ret);
5e6e3a92
BZ
606 return ret;
607}
608
5e6e3a92
BZ
609/*
610 * CFG802.11 network device handler for open.
611 *
612 * Starts the data queue.
613 */
614static int
615mwifiex_open(struct net_device *dev)
616{
ea2325b8
JB
617 netif_carrier_off(dev);
618
5e6e3a92
BZ
619 return 0;
620}
621
622/*
623 * CFG802.11 network device handler for close.
624 */
625static int
626mwifiex_close(struct net_device *dev)
627{
f162cac8
AK
628 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
629
630 if (priv->scan_request) {
631 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
632 cfg80211_scan_done(priv->scan_request, 1);
633 priv->scan_request = NULL;
75ab753d 634 priv->scan_aborting = true;
f162cac8
AK
635 }
636
5e6e3a92
BZ
637 return 0;
638}
639
e39faa73
SP
640/*
641 * Add buffer into wmm tx queue and queue work to transmit it.
642 */
643int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
644{
47411a06
AP
645 struct netdev_queue *txq;
646 int index = mwifiex_1d_to_wmm_queue[skb->priority];
647
648 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
649 txq = netdev_get_tx_queue(priv->netdev, index);
650 if (!netif_tx_queue_stopped(txq)) {
651 netif_tx_stop_queue(txq);
652 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
653 }
654 }
655
e39faa73 656 atomic_inc(&priv->adapter->tx_pending);
47411a06 657 mwifiex_wmm_add_buf_txqueue(priv, skb);
e39faa73 658
b2713f67 659 mwifiex_queue_main_work(priv->adapter);
e39faa73
SP
660
661 return 0;
662}
663
18ca4382 664struct sk_buff *
808bbebc 665mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
18ca4382 666 struct sk_buff *skb, u8 flag, u64 *cookie)
808bbebc
AK
667{
668 struct sk_buff *orig_skb = skb;
669 struct mwifiex_txinfo *tx_info, *orig_tx_info;
670
671 skb = skb_clone(skb, GFP_ATOMIC);
672 if (skb) {
673 unsigned long flags;
674 int id;
675
676 spin_lock_irqsave(&priv->ack_status_lock, flags);
677 id = idr_alloc(&priv->ack_status_frames, orig_skb,
678 1, 0xff, GFP_ATOMIC);
679 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
680
681 if (id >= 0) {
682 tx_info = MWIFIEX_SKB_TXCB(skb);
683 tx_info->ack_frame_id = id;
684 tx_info->flags |= flag;
685 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
686 orig_tx_info->ack_frame_id = id;
687 orig_tx_info->flags |= flag;
18ca4382
AK
688
689 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
690 orig_tx_info->cookie = *cookie;
691
808bbebc
AK
692 } else if (skb_shared(skb)) {
693 kfree_skb(orig_skb);
694 } else {
695 kfree_skb(skb);
696 skb = orig_skb;
697 }
698 } else {
699 /* couldn't clone -- lose tx status ... */
700 skb = orig_skb;
701 }
702
703 return skb;
704}
705
5e6e3a92
BZ
706/*
707 * CFG802.11 network device handler for data transmission.
708 */
709static int
710mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
711{
712 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
270e58e8 713 struct sk_buff *new_skb;
5e6e3a92 714 struct mwifiex_txinfo *tx_info;
808bbebc 715 bool multicast;
5e6e3a92 716
9da9a3b2 717 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
f57c1edc 718 jiffies, priv->bss_type, priv->bss_num);
5e6e3a92
BZ
719
720 if (priv->adapter->surprise_removed) {
b53575ec 721 kfree_skb(skb);
5e6e3a92
BZ
722 priv->stats.tx_dropped++;
723 return 0;
724 }
725 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
726 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
b53575ec 727 kfree_skb(skb);
5e6e3a92
BZ
728 priv->stats.tx_dropped++;
729 return 0;
730 }
731 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
732 dev_dbg(priv->adapter->dev,
733 "data: Tx: insufficient skb headroom %d\n",
f57c1edc 734 skb_headroom(skb));
5e6e3a92
BZ
735 /* Insufficient skb headroom - allocate a new skb */
736 new_skb =
737 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
738 if (unlikely(!new_skb)) {
739 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
b53575ec 740 kfree_skb(skb);
5e6e3a92
BZ
741 priv->stats.tx_dropped++;
742 return 0;
743 }
744 kfree_skb(skb);
745 skb = new_skb;
746 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
f57c1edc 747 skb_headroom(skb));
5e6e3a92
BZ
748 }
749
750 tx_info = MWIFIEX_SKB_TXCB(skb);
d76744a9 751 memset(tx_info, 0, sizeof(*tx_info));
9da9a3b2
YAP
752 tx_info->bss_num = priv->bss_num;
753 tx_info->bss_type = priv->bss_type;
a1ed4849 754 tx_info->pkt_len = skb->len;
47411a06 755
808bbebc
AK
756 multicast = is_multicast_ether_addr(skb->data);
757
758 if (unlikely(!multicast && skb->sk &&
759 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
760 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
761 skb = mwifiex_clone_skb_for_tx_status(priv,
762 skb,
18ca4382 763 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
808bbebc 764
47411a06
AP
765 /* Record the current time the packet was queued; used to
766 * determine the amount of time the packet was queued in
767 * the driver before it was sent to the firmware.
768 * The delay is then sent along with the packet to the
769 * firmware for aggregate delay calculation for stats and
770 * MSDU lifetime expiry.
771 */
c64800e7 772 __net_timestamp(skb);
5e6e3a92 773
9927baa3
AP
774 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
775 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
776 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
777 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
778 mwifiex_tdls_check_tx(priv, skb);
779 }
780
e39faa73 781 mwifiex_queue_tx_pkt(priv, skb);
5e6e3a92
BZ
782
783 return 0;
784}
785
786/*
787 * CFG802.11 network device handler for setting MAC address.
788 */
789static int
790mwifiex_set_mac_address(struct net_device *dev, void *addr)
791{
792 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
a5ffddb7 793 struct sockaddr *hw_addr = addr;
270e58e8 794 int ret;
5e6e3a92
BZ
795
796 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
797
600f5d90 798 /* Send request to firmware */
fa0ecbb9
BZ
799 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
800 HostCmd_ACT_GEN_SET, 0, NULL, true);
600f5d90
AK
801
802 if (!ret)
803 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
804 else
f57c1edc
YAP
805 dev_err(priv->adapter->dev,
806 "set mac address failed: ret=%d\n", ret);
600f5d90 807
5e6e3a92
BZ
808 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
809
600f5d90 810 return ret;
5e6e3a92
BZ
811}
812
813/*
814 * CFG802.11 network device handler for setting multicast list.
815 */
816static void mwifiex_set_multicast_list(struct net_device *dev)
817{
818 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
600f5d90
AK
819 struct mwifiex_multicast_list mcast_list;
820
821 if (dev->flags & IFF_PROMISC) {
822 mcast_list.mode = MWIFIEX_PROMISC_MODE;
823 } else if (dev->flags & IFF_ALLMULTI ||
824 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
825 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
826 } else {
827 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
6390d885
DD
828 mcast_list.num_multicast_addr =
829 mwifiex_copy_mcast_addr(&mcast_list, dev);
600f5d90
AK
830 }
831 mwifiex_request_set_multicast_list(priv, &mcast_list);
5e6e3a92
BZ
832}
833
834/*
835 * CFG802.11 network device handler for transmission timeout.
836 */
837static void
838mwifiex_tx_timeout(struct net_device *dev)
839{
840 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
841
5e6e3a92 842 priv->num_tx_timeout++;
8908c7d5
AN
843 priv->tx_timeout_cnt++;
844 dev_err(priv->adapter->dev,
845 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
846 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
847 mwifiex_set_trans_start(dev);
848
849 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
850 priv->adapter->if_ops.card_reset) {
851 dev_err(priv->adapter->dev,
852 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
853 priv->adapter->if_ops.card_reset(priv->adapter);
854 }
5e6e3a92
BZ
855}
856
11cd07a9
XH
857void mwifiex_dump_drv_info(struct mwifiex_adapter *adapter)
858{
859 void *p;
860 char drv_version[64];
861 struct usb_card_rec *cardp;
862 struct sdio_mmc_card *sdio_card;
863 struct mwifiex_private *priv;
864 int i, idx;
865 struct netdev_queue *txq;
866 struct mwifiex_debug_info *debug_info;
867
868 if (adapter->drv_info_dump) {
869 vfree(adapter->drv_info_dump);
870 adapter->drv_info_size = 0;
871 }
872
873 dev_info(adapter->dev, "=== DRIVER INFO DUMP START===\n");
874
875 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
876
877 if (!adapter->drv_info_dump)
878 return;
879
880 p = (char *)(adapter->drv_info_dump);
881 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
882
883 mwifiex_drv_get_driver_version(adapter, drv_version,
884 sizeof(drv_version) - 1);
885 p += sprintf(p, "driver_version = %s\n", drv_version);
886
887 if (adapter->iface_type == MWIFIEX_USB) {
888 cardp = (struct usb_card_rec *)adapter->card;
889 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
890 atomic_read(&cardp->tx_cmd_urb_pending));
891 p += sprintf(p, "tx_data_urb_pending = %d\n",
892 atomic_read(&cardp->tx_data_urb_pending));
893 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
894 atomic_read(&cardp->rx_cmd_urb_pending));
895 p += sprintf(p, "rx_data_urb_pending = %d\n",
896 atomic_read(&cardp->rx_data_urb_pending));
897 }
898
899 p += sprintf(p, "tx_pending = %d\n",
900 atomic_read(&adapter->tx_pending));
901 p += sprintf(p, "rx_pending = %d\n",
902 atomic_read(&adapter->rx_pending));
903
904 if (adapter->iface_type == MWIFIEX_SDIO) {
905 sdio_card = (struct sdio_mmc_card *)adapter->card;
906 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
907 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
908 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
909 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
910 }
911
912 for (i = 0; i < adapter->priv_num; i++) {
913 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
914 continue;
915 priv = adapter->priv[i];
916 p += sprintf(p, "\n[interface : \"%s\"]\n",
917 priv->netdev->name);
918 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
919 atomic_read(&priv->wmm_tx_pending[0]));
920 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
921 atomic_read(&priv->wmm_tx_pending[1]));
922 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
923 atomic_read(&priv->wmm_tx_pending[2]));
924 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
925 atomic_read(&priv->wmm_tx_pending[3]));
926 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
927 "Disconnected" : "Connected");
928 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
929 ? "on" : "off"));
930 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
931 txq = netdev_get_tx_queue(priv->netdev, idx);
932 p += sprintf(p, "tx queue %d:%s ", idx,
933 netif_tx_queue_stopped(txq) ?
934 "stopped" : "started");
935 }
936 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
937 priv->netdev->name, priv->num_tx_timeout);
938 }
939
809c6ea8
XH
940 if (adapter->iface_type == MWIFIEX_SDIO) {
941 p += sprintf(p, "\n=== SDIO register DUMP===\n");
942 if (adapter->if_ops.reg_dump)
943 p += adapter->if_ops.reg_dump(adapter, p);
944 }
945
11cd07a9
XH
946 p += sprintf(p, "\n=== MORE DEBUG INFORMATION\n");
947 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
948 if (debug_info) {
949 for (i = 0; i < adapter->priv_num; i++) {
950 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
951 continue;
952 priv = adapter->priv[i];
953 mwifiex_get_debug_info(priv, debug_info);
954 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
955 break;
956 }
957 kfree(debug_info);
958 }
959
960 adapter->drv_info_size = p - adapter->drv_info_dump;
961 dev_info(adapter->dev, "=== DRIVER INFO DUMP END===\n");
962}
963EXPORT_SYMBOL_GPL(mwifiex_dump_drv_info);
964
5e6e3a92
BZ
965/*
966 * CFG802.11 network device handler for statistics retrieval.
967 */
968static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
969{
970 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
971
972 return &priv->stats;
973}
974
47411a06 975static u16
f663dd9a 976mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
99932d4f 977 void *accel_priv, select_queue_fallback_t fallback)
47411a06 978{
fa9ffc74 979 skb->priority = cfg80211_classify8021d(skb, NULL);
47411a06
AP
980 return mwifiex_1d_to_wmm_queue[skb->priority];
981}
982
5e6e3a92
BZ
983/* Network device handlers */
984static const struct net_device_ops mwifiex_netdev_ops = {
985 .ndo_open = mwifiex_open,
986 .ndo_stop = mwifiex_close,
987 .ndo_start_xmit = mwifiex_hard_start_xmit,
988 .ndo_set_mac_address = mwifiex_set_mac_address,
989 .ndo_tx_timeout = mwifiex_tx_timeout,
990 .ndo_get_stats = mwifiex_get_stats,
afc4b13d 991 .ndo_set_rx_mode = mwifiex_set_multicast_list,
47411a06 992 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
5e6e3a92
BZ
993};
994
995/*
996 * This function initializes the private structure parameters.
997 *
998 * The following wait queues are initialized -
999 * - IOCTL wait queue
1000 * - Command wait queue
1001 * - Statistics wait queue
1002 *
1003 * ...and the following default parameters are set -
1004 * - Current key index : Set to 0
1005 * - Rate index : Set to auto
1006 * - Media connected : Set to disconnected
1007 * - Adhoc link sensed : Set to false
1008 * - Nick name : Set to null
1009 * - Number of Tx timeout : Set to 0
1010 * - Device address : Set to current address
cbf6e055 1011 * - Rx histogram statistc : Set to 0
5e6e3a92
BZ
1012 *
1013 * In addition, the CFG80211 work queue is also created.
1014 */
93a1df48 1015void mwifiex_init_priv_params(struct mwifiex_private *priv,
047eaaf6 1016 struct net_device *dev)
5e6e3a92
BZ
1017{
1018 dev->netdev_ops = &mwifiex_netdev_ops;
f16fdc9d 1019 dev->destructor = free_netdev;
5e6e3a92 1020 /* Initialize private structure */
5e6e3a92
BZ
1021 priv->current_key_index = 0;
1022 priv->media_connected = false;
ede98bfa
AP
1023 memset(priv->mgmt_ie, 0,
1024 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
f31acabe
AP
1025 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1026 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1027 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
2ade5667 1028 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
5e6e3a92 1029 priv->num_tx_timeout = 0;
8d05eb22 1030 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
5e6e3a92 1031 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
cbf6e055
XH
1032
1033 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1034 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1035 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1036 if (priv->hist_data)
1037 mwifiex_hist_data_reset(priv);
1038 }
5e6e3a92
BZ
1039}
1040
5e6e3a92
BZ
1041/*
1042 * This function check if command is pending.
1043 */
1044int is_command_pending(struct mwifiex_adapter *adapter)
1045{
1046 unsigned long flags;
1047 int is_cmd_pend_q_empty;
1048
1049 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1050 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1051 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1052
1053 return !is_cmd_pend_q_empty;
1054}
1055
6e251174
AP
1056/*
1057 * This is the RX work queue function.
1058 *
1059 * It handles the RX operations.
1060 */
1061static void mwifiex_rx_work_queue(struct work_struct *work)
1062{
1063 struct mwifiex_adapter *adapter =
1064 container_of(work, struct mwifiex_adapter, rx_work);
1065
1066 if (adapter->surprise_removed)
1067 return;
1068 mwifiex_process_rx(adapter);
1069}
1070
5e6e3a92
BZ
1071/*
1072 * This is the main work queue function.
1073 *
1074 * It handles the main process, which in turn handles the complete
1075 * driver operations.
1076 */
1077static void mwifiex_main_work_queue(struct work_struct *work)
1078{
1079 struct mwifiex_adapter *adapter =
1080 container_of(work, struct mwifiex_adapter, main_work);
1081
1082 if (adapter->surprise_removed)
1083 return;
1084 mwifiex_main_process(adapter);
1085}
1086
5e6e3a92
BZ
1087/*
1088 * This function adds the card.
1089 *
1090 * This function follows the following major steps to set up the device -
1091 * - Initialize software. This includes probing the card, registering
1092 * the interface operations table, and allocating/initializing the
1093 * adapter structure
1094 * - Set up the netlink socket
1095 * - Create and start the main work queue
1096 * - Register the device
1097 * - Initialize firmware and hardware
1098 * - Add logical interfaces
1099 */
1100int
1101mwifiex_add_card(void *card, struct semaphore *sem,
d930faee 1102 struct mwifiex_if_ops *if_ops, u8 iface_type)
5e6e3a92 1103{
2be7859f 1104 struct mwifiex_adapter *adapter;
5e6e3a92
BZ
1105
1106 if (down_interruptible(sem))
1107 goto exit_sem_err;
1108
93a1df48 1109 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
5e6e3a92
BZ
1110 pr_err("%s: software init failed\n", __func__);
1111 goto err_init_sw;
1112 }
1113
d930faee 1114 adapter->iface_type = iface_type;
6b41f941 1115 adapter->card_sem = sem;
d930faee 1116
5e6e3a92 1117 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
5e6e3a92
BZ
1118 adapter->surprise_removed = false;
1119 init_waitqueue_head(&adapter->init_wait_q);
1120 adapter->is_suspended = false;
1121 adapter->hs_activated = false;
1122 init_waitqueue_head(&adapter->hs_activate_wait_q);
600f5d90 1123 init_waitqueue_head(&adapter->cmd_wait_q.wait);
600f5d90 1124 adapter->cmd_wait_q.status = 0;
efaaa8b8 1125 adapter->scan_wait_q_woken = false;
5e6e3a92 1126
ec4a16b4 1127 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
6e251174
AP
1128 adapter->rx_work_enabled = true;
1129 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1130 }
1131
448a71cc
AK
1132 adapter->workqueue =
1133 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1134 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
5e6e3a92
BZ
1135 if (!adapter->workqueue)
1136 goto err_kmalloc;
1137
1138 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
6e251174
AP
1139
1140 if (adapter->rx_work_enabled) {
1141 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1142 WQ_HIGHPRI |
1143 WQ_MEM_RECLAIM |
1144 WQ_UNBOUND, 1);
1145 if (!adapter->rx_workqueue)
1146 goto err_kmalloc;
1147
1148 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1149 }
1150
5e6e3a92 1151 /* Register the device. Fill up the private data structure with relevant
232fde06 1152 information from the card. */
5e6e3a92
BZ
1153 if (adapter->if_ops.register_dev(adapter)) {
1154 pr_err("%s: failed to register mwifiex device\n", __func__);
1155 goto err_registerdev;
1156 }
1157
5e6e3a92
BZ
1158 if (mwifiex_init_hw_fw(adapter)) {
1159 pr_err("%s: firmware init failed\n", __func__);
1160 goto err_init_fw;
1161 }
2be7859f 1162
5e6e3a92
BZ
1163 return 0;
1164
5e6e3a92 1165err_init_fw:
5e6e3a92 1166 pr_debug("info: %s: unregister device\n", __func__);
4daffe35
AK
1167 if (adapter->if_ops.unregister_dev)
1168 adapter->if_ops.unregister_dev(adapter);
7197325b 1169 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
5e6e3a92
BZ
1170 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1171 adapter->init_wait_q_woken = false;
636c4598
YAP
1172
1173 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
1174 wait_event_interruptible(adapter->init_wait_q,
1175 adapter->init_wait_q_woken);
1176 }
6b41f941
AK
1177err_registerdev:
1178 adapter->surprise_removed = true;
1179 mwifiex_terminate_workqueue(adapter);
1180err_kmalloc:
5e6e3a92
BZ
1181 mwifiex_free_adapter(adapter);
1182
1183err_init_sw:
1184 up(sem);
1185
1186exit_sem_err:
1187 return -1;
1188}
1189EXPORT_SYMBOL_GPL(mwifiex_add_card);
1190
1191/*
1192 * This function removes the card.
1193 *
1194 * This function follows the following major steps to remove the device -
1195 * - Stop data traffic
1196 * - Shutdown firmware
1197 * - Remove the logical interfaces
1198 * - Terminate the work queue
1199 * - Unregister the device
1200 * - Free the adapter structure
1201 */
1202int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1203{
1204 struct mwifiex_private *priv = NULL;
5e6e3a92
BZ
1205 int i;
1206
1207 if (down_interruptible(sem))
1208 goto exit_sem_err;
1209
1210 if (!adapter)
1211 goto exit_remove;
1212
232fde06
DD
1213 /* We can no longer handle interrupts once we start doing the teardown
1214 * below. */
1215 if (adapter->if_ops.disable_int)
1216 adapter->if_ops.disable_int(adapter);
1217
5e6e3a92
BZ
1218 adapter->surprise_removed = true;
1219
9d2e85e0
MY
1220 mwifiex_terminate_workqueue(adapter);
1221
5e6e3a92
BZ
1222 /* Stop data */
1223 for (i = 0; i < adapter->priv_num; i++) {
1224 priv = adapter->priv[i];
93a1df48 1225 if (priv && priv->netdev) {
47411a06 1226 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
5e6e3a92
BZ
1227 if (netif_carrier_ok(priv->netdev))
1228 netif_carrier_off(priv->netdev);
1229 }
1230 }
1231
1232 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1233 adapter->init_wait_q_woken = false;
636c4598
YAP
1234
1235 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
1236 wait_event_interruptible(adapter->init_wait_q,
1237 adapter->init_wait_q_woken);
1238 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1239 if (atomic_read(&adapter->rx_pending) ||
1240 atomic_read(&adapter->tx_pending) ||
600f5d90 1241 atomic_read(&adapter->cmd_pending)) {
5e6e3a92 1242 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
600f5d90 1243 "cmd_pending=%d\n",
5e6e3a92
BZ
1244 atomic_read(&adapter->rx_pending),
1245 atomic_read(&adapter->tx_pending),
600f5d90 1246 atomic_read(&adapter->cmd_pending));
5e6e3a92
BZ
1247 }
1248
93a1df48
YAP
1249 for (i = 0; i < adapter->priv_num; i++) {
1250 priv = adapter->priv[i];
1251
1252 if (!priv)
1253 continue;
1254
1255 rtnl_lock();
4facc34a
AP
1256 if (priv->netdev &&
1257 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1258 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
93a1df48
YAP
1259 rtnl_unlock();
1260 }
1261
c2868ade
AK
1262 wiphy_unregister(adapter->wiphy);
1263 wiphy_free(adapter->wiphy);
64b05e2f 1264
5e6e3a92
BZ
1265 /* Unregister device */
1266 dev_dbg(adapter->dev, "info: unregister device\n");
4daffe35
AK
1267 if (adapter->if_ops.unregister_dev)
1268 adapter->if_ops.unregister_dev(adapter);
5e6e3a92
BZ
1269 /* Free adapter structure */
1270 dev_dbg(adapter->dev, "info: free adapter\n");
1271 mwifiex_free_adapter(adapter);
1272
1273exit_remove:
1274 up(sem);
1275exit_sem_err:
1276 return 0;
1277}
1278EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1279
1280/*
1281 * This function initializes the module.
1282 *
1283 * The debug FS is also initialized if configured.
1284 */
1285static int
1286mwifiex_init_module(void)
1287{
1288#ifdef CONFIG_DEBUG_FS
1289 mwifiex_debugfs_init();
1290#endif
1291 return 0;
1292}
1293
1294/*
1295 * This function cleans up the module.
1296 *
1297 * The debug FS is removed if available.
1298 */
1299static void
1300mwifiex_cleanup_module(void)
1301{
1302#ifdef CONFIG_DEBUG_FS
1303 mwifiex_debugfs_remove();
1304#endif
1305}
1306
1307module_init(mwifiex_init_module);
1308module_exit(mwifiex_cleanup_module);
1309
1310MODULE_AUTHOR("Marvell International Ltd.");
1311MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1312MODULE_VERSION(VERSION);
1313MODULE_LICENSE("GPL v2");