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