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 <linux/suspend.h>
21 
22 #include "main.h"
23 #include "wmm.h"
24 #include "cfg80211.h"
25 #include "11n.h"
26 
27 #define VERSION	"1.0"
28 #define MFG_FIRMWARE	"mwifiex_mfg.bin"
29 
30 static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
31 module_param(debug_mask, uint, 0);
32 MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
33 
34 const char driver_version[] = "mwifiex " VERSION " (%s) ";
35 static char *cal_data_cfg;
36 module_param(cal_data_cfg, charp, 0);
37 
38 static unsigned short driver_mode;
39 module_param(driver_mode, ushort, 0);
40 MODULE_PARM_DESC(driver_mode,
41 		 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
42 
43 bool mfg_mode;
44 module_param(mfg_mode, bool, 0);
45 MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
46 
47 bool aggr_ctrl;
48 module_param(aggr_ctrl, bool, 0000);
49 MODULE_PARM_DESC(aggr_ctrl, "usb tx aggregation enable:1, disable:0");
50 
51 /*
52  * This function registers the device and performs all the necessary
53  * initializations.
54  *
55  * The following initialization operations are performed -
56  *      - Allocate adapter structure
57  *      - Save interface specific operations table in adapter
58  *      - Call interface specific initialization routine
59  *      - Allocate private structures
60  *      - Set default adapter structure parameters
61  *      - Initialize locks
62  *
63  * In case of any errors during inittialization, this function also ensures
64  * proper cleanup before exiting.
65  */
mwifiex_register(void * card,struct device * dev,struct mwifiex_if_ops * if_ops,void ** padapter)66 static int mwifiex_register(void *card, struct device *dev,
67 			    struct mwifiex_if_ops *if_ops, void **padapter)
68 {
69 	struct mwifiex_adapter *adapter;
70 	int i;
71 
72 	adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
73 	if (!adapter)
74 		return -ENOMEM;
75 
76 	*padapter = adapter;
77 	adapter->dev = dev;
78 	adapter->card = card;
79 
80 	/* Save interface specific operations in adapter */
81 	memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
82 	adapter->debug_mask = debug_mask;
83 
84 	/* card specific initialization has been deferred until now .. */
85 	if (adapter->if_ops.init_if)
86 		if (adapter->if_ops.init_if(adapter))
87 			goto error;
88 
89 	adapter->priv_num = 0;
90 
91 	for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
92 		/* Allocate memory for private structure */
93 		adapter->priv[i] =
94 			kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
95 		if (!adapter->priv[i])
96 			goto error;
97 
98 		adapter->priv[i]->adapter = adapter;
99 		adapter->priv_num++;
100 	}
101 	mwifiex_init_lock_list(adapter);
102 
103 	timer_setup(&adapter->cmd_timer, mwifiex_cmd_timeout_func, 0);
104 
105 	return 0;
106 
107 error:
108 	mwifiex_dbg(adapter, ERROR,
109 		    "info: leave mwifiex_register with error\n");
110 
111 	for (i = 0; i < adapter->priv_num; i++)
112 		kfree(adapter->priv[i]);
113 
114 	kfree(adapter);
115 
116 	return -1;
117 }
118 
119 /*
120  * This function unregisters the device and performs all the necessary
121  * cleanups.
122  *
123  * The following cleanup operations are performed -
124  *      - Free the timers
125  *      - Free beacon buffers
126  *      - Free private structures
127  *      - Free adapter structure
128  */
mwifiex_unregister(struct mwifiex_adapter * adapter)129 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
130 {
131 	s32 i;
132 
133 	if (adapter->if_ops.cleanup_if)
134 		adapter->if_ops.cleanup_if(adapter);
135 
136 	del_timer_sync(&adapter->cmd_timer);
137 
138 	/* Free private structures */
139 	for (i = 0; i < adapter->priv_num; i++) {
140 		if (adapter->priv[i]) {
141 			mwifiex_free_curr_bcn(adapter->priv[i]);
142 			kfree(adapter->priv[i]);
143 		}
144 	}
145 
146 	if (adapter->nd_info) {
147 		for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
148 			kfree(adapter->nd_info->matches[i]);
149 		kfree(adapter->nd_info);
150 		adapter->nd_info = NULL;
151 	}
152 
153 	kfree(adapter->regd);
154 
155 	kfree(adapter);
156 	return 0;
157 }
158 
mwifiex_queue_main_work(struct mwifiex_adapter * adapter)159 void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
160 {
161 	unsigned long flags;
162 
163 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
164 	if (adapter->mwifiex_processing) {
165 		adapter->more_task_flag = true;
166 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
167 	} else {
168 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
169 		queue_work(adapter->workqueue, &adapter->main_work);
170 	}
171 }
172 EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
173 
mwifiex_queue_rx_work(struct mwifiex_adapter * adapter)174 static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
175 {
176 	spin_lock_bh(&adapter->rx_proc_lock);
177 	if (adapter->rx_processing) {
178 		spin_unlock_bh(&adapter->rx_proc_lock);
179 	} else {
180 		spin_unlock_bh(&adapter->rx_proc_lock);
181 		queue_work(adapter->rx_workqueue, &adapter->rx_work);
182 	}
183 }
184 
mwifiex_process_rx(struct mwifiex_adapter * adapter)185 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
186 {
187 	struct sk_buff *skb;
188 	struct mwifiex_rxinfo *rx_info;
189 
190 	spin_lock_bh(&adapter->rx_proc_lock);
191 	if (adapter->rx_processing || adapter->rx_locked) {
192 		spin_unlock_bh(&adapter->rx_proc_lock);
193 		goto exit_rx_proc;
194 	} else {
195 		adapter->rx_processing = true;
196 		spin_unlock_bh(&adapter->rx_proc_lock);
197 	}
198 
199 	/* Check for Rx data */
200 	while ((skb = skb_dequeue(&adapter->rx_data_q))) {
201 		atomic_dec(&adapter->rx_pending);
202 		if ((adapter->delay_main_work ||
203 		     adapter->iface_type == MWIFIEX_USB) &&
204 		    (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
205 			if (adapter->if_ops.submit_rem_rx_urbs)
206 				adapter->if_ops.submit_rem_rx_urbs(adapter);
207 			adapter->delay_main_work = false;
208 			mwifiex_queue_main_work(adapter);
209 		}
210 		rx_info = MWIFIEX_SKB_RXCB(skb);
211 		if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
212 			if (adapter->if_ops.deaggr_pkt)
213 				adapter->if_ops.deaggr_pkt(adapter, skb);
214 			dev_kfree_skb_any(skb);
215 		} else {
216 			mwifiex_handle_rx_packet(adapter, skb);
217 		}
218 	}
219 	spin_lock_bh(&adapter->rx_proc_lock);
220 	adapter->rx_processing = false;
221 	spin_unlock_bh(&adapter->rx_proc_lock);
222 
223 exit_rx_proc:
224 	return 0;
225 }
226 
227 /*
228  * The main process.
229  *
230  * This function is the main procedure of the driver and handles various driver
231  * operations. It runs in a loop and provides the core functionalities.
232  *
233  * The main responsibilities of this function are -
234  *      - Ensure concurrency control
235  *      - Handle pending interrupts and call interrupt handlers
236  *      - Wake up the card if required
237  *      - Handle command responses and call response handlers
238  *      - Handle events and call event handlers
239  *      - Execute pending commands
240  *      - Transmit pending data packets
241  */
mwifiex_main_process(struct mwifiex_adapter * adapter)242 int mwifiex_main_process(struct mwifiex_adapter *adapter)
243 {
244 	int ret = 0;
245 	unsigned long flags;
246 
247 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
248 
249 	/* Check if already processing */
250 	if (adapter->mwifiex_processing || adapter->main_locked) {
251 		adapter->more_task_flag = true;
252 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
253 		return 0;
254 	} else {
255 		adapter->mwifiex_processing = true;
256 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
257 	}
258 process_start:
259 	do {
260 		if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
261 			break;
262 
263 		/* For non-USB interfaces, If we process interrupts first, it
264 		 * would increase RX pending even further. Avoid this by
265 		 * checking if rx_pending has crossed high threshold and
266 		 * schedule rx work queue and then process interrupts.
267 		 * For USB interface, there are no interrupts. We already have
268 		 * HIGH_RX_PENDING check in usb.c
269 		 */
270 		if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
271 		    adapter->iface_type != MWIFIEX_USB) {
272 			adapter->delay_main_work = true;
273 			mwifiex_queue_rx_work(adapter);
274 			break;
275 		}
276 
277 		/* Handle pending interrupt if any */
278 		if (adapter->int_status) {
279 			if (adapter->hs_activated)
280 				mwifiex_process_hs_config(adapter);
281 			if (adapter->if_ops.process_int_status)
282 				adapter->if_ops.process_int_status(adapter);
283 		}
284 
285 		if (adapter->rx_work_enabled && adapter->data_received)
286 			mwifiex_queue_rx_work(adapter);
287 
288 		/* Need to wake up the card ? */
289 		if ((adapter->ps_state == PS_STATE_SLEEP) &&
290 		    (adapter->pm_wakeup_card_req &&
291 		     !adapter->pm_wakeup_fw_try) &&
292 		    (is_command_pending(adapter) ||
293 		     !skb_queue_empty(&adapter->tx_data_q) ||
294 		     !mwifiex_bypass_txlist_empty(adapter) ||
295 		     !mwifiex_wmm_lists_empty(adapter))) {
296 			adapter->pm_wakeup_fw_try = true;
297 			mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
298 			adapter->if_ops.wakeup(adapter);
299 			continue;
300 		}
301 
302 		if (IS_CARD_RX_RCVD(adapter)) {
303 			adapter->data_received = false;
304 			adapter->pm_wakeup_fw_try = false;
305 			del_timer(&adapter->wakeup_timer);
306 			if (adapter->ps_state == PS_STATE_SLEEP)
307 				adapter->ps_state = PS_STATE_AWAKE;
308 		} else {
309 			/* We have tried to wakeup the card already */
310 			if (adapter->pm_wakeup_fw_try)
311 				break;
312 			if (adapter->ps_state == PS_STATE_PRE_SLEEP)
313 				mwifiex_check_ps_cond(adapter);
314 
315 			if (adapter->ps_state != PS_STATE_AWAKE)
316 				break;
317 			if (adapter->tx_lock_flag) {
318 				if (adapter->iface_type == MWIFIEX_USB) {
319 					if (!adapter->usb_mc_setup)
320 						break;
321 				} else
322 					break;
323 			}
324 
325 			if ((!adapter->scan_chan_gap_enabled &&
326 			     adapter->scan_processing) || adapter->data_sent ||
327 			     mwifiex_is_tdls_chan_switching
328 			     (mwifiex_get_priv(adapter,
329 					       MWIFIEX_BSS_ROLE_STA)) ||
330 			    (mwifiex_wmm_lists_empty(adapter) &&
331 			     mwifiex_bypass_txlist_empty(adapter) &&
332 			     skb_queue_empty(&adapter->tx_data_q))) {
333 				if (adapter->cmd_sent || adapter->curr_cmd ||
334 					!mwifiex_is_send_cmd_allowed
335 						(mwifiex_get_priv(adapter,
336 						MWIFIEX_BSS_ROLE_STA)) ||
337 				    (!is_command_pending(adapter)))
338 					break;
339 			}
340 		}
341 
342 		/* Check for event */
343 		if (adapter->event_received) {
344 			adapter->event_received = false;
345 			mwifiex_process_event(adapter);
346 		}
347 
348 		/* Check for Cmd Resp */
349 		if (adapter->cmd_resp_received) {
350 			adapter->cmd_resp_received = false;
351 			mwifiex_process_cmdresp(adapter);
352 
353 			/* call mwifiex back when init_fw is done */
354 			if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
355 				adapter->hw_status = MWIFIEX_HW_STATUS_READY;
356 				mwifiex_init_fw_complete(adapter);
357 			}
358 		}
359 
360 		/* Check if we need to confirm Sleep Request
361 		   received previously */
362 		if (adapter->ps_state == PS_STATE_PRE_SLEEP)
363 			mwifiex_check_ps_cond(adapter);
364 
365 		/* * The ps_state may have been changed during processing of
366 		 * Sleep Request event.
367 		 */
368 		if ((adapter->ps_state == PS_STATE_SLEEP) ||
369 		    (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
370 		    (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
371 			continue;
372 		}
373 
374 		if (adapter->tx_lock_flag) {
375 			if (adapter->iface_type == MWIFIEX_USB) {
376 				if (!adapter->usb_mc_setup)
377 					continue;
378 			} else
379 				continue;
380 		}
381 
382 		if (!adapter->cmd_sent && !adapter->curr_cmd &&
383 		    mwifiex_is_send_cmd_allowed
384 		    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
385 			if (mwifiex_exec_next_cmd(adapter) == -1) {
386 				ret = -1;
387 				break;
388 			}
389 		}
390 
391 		/** If USB Multi channel setup ongoing,
392 		 *  wait for ready to tx data.
393 		 */
394 		if (adapter->iface_type == MWIFIEX_USB &&
395 		    adapter->usb_mc_setup)
396 			continue;
397 
398 		if ((adapter->scan_chan_gap_enabled ||
399 		     !adapter->scan_processing) &&
400 		    !adapter->data_sent &&
401 		    !skb_queue_empty(&adapter->tx_data_q)) {
402 			mwifiex_process_tx_queue(adapter);
403 			if (adapter->hs_activated) {
404 				clear_bit(MWIFIEX_IS_HS_CONFIGURED,
405 					  &adapter->work_flags);
406 				mwifiex_hs_activated_event
407 					(mwifiex_get_priv
408 					(adapter, MWIFIEX_BSS_ROLE_ANY),
409 					false);
410 			}
411 		}
412 
413 		if ((adapter->scan_chan_gap_enabled ||
414 		     !adapter->scan_processing) &&
415 		    !adapter->data_sent &&
416 		    !mwifiex_bypass_txlist_empty(adapter) &&
417 		    !mwifiex_is_tdls_chan_switching
418 			(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
419 			mwifiex_process_bypass_tx(adapter);
420 			if (adapter->hs_activated) {
421 				clear_bit(MWIFIEX_IS_HS_CONFIGURED,
422 					  &adapter->work_flags);
423 				mwifiex_hs_activated_event
424 					(mwifiex_get_priv
425 					 (adapter, MWIFIEX_BSS_ROLE_ANY),
426 					 false);
427 			}
428 		}
429 
430 		if ((adapter->scan_chan_gap_enabled ||
431 		     !adapter->scan_processing) &&
432 		    !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
433 		    !mwifiex_is_tdls_chan_switching
434 			(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
435 			mwifiex_wmm_process_tx(adapter);
436 			if (adapter->hs_activated) {
437 				clear_bit(MWIFIEX_IS_HS_CONFIGURED,
438 					  &adapter->work_flags);
439 				mwifiex_hs_activated_event
440 					(mwifiex_get_priv
441 					 (adapter, MWIFIEX_BSS_ROLE_ANY),
442 					 false);
443 			}
444 		}
445 
446 		if (adapter->delay_null_pkt && !adapter->cmd_sent &&
447 		    !adapter->curr_cmd && !is_command_pending(adapter) &&
448 		    (mwifiex_wmm_lists_empty(adapter) &&
449 		     mwifiex_bypass_txlist_empty(adapter) &&
450 		     skb_queue_empty(&adapter->tx_data_q))) {
451 			if (!mwifiex_send_null_packet
452 			    (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
453 			     MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
454 			     MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
455 				adapter->delay_null_pkt = false;
456 				adapter->ps_state = PS_STATE_SLEEP;
457 			}
458 			break;
459 		}
460 	} while (true);
461 
462 	spin_lock_irqsave(&adapter->main_proc_lock, flags);
463 	if (adapter->more_task_flag) {
464 		adapter->more_task_flag = false;
465 		spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
466 		goto process_start;
467 	}
468 	adapter->mwifiex_processing = false;
469 	spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
470 
471 	return ret;
472 }
473 EXPORT_SYMBOL_GPL(mwifiex_main_process);
474 
475 /*
476  * This function frees the adapter structure.
477  *
478  * Additionally, this closes the netlink socket, frees the timers
479  * and private structures.
480  */
mwifiex_free_adapter(struct mwifiex_adapter * adapter)481 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
482 {
483 	if (!adapter) {
484 		pr_err("%s: adapter is NULL\n", __func__);
485 		return;
486 	}
487 
488 	mwifiex_unregister(adapter);
489 	pr_debug("info: %s: free adapter\n", __func__);
490 }
491 
492 /*
493  * This function cancels all works in the queue and destroys
494  * the main workqueue.
495  */
mwifiex_terminate_workqueue(struct mwifiex_adapter * adapter)496 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
497 {
498 	if (adapter->workqueue) {
499 		flush_workqueue(adapter->workqueue);
500 		destroy_workqueue(adapter->workqueue);
501 		adapter->workqueue = NULL;
502 	}
503 
504 	if (adapter->rx_workqueue) {
505 		flush_workqueue(adapter->rx_workqueue);
506 		destroy_workqueue(adapter->rx_workqueue);
507 		adapter->rx_workqueue = NULL;
508 	}
509 }
510 
511 /*
512  * This function gets firmware and initializes it.
513  *
514  * The main initialization steps followed are -
515  *      - Download the correct firmware to card
516  *      - Issue the init commands to firmware
517  */
_mwifiex_fw_dpc(const struct firmware * firmware,void * context)518 static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
519 {
520 	int ret;
521 	char fmt[64];
522 	struct mwifiex_adapter *adapter = context;
523 	struct mwifiex_fw_image fw;
524 	bool init_failed = false;
525 	struct wireless_dev *wdev;
526 	struct completion *fw_done = adapter->fw_done;
527 
528 	if (!firmware) {
529 		mwifiex_dbg(adapter, ERROR,
530 			    "Failed to get firmware %s\n", adapter->fw_name);
531 		goto err_dnld_fw;
532 	}
533 
534 	memset(&fw, 0, sizeof(struct mwifiex_fw_image));
535 	adapter->firmware = firmware;
536 	fw.fw_buf = (u8 *) adapter->firmware->data;
537 	fw.fw_len = adapter->firmware->size;
538 
539 	if (adapter->if_ops.dnld_fw) {
540 		ret = adapter->if_ops.dnld_fw(adapter, &fw);
541 	} else {
542 		ret = mwifiex_dnld_fw(adapter, &fw);
543 	}
544 
545 	if (ret == -1)
546 		goto err_dnld_fw;
547 
548 	mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
549 
550 	if (cal_data_cfg) {
551 		if ((request_firmware(&adapter->cal_data, cal_data_cfg,
552 				      adapter->dev)) < 0)
553 			mwifiex_dbg(adapter, ERROR,
554 				    "Cal data request_firmware() failed\n");
555 	}
556 
557 	/* enable host interrupt after fw dnld is successful */
558 	if (adapter->if_ops.enable_int) {
559 		if (adapter->if_ops.enable_int(adapter))
560 			goto err_dnld_fw;
561 	}
562 
563 	adapter->init_wait_q_woken = false;
564 	ret = mwifiex_init_fw(adapter);
565 	if (ret == -1) {
566 		goto err_init_fw;
567 	} else if (!ret) {
568 		adapter->hw_status = MWIFIEX_HW_STATUS_READY;
569 		goto done;
570 	}
571 	/* Wait for mwifiex_init to complete */
572 	if (!adapter->mfg_mode) {
573 		wait_event_interruptible(adapter->init_wait_q,
574 					 adapter->init_wait_q_woken);
575 		if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
576 			goto err_init_fw;
577 	}
578 
579 	if (!adapter->wiphy) {
580 		if (mwifiex_register_cfg80211(adapter)) {
581 			mwifiex_dbg(adapter, ERROR,
582 				    "cannot register with cfg80211\n");
583 			goto err_init_fw;
584 		}
585 	}
586 
587 	if (mwifiex_init_channel_scan_gap(adapter)) {
588 		mwifiex_dbg(adapter, ERROR,
589 			    "could not init channel stats table\n");
590 		goto err_init_chan_scan;
591 	}
592 
593 	if (driver_mode) {
594 		driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
595 		driver_mode |= MWIFIEX_DRIVER_MODE_STA;
596 	}
597 
598 	rtnl_lock();
599 	/* Create station interface by default */
600 	wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
601 					NL80211_IFTYPE_STATION, NULL);
602 	if (IS_ERR(wdev)) {
603 		mwifiex_dbg(adapter, ERROR,
604 			    "cannot create default STA interface\n");
605 		rtnl_unlock();
606 		goto err_add_intf;
607 	}
608 
609 	if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
610 		wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
611 						NL80211_IFTYPE_AP, NULL);
612 		if (IS_ERR(wdev)) {
613 			mwifiex_dbg(adapter, ERROR,
614 				    "cannot create AP interface\n");
615 			rtnl_unlock();
616 			goto err_add_intf;
617 		}
618 	}
619 
620 	if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
621 		wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
622 						NL80211_IFTYPE_P2P_CLIENT, NULL);
623 		if (IS_ERR(wdev)) {
624 			mwifiex_dbg(adapter, ERROR,
625 				    "cannot create p2p client interface\n");
626 			rtnl_unlock();
627 			goto err_add_intf;
628 		}
629 	}
630 	rtnl_unlock();
631 
632 	mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
633 	mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
634 	goto done;
635 
636 err_add_intf:
637 	vfree(adapter->chan_stats);
638 err_init_chan_scan:
639 	wiphy_unregister(adapter->wiphy);
640 	wiphy_free(adapter->wiphy);
641 err_init_fw:
642 	if (adapter->if_ops.disable_int)
643 		adapter->if_ops.disable_int(adapter);
644 err_dnld_fw:
645 	mwifiex_dbg(adapter, ERROR,
646 		    "info: %s: unregister device\n", __func__);
647 	if (adapter->if_ops.unregister_dev)
648 		adapter->if_ops.unregister_dev(adapter);
649 
650 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
651 	mwifiex_terminate_workqueue(adapter);
652 
653 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
654 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
655 		mwifiex_shutdown_drv(adapter);
656 		mwifiex_free_cmd_buffers(adapter);
657 	}
658 
659 	init_failed = true;
660 done:
661 	if (adapter->cal_data) {
662 		release_firmware(adapter->cal_data);
663 		adapter->cal_data = NULL;
664 	}
665 	if (adapter->firmware) {
666 		release_firmware(adapter->firmware);
667 		adapter->firmware = NULL;
668 	}
669 	if (init_failed) {
670 		if (adapter->irq_wakeup >= 0)
671 			device_init_wakeup(adapter->dev, false);
672 		mwifiex_free_adapter(adapter);
673 	}
674 	/* Tell all current and future waiters we're finished */
675 	complete_all(fw_done);
676 
677 	return init_failed ? -EIO : 0;
678 }
679 
mwifiex_fw_dpc(const struct firmware * firmware,void * context)680 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
681 {
682 	_mwifiex_fw_dpc(firmware, context);
683 }
684 
685 /*
686  * This function gets the firmware and (if called asynchronously) kicks off the
687  * HW init when done.
688  */
mwifiex_init_hw_fw(struct mwifiex_adapter * adapter,bool req_fw_nowait)689 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
690 			      bool req_fw_nowait)
691 {
692 	int ret;
693 
694 	/* Override default firmware with manufacturing one if
695 	 * manufacturing mode is enabled
696 	 */
697 	if (mfg_mode) {
698 		if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
699 			    sizeof(adapter->fw_name)) >=
700 			    sizeof(adapter->fw_name)) {
701 			pr_err("%s: fw_name too long!\n", __func__);
702 			return -1;
703 		}
704 	}
705 
706 	if (req_fw_nowait) {
707 		ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
708 					      adapter->dev, GFP_KERNEL, adapter,
709 					      mwifiex_fw_dpc);
710 	} else {
711 		ret = request_firmware(&adapter->firmware,
712 				       adapter->fw_name,
713 				       adapter->dev);
714 	}
715 
716 	if (ret < 0)
717 		mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
718 			    req_fw_nowait ? "_nowait" : "", ret);
719 	return ret;
720 }
721 
722 /*
723  * CFG802.11 network device handler for open.
724  *
725  * Starts the data queue.
726  */
727 static int
mwifiex_open(struct net_device * dev)728 mwifiex_open(struct net_device *dev)
729 {
730 	netif_carrier_off(dev);
731 
732 	return 0;
733 }
734 
735 /*
736  * CFG802.11 network device handler for close.
737  */
738 static int
mwifiex_close(struct net_device * dev)739 mwifiex_close(struct net_device *dev)
740 {
741 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
742 
743 	if (priv->scan_request) {
744 		struct cfg80211_scan_info info = {
745 			.aborted = true,
746 		};
747 
748 		mwifiex_dbg(priv->adapter, INFO,
749 			    "aborting scan on ndo_stop\n");
750 		cfg80211_scan_done(priv->scan_request, &info);
751 		priv->scan_request = NULL;
752 		priv->scan_aborting = true;
753 	}
754 
755 	if (priv->sched_scanning) {
756 		mwifiex_dbg(priv->adapter, INFO,
757 			    "aborting bgscan on ndo_stop\n");
758 		mwifiex_stop_bg_scan(priv);
759 		cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
760 	}
761 
762 	return 0;
763 }
764 
765 static bool
mwifiex_bypass_tx_queue(struct mwifiex_private * priv,struct sk_buff * skb)766 mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
767 			struct sk_buff *skb)
768 {
769 	struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
770 
771 	if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
772 	    mwifiex_is_skb_mgmt_frame(skb) ||
773 	    (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
774 	     ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
775 	     (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
776 		mwifiex_dbg(priv->adapter, DATA,
777 			    "bypass txqueue; eth type %#x, mgmt %d\n",
778 			     ntohs(eth_hdr->h_proto),
779 			     mwifiex_is_skb_mgmt_frame(skb));
780 		return true;
781 	}
782 
783 	return false;
784 }
785 /*
786  * Add buffer into wmm tx queue and queue work to transmit it.
787  */
mwifiex_queue_tx_pkt(struct mwifiex_private * priv,struct sk_buff * skb)788 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
789 {
790 	struct netdev_queue *txq;
791 	int index = mwifiex_1d_to_wmm_queue[skb->priority];
792 
793 	if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
794 		txq = netdev_get_tx_queue(priv->netdev, index);
795 		if (!netif_tx_queue_stopped(txq)) {
796 			netif_tx_stop_queue(txq);
797 			mwifiex_dbg(priv->adapter, DATA,
798 				    "stop queue: %d\n", index);
799 		}
800 	}
801 
802 	if (mwifiex_bypass_tx_queue(priv, skb)) {
803 		atomic_inc(&priv->adapter->tx_pending);
804 		atomic_inc(&priv->adapter->bypass_tx_pending);
805 		mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
806 	 } else {
807 		atomic_inc(&priv->adapter->tx_pending);
808 		mwifiex_wmm_add_buf_txqueue(priv, skb);
809 	 }
810 
811 	mwifiex_queue_main_work(priv->adapter);
812 
813 	return 0;
814 }
815 
816 struct sk_buff *
mwifiex_clone_skb_for_tx_status(struct mwifiex_private * priv,struct sk_buff * skb,u8 flag,u64 * cookie)817 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
818 				struct sk_buff *skb, u8 flag, u64 *cookie)
819 {
820 	struct sk_buff *orig_skb = skb;
821 	struct mwifiex_txinfo *tx_info, *orig_tx_info;
822 
823 	skb = skb_clone(skb, GFP_ATOMIC);
824 	if (skb) {
825 		int id;
826 
827 		spin_lock_bh(&priv->ack_status_lock);
828 		id = idr_alloc(&priv->ack_status_frames, orig_skb,
829 			       1, 0x10, GFP_ATOMIC);
830 		spin_unlock_bh(&priv->ack_status_lock);
831 
832 		if (id >= 0) {
833 			tx_info = MWIFIEX_SKB_TXCB(skb);
834 			tx_info->ack_frame_id = id;
835 			tx_info->flags |= flag;
836 			orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
837 			orig_tx_info->ack_frame_id = id;
838 			orig_tx_info->flags |= flag;
839 
840 			if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
841 				orig_tx_info->cookie = *cookie;
842 
843 		} else if (skb_shared(skb)) {
844 			kfree_skb(orig_skb);
845 		} else {
846 			kfree_skb(skb);
847 			skb = orig_skb;
848 		}
849 	} else {
850 		/* couldn't clone -- lose tx status ... */
851 		skb = orig_skb;
852 	}
853 
854 	return skb;
855 }
856 
857 /*
858  * CFG802.11 network device handler for data transmission.
859  */
860 static netdev_tx_t
mwifiex_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)861 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
862 {
863 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
864 	struct sk_buff *new_skb;
865 	struct mwifiex_txinfo *tx_info;
866 	bool multicast;
867 
868 	mwifiex_dbg(priv->adapter, DATA,
869 		    "data: %lu BSS(%d-%d): Data <= kernel\n",
870 		    jiffies, priv->bss_type, priv->bss_num);
871 
872 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
873 		kfree_skb(skb);
874 		priv->stats.tx_dropped++;
875 		return 0;
876 	}
877 	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
878 		mwifiex_dbg(priv->adapter, ERROR,
879 			    "Tx: bad skb len %d\n", skb->len);
880 		kfree_skb(skb);
881 		priv->stats.tx_dropped++;
882 		return 0;
883 	}
884 	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
885 		mwifiex_dbg(priv->adapter, DATA,
886 			    "data: Tx: insufficient skb headroom %d\n",
887 			    skb_headroom(skb));
888 		/* Insufficient skb headroom - allocate a new skb */
889 		new_skb =
890 			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
891 		if (unlikely(!new_skb)) {
892 			mwifiex_dbg(priv->adapter, ERROR,
893 				    "Tx: cannot alloca new_skb\n");
894 			kfree_skb(skb);
895 			priv->stats.tx_dropped++;
896 			return 0;
897 		}
898 		kfree_skb(skb);
899 		skb = new_skb;
900 		mwifiex_dbg(priv->adapter, INFO,
901 			    "info: new skb headroomd %d\n",
902 			    skb_headroom(skb));
903 	}
904 
905 	tx_info = MWIFIEX_SKB_TXCB(skb);
906 	memset(tx_info, 0, sizeof(*tx_info));
907 	tx_info->bss_num = priv->bss_num;
908 	tx_info->bss_type = priv->bss_type;
909 	tx_info->pkt_len = skb->len;
910 
911 	multicast = is_multicast_ether_addr(skb->data);
912 
913 	if (unlikely(!multicast && skb->sk &&
914 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
915 		     priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
916 		skb = mwifiex_clone_skb_for_tx_status(priv,
917 						      skb,
918 					MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
919 
920 	/* Record the current time the packet was queued; used to
921 	 * determine the amount of time the packet was queued in
922 	 * the driver before it was sent to the firmware.
923 	 * The delay is then sent along with the packet to the
924 	 * firmware for aggregate delay calculation for stats and
925 	 * MSDU lifetime expiry.
926 	 */
927 	__net_timestamp(skb);
928 
929 	if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
930 	    priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
931 	    !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
932 		if (priv->adapter->auto_tdls && priv->check_tdls_tx)
933 			mwifiex_tdls_check_tx(priv, skb);
934 	}
935 
936 	mwifiex_queue_tx_pkt(priv, skb);
937 
938 	return 0;
939 }
940 
mwifiex_set_mac_address(struct mwifiex_private * priv,struct net_device * dev,bool external,u8 * new_mac)941 int mwifiex_set_mac_address(struct mwifiex_private *priv,
942 			    struct net_device *dev, bool external,
943 			    u8 *new_mac)
944 {
945 	int ret;
946 	u64 mac_addr, old_mac_addr;
947 
948 	old_mac_addr = ether_addr_to_u64(priv->curr_addr);
949 
950 	if (external) {
951 		mac_addr = ether_addr_to_u64(new_mac);
952 	} else {
953 		/* Internal mac address change */
954 		if (priv->bss_type == MWIFIEX_BSS_TYPE_ANY)
955 			return -ENOTSUPP;
956 
957 		mac_addr = old_mac_addr;
958 
959 		if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P) {
960 			mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
961 			mac_addr += priv->bss_num;
962 		} else if (priv->adapter->priv[0] != priv) {
963 			/* Set mac address based on bss_type/bss_num */
964 			mac_addr ^= BIT_ULL(priv->bss_type + 8);
965 			mac_addr += priv->bss_num;
966 		}
967 	}
968 
969 	u64_to_ether_addr(mac_addr, priv->curr_addr);
970 
971 	/* Send request to firmware */
972 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
973 			       HostCmd_ACT_GEN_SET, 0, NULL, true);
974 
975 	if (ret) {
976 		u64_to_ether_addr(old_mac_addr, priv->curr_addr);
977 		mwifiex_dbg(priv->adapter, ERROR,
978 			    "set mac address failed: ret=%d\n", ret);
979 		return ret;
980 	}
981 
982 	ether_addr_copy(dev->dev_addr, priv->curr_addr);
983 	return 0;
984 }
985 
986 /* CFG802.11 network device handler for setting MAC address.
987  */
988 static int
mwifiex_ndo_set_mac_address(struct net_device * dev,void * addr)989 mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
990 {
991 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
992 	struct sockaddr *hw_addr = addr;
993 
994 	return mwifiex_set_mac_address(priv, dev, true, hw_addr->sa_data);
995 }
996 
997 /*
998  * CFG802.11 network device handler for setting multicast list.
999  */
mwifiex_set_multicast_list(struct net_device * dev)1000 static void mwifiex_set_multicast_list(struct net_device *dev)
1001 {
1002 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1003 	struct mwifiex_multicast_list mcast_list;
1004 
1005 	if (dev->flags & IFF_PROMISC) {
1006 		mcast_list.mode = MWIFIEX_PROMISC_MODE;
1007 	} else if (dev->flags & IFF_ALLMULTI ||
1008 		   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
1009 		mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
1010 	} else {
1011 		mcast_list.mode = MWIFIEX_MULTICAST_MODE;
1012 		mcast_list.num_multicast_addr =
1013 			mwifiex_copy_mcast_addr(&mcast_list, dev);
1014 	}
1015 	mwifiex_request_set_multicast_list(priv, &mcast_list);
1016 }
1017 
1018 /*
1019  * CFG802.11 network device handler for transmission timeout.
1020  */
1021 static void
mwifiex_tx_timeout(struct net_device * dev)1022 mwifiex_tx_timeout(struct net_device *dev)
1023 {
1024 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1025 
1026 	priv->num_tx_timeout++;
1027 	priv->tx_timeout_cnt++;
1028 	mwifiex_dbg(priv->adapter, ERROR,
1029 		    "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1030 		    jiffies, priv->tx_timeout_cnt, priv->bss_type,
1031 		    priv->bss_num);
1032 	mwifiex_set_trans_start(dev);
1033 
1034 	if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1035 	    priv->adapter->if_ops.card_reset) {
1036 		mwifiex_dbg(priv->adapter, ERROR,
1037 			    "tx_timeout_cnt exceeds threshold.\t"
1038 			    "Triggering card reset!\n");
1039 		priv->adapter->if_ops.card_reset(priv->adapter);
1040 	}
1041 }
1042 
mwifiex_multi_chan_resync(struct mwifiex_adapter * adapter)1043 void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1044 {
1045 	struct usb_card_rec *card = adapter->card;
1046 	struct mwifiex_private *priv;
1047 	u16 tx_buf_size;
1048 	int i, ret;
1049 
1050 	card->mc_resync_flag = true;
1051 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1052 		if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1053 			mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1054 			return;
1055 		}
1056 	}
1057 
1058 	card->mc_resync_flag = false;
1059 	tx_buf_size = 0xffff;
1060 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1061 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1062 			       HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1063 	if (ret)
1064 		mwifiex_dbg(adapter, ERROR,
1065 			    "send reconfig tx buf size cmd err\n");
1066 }
1067 EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1068 
mwifiex_upload_device_dump(struct mwifiex_adapter * adapter)1069 void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1070 {
1071 	/* Dump all the memory data into single file, a userspace script will
1072 	 * be used to split all the memory data to multiple files
1073 	 */
1074 	mwifiex_dbg(adapter, MSG,
1075 		    "== mwifiex dump information to /sys/class/devcoredump start\n");
1076 	dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
1077 		      GFP_KERNEL);
1078 	mwifiex_dbg(adapter, MSG,
1079 		    "== mwifiex dump information to /sys/class/devcoredump end\n");
1080 
1081 	/* Device dump data will be freed in device coredump release function
1082 	 * after 5 min. Here reset adapter->devdump_data and ->devdump_len
1083 	 * to avoid it been accidentally reused.
1084 	 */
1085 	adapter->devdump_data = NULL;
1086 	adapter->devdump_len = 0;
1087 }
1088 EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1089 
mwifiex_drv_info_dump(struct mwifiex_adapter * adapter)1090 void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
1091 {
1092 	char *p;
1093 	char drv_version[64];
1094 	struct usb_card_rec *cardp;
1095 	struct sdio_mmc_card *sdio_card;
1096 	struct mwifiex_private *priv;
1097 	int i, idx;
1098 	struct netdev_queue *txq;
1099 	struct mwifiex_debug_info *debug_info;
1100 
1101 	mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
1102 
1103 	p = adapter->devdump_data;
1104 	strcpy(p, "========Start dump driverinfo========\n");
1105 	p += strlen("========Start dump driverinfo========\n");
1106 	p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1107 
1108 	mwifiex_drv_get_driver_version(adapter, drv_version,
1109 				       sizeof(drv_version) - 1);
1110 	p += sprintf(p, "driver_version = %s\n", drv_version);
1111 
1112 	if (adapter->iface_type == MWIFIEX_USB) {
1113 		cardp = (struct usb_card_rec *)adapter->card;
1114 		p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1115 			     atomic_read(&cardp->tx_cmd_urb_pending));
1116 		p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1117 			     atomic_read(&cardp->port[0].tx_data_urb_pending));
1118 		p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1119 			     atomic_read(&cardp->port[1].tx_data_urb_pending));
1120 		p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1121 			     atomic_read(&cardp->rx_cmd_urb_pending));
1122 		p += sprintf(p, "rx_data_urb_pending = %d\n",
1123 			     atomic_read(&cardp->rx_data_urb_pending));
1124 	}
1125 
1126 	p += sprintf(p, "tx_pending = %d\n",
1127 		     atomic_read(&adapter->tx_pending));
1128 	p += sprintf(p, "rx_pending = %d\n",
1129 		     atomic_read(&adapter->rx_pending));
1130 
1131 	if (adapter->iface_type == MWIFIEX_SDIO) {
1132 		sdio_card = (struct sdio_mmc_card *)adapter->card;
1133 		p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1134 			     sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1135 		p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1136 			     sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1137 	}
1138 
1139 	for (i = 0; i < adapter->priv_num; i++) {
1140 		if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1141 			continue;
1142 		priv = adapter->priv[i];
1143 		p += sprintf(p, "\n[interface  : \"%s\"]\n",
1144 			     priv->netdev->name);
1145 		p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1146 			     atomic_read(&priv->wmm_tx_pending[0]));
1147 		p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1148 			     atomic_read(&priv->wmm_tx_pending[1]));
1149 		p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1150 			     atomic_read(&priv->wmm_tx_pending[2]));
1151 		p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1152 			     atomic_read(&priv->wmm_tx_pending[3]));
1153 		p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1154 			     "Disconnected" : "Connected");
1155 		p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1156 			     ? "on" : "off"));
1157 		for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1158 			txq = netdev_get_tx_queue(priv->netdev, idx);
1159 			p += sprintf(p, "tx queue %d:%s  ", idx,
1160 				     netif_tx_queue_stopped(txq) ?
1161 				     "stopped" : "started");
1162 		}
1163 		p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1164 			     priv->netdev->name, priv->num_tx_timeout);
1165 	}
1166 
1167 	if (adapter->iface_type == MWIFIEX_SDIO ||
1168 	    adapter->iface_type == MWIFIEX_PCIE) {
1169 		p += sprintf(p, "\n=== %s register dump===\n",
1170 			     adapter->iface_type == MWIFIEX_SDIO ?
1171 							"SDIO" : "PCIE");
1172 		if (adapter->if_ops.reg_dump)
1173 			p += adapter->if_ops.reg_dump(adapter, p);
1174 	}
1175 	p += sprintf(p, "\n=== more debug information\n");
1176 	debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1177 	if (debug_info) {
1178 		for (i = 0; i < adapter->priv_num; i++) {
1179 			if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1180 				continue;
1181 			priv = adapter->priv[i];
1182 			mwifiex_get_debug_info(priv, debug_info);
1183 			p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1184 			break;
1185 		}
1186 		kfree(debug_info);
1187 	}
1188 
1189 	strcpy(p, "\n========End dump========\n");
1190 	p += strlen("\n========End dump========\n");
1191 	mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1192 	adapter->devdump_len = p - (char *)adapter->devdump_data;
1193 }
1194 EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1195 
mwifiex_prepare_fw_dump_info(struct mwifiex_adapter * adapter)1196 void mwifiex_prepare_fw_dump_info(struct mwifiex_adapter *adapter)
1197 {
1198 	u8 idx;
1199 	char *fw_dump_ptr;
1200 	u32 dump_len = 0;
1201 
1202 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1203 		struct memory_type_mapping *entry =
1204 				&adapter->mem_type_mapping_tbl[idx];
1205 
1206 		if (entry->mem_ptr) {
1207 			dump_len += (strlen("========Start dump ") +
1208 					strlen(entry->mem_name) +
1209 					strlen("========\n") +
1210 					(entry->mem_size + 1) +
1211 					strlen("\n========End dump========\n"));
1212 		}
1213 	}
1214 
1215 	if (dump_len + 1 + adapter->devdump_len > MWIFIEX_FW_DUMP_SIZE) {
1216 		/* Realloc in case buffer overflow */
1217 		fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
1218 		mwifiex_dbg(adapter, MSG, "Realloc device dump data.\n");
1219 		if (!fw_dump_ptr) {
1220 			vfree(adapter->devdump_data);
1221 			mwifiex_dbg(adapter, ERROR,
1222 				    "vzalloc devdump data failure!\n");
1223 			return;
1224 		}
1225 
1226 		memmove(fw_dump_ptr, adapter->devdump_data,
1227 			adapter->devdump_len);
1228 		vfree(adapter->devdump_data);
1229 		adapter->devdump_data = fw_dump_ptr;
1230 	}
1231 
1232 	fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
1233 
1234 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1235 		struct memory_type_mapping *entry =
1236 					&adapter->mem_type_mapping_tbl[idx];
1237 
1238 		if (entry->mem_ptr) {
1239 			strcpy(fw_dump_ptr, "========Start dump ");
1240 			fw_dump_ptr += strlen("========Start dump ");
1241 
1242 			strcpy(fw_dump_ptr, entry->mem_name);
1243 			fw_dump_ptr += strlen(entry->mem_name);
1244 
1245 			strcpy(fw_dump_ptr, "========\n");
1246 			fw_dump_ptr += strlen("========\n");
1247 
1248 			memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1249 			fw_dump_ptr += entry->mem_size;
1250 
1251 			strcpy(fw_dump_ptr, "\n========End dump========\n");
1252 			fw_dump_ptr += strlen("\n========End dump========\n");
1253 		}
1254 	}
1255 
1256 	adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
1257 
1258 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1259 		struct memory_type_mapping *entry =
1260 			&adapter->mem_type_mapping_tbl[idx];
1261 
1262 		vfree(entry->mem_ptr);
1263 		entry->mem_ptr = NULL;
1264 		entry->mem_size = 0;
1265 	}
1266 }
1267 EXPORT_SYMBOL_GPL(mwifiex_prepare_fw_dump_info);
1268 
1269 /*
1270  * CFG802.11 network device handler for statistics retrieval.
1271  */
mwifiex_get_stats(struct net_device * dev)1272 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1273 {
1274 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1275 
1276 	return &priv->stats;
1277 }
1278 
1279 static u16
mwifiex_netdev_select_wmm_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)1280 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1281 				struct net_device *sb_dev)
1282 {
1283 	skb->priority = cfg80211_classify8021d(skb, NULL);
1284 	return mwifiex_1d_to_wmm_queue[skb->priority];
1285 }
1286 
1287 /* Network device handlers */
1288 static const struct net_device_ops mwifiex_netdev_ops = {
1289 	.ndo_open = mwifiex_open,
1290 	.ndo_stop = mwifiex_close,
1291 	.ndo_start_xmit = mwifiex_hard_start_xmit,
1292 	.ndo_set_mac_address = mwifiex_ndo_set_mac_address,
1293 	.ndo_validate_addr = eth_validate_addr,
1294 	.ndo_tx_timeout = mwifiex_tx_timeout,
1295 	.ndo_get_stats = mwifiex_get_stats,
1296 	.ndo_set_rx_mode = mwifiex_set_multicast_list,
1297 	.ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1298 };
1299 
1300 /*
1301  * This function initializes the private structure parameters.
1302  *
1303  * The following wait queues are initialized -
1304  *      - IOCTL wait queue
1305  *      - Command wait queue
1306  *      - Statistics wait queue
1307  *
1308  * ...and the following default parameters are set -
1309  *      - Current key index     : Set to 0
1310  *      - Rate index            : Set to auto
1311  *      - Media connected       : Set to disconnected
1312  *      - Adhoc link sensed     : Set to false
1313  *      - Nick name             : Set to null
1314  *      - Number of Tx timeout  : Set to 0
1315  *      - Device address        : Set to current address
1316  *      - Rx histogram statistc : Set to 0
1317  *
1318  * In addition, the CFG80211 work queue is also created.
1319  */
mwifiex_init_priv_params(struct mwifiex_private * priv,struct net_device * dev)1320 void mwifiex_init_priv_params(struct mwifiex_private *priv,
1321 			      struct net_device *dev)
1322 {
1323 	dev->netdev_ops = &mwifiex_netdev_ops;
1324 	dev->needs_free_netdev = true;
1325 	/* Initialize private structure */
1326 	priv->current_key_index = 0;
1327 	priv->media_connected = false;
1328 	memset(priv->mgmt_ie, 0,
1329 	       sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1330 	priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1331 	priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1332 	priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1333 	priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1334 	priv->num_tx_timeout = 0;
1335 	if (is_valid_ether_addr(dev->dev_addr))
1336 		ether_addr_copy(priv->curr_addr, dev->dev_addr);
1337 	else
1338 		ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1339 
1340 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1341 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1342 		priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1343 		if (priv->hist_data)
1344 			mwifiex_hist_data_reset(priv);
1345 	}
1346 }
1347 
1348 /*
1349  * This function check if command is pending.
1350  */
is_command_pending(struct mwifiex_adapter * adapter)1351 int is_command_pending(struct mwifiex_adapter *adapter)
1352 {
1353 	int is_cmd_pend_q_empty;
1354 
1355 	spin_lock_bh(&adapter->cmd_pending_q_lock);
1356 	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1357 	spin_unlock_bh(&adapter->cmd_pending_q_lock);
1358 
1359 	return !is_cmd_pend_q_empty;
1360 }
1361 
1362 /*
1363  * This is the RX work queue function.
1364  *
1365  * It handles the RX operations.
1366  */
mwifiex_rx_work_queue(struct work_struct * work)1367 static void mwifiex_rx_work_queue(struct work_struct *work)
1368 {
1369 	struct mwifiex_adapter *adapter =
1370 		container_of(work, struct mwifiex_adapter, rx_work);
1371 
1372 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1373 		return;
1374 	mwifiex_process_rx(adapter);
1375 }
1376 
1377 /*
1378  * This is the main work queue function.
1379  *
1380  * It handles the main process, which in turn handles the complete
1381  * driver operations.
1382  */
mwifiex_main_work_queue(struct work_struct * work)1383 static void mwifiex_main_work_queue(struct work_struct *work)
1384 {
1385 	struct mwifiex_adapter *adapter =
1386 		container_of(work, struct mwifiex_adapter, main_work);
1387 
1388 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1389 		return;
1390 	mwifiex_main_process(adapter);
1391 }
1392 
1393 /* Common teardown code used for both device removal and reset */
mwifiex_uninit_sw(struct mwifiex_adapter * adapter)1394 static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
1395 {
1396 	struct mwifiex_private *priv;
1397 	int i;
1398 
1399 	/* We can no longer handle interrupts once we start doing the teardown
1400 	 * below.
1401 	 */
1402 	if (adapter->if_ops.disable_int)
1403 		adapter->if_ops.disable_int(adapter);
1404 
1405 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1406 	mwifiex_terminate_workqueue(adapter);
1407 	adapter->int_status = 0;
1408 
1409 	/* Stop data */
1410 	for (i = 0; i < adapter->priv_num; i++) {
1411 		priv = adapter->priv[i];
1412 		if (priv && priv->netdev) {
1413 			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1414 			if (netif_carrier_ok(priv->netdev))
1415 				netif_carrier_off(priv->netdev);
1416 			netif_device_detach(priv->netdev);
1417 		}
1418 	}
1419 
1420 	mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
1421 	mwifiex_shutdown_drv(adapter);
1422 	mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1423 
1424 	if (atomic_read(&adapter->rx_pending) ||
1425 	    atomic_read(&adapter->tx_pending) ||
1426 	    atomic_read(&adapter->cmd_pending)) {
1427 		mwifiex_dbg(adapter, ERROR,
1428 			    "rx_pending=%d, tx_pending=%d,\t"
1429 			    "cmd_pending=%d\n",
1430 			    atomic_read(&adapter->rx_pending),
1431 			    atomic_read(&adapter->tx_pending),
1432 			    atomic_read(&adapter->cmd_pending));
1433 	}
1434 
1435 	for (i = 0; i < adapter->priv_num; i++) {
1436 		priv = adapter->priv[i];
1437 		if (!priv)
1438 			continue;
1439 		rtnl_lock();
1440 		if (priv->netdev &&
1441 		    priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1442 			mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1443 		rtnl_unlock();
1444 	}
1445 
1446 	wiphy_unregister(adapter->wiphy);
1447 	wiphy_free(adapter->wiphy);
1448 	adapter->wiphy = NULL;
1449 
1450 	vfree(adapter->chan_stats);
1451 	mwifiex_free_cmd_buffers(adapter);
1452 }
1453 
1454 /*
1455  * This function gets called during PCIe function level reset.
1456  */
mwifiex_shutdown_sw(struct mwifiex_adapter * adapter)1457 int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
1458 {
1459 	struct mwifiex_private *priv;
1460 
1461 	if (!adapter)
1462 		return 0;
1463 
1464 	wait_for_completion(adapter->fw_done);
1465 	/* Caller should ensure we aren't suspending while this happens */
1466 	reinit_completion(adapter->fw_done);
1467 
1468 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1469 	mwifiex_deauthenticate(priv, NULL);
1470 
1471 	mwifiex_uninit_sw(adapter);
1472 
1473 	if (adapter->if_ops.down_dev)
1474 		adapter->if_ops.down_dev(adapter);
1475 
1476 	return 0;
1477 }
1478 EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
1479 
1480 /* This function gets called during PCIe function level reset. Required
1481  * code is extracted from mwifiex_add_card()
1482  */
1483 int
mwifiex_reinit_sw(struct mwifiex_adapter * adapter)1484 mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
1485 {
1486 	int ret;
1487 
1488 	mwifiex_init_lock_list(adapter);
1489 	if (adapter->if_ops.up_dev)
1490 		adapter->if_ops.up_dev(adapter);
1491 
1492 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1493 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1494 	init_waitqueue_head(&adapter->init_wait_q);
1495 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1496 	adapter->hs_activated = false;
1497 	clear_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags);
1498 	init_waitqueue_head(&adapter->hs_activate_wait_q);
1499 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
1500 	adapter->cmd_wait_q.status = 0;
1501 	adapter->scan_wait_q_woken = false;
1502 
1503 	if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1504 		adapter->rx_work_enabled = true;
1505 
1506 	adapter->workqueue =
1507 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
1508 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1509 	if (!adapter->workqueue)
1510 		goto err_kmalloc;
1511 
1512 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1513 
1514 	if (adapter->rx_work_enabled) {
1515 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1516 							WQ_HIGHPRI |
1517 							WQ_MEM_RECLAIM |
1518 							WQ_UNBOUND, 1);
1519 		if (!adapter->rx_workqueue)
1520 			goto err_kmalloc;
1521 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1522 	}
1523 
1524 	/* Register the device. Fill up the private data structure with
1525 	 * relevant information from the card. Some code extracted from
1526 	 * mwifiex_register_dev()
1527 	 */
1528 	mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
1529 
1530 	if (mwifiex_init_hw_fw(adapter, false)) {
1531 		mwifiex_dbg(adapter, ERROR,
1532 			    "%s: firmware init failed\n", __func__);
1533 		goto err_init_fw;
1534 	}
1535 
1536 	/* _mwifiex_fw_dpc() does its own cleanup */
1537 	ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1538 	if (ret) {
1539 		pr_err("Failed to bring up adapter: %d\n", ret);
1540 		return ret;
1541 	}
1542 	mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
1543 
1544 	return 0;
1545 
1546 err_init_fw:
1547 	mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1548 	if (adapter->if_ops.unregister_dev)
1549 		adapter->if_ops.unregister_dev(adapter);
1550 
1551 err_kmalloc:
1552 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1553 	mwifiex_terminate_workqueue(adapter);
1554 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1555 		mwifiex_dbg(adapter, ERROR,
1556 			    "info: %s: shutdown mwifiex\n", __func__);
1557 		mwifiex_shutdown_drv(adapter);
1558 		mwifiex_free_cmd_buffers(adapter);
1559 	}
1560 
1561 	complete_all(adapter->fw_done);
1562 	mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1563 
1564 	return -1;
1565 }
1566 EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
1567 
mwifiex_irq_wakeup_handler(int irq,void * priv)1568 static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1569 {
1570 	struct mwifiex_adapter *adapter = priv;
1571 
1572 	dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1573 	adapter->wake_by_wifi = true;
1574 	disable_irq_nosync(irq);
1575 
1576 	/* Notify PM core we are wakeup source */
1577 	pm_wakeup_event(adapter->dev, 0);
1578 	pm_system_wakeup();
1579 
1580 	return IRQ_HANDLED;
1581 }
1582 
mwifiex_probe_of(struct mwifiex_adapter * adapter)1583 static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1584 {
1585 	int ret;
1586 	struct device *dev = adapter->dev;
1587 
1588 	if (!dev->of_node)
1589 		goto err_exit;
1590 
1591 	adapter->dt_node = dev->of_node;
1592 	adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1593 	if (!adapter->irq_wakeup) {
1594 		dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1595 		goto err_exit;
1596 	}
1597 
1598 	ret = devm_request_irq(dev, adapter->irq_wakeup,
1599 			       mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1600 			       "wifi_wake", adapter);
1601 	if (ret) {
1602 		dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1603 			adapter->irq_wakeup, ret);
1604 		goto err_exit;
1605 	}
1606 
1607 	disable_irq(adapter->irq_wakeup);
1608 	if (device_init_wakeup(dev, true)) {
1609 		dev_err(dev, "fail to init wakeup for mwifiex\n");
1610 		goto err_exit;
1611 	}
1612 	return;
1613 
1614 err_exit:
1615 	adapter->irq_wakeup = -1;
1616 }
1617 
1618 /*
1619  * This function adds the card.
1620  *
1621  * This function follows the following major steps to set up the device -
1622  *      - Initialize software. This includes probing the card, registering
1623  *        the interface operations table, and allocating/initializing the
1624  *        adapter structure
1625  *      - Set up the netlink socket
1626  *      - Create and start the main work queue
1627  *      - Register the device
1628  *      - Initialize firmware and hardware
1629  *      - Add logical interfaces
1630  */
1631 int
mwifiex_add_card(void * card,struct completion * fw_done,struct mwifiex_if_ops * if_ops,u8 iface_type,struct device * dev)1632 mwifiex_add_card(void *card, struct completion *fw_done,
1633 		 struct mwifiex_if_ops *if_ops, u8 iface_type,
1634 		 struct device *dev)
1635 {
1636 	struct mwifiex_adapter *adapter;
1637 
1638 	if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
1639 		pr_err("%s: software init failed\n", __func__);
1640 		goto err_init_sw;
1641 	}
1642 
1643 	mwifiex_probe_of(adapter);
1644 
1645 	adapter->iface_type = iface_type;
1646 	adapter->fw_done = fw_done;
1647 
1648 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1649 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1650 	init_waitqueue_head(&adapter->init_wait_q);
1651 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1652 	adapter->hs_activated = false;
1653 	init_waitqueue_head(&adapter->hs_activate_wait_q);
1654 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
1655 	adapter->cmd_wait_q.status = 0;
1656 	adapter->scan_wait_q_woken = false;
1657 
1658 	if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1659 		adapter->rx_work_enabled = true;
1660 
1661 	adapter->workqueue =
1662 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
1663 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1664 	if (!adapter->workqueue)
1665 		goto err_kmalloc;
1666 
1667 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1668 
1669 	if (adapter->rx_work_enabled) {
1670 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1671 							WQ_HIGHPRI |
1672 							WQ_MEM_RECLAIM |
1673 							WQ_UNBOUND, 1);
1674 		if (!adapter->rx_workqueue)
1675 			goto err_kmalloc;
1676 
1677 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1678 	}
1679 
1680 	/* Register the device. Fill up the private data structure with relevant
1681 	   information from the card. */
1682 	if (adapter->if_ops.register_dev(adapter)) {
1683 		pr_err("%s: failed to register mwifiex device\n", __func__);
1684 		goto err_registerdev;
1685 	}
1686 
1687 	if (mwifiex_init_hw_fw(adapter, true)) {
1688 		pr_err("%s: firmware init failed\n", __func__);
1689 		goto err_init_fw;
1690 	}
1691 
1692 	return 0;
1693 
1694 err_init_fw:
1695 	pr_debug("info: %s: unregister device\n", __func__);
1696 	if (adapter->if_ops.unregister_dev)
1697 		adapter->if_ops.unregister_dev(adapter);
1698 err_registerdev:
1699 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1700 	mwifiex_terminate_workqueue(adapter);
1701 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1702 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
1703 		mwifiex_shutdown_drv(adapter);
1704 		mwifiex_free_cmd_buffers(adapter);
1705 	}
1706 err_kmalloc:
1707 	if (adapter->irq_wakeup >= 0)
1708 		device_init_wakeup(adapter->dev, false);
1709 	mwifiex_free_adapter(adapter);
1710 
1711 err_init_sw:
1712 
1713 	return -1;
1714 }
1715 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1716 
1717 /*
1718  * This function removes the card.
1719  *
1720  * This function follows the following major steps to remove the device -
1721  *      - Stop data traffic
1722  *      - Shutdown firmware
1723  *      - Remove the logical interfaces
1724  *      - Terminate the work queue
1725  *      - Unregister the device
1726  *      - Free the adapter structure
1727  */
mwifiex_remove_card(struct mwifiex_adapter * adapter)1728 int mwifiex_remove_card(struct mwifiex_adapter *adapter)
1729 {
1730 	if (!adapter)
1731 		return 0;
1732 
1733 	mwifiex_uninit_sw(adapter);
1734 
1735 	if (adapter->irq_wakeup >= 0)
1736 		device_init_wakeup(adapter->dev, false);
1737 
1738 	/* Unregister device */
1739 	mwifiex_dbg(adapter, INFO,
1740 		    "info: unregister device\n");
1741 	if (adapter->if_ops.unregister_dev)
1742 		adapter->if_ops.unregister_dev(adapter);
1743 	/* Free adapter structure */
1744 	mwifiex_dbg(adapter, INFO,
1745 		    "info: free adapter\n");
1746 	mwifiex_free_adapter(adapter);
1747 
1748 	return 0;
1749 }
1750 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1751 
_mwifiex_dbg(const struct mwifiex_adapter * adapter,int mask,const char * fmt,...)1752 void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1753 		  const char *fmt, ...)
1754 {
1755 	struct va_format vaf;
1756 	va_list args;
1757 
1758 	if (!(adapter->debug_mask & mask))
1759 		return;
1760 
1761 	va_start(args, fmt);
1762 
1763 	vaf.fmt = fmt;
1764 	vaf.va = &args;
1765 
1766 	if (adapter->dev)
1767 		dev_info(adapter->dev, "%pV", &vaf);
1768 	else
1769 		pr_info("%pV", &vaf);
1770 
1771 	va_end(args);
1772 }
1773 EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1774 
1775 /*
1776  * This function initializes the module.
1777  *
1778  * The debug FS is also initialized if configured.
1779  */
1780 static int
mwifiex_init_module(void)1781 mwifiex_init_module(void)
1782 {
1783 #ifdef CONFIG_DEBUG_FS
1784 	mwifiex_debugfs_init();
1785 #endif
1786 	return 0;
1787 }
1788 
1789 /*
1790  * This function cleans up the module.
1791  *
1792  * The debug FS is removed if available.
1793  */
1794 static void
mwifiex_cleanup_module(void)1795 mwifiex_cleanup_module(void)
1796 {
1797 #ifdef CONFIG_DEBUG_FS
1798 	mwifiex_debugfs_remove();
1799 #endif
1800 }
1801 
1802 module_init(mwifiex_init_module);
1803 module_exit(mwifiex_cleanup_module);
1804 
1805 MODULE_AUTHOR("Marvell International Ltd.");
1806 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1807 MODULE_VERSION(VERSION);
1808 MODULE_LICENSE("GPL v2");
1809