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