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