xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/wmi.c (revision 867e6d38)
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 size_t ath11k_wmi_fw_stats_num_vdevs(struct list_head *head)
5421 {
5422 	struct ath11k_fw_stats_vdev *i;
5423 	size_t num = 0;
5424 
5425 	list_for_each_entry(i, head, list)
5426 		++num;
5427 
5428 	return num;
5429 }
5430 
5431 static size_t ath11k_wmi_fw_stats_num_bcn(struct list_head *head)
5432 {
5433 	struct ath11k_fw_stats_bcn *i;
5434 	size_t num = 0;
5435 
5436 	list_for_each_entry(i, head, list)
5437 		++num;
5438 
5439 	return num;
5440 }
5441 
5442 static void
5443 ath11k_wmi_fw_pdev_base_stats_fill(const struct ath11k_fw_stats_pdev *pdev,
5444 				   char *buf, u32 *length)
5445 {
5446 	u32 len = *length;
5447 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5448 
5449 	len += scnprintf(buf + len, buf_len - len, "\n");
5450 	len += scnprintf(buf + len, buf_len - len, "%30s\n",
5451 			"ath11k PDEV stats");
5452 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5453 			"=================");
5454 
5455 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5456 			"Channel noise floor", pdev->ch_noise_floor);
5457 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5458 			"Channel TX power", pdev->chan_tx_power);
5459 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5460 			"TX frame count", pdev->tx_frame_count);
5461 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5462 			"RX frame count", pdev->rx_frame_count);
5463 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5464 			"RX clear count", pdev->rx_clear_count);
5465 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5466 			"Cycle count", pdev->cycle_count);
5467 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5468 			"PHY error count", pdev->phy_err_count);
5469 
5470 	*length = len;
5471 }
5472 
5473 static void
5474 ath11k_wmi_fw_pdev_tx_stats_fill(const struct ath11k_fw_stats_pdev *pdev,
5475 				 char *buf, u32 *length)
5476 {
5477 	u32 len = *length;
5478 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5479 
5480 	len += scnprintf(buf + len, buf_len - len, "\n%30s\n",
5481 			 "ath11k PDEV TX stats");
5482 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5483 			 "====================");
5484 
5485 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5486 			 "HTT cookies queued", pdev->comp_queued);
5487 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5488 			 "HTT cookies disp.", pdev->comp_delivered);
5489 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5490 			 "MSDU queued", pdev->msdu_enqued);
5491 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5492 			 "MPDU queued", pdev->mpdu_enqued);
5493 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5494 			 "MSDUs dropped", pdev->wmm_drop);
5495 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5496 			 "Local enqued", pdev->local_enqued);
5497 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5498 			 "Local freed", pdev->local_freed);
5499 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5500 			 "HW queued", pdev->hw_queued);
5501 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5502 			 "PPDUs reaped", pdev->hw_reaped);
5503 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5504 			 "Num underruns", pdev->underrun);
5505 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5506 			 "PPDUs cleaned", pdev->tx_abort);
5507 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5508 			 "MPDUs requed", pdev->mpdus_requed);
5509 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5510 			 "Excessive retries", pdev->tx_ko);
5511 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5512 			 "HW rate", pdev->data_rc);
5513 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5514 			 "Sched self triggers", pdev->self_triggers);
5515 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5516 			 "Dropped due to SW retries",
5517 			 pdev->sw_retry_failure);
5518 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5519 			 "Illegal rate phy errors",
5520 			 pdev->illgl_rate_phy_err);
5521 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5522 			 "PDEV continuous xretry", pdev->pdev_cont_xretry);
5523 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5524 			 "TX timeout", pdev->pdev_tx_timeout);
5525 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5526 			 "PDEV resets", pdev->pdev_resets);
5527 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5528 			 "Stateless TIDs alloc failures",
5529 			 pdev->stateless_tid_alloc_failure);
5530 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5531 			 "PHY underrun", pdev->phy_underrun);
5532 	len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
5533 			 "MPDU is more than txop limit", pdev->txop_ovf);
5534 	*length = len;
5535 }
5536 
5537 static void
5538 ath11k_wmi_fw_pdev_rx_stats_fill(const struct ath11k_fw_stats_pdev *pdev,
5539 				 char *buf, u32 *length)
5540 {
5541 	u32 len = *length;
5542 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5543 
5544 	len += scnprintf(buf + len, buf_len - len, "\n%30s\n",
5545 			 "ath11k PDEV RX stats");
5546 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5547 			 "====================");
5548 
5549 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5550 			 "Mid PPDU route change",
5551 			 pdev->mid_ppdu_route_change);
5552 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5553 			 "Tot. number of statuses", pdev->status_rcvd);
5554 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5555 			 "Extra frags on rings 0", pdev->r0_frags);
5556 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5557 			 "Extra frags on rings 1", pdev->r1_frags);
5558 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5559 			 "Extra frags on rings 2", pdev->r2_frags);
5560 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5561 			 "Extra frags on rings 3", pdev->r3_frags);
5562 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5563 			 "MSDUs delivered to HTT", pdev->htt_msdus);
5564 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5565 			 "MPDUs delivered to HTT", pdev->htt_mpdus);
5566 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5567 			 "MSDUs delivered to stack", pdev->loc_msdus);
5568 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5569 			 "MPDUs delivered to stack", pdev->loc_mpdus);
5570 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5571 			 "Oversized AMSUs", pdev->oversize_amsdu);
5572 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5573 			 "PHY errors", pdev->phy_errs);
5574 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5575 			 "PHY errors drops", pdev->phy_err_drop);
5576 	len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
5577 			 "MPDU errors (FCS, MIC, ENC)", pdev->mpdu_errs);
5578 	*length = len;
5579 }
5580 
5581 static void
5582 ath11k_wmi_fw_vdev_stats_fill(struct ath11k *ar,
5583 			      const struct ath11k_fw_stats_vdev *vdev,
5584 			      char *buf, u32 *length)
5585 {
5586 	u32 len = *length;
5587 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5588 	struct ath11k_vif *arvif = ath11k_mac_get_arvif(ar, vdev->vdev_id);
5589 	u8 *vif_macaddr;
5590 	int i;
5591 
5592 	/* VDEV stats has all the active VDEVs of other PDEVs as well,
5593 	 * ignoring those not part of requested PDEV
5594 	 */
5595 	if (!arvif)
5596 		return;
5597 
5598 	vif_macaddr = arvif->vif->addr;
5599 
5600 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5601 			 "VDEV ID", vdev->vdev_id);
5602 	len += scnprintf(buf + len, buf_len - len, "%30s %pM\n",
5603 			 "VDEV MAC address", vif_macaddr);
5604 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5605 			 "beacon snr", vdev->beacon_snr);
5606 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5607 			 "data snr", vdev->data_snr);
5608 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5609 			 "num rx frames", vdev->num_rx_frames);
5610 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5611 			 "num rts fail", vdev->num_rts_fail);
5612 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5613 			 "num rts success", vdev->num_rts_success);
5614 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5615 			 "num rx err", vdev->num_rx_err);
5616 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5617 			 "num rx discard", vdev->num_rx_discard);
5618 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5619 			 "num tx not acked", vdev->num_tx_not_acked);
5620 
5621 	for (i = 0 ; i < ARRAY_SIZE(vdev->num_tx_frames); i++)
5622 		len += scnprintf(buf + len, buf_len - len,
5623 				"%25s [%02d] %u\n",
5624 				"num tx frames", i,
5625 				vdev->num_tx_frames[i]);
5626 
5627 	for (i = 0 ; i < ARRAY_SIZE(vdev->num_tx_frames_retries); i++)
5628 		len += scnprintf(buf + len, buf_len - len,
5629 				"%25s [%02d] %u\n",
5630 				"num tx frames retries", i,
5631 				vdev->num_tx_frames_retries[i]);
5632 
5633 	for (i = 0 ; i < ARRAY_SIZE(vdev->num_tx_frames_failures); i++)
5634 		len += scnprintf(buf + len, buf_len - len,
5635 				"%25s [%02d] %u\n",
5636 				"num tx frames failures", i,
5637 				vdev->num_tx_frames_failures[i]);
5638 
5639 	for (i = 0 ; i < ARRAY_SIZE(vdev->tx_rate_history); i++)
5640 		len += scnprintf(buf + len, buf_len - len,
5641 				"%25s [%02d] 0x%08x\n",
5642 				"tx rate history", i,
5643 				vdev->tx_rate_history[i]);
5644 
5645 	for (i = 0 ; i < ARRAY_SIZE(vdev->beacon_rssi_history); i++)
5646 		len += scnprintf(buf + len, buf_len - len,
5647 				"%25s [%02d] %u\n",
5648 				"beacon rssi history", i,
5649 				vdev->beacon_rssi_history[i]);
5650 
5651 	len += scnprintf(buf + len, buf_len - len, "\n");
5652 	*length = len;
5653 }
5654 
5655 static void
5656 ath11k_wmi_fw_bcn_stats_fill(struct ath11k *ar,
5657 			     const struct ath11k_fw_stats_bcn *bcn,
5658 			     char *buf, u32 *length)
5659 {
5660 	u32 len = *length;
5661 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5662 	struct ath11k_vif *arvif = ath11k_mac_get_arvif(ar, bcn->vdev_id);
5663 	u8 *vdev_macaddr;
5664 
5665 	if (!arvif) {
5666 		ath11k_warn(ar->ab, "invalid vdev id %d in bcn stats",
5667 			    bcn->vdev_id);
5668 		return;
5669 	}
5670 
5671 	vdev_macaddr = arvif->vif->addr;
5672 
5673 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5674 			 "VDEV ID", bcn->vdev_id);
5675 	len += scnprintf(buf + len, buf_len - len, "%30s %pM\n",
5676 			 "VDEV MAC address", vdev_macaddr);
5677 	len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5678 			 "================");
5679 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5680 			 "Num of beacon tx success", bcn->tx_bcn_succ_cnt);
5681 	len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
5682 			 "Num of beacon tx failures", bcn->tx_bcn_outage_cnt);
5683 
5684 	len += scnprintf(buf + len, buf_len - len, "\n");
5685 	*length = len;
5686 }
5687 
5688 void ath11k_wmi_fw_stats_fill(struct ath11k *ar,
5689 			      struct ath11k_fw_stats *fw_stats,
5690 			      u32 stats_id, char *buf)
5691 {
5692 	u32 len = 0;
5693 	u32 buf_len = ATH11K_FW_STATS_BUF_SIZE;
5694 	const struct ath11k_fw_stats_pdev *pdev;
5695 	const struct ath11k_fw_stats_vdev *vdev;
5696 	const struct ath11k_fw_stats_bcn *bcn;
5697 	size_t num_bcn;
5698 
5699 	spin_lock_bh(&ar->data_lock);
5700 
5701 	if (stats_id == WMI_REQUEST_PDEV_STAT) {
5702 		pdev = list_first_entry_or_null(&fw_stats->pdevs,
5703 						struct ath11k_fw_stats_pdev, list);
5704 		if (!pdev) {
5705 			ath11k_warn(ar->ab, "failed to get pdev stats\n");
5706 			goto unlock;
5707 		}
5708 
5709 		ath11k_wmi_fw_pdev_base_stats_fill(pdev, buf, &len);
5710 		ath11k_wmi_fw_pdev_tx_stats_fill(pdev, buf, &len);
5711 		ath11k_wmi_fw_pdev_rx_stats_fill(pdev, buf, &len);
5712 	}
5713 
5714 	if (stats_id == WMI_REQUEST_VDEV_STAT) {
5715 		len += scnprintf(buf + len, buf_len - len, "\n");
5716 		len += scnprintf(buf + len, buf_len - len, "%30s\n",
5717 				 "ath11k VDEV stats");
5718 		len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5719 				 "=================");
5720 
5721 		list_for_each_entry(vdev, &fw_stats->vdevs, list)
5722 			ath11k_wmi_fw_vdev_stats_fill(ar, vdev, buf, &len);
5723 	}
5724 
5725 	if (stats_id == WMI_REQUEST_BCN_STAT) {
5726 		num_bcn = ath11k_wmi_fw_stats_num_bcn(&fw_stats->bcn);
5727 
5728 		len += scnprintf(buf + len, buf_len - len, "\n");
5729 		len += scnprintf(buf + len, buf_len - len, "%30s (%zu)\n",
5730 				 "ath11k Beacon stats", num_bcn);
5731 		len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
5732 				 "===================");
5733 
5734 		list_for_each_entry(bcn, &fw_stats->bcn, list)
5735 			ath11k_wmi_fw_bcn_stats_fill(ar, bcn, buf, &len);
5736 	}
5737 
5738 unlock:
5739 	spin_unlock_bh(&ar->data_lock);
5740 
5741 	if (len >= buf_len)
5742 		buf[len - 1] = 0;
5743 	else
5744 		buf[len] = 0;
5745 }
5746 
5747 static void ath11k_wmi_op_ep_tx_credits(struct ath11k_base *ab)
5748 {
5749 	/* try to send pending beacons first. they take priority */
5750 	wake_up(&ab->wmi_ab.tx_credits_wq);
5751 }
5752 
5753 static void ath11k_wmi_htc_tx_complete(struct ath11k_base *ab,
5754 				       struct sk_buff *skb)
5755 {
5756 	dev_kfree_skb(skb);
5757 }
5758 
5759 static bool ath11k_reg_is_world_alpha(char *alpha)
5760 {
5761 	return alpha[0] == '0' && alpha[1] == '0';
5762 }
5763 
5764 static int ath11k_reg_chan_list_event(struct ath11k_base *ab, struct sk_buff *skb)
5765 {
5766 	struct cur_regulatory_info *reg_info = NULL;
5767 	struct ieee80211_regdomain *regd = NULL;
5768 	bool intersect = false;
5769 	int ret = 0, pdev_idx;
5770 	struct ath11k *ar;
5771 
5772 	reg_info = kzalloc(sizeof(*reg_info), GFP_ATOMIC);
5773 	if (!reg_info) {
5774 		ret = -ENOMEM;
5775 		goto fallback;
5776 	}
5777 
5778 	ret = ath11k_pull_reg_chan_list_update_ev(ab, skb, reg_info);
5779 	if (ret) {
5780 		ath11k_warn(ab, "failed to extract regulatory info from received event\n");
5781 		goto fallback;
5782 	}
5783 
5784 	if (reg_info->status_code != REG_SET_CC_STATUS_PASS) {
5785 		/* In case of failure to set the requested ctry,
5786 		 * fw retains the current regd. We print a failure info
5787 		 * and return from here.
5788 		 */
5789 		ath11k_warn(ab, "Failed to set the requested Country regulatory setting\n");
5790 		goto mem_free;
5791 	}
5792 
5793 	pdev_idx = reg_info->phy_id;
5794 
5795 	if (pdev_idx >= ab->num_radios) {
5796 		/* Process the event for phy0 only if single_pdev_only
5797 		 * is true. If pdev_idx is valid but not 0, discard the
5798 		 * event. Otherwise, it goes to fallback.
5799 		 */
5800 		if (ab->hw_params.single_pdev_only &&
5801 		    pdev_idx < ab->hw_params.num_rxmda_per_pdev)
5802 			goto mem_free;
5803 		else
5804 			goto fallback;
5805 	}
5806 
5807 	/* Avoid multiple overwrites to default regd, during core
5808 	 * stop-start after mac registration.
5809 	 */
5810 	if (ab->default_regd[pdev_idx] && !ab->new_regd[pdev_idx] &&
5811 	    !memcmp((char *)ab->default_regd[pdev_idx]->alpha2,
5812 		    (char *)reg_info->alpha2, 2))
5813 		goto mem_free;
5814 
5815 	/* Intersect new rules with default regd if a new country setting was
5816 	 * requested, i.e a default regd was already set during initialization
5817 	 * and the regd coming from this event has a valid country info.
5818 	 */
5819 	if (ab->default_regd[pdev_idx] &&
5820 	    !ath11k_reg_is_world_alpha((char *)
5821 		ab->default_regd[pdev_idx]->alpha2) &&
5822 	    !ath11k_reg_is_world_alpha((char *)reg_info->alpha2))
5823 		intersect = true;
5824 
5825 	regd = ath11k_reg_build_regd(ab, reg_info, intersect);
5826 	if (!regd) {
5827 		ath11k_warn(ab, "failed to build regd from reg_info\n");
5828 		goto fallback;
5829 	}
5830 
5831 	spin_lock(&ab->base_lock);
5832 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags)) {
5833 		/* Once mac is registered, ar is valid and all CC events from
5834 		 * fw is considered to be received due to user requests
5835 		 * currently.
5836 		 * Free previously built regd before assigning the newly
5837 		 * generated regd to ar. NULL pointer handling will be
5838 		 * taken care by kfree itself.
5839 		 */
5840 		ar = ab->pdevs[pdev_idx].ar;
5841 		kfree(ab->new_regd[pdev_idx]);
5842 		ab->new_regd[pdev_idx] = regd;
5843 		ieee80211_queue_work(ar->hw, &ar->regd_update_work);
5844 	} else {
5845 		/* Multiple events for the same *ar is not expected. But we
5846 		 * can still clear any previously stored default_regd if we
5847 		 * are receiving this event for the same radio by mistake.
5848 		 * NULL pointer handling will be taken care by kfree itself.
5849 		 */
5850 		kfree(ab->default_regd[pdev_idx]);
5851 		/* This regd would be applied during mac registration */
5852 		ab->default_regd[pdev_idx] = regd;
5853 	}
5854 	ab->dfs_region = reg_info->dfs_region;
5855 	spin_unlock(&ab->base_lock);
5856 
5857 	goto mem_free;
5858 
5859 fallback:
5860 	/* Fallback to older reg (by sending previous country setting
5861 	 * again if fw has succeded and we failed to process here.
5862 	 * The Regdomain should be uniform across driver and fw. Since the
5863 	 * FW has processed the command and sent a success status, we expect
5864 	 * this function to succeed as well. If it doesn't, CTRY needs to be
5865 	 * reverted at the fw and the old SCAN_CHAN_LIST cmd needs to be sent.
5866 	 */
5867 	/* TODO: This is rare, but still should also be handled */
5868 	WARN_ON(1);
5869 mem_free:
5870 	if (reg_info) {
5871 		kfree(reg_info->reg_rules_2g_ptr);
5872 		kfree(reg_info->reg_rules_5g_ptr);
5873 		kfree(reg_info);
5874 	}
5875 	return ret;
5876 }
5877 
5878 static int ath11k_wmi_tlv_rdy_parse(struct ath11k_base *ab, u16 tag, u16 len,
5879 				    const void *ptr, void *data)
5880 {
5881 	struct wmi_tlv_rdy_parse *rdy_parse = data;
5882 	struct wmi_ready_event fixed_param;
5883 	struct wmi_mac_addr *addr_list;
5884 	struct ath11k_pdev *pdev;
5885 	u32 num_mac_addr;
5886 	int i;
5887 
5888 	switch (tag) {
5889 	case WMI_TAG_READY_EVENT:
5890 		memset(&fixed_param, 0, sizeof(fixed_param));
5891 		memcpy(&fixed_param, (struct wmi_ready_event *)ptr,
5892 		       min_t(u16, sizeof(fixed_param), len));
5893 		ab->wlan_init_status = fixed_param.ready_event_min.status;
5894 		rdy_parse->num_extra_mac_addr =
5895 			fixed_param.ready_event_min.num_extra_mac_addr;
5896 
5897 		ether_addr_copy(ab->mac_addr,
5898 				fixed_param.ready_event_min.mac_addr.addr);
5899 		ab->pktlog_defs_checksum = fixed_param.pktlog_defs_checksum;
5900 		ab->wmi_ready = true;
5901 		break;
5902 	case WMI_TAG_ARRAY_FIXED_STRUCT:
5903 		addr_list = (struct wmi_mac_addr *)ptr;
5904 		num_mac_addr = rdy_parse->num_extra_mac_addr;
5905 
5906 		if (!(ab->num_radios > 1 && num_mac_addr >= ab->num_radios))
5907 			break;
5908 
5909 		for (i = 0; i < ab->num_radios; i++) {
5910 			pdev = &ab->pdevs[i];
5911 			ether_addr_copy(pdev->mac_addr, addr_list[i].addr);
5912 		}
5913 		ab->pdevs_macaddr_valid = true;
5914 		break;
5915 	default:
5916 		break;
5917 	}
5918 
5919 	return 0;
5920 }
5921 
5922 static int ath11k_ready_event(struct ath11k_base *ab, struct sk_buff *skb)
5923 {
5924 	struct wmi_tlv_rdy_parse rdy_parse = { };
5925 	int ret;
5926 
5927 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
5928 				  ath11k_wmi_tlv_rdy_parse, &rdy_parse);
5929 	if (ret) {
5930 		ath11k_warn(ab, "failed to parse tlv %d\n", ret);
5931 		return ret;
5932 	}
5933 
5934 	complete(&ab->wmi_ab.unified_ready);
5935 	return 0;
5936 }
5937 
5938 static void ath11k_peer_delete_resp_event(struct ath11k_base *ab, struct sk_buff *skb)
5939 {
5940 	struct wmi_peer_delete_resp_event peer_del_resp;
5941 	struct ath11k *ar;
5942 
5943 	if (ath11k_pull_peer_del_resp_ev(ab, skb, &peer_del_resp) != 0) {
5944 		ath11k_warn(ab, "failed to extract peer delete resp");
5945 		return;
5946 	}
5947 
5948 	rcu_read_lock();
5949 	ar = ath11k_mac_get_ar_by_vdev_id(ab, peer_del_resp.vdev_id);
5950 	if (!ar) {
5951 		ath11k_warn(ab, "invalid vdev id in peer delete resp ev %d",
5952 			    peer_del_resp.vdev_id);
5953 		rcu_read_unlock();
5954 		return;
5955 	}
5956 
5957 	complete(&ar->peer_delete_done);
5958 	rcu_read_unlock();
5959 	ath11k_dbg(ab, ATH11K_DBG_WMI, "peer delete resp for vdev id %d addr %pM\n",
5960 		   peer_del_resp.vdev_id, peer_del_resp.peer_macaddr.addr);
5961 }
5962 
5963 static void ath11k_vdev_delete_resp_event(struct ath11k_base *ab,
5964 					  struct sk_buff *skb)
5965 {
5966 	struct ath11k *ar;
5967 	u32 vdev_id = 0;
5968 
5969 	if (ath11k_pull_vdev_del_resp_ev(ab, skb, &vdev_id) != 0) {
5970 		ath11k_warn(ab, "failed to extract vdev delete resp");
5971 		return;
5972 	}
5973 
5974 	rcu_read_lock();
5975 	ar = ath11k_mac_get_ar_by_vdev_id(ab, vdev_id);
5976 	if (!ar) {
5977 		ath11k_warn(ab, "invalid vdev id in vdev delete resp ev %d",
5978 			    vdev_id);
5979 		rcu_read_unlock();
5980 		return;
5981 	}
5982 
5983 	complete(&ar->vdev_delete_done);
5984 
5985 	rcu_read_unlock();
5986 
5987 	ath11k_dbg(ab, ATH11K_DBG_WMI, "vdev delete resp for vdev id %d\n",
5988 		   vdev_id);
5989 }
5990 
5991 static inline const char *ath11k_wmi_vdev_resp_print(u32 vdev_resp_status)
5992 {
5993 	switch (vdev_resp_status) {
5994 	case WMI_VDEV_START_RESPONSE_INVALID_VDEVID:
5995 		return "invalid vdev id";
5996 	case WMI_VDEV_START_RESPONSE_NOT_SUPPORTED:
5997 		return "not supported";
5998 	case WMI_VDEV_START_RESPONSE_DFS_VIOLATION:
5999 		return "dfs violation";
6000 	case WMI_VDEV_START_RESPONSE_INVALID_REGDOMAIN:
6001 		return "invalid regdomain";
6002 	default:
6003 		return "unknown";
6004 	}
6005 }
6006 
6007 static void ath11k_vdev_start_resp_event(struct ath11k_base *ab, struct sk_buff *skb)
6008 {
6009 	struct wmi_vdev_start_resp_event vdev_start_resp;
6010 	struct ath11k *ar;
6011 	u32 status;
6012 
6013 	if (ath11k_pull_vdev_start_resp_tlv(ab, skb, &vdev_start_resp) != 0) {
6014 		ath11k_warn(ab, "failed to extract vdev start resp");
6015 		return;
6016 	}
6017 
6018 	rcu_read_lock();
6019 	ar = ath11k_mac_get_ar_by_vdev_id(ab, vdev_start_resp.vdev_id);
6020 	if (!ar) {
6021 		ath11k_warn(ab, "invalid vdev id in vdev start resp ev %d",
6022 			    vdev_start_resp.vdev_id);
6023 		rcu_read_unlock();
6024 		return;
6025 	}
6026 
6027 	ar->last_wmi_vdev_start_status = 0;
6028 
6029 	status = vdev_start_resp.status;
6030 
6031 	if (WARN_ON_ONCE(status)) {
6032 		ath11k_warn(ab, "vdev start resp error status %d (%s)\n",
6033 			    status, ath11k_wmi_vdev_resp_print(status));
6034 		ar->last_wmi_vdev_start_status = status;
6035 	}
6036 
6037 	complete(&ar->vdev_setup_done);
6038 
6039 	rcu_read_unlock();
6040 
6041 	ath11k_dbg(ab, ATH11K_DBG_WMI, "vdev start resp for vdev id %d",
6042 		   vdev_start_resp.vdev_id);
6043 }
6044 
6045 static void ath11k_bcn_tx_status_event(struct ath11k_base *ab, struct sk_buff *skb)
6046 {
6047 	u32 vdev_id, tx_status;
6048 
6049 	if (ath11k_pull_bcn_tx_status_ev(ab, skb->data, skb->len,
6050 					 &vdev_id, &tx_status) != 0) {
6051 		ath11k_warn(ab, "failed to extract bcn tx status");
6052 		return;
6053 	}
6054 }
6055 
6056 static void ath11k_vdev_stopped_event(struct ath11k_base *ab, struct sk_buff *skb)
6057 {
6058 	struct ath11k *ar;
6059 	u32 vdev_id = 0;
6060 
6061 	if (ath11k_pull_vdev_stopped_param_tlv(ab, skb, &vdev_id) != 0) {
6062 		ath11k_warn(ab, "failed to extract vdev stopped event");
6063 		return;
6064 	}
6065 
6066 	rcu_read_lock();
6067 	ar = ath11k_mac_get_ar_by_vdev_id(ab, vdev_id);
6068 	if (!ar) {
6069 		ath11k_warn(ab, "invalid vdev id in vdev stopped ev %d",
6070 			    vdev_id);
6071 		rcu_read_unlock();
6072 		return;
6073 	}
6074 
6075 	complete(&ar->vdev_setup_done);
6076 
6077 	rcu_read_unlock();
6078 
6079 	ath11k_dbg(ab, ATH11K_DBG_WMI, "vdev stopped for vdev id %d", vdev_id);
6080 }
6081 
6082 static void ath11k_mgmt_rx_event(struct ath11k_base *ab, struct sk_buff *skb)
6083 {
6084 	struct mgmt_rx_event_params rx_ev = {0};
6085 	struct ath11k *ar;
6086 	struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
6087 	struct ieee80211_hdr *hdr;
6088 	u16 fc;
6089 	struct ieee80211_supported_band *sband;
6090 
6091 	if (ath11k_pull_mgmt_rx_params_tlv(ab, skb, &rx_ev) != 0) {
6092 		ath11k_warn(ab, "failed to extract mgmt rx event");
6093 		dev_kfree_skb(skb);
6094 		return;
6095 	}
6096 
6097 	memset(status, 0, sizeof(*status));
6098 
6099 	ath11k_dbg(ab, ATH11K_DBG_MGMT, "mgmt rx event status %08x\n",
6100 		   rx_ev.status);
6101 
6102 	rcu_read_lock();
6103 	ar = ath11k_mac_get_ar_by_pdev_id(ab, rx_ev.pdev_id);
6104 
6105 	if (!ar) {
6106 		ath11k_warn(ab, "invalid pdev_id %d in mgmt_rx_event\n",
6107 			    rx_ev.pdev_id);
6108 		dev_kfree_skb(skb);
6109 		goto exit;
6110 	}
6111 
6112 	if ((test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) ||
6113 	    (rx_ev.status & (WMI_RX_STATUS_ERR_DECRYPT |
6114 	    WMI_RX_STATUS_ERR_KEY_CACHE_MISS | WMI_RX_STATUS_ERR_CRC))) {
6115 		dev_kfree_skb(skb);
6116 		goto exit;
6117 	}
6118 
6119 	if (rx_ev.status & WMI_RX_STATUS_ERR_MIC)
6120 		status->flag |= RX_FLAG_MMIC_ERROR;
6121 
6122 	if (rx_ev.chan_freq >= ATH11K_MIN_6G_FREQ) {
6123 		status->band = NL80211_BAND_6GHZ;
6124 	} else if (rx_ev.channel >= 1 && rx_ev.channel <= 14) {
6125 		status->band = NL80211_BAND_2GHZ;
6126 	} else if (rx_ev.channel >= 36 && rx_ev.channel <= ATH11K_MAX_5G_CHAN) {
6127 		status->band = NL80211_BAND_5GHZ;
6128 	} else {
6129 		/* Shouldn't happen unless list of advertised channels to
6130 		 * mac80211 has been changed.
6131 		 */
6132 		WARN_ON_ONCE(1);
6133 		dev_kfree_skb(skb);
6134 		goto exit;
6135 	}
6136 
6137 	if (rx_ev.phy_mode == MODE_11B &&
6138 	    (status->band == NL80211_BAND_5GHZ || status->band == NL80211_BAND_6GHZ))
6139 		ath11k_dbg(ab, ATH11K_DBG_WMI,
6140 			   "wmi mgmt rx 11b (CCK) on 5/6GHz, band = %d\n", status->band);
6141 
6142 	sband = &ar->mac.sbands[status->band];
6143 
6144 	status->freq = ieee80211_channel_to_frequency(rx_ev.channel,
6145 						      status->band);
6146 	status->signal = rx_ev.snr + ATH11K_DEFAULT_NOISE_FLOOR;
6147 	status->rate_idx = ath11k_mac_bitrate_to_idx(sband, rx_ev.rate / 100);
6148 
6149 	hdr = (struct ieee80211_hdr *)skb->data;
6150 	fc = le16_to_cpu(hdr->frame_control);
6151 
6152 	/* Firmware is guaranteed to report all essential management frames via
6153 	 * WMI while it can deliver some extra via HTT. Since there can be
6154 	 * duplicates split the reporting wrt monitor/sniffing.
6155 	 */
6156 	status->flag |= RX_FLAG_SKIP_MONITOR;
6157 
6158 	/* In case of PMF, FW delivers decrypted frames with Protected Bit set.
6159 	 * Don't clear that. Also, FW delivers broadcast management frames
6160 	 * (ex: group privacy action frames in mesh) as encrypted payload.
6161 	 */
6162 	if (ieee80211_has_protected(hdr->frame_control) &&
6163 	    !is_multicast_ether_addr(ieee80211_get_DA(hdr))) {
6164 		status->flag |= RX_FLAG_DECRYPTED;
6165 
6166 		if (!ieee80211_is_robust_mgmt_frame(skb)) {
6167 			status->flag |= RX_FLAG_IV_STRIPPED |
6168 					RX_FLAG_MMIC_STRIPPED;
6169 			hdr->frame_control = __cpu_to_le16(fc &
6170 					     ~IEEE80211_FCTL_PROTECTED);
6171 		}
6172 	}
6173 
6174 	if (ieee80211_is_beacon(hdr->frame_control))
6175 		ath11k_mac_handle_beacon(ar, skb);
6176 
6177 	ath11k_dbg(ab, ATH11K_DBG_MGMT,
6178 		   "event mgmt rx skb %pK len %d ftype %02x stype %02x\n",
6179 		   skb, skb->len,
6180 		   fc & IEEE80211_FCTL_FTYPE, fc & IEEE80211_FCTL_STYPE);
6181 
6182 	ath11k_dbg(ab, ATH11K_DBG_MGMT,
6183 		   "event mgmt rx freq %d band %d snr %d, rate_idx %d\n",
6184 		   status->freq, status->band, status->signal,
6185 		   status->rate_idx);
6186 
6187 	ieee80211_rx_ni(ar->hw, skb);
6188 
6189 exit:
6190 	rcu_read_unlock();
6191 }
6192 
6193 static void ath11k_mgmt_tx_compl_event(struct ath11k_base *ab, struct sk_buff *skb)
6194 {
6195 	struct wmi_mgmt_tx_compl_event tx_compl_param = {0};
6196 	struct ath11k *ar;
6197 
6198 	if (ath11k_pull_mgmt_tx_compl_param_tlv(ab, skb, &tx_compl_param) != 0) {
6199 		ath11k_warn(ab, "failed to extract mgmt tx compl event");
6200 		return;
6201 	}
6202 
6203 	rcu_read_lock();
6204 	ar = ath11k_mac_get_ar_by_pdev_id(ab, tx_compl_param.pdev_id);
6205 	if (!ar) {
6206 		ath11k_warn(ab, "invalid pdev id %d in mgmt_tx_compl_event\n",
6207 			    tx_compl_param.pdev_id);
6208 		goto exit;
6209 	}
6210 
6211 	wmi_process_mgmt_tx_comp(ar, tx_compl_param.desc_id,
6212 				 tx_compl_param.status);
6213 
6214 	ath11k_dbg(ab, ATH11K_DBG_MGMT,
6215 		   "mgmt tx compl ev pdev_id %d, desc_id %d, status %d",
6216 		   tx_compl_param.pdev_id, tx_compl_param.desc_id,
6217 		   tx_compl_param.status);
6218 
6219 exit:
6220 	rcu_read_unlock();
6221 }
6222 
6223 static struct ath11k *ath11k_get_ar_on_scan_abort(struct ath11k_base *ab,
6224 						  u32 vdev_id)
6225 {
6226 	int i;
6227 	struct ath11k_pdev *pdev;
6228 	struct ath11k *ar;
6229 
6230 	for (i = 0; i < ab->num_radios; i++) {
6231 		pdev = rcu_dereference(ab->pdevs_active[i]);
6232 		if (pdev && pdev->ar) {
6233 			ar = pdev->ar;
6234 
6235 			spin_lock_bh(&ar->data_lock);
6236 			if (ar->scan.state == ATH11K_SCAN_ABORTING &&
6237 			    ar->scan.vdev_id == vdev_id) {
6238 				spin_unlock_bh(&ar->data_lock);
6239 				return ar;
6240 			}
6241 			spin_unlock_bh(&ar->data_lock);
6242 		}
6243 	}
6244 	return NULL;
6245 }
6246 
6247 static void ath11k_scan_event(struct ath11k_base *ab, struct sk_buff *skb)
6248 {
6249 	struct ath11k *ar;
6250 	struct wmi_scan_event scan_ev = {0};
6251 
6252 	if (ath11k_pull_scan_ev(ab, skb, &scan_ev) != 0) {
6253 		ath11k_warn(ab, "failed to extract scan event");
6254 		return;
6255 	}
6256 
6257 	rcu_read_lock();
6258 
6259 	/* In case the scan was cancelled, ex. during interface teardown,
6260 	 * the interface will not be found in active interfaces.
6261 	 * Rather, in such scenarios, iterate over the active pdev's to
6262 	 * search 'ar' if the corresponding 'ar' scan is ABORTING and the
6263 	 * aborting scan's vdev id matches this event info.
6264 	 */
6265 	if (scan_ev.event_type == WMI_SCAN_EVENT_COMPLETED &&
6266 	    scan_ev.reason == WMI_SCAN_REASON_CANCELLED)
6267 		ar = ath11k_get_ar_on_scan_abort(ab, scan_ev.vdev_id);
6268 	else
6269 		ar = ath11k_mac_get_ar_by_vdev_id(ab, scan_ev.vdev_id);
6270 
6271 	if (!ar) {
6272 		ath11k_warn(ab, "Received scan event for unknown vdev");
6273 		rcu_read_unlock();
6274 		return;
6275 	}
6276 
6277 	spin_lock_bh(&ar->data_lock);
6278 
6279 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6280 		   "scan event %s type %d reason %d freq %d req_id %d scan_id %d vdev_id %d state %s (%d)\n",
6281 		   ath11k_wmi_event_scan_type_str(scan_ev.event_type, scan_ev.reason),
6282 		   scan_ev.event_type, scan_ev.reason, scan_ev.channel_freq,
6283 		   scan_ev.scan_req_id, scan_ev.scan_id, scan_ev.vdev_id,
6284 		   ath11k_scan_state_str(ar->scan.state), ar->scan.state);
6285 
6286 	switch (scan_ev.event_type) {
6287 	case WMI_SCAN_EVENT_STARTED:
6288 		ath11k_wmi_event_scan_started(ar);
6289 		break;
6290 	case WMI_SCAN_EVENT_COMPLETED:
6291 		ath11k_wmi_event_scan_completed(ar);
6292 		break;
6293 	case WMI_SCAN_EVENT_BSS_CHANNEL:
6294 		ath11k_wmi_event_scan_bss_chan(ar);
6295 		break;
6296 	case WMI_SCAN_EVENT_FOREIGN_CHAN:
6297 		ath11k_wmi_event_scan_foreign_chan(ar, scan_ev.channel_freq);
6298 		break;
6299 	case WMI_SCAN_EVENT_START_FAILED:
6300 		ath11k_warn(ab, "received scan start failure event\n");
6301 		ath11k_wmi_event_scan_start_failed(ar);
6302 		break;
6303 	case WMI_SCAN_EVENT_DEQUEUED:
6304 	case WMI_SCAN_EVENT_PREEMPTED:
6305 	case WMI_SCAN_EVENT_RESTARTED:
6306 	case WMI_SCAN_EVENT_FOREIGN_CHAN_EXIT:
6307 	default:
6308 		break;
6309 	}
6310 
6311 	spin_unlock_bh(&ar->data_lock);
6312 
6313 	rcu_read_unlock();
6314 }
6315 
6316 static void ath11k_peer_sta_kickout_event(struct ath11k_base *ab, struct sk_buff *skb)
6317 {
6318 	struct wmi_peer_sta_kickout_arg arg = {};
6319 	struct ieee80211_sta *sta;
6320 	struct ath11k_peer *peer;
6321 	struct ath11k *ar;
6322 
6323 	if (ath11k_pull_peer_sta_kickout_ev(ab, skb, &arg) != 0) {
6324 		ath11k_warn(ab, "failed to extract peer sta kickout event");
6325 		return;
6326 	}
6327 
6328 	rcu_read_lock();
6329 
6330 	spin_lock_bh(&ab->base_lock);
6331 
6332 	peer = ath11k_peer_find_by_addr(ab, arg.mac_addr);
6333 
6334 	if (!peer) {
6335 		ath11k_warn(ab, "peer not found %pM\n",
6336 			    arg.mac_addr);
6337 		goto exit;
6338 	}
6339 
6340 	ar = ath11k_mac_get_ar_by_vdev_id(ab, peer->vdev_id);
6341 	if (!ar) {
6342 		ath11k_warn(ab, "invalid vdev id in peer sta kickout ev %d",
6343 			    peer->vdev_id);
6344 		goto exit;
6345 	}
6346 
6347 	sta = ieee80211_find_sta_by_ifaddr(ar->hw,
6348 					   arg.mac_addr, NULL);
6349 	if (!sta) {
6350 		ath11k_warn(ab, "Spurious quick kickout for STA %pM\n",
6351 			    arg.mac_addr);
6352 		goto exit;
6353 	}
6354 
6355 	ath11k_dbg(ab, ATH11K_DBG_WMI, "peer sta kickout event %pM",
6356 		   arg.mac_addr);
6357 
6358 	ieee80211_report_low_ack(sta, 10);
6359 
6360 exit:
6361 	spin_unlock_bh(&ab->base_lock);
6362 	rcu_read_unlock();
6363 }
6364 
6365 static void ath11k_roam_event(struct ath11k_base *ab, struct sk_buff *skb)
6366 {
6367 	struct wmi_roam_event roam_ev = {};
6368 	struct ath11k *ar;
6369 
6370 	if (ath11k_pull_roam_ev(ab, skb, &roam_ev) != 0) {
6371 		ath11k_warn(ab, "failed to extract roam event");
6372 		return;
6373 	}
6374 
6375 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6376 		   "wmi roam event vdev %u reason 0x%08x rssi %d\n",
6377 		   roam_ev.vdev_id, roam_ev.reason, roam_ev.rssi);
6378 
6379 	rcu_read_lock();
6380 	ar = ath11k_mac_get_ar_by_vdev_id(ab, roam_ev.vdev_id);
6381 	if (!ar) {
6382 		ath11k_warn(ab, "invalid vdev id in roam ev %d",
6383 			    roam_ev.vdev_id);
6384 		rcu_read_unlock();
6385 		return;
6386 	}
6387 
6388 	if (roam_ev.reason >= WMI_ROAM_REASON_MAX)
6389 		ath11k_warn(ab, "ignoring unknown roam event reason %d on vdev %i\n",
6390 			    roam_ev.reason, roam_ev.vdev_id);
6391 
6392 	switch (roam_ev.reason) {
6393 	case WMI_ROAM_REASON_BEACON_MISS:
6394 		ath11k_mac_handle_beacon_miss(ar, roam_ev.vdev_id);
6395 		break;
6396 	case WMI_ROAM_REASON_BETTER_AP:
6397 	case WMI_ROAM_REASON_LOW_RSSI:
6398 	case WMI_ROAM_REASON_SUITABLE_AP_FOUND:
6399 	case WMI_ROAM_REASON_HO_FAILED:
6400 		ath11k_warn(ab, "ignoring not implemented roam event reason %d on vdev %i\n",
6401 			    roam_ev.reason, roam_ev.vdev_id);
6402 		break;
6403 	}
6404 
6405 	rcu_read_unlock();
6406 }
6407 
6408 static void ath11k_chan_info_event(struct ath11k_base *ab, struct sk_buff *skb)
6409 {
6410 	struct wmi_chan_info_event ch_info_ev = {0};
6411 	struct ath11k *ar;
6412 	struct survey_info *survey;
6413 	int idx;
6414 	/* HW channel counters frequency value in hertz */
6415 	u32 cc_freq_hz = ab->cc_freq_hz;
6416 
6417 	if (ath11k_pull_chan_info_ev(ab, skb->data, skb->len, &ch_info_ev) != 0) {
6418 		ath11k_warn(ab, "failed to extract chan info event");
6419 		return;
6420 	}
6421 
6422 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6423 		   "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",
6424 		   ch_info_ev.vdev_id, ch_info_ev.err_code, ch_info_ev.freq,
6425 		   ch_info_ev.cmd_flags, ch_info_ev.noise_floor,
6426 		   ch_info_ev.rx_clear_count, ch_info_ev.cycle_count,
6427 		   ch_info_ev.mac_clk_mhz);
6428 
6429 	if (ch_info_ev.cmd_flags == WMI_CHAN_INFO_END_RESP) {
6430 		ath11k_dbg(ab, ATH11K_DBG_WMI, "chan info report completed\n");
6431 		return;
6432 	}
6433 
6434 	rcu_read_lock();
6435 	ar = ath11k_mac_get_ar_by_vdev_id(ab, ch_info_ev.vdev_id);
6436 	if (!ar) {
6437 		ath11k_warn(ab, "invalid vdev id in chan info ev %d",
6438 			    ch_info_ev.vdev_id);
6439 		rcu_read_unlock();
6440 		return;
6441 	}
6442 	spin_lock_bh(&ar->data_lock);
6443 
6444 	switch (ar->scan.state) {
6445 	case ATH11K_SCAN_IDLE:
6446 	case ATH11K_SCAN_STARTING:
6447 		ath11k_warn(ab, "received chan info event without a scan request, ignoring\n");
6448 		goto exit;
6449 	case ATH11K_SCAN_RUNNING:
6450 	case ATH11K_SCAN_ABORTING:
6451 		break;
6452 	}
6453 
6454 	idx = freq_to_idx(ar, ch_info_ev.freq);
6455 	if (idx >= ARRAY_SIZE(ar->survey)) {
6456 		ath11k_warn(ab, "chan info: invalid frequency %d (idx %d out of bounds)\n",
6457 			    ch_info_ev.freq, idx);
6458 		goto exit;
6459 	}
6460 
6461 	/* If FW provides MAC clock frequency in Mhz, overriding the initialized
6462 	 * HW channel counters frequency value
6463 	 */
6464 	if (ch_info_ev.mac_clk_mhz)
6465 		cc_freq_hz = (ch_info_ev.mac_clk_mhz * 1000);
6466 
6467 	if (ch_info_ev.cmd_flags == WMI_CHAN_INFO_START_RESP) {
6468 		survey = &ar->survey[idx];
6469 		memset(survey, 0, sizeof(*survey));
6470 		survey->noise = ch_info_ev.noise_floor;
6471 		survey->filled = SURVEY_INFO_NOISE_DBM | SURVEY_INFO_TIME |
6472 				 SURVEY_INFO_TIME_BUSY;
6473 		survey->time = div_u64(ch_info_ev.cycle_count, cc_freq_hz);
6474 		survey->time_busy = div_u64(ch_info_ev.rx_clear_count, cc_freq_hz);
6475 	}
6476 exit:
6477 	spin_unlock_bh(&ar->data_lock);
6478 	rcu_read_unlock();
6479 }
6480 
6481 static void
6482 ath11k_pdev_bss_chan_info_event(struct ath11k_base *ab, struct sk_buff *skb)
6483 {
6484 	struct wmi_pdev_bss_chan_info_event bss_ch_info_ev = {};
6485 	struct survey_info *survey;
6486 	struct ath11k *ar;
6487 	u32 cc_freq_hz = ab->cc_freq_hz;
6488 	u64 busy, total, tx, rx, rx_bss;
6489 	int idx;
6490 
6491 	if (ath11k_pull_pdev_bss_chan_info_ev(ab, skb, &bss_ch_info_ev) != 0) {
6492 		ath11k_warn(ab, "failed to extract pdev bss chan info event");
6493 		return;
6494 	}
6495 
6496 	busy = (u64)(bss_ch_info_ev.rx_clear_count_high) << 32 |
6497 			bss_ch_info_ev.rx_clear_count_low;
6498 
6499 	total = (u64)(bss_ch_info_ev.cycle_count_high) << 32 |
6500 			bss_ch_info_ev.cycle_count_low;
6501 
6502 	tx = (u64)(bss_ch_info_ev.tx_cycle_count_high) << 32 |
6503 			bss_ch_info_ev.tx_cycle_count_low;
6504 
6505 	rx = (u64)(bss_ch_info_ev.rx_cycle_count_high) << 32 |
6506 			bss_ch_info_ev.rx_cycle_count_low;
6507 
6508 	rx_bss = (u64)(bss_ch_info_ev.rx_bss_cycle_count_high) << 32 |
6509 			bss_ch_info_ev.rx_bss_cycle_count_low;
6510 
6511 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6512 		   "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",
6513 		   bss_ch_info_ev.pdev_id, bss_ch_info_ev.freq,
6514 		   bss_ch_info_ev.noise_floor, busy, total,
6515 		   tx, rx, rx_bss);
6516 
6517 	rcu_read_lock();
6518 	ar = ath11k_mac_get_ar_by_pdev_id(ab, bss_ch_info_ev.pdev_id);
6519 
6520 	if (!ar) {
6521 		ath11k_warn(ab, "invalid pdev id %d in bss_chan_info event\n",
6522 			    bss_ch_info_ev.pdev_id);
6523 		rcu_read_unlock();
6524 		return;
6525 	}
6526 
6527 	spin_lock_bh(&ar->data_lock);
6528 	idx = freq_to_idx(ar, bss_ch_info_ev.freq);
6529 	if (idx >= ARRAY_SIZE(ar->survey)) {
6530 		ath11k_warn(ab, "bss chan info: invalid frequency %d (idx %d out of bounds)\n",
6531 			    bss_ch_info_ev.freq, idx);
6532 		goto exit;
6533 	}
6534 
6535 	survey = &ar->survey[idx];
6536 
6537 	survey->noise     = bss_ch_info_ev.noise_floor;
6538 	survey->time      = div_u64(total, cc_freq_hz);
6539 	survey->time_busy = div_u64(busy, cc_freq_hz);
6540 	survey->time_rx   = div_u64(rx_bss, cc_freq_hz);
6541 	survey->time_tx   = div_u64(tx, cc_freq_hz);
6542 	survey->filled   |= (SURVEY_INFO_NOISE_DBM |
6543 			     SURVEY_INFO_TIME |
6544 			     SURVEY_INFO_TIME_BUSY |
6545 			     SURVEY_INFO_TIME_RX |
6546 			     SURVEY_INFO_TIME_TX);
6547 exit:
6548 	spin_unlock_bh(&ar->data_lock);
6549 	complete(&ar->bss_survey_done);
6550 
6551 	rcu_read_unlock();
6552 }
6553 
6554 static void ath11k_vdev_install_key_compl_event(struct ath11k_base *ab,
6555 						struct sk_buff *skb)
6556 {
6557 	struct wmi_vdev_install_key_complete_arg install_key_compl = {0};
6558 	struct ath11k *ar;
6559 
6560 	if (ath11k_pull_vdev_install_key_compl_ev(ab, skb, &install_key_compl) != 0) {
6561 		ath11k_warn(ab, "failed to extract install key compl event");
6562 		return;
6563 	}
6564 
6565 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6566 		   "vdev install key ev idx %d flags %08x macaddr %pM status %d\n",
6567 		   install_key_compl.key_idx, install_key_compl.key_flags,
6568 		   install_key_compl.macaddr, install_key_compl.status);
6569 
6570 	rcu_read_lock();
6571 	ar = ath11k_mac_get_ar_by_vdev_id(ab, install_key_compl.vdev_id);
6572 	if (!ar) {
6573 		ath11k_warn(ab, "invalid vdev id in install key compl ev %d",
6574 			    install_key_compl.vdev_id);
6575 		rcu_read_unlock();
6576 		return;
6577 	}
6578 
6579 	ar->install_key_status = 0;
6580 
6581 	if (install_key_compl.status != WMI_VDEV_INSTALL_KEY_COMPL_STATUS_SUCCESS) {
6582 		ath11k_warn(ab, "install key failed for %pM status %d\n",
6583 			    install_key_compl.macaddr, install_key_compl.status);
6584 		ar->install_key_status = install_key_compl.status;
6585 	}
6586 
6587 	complete(&ar->install_key_done);
6588 	rcu_read_unlock();
6589 }
6590 
6591 static void ath11k_service_available_event(struct ath11k_base *ab, struct sk_buff *skb)
6592 {
6593 	const void **tb;
6594 	const struct wmi_service_available_event *ev;
6595 	int ret;
6596 	int i, j;
6597 
6598 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6599 	if (IS_ERR(tb)) {
6600 		ret = PTR_ERR(tb);
6601 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6602 		return;
6603 	}
6604 
6605 	ev = tb[WMI_TAG_SERVICE_AVAILABLE_EVENT];
6606 	if (!ev) {
6607 		ath11k_warn(ab, "failed to fetch svc available ev");
6608 		kfree(tb);
6609 		return;
6610 	}
6611 
6612 	/* TODO: Use wmi_service_segment_offset information to get the service
6613 	 * especially when more services are advertised in multiple sevice
6614 	 * available events.
6615 	 */
6616 	for (i = 0, j = WMI_MAX_SERVICE;
6617 	     i < WMI_SERVICE_SEGMENT_BM_SIZE32 && j < WMI_MAX_EXT_SERVICE;
6618 	     i++) {
6619 		do {
6620 			if (ev->wmi_service_segment_bitmap[i] &
6621 			    BIT(j % WMI_AVAIL_SERVICE_BITS_IN_SIZE32))
6622 				set_bit(j, ab->wmi_ab.svc_map);
6623 		} while (++j % WMI_AVAIL_SERVICE_BITS_IN_SIZE32);
6624 	}
6625 
6626 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6627 		   "wmi_ext_service_bitmap 0:0x%x, 1:0x%x, 2:0x%x, 3:0x%x",
6628 		   ev->wmi_service_segment_bitmap[0], ev->wmi_service_segment_bitmap[1],
6629 		   ev->wmi_service_segment_bitmap[2], ev->wmi_service_segment_bitmap[3]);
6630 
6631 	kfree(tb);
6632 }
6633 
6634 static void ath11k_peer_assoc_conf_event(struct ath11k_base *ab, struct sk_buff *skb)
6635 {
6636 	struct wmi_peer_assoc_conf_arg peer_assoc_conf = {0};
6637 	struct ath11k *ar;
6638 
6639 	if (ath11k_pull_peer_assoc_conf_ev(ab, skb, &peer_assoc_conf) != 0) {
6640 		ath11k_warn(ab, "failed to extract peer assoc conf event");
6641 		return;
6642 	}
6643 
6644 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6645 		   "peer assoc conf ev vdev id %d macaddr %pM\n",
6646 		   peer_assoc_conf.vdev_id, peer_assoc_conf.macaddr);
6647 
6648 	rcu_read_lock();
6649 	ar = ath11k_mac_get_ar_by_vdev_id(ab, peer_assoc_conf.vdev_id);
6650 
6651 	if (!ar) {
6652 		ath11k_warn(ab, "invalid vdev id in peer assoc conf ev %d",
6653 			    peer_assoc_conf.vdev_id);
6654 		rcu_read_unlock();
6655 		return;
6656 	}
6657 
6658 	complete(&ar->peer_assoc_done);
6659 	rcu_read_unlock();
6660 }
6661 
6662 static void ath11k_update_stats_event(struct ath11k_base *ab, struct sk_buff *skb)
6663 {
6664 	ath11k_debugfs_fw_stats_process(ab, skb);
6665 }
6666 
6667 /* PDEV_CTL_FAILSAFE_CHECK_EVENT is received from FW when the frequency scanned
6668  * is not part of BDF CTL(Conformance test limits) table entries.
6669  */
6670 static void ath11k_pdev_ctl_failsafe_check_event(struct ath11k_base *ab,
6671 						 struct sk_buff *skb)
6672 {
6673 	const void **tb;
6674 	const struct wmi_pdev_ctl_failsafe_chk_event *ev;
6675 	int ret;
6676 
6677 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6678 	if (IS_ERR(tb)) {
6679 		ret = PTR_ERR(tb);
6680 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6681 		return;
6682 	}
6683 
6684 	ev = tb[WMI_TAG_PDEV_CTL_FAILSAFE_CHECK_EVENT];
6685 	if (!ev) {
6686 		ath11k_warn(ab, "failed to fetch pdev ctl failsafe check ev");
6687 		kfree(tb);
6688 		return;
6689 	}
6690 
6691 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6692 		   "pdev ctl failsafe check ev status %d\n",
6693 		   ev->ctl_failsafe_status);
6694 
6695 	/* If ctl_failsafe_status is set to 1 FW will max out the Transmit power
6696 	 * to 10 dBm else the CTL power entry in the BDF would be picked up.
6697 	 */
6698 	if (ev->ctl_failsafe_status != 0)
6699 		ath11k_warn(ab, "pdev ctl failsafe failure status %d",
6700 			    ev->ctl_failsafe_status);
6701 
6702 	kfree(tb);
6703 }
6704 
6705 static void
6706 ath11k_wmi_process_csa_switch_count_event(struct ath11k_base *ab,
6707 					  const struct wmi_pdev_csa_switch_ev *ev,
6708 					  const u32 *vdev_ids)
6709 {
6710 	int i;
6711 	struct ath11k_vif *arvif;
6712 
6713 	/* Finish CSA once the switch count becomes NULL */
6714 	if (ev->current_switch_count)
6715 		return;
6716 
6717 	rcu_read_lock();
6718 	for (i = 0; i < ev->num_vdevs; i++) {
6719 		arvif = ath11k_mac_get_arvif_by_vdev_id(ab, vdev_ids[i]);
6720 
6721 		if (!arvif) {
6722 			ath11k_warn(ab, "Recvd csa status for unknown vdev %d",
6723 				    vdev_ids[i]);
6724 			continue;
6725 		}
6726 
6727 		if (arvif->is_up && arvif->vif->csa_active)
6728 			ieee80211_csa_finish(arvif->vif);
6729 	}
6730 	rcu_read_unlock();
6731 }
6732 
6733 static void
6734 ath11k_wmi_pdev_csa_switch_count_status_event(struct ath11k_base *ab,
6735 					      struct sk_buff *skb)
6736 {
6737 	const void **tb;
6738 	const struct wmi_pdev_csa_switch_ev *ev;
6739 	const u32 *vdev_ids;
6740 	int ret;
6741 
6742 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6743 	if (IS_ERR(tb)) {
6744 		ret = PTR_ERR(tb);
6745 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6746 		return;
6747 	}
6748 
6749 	ev = tb[WMI_TAG_PDEV_CSA_SWITCH_COUNT_STATUS_EVENT];
6750 	vdev_ids = tb[WMI_TAG_ARRAY_UINT32];
6751 
6752 	if (!ev || !vdev_ids) {
6753 		ath11k_warn(ab, "failed to fetch pdev csa switch count ev");
6754 		kfree(tb);
6755 		return;
6756 	}
6757 
6758 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6759 		   "pdev csa switch count %d for pdev %d, num_vdevs %d",
6760 		   ev->current_switch_count, ev->pdev_id,
6761 		   ev->num_vdevs);
6762 
6763 	ath11k_wmi_process_csa_switch_count_event(ab, ev, vdev_ids);
6764 
6765 	kfree(tb);
6766 }
6767 
6768 static void
6769 ath11k_wmi_pdev_dfs_radar_detected_event(struct ath11k_base *ab, struct sk_buff *skb)
6770 {
6771 	const void **tb;
6772 	const struct wmi_pdev_radar_ev *ev;
6773 	struct ath11k *ar;
6774 	int ret;
6775 
6776 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6777 	if (IS_ERR(tb)) {
6778 		ret = PTR_ERR(tb);
6779 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6780 		return;
6781 	}
6782 
6783 	ev = tb[WMI_TAG_PDEV_DFS_RADAR_DETECTION_EVENT];
6784 
6785 	if (!ev) {
6786 		ath11k_warn(ab, "failed to fetch pdev dfs radar detected ev");
6787 		kfree(tb);
6788 		return;
6789 	}
6790 
6791 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6792 		   "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",
6793 		   ev->pdev_id, ev->detection_mode, ev->chan_freq, ev->chan_width,
6794 		   ev->detector_id, ev->segment_id, ev->timestamp, ev->is_chirp,
6795 		   ev->freq_offset, ev->sidx);
6796 
6797 	ar = ath11k_mac_get_ar_by_pdev_id(ab, ev->pdev_id);
6798 
6799 	if (!ar) {
6800 		ath11k_warn(ab, "radar detected in invalid pdev %d\n",
6801 			    ev->pdev_id);
6802 		goto exit;
6803 	}
6804 
6805 	ath11k_dbg(ar->ab, ATH11K_DBG_REG, "DFS Radar Detected in pdev %d\n",
6806 		   ev->pdev_id);
6807 
6808 	if (ar->dfs_block_radar_events)
6809 		ath11k_info(ab, "DFS Radar detected, but ignored as requested\n");
6810 	else
6811 		ieee80211_radar_detected(ar->hw);
6812 
6813 exit:
6814 	kfree(tb);
6815 }
6816 
6817 static void
6818 ath11k_wmi_pdev_temperature_event(struct ath11k_base *ab,
6819 				  struct sk_buff *skb)
6820 {
6821 	struct ath11k *ar;
6822 	const void **tb;
6823 	const struct wmi_pdev_temperature_event *ev;
6824 	int ret;
6825 
6826 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6827 	if (IS_ERR(tb)) {
6828 		ret = PTR_ERR(tb);
6829 		ath11k_warn(ab, "failed to parse tlv: %d\n", ret);
6830 		return;
6831 	}
6832 
6833 	ev = tb[WMI_TAG_PDEV_TEMPERATURE_EVENT];
6834 	if (!ev) {
6835 		ath11k_warn(ab, "failed to fetch pdev temp ev");
6836 		kfree(tb);
6837 		return;
6838 	}
6839 
6840 	ath11k_dbg(ab, ATH11K_DBG_WMI,
6841 		   "pdev temperature ev temp %d pdev_id %d\n", ev->temp, ev->pdev_id);
6842 
6843 	ar = ath11k_mac_get_ar_by_pdev_id(ab, ev->pdev_id);
6844 	if (!ar) {
6845 		ath11k_warn(ab, "invalid pdev id in pdev temperature ev %d", ev->pdev_id);
6846 		kfree(tb);
6847 		return;
6848 	}
6849 
6850 	ath11k_thermal_event_temperature(ar, ev->temp);
6851 
6852 	kfree(tb);
6853 }
6854 
6855 static void ath11k_fils_discovery_event(struct ath11k_base *ab,
6856 					struct sk_buff *skb)
6857 {
6858 	const void **tb;
6859 	const struct wmi_fils_discovery_event *ev;
6860 	int ret;
6861 
6862 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6863 	if (IS_ERR(tb)) {
6864 		ret = PTR_ERR(tb);
6865 		ath11k_warn(ab,
6866 			    "failed to parse FILS discovery event tlv %d\n",
6867 			    ret);
6868 		return;
6869 	}
6870 
6871 	ev = tb[WMI_TAG_HOST_SWFDA_EVENT];
6872 	if (!ev) {
6873 		ath11k_warn(ab, "failed to fetch FILS discovery event\n");
6874 		kfree(tb);
6875 		return;
6876 	}
6877 
6878 	ath11k_warn(ab,
6879 		    "FILS discovery frame expected from host for vdev_id: %u, transmission scheduled at %u, next TBTT: %u\n",
6880 		    ev->vdev_id, ev->fils_tt, ev->tbtt);
6881 
6882 	kfree(tb);
6883 }
6884 
6885 static void ath11k_probe_resp_tx_status_event(struct ath11k_base *ab,
6886 					      struct sk_buff *skb)
6887 {
6888 	const void **tb;
6889 	const struct wmi_probe_resp_tx_status_event *ev;
6890 	int ret;
6891 
6892 	tb = ath11k_wmi_tlv_parse_alloc(ab, skb->data, skb->len, GFP_ATOMIC);
6893 	if (IS_ERR(tb)) {
6894 		ret = PTR_ERR(tb);
6895 		ath11k_warn(ab,
6896 			    "failed to parse probe response transmission status event tlv: %d\n",
6897 			    ret);
6898 		return;
6899 	}
6900 
6901 	ev = tb[WMI_TAG_OFFLOAD_PRB_RSP_TX_STATUS_EVENT];
6902 	if (!ev) {
6903 		ath11k_warn(ab,
6904 			    "failed to fetch probe response transmission status event");
6905 		kfree(tb);
6906 		return;
6907 	}
6908 
6909 	if (ev->tx_status)
6910 		ath11k_warn(ab,
6911 			    "Probe response transmission failed for vdev_id %u, status %u\n",
6912 			    ev->vdev_id, ev->tx_status);
6913 
6914 	kfree(tb);
6915 }
6916 
6917 static int ath11k_wmi_tlv_wow_wakeup_host_parse(struct ath11k_base *ab,
6918 						u16 tag, u16 len,
6919 						const void *ptr, void *data)
6920 {
6921 	struct wmi_wow_ev_arg *ev = data;
6922 	const char *wow_pg_fault;
6923 	int wow_pg_len;
6924 
6925 	switch (tag) {
6926 	case WMI_TAG_WOW_EVENT_INFO:
6927 		memcpy(ev, ptr, sizeof(*ev));
6928 		ath11k_dbg(ab, ATH11K_DBG_WMI, "wow wakeup host reason %d %s\n",
6929 			   ev->wake_reason, wow_reason(ev->wake_reason));
6930 		break;
6931 
6932 	case WMI_TAG_ARRAY_BYTE:
6933 		if (ev && ev->wake_reason == WOW_REASON_PAGE_FAULT) {
6934 			wow_pg_fault = ptr;
6935 			/* the first 4 bytes are length */
6936 			wow_pg_len = *(int *)wow_pg_fault;
6937 			wow_pg_fault += sizeof(int);
6938 			ath11k_dbg(ab, ATH11K_DBG_WMI, "wow data_len = %d\n",
6939 				   wow_pg_len);
6940 			ath11k_dbg_dump(ab, ATH11K_DBG_WMI,
6941 					"wow_event_info_type packet present",
6942 					"wow_pg_fault ",
6943 					wow_pg_fault,
6944 					wow_pg_len);
6945 		}
6946 		break;
6947 	default:
6948 		break;
6949 	}
6950 
6951 	return 0;
6952 }
6953 
6954 static void ath11k_wmi_event_wow_wakeup_host(struct ath11k_base *ab, struct sk_buff *skb)
6955 {
6956 	struct wmi_wow_ev_arg ev = { };
6957 	int ret;
6958 
6959 	ret = ath11k_wmi_tlv_iter(ab, skb->data, skb->len,
6960 				  ath11k_wmi_tlv_wow_wakeup_host_parse,
6961 				  &ev);
6962 	if (ret) {
6963 		ath11k_warn(ab, "failed to parse wmi wow tlv: %d\n", ret);
6964 		return;
6965 	}
6966 
6967 	complete(&ab->wow.wakeup_completed);
6968 }
6969 
6970 static void ath11k_wmi_tlv_op_rx(struct ath11k_base *ab, struct sk_buff *skb)
6971 {
6972 	struct wmi_cmd_hdr *cmd_hdr;
6973 	enum wmi_tlv_event_id id;
6974 
6975 	cmd_hdr = (struct wmi_cmd_hdr *)skb->data;
6976 	id = FIELD_GET(WMI_CMD_HDR_CMD_ID, (cmd_hdr->cmd_id));
6977 
6978 	if (skb_pull(skb, sizeof(struct wmi_cmd_hdr)) == NULL)
6979 		goto out;
6980 
6981 	switch (id) {
6982 		/* Process all the WMI events here */
6983 	case WMI_SERVICE_READY_EVENTID:
6984 		ath11k_service_ready_event(ab, skb);
6985 		break;
6986 	case WMI_SERVICE_READY_EXT_EVENTID:
6987 		ath11k_service_ready_ext_event(ab, skb);
6988 		break;
6989 	case WMI_SERVICE_READY_EXT2_EVENTID:
6990 		ath11k_service_ready_ext2_event(ab, skb);
6991 		break;
6992 	case WMI_REG_CHAN_LIST_CC_EVENTID:
6993 		ath11k_reg_chan_list_event(ab, skb);
6994 		break;
6995 	case WMI_READY_EVENTID:
6996 		ath11k_ready_event(ab, skb);
6997 		break;
6998 	case WMI_PEER_DELETE_RESP_EVENTID:
6999 		ath11k_peer_delete_resp_event(ab, skb);
7000 		break;
7001 	case WMI_VDEV_START_RESP_EVENTID:
7002 		ath11k_vdev_start_resp_event(ab, skb);
7003 		break;
7004 	case WMI_OFFLOAD_BCN_TX_STATUS_EVENTID:
7005 		ath11k_bcn_tx_status_event(ab, skb);
7006 		break;
7007 	case WMI_VDEV_STOPPED_EVENTID:
7008 		ath11k_vdev_stopped_event(ab, skb);
7009 		break;
7010 	case WMI_MGMT_RX_EVENTID:
7011 		ath11k_mgmt_rx_event(ab, skb);
7012 		/* mgmt_rx_event() owns the skb now! */
7013 		return;
7014 	case WMI_MGMT_TX_COMPLETION_EVENTID:
7015 		ath11k_mgmt_tx_compl_event(ab, skb);
7016 		break;
7017 	case WMI_SCAN_EVENTID:
7018 		ath11k_scan_event(ab, skb);
7019 		break;
7020 	case WMI_PEER_STA_KICKOUT_EVENTID:
7021 		ath11k_peer_sta_kickout_event(ab, skb);
7022 		break;
7023 	case WMI_ROAM_EVENTID:
7024 		ath11k_roam_event(ab, skb);
7025 		break;
7026 	case WMI_CHAN_INFO_EVENTID:
7027 		ath11k_chan_info_event(ab, skb);
7028 		break;
7029 	case WMI_PDEV_BSS_CHAN_INFO_EVENTID:
7030 		ath11k_pdev_bss_chan_info_event(ab, skb);
7031 		break;
7032 	case WMI_VDEV_INSTALL_KEY_COMPLETE_EVENTID:
7033 		ath11k_vdev_install_key_compl_event(ab, skb);
7034 		break;
7035 	case WMI_SERVICE_AVAILABLE_EVENTID:
7036 		ath11k_service_available_event(ab, skb);
7037 		break;
7038 	case WMI_PEER_ASSOC_CONF_EVENTID:
7039 		ath11k_peer_assoc_conf_event(ab, skb);
7040 		break;
7041 	case WMI_UPDATE_STATS_EVENTID:
7042 		ath11k_update_stats_event(ab, skb);
7043 		break;
7044 	case WMI_PDEV_CTL_FAILSAFE_CHECK_EVENTID:
7045 		ath11k_pdev_ctl_failsafe_check_event(ab, skb);
7046 		break;
7047 	case WMI_PDEV_CSA_SWITCH_COUNT_STATUS_EVENTID:
7048 		ath11k_wmi_pdev_csa_switch_count_status_event(ab, skb);
7049 		break;
7050 	case WMI_PDEV_TEMPERATURE_EVENTID:
7051 		ath11k_wmi_pdev_temperature_event(ab, skb);
7052 		break;
7053 	case WMI_PDEV_DMA_RING_BUF_RELEASE_EVENTID:
7054 		ath11k_wmi_pdev_dma_ring_buf_release_event(ab, skb);
7055 		break;
7056 	case WMI_HOST_FILS_DISCOVERY_EVENTID:
7057 		ath11k_fils_discovery_event(ab, skb);
7058 		break;
7059 	case WMI_OFFLOAD_PROB_RESP_TX_STATUS_EVENTID:
7060 		ath11k_probe_resp_tx_status_event(ab, skb);
7061 		break;
7062 	/* add Unsupported events here */
7063 	case WMI_TBTTOFFSET_EXT_UPDATE_EVENTID:
7064 	case WMI_PEER_OPER_MODE_CHANGE_EVENTID:
7065 	case WMI_TWT_ENABLE_EVENTID:
7066 	case WMI_TWT_DISABLE_EVENTID:
7067 	case WMI_PDEV_DMA_RING_CFG_RSP_EVENTID:
7068 		ath11k_dbg(ab, ATH11K_DBG_WMI,
7069 			   "ignoring unsupported event 0x%x\n", id);
7070 		break;
7071 	case WMI_PDEV_DFS_RADAR_DETECTION_EVENTID:
7072 		ath11k_wmi_pdev_dfs_radar_detected_event(ab, skb);
7073 		break;
7074 	case WMI_VDEV_DELETE_RESP_EVENTID:
7075 		ath11k_vdev_delete_resp_event(ab, skb);
7076 		break;
7077 	case WMI_WOW_WAKEUP_HOST_EVENTID:
7078 		ath11k_wmi_event_wow_wakeup_host(ab, skb);
7079 		break;
7080 	/* TODO: Add remaining events */
7081 	default:
7082 		ath11k_dbg(ab, ATH11K_DBG_WMI, "Unknown eventid: 0x%x\n", id);
7083 		break;
7084 	}
7085 
7086 out:
7087 	dev_kfree_skb(skb);
7088 }
7089 
7090 static int ath11k_connect_pdev_htc_service(struct ath11k_base *ab,
7091 					   u32 pdev_idx)
7092 {
7093 	int status;
7094 	u32 svc_id[] = { ATH11K_HTC_SVC_ID_WMI_CONTROL,
7095 			 ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC1,
7096 			 ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC2 };
7097 
7098 	struct ath11k_htc_svc_conn_req conn_req;
7099 	struct ath11k_htc_svc_conn_resp conn_resp;
7100 
7101 	memset(&conn_req, 0, sizeof(conn_req));
7102 	memset(&conn_resp, 0, sizeof(conn_resp));
7103 
7104 	/* these fields are the same for all service endpoints */
7105 	conn_req.ep_ops.ep_tx_complete = ath11k_wmi_htc_tx_complete;
7106 	conn_req.ep_ops.ep_rx_complete = ath11k_wmi_tlv_op_rx;
7107 	conn_req.ep_ops.ep_tx_credits = ath11k_wmi_op_ep_tx_credits;
7108 
7109 	/* connect to control service */
7110 	conn_req.service_id = svc_id[pdev_idx];
7111 
7112 	status = ath11k_htc_connect_service(&ab->htc, &conn_req, &conn_resp);
7113 	if (status) {
7114 		ath11k_warn(ab, "failed to connect to WMI CONTROL service status: %d\n",
7115 			    status);
7116 		return status;
7117 	}
7118 
7119 	ab->wmi_ab.wmi_endpoint_id[pdev_idx] = conn_resp.eid;
7120 	ab->wmi_ab.wmi[pdev_idx].eid = conn_resp.eid;
7121 	ab->wmi_ab.max_msg_len[pdev_idx] = conn_resp.max_msg_len;
7122 
7123 	return 0;
7124 }
7125 
7126 static int
7127 ath11k_wmi_send_unit_test_cmd(struct ath11k *ar,
7128 			      struct wmi_unit_test_cmd ut_cmd,
7129 			      u32 *test_args)
7130 {
7131 	struct ath11k_pdev_wmi *wmi = ar->wmi;
7132 	struct wmi_unit_test_cmd *cmd;
7133 	struct sk_buff *skb;
7134 	struct wmi_tlv *tlv;
7135 	void *ptr;
7136 	u32 *ut_cmd_args;
7137 	int buf_len, arg_len;
7138 	int ret;
7139 	int i;
7140 
7141 	arg_len = sizeof(u32) * ut_cmd.num_args;
7142 	buf_len = sizeof(ut_cmd) + arg_len + TLV_HDR_SIZE;
7143 
7144 	skb = ath11k_wmi_alloc_skb(wmi->wmi_ab, buf_len);
7145 	if (!skb)
7146 		return -ENOMEM;
7147 
7148 	cmd = (struct wmi_unit_test_cmd *)skb->data;
7149 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_UNIT_TEST_CMD) |
7150 			  FIELD_PREP(WMI_TLV_LEN, sizeof(ut_cmd) - TLV_HDR_SIZE);
7151 
7152 	cmd->vdev_id = ut_cmd.vdev_id;
7153 	cmd->module_id = ut_cmd.module_id;
7154 	cmd->num_args = ut_cmd.num_args;
7155 	cmd->diag_token = ut_cmd.diag_token;
7156 
7157 	ptr = skb->data + sizeof(ut_cmd);
7158 
7159 	tlv = ptr;
7160 	tlv->header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_ARRAY_UINT32) |
7161 		      FIELD_PREP(WMI_TLV_LEN, arg_len);
7162 
7163 	ptr += TLV_HDR_SIZE;
7164 
7165 	ut_cmd_args = ptr;
7166 	for (i = 0; i < ut_cmd.num_args; i++)
7167 		ut_cmd_args[i] = test_args[i];
7168 
7169 	ret = ath11k_wmi_cmd_send(wmi, skb, WMI_UNIT_TEST_CMDID);
7170 
7171 	if (ret) {
7172 		ath11k_warn(ar->ab, "failed to send WMI_UNIT_TEST CMD :%d\n",
7173 			    ret);
7174 		dev_kfree_skb(skb);
7175 	}
7176 
7177 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
7178 		   "WMI unit test : module %d vdev %d n_args %d token %d\n",
7179 		   cmd->module_id, cmd->vdev_id, cmd->num_args,
7180 		   cmd->diag_token);
7181 
7182 	return ret;
7183 }
7184 
7185 int ath11k_wmi_simulate_radar(struct ath11k *ar)
7186 {
7187 	struct ath11k_vif *arvif;
7188 	u32 dfs_args[DFS_MAX_TEST_ARGS];
7189 	struct wmi_unit_test_cmd wmi_ut;
7190 	bool arvif_found = false;
7191 
7192 	list_for_each_entry(arvif, &ar->arvifs, list) {
7193 		if (arvif->is_started && arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7194 			arvif_found = true;
7195 			break;
7196 		}
7197 	}
7198 
7199 	if (!arvif_found)
7200 		return -EINVAL;
7201 
7202 	dfs_args[DFS_TEST_CMDID] = 0;
7203 	dfs_args[DFS_TEST_PDEV_ID] = ar->pdev->pdev_id;
7204 	/* Currently we could pass segment_id(b0 - b1), chirp(b2)
7205 	 * freq offset (b3 - b10) to unit test. For simulation
7206 	 * purpose this can be set to 0 which is valid.
7207 	 */
7208 	dfs_args[DFS_TEST_RADAR_PARAM] = 0;
7209 
7210 	wmi_ut.vdev_id = arvif->vdev_id;
7211 	wmi_ut.module_id = DFS_UNIT_TEST_MODULE;
7212 	wmi_ut.num_args = DFS_MAX_TEST_ARGS;
7213 	wmi_ut.diag_token = DFS_UNIT_TEST_TOKEN;
7214 
7215 	ath11k_dbg(ar->ab, ATH11K_DBG_REG, "Triggering Radar Simulation\n");
7216 
7217 	return ath11k_wmi_send_unit_test_cmd(ar, wmi_ut, dfs_args);
7218 }
7219 
7220 int ath11k_wmi_connect(struct ath11k_base *ab)
7221 {
7222 	u32 i;
7223 	u8 wmi_ep_count;
7224 
7225 	wmi_ep_count = ab->htc.wmi_ep_count;
7226 	if (wmi_ep_count > ab->hw_params.max_radios)
7227 		return -1;
7228 
7229 	for (i = 0; i < wmi_ep_count; i++)
7230 		ath11k_connect_pdev_htc_service(ab, i);
7231 
7232 	return 0;
7233 }
7234 
7235 static void ath11k_wmi_pdev_detach(struct ath11k_base *ab, u8 pdev_id)
7236 {
7237 	if (WARN_ON(pdev_id >= MAX_RADIOS))
7238 		return;
7239 
7240 	/* TODO: Deinit any pdev specific wmi resource */
7241 }
7242 
7243 int ath11k_wmi_pdev_attach(struct ath11k_base *ab,
7244 			   u8 pdev_id)
7245 {
7246 	struct ath11k_pdev_wmi *wmi_handle;
7247 
7248 	if (pdev_id >= ab->hw_params.max_radios)
7249 		return -EINVAL;
7250 
7251 	wmi_handle = &ab->wmi_ab.wmi[pdev_id];
7252 
7253 	wmi_handle->wmi_ab = &ab->wmi_ab;
7254 
7255 	ab->wmi_ab.ab = ab;
7256 	/* TODO: Init remaining resource specific to pdev */
7257 
7258 	return 0;
7259 }
7260 
7261 int ath11k_wmi_attach(struct ath11k_base *ab)
7262 {
7263 	int ret;
7264 
7265 	ret = ath11k_wmi_pdev_attach(ab, 0);
7266 	if (ret)
7267 		return ret;
7268 
7269 	ab->wmi_ab.ab = ab;
7270 	ab->wmi_ab.preferred_hw_mode = WMI_HOST_HW_MODE_MAX;
7271 
7272 	/* It's overwritten when service_ext_ready is handled */
7273 	if (ab->hw_params.single_pdev_only)
7274 		ab->wmi_ab.preferred_hw_mode = WMI_HOST_HW_MODE_SINGLE;
7275 
7276 	/* TODO: Init remaining wmi soc resources required */
7277 	init_completion(&ab->wmi_ab.service_ready);
7278 	init_completion(&ab->wmi_ab.unified_ready);
7279 
7280 	return 0;
7281 }
7282 
7283 void ath11k_wmi_detach(struct ath11k_base *ab)
7284 {
7285 	int i;
7286 
7287 	/* TODO: Deinit wmi resource specific to SOC as required */
7288 
7289 	for (i = 0; i < ab->htc.wmi_ep_count; i++)
7290 		ath11k_wmi_pdev_detach(ab, i);
7291 
7292 	ath11k_wmi_free_dbring_caps(ab);
7293 }
7294 
7295 int ath11k_wmi_wow_host_wakeup_ind(struct ath11k *ar)
7296 {
7297 	struct wmi_wow_host_wakeup_ind *cmd;
7298 	struct sk_buff *skb;
7299 	size_t len;
7300 
7301 	len = sizeof(*cmd);
7302 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
7303 	if (!skb)
7304 		return -ENOMEM;
7305 
7306 	cmd = (struct wmi_wow_host_wakeup_ind *)skb->data;
7307 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG,
7308 				     WMI_TAG_WOW_HOSTWAKEUP_FROM_SLEEP_CMD) |
7309 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
7310 
7311 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "wmi tlv wow host wakeup ind\n");
7312 
7313 	return ath11k_wmi_cmd_send(ar->wmi, skb, WMI_WOW_HOSTWAKEUP_FROM_SLEEP_CMDID);
7314 }
7315 
7316 int ath11k_wmi_wow_enable(struct ath11k *ar)
7317 {
7318 	struct wmi_wow_enable_cmd *cmd;
7319 	struct sk_buff *skb;
7320 	int len;
7321 
7322 	len = sizeof(*cmd);
7323 	skb = ath11k_wmi_alloc_skb(ar->wmi->wmi_ab, len);
7324 	if (!skb)
7325 		return -ENOMEM;
7326 
7327 	cmd = (struct wmi_wow_enable_cmd *)skb->data;
7328 	cmd->tlv_header = FIELD_PREP(WMI_TLV_TAG, WMI_TAG_WOW_ENABLE_CMD) |
7329 			  FIELD_PREP(WMI_TLV_LEN, sizeof(*cmd) - TLV_HDR_SIZE);
7330 
7331 	cmd->enable = 1;
7332 	cmd->pause_iface_config = WOW_IFACE_PAUSE_ENABLED;
7333 	ath11k_dbg(ar->ab, ATH11K_DBG_WMI, "wmi tlv wow enable\n");
7334 
7335 	return ath11k_wmi_cmd_send(ar->wmi, skb, WMI_WOW_ENABLE_CMDID);
7336 }
7337