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