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