blob: b2ba262f8a139774d94432bc2f64855630856020 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: HW/FW Initialization
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 "decl.h"
21#include "ioctl.h"
22#include "util.h"
23#include "fw.h"
24#include "main.h"
25#include "wmm.h"
26#include "11n.h"
27
28/*
29 * This function adds a BSS priority table to the table list.
30 *
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.
33 */
34static int mwifiex_add_bss_prio_tbl(struct mwifiex_private *priv)
35{
36 struct mwifiex_adapter *adapter = priv->adapter;
37 struct mwifiex_bss_prio_node *bss_prio;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -070038 struct mwifiex_bss_prio_tbl *tbl = adapter->bss_prio_tbl;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070039 unsigned long flags;
40
41 bss_prio = kzalloc(sizeof(struct mwifiex_bss_prio_node), GFP_KERNEL);
42 if (!bss_prio) {
43 dev_err(adapter->dev, "%s: failed to alloc bss_prio\n",
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -070044 __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020045 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070046 }
47
48 bss_prio->priv = priv;
49 INIT_LIST_HEAD(&bss_prio->list);
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -070050 if (!tbl[priv->bss_priority].bss_prio_cur)
51 tbl[priv->bss_priority].bss_prio_cur = bss_prio;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070052
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -070053 spin_lock_irqsave(&tbl[priv->bss_priority].bss_prio_lock, flags);
54 list_add_tail(&bss_prio->list, &tbl[priv->bss_priority].bss_prio_head);
55 spin_unlock_irqrestore(&tbl[priv->bss_priority].bss_prio_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070056
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070057 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070058}
59
Amitkumar Karwar3249ba72012-06-06 21:12:41 -070060static void scan_delay_timer_fn(unsigned long data)
61{
62 struct mwifiex_private *priv = (struct mwifiex_private *)data;
63 struct mwifiex_adapter *adapter = priv->adapter;
64 struct cmd_ctrl_node *cmd_node, *tmp_node;
65 unsigned long flags;
66
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070067 if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT) {
68 /*
69 * Abort scan operation by cancelling all pending scan
70 * commands
71 */
72 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
73 list_for_each_entry_safe(cmd_node, tmp_node,
74 &adapter->scan_pending_q, list) {
75 list_del(&cmd_node->list);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070076 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
77 }
78 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
79
80 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
81 adapter->scan_processing = false;
82 adapter->scan_delay_cnt = 0;
83 adapter->empty_tx_q_cnt = 0;
84 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
85
86 if (priv->user_scan_cfg) {
87 dev_dbg(priv->adapter->dev,
88 "info: %s: scan aborted\n", __func__);
89 cfg80211_scan_done(priv->scan_request, 1);
90 priv->scan_request = NULL;
91 kfree(priv->user_scan_cfg);
92 priv->user_scan_cfg = NULL;
93 }
Amitkumar Karwar26fc36a2012-08-27 20:32:51 -070094
95 if (priv->scan_pending_on_block) {
96 priv->scan_pending_on_block = false;
97 up(&priv->async_sem);
98 }
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070099 goto done;
100 }
101
102 if (!atomic_read(&priv->adapter->is_tx_received)) {
103 adapter->empty_tx_q_cnt++;
104 if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700105 /*
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700106 * No Tx traffic for 200msec. Get scan command from
107 * scan pending queue and put to cmd pending queue to
108 * resume scan operation
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700109 */
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700110 adapter->scan_delay_cnt = 0;
111 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700112 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700113 cmd_node = list_first_entry(&adapter->scan_pending_q,
114 struct cmd_ctrl_node, list);
115 list_del(&cmd_node->list);
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700116 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
117 flags);
118
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700119 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
120 true);
Amitkumar Karwar9a17bad2012-09-18 15:33:32 -0700121 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700122 goto done;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700123 }
124 } else {
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700125 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700126 }
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700127
128 /* Delay scan operation further by 20msec */
129 mod_timer(&priv->scan_delay_timer, jiffies +
130 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
131 adapter->scan_delay_cnt++;
132
133done:
134 if (atomic_read(&priv->adapter->is_tx_received))
135 atomic_set(&priv->adapter->is_tx_received, false);
136
137 return;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700138}
139
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700140/*
141 * This function initializes the private structure and sets default
142 * values to the members.
143 *
144 * Additionally, it also initializes all the locks and sets up all the
145 * lists.
146 */
147static int mwifiex_init_priv(struct mwifiex_private *priv)
148{
149 u32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700150
151 priv->media_connected = false;
152 memset(priv->curr_addr, 0xff, ETH_ALEN);
153
154 priv->pkt_tx_ctrl = 0;
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700155 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700156 priv->data_rate = 0; /* Initially indicate the rate as auto */
157 priv->is_data_rate_auto = true;
158 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
159 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
160
Amitkumar Karwar5eb02e42012-02-24 21:36:04 -0800161 priv->sec_info.wep_enabled = 0;
Marc Yangf986b6d2011-03-28 17:55:42 -0700162 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
Yogesh Ashok Powar2be50b82011-04-01 18:36:47 -0700163 priv->sec_info.encryption_mode = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700164 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
165 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
166 priv->wep_key_curr_index = 0;
167 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
168 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
169
170 priv->beacon_period = 100; /* beacon interval */ ;
171 priv->attempted_bss_desc = NULL;
172 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
173 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
174
175 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
176 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
177 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
178 priv->assoc_rsp_size = 0;
179 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
180 priv->atim_window = 0;
181 priv->adhoc_state = ADHOC_IDLE;
182 priv->tx_power_level = 0;
183 priv->max_tx_power_level = 0;
184 priv->min_tx_power_level = 0;
185 priv->tx_rate = 0;
186 priv->rxpd_htinfo = 0;
187 priv->rxpd_rate = 0;
188 priv->rate_bitmap = 0;
189 priv->data_rssi_last = 0;
190 priv->data_rssi_avg = 0;
191 priv->data_nf_avg = 0;
192 priv->data_nf_last = 0;
193 priv->bcn_rssi_last = 0;
194 priv->bcn_rssi_avg = 0;
195 priv->bcn_nf_avg = 0;
196 priv->bcn_nf_last = 0;
197 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
198 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
199 priv->wpa_ie_len = 0;
200 priv->wpa_is_gtk_set = false;
201
202 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
203 priv->assoc_tlv_buf_len = 0;
204 memset(&priv->wps, 0, sizeof(priv->wps));
205 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
206 priv->gen_ie_buf_len = 0;
207 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
208
209 priv->wmm_required = true;
210 priv->wmm_enabled = false;
211 priv->wmm_qosinfo = 0;
212 priv->curr_bcn_buf = NULL;
213 priv->curr_bcn_size = 0;
Avinash Patil13d7ba72012-04-09 20:06:56 -0700214 priv->wps_ie = NULL;
215 priv->wps_ie_len = 0;
Avinash Patilc8258912012-08-03 18:06:05 -0700216 priv->ap_11n_enabled = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700217
218 priv->scan_block = false;
219
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700220 setup_timer(&priv->scan_delay_timer, scan_delay_timer_fn,
221 (unsigned long)priv);
222
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700223 return mwifiex_add_bss_prio_tbl(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700224}
225
226/*
227 * This function allocates buffers for members of the adapter
228 * structure.
229 *
230 * The memory allocated includes scan table, command buffers, and
231 * sleep confirm command buffer. In addition, the queues are
232 * also initialized.
233 */
234static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
235{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700236 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700237
238 /* Allocate command buffer */
239 ret = mwifiex_alloc_cmd_buffer(adapter);
240 if (ret) {
241 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700242 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700243 return -1;
244 }
245
246 adapter->sleep_cfm =
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700247 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700248 + INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700249
250 if (!adapter->sleep_cfm) {
251 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
252 " cmd buffer\n", __func__);
253 return -1;
254 }
255 skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
256
257 return 0;
258}
259
260/*
261 * This function initializes the adapter structure and sets default
262 * values to the members of adapter.
263 *
264 * This also initializes the WMM related parameters in the driver private
265 * structures.
266 */
267static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
268{
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700269 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700270
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700271 skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700272
273 adapter->cmd_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700274
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700275 if (adapter->iface_type == MWIFIEX_SDIO)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700276 adapter->data_sent = true;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700277 else
278 adapter->data_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700279
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700280 adapter->cmd_resp_received = false;
281 adapter->event_received = false;
282 adapter->data_received = false;
283
284 adapter->surprise_removed = false;
285
286 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
287
288 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
289 adapter->ps_state = PS_STATE_AWAKE;
290 adapter->need_to_wakeup = false;
291
292 adapter->scan_mode = HostCmd_BSS_MODE_ANY;
293 adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
294 adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
295 adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
296
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700297 adapter->scan_probes = 1;
298
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700299 adapter->multiple_dtim = 1;
300
301 adapter->local_listen_interval = 0; /* default value in firmware
302 will be used */
303
304 adapter->is_deep_sleep = false;
305
306 adapter->delay_null_pkt = false;
307 adapter->delay_to_ps = 1000;
308 adapter->enhanced_ps_mode = PS_MODE_AUTO;
309
310 adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
311 default */
312 adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
313 default */
314 adapter->pm_wakeup_card_req = false;
315
316 adapter->pm_wakeup_fw_try = false;
317
318 adapter->max_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
319 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
320 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
321
322 adapter->is_hs_configured = false;
323 adapter->hs_cfg.conditions = cpu_to_le32(HOST_SLEEP_CFG_COND_DEF);
324 adapter->hs_cfg.gpio = HOST_SLEEP_CFG_GPIO_DEF;
325 adapter->hs_cfg.gap = HOST_SLEEP_CFG_GAP_DEF;
326 adapter->hs_activated = false;
327
328 memset(adapter->event_body, 0, sizeof(adapter->event_body));
329 adapter->hw_dot_11n_dev_cap = 0;
330 adapter->hw_dev_mcs_support = 0;
Amitkumar Karwar21c3ba32011-12-20 23:47:21 -0800331 adapter->sec_chan_offset = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700332 adapter->adhoc_11n_enabled = false;
333
334 mwifiex_wmm_init(adapter);
335
336 if (adapter->sleep_cfm) {
Yogesh Ashok Powar8ed13032011-11-07 21:41:10 -0800337 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
338 adapter->sleep_cfm->data;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700339 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
340 sleep_cfm_buf->command =
341 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
342 sleep_cfm_buf->size =
343 cpu_to_le16(adapter->sleep_cfm->len);
344 sleep_cfm_buf->result = 0;
345 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
346 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700347 }
348 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
349 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
350 adapter->tx_lock_flag = false;
351 adapter->null_pkt_interval = 0;
352 adapter->fw_bands = 0;
353 adapter->config_bands = 0;
354 adapter->adhoc_start_band = 0;
355 adapter->scan_channels = NULL;
356 adapter->fw_release_number = 0;
357 adapter->fw_cap_info = 0;
358 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
359 adapter->event_cause = 0;
360 adapter->region_code = 0;
361 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
362 adapter->adhoc_awake_period = 0;
363 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
364 adapter->arp_filter_size = 0;
Avinash Patilede98bf2012-05-08 18:30:28 -0700365 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700366 adapter->empty_tx_q_cnt = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700367}
368
369/*
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800370 * This function sets trans_start per tx_queue
371 */
372void mwifiex_set_trans_start(struct net_device *dev)
373{
374 int i;
375
376 for (i = 0; i < dev->num_tx_queues; i++)
377 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
378
379 dev->trans_start = jiffies;
380}
381
382/*
383 * This function wakes up all queues in net_device
384 */
385void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
386 struct mwifiex_adapter *adapter)
387{
388 unsigned long dev_queue_flags;
389
390 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
391 netif_tx_wake_all_queues(netdev);
392 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
393}
394
395/*
396 * This function stops all queues in net_device
397 */
398void mwifiex_stop_net_dev_queue(struct net_device *netdev,
399 struct mwifiex_adapter *adapter)
400{
401 unsigned long dev_queue_flags;
402
403 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
404 netif_tx_stop_all_queues(netdev);
405 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
406}
407
408/*
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700409 * This function releases the lock variables and frees the locks and
410 * associated locks.
411 */
412static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
413{
414 struct mwifiex_private *priv;
415 s32 i, j;
416
417 /* Free lists */
418 list_del(&adapter->cmd_free_q);
419 list_del(&adapter->cmd_pending_q);
420 list_del(&adapter->scan_pending_q);
421
422 for (i = 0; i < adapter->priv_num; i++)
423 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
424
425 for (i = 0; i < adapter->priv_num; i++) {
426 if (adapter->priv[i]) {
427 priv = adapter->priv[i];
428 for (j = 0; j < MAX_NUM_TID; ++j)
429 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
430 list_del(&priv->tx_ba_stream_tbl_ptr);
431 list_del(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700432 list_del(&priv->sta_list);
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700433 }
434 }
435}
436
437/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700438 * This function frees the adapter structure.
439 *
440 * The freeing operation is done recursively, by canceling all
441 * pending commands, freeing the member buffers previously
442 * allocated (command buffers, scan table buffer, sleep confirm
443 * command buffer), stopping the timers and calling the cleanup
444 * routines for every interface, before the actual adapter
445 * structure is freed.
446 */
447static void
448mwifiex_free_adapter(struct mwifiex_adapter *adapter)
449{
450 if (!adapter) {
451 pr_err("%s: adapter is NULL\n", __func__);
452 return;
453 }
454
455 mwifiex_cancel_all_pending_cmd(adapter);
456
457 /* Free lock variables */
458 mwifiex_free_lock_list(adapter);
459
460 /* Free command buffer */
461 dev_dbg(adapter->dev, "info: free cmd buffer\n");
462 mwifiex_free_cmd_buffer(adapter);
463
464 del_timer(&adapter->cmd_timer);
465
466 dev_dbg(adapter->dev, "info: free scan table\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700468 if (adapter->if_ops.cleanup_if)
469 adapter->if_ops.cleanup_if(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700470
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800471 if (adapter->sleep_cfm)
472 dev_kfree_skb_any(adapter->sleep_cfm);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700473}
474
475/*
476 * This function intializes the lock variables and
477 * the list heads.
478 */
479int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
480{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700481 struct mwifiex_private *priv;
482 s32 i, j;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700483
484 spin_lock_init(&adapter->mwifiex_lock);
485 spin_lock_init(&adapter->int_lock);
486 spin_lock_init(&adapter->main_proc_lock);
487 spin_lock_init(&adapter->mwifiex_cmd_lock);
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800488 spin_lock_init(&adapter->queue_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700489 for (i = 0; i < adapter->priv_num; i++) {
490 if (adapter->priv[i]) {
491 priv = adapter->priv[i];
492 spin_lock_init(&priv->rx_pkt_lock);
493 spin_lock_init(&priv->wmm.ra_list_spinlock);
494 spin_lock_init(&priv->curr_bcn_buf_lock);
Avinash Patil017a92a2012-08-03 18:06:07 -0700495 spin_lock_init(&priv->sta_list_spinlock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700496 }
497 }
498
499 /* Initialize cmd_free_q */
500 INIT_LIST_HEAD(&adapter->cmd_free_q);
501 /* Initialize cmd_pending_q */
502 INIT_LIST_HEAD(&adapter->cmd_pending_q);
503 /* Initialize scan_pending_q */
504 INIT_LIST_HEAD(&adapter->scan_pending_q);
505
506 spin_lock_init(&adapter->cmd_free_q_lock);
507 spin_lock_init(&adapter->cmd_pending_q_lock);
508 spin_lock_init(&adapter->scan_pending_q_lock);
509
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700510 skb_queue_head_init(&adapter->usb_rx_data_q);
511
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700512 for (i = 0; i < adapter->priv_num; ++i) {
513 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
514 adapter->bss_prio_tbl[i].bss_prio_cur = NULL;
515 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
516 }
517
518 for (i = 0; i < adapter->priv_num; i++) {
519 if (!adapter->priv[i])
520 continue;
521 priv = adapter->priv[i];
522 for (j = 0; j < MAX_NUM_TID; ++j) {
523 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
524 spin_lock_init(&priv->wmm.tid_tbl_ptr[j].tid_tbl_lock);
525 }
526 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
527 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700528 INIT_LIST_HEAD(&priv->sta_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700529
530 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
531 spin_lock_init(&priv->rx_reorder_tbl_lock);
532 }
533
534 return 0;
535}
536
537/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700538 * This function initializes the firmware.
539 *
540 * The following operations are performed sequentially -
541 * - Allocate adapter structure
542 * - Initialize the adapter structure
543 * - Initialize the private structure
544 * - Add BSS priority tables to the adapter structure
545 * - For each interface, send the init commands to firmware
546 * - Send the first command in command pending queue, if available
547 */
548int mwifiex_init_fw(struct mwifiex_adapter *adapter)
549{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700550 int ret;
551 struct mwifiex_private *priv;
552 u8 i, first_sta = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700553 int is_cmd_pend_q_empty;
554 unsigned long flags;
555
556 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
557
558 /* Allocate memory for member of adapter structure */
559 ret = mwifiex_allocate_adapter(adapter);
560 if (ret)
561 return -1;
562
563 /* Initialize adapter structure */
564 mwifiex_init_adapter(adapter);
565
566 for (i = 0; i < adapter->priv_num; i++) {
567 if (adapter->priv[i]) {
568 priv = adapter->priv[i];
569
570 /* Initialize private structure */
571 ret = mwifiex_init_priv(priv);
572 if (ret)
573 return -1;
574 }
575 }
576 for (i = 0; i < adapter->priv_num; i++) {
577 if (adapter->priv[i]) {
578 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
579 if (ret == -1)
580 return -1;
581
582 first_sta = false;
583 }
584 }
585
586 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
587 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
588 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
589 if (!is_cmd_pend_q_empty) {
590 /* Send the first command in queue and return */
591 if (mwifiex_main_process(adapter) != -1)
592 ret = -EINPROGRESS;
593 } else {
594 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
595 }
596
597 return ret;
598}
599
600/*
601 * This function deletes the BSS priority tables.
602 *
603 * The function traverses through all the allocated BSS priority nodes
604 * in every BSS priority table and frees them.
605 */
606static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
607{
608 int i;
609 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700610 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node, **cur;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611 struct list_head *head;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700612 spinlock_t *lock; /* bss priority lock */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700613 unsigned long flags;
614
615 for (i = 0; i < adapter->priv_num; ++i) {
616 head = &adapter->bss_prio_tbl[i].bss_prio_head;
617 cur = &adapter->bss_prio_tbl[i].bss_prio_cur;
618 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
619 dev_dbg(adapter->dev, "info: delete BSS priority table,"
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800620 " bss_type = %d, bss_num = %d, i = %d,"
621 " head = %p, cur = %p\n",
622 priv->bss_type, priv->bss_num, i, head, *cur);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700623 if (*cur) {
624 spin_lock_irqsave(lock, flags);
625 if (list_empty(head)) {
626 spin_unlock_irqrestore(lock, flags);
627 continue;
628 }
629 bssprio_node = list_first_entry(head,
630 struct mwifiex_bss_prio_node, list);
631 spin_unlock_irqrestore(lock, flags);
632
633 list_for_each_entry_safe(bssprio_node, tmp_node, head,
634 list) {
635 if (bssprio_node->priv == priv) {
636 dev_dbg(adapter->dev, "info: Delete "
637 "node %p, next = %p\n",
638 bssprio_node, tmp_node);
639 spin_lock_irqsave(lock, flags);
640 list_del(&bssprio_node->list);
641 spin_unlock_irqrestore(lock, flags);
642 kfree(bssprio_node);
643 }
644 }
645 *cur = (struct mwifiex_bss_prio_node *)head;
646 }
647 }
648}
649
650/*
651 * This function is used to shutdown the driver.
652 *
653 * The following operations are performed sequentially -
654 * - Check if already shut down
655 * - Make sure the main process has stopped
656 * - Clean up the Tx and Rx queues
657 * - Delete BSS priority tables
658 * - Free the adapter
659 * - Notify completion
660 */
661int
662mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
663{
664 int ret = -EINPROGRESS;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700665 struct mwifiex_private *priv;
666 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700667 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700668 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700669
670 /* mwifiex already shutdown */
671 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
672 return 0;
673
674 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
675 /* wait for mwifiex_process to complete */
676 if (adapter->mwifiex_processing) {
677 dev_warn(adapter->dev, "main process is still running\n");
678 return ret;
679 }
680
681 /* shut down mwifiex */
682 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
683
684 /* Clean up Tx/Rx queues and delete BSS priority table */
685 for (i = 0; i < adapter->priv_num; i++) {
686 if (adapter->priv[i]) {
687 priv = adapter->priv[i];
688
689 mwifiex_clean_txrx(priv);
690 mwifiex_delete_bss_prio_tbl(priv);
691 }
692 }
693
694 spin_lock_irqsave(&adapter->mwifiex_lock, flags);
695
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700696 if (adapter->if_ops.data_complete) {
697 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
698 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
699
700 priv = adapter->priv[rx_info->bss_num];
701 if (priv)
702 priv->stats.rx_dropped++;
703
704 adapter->if_ops.data_complete(adapter, skb);
705 }
706 }
707
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700708 /* Free adapter structure */
709 mwifiex_free_adapter(adapter);
710
711 spin_unlock_irqrestore(&adapter->mwifiex_lock, flags);
712
713 /* Notify completion */
714 ret = mwifiex_shutdown_fw_complete(adapter);
715
716 return ret;
717}
718
719/*
720 * This function downloads the firmware to the card.
721 *
722 * The actual download is preceded by two sanity checks -
723 * - Check if firmware is already running
724 * - Check if the interface is the winner to download the firmware
725 *
726 * ...and followed by another -
727 * - Check if the firmware is downloaded successfully
728 *
729 * After download is successfully completed, the host interrupts are enabled.
730 */
731int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
732 struct mwifiex_fw_image *pmfw)
733{
Amitkumar Karward930fae2011-10-11 17:41:21 -0700734 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700735 u32 poll_num = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700736
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700737 if (adapter->if_ops.check_fw_status) {
738 adapter->winner = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700739
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700740 /* check if firmware is already running */
741 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
742 if (!ret) {
743 dev_notice(adapter->dev,
744 "WLAN FW already running! Skip FW dnld\n");
745 goto done;
746 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700747
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700748 poll_num = MAX_FIRMWARE_POLL_TRIES;
749
750 /* check if we are the winner for downloading FW */
751 if (!adapter->winner) {
752 dev_notice(adapter->dev,
753 "FW already running! Skip FW dnld\n");
754 poll_num = MAX_MULTI_INTERFACE_POLL_TRIES;
755 goto poll_fw;
756 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700757 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700758
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700759 if (pmfw) {
760 /* Download firmware with helper */
761 ret = adapter->if_ops.prog_fw(adapter, pmfw);
762 if (ret) {
763 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
764 return ret;
765 }
766 }
767
768poll_fw:
769 /* Check if the firmware is downloaded successfully or not */
Amitkumar Karward930fae2011-10-11 17:41:21 -0700770 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700771 if (ret) {
772 dev_err(adapter->dev, "FW failed to be active in time\n");
773 return -1;
774 }
775done:
776 /* re-enable host interrupt for mwifiex after fw dnld is successful */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700777 if (adapter->if_ops.enable_int)
778 adapter->if_ops.enable_int(adapter);
779
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700780 return ret;
781}