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