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