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