Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[deliverable/linux.git] / drivers / net / wireless / mwifiex / main.c
1 /*
2 * Marvell Wireless LAN device driver: major functions
3 *
4 * Copyright (C) 2011-2014, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20 #include "main.h"
21 #include "wmm.h"
22 #include "cfg80211.h"
23 #include "11n.h"
24
25 #define VERSION "1.0"
26
27 const char driver_version[] = "mwifiex " VERSION " (%s) ";
28 static char *cal_data_cfg;
29 module_param(cal_data_cfg, charp, 0);
30
31 static unsigned short driver_mode;
32 module_param(driver_mode, ushort, 0);
33 MODULE_PARM_DESC(driver_mode,
34 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
35
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 */
51 static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
52 void **padapter)
53 {
54 struct mwifiex_adapter *adapter;
55 int i;
56
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
58 if (!adapter)
59 return -ENOMEM;
60
61 *padapter = adapter;
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 .. */
68 if (adapter->if_ops.init_if)
69 if (adapter->if_ops.init_if(adapter))
70 goto error;
71
72 adapter->priv_num = 0;
73
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;
80
81 adapter->priv[i]->adapter = adapter;
82 adapter->priv_num++;
83 }
84 mwifiex_init_lock_list(adapter);
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
90 return 0;
91
92 error:
93 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
94
95 for (i = 0; i < adapter->priv_num; i++)
96 kfree(adapter->priv[i]);
97
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 */
113 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
114 {
115 s32 i;
116
117 if (adapter->if_ops.cleanup_if)
118 adapter->if_ops.cleanup_if(adapter);
119
120 del_timer_sync(&adapter->cmd_timer);
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
130 vfree(adapter->chan_stats);
131 kfree(adapter);
132 return 0;
133 }
134
135 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
136 {
137 unsigned long flags;
138 struct sk_buff *skb;
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 &&
153 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
154 if (adapter->if_ops.submit_rem_rx_urbs)
155 adapter->if_ops.submit_rem_rx_urbs(adapter);
156 adapter->delay_main_work = false;
157 queue_work(adapter->workqueue, &adapter->main_work);
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
165 exit_rx_proc:
166 return 0;
167 }
168
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 */
184 int 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 }
199 process_start:
200 do {
201 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
202 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
203 break;
204
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
218 /* Handle pending interrupt if any */
219 if (adapter->int_status) {
220 if (adapter->hs_activated)
221 mwifiex_process_hs_config(adapter);
222 if (adapter->if_ops.process_int_status)
223 adapter->if_ops.process_int_status(adapter);
224 }
225
226 if (adapter->rx_work_enabled && adapter->data_received)
227 queue_work(adapter->rx_workqueue, &adapter->rx_work);
228
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) &&
233 (is_command_pending(adapter) ||
234 !mwifiex_wmm_lists_empty(adapter))) {
235 adapter->pm_wakeup_fw_try = true;
236 adapter->if_ops.wakeup(adapter);
237 continue;
238 }
239
240 if (IS_CARD_RX_RCVD(adapter)) {
241 adapter->data_received = false;
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
253 if ((!adapter->scan_chan_gap_enabled &&
254 adapter->scan_processing) || adapter->data_sent ||
255 mwifiex_wmm_lists_empty(adapter)) {
256 if (adapter->cmd_sent || adapter->curr_cmd ||
257 (!is_command_pending(adapter)))
258 break;
259 }
260 }
261
262 /* Check for event */
263 if (adapter->event_received) {
264 adapter->event_received = false;
265 mwifiex_process_event(adapter);
266 }
267
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
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 */
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)
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
303 if ((adapter->scan_chan_gap_enabled ||
304 !adapter->scan_processing) &&
305 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
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 &&
317 !adapter->curr_cmd && !is_command_pending(adapter) &&
318 mwifiex_wmm_lists_empty(adapter)) {
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
330 spin_lock_irqsave(&adapter->main_proc_lock, flags);
331 if (!adapter->delay_main_work &&
332 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
333 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
334 goto process_start;
335 }
336
337 adapter->mwifiex_processing = false;
338 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
339
340 exit_main_proc:
341 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
342 mwifiex_shutdown_drv(adapter);
343 return ret;
344 }
345 EXPORT_SYMBOL_GPL(mwifiex_main_process);
346
347 /*
348 * This function frees the adapter structure.
349 *
350 * Additionally, this closes the netlink socket, frees the timers
351 * and private structures.
352 */
353 static 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
364 /*
365 * This function cancels all works in the queue and destroys
366 * the main workqueue.
367 */
368 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
369 {
370 flush_workqueue(adapter->workqueue);
371 destroy_workqueue(adapter->workqueue);
372 adapter->workqueue = NULL;
373
374 if (adapter->rx_workqueue) {
375 flush_workqueue(adapter->rx_workqueue);
376 destroy_workqueue(adapter->rx_workqueue);
377 adapter->rx_workqueue = NULL;
378 }
379 }
380
381 /*
382 * This function gets firmware and initializes it.
383 *
384 * The main initialization steps followed are -
385 * - Download the correct firmware to card
386 * - Issue the init commands to firmware
387 */
388 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
389 {
390 int ret;
391 char fmt[64];
392 struct mwifiex_private *priv;
393 struct mwifiex_adapter *adapter = context;
394 struct mwifiex_fw_image fw;
395 struct semaphore *sem = adapter->card_sem;
396 bool init_failed = false;
397 struct wireless_dev *wdev;
398
399 if (!firmware) {
400 dev_err(adapter->dev,
401 "Failed to get firmware %s\n", adapter->fw_name);
402 goto err_dnld_fw;
403 }
404
405 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
406 adapter->firmware = firmware;
407 fw.fw_buf = (u8 *) adapter->firmware->data;
408 fw.fw_len = adapter->firmware->size;
409
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);
414 if (ret == -1)
415 goto err_dnld_fw;
416
417 dev_notice(adapter->dev, "WLAN FW is active\n");
418
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
426 /* enable host interrupt after fw dnld is successful */
427 if (adapter->if_ops.enable_int) {
428 if (adapter->if_ops.enable_int(adapter))
429 goto err_dnld_fw;
430 }
431
432 adapter->init_wait_q_woken = false;
433 ret = mwifiex_init_fw(adapter);
434 if (ret == -1) {
435 goto err_init_fw;
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);
443 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
444 goto err_init_fw;
445
446 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
447 if (mwifiex_register_cfg80211(adapter)) {
448 dev_err(adapter->dev, "cannot register with cfg80211\n");
449 goto err_init_fw;
450 }
451
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
457 if (driver_mode) {
458 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
459 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
460 }
461
462 rtnl_lock();
463 /* Create station interface by default */
464 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
465 NL80211_IFTYPE_STATION, NULL, NULL);
466 if (IS_ERR(wdev)) {
467 dev_err(adapter->dev, "cannot create default STA interface\n");
468 rtnl_unlock();
469 goto err_add_intf;
470 }
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 }
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;
498
499 err_add_intf:
500 wiphy_unregister(adapter->wiphy);
501 wiphy_free(adapter->wiphy);
502 err_init_fw:
503 if (adapter->if_ops.disable_int)
504 adapter->if_ops.disable_int(adapter);
505 err_dnld_fw:
506 pr_debug("info: %s: unregister device\n", __func__);
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);
521 init_failed = true;
522 done:
523 if (adapter->cal_data) {
524 release_firmware(adapter->cal_data);
525 adapter->cal_data = NULL;
526 }
527 if (adapter->firmware) {
528 release_firmware(adapter->firmware);
529 adapter->firmware = NULL;
530 }
531 if (init_failed)
532 mwifiex_free_adapter(adapter);
533 up(sem);
534 return;
535 }
536
537 /*
538 * This function initializes the hardware and gets firmware.
539 */
540 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
541 {
542 int ret;
543
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);
550 return ret;
551 }
552
553 /*
554 * CFG802.11 network device handler for open.
555 *
556 * Starts the data queue.
557 */
558 static int
559 mwifiex_open(struct net_device *dev)
560 {
561 netif_tx_start_all_queues(dev);
562 return 0;
563 }
564
565 /*
566 * CFG802.11 network device handler for close.
567 */
568 static int
569 mwifiex_close(struct net_device *dev)
570 {
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;
577 priv->scan_aborting = true;
578 }
579
580 return 0;
581 }
582
583 /*
584 * Add buffer into wmm tx queue and queue work to transmit it.
585 */
586 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
587 {
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
599 atomic_inc(&priv->adapter->tx_pending);
600 mwifiex_wmm_add_buf_txqueue(priv, skb);
601
602 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
603
604 return 0;
605 }
606
607 /*
608 * CFG802.11 network device handler for data transmission.
609 */
610 static int
611 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
612 {
613 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
614 struct sk_buff *new_skb;
615 struct mwifiex_txinfo *tx_info;
616
617 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
618 jiffies, priv->bss_type, priv->bss_num);
619
620 if (priv->adapter->surprise_removed) {
621 kfree_skb(skb);
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);
627 kfree_skb(skb);
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",
634 skb_headroom(skb));
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");
640 kfree_skb(skb);
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",
647 skb_headroom(skb));
648 }
649
650 tx_info = MWIFIEX_SKB_TXCB(skb);
651 memset(tx_info, 0, sizeof(*tx_info));
652 tx_info->bss_num = priv->bss_num;
653 tx_info->bss_type = priv->bss_type;
654 tx_info->pkt_len = skb->len;
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 */
663 __net_timestamp(skb);
664
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
672 mwifiex_queue_tx_pkt(priv, skb);
673
674 return 0;
675 }
676
677 /*
678 * CFG802.11 network device handler for setting MAC address.
679 */
680 static int
681 mwifiex_set_mac_address(struct net_device *dev, void *addr)
682 {
683 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
684 struct sockaddr *hw_addr = addr;
685 int ret;
686
687 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
688
689 /* Send request to firmware */
690 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
691 HostCmd_ACT_GEN_SET, 0, NULL, true);
692
693 if (!ret)
694 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
695 else
696 dev_err(priv->adapter->dev,
697 "set mac address failed: ret=%d\n", ret);
698
699 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
700
701 return ret;
702 }
703
704 /*
705 * CFG802.11 network device handler for setting multicast list.
706 */
707 static void mwifiex_set_multicast_list(struct net_device *dev)
708 {
709 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
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;
719 mcast_list.num_multicast_addr =
720 mwifiex_copy_mcast_addr(&mcast_list, dev);
721 }
722 mwifiex_request_set_multicast_list(priv, &mcast_list);
723 }
724
725 /*
726 * CFG802.11 network device handler for transmission timeout.
727 */
728 static void
729 mwifiex_tx_timeout(struct net_device *dev)
730 {
731 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
732
733 priv->num_tx_timeout++;
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 }
746 }
747
748 /*
749 * CFG802.11 network device handler for statistics retrieval.
750 */
751 static 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
758 static u16
759 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
760 void *accel_priv, select_queue_fallback_t fallback)
761 {
762 skb->priority = cfg80211_classify8021d(skb, NULL);
763 return mwifiex_1d_to_wmm_queue[skb->priority];
764 }
765
766 /* Network device handlers */
767 static 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,
774 .ndo_set_rx_mode = mwifiex_set_multicast_list,
775 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
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 */
797 void mwifiex_init_priv_params(struct mwifiex_private *priv,
798 struct net_device *dev)
799 {
800 dev->netdev_ops = &mwifiex_netdev_ops;
801 dev->destructor = free_netdev;
802 /* Initialize private structure */
803 priv->current_key_index = 0;
804 priv->media_connected = false;
805 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
806 memset(priv->mgmt_ie, 0,
807 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
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;
812 priv->num_tx_timeout = 0;
813 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
814 }
815
816 /*
817 * This function check if command is pending.
818 */
819 int 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
831 /*
832 * This is the RX work queue function.
833 *
834 * It handles the RX operations.
835 */
836 static 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
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 */
852 static 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
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 */
875 int
876 mwifiex_add_card(void *card, struct semaphore *sem,
877 struct mwifiex_if_ops *if_ops, u8 iface_type)
878 {
879 struct mwifiex_adapter *adapter;
880
881 if (down_interruptible(sem))
882 goto exit_sem_err;
883
884 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
885 pr_err("%s: software init failed\n", __func__);
886 goto err_init_sw;
887 }
888
889 adapter->iface_type = iface_type;
890 adapter->card_sem = sem;
891
892 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
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);
898 init_waitqueue_head(&adapter->cmd_wait_q.wait);
899 adapter->cmd_wait_q.status = 0;
900 adapter->scan_wait_q_woken = false;
901
902 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
903 adapter->rx_work_enabled = true;
904 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
905 }
906
907 adapter->workqueue =
908 alloc_workqueue("MWIFIEX_WORK_QUEUE",
909 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
910 if (!adapter->workqueue)
911 goto err_kmalloc;
912
913 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
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
926 if (adapter->if_ops.iface_work)
927 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
928
929 /* Register the device. Fill up the private data structure with relevant
930 information from the card. */
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
936 if (mwifiex_init_hw_fw(adapter)) {
937 pr_err("%s: firmware init failed\n", __func__);
938 goto err_init_fw;
939 }
940
941 return 0;
942
943 err_init_fw:
944 pr_debug("info: %s: unregister device\n", __func__);
945 if (adapter->if_ops.unregister_dev)
946 adapter->if_ops.unregister_dev(adapter);
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;
951
952 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
953 wait_event_interruptible(adapter->init_wait_q,
954 adapter->init_wait_q_woken);
955 }
956 err_registerdev:
957 adapter->surprise_removed = true;
958 mwifiex_terminate_workqueue(adapter);
959 err_kmalloc:
960 mwifiex_free_adapter(adapter);
961
962 err_init_sw:
963 up(sem);
964
965 exit_sem_err:
966 return -1;
967 }
968 EXPORT_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 */
981 int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
982 {
983 struct mwifiex_private *priv = NULL;
984 int i;
985
986 if (down_interruptible(sem))
987 goto exit_sem_err;
988
989 if (!adapter)
990 goto exit_remove;
991
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
997 adapter->surprise_removed = true;
998
999 /* Stop data */
1000 for (i = 0; i < adapter->priv_num; i++) {
1001 priv = adapter->priv[i];
1002 if (priv && priv->netdev) {
1003 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
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;
1011
1012 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
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) ||
1018 atomic_read(&adapter->cmd_pending)) {
1019 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
1020 "cmd_pending=%d\n",
1021 atomic_read(&adapter->rx_pending),
1022 atomic_read(&adapter->tx_pending),
1023 atomic_read(&adapter->cmd_pending));
1024 }
1025
1026 for (i = 0; i < adapter->priv_num; i++) {
1027 priv = adapter->priv[i];
1028
1029 if (!priv)
1030 continue;
1031
1032 rtnl_lock();
1033 if (priv->wdev && priv->netdev)
1034 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
1035 rtnl_unlock();
1036 }
1037
1038 wiphy_unregister(adapter->wiphy);
1039 wiphy_free(adapter->wiphy);
1040
1041 mwifiex_terminate_workqueue(adapter);
1042
1043 /* Unregister device */
1044 dev_dbg(adapter->dev, "info: unregister device\n");
1045 if (adapter->if_ops.unregister_dev)
1046 adapter->if_ops.unregister_dev(adapter);
1047 /* Free adapter structure */
1048 dev_dbg(adapter->dev, "info: free adapter\n");
1049 mwifiex_free_adapter(adapter);
1050
1051 exit_remove:
1052 up(sem);
1053 exit_sem_err:
1054 return 0;
1055 }
1056 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1057
1058 /*
1059 * This function initializes the module.
1060 *
1061 * The debug FS is also initialized if configured.
1062 */
1063 static int
1064 mwifiex_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 */
1077 static void
1078 mwifiex_cleanup_module(void)
1079 {
1080 #ifdef CONFIG_DEBUG_FS
1081 mwifiex_debugfs_remove();
1082 #endif
1083 }
1084
1085 module_init(mwifiex_init_module);
1086 module_exit(mwifiex_cleanup_module);
1087
1088 MODULE_AUTHOR("Marvell International Ltd.");
1089 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1090 MODULE_VERSION(VERSION);
1091 MODULE_LICENSE("GPL v2");
This page took 0.082262 seconds and 6 git commands to generate.