mwifiex: fix scan problem on big endian platforms
[deliverable/linux.git] / drivers / net / wireless / 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"
26
27const char driver_version[] = "mwifiex " VERSION " (%s) ";
388ec385
AK
28static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
5e6e3a92 30
0013c7ce
AP
31static unsigned short driver_mode;
32module_param(driver_mode, ushort, 0);
33MODULE_PARM_DESC(driver_mode,
34 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
35
5e6e3a92
BZ
36/*
37 * This function registers the device and performs all the necessary
38 * initializations.
39 *
40 * The following initialization operations are performed -
41 * - Allocate adapter structure
42 * - Save interface specific operations table in adapter
43 * - Call interface specific initialization routine
44 * - Allocate private structures
45 * - Set default adapter structure parameters
46 * - Initialize locks
47 *
48 * In case of any errors during inittialization, this function also ensures
49 * proper cleanup before exiting.
50 */
51static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
287546df 52 void **padapter)
5e6e3a92 53{
2be7859f
AK
54 struct mwifiex_adapter *adapter;
55 int i;
5e6e3a92
BZ
56
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
5e6e3a92 58 if (!adapter)
b53575ec 59 return -ENOMEM;
5e6e3a92 60
287546df 61 *padapter = adapter;
5e6e3a92
BZ
62 adapter->card = card;
63
64 /* Save interface specific operations in adapter */
65 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
66
67 /* card specific initialization has been deferred until now .. */
4daffe35
AK
68 if (adapter->if_ops.init_if)
69 if (adapter->if_ops.init_if(adapter))
70 goto error;
5e6e3a92
BZ
71
72 adapter->priv_num = 0;
5e6e3a92 73
64b05e2f
AP
74 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
75 /* Allocate memory for private structure */
76 adapter->priv[i] =
77 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
78 if (!adapter->priv[i])
79 goto error;
93a1df48 80
64b05e2f 81 adapter->priv[i]->adapter = adapter;
64b05e2f
AP
82 adapter->priv_num++;
83 }
44b815c6 84 mwifiex_init_lock_list(adapter);
5e6e3a92
BZ
85
86 init_timer(&adapter->cmd_timer);
87 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
88 adapter->cmd_timer.data = (unsigned long) adapter;
89
5e6e3a92
BZ
90 return 0;
91
92error:
93 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
94
93a1df48 95 for (i = 0; i < adapter->priv_num; i++)
5e6e3a92 96 kfree(adapter->priv[i]);
93a1df48 97
5e6e3a92
BZ
98 kfree(adapter);
99
100 return -1;
101}
102
103/*
104 * This function unregisters the device and performs all the necessary
105 * cleanups.
106 *
107 * The following cleanup operations are performed -
108 * - Free the timers
109 * - Free beacon buffers
110 * - Free private structures
111 * - Free adapter structure
112 */
113static int mwifiex_unregister(struct mwifiex_adapter *adapter)
114{
270e58e8 115 s32 i;
5e6e3a92 116
4cfda67d
AK
117 if (adapter->if_ops.cleanup_if)
118 adapter->if_ops.cleanup_if(adapter);
119
629873f2 120 del_timer_sync(&adapter->cmd_timer);
5e6e3a92
BZ
121
122 /* Free private structures */
123 for (i = 0; i < adapter->priv_num; i++) {
124 if (adapter->priv[i]) {
125 mwifiex_free_curr_bcn(adapter->priv[i]);
126 kfree(adapter->priv[i]);
127 }
128 }
129
bf354433 130 vfree(adapter->chan_stats);
5e6e3a92
BZ
131 kfree(adapter);
132 return 0;
133}
134
6e251174
AP
135static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
136{
137 unsigned long flags;
138 struct sk_buff *skb;
6e251174
AP
139
140 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
141 if (adapter->rx_processing || adapter->rx_locked) {
142 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
143 goto exit_rx_proc;
144 } else {
145 adapter->rx_processing = true;
146 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
147 }
148
149 /* Check for Rx data */
150 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
151 atomic_dec(&adapter->rx_pending);
152 if (adapter->delay_main_work &&
b43a0d9d 153 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
cf6a64fd
AK
154 if (adapter->if_ops.submit_rem_rx_urbs)
155 adapter->if_ops.submit_rem_rx_urbs(adapter);
6e251174 156 adapter->delay_main_work = false;
b43a0d9d 157 queue_work(adapter->workqueue, &adapter->main_work);
6e251174
AP
158 }
159 mwifiex_handle_rx_packet(adapter, skb);
160 }
161 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
162 adapter->rx_processing = false;
163 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
164
6e251174
AP
165exit_rx_proc:
166 return 0;
167}
168
5e6e3a92
BZ
169/*
170 * The main process.
171 *
172 * This function is the main procedure of the driver and handles various driver
173 * operations. It runs in a loop and provides the core functionalities.
174 *
175 * The main responsibilities of this function are -
176 * - Ensure concurrency control
177 * - Handle pending interrupts and call interrupt handlers
178 * - Wake up the card if required
179 * - Handle command responses and call response handlers
180 * - Handle events and call event handlers
181 * - Execute pending commands
182 * - Transmit pending data packets
183 */
184int mwifiex_main_process(struct mwifiex_adapter *adapter)
185{
186 int ret = 0;
187 unsigned long flags;
188
189 spin_lock_irqsave(&adapter->main_proc_lock, flags);
190
191 /* Check if already processing */
192 if (adapter->mwifiex_processing) {
193 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
194 goto exit_main_proc;
195 } else {
196 adapter->mwifiex_processing = true;
197 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
198 }
199process_start:
200 do {
201 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
202 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
203 break;
204
6e251174
AP
205 /* If we process interrupts first, it would increase RX pending
206 * even further. Avoid this by checking if rx_pending has
207 * crossed high threshold and schedule rx work queue
208 * and then process interrupts
209 */
210 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
211 adapter->delay_main_work = true;
212 if (!adapter->rx_processing)
213 queue_work(adapter->rx_workqueue,
214 &adapter->rx_work);
215 break;
216 }
217
5e6e3a92
BZ
218 /* Handle pending interrupt if any */
219 if (adapter->int_status) {
220 if (adapter->hs_activated)
221 mwifiex_process_hs_config(adapter);
4daffe35
AK
222 if (adapter->if_ops.process_int_status)
223 adapter->if_ops.process_int_status(adapter);
5e6e3a92
BZ
224 }
225
6e251174
AP
226 if (adapter->rx_work_enabled && adapter->data_received)
227 queue_work(adapter->rx_workqueue, &adapter->rx_work);
228
5e6e3a92
BZ
229 /* Need to wake up the card ? */
230 if ((adapter->ps_state == PS_STATE_SLEEP) &&
231 (adapter->pm_wakeup_card_req &&
232 !adapter->pm_wakeup_fw_try) &&
f57c1edc
YAP
233 (is_command_pending(adapter) ||
234 !mwifiex_wmm_lists_empty(adapter))) {
5e6e3a92
BZ
235 adapter->pm_wakeup_fw_try = true;
236 adapter->if_ops.wakeup(adapter);
237 continue;
238 }
4daffe35 239
5e6e3a92 240 if (IS_CARD_RX_RCVD(adapter)) {
6e251174 241 adapter->data_received = false;
5e6e3a92
BZ
242 adapter->pm_wakeup_fw_try = false;
243 if (adapter->ps_state == PS_STATE_SLEEP)
244 adapter->ps_state = PS_STATE_AWAKE;
245 } else {
246 /* We have tried to wakeup the card already */
247 if (adapter->pm_wakeup_fw_try)
248 break;
249 if (adapter->ps_state != PS_STATE_AWAKE ||
250 adapter->tx_lock_flag)
251 break;
252
5ec39efa 253 if ((!adapter->scan_chan_gap_enabled &&
5ec39efa 254 adapter->scan_processing) || adapter->data_sent ||
f57c1edc
YAP
255 mwifiex_wmm_lists_empty(adapter)) {
256 if (adapter->cmd_sent || adapter->curr_cmd ||
257 (!is_command_pending(adapter)))
5e6e3a92
BZ
258 break;
259 }
260 }
261
20474129
AK
262 /* Check for event */
263 if (adapter->event_received) {
264 adapter->event_received = false;
265 mwifiex_process_event(adapter);
266 }
267
5e6e3a92
BZ
268 /* Check for Cmd Resp */
269 if (adapter->cmd_resp_received) {
270 adapter->cmd_resp_received = false;
271 mwifiex_process_cmdresp(adapter);
272
273 /* call mwifiex back when init_fw is done */
274 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
275 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
276 mwifiex_init_fw_complete(adapter);
277 }
278 }
279
5e6e3a92
BZ
280 /* Check if we need to confirm Sleep Request
281 received previously */
282 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
283 if (!adapter->cmd_sent && !adapter->curr_cmd)
284 mwifiex_check_ps_cond(adapter);
285 }
286
287 /* * The ps_state may have been changed during processing of
288 * Sleep Request event.
289 */
f57c1edc
YAP
290 if ((adapter->ps_state == PS_STATE_SLEEP) ||
291 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
292 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
293 adapter->tx_lock_flag)
5e6e3a92
BZ
294 continue;
295
296 if (!adapter->cmd_sent && !adapter->curr_cmd) {
297 if (mwifiex_exec_next_cmd(adapter) == -1) {
298 ret = -1;
299 break;
300 }
301 }
302
5ec39efa 303 if ((adapter->scan_chan_gap_enabled ||
d8d91253 304 !adapter->scan_processing) &&
3249ba73 305 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
306 mwifiex_wmm_process_tx(adapter);
307 if (adapter->hs_activated) {
308 adapter->is_hs_configured = false;
309 mwifiex_hs_activated_event
310 (mwifiex_get_priv
311 (adapter, MWIFIEX_BSS_ROLE_ANY),
312 false);
313 }
314 }
315
316 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
f57c1edc
YAP
317 !adapter->curr_cmd && !is_command_pending(adapter) &&
318 mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
319 if (!mwifiex_send_null_packet
320 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
321 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
322 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
323 adapter->delay_null_pkt = false;
324 adapter->ps_state = PS_STATE_SLEEP;
325 }
326 break;
327 }
328 } while (true);
329
453b0c3f 330 spin_lock_irqsave(&adapter->main_proc_lock, flags);
f73e557b
AP
331 if (!adapter->delay_main_work &&
332 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
453b0c3f 333 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
5e6e3a92 334 goto process_start;
453b0c3f 335 }
5e6e3a92 336
5e6e3a92
BZ
337 adapter->mwifiex_processing = false;
338 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
339
340exit_main_proc:
341 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
342 mwifiex_shutdown_drv(adapter);
343 return ret;
344}
601216e1 345EXPORT_SYMBOL_GPL(mwifiex_main_process);
5e6e3a92 346
5e6e3a92
BZ
347/*
348 * This function frees the adapter structure.
349 *
350 * Additionally, this closes the netlink socket, frees the timers
351 * and private structures.
352 */
353static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
354{
355 if (!adapter) {
356 pr_err("%s: adapter is NULL\n", __func__);
357 return;
358 }
359
360 mwifiex_unregister(adapter);
361 pr_debug("info: %s: free adapter\n", __func__);
362}
363
6b41f941
AK
364/*
365 * This function cancels all works in the queue and destroys
366 * the main workqueue.
367 */
368static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
369{
370 flush_workqueue(adapter->workqueue);
371 destroy_workqueue(adapter->workqueue);
372 adapter->workqueue = NULL;
6e251174
AP
373
374 if (adapter->rx_workqueue) {
375 flush_workqueue(adapter->rx_workqueue);
376 destroy_workqueue(adapter->rx_workqueue);
377 adapter->rx_workqueue = NULL;
378 }
6b41f941
AK
379}
380
5e6e3a92 381/*
59a4cc25 382 * This function gets firmware and initializes it.
5e6e3a92
BZ
383 *
384 * The main initialization steps followed are -
385 * - Download the correct firmware to card
5e6e3a92
BZ
386 * - Issue the init commands to firmware
387 */
59a4cc25 388static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
5e6e3a92 389{
bec568ff 390 int ret;
59a4cc25
AK
391 char fmt[64];
392 struct mwifiex_private *priv;
393 struct mwifiex_adapter *adapter = context;
5e6e3a92 394 struct mwifiex_fw_image fw;
c3afd99f
AK
395 struct semaphore *sem = adapter->card_sem;
396 bool init_failed = false;
68b76e99 397 struct wireless_dev *wdev;
5e6e3a92 398
59a4cc25
AK
399 if (!firmware) {
400 dev_err(adapter->dev,
401 "Failed to get firmware %s\n", adapter->fw_name);
6b41f941 402 goto err_dnld_fw;
5e6e3a92 403 }
59a4cc25
AK
404
405 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
406 adapter->firmware = firmware;
5e6e3a92
BZ
407 fw.fw_buf = (u8 *) adapter->firmware->data;
408 fw.fw_len = adapter->firmware->size;
409
4daffe35
AK
410 if (adapter->if_ops.dnld_fw)
411 ret = adapter->if_ops.dnld_fw(adapter, &fw);
412 else
413 ret = mwifiex_dnld_fw(adapter, &fw);
5e6e3a92 414 if (ret == -1)
6b41f941 415 goto err_dnld_fw;
5e6e3a92
BZ
416
417 dev_notice(adapter->dev, "WLAN FW is active\n");
418
388ec385
AK
419 if (cal_data_cfg) {
420 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
421 adapter->dev)) < 0)
422 dev_err(adapter->dev,
423 "Cal data request_firmware() failed\n");
424 }
425
232fde06 426 /* enable host interrupt after fw dnld is successful */
6b41f941
AK
427 if (adapter->if_ops.enable_int) {
428 if (adapter->if_ops.enable_int(adapter))
429 goto err_dnld_fw;
430 }
232fde06 431
5e6e3a92
BZ
432 adapter->init_wait_q_woken = false;
433 ret = mwifiex_init_fw(adapter);
434 if (ret == -1) {
6b41f941 435 goto err_init_fw;
5e6e3a92
BZ
436 } else if (!ret) {
437 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
438 goto done;
439 }
440 /* Wait for mwifiex_init to complete */
441 wait_event_interruptible(adapter->init_wait_q,
442 adapter->init_wait_q_woken);
59a4cc25 443 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
6b41f941 444 goto err_init_fw;
59a4cc25 445
d6bffe8b
AP
446 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
447 if (mwifiex_register_cfg80211(adapter)) {
59a4cc25 448 dev_err(adapter->dev, "cannot register with cfg80211\n");
d1af2943 449 goto err_init_fw;
59a4cc25
AK
450 }
451
bf354433
AP
452 if (mwifiex_init_channel_scan_gap(adapter)) {
453 dev_err(adapter->dev, "could not init channel stats table\n");
454 goto err_init_fw;
455 }
456
0013c7ce
AP
457 if (driver_mode) {
458 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
459 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
460 }
461
59a4cc25
AK
462 rtnl_lock();
463 /* Create station interface by default */
68b76e99
AK
464 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
465 NL80211_IFTYPE_STATION, NULL, NULL);
466 if (IS_ERR(wdev)) {
59a4cc25 467 dev_err(adapter->dev, "cannot create default STA interface\n");
bec568ff 468 rtnl_unlock();
59a4cc25 469 goto err_add_intf;
5e6e3a92 470 }
0013c7ce
AP
471
472 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
473 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
474 NL80211_IFTYPE_AP, NULL, NULL);
475 if (IS_ERR(wdev)) {
476 dev_err(adapter->dev, "cannot create AP interface\n");
477 rtnl_unlock();
478 goto err_add_intf;
479 }
480 }
481
482 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
483 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
484 NL80211_IFTYPE_P2P_CLIENT, NULL,
485 NULL);
486 if (IS_ERR(wdev)) {
487 dev_err(adapter->dev,
488 "cannot create p2p client interface\n");
489 rtnl_unlock();
490 goto err_add_intf;
491 }
492 }
59a4cc25
AK
493 rtnl_unlock();
494
495 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
496 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
497 goto done;
5e6e3a92 498
59a4cc25 499err_add_intf:
6b41f941
AK
500 wiphy_unregister(adapter->wiphy);
501 wiphy_free(adapter->wiphy);
59a4cc25 502err_init_fw:
232fde06
DD
503 if (adapter->if_ops.disable_int)
504 adapter->if_ops.disable_int(adapter);
6b41f941 505err_dnld_fw:
59a4cc25 506 pr_debug("info: %s: unregister device\n", __func__);
6b41f941
AK
507 if (adapter->if_ops.unregister_dev)
508 adapter->if_ops.unregister_dev(adapter);
509
510 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
511 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
512 pr_debug("info: %s: shutdown mwifiex\n", __func__);
513 adapter->init_wait_q_woken = false;
514
515 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
516 wait_event_interruptible(adapter->init_wait_q,
517 adapter->init_wait_q_woken);
518 }
519 adapter->surprise_removed = true;
520 mwifiex_terminate_workqueue(adapter);
c3afd99f 521 init_failed = true;
5e6e3a92 522done:
388ec385
AK
523 if (adapter->cal_data) {
524 release_firmware(adapter->cal_data);
525 adapter->cal_data = NULL;
526 }
c3afd99f
AK
527 if (adapter->firmware) {
528 release_firmware(adapter->firmware);
529 adapter->firmware = NULL;
530 }
c3afd99f
AK
531 if (init_failed)
532 mwifiex_free_adapter(adapter);
533 up(sem);
59a4cc25
AK
534 return;
535}
536
537/*
538 * This function initializes the hardware and gets firmware.
539 */
540static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
541{
542 int ret;
543
59a4cc25
AK
544 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
545 adapter->dev, GFP_KERNEL, adapter,
546 mwifiex_fw_dpc);
547 if (ret < 0)
548 dev_err(adapter->dev,
549 "request_firmware_nowait() returned error %d\n", ret);
5e6e3a92
BZ
550 return ret;
551}
552
5e6e3a92
BZ
553/*
554 * CFG802.11 network device handler for open.
555 *
556 * Starts the data queue.
557 */
558static int
559mwifiex_open(struct net_device *dev)
560{
bbea3bc4 561 netif_tx_start_all_queues(dev);
5e6e3a92
BZ
562 return 0;
563}
564
565/*
566 * CFG802.11 network device handler for close.
567 */
568static int
569mwifiex_close(struct net_device *dev)
570{
f162cac8
AK
571 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
572
573 if (priv->scan_request) {
574 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
575 cfg80211_scan_done(priv->scan_request, 1);
576 priv->scan_request = NULL;
75ab753d 577 priv->scan_aborting = true;
f162cac8
AK
578 }
579
5e6e3a92
BZ
580 return 0;
581}
582
e39faa73
SP
583/*
584 * Add buffer into wmm tx queue and queue work to transmit it.
585 */
586int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
587{
47411a06
AP
588 struct netdev_queue *txq;
589 int index = mwifiex_1d_to_wmm_queue[skb->priority];
590
591 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
592 txq = netdev_get_tx_queue(priv->netdev, index);
593 if (!netif_tx_queue_stopped(txq)) {
594 netif_tx_stop_queue(txq);
595 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
596 }
597 }
598
e39faa73 599 atomic_inc(&priv->adapter->tx_pending);
47411a06 600 mwifiex_wmm_add_buf_txqueue(priv, skb);
e39faa73 601
e39faa73
SP
602 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
603
604 return 0;
605}
606
5e6e3a92
BZ
607/*
608 * CFG802.11 network device handler for data transmission.
609 */
610static int
611mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
612{
613 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
270e58e8 614 struct sk_buff *new_skb;
5e6e3a92
BZ
615 struct mwifiex_txinfo *tx_info;
616
9da9a3b2 617 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
f57c1edc 618 jiffies, priv->bss_type, priv->bss_num);
5e6e3a92
BZ
619
620 if (priv->adapter->surprise_removed) {
b53575ec 621 kfree_skb(skb);
5e6e3a92
BZ
622 priv->stats.tx_dropped++;
623 return 0;
624 }
625 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
626 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
b53575ec 627 kfree_skb(skb);
5e6e3a92
BZ
628 priv->stats.tx_dropped++;
629 return 0;
630 }
631 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
632 dev_dbg(priv->adapter->dev,
633 "data: Tx: insufficient skb headroom %d\n",
f57c1edc 634 skb_headroom(skb));
5e6e3a92
BZ
635 /* Insufficient skb headroom - allocate a new skb */
636 new_skb =
637 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
638 if (unlikely(!new_skb)) {
639 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
b53575ec 640 kfree_skb(skb);
5e6e3a92
BZ
641 priv->stats.tx_dropped++;
642 return 0;
643 }
644 kfree_skb(skb);
645 skb = new_skb;
646 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
f57c1edc 647 skb_headroom(skb));
5e6e3a92
BZ
648 }
649
650 tx_info = MWIFIEX_SKB_TXCB(skb);
d76744a9 651 memset(tx_info, 0, sizeof(*tx_info));
9da9a3b2
YAP
652 tx_info->bss_num = priv->bss_num;
653 tx_info->bss_type = priv->bss_type;
a1ed4849 654 tx_info->pkt_len = skb->len;
47411a06
AP
655
656 /* Record the current time the packet was queued; used to
657 * determine the amount of time the packet was queued in
658 * the driver before it was sent to the firmware.
659 * The delay is then sent along with the packet to the
660 * firmware for aggregate delay calculation for stats and
661 * MSDU lifetime expiry.
662 */
c64800e7 663 __net_timestamp(skb);
5e6e3a92 664
9927baa3
AP
665 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
666 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
667 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
668 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
669 mwifiex_tdls_check_tx(priv, skb);
670 }
671
e39faa73 672 mwifiex_queue_tx_pkt(priv, skb);
5e6e3a92
BZ
673
674 return 0;
675}
676
677/*
678 * CFG802.11 network device handler for setting MAC address.
679 */
680static int
681mwifiex_set_mac_address(struct net_device *dev, void *addr)
682{
683 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
a5ffddb7 684 struct sockaddr *hw_addr = addr;
270e58e8 685 int ret;
5e6e3a92
BZ
686
687 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
688
600f5d90 689 /* Send request to firmware */
fa0ecbb9
BZ
690 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
691 HostCmd_ACT_GEN_SET, 0, NULL, true);
600f5d90
AK
692
693 if (!ret)
694 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
695 else
f57c1edc
YAP
696 dev_err(priv->adapter->dev,
697 "set mac address failed: ret=%d\n", ret);
600f5d90 698
5e6e3a92
BZ
699 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
700
600f5d90 701 return ret;
5e6e3a92
BZ
702}
703
704/*
705 * CFG802.11 network device handler for setting multicast list.
706 */
707static void mwifiex_set_multicast_list(struct net_device *dev)
708{
709 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
600f5d90
AK
710 struct mwifiex_multicast_list mcast_list;
711
712 if (dev->flags & IFF_PROMISC) {
713 mcast_list.mode = MWIFIEX_PROMISC_MODE;
714 } else if (dev->flags & IFF_ALLMULTI ||
715 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
716 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
717 } else {
718 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
6390d885
DD
719 mcast_list.num_multicast_addr =
720 mwifiex_copy_mcast_addr(&mcast_list, dev);
600f5d90
AK
721 }
722 mwifiex_request_set_multicast_list(priv, &mcast_list);
5e6e3a92
BZ
723}
724
725/*
726 * CFG802.11 network device handler for transmission timeout.
727 */
728static void
729mwifiex_tx_timeout(struct net_device *dev)
730{
731 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
732
5e6e3a92 733 priv->num_tx_timeout++;
8908c7d5
AN
734 priv->tx_timeout_cnt++;
735 dev_err(priv->adapter->dev,
736 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
737 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
738 mwifiex_set_trans_start(dev);
739
740 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
741 priv->adapter->if_ops.card_reset) {
742 dev_err(priv->adapter->dev,
743 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
744 priv->adapter->if_ops.card_reset(priv->adapter);
745 }
5e6e3a92
BZ
746}
747
748/*
749 * CFG802.11 network device handler for statistics retrieval.
750 */
751static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
752{
753 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
754
755 return &priv->stats;
756}
757
47411a06 758static u16
f663dd9a 759mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
99932d4f 760 void *accel_priv, select_queue_fallback_t fallback)
47411a06 761{
fa9ffc74 762 skb->priority = cfg80211_classify8021d(skb, NULL);
47411a06
AP
763 return mwifiex_1d_to_wmm_queue[skb->priority];
764}
765
5e6e3a92
BZ
766/* Network device handlers */
767static const struct net_device_ops mwifiex_netdev_ops = {
768 .ndo_open = mwifiex_open,
769 .ndo_stop = mwifiex_close,
770 .ndo_start_xmit = mwifiex_hard_start_xmit,
771 .ndo_set_mac_address = mwifiex_set_mac_address,
772 .ndo_tx_timeout = mwifiex_tx_timeout,
773 .ndo_get_stats = mwifiex_get_stats,
afc4b13d 774 .ndo_set_rx_mode = mwifiex_set_multicast_list,
47411a06 775 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
5e6e3a92
BZ
776};
777
778/*
779 * This function initializes the private structure parameters.
780 *
781 * The following wait queues are initialized -
782 * - IOCTL wait queue
783 * - Command wait queue
784 * - Statistics wait queue
785 *
786 * ...and the following default parameters are set -
787 * - Current key index : Set to 0
788 * - Rate index : Set to auto
789 * - Media connected : Set to disconnected
790 * - Adhoc link sensed : Set to false
791 * - Nick name : Set to null
792 * - Number of Tx timeout : Set to 0
793 * - Device address : Set to current address
794 *
795 * In addition, the CFG80211 work queue is also created.
796 */
93a1df48
YAP
797void mwifiex_init_priv_params(struct mwifiex_private *priv,
798 struct net_device *dev)
5e6e3a92
BZ
799{
800 dev->netdev_ops = &mwifiex_netdev_ops;
f16fdc9d 801 dev->destructor = free_netdev;
5e6e3a92 802 /* Initialize private structure */
5e6e3a92
BZ
803 priv->current_key_index = 0;
804 priv->media_connected = false;
805 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
ede98bfa
AP
806 memset(priv->mgmt_ie, 0,
807 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
f31acabe
AP
808 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
809 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
810 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
811 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
5e6e3a92 812 priv->num_tx_timeout = 0;
5e6e3a92
BZ
813 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
814}
815
5e6e3a92
BZ
816/*
817 * This function check if command is pending.
818 */
819int is_command_pending(struct mwifiex_adapter *adapter)
820{
821 unsigned long flags;
822 int is_cmd_pend_q_empty;
823
824 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
825 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
826 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
827
828 return !is_cmd_pend_q_empty;
829}
830
6e251174
AP
831/*
832 * This is the RX work queue function.
833 *
834 * It handles the RX operations.
835 */
836static void mwifiex_rx_work_queue(struct work_struct *work)
837{
838 struct mwifiex_adapter *adapter =
839 container_of(work, struct mwifiex_adapter, rx_work);
840
841 if (adapter->surprise_removed)
842 return;
843 mwifiex_process_rx(adapter);
844}
845
5e6e3a92
BZ
846/*
847 * This is the main work queue function.
848 *
849 * It handles the main process, which in turn handles the complete
850 * driver operations.
851 */
852static void mwifiex_main_work_queue(struct work_struct *work)
853{
854 struct mwifiex_adapter *adapter =
855 container_of(work, struct mwifiex_adapter, main_work);
856
857 if (adapter->surprise_removed)
858 return;
859 mwifiex_main_process(adapter);
860}
861
5e6e3a92
BZ
862/*
863 * This function adds the card.
864 *
865 * This function follows the following major steps to set up the device -
866 * - Initialize software. This includes probing the card, registering
867 * the interface operations table, and allocating/initializing the
868 * adapter structure
869 * - Set up the netlink socket
870 * - Create and start the main work queue
871 * - Register the device
872 * - Initialize firmware and hardware
873 * - Add logical interfaces
874 */
875int
876mwifiex_add_card(void *card, struct semaphore *sem,
d930faee 877 struct mwifiex_if_ops *if_ops, u8 iface_type)
5e6e3a92 878{
2be7859f 879 struct mwifiex_adapter *adapter;
5e6e3a92
BZ
880
881 if (down_interruptible(sem))
882 goto exit_sem_err;
883
93a1df48 884 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
5e6e3a92
BZ
885 pr_err("%s: software init failed\n", __func__);
886 goto err_init_sw;
887 }
888
d930faee 889 adapter->iface_type = iface_type;
6b41f941 890 adapter->card_sem = sem;
d930faee 891
5e6e3a92 892 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
5e6e3a92
BZ
893 adapter->surprise_removed = false;
894 init_waitqueue_head(&adapter->init_wait_q);
895 adapter->is_suspended = false;
896 adapter->hs_activated = false;
897 init_waitqueue_head(&adapter->hs_activate_wait_q);
600f5d90 898 init_waitqueue_head(&adapter->cmd_wait_q.wait);
600f5d90 899 adapter->cmd_wait_q.status = 0;
efaaa8b8 900 adapter->scan_wait_q_woken = false;
5e6e3a92 901
ec4a16b4 902 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
6e251174
AP
903 adapter->rx_work_enabled = true;
904 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
905 }
906
448a71cc
AK
907 adapter->workqueue =
908 alloc_workqueue("MWIFIEX_WORK_QUEUE",
909 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
5e6e3a92
BZ
910 if (!adapter->workqueue)
911 goto err_kmalloc;
912
913 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
6e251174
AP
914
915 if (adapter->rx_work_enabled) {
916 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
917 WQ_HIGHPRI |
918 WQ_MEM_RECLAIM |
919 WQ_UNBOUND, 1);
920 if (!adapter->rx_workqueue)
921 goto err_kmalloc;
922
923 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
924 }
925
92c2538f
AK
926 if (adapter->if_ops.iface_work)
927 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
5e6e3a92
BZ
928
929 /* Register the device. Fill up the private data structure with relevant
232fde06 930 information from the card. */
5e6e3a92
BZ
931 if (adapter->if_ops.register_dev(adapter)) {
932 pr_err("%s: failed to register mwifiex device\n", __func__);
933 goto err_registerdev;
934 }
935
5e6e3a92
BZ
936 if (mwifiex_init_hw_fw(adapter)) {
937 pr_err("%s: firmware init failed\n", __func__);
938 goto err_init_fw;
939 }
2be7859f 940
5e6e3a92
BZ
941 return 0;
942
5e6e3a92 943err_init_fw:
5e6e3a92 944 pr_debug("info: %s: unregister device\n", __func__);
4daffe35
AK
945 if (adapter->if_ops.unregister_dev)
946 adapter->if_ops.unregister_dev(adapter);
5e6e3a92
BZ
947 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
948 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
949 pr_debug("info: %s: shutdown mwifiex\n", __func__);
950 adapter->init_wait_q_woken = false;
636c4598
YAP
951
952 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
953 wait_event_interruptible(adapter->init_wait_q,
954 adapter->init_wait_q_woken);
955 }
6b41f941
AK
956err_registerdev:
957 adapter->surprise_removed = true;
958 mwifiex_terminate_workqueue(adapter);
959err_kmalloc:
5e6e3a92
BZ
960 mwifiex_free_adapter(adapter);
961
962err_init_sw:
963 up(sem);
964
965exit_sem_err:
966 return -1;
967}
968EXPORT_SYMBOL_GPL(mwifiex_add_card);
969
970/*
971 * This function removes the card.
972 *
973 * This function follows the following major steps to remove the device -
974 * - Stop data traffic
975 * - Shutdown firmware
976 * - Remove the logical interfaces
977 * - Terminate the work queue
978 * - Unregister the device
979 * - Free the adapter structure
980 */
981int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
982{
983 struct mwifiex_private *priv = NULL;
5e6e3a92
BZ
984 int i;
985
986 if (down_interruptible(sem))
987 goto exit_sem_err;
988
989 if (!adapter)
990 goto exit_remove;
991
232fde06
DD
992 /* We can no longer handle interrupts once we start doing the teardown
993 * below. */
994 if (adapter->if_ops.disable_int)
995 adapter->if_ops.disable_int(adapter);
996
5e6e3a92
BZ
997 adapter->surprise_removed = true;
998
999 /* Stop data */
1000 for (i = 0; i < adapter->priv_num; i++) {
1001 priv = adapter->priv[i];
93a1df48 1002 if (priv && priv->netdev) {
47411a06 1003 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
5e6e3a92
BZ
1004 if (netif_carrier_ok(priv->netdev))
1005 netif_carrier_off(priv->netdev);
1006 }
1007 }
1008
1009 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1010 adapter->init_wait_q_woken = false;
636c4598
YAP
1011
1012 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
1013 wait_event_interruptible(adapter->init_wait_q,
1014 adapter->init_wait_q_woken);
1015 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1016 if (atomic_read(&adapter->rx_pending) ||
1017 atomic_read(&adapter->tx_pending) ||
600f5d90 1018 atomic_read(&adapter->cmd_pending)) {
5e6e3a92 1019 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
600f5d90 1020 "cmd_pending=%d\n",
5e6e3a92
BZ
1021 atomic_read(&adapter->rx_pending),
1022 atomic_read(&adapter->tx_pending),
600f5d90 1023 atomic_read(&adapter->cmd_pending));
5e6e3a92
BZ
1024 }
1025
93a1df48
YAP
1026 for (i = 0; i < adapter->priv_num; i++) {
1027 priv = adapter->priv[i];
1028
1029 if (!priv)
1030 continue;
1031
1032 rtnl_lock();
2da8cbf8 1033 if (priv->wdev && priv->netdev)
84efbb84 1034 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
93a1df48
YAP
1035 rtnl_unlock();
1036 }
1037
c2868ade
AK
1038 wiphy_unregister(adapter->wiphy);
1039 wiphy_free(adapter->wiphy);
64b05e2f 1040
5e6e3a92
BZ
1041 mwifiex_terminate_workqueue(adapter);
1042
1043 /* Unregister device */
1044 dev_dbg(adapter->dev, "info: unregister device\n");
4daffe35
AK
1045 if (adapter->if_ops.unregister_dev)
1046 adapter->if_ops.unregister_dev(adapter);
5e6e3a92
BZ
1047 /* Free adapter structure */
1048 dev_dbg(adapter->dev, "info: free adapter\n");
1049 mwifiex_free_adapter(adapter);
1050
1051exit_remove:
1052 up(sem);
1053exit_sem_err:
1054 return 0;
1055}
1056EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1057
1058/*
1059 * This function initializes the module.
1060 *
1061 * The debug FS is also initialized if configured.
1062 */
1063static int
1064mwifiex_init_module(void)
1065{
1066#ifdef CONFIG_DEBUG_FS
1067 mwifiex_debugfs_init();
1068#endif
1069 return 0;
1070}
1071
1072/*
1073 * This function cleans up the module.
1074 *
1075 * The debug FS is removed if available.
1076 */
1077static void
1078mwifiex_cleanup_module(void)
1079{
1080#ifdef CONFIG_DEBUG_FS
1081 mwifiex_debugfs_remove();
1082#endif
1083}
1084
1085module_init(mwifiex_init_module);
1086module_exit(mwifiex_cleanup_module);
1087
1088MODULE_AUTHOR("Marvell International Ltd.");
1089MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1090MODULE_VERSION(VERSION);
1091MODULE_LICENSE("GPL v2");
This page took 0.377864 seconds and 5 git commands to generate.