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