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