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