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