blob: 71bbf120468564cce21c7bc3c4358376b816b6c4 [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 Karwar06041112013-05-17 17:50:19 -070062 if (adapter->surprise_removed)
63 return;
64
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070065 if (adapter->scan_delay_cnt == MWIFIEX_MAX_SCAN_DELAY_CNT) {
66 /*
67 * Abort scan operation by cancelling all pending scan
68 * commands
69 */
70 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
71 list_for_each_entry_safe(cmd_node, tmp_node,
72 &adapter->scan_pending_q, list) {
73 list_del(&cmd_node->list);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070074 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
75 }
76 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
77
78 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
79 adapter->scan_processing = false;
80 adapter->scan_delay_cnt = 0;
81 adapter->empty_tx_q_cnt = 0;
82 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
83
84 if (priv->user_scan_cfg) {
Amitkumar Karwarf162cac2012-10-19 19:19:16 -070085 if (priv->scan_request) {
86 dev_dbg(priv->adapter->dev,
87 "info: aborting scan\n");
88 cfg80211_scan_done(priv->scan_request, 1);
89 priv->scan_request = NULL;
90 } else {
91 dev_dbg(priv->adapter->dev,
92 "info: scan already aborted\n");
93 }
94
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -070095 kfree(priv->user_scan_cfg);
96 priv->user_scan_cfg = NULL;
97 }
98 goto done;
99 }
100
101 if (!atomic_read(&priv->adapter->is_tx_received)) {
102 adapter->empty_tx_q_cnt++;
103 if (adapter->empty_tx_q_cnt == MWIFIEX_MAX_EMPTY_TX_Q_CNT) {
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700104 /*
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700105 * No Tx traffic for 200msec. Get scan command from
106 * scan pending queue and put to cmd pending queue to
107 * resume scan operation
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700108 */
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700109 adapter->scan_delay_cnt = 0;
110 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700111 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700112 cmd_node = list_first_entry(&adapter->scan_pending_q,
113 struct cmd_ctrl_node, list);
114 list_del(&cmd_node->list);
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700115 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
116 flags);
117
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700118 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
119 true);
Amitkumar Karwar9a17bad2012-09-18 15:33:32 -0700120 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700121 goto done;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700122 }
123 } else {
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700124 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700125 }
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700126
127 /* Delay scan operation further by 20msec */
128 mod_timer(&priv->scan_delay_timer, jiffies +
129 msecs_to_jiffies(MWIFIEX_SCAN_DELAY_MSEC));
130 adapter->scan_delay_cnt++;
131
132done:
133 if (atomic_read(&priv->adapter->is_tx_received))
134 atomic_set(&priv->adapter->is_tx_received, false);
135
136 return;
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700137}
138
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700139/*
140 * This function initializes the private structure and sets default
141 * values to the members.
142 *
143 * Additionally, it also initializes all the locks and sets up all the
144 * lists.
145 */
Stone Piao9197ab92012-09-25 20:23:42 -0700146int mwifiex_init_priv(struct mwifiex_private *priv)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700147{
148 u32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700149
150 priv->media_connected = false;
151 memset(priv->curr_addr, 0xff, ETH_ALEN);
152
153 priv->pkt_tx_ctrl = 0;
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700154 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700155 priv->data_rate = 0; /* Initially indicate the rate as auto */
156 priv->is_data_rate_auto = true;
157 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
158 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
159
Amitkumar Karwar5eb02e42012-02-24 21:36:04 -0800160 priv->sec_info.wep_enabled = 0;
Marc Yangf986b6d2011-03-28 17:55:42 -0700161 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
Yogesh Ashok Powar2be50b82011-04-01 18:36:47 -0700162 priv->sec_info.encryption_mode = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700163 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
164 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
165 priv->wep_key_curr_index = 0;
166 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
167 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
168
169 priv->beacon_period = 100; /* beacon interval */ ;
170 priv->attempted_bss_desc = NULL;
171 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
172 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
173
174 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
175 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
176 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
177 priv->assoc_rsp_size = 0;
178 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
179 priv->atim_window = 0;
180 priv->adhoc_state = ADHOC_IDLE;
181 priv->tx_power_level = 0;
182 priv->max_tx_power_level = 0;
183 priv->min_tx_power_level = 0;
184 priv->tx_rate = 0;
185 priv->rxpd_htinfo = 0;
186 priv->rxpd_rate = 0;
187 priv->rate_bitmap = 0;
188 priv->data_rssi_last = 0;
189 priv->data_rssi_avg = 0;
190 priv->data_nf_avg = 0;
191 priv->data_nf_last = 0;
192 priv->bcn_rssi_last = 0;
193 priv->bcn_rssi_avg = 0;
194 priv->bcn_nf_avg = 0;
195 priv->bcn_nf_last = 0;
196 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
197 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
198 priv->wpa_ie_len = 0;
199 priv->wpa_is_gtk_set = false;
200
201 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
202 priv->assoc_tlv_buf_len = 0;
203 memset(&priv->wps, 0, sizeof(priv->wps));
204 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
205 priv->gen_ie_buf_len = 0;
206 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
207
208 priv->wmm_required = true;
209 priv->wmm_enabled = false;
210 priv->wmm_qosinfo = 0;
211 priv->curr_bcn_buf = NULL;
212 priv->curr_bcn_size = 0;
Avinash Patil13d7ba72012-04-09 20:06:56 -0700213 priv->wps_ie = NULL;
214 priv->wps_ie_len = 0;
Avinash Patilc8258912012-08-03 18:06:05 -0700215 priv->ap_11n_enabled = 0;
Stone Piao7feb4c42012-09-25 20:23:36 -0700216 memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
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
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700318 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
319 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
320
321 adapter->is_hs_configured = false;
Amitkumar Karwarcc0b5a62013-03-04 16:27:57 -0800322 adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
323 adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
324 adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700325 adapter->hs_activated = false;
326
327 memset(adapter->event_body, 0, sizeof(adapter->event_body));
328 adapter->hw_dot_11n_dev_cap = 0;
329 adapter->hw_dev_mcs_support = 0;
Amitkumar Karwar21c3ba32011-12-20 23:47:21 -0800330 adapter->sec_chan_offset = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700331 adapter->adhoc_11n_enabled = false;
332
333 mwifiex_wmm_init(adapter);
334
335 if (adapter->sleep_cfm) {
Yogesh Ashok Powar8ed13032011-11-07 21:41:10 -0800336 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
337 adapter->sleep_cfm->data;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700338 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
339 sleep_cfm_buf->command =
340 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
341 sleep_cfm_buf->size =
342 cpu_to_le16(adapter->sleep_cfm->len);
343 sleep_cfm_buf->result = 0;
344 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
345 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700346 }
347 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
348 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
349 adapter->tx_lock_flag = false;
350 adapter->null_pkt_interval = 0;
351 adapter->fw_bands = 0;
352 adapter->config_bands = 0;
353 adapter->adhoc_start_band = 0;
354 adapter->scan_channels = NULL;
355 adapter->fw_release_number = 0;
356 adapter->fw_cap_info = 0;
357 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
358 adapter->event_cause = 0;
359 adapter->region_code = 0;
360 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
361 adapter->adhoc_awake_period = 0;
362 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
363 adapter->arp_filter_size = 0;
Avinash Patilede98bf2012-05-08 18:30:28 -0700364 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700365 adapter->empty_tx_q_cnt = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700366}
367
368/*
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800369 * This function sets trans_start per tx_queue
370 */
371void mwifiex_set_trans_start(struct net_device *dev)
372{
373 int i;
374
375 for (i = 0; i < dev->num_tx_queues; i++)
376 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
377
378 dev->trans_start = jiffies;
379}
380
381/*
382 * This function wakes up all queues in net_device
383 */
384void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
385 struct mwifiex_adapter *adapter)
386{
387 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700388 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800389
390 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700391
392 for (i = 0; i < netdev->num_tx_queues; i++) {
393 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
394
395 if (netif_tx_queue_stopped(txq))
396 netif_tx_wake_queue(txq);
397 }
398
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800399 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
400}
401
402/*
403 * This function stops all queues in net_device
404 */
405void mwifiex_stop_net_dev_queue(struct net_device *netdev,
406 struct mwifiex_adapter *adapter)
407{
408 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700409 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800410
411 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700412
413 for (i = 0; i < netdev->num_tx_queues; i++) {
414 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
415
416 if (!netif_tx_queue_stopped(txq))
417 netif_tx_stop_queue(txq);
418 }
419
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800420 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
421}
422
423/*
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700424 * This function releases the lock variables and frees the locks and
425 * associated locks.
426 */
427static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
428{
429 struct mwifiex_private *priv;
430 s32 i, j;
431
432 /* Free lists */
433 list_del(&adapter->cmd_free_q);
434 list_del(&adapter->cmd_pending_q);
435 list_del(&adapter->scan_pending_q);
436
437 for (i = 0; i < adapter->priv_num; i++)
438 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
439
440 for (i = 0; i < adapter->priv_num; i++) {
441 if (adapter->priv[i]) {
442 priv = adapter->priv[i];
443 for (j = 0; j < MAX_NUM_TID; ++j)
444 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
445 list_del(&priv->tx_ba_stream_tbl_ptr);
446 list_del(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700447 list_del(&priv->sta_list);
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700448 }
449 }
450}
451
452/*
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700453 * This function performs cleanup for adapter structure.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700454 *
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700455 * The cleanup is done recursively, by canceling all pending
456 * commands, freeing the member buffers previously allocated
457 * (command buffers, scan table buffer, sleep confirm command
458 * buffer), stopping the timers and calling the cleanup routines
459 * for every interface.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700460 */
461static void
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700462mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700463{
Amitkumar Karwar06041112013-05-17 17:50:19 -0700464 int i;
465
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700466 if (!adapter) {
467 pr_err("%s: adapter is NULL\n", __func__);
468 return;
469 }
470
Amitkumar Karwar06041112013-05-17 17:50:19 -0700471 for (i = 0; i < adapter->priv_num; i++) {
472 if (adapter->priv[i])
473 del_timer_sync(&adapter->priv[i]->scan_delay_timer);
474 }
475
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700476 mwifiex_cancel_all_pending_cmd(adapter);
477
478 /* Free lock variables */
479 mwifiex_free_lock_list(adapter);
480
481 /* Free command buffer */
482 dev_dbg(adapter->dev, "info: free cmd buffer\n");
483 mwifiex_free_cmd_buffer(adapter);
484
485 del_timer(&adapter->cmd_timer);
486
487 dev_dbg(adapter->dev, "info: free scan table\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700488
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700489 if (adapter->if_ops.cleanup_if)
490 adapter->if_ops.cleanup_if(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700491
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800492 if (adapter->sleep_cfm)
493 dev_kfree_skb_any(adapter->sleep_cfm);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700494}
495
496/*
497 * This function intializes the lock variables and
498 * the list heads.
499 */
500int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
501{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700502 struct mwifiex_private *priv;
503 s32 i, j;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700504
505 spin_lock_init(&adapter->mwifiex_lock);
506 spin_lock_init(&adapter->int_lock);
507 spin_lock_init(&adapter->main_proc_lock);
508 spin_lock_init(&adapter->mwifiex_cmd_lock);
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800509 spin_lock_init(&adapter->queue_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510 for (i = 0; i < adapter->priv_num; i++) {
511 if (adapter->priv[i]) {
512 priv = adapter->priv[i];
513 spin_lock_init(&priv->rx_pkt_lock);
514 spin_lock_init(&priv->wmm.ra_list_spinlock);
515 spin_lock_init(&priv->curr_bcn_buf_lock);
Avinash Patil017a92a2012-08-03 18:06:07 -0700516 spin_lock_init(&priv->sta_list_spinlock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700517 }
518 }
519
520 /* Initialize cmd_free_q */
521 INIT_LIST_HEAD(&adapter->cmd_free_q);
522 /* Initialize cmd_pending_q */
523 INIT_LIST_HEAD(&adapter->cmd_pending_q);
524 /* Initialize scan_pending_q */
525 INIT_LIST_HEAD(&adapter->scan_pending_q);
526
527 spin_lock_init(&adapter->cmd_free_q_lock);
528 spin_lock_init(&adapter->cmd_pending_q_lock);
529 spin_lock_init(&adapter->scan_pending_q_lock);
530
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700531 skb_queue_head_init(&adapter->usb_rx_data_q);
532
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700533 for (i = 0; i < adapter->priv_num; ++i) {
534 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700535 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
536 }
537
538 for (i = 0; i < adapter->priv_num; i++) {
539 if (!adapter->priv[i])
540 continue;
541 priv = adapter->priv[i];
Andreas Fenkart6d2344e2013-04-04 20:03:51 -0700542 for (j = 0; j < MAX_NUM_TID; ++j)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700543 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700544 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
545 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700546 INIT_LIST_HEAD(&priv->sta_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700547
548 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
549 spin_lock_init(&priv->rx_reorder_tbl_lock);
550 }
551
552 return 0;
553}
554
555/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700556 * This function initializes the firmware.
557 *
558 * The following operations are performed sequentially -
559 * - Allocate adapter structure
560 * - Initialize the adapter structure
561 * - Initialize the private structure
562 * - Add BSS priority tables to the adapter structure
563 * - For each interface, send the init commands to firmware
564 * - Send the first command in command pending queue, if available
565 */
566int mwifiex_init_fw(struct mwifiex_adapter *adapter)
567{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700568 int ret;
569 struct mwifiex_private *priv;
570 u8 i, first_sta = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700571 int is_cmd_pend_q_empty;
572 unsigned long flags;
573
574 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
575
576 /* Allocate memory for member of adapter structure */
577 ret = mwifiex_allocate_adapter(adapter);
578 if (ret)
579 return -1;
580
581 /* Initialize adapter structure */
582 mwifiex_init_adapter(adapter);
583
584 for (i = 0; i < adapter->priv_num; i++) {
585 if (adapter->priv[i]) {
586 priv = adapter->priv[i];
587
588 /* Initialize private structure */
589 ret = mwifiex_init_priv(priv);
590 if (ret)
591 return -1;
592 }
593 }
Avinash Patilc6d1d872013-01-03 21:21:29 -0800594
595 if (adapter->if_ops.init_fw_port) {
596 if (adapter->if_ops.init_fw_port(adapter))
597 return -1;
598 }
599
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600 for (i = 0; i < adapter->priv_num; i++) {
601 if (adapter->priv[i]) {
602 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
603 if (ret == -1)
604 return -1;
605
606 first_sta = false;
607 }
608 }
609
610 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
611 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
612 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
613 if (!is_cmd_pend_q_empty) {
614 /* Send the first command in queue and return */
615 if (mwifiex_main_process(adapter) != -1)
616 ret = -EINPROGRESS;
617 } else {
618 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
619 }
620
621 return ret;
622}
623
624/*
625 * This function deletes the BSS priority tables.
626 *
627 * The function traverses through all the allocated BSS priority nodes
628 * in every BSS priority table and frees them.
629 */
630static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
631{
632 int i;
633 struct mwifiex_adapter *adapter = priv->adapter;
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700634 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700635 struct list_head *head;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700636 spinlock_t *lock; /* bss priority lock */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700637 unsigned long flags;
638
639 for (i = 0; i < adapter->priv_num; ++i) {
640 head = &adapter->bss_prio_tbl[i].bss_prio_head;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700641 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
642 dev_dbg(adapter->dev, "info: delete BSS priority table,"
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800643 " bss_type = %d, bss_num = %d, i = %d,"
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700644 " head = %p\n",
645 priv->bss_type, priv->bss_num, i, head);
646
647 {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700648 spin_lock_irqsave(lock, flags);
649 if (list_empty(head)) {
650 spin_unlock_irqrestore(lock, flags);
651 continue;
652 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700653 list_for_each_entry_safe(bssprio_node, tmp_node, head,
654 list) {
655 if (bssprio_node->priv == priv) {
656 dev_dbg(adapter->dev, "info: Delete "
657 "node %p, next = %p\n",
658 bssprio_node, tmp_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700659 list_del(&bssprio_node->list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700660 kfree(bssprio_node);
661 }
662 }
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700663 spin_unlock_irqrestore(lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664 }
665 }
666}
667
668/*
Stone Piao9197ab92012-09-25 20:23:42 -0700669 * This function frees the private structure, including cleans
670 * up the TX and RX queues and frees the BSS priority tables.
671 */
672void mwifiex_free_priv(struct mwifiex_private *priv)
673{
674 mwifiex_clean_txrx(priv);
675 mwifiex_delete_bss_prio_tbl(priv);
676 mwifiex_free_curr_bcn(priv);
677}
678
679/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700680 * This function is used to shutdown the driver.
681 *
682 * The following operations are performed sequentially -
683 * - Check if already shut down
684 * - Make sure the main process has stopped
685 * - Clean up the Tx and Rx queues
686 * - Delete BSS priority tables
687 * - Free the adapter
688 * - Notify completion
689 */
690int
691mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
692{
693 int ret = -EINPROGRESS;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700694 struct mwifiex_private *priv;
695 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700697 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700698
699 /* mwifiex already shutdown */
700 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
701 return 0;
702
703 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
704 /* wait for mwifiex_process to complete */
705 if (adapter->mwifiex_processing) {
706 dev_warn(adapter->dev, "main process is still running\n");
707 return ret;
708 }
709
Bing Zhao084c7182013-03-15 18:47:07 -0700710 /* cancel current command */
711 if (adapter->curr_cmd) {
712 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
713 del_timer(&adapter->cmd_timer);
Bing Zhao9908b072013-04-01 12:44:46 -0700714 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
Bing Zhao084c7182013-03-15 18:47:07 -0700715 adapter->curr_cmd = NULL;
716 }
717
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700718 /* shut down mwifiex */
719 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
720
721 /* Clean up Tx/Rx queues and delete BSS priority table */
722 for (i = 0; i < adapter->priv_num; i++) {
723 if (adapter->priv[i]) {
724 priv = adapter->priv[i];
725
726 mwifiex_clean_txrx(priv);
727 mwifiex_delete_bss_prio_tbl(priv);
728 }
729 }
730
731 spin_lock_irqsave(&adapter->mwifiex_lock, flags);
732
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700733 if (adapter->if_ops.data_complete) {
734 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
735 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
736
737 priv = adapter->priv[rx_info->bss_num];
738 if (priv)
739 priv->stats.rx_dropped++;
740
741 adapter->if_ops.data_complete(adapter, skb);
742 }
743 }
744
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700745 mwifiex_adapter_cleanup(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700746
747 spin_unlock_irqrestore(&adapter->mwifiex_lock, flags);
748
749 /* Notify completion */
750 ret = mwifiex_shutdown_fw_complete(adapter);
751
752 return ret;
753}
754
755/*
756 * This function downloads the firmware to the card.
757 *
758 * The actual download is preceded by two sanity checks -
759 * - Check if firmware is already running
760 * - Check if the interface is the winner to download the firmware
761 *
762 * ...and followed by another -
763 * - Check if the firmware is downloaded successfully
764 *
765 * After download is successfully completed, the host interrupts are enabled.
766 */
767int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
768 struct mwifiex_fw_image *pmfw)
769{
Amitkumar Karward930fae2011-10-11 17:41:21 -0700770 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700771 u32 poll_num = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700772
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700773 if (adapter->if_ops.check_fw_status) {
774 adapter->winner = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700775
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700776 /* check if firmware is already running */
777 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
778 if (!ret) {
779 dev_notice(adapter->dev,
780 "WLAN FW already running! Skip FW dnld\n");
781 goto done;
782 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700783
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700784 poll_num = MAX_FIRMWARE_POLL_TRIES;
785
786 /* check if we are the winner for downloading FW */
787 if (!adapter->winner) {
788 dev_notice(adapter->dev,
789 "FW already running! Skip FW dnld\n");
790 poll_num = MAX_MULTI_INTERFACE_POLL_TRIES;
791 goto poll_fw;
792 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700793 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700794
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700795 if (pmfw) {
796 /* Download firmware with helper */
797 ret = adapter->if_ops.prog_fw(adapter, pmfw);
798 if (ret) {
799 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
800 return ret;
801 }
802 }
803
804poll_fw:
805 /* Check if the firmware is downloaded successfully or not */
Amitkumar Karward930fae2011-10-11 17:41:21 -0700806 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700807 if (ret) {
808 dev_err(adapter->dev, "FW failed to be active in time\n");
809 return -1;
810 }
811done:
812 /* re-enable host interrupt for mwifiex after fw dnld is successful */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700813 if (adapter->if_ops.enable_int)
814 adapter->if_ops.enable_int(adapter);
815
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700816 return ret;
817}