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