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