blob: b115e0f94dd7850fd6f1d566c687986661941ffc [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: HW/FW Initialization
3 *
Xinming Hu65da33f2014-06-19 21:38:57 -07004 * Copyright (C) 2011-2014, Marvell International Ltd.
Bing Zhao5e6e3a92011-03-21 18:00:50 -07005 *
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 Karwar46361872014-12-31 02:36:41 -080055static void wakeup_timer_fn(unsigned long data)
56{
57 struct mwifiex_adapter *adapter = (struct mwifiex_adapter *)data;
58
59 dev_err(adapter->dev, "Firmware wakeup failed\n");
60 adapter->hw_status = MWIFIEX_HW_STATUS_RESET;
61 mwifiex_cancel_all_pending_cmd(adapter);
62
63 if (adapter->if_ops.card_reset)
64 adapter->if_ops.card_reset(adapter);
65}
66
Bing Zhao5e6e3a92011-03-21 18:00:50 -070067/*
68 * This function initializes the private structure and sets default
69 * values to the members.
70 *
71 * Additionally, it also initializes all the locks and sets up all the
72 * lists.
73 */
Stone Piao9197ab92012-09-25 20:23:42 -070074int mwifiex_init_priv(struct mwifiex_private *priv)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070075{
76 u32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070077
78 priv->media_connected = false;
79 memset(priv->curr_addr, 0xff, ETH_ALEN);
80
81 priv->pkt_tx_ctrl = 0;
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070082 priv->bss_mode = NL80211_IFTYPE_UNSPECIFIED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070083 priv->data_rate = 0; /* Initially indicate the rate as auto */
84 priv->is_data_rate_auto = true;
85 priv->bcn_avg_factor = DEFAULT_BCN_AVG_FACTOR;
86 priv->data_avg_factor = DEFAULT_DATA_AVG_FACTOR;
87
Amitkumar Karwar5eb02e42012-02-24 21:36:04 -080088 priv->sec_info.wep_enabled = 0;
Marc Yangf986b6d2011-03-28 17:55:42 -070089 priv->sec_info.authentication_mode = NL80211_AUTHTYPE_OPEN_SYSTEM;
Yogesh Ashok Powar2be50b82011-04-01 18:36:47 -070090 priv->sec_info.encryption_mode = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070091 for (i = 0; i < ARRAY_SIZE(priv->wep_key); i++)
92 memset(&priv->wep_key[i], 0, sizeof(struct mwifiex_wep_key));
93 priv->wep_key_curr_index = 0;
94 priv->curr_pkt_filter = HostCmd_ACT_MAC_RX_ON | HostCmd_ACT_MAC_TX_ON |
95 HostCmd_ACT_MAC_ETHERNETII_ENABLE;
96
97 priv->beacon_period = 100; /* beacon interval */ ;
98 priv->attempted_bss_desc = NULL;
99 memset(&priv->curr_bss_params, 0, sizeof(priv->curr_bss_params));
100 priv->listen_interval = MWIFIEX_DEFAULT_LISTEN_INTERVAL;
101
102 memset(&priv->prev_ssid, 0, sizeof(priv->prev_ssid));
103 memset(&priv->prev_bssid, 0, sizeof(priv->prev_bssid));
104 memset(&priv->assoc_rsp_buf, 0, sizeof(priv->assoc_rsp_buf));
105 priv->assoc_rsp_size = 0;
106 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
107 priv->atim_window = 0;
108 priv->adhoc_state = ADHOC_IDLE;
109 priv->tx_power_level = 0;
110 priv->max_tx_power_level = 0;
111 priv->min_tx_power_level = 0;
112 priv->tx_rate = 0;
113 priv->rxpd_htinfo = 0;
114 priv->rxpd_rate = 0;
115 priv->rate_bitmap = 0;
116 priv->data_rssi_last = 0;
117 priv->data_rssi_avg = 0;
118 priv->data_nf_avg = 0;
119 priv->data_nf_last = 0;
120 priv->bcn_rssi_last = 0;
121 priv->bcn_rssi_avg = 0;
122 priv->bcn_nf_avg = 0;
123 priv->bcn_nf_last = 0;
124 memset(&priv->wpa_ie, 0, sizeof(priv->wpa_ie));
125 memset(&priv->aes_key, 0, sizeof(priv->aes_key));
126 priv->wpa_ie_len = 0;
127 priv->wpa_is_gtk_set = false;
128
129 memset(&priv->assoc_tlv_buf, 0, sizeof(priv->assoc_tlv_buf));
130 priv->assoc_tlv_buf_len = 0;
131 memset(&priv->wps, 0, sizeof(priv->wps));
132 memset(&priv->gen_ie_buf, 0, sizeof(priv->gen_ie_buf));
133 priv->gen_ie_buf_len = 0;
134 memset(priv->vs_ie, 0, sizeof(priv->vs_ie));
135
136 priv->wmm_required = true;
137 priv->wmm_enabled = false;
138 priv->wmm_qosinfo = 0;
139 priv->curr_bcn_buf = NULL;
140 priv->curr_bcn_size = 0;
Avinash Patil13d7ba72012-04-09 20:06:56 -0700141 priv->wps_ie = NULL;
142 priv->wps_ie_len = 0;
Avinash Patilc8258912012-08-03 18:06:05 -0700143 priv->ap_11n_enabled = 0;
Stone Piao7feb4c42012-09-25 20:23:36 -0700144 memset(&priv->roc_cfg, 0, sizeof(priv->roc_cfg));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700145
146 priv->scan_block = false;
147
Amitkumar Karwarb8876642013-06-18 16:36:58 -0700148 priv->csa_chan = 0;
149 priv->csa_expire_time = 0;
Avinash Patil61c873042013-07-22 19:17:41 -0700150 priv->del_list_idx = 0;
Avinash Patil587b36d2013-08-23 16:48:23 -0700151 priv->hs2_enabled = false;
Avinash Patil9927baa2014-11-13 21:54:16 +0530152 priv->check_tdls_tx = false;
Avinash Patil41a24a22014-02-07 16:27:29 -0800153 memcpy(priv->tos_to_tid_inv, tos_to_tid_inv, MAX_NUM_TID);
Amitkumar Karwarb8876642013-06-18 16:36:58 -0700154
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700155 return mwifiex_add_bss_prio_tbl(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700156}
157
158/*
159 * This function allocates buffers for members of the adapter
160 * structure.
161 *
162 * The memory allocated includes scan table, command buffers, and
163 * sleep confirm command buffer. In addition, the queues are
164 * also initialized.
165 */
166static int mwifiex_allocate_adapter(struct mwifiex_adapter *adapter)
167{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700168 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700169
170 /* Allocate command buffer */
171 ret = mwifiex_alloc_cmd_buffer(adapter);
172 if (ret) {
173 dev_err(adapter->dev, "%s: failed to alloc cmd buffer\n",
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700174 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700175 return -1;
176 }
177
178 adapter->sleep_cfm =
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700179 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700180 + INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700181
182 if (!adapter->sleep_cfm) {
183 dev_err(adapter->dev, "%s: failed to alloc sleep cfm"
184 " cmd buffer\n", __func__);
185 return -1;
186 }
187 skb_reserve(adapter->sleep_cfm, INTF_HEADER_LEN);
188
189 return 0;
190}
191
192/*
193 * This function initializes the adapter structure and sets default
194 * values to the members of adapter.
195 *
196 * This also initializes the WMM related parameters in the driver private
197 * structures.
198 */
199static void mwifiex_init_adapter(struct mwifiex_adapter *adapter)
200{
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700201 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700202
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700203 skb_put(adapter->sleep_cfm, sizeof(struct mwifiex_opt_sleep_confirm));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700204
205 adapter->cmd_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700206
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700207 if (adapter->iface_type == MWIFIEX_SDIO)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700208 adapter->data_sent = true;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700209 else
210 adapter->data_sent = false;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700211
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700212 adapter->cmd_resp_received = false;
213 adapter->event_received = false;
214 adapter->data_received = false;
215
216 adapter->surprise_removed = false;
217
218 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
219
220 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
221 adapter->ps_state = PS_STATE_AWAKE;
222 adapter->need_to_wakeup = false;
223
224 adapter->scan_mode = HostCmd_BSS_MODE_ANY;
225 adapter->specific_scan_time = MWIFIEX_SPECIFIC_SCAN_CHAN_TIME;
226 adapter->active_scan_time = MWIFIEX_ACTIVE_SCAN_CHAN_TIME;
227 adapter->passive_scan_time = MWIFIEX_PASSIVE_SCAN_CHAN_TIME;
Avinash Patilcb91be82014-09-12 20:08:54 +0530228 adapter->scan_chan_gap_time = MWIFIEX_DEF_SCAN_CHAN_GAP_TIME;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700229
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700230 adapter->scan_probes = 1;
231
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700232 adapter->multiple_dtim = 1;
233
234 adapter->local_listen_interval = 0; /* default value in firmware
235 will be used */
236
237 adapter->is_deep_sleep = false;
238
239 adapter->delay_null_pkt = false;
240 adapter->delay_to_ps = 1000;
241 adapter->enhanced_ps_mode = PS_MODE_AUTO;
242
243 adapter->gen_null_pkt = false; /* Disable NULL Pkg generation by
244 default */
245 adapter->pps_uapsd_mode = false; /* Disable pps/uapsd mode by
246 default */
247 adapter->pm_wakeup_card_req = false;
248
249 adapter->pm_wakeup_fw_try = false;
250
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700251 adapter->curr_tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
252
253 adapter->is_hs_configured = false;
Amitkumar Karwarcc0b5a62013-03-04 16:27:57 -0800254 adapter->hs_cfg.conditions = cpu_to_le32(HS_CFG_COND_DEF);
255 adapter->hs_cfg.gpio = HS_CFG_GPIO_DEF;
256 adapter->hs_cfg.gap = HS_CFG_GAP_DEF;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700257 adapter->hs_activated = false;
258
259 memset(adapter->event_body, 0, sizeof(adapter->event_body));
260 adapter->hw_dot_11n_dev_cap = 0;
261 adapter->hw_dev_mcs_support = 0;
Amitkumar Karwar21c3ba32011-12-20 23:47:21 -0800262 adapter->sec_chan_offset = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700263 adapter->adhoc_11n_enabled = false;
264
265 mwifiex_wmm_init(adapter);
266
267 if (adapter->sleep_cfm) {
Yogesh Ashok Powar8ed13032011-11-07 21:41:10 -0800268 sleep_cfm_buf = (struct mwifiex_opt_sleep_confirm *)
269 adapter->sleep_cfm->data;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700270 memset(sleep_cfm_buf, 0, adapter->sleep_cfm->len);
271 sleep_cfm_buf->command =
272 cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
273 sleep_cfm_buf->size =
274 cpu_to_le16(adapter->sleep_cfm->len);
275 sleep_cfm_buf->result = 0;
276 sleep_cfm_buf->action = cpu_to_le16(SLEEP_CONFIRM);
277 sleep_cfm_buf->resp_ctrl = cpu_to_le16(RESP_NEEDED);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700278 }
279 memset(&adapter->sleep_params, 0, sizeof(adapter->sleep_params));
280 memset(&adapter->sleep_period, 0, sizeof(adapter->sleep_period));
281 adapter->tx_lock_flag = false;
282 adapter->null_pkt_interval = 0;
283 adapter->fw_bands = 0;
284 adapter->config_bands = 0;
285 adapter->adhoc_start_band = 0;
286 adapter->scan_channels = NULL;
287 adapter->fw_release_number = 0;
288 adapter->fw_cap_info = 0;
289 memset(&adapter->upld_buf, 0, sizeof(adapter->upld_buf));
290 adapter->event_cause = 0;
291 adapter->region_code = 0;
292 adapter->bcn_miss_time_out = DEFAULT_BCN_MISS_TIMEOUT;
293 adapter->adhoc_awake_period = 0;
294 memset(&adapter->arp_filter, 0, sizeof(adapter->arp_filter));
295 adapter->arp_filter_size = 0;
Avinash Patilede98bf2012-05-08 18:30:28 -0700296 adapter->max_mgmt_ie_index = MAX_MGMT_IE_INDEX;
Amitkumar Karwar21f58d202014-02-11 18:39:56 -0800297 adapter->ext_scan = true;
Amitkumar Karwar4b9fede2014-08-19 08:24:25 -0400298 adapter->key_api_major_ver = 0;
299 adapter->key_api_minor_ver = 0;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800300
301 setup_timer(&adapter->wakeup_timer, wakeup_timer_fn,
302 (unsigned long)adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700303}
304
305/*
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800306 * This function sets trans_start per tx_queue
307 */
308void mwifiex_set_trans_start(struct net_device *dev)
309{
310 int i;
311
312 for (i = 0; i < dev->num_tx_queues; i++)
313 netdev_get_tx_queue(dev, i)->trans_start = jiffies;
314
315 dev->trans_start = jiffies;
316}
317
318/*
319 * This function wakes up all queues in net_device
320 */
321void mwifiex_wake_up_net_dev_queue(struct net_device *netdev,
322 struct mwifiex_adapter *adapter)
323{
324 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700325 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800326
327 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700328
329 for (i = 0; i < netdev->num_tx_queues; i++) {
330 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
331
332 if (netif_tx_queue_stopped(txq))
333 netif_tx_wake_queue(txq);
334 }
335
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800336 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
337}
338
339/*
340 * This function stops all queues in net_device
341 */
342void mwifiex_stop_net_dev_queue(struct net_device *netdev,
343 struct mwifiex_adapter *adapter)
344{
345 unsigned long dev_queue_flags;
Avinash Patil47411a02012-11-01 18:44:16 -0700346 unsigned int i;
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800347
348 spin_lock_irqsave(&adapter->queue_lock, dev_queue_flags);
Avinash Patil47411a02012-11-01 18:44:16 -0700349
350 for (i = 0; i < netdev->num_tx_queues; i++) {
351 struct netdev_queue *txq = netdev_get_tx_queue(netdev, i);
352
353 if (!netif_tx_queue_stopped(txq))
354 netif_tx_stop_queue(txq);
355 }
356
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800357 spin_unlock_irqrestore(&adapter->queue_lock, dev_queue_flags);
358}
359
360/*
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700361 * This function releases the lock variables and frees the locks and
362 * associated locks.
363 */
364static void mwifiex_free_lock_list(struct mwifiex_adapter *adapter)
365{
366 struct mwifiex_private *priv;
367 s32 i, j;
368
369 /* Free lists */
370 list_del(&adapter->cmd_free_q);
371 list_del(&adapter->cmd_pending_q);
372 list_del(&adapter->scan_pending_q);
373
374 for (i = 0; i < adapter->priv_num; i++)
375 list_del(&adapter->bss_prio_tbl[i].bss_prio_head);
376
377 for (i = 0; i < adapter->priv_num; i++) {
378 if (adapter->priv[i]) {
379 priv = adapter->priv[i];
380 for (j = 0; j < MAX_NUM_TID; ++j)
381 list_del(&priv->wmm.tid_tbl_ptr[j].ra_list);
382 list_del(&priv->tx_ba_stream_tbl_ptr);
383 list_del(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700384 list_del(&priv->sta_list);
Avinash Patil9927baa2014-11-13 21:54:16 +0530385 list_del(&priv->auto_tdls_list);
Amitkumar Karwar711825a2011-10-12 20:29:33 -0700386 }
387 }
388}
389
390/*
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700391 * This function performs cleanup for adapter structure.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700392 *
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700393 * The cleanup is done recursively, by canceling all pending
394 * commands, freeing the member buffers previously allocated
395 * (command buffers, scan table buffer, sleep confirm command
396 * buffer), stopping the timers and calling the cleanup routines
397 * for every interface.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700398 */
399static void
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700400mwifiex_adapter_cleanup(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700401{
Amitkumar Karwar92c25382014-06-19 21:38:52 -0700402 int idx;
403
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700404 if (!adapter) {
405 pr_err("%s: adapter is NULL\n", __func__);
406 return;
407 }
408
Amitkumar Karwar46361872014-12-31 02:36:41 -0800409 del_timer(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700410 mwifiex_cancel_all_pending_cmd(adapter);
Amitkumar Karwarfc647462014-12-31 02:36:42 -0800411 wake_up_interruptible(&adapter->cmd_wait_q.wait);
412 wake_up_interruptible(&adapter->hs_activate_wait_q);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700413
414 /* Free lock variables */
415 mwifiex_free_lock_list(adapter);
416
417 /* Free command buffer */
418 dev_dbg(adapter->dev, "info: free cmd buffer\n");
419 mwifiex_free_cmd_buffer(adapter);
420
Amitkumar Karwar92c25382014-06-19 21:38:52 -0700421 for (idx = 0; idx < adapter->num_mem_types; idx++) {
422 struct memory_type_mapping *entry =
423 &adapter->mem_type_mapping_tbl[idx];
424
425 if (entry->mem_ptr) {
426 vfree(entry->mem_ptr);
427 entry->mem_ptr = NULL;
428 }
429 entry->mem_size = 0;
430 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700431
Xinming Hu11cd07a2014-12-23 19:14:10 +0530432 if (adapter->drv_info_dump) {
433 vfree(adapter->drv_info_dump);
434 adapter->drv_info_size = 0;
435 }
436
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800437 if (adapter->sleep_cfm)
438 dev_kfree_skb_any(adapter->sleep_cfm);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700439}
440
441/*
442 * This function intializes the lock variables and
443 * the list heads.
444 */
445int mwifiex_init_lock_list(struct mwifiex_adapter *adapter)
446{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700447 struct mwifiex_private *priv;
448 s32 i, j;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700449
450 spin_lock_init(&adapter->mwifiex_lock);
451 spin_lock_init(&adapter->int_lock);
452 spin_lock_init(&adapter->main_proc_lock);
453 spin_lock_init(&adapter->mwifiex_cmd_lock);
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800454 spin_lock_init(&adapter->queue_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700455 for (i = 0; i < adapter->priv_num; i++) {
456 if (adapter->priv[i]) {
457 priv = adapter->priv[i];
458 spin_lock_init(&priv->rx_pkt_lock);
459 spin_lock_init(&priv->wmm.ra_list_spinlock);
460 spin_lock_init(&priv->curr_bcn_buf_lock);
Avinash Patil017a92a2012-08-03 18:06:07 -0700461 spin_lock_init(&priv->sta_list_spinlock);
Avinash Patil9927baa2014-11-13 21:54:16 +0530462 spin_lock_init(&priv->auto_tdls_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700463 }
464 }
465
466 /* Initialize cmd_free_q */
467 INIT_LIST_HEAD(&adapter->cmd_free_q);
468 /* Initialize cmd_pending_q */
469 INIT_LIST_HEAD(&adapter->cmd_pending_q);
470 /* Initialize scan_pending_q */
471 INIT_LIST_HEAD(&adapter->scan_pending_q);
472
473 spin_lock_init(&adapter->cmd_free_q_lock);
474 spin_lock_init(&adapter->cmd_pending_q_lock);
475 spin_lock_init(&adapter->scan_pending_q_lock);
Avinash Patil6e251172014-09-12 20:08:59 +0530476 spin_lock_init(&adapter->rx_proc_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700477
Avinash Patil6e251172014-09-12 20:08:59 +0530478 skb_queue_head_init(&adapter->rx_data_q);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700479
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700480 for (i = 0; i < adapter->priv_num; ++i) {
481 INIT_LIST_HEAD(&adapter->bss_prio_tbl[i].bss_prio_head);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700482 spin_lock_init(&adapter->bss_prio_tbl[i].bss_prio_lock);
483 }
484
485 for (i = 0; i < adapter->priv_num; i++) {
486 if (!adapter->priv[i])
487 continue;
488 priv = adapter->priv[i];
Andreas Fenkart6d2344e2013-04-04 20:03:51 -0700489 for (j = 0; j < MAX_NUM_TID; ++j)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700490 INIT_LIST_HEAD(&priv->wmm.tid_tbl_ptr[j].ra_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700491 INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr);
492 INIT_LIST_HEAD(&priv->rx_reorder_tbl_ptr);
Avinash Patil017a92a2012-08-03 18:06:07 -0700493 INIT_LIST_HEAD(&priv->sta_list);
Avinash Patil9927baa2014-11-13 21:54:16 +0530494 INIT_LIST_HEAD(&priv->auto_tdls_list);
Avinash Patil56bd24a2014-02-07 16:30:35 -0800495 skb_queue_head_init(&priv->tdls_txq);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700496
497 spin_lock_init(&priv->tx_ba_stream_tbl_lock);
498 spin_lock_init(&priv->rx_reorder_tbl_lock);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800499
500 spin_lock_init(&priv->ack_status_lock);
501 idr_init(&priv->ack_status_frames);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700502 }
503
504 return 0;
505}
506
507/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700508 * This function initializes the firmware.
509 *
510 * The following operations are performed sequentially -
511 * - Allocate adapter structure
512 * - Initialize the adapter structure
513 * - Initialize the private structure
514 * - Add BSS priority tables to the adapter structure
515 * - For each interface, send the init commands to firmware
516 * - Send the first command in command pending queue, if available
517 */
518int mwifiex_init_fw(struct mwifiex_adapter *adapter)
519{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700520 int ret;
521 struct mwifiex_private *priv;
522 u8 i, first_sta = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700523 int is_cmd_pend_q_empty;
524 unsigned long flags;
525
526 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
527
528 /* Allocate memory for member of adapter structure */
529 ret = mwifiex_allocate_adapter(adapter);
530 if (ret)
531 return -1;
532
533 /* Initialize adapter structure */
534 mwifiex_init_adapter(adapter);
535
536 for (i = 0; i < adapter->priv_num; i++) {
537 if (adapter->priv[i]) {
538 priv = adapter->priv[i];
539
540 /* Initialize private structure */
541 ret = mwifiex_init_priv(priv);
542 if (ret)
543 return -1;
544 }
545 }
Avinash Patilc6d1d872013-01-03 21:21:29 -0800546
547 if (adapter->if_ops.init_fw_port) {
548 if (adapter->if_ops.init_fw_port(adapter))
549 return -1;
550 }
551
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700552 for (i = 0; i < adapter->priv_num; i++) {
553 if (adapter->priv[i]) {
554 ret = mwifiex_sta_init_cmd(adapter->priv[i], first_sta);
555 if (ret == -1)
556 return -1;
557
558 first_sta = false;
559 }
560 }
561
562 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
563 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
564 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
565 if (!is_cmd_pend_q_empty) {
566 /* Send the first command in queue and return */
567 if (mwifiex_main_process(adapter) != -1)
568 ret = -EINPROGRESS;
569 } else {
570 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
571 }
572
573 return ret;
574}
575
576/*
577 * This function deletes the BSS priority tables.
578 *
579 * The function traverses through all the allocated BSS priority nodes
580 * in every BSS priority table and frees them.
581 */
582static void mwifiex_delete_bss_prio_tbl(struct mwifiex_private *priv)
583{
584 int i;
585 struct mwifiex_adapter *adapter = priv->adapter;
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700586 struct mwifiex_bss_prio_node *bssprio_node, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700587 struct list_head *head;
Yogesh Ashok Powar931f1582012-03-13 19:22:36 -0700588 spinlock_t *lock; /* bss priority lock */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700589 unsigned long flags;
590
591 for (i = 0; i < adapter->priv_num; ++i) {
592 head = &adapter->bss_prio_tbl[i].bss_prio_head;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700593 lock = &adapter->bss_prio_tbl[i].bss_prio_lock;
594 dev_dbg(adapter->dev, "info: delete BSS priority table,"
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800595 " bss_type = %d, bss_num = %d, i = %d,"
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700596 " head = %p\n",
597 priv->bss_type, priv->bss_num, i, head);
598
599 {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600 spin_lock_irqsave(lock, flags);
601 if (list_empty(head)) {
602 spin_unlock_irqrestore(lock, flags);
603 continue;
604 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700605 list_for_each_entry_safe(bssprio_node, tmp_node, head,
606 list) {
607 if (bssprio_node->priv == priv) {
608 dev_dbg(adapter->dev, "info: Delete "
609 "node %p, next = %p\n",
610 bssprio_node, tmp_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611 list_del(&bssprio_node->list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700612 kfree(bssprio_node);
613 }
614 }
Andreas Fenkartb006ed52013-04-18 16:34:12 -0700615 spin_unlock_irqrestore(lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700616 }
617 }
618}
619
620/*
Stone Piao9197ab92012-09-25 20:23:42 -0700621 * This function frees the private structure, including cleans
622 * up the TX and RX queues and frees the BSS priority tables.
623 */
624void mwifiex_free_priv(struct mwifiex_private *priv)
625{
626 mwifiex_clean_txrx(priv);
627 mwifiex_delete_bss_prio_tbl(priv);
628 mwifiex_free_curr_bcn(priv);
629}
630
631/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700632 * This function is used to shutdown the driver.
633 *
634 * The following operations are performed sequentially -
635 * - Check if already shut down
636 * - Make sure the main process has stopped
637 * - Clean up the Tx and Rx queues
638 * - Delete BSS priority tables
639 * - Free the adapter
640 * - Notify completion
641 */
642int
643mwifiex_shutdown_drv(struct mwifiex_adapter *adapter)
644{
645 int ret = -EINPROGRESS;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700646 struct mwifiex_private *priv;
647 s32 i;
Avinash Patil6e251172014-09-12 20:08:59 +0530648 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700649 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700650
651 /* mwifiex already shutdown */
652 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
653 return 0;
654
655 adapter->hw_status = MWIFIEX_HW_STATUS_CLOSING;
656 /* wait for mwifiex_process to complete */
657 if (adapter->mwifiex_processing) {
658 dev_warn(adapter->dev, "main process is still running\n");
659 return ret;
660 }
661
Bing Zhao084c7182013-03-15 18:47:07 -0700662 /* cancel current command */
663 if (adapter->curr_cmd) {
664 dev_warn(adapter->dev, "curr_cmd is still in processing\n");
Avinash Patil629873f2014-02-18 15:47:55 -0800665 del_timer_sync(&adapter->cmd_timer);
Bing Zhao9908b072013-04-01 12:44:46 -0700666 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
Bing Zhao084c7182013-03-15 18:47:07 -0700667 adapter->curr_cmd = NULL;
668 }
669
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700670 /* shut down mwifiex */
671 dev_dbg(adapter->dev, "info: shutdown mwifiex...\n");
672
673 /* Clean up Tx/Rx queues and delete BSS priority table */
674 for (i = 0; i < adapter->priv_num; i++) {
675 if (adapter->priv[i]) {
676 priv = adapter->priv[i];
677
Avinash Patil9927baa2014-11-13 21:54:16 +0530678 mwifiex_clean_auto_tdls(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700679 mwifiex_clean_txrx(priv);
680 mwifiex_delete_bss_prio_tbl(priv);
681 }
682 }
683
Avinash Patil6e251172014-09-12 20:08:59 +0530684 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
685
686 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
687 struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb);
688
689 atomic_dec(&adapter->rx_pending);
690 priv = adapter->priv[rx_info->bss_num];
691 if (priv)
692 priv->stats.rx_dropped++;
693
694 dev_kfree_skb_any(skb);
695 }
696
697 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
698
Avinash Patil013a4922013-05-17 17:50:24 -0700699 spin_lock(&adapter->mwifiex_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700700
Amitkumar Karwarc56ecf52013-05-17 17:50:18 -0700701 mwifiex_adapter_cleanup(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700702
Avinash Patil013a4922013-05-17 17:50:24 -0700703 spin_unlock(&adapter->mwifiex_lock);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700704
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");
Daniel Drake232fde02013-07-13 10:57:10 -0400737 return 0;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700738 }
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");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700746 goto poll_fw;
747 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700748 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700749
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700750 if (pmfw) {
751 /* Download firmware with helper */
752 ret = adapter->if_ops.prog_fw(adapter, pmfw);
753 if (ret) {
754 dev_err(adapter->dev, "prog_fw failed ret=%#x\n", ret);
755 return ret;
756 }
757 }
758
759poll_fw:
760 /* Check if the firmware is downloaded successfully or not */
Amitkumar Karward930fae2011-10-11 17:41:21 -0700761 ret = adapter->if_ops.check_fw_status(adapter, poll_num);
Daniel Drake232fde02013-07-13 10:57:10 -0400762 if (ret)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700763 dev_err(adapter->dev, "FW failed to be active in time\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700764
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700765 return ret;
766}