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_work(struct work_struct *work)
55 {
56 	struct mwifiex_adapter *adapter =
57 		container_of(work, struct mwifiex_adapter, devdump_work.work);
58 
59 	mwifiex_upload_device_dump(adapter);
60 }
61 
62 /*
63  * This function initializes the private structure and sets default
64  * values to the members.
65  *
66  * Additionally, it also initializes all the locks and sets up all the
67  * lists.
68  */
69 int mwifiex_init_priv(struct mwifiex_private *priv)
70 {
71 	u32 i;
72 
73 	priv->media_connected = false;
74 	eth_broadcast_addr(priv->curr_addr);
75 	priv->port_open = false;
76 	priv->usb_port = MWIFIEX_USB_EP_DATA;
77 	priv->pkt_tx_ctrl = 0;
78 	priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
79 	priv->data_rate = 0;	/* Initially indicate the rate as auto */
80 	priv->is_data_rate_auto = true;
81 	priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
82 	priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
83 
84 	priv->sec_info.wep_enabled = 0;
85 	priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
86 	priv->sec_info.encryption_mode = 0;
87 	for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
88 		memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
89 	priv->wep_key_curr_index = 0;
90 	priv->curr_pkt_filter = HostCmd_ACT_MAC_DYNAMIC_BW_ENABLE |
91 				HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
92 				HostCmd_ACT_MAC_ETHERNETII_ENABLE;
93 
94 	priv->beacon_period = 100; /* beacon interval */
95 	priv->attempted_bss_desc = NULL;
96 	memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
97 	priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
98 
99 	memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
100 	memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
101 	memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
102 	priv->assoc_rsp_size = 0;
103 	priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
104 	priv->atim_window = 0;
105 	priv->adhoc_state = ADHOC_IDLE;
106 	priv->tx_power_level = 0;
107 	priv->max_tx_power_level = 0;
108 	priv->min_tx_power_level = 0;
109 	priv->tx_ant = 0;
110 	priv->rx_ant = 0;
111 	priv->tx_rate = 0;
112 	priv->rxpd_htinfo = 0;
113 	priv->rxpd_rate = 0;
114 	priv->rate_bitmap = 0;
115 	priv->data_rssi_last = 0;
116 	priv->data_rssi_avg = 0;
117 	priv->data_nf_avg = 0;
118 	priv->data_nf_last = 0;
119 	priv->bcn_rssi_last = 0;
120 	priv->bcn_rssi_avg = 0;
121 	priv->bcn_nf_avg = 0;
122 	priv->bcn_nf_last = 0;
123 	memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
124 	memset(&priv->aes_key, 0, sizeof(priv->aes_key));
125 	priv->wpa_ie_len = 0;
126 	priv->wpa_is_gtk_set = false;
127 
128 	memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
129 	priv->assoc_tlv_buf_len = 0;
130 	memset(&priv->wps, 0, sizeof(priv->wps));
131 	memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
132 	priv->gen_ie_buf_len = 0;
133 	memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
134 
135 	priv->wmm_required = true;
136 	priv->wmm_enabled = false;
137 	priv->wmm_qosinfo = 0;
138 	priv->curr_bcn_buf = NULL;
139 	priv->curr_bcn_size = 0;
140 	priv->wps_ie = NULL;
141 	priv->wps_ie_len = 0;
142 	priv->ap_11n_enabled = 0;
143 	memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
144 
145 	priv->scan_block = false;
146 
147 	priv->csa_chan = 0;
148 	priv->csa_expire_time = 0;
149 	priv->del_list_idx = 0;
150 	priv->hs2_enabled = false;
151 	priv->check_tdls_tx = false;
152 	memcpy(priv->tos_to_tid_inv, tos_to_tid_inv, MAX_NUM_TID);
153 
154 	mwifiex_init_11h_params(priv);
155 
156 	return mwifiex_add_bss_prio_tbl(priv);
157 }
158 
159 /*
160  * This function allocates buffers for members of the adapter
161  * structure.
162  *
163  * The memory allocated includes scan table, command buffers, and
164  * sleep confirm command buffer. In addition, the queues are
165  * also initialized.
166  */
167 static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
168 {
169 	int ret;
170 
171 	/* Allocate command buffer */
172 	ret = mwifiex_alloc_cmd_buffer(adapter);
173 	if (ret) {
174 		mwifiex_dbg(adapter, ERROR,
175 			    "%s: failed to alloc cmd buffer\n",
176 			    __func__);
177 		return -1;
178 	}
179 
180 	adapter->sleep_cfm =
181 		dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
182 			      + INTF_HEADER_LEN);
183 
184 	if (!adapter->sleep_cfm) {
185 		mwifiex_dbg(adapter, ERROR,
186 			    "%s: failed to alloc sleep cfm\t"
187 			    " cmd buffer\n", __func__);
188 		return -1;
189 	}
190 	skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
191 
192 	return 0;
193 }
194 
195 /*
196  * This function initializes the adapter structure and sets default
197  * values to the members of adapter.
198  *
199  * This also initializes the WMM related parameters in the driver private
200  * structures.
201  */
202 static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
203 {
204 	struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
205 
206 	skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
207 
208 	adapter->cmd_sent = false;
209 
210 	if (adapter->iface_type == MWIFIEX_SDIO)
211 		adapter->data_sent = true;
212 	else
213 		adapter->data_sent = false;
214 
215 	if (adapter->iface_type == MWIFIEX_USB)
216 		adapter->intf_hdr_len = 0;
217 	else
218 		adapter->intf_hdr_len = INTF_HEADER_LEN;
219 
220 	adapter->cmd_resp_received = false;
221 	adapter->event_received = false;
222 	adapter->data_received = false;
223 
224 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
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 	clear_bit(MWIFIEX_IS_HS_CONFIGURED, &adapter->work_flags);
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_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->fw_release_number = 0;
292 	adapter->fw_cap_info = 0;
293 	memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
294 	adapter->event_cause = 0;
295 	adapter->region_code = 0;
296 	adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
297 	adapter->adhoc_awake_period = 0;
298 	memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
299 	adapter->arp_filter_size = 0;
300 	adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
301 	adapter->mfg_mode = mfg_mode;
302 	adapter->key_api_major_ver = 0;
303 	adapter->key_api_minor_ver = 0;
304 	eth_broadcast_addr(adapter->perm_addr);
305 	adapter->iface_limit.sta_intf = MWIFIEX_MAX_STA_NUM;
306 	adapter->iface_limit.uap_intf = MWIFIEX_MAX_UAP_NUM;
307 	adapter->iface_limit.p2p_intf = MWIFIEX_MAX_P2P_NUM;
308 	adapter->active_scan_triggered = false;
309 	timer_setup(&adapter->wakeup_timer, wakeup_timer_fn, 0);
310 	adapter->devdump_len = 0;
311 	INIT_DELAYED_WORK(&adapter->devdump_work, fw_dump_work);
312 }
313 
314 /*
315  * This function sets trans_start per tx_queue
316  */
317 void mwifiex_set_trans_start(struct net_device *dev)
318 {
319 	int i;
320 
321 	for (i = 0; i < dev->num_tx_queues; i++)
322 		txq_trans_cond_update(netdev_get_tx_queue(dev, i));
323 
324 	netif_trans_update(dev);
325 }
326 
327 /*
328  * This function wakes up all queues in net_device
329  */
330 void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
331 					struct mwifiex_adapter *adapter)
332 {
333 	spin_lock_bh(&adapter->queue_lock);
334 	netif_tx_wake_all_queues(netdev);
335 	spin_unlock_bh(&adapter->queue_lock);
336 }
337 
338 /*
339  * This function stops all queues in net_device
340  */
341 void mwifiex_stop_net_dev_queue(struct net_device *netdev,
342 					struct mwifiex_adapter *adapter)
343 {
344 	spin_lock_bh(&adapter->queue_lock);
345 	netif_tx_stop_all_queues(netdev);
346 	spin_unlock_bh(&adapter->queue_lock);
347 }
348 
349 /*
350  * This function invalidates the list heads.
351  */
352 static void mwifiex_invalidate_lists(struct mwifiex_adapter *adapter)
353 {
354 	struct mwifiex_private *priv;
355 	s32 i, j;
356 
357 	list_del(&adapter->cmd_free_q);
358 	list_del(&adapter->cmd_pending_q);
359 	list_del(&adapter->scan_pending_q);
360 
361 	for (i = 0; i < adapter->priv_num; i++)
362 		list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
363 
364 	for (i = 0; i < adapter->priv_num; i++) {
365 		if (adapter->priv[i]) {
366 			priv = adapter->priv[i];
367 			for (j = 0; j < MAX_NUM_TID; ++j)
368 				list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
369 			list_del(&priv->tx_ba_stream_tbl_ptr);
370 			list_del(&priv->rx_reorder_tbl_ptr);
371 			list_del(&priv->sta_list);
372 			list_del(&priv->auto_tdls_list);
373 		}
374 	}
375 }
376 
377 /*
378  * This function performs cleanup for adapter structure.
379  *
380  * The cleanup is done recursively, by canceling all pending
381  * commands, freeing the member buffers previously allocated
382  * (command buffers, scan table buffer, sleep confirm command
383  * buffer), stopping the timers and calling the cleanup routines
384  * for every interface.
385  */
386 static void
387 mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
388 {
389 	del_timer(&adapter->wakeup_timer);
390 	cancel_delayed_work_sync(&adapter->devdump_work);
391 	mwifiex_cancel_all_pending_cmd(adapter);
392 	wake_up_interruptible(&adapter->cmd_wait_q.wait);
393 	wake_up_interruptible(&adapter->hs_activate_wait_q);
394 }
395 
396 void mwifiex_free_cmd_buffers(struct mwifiex_adapter *adapter)
397 {
398 	mwifiex_invalidate_lists(adapter);
399 
400 	/* Free command buffer */
401 	mwifiex_dbg(adapter, INFO, "info: free cmd buffer\n");
402 	mwifiex_free_cmd_buffer(adapter);
403 
404 	if (adapter->sleep_cfm)
405 		dev_kfree_skb_any(adapter->sleep_cfm);
406 }
407 
408 /*
409  *  This function intializes the lock variables and
410  *  the list heads.
411  */
412 int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
413 {
414 	struct mwifiex_private *priv;
415 	s32 i, j;
416 
417 	spin_lock_init(&adapter->int_lock);
418 	spin_lock_init(&adapter->main_proc_lock);
419 	spin_lock_init(&adapter->mwifiex_cmd_lock);
420 	spin_lock_init(&adapter->queue_lock);
421 	for (i = 0; i < adapter->priv_num; i++) {
422 		if (adapter->priv[i]) {
423 			priv = adapter->priv[i];
424 			spin_lock_init(&priv->wmm.ra_list_spinlock);
425 			spin_lock_init(&priv->curr_bcn_buf_lock);
426 			spin_lock_init(&priv->sta_list_spinlock);
427 			spin_lock_init(&priv->auto_tdls_lock);
428 		}
429 	}
430 
431 	/* Initialize cmd_free_q */
432 	INIT_LIST_HEAD(&adapter->cmd_free_q);
433 	/* Initialize cmd_pending_q */
434 	INIT_LIST_HEAD(&adapter->cmd_pending_q);
435 	/* Initialize scan_pending_q */
436 	INIT_LIST_HEAD(&adapter->scan_pending_q);
437 
438 	spin_lock_init(&adapter->cmd_free_q_lock);
439 	spin_lock_init(&adapter->cmd_pending_q_lock);
440 	spin_lock_init(&adapter->scan_pending_q_lock);
441 	spin_lock_init(&adapter->rx_proc_lock);
442 
443 	skb_queue_head_init(&adapter->rx_data_q);
444 	skb_queue_head_init(&adapter->tx_data_q);
445 
446 	for (i = 0; i < adapter->priv_num; ++i) {
447 		INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
448 		spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
449 	}
450 
451 	for (i = 0; i < adapter->priv_num; i++) {
452 		if (!adapter->priv[i])
453 			continue;
454 		priv = adapter->priv[i];
455 		for (j = 0; j < MAX_NUM_TID; ++j)
456 			INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
457 		INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
458 		INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
459 		INIT_LIST_HEAD(&priv->sta_list);
460 		INIT_LIST_HEAD(&priv->auto_tdls_list);
461 		skb_queue_head_init(&priv->tdls_txq);
462 		skb_queue_head_init(&priv->bypass_txq);
463 
464 		spin_lock_init(&priv->tx_ba_stream_tbl_lock);
465 		spin_lock_init(&priv->rx_reorder_tbl_lock);
466 
467 		spin_lock_init(&priv->ack_status_lock);
468 		idr_init(&priv->ack_status_frames);
469 	}
470 
471 	return 0;
472 }
473 
474 /*
475  * This function initializes the firmware.
476  *
477  * The following operations are performed sequentially -
478  *      - Allocate adapter structure
479  *      - Initialize the adapter structure
480  *      - Initialize the private structure
481  *      - Add BSS priority tables to the adapter structure
482  *      - For each interface, send the init commands to firmware
483  *      - Send the first command in command pending queue, if available
484  */
485 int mwifiex_init_fw(struct mwifiex_adapter *adapter)
486 {
487 	int ret;
488 	struct mwifiex_private *priv;
489 	u8 i, first_sta = true;
490 	int is_cmd_pend_q_empty;
491 
492 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
493 
494 	/* Allocate memory for member of adapter structure */
495 	ret = mwifiex_allocate_adapter(adapter);
496 	if (ret)
497 		return -1;
498 
499 	/* Initialize adapter structure */
500 	mwifiex_init_adapter(adapter);
501 
502 	for (i = 0; i < adapter->priv_num; i++) {
503 		if (adapter->priv[i]) {
504 			priv = adapter->priv[i];
505 
506 			/* Initialize private structure */
507 			ret = mwifiex_init_priv(priv);
508 			if (ret)
509 				return -1;
510 		}
511 	}
512 	if (adapter->mfg_mode) {
513 		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
514 		ret = -EINPROGRESS;
515 	} else {
516 		for (i = 0; i < adapter->priv_num; i++) {
517 			if (adapter->priv[i]) {
518 				ret = mwifiex_sta_init_cmd(adapter->priv[i],
519 							   first_sta, true);
520 				if (ret == -1)
521 					return -1;
522 
523 				first_sta = false;
524 			}
525 
526 
527 
528 		}
529 	}
530 
531 	spin_lock_bh(&adapter->cmd_pending_q_lock);
532 	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
533 	spin_unlock_bh(&adapter->cmd_pending_q_lock);
534 	if (!is_cmd_pend_q_empty) {
535 		/* Send the first command in queue and return */
536 		if (mwifiex_main_process(adapter) != -1)
537 			ret = -EINPROGRESS;
538 	} else {
539 		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
540 	}
541 
542 	return ret;
543 }
544 
545 /*
546  * This function deletes the BSS priority tables.
547  *
548  * The function traverses through all the allocated BSS priority nodes
549  * in every BSS priority table and frees them.
550  */
551 static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
552 {
553 	int i;
554 	struct mwifiex_adapter *adapter = priv->adapter;
555 	struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
556 	struct list_head *head;
557 	spinlock_t *lock; /* bss priority lock */
558 
559 	for (i = 0; i < adapter->priv_num; ++i) {
560 		head = &adapter->bss_prio_tbl[i].bss_prio_head;
561 		lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
562 		mwifiex_dbg(adapter, INFO,
563 			    "info: delete BSS priority table,\t"
564 			    "bss_type = %d, bss_num = %d, i = %d,\t"
565 			    "head = %p\n",
566 			    priv->bss_type, priv->bss_num, i, head);
567 
568 		{
569 			spin_lock_bh(lock);
570 			list_for_each_entry_safe(bssprio_node, tmp_node, head,
571 						 list) {
572 				if (bssprio_node->priv == priv) {
573 					mwifiex_dbg(adapter, INFO,
574 						    "info: Delete\t"
575 						    "node %p, next = %p\n",
576 						    bssprio_node, tmp_node);
577 					list_del(&bssprio_node->list);
578 					kfree(bssprio_node);
579 				}
580 			}
581 			spin_unlock_bh(lock);
582 		}
583 	}
584 }
585 
586 /*
587  * This function frees the private structure, including cleans
588  * up the TX and RX queues and frees the BSS priority tables.
589  */
590 void mwifiex_free_priv(struct mwifiex_private *priv)
591 {
592 	mwifiex_clean_txrx(priv);
593 	mwifiex_delete_bss_prio_tbl(priv);
594 	mwifiex_free_curr_bcn(priv);
595 }
596 
597 /*
598  * This function is used to shutdown the driver.
599  *
600  * The following operations are performed sequentially -
601  *      - Check if already shut down
602  *      - Make sure the main process has stopped
603  *      - Clean up the Tx and Rx queues
604  *      - Delete BSS priority tables
605  *      - Free the adapter
606  *      - Notify completion
607  */
608 void
609 mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
610 {
611 	struct mwifiex_private *priv;
612 	s32 i;
613 	struct sk_buff *skb;
614 
615 	/* mwifiex already shutdown */
616 	if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
617 		return;
618 
619 	/* cancel current command */
620 	if (adapter->curr_cmd) {
621 		mwifiex_dbg(adapter, WARN,
622 			    "curr_cmd is still in processing\n");
623 		del_timer_sync(&adapter->cmd_timer);
624 		mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
625 		adapter->curr_cmd = NULL;
626 	}
627 
628 	/* shut down mwifiex */
629 	mwifiex_dbg(adapter, MSG,
630 		    "info: shutdown mwifiex...\n");
631 
632 	/* Clean up Tx/Rx queues and delete BSS priority table */
633 	for (i = 0; i < adapter->priv_num; i++) {
634 		if (adapter->priv[i]) {
635 			priv = adapter->priv[i];
636 
637 			mwifiex_clean_auto_tdls(priv);
638 			mwifiex_abort_cac(priv);
639 			mwifiex_free_priv(priv);
640 		}
641 	}
642 
643 	atomic_set(&adapter->tx_queued, 0);
644 	while ((skb = skb_dequeue(&adapter->tx_data_q)))
645 		mwifiex_write_data_complete(adapter, skb, 0, 0);
646 
647 	spin_lock_bh(&adapter->rx_proc_lock);
648 
649 	while ((skb = skb_dequeue(&adapter->rx_data_q))) {
650 		struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
651 
652 		atomic_dec(&adapter->rx_pending);
653 		priv = adapter->priv[rx_info->bss_num];
654 		if (priv)
655 			priv->stats.rx_dropped++;
656 
657 		dev_kfree_skb_any(skb);
658 	}
659 
660 	spin_unlock_bh(&adapter->rx_proc_lock);
661 
662 	mwifiex_adapter_cleanup(adapter);
663 
664 	adapter->hw_status = MWIFIEX_HW_STATUS_NOT_READY;
665 }
666 
667 /*
668  * This function downloads the firmware to the card.
669  *
670  * The actual download is preceded by two sanity checks -
671  *      - Check if firmware is already running
672  *      - Check if the interface is the winner to download the firmware
673  *
674  * ...and followed by another -
675  *      - Check if the firmware is downloaded successfully
676  *
677  * After download is successfully completed, the host interrupts are enabled.
678  */
679 int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
680 		    struct mwifiex_fw_image *pmfw)
681 {
682 	int ret;
683 	u32 poll_num = 1;
684 
685 	/* check if firmware is already running */
686 	ret = adapter->if_ops.check_fw_status(adapter, poll_num);
687 	if (!ret) {
688 		mwifiex_dbg(adapter, MSG,
689 			    "WLAN FW already running! Skip FW dnld\n");
690 		return 0;
691 	}
692 
693 	/* check if we are the winner for downloading FW */
694 	if (adapter->if_ops.check_winner_status) {
695 		adapter->winner = 0;
696 		ret = adapter->if_ops.check_winner_status(adapter);
697 
698 		poll_num = MAX_FIRMWARE_POLL_TRIES;
699 		if (ret) {
700 			mwifiex_dbg(adapter, MSG,
701 				    "WLAN read winner status failed!\n");
702 			return ret;
703 		}
704 
705 		if (!adapter->winner) {
706 			mwifiex_dbg(adapter, MSG,
707 				    "WLAN is not the winner! Skip FW dnld\n");
708 			goto poll_fw;
709 		}
710 	}
711 
712 	if (pmfw) {
713 		/* Download firmware with helper */
714 		ret = adapter->if_ops.prog_fw(adapter, pmfw);
715 		if (ret) {
716 			mwifiex_dbg(adapter, ERROR,
717 				    "prog_fw failed ret=%#x\n", ret);
718 			return ret;
719 		}
720 	}
721 
722 poll_fw:
723 	/* Check if the firmware is downloaded successfully or not */
724 	ret = adapter->if_ops.check_fw_status(adapter, poll_num);
725 	if (ret)
726 		mwifiex_dbg(adapter, ERROR,
727 			    "FW failed to be active in time\n");
728 
729 	return ret;
730 }
731 EXPORT_SYMBOL_GPL(mwifiex_dnld_fw);
732