Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[deliverable/linux.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
3d99d987
AP
450 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
451 ret = mwifiex_process_uap_event(priv);
452 else
453 ret = mwifiex_process_sta_event(priv);
5e6e3a92
BZ
454
455 adapter->event_cause = 0;
456 adapter->event_skb = NULL;
d930faee 457 adapter->if_ops.event_complete(adapter, skb);
5e6e3a92
BZ
458
459 return ret;
460}
461
462/*
600f5d90
AK
463 * This function is used to send synchronous command to the firmware.
464 *
465 * it allocates a wait queue for the command and wait for the command
466 * response.
467 */
468int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
469 u16 cmd_action, u32 cmd_oid, void *data_buf)
470{
471 int ret = 0;
472 struct mwifiex_adapter *adapter = priv->adapter;
473
474 adapter->cmd_wait_q_required = true;
600f5d90
AK
475
476 ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
477 data_buf);
478 if (!ret)
479 ret = mwifiex_wait_queue_complete(adapter);
480
481 return ret;
482}
483
484
485/*
486 * This function prepares a command and asynchronously send it to the firmware.
5e6e3a92
BZ
487 *
488 * Preparation includes -
489 * - Sanity tests to make sure the card is still present or the FW
490 * is not reset
491 * - Getting a new command node from the command free queue
492 * - Initializing the command node for default parameters
493 * - Fill up the non-default parameters and buffer pointers
494 * - Add the command to pending queue
495 */
600f5d90
AK
496int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
497 u16 cmd_action, u32 cmd_oid, void *data_buf)
5e6e3a92 498{
270e58e8 499 int ret;
5e6e3a92 500 struct mwifiex_adapter *adapter = priv->adapter;
270e58e8
YAP
501 struct cmd_ctrl_node *cmd_node;
502 struct host_cmd_ds_command *cmd_ptr;
5e6e3a92
BZ
503
504 if (!adapter) {
505 pr_err("PREP_CMD: adapter is NULL\n");
506 return -1;
507 }
508
509 if (adapter->is_suspended) {
510 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
511 return -1;
512 }
513
514 if (adapter->surprise_removed) {
515 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
516 return -1;
517 }
518
519 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
520 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
521 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
522 return -1;
523 }
524 }
525
526 /* Get a new command node */
527 cmd_node = mwifiex_get_cmd_node(adapter);
528
529 if (!cmd_node) {
530 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
531 return -1;
532 }
533
534 /* Initialize the command node */
600f5d90 535 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
5e6e3a92
BZ
536
537 if (!cmd_node->cmd_skb) {
538 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
539 return -1;
540 }
541
542 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
543 0, sizeof(struct host_cmd_ds_command));
544
545 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
546 cmd_ptr->command = cpu_to_le16(cmd_no);
547 cmd_ptr->result = 0;
548
549 /* Prepare command */
550 if (cmd_no) {
40d07030
AP
551 switch (cmd_no) {
552 case HostCmd_CMD_UAP_SYS_CONFIG:
553 case HostCmd_CMD_UAP_BSS_START:
554 case HostCmd_CMD_UAP_BSS_STOP:
555 ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
556 cmd_oid, data_buf,
557 cmd_ptr);
558 break;
559 default:
560 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
561 cmd_oid, data_buf,
562 cmd_ptr);
563 break;
564 }
5e6e3a92
BZ
565 } else {
566 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
567 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
568 }
569
570 /* Return error, since the command preparation failed */
571 if (ret) {
572 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
aea0701e 573 cmd_no);
5e6e3a92
BZ
574 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
575 return -1;
576 }
577
578 /* Send command */
efaaa8b8 579 if (cmd_no == HostCmd_CMD_802_11_SCAN) {
5e6e3a92 580 mwifiex_queue_scan_cmd(priv, cmd_node);
efaaa8b8
AK
581 } else {
582 adapter->cmd_queued = cmd_node;
5e6e3a92 583 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
1a1fb970 584 queue_work(adapter->workqueue, &adapter->main_work);
efaaa8b8 585 }
5e6e3a92
BZ
586
587 return ret;
588}
589
590/*
591 * This function returns a command to the command free queue.
592 *
593 * The function also calls the completion callback if required, before
594 * cleaning the command node and re-inserting it into the free queue.
595 */
596void
597mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
598 struct cmd_ctrl_node *cmd_node)
599{
5e6e3a92
BZ
600 unsigned long flags;
601
19a89860 602 if (!cmd_node)
5e6e3a92 603 return;
600f5d90
AK
604
605 if (cmd_node->wait_q_enabled)
efaaa8b8 606 mwifiex_complete_cmd(adapter, cmd_node);
5e6e3a92
BZ
607 /* Clean the node */
608 mwifiex_clean_cmd_node(adapter, cmd_node);
609
610 /* Insert node into cmd_free_q */
611 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
612 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
613 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
5e6e3a92
BZ
614}
615
616/*
617 * This function queues a command to the command pending queue.
618 *
619 * This in effect adds the command to the command list to be executed.
620 * Exit PS command is handled specially, by placing it always to the
621 * front of the command queue.
622 */
623void
624mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
625 struct cmd_ctrl_node *cmd_node, u32 add_tail)
626{
627 struct host_cmd_ds_command *host_cmd = NULL;
628 u16 command;
629 unsigned long flags;
630
631 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
632 if (!host_cmd) {
633 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
634 return;
635 }
636
637 command = le16_to_cpu(host_cmd->command);
638
639 /* Exit_PS command needs to be queued in the header always. */
640 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
641 struct host_cmd_ds_802_11_ps_mode_enh *pm =
aea0701e
YAP
642 &host_cmd->params.psmode_enh;
643 if ((le16_to_cpu(pm->action) == DIS_PS) ||
644 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
5e6e3a92
BZ
645 if (adapter->ps_state != PS_STATE_AWAKE)
646 add_tail = false;
647 }
648 }
649
650 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
651 if (add_tail)
652 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
653 else
654 list_add(&cmd_node->list, &adapter->cmd_pending_q);
655 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
656
657 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
5e6e3a92
BZ
658}
659
660/*
661 * This function executes the next command in command pending queue.
662 *
663 * This function will fail if a command is already in processing stage,
664 * otherwise it will dequeue the first command from the command pending
665 * queue and send to the firmware.
666 *
667 * If the device is currently in host sleep mode, any commands, except the
668 * host sleep configuration command will de-activate the host sleep. For PS
669 * mode, the function will put the firmware back to sleep if applicable.
670 */
671int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
672{
270e58e8
YAP
673 struct mwifiex_private *priv;
674 struct cmd_ctrl_node *cmd_node;
5e6e3a92
BZ
675 int ret = 0;
676 struct host_cmd_ds_command *host_cmd;
677 unsigned long cmd_flags;
678 unsigned long cmd_pending_q_flags;
679
680 /* Check if already in processing */
681 if (adapter->curr_cmd) {
682 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
683 return -1;
684 }
685
686 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
687 /* Check if any command is pending */
688 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
689 if (list_empty(&adapter->cmd_pending_q)) {
690 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
691 cmd_pending_q_flags);
692 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
693 return 0;
694 }
695 cmd_node = list_first_entry(&adapter->cmd_pending_q,
696 struct cmd_ctrl_node, list);
697 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
698 cmd_pending_q_flags);
699
700 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
701 priv = cmd_node->priv;
702
703 if (adapter->ps_state != PS_STATE_AWAKE) {
704 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
705 " this should not happen\n", __func__);
706 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
707 return ret;
708 }
709
710 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
711 list_del(&cmd_node->list);
712 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
713 cmd_pending_q_flags);
714
715 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
716 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
717 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
718 /* Any command sent to the firmware when host is in sleep
719 * mode should de-configure host sleep. We should skip the
720 * host sleep configuration command itself though
721 */
722 if (priv && (host_cmd->command !=
723 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
724 if (adapter->hs_activated) {
725 adapter->is_hs_configured = false;
726 mwifiex_hs_activated_event(priv, false);
727 }
728 }
729
730 return ret;
731}
732
733/*
734 * This function handles the command response.
735 *
736 * After processing, the function cleans the command node and puts
737 * it back to the command free queue.
738 */
739int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
740{
270e58e8 741 struct host_cmd_ds_command *resp;
5e6e3a92
BZ
742 struct mwifiex_private *priv =
743 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
744 int ret = 0;
745 uint16_t orig_cmdresp_no;
746 uint16_t cmdresp_no;
747 uint16_t cmdresp_result;
5e6e3a92
BZ
748 struct timeval tstamp;
749 unsigned long flags;
750
751 /* Now we got response from FW, cancel the command timer */
752 del_timer(&adapter->cmd_timer);
753
754 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
755 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
756 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
aea0701e 757 le16_to_cpu(resp->command));
5e6e3a92
BZ
758 return -1;
759 }
760
5e6e3a92
BZ
761 adapter->num_cmd_timeout = 0;
762
763 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
764 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
765 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
aea0701e 766 le16_to_cpu(resp->command));
5e6e3a92
BZ
767 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
768 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
769 adapter->curr_cmd = NULL;
770 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
771 return -1;
772 }
773
774 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
775 /* Copy original response back to response buffer */
a5ffddb7 776 struct mwifiex_ds_misc_cmd *hostcmd;
5e6e3a92
BZ
777 uint16_t size = le16_to_cpu(resp->size);
778 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
779 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
780 if (adapter->curr_cmd->data_buf) {
a5ffddb7 781 hostcmd = adapter->curr_cmd->data_buf;
5e6e3a92 782 hostcmd->len = size;
a5ffddb7 783 memcpy(hostcmd->cmd, resp, size);
5e6e3a92
BZ
784 }
785 }
786 orig_cmdresp_no = le16_to_cpu(resp->command);
787
788 /* Get BSS number and corresponding priv */
789 priv = mwifiex_get_priv_by_id(adapter,
aea0701e
YAP
790 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
791 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
5e6e3a92
BZ
792 if (!priv)
793 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
794 /* Clear RET_BIT from HostCmd */
795 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
796
797 cmdresp_no = le16_to_cpu(resp->command);
798 cmdresp_result = le16_to_cpu(resp->result);
799
800 /* Save the last command response to debug log */
801 adapter->dbg.last_cmd_resp_index =
aea0701e 802 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
5e6e3a92 803 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
aea0701e 804 orig_cmdresp_no;
5e6e3a92
BZ
805
806 do_gettimeofday(&tstamp);
807 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
808 " len %d, seqno 0x%x\n",
809 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
810 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
811
812 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
813 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
600f5d90
AK
814 if (adapter->curr_cmd->wait_q_enabled)
815 adapter->cmd_wait_q.status = -1;
5e6e3a92
BZ
816
817 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
818 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
819 adapter->curr_cmd = NULL;
820 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
821 return -1;
822 }
823
824 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
825 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
aea0701e
YAP
826 if ((cmdresp_result == HostCmd_RESULT_OK) &&
827 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
5e6e3a92
BZ
828 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
829 } else {
830 /* handle response */
600f5d90 831 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
5e6e3a92
BZ
832 }
833
834 /* Check init command response */
835 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
a0f6d6ca 836 if (ret) {
5e6e3a92
BZ
837 dev_err(adapter->dev, "%s: cmd %#x failed during "
838 "initialization\n", __func__, cmdresp_no);
839 mwifiex_init_fw_complete(adapter);
840 return -1;
841 } else if (adapter->last_init_cmd == cmdresp_no)
842 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
843 }
844
845 if (adapter->curr_cmd) {
a0f6d6ca
AK
846 if (adapter->curr_cmd->wait_q_enabled)
847 adapter->cmd_wait_q.status = ret;
5e6e3a92
BZ
848
849 /* Clean up and put current command back to cmd_free_q */
850 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
851
852 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
853 adapter->curr_cmd = NULL;
854 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
855 }
856
857 return ret;
858}
859
860/*
861 * This function handles the timeout of command sending.
862 *
863 * It will re-send the same command again.
864 */
865void
866mwifiex_cmd_timeout_func(unsigned long function_context)
867{
868 struct mwifiex_adapter *adapter =
869 (struct mwifiex_adapter *) function_context;
270e58e8 870 struct cmd_ctrl_node *cmd_node;
5e6e3a92
BZ
871 struct timeval tstamp;
872
873 adapter->num_cmd_timeout++;
874 adapter->dbg.num_cmd_timeout++;
875 if (!adapter->curr_cmd) {
876 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
877 return;
878 }
879 cmd_node = adapter->curr_cmd;
600f5d90
AK
880 if (cmd_node->wait_q_enabled)
881 adapter->cmd_wait_q.status = -ETIMEDOUT;
5e6e3a92
BZ
882
883 if (cmd_node) {
884 adapter->dbg.timeout_cmd_id =
885 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
886 adapter->dbg.timeout_cmd_act =
887 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
888 do_gettimeofday(&tstamp);
aea0701e
YAP
889 dev_err(adapter->dev,
890 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
891 __func__, tstamp.tv_sec, tstamp.tv_usec,
892 adapter->dbg.timeout_cmd_id,
893 adapter->dbg.timeout_cmd_act);
5e6e3a92
BZ
894
895 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
aea0701e 896 adapter->dbg.num_tx_host_to_card_failure);
5e6e3a92 897 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
aea0701e 898 adapter->dbg.num_cmd_host_to_card_failure);
5e6e3a92
BZ
899
900 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
aea0701e 901 adapter->dbg.num_cmd_timeout);
5e6e3a92 902 dev_err(adapter->dev, "num_tx_timeout = %d\n",
aea0701e 903 adapter->dbg.num_tx_timeout);
5e6e3a92
BZ
904
905 dev_err(adapter->dev, "last_cmd_index = %d\n",
aea0701e 906 adapter->dbg.last_cmd_index);
5e6e3a92 907 print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
aea0701e 908 adapter->dbg.last_cmd_id, DBG_CMD_NUM);
5e6e3a92 909 print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
aea0701e 910 adapter->dbg.last_cmd_act, DBG_CMD_NUM);
5e6e3a92
BZ
911
912 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
aea0701e 913 adapter->dbg.last_cmd_resp_index);
5e6e3a92 914 print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
aea0701e
YAP
915 adapter->dbg.last_cmd_resp_id,
916 DBG_CMD_NUM);
5e6e3a92
BZ
917
918 dev_err(adapter->dev, "last_event_index = %d\n",
aea0701e 919 adapter->dbg.last_event_index);
5e6e3a92 920 print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
aea0701e 921 adapter->dbg.last_event, DBG_CMD_NUM);
5e6e3a92
BZ
922
923 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
aea0701e 924 adapter->data_sent, adapter->cmd_sent);
5e6e3a92
BZ
925
926 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
aea0701e 927 adapter->ps_mode, adapter->ps_state);
5e6e3a92
BZ
928 }
929 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
930 mwifiex_init_fw_complete(adapter);
5e6e3a92
BZ
931}
932
933/*
934 * This function cancels all the pending commands.
935 *
936 * The current command, all commands in command pending queue and all scan
937 * commands in scan pending queue are cancelled. All the completion callbacks
938 * are called with failure status to ensure cleanup.
939 */
940void
941mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
942{
270e58e8 943 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
5e6e3a92
BZ
944 unsigned long flags;
945
946 /* Cancel current cmd */
600f5d90 947 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
5e6e3a92 948 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
600f5d90 949 adapter->curr_cmd->wait_q_enabled = false;
5e6e3a92 950 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
600f5d90 951 adapter->cmd_wait_q.status = -1;
efaaa8b8 952 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
5e6e3a92
BZ
953 }
954 /* Cancel all pending command */
955 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
956 list_for_each_entry_safe(cmd_node, tmp_node,
957 &adapter->cmd_pending_q, list) {
958 list_del(&cmd_node->list);
959 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
960
600f5d90
AK
961 if (cmd_node->wait_q_enabled) {
962 adapter->cmd_wait_q.status = -1;
efaaa8b8 963 mwifiex_complete_cmd(adapter, cmd_node);
600f5d90 964 cmd_node->wait_q_enabled = false;
5e6e3a92
BZ
965 }
966 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
967 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
968 }
969 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
970
971 /* Cancel all pending scan command */
972 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
973 list_for_each_entry_safe(cmd_node, tmp_node,
974 &adapter->scan_pending_q, list) {
975 list_del(&cmd_node->list);
976 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
977
600f5d90 978 cmd_node->wait_q_enabled = false;
5e6e3a92
BZ
979 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
980 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
981 }
982 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
983
984 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
985 adapter->scan_processing = false;
986 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
987}
988
989/*
990 * This function cancels all pending commands that matches with
991 * the given IOCTL request.
992 *
993 * Both the current command buffer and the pending command queue are
994 * searched for matching IOCTL request. The completion callback of
995 * the matched command is called with failure status to ensure cleanup.
996 * In case of scan commands, all pending commands in scan pending queue
997 * are cancelled.
998 */
999void
600f5d90 1000mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
5e6e3a92
BZ
1001{
1002 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1003 unsigned long cmd_flags;
5e6e3a92
BZ
1004 unsigned long scan_pending_q_flags;
1005 uint16_t cancel_scan_cmd = false;
1006
1007 if ((adapter->curr_cmd) &&
aea0701e 1008 (adapter->curr_cmd->wait_q_enabled)) {
5e6e3a92
BZ
1009 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1010 cmd_node = adapter->curr_cmd;
600f5d90 1011 cmd_node->wait_q_enabled = false;
5e6e3a92 1012 cmd_node->cmd_flag |= CMD_F_CANCELED;
5e6e3a92 1013 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
51e708c1
YAP
1014 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1015 adapter->curr_cmd = NULL;
600f5d90 1016 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
5e6e3a92 1017 }
600f5d90 1018
5e6e3a92
BZ
1019 /* Cancel all pending scan command */
1020 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1021 scan_pending_q_flags);
1022 list_for_each_entry_safe(cmd_node, tmp_node,
1023 &adapter->scan_pending_q, list) {
600f5d90
AK
1024 list_del(&cmd_node->list);
1025 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1026 scan_pending_q_flags);
1027 cmd_node->wait_q_enabled = false;
1028 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1029 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1030 scan_pending_q_flags);
1031 cancel_scan_cmd = true;
5e6e3a92
BZ
1032 }
1033 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1034 scan_pending_q_flags);
1035
1036 if (cancel_scan_cmd) {
1037 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1038 adapter->scan_processing = false;
1039 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1040 }
600f5d90 1041 adapter->cmd_wait_q.status = -1;
5e6e3a92
BZ
1042}
1043
1044/*
1045 * This function sends the sleep confirm command to firmware, if
1046 * possible.
1047 *
1048 * The sleep confirm command cannot be issued if command response,
1049 * data response or event response is awaiting handling, or if we
1050 * are in the middle of sending a command, or expecting a command
1051 * response.
1052 */
1053void
1054mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1055{
1056 if (!adapter->cmd_sent &&
1057 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1058 mwifiex_dnld_sleep_confirm_cmd(adapter);
1059 else
1060 dev_dbg(adapter->dev,
1061 "cmd: Delay Sleep Confirm (%s%s%s)\n",
aea0701e
YAP
1062 (adapter->cmd_sent) ? "D" : "",
1063 (adapter->curr_cmd) ? "C" : "",
1064 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
5e6e3a92
BZ
1065}
1066
1067/*
1068 * This function sends a Host Sleep activated event to applications.
1069 *
1070 * This event is generated by the driver, with a blank event body.
1071 */
1072void
1073mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1074{
1075 if (activated) {
1076 if (priv->adapter->is_hs_configured) {
1077 priv->adapter->hs_activated = true;
1078 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1079 priv->adapter->hs_activate_wait_q_woken = true;
1080 wake_up_interruptible(
1081 &priv->adapter->hs_activate_wait_q);
1082 } else {
1083 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1084 }
1085 } else {
1086 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1087 priv->adapter->hs_activated = false;
1088 }
1089}
1090
1091/*
1092 * This function handles the command response of a Host Sleep configuration
1093 * command.
1094 *
1095 * Handling includes changing the header fields into CPU format
1096 * and setting the current host sleep activation status in driver.
1097 *
1098 * In case host sleep status change, the function generates an event to
1099 * notify the applications.
1100 */
1101int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1102 struct host_cmd_ds_command *resp)
1103{
1104 struct mwifiex_adapter *adapter = priv->adapter;
1105 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1106 &resp->params.opt_hs_cfg;
1107 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1108
a4186ea6
AK
1109 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1110 adapter->iface_type == MWIFIEX_SDIO) {
5e6e3a92
BZ
1111 mwifiex_hs_activated_event(priv, true);
1112 return 0;
1113 } else {
1114 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1115 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1116 resp->result, conditions,
aea0701e
YAP
1117 phs_cfg->params.hs_config.gpio,
1118 phs_cfg->params.hs_config.gap);
5e6e3a92
BZ
1119 }
1120 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1121 adapter->is_hs_configured = true;
a4186ea6
AK
1122 if (adapter->iface_type == MWIFIEX_USB ||
1123 adapter->iface_type == MWIFIEX_PCIE)
1124 mwifiex_hs_activated_event(priv, true);
5e6e3a92
BZ
1125 } else {
1126 adapter->is_hs_configured = false;
1127 if (adapter->hs_activated)
1128 mwifiex_hs_activated_event(priv, false);
1129 }
1130
1131 return 0;
1132}
1133
1134/*
1135 * This function wakes up the adapter and generates a Host Sleep
1136 * cancel event on receiving the power up interrupt.
1137 */
1138void
1139mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1140{
1141 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1142 " since there is interrupt from the firmware\n", __func__);
1143
1144 adapter->if_ops.wakeup(adapter);
1145 adapter->hs_activated = false;
1146 adapter->is_hs_configured = false;
1147 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
aea0701e
YAP
1148 MWIFIEX_BSS_ROLE_ANY),
1149 false);
5e6e3a92 1150}
4daffe35 1151EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
5e6e3a92
BZ
1152
1153/*
1154 * This function handles the command response of a sleep confirm command.
1155 *
1156 * The function sets the card state to SLEEP if the response indicates success.
1157 */
1158void
1159mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1160 u8 *pbuf, u32 upld_len)
1161{
1162 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1163 struct mwifiex_private *priv =
1164 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1165 uint16_t result = le16_to_cpu(cmd->result);
1166 uint16_t command = le16_to_cpu(cmd->command);
1167 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1168
1169 if (!upld_len) {
1170 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1171 return;
1172 }
1173
1174 /* Get BSS number and corresponding priv */
1175 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1176 HostCmd_GET_BSS_TYPE(seq_num));
1177 if (!priv)
1178 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1179
1180 /* Update sequence number */
1181 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1182 /* Clear RET_BIT from HostCmd */
1183 command &= HostCmd_CMD_ID_MASK;
1184
1185 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
aea0701e
YAP
1186 dev_err(adapter->dev,
1187 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1188 __func__, command, result);
5e6e3a92
BZ
1189 return;
1190 }
1191
1192 if (result) {
1193 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
aea0701e 1194 __func__);
5e6e3a92
BZ
1195 adapter->pm_wakeup_card_req = false;
1196 adapter->ps_state = PS_STATE_AWAKE;
1197 return;
1198 }
1199 adapter->pm_wakeup_card_req = true;
1200 if (adapter->is_hs_configured)
aea0701e
YAP
1201 mwifiex_hs_activated_event(mwifiex_get_priv
1202 (adapter, MWIFIEX_BSS_ROLE_ANY),
1203 true);
5e6e3a92
BZ
1204 adapter->ps_state = PS_STATE_SLEEP;
1205 cmd->command = cpu_to_le16(command);
1206 cmd->seq_num = cpu_to_le16(seq_num);
1207}
1208EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1209
1210/*
1211 * This function prepares an enhanced power mode command.
1212 *
1213 * This function can be used to disable power save or to configure
1214 * power save with auto PS or STA PS or auto deep sleep.
1215 *
1216 * Preparation includes -
1217 * - Setting command ID, action and proper size
1218 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1219 * auto deep sleep TLV (as required)
1220 * - Ensuring correct endian-ness
1221 */
1222int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1223 struct host_cmd_ds_command *cmd,
1224 u16 cmd_action, uint16_t ps_bitmap,
a5ffddb7 1225 struct mwifiex_ds_auto_ds *auto_ds)
5e6e3a92
BZ
1226{
1227 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1228 &cmd->params.psmode_enh;
270e58e8 1229 u8 *tlv;
5e6e3a92
BZ
1230 u16 cmd_size = 0;
1231
1232 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1233 if (cmd_action == DIS_AUTO_PS) {
1234 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1235 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
2b06bdbe 1236 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
aea0701e 1237 sizeof(psmode_enh->params.ps_bitmap));
5e6e3a92
BZ
1238 } else if (cmd_action == GET_PS) {
1239 psmode_enh->action = cpu_to_le16(GET_PS);
1240 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
2b06bdbe 1241 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
aea0701e 1242 sizeof(psmode_enh->params.ps_bitmap));
5e6e3a92
BZ
1243 } else if (cmd_action == EN_AUTO_PS) {
1244 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
2b06bdbe
MY
1245 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1246 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
aea0701e 1247 sizeof(psmode_enh->params.ps_bitmap);
5e6e3a92
BZ
1248 tlv = (u8 *) cmd + cmd_size;
1249 if (ps_bitmap & BITMAP_STA_PS) {
1250 struct mwifiex_adapter *adapter = priv->adapter;
1251 struct mwifiex_ie_types_ps_param *ps_tlv =
1252 (struct mwifiex_ie_types_ps_param *) tlv;
1253 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1254 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1255 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1256 sizeof(struct mwifiex_ie_types_header));
1257 cmd_size += sizeof(*ps_tlv);
1258 tlv += sizeof(*ps_tlv);
1259 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1260 ps_mode->null_pkt_interval =
aea0701e 1261 cpu_to_le16(adapter->null_pkt_interval);
5e6e3a92 1262 ps_mode->multiple_dtims =
aea0701e 1263 cpu_to_le16(adapter->multiple_dtim);
5e6e3a92 1264 ps_mode->bcn_miss_timeout =
aea0701e 1265 cpu_to_le16(adapter->bcn_miss_time_out);
5e6e3a92
BZ
1266 ps_mode->local_listen_interval =
1267 cpu_to_le16(adapter->local_listen_interval);
1268 ps_mode->adhoc_wake_period =
1269 cpu_to_le16(adapter->adhoc_awake_period);
1270 ps_mode->delay_to_ps =
aea0701e
YAP
1271 cpu_to_le16(adapter->delay_to_ps);
1272 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
5e6e3a92
BZ
1273
1274 }
1275 if (ps_bitmap & BITMAP_AUTO_DS) {
2b06bdbe 1276 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
5e6e3a92 1277 (struct mwifiex_ie_types_auto_ds_param *) tlv;
5e6e3a92 1278 u16 idletime = 0;
2b06bdbe
MY
1279
1280 auto_ds_tlv->header.type =
5e6e3a92 1281 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
2b06bdbe
MY
1282 auto_ds_tlv->header.len =
1283 cpu_to_le16(sizeof(*auto_ds_tlv) -
5e6e3a92 1284 sizeof(struct mwifiex_ie_types_header));
2b06bdbe
MY
1285 cmd_size += sizeof(*auto_ds_tlv);
1286 tlv += sizeof(*auto_ds_tlv);
a5ffddb7
AK
1287 if (auto_ds)
1288 idletime = auto_ds->idle_time;
5e6e3a92 1289 dev_dbg(priv->adapter->dev,
aea0701e 1290 "cmd: PS Command: Enter Auto Deep Sleep\n");
2b06bdbe 1291 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
5e6e3a92
BZ
1292 }
1293 cmd->size = cpu_to_le16(cmd_size);
1294 }
1295 return 0;
1296}
1297
1298/*
1299 * This function handles the command response of an enhanced power mode
1300 * command.
1301 *
1302 * Handling includes changing the header fields into CPU format
1303 * and setting the current enhanced power mode in driver.
1304 */
1305int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1306 struct host_cmd_ds_command *resp,
a5ffddb7 1307 struct mwifiex_ds_pm_cfg *pm_cfg)
5e6e3a92
BZ
1308{
1309 struct mwifiex_adapter *adapter = priv->adapter;
1310 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1311 &resp->params.psmode_enh;
1312 uint16_t action = le16_to_cpu(ps_mode->action);
1313 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1314 uint16_t auto_ps_bitmap =
2b06bdbe 1315 le16_to_cpu(ps_mode->params.ps_bitmap);
5e6e3a92 1316
aea0701e
YAP
1317 dev_dbg(adapter->dev,
1318 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1319 __func__, resp->result, action);
5e6e3a92
BZ
1320 if (action == EN_AUTO_PS) {
1321 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1322 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1323 priv->adapter->is_deep_sleep = true;
1324 }
1325 if (auto_ps_bitmap & BITMAP_STA_PS) {
1326 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1327 if (adapter->sleep_period.period)
aea0701e
YAP
1328 dev_dbg(adapter->dev,
1329 "cmd: set to uapsd/pps mode\n");
5e6e3a92
BZ
1330 }
1331 } else if (action == DIS_AUTO_PS) {
1332 if (ps_bitmap & BITMAP_AUTO_DS) {
1333 priv->adapter->is_deep_sleep = false;
1334 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1335 }
1336 if (ps_bitmap & BITMAP_STA_PS) {
1337 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1338 if (adapter->sleep_period.period) {
1339 adapter->delay_null_pkt = false;
1340 adapter->tx_lock_flag = false;
1341 adapter->pps_uapsd_mode = false;
1342 }
1343 }
1344 } else if (action == GET_PS) {
2b06bdbe 1345 if (ps_bitmap & BITMAP_STA_PS)
5e6e3a92
BZ
1346 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1347 else
1348 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1349
1350 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1351
a5ffddb7 1352 if (pm_cfg) {
5e6e3a92 1353 /* This section is for get power save mode */
5e6e3a92
BZ
1354 if (ps_bitmap & BITMAP_STA_PS)
1355 pm_cfg->param.ps_mode = 1;
1356 else
1357 pm_cfg->param.ps_mode = 0;
1358 }
1359 }
1360 return 0;
1361}
1362
1363/*
1364 * This function prepares command to get hardware specifications.
1365 *
1366 * Preparation includes -
1367 * - Setting command ID, action and proper size
1368 * - Setting permanent address parameter
1369 * - Ensuring correct endian-ness
1370 */
1371int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1372 struct host_cmd_ds_command *cmd)
1373{
1374 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1375
1376 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1377 cmd->size =
1378 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1379 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1380
1381 return 0;
1382}
1383
1384/*
1385 * This function handles the command response of get hardware
1386 * specifications.
1387 *
1388 * Handling includes changing the header fields into CPU format
1389 * and saving/updating the following parameters in driver -
1390 * - Firmware capability information
1391 * - Firmware band settings
1392 * - Ad-hoc start band and channel
1393 * - Ad-hoc 11n activation status
1394 * - Firmware release number
1395 * - Number of antennas
1396 * - Hardware address
1397 * - Hardware interface version
1398 * - Firmware version
1399 * - Region code
1400 * - 11n capabilities
1401 * - MCS support fields
1402 * - MP end port
1403 */
1404int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1405 struct host_cmd_ds_command *resp)
1406{
1407 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1408 struct mwifiex_adapter *adapter = priv->adapter;
1409 int i;
1410
1411 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1412
1413 if (IS_SUPPORT_MULTI_BANDS(adapter))
1414 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1415 else
1416 adapter->fw_bands = BAND_B;
1417
1418 adapter->config_bands = adapter->fw_bands;
1419
1420 if (adapter->fw_bands & BAND_A) {
1421 if (adapter->fw_bands & BAND_GN) {
1422 adapter->config_bands |= BAND_AN;
1423 adapter->fw_bands |= BAND_AN;
1424 }
1425 if (adapter->fw_bands & BAND_AN) {
1426 adapter->adhoc_start_band = BAND_A | BAND_AN;
1427 adapter->adhoc_11n_enabled = true;
1428 } else {
1429 adapter->adhoc_start_band = BAND_A;
1430 }
1431 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1432 } else if (adapter->fw_bands & BAND_GN) {
1433 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1434 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1435 adapter->adhoc_11n_enabled = true;
1436 } else if (adapter->fw_bands & BAND_G) {
1437 adapter->adhoc_start_band = BAND_G | BAND_B;
1438 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1439 } else if (adapter->fw_bands & BAND_B) {
1440 adapter->adhoc_start_band = BAND_B;
1441 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1442 }
1443
1444 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1445 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1446
1447 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
aea0701e 1448 adapter->fw_release_number);
5e6e3a92 1449 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
aea0701e
YAP
1450 hw_spec->permanent_addr);
1451 dev_dbg(adapter->dev,
1452 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
5e6e3a92 1453 le16_to_cpu(hw_spec->hw_if_version),
aea0701e 1454 le16_to_cpu(hw_spec->version));
5e6e3a92
BZ
1455
1456 if (priv->curr_addr[0] == 0xff)
1457 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1458
1459 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1460
1461 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1462 /* Use the region code to search for the index */
1463 if (adapter->region_code == region_code_index[i])
1464 break;
1465
1466 /* If it's unidentified region code, use the default (USA) */
1467 if (i >= MWIFIEX_MAX_REGION_CODE) {
1468 adapter->region_code = 0x10;
aea0701e
YAP
1469 dev_dbg(adapter->dev,
1470 "cmd: unknown region code, use default (USA)\n");
5e6e3a92
BZ
1471 }
1472
1473 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
5e6e3a92 1474 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
5e6e3a92
BZ
1475
1476 if (adapter->if_ops.update_mp_end_port)
1477 adapter->if_ops.update_mp_end_port(adapter,
1478 le16_to_cpu(hw_spec->mp_end_port));
1479
1480 return 0;
1481}
This page took 0.218732 seconds and 5 git commands to generate.