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