blob: 2ffabddbcfcaf305ad27cb789dfc6627daf1189c [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;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700338 u32 i;
339
340 /* Allocate and initialize struct cmd_ctrl_node */
Joe Perches0d2e7a52013-02-03 17:28:14 +0000341 cmd_array = kcalloc(MWIFIEX_NUM_OF_CMD_BUFFER,
342 sizeof(struct cmd_ctrl_node), GFP_KERNEL);
343 if (!cmd_array)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200344 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700345
346 adapter->cmd_pool = cmd_array;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700347
348 /* Allocate and initialize command buffers */
349 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
350 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
351 if (!cmd_array[i].skb) {
352 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
353 return -1;
354 }
355 }
356
357 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
358 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
359
360 return 0;
361}
362
363/*
364 * This function frees the command buffers.
365 *
366 * The function calls the completion callback for all the command
367 * buffers that still have response buffers associated with them.
368 */
369int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
370{
371 struct cmd_ctrl_node *cmd_array;
372 u32 i;
373
374 /* Need to check if cmd pool is allocated or not */
375 if (!adapter->cmd_pool) {
376 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
377 return 0;
378 }
379
380 cmd_array = adapter->cmd_pool;
381
382 /* Release shared memory buffers */
383 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
384 if (cmd_array[i].skb) {
385 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
386 dev_kfree_skb_any(cmd_array[i].skb);
387 }
388 if (!cmd_array[i].resp_skb)
389 continue;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700390
391 if (adapter->iface_type == MWIFIEX_USB)
392 adapter->if_ops.cmdrsp_complete(adapter,
393 cmd_array[i].resp_skb);
394 else
395 dev_kfree_skb_any(cmd_array[i].resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700396 }
397 /* Release struct cmd_ctrl_node */
398 if (adapter->cmd_pool) {
399 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
400 kfree(adapter->cmd_pool);
401 adapter->cmd_pool = NULL;
402 }
403
404 return 0;
405}
406
407/*
408 * This function handles events generated by firmware.
409 *
410 * Event body of events received from firmware are not used (though they are
411 * saved), only the event ID is used. Some events are re-invoked by
412 * the driver, with a new event body.
413 *
414 * After processing, the function calls the completion callback
415 * for cleanup.
416 */
417int mwifiex_process_event(struct mwifiex_adapter *adapter)
418{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700419 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700420 struct mwifiex_private *priv =
421 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
422 struct sk_buff *skb = adapter->event_skb;
423 u32 eventcause = adapter->event_cause;
424 struct timeval tstamp;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700425 struct mwifiex_rxinfo *rx_info;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700426
427 /* Save the last event to debug log */
428 adapter->dbg.last_event_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700429 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700430 adapter->dbg.last_event[adapter->dbg.last_event_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700431 (u16) eventcause;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700432
433 /* Get BSS number and corresponding priv */
434 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
435 EVENT_GET_BSS_TYPE(eventcause));
436 if (!priv)
437 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
438 /* Clear BSS_NO_BITS from event */
439 eventcause &= EVENT_ID_MASK;
440 adapter->event_cause = eventcause;
441
442 if (skb) {
443 rx_info = MWIFIEX_SKB_RXCB(skb);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800444 rx_info->bss_num = priv->bss_num;
445 rx_info->bss_type = priv->bss_type;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700446 }
447
448 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
449 do_gettimeofday(&tstamp);
450 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700451 tstamp.tv_sec, tstamp.tv_usec, eventcause);
Avinash Patil03785382012-05-08 18:30:14 -0700452 } else {
453 /* Handle PS_SLEEP/AWAKE events on STA */
454 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
455 if (!priv)
456 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700457 }
458
Avinash Patil3d99d982012-08-03 18:06:06 -0700459 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
460 ret = mwifiex_process_uap_event(priv);
461 else
462 ret = mwifiex_process_sta_event(priv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700463
464 adapter->event_cause = 0;
465 adapter->event_skb = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700466 adapter->if_ops.event_complete(adapter, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467
468 return ret;
469}
470
471/*
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700472 * This function is used to send synchronous command to the firmware.
473 *
474 * it allocates a wait queue for the command and wait for the command
475 * response.
476 */
477int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
478 u16 cmd_action, u32 cmd_oid, void *data_buf)
479{
480 int ret = 0;
481 struct mwifiex_adapter *adapter = priv->adapter;
482
483 adapter->cmd_wait_q_required = true;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700484
485 ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
486 data_buf);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700487
488 return ret;
489}
490
491
492/*
493 * This function prepares a command and asynchronously send it to the firmware.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700494 *
495 * Preparation includes -
496 * - Sanity tests to make sure the card is still present or the FW
497 * is not reset
498 * - Getting a new command node from the command free queue
499 * - Initializing the command node for default parameters
500 * - Fill up the non-default parameters and buffer pointers
501 * - Add the command to pending queue
502 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700503int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
504 u16 cmd_action, u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700505{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700506 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700507 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700508 struct cmd_ctrl_node *cmd_node;
509 struct host_cmd_ds_command *cmd_ptr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510
511 if (!adapter) {
512 pr_err("PREP_CMD: adapter is NULL\n");
513 return -1;
514 }
515
516 if (adapter->is_suspended) {
517 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
518 return -1;
519 }
520
521 if (adapter->surprise_removed) {
522 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
523 return -1;
524 }
525
526 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
527 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
528 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
529 return -1;
530 }
531 }
532
533 /* Get a new command node */
534 cmd_node = mwifiex_get_cmd_node(adapter);
535
536 if (!cmd_node) {
537 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
538 return -1;
539 }
540
541 /* Initialize the command node */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700542 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700543
544 if (!cmd_node->cmd_skb) {
545 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
546 return -1;
547 }
548
549 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
550 0, sizeof(struct host_cmd_ds_command));
551
552 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
553 cmd_ptr->command = cpu_to_le16(cmd_no);
554 cmd_ptr->result = 0;
555
556 /* Prepare command */
557 if (cmd_no) {
Avinash Patil40d07032012-05-08 18:30:19 -0700558 switch (cmd_no) {
559 case HostCmd_CMD_UAP_SYS_CONFIG:
560 case HostCmd_CMD_UAP_BSS_START:
561 case HostCmd_CMD_UAP_BSS_STOP:
562 ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
563 cmd_oid, data_buf,
564 cmd_ptr);
565 break;
566 default:
567 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
568 cmd_oid, data_buf,
569 cmd_ptr);
570 break;
571 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700572 } else {
573 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
574 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
575 }
576
577 /* Return error, since the command preparation failed */
578 if (ret) {
579 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700580 cmd_no);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700581 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
582 return -1;
583 }
584
585 /* Send command */
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700586 if (cmd_no == HostCmd_CMD_802_11_SCAN) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700587 mwifiex_queue_scan_cmd(priv, cmd_node);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700588 } else {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700589 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
Amitkumar Karwar1a1fb972012-06-27 19:57:56 -0700590 queue_work(adapter->workqueue, &adapter->main_work);
Amitkumar Karwar00d7ea12013-03-15 18:47:05 -0700591 if (cmd_node->wait_q_enabled)
592 ret = mwifiex_wait_queue_complete(adapter, cmd_node);
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700593 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700594
595 return ret;
596}
597
598/*
599 * This function returns a command to the command free queue.
600 *
601 * The function also calls the completion callback if required, before
602 * cleaning the command node and re-inserting it into the free queue.
603 */
604void
605mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
606 struct cmd_ctrl_node *cmd_node)
607{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700608 unsigned long flags;
609
Yogesh Ashok Powar19a89862011-04-13 17:27:07 -0700610 if (!cmd_node)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611 return;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700612
613 if (cmd_node->wait_q_enabled)
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700614 mwifiex_complete_cmd(adapter, cmd_node);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700615 /* Clean the node */
616 mwifiex_clean_cmd_node(adapter, cmd_node);
617
618 /* Insert node into cmd_free_q */
619 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
620 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
621 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700622}
623
624/*
625 * This function queues a command to the command pending queue.
626 *
627 * This in effect adds the command to the command list to be executed.
628 * Exit PS command is handled specially, by placing it always to the
629 * front of the command queue.
630 */
631void
632mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
633 struct cmd_ctrl_node *cmd_node, u32 add_tail)
634{
635 struct host_cmd_ds_command *host_cmd = NULL;
636 u16 command;
637 unsigned long flags;
638
639 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
640 if (!host_cmd) {
641 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
642 return;
643 }
644
645 command = le16_to_cpu(host_cmd->command);
646
647 /* Exit_PS command needs to be queued in the header always. */
648 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
649 struct host_cmd_ds_802_11_ps_mode_enh *pm =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700650 &host_cmd->params.psmode_enh;
651 if ((le16_to_cpu(pm->action) == DIS_PS) ||
652 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700653 if (adapter->ps_state != PS_STATE_AWAKE)
654 add_tail = false;
655 }
656 }
657
658 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
659 if (add_tail)
660 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
661 else
662 list_add(&cmd_node->list, &adapter->cmd_pending_q);
663 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
664
665 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700666}
667
668/*
669 * This function executes the next command in command pending queue.
670 *
671 * This function will fail if a command is already in processing stage,
672 * otherwise it will dequeue the first command from the command pending
673 * queue and send to the firmware.
674 *
675 * If the device is currently in host sleep mode, any commands, except the
676 * host sleep configuration command will de-activate the host sleep. For PS
677 * mode, the function will put the firmware back to sleep if applicable.
678 */
679int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
680{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700681 struct mwifiex_private *priv;
682 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700683 int ret = 0;
684 struct host_cmd_ds_command *host_cmd;
685 unsigned long cmd_flags;
686 unsigned long cmd_pending_q_flags;
687
688 /* Check if already in processing */
689 if (adapter->curr_cmd) {
690 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
691 return -1;
692 }
693
694 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
695 /* Check if any command is pending */
696 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
697 if (list_empty(&adapter->cmd_pending_q)) {
698 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
699 cmd_pending_q_flags);
700 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
701 return 0;
702 }
703 cmd_node = list_first_entry(&adapter->cmd_pending_q,
704 struct cmd_ctrl_node, list);
705 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
706 cmd_pending_q_flags);
707
708 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
709 priv = cmd_node->priv;
710
711 if (adapter->ps_state != PS_STATE_AWAKE) {
712 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
713 " this should not happen\n", __func__);
714 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
715 return ret;
716 }
717
718 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
719 list_del(&cmd_node->list);
720 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
721 cmd_pending_q_flags);
722
723 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
724 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
725 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
726 /* Any command sent to the firmware when host is in sleep
727 * mode should de-configure host sleep. We should skip the
728 * host sleep configuration command itself though
729 */
730 if (priv && (host_cmd->command !=
731 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
732 if (adapter->hs_activated) {
733 adapter->is_hs_configured = false;
734 mwifiex_hs_activated_event(priv, false);
735 }
736 }
737
738 return ret;
739}
740
741/*
742 * This function handles the command response.
743 *
744 * After processing, the function cleans the command node and puts
745 * it back to the command free queue.
746 */
747int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
748{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700749 struct host_cmd_ds_command *resp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700750 struct mwifiex_private *priv =
751 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
752 int ret = 0;
753 uint16_t orig_cmdresp_no;
754 uint16_t cmdresp_no;
755 uint16_t cmdresp_result;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700756 struct timeval tstamp;
757 unsigned long flags;
758
759 /* Now we got response from FW, cancel the command timer */
760 del_timer(&adapter->cmd_timer);
761
762 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
763 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
764 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700765 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700766 return -1;
767 }
768
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700769 adapter->num_cmd_timeout = 0;
770
771 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
772 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
773 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700774 le16_to_cpu(resp->command));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
776 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
777 adapter->curr_cmd = NULL;
778 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
779 return -1;
780 }
781
782 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
783 /* Copy original response back to response buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700784 struct mwifiex_ds_misc_cmd *hostcmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700785 uint16_t size = le16_to_cpu(resp->size);
786 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
787 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
788 if (adapter->curr_cmd->data_buf) {
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700789 hostcmd = adapter->curr_cmd->data_buf;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700790 hostcmd->len = size;
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700791 memcpy(hostcmd->cmd, resp, size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792 }
793 }
794 orig_cmdresp_no = le16_to_cpu(resp->command);
795
796 /* Get BSS number and corresponding priv */
797 priv = mwifiex_get_priv_by_id(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700798 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
799 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700800 if (!priv)
801 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
802 /* Clear RET_BIT from HostCmd */
803 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
804
805 cmdresp_no = le16_to_cpu(resp->command);
806 cmdresp_result = le16_to_cpu(resp->result);
807
808 /* Save the last command response to debug log */
809 adapter->dbg.last_cmd_resp_index =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700810 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700811 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700812 orig_cmdresp_no;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700813
814 do_gettimeofday(&tstamp);
815 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
816 " len %d, seqno 0x%x\n",
817 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
818 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
819
820 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
821 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700822 if (adapter->curr_cmd->wait_q_enabled)
823 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700824
825 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
826 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
827 adapter->curr_cmd = NULL;
828 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
829 return -1;
830 }
831
832 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
833 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700834 if ((cmdresp_result == HostCmd_RESULT_OK) &&
835 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700836 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
837 } else {
838 /* handle response */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700839 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700840 }
841
842 /* Check init command response */
843 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800844 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700845 dev_err(adapter->dev, "%s: cmd %#x failed during "
846 "initialization\n", __func__, cmdresp_no);
847 mwifiex_init_fw_complete(adapter);
848 return -1;
849 } else if (adapter->last_init_cmd == cmdresp_no)
850 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
851 }
852
853 if (adapter->curr_cmd) {
Amitkumar Karwara0f6d6c2012-02-24 21:36:05 -0800854 if (adapter->curr_cmd->wait_q_enabled)
855 adapter->cmd_wait_q.status = ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700856
857 /* Clean up and put current command back to cmd_free_q */
858 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
859
860 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
861 adapter->curr_cmd = NULL;
862 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
863 }
864
865 return ret;
866}
867
868/*
869 * This function handles the timeout of command sending.
870 *
871 * It will re-send the same command again.
872 */
873void
874mwifiex_cmd_timeout_func(unsigned long function_context)
875{
876 struct mwifiex_adapter *adapter =
877 (struct mwifiex_adapter *) function_context;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700878 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700879 struct timeval tstamp;
880
881 adapter->num_cmd_timeout++;
882 adapter->dbg.num_cmd_timeout++;
883 if (!adapter->curr_cmd) {
884 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
885 return;
886 }
887 cmd_node = adapter->curr_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700888 if (cmd_node) {
889 adapter->dbg.timeout_cmd_id =
890 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
891 adapter->dbg.timeout_cmd_act =
892 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
893 do_gettimeofday(&tstamp);
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700894 dev_err(adapter->dev,
895 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
896 __func__, tstamp.tv_sec, tstamp.tv_usec,
897 adapter->dbg.timeout_cmd_id,
898 adapter->dbg.timeout_cmd_act);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700899
900 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700901 adapter->dbg.num_tx_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700902 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700903 adapter->dbg.num_cmd_host_to_card_failure);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700904
905 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700906 adapter->dbg.num_cmd_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700907 dev_err(adapter->dev, "num_tx_timeout = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700908 adapter->dbg.num_tx_timeout);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700909
910 dev_err(adapter->dev, "last_cmd_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700911 adapter->dbg.last_cmd_index);
Andrei Emeltchenkoafe38402012-10-05 13:57:49 -0700912 dev_err(adapter->dev, "last_cmd_id: %*ph\n",
913 (int)sizeof(adapter->dbg.last_cmd_id),
914 adapter->dbg.last_cmd_id);
915 dev_err(adapter->dev, "last_cmd_act: %*ph\n",
916 (int)sizeof(adapter->dbg.last_cmd_act),
917 adapter->dbg.last_cmd_act);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700918
919 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700920 adapter->dbg.last_cmd_resp_index);
Andrei Emeltchenkoafe38402012-10-05 13:57:49 -0700921 dev_err(adapter->dev, "last_cmd_resp_id: %*ph\n",
922 (int)sizeof(adapter->dbg.last_cmd_resp_id),
923 adapter->dbg.last_cmd_resp_id);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700924
925 dev_err(adapter->dev, "last_event_index = %d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700926 adapter->dbg.last_event_index);
Andrei Emeltchenkoafe38402012-10-05 13:57:49 -0700927 dev_err(adapter->dev, "last_event: %*ph\n",
928 (int)sizeof(adapter->dbg.last_event),
929 adapter->dbg.last_event);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700930
931 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700932 adapter->data_sent, adapter->cmd_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700933
934 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -0700935 adapter->ps_mode, adapter->ps_state);
Bing Zhaob1a47aa2012-11-15 15:58:47 -0800936
937 if (cmd_node->wait_q_enabled) {
938 adapter->cmd_wait_q.status = -ETIMEDOUT;
939 wake_up_interruptible(&adapter->cmd_wait_q.wait);
940 mwifiex_cancel_pending_ioctl(adapter);
941 /* reset cmd_sent flag to unblock new commands */
942 adapter->cmd_sent = false;
943 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700944 }
945 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
946 mwifiex_init_fw_complete(adapter);
Amitkumar Karward31ab352012-11-01 18:44:14 -0700947
948 if (adapter->if_ops.card_reset)
949 adapter->if_ops.card_reset(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700950}
951
952/*
953 * This function cancels all the pending commands.
954 *
955 * The current command, all commands in command pending queue and all scan
956 * commands in scan pending queue are cancelled. All the completion callbacks
957 * are called with failure status to ensure cleanup.
958 */
959void
960mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
961{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700962 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700963 unsigned long flags;
964
965 /* Cancel current cmd */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700966 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700967 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700968 adapter->curr_cmd->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700969 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700970 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700971 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700972 }
973 /* Cancel all pending command */
974 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
975 list_for_each_entry_safe(cmd_node, tmp_node,
976 &adapter->cmd_pending_q, list) {
977 list_del(&cmd_node->list);
978 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
979
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700980 if (cmd_node->wait_q_enabled) {
981 adapter->cmd_wait_q.status = -1;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700982 mwifiex_complete_cmd(adapter, cmd_node);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700983 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700984 }
985 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
986 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
987 }
988 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
989
990 /* Cancel all pending scan command */
991 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
992 list_for_each_entry_safe(cmd_node, tmp_node,
993 &adapter->scan_pending_q, list) {
994 list_del(&cmd_node->list);
995 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
996
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700997 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700998 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
999 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1000 }
1001 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1002
1003 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
1004 adapter->scan_processing = false;
1005 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
1006}
1007
1008/*
1009 * This function cancels all pending commands that matches with
1010 * the given IOCTL request.
1011 *
1012 * Both the current command buffer and the pending command queue are
1013 * searched for matching IOCTL request. The completion callback of
1014 * the matched command is called with failure status to ensure cleanup.
1015 * In case of scan commands, all pending commands in scan pending queue
1016 * are cancelled.
1017 */
1018void
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001019mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001020{
1021 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1022 unsigned long cmd_flags;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001023 unsigned long scan_pending_q_flags;
1024 uint16_t cancel_scan_cmd = false;
1025
1026 if ((adapter->curr_cmd) &&
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001027 (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001028 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1029 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001030 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001031 cmd_node->cmd_flag |= CMD_F_CANCELED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001032 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
Yogesh Ashok Powar51e708c2011-12-13 20:43:16 -08001033 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1034 adapter->curr_cmd = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001035 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001036 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001037
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001038 /* Cancel all pending scan command */
1039 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1040 scan_pending_q_flags);
1041 list_for_each_entry_safe(cmd_node, tmp_node,
1042 &adapter->scan_pending_q, list) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001043 list_del(&cmd_node->list);
1044 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1045 scan_pending_q_flags);
1046 cmd_node->wait_q_enabled = false;
1047 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1048 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1049 scan_pending_q_flags);
1050 cancel_scan_cmd = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001051 }
1052 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1053 scan_pending_q_flags);
1054
1055 if (cancel_scan_cmd) {
1056 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1057 adapter->scan_processing = false;
1058 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1059 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001060 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001061}
1062
1063/*
1064 * This function sends the sleep confirm command to firmware, if
1065 * possible.
1066 *
1067 * The sleep confirm command cannot be issued if command response,
1068 * data response or event response is awaiting handling, or if we
1069 * are in the middle of sending a command, or expecting a command
1070 * response.
1071 */
1072void
1073mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1074{
1075 if (!adapter->cmd_sent &&
1076 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1077 mwifiex_dnld_sleep_confirm_cmd(adapter);
1078 else
1079 dev_dbg(adapter->dev,
1080 "cmd: Delay Sleep Confirm (%s%s%s)\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001081 (adapter->cmd_sent) ? "D" : "",
1082 (adapter->curr_cmd) ? "C" : "",
1083 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001084}
1085
1086/*
1087 * This function sends a Host Sleep activated event to applications.
1088 *
1089 * This event is generated by the driver, with a blank event body.
1090 */
1091void
1092mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1093{
1094 if (activated) {
1095 if (priv->adapter->is_hs_configured) {
1096 priv->adapter->hs_activated = true;
Avinash Patil2db96c32012-09-27 19:00:10 -07001097 mwifiex_update_rxreor_flags(priv->adapter,
1098 RXREOR_FORCE_NO_DROP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001099 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1100 priv->adapter->hs_activate_wait_q_woken = true;
1101 wake_up_interruptible(
1102 &priv->adapter->hs_activate_wait_q);
1103 } else {
1104 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1105 }
1106 } else {
1107 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1108 priv->adapter->hs_activated = false;
1109 }
1110}
1111
1112/*
1113 * This function handles the command response of a Host Sleep configuration
1114 * command.
1115 *
1116 * Handling includes changing the header fields into CPU format
1117 * and setting the current host sleep activation status in driver.
1118 *
1119 * In case host sleep status change, the function generates an event to
1120 * notify the applications.
1121 */
1122int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1123 struct host_cmd_ds_command *resp)
1124{
1125 struct mwifiex_adapter *adapter = priv->adapter;
1126 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1127 &resp->params.opt_hs_cfg;
1128 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1129
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001130 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1131 adapter->iface_type == MWIFIEX_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001132 mwifiex_hs_activated_event(priv, true);
1133 return 0;
1134 } else {
1135 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1136 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1137 resp->result, conditions,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001138 phs_cfg->params.hs_config.gpio,
1139 phs_cfg->params.hs_config.gap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001140 }
1141 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1142 adapter->is_hs_configured = true;
Amitkumar Karwara4186ea2012-06-20 19:58:37 -07001143 if (adapter->iface_type == MWIFIEX_USB ||
1144 adapter->iface_type == MWIFIEX_PCIE)
1145 mwifiex_hs_activated_event(priv, true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001146 } else {
1147 adapter->is_hs_configured = false;
1148 if (adapter->hs_activated)
1149 mwifiex_hs_activated_event(priv, false);
1150 }
1151
1152 return 0;
1153}
1154
1155/*
1156 * This function wakes up the adapter and generates a Host Sleep
1157 * cancel event on receiving the power up interrupt.
1158 */
1159void
1160mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1161{
1162 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1163 " since there is interrupt from the firmware\n", __func__);
1164
1165 adapter->if_ops.wakeup(adapter);
1166 adapter->hs_activated = false;
1167 adapter->is_hs_configured = false;
1168 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001169 MWIFIEX_BSS_ROLE_ANY),
1170 false);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001171}
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001172EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001173
1174/*
1175 * This function handles the command response of a sleep confirm command.
1176 *
1177 * The function sets the card state to SLEEP if the response indicates success.
1178 */
1179void
1180mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1181 u8 *pbuf, u32 upld_len)
1182{
1183 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1184 struct mwifiex_private *priv =
1185 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1186 uint16_t result = le16_to_cpu(cmd->result);
1187 uint16_t command = le16_to_cpu(cmd->command);
1188 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1189
1190 if (!upld_len) {
1191 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1192 return;
1193 }
1194
1195 /* Get BSS number and corresponding priv */
1196 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1197 HostCmd_GET_BSS_TYPE(seq_num));
1198 if (!priv)
1199 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1200
1201 /* Update sequence number */
1202 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1203 /* Clear RET_BIT from HostCmd */
1204 command &= HostCmd_CMD_ID_MASK;
1205
1206 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001207 dev_err(adapter->dev,
1208 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1209 __func__, command, result);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001210 return;
1211 }
1212
1213 if (result) {
1214 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001215 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001216 adapter->pm_wakeup_card_req = false;
1217 adapter->ps_state = PS_STATE_AWAKE;
1218 return;
1219 }
1220 adapter->pm_wakeup_card_req = true;
1221 if (adapter->is_hs_configured)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001222 mwifiex_hs_activated_event(mwifiex_get_priv
1223 (adapter, MWIFIEX_BSS_ROLE_ANY),
1224 true);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001225 adapter->ps_state = PS_STATE_SLEEP;
1226 cmd->command = cpu_to_le16(command);
1227 cmd->seq_num = cpu_to_le16(seq_num);
1228}
1229EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1230
1231/*
1232 * This function prepares an enhanced power mode command.
1233 *
1234 * This function can be used to disable power save or to configure
1235 * power save with auto PS or STA PS or auto deep sleep.
1236 *
1237 * Preparation includes -
1238 * - Setting command ID, action and proper size
1239 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1240 * auto deep sleep TLV (as required)
1241 * - Ensuring correct endian-ness
1242 */
1243int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1244 struct host_cmd_ds_command *cmd,
1245 u16 cmd_action, uint16_t ps_bitmap,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001246 struct mwifiex_ds_auto_ds *auto_ds)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001247{
1248 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1249 &cmd->params.psmode_enh;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001250 u8 *tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001251 u16 cmd_size = 0;
1252
1253 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1254 if (cmd_action == DIS_AUTO_PS) {
1255 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1256 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001257 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001258 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001259 } else if (cmd_action == GET_PS) {
1260 psmode_enh->action = cpu_to_le16(GET_PS);
1261 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001262 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001263 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001264 } else if (cmd_action == EN_AUTO_PS) {
1265 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001266 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1267 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001268 sizeof(psmode_enh->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001269 tlv = (u8 *) cmd + cmd_size;
1270 if (ps_bitmap & BITMAP_STA_PS) {
1271 struct mwifiex_adapter *adapter = priv->adapter;
1272 struct mwifiex_ie_types_ps_param *ps_tlv =
1273 (struct mwifiex_ie_types_ps_param *) tlv;
1274 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1275 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1276 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1277 sizeof(struct mwifiex_ie_types_header));
1278 cmd_size += sizeof(*ps_tlv);
1279 tlv += sizeof(*ps_tlv);
1280 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1281 ps_mode->null_pkt_interval =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001282 cpu_to_le16(adapter->null_pkt_interval);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001283 ps_mode->multiple_dtims =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001284 cpu_to_le16(adapter->multiple_dtim);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001285 ps_mode->bcn_miss_timeout =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001286 cpu_to_le16(adapter->bcn_miss_time_out);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001287 ps_mode->local_listen_interval =
1288 cpu_to_le16(adapter->local_listen_interval);
1289 ps_mode->adhoc_wake_period =
1290 cpu_to_le16(adapter->adhoc_awake_period);
1291 ps_mode->delay_to_ps =
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001292 cpu_to_le16(adapter->delay_to_ps);
1293 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001294
1295 }
1296 if (ps_bitmap & BITMAP_AUTO_DS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001297 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001298 (struct mwifiex_ie_types_auto_ds_param *) tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001299 u16 idletime = 0;
Marc Yang2b06bdb2011-03-30 18:12:44 -07001300
1301 auto_ds_tlv->header.type =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001302 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001303 auto_ds_tlv->header.len =
1304 cpu_to_le16(sizeof(*auto_ds_tlv) -
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001305 sizeof(struct mwifiex_ie_types_header));
Marc Yang2b06bdb2011-03-30 18:12:44 -07001306 cmd_size += sizeof(*auto_ds_tlv);
1307 tlv += sizeof(*auto_ds_tlv);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001308 if (auto_ds)
1309 idletime = auto_ds->idle_time;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001310 dev_dbg(priv->adapter->dev,
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001311 "cmd: PS Command: Enter Auto Deep Sleep\n");
Marc Yang2b06bdb2011-03-30 18:12:44 -07001312 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001313 }
1314 cmd->size = cpu_to_le16(cmd_size);
1315 }
1316 return 0;
1317}
1318
1319/*
1320 * This function handles the command response of an enhanced power mode
1321 * command.
1322 *
1323 * Handling includes changing the header fields into CPU format
1324 * and setting the current enhanced power mode in driver.
1325 */
1326int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1327 struct host_cmd_ds_command *resp,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001328 struct mwifiex_ds_pm_cfg *pm_cfg)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001329{
1330 struct mwifiex_adapter *adapter = priv->adapter;
1331 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1332 &resp->params.psmode_enh;
1333 uint16_t action = le16_to_cpu(ps_mode->action);
1334 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1335 uint16_t auto_ps_bitmap =
Marc Yang2b06bdb2011-03-30 18:12:44 -07001336 le16_to_cpu(ps_mode->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001337
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001338 dev_dbg(adapter->dev,
1339 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1340 __func__, resp->result, action);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001341 if (action == EN_AUTO_PS) {
1342 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1343 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1344 priv->adapter->is_deep_sleep = true;
1345 }
1346 if (auto_ps_bitmap & BITMAP_STA_PS) {
1347 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1348 if (adapter->sleep_period.period)
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001349 dev_dbg(adapter->dev,
1350 "cmd: set to uapsd/pps mode\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001351 }
1352 } else if (action == DIS_AUTO_PS) {
1353 if (ps_bitmap & BITMAP_AUTO_DS) {
1354 priv->adapter->is_deep_sleep = false;
1355 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1356 }
1357 if (ps_bitmap & BITMAP_STA_PS) {
1358 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1359 if (adapter->sleep_period.period) {
1360 adapter->delay_null_pkt = false;
1361 adapter->tx_lock_flag = false;
1362 adapter->pps_uapsd_mode = false;
1363 }
1364 }
1365 } else if (action == GET_PS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001366 if (ps_bitmap & BITMAP_STA_PS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001367 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1368 else
1369 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1370
1371 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1372
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001373 if (pm_cfg) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001374 /* This section is for get power save mode */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001375 if (ps_bitmap & BITMAP_STA_PS)
1376 pm_cfg->param.ps_mode = 1;
1377 else
1378 pm_cfg->param.ps_mode = 0;
1379 }
1380 }
1381 return 0;
1382}
1383
1384/*
1385 * This function prepares command to get hardware specifications.
1386 *
1387 * Preparation includes -
1388 * - Setting command ID, action and proper size
1389 * - Setting permanent address parameter
1390 * - Ensuring correct endian-ness
1391 */
1392int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1393 struct host_cmd_ds_command *cmd)
1394{
1395 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1396
1397 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1398 cmd->size =
1399 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1400 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1401
1402 return 0;
1403}
1404
1405/*
1406 * This function handles the command response of get hardware
1407 * specifications.
1408 *
1409 * Handling includes changing the header fields into CPU format
1410 * and saving/updating the following parameters in driver -
1411 * - Firmware capability information
1412 * - Firmware band settings
1413 * - Ad-hoc start band and channel
1414 * - Ad-hoc 11n activation status
1415 * - Firmware release number
1416 * - Number of antennas
1417 * - Hardware address
1418 * - Hardware interface version
1419 * - Firmware version
1420 * - Region code
1421 * - 11n capabilities
1422 * - MCS support fields
1423 * - MP end port
1424 */
1425int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1426 struct host_cmd_ds_command *resp)
1427{
1428 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1429 struct mwifiex_adapter *adapter = priv->adapter;
1430 int i;
1431
1432 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1433
1434 if (IS_SUPPORT_MULTI_BANDS(adapter))
1435 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1436 else
1437 adapter->fw_bands = BAND_B;
1438
1439 adapter->config_bands = adapter->fw_bands;
1440
1441 if (adapter->fw_bands & BAND_A) {
1442 if (adapter->fw_bands & BAND_GN) {
1443 adapter->config_bands |= BAND_AN;
1444 adapter->fw_bands |= BAND_AN;
1445 }
1446 if (adapter->fw_bands & BAND_AN) {
1447 adapter->adhoc_start_band = BAND_A | BAND_AN;
1448 adapter->adhoc_11n_enabled = true;
1449 } else {
1450 adapter->adhoc_start_band = BAND_A;
1451 }
1452 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1453 } else if (adapter->fw_bands & BAND_GN) {
1454 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1455 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1456 adapter->adhoc_11n_enabled = true;
1457 } else if (adapter->fw_bands & BAND_G) {
1458 adapter->adhoc_start_band = BAND_G | BAND_B;
1459 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1460 } else if (adapter->fw_bands & BAND_B) {
1461 adapter->adhoc_start_band = BAND_B;
1462 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1463 }
1464
1465 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1466 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1467
Yogesh Ashok Powara5f39052013-02-15 21:44:30 -08001468 if (le32_to_cpu(hw_spec->dot_11ac_dev_cap)) {
1469 adapter->is_hw_11ac_capable = true;
1470
1471 /* Copy 11AC cap */
1472 adapter->hw_dot_11ac_dev_cap =
1473 le32_to_cpu(hw_spec->dot_11ac_dev_cap);
1474 adapter->usr_dot_11ac_dev_cap_bg = adapter->hw_dot_11ac_dev_cap;
1475 adapter->usr_dot_11ac_dev_cap_a = adapter->hw_dot_11ac_dev_cap;
1476
1477 /* Copy 11AC mcs */
1478 adapter->hw_dot_11ac_mcs_support =
1479 le32_to_cpu(hw_spec->dot_11ac_mcs_support);
1480 adapter->usr_dot_11ac_mcs_support =
1481 adapter->hw_dot_11ac_mcs_support;
1482 } else {
1483 adapter->is_hw_11ac_capable = false;
1484 }
1485
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001486 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001487 adapter->fw_release_number);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001488 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001489 hw_spec->permanent_addr);
1490 dev_dbg(adapter->dev,
1491 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001492 le16_to_cpu(hw_spec->hw_if_version),
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001493 le16_to_cpu(hw_spec->version));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001494
1495 if (priv->curr_addr[0] == 0xff)
1496 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1497
1498 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1499
1500 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1501 /* Use the region code to search for the index */
1502 if (adapter->region_code == region_code_index[i])
1503 break;
1504
1505 /* If it's unidentified region code, use the default (USA) */
1506 if (i >= MWIFIEX_MAX_REGION_CODE) {
1507 adapter->region_code = 0x10;
Yogesh Ashok Powaraea07012012-03-13 19:22:35 -07001508 dev_dbg(adapter->dev,
1509 "cmd: unknown region code, use default (USA)\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001510 }
1511
1512 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001513 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001514
1515 if (adapter->if_ops.update_mp_end_port)
1516 adapter->if_ops.update_mp_end_port(adapter,
1517 le16_to_cpu(hw_spec->mp_end_port));
1518
1519 return 0;
1520}