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