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