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