blob: 5019f1f2e01c51ea146c62cec0fa4b9cb4521ac3 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: commands and events
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"
Yogesh Ashok Powara5f39052013-02-15 21:44:30 -080027#include "11ac.h"
Bing Zhao5e6e3a92011-03-21 18:00:50 -070028
29/*
30 * This function initializes a command node.
31 *
32 * The actual allocation of the node is not done by this function. It only
33 * initiates a node by filling it with default parameters. Similarly,
34 * allocation of the different buffers used (IOCTL buffer, data buffer) are
35 * not done by this function either.
36 */
37static void
38mwifiex_init_cmd_node(struct mwifiex_private *priv,
39 struct cmd_ctrl_node *cmd_node,
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070040 u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070041{
42 cmd_node->priv = priv;
43 cmd_node->cmd_oid = cmd_oid;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -070044 if (priv->adapter->cmd_wait_q_required) {
45 cmd_node->wait_q_enabled = priv->adapter->cmd_wait_q_required;
46 priv->adapter->cmd_wait_q_required = false;
47 cmd_node->cmd_wait_q_woken = false;
48 cmd_node->condition = &cmd_node->cmd_wait_q_woken;
49 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -070050 cmd_node->data_buf = data_buf;
51 cmd_node->cmd_skb = cmd_node->skb;
52}
53
54/*
55 * This function returns a command node from the free queue depending upon
56 * availability.
57 */
58static struct cmd_ctrl_node *
59mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
60{
61 struct cmd_ctrl_node *cmd_node;
62 unsigned long flags;
63
64 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
65 if (list_empty(&adapter->cmd_free_q)) {
66 dev_err(adapter->dev, "GET_CMD_NODE: cmd node not available\n");
67 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
68 return NULL;
69 }
70 cmd_node = list_first_entry(&adapter->cmd_free_q,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -070071 struct cmd_ctrl_node, list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070072 list_del(&cmd_node->list);
73 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
74
75 return cmd_node;
76}
77
78/*
79 * This function cleans up a command node.
80 *
81 * The function resets the fields including the buffer pointers.
82 * This function does not try to free the buffers. They must be
83 * freed before calling this function.
84 *
85 * This function will however call the receive completion callback
86 * in case a response buffer is still available before resetting
87 * the pointer.
88 */
89static void
90mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
91 struct cmd_ctrl_node *cmd_node)
92{
93 cmd_node->cmd_oid = 0;
94 cmd_node->cmd_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070095 cmd_node->data_buf = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070096 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070097
Amitkumar Karwar5cf80992011-09-21 21:43:25 -070098 if (cmd_node->cmd_skb)
99 skb_trim(cmd_node->cmd_skb, 0);
100
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700101 if (cmd_node->resp_skb) {
Amitkumar Karward930fae2011-10-11 17:41:21 -0700102 adapter->if_ops.cmdrsp_complete(adapter, cmd_node->resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700103 cmd_node->resp_skb = NULL;
104 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700105}
106
107/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700108 * This function sends a host command to the firmware.
109 *
110 * The function copies the host command into the driver command
111 * buffer, which will be transferred to the firmware later by the
112 * main thread.
113 */
114static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700115 struct host_cmd_ds_command *cmd,
116 struct mwifiex_ds_misc_cmd *pcmd_ptr)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700117{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700118 /* Copy the HOST command to command buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700119 memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700120 dev_dbg(priv->adapter->dev, "cmd: host cmd size = %d\n", pcmd_ptr->len);
121 return 0;
122}
123
124/*
125 * This function downloads a command to the firmware.
126 *
127 * The function performs sanity tests, sets the command sequence
128 * number and size, converts the header fields to CPU format before
129 * sending. Afterwards, it logs the command ID and action for debugging
130 * and sets up the command timeout timer.
131 */
132static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
133 struct cmd_ctrl_node *cmd_node)
134{
135
136 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700137 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700138 struct host_cmd_ds_command *host_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700139 uint16_t cmd_code;
140 uint16_t cmd_size;
141 struct timeval tstamp;
142 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700143 __le32 tmp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700144
145 if (!adapter || !cmd_node)
146 return -1;
147
148 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700149
150 /* Sanity test */
151 if (host_cmd == NULL || host_cmd->size == 0) {
152 dev_err(adapter->dev, "DNLD_CMD: host_cmd is null"
153 " or cmd size is 0, not sending\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700154 if (cmd_node->wait_q_enabled)
155 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700156 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
157 return -1;
158 }
159
160 /* Set command sequence number */
161 adapter->seq_num++;
162 host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700163 (adapter->seq_num,
164 cmd_node->priv->bss_num,
165 cmd_node->priv->bss_type));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700166
167 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
168 adapter->curr_cmd = cmd_node;
169 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
170
171 cmd_code = le16_to_cpu(host_cmd->command);
172 cmd_size = le16_to_cpu(host_cmd->size);
173
Stone Piaoda251862012-08-22 20:26:31 -0700174 /* Adjust skb length */
175 if (cmd_node->cmd_skb->len > cmd_size)
176 /*
177 * cmd_size is less than sizeof(struct host_cmd_ds_command).
178 * Trim off the unused portion.
179 */
180 skb_trim(cmd_node->cmd_skb, cmd_size);
181 else if (cmd_node->cmd_skb->len < cmd_size)
182 /*
183 * cmd_size is larger than sizeof(struct host_cmd_ds_command)
184 * because we have appended custom IE TLV. Increase skb length
185 * accordingly.
186 */
187 skb_put(cmd_node->cmd_skb, cmd_size - cmd_node->cmd_skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700188
189 do_gettimeofday(&tstamp);
190 dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
191 " seqno %#x\n",
192 tstamp.tv_sec, tstamp.tv_usec, cmd_code,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700193 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
194 le16_to_cpu(host_cmd->seq_num));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700195
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700196 if (adapter->iface_type == MWIFIEX_USB) {
197 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
198 skb_push(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
199 memcpy(cmd_node->cmd_skb->data, &tmp, MWIFIEX_TYPE_LEN);
200 adapter->cmd_sent = true;
201 ret = adapter->if_ops.host_to_card(adapter,
202 MWIFIEX_USB_EP_CMD_EVENT,
203 cmd_node->cmd_skb, NULL);
204 skb_pull(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
205 if (ret == -EBUSY)
206 cmd_node->cmd_skb = NULL;
207 } else {
208 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
209 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
210 cmd_node->cmd_skb, NULL);
211 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
212 }
Bing Zhao18bf9652011-04-06 16:46:55 -0700213
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700214 if (ret == -1) {
215 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700216 if (adapter->iface_type == MWIFIEX_USB)
217 adapter->cmd_sent = false;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700218 if (cmd_node->wait_q_enabled)
219 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700220 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
221
222 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
223 adapter->curr_cmd = NULL;
224 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
225
226 adapter->dbg.num_cmd_host_to_card_failure++;
227 return -1;
228 }
229
230 /* Save the last command id and action to debug log */
231 adapter->dbg.last_cmd_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700232 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700233 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
234 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700235 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700236
237 /* Clear BSS_NO_BITS from HostCmd */
238 cmd_code &= HostCmd_CMD_ID_MASK;
239
240 /* Setup the timer after transmit command */
241 mod_timer(&adapter->cmd_timer,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700242 jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700243
244 return 0;
245}
246
247/*
248 * This function downloads a sleep confirm command to the firmware.
249 *
250 * The function performs sanity tests, sets the command sequence
251 * number and size, converts the header fields to CPU format before
252 * sending.
253 *
254 * No responses are needed for sleep confirm command.
255 */
256static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
257{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700258 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700259 struct mwifiex_private *priv;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700260 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
261 (struct mwifiex_opt_sleep_confirm *)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700262 adapter->sleep_cfm->data;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700263 struct sk_buff *sleep_cfm_tmp;
264 __le32 tmp;
265
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700266 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
267
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700268 sleep_cfm_buf->seq_num =
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700269 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
270 (adapter->seq_num, priv->bss_num,
271 priv->bss_type)));
272 adapter->seq_num++;
273
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700274 if (adapter->iface_type == MWIFIEX_USB) {
275 sleep_cfm_tmp =
276 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
277 + MWIFIEX_TYPE_LEN);
278 skb_put(sleep_cfm_tmp, sizeof(struct mwifiex_opt_sleep_confirm)
279 + MWIFIEX_TYPE_LEN);
280 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
281 memcpy(sleep_cfm_tmp->data, &tmp, MWIFIEX_TYPE_LEN);
282 memcpy(sleep_cfm_tmp->data + MWIFIEX_TYPE_LEN,
283 adapter->sleep_cfm->data,
284 sizeof(struct mwifiex_opt_sleep_confirm));
285 ret = adapter->if_ops.host_to_card(adapter,
286 MWIFIEX_USB_EP_CMD_EVENT,
287 sleep_cfm_tmp, NULL);
288 if (ret != -EBUSY)
289 dev_kfree_skb_any(sleep_cfm_tmp);
290 } else {
291 skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
292 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
293 adapter->sleep_cfm, NULL);
294 skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
295 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700296
297 if (ret == -1) {
298 dev_err(adapter->dev, "SLEEP_CFM: failed\n");
299 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
300 return -1;
301 }
302 if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700303 == MWIFIEX_BSS_ROLE_STA) {
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700304 if (!sleep_cfm_buf->resp_ctrl)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700305 /* Response is not needed for sleep
306 confirm command */
307 adapter->ps_state = PS_STATE_SLEEP;
308 else
309 adapter->ps_state = PS_STATE_SLEEP_CFM;
310
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700311 if (!sleep_cfm_buf->resp_ctrl &&
312 (adapter->is_hs_configured &&
313 !adapter->sleep_period.period)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700314 adapter->pm_wakeup_card_req = true;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700315 mwifiex_hs_activated_event(mwifiex_get_priv
316 (adapter, MWIFIEX_BSS_ROLE_STA), true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700317 }
318 }
319
320 return ret;
321}
322
323/*
324 * This function allocates the command buffers and links them to
325 * the command free queue.
326 *
327 * The driver uses a pre allocated number of command buffers, which
328 * are created at driver initializations and freed at driver cleanup.
329 * Every command needs to obtain a command buffer from this pool before
330 * it can be issued. The command free queue lists the command buffers
331 * currently free to use, while the command pending queue lists the
332 * command buffers already in use and awaiting handling. Command buffers
333 * are returned to the free queue after use.
334 */
335int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
336{
337 struct cmd_ctrl_node *cmd_array;
338 u32 buf_size;
339 u32 i;
340
341 /* Allocate and initialize struct cmd_ctrl_node */
342 buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
343 cmd_array = kzalloc(buf_size, GFP_KERNEL);
344 if (!cmd_array) {
345 dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700346 __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200347 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348 }
349
350 adapter->cmd_pool = cmd_array;
351 memset(adapter->cmd_pool, 0, buf_size);
352
353 /* Allocate and initialize command buffers */
354 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
355 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
356 if (!cmd_array[i].skb) {
357 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
358 return -1;
359 }
360 }
361
362 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
363 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
364
365 return 0;
366}
367
368/*
369 * This function frees the command buffers.
370 *
371 * The function calls the completion callback for all the command
372 * buffers that still have response buffers associated with them.
373 */
374int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
375{
376 struct cmd_ctrl_node *cmd_array;
377 u32 i;
378
379 /* Need to check if cmd pool is allocated or not */
380 if (!adapter->cmd_pool) {
381 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
382 return 0;
383 }
384
385 cmd_array = adapter->cmd_pool;
386
387 /* Release shared memory buffers */
388 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
389 if (cmd_array[i].skb) {
390 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
391 dev_kfree_skb_any(cmd_array[i].skb);
392 }
393 if (!cmd_array[i].resp_skb)
394 continue;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700395
396 if (adapter->iface_type == MWIFIEX_USB)
397 adapter->if_ops.cmdrsp_complete(adapter,
398 cmd_array[i].resp_skb);
399 else
400 dev_kfree_skb_any(cmd_array[i].resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700401 }
402 /* Release struct cmd_ctrl_node */
403 if (adapter->cmd_pool) {
404 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
405 kfree(adapter->cmd_pool);
406 adapter->cmd_pool = NULL;
407 }
408
409 return 0;
410}
411
412/*
413 * This function handles events generated by firmware.
414 *
415 * Event body of events received from firmware are not used (though they are
416 * saved), only the event ID is used. Some events are re-invoked by
417 * the driver, with a new event body.
418 *
419 * After processing, the function calls the completion callback
420 * for cleanup.
421 */
422int mwifiex_process_event(struct mwifiex_adapter *adapter)
423{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700424 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700425 struct mwifiex_private *priv =
426 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
427 struct sk_buff *skb = adapter->event_skb;
428 u32 eventcause = adapter->event_cause;
429 struct timeval tstamp;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700430 struct mwifiex_rxinfo *rx_info;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700431
432 /* Save the last event to debug log */
433 adapter->dbg.last_event_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700434 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700435 adapter->dbg.last_event[adapter->dbg.last_event_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700436 (u16) eventcause;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437
438 /* Get BSS number and corresponding priv */
439 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
440 EVENT_GET_BSS_TYPE(eventcause));
441 if (!priv)
442 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
443 /* Clear BSS_NO_BITS from event */
444 eventcause &= EVENT_ID_MASK;
445 adapter->event_cause = eventcause;
446
447 if (skb) {
448 rx_info = MWIFIEX_SKB_RXCB(skb);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800449 rx_info->bss_num = priv->bss_num;
450 rx_info->bss_type = priv->bss_type;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700451 }
452
453 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
454 do_gettimeofday(&tstamp);
455 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700456 tstamp.tv_sec, tstamp.tv_usec, eventcause);
Avinash Patil03785382012-05-08 18:30:14 -0700457 } else {
458 /* Handle PS_SLEEP/AWAKE events on STA */
459 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
460 if (!priv)
461 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700462 }
463
Avinash Patil3d99d982012-08-03 18:06:06 -0700464 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
465 ret = mwifiex_process_uap_event(priv);
466 else
467 ret = mwifiex_process_sta_event(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700468
469 adapter->event_cause = 0;
470 adapter->event_skb = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700471 adapter->if_ops.event_complete(adapter, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700472
473 return ret;
474}
475
476/*
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700477 * This function is used to send synchronous command to the firmware.
478 *
479 * it allocates a wait queue for the command and wait for the command
480 * response.
481 */
482int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
483 u16 cmd_action, u32 cmd_oid, void *data_buf)
484{
485 int ret = 0;
486 struct mwifiex_adapter *adapter = priv->adapter;
487
488 adapter->cmd_wait_q_required = true;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700489
490 ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
491 data_buf);
492 if (!ret)
493 ret = mwifiex_wait_queue_complete(adapter);
494
495 return ret;
496}
497
498
499/*
500 * This function prepares a command and asynchronously send it to the firmware.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700501 *
502 * Preparation includes -
503 * - Sanity tests to make sure the card is still present or the FW
504 * is not reset
505 * - Getting a new command node from the command free queue
506 * - Initializing the command node for default parameters
507 * - Fill up the non-default parameters and buffer pointers
508 * - Add the command to pending queue
509 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700510int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
511 u16 cmd_action, u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700512{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700513 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700514 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700515 struct cmd_ctrl_node *cmd_node;
516 struct host_cmd_ds_command *cmd_ptr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700517
518 if (!adapter) {
519 pr_err("PREP_CMD: adapter is NULL\n");
520 return -1;
521 }
522
523 if (adapter->is_suspended) {
524 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
525 return -1;
526 }
527
528 if (adapter->surprise_removed) {
529 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
530 return -1;
531 }
532
533 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
534 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
535 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
536 return -1;
537 }
538 }
539
540 /* Get a new command node */
541 cmd_node = mwifiex_get_cmd_node(adapter);
542
543 if (!cmd_node) {
544 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
545 return -1;
546 }
547
548 /* Initialize the command node */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700549 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700550
551 if (!cmd_node->cmd_skb) {
552 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
553 return -1;
554 }
555
556 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
557 0, sizeof(struct host_cmd_ds_command));
558
559 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
560 cmd_ptr->command = cpu_to_le16(cmd_no);
561 cmd_ptr->result = 0;
562
563 /* Prepare command */
564 if (cmd_no) {
Avinash Patil40d07032012-05-08 18:30:19 -0700565 switch (cmd_no) {
566 case HostCmd_CMD_UAP_SYS_CONFIG:
567 case HostCmd_CMD_UAP_BSS_START:
568 case HostCmd_CMD_UAP_BSS_STOP:
569 ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
570 cmd_oid, data_buf,
571 cmd_ptr);
572 break;
573 default:
574 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
575 cmd_oid, data_buf,
576 cmd_ptr);
577 break;
578 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700579 } else {
580 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
581 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
582 }
583
584 /* Return error, since the command preparation failed */
585 if (ret) {
586 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700587 cmd_no);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700588 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
589 return -1;
590 }
591
592 /* Send command */
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700593 if (cmd_no == HostCmd_CMD_802_11_SCAN) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700594 mwifiex_queue_scan_cmd(priv, cmd_node);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700595 } else {
596 adapter->cmd_queued = cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700597 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
Amitkumar Karwar1a1fb972012-06-27 19:57:56 -0700598 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700599 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600
601 return ret;
602}
603
604/*
605 * This function returns a command to the command free queue.
606 *
607 * The function also calls the completion callback if required, before
608 * cleaning the command node and re-inserting it into the free queue.
609 */
610void
611mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
612 struct cmd_ctrl_node *cmd_node)
613{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700614 unsigned long flags;
615
Yogesh Ashok Powar19a89862011-04-13 17:27:07 -0700616 if (!cmd_node)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700617 return;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700618
619 if (cmd_node->wait_q_enabled)
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700620 mwifiex_complete_cmd(adapter, cmd_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700621 /* Clean the node */
622 mwifiex_clean_cmd_node(adapter, cmd_node);
623
624 /* Insert node into cmd_free_q */
625 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
626 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
627 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700628}
629
630/*
631 * This function queues a command to the command pending queue.
632 *
633 * This in effect adds the command to the command list to be executed.
634 * Exit PS command is handled specially, by placing it always to the
635 * front of the command queue.
636 */
637void
638mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
639 struct cmd_ctrl_node *cmd_node, u32 add_tail)
640{
641 struct host_cmd_ds_command *host_cmd = NULL;
642 u16 command;
643 unsigned long flags;
644
645 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
646 if (!host_cmd) {
647 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
648 return;
649 }
650
651 command = le16_to_cpu(host_cmd->command);
652
653 /* Exit_PS command needs to be queued in the header always. */
654 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
655 struct host_cmd_ds_802_11_ps_mode_enh *pm =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700656 &host_cmd->params.psmode_enh;
657 if ((le16_to_cpu(pm->action) == DIS_PS) ||
658 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700659 if (adapter->ps_state != PS_STATE_AWAKE)
660 add_tail = false;
661 }
662 }
663
664 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
665 if (add_tail)
666 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
667 else
668 list_add(&cmd_node->list, &adapter->cmd_pending_q);
669 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
670
671 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700672}
673
674/*
675 * This function executes the next command in command pending queue.
676 *
677 * This function will fail if a command is already in processing stage,
678 * otherwise it will dequeue the first command from the command pending
679 * queue and send to the firmware.
680 *
681 * If the device is currently in host sleep mode, any commands, except the
682 * host sleep configuration command will de-activate the host sleep. For PS
683 * mode, the function will put the firmware back to sleep if applicable.
684 */
685int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
686{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700687 struct mwifiex_private *priv;
688 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700689 int ret = 0;
690 struct host_cmd_ds_command *host_cmd;
691 unsigned long cmd_flags;
692 unsigned long cmd_pending_q_flags;
693
694 /* Check if already in processing */
695 if (adapter->curr_cmd) {
696 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
697 return -1;
698 }
699
700 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
701 /* Check if any command is pending */
702 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
703 if (list_empty(&adapter->cmd_pending_q)) {
704 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
705 cmd_pending_q_flags);
706 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
707 return 0;
708 }
709 cmd_node = list_first_entry(&adapter->cmd_pending_q,
710 struct cmd_ctrl_node, list);
711 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
712 cmd_pending_q_flags);
713
714 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
715 priv = cmd_node->priv;
716
717 if (adapter->ps_state != PS_STATE_AWAKE) {
718 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
719 " this should not happen\n", __func__);
720 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
721 return ret;
722 }
723
724 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
725 list_del(&cmd_node->list);
726 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
727 cmd_pending_q_flags);
728
729 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
730 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
731 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
732 /* Any command sent to the firmware when host is in sleep
733 * mode should de-configure host sleep. We should skip the
734 * host sleep configuration command itself though
735 */
736 if (priv && (host_cmd->command !=
737 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
738 if (adapter->hs_activated) {
739 adapter->is_hs_configured = false;
740 mwifiex_hs_activated_event(priv, false);
741 }
742 }
743
744 return ret;
745}
746
747/*
748 * This function handles the command response.
749 *
750 * After processing, the function cleans the command node and puts
751 * it back to the command free queue.
752 */
753int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
754{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700755 struct host_cmd_ds_command *resp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700756 struct mwifiex_private *priv =
757 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
758 int ret = 0;
759 uint16_t orig_cmdresp_no;
760 uint16_t cmdresp_no;
761 uint16_t cmdresp_result;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700762 struct timeval tstamp;
763 unsigned long flags;
764
765 /* Now we got response from FW, cancel the command timer */
766 del_timer(&adapter->cmd_timer);
767
768 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
769 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
770 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700771 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700772 return -1;
773 }
774
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775 adapter->num_cmd_timeout = 0;
776
777 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
778 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
779 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700780 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700781 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
782 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
783 adapter->curr_cmd = NULL;
784 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
785 return -1;
786 }
787
788 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
789 /* Copy original response back to response buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700790 struct mwifiex_ds_misc_cmd *hostcmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791 uint16_t size = le16_to_cpu(resp->size);
792 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
793 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
794 if (adapter->curr_cmd->data_buf) {
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700795 hostcmd = adapter->curr_cmd->data_buf;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700796 hostcmd->len = size;
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700797 memcpy(hostcmd->cmd, resp, size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700798 }
799 }
800 orig_cmdresp_no = le16_to_cpu(resp->command);
801
802 /* Get BSS number and corresponding priv */
803 priv = mwifiex_get_priv_by_id(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700804 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
805 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700806 if (!priv)
807 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
808 /* Clear RET_BIT from HostCmd */
809 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
810
811 cmdresp_no = le16_to_cpu(resp->command);
812 cmdresp_result = le16_to_cpu(resp->result);
813
814 /* Save the last command response to debug log */
815 adapter->dbg.last_cmd_resp_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700816 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700817 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700818 orig_cmdresp_no;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700819
820 do_gettimeofday(&tstamp);
821 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
822 " len %d, seqno 0x%x\n",
823 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
824 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
825
826 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
827 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700828 if (adapter->curr_cmd->wait_q_enabled)
829 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700830
831 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
832 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
833 adapter->curr_cmd = NULL;
834 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
835 return -1;
836 }
837
838 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
839 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700840 if ((cmdresp_result == HostCmd_RESULT_OK) &&
841 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700842 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
843 } else {
844 /* handle response */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700845 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700846 }
847
848 /* Check init command response */
849 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800850 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700851 dev_err(adapter->dev, "%s: cmd %#x failed during "
852 "initialization\n", __func__, cmdresp_no);
853 mwifiex_init_fw_complete(adapter);
854 return -1;
855 } else if (adapter->last_init_cmd == cmdresp_no)
856 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
857 }
858
859 if (adapter->curr_cmd) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800860 if (adapter->curr_cmd->wait_q_enabled)
861 adapter->cmd_wait_q.status = ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700862
863 /* Clean up and put current command back to cmd_free_q */
864 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
865
866 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
867 adapter->curr_cmd = NULL;
868 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
869 }
870
871 return ret;
872}
873
874/*
875 * This function handles the timeout of command sending.
876 *
877 * It will re-send the same command again.
878 */
879void
880mwifiex_cmd_timeout_func(unsigned long function_context)
881{
882 struct mwifiex_adapter *adapter =
883 (struct mwifiex_adapter *) function_context;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700884 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700885 struct timeval tstamp;
886
887 adapter->num_cmd_timeout++;
888 adapter->dbg.num_cmd_timeout++;
889 if (!adapter->curr_cmd) {
890 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
891 return;
892 }
893 cmd_node = adapter->curr_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894 if (cmd_node) {
895 adapter->dbg.timeout_cmd_id =
896 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
897 adapter->dbg.timeout_cmd_act =
898 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
899 do_gettimeofday(&tstamp);
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700900 dev_err(adapter->dev,
901 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
902 __func__, tstamp.tv_sec, tstamp.tv_usec,
903 adapter->dbg.timeout_cmd_id,
904 adapter->dbg.timeout_cmd_act);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700905
906 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700907 adapter->dbg.num_tx_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700908 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700909 adapter->dbg.num_cmd_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700910
911 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700912 adapter->dbg.num_cmd_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700913 dev_err(adapter->dev, "num_tx_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700914 adapter->dbg.num_tx_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700915
916 dev_err(adapter->dev, "last_cmd_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700917 adapter->dbg.last_cmd_index);
Andrei Emeltchenkoafe38402012-10-05 13:57:49 -0700918 dev_err(adapter->dev, "last_cmd_id: %*ph\n",
919 (int)sizeof(adapter->dbg.last_cmd_id),
920 adapter->dbg.last_cmd_id);
921 dev_err(adapter->dev, "last_cmd_act: %*ph\n",
922 (int)sizeof(adapter->dbg.last_cmd_act),
923 adapter->dbg.last_cmd_act);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700924
925 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700926 adapter->dbg.last_cmd_resp_index);
Andrei Emeltchenkoafe38402012-10-05 13:57:49 -0700927 dev_err(adapter->dev, "last_cmd_resp_id: %*ph\n",
928 (int)sizeof(adapter->dbg.last_cmd_resp_id),
929 adapter->dbg.last_cmd_resp_id);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700930
931 dev_err(adapter->dev, "last_event_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700932 adapter->dbg.last_event_index);
Andrei Emeltchenkoafe38402012-10-05 13:57:49 -0700933 dev_err(adapter->dev, "last_event: %*ph\n",
934 (int)sizeof(adapter->dbg.last_event),
935 adapter->dbg.last_event);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700936
937 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700938 adapter->data_sent, adapter->cmd_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700939
940 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700941 adapter->ps_mode, adapter->ps_state);
Bing Zhaob1a47aa2012-11-15 15:58:47 -0800942
943 if (cmd_node->wait_q_enabled) {
944 adapter->cmd_wait_q.status = -ETIMEDOUT;
945 wake_up_interruptible(&adapter->cmd_wait_q.wait);
946 mwifiex_cancel_pending_ioctl(adapter);
947 /* reset cmd_sent flag to unblock new commands */
948 adapter->cmd_sent = false;
949 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700950 }
951 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
952 mwifiex_init_fw_complete(adapter);
Amitkumar Karward31ab352012-11-01 18:44:14 -0700953
954 if (adapter->if_ops.card_reset)
955 adapter->if_ops.card_reset(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700956}
957
958/*
959 * This function cancels all the pending commands.
960 *
961 * The current command, all commands in command pending queue and all scan
962 * commands in scan pending queue are cancelled. All the completion callbacks
963 * are called with failure status to ensure cleanup.
964 */
965void
966mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
967{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700968 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700969 unsigned long flags;
970
971 /* Cancel current cmd */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700972 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700973 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700974 adapter->curr_cmd->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700975 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700976 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700977 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700978 }
979 /* Cancel all pending command */
980 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
981 list_for_each_entry_safe(cmd_node, tmp_node,
982 &adapter->cmd_pending_q, list) {
983 list_del(&cmd_node->list);
984 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
985
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700986 if (cmd_node->wait_q_enabled) {
987 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700988 mwifiex_complete_cmd(adapter, cmd_node);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700989 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700990 }
991 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
992 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
993 }
994 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
995
996 /* Cancel all pending scan command */
997 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
998 list_for_each_entry_safe(cmd_node, tmp_node,
999 &adapter->scan_pending_q, list) {
1000 list_del(&cmd_node->list);
1001 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1002
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001003 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001004 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1005 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1006 }
1007 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1008
1009 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
1010 adapter->scan_processing = false;
1011 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
1012}
1013
1014/*
1015 * This function cancels all pending commands that matches with
1016 * the given IOCTL request.
1017 *
1018 * Both the current command buffer and the pending command queue are
1019 * searched for matching IOCTL request. The completion callback of
1020 * the matched command is called with failure status to ensure cleanup.
1021 * In case of scan commands, all pending commands in scan pending queue
1022 * are cancelled.
1023 */
1024void
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001025mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001026{
1027 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1028 unsigned long cmd_flags;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001029 unsigned long scan_pending_q_flags;
1030 uint16_t cancel_scan_cmd = false;
1031
1032 if ((adapter->curr_cmd) &&
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001033 (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001034 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1035 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001036 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001037 cmd_node->cmd_flag |= CMD_F_CANCELED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001038 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
Yogesh Ashok Powar51e708c2011-12-13 20:43:16 -08001039 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1040 adapter->curr_cmd = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001041 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001042 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001043
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001044 /* Cancel all pending scan command */
1045 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1046 scan_pending_q_flags);
1047 list_for_each_entry_safe(cmd_node, tmp_node,
1048 &adapter->scan_pending_q, list) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001049 list_del(&cmd_node->list);
1050 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1051 scan_pending_q_flags);
1052 cmd_node->wait_q_enabled = false;
1053 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1054 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1055 scan_pending_q_flags);
1056 cancel_scan_cmd = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001057 }
1058 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1059 scan_pending_q_flags);
1060
1061 if (cancel_scan_cmd) {
1062 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1063 adapter->scan_processing = false;
1064 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1065 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001066 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001067}
1068
1069/*
1070 * This function sends the sleep confirm command to firmware, if
1071 * possible.
1072 *
1073 * The sleep confirm command cannot be issued if command response,
1074 * data response or event response is awaiting handling, or if we
1075 * are in the middle of sending a command, or expecting a command
1076 * response.
1077 */
1078void
1079mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1080{
1081 if (!adapter->cmd_sent &&
1082 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1083 mwifiex_dnld_sleep_confirm_cmd(adapter);
1084 else
1085 dev_dbg(adapter->dev,
1086 "cmd: Delay Sleep Confirm (%s%s%s)\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001087 (adapter->cmd_sent) ? "D" : "",
1088 (adapter->curr_cmd) ? "C" : "",
1089 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001090}
1091
1092/*
1093 * This function sends a Host Sleep activated event to applications.
1094 *
1095 * This event is generated by the driver, with a blank event body.
1096 */
1097void
1098mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1099{
1100 if (activated) {
1101 if (priv->adapter->is_hs_configured) {
1102 priv->adapter->hs_activated = true;
Avinash Patil2db96c32012-09-27 19:00:10 -07001103 mwifiex_update_rxreor_flags(priv->adapter,
1104 RXREOR_FORCE_NO_DROP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001105 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1106 priv->adapter->hs_activate_wait_q_woken = true;
1107 wake_up_interruptible(
1108 &priv->adapter->hs_activate_wait_q);
1109 } else {
1110 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1111 }
1112 } else {
1113 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1114 priv->adapter->hs_activated = false;
1115 }
1116}
1117
1118/*
1119 * This function handles the command response of a Host Sleep configuration
1120 * command.
1121 *
1122 * Handling includes changing the header fields into CPU format
1123 * and setting the current host sleep activation status in driver.
1124 *
1125 * In case host sleep status change, the function generates an event to
1126 * notify the applications.
1127 */
1128int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1129 struct host_cmd_ds_command *resp)
1130{
1131 struct mwifiex_adapter *adapter = priv->adapter;
1132 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1133 &resp->params.opt_hs_cfg;
1134 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1135
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001136 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1137 adapter->iface_type == MWIFIEX_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001138 mwifiex_hs_activated_event(priv, true);
1139 return 0;
1140 } else {
1141 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1142 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1143 resp->result, conditions,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001144 phs_cfg->params.hs_config.gpio,
1145 phs_cfg->params.hs_config.gap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001146 }
1147 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1148 adapter->is_hs_configured = true;
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001149 if (adapter->iface_type == MWIFIEX_USB ||
1150 adapter->iface_type == MWIFIEX_PCIE)
1151 mwifiex_hs_activated_event(priv, true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001152 } else {
1153 adapter->is_hs_configured = false;
1154 if (adapter->hs_activated)
1155 mwifiex_hs_activated_event(priv, false);
1156 }
1157
1158 return 0;
1159}
1160
1161/*
1162 * This function wakes up the adapter and generates a Host Sleep
1163 * cancel event on receiving the power up interrupt.
1164 */
1165void
1166mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1167{
1168 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1169 " since there is interrupt from the firmware\n", __func__);
1170
1171 adapter->if_ops.wakeup(adapter);
1172 adapter->hs_activated = false;
1173 adapter->is_hs_configured = false;
1174 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001175 MWIFIEX_BSS_ROLE_ANY),
1176 false);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001177}
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001178EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001179
1180/*
1181 * This function handles the command response of a sleep confirm command.
1182 *
1183 * The function sets the card state to SLEEP if the response indicates success.
1184 */
1185void
1186mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1187 u8 *pbuf, u32 upld_len)
1188{
1189 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1190 struct mwifiex_private *priv =
1191 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1192 uint16_t result = le16_to_cpu(cmd->result);
1193 uint16_t command = le16_to_cpu(cmd->command);
1194 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1195
1196 if (!upld_len) {
1197 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1198 return;
1199 }
1200
1201 /* Get BSS number and corresponding priv */
1202 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1203 HostCmd_GET_BSS_TYPE(seq_num));
1204 if (!priv)
1205 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1206
1207 /* Update sequence number */
1208 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1209 /* Clear RET_BIT from HostCmd */
1210 command &= HostCmd_CMD_ID_MASK;
1211
1212 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001213 dev_err(adapter->dev,
1214 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1215 __func__, command, result);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001216 return;
1217 }
1218
1219 if (result) {
1220 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001221 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001222 adapter->pm_wakeup_card_req = false;
1223 adapter->ps_state = PS_STATE_AWAKE;
1224 return;
1225 }
1226 adapter->pm_wakeup_card_req = true;
1227 if (adapter->is_hs_configured)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001228 mwifiex_hs_activated_event(mwifiex_get_priv
1229 (adapter, MWIFIEX_BSS_ROLE_ANY),
1230 true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001231 adapter->ps_state = PS_STATE_SLEEP;
1232 cmd->command = cpu_to_le16(command);
1233 cmd->seq_num = cpu_to_le16(seq_num);
1234}
1235EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1236
1237/*
1238 * This function prepares an enhanced power mode command.
1239 *
1240 * This function can be used to disable power save or to configure
1241 * power save with auto PS or STA PS or auto deep sleep.
1242 *
1243 * Preparation includes -
1244 * - Setting command ID, action and proper size
1245 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1246 * auto deep sleep TLV (as required)
1247 * - Ensuring correct endian-ness
1248 */
1249int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1250 struct host_cmd_ds_command *cmd,
1251 u16 cmd_action, uint16_t ps_bitmap,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001252 struct mwifiex_ds_auto_ds *auto_ds)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253{
1254 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1255 &cmd->params.psmode_enh;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001256 u8 *tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001257 u16 cmd_size = 0;
1258
1259 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1260 if (cmd_action == DIS_AUTO_PS) {
1261 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1262 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001263 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001264 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001265 } else if (cmd_action == GET_PS) {
1266 psmode_enh->action = cpu_to_le16(GET_PS);
1267 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001268 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001269 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001270 } else if (cmd_action == EN_AUTO_PS) {
1271 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001272 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1273 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001274 sizeof(psmode_enh->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001275 tlv = (u8 *) cmd + cmd_size;
1276 if (ps_bitmap & BITMAP_STA_PS) {
1277 struct mwifiex_adapter *adapter = priv->adapter;
1278 struct mwifiex_ie_types_ps_param *ps_tlv =
1279 (struct mwifiex_ie_types_ps_param *) tlv;
1280 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1281 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1282 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1283 sizeof(struct mwifiex_ie_types_header));
1284 cmd_size += sizeof(*ps_tlv);
1285 tlv += sizeof(*ps_tlv);
1286 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1287 ps_mode->null_pkt_interval =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001288 cpu_to_le16(adapter->null_pkt_interval);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001289 ps_mode->multiple_dtims =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001290 cpu_to_le16(adapter->multiple_dtim);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001291 ps_mode->bcn_miss_timeout =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001292 cpu_to_le16(adapter->bcn_miss_time_out);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001293 ps_mode->local_listen_interval =
1294 cpu_to_le16(adapter->local_listen_interval);
1295 ps_mode->adhoc_wake_period =
1296 cpu_to_le16(adapter->adhoc_awake_period);
1297 ps_mode->delay_to_ps =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001298 cpu_to_le16(adapter->delay_to_ps);
1299 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001300
1301 }
1302 if (ps_bitmap & BITMAP_AUTO_DS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001303 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001304 (struct mwifiex_ie_types_auto_ds_param *) tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001305 u16 idletime = 0;
Marc Yang2b06bdb2011-03-30 18:12:44 -07001306
1307 auto_ds_tlv->header.type =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001308 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001309 auto_ds_tlv->header.len =
1310 cpu_to_le16(sizeof(*auto_ds_tlv) -
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001311 sizeof(struct mwifiex_ie_types_header));
Marc Yang2b06bdb2011-03-30 18:12:44 -07001312 cmd_size += sizeof(*auto_ds_tlv);
1313 tlv += sizeof(*auto_ds_tlv);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001314 if (auto_ds)
1315 idletime = auto_ds->idle_time;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001316 dev_dbg(priv->adapter->dev,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001317 "cmd: PS Command: Enter Auto Deep Sleep\n");
Marc Yang2b06bdb2011-03-30 18:12:44 -07001318 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001319 }
1320 cmd->size = cpu_to_le16(cmd_size);
1321 }
1322 return 0;
1323}
1324
1325/*
1326 * This function handles the command response of an enhanced power mode
1327 * command.
1328 *
1329 * Handling includes changing the header fields into CPU format
1330 * and setting the current enhanced power mode in driver.
1331 */
1332int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1333 struct host_cmd_ds_command *resp,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001334 struct mwifiex_ds_pm_cfg *pm_cfg)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001335{
1336 struct mwifiex_adapter *adapter = priv->adapter;
1337 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1338 &resp->params.psmode_enh;
1339 uint16_t action = le16_to_cpu(ps_mode->action);
1340 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1341 uint16_t auto_ps_bitmap =
Marc Yang2b06bdb2011-03-30 18:12:44 -07001342 le16_to_cpu(ps_mode->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001343
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001344 dev_dbg(adapter->dev,
1345 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1346 __func__, resp->result, action);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001347 if (action == EN_AUTO_PS) {
1348 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1349 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1350 priv->adapter->is_deep_sleep = true;
1351 }
1352 if (auto_ps_bitmap & BITMAP_STA_PS) {
1353 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1354 if (adapter->sleep_period.period)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001355 dev_dbg(adapter->dev,
1356 "cmd: set to uapsd/pps mode\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001357 }
1358 } else if (action == DIS_AUTO_PS) {
1359 if (ps_bitmap & BITMAP_AUTO_DS) {
1360 priv->adapter->is_deep_sleep = false;
1361 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1362 }
1363 if (ps_bitmap & BITMAP_STA_PS) {
1364 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1365 if (adapter->sleep_period.period) {
1366 adapter->delay_null_pkt = false;
1367 adapter->tx_lock_flag = false;
1368 adapter->pps_uapsd_mode = false;
1369 }
1370 }
1371 } else if (action == GET_PS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001372 if (ps_bitmap & BITMAP_STA_PS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001373 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1374 else
1375 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1376
1377 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1378
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001379 if (pm_cfg) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001380 /* This section is for get power save mode */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001381 if (ps_bitmap & BITMAP_STA_PS)
1382 pm_cfg->param.ps_mode = 1;
1383 else
1384 pm_cfg->param.ps_mode = 0;
1385 }
1386 }
1387 return 0;
1388}
1389
1390/*
1391 * This function prepares command to get hardware specifications.
1392 *
1393 * Preparation includes -
1394 * - Setting command ID, action and proper size
1395 * - Setting permanent address parameter
1396 * - Ensuring correct endian-ness
1397 */
1398int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1399 struct host_cmd_ds_command *cmd)
1400{
1401 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1402
1403 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1404 cmd->size =
1405 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1406 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1407
1408 return 0;
1409}
1410
1411/*
1412 * This function handles the command response of get hardware
1413 * specifications.
1414 *
1415 * Handling includes changing the header fields into CPU format
1416 * and saving/updating the following parameters in driver -
1417 * - Firmware capability information
1418 * - Firmware band settings
1419 * - Ad-hoc start band and channel
1420 * - Ad-hoc 11n activation status
1421 * - Firmware release number
1422 * - Number of antennas
1423 * - Hardware address
1424 * - Hardware interface version
1425 * - Firmware version
1426 * - Region code
1427 * - 11n capabilities
1428 * - MCS support fields
1429 * - MP end port
1430 */
1431int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1432 struct host_cmd_ds_command *resp)
1433{
1434 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1435 struct mwifiex_adapter *adapter = priv->adapter;
1436 int i;
1437
1438 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1439
1440 if (IS_SUPPORT_MULTI_BANDS(adapter))
1441 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1442 else
1443 adapter->fw_bands = BAND_B;
1444
1445 adapter->config_bands = adapter->fw_bands;
1446
1447 if (adapter->fw_bands & BAND_A) {
1448 if (adapter->fw_bands & BAND_GN) {
1449 adapter->config_bands |= BAND_AN;
1450 adapter->fw_bands |= BAND_AN;
1451 }
1452 if (adapter->fw_bands & BAND_AN) {
1453 adapter->adhoc_start_band = BAND_A | BAND_AN;
1454 adapter->adhoc_11n_enabled = true;
1455 } else {
1456 adapter->adhoc_start_band = BAND_A;
1457 }
1458 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1459 } else if (adapter->fw_bands & BAND_GN) {
1460 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1461 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1462 adapter->adhoc_11n_enabled = true;
1463 } else if (adapter->fw_bands & BAND_G) {
1464 adapter->adhoc_start_band = BAND_G | BAND_B;
1465 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1466 } else if (adapter->fw_bands & BAND_B) {
1467 adapter->adhoc_start_band = BAND_B;
1468 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1469 }
1470
1471 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1472 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1473
Yogesh Ashok Powara5f39052013-02-15 21:44:30 -08001474 if (le32_to_cpu(hw_spec->dot_11ac_dev_cap)) {
1475 adapter->is_hw_11ac_capable = true;
1476
1477 /* Copy 11AC cap */
1478 adapter->hw_dot_11ac_dev_cap =
1479 le32_to_cpu(hw_spec->dot_11ac_dev_cap);
1480 adapter->usr_dot_11ac_dev_cap_bg = adapter->hw_dot_11ac_dev_cap;
1481 adapter->usr_dot_11ac_dev_cap_a = adapter->hw_dot_11ac_dev_cap;
1482
1483 /* Copy 11AC mcs */
1484 adapter->hw_dot_11ac_mcs_support =
1485 le32_to_cpu(hw_spec->dot_11ac_mcs_support);
1486 adapter->usr_dot_11ac_mcs_support =
1487 adapter->hw_dot_11ac_mcs_support;
1488 } else {
1489 adapter->is_hw_11ac_capable = false;
1490 }
1491
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001492 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001493 adapter->fw_release_number);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001494 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001495 hw_spec->permanent_addr);
1496 dev_dbg(adapter->dev,
1497 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001498 le16_to_cpu(hw_spec->hw_if_version),
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001499 le16_to_cpu(hw_spec->version));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001500
1501 if (priv->curr_addr[0] == 0xff)
1502 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1503
1504 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1505
1506 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1507 /* Use the region code to search for the index */
1508 if (adapter->region_code == region_code_index[i])
1509 break;
1510
1511 /* If it's unidentified region code, use the default (USA) */
1512 if (i >= MWIFIEX_MAX_REGION_CODE) {
1513 adapter->region_code = 0x10;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001514 dev_dbg(adapter->dev,
1515 "cmd: unknown region code, use default (USA)\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001516 }
1517
1518 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001519 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001520
1521 if (adapter->if_ops.update_mp_end_port)
1522 adapter->if_ops.update_mp_end_port(adapter,
1523 le16_to_cpu(hw_spec->mp_end_port));
1524
1525 return 0;
1526}