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