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