• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Marvell Wireless LAN device driver: major functions
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 "main.h"
21 #include "wmm.h"
22 #include "cfg80211.h"
23 #include "11n.h"
24 
25 #define VERSION	"1.0"
26 
27 const char driver_version[] = "mwifiex " VERSION " (%s) ";
28 static char *cal_data_cfg;
29 module_param(cal_data_cfg, charp, 0);
30 
31 /*
32  * This function registers the device and performs all the necessary
33  * initializations.
34  *
35  * The following initialization operations are performed -
36  *      - Allocate adapter structure
37  *      - Save interface specific operations table in adapter
38  *      - Call interface specific initialization routine
39  *      - Allocate private structures
40  *      - Set default adapter structure parameters
41  *      - Initialize locks
42  *
43  * In case of any errors during inittialization, this function also ensures
44  * proper cleanup before exiting.
45  */
mwifiex_register(void * card,struct mwifiex_if_ops * if_ops,void ** padapter)46 static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
47 			    void **padapter)
48 {
49 	struct mwifiex_adapter *adapter;
50 	int i;
51 
52 	adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
53 	if (!adapter)
54 		return -ENOMEM;
55 
56 	*padapter = adapter;
57 	adapter->card = card;
58 
59 	/* Save interface specific operations in adapter */
60 	memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
61 
62 	/* card specific initialization has been deferred until now .. */
63 	if (adapter->if_ops.init_if)
64 		if (adapter->if_ops.init_if(adapter))
65 			goto error;
66 
67 	adapter->priv_num = 0;
68 
69 	for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
70 		/* Allocate memory for private structure */
71 		adapter->priv[i] =
72 			kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
73 		if (!adapter->priv[i])
74 			goto error;
75 
76 		adapter->priv[i]->adapter = adapter;
77 		adapter->priv_num++;
78 	}
79 	mwifiex_init_lock_list(adapter);
80 
81 	init_timer(&adapter->cmd_timer);
82 	adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
83 	adapter->cmd_timer.data = (unsigned long) adapter;
84 
85 	return 0;
86 
87 error:
88 	dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
89 
90 	for (i = 0; i < adapter->priv_num; i++)
91 		kfree(adapter->priv[i]);
92 
93 	kfree(adapter);
94 
95 	return -1;
96 }
97 
98 /*
99  * This function unregisters the device and performs all the necessary
100  * cleanups.
101  *
102  * The following cleanup operations are performed -
103  *      - Free the timers
104  *      - Free beacon buffers
105  *      - Free private structures
106  *      - Free adapter structure
107  */
mwifiex_unregister(struct mwifiex_adapter * adapter)108 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
109 {
110 	s32 i;
111 
112 	if (adapter->if_ops.cleanup_if)
113 		adapter->if_ops.cleanup_if(adapter);
114 
115 	del_timer_sync(&adapter->cmd_timer);
116 
117 	/* Free private structures */
118 	for (i = 0; i < adapter->priv_num; i++) {
119 		if (adapter->priv[i]) {
120 			mwifiex_free_curr_bcn(adapter->priv[i]);
121 			kfree(adapter->priv[i]);
122 		}
123 	}
124 
125 	kfree(adapter);
126 	return 0;
127 }
128 
mwifiex_process_rx(struct mwifiex_adapter * adapter)129 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
130 {
131 	unsigned long flags;
132 	struct sk_buff *skb;
133 
134 	spin_lock_irqsave(&adapter->rx_proc_lock, flags);
135 	if (adapter->rx_processing || adapter->rx_locked) {
136 		spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
137 		goto exit_rx_proc;
138 	} else {
139 		adapter->rx_processing = true;
140 		spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
141 	}
142 
143 	/* Check for Rx data */
144 	while ((skb = skb_dequeue(&adapter->rx_data_q))) {
145 		atomic_dec(&adapter->rx_pending);
146 		if (adapter->delay_main_work &&
147 		    (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
148 			adapter->delay_main_work = false;
149 			queue_work(adapter->workqueue, &adapter->main_work);
150 		}
151 		mwifiex_handle_rx_packet(adapter, skb);
152 	}
153 	spin_lock_irqsave(&adapter->rx_proc_lock, flags);
154 	adapter->rx_processing = false;
155 	spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
156 
157 exit_rx_proc:
158 	return 0;
159 }
160 
161 /*
162  * The main process.
163  *
164  * This function is the main procedure of the driver and handles various driver
165  * operations. It runs in a loop and provides the core functionalities.
166  *
167  * The main responsibilities of this function are -
168  *      - Ensure concurrency control
169  *      - Handle pending interrupts and call interrupt handlers
170  *      - Wake up the card if required
171  *      - Handle command responses and call response handlers
172  *      - Handle events and call event handlers
173  *      - Execute pending commands
174  *      - Transmit pending data packets
175  */
mwifiex_main_process(struct mwifiex_adapter * adapter)176 int mwifiex_main_process(struct mwifiex_adapter *adapter)
177 {
178 	int ret = 0;
179 	unsigned long flags;
180 	struct sk_buff *skb;
181 
182 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
183 
184 	/* Check if already processing */
185 	if (adapter->mwifiex_processing) {
186 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
187 		goto exit_main_proc;
188 	} else {
189 		adapter->mwifiex_processing = true;
190 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
191 	}
192 process_start:
193 	do {
194 		if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
195 		    (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
196 			break;
197 
198 		/* If we process interrupts first, it would increase RX pending
199 		 * even further. Avoid this by checking if rx_pending has
200 		 * crossed high threshold and schedule rx work queue
201 		 * and then process interrupts
202 		 */
203 		if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
204 			adapter->delay_main_work = true;
205 			if (!adapter->rx_processing)
206 				queue_work(adapter->rx_workqueue,
207 					   &adapter->rx_work);
208 			break;
209 		}
210 
211 		/* Handle pending interrupt if any */
212 		if (adapter->int_status) {
213 			if (adapter->hs_activated)
214 				mwifiex_process_hs_config(adapter);
215 			if (adapter->if_ops.process_int_status)
216 				adapter->if_ops.process_int_status(adapter);
217 		}
218 
219 		if (adapter->rx_work_enabled && adapter->data_received)
220 			queue_work(adapter->rx_workqueue, &adapter->rx_work);
221 
222 		/* Need to wake up the card ? */
223 		if ((adapter->ps_state == PS_STATE_SLEEP) &&
224 		    (adapter->pm_wakeup_card_req &&
225 		     !adapter->pm_wakeup_fw_try) &&
226 		    (is_command_pending(adapter) ||
227 		     !mwifiex_wmm_lists_empty(adapter))) {
228 			adapter->pm_wakeup_fw_try = true;
229 			adapter->if_ops.wakeup(adapter);
230 			continue;
231 		}
232 
233 		if (IS_CARD_RX_RCVD(adapter)) {
234 			adapter->data_received = false;
235 			adapter->pm_wakeup_fw_try = false;
236 			if (adapter->ps_state == PS_STATE_SLEEP)
237 				adapter->ps_state = PS_STATE_AWAKE;
238 		} else {
239 			/* We have tried to wakeup the card already */
240 			if (adapter->pm_wakeup_fw_try)
241 				break;
242 			if (adapter->ps_state != PS_STATE_AWAKE ||
243 			    adapter->tx_lock_flag)
244 				break;
245 
246 			if ((!adapter->scan_chan_gap_enabled &&
247 			     adapter->scan_processing) || adapter->data_sent ||
248 			    mwifiex_wmm_lists_empty(adapter)) {
249 				if (adapter->cmd_sent || adapter->curr_cmd ||
250 				    (!is_command_pending(adapter)))
251 					break;
252 			}
253 		}
254 
255 		/* Check Rx data for USB */
256 		if (adapter->iface_type == MWIFIEX_USB)
257 			while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
258 				mwifiex_handle_rx_packet(adapter, skb);
259 
260 		/* Check for event */
261 		if (adapter->event_received) {
262 			adapter->event_received = false;
263 			mwifiex_process_event(adapter);
264 		}
265 
266 		/* Check for Cmd Resp */
267 		if (adapter->cmd_resp_received) {
268 			adapter->cmd_resp_received = false;
269 			mwifiex_process_cmdresp(adapter);
270 
271 			/* call mwifiex back when init_fw is done */
272 			if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
273 				adapter->hw_status = MWIFIEX_HW_STATUS_READY;
274 				mwifiex_init_fw_complete(adapter);
275 			}
276 		}
277 
278 		/* Check if we need to confirm Sleep Request
279 		   received previously */
280 		if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
281 			if (!adapter->cmd_sent && !adapter->curr_cmd)
282 				mwifiex_check_ps_cond(adapter);
283 		}
284 
285 		/* * The ps_state may have been changed during processing of
286 		 * Sleep Request event.
287 		 */
288 		if ((adapter->ps_state == PS_STATE_SLEEP) ||
289 		    (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
290 		    (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
291 		    adapter->tx_lock_flag)
292 			continue;
293 
294 		if (!adapter->cmd_sent && !adapter->curr_cmd) {
295 			if (mwifiex_exec_next_cmd(adapter) == -1) {
296 				ret = -1;
297 				break;
298 			}
299 		}
300 
301 		if ((adapter->scan_chan_gap_enabled ||
302 		     !adapter->scan_processing) &&
303 		    !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
304 			mwifiex_wmm_process_tx(adapter);
305 			if (adapter->hs_activated) {
306 				adapter->is_hs_configured = false;
307 				mwifiex_hs_activated_event
308 					(mwifiex_get_priv
309 					 (adapter, MWIFIEX_BSS_ROLE_ANY),
310 					 false);
311 			}
312 		}
313 
314 		if (adapter->delay_null_pkt && !adapter->cmd_sent &&
315 		    !adapter->curr_cmd && !is_command_pending(adapter) &&
316 		    mwifiex_wmm_lists_empty(adapter)) {
317 			if (!mwifiex_send_null_packet
318 			    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
319 			     MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
320 			     MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
321 				adapter->delay_null_pkt = false;
322 				adapter->ps_state = PS_STATE_SLEEP;
323 			}
324 			break;
325 		}
326 	} while (true);
327 
328 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
329 	if (!adapter->delay_main_work &&
330 	    (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
331 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
332 		goto process_start;
333 	}
334 
335 	adapter->mwifiex_processing = false;
336 	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
337 
338 exit_main_proc:
339 	if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
340 		mwifiex_shutdown_drv(adapter);
341 	return ret;
342 }
343 EXPORT_SYMBOL_GPL(mwifiex_main_process);
344 
345 /*
346  * This function frees the adapter structure.
347  *
348  * Additionally, this closes the netlink socket, frees the timers
349  * and private structures.
350  */
mwifiex_free_adapter(struct mwifiex_adapter * adapter)351 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
352 {
353 	if (!adapter) {
354 		pr_err("%s: adapter is NULL\n", __func__);
355 		return;
356 	}
357 
358 	mwifiex_unregister(adapter);
359 	pr_debug("info: %s: free adapter\n", __func__);
360 }
361 
362 /*
363  * This function cancels all works in the queue and destroys
364  * the main workqueue.
365  */
mwifiex_terminate_workqueue(struct mwifiex_adapter * adapter)366 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
367 {
368 	flush_workqueue(adapter->workqueue);
369 	destroy_workqueue(adapter->workqueue);
370 	adapter->workqueue = NULL;
371 
372 	if (adapter->rx_workqueue) {
373 		flush_workqueue(adapter->rx_workqueue);
374 		destroy_workqueue(adapter->rx_workqueue);
375 		adapter->rx_workqueue = NULL;
376 	}
377 }
378 
379 /*
380  * This function gets firmware and initializes it.
381  *
382  * The main initialization steps followed are -
383  *      - Download the correct firmware to card
384  *      - Issue the init commands to firmware
385  */
mwifiex_fw_dpc(const struct firmware * firmware,void * context)386 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
387 {
388 	int ret;
389 	char fmt[64];
390 	struct mwifiex_private *priv;
391 	struct mwifiex_adapter *adapter = context;
392 	struct mwifiex_fw_image fw;
393 	struct semaphore *sem = adapter->card_sem;
394 	bool init_failed = false;
395 	struct wireless_dev *wdev;
396 
397 	if (!firmware) {
398 		dev_err(adapter->dev,
399 			"Failed to get firmware %s\n", adapter->fw_name);
400 		goto err_dnld_fw;
401 	}
402 
403 	memset(&fw, 0, sizeof(struct mwifiex_fw_image));
404 	adapter->firmware = firmware;
405 	fw.fw_buf = (u8 *) adapter->firmware->data;
406 	fw.fw_len = adapter->firmware->size;
407 
408 	if (adapter->if_ops.dnld_fw)
409 		ret = adapter->if_ops.dnld_fw(adapter, &fw);
410 	else
411 		ret = mwifiex_dnld_fw(adapter, &fw);
412 	if (ret == -1)
413 		goto err_dnld_fw;
414 
415 	dev_notice(adapter->dev, "WLAN FW is active\n");
416 
417 	if (cal_data_cfg) {
418 		if ((request_firmware(&adapter->cal_data, cal_data_cfg,
419 				      adapter->dev)) < 0)
420 			dev_err(adapter->dev,
421 				"Cal data request_firmware() failed\n");
422 	}
423 
424 	/* enable host interrupt after fw dnld is successful */
425 	if (adapter->if_ops.enable_int) {
426 		if (adapter->if_ops.enable_int(adapter))
427 			goto err_dnld_fw;
428 	}
429 
430 	adapter->init_wait_q_woken = false;
431 	ret = mwifiex_init_fw(adapter);
432 	if (ret == -1) {
433 		goto err_init_fw;
434 	} else if (!ret) {
435 		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
436 		goto done;
437 	}
438 	/* Wait for mwifiex_init to complete */
439 	wait_event_interruptible(adapter->init_wait_q,
440 				 adapter->init_wait_q_woken);
441 	if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
442 		goto err_init_fw;
443 
444 	priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
445 	if (mwifiex_register_cfg80211(adapter)) {
446 		dev_err(adapter->dev, "cannot register with cfg80211\n");
447 		goto err_init_fw;
448 	}
449 
450 	rtnl_lock();
451 	/* Create station interface by default */
452 	wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
453 					NL80211_IFTYPE_STATION, NULL, NULL);
454 	if (IS_ERR(wdev)) {
455 		dev_err(adapter->dev, "cannot create default STA interface\n");
456 		rtnl_unlock();
457 		goto err_add_intf;
458 	}
459 	rtnl_unlock();
460 
461 	mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
462 	dev_notice(adapter->dev, "driver_version = %s\n", fmt);
463 	goto done;
464 
465 err_add_intf:
466 	wiphy_unregister(adapter->wiphy);
467 	wiphy_free(adapter->wiphy);
468 err_init_fw:
469 	if (adapter->if_ops.disable_int)
470 		adapter->if_ops.disable_int(adapter);
471 err_dnld_fw:
472 	pr_debug("info: %s: unregister device\n", __func__);
473 	if (adapter->if_ops.unregister_dev)
474 		adapter->if_ops.unregister_dev(adapter);
475 
476 	if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
477 	    (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
478 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
479 		adapter->init_wait_q_woken = false;
480 
481 		if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
482 			wait_event_interruptible(adapter->init_wait_q,
483 						 adapter->init_wait_q_woken);
484 	}
485 	adapter->surprise_removed = true;
486 	mwifiex_terminate_workqueue(adapter);
487 	init_failed = true;
488 done:
489 	if (adapter->cal_data) {
490 		release_firmware(adapter->cal_data);
491 		adapter->cal_data = NULL;
492 	}
493 	if (adapter->firmware) {
494 		release_firmware(adapter->firmware);
495 		adapter->firmware = NULL;
496 	}
497 	if (init_failed)
498 		mwifiex_free_adapter(adapter);
499 	up(sem);
500 	return;
501 }
502 
503 /*
504  * This function initializes the hardware and gets firmware.
505  */
mwifiex_init_hw_fw(struct mwifiex_adapter * adapter)506 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
507 {
508 	int ret;
509 
510 	ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
511 				      adapter->dev, GFP_KERNEL, adapter,
512 				      mwifiex_fw_dpc);
513 	if (ret < 0)
514 		dev_err(adapter->dev,
515 			"request_firmware_nowait() returned error %d\n", ret);
516 	return ret;
517 }
518 
519 /*
520  * CFG802.11 network device handler for open.
521  *
522  * Starts the data queue.
523  */
524 static int
mwifiex_open(struct net_device * dev)525 mwifiex_open(struct net_device *dev)
526 {
527 	netif_tx_start_all_queues(dev);
528 	return 0;
529 }
530 
531 /*
532  * CFG802.11 network device handler for close.
533  */
534 static int
mwifiex_close(struct net_device * dev)535 mwifiex_close(struct net_device *dev)
536 {
537 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
538 
539 	if (priv->scan_request) {
540 		dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
541 		cfg80211_scan_done(priv->scan_request, 1);
542 		priv->scan_request = NULL;
543 		priv->scan_aborting = true;
544 	}
545 
546 	return 0;
547 }
548 
549 /*
550  * Add buffer into wmm tx queue and queue work to transmit it.
551  */
mwifiex_queue_tx_pkt(struct mwifiex_private * priv,struct sk_buff * skb)552 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
553 {
554 	struct netdev_queue *txq;
555 	int index = mwifiex_1d_to_wmm_queue[skb->priority];
556 
557 	if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
558 		txq = netdev_get_tx_queue(priv->netdev, index);
559 		if (!netif_tx_queue_stopped(txq)) {
560 			netif_tx_stop_queue(txq);
561 			dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
562 		}
563 	}
564 
565 	atomic_inc(&priv->adapter->tx_pending);
566 	mwifiex_wmm_add_buf_txqueue(priv, skb);
567 
568 	queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
569 
570 	return 0;
571 }
572 
573 /*
574  * CFG802.11 network device handler for data transmission.
575  */
576 static int
mwifiex_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)577 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
578 {
579 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
580 	struct sk_buff *new_skb;
581 	struct mwifiex_txinfo *tx_info;
582 
583 	dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
584 		jiffies, priv->bss_type, priv->bss_num);
585 
586 	if (priv->adapter->surprise_removed) {
587 		kfree_skb(skb);
588 		priv->stats.tx_dropped++;
589 		return 0;
590 	}
591 	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
592 		dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
593 		kfree_skb(skb);
594 		priv->stats.tx_dropped++;
595 		return 0;
596 	}
597 	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
598 		dev_dbg(priv->adapter->dev,
599 			"data: Tx: insufficient skb headroom %d\n",
600 			skb_headroom(skb));
601 		/* Insufficient skb headroom - allocate a new skb */
602 		new_skb =
603 			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
604 		if (unlikely(!new_skb)) {
605 			dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
606 			kfree_skb(skb);
607 			priv->stats.tx_dropped++;
608 			return 0;
609 		}
610 		kfree_skb(skb);
611 		skb = new_skb;
612 		dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
613 			skb_headroom(skb));
614 	}
615 
616 	tx_info = MWIFIEX_SKB_TXCB(skb);
617 	memset(tx_info, 0, sizeof(*tx_info));
618 	tx_info->bss_num = priv->bss_num;
619 	tx_info->bss_type = priv->bss_type;
620 	tx_info->pkt_len = skb->len;
621 
622 	/* Record the current time the packet was queued; used to
623 	 * determine the amount of time the packet was queued in
624 	 * the driver before it was sent to the firmware.
625 	 * The delay is then sent along with the packet to the
626 	 * firmware for aggregate delay calculation for stats and
627 	 * MSDU lifetime expiry.
628 	 */
629 	__net_timestamp(skb);
630 
631 	mwifiex_queue_tx_pkt(priv, skb);
632 
633 	return 0;
634 }
635 
636 /*
637  * CFG802.11 network device handler for setting MAC address.
638  */
639 static int
mwifiex_set_mac_address(struct net_device * dev,void * addr)640 mwifiex_set_mac_address(struct net_device *dev, void *addr)
641 {
642 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
643 	struct sockaddr *hw_addr = addr;
644 	int ret;
645 
646 	memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
647 
648 	/* Send request to firmware */
649 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
650 			       HostCmd_ACT_GEN_SET, 0, NULL, true);
651 
652 	if (!ret)
653 		memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
654 	else
655 		dev_err(priv->adapter->dev,
656 			"set mac address failed: ret=%d\n", ret);
657 
658 	memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
659 
660 	return ret;
661 }
662 
663 /*
664  * CFG802.11 network device handler for setting multicast list.
665  */
mwifiex_set_multicast_list(struct net_device * dev)666 static void mwifiex_set_multicast_list(struct net_device *dev)
667 {
668 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
669 	struct mwifiex_multicast_list mcast_list;
670 
671 	if (dev->flags & IFF_PROMISC) {
672 		mcast_list.mode = MWIFIEX_PROMISC_MODE;
673 	} else if (dev->flags & IFF_ALLMULTI ||
674 		   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
675 		mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
676 	} else {
677 		mcast_list.mode = MWIFIEX_MULTICAST_MODE;
678 		mcast_list.num_multicast_addr =
679 			mwifiex_copy_mcast_addr(&mcast_list, dev);
680 	}
681 	mwifiex_request_set_multicast_list(priv, &mcast_list);
682 }
683 
684 /*
685  * CFG802.11 network device handler for transmission timeout.
686  */
687 static void
mwifiex_tx_timeout(struct net_device * dev)688 mwifiex_tx_timeout(struct net_device *dev)
689 {
690 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
691 
692 	priv->num_tx_timeout++;
693 	priv->tx_timeout_cnt++;
694 	dev_err(priv->adapter->dev,
695 		"%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
696 		jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
697 	mwifiex_set_trans_start(dev);
698 
699 	if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
700 	    priv->adapter->if_ops.card_reset) {
701 		dev_err(priv->adapter->dev,
702 			"tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
703 		priv->adapter->if_ops.card_reset(priv->adapter);
704 	}
705 }
706 
707 /*
708  * CFG802.11 network device handler for statistics retrieval.
709  */
mwifiex_get_stats(struct net_device * dev)710 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
711 {
712 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
713 
714 	return &priv->stats;
715 }
716 
717 static u16
mwifiex_netdev_select_wmm_queue(struct net_device * dev,struct sk_buff * skb,void * accel_priv,select_queue_fallback_t fallback)718 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
719 				void *accel_priv, select_queue_fallback_t fallback)
720 {
721 	skb->priority = cfg80211_classify8021d(skb, NULL);
722 	return mwifiex_1d_to_wmm_queue[skb->priority];
723 }
724 
725 /* Network device handlers */
726 static const struct net_device_ops mwifiex_netdev_ops = {
727 	.ndo_open = mwifiex_open,
728 	.ndo_stop = mwifiex_close,
729 	.ndo_start_xmit = mwifiex_hard_start_xmit,
730 	.ndo_set_mac_address = mwifiex_set_mac_address,
731 	.ndo_tx_timeout = mwifiex_tx_timeout,
732 	.ndo_get_stats = mwifiex_get_stats,
733 	.ndo_set_rx_mode = mwifiex_set_multicast_list,
734 	.ndo_select_queue = mwifiex_netdev_select_wmm_queue,
735 };
736 
737 /*
738  * This function initializes the private structure parameters.
739  *
740  * The following wait queues are initialized -
741  *      - IOCTL wait queue
742  *      - Command wait queue
743  *      - Statistics wait queue
744  *
745  * ...and the following default parameters are set -
746  *      - Current key index     : Set to 0
747  *      - Rate index            : Set to auto
748  *      - Media connected       : Set to disconnected
749  *      - Adhoc link sensed     : Set to false
750  *      - Nick name             : Set to null
751  *      - Number of Tx timeout  : Set to 0
752  *      - Device address        : Set to current address
753  *
754  * In addition, the CFG80211 work queue is also created.
755  */
mwifiex_init_priv_params(struct mwifiex_private * priv,struct net_device * dev)756 void mwifiex_init_priv_params(struct mwifiex_private *priv,
757 						struct net_device *dev)
758 {
759 	dev->netdev_ops = &mwifiex_netdev_ops;
760 	dev->destructor = free_netdev;
761 	/* Initialize private structure */
762 	priv->current_key_index = 0;
763 	priv->media_connected = false;
764 	memset(&priv->nick_name, 0, sizeof(priv->nick_name));
765 	memset(priv->mgmt_ie, 0,
766 	       sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
767 	priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
768 	priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
769 	priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
770 	priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
771 	priv->num_tx_timeout = 0;
772 	memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
773 }
774 
775 /*
776  * This function check if command is pending.
777  */
is_command_pending(struct mwifiex_adapter * adapter)778 int is_command_pending(struct mwifiex_adapter *adapter)
779 {
780 	unsigned long flags;
781 	int is_cmd_pend_q_empty;
782 
783 	spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
784 	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
785 	spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
786 
787 	return !is_cmd_pend_q_empty;
788 }
789 
790 /*
791  * This is the RX work queue function.
792  *
793  * It handles the RX operations.
794  */
mwifiex_rx_work_queue(struct work_struct * work)795 static void mwifiex_rx_work_queue(struct work_struct *work)
796 {
797 	struct mwifiex_adapter *adapter =
798 		container_of(work, struct mwifiex_adapter, rx_work);
799 
800 	if (adapter->surprise_removed)
801 		return;
802 	mwifiex_process_rx(adapter);
803 }
804 
805 /*
806  * This is the main work queue function.
807  *
808  * It handles the main process, which in turn handles the complete
809  * driver operations.
810  */
mwifiex_main_work_queue(struct work_struct * work)811 static void mwifiex_main_work_queue(struct work_struct *work)
812 {
813 	struct mwifiex_adapter *adapter =
814 		container_of(work, struct mwifiex_adapter, main_work);
815 
816 	if (adapter->surprise_removed)
817 		return;
818 	mwifiex_main_process(adapter);
819 }
820 
821 /*
822  * This function adds the card.
823  *
824  * This function follows the following major steps to set up the device -
825  *      - Initialize software. This includes probing the card, registering
826  *        the interface operations table, and allocating/initializing the
827  *        adapter structure
828  *      - Set up the netlink socket
829  *      - Create and start the main work queue
830  *      - Register the device
831  *      - Initialize firmware and hardware
832  *      - Add logical interfaces
833  */
834 int
mwifiex_add_card(void * card,struct semaphore * sem,struct mwifiex_if_ops * if_ops,u8 iface_type)835 mwifiex_add_card(void *card, struct semaphore *sem,
836 		 struct mwifiex_if_ops *if_ops, u8 iface_type)
837 {
838 	struct mwifiex_adapter *adapter;
839 
840 	if (down_interruptible(sem))
841 		goto exit_sem_err;
842 
843 	if (mwifiex_register(card, if_ops, (void **)&adapter)) {
844 		pr_err("%s: software init failed\n", __func__);
845 		goto err_init_sw;
846 	}
847 
848 	adapter->iface_type = iface_type;
849 	adapter->card_sem = sem;
850 
851 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
852 	adapter->surprise_removed = false;
853 	init_waitqueue_head(&adapter->init_wait_q);
854 	adapter->is_suspended = false;
855 	adapter->hs_activated = false;
856 	init_waitqueue_head(&adapter->hs_activate_wait_q);
857 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
858 	adapter->cmd_wait_q.status = 0;
859 	adapter->scan_wait_q_woken = false;
860 
861 	if (num_possible_cpus() > 1) {
862 		adapter->rx_work_enabled = true;
863 		pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
864 	}
865 
866 	adapter->workqueue =
867 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
868 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
869 	if (!adapter->workqueue)
870 		goto err_kmalloc;
871 
872 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
873 
874 	if (adapter->rx_work_enabled) {
875 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
876 							WQ_HIGHPRI |
877 							WQ_MEM_RECLAIM |
878 							WQ_UNBOUND, 1);
879 		if (!adapter->rx_workqueue)
880 			goto err_kmalloc;
881 
882 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
883 	}
884 
885 	if (adapter->if_ops.iface_work)
886 		INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
887 
888 	/* Register the device. Fill up the private data structure with relevant
889 	   information from the card. */
890 	if (adapter->if_ops.register_dev(adapter)) {
891 		pr_err("%s: failed to register mwifiex device\n", __func__);
892 		goto err_registerdev;
893 	}
894 
895 	if (mwifiex_init_hw_fw(adapter)) {
896 		pr_err("%s: firmware init failed\n", __func__);
897 		goto err_init_fw;
898 	}
899 
900 	return 0;
901 
902 err_init_fw:
903 	pr_debug("info: %s: unregister device\n", __func__);
904 	if (adapter->if_ops.unregister_dev)
905 		adapter->if_ops.unregister_dev(adapter);
906 	if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
907 	    (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
908 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
909 		adapter->init_wait_q_woken = false;
910 
911 		if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
912 			wait_event_interruptible(adapter->init_wait_q,
913 						 adapter->init_wait_q_woken);
914 	}
915 err_registerdev:
916 	adapter->surprise_removed = true;
917 	mwifiex_terminate_workqueue(adapter);
918 err_kmalloc:
919 	mwifiex_free_adapter(adapter);
920 
921 err_init_sw:
922 	up(sem);
923 
924 exit_sem_err:
925 	return -1;
926 }
927 EXPORT_SYMBOL_GPL(mwifiex_add_card);
928 
929 /*
930  * This function removes the card.
931  *
932  * This function follows the following major steps to remove the device -
933  *      - Stop data traffic
934  *      - Shutdown firmware
935  *      - Remove the logical interfaces
936  *      - Terminate the work queue
937  *      - Unregister the device
938  *      - Free the adapter structure
939  */
mwifiex_remove_card(struct mwifiex_adapter * adapter,struct semaphore * sem)940 int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
941 {
942 	struct mwifiex_private *priv = NULL;
943 	int i;
944 
945 	if (down_interruptible(sem))
946 		goto exit_sem_err;
947 
948 	if (!adapter)
949 		goto exit_remove;
950 
951 	/* We can no longer handle interrupts once we start doing the teardown
952 	 * below. */
953 	if (adapter->if_ops.disable_int)
954 		adapter->if_ops.disable_int(adapter);
955 
956 	adapter->surprise_removed = true;
957 
958 	/* Stop data */
959 	for (i = 0; i < adapter->priv_num; i++) {
960 		priv = adapter->priv[i];
961 		if (priv && priv->netdev) {
962 			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
963 			if (netif_carrier_ok(priv->netdev))
964 				netif_carrier_off(priv->netdev);
965 		}
966 	}
967 
968 	dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
969 	adapter->init_wait_q_woken = false;
970 
971 	if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
972 		wait_event_interruptible(adapter->init_wait_q,
973 					 adapter->init_wait_q_woken);
974 	dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
975 	if (atomic_read(&adapter->rx_pending) ||
976 	    atomic_read(&adapter->tx_pending) ||
977 	    atomic_read(&adapter->cmd_pending)) {
978 		dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
979 		       "cmd_pending=%d\n",
980 		       atomic_read(&adapter->rx_pending),
981 		       atomic_read(&adapter->tx_pending),
982 		       atomic_read(&adapter->cmd_pending));
983 	}
984 
985 	for (i = 0; i < adapter->priv_num; i++) {
986 		priv = adapter->priv[i];
987 
988 		if (!priv)
989 			continue;
990 
991 		rtnl_lock();
992 		if (priv->wdev && priv->netdev)
993 			mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
994 		rtnl_unlock();
995 	}
996 
997 	wiphy_unregister(adapter->wiphy);
998 	wiphy_free(adapter->wiphy);
999 
1000 	mwifiex_terminate_workqueue(adapter);
1001 
1002 	/* Unregister device */
1003 	dev_dbg(adapter->dev, "info: unregister device\n");
1004 	if (adapter->if_ops.unregister_dev)
1005 		adapter->if_ops.unregister_dev(adapter);
1006 	/* Free adapter structure */
1007 	dev_dbg(adapter->dev, "info: free adapter\n");
1008 	mwifiex_free_adapter(adapter);
1009 
1010 exit_remove:
1011 	up(sem);
1012 exit_sem_err:
1013 	return 0;
1014 }
1015 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1016 
1017 /*
1018  * This function initializes the module.
1019  *
1020  * The debug FS is also initialized if configured.
1021  */
1022 static int
mwifiex_init_module(void)1023 mwifiex_init_module(void)
1024 {
1025 #ifdef CONFIG_DEBUG_FS
1026 	mwifiex_debugfs_init();
1027 #endif
1028 	return 0;
1029 }
1030 
1031 /*
1032  * This function cleans up the module.
1033  *
1034  * The debug FS is removed if available.
1035  */
1036 static void
mwifiex_cleanup_module(void)1037 mwifiex_cleanup_module(void)
1038 {
1039 #ifdef CONFIG_DEBUG_FS
1040 	mwifiex_debugfs_remove();
1041 #endif
1042 }
1043 
1044 module_init(mwifiex_init_module);
1045 module_exit(mwifiex_cleanup_module);
1046 
1047 MODULE_AUTHOR("Marvell International Ltd.");
1048 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1049 MODULE_VERSION(VERSION);
1050 MODULE_LICENSE("GPL v2");
1051