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