blob: 118a29f6e2400b4c77c73106412a45736e5a4767 [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);
76 cmd_node->wait_q_enabled = false;
77 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
78 }
79 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
80
81 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
82 adapter->scan_processing = false;
83 adapter->scan_delay_cnt = 0;
84 adapter->empty_tx_q_cnt = 0;
85 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
86
87 if (priv->user_scan_cfg) {
88 dev_dbg(priv->adapter->dev,
89 "info: %s: scan aborted\n", __func__);
90 cfg80211_scan_done(priv->scan_request, 1);
91 priv->scan_request = NULL;
92 kfree(priv->user_scan_cfg);
93 priv->user_scan_cfg = NULL;
94 }
95 goto done;
96 }
97
98 if (!atomic_read(&priv->adapter->is_tx_received)) {
99 adapter->empty_tx_q_cnt++;
100 if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700101 /*
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700102 * No Tx traffic for 200msec. Get scan command from
103 * scan pending queue and put to cmd pending queue to
104 * resume scan operation
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700105 */
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700106 adapter->scan_delay_cnt = 0;
107 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700108 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700109 cmd_node = list_first_entry(&adapter->scan_pending_q,
110 struct cmd_ctrl_node, list);
111 list_del(&cmd_node->list);
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700112 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
113 flags);
114
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700115 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
116 true);
117 goto done;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700118 }
119 } else {
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700120 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700121 }
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700122
123 /* Delay scan operation further by 20msec */
124 mod_timer(&priv->scan_delay_timer, jiffies +
125 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
126 adapter->scan_delay_cnt++;
127
128done:
129 if (atomic_read(&priv->adapter->is_tx_received))
130 atomic_set(&priv->adapter->is_tx_received, false);
131
132 return;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700133}
134
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700135/*
136 * This function initializes the private structure and sets default
137 * values to the members.
138 *
139 * Additionally, it also initializes all the locks and sets up all the
140 * lists.
141 */
142static int mwifiex_init_priv(struct mwifiex_private *priv)
143{
144 u32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700145
146 priv->media_connected = false;
147 memset(priv->curr_addr, 0xff, ETH_ALEN);
148
149 priv->pkt_tx_ctrl = 0;
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700150 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700151 priv->data_rate = 0; /* Initially indicate the rate as auto */
152 priv->is_data_rate_auto = true;
153 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
154 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
155
Amitkumar Karwar5eb02e42012-02-24 21:36:04 -0800156 priv->sec_info.wep_enabled = 0;
Marc Yangf986b6d2011-03-28 17:55:42 -0700157 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
Yogesh Ashok Powar2be50b82011-04-01 18:36:47 -0700158 priv->sec_info.encryption_mode = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700159 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
160 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
161 priv->wep_key_curr_index = 0;
162 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
163 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
164
165 priv->beacon_period = 100; /* beacon interval */ ;
166 priv->attempted_bss_desc = NULL;
167 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
168 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
169
170 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
171 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
172 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
173 priv->assoc_rsp_size = 0;
174 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
175 priv->atim_window = 0;
176 priv->adhoc_state = ADHOC_IDLE;
177 priv->tx_power_level = 0;
178 priv->max_tx_power_level = 0;
179 priv->min_tx_power_level = 0;
180 priv->tx_rate = 0;
181 priv->rxpd_htinfo = 0;
182 priv->rxpd_rate = 0;
183 priv->rate_bitmap = 0;
184 priv->data_rssi_last = 0;
185 priv->data_rssi_avg = 0;
186 priv->data_nf_avg = 0;
187 priv->data_nf_last = 0;
188 priv->bcn_rssi_last = 0;
189 priv->bcn_rssi_avg = 0;
190 priv->bcn_nf_avg = 0;
191 priv->bcn_nf_last = 0;
192 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
193 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
194 priv->wpa_ie_len = 0;
195 priv->wpa_is_gtk_set = false;
196
197 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
198 priv->assoc_tlv_buf_len = 0;
199 memset(&priv->wps, 0, sizeof(priv->wps));
200 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
201 priv->gen_ie_buf_len = 0;
202 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
203
204 priv->wmm_required = true;
205 priv->wmm_enabled = false;
206 priv->wmm_qosinfo = 0;
207 priv->curr_bcn_buf = NULL;
208 priv->curr_bcn_size = 0;
Avinash Patil13d7ba72012-04-09 20:06:56 -0700209 priv->wps_ie = NULL;
210 priv->wps_ie_len = 0;
Avinash Patilc8258912012-08-03 18:06:05 -0700211 priv->ap_11n_enabled = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700212
213 priv->scan_block = false;
214
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700215 setup_timer(&priv->scan_delay_timer, scan_delay_timer_fn,
216 (unsigned long)priv);
217
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700218 return mwifiex_add_bss_prio_tbl(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219}
220
221/*
222 * This function allocates buffers for members of the adapter
223 * structure.
224 *
225 * The memory allocated includes scan table, command buffers, and
226 * sleep confirm command buffer. In addition, the queues are
227 * also initialized.
228 */
229static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
230{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700231 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700232
233 /* Allocate command buffer */
234 ret = mwifiex_alloc_cmd_buffer(adapter);
235 if (ret) {
236 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700237 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700238 return -1;
239 }
240
241 adapter->sleep_cfm =
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700242 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700243 + INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700244
245 if (!adapter->sleep_cfm) {
246 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
247 " cmd buffer\n", __func__);
248 return -1;
249 }
250 skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
251
252 return 0;
253}
254
255/*
256 * This function initializes the adapter structure and sets default
257 * values to the members of adapter.
258 *
259 * This also initializes the WMM related parameters in the driver private
260 * structures.
261 */
262static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
263{
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700264 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700265
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700266 skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700267
268 adapter->cmd_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700269
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700270 if (adapter->iface_type == MWIFIEX_SDIO)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700271 adapter->data_sent = true;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700272 else
273 adapter->data_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700274
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700275 adapter->cmd_resp_received = false;
276 adapter->event_received = false;
277 adapter->data_received = false;
278
279 adapter->surprise_removed = false;
280
281 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
282
283 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
284 adapter->ps_state = PS_STATE_AWAKE;
285 adapter->need_to_wakeup = false;
286
287 adapter->scan_mode = HostCmd_BSS_MODE_ANY;
288 adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
289 adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
290 adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
291
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700292 adapter->scan_probes = 1;
293
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700294 adapter->multiple_dtim = 1;
295
296 adapter->local_listen_interval = 0; /* default value in firmware
297 will be used */
298
299 adapter->is_deep_sleep = false;
300
301 adapter->delay_null_pkt = false;
302 adapter->delay_to_ps = 1000;
303 adapter->enhanced_ps_mode = PS_MODE_AUTO;
304
305 adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
306 default */
307 adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
308 default */
309 adapter->pm_wakeup_card_req = false;
310
311 adapter->pm_wakeup_fw_try = false;
312
313 adapter->max_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
314 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
315 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
316
317 adapter->is_hs_configured = false;
318 adapter->hs_cfg.conditions = cpu_to_le32(HOST_SLEEP_CFG_COND_DEF);
319 adapter->hs_cfg.gpio = HOST_SLEEP_CFG_GPIO_DEF;
320 adapter->hs_cfg.gap = HOST_SLEEP_CFG_GAP_DEF;
321 adapter->hs_activated = false;
322
323 memset(adapter->event_body, 0, sizeof(adapter->event_body));
324 adapter->hw_dot_11n_dev_cap = 0;
325 adapter->hw_dev_mcs_support = 0;
Amitkumar Karwar21c3ba32011-12-20 23:47:21 -0800326 adapter->sec_chan_offset = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700327 adapter->adhoc_11n_enabled = false;
328
329 mwifiex_wmm_init(adapter);
330
331 if (adapter->sleep_cfm) {
Yogesh Ashok Powar8ed13032011-11-07 21:41:10 -0800332 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
333 adapter->sleep_cfm->data;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700334 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
335 sleep_cfm_buf->command =
336 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
337 sleep_cfm_buf->size =
338 cpu_to_le16(adapter->sleep_cfm->len);
339 sleep_cfm_buf->result = 0;
340 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
341 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700342 }
343 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
344 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
345 adapter->tx_lock_flag = false;
346 adapter->null_pkt_interval = 0;
347 adapter->fw_bands = 0;
348 adapter->config_bands = 0;
349 adapter->adhoc_start_band = 0;
350 adapter->scan_channels = NULL;
351 adapter->fw_release_number = 0;
352 adapter->fw_cap_info = 0;
353 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
354 adapter->event_cause = 0;
355 adapter->region_code = 0;
356 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
357 adapter->adhoc_awake_period = 0;
358 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
359 adapter->arp_filter_size = 0;
Avinash Patilede98bf2012-05-08 18:30:28 -0700360 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700361 adapter->empty_tx_q_cnt = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700362}
363
364/*
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800365 * This function sets trans_start per tx_queue
366 */
367void mwifiex_set_trans_start(struct net_device *dev)
368{
369 int i;
370
371 for (i = 0; i < dev->num_tx_queues; i++)
372 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
373
374 dev->trans_start = jiffies;
375}
376
377/*
378 * This function wakes up all queues in net_device
379 */
380void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
381 struct mwifiex_adapter *adapter)
382{
383 unsigned long dev_queue_flags;
384
385 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
386 netif_tx_wake_all_queues(netdev);
387 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
388}
389
390/*
391 * This function stops all queues in net_device
392 */
393void mwifiex_stop_net_dev_queue(struct net_device *netdev,
394 struct mwifiex_adapter *adapter)
395{
396 unsigned long dev_queue_flags;
397
398 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
399 netif_tx_stop_all_queues(netdev);
400 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
401}
402
403/*
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700404 * This function releases the lock variables and frees the locks and
405 * associated locks.
406 */
407static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
408{
409 struct mwifiex_private *priv;
410 s32 i, j;
411
412 /* Free lists */
413 list_del(&adapter->cmd_free_q);
414 list_del(&adapter->cmd_pending_q);
415 list_del(&adapter->scan_pending_q);
416
417 for (i = 0; i < adapter->priv_num; i++)
418 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
419
420 for (i = 0; i < adapter->priv_num; i++) {
421 if (adapter->priv[i]) {
422 priv = adapter->priv[i];
423 for (j = 0; j < MAX_NUM_TID; ++j)
424 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
425 list_del(&priv->tx_ba_stream_tbl_ptr);
426 list_del(&priv->rx_reorder_tbl_ptr);
427 }
428 }
429}
430
431/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700432 * This function frees the adapter structure.
433 *
434 * The freeing operation is done recursively, by canceling all
435 * pending commands, freeing the member buffers previously
436 * allocated (command buffers, scan table buffer, sleep confirm
437 * command buffer), stopping the timers and calling the cleanup
438 * routines for every interface, before the actual adapter
439 * structure is freed.
440 */
441static void
442mwifiex_free_adapter(struct mwifiex_adapter *adapter)
443{
444 if (!adapter) {
445 pr_err("%s: adapter is NULL\n", __func__);
446 return;
447 }
448
449 mwifiex_cancel_all_pending_cmd(adapter);
450
451 /* Free lock variables */
452 mwifiex_free_lock_list(adapter);
453
454 /* Free command buffer */
455 dev_dbg(adapter->dev, "info: free cmd buffer\n");
456 mwifiex_free_cmd_buffer(adapter);
457
458 del_timer(&adapter->cmd_timer);
459
460 dev_dbg(adapter->dev, "info: free scan table\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700461
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700462 if (adapter->if_ops.cleanup_if)
463 adapter->if_ops.cleanup_if(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700464
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800465 if (adapter->sleep_cfm)
466 dev_kfree_skb_any(adapter->sleep_cfm);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467}
468
469/*
470 * This function intializes the lock variables and
471 * the list heads.
472 */
473int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
474{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700475 struct mwifiex_private *priv;
476 s32 i, j;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700477
478 spin_lock_init(&adapter->mwifiex_lock);
479 spin_lock_init(&adapter->int_lock);
480 spin_lock_init(&adapter->main_proc_lock);
481 spin_lock_init(&adapter->mwifiex_cmd_lock);
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800482 spin_lock_init(&adapter->queue_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700483 for (i = 0; i < adapter->priv_num; i++) {
484 if (adapter->priv[i]) {
485 priv = adapter->priv[i];
486 spin_lock_init(&priv->rx_pkt_lock);
487 spin_lock_init(&priv->wmm.ra_list_spinlock);
488 spin_lock_init(&priv->curr_bcn_buf_lock);
489 }
490 }
491
492 /* Initialize cmd_free_q */
493 INIT_LIST_HEAD(&adapter->cmd_free_q);
494 /* Initialize cmd_pending_q */
495 INIT_LIST_HEAD(&adapter->cmd_pending_q);
496 /* Initialize scan_pending_q */
497 INIT_LIST_HEAD(&adapter->scan_pending_q);
498
499 spin_lock_init(&adapter->cmd_free_q_lock);
500 spin_lock_init(&adapter->cmd_pending_q_lock);
501 spin_lock_init(&adapter->scan_pending_q_lock);
502
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700503 skb_queue_head_init(&adapter->usb_rx_data_q);
504
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700505 for (i = 0; i < adapter->priv_num; ++i) {
506 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
507 adapter->bss_prio_tbl[i].bss_prio_cur = NULL;
508 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
509 }
510
511 for (i = 0; i < adapter->priv_num; i++) {
512 if (!adapter->priv[i])
513 continue;
514 priv = adapter->priv[i];
515 for (j = 0; j < MAX_NUM_TID; ++j) {
516 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
517 spin_lock_init(&priv->wmm.tid_tbl_ptr[j].tid_tbl_lock);
518 }
519 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
520 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
521
522 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
523 spin_lock_init(&priv->rx_reorder_tbl_lock);
524 }
525
526 return 0;
527}
528
529/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700530 * This function initializes the firmware.
531 *
532 * The following operations are performed sequentially -
533 * - Allocate adapter structure
534 * - Initialize the adapter structure
535 * - Initialize the private structure
536 * - Add BSS priority tables to the adapter structure
537 * - For each interface, send the init commands to firmware
538 * - Send the first command in command pending queue, if available
539 */
540int mwifiex_init_fw(struct mwifiex_adapter *adapter)
541{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700542 int ret;
543 struct mwifiex_private *priv;
544 u8 i, first_sta = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700545 int is_cmd_pend_q_empty;
546 unsigned long flags;
547
548 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
549
550 /* Allocate memory for member of adapter structure */
551 ret = mwifiex_allocate_adapter(adapter);
552 if (ret)
553 return -1;
554
555 /* Initialize adapter structure */
556 mwifiex_init_adapter(adapter);
557
558 for (i = 0; i < adapter->priv_num; i++) {
559 if (adapter->priv[i]) {
560 priv = adapter->priv[i];
561
562 /* Initialize private structure */
563 ret = mwifiex_init_priv(priv);
564 if (ret)
565 return -1;
566 }
567 }
568 for (i = 0; i < adapter->priv_num; i++) {
569 if (adapter->priv[i]) {
570 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
571 if (ret == -1)
572 return -1;
573
574 first_sta = false;
575 }
576 }
577
578 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
579 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
580 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
581 if (!is_cmd_pend_q_empty) {
582 /* Send the first command in queue and return */
583 if (mwifiex_main_process(adapter) != -1)
584 ret = -EINPROGRESS;
585 } else {
586 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
587 }
588
589 return ret;
590}
591
592/*
593 * This function deletes the BSS priority tables.
594 *
595 * The function traverses through all the allocated BSS priority nodes
596 * in every BSS priority table and frees them.
597 */
598static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
599{
600 int i;
601 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700602 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node, **cur;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700603 struct list_head *head;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700604 spinlock_t *lock; /* bss priority lock */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700605 unsigned long flags;
606
607 for (i = 0; i < adapter->priv_num; ++i) {
608 head = &adapter->bss_prio_tbl[i].bss_prio_head;
609 cur = &adapter->bss_prio_tbl[i].bss_prio_cur;
610 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
611 dev_dbg(adapter->dev, "info: delete BSS priority table,"
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800612 " bss_type = %d, bss_num = %d, i = %d,"
613 " head = %p, cur = %p\n",
614 priv->bss_type, priv->bss_num, i, head, *cur);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700615 if (*cur) {
616 spin_lock_irqsave(lock, flags);
617 if (list_empty(head)) {
618 spin_unlock_irqrestore(lock, flags);
619 continue;
620 }
621 bssprio_node = list_first_entry(head,
622 struct mwifiex_bss_prio_node, list);
623 spin_unlock_irqrestore(lock, flags);
624
625 list_for_each_entry_safe(bssprio_node, tmp_node, head,
626 list) {
627 if (bssprio_node->priv == priv) {
628 dev_dbg(adapter->dev, "info: Delete "
629 "node %p, next = %p\n",
630 bssprio_node, tmp_node);
631 spin_lock_irqsave(lock, flags);
632 list_del(&bssprio_node->list);
633 spin_unlock_irqrestore(lock, flags);
634 kfree(bssprio_node);
635 }
636 }
637 *cur = (struct mwifiex_bss_prio_node *)head;
638 }
639 }
640}
641
642/*
643 * This function is used to shutdown the driver.
644 *
645 * The following operations are performed sequentially -
646 * - Check if already shut down
647 * - Make sure the main process has stopped
648 * - Clean up the Tx and Rx queues
649 * - Delete BSS priority tables
650 * - Free the adapter
651 * - Notify completion
652 */
653int
654mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
655{
656 int ret = -EINPROGRESS;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700657 struct mwifiex_private *priv;
658 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700659 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700660 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700661
662 /* mwifiex already shutdown */
663 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
664 return 0;
665
666 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
667 /* wait for mwifiex_process to complete */
668 if (adapter->mwifiex_processing) {
669 dev_warn(adapter->dev, "main process is still running\n");
670 return ret;
671 }
672
673 /* shut down mwifiex */
674 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
675
676 /* Clean up Tx/Rx queues and delete BSS priority table */
677 for (i = 0; i < adapter->priv_num; i++) {
678 if (adapter->priv[i]) {
679 priv = adapter->priv[i];
680
681 mwifiex_clean_txrx(priv);
682 mwifiex_delete_bss_prio_tbl(priv);
683 }
684 }
685
686 spin_lock_irqsave(&adapter->mwifiex_lock, flags);
687
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700688 if (adapter->if_ops.data_complete) {
689 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
690 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
691
692 priv = adapter->priv[rx_info->bss_num];
693 if (priv)
694 priv->stats.rx_dropped++;
695
696 adapter->if_ops.data_complete(adapter, skb);
697 }
698 }
699
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700700 /* Free adapter structure */
701 mwifiex_free_adapter(adapter);
702
703 spin_unlock_irqrestore(&adapter->mwifiex_lock, flags);
704
705 /* Notify completion */
706 ret = mwifiex_shutdown_fw_complete(adapter);
707
708 return ret;
709}
710
711/*
712 * This function downloads the firmware to the card.
713 *
714 * The actual download is preceded by two sanity checks -
715 * - Check if firmware is already running
716 * - Check if the interface is the winner to download the firmware
717 *
718 * ...and followed by another -
719 * - Check if the firmware is downloaded successfully
720 *
721 * After download is successfully completed, the host interrupts are enabled.
722 */
723int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
724 struct mwifiex_fw_image *pmfw)
725{
Amitkumar Karward930fae2011-10-11 17:41:21 -0700726 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700727 u32 poll_num = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700728
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700729 if (adapter->if_ops.check_fw_status) {
730 adapter->winner = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700731
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700732 /* check if firmware is already running */
733 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
734 if (!ret) {
735 dev_notice(adapter->dev,
736 "WLAN FW already running! Skip FW dnld\n");
737 goto done;
738 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700739
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700740 poll_num = MAX_FIRMWARE_POLL_TRIES;
741
742 /* check if we are the winner for downloading FW */
743 if (!adapter->winner) {
744 dev_notice(adapter->dev,
745 "FW already running! Skip FW dnld\n");
746 poll_num = MAX_MULTI_INTERFACE_POLL_TRIES;
747 goto poll_fw;
748 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700749 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700750
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700751 if (pmfw) {
752 /* Download firmware with helper */
753 ret = adapter->if_ops.prog_fw(adapter, pmfw);
754 if (ret) {
755 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
756 return ret;
757 }
758 }
759
760poll_fw:
761 /* Check if the firmware is downloaded successfully or not */
Amitkumar Karward930fae2011-10-11 17:41:21 -0700762 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700763 if (ret) {
764 dev_err(adapter->dev, "FW failed to be active in time\n");
765 return -1;
766 }
767done:
768 /* re-enable host interrupt for mwifiex after fw dnld is successful */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700769 if (adapter->if_ops.enable_int)
770 adapter->if_ops.enable_int(adapter);
771
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700772 return ret;
773}