]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/mwifiex/main.c
Merge remote-tracking branch 'sound-current/for-linus'
[karo-tx-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 static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
28 module_param(debug_mask, uint, 0);
29 MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
30
31 const char driver_version[] = "mwifiex " VERSION " (%s) ";
32 static char *cal_data_cfg;
33 module_param(cal_data_cfg, charp, 0);
34
35 static unsigned short driver_mode;
36 module_param(driver_mode, ushort, 0);
37 MODULE_PARM_DESC(driver_mode,
38                  "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
39
40 /*
41  * This function registers the device and performs all the necessary
42  * initializations.
43  *
44  * The following initialization operations are performed -
45  *      - Allocate adapter structure
46  *      - Save interface specific operations table in adapter
47  *      - Call interface specific initialization routine
48  *      - Allocate private structures
49  *      - Set default adapter structure parameters
50  *      - Initialize locks
51  *
52  * In case of any errors during inittialization, this function also ensures
53  * proper cleanup before exiting.
54  */
55 static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
56                             void **padapter)
57 {
58         struct mwifiex_adapter *adapter;
59         int i;
60
61         adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
62         if (!adapter)
63                 return -ENOMEM;
64
65         *padapter = adapter;
66         adapter->card = card;
67
68         /* Save interface specific operations in adapter */
69         memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
70         adapter->debug_mask = debug_mask;
71
72         /* card specific initialization has been deferred until now .. */
73         if (adapter->if_ops.init_if)
74                 if (adapter->if_ops.init_if(adapter))
75                         goto error;
76
77         adapter->priv_num = 0;
78
79         for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
80                 /* Allocate memory for private structure */
81                 adapter->priv[i] =
82                         kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
83                 if (!adapter->priv[i])
84                         goto error;
85
86                 adapter->priv[i]->adapter = adapter;
87                 adapter->priv_num++;
88         }
89         mwifiex_init_lock_list(adapter);
90
91         setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
92                     (unsigned long)adapter);
93
94         return 0;
95
96 error:
97         mwifiex_dbg(adapter, ERROR,
98                     "info: leave mwifiex_register with error\n");
99
100         for (i = 0; i < adapter->priv_num; i++)
101                 kfree(adapter->priv[i]);
102
103         kfree(adapter);
104
105         return -1;
106 }
107
108 /*
109  * This function unregisters the device and performs all the necessary
110  * cleanups.
111  *
112  * The following cleanup operations are performed -
113  *      - Free the timers
114  *      - Free beacon buffers
115  *      - Free private structures
116  *      - Free adapter structure
117  */
118 static int mwifiex_unregister(struct mwifiex_adapter *adapter)
119 {
120         s32 i;
121
122         if (adapter->if_ops.cleanup_if)
123                 adapter->if_ops.cleanup_if(adapter);
124
125         del_timer_sync(&adapter->cmd_timer);
126
127         /* Free private structures */
128         for (i = 0; i < adapter->priv_num; i++) {
129                 if (adapter->priv[i]) {
130                         mwifiex_free_curr_bcn(adapter->priv[i]);
131                         kfree(adapter->priv[i]);
132                 }
133         }
134
135         vfree(adapter->chan_stats);
136         kfree(adapter);
137         return 0;
138 }
139
140 void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
141 {
142         unsigned long flags;
143
144         spin_lock_irqsave(&adapter->main_proc_lock, flags);
145         if (adapter->mwifiex_processing) {
146                 adapter->more_task_flag = true;
147                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
148         } else {
149                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
150                 queue_work(adapter->workqueue, &adapter->main_work);
151         }
152 }
153 EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
154
155 static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
156 {
157         unsigned long flags;
158
159         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
160         if (adapter->rx_processing) {
161                 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
162         } else {
163                 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
164                 queue_work(adapter->rx_workqueue, &adapter->rx_work);
165         }
166 }
167
168 static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
169 {
170         unsigned long flags;
171         struct sk_buff *skb;
172         struct mwifiex_rxinfo *rx_info;
173
174         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
175         if (adapter->rx_processing || adapter->rx_locked) {
176                 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
177                 goto exit_rx_proc;
178         } else {
179                 adapter->rx_processing = true;
180                 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
181         }
182
183         /* Check for Rx data */
184         while ((skb = skb_dequeue(&adapter->rx_data_q))) {
185                 atomic_dec(&adapter->rx_pending);
186                 if ((adapter->delay_main_work ||
187                      adapter->iface_type == MWIFIEX_USB) &&
188                     (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
189                         if (adapter->if_ops.submit_rem_rx_urbs)
190                                 adapter->if_ops.submit_rem_rx_urbs(adapter);
191                         adapter->delay_main_work = false;
192                         mwifiex_queue_main_work(adapter);
193                 }
194                 rx_info = MWIFIEX_SKB_RXCB(skb);
195                 if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
196                         if (adapter->if_ops.deaggr_pkt)
197                                 adapter->if_ops.deaggr_pkt(adapter, skb);
198                         dev_kfree_skb_any(skb);
199                 } else {
200                         mwifiex_handle_rx_packet(adapter, skb);
201                 }
202         }
203         spin_lock_irqsave(&adapter->rx_proc_lock, flags);
204         adapter->rx_processing = false;
205         spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
206
207 exit_rx_proc:
208         return 0;
209 }
210
211 /*
212  * The main process.
213  *
214  * This function is the main procedure of the driver and handles various driver
215  * operations. It runs in a loop and provides the core functionalities.
216  *
217  * The main responsibilities of this function are -
218  *      - Ensure concurrency control
219  *      - Handle pending interrupts and call interrupt handlers
220  *      - Wake up the card if required
221  *      - Handle command responses and call response handlers
222  *      - Handle events and call event handlers
223  *      - Execute pending commands
224  *      - Transmit pending data packets
225  */
226 int mwifiex_main_process(struct mwifiex_adapter *adapter)
227 {
228         int ret = 0;
229         unsigned long flags;
230
231         spin_lock_irqsave(&adapter->main_proc_lock, flags);
232
233         /* Check if already processing */
234         if (adapter->mwifiex_processing || adapter->main_locked) {
235                 adapter->more_task_flag = true;
236                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
237                 goto exit_main_proc;
238         } else {
239                 adapter->mwifiex_processing = true;
240                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
241         }
242 process_start:
243         do {
244                 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
245                     (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
246                         break;
247
248                 /* For non-USB interfaces, If we process interrupts first, it
249                  * would increase RX pending even further. Avoid this by
250                  * checking if rx_pending has crossed high threshold and
251                  * schedule rx work queue and then process interrupts.
252                  * For USB interface, there are no interrupts. We already have
253                  * HIGH_RX_PENDING check in usb.c
254                  */
255                 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
256                     adapter->iface_type != MWIFIEX_USB) {
257                         adapter->delay_main_work = true;
258                         mwifiex_queue_rx_work(adapter);
259                         break;
260                 }
261
262                 /* Handle pending interrupt if any */
263                 if (adapter->int_status) {
264                         if (adapter->hs_activated)
265                                 mwifiex_process_hs_config(adapter);
266                         if (adapter->if_ops.process_int_status)
267                                 adapter->if_ops.process_int_status(adapter);
268                 }
269
270                 if (adapter->rx_work_enabled && adapter->data_received)
271                         mwifiex_queue_rx_work(adapter);
272
273                 /* Need to wake up the card ? */
274                 if ((adapter->ps_state == PS_STATE_SLEEP) &&
275                     (adapter->pm_wakeup_card_req &&
276                      !adapter->pm_wakeup_fw_try) &&
277                     (is_command_pending(adapter) ||
278                      !skb_queue_empty(&adapter->tx_data_q) ||
279                      !mwifiex_bypass_txlist_empty(adapter) ||
280                      !mwifiex_wmm_lists_empty(adapter))) {
281                         adapter->pm_wakeup_fw_try = true;
282                         mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
283                         adapter->if_ops.wakeup(adapter);
284                         continue;
285                 }
286
287                 if (IS_CARD_RX_RCVD(adapter)) {
288                         adapter->data_received = false;
289                         adapter->pm_wakeup_fw_try = false;
290                         del_timer(&adapter->wakeup_timer);
291                         if (adapter->ps_state == PS_STATE_SLEEP)
292                                 adapter->ps_state = PS_STATE_AWAKE;
293                 } else {
294                         /* We have tried to wakeup the card already */
295                         if (adapter->pm_wakeup_fw_try)
296                                 break;
297                         if (adapter->ps_state != PS_STATE_AWAKE)
298                                 break;
299                         if (adapter->tx_lock_flag) {
300                                 if (adapter->iface_type == MWIFIEX_USB) {
301                                         if (!adapter->usb_mc_setup)
302                                                 break;
303                                 } else
304                                         break;
305                         }
306
307                         if ((!adapter->scan_chan_gap_enabled &&
308                              adapter->scan_processing) || adapter->data_sent ||
309                              mwifiex_is_tdls_chan_switching
310                              (mwifiex_get_priv(adapter,
311                                                MWIFIEX_BSS_ROLE_STA)) ||
312                             (mwifiex_wmm_lists_empty(adapter) &&
313                              mwifiex_bypass_txlist_empty(adapter) &&
314                              skb_queue_empty(&adapter->tx_data_q))) {
315                                 if (adapter->cmd_sent || adapter->curr_cmd ||
316                                         !mwifiex_is_send_cmd_allowed
317                                                 (mwifiex_get_priv(adapter,
318                                                 MWIFIEX_BSS_ROLE_STA)) ||
319                                     (!is_command_pending(adapter)))
320                                         break;
321                         }
322                 }
323
324                 /* Check for event */
325                 if (adapter->event_received) {
326                         adapter->event_received = false;
327                         mwifiex_process_event(adapter);
328                 }
329
330                 /* Check for Cmd Resp */
331                 if (adapter->cmd_resp_received) {
332                         adapter->cmd_resp_received = false;
333                         mwifiex_process_cmdresp(adapter);
334
335                         /* call mwifiex back when init_fw is done */
336                         if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
337                                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
338                                 mwifiex_init_fw_complete(adapter);
339                         }
340                 }
341
342                 /* Check if we need to confirm Sleep Request
343                    received previously */
344                 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
345                         if (!adapter->cmd_sent && !adapter->curr_cmd)
346                                 mwifiex_check_ps_cond(adapter);
347                 }
348
349                 /* * The ps_state may have been changed during processing of
350                  * Sleep Request event.
351                  */
352                 if ((adapter->ps_state == PS_STATE_SLEEP) ||
353                     (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
354                     (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
355                         continue;
356                 }
357
358                 if (adapter->tx_lock_flag) {
359                         if (adapter->iface_type == MWIFIEX_USB) {
360                                 if (!adapter->usb_mc_setup)
361                                         continue;
362                         } else
363                                 continue;
364                 }
365
366                 if (!adapter->cmd_sent && !adapter->curr_cmd &&
367                     mwifiex_is_send_cmd_allowed
368                     (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
369                         if (mwifiex_exec_next_cmd(adapter) == -1) {
370                                 ret = -1;
371                                 break;
372                         }
373                 }
374
375                 /** If USB Multi channel setup ongoing,
376                  *  wait for ready to tx data.
377                  */
378                 if (adapter->iface_type == MWIFIEX_USB &&
379                     adapter->usb_mc_setup)
380                         continue;
381
382                 if ((adapter->scan_chan_gap_enabled ||
383                      !adapter->scan_processing) &&
384                     !adapter->data_sent &&
385                     !skb_queue_empty(&adapter->tx_data_q)) {
386                         mwifiex_process_tx_queue(adapter);
387                         if (adapter->hs_activated) {
388                                 adapter->is_hs_configured = false;
389                                 mwifiex_hs_activated_event
390                                         (mwifiex_get_priv
391                                         (adapter, MWIFIEX_BSS_ROLE_ANY),
392                                         false);
393                         }
394                 }
395
396                 if ((adapter->scan_chan_gap_enabled ||
397                      !adapter->scan_processing) &&
398                     !adapter->data_sent &&
399                     !mwifiex_bypass_txlist_empty(adapter) &&
400                     !mwifiex_is_tdls_chan_switching
401                         (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
402                         mwifiex_process_bypass_tx(adapter);
403                         if (adapter->hs_activated) {
404                                 adapter->is_hs_configured = false;
405                                 mwifiex_hs_activated_event
406                                         (mwifiex_get_priv
407                                          (adapter, MWIFIEX_BSS_ROLE_ANY),
408                                          false);
409                         }
410                 }
411
412                 if ((adapter->scan_chan_gap_enabled ||
413                      !adapter->scan_processing) &&
414                     !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
415                     !mwifiex_is_tdls_chan_switching
416                         (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
417                         mwifiex_wmm_process_tx(adapter);
418                         if (adapter->hs_activated) {
419                                 adapter->is_hs_configured = false;
420                                 mwifiex_hs_activated_event
421                                         (mwifiex_get_priv
422                                          (adapter, MWIFIEX_BSS_ROLE_ANY),
423                                          false);
424                         }
425                 }
426
427                 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
428                     !adapter->curr_cmd && !is_command_pending(adapter) &&
429                     (mwifiex_wmm_lists_empty(adapter) &&
430                      mwifiex_bypass_txlist_empty(adapter) &&
431                      skb_queue_empty(&adapter->tx_data_q))) {
432                         if (!mwifiex_send_null_packet
433                             (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
434                              MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
435                              MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
436                                 adapter->delay_null_pkt = false;
437                                 adapter->ps_state = PS_STATE_SLEEP;
438                         }
439                         break;
440                 }
441         } while (true);
442
443         spin_lock_irqsave(&adapter->main_proc_lock, flags);
444         if (adapter->more_task_flag) {
445                 adapter->more_task_flag = false;
446                 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
447                 goto process_start;
448         }
449         adapter->mwifiex_processing = false;
450         spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
451
452 exit_main_proc:
453         if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
454                 mwifiex_shutdown_drv(adapter);
455         return ret;
456 }
457 EXPORT_SYMBOL_GPL(mwifiex_main_process);
458
459 /*
460  * This function frees the adapter structure.
461  *
462  * Additionally, this closes the netlink socket, frees the timers
463  * and private structures.
464  */
465 static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
466 {
467         if (!adapter) {
468                 pr_err("%s: adapter is NULL\n", __func__);
469                 return;
470         }
471
472         mwifiex_unregister(adapter);
473         pr_debug("info: %s: free adapter\n", __func__);
474 }
475
476 /*
477  * This function cancels all works in the queue and destroys
478  * the main workqueue.
479  */
480 static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
481 {
482         flush_workqueue(adapter->workqueue);
483         destroy_workqueue(adapter->workqueue);
484         adapter->workqueue = NULL;
485
486         if (adapter->rx_workqueue) {
487                 flush_workqueue(adapter->rx_workqueue);
488                 destroy_workqueue(adapter->rx_workqueue);
489                 adapter->rx_workqueue = NULL;
490         }
491 }
492
493 /*
494  * This function gets firmware and initializes it.
495  *
496  * The main initialization steps followed are -
497  *      - Download the correct firmware to card
498  *      - Issue the init commands to firmware
499  */
500 static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
501 {
502         int ret;
503         char fmt[64];
504         struct mwifiex_private *priv;
505         struct mwifiex_adapter *adapter = context;
506         struct mwifiex_fw_image fw;
507         struct semaphore *sem = adapter->card_sem;
508         bool init_failed = false;
509         struct wireless_dev *wdev;
510
511         if (!firmware) {
512                 mwifiex_dbg(adapter, ERROR,
513                             "Failed to get firmware %s\n", adapter->fw_name);
514                 goto err_dnld_fw;
515         }
516
517         memset(&fw, 0, sizeof(struct mwifiex_fw_image));
518         adapter->firmware = firmware;
519         fw.fw_buf = (u8 *) adapter->firmware->data;
520         fw.fw_len = adapter->firmware->size;
521
522         if (adapter->if_ops.dnld_fw)
523                 ret = adapter->if_ops.dnld_fw(adapter, &fw);
524         else
525                 ret = mwifiex_dnld_fw(adapter, &fw);
526         if (ret == -1)
527                 goto err_dnld_fw;
528
529         mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
530
531         if (cal_data_cfg) {
532                 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
533                                       adapter->dev)) < 0)
534                         mwifiex_dbg(adapter, ERROR,
535                                     "Cal data request_firmware() failed\n");
536         }
537
538         /* enable host interrupt after fw dnld is successful */
539         if (adapter->if_ops.enable_int) {
540                 if (adapter->if_ops.enable_int(adapter))
541                         goto err_dnld_fw;
542         }
543
544         adapter->init_wait_q_woken = false;
545         ret = mwifiex_init_fw(adapter);
546         if (ret == -1) {
547                 goto err_init_fw;
548         } else if (!ret) {
549                 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
550                 goto done;
551         }
552         /* Wait for mwifiex_init to complete */
553         wait_event_interruptible(adapter->init_wait_q,
554                                  adapter->init_wait_q_woken);
555         if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
556                 goto err_init_fw;
557
558         priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
559         if (mwifiex_register_cfg80211(adapter)) {
560                 mwifiex_dbg(adapter, ERROR,
561                             "cannot register with cfg80211\n");
562                 goto err_init_fw;
563         }
564
565         if (mwifiex_init_channel_scan_gap(adapter)) {
566                 mwifiex_dbg(adapter, ERROR,
567                             "could not init channel stats table\n");
568                 goto err_init_fw;
569         }
570
571         if (driver_mode) {
572                 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
573                 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
574         }
575
576         rtnl_lock();
577         /* Create station interface by default */
578         wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
579                                         NL80211_IFTYPE_STATION, NULL, NULL);
580         if (IS_ERR(wdev)) {
581                 mwifiex_dbg(adapter, ERROR,
582                             "cannot create default STA interface\n");
583                 rtnl_unlock();
584                 goto err_add_intf;
585         }
586
587         if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
588                 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
589                                                 NL80211_IFTYPE_AP, NULL, NULL);
590                 if (IS_ERR(wdev)) {
591                         mwifiex_dbg(adapter, ERROR,
592                                     "cannot create AP interface\n");
593                         rtnl_unlock();
594                         goto err_add_intf;
595                 }
596         }
597
598         if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
599                 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
600                                                 NL80211_IFTYPE_P2P_CLIENT, NULL,
601                                                 NULL);
602                 if (IS_ERR(wdev)) {
603                         mwifiex_dbg(adapter, ERROR,
604                                     "cannot create p2p client interface\n");
605                         rtnl_unlock();
606                         goto err_add_intf;
607                 }
608         }
609         rtnl_unlock();
610
611         mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
612         mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
613         goto done;
614
615 err_add_intf:
616         wiphy_unregister(adapter->wiphy);
617         wiphy_free(adapter->wiphy);
618 err_init_fw:
619         if (adapter->if_ops.disable_int)
620                 adapter->if_ops.disable_int(adapter);
621 err_dnld_fw:
622         mwifiex_dbg(adapter, ERROR,
623                     "info: %s: unregister device\n", __func__);
624         if (adapter->if_ops.unregister_dev)
625                 adapter->if_ops.unregister_dev(adapter);
626
627         if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
628                 pr_debug("info: %s: shutdown mwifiex\n", __func__);
629                 adapter->init_wait_q_woken = false;
630
631                 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
632                         wait_event_interruptible(adapter->init_wait_q,
633                                                  adapter->init_wait_q_woken);
634         }
635         adapter->surprise_removed = true;
636         mwifiex_terminate_workqueue(adapter);
637         init_failed = true;
638 done:
639         if (adapter->cal_data) {
640                 release_firmware(adapter->cal_data);
641                 adapter->cal_data = NULL;
642         }
643         if (adapter->firmware) {
644                 release_firmware(adapter->firmware);
645                 adapter->firmware = NULL;
646         }
647         if (init_failed)
648                 mwifiex_free_adapter(adapter);
649         up(sem);
650         return;
651 }
652
653 /*
654  * This function initializes the hardware and gets firmware.
655  */
656 static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
657 {
658         int ret;
659
660         ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
661                                       adapter->dev, GFP_KERNEL, adapter,
662                                       mwifiex_fw_dpc);
663         if (ret < 0)
664                 mwifiex_dbg(adapter, ERROR,
665                             "request_firmware_nowait error %d\n", ret);
666         return ret;
667 }
668
669 /*
670  * CFG802.11 network device handler for open.
671  *
672  * Starts the data queue.
673  */
674 static int
675 mwifiex_open(struct net_device *dev)
676 {
677         netif_carrier_off(dev);
678
679         return 0;
680 }
681
682 /*
683  * CFG802.11 network device handler for close.
684  */
685 static int
686 mwifiex_close(struct net_device *dev)
687 {
688         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
689
690         if (priv->scan_request) {
691                 mwifiex_dbg(priv->adapter, INFO,
692                             "aborting scan on ndo_stop\n");
693                 cfg80211_scan_done(priv->scan_request, 1);
694                 priv->scan_request = NULL;
695                 priv->scan_aborting = true;
696         }
697
698         return 0;
699 }
700
701 static bool
702 mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
703                         struct sk_buff *skb)
704 {
705         struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
706
707         if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
708             mwifiex_is_skb_mgmt_frame(skb) ||
709             (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
710              ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
711              (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
712                 mwifiex_dbg(priv->adapter, DATA,
713                             "bypass txqueue; eth type %#x, mgmt %d\n",
714                              ntohs(eth_hdr->h_proto),
715                              mwifiex_is_skb_mgmt_frame(skb));
716                 return true;
717         }
718
719         return false;
720 }
721 /*
722  * Add buffer into wmm tx queue and queue work to transmit it.
723  */
724 int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
725 {
726         struct netdev_queue *txq;
727         int index = mwifiex_1d_to_wmm_queue[skb->priority];
728
729         if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
730                 txq = netdev_get_tx_queue(priv->netdev, index);
731                 if (!netif_tx_queue_stopped(txq)) {
732                         netif_tx_stop_queue(txq);
733                         mwifiex_dbg(priv->adapter, DATA,
734                                     "stop queue: %d\n", index);
735                 }
736         }
737
738         if (mwifiex_bypass_tx_queue(priv, skb)) {
739                 atomic_inc(&priv->adapter->tx_pending);
740                 atomic_inc(&priv->adapter->bypass_tx_pending);
741                 mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
742          } else {
743                 atomic_inc(&priv->adapter->tx_pending);
744                 mwifiex_wmm_add_buf_txqueue(priv, skb);
745          }
746
747         mwifiex_queue_main_work(priv->adapter);
748
749         return 0;
750 }
751
752 struct sk_buff *
753 mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
754                                 struct sk_buff *skb, u8 flag, u64 *cookie)
755 {
756         struct sk_buff *orig_skb = skb;
757         struct mwifiex_txinfo *tx_info, *orig_tx_info;
758
759         skb = skb_clone(skb, GFP_ATOMIC);
760         if (skb) {
761                 unsigned long flags;
762                 int id;
763
764                 spin_lock_irqsave(&priv->ack_status_lock, flags);
765                 id = idr_alloc(&priv->ack_status_frames, orig_skb,
766                                1, 0xff, GFP_ATOMIC);
767                 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
768
769                 if (id >= 0) {
770                         tx_info = MWIFIEX_SKB_TXCB(skb);
771                         tx_info->ack_frame_id = id;
772                         tx_info->flags |= flag;
773                         orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
774                         orig_tx_info->ack_frame_id = id;
775                         orig_tx_info->flags |= flag;
776
777                         if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
778                                 orig_tx_info->cookie = *cookie;
779
780                 } else if (skb_shared(skb)) {
781                         kfree_skb(orig_skb);
782                 } else {
783                         kfree_skb(skb);
784                         skb = orig_skb;
785                 }
786         } else {
787                 /* couldn't clone -- lose tx status ... */
788                 skb = orig_skb;
789         }
790
791         return skb;
792 }
793
794 /*
795  * CFG802.11 network device handler for data transmission.
796  */
797 static int
798 mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
799 {
800         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
801         struct sk_buff *new_skb;
802         struct mwifiex_txinfo *tx_info;
803         bool multicast;
804
805         mwifiex_dbg(priv->adapter, DATA,
806                     "data: %lu BSS(%d-%d): Data <= kernel\n",
807                     jiffies, priv->bss_type, priv->bss_num);
808
809         if (priv->adapter->surprise_removed) {
810                 kfree_skb(skb);
811                 priv->stats.tx_dropped++;
812                 return 0;
813         }
814         if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
815                 mwifiex_dbg(priv->adapter, ERROR,
816                             "Tx: bad skb len %d\n", skb->len);
817                 kfree_skb(skb);
818                 priv->stats.tx_dropped++;
819                 return 0;
820         }
821         if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
822                 mwifiex_dbg(priv->adapter, DATA,
823                             "data: Tx: insufficient skb headroom %d\n",
824                             skb_headroom(skb));
825                 /* Insufficient skb headroom - allocate a new skb */
826                 new_skb =
827                         skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
828                 if (unlikely(!new_skb)) {
829                         mwifiex_dbg(priv->adapter, ERROR,
830                                     "Tx: cannot alloca new_skb\n");
831                         kfree_skb(skb);
832                         priv->stats.tx_dropped++;
833                         return 0;
834                 }
835                 kfree_skb(skb);
836                 skb = new_skb;
837                 mwifiex_dbg(priv->adapter, INFO,
838                             "info: new skb headroomd %d\n",
839                             skb_headroom(skb));
840         }
841
842         tx_info = MWIFIEX_SKB_TXCB(skb);
843         memset(tx_info, 0, sizeof(*tx_info));
844         tx_info->bss_num = priv->bss_num;
845         tx_info->bss_type = priv->bss_type;
846         tx_info->pkt_len = skb->len;
847
848         multicast = is_multicast_ether_addr(skb->data);
849
850         if (unlikely(!multicast && skb->sk &&
851                      skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
852                      priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
853                 skb = mwifiex_clone_skb_for_tx_status(priv,
854                                                       skb,
855                                         MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
856
857         /* Record the current time the packet was queued; used to
858          * determine the amount of time the packet was queued in
859          * the driver before it was sent to the firmware.
860          * The delay is then sent along with the packet to the
861          * firmware for aggregate delay calculation for stats and
862          * MSDU lifetime expiry.
863          */
864         __net_timestamp(skb);
865
866         if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
867             priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
868             !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
869                 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
870                         mwifiex_tdls_check_tx(priv, skb);
871         }
872
873         mwifiex_queue_tx_pkt(priv, skb);
874
875         return 0;
876 }
877
878 /*
879  * CFG802.11 network device handler for setting MAC address.
880  */
881 static int
882 mwifiex_set_mac_address(struct net_device *dev, void *addr)
883 {
884         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
885         struct sockaddr *hw_addr = addr;
886         int ret;
887
888         memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
889
890         /* Send request to firmware */
891         ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
892                                HostCmd_ACT_GEN_SET, 0, NULL, true);
893
894         if (!ret)
895                 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
896         else
897                 mwifiex_dbg(priv->adapter, ERROR,
898                             "set mac address failed: ret=%d\n", ret);
899
900         memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
901
902         return ret;
903 }
904
905 /*
906  * CFG802.11 network device handler for setting multicast list.
907  */
908 static void mwifiex_set_multicast_list(struct net_device *dev)
909 {
910         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
911         struct mwifiex_multicast_list mcast_list;
912
913         if (dev->flags & IFF_PROMISC) {
914                 mcast_list.mode = MWIFIEX_PROMISC_MODE;
915         } else if (dev->flags & IFF_ALLMULTI ||
916                    netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
917                 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
918         } else {
919                 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
920                 mcast_list.num_multicast_addr =
921                         mwifiex_copy_mcast_addr(&mcast_list, dev);
922         }
923         mwifiex_request_set_multicast_list(priv, &mcast_list);
924 }
925
926 /*
927  * CFG802.11 network device handler for transmission timeout.
928  */
929 static void
930 mwifiex_tx_timeout(struct net_device *dev)
931 {
932         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
933
934         priv->num_tx_timeout++;
935         priv->tx_timeout_cnt++;
936         mwifiex_dbg(priv->adapter, ERROR,
937                     "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
938                     jiffies, priv->tx_timeout_cnt, priv->bss_type,
939                     priv->bss_num);
940         mwifiex_set_trans_start(dev);
941
942         if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
943             priv->adapter->if_ops.card_reset) {
944                 mwifiex_dbg(priv->adapter, ERROR,
945                             "tx_timeout_cnt exceeds threshold.\t"
946                             "Triggering card reset!\n");
947                 priv->adapter->if_ops.card_reset(priv->adapter);
948         }
949 }
950
951 void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
952 {
953         struct usb_card_rec *card = adapter->card;
954         struct mwifiex_private *priv;
955         u16 tx_buf_size;
956         int i, ret;
957
958         card->mc_resync_flag = true;
959         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
960                 if (atomic_read(&card->port[i].tx_data_urb_pending)) {
961                         mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
962                         return;
963                 }
964         }
965
966         card->mc_resync_flag = false;
967         tx_buf_size = 0xffff;
968         priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
969         ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
970                                HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
971         if (ret)
972                 mwifiex_dbg(adapter, ERROR,
973                             "send reconfig tx buf size cmd err\n");
974 }
975 EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
976
977 void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
978 {
979         void *p;
980         char drv_version[64];
981         struct usb_card_rec *cardp;
982         struct sdio_mmc_card *sdio_card;
983         struct mwifiex_private *priv;
984         int i, idx;
985         struct netdev_queue *txq;
986         struct mwifiex_debug_info *debug_info;
987
988         if (adapter->drv_info_dump) {
989                 vfree(adapter->drv_info_dump);
990                 adapter->drv_info_dump = NULL;
991                 adapter->drv_info_size = 0;
992         }
993
994         mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
995
996         adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
997
998         if (!adapter->drv_info_dump)
999                 return;
1000
1001         p = (char *)(adapter->drv_info_dump);
1002         p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1003
1004         mwifiex_drv_get_driver_version(adapter, drv_version,
1005                                        sizeof(drv_version) - 1);
1006         p += sprintf(p, "driver_version = %s\n", drv_version);
1007
1008         if (adapter->iface_type == MWIFIEX_USB) {
1009                 cardp = (struct usb_card_rec *)adapter->card;
1010                 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1011                              atomic_read(&cardp->tx_cmd_urb_pending));
1012                 p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1013                              atomic_read(&cardp->port[0].tx_data_urb_pending));
1014                 p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1015                              atomic_read(&cardp->port[1].tx_data_urb_pending));
1016                 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1017                              atomic_read(&cardp->rx_cmd_urb_pending));
1018                 p += sprintf(p, "rx_data_urb_pending = %d\n",
1019                              atomic_read(&cardp->rx_data_urb_pending));
1020         }
1021
1022         p += sprintf(p, "tx_pending = %d\n",
1023                      atomic_read(&adapter->tx_pending));
1024         p += sprintf(p, "rx_pending = %d\n",
1025                      atomic_read(&adapter->rx_pending));
1026
1027         if (adapter->iface_type == MWIFIEX_SDIO) {
1028                 sdio_card = (struct sdio_mmc_card *)adapter->card;
1029                 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1030                              sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1031                 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1032                              sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1033         }
1034
1035         for (i = 0; i < adapter->priv_num; i++) {
1036                 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1037                         continue;
1038                 priv = adapter->priv[i];
1039                 p += sprintf(p, "\n[interface  : \"%s\"]\n",
1040                              priv->netdev->name);
1041                 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1042                              atomic_read(&priv->wmm_tx_pending[0]));
1043                 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1044                              atomic_read(&priv->wmm_tx_pending[1]));
1045                 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1046                              atomic_read(&priv->wmm_tx_pending[2]));
1047                 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1048                              atomic_read(&priv->wmm_tx_pending[3]));
1049                 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1050                              "Disconnected" : "Connected");
1051                 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1052                              ? "on" : "off"));
1053                 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1054                         txq = netdev_get_tx_queue(priv->netdev, idx);
1055                         p += sprintf(p, "tx queue %d:%s  ", idx,
1056                                      netif_tx_queue_stopped(txq) ?
1057                                      "stopped" : "started");
1058                 }
1059                 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1060                              priv->netdev->name, priv->num_tx_timeout);
1061         }
1062
1063         if (adapter->iface_type == MWIFIEX_SDIO) {
1064                 p += sprintf(p, "\n=== SDIO register dump===\n");
1065                 if (adapter->if_ops.reg_dump)
1066                         p += adapter->if_ops.reg_dump(adapter, p);
1067         }
1068
1069         p += sprintf(p, "\n=== more debug information\n");
1070         debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1071         if (debug_info) {
1072                 for (i = 0; i < adapter->priv_num; i++) {
1073                         if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1074                                 continue;
1075                         priv = adapter->priv[i];
1076                         mwifiex_get_debug_info(priv, debug_info);
1077                         p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1078                         break;
1079                 }
1080                 kfree(debug_info);
1081         }
1082
1083         adapter->drv_info_size = p - adapter->drv_info_dump;
1084         mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
1085 }
1086 EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
1087
1088 void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1089 {
1090         u8 idx, *dump_data, *fw_dump_ptr;
1091         u32 dump_len;
1092
1093         dump_len = (strlen("========Start dump driverinfo========\n") +
1094                        adapter->drv_info_size +
1095                        strlen("\n========End dump========\n"));
1096
1097         for (idx = 0; idx < adapter->num_mem_types; idx++) {
1098                 struct memory_type_mapping *entry =
1099                                 &adapter->mem_type_mapping_tbl[idx];
1100
1101                 if (entry->mem_ptr) {
1102                         dump_len += (strlen("========Start dump ") +
1103                                         strlen(entry->mem_name) +
1104                                         strlen("========\n") +
1105                                         (entry->mem_size + 1) +
1106                                         strlen("\n========End dump========\n"));
1107                 }
1108         }
1109
1110         dump_data = vzalloc(dump_len + 1);
1111         if (!dump_data)
1112                 goto done;
1113
1114         fw_dump_ptr = dump_data;
1115
1116         /* Dump all the memory data into single file, a userspace script will
1117          * be used to split all the memory data to multiple files
1118          */
1119         mwifiex_dbg(adapter, MSG,
1120                     "== mwifiex dump information to /sys/class/devcoredump start");
1121
1122         strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
1123         fw_dump_ptr += strlen("========Start dump driverinfo========\n");
1124         memcpy(fw_dump_ptr, adapter->drv_info_dump, adapter->drv_info_size);
1125         fw_dump_ptr += adapter->drv_info_size;
1126         strcpy(fw_dump_ptr, "\n========End dump========\n");
1127         fw_dump_ptr += strlen("\n========End dump========\n");
1128
1129         for (idx = 0; idx < adapter->num_mem_types; idx++) {
1130                 struct memory_type_mapping *entry =
1131                                         &adapter->mem_type_mapping_tbl[idx];
1132
1133                 if (entry->mem_ptr) {
1134                         strcpy(fw_dump_ptr, "========Start dump ");
1135                         fw_dump_ptr += strlen("========Start dump ");
1136
1137                         strcpy(fw_dump_ptr, entry->mem_name);
1138                         fw_dump_ptr += strlen(entry->mem_name);
1139
1140                         strcpy(fw_dump_ptr, "========\n");
1141                         fw_dump_ptr += strlen("========\n");
1142
1143                         memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1144                         fw_dump_ptr += entry->mem_size;
1145
1146                         strcpy(fw_dump_ptr, "\n========End dump========\n");
1147                         fw_dump_ptr += strlen("\n========End dump========\n");
1148                 }
1149         }
1150
1151         /* device dump data will be free in device coredump release function
1152          * after 5 min
1153          */
1154         dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
1155         mwifiex_dbg(adapter, MSG,
1156                     "== mwifiex dump information to /sys/class/devcoredump end");
1157
1158 done:
1159         for (idx = 0; idx < adapter->num_mem_types; idx++) {
1160                 struct memory_type_mapping *entry =
1161                         &adapter->mem_type_mapping_tbl[idx];
1162
1163                 if (entry->mem_ptr) {
1164                         vfree(entry->mem_ptr);
1165                         entry->mem_ptr = NULL;
1166                 }
1167                 entry->mem_size = 0;
1168         }
1169
1170         if (adapter->drv_info_dump) {
1171                 vfree(adapter->drv_info_dump);
1172                 adapter->drv_info_dump = NULL;
1173                 adapter->drv_info_size = 0;
1174         }
1175 }
1176 EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1177
1178 /*
1179  * CFG802.11 network device handler for statistics retrieval.
1180  */
1181 static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1182 {
1183         struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1184
1185         return &priv->stats;
1186 }
1187
1188 static u16
1189 mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
1190                                 void *accel_priv, select_queue_fallback_t fallback)
1191 {
1192         skb->priority = cfg80211_classify8021d(skb, NULL);
1193         return mwifiex_1d_to_wmm_queue[skb->priority];
1194 }
1195
1196 /* Network device handlers */
1197 static const struct net_device_ops mwifiex_netdev_ops = {
1198         .ndo_open = mwifiex_open,
1199         .ndo_stop = mwifiex_close,
1200         .ndo_start_xmit = mwifiex_hard_start_xmit,
1201         .ndo_set_mac_address = mwifiex_set_mac_address,
1202         .ndo_validate_addr = eth_validate_addr,
1203         .ndo_tx_timeout = mwifiex_tx_timeout,
1204         .ndo_get_stats = mwifiex_get_stats,
1205         .ndo_set_rx_mode = mwifiex_set_multicast_list,
1206         .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
1207 };
1208
1209 /*
1210  * This function initializes the private structure parameters.
1211  *
1212  * The following wait queues are initialized -
1213  *      - IOCTL wait queue
1214  *      - Command wait queue
1215  *      - Statistics wait queue
1216  *
1217  * ...and the following default parameters are set -
1218  *      - Current key index     : Set to 0
1219  *      - Rate index            : Set to auto
1220  *      - Media connected       : Set to disconnected
1221  *      - Adhoc link sensed     : Set to false
1222  *      - Nick name             : Set to null
1223  *      - Number of Tx timeout  : Set to 0
1224  *      - Device address        : Set to current address
1225  *      - Rx histogram statistc : Set to 0
1226  *
1227  * In addition, the CFG80211 work queue is also created.
1228  */
1229 void mwifiex_init_priv_params(struct mwifiex_private *priv,
1230                               struct net_device *dev)
1231 {
1232         dev->netdev_ops = &mwifiex_netdev_ops;
1233         dev->destructor = free_netdev;
1234         /* Initialize private structure */
1235         priv->current_key_index = 0;
1236         priv->media_connected = false;
1237         memset(priv->mgmt_ie, 0,
1238                sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
1239         priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1240         priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1241         priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
1242         priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
1243         priv->num_tx_timeout = 0;
1244         ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
1245         memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
1246
1247         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1248             GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1249                 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1250                 if (priv->hist_data)
1251                         mwifiex_hist_data_reset(priv);
1252         }
1253 }
1254
1255 /*
1256  * This function check if command is pending.
1257  */
1258 int is_command_pending(struct mwifiex_adapter *adapter)
1259 {
1260         unsigned long flags;
1261         int is_cmd_pend_q_empty;
1262
1263         spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1264         is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1265         spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1266
1267         return !is_cmd_pend_q_empty;
1268 }
1269
1270 /*
1271  * This is the RX work queue function.
1272  *
1273  * It handles the RX operations.
1274  */
1275 static void mwifiex_rx_work_queue(struct work_struct *work)
1276 {
1277         struct mwifiex_adapter *adapter =
1278                 container_of(work, struct mwifiex_adapter, rx_work);
1279
1280         if (adapter->surprise_removed)
1281                 return;
1282         mwifiex_process_rx(adapter);
1283 }
1284
1285 /*
1286  * This is the main work queue function.
1287  *
1288  * It handles the main process, which in turn handles the complete
1289  * driver operations.
1290  */
1291 static void mwifiex_main_work_queue(struct work_struct *work)
1292 {
1293         struct mwifiex_adapter *adapter =
1294                 container_of(work, struct mwifiex_adapter, main_work);
1295
1296         if (adapter->surprise_removed)
1297                 return;
1298         mwifiex_main_process(adapter);
1299 }
1300
1301 /*
1302  * This function adds the card.
1303  *
1304  * This function follows the following major steps to set up the device -
1305  *      - Initialize software. This includes probing the card, registering
1306  *        the interface operations table, and allocating/initializing the
1307  *        adapter structure
1308  *      - Set up the netlink socket
1309  *      - Create and start the main work queue
1310  *      - Register the device
1311  *      - Initialize firmware and hardware
1312  *      - Add logical interfaces
1313  */
1314 int
1315 mwifiex_add_card(void *card, struct semaphore *sem,
1316                  struct mwifiex_if_ops *if_ops, u8 iface_type)
1317 {
1318         struct mwifiex_adapter *adapter;
1319
1320         if (down_interruptible(sem))
1321                 goto exit_sem_err;
1322
1323         if (mwifiex_register(card, if_ops, (void **)&adapter)) {
1324                 pr_err("%s: software init failed\n", __func__);
1325                 goto err_init_sw;
1326         }
1327
1328         adapter->iface_type = iface_type;
1329         adapter->card_sem = sem;
1330
1331         adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1332         adapter->surprise_removed = false;
1333         init_waitqueue_head(&adapter->init_wait_q);
1334         adapter->is_suspended = false;
1335         adapter->hs_activated = false;
1336         init_waitqueue_head(&adapter->hs_activate_wait_q);
1337         init_waitqueue_head(&adapter->cmd_wait_q.wait);
1338         adapter->cmd_wait_q.status = 0;
1339         adapter->scan_wait_q_woken = false;
1340
1341         if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
1342                 adapter->rx_work_enabled = true;
1343                 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1344         }
1345
1346         adapter->workqueue =
1347                 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1348                                 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1349         if (!adapter->workqueue)
1350                 goto err_kmalloc;
1351
1352         INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1353
1354         if (adapter->rx_work_enabled) {
1355                 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1356                                                         WQ_HIGHPRI |
1357                                                         WQ_MEM_RECLAIM |
1358                                                         WQ_UNBOUND, 1);
1359                 if (!adapter->rx_workqueue)
1360                         goto err_kmalloc;
1361
1362                 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1363         }
1364
1365         /* Register the device. Fill up the private data structure with relevant
1366            information from the card. */
1367         if (adapter->if_ops.register_dev(adapter)) {
1368                 pr_err("%s: failed to register mwifiex device\n", __func__);
1369                 goto err_registerdev;
1370         }
1371
1372         if (mwifiex_init_hw_fw(adapter)) {
1373                 pr_err("%s: firmware init failed\n", __func__);
1374                 goto err_init_fw;
1375         }
1376
1377         return 0;
1378
1379 err_init_fw:
1380         pr_debug("info: %s: unregister device\n", __func__);
1381         if (adapter->if_ops.unregister_dev)
1382                 adapter->if_ops.unregister_dev(adapter);
1383         if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1384                 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1385                 adapter->init_wait_q_woken = false;
1386
1387                 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
1388                         wait_event_interruptible(adapter->init_wait_q,
1389                                                  adapter->init_wait_q_woken);
1390         }
1391 err_registerdev:
1392         adapter->surprise_removed = true;
1393         mwifiex_terminate_workqueue(adapter);
1394 err_kmalloc:
1395         mwifiex_free_adapter(adapter);
1396
1397 err_init_sw:
1398         up(sem);
1399
1400 exit_sem_err:
1401         return -1;
1402 }
1403 EXPORT_SYMBOL_GPL(mwifiex_add_card);
1404
1405 /*
1406  * This function removes the card.
1407  *
1408  * This function follows the following major steps to remove the device -
1409  *      - Stop data traffic
1410  *      - Shutdown firmware
1411  *      - Remove the logical interfaces
1412  *      - Terminate the work queue
1413  *      - Unregister the device
1414  *      - Free the adapter structure
1415  */
1416 int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1417 {
1418         struct mwifiex_private *priv = NULL;
1419         int i;
1420
1421         if (down_interruptible(sem))
1422                 goto exit_sem_err;
1423
1424         if (!adapter)
1425                 goto exit_remove;
1426
1427         /* We can no longer handle interrupts once we start doing the teardown
1428          * below. */
1429         if (adapter->if_ops.disable_int)
1430                 adapter->if_ops.disable_int(adapter);
1431
1432         adapter->surprise_removed = true;
1433
1434         mwifiex_terminate_workqueue(adapter);
1435
1436         /* Stop data */
1437         for (i = 0; i < adapter->priv_num; i++) {
1438                 priv = adapter->priv[i];
1439                 if (priv && priv->netdev) {
1440                         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1441                         if (netif_carrier_ok(priv->netdev))
1442                                 netif_carrier_off(priv->netdev);
1443                 }
1444         }
1445
1446         mwifiex_dbg(adapter, CMD,
1447                     "cmd: calling mwifiex_shutdown_drv...\n");
1448         adapter->init_wait_q_woken = false;
1449
1450         if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
1451                 wait_event_interruptible(adapter->init_wait_q,
1452                                          adapter->init_wait_q_woken);
1453         mwifiex_dbg(adapter, CMD,
1454                     "cmd: mwifiex_shutdown_drv done\n");
1455         if (atomic_read(&adapter->rx_pending) ||
1456             atomic_read(&adapter->tx_pending) ||
1457             atomic_read(&adapter->cmd_pending)) {
1458                 mwifiex_dbg(adapter, ERROR,
1459                             "rx_pending=%d, tx_pending=%d,\t"
1460                             "cmd_pending=%d\n",
1461                             atomic_read(&adapter->rx_pending),
1462                             atomic_read(&adapter->tx_pending),
1463                             atomic_read(&adapter->cmd_pending));
1464         }
1465
1466         for (i = 0; i < adapter->priv_num; i++) {
1467                 priv = adapter->priv[i];
1468
1469                 if (!priv)
1470                         continue;
1471
1472                 rtnl_lock();
1473                 if (priv->netdev &&
1474                     priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1475                         mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1476                 rtnl_unlock();
1477         }
1478
1479         wiphy_unregister(adapter->wiphy);
1480         wiphy_free(adapter->wiphy);
1481
1482         /* Unregister device */
1483         mwifiex_dbg(adapter, INFO,
1484                     "info: unregister device\n");
1485         if (adapter->if_ops.unregister_dev)
1486                 adapter->if_ops.unregister_dev(adapter);
1487         /* Free adapter structure */
1488         mwifiex_dbg(adapter, INFO,
1489                     "info: free adapter\n");
1490         mwifiex_free_adapter(adapter);
1491
1492 exit_remove:
1493         up(sem);
1494 exit_sem_err:
1495         return 0;
1496 }
1497 EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1498
1499 void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1500                   const char *fmt, ...)
1501 {
1502         struct va_format vaf;
1503         va_list args;
1504
1505         if (!adapter->dev || !(adapter->debug_mask & mask))
1506                 return;
1507
1508         va_start(args, fmt);
1509
1510         vaf.fmt = fmt;
1511         vaf.va = &args;
1512
1513         dev_info(adapter->dev, "%pV", &vaf);
1514
1515         va_end(args);
1516 }
1517 EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1518
1519 /*
1520  * This function initializes the module.
1521  *
1522  * The debug FS is also initialized if configured.
1523  */
1524 static int
1525 mwifiex_init_module(void)
1526 {
1527 #ifdef CONFIG_DEBUG_FS
1528         mwifiex_debugfs_init();
1529 #endif
1530         return 0;
1531 }
1532
1533 /*
1534  * This function cleans up the module.
1535  *
1536  * The debug FS is removed if available.
1537  */
1538 static void
1539 mwifiex_cleanup_module(void)
1540 {
1541 #ifdef CONFIG_DEBUG_FS
1542         mwifiex_debugfs_remove();
1543 #endif
1544 }
1545
1546 module_init(mwifiex_init_module);
1547 module_exit(mwifiex_cleanup_module);
1548
1549 MODULE_AUTHOR("Marvell International Ltd.");
1550 MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1551 MODULE_VERSION(VERSION);
1552 MODULE_LICENSE("GPL v2");