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