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