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