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