xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/wmi.c (revision e8ffca61)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 #include <linux/skbuff.h>
6 #include <linux/ctype.h>
7 #include <net/mac80211.h>
8 #include <net/cfg80211.h>
9 #include <linux/completion.h>
10 #include <linux/if_ether.h>
11 #include <linux/types.h>
12 #include <linux/pci.h>
13 #include <linux/uuid.h>
14 #include <linux/time.h>
15 #include <linux/of.h>
16 #include "core.h"
17 #include "debug.h"
18 #include "mac.h"
19 #include "hw.h"
20 #include "peer.h"
21 
22 struct wmi_tlv_policy {
23 	size_t min_len;
24 };
25 
26 struct wmi_tlv_svc_ready_parse {
27 	bool wmi_svc_bitmap_done;
28 };
29 
30 struct wmi_tlv_dma_ring_caps_parse {
31 	struct wmi_dma_ring_capabilities *dma_ring_caps;
32 	u32 n_dma_ring_caps;
33 };
34 
35 struct wmi_tlv_svc_rdy_ext_parse {
36 	struct ath11k_service_ext_param param;
37 	struct wmi_soc_mac_phy_hw_mode_caps *hw_caps;
38 	struct wmi_hw_mode_capabilities *hw_mode_caps;
39 	u32 n_hw_mode_caps;
40 	u32 tot_phy_id;
41 	struct wmi_hw_mode_capabilities pref_hw_mode_caps;
42 	struct wmi_mac_phy_capabilities *mac_phy_caps;
43 	u32 n_mac_phy_caps;
44 	struct wmi_soc_hal_reg_capabilities *soc_hal_reg_caps;
45 	struct wmi_hal_reg_capabilities_ext *ext_hal_reg_caps;
46 	u32 n_ext_hal_reg_caps;
47 	struct wmi_tlv_dma_ring_caps_parse dma_caps_parse;
48 	bool hw_mode_done;
49 	bool mac_phy_done;
50 	bool ext_hal_reg_done;
51 	bool mac_phy_chainmask_combo_done;
52 	bool mac_phy_chainmask_cap_done;
53 	bool oem_dma_ring_cap_done;
54 	bool dma_ring_cap_done;
55 };
56 
57 struct wmi_tlv_svc_rdy_ext2_parse {
58 	struct wmi_tlv_dma_ring_caps_parse dma_caps_parse;
59 	bool dma_ring_cap_done;
60 };
61 
62 struct wmi_tlv_rdy_parse {
63 	u32 num_extra_mac_addr;
64 };
65 
66 struct wmi_tlv_dma_buf_release_parse {
67 	struct ath11k_wmi_dma_buf_release_fixed_param fixed;
68 	struct wmi_dma_buf_release_entry *buf_entry;
69 	struct wmi_dma_buf_release_meta_data *meta_data;
70 	u32 num_buf_entry;
71 	u32 num_meta;
72 	bool buf_entry_done;
73 	bool meta_data_done;
74 };
75 
76 static const struct wmi_tlv_policy wmi_tlv_policies[] = {
77 	[WMI_TAG_ARRAY_BYTE]
78 		= { .min_len = 0 },
79 	[WMI_TAG_ARRAY_UINT32]
80 		= { .min_len = 0 },
81 	[WMI_TAG_SERVICE_READY_EVENT]
82 		= { .min_len = sizeof(struct wmi_service_ready_event) },
83 	[WMI_TAG_SERVICE_READY_EXT_EVENT]
84 		= { .min_len =  sizeof(struct wmi_service_ready_ext_event) },
85 	[WMI_TAG_SOC_MAC_PHY_HW_MODE_CAPS]
86 		= { .min_len = sizeof(struct wmi_soc_mac_phy_hw_mode_caps) },
87 	[WMI_TAG_SOC_HAL_REG_CAPABILITIES]
88 		= { .min_len = sizeof(struct wmi_soc_hal_reg_capabilities) },
89 	[WMI_TAG_VDEV_START_RESPONSE_EVENT]
90 		= { .min_len = sizeof(struct wmi_vdev_start_resp_event) },
91 	[WMI_TAG_PEER_DELETE_RESP_EVENT]
92 		= { .min_len = sizeof(struct wmi_peer_delete_resp_event) },
93 	[WMI_TAG_OFFLOAD_BCN_TX_STATUS_EVENT]
94 		= { .min_len = sizeof(struct wmi_bcn_tx_status_event) },
95 	[WMI_TAG_VDEV_STOPPED_EVENT]
96 		= { .min_len = sizeof(struct wmi_vdev_stopped_event) },
97 	[WMI_TAG_REG_CHAN_LIST_CC_EVENT]
98 		= { .min_len = sizeof(struct wmi_reg_chan_list_cc_event) },
99 	[WMI_TAG_MGMT_RX_HDR]
100 		= { .min_len = sizeof(struct wmi_mgmt_rx_hdr) },
101 	[WMI_TAG_MGMT_TX_COMPL_EVENT]
102 		= { .min_len = sizeof(struct wmi_mgmt_tx_compl_event) },
103 	[WMI_TAG_SCAN_EVENT]
104 		= { .min_len = sizeof(struct wmi_scan_event) },
105 	[WMI_TAG_PEER_STA_KICKOUT_EVENT]
106 		= { .min_len = sizeof(struct wmi_peer_sta_kickout_event) },
107 	[WMI_TAG_ROAM_EVENT]
108 		= { .min_len = sizeof(struct wmi_roam_event) },
109 	[WMI_TAG_CHAN_INFO_EVENT]
110 		= { .min_len = sizeof(struct wmi_chan_info_event) },
111 	[WMI_TAG_PDEV_BSS_CHAN_INFO_EVENT]
112 		= { .min_len = sizeof(struct wmi_pdev_bss_chan_info_event) },
113 	[WMI_TAG_VDEV_INSTALL_KEY_COMPLETE_EVENT]
114 		= { .min_len = sizeof(struct wmi_vdev_install_key_compl_event) },
115 	[WMI_TAG_READY_EVENT] = {
116 		.min_len = sizeof(struct wmi_ready_event_min) },
117 	[WMI_TAG_SERVICE_AVAILABLE_EVENT]
118 		= {.min_len = sizeof(struct wmi_service_available_event) },
119 	[WMI_TAG_PEER_ASSOC_CONF_EVENT]
120 		= { .min_len = sizeof(struct wmi_peer_assoc_conf_event) },
121 	[WMI_TAG_STATS_EVENT]
122 		= { .min_len = sizeof(struct wmi_stats_event) },
123 	[WMI_TAG_PDEV_CTL_FAILSAFE_CHECK_EVENT]
124 		= { .min_len = sizeof(struct wmi_pdev_ctl_failsafe_chk_event) },
125 	[WMI_TAG_HOST_SWFDA_EVENT] = {
126 		.min_len = sizeof(struct wmi_fils_discovery_event) },
127 	[WMI_TAG_OFFLOAD_PRB_RSP_TX_STATUS_EVENT] = {
128 		.min_len = sizeof(struct wmi_probe_resp_tx_status_event) },
129 	[WMI_TAG_VDEV_DELETE_RESP_EVENT] = {
130 		.min_len = sizeof(struct wmi_vdev_delete_resp_event) },
131 };
132 
133 #define PRIMAP(_hw_mode_) \
134 	[_hw_mode_] = _hw_mode_##_PRI
135 
136 static const int ath11k_hw_mode_pri_map[] = {
137 	PRIMAP(WMI_HOST_HW_MODE_SINGLE),
138 	PRIMAP(WMI_HOST_HW_MODE_DBS),
139 	PRIMAP(WMI_HOST_HW_MODE_SBS_PASSIVE),
140 	PRIMAP(WMI_HOST_HW_MODE_SBS),
141 	PRIMAP(WMI_HOST_HW_MODE_DBS_SBS),
142 	PRIMAP(WMI_HOST_HW_MODE_DBS_OR_SBS),
143 	/* keep last */
144 	PRIMAP(WMI_HOST_HW_MODE_MAX),
145 };
146 
147 static int
148 ath11k_wmi_tlv_iter(struct ath11k_base *ab, const void *ptr, size_t len,
149 		    int (*iter)(struct ath11k_base *ab, u16 tag, u16 len,
150 				const void *ptr, void *data),
151 		    void *data)
152 {
153 	const void *begin = ptr;
154 	const struct wmi_tlv *tlv;
155 	u16 tlv_tag, tlv_len;
156 	int ret;
157 
158 	while (len > 0) {
159 		if (len < sizeof(*tlv)) {
160 			ath11k_err(ab, "wmi tlv parse failure at byte %zd (%zu bytes left, %zu expected)\n",
161 				   ptr - begin, len, sizeof(*tlv));
162 			return -EINVAL;
163 		}
164 
165 		tlv = ptr;
166 		tlv_tag = FIELD_GET(WMI_TLV_TAG, tlv->header);
167 		tlv_len = FIELD_GET(WMI_TLV_LEN, tlv->header);
168 		ptr += sizeof(*tlv);
169 		len -= sizeof(*tlv);
170 
171 		if (tlv_len > len) {
172 			ath11k_err(ab, "wmi tlv parse failure of tag %u at byte %zd (%zu bytes left, %u expected)\n",
173 				   tlv_tag, ptr - begin, len, tlv_len);
174 			return -EINVAL;
175 		}
176 
177 		if (tlv_tag < ARRAY_SIZE(wmi_tlv_policies) &&
178 		    wmi_tlv_policies[tlv_tag].min_len &&
179 		    wmi_tlv_policies[tlv_tag].min_len > tlv_len) {
180 			ath11k_err(ab, "wmi tlv parse failure of tag %u at byte %zd (%u bytes is less than min length %zu)\n",
181 				   tlv_tag, ptr - begin, tlv_len,
182 				   wmi_tlv_policies[tlv_tag].min_len);
183 			return -EINVAL;
184 		}
185 
186 		ret = iter(ab, tlv_tag, tlv_len, ptr, data);
187 		if (ret)
188 			return ret;
189 
190 		ptr += tlv_len;
191 		len -= tlv_len;
192 	}
193 
194 	return 0;
195 }
196 
197 static int ath11k_wmi_tlv_iter_parse(struct ath11k_base *ab, u16 tag, u16 len,
198 				     const void *ptr, void *data)
199 {
200 	const void **tb = data;
201 
202 	if (tag < WMI_TAG_MAX)
203 		tb[tag] = ptr;
204 
205 	return 0;
206 }
207 
208 static int ath11k_wmi_tlv_parse(struct ath11k_base *ar, const void **tb,
209 				const void *ptr, size_t len)
210 {
211 	return ath11k_wmi_tlv_iter(ar, ptr, len, ath11k_wmi_tlv_iter_parse,
212 				   (void *)tb);
213 }
214 
215 static const void **
216 ath11k_wmi_tlv_parse_alloc(struct ath11k_base *ab, const void *ptr,
217 			   size_t len, gfp_t gfp)
218 {
219 	const void **tb;
220 	int ret;
221 
222 	tb = kcalloc(WMI_TAG_MAX, sizeof(*tb), gfp);
223 	if (!tb)
224 		return ERR_PTR(-ENOMEM);
225 
226 	ret = ath11k_wmi_tlv_parse(ab, tb, ptr, len);
227 	if (ret) {
228 		kfree(tb);
229 		return ERR_PTR(ret);
230 	}
231 
232 	return tb;
233 }
234 
235 static int ath11k_wmi_cmd_send_nowait(struct ath11k_pdev_wmi *wmi, struct sk_buff *skb,
236 				      u32 cmd_id)
237 {
238 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
239 	struct ath11k_base *ab = wmi->wmi_ab->ab;
240 	struct wmi_cmd_hdr *cmd_hdr;
241 	int ret;
242 	u32 cmd = 0;
243 
244 	if (skb_push(skb, sizeof(struct wmi_cmd_hdr)) == NULL)
245 		return -ENOMEM;
246 
247 	cmd |= FIELD_PREP(WMI_CMD_HDR_CMD_ID, cmd_id);
248 
249 	cmd_hdr = (struct wmi_cmd_hdr *)skb->data;
250 	cmd_hdr->cmd_id = cmd;
251 
252 	memset(skb_cb, 0, sizeof(*skb_cb));
253 	ret = ath11k_htc_send(&ab->htc, wmi->eid, skb);
254 
255 	if (ret)
256 		goto err_pull;
257 
258 	return 0;
259 
260 err_pull:
261 	skb_pull(skb, sizeof(struct wmi_cmd_hdr));
262 	return ret;
263 }
264 
265 int ath11k_wmi_cmd_send(struct ath11k_pdev_wmi *wmi, struct sk_buff *skb,
266 			u32 cmd_id)
267 {
268 	struct ath11k_wmi_base *wmi_sc = wmi->wmi_ab;
269 	int ret = -EOPNOTSUPP;
270 
271 	might_sleep();
272 
273 	wait_event_timeout(wmi_sc->tx_credits_wq, ({
274 		ret = ath11k_wmi_cmd_send_nowait(wmi, skb, cmd_id);
275 
276 		if (ret && test_bit(ATH11K_FLAG_CRASH_FLUSH, &wmi_sc->ab->dev_flags))
277 			ret = -ESHUTDOWN;
278 
279 		(ret != -EAGAIN);
280 	}), WMI_SEND_TIMEOUT_HZ);
281 
282 	if (ret == -EAGAIN)
283 		ath11k_warn(wmi_sc->ab, "wmi command %d timeout\n", cmd_id);
284 
285 	return ret;
286 }
287 
288 static int ath11k_pull_svc_ready_ext(struct ath11k_pdev_wmi *wmi_handle,
289 				     const void *ptr,
290 				     struct ath11k_service_ext_param *param)
291 {
292 	const struct wmi_service_ready_ext_event *ev = ptr;
293 
294 	if (!ev)
295 		return -EINVAL;
296 
297 	/* Move this to host based bitmap */
298 	param->default_conc_scan_config_bits = ev->default_conc_scan_config_bits;
299 	param->default_fw_config_bits =	ev->default_fw_config_bits;
300 	param->he_cap_info = ev->he_cap_info;
301 	param->mpdu_density = ev->mpdu_density;
302 	param->max_bssid_rx_filters = ev->max_bssid_rx_filters;
303 	memcpy(&param->ppet, &ev->ppet, sizeof(param->ppet));
304 
305 	return 0;
306 }
307 
308 static int
309 ath11k_pull_mac_phy_cap_svc_ready_ext(struct ath11k_pdev_wmi *wmi_handle,
310 				      struct wmi_soc_mac_phy_hw_mode_caps *hw_caps,
311 				      struct wmi_hw_mode_capabilities *wmi_hw_mode_caps,
312 				      struct wmi_soc_hal_reg_capabilities *hal_reg_caps,
313 				      struct wmi_mac_phy_capabilities *wmi_mac_phy_caps,
314 				      u8 hw_mode_id, u8 phy_id,
315 				      struct ath11k_pdev *pdev)
316 {
317 	struct wmi_mac_phy_capabilities *mac_phy_caps;
318 	struct ath11k_band_cap *cap_band;
319 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
320 	u32 phy_map;
321 	u32 hw_idx, phy_idx = 0;
322 
323 	if (!hw_caps || !wmi_hw_mode_caps || !hal_reg_caps)
324 		return -EINVAL;
325 
326 	for (hw_idx = 0; hw_idx < hw_caps->num_hw_modes; hw_idx++) {
327 		if (hw_mode_id == wmi_hw_mode_caps[hw_idx].hw_mode_id)
328 			break;
329 
330 		phy_map = wmi_hw_mode_caps[hw_idx].phy_id_map;
331 		while (phy_map) {
332 			phy_map >>= 1;
333 			phy_idx++;
334 		}
335 	}
336 
337 	if (hw_idx == hw_caps->num_hw_modes)
338 		return -EINVAL;
339 
340 	phy_idx += phy_id;
341 	if (phy_id >= hal_reg_caps->num_phy)
342 		return -EINVAL;
343 
344 	mac_phy_caps = wmi_mac_phy_caps + phy_idx;
345 
346 	pdev->pdev_id = mac_phy_caps->pdev_id;
347 	pdev_cap->supported_bands |= mac_phy_caps->supported_bands;
348 	pdev_cap->ampdu_density = mac_phy_caps->ampdu_density;
349 
350 	/* Take non-zero tx/rx chainmask. If tx/rx chainmask differs from
351 	 * band to band for a single radio, need to see how this should be
352 	 * handled.
353 	 */
354 	if (mac_phy_caps->supported_bands & WMI_HOST_WLAN_2G_CAP) {
355 		pdev_cap->tx_chain_mask = mac_phy_caps->tx_chain_mask_2g;
356 		pdev_cap->rx_chain_mask = mac_phy_caps->rx_chain_mask_2g;
357 	} else if (mac_phy_caps->supported_bands & WMI_HOST_WLAN_5G_CAP) {
358 		pdev_cap->vht_cap = mac_phy_caps->vht_cap_info_5g;
359 		pdev_cap->vht_mcs = mac_phy_caps->vht_supp_mcs_5g;
360 		pdev_cap->he_mcs = mac_phy_caps->he_supp_mcs_5g;
361 		pdev_cap->tx_chain_mask = mac_phy_caps->tx_chain_mask_5g;
362 		pdev_cap->rx_chain_mask = mac_phy_caps->rx_chain_mask_5g;
363 		pdev_cap->nss_ratio_enabled =
364 			WMI_NSS_RATIO_ENABLE_DISABLE_GET(mac_phy_caps->nss_ratio);
365 		pdev_cap->nss_ratio_info =
366 			WMI_NSS_RATIO_INFO_GET(mac_phy_caps->nss_ratio);
367 	} else {
368 		return -EINVAL;
369 	}
370 
371 	/* tx/rx chainmask reported from fw depends on the actual hw chains used,
372 	 * For example, for 4x4 capable macphys, first 4 chains can be used for first
373 	 * mac and the remaing 4 chains can be used for the second mac or vice-versa.
374 	 * In this case, tx/rx chainmask 0xf will be advertised for first mac and 0xf0
375 	 * will be advertised for second mac or vice-versa. Compute the shift value
376 	 * for tx/rx chainmask which will be used to advertise supported ht/vht rates to
377 	 * mac80211.
378 	 */
379 	pdev_cap->tx_chain_mask_shift =
380 			find_first_bit((unsigned long *)&pdev_cap->tx_chain_mask, 32);
381 	pdev_cap->rx_chain_mask_shift =
382 			find_first_bit((unsigned long *)&pdev_cap->rx_chain_mask, 32);
383 
384 	if (mac_phy_caps->supported_bands & WMI_HOST_WLAN_2G_CAP) {
385 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
386 		cap_band->phy_id = mac_phy_caps->phy_id;
387 		cap_band->max_bw_supported = mac_phy_caps->max_bw_supported_2g;
388 		cap_band->ht_cap_info = mac_phy_caps->ht_cap_info_2g;
389 		cap_band->he_cap_info[0] = mac_phy_caps->he_cap_info_2g;
390 		cap_band->he_cap_info[1] = mac_phy_caps->he_cap_info_2g_ext;
391 		cap_band->he_mcs = mac_phy_caps->he_supp_mcs_2g;
392 		memcpy(cap_band->he_cap_phy_info, &mac_phy_caps->he_cap_phy_info_2g,
393 		       sizeof(u32) * PSOC_HOST_MAX_PHY_SIZE);
394 		memcpy(&cap_band->he_ppet, &mac_phy_caps->he_ppet2g,
395 		       sizeof(struct ath11k_ppe_threshold));
396 	}
397 
398 	if (mac_phy_caps->supported_bands & WMI_HOST_WLAN_5G_CAP) {
399 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
400 		cap_band->phy_id = mac_phy_caps->phy_id;
401 		cap_band->max_bw_supported = mac_phy_caps->max_bw_supported_5g;
402 		cap_band->ht_cap_info = mac_phy_caps->ht_cap_info_5g;
403 		cap_band->he_cap_info[0] = mac_phy_caps->he_cap_info_5g;
404 		cap_band->he_cap_info[1] = mac_phy_caps->he_cap_info_5g_ext;
405 		cap_band->he_mcs = mac_phy_caps->he_supp_mcs_5g;
406 		memcpy(cap_band->he_cap_phy_info, &mac_phy_caps->he_cap_phy_info_5g,
407 		       sizeof(u32) * PSOC_HOST_MAX_PHY_SIZE);
408 		memcpy(&cap_band->he_ppet, &mac_phy_caps->he_ppet5g,
409 		       sizeof(struct ath11k_ppe_threshold));
410 
411 		cap_band = &pdev_cap->band[NL80211_BAND_6GHZ];
412 		cap_band->max_bw_supported = mac_phy_caps->max_bw_supported_5g;
413 		cap_band->ht_cap_info = mac_phy_caps->ht_cap_info_5g;
414 		cap_band->he_cap_info[0] = mac_phy_caps->he_cap_info_5g;
415 		cap_band->he_cap_info[1] = mac_phy_caps->he_cap_info_5g_ext;
416 		cap_band->he_mcs = mac_phy_caps->he_supp_mcs_5g;
417 		memcpy(cap_band->he_cap_phy_info, &mac_phy_caps->he_cap_phy_info_5g,
418 		       sizeof(u32) * PSOC_HOST_MAX_PHY_SIZE);
419 		memcpy(&cap_band->he_ppet, &mac_phy_caps->he_ppet5g,
420 		       sizeof(struct ath11k_ppe_threshold));
421 	}
422 
423 	return 0;
424 }
425 
426 static int
427 ath11k_pull_reg_cap_svc_rdy_ext(struct ath11k_pdev_wmi *wmi_handle,
428 				struct wmi_soc_hal_reg_capabilities *reg_caps,
429 				struct wmi_hal_reg_capabilities_ext *wmi_ext_reg_cap,
430 				u8 phy_idx,
431 				struct ath11k_hal_reg_capabilities_ext *param)
432 {
433 	struct wmi_hal_reg_capabilities_ext *ext_reg_cap;
434 
435 	if (!reg_caps || !wmi_ext_reg_cap)
436 		return -EINVAL;
437 
438 	if (phy_idx >= reg_caps->num_phy)
439 		return -EINVAL;
440 
441 	ext_reg_cap = &wmi_ext_reg_cap[phy_idx];
442 
443 	param->phy_id = ext_reg_cap->phy_id;
444 	param->eeprom_reg_domain = ext_reg_cap->eeprom_reg_domain;
445 	param->eeprom_reg_domain_ext =
446 			      ext_reg_cap->eeprom_reg_domain_ext;
447 	param->regcap1 = ext_reg_cap->regcap1;
448 	param->regcap2 = ext_reg_cap->regcap2;
449 	/* check if param->wireless_mode is needed */
450 	param->low_2ghz_chan = ext_reg_cap->low_2ghz_chan;
451 	param->high_2ghz_chan = ext_reg_cap->high_2ghz_chan;
452 	param->low_5ghz_chan = ext_reg_cap->low_5ghz_chan;
453 	param->high_5ghz_chan = ext_reg_cap->high_5ghz_chan;
454 
455 	return 0;
456 }
457 
458 static int ath11k_pull_service_ready_tlv(struct ath11k_base *ab,
459 					 const void *evt_buf,
460 					 struct ath11k_targ_cap *cap)
461 {
462 	const struct wmi_service_ready_event *ev = evt_buf;
463 
464 	if (!ev) {
465 		ath11k_err(ab, "%s: failed by NULL param\n",
466 			   __func__);
467 		return -EINVAL;
468 	}
469 
470 	cap->phy_capability = ev->phy_capability;
471 	cap->max_frag_entry = ev->max_frag_entry;
472 	cap->num_rf_chains = ev->num_rf_chains;
473 	cap->ht_cap_info = ev->ht_cap_info;
474 	cap->vht_cap_info = ev->vht_cap_info;
475 	cap->vht_supp_mcs = ev->vht_supp_mcs;
476 	cap->hw_min_tx_power = ev->hw_min_tx_power;
477 	cap->hw_max_tx_power = ev->hw_max_tx_power;
478 	cap->sys_cap_info = ev->sys_cap_info;
479 	cap->min_pkt_size_enable = ev->min_pkt_size_enable;
480 	cap->max_bcn_ie_size = ev->max_bcn_ie_size;
481 	cap->max_num_scan_channels = ev->max_num_scan_channels;
482 	cap->max_supported_macs = ev->max_supported_macs;
483 	cap->wmi_fw_sub_feat_caps = ev->wmi_fw_sub_feat_caps;
484 	cap->txrx_chainmask = ev->txrx_chainmask;
485 	cap->default_dbs_hw_mode_index = ev->default_dbs_hw_mode_index;
486 	cap->num_msdu_desc = ev->num_msdu_desc;
487 
488 	return 0;
489 }
490 
491 /* Save the wmi_service_bitmap into a linear bitmap. The wmi_services in
492  * wmi_service ready event are advertised in b0-b3 (LSB 4-bits) of each
493  * 4-byte word.
494  */
495 static void ath11k_wmi_service_bitmap_copy(struct ath11k_pdev_wmi *wmi,
496 					   const u32 *wmi_svc_bm)
497 {
498 	int i, j;
499 
500 	for (i = 0, j = 0; i < WMI_SERVICE_BM_SIZE && j < WMI_MAX_SERVICE; i++) {
501 		do {
502 			if (wmi_svc_bm[i] & BIT(j % WMI_SERVICE_BITS_IN_SIZE32))
503 				set_bit(j, wmi->wmi_ab->svc_map);
504 		} while (++j % WMI_SERVICE_BITS_IN_SIZE32);
505 	}
506 }
507 
508 static int ath11k_wmi_tlv_svc_rdy_parse(struct ath11k_base *ab, u16 tag, u16 len,
509 					const void *ptr, void *data)
510 {
511 	struct wmi_tlv_svc_ready_parse *svc_ready = data;
512 	struct ath11k_pdev_wmi *wmi_handle = &ab->wmi_ab.wmi[0];
513 	u16 expect_len;
514 
515 	switch (tag) {
516 	case WMI_TAG_SERVICE_READY_EVENT:
517 		if (ath11k_pull_service_ready_tlv(ab, ptr, &ab->target_caps))
518 			return -EINVAL;
519 		break;
520 
521 	case WMI_TAG_ARRAY_UINT32:
522 		if (!svc_ready->wmi_svc_bitmap_done) {
523 			expect_len = WMI_SERVICE_BM_SIZE * sizeof(u32);
524 			if (len < expect_len) {
525 				ath11k_warn(ab, "invalid len %d for the tag 0x%x\n",
526 					    len, tag);
527 				return -EINVAL;
528 			}
529 
530 			ath11k_wmi_service_bitmap_copy(wmi_handle, ptr);
531 
532 			svc_ready->wmi_svc_bitmap_done = true;
533 		}
534 		break;
535 	default:
536 		break;
537 	}
538 
539 	return 0;
540 }
541 
542 static int ath11k_service_ready_event(struct ath11k_base *ab, struct sk_buff *skb)
543 {
544 	struct wmi_tlv_svc_ready_parse svc_ready = { };
545 	int ret;
546 
547 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
548 				  ath11k_wmi_tlv_svc_rdy_parse,
549 				  &svc_ready);
550 	if (ret) {
551 		ath11k_warn(ab, "failed to parse tlv %d\n", ret);
552 		return ret;
553 	}
554 
555 	return 0;
556 }
557 
558 struct sk_buff *ath11k_wmi_alloc_skb(struct ath11k_wmi_base *wmi_sc, u32 len)
559 {
560 	struct sk_buff *skb;
561 	struct ath11k_base *ab = wmi_sc->ab;
562 	u32 round_len = roundup(len, 4);
563 
564 	skb = ath11k_htc_alloc_skb(ab, WMI_SKB_HEADROOM + round_len);
565 	if (!skb)
566 		return NULL;
567 
568 	skb_reserve(skb, WMI_SKB_HEADROOM);
569 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
570 		ath11k_warn(ab, "unaligned WMI skb data\n");
571 
572 	skb_put(skb, round_len);
573 	memset(skb->data, 0, round_len);
574 
575 	return skb;
576 }
577 
578 int ath11k_wmi_mgmt_send(struct ath11k *ar, u32 vdev_id, u32 buf_id,
579 			 struct sk_buff *frame)
580 {
581 	struct ath11k_pdev_wmi *wmi = ar->wmi;
582 	struct wmi_mgmt_send_cmd *cmd;
583 	struct wmi_tlv *frame_tlv;
584 	struct sk_buff *skb;
585 	u32 buf_len;
586 	int ret, len;
587 
588 	buf_len = frame->len < WMI_MGMT_SEND_DOWNLD_LEN ?
589 		  frame->len : WMI_MGMT_SEND_DOWNLD_LEN;
590 
591 	len = sizeof(*cmd) + sizeof(*frame_tlv) + roundup(buf_len, 4);
592 
593 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
594 	if (!skb)
595 		return -ENOMEM;
596 
597 	cmd = (struct wmi_mgmt_send_cmd *)skb->data;
598 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_MGMT_TX_SEND_CMD) |
599 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
600 	cmd->vdev_id = vdev_id;
601 	cmd->desc_id = buf_id;
602 	cmd->chanfreq = 0;
603 	cmd->paddr_lo = lower_32_bits(ATH11K_SKB_CB(frame)->paddr);
604 	cmd->paddr_hi = upper_32_bits(ATH11K_SKB_CB(frame)->paddr);
605 	cmd->frame_len = frame->len;
606 	cmd->buf_len = buf_len;
607 	cmd->tx_params_valid = 0;
608 
609 	frame_tlv = (struct wmi_tlv *)(skb->data + sizeof(*cmd));
610 	frame_tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
611 			    FIELD_PREP(WMI_TLV_LEN, buf_len);
612 
613 	memcpy(frame_tlv->value, frame->data, buf_len);
614 
615 	ath11k_ce_byte_swap(frame_tlv->value, buf_len);
616 
617 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_MGMT_TX_SEND_CMDID);
618 	if (ret) {
619 		ath11k_warn(ar->ab,
620 			    "failed to submit WMI_MGMT_TX_SEND_CMDID cmd\n");
621 		dev_kfree_skb(skb);
622 	}
623 
624 	return ret;
625 }
626 
627 int ath11k_wmi_vdev_create(struct ath11k *ar, u8 *macaddr,
628 			   struct vdev_create_params *param)
629 {
630 	struct ath11k_pdev_wmi *wmi = ar->wmi;
631 	struct wmi_vdev_create_cmd *cmd;
632 	struct sk_buff *skb;
633 	struct wmi_vdev_txrx_streams *txrx_streams;
634 	struct wmi_tlv *tlv;
635 	int ret, len;
636 	void *ptr;
637 
638 	/* It can be optimized my sending tx/rx chain configuration
639 	 * only for supported bands instead of always sending it for
640 	 * both the bands.
641 	 */
642 	len = sizeof(*cmd) + TLV_HDR_SIZE +
643 		(WMI_NUM_SUPPORTED_BAND_MAX * sizeof(*txrx_streams));
644 
645 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
646 	if (!skb)
647 		return -ENOMEM;
648 
649 	cmd = (struct wmi_vdev_create_cmd *)skb->data;
650 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_CREATE_CMD) |
651 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
652 
653 	cmd->vdev_id = param->if_id;
654 	cmd->vdev_type = param->type;
655 	cmd->vdev_subtype = param->subtype;
656 	cmd->num_cfg_txrx_streams = WMI_NUM_SUPPORTED_BAND_MAX;
657 	cmd->pdev_id = param->pdev_id;
658 	ether_addr_copy(cmd->vdev_macaddr.addr, macaddr);
659 
660 	ptr = skb->data + sizeof(*cmd);
661 	len = WMI_NUM_SUPPORTED_BAND_MAX * sizeof(*txrx_streams);
662 
663 	tlv = ptr;
664 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
665 		      FIELD_PREP(WMI_TLV_LEN, len);
666 
667 	ptr += TLV_HDR_SIZE;
668 	txrx_streams = ptr;
669 	len = sizeof(*txrx_streams);
670 	txrx_streams->tlv_header =
671 		FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_TXRX_STREAMS) |
672 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
673 	txrx_streams->band = WMI_TPC_CHAINMASK_CONFIG_BAND_2G;
674 	txrx_streams->supported_tx_streams =
675 				 param->chains[NL80211_BAND_2GHZ].tx;
676 	txrx_streams->supported_rx_streams =
677 				 param->chains[NL80211_BAND_2GHZ].rx;
678 
679 	txrx_streams++;
680 	txrx_streams->tlv_header =
681 		FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_TXRX_STREAMS) |
682 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
683 	txrx_streams->band = WMI_TPC_CHAINMASK_CONFIG_BAND_5G;
684 	txrx_streams->supported_tx_streams =
685 				 param->chains[NL80211_BAND_5GHZ].tx;
686 	txrx_streams->supported_rx_streams =
687 				 param->chains[NL80211_BAND_5GHZ].rx;
688 
689 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_CREATE_CMDID);
690 	if (ret) {
691 		ath11k_warn(ar->ab,
692 			    "failed to submit WMI_VDEV_CREATE_CMDID\n");
693 		dev_kfree_skb(skb);
694 	}
695 
696 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
697 		   "WMI vdev create: id %d type %d subtype %d macaddr %pM pdevid %d\n",
698 		   param->if_id, param->type, param->subtype,
699 		   macaddr, param->pdev_id);
700 
701 	return ret;
702 }
703 
704 int ath11k_wmi_vdev_delete(struct ath11k *ar, u8 vdev_id)
705 {
706 	struct ath11k_pdev_wmi *wmi = ar->wmi;
707 	struct wmi_vdev_delete_cmd *cmd;
708 	struct sk_buff *skb;
709 	int ret;
710 
711 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
712 	if (!skb)
713 		return -ENOMEM;
714 
715 	cmd = (struct wmi_vdev_delete_cmd *)skb->data;
716 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_DELETE_CMD) |
717 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
718 	cmd->vdev_id = vdev_id;
719 
720 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_DELETE_CMDID);
721 	if (ret) {
722 		ath11k_warn(ar->ab, "failed to submit WMI_VDEV_DELETE_CMDID\n");
723 		dev_kfree_skb(skb);
724 	}
725 
726 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "WMI vdev delete id %d\n", vdev_id);
727 
728 	return ret;
729 }
730 
731 int ath11k_wmi_vdev_stop(struct ath11k *ar, u8 vdev_id)
732 {
733 	struct ath11k_pdev_wmi *wmi = ar->wmi;
734 	struct wmi_vdev_stop_cmd *cmd;
735 	struct sk_buff *skb;
736 	int ret;
737 
738 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
739 	if (!skb)
740 		return -ENOMEM;
741 
742 	cmd = (struct wmi_vdev_stop_cmd *)skb->data;
743 
744 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_STOP_CMD) |
745 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
746 	cmd->vdev_id = vdev_id;
747 
748 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_STOP_CMDID);
749 	if (ret) {
750 		ath11k_warn(ar->ab, "failed to submit WMI_VDEV_STOP cmd\n");
751 		dev_kfree_skb(skb);
752 	}
753 
754 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "WMI vdev stop id 0x%x\n", vdev_id);
755 
756 	return ret;
757 }
758 
759 int ath11k_wmi_vdev_down(struct ath11k *ar, u8 vdev_id)
760 {
761 	struct ath11k_pdev_wmi *wmi = ar->wmi;
762 	struct wmi_vdev_down_cmd *cmd;
763 	struct sk_buff *skb;
764 	int ret;
765 
766 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
767 	if (!skb)
768 		return -ENOMEM;
769 
770 	cmd = (struct wmi_vdev_down_cmd *)skb->data;
771 
772 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_DOWN_CMD) |
773 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
774 	cmd->vdev_id = vdev_id;
775 
776 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_DOWN_CMDID);
777 	if (ret) {
778 		ath11k_warn(ar->ab, "failed to submit WMI_VDEV_DOWN cmd\n");
779 		dev_kfree_skb(skb);
780 	}
781 
782 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "WMI vdev down id 0x%x\n", vdev_id);
783 
784 	return ret;
785 }
786 
787 static void ath11k_wmi_put_wmi_channel(struct wmi_channel *chan,
788 				       struct wmi_vdev_start_req_arg *arg)
789 {
790 	u32 center_freq1 = arg->channel.band_center_freq1;
791 
792 	memset(chan, 0, sizeof(*chan));
793 
794 	chan->mhz = arg->channel.freq;
795 	chan->band_center_freq1 = arg->channel.band_center_freq1;
796 
797 	if (arg->channel.mode == MODE_11AX_HE160) {
798 		if (arg->channel.freq > arg->channel.band_center_freq1)
799 			chan->band_center_freq1 = center_freq1 + 40;
800 		else
801 			chan->band_center_freq1 = center_freq1 - 40;
802 
803 		chan->band_center_freq2 = arg->channel.band_center_freq1;
804 
805 	} else if (arg->channel.mode == MODE_11AC_VHT80_80) {
806 		chan->band_center_freq2 = arg->channel.band_center_freq2;
807 	} else {
808 		chan->band_center_freq2 = 0;
809 	}
810 
811 	chan->info |= FIELD_PREP(WMI_CHAN_INFO_MODE, arg->channel.mode);
812 	if (arg->channel.passive)
813 		chan->info |= WMI_CHAN_INFO_PASSIVE;
814 	if (arg->channel.allow_ibss)
815 		chan->info |= WMI_CHAN_INFO_ADHOC_ALLOWED;
816 	if (arg->channel.allow_ht)
817 		chan->info |= WMI_CHAN_INFO_ALLOW_HT;
818 	if (arg->channel.allow_vht)
819 		chan->info |= WMI_CHAN_INFO_ALLOW_VHT;
820 	if (arg->channel.allow_he)
821 		chan->info |= WMI_CHAN_INFO_ALLOW_HE;
822 	if (arg->channel.ht40plus)
823 		chan->info |= WMI_CHAN_INFO_HT40_PLUS;
824 	if (arg->channel.chan_radar)
825 		chan->info |= WMI_CHAN_INFO_DFS;
826 	if (arg->channel.freq2_radar)
827 		chan->info |= WMI_CHAN_INFO_DFS_FREQ2;
828 
829 	chan->reg_info_1 = FIELD_PREP(WMI_CHAN_REG_INFO1_MAX_PWR,
830 				      arg->channel.max_power) |
831 		FIELD_PREP(WMI_CHAN_REG_INFO1_MAX_REG_PWR,
832 			   arg->channel.max_reg_power);
833 
834 	chan->reg_info_2 = FIELD_PREP(WMI_CHAN_REG_INFO2_ANT_MAX,
835 				      arg->channel.max_antenna_gain) |
836 		FIELD_PREP(WMI_CHAN_REG_INFO2_MAX_TX_PWR,
837 			   arg->channel.max_power);
838 }
839 
840 int ath11k_wmi_vdev_start(struct ath11k *ar, struct wmi_vdev_start_req_arg *arg,
841 			  bool restart)
842 {
843 	struct ath11k_pdev_wmi *wmi = ar->wmi;
844 	struct wmi_vdev_start_request_cmd *cmd;
845 	struct sk_buff *skb;
846 	struct wmi_channel *chan;
847 	struct wmi_tlv *tlv;
848 	void *ptr;
849 	int ret, len;
850 
851 	if (WARN_ON(arg->ssid_len > sizeof(cmd->ssid.ssid)))
852 		return -EINVAL;
853 
854 	len = sizeof(*cmd) + sizeof(*chan) + TLV_HDR_SIZE;
855 
856 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
857 	if (!skb)
858 		return -ENOMEM;
859 
860 	cmd = (struct wmi_vdev_start_request_cmd *)skb->data;
861 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
862 				     WMI_TAG_VDEV_START_REQUEST_CMD) |
863 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
864 	cmd->vdev_id = arg->vdev_id;
865 	cmd->beacon_interval = arg->bcn_intval;
866 	cmd->bcn_tx_rate = arg->bcn_tx_rate;
867 	cmd->dtim_period = arg->dtim_period;
868 	cmd->num_noa_descriptors = arg->num_noa_descriptors;
869 	cmd->preferred_rx_streams = arg->pref_rx_streams;
870 	cmd->preferred_tx_streams = arg->pref_tx_streams;
871 	cmd->cac_duration_ms = arg->cac_duration_ms;
872 	cmd->regdomain = arg->regdomain;
873 	cmd->he_ops = arg->he_ops;
874 
875 	if (!restart) {
876 		if (arg->ssid) {
877 			cmd->ssid.ssid_len = arg->ssid_len;
878 			memcpy(cmd->ssid.ssid, arg->ssid, arg->ssid_len);
879 		}
880 		if (arg->hidden_ssid)
881 			cmd->flags |= WMI_VDEV_START_HIDDEN_SSID;
882 		if (arg->pmf_enabled)
883 			cmd->flags |= WMI_VDEV_START_PMF_ENABLED;
884 	}
885 
886 	cmd->flags |= WMI_VDEV_START_LDPC_RX_ENABLED;
887 	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
888 		cmd->flags |= WMI_VDEV_START_HW_ENCRYPTION_DISABLED;
889 
890 	ptr = skb->data + sizeof(*cmd);
891 	chan = ptr;
892 
893 	ath11k_wmi_put_wmi_channel(chan, arg);
894 
895 	chan->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_CHANNEL) |
896 			   FIELD_PREP(WMI_TLV_LEN,
897 				      sizeof(*chan) - TLV_HDR_SIZE);
898 	ptr += sizeof(*chan);
899 
900 	tlv = ptr;
901 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
902 		      FIELD_PREP(WMI_TLV_LEN, 0);
903 
904 	/* Note: This is a nested TLV containing:
905 	 * [wmi_tlv][wmi_p2p_noa_descriptor][wmi_tlv]..
906 	 */
907 
908 	ptr += sizeof(*tlv);
909 
910 	if (restart)
911 		ret = ath11k_wmi_cmd_send(wmi, skb,
912 					  WMI_VDEV_RESTART_REQUEST_CMDID);
913 	else
914 		ret = ath11k_wmi_cmd_send(wmi, skb,
915 					  WMI_VDEV_START_REQUEST_CMDID);
916 	if (ret) {
917 		ath11k_warn(ar->ab, "failed to submit vdev_%s cmd\n",
918 			    restart ? "restart" : "start");
919 		dev_kfree_skb(skb);
920 	}
921 
922 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "vdev %s id 0x%x freq 0x%x mode 0x%x\n",
923 		   restart ? "restart" : "start", arg->vdev_id,
924 		   arg->channel.freq, arg->channel.mode);
925 
926 	return ret;
927 }
928 
929 int ath11k_wmi_vdev_up(struct ath11k *ar, u32 vdev_id, u32 aid, const u8 *bssid)
930 {
931 	struct ath11k_pdev_wmi *wmi = ar->wmi;
932 	struct wmi_vdev_up_cmd *cmd;
933 	struct sk_buff *skb;
934 	int ret;
935 
936 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
937 	if (!skb)
938 		return -ENOMEM;
939 
940 	cmd = (struct wmi_vdev_up_cmd *)skb->data;
941 
942 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_UP_CMD) |
943 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
944 	cmd->vdev_id = vdev_id;
945 	cmd->vdev_assoc_id = aid;
946 
947 	ether_addr_copy(cmd->vdev_bssid.addr, bssid);
948 
949 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_UP_CMDID);
950 	if (ret) {
951 		ath11k_warn(ar->ab, "failed to submit WMI_VDEV_UP cmd\n");
952 		dev_kfree_skb(skb);
953 	}
954 
955 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
956 		   "WMI mgmt vdev up id 0x%x assoc id %d bssid %pM\n",
957 		   vdev_id, aid, bssid);
958 
959 	return ret;
960 }
961 
962 int ath11k_wmi_send_peer_create_cmd(struct ath11k *ar,
963 				    struct peer_create_params *param)
964 {
965 	struct ath11k_pdev_wmi *wmi = ar->wmi;
966 	struct wmi_peer_create_cmd *cmd;
967 	struct sk_buff *skb;
968 	int ret;
969 
970 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
971 	if (!skb)
972 		return -ENOMEM;
973 
974 	cmd = (struct wmi_peer_create_cmd *)skb->data;
975 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PEER_CREATE_CMD) |
976 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
977 
978 	ether_addr_copy(cmd->peer_macaddr.addr, param->peer_addr);
979 	cmd->peer_type = param->peer_type;
980 	cmd->vdev_id = param->vdev_id;
981 
982 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PEER_CREATE_CMDID);
983 	if (ret) {
984 		ath11k_warn(ar->ab, "failed to submit WMI_PEER_CREATE cmd\n");
985 		dev_kfree_skb(skb);
986 	}
987 
988 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
989 		   "WMI peer create vdev_id %d peer_addr %pM\n",
990 		   param->vdev_id, param->peer_addr);
991 
992 	return ret;
993 }
994 
995 int ath11k_wmi_send_peer_delete_cmd(struct ath11k *ar,
996 				    const u8 *peer_addr, u8 vdev_id)
997 {
998 	struct ath11k_pdev_wmi *wmi = ar->wmi;
999 	struct wmi_peer_delete_cmd *cmd;
1000 	struct sk_buff *skb;
1001 	int ret;
1002 
1003 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1004 	if (!skb)
1005 		return -ENOMEM;
1006 
1007 	cmd = (struct wmi_peer_delete_cmd *)skb->data;
1008 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PEER_DELETE_CMD) |
1009 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1010 
1011 	ether_addr_copy(cmd->peer_macaddr.addr, peer_addr);
1012 	cmd->vdev_id = vdev_id;
1013 
1014 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1015 		   "WMI peer delete vdev_id %d peer_addr %pM\n",
1016 		   vdev_id,  peer_addr);
1017 
1018 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PEER_DELETE_CMDID);
1019 	if (ret) {
1020 		ath11k_warn(ar->ab, "failed to send WMI_PEER_DELETE cmd\n");
1021 		dev_kfree_skb(skb);
1022 	}
1023 
1024 	return ret;
1025 }
1026 
1027 int ath11k_wmi_send_pdev_set_regdomain(struct ath11k *ar,
1028 				       struct pdev_set_regdomain_params *param)
1029 {
1030 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1031 	struct wmi_pdev_set_regdomain_cmd *cmd;
1032 	struct sk_buff *skb;
1033 	int ret;
1034 
1035 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1036 	if (!skb)
1037 		return -ENOMEM;
1038 
1039 	cmd = (struct wmi_pdev_set_regdomain_cmd *)skb->data;
1040 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1041 				     WMI_TAG_PDEV_SET_REGDOMAIN_CMD) |
1042 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1043 
1044 	cmd->reg_domain = param->current_rd_in_use;
1045 	cmd->reg_domain_2g = param->current_rd_2g;
1046 	cmd->reg_domain_5g = param->current_rd_5g;
1047 	cmd->conformance_test_limit_2g = param->ctl_2g;
1048 	cmd->conformance_test_limit_5g = param->ctl_5g;
1049 	cmd->dfs_domain = param->dfs_domain;
1050 	cmd->pdev_id = param->pdev_id;
1051 
1052 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1053 		   "WMI pdev regd rd %d rd2g %d rd5g %d domain %d pdev id %d\n",
1054 		   param->current_rd_in_use, param->current_rd_2g,
1055 		   param->current_rd_5g, param->dfs_domain, param->pdev_id);
1056 
1057 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PDEV_SET_REGDOMAIN_CMDID);
1058 	if (ret) {
1059 		ath11k_warn(ar->ab,
1060 			    "failed to send WMI_PDEV_SET_REGDOMAIN cmd\n");
1061 		dev_kfree_skb(skb);
1062 	}
1063 
1064 	return ret;
1065 }
1066 
1067 int ath11k_wmi_set_peer_param(struct ath11k *ar, const u8 *peer_addr,
1068 			      u32 vdev_id, u32 param_id, u32 param_val)
1069 {
1070 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1071 	struct wmi_peer_set_param_cmd *cmd;
1072 	struct sk_buff *skb;
1073 	int ret;
1074 
1075 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1076 	if (!skb)
1077 		return -ENOMEM;
1078 
1079 	cmd = (struct wmi_peer_set_param_cmd *)skb->data;
1080 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PEER_SET_PARAM_CMD) |
1081 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1082 	ether_addr_copy(cmd->peer_macaddr.addr, peer_addr);
1083 	cmd->vdev_id = vdev_id;
1084 	cmd->param_id = param_id;
1085 	cmd->param_value = param_val;
1086 
1087 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PEER_SET_PARAM_CMDID);
1088 	if (ret) {
1089 		ath11k_warn(ar->ab, "failed to send WMI_PEER_SET_PARAM cmd\n");
1090 		dev_kfree_skb(skb);
1091 	}
1092 
1093 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1094 		   "WMI vdev %d peer 0x%pM set param %d value %d\n",
1095 		   vdev_id, peer_addr, param_id, param_val);
1096 
1097 	return ret;
1098 }
1099 
1100 int ath11k_wmi_send_peer_flush_tids_cmd(struct ath11k *ar,
1101 					u8 peer_addr[ETH_ALEN],
1102 					struct peer_flush_params *param)
1103 {
1104 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1105 	struct wmi_peer_flush_tids_cmd *cmd;
1106 	struct sk_buff *skb;
1107 	int ret;
1108 
1109 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1110 	if (!skb)
1111 		return -ENOMEM;
1112 
1113 	cmd = (struct wmi_peer_flush_tids_cmd *)skb->data;
1114 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PEER_FLUSH_TIDS_CMD) |
1115 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1116 
1117 	ether_addr_copy(cmd->peer_macaddr.addr, peer_addr);
1118 	cmd->peer_tid_bitmap = param->peer_tid_bitmap;
1119 	cmd->vdev_id = param->vdev_id;
1120 
1121 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PEER_FLUSH_TIDS_CMDID);
1122 	if (ret) {
1123 		ath11k_warn(ar->ab,
1124 			    "failed to send WMI_PEER_FLUSH_TIDS cmd\n");
1125 		dev_kfree_skb(skb);
1126 	}
1127 
1128 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1129 		   "WMI peer flush vdev_id %d peer_addr %pM tids %08x\n",
1130 		   param->vdev_id, peer_addr, param->peer_tid_bitmap);
1131 
1132 	return ret;
1133 }
1134 
1135 int ath11k_wmi_peer_rx_reorder_queue_setup(struct ath11k *ar,
1136 					   int vdev_id, const u8 *addr,
1137 					   dma_addr_t paddr, u8 tid,
1138 					   u8 ba_window_size_valid,
1139 					   u32 ba_window_size)
1140 {
1141 	struct wmi_peer_reorder_queue_setup_cmd *cmd;
1142 	struct sk_buff *skb;
1143 	int ret;
1144 
1145 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, sizeof(*cmd));
1146 	if (!skb)
1147 		return -ENOMEM;
1148 
1149 	cmd = (struct wmi_peer_reorder_queue_setup_cmd *)skb->data;
1150 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1151 				     WMI_TAG_REORDER_QUEUE_SETUP_CMD) |
1152 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1153 
1154 	ether_addr_copy(cmd->peer_macaddr.addr, addr);
1155 	cmd->vdev_id = vdev_id;
1156 	cmd->tid = tid;
1157 	cmd->queue_ptr_lo = lower_32_bits(paddr);
1158 	cmd->queue_ptr_hi = upper_32_bits(paddr);
1159 	cmd->queue_no = tid;
1160 	cmd->ba_window_size_valid = ba_window_size_valid;
1161 	cmd->ba_window_size = ba_window_size;
1162 
1163 	ret = ath11k_wmi_cmd_send(ar->wmi, skb,
1164 				  WMI_PEER_REORDER_QUEUE_SETUP_CMDID);
1165 	if (ret) {
1166 		ath11k_warn(ar->ab,
1167 			    "failed to send WMI_PEER_REORDER_QUEUE_SETUP\n");
1168 		dev_kfree_skb(skb);
1169 	}
1170 
1171 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1172 		   "wmi rx reorder queue setup addr %pM vdev_id %d tid %d\n",
1173 		   addr, vdev_id, tid);
1174 
1175 	return ret;
1176 }
1177 
1178 int
1179 ath11k_wmi_rx_reord_queue_remove(struct ath11k *ar,
1180 				 struct rx_reorder_queue_remove_params *param)
1181 {
1182 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1183 	struct wmi_peer_reorder_queue_remove_cmd *cmd;
1184 	struct sk_buff *skb;
1185 	int ret;
1186 
1187 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1188 	if (!skb)
1189 		return -ENOMEM;
1190 
1191 	cmd = (struct wmi_peer_reorder_queue_remove_cmd *)skb->data;
1192 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1193 				     WMI_TAG_REORDER_QUEUE_REMOVE_CMD) |
1194 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1195 
1196 	ether_addr_copy(cmd->peer_macaddr.addr, param->peer_macaddr);
1197 	cmd->vdev_id = param->vdev_id;
1198 	cmd->tid_mask = param->peer_tid_bitmap;
1199 
1200 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1201 		   "%s: peer_macaddr %pM vdev_id %d, tid_map %d", __func__,
1202 		   param->peer_macaddr, param->vdev_id, param->peer_tid_bitmap);
1203 
1204 	ret = ath11k_wmi_cmd_send(wmi, skb,
1205 				  WMI_PEER_REORDER_QUEUE_REMOVE_CMDID);
1206 	if (ret) {
1207 		ath11k_warn(ar->ab,
1208 			    "failed to send WMI_PEER_REORDER_QUEUE_REMOVE_CMDID");
1209 		dev_kfree_skb(skb);
1210 	}
1211 
1212 	return ret;
1213 }
1214 
1215 int ath11k_wmi_pdev_set_param(struct ath11k *ar, u32 param_id,
1216 			      u32 param_value, u8 pdev_id)
1217 {
1218 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1219 	struct wmi_pdev_set_param_cmd *cmd;
1220 	struct sk_buff *skb;
1221 	int ret;
1222 
1223 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1224 	if (!skb)
1225 		return -ENOMEM;
1226 
1227 	cmd = (struct wmi_pdev_set_param_cmd *)skb->data;
1228 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_SET_PARAM_CMD) |
1229 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1230 	cmd->pdev_id = pdev_id;
1231 	cmd->param_id = param_id;
1232 	cmd->param_value = param_value;
1233 
1234 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PDEV_SET_PARAM_CMDID);
1235 	if (ret) {
1236 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_SET_PARAM cmd\n");
1237 		dev_kfree_skb(skb);
1238 	}
1239 
1240 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1241 		   "WMI pdev set param %d pdev id %d value %d\n",
1242 		   param_id, pdev_id, param_value);
1243 
1244 	return ret;
1245 }
1246 
1247 int ath11k_wmi_pdev_set_ps_mode(struct ath11k *ar, int vdev_id, u32 enable)
1248 {
1249 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1250 	struct wmi_pdev_set_ps_mode_cmd *cmd;
1251 	struct sk_buff *skb;
1252 	int ret;
1253 
1254 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1255 	if (!skb)
1256 		return -ENOMEM;
1257 
1258 	cmd = (struct wmi_pdev_set_ps_mode_cmd *)skb->data;
1259 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_STA_POWERSAVE_MODE_CMD) |
1260 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1261 	cmd->vdev_id = vdev_id;
1262 	cmd->sta_ps_mode = enable;
1263 
1264 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_STA_POWERSAVE_MODE_CMDID);
1265 	if (ret) {
1266 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_SET_PARAM cmd\n");
1267 		dev_kfree_skb(skb);
1268 	}
1269 
1270 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1271 		   "WMI vdev set psmode %d vdev id %d\n",
1272 		   enable, vdev_id);
1273 
1274 	return ret;
1275 }
1276 
1277 int ath11k_wmi_pdev_suspend(struct ath11k *ar, u32 suspend_opt,
1278 			    u32 pdev_id)
1279 {
1280 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1281 	struct wmi_pdev_suspend_cmd *cmd;
1282 	struct sk_buff *skb;
1283 	int ret;
1284 
1285 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1286 	if (!skb)
1287 		return -ENOMEM;
1288 
1289 	cmd = (struct wmi_pdev_suspend_cmd *)skb->data;
1290 
1291 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_SUSPEND_CMD) |
1292 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1293 
1294 	cmd->suspend_opt = suspend_opt;
1295 	cmd->pdev_id = pdev_id;
1296 
1297 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PDEV_SUSPEND_CMDID);
1298 	if (ret) {
1299 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_SUSPEND cmd\n");
1300 		dev_kfree_skb(skb);
1301 	}
1302 
1303 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1304 		   "WMI pdev suspend pdev_id %d\n", pdev_id);
1305 
1306 	return ret;
1307 }
1308 
1309 int ath11k_wmi_pdev_resume(struct ath11k *ar, u32 pdev_id)
1310 {
1311 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1312 	struct wmi_pdev_resume_cmd *cmd;
1313 	struct sk_buff *skb;
1314 	int ret;
1315 
1316 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1317 	if (!skb)
1318 		return -ENOMEM;
1319 
1320 	cmd = (struct wmi_pdev_resume_cmd *)skb->data;
1321 
1322 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_RESUME_CMD) |
1323 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1324 	cmd->pdev_id = pdev_id;
1325 
1326 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1327 		   "WMI pdev resume pdev id %d\n", pdev_id);
1328 
1329 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PDEV_RESUME_CMDID);
1330 	if (ret) {
1331 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_RESUME cmd\n");
1332 		dev_kfree_skb(skb);
1333 	}
1334 
1335 	return ret;
1336 }
1337 
1338 /* TODO FW Support for the cmd is not available yet.
1339  * Can be tested once the command and corresponding
1340  * event is implemented in FW
1341  */
1342 int ath11k_wmi_pdev_bss_chan_info_request(struct ath11k *ar,
1343 					  enum wmi_bss_chan_info_req_type type)
1344 {
1345 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1346 	struct wmi_pdev_bss_chan_info_req_cmd *cmd;
1347 	struct sk_buff *skb;
1348 	int ret;
1349 
1350 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1351 	if (!skb)
1352 		return -ENOMEM;
1353 
1354 	cmd = (struct wmi_pdev_bss_chan_info_req_cmd *)skb->data;
1355 
1356 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1357 				     WMI_TAG_PDEV_BSS_CHAN_INFO_REQUEST) |
1358 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1359 	cmd->req_type = type;
1360 	cmd->pdev_id = ar->pdev->pdev_id;
1361 
1362 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1363 		   "WMI bss chan info req type %d\n", type);
1364 
1365 	ret = ath11k_wmi_cmd_send(wmi, skb,
1366 				  WMI_PDEV_BSS_CHAN_INFO_REQUEST_CMDID);
1367 	if (ret) {
1368 		ath11k_warn(ar->ab,
1369 			    "failed to send WMI_PDEV_BSS_CHAN_INFO_REQUEST cmd\n");
1370 		dev_kfree_skb(skb);
1371 	}
1372 
1373 	return ret;
1374 }
1375 
1376 int ath11k_wmi_send_set_ap_ps_param_cmd(struct ath11k *ar, u8 *peer_addr,
1377 					struct ap_ps_params *param)
1378 {
1379 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1380 	struct wmi_ap_ps_peer_cmd *cmd;
1381 	struct sk_buff *skb;
1382 	int ret;
1383 
1384 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1385 	if (!skb)
1386 		return -ENOMEM;
1387 
1388 	cmd = (struct wmi_ap_ps_peer_cmd *)skb->data;
1389 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_AP_PS_PEER_CMD) |
1390 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1391 
1392 	cmd->vdev_id = param->vdev_id;
1393 	ether_addr_copy(cmd->peer_macaddr.addr, peer_addr);
1394 	cmd->param = param->param;
1395 	cmd->value = param->value;
1396 
1397 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_AP_PS_PEER_PARAM_CMDID);
1398 	if (ret) {
1399 		ath11k_warn(ar->ab,
1400 			    "failed to send WMI_AP_PS_PEER_PARAM_CMDID\n");
1401 		dev_kfree_skb(skb);
1402 	}
1403 
1404 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1405 		   "WMI set ap ps vdev id %d peer %pM param %d value %d\n",
1406 		   param->vdev_id, peer_addr, param->param, param->value);
1407 
1408 	return ret;
1409 }
1410 
1411 int ath11k_wmi_set_sta_ps_param(struct ath11k *ar, u32 vdev_id,
1412 				u32 param, u32 param_value)
1413 {
1414 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1415 	struct wmi_sta_powersave_param_cmd *cmd;
1416 	struct sk_buff *skb;
1417 	int ret;
1418 
1419 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1420 	if (!skb)
1421 		return -ENOMEM;
1422 
1423 	cmd = (struct wmi_sta_powersave_param_cmd *)skb->data;
1424 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1425 				     WMI_TAG_STA_POWERSAVE_PARAM_CMD) |
1426 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1427 
1428 	cmd->vdev_id = vdev_id;
1429 	cmd->param = param;
1430 	cmd->value = param_value;
1431 
1432 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1433 		   "WMI set sta ps vdev_id %d param %d value %d\n",
1434 		   vdev_id, param, param_value);
1435 
1436 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_STA_POWERSAVE_PARAM_CMDID);
1437 	if (ret) {
1438 		ath11k_warn(ar->ab, "failed to send WMI_STA_POWERSAVE_PARAM_CMDID");
1439 		dev_kfree_skb(skb);
1440 	}
1441 
1442 	return ret;
1443 }
1444 
1445 int ath11k_wmi_force_fw_hang_cmd(struct ath11k *ar, u32 type, u32 delay_time_ms)
1446 {
1447 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1448 	struct wmi_force_fw_hang_cmd *cmd;
1449 	struct sk_buff *skb;
1450 	int ret, len;
1451 
1452 	len = sizeof(*cmd);
1453 
1454 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
1455 	if (!skb)
1456 		return -ENOMEM;
1457 
1458 	cmd = (struct wmi_force_fw_hang_cmd *)skb->data;
1459 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_FORCE_FW_HANG_CMD) |
1460 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
1461 
1462 	cmd->type = type;
1463 	cmd->delay_time_ms = delay_time_ms;
1464 
1465 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_FORCE_FW_HANG_CMDID);
1466 
1467 	if (ret) {
1468 		ath11k_warn(ar->ab, "Failed to send WMI_FORCE_FW_HANG_CMDID");
1469 		dev_kfree_skb(skb);
1470 	}
1471 	return ret;
1472 }
1473 
1474 int ath11k_wmi_vdev_set_param_cmd(struct ath11k *ar, u32 vdev_id,
1475 				  u32 param_id, u32 param_value)
1476 {
1477 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1478 	struct wmi_vdev_set_param_cmd *cmd;
1479 	struct sk_buff *skb;
1480 	int ret;
1481 
1482 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1483 	if (!skb)
1484 		return -ENOMEM;
1485 
1486 	cmd = (struct wmi_vdev_set_param_cmd *)skb->data;
1487 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_SET_PARAM_CMD) |
1488 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1489 
1490 	cmd->vdev_id = vdev_id;
1491 	cmd->param_id = param_id;
1492 	cmd->param_value = param_value;
1493 
1494 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_SET_PARAM_CMDID);
1495 	if (ret) {
1496 		ath11k_warn(ar->ab,
1497 			    "failed to send WMI_VDEV_SET_PARAM_CMDID\n");
1498 		dev_kfree_skb(skb);
1499 	}
1500 
1501 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1502 		   "WMI vdev id 0x%x set param %d value %d\n",
1503 		   vdev_id, param_id, param_value);
1504 
1505 	return ret;
1506 }
1507 
1508 int ath11k_wmi_send_stats_request_cmd(struct ath11k *ar,
1509 				      struct stats_request_params *param)
1510 {
1511 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1512 	struct wmi_request_stats_cmd *cmd;
1513 	struct sk_buff *skb;
1514 	int ret;
1515 
1516 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1517 	if (!skb)
1518 		return -ENOMEM;
1519 
1520 	cmd = (struct wmi_request_stats_cmd *)skb->data;
1521 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_REQUEST_STATS_CMD) |
1522 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1523 
1524 	cmd->stats_id = param->stats_id;
1525 	cmd->vdev_id = param->vdev_id;
1526 	cmd->pdev_id = param->pdev_id;
1527 
1528 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_REQUEST_STATS_CMDID);
1529 	if (ret) {
1530 		ath11k_warn(ar->ab, "failed to send WMI_REQUEST_STATS cmd\n");
1531 		dev_kfree_skb(skb);
1532 	}
1533 
1534 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1535 		   "WMI request stats 0x%x vdev id %d pdev id %d\n",
1536 		   param->stats_id, param->vdev_id, param->pdev_id);
1537 
1538 	return ret;
1539 }
1540 
1541 int ath11k_wmi_send_pdev_temperature_cmd(struct ath11k *ar)
1542 {
1543 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1544 	struct wmi_get_pdev_temperature_cmd *cmd;
1545 	struct sk_buff *skb;
1546 	int ret;
1547 
1548 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1549 	if (!skb)
1550 		return -ENOMEM;
1551 
1552 	cmd = (struct wmi_get_pdev_temperature_cmd *)skb->data;
1553 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_GET_TEMPERATURE_CMD) |
1554 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1555 	cmd->pdev_id = ar->pdev->pdev_id;
1556 
1557 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PDEV_GET_TEMPERATURE_CMDID);
1558 	if (ret) {
1559 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_GET_TEMPERATURE cmd\n");
1560 		dev_kfree_skb(skb);
1561 	}
1562 
1563 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1564 		   "WMI pdev get temperature for pdev_id %d\n", ar->pdev->pdev_id);
1565 
1566 	return ret;
1567 }
1568 
1569 int ath11k_wmi_send_bcn_offload_control_cmd(struct ath11k *ar,
1570 					    u32 vdev_id, u32 bcn_ctrl_op)
1571 {
1572 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1573 	struct wmi_bcn_offload_ctrl_cmd *cmd;
1574 	struct sk_buff *skb;
1575 	int ret;
1576 
1577 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
1578 	if (!skb)
1579 		return -ENOMEM;
1580 
1581 	cmd = (struct wmi_bcn_offload_ctrl_cmd *)skb->data;
1582 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1583 				     WMI_TAG_BCN_OFFLOAD_CTRL_CMD) |
1584 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1585 
1586 	cmd->vdev_id = vdev_id;
1587 	cmd->bcn_ctrl_op = bcn_ctrl_op;
1588 
1589 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1590 		   "WMI bcn ctrl offload vdev id %d ctrl_op %d\n",
1591 		   vdev_id, bcn_ctrl_op);
1592 
1593 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_BCN_OFFLOAD_CTRL_CMDID);
1594 	if (ret) {
1595 		ath11k_warn(ar->ab,
1596 			    "failed to send WMI_BCN_OFFLOAD_CTRL_CMDID\n");
1597 		dev_kfree_skb(skb);
1598 	}
1599 
1600 	return ret;
1601 }
1602 
1603 int ath11k_wmi_bcn_tmpl(struct ath11k *ar, u32 vdev_id,
1604 			struct ieee80211_mutable_offsets *offs,
1605 			struct sk_buff *bcn)
1606 {
1607 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1608 	struct wmi_bcn_tmpl_cmd *cmd;
1609 	struct wmi_bcn_prb_info *bcn_prb_info;
1610 	struct wmi_tlv *tlv;
1611 	struct sk_buff *skb;
1612 	void *ptr;
1613 	int ret, len;
1614 	size_t aligned_len = roundup(bcn->len, 4);
1615 
1616 	len = sizeof(*cmd) + sizeof(*bcn_prb_info) + TLV_HDR_SIZE + aligned_len;
1617 
1618 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
1619 	if (!skb)
1620 		return -ENOMEM;
1621 
1622 	cmd = (struct wmi_bcn_tmpl_cmd *)skb->data;
1623 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_BCN_TMPL_CMD) |
1624 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1625 	cmd->vdev_id = vdev_id;
1626 	cmd->tim_ie_offset = offs->tim_offset;
1627 	cmd->csa_switch_count_offset = offs->cntdwn_counter_offs[0];
1628 	cmd->ext_csa_switch_count_offset = offs->cntdwn_counter_offs[1];
1629 	cmd->buf_len = bcn->len;
1630 
1631 	ptr = skb->data + sizeof(*cmd);
1632 
1633 	bcn_prb_info = ptr;
1634 	len = sizeof(*bcn_prb_info);
1635 	bcn_prb_info->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1636 					      WMI_TAG_BCN_PRB_INFO) |
1637 				   FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
1638 	bcn_prb_info->caps = 0;
1639 	bcn_prb_info->erp = 0;
1640 
1641 	ptr += sizeof(*bcn_prb_info);
1642 
1643 	tlv = ptr;
1644 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
1645 		      FIELD_PREP(WMI_TLV_LEN, aligned_len);
1646 	memcpy(tlv->value, bcn->data, bcn->len);
1647 
1648 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_BCN_TMPL_CMDID);
1649 	if (ret) {
1650 		ath11k_warn(ar->ab, "failed to send WMI_BCN_TMPL_CMDID\n");
1651 		dev_kfree_skb(skb);
1652 	}
1653 
1654 	return ret;
1655 }
1656 
1657 int ath11k_wmi_vdev_install_key(struct ath11k *ar,
1658 				struct wmi_vdev_install_key_arg *arg)
1659 {
1660 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1661 	struct wmi_vdev_install_key_cmd *cmd;
1662 	struct wmi_tlv *tlv;
1663 	struct sk_buff *skb;
1664 	int ret, len;
1665 	int key_len_aligned = roundup(arg->key_len, sizeof(uint32_t));
1666 
1667 	len = sizeof(*cmd) + TLV_HDR_SIZE + key_len_aligned;
1668 
1669 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
1670 	if (!skb)
1671 		return -ENOMEM;
1672 
1673 	cmd = (struct wmi_vdev_install_key_cmd *)skb->data;
1674 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VDEV_INSTALL_KEY_CMD) |
1675 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1676 	cmd->vdev_id = arg->vdev_id;
1677 	ether_addr_copy(cmd->peer_macaddr.addr, arg->macaddr);
1678 	cmd->key_idx = arg->key_idx;
1679 	cmd->key_flags = arg->key_flags;
1680 	cmd->key_cipher = arg->key_cipher;
1681 	cmd->key_len = arg->key_len;
1682 	cmd->key_txmic_len = arg->key_txmic_len;
1683 	cmd->key_rxmic_len = arg->key_rxmic_len;
1684 
1685 	if (arg->key_rsc_counter)
1686 		memcpy(&cmd->key_rsc_counter, &arg->key_rsc_counter,
1687 		       sizeof(struct wmi_key_seq_counter));
1688 
1689 	tlv = (struct wmi_tlv *)(skb->data + sizeof(*cmd));
1690 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
1691 		      FIELD_PREP(WMI_TLV_LEN, key_len_aligned);
1692 	memcpy(tlv->value, (u8 *)arg->key_data, key_len_aligned);
1693 
1694 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_VDEV_INSTALL_KEY_CMDID);
1695 	if (ret) {
1696 		ath11k_warn(ar->ab,
1697 			    "failed to send WMI_VDEV_INSTALL_KEY cmd\n");
1698 		dev_kfree_skb(skb);
1699 	}
1700 
1701 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1702 		   "WMI vdev install key idx %d cipher %d len %d\n",
1703 		   arg->key_idx, arg->key_cipher, arg->key_len);
1704 
1705 	return ret;
1706 }
1707 
1708 static inline void
1709 ath11k_wmi_copy_peer_flags(struct wmi_peer_assoc_complete_cmd *cmd,
1710 			   struct peer_assoc_params *param,
1711 			   bool hw_crypto_disabled)
1712 {
1713 	cmd->peer_flags = 0;
1714 
1715 	if (param->is_wme_set) {
1716 		if (param->qos_flag)
1717 			cmd->peer_flags |= WMI_PEER_QOS;
1718 		if (param->apsd_flag)
1719 			cmd->peer_flags |= WMI_PEER_APSD;
1720 		if (param->ht_flag)
1721 			cmd->peer_flags |= WMI_PEER_HT;
1722 		if (param->bw_40)
1723 			cmd->peer_flags |= WMI_PEER_40MHZ;
1724 		if (param->bw_80)
1725 			cmd->peer_flags |= WMI_PEER_80MHZ;
1726 		if (param->bw_160)
1727 			cmd->peer_flags |= WMI_PEER_160MHZ;
1728 
1729 		/* Typically if STBC is enabled for VHT it should be enabled
1730 		 * for HT as well
1731 		 **/
1732 		if (param->stbc_flag)
1733 			cmd->peer_flags |= WMI_PEER_STBC;
1734 
1735 		/* Typically if LDPC is enabled for VHT it should be enabled
1736 		 * for HT as well
1737 		 **/
1738 		if (param->ldpc_flag)
1739 			cmd->peer_flags |= WMI_PEER_LDPC;
1740 
1741 		if (param->static_mimops_flag)
1742 			cmd->peer_flags |= WMI_PEER_STATIC_MIMOPS;
1743 		if (param->dynamic_mimops_flag)
1744 			cmd->peer_flags |= WMI_PEER_DYN_MIMOPS;
1745 		if (param->spatial_mux_flag)
1746 			cmd->peer_flags |= WMI_PEER_SPATIAL_MUX;
1747 		if (param->vht_flag)
1748 			cmd->peer_flags |= WMI_PEER_VHT;
1749 		if (param->he_flag)
1750 			cmd->peer_flags |= WMI_PEER_HE;
1751 		if (param->twt_requester)
1752 			cmd->peer_flags |= WMI_PEER_TWT_REQ;
1753 		if (param->twt_responder)
1754 			cmd->peer_flags |= WMI_PEER_TWT_RESP;
1755 	}
1756 
1757 	/* Suppress authorization for all AUTH modes that need 4-way handshake
1758 	 * (during re-association).
1759 	 * Authorization will be done for these modes on key installation.
1760 	 */
1761 	if (param->auth_flag)
1762 		cmd->peer_flags |= WMI_PEER_AUTH;
1763 	if (param->need_ptk_4_way) {
1764 		cmd->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1765 		if (!hw_crypto_disabled)
1766 			cmd->peer_flags &= ~WMI_PEER_AUTH;
1767 	}
1768 	if (param->need_gtk_2_way)
1769 		cmd->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1770 	/* safe mode bypass the 4-way handshake */
1771 	if (param->safe_mode_enabled)
1772 		cmd->peer_flags &= ~(WMI_PEER_NEED_PTK_4_WAY |
1773 				     WMI_PEER_NEED_GTK_2_WAY);
1774 
1775 	if (param->is_pmf_enabled)
1776 		cmd->peer_flags |= WMI_PEER_PMF;
1777 
1778 	/* Disable AMSDU for station transmit, if user configures it */
1779 	/* Disable AMSDU for AP transmit to 11n Stations, if user configures
1780 	 * it
1781 	 * if (param->amsdu_disable) Add after FW support
1782 	 **/
1783 
1784 	/* Target asserts if node is marked HT and all MCS is set to 0.
1785 	 * Mark the node as non-HT if all the mcs rates are disabled through
1786 	 * iwpriv
1787 	 **/
1788 	if (param->peer_ht_rates.num_rates == 0)
1789 		cmd->peer_flags &= ~WMI_PEER_HT;
1790 }
1791 
1792 int ath11k_wmi_send_peer_assoc_cmd(struct ath11k *ar,
1793 				   struct peer_assoc_params *param)
1794 {
1795 	struct ath11k_pdev_wmi *wmi = ar->wmi;
1796 	struct wmi_peer_assoc_complete_cmd *cmd;
1797 	struct wmi_vht_rate_set *mcs;
1798 	struct wmi_he_rate_set *he_mcs;
1799 	struct sk_buff *skb;
1800 	struct wmi_tlv *tlv;
1801 	void *ptr;
1802 	u32 peer_legacy_rates_align;
1803 	u32 peer_ht_rates_align;
1804 	int i, ret, len;
1805 
1806 	peer_legacy_rates_align = roundup(param->peer_legacy_rates.num_rates,
1807 					  sizeof(u32));
1808 	peer_ht_rates_align = roundup(param->peer_ht_rates.num_rates,
1809 				      sizeof(u32));
1810 
1811 	len = sizeof(*cmd) +
1812 	      TLV_HDR_SIZE + (peer_legacy_rates_align * sizeof(u8)) +
1813 	      TLV_HDR_SIZE + (peer_ht_rates_align * sizeof(u8)) +
1814 	      sizeof(*mcs) + TLV_HDR_SIZE +
1815 	      (sizeof(*he_mcs) * param->peer_he_mcs_count);
1816 
1817 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
1818 	if (!skb)
1819 		return -ENOMEM;
1820 
1821 	ptr = skb->data;
1822 
1823 	cmd = ptr;
1824 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1825 				     WMI_TAG_PEER_ASSOC_COMPLETE_CMD) |
1826 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
1827 
1828 	cmd->vdev_id = param->vdev_id;
1829 
1830 	cmd->peer_new_assoc = param->peer_new_assoc;
1831 	cmd->peer_associd = param->peer_associd;
1832 
1833 	ath11k_wmi_copy_peer_flags(cmd, param,
1834 				   test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED,
1835 					    &ar->ab->dev_flags));
1836 
1837 	ether_addr_copy(cmd->peer_macaddr.addr, param->peer_mac);
1838 
1839 	cmd->peer_rate_caps = param->peer_rate_caps;
1840 	cmd->peer_caps = param->peer_caps;
1841 	cmd->peer_listen_intval = param->peer_listen_intval;
1842 	cmd->peer_ht_caps = param->peer_ht_caps;
1843 	cmd->peer_max_mpdu = param->peer_max_mpdu;
1844 	cmd->peer_mpdu_density = param->peer_mpdu_density;
1845 	cmd->peer_vht_caps = param->peer_vht_caps;
1846 	cmd->peer_phymode = param->peer_phymode;
1847 
1848 	/* Update 11ax capabilities */
1849 	cmd->peer_he_cap_info = param->peer_he_cap_macinfo[0];
1850 	cmd->peer_he_cap_info_ext = param->peer_he_cap_macinfo[1];
1851 	cmd->peer_he_cap_info_internal = param->peer_he_cap_macinfo_internal;
1852 	cmd->peer_he_caps_6ghz = param->peer_he_caps_6ghz;
1853 	cmd->peer_he_ops = param->peer_he_ops;
1854 	memcpy(&cmd->peer_he_cap_phy, &param->peer_he_cap_phyinfo,
1855 	       sizeof(param->peer_he_cap_phyinfo));
1856 	memcpy(&cmd->peer_ppet, &param->peer_ppet,
1857 	       sizeof(param->peer_ppet));
1858 
1859 	/* Update peer legacy rate information */
1860 	ptr += sizeof(*cmd);
1861 
1862 	tlv = ptr;
1863 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
1864 		      FIELD_PREP(WMI_TLV_LEN, peer_legacy_rates_align);
1865 
1866 	ptr += TLV_HDR_SIZE;
1867 
1868 	cmd->num_peer_legacy_rates = param->peer_legacy_rates.num_rates;
1869 	memcpy(ptr, param->peer_legacy_rates.rates,
1870 	       param->peer_legacy_rates.num_rates);
1871 
1872 	/* Update peer HT rate information */
1873 	ptr += peer_legacy_rates_align;
1874 
1875 	tlv = ptr;
1876 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
1877 		      FIELD_PREP(WMI_TLV_LEN, peer_ht_rates_align);
1878 	ptr += TLV_HDR_SIZE;
1879 	cmd->num_peer_ht_rates = param->peer_ht_rates.num_rates;
1880 	memcpy(ptr, param->peer_ht_rates.rates,
1881 	       param->peer_ht_rates.num_rates);
1882 
1883 	/* VHT Rates */
1884 	ptr += peer_ht_rates_align;
1885 
1886 	mcs = ptr;
1887 
1888 	mcs->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_VHT_RATE_SET) |
1889 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*mcs) - TLV_HDR_SIZE);
1890 
1891 	cmd->peer_nss = param->peer_nss;
1892 
1893 	/* Update bandwidth-NSS mapping */
1894 	cmd->peer_bw_rxnss_override = 0;
1895 	cmd->peer_bw_rxnss_override |= param->peer_bw_rxnss_override;
1896 
1897 	if (param->vht_capable) {
1898 		mcs->rx_max_rate = param->rx_max_rate;
1899 		mcs->rx_mcs_set = param->rx_mcs_set;
1900 		mcs->tx_max_rate = param->tx_max_rate;
1901 		mcs->tx_mcs_set = param->tx_mcs_set;
1902 	}
1903 
1904 	/* HE Rates */
1905 	cmd->peer_he_mcs = param->peer_he_mcs_count;
1906 	cmd->min_data_rate = param->min_data_rate;
1907 
1908 	ptr += sizeof(*mcs);
1909 
1910 	len = param->peer_he_mcs_count * sizeof(*he_mcs);
1911 
1912 	tlv = ptr;
1913 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
1914 		      FIELD_PREP(WMI_TLV_LEN, len);
1915 	ptr += TLV_HDR_SIZE;
1916 
1917 	/* Loop through the HE rate set */
1918 	for (i = 0; i < param->peer_he_mcs_count; i++) {
1919 		he_mcs = ptr;
1920 		he_mcs->tlv_header = FIELD_PREP(WMI_TLV_TAG,
1921 						WMI_TAG_HE_RATE_SET) |
1922 				     FIELD_PREP(WMI_TLV_LEN,
1923 						sizeof(*he_mcs) - TLV_HDR_SIZE);
1924 
1925 		he_mcs->rx_mcs_set = param->peer_he_tx_mcs_set[i];
1926 		he_mcs->tx_mcs_set = param->peer_he_rx_mcs_set[i];
1927 		ptr += sizeof(*he_mcs);
1928 	}
1929 
1930 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_PEER_ASSOC_CMDID);
1931 	if (ret) {
1932 		ath11k_warn(ar->ab,
1933 			    "failed to send WMI_PEER_ASSOC_CMDID\n");
1934 		dev_kfree_skb(skb);
1935 	}
1936 
1937 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1938 		   "wmi peer assoc vdev id %d assoc id %d peer mac %pM peer_flags %x rate_caps %x peer_caps %x listen_intval %d ht_caps %x max_mpdu %d nss %d phymode %d peer_mpdu_density %d vht_caps %x he cap_info %x he ops %x he cap_info_ext %x he phy %x %x %x peer_bw_rxnss_override %x\n",
1939 		   cmd->vdev_id, cmd->peer_associd, param->peer_mac,
1940 		   cmd->peer_flags, cmd->peer_rate_caps, cmd->peer_caps,
1941 		   cmd->peer_listen_intval, cmd->peer_ht_caps,
1942 		   cmd->peer_max_mpdu, cmd->peer_nss, cmd->peer_phymode,
1943 		   cmd->peer_mpdu_density,
1944 		   cmd->peer_vht_caps, cmd->peer_he_cap_info,
1945 		   cmd->peer_he_ops, cmd->peer_he_cap_info_ext,
1946 		   cmd->peer_he_cap_phy[0], cmd->peer_he_cap_phy[1],
1947 		   cmd->peer_he_cap_phy[2],
1948 		   cmd->peer_bw_rxnss_override);
1949 
1950 	return ret;
1951 }
1952 
1953 void ath11k_wmi_start_scan_init(struct ath11k *ar,
1954 				struct scan_req_params *arg)
1955 {
1956 	/* setup commonly used values */
1957 	arg->scan_req_id = 1;
1958 	arg->scan_priority = WMI_SCAN_PRIORITY_LOW;
1959 	arg->dwell_time_active = 50;
1960 	arg->dwell_time_active_2g = 0;
1961 	arg->dwell_time_passive = 150;
1962 	arg->dwell_time_active_6g = 40;
1963 	arg->dwell_time_passive_6g = 30;
1964 	arg->min_rest_time = 50;
1965 	arg->max_rest_time = 500;
1966 	arg->repeat_probe_time = 0;
1967 	arg->probe_spacing_time = 0;
1968 	arg->idle_time = 0;
1969 	arg->max_scan_time = 20000;
1970 	arg->probe_delay = 5;
1971 	arg->notify_scan_events = WMI_SCAN_EVENT_STARTED |
1972 				  WMI_SCAN_EVENT_COMPLETED |
1973 				  WMI_SCAN_EVENT_BSS_CHANNEL |
1974 				  WMI_SCAN_EVENT_FOREIGN_CHAN |
1975 				  WMI_SCAN_EVENT_DEQUEUED;
1976 	arg->scan_flags |= WMI_SCAN_CHAN_STAT_EVENT;
1977 	arg->num_bssid = 1;
1978 
1979 	/* fill bssid_list[0] with 0xff, otherwise bssid and RA will be
1980 	 * ZEROs in probe request
1981 	 */
1982 	eth_broadcast_addr(arg->bssid_list[0].addr);
1983 }
1984 
1985 static inline void
1986 ath11k_wmi_copy_scan_event_cntrl_flags(struct wmi_start_scan_cmd *cmd,
1987 				       struct scan_req_params *param)
1988 {
1989 	/* Scan events subscription */
1990 	if (param->scan_ev_started)
1991 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_STARTED;
1992 	if (param->scan_ev_completed)
1993 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_COMPLETED;
1994 	if (param->scan_ev_bss_chan)
1995 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_BSS_CHANNEL;
1996 	if (param->scan_ev_foreign_chan)
1997 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_FOREIGN_CHAN;
1998 	if (param->scan_ev_dequeued)
1999 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_DEQUEUED;
2000 	if (param->scan_ev_preempted)
2001 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_PREEMPTED;
2002 	if (param->scan_ev_start_failed)
2003 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_START_FAILED;
2004 	if (param->scan_ev_restarted)
2005 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_RESTARTED;
2006 	if (param->scan_ev_foreign_chn_exit)
2007 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_FOREIGN_CHAN_EXIT;
2008 	if (param->scan_ev_suspended)
2009 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_SUSPENDED;
2010 	if (param->scan_ev_resumed)
2011 		cmd->notify_scan_events |=  WMI_SCAN_EVENT_RESUMED;
2012 
2013 	/** Set scan control flags */
2014 	cmd->scan_ctrl_flags = 0;
2015 	if (param->scan_f_passive)
2016 		cmd->scan_ctrl_flags |=  WMI_SCAN_FLAG_PASSIVE;
2017 	if (param->scan_f_strict_passive_pch)
2018 		cmd->scan_ctrl_flags |=  WMI_SCAN_FLAG_STRICT_PASSIVE_ON_PCHN;
2019 	if (param->scan_f_promisc_mode)
2020 		cmd->scan_ctrl_flags |=  WMI_SCAN_FILTER_PROMISCUOS;
2021 	if (param->scan_f_capture_phy_err)
2022 		cmd->scan_ctrl_flags |=  WMI_SCAN_CAPTURE_PHY_ERROR;
2023 	if (param->scan_f_half_rate)
2024 		cmd->scan_ctrl_flags |=  WMI_SCAN_FLAG_HALF_RATE_SUPPORT;
2025 	if (param->scan_f_quarter_rate)
2026 		cmd->scan_ctrl_flags |=  WMI_SCAN_FLAG_QUARTER_RATE_SUPPORT;
2027 	if (param->scan_f_cck_rates)
2028 		cmd->scan_ctrl_flags |=  WMI_SCAN_ADD_CCK_RATES;
2029 	if (param->scan_f_ofdm_rates)
2030 		cmd->scan_ctrl_flags |=  WMI_SCAN_ADD_OFDM_RATES;
2031 	if (param->scan_f_chan_stat_evnt)
2032 		cmd->scan_ctrl_flags |=  WMI_SCAN_CHAN_STAT_EVENT;
2033 	if (param->scan_f_filter_prb_req)
2034 		cmd->scan_ctrl_flags |=  WMI_SCAN_FILTER_PROBE_REQ;
2035 	if (param->scan_f_bcast_probe)
2036 		cmd->scan_ctrl_flags |=  WMI_SCAN_ADD_BCAST_PROBE_REQ;
2037 	if (param->scan_f_offchan_mgmt_tx)
2038 		cmd->scan_ctrl_flags |=  WMI_SCAN_OFFCHAN_MGMT_TX;
2039 	if (param->scan_f_offchan_data_tx)
2040 		cmd->scan_ctrl_flags |=  WMI_SCAN_OFFCHAN_DATA_TX;
2041 	if (param->scan_f_force_active_dfs_chn)
2042 		cmd->scan_ctrl_flags |=  WMI_SCAN_FLAG_FORCE_ACTIVE_ON_DFS;
2043 	if (param->scan_f_add_tpc_ie_in_probe)
2044 		cmd->scan_ctrl_flags |=  WMI_SCAN_ADD_TPC_IE_IN_PROBE_REQ;
2045 	if (param->scan_f_add_ds_ie_in_probe)
2046 		cmd->scan_ctrl_flags |=  WMI_SCAN_ADD_DS_IE_IN_PROBE_REQ;
2047 	if (param->scan_f_add_spoofed_mac_in_probe)
2048 		cmd->scan_ctrl_flags |=  WMI_SCAN_ADD_SPOOF_MAC_IN_PROBE_REQ;
2049 	if (param->scan_f_add_rand_seq_in_probe)
2050 		cmd->scan_ctrl_flags |=  WMI_SCAN_RANDOM_SEQ_NO_IN_PROBE_REQ;
2051 	if (param->scan_f_en_ie_whitelist_in_probe)
2052 		cmd->scan_ctrl_flags |=
2053 			 WMI_SCAN_ENABLE_IE_WHTELIST_IN_PROBE_REQ;
2054 
2055 	/* for adaptive scan mode using 3 bits (21 - 23 bits) */
2056 	WMI_SCAN_SET_DWELL_MODE(cmd->scan_ctrl_flags,
2057 				param->adaptive_dwell_time_mode);
2058 }
2059 
2060 int ath11k_wmi_send_scan_start_cmd(struct ath11k *ar,
2061 				   struct scan_req_params *params)
2062 {
2063 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2064 	struct wmi_start_scan_cmd *cmd;
2065 	struct wmi_ssid *ssid = NULL;
2066 	struct wmi_mac_addr *bssid;
2067 	struct sk_buff *skb;
2068 	struct wmi_tlv *tlv;
2069 	void *ptr;
2070 	int i, ret, len;
2071 	u32 *tmp_ptr;
2072 	u8 extraie_len_with_pad = 0;
2073 	struct hint_short_ssid *s_ssid = NULL;
2074 	struct hint_bssid *hint_bssid = NULL;
2075 
2076 	len = sizeof(*cmd);
2077 
2078 	len += TLV_HDR_SIZE;
2079 	if (params->num_chan)
2080 		len += params->num_chan * sizeof(u32);
2081 
2082 	len += TLV_HDR_SIZE;
2083 	if (params->num_ssids)
2084 		len += params->num_ssids * sizeof(*ssid);
2085 
2086 	len += TLV_HDR_SIZE;
2087 	if (params->num_bssid)
2088 		len += sizeof(*bssid) * params->num_bssid;
2089 
2090 	len += TLV_HDR_SIZE;
2091 	if (params->extraie.len)
2092 		extraie_len_with_pad =
2093 			roundup(params->extraie.len, sizeof(u32));
2094 	len += extraie_len_with_pad;
2095 
2096 	if (params->num_hint_bssid)
2097 		len += TLV_HDR_SIZE +
2098 		       params->num_hint_bssid * sizeof(struct hint_bssid);
2099 
2100 	if (params->num_hint_s_ssid)
2101 		len += TLV_HDR_SIZE +
2102 		       params->num_hint_s_ssid * sizeof(struct hint_short_ssid);
2103 
2104 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2105 	if (!skb)
2106 		return -ENOMEM;
2107 
2108 	ptr = skb->data;
2109 
2110 	cmd = ptr;
2111 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_START_SCAN_CMD) |
2112 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2113 
2114 	cmd->scan_id = params->scan_id;
2115 	cmd->scan_req_id = params->scan_req_id;
2116 	cmd->vdev_id = params->vdev_id;
2117 	cmd->scan_priority = params->scan_priority;
2118 	cmd->notify_scan_events = params->notify_scan_events;
2119 
2120 	ath11k_wmi_copy_scan_event_cntrl_flags(cmd, params);
2121 
2122 	cmd->dwell_time_active = params->dwell_time_active;
2123 	cmd->dwell_time_active_2g = params->dwell_time_active_2g;
2124 	cmd->dwell_time_passive = params->dwell_time_passive;
2125 	cmd->dwell_time_active_6g = params->dwell_time_active_6g;
2126 	cmd->dwell_time_passive_6g = params->dwell_time_passive_6g;
2127 	cmd->min_rest_time = params->min_rest_time;
2128 	cmd->max_rest_time = params->max_rest_time;
2129 	cmd->repeat_probe_time = params->repeat_probe_time;
2130 	cmd->probe_spacing_time = params->probe_spacing_time;
2131 	cmd->idle_time = params->idle_time;
2132 	cmd->max_scan_time = params->max_scan_time;
2133 	cmd->probe_delay = params->probe_delay;
2134 	cmd->burst_duration = params->burst_duration;
2135 	cmd->num_chan = params->num_chan;
2136 	cmd->num_bssid = params->num_bssid;
2137 	cmd->num_ssids = params->num_ssids;
2138 	cmd->ie_len = params->extraie.len;
2139 	cmd->n_probes = params->n_probes;
2140 
2141 	ptr += sizeof(*cmd);
2142 
2143 	len = params->num_chan * sizeof(u32);
2144 
2145 	tlv = ptr;
2146 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_UINT32) |
2147 		      FIELD_PREP(WMI_TLV_LEN, len);
2148 	ptr += TLV_HDR_SIZE;
2149 	tmp_ptr = (u32 *)ptr;
2150 
2151 	for (i = 0; i < params->num_chan; ++i)
2152 		tmp_ptr[i] = params->chan_list[i];
2153 
2154 	ptr += len;
2155 
2156 	len = params->num_ssids * sizeof(*ssid);
2157 	tlv = ptr;
2158 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_FIXED_STRUCT) |
2159 		      FIELD_PREP(WMI_TLV_LEN, len);
2160 
2161 	ptr += TLV_HDR_SIZE;
2162 
2163 	if (params->num_ssids) {
2164 		ssid = ptr;
2165 		for (i = 0; i < params->num_ssids; ++i) {
2166 			ssid->ssid_len = params->ssid[i].length;
2167 			memcpy(ssid->ssid, params->ssid[i].ssid,
2168 			       params->ssid[i].length);
2169 			ssid++;
2170 		}
2171 	}
2172 
2173 	ptr += (params->num_ssids * sizeof(*ssid));
2174 	len = params->num_bssid * sizeof(*bssid);
2175 	tlv = ptr;
2176 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_FIXED_STRUCT) |
2177 		      FIELD_PREP(WMI_TLV_LEN, len);
2178 
2179 	ptr += TLV_HDR_SIZE;
2180 	bssid = ptr;
2181 
2182 	if (params->num_bssid) {
2183 		for (i = 0; i < params->num_bssid; ++i) {
2184 			ether_addr_copy(bssid->addr,
2185 					params->bssid_list[i].addr);
2186 			bssid++;
2187 		}
2188 	}
2189 
2190 	ptr += params->num_bssid * sizeof(*bssid);
2191 
2192 	len = extraie_len_with_pad;
2193 	tlv = ptr;
2194 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
2195 		      FIELD_PREP(WMI_TLV_LEN, len);
2196 	ptr += TLV_HDR_SIZE;
2197 
2198 	if (params->extraie.len)
2199 		memcpy(ptr, params->extraie.ptr,
2200 		       params->extraie.len);
2201 
2202 	ptr += extraie_len_with_pad;
2203 
2204 	if (params->num_hint_s_ssid) {
2205 		len = params->num_hint_s_ssid * sizeof(struct hint_short_ssid);
2206 		tlv = ptr;
2207 		tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_FIXED_STRUCT) |
2208 			      FIELD_PREP(WMI_TLV_LEN, len);
2209 		ptr += TLV_HDR_SIZE;
2210 		s_ssid = ptr;
2211 		for (i = 0; i < params->num_hint_s_ssid; ++i) {
2212 			s_ssid->freq_flags = params->hint_s_ssid[i].freq_flags;
2213 			s_ssid->short_ssid = params->hint_s_ssid[i].short_ssid;
2214 			s_ssid++;
2215 		}
2216 		ptr += len;
2217 	}
2218 
2219 	if (params->num_hint_bssid) {
2220 		len = params->num_hint_bssid * sizeof(struct hint_bssid);
2221 		tlv = ptr;
2222 		tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_FIXED_STRUCT) |
2223 			      FIELD_PREP(WMI_TLV_LEN, len);
2224 		ptr += TLV_HDR_SIZE;
2225 		hint_bssid = ptr;
2226 		for (i = 0; i < params->num_hint_bssid; ++i) {
2227 			hint_bssid->freq_flags =
2228 				params->hint_bssid[i].freq_flags;
2229 			ether_addr_copy(&params->hint_bssid[i].bssid.addr[0],
2230 					&hint_bssid->bssid.addr[0]);
2231 			hint_bssid++;
2232 		}
2233 	}
2234 
2235 	ret = ath11k_wmi_cmd_send(wmi, skb,
2236 				  WMI_START_SCAN_CMDID);
2237 	if (ret) {
2238 		ath11k_warn(ar->ab, "failed to send WMI_START_SCAN_CMDID\n");
2239 		dev_kfree_skb(skb);
2240 	}
2241 
2242 	return ret;
2243 }
2244 
2245 int ath11k_wmi_send_scan_stop_cmd(struct ath11k *ar,
2246 				  struct scan_cancel_param *param)
2247 {
2248 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2249 	struct wmi_stop_scan_cmd *cmd;
2250 	struct sk_buff *skb;
2251 	int ret;
2252 
2253 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2254 	if (!skb)
2255 		return -ENOMEM;
2256 
2257 	cmd = (struct wmi_stop_scan_cmd *)skb->data;
2258 
2259 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_STOP_SCAN_CMD) |
2260 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2261 
2262 	cmd->vdev_id = param->vdev_id;
2263 	cmd->requestor = param->requester;
2264 	cmd->scan_id = param->scan_id;
2265 	cmd->pdev_id = param->pdev_id;
2266 	/* stop the scan with the corresponding scan_id */
2267 	if (param->req_type == WLAN_SCAN_CANCEL_PDEV_ALL) {
2268 		/* Cancelling all scans */
2269 		cmd->req_type =  WMI_SCAN_STOP_ALL;
2270 	} else if (param->req_type == WLAN_SCAN_CANCEL_VDEV_ALL) {
2271 		/* Cancelling VAP scans */
2272 		cmd->req_type =  WMI_SCN_STOP_VAP_ALL;
2273 	} else if (param->req_type == WLAN_SCAN_CANCEL_SINGLE) {
2274 		/* Cancelling specific scan */
2275 		cmd->req_type =  WMI_SCAN_STOP_ONE;
2276 	} else {
2277 		ath11k_warn(ar->ab, "invalid scan cancel param %d",
2278 			    param->req_type);
2279 		dev_kfree_skb(skb);
2280 		return -EINVAL;
2281 	}
2282 
2283 	ret = ath11k_wmi_cmd_send(wmi, skb,
2284 				  WMI_STOP_SCAN_CMDID);
2285 	if (ret) {
2286 		ath11k_warn(ar->ab, "failed to send WMI_STOP_SCAN_CMDID\n");
2287 		dev_kfree_skb(skb);
2288 	}
2289 
2290 	return ret;
2291 }
2292 
2293 int ath11k_wmi_send_scan_chan_list_cmd(struct ath11k *ar,
2294 				       struct scan_chan_list_params *chan_list)
2295 {
2296 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2297 	struct wmi_scan_chan_list_cmd *cmd;
2298 	struct sk_buff *skb;
2299 	struct wmi_channel *chan_info;
2300 	struct channel_param *tchan_info;
2301 	struct wmi_tlv *tlv;
2302 	void *ptr;
2303 	int i, ret, len;
2304 	u16 num_send_chans, num_sends = 0, max_chan_limit = 0;
2305 	u32 *reg1, *reg2;
2306 
2307 	tchan_info = chan_list->ch_param;
2308 	while (chan_list->nallchans) {
2309 		len = sizeof(*cmd) + TLV_HDR_SIZE;
2310 		max_chan_limit = (wmi->wmi_ab->max_msg_len[ar->pdev_idx] - len) /
2311 			sizeof(*chan_info);
2312 
2313 		if (chan_list->nallchans > max_chan_limit)
2314 			num_send_chans = max_chan_limit;
2315 		else
2316 			num_send_chans = chan_list->nallchans;
2317 
2318 		chan_list->nallchans -= num_send_chans;
2319 		len += sizeof(*chan_info) * num_send_chans;
2320 
2321 		skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2322 		if (!skb)
2323 			return -ENOMEM;
2324 
2325 		cmd = (struct wmi_scan_chan_list_cmd *)skb->data;
2326 		cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_SCAN_CHAN_LIST_CMD) |
2327 			FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2328 		cmd->pdev_id = chan_list->pdev_id;
2329 		cmd->num_scan_chans = num_send_chans;
2330 		if (num_sends)
2331 			cmd->flags |= WMI_APPEND_TO_EXISTING_CHAN_LIST_FLAG;
2332 
2333 		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2334 			   "WMI no.of chan = %d len = %d pdev_id = %d num_sends = %d\n",
2335 			   num_send_chans, len, cmd->pdev_id, num_sends);
2336 
2337 		ptr = skb->data + sizeof(*cmd);
2338 
2339 		len = sizeof(*chan_info) * num_send_chans;
2340 		tlv = ptr;
2341 		tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
2342 			      FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
2343 		ptr += TLV_HDR_SIZE;
2344 
2345 		for (i = 0; i < num_send_chans; ++i) {
2346 			chan_info = ptr;
2347 			memset(chan_info, 0, sizeof(*chan_info));
2348 			len = sizeof(*chan_info);
2349 			chan_info->tlv_header = FIELD_PREP(WMI_TLV_TAG,
2350 							   WMI_TAG_CHANNEL) |
2351 						FIELD_PREP(WMI_TLV_LEN,
2352 							   len - TLV_HDR_SIZE);
2353 
2354 			reg1 = &chan_info->reg_info_1;
2355 			reg2 = &chan_info->reg_info_2;
2356 			chan_info->mhz = tchan_info->mhz;
2357 			chan_info->band_center_freq1 = tchan_info->cfreq1;
2358 			chan_info->band_center_freq2 = tchan_info->cfreq2;
2359 
2360 			if (tchan_info->is_chan_passive)
2361 				chan_info->info |= WMI_CHAN_INFO_PASSIVE;
2362 			if (tchan_info->allow_he)
2363 				chan_info->info |= WMI_CHAN_INFO_ALLOW_HE;
2364 			else if (tchan_info->allow_vht)
2365 				chan_info->info |= WMI_CHAN_INFO_ALLOW_VHT;
2366 			else if (tchan_info->allow_ht)
2367 				chan_info->info |= WMI_CHAN_INFO_ALLOW_HT;
2368 			if (tchan_info->half_rate)
2369 				chan_info->info |= WMI_CHAN_INFO_HALF_RATE;
2370 			if (tchan_info->quarter_rate)
2371 				chan_info->info |= WMI_CHAN_INFO_QUARTER_RATE;
2372 			if (tchan_info->psc_channel)
2373 				chan_info->info |= WMI_CHAN_INFO_PSC;
2374 			if (tchan_info->dfs_set)
2375 				chan_info->info |= WMI_CHAN_INFO_DFS;
2376 
2377 			chan_info->info |= FIELD_PREP(WMI_CHAN_INFO_MODE,
2378 						      tchan_info->phy_mode);
2379 			*reg1 |= FIELD_PREP(WMI_CHAN_REG_INFO1_MIN_PWR,
2380 					    tchan_info->minpower);
2381 			*reg1 |= FIELD_PREP(WMI_CHAN_REG_INFO1_MAX_PWR,
2382 					    tchan_info->maxpower);
2383 			*reg1 |= FIELD_PREP(WMI_CHAN_REG_INFO1_MAX_REG_PWR,
2384 					    tchan_info->maxregpower);
2385 			*reg1 |= FIELD_PREP(WMI_CHAN_REG_INFO1_REG_CLS,
2386 					    tchan_info->reg_class_id);
2387 			*reg2 |= FIELD_PREP(WMI_CHAN_REG_INFO2_ANT_MAX,
2388 					    tchan_info->antennamax);
2389 
2390 			ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2391 				   "WMI chan scan list chan[%d] = %u, chan_info->info %8x\n",
2392 				   i, chan_info->mhz, chan_info->info);
2393 
2394 			ptr += sizeof(*chan_info);
2395 
2396 			tchan_info++;
2397 		}
2398 
2399 		ret = ath11k_wmi_cmd_send(wmi, skb, WMI_SCAN_CHAN_LIST_CMDID);
2400 		if (ret) {
2401 			ath11k_warn(ar->ab, "failed to send WMI_SCAN_CHAN_LIST cmd\n");
2402 			dev_kfree_skb(skb);
2403 			return ret;
2404 		}
2405 
2406 		num_sends++;
2407 	}
2408 
2409 	return 0;
2410 }
2411 
2412 int ath11k_wmi_send_wmm_update_cmd_tlv(struct ath11k *ar, u32 vdev_id,
2413 				       struct wmi_wmm_params_all_arg *param)
2414 {
2415 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2416 	struct wmi_vdev_set_wmm_params_cmd *cmd;
2417 	struct wmi_wmm_params *wmm_param;
2418 	struct wmi_wmm_params_arg *wmi_wmm_arg;
2419 	struct sk_buff *skb;
2420 	int ret, ac;
2421 
2422 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2423 	if (!skb)
2424 		return -ENOMEM;
2425 
2426 	cmd = (struct wmi_vdev_set_wmm_params_cmd *)skb->data;
2427 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
2428 				     WMI_TAG_VDEV_SET_WMM_PARAMS_CMD) |
2429 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2430 
2431 	cmd->vdev_id = vdev_id;
2432 	cmd->wmm_param_type = 0;
2433 
2434 	for (ac = 0; ac < WME_NUM_AC; ac++) {
2435 		switch (ac) {
2436 		case WME_AC_BE:
2437 			wmi_wmm_arg = &param->ac_be;
2438 			break;
2439 		case WME_AC_BK:
2440 			wmi_wmm_arg = &param->ac_bk;
2441 			break;
2442 		case WME_AC_VI:
2443 			wmi_wmm_arg = &param->ac_vi;
2444 			break;
2445 		case WME_AC_VO:
2446 			wmi_wmm_arg = &param->ac_vo;
2447 			break;
2448 		}
2449 
2450 		wmm_param = (struct wmi_wmm_params *)&cmd->wmm_params[ac];
2451 		wmm_param->tlv_header =
2452 				FIELD_PREP(WMI_TLV_TAG,
2453 					   WMI_TAG_VDEV_SET_WMM_PARAMS_CMD) |
2454 				FIELD_PREP(WMI_TLV_LEN,
2455 					   sizeof(*wmm_param) - TLV_HDR_SIZE);
2456 
2457 		wmm_param->aifs = wmi_wmm_arg->aifs;
2458 		wmm_param->cwmin = wmi_wmm_arg->cwmin;
2459 		wmm_param->cwmax = wmi_wmm_arg->cwmax;
2460 		wmm_param->txoplimit = wmi_wmm_arg->txop;
2461 		wmm_param->acm = wmi_wmm_arg->acm;
2462 		wmm_param->no_ack = wmi_wmm_arg->no_ack;
2463 
2464 		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2465 			   "wmi wmm set ac %d aifs %d cwmin %d cwmax %d txop %d acm %d no_ack %d\n",
2466 			   ac, wmm_param->aifs, wmm_param->cwmin,
2467 			   wmm_param->cwmax, wmm_param->txoplimit,
2468 			   wmm_param->acm, wmm_param->no_ack);
2469 	}
2470 	ret = ath11k_wmi_cmd_send(wmi, skb,
2471 				  WMI_VDEV_SET_WMM_PARAMS_CMDID);
2472 	if (ret) {
2473 		ath11k_warn(ar->ab,
2474 			    "failed to send WMI_VDEV_SET_WMM_PARAMS_CMDID");
2475 		dev_kfree_skb(skb);
2476 	}
2477 
2478 	return ret;
2479 }
2480 
2481 int ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(struct ath11k *ar,
2482 						  u32 pdev_id)
2483 {
2484 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2485 	struct wmi_dfs_phyerr_offload_cmd *cmd;
2486 	struct sk_buff *skb;
2487 	int ret;
2488 
2489 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2490 	if (!skb)
2491 		return -ENOMEM;
2492 
2493 	cmd = (struct wmi_dfs_phyerr_offload_cmd *)skb->data;
2494 	cmd->tlv_header =
2495 		FIELD_PREP(WMI_TLV_TAG,
2496 			   WMI_TAG_PDEV_DFS_PHYERR_OFFLOAD_ENABLE_CMD) |
2497 		FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2498 
2499 	cmd->pdev_id = pdev_id;
2500 
2501 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2502 		   "WMI dfs phy err offload enable pdev id %d\n", pdev_id);
2503 
2504 	ret = ath11k_wmi_cmd_send(wmi, skb,
2505 				  WMI_PDEV_DFS_PHYERR_OFFLOAD_ENABLE_CMDID);
2506 	if (ret) {
2507 		ath11k_warn(ar->ab,
2508 			    "failed to send WMI_PDEV_DFS_PHYERR_OFFLOAD_ENABLE cmd\n");
2509 		dev_kfree_skb(skb);
2510 	}
2511 
2512 	return ret;
2513 }
2514 
2515 int ath11k_wmi_delba_send(struct ath11k *ar, u32 vdev_id, const u8 *mac,
2516 			  u32 tid, u32 initiator, u32 reason)
2517 {
2518 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2519 	struct wmi_delba_send_cmd *cmd;
2520 	struct sk_buff *skb;
2521 	int ret;
2522 
2523 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2524 	if (!skb)
2525 		return -ENOMEM;
2526 
2527 	cmd = (struct wmi_delba_send_cmd *)skb->data;
2528 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_DELBA_SEND_CMD) |
2529 			FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2530 	cmd->vdev_id = vdev_id;
2531 	ether_addr_copy(cmd->peer_macaddr.addr, mac);
2532 	cmd->tid = tid;
2533 	cmd->initiator = initiator;
2534 	cmd->reasoncode = reason;
2535 
2536 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2537 		   "wmi delba send vdev_id 0x%X mac_addr %pM tid %u initiator %u reason %u\n",
2538 		   vdev_id, mac, tid, initiator, reason);
2539 
2540 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_DELBA_SEND_CMDID);
2541 
2542 	if (ret) {
2543 		ath11k_warn(ar->ab,
2544 			    "failed to send WMI_DELBA_SEND_CMDID cmd\n");
2545 		dev_kfree_skb(skb);
2546 	}
2547 
2548 	return ret;
2549 }
2550 
2551 int ath11k_wmi_addba_set_resp(struct ath11k *ar, u32 vdev_id, const u8 *mac,
2552 			      u32 tid, u32 status)
2553 {
2554 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2555 	struct wmi_addba_setresponse_cmd *cmd;
2556 	struct sk_buff *skb;
2557 	int ret;
2558 
2559 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2560 	if (!skb)
2561 		return -ENOMEM;
2562 
2563 	cmd = (struct wmi_addba_setresponse_cmd *)skb->data;
2564 	cmd->tlv_header =
2565 		FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ADDBA_SETRESPONSE_CMD) |
2566 		FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2567 	cmd->vdev_id = vdev_id;
2568 	ether_addr_copy(cmd->peer_macaddr.addr, mac);
2569 	cmd->tid = tid;
2570 	cmd->statuscode = status;
2571 
2572 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2573 		   "wmi addba set resp vdev_id 0x%X mac_addr %pM tid %u status %u\n",
2574 		   vdev_id, mac, tid, status);
2575 
2576 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_ADDBA_SET_RESP_CMDID);
2577 
2578 	if (ret) {
2579 		ath11k_warn(ar->ab,
2580 			    "failed to send WMI_ADDBA_SET_RESP_CMDID cmd\n");
2581 		dev_kfree_skb(skb);
2582 	}
2583 
2584 	return ret;
2585 }
2586 
2587 int ath11k_wmi_addba_send(struct ath11k *ar, u32 vdev_id, const u8 *mac,
2588 			  u32 tid, u32 buf_size)
2589 {
2590 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2591 	struct wmi_addba_send_cmd *cmd;
2592 	struct sk_buff *skb;
2593 	int ret;
2594 
2595 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2596 	if (!skb)
2597 		return -ENOMEM;
2598 
2599 	cmd = (struct wmi_addba_send_cmd *)skb->data;
2600 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ADDBA_SEND_CMD) |
2601 		FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2602 	cmd->vdev_id = vdev_id;
2603 	ether_addr_copy(cmd->peer_macaddr.addr, mac);
2604 	cmd->tid = tid;
2605 	cmd->buffersize = buf_size;
2606 
2607 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2608 		   "wmi addba send vdev_id 0x%X mac_addr %pM tid %u bufsize %u\n",
2609 		   vdev_id, mac, tid, buf_size);
2610 
2611 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_ADDBA_SEND_CMDID);
2612 
2613 	if (ret) {
2614 		ath11k_warn(ar->ab,
2615 			    "failed to send WMI_ADDBA_SEND_CMDID cmd\n");
2616 		dev_kfree_skb(skb);
2617 	}
2618 
2619 	return ret;
2620 }
2621 
2622 int ath11k_wmi_addba_clear_resp(struct ath11k *ar, u32 vdev_id, const u8 *mac)
2623 {
2624 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2625 	struct wmi_addba_clear_resp_cmd *cmd;
2626 	struct sk_buff *skb;
2627 	int ret;
2628 
2629 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2630 	if (!skb)
2631 		return -ENOMEM;
2632 
2633 	cmd = (struct wmi_addba_clear_resp_cmd *)skb->data;
2634 	cmd->tlv_header =
2635 		FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ADDBA_CLEAR_RESP_CMD) |
2636 		FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2637 	cmd->vdev_id = vdev_id;
2638 	ether_addr_copy(cmd->peer_macaddr.addr, mac);
2639 
2640 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2641 		   "wmi addba clear resp vdev_id 0x%X mac_addr %pM\n",
2642 		   vdev_id, mac);
2643 
2644 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_ADDBA_CLEAR_RESP_CMDID);
2645 
2646 	if (ret) {
2647 		ath11k_warn(ar->ab,
2648 			    "failed to send WMI_ADDBA_CLEAR_RESP_CMDID cmd\n");
2649 		dev_kfree_skb(skb);
2650 	}
2651 
2652 	return ret;
2653 }
2654 
2655 int ath11k_wmi_pdev_peer_pktlog_filter(struct ath11k *ar, u8 *addr, u8 enable)
2656 {
2657 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2658 	struct wmi_pdev_pktlog_filter_cmd *cmd;
2659 	struct wmi_pdev_pktlog_filter_info *info;
2660 	struct sk_buff *skb;
2661 	struct wmi_tlv *tlv;
2662 	void *ptr;
2663 	int ret, len;
2664 
2665 	len = sizeof(*cmd) + sizeof(*info) + TLV_HDR_SIZE;
2666 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2667 	if (!skb)
2668 		return -ENOMEM;
2669 
2670 	cmd = (struct wmi_pdev_pktlog_filter_cmd *)skb->data;
2671 
2672 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_PEER_PKTLOG_FILTER_CMD) |
2673 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2674 
2675 	cmd->pdev_id = DP_HW2SW_MACID(ar->pdev->pdev_id);
2676 	cmd->num_mac = 1;
2677 	cmd->enable = enable;
2678 
2679 	ptr = skb->data + sizeof(*cmd);
2680 
2681 	tlv = ptr;
2682 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
2683 		      FIELD_PREP(WMI_TLV_LEN, sizeof(*info));
2684 
2685 	ptr += TLV_HDR_SIZE;
2686 	info = ptr;
2687 
2688 	ether_addr_copy(info->peer_macaddr.addr, addr);
2689 	info->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_PEER_PKTLOG_FILTER_INFO) |
2690 			   FIELD_PREP(WMI_TLV_LEN,
2691 				      sizeof(*info) - TLV_HDR_SIZE);
2692 
2693 	ret = ath11k_wmi_cmd_send(wmi, skb,
2694 				  WMI_PDEV_PKTLOG_FILTER_CMDID);
2695 	if (ret) {
2696 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_PKTLOG_ENABLE_CMDID\n");
2697 		dev_kfree_skb(skb);
2698 	}
2699 
2700 	return ret;
2701 }
2702 
2703 int
2704 ath11k_wmi_send_init_country_cmd(struct ath11k *ar,
2705 				 struct wmi_init_country_params init_cc_params)
2706 {
2707 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2708 	struct wmi_init_country_cmd *cmd;
2709 	struct sk_buff *skb;
2710 	int ret;
2711 
2712 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2713 	if (!skb)
2714 		return -ENOMEM;
2715 
2716 	cmd = (struct wmi_init_country_cmd *)skb->data;
2717 	cmd->tlv_header =
2718 		FIELD_PREP(WMI_TLV_TAG,
2719 			   WMI_TAG_SET_INIT_COUNTRY_CMD) |
2720 		FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2721 
2722 	cmd->pdev_id = ar->pdev->pdev_id;
2723 
2724 	switch (init_cc_params.flags) {
2725 	case ALPHA_IS_SET:
2726 		cmd->init_cc_type = WMI_COUNTRY_INFO_TYPE_ALPHA;
2727 		memcpy((u8 *)&cmd->cc_info.alpha2,
2728 		       init_cc_params.cc_info.alpha2, 3);
2729 		break;
2730 	case CC_IS_SET:
2731 		cmd->init_cc_type = WMI_COUNTRY_INFO_TYPE_COUNTRY_CODE;
2732 		cmd->cc_info.country_code = init_cc_params.cc_info.country_code;
2733 		break;
2734 	case REGDMN_IS_SET:
2735 		cmd->init_cc_type = WMI_COUNTRY_INFO_TYPE_REGDOMAIN;
2736 		cmd->cc_info.regdom_id = init_cc_params.cc_info.regdom_id;
2737 		break;
2738 	default:
2739 		ret = -EINVAL;
2740 		goto out;
2741 	}
2742 
2743 	ret = ath11k_wmi_cmd_send(wmi, skb,
2744 				  WMI_SET_INIT_COUNTRY_CMDID);
2745 
2746 out:
2747 	if (ret) {
2748 		ath11k_warn(ar->ab,
2749 			    "failed to send WMI_SET_INIT_COUNTRY CMD :%d\n",
2750 			    ret);
2751 		dev_kfree_skb(skb);
2752 	}
2753 
2754 	return ret;
2755 }
2756 
2757 int
2758 ath11k_wmi_send_thermal_mitigation_param_cmd(struct ath11k *ar,
2759 					     struct thermal_mitigation_params *param)
2760 {
2761 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2762 	struct wmi_therm_throt_config_request_cmd *cmd;
2763 	struct wmi_therm_throt_level_config_info *lvl_conf;
2764 	struct wmi_tlv *tlv;
2765 	struct sk_buff *skb;
2766 	int i, ret, len;
2767 
2768 	len = sizeof(*cmd) + TLV_HDR_SIZE +
2769 	      THERMAL_LEVELS * sizeof(struct wmi_therm_throt_level_config_info);
2770 
2771 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2772 	if (!skb)
2773 		return -ENOMEM;
2774 
2775 	cmd = (struct wmi_therm_throt_config_request_cmd *)skb->data;
2776 
2777 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_THERM_THROT_CONFIG_REQUEST) |
2778 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2779 
2780 	cmd->pdev_id = ar->pdev->pdev_id;
2781 	cmd->enable = param->enable;
2782 	cmd->dc = param->dc;
2783 	cmd->dc_per_event = param->dc_per_event;
2784 	cmd->therm_throt_levels = THERMAL_LEVELS;
2785 
2786 	tlv = (struct wmi_tlv *)(skb->data + sizeof(*cmd));
2787 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
2788 		      FIELD_PREP(WMI_TLV_LEN,
2789 				 (THERMAL_LEVELS *
2790 				  sizeof(struct wmi_therm_throt_level_config_info)));
2791 
2792 	lvl_conf = (struct wmi_therm_throt_level_config_info *)(skb->data +
2793 								sizeof(*cmd) +
2794 								TLV_HDR_SIZE);
2795 	for (i = 0; i < THERMAL_LEVELS; i++) {
2796 		lvl_conf->tlv_header =
2797 			FIELD_PREP(WMI_TLV_TAG, WMI_TAG_THERM_THROT_LEVEL_CONFIG_INFO) |
2798 			FIELD_PREP(WMI_TLV_LEN, sizeof(*lvl_conf) - TLV_HDR_SIZE);
2799 
2800 		lvl_conf->temp_lwm = param->levelconf[i].tmplwm;
2801 		lvl_conf->temp_hwm = param->levelconf[i].tmphwm;
2802 		lvl_conf->dc_off_percent = param->levelconf[i].dcoffpercent;
2803 		lvl_conf->prio = param->levelconf[i].priority;
2804 		lvl_conf++;
2805 	}
2806 
2807 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_THERM_THROT_SET_CONF_CMDID);
2808 	if (ret) {
2809 		ath11k_warn(ar->ab, "failed to send THERM_THROT_SET_CONF cmd\n");
2810 		dev_kfree_skb(skb);
2811 	}
2812 
2813 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
2814 		   "WMI vdev set thermal throt pdev_id %d enable %d dc %d dc_per_event %x levels %d\n",
2815 		   ar->pdev->pdev_id, param->enable, param->dc,
2816 		   param->dc_per_event, THERMAL_LEVELS);
2817 
2818 	return ret;
2819 }
2820 
2821 int ath11k_wmi_pdev_pktlog_enable(struct ath11k *ar, u32 pktlog_filter)
2822 {
2823 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2824 	struct wmi_pktlog_enable_cmd *cmd;
2825 	struct sk_buff *skb;
2826 	int ret;
2827 
2828 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2829 	if (!skb)
2830 		return -ENOMEM;
2831 
2832 	cmd = (struct wmi_pktlog_enable_cmd *)skb->data;
2833 
2834 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_PKTLOG_ENABLE_CMD) |
2835 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2836 
2837 	cmd->pdev_id = DP_HW2SW_MACID(ar->pdev->pdev_id);
2838 	cmd->evlist = pktlog_filter;
2839 	cmd->enable = ATH11K_WMI_PKTLOG_ENABLE_FORCE;
2840 
2841 	ret = ath11k_wmi_cmd_send(wmi, skb,
2842 				  WMI_PDEV_PKTLOG_ENABLE_CMDID);
2843 	if (ret) {
2844 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_PKTLOG_ENABLE_CMDID\n");
2845 		dev_kfree_skb(skb);
2846 	}
2847 
2848 	return ret;
2849 }
2850 
2851 int ath11k_wmi_pdev_pktlog_disable(struct ath11k *ar)
2852 {
2853 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2854 	struct wmi_pktlog_disable_cmd *cmd;
2855 	struct sk_buff *skb;
2856 	int ret;
2857 
2858 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, sizeof(*cmd));
2859 	if (!skb)
2860 		return -ENOMEM;
2861 
2862 	cmd = (struct wmi_pktlog_disable_cmd *)skb->data;
2863 
2864 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_PKTLOG_DISABLE_CMD) |
2865 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
2866 
2867 	cmd->pdev_id = DP_HW2SW_MACID(ar->pdev->pdev_id);
2868 
2869 	ret = ath11k_wmi_cmd_send(wmi, skb,
2870 				  WMI_PDEV_PKTLOG_DISABLE_CMDID);
2871 	if (ret) {
2872 		ath11k_warn(ar->ab, "failed to send WMI_PDEV_PKTLOG_ENABLE_CMDID\n");
2873 		dev_kfree_skb(skb);
2874 	}
2875 
2876 	return ret;
2877 }
2878 
2879 int
2880 ath11k_wmi_send_twt_enable_cmd(struct ath11k *ar, u32 pdev_id)
2881 {
2882 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2883 	struct ath11k_base *ab = wmi->wmi_ab->ab;
2884 	struct wmi_twt_enable_params_cmd *cmd;
2885 	struct sk_buff *skb;
2886 	int ret, len;
2887 
2888 	len = sizeof(*cmd);
2889 
2890 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2891 	if (!skb)
2892 		return -ENOMEM;
2893 
2894 	cmd = (struct wmi_twt_enable_params_cmd *)skb->data;
2895 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_TWT_ENABLE_CMD) |
2896 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
2897 	cmd->pdev_id = pdev_id;
2898 	cmd->sta_cong_timer_ms = ATH11K_TWT_DEF_STA_CONG_TIMER_MS;
2899 	cmd->default_slot_size = ATH11K_TWT_DEF_DEFAULT_SLOT_SIZE;
2900 	cmd->congestion_thresh_setup = ATH11K_TWT_DEF_CONGESTION_THRESH_SETUP;
2901 	cmd->congestion_thresh_teardown =
2902 		ATH11K_TWT_DEF_CONGESTION_THRESH_TEARDOWN;
2903 	cmd->congestion_thresh_critical =
2904 		ATH11K_TWT_DEF_CONGESTION_THRESH_CRITICAL;
2905 	cmd->interference_thresh_teardown =
2906 		ATH11K_TWT_DEF_INTERFERENCE_THRESH_TEARDOWN;
2907 	cmd->interference_thresh_setup =
2908 		ATH11K_TWT_DEF_INTERFERENCE_THRESH_SETUP;
2909 	cmd->min_no_sta_setup = ATH11K_TWT_DEF_MIN_NO_STA_SETUP;
2910 	cmd->min_no_sta_teardown = ATH11K_TWT_DEF_MIN_NO_STA_TEARDOWN;
2911 	cmd->no_of_bcast_mcast_slots = ATH11K_TWT_DEF_NO_OF_BCAST_MCAST_SLOTS;
2912 	cmd->min_no_twt_slots = ATH11K_TWT_DEF_MIN_NO_TWT_SLOTS;
2913 	cmd->max_no_sta_twt = ATH11K_TWT_DEF_MAX_NO_STA_TWT;
2914 	cmd->mode_check_interval = ATH11K_TWT_DEF_MODE_CHECK_INTERVAL;
2915 	cmd->add_sta_slot_interval = ATH11K_TWT_DEF_ADD_STA_SLOT_INTERVAL;
2916 	cmd->remove_sta_slot_interval =
2917 		ATH11K_TWT_DEF_REMOVE_STA_SLOT_INTERVAL;
2918 	/* TODO add MBSSID support */
2919 	cmd->mbss_support = 0;
2920 
2921 	ret = ath11k_wmi_cmd_send(wmi, skb,
2922 				  WMI_TWT_ENABLE_CMDID);
2923 	if (ret) {
2924 		ath11k_warn(ab, "Failed to send WMI_TWT_ENABLE_CMDID");
2925 		dev_kfree_skb(skb);
2926 	}
2927 	return ret;
2928 }
2929 
2930 int
2931 ath11k_wmi_send_twt_disable_cmd(struct ath11k *ar, u32 pdev_id)
2932 {
2933 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2934 	struct ath11k_base *ab = wmi->wmi_ab->ab;
2935 	struct wmi_twt_disable_params_cmd *cmd;
2936 	struct sk_buff *skb;
2937 	int ret, len;
2938 
2939 	len = sizeof(*cmd);
2940 
2941 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2942 	if (!skb)
2943 		return -ENOMEM;
2944 
2945 	cmd = (struct wmi_twt_disable_params_cmd *)skb->data;
2946 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_TWT_DISABLE_CMD) |
2947 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
2948 	cmd->pdev_id = pdev_id;
2949 
2950 	ret = ath11k_wmi_cmd_send(wmi, skb,
2951 				  WMI_TWT_DISABLE_CMDID);
2952 	if (ret) {
2953 		ath11k_warn(ab, "Failed to send WMI_TWT_DISABLE_CMDID");
2954 		dev_kfree_skb(skb);
2955 	}
2956 	return ret;
2957 }
2958 
2959 int
2960 ath11k_wmi_send_obss_spr_cmd(struct ath11k *ar, u32 vdev_id,
2961 			     struct ieee80211_he_obss_pd *he_obss_pd)
2962 {
2963 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2964 	struct ath11k_base *ab = wmi->wmi_ab->ab;
2965 	struct wmi_obss_spatial_reuse_params_cmd *cmd;
2966 	struct sk_buff *skb;
2967 	int ret, len;
2968 
2969 	len = sizeof(*cmd);
2970 
2971 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
2972 	if (!skb)
2973 		return -ENOMEM;
2974 
2975 	cmd = (struct wmi_obss_spatial_reuse_params_cmd *)skb->data;
2976 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
2977 				     WMI_TAG_OBSS_SPATIAL_REUSE_SET_CMD) |
2978 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
2979 	cmd->vdev_id = vdev_id;
2980 	cmd->enable = he_obss_pd->enable;
2981 	cmd->obss_min = he_obss_pd->min_offset;
2982 	cmd->obss_max = he_obss_pd->max_offset;
2983 
2984 	ret = ath11k_wmi_cmd_send(wmi, skb,
2985 				  WMI_PDEV_OBSS_PD_SPATIAL_REUSE_CMDID);
2986 	if (ret) {
2987 		ath11k_warn(ab,
2988 			    "Failed to send WMI_PDEV_OBSS_PD_SPATIAL_REUSE_CMDID");
2989 		dev_kfree_skb(skb);
2990 	}
2991 	return ret;
2992 }
2993 
2994 int
2995 ath11k_wmi_pdev_set_srg_bss_color_bitmap(struct ath11k *ar, u32 *bitmap)
2996 {
2997 	struct ath11k_pdev_wmi *wmi = ar->wmi;
2998 	struct ath11k_base *ab = wmi->wmi_ab->ab;
2999 	struct wmi_pdev_obss_pd_bitmap_cmd *cmd;
3000 	struct sk_buff *skb;
3001 	int ret, len;
3002 
3003 	len = sizeof(*cmd);
3004 
3005 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3006 	if (!skb)
3007 		return -ENOMEM;
3008 
3009 	cmd = (struct wmi_pdev_obss_pd_bitmap_cmd *)skb->data;
3010 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3011 				     WMI_TAG_PDEV_SRG_BSS_COLOR_BITMAP_CMD) |
3012 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3013 	cmd->pdev_id = ar->pdev->pdev_id;
3014 	memcpy(cmd->bitmap, bitmap, sizeof(cmd->bitmap));
3015 
3016 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3017 		   "obss pd pdev_id %d bss color bitmap %08x %08x\n",
3018 		   cmd->pdev_id, cmd->bitmap[0], cmd->bitmap[1]);
3019 
3020 	ret = ath11k_wmi_cmd_send(wmi, skb,
3021 				  WMI_PDEV_SET_SRG_BSS_COLOR_BITMAP_CMDID);
3022 	if (ret) {
3023 		ath11k_warn(ab,
3024 			    "failed to send WMI_PDEV_SET_SRG_BSS_COLOR_BITMAP_CMDID");
3025 		dev_kfree_skb(skb);
3026 	}
3027 
3028 	return ret;
3029 }
3030 
3031 int
3032 ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(struct ath11k *ar, u32 *bitmap)
3033 {
3034 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3035 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3036 	struct wmi_pdev_obss_pd_bitmap_cmd *cmd;
3037 	struct sk_buff *skb;
3038 	int ret, len;
3039 
3040 	len = sizeof(*cmd);
3041 
3042 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3043 	if (!skb)
3044 		return -ENOMEM;
3045 
3046 	cmd = (struct wmi_pdev_obss_pd_bitmap_cmd *)skb->data;
3047 	cmd->tlv_header =
3048 		FIELD_PREP(WMI_TLV_TAG,
3049 			   WMI_TAG_PDEV_SRG_PARTIAL_BSSID_BITMAP_CMD) |
3050 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3051 	cmd->pdev_id = ar->pdev->pdev_id;
3052 	memcpy(cmd->bitmap, bitmap, sizeof(cmd->bitmap));
3053 
3054 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3055 		   "obss pd pdev_id %d partial bssid bitmap %08x %08x\n",
3056 		   cmd->pdev_id, cmd->bitmap[0], cmd->bitmap[1]);
3057 
3058 	ret = ath11k_wmi_cmd_send(wmi, skb,
3059 				  WMI_PDEV_SET_SRG_PARTIAL_BSSID_BITMAP_CMDID);
3060 	if (ret) {
3061 		ath11k_warn(ab,
3062 			    "failed to send WMI_PDEV_SET_SRG_PARTIAL_BSSID_BITMAP_CMDID");
3063 		dev_kfree_skb(skb);
3064 	}
3065 
3066 	return ret;
3067 }
3068 
3069 int
3070 ath11k_wmi_pdev_srg_obss_color_enable_bitmap(struct ath11k *ar, u32 *bitmap)
3071 {
3072 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3073 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3074 	struct wmi_pdev_obss_pd_bitmap_cmd *cmd;
3075 	struct sk_buff *skb;
3076 	int ret, len;
3077 
3078 	len = sizeof(*cmd);
3079 
3080 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3081 	if (!skb)
3082 		return -ENOMEM;
3083 
3084 	cmd = (struct wmi_pdev_obss_pd_bitmap_cmd *)skb->data;
3085 	cmd->tlv_header =
3086 		FIELD_PREP(WMI_TLV_TAG,
3087 			   WMI_TAG_PDEV_SRG_OBSS_COLOR_ENABLE_BITMAP_CMD) |
3088 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3089 	cmd->pdev_id = ar->pdev->pdev_id;
3090 	memcpy(cmd->bitmap, bitmap, sizeof(cmd->bitmap));
3091 
3092 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3093 		   "obss pd srg pdev_id %d bss color enable bitmap %08x %08x\n",
3094 		   cmd->pdev_id, cmd->bitmap[0], cmd->bitmap[1]);
3095 
3096 	ret = ath11k_wmi_cmd_send(wmi, skb,
3097 				  WMI_PDEV_SET_SRG_OBSS_COLOR_ENABLE_BITMAP_CMDID);
3098 	if (ret) {
3099 		ath11k_warn(ab,
3100 			    "failed to send WMI_PDEV_SET_SRG_OBSS_COLOR_ENABLE_BITMAP_CMDID");
3101 		dev_kfree_skb(skb);
3102 	}
3103 
3104 	return ret;
3105 }
3106 
3107 int
3108 ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(struct ath11k *ar, u32 *bitmap)
3109 {
3110 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3111 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3112 	struct wmi_pdev_obss_pd_bitmap_cmd *cmd;
3113 	struct sk_buff *skb;
3114 	int ret, len;
3115 
3116 	len = sizeof(*cmd);
3117 
3118 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3119 	if (!skb)
3120 		return -ENOMEM;
3121 
3122 	cmd = (struct wmi_pdev_obss_pd_bitmap_cmd *)skb->data;
3123 	cmd->tlv_header =
3124 		FIELD_PREP(WMI_TLV_TAG,
3125 			   WMI_TAG_PDEV_SRG_OBSS_BSSID_ENABLE_BITMAP_CMD) |
3126 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3127 	cmd->pdev_id = ar->pdev->pdev_id;
3128 	memcpy(cmd->bitmap, bitmap, sizeof(cmd->bitmap));
3129 
3130 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3131 		   "obss pd srg pdev_id %d bssid enable bitmap %08x %08x\n",
3132 		   cmd->pdev_id, cmd->bitmap[0], cmd->bitmap[1]);
3133 
3134 	ret = ath11k_wmi_cmd_send(wmi, skb,
3135 				  WMI_PDEV_SET_SRG_OBSS_BSSID_ENABLE_BITMAP_CMDID);
3136 	if (ret) {
3137 		ath11k_warn(ab,
3138 			    "failed to send WMI_PDEV_SET_SRG_OBSS_BSSID_ENABLE_BITMAP_CMDID");
3139 		dev_kfree_skb(skb);
3140 	}
3141 
3142 	return ret;
3143 }
3144 
3145 int
3146 ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(struct ath11k *ar, u32 *bitmap)
3147 {
3148 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3149 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3150 	struct wmi_pdev_obss_pd_bitmap_cmd *cmd;
3151 	struct sk_buff *skb;
3152 	int ret, len;
3153 
3154 	len = sizeof(*cmd);
3155 
3156 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3157 	if (!skb)
3158 		return -ENOMEM;
3159 
3160 	cmd = (struct wmi_pdev_obss_pd_bitmap_cmd *)skb->data;
3161 	cmd->tlv_header =
3162 		FIELD_PREP(WMI_TLV_TAG,
3163 			   WMI_TAG_PDEV_NON_SRG_OBSS_COLOR_ENABLE_BITMAP_CMD) |
3164 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3165 	cmd->pdev_id = ar->pdev->pdev_id;
3166 	memcpy(cmd->bitmap, bitmap, sizeof(cmd->bitmap));
3167 
3168 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3169 		   "obss pd non_srg pdev_id %d bss color enable bitmap %08x %08x\n",
3170 		   cmd->pdev_id, cmd->bitmap[0], cmd->bitmap[1]);
3171 
3172 	ret = ath11k_wmi_cmd_send(wmi, skb,
3173 				  WMI_PDEV_SET_NON_SRG_OBSS_COLOR_ENABLE_BITMAP_CMDID);
3174 	if (ret) {
3175 		ath11k_warn(ab,
3176 			    "failed to send WMI_PDEV_SET_NON_SRG_OBSS_COLOR_ENABLE_BITMAP_CMDID");
3177 		dev_kfree_skb(skb);
3178 	}
3179 
3180 	return ret;
3181 }
3182 
3183 int
3184 ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(struct ath11k *ar, u32 *bitmap)
3185 {
3186 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3187 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3188 	struct wmi_pdev_obss_pd_bitmap_cmd *cmd;
3189 	struct sk_buff *skb;
3190 	int ret, len;
3191 
3192 	len = sizeof(*cmd);
3193 
3194 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3195 	if (!skb)
3196 		return -ENOMEM;
3197 
3198 	cmd = (struct wmi_pdev_obss_pd_bitmap_cmd *)skb->data;
3199 	cmd->tlv_header =
3200 		FIELD_PREP(WMI_TLV_TAG,
3201 			   WMI_TAG_PDEV_NON_SRG_OBSS_BSSID_ENABLE_BITMAP_CMD) |
3202 		FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3203 	cmd->pdev_id = ar->pdev->pdev_id;
3204 	memcpy(cmd->bitmap, bitmap, sizeof(cmd->bitmap));
3205 
3206 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3207 		   "obss pd non_srg pdev_id %d bssid enable bitmap %08x %08x\n",
3208 		   cmd->pdev_id, cmd->bitmap[0], cmd->bitmap[1]);
3209 
3210 	ret = ath11k_wmi_cmd_send(wmi, skb,
3211 				  WMI_PDEV_SET_NON_SRG_OBSS_BSSID_ENABLE_BITMAP_CMDID);
3212 	if (ret) {
3213 		ath11k_warn(ab,
3214 			    "failed to send WMI_PDEV_SET_NON_SRG_OBSS_BSSID_ENABLE_BITMAP_CMDID");
3215 		dev_kfree_skb(skb);
3216 	}
3217 
3218 	return ret;
3219 }
3220 
3221 int
3222 ath11k_wmi_send_obss_color_collision_cfg_cmd(struct ath11k *ar, u32 vdev_id,
3223 					     u8 bss_color, u32 period,
3224 					     bool enable)
3225 {
3226 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3227 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3228 	struct wmi_obss_color_collision_cfg_params_cmd *cmd;
3229 	struct sk_buff *skb;
3230 	int ret, len;
3231 
3232 	len = sizeof(*cmd);
3233 
3234 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3235 	if (!skb)
3236 		return -ENOMEM;
3237 
3238 	cmd = (struct wmi_obss_color_collision_cfg_params_cmd *)skb->data;
3239 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3240 				     WMI_TAG_OBSS_COLOR_COLLISION_DET_CONFIG) |
3241 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3242 	cmd->vdev_id = vdev_id;
3243 	cmd->evt_type = enable ? ATH11K_OBSS_COLOR_COLLISION_DETECTION :
3244 				 ATH11K_OBSS_COLOR_COLLISION_DETECTION_DISABLE;
3245 	cmd->current_bss_color = bss_color;
3246 	cmd->detection_period_ms = period;
3247 	cmd->scan_period_ms = ATH11K_BSS_COLOR_COLLISION_SCAN_PERIOD_MS;
3248 	cmd->free_slot_expiry_time_ms = 0;
3249 	cmd->flags = 0;
3250 
3251 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3252 		   "wmi_send_obss_color_collision_cfg id %d type %d bss_color %d detect_period %d scan_period %d\n",
3253 		   cmd->vdev_id, cmd->evt_type, cmd->current_bss_color,
3254 		   cmd->detection_period_ms, cmd->scan_period_ms);
3255 
3256 	ret = ath11k_wmi_cmd_send(wmi, skb,
3257 				  WMI_OBSS_COLOR_COLLISION_DET_CONFIG_CMDID);
3258 	if (ret) {
3259 		ath11k_warn(ab, "Failed to send WMI_OBSS_COLOR_COLLISION_DET_CONFIG_CMDID");
3260 		dev_kfree_skb(skb);
3261 	}
3262 	return ret;
3263 }
3264 
3265 int ath11k_wmi_send_bss_color_change_enable_cmd(struct ath11k *ar, u32 vdev_id,
3266 						bool enable)
3267 {
3268 	struct ath11k_pdev_wmi *wmi = ar->wmi;
3269 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3270 	struct wmi_bss_color_change_enable_params_cmd *cmd;
3271 	struct sk_buff *skb;
3272 	int ret, len;
3273 
3274 	len = sizeof(*cmd);
3275 
3276 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3277 	if (!skb)
3278 		return -ENOMEM;
3279 
3280 	cmd = (struct wmi_bss_color_change_enable_params_cmd *)skb->data;
3281 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_BSS_COLOR_CHANGE_ENABLE) |
3282 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3283 	cmd->vdev_id = vdev_id;
3284 	cmd->enable = enable ? 1 : 0;
3285 
3286 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3287 		   "wmi_send_bss_color_change_enable id %d enable %d\n",
3288 		   cmd->vdev_id, cmd->enable);
3289 
3290 	ret = ath11k_wmi_cmd_send(wmi, skb,
3291 				  WMI_BSS_COLOR_CHANGE_ENABLE_CMDID);
3292 	if (ret) {
3293 		ath11k_warn(ab, "Failed to send WMI_BSS_COLOR_CHANGE_ENABLE_CMDID");
3294 		dev_kfree_skb(skb);
3295 	}
3296 	return ret;
3297 }
3298 
3299 int ath11k_wmi_fils_discovery_tmpl(struct ath11k *ar, u32 vdev_id,
3300 				   struct sk_buff *tmpl)
3301 {
3302 	struct wmi_tlv *tlv;
3303 	struct sk_buff *skb;
3304 	void *ptr;
3305 	int ret, len;
3306 	size_t aligned_len;
3307 	struct wmi_fils_discovery_tmpl_cmd *cmd;
3308 
3309 	aligned_len = roundup(tmpl->len, 4);
3310 	len = sizeof(*cmd) + TLV_HDR_SIZE + aligned_len;
3311 
3312 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3313 		   "WMI vdev %i set FILS discovery template\n", vdev_id);
3314 
3315 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
3316 	if (!skb)
3317 		return -ENOMEM;
3318 
3319 	cmd = (struct wmi_fils_discovery_tmpl_cmd *)skb->data;
3320 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3321 				     WMI_TAG_FILS_DISCOVERY_TMPL_CMD) |
3322 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3323 	cmd->vdev_id = vdev_id;
3324 	cmd->buf_len = tmpl->len;
3325 	ptr = skb->data + sizeof(*cmd);
3326 
3327 	tlv = ptr;
3328 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
3329 		      FIELD_PREP(WMI_TLV_LEN, aligned_len);
3330 	memcpy(tlv->value, tmpl->data, tmpl->len);
3331 
3332 	ret = ath11k_wmi_cmd_send(ar->wmi, skb, WMI_FILS_DISCOVERY_TMPL_CMDID);
3333 	if (ret) {
3334 		ath11k_warn(ar->ab,
3335 			    "WMI vdev %i failed to send FILS discovery template command\n",
3336 			    vdev_id);
3337 		dev_kfree_skb(skb);
3338 	}
3339 	return ret;
3340 }
3341 
3342 int ath11k_wmi_probe_resp_tmpl(struct ath11k *ar, u32 vdev_id,
3343 			       struct sk_buff *tmpl)
3344 {
3345 	struct wmi_probe_tmpl_cmd *cmd;
3346 	struct wmi_bcn_prb_info *probe_info;
3347 	struct wmi_tlv *tlv;
3348 	struct sk_buff *skb;
3349 	void *ptr;
3350 	int ret, len;
3351 	size_t aligned_len = roundup(tmpl->len, 4);
3352 
3353 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3354 		   "WMI vdev %i set probe response template\n", vdev_id);
3355 
3356 	len = sizeof(*cmd) + sizeof(*probe_info) + TLV_HDR_SIZE + aligned_len;
3357 
3358 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
3359 	if (!skb)
3360 		return -ENOMEM;
3361 
3362 	cmd = (struct wmi_probe_tmpl_cmd *)skb->data;
3363 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PRB_TMPL_CMD) |
3364 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3365 	cmd->vdev_id = vdev_id;
3366 	cmd->buf_len = tmpl->len;
3367 
3368 	ptr = skb->data + sizeof(*cmd);
3369 
3370 	probe_info = ptr;
3371 	len = sizeof(*probe_info);
3372 	probe_info->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3373 					    WMI_TAG_BCN_PRB_INFO) |
3374 				 FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3375 	probe_info->caps = 0;
3376 	probe_info->erp = 0;
3377 
3378 	ptr += sizeof(*probe_info);
3379 
3380 	tlv = ptr;
3381 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_BYTE) |
3382 		      FIELD_PREP(WMI_TLV_LEN, aligned_len);
3383 	memcpy(tlv->value, tmpl->data, tmpl->len);
3384 
3385 	ret = ath11k_wmi_cmd_send(ar->wmi, skb, WMI_PRB_TMPL_CMDID);
3386 	if (ret) {
3387 		ath11k_warn(ar->ab,
3388 			    "WMI vdev %i failed to send probe response template command\n",
3389 			    vdev_id);
3390 		dev_kfree_skb(skb);
3391 	}
3392 	return ret;
3393 }
3394 
3395 int ath11k_wmi_fils_discovery(struct ath11k *ar, u32 vdev_id, u32 interval,
3396 			      bool unsol_bcast_probe_resp_enabled)
3397 {
3398 	struct sk_buff *skb;
3399 	int ret, len;
3400 	struct wmi_fils_discovery_cmd *cmd;
3401 
3402 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3403 		   "WMI vdev %i set %s interval to %u TU\n",
3404 		   vdev_id, unsol_bcast_probe_resp_enabled ?
3405 		   "unsolicited broadcast probe response" : "FILS discovery",
3406 		   interval);
3407 
3408 	len = sizeof(*cmd);
3409 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
3410 	if (!skb)
3411 		return -ENOMEM;
3412 
3413 	cmd = (struct wmi_fils_discovery_cmd *)skb->data;
3414 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ENABLE_FILS_CMD) |
3415 			  FIELD_PREP(WMI_TLV_LEN, len - TLV_HDR_SIZE);
3416 	cmd->vdev_id = vdev_id;
3417 	cmd->interval = interval;
3418 	cmd->config = unsol_bcast_probe_resp_enabled;
3419 
3420 	ret = ath11k_wmi_cmd_send(ar->wmi, skb, WMI_ENABLE_FILS_CMDID);
3421 	if (ret) {
3422 		ath11k_warn(ar->ab,
3423 			    "WMI vdev %i failed to send FILS discovery enable/disable command\n",
3424 			    vdev_id);
3425 		dev_kfree_skb(skb);
3426 	}
3427 	return ret;
3428 }
3429 
3430 static void
3431 ath11k_fill_band_to_mac_param(struct ath11k_base  *soc,
3432 			      struct wmi_host_pdev_band_to_mac *band_to_mac)
3433 {
3434 	u8 i;
3435 	struct ath11k_hal_reg_capabilities_ext *hal_reg_cap;
3436 	struct ath11k_pdev *pdev;
3437 
3438 	for (i = 0; i < soc->num_radios; i++) {
3439 		pdev = &soc->pdevs[i];
3440 		hal_reg_cap = &soc->hal_reg_cap[i];
3441 		band_to_mac[i].pdev_id = pdev->pdev_id;
3442 
3443 		switch (pdev->cap.supported_bands) {
3444 		case WMI_HOST_WLAN_2G_5G_CAP:
3445 			band_to_mac[i].start_freq = hal_reg_cap->low_2ghz_chan;
3446 			band_to_mac[i].end_freq = hal_reg_cap->high_5ghz_chan;
3447 			break;
3448 		case WMI_HOST_WLAN_2G_CAP:
3449 			band_to_mac[i].start_freq = hal_reg_cap->low_2ghz_chan;
3450 			band_to_mac[i].end_freq = hal_reg_cap->high_2ghz_chan;
3451 			break;
3452 		case WMI_HOST_WLAN_5G_CAP:
3453 			band_to_mac[i].start_freq = hal_reg_cap->low_5ghz_chan;
3454 			band_to_mac[i].end_freq = hal_reg_cap->high_5ghz_chan;
3455 			break;
3456 		default:
3457 			break;
3458 		}
3459 	}
3460 }
3461 
3462 static void
3463 ath11k_wmi_copy_resource_config(struct wmi_resource_config *wmi_cfg,
3464 				struct target_resource_config *tg_cfg)
3465 {
3466 	wmi_cfg->num_vdevs = tg_cfg->num_vdevs;
3467 	wmi_cfg->num_peers = tg_cfg->num_peers;
3468 	wmi_cfg->num_offload_peers = tg_cfg->num_offload_peers;
3469 	wmi_cfg->num_offload_reorder_buffs = tg_cfg->num_offload_reorder_buffs;
3470 	wmi_cfg->num_peer_keys = tg_cfg->num_peer_keys;
3471 	wmi_cfg->num_tids = tg_cfg->num_tids;
3472 	wmi_cfg->ast_skid_limit = tg_cfg->ast_skid_limit;
3473 	wmi_cfg->tx_chain_mask = tg_cfg->tx_chain_mask;
3474 	wmi_cfg->rx_chain_mask = tg_cfg->rx_chain_mask;
3475 	wmi_cfg->rx_timeout_pri[0] = tg_cfg->rx_timeout_pri[0];
3476 	wmi_cfg->rx_timeout_pri[1] = tg_cfg->rx_timeout_pri[1];
3477 	wmi_cfg->rx_timeout_pri[2] = tg_cfg->rx_timeout_pri[2];
3478 	wmi_cfg->rx_timeout_pri[3] = tg_cfg->rx_timeout_pri[3];
3479 	wmi_cfg->rx_decap_mode = tg_cfg->rx_decap_mode;
3480 	wmi_cfg->scan_max_pending_req = tg_cfg->scan_max_pending_req;
3481 	wmi_cfg->bmiss_offload_max_vdev = tg_cfg->bmiss_offload_max_vdev;
3482 	wmi_cfg->roam_offload_max_vdev = tg_cfg->roam_offload_max_vdev;
3483 	wmi_cfg->roam_offload_max_ap_profiles =
3484 		tg_cfg->roam_offload_max_ap_profiles;
3485 	wmi_cfg->num_mcast_groups = tg_cfg->num_mcast_groups;
3486 	wmi_cfg->num_mcast_table_elems = tg_cfg->num_mcast_table_elems;
3487 	wmi_cfg->mcast2ucast_mode = tg_cfg->mcast2ucast_mode;
3488 	wmi_cfg->tx_dbg_log_size = tg_cfg->tx_dbg_log_size;
3489 	wmi_cfg->num_wds_entries = tg_cfg->num_wds_entries;
3490 	wmi_cfg->dma_burst_size = tg_cfg->dma_burst_size;
3491 	wmi_cfg->mac_aggr_delim = tg_cfg->mac_aggr_delim;
3492 	wmi_cfg->rx_skip_defrag_timeout_dup_detection_check =
3493 		tg_cfg->rx_skip_defrag_timeout_dup_detection_check;
3494 	wmi_cfg->vow_config = tg_cfg->vow_config;
3495 	wmi_cfg->gtk_offload_max_vdev = tg_cfg->gtk_offload_max_vdev;
3496 	wmi_cfg->num_msdu_desc = tg_cfg->num_msdu_desc;
3497 	wmi_cfg->max_frag_entries = tg_cfg->max_frag_entries;
3498 	wmi_cfg->num_tdls_vdevs = tg_cfg->num_tdls_vdevs;
3499 	wmi_cfg->num_tdls_conn_table_entries =
3500 		tg_cfg->num_tdls_conn_table_entries;
3501 	wmi_cfg->beacon_tx_offload_max_vdev =
3502 		tg_cfg->beacon_tx_offload_max_vdev;
3503 	wmi_cfg->num_multicast_filter_entries =
3504 		tg_cfg->num_multicast_filter_entries;
3505 	wmi_cfg->num_wow_filters = tg_cfg->num_wow_filters;
3506 	wmi_cfg->num_keep_alive_pattern = tg_cfg->num_keep_alive_pattern;
3507 	wmi_cfg->keep_alive_pattern_size = tg_cfg->keep_alive_pattern_size;
3508 	wmi_cfg->max_tdls_concurrent_sleep_sta =
3509 		tg_cfg->max_tdls_concurrent_sleep_sta;
3510 	wmi_cfg->max_tdls_concurrent_buffer_sta =
3511 		tg_cfg->max_tdls_concurrent_buffer_sta;
3512 	wmi_cfg->wmi_send_separate = tg_cfg->wmi_send_separate;
3513 	wmi_cfg->num_ocb_vdevs = tg_cfg->num_ocb_vdevs;
3514 	wmi_cfg->num_ocb_channels = tg_cfg->num_ocb_channels;
3515 	wmi_cfg->num_ocb_schedules = tg_cfg->num_ocb_schedules;
3516 	wmi_cfg->bpf_instruction_size = tg_cfg->bpf_instruction_size;
3517 	wmi_cfg->max_bssid_rx_filters = tg_cfg->max_bssid_rx_filters;
3518 	wmi_cfg->use_pdev_id = tg_cfg->use_pdev_id;
3519 	wmi_cfg->flag1 = tg_cfg->flag1;
3520 	wmi_cfg->peer_map_unmap_v2_support = tg_cfg->peer_map_unmap_v2_support;
3521 	wmi_cfg->sched_params = tg_cfg->sched_params;
3522 	wmi_cfg->twt_ap_pdev_count = tg_cfg->twt_ap_pdev_count;
3523 	wmi_cfg->twt_ap_sta_count = tg_cfg->twt_ap_sta_count;
3524 }
3525 
3526 static int ath11k_init_cmd_send(struct ath11k_pdev_wmi *wmi,
3527 				struct wmi_init_cmd_param *param)
3528 {
3529 	struct ath11k_base *ab = wmi->wmi_ab->ab;
3530 	struct sk_buff *skb;
3531 	struct wmi_init_cmd *cmd;
3532 	struct wmi_resource_config *cfg;
3533 	struct wmi_pdev_set_hw_mode_cmd_param *hw_mode;
3534 	struct wmi_pdev_band_to_mac *band_to_mac;
3535 	struct wlan_host_mem_chunk *host_mem_chunks;
3536 	struct wmi_tlv *tlv;
3537 	size_t ret, len;
3538 	void *ptr;
3539 	u32 hw_mode_len = 0;
3540 	u16 idx;
3541 
3542 	if (param->hw_mode_id != WMI_HOST_HW_MODE_MAX)
3543 		hw_mode_len = sizeof(*hw_mode) + TLV_HDR_SIZE +
3544 			      (param->num_band_to_mac * sizeof(*band_to_mac));
3545 
3546 	len = sizeof(*cmd) + TLV_HDR_SIZE + sizeof(*cfg) + hw_mode_len +
3547 	      (param->num_mem_chunks ? (sizeof(*host_mem_chunks) * WMI_MAX_MEM_REQS) : 0);
3548 
3549 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, len);
3550 	if (!skb)
3551 		return -ENOMEM;
3552 
3553 	cmd = (struct wmi_init_cmd *)skb->data;
3554 
3555 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_INIT_CMD) |
3556 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3557 
3558 	ptr = skb->data + sizeof(*cmd);
3559 	cfg = ptr;
3560 
3561 	ath11k_wmi_copy_resource_config(cfg, param->res_cfg);
3562 
3563 	cfg->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_RESOURCE_CONFIG) |
3564 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cfg) - TLV_HDR_SIZE);
3565 
3566 	ptr += sizeof(*cfg);
3567 	host_mem_chunks = ptr + TLV_HDR_SIZE;
3568 	len = sizeof(struct wlan_host_mem_chunk);
3569 
3570 	for (idx = 0; idx < param->num_mem_chunks; ++idx) {
3571 		host_mem_chunks[idx].tlv_header =
3572 				FIELD_PREP(WMI_TLV_TAG,
3573 					   WMI_TAG_WLAN_HOST_MEMORY_CHUNK) |
3574 				FIELD_PREP(WMI_TLV_LEN, len);
3575 
3576 		host_mem_chunks[idx].ptr = param->mem_chunks[idx].paddr;
3577 		host_mem_chunks[idx].size = param->mem_chunks[idx].len;
3578 		host_mem_chunks[idx].req_id = param->mem_chunks[idx].req_id;
3579 
3580 		ath11k_dbg(ab, ATH11K_DBG_WMI,
3581 			   "WMI host mem chunk req_id %d paddr 0x%llx len %d\n",
3582 			   param->mem_chunks[idx].req_id,
3583 			   (u64)param->mem_chunks[idx].paddr,
3584 			   param->mem_chunks[idx].len);
3585 	}
3586 	cmd->num_host_mem_chunks = param->num_mem_chunks;
3587 	len = sizeof(struct wlan_host_mem_chunk) * param->num_mem_chunks;
3588 
3589 	/* num_mem_chunks is zero */
3590 	tlv = ptr;
3591 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
3592 		      FIELD_PREP(WMI_TLV_LEN, len);
3593 	ptr += TLV_HDR_SIZE + len;
3594 
3595 	if (param->hw_mode_id != WMI_HOST_HW_MODE_MAX) {
3596 		hw_mode = (struct wmi_pdev_set_hw_mode_cmd_param *)ptr;
3597 		hw_mode->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3598 						 WMI_TAG_PDEV_SET_HW_MODE_CMD) |
3599 				      FIELD_PREP(WMI_TLV_LEN,
3600 						 sizeof(*hw_mode) - TLV_HDR_SIZE);
3601 
3602 		hw_mode->hw_mode_index = param->hw_mode_id;
3603 		hw_mode->num_band_to_mac = param->num_band_to_mac;
3604 
3605 		ptr += sizeof(*hw_mode);
3606 
3607 		len = param->num_band_to_mac * sizeof(*band_to_mac);
3608 		tlv = ptr;
3609 		tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_STRUCT) |
3610 			      FIELD_PREP(WMI_TLV_LEN, len);
3611 
3612 		ptr += TLV_HDR_SIZE;
3613 		len = sizeof(*band_to_mac);
3614 
3615 		for (idx = 0; idx < param->num_band_to_mac; idx++) {
3616 			band_to_mac = (void *)ptr;
3617 
3618 			band_to_mac->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3619 							     WMI_TAG_PDEV_BAND_TO_MAC) |
3620 						  FIELD_PREP(WMI_TLV_LEN,
3621 							     len - TLV_HDR_SIZE);
3622 			band_to_mac->pdev_id = param->band_to_mac[idx].pdev_id;
3623 			band_to_mac->start_freq =
3624 				param->band_to_mac[idx].start_freq;
3625 			band_to_mac->end_freq =
3626 				param->band_to_mac[idx].end_freq;
3627 			ptr += sizeof(*band_to_mac);
3628 		}
3629 	}
3630 
3631 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_INIT_CMDID);
3632 	if (ret) {
3633 		ath11k_warn(ab, "failed to send WMI_INIT_CMDID\n");
3634 		dev_kfree_skb(skb);
3635 	}
3636 
3637 	return ret;
3638 }
3639 
3640 int ath11k_wmi_pdev_lro_cfg(struct ath11k *ar,
3641 			    int pdev_id)
3642 {
3643 	struct ath11k_wmi_pdev_lro_config_cmd *cmd;
3644 	struct sk_buff *skb;
3645 	int ret;
3646 
3647 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, sizeof(*cmd));
3648 	if (!skb)
3649 		return -ENOMEM;
3650 
3651 	cmd = (struct ath11k_wmi_pdev_lro_config_cmd *)skb->data;
3652 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_LRO_INFO_CMD) |
3653 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3654 
3655 	get_random_bytes(cmd->th_4, sizeof(uint32_t) * ATH11K_IPV4_TH_SEED_SIZE);
3656 	get_random_bytes(cmd->th_6, sizeof(uint32_t) * ATH11K_IPV6_TH_SEED_SIZE);
3657 
3658 	cmd->pdev_id = pdev_id;
3659 
3660 	ret = ath11k_wmi_cmd_send(ar->wmi, skb, WMI_LRO_CONFIG_CMDID);
3661 	if (ret) {
3662 		ath11k_warn(ar->ab,
3663 			    "failed to send lro cfg req wmi cmd\n");
3664 		goto err;
3665 	}
3666 
3667 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3668 		   "WMI lro cfg cmd pdev_id 0x%x\n", pdev_id);
3669 	return 0;
3670 err:
3671 	dev_kfree_skb(skb);
3672 	return ret;
3673 }
3674 
3675 int ath11k_wmi_wait_for_service_ready(struct ath11k_base *ab)
3676 {
3677 	unsigned long time_left;
3678 
3679 	time_left = wait_for_completion_timeout(&ab->wmi_ab.service_ready,
3680 						WMI_SERVICE_READY_TIMEOUT_HZ);
3681 	if (!time_left)
3682 		return -ETIMEDOUT;
3683 
3684 	return 0;
3685 }
3686 
3687 int ath11k_wmi_wait_for_unified_ready(struct ath11k_base *ab)
3688 {
3689 	unsigned long time_left;
3690 
3691 	time_left = wait_for_completion_timeout(&ab->wmi_ab.unified_ready,
3692 						WMI_SERVICE_READY_TIMEOUT_HZ);
3693 	if (!time_left)
3694 		return -ETIMEDOUT;
3695 
3696 	return 0;
3697 }
3698 
3699 int ath11k_wmi_set_hw_mode(struct ath11k_base *ab,
3700 			   enum wmi_host_hw_mode_config_type mode)
3701 {
3702 	struct wmi_pdev_set_hw_mode_cmd_param *cmd;
3703 	struct sk_buff *skb;
3704 	struct ath11k_wmi_base *wmi_ab = &ab->wmi_ab;
3705 	int len;
3706 	int ret;
3707 
3708 	len = sizeof(*cmd);
3709 
3710 	skb = ath11k_wmi_alloc_skb(wmi_ab, len);
3711 	if (!skb)
3712 		return -ENOMEM;
3713 
3714 	cmd = (struct wmi_pdev_set_hw_mode_cmd_param *)skb->data;
3715 
3716 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_PDEV_SET_HW_MODE_CMD) |
3717 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3718 
3719 	cmd->pdev_id = WMI_PDEV_ID_SOC;
3720 	cmd->hw_mode_index = mode;
3721 
3722 	ret = ath11k_wmi_cmd_send(&wmi_ab->wmi[0], skb, WMI_PDEV_SET_HW_MODE_CMDID);
3723 	if (ret) {
3724 		ath11k_warn(ab, "failed to send WMI_PDEV_SET_HW_MODE_CMDID\n");
3725 		dev_kfree_skb(skb);
3726 	}
3727 
3728 	return ret;
3729 }
3730 
3731 int ath11k_wmi_cmd_init(struct ath11k_base *ab)
3732 {
3733 	struct ath11k_wmi_base *wmi_sc = &ab->wmi_ab;
3734 	struct wmi_init_cmd_param init_param;
3735 	struct target_resource_config  config;
3736 
3737 	memset(&init_param, 0, sizeof(init_param));
3738 	memset(&config, 0, sizeof(config));
3739 
3740 	ab->hw_params.hw_ops->wmi_init_config(ab, &config);
3741 
3742 	memcpy(&wmi_sc->wlan_resource_config, &config, sizeof(config));
3743 
3744 	init_param.res_cfg = &wmi_sc->wlan_resource_config;
3745 	init_param.num_mem_chunks = wmi_sc->num_mem_chunks;
3746 	init_param.hw_mode_id = wmi_sc->preferred_hw_mode;
3747 	init_param.mem_chunks = wmi_sc->mem_chunks;
3748 
3749 	if (ab->hw_params.single_pdev_only)
3750 		init_param.hw_mode_id = WMI_HOST_HW_MODE_MAX;
3751 
3752 	init_param.num_band_to_mac = ab->num_radios;
3753 	ath11k_fill_band_to_mac_param(ab, init_param.band_to_mac);
3754 
3755 	return ath11k_init_cmd_send(&wmi_sc->wmi[0], &init_param);
3756 }
3757 
3758 int ath11k_wmi_vdev_spectral_conf(struct ath11k *ar,
3759 				  struct ath11k_wmi_vdev_spectral_conf_param *param)
3760 {
3761 	struct ath11k_wmi_vdev_spectral_conf_cmd *cmd;
3762 	struct sk_buff *skb;
3763 	int ret;
3764 
3765 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, sizeof(*cmd));
3766 	if (!skb)
3767 		return -ENOMEM;
3768 
3769 	cmd = (struct ath11k_wmi_vdev_spectral_conf_cmd *)skb->data;
3770 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3771 				     WMI_TAG_VDEV_SPECTRAL_CONFIGURE_CMD) |
3772 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3773 
3774 	memcpy(&cmd->param, param, sizeof(*param));
3775 
3776 	ret = ath11k_wmi_cmd_send(ar->wmi, skb,
3777 				  WMI_VDEV_SPECTRAL_SCAN_CONFIGURE_CMDID);
3778 	if (ret) {
3779 		ath11k_warn(ar->ab,
3780 			    "failed to send spectral scan config wmi cmd\n");
3781 		goto err;
3782 	}
3783 
3784 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3785 		   "WMI spectral scan config cmd vdev_id 0x%x\n",
3786 		   param->vdev_id);
3787 
3788 	return 0;
3789 err:
3790 	dev_kfree_skb(skb);
3791 	return ret;
3792 }
3793 
3794 int ath11k_wmi_vdev_spectral_enable(struct ath11k *ar, u32 vdev_id,
3795 				    u32 trigger, u32 enable)
3796 {
3797 	struct ath11k_wmi_vdev_spectral_enable_cmd *cmd;
3798 	struct sk_buff *skb;
3799 	int ret;
3800 
3801 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, sizeof(*cmd));
3802 	if (!skb)
3803 		return -ENOMEM;
3804 
3805 	cmd = (struct ath11k_wmi_vdev_spectral_enable_cmd *)skb->data;
3806 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
3807 				     WMI_TAG_VDEV_SPECTRAL_ENABLE_CMD) |
3808 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3809 
3810 	cmd->vdev_id = vdev_id;
3811 	cmd->trigger_cmd = trigger;
3812 	cmd->enable_cmd = enable;
3813 
3814 	ret = ath11k_wmi_cmd_send(ar->wmi, skb,
3815 				  WMI_VDEV_SPECTRAL_SCAN_ENABLE_CMDID);
3816 	if (ret) {
3817 		ath11k_warn(ar->ab,
3818 			    "failed to send spectral enable wmi cmd\n");
3819 		goto err;
3820 	}
3821 
3822 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3823 		   "WMI spectral enable cmd vdev id 0x%x\n",
3824 		   vdev_id);
3825 
3826 	return 0;
3827 err:
3828 	dev_kfree_skb(skb);
3829 	return ret;
3830 }
3831 
3832 int ath11k_wmi_pdev_dma_ring_cfg(struct ath11k *ar,
3833 				 struct ath11k_wmi_pdev_dma_ring_cfg_req_cmd *param)
3834 {
3835 	struct ath11k_wmi_pdev_dma_ring_cfg_req_cmd *cmd;
3836 	struct sk_buff *skb;
3837 	int ret;
3838 
3839 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, sizeof(*cmd));
3840 	if (!skb)
3841 		return -ENOMEM;
3842 
3843 	cmd = (struct ath11k_wmi_pdev_dma_ring_cfg_req_cmd *)skb->data;
3844 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_DMA_RING_CFG_REQ) |
3845 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
3846 
3847 	cmd->pdev_id		= param->pdev_id;
3848 	cmd->module_id		= param->module_id;
3849 	cmd->base_paddr_lo	= param->base_paddr_lo;
3850 	cmd->base_paddr_hi	= param->base_paddr_hi;
3851 	cmd->head_idx_paddr_lo	= param->head_idx_paddr_lo;
3852 	cmd->head_idx_paddr_hi	= param->head_idx_paddr_hi;
3853 	cmd->tail_idx_paddr_lo	= param->tail_idx_paddr_lo;
3854 	cmd->tail_idx_paddr_hi	= param->tail_idx_paddr_hi;
3855 	cmd->num_elems		= param->num_elems;
3856 	cmd->buf_size		= param->buf_size;
3857 	cmd->num_resp_per_event	= param->num_resp_per_event;
3858 	cmd->event_timeout_ms	= param->event_timeout_ms;
3859 
3860 	ret = ath11k_wmi_cmd_send(ar->wmi, skb,
3861 				  WMI_PDEV_DMA_RING_CFG_REQ_CMDID);
3862 	if (ret) {
3863 		ath11k_warn(ar->ab,
3864 			    "failed to send dma ring cfg req wmi cmd\n");
3865 		goto err;
3866 	}
3867 
3868 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
3869 		   "WMI DMA ring cfg req cmd pdev_id 0x%x\n",
3870 		   param->pdev_id);
3871 
3872 	return 0;
3873 err:
3874 	dev_kfree_skb(skb);
3875 	return ret;
3876 }
3877 
3878 static int ath11k_wmi_tlv_dma_buf_entry_parse(struct ath11k_base *soc,
3879 					      u16 tag, u16 len,
3880 					      const void *ptr, void *data)
3881 {
3882 	struct wmi_tlv_dma_buf_release_parse *parse = data;
3883 
3884 	if (tag != WMI_TAG_DMA_BUF_RELEASE_ENTRY)
3885 		return -EPROTO;
3886 
3887 	if (parse->num_buf_entry >= parse->fixed.num_buf_release_entry)
3888 		return -ENOBUFS;
3889 
3890 	parse->num_buf_entry++;
3891 	return 0;
3892 }
3893 
3894 static int ath11k_wmi_tlv_dma_buf_meta_parse(struct ath11k_base *soc,
3895 					     u16 tag, u16 len,
3896 					     const void *ptr, void *data)
3897 {
3898 	struct wmi_tlv_dma_buf_release_parse *parse = data;
3899 
3900 	if (tag != WMI_TAG_DMA_BUF_RELEASE_SPECTRAL_META_DATA)
3901 		return -EPROTO;
3902 
3903 	if (parse->num_meta >= parse->fixed.num_meta_data_entry)
3904 		return -ENOBUFS;
3905 
3906 	parse->num_meta++;
3907 	return 0;
3908 }
3909 
3910 static int ath11k_wmi_tlv_dma_buf_parse(struct ath11k_base *ab,
3911 					u16 tag, u16 len,
3912 					const void *ptr, void *data)
3913 {
3914 	struct wmi_tlv_dma_buf_release_parse *parse = data;
3915 	int ret;
3916 
3917 	switch (tag) {
3918 	case WMI_TAG_DMA_BUF_RELEASE:
3919 		memcpy(&parse->fixed, ptr,
3920 		       sizeof(struct ath11k_wmi_dma_buf_release_fixed_param));
3921 		parse->fixed.pdev_id = DP_HW2SW_MACID(parse->fixed.pdev_id);
3922 		break;
3923 	case WMI_TAG_ARRAY_STRUCT:
3924 		if (!parse->buf_entry_done) {
3925 			parse->num_buf_entry = 0;
3926 			parse->buf_entry = (struct wmi_dma_buf_release_entry *)ptr;
3927 
3928 			ret = ath11k_wmi_tlv_iter(ab, ptr, len,
3929 						  ath11k_wmi_tlv_dma_buf_entry_parse,
3930 						  parse);
3931 			if (ret) {
3932 				ath11k_warn(ab, "failed to parse dma buf entry tlv %d\n",
3933 					    ret);
3934 				return ret;
3935 			}
3936 
3937 			parse->buf_entry_done = true;
3938 		} else if (!parse->meta_data_done) {
3939 			parse->num_meta = 0;
3940 			parse->meta_data = (struct wmi_dma_buf_release_meta_data *)ptr;
3941 
3942 			ret = ath11k_wmi_tlv_iter(ab, ptr, len,
3943 						  ath11k_wmi_tlv_dma_buf_meta_parse,
3944 						  parse);
3945 			if (ret) {
3946 				ath11k_warn(ab, "failed to parse dma buf meta tlv %d\n",
3947 					    ret);
3948 				return ret;
3949 			}
3950 
3951 			parse->meta_data_done = true;
3952 		}
3953 		break;
3954 	default:
3955 		break;
3956 	}
3957 	return 0;
3958 }
3959 
3960 static void ath11k_wmi_pdev_dma_ring_buf_release_event(struct ath11k_base *ab,
3961 						       struct sk_buff *skb)
3962 {
3963 	struct wmi_tlv_dma_buf_release_parse parse = { };
3964 	struct ath11k_dbring_buf_release_event param;
3965 	int ret;
3966 
3967 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
3968 				  ath11k_wmi_tlv_dma_buf_parse,
3969 				  &parse);
3970 	if (ret) {
3971 		ath11k_warn(ab, "failed to parse dma buf release tlv %d\n", ret);
3972 		return;
3973 	}
3974 
3975 	param.fixed		= parse.fixed;
3976 	param.buf_entry		= parse.buf_entry;
3977 	param.num_buf_entry	= parse.num_buf_entry;
3978 	param.meta_data		= parse.meta_data;
3979 	param.num_meta		= parse.num_meta;
3980 
3981 	ret = ath11k_dbring_buffer_release_event(ab, &param);
3982 	if (ret) {
3983 		ath11k_warn(ab, "failed to handle dma buf release event %d\n", ret);
3984 		return;
3985 	}
3986 }
3987 
3988 static int ath11k_wmi_tlv_hw_mode_caps_parse(struct ath11k_base *soc,
3989 					     u16 tag, u16 len,
3990 					     const void *ptr, void *data)
3991 {
3992 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
3993 	struct wmi_hw_mode_capabilities *hw_mode_cap;
3994 	u32 phy_map = 0;
3995 
3996 	if (tag != WMI_TAG_HW_MODE_CAPABILITIES)
3997 		return -EPROTO;
3998 
3999 	if (svc_rdy_ext->n_hw_mode_caps >= svc_rdy_ext->param.num_hw_modes)
4000 		return -ENOBUFS;
4001 
4002 	hw_mode_cap = container_of(ptr, struct wmi_hw_mode_capabilities,
4003 				   hw_mode_id);
4004 	svc_rdy_ext->n_hw_mode_caps++;
4005 
4006 	phy_map = hw_mode_cap->phy_id_map;
4007 	while (phy_map) {
4008 		svc_rdy_ext->tot_phy_id++;
4009 		phy_map = phy_map >> 1;
4010 	}
4011 
4012 	return 0;
4013 }
4014 
4015 static int ath11k_wmi_tlv_hw_mode_caps(struct ath11k_base *soc,
4016 				       u16 len, const void *ptr, void *data)
4017 {
4018 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
4019 	struct wmi_hw_mode_capabilities *hw_mode_caps;
4020 	enum wmi_host_hw_mode_config_type mode, pref;
4021 	u32 i;
4022 	int ret;
4023 
4024 	svc_rdy_ext->n_hw_mode_caps = 0;
4025 	svc_rdy_ext->hw_mode_caps = (struct wmi_hw_mode_capabilities *)ptr;
4026 
4027 	ret = ath11k_wmi_tlv_iter(soc, ptr, len,
4028 				  ath11k_wmi_tlv_hw_mode_caps_parse,
4029 				  svc_rdy_ext);
4030 	if (ret) {
4031 		ath11k_warn(soc, "failed to parse tlv %d\n", ret);
4032 		return ret;
4033 	}
4034 
4035 	i = 0;
4036 	while (i < svc_rdy_ext->n_hw_mode_caps) {
4037 		hw_mode_caps = &svc_rdy_ext->hw_mode_caps[i];
4038 		mode = hw_mode_caps->hw_mode_id;
4039 		pref = soc->wmi_ab.preferred_hw_mode;
4040 
4041 		if (ath11k_hw_mode_pri_map[mode] < ath11k_hw_mode_pri_map[pref]) {
4042 			svc_rdy_ext->pref_hw_mode_caps = *hw_mode_caps;
4043 			soc->wmi_ab.preferred_hw_mode = mode;
4044 		}
4045 		i++;
4046 	}
4047 
4048 	ath11k_dbg(soc, ATH11K_DBG_WMI, "preferred_hw_mode:%d\n",
4049 		   soc->wmi_ab.preferred_hw_mode);
4050 	if (soc->wmi_ab.preferred_hw_mode == WMI_HOST_HW_MODE_MAX)
4051 		return -EINVAL;
4052 
4053 	return 0;
4054 }
4055 
4056 static int ath11k_wmi_tlv_mac_phy_caps_parse(struct ath11k_base *soc,
4057 					     u16 tag, u16 len,
4058 					     const void *ptr, void *data)
4059 {
4060 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
4061 
4062 	if (tag != WMI_TAG_MAC_PHY_CAPABILITIES)
4063 		return -EPROTO;
4064 
4065 	if (svc_rdy_ext->n_mac_phy_caps >= svc_rdy_ext->tot_phy_id)
4066 		return -ENOBUFS;
4067 
4068 	len = min_t(u16, len, sizeof(struct wmi_mac_phy_capabilities));
4069 	if (!svc_rdy_ext->n_mac_phy_caps) {
4070 		svc_rdy_ext->mac_phy_caps = kcalloc(svc_rdy_ext->tot_phy_id,
4071 						    len, GFP_ATOMIC);
4072 		if (!svc_rdy_ext->mac_phy_caps)
4073 			return -ENOMEM;
4074 	}
4075 
4076 	memcpy(svc_rdy_ext->mac_phy_caps + svc_rdy_ext->n_mac_phy_caps, ptr, len);
4077 	svc_rdy_ext->n_mac_phy_caps++;
4078 	return 0;
4079 }
4080 
4081 static int ath11k_wmi_tlv_ext_hal_reg_caps_parse(struct ath11k_base *soc,
4082 						 u16 tag, u16 len,
4083 						 const void *ptr, void *data)
4084 {
4085 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
4086 
4087 	if (tag != WMI_TAG_HAL_REG_CAPABILITIES_EXT)
4088 		return -EPROTO;
4089 
4090 	if (svc_rdy_ext->n_ext_hal_reg_caps >= svc_rdy_ext->param.num_phy)
4091 		return -ENOBUFS;
4092 
4093 	svc_rdy_ext->n_ext_hal_reg_caps++;
4094 	return 0;
4095 }
4096 
4097 static int ath11k_wmi_tlv_ext_hal_reg_caps(struct ath11k_base *soc,
4098 					   u16 len, const void *ptr, void *data)
4099 {
4100 	struct ath11k_pdev_wmi *wmi_handle = &soc->wmi_ab.wmi[0];
4101 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
4102 	struct ath11k_hal_reg_capabilities_ext reg_cap;
4103 	int ret;
4104 	u32 i;
4105 
4106 	svc_rdy_ext->n_ext_hal_reg_caps = 0;
4107 	svc_rdy_ext->ext_hal_reg_caps = (struct wmi_hal_reg_capabilities_ext *)ptr;
4108 	ret = ath11k_wmi_tlv_iter(soc, ptr, len,
4109 				  ath11k_wmi_tlv_ext_hal_reg_caps_parse,
4110 				  svc_rdy_ext);
4111 	if (ret) {
4112 		ath11k_warn(soc, "failed to parse tlv %d\n", ret);
4113 		return ret;
4114 	}
4115 
4116 	for (i = 0; i < svc_rdy_ext->param.num_phy; i++) {
4117 		ret = ath11k_pull_reg_cap_svc_rdy_ext(wmi_handle,
4118 						      svc_rdy_ext->soc_hal_reg_caps,
4119 						      svc_rdy_ext->ext_hal_reg_caps, i,
4120 						      &reg_cap);
4121 		if (ret) {
4122 			ath11k_warn(soc, "failed to extract reg cap %d\n", i);
4123 			return ret;
4124 		}
4125 
4126 		memcpy(&soc->hal_reg_cap[reg_cap.phy_id],
4127 		       &reg_cap, sizeof(reg_cap));
4128 	}
4129 	return 0;
4130 }
4131 
4132 static int ath11k_wmi_tlv_ext_soc_hal_reg_caps_parse(struct ath11k_base *soc,
4133 						     u16 len, const void *ptr,
4134 						     void *data)
4135 {
4136 	struct ath11k_pdev_wmi *wmi_handle = &soc->wmi_ab.wmi[0];
4137 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
4138 	u8 hw_mode_id = svc_rdy_ext->pref_hw_mode_caps.hw_mode_id;
4139 	u32 phy_id_map;
4140 	int pdev_index = 0;
4141 	int ret;
4142 
4143 	svc_rdy_ext->soc_hal_reg_caps = (struct wmi_soc_hal_reg_capabilities *)ptr;
4144 	svc_rdy_ext->param.num_phy = svc_rdy_ext->soc_hal_reg_caps->num_phy;
4145 
4146 	soc->num_radios = 0;
4147 	phy_id_map = svc_rdy_ext->pref_hw_mode_caps.phy_id_map;
4148 
4149 	while (phy_id_map && soc->num_radios < MAX_RADIOS) {
4150 		ret = ath11k_pull_mac_phy_cap_svc_ready_ext(wmi_handle,
4151 							    svc_rdy_ext->hw_caps,
4152 							    svc_rdy_ext->hw_mode_caps,
4153 							    svc_rdy_ext->soc_hal_reg_caps,
4154 							    svc_rdy_ext->mac_phy_caps,
4155 							    hw_mode_id, soc->num_radios,
4156 							    &soc->pdevs[pdev_index]);
4157 		if (ret) {
4158 			ath11k_warn(soc, "failed to extract mac caps, idx :%d\n",
4159 				    soc->num_radios);
4160 			return ret;
4161 		}
4162 
4163 		soc->num_radios++;
4164 
4165 		/* For QCA6390, save mac_phy capability in the same pdev */
4166 		if (soc->hw_params.single_pdev_only)
4167 			pdev_index = 0;
4168 		else
4169 			pdev_index = soc->num_radios;
4170 
4171 		/* TODO: mac_phy_cap prints */
4172 		phy_id_map >>= 1;
4173 	}
4174 
4175 	/* For QCA6390, set num_radios to 1 because host manages
4176 	 * both 2G and 5G radio in one pdev.
4177 	 * Set pdev_id = 0 and 0 means soc level.
4178 	 */
4179 	if (soc->hw_params.single_pdev_only) {
4180 		soc->num_radios = 1;
4181 		soc->pdevs[0].pdev_id = 0;
4182 	}
4183 
4184 	return 0;
4185 }
4186 
4187 static int ath11k_wmi_tlv_dma_ring_caps_parse(struct ath11k_base *soc,
4188 					      u16 tag, u16 len,
4189 					      const void *ptr, void *data)
4190 {
4191 	struct wmi_tlv_dma_ring_caps_parse *parse = data;
4192 
4193 	if (tag != WMI_TAG_DMA_RING_CAPABILITIES)
4194 		return -EPROTO;
4195 
4196 	parse->n_dma_ring_caps++;
4197 	return 0;
4198 }
4199 
4200 static int ath11k_wmi_alloc_dbring_caps(struct ath11k_base *ab,
4201 					u32 num_cap)
4202 {
4203 	size_t sz;
4204 	void *ptr;
4205 
4206 	sz = num_cap * sizeof(struct ath11k_dbring_cap);
4207 	ptr = kzalloc(sz, GFP_ATOMIC);
4208 	if (!ptr)
4209 		return -ENOMEM;
4210 
4211 	ab->db_caps = ptr;
4212 	ab->num_db_cap = num_cap;
4213 
4214 	return 0;
4215 }
4216 
4217 static void ath11k_wmi_free_dbring_caps(struct ath11k_base *ab)
4218 {
4219 	kfree(ab->db_caps);
4220 	ab->db_caps = NULL;
4221 }
4222 
4223 static int ath11k_wmi_tlv_dma_ring_caps(struct ath11k_base *ab,
4224 					u16 len, const void *ptr, void *data)
4225 {
4226 	struct wmi_tlv_dma_ring_caps_parse *dma_caps_parse = data;
4227 	struct wmi_dma_ring_capabilities *dma_caps;
4228 	struct ath11k_dbring_cap *dir_buff_caps;
4229 	int ret;
4230 	u32 i;
4231 
4232 	dma_caps_parse->n_dma_ring_caps = 0;
4233 	dma_caps = (struct wmi_dma_ring_capabilities *)ptr;
4234 	ret = ath11k_wmi_tlv_iter(ab, ptr, len,
4235 				  ath11k_wmi_tlv_dma_ring_caps_parse,
4236 				  dma_caps_parse);
4237 	if (ret) {
4238 		ath11k_warn(ab, "failed to parse dma ring caps tlv %d\n", ret);
4239 		return ret;
4240 	}
4241 
4242 	if (!dma_caps_parse->n_dma_ring_caps)
4243 		return 0;
4244 
4245 	if (ab->num_db_cap) {
4246 		ath11k_warn(ab, "Already processed, so ignoring dma ring caps\n");
4247 		return 0;
4248 	}
4249 
4250 	ret = ath11k_wmi_alloc_dbring_caps(ab, dma_caps_parse->n_dma_ring_caps);
4251 	if (ret)
4252 		return ret;
4253 
4254 	dir_buff_caps = ab->db_caps;
4255 	for (i = 0; i < dma_caps_parse->n_dma_ring_caps; i++) {
4256 		if (dma_caps[i].module_id >= WMI_DIRECT_BUF_MAX) {
4257 			ath11k_warn(ab, "Invalid module id %d\n", dma_caps[i].module_id);
4258 			ret = -EINVAL;
4259 			goto free_dir_buff;
4260 		}
4261 
4262 		dir_buff_caps[i].id = dma_caps[i].module_id;
4263 		dir_buff_caps[i].pdev_id = DP_HW2SW_MACID(dma_caps[i].pdev_id);
4264 		dir_buff_caps[i].min_elem = dma_caps[i].min_elem;
4265 		dir_buff_caps[i].min_buf_sz = dma_caps[i].min_buf_sz;
4266 		dir_buff_caps[i].min_buf_align = dma_caps[i].min_buf_align;
4267 	}
4268 
4269 	return 0;
4270 
4271 free_dir_buff:
4272 	ath11k_wmi_free_dbring_caps(ab);
4273 	return ret;
4274 }
4275 
4276 static int ath11k_wmi_tlv_svc_rdy_ext_parse(struct ath11k_base *ab,
4277 					    u16 tag, u16 len,
4278 					    const void *ptr, void *data)
4279 {
4280 	struct ath11k_pdev_wmi *wmi_handle = &ab->wmi_ab.wmi[0];
4281 	struct wmi_tlv_svc_rdy_ext_parse *svc_rdy_ext = data;
4282 	int ret;
4283 
4284 	switch (tag) {
4285 	case WMI_TAG_SERVICE_READY_EXT_EVENT:
4286 		ret = ath11k_pull_svc_ready_ext(wmi_handle, ptr,
4287 						&svc_rdy_ext->param);
4288 		if (ret) {
4289 			ath11k_warn(ab, "unable to extract ext params\n");
4290 			return ret;
4291 		}
4292 		break;
4293 
4294 	case WMI_TAG_SOC_MAC_PHY_HW_MODE_CAPS:
4295 		svc_rdy_ext->hw_caps = (struct wmi_soc_mac_phy_hw_mode_caps *)ptr;
4296 		svc_rdy_ext->param.num_hw_modes = svc_rdy_ext->hw_caps->num_hw_modes;
4297 		break;
4298 
4299 	case WMI_TAG_SOC_HAL_REG_CAPABILITIES:
4300 		ret = ath11k_wmi_tlv_ext_soc_hal_reg_caps_parse(ab, len, ptr,
4301 								svc_rdy_ext);
4302 		if (ret)
4303 			return ret;
4304 		break;
4305 
4306 	case WMI_TAG_ARRAY_STRUCT:
4307 		if (!svc_rdy_ext->hw_mode_done) {
4308 			ret = ath11k_wmi_tlv_hw_mode_caps(ab, len, ptr,
4309 							  svc_rdy_ext);
4310 			if (ret)
4311 				return ret;
4312 
4313 			svc_rdy_ext->hw_mode_done = true;
4314 		} else if (!svc_rdy_ext->mac_phy_done) {
4315 			svc_rdy_ext->n_mac_phy_caps = 0;
4316 			ret = ath11k_wmi_tlv_iter(ab, ptr, len,
4317 						  ath11k_wmi_tlv_mac_phy_caps_parse,
4318 						  svc_rdy_ext);
4319 			if (ret) {
4320 				ath11k_warn(ab, "failed to parse tlv %d\n", ret);
4321 				return ret;
4322 			}
4323 
4324 			svc_rdy_ext->mac_phy_done = true;
4325 		} else if (!svc_rdy_ext->ext_hal_reg_done) {
4326 			ret = ath11k_wmi_tlv_ext_hal_reg_caps(ab, len, ptr,
4327 							      svc_rdy_ext);
4328 			if (ret)
4329 				return ret;
4330 
4331 			svc_rdy_ext->ext_hal_reg_done = true;
4332 		} else if (!svc_rdy_ext->mac_phy_chainmask_combo_done) {
4333 			svc_rdy_ext->mac_phy_chainmask_combo_done = true;
4334 		} else if (!svc_rdy_ext->mac_phy_chainmask_cap_done) {
4335 			svc_rdy_ext->mac_phy_chainmask_cap_done = true;
4336 		} else if (!svc_rdy_ext->oem_dma_ring_cap_done) {
4337 			svc_rdy_ext->oem_dma_ring_cap_done = true;
4338 		} else if (!svc_rdy_ext->dma_ring_cap_done) {
4339 			ret = ath11k_wmi_tlv_dma_ring_caps(ab, len, ptr,
4340 							   &svc_rdy_ext->dma_caps_parse);
4341 			if (ret)
4342 				return ret;
4343 
4344 			svc_rdy_ext->dma_ring_cap_done = true;
4345 		}
4346 		break;
4347 
4348 	default:
4349 		break;
4350 	}
4351 	return 0;
4352 }
4353 
4354 static int ath11k_service_ready_ext_event(struct ath11k_base *ab,
4355 					  struct sk_buff *skb)
4356 {
4357 	struct wmi_tlv_svc_rdy_ext_parse svc_rdy_ext = { };
4358 	int ret;
4359 
4360 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
4361 				  ath11k_wmi_tlv_svc_rdy_ext_parse,
4362 				  &svc_rdy_ext);
4363 	if (ret) {
4364 		ath11k_warn(ab, "failed to parse tlv %d\n", ret);
4365 		goto err;
4366 	}
4367 
4368 	if (!test_bit(WMI_TLV_SERVICE_EXT2_MSG, ab->wmi_ab.svc_map))
4369 		complete(&ab->wmi_ab.service_ready);
4370 
4371 	kfree(svc_rdy_ext.mac_phy_caps);
4372 	return 0;
4373 
4374 err:
4375 	ath11k_wmi_free_dbring_caps(ab);
4376 	return ret;
4377 }
4378 
4379 static int ath11k_wmi_tlv_svc_rdy_ext2_parse(struct ath11k_base *ab,
4380 					     u16 tag, u16 len,
4381 					     const void *ptr, void *data)
4382 {
4383 	struct wmi_tlv_svc_rdy_ext2_parse *parse = data;
4384 	int ret;
4385 
4386 	switch (tag) {
4387 	case WMI_TAG_ARRAY_STRUCT:
4388 		if (!parse->dma_ring_cap_done) {
4389 			ret = ath11k_wmi_tlv_dma_ring_caps(ab, len, ptr,
4390 							   &parse->dma_caps_parse);
4391 			if (ret)
4392 				return ret;
4393 
4394 			parse->dma_ring_cap_done = true;
4395 		}
4396 		break;
4397 	default:
4398 		break;
4399 	}
4400 
4401 	return 0;
4402 }
4403 
4404 static int ath11k_service_ready_ext2_event(struct ath11k_base *ab,
4405 					   struct sk_buff *skb)
4406 {
4407 	struct wmi_tlv_svc_rdy_ext2_parse svc_rdy_ext2 = { };
4408 	int ret;
4409 
4410 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
4411 				  ath11k_wmi_tlv_svc_rdy_ext2_parse,
4412 				  &svc_rdy_ext2);
4413 	if (ret) {
4414 		ath11k_warn(ab, "failed to parse ext2 event tlv %d\n", ret);
4415 		goto err;
4416 	}
4417 
4418 	complete(&ab->wmi_ab.service_ready);
4419 
4420 	return 0;
4421 
4422 err:
4423 	ath11k_wmi_free_dbring_caps(ab);
4424 	return ret;
4425 }
4426 
4427 static int ath11k_pull_vdev_start_resp_tlv(struct ath11k_base *ab, struct sk_buff *skb,
4428 					   struct wmi_vdev_start_resp_event *vdev_rsp)
4429 {
4430 	const void **tb;
4431 	const struct wmi_vdev_start_resp_event *ev;
4432 	int ret;
4433 
4434 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4435 	if (IS_ERR(tb)) {
4436 		ret = PTR_ERR(tb);
4437 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4438 		return ret;
4439 	}
4440 
4441 	ev = tb[WMI_TAG_VDEV_START_RESPONSE_EVENT];
4442 	if (!ev) {
4443 		ath11k_warn(ab, "failed to fetch vdev start resp ev");
4444 		kfree(tb);
4445 		return -EPROTO;
4446 	}
4447 
4448 	memset(vdev_rsp, 0, sizeof(*vdev_rsp));
4449 
4450 	vdev_rsp->vdev_id = ev->vdev_id;
4451 	vdev_rsp->requestor_id = ev->requestor_id;
4452 	vdev_rsp->resp_type = ev->resp_type;
4453 	vdev_rsp->status = ev->status;
4454 	vdev_rsp->chain_mask = ev->chain_mask;
4455 	vdev_rsp->smps_mode = ev->smps_mode;
4456 	vdev_rsp->mac_id = ev->mac_id;
4457 	vdev_rsp->cfgd_tx_streams = ev->cfgd_tx_streams;
4458 	vdev_rsp->cfgd_rx_streams = ev->cfgd_rx_streams;
4459 
4460 	kfree(tb);
4461 	return 0;
4462 }
4463 
4464 static struct cur_reg_rule
4465 *create_reg_rules_from_wmi(u32 num_reg_rules,
4466 			   struct wmi_regulatory_rule_struct *wmi_reg_rule)
4467 {
4468 	struct cur_reg_rule *reg_rule_ptr;
4469 	u32 count;
4470 
4471 	reg_rule_ptr = kcalloc(num_reg_rules, sizeof(*reg_rule_ptr),
4472 			       GFP_ATOMIC);
4473 
4474 	if (!reg_rule_ptr)
4475 		return NULL;
4476 
4477 	for (count = 0; count < num_reg_rules; count++) {
4478 		reg_rule_ptr[count].start_freq =
4479 			FIELD_GET(REG_RULE_START_FREQ,
4480 				  wmi_reg_rule[count].freq_info);
4481 		reg_rule_ptr[count].end_freq =
4482 			FIELD_GET(REG_RULE_END_FREQ,
4483 				  wmi_reg_rule[count].freq_info);
4484 		reg_rule_ptr[count].max_bw =
4485 			FIELD_GET(REG_RULE_MAX_BW,
4486 				  wmi_reg_rule[count].bw_pwr_info);
4487 		reg_rule_ptr[count].reg_power =
4488 			FIELD_GET(REG_RULE_REG_PWR,
4489 				  wmi_reg_rule[count].bw_pwr_info);
4490 		reg_rule_ptr[count].ant_gain =
4491 			FIELD_GET(REG_RULE_ANT_GAIN,
4492 				  wmi_reg_rule[count].bw_pwr_info);
4493 		reg_rule_ptr[count].flags =
4494 			FIELD_GET(REG_RULE_FLAGS,
4495 				  wmi_reg_rule[count].flag_info);
4496 	}
4497 
4498 	return reg_rule_ptr;
4499 }
4500 
4501 static int ath11k_pull_reg_chan_list_update_ev(struct ath11k_base *ab,
4502 					       struct sk_buff *skb,
4503 					       struct cur_regulatory_info *reg_info)
4504 {
4505 	const void **tb;
4506 	const struct wmi_reg_chan_list_cc_event *chan_list_event_hdr;
4507 	struct wmi_regulatory_rule_struct *wmi_reg_rule;
4508 	u32 num_2g_reg_rules, num_5g_reg_rules;
4509 	int ret;
4510 
4511 	ath11k_dbg(ab, ATH11K_DBG_WMI, "processing regulatory channel list\n");
4512 
4513 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4514 	if (IS_ERR(tb)) {
4515 		ret = PTR_ERR(tb);
4516 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4517 		return ret;
4518 	}
4519 
4520 	chan_list_event_hdr = tb[WMI_TAG_REG_CHAN_LIST_CC_EVENT];
4521 	if (!chan_list_event_hdr) {
4522 		ath11k_warn(ab, "failed to fetch reg chan list update ev\n");
4523 		kfree(tb);
4524 		return -EPROTO;
4525 	}
4526 
4527 	reg_info->num_2g_reg_rules = chan_list_event_hdr->num_2g_reg_rules;
4528 	reg_info->num_5g_reg_rules = chan_list_event_hdr->num_5g_reg_rules;
4529 
4530 	if (!(reg_info->num_2g_reg_rules + reg_info->num_5g_reg_rules)) {
4531 		ath11k_warn(ab, "No regulatory rules available in the event info\n");
4532 		kfree(tb);
4533 		return -EINVAL;
4534 	}
4535 
4536 	memcpy(reg_info->alpha2, &chan_list_event_hdr->alpha2,
4537 	       REG_ALPHA2_LEN);
4538 	reg_info->dfs_region = chan_list_event_hdr->dfs_region;
4539 	reg_info->phybitmap = chan_list_event_hdr->phybitmap;
4540 	reg_info->num_phy = chan_list_event_hdr->num_phy;
4541 	reg_info->phy_id = chan_list_event_hdr->phy_id;
4542 	reg_info->ctry_code = chan_list_event_hdr->country_id;
4543 	reg_info->reg_dmn_pair = chan_list_event_hdr->domain_code;
4544 	if (chan_list_event_hdr->status_code == WMI_REG_SET_CC_STATUS_PASS)
4545 		reg_info->status_code = REG_SET_CC_STATUS_PASS;
4546 	else if (chan_list_event_hdr->status_code == WMI_REG_CURRENT_ALPHA2_NOT_FOUND)
4547 		reg_info->status_code = REG_CURRENT_ALPHA2_NOT_FOUND;
4548 	else if (chan_list_event_hdr->status_code == WMI_REG_INIT_ALPHA2_NOT_FOUND)
4549 		reg_info->status_code = REG_INIT_ALPHA2_NOT_FOUND;
4550 	else if (chan_list_event_hdr->status_code == WMI_REG_SET_CC_CHANGE_NOT_ALLOWED)
4551 		reg_info->status_code = REG_SET_CC_CHANGE_NOT_ALLOWED;
4552 	else if (chan_list_event_hdr->status_code == WMI_REG_SET_CC_STATUS_NO_MEMORY)
4553 		reg_info->status_code = REG_SET_CC_STATUS_NO_MEMORY;
4554 	else if (chan_list_event_hdr->status_code == WMI_REG_SET_CC_STATUS_FAIL)
4555 		reg_info->status_code = REG_SET_CC_STATUS_FAIL;
4556 
4557 	reg_info->min_bw_2g = chan_list_event_hdr->min_bw_2g;
4558 	reg_info->max_bw_2g = chan_list_event_hdr->max_bw_2g;
4559 	reg_info->min_bw_5g = chan_list_event_hdr->min_bw_5g;
4560 	reg_info->max_bw_5g = chan_list_event_hdr->max_bw_5g;
4561 
4562 	num_2g_reg_rules = reg_info->num_2g_reg_rules;
4563 	num_5g_reg_rules = reg_info->num_5g_reg_rules;
4564 
4565 	ath11k_dbg(ab, ATH11K_DBG_WMI,
4566 		   "%s:cc %s dsf %d BW: min_2g %d max_2g %d min_5g %d max_5g %d",
4567 		   __func__, reg_info->alpha2, reg_info->dfs_region,
4568 		   reg_info->min_bw_2g, reg_info->max_bw_2g,
4569 		   reg_info->min_bw_5g, reg_info->max_bw_5g);
4570 
4571 	ath11k_dbg(ab, ATH11K_DBG_WMI,
4572 		   "%s: num_2g_reg_rules %d num_5g_reg_rules %d", __func__,
4573 		   num_2g_reg_rules, num_5g_reg_rules);
4574 
4575 	wmi_reg_rule =
4576 		(struct wmi_regulatory_rule_struct *)((u8 *)chan_list_event_hdr
4577 						+ sizeof(*chan_list_event_hdr)
4578 						+ sizeof(struct wmi_tlv));
4579 
4580 	if (num_2g_reg_rules) {
4581 		reg_info->reg_rules_2g_ptr = create_reg_rules_from_wmi(num_2g_reg_rules,
4582 								       wmi_reg_rule);
4583 		if (!reg_info->reg_rules_2g_ptr) {
4584 			kfree(tb);
4585 			ath11k_warn(ab, "Unable to Allocate memory for 2g rules\n");
4586 			return -ENOMEM;
4587 		}
4588 	}
4589 
4590 	if (num_5g_reg_rules) {
4591 		wmi_reg_rule += num_2g_reg_rules;
4592 		reg_info->reg_rules_5g_ptr = create_reg_rules_from_wmi(num_5g_reg_rules,
4593 								       wmi_reg_rule);
4594 		if (!reg_info->reg_rules_5g_ptr) {
4595 			kfree(tb);
4596 			ath11k_warn(ab, "Unable to Allocate memory for 5g rules\n");
4597 			return -ENOMEM;
4598 		}
4599 	}
4600 
4601 	ath11k_dbg(ab, ATH11K_DBG_WMI, "processed regulatory channel list\n");
4602 
4603 	kfree(tb);
4604 	return 0;
4605 }
4606 
4607 static int ath11k_pull_peer_del_resp_ev(struct ath11k_base *ab, struct sk_buff *skb,
4608 					struct wmi_peer_delete_resp_event *peer_del_resp)
4609 {
4610 	const void **tb;
4611 	const struct wmi_peer_delete_resp_event *ev;
4612 	int ret;
4613 
4614 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4615 	if (IS_ERR(tb)) {
4616 		ret = PTR_ERR(tb);
4617 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4618 		return ret;
4619 	}
4620 
4621 	ev = tb[WMI_TAG_PEER_DELETE_RESP_EVENT];
4622 	if (!ev) {
4623 		ath11k_warn(ab, "failed to fetch peer delete resp ev");
4624 		kfree(tb);
4625 		return -EPROTO;
4626 	}
4627 
4628 	memset(peer_del_resp, 0, sizeof(*peer_del_resp));
4629 
4630 	peer_del_resp->vdev_id = ev->vdev_id;
4631 	ether_addr_copy(peer_del_resp->peer_macaddr.addr,
4632 			ev->peer_macaddr.addr);
4633 
4634 	kfree(tb);
4635 	return 0;
4636 }
4637 
4638 static int ath11k_pull_vdev_del_resp_ev(struct ath11k_base *ab,
4639 					struct sk_buff *skb,
4640 					u32 *vdev_id)
4641 {
4642 	const void **tb;
4643 	const struct wmi_vdev_delete_resp_event *ev;
4644 	int ret;
4645 
4646 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4647 	if (IS_ERR(tb)) {
4648 		ret = PTR_ERR(tb);
4649 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4650 		return ret;
4651 	}
4652 
4653 	ev = tb[WMI_TAG_VDEV_DELETE_RESP_EVENT];
4654 	if (!ev) {
4655 		ath11k_warn(ab, "failed to fetch vdev delete resp ev");
4656 		kfree(tb);
4657 		return -EPROTO;
4658 	}
4659 
4660 	*vdev_id = ev->vdev_id;
4661 
4662 	kfree(tb);
4663 	return 0;
4664 }
4665 
4666 static int ath11k_pull_bcn_tx_status_ev(struct ath11k_base *ab, void *evt_buf,
4667 					u32 len, u32 *vdev_id,
4668 					u32 *tx_status)
4669 {
4670 	const void **tb;
4671 	const struct wmi_bcn_tx_status_event *ev;
4672 	int ret;
4673 
4674 	tb = ath11k_wmi_tlv_parse_alloc(ab, evt_buf, len, GFP_ATOMIC);
4675 	if (IS_ERR(tb)) {
4676 		ret = PTR_ERR(tb);
4677 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4678 		return ret;
4679 	}
4680 
4681 	ev = tb[WMI_TAG_OFFLOAD_BCN_TX_STATUS_EVENT];
4682 	if (!ev) {
4683 		ath11k_warn(ab, "failed to fetch bcn tx status ev");
4684 		kfree(tb);
4685 		return -EPROTO;
4686 	}
4687 
4688 	*vdev_id   = ev->vdev_id;
4689 	*tx_status = ev->tx_status;
4690 
4691 	kfree(tb);
4692 	return 0;
4693 }
4694 
4695 static int ath11k_pull_vdev_stopped_param_tlv(struct ath11k_base *ab, struct sk_buff *skb,
4696 					      u32 *vdev_id)
4697 {
4698 	const void **tb;
4699 	const struct wmi_vdev_stopped_event *ev;
4700 	int ret;
4701 
4702 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4703 	if (IS_ERR(tb)) {
4704 		ret = PTR_ERR(tb);
4705 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4706 		return ret;
4707 	}
4708 
4709 	ev = tb[WMI_TAG_VDEV_STOPPED_EVENT];
4710 	if (!ev) {
4711 		ath11k_warn(ab, "failed to fetch vdev stop ev");
4712 		kfree(tb);
4713 		return -EPROTO;
4714 	}
4715 
4716 	*vdev_id =  ev->vdev_id;
4717 
4718 	kfree(tb);
4719 	return 0;
4720 }
4721 
4722 static int ath11k_pull_mgmt_rx_params_tlv(struct ath11k_base *ab,
4723 					  struct sk_buff *skb,
4724 					  struct mgmt_rx_event_params *hdr)
4725 {
4726 	const void **tb;
4727 	const struct wmi_mgmt_rx_hdr *ev;
4728 	const u8 *frame;
4729 	int ret;
4730 
4731 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4732 	if (IS_ERR(tb)) {
4733 		ret = PTR_ERR(tb);
4734 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4735 		return ret;
4736 	}
4737 
4738 	ev = tb[WMI_TAG_MGMT_RX_HDR];
4739 	frame = tb[WMI_TAG_ARRAY_BYTE];
4740 
4741 	if (!ev || !frame) {
4742 		ath11k_warn(ab, "failed to fetch mgmt rx hdr");
4743 		kfree(tb);
4744 		return -EPROTO;
4745 	}
4746 
4747 	hdr->pdev_id =  ev->pdev_id;
4748 	hdr->chan_freq = ev->chan_freq;
4749 	hdr->channel =  ev->channel;
4750 	hdr->snr =  ev->snr;
4751 	hdr->rate =  ev->rate;
4752 	hdr->phy_mode =  ev->phy_mode;
4753 	hdr->buf_len =  ev->buf_len;
4754 	hdr->status =  ev->status;
4755 	hdr->flags =  ev->flags;
4756 	hdr->rssi =  ev->rssi;
4757 	hdr->tsf_delta =  ev->tsf_delta;
4758 	memcpy(hdr->rssi_ctl, ev->rssi_ctl, sizeof(hdr->rssi_ctl));
4759 
4760 	if (skb->len < (frame - skb->data) + hdr->buf_len) {
4761 		ath11k_warn(ab, "invalid length in mgmt rx hdr ev");
4762 		kfree(tb);
4763 		return -EPROTO;
4764 	}
4765 
4766 	/* shift the sk_buff to point to `frame` */
4767 	skb_trim(skb, 0);
4768 	skb_put(skb, frame - skb->data);
4769 	skb_pull(skb, frame - skb->data);
4770 	skb_put(skb, hdr->buf_len);
4771 
4772 	ath11k_ce_byte_swap(skb->data, hdr->buf_len);
4773 
4774 	kfree(tb);
4775 	return 0;
4776 }
4777 
4778 static int wmi_process_mgmt_tx_comp(struct ath11k *ar, u32 desc_id,
4779 				    u32 status)
4780 {
4781 	struct sk_buff *msdu;
4782 	struct ieee80211_tx_info *info;
4783 	struct ath11k_skb_cb *skb_cb;
4784 
4785 	spin_lock_bh(&ar->txmgmt_idr_lock);
4786 	msdu = idr_find(&ar->txmgmt_idr, desc_id);
4787 
4788 	if (!msdu) {
4789 		ath11k_warn(ar->ab, "received mgmt tx compl for invalid msdu_id: %d\n",
4790 			    desc_id);
4791 		spin_unlock_bh(&ar->txmgmt_idr_lock);
4792 		return -ENOENT;
4793 	}
4794 
4795 	idr_remove(&ar->txmgmt_idr, desc_id);
4796 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4797 
4798 	skb_cb = ATH11K_SKB_CB(msdu);
4799 	dma_unmap_single(ar->ab->dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
4800 
4801 	info = IEEE80211_SKB_CB(msdu);
4802 	if ((!(info->flags & IEEE80211_TX_CTL_NO_ACK)) && !status)
4803 		info->flags |= IEEE80211_TX_STAT_ACK;
4804 
4805 	ieee80211_tx_status_irqsafe(ar->hw, msdu);
4806 
4807 	/* WARN when we received this event without doing any mgmt tx */
4808 	if (atomic_dec_if_positive(&ar->num_pending_mgmt_tx) < 0)
4809 		WARN_ON_ONCE(1);
4810 
4811 	return 0;
4812 }
4813 
4814 static int ath11k_pull_mgmt_tx_compl_param_tlv(struct ath11k_base *ab,
4815 					       struct sk_buff *skb,
4816 					       struct wmi_mgmt_tx_compl_event *param)
4817 {
4818 	const void **tb;
4819 	const struct wmi_mgmt_tx_compl_event *ev;
4820 	int ret;
4821 
4822 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4823 	if (IS_ERR(tb)) {
4824 		ret = PTR_ERR(tb);
4825 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4826 		return ret;
4827 	}
4828 
4829 	ev = tb[WMI_TAG_MGMT_TX_COMPL_EVENT];
4830 	if (!ev) {
4831 		ath11k_warn(ab, "failed to fetch mgmt tx compl ev");
4832 		kfree(tb);
4833 		return -EPROTO;
4834 	}
4835 
4836 	param->pdev_id = ev->pdev_id;
4837 	param->desc_id = ev->desc_id;
4838 	param->status = ev->status;
4839 
4840 	kfree(tb);
4841 	return 0;
4842 }
4843 
4844 static void ath11k_wmi_event_scan_started(struct ath11k *ar)
4845 {
4846 	lockdep_assert_held(&ar->data_lock);
4847 
4848 	switch (ar->scan.state) {
4849 	case ATH11K_SCAN_IDLE:
4850 	case ATH11K_SCAN_RUNNING:
4851 	case ATH11K_SCAN_ABORTING:
4852 		ath11k_warn(ar->ab, "received scan started event in an invalid scan state: %s (%d)\n",
4853 			    ath11k_scan_state_str(ar->scan.state),
4854 			    ar->scan.state);
4855 		break;
4856 	case ATH11K_SCAN_STARTING:
4857 		ar->scan.state = ATH11K_SCAN_RUNNING;
4858 		complete(&ar->scan.started);
4859 		break;
4860 	}
4861 }
4862 
4863 static void ath11k_wmi_event_scan_start_failed(struct ath11k *ar)
4864 {
4865 	lockdep_assert_held(&ar->data_lock);
4866 
4867 	switch (ar->scan.state) {
4868 	case ATH11K_SCAN_IDLE:
4869 	case ATH11K_SCAN_RUNNING:
4870 	case ATH11K_SCAN_ABORTING:
4871 		ath11k_warn(ar->ab, "received scan start failed event in an invalid scan state: %s (%d)\n",
4872 			    ath11k_scan_state_str(ar->scan.state),
4873 			    ar->scan.state);
4874 		break;
4875 	case ATH11K_SCAN_STARTING:
4876 		complete(&ar->scan.started);
4877 		__ath11k_mac_scan_finish(ar);
4878 		break;
4879 	}
4880 }
4881 
4882 static void ath11k_wmi_event_scan_completed(struct ath11k *ar)
4883 {
4884 	lockdep_assert_held(&ar->data_lock);
4885 
4886 	switch (ar->scan.state) {
4887 	case ATH11K_SCAN_IDLE:
4888 	case ATH11K_SCAN_STARTING:
4889 		/* One suspected reason scan can be completed while starting is
4890 		 * if firmware fails to deliver all scan events to the host,
4891 		 * e.g. when transport pipe is full. This has been observed
4892 		 * with spectral scan phyerr events starving wmi transport
4893 		 * pipe. In such case the "scan completed" event should be (and
4894 		 * is) ignored by the host as it may be just firmware's scan
4895 		 * state machine recovering.
4896 		 */
4897 		ath11k_warn(ar->ab, "received scan completed event in an invalid scan state: %s (%d)\n",
4898 			    ath11k_scan_state_str(ar->scan.state),
4899 			    ar->scan.state);
4900 		break;
4901 	case ATH11K_SCAN_RUNNING:
4902 	case ATH11K_SCAN_ABORTING:
4903 		__ath11k_mac_scan_finish(ar);
4904 		break;
4905 	}
4906 }
4907 
4908 static void ath11k_wmi_event_scan_bss_chan(struct ath11k *ar)
4909 {
4910 	lockdep_assert_held(&ar->data_lock);
4911 
4912 	switch (ar->scan.state) {
4913 	case ATH11K_SCAN_IDLE:
4914 	case ATH11K_SCAN_STARTING:
4915 		ath11k_warn(ar->ab, "received scan bss chan event in an invalid scan state: %s (%d)\n",
4916 			    ath11k_scan_state_str(ar->scan.state),
4917 			    ar->scan.state);
4918 		break;
4919 	case ATH11K_SCAN_RUNNING:
4920 	case ATH11K_SCAN_ABORTING:
4921 		ar->scan_channel = NULL;
4922 		break;
4923 	}
4924 }
4925 
4926 static void ath11k_wmi_event_scan_foreign_chan(struct ath11k *ar, u32 freq)
4927 {
4928 	lockdep_assert_held(&ar->data_lock);
4929 
4930 	switch (ar->scan.state) {
4931 	case ATH11K_SCAN_IDLE:
4932 	case ATH11K_SCAN_STARTING:
4933 		ath11k_warn(ar->ab, "received scan foreign chan event in an invalid scan state: %s (%d)\n",
4934 			    ath11k_scan_state_str(ar->scan.state),
4935 			    ar->scan.state);
4936 		break;
4937 	case ATH11K_SCAN_RUNNING:
4938 	case ATH11K_SCAN_ABORTING:
4939 		ar->scan_channel = ieee80211_get_channel(ar->hw->wiphy, freq);
4940 		break;
4941 	}
4942 }
4943 
4944 static const char *
4945 ath11k_wmi_event_scan_type_str(enum wmi_scan_event_type type,
4946 			       enum wmi_scan_completion_reason reason)
4947 {
4948 	switch (type) {
4949 	case WMI_SCAN_EVENT_STARTED:
4950 		return "started";
4951 	case WMI_SCAN_EVENT_COMPLETED:
4952 		switch (reason) {
4953 		case WMI_SCAN_REASON_COMPLETED:
4954 			return "completed";
4955 		case WMI_SCAN_REASON_CANCELLED:
4956 			return "completed [cancelled]";
4957 		case WMI_SCAN_REASON_PREEMPTED:
4958 			return "completed [preempted]";
4959 		case WMI_SCAN_REASON_TIMEDOUT:
4960 			return "completed [timedout]";
4961 		case WMI_SCAN_REASON_INTERNAL_FAILURE:
4962 			return "completed [internal err]";
4963 		case WMI_SCAN_REASON_MAX:
4964 			break;
4965 		}
4966 		return "completed [unknown]";
4967 	case WMI_SCAN_EVENT_BSS_CHANNEL:
4968 		return "bss channel";
4969 	case WMI_SCAN_EVENT_FOREIGN_CHAN:
4970 		return "foreign channel";
4971 	case WMI_SCAN_EVENT_DEQUEUED:
4972 		return "dequeued";
4973 	case WMI_SCAN_EVENT_PREEMPTED:
4974 		return "preempted";
4975 	case WMI_SCAN_EVENT_START_FAILED:
4976 		return "start failed";
4977 	case WMI_SCAN_EVENT_RESTARTED:
4978 		return "restarted";
4979 	case WMI_SCAN_EVENT_FOREIGN_CHAN_EXIT:
4980 		return "foreign channel exit";
4981 	default:
4982 		return "unknown";
4983 	}
4984 }
4985 
4986 static int ath11k_pull_scan_ev(struct ath11k_base *ab, struct sk_buff *skb,
4987 			       struct wmi_scan_event *scan_evt_param)
4988 {
4989 	const void **tb;
4990 	const struct wmi_scan_event *ev;
4991 	int ret;
4992 
4993 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
4994 	if (IS_ERR(tb)) {
4995 		ret = PTR_ERR(tb);
4996 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
4997 		return ret;
4998 	}
4999 
5000 	ev = tb[WMI_TAG_SCAN_EVENT];
5001 	if (!ev) {
5002 		ath11k_warn(ab, "failed to fetch scan ev");
5003 		kfree(tb);
5004 		return -EPROTO;
5005 	}
5006 
5007 	scan_evt_param->event_type = ev->event_type;
5008 	scan_evt_param->reason = ev->reason;
5009 	scan_evt_param->channel_freq = ev->channel_freq;
5010 	scan_evt_param->scan_req_id = ev->scan_req_id;
5011 	scan_evt_param->scan_id = ev->scan_id;
5012 	scan_evt_param->vdev_id = ev->vdev_id;
5013 	scan_evt_param->tsf_timestamp = ev->tsf_timestamp;
5014 
5015 	kfree(tb);
5016 	return 0;
5017 }
5018 
5019 static int ath11k_pull_peer_sta_kickout_ev(struct ath11k_base *ab, struct sk_buff *skb,
5020 					   struct wmi_peer_sta_kickout_arg *arg)
5021 {
5022 	const void **tb;
5023 	const struct wmi_peer_sta_kickout_event *ev;
5024 	int ret;
5025 
5026 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
5027 	if (IS_ERR(tb)) {
5028 		ret = PTR_ERR(tb);
5029 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5030 		return ret;
5031 	}
5032 
5033 	ev = tb[WMI_TAG_PEER_STA_KICKOUT_EVENT];
5034 	if (!ev) {
5035 		ath11k_warn(ab, "failed to fetch peer sta kickout ev");
5036 		kfree(tb);
5037 		return -EPROTO;
5038 	}
5039 
5040 	arg->mac_addr = ev->peer_macaddr.addr;
5041 
5042 	kfree(tb);
5043 	return 0;
5044 }
5045 
5046 static int ath11k_pull_roam_ev(struct ath11k_base *ab, struct sk_buff *skb,
5047 			       struct wmi_roam_event *roam_ev)
5048 {
5049 	const void **tb;
5050 	const struct wmi_roam_event *ev;
5051 	int ret;
5052 
5053 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
5054 	if (IS_ERR(tb)) {
5055 		ret = PTR_ERR(tb);
5056 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5057 		return ret;
5058 	}
5059 
5060 	ev = tb[WMI_TAG_ROAM_EVENT];
5061 	if (!ev) {
5062 		ath11k_warn(ab, "failed to fetch roam ev");
5063 		kfree(tb);
5064 		return -EPROTO;
5065 	}
5066 
5067 	roam_ev->vdev_id = ev->vdev_id;
5068 	roam_ev->reason = ev->reason;
5069 	roam_ev->rssi = ev->rssi;
5070 
5071 	kfree(tb);
5072 	return 0;
5073 }
5074 
5075 static int freq_to_idx(struct ath11k *ar, int freq)
5076 {
5077 	struct ieee80211_supported_band *sband;
5078 	int band, ch, idx = 0;
5079 
5080 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
5081 		sband = ar->hw->wiphy->bands[band];
5082 		if (!sband)
5083 			continue;
5084 
5085 		for (ch = 0; ch < sband->n_channels; ch++, idx++)
5086 			if (sband->channels[ch].center_freq == freq)
5087 				goto exit;
5088 	}
5089 
5090 exit:
5091 	return idx;
5092 }
5093 
5094 static int ath11k_pull_chan_info_ev(struct ath11k_base *ab, u8 *evt_buf,
5095 				    u32 len, struct wmi_chan_info_event *ch_info_ev)
5096 {
5097 	const void **tb;
5098 	const struct wmi_chan_info_event *ev;
5099 	int ret;
5100 
5101 	tb = ath11k_wmi_tlv_parse_alloc(ab, evt_buf, len, GFP_ATOMIC);
5102 	if (IS_ERR(tb)) {
5103 		ret = PTR_ERR(tb);
5104 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5105 		return ret;
5106 	}
5107 
5108 	ev = tb[WMI_TAG_CHAN_INFO_EVENT];
5109 	if (!ev) {
5110 		ath11k_warn(ab, "failed to fetch chan info ev");
5111 		kfree(tb);
5112 		return -EPROTO;
5113 	}
5114 
5115 	ch_info_ev->err_code = ev->err_code;
5116 	ch_info_ev->freq = ev->freq;
5117 	ch_info_ev->cmd_flags = ev->cmd_flags;
5118 	ch_info_ev->noise_floor = ev->noise_floor;
5119 	ch_info_ev->rx_clear_count = ev->rx_clear_count;
5120 	ch_info_ev->cycle_count = ev->cycle_count;
5121 	ch_info_ev->chan_tx_pwr_range = ev->chan_tx_pwr_range;
5122 	ch_info_ev->chan_tx_pwr_tp = ev->chan_tx_pwr_tp;
5123 	ch_info_ev->rx_frame_count = ev->rx_frame_count;
5124 	ch_info_ev->tx_frame_cnt = ev->tx_frame_cnt;
5125 	ch_info_ev->mac_clk_mhz = ev->mac_clk_mhz;
5126 	ch_info_ev->vdev_id = ev->vdev_id;
5127 
5128 	kfree(tb);
5129 	return 0;
5130 }
5131 
5132 static int
5133 ath11k_pull_pdev_bss_chan_info_ev(struct ath11k_base *ab, struct sk_buff *skb,
5134 				  struct wmi_pdev_bss_chan_info_event *bss_ch_info_ev)
5135 {
5136 	const void **tb;
5137 	const struct wmi_pdev_bss_chan_info_event *ev;
5138 	int ret;
5139 
5140 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
5141 	if (IS_ERR(tb)) {
5142 		ret = PTR_ERR(tb);
5143 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5144 		return ret;
5145 	}
5146 
5147 	ev = tb[WMI_TAG_PDEV_BSS_CHAN_INFO_EVENT];
5148 	if (!ev) {
5149 		ath11k_warn(ab, "failed to fetch pdev bss chan info ev");
5150 		kfree(tb);
5151 		return -EPROTO;
5152 	}
5153 
5154 	bss_ch_info_ev->pdev_id = ev->pdev_id;
5155 	bss_ch_info_ev->freq = ev->freq;
5156 	bss_ch_info_ev->noise_floor = ev->noise_floor;
5157 	bss_ch_info_ev->rx_clear_count_low = ev->rx_clear_count_low;
5158 	bss_ch_info_ev->rx_clear_count_high = ev->rx_clear_count_high;
5159 	bss_ch_info_ev->cycle_count_low = ev->cycle_count_low;
5160 	bss_ch_info_ev->cycle_count_high = ev->cycle_count_high;
5161 	bss_ch_info_ev->tx_cycle_count_low = ev->tx_cycle_count_low;
5162 	bss_ch_info_ev->tx_cycle_count_high = ev->tx_cycle_count_high;
5163 	bss_ch_info_ev->rx_cycle_count_low = ev->rx_cycle_count_low;
5164 	bss_ch_info_ev->rx_cycle_count_high = ev->rx_cycle_count_high;
5165 	bss_ch_info_ev->rx_bss_cycle_count_low = ev->rx_bss_cycle_count_low;
5166 	bss_ch_info_ev->rx_bss_cycle_count_high = ev->rx_bss_cycle_count_high;
5167 
5168 	kfree(tb);
5169 	return 0;
5170 }
5171 
5172 static int
5173 ath11k_pull_vdev_install_key_compl_ev(struct ath11k_base *ab, struct sk_buff *skb,
5174 				      struct wmi_vdev_install_key_complete_arg *arg)
5175 {
5176 	const void **tb;
5177 	const struct wmi_vdev_install_key_compl_event *ev;
5178 	int ret;
5179 
5180 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
5181 	if (IS_ERR(tb)) {
5182 		ret = PTR_ERR(tb);
5183 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5184 		return ret;
5185 	}
5186 
5187 	ev = tb[WMI_TAG_VDEV_INSTALL_KEY_COMPLETE_EVENT];
5188 	if (!ev) {
5189 		ath11k_warn(ab, "failed to fetch vdev install key compl ev");
5190 		kfree(tb);
5191 		return -EPROTO;
5192 	}
5193 
5194 	arg->vdev_id = ev->vdev_id;
5195 	arg->macaddr = ev->peer_macaddr.addr;
5196 	arg->key_idx = ev->key_idx;
5197 	arg->key_flags = ev->key_flags;
5198 	arg->status = ev->status;
5199 
5200 	kfree(tb);
5201 	return 0;
5202 }
5203 
5204 static int ath11k_pull_peer_assoc_conf_ev(struct ath11k_base *ab, struct sk_buff *skb,
5205 					  struct wmi_peer_assoc_conf_arg *peer_assoc_conf)
5206 {
5207 	const void **tb;
5208 	const struct wmi_peer_assoc_conf_event *ev;
5209 	int ret;
5210 
5211 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
5212 	if (IS_ERR(tb)) {
5213 		ret = PTR_ERR(tb);
5214 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5215 		return ret;
5216 	}
5217 
5218 	ev = tb[WMI_TAG_PEER_ASSOC_CONF_EVENT];
5219 	if (!ev) {
5220 		ath11k_warn(ab, "failed to fetch peer assoc conf ev");
5221 		kfree(tb);
5222 		return -EPROTO;
5223 	}
5224 
5225 	peer_assoc_conf->vdev_id = ev->vdev_id;
5226 	peer_assoc_conf->macaddr = ev->peer_macaddr.addr;
5227 
5228 	kfree(tb);
5229 	return 0;
5230 }
5231 
5232 static void ath11k_wmi_pull_pdev_stats_base(const struct wmi_pdev_stats_base *src,
5233 					    struct ath11k_fw_stats_pdev *dst)
5234 {
5235 	dst->ch_noise_floor = src->chan_nf;
5236 	dst->tx_frame_count = src->tx_frame_count;
5237 	dst->rx_frame_count = src->rx_frame_count;
5238 	dst->rx_clear_count = src->rx_clear_count;
5239 	dst->cycle_count = src->cycle_count;
5240 	dst->phy_err_count = src->phy_err_count;
5241 	dst->chan_tx_power = src->chan_tx_pwr;
5242 }
5243 
5244 static void
5245 ath11k_wmi_pull_pdev_stats_tx(const struct wmi_pdev_stats_tx *src,
5246 			      struct ath11k_fw_stats_pdev *dst)
5247 {
5248 	dst->comp_queued = src->comp_queued;
5249 	dst->comp_delivered = src->comp_delivered;
5250 	dst->msdu_enqued = src->msdu_enqued;
5251 	dst->mpdu_enqued = src->mpdu_enqued;
5252 	dst->wmm_drop = src->wmm_drop;
5253 	dst->local_enqued = src->local_enqued;
5254 	dst->local_freed = src->local_freed;
5255 	dst->hw_queued = src->hw_queued;
5256 	dst->hw_reaped = src->hw_reaped;
5257 	dst->underrun = src->underrun;
5258 	dst->hw_paused = src->hw_paused;
5259 	dst->tx_abort = src->tx_abort;
5260 	dst->mpdus_requeued = src->mpdus_requeued;
5261 	dst->tx_ko = src->tx_ko;
5262 	dst->tx_xretry = src->tx_xretry;
5263 	dst->data_rc = src->data_rc;
5264 	dst->self_triggers = src->self_triggers;
5265 	dst->sw_retry_failure = src->sw_retry_failure;
5266 	dst->illgl_rate_phy_err = src->illgl_rate_phy_err;
5267 	dst->pdev_cont_xretry = src->pdev_cont_xretry;
5268 	dst->pdev_tx_timeout = src->pdev_tx_timeout;
5269 	dst->pdev_resets = src->pdev_resets;
5270 	dst->stateless_tid_alloc_failure = src->stateless_tid_alloc_failure;
5271 	dst->phy_underrun = src->phy_underrun;
5272 	dst->txop_ovf = src->txop_ovf;
5273 	dst->seq_posted = src->seq_posted;
5274 	dst->seq_failed_queueing = src->seq_failed_queueing;
5275 	dst->seq_completed = src->seq_completed;
5276 	dst->seq_restarted = src->seq_restarted;
5277 	dst->mu_seq_posted = src->mu_seq_posted;
5278 	dst->mpdus_sw_flush = src->mpdus_sw_flush;
5279 	dst->mpdus_hw_filter = src->mpdus_hw_filter;
5280 	dst->mpdus_truncated = src->mpdus_truncated;
5281 	dst->mpdus_ack_failed = src->mpdus_ack_failed;
5282 	dst->mpdus_expired = src->mpdus_expired;
5283 }
5284 
5285 static void ath11k_wmi_pull_pdev_stats_rx(const struct wmi_pdev_stats_rx *src,
5286 					  struct ath11k_fw_stats_pdev *dst)
5287 {
5288 	dst->mid_ppdu_route_change = src->mid_ppdu_route_change;
5289 	dst->status_rcvd = src->status_rcvd;
5290 	dst->r0_frags = src->r0_frags;
5291 	dst->r1_frags = src->r1_frags;
5292 	dst->r2_frags = src->r2_frags;
5293 	dst->r3_frags = src->r3_frags;
5294 	dst->htt_msdus = src->htt_msdus;
5295 	dst->htt_mpdus = src->htt_mpdus;
5296 	dst->loc_msdus = src->loc_msdus;
5297 	dst->loc_mpdus = src->loc_mpdus;
5298 	dst->oversize_amsdu = src->oversize_amsdu;
5299 	dst->phy_errs = src->phy_errs;
5300 	dst->phy_err_drop = src->phy_err_drop;
5301 	dst->mpdu_errs = src->mpdu_errs;
5302 	dst->rx_ovfl_errs = src->rx_ovfl_errs;
5303 }
5304 
5305 static void
5306 ath11k_wmi_pull_vdev_stats(const struct wmi_vdev_stats *src,
5307 			   struct ath11k_fw_stats_vdev *dst)
5308 {
5309 	int i;
5310 
5311 	dst->vdev_id = src->vdev_id;
5312 	dst->beacon_snr = src->beacon_snr;
5313 	dst->data_snr = src->data_snr;
5314 	dst->num_rx_frames = src->num_rx_frames;
5315 	dst->num_rts_fail = src->num_rts_fail;
5316 	dst->num_rts_success = src->num_rts_success;
5317 	dst->num_rx_err = src->num_rx_err;
5318 	dst->num_rx_discard = src->num_rx_discard;
5319 	dst->num_tx_not_acked = src->num_tx_not_acked;
5320 
5321 	for (i = 0; i < ARRAY_SIZE(src->num_tx_frames); i++)
5322 		dst->num_tx_frames[i] = src->num_tx_frames[i];
5323 
5324 	for (i = 0; i < ARRAY_SIZE(src->num_tx_frames_retries); i++)
5325 		dst->num_tx_frames_retries[i] = src->num_tx_frames_retries[i];
5326 
5327 	for (i = 0; i < ARRAY_SIZE(src->num_tx_frames_failures); i++)
5328 		dst->num_tx_frames_failures[i] = src->num_tx_frames_failures[i];
5329 
5330 	for (i = 0; i < ARRAY_SIZE(src->tx_rate_history); i++)
5331 		dst->tx_rate_history[i] = src->tx_rate_history[i];
5332 
5333 	for (i = 0; i < ARRAY_SIZE(src->beacon_rssi_history); i++)
5334 		dst->beacon_rssi_history[i] = src->beacon_rssi_history[i];
5335 }
5336 
5337 static void
5338 ath11k_wmi_pull_bcn_stats(const struct wmi_bcn_stats *src,
5339 			  struct ath11k_fw_stats_bcn *dst)
5340 {
5341 	dst->vdev_id = src->vdev_id;
5342 	dst->tx_bcn_succ_cnt = src->tx_bcn_succ_cnt;
5343 	dst->tx_bcn_outage_cnt = src->tx_bcn_outage_cnt;
5344 }
5345 
5346 int ath11k_wmi_pull_fw_stats(struct ath11k_base *ab, struct sk_buff *skb,
5347 			     struct ath11k_fw_stats *stats)
5348 {
5349 	const void **tb;
5350 	const struct wmi_stats_event *ev;
5351 	const void *data;
5352 	int i, ret;
5353 	u32 len = skb->len;
5354 
5355 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, len, GFP_ATOMIC);
5356 	if (IS_ERR(tb)) {
5357 		ret = PTR_ERR(tb);
5358 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
5359 		return ret;
5360 	}
5361 
5362 	ev = tb[WMI_TAG_STATS_EVENT];
5363 	data = tb[WMI_TAG_ARRAY_BYTE];
5364 	if (!ev || !data) {
5365 		ath11k_warn(ab, "failed to fetch update stats ev");
5366 		kfree(tb);
5367 		return -EPROTO;
5368 	}
5369 
5370 	ath11k_dbg(ab, ATH11K_DBG_WMI,
5371 		   "wmi stats update ev pdev_id %d pdev %i vdev %i bcn %i\n",
5372 		   ev->pdev_id,
5373 		   ev->num_pdev_stats, ev->num_vdev_stats,
5374 		   ev->num_bcn_stats);
5375 
5376 	stats->pdev_id = ev->pdev_id;
5377 	stats->stats_id = 0;
5378 
5379 	for (i = 0; i < ev->num_pdev_stats; i++) {
5380 		const struct wmi_pdev_stats *src;
5381 		struct ath11k_fw_stats_pdev *dst;
5382 
5383 		src = data;
5384 		if (len < sizeof(*src)) {
5385 			kfree(tb);
5386 			return -EPROTO;
5387 		}
5388 
5389 		stats->stats_id = WMI_REQUEST_PDEV_STAT;
5390 
5391 		data += sizeof(*src);
5392 		len -= sizeof(*src);
5393 
5394 		dst = kzalloc(sizeof(*dst), GFP_ATOMIC);
5395 		if (!dst)
5396 			continue;
5397 
5398 		ath11k_wmi_pull_pdev_stats_base(&src->base, dst);
5399 		ath11k_wmi_pull_pdev_stats_tx(&src->tx, dst);
5400 		ath11k_wmi_pull_pdev_stats_rx(&src->rx, dst);
5401 		list_add_tail(&dst->list, &stats->pdevs);
5402 	}
5403 
5404 	for (i = 0; i < ev->num_vdev_stats; i++) {
5405 		const struct wmi_vdev_stats *src;
5406 		struct ath11k_fw_stats_vdev *dst;
5407 
5408 		src = data;
5409 		if (len < sizeof(*src)) {
5410 			kfree(tb);
5411 			return -EPROTO;
5412 		}
5413 
5414 		stats->stats_id = WMI_REQUEST_VDEV_STAT;
5415 
5416 		data += sizeof(*src);
5417 		len -= sizeof(*src);
5418 
5419 		dst = kzalloc(sizeof(*dst), GFP_ATOMIC);
5420 		if (!dst)
5421 			continue;
5422 
5423 		ath11k_wmi_pull_vdev_stats(src, dst);
5424 		list_add_tail(&dst->list, &stats->vdevs);
5425 	}
5426 
5427 	for (i = 0; i < ev->num_bcn_stats; i++) {
5428 		const struct wmi_bcn_stats *src;
5429 		struct ath11k_fw_stats_bcn *dst;
5430 
5431 		src = data;
5432 		if (len < sizeof(*src)) {
5433 			kfree(tb);
5434 			return -EPROTO;
5435 		}
5436 
5437 		stats->stats_id = WMI_REQUEST_BCN_STAT;
5438 
5439 		data += sizeof(*src);
5440 		len -= sizeof(*src);
5441 
5442 		dst = kzalloc(sizeof(*dst), GFP_ATOMIC);
5443 		if (!dst)
5444 			continue;
5445 
5446 		ath11k_wmi_pull_bcn_stats(src, dst);
5447 		list_add_tail(&dst->list, &stats->bcn);
5448 	}
5449 
5450 	kfree(tb);
5451 	return 0;
5452 }
5453 
5454 size_t ath11k_wmi_fw_stats_num_vdevs(struct list_head *head)
5455 {
5456 	struct ath11k_fw_stats_vdev *i;
5457 	size_t num = 0;
5458 
5459 	list_for_each_entry(i, head, list)
5460 		++num;
5461 
5462 	return num;
5463 }
5464 
5465 static size_t ath11k_wmi_fw_stats_num_bcn(struct list_head *head)
5466 {
5467 	struct ath11k_fw_stats_bcn *i;
5468 	size_t num = 0;
5469 
5470 	list_for_each_entry(i, head, list)
5471 		++num;
5472 
5473 	return num;
5474 }
5475 
5476 static void
5477 ath11k_wmi_fw_pdev_base_stats_fill(const struct ath11k_fw_stats_pdev *pdev,
5478 				   char *buf, u32 *length)
5479 {
5480 	u32 len = *length;
5481 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5482 
5483 	len += scnprintf(buf + len, buf_len - len, "\n");
5484 	len += scnprintf(buf + len, buf_len - len, "%30s\n",
5485 			"ath11k PDEV stats");
5486 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5487 			"=================");
5488 
5489 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5490 			"Channel noise floor", pdev->ch_noise_floor);
5491 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5492 			"Channel TX power", pdev->chan_tx_power);
5493 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5494 			"TX frame count", pdev->tx_frame_count);
5495 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5496 			"RX frame count", pdev->rx_frame_count);
5497 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5498 			"RX clear count", pdev->rx_clear_count);
5499 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5500 			"Cycle count", pdev->cycle_count);
5501 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5502 			"PHY error count", pdev->phy_err_count);
5503 
5504 	*length = len;
5505 }
5506 
5507 static void
5508 ath11k_wmi_fw_pdev_tx_stats_fill(const struct ath11k_fw_stats_pdev *pdev,
5509 				 char *buf, u32 *length)
5510 {
5511 	u32 len = *length;
5512 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5513 
5514 	len += scnprintf(buf + len, buf_len - len, "\n%30s\n",
5515 			 "ath11k PDEV TX stats");
5516 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5517 			 "====================");
5518 
5519 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5520 			 "HTT cookies queued", pdev->comp_queued);
5521 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5522 			 "HTT cookies disp.", pdev->comp_delivered);
5523 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5524 			 "MSDU queued", pdev->msdu_enqued);
5525 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5526 			 "MPDU queued", pdev->mpdu_enqued);
5527 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5528 			 "MSDUs dropped", pdev->wmm_drop);
5529 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5530 			 "Local enqued", pdev->local_enqued);
5531 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5532 			 "Local freed", pdev->local_freed);
5533 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5534 			 "HW queued", pdev->hw_queued);
5535 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5536 			 "PPDUs reaped", pdev->hw_reaped);
5537 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5538 			 "Num underruns", pdev->underrun);
5539 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5540 			 "Num HW Paused", pdev->hw_paused);
5541 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5542 			 "PPDUs cleaned", pdev->tx_abort);
5543 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5544 			 "MPDUs requeued", pdev->mpdus_requeued);
5545 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5546 			 "PPDU OK", pdev->tx_ko);
5547 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5548 			 "Excessive retries", pdev->tx_xretry);
5549 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5550 			 "HW rate", pdev->data_rc);
5551 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5552 			 "Sched self triggers", pdev->self_triggers);
5553 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5554 			 "Dropped due to SW retries",
5555 			 pdev->sw_retry_failure);
5556 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5557 			 "Illegal rate phy errors",
5558 			 pdev->illgl_rate_phy_err);
5559 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5560 			 "PDEV continuous xretry", pdev->pdev_cont_xretry);
5561 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5562 			 "TX timeout", pdev->pdev_tx_timeout);
5563 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5564 			 "PDEV resets", pdev->pdev_resets);
5565 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5566 			 "Stateless TIDs alloc failures",
5567 			 pdev->stateless_tid_alloc_failure);
5568 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5569 			 "PHY underrun", pdev->phy_underrun);
5570 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5571 			 "MPDU is more than txop limit", pdev->txop_ovf);
5572 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5573 			 "Num sequences posted", pdev->seq_posted);
5574 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5575 			 "Num seq failed queueing ", pdev->seq_failed_queueing);
5576 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5577 			 "Num sequences completed ", pdev->seq_completed);
5578 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5579 			 "Num sequences restarted ", pdev->seq_restarted);
5580 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5581 			 "Num of MU sequences posted ", pdev->mu_seq_posted);
5582 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5583 			 "Num of MPDUS SW flushed ", pdev->mpdus_sw_flush);
5584 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5585 			 "Num of MPDUS HW filtered ", pdev->mpdus_hw_filter);
5586 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5587 			 "Num of MPDUS truncated ", pdev->mpdus_truncated);
5588 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5589 			 "Num of MPDUS ACK failed ", pdev->mpdus_ack_failed);
5590 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5591 			 "Num of MPDUS expired ", pdev->mpdus_expired);
5592 	*length = len;
5593 }
5594 
5595 static void
5596 ath11k_wmi_fw_pdev_rx_stats_fill(const struct ath11k_fw_stats_pdev *pdev,
5597 				 char *buf, u32 *length)
5598 {
5599 	u32 len = *length;
5600 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5601 
5602 	len += scnprintf(buf + len, buf_len - len, "\n%30s\n",
5603 			 "ath11k PDEV RX stats");
5604 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5605 			 "====================");
5606 
5607 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5608 			 "Mid PPDU route change",
5609 			 pdev->mid_ppdu_route_change);
5610 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5611 			 "Tot. number of statuses", pdev->status_rcvd);
5612 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5613 			 "Extra frags on rings 0", pdev->r0_frags);
5614 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5615 			 "Extra frags on rings 1", pdev->r1_frags);
5616 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5617 			 "Extra frags on rings 2", pdev->r2_frags);
5618 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5619 			 "Extra frags on rings 3", pdev->r3_frags);
5620 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5621 			 "MSDUs delivered to HTT", pdev->htt_msdus);
5622 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5623 			 "MPDUs delivered to HTT", pdev->htt_mpdus);
5624 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5625 			 "MSDUs delivered to stack", pdev->loc_msdus);
5626 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5627 			 "MPDUs delivered to stack", pdev->loc_mpdus);
5628 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5629 			 "Oversized AMSUs", pdev->oversize_amsdu);
5630 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5631 			 "PHY errors", pdev->phy_errs);
5632 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5633 			 "PHY errors drops", pdev->phy_err_drop);
5634 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5635 			 "MPDU errors (FCS, MIC, ENC)", pdev->mpdu_errs);
5636 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5637 			 "Overflow errors", pdev->rx_ovfl_errs);
5638 	*length = len;
5639 }
5640 
5641 static void
5642 ath11k_wmi_fw_vdev_stats_fill(struct ath11k *ar,
5643 			      const struct ath11k_fw_stats_vdev *vdev,
5644 			      char *buf, u32 *length)
5645 {
5646 	u32 len = *length;
5647 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5648 	struct ath11k_vif *arvif = ath11k_mac_get_arvif(ar, vdev->vdev_id);
5649 	u8 *vif_macaddr;
5650 	int i;
5651 
5652 	/* VDEV stats has all the active VDEVs of other PDEVs as well,
5653 	 * ignoring those not part of requested PDEV
5654 	 */
5655 	if (!arvif)
5656 		return;
5657 
5658 	vif_macaddr = arvif->vif->addr;
5659 
5660 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5661 			 "VDEV ID", vdev->vdev_id);
5662 	len += scnprintf(buf + len, buf_len - len, "%30s %pM\n",
5663 			 "VDEV MAC address", vif_macaddr);
5664 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5665 			 "beacon snr", vdev->beacon_snr);
5666 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5667 			 "data snr", vdev->data_snr);
5668 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5669 			 "num rx frames", vdev->num_rx_frames);
5670 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5671 			 "num rts fail", vdev->num_rts_fail);
5672 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5673 			 "num rts success", vdev->num_rts_success);
5674 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5675 			 "num rx err", vdev->num_rx_err);
5676 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5677 			 "num rx discard", vdev->num_rx_discard);
5678 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5679 			 "num tx not acked", vdev->num_tx_not_acked);
5680 
5681 	for (i = 0 ; i < ARRAY_SIZE(vdev->num_tx_frames); i++)
5682 		len += scnprintf(buf + len, buf_len - len,
5683 				"%25s [%02d] %u\n",
5684 				"num tx frames", i,
5685 				vdev->num_tx_frames[i]);
5686 
5687 	for (i = 0 ; i < ARRAY_SIZE(vdev->num_tx_frames_retries); i++)
5688 		len += scnprintf(buf + len, buf_len - len,
5689 				"%25s [%02d] %u\n",
5690 				"num tx frames retries", i,
5691 				vdev->num_tx_frames_retries[i]);
5692 
5693 	for (i = 0 ; i < ARRAY_SIZE(vdev->num_tx_frames_failures); i++)
5694 		len += scnprintf(buf + len, buf_len - len,
5695 				"%25s [%02d] %u\n",
5696 				"num tx frames failures", i,
5697 				vdev->num_tx_frames_failures[i]);
5698 
5699 	for (i = 0 ; i < ARRAY_SIZE(vdev->tx_rate_history); i++)
5700 		len += scnprintf(buf + len, buf_len - len,
5701 				"%25s [%02d] 0x%08x\n",
5702 				"tx rate history", i,
5703 				vdev->tx_rate_history[i]);
5704 
5705 	for (i = 0 ; i < ARRAY_SIZE(vdev->beacon_rssi_history); i++)
5706 		len += scnprintf(buf + len, buf_len - len,
5707 				"%25s [%02d] %u\n",
5708 				"beacon rssi history", i,
5709 				vdev->beacon_rssi_history[i]);
5710 
5711 	len += scnprintf(buf + len, buf_len - len, "\n");
5712 	*length = len;
5713 }
5714 
5715 static void
5716 ath11k_wmi_fw_bcn_stats_fill(struct ath11k *ar,
5717 			     const struct ath11k_fw_stats_bcn *bcn,
5718 			     char *buf, u32 *length)
5719 {
5720 	u32 len = *length;
5721 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5722 	struct ath11k_vif *arvif = ath11k_mac_get_arvif(ar, bcn->vdev_id);
5723 	u8 *vdev_macaddr;
5724 
5725 	if (!arvif) {
5726 		ath11k_warn(ar->ab, "invalid vdev id %d in bcn stats",
5727 			    bcn->vdev_id);
5728 		return;
5729 	}
5730 
5731 	vdev_macaddr = arvif->vif->addr;
5732 
5733 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5734 			 "VDEV ID", bcn->vdev_id);
5735 	len += scnprintf(buf + len, buf_len - len, "%30s %pM\n",
5736 			 "VDEV MAC address", vdev_macaddr);
5737 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5738 			 "================");
5739 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5740 			 "Num of beacon tx success", bcn->tx_bcn_succ_cnt);
5741 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5742 			 "Num of beacon tx failures", bcn->tx_bcn_outage_cnt);
5743 
5744 	len += scnprintf(buf + len, buf_len - len, "\n");
5745 	*length = len;
5746 }
5747 
5748 void ath11k_wmi_fw_stats_fill(struct ath11k *ar,
5749 			      struct ath11k_fw_stats *fw_stats,
5750 			      u32 stats_id, char *buf)
5751 {
5752 	u32 len = 0;
5753 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5754 	const struct ath11k_fw_stats_pdev *pdev;
5755 	const struct ath11k_fw_stats_vdev *vdev;
5756 	const struct ath11k_fw_stats_bcn *bcn;
5757 	size_t num_bcn;
5758 
5759 	spin_lock_bh(&ar->data_lock);
5760 
5761 	if (stats_id == WMI_REQUEST_PDEV_STAT) {
5762 		pdev = list_first_entry_or_null(&fw_stats->pdevs,
5763 						struct ath11k_fw_stats_pdev, list);
5764 		if (!pdev) {
5765 			ath11k_warn(ar->ab, "failed to get pdev stats\n");
5766 			goto unlock;
5767 		}
5768 
5769 		ath11k_wmi_fw_pdev_base_stats_fill(pdev, buf, &len);
5770 		ath11k_wmi_fw_pdev_tx_stats_fill(pdev, buf, &len);
5771 		ath11k_wmi_fw_pdev_rx_stats_fill(pdev, buf, &len);
5772 	}
5773 
5774 	if (stats_id == WMI_REQUEST_VDEV_STAT) {
5775 		len += scnprintf(buf + len, buf_len - len, "\n");
5776 		len += scnprintf(buf + len, buf_len - len, "%30s\n",
5777 				 "ath11k VDEV stats");
5778 		len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5779 				 "=================");
5780 
5781 		list_for_each_entry(vdev, &fw_stats->vdevs, list)
5782 			ath11k_wmi_fw_vdev_stats_fill(ar, vdev, buf, &len);
5783 	}
5784 
5785 	if (stats_id == WMI_REQUEST_BCN_STAT) {
5786 		num_bcn = ath11k_wmi_fw_stats_num_bcn(&fw_stats->bcn);
5787 
5788 		len += scnprintf(buf + len, buf_len - len, "\n");
5789 		len += scnprintf(buf + len, buf_len - len, "%30s (%zu)\n",
5790 				 "ath11k Beacon stats", num_bcn);
5791 		len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5792 				 "===================");
5793 
5794 		list_for_each_entry(bcn, &fw_stats->bcn, list)
5795 			ath11k_wmi_fw_bcn_stats_fill(ar, bcn, buf, &len);
5796 	}
5797 
5798 unlock:
5799 	spin_unlock_bh(&ar->data_lock);
5800 
5801 	if (len >= buf_len)
5802 		buf[len - 1] = 0;
5803 	else
5804 		buf[len] = 0;
5805 }
5806 
5807 static void ath11k_wmi_op_ep_tx_credits(struct ath11k_base *ab)
5808 {
5809 	/* try to send pending beacons first. they take priority */
5810 	wake_up(&ab->wmi_ab.tx_credits_wq);
5811 }
5812 
5813 static void ath11k_wmi_htc_tx_complete(struct ath11k_base *ab,
5814 				       struct sk_buff *skb)
5815 {
5816 	dev_kfree_skb(skb);
5817 }
5818 
5819 static bool ath11k_reg_is_world_alpha(char *alpha)
5820 {
5821 	return alpha[0] == '0' && alpha[1] == '0';
5822 }
5823 
5824 static int ath11k_reg_chan_list_event(struct ath11k_base *ab, struct sk_buff *skb)
5825 {
5826 	struct cur_regulatory_info *reg_info = NULL;
5827 	struct ieee80211_regdomain *regd = NULL;
5828 	bool intersect = false;
5829 	int ret = 0, pdev_idx;
5830 	struct ath11k *ar;
5831 
5832 	reg_info = kzalloc(sizeof(*reg_info), GFP_ATOMIC);
5833 	if (!reg_info) {
5834 		ret = -ENOMEM;
5835 		goto fallback;
5836 	}
5837 
5838 	ret = ath11k_pull_reg_chan_list_update_ev(ab, skb, reg_info);
5839 	if (ret) {
5840 		ath11k_warn(ab, "failed to extract regulatory info from received event\n");
5841 		goto fallback;
5842 	}
5843 
5844 	if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
5845 		/* In case of failure to set the requested ctry,
5846 		 * fw retains the current regd. We print a failure info
5847 		 * and return from here.
5848 		 */
5849 		ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");
5850 		goto mem_free;
5851 	}
5852 
5853 	pdev_idx = reg_info->phy_id;
5854 
5855 	/* Avoid default reg rule updates sent during FW recovery if
5856 	 * it is already available
5857 	 */
5858 	spin_lock(&ab->base_lock);
5859 	if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags) &&
5860 	    ab->default_regd[pdev_idx]) {
5861 		spin_unlock(&ab->base_lock);
5862 		goto mem_free;
5863 	}
5864 	spin_unlock(&ab->base_lock);
5865 
5866 	if (pdev_idx >= ab->num_radios) {
5867 		/* Process the event for phy0 only if single_pdev_only
5868 		 * is true. If pdev_idx is valid but not 0, discard the
5869 		 * event. Otherwise, it goes to fallback.
5870 		 */
5871 		if (ab->hw_params.single_pdev_only &&
5872 		    pdev_idx < ab->hw_params.num_rxmda_per_pdev)
5873 			goto mem_free;
5874 		else
5875 			goto fallback;
5876 	}
5877 
5878 	/* Avoid multiple overwrites to default regd, during core
5879 	 * stop-start after mac registration.
5880 	 */
5881 	if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
5882 	    !memcmp((char *)ab->default_regd[pdev_idx]->alpha2,
5883 		    (char *)reg_info->alpha2, 2))
5884 		goto mem_free;
5885 
5886 	/* Intersect new rules with default regd if a new country setting was
5887 	 * requested, i.e a default regd was already set during initialization
5888 	 * and the regd coming from this event has a valid country info.
5889 	 */
5890 	if (ab->default_regd[pdev_idx] &&
5891 	    !ath11k_reg_is_world_alpha((char *)
5892 		ab->default_regd[pdev_idx]->alpha2) &&
5893 	    !ath11k_reg_is_world_alpha((char *)reg_info->alpha2))
5894 		intersect = true;
5895 
5896 	regd = ath11k_reg_build_regd(ab, reg_info, intersect);
5897 	if (!regd) {
5898 		ath11k_warn(ab, "failed to build regd from reg_info\n");
5899 		goto fallback;
5900 	}
5901 
5902 	spin_lock(&ab->base_lock);
5903 	if (ab->default_regd[pdev_idx]) {
5904 		/* The initial rules from FW after WMI Init is to build
5905 		 * the default regd. From then on, any rules updated for
5906 		 * the pdev could be due to user reg changes.
5907 		 * Free previously built regd before assigning the newly
5908 		 * generated regd to ar. NULL pointer handling will be
5909 		 * taken care by kfree itself.
5910 		 */
5911 		ar = ab->pdevs[pdev_idx].ar;
5912 		kfree(ab->new_regd[pdev_idx]);
5913 		ab->new_regd[pdev_idx] = regd;
5914 		ieee80211_queue_work(ar->hw, &ar->regd_update_work);
5915 	} else {
5916 		/* This regd would be applied during mac registration and is
5917 		 * held constant throughout for regd intersection purpose
5918 		 */
5919 		ab->default_regd[pdev_idx] = regd;
5920 	}
5921 	ab->dfs_region = reg_info->dfs_region;
5922 	spin_unlock(&ab->base_lock);
5923 
5924 	goto mem_free;
5925 
5926 fallback:
5927 	/* Fallback to older reg (by sending previous country setting
5928 	 * again if fw has succeded and we failed to process here.
5929 	 * The Regdomain should be uniform across driver and fw. Since the
5930 	 * FW has processed the command and sent a success status, we expect
5931 	 * this function to succeed as well. If it doesn't, CTRY needs to be
5932 	 * reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.
5933 	 */
5934 	/* TODO: This is rare, but still should also be handled */
5935 	WARN_ON(1);
5936 mem_free:
5937 	if (reg_info) {
5938 		kfree(reg_info->reg_rules_2g_ptr);
5939 		kfree(reg_info->reg_rules_5g_ptr);
5940 		kfree(reg_info);
5941 	}
5942 	return ret;
5943 }
5944 
5945 static int ath11k_wmi_tlv_rdy_parse(struct ath11k_base *ab, u16 tag, u16 len,
5946 				    const void *ptr, void *data)
5947 {
5948 	struct wmi_tlv_rdy_parse *rdy_parse = data;
5949 	struct wmi_ready_event fixed_param;
5950 	struct wmi_mac_addr *addr_list;
5951 	struct ath11k_pdev *pdev;
5952 	u32 num_mac_addr;
5953 	int i;
5954 
5955 	switch (tag) {
5956 	case WMI_TAG_READY_EVENT:
5957 		memset(&fixed_param, 0, sizeof(fixed_param));
5958 		memcpy(&fixed_param, (struct wmi_ready_event *)ptr,
5959 		       min_t(u16, sizeof(fixed_param), len));
5960 		ab->wlan_init_status = fixed_param.ready_event_min.status;
5961 		rdy_parse->num_extra_mac_addr =
5962 			fixed_param.ready_event_min.num_extra_mac_addr;
5963 
5964 		ether_addr_copy(ab->mac_addr,
5965 				fixed_param.ready_event_min.mac_addr.addr);
5966 		ab->pktlog_defs_checksum = fixed_param.pktlog_defs_checksum;
5967 		ab->wmi_ready = true;
5968 		break;
5969 	case WMI_TAG_ARRAY_FIXED_STRUCT:
5970 		addr_list = (struct wmi_mac_addr *)ptr;
5971 		num_mac_addr = rdy_parse->num_extra_mac_addr;
5972 
5973 		if (!(ab->num_radios > 1 && num_mac_addr >= ab->num_radios))
5974 			break;
5975 
5976 		for (i = 0; i < ab->num_radios; i++) {
5977 			pdev = &ab->pdevs[i];
5978 			ether_addr_copy(pdev->mac_addr, addr_list[i].addr);
5979 		}
5980 		ab->pdevs_macaddr_valid = true;
5981 		break;
5982 	default:
5983 		break;
5984 	}
5985 
5986 	return 0;
5987 }
5988 
5989 static int ath11k_ready_event(struct ath11k_base *ab, struct sk_buff *skb)
5990 {
5991 	struct wmi_tlv_rdy_parse rdy_parse = { };
5992 	int ret;
5993 
5994 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
5995 				  ath11k_wmi_tlv_rdy_parse, &rdy_parse);
5996 	if (ret) {
5997 		ath11k_warn(ab, "failed to parse tlv %d\n", ret);
5998 		return ret;
5999 	}
6000 
6001 	complete(&ab->wmi_ab.unified_ready);
6002 	return 0;
6003 }
6004 
6005 static void ath11k_peer_delete_resp_event(struct ath11k_base *ab, struct sk_buff *skb)
6006 {
6007 	struct wmi_peer_delete_resp_event peer_del_resp;
6008 	struct ath11k *ar;
6009 
6010 	if (ath11k_pull_peer_del_resp_ev(ab, skb, &peer_del_resp) != 0) {
6011 		ath11k_warn(ab, "failed to extract peer delete resp");
6012 		return;
6013 	}
6014 
6015 	rcu_read_lock();
6016 	ar = ath11k_mac_get_ar_by_vdev_id(ab, peer_del_resp.vdev_id);
6017 	if (!ar) {
6018 		ath11k_warn(ab, "invalid vdev id in peer delete resp ev %d",
6019 			    peer_del_resp.vdev_id);
6020 		rcu_read_unlock();
6021 		return;
6022 	}
6023 
6024 	complete(&ar->peer_delete_done);
6025 	rcu_read_unlock();
6026 	ath11k_dbg(ab, ATH11K_DBG_WMI, "peer delete resp for vdev id %d addr %pM\n",
6027 		   peer_del_resp.vdev_id, peer_del_resp.peer_macaddr.addr);
6028 }
6029 
6030 static void ath11k_vdev_delete_resp_event(struct ath11k_base *ab,
6031 					  struct sk_buff *skb)
6032 {
6033 	struct ath11k *ar;
6034 	u32 vdev_id = 0;
6035 
6036 	if (ath11k_pull_vdev_del_resp_ev(ab, skb, &vdev_id) != 0) {
6037 		ath11k_warn(ab, "failed to extract vdev delete resp");
6038 		return;
6039 	}
6040 
6041 	rcu_read_lock();
6042 	ar = ath11k_mac_get_ar_by_vdev_id(ab, vdev_id);
6043 	if (!ar) {
6044 		ath11k_warn(ab, "invalid vdev id in vdev delete resp ev %d",
6045 			    vdev_id);
6046 		rcu_read_unlock();
6047 		return;
6048 	}
6049 
6050 	complete(&ar->vdev_delete_done);
6051 
6052 	rcu_read_unlock();
6053 
6054 	ath11k_dbg(ab, ATH11K_DBG_WMI, "vdev delete resp for vdev id %d\n",
6055 		   vdev_id);
6056 }
6057 
6058 static inline const char *ath11k_wmi_vdev_resp_print(u32 vdev_resp_status)
6059 {
6060 	switch (vdev_resp_status) {
6061 	case WMI_VDEV_START_RESPONSE_INVALID_VDEVID:
6062 		return "invalid vdev id";
6063 	case WMI_VDEV_START_RESPONSE_NOT_SUPPORTED:
6064 		return "not supported";
6065 	case WMI_VDEV_START_RESPONSE_DFS_VIOLATION:
6066 		return "dfs violation";
6067 	case WMI_VDEV_START_RESPONSE_INVALID_REGDOMAIN:
6068 		return "invalid regdomain";
6069 	default:
6070 		return "unknown";
6071 	}
6072 }
6073 
6074 static void ath11k_vdev_start_resp_event(struct ath11k_base *ab, struct sk_buff *skb)
6075 {
6076 	struct wmi_vdev_start_resp_event vdev_start_resp;
6077 	struct ath11k *ar;
6078 	u32 status;
6079 
6080 	if (ath11k_pull_vdev_start_resp_tlv(ab, skb, &vdev_start_resp) != 0) {
6081 		ath11k_warn(ab, "failed to extract vdev start resp");
6082 		return;
6083 	}
6084 
6085 	rcu_read_lock();
6086 	ar = ath11k_mac_get_ar_by_vdev_id(ab, vdev_start_resp.vdev_id);
6087 	if (!ar) {
6088 		ath11k_warn(ab, "invalid vdev id in vdev start resp ev %d",
6089 			    vdev_start_resp.vdev_id);
6090 		rcu_read_unlock();
6091 		return;
6092 	}
6093 
6094 	ar->last_wmi_vdev_start_status = 0;
6095 
6096 	status = vdev_start_resp.status;
6097 
6098 	if (WARN_ON_ONCE(status)) {
6099 		ath11k_warn(ab, "vdev start resp error status %d (%s)\n",
6100 			    status, ath11k_wmi_vdev_resp_print(status));
6101 		ar->last_wmi_vdev_start_status = status;
6102 	}
6103 
6104 	complete(&ar->vdev_setup_done);
6105 
6106 	rcu_read_unlock();
6107 
6108 	ath11k_dbg(ab, ATH11K_DBG_WMI, "vdev start resp for vdev id %d",
6109 		   vdev_start_resp.vdev_id);
6110 }
6111 
6112 static void ath11k_bcn_tx_status_event(struct ath11k_base *ab, struct sk_buff *skb)
6113 {
6114 	u32 vdev_id, tx_status;
6115 
6116 	if (ath11k_pull_bcn_tx_status_ev(ab, skb->data, skb->len,
6117 					 &vdev_id, &tx_status) != 0) {
6118 		ath11k_warn(ab, "failed to extract bcn tx status");
6119 		return;
6120 	}
6121 }
6122 
6123 static void ath11k_vdev_stopped_event(struct ath11k_base *ab, struct sk_buff *skb)
6124 {
6125 	struct ath11k *ar;
6126 	u32 vdev_id = 0;
6127 
6128 	if (ath11k_pull_vdev_stopped_param_tlv(ab, skb, &vdev_id) != 0) {
6129 		ath11k_warn(ab, "failed to extract vdev stopped event");
6130 		return;
6131 	}
6132 
6133 	rcu_read_lock();
6134 	ar = ath11k_mac_get_ar_by_vdev_id(ab, vdev_id);
6135 	if (!ar) {
6136 		ath11k_warn(ab, "invalid vdev id in vdev stopped ev %d",
6137 			    vdev_id);
6138 		rcu_read_unlock();
6139 		return;
6140 	}
6141 
6142 	complete(&ar->vdev_setup_done);
6143 
6144 	rcu_read_unlock();
6145 
6146 	ath11k_dbg(ab, ATH11K_DBG_WMI, "vdev stopped for vdev id %d", vdev_id);
6147 }
6148 
6149 static void ath11k_mgmt_rx_event(struct ath11k_base *ab, struct sk_buff *skb)
6150 {
6151 	struct mgmt_rx_event_params rx_ev = {0};
6152 	struct ath11k *ar;
6153 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
6154 	struct ieee80211_hdr *hdr;
6155 	u16 fc;
6156 	struct ieee80211_supported_band *sband;
6157 
6158 	if (ath11k_pull_mgmt_rx_params_tlv(ab, skb, &rx_ev) != 0) {
6159 		ath11k_warn(ab, "failed to extract mgmt rx event");
6160 		dev_kfree_skb(skb);
6161 		return;
6162 	}
6163 
6164 	memset(status, 0, sizeof(*status));
6165 
6166 	ath11k_dbg(ab, ATH11K_DBG_MGMT, "mgmt rx event status %08x\n",
6167 		   rx_ev.status);
6168 
6169 	rcu_read_lock();
6170 	ar = ath11k_mac_get_ar_by_pdev_id(ab, rx_ev.pdev_id);
6171 
6172 	if (!ar) {
6173 		ath11k_warn(ab, "invalid pdev_id %d in mgmt_rx_event\n",
6174 			    rx_ev.pdev_id);
6175 		dev_kfree_skb(skb);
6176 		goto exit;
6177 	}
6178 
6179 	if ((test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) ||
6180 	    (rx_ev.status & (WMI_RX_STATUS_ERR_DECRYPT |
6181 	    WMI_RX_STATUS_ERR_KEY_CACHE_MISS | WMI_RX_STATUS_ERR_CRC))) {
6182 		dev_kfree_skb(skb);
6183 		goto exit;
6184 	}
6185 
6186 	if (rx_ev.status & WMI_RX_STATUS_ERR_MIC)
6187 		status->flag |= RX_FLAG_MMIC_ERROR;
6188 
6189 	if (rx_ev.chan_freq >= ATH11K_MIN_6G_FREQ &&
6190 	    rx_ev.chan_freq <= ATH11K_MAX_6G_FREQ) {
6191 		status->band = NL80211_BAND_6GHZ;
6192 		status->freq = rx_ev.chan_freq;
6193 	} else if (rx_ev.channel >= 1 && rx_ev.channel <= 14) {
6194 		status->band = NL80211_BAND_2GHZ;
6195 	} else if (rx_ev.channel >= 36 && rx_ev.channel <= ATH11K_MAX_5G_CHAN) {
6196 		status->band = NL80211_BAND_5GHZ;
6197 	} else {
6198 		/* Shouldn't happen unless list of advertised channels to
6199 		 * mac80211 has been changed.
6200 		 */
6201 		WARN_ON_ONCE(1);
6202 		dev_kfree_skb(skb);
6203 		goto exit;
6204 	}
6205 
6206 	if (rx_ev.phy_mode == MODE_11B &&
6207 	    (status->band == NL80211_BAND_5GHZ || status->band == NL80211_BAND_6GHZ))
6208 		ath11k_dbg(ab, ATH11K_DBG_WMI,
6209 			   "wmi mgmt rx 11b (CCK) on 5/6GHz, band = %d\n", status->band);
6210 
6211 	sband = &ar->mac.sbands[status->band];
6212 
6213 	if (status->band != NL80211_BAND_6GHZ)
6214 		status->freq = ieee80211_channel_to_frequency(rx_ev.channel,
6215 							      status->band);
6216 
6217 	status->signal = rx_ev.snr + ATH11K_DEFAULT_NOISE_FLOOR;
6218 	status->rate_idx = ath11k_mac_bitrate_to_idx(sband, rx_ev.rate / 100);
6219 
6220 	hdr = (struct ieee80211_hdr *)skb->data;
6221 	fc = le16_to_cpu(hdr->frame_control);
6222 
6223 	/* Firmware is guaranteed to report all essential management frames via
6224 	 * WMI while it can deliver some extra via HTT. Since there can be
6225 	 * duplicates split the reporting wrt monitor/sniffing.
6226 	 */
6227 	status->flag |= RX_FLAG_SKIP_MONITOR;
6228 
6229 	/* In case of PMF, FW delivers decrypted frames with Protected Bit set.
6230 	 * Don't clear that. Also, FW delivers broadcast management frames
6231 	 * (ex: group privacy action frames in mesh) as encrypted payload.
6232 	 */
6233 	if (ieee80211_has_protected(hdr->frame_control) &&
6234 	    !is_multicast_ether_addr(ieee80211_get_DA(hdr))) {
6235 		status->flag |= RX_FLAG_DECRYPTED;
6236 
6237 		if (!ieee80211_is_robust_mgmt_frame(skb)) {
6238 			status->flag |= RX_FLAG_IV_STRIPPED |
6239 					RX_FLAG_MMIC_STRIPPED;
6240 			hdr->frame_control = __cpu_to_le16(fc &
6241 					     ~IEEE80211_FCTL_PROTECTED);
6242 		}
6243 	}
6244 
6245 	if (ieee80211_is_beacon(hdr->frame_control))
6246 		ath11k_mac_handle_beacon(ar, skb);
6247 
6248 	ath11k_dbg(ab, ATH11K_DBG_MGMT,
6249 		   "event mgmt rx skb %pK len %d ftype %02x stype %02x\n",
6250 		   skb, skb->len,
6251 		   fc & IEEE80211_FCTL_FTYPE, fc & IEEE80211_FCTL_STYPE);
6252 
6253 	ath11k_dbg(ab, ATH11K_DBG_MGMT,
6254 		   "event mgmt rx freq %d band %d snr %d, rate_idx %d\n",
6255 		   status->freq, status->band, status->signal,
6256 		   status->rate_idx);
6257 
6258 	ieee80211_rx_ni(ar->hw, skb);
6259 
6260 exit:
6261 	rcu_read_unlock();
6262 }
6263 
6264 static void ath11k_mgmt_tx_compl_event(struct ath11k_base *ab, struct sk_buff *skb)
6265 {
6266 	struct wmi_mgmt_tx_compl_event tx_compl_param = {0};
6267 	struct ath11k *ar;
6268 
6269 	if (ath11k_pull_mgmt_tx_compl_param_tlv(ab, skb, &tx_compl_param) != 0) {
6270 		ath11k_warn(ab, "failed to extract mgmt tx compl event");
6271 		return;
6272 	}
6273 
6274 	rcu_read_lock();
6275 	ar = ath11k_mac_get_ar_by_pdev_id(ab, tx_compl_param.pdev_id);
6276 	if (!ar) {
6277 		ath11k_warn(ab, "invalid pdev id %d in mgmt_tx_compl_event\n",
6278 			    tx_compl_param.pdev_id);
6279 		goto exit;
6280 	}
6281 
6282 	wmi_process_mgmt_tx_comp(ar, tx_compl_param.desc_id,
6283 				 tx_compl_param.status);
6284 
6285 	ath11k_dbg(ab, ATH11K_DBG_MGMT,
6286 		   "mgmt tx compl ev pdev_id %d, desc_id %d, status %d",
6287 		   tx_compl_param.pdev_id, tx_compl_param.desc_id,
6288 		   tx_compl_param.status);
6289 
6290 exit:
6291 	rcu_read_unlock();
6292 }
6293 
6294 static struct ath11k *ath11k_get_ar_on_scan_state(struct ath11k_base *ab,
6295 						  u32 vdev_id,
6296 						  enum ath11k_scan_state state)
6297 {
6298 	int i;
6299 	struct ath11k_pdev *pdev;
6300 	struct ath11k *ar;
6301 
6302 	for (i = 0; i < ab->num_radios; i++) {
6303 		pdev = rcu_dereference(ab->pdevs_active[i]);
6304 		if (pdev && pdev->ar) {
6305 			ar = pdev->ar;
6306 
6307 			spin_lock_bh(&ar->data_lock);
6308 			if (ar->scan.state == state &&
6309 			    ar->scan.vdev_id == vdev_id) {
6310 				spin_unlock_bh(&ar->data_lock);
6311 				return ar;
6312 			}
6313 			spin_unlock_bh(&ar->data_lock);
6314 		}
6315 	}
6316 	return NULL;
6317 }
6318 
6319 static void ath11k_scan_event(struct ath11k_base *ab, struct sk_buff *skb)
6320 {
6321 	struct ath11k *ar;
6322 	struct wmi_scan_event scan_ev = {0};
6323 
6324 	if (ath11k_pull_scan_ev(ab, skb, &scan_ev) != 0) {
6325 		ath11k_warn(ab, "failed to extract scan event");
6326 		return;
6327 	}
6328 
6329 	rcu_read_lock();
6330 
6331 	/* In case the scan was cancelled, ex. during interface teardown,
6332 	 * the interface will not be found in active interfaces.
6333 	 * Rather, in such scenarios, iterate over the active pdev's to
6334 	 * search 'ar' if the corresponding 'ar' scan is ABORTING and the
6335 	 * aborting scan's vdev id matches this event info.
6336 	 */
6337 	if (scan_ev.event_type == WMI_SCAN_EVENT_COMPLETED &&
6338 	    scan_ev.reason == WMI_SCAN_REASON_CANCELLED) {
6339 		ar = ath11k_get_ar_on_scan_state(ab, scan_ev.vdev_id,
6340 						 ATH11K_SCAN_ABORTING);
6341 		if (!ar)
6342 			ar = ath11k_get_ar_on_scan_state(ab, scan_ev.vdev_id,
6343 							 ATH11K_SCAN_RUNNING);
6344 	} else {
6345 		ar = ath11k_mac_get_ar_by_vdev_id(ab, scan_ev.vdev_id);
6346 	}
6347 
6348 	if (!ar) {
6349 		ath11k_warn(ab, "Received scan event for unknown vdev");
6350 		rcu_read_unlock();
6351 		return;
6352 	}
6353 
6354 	spin_lock_bh(&ar->data_lock);
6355 
6356 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6357 		   "scan event %s type %d reason %d freq %d req_id %d scan_id %d vdev_id %d state %s (%d)\n",
6358 		   ath11k_wmi_event_scan_type_str(scan_ev.event_type, scan_ev.reason),
6359 		   scan_ev.event_type, scan_ev.reason, scan_ev.channel_freq,
6360 		   scan_ev.scan_req_id, scan_ev.scan_id, scan_ev.vdev_id,
6361 		   ath11k_scan_state_str(ar->scan.state), ar->scan.state);
6362 
6363 	switch (scan_ev.event_type) {
6364 	case WMI_SCAN_EVENT_STARTED:
6365 		ath11k_wmi_event_scan_started(ar);
6366 		break;
6367 	case WMI_SCAN_EVENT_COMPLETED:
6368 		ath11k_wmi_event_scan_completed(ar);
6369 		break;
6370 	case WMI_SCAN_EVENT_BSS_CHANNEL:
6371 		ath11k_wmi_event_scan_bss_chan(ar);
6372 		break;
6373 	case WMI_SCAN_EVENT_FOREIGN_CHAN:
6374 		ath11k_wmi_event_scan_foreign_chan(ar, scan_ev.channel_freq);
6375 		break;
6376 	case WMI_SCAN_EVENT_START_FAILED:
6377 		ath11k_warn(ab, "received scan start failure event\n");
6378 		ath11k_wmi_event_scan_start_failed(ar);
6379 		break;
6380 	case WMI_SCAN_EVENT_DEQUEUED:
6381 		__ath11k_mac_scan_finish(ar);
6382 		break;
6383 	case WMI_SCAN_EVENT_PREEMPTED:
6384 	case WMI_SCAN_EVENT_RESTARTED:
6385 	case WMI_SCAN_EVENT_FOREIGN_CHAN_EXIT:
6386 	default:
6387 		break;
6388 	}
6389 
6390 	spin_unlock_bh(&ar->data_lock);
6391 
6392 	rcu_read_unlock();
6393 }
6394 
6395 static void ath11k_peer_sta_kickout_event(struct ath11k_base *ab, struct sk_buff *skb)
6396 {
6397 	struct wmi_peer_sta_kickout_arg arg = {};
6398 	struct ieee80211_sta *sta;
6399 	struct ath11k_peer *peer;
6400 	struct ath11k *ar;
6401 
6402 	if (ath11k_pull_peer_sta_kickout_ev(ab, skb, &arg) != 0) {
6403 		ath11k_warn(ab, "failed to extract peer sta kickout event");
6404 		return;
6405 	}
6406 
6407 	rcu_read_lock();
6408 
6409 	spin_lock_bh(&ab->base_lock);
6410 
6411 	peer = ath11k_peer_find_by_addr(ab, arg.mac_addr);
6412 
6413 	if (!peer) {
6414 		ath11k_warn(ab, "peer not found %pM\n",
6415 			    arg.mac_addr);
6416 		goto exit;
6417 	}
6418 
6419 	ar = ath11k_mac_get_ar_by_vdev_id(ab, peer->vdev_id);
6420 	if (!ar) {
6421 		ath11k_warn(ab, "invalid vdev id in peer sta kickout ev %d",
6422 			    peer->vdev_id);
6423 		goto exit;
6424 	}
6425 
6426 	sta = ieee80211_find_sta_by_ifaddr(ar->hw,
6427 					   arg.mac_addr, NULL);
6428 	if (!sta) {
6429 		ath11k_warn(ab, "Spurious quick kickout for STA %pM\n",
6430 			    arg.mac_addr);
6431 		goto exit;
6432 	}
6433 
6434 	ath11k_dbg(ab, ATH11K_DBG_WMI, "peer sta kickout event %pM",
6435 		   arg.mac_addr);
6436 
6437 	ieee80211_report_low_ack(sta, 10);
6438 
6439 exit:
6440 	spin_unlock_bh(&ab->base_lock);
6441 	rcu_read_unlock();
6442 }
6443 
6444 static void ath11k_roam_event(struct ath11k_base *ab, struct sk_buff *skb)
6445 {
6446 	struct wmi_roam_event roam_ev = {};
6447 	struct ath11k *ar;
6448 
6449 	if (ath11k_pull_roam_ev(ab, skb, &roam_ev) != 0) {
6450 		ath11k_warn(ab, "failed to extract roam event");
6451 		return;
6452 	}
6453 
6454 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6455 		   "wmi roam event vdev %u reason 0x%08x rssi %d\n",
6456 		   roam_ev.vdev_id, roam_ev.reason, roam_ev.rssi);
6457 
6458 	rcu_read_lock();
6459 	ar = ath11k_mac_get_ar_by_vdev_id(ab, roam_ev.vdev_id);
6460 	if (!ar) {
6461 		ath11k_warn(ab, "invalid vdev id in roam ev %d",
6462 			    roam_ev.vdev_id);
6463 		rcu_read_unlock();
6464 		return;
6465 	}
6466 
6467 	if (roam_ev.reason >= WMI_ROAM_REASON_MAX)
6468 		ath11k_warn(ab, "ignoring unknown roam event reason %d on vdev %i\n",
6469 			    roam_ev.reason, roam_ev.vdev_id);
6470 
6471 	switch (roam_ev.reason) {
6472 	case WMI_ROAM_REASON_BEACON_MISS:
6473 		ath11k_mac_handle_beacon_miss(ar, roam_ev.vdev_id);
6474 		break;
6475 	case WMI_ROAM_REASON_BETTER_AP:
6476 	case WMI_ROAM_REASON_LOW_RSSI:
6477 	case WMI_ROAM_REASON_SUITABLE_AP_FOUND:
6478 	case WMI_ROAM_REASON_HO_FAILED:
6479 		ath11k_warn(ab, "ignoring not implemented roam event reason %d on vdev %i\n",
6480 			    roam_ev.reason, roam_ev.vdev_id);
6481 		break;
6482 	}
6483 
6484 	rcu_read_unlock();
6485 }
6486 
6487 static void ath11k_chan_info_event(struct ath11k_base *ab, struct sk_buff *skb)
6488 {
6489 	struct wmi_chan_info_event ch_info_ev = {0};
6490 	struct ath11k *ar;
6491 	struct survey_info *survey;
6492 	int idx;
6493 	/* HW channel counters frequency value in hertz */
6494 	u32 cc_freq_hz = ab->cc_freq_hz;
6495 
6496 	if (ath11k_pull_chan_info_ev(ab, skb->data, skb->len, &ch_info_ev) != 0) {
6497 		ath11k_warn(ab, "failed to extract chan info event");
6498 		return;
6499 	}
6500 
6501 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6502 		   "chan info vdev_id %d err_code %d freq %d cmd_flags %d noise_floor %d rx_clear_count %d cycle_count %d mac_clk_mhz %d\n",
6503 		   ch_info_ev.vdev_id, ch_info_ev.err_code, ch_info_ev.freq,
6504 		   ch_info_ev.cmd_flags, ch_info_ev.noise_floor,
6505 		   ch_info_ev.rx_clear_count, ch_info_ev.cycle_count,
6506 		   ch_info_ev.mac_clk_mhz);
6507 
6508 	if (ch_info_ev.cmd_flags == WMI_CHAN_INFO_END_RESP) {
6509 		ath11k_dbg(ab, ATH11K_DBG_WMI, "chan info report completed\n");
6510 		return;
6511 	}
6512 
6513 	rcu_read_lock();
6514 	ar = ath11k_mac_get_ar_by_vdev_id(ab, ch_info_ev.vdev_id);
6515 	if (!ar) {
6516 		ath11k_warn(ab, "invalid vdev id in chan info ev %d",
6517 			    ch_info_ev.vdev_id);
6518 		rcu_read_unlock();
6519 		return;
6520 	}
6521 	spin_lock_bh(&ar->data_lock);
6522 
6523 	switch (ar->scan.state) {
6524 	case ATH11K_SCAN_IDLE:
6525 	case ATH11K_SCAN_STARTING:
6526 		ath11k_warn(ab, "received chan info event without a scan request, ignoring\n");
6527 		goto exit;
6528 	case ATH11K_SCAN_RUNNING:
6529 	case ATH11K_SCAN_ABORTING:
6530 		break;
6531 	}
6532 
6533 	idx = freq_to_idx(ar, ch_info_ev.freq);
6534 	if (idx >= ARRAY_SIZE(ar->survey)) {
6535 		ath11k_warn(ab, "chan info: invalid frequency %d (idx %d out of bounds)\n",
6536 			    ch_info_ev.freq, idx);
6537 		goto exit;
6538 	}
6539 
6540 	/* If FW provides MAC clock frequency in Mhz, overriding the initialized
6541 	 * HW channel counters frequency value
6542 	 */
6543 	if (ch_info_ev.mac_clk_mhz)
6544 		cc_freq_hz = (ch_info_ev.mac_clk_mhz * 1000);
6545 
6546 	if (ch_info_ev.cmd_flags == WMI_CHAN_INFO_START_RESP) {
6547 		survey = &ar->survey[idx];
6548 		memset(survey, 0, sizeof(*survey));
6549 		survey->noise = ch_info_ev.noise_floor;
6550 		survey->filled = SURVEY_INFO_NOISE_DBM | SURVEY_INFO_TIME |
6551 				 SURVEY_INFO_TIME_BUSY;
6552 		survey->time = div_u64(ch_info_ev.cycle_count, cc_freq_hz);
6553 		survey->time_busy = div_u64(ch_info_ev.rx_clear_count, cc_freq_hz);
6554 	}
6555 exit:
6556 	spin_unlock_bh(&ar->data_lock);
6557 	rcu_read_unlock();
6558 }
6559 
6560 static void
6561 ath11k_pdev_bss_chan_info_event(struct ath11k_base *ab, struct sk_buff *skb)
6562 {
6563 	struct wmi_pdev_bss_chan_info_event bss_ch_info_ev = {};
6564 	struct survey_info *survey;
6565 	struct ath11k *ar;
6566 	u32 cc_freq_hz = ab->cc_freq_hz;
6567 	u64 busy, total, tx, rx, rx_bss;
6568 	int idx;
6569 
6570 	if (ath11k_pull_pdev_bss_chan_info_ev(ab, skb, &bss_ch_info_ev) != 0) {
6571 		ath11k_warn(ab, "failed to extract pdev bss chan info event");
6572 		return;
6573 	}
6574 
6575 	busy = (u64)(bss_ch_info_ev.rx_clear_count_high) << 32 |
6576 			bss_ch_info_ev.rx_clear_count_low;
6577 
6578 	total = (u64)(bss_ch_info_ev.cycle_count_high) << 32 |
6579 			bss_ch_info_ev.cycle_count_low;
6580 
6581 	tx = (u64)(bss_ch_info_ev.tx_cycle_count_high) << 32 |
6582 			bss_ch_info_ev.tx_cycle_count_low;
6583 
6584 	rx = (u64)(bss_ch_info_ev.rx_cycle_count_high) << 32 |
6585 			bss_ch_info_ev.rx_cycle_count_low;
6586 
6587 	rx_bss = (u64)(bss_ch_info_ev.rx_bss_cycle_count_high) << 32 |
6588 			bss_ch_info_ev.rx_bss_cycle_count_low;
6589 
6590 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6591 		   "pdev bss chan info:\n pdev_id: %d freq: %d noise: %d cycle: busy %llu total %llu tx %llu rx %llu rx_bss %llu\n",
6592 		   bss_ch_info_ev.pdev_id, bss_ch_info_ev.freq,
6593 		   bss_ch_info_ev.noise_floor, busy, total,
6594 		   tx, rx, rx_bss);
6595 
6596 	rcu_read_lock();
6597 	ar = ath11k_mac_get_ar_by_pdev_id(ab, bss_ch_info_ev.pdev_id);
6598 
6599 	if (!ar) {
6600 		ath11k_warn(ab, "invalid pdev id %d in bss_chan_info event\n",
6601 			    bss_ch_info_ev.pdev_id);
6602 		rcu_read_unlock();
6603 		return;
6604 	}
6605 
6606 	spin_lock_bh(&ar->data_lock);
6607 	idx = freq_to_idx(ar, bss_ch_info_ev.freq);
6608 	if (idx >= ARRAY_SIZE(ar->survey)) {
6609 		ath11k_warn(ab, "bss chan info: invalid frequency %d (idx %d out of bounds)\n",
6610 			    bss_ch_info_ev.freq, idx);
6611 		goto exit;
6612 	}
6613 
6614 	survey = &ar->survey[idx];
6615 
6616 	survey->noise     = bss_ch_info_ev.noise_floor;
6617 	survey->time      = div_u64(total, cc_freq_hz);
6618 	survey->time_busy = div_u64(busy, cc_freq_hz);
6619 	survey->time_rx   = div_u64(rx_bss, cc_freq_hz);
6620 	survey->time_tx   = div_u64(tx, cc_freq_hz);
6621 	survey->filled   |= (SURVEY_INFO_NOISE_DBM |
6622 			     SURVEY_INFO_TIME |
6623 			     SURVEY_INFO_TIME_BUSY |
6624 			     SURVEY_INFO_TIME_RX |
6625 			     SURVEY_INFO_TIME_TX);
6626 exit:
6627 	spin_unlock_bh(&ar->data_lock);
6628 	complete(&ar->bss_survey_done);
6629 
6630 	rcu_read_unlock();
6631 }
6632 
6633 static void ath11k_vdev_install_key_compl_event(struct ath11k_base *ab,
6634 						struct sk_buff *skb)
6635 {
6636 	struct wmi_vdev_install_key_complete_arg install_key_compl = {0};
6637 	struct ath11k *ar;
6638 
6639 	if (ath11k_pull_vdev_install_key_compl_ev(ab, skb, &install_key_compl) != 0) {
6640 		ath11k_warn(ab, "failed to extract install key compl event");
6641 		return;
6642 	}
6643 
6644 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6645 		   "vdev install key ev idx %d flags %08x macaddr %pM status %d\n",
6646 		   install_key_compl.key_idx, install_key_compl.key_flags,
6647 		   install_key_compl.macaddr, install_key_compl.status);
6648 
6649 	rcu_read_lock();
6650 	ar = ath11k_mac_get_ar_by_vdev_id(ab, install_key_compl.vdev_id);
6651 	if (!ar) {
6652 		ath11k_warn(ab, "invalid vdev id in install key compl ev %d",
6653 			    install_key_compl.vdev_id);
6654 		rcu_read_unlock();
6655 		return;
6656 	}
6657 
6658 	ar->install_key_status = 0;
6659 
6660 	if (install_key_compl.status != WMI_VDEV_INSTALL_KEY_COMPL_STATUS_SUCCESS) {
6661 		ath11k_warn(ab, "install key failed for %pM status %d\n",
6662 			    install_key_compl.macaddr, install_key_compl.status);
6663 		ar->install_key_status = install_key_compl.status;
6664 	}
6665 
6666 	complete(&ar->install_key_done);
6667 	rcu_read_unlock();
6668 }
6669 
6670 static void ath11k_service_available_event(struct ath11k_base *ab, struct sk_buff *skb)
6671 {
6672 	const void **tb;
6673 	const struct wmi_service_available_event *ev;
6674 	int ret;
6675 	int i, j;
6676 
6677 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6678 	if (IS_ERR(tb)) {
6679 		ret = PTR_ERR(tb);
6680 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6681 		return;
6682 	}
6683 
6684 	ev = tb[WMI_TAG_SERVICE_AVAILABLE_EVENT];
6685 	if (!ev) {
6686 		ath11k_warn(ab, "failed to fetch svc available ev");
6687 		kfree(tb);
6688 		return;
6689 	}
6690 
6691 	/* TODO: Use wmi_service_segment_offset information to get the service
6692 	 * especially when more services are advertised in multiple sevice
6693 	 * available events.
6694 	 */
6695 	for (i = 0, j = WMI_MAX_SERVICE;
6696 	     i < WMI_SERVICE_SEGMENT_BM_SIZE32 && j < WMI_MAX_EXT_SERVICE;
6697 	     i++) {
6698 		do {
6699 			if (ev->wmi_service_segment_bitmap[i] &
6700 			    BIT(j % WMI_AVAIL_SERVICE_BITS_IN_SIZE32))
6701 				set_bit(j, ab->wmi_ab.svc_map);
6702 		} while (++j % WMI_AVAIL_SERVICE_BITS_IN_SIZE32);
6703 	}
6704 
6705 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6706 		   "wmi_ext_service_bitmap 0:0x%x, 1:0x%x, 2:0x%x, 3:0x%x",
6707 		   ev->wmi_service_segment_bitmap[0], ev->wmi_service_segment_bitmap[1],
6708 		   ev->wmi_service_segment_bitmap[2], ev->wmi_service_segment_bitmap[3]);
6709 
6710 	kfree(tb);
6711 }
6712 
6713 static void ath11k_peer_assoc_conf_event(struct ath11k_base *ab, struct sk_buff *skb)
6714 {
6715 	struct wmi_peer_assoc_conf_arg peer_assoc_conf = {0};
6716 	struct ath11k *ar;
6717 
6718 	if (ath11k_pull_peer_assoc_conf_ev(ab, skb, &peer_assoc_conf) != 0) {
6719 		ath11k_warn(ab, "failed to extract peer assoc conf event");
6720 		return;
6721 	}
6722 
6723 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6724 		   "peer assoc conf ev vdev id %d macaddr %pM\n",
6725 		   peer_assoc_conf.vdev_id, peer_assoc_conf.macaddr);
6726 
6727 	rcu_read_lock();
6728 	ar = ath11k_mac_get_ar_by_vdev_id(ab, peer_assoc_conf.vdev_id);
6729 
6730 	if (!ar) {
6731 		ath11k_warn(ab, "invalid vdev id in peer assoc conf ev %d",
6732 			    peer_assoc_conf.vdev_id);
6733 		rcu_read_unlock();
6734 		return;
6735 	}
6736 
6737 	complete(&ar->peer_assoc_done);
6738 	rcu_read_unlock();
6739 }
6740 
6741 static void ath11k_update_stats_event(struct ath11k_base *ab, struct sk_buff *skb)
6742 {
6743 	ath11k_debugfs_fw_stats_process(ab, skb);
6744 }
6745 
6746 /* PDEV_CTL_FAILSAFE_CHECK_EVENT is received from FW when the frequency scanned
6747  * is not part of BDF CTL(Conformance test limits) table entries.
6748  */
6749 static void ath11k_pdev_ctl_failsafe_check_event(struct ath11k_base *ab,
6750 						 struct sk_buff *skb)
6751 {
6752 	const void **tb;
6753 	const struct wmi_pdev_ctl_failsafe_chk_event *ev;
6754 	int ret;
6755 
6756 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6757 	if (IS_ERR(tb)) {
6758 		ret = PTR_ERR(tb);
6759 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6760 		return;
6761 	}
6762 
6763 	ev = tb[WMI_TAG_PDEV_CTL_FAILSAFE_CHECK_EVENT];
6764 	if (!ev) {
6765 		ath11k_warn(ab, "failed to fetch pdev ctl failsafe check ev");
6766 		kfree(tb);
6767 		return;
6768 	}
6769 
6770 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6771 		   "pdev ctl failsafe check ev status %d\n",
6772 		   ev->ctl_failsafe_status);
6773 
6774 	/* If ctl_failsafe_status is set to 1 FW will max out the Transmit power
6775 	 * to 10 dBm else the CTL power entry in the BDF would be picked up.
6776 	 */
6777 	if (ev->ctl_failsafe_status != 0)
6778 		ath11k_warn(ab, "pdev ctl failsafe failure status %d",
6779 			    ev->ctl_failsafe_status);
6780 
6781 	kfree(tb);
6782 }
6783 
6784 static void
6785 ath11k_wmi_process_csa_switch_count_event(struct ath11k_base *ab,
6786 					  const struct wmi_pdev_csa_switch_ev *ev,
6787 					  const u32 *vdev_ids)
6788 {
6789 	int i;
6790 	struct ath11k_vif *arvif;
6791 
6792 	/* Finish CSA once the switch count becomes NULL */
6793 	if (ev->current_switch_count)
6794 		return;
6795 
6796 	rcu_read_lock();
6797 	for (i = 0; i < ev->num_vdevs; i++) {
6798 		arvif = ath11k_mac_get_arvif_by_vdev_id(ab, vdev_ids[i]);
6799 
6800 		if (!arvif) {
6801 			ath11k_warn(ab, "Recvd csa status for unknown vdev %d",
6802 				    vdev_ids[i]);
6803 			continue;
6804 		}
6805 
6806 		if (arvif->is_up && arvif->vif->csa_active)
6807 			ieee80211_csa_finish(arvif->vif);
6808 	}
6809 	rcu_read_unlock();
6810 }
6811 
6812 static void
6813 ath11k_wmi_pdev_csa_switch_count_status_event(struct ath11k_base *ab,
6814 					      struct sk_buff *skb)
6815 {
6816 	const void **tb;
6817 	const struct wmi_pdev_csa_switch_ev *ev;
6818 	const u32 *vdev_ids;
6819 	int ret;
6820 
6821 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6822 	if (IS_ERR(tb)) {
6823 		ret = PTR_ERR(tb);
6824 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6825 		return;
6826 	}
6827 
6828 	ev = tb[WMI_TAG_PDEV_CSA_SWITCH_COUNT_STATUS_EVENT];
6829 	vdev_ids = tb[WMI_TAG_ARRAY_UINT32];
6830 
6831 	if (!ev || !vdev_ids) {
6832 		ath11k_warn(ab, "failed to fetch pdev csa switch count ev");
6833 		kfree(tb);
6834 		return;
6835 	}
6836 
6837 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6838 		   "pdev csa switch count %d for pdev %d, num_vdevs %d",
6839 		   ev->current_switch_count, ev->pdev_id,
6840 		   ev->num_vdevs);
6841 
6842 	ath11k_wmi_process_csa_switch_count_event(ab, ev, vdev_ids);
6843 
6844 	kfree(tb);
6845 }
6846 
6847 static void
6848 ath11k_wmi_pdev_dfs_radar_detected_event(struct ath11k_base *ab, struct sk_buff *skb)
6849 {
6850 	const void **tb;
6851 	const struct wmi_pdev_radar_ev *ev;
6852 	struct ath11k *ar;
6853 	int ret;
6854 
6855 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6856 	if (IS_ERR(tb)) {
6857 		ret = PTR_ERR(tb);
6858 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6859 		return;
6860 	}
6861 
6862 	ev = tb[WMI_TAG_PDEV_DFS_RADAR_DETECTION_EVENT];
6863 
6864 	if (!ev) {
6865 		ath11k_warn(ab, "failed to fetch pdev dfs radar detected ev");
6866 		kfree(tb);
6867 		return;
6868 	}
6869 
6870 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6871 		   "pdev dfs radar detected on pdev %d, detection mode %d, chan freq %d, chan_width %d, detector id %d, seg id %d, timestamp %d, chirp %d, freq offset %d, sidx %d",
6872 		   ev->pdev_id, ev->detection_mode, ev->chan_freq, ev->chan_width,
6873 		   ev->detector_id, ev->segment_id, ev->timestamp, ev->is_chirp,
6874 		   ev->freq_offset, ev->sidx);
6875 
6876 	ar = ath11k_mac_get_ar_by_pdev_id(ab, ev->pdev_id);
6877 
6878 	if (!ar) {
6879 		ath11k_warn(ab, "radar detected in invalid pdev %d\n",
6880 			    ev->pdev_id);
6881 		goto exit;
6882 	}
6883 
6884 	ath11k_dbg(ar->ab, ATH11K_DBG_REG, "DFS Radar Detected in pdev %d\n",
6885 		   ev->pdev_id);
6886 
6887 	if (ar->dfs_block_radar_events)
6888 		ath11k_info(ab, "DFS Radar detected, but ignored as requested\n");
6889 	else
6890 		ieee80211_radar_detected(ar->hw);
6891 
6892 exit:
6893 	kfree(tb);
6894 }
6895 
6896 static void
6897 ath11k_wmi_pdev_temperature_event(struct ath11k_base *ab,
6898 				  struct sk_buff *skb)
6899 {
6900 	struct ath11k *ar;
6901 	const void **tb;
6902 	const struct wmi_pdev_temperature_event *ev;
6903 	int ret;
6904 
6905 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6906 	if (IS_ERR(tb)) {
6907 		ret = PTR_ERR(tb);
6908 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6909 		return;
6910 	}
6911 
6912 	ev = tb[WMI_TAG_PDEV_TEMPERATURE_EVENT];
6913 	if (!ev) {
6914 		ath11k_warn(ab, "failed to fetch pdev temp ev");
6915 		kfree(tb);
6916 		return;
6917 	}
6918 
6919 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6920 		   "pdev temperature ev temp %d pdev_id %d\n", ev->temp, ev->pdev_id);
6921 
6922 	ar = ath11k_mac_get_ar_by_pdev_id(ab, ev->pdev_id);
6923 	if (!ar) {
6924 		ath11k_warn(ab, "invalid pdev id in pdev temperature ev %d", ev->pdev_id);
6925 		kfree(tb);
6926 		return;
6927 	}
6928 
6929 	ath11k_thermal_event_temperature(ar, ev->temp);
6930 
6931 	kfree(tb);
6932 }
6933 
6934 static void ath11k_fils_discovery_event(struct ath11k_base *ab,
6935 					struct sk_buff *skb)
6936 {
6937 	const void **tb;
6938 	const struct wmi_fils_discovery_event *ev;
6939 	int ret;
6940 
6941 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6942 	if (IS_ERR(tb)) {
6943 		ret = PTR_ERR(tb);
6944 		ath11k_warn(ab,
6945 			    "failed to parse FILS discovery event tlv %d\n",
6946 			    ret);
6947 		return;
6948 	}
6949 
6950 	ev = tb[WMI_TAG_HOST_SWFDA_EVENT];
6951 	if (!ev) {
6952 		ath11k_warn(ab, "failed to fetch FILS discovery event\n");
6953 		kfree(tb);
6954 		return;
6955 	}
6956 
6957 	ath11k_warn(ab,
6958 		    "FILS discovery frame expected from host for vdev_id: %u, transmission scheduled at %u, next TBTT: %u\n",
6959 		    ev->vdev_id, ev->fils_tt, ev->tbtt);
6960 
6961 	kfree(tb);
6962 }
6963 
6964 static void ath11k_probe_resp_tx_status_event(struct ath11k_base *ab,
6965 					      struct sk_buff *skb)
6966 {
6967 	const void **tb;
6968 	const struct wmi_probe_resp_tx_status_event *ev;
6969 	int ret;
6970 
6971 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6972 	if (IS_ERR(tb)) {
6973 		ret = PTR_ERR(tb);
6974 		ath11k_warn(ab,
6975 			    "failed to parse probe response transmission status event tlv: %d\n",
6976 			    ret);
6977 		return;
6978 	}
6979 
6980 	ev = tb[WMI_TAG_OFFLOAD_PRB_RSP_TX_STATUS_EVENT];
6981 	if (!ev) {
6982 		ath11k_warn(ab,
6983 			    "failed to fetch probe response transmission status event");
6984 		kfree(tb);
6985 		return;
6986 	}
6987 
6988 	if (ev->tx_status)
6989 		ath11k_warn(ab,
6990 			    "Probe response transmission failed for vdev_id %u, status %u\n",
6991 			    ev->vdev_id, ev->tx_status);
6992 
6993 	kfree(tb);
6994 }
6995 
6996 static int ath11k_wmi_tlv_wow_wakeup_host_parse(struct ath11k_base *ab,
6997 						u16 tag, u16 len,
6998 						const void *ptr, void *data)
6999 {
7000 	struct wmi_wow_ev_arg *ev = data;
7001 	const char *wow_pg_fault;
7002 	int wow_pg_len;
7003 
7004 	switch (tag) {
7005 	case WMI_TAG_WOW_EVENT_INFO:
7006 		memcpy(ev, ptr, sizeof(*ev));
7007 		ath11k_dbg(ab, ATH11K_DBG_WMI, "wow wakeup host reason %d %s\n",
7008 			   ev->wake_reason, wow_reason(ev->wake_reason));
7009 		break;
7010 
7011 	case WMI_TAG_ARRAY_BYTE:
7012 		if (ev && ev->wake_reason == WOW_REASON_PAGE_FAULT) {
7013 			wow_pg_fault = ptr;
7014 			/* the first 4 bytes are length */
7015 			wow_pg_len = *(int *)wow_pg_fault;
7016 			wow_pg_fault += sizeof(int);
7017 			ath11k_dbg(ab, ATH11K_DBG_WMI, "wow data_len = %d\n",
7018 				   wow_pg_len);
7019 			ath11k_dbg_dump(ab, ATH11K_DBG_WMI,
7020 					"wow_event_info_type packet present",
7021 					"wow_pg_fault ",
7022 					wow_pg_fault,
7023 					wow_pg_len);
7024 		}
7025 		break;
7026 	default:
7027 		break;
7028 	}
7029 
7030 	return 0;
7031 }
7032 
7033 static void ath11k_wmi_event_wow_wakeup_host(struct ath11k_base *ab, struct sk_buff *skb)
7034 {
7035 	struct wmi_wow_ev_arg ev = { };
7036 	int ret;
7037 
7038 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
7039 				  ath11k_wmi_tlv_wow_wakeup_host_parse,
7040 				  &ev);
7041 	if (ret) {
7042 		ath11k_warn(ab, "failed to parse wmi wow tlv: %d\n", ret);
7043 		return;
7044 	}
7045 
7046 	complete(&ab->wow.wakeup_completed);
7047 }
7048 
7049 static void ath11k_wmi_tlv_op_rx(struct ath11k_base *ab, struct sk_buff *skb)
7050 {
7051 	struct wmi_cmd_hdr *cmd_hdr;
7052 	enum wmi_tlv_event_id id;
7053 
7054 	cmd_hdr = (struct wmi_cmd_hdr *)skb->data;
7055 	id = FIELD_GET(WMI_CMD_HDR_CMD_ID, (cmd_hdr->cmd_id));
7056 
7057 	if (skb_pull(skb, sizeof(struct wmi_cmd_hdr)) == NULL)
7058 		goto out;
7059 
7060 	switch (id) {
7061 		/* Process all the WMI events here */
7062 	case WMI_SERVICE_READY_EVENTID:
7063 		ath11k_service_ready_event(ab, skb);
7064 		break;
7065 	case WMI_SERVICE_READY_EXT_EVENTID:
7066 		ath11k_service_ready_ext_event(ab, skb);
7067 		break;
7068 	case WMI_SERVICE_READY_EXT2_EVENTID:
7069 		ath11k_service_ready_ext2_event(ab, skb);
7070 		break;
7071 	case WMI_REG_CHAN_LIST_CC_EVENTID:
7072 		ath11k_reg_chan_list_event(ab, skb);
7073 		break;
7074 	case WMI_READY_EVENTID:
7075 		ath11k_ready_event(ab, skb);
7076 		break;
7077 	case WMI_PEER_DELETE_RESP_EVENTID:
7078 		ath11k_peer_delete_resp_event(ab, skb);
7079 		break;
7080 	case WMI_VDEV_START_RESP_EVENTID:
7081 		ath11k_vdev_start_resp_event(ab, skb);
7082 		break;
7083 	case WMI_OFFLOAD_BCN_TX_STATUS_EVENTID:
7084 		ath11k_bcn_tx_status_event(ab, skb);
7085 		break;
7086 	case WMI_VDEV_STOPPED_EVENTID:
7087 		ath11k_vdev_stopped_event(ab, skb);
7088 		break;
7089 	case WMI_MGMT_RX_EVENTID:
7090 		ath11k_mgmt_rx_event(ab, skb);
7091 		/* mgmt_rx_event() owns the skb now! */
7092 		return;
7093 	case WMI_MGMT_TX_COMPLETION_EVENTID:
7094 		ath11k_mgmt_tx_compl_event(ab, skb);
7095 		break;
7096 	case WMI_SCAN_EVENTID:
7097 		ath11k_scan_event(ab, skb);
7098 		break;
7099 	case WMI_PEER_STA_KICKOUT_EVENTID:
7100 		ath11k_peer_sta_kickout_event(ab, skb);
7101 		break;
7102 	case WMI_ROAM_EVENTID:
7103 		ath11k_roam_event(ab, skb);
7104 		break;
7105 	case WMI_CHAN_INFO_EVENTID:
7106 		ath11k_chan_info_event(ab, skb);
7107 		break;
7108 	case WMI_PDEV_BSS_CHAN_INFO_EVENTID:
7109 		ath11k_pdev_bss_chan_info_event(ab, skb);
7110 		break;
7111 	case WMI_VDEV_INSTALL_KEY_COMPLETE_EVENTID:
7112 		ath11k_vdev_install_key_compl_event(ab, skb);
7113 		break;
7114 	case WMI_SERVICE_AVAILABLE_EVENTID:
7115 		ath11k_service_available_event(ab, skb);
7116 		break;
7117 	case WMI_PEER_ASSOC_CONF_EVENTID:
7118 		ath11k_peer_assoc_conf_event(ab, skb);
7119 		break;
7120 	case WMI_UPDATE_STATS_EVENTID:
7121 		ath11k_update_stats_event(ab, skb);
7122 		break;
7123 	case WMI_PDEV_CTL_FAILSAFE_CHECK_EVENTID:
7124 		ath11k_pdev_ctl_failsafe_check_event(ab, skb);
7125 		break;
7126 	case WMI_PDEV_CSA_SWITCH_COUNT_STATUS_EVENTID:
7127 		ath11k_wmi_pdev_csa_switch_count_status_event(ab, skb);
7128 		break;
7129 	case WMI_PDEV_TEMPERATURE_EVENTID:
7130 		ath11k_wmi_pdev_temperature_event(ab, skb);
7131 		break;
7132 	case WMI_PDEV_DMA_RING_BUF_RELEASE_EVENTID:
7133 		ath11k_wmi_pdev_dma_ring_buf_release_event(ab, skb);
7134 		break;
7135 	case WMI_HOST_FILS_DISCOVERY_EVENTID:
7136 		ath11k_fils_discovery_event(ab, skb);
7137 		break;
7138 	case WMI_OFFLOAD_PROB_RESP_TX_STATUS_EVENTID:
7139 		ath11k_probe_resp_tx_status_event(ab, skb);
7140 		break;
7141 	/* add Unsupported events here */
7142 	case WMI_TBTTOFFSET_EXT_UPDATE_EVENTID:
7143 	case WMI_PEER_OPER_MODE_CHANGE_EVENTID:
7144 	case WMI_TWT_ENABLE_EVENTID:
7145 	case WMI_TWT_DISABLE_EVENTID:
7146 	case WMI_PDEV_DMA_RING_CFG_RSP_EVENTID:
7147 	case WMI_PEER_CREATE_CONF_EVENTID:
7148 		ath11k_dbg(ab, ATH11K_DBG_WMI,
7149 			   "ignoring unsupported event 0x%x\n", id);
7150 		break;
7151 	case WMI_PDEV_DFS_RADAR_DETECTION_EVENTID:
7152 		ath11k_wmi_pdev_dfs_radar_detected_event(ab, skb);
7153 		break;
7154 	case WMI_VDEV_DELETE_RESP_EVENTID:
7155 		ath11k_vdev_delete_resp_event(ab, skb);
7156 		break;
7157 	case WMI_WOW_WAKEUP_HOST_EVENTID:
7158 		ath11k_wmi_event_wow_wakeup_host(ab, skb);
7159 		break;
7160 	/* TODO: Add remaining events */
7161 	default:
7162 		ath11k_dbg(ab, ATH11K_DBG_WMI, "Unknown eventid: 0x%x\n", id);
7163 		break;
7164 	}
7165 
7166 out:
7167 	dev_kfree_skb(skb);
7168 }
7169 
7170 static int ath11k_connect_pdev_htc_service(struct ath11k_base *ab,
7171 					   u32 pdev_idx)
7172 {
7173 	int status;
7174 	u32 svc_id[] = { ATH11K_HTC_SVC_ID_WMI_CONTROL,
7175 			 ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC1,
7176 			 ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC2 };
7177 
7178 	struct ath11k_htc_svc_conn_req conn_req;
7179 	struct ath11k_htc_svc_conn_resp conn_resp;
7180 
7181 	memset(&conn_req, 0, sizeof(conn_req));
7182 	memset(&conn_resp, 0, sizeof(conn_resp));
7183 
7184 	/* these fields are the same for all service endpoints */
7185 	conn_req.ep_ops.ep_tx_complete = ath11k_wmi_htc_tx_complete;
7186 	conn_req.ep_ops.ep_rx_complete = ath11k_wmi_tlv_op_rx;
7187 	conn_req.ep_ops.ep_tx_credits = ath11k_wmi_op_ep_tx_credits;
7188 
7189 	/* connect to control service */
7190 	conn_req.service_id = svc_id[pdev_idx];
7191 
7192 	status = ath11k_htc_connect_service(&ab->htc, &conn_req, &conn_resp);
7193 	if (status) {
7194 		ath11k_warn(ab, "failed to connect to WMI CONTROL service status: %d\n",
7195 			    status);
7196 		return status;
7197 	}
7198 
7199 	ab->wmi_ab.wmi_endpoint_id[pdev_idx] = conn_resp.eid;
7200 	ab->wmi_ab.wmi[pdev_idx].eid = conn_resp.eid;
7201 	ab->wmi_ab.max_msg_len[pdev_idx] = conn_resp.max_msg_len;
7202 
7203 	return 0;
7204 }
7205 
7206 static int
7207 ath11k_wmi_send_unit_test_cmd(struct ath11k *ar,
7208 			      struct wmi_unit_test_cmd ut_cmd,
7209 			      u32 *test_args)
7210 {
7211 	struct ath11k_pdev_wmi *wmi = ar->wmi;
7212 	struct wmi_unit_test_cmd *cmd;
7213 	struct sk_buff *skb;
7214 	struct wmi_tlv *tlv;
7215 	void *ptr;
7216 	u32 *ut_cmd_args;
7217 	int buf_len, arg_len;
7218 	int ret;
7219 	int i;
7220 
7221 	arg_len = sizeof(u32) * ut_cmd.num_args;
7222 	buf_len = sizeof(ut_cmd) + arg_len + TLV_HDR_SIZE;
7223 
7224 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, buf_len);
7225 	if (!skb)
7226 		return -ENOMEM;
7227 
7228 	cmd = (struct wmi_unit_test_cmd *)skb->data;
7229 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_UNIT_TEST_CMD) |
7230 			  FIELD_PREP(WMI_TLV_LEN, sizeof(ut_cmd) - TLV_HDR_SIZE);
7231 
7232 	cmd->vdev_id = ut_cmd.vdev_id;
7233 	cmd->module_id = ut_cmd.module_id;
7234 	cmd->num_args = ut_cmd.num_args;
7235 	cmd->diag_token = ut_cmd.diag_token;
7236 
7237 	ptr = skb->data + sizeof(ut_cmd);
7238 
7239 	tlv = ptr;
7240 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_UINT32) |
7241 		      FIELD_PREP(WMI_TLV_LEN, arg_len);
7242 
7243 	ptr += TLV_HDR_SIZE;
7244 
7245 	ut_cmd_args = ptr;
7246 	for (i = 0; i < ut_cmd.num_args; i++)
7247 		ut_cmd_args[i] = test_args[i];
7248 
7249 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_UNIT_TEST_CMDID);
7250 
7251 	if (ret) {
7252 		ath11k_warn(ar->ab, "failed to send WMI_UNIT_TEST CMD :%d\n",
7253 			    ret);
7254 		dev_kfree_skb(skb);
7255 	}
7256 
7257 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
7258 		   "WMI unit test : module %d vdev %d n_args %d token %d\n",
7259 		   cmd->module_id, cmd->vdev_id, cmd->num_args,
7260 		   cmd->diag_token);
7261 
7262 	return ret;
7263 }
7264 
7265 int ath11k_wmi_simulate_radar(struct ath11k *ar)
7266 {
7267 	struct ath11k_vif *arvif;
7268 	u32 dfs_args[DFS_MAX_TEST_ARGS];
7269 	struct wmi_unit_test_cmd wmi_ut;
7270 	bool arvif_found = false;
7271 
7272 	list_for_each_entry(arvif, &ar->arvifs, list) {
7273 		if (arvif->is_started && arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7274 			arvif_found = true;
7275 			break;
7276 		}
7277 	}
7278 
7279 	if (!arvif_found)
7280 		return -EINVAL;
7281 
7282 	dfs_args[DFS_TEST_CMDID] = 0;
7283 	dfs_args[DFS_TEST_PDEV_ID] = ar->pdev->pdev_id;
7284 	/* Currently we could pass segment_id(b0 - b1), chirp(b2)
7285 	 * freq offset (b3 - b10) to unit test. For simulation
7286 	 * purpose this can be set to 0 which is valid.
7287 	 */
7288 	dfs_args[DFS_TEST_RADAR_PARAM] = 0;
7289 
7290 	wmi_ut.vdev_id = arvif->vdev_id;
7291 	wmi_ut.module_id = DFS_UNIT_TEST_MODULE;
7292 	wmi_ut.num_args = DFS_MAX_TEST_ARGS;
7293 	wmi_ut.diag_token = DFS_UNIT_TEST_TOKEN;
7294 
7295 	ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Triggering Radar Simulation\n");
7296 
7297 	return ath11k_wmi_send_unit_test_cmd(ar, wmi_ut, dfs_args);
7298 }
7299 
7300 int ath11k_wmi_connect(struct ath11k_base *ab)
7301 {
7302 	u32 i;
7303 	u8 wmi_ep_count;
7304 
7305 	wmi_ep_count = ab->htc.wmi_ep_count;
7306 	if (wmi_ep_count > ab->hw_params.max_radios)
7307 		return -1;
7308 
7309 	for (i = 0; i < wmi_ep_count; i++)
7310 		ath11k_connect_pdev_htc_service(ab, i);
7311 
7312 	return 0;
7313 }
7314 
7315 static void ath11k_wmi_pdev_detach(struct ath11k_base *ab, u8 pdev_id)
7316 {
7317 	if (WARN_ON(pdev_id >= MAX_RADIOS))
7318 		return;
7319 
7320 	/* TODO: Deinit any pdev specific wmi resource */
7321 }
7322 
7323 int ath11k_wmi_pdev_attach(struct ath11k_base *ab,
7324 			   u8 pdev_id)
7325 {
7326 	struct ath11k_pdev_wmi *wmi_handle;
7327 
7328 	if (pdev_id >= ab->hw_params.max_radios)
7329 		return -EINVAL;
7330 
7331 	wmi_handle = &ab->wmi_ab.wmi[pdev_id];
7332 
7333 	wmi_handle->wmi_ab = &ab->wmi_ab;
7334 
7335 	ab->wmi_ab.ab = ab;
7336 	/* TODO: Init remaining resource specific to pdev */
7337 
7338 	return 0;
7339 }
7340 
7341 int ath11k_wmi_attach(struct ath11k_base *ab)
7342 {
7343 	int ret;
7344 
7345 	ret = ath11k_wmi_pdev_attach(ab, 0);
7346 	if (ret)
7347 		return ret;
7348 
7349 	ab->wmi_ab.ab = ab;
7350 	ab->wmi_ab.preferred_hw_mode = WMI_HOST_HW_MODE_MAX;
7351 
7352 	/* It's overwritten when service_ext_ready is handled */
7353 	if (ab->hw_params.single_pdev_only)
7354 		ab->wmi_ab.preferred_hw_mode = WMI_HOST_HW_MODE_SINGLE;
7355 
7356 	/* TODO: Init remaining wmi soc resources required */
7357 	init_completion(&ab->wmi_ab.service_ready);
7358 	init_completion(&ab->wmi_ab.unified_ready);
7359 
7360 	return 0;
7361 }
7362 
7363 void ath11k_wmi_detach(struct ath11k_base *ab)
7364 {
7365 	int i;
7366 
7367 	/* TODO: Deinit wmi resource specific to SOC as required */
7368 
7369 	for (i = 0; i < ab->htc.wmi_ep_count; i++)
7370 		ath11k_wmi_pdev_detach(ab, i);
7371 
7372 	ath11k_wmi_free_dbring_caps(ab);
7373 }
7374 
7375 int ath11k_wmi_wow_host_wakeup_ind(struct ath11k *ar)
7376 {
7377 	struct wmi_wow_host_wakeup_ind *cmd;
7378 	struct sk_buff *skb;
7379 	size_t len;
7380 
7381 	len = sizeof(*cmd);
7382 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
7383 	if (!skb)
7384 		return -ENOMEM;
7385 
7386 	cmd = (struct wmi_wow_host_wakeup_ind *)skb->data;
7387 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
7388 				     WMI_TAG_WOW_HOSTWAKEUP_FROM_SLEEP_CMD) |
7389 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
7390 
7391 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "wmi tlv wow host wakeup ind\n");
7392 
7393 	return ath11k_wmi_cmd_send(ar->wmi, skb, WMI_WOW_HOSTWAKEUP_FROM_SLEEP_CMDID);
7394 }
7395 
7396 int ath11k_wmi_wow_enable(struct ath11k *ar)
7397 {
7398 	struct wmi_wow_enable_cmd *cmd;
7399 	struct sk_buff *skb;
7400 	int len;
7401 
7402 	len = sizeof(*cmd);
7403 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
7404 	if (!skb)
7405 		return -ENOMEM;
7406 
7407 	cmd = (struct wmi_wow_enable_cmd *)skb->data;
7408 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_WOW_ENABLE_CMD) |
7409 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
7410 
7411 	cmd->enable = 1;
7412 	cmd->pause_iface_config = WOW_IFACE_PAUSE_ENABLED;
7413 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "wmi tlv wow enable\n");
7414 
7415 	return ath11k_wmi_cmd_send(ar->wmi, skb, WMI_WOW_ENABLE_CMDID);
7416 }
7417