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