Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[linux-2.6-microblaze.git] / drivers / net / wireless / mwifiex / init.c
1 /*
2  * Marvell Wireless LAN device driver: HW/FW Initialization
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
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 "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27
28 /*
29  * This function adds a BSS priority table to the table list.
30  *
31  * The function allocates a new BSS priority table node and adds it to
32  * the end of BSS priority table list, kept in driver memory.
33  */
34 static int mwifiex_add_bss_prio_tbl(struct mwifiex_private *priv)
35 {
36         struct mwifiex_adapter *adapter = priv->adapter;
37         struct mwifiex_bss_prio_node *bss_prio;
38         struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
39         unsigned long flags;
40
41         bss_prio = kzalloc(sizeof(struct mwifiex_bss_prio_node), GFP_KERNEL);
42         if (!bss_prio)
43                 return -ENOMEM;
44
45         bss_prio->priv = priv;
46         INIT_LIST_HEAD(&bss_prio->list);
47
48         spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
49         list_add_tail(&bss_prio->list, &tbl[priv->bss_priority].bss_prio_head);
50         spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
51
52         return 0;
53 }
54
55 /*
56  * This function initializes the private structure and sets default
57  * values to the members.
58  *
59  * Additionally, it also initializes all the locks and sets up all the
60  * lists.
61  */
62 int mwifiex_init_priv(struct mwifiex_private *priv)
63 {
64         u32 i;
65
66         priv->media_connected = false;
67         memset(priv->curr_addr, 0xff, ETH_ALEN);
68
69         priv->pkt_tx_ctrl = 0;
70         priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
71         priv->data_rate = 0;    /* Initially indicate the rate as auto */
72         priv->is_data_rate_auto = true;
73         priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
74         priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
75
76         priv->sec_info.wep_enabled = 0;
77         priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
78         priv->sec_info.encryption_mode = 0;
79         for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
80                 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
81         priv->wep_key_curr_index = 0;
82         priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
83                                 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
84
85         priv->beacon_period = 100; /* beacon interval */ ;
86         priv->attempted_bss_desc = NULL;
87         memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
88         priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
89
90         memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
91         memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
92         memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
93         priv->assoc_rsp_size = 0;
94         priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
95         priv->atim_window = 0;
96         priv->adhoc_state = ADHOC_IDLE;
97         priv->tx_power_level = 0;
98         priv->max_tx_power_level = 0;
99         priv->min_tx_power_level = 0;
100         priv->tx_rate = 0;
101         priv->rxpd_htinfo = 0;
102         priv->rxpd_rate = 0;
103         priv->rate_bitmap = 0;
104         priv->data_rssi_last = 0;
105         priv->data_rssi_avg = 0;
106         priv->data_nf_avg = 0;
107         priv->data_nf_last = 0;
108         priv->bcn_rssi_last = 0;
109         priv->bcn_rssi_avg = 0;
110         priv->bcn_nf_avg = 0;
111         priv->bcn_nf_last = 0;
112         memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
113         memset(&priv->aes_key, 0, sizeof(priv->aes_key));
114         priv->wpa_ie_len = 0;
115         priv->wpa_is_gtk_set = false;
116
117         memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
118         priv->assoc_tlv_buf_len = 0;
119         memset(&priv->wps, 0, sizeof(priv->wps));
120         memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
121         priv->gen_ie_buf_len = 0;
122         memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
123
124         priv->wmm_required = true;
125         priv->wmm_enabled = false;
126         priv->wmm_qosinfo = 0;
127         priv->curr_bcn_buf = NULL;
128         priv->curr_bcn_size = 0;
129         priv->wps_ie = NULL;
130         priv->wps_ie_len = 0;
131         priv->ap_11n_enabled = 0;
132         memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
133
134         priv->scan_block = false;
135
136         priv->csa_chan = 0;
137         priv->csa_expire_time = 0;
138         priv->del_list_idx = 0;
139
140         return mwifiex_add_bss_prio_tbl(priv);
141 }
142
143 /*
144  * This function allocates buffers for members of the adapter
145  * structure.
146  *
147  * The memory allocated includes scan table, command buffers, and
148  * sleep confirm command buffer. In addition, the queues are
149  * also initialized.
150  */
151 static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
152 {
153         int ret;
154
155         /* Allocate command buffer */
156         ret = mwifiex_alloc_cmd_buffer(adapter);
157         if (ret) {
158                 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
159                         __func__);
160                 return -1;
161         }
162
163         adapter->sleep_cfm =
164                 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
165                               + INTF_HEADER_LEN);
166
167         if (!adapter->sleep_cfm) {
168                 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
169                         " cmd buffer\n", __func__);
170                 return -1;
171         }
172         skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
173
174         return 0;
175 }
176
177 /*
178  * This function initializes the adapter structure and sets default
179  * values to the members of adapter.
180  *
181  * This also initializes the WMM related parameters in the driver private
182  * structures.
183  */
184 static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
185 {
186         struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
187
188         skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
189
190         adapter->cmd_sent = false;
191
192         if (adapter->iface_type == MWIFIEX_SDIO)
193                 adapter->data_sent = true;
194         else
195                 adapter->data_sent = false;
196
197         adapter->cmd_resp_received = false;
198         adapter->event_received = false;
199         adapter->data_received = false;
200
201         adapter->surprise_removed = false;
202
203         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
204
205         adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
206         adapter->ps_state = PS_STATE_AWAKE;
207         adapter->need_to_wakeup = false;
208
209         adapter->scan_mode = HostCmd_BSS_MODE_ANY;
210         adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
211         adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
212         adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
213
214         adapter->scan_probes = 1;
215
216         adapter->multiple_dtim = 1;
217
218         adapter->local_listen_interval = 0;     /* default value in firmware
219                                                    will be used */
220
221         adapter->is_deep_sleep = false;
222
223         adapter->delay_null_pkt = false;
224         adapter->delay_to_ps = 1000;
225         adapter->enhanced_ps_mode = PS_MODE_AUTO;
226
227         adapter->gen_null_pkt = false;  /* Disable NULL Pkg generation by
228                                            default */
229         adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
230                                            default */
231         adapter->pm_wakeup_card_req = false;
232
233         adapter->pm_wakeup_fw_try = false;
234
235         adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
236         adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
237
238         adapter->is_hs_configured = false;
239         adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
240         adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
241         adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
242         adapter->hs_activated = false;
243
244         memset(adapter->event_body, 0, sizeof(adapter->event_body));
245         adapter->hw_dot_11n_dev_cap = 0;
246         adapter->hw_dev_mcs_support = 0;
247         adapter->sec_chan_offset = 0;
248         adapter->adhoc_11n_enabled = false;
249
250         mwifiex_wmm_init(adapter);
251
252         if (adapter->sleep_cfm) {
253                 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
254                                                 adapter->sleep_cfm->data;
255                 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
256                 sleep_cfm_buf->command =
257                                 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
258                 sleep_cfm_buf->size =
259                                 cpu_to_le16(adapter->sleep_cfm->len);
260                 sleep_cfm_buf->result = 0;
261                 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
262                 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
263         }
264         memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
265         memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
266         adapter->tx_lock_flag = false;
267         adapter->null_pkt_interval = 0;
268         adapter->fw_bands = 0;
269         adapter->config_bands = 0;
270         adapter->adhoc_start_band = 0;
271         adapter->scan_channels = NULL;
272         adapter->fw_release_number = 0;
273         adapter->fw_cap_info = 0;
274         memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
275         adapter->event_cause = 0;
276         adapter->region_code = 0;
277         adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
278         adapter->adhoc_awake_period = 0;
279         memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
280         adapter->arp_filter_size = 0;
281         adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
282         adapter->empty_tx_q_cnt = 0;
283 }
284
285 /*
286  * This function sets trans_start per tx_queue
287  */
288 void mwifiex_set_trans_start(struct net_device *dev)
289 {
290         int i;
291
292         for (i = 0; i < dev->num_tx_queues; i++)
293                 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
294
295         dev->trans_start = jiffies;
296 }
297
298 /*
299  * This function wakes up all queues in net_device
300  */
301 void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
302                                         struct mwifiex_adapter *adapter)
303 {
304         unsigned long dev_queue_flags;
305         unsigned int i;
306
307         spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
308
309         for (i = 0; i < netdev->num_tx_queues; i++) {
310                 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
311
312                 if (netif_tx_queue_stopped(txq))
313                         netif_tx_wake_queue(txq);
314         }
315
316         spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
317 }
318
319 /*
320  * This function stops all queues in net_device
321  */
322 void mwifiex_stop_net_dev_queue(struct net_device *netdev,
323                                         struct mwifiex_adapter *adapter)
324 {
325         unsigned long dev_queue_flags;
326         unsigned int i;
327
328         spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
329
330         for (i = 0; i < netdev->num_tx_queues; i++) {
331                 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
332
333                 if (!netif_tx_queue_stopped(txq))
334                         netif_tx_stop_queue(txq);
335         }
336
337         spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
338 }
339
340 /*
341  *  This function releases the lock variables and frees the locks and
342  *  associated locks.
343  */
344 static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
345 {
346         struct mwifiex_private *priv;
347         s32 i, j;
348
349         /* Free lists */
350         list_del(&adapter->cmd_free_q);
351         list_del(&adapter->cmd_pending_q);
352         list_del(&adapter->scan_pending_q);
353
354         for (i = 0; i < adapter->priv_num; i++)
355                 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
356
357         for (i = 0; i < adapter->priv_num; i++) {
358                 if (adapter->priv[i]) {
359                         priv = adapter->priv[i];
360                         for (j = 0; j < MAX_NUM_TID; ++j)
361                                 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
362                         list_del(&priv->tx_ba_stream_tbl_ptr);
363                         list_del(&priv->rx_reorder_tbl_ptr);
364                         list_del(&priv->sta_list);
365                 }
366         }
367 }
368
369 /*
370  * This function performs cleanup for adapter structure.
371  *
372  * The cleanup is done recursively, by canceling all pending
373  * commands, freeing the member buffers previously allocated
374  * (command buffers, scan table buffer, sleep confirm command
375  * buffer), stopping the timers and calling the cleanup routines
376  * for every interface.
377  */
378 static void
379 mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
380 {
381         if (!adapter) {
382                 pr_err("%s: adapter is NULL\n", __func__);
383                 return;
384         }
385
386         mwifiex_cancel_all_pending_cmd(adapter);
387
388         /* Free lock variables */
389         mwifiex_free_lock_list(adapter);
390
391         /* Free command buffer */
392         dev_dbg(adapter->dev, "info: free cmd buffer\n");
393         mwifiex_free_cmd_buffer(adapter);
394
395         dev_dbg(adapter->dev, "info: free scan table\n");
396
397         if (adapter->sleep_cfm)
398                 dev_kfree_skb_any(adapter->sleep_cfm);
399 }
400
401 /*
402  *  This function intializes the lock variables and
403  *  the list heads.
404  */
405 int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
406 {
407         struct mwifiex_private *priv;
408         s32 i, j;
409
410         spin_lock_init(&adapter->mwifiex_lock);
411         spin_lock_init(&adapter->int_lock);
412         spin_lock_init(&adapter->main_proc_lock);
413         spin_lock_init(&adapter->mwifiex_cmd_lock);
414         spin_lock_init(&adapter->queue_lock);
415         for (i = 0; i < adapter->priv_num; i++) {
416                 if (adapter->priv[i]) {
417                         priv = adapter->priv[i];
418                         spin_lock_init(&priv->rx_pkt_lock);
419                         spin_lock_init(&priv->wmm.ra_list_spinlock);
420                         spin_lock_init(&priv->curr_bcn_buf_lock);
421                         spin_lock_init(&priv->sta_list_spinlock);
422                 }
423         }
424
425         /* Initialize cmd_free_q */
426         INIT_LIST_HEAD(&adapter->cmd_free_q);
427         /* Initialize cmd_pending_q */
428         INIT_LIST_HEAD(&adapter->cmd_pending_q);
429         /* Initialize scan_pending_q */
430         INIT_LIST_HEAD(&adapter->scan_pending_q);
431
432         spin_lock_init(&adapter->cmd_free_q_lock);
433         spin_lock_init(&adapter->cmd_pending_q_lock);
434         spin_lock_init(&adapter->scan_pending_q_lock);
435
436         skb_queue_head_init(&adapter->usb_rx_data_q);
437
438         for (i = 0; i < adapter->priv_num; ++i) {
439                 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
440                 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
441         }
442
443         for (i = 0; i < adapter->priv_num; i++) {
444                 if (!adapter->priv[i])
445                         continue;
446                 priv = adapter->priv[i];
447                 for (j = 0; j < MAX_NUM_TID; ++j)
448                         INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
449                 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
450                 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
451                 INIT_LIST_HEAD(&priv->sta_list);
452
453                 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
454                 spin_lock_init(&priv->rx_reorder_tbl_lock);
455         }
456
457         return 0;
458 }
459
460 /*
461  * This function initializes the firmware.
462  *
463  * The following operations are performed sequentially -
464  *      - Allocate adapter structure
465  *      - Initialize the adapter structure
466  *      - Initialize the private structure
467  *      - Add BSS priority tables to the adapter structure
468  *      - For each interface, send the init commands to firmware
469  *      - Send the first command in command pending queue, if available
470  */
471 int mwifiex_init_fw(struct mwifiex_adapter *adapter)
472 {
473         int ret;
474         struct mwifiex_private *priv;
475         u8 i, first_sta = true;
476         int is_cmd_pend_q_empty;
477         unsigned long flags;
478
479         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
480
481         /* Allocate memory for member of adapter structure */
482         ret = mwifiex_allocate_adapter(adapter);
483         if (ret)
484                 return -1;
485
486         /* Initialize adapter structure */
487         mwifiex_init_adapter(adapter);
488
489         for (i = 0; i < adapter->priv_num; i++) {
490                 if (adapter->priv[i]) {
491                         priv = adapter->priv[i];
492
493                         /* Initialize private structure */
494                         ret = mwifiex_init_priv(priv);
495                         if (ret)
496                                 return -1;
497                 }
498         }
499
500         if (adapter->if_ops.init_fw_port) {
501                 if (adapter->if_ops.init_fw_port(adapter))
502                         return -1;
503         }
504
505         for (i = 0; i < adapter->priv_num; i++) {
506                 if (adapter->priv[i]) {
507                         ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
508                         if (ret == -1)
509                                 return -1;
510
511                         first_sta = false;
512                 }
513         }
514
515         spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
516         is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
517         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
518         if (!is_cmd_pend_q_empty) {
519                 /* Send the first command in queue and return */
520                 if (mwifiex_main_process(adapter) != -1)
521                         ret = -EINPROGRESS;
522         } else {
523                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
524         }
525
526         return ret;
527 }
528
529 /*
530  * This function deletes the BSS priority tables.
531  *
532  * The function traverses through all the allocated BSS priority nodes
533  * in every BSS priority table and frees them.
534  */
535 static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
536 {
537         int i;
538         struct mwifiex_adapter *adapter = priv->adapter;
539         struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
540         struct list_head *head;
541         spinlock_t *lock; /* bss priority lock */
542         unsigned long flags;
543
544         for (i = 0; i < adapter->priv_num; ++i) {
545                 head = &adapter->bss_prio_tbl[i].bss_prio_head;
546                 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
547                 dev_dbg(adapter->dev, "info: delete BSS priority table,"
548                                 " bss_type = %d, bss_num = %d, i = %d,"
549                                 " head = %p\n",
550                               priv->bss_type, priv->bss_num, i, head);
551
552                 {
553                         spin_lock_irqsave(lock, flags);
554                         if (list_empty(head)) {
555                                 spin_unlock_irqrestore(lock, flags);
556                                 continue;
557                         }
558                         list_for_each_entry_safe(bssprio_node, tmp_node, head,
559                                                  list) {
560                                 if (bssprio_node->priv == priv) {
561                                         dev_dbg(adapter->dev, "info: Delete "
562                                                 "node %p, next = %p\n",
563                                                 bssprio_node, tmp_node);
564                                         list_del(&bssprio_node->list);
565                                         kfree(bssprio_node);
566                                 }
567                         }
568                         spin_unlock_irqrestore(lock, flags);
569                 }
570         }
571 }
572
573 /*
574  * This function frees the private structure, including cleans
575  * up the TX and RX queues and frees the BSS priority tables.
576  */
577 void mwifiex_free_priv(struct mwifiex_private *priv)
578 {
579         mwifiex_clean_txrx(priv);
580         mwifiex_delete_bss_prio_tbl(priv);
581         mwifiex_free_curr_bcn(priv);
582 }
583
584 /*
585  * This function is used to shutdown the driver.
586  *
587  * The following operations are performed sequentially -
588  *      - Check if already shut down
589  *      - Make sure the main process has stopped
590  *      - Clean up the Tx and Rx queues
591  *      - Delete BSS priority tables
592  *      - Free the adapter
593  *      - Notify completion
594  */
595 int
596 mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
597 {
598         int ret = -EINPROGRESS;
599         struct mwifiex_private *priv;
600         s32 i;
601         struct sk_buff *skb;
602
603         /* mwifiex already shutdown */
604         if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
605                 return 0;
606
607         adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
608         /* wait for mwifiex_process to complete */
609         if (adapter->mwifiex_processing) {
610                 dev_warn(adapter->dev, "main process is still running\n");
611                 return ret;
612         }
613
614         /* cancel current command */
615         if (adapter->curr_cmd) {
616                 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
617                 del_timer(&adapter->cmd_timer);
618                 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
619                 adapter->curr_cmd = NULL;
620         }
621
622         /* shut down mwifiex */
623         dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
624
625         /* Clean up Tx/Rx queues and delete BSS priority table */
626         for (i = 0; i < adapter->priv_num; i++) {
627                 if (adapter->priv[i]) {
628                         priv = adapter->priv[i];
629
630                         mwifiex_clean_txrx(priv);
631                         mwifiex_delete_bss_prio_tbl(priv);
632                 }
633         }
634
635         spin_lock(&adapter->mwifiex_lock);
636
637         if (adapter->if_ops.data_complete) {
638                 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
639                         struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
640
641                         priv = adapter->priv[rx_info->bss_num];
642                         if (priv)
643                                 priv->stats.rx_dropped++;
644
645                         adapter->if_ops.data_complete(adapter, skb);
646                 }
647         }
648
649         mwifiex_adapter_cleanup(adapter);
650
651         spin_unlock(&adapter->mwifiex_lock);
652
653         /* Notify completion */
654         ret = mwifiex_shutdown_fw_complete(adapter);
655
656         return ret;
657 }
658
659 /*
660  * This function downloads the firmware to the card.
661  *
662  * The actual download is preceded by two sanity checks -
663  *      - Check if firmware is already running
664  *      - Check if the interface is the winner to download the firmware
665  *
666  * ...and followed by another -
667  *      - Check if the firmware is downloaded successfully
668  *
669  * After download is successfully completed, the host interrupts are enabled.
670  */
671 int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
672                     struct mwifiex_fw_image *pmfw)
673 {
674         int ret;
675         u32 poll_num = 1;
676
677         if (adapter->if_ops.check_fw_status) {
678                 adapter->winner = 0;
679
680                 /* check if firmware is already running */
681                 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
682                 if (!ret) {
683                         dev_notice(adapter->dev,
684                                    "WLAN FW already running! Skip FW dnld\n");
685                         return 0;
686                 }
687
688                 poll_num = MAX_FIRMWARE_POLL_TRIES;
689
690                 /* check if we are the winner for downloading FW */
691                 if (!adapter->winner) {
692                         dev_notice(adapter->dev,
693                                    "FW already running! Skip FW dnld\n");
694                         goto poll_fw;
695                 }
696         }
697
698         if (pmfw) {
699                 /* Download firmware with helper */
700                 ret = adapter->if_ops.prog_fw(adapter, pmfw);
701                 if (ret) {
702                         dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
703                         return ret;
704                 }
705         }
706
707 poll_fw:
708         /* Check if the firmware is downloaded successfully or not */
709         ret = adapter->if_ops.check_fw_status(adapter, poll_num);
710         if (ret)
711                 dev_err(adapter->dev, "FW failed to be active in time\n");
712
713         return ret;
714 }