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