mwifiex: remove redundant flag MWIFIEX_HW_STATUS_FW_READY
[linux-2.6/btrfs-unstable.git] / drivers / net / wireless / mwifiex / main.c
blob2568395f02760e92da0d963449620f8422c30a21
1 /*
2 * Marvell Wireless LAN device driver: major functions
4 * Copyright (C) 2011-2014, Marvell International Ltd.
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.
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.
20 #include "main.h"
21 #include "wmm.h"
22 #include "cfg80211.h"
23 #include "11n.h"
25 #define VERSION "1.0"
27 const char driver_version[] = "mwifiex " VERSION " (%s) ";
28 static char *cal_data_cfg;
29 module_param(cal_data_cfg, charp, 0);
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");
37 * This function registers the device and performs all the necessary
38 * initializations.
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
48 * In case of any errors during inittialization, this function also ensures
49 * proper cleanup before exiting.
51 static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
52 void **padapter)
54 struct mwifiex_adapter *adapter;
55 int i;
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
58 if (!adapter)
59 return -ENOMEM;
61 *padapter = adapter;
62 adapter->card = card;
64 /* Save interface specific operations in adapter */
65 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
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;
72 adapter->priv_num = 0;
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;
81 adapter->priv[i]->adapter = adapter;
82 adapter->priv_num++;
84 mwifiex_init_lock_list(adapter);
86 init_timer(&adapter->cmd_timer);
87 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
88 adapter->cmd_timer.data = (unsigned long) adapter;
90 return 0;
92 error:
93 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
95 for (i = 0; i < adapter->priv_num; i++)
96 kfree(adapter->priv[i]);
98 kfree(adapter);
100 return -1;
104 * This function unregisters the device and performs all the necessary
105 * cleanups.
107 * The following cleanup operations are performed -
108 * - Free the timers
109 * - Free beacon buffers
110 * - Free private structures
111 * - Free adapter structure
113 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
115 s32 i;
117 if (adapter->if_ops.cleanup_if)
118 adapter->if_ops.cleanup_if(adapter);
120 del_timer_sync(&adapter->cmd_timer);
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]);
130 vfree(adapter->chan_stats);
131 kfree(adapter);
132 return 0;
135 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
137 unsigned long flags;
138 struct sk_buff *skb;
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);
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 adapter->iface_type == MWIFIEX_USB) &&
154 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
155 if (adapter->if_ops.submit_rem_rx_urbs)
156 adapter->if_ops.submit_rem_rx_urbs(adapter);
157 adapter->delay_main_work = false;
158 queue_work(adapter->workqueue, &adapter->main_work);
160 mwifiex_handle_rx_packet(adapter, skb);
162 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
163 adapter->rx_processing = false;
164 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
166 exit_rx_proc:
167 return 0;
171 * The main process.
173 * This function is the main procedure of the driver and handles various driver
174 * operations. It runs in a loop and provides the core functionalities.
176 * The main responsibilities of this function are -
177 * - Ensure concurrency control
178 * - Handle pending interrupts and call interrupt handlers
179 * - Wake up the card if required
180 * - Handle command responses and call response handlers
181 * - Handle events and call event handlers
182 * - Execute pending commands
183 * - Transmit pending data packets
185 int mwifiex_main_process(struct mwifiex_adapter *adapter)
187 int ret = 0;
188 unsigned long flags;
190 spin_lock_irqsave(&adapter->main_proc_lock, flags);
192 /* Check if already processing */
193 if (adapter->mwifiex_processing) {
194 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
195 goto exit_main_proc;
196 } else {
197 adapter->mwifiex_processing = true;
198 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
200 process_start:
201 do {
202 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
203 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
204 break;
206 /* For non-USB interfaces, If we process interrupts first, it
207 * would increase RX pending even further. Avoid this by
208 * checking if rx_pending has crossed high threshold and
209 * schedule rx work queue and then process interrupts.
210 * For USB interface, there are no interrupts. We already have
211 * HIGH_RX_PENDING check in usb.c
213 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
214 adapter->iface_type != MWIFIEX_USB) {
215 adapter->delay_main_work = true;
216 if (!adapter->rx_processing)
217 queue_work(adapter->rx_workqueue,
218 &adapter->rx_work);
219 break;
222 /* Handle pending interrupt if any */
223 if (adapter->int_status) {
224 if (adapter->hs_activated)
225 mwifiex_process_hs_config(adapter);
226 if (adapter->if_ops.process_int_status)
227 adapter->if_ops.process_int_status(adapter);
230 if (adapter->rx_work_enabled && adapter->data_received)
231 queue_work(adapter->rx_workqueue, &adapter->rx_work);
233 /* Need to wake up the card ? */
234 if ((adapter->ps_state == PS_STATE_SLEEP) &&
235 (adapter->pm_wakeup_card_req &&
236 !adapter->pm_wakeup_fw_try) &&
237 (is_command_pending(adapter) ||
238 !mwifiex_wmm_lists_empty(adapter))) {
239 adapter->pm_wakeup_fw_try = true;
240 adapter->if_ops.wakeup(adapter);
241 continue;
244 if (IS_CARD_RX_RCVD(adapter)) {
245 adapter->data_received = false;
246 adapter->pm_wakeup_fw_try = false;
247 if (adapter->ps_state == PS_STATE_SLEEP)
248 adapter->ps_state = PS_STATE_AWAKE;
249 } else {
250 /* We have tried to wakeup the card already */
251 if (adapter->pm_wakeup_fw_try)
252 break;
253 if (adapter->ps_state != PS_STATE_AWAKE ||
254 adapter->tx_lock_flag)
255 break;
257 if ((!adapter->scan_chan_gap_enabled &&
258 adapter->scan_processing) || adapter->data_sent ||
259 mwifiex_wmm_lists_empty(adapter)) {
260 if (adapter->cmd_sent || adapter->curr_cmd ||
261 (!is_command_pending(adapter)))
262 break;
266 /* Check for event */
267 if (adapter->event_received) {
268 adapter->event_received = false;
269 mwifiex_process_event(adapter);
272 /* Check for Cmd Resp */
273 if (adapter->cmd_resp_received) {
274 adapter->cmd_resp_received = false;
275 mwifiex_process_cmdresp(adapter);
277 /* call mwifiex back when init_fw is done */
278 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
279 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
280 mwifiex_init_fw_complete(adapter);
284 /* Check if we need to confirm Sleep Request
285 received previously */
286 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
287 if (!adapter->cmd_sent && !adapter->curr_cmd)
288 mwifiex_check_ps_cond(adapter);
291 /* * The ps_state may have been changed during processing of
292 * Sleep Request event.
294 if ((adapter->ps_state == PS_STATE_SLEEP) ||
295 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
296 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
297 adapter->tx_lock_flag)
298 continue;
300 if (!adapter->cmd_sent && !adapter->curr_cmd) {
301 if (mwifiex_exec_next_cmd(adapter) == -1) {
302 ret = -1;
303 break;
307 if ((adapter->scan_chan_gap_enabled ||
308 !adapter->scan_processing) &&
309 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
310 mwifiex_wmm_process_tx(adapter);
311 if (adapter->hs_activated) {
312 adapter->is_hs_configured = false;
313 mwifiex_hs_activated_event
314 (mwifiex_get_priv
315 (adapter, MWIFIEX_BSS_ROLE_ANY),
316 false);
320 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
321 !adapter->curr_cmd && !is_command_pending(adapter) &&
322 mwifiex_wmm_lists_empty(adapter)) {
323 if (!mwifiex_send_null_packet
324 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
325 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
326 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
327 adapter->delay_null_pkt = false;
328 adapter->ps_state = PS_STATE_SLEEP;
330 break;
332 } while (true);
334 spin_lock_irqsave(&adapter->main_proc_lock, flags);
335 if (!adapter->delay_main_work &&
336 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
337 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
338 goto process_start;
341 adapter->mwifiex_processing = false;
342 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
344 exit_main_proc:
345 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
346 mwifiex_shutdown_drv(adapter);
347 return ret;
349 EXPORT_SYMBOL_GPL(mwifiex_main_process);
352 * This function frees the adapter structure.
354 * Additionally, this closes the netlink socket, frees the timers
355 * and private structures.
357 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
359 if (!adapter) {
360 pr_err("%s: adapter is NULL\n", __func__);
361 return;
364 mwifiex_unregister(adapter);
365 pr_debug("info: %s: free adapter\n", __func__);
369 * This function cancels all works in the queue and destroys
370 * the main workqueue.
372 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
374 flush_workqueue(adapter->workqueue);
375 destroy_workqueue(adapter->workqueue);
376 adapter->workqueue = NULL;
378 if (adapter->rx_workqueue) {
379 flush_workqueue(adapter->rx_workqueue);
380 destroy_workqueue(adapter->rx_workqueue);
381 adapter->rx_workqueue = NULL;
386 * This function gets firmware and initializes it.
388 * The main initialization steps followed are -
389 * - Download the correct firmware to card
390 * - Issue the init commands to firmware
392 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
394 int ret;
395 char fmt[64];
396 struct mwifiex_private *priv;
397 struct mwifiex_adapter *adapter = context;
398 struct mwifiex_fw_image fw;
399 struct semaphore *sem = adapter->card_sem;
400 bool init_failed = false;
401 struct wireless_dev *wdev;
403 if (!firmware) {
404 dev_err(adapter->dev,
405 "Failed to get firmware %s\n", adapter->fw_name);
406 goto err_dnld_fw;
409 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
410 adapter->firmware = firmware;
411 fw.fw_buf = (u8 *) adapter->firmware->data;
412 fw.fw_len = adapter->firmware->size;
414 if (adapter->if_ops.dnld_fw)
415 ret = adapter->if_ops.dnld_fw(adapter, &fw);
416 else
417 ret = mwifiex_dnld_fw(adapter, &fw);
418 if (ret == -1)
419 goto err_dnld_fw;
421 dev_notice(adapter->dev, "WLAN FW is active\n");
423 if (cal_data_cfg) {
424 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
425 adapter->dev)) < 0)
426 dev_err(adapter->dev,
427 "Cal data request_firmware() failed\n");
430 /* enable host interrupt after fw dnld is successful */
431 if (adapter->if_ops.enable_int) {
432 if (adapter->if_ops.enable_int(adapter))
433 goto err_dnld_fw;
436 adapter->init_wait_q_woken = false;
437 ret = mwifiex_init_fw(adapter);
438 if (ret == -1) {
439 goto err_init_fw;
440 } else if (!ret) {
441 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
442 goto done;
444 /* Wait for mwifiex_init to complete */
445 wait_event_interruptible(adapter->init_wait_q,
446 adapter->init_wait_q_woken);
447 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
448 goto err_init_fw;
450 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
451 if (mwifiex_register_cfg80211(adapter)) {
452 dev_err(adapter->dev, "cannot register with cfg80211\n");
453 goto err_init_fw;
456 if (mwifiex_init_channel_scan_gap(adapter)) {
457 dev_err(adapter->dev, "could not init channel stats table\n");
458 goto err_init_fw;
461 if (driver_mode) {
462 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
463 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
466 rtnl_lock();
467 /* Create station interface by default */
468 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
469 NL80211_IFTYPE_STATION, NULL, NULL);
470 if (IS_ERR(wdev)) {
471 dev_err(adapter->dev, "cannot create default STA interface\n");
472 rtnl_unlock();
473 goto err_add_intf;
476 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
477 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
478 NL80211_IFTYPE_AP, NULL, NULL);
479 if (IS_ERR(wdev)) {
480 dev_err(adapter->dev, "cannot create AP interface\n");
481 rtnl_unlock();
482 goto err_add_intf;
486 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
487 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
488 NL80211_IFTYPE_P2P_CLIENT, NULL,
489 NULL);
490 if (IS_ERR(wdev)) {
491 dev_err(adapter->dev,
492 "cannot create p2p client interface\n");
493 rtnl_unlock();
494 goto err_add_intf;
497 rtnl_unlock();
499 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
500 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
501 goto done;
503 err_add_intf:
504 wiphy_unregister(adapter->wiphy);
505 wiphy_free(adapter->wiphy);
506 err_init_fw:
507 if (adapter->if_ops.disable_int)
508 adapter->if_ops.disable_int(adapter);
509 err_dnld_fw:
510 pr_debug("info: %s: unregister device\n", __func__);
511 if (adapter->if_ops.unregister_dev)
512 adapter->if_ops.unregister_dev(adapter);
514 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
515 pr_debug("info: %s: shutdown mwifiex\n", __func__);
516 adapter->init_wait_q_woken = false;
518 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
519 wait_event_interruptible(adapter->init_wait_q,
520 adapter->init_wait_q_woken);
522 adapter->surprise_removed = true;
523 mwifiex_terminate_workqueue(adapter);
524 init_failed = true;
525 done:
526 if (adapter->cal_data) {
527 release_firmware(adapter->cal_data);
528 adapter->cal_data = NULL;
530 if (adapter->firmware) {
531 release_firmware(adapter->firmware);
532 adapter->firmware = NULL;
534 if (init_failed)
535 mwifiex_free_adapter(adapter);
536 up(sem);
537 return;
541 * This function initializes the hardware and gets firmware.
543 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
545 int ret;
547 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
548 adapter->dev, GFP_KERNEL, adapter,
549 mwifiex_fw_dpc);
550 if (ret < 0)
551 dev_err(adapter->dev,
552 "request_firmware_nowait() returned error %d\n", ret);
553 return ret;
557 * CFG802.11 network device handler for open.
559 * Starts the data queue.
561 static int
562 mwifiex_open(struct net_device *dev)
564 netif_tx_start_all_queues(dev);
565 return 0;
569 * CFG802.11 network device handler for close.
571 static int
572 mwifiex_close(struct net_device *dev)
574 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
576 if (priv->scan_request) {
577 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
578 cfg80211_scan_done(priv->scan_request, 1);
579 priv->scan_request = NULL;
580 priv->scan_aborting = true;
583 return 0;
587 * Add buffer into wmm tx queue and queue work to transmit it.
589 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
591 struct netdev_queue *txq;
592 int index = mwifiex_1d_to_wmm_queue[skb->priority];
594 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
595 txq = netdev_get_tx_queue(priv->netdev, index);
596 if (!netif_tx_queue_stopped(txq)) {
597 netif_tx_stop_queue(txq);
598 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
602 atomic_inc(&priv->adapter->tx_pending);
603 mwifiex_wmm_add_buf_txqueue(priv, skb);
605 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
607 return 0;
610 struct sk_buff *
611 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
612 struct sk_buff *skb, u8 flag, u64 *cookie)
614 struct sk_buff *orig_skb = skb;
615 struct mwifiex_txinfo *tx_info, *orig_tx_info;
617 skb = skb_clone(skb, GFP_ATOMIC);
618 if (skb) {
619 unsigned long flags;
620 int id;
622 spin_lock_irqsave(&priv->ack_status_lock, flags);
623 id = idr_alloc(&priv->ack_status_frames, orig_skb,
624 1, 0xff, GFP_ATOMIC);
625 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
627 if (id >= 0) {
628 tx_info = MWIFIEX_SKB_TXCB(skb);
629 tx_info->ack_frame_id = id;
630 tx_info->flags |= flag;
631 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
632 orig_tx_info->ack_frame_id = id;
633 orig_tx_info->flags |= flag;
635 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
636 orig_tx_info->cookie = *cookie;
638 } else if (skb_shared(skb)) {
639 kfree_skb(orig_skb);
640 } else {
641 kfree_skb(skb);
642 skb = orig_skb;
644 } else {
645 /* couldn't clone -- lose tx status ... */
646 skb = orig_skb;
649 return skb;
653 * CFG802.11 network device handler for data transmission.
655 static int
656 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
658 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
659 struct sk_buff *new_skb;
660 struct mwifiex_txinfo *tx_info;
661 bool multicast;
663 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
664 jiffies, priv->bss_type, priv->bss_num);
666 if (priv->adapter->surprise_removed) {
667 kfree_skb(skb);
668 priv->stats.tx_dropped++;
669 return 0;
671 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
672 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
673 kfree_skb(skb);
674 priv->stats.tx_dropped++;
675 return 0;
677 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
678 dev_dbg(priv->adapter->dev,
679 "data: Tx: insufficient skb headroom %d\n",
680 skb_headroom(skb));
681 /* Insufficient skb headroom - allocate a new skb */
682 new_skb =
683 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
684 if (unlikely(!new_skb)) {
685 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
686 kfree_skb(skb);
687 priv->stats.tx_dropped++;
688 return 0;
690 kfree_skb(skb);
691 skb = new_skb;
692 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
693 skb_headroom(skb));
696 tx_info = MWIFIEX_SKB_TXCB(skb);
697 memset(tx_info, 0, sizeof(*tx_info));
698 tx_info->bss_num = priv->bss_num;
699 tx_info->bss_type = priv->bss_type;
700 tx_info->pkt_len = skb->len;
702 multicast = is_multicast_ether_addr(skb->data);
704 if (unlikely(!multicast && skb->sk &&
705 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
706 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
707 skb = mwifiex_clone_skb_for_tx_status(priv,
708 skb,
709 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
711 /* Record the current time the packet was queued; used to
712 * determine the amount of time the packet was queued in
713 * the driver before it was sent to the firmware.
714 * The delay is then sent along with the packet to the
715 * firmware for aggregate delay calculation for stats and
716 * MSDU lifetime expiry.
718 __net_timestamp(skb);
720 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
721 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
722 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
723 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
724 mwifiex_tdls_check_tx(priv, skb);
727 mwifiex_queue_tx_pkt(priv, skb);
729 return 0;
733 * CFG802.11 network device handler for setting MAC address.
735 static int
736 mwifiex_set_mac_address(struct net_device *dev, void *addr)
738 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
739 struct sockaddr *hw_addr = addr;
740 int ret;
742 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
744 /* Send request to firmware */
745 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
746 HostCmd_ACT_GEN_SET, 0, NULL, true);
748 if (!ret)
749 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
750 else
751 dev_err(priv->adapter->dev,
752 "set mac address failed: ret=%d\n", ret);
754 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
756 return ret;
760 * CFG802.11 network device handler for setting multicast list.
762 static void mwifiex_set_multicast_list(struct net_device *dev)
764 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
765 struct mwifiex_multicast_list mcast_list;
767 if (dev->flags & IFF_PROMISC) {
768 mcast_list.mode = MWIFIEX_PROMISC_MODE;
769 } else if (dev->flags & IFF_ALLMULTI ||
770 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
771 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
772 } else {
773 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
774 mcast_list.num_multicast_addr =
775 mwifiex_copy_mcast_addr(&mcast_list, dev);
777 mwifiex_request_set_multicast_list(priv, &mcast_list);
781 * CFG802.11 network device handler for transmission timeout.
783 static void
784 mwifiex_tx_timeout(struct net_device *dev)
786 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
788 priv->num_tx_timeout++;
789 priv->tx_timeout_cnt++;
790 dev_err(priv->adapter->dev,
791 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
792 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
793 mwifiex_set_trans_start(dev);
795 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
796 priv->adapter->if_ops.card_reset) {
797 dev_err(priv->adapter->dev,
798 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
799 priv->adapter->if_ops.card_reset(priv->adapter);
803 void mwifiex_dump_drv_info(struct mwifiex_adapter *adapter)
805 void *p;
806 char drv_version[64];
807 struct usb_card_rec *cardp;
808 struct sdio_mmc_card *sdio_card;
809 struct mwifiex_private *priv;
810 int i, idx;
811 struct netdev_queue *txq;
812 struct mwifiex_debug_info *debug_info;
814 if (adapter->drv_info_dump) {
815 vfree(adapter->drv_info_dump);
816 adapter->drv_info_size = 0;
819 dev_info(adapter->dev, "=== DRIVER INFO DUMP START===\n");
821 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
823 if (!adapter->drv_info_dump)
824 return;
826 p = (char *)(adapter->drv_info_dump);
827 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
829 mwifiex_drv_get_driver_version(adapter, drv_version,
830 sizeof(drv_version) - 1);
831 p += sprintf(p, "driver_version = %s\n", drv_version);
833 if (adapter->iface_type == MWIFIEX_USB) {
834 cardp = (struct usb_card_rec *)adapter->card;
835 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
836 atomic_read(&cardp->tx_cmd_urb_pending));
837 p += sprintf(p, "tx_data_urb_pending = %d\n",
838 atomic_read(&cardp->tx_data_urb_pending));
839 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
840 atomic_read(&cardp->rx_cmd_urb_pending));
841 p += sprintf(p, "rx_data_urb_pending = %d\n",
842 atomic_read(&cardp->rx_data_urb_pending));
845 p += sprintf(p, "tx_pending = %d\n",
846 atomic_read(&adapter->tx_pending));
847 p += sprintf(p, "rx_pending = %d\n",
848 atomic_read(&adapter->rx_pending));
850 if (adapter->iface_type == MWIFIEX_SDIO) {
851 sdio_card = (struct sdio_mmc_card *)adapter->card;
852 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
853 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
854 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
855 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
858 for (i = 0; i < adapter->priv_num; i++) {
859 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
860 continue;
861 priv = adapter->priv[i];
862 p += sprintf(p, "\n[interface : \"%s\"]\n",
863 priv->netdev->name);
864 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
865 atomic_read(&priv->wmm_tx_pending[0]));
866 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
867 atomic_read(&priv->wmm_tx_pending[1]));
868 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
869 atomic_read(&priv->wmm_tx_pending[2]));
870 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
871 atomic_read(&priv->wmm_tx_pending[3]));
872 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
873 "Disconnected" : "Connected");
874 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
875 ? "on" : "off"));
876 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
877 txq = netdev_get_tx_queue(priv->netdev, idx);
878 p += sprintf(p, "tx queue %d:%s ", idx,
879 netif_tx_queue_stopped(txq) ?
880 "stopped" : "started");
882 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
883 priv->netdev->name, priv->num_tx_timeout);
886 if (adapter->iface_type == MWIFIEX_SDIO) {
887 p += sprintf(p, "\n=== SDIO register DUMP===\n");
888 if (adapter->if_ops.reg_dump)
889 p += adapter->if_ops.reg_dump(adapter, p);
892 p += sprintf(p, "\n=== MORE DEBUG INFORMATION\n");
893 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
894 if (debug_info) {
895 for (i = 0; i < adapter->priv_num; i++) {
896 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
897 continue;
898 priv = adapter->priv[i];
899 mwifiex_get_debug_info(priv, debug_info);
900 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
901 break;
903 kfree(debug_info);
906 adapter->drv_info_size = p - adapter->drv_info_dump;
907 dev_info(adapter->dev, "=== DRIVER INFO DUMP END===\n");
909 EXPORT_SYMBOL_GPL(mwifiex_dump_drv_info);
912 * CFG802.11 network device handler for statistics retrieval.
914 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
916 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
918 return &priv->stats;
921 static u16
922 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
923 void *accel_priv, select_queue_fallback_t fallback)
925 skb->priority = cfg80211_classify8021d(skb, NULL);
926 return mwifiex_1d_to_wmm_queue[skb->priority];
929 /* Network device handlers */
930 static const struct net_device_ops mwifiex_netdev_ops = {
931 .ndo_open = mwifiex_open,
932 .ndo_stop = mwifiex_close,
933 .ndo_start_xmit = mwifiex_hard_start_xmit,
934 .ndo_set_mac_address = mwifiex_set_mac_address,
935 .ndo_tx_timeout = mwifiex_tx_timeout,
936 .ndo_get_stats = mwifiex_get_stats,
937 .ndo_set_rx_mode = mwifiex_set_multicast_list,
938 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
942 * This function initializes the private structure parameters.
944 * The following wait queues are initialized -
945 * - IOCTL wait queue
946 * - Command wait queue
947 * - Statistics wait queue
949 * ...and the following default parameters are set -
950 * - Current key index : Set to 0
951 * - Rate index : Set to auto
952 * - Media connected : Set to disconnected
953 * - Adhoc link sensed : Set to false
954 * - Nick name : Set to null
955 * - Number of Tx timeout : Set to 0
956 * - Device address : Set to current address
957 * - Rx histogram statistc : Set to 0
959 * In addition, the CFG80211 work queue is also created.
961 void mwifiex_init_priv_params(struct mwifiex_private *priv,
962 struct net_device *dev)
964 dev->netdev_ops = &mwifiex_netdev_ops;
965 dev->destructor = free_netdev;
966 /* Initialize private structure */
967 priv->current_key_index = 0;
968 priv->media_connected = false;
969 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
970 memset(priv->mgmt_ie, 0,
971 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
972 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
973 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
974 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
975 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
976 priv->num_tx_timeout = 0;
977 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
979 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
980 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
981 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
982 if (priv->hist_data)
983 mwifiex_hist_data_reset(priv);
988 * This function check if command is pending.
990 int is_command_pending(struct mwifiex_adapter *adapter)
992 unsigned long flags;
993 int is_cmd_pend_q_empty;
995 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
996 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
997 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
999 return !is_cmd_pend_q_empty;
1003 * This is the RX work queue function.
1005 * It handles the RX operations.
1007 static void mwifiex_rx_work_queue(struct work_struct *work)
1009 struct mwifiex_adapter *adapter =
1010 container_of(work, struct mwifiex_adapter, rx_work);
1012 if (adapter->surprise_removed)
1013 return;
1014 mwifiex_process_rx(adapter);
1018 * This is the main work queue function.
1020 * It handles the main process, which in turn handles the complete
1021 * driver operations.
1023 static void mwifiex_main_work_queue(struct work_struct *work)
1025 struct mwifiex_adapter *adapter =
1026 container_of(work, struct mwifiex_adapter, main_work);
1028 if (adapter->surprise_removed)
1029 return;
1030 mwifiex_main_process(adapter);
1034 * This function adds the card.
1036 * This function follows the following major steps to set up the device -
1037 * - Initialize software. This includes probing the card, registering
1038 * the interface operations table, and allocating/initializing the
1039 * adapter structure
1040 * - Set up the netlink socket
1041 * - Create and start the main work queue
1042 * - Register the device
1043 * - Initialize firmware and hardware
1044 * - Add logical interfaces
1047 mwifiex_add_card(void *card, struct semaphore *sem,
1048 struct mwifiex_if_ops *if_ops, u8 iface_type)
1050 struct mwifiex_adapter *adapter;
1052 if (down_interruptible(sem))
1053 goto exit_sem_err;
1055 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
1056 pr_err("%s: software init failed\n", __func__);
1057 goto err_init_sw;
1060 adapter->iface_type = iface_type;
1061 adapter->card_sem = sem;
1063 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1064 adapter->surprise_removed = false;
1065 init_waitqueue_head(&adapter->init_wait_q);
1066 adapter->is_suspended = false;
1067 adapter->hs_activated = false;
1068 init_waitqueue_head(&adapter->hs_activate_wait_q);
1069 init_waitqueue_head(&adapter->cmd_wait_q.wait);
1070 adapter->cmd_wait_q.status = 0;
1071 adapter->scan_wait_q_woken = false;
1073 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
1074 adapter->rx_work_enabled = true;
1075 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1078 adapter->workqueue =
1079 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1080 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1081 if (!adapter->workqueue)
1082 goto err_kmalloc;
1084 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1086 if (adapter->rx_work_enabled) {
1087 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1088 WQ_HIGHPRI |
1089 WQ_MEM_RECLAIM |
1090 WQ_UNBOUND, 1);
1091 if (!adapter->rx_workqueue)
1092 goto err_kmalloc;
1094 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1097 if (adapter->if_ops.iface_work)
1098 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
1100 /* Register the device. Fill up the private data structure with relevant
1101 information from the card. */
1102 if (adapter->if_ops.register_dev(adapter)) {
1103 pr_err("%s: failed to register mwifiex device\n", __func__);
1104 goto err_registerdev;
1107 if (mwifiex_init_hw_fw(adapter)) {
1108 pr_err("%s: firmware init failed\n", __func__);
1109 goto err_init_fw;
1112 return 0;
1114 err_init_fw:
1115 pr_debug("info: %s: unregister device\n", __func__);
1116 if (adapter->if_ops.unregister_dev)
1117 adapter->if_ops.unregister_dev(adapter);
1118 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1119 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1120 adapter->init_wait_q_woken = false;
1122 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
1123 wait_event_interruptible(adapter->init_wait_q,
1124 adapter->init_wait_q_woken);
1126 err_registerdev:
1127 adapter->surprise_removed = true;
1128 mwifiex_terminate_workqueue(adapter);
1129 err_kmalloc:
1130 mwifiex_free_adapter(adapter);
1132 err_init_sw:
1133 up(sem);
1135 exit_sem_err:
1136 return -1;
1138 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1141 * This function removes the card.
1143 * This function follows the following major steps to remove the device -
1144 * - Stop data traffic
1145 * - Shutdown firmware
1146 * - Remove the logical interfaces
1147 * - Terminate the work queue
1148 * - Unregister the device
1149 * - Free the adapter structure
1151 int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1153 struct mwifiex_private *priv = NULL;
1154 int i;
1156 if (down_interruptible(sem))
1157 goto exit_sem_err;
1159 if (!adapter)
1160 goto exit_remove;
1162 /* We can no longer handle interrupts once we start doing the teardown
1163 * below. */
1164 if (adapter->if_ops.disable_int)
1165 adapter->if_ops.disable_int(adapter);
1167 adapter->surprise_removed = true;
1169 /* Stop data */
1170 for (i = 0; i < adapter->priv_num; i++) {
1171 priv = adapter->priv[i];
1172 if (priv && priv->netdev) {
1173 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1174 if (netif_carrier_ok(priv->netdev))
1175 netif_carrier_off(priv->netdev);
1179 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1180 adapter->init_wait_q_woken = false;
1182 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
1183 wait_event_interruptible(adapter->init_wait_q,
1184 adapter->init_wait_q_woken);
1185 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1186 if (atomic_read(&adapter->rx_pending) ||
1187 atomic_read(&adapter->tx_pending) ||
1188 atomic_read(&adapter->cmd_pending)) {
1189 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
1190 "cmd_pending=%d\n",
1191 atomic_read(&adapter->rx_pending),
1192 atomic_read(&adapter->tx_pending),
1193 atomic_read(&adapter->cmd_pending));
1196 for (i = 0; i < adapter->priv_num; i++) {
1197 priv = adapter->priv[i];
1199 if (!priv)
1200 continue;
1202 rtnl_lock();
1203 if (priv->wdev && priv->netdev)
1204 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
1205 rtnl_unlock();
1208 wiphy_unregister(adapter->wiphy);
1209 wiphy_free(adapter->wiphy);
1211 mwifiex_terminate_workqueue(adapter);
1213 /* Unregister device */
1214 dev_dbg(adapter->dev, "info: unregister device\n");
1215 if (adapter->if_ops.unregister_dev)
1216 adapter->if_ops.unregister_dev(adapter);
1217 /* Free adapter structure */
1218 dev_dbg(adapter->dev, "info: free adapter\n");
1219 mwifiex_free_adapter(adapter);
1221 exit_remove:
1222 up(sem);
1223 exit_sem_err:
1224 return 0;
1226 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1229 * This function initializes the module.
1231 * The debug FS is also initialized if configured.
1233 static int
1234 mwifiex_init_module(void)
1236 #ifdef CONFIG_DEBUG_FS
1237 mwifiex_debugfs_init();
1238 #endif
1239 return 0;
1243 * This function cleans up the module.
1245 * The debug FS is removed if available.
1247 static void
1248 mwifiex_cleanup_module(void)
1250 #ifdef CONFIG_DEBUG_FS
1251 mwifiex_debugfs_remove();
1252 #endif
1255 module_init(mwifiex_init_module);
1256 module_exit(mwifiex_cleanup_module);
1258 MODULE_AUTHOR("Marvell International Ltd.");
1259 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1260 MODULE_VERSION(VERSION);
1261 MODULE_LICENSE("GPL v2");