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