blob: 58e151edfa094e6e0ee70e5f81b07982df6c6981 [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);
Joe Perches0d2e7a52013-02-03 17:28:14 +000042 if (!bss_prio)
Christoph Fritzb53575e2011-05-08 22:50:09 +020043 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070044
45 bss_prio->priv = priv;
46 INIT_LIST_HEAD(&bss_prio->list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070047
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -070048 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);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070051
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070052 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070053}
54
Amitkumar Karwar3249ba72012-06-06 21:12:41 -070055static void scan_delay_timer_fn(unsigned long data)
56{
57 struct mwifiex_private *priv = (struct mwifiex_private *)data;
58 struct mwifiex_adapter *adapter = priv->adapter;
59 struct cmd_ctrl_node *cmd_node, *tmp_node;
60 unsigned long flags;
61
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070062 if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT) {
63 /*
64 * Abort scan operation by cancelling all pending scan
65 * commands
66 */
67 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
68 list_for_each_entry_safe(cmd_node, tmp_node,
69 &adapter->scan_pending_q, list) {
70 list_del(&cmd_node->list);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070071 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
72 }
73 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
74
75 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
76 adapter->scan_processing = false;
77 adapter->scan_delay_cnt = 0;
78 adapter->empty_tx_q_cnt = 0;
79 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
80
81 if (priv->user_scan_cfg) {
Amitkumar Karwarf162cac2012-10-19 19:19:16 -070082 if (priv->scan_request) {
83 dev_dbg(priv->adapter->dev,
84 "info: aborting scan\n");
85 cfg80211_scan_done(priv->scan_request, 1);
86 priv->scan_request = NULL;
87 } else {
88 dev_dbg(priv->adapter->dev,
89 "info: scan already aborted\n");
90 }
91
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070092 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);
Amitkumar Karwar9a17bad2012-09-18 15:33:32 -0700117 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700118 goto done;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700119 }
120 } else {
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700121 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700122 }
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700123
124 /* Delay scan operation further by 20msec */
125 mod_timer(&priv->scan_delay_timer, jiffies +
126 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
127 adapter->scan_delay_cnt++;
128
129done:
130 if (atomic_read(&priv->adapter->is_tx_received))
131 atomic_set(&priv->adapter->is_tx_received, false);
132
133 return;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700134}
135
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700136/*
137 * This function initializes the private structure and sets default
138 * values to the members.
139 *
140 * Additionally, it also initializes all the locks and sets up all the
141 * lists.
142 */
Stone Piao9197ab92012-09-25 20:23:42 -0700143int mwifiex_init_priv(struct mwifiex_private *priv)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700144{
145 u32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700146
147 priv->media_connected = false;
148 memset(priv->curr_addr, 0xff, ETH_ALEN);
149
150 priv->pkt_tx_ctrl = 0;
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700151 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700152 priv->data_rate = 0; /* Initially indicate the rate as auto */
153 priv->is_data_rate_auto = true;
154 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
155 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
156
Amitkumar Karwar5eb02e42012-02-24 21:36:04 -0800157 priv->sec_info.wep_enabled = 0;
Marc Yangf986b6d2011-03-28 17:55:42 -0700158 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
Yogesh Ashok Powar2be50b82011-04-01 18:36:47 -0700159 priv->sec_info.encryption_mode = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700160 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
161 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
162 priv->wep_key_curr_index = 0;
163 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
164 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
165
166 priv->beacon_period = 100; /* beacon interval */ ;
167 priv->attempted_bss_desc = NULL;
168 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
169 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
170
171 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
172 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
173 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
174 priv->assoc_rsp_size = 0;
175 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
176 priv->atim_window = 0;
177 priv->adhoc_state = ADHOC_IDLE;
178 priv->tx_power_level = 0;
179 priv->max_tx_power_level = 0;
180 priv->min_tx_power_level = 0;
181 priv->tx_rate = 0;
182 priv->rxpd_htinfo = 0;
183 priv->rxpd_rate = 0;
184 priv->rate_bitmap = 0;
185 priv->data_rssi_last = 0;
186 priv->data_rssi_avg = 0;
187 priv->data_nf_avg = 0;
188 priv->data_nf_last = 0;
189 priv->bcn_rssi_last = 0;
190 priv->bcn_rssi_avg = 0;
191 priv->bcn_nf_avg = 0;
192 priv->bcn_nf_last = 0;
193 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
194 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
195 priv->wpa_ie_len = 0;
196 priv->wpa_is_gtk_set = false;
197
198 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
199 priv->assoc_tlv_buf_len = 0;
200 memset(&priv->wps, 0, sizeof(priv->wps));
201 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
202 priv->gen_ie_buf_len = 0;
203 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
204
205 priv->wmm_required = true;
206 priv->wmm_enabled = false;
207 priv->wmm_qosinfo = 0;
208 priv->curr_bcn_buf = NULL;
209 priv->curr_bcn_size = 0;
Avinash Patil13d7ba72012-04-09 20:06:56 -0700210 priv->wps_ie = NULL;
211 priv->wps_ie_len = 0;
Avinash Patilc8258912012-08-03 18:06:05 -0700212 priv->ap_11n_enabled = 0;
Stone Piao7feb4c42012-09-25 20:23:36 -0700213 memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700214
215 priv->scan_block = false;
216
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700217 setup_timer(&priv->scan_delay_timer, scan_delay_timer_fn,
218 (unsigned long)priv);
219
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700220 return mwifiex_add_bss_prio_tbl(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700221}
222
223/*
224 * This function allocates buffers for members of the adapter
225 * structure.
226 *
227 * The memory allocated includes scan table, command buffers, and
228 * sleep confirm command buffer. In addition, the queues are
229 * also initialized.
230 */
231static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
232{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700233 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700234
235 /* Allocate command buffer */
236 ret = mwifiex_alloc_cmd_buffer(adapter);
237 if (ret) {
238 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700239 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700240 return -1;
241 }
242
243 adapter->sleep_cfm =
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700244 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700245 + INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700246
247 if (!adapter->sleep_cfm) {
248 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
249 " cmd buffer\n", __func__);
250 return -1;
251 }
252 skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
253
254 return 0;
255}
256
257/*
258 * This function initializes the adapter structure and sets default
259 * values to the members of adapter.
260 *
261 * This also initializes the WMM related parameters in the driver private
262 * structures.
263 */
264static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
265{
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700266 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700267
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700268 skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700269
270 adapter->cmd_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700271
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700272 if (adapter->iface_type == MWIFIEX_SDIO)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700273 adapter->data_sent = true;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700274 else
275 adapter->data_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700276
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700277 adapter->cmd_resp_received = false;
278 adapter->event_received = false;
279 adapter->data_received = false;
280
281 adapter->surprise_removed = false;
282
283 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
284
285 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
286 adapter->ps_state = PS_STATE_AWAKE;
287 adapter->need_to_wakeup = false;
288
289 adapter->scan_mode = HostCmd_BSS_MODE_ANY;
290 adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
291 adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
292 adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
293
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700294 adapter->scan_probes = 1;
295
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700296 adapter->multiple_dtim = 1;
297
298 adapter->local_listen_interval = 0; /* default value in firmware
299 will be used */
300
301 adapter->is_deep_sleep = false;
302
303 adapter->delay_null_pkt = false;
304 adapter->delay_to_ps = 1000;
305 adapter->enhanced_ps_mode = PS_MODE_AUTO;
306
307 adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
308 default */
309 adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
310 default */
311 adapter->pm_wakeup_card_req = false;
312
313 adapter->pm_wakeup_fw_try = false;
314
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700315 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
316 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
317
318 adapter->is_hs_configured = false;
Amitkumar Karwarcc0b5a62013-03-04 16:27:57 -0800319 adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
320 adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
321 adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700322 adapter->hs_activated = false;
323
324 memset(adapter->event_body, 0, sizeof(adapter->event_body));
325 adapter->hw_dot_11n_dev_cap = 0;
326 adapter->hw_dev_mcs_support = 0;
Amitkumar Karwar21c3ba32011-12-20 23:47:21 -0800327 adapter->sec_chan_offset = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700328 adapter->adhoc_11n_enabled = false;
329
330 mwifiex_wmm_init(adapter);
331
332 if (adapter->sleep_cfm) {
Yogesh Ashok Powar8ed13032011-11-07 21:41:10 -0800333 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
334 adapter->sleep_cfm->data;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700335 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
336 sleep_cfm_buf->command =
337 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
338 sleep_cfm_buf->size =
339 cpu_to_le16(adapter->sleep_cfm->len);
340 sleep_cfm_buf->result = 0;
341 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
342 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700343 }
344 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
345 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
346 adapter->tx_lock_flag = false;
347 adapter->null_pkt_interval = 0;
348 adapter->fw_bands = 0;
349 adapter->config_bands = 0;
350 adapter->adhoc_start_band = 0;
351 adapter->scan_channels = NULL;
352 adapter->fw_release_number = 0;
353 adapter->fw_cap_info = 0;
354 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
355 adapter->event_cause = 0;
356 adapter->region_code = 0;
357 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
358 adapter->adhoc_awake_period = 0;
359 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
360 adapter->arp_filter_size = 0;
Avinash Patilede98bf2012-05-08 18:30:28 -0700361 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700362 adapter->empty_tx_q_cnt = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700363}
364
365/*
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800366 * This function sets trans_start per tx_queue
367 */
368void mwifiex_set_trans_start(struct net_device *dev)
369{
370 int i;
371
372 for (i = 0; i < dev->num_tx_queues; i++)
373 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
374
375 dev->trans_start = jiffies;
376}
377
378/*
379 * This function wakes up all queues in net_device
380 */
381void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
382 struct mwifiex_adapter *adapter)
383{
384 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700385 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800386
387 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700388
389 for (i = 0; i < netdev->num_tx_queues; i++) {
390 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
391
392 if (netif_tx_queue_stopped(txq))
393 netif_tx_wake_queue(txq);
394 }
395
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800396 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
397}
398
399/*
400 * This function stops all queues in net_device
401 */
402void mwifiex_stop_net_dev_queue(struct net_device *netdev,
403 struct mwifiex_adapter *adapter)
404{
405 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700406 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800407
408 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700409
410 for (i = 0; i < netdev->num_tx_queues; i++) {
411 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
412
413 if (!netif_tx_queue_stopped(txq))
414 netif_tx_stop_queue(txq);
415 }
416
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800417 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
418}
419
420/*
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700421 * This function releases the lock variables and frees the locks and
422 * associated locks.
423 */
424static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
425{
426 struct mwifiex_private *priv;
427 s32 i, j;
428
429 /* Free lists */
430 list_del(&adapter->cmd_free_q);
431 list_del(&adapter->cmd_pending_q);
432 list_del(&adapter->scan_pending_q);
433
434 for (i = 0; i < adapter->priv_num; i++)
435 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
436
437 for (i = 0; i < adapter->priv_num; i++) {
438 if (adapter->priv[i]) {
439 priv = adapter->priv[i];
440 for (j = 0; j < MAX_NUM_TID; ++j)
441 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
442 list_del(&priv->tx_ba_stream_tbl_ptr);
443 list_del(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700444 list_del(&priv->sta_list);
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700445 }
446 }
447}
448
449/*
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700450 * This function performs cleanup for adapter structure.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700451 *
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700452 * The cleanup is done recursively, by canceling all pending
453 * commands, freeing the member buffers previously allocated
454 * (command buffers, scan table buffer, sleep confirm command
455 * buffer), stopping the timers and calling the cleanup routines
456 * for every interface.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700457 */
458static void
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700459mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700460{
461 if (!adapter) {
462 pr_err("%s: adapter is NULL\n", __func__);
463 return;
464 }
465
466 mwifiex_cancel_all_pending_cmd(adapter);
467
468 /* Free lock variables */
469 mwifiex_free_lock_list(adapter);
470
471 /* Free command buffer */
472 dev_dbg(adapter->dev, "info: free cmd buffer\n");
473 mwifiex_free_cmd_buffer(adapter);
474
475 del_timer(&adapter->cmd_timer);
476
477 dev_dbg(adapter->dev, "info: free scan table\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700478
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700479 if (adapter->if_ops.cleanup_if)
480 adapter->if_ops.cleanup_if(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700481
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800482 if (adapter->sleep_cfm)
483 dev_kfree_skb_any(adapter->sleep_cfm);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700484}
485
486/*
487 * This function intializes the lock variables and
488 * the list heads.
489 */
490int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
491{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700492 struct mwifiex_private *priv;
493 s32 i, j;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700494
495 spin_lock_init(&adapter->mwifiex_lock);
496 spin_lock_init(&adapter->int_lock);
497 spin_lock_init(&adapter->main_proc_lock);
498 spin_lock_init(&adapter->mwifiex_cmd_lock);
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800499 spin_lock_init(&adapter->queue_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700500 for (i = 0; i < adapter->priv_num; i++) {
501 if (adapter->priv[i]) {
502 priv = adapter->priv[i];
503 spin_lock_init(&priv->rx_pkt_lock);
504 spin_lock_init(&priv->wmm.ra_list_spinlock);
505 spin_lock_init(&priv->curr_bcn_buf_lock);
Avinash Patil017a92a2012-08-03 18:06:07 -0700506 spin_lock_init(&priv->sta_list_spinlock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700507 }
508 }
509
510 /* Initialize cmd_free_q */
511 INIT_LIST_HEAD(&adapter->cmd_free_q);
512 /* Initialize cmd_pending_q */
513 INIT_LIST_HEAD(&adapter->cmd_pending_q);
514 /* Initialize scan_pending_q */
515 INIT_LIST_HEAD(&adapter->scan_pending_q);
516
517 spin_lock_init(&adapter->cmd_free_q_lock);
518 spin_lock_init(&adapter->cmd_pending_q_lock);
519 spin_lock_init(&adapter->scan_pending_q_lock);
520
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700521 skb_queue_head_init(&adapter->usb_rx_data_q);
522
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700523 for (i = 0; i < adapter->priv_num; ++i) {
524 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700525 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
526 }
527
528 for (i = 0; i < adapter->priv_num; i++) {
529 if (!adapter->priv[i])
530 continue;
531 priv = adapter->priv[i];
Andreas Fenkart6d2344e2013-04-04 20:03:51 -0700532 for (j = 0; j < MAX_NUM_TID; ++j)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700533 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700534 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
535 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700536 INIT_LIST_HEAD(&priv->sta_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700537
538 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
539 spin_lock_init(&priv->rx_reorder_tbl_lock);
540 }
541
542 return 0;
543}
544
545/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700546 * This function initializes the firmware.
547 *
548 * The following operations are performed sequentially -
549 * - Allocate adapter structure
550 * - Initialize the adapter structure
551 * - Initialize the private structure
552 * - Add BSS priority tables to the adapter structure
553 * - For each interface, send the init commands to firmware
554 * - Send the first command in command pending queue, if available
555 */
556int mwifiex_init_fw(struct mwifiex_adapter *adapter)
557{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700558 int ret;
559 struct mwifiex_private *priv;
560 u8 i, first_sta = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700561 int is_cmd_pend_q_empty;
562 unsigned long flags;
563
564 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
565
566 /* Allocate memory for member of adapter structure */
567 ret = mwifiex_allocate_adapter(adapter);
568 if (ret)
569 return -1;
570
571 /* Initialize adapter structure */
572 mwifiex_init_adapter(adapter);
573
574 for (i = 0; i < adapter->priv_num; i++) {
575 if (adapter->priv[i]) {
576 priv = adapter->priv[i];
577
578 /* Initialize private structure */
579 ret = mwifiex_init_priv(priv);
580 if (ret)
581 return -1;
582 }
583 }
Avinash Patilc6d1d872013-01-03 21:21:29 -0800584
585 if (adapter->if_ops.init_fw_port) {
586 if (adapter->if_ops.init_fw_port(adapter))
587 return -1;
588 }
589
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700590 for (i = 0; i < adapter->priv_num; i++) {
591 if (adapter->priv[i]) {
592 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
593 if (ret == -1)
594 return -1;
595
596 first_sta = false;
597 }
598 }
599
600 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
601 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
602 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
603 if (!is_cmd_pend_q_empty) {
604 /* Send the first command in queue and return */
605 if (mwifiex_main_process(adapter) != -1)
606 ret = -EINPROGRESS;
607 } else {
608 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
609 }
610
611 return ret;
612}
613
614/*
615 * This function deletes the BSS priority tables.
616 *
617 * The function traverses through all the allocated BSS priority nodes
618 * in every BSS priority table and frees them.
619 */
620static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
621{
622 int i;
623 struct mwifiex_adapter *adapter = priv->adapter;
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700624 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700625 struct list_head *head;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700626 spinlock_t *lock; /* bss priority lock */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700627 unsigned long flags;
628
629 for (i = 0; i < adapter->priv_num; ++i) {
630 head = &adapter->bss_prio_tbl[i].bss_prio_head;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700631 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
632 dev_dbg(adapter->dev, "info: delete BSS priority table,"
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800633 " bss_type = %d, bss_num = %d, i = %d,"
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700634 " head = %p\n",
635 priv->bss_type, priv->bss_num, i, head);
636
637 {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700638 spin_lock_irqsave(lock, flags);
639 if (list_empty(head)) {
640 spin_unlock_irqrestore(lock, flags);
641 continue;
642 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700643 list_for_each_entry_safe(bssprio_node, tmp_node, head,
644 list) {
645 if (bssprio_node->priv == priv) {
646 dev_dbg(adapter->dev, "info: Delete "
647 "node %p, next = %p\n",
648 bssprio_node, tmp_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700649 list_del(&bssprio_node->list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700650 kfree(bssprio_node);
651 }
652 }
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700653 spin_unlock_irqrestore(lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700654 }
655 }
656}
657
658/*
Stone Piao9197ab92012-09-25 20:23:42 -0700659 * This function frees the private structure, including cleans
660 * up the TX and RX queues and frees the BSS priority tables.
661 */
662void mwifiex_free_priv(struct mwifiex_private *priv)
663{
664 mwifiex_clean_txrx(priv);
665 mwifiex_delete_bss_prio_tbl(priv);
666 mwifiex_free_curr_bcn(priv);
667}
668
669/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700670 * This function is used to shutdown the driver.
671 *
672 * The following operations are performed sequentially -
673 * - Check if already shut down
674 * - Make sure the main process has stopped
675 * - Clean up the Tx and Rx queues
676 * - Delete BSS priority tables
677 * - Free the adapter
678 * - Notify completion
679 */
680int
681mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
682{
683 int ret = -EINPROGRESS;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700684 struct mwifiex_private *priv;
685 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700686 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700687 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700688
689 /* mwifiex already shutdown */
690 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
691 return 0;
692
693 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
694 /* wait for mwifiex_process to complete */
695 if (adapter->mwifiex_processing) {
696 dev_warn(adapter->dev, "main process is still running\n");
697 return ret;
698 }
699
Bing Zhao084c7182013-03-15 18:47:07 -0700700 /* cancel current command */
701 if (adapter->curr_cmd) {
702 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
703 del_timer(&adapter->cmd_timer);
Bing Zhao9908b072013-04-01 12:44:46 -0700704 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
Bing Zhao084c7182013-03-15 18:47:07 -0700705 adapter->curr_cmd = NULL;
706 }
707
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700708 /* shut down mwifiex */
709 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
710
711 /* Clean up Tx/Rx queues and delete BSS priority table */
712 for (i = 0; i < adapter->priv_num; i++) {
713 if (adapter->priv[i]) {
714 priv = adapter->priv[i];
715
716 mwifiex_clean_txrx(priv);
717 mwifiex_delete_bss_prio_tbl(priv);
718 }
719 }
720
721 spin_lock_irqsave(&adapter->mwifiex_lock, flags);
722
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700723 if (adapter->if_ops.data_complete) {
724 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
725 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
726
727 priv = adapter->priv[rx_info->bss_num];
728 if (priv)
729 priv->stats.rx_dropped++;
730
731 adapter->if_ops.data_complete(adapter, skb);
732 }
733 }
734
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700735 mwifiex_adapter_cleanup(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700736
737 spin_unlock_irqrestore(&adapter->mwifiex_lock, flags);
738
739 /* Notify completion */
740 ret = mwifiex_shutdown_fw_complete(adapter);
741
742 return ret;
743}
744
745/*
746 * This function downloads the firmware to the card.
747 *
748 * The actual download is preceded by two sanity checks -
749 * - Check if firmware is already running
750 * - Check if the interface is the winner to download the firmware
751 *
752 * ...and followed by another -
753 * - Check if the firmware is downloaded successfully
754 *
755 * After download is successfully completed, the host interrupts are enabled.
756 */
757int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
758 struct mwifiex_fw_image *pmfw)
759{
Amitkumar Karward930fae2011-10-11 17:41:21 -0700760 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700761 u32 poll_num = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700762
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700763 if (adapter->if_ops.check_fw_status) {
764 adapter->winner = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700765
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700766 /* check if firmware is already running */
767 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
768 if (!ret) {
769 dev_notice(adapter->dev,
770 "WLAN FW already running! Skip FW dnld\n");
771 goto done;
772 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700773
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700774 poll_num = MAX_FIRMWARE_POLL_TRIES;
775
776 /* check if we are the winner for downloading FW */
777 if (!adapter->winner) {
778 dev_notice(adapter->dev,
779 "FW already running! Skip FW dnld\n");
780 poll_num = MAX_MULTI_INTERFACE_POLL_TRIES;
781 goto poll_fw;
782 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700783 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700784
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700785 if (pmfw) {
786 /* Download firmware with helper */
787 ret = adapter->if_ops.prog_fw(adapter, pmfw);
788 if (ret) {
789 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
790 return ret;
791 }
792 }
793
794poll_fw:
795 /* Check if the firmware is downloaded successfully or not */
Amitkumar Karward930fae2011-10-11 17:41:21 -0700796 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700797 if (ret) {
798 dev_err(adapter->dev, "FW failed to be active in time\n");
799 return -1;
800 }
801done:
802 /* re-enable host interrupt for mwifiex after fw dnld is successful */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700803 if (adapter->if_ops.enable_int)
804 adapter->if_ops.enable_int(adapter);
805
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700806 return ret;
807}