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