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