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