blob: 308c56fd32eb539ac1ae439758132d2ece700f8f [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
55/*
56 * This function initializes the private structure and sets default
57 * values to the members.
58 *
59 * Additionally, it also initializes all the locks and sets up all the
60 * lists.
61 */
Stone Piao9197ab92012-09-25 20:23:42 -070062int mwifiex_init_priv(struct mwifiex_private *priv)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070063{
64 u32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070065
66 priv->media_connected = false;
67 memset(priv->curr_addr, 0xff, ETH_ALEN);
68
69 priv->pkt_tx_ctrl = 0;
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070070 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071 priv->data_rate = 0; /* Initially indicate the rate as auto */
72 priv->is_data_rate_auto = true;
73 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
74 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
75
Amitkumar Karwar5eb02e42012-02-24 21:36:04 -080076 priv->sec_info.wep_enabled = 0;
Marc Yangf986b6d2011-03-28 17:55:42 -070077 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
Yogesh Ashok Powar2be50b82011-04-01 18:36:47 -070078 priv->sec_info.encryption_mode = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070079 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
80 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
81 priv->wep_key_curr_index = 0;
82 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
83 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
84
85 priv->beacon_period = 100; /* beacon interval */ ;
86 priv->attempted_bss_desc = NULL;
87 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
88 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
89
90 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
91 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
92 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
93 priv->assoc_rsp_size = 0;
94 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
95 priv->atim_window = 0;
96 priv->adhoc_state = ADHOC_IDLE;
97 priv->tx_power_level = 0;
98 priv->max_tx_power_level = 0;
99 priv->min_tx_power_level = 0;
100 priv->tx_rate = 0;
101 priv->rxpd_htinfo = 0;
102 priv->rxpd_rate = 0;
103 priv->rate_bitmap = 0;
104 priv->data_rssi_last = 0;
105 priv->data_rssi_avg = 0;
106 priv->data_nf_avg = 0;
107 priv->data_nf_last = 0;
108 priv->bcn_rssi_last = 0;
109 priv->bcn_rssi_avg = 0;
110 priv->bcn_nf_avg = 0;
111 priv->bcn_nf_last = 0;
112 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
113 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
114 priv->wpa_ie_len = 0;
115 priv->wpa_is_gtk_set = false;
116
117 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
118 priv->assoc_tlv_buf_len = 0;
119 memset(&priv->wps, 0, sizeof(priv->wps));
120 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
121 priv->gen_ie_buf_len = 0;
122 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
123
124 priv->wmm_required = true;
125 priv->wmm_enabled = false;
126 priv->wmm_qosinfo = 0;
127 priv->curr_bcn_buf = NULL;
128 priv->curr_bcn_size = 0;
Avinash Patil13d7ba72012-04-09 20:06:56 -0700129 priv->wps_ie = NULL;
130 priv->wps_ie_len = 0;
Avinash Patilc8258912012-08-03 18:06:05 -0700131 priv->ap_11n_enabled = 0;
Stone Piao7feb4c42012-09-25 20:23:36 -0700132 memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700133
134 priv->scan_block = false;
135
Amitkumar Karwarb8876642013-06-18 16:36:58 -0700136 priv->csa_chan = 0;
137 priv->csa_expire_time = 0;
Avinash Patil61c873042013-07-22 19:17:41 -0700138 priv->del_list_idx = 0;
Avinash Patil587b36d2013-08-23 16:48:23 -0700139 priv->hs2_enabled = false;
Amitkumar Karwarb8876642013-06-18 16:36:58 -0700140
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700141 return mwifiex_add_bss_prio_tbl(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700142}
143
144/*
145 * This function allocates buffers for members of the adapter
146 * structure.
147 *
148 * The memory allocated includes scan table, command buffers, and
149 * sleep confirm command buffer. In addition, the queues are
150 * also initialized.
151 */
152static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
153{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700154 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700155
156 /* Allocate command buffer */
157 ret = mwifiex_alloc_cmd_buffer(adapter);
158 if (ret) {
159 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700160 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700161 return -1;
162 }
163
164 adapter->sleep_cfm =
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700165 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700166 + INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700167
168 if (!adapter->sleep_cfm) {
169 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
170 " cmd buffer\n", __func__);
171 return -1;
172 }
173 skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
174
175 return 0;
176}
177
178/*
179 * This function initializes the adapter structure and sets default
180 * values to the members of adapter.
181 *
182 * This also initializes the WMM related parameters in the driver private
183 * structures.
184 */
185static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
186{
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700187 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700188
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700189 skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700190
191 adapter->cmd_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700192
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700193 if (adapter->iface_type == MWIFIEX_SDIO)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700194 adapter->data_sent = true;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700195 else
196 adapter->data_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700197
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700198 adapter->cmd_resp_received = false;
199 adapter->event_received = false;
200 adapter->data_received = false;
201
202 adapter->surprise_removed = false;
203
204 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
205
206 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
207 adapter->ps_state = PS_STATE_AWAKE;
208 adapter->need_to_wakeup = false;
209
210 adapter->scan_mode = HostCmd_BSS_MODE_ANY;
211 adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
212 adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
213 adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
214
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700215 adapter->scan_probes = 1;
216
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700217 adapter->multiple_dtim = 1;
218
219 adapter->local_listen_interval = 0; /* default value in firmware
220 will be used */
221
222 adapter->is_deep_sleep = false;
223
224 adapter->delay_null_pkt = false;
225 adapter->delay_to_ps = 1000;
226 adapter->enhanced_ps_mode = PS_MODE_AUTO;
227
228 adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
229 default */
230 adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
231 default */
232 adapter->pm_wakeup_card_req = false;
233
234 adapter->pm_wakeup_fw_try = false;
235
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700236 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
237 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
238
239 adapter->is_hs_configured = false;
Amitkumar Karwarcc0b5a62013-03-04 16:27:57 -0800240 adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
241 adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
242 adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700243 adapter->hs_activated = false;
244
245 memset(adapter->event_body, 0, sizeof(adapter->event_body));
246 adapter->hw_dot_11n_dev_cap = 0;
247 adapter->hw_dev_mcs_support = 0;
Amitkumar Karwar21c3ba32011-12-20 23:47:21 -0800248 adapter->sec_chan_offset = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700249 adapter->adhoc_11n_enabled = false;
250
251 mwifiex_wmm_init(adapter);
252
253 if (adapter->sleep_cfm) {
Yogesh Ashok Powar8ed13032011-11-07 21:41:10 -0800254 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
255 adapter->sleep_cfm->data;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700256 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
257 sleep_cfm_buf->command =
258 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
259 sleep_cfm_buf->size =
260 cpu_to_le16(adapter->sleep_cfm->len);
261 sleep_cfm_buf->result = 0;
262 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
263 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700264 }
265 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
266 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
267 adapter->tx_lock_flag = false;
268 adapter->null_pkt_interval = 0;
269 adapter->fw_bands = 0;
270 adapter->config_bands = 0;
271 adapter->adhoc_start_band = 0;
272 adapter->scan_channels = NULL;
273 adapter->fw_release_number = 0;
274 adapter->fw_cap_info = 0;
275 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
276 adapter->event_cause = 0;
277 adapter->region_code = 0;
278 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
279 adapter->adhoc_awake_period = 0;
280 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
281 adapter->arp_filter_size = 0;
Avinash Patilede98bf2012-05-08 18:30:28 -0700282 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
Amitkumar Karwarbdd37be2012-08-03 18:06:04 -0700283 adapter->empty_tx_q_cnt = 0;
Amitkumar Karwar21f58d202014-02-11 18:39:56 -0800284 adapter->ext_scan = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700285}
286
287/*
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800288 * This function sets trans_start per tx_queue
289 */
290void mwifiex_set_trans_start(struct net_device *dev)
291{
292 int i;
293
294 for (i = 0; i < dev->num_tx_queues; i++)
295 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
296
297 dev->trans_start = jiffies;
298}
299
300/*
301 * This function wakes up all queues in net_device
302 */
303void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
304 struct mwifiex_adapter *adapter)
305{
306 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700307 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800308
309 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700310
311 for (i = 0; i < netdev->num_tx_queues; i++) {
312 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
313
314 if (netif_tx_queue_stopped(txq))
315 netif_tx_wake_queue(txq);
316 }
317
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800318 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
319}
320
321/*
322 * This function stops all queues in net_device
323 */
324void mwifiex_stop_net_dev_queue(struct net_device *netdev,
325 struct mwifiex_adapter *adapter)
326{
327 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700328 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800329
330 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700331
332 for (i = 0; i < netdev->num_tx_queues; i++) {
333 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
334
335 if (!netif_tx_queue_stopped(txq))
336 netif_tx_stop_queue(txq);
337 }
338
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800339 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
340}
341
342/*
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700343 * This function releases the lock variables and frees the locks and
344 * associated locks.
345 */
346static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
347{
348 struct mwifiex_private *priv;
349 s32 i, j;
350
351 /* Free lists */
352 list_del(&adapter->cmd_free_q);
353 list_del(&adapter->cmd_pending_q);
354 list_del(&adapter->scan_pending_q);
355
356 for (i = 0; i < adapter->priv_num; i++)
357 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
358
359 for (i = 0; i < adapter->priv_num; i++) {
360 if (adapter->priv[i]) {
361 priv = adapter->priv[i];
362 for (j = 0; j < MAX_NUM_TID; ++j)
363 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
364 list_del(&priv->tx_ba_stream_tbl_ptr);
365 list_del(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700366 list_del(&priv->sta_list);
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700367 }
368 }
369}
370
371/*
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700372 * This function performs cleanup for adapter structure.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700373 *
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700374 * The cleanup is done recursively, by canceling all pending
375 * commands, freeing the member buffers previously allocated
376 * (command buffers, scan table buffer, sleep confirm command
377 * buffer), stopping the timers and calling the cleanup routines
378 * for every interface.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700379 */
380static void
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700381mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700382{
383 if (!adapter) {
384 pr_err("%s: adapter is NULL\n", __func__);
385 return;
386 }
387
388 mwifiex_cancel_all_pending_cmd(adapter);
389
390 /* Free lock variables */
391 mwifiex_free_lock_list(adapter);
392
393 /* Free command buffer */
394 dev_dbg(adapter->dev, "info: free cmd buffer\n");
395 mwifiex_free_cmd_buffer(adapter);
396
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700397 dev_dbg(adapter->dev, "info: free scan table\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700398
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800399 if (adapter->sleep_cfm)
400 dev_kfree_skb_any(adapter->sleep_cfm);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700401}
402
403/*
404 * This function intializes the lock variables and
405 * the list heads.
406 */
407int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
408{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700409 struct mwifiex_private *priv;
410 s32 i, j;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700411
412 spin_lock_init(&adapter->mwifiex_lock);
413 spin_lock_init(&adapter->int_lock);
414 spin_lock_init(&adapter->main_proc_lock);
415 spin_lock_init(&adapter->mwifiex_cmd_lock);
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800416 spin_lock_init(&adapter->queue_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700417 for (i = 0; i < adapter->priv_num; i++) {
418 if (adapter->priv[i]) {
419 priv = adapter->priv[i];
420 spin_lock_init(&priv->rx_pkt_lock);
421 spin_lock_init(&priv->wmm.ra_list_spinlock);
422 spin_lock_init(&priv->curr_bcn_buf_lock);
Avinash Patil017a92a2012-08-03 18:06:07 -0700423 spin_lock_init(&priv->sta_list_spinlock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700424 }
425 }
426
427 /* Initialize cmd_free_q */
428 INIT_LIST_HEAD(&adapter->cmd_free_q);
429 /* Initialize cmd_pending_q */
430 INIT_LIST_HEAD(&adapter->cmd_pending_q);
431 /* Initialize scan_pending_q */
432 INIT_LIST_HEAD(&adapter->scan_pending_q);
433
434 spin_lock_init(&adapter->cmd_free_q_lock);
435 spin_lock_init(&adapter->cmd_pending_q_lock);
436 spin_lock_init(&adapter->scan_pending_q_lock);
437
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700438 skb_queue_head_init(&adapter->usb_rx_data_q);
439
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700440 for (i = 0; i < adapter->priv_num; ++i) {
441 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700442 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
443 }
444
445 for (i = 0; i < adapter->priv_num; i++) {
446 if (!adapter->priv[i])
447 continue;
448 priv = adapter->priv[i];
Andreas Fenkart6d2344e2013-04-04 20:03:51 -0700449 for (j = 0; j < MAX_NUM_TID; ++j)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700450 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700451 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
452 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700453 INIT_LIST_HEAD(&priv->sta_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700454
455 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
456 spin_lock_init(&priv->rx_reorder_tbl_lock);
457 }
458
459 return 0;
460}
461
462/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700463 * This function initializes the firmware.
464 *
465 * The following operations are performed sequentially -
466 * - Allocate adapter structure
467 * - Initialize the adapter structure
468 * - Initialize the private structure
469 * - Add BSS priority tables to the adapter structure
470 * - For each interface, send the init commands to firmware
471 * - Send the first command in command pending queue, if available
472 */
473int mwifiex_init_fw(struct mwifiex_adapter *adapter)
474{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700475 int ret;
476 struct mwifiex_private *priv;
477 u8 i, first_sta = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700478 int is_cmd_pend_q_empty;
479 unsigned long flags;
480
481 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
482
483 /* Allocate memory for member of adapter structure */
484 ret = mwifiex_allocate_adapter(adapter);
485 if (ret)
486 return -1;
487
488 /* Initialize adapter structure */
489 mwifiex_init_adapter(adapter);
490
491 for (i = 0; i < adapter->priv_num; i++) {
492 if (adapter->priv[i]) {
493 priv = adapter->priv[i];
494
495 /* Initialize private structure */
496 ret = mwifiex_init_priv(priv);
497 if (ret)
498 return -1;
499 }
500 }
Avinash Patilc6d1d872013-01-03 21:21:29 -0800501
502 if (adapter->if_ops.init_fw_port) {
503 if (adapter->if_ops.init_fw_port(adapter))
504 return -1;
505 }
506
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700507 for (i = 0; i < adapter->priv_num; i++) {
508 if (adapter->priv[i]) {
509 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
510 if (ret == -1)
511 return -1;
512
513 first_sta = false;
514 }
515 }
516
517 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
518 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
519 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
520 if (!is_cmd_pend_q_empty) {
521 /* Send the first command in queue and return */
522 if (mwifiex_main_process(adapter) != -1)
523 ret = -EINPROGRESS;
524 } else {
525 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
526 }
527
528 return ret;
529}
530
531/*
532 * This function deletes the BSS priority tables.
533 *
534 * The function traverses through all the allocated BSS priority nodes
535 * in every BSS priority table and frees them.
536 */
537static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
538{
539 int i;
540 struct mwifiex_adapter *adapter = priv->adapter;
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700541 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700542 struct list_head *head;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700543 spinlock_t *lock; /* bss priority lock */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700544 unsigned long flags;
545
546 for (i = 0; i < adapter->priv_num; ++i) {
547 head = &adapter->bss_prio_tbl[i].bss_prio_head;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700548 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
549 dev_dbg(adapter->dev, "info: delete BSS priority table,"
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800550 " bss_type = %d, bss_num = %d, i = %d,"
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700551 " head = %p\n",
552 priv->bss_type, priv->bss_num, i, head);
553
554 {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700555 spin_lock_irqsave(lock, flags);
556 if (list_empty(head)) {
557 spin_unlock_irqrestore(lock, flags);
558 continue;
559 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700560 list_for_each_entry_safe(bssprio_node, tmp_node, head,
561 list) {
562 if (bssprio_node->priv == priv) {
563 dev_dbg(adapter->dev, "info: Delete "
564 "node %p, next = %p\n",
565 bssprio_node, tmp_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700566 list_del(&bssprio_node->list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700567 kfree(bssprio_node);
568 }
569 }
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700570 spin_unlock_irqrestore(lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700571 }
572 }
573}
574
575/*
Stone Piao9197ab92012-09-25 20:23:42 -0700576 * This function frees the private structure, including cleans
577 * up the TX and RX queues and frees the BSS priority tables.
578 */
579void mwifiex_free_priv(struct mwifiex_private *priv)
580{
581 mwifiex_clean_txrx(priv);
582 mwifiex_delete_bss_prio_tbl(priv);
583 mwifiex_free_curr_bcn(priv);
584}
585
586/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700587 * This function is used to shutdown the driver.
588 *
589 * The following operations are performed sequentially -
590 * - Check if already shut down
591 * - Make sure the main process has stopped
592 * - Clean up the Tx and Rx queues
593 * - Delete BSS priority tables
594 * - Free the adapter
595 * - Notify completion
596 */
597int
598mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
599{
600 int ret = -EINPROGRESS;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700601 struct mwifiex_private *priv;
602 s32 i;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700603 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700604
605 /* mwifiex already shutdown */
606 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
607 return 0;
608
609 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
610 /* wait for mwifiex_process to complete */
611 if (adapter->mwifiex_processing) {
612 dev_warn(adapter->dev, "main process is still running\n");
613 return ret;
614 }
615
Bing Zhao084c7182013-03-15 18:47:07 -0700616 /* cancel current command */
617 if (adapter->curr_cmd) {
618 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
619 del_timer(&adapter->cmd_timer);
Bing Zhao9908b072013-04-01 12:44:46 -0700620 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
Bing Zhao084c7182013-03-15 18:47:07 -0700621 adapter->curr_cmd = NULL;
622 }
623
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700624 /* shut down mwifiex */
625 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
626
627 /* Clean up Tx/Rx queues and delete BSS priority table */
628 for (i = 0; i < adapter->priv_num; i++) {
629 if (adapter->priv[i]) {
630 priv = adapter->priv[i];
631
632 mwifiex_clean_txrx(priv);
633 mwifiex_delete_bss_prio_tbl(priv);
634 }
635 }
636
Avinash Patil013a4922013-05-17 17:50:24 -0700637 spin_lock(&adapter->mwifiex_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700638
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700639 if (adapter->if_ops.data_complete) {
640 while ((skb = skb_dequeue(&adapter->usb_rx_data_q))) {
641 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
642
643 priv = adapter->priv[rx_info->bss_num];
644 if (priv)
645 priv->stats.rx_dropped++;
646
Ujjal Royca8d6df2013-12-02 23:17:53 -0800647 dev_kfree_skb_any(skb);
648 adapter->if_ops.data_complete(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700649 }
650 }
651
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700652 mwifiex_adapter_cleanup(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700653
Avinash Patil013a4922013-05-17 17:50:24 -0700654 spin_unlock(&adapter->mwifiex_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700655
656 /* Notify completion */
657 ret = mwifiex_shutdown_fw_complete(adapter);
658
659 return ret;
660}
661
662/*
663 * This function downloads the firmware to the card.
664 *
665 * The actual download is preceded by two sanity checks -
666 * - Check if firmware is already running
667 * - Check if the interface is the winner to download the firmware
668 *
669 * ...and followed by another -
670 * - Check if the firmware is downloaded successfully
671 *
672 * After download is successfully completed, the host interrupts are enabled.
673 */
674int mwifiex_dnld_fw(struct mwifiex_adapter *adapter,
675 struct mwifiex_fw_image *pmfw)
676{
Amitkumar Karward930fae2011-10-11 17:41:21 -0700677 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700678 u32 poll_num = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700679
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700680 if (adapter->if_ops.check_fw_status) {
681 adapter->winner = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700682
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700683 /* check if firmware is already running */
684 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
685 if (!ret) {
686 dev_notice(adapter->dev,
687 "WLAN FW already running! Skip FW dnld\n");
Daniel Drake232fde02013-07-13 10:57:10 -0400688 return 0;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700689 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700690
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700691 poll_num = MAX_FIRMWARE_POLL_TRIES;
692
693 /* check if we are the winner for downloading FW */
694 if (!adapter->winner) {
695 dev_notice(adapter->dev,
696 "FW already running! Skip FW dnld\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700697 goto poll_fw;
698 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700699 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700700
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700701 if (pmfw) {
702 /* Download firmware with helper */
703 ret = adapter->if_ops.prog_fw(adapter, pmfw);
704 if (ret) {
705 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
706 return ret;
707 }
708 }
709
710poll_fw:
711 /* Check if the firmware is downloaded successfully or not */
Amitkumar Karward930fae2011-10-11 17:41:21 -0700712 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
Daniel Drake232fde02013-07-13 10:57:10 -0400713 if (ret)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700714 dev_err(adapter->dev, "FW failed to be active in time\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700715
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700716 return ret;
717}