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