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