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 	del_timer_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 		if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
729 			    sizeof(adapter->fw_name)) >=
730 			    sizeof(adapter->fw_name)) {
731 			pr_err("%s: fw_name too long!\n", __func__);
732 			return -1;
733 		}
734 	}
735 
736 	if (req_fw_nowait) {
737 		ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
738 					      adapter->dev, GFP_KERNEL, adapter,
739 					      mwifiex_fw_dpc);
740 	} else {
741 		ret = request_firmware(&adapter->firmware,
742 				       adapter->fw_name,
743 				       adapter->dev);
744 	}
745 
746 	if (ret < 0)
747 		mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
748 			    req_fw_nowait ? "_nowait" : "", ret);
749 	return ret;
750 }
751 
752 /*
753  * CFG802.11 network device handler for open.
754  *
755  * Starts the data queue.
756  */
757 static int
mwifiex_open(struct net_device * dev)758 mwifiex_open(struct net_device *dev)
759 {
760 	netif_carrier_off(dev);
761 
762 	return 0;
763 }
764 
765 /*
766  * CFG802.11 network device handler for close.
767  */
768 static int
mwifiex_close(struct net_device * dev)769 mwifiex_close(struct net_device *dev)
770 {
771 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
772 
773 	if (priv->scan_request) {
774 		struct cfg80211_scan_info info = {
775 			.aborted = true,
776 		};
777 
778 		mwifiex_dbg(priv->adapter, INFO,
779 			    "aborting scan on ndo_stop\n");
780 		cfg80211_scan_done(priv->scan_request, &info);
781 		priv->scan_request = NULL;
782 		priv->scan_aborting = true;
783 	}
784 
785 	if (priv->sched_scanning) {
786 		mwifiex_dbg(priv->adapter, INFO,
787 			    "aborting bgscan on ndo_stop\n");
788 		mwifiex_stop_bg_scan(priv);
789 		cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
790 	}
791 
792 	return 0;
793 }
794 
795 static bool
mwifiex_bypass_tx_queue(struct mwifiex_private * priv,struct sk_buff * skb)796 mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
797 			struct sk_buff *skb)
798 {
799 	struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
800 
801 	if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
802 	    mwifiex_is_skb_mgmt_frame(skb) ||
803 	    (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
804 	     ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
805 	     (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
806 		mwifiex_dbg(priv->adapter, DATA,
807 			    "bypass txqueue; eth type %#x, mgmt %d\n",
808 			     ntohs(eth_hdr->h_proto),
809 			     mwifiex_is_skb_mgmt_frame(skb));
810 		return true;
811 	}
812 
813 	return false;
814 }
815 /*
816  * Add buffer into wmm tx queue and queue work to transmit it.
817  */
mwifiex_queue_tx_pkt(struct mwifiex_private * priv,struct sk_buff * skb)818 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
819 {
820 	struct netdev_queue *txq;
821 	int index = mwifiex_1d_to_wmm_queue[skb->priority];
822 
823 	if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
824 		txq = netdev_get_tx_queue(priv->netdev, index);
825 		if (!netif_tx_queue_stopped(txq)) {
826 			netif_tx_stop_queue(txq);
827 			mwifiex_dbg(priv->adapter, DATA,
828 				    "stop queue: %d\n", index);
829 		}
830 	}
831 
832 	if (mwifiex_bypass_tx_queue(priv, skb)) {
833 		atomic_inc(&priv->adapter->tx_pending);
834 		atomic_inc(&priv->adapter->bypass_tx_pending);
835 		mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
836 	 } else {
837 		atomic_inc(&priv->adapter->tx_pending);
838 		mwifiex_wmm_add_buf_txqueue(priv, skb);
839 	 }
840 
841 	mwifiex_queue_main_work(priv->adapter);
842 
843 	return 0;
844 }
845 
846 struct sk_buff *
mwifiex_clone_skb_for_tx_status(struct mwifiex_private * priv,struct sk_buff * skb,u8 flag,u64 * cookie)847 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
848 				struct sk_buff *skb, u8 flag, u64 *cookie)
849 {
850 	struct sk_buff *orig_skb = skb;
851 	struct mwifiex_txinfo *tx_info, *orig_tx_info;
852 
853 	skb = skb_clone(skb, GFP_ATOMIC);
854 	if (skb) {
855 		int id;
856 
857 		spin_lock_bh(&priv->ack_status_lock);
858 		id = idr_alloc(&priv->ack_status_frames, orig_skb,
859 			       1, 0x10, GFP_ATOMIC);
860 		spin_unlock_bh(&priv->ack_status_lock);
861 
862 		if (id >= 0) {
863 			tx_info = MWIFIEX_SKB_TXCB(skb);
864 			tx_info->ack_frame_id = id;
865 			tx_info->flags |= flag;
866 			orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
867 			orig_tx_info->ack_frame_id = id;
868 			orig_tx_info->flags |= flag;
869 
870 			if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
871 				orig_tx_info->cookie = *cookie;
872 
873 		} else if (skb_shared(skb)) {
874 			kfree_skb(orig_skb);
875 		} else {
876 			kfree_skb(skb);
877 			skb = orig_skb;
878 		}
879 	} else {
880 		/* couldn't clone -- lose tx status ... */
881 		skb = orig_skb;
882 	}
883 
884 	return skb;
885 }
886 
887 /*
888  * CFG802.11 network device handler for data transmission.
889  */
890 static netdev_tx_t
mwifiex_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)891 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
892 {
893 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
894 	struct sk_buff *new_skb;
895 	struct mwifiex_txinfo *tx_info;
896 	bool multicast;
897 
898 	mwifiex_dbg(priv->adapter, DATA,
899 		    "data: %lu BSS(%d-%d): Data <= kernel\n",
900 		    jiffies, priv->bss_type, priv->bss_num);
901 
902 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &priv->adapter->work_flags)) {
903 		kfree_skb(skb);
904 		priv->stats.tx_dropped++;
905 		return 0;
906 	}
907 	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
908 		mwifiex_dbg(priv->adapter, ERROR,
909 			    "Tx: bad skb len %d\n", skb->len);
910 		kfree_skb(skb);
911 		priv->stats.tx_dropped++;
912 		return 0;
913 	}
914 	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
915 		mwifiex_dbg(priv->adapter, DATA,
916 			    "data: Tx: insufficient skb headroom %d\n",
917 			    skb_headroom(skb));
918 		/* Insufficient skb headroom - allocate a new skb */
919 		new_skb =
920 			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
921 		if (unlikely(!new_skb)) {
922 			mwifiex_dbg(priv->adapter, ERROR,
923 				    "Tx: cannot alloca new_skb\n");
924 			kfree_skb(skb);
925 			priv->stats.tx_dropped++;
926 			return 0;
927 		}
928 		kfree_skb(skb);
929 		skb = new_skb;
930 		mwifiex_dbg(priv->adapter, INFO,
931 			    "info: new skb headroomd %d\n",
932 			    skb_headroom(skb));
933 	}
934 
935 	tx_info = MWIFIEX_SKB_TXCB(skb);
936 	memset(tx_info, 0, sizeof(*tx_info));
937 	tx_info->bss_num = priv->bss_num;
938 	tx_info->bss_type = priv->bss_type;
939 	tx_info->pkt_len = skb->len;
940 
941 	multicast = is_multicast_ether_addr(skb->data);
942 
943 	if (unlikely(!multicast && skb->sk &&
944 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
945 		     priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
946 		skb = mwifiex_clone_skb_for_tx_status(priv,
947 						      skb,
948 					MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
949 
950 	/* Record the current time the packet was queued; used to
951 	 * determine the amount of time the packet was queued in
952 	 * the driver before it was sent to the firmware.
953 	 * The delay is then sent along with the packet to the
954 	 * firmware for aggregate delay calculation for stats and
955 	 * MSDU lifetime expiry.
956 	 */
957 	__net_timestamp(skb);
958 
959 	if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
960 	    priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
961 	    !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
962 		if (priv->adapter->auto_tdls && priv->check_tdls_tx)
963 			mwifiex_tdls_check_tx(priv, skb);
964 	}
965 
966 	mwifiex_queue_tx_pkt(priv, skb);
967 
968 	return 0;
969 }
970 
mwifiex_set_mac_address(struct mwifiex_private * priv,struct net_device * dev,bool external,u8 * new_mac)971 int mwifiex_set_mac_address(struct mwifiex_private *priv,
972 			    struct net_device *dev, bool external,
973 			    u8 *new_mac)
974 {
975 	int ret;
976 	u64 mac_addr, old_mac_addr;
977 
978 	old_mac_addr = ether_addr_to_u64(priv->curr_addr);
979 
980 	if (external) {
981 		mac_addr = ether_addr_to_u64(new_mac);
982 	} else {
983 		/* Internal mac address change */
984 		if (priv->bss_type == MWIFIEX_BSS_TYPE_ANY)
985 			return -EOPNOTSUPP;
986 
987 		mac_addr = old_mac_addr;
988 
989 		if (priv->bss_type == MWIFIEX_BSS_TYPE_P2P) {
990 			mac_addr |= BIT_ULL(MWIFIEX_MAC_LOCAL_ADMIN_BIT);
991 			mac_addr += priv->bss_num;
992 		} else if (priv->adapter->priv[0] != priv) {
993 			/* Set mac address based on bss_type/bss_num */
994 			mac_addr ^= BIT_ULL(priv->bss_type + 8);
995 			mac_addr += priv->bss_num;
996 		}
997 	}
998 
999 	u64_to_ether_addr(mac_addr, priv->curr_addr);
1000 
1001 	/* Send request to firmware */
1002 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
1003 			       HostCmd_ACT_GEN_SET, 0, NULL, true);
1004 
1005 	if (ret) {
1006 		u64_to_ether_addr(old_mac_addr, priv->curr_addr);
1007 		mwifiex_dbg(priv->adapter, ERROR,
1008 			    "set mac address failed: ret=%d\n", ret);
1009 		return ret;
1010 	}
1011 
1012 	eth_hw_addr_set(dev, priv->curr_addr);
1013 	return 0;
1014 }
1015 
1016 /* CFG802.11 network device handler for setting MAC address.
1017  */
1018 static int
mwifiex_ndo_set_mac_address(struct net_device * dev,void * addr)1019 mwifiex_ndo_set_mac_address(struct net_device *dev, void *addr)
1020 {
1021 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1022 	struct sockaddr *hw_addr = addr;
1023 
1024 	return mwifiex_set_mac_address(priv, dev, true, hw_addr->sa_data);
1025 }
1026 
1027 /*
1028  * CFG802.11 network device handler for setting multicast list.
1029  */
mwifiex_set_multicast_list(struct net_device * dev)1030 static void mwifiex_set_multicast_list(struct net_device *dev)
1031 {
1032 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1033 	struct mwifiex_multicast_list mcast_list;
1034 
1035 	if (dev->flags & IFF_PROMISC) {
1036 		mcast_list.mode = MWIFIEX_PROMISC_MODE;
1037 	} else if (dev->flags & IFF_ALLMULTI ||
1038 		   netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
1039 		mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
1040 	} else {
1041 		mcast_list.mode = MWIFIEX_MULTICAST_MODE;
1042 		mcast_list.num_multicast_addr =
1043 			mwifiex_copy_mcast_addr(&mcast_list, dev);
1044 	}
1045 	mwifiex_request_set_multicast_list(priv, &mcast_list);
1046 }
1047 
1048 /*
1049  * CFG802.11 network device handler for transmission timeout.
1050  */
1051 static void
mwifiex_tx_timeout(struct net_device * dev,unsigned int txqueue)1052 mwifiex_tx_timeout(struct net_device *dev, unsigned int txqueue)
1053 {
1054 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1055 
1056 	priv->num_tx_timeout++;
1057 	priv->tx_timeout_cnt++;
1058 	mwifiex_dbg(priv->adapter, ERROR,
1059 		    "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
1060 		    jiffies, priv->tx_timeout_cnt, priv->bss_type,
1061 		    priv->bss_num);
1062 	mwifiex_set_trans_start(dev);
1063 
1064 	if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
1065 	    priv->adapter->if_ops.card_reset) {
1066 		mwifiex_dbg(priv->adapter, ERROR,
1067 			    "tx_timeout_cnt exceeds threshold.\t"
1068 			    "Triggering card reset!\n");
1069 		priv->adapter->if_ops.card_reset(priv->adapter);
1070 	}
1071 }
1072 
mwifiex_multi_chan_resync(struct mwifiex_adapter * adapter)1073 void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1074 {
1075 	struct usb_card_rec *card = adapter->card;
1076 	struct mwifiex_private *priv;
1077 	u16 tx_buf_size;
1078 	int i, ret;
1079 
1080 	card->mc_resync_flag = true;
1081 	for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1082 		if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1083 			mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1084 			return;
1085 		}
1086 	}
1087 
1088 	card->mc_resync_flag = false;
1089 	tx_buf_size = 0xffff;
1090 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1091 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1092 			       HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1093 	if (ret)
1094 		mwifiex_dbg(adapter, ERROR,
1095 			    "send reconfig tx buf size cmd err\n");
1096 }
1097 EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1098 
mwifiex_upload_device_dump(struct mwifiex_adapter * adapter)1099 void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1100 {
1101 	/* Dump all the memory data into single file, a userspace script will
1102 	 * be used to split all the memory data to multiple files
1103 	 */
1104 	mwifiex_dbg(adapter, MSG,
1105 		    "== mwifiex dump information to /sys/class/devcoredump start\n");
1106 	dev_coredumpv(adapter->dev, adapter->devdump_data, adapter->devdump_len,
1107 		      GFP_KERNEL);
1108 	mwifiex_dbg(adapter, MSG,
1109 		    "== mwifiex dump information to /sys/class/devcoredump end\n");
1110 
1111 	/* Device dump data will be freed in device coredump release function
1112 	 * after 5 min. Here reset adapter->devdump_data and ->devdump_len
1113 	 * to avoid it been accidentally reused.
1114 	 */
1115 	adapter->devdump_data = NULL;
1116 	adapter->devdump_len = 0;
1117 }
1118 EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1119 
mwifiex_drv_info_dump(struct mwifiex_adapter * adapter)1120 void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
1121 {
1122 	char *p;
1123 	char drv_version[64];
1124 	struct usb_card_rec *cardp;
1125 	struct sdio_mmc_card *sdio_card;
1126 	struct mwifiex_private *priv;
1127 	int i, idx;
1128 	struct netdev_queue *txq;
1129 	struct mwifiex_debug_info *debug_info;
1130 
1131 	mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
1132 
1133 	p = adapter->devdump_data;
1134 	strcpy(p, "========Start dump driverinfo========\n");
1135 	p += strlen("========Start dump driverinfo========\n");
1136 	p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1137 
1138 	mwifiex_drv_get_driver_version(adapter, drv_version,
1139 				       sizeof(drv_version) - 1);
1140 	p += sprintf(p, "driver_version = %s\n", drv_version);
1141 
1142 	if (adapter->iface_type == MWIFIEX_USB) {
1143 		cardp = (struct usb_card_rec *)adapter->card;
1144 		p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1145 			     atomic_read(&cardp->tx_cmd_urb_pending));
1146 		p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1147 			     atomic_read(&cardp->port[0].tx_data_urb_pending));
1148 		p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1149 			     atomic_read(&cardp->port[1].tx_data_urb_pending));
1150 		p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1151 			     atomic_read(&cardp->rx_cmd_urb_pending));
1152 		p += sprintf(p, "rx_data_urb_pending = %d\n",
1153 			     atomic_read(&cardp->rx_data_urb_pending));
1154 	}
1155 
1156 	p += sprintf(p, "tx_pending = %d\n",
1157 		     atomic_read(&adapter->tx_pending));
1158 	p += sprintf(p, "rx_pending = %d\n",
1159 		     atomic_read(&adapter->rx_pending));
1160 
1161 	if (adapter->iface_type == MWIFIEX_SDIO) {
1162 		sdio_card = (struct sdio_mmc_card *)adapter->card;
1163 		p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1164 			     sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1165 		p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1166 			     sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1167 	}
1168 
1169 	for (i = 0; i < adapter->priv_num; i++) {
1170 		if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1171 			continue;
1172 		priv = adapter->priv[i];
1173 		p += sprintf(p, "\n[interface  : \"%s\"]\n",
1174 			     priv->netdev->name);
1175 		p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1176 			     atomic_read(&priv->wmm_tx_pending[0]));
1177 		p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1178 			     atomic_read(&priv->wmm_tx_pending[1]));
1179 		p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1180 			     atomic_read(&priv->wmm_tx_pending[2]));
1181 		p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1182 			     atomic_read(&priv->wmm_tx_pending[3]));
1183 		p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1184 			     "Disconnected" : "Connected");
1185 		p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1186 			     ? "on" : "off"));
1187 		for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1188 			txq = netdev_get_tx_queue(priv->netdev, idx);
1189 			p += sprintf(p, "tx queue %d:%s  ", idx,
1190 				     netif_tx_queue_stopped(txq) ?
1191 				     "stopped" : "started");
1192 		}
1193 		p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1194 			     priv->netdev->name, priv->num_tx_timeout);
1195 	}
1196 
1197 	if (adapter->iface_type == MWIFIEX_SDIO ||
1198 	    adapter->iface_type == MWIFIEX_PCIE) {
1199 		p += sprintf(p, "\n=== %s register dump===\n",
1200 			     adapter->iface_type == MWIFIEX_SDIO ?
1201 							"SDIO" : "PCIE");
1202 		if (adapter->if_ops.reg_dump)
1203 			p += adapter->if_ops.reg_dump(adapter, p);
1204 	}
1205 	p += sprintf(p, "\n=== more debug information\n");
1206 	debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1207 	if (debug_info) {
1208 		for (i = 0; i < adapter->priv_num; i++) {
1209 			if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1210 				continue;
1211 			priv = adapter->priv[i];
1212 			mwifiex_get_debug_info(priv, debug_info);
1213 			p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1214 			break;
1215 		}
1216 		kfree(debug_info);
1217 	}
1218 
1219 	strcpy(p, "\n========End dump========\n");
1220 	p += strlen("\n========End dump========\n");
1221 	mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1222 	adapter->devdump_len = p - (char *)adapter->devdump_data;
1223 }
1224 EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1225 
mwifiex_prepare_fw_dump_info(struct mwifiex_adapter * adapter)1226 void mwifiex_prepare_fw_dump_info(struct mwifiex_adapter *adapter)
1227 {
1228 	u8 idx;
1229 	char *fw_dump_ptr;
1230 	u32 dump_len = 0;
1231 
1232 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1233 		struct memory_type_mapping *entry =
1234 				&adapter->mem_type_mapping_tbl[idx];
1235 
1236 		if (entry->mem_ptr) {
1237 			dump_len += (strlen("========Start dump ") +
1238 					strlen(entry->mem_name) +
1239 					strlen("========\n") +
1240 					(entry->mem_size + 1) +
1241 					strlen("\n========End dump========\n"));
1242 		}
1243 	}
1244 
1245 	if (dump_len + 1 + adapter->devdump_len > MWIFIEX_FW_DUMP_SIZE) {
1246 		/* Realloc in case buffer overflow */
1247 		fw_dump_ptr = vzalloc(dump_len + 1 + adapter->devdump_len);
1248 		mwifiex_dbg(adapter, MSG, "Realloc device dump data.\n");
1249 		if (!fw_dump_ptr) {
1250 			vfree(adapter->devdump_data);
1251 			mwifiex_dbg(adapter, ERROR,
1252 				    "vzalloc devdump data failure!\n");
1253 			return;
1254 		}
1255 
1256 		memmove(fw_dump_ptr, adapter->devdump_data,
1257 			adapter->devdump_len);
1258 		vfree(adapter->devdump_data);
1259 		adapter->devdump_data = fw_dump_ptr;
1260 	}
1261 
1262 	fw_dump_ptr = (char *)adapter->devdump_data + adapter->devdump_len;
1263 
1264 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1265 		struct memory_type_mapping *entry =
1266 					&adapter->mem_type_mapping_tbl[idx];
1267 
1268 		if (entry->mem_ptr) {
1269 			strcpy(fw_dump_ptr, "========Start dump ");
1270 			fw_dump_ptr += strlen("========Start dump ");
1271 
1272 			strcpy(fw_dump_ptr, entry->mem_name);
1273 			fw_dump_ptr += strlen(entry->mem_name);
1274 
1275 			strcpy(fw_dump_ptr, "========\n");
1276 			fw_dump_ptr += strlen("========\n");
1277 
1278 			memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1279 			fw_dump_ptr += entry->mem_size;
1280 
1281 			strcpy(fw_dump_ptr, "\n========End dump========\n");
1282 			fw_dump_ptr += strlen("\n========End dump========\n");
1283 		}
1284 	}
1285 
1286 	adapter->devdump_len = fw_dump_ptr - (char *)adapter->devdump_data;
1287 
1288 	for (idx = 0; idx < adapter->num_mem_types; idx++) {
1289 		struct memory_type_mapping *entry =
1290 			&adapter->mem_type_mapping_tbl[idx];
1291 
1292 		vfree(entry->mem_ptr);
1293 		entry->mem_ptr = NULL;
1294 		entry->mem_size = 0;
1295 	}
1296 }
1297 EXPORT_SYMBOL_GPL(mwifiex_prepare_fw_dump_info);
1298 
1299 /*
1300  * CFG802.11 network device handler for statistics retrieval.
1301  */
mwifiex_get_stats(struct net_device * dev)1302 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1303 {
1304 	struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1305 
1306 	return &priv->stats;
1307 }
1308 
1309 static u16
mwifiex_netdev_select_wmm_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)1310 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1311 				struct net_device *sb_dev)
1312 {
1313 	skb->priority = cfg80211_classify8021d(skb, NULL);
1314 	return mwifiex_1d_to_wmm_queue[skb->priority];
1315 }
1316 
1317 /* Network device handlers */
1318 static const struct net_device_ops mwifiex_netdev_ops = {
1319 	.ndo_open = mwifiex_open,
1320 	.ndo_stop = mwifiex_close,
1321 	.ndo_start_xmit = mwifiex_hard_start_xmit,
1322 	.ndo_set_mac_address = mwifiex_ndo_set_mac_address,
1323 	.ndo_validate_addr = eth_validate_addr,
1324 	.ndo_tx_timeout = mwifiex_tx_timeout,
1325 	.ndo_get_stats = mwifiex_get_stats,
1326 	.ndo_set_rx_mode = mwifiex_set_multicast_list,
1327 	.ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1328 };
1329 
1330 /*
1331  * This function initializes the private structure parameters.
1332  *
1333  * The following wait queues are initialized -
1334  *      - IOCTL wait queue
1335  *      - Command wait queue
1336  *      - Statistics wait queue
1337  *
1338  * ...and the following default parameters are set -
1339  *      - Current key index     : Set to 0
1340  *      - Rate index            : Set to auto
1341  *      - Media connected       : Set to disconnected
1342  *      - Adhoc link sensed     : Set to false
1343  *      - Nick name             : Set to null
1344  *      - Number of Tx timeout  : Set to 0
1345  *      - Device address        : Set to current address
1346  *      - Rx histogram statistc : Set to 0
1347  *
1348  * In addition, the CFG80211 work queue is also created.
1349  */
mwifiex_init_priv_params(struct mwifiex_private * priv,struct net_device * dev)1350 void mwifiex_init_priv_params(struct mwifiex_private *priv,
1351 			      struct net_device *dev)
1352 {
1353 	dev->netdev_ops = &mwifiex_netdev_ops;
1354 	dev->needs_free_netdev = true;
1355 	/* Initialize private structure */
1356 	priv->current_key_index = 0;
1357 	priv->media_connected = false;
1358 	memset(priv->mgmt_ie, 0,
1359 	       sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1360 	priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1361 	priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1362 	priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1363 	priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1364 	priv->num_tx_timeout = 0;
1365 	if (is_valid_ether_addr(dev->dev_addr))
1366 		ether_addr_copy(priv->curr_addr, dev->dev_addr);
1367 	else
1368 		ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1369 
1370 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1371 	    GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1372 		priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1373 		if (priv->hist_data)
1374 			mwifiex_hist_data_reset(priv);
1375 	}
1376 }
1377 
1378 /*
1379  * This function check if command is pending.
1380  */
is_command_pending(struct mwifiex_adapter * adapter)1381 int is_command_pending(struct mwifiex_adapter *adapter)
1382 {
1383 	int is_cmd_pend_q_empty;
1384 
1385 	spin_lock_bh(&adapter->cmd_pending_q_lock);
1386 	is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1387 	spin_unlock_bh(&adapter->cmd_pending_q_lock);
1388 
1389 	return !is_cmd_pend_q_empty;
1390 }
1391 
1392 /*
1393  * This is the RX work queue function.
1394  *
1395  * It handles the RX operations.
1396  */
mwifiex_rx_work_queue(struct work_struct * work)1397 static void mwifiex_rx_work_queue(struct work_struct *work)
1398 {
1399 	struct mwifiex_adapter *adapter =
1400 		container_of(work, struct mwifiex_adapter, rx_work);
1401 
1402 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1403 		return;
1404 	mwifiex_process_rx(adapter);
1405 }
1406 
1407 /*
1408  * This is the main work queue function.
1409  *
1410  * It handles the main process, which in turn handles the complete
1411  * driver operations.
1412  */
mwifiex_main_work_queue(struct work_struct * work)1413 static void mwifiex_main_work_queue(struct work_struct *work)
1414 {
1415 	struct mwifiex_adapter *adapter =
1416 		container_of(work, struct mwifiex_adapter, main_work);
1417 
1418 	if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
1419 		return;
1420 	mwifiex_main_process(adapter);
1421 }
1422 
1423 /* Common teardown code used for both device removal and reset */
mwifiex_uninit_sw(struct mwifiex_adapter * adapter)1424 static void mwifiex_uninit_sw(struct mwifiex_adapter *adapter)
1425 {
1426 	struct mwifiex_private *priv;
1427 	int i;
1428 
1429 	/* We can no longer handle interrupts once we start doing the teardown
1430 	 * below.
1431 	 */
1432 	if (adapter->if_ops.disable_int)
1433 		adapter->if_ops.disable_int(adapter);
1434 
1435 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1436 	mwifiex_terminate_workqueue(adapter);
1437 	adapter->int_status = 0;
1438 
1439 	/* Stop data */
1440 	for (i = 0; i < adapter->priv_num; i++) {
1441 		priv = adapter->priv[i];
1442 		if (priv && priv->netdev) {
1443 			mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1444 			if (netif_carrier_ok(priv->netdev))
1445 				netif_carrier_off(priv->netdev);
1446 			netif_device_detach(priv->netdev);
1447 		}
1448 	}
1449 
1450 	mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
1451 	mwifiex_shutdown_drv(adapter);
1452 	mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1453 
1454 	if (atomic_read(&adapter->rx_pending) ||
1455 	    atomic_read(&adapter->tx_pending) ||
1456 	    atomic_read(&adapter->cmd_pending)) {
1457 		mwifiex_dbg(adapter, ERROR,
1458 			    "rx_pending=%d, tx_pending=%d,\t"
1459 			    "cmd_pending=%d\n",
1460 			    atomic_read(&adapter->rx_pending),
1461 			    atomic_read(&adapter->tx_pending),
1462 			    atomic_read(&adapter->cmd_pending));
1463 	}
1464 
1465 	for (i = 0; i < adapter->priv_num; i++) {
1466 		priv = adapter->priv[i];
1467 		if (!priv)
1468 			continue;
1469 		rtnl_lock();
1470 		if (priv->netdev &&
1471 		    priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
1472 			/*
1473 			 * Close the netdev now, because if we do it later, the
1474 			 * netdev notifiers will need to acquire the wiphy lock
1475 			 * again --> deadlock.
1476 			 */
1477 			dev_close(priv->wdev.netdev);
1478 			wiphy_lock(adapter->wiphy);
1479 			mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1480 			wiphy_unlock(adapter->wiphy);
1481 		}
1482 		rtnl_unlock();
1483 	}
1484 
1485 	wiphy_unregister(adapter->wiphy);
1486 	wiphy_free(adapter->wiphy);
1487 	adapter->wiphy = NULL;
1488 
1489 	vfree(adapter->chan_stats);
1490 	mwifiex_free_cmd_buffers(adapter);
1491 }
1492 
1493 /*
1494  * This function can be used for shutting down the adapter SW.
1495  */
mwifiex_shutdown_sw(struct mwifiex_adapter * adapter)1496 int mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
1497 {
1498 	struct mwifiex_private *priv;
1499 
1500 	if (!adapter)
1501 		return 0;
1502 
1503 	wait_for_completion(adapter->fw_done);
1504 	/* Caller should ensure we aren't suspending while this happens */
1505 	reinit_completion(adapter->fw_done);
1506 
1507 	priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1508 	mwifiex_deauthenticate(priv, NULL);
1509 
1510 	mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
1511 
1512 	mwifiex_uninit_sw(adapter);
1513 	adapter->is_up = false;
1514 
1515 	if (adapter->if_ops.down_dev)
1516 		adapter->if_ops.down_dev(adapter);
1517 
1518 	return 0;
1519 }
1520 EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
1521 
1522 /* This function can be used for reinitting the adapter SW. Required
1523  * code is extracted from mwifiex_add_card()
1524  */
1525 int
mwifiex_reinit_sw(struct mwifiex_adapter * adapter)1526 mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
1527 {
1528 	int ret;
1529 
1530 	mwifiex_init_lock_list(adapter);
1531 	if (adapter->if_ops.up_dev)
1532 		adapter->if_ops.up_dev(adapter);
1533 
1534 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1535 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1536 	init_waitqueue_head(&adapter->init_wait_q);
1537 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1538 	adapter->hs_activated = false;
1539 	clear_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags);
1540 	init_waitqueue_head(&adapter->hs_activate_wait_q);
1541 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
1542 	adapter->cmd_wait_q.status = 0;
1543 	adapter->scan_wait_q_woken = false;
1544 
1545 	if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1546 		adapter->rx_work_enabled = true;
1547 
1548 	adapter->workqueue =
1549 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
1550 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1551 	if (!adapter->workqueue)
1552 		goto err_kmalloc;
1553 
1554 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1555 
1556 	if (adapter->rx_work_enabled) {
1557 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1558 							WQ_HIGHPRI |
1559 							WQ_MEM_RECLAIM |
1560 							WQ_UNBOUND, 1);
1561 		if (!adapter->rx_workqueue)
1562 			goto err_kmalloc;
1563 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1564 	}
1565 
1566 	/* Register the device. Fill up the private data structure with
1567 	 * relevant information from the card. Some code extracted from
1568 	 * mwifiex_register_dev()
1569 	 */
1570 	mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
1571 
1572 	if (mwifiex_init_hw_fw(adapter, false)) {
1573 		mwifiex_dbg(adapter, ERROR,
1574 			    "%s: firmware init failed\n", __func__);
1575 		goto err_init_fw;
1576 	}
1577 
1578 	/* _mwifiex_fw_dpc() does its own cleanup */
1579 	ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1580 	if (ret) {
1581 		pr_err("Failed to bring up adapter: %d\n", ret);
1582 		return ret;
1583 	}
1584 	mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
1585 
1586 	return 0;
1587 
1588 err_init_fw:
1589 	mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1590 	if (adapter->if_ops.unregister_dev)
1591 		adapter->if_ops.unregister_dev(adapter);
1592 
1593 err_kmalloc:
1594 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1595 	mwifiex_terminate_workqueue(adapter);
1596 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1597 		mwifiex_dbg(adapter, ERROR,
1598 			    "info: %s: shutdown mwifiex\n", __func__);
1599 		mwifiex_shutdown_drv(adapter);
1600 		mwifiex_free_cmd_buffers(adapter);
1601 	}
1602 
1603 	complete_all(adapter->fw_done);
1604 	mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1605 
1606 	return -1;
1607 }
1608 EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
1609 
mwifiex_irq_wakeup_handler(int irq,void * priv)1610 static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1611 {
1612 	struct mwifiex_adapter *adapter = priv;
1613 
1614 	dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1615 	adapter->wake_by_wifi = true;
1616 	disable_irq_nosync(irq);
1617 
1618 	/* Notify PM core we are wakeup source */
1619 	pm_wakeup_event(adapter->dev, 0);
1620 	pm_system_wakeup();
1621 
1622 	return IRQ_HANDLED;
1623 }
1624 
mwifiex_probe_of(struct mwifiex_adapter * adapter)1625 static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1626 {
1627 	int ret;
1628 	struct device *dev = adapter->dev;
1629 
1630 	if (!dev->of_node)
1631 		goto err_exit;
1632 
1633 	adapter->dt_node = dev->of_node;
1634 	adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1635 	if (!adapter->irq_wakeup) {
1636 		dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1637 		goto err_exit;
1638 	}
1639 
1640 	ret = devm_request_irq(dev, adapter->irq_wakeup,
1641 			       mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1642 			       "wifi_wake", adapter);
1643 	if (ret) {
1644 		dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1645 			adapter->irq_wakeup, ret);
1646 		goto err_exit;
1647 	}
1648 
1649 	disable_irq(adapter->irq_wakeup);
1650 	if (device_init_wakeup(dev, true)) {
1651 		dev_err(dev, "fail to init wakeup for mwifiex\n");
1652 		goto err_exit;
1653 	}
1654 	return;
1655 
1656 err_exit:
1657 	adapter->irq_wakeup = -1;
1658 }
1659 
1660 /*
1661  * This function adds the card.
1662  *
1663  * This function follows the following major steps to set up the device -
1664  *      - Initialize software. This includes probing the card, registering
1665  *        the interface operations table, and allocating/initializing the
1666  *        adapter structure
1667  *      - Set up the netlink socket
1668  *      - Create and start the main work queue
1669  *      - Register the device
1670  *      - Initialize firmware and hardware
1671  *      - Add logical interfaces
1672  */
1673 int
mwifiex_add_card(void * card,struct completion * fw_done,struct mwifiex_if_ops * if_ops,u8 iface_type,struct device * dev)1674 mwifiex_add_card(void *card, struct completion *fw_done,
1675 		 struct mwifiex_if_ops *if_ops, u8 iface_type,
1676 		 struct device *dev)
1677 {
1678 	struct mwifiex_adapter *adapter;
1679 
1680 	if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
1681 		pr_err("%s: software init failed\n", __func__);
1682 		goto err_init_sw;
1683 	}
1684 
1685 	mwifiex_probe_of(adapter);
1686 
1687 	adapter->iface_type = iface_type;
1688 	adapter->fw_done = fw_done;
1689 
1690 	adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1691 	clear_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1692 	init_waitqueue_head(&adapter->init_wait_q);
1693 	clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
1694 	adapter->hs_activated = false;
1695 	init_waitqueue_head(&adapter->hs_activate_wait_q);
1696 	init_waitqueue_head(&adapter->cmd_wait_q.wait);
1697 	adapter->cmd_wait_q.status = 0;
1698 	adapter->scan_wait_q_woken = false;
1699 
1700 	if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1701 		adapter->rx_work_enabled = true;
1702 
1703 	adapter->workqueue =
1704 		alloc_workqueue("MWIFIEX_WORK_QUEUE",
1705 				WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1706 	if (!adapter->workqueue)
1707 		goto err_kmalloc;
1708 
1709 	INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1710 
1711 	if (adapter->rx_work_enabled) {
1712 		adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1713 							WQ_HIGHPRI |
1714 							WQ_MEM_RECLAIM |
1715 							WQ_UNBOUND, 1);
1716 		if (!adapter->rx_workqueue)
1717 			goto err_kmalloc;
1718 
1719 		INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1720 	}
1721 
1722 	/* Register the device. Fill up the private data structure with relevant
1723 	   information from the card. */
1724 	if (adapter->if_ops.register_dev(adapter)) {
1725 		pr_err("%s: failed to register mwifiex device\n", __func__);
1726 		goto err_registerdev;
1727 	}
1728 
1729 	if (mwifiex_init_hw_fw(adapter, true)) {
1730 		pr_err("%s: firmware init failed\n", __func__);
1731 		goto err_init_fw;
1732 	}
1733 
1734 	return 0;
1735 
1736 err_init_fw:
1737 	pr_debug("info: %s: unregister device\n", __func__);
1738 	if (adapter->if_ops.unregister_dev)
1739 		adapter->if_ops.unregister_dev(adapter);
1740 err_registerdev:
1741 	set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
1742 	mwifiex_terminate_workqueue(adapter);
1743 	if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1744 		pr_debug("info: %s: shutdown mwifiex\n", __func__);
1745 		mwifiex_shutdown_drv(adapter);
1746 		mwifiex_free_cmd_buffers(adapter);
1747 	}
1748 err_kmalloc:
1749 	if (adapter->irq_wakeup >= 0)
1750 		device_init_wakeup(adapter->dev, false);
1751 	mwifiex_free_adapter(adapter);
1752 
1753 err_init_sw:
1754 
1755 	return -1;
1756 }
1757 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1758 
1759 /*
1760  * This function removes the card.
1761  *
1762  * This function follows the following major steps to remove the device -
1763  *      - Stop data traffic
1764  *      - Shutdown firmware
1765  *      - Remove the logical interfaces
1766  *      - Terminate the work queue
1767  *      - Unregister the device
1768  *      - Free the adapter structure
1769  */
mwifiex_remove_card(struct mwifiex_adapter * adapter)1770 int mwifiex_remove_card(struct mwifiex_adapter *adapter)
1771 {
1772 	if (!adapter)
1773 		return 0;
1774 
1775 	if (adapter->is_up)
1776 		mwifiex_uninit_sw(adapter);
1777 
1778 	if (adapter->irq_wakeup >= 0)
1779 		device_init_wakeup(adapter->dev, false);
1780 
1781 	/* Unregister device */
1782 	mwifiex_dbg(adapter, INFO,
1783 		    "info: unregister device\n");
1784 	if (adapter->if_ops.unregister_dev)
1785 		adapter->if_ops.unregister_dev(adapter);
1786 	/* Free adapter structure */
1787 	mwifiex_dbg(adapter, INFO,
1788 		    "info: free adapter\n");
1789 	mwifiex_free_adapter(adapter);
1790 
1791 	return 0;
1792 }
1793 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1794 
_mwifiex_dbg(const struct mwifiex_adapter * adapter,int mask,const char * fmt,...)1795 void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1796 		  const char *fmt, ...)
1797 {
1798 	struct va_format vaf;
1799 	va_list args;
1800 
1801 	if (!(adapter->debug_mask & mask))
1802 		return;
1803 
1804 	va_start(args, fmt);
1805 
1806 	vaf.fmt = fmt;
1807 	vaf.va = &args;
1808 
1809 	if (adapter->dev)
1810 		dev_info(adapter->dev, "%pV", &vaf);
1811 	else
1812 		pr_info("%pV", &vaf);
1813 
1814 	va_end(args);
1815 }
1816 EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1817 
1818 /*
1819  * This function initializes the module.
1820  *
1821  * The debug FS is also initialized if configured.
1822  */
1823 static int
mwifiex_init_module(void)1824 mwifiex_init_module(void)
1825 {
1826 #ifdef CONFIG_DEBUG_FS
1827 	mwifiex_debugfs_init();
1828 #endif
1829 	return 0;
1830 }
1831 
1832 /*
1833  * This function cleans up the module.
1834  *
1835  * The debug FS is removed if available.
1836  */
1837 static void
mwifiex_cleanup_module(void)1838 mwifiex_cleanup_module(void)
1839 {
1840 #ifdef CONFIG_DEBUG_FS
1841 	mwifiex_debugfs_remove();
1842 #endif
1843 }
1844 
1845 module_init(mwifiex_init_module);
1846 module_exit(mwifiex_cleanup_module);
1847 
1848 MODULE_AUTHOR("Marvell International Ltd.");
1849 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1850 MODULE_VERSION(VERSION);
1851 MODULE_LICENSE("GPL v2");
1852