1 /*
2  * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/etherdevice.h>
20 #include <linux/firmware.h>
21 #include <linux/bitops.h>
22 #include "smd.h"
23 
24 struct wcn36xx_cfg_val {
25 	u32 cfg_id;
26 	u32 value;
27 };
28 
29 #define WCN36XX_CFG_VAL(id, val) \
30 { \
31 	.cfg_id = WCN36XX_HAL_CFG_ ## id, \
32 	.value = val \
33 }
34 
35 static struct wcn36xx_cfg_val wcn36xx_cfg_vals[] = {
36 	WCN36XX_CFG_VAL(CURRENT_TX_ANTENNA, 1),
37 	WCN36XX_CFG_VAL(CURRENT_RX_ANTENNA, 1),
38 	WCN36XX_CFG_VAL(LOW_GAIN_OVERRIDE, 0),
39 	WCN36XX_CFG_VAL(POWER_STATE_PER_CHAIN, 785),
40 	WCN36XX_CFG_VAL(CAL_PERIOD, 5),
41 	WCN36XX_CFG_VAL(CAL_CONTROL, 1),
42 	WCN36XX_CFG_VAL(PROXIMITY, 0),
43 	WCN36XX_CFG_VAL(NETWORK_DENSITY, 3),
44 	WCN36XX_CFG_VAL(MAX_MEDIUM_TIME, 6000),
45 	WCN36XX_CFG_VAL(MAX_MPDUS_IN_AMPDU, 64),
46 	WCN36XX_CFG_VAL(RTS_THRESHOLD, 2347),
47 	WCN36XX_CFG_VAL(SHORT_RETRY_LIMIT, 6),
48 	WCN36XX_CFG_VAL(LONG_RETRY_LIMIT, 6),
49 	WCN36XX_CFG_VAL(FRAGMENTATION_THRESHOLD, 8000),
50 	WCN36XX_CFG_VAL(DYNAMIC_THRESHOLD_ZERO, 5),
51 	WCN36XX_CFG_VAL(DYNAMIC_THRESHOLD_ONE, 10),
52 	WCN36XX_CFG_VAL(DYNAMIC_THRESHOLD_TWO, 15),
53 	WCN36XX_CFG_VAL(FIXED_RATE, 0),
54 	WCN36XX_CFG_VAL(RETRYRATE_POLICY, 4),
55 	WCN36XX_CFG_VAL(RETRYRATE_SECONDARY, 0),
56 	WCN36XX_CFG_VAL(RETRYRATE_TERTIARY, 0),
57 	WCN36XX_CFG_VAL(FORCE_POLICY_PROTECTION, 5),
58 	WCN36XX_CFG_VAL(FIXED_RATE_MULTICAST_24GHZ, 1),
59 	WCN36XX_CFG_VAL(FIXED_RATE_MULTICAST_5GHZ, 5),
60 	WCN36XX_CFG_VAL(DEFAULT_RATE_INDEX_5GHZ, 5),
61 	WCN36XX_CFG_VAL(MAX_BA_SESSIONS, 40),
62 	WCN36XX_CFG_VAL(PS_DATA_INACTIVITY_TIMEOUT, 200),
63 	WCN36XX_CFG_VAL(PS_ENABLE_BCN_FILTER, 1),
64 	WCN36XX_CFG_VAL(PS_ENABLE_RSSI_MONITOR, 1),
65 	WCN36XX_CFG_VAL(NUM_BEACON_PER_RSSI_AVERAGE, 20),
66 	WCN36XX_CFG_VAL(STATS_PERIOD, 10),
67 	WCN36XX_CFG_VAL(CFP_MAX_DURATION, 30000),
68 	WCN36XX_CFG_VAL(FRAME_TRANS_ENABLED, 0),
69 	WCN36XX_CFG_VAL(BA_THRESHOLD_HIGH, 128),
70 	WCN36XX_CFG_VAL(MAX_BA_BUFFERS, 2560),
71 	WCN36XX_CFG_VAL(DYNAMIC_PS_POLL_VALUE, 0),
72 	WCN36XX_CFG_VAL(TX_PWR_CTRL_ENABLE, 1),
73 	WCN36XX_CFG_VAL(ENABLE_CLOSE_LOOP, 1),
74 	WCN36XX_CFG_VAL(ENABLE_LPWR_IMG_TRANSITION, 0),
75 	WCN36XX_CFG_VAL(MAX_ASSOC_LIMIT, 10),
76 	WCN36XX_CFG_VAL(ENABLE_MCC_ADAPTIVE_SCHEDULER, 0),
77 };
78 
79 static int put_cfg_tlv_u32(struct wcn36xx *wcn, size_t *len, u32 id, u32 value)
80 {
81 	struct wcn36xx_hal_cfg *entry;
82 	u32 *val;
83 
84 	if (*len + sizeof(*entry) + sizeof(u32) >= WCN36XX_HAL_BUF_SIZE) {
85 		wcn36xx_err("Not enough room for TLV entry\n");
86 		return -ENOMEM;
87 	}
88 
89 	entry = (struct wcn36xx_hal_cfg *) (wcn->hal_buf + *len);
90 	entry->id = id;
91 	entry->len = sizeof(u32);
92 	entry->pad_bytes = 0;
93 	entry->reserve = 0;
94 
95 	val = (u32 *) (entry + 1);
96 	*val = value;
97 
98 	*len += sizeof(*entry) + sizeof(u32);
99 
100 	return 0;
101 }
102 
103 static void wcn36xx_smd_set_bss_nw_type(struct wcn36xx *wcn,
104 		struct ieee80211_sta *sta,
105 		struct wcn36xx_hal_config_bss_params *bss_params)
106 {
107 	if (NL80211_BAND_5GHZ == WCN36XX_BAND(wcn))
108 		bss_params->nw_type = WCN36XX_HAL_11A_NW_TYPE;
109 	else if (sta && sta->ht_cap.ht_supported)
110 		bss_params->nw_type = WCN36XX_HAL_11N_NW_TYPE;
111 	else if (sta && (sta->supp_rates[NL80211_BAND_2GHZ] & 0x7f))
112 		bss_params->nw_type = WCN36XX_HAL_11G_NW_TYPE;
113 	else
114 		bss_params->nw_type = WCN36XX_HAL_11B_NW_TYPE;
115 }
116 
117 static inline u8 is_cap_supported(unsigned long caps, unsigned long flag)
118 {
119 	return caps & flag ? 1 : 0;
120 }
121 static void wcn36xx_smd_set_bss_ht_params(struct ieee80211_vif *vif,
122 		struct ieee80211_sta *sta,
123 		struct wcn36xx_hal_config_bss_params *bss_params)
124 {
125 	if (sta && sta->ht_cap.ht_supported) {
126 		unsigned long caps = sta->ht_cap.cap;
127 		bss_params->ht = sta->ht_cap.ht_supported;
128 		bss_params->tx_channel_width_set = is_cap_supported(caps,
129 			IEEE80211_HT_CAP_SUP_WIDTH_20_40);
130 		bss_params->lsig_tx_op_protection_full_support =
131 			is_cap_supported(caps,
132 					 IEEE80211_HT_CAP_LSIG_TXOP_PROT);
133 
134 		bss_params->ht_oper_mode = vif->bss_conf.ht_operation_mode;
135 		bss_params->lln_non_gf_coexist =
136 			!!(vif->bss_conf.ht_operation_mode &
137 			   IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
138 		/* IEEE80211_HT_STBC_PARAM_DUAL_CTS_PROT */
139 		bss_params->dual_cts_protection = 0;
140 		/* IEEE80211_HT_OP_MODE_PROTECTION_20MHZ */
141 		bss_params->ht20_coexist = 0;
142 	}
143 }
144 
145 static void wcn36xx_smd_set_sta_ht_params(struct ieee80211_sta *sta,
146 		struct wcn36xx_hal_config_sta_params *sta_params)
147 {
148 	if (sta->ht_cap.ht_supported) {
149 		unsigned long caps = sta->ht_cap.cap;
150 		sta_params->ht_capable = sta->ht_cap.ht_supported;
151 		sta_params->tx_channel_width_set = is_cap_supported(caps,
152 			IEEE80211_HT_CAP_SUP_WIDTH_20_40);
153 		sta_params->lsig_txop_protection = is_cap_supported(caps,
154 			IEEE80211_HT_CAP_LSIG_TXOP_PROT);
155 
156 		sta_params->max_ampdu_size = sta->ht_cap.ampdu_factor;
157 		sta_params->max_ampdu_density = sta->ht_cap.ampdu_density;
158 		sta_params->max_amsdu_size = is_cap_supported(caps,
159 			IEEE80211_HT_CAP_MAX_AMSDU);
160 		sta_params->sgi_20Mhz = is_cap_supported(caps,
161 			IEEE80211_HT_CAP_SGI_20);
162 		sta_params->sgi_40mhz =	is_cap_supported(caps,
163 			IEEE80211_HT_CAP_SGI_40);
164 		sta_params->green_field_capable = is_cap_supported(caps,
165 			IEEE80211_HT_CAP_GRN_FLD);
166 		sta_params->delayed_ba_support = is_cap_supported(caps,
167 			IEEE80211_HT_CAP_DELAY_BA);
168 		sta_params->dsss_cck_mode_40mhz = is_cap_supported(caps,
169 			IEEE80211_HT_CAP_DSSSCCK40);
170 	}
171 }
172 
173 static void wcn36xx_smd_set_sta_default_ht_params(
174 		struct wcn36xx_hal_config_sta_params *sta_params)
175 {
176 	sta_params->ht_capable = 1;
177 	sta_params->tx_channel_width_set = 1;
178 	sta_params->lsig_txop_protection = 1;
179 	sta_params->max_ampdu_size = 3;
180 	sta_params->max_ampdu_density = 5;
181 	sta_params->max_amsdu_size = 0;
182 	sta_params->sgi_20Mhz = 1;
183 	sta_params->sgi_40mhz = 1;
184 	sta_params->green_field_capable = 1;
185 	sta_params->delayed_ba_support = 0;
186 	sta_params->dsss_cck_mode_40mhz = 1;
187 }
188 
189 static void wcn36xx_smd_set_sta_params(struct wcn36xx *wcn,
190 		struct ieee80211_vif *vif,
191 		struct ieee80211_sta *sta,
192 		struct wcn36xx_hal_config_sta_params *sta_params)
193 {
194 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
195 	struct wcn36xx_sta *sta_priv = NULL;
196 	if (vif->type == NL80211_IFTYPE_ADHOC ||
197 	    vif->type == NL80211_IFTYPE_AP ||
198 	    vif->type == NL80211_IFTYPE_MESH_POINT) {
199 		sta_params->type = 1;
200 		sta_params->sta_index = WCN36XX_HAL_STA_INVALID_IDX;
201 	} else {
202 		sta_params->type = 0;
203 		sta_params->sta_index = vif_priv->self_sta_index;
204 	}
205 
206 	sta_params->listen_interval = WCN36XX_LISTEN_INTERVAL(wcn);
207 
208 	/*
209 	 * In STA mode ieee80211_sta contains bssid and ieee80211_vif
210 	 * contains our mac address. In  AP mode we are bssid so vif
211 	 * contains bssid and ieee80211_sta contains mac.
212 	 */
213 	if (NL80211_IFTYPE_STATION == vif->type)
214 		memcpy(&sta_params->mac, vif->addr, ETH_ALEN);
215 	else
216 		memcpy(&sta_params->bssid, vif->addr, ETH_ALEN);
217 
218 	sta_params->encrypt_type = vif_priv->encrypt_type;
219 	sta_params->short_preamble_supported = true;
220 
221 	sta_params->rifs_mode = 0;
222 	sta_params->rmf = 0;
223 	sta_params->action = 0;
224 	sta_params->uapsd = 0;
225 	sta_params->mimo_ps = WCN36XX_HAL_HT_MIMO_PS_STATIC;
226 	sta_params->max_ampdu_duration = 0;
227 	sta_params->bssid_index = vif_priv->bss_index;
228 	sta_params->p2p = 0;
229 
230 	if (sta) {
231 		sta_priv = wcn36xx_sta_to_priv(sta);
232 		if (NL80211_IFTYPE_STATION == vif->type)
233 			memcpy(&sta_params->bssid, sta->addr, ETH_ALEN);
234 		else
235 			memcpy(&sta_params->mac, sta->addr, ETH_ALEN);
236 		sta_params->wmm_enabled = sta->wme;
237 		sta_params->max_sp_len = sta->max_sp;
238 		sta_params->aid = sta_priv->aid;
239 		wcn36xx_smd_set_sta_ht_params(sta, sta_params);
240 		memcpy(&sta_params->supported_rates, &sta_priv->supported_rates,
241 			sizeof(sta_priv->supported_rates));
242 	} else {
243 		wcn36xx_set_default_rates(&sta_params->supported_rates);
244 		wcn36xx_smd_set_sta_default_ht_params(sta_params);
245 	}
246 }
247 
248 static int wcn36xx_smd_send_and_wait(struct wcn36xx *wcn, size_t len)
249 {
250 	int ret = 0;
251 	unsigned long start;
252 	wcn36xx_dbg_dump(WCN36XX_DBG_SMD_DUMP, "HAL >>> ", wcn->hal_buf, len);
253 
254 	init_completion(&wcn->hal_rsp_compl);
255 	start = jiffies;
256 	ret = wcn->ctrl_ops->tx(wcn->hal_buf, len);
257 	if (ret) {
258 		wcn36xx_err("HAL TX failed\n");
259 		goto out;
260 	}
261 	if (wait_for_completion_timeout(&wcn->hal_rsp_compl,
262 		msecs_to_jiffies(HAL_MSG_TIMEOUT)) <= 0) {
263 		wcn36xx_err("Timeout! No SMD response in %dms\n",
264 			    HAL_MSG_TIMEOUT);
265 		ret = -ETIME;
266 		goto out;
267 	}
268 	wcn36xx_dbg(WCN36XX_DBG_SMD, "SMD command completed in %dms",
269 		    jiffies_to_msecs(jiffies - start));
270 out:
271 	return ret;
272 }
273 
274 static void init_hal_msg(struct wcn36xx_hal_msg_header *hdr,
275 			 enum wcn36xx_hal_host_msg_type msg_type,
276 			 size_t msg_size)
277 {
278 	memset(hdr, 0, msg_size + sizeof(*hdr));
279 	hdr->msg_type = msg_type;
280 	hdr->msg_version = WCN36XX_HAL_MSG_VERSION0;
281 	hdr->len = msg_size + sizeof(*hdr);
282 }
283 
284 #define INIT_HAL_MSG(msg_body, type) \
285 	do {								\
286 		memset(&msg_body, 0, sizeof(msg_body));			\
287 		msg_body.header.msg_type = type;			\
288 		msg_body.header.msg_version = WCN36XX_HAL_MSG_VERSION0; \
289 		msg_body.header.len = sizeof(msg_body);			\
290 	} while (0)							\
291 
292 #define PREPARE_HAL_BUF(send_buf, msg_body) \
293 	do {							\
294 		memset(send_buf, 0, msg_body.header.len);	\
295 		memcpy(send_buf, &msg_body, sizeof(msg_body));	\
296 	} while (0)						\
297 
298 static int wcn36xx_smd_rsp_status_check(void *buf, size_t len)
299 {
300 	struct wcn36xx_fw_msg_status_rsp *rsp;
301 
302 	if (len < sizeof(struct wcn36xx_hal_msg_header) +
303 	    sizeof(struct wcn36xx_fw_msg_status_rsp))
304 		return -EIO;
305 
306 	rsp = (struct wcn36xx_fw_msg_status_rsp *)
307 		(buf + sizeof(struct wcn36xx_hal_msg_header));
308 
309 	if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->status)
310 		return rsp->status;
311 
312 	return 0;
313 }
314 
315 int wcn36xx_smd_load_nv(struct wcn36xx *wcn)
316 {
317 	struct nv_data *nv_d;
318 	struct wcn36xx_hal_nv_img_download_req_msg msg_body;
319 	int fw_bytes_left;
320 	int ret;
321 	u16 fm_offset = 0;
322 
323 	if (!wcn->nv) {
324 		ret = request_firmware(&wcn->nv, WLAN_NV_FILE, wcn->dev);
325 		if (ret) {
326 			wcn36xx_err("Failed to load nv file %s: %d\n",
327 				      WLAN_NV_FILE, ret);
328 			goto out;
329 		}
330 	}
331 
332 	nv_d = (struct nv_data *)wcn->nv->data;
333 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_DOWNLOAD_NV_REQ);
334 
335 	msg_body.header.len += WCN36XX_NV_FRAGMENT_SIZE;
336 
337 	msg_body.frag_number = 0;
338 	/* hal_buf must be protected with  mutex */
339 	mutex_lock(&wcn->hal_mutex);
340 
341 	do {
342 		fw_bytes_left = wcn->nv->size - fm_offset - 4;
343 		if (fw_bytes_left > WCN36XX_NV_FRAGMENT_SIZE) {
344 			msg_body.last_fragment = 0;
345 			msg_body.nv_img_buffer_size = WCN36XX_NV_FRAGMENT_SIZE;
346 		} else {
347 			msg_body.last_fragment = 1;
348 			msg_body.nv_img_buffer_size = fw_bytes_left;
349 
350 			/* Do not forget update general message len */
351 			msg_body.header.len = sizeof(msg_body) + fw_bytes_left;
352 
353 		}
354 
355 		/* Add load NV request message header */
356 		memcpy(wcn->hal_buf, &msg_body,	sizeof(msg_body));
357 
358 		/* Add NV body itself */
359 		memcpy(wcn->hal_buf + sizeof(msg_body),
360 		       &nv_d->table + fm_offset,
361 		       msg_body.nv_img_buffer_size);
362 
363 		ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
364 		if (ret)
365 			goto out_unlock;
366 		ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf,
367 						   wcn->hal_rsp_len);
368 		if (ret) {
369 			wcn36xx_err("hal_load_nv response failed err=%d\n",
370 				    ret);
371 			goto out_unlock;
372 		}
373 		msg_body.frag_number++;
374 		fm_offset += WCN36XX_NV_FRAGMENT_SIZE;
375 
376 	} while (msg_body.last_fragment != 1);
377 
378 out_unlock:
379 	mutex_unlock(&wcn->hal_mutex);
380 out:	return ret;
381 }
382 
383 static int wcn36xx_smd_start_rsp(struct wcn36xx *wcn, void *buf, size_t len)
384 {
385 	struct wcn36xx_hal_mac_start_rsp_msg *rsp;
386 
387 	if (len < sizeof(*rsp))
388 		return -EIO;
389 
390 	rsp = (struct wcn36xx_hal_mac_start_rsp_msg *)buf;
391 
392 	if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->start_rsp_params.status)
393 		return -EIO;
394 
395 	memcpy(wcn->crm_version, rsp->start_rsp_params.crm_version,
396 	       WCN36XX_HAL_VERSION_LENGTH);
397 	memcpy(wcn->wlan_version, rsp->start_rsp_params.wlan_version,
398 	       WCN36XX_HAL_VERSION_LENGTH);
399 
400 	/* null terminate the strings, just in case */
401 	wcn->crm_version[WCN36XX_HAL_VERSION_LENGTH] = '\0';
402 	wcn->wlan_version[WCN36XX_HAL_VERSION_LENGTH] = '\0';
403 
404 	wcn->fw_revision = rsp->start_rsp_params.version.revision;
405 	wcn->fw_version = rsp->start_rsp_params.version.version;
406 	wcn->fw_minor = rsp->start_rsp_params.version.minor;
407 	wcn->fw_major = rsp->start_rsp_params.version.major;
408 
409 	wcn36xx_info("firmware WLAN version '%s' and CRM version '%s'\n",
410 		     wcn->wlan_version, wcn->crm_version);
411 
412 	wcn36xx_info("firmware API %u.%u.%u.%u, %u stations, %u bssids\n",
413 		     wcn->fw_major, wcn->fw_minor,
414 		     wcn->fw_version, wcn->fw_revision,
415 		     rsp->start_rsp_params.stations,
416 		     rsp->start_rsp_params.bssids);
417 
418 	return 0;
419 }
420 
421 int wcn36xx_smd_start(struct wcn36xx *wcn)
422 {
423 	struct wcn36xx_hal_mac_start_req_msg msg_body, *body;
424 	int ret = 0;
425 	int i;
426 	size_t len;
427 
428 	mutex_lock(&wcn->hal_mutex);
429 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_START_REQ);
430 
431 	msg_body.params.type = DRIVER_TYPE_PRODUCTION;
432 	msg_body.params.len = 0;
433 
434 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
435 
436 	body = (struct wcn36xx_hal_mac_start_req_msg *)wcn->hal_buf;
437 	len = body->header.len;
438 
439 	for (i = 0; i < ARRAY_SIZE(wcn36xx_cfg_vals); i++) {
440 		ret = put_cfg_tlv_u32(wcn, &len, wcn36xx_cfg_vals[i].cfg_id,
441 				      wcn36xx_cfg_vals[i].value);
442 		if (ret)
443 			goto out;
444 	}
445 	body->header.len = len;
446 	body->params.len = len - sizeof(*body);
447 
448 	wcn36xx_dbg(WCN36XX_DBG_HAL, "hal start type %d\n",
449 		    msg_body.params.type);
450 
451 	ret = wcn36xx_smd_send_and_wait(wcn, body->header.len);
452 	if (ret) {
453 		wcn36xx_err("Sending hal_start failed\n");
454 		goto out;
455 	}
456 
457 	ret = wcn36xx_smd_start_rsp(wcn, wcn->hal_buf, wcn->hal_rsp_len);
458 	if (ret) {
459 		wcn36xx_err("hal_start response failed err=%d\n", ret);
460 		goto out;
461 	}
462 
463 out:
464 	mutex_unlock(&wcn->hal_mutex);
465 	return ret;
466 }
467 
468 int wcn36xx_smd_stop(struct wcn36xx *wcn)
469 {
470 	struct wcn36xx_hal_mac_stop_req_msg msg_body;
471 	int ret = 0;
472 
473 	mutex_lock(&wcn->hal_mutex);
474 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_STOP_REQ);
475 
476 	msg_body.stop_req_params.reason = HAL_STOP_TYPE_RF_KILL;
477 
478 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
479 
480 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
481 	if (ret) {
482 		wcn36xx_err("Sending hal_stop failed\n");
483 		goto out;
484 	}
485 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
486 	if (ret) {
487 		wcn36xx_err("hal_stop response failed err=%d\n", ret);
488 		goto out;
489 	}
490 out:
491 	mutex_unlock(&wcn->hal_mutex);
492 	return ret;
493 }
494 
495 int wcn36xx_smd_init_scan(struct wcn36xx *wcn, enum wcn36xx_hal_sys_mode mode)
496 {
497 	struct wcn36xx_hal_init_scan_req_msg msg_body;
498 	int ret = 0;
499 
500 	mutex_lock(&wcn->hal_mutex);
501 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_INIT_SCAN_REQ);
502 
503 	msg_body.mode = mode;
504 
505 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
506 
507 	wcn36xx_dbg(WCN36XX_DBG_HAL, "hal init scan mode %d\n", msg_body.mode);
508 
509 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
510 	if (ret) {
511 		wcn36xx_err("Sending hal_init_scan failed\n");
512 		goto out;
513 	}
514 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
515 	if (ret) {
516 		wcn36xx_err("hal_init_scan response failed err=%d\n", ret);
517 		goto out;
518 	}
519 out:
520 	mutex_unlock(&wcn->hal_mutex);
521 	return ret;
522 }
523 
524 int wcn36xx_smd_start_scan(struct wcn36xx *wcn)
525 {
526 	struct wcn36xx_hal_start_scan_req_msg msg_body;
527 	int ret = 0;
528 
529 	mutex_lock(&wcn->hal_mutex);
530 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_START_SCAN_REQ);
531 
532 	msg_body.scan_channel = WCN36XX_HW_CHANNEL(wcn);
533 
534 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
535 
536 	wcn36xx_dbg(WCN36XX_DBG_HAL, "hal start scan channel %d\n",
537 		    msg_body.scan_channel);
538 
539 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
540 	if (ret) {
541 		wcn36xx_err("Sending hal_start_scan failed\n");
542 		goto out;
543 	}
544 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
545 	if (ret) {
546 		wcn36xx_err("hal_start_scan response failed err=%d\n", ret);
547 		goto out;
548 	}
549 out:
550 	mutex_unlock(&wcn->hal_mutex);
551 	return ret;
552 }
553 
554 int wcn36xx_smd_end_scan(struct wcn36xx *wcn)
555 {
556 	struct wcn36xx_hal_end_scan_req_msg msg_body;
557 	int ret = 0;
558 
559 	mutex_lock(&wcn->hal_mutex);
560 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_END_SCAN_REQ);
561 
562 	msg_body.scan_channel = WCN36XX_HW_CHANNEL(wcn);
563 
564 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
565 
566 	wcn36xx_dbg(WCN36XX_DBG_HAL, "hal end scan channel %d\n",
567 		    msg_body.scan_channel);
568 
569 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
570 	if (ret) {
571 		wcn36xx_err("Sending hal_end_scan failed\n");
572 		goto out;
573 	}
574 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
575 	if (ret) {
576 		wcn36xx_err("hal_end_scan response failed err=%d\n", ret);
577 		goto out;
578 	}
579 out:
580 	mutex_unlock(&wcn->hal_mutex);
581 	return ret;
582 }
583 
584 int wcn36xx_smd_finish_scan(struct wcn36xx *wcn,
585 			    enum wcn36xx_hal_sys_mode mode)
586 {
587 	struct wcn36xx_hal_finish_scan_req_msg msg_body;
588 	int ret = 0;
589 
590 	mutex_lock(&wcn->hal_mutex);
591 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_FINISH_SCAN_REQ);
592 
593 	msg_body.mode = mode;
594 
595 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
596 
597 	wcn36xx_dbg(WCN36XX_DBG_HAL, "hal finish scan mode %d\n",
598 		    msg_body.mode);
599 
600 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
601 	if (ret) {
602 		wcn36xx_err("Sending hal_finish_scan failed\n");
603 		goto out;
604 	}
605 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
606 	if (ret) {
607 		wcn36xx_err("hal_finish_scan response failed err=%d\n", ret);
608 		goto out;
609 	}
610 out:
611 	mutex_unlock(&wcn->hal_mutex);
612 	return ret;
613 }
614 
615 static int wcn36xx_smd_switch_channel_rsp(void *buf, size_t len)
616 {
617 	struct wcn36xx_hal_switch_channel_rsp_msg *rsp;
618 	int ret = 0;
619 
620 	ret = wcn36xx_smd_rsp_status_check(buf, len);
621 	if (ret)
622 		return ret;
623 	rsp = (struct wcn36xx_hal_switch_channel_rsp_msg *)buf;
624 	wcn36xx_dbg(WCN36XX_DBG_HAL, "channel switched to: %d, status: %d\n",
625 		    rsp->channel_number, rsp->status);
626 	return ret;
627 }
628 
629 int wcn36xx_smd_switch_channel(struct wcn36xx *wcn,
630 			       struct ieee80211_vif *vif, int ch)
631 {
632 	struct wcn36xx_hal_switch_channel_req_msg msg_body;
633 	int ret = 0;
634 
635 	mutex_lock(&wcn->hal_mutex);
636 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_CH_SWITCH_REQ);
637 
638 	msg_body.channel_number = (u8)ch;
639 	msg_body.tx_mgmt_power = 0xbf;
640 	msg_body.max_tx_power = 0xbf;
641 	memcpy(msg_body.self_sta_mac_addr, vif->addr, ETH_ALEN);
642 
643 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
644 
645 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
646 	if (ret) {
647 		wcn36xx_err("Sending hal_switch_channel failed\n");
648 		goto out;
649 	}
650 	ret = wcn36xx_smd_switch_channel_rsp(wcn->hal_buf, wcn->hal_rsp_len);
651 	if (ret) {
652 		wcn36xx_err("hal_switch_channel response failed err=%d\n", ret);
653 		goto out;
654 	}
655 out:
656 	mutex_unlock(&wcn->hal_mutex);
657 	return ret;
658 }
659 
660 static int wcn36xx_smd_update_scan_params_rsp(void *buf, size_t len)
661 {
662 	struct wcn36xx_hal_update_scan_params_resp *rsp;
663 
664 	rsp = (struct wcn36xx_hal_update_scan_params_resp *)buf;
665 
666 	/* Remove the PNO version bit */
667 	rsp->status &= (~(WCN36XX_FW_MSG_PNO_VERSION_MASK));
668 
669 	if (WCN36XX_FW_MSG_RESULT_SUCCESS != rsp->status) {
670 		wcn36xx_warn("error response from update scan\n");
671 		return rsp->status;
672 	}
673 
674 	return 0;
675 }
676 
677 int wcn36xx_smd_update_scan_params(struct wcn36xx *wcn,
678 				   u8 *channels, size_t channel_count)
679 {
680 	struct wcn36xx_hal_update_scan_params_req_ex msg_body;
681 	int ret = 0;
682 
683 	mutex_lock(&wcn->hal_mutex);
684 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_UPDATE_SCAN_PARAM_REQ);
685 
686 	msg_body.dot11d_enabled	= false;
687 	msg_body.dot11d_resolved = true;
688 
689 	msg_body.channel_count = channel_count;
690 	memcpy(msg_body.channels, channels, channel_count);
691 	msg_body.active_min_ch_time = 60;
692 	msg_body.active_max_ch_time = 120;
693 	msg_body.passive_min_ch_time = 60;
694 	msg_body.passive_max_ch_time = 110;
695 	msg_body.state = PHY_SINGLE_CHANNEL_CENTERED;
696 
697 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
698 
699 	wcn36xx_dbg(WCN36XX_DBG_HAL,
700 		    "hal update scan params channel_count %d\n",
701 		    msg_body.channel_count);
702 
703 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
704 	if (ret) {
705 		wcn36xx_err("Sending hal_update_scan_params failed\n");
706 		goto out;
707 	}
708 	ret = wcn36xx_smd_update_scan_params_rsp(wcn->hal_buf,
709 						 wcn->hal_rsp_len);
710 	if (ret) {
711 		wcn36xx_err("hal_update_scan_params response failed err=%d\n",
712 			    ret);
713 		goto out;
714 	}
715 out:
716 	mutex_unlock(&wcn->hal_mutex);
717 	return ret;
718 }
719 
720 static int wcn36xx_smd_add_sta_self_rsp(struct wcn36xx *wcn,
721 					struct ieee80211_vif *vif,
722 					void *buf,
723 					size_t len)
724 {
725 	struct wcn36xx_hal_add_sta_self_rsp_msg *rsp;
726 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
727 
728 	if (len < sizeof(*rsp))
729 		return -EINVAL;
730 
731 	rsp = (struct wcn36xx_hal_add_sta_self_rsp_msg *)buf;
732 
733 	if (rsp->status != WCN36XX_FW_MSG_RESULT_SUCCESS) {
734 		wcn36xx_warn("hal add sta self failure: %d\n",
735 			     rsp->status);
736 		return rsp->status;
737 	}
738 
739 	wcn36xx_dbg(WCN36XX_DBG_HAL,
740 		    "hal add sta self status %d self_sta_index %d dpu_index %d\n",
741 		    rsp->status, rsp->self_sta_index, rsp->dpu_index);
742 
743 	vif_priv->self_sta_index = rsp->self_sta_index;
744 	vif_priv->self_dpu_desc_index = rsp->dpu_index;
745 
746 	return 0;
747 }
748 
749 int wcn36xx_smd_add_sta_self(struct wcn36xx *wcn, struct ieee80211_vif *vif)
750 {
751 	struct wcn36xx_hal_add_sta_self_req msg_body;
752 	int ret = 0;
753 
754 	mutex_lock(&wcn->hal_mutex);
755 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_ADD_STA_SELF_REQ);
756 
757 	memcpy(&msg_body.self_addr, vif->addr, ETH_ALEN);
758 
759 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
760 
761 	wcn36xx_dbg(WCN36XX_DBG_HAL,
762 		    "hal add sta self self_addr %pM status %d\n",
763 		    msg_body.self_addr, msg_body.status);
764 
765 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
766 	if (ret) {
767 		wcn36xx_err("Sending hal_add_sta_self failed\n");
768 		goto out;
769 	}
770 	ret = wcn36xx_smd_add_sta_self_rsp(wcn,
771 					   vif,
772 					   wcn->hal_buf,
773 					   wcn->hal_rsp_len);
774 	if (ret) {
775 		wcn36xx_err("hal_add_sta_self response failed err=%d\n", ret);
776 		goto out;
777 	}
778 out:
779 	mutex_unlock(&wcn->hal_mutex);
780 	return ret;
781 }
782 
783 int wcn36xx_smd_delete_sta_self(struct wcn36xx *wcn, u8 *addr)
784 {
785 	struct wcn36xx_hal_del_sta_self_req_msg msg_body;
786 	int ret = 0;
787 
788 	mutex_lock(&wcn->hal_mutex);
789 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_DEL_STA_SELF_REQ);
790 
791 	memcpy(&msg_body.self_addr, addr, ETH_ALEN);
792 
793 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
794 
795 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
796 	if (ret) {
797 		wcn36xx_err("Sending hal_delete_sta_self failed\n");
798 		goto out;
799 	}
800 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
801 	if (ret) {
802 		wcn36xx_err("hal_delete_sta_self response failed err=%d\n",
803 			    ret);
804 		goto out;
805 	}
806 out:
807 	mutex_unlock(&wcn->hal_mutex);
808 	return ret;
809 }
810 
811 int wcn36xx_smd_delete_sta(struct wcn36xx *wcn, u8 sta_index)
812 {
813 	struct wcn36xx_hal_delete_sta_req_msg msg_body;
814 	int ret = 0;
815 
816 	mutex_lock(&wcn->hal_mutex);
817 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_DELETE_STA_REQ);
818 
819 	msg_body.sta_index = sta_index;
820 
821 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
822 
823 	wcn36xx_dbg(WCN36XX_DBG_HAL,
824 		    "hal delete sta sta_index %d\n",
825 		    msg_body.sta_index);
826 
827 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
828 	if (ret) {
829 		wcn36xx_err("Sending hal_delete_sta failed\n");
830 		goto out;
831 	}
832 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
833 	if (ret) {
834 		wcn36xx_err("hal_delete_sta response failed err=%d\n", ret);
835 		goto out;
836 	}
837 out:
838 	mutex_unlock(&wcn->hal_mutex);
839 	return ret;
840 }
841 
842 static int wcn36xx_smd_join_rsp(void *buf, size_t len)
843 {
844 	struct wcn36xx_hal_join_rsp_msg *rsp;
845 
846 	if (wcn36xx_smd_rsp_status_check(buf, len))
847 		return -EIO;
848 
849 	rsp = (struct wcn36xx_hal_join_rsp_msg *)buf;
850 
851 	wcn36xx_dbg(WCN36XX_DBG_HAL,
852 		    "hal rsp join status %d tx_mgmt_power %d\n",
853 		    rsp->status, rsp->tx_mgmt_power);
854 
855 	return 0;
856 }
857 
858 int wcn36xx_smd_join(struct wcn36xx *wcn, const u8 *bssid, u8 *vif, u8 ch)
859 {
860 	struct wcn36xx_hal_join_req_msg msg_body;
861 	int ret = 0;
862 
863 	mutex_lock(&wcn->hal_mutex);
864 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_JOIN_REQ);
865 
866 	memcpy(&msg_body.bssid, bssid, ETH_ALEN);
867 	memcpy(&msg_body.self_sta_mac_addr, vif, ETH_ALEN);
868 	msg_body.channel = ch;
869 
870 	if (conf_is_ht40_minus(&wcn->hw->conf))
871 		msg_body.secondary_channel_offset =
872 			PHY_DOUBLE_CHANNEL_HIGH_PRIMARY;
873 	else if (conf_is_ht40_plus(&wcn->hw->conf))
874 		msg_body.secondary_channel_offset =
875 			PHY_DOUBLE_CHANNEL_LOW_PRIMARY;
876 	else
877 		msg_body.secondary_channel_offset =
878 			PHY_SINGLE_CHANNEL_CENTERED;
879 
880 	msg_body.link_state = WCN36XX_HAL_LINK_PREASSOC_STATE;
881 
882 	msg_body.max_tx_power = 0xbf;
883 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
884 
885 	wcn36xx_dbg(WCN36XX_DBG_HAL,
886 		    "hal join req bssid %pM self_sta_mac_addr %pM channel %d link_state %d\n",
887 		    msg_body.bssid, msg_body.self_sta_mac_addr,
888 		    msg_body.channel, msg_body.link_state);
889 
890 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
891 	if (ret) {
892 		wcn36xx_err("Sending hal_join failed\n");
893 		goto out;
894 	}
895 	ret = wcn36xx_smd_join_rsp(wcn->hal_buf, wcn->hal_rsp_len);
896 	if (ret) {
897 		wcn36xx_err("hal_join response failed err=%d\n", ret);
898 		goto out;
899 	}
900 out:
901 	mutex_unlock(&wcn->hal_mutex);
902 	return ret;
903 }
904 
905 int wcn36xx_smd_set_link_st(struct wcn36xx *wcn, const u8 *bssid,
906 			    const u8 *sta_mac,
907 			    enum wcn36xx_hal_link_state state)
908 {
909 	struct wcn36xx_hal_set_link_state_req_msg msg_body;
910 	int ret = 0;
911 
912 	mutex_lock(&wcn->hal_mutex);
913 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_LINK_ST_REQ);
914 
915 	memcpy(&msg_body.bssid, bssid, ETH_ALEN);
916 	memcpy(&msg_body.self_mac_addr, sta_mac, ETH_ALEN);
917 	msg_body.state = state;
918 
919 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
920 
921 	wcn36xx_dbg(WCN36XX_DBG_HAL,
922 		    "hal set link state bssid %pM self_mac_addr %pM state %d\n",
923 		    msg_body.bssid, msg_body.self_mac_addr, msg_body.state);
924 
925 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
926 	if (ret) {
927 		wcn36xx_err("Sending hal_set_link_st failed\n");
928 		goto out;
929 	}
930 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
931 	if (ret) {
932 		wcn36xx_err("hal_set_link_st response failed err=%d\n", ret);
933 		goto out;
934 	}
935 out:
936 	mutex_unlock(&wcn->hal_mutex);
937 	return ret;
938 }
939 
940 static void wcn36xx_smd_convert_sta_to_v1(struct wcn36xx *wcn,
941 			const struct wcn36xx_hal_config_sta_params *orig,
942 			struct wcn36xx_hal_config_sta_params_v1 *v1)
943 {
944 	/* convert orig to v1 format */
945 	memcpy(&v1->bssid, orig->bssid, ETH_ALEN);
946 	memcpy(&v1->mac, orig->mac, ETH_ALEN);
947 	v1->aid = orig->aid;
948 	v1->type = orig->type;
949 	v1->short_preamble_supported = orig->short_preamble_supported;
950 	v1->listen_interval = orig->listen_interval;
951 	v1->wmm_enabled = orig->wmm_enabled;
952 	v1->ht_capable = orig->ht_capable;
953 	v1->tx_channel_width_set = orig->tx_channel_width_set;
954 	v1->rifs_mode = orig->rifs_mode;
955 	v1->lsig_txop_protection = orig->lsig_txop_protection;
956 	v1->max_ampdu_size = orig->max_ampdu_size;
957 	v1->max_ampdu_density = orig->max_ampdu_density;
958 	v1->sgi_40mhz = orig->sgi_40mhz;
959 	v1->sgi_20Mhz = orig->sgi_20Mhz;
960 	v1->rmf = orig->rmf;
961 	v1->encrypt_type = orig->encrypt_type;
962 	v1->action = orig->action;
963 	v1->uapsd = orig->uapsd;
964 	v1->max_sp_len = orig->max_sp_len;
965 	v1->green_field_capable = orig->green_field_capable;
966 	v1->mimo_ps = orig->mimo_ps;
967 	v1->delayed_ba_support = orig->delayed_ba_support;
968 	v1->max_ampdu_duration = orig->max_ampdu_duration;
969 	v1->dsss_cck_mode_40mhz = orig->dsss_cck_mode_40mhz;
970 	memcpy(&v1->supported_rates, &orig->supported_rates,
971 	       sizeof(orig->supported_rates));
972 	v1->sta_index = orig->sta_index;
973 	v1->bssid_index = orig->bssid_index;
974 	v1->p2p = orig->p2p;
975 }
976 
977 static int wcn36xx_smd_config_sta_rsp(struct wcn36xx *wcn,
978 				      struct ieee80211_sta *sta,
979 				      void *buf,
980 				      size_t len)
981 {
982 	struct wcn36xx_hal_config_sta_rsp_msg *rsp;
983 	struct config_sta_rsp_params *params;
984 	struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
985 
986 	if (len < sizeof(*rsp))
987 		return -EINVAL;
988 
989 	rsp = (struct wcn36xx_hal_config_sta_rsp_msg *)buf;
990 	params = &rsp->params;
991 
992 	if (params->status != WCN36XX_FW_MSG_RESULT_SUCCESS) {
993 		wcn36xx_warn("hal config sta response failure: %d\n",
994 			     params->status);
995 		return -EIO;
996 	}
997 
998 	sta_priv->sta_index = params->sta_index;
999 	sta_priv->dpu_desc_index = params->dpu_index;
1000 	sta_priv->ucast_dpu_sign = params->uc_ucast_sig;
1001 
1002 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1003 		    "hal config sta rsp status %d sta_index %d bssid_index %d uc_ucast_sig %d p2p %d\n",
1004 		    params->status, params->sta_index, params->bssid_index,
1005 		    params->uc_ucast_sig, params->p2p);
1006 
1007 	return 0;
1008 }
1009 
1010 static int wcn36xx_smd_config_sta_v1(struct wcn36xx *wcn,
1011 		     const struct wcn36xx_hal_config_sta_req_msg *orig)
1012 {
1013 	struct wcn36xx_hal_config_sta_req_msg_v1 msg_body;
1014 	struct wcn36xx_hal_config_sta_params_v1 *sta = &msg_body.sta_params;
1015 
1016 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_CONFIG_STA_REQ);
1017 
1018 	wcn36xx_smd_convert_sta_to_v1(wcn, &orig->sta_params,
1019 				      &msg_body.sta_params);
1020 
1021 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1022 
1023 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1024 		    "hal config sta v1 action %d sta_index %d bssid_index %d bssid %pM type %d mac %pM aid %d\n",
1025 		    sta->action, sta->sta_index, sta->bssid_index,
1026 		    sta->bssid, sta->type, sta->mac, sta->aid);
1027 
1028 	return wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1029 }
1030 
1031 int wcn36xx_smd_config_sta(struct wcn36xx *wcn, struct ieee80211_vif *vif,
1032 			   struct ieee80211_sta *sta)
1033 {
1034 	struct wcn36xx_hal_config_sta_req_msg msg;
1035 	struct wcn36xx_hal_config_sta_params *sta_params;
1036 	int ret = 0;
1037 
1038 	mutex_lock(&wcn->hal_mutex);
1039 	INIT_HAL_MSG(msg, WCN36XX_HAL_CONFIG_STA_REQ);
1040 
1041 	sta_params = &msg.sta_params;
1042 
1043 	wcn36xx_smd_set_sta_params(wcn, vif, sta, sta_params);
1044 
1045 	if (!wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
1046 		ret = wcn36xx_smd_config_sta_v1(wcn, &msg);
1047 	} else {
1048 		PREPARE_HAL_BUF(wcn->hal_buf, msg);
1049 
1050 		wcn36xx_dbg(WCN36XX_DBG_HAL,
1051 			    "hal config sta action %d sta_index %d bssid_index %d bssid %pM type %d mac %pM aid %d\n",
1052 			    sta_params->action, sta_params->sta_index,
1053 			    sta_params->bssid_index, sta_params->bssid,
1054 			    sta_params->type, sta_params->mac, sta_params->aid);
1055 
1056 		ret = wcn36xx_smd_send_and_wait(wcn, msg.header.len);
1057 	}
1058 	if (ret) {
1059 		wcn36xx_err("Sending hal_config_sta failed\n");
1060 		goto out;
1061 	}
1062 	ret = wcn36xx_smd_config_sta_rsp(wcn,
1063 					 sta,
1064 					 wcn->hal_buf,
1065 					 wcn->hal_rsp_len);
1066 	if (ret) {
1067 		wcn36xx_err("hal_config_sta response failed err=%d\n", ret);
1068 		goto out;
1069 	}
1070 out:
1071 	mutex_unlock(&wcn->hal_mutex);
1072 	return ret;
1073 }
1074 
1075 static int wcn36xx_smd_config_bss_v1(struct wcn36xx *wcn,
1076 			const struct wcn36xx_hal_config_bss_req_msg *orig)
1077 {
1078 	struct wcn36xx_hal_config_bss_req_msg_v1 msg_body;
1079 	struct wcn36xx_hal_config_bss_params_v1 *bss = &msg_body.bss_params;
1080 	struct wcn36xx_hal_config_sta_params_v1 *sta = &bss->sta;
1081 
1082 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_CONFIG_BSS_REQ);
1083 
1084 	/* convert orig to v1 */
1085 	memcpy(&msg_body.bss_params.bssid,
1086 	       &orig->bss_params.bssid, ETH_ALEN);
1087 	memcpy(&msg_body.bss_params.self_mac_addr,
1088 	       &orig->bss_params.self_mac_addr, ETH_ALEN);
1089 
1090 	msg_body.bss_params.bss_type = orig->bss_params.bss_type;
1091 	msg_body.bss_params.oper_mode = orig->bss_params.oper_mode;
1092 	msg_body.bss_params.nw_type = orig->bss_params.nw_type;
1093 
1094 	msg_body.bss_params.short_slot_time_supported =
1095 		orig->bss_params.short_slot_time_supported;
1096 	msg_body.bss_params.lla_coexist = orig->bss_params.lla_coexist;
1097 	msg_body.bss_params.llb_coexist = orig->bss_params.llb_coexist;
1098 	msg_body.bss_params.llg_coexist = orig->bss_params.llg_coexist;
1099 	msg_body.bss_params.ht20_coexist = orig->bss_params.ht20_coexist;
1100 	msg_body.bss_params.lln_non_gf_coexist =
1101 		orig->bss_params.lln_non_gf_coexist;
1102 
1103 	msg_body.bss_params.lsig_tx_op_protection_full_support =
1104 		orig->bss_params.lsig_tx_op_protection_full_support;
1105 	msg_body.bss_params.rifs_mode = orig->bss_params.rifs_mode;
1106 	msg_body.bss_params.beacon_interval = orig->bss_params.beacon_interval;
1107 	msg_body.bss_params.dtim_period = orig->bss_params.dtim_period;
1108 	msg_body.bss_params.tx_channel_width_set =
1109 		orig->bss_params.tx_channel_width_set;
1110 	msg_body.bss_params.oper_channel = orig->bss_params.oper_channel;
1111 	msg_body.bss_params.ext_channel = orig->bss_params.ext_channel;
1112 
1113 	msg_body.bss_params.reserved = orig->bss_params.reserved;
1114 
1115 	memcpy(&msg_body.bss_params.ssid,
1116 	       &orig->bss_params.ssid,
1117 	       sizeof(orig->bss_params.ssid));
1118 
1119 	msg_body.bss_params.action = orig->bss_params.action;
1120 	msg_body.bss_params.rateset = orig->bss_params.rateset;
1121 	msg_body.bss_params.ht = orig->bss_params.ht;
1122 	msg_body.bss_params.obss_prot_enabled =
1123 		orig->bss_params.obss_prot_enabled;
1124 	msg_body.bss_params.rmf = orig->bss_params.rmf;
1125 	msg_body.bss_params.ht_oper_mode = orig->bss_params.ht_oper_mode;
1126 	msg_body.bss_params.dual_cts_protection =
1127 		orig->bss_params.dual_cts_protection;
1128 
1129 	msg_body.bss_params.max_probe_resp_retry_limit =
1130 		orig->bss_params.max_probe_resp_retry_limit;
1131 	msg_body.bss_params.hidden_ssid = orig->bss_params.hidden_ssid;
1132 	msg_body.bss_params.proxy_probe_resp =
1133 		orig->bss_params.proxy_probe_resp;
1134 	msg_body.bss_params.edca_params_valid =
1135 		orig->bss_params.edca_params_valid;
1136 
1137 	memcpy(&msg_body.bss_params.acbe,
1138 	       &orig->bss_params.acbe,
1139 	       sizeof(orig->bss_params.acbe));
1140 	memcpy(&msg_body.bss_params.acbk,
1141 	       &orig->bss_params.acbk,
1142 	       sizeof(orig->bss_params.acbk));
1143 	memcpy(&msg_body.bss_params.acvi,
1144 	       &orig->bss_params.acvi,
1145 	       sizeof(orig->bss_params.acvi));
1146 	memcpy(&msg_body.bss_params.acvo,
1147 	       &orig->bss_params.acvo,
1148 	       sizeof(orig->bss_params.acvo));
1149 
1150 	msg_body.bss_params.ext_set_sta_key_param_valid =
1151 		orig->bss_params.ext_set_sta_key_param_valid;
1152 
1153 	memcpy(&msg_body.bss_params.ext_set_sta_key_param,
1154 	       &orig->bss_params.ext_set_sta_key_param,
1155 	       sizeof(orig->bss_params.acvo));
1156 
1157 	msg_body.bss_params.wcn36xx_hal_persona =
1158 		orig->bss_params.wcn36xx_hal_persona;
1159 	msg_body.bss_params.spectrum_mgt_enable =
1160 		orig->bss_params.spectrum_mgt_enable;
1161 	msg_body.bss_params.tx_mgmt_power = orig->bss_params.tx_mgmt_power;
1162 	msg_body.bss_params.max_tx_power = orig->bss_params.max_tx_power;
1163 
1164 	wcn36xx_smd_convert_sta_to_v1(wcn, &orig->bss_params.sta,
1165 				      &msg_body.bss_params.sta);
1166 
1167 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1168 
1169 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1170 		    "hal config bss v1 bssid %pM self_mac_addr %pM bss_type %d oper_mode %d nw_type %d\n",
1171 		    bss->bssid, bss->self_mac_addr, bss->bss_type,
1172 		    bss->oper_mode, bss->nw_type);
1173 
1174 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1175 		    "- sta bssid %pM action %d sta_index %d bssid_index %d aid %d type %d mac %pM\n",
1176 		    sta->bssid, sta->action, sta->sta_index,
1177 		    sta->bssid_index, sta->aid, sta->type, sta->mac);
1178 
1179 	return wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1180 }
1181 
1182 
1183 static int wcn36xx_smd_config_bss_rsp(struct wcn36xx *wcn,
1184 				      struct ieee80211_vif *vif,
1185 				      struct ieee80211_sta *sta,
1186 				      void *buf,
1187 				      size_t len)
1188 {
1189 	struct wcn36xx_hal_config_bss_rsp_msg *rsp;
1190 	struct wcn36xx_hal_config_bss_rsp_params *params;
1191 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1192 
1193 	if (len < sizeof(*rsp))
1194 		return -EINVAL;
1195 
1196 	rsp = (struct wcn36xx_hal_config_bss_rsp_msg *)buf;
1197 	params = &rsp->bss_rsp_params;
1198 
1199 	if (params->status != WCN36XX_FW_MSG_RESULT_SUCCESS) {
1200 		wcn36xx_warn("hal config bss response failure: %d\n",
1201 			     params->status);
1202 		return -EIO;
1203 	}
1204 
1205 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1206 		    "hal config bss rsp status %d bss_idx %d dpu_desc_index %d"
1207 		    " sta_idx %d self_idx %d bcast_idx %d mac %pM"
1208 		    " power %d ucast_dpu_signature %d\n",
1209 		    params->status, params->bss_index, params->dpu_desc_index,
1210 		    params->bss_sta_index, params->bss_self_sta_index,
1211 		    params->bss_bcast_sta_idx, params->mac,
1212 		    params->tx_mgmt_power, params->ucast_dpu_signature);
1213 
1214 	vif_priv->bss_index = params->bss_index;
1215 
1216 	if (sta) {
1217 		struct wcn36xx_sta *sta_priv = wcn36xx_sta_to_priv(sta);
1218 		sta_priv->bss_sta_index = params->bss_sta_index;
1219 		sta_priv->bss_dpu_desc_index = params->dpu_desc_index;
1220 	}
1221 
1222 	vif_priv->self_ucast_dpu_sign = params->ucast_dpu_signature;
1223 
1224 	return 0;
1225 }
1226 
1227 int wcn36xx_smd_config_bss(struct wcn36xx *wcn, struct ieee80211_vif *vif,
1228 			   struct ieee80211_sta *sta, const u8 *bssid,
1229 			   bool update)
1230 {
1231 	struct wcn36xx_hal_config_bss_req_msg msg;
1232 	struct wcn36xx_hal_config_bss_params *bss;
1233 	struct wcn36xx_hal_config_sta_params *sta_params;
1234 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1235 	int ret = 0;
1236 
1237 	mutex_lock(&wcn->hal_mutex);
1238 	INIT_HAL_MSG(msg, WCN36XX_HAL_CONFIG_BSS_REQ);
1239 
1240 	bss = &msg.bss_params;
1241 	sta_params = &bss->sta;
1242 
1243 	WARN_ON(is_zero_ether_addr(bssid));
1244 
1245 	memcpy(&bss->bssid, bssid, ETH_ALEN);
1246 
1247 	memcpy(bss->self_mac_addr, vif->addr, ETH_ALEN);
1248 
1249 	if (vif->type == NL80211_IFTYPE_STATION) {
1250 		bss->bss_type = WCN36XX_HAL_INFRASTRUCTURE_MODE;
1251 
1252 		/* STA */
1253 		bss->oper_mode = 1;
1254 		bss->wcn36xx_hal_persona = WCN36XX_HAL_STA_MODE;
1255 	} else if (vif->type == NL80211_IFTYPE_AP ||
1256 		   vif->type == NL80211_IFTYPE_MESH_POINT) {
1257 		bss->bss_type = WCN36XX_HAL_INFRA_AP_MODE;
1258 
1259 		/* AP */
1260 		bss->oper_mode = 0;
1261 		bss->wcn36xx_hal_persona = WCN36XX_HAL_STA_SAP_MODE;
1262 	} else if (vif->type == NL80211_IFTYPE_ADHOC) {
1263 		bss->bss_type = WCN36XX_HAL_IBSS_MODE;
1264 
1265 		/* STA */
1266 		bss->oper_mode = 1;
1267 	} else {
1268 		wcn36xx_warn("Unknown type for bss config: %d\n", vif->type);
1269 	}
1270 
1271 	if (vif->type == NL80211_IFTYPE_STATION)
1272 		wcn36xx_smd_set_bss_nw_type(wcn, sta, bss);
1273 	else
1274 		bss->nw_type = WCN36XX_HAL_11N_NW_TYPE;
1275 
1276 	bss->short_slot_time_supported = vif->bss_conf.use_short_slot;
1277 	bss->lla_coexist = 0;
1278 	bss->llb_coexist = 0;
1279 	bss->llg_coexist = 0;
1280 	bss->rifs_mode = 0;
1281 	bss->beacon_interval = vif->bss_conf.beacon_int;
1282 	bss->dtim_period = vif_priv->dtim_period;
1283 
1284 	wcn36xx_smd_set_bss_ht_params(vif, sta, bss);
1285 
1286 	bss->oper_channel = WCN36XX_HW_CHANNEL(wcn);
1287 
1288 	if (conf_is_ht40_minus(&wcn->hw->conf))
1289 		bss->ext_channel = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1290 	else if (conf_is_ht40_plus(&wcn->hw->conf))
1291 		bss->ext_channel = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1292 	else
1293 		bss->ext_channel = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1294 
1295 	bss->reserved = 0;
1296 	wcn36xx_smd_set_sta_params(wcn, vif, sta, sta_params);
1297 
1298 	/* wcn->ssid is only valid in AP and IBSS mode */
1299 	bss->ssid.length = vif_priv->ssid.length;
1300 	memcpy(bss->ssid.ssid, vif_priv->ssid.ssid, vif_priv->ssid.length);
1301 
1302 	bss->obss_prot_enabled = 0;
1303 	bss->rmf = 0;
1304 	bss->max_probe_resp_retry_limit = 0;
1305 	bss->hidden_ssid = vif->bss_conf.hidden_ssid;
1306 	bss->proxy_probe_resp = 0;
1307 	bss->edca_params_valid = 0;
1308 
1309 	/* FIXME: set acbe, acbk, acvi and acvo */
1310 
1311 	bss->ext_set_sta_key_param_valid = 0;
1312 
1313 	/* FIXME: set ext_set_sta_key_param */
1314 
1315 	bss->spectrum_mgt_enable = 0;
1316 	bss->tx_mgmt_power = 0;
1317 	bss->max_tx_power = WCN36XX_MAX_POWER(wcn);
1318 
1319 	bss->action = update;
1320 
1321 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1322 		    "hal config bss bssid %pM self_mac_addr %pM bss_type %d oper_mode %d nw_type %d\n",
1323 		    bss->bssid, bss->self_mac_addr, bss->bss_type,
1324 		    bss->oper_mode, bss->nw_type);
1325 
1326 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1327 		    "- sta bssid %pM action %d sta_index %d bssid_index %d aid %d type %d mac %pM\n",
1328 		    sta_params->bssid, sta_params->action,
1329 		    sta_params->sta_index, sta_params->bssid_index,
1330 		    sta_params->aid, sta_params->type,
1331 		    sta_params->mac);
1332 
1333 	if (!wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
1334 		ret = wcn36xx_smd_config_bss_v1(wcn, &msg);
1335 	} else {
1336 		PREPARE_HAL_BUF(wcn->hal_buf, msg);
1337 
1338 		ret = wcn36xx_smd_send_and_wait(wcn, msg.header.len);
1339 	}
1340 	if (ret) {
1341 		wcn36xx_err("Sending hal_config_bss failed\n");
1342 		goto out;
1343 	}
1344 	ret = wcn36xx_smd_config_bss_rsp(wcn,
1345 					 vif,
1346 					 sta,
1347 					 wcn->hal_buf,
1348 					 wcn->hal_rsp_len);
1349 	if (ret) {
1350 		wcn36xx_err("hal_config_bss response failed err=%d\n", ret);
1351 		goto out;
1352 	}
1353 out:
1354 	mutex_unlock(&wcn->hal_mutex);
1355 	return ret;
1356 }
1357 
1358 int wcn36xx_smd_delete_bss(struct wcn36xx *wcn, struct ieee80211_vif *vif)
1359 {
1360 	struct wcn36xx_hal_delete_bss_req_msg msg_body;
1361 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1362 	int ret = 0;
1363 
1364 	mutex_lock(&wcn->hal_mutex);
1365 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_DELETE_BSS_REQ);
1366 
1367 	msg_body.bss_index = vif_priv->bss_index;
1368 
1369 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1370 
1371 	wcn36xx_dbg(WCN36XX_DBG_HAL, "hal delete bss %d\n", msg_body.bss_index);
1372 
1373 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1374 	if (ret) {
1375 		wcn36xx_err("Sending hal_delete_bss failed\n");
1376 		goto out;
1377 	}
1378 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1379 	if (ret) {
1380 		wcn36xx_err("hal_delete_bss response failed err=%d\n", ret);
1381 		goto out;
1382 	}
1383 out:
1384 	mutex_unlock(&wcn->hal_mutex);
1385 	return ret;
1386 }
1387 
1388 int wcn36xx_smd_send_beacon(struct wcn36xx *wcn, struct ieee80211_vif *vif,
1389 			    struct sk_buff *skb_beacon, u16 tim_off,
1390 			    u16 p2p_off)
1391 {
1392 	struct wcn36xx_hal_send_beacon_req_msg msg_body;
1393 	int ret = 0, pad, pvm_len;
1394 
1395 	mutex_lock(&wcn->hal_mutex);
1396 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_SEND_BEACON_REQ);
1397 
1398 	pvm_len = skb_beacon->data[tim_off + 1] - 3;
1399 	pad = TIM_MIN_PVM_SIZE - pvm_len;
1400 
1401 	/* Padding is irrelevant to mesh mode since tim_off is always 0. */
1402 	if (vif->type == NL80211_IFTYPE_MESH_POINT)
1403 		pad = 0;
1404 
1405 	msg_body.beacon_length = skb_beacon->len + pad;
1406 	/* TODO need to find out why + 6 is needed */
1407 	msg_body.beacon_length6 = msg_body.beacon_length + 6;
1408 
1409 	if (msg_body.beacon_length > BEACON_TEMPLATE_SIZE) {
1410 		wcn36xx_err("Beacon is to big: beacon size=%d\n",
1411 			      msg_body.beacon_length);
1412 		ret = -ENOMEM;
1413 		goto out;
1414 	}
1415 	memcpy(msg_body.beacon, skb_beacon->data, skb_beacon->len);
1416 	memcpy(msg_body.bssid, vif->addr, ETH_ALEN);
1417 
1418 	if (pad > 0) {
1419 		/*
1420 		 * The wcn36xx FW has a fixed size for the PVM in the TIM. If
1421 		 * given the beacon template from mac80211 with a PVM shorter
1422 		 * than the FW expectes it will overwrite the data after the
1423 		 * TIM.
1424 		 */
1425 		wcn36xx_dbg(WCN36XX_DBG_HAL, "Pad TIM PVM. %d bytes at %d\n",
1426 			    pad, pvm_len);
1427 		memmove(&msg_body.beacon[tim_off + 5 + pvm_len + pad],
1428 			&msg_body.beacon[tim_off + 5 + pvm_len],
1429 			skb_beacon->len - (tim_off + 5 + pvm_len));
1430 		memset(&msg_body.beacon[tim_off + 5 + pvm_len], 0, pad);
1431 		msg_body.beacon[tim_off + 1] += pad;
1432 	}
1433 
1434 	/* TODO need to find out why this is needed? */
1435 	if (vif->type == NL80211_IFTYPE_MESH_POINT)
1436 		/* mesh beacon don't need this, so push further down */
1437 		msg_body.tim_ie_offset = 256;
1438 	else
1439 		msg_body.tim_ie_offset = tim_off+4;
1440 	msg_body.p2p_ie_offset = p2p_off;
1441 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1442 
1443 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1444 		    "hal send beacon beacon_length %d\n",
1445 		    msg_body.beacon_length);
1446 
1447 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1448 	if (ret) {
1449 		wcn36xx_err("Sending hal_send_beacon failed\n");
1450 		goto out;
1451 	}
1452 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1453 	if (ret) {
1454 		wcn36xx_err("hal_send_beacon response failed err=%d\n", ret);
1455 		goto out;
1456 	}
1457 out:
1458 	mutex_unlock(&wcn->hal_mutex);
1459 	return ret;
1460 }
1461 
1462 int wcn36xx_smd_update_proberesp_tmpl(struct wcn36xx *wcn,
1463 				      struct ieee80211_vif *vif,
1464 				      struct sk_buff *skb)
1465 {
1466 	struct wcn36xx_hal_send_probe_resp_req_msg msg;
1467 	int ret = 0;
1468 
1469 	mutex_lock(&wcn->hal_mutex);
1470 	INIT_HAL_MSG(msg, WCN36XX_HAL_UPDATE_PROBE_RSP_TEMPLATE_REQ);
1471 
1472 	if (skb->len > BEACON_TEMPLATE_SIZE) {
1473 		wcn36xx_warn("probe response template is too big: %d\n",
1474 			     skb->len);
1475 		ret = -E2BIG;
1476 		goto out;
1477 	}
1478 
1479 	msg.probe_resp_template_len = skb->len;
1480 	memcpy(&msg.probe_resp_template, skb->data, skb->len);
1481 
1482 	memcpy(msg.bssid, vif->addr, ETH_ALEN);
1483 
1484 	PREPARE_HAL_BUF(wcn->hal_buf, msg);
1485 
1486 	wcn36xx_dbg(WCN36XX_DBG_HAL,
1487 		    "hal update probe rsp len %d bssid %pM\n",
1488 		    msg.probe_resp_template_len, msg.bssid);
1489 
1490 	ret = wcn36xx_smd_send_and_wait(wcn, msg.header.len);
1491 	if (ret) {
1492 		wcn36xx_err("Sending hal_update_proberesp_tmpl failed\n");
1493 		goto out;
1494 	}
1495 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1496 	if (ret) {
1497 		wcn36xx_err("hal_update_proberesp_tmpl response failed err=%d\n",
1498 			    ret);
1499 		goto out;
1500 	}
1501 out:
1502 	mutex_unlock(&wcn->hal_mutex);
1503 	return ret;
1504 }
1505 
1506 int wcn36xx_smd_set_stakey(struct wcn36xx *wcn,
1507 			   enum ani_ed_type enc_type,
1508 			   u8 keyidx,
1509 			   u8 keylen,
1510 			   u8 *key,
1511 			   u8 sta_index)
1512 {
1513 	struct wcn36xx_hal_set_sta_key_req_msg msg_body;
1514 	int ret = 0;
1515 
1516 	mutex_lock(&wcn->hal_mutex);
1517 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_STAKEY_REQ);
1518 
1519 	msg_body.set_sta_key_params.sta_index = sta_index;
1520 	msg_body.set_sta_key_params.enc_type = enc_type;
1521 
1522 	msg_body.set_sta_key_params.key[0].id = keyidx;
1523 	msg_body.set_sta_key_params.key[0].unicast = 1;
1524 	msg_body.set_sta_key_params.key[0].direction = WCN36XX_HAL_TX_RX;
1525 	msg_body.set_sta_key_params.key[0].pae_role = 0;
1526 	msg_body.set_sta_key_params.key[0].length = keylen;
1527 	memcpy(msg_body.set_sta_key_params.key[0].key, key, keylen);
1528 	msg_body.set_sta_key_params.single_tid_rc = 1;
1529 
1530 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1531 
1532 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1533 	if (ret) {
1534 		wcn36xx_err("Sending hal_set_stakey failed\n");
1535 		goto out;
1536 	}
1537 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1538 	if (ret) {
1539 		wcn36xx_err("hal_set_stakey response failed err=%d\n", ret);
1540 		goto out;
1541 	}
1542 out:
1543 	mutex_unlock(&wcn->hal_mutex);
1544 	return ret;
1545 }
1546 
1547 int wcn36xx_smd_set_bsskey(struct wcn36xx *wcn,
1548 			   enum ani_ed_type enc_type,
1549 			   u8 keyidx,
1550 			   u8 keylen,
1551 			   u8 *key)
1552 {
1553 	struct wcn36xx_hal_set_bss_key_req_msg msg_body;
1554 	int ret = 0;
1555 
1556 	mutex_lock(&wcn->hal_mutex);
1557 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_BSSKEY_REQ);
1558 	msg_body.bss_idx = 0;
1559 	msg_body.enc_type = enc_type;
1560 	msg_body.num_keys = 1;
1561 	msg_body.keys[0].id = keyidx;
1562 	msg_body.keys[0].unicast = 0;
1563 	msg_body.keys[0].direction = WCN36XX_HAL_RX_ONLY;
1564 	msg_body.keys[0].pae_role = 0;
1565 	msg_body.keys[0].length = keylen;
1566 	memcpy(msg_body.keys[0].key, key, keylen);
1567 
1568 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1569 
1570 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1571 	if (ret) {
1572 		wcn36xx_err("Sending hal_set_bsskey failed\n");
1573 		goto out;
1574 	}
1575 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1576 	if (ret) {
1577 		wcn36xx_err("hal_set_bsskey response failed err=%d\n", ret);
1578 		goto out;
1579 	}
1580 out:
1581 	mutex_unlock(&wcn->hal_mutex);
1582 	return ret;
1583 }
1584 
1585 int wcn36xx_smd_remove_stakey(struct wcn36xx *wcn,
1586 			      enum ani_ed_type enc_type,
1587 			      u8 keyidx,
1588 			      u8 sta_index)
1589 {
1590 	struct wcn36xx_hal_remove_sta_key_req_msg msg_body;
1591 	int ret = 0;
1592 
1593 	mutex_lock(&wcn->hal_mutex);
1594 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_RMV_STAKEY_REQ);
1595 
1596 	msg_body.sta_idx = sta_index;
1597 	msg_body.enc_type = enc_type;
1598 	msg_body.key_id = keyidx;
1599 
1600 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1601 
1602 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1603 	if (ret) {
1604 		wcn36xx_err("Sending hal_remove_stakey failed\n");
1605 		goto out;
1606 	}
1607 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1608 	if (ret) {
1609 		wcn36xx_err("hal_remove_stakey response failed err=%d\n", ret);
1610 		goto out;
1611 	}
1612 out:
1613 	mutex_unlock(&wcn->hal_mutex);
1614 	return ret;
1615 }
1616 
1617 int wcn36xx_smd_remove_bsskey(struct wcn36xx *wcn,
1618 			      enum ani_ed_type enc_type,
1619 			      u8 keyidx)
1620 {
1621 	struct wcn36xx_hal_remove_bss_key_req_msg msg_body;
1622 	int ret = 0;
1623 
1624 	mutex_lock(&wcn->hal_mutex);
1625 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_RMV_BSSKEY_REQ);
1626 	msg_body.bss_idx = 0;
1627 	msg_body.enc_type = enc_type;
1628 	msg_body.key_id = keyidx;
1629 
1630 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1631 
1632 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1633 	if (ret) {
1634 		wcn36xx_err("Sending hal_remove_bsskey failed\n");
1635 		goto out;
1636 	}
1637 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1638 	if (ret) {
1639 		wcn36xx_err("hal_remove_bsskey response failed err=%d\n", ret);
1640 		goto out;
1641 	}
1642 out:
1643 	mutex_unlock(&wcn->hal_mutex);
1644 	return ret;
1645 }
1646 
1647 int wcn36xx_smd_enter_bmps(struct wcn36xx *wcn, struct ieee80211_vif *vif)
1648 {
1649 	struct wcn36xx_hal_enter_bmps_req_msg msg_body;
1650 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1651 	int ret = 0;
1652 
1653 	mutex_lock(&wcn->hal_mutex);
1654 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_ENTER_BMPS_REQ);
1655 
1656 	msg_body.bss_index = vif_priv->bss_index;
1657 	msg_body.tbtt = vif->bss_conf.sync_tsf;
1658 	msg_body.dtim_period = vif_priv->dtim_period;
1659 
1660 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1661 
1662 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1663 	if (ret) {
1664 		wcn36xx_err("Sending hal_enter_bmps failed\n");
1665 		goto out;
1666 	}
1667 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1668 	if (ret) {
1669 		wcn36xx_err("hal_enter_bmps response failed err=%d\n", ret);
1670 		goto out;
1671 	}
1672 out:
1673 	mutex_unlock(&wcn->hal_mutex);
1674 	return ret;
1675 }
1676 
1677 int wcn36xx_smd_exit_bmps(struct wcn36xx *wcn, struct ieee80211_vif *vif)
1678 {
1679 	struct wcn36xx_hal_exit_bmps_req_msg msg_body;
1680 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1681 	int ret = 0;
1682 
1683 	mutex_lock(&wcn->hal_mutex);
1684 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_EXIT_BMPS_REQ);
1685 
1686 	msg_body.bss_index = vif_priv->bss_index;
1687 
1688 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1689 
1690 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1691 	if (ret) {
1692 		wcn36xx_err("Sending hal_exit_bmps failed\n");
1693 		goto out;
1694 	}
1695 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1696 	if (ret) {
1697 		wcn36xx_err("hal_exit_bmps response failed err=%d\n", ret);
1698 		goto out;
1699 	}
1700 out:
1701 	mutex_unlock(&wcn->hal_mutex);
1702 	return ret;
1703 }
1704 int wcn36xx_smd_set_power_params(struct wcn36xx *wcn, bool ignore_dtim)
1705 {
1706 	struct wcn36xx_hal_set_power_params_req_msg msg_body;
1707 	int ret = 0;
1708 
1709 	mutex_lock(&wcn->hal_mutex);
1710 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_SET_POWER_PARAMS_REQ);
1711 
1712 	/*
1713 	 * When host is down ignore every second dtim
1714 	 */
1715 	if (ignore_dtim) {
1716 		msg_body.ignore_dtim = 1;
1717 		msg_body.dtim_period = 2;
1718 	}
1719 	msg_body.listen_interval = WCN36XX_LISTEN_INTERVAL(wcn);
1720 
1721 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1722 
1723 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1724 	if (ret) {
1725 		wcn36xx_err("Sending hal_set_power_params failed\n");
1726 		goto out;
1727 	}
1728 
1729 out:
1730 	mutex_unlock(&wcn->hal_mutex);
1731 	return ret;
1732 }
1733 /* Notice: This function should be called after associated, or else it
1734  * will be invalid
1735  */
1736 int wcn36xx_smd_keep_alive_req(struct wcn36xx *wcn,
1737 			       struct ieee80211_vif *vif,
1738 			       int packet_type)
1739 {
1740 	struct wcn36xx_hal_keep_alive_req_msg msg_body;
1741 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
1742 	int ret = 0;
1743 
1744 	mutex_lock(&wcn->hal_mutex);
1745 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_KEEP_ALIVE_REQ);
1746 
1747 	if (packet_type == WCN36XX_HAL_KEEP_ALIVE_NULL_PKT) {
1748 		msg_body.bss_index = vif_priv->bss_index;
1749 		msg_body.packet_type = WCN36XX_HAL_KEEP_ALIVE_NULL_PKT;
1750 		msg_body.time_period = WCN36XX_KEEP_ALIVE_TIME_PERIOD;
1751 	} else if (packet_type == WCN36XX_HAL_KEEP_ALIVE_UNSOLICIT_ARP_RSP) {
1752 		/* TODO: it also support ARP response type */
1753 	} else {
1754 		wcn36xx_warn("unknown keep alive packet type %d\n", packet_type);
1755 		ret = -EINVAL;
1756 		goto out;
1757 	}
1758 
1759 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1760 
1761 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1762 	if (ret) {
1763 		wcn36xx_err("Sending hal_keep_alive failed\n");
1764 		goto out;
1765 	}
1766 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1767 	if (ret) {
1768 		wcn36xx_err("hal_keep_alive response failed err=%d\n", ret);
1769 		goto out;
1770 	}
1771 out:
1772 	mutex_unlock(&wcn->hal_mutex);
1773 	return ret;
1774 }
1775 
1776 int wcn36xx_smd_dump_cmd_req(struct wcn36xx *wcn, u32 arg1, u32 arg2,
1777 			     u32 arg3, u32 arg4, u32 arg5)
1778 {
1779 	struct wcn36xx_hal_dump_cmd_req_msg msg_body;
1780 	int ret = 0;
1781 
1782 	mutex_lock(&wcn->hal_mutex);
1783 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_DUMP_COMMAND_REQ);
1784 
1785 	msg_body.arg1 = arg1;
1786 	msg_body.arg2 = arg2;
1787 	msg_body.arg3 = arg3;
1788 	msg_body.arg4 = arg4;
1789 	msg_body.arg5 = arg5;
1790 
1791 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1792 
1793 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1794 	if (ret) {
1795 		wcn36xx_err("Sending hal_dump_cmd failed\n");
1796 		goto out;
1797 	}
1798 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1799 	if (ret) {
1800 		wcn36xx_err("hal_dump_cmd response failed err=%d\n", ret);
1801 		goto out;
1802 	}
1803 out:
1804 	mutex_unlock(&wcn->hal_mutex);
1805 	return ret;
1806 }
1807 
1808 void set_feat_caps(u32 *bitmap, enum place_holder_in_cap_bitmap cap)
1809 {
1810 	int arr_idx, bit_idx;
1811 
1812 	if (cap < 0 || cap > 127) {
1813 		wcn36xx_warn("error cap idx %d\n", cap);
1814 		return;
1815 	}
1816 
1817 	arr_idx = cap / 32;
1818 	bit_idx = cap % 32;
1819 	bitmap[arr_idx] |= (1 << bit_idx);
1820 }
1821 
1822 int get_feat_caps(u32 *bitmap, enum place_holder_in_cap_bitmap cap)
1823 {
1824 	int arr_idx, bit_idx;
1825 	int ret = 0;
1826 
1827 	if (cap < 0 || cap > 127) {
1828 		wcn36xx_warn("error cap idx %d\n", cap);
1829 		return -EINVAL;
1830 	}
1831 
1832 	arr_idx = cap / 32;
1833 	bit_idx = cap % 32;
1834 	ret = (bitmap[arr_idx] & (1 << bit_idx)) ? 1 : 0;
1835 	return ret;
1836 }
1837 
1838 void clear_feat_caps(u32 *bitmap, enum place_holder_in_cap_bitmap cap)
1839 {
1840 	int arr_idx, bit_idx;
1841 
1842 	if (cap < 0 || cap > 127) {
1843 		wcn36xx_warn("error cap idx %d\n", cap);
1844 		return;
1845 	}
1846 
1847 	arr_idx = cap / 32;
1848 	bit_idx = cap % 32;
1849 	bitmap[arr_idx] &= ~(1 << bit_idx);
1850 }
1851 
1852 int wcn36xx_smd_feature_caps_exchange(struct wcn36xx *wcn)
1853 {
1854 	struct wcn36xx_hal_feat_caps_msg msg_body, *rsp;
1855 	int ret = 0, i;
1856 
1857 	mutex_lock(&wcn->hal_mutex);
1858 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_FEATURE_CAPS_EXCHANGE_REQ);
1859 
1860 	set_feat_caps(msg_body.feat_caps, STA_POWERSAVE);
1861 
1862 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1863 
1864 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1865 	if (ret) {
1866 		wcn36xx_err("Sending hal_feature_caps_exchange failed\n");
1867 		goto out;
1868 	}
1869 	if (wcn->hal_rsp_len != sizeof(*rsp)) {
1870 		wcn36xx_err("Invalid hal_feature_caps_exchange response");
1871 		goto out;
1872 	}
1873 
1874 	rsp = (struct wcn36xx_hal_feat_caps_msg *) wcn->hal_buf;
1875 
1876 	for (i = 0; i < WCN36XX_HAL_CAPS_SIZE; i++)
1877 		wcn->fw_feat_caps[i] = rsp->feat_caps[i];
1878 out:
1879 	mutex_unlock(&wcn->hal_mutex);
1880 	return ret;
1881 }
1882 
1883 int wcn36xx_smd_add_ba_session(struct wcn36xx *wcn,
1884 		struct ieee80211_sta *sta,
1885 		u16 tid,
1886 		u16 *ssn,
1887 		u8 direction,
1888 		u8 sta_index)
1889 {
1890 	struct wcn36xx_hal_add_ba_session_req_msg msg_body;
1891 	int ret = 0;
1892 
1893 	mutex_lock(&wcn->hal_mutex);
1894 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_ADD_BA_SESSION_REQ);
1895 
1896 	msg_body.sta_index = sta_index;
1897 	memcpy(&msg_body.mac_addr, sta->addr, ETH_ALEN);
1898 	msg_body.dialog_token = 0x10;
1899 	msg_body.tid = tid;
1900 
1901 	/* Immediate BA because Delayed BA is not supported */
1902 	msg_body.policy = 1;
1903 	msg_body.buffer_size = WCN36XX_AGGR_BUFFER_SIZE;
1904 	msg_body.timeout = 0;
1905 	if (ssn)
1906 		msg_body.ssn = *ssn;
1907 	msg_body.direction = direction;
1908 
1909 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1910 
1911 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1912 	if (ret) {
1913 		wcn36xx_err("Sending hal_add_ba_session failed\n");
1914 		goto out;
1915 	}
1916 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1917 	if (ret) {
1918 		wcn36xx_err("hal_add_ba_session response failed err=%d\n", ret);
1919 		goto out;
1920 	}
1921 out:
1922 	mutex_unlock(&wcn->hal_mutex);
1923 	return ret;
1924 }
1925 
1926 int wcn36xx_smd_add_ba(struct wcn36xx *wcn)
1927 {
1928 	struct wcn36xx_hal_add_ba_req_msg msg_body;
1929 	int ret = 0;
1930 
1931 	mutex_lock(&wcn->hal_mutex);
1932 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_ADD_BA_REQ);
1933 
1934 	msg_body.session_id = 0;
1935 	msg_body.win_size = WCN36XX_AGGR_BUFFER_SIZE;
1936 
1937 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1938 
1939 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1940 	if (ret) {
1941 		wcn36xx_err("Sending hal_add_ba failed\n");
1942 		goto out;
1943 	}
1944 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1945 	if (ret) {
1946 		wcn36xx_err("hal_add_ba response failed err=%d\n", ret);
1947 		goto out;
1948 	}
1949 out:
1950 	mutex_unlock(&wcn->hal_mutex);
1951 	return ret;
1952 }
1953 
1954 int wcn36xx_smd_del_ba(struct wcn36xx *wcn, u16 tid, u8 sta_index)
1955 {
1956 	struct wcn36xx_hal_del_ba_req_msg msg_body;
1957 	int ret = 0;
1958 
1959 	mutex_lock(&wcn->hal_mutex);
1960 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_DEL_BA_REQ);
1961 
1962 	msg_body.sta_index = sta_index;
1963 	msg_body.tid = tid;
1964 	msg_body.direction = 0;
1965 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
1966 
1967 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
1968 	if (ret) {
1969 		wcn36xx_err("Sending hal_del_ba failed\n");
1970 		goto out;
1971 	}
1972 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
1973 	if (ret) {
1974 		wcn36xx_err("hal_del_ba response failed err=%d\n", ret);
1975 		goto out;
1976 	}
1977 out:
1978 	mutex_unlock(&wcn->hal_mutex);
1979 	return ret;
1980 }
1981 
1982 static int wcn36xx_smd_trigger_ba_rsp(void *buf, int len)
1983 {
1984 	struct wcn36xx_hal_trigger_ba_rsp_msg *rsp;
1985 
1986 	if (len < sizeof(*rsp))
1987 		return -EINVAL;
1988 
1989 	rsp = (struct wcn36xx_hal_trigger_ba_rsp_msg *) buf;
1990 	return rsp->status;
1991 }
1992 
1993 int wcn36xx_smd_trigger_ba(struct wcn36xx *wcn, u8 sta_index)
1994 {
1995 	struct wcn36xx_hal_trigger_ba_req_msg msg_body;
1996 	struct wcn36xx_hal_trigger_ba_req_candidate *candidate;
1997 	int ret = 0;
1998 
1999 	mutex_lock(&wcn->hal_mutex);
2000 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_TRIGGER_BA_REQ);
2001 
2002 	msg_body.session_id = 0;
2003 	msg_body.candidate_cnt = 1;
2004 	msg_body.header.len += sizeof(*candidate);
2005 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
2006 
2007 	candidate = (struct wcn36xx_hal_trigger_ba_req_candidate *)
2008 		(wcn->hal_buf + sizeof(msg_body));
2009 	candidate->sta_index = sta_index;
2010 	candidate->tid_bitmap = 1;
2011 
2012 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body.header.len);
2013 	if (ret) {
2014 		wcn36xx_err("Sending hal_trigger_ba failed\n");
2015 		goto out;
2016 	}
2017 	ret = wcn36xx_smd_trigger_ba_rsp(wcn->hal_buf, wcn->hal_rsp_len);
2018 	if (ret) {
2019 		wcn36xx_err("hal_trigger_ba response failed err=%d\n", ret);
2020 		goto out;
2021 	}
2022 out:
2023 	mutex_unlock(&wcn->hal_mutex);
2024 	return ret;
2025 }
2026 
2027 static int wcn36xx_smd_tx_compl_ind(struct wcn36xx *wcn, void *buf, size_t len)
2028 {
2029 	struct wcn36xx_hal_tx_compl_ind_msg *rsp = buf;
2030 
2031 	if (len != sizeof(*rsp)) {
2032 		wcn36xx_warn("Bad TX complete indication\n");
2033 		return -EIO;
2034 	}
2035 
2036 	wcn36xx_dxe_tx_ack_ind(wcn, rsp->status);
2037 
2038 	return 0;
2039 }
2040 
2041 static int wcn36xx_smd_missed_beacon_ind(struct wcn36xx *wcn,
2042 					 void *buf,
2043 					 size_t len)
2044 {
2045 	struct wcn36xx_hal_missed_beacon_ind_msg *rsp = buf;
2046 	struct ieee80211_vif *vif = NULL;
2047 	struct wcn36xx_vif *tmp;
2048 
2049 	/* Old FW does not have bss index */
2050 	if (wcn36xx_is_fw_version(wcn, 1, 2, 2, 24)) {
2051 		list_for_each_entry(tmp, &wcn->vif_list, list) {
2052 			wcn36xx_dbg(WCN36XX_DBG_HAL, "beacon missed bss_index %d\n",
2053 				    tmp->bss_index);
2054 			vif = wcn36xx_priv_to_vif(tmp);
2055 			ieee80211_connection_loss(vif);
2056 		}
2057 		return 0;
2058 	}
2059 
2060 	if (len != sizeof(*rsp)) {
2061 		wcn36xx_warn("Corrupted missed beacon indication\n");
2062 		return -EIO;
2063 	}
2064 
2065 	list_for_each_entry(tmp, &wcn->vif_list, list) {
2066 		if (tmp->bss_index == rsp->bss_index) {
2067 			wcn36xx_dbg(WCN36XX_DBG_HAL, "beacon missed bss_index %d\n",
2068 				    rsp->bss_index);
2069 			vif = wcn36xx_priv_to_vif(tmp);
2070 			ieee80211_connection_loss(vif);
2071 			return 0;
2072 		}
2073 	}
2074 
2075 	wcn36xx_warn("BSS index %d not found\n", rsp->bss_index);
2076 	return -ENOENT;
2077 }
2078 
2079 static int wcn36xx_smd_delete_sta_context_ind(struct wcn36xx *wcn,
2080 					      void *buf,
2081 					      size_t len)
2082 {
2083 	struct wcn36xx_hal_delete_sta_context_ind_msg *rsp = buf;
2084 	struct wcn36xx_vif *tmp;
2085 	struct ieee80211_sta *sta;
2086 
2087 	if (len != sizeof(*rsp)) {
2088 		wcn36xx_warn("Corrupted delete sta indication\n");
2089 		return -EIO;
2090 	}
2091 
2092 	wcn36xx_dbg(WCN36XX_DBG_HAL, "delete station indication %pM index %d\n",
2093 		    rsp->addr2, rsp->sta_id);
2094 
2095 	list_for_each_entry(tmp, &wcn->vif_list, list) {
2096 		rcu_read_lock();
2097 		sta = ieee80211_find_sta(wcn36xx_priv_to_vif(tmp), rsp->addr2);
2098 		if (sta)
2099 			ieee80211_report_low_ack(sta, 0);
2100 		rcu_read_unlock();
2101 		if (sta)
2102 			return 0;
2103 	}
2104 
2105 	wcn36xx_warn("STA with addr %pM and index %d not found\n",
2106 		     rsp->addr2,
2107 		     rsp->sta_id);
2108 	return -ENOENT;
2109 }
2110 
2111 int wcn36xx_smd_update_cfg(struct wcn36xx *wcn, u32 cfg_id, u32 value)
2112 {
2113 	struct wcn36xx_hal_update_cfg_req_msg msg_body, *body;
2114 	size_t len;
2115 	int ret = 0;
2116 
2117 	mutex_lock(&wcn->hal_mutex);
2118 	INIT_HAL_MSG(msg_body, WCN36XX_HAL_UPDATE_CFG_REQ);
2119 
2120 	PREPARE_HAL_BUF(wcn->hal_buf, msg_body);
2121 
2122 	body = (struct wcn36xx_hal_update_cfg_req_msg *) wcn->hal_buf;
2123 	len = msg_body.header.len;
2124 
2125 	put_cfg_tlv_u32(wcn, &len, cfg_id, value);
2126 	body->header.len = len;
2127 	body->len = len - sizeof(*body);
2128 
2129 	ret = wcn36xx_smd_send_and_wait(wcn, body->header.len);
2130 	if (ret) {
2131 		wcn36xx_err("Sending hal_update_cfg failed\n");
2132 		goto out;
2133 	}
2134 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
2135 	if (ret) {
2136 		wcn36xx_err("hal_update_cfg response failed err=%d\n", ret);
2137 		goto out;
2138 	}
2139 out:
2140 	mutex_unlock(&wcn->hal_mutex);
2141 	return ret;
2142 }
2143 
2144 int wcn36xx_smd_set_mc_list(struct wcn36xx *wcn,
2145 			    struct ieee80211_vif *vif,
2146 			    struct wcn36xx_hal_rcv_flt_mc_addr_list_type *fp)
2147 {
2148 	struct wcn36xx_vif *vif_priv = wcn36xx_vif_to_priv(vif);
2149 	struct wcn36xx_hal_rcv_flt_pkt_set_mc_list_req_msg *msg_body = NULL;
2150 	int ret = 0;
2151 
2152 	mutex_lock(&wcn->hal_mutex);
2153 
2154 	msg_body = (struct wcn36xx_hal_rcv_flt_pkt_set_mc_list_req_msg *)
2155 		   wcn->hal_buf;
2156 	init_hal_msg(&msg_body->header, WCN36XX_HAL_8023_MULTICAST_LIST_REQ,
2157 		     sizeof(msg_body->mc_addr_list));
2158 
2159 	/* An empty list means all mc traffic will be received */
2160 	if (fp)
2161 		memcpy(&msg_body->mc_addr_list, fp,
2162 		       sizeof(msg_body->mc_addr_list));
2163 	else
2164 		msg_body->mc_addr_list.mc_addr_count = 0;
2165 
2166 	msg_body->mc_addr_list.bss_index = vif_priv->bss_index;
2167 
2168 	ret = wcn36xx_smd_send_and_wait(wcn, msg_body->header.len);
2169 	if (ret) {
2170 		wcn36xx_err("Sending HAL_8023_MULTICAST_LIST failed\n");
2171 		goto out;
2172 	}
2173 	ret = wcn36xx_smd_rsp_status_check(wcn->hal_buf, wcn->hal_rsp_len);
2174 	if (ret) {
2175 		wcn36xx_err("HAL_8023_MULTICAST_LIST rsp failed err=%d\n", ret);
2176 		goto out;
2177 	}
2178 out:
2179 	mutex_unlock(&wcn->hal_mutex);
2180 	return ret;
2181 }
2182 
2183 static void wcn36xx_smd_rsp_process(struct wcn36xx *wcn, void *buf, size_t len)
2184 {
2185 	struct wcn36xx_hal_msg_header *msg_header = buf;
2186 	struct wcn36xx_hal_ind_msg *msg_ind;
2187 	wcn36xx_dbg_dump(WCN36XX_DBG_SMD_DUMP, "SMD <<< ", buf, len);
2188 
2189 	switch (msg_header->msg_type) {
2190 	case WCN36XX_HAL_START_RSP:
2191 	case WCN36XX_HAL_CONFIG_STA_RSP:
2192 	case WCN36XX_HAL_CONFIG_BSS_RSP:
2193 	case WCN36XX_HAL_ADD_STA_SELF_RSP:
2194 	case WCN36XX_HAL_STOP_RSP:
2195 	case WCN36XX_HAL_DEL_STA_SELF_RSP:
2196 	case WCN36XX_HAL_DELETE_STA_RSP:
2197 	case WCN36XX_HAL_INIT_SCAN_RSP:
2198 	case WCN36XX_HAL_START_SCAN_RSP:
2199 	case WCN36XX_HAL_END_SCAN_RSP:
2200 	case WCN36XX_HAL_FINISH_SCAN_RSP:
2201 	case WCN36XX_HAL_DOWNLOAD_NV_RSP:
2202 	case WCN36XX_HAL_DELETE_BSS_RSP:
2203 	case WCN36XX_HAL_SEND_BEACON_RSP:
2204 	case WCN36XX_HAL_SET_LINK_ST_RSP:
2205 	case WCN36XX_HAL_UPDATE_PROBE_RSP_TEMPLATE_RSP:
2206 	case WCN36XX_HAL_SET_BSSKEY_RSP:
2207 	case WCN36XX_HAL_SET_STAKEY_RSP:
2208 	case WCN36XX_HAL_RMV_STAKEY_RSP:
2209 	case WCN36XX_HAL_RMV_BSSKEY_RSP:
2210 	case WCN36XX_HAL_ENTER_BMPS_RSP:
2211 	case WCN36XX_HAL_SET_POWER_PARAMS_RSP:
2212 	case WCN36XX_HAL_EXIT_BMPS_RSP:
2213 	case WCN36XX_HAL_KEEP_ALIVE_RSP:
2214 	case WCN36XX_HAL_DUMP_COMMAND_RSP:
2215 	case WCN36XX_HAL_ADD_BA_SESSION_RSP:
2216 	case WCN36XX_HAL_ADD_BA_RSP:
2217 	case WCN36XX_HAL_DEL_BA_RSP:
2218 	case WCN36XX_HAL_TRIGGER_BA_RSP:
2219 	case WCN36XX_HAL_UPDATE_CFG_RSP:
2220 	case WCN36XX_HAL_JOIN_RSP:
2221 	case WCN36XX_HAL_UPDATE_SCAN_PARAM_RSP:
2222 	case WCN36XX_HAL_CH_SWITCH_RSP:
2223 	case WCN36XX_HAL_FEATURE_CAPS_EXCHANGE_RSP:
2224 	case WCN36XX_HAL_8023_MULTICAST_LIST_RSP:
2225 		memcpy(wcn->hal_buf, buf, len);
2226 		wcn->hal_rsp_len = len;
2227 		complete(&wcn->hal_rsp_compl);
2228 		break;
2229 
2230 	case WCN36XX_HAL_COEX_IND:
2231 	case WCN36XX_HAL_AVOID_FREQ_RANGE_IND:
2232 	case WCN36XX_HAL_DEL_BA_IND:
2233 	case WCN36XX_HAL_OTA_TX_COMPL_IND:
2234 	case WCN36XX_HAL_MISSED_BEACON_IND:
2235 	case WCN36XX_HAL_DELETE_STA_CONTEXT_IND:
2236 		msg_ind = kmalloc(sizeof(*msg_ind) + len, GFP_KERNEL);
2237 		if (!msg_ind) {
2238 			/*
2239 			 * FIXME: Do something smarter then just
2240 			 * printing an error.
2241 			 */
2242 			wcn36xx_err("Run out of memory while handling SMD_EVENT (%d)\n",
2243 				    msg_header->msg_type);
2244 			break;
2245 		}
2246 
2247 		msg_ind->msg_len = len;
2248 		memcpy(msg_ind->msg, buf, len);
2249 
2250 		spin_lock(&wcn->hal_ind_lock);
2251 		list_add_tail(&msg_ind->list, &wcn->hal_ind_queue);
2252 		queue_work(wcn->hal_ind_wq, &wcn->hal_ind_work);
2253 		spin_unlock(&wcn->hal_ind_lock);
2254 		wcn36xx_dbg(WCN36XX_DBG_HAL, "indication arrived\n");
2255 		break;
2256 	default:
2257 		wcn36xx_err("SMD_EVENT (%d) not supported\n",
2258 			      msg_header->msg_type);
2259 	}
2260 }
2261 static void wcn36xx_ind_smd_work(struct work_struct *work)
2262 {
2263 	struct wcn36xx *wcn =
2264 		container_of(work, struct wcn36xx, hal_ind_work);
2265 	struct wcn36xx_hal_msg_header *msg_header;
2266 	struct wcn36xx_hal_ind_msg *hal_ind_msg;
2267 	unsigned long flags;
2268 
2269 	spin_lock_irqsave(&wcn->hal_ind_lock, flags);
2270 
2271 	hal_ind_msg = list_first_entry(&wcn->hal_ind_queue,
2272 				       struct wcn36xx_hal_ind_msg,
2273 				       list);
2274 
2275 	msg_header = (struct wcn36xx_hal_msg_header *)hal_ind_msg->msg;
2276 
2277 	switch (msg_header->msg_type) {
2278 	case WCN36XX_HAL_COEX_IND:
2279 	case WCN36XX_HAL_DEL_BA_IND:
2280 	case WCN36XX_HAL_AVOID_FREQ_RANGE_IND:
2281 		break;
2282 	case WCN36XX_HAL_OTA_TX_COMPL_IND:
2283 		wcn36xx_smd_tx_compl_ind(wcn,
2284 					 hal_ind_msg->msg,
2285 					 hal_ind_msg->msg_len);
2286 		break;
2287 	case WCN36XX_HAL_MISSED_BEACON_IND:
2288 		wcn36xx_smd_missed_beacon_ind(wcn,
2289 					      hal_ind_msg->msg,
2290 					      hal_ind_msg->msg_len);
2291 		break;
2292 	case WCN36XX_HAL_DELETE_STA_CONTEXT_IND:
2293 		wcn36xx_smd_delete_sta_context_ind(wcn,
2294 						   hal_ind_msg->msg,
2295 						   hal_ind_msg->msg_len);
2296 		break;
2297 	default:
2298 		wcn36xx_err("SMD_EVENT (%d) not supported\n",
2299 			      msg_header->msg_type);
2300 	}
2301 	list_del(wcn->hal_ind_queue.next);
2302 	spin_unlock_irqrestore(&wcn->hal_ind_lock, flags);
2303 	kfree(hal_ind_msg);
2304 }
2305 int wcn36xx_smd_open(struct wcn36xx *wcn)
2306 {
2307 	int ret = 0;
2308 	wcn->hal_ind_wq = create_freezable_workqueue("wcn36xx_smd_ind");
2309 	if (!wcn->hal_ind_wq) {
2310 		wcn36xx_err("failed to allocate wq\n");
2311 		ret = -ENOMEM;
2312 		goto out;
2313 	}
2314 	INIT_WORK(&wcn->hal_ind_work, wcn36xx_ind_smd_work);
2315 	INIT_LIST_HEAD(&wcn->hal_ind_queue);
2316 	spin_lock_init(&wcn->hal_ind_lock);
2317 
2318 	ret = wcn->ctrl_ops->open(wcn, wcn36xx_smd_rsp_process);
2319 	if (ret) {
2320 		wcn36xx_err("failed to open control channel\n");
2321 		goto free_wq;
2322 	}
2323 
2324 	return ret;
2325 
2326 free_wq:
2327 	destroy_workqueue(wcn->hal_ind_wq);
2328 out:
2329 	return ret;
2330 }
2331 
2332 void wcn36xx_smd_close(struct wcn36xx *wcn)
2333 {
2334 	wcn->ctrl_ops->close();
2335 	destroy_workqueue(wcn->hal_ind_wq);
2336 }
2337