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