mwifiex: append peer mac address TLV in key material command to firmware
[linux-2.6-block.git] / drivers / net / wireless / mwifiex / cmdevt.c
CommitLineData
5e6e3a92
BZ
1/*
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"
27
28/*
29 * This function initializes a command node.
30 *
31 * The actual allocation of the node is not done by this function. It only
32 * initiates a node by filling it with default parameters. Similarly,
33 * allocation of the different buffers used (IOCTL buffer, data buffer) are
34 * not done by this function either.
35 */
36static void
37mwifiex_init_cmd_node(struct mwifiex_private *priv,
38 struct cmd_ctrl_node *cmd_node,
600f5d90 39 u32 cmd_oid, void *data_buf)
5e6e3a92
BZ
40{
41 cmd_node->priv = priv;
42 cmd_node->cmd_oid = cmd_oid;
efaaa8b8
AK
43 if (priv->adapter->cmd_wait_q_required) {
44 cmd_node->wait_q_enabled = priv->adapter->cmd_wait_q_required;
45 priv->adapter->cmd_wait_q_required = false;
46 cmd_node->cmd_wait_q_woken = false;
47 cmd_node->condition = &cmd_node->cmd_wait_q_woken;
48 }
5e6e3a92
BZ
49 cmd_node->data_buf = data_buf;
50 cmd_node->cmd_skb = cmd_node->skb;
51}
52
53/*
54 * This function returns a command node from the free queue depending upon
55 * availability.
56 */
57static struct cmd_ctrl_node *
58mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
59{
60 struct cmd_ctrl_node *cmd_node;
61 unsigned long flags;
62
63 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
64 if (list_empty(&adapter->cmd_free_q)) {
65 dev_err(adapter->dev, "GET_CMD_NODE: cmd node not available\n");
66 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
67 return NULL;
68 }
69 cmd_node = list_first_entry(&adapter->cmd_free_q,
aea0701e 70 struct cmd_ctrl_node, list);
5e6e3a92
BZ
71 list_del(&cmd_node->list);
72 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
73
74 return cmd_node;
75}
76
77/*
78 * This function cleans up a command node.
79 *
80 * The function resets the fields including the buffer pointers.
81 * This function does not try to free the buffers. They must be
82 * freed before calling this function.
83 *
84 * This function will however call the receive completion callback
85 * in case a response buffer is still available before resetting
86 * the pointer.
87 */
88static void
89mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
90 struct cmd_ctrl_node *cmd_node)
91{
92 cmd_node->cmd_oid = 0;
93 cmd_node->cmd_flag = 0;
5e6e3a92 94 cmd_node->data_buf = NULL;
600f5d90 95 cmd_node->wait_q_enabled = false;
5e6e3a92 96
5cf80993
AK
97 if (cmd_node->cmd_skb)
98 skb_trim(cmd_node->cmd_skb, 0);
99
5e6e3a92 100 if (cmd_node->resp_skb) {
d930faee 101 adapter->if_ops.cmdrsp_complete(adapter, cmd_node->resp_skb);
5e6e3a92
BZ
102 cmd_node->resp_skb = NULL;
103 }
5e6e3a92
BZ
104}
105
5e6e3a92
BZ
106/*
107 * This function sends a host command to the firmware.
108 *
109 * The function copies the host command into the driver command
110 * buffer, which will be transferred to the firmware later by the
111 * main thread.
112 */
113static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
a5ffddb7
AK
114 struct host_cmd_ds_command *cmd,
115 struct mwifiex_ds_misc_cmd *pcmd_ptr)
5e6e3a92 116{
5e6e3a92 117 /* Copy the HOST command to command buffer */
a5ffddb7 118 memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
5e6e3a92
BZ
119 dev_dbg(priv->adapter->dev, "cmd: host cmd size = %d\n", pcmd_ptr->len);
120 return 0;
121}
122
123/*
124 * This function downloads a command to the firmware.
125 *
126 * The function performs sanity tests, sets the command sequence
127 * number and size, converts the header fields to CPU format before
128 * sending. Afterwards, it logs the command ID and action for debugging
129 * and sets up the command timeout timer.
130 */
131static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
132 struct cmd_ctrl_node *cmd_node)
133{
134
135 struct mwifiex_adapter *adapter = priv->adapter;
270e58e8 136 int ret;
5e6e3a92 137 struct host_cmd_ds_command *host_cmd;
5e6e3a92
BZ
138 uint16_t cmd_code;
139 uint16_t cmd_size;
140 struct timeval tstamp;
141 unsigned long flags;
4daffe35 142 __le32 tmp;
5e6e3a92
BZ
143
144 if (!adapter || !cmd_node)
145 return -1;
146
147 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
5e6e3a92
BZ
148
149 /* Sanity test */
150 if (host_cmd == NULL || host_cmd->size == 0) {
151 dev_err(adapter->dev, "DNLD_CMD: host_cmd is null"
152 " or cmd size is 0, not sending\n");
600f5d90
AK
153 if (cmd_node->wait_q_enabled)
154 adapter->cmd_wait_q.status = -1;
5e6e3a92
BZ
155 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
156 return -1;
157 }
158
159 /* Set command sequence number */
160 adapter->seq_num++;
161 host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
aea0701e
YAP
162 (adapter->seq_num,
163 cmd_node->priv->bss_num,
164 cmd_node->priv->bss_type));
5e6e3a92
BZ
165
166 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
167 adapter->curr_cmd = cmd_node;
168 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
169
170 cmd_code = le16_to_cpu(host_cmd->command);
171 cmd_size = le16_to_cpu(host_cmd->size);
172
173 skb_trim(cmd_node->cmd_skb, cmd_size);
174
175 do_gettimeofday(&tstamp);
176 dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
177 " seqno %#x\n",
178 tstamp.tv_sec, tstamp.tv_usec, cmd_code,
aea0701e
YAP
179 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
180 le16_to_cpu(host_cmd->seq_num));
5e6e3a92 181
4daffe35
AK
182 if (adapter->iface_type == MWIFIEX_USB) {
183 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
184 skb_push(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
185 memcpy(cmd_node->cmd_skb->data, &tmp, MWIFIEX_TYPE_LEN);
186 adapter->cmd_sent = true;
187 ret = adapter->if_ops.host_to_card(adapter,
188 MWIFIEX_USB_EP_CMD_EVENT,
189 cmd_node->cmd_skb, NULL);
190 skb_pull(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
191 if (ret == -EBUSY)
192 cmd_node->cmd_skb = NULL;
193 } else {
194 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
195 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
196 cmd_node->cmd_skb, NULL);
197 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
198 }
18bf9657 199
5e6e3a92
BZ
200 if (ret == -1) {
201 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
4daffe35
AK
202 if (adapter->iface_type == MWIFIEX_USB)
203 adapter->cmd_sent = false;
600f5d90
AK
204 if (cmd_node->wait_q_enabled)
205 adapter->cmd_wait_q.status = -1;
5e6e3a92
BZ
206 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
207
208 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
209 adapter->curr_cmd = NULL;
210 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
211
212 adapter->dbg.num_cmd_host_to_card_failure++;
213 return -1;
214 }
215
216 /* Save the last command id and action to debug log */
217 adapter->dbg.last_cmd_index =
aea0701e 218 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
5e6e3a92
BZ
219 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
220 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
aea0701e 221 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
5e6e3a92
BZ
222
223 /* Clear BSS_NO_BITS from HostCmd */
224 cmd_code &= HostCmd_CMD_ID_MASK;
225
226 /* Setup the timer after transmit command */
227 mod_timer(&adapter->cmd_timer,
aea0701e 228 jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
5e6e3a92
BZ
229
230 return 0;
231}
232
233/*
234 * This function downloads a sleep confirm command to the firmware.
235 *
236 * The function performs sanity tests, sets the command sequence
237 * number and size, converts the header fields to CPU format before
238 * sending.
239 *
240 * No responses are needed for sleep confirm command.
241 */
242static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
243{
270e58e8 244 int ret;
5e6e3a92 245 struct mwifiex_private *priv;
7cc5eb62
AK
246 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
247 (struct mwifiex_opt_sleep_confirm *)
aea0701e 248 adapter->sleep_cfm->data;
4daffe35
AK
249 struct sk_buff *sleep_cfm_tmp;
250 __le32 tmp;
251
5e6e3a92
BZ
252 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
253
7cc5eb62 254 sleep_cfm_buf->seq_num =
5e6e3a92
BZ
255 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
256 (adapter->seq_num, priv->bss_num,
257 priv->bss_type)));
258 adapter->seq_num++;
259
4daffe35
AK
260 if (adapter->iface_type == MWIFIEX_USB) {
261 sleep_cfm_tmp =
262 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
263 + MWIFIEX_TYPE_LEN);
264 skb_put(sleep_cfm_tmp, sizeof(struct mwifiex_opt_sleep_confirm)
265 + MWIFIEX_TYPE_LEN);
266 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
267 memcpy(sleep_cfm_tmp->data, &tmp, MWIFIEX_TYPE_LEN);
268 memcpy(sleep_cfm_tmp->data + MWIFIEX_TYPE_LEN,
269 adapter->sleep_cfm->data,
270 sizeof(struct mwifiex_opt_sleep_confirm));
271 ret = adapter->if_ops.host_to_card(adapter,
272 MWIFIEX_USB_EP_CMD_EVENT,
273 sleep_cfm_tmp, NULL);
274 if (ret != -EBUSY)
275 dev_kfree_skb_any(sleep_cfm_tmp);
276 } else {
277 skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
278 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
279 adapter->sleep_cfm, NULL);
280 skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
281 }
5e6e3a92
BZ
282
283 if (ret == -1) {
284 dev_err(adapter->dev, "SLEEP_CFM: failed\n");
285 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
286 return -1;
287 }
288 if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
aea0701e 289 == MWIFIEX_BSS_ROLE_STA) {
7cc5eb62 290 if (!sleep_cfm_buf->resp_ctrl)
5e6e3a92
BZ
291 /* Response is not needed for sleep
292 confirm command */
293 adapter->ps_state = PS_STATE_SLEEP;
294 else
295 adapter->ps_state = PS_STATE_SLEEP_CFM;
296
aea0701e
YAP
297 if (!sleep_cfm_buf->resp_ctrl &&
298 (adapter->is_hs_configured &&
299 !adapter->sleep_period.period)) {
5e6e3a92 300 adapter->pm_wakeup_card_req = true;
aea0701e
YAP
301 mwifiex_hs_activated_event(mwifiex_get_priv
302 (adapter, MWIFIEX_BSS_ROLE_STA), true);
5e6e3a92
BZ
303 }
304 }
305
306 return ret;
307}
308
309/*
310 * This function allocates the command buffers and links them to
311 * the command free queue.
312 *
313 * The driver uses a pre allocated number of command buffers, which
314 * are created at driver initializations and freed at driver cleanup.
315 * Every command needs to obtain a command buffer from this pool before
316 * it can be issued. The command free queue lists the command buffers
317 * currently free to use, while the command pending queue lists the
318 * command buffers already in use and awaiting handling. Command buffers
319 * are returned to the free queue after use.
320 */
321int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
322{
323 struct cmd_ctrl_node *cmd_array;
324 u32 buf_size;
325 u32 i;
326
327 /* Allocate and initialize struct cmd_ctrl_node */
328 buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
329 cmd_array = kzalloc(buf_size, GFP_KERNEL);
330 if (!cmd_array) {
331 dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
aea0701e 332 __func__);
b53575ec 333 return -ENOMEM;
5e6e3a92
BZ
334 }
335
336 adapter->cmd_pool = cmd_array;
337 memset(adapter->cmd_pool, 0, buf_size);
338
339 /* Allocate and initialize command buffers */
340 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
341 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
342 if (!cmd_array[i].skb) {
343 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
344 return -1;
345 }
346 }
347
348 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
349 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
350
351 return 0;
352}
353
354/*
355 * This function frees the command buffers.
356 *
357 * The function calls the completion callback for all the command
358 * buffers that still have response buffers associated with them.
359 */
360int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
361{
362 struct cmd_ctrl_node *cmd_array;
363 u32 i;
364
365 /* Need to check if cmd pool is allocated or not */
366 if (!adapter->cmd_pool) {
367 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
368 return 0;
369 }
370
371 cmd_array = adapter->cmd_pool;
372
373 /* Release shared memory buffers */
374 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
375 if (cmd_array[i].skb) {
376 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
377 dev_kfree_skb_any(cmd_array[i].skb);
378 }
379 if (!cmd_array[i].resp_skb)
380 continue;
4daffe35
AK
381
382 if (adapter->iface_type == MWIFIEX_USB)
383 adapter->if_ops.cmdrsp_complete(adapter,
384 cmd_array[i].resp_skb);
385 else
386 dev_kfree_skb_any(cmd_array[i].resp_skb);
5e6e3a92
BZ
387 }
388 /* Release struct cmd_ctrl_node */
389 if (adapter->cmd_pool) {
390 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
391 kfree(adapter->cmd_pool);
392 adapter->cmd_pool = NULL;
393 }
394
395 return 0;
396}
397
398/*
399 * This function handles events generated by firmware.
400 *
401 * Event body of events received from firmware are not used (though they are
402 * saved), only the event ID is used. Some events are re-invoked by
403 * the driver, with a new event body.
404 *
405 * After processing, the function calls the completion callback
406 * for cleanup.
407 */
408int mwifiex_process_event(struct mwifiex_adapter *adapter)
409{
270e58e8 410 int ret;
5e6e3a92
BZ
411 struct mwifiex_private *priv =
412 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
413 struct sk_buff *skb = adapter->event_skb;
414 u32 eventcause = adapter->event_cause;
415 struct timeval tstamp;
270e58e8 416 struct mwifiex_rxinfo *rx_info;
5e6e3a92
BZ
417
418 /* Save the last event to debug log */
419 adapter->dbg.last_event_index =
aea0701e 420 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
5e6e3a92 421 adapter->dbg.last_event[adapter->dbg.last_event_index] =
aea0701e 422 (u16) eventcause;
5e6e3a92
BZ
423
424 /* Get BSS number and corresponding priv */
425 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
426 EVENT_GET_BSS_TYPE(eventcause));
427 if (!priv)
428 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
429 /* Clear BSS_NO_BITS from event */
430 eventcause &= EVENT_ID_MASK;
431 adapter->event_cause = eventcause;
432
433 if (skb) {
434 rx_info = MWIFIEX_SKB_RXCB(skb);
9da9a3b2
YAP
435 rx_info->bss_num = priv->bss_num;
436 rx_info->bss_type = priv->bss_type;
5e6e3a92
BZ
437 }
438
439 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
440 do_gettimeofday(&tstamp);
441 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
aea0701e 442 tstamp.tv_sec, tstamp.tv_usec, eventcause);
03785387
AP
443 } else {
444 /* Handle PS_SLEEP/AWAKE events on STA */
445 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
446 if (!priv)
447 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
5e6e3a92
BZ
448 }
449
450 ret = mwifiex_process_sta_event(priv);
451
452 adapter->event_cause = 0;
453 adapter->event_skb = NULL;
d930faee 454 adapter->if_ops.event_complete(adapter, skb);
5e6e3a92
BZ
455
456 return ret;
457}
458
459/*
600f5d90
AK
460 * This function is used to send synchronous command to the firmware.
461 *
462 * it allocates a wait queue for the command and wait for the command
463 * response.
464 */
465int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
466 u16 cmd_action, u32 cmd_oid, void *data_buf)
467{
468 int ret = 0;
469 struct mwifiex_adapter *adapter = priv->adapter;
470
471 adapter->cmd_wait_q_required = true;
600f5d90
AK
472
473 ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
474 data_buf);
475 if (!ret)
476 ret = mwifiex_wait_queue_complete(adapter);
477
478 return ret;
479}
480
481
482/*
483 * This function prepares a command and asynchronously send it to the firmware.
5e6e3a92
BZ
484 *
485 * Preparation includes -
486 * - Sanity tests to make sure the card is still present or the FW
487 * is not reset
488 * - Getting a new command node from the command free queue
489 * - Initializing the command node for default parameters
490 * - Fill up the non-default parameters and buffer pointers
491 * - Add the command to pending queue
492 */
600f5d90
AK
493int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
494 u16 cmd_action, u32 cmd_oid, void *data_buf)
5e6e3a92 495{
270e58e8 496 int ret;
5e6e3a92 497 struct mwifiex_adapter *adapter = priv->adapter;
270e58e8
YAP
498 struct cmd_ctrl_node *cmd_node;
499 struct host_cmd_ds_command *cmd_ptr;
5e6e3a92
BZ
500
501 if (!adapter) {
502 pr_err("PREP_CMD: adapter is NULL\n");
503 return -1;
504 }
505
506 if (adapter->is_suspended) {
507 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
508 return -1;
509 }
510
511 if (adapter->surprise_removed) {
512 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
513 return -1;
514 }
515
516 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
517 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
518 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
519 return -1;
520 }
521 }
522
523 /* Get a new command node */
524 cmd_node = mwifiex_get_cmd_node(adapter);
525
526 if (!cmd_node) {
527 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
528 return -1;
529 }
530
531 /* Initialize the command node */
600f5d90 532 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
5e6e3a92
BZ
533
534 if (!cmd_node->cmd_skb) {
535 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
536 return -1;
537 }
538
539 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
540 0, sizeof(struct host_cmd_ds_command));
541
542 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
543 cmd_ptr->command = cpu_to_le16(cmd_no);
544 cmd_ptr->result = 0;
545
546 /* Prepare command */
547 if (cmd_no) {
548 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
549 cmd_oid, data_buf, cmd_ptr);
550 } else {
551 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
552 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
553 }
554
555 /* Return error, since the command preparation failed */
556 if (ret) {
557 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
aea0701e 558 cmd_no);
5e6e3a92
BZ
559 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
560 return -1;
561 }
562
563 /* Send command */
efaaa8b8 564 if (cmd_no == HostCmd_CMD_802_11_SCAN) {
5e6e3a92 565 mwifiex_queue_scan_cmd(priv, cmd_node);
efaaa8b8
AK
566 } else {
567 adapter->cmd_queued = cmd_node;
5e6e3a92 568 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
efaaa8b8 569 }
5e6e3a92
BZ
570
571 return ret;
572}
573
574/*
575 * This function returns a command to the command free queue.
576 *
577 * The function also calls the completion callback if required, before
578 * cleaning the command node and re-inserting it into the free queue.
579 */
580void
581mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
582 struct cmd_ctrl_node *cmd_node)
583{
5e6e3a92
BZ
584 unsigned long flags;
585
19a89860 586 if (!cmd_node)
5e6e3a92 587 return;
600f5d90
AK
588
589 if (cmd_node->wait_q_enabled)
efaaa8b8 590 mwifiex_complete_cmd(adapter, cmd_node);
5e6e3a92
BZ
591 /* Clean the node */
592 mwifiex_clean_cmd_node(adapter, cmd_node);
593
594 /* Insert node into cmd_free_q */
595 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
596 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
597 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
5e6e3a92
BZ
598}
599
600/*
601 * This function queues a command to the command pending queue.
602 *
603 * This in effect adds the command to the command list to be executed.
604 * Exit PS command is handled specially, by placing it always to the
605 * front of the command queue.
606 */
607void
608mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
609 struct cmd_ctrl_node *cmd_node, u32 add_tail)
610{
611 struct host_cmd_ds_command *host_cmd = NULL;
612 u16 command;
613 unsigned long flags;
614
615 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
616 if (!host_cmd) {
617 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
618 return;
619 }
620
621 command = le16_to_cpu(host_cmd->command);
622
623 /* Exit_PS command needs to be queued in the header always. */
624 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
625 struct host_cmd_ds_802_11_ps_mode_enh *pm =
aea0701e
YAP
626 &host_cmd->params.psmode_enh;
627 if ((le16_to_cpu(pm->action) == DIS_PS) ||
628 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
5e6e3a92
BZ
629 if (adapter->ps_state != PS_STATE_AWAKE)
630 add_tail = false;
631 }
632 }
633
634 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
635 if (add_tail)
636 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
637 else
638 list_add(&cmd_node->list, &adapter->cmd_pending_q);
639 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
640
641 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
5e6e3a92
BZ
642}
643
644/*
645 * This function executes the next command in command pending queue.
646 *
647 * This function will fail if a command is already in processing stage,
648 * otherwise it will dequeue the first command from the command pending
649 * queue and send to the firmware.
650 *
651 * If the device is currently in host sleep mode, any commands, except the
652 * host sleep configuration command will de-activate the host sleep. For PS
653 * mode, the function will put the firmware back to sleep if applicable.
654 */
655int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
656{
270e58e8
YAP
657 struct mwifiex_private *priv;
658 struct cmd_ctrl_node *cmd_node;
5e6e3a92
BZ
659 int ret = 0;
660 struct host_cmd_ds_command *host_cmd;
661 unsigned long cmd_flags;
662 unsigned long cmd_pending_q_flags;
663
664 /* Check if already in processing */
665 if (adapter->curr_cmd) {
666 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
667 return -1;
668 }
669
670 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
671 /* Check if any command is pending */
672 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
673 if (list_empty(&adapter->cmd_pending_q)) {
674 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
675 cmd_pending_q_flags);
676 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
677 return 0;
678 }
679 cmd_node = list_first_entry(&adapter->cmd_pending_q,
680 struct cmd_ctrl_node, list);
681 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
682 cmd_pending_q_flags);
683
684 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
685 priv = cmd_node->priv;
686
687 if (adapter->ps_state != PS_STATE_AWAKE) {
688 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
689 " this should not happen\n", __func__);
690 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
691 return ret;
692 }
693
694 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
695 list_del(&cmd_node->list);
696 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
697 cmd_pending_q_flags);
698
699 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
700 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
701 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
702 /* Any command sent to the firmware when host is in sleep
703 * mode should de-configure host sleep. We should skip the
704 * host sleep configuration command itself though
705 */
706 if (priv && (host_cmd->command !=
707 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
708 if (adapter->hs_activated) {
709 adapter->is_hs_configured = false;
710 mwifiex_hs_activated_event(priv, false);
711 }
712 }
713
714 return ret;
715}
716
717/*
718 * This function handles the command response.
719 *
720 * After processing, the function cleans the command node and puts
721 * it back to the command free queue.
722 */
723int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
724{
270e58e8 725 struct host_cmd_ds_command *resp;
5e6e3a92
BZ
726 struct mwifiex_private *priv =
727 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
728 int ret = 0;
729 uint16_t orig_cmdresp_no;
730 uint16_t cmdresp_no;
731 uint16_t cmdresp_result;
5e6e3a92
BZ
732 struct timeval tstamp;
733 unsigned long flags;
734
735 /* Now we got response from FW, cancel the command timer */
736 del_timer(&adapter->cmd_timer);
737
738 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
739 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
740 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
aea0701e 741 le16_to_cpu(resp->command));
5e6e3a92
BZ
742 return -1;
743 }
744
5e6e3a92
BZ
745 adapter->num_cmd_timeout = 0;
746
747 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
748 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
749 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
aea0701e 750 le16_to_cpu(resp->command));
5e6e3a92
BZ
751 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
752 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
753 adapter->curr_cmd = NULL;
754 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
755 return -1;
756 }
757
758 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
759 /* Copy original response back to response buffer */
a5ffddb7 760 struct mwifiex_ds_misc_cmd *hostcmd;
5e6e3a92
BZ
761 uint16_t size = le16_to_cpu(resp->size);
762 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
763 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
764 if (adapter->curr_cmd->data_buf) {
a5ffddb7 765 hostcmd = adapter->curr_cmd->data_buf;
5e6e3a92 766 hostcmd->len = size;
a5ffddb7 767 memcpy(hostcmd->cmd, resp, size);
5e6e3a92
BZ
768 }
769 }
770 orig_cmdresp_no = le16_to_cpu(resp->command);
771
772 /* Get BSS number and corresponding priv */
773 priv = mwifiex_get_priv_by_id(adapter,
aea0701e
YAP
774 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
775 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
5e6e3a92
BZ
776 if (!priv)
777 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
778 /* Clear RET_BIT from HostCmd */
779 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
780
781 cmdresp_no = le16_to_cpu(resp->command);
782 cmdresp_result = le16_to_cpu(resp->result);
783
784 /* Save the last command response to debug log */
785 adapter->dbg.last_cmd_resp_index =
aea0701e 786 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
5e6e3a92 787 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
aea0701e 788 orig_cmdresp_no;
5e6e3a92
BZ
789
790 do_gettimeofday(&tstamp);
791 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
792 " len %d, seqno 0x%x\n",
793 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
794 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
795
796 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
797 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
600f5d90
AK
798 if (adapter->curr_cmd->wait_q_enabled)
799 adapter->cmd_wait_q.status = -1;
5e6e3a92
BZ
800
801 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
802 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
803 adapter->curr_cmd = NULL;
804 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
805 return -1;
806 }
807
808 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
809 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
aea0701e
YAP
810 if ((cmdresp_result == HostCmd_RESULT_OK) &&
811 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
5e6e3a92
BZ
812 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
813 } else {
814 /* handle response */
600f5d90 815 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
5e6e3a92
BZ
816 }
817
818 /* Check init command response */
819 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
a0f6d6ca 820 if (ret) {
5e6e3a92
BZ
821 dev_err(adapter->dev, "%s: cmd %#x failed during "
822 "initialization\n", __func__, cmdresp_no);
823 mwifiex_init_fw_complete(adapter);
824 return -1;
825 } else if (adapter->last_init_cmd == cmdresp_no)
826 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
827 }
828
829 if (adapter->curr_cmd) {
a0f6d6ca
AK
830 if (adapter->curr_cmd->wait_q_enabled)
831 adapter->cmd_wait_q.status = ret;
5e6e3a92
BZ
832
833 /* Clean up and put current command back to cmd_free_q */
834 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
835
836 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
837 adapter->curr_cmd = NULL;
838 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
839 }
840
841 return ret;
842}
843
844/*
845 * This function handles the timeout of command sending.
846 *
847 * It will re-send the same command again.
848 */
849void
850mwifiex_cmd_timeout_func(unsigned long function_context)
851{
852 struct mwifiex_adapter *adapter =
853 (struct mwifiex_adapter *) function_context;
270e58e8 854 struct cmd_ctrl_node *cmd_node;
5e6e3a92
BZ
855 struct timeval tstamp;
856
857 adapter->num_cmd_timeout++;
858 adapter->dbg.num_cmd_timeout++;
859 if (!adapter->curr_cmd) {
860 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
861 return;
862 }
863 cmd_node = adapter->curr_cmd;
600f5d90
AK
864 if (cmd_node->wait_q_enabled)
865 adapter->cmd_wait_q.status = -ETIMEDOUT;
5e6e3a92
BZ
866
867 if (cmd_node) {
868 adapter->dbg.timeout_cmd_id =
869 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
870 adapter->dbg.timeout_cmd_act =
871 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
872 do_gettimeofday(&tstamp);
aea0701e
YAP
873 dev_err(adapter->dev,
874 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
875 __func__, tstamp.tv_sec, tstamp.tv_usec,
876 adapter->dbg.timeout_cmd_id,
877 adapter->dbg.timeout_cmd_act);
5e6e3a92
BZ
878
879 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
aea0701e 880 adapter->dbg.num_tx_host_to_card_failure);
5e6e3a92 881 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
aea0701e 882 adapter->dbg.num_cmd_host_to_card_failure);
5e6e3a92
BZ
883
884 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
aea0701e 885 adapter->dbg.num_cmd_timeout);
5e6e3a92 886 dev_err(adapter->dev, "num_tx_timeout = %d\n",
aea0701e 887 adapter->dbg.num_tx_timeout);
5e6e3a92
BZ
888
889 dev_err(adapter->dev, "last_cmd_index = %d\n",
aea0701e 890 adapter->dbg.last_cmd_index);
5e6e3a92 891 print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
aea0701e 892 adapter->dbg.last_cmd_id, DBG_CMD_NUM);
5e6e3a92 893 print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
aea0701e 894 adapter->dbg.last_cmd_act, DBG_CMD_NUM);
5e6e3a92
BZ
895
896 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
aea0701e 897 adapter->dbg.last_cmd_resp_index);
5e6e3a92 898 print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
aea0701e
YAP
899 adapter->dbg.last_cmd_resp_id,
900 DBG_CMD_NUM);
5e6e3a92
BZ
901
902 dev_err(adapter->dev, "last_event_index = %d\n",
aea0701e 903 adapter->dbg.last_event_index);
5e6e3a92 904 print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
aea0701e 905 adapter->dbg.last_event, DBG_CMD_NUM);
5e6e3a92
BZ
906
907 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
aea0701e 908 adapter->data_sent, adapter->cmd_sent);
5e6e3a92
BZ
909
910 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
aea0701e 911 adapter->ps_mode, adapter->ps_state);
5e6e3a92
BZ
912 }
913 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
914 mwifiex_init_fw_complete(adapter);
5e6e3a92
BZ
915}
916
917/*
918 * This function cancels all the pending commands.
919 *
920 * The current command, all commands in command pending queue and all scan
921 * commands in scan pending queue are cancelled. All the completion callbacks
922 * are called with failure status to ensure cleanup.
923 */
924void
925mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
926{
270e58e8 927 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
5e6e3a92
BZ
928 unsigned long flags;
929
930 /* Cancel current cmd */
600f5d90 931 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
5e6e3a92 932 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
600f5d90 933 adapter->curr_cmd->wait_q_enabled = false;
5e6e3a92 934 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
600f5d90 935 adapter->cmd_wait_q.status = -1;
efaaa8b8 936 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
5e6e3a92
BZ
937 }
938 /* Cancel all pending command */
939 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
940 list_for_each_entry_safe(cmd_node, tmp_node,
941 &adapter->cmd_pending_q, list) {
942 list_del(&cmd_node->list);
943 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
944
600f5d90
AK
945 if (cmd_node->wait_q_enabled) {
946 adapter->cmd_wait_q.status = -1;
efaaa8b8 947 mwifiex_complete_cmd(adapter, cmd_node);
600f5d90 948 cmd_node->wait_q_enabled = false;
5e6e3a92
BZ
949 }
950 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
951 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
952 }
953 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
954
955 /* Cancel all pending scan command */
956 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
957 list_for_each_entry_safe(cmd_node, tmp_node,
958 &adapter->scan_pending_q, list) {
959 list_del(&cmd_node->list);
960 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
961
600f5d90 962 cmd_node->wait_q_enabled = false;
5e6e3a92
BZ
963 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
964 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
965 }
966 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
967
968 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
969 adapter->scan_processing = false;
970 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
971}
972
973/*
974 * This function cancels all pending commands that matches with
975 * the given IOCTL request.
976 *
977 * Both the current command buffer and the pending command queue are
978 * searched for matching IOCTL request. The completion callback of
979 * the matched command is called with failure status to ensure cleanup.
980 * In case of scan commands, all pending commands in scan pending queue
981 * are cancelled.
982 */
983void
600f5d90 984mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
5e6e3a92
BZ
985{
986 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
987 unsigned long cmd_flags;
5e6e3a92
BZ
988 unsigned long scan_pending_q_flags;
989 uint16_t cancel_scan_cmd = false;
990
991 if ((adapter->curr_cmd) &&
aea0701e 992 (adapter->curr_cmd->wait_q_enabled)) {
5e6e3a92
BZ
993 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
994 cmd_node = adapter->curr_cmd;
600f5d90 995 cmd_node->wait_q_enabled = false;
5e6e3a92 996 cmd_node->cmd_flag |= CMD_F_CANCELED;
5e6e3a92 997 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
51e708c1
YAP
998 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
999 adapter->curr_cmd = NULL;
600f5d90 1000 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
5e6e3a92 1001 }
600f5d90 1002
5e6e3a92
BZ
1003 /* Cancel all pending scan command */
1004 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1005 scan_pending_q_flags);
1006 list_for_each_entry_safe(cmd_node, tmp_node,
1007 &adapter->scan_pending_q, list) {
600f5d90
AK
1008 list_del(&cmd_node->list);
1009 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1010 scan_pending_q_flags);
1011 cmd_node->wait_q_enabled = false;
1012 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1013 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1014 scan_pending_q_flags);
1015 cancel_scan_cmd = true;
5e6e3a92
BZ
1016 }
1017 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1018 scan_pending_q_flags);
1019
1020 if (cancel_scan_cmd) {
1021 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1022 adapter->scan_processing = false;
1023 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1024 }
600f5d90 1025 adapter->cmd_wait_q.status = -1;
5e6e3a92
BZ
1026}
1027
1028/*
1029 * This function sends the sleep confirm command to firmware, if
1030 * possible.
1031 *
1032 * The sleep confirm command cannot be issued if command response,
1033 * data response or event response is awaiting handling, or if we
1034 * are in the middle of sending a command, or expecting a command
1035 * response.
1036 */
1037void
1038mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1039{
1040 if (!adapter->cmd_sent &&
1041 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1042 mwifiex_dnld_sleep_confirm_cmd(adapter);
1043 else
1044 dev_dbg(adapter->dev,
1045 "cmd: Delay Sleep Confirm (%s%s%s)\n",
aea0701e
YAP
1046 (adapter->cmd_sent) ? "D" : "",
1047 (adapter->curr_cmd) ? "C" : "",
1048 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
5e6e3a92
BZ
1049}
1050
1051/*
1052 * This function sends a Host Sleep activated event to applications.
1053 *
1054 * This event is generated by the driver, with a blank event body.
1055 */
1056void
1057mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1058{
1059 if (activated) {
1060 if (priv->adapter->is_hs_configured) {
1061 priv->adapter->hs_activated = true;
1062 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1063 priv->adapter->hs_activate_wait_q_woken = true;
1064 wake_up_interruptible(
1065 &priv->adapter->hs_activate_wait_q);
1066 } else {
1067 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1068 }
1069 } else {
1070 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1071 priv->adapter->hs_activated = false;
1072 }
1073}
1074
1075/*
1076 * This function handles the command response of a Host Sleep configuration
1077 * command.
1078 *
1079 * Handling includes changing the header fields into CPU format
1080 * and setting the current host sleep activation status in driver.
1081 *
1082 * In case host sleep status change, the function generates an event to
1083 * notify the applications.
1084 */
1085int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1086 struct host_cmd_ds_command *resp)
1087{
1088 struct mwifiex_adapter *adapter = priv->adapter;
1089 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1090 &resp->params.opt_hs_cfg;
1091 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1092
1093 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE)) {
1094 mwifiex_hs_activated_event(priv, true);
1095 return 0;
1096 } else {
1097 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1098 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1099 resp->result, conditions,
aea0701e
YAP
1100 phs_cfg->params.hs_config.gpio,
1101 phs_cfg->params.hs_config.gap);
5e6e3a92
BZ
1102 }
1103 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1104 adapter->is_hs_configured = true;
1105 } else {
1106 adapter->is_hs_configured = false;
1107 if (adapter->hs_activated)
1108 mwifiex_hs_activated_event(priv, false);
1109 }
1110
1111 return 0;
1112}
1113
1114/*
1115 * This function wakes up the adapter and generates a Host Sleep
1116 * cancel event on receiving the power up interrupt.
1117 */
1118void
1119mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1120{
1121 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1122 " since there is interrupt from the firmware\n", __func__);
1123
1124 adapter->if_ops.wakeup(adapter);
1125 adapter->hs_activated = false;
1126 adapter->is_hs_configured = false;
1127 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
aea0701e
YAP
1128 MWIFIEX_BSS_ROLE_ANY),
1129 false);
5e6e3a92 1130}
4daffe35 1131EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
5e6e3a92
BZ
1132
1133/*
1134 * This function handles the command response of a sleep confirm command.
1135 *
1136 * The function sets the card state to SLEEP if the response indicates success.
1137 */
1138void
1139mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1140 u8 *pbuf, u32 upld_len)
1141{
1142 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1143 struct mwifiex_private *priv =
1144 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1145 uint16_t result = le16_to_cpu(cmd->result);
1146 uint16_t command = le16_to_cpu(cmd->command);
1147 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1148
1149 if (!upld_len) {
1150 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1151 return;
1152 }
1153
1154 /* Get BSS number and corresponding priv */
1155 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1156 HostCmd_GET_BSS_TYPE(seq_num));
1157 if (!priv)
1158 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1159
1160 /* Update sequence number */
1161 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1162 /* Clear RET_BIT from HostCmd */
1163 command &= HostCmd_CMD_ID_MASK;
1164
1165 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
aea0701e
YAP
1166 dev_err(adapter->dev,
1167 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1168 __func__, command, result);
5e6e3a92
BZ
1169 return;
1170 }
1171
1172 if (result) {
1173 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
aea0701e 1174 __func__);
5e6e3a92
BZ
1175 adapter->pm_wakeup_card_req = false;
1176 adapter->ps_state = PS_STATE_AWAKE;
1177 return;
1178 }
1179 adapter->pm_wakeup_card_req = true;
1180 if (adapter->is_hs_configured)
aea0701e
YAP
1181 mwifiex_hs_activated_event(mwifiex_get_priv
1182 (adapter, MWIFIEX_BSS_ROLE_ANY),
1183 true);
5e6e3a92
BZ
1184 adapter->ps_state = PS_STATE_SLEEP;
1185 cmd->command = cpu_to_le16(command);
1186 cmd->seq_num = cpu_to_le16(seq_num);
1187}
1188EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1189
1190/*
1191 * This function prepares an enhanced power mode command.
1192 *
1193 * This function can be used to disable power save or to configure
1194 * power save with auto PS or STA PS or auto deep sleep.
1195 *
1196 * Preparation includes -
1197 * - Setting command ID, action and proper size
1198 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1199 * auto deep sleep TLV (as required)
1200 * - Ensuring correct endian-ness
1201 */
1202int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1203 struct host_cmd_ds_command *cmd,
1204 u16 cmd_action, uint16_t ps_bitmap,
a5ffddb7 1205 struct mwifiex_ds_auto_ds *auto_ds)
5e6e3a92
BZ
1206{
1207 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1208 &cmd->params.psmode_enh;
270e58e8 1209 u8 *tlv;
5e6e3a92
BZ
1210 u16 cmd_size = 0;
1211
1212 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1213 if (cmd_action == DIS_AUTO_PS) {
1214 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1215 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
2b06bdbe 1216 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
aea0701e 1217 sizeof(psmode_enh->params.ps_bitmap));
5e6e3a92
BZ
1218 } else if (cmd_action == GET_PS) {
1219 psmode_enh->action = cpu_to_le16(GET_PS);
1220 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
2b06bdbe 1221 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
aea0701e 1222 sizeof(psmode_enh->params.ps_bitmap));
5e6e3a92
BZ
1223 } else if (cmd_action == EN_AUTO_PS) {
1224 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
2b06bdbe
MY
1225 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1226 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
aea0701e 1227 sizeof(psmode_enh->params.ps_bitmap);
5e6e3a92
BZ
1228 tlv = (u8 *) cmd + cmd_size;
1229 if (ps_bitmap & BITMAP_STA_PS) {
1230 struct mwifiex_adapter *adapter = priv->adapter;
1231 struct mwifiex_ie_types_ps_param *ps_tlv =
1232 (struct mwifiex_ie_types_ps_param *) tlv;
1233 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1234 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1235 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1236 sizeof(struct mwifiex_ie_types_header));
1237 cmd_size += sizeof(*ps_tlv);
1238 tlv += sizeof(*ps_tlv);
1239 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1240 ps_mode->null_pkt_interval =
aea0701e 1241 cpu_to_le16(adapter->null_pkt_interval);
5e6e3a92 1242 ps_mode->multiple_dtims =
aea0701e 1243 cpu_to_le16(adapter->multiple_dtim);
5e6e3a92 1244 ps_mode->bcn_miss_timeout =
aea0701e 1245 cpu_to_le16(adapter->bcn_miss_time_out);
5e6e3a92
BZ
1246 ps_mode->local_listen_interval =
1247 cpu_to_le16(adapter->local_listen_interval);
1248 ps_mode->adhoc_wake_period =
1249 cpu_to_le16(adapter->adhoc_awake_period);
1250 ps_mode->delay_to_ps =
aea0701e
YAP
1251 cpu_to_le16(adapter->delay_to_ps);
1252 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
5e6e3a92
BZ
1253
1254 }
1255 if (ps_bitmap & BITMAP_AUTO_DS) {
2b06bdbe 1256 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
5e6e3a92 1257 (struct mwifiex_ie_types_auto_ds_param *) tlv;
5e6e3a92 1258 u16 idletime = 0;
2b06bdbe
MY
1259
1260 auto_ds_tlv->header.type =
5e6e3a92 1261 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
2b06bdbe
MY
1262 auto_ds_tlv->header.len =
1263 cpu_to_le16(sizeof(*auto_ds_tlv) -
5e6e3a92 1264 sizeof(struct mwifiex_ie_types_header));
2b06bdbe
MY
1265 cmd_size += sizeof(*auto_ds_tlv);
1266 tlv += sizeof(*auto_ds_tlv);
a5ffddb7
AK
1267 if (auto_ds)
1268 idletime = auto_ds->idle_time;
5e6e3a92 1269 dev_dbg(priv->adapter->dev,
aea0701e 1270 "cmd: PS Command: Enter Auto Deep Sleep\n");
2b06bdbe 1271 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
5e6e3a92
BZ
1272 }
1273 cmd->size = cpu_to_le16(cmd_size);
1274 }
1275 return 0;
1276}
1277
1278/*
1279 * This function handles the command response of an enhanced power mode
1280 * command.
1281 *
1282 * Handling includes changing the header fields into CPU format
1283 * and setting the current enhanced power mode in driver.
1284 */
1285int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1286 struct host_cmd_ds_command *resp,
a5ffddb7 1287 struct mwifiex_ds_pm_cfg *pm_cfg)
5e6e3a92
BZ
1288{
1289 struct mwifiex_adapter *adapter = priv->adapter;
1290 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1291 &resp->params.psmode_enh;
1292 uint16_t action = le16_to_cpu(ps_mode->action);
1293 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1294 uint16_t auto_ps_bitmap =
2b06bdbe 1295 le16_to_cpu(ps_mode->params.ps_bitmap);
5e6e3a92 1296
aea0701e
YAP
1297 dev_dbg(adapter->dev,
1298 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1299 __func__, resp->result, action);
5e6e3a92
BZ
1300 if (action == EN_AUTO_PS) {
1301 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1302 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1303 priv->adapter->is_deep_sleep = true;
1304 }
1305 if (auto_ps_bitmap & BITMAP_STA_PS) {
1306 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1307 if (adapter->sleep_period.period)
aea0701e
YAP
1308 dev_dbg(adapter->dev,
1309 "cmd: set to uapsd/pps mode\n");
5e6e3a92
BZ
1310 }
1311 } else if (action == DIS_AUTO_PS) {
1312 if (ps_bitmap & BITMAP_AUTO_DS) {
1313 priv->adapter->is_deep_sleep = false;
1314 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1315 }
1316 if (ps_bitmap & BITMAP_STA_PS) {
1317 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1318 if (adapter->sleep_period.period) {
1319 adapter->delay_null_pkt = false;
1320 adapter->tx_lock_flag = false;
1321 adapter->pps_uapsd_mode = false;
1322 }
1323 }
1324 } else if (action == GET_PS) {
2b06bdbe 1325 if (ps_bitmap & BITMAP_STA_PS)
5e6e3a92
BZ
1326 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1327 else
1328 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1329
1330 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1331
a5ffddb7 1332 if (pm_cfg) {
5e6e3a92 1333 /* This section is for get power save mode */
5e6e3a92
BZ
1334 if (ps_bitmap & BITMAP_STA_PS)
1335 pm_cfg->param.ps_mode = 1;
1336 else
1337 pm_cfg->param.ps_mode = 0;
1338 }
1339 }
1340 return 0;
1341}
1342
1343/*
1344 * This function prepares command to get hardware specifications.
1345 *
1346 * Preparation includes -
1347 * - Setting command ID, action and proper size
1348 * - Setting permanent address parameter
1349 * - Ensuring correct endian-ness
1350 */
1351int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1352 struct host_cmd_ds_command *cmd)
1353{
1354 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1355
1356 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1357 cmd->size =
1358 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1359 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1360
1361 return 0;
1362}
1363
1364/*
1365 * This function handles the command response of get hardware
1366 * specifications.
1367 *
1368 * Handling includes changing the header fields into CPU format
1369 * and saving/updating the following parameters in driver -
1370 * - Firmware capability information
1371 * - Firmware band settings
1372 * - Ad-hoc start band and channel
1373 * - Ad-hoc 11n activation status
1374 * - Firmware release number
1375 * - Number of antennas
1376 * - Hardware address
1377 * - Hardware interface version
1378 * - Firmware version
1379 * - Region code
1380 * - 11n capabilities
1381 * - MCS support fields
1382 * - MP end port
1383 */
1384int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1385 struct host_cmd_ds_command *resp)
1386{
1387 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1388 struct mwifiex_adapter *adapter = priv->adapter;
1389 int i;
1390
1391 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1392
1393 if (IS_SUPPORT_MULTI_BANDS(adapter))
1394 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1395 else
1396 adapter->fw_bands = BAND_B;
1397
1398 adapter->config_bands = adapter->fw_bands;
1399
1400 if (adapter->fw_bands & BAND_A) {
1401 if (adapter->fw_bands & BAND_GN) {
1402 adapter->config_bands |= BAND_AN;
1403 adapter->fw_bands |= BAND_AN;
1404 }
1405 if (adapter->fw_bands & BAND_AN) {
1406 adapter->adhoc_start_band = BAND_A | BAND_AN;
1407 adapter->adhoc_11n_enabled = true;
1408 } else {
1409 adapter->adhoc_start_band = BAND_A;
1410 }
1411 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1412 } else if (adapter->fw_bands & BAND_GN) {
1413 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1414 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1415 adapter->adhoc_11n_enabled = true;
1416 } else if (adapter->fw_bands & BAND_G) {
1417 adapter->adhoc_start_band = BAND_G | BAND_B;
1418 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1419 } else if (adapter->fw_bands & BAND_B) {
1420 adapter->adhoc_start_band = BAND_B;
1421 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1422 }
1423
1424 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1425 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1426
1427 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
aea0701e 1428 adapter->fw_release_number);
5e6e3a92 1429 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
aea0701e
YAP
1430 hw_spec->permanent_addr);
1431 dev_dbg(adapter->dev,
1432 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
5e6e3a92 1433 le16_to_cpu(hw_spec->hw_if_version),
aea0701e 1434 le16_to_cpu(hw_spec->version));
5e6e3a92
BZ
1435
1436 if (priv->curr_addr[0] == 0xff)
1437 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1438
1439 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1440
1441 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1442 /* Use the region code to search for the index */
1443 if (adapter->region_code == region_code_index[i])
1444 break;
1445
1446 /* If it's unidentified region code, use the default (USA) */
1447 if (i >= MWIFIEX_MAX_REGION_CODE) {
1448 adapter->region_code = 0x10;
aea0701e
YAP
1449 dev_dbg(adapter->dev,
1450 "cmd: unknown region code, use default (USA)\n");
5e6e3a92
BZ
1451 }
1452
1453 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
5e6e3a92 1454 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
5e6e3a92
BZ
1455
1456 if (adapter->if_ops.update_mp_end_port)
1457 adapter->if_ops.update_mp_end_port(adapter,
1458 le16_to_cpu(hw_spec->mp_end_port));
1459
1460 return 0;
1461}