blob: bb6fecd7761902fd9c95bb24559391f99b747e0b [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: commands and events
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include "decl.h"
21#include "ioctl.h"
22#include "util.h"
23#include "fw.h"
24#include "main.h"
25#include "wmm.h"
26#include "11n.h"
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,
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070039 u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070040{
41 cmd_node->priv = priv;
42 cmd_node->cmd_oid = cmd_oid;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070043 cmd_node->wait_q_enabled = priv->adapter->cmd_wait_q_required;
44 priv->adapter->cmd_wait_q_required = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070045 cmd_node->data_buf = data_buf;
46 cmd_node->cmd_skb = cmd_node->skb;
47}
48
49/*
50 * This function returns a command node from the free queue depending upon
51 * availability.
52 */
53static struct cmd_ctrl_node *
54mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
55{
56 struct cmd_ctrl_node *cmd_node;
57 unsigned long flags;
58
59 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
60 if (list_empty(&adapter->cmd_free_q)) {
61 dev_err(adapter->dev, "GET_CMD_NODE: cmd node not available\n");
62 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
63 return NULL;
64 }
65 cmd_node = list_first_entry(&adapter->cmd_free_q,
66 struct cmd_ctrl_node, list);
67 list_del(&cmd_node->list);
68 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
69
70 return cmd_node;
71}
72
73/*
74 * This function cleans up a command node.
75 *
76 * The function resets the fields including the buffer pointers.
77 * This function does not try to free the buffers. They must be
78 * freed before calling this function.
79 *
80 * This function will however call the receive completion callback
81 * in case a response buffer is still available before resetting
82 * the pointer.
83 */
84static void
85mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
86 struct cmd_ctrl_node *cmd_node)
87{
88 cmd_node->cmd_oid = 0;
89 cmd_node->cmd_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070090 cmd_node->data_buf = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070091 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070092
93 if (cmd_node->resp_skb) {
94 mwifiex_recv_complete(adapter, cmd_node->resp_skb, 0);
95 cmd_node->resp_skb = NULL;
96 }
97
98 return;
99}
100
101/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700102 * This function sends a host command to the firmware.
103 *
104 * The function copies the host command into the driver command
105 * buffer, which will be transferred to the firmware later by the
106 * main thread.
107 */
108static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
109 struct host_cmd_ds_command *cmd, void *data_buf)
110{
111 struct mwifiex_ds_misc_cmd *pcmd_ptr =
112 (struct mwifiex_ds_misc_cmd *) data_buf;
113
114 /* Copy the HOST command to command buffer */
115 memcpy((void *) cmd, pcmd_ptr->cmd, pcmd_ptr->len);
116 dev_dbg(priv->adapter->dev, "cmd: host cmd size = %d\n", pcmd_ptr->len);
117 return 0;
118}
119
120/*
121 * This function downloads a command to the firmware.
122 *
123 * The function performs sanity tests, sets the command sequence
124 * number and size, converts the header fields to CPU format before
125 * sending. Afterwards, it logs the command ID and action for debugging
126 * and sets up the command timeout timer.
127 */
128static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
129 struct cmd_ctrl_node *cmd_node)
130{
131
132 struct mwifiex_adapter *adapter = priv->adapter;
133 int ret = 0;
134 struct host_cmd_ds_command *host_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700135 uint16_t cmd_code;
136 uint16_t cmd_size;
137 struct timeval tstamp;
138 unsigned long flags;
139
140 if (!adapter || !cmd_node)
141 return -1;
142
143 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700144
145 /* Sanity test */
146 if (host_cmd == NULL || host_cmd->size == 0) {
147 dev_err(adapter->dev, "DNLD_CMD: host_cmd is null"
148 " or cmd size is 0, not sending\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700149 if (cmd_node->wait_q_enabled)
150 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700151 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
152 return -1;
153 }
154
155 /* Set command sequence number */
156 adapter->seq_num++;
157 host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
158 (adapter->seq_num, cmd_node->priv->bss_num,
159 cmd_node->priv->bss_type));
160
161 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
162 adapter->curr_cmd = cmd_node;
163 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
164
165 cmd_code = le16_to_cpu(host_cmd->command);
166 cmd_size = le16_to_cpu(host_cmd->size);
167
168 skb_trim(cmd_node->cmd_skb, cmd_size);
169
170 do_gettimeofday(&tstamp);
171 dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
172 " seqno %#x\n",
173 tstamp.tv_sec, tstamp.tv_usec, cmd_code,
174 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
175 le16_to_cpu(host_cmd->seq_num));
176
177 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
178
179 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
180 cmd_node->cmd_skb->data,
181 cmd_node->cmd_skb->len, NULL);
182
Bing Zhao18bf9652011-04-06 16:46:55 -0700183 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
184
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700185 if (ret == -1) {
186 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700187 if (cmd_node->wait_q_enabled)
188 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700189 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
190
191 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
192 adapter->curr_cmd = NULL;
193 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
194
195 adapter->dbg.num_cmd_host_to_card_failure++;
196 return -1;
197 }
198
199 /* Save the last command id and action to debug log */
200 adapter->dbg.last_cmd_index =
201 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
202 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
203 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
204 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
205
206 /* Clear BSS_NO_BITS from HostCmd */
207 cmd_code &= HostCmd_CMD_ID_MASK;
208
209 /* Setup the timer after transmit command */
210 mod_timer(&adapter->cmd_timer,
211 jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
212
213 return 0;
214}
215
216/*
217 * This function downloads a sleep confirm command to the firmware.
218 *
219 * The function performs sanity tests, sets the command sequence
220 * number and size, converts the header fields to CPU format before
221 * sending.
222 *
223 * No responses are needed for sleep confirm command.
224 */
225static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
226{
227 int ret = 0;
228 u16 cmd_len = 0;
229 struct mwifiex_private *priv;
230 struct mwifiex_opt_sleep_confirm_buffer *sleep_cfm_buf =
231 (struct mwifiex_opt_sleep_confirm_buffer *)
232 adapter->sleep_cfm->data;
233 cmd_len = sizeof(struct mwifiex_opt_sleep_confirm);
234 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
235
236 sleep_cfm_buf->ps_cfm_sleep.seq_num =
237 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
238 (adapter->seq_num, priv->bss_num,
239 priv->bss_type)));
240 adapter->seq_num++;
241
242 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
243 adapter->sleep_cfm->data,
244 adapter->sleep_cfm->len +
245 INTF_HEADER_LEN, NULL);
246
247 if (ret == -1) {
248 dev_err(adapter->dev, "SLEEP_CFM: failed\n");
249 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
250 return -1;
251 }
252 if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
253 == MWIFIEX_BSS_ROLE_STA) {
Marc Yang2b06bdb2011-03-30 18:12:44 -0700254 if (!sleep_cfm_buf->ps_cfm_sleep.resp_ctrl)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700255 /* Response is not needed for sleep
256 confirm command */
257 adapter->ps_state = PS_STATE_SLEEP;
258 else
259 adapter->ps_state = PS_STATE_SLEEP_CFM;
260
Marc Yang2b06bdb2011-03-30 18:12:44 -0700261 if (!sleep_cfm_buf->ps_cfm_sleep.resp_ctrl
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700262 && (adapter->is_hs_configured
263 && !adapter->sleep_period.period)) {
264 adapter->pm_wakeup_card_req = true;
265 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
266 MWIFIEX_BSS_ROLE_STA), true);
267 }
268 }
269
270 return ret;
271}
272
273/*
274 * This function allocates the command buffers and links them to
275 * the command free queue.
276 *
277 * The driver uses a pre allocated number of command buffers, which
278 * are created at driver initializations and freed at driver cleanup.
279 * Every command needs to obtain a command buffer from this pool before
280 * it can be issued. The command free queue lists the command buffers
281 * currently free to use, while the command pending queue lists the
282 * command buffers already in use and awaiting handling. Command buffers
283 * are returned to the free queue after use.
284 */
285int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
286{
287 struct cmd_ctrl_node *cmd_array;
288 u32 buf_size;
289 u32 i;
290
291 /* Allocate and initialize struct cmd_ctrl_node */
292 buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
293 cmd_array = kzalloc(buf_size, GFP_KERNEL);
294 if (!cmd_array) {
295 dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
296 __func__);
297 return -1;
298 }
299
300 adapter->cmd_pool = cmd_array;
301 memset(adapter->cmd_pool, 0, buf_size);
302
303 /* Allocate and initialize command buffers */
304 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
305 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
306 if (!cmd_array[i].skb) {
307 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
308 return -1;
309 }
310 }
311
312 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
313 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
314
315 return 0;
316}
317
318/*
319 * This function frees the command buffers.
320 *
321 * The function calls the completion callback for all the command
322 * buffers that still have response buffers associated with them.
323 */
324int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
325{
326 struct cmd_ctrl_node *cmd_array;
327 u32 i;
328
329 /* Need to check if cmd pool is allocated or not */
330 if (!adapter->cmd_pool) {
331 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
332 return 0;
333 }
334
335 cmd_array = adapter->cmd_pool;
336
337 /* Release shared memory buffers */
338 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
339 if (cmd_array[i].skb) {
340 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
341 dev_kfree_skb_any(cmd_array[i].skb);
342 }
343 if (!cmd_array[i].resp_skb)
344 continue;
345 mwifiex_recv_complete(adapter, cmd_array[i].resp_skb, 0);
346 }
347 /* Release struct cmd_ctrl_node */
348 if (adapter->cmd_pool) {
349 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
350 kfree(adapter->cmd_pool);
351 adapter->cmd_pool = NULL;
352 }
353
354 return 0;
355}
356
357/*
358 * This function handles events generated by firmware.
359 *
360 * Event body of events received from firmware are not used (though they are
361 * saved), only the event ID is used. Some events are re-invoked by
362 * the driver, with a new event body.
363 *
364 * After processing, the function calls the completion callback
365 * for cleanup.
366 */
367int mwifiex_process_event(struct mwifiex_adapter *adapter)
368{
369 int ret = 0;
370 struct mwifiex_private *priv =
371 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
372 struct sk_buff *skb = adapter->event_skb;
373 u32 eventcause = adapter->event_cause;
374 struct timeval tstamp;
375 struct mwifiex_rxinfo *rx_info = NULL;
376
377 /* Save the last event to debug log */
378 adapter->dbg.last_event_index =
379 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
380 adapter->dbg.last_event[adapter->dbg.last_event_index] =
381 (u16) eventcause;
382
383 /* Get BSS number and corresponding priv */
384 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
385 EVENT_GET_BSS_TYPE(eventcause));
386 if (!priv)
387 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
388 /* Clear BSS_NO_BITS from event */
389 eventcause &= EVENT_ID_MASK;
390 adapter->event_cause = eventcause;
391
392 if (skb) {
393 rx_info = MWIFIEX_SKB_RXCB(skb);
394 rx_info->bss_index = priv->bss_index;
395 }
396
397 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
398 do_gettimeofday(&tstamp);
399 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
400 tstamp.tv_sec, tstamp.tv_usec, eventcause);
401 }
402
403 ret = mwifiex_process_sta_event(priv);
404
405 adapter->event_cause = 0;
406 adapter->event_skb = NULL;
407
408 mwifiex_recv_complete(adapter, skb, 0);
409
410 return ret;
411}
412
413/*
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700414 * This function is used to send synchronous command to the firmware.
415 *
416 * it allocates a wait queue for the command and wait for the command
417 * response.
418 */
419int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
420 u16 cmd_action, u32 cmd_oid, void *data_buf)
421{
422 int ret = 0;
423 struct mwifiex_adapter *adapter = priv->adapter;
424
425 adapter->cmd_wait_q_required = true;
426 adapter->cmd_wait_q.condition = false;
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.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700439 *
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 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700448int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
449 u16 cmd_action, u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700450{
451 int ret = 0;
452 struct mwifiex_adapter *adapter = priv->adapter;
453 struct cmd_ctrl_node *cmd_node = NULL;
454 struct host_cmd_ds_command *cmd_ptr = NULL;
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 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700487 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700488
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",
513 cmd_no);
514 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
515 return -1;
516 }
517
518 /* Send command */
519 if (cmd_no == HostCmd_CMD_802_11_SCAN)
520 mwifiex_queue_scan_cmd(priv, cmd_node);
521 else
522 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
523
524 return ret;
525}
526
527/*
528 * This function returns a command to the command free queue.
529 *
530 * The function also calls the completion callback if required, before
531 * cleaning the command node and re-inserting it into the free queue.
532 */
533void
534mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
535 struct cmd_ctrl_node *cmd_node)
536{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700537 unsigned long flags;
538
539 if (cmd_node == NULL)
540 return;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700541
542 if (cmd_node->wait_q_enabled)
543 mwifiex_complete_cmd(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700544 /* Clean the node */
545 mwifiex_clean_cmd_node(adapter, cmd_node);
546
547 /* Insert node into cmd_free_q */
548 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
549 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
550 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
551
552 return;
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 =
581 &host_cmd->params.psmode_enh;
582 if ((le16_to_cpu(pm->action) == DIS_PS)
583 || (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
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);
597
598 return;
599}
600
601/*
602 * This function executes the next command in command pending queue.
603 *
604 * This function will fail if a command is already in processing stage,
605 * otherwise it will dequeue the first command from the command pending
606 * queue and send to the firmware.
607 *
608 * If the device is currently in host sleep mode, any commands, except the
609 * host sleep configuration command will de-activate the host sleep. For PS
610 * mode, the function will put the firmware back to sleep if applicable.
611 */
612int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
613{
614 struct mwifiex_private *priv = NULL;
615 struct cmd_ctrl_node *cmd_node = NULL;
616 int ret = 0;
617 struct host_cmd_ds_command *host_cmd;
618 unsigned long cmd_flags;
619 unsigned long cmd_pending_q_flags;
620
621 /* Check if already in processing */
622 if (adapter->curr_cmd) {
623 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
624 return -1;
625 }
626
627 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
628 /* Check if any command is pending */
629 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
630 if (list_empty(&adapter->cmd_pending_q)) {
631 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
632 cmd_pending_q_flags);
633 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
634 return 0;
635 }
636 cmd_node = list_first_entry(&adapter->cmd_pending_q,
637 struct cmd_ctrl_node, list);
638 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
639 cmd_pending_q_flags);
640
641 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
642 priv = cmd_node->priv;
643
644 if (adapter->ps_state != PS_STATE_AWAKE) {
645 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
646 " this should not happen\n", __func__);
647 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
648 return ret;
649 }
650
651 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
652 list_del(&cmd_node->list);
653 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
654 cmd_pending_q_flags);
655
656 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
657 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
658 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
659 /* Any command sent to the firmware when host is in sleep
660 * mode should de-configure host sleep. We should skip the
661 * host sleep configuration command itself though
662 */
663 if (priv && (host_cmd->command !=
664 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
665 if (adapter->hs_activated) {
666 adapter->is_hs_configured = false;
667 mwifiex_hs_activated_event(priv, false);
668 }
669 }
670
671 return ret;
672}
673
674/*
675 * This function handles the command response.
676 *
677 * After processing, the function cleans the command node and puts
678 * it back to the command free queue.
679 */
680int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
681{
682 struct host_cmd_ds_command *resp = NULL;
683 struct mwifiex_private *priv =
684 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
685 int ret = 0;
686 uint16_t orig_cmdresp_no;
687 uint16_t cmdresp_no;
688 uint16_t cmdresp_result;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700689 struct timeval tstamp;
690 unsigned long flags;
691
692 /* Now we got response from FW, cancel the command timer */
693 del_timer(&adapter->cmd_timer);
694
695 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
696 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
697 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
698 le16_to_cpu(resp->command));
699 return -1;
700 }
701
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700702 adapter->num_cmd_timeout = 0;
703
704 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
705 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
706 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
707 le16_to_cpu(resp->command));
708 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
709 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
710 adapter->curr_cmd = NULL;
711 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
712 return -1;
713 }
714
715 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
716 /* Copy original response back to response buffer */
717 struct mwifiex_ds_misc_cmd *hostcmd = NULL;
718 uint16_t size = le16_to_cpu(resp->size);
719 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
720 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
721 if (adapter->curr_cmd->data_buf) {
722 hostcmd = (struct mwifiex_ds_misc_cmd *)
723 adapter->curr_cmd->data_buf;
724 hostcmd->len = size;
725 memcpy(hostcmd->cmd, (void *) resp, size);
726 }
727 }
728 orig_cmdresp_no = le16_to_cpu(resp->command);
729
730 /* Get BSS number and corresponding priv */
731 priv = mwifiex_get_priv_by_id(adapter,
732 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
733 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
734 if (!priv)
735 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
736 /* Clear RET_BIT from HostCmd */
737 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
738
739 cmdresp_no = le16_to_cpu(resp->command);
740 cmdresp_result = le16_to_cpu(resp->result);
741
742 /* Save the last command response to debug log */
743 adapter->dbg.last_cmd_resp_index =
744 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
745 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
746 orig_cmdresp_no;
747
748 do_gettimeofday(&tstamp);
749 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
750 " len %d, seqno 0x%x\n",
751 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
752 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
753
754 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
755 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700756 if (adapter->curr_cmd->wait_q_enabled)
757 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758
759 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
760 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
761 adapter->curr_cmd = NULL;
762 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
763 return -1;
764 }
765
766 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
767 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
768 if ((cmdresp_result == HostCmd_RESULT_OK)
769 && (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
770 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
771 } else {
772 /* handle response */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700773 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 }
775
776 /* Check init command response */
777 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
778 if (ret == -1) {
779 dev_err(adapter->dev, "%s: cmd %#x failed during "
780 "initialization\n", __func__, cmdresp_no);
781 mwifiex_init_fw_complete(adapter);
782 return -1;
783 } else if (adapter->last_init_cmd == cmdresp_no)
784 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
785 }
786
787 if (adapter->curr_cmd) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700788 if (adapter->curr_cmd->wait_q_enabled && (!ret))
789 adapter->cmd_wait_q.status = 0;
790 else if (adapter->curr_cmd->wait_q_enabled && (ret == -1))
791 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792
793 /* Clean up and put current command back to cmd_free_q */
794 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
795
796 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
797 adapter->curr_cmd = NULL;
798 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
799 }
800
801 return ret;
802}
803
804/*
805 * This function handles the timeout of command sending.
806 *
807 * It will re-send the same command again.
808 */
809void
810mwifiex_cmd_timeout_func(unsigned long function_context)
811{
812 struct mwifiex_adapter *adapter =
813 (struct mwifiex_adapter *) function_context;
814 struct cmd_ctrl_node *cmd_node = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700815 struct timeval tstamp;
816
817 adapter->num_cmd_timeout++;
818 adapter->dbg.num_cmd_timeout++;
819 if (!adapter->curr_cmd) {
820 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
821 return;
822 }
823 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700824 if (cmd_node->wait_q_enabled)
825 adapter->cmd_wait_q.status = -ETIMEDOUT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700826
827 if (cmd_node) {
828 adapter->dbg.timeout_cmd_id =
829 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
830 adapter->dbg.timeout_cmd_act =
831 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
832 do_gettimeofday(&tstamp);
833 dev_err(adapter->dev, "%s: Timeout cmd id (%lu.%lu) = %#x,"
834 " act = %#x\n", __func__,
835 tstamp.tv_sec, tstamp.tv_usec,
836 adapter->dbg.timeout_cmd_id,
837 adapter->dbg.timeout_cmd_act);
838
839 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
840 adapter->dbg.num_tx_host_to_card_failure);
841 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
842 adapter->dbg.num_cmd_host_to_card_failure);
843
844 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
845 adapter->dbg.num_cmd_timeout);
846 dev_err(adapter->dev, "num_tx_timeout = %d\n",
847 adapter->dbg.num_tx_timeout);
848
849 dev_err(adapter->dev, "last_cmd_index = %d\n",
850 adapter->dbg.last_cmd_index);
851 print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
852 adapter->dbg.last_cmd_id, DBG_CMD_NUM);
853 print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
854 adapter->dbg.last_cmd_act, DBG_CMD_NUM);
855
856 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
857 adapter->dbg.last_cmd_resp_index);
858 print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
859 adapter->dbg.last_cmd_resp_id, DBG_CMD_NUM);
860
861 dev_err(adapter->dev, "last_event_index = %d\n",
862 adapter->dbg.last_event_index);
863 print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
864 adapter->dbg.last_event, DBG_CMD_NUM);
865
866 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
867 adapter->data_sent, adapter->cmd_sent);
868
869 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
870 adapter->ps_mode, adapter->ps_state);
871 }
872 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
873 mwifiex_init_fw_complete(adapter);
874
875 return;
876}
877
878/*
879 * This function cancels all the pending commands.
880 *
881 * The current command, all commands in command pending queue and all scan
882 * commands in scan pending queue are cancelled. All the completion callbacks
883 * are called with failure status to ensure cleanup.
884 */
885void
886mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
887{
888 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700889 unsigned long flags;
890
891 /* Cancel current cmd */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700892 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700893 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700894 adapter->curr_cmd->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700895 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700896 adapter->cmd_wait_q.status = -1;
897 mwifiex_complete_cmd(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700898 }
899 /* Cancel all pending command */
900 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
901 list_for_each_entry_safe(cmd_node, tmp_node,
902 &adapter->cmd_pending_q, list) {
903 list_del(&cmd_node->list);
904 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
905
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700906 if (cmd_node->wait_q_enabled) {
907 adapter->cmd_wait_q.status = -1;
908 mwifiex_complete_cmd(adapter);
909 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700910 }
911 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
912 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
913 }
914 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
915
916 /* Cancel all pending scan command */
917 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
918 list_for_each_entry_safe(cmd_node, tmp_node,
919 &adapter->scan_pending_q, list) {
920 list_del(&cmd_node->list);
921 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
922
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700923 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700924 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
925 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
926 }
927 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
928
929 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
930 adapter->scan_processing = false;
931 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
932}
933
934/*
935 * This function cancels all pending commands that matches with
936 * the given IOCTL request.
937 *
938 * Both the current command buffer and the pending command queue are
939 * searched for matching IOCTL request. The completion callback of
940 * the matched command is called with failure status to ensure cleanup.
941 * In case of scan commands, all pending commands in scan pending queue
942 * are cancelled.
943 */
944void
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700945mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700946{
947 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
948 unsigned long cmd_flags;
949 unsigned long cmd_pending_q_flags;
950 unsigned long scan_pending_q_flags;
951 uint16_t cancel_scan_cmd = false;
952
953 if ((adapter->curr_cmd) &&
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700954 (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700955 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
956 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700957 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700958 cmd_node->cmd_flag |= CMD_F_CANCELED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700959 spin_lock_irqsave(&adapter->cmd_pending_q_lock,
960 cmd_pending_q_flags);
961 list_del(&cmd_node->list);
962 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
963 cmd_pending_q_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700964 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700965 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700966 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700967
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700968 /* Cancel all pending scan command */
969 spin_lock_irqsave(&adapter->scan_pending_q_lock,
970 scan_pending_q_flags);
971 list_for_each_entry_safe(cmd_node, tmp_node,
972 &adapter->scan_pending_q, list) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700973 list_del(&cmd_node->list);
974 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
975 scan_pending_q_flags);
976 cmd_node->wait_q_enabled = false;
977 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
978 spin_lock_irqsave(&adapter->scan_pending_q_lock,
979 scan_pending_q_flags);
980 cancel_scan_cmd = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700981 }
982 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
983 scan_pending_q_flags);
984
985 if (cancel_scan_cmd) {
986 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
987 adapter->scan_processing = false;
988 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
989 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700990 adapter->cmd_wait_q.status = -1;
991 mwifiex_complete_cmd(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700992
993 return;
994}
995
996/*
997 * This function sends the sleep confirm command to firmware, if
998 * possible.
999 *
1000 * The sleep confirm command cannot be issued if command response,
1001 * data response or event response is awaiting handling, or if we
1002 * are in the middle of sending a command, or expecting a command
1003 * response.
1004 */
1005void
1006mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1007{
1008 if (!adapter->cmd_sent &&
1009 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1010 mwifiex_dnld_sleep_confirm_cmd(adapter);
1011 else
1012 dev_dbg(adapter->dev,
1013 "cmd: Delay Sleep Confirm (%s%s%s)\n",
1014 (adapter->cmd_sent) ? "D" : "",
1015 (adapter->curr_cmd) ? "C" : "",
1016 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
1017}
1018
1019/*
1020 * This function sends a Host Sleep activated event to applications.
1021 *
1022 * This event is generated by the driver, with a blank event body.
1023 */
1024void
1025mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1026{
1027 if (activated) {
1028 if (priv->adapter->is_hs_configured) {
1029 priv->adapter->hs_activated = true;
1030 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1031 priv->adapter->hs_activate_wait_q_woken = true;
1032 wake_up_interruptible(
1033 &priv->adapter->hs_activate_wait_q);
1034 } else {
1035 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1036 }
1037 } else {
1038 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1039 priv->adapter->hs_activated = false;
1040 }
1041}
1042
1043/*
1044 * This function handles the command response of a Host Sleep configuration
1045 * command.
1046 *
1047 * Handling includes changing the header fields into CPU format
1048 * and setting the current host sleep activation status in driver.
1049 *
1050 * In case host sleep status change, the function generates an event to
1051 * notify the applications.
1052 */
1053int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1054 struct host_cmd_ds_command *resp)
1055{
1056 struct mwifiex_adapter *adapter = priv->adapter;
1057 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1058 &resp->params.opt_hs_cfg;
1059 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1060
1061 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE)) {
1062 mwifiex_hs_activated_event(priv, true);
1063 return 0;
1064 } else {
1065 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1066 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1067 resp->result, conditions,
1068 phs_cfg->params.hs_config.gpio,
1069 phs_cfg->params.hs_config.gap);
1070 }
1071 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1072 adapter->is_hs_configured = true;
1073 } else {
1074 adapter->is_hs_configured = false;
1075 if (adapter->hs_activated)
1076 mwifiex_hs_activated_event(priv, false);
1077 }
1078
1079 return 0;
1080}
1081
1082/*
1083 * This function wakes up the adapter and generates a Host Sleep
1084 * cancel event on receiving the power up interrupt.
1085 */
1086void
1087mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1088{
1089 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1090 " since there is interrupt from the firmware\n", __func__);
1091
1092 adapter->if_ops.wakeup(adapter);
1093 adapter->hs_activated = false;
1094 adapter->is_hs_configured = false;
1095 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1096 MWIFIEX_BSS_ROLE_ANY), false);
1097 return;
1098}
1099
1100/*
1101 * This function handles the command response of a sleep confirm command.
1102 *
1103 * The function sets the card state to SLEEP if the response indicates success.
1104 */
1105void
1106mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1107 u8 *pbuf, u32 upld_len)
1108{
1109 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1110 struct mwifiex_private *priv =
1111 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1112 uint16_t result = le16_to_cpu(cmd->result);
1113 uint16_t command = le16_to_cpu(cmd->command);
1114 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1115
1116 if (!upld_len) {
1117 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1118 return;
1119 }
1120
1121 /* Get BSS number and corresponding priv */
1122 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1123 HostCmd_GET_BSS_TYPE(seq_num));
1124 if (!priv)
1125 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1126
1127 /* Update sequence number */
1128 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1129 /* Clear RET_BIT from HostCmd */
1130 command &= HostCmd_CMD_ID_MASK;
1131
1132 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
1133 dev_err(adapter->dev, "%s: received unexpected response for"
1134 " cmd %x, result = %x\n", __func__, command, result);
1135 return;
1136 }
1137
1138 if (result) {
1139 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
1140 __func__);
1141 adapter->pm_wakeup_card_req = false;
1142 adapter->ps_state = PS_STATE_AWAKE;
1143 return;
1144 }
1145 adapter->pm_wakeup_card_req = true;
1146 if (adapter->is_hs_configured)
1147 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1148 MWIFIEX_BSS_ROLE_ANY), true);
1149 adapter->ps_state = PS_STATE_SLEEP;
1150 cmd->command = cpu_to_le16(command);
1151 cmd->seq_num = cpu_to_le16(seq_num);
1152}
1153EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1154
1155/*
1156 * This function prepares an enhanced power mode command.
1157 *
1158 * This function can be used to disable power save or to configure
1159 * power save with auto PS or STA PS or auto deep sleep.
1160 *
1161 * Preparation includes -
1162 * - Setting command ID, action and proper size
1163 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1164 * auto deep sleep TLV (as required)
1165 * - Ensuring correct endian-ness
1166 */
1167int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1168 struct host_cmd_ds_command *cmd,
1169 u16 cmd_action, uint16_t ps_bitmap,
1170 void *data_buf)
1171{
1172 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1173 &cmd->params.psmode_enh;
1174 u8 *tlv = NULL;
1175 u16 cmd_size = 0;
1176
1177 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1178 if (cmd_action == DIS_AUTO_PS) {
1179 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1180 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001181 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1182 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001183 } else if (cmd_action == GET_PS) {
1184 psmode_enh->action = cpu_to_le16(GET_PS);
1185 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001186 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1187 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001188 } else if (cmd_action == EN_AUTO_PS) {
1189 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001190 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1191 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
1192 sizeof(psmode_enh->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001193 tlv = (u8 *) cmd + cmd_size;
1194 if (ps_bitmap & BITMAP_STA_PS) {
1195 struct mwifiex_adapter *adapter = priv->adapter;
1196 struct mwifiex_ie_types_ps_param *ps_tlv =
1197 (struct mwifiex_ie_types_ps_param *) tlv;
1198 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1199 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1200 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1201 sizeof(struct mwifiex_ie_types_header));
1202 cmd_size += sizeof(*ps_tlv);
1203 tlv += sizeof(*ps_tlv);
1204 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1205 ps_mode->null_pkt_interval =
1206 cpu_to_le16(adapter->null_pkt_interval);
1207 ps_mode->multiple_dtims =
1208 cpu_to_le16(adapter->multiple_dtim);
1209 ps_mode->bcn_miss_timeout =
1210 cpu_to_le16(adapter->bcn_miss_time_out);
1211 ps_mode->local_listen_interval =
1212 cpu_to_le16(adapter->local_listen_interval);
1213 ps_mode->adhoc_wake_period =
1214 cpu_to_le16(adapter->adhoc_awake_period);
1215 ps_mode->delay_to_ps =
1216 cpu_to_le16(adapter->delay_to_ps);
1217 ps_mode->mode =
1218 cpu_to_le16(adapter->enhanced_ps_mode);
1219
1220 }
1221 if (ps_bitmap & BITMAP_AUTO_DS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001222 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001223 (struct mwifiex_ie_types_auto_ds_param *) tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001224 u16 idletime = 0;
Marc Yang2b06bdb2011-03-30 18:12:44 -07001225
1226 auto_ds_tlv->header.type =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001227 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001228 auto_ds_tlv->header.len =
1229 cpu_to_le16(sizeof(*auto_ds_tlv) -
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001230 sizeof(struct mwifiex_ie_types_header));
Marc Yang2b06bdb2011-03-30 18:12:44 -07001231 cmd_size += sizeof(*auto_ds_tlv);
1232 tlv += sizeof(*auto_ds_tlv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001233 if (data_buf)
1234 idletime = ((struct mwifiex_ds_auto_ds *)
1235 data_buf)->idle_time;
1236 dev_dbg(priv->adapter->dev,
1237 "cmd: PS Command: Enter Auto Deep Sleep\n");
Marc Yang2b06bdb2011-03-30 18:12:44 -07001238 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001239 }
1240 cmd->size = cpu_to_le16(cmd_size);
1241 }
1242 return 0;
1243}
1244
1245/*
1246 * This function handles the command response of an enhanced power mode
1247 * command.
1248 *
1249 * Handling includes changing the header fields into CPU format
1250 * and setting the current enhanced power mode in driver.
1251 */
1252int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1253 struct host_cmd_ds_command *resp,
1254 void *data_buf)
1255{
1256 struct mwifiex_adapter *adapter = priv->adapter;
1257 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1258 &resp->params.psmode_enh;
1259 uint16_t action = le16_to_cpu(ps_mode->action);
1260 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1261 uint16_t auto_ps_bitmap =
Marc Yang2b06bdb2011-03-30 18:12:44 -07001262 le16_to_cpu(ps_mode->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001263
1264 dev_dbg(adapter->dev, "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1265 __func__, resp->result, action);
1266 if (action == EN_AUTO_PS) {
1267 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1268 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1269 priv->adapter->is_deep_sleep = true;
1270 }
1271 if (auto_ps_bitmap & BITMAP_STA_PS) {
1272 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1273 if (adapter->sleep_period.period)
1274 dev_dbg(adapter->dev, "cmd: set to uapsd/pps mode\n");
1275 }
1276 } else if (action == DIS_AUTO_PS) {
1277 if (ps_bitmap & BITMAP_AUTO_DS) {
1278 priv->adapter->is_deep_sleep = false;
1279 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1280 }
1281 if (ps_bitmap & BITMAP_STA_PS) {
1282 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1283 if (adapter->sleep_period.period) {
1284 adapter->delay_null_pkt = false;
1285 adapter->tx_lock_flag = false;
1286 adapter->pps_uapsd_mode = false;
1287 }
1288 }
1289 } else if (action == GET_PS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001290 if (ps_bitmap & BITMAP_STA_PS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001291 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1292 else
1293 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1294
1295 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1296
1297 if (data_buf) {
1298 /* This section is for get power save mode */
1299 struct mwifiex_ds_pm_cfg *pm_cfg =
1300 (struct mwifiex_ds_pm_cfg *)data_buf;
1301 if (ps_bitmap & BITMAP_STA_PS)
1302 pm_cfg->param.ps_mode = 1;
1303 else
1304 pm_cfg->param.ps_mode = 0;
1305 }
1306 }
1307 return 0;
1308}
1309
1310/*
1311 * This function prepares command to get hardware specifications.
1312 *
1313 * Preparation includes -
1314 * - Setting command ID, action and proper size
1315 * - Setting permanent address parameter
1316 * - Ensuring correct endian-ness
1317 */
1318int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1319 struct host_cmd_ds_command *cmd)
1320{
1321 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1322
1323 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1324 cmd->size =
1325 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1326 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1327
1328 return 0;
1329}
1330
1331/*
1332 * This function handles the command response of get hardware
1333 * specifications.
1334 *
1335 * Handling includes changing the header fields into CPU format
1336 * and saving/updating the following parameters in driver -
1337 * - Firmware capability information
1338 * - Firmware band settings
1339 * - Ad-hoc start band and channel
1340 * - Ad-hoc 11n activation status
1341 * - Firmware release number
1342 * - Number of antennas
1343 * - Hardware address
1344 * - Hardware interface version
1345 * - Firmware version
1346 * - Region code
1347 * - 11n capabilities
1348 * - MCS support fields
1349 * - MP end port
1350 */
1351int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1352 struct host_cmd_ds_command *resp)
1353{
1354 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1355 struct mwifiex_adapter *adapter = priv->adapter;
1356 int i;
1357
1358 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1359
1360 if (IS_SUPPORT_MULTI_BANDS(adapter))
1361 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1362 else
1363 adapter->fw_bands = BAND_B;
1364
1365 adapter->config_bands = adapter->fw_bands;
1366
1367 if (adapter->fw_bands & BAND_A) {
1368 if (adapter->fw_bands & BAND_GN) {
1369 adapter->config_bands |= BAND_AN;
1370 adapter->fw_bands |= BAND_AN;
1371 }
1372 if (adapter->fw_bands & BAND_AN) {
1373 adapter->adhoc_start_band = BAND_A | BAND_AN;
1374 adapter->adhoc_11n_enabled = true;
1375 } else {
1376 adapter->adhoc_start_band = BAND_A;
1377 }
1378 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1379 } else if (adapter->fw_bands & BAND_GN) {
1380 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1381 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1382 adapter->adhoc_11n_enabled = true;
1383 } else if (adapter->fw_bands & BAND_G) {
1384 adapter->adhoc_start_band = BAND_G | BAND_B;
1385 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1386 } else if (adapter->fw_bands & BAND_B) {
1387 adapter->adhoc_start_band = BAND_B;
1388 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1389 }
1390
1391 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1392 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1393
1394 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
1395 adapter->fw_release_number);
1396 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
1397 hw_spec->permanent_addr);
1398 dev_dbg(adapter->dev, "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
1399 le16_to_cpu(hw_spec->hw_if_version),
1400 le16_to_cpu(hw_spec->version));
1401
1402 if (priv->curr_addr[0] == 0xff)
1403 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1404
1405 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1406
1407 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1408 /* Use the region code to search for the index */
1409 if (adapter->region_code == region_code_index[i])
1410 break;
1411
1412 /* If it's unidentified region code, use the default (USA) */
1413 if (i >= MWIFIEX_MAX_REGION_CODE) {
1414 adapter->region_code = 0x10;
1415 dev_dbg(adapter->dev, "cmd: unknown region code, use default (USA)\n");
1416 }
1417
1418 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001419 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001420
1421 if (adapter->if_ops.update_mp_end_port)
1422 adapter->if_ops.update_mp_end_port(adapter,
1423 le16_to_cpu(hw_spec->mp_end_port));
1424
1425 return 0;
1426}