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