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
;
40 bss_prio
= kzalloc(sizeof(struct mwifiex_bss_prio_node
), GFP_KERNEL
);
42 dev_err(adapter
->dev
, "%s: failed to alloc bss_prio\n",
47 bss_prio
->priv
= priv
;
48 INIT_LIST_HEAD(&bss_prio
->list
);
49 if (!adapter
->bss_prio_tbl
[priv
->bss_priority
].bss_prio_cur
)
50 adapter
->bss_prio_tbl
[priv
->bss_priority
].bss_prio_cur
=
53 spin_lock_irqsave(&adapter
->bss_prio_tbl
[priv
->bss_priority
]
54 .bss_prio_lock
, flags
);
55 list_add_tail(&bss_prio
->list
,
56 &adapter
->bss_prio_tbl
[priv
->bss_priority
]
58 spin_unlock_irqrestore(&adapter
->bss_prio_tbl
[priv
->bss_priority
]
59 .bss_prio_lock
, flags
);
65 * This function initializes the private structure and sets default
66 * values to the members.
68 * Additionally, it also initializes all the locks and sets up all the
71 static int mwifiex_init_priv(struct mwifiex_private
*priv
)
76 priv
->media_connected
= false;
77 memset(priv
->curr_addr
, 0xff, ETH_ALEN
);
79 priv
->pkt_tx_ctrl
= 0;
80 priv
->bss_mode
= NL80211_IFTYPE_STATION
;
81 priv
->data_rate
= 0; /* Initially indicate the rate as auto */
82 priv
->is_data_rate_auto
= true;
83 priv
->bcn_avg_factor
= DEFAULT_BCN_AVG_FACTOR
;
84 priv
->data_avg_factor
= DEFAULT_DATA_AVG_FACTOR
;
86 priv
->sec_info
.wep_status
= MWIFIEX_802_11_WEP_DISABLED
;
87 priv
->sec_info
.authentication_mode
= NL80211_AUTHTYPE_OPEN_SYSTEM
;
88 priv
->sec_info
.encryption_mode
= 0;
89 for (i
= 0; i
< ARRAY_SIZE(priv
->wep_key
); i
++)
90 memset(&priv
->wep_key
[i
], 0, sizeof(struct mwifiex_wep_key
));
91 priv
->wep_key_curr_index
= 0;
92 priv
->curr_pkt_filter
= HostCmd_ACT_MAC_RX_ON
| HostCmd_ACT_MAC_TX_ON
|
93 HostCmd_ACT_MAC_ETHERNETII_ENABLE
;
95 priv
->beacon_period
= 100; /* beacon interval */ ;
96 priv
->attempted_bss_desc
= NULL
;
97 memset(&priv
->curr_bss_params
, 0, sizeof(priv
->curr_bss_params
));
98 priv
->listen_interval
= MWIFIEX_DEFAULT_LISTEN_INTERVAL
;
100 memset(&priv
->prev_ssid
, 0, sizeof(priv
->prev_ssid
));
101 memset(&priv
->prev_bssid
, 0, sizeof(priv
->prev_bssid
));
102 memset(&priv
->assoc_rsp_buf
, 0, sizeof(priv
->assoc_rsp_buf
));
103 priv
->assoc_rsp_size
= 0;
104 priv
->adhoc_channel
= DEFAULT_AD_HOC_CHANNEL
;
105 priv
->atim_window
= 0;
106 priv
->adhoc_state
= ADHOC_IDLE
;
107 priv
->tx_power_level
= 0;
108 priv
->max_tx_power_level
= 0;
109 priv
->min_tx_power_level
= 0;
111 priv
->rxpd_htinfo
= 0;
113 priv
->rate_bitmap
= 0;
114 priv
->data_rssi_last
= 0;
115 priv
->data_rssi_avg
= 0;
116 priv
->data_nf_avg
= 0;
117 priv
->data_nf_last
= 0;
118 priv
->bcn_rssi_last
= 0;
119 priv
->bcn_rssi_avg
= 0;
120 priv
->bcn_nf_avg
= 0;
121 priv
->bcn_nf_last
= 0;
122 memset(&priv
->wpa_ie
, 0, sizeof(priv
->wpa_ie
));
123 memset(&priv
->aes_key
, 0, sizeof(priv
->aes_key
));
124 priv
->wpa_ie_len
= 0;
125 priv
->wpa_is_gtk_set
= false;
127 memset(&priv
->assoc_tlv_buf
, 0, sizeof(priv
->assoc_tlv_buf
));
128 priv
->assoc_tlv_buf_len
= 0;
129 memset(&priv
->wps
, 0, sizeof(priv
->wps
));
130 memset(&priv
->gen_ie_buf
, 0, sizeof(priv
->gen_ie_buf
));
131 priv
->gen_ie_buf_len
= 0;
132 memset(priv
->vs_ie
, 0, sizeof(priv
->vs_ie
));
134 priv
->wmm_required
= true;
135 priv
->wmm_enabled
= false;
136 priv
->wmm_qosinfo
= 0;
137 priv
->curr_bcn_buf
= NULL
;
138 priv
->curr_bcn_size
= 0;
140 priv
->scan_block
= false;
142 ret
= mwifiex_add_bss_prio_tbl(priv
);
148 * This function allocates buffers for members of the adapter
151 * The memory allocated includes scan table, command buffers, and
152 * sleep confirm command buffer. In addition, the queues are
155 static int mwifiex_allocate_adapter(struct mwifiex_adapter
*adapter
)
159 struct mwifiex_bssdescriptor
*temp_scan_table
;
161 /* Allocate buffer to store the BSSID list */
162 buf_size
= sizeof(struct mwifiex_bssdescriptor
) * IW_MAX_AP
;
163 temp_scan_table
= kzalloc(buf_size
, GFP_KERNEL
);
164 if (!temp_scan_table
) {
165 dev_err(adapter
->dev
, "%s: failed to alloc temp_scan_table\n",
170 adapter
->scan_table
= temp_scan_table
;
172 /* Allocate command buffer */
173 ret
= mwifiex_alloc_cmd_buffer(adapter
);
175 dev_err(adapter
->dev
, "%s: failed to alloc cmd buffer\n",
181 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm_buffer
)
184 if (!adapter
->sleep_cfm
) {
185 dev_err(adapter
->dev
, "%s: failed to alloc sleep cfm"
186 " cmd buffer\n", __func__
);
189 skb_reserve(adapter
->sleep_cfm
, INTF_HEADER_LEN
);
195 * This function initializes the adapter structure and sets default
196 * values to the members of adapter.
198 * This also initializes the WMM related parameters in the driver private
201 static void mwifiex_init_adapter(struct mwifiex_adapter
*adapter
)
203 struct mwifiex_opt_sleep_confirm_buffer
*sleep_cfm_buf
= NULL
;
205 skb_put(adapter
->sleep_cfm
, sizeof(sleep_cfm_buf
->ps_cfm_sleep
));
206 sleep_cfm_buf
= (struct mwifiex_opt_sleep_confirm_buffer
*)
207 (adapter
->sleep_cfm
->data
);
209 adapter
->cmd_sent
= false;
210 adapter
->data_sent
= true;
211 adapter
->cmd_resp_received
= false;
212 adapter
->event_received
= false;
213 adapter
->data_received
= false;
215 adapter
->surprise_removed
= false;
217 adapter
->hw_status
= MWIFIEX_HW_STATUS_INITIALIZING
;
219 adapter
->ps_mode
= MWIFIEX_802_11_POWER_MODE_CAM
;
220 adapter
->ps_state
= PS_STATE_AWAKE
;
221 adapter
->need_to_wakeup
= false;
223 adapter
->scan_mode
= HostCmd_BSS_MODE_ANY
;
224 adapter
->specific_scan_time
= MWIFIEX_SPECIFIC_SCAN_CHAN_TIME
;
225 adapter
->active_scan_time
= MWIFIEX_ACTIVE_SCAN_CHAN_TIME
;
226 adapter
->passive_scan_time
= MWIFIEX_PASSIVE_SCAN_CHAN_TIME
;
228 adapter
->num_in_scan_table
= 0;
229 memset(adapter
->scan_table
, 0,
230 (sizeof(struct mwifiex_bssdescriptor
) * IW_MAX_AP
));
231 adapter
->scan_probes
= 1;
233 memset(adapter
->bcn_buf
, 0, sizeof(adapter
->bcn_buf
));
234 adapter
->bcn_buf_end
= adapter
->bcn_buf
;
236 adapter
->multiple_dtim
= 1;
238 adapter
->local_listen_interval
= 0; /* default value in firmware
241 adapter
->is_deep_sleep
= false;
243 adapter
->delay_null_pkt
= false;
244 adapter
->delay_to_ps
= 1000;
245 adapter
->enhanced_ps_mode
= PS_MODE_AUTO
;
247 adapter
->gen_null_pkt
= false; /* Disable NULL Pkg generation by
249 adapter
->pps_uapsd_mode
= false; /* Disable pps/uapsd mode by
251 adapter
->pm_wakeup_card_req
= false;
253 adapter
->pm_wakeup_fw_try
= false;
255 adapter
->max_tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
256 adapter
->tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
257 adapter
->curr_tx_buf_size
= MWIFIEX_TX_DATA_BUF_SIZE_2K
;
259 adapter
->is_hs_configured
= false;
260 adapter
->hs_cfg
.conditions
= cpu_to_le32(HOST_SLEEP_CFG_COND_DEF
);
261 adapter
->hs_cfg
.gpio
= HOST_SLEEP_CFG_GPIO_DEF
;
262 adapter
->hs_cfg
.gap
= HOST_SLEEP_CFG_GAP_DEF
;
263 adapter
->hs_activated
= false;
265 memset(adapter
->event_body
, 0, sizeof(adapter
->event_body
));
266 adapter
->hw_dot_11n_dev_cap
= 0;
267 adapter
->hw_dev_mcs_support
= 0;
268 adapter
->chan_offset
= 0;
269 adapter
->adhoc_11n_enabled
= false;
271 mwifiex_wmm_init(adapter
);
273 if (adapter
->sleep_cfm
) {
274 memset(&sleep_cfm_buf
->ps_cfm_sleep
, 0,
275 adapter
->sleep_cfm
->len
);
276 sleep_cfm_buf
->ps_cfm_sleep
.command
=
277 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH
);
278 sleep_cfm_buf
->ps_cfm_sleep
.size
=
279 cpu_to_le16(adapter
->sleep_cfm
->len
);
280 sleep_cfm_buf
->ps_cfm_sleep
.result
= 0;
281 sleep_cfm_buf
->ps_cfm_sleep
.action
= cpu_to_le16(SLEEP_CONFIRM
);
282 sleep_cfm_buf
->ps_cfm_sleep
.resp_ctrl
=
283 cpu_to_le16(RESP_NEEDED
);
285 memset(&adapter
->sleep_params
, 0, sizeof(adapter
->sleep_params
));
286 memset(&adapter
->sleep_period
, 0, sizeof(adapter
->sleep_period
));
287 adapter
->tx_lock_flag
= false;
288 adapter
->null_pkt_interval
= 0;
289 adapter
->fw_bands
= 0;
290 adapter
->config_bands
= 0;
291 adapter
->adhoc_start_band
= 0;
292 adapter
->scan_channels
= NULL
;
293 adapter
->fw_release_number
= 0;
294 adapter
->fw_cap_info
= 0;
295 memset(&adapter
->upld_buf
, 0, sizeof(adapter
->upld_buf
));
296 adapter
->event_cause
= 0;
297 adapter
->region_code
= 0;
298 adapter
->bcn_miss_time_out
= DEFAULT_BCN_MISS_TIMEOUT
;
299 adapter
->adhoc_awake_period
= 0;
300 memset(&adapter
->arp_filter
, 0, sizeof(adapter
->arp_filter
));
301 adapter
->arp_filter_size
= 0;
307 * This function frees the adapter structure.
309 * The freeing operation is done recursively, by canceling all
310 * pending commands, freeing the member buffers previously
311 * allocated (command buffers, scan table buffer, sleep confirm
312 * command buffer), stopping the timers and calling the cleanup
313 * routines for every interface, before the actual adapter
314 * structure is freed.
317 mwifiex_free_adapter(struct mwifiex_adapter
*adapter
)
320 pr_err("%s: adapter is NULL\n", __func__
);
324 mwifiex_cancel_all_pending_cmd(adapter
);
326 /* Free lock variables */
327 mwifiex_free_lock_list(adapter
);
329 /* Free command buffer */
330 dev_dbg(adapter
->dev
, "info: free cmd buffer\n");
331 mwifiex_free_cmd_buffer(adapter
);
333 del_timer(&adapter
->cmd_timer
);
335 dev_dbg(adapter
->dev
, "info: free scan table\n");
336 kfree(adapter
->scan_table
);
337 adapter
->scan_table
= NULL
;
339 adapter
->if_ops
.cleanup_if(adapter
);
341 dev_kfree_skb_any(adapter
->sleep_cfm
);
347 * This function intializes the lock variables and
350 int mwifiex_init_lock_list(struct mwifiex_adapter
*adapter
)
352 struct mwifiex_private
*priv
= NULL
;
356 spin_lock_init(&adapter
->mwifiex_lock
);
357 spin_lock_init(&adapter
->int_lock
);
358 spin_lock_init(&adapter
->main_proc_lock
);
359 spin_lock_init(&adapter
->mwifiex_cmd_lock
);
360 for (i
= 0; i
< adapter
->priv_num
; i
++) {
361 if (adapter
->priv
[i
]) {
362 priv
= adapter
->priv
[i
];
363 spin_lock_init(&priv
->rx_pkt_lock
);
364 spin_lock_init(&priv
->wmm
.ra_list_spinlock
);
365 spin_lock_init(&priv
->curr_bcn_buf_lock
);
369 /* Initialize cmd_free_q */
370 INIT_LIST_HEAD(&adapter
->cmd_free_q
);
371 /* Initialize cmd_pending_q */
372 INIT_LIST_HEAD(&adapter
->cmd_pending_q
);
373 /* Initialize scan_pending_q */
374 INIT_LIST_HEAD(&adapter
->scan_pending_q
);
376 spin_lock_init(&adapter
->cmd_free_q_lock
);
377 spin_lock_init(&adapter
->cmd_pending_q_lock
);
378 spin_lock_init(&adapter
->scan_pending_q_lock
);
380 for (i
= 0; i
< adapter
->priv_num
; ++i
) {
381 INIT_LIST_HEAD(&adapter
->bss_prio_tbl
[i
].bss_prio_head
);
382 adapter
->bss_prio_tbl
[i
].bss_prio_cur
= NULL
;
383 spin_lock_init(&adapter
->bss_prio_tbl
[i
].bss_prio_lock
);
386 for (i
= 0; i
< adapter
->priv_num
; i
++) {
387 if (!adapter
->priv
[i
])
389 priv
= adapter
->priv
[i
];
390 for (j
= 0; j
< MAX_NUM_TID
; ++j
) {
391 INIT_LIST_HEAD(&priv
->wmm
.tid_tbl_ptr
[j
].ra_list
);
392 spin_lock_init(&priv
->wmm
.tid_tbl_ptr
[j
].tid_tbl_lock
);
394 INIT_LIST_HEAD(&priv
->tx_ba_stream_tbl_ptr
);
395 INIT_LIST_HEAD(&priv
->rx_reorder_tbl_ptr
);
397 spin_lock_init(&priv
->tx_ba_stream_tbl_lock
);
398 spin_lock_init(&priv
->rx_reorder_tbl_lock
);
405 * This function releases the lock variables and frees the locks and
408 void mwifiex_free_lock_list(struct mwifiex_adapter
*adapter
)
410 struct mwifiex_private
*priv
= NULL
;
415 list_del(&adapter
->cmd_free_q
);
416 list_del(&adapter
->cmd_pending_q
);
417 list_del(&adapter
->scan_pending_q
);
419 for (i
= 0; i
< adapter
->priv_num
; i
++)
420 list_del(&adapter
->bss_prio_tbl
[i
].bss_prio_head
);
422 for (i
= 0; i
< adapter
->priv_num
; i
++) {
423 if (adapter
->priv
[i
]) {
424 priv
= adapter
->priv
[i
];
425 for (j
= 0; j
< MAX_NUM_TID
; ++j
)
426 list_del(&priv
->wmm
.tid_tbl_ptr
[j
].ra_list
);
427 list_del(&priv
->tx_ba_stream_tbl_ptr
);
428 list_del(&priv
->rx_reorder_tbl_ptr
);
436 * This function initializes the firmware.
438 * The following operations are performed sequentially -
439 * - Allocate adapter structure
440 * - Initialize the adapter structure
441 * - Initialize the private structure
442 * - Add BSS priority tables to the adapter structure
443 * - For each interface, send the init commands to firmware
444 * - Send the first command in command pending queue, if available
446 int mwifiex_init_fw(struct mwifiex_adapter
*adapter
)
449 struct mwifiex_private
*priv
= NULL
;
452 int is_cmd_pend_q_empty
;
455 adapter
->hw_status
= MWIFIEX_HW_STATUS_INITIALIZING
;
457 /* Allocate memory for member of adapter structure */
458 ret
= mwifiex_allocate_adapter(adapter
);
462 /* Initialize adapter structure */
463 mwifiex_init_adapter(adapter
);
465 for (i
= 0; i
< adapter
->priv_num
; i
++) {
466 if (adapter
->priv
[i
]) {
467 priv
= adapter
->priv
[i
];
469 /* Initialize private structure */
470 ret
= mwifiex_init_priv(priv
);
475 for (i
= 0; i
< adapter
->priv_num
; i
++) {
476 if (adapter
->priv
[i
]) {
477 ret
= mwifiex_sta_init_cmd(adapter
->priv
[i
], first_sta
);
485 spin_lock_irqsave(&adapter
->cmd_pending_q_lock
, flags
);
486 is_cmd_pend_q_empty
= list_empty(&adapter
->cmd_pending_q
);
487 spin_unlock_irqrestore(&adapter
->cmd_pending_q_lock
, flags
);
488 if (!is_cmd_pend_q_empty
) {
489 /* Send the first command in queue and return */
490 if (mwifiex_main_process(adapter
) != -1)
493 adapter
->hw_status
= MWIFIEX_HW_STATUS_READY
;
500 * This function deletes the BSS priority tables.
502 * The function traverses through all the allocated BSS priority nodes
503 * in every BSS priority table and frees them.
505 static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private
*priv
)
508 struct mwifiex_adapter
*adapter
= priv
->adapter
;
509 struct mwifiex_bss_prio_node
*bssprio_node
= NULL
, *tmp_node
= NULL
,
511 struct list_head
*head
;
515 for (i
= 0; i
< adapter
->priv_num
; ++i
) {
516 head
= &adapter
->bss_prio_tbl
[i
].bss_prio_head
;
517 cur
= &adapter
->bss_prio_tbl
[i
].bss_prio_cur
;
518 lock
= &adapter
->bss_prio_tbl
[i
].bss_prio_lock
;
519 dev_dbg(adapter
->dev
, "info: delete BSS priority table,"
520 " index = %d, i = %d, head = %p, cur = %p\n",
521 priv
->bss_index
, i
, head
, *cur
);
523 spin_lock_irqsave(lock
, flags
);
524 if (list_empty(head
)) {
525 spin_unlock_irqrestore(lock
, flags
);
528 bssprio_node
= list_first_entry(head
,
529 struct mwifiex_bss_prio_node
, list
);
530 spin_unlock_irqrestore(lock
, flags
);
532 list_for_each_entry_safe(bssprio_node
, tmp_node
, head
,
534 if (bssprio_node
->priv
== priv
) {
535 dev_dbg(adapter
->dev
, "info: Delete "
536 "node %p, next = %p\n",
537 bssprio_node
, tmp_node
);
538 spin_lock_irqsave(lock
, flags
);
539 list_del(&bssprio_node
->list
);
540 spin_unlock_irqrestore(lock
, flags
);
544 *cur
= (struct mwifiex_bss_prio_node
*)head
;
550 * This function is used to shutdown the driver.
552 * The following operations are performed sequentially -
553 * - Check if already shut down
554 * - Make sure the main process has stopped
555 * - Clean up the Tx and Rx queues
556 * - Delete BSS priority tables
558 * - Notify completion
561 mwifiex_shutdown_drv(struct mwifiex_adapter
*adapter
)
563 int ret
= -EINPROGRESS
;
564 struct mwifiex_private
*priv
= NULL
;
568 /* mwifiex already shutdown */
569 if (adapter
->hw_status
== MWIFIEX_HW_STATUS_NOT_READY
)
572 adapter
->hw_status
= MWIFIEX_HW_STATUS_CLOSING
;
573 /* wait for mwifiex_process to complete */
574 if (adapter
->mwifiex_processing
) {
575 dev_warn(adapter
->dev
, "main process is still running\n");
579 /* shut down mwifiex */
580 dev_dbg(adapter
->dev
, "info: shutdown mwifiex...\n");
582 /* Clean up Tx/Rx queues and delete BSS priority table */
583 for (i
= 0; i
< adapter
->priv_num
; i
++) {
584 if (adapter
->priv
[i
]) {
585 priv
= adapter
->priv
[i
];
587 mwifiex_clean_txrx(priv
);
588 mwifiex_delete_bss_prio_tbl(priv
);
592 spin_lock_irqsave(&adapter
->mwifiex_lock
, flags
);
594 /* Free adapter structure */
595 mwifiex_free_adapter(adapter
);
597 spin_unlock_irqrestore(&adapter
->mwifiex_lock
, flags
);
599 /* Notify completion */
600 ret
= mwifiex_shutdown_fw_complete(adapter
);
606 * This function downloads the firmware to the card.
608 * The actual download is preceded by two sanity checks -
609 * - Check if firmware is already running
610 * - Check if the interface is the winner to download the firmware
612 * ...and followed by another -
613 * - Check if the firmware is downloaded successfully
615 * After download is successfully completed, the host interrupts are enabled.
617 int mwifiex_dnld_fw(struct mwifiex_adapter
*adapter
,
618 struct mwifiex_fw_image
*pmfw
)
624 /* Check if firmware is already running */
625 ret
= adapter
->if_ops
.check_fw_status(adapter
, poll_num
, &winner
);
627 dev_notice(adapter
->dev
,
628 "WLAN FW already running! Skip FW download\n");
631 poll_num
= MAX_FIRMWARE_POLL_TRIES
;
633 /* Check if we are the winner for downloading FW */
635 dev_notice(adapter
->dev
,
636 "Other interface already running!"
637 " Skip FW download\n");
638 poll_num
= MAX_MULTI_INTERFACE_POLL_TRIES
;
642 /* Download firmware with helper */
643 ret
= adapter
->if_ops
.prog_fw(adapter
, pmfw
);
645 dev_err(adapter
->dev
, "prog_fw failed ret=%#x\n", ret
);
651 /* Check if the firmware is downloaded successfully or not */
652 ret
= adapter
->if_ops
.check_fw_status(adapter
, poll_num
, NULL
);
654 dev_err(adapter
->dev
, "FW failed to be active in time\n");
658 /* re-enable host interrupt for mwifiex after fw dnld is successful */
659 adapter
->if_ops
.enable_int(adapter
);