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