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