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