1 /*
2  * NXP Wireless LAN device driver: station command response handling
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * 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 /*
31  * This function handles the command response error case.
32  *
33  * For scan response error, the function cancels all the pending
34  * scan commands and generates an event to inform the applications
35  * of the scan completion.
36  *
37  * For Power Save command failure, we do not retry enter PS
38  * command in case of Ad-hoc mode.
39  *
40  * For all other response errors, the current command buffer is freed
41  * and returned to the free command queue.
42  */
43 static void
mwifiex_process_cmdresp_error(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)44 mwifiex_process_cmdresp_error(struct mwifiex_private *priv,
45 			      struct host_cmd_ds_command *resp)
46 {
47 	struct mwifiex_adapter *adapter = priv->adapter;
48 	struct host_cmd_ds_802_11_ps_mode_enh *pm;
49 
50 	mwifiex_dbg(adapter, ERROR,
51 		    "CMD_RESP: cmd %#x error, result=%#x\n",
52 		    resp->command, resp->result);
53 
54 	if (adapter->curr_cmd->wait_q_enabled)
55 		adapter->cmd_wait_q.status = -1;
56 
57 	switch (le16_to_cpu(resp->command)) {
58 	case HostCmd_CMD_802_11_PS_MODE_ENH:
59 		pm = &resp->params.psmode_enh;
60 		mwifiex_dbg(adapter, ERROR,
61 			    "PS_MODE_ENH cmd failed: result=0x%x action=0x%X\n",
62 			    resp->result, le16_to_cpu(pm->action));
63 		/* We do not re-try enter-ps command in ad-hoc mode. */
64 		if (le16_to_cpu(pm->action) == EN_AUTO_PS &&
65 		    (le16_to_cpu(pm->params.ps_bitmap) & BITMAP_STA_PS) &&
66 		    priv->bss_mode == NL80211_IFTYPE_ADHOC)
67 			adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
68 
69 		break;
70 	case HostCmd_CMD_802_11_SCAN:
71 	case HostCmd_CMD_802_11_SCAN_EXT:
72 		mwifiex_cancel_scan(adapter);
73 		break;
74 
75 	case HostCmd_CMD_MAC_CONTROL:
76 		break;
77 
78 	case HostCmd_CMD_SDIO_SP_RX_AGGR_CFG:
79 		mwifiex_dbg(adapter, MSG,
80 			    "SDIO RX single-port aggregation Not support\n");
81 		break;
82 
83 	default:
84 		break;
85 	}
86 	/* Handling errors here */
87 	mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
88 
89 	spin_lock_bh(&adapter->mwifiex_cmd_lock);
90 	adapter->curr_cmd = NULL;
91 	spin_unlock_bh(&adapter->mwifiex_cmd_lock);
92 }
93 
94 /*
95  * This function handles the command response of get RSSI info.
96  *
97  * Handling includes changing the header fields into CPU format
98  * and saving the following parameters in driver -
99  *      - Last data and beacon RSSI value
100  *      - Average data and beacon RSSI value
101  *      - Last data and beacon NF value
102  *      - Average data and beacon NF value
103  *
104  * The parameters are send to the application as well, along with
105  * calculated SNR values.
106  */
mwifiex_ret_802_11_rssi_info(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)107 static int mwifiex_ret_802_11_rssi_info(struct mwifiex_private *priv,
108 					struct host_cmd_ds_command *resp)
109 {
110 	struct host_cmd_ds_802_11_rssi_info_rsp *rssi_info_rsp =
111 						&resp->params.rssi_info_rsp;
112 	struct mwifiex_ds_misc_subsc_evt *subsc_evt =
113 						&priv->async_subsc_evt_storage;
114 
115 	priv->data_rssi_last = le16_to_cpu(rssi_info_rsp->data_rssi_last);
116 	priv->data_nf_last = le16_to_cpu(rssi_info_rsp->data_nf_last);
117 
118 	priv->data_rssi_avg = le16_to_cpu(rssi_info_rsp->data_rssi_avg);
119 	priv->data_nf_avg = le16_to_cpu(rssi_info_rsp->data_nf_avg);
120 
121 	priv->bcn_rssi_last = le16_to_cpu(rssi_info_rsp->bcn_rssi_last);
122 	priv->bcn_nf_last = le16_to_cpu(rssi_info_rsp->bcn_nf_last);
123 
124 	priv->bcn_rssi_avg = le16_to_cpu(rssi_info_rsp->bcn_rssi_avg);
125 	priv->bcn_nf_avg = le16_to_cpu(rssi_info_rsp->bcn_nf_avg);
126 
127 	if (priv->subsc_evt_rssi_state == EVENT_HANDLED)
128 		return 0;
129 
130 	memset(subsc_evt, 0x00, sizeof(struct mwifiex_ds_misc_subsc_evt));
131 
132 	/* Resubscribe low and high rssi events with new thresholds */
133 	subsc_evt->events = BITMASK_BCN_RSSI_LOW | BITMASK_BCN_RSSI_HIGH;
134 	subsc_evt->action = HostCmd_ACT_BITWISE_SET;
135 	if (priv->subsc_evt_rssi_state == RSSI_LOW_RECVD) {
136 		subsc_evt->bcn_l_rssi_cfg.abs_value = abs(priv->bcn_rssi_avg -
137 				priv->cqm_rssi_hyst);
138 		subsc_evt->bcn_h_rssi_cfg.abs_value = abs(priv->cqm_rssi_thold);
139 	} else if (priv->subsc_evt_rssi_state == RSSI_HIGH_RECVD) {
140 		subsc_evt->bcn_l_rssi_cfg.abs_value = abs(priv->cqm_rssi_thold);
141 		subsc_evt->bcn_h_rssi_cfg.abs_value = abs(priv->bcn_rssi_avg +
142 				priv->cqm_rssi_hyst);
143 	}
144 	subsc_evt->bcn_l_rssi_cfg.evt_freq = 1;
145 	subsc_evt->bcn_h_rssi_cfg.evt_freq = 1;
146 
147 	priv->subsc_evt_rssi_state = EVENT_HANDLED;
148 
149 	mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SUBSCRIBE_EVENT,
150 			 0, 0, subsc_evt, false);
151 
152 	return 0;
153 }
154 
155 /*
156  * This function handles the command response of set/get SNMP
157  * MIB parameters.
158  *
159  * Handling includes changing the header fields into CPU format
160  * and saving the parameter in driver.
161  *
162  * The following parameters are supported -
163  *      - Fragmentation threshold
164  *      - RTS threshold
165  *      - Short retry limit
166  */
mwifiex_ret_802_11_snmp_mib(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,u32 * data_buf)167 static int mwifiex_ret_802_11_snmp_mib(struct mwifiex_private *priv,
168 				       struct host_cmd_ds_command *resp,
169 				       u32 *data_buf)
170 {
171 	struct host_cmd_ds_802_11_snmp_mib *smib = &resp->params.smib;
172 	u16 oid = le16_to_cpu(smib->oid);
173 	u16 query_type = le16_to_cpu(smib->query_type);
174 	u32 ul_temp;
175 
176 	mwifiex_dbg(priv->adapter, INFO,
177 		    "info: SNMP_RESP: oid value = %#x,\t"
178 		    "query_type = %#x, buf size = %#x\n",
179 		    oid, query_type, le16_to_cpu(smib->buf_size));
180 	if (query_type == HostCmd_ACT_GEN_GET) {
181 		ul_temp = get_unaligned_le16(smib->value);
182 		if (data_buf)
183 			*data_buf = ul_temp;
184 		switch (oid) {
185 		case FRAG_THRESH_I:
186 			mwifiex_dbg(priv->adapter, INFO,
187 				    "info: SNMP_RESP: FragThsd =%u\n",
188 				    ul_temp);
189 			break;
190 		case RTS_THRESH_I:
191 			mwifiex_dbg(priv->adapter, INFO,
192 				    "info: SNMP_RESP: RTSThsd =%u\n",
193 				    ul_temp);
194 			break;
195 		case SHORT_RETRY_LIM_I:
196 			mwifiex_dbg(priv->adapter, INFO,
197 				    "info: SNMP_RESP: TxRetryCount=%u\n",
198 				    ul_temp);
199 			break;
200 		case DTIM_PERIOD_I:
201 			mwifiex_dbg(priv->adapter, INFO,
202 				    "info: SNMP_RESP: DTIM period=%u\n",
203 				    ul_temp);
204 			break;
205 		default:
206 			break;
207 		}
208 	}
209 
210 	return 0;
211 }
212 
213 /*
214  * This function handles the command response of get log request
215  *
216  * Handling includes changing the header fields into CPU format
217  * and sending the received parameters to application.
218  */
mwifiex_ret_get_log(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,struct mwifiex_ds_get_stats * stats)219 static int mwifiex_ret_get_log(struct mwifiex_private *priv,
220 			       struct host_cmd_ds_command *resp,
221 			       struct mwifiex_ds_get_stats *stats)
222 {
223 	struct host_cmd_ds_802_11_get_log *get_log =
224 		&resp->params.get_log;
225 
226 	if (stats) {
227 		stats->mcast_tx_frame = le32_to_cpu(get_log->mcast_tx_frame);
228 		stats->failed = le32_to_cpu(get_log->failed);
229 		stats->retry = le32_to_cpu(get_log->retry);
230 		stats->multi_retry = le32_to_cpu(get_log->multi_retry);
231 		stats->frame_dup = le32_to_cpu(get_log->frame_dup);
232 		stats->rts_success = le32_to_cpu(get_log->rts_success);
233 		stats->rts_failure = le32_to_cpu(get_log->rts_failure);
234 		stats->ack_failure = le32_to_cpu(get_log->ack_failure);
235 		stats->rx_frag = le32_to_cpu(get_log->rx_frag);
236 		stats->mcast_rx_frame = le32_to_cpu(get_log->mcast_rx_frame);
237 		stats->fcs_error = le32_to_cpu(get_log->fcs_error);
238 		stats->tx_frame = le32_to_cpu(get_log->tx_frame);
239 		stats->wep_icv_error[0] =
240 			le32_to_cpu(get_log->wep_icv_err_cnt[0]);
241 		stats->wep_icv_error[1] =
242 			le32_to_cpu(get_log->wep_icv_err_cnt[1]);
243 		stats->wep_icv_error[2] =
244 			le32_to_cpu(get_log->wep_icv_err_cnt[2]);
245 		stats->wep_icv_error[3] =
246 			le32_to_cpu(get_log->wep_icv_err_cnt[3]);
247 		stats->bcn_rcv_cnt = le32_to_cpu(get_log->bcn_rcv_cnt);
248 		stats->bcn_miss_cnt = le32_to_cpu(get_log->bcn_miss_cnt);
249 	}
250 
251 	return 0;
252 }
253 
254 /*
255  * This function handles the command response of set/get Tx rate
256  * configurations.
257  *
258  * Handling includes changing the header fields into CPU format
259  * and saving the following parameters in driver -
260  *      - DSSS rate bitmap
261  *      - OFDM rate bitmap
262  *      - HT MCS rate bitmaps
263  *
264  * Based on the new rate bitmaps, the function re-evaluates if
265  * auto data rate has been activated. If not, it sends another
266  * query to the firmware to get the current Tx data rate.
267  */
mwifiex_ret_tx_rate_cfg(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)268 static int mwifiex_ret_tx_rate_cfg(struct mwifiex_private *priv,
269 				   struct host_cmd_ds_command *resp)
270 {
271 	struct host_cmd_ds_tx_rate_cfg *rate_cfg = &resp->params.tx_rate_cfg;
272 	struct mwifiex_rate_scope *rate_scope;
273 	struct mwifiex_ie_types_header *head;
274 	u16 tlv, tlv_buf_len, tlv_buf_left;
275 	u8 *tlv_buf;
276 	u32 i;
277 
278 	tlv_buf = ((u8 *)rate_cfg) + sizeof(struct host_cmd_ds_tx_rate_cfg);
279 	tlv_buf_left = le16_to_cpu(resp->size) - S_DS_GEN - sizeof(*rate_cfg);
280 
281 	while (tlv_buf_left >= sizeof(*head)) {
282 		head = (struct mwifiex_ie_types_header *)tlv_buf;
283 		tlv = le16_to_cpu(head->type);
284 		tlv_buf_len = le16_to_cpu(head->len);
285 
286 		if (tlv_buf_left < (sizeof(*head) + tlv_buf_len))
287 			break;
288 
289 		switch (tlv) {
290 		case TLV_TYPE_RATE_SCOPE:
291 			rate_scope = (struct mwifiex_rate_scope *) tlv_buf;
292 			priv->bitmap_rates[0] =
293 				le16_to_cpu(rate_scope->hr_dsss_rate_bitmap);
294 			priv->bitmap_rates[1] =
295 				le16_to_cpu(rate_scope->ofdm_rate_bitmap);
296 			for (i = 0;
297 			     i < ARRAY_SIZE(rate_scope->ht_mcs_rate_bitmap);
298 			     i++)
299 				priv->bitmap_rates[2 + i] =
300 					le16_to_cpu(rate_scope->
301 						    ht_mcs_rate_bitmap[i]);
302 
303 			if (priv->adapter->fw_api_ver == MWIFIEX_FW_V15) {
304 				for (i = 0; i < ARRAY_SIZE(rate_scope->
305 							   vht_mcs_rate_bitmap);
306 				     i++)
307 					priv->bitmap_rates[10 + i] =
308 					    le16_to_cpu(rate_scope->
309 							vht_mcs_rate_bitmap[i]);
310 			}
311 			break;
312 			/* Add RATE_DROP tlv here */
313 		}
314 
315 		tlv_buf += (sizeof(*head) + tlv_buf_len);
316 		tlv_buf_left -= (sizeof(*head) + tlv_buf_len);
317 	}
318 
319 	priv->is_data_rate_auto = mwifiex_is_rate_auto(priv);
320 
321 	if (priv->is_data_rate_auto)
322 		priv->data_rate = 0;
323 	else
324 		return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_TX_RATE_QUERY,
325 					HostCmd_ACT_GEN_GET, 0, NULL, false);
326 
327 	return 0;
328 }
329 
330 /*
331  * This function handles the command response of get Tx power level.
332  *
333  * Handling includes saving the maximum and minimum Tx power levels
334  * in driver, as well as sending the values to user.
335  */
mwifiex_get_power_level(struct mwifiex_private * priv,void * data_buf)336 static int mwifiex_get_power_level(struct mwifiex_private *priv, void *data_buf)
337 {
338 	int length, max_power = -1, min_power = -1;
339 	struct mwifiex_types_power_group *pg_tlv_hdr;
340 	struct mwifiex_power_group *pg;
341 
342 	if (!data_buf)
343 		return -1;
344 
345 	pg_tlv_hdr = (struct mwifiex_types_power_group *)((u8 *)data_buf);
346 	pg = (struct mwifiex_power_group *)
347 		((u8 *) pg_tlv_hdr + sizeof(struct mwifiex_types_power_group));
348 	length = le16_to_cpu(pg_tlv_hdr->length);
349 
350 	/* At least one structure required to update power */
351 	if (length < sizeof(struct mwifiex_power_group))
352 		return 0;
353 
354 	max_power = pg->power_max;
355 	min_power = pg->power_min;
356 	length -= sizeof(struct mwifiex_power_group);
357 
358 	while (length >= sizeof(struct mwifiex_power_group)) {
359 		pg++;
360 		if (max_power < pg->power_max)
361 			max_power = pg->power_max;
362 
363 		if (min_power > pg->power_min)
364 			min_power = pg->power_min;
365 
366 		length -= sizeof(struct mwifiex_power_group);
367 	}
368 	priv->min_tx_power_level = (u8) min_power;
369 	priv->max_tx_power_level = (u8) max_power;
370 
371 	return 0;
372 }
373 
374 /*
375  * This function handles the command response of set/get Tx power
376  * configurations.
377  *
378  * Handling includes changing the header fields into CPU format
379  * and saving the current Tx power level in driver.
380  */
mwifiex_ret_tx_power_cfg(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)381 static int mwifiex_ret_tx_power_cfg(struct mwifiex_private *priv,
382 				    struct host_cmd_ds_command *resp)
383 {
384 	struct mwifiex_adapter *adapter = priv->adapter;
385 	struct host_cmd_ds_txpwr_cfg *txp_cfg = &resp->params.txp_cfg;
386 	struct mwifiex_types_power_group *pg_tlv_hdr;
387 	struct mwifiex_power_group *pg;
388 	u16 action = le16_to_cpu(txp_cfg->action);
389 	u16 tlv_buf_left;
390 
391 	pg_tlv_hdr = (struct mwifiex_types_power_group *)
392 		((u8 *)txp_cfg +
393 		 sizeof(struct host_cmd_ds_txpwr_cfg));
394 
395 	pg = (struct mwifiex_power_group *)
396 		((u8 *)pg_tlv_hdr +
397 		 sizeof(struct mwifiex_types_power_group));
398 
399 	tlv_buf_left = le16_to_cpu(resp->size) - S_DS_GEN - sizeof(*txp_cfg);
400 	if (tlv_buf_left <
401 			le16_to_cpu(pg_tlv_hdr->length) + sizeof(*pg_tlv_hdr))
402 		return 0;
403 
404 	switch (action) {
405 	case HostCmd_ACT_GEN_GET:
406 		if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
407 			mwifiex_get_power_level(priv, pg_tlv_hdr);
408 
409 		priv->tx_power_level = (u16) pg->power_min;
410 		break;
411 
412 	case HostCmd_ACT_GEN_SET:
413 		if (!le32_to_cpu(txp_cfg->mode))
414 			break;
415 
416 		if (pg->power_max == pg->power_min)
417 			priv->tx_power_level = (u16) pg->power_min;
418 		break;
419 	default:
420 		mwifiex_dbg(adapter, ERROR,
421 			    "CMD_RESP: unknown cmd action %d\n",
422 			    action);
423 		return 0;
424 	}
425 	mwifiex_dbg(adapter, INFO,
426 		    "info: Current TxPower Level = %d, Max Power=%d, Min Power=%d\n",
427 		    priv->tx_power_level, priv->max_tx_power_level,
428 		    priv->min_tx_power_level);
429 
430 	return 0;
431 }
432 
433 /*
434  * This function handles the command response of get RF Tx power.
435  */
mwifiex_ret_rf_tx_power(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)436 static int mwifiex_ret_rf_tx_power(struct mwifiex_private *priv,
437 				   struct host_cmd_ds_command *resp)
438 {
439 	struct host_cmd_ds_rf_tx_pwr *txp = &resp->params.txp;
440 	u16 action = le16_to_cpu(txp->action);
441 
442 	priv->tx_power_level = le16_to_cpu(txp->cur_level);
443 
444 	if (action == HostCmd_ACT_GEN_GET) {
445 		priv->max_tx_power_level = txp->max_power;
446 		priv->min_tx_power_level = txp->min_power;
447 	}
448 
449 	mwifiex_dbg(priv->adapter, INFO,
450 		    "Current TxPower Level=%d, Max Power=%d, Min Power=%d\n",
451 		    priv->tx_power_level, priv->max_tx_power_level,
452 		    priv->min_tx_power_level);
453 
454 	return 0;
455 }
456 
457 /*
458  * This function handles the command response of set rf antenna
459  */
mwifiex_ret_rf_antenna(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)460 static int mwifiex_ret_rf_antenna(struct mwifiex_private *priv,
461 				  struct host_cmd_ds_command *resp)
462 {
463 	struct host_cmd_ds_rf_ant_mimo *ant_mimo = &resp->params.ant_mimo;
464 	struct host_cmd_ds_rf_ant_siso *ant_siso = &resp->params.ant_siso;
465 	struct mwifiex_adapter *adapter = priv->adapter;
466 
467 	if (adapter->hw_dev_mcs_support == HT_STREAM_2X2) {
468 		priv->tx_ant = le16_to_cpu(ant_mimo->tx_ant_mode);
469 		priv->rx_ant = le16_to_cpu(ant_mimo->rx_ant_mode);
470 		mwifiex_dbg(adapter, INFO,
471 			    "RF_ANT_RESP: Tx action = 0x%x, Tx Mode = 0x%04x\t"
472 			    "Rx action = 0x%x, Rx Mode = 0x%04x\n",
473 			    le16_to_cpu(ant_mimo->action_tx),
474 			    le16_to_cpu(ant_mimo->tx_ant_mode),
475 			    le16_to_cpu(ant_mimo->action_rx),
476 			    le16_to_cpu(ant_mimo->rx_ant_mode));
477 	} else {
478 		priv->tx_ant = le16_to_cpu(ant_siso->ant_mode);
479 		priv->rx_ant = le16_to_cpu(ant_siso->ant_mode);
480 		mwifiex_dbg(adapter, INFO,
481 			    "RF_ANT_RESP: action = 0x%x, Mode = 0x%04x\n",
482 			    le16_to_cpu(ant_siso->action),
483 			    le16_to_cpu(ant_siso->ant_mode));
484 	}
485 	return 0;
486 }
487 
488 /*
489  * This function handles the command response of set/get MAC address.
490  *
491  * Handling includes saving the MAC address in driver.
492  */
mwifiex_ret_802_11_mac_address(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)493 static int mwifiex_ret_802_11_mac_address(struct mwifiex_private *priv,
494 					  struct host_cmd_ds_command *resp)
495 {
496 	struct host_cmd_ds_802_11_mac_address *cmd_mac_addr =
497 							&resp->params.mac_addr;
498 
499 	memcpy(priv->curr_addr, cmd_mac_addr->mac_addr, ETH_ALEN);
500 
501 	mwifiex_dbg(priv->adapter, INFO,
502 		    "info: set mac address: %pM\n", priv->curr_addr);
503 
504 	return 0;
505 }
506 
507 /*
508  * This function handles the command response of set/get MAC multicast
509  * address.
510  */
mwifiex_ret_mac_multicast_adr(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)511 static int mwifiex_ret_mac_multicast_adr(struct mwifiex_private *priv,
512 					 struct host_cmd_ds_command *resp)
513 {
514 	return 0;
515 }
516 
517 /*
518  * This function handles the command response of get Tx rate query.
519  *
520  * Handling includes changing the header fields into CPU format
521  * and saving the Tx rate and HT information parameters in driver.
522  *
523  * Both rate configuration and current data rate can be retrieved
524  * with this request.
525  */
mwifiex_ret_802_11_tx_rate_query(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)526 static int mwifiex_ret_802_11_tx_rate_query(struct mwifiex_private *priv,
527 					    struct host_cmd_ds_command *resp)
528 {
529 	priv->tx_rate = resp->params.tx_rate.tx_rate;
530 	priv->tx_htinfo = resp->params.tx_rate.ht_info;
531 	if (!priv->is_data_rate_auto)
532 		priv->data_rate =
533 			mwifiex_index_to_data_rate(priv, priv->tx_rate,
534 						   priv->tx_htinfo);
535 
536 	return 0;
537 }
538 
539 /*
540  * This function handles the command response of a deauthenticate
541  * command.
542  *
543  * If the deauthenticated MAC matches the current BSS MAC, the connection
544  * state is reset.
545  */
mwifiex_ret_802_11_deauthenticate(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)546 static int mwifiex_ret_802_11_deauthenticate(struct mwifiex_private *priv,
547 					     struct host_cmd_ds_command *resp)
548 {
549 	struct mwifiex_adapter *adapter = priv->adapter;
550 
551 	adapter->dbg.num_cmd_deauth++;
552 	if (!memcmp(resp->params.deauth.mac_addr,
553 		    &priv->curr_bss_params.bss_descriptor.mac_address,
554 		    sizeof(resp->params.deauth.mac_addr)))
555 		mwifiex_reset_connect_state(priv, WLAN_REASON_DEAUTH_LEAVING,
556 					    false);
557 
558 	return 0;
559 }
560 
561 /*
562  * This function handles the command response of ad-hoc stop.
563  *
564  * The function resets the connection state in driver.
565  */
mwifiex_ret_802_11_ad_hoc_stop(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)566 static int mwifiex_ret_802_11_ad_hoc_stop(struct mwifiex_private *priv,
567 					  struct host_cmd_ds_command *resp)
568 {
569 	mwifiex_reset_connect_state(priv, WLAN_REASON_DEAUTH_LEAVING, false);
570 	return 0;
571 }
572 
573 /*
574  * This function handles the command response of set/get v1 key material.
575  *
576  * Handling includes updating the driver parameters to reflect the
577  * changes.
578  */
mwifiex_ret_802_11_key_material_v1(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)579 static int mwifiex_ret_802_11_key_material_v1(struct mwifiex_private *priv,
580 					      struct host_cmd_ds_command *resp)
581 {
582 	struct host_cmd_ds_802_11_key_material *key =
583 						&resp->params.key_material;
584 	int len;
585 
586 	len = le16_to_cpu(key->key_param_set.key_len);
587 	if (len > sizeof(key->key_param_set.key))
588 		return -EINVAL;
589 
590 	if (le16_to_cpu(key->action) == HostCmd_ACT_GEN_SET) {
591 		if ((le16_to_cpu(key->key_param_set.key_info) & KEY_MCAST)) {
592 			mwifiex_dbg(priv->adapter, INFO,
593 				    "info: key: GTK is set\n");
594 			priv->wpa_is_gtk_set = true;
595 			priv->scan_block = false;
596 			priv->port_open = true;
597 		}
598 	}
599 
600 	memset(priv->aes_key.key_param_set.key, 0,
601 	       sizeof(key->key_param_set.key));
602 	priv->aes_key.key_param_set.key_len = cpu_to_le16(len);
603 	memcpy(priv->aes_key.key_param_set.key, key->key_param_set.key, len);
604 
605 	return 0;
606 }
607 
608 /*
609  * This function handles the command response of set/get v2 key material.
610  *
611  * Handling includes updating the driver parameters to reflect the
612  * changes.
613  */
mwifiex_ret_802_11_key_material_v2(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)614 static int mwifiex_ret_802_11_key_material_v2(struct mwifiex_private *priv,
615 					      struct host_cmd_ds_command *resp)
616 {
617 	struct host_cmd_ds_802_11_key_material_v2 *key_v2;
618 	int len;
619 
620 	key_v2 = &resp->params.key_material_v2;
621 
622 	len = le16_to_cpu(key_v2->key_param_set.key_params.aes.key_len);
623 	if (len > sizeof(key_v2->key_param_set.key_params.aes.key))
624 		return -EINVAL;
625 
626 	if (le16_to_cpu(key_v2->action) == HostCmd_ACT_GEN_SET) {
627 		if ((le16_to_cpu(key_v2->key_param_set.key_info) & KEY_MCAST)) {
628 			mwifiex_dbg(priv->adapter, INFO, "info: key: GTK is set\n");
629 			priv->wpa_is_gtk_set = true;
630 			priv->scan_block = false;
631 			priv->port_open = true;
632 		}
633 	}
634 
635 	if (key_v2->key_param_set.key_type != KEY_TYPE_ID_AES)
636 		return 0;
637 
638 	memset(priv->aes_key_v2.key_param_set.key_params.aes.key, 0,
639 	       sizeof(key_v2->key_param_set.key_params.aes.key));
640 	priv->aes_key_v2.key_param_set.key_params.aes.key_len =
641 				cpu_to_le16(len);
642 	memcpy(priv->aes_key_v2.key_param_set.key_params.aes.key,
643 	       key_v2->key_param_set.key_params.aes.key, len);
644 
645 	return 0;
646 }
647 
648 /* Wrapper function for processing response of key material command */
mwifiex_ret_802_11_key_material(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)649 static int mwifiex_ret_802_11_key_material(struct mwifiex_private *priv,
650 					   struct host_cmd_ds_command *resp)
651 {
652 	if (priv->adapter->key_api_major_ver == KEY_API_VER_MAJOR_V2)
653 		return mwifiex_ret_802_11_key_material_v2(priv, resp);
654 	else
655 		return mwifiex_ret_802_11_key_material_v1(priv, resp);
656 }
657 
658 /*
659  * This function handles the command response of get 11d domain information.
660  */
mwifiex_ret_802_11d_domain_info(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)661 static int mwifiex_ret_802_11d_domain_info(struct mwifiex_private *priv,
662 					   struct host_cmd_ds_command *resp)
663 {
664 	struct host_cmd_ds_802_11d_domain_info_rsp *domain_info =
665 		&resp->params.domain_info_resp;
666 	struct mwifiex_ietypes_domain_param_set *domain = &domain_info->domain;
667 	u16 action = le16_to_cpu(domain_info->action);
668 	u8 no_of_triplet;
669 
670 	no_of_triplet = (u8) ((le16_to_cpu(domain->header.len)
671 				- IEEE80211_COUNTRY_STRING_LEN)
672 			      / sizeof(struct ieee80211_country_ie_triplet));
673 
674 	mwifiex_dbg(priv->adapter, INFO,
675 		    "info: 11D Domain Info Resp: no_of_triplet=%d\n",
676 		    no_of_triplet);
677 
678 	if (no_of_triplet > MWIFIEX_MAX_TRIPLET_802_11D) {
679 		mwifiex_dbg(priv->adapter, FATAL,
680 			    "11D: invalid number of triplets %d returned\n",
681 			    no_of_triplet);
682 		return -1;
683 	}
684 
685 	switch (action) {
686 	case HostCmd_ACT_GEN_SET:  /* Proc Set Action */
687 		break;
688 	case HostCmd_ACT_GEN_GET:
689 		break;
690 	default:
691 		mwifiex_dbg(priv->adapter, ERROR,
692 			    "11D: invalid action:%d\n", domain_info->action);
693 		return -1;
694 	}
695 
696 	return 0;
697 }
698 
699 /*
700  * This function handles the command response of get extended version.
701  *
702  * Handling includes forming the extended version string and sending it
703  * to application.
704  */
mwifiex_ret_ver_ext(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,struct host_cmd_ds_version_ext * version_ext)705 static int mwifiex_ret_ver_ext(struct mwifiex_private *priv,
706 			       struct host_cmd_ds_command *resp,
707 			       struct host_cmd_ds_version_ext *version_ext)
708 {
709 	struct host_cmd_ds_version_ext *ver_ext = &resp->params.verext;
710 
711 	if (test_and_clear_bit(MWIFIEX_IS_REQUESTING_FW_VEREXT, &priv->adapter->work_flags)) {
712 		if (strncmp(ver_ext->version_str, "ChipRev:20, BB:9b(10.00), RF:40(21)",
713 			    MWIFIEX_VERSION_STR_LENGTH) == 0) {
714 			struct mwifiex_ds_auto_ds auto_ds = {
715 				.auto_ds = DEEP_SLEEP_OFF,
716 			};
717 
718 			mwifiex_dbg(priv->adapter, MSG,
719 				    "Bad HW revision detected, disabling deep sleep\n");
720 
721 			if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
722 					     DIS_AUTO_PS, BITMAP_AUTO_DS, &auto_ds, false)) {
723 				mwifiex_dbg(priv->adapter, MSG,
724 					    "Disabling deep sleep failed.\n");
725 			}
726 		}
727 
728 		return 0;
729 	}
730 
731 	if (version_ext) {
732 		version_ext->version_str_sel = ver_ext->version_str_sel;
733 		memcpy(version_ext->version_str, ver_ext->version_str,
734 		       MWIFIEX_VERSION_STR_LENGTH);
735 		memcpy(priv->version_str, ver_ext->version_str,
736 		       MWIFIEX_VERSION_STR_LENGTH);
737 
738 		/* Ensure the version string from the firmware is 0-terminated */
739 		priv->version_str[MWIFIEX_VERSION_STR_LENGTH - 1] = '\0';
740 	}
741 	return 0;
742 }
743 
744 /*
745  * This function handles the command response of remain on channel.
746  */
747 static int
mwifiex_ret_remain_on_chan(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,struct host_cmd_ds_remain_on_chan * roc_cfg)748 mwifiex_ret_remain_on_chan(struct mwifiex_private *priv,
749 			   struct host_cmd_ds_command *resp,
750 			   struct host_cmd_ds_remain_on_chan *roc_cfg)
751 {
752 	struct host_cmd_ds_remain_on_chan *resp_cfg = &resp->params.roc_cfg;
753 
754 	if (roc_cfg)
755 		memcpy(roc_cfg, resp_cfg, sizeof(*roc_cfg));
756 
757 	return 0;
758 }
759 
760 /*
761  * This function handles the command response of P2P mode cfg.
762  */
763 static int
mwifiex_ret_p2p_mode_cfg(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,void * data_buf)764 mwifiex_ret_p2p_mode_cfg(struct mwifiex_private *priv,
765 			 struct host_cmd_ds_command *resp,
766 			 void *data_buf)
767 {
768 	struct host_cmd_ds_p2p_mode_cfg *mode_cfg = &resp->params.mode_cfg;
769 
770 	if (data_buf)
771 		put_unaligned_le16(le16_to_cpu(mode_cfg->mode), data_buf);
772 
773 	return 0;
774 }
775 
776 /* This function handles the command response of mem_access command
777  */
778 static int
mwifiex_ret_mem_access(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,void * pioctl_buf)779 mwifiex_ret_mem_access(struct mwifiex_private *priv,
780 		       struct host_cmd_ds_command *resp, void *pioctl_buf)
781 {
782 	struct host_cmd_ds_mem_access *mem = (void *)&resp->params.mem;
783 
784 	priv->mem_rw.addr = le32_to_cpu(mem->addr);
785 	priv->mem_rw.value = le32_to_cpu(mem->value);
786 
787 	return 0;
788 }
789 /*
790  * This function handles the command response of register access.
791  *
792  * The register value and offset are returned to the user. For EEPROM
793  * access, the byte count is also returned.
794  */
mwifiex_ret_reg_access(u16 type,struct host_cmd_ds_command * resp,void * data_buf)795 static int mwifiex_ret_reg_access(u16 type, struct host_cmd_ds_command *resp,
796 				  void *data_buf)
797 {
798 	struct mwifiex_ds_reg_rw *reg_rw;
799 	struct mwifiex_ds_read_eeprom *eeprom;
800 	union reg {
801 		struct host_cmd_ds_mac_reg_access *mac;
802 		struct host_cmd_ds_bbp_reg_access *bbp;
803 		struct host_cmd_ds_rf_reg_access *rf;
804 		struct host_cmd_ds_pmic_reg_access *pmic;
805 		struct host_cmd_ds_802_11_eeprom_access *eeprom;
806 	} r;
807 
808 	if (!data_buf)
809 		return 0;
810 
811 	reg_rw = data_buf;
812 	eeprom = data_buf;
813 	switch (type) {
814 	case HostCmd_CMD_MAC_REG_ACCESS:
815 		r.mac = &resp->params.mac_reg;
816 		reg_rw->offset = (u32) le16_to_cpu(r.mac->offset);
817 		reg_rw->value = le32_to_cpu(r.mac->value);
818 		break;
819 	case HostCmd_CMD_BBP_REG_ACCESS:
820 		r.bbp = &resp->params.bbp_reg;
821 		reg_rw->offset = (u32) le16_to_cpu(r.bbp->offset);
822 		reg_rw->value = (u32) r.bbp->value;
823 		break;
824 
825 	case HostCmd_CMD_RF_REG_ACCESS:
826 		r.rf = &resp->params.rf_reg;
827 		reg_rw->offset = (u32) le16_to_cpu(r.rf->offset);
828 		reg_rw->value = (u32) r.bbp->value;
829 		break;
830 	case HostCmd_CMD_PMIC_REG_ACCESS:
831 		r.pmic = &resp->params.pmic_reg;
832 		reg_rw->offset = (u32) le16_to_cpu(r.pmic->offset);
833 		reg_rw->value = (u32) r.pmic->value;
834 		break;
835 	case HostCmd_CMD_CAU_REG_ACCESS:
836 		r.rf = &resp->params.rf_reg;
837 		reg_rw->offset = (u32) le16_to_cpu(r.rf->offset);
838 		reg_rw->value = (u32) r.rf->value;
839 		break;
840 	case HostCmd_CMD_802_11_EEPROM_ACCESS:
841 		r.eeprom = &resp->params.eeprom;
842 		pr_debug("info: EEPROM read len=%x\n",
843 				le16_to_cpu(r.eeprom->byte_count));
844 		if (eeprom->byte_count < le16_to_cpu(r.eeprom->byte_count)) {
845 			eeprom->byte_count = 0;
846 			pr_debug("info: EEPROM read length is too big\n");
847 			return -1;
848 		}
849 		eeprom->offset = le16_to_cpu(r.eeprom->offset);
850 		eeprom->byte_count = le16_to_cpu(r.eeprom->byte_count);
851 		if (eeprom->byte_count > 0)
852 			memcpy(&eeprom->value, &r.eeprom->value,
853 			       min((u16)MAX_EEPROM_DATA, eeprom->byte_count));
854 		break;
855 	default:
856 		return -1;
857 	}
858 	return 0;
859 }
860 
861 /*
862  * This function handles the command response of get IBSS coalescing status.
863  *
864  * If the received BSSID is different than the current one, the current BSSID,
865  * beacon interval, ATIM window and ERP information are updated, along with
866  * changing the ad-hoc state accordingly.
867  */
mwifiex_ret_ibss_coalescing_status(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)868 static int mwifiex_ret_ibss_coalescing_status(struct mwifiex_private *priv,
869 					      struct host_cmd_ds_command *resp)
870 {
871 	struct host_cmd_ds_802_11_ibss_status *ibss_coal_resp =
872 					&(resp->params.ibss_coalescing);
873 
874 	if (le16_to_cpu(ibss_coal_resp->action) == HostCmd_ACT_GEN_SET)
875 		return 0;
876 
877 	mwifiex_dbg(priv->adapter, INFO,
878 		    "info: new BSSID %pM\n", ibss_coal_resp->bssid);
879 
880 	/* If rsp has NULL BSSID, Just return..... No Action */
881 	if (is_zero_ether_addr(ibss_coal_resp->bssid)) {
882 		mwifiex_dbg(priv->adapter, FATAL, "new BSSID is NULL\n");
883 		return 0;
884 	}
885 
886 	/* If BSSID is diff, modify current BSS parameters */
887 	if (!ether_addr_equal(priv->curr_bss_params.bss_descriptor.mac_address, ibss_coal_resp->bssid)) {
888 		/* BSSID */
889 		memcpy(priv->curr_bss_params.bss_descriptor.mac_address,
890 		       ibss_coal_resp->bssid, ETH_ALEN);
891 
892 		/* Beacon Interval */
893 		priv->curr_bss_params.bss_descriptor.beacon_period
894 			= le16_to_cpu(ibss_coal_resp->beacon_interval);
895 
896 		/* ERP Information */
897 		priv->curr_bss_params.bss_descriptor.erp_flags =
898 			(u8) le16_to_cpu(ibss_coal_resp->use_g_rate_protect);
899 
900 		priv->adhoc_state = ADHOC_COALESCED;
901 	}
902 
903 	return 0;
904 }
mwifiex_ret_tdls_oper(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)905 static int mwifiex_ret_tdls_oper(struct mwifiex_private *priv,
906 				 struct host_cmd_ds_command *resp)
907 {
908 	struct host_cmd_ds_tdls_oper *cmd_tdls_oper = &resp->params.tdls_oper;
909 	u16 reason = le16_to_cpu(cmd_tdls_oper->reason);
910 	u16 action = le16_to_cpu(cmd_tdls_oper->tdls_action);
911 	struct mwifiex_sta_node *node =
912 			   mwifiex_get_sta_entry(priv, cmd_tdls_oper->peer_mac);
913 
914 	switch (action) {
915 	case ACT_TDLS_DELETE:
916 		if (reason) {
917 			if (!node || reason == TDLS_ERR_LINK_NONEXISTENT)
918 				mwifiex_dbg(priv->adapter, MSG,
919 					    "TDLS link delete for %pM failed: reason %d\n",
920 					    cmd_tdls_oper->peer_mac, reason);
921 			else
922 				mwifiex_dbg(priv->adapter, ERROR,
923 					    "TDLS link delete for %pM failed: reason %d\n",
924 					    cmd_tdls_oper->peer_mac, reason);
925 		} else {
926 			mwifiex_dbg(priv->adapter, MSG,
927 				    "TDLS link delete for %pM successful\n",
928 				    cmd_tdls_oper->peer_mac);
929 		}
930 		break;
931 	case ACT_TDLS_CREATE:
932 		if (reason) {
933 			mwifiex_dbg(priv->adapter, ERROR,
934 				    "TDLS link creation for %pM failed: reason %d",
935 				    cmd_tdls_oper->peer_mac, reason);
936 			if (node && reason != TDLS_ERR_LINK_EXISTS)
937 				node->tdls_status = TDLS_SETUP_FAILURE;
938 		} else {
939 			mwifiex_dbg(priv->adapter, MSG,
940 				    "TDLS link creation for %pM successful",
941 				    cmd_tdls_oper->peer_mac);
942 		}
943 		break;
944 	case ACT_TDLS_CONFIG:
945 		if (reason) {
946 			mwifiex_dbg(priv->adapter, ERROR,
947 				    "TDLS link config for %pM failed, reason %d\n",
948 				    cmd_tdls_oper->peer_mac, reason);
949 			if (node)
950 				node->tdls_status = TDLS_SETUP_FAILURE;
951 		} else {
952 			mwifiex_dbg(priv->adapter, MSG,
953 				    "TDLS link config for %pM successful\n",
954 				    cmd_tdls_oper->peer_mac);
955 		}
956 		break;
957 	default:
958 		mwifiex_dbg(priv->adapter, ERROR,
959 			    "Unknown TDLS command action response %d", action);
960 		return -1;
961 	}
962 
963 	return 0;
964 }
965 /*
966  * This function handles the command response for subscribe event command.
967  */
mwifiex_ret_subsc_evt(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)968 static int mwifiex_ret_subsc_evt(struct mwifiex_private *priv,
969 				 struct host_cmd_ds_command *resp)
970 {
971 	struct host_cmd_ds_802_11_subsc_evt *cmd_sub_event =
972 		&resp->params.subsc_evt;
973 
974 	/* For every subscribe event command (Get/Set/Clear), FW reports the
975 	 * current set of subscribed events*/
976 	mwifiex_dbg(priv->adapter, EVENT,
977 		    "Bitmap of currently subscribed events: %16x\n",
978 		    le16_to_cpu(cmd_sub_event->events));
979 
980 	return 0;
981 }
982 
mwifiex_ret_uap_sta_list(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)983 static int mwifiex_ret_uap_sta_list(struct mwifiex_private *priv,
984 				    struct host_cmd_ds_command *resp)
985 {
986 	struct host_cmd_ds_sta_list *sta_list =
987 		&resp->params.sta_list;
988 	struct mwifiex_ie_types_sta_info *sta_info = (void *)&sta_list->tlv;
989 	int i;
990 	struct mwifiex_sta_node *sta_node;
991 
992 	for (i = 0; i < (le16_to_cpu(sta_list->sta_count)); i++) {
993 		sta_node = mwifiex_get_sta_entry(priv, sta_info->mac);
994 		if (unlikely(!sta_node))
995 			continue;
996 
997 		sta_node->stats.rssi = sta_info->rssi;
998 		sta_info++;
999 	}
1000 
1001 	return 0;
1002 }
1003 
1004 /* This function handles the command response of set_cfg_data */
mwifiex_ret_cfg_data(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)1005 static int mwifiex_ret_cfg_data(struct mwifiex_private *priv,
1006 				struct host_cmd_ds_command *resp)
1007 {
1008 	if (resp->result != HostCmd_RESULT_OK) {
1009 		mwifiex_dbg(priv->adapter, ERROR, "Cal data cmd resp failed\n");
1010 		return -1;
1011 	}
1012 
1013 	return 0;
1014 }
1015 
1016 /** This Function handles the command response of sdio rx aggr */
mwifiex_ret_sdio_rx_aggr_cfg(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)1017 static int mwifiex_ret_sdio_rx_aggr_cfg(struct mwifiex_private *priv,
1018 					struct host_cmd_ds_command *resp)
1019 {
1020 	struct mwifiex_adapter *adapter = priv->adapter;
1021 	struct host_cmd_sdio_sp_rx_aggr_cfg *cfg =
1022 				&resp->params.sdio_rx_aggr_cfg;
1023 
1024 	adapter->sdio_rx_aggr_enable = cfg->enable;
1025 	adapter->sdio_rx_block_size = le16_to_cpu(cfg->block_size);
1026 
1027 	return 0;
1028 }
1029 
mwifiex_ret_robust_coex(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,bool * is_timeshare)1030 static int mwifiex_ret_robust_coex(struct mwifiex_private *priv,
1031 				   struct host_cmd_ds_command *resp,
1032 				   bool *is_timeshare)
1033 {
1034 	struct host_cmd_ds_robust_coex *coex = &resp->params.coex;
1035 	struct mwifiex_ie_types_robust_coex *coex_tlv;
1036 	u16 action = le16_to_cpu(coex->action);
1037 	u32 mode;
1038 
1039 	coex_tlv = (struct mwifiex_ie_types_robust_coex
1040 		    *)((u8 *)coex + sizeof(struct host_cmd_ds_robust_coex));
1041 	if (action == HostCmd_ACT_GEN_GET) {
1042 		mode = le32_to_cpu(coex_tlv->mode);
1043 		if (mode == MWIFIEX_COEX_MODE_TIMESHARE)
1044 			*is_timeshare = true;
1045 		else
1046 			*is_timeshare = false;
1047 	}
1048 
1049 	return 0;
1050 }
1051 
1052 static struct ieee80211_regdomain *
mwifiex_create_custom_regdomain(struct mwifiex_private * priv,u8 * buf,u16 buf_len)1053 mwifiex_create_custom_regdomain(struct mwifiex_private *priv,
1054 				u8 *buf, u16 buf_len)
1055 {
1056 	u16 num_chan = buf_len / 2;
1057 	struct ieee80211_regdomain *regd;
1058 	struct ieee80211_reg_rule *rule;
1059 	bool new_rule;
1060 	int idx, freq, prev_freq = 0;
1061 	u32 bw, prev_bw = 0;
1062 	u8 chflags, prev_chflags = 0, valid_rules = 0;
1063 
1064 	if (WARN_ON_ONCE(num_chan > NL80211_MAX_SUPP_REG_RULES))
1065 		return ERR_PTR(-EINVAL);
1066 
1067 	regd = kzalloc(struct_size(regd, reg_rules, num_chan), GFP_KERNEL);
1068 	if (!regd)
1069 		return ERR_PTR(-ENOMEM);
1070 
1071 	for (idx = 0; idx < num_chan; idx++) {
1072 		u8 chan;
1073 		enum nl80211_band band;
1074 
1075 		chan = *buf++;
1076 		if (!chan) {
1077 			kfree(regd);
1078 			return NULL;
1079 		}
1080 		chflags = *buf++;
1081 		band = (chan <= 14) ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
1082 		freq = ieee80211_channel_to_frequency(chan, band);
1083 		new_rule = false;
1084 
1085 		if (chflags & MWIFIEX_CHANNEL_DISABLED)
1086 			continue;
1087 
1088 		if (band == NL80211_BAND_5GHZ) {
1089 			if (!(chflags & MWIFIEX_CHANNEL_NOHT80))
1090 				bw = MHZ_TO_KHZ(80);
1091 			else if (!(chflags & MWIFIEX_CHANNEL_NOHT40))
1092 				bw = MHZ_TO_KHZ(40);
1093 			else
1094 				bw = MHZ_TO_KHZ(20);
1095 		} else {
1096 			if (!(chflags & MWIFIEX_CHANNEL_NOHT40))
1097 				bw = MHZ_TO_KHZ(40);
1098 			else
1099 				bw = MHZ_TO_KHZ(20);
1100 		}
1101 
1102 		if (idx == 0 || prev_chflags != chflags || prev_bw != bw ||
1103 		    freq - prev_freq > 20) {
1104 			valid_rules++;
1105 			new_rule = true;
1106 		}
1107 
1108 		rule = &regd->reg_rules[valid_rules - 1];
1109 
1110 		rule->freq_range.end_freq_khz = MHZ_TO_KHZ(freq + 10);
1111 
1112 		prev_chflags = chflags;
1113 		prev_freq = freq;
1114 		prev_bw = bw;
1115 
1116 		if (!new_rule)
1117 			continue;
1118 
1119 		rule->freq_range.start_freq_khz = MHZ_TO_KHZ(freq - 10);
1120 		rule->power_rule.max_eirp = DBM_TO_MBM(19);
1121 
1122 		if (chflags & MWIFIEX_CHANNEL_PASSIVE)
1123 			rule->flags = NL80211_RRF_NO_IR;
1124 
1125 		if (chflags & MWIFIEX_CHANNEL_DFS)
1126 			rule->flags = NL80211_RRF_DFS;
1127 
1128 		rule->freq_range.max_bandwidth_khz = bw;
1129 	}
1130 
1131 	regd->n_reg_rules = valid_rules;
1132 	regd->alpha2[0] = '9';
1133 	regd->alpha2[1] = '9';
1134 
1135 	return regd;
1136 }
1137 
mwifiex_ret_chan_region_cfg(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)1138 static int mwifiex_ret_chan_region_cfg(struct mwifiex_private *priv,
1139 				       struct host_cmd_ds_command *resp)
1140 {
1141 	struct host_cmd_ds_chan_region_cfg *reg = &resp->params.reg_cfg;
1142 	u16 action = le16_to_cpu(reg->action);
1143 	u16 tlv, tlv_buf_len, tlv_buf_left;
1144 	struct mwifiex_ie_types_header *head;
1145 	struct ieee80211_regdomain *regd;
1146 	u8 *tlv_buf;
1147 
1148 	if (action != HostCmd_ACT_GEN_GET)
1149 		return 0;
1150 
1151 	tlv_buf = (u8 *)reg + sizeof(*reg);
1152 	tlv_buf_left = le16_to_cpu(resp->size) - S_DS_GEN - sizeof(*reg);
1153 
1154 	while (tlv_buf_left >= sizeof(*head)) {
1155 		head = (struct mwifiex_ie_types_header *)tlv_buf;
1156 		tlv = le16_to_cpu(head->type);
1157 		tlv_buf_len = le16_to_cpu(head->len);
1158 
1159 		if (tlv_buf_left < (sizeof(*head) + tlv_buf_len))
1160 			break;
1161 
1162 		switch (tlv) {
1163 		case TLV_TYPE_CHAN_ATTR_CFG:
1164 			mwifiex_dbg_dump(priv->adapter, CMD_D, "CHAN:",
1165 					 (u8 *)head + sizeof(*head),
1166 					 tlv_buf_len);
1167 			regd = mwifiex_create_custom_regdomain(priv,
1168 				(u8 *)head + sizeof(*head), tlv_buf_len);
1169 			if (!IS_ERR(regd))
1170 				priv->adapter->regd = regd;
1171 			break;
1172 		}
1173 
1174 		tlv_buf += (sizeof(*head) + tlv_buf_len);
1175 		tlv_buf_left -= (sizeof(*head) + tlv_buf_len);
1176 	}
1177 
1178 	return 0;
1179 }
1180 
mwifiex_ret_pkt_aggr_ctrl(struct mwifiex_private * priv,struct host_cmd_ds_command * resp)1181 static int mwifiex_ret_pkt_aggr_ctrl(struct mwifiex_private *priv,
1182 				     struct host_cmd_ds_command *resp)
1183 {
1184 	struct host_cmd_ds_pkt_aggr_ctrl *pkt_aggr_ctrl =
1185 					&resp->params.pkt_aggr_ctrl;
1186 	struct mwifiex_adapter *adapter = priv->adapter;
1187 
1188 	adapter->bus_aggr.enable = le16_to_cpu(pkt_aggr_ctrl->enable);
1189 	if (adapter->bus_aggr.enable)
1190 		adapter->intf_hdr_len = INTF_HEADER_LEN;
1191 	adapter->bus_aggr.mode = MWIFIEX_BUS_AGGR_MODE_LEN_V2;
1192 	adapter->bus_aggr.tx_aggr_max_size =
1193 				le16_to_cpu(pkt_aggr_ctrl->tx_aggr_max_size);
1194 	adapter->bus_aggr.tx_aggr_max_num =
1195 				le16_to_cpu(pkt_aggr_ctrl->tx_aggr_max_num);
1196 	adapter->bus_aggr.tx_aggr_align =
1197 				le16_to_cpu(pkt_aggr_ctrl->tx_aggr_align);
1198 
1199 	return 0;
1200 }
1201 
mwifiex_ret_get_chan_info(struct mwifiex_private * priv,struct host_cmd_ds_command * resp,struct mwifiex_channel_band * channel_band)1202 static int mwifiex_ret_get_chan_info(struct mwifiex_private *priv,
1203 				     struct host_cmd_ds_command *resp,
1204 				     struct mwifiex_channel_band *channel_band)
1205 {
1206 	struct host_cmd_ds_sta_configure *sta_cfg_cmd = &resp->params.sta_cfg;
1207 	struct host_cmd_tlv_channel_band *tlv_band_channel;
1208 
1209 	tlv_band_channel =
1210 	(struct host_cmd_tlv_channel_band *)sta_cfg_cmd->tlv_buffer;
1211 	memcpy(&channel_band->band_config, &tlv_band_channel->band_config,
1212 	       sizeof(struct mwifiex_band_config));
1213 	channel_band->channel = tlv_band_channel->channel;
1214 
1215 	return 0;
1216 }
1217 
1218 /*
1219  * This function handles the command responses.
1220  *
1221  * This is a generic function, which calls command specific
1222  * response handlers based on the command ID.
1223  */
mwifiex_process_sta_cmdresp(struct mwifiex_private * priv,u16 cmdresp_no,struct host_cmd_ds_command * resp)1224 int mwifiex_process_sta_cmdresp(struct mwifiex_private *priv, u16 cmdresp_no,
1225 				struct host_cmd_ds_command *resp)
1226 {
1227 	int ret = 0;
1228 	struct mwifiex_adapter *adapter = priv->adapter;
1229 	void *data_buf = adapter->curr_cmd->data_buf;
1230 
1231 	/* If the command is not successful, cleanup and return failure */
1232 	if (resp->result != HostCmd_RESULT_OK) {
1233 		mwifiex_process_cmdresp_error(priv, resp);
1234 		return -1;
1235 	}
1236 	/* Command successful, handle response */
1237 	switch (cmdresp_no) {
1238 	case HostCmd_CMD_GET_HW_SPEC:
1239 		ret = mwifiex_ret_get_hw_spec(priv, resp);
1240 		break;
1241 	case HostCmd_CMD_CFG_DATA:
1242 		ret = mwifiex_ret_cfg_data(priv, resp);
1243 		break;
1244 	case HostCmd_CMD_MAC_CONTROL:
1245 		break;
1246 	case HostCmd_CMD_802_11_MAC_ADDRESS:
1247 		ret = mwifiex_ret_802_11_mac_address(priv, resp);
1248 		break;
1249 	case HostCmd_CMD_MAC_MULTICAST_ADR:
1250 		ret = mwifiex_ret_mac_multicast_adr(priv, resp);
1251 		break;
1252 	case HostCmd_CMD_TX_RATE_CFG:
1253 		ret = mwifiex_ret_tx_rate_cfg(priv, resp);
1254 		break;
1255 	case HostCmd_CMD_802_11_SCAN:
1256 		ret = mwifiex_ret_802_11_scan(priv, resp);
1257 		adapter->curr_cmd->wait_q_enabled = false;
1258 		break;
1259 	case HostCmd_CMD_802_11_SCAN_EXT:
1260 		ret = mwifiex_ret_802_11_scan_ext(priv, resp);
1261 		adapter->curr_cmd->wait_q_enabled = false;
1262 		break;
1263 	case HostCmd_CMD_802_11_BG_SCAN_QUERY:
1264 		ret = mwifiex_ret_802_11_scan(priv, resp);
1265 		cfg80211_sched_scan_results(priv->wdev.wiphy, 0);
1266 		mwifiex_dbg(adapter, CMD,
1267 			    "info: CMD_RESP: BG_SCAN result is ready!\n");
1268 		break;
1269 	case HostCmd_CMD_802_11_BG_SCAN_CONFIG:
1270 		break;
1271 	case HostCmd_CMD_TXPWR_CFG:
1272 		ret = mwifiex_ret_tx_power_cfg(priv, resp);
1273 		break;
1274 	case HostCmd_CMD_RF_TX_PWR:
1275 		ret = mwifiex_ret_rf_tx_power(priv, resp);
1276 		break;
1277 	case HostCmd_CMD_RF_ANTENNA:
1278 		ret = mwifiex_ret_rf_antenna(priv, resp);
1279 		break;
1280 	case HostCmd_CMD_802_11_PS_MODE_ENH:
1281 		ret = mwifiex_ret_enh_power_mode(priv, resp, data_buf);
1282 		break;
1283 	case HostCmd_CMD_802_11_HS_CFG_ENH:
1284 		ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
1285 		break;
1286 	case HostCmd_CMD_802_11_ASSOCIATE:
1287 		ret = mwifiex_ret_802_11_associate(priv, resp);
1288 		break;
1289 	case HostCmd_CMD_802_11_DEAUTHENTICATE:
1290 		ret = mwifiex_ret_802_11_deauthenticate(priv, resp);
1291 		break;
1292 	case HostCmd_CMD_802_11_AD_HOC_START:
1293 	case HostCmd_CMD_802_11_AD_HOC_JOIN:
1294 		ret = mwifiex_ret_802_11_ad_hoc(priv, resp);
1295 		break;
1296 	case HostCmd_CMD_802_11_AD_HOC_STOP:
1297 		ret = mwifiex_ret_802_11_ad_hoc_stop(priv, resp);
1298 		break;
1299 	case HostCmd_CMD_802_11_GET_LOG:
1300 		ret = mwifiex_ret_get_log(priv, resp, data_buf);
1301 		break;
1302 	case HostCmd_CMD_RSSI_INFO:
1303 		ret = mwifiex_ret_802_11_rssi_info(priv, resp);
1304 		break;
1305 	case HostCmd_CMD_802_11_SNMP_MIB:
1306 		ret = mwifiex_ret_802_11_snmp_mib(priv, resp, data_buf);
1307 		break;
1308 	case HostCmd_CMD_802_11_TX_RATE_QUERY:
1309 		ret = mwifiex_ret_802_11_tx_rate_query(priv, resp);
1310 		break;
1311 	case HostCmd_CMD_VERSION_EXT:
1312 		ret = mwifiex_ret_ver_ext(priv, resp, data_buf);
1313 		break;
1314 	case HostCmd_CMD_REMAIN_ON_CHAN:
1315 		ret = mwifiex_ret_remain_on_chan(priv, resp, data_buf);
1316 		break;
1317 	case HostCmd_CMD_11AC_CFG:
1318 		break;
1319 	case HostCmd_CMD_PACKET_AGGR_CTRL:
1320 		ret = mwifiex_ret_pkt_aggr_ctrl(priv, resp);
1321 		break;
1322 	case HostCmd_CMD_P2P_MODE_CFG:
1323 		ret = mwifiex_ret_p2p_mode_cfg(priv, resp, data_buf);
1324 		break;
1325 	case HostCmd_CMD_MGMT_FRAME_REG:
1326 	case HostCmd_CMD_FUNC_INIT:
1327 	case HostCmd_CMD_FUNC_SHUTDOWN:
1328 		break;
1329 	case HostCmd_CMD_802_11_KEY_MATERIAL:
1330 		ret = mwifiex_ret_802_11_key_material(priv, resp);
1331 		break;
1332 	case HostCmd_CMD_802_11D_DOMAIN_INFO:
1333 		ret = mwifiex_ret_802_11d_domain_info(priv, resp);
1334 		break;
1335 	case HostCmd_CMD_11N_ADDBA_REQ:
1336 		ret = mwifiex_ret_11n_addba_req(priv, resp);
1337 		break;
1338 	case HostCmd_CMD_11N_DELBA:
1339 		ret = mwifiex_ret_11n_delba(priv, resp);
1340 		break;
1341 	case HostCmd_CMD_11N_ADDBA_RSP:
1342 		ret = mwifiex_ret_11n_addba_resp(priv, resp);
1343 		break;
1344 	case HostCmd_CMD_RECONFIGURE_TX_BUFF:
1345 		if (0xffff == (u16)le16_to_cpu(resp->params.tx_buf.buff_size)) {
1346 			if (adapter->iface_type == MWIFIEX_USB &&
1347 			    adapter->usb_mc_setup) {
1348 				if (adapter->if_ops.multi_port_resync)
1349 					adapter->if_ops.
1350 						multi_port_resync(adapter);
1351 				adapter->usb_mc_setup = false;
1352 				adapter->tx_lock_flag = false;
1353 			}
1354 			break;
1355 		}
1356 		adapter->tx_buf_size = (u16) le16_to_cpu(resp->params.
1357 							     tx_buf.buff_size);
1358 		adapter->tx_buf_size = (adapter->tx_buf_size
1359 					/ MWIFIEX_SDIO_BLOCK_SIZE)
1360 				       * MWIFIEX_SDIO_BLOCK_SIZE;
1361 		adapter->curr_tx_buf_size = adapter->tx_buf_size;
1362 		mwifiex_dbg(adapter, CMD, "cmd: curr_tx_buf_size=%d\n",
1363 			    adapter->curr_tx_buf_size);
1364 
1365 		if (adapter->if_ops.update_mp_end_port)
1366 			adapter->if_ops.update_mp_end_port(adapter,
1367 				le16_to_cpu(resp->params.tx_buf.mp_end_port));
1368 		break;
1369 	case HostCmd_CMD_AMSDU_AGGR_CTRL:
1370 		break;
1371 	case HostCmd_CMD_WMM_GET_STATUS:
1372 		ret = mwifiex_ret_wmm_get_status(priv, resp);
1373 		break;
1374 	case HostCmd_CMD_802_11_IBSS_COALESCING_STATUS:
1375 		ret = mwifiex_ret_ibss_coalescing_status(priv, resp);
1376 		break;
1377 	case HostCmd_CMD_MEM_ACCESS:
1378 		ret = mwifiex_ret_mem_access(priv, resp, data_buf);
1379 		break;
1380 	case HostCmd_CMD_MAC_REG_ACCESS:
1381 	case HostCmd_CMD_BBP_REG_ACCESS:
1382 	case HostCmd_CMD_RF_REG_ACCESS:
1383 	case HostCmd_CMD_PMIC_REG_ACCESS:
1384 	case HostCmd_CMD_CAU_REG_ACCESS:
1385 	case HostCmd_CMD_802_11_EEPROM_ACCESS:
1386 		ret = mwifiex_ret_reg_access(cmdresp_no, resp, data_buf);
1387 		break;
1388 	case HostCmd_CMD_SET_BSS_MODE:
1389 		break;
1390 	case HostCmd_CMD_11N_CFG:
1391 		break;
1392 	case HostCmd_CMD_PCIE_DESC_DETAILS:
1393 		break;
1394 	case HostCmd_CMD_802_11_SUBSCRIBE_EVENT:
1395 		ret = mwifiex_ret_subsc_evt(priv, resp);
1396 		break;
1397 	case HostCmd_CMD_UAP_SYS_CONFIG:
1398 		break;
1399 	case HOST_CMD_APCMD_STA_LIST:
1400 		ret = mwifiex_ret_uap_sta_list(priv, resp);
1401 		break;
1402 	case HostCmd_CMD_UAP_BSS_START:
1403 		adapter->tx_lock_flag = false;
1404 		adapter->pps_uapsd_mode = false;
1405 		adapter->delay_null_pkt = false;
1406 		priv->bss_started = 1;
1407 		break;
1408 	case HostCmd_CMD_UAP_BSS_STOP:
1409 		priv->bss_started = 0;
1410 		break;
1411 	case HostCmd_CMD_UAP_STA_DEAUTH:
1412 		break;
1413 	case HOST_CMD_APCMD_SYS_RESET:
1414 		break;
1415 	case HostCmd_CMD_MEF_CFG:
1416 		break;
1417 	case HostCmd_CMD_COALESCE_CFG:
1418 		break;
1419 	case HostCmd_CMD_TDLS_OPER:
1420 		ret = mwifiex_ret_tdls_oper(priv, resp);
1421 		break;
1422 	case HostCmd_CMD_MC_POLICY:
1423 		break;
1424 	case HostCmd_CMD_CHAN_REPORT_REQUEST:
1425 		break;
1426 	case HostCmd_CMD_SDIO_SP_RX_AGGR_CFG:
1427 		ret = mwifiex_ret_sdio_rx_aggr_cfg(priv, resp);
1428 		break;
1429 	case HostCmd_CMD_HS_WAKEUP_REASON:
1430 		ret = mwifiex_ret_wakeup_reason(priv, resp, data_buf);
1431 		break;
1432 	case HostCmd_CMD_TDLS_CONFIG:
1433 		break;
1434 	case HostCmd_CMD_ROBUST_COEX:
1435 		ret = mwifiex_ret_robust_coex(priv, resp, data_buf);
1436 		break;
1437 	case HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG:
1438 		break;
1439 	case HostCmd_CMD_CHAN_REGION_CFG:
1440 		ret = mwifiex_ret_chan_region_cfg(priv, resp);
1441 		break;
1442 	case HostCmd_CMD_STA_CONFIGURE:
1443 		ret = mwifiex_ret_get_chan_info(priv, resp, data_buf);
1444 		break;
1445 	default:
1446 		mwifiex_dbg(adapter, ERROR,
1447 			    "CMD_RESP: unknown cmd response %#x\n",
1448 			    resp->command);
1449 		break;
1450 	}
1451 
1452 	return ret;
1453 }
1454