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