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