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