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