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