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