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