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