2 * Marvell Wireless LAN device driver: HW/FW Initialization
4 * Copyright (C) 2011, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
29 * This function adds a BSS priority table to the table list.
31 * The function allocates a new BSS priority table node and adds it to
32 * the end of BSS priority table list, kept in driver memory.
34 static int mwifiex_add_bss_prio_tbl(struct mwifiex_private *priv)
36 struct mwifiex_adapter *adapter = priv->adapter;
37 struct mwifiex_bss_prio_node *bss_prio;
38 struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
41 bss_prio = kzalloc(sizeof(struct mwifiex_bss_prio_node), GFP_KERNEL);
45 bss_prio->priv = priv;
46 INIT_LIST_HEAD(&bss_prio->list);
48 spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
49 list_add_tail(&bss_prio->list, &tbl[priv->bss_priority].bss_prio_head);
50 spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
56 * This function initializes the private structure and sets default
57 * values to the members.
59 * Additionally, it also initializes all the locks and sets up all the
62 int mwifiex_init_priv(struct mwifiex_private *priv)
66 priv->media_connected = false;
67 memset(priv->curr_addr, 0xff, ETH_ALEN);
69 priv->pkt_tx_ctrl = 0;
70 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
71 priv->data_rate = 0; /* Initially indicate the rate as auto */
72 priv->is_data_rate_auto = true;
73 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
74 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
76 priv->sec_info.wep_enabled = 0;
77 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
78 priv->sec_info.encryption_mode = 0;
79 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
80 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
81 priv->wep_key_curr_index = 0;
82 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
83 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
85 priv->beacon_period = 100; /* beacon interval */ ;
86 priv->attempted_bss_desc = NULL;
87 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
88 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
90 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
91 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
92 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
93 priv->assoc_rsp_size = 0;
94 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
95 priv->atim_window = 0;
96 priv->adhoc_state = ADHOC_IDLE;
97 priv->tx_power_level = 0;
98 priv->max_tx_power_level = 0;
99 priv->min_tx_power_level = 0;
101 priv->rxpd_htinfo = 0;
103 priv->rate_bitmap = 0;
104 priv->data_rssi_last = 0;
105 priv->data_rssi_avg = 0;
106 priv->data_nf_avg = 0;
107 priv->data_nf_last = 0;
108 priv->bcn_rssi_last = 0;
109 priv->bcn_rssi_avg = 0;
110 priv->bcn_nf_avg = 0;
111 priv->bcn_nf_last = 0;
112 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
113 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
114 priv->wpa_ie_len = 0;
115 priv->wpa_is_gtk_set = false;
117 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
118 priv->assoc_tlv_buf_len = 0;
119 memset(&priv->wps, 0, sizeof(priv->wps));
120 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
121 priv->gen_ie_buf_len = 0;
122 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
124 priv->wmm_required = true;
125 priv->wmm_enabled = false;
126 priv->wmm_qosinfo = 0;
127 priv->curr_bcn_buf = NULL;
128 priv->curr_bcn_size = 0;
130 priv->wps_ie_len = 0;
131 priv->ap_11n_enabled = 0;
132 memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
134 priv->scan_block = false;
137 priv->csa_expire_time = 0;
138 priv->del_list_idx = 0;
139 priv->hs2_enabled = false;
140 memcpy(priv->tos_to_tid_inv, tos_to_tid_inv, MAX_NUM_TID);
142 return mwifiex_add_bss_prio_tbl(priv);
146 * This function allocates buffers for members of the adapter
149 * The memory allocated includes scan table, command buffers, and
150 * sleep confirm command buffer. In addition, the queues are
153 static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
157 /* Allocate command buffer */
158 ret = mwifiex_alloc_cmd_buffer(adapter);
160 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
166 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
169 if (!adapter->sleep_cfm) {
170 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
171 " cmd buffer\n", __func__);
174 skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
180 * This function initializes the adapter structure and sets default
181 * values to the members of adapter.
183 * This also initializes the WMM related parameters in the driver private
186 static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
188 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
190 skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
192 adapter->cmd_sent = false;
194 if (adapter->iface_type == MWIFIEX_SDIO)
195 adapter->data_sent = true;
197 adapter->data_sent = false;
199 adapter->cmd_resp_received = false;
200 adapter->event_received = false;
201 adapter->data_received = false;
203 adapter->surprise_removed = false;
205 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
207 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
208 adapter->ps_state = PS_STATE_AWAKE;
209 adapter->need_to_wakeup = false;
211 adapter->scan_mode = HostCmd_BSS_MODE_ANY;
212 adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
213 adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
214 adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
216 adapter->scan_probes = 1;
218 adapter->multiple_dtim = 1;
220 adapter->local_listen_interval = 0; /* default value in firmware
223 adapter->is_deep_sleep = false;
225 adapter->delay_null_pkt = false;
226 adapter->delay_to_ps = 1000;
227 adapter->enhanced_ps_mode = PS_MODE_AUTO;
229 adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
231 adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
233 adapter->pm_wakeup_card_req = false;
235 adapter->pm_wakeup_fw_try = false;
237 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
239 adapter->is_hs_configured = false;
240 adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
241 adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
242 adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
243 adapter->hs_activated = false;
245 memset(adapter->event_body, 0, sizeof(adapter->event_body));
246 adapter->hw_dot_11n_dev_cap = 0;
247 adapter->hw_dev_mcs_support = 0;
248 adapter->sec_chan_offset = 0;
249 adapter->adhoc_11n_enabled = false;
251 mwifiex_wmm_init(adapter);
253 if (adapter->sleep_cfm) {
254 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
255 adapter->sleep_cfm->data;
256 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
257 sleep_cfm_buf->command =
258 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
259 sleep_cfm_buf->size =
260 cpu_to_le16(adapter->sleep_cfm->len);
261 sleep_cfm_buf->result = 0;
262 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
263 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
265 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
266 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
267 adapter->tx_lock_flag = false;
268 adapter->null_pkt_interval = 0;
269 adapter->fw_bands = 0;
270 adapter->config_bands = 0;
271 adapter->adhoc_start_band = 0;
272 adapter->scan_channels = NULL;
273 adapter->fw_release_number = 0;
274 adapter->fw_cap_info = 0;
275 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
276 adapter->event_cause = 0;
277 adapter->region_code = 0;
278 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
279 adapter->adhoc_awake_period = 0;
280 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
281 adapter->arp_filter_size = 0;
282 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
283 adapter->empty_tx_q_cnt = 0;
284 adapter->ext_scan = true;
285 adapter->fw_key_api_major_ver = 0;
286 adapter->fw_key_api_minor_ver = 0;
290 * This function sets trans_start per tx_queue
292 void mwifiex_set_trans_start(struct net_device *dev)
296 for (i = 0; i < dev->num_tx_queues; i++)
297 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
299 dev->trans_start = jiffies;
303 * This function wakes up all queues in net_device
305 void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
306 struct mwifiex_adapter *adapter)
308 unsigned long dev_queue_flags;
311 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
313 for (i = 0; i < netdev->num_tx_queues; i++) {
314 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
316 if (netif_tx_queue_stopped(txq))
317 netif_tx_wake_queue(txq);
320 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
324 * This function stops all queues in net_device
326 void mwifiex_stop_net_dev_queue(struct net_device *netdev,
327 struct mwifiex_adapter *adapter)
329 unsigned long dev_queue_flags;
332 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
334 for (i = 0; i < netdev->num_tx_queues; i++) {
335 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
337 if (!netif_tx_queue_stopped(txq))
338 netif_tx_stop_queue(txq);
341 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
345 * This function releases the lock variables and frees the locks and
348 static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
350 struct mwifiex_private *priv;
354 list_del(&adapter->cmd_free_q);
355 list_del(&adapter->cmd_pending_q);
356 list_del(&adapter->scan_pending_q);
358 for (i = 0; i < adapter->priv_num; i++)
359 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
361 for (i = 0; i < adapter->priv_num; i++) {
362 if (adapter->priv[i]) {
363 priv = adapter->priv[i];
364 for (j = 0; j < MAX_NUM_TID; ++j)
365 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
366 list_del(&priv->tx_ba_stream_tbl_ptr);
367 list_del(&priv->rx_reorder_tbl_ptr);
368 list_del(&priv->sta_list);
374 * This function performs cleanup for adapter structure.
376 * The cleanup is done recursively, by canceling all pending
377 * commands, freeing the member buffers previously allocated
378 * (command buffers, scan table buffer, sleep confirm command
379 * buffer), stopping the timers and calling the cleanup routines
380 * for every interface.
383 mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
386 pr_err("%s: adapter is NULL\n", __func__);
390 mwifiex_cancel_all_pending_cmd(adapter);
392 /* Free lock variables */
393 mwifiex_free_lock_list(adapter);
395 /* Free command buffer */
396 dev_dbg(adapter->dev, "info: free cmd buffer\n");
397 mwifiex_free_cmd_buffer(adapter);
399 dev_dbg(adapter->dev, "info: free scan table\n");
401 if (adapter->sleep_cfm)
402 dev_kfree_skb_any(adapter->sleep_cfm);
406 * This function intializes the lock variables and
409 int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
411 struct mwifiex_private *priv;
414 spin_lock_init(&adapter->mwifiex_lock);
415 spin_lock_init(&adapter->int_lock);
416 spin_lock_init(&adapter->main_proc_lock);
417 spin_lock_init(&adapter->mwifiex_cmd_lock);
418 spin_lock_init(&adapter->queue_lock);
419 for (i = 0; i < adapter->priv_num; i++) {
420 if (adapter->priv[i]) {
421 priv = adapter->priv[i];
422 spin_lock_init(&priv->rx_pkt_lock);
423 spin_lock_init(&priv->wmm.ra_list_spinlock);
424 spin_lock_init(&priv->curr_bcn_buf_lock);
425 spin_lock_init(&priv->sta_list_spinlock);
429 /* Initialize cmd_free_q */
430 INIT_LIST_HEAD(&adapter->cmd_free_q);
431 /* Initialize cmd_pending_q */
432 INIT_LIST_HEAD(&adapter->cmd_pending_q);
433 /* Initialize scan_pending_q */
434 INIT_LIST_HEAD(&adapter->scan_pending_q);
436 spin_lock_init(&adapter->cmd_free_q_lock);
437 spin_lock_init(&adapter->cmd_pending_q_lock);
438 spin_lock_init(&adapter->scan_pending_q_lock);
440 skb_queue_head_init(&adapter->usb_rx_data_q);
442 for (i = 0; i < adapter->priv_num; ++i) {
443 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
444 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
447 for (i = 0; i < adapter->priv_num; i++) {
448 if (!adapter->priv[i])
450 priv = adapter->priv[i];
451 for (j = 0; j < MAX_NUM_TID; ++j)
452 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
453 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
454 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
455 INIT_LIST_HEAD(&priv->sta_list);
456 skb_queue_head_init(&priv->tdls_txq);
458 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
459 spin_lock_init(&priv->rx_reorder_tbl_lock);
466 * This function initializes the firmware.
468 * The following operations are performed sequentially -
469 * - Allocate adapter structure
470 * - Initialize the adapter structure
471 * - Initialize the private structure
472 * - Add BSS priority tables to the adapter structure
473 * - For each interface, send the init commands to firmware
474 * - Send the first command in command pending queue, if available
476 int mwifiex_init_fw(struct mwifiex_adapter *adapter)
479 struct mwifiex_private *priv;
480 u8 i, first_sta = true;
481 int is_cmd_pend_q_empty;
484 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
486 /* Allocate memory for member of adapter structure */
487 ret = mwifiex_allocate_adapter(adapter);
491 /* Initialize adapter structure */
492 mwifiex_init_adapter(adapter);
494 for (i = 0; i < adapter->priv_num; i++) {
495 if (adapter->priv[i]) {
496 priv = adapter->priv[i];
498 /* Initialize private structure */
499 ret = mwifiex_init_priv(priv);
505 if (adapter->if_ops.init_fw_port) {
506 if (adapter->if_ops.init_fw_port(adapter))
510 for (i = 0; i < adapter->priv_num; i++) {
511 if (adapter->priv[i]) {
512 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
520 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
521 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
522 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
523 if (!is_cmd_pend_q_empty) {
524 /* Send the first command in queue and return */
525 if (mwifiex_main_process(adapter) != -1)
528 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
535 * This function deletes the BSS priority tables.
537 * The function traverses through all the allocated BSS priority nodes
538 * in every BSS priority table and frees them.
540 static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
543 struct mwifiex_adapter *adapter = priv->adapter;
544 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
545 struct list_head *head;
546 spinlock_t *lock; /* bss priority lock */
549 for (i = 0; i < adapter->priv_num; ++i) {
550 head = &adapter->bss_prio_tbl[i].bss_prio_head;
551 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
552 dev_dbg(adapter->dev, "info: delete BSS priority table,"
553 " bss_type = %d, bss_num = %d, i = %d,"
555 priv->bss_type, priv->bss_num, i, head);
558 spin_lock_irqsave(lock, flags);
559 if (list_empty(head)) {
560 spin_unlock_irqrestore(lock, flags);
563 list_for_each_entry_safe(bssprio_node, tmp_node, head,
565 if (bssprio_node->priv == priv) {
566 dev_dbg(adapter->dev, "info: Delete "
567 "node %p, next = %p\n",
568 bssprio_node, tmp_node);
569 list_del(&bssprio_node->list);
573 spin_unlock_irqrestore(lock, flags);
579 * This function frees the private structure, including cleans
580 * up the TX and RX queues and frees the BSS priority tables.
582 void mwifiex_free_priv(struct mwifiex_private *priv)
584 mwifiex_clean_txrx(priv);
585 mwifiex_delete_bss_prio_tbl(priv);
586 mwifiex_free_curr_bcn(priv);
590 * This function is used to shutdown the driver.
592 * The following operations are performed sequentially -
593 * - Check if already shut down
594 * - Make sure the main process has stopped
595 * - Clean up the Tx and Rx queues
596 * - Delete BSS priority tables
598 * - Notify completion
601 mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
603 int ret = -EINPROGRESS;
604 struct mwifiex_private *priv;
608 /* mwifiex already shutdown */
609 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
612 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
613 /* wait for mwifiex_process to complete */
614 if (adapter->mwifiex_processing) {
615 dev_warn(adapter->dev, "main process is still running\n");
619 /* cancel current command */
620 if (adapter->curr_cmd) {
621 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
622 del_timer_sync(&adapter->cmd_timer);
623 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
624 adapter->curr_cmd = NULL;
627 /* shut down mwifiex */
628 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
630 /* Clean up Tx/Rx queues and delete BSS priority table */
631 for (i = 0; i < adapter->priv_num; i++) {
632 if (adapter->priv[i]) {
633 priv = adapter->priv[i];
635 mwifiex_clean_txrx(priv);
636 mwifiex_delete_bss_prio_tbl(priv);
640 spin_lock(&adapter->mwifiex_lock);
642 if (adapter->if_ops.data_complete) {
643 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
644 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
646 priv = adapter->priv[rx_info->bss_num];
648 priv->stats.rx_dropped++;
650 dev_kfree_skb_any(skb);
651 adapter->if_ops.data_complete(adapter);
655 mwifiex_adapter_cleanup(adapter);
657 spin_unlock(&adapter->mwifiex_lock);
659 /* Notify completion */
660 ret = mwifiex_shutdown_fw_complete(adapter);
666 * This function downloads the firmware to the card.
668 * The actual download is preceded by two sanity checks -
669 * - Check if firmware is already running
670 * - Check if the interface is the winner to download the firmware
672 * ...and followed by another -
673 * - Check if the firmware is downloaded successfully
675 * After download is successfully completed, the host interrupts are enabled.
677 int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
678 struct mwifiex_fw_image *pmfw)
683 if (adapter->if_ops.check_fw_status) {
686 /* check if firmware is already running */
687 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
689 dev_notice(adapter->dev,
690 "WLAN FW already running! Skip FW dnld\n");
694 poll_num = MAX_FIRMWARE_POLL_TRIES;
696 /* check if we are the winner for downloading FW */
697 if (!adapter->winner) {
698 dev_notice(adapter->dev,
699 "FW already running! Skip FW dnld\n");
705 /* Download firmware with helper */
706 ret = adapter->if_ops.prog_fw(adapter, pmfw);
708 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
714 /* Check if the firmware is downloaded successfully or not */
715 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
717 dev_err(adapter->dev, "FW failed to be active in time\n");