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