xref: /linux/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.c (revision 16e5ac127d8d18adf85fe5ba847d77b58d1ed418)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/firmware.h>
5 #include "mt76_connac2_mac.h"
6 #include "mt76_connac_mcu.h"
7 
8 int mt76_connac_mcu_start_firmware(struct mt76_dev *dev, u32 addr, u32 option)
9 {
10 	struct {
11 		__le32 option;
12 		__le32 addr;
13 	} req = {
14 		.option = cpu_to_le32(option),
15 		.addr = cpu_to_le32(addr),
16 	};
17 
18 	return mt76_mcu_send_msg(dev, MCU_CMD(FW_START_REQ), &req,
19 				 sizeof(req), true);
20 }
21 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_firmware);
22 
23 int mt76_connac_mcu_patch_sem_ctrl(struct mt76_dev *dev, bool get)
24 {
25 	u32 op = get ? PATCH_SEM_GET : PATCH_SEM_RELEASE;
26 	struct {
27 		__le32 op;
28 	} req = {
29 		.op = cpu_to_le32(op),
30 	};
31 
32 	return mt76_mcu_send_msg(dev, MCU_CMD(PATCH_SEM_CONTROL),
33 				 &req, sizeof(req), true);
34 }
35 EXPORT_SYMBOL_GPL(mt76_connac_mcu_patch_sem_ctrl);
36 
37 int mt76_connac_mcu_start_patch(struct mt76_dev *dev)
38 {
39 	struct {
40 		u8 check_crc;
41 		u8 reserved[3];
42 	} req = {
43 		.check_crc = 0,
44 	};
45 
46 	return mt76_mcu_send_msg(dev, MCU_CMD(PATCH_FINISH_REQ),
47 				 &req, sizeof(req), true);
48 }
49 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_patch);
50 
51 #define MCU_PATCH_ADDRESS	0x200000
52 
53 int mt76_connac_mcu_init_download(struct mt76_dev *dev, u32 addr, u32 len,
54 				  u32 mode)
55 {
56 	struct {
57 		__le32 addr;
58 		__le32 len;
59 		__le32 mode;
60 	} req = {
61 		.addr = cpu_to_le32(addr),
62 		.len = cpu_to_le32(len),
63 		.mode = cpu_to_le32(mode),
64 	};
65 	int cmd;
66 
67 	if ((!is_connac_v1(dev) && addr == MCU_PATCH_ADDRESS) ||
68 	    (is_mt7921(dev) && addr == 0x900000) ||
69 	    (is_mt7925(dev) && addr == 0x900000) ||
70 	    (is_mt7996(dev) && addr == 0x900000))
71 		cmd = MCU_CMD(PATCH_START_REQ);
72 	else
73 		cmd = MCU_CMD(TARGET_ADDRESS_LEN_REQ);
74 
75 	return mt76_mcu_send_msg(dev, cmd, &req, sizeof(req), true);
76 }
77 EXPORT_SYMBOL_GPL(mt76_connac_mcu_init_download);
78 
79 int mt76_connac_mcu_set_channel_domain(struct mt76_phy *phy)
80 {
81 	int len, i, n_max_channels, n_2ch = 0, n_5ch = 0, n_6ch = 0;
82 	struct mt76_connac_mcu_channel_domain {
83 		u8 alpha2[4]; /* regulatory_request.alpha2 */
84 		u8 bw_2g; /* BW_20_40M		0
85 			   * BW_20M		1
86 			   * BW_20_40_80M	2
87 			   * BW_20_40_80_160M	3
88 			   * BW_20_40_80_8080M	4
89 			   */
90 		u8 bw_5g;
91 		u8 bw_6g;
92 		u8 pad;
93 		u8 n_2ch;
94 		u8 n_5ch;
95 		u8 n_6ch;
96 		u8 pad2;
97 	} __packed hdr = {
98 		.bw_2g = 0,
99 		.bw_5g = 3, /* BW_20_40_80_160M */
100 		.bw_6g = 3,
101 	};
102 	struct mt76_connac_mcu_chan {
103 		__le16 hw_value;
104 		__le16 pad;
105 		__le32 flags;
106 	} __packed channel;
107 	struct mt76_dev *dev = phy->dev;
108 	struct ieee80211_channel *chan;
109 	struct sk_buff *skb;
110 
111 	n_max_channels = phy->sband_2g.sband.n_channels +
112 			 phy->sband_5g.sband.n_channels +
113 			 phy->sband_6g.sband.n_channels;
114 	len = sizeof(hdr) + n_max_channels * sizeof(channel);
115 
116 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
117 	if (!skb)
118 		return -ENOMEM;
119 
120 	skb_reserve(skb, sizeof(hdr));
121 
122 	for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
123 		chan = &phy->sband_2g.sband.channels[i];
124 		if (chan->flags & IEEE80211_CHAN_DISABLED)
125 			continue;
126 
127 		channel.hw_value = cpu_to_le16(chan->hw_value);
128 		channel.flags = cpu_to_le32(chan->flags);
129 		channel.pad = 0;
130 
131 		skb_put_data(skb, &channel, sizeof(channel));
132 		n_2ch++;
133 	}
134 	for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
135 		chan = &phy->sband_5g.sband.channels[i];
136 		if (chan->flags & IEEE80211_CHAN_DISABLED)
137 			continue;
138 
139 		channel.hw_value = cpu_to_le16(chan->hw_value);
140 		channel.flags = cpu_to_le32(chan->flags);
141 		channel.pad = 0;
142 
143 		skb_put_data(skb, &channel, sizeof(channel));
144 		n_5ch++;
145 	}
146 	for (i = 0; i < phy->sband_6g.sband.n_channels; i++) {
147 		chan = &phy->sband_6g.sband.channels[i];
148 		if (chan->flags & IEEE80211_CHAN_DISABLED)
149 			continue;
150 
151 		channel.hw_value = cpu_to_le16(chan->hw_value);
152 		channel.flags = cpu_to_le32(chan->flags);
153 		channel.pad = 0;
154 
155 		skb_put_data(skb, &channel, sizeof(channel));
156 		n_6ch++;
157 	}
158 
159 	BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(hdr.alpha2));
160 	memcpy(hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
161 	hdr.n_2ch = n_2ch;
162 	hdr.n_5ch = n_5ch;
163 	hdr.n_6ch = n_6ch;
164 
165 	memcpy(__skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
166 
167 	return mt76_mcu_skb_send_msg(dev, skb, MCU_CE_CMD(SET_CHAN_DOMAIN),
168 				     false);
169 }
170 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_channel_domain);
171 
172 int mt76_connac_mcu_set_mac_enable(struct mt76_dev *dev, int band, bool enable,
173 				   bool hdr_trans)
174 {
175 	struct {
176 		u8 enable;
177 		u8 band;
178 		u8 rsv[2];
179 	} __packed req_mac = {
180 		.enable = enable,
181 		.band = band,
182 	};
183 
184 	return mt76_mcu_send_msg(dev, MCU_EXT_CMD(MAC_INIT_CTRL), &req_mac,
185 				 sizeof(req_mac), true);
186 }
187 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_mac_enable);
188 
189 int mt76_connac_mcu_set_vif_ps(struct mt76_dev *dev, struct ieee80211_vif *vif)
190 {
191 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
192 	struct {
193 		u8 bss_idx;
194 		u8 ps_state; /* 0: device awake
195 			      * 1: static power save
196 			      * 2: dynamic power saving
197 			      */
198 	} req = {
199 		.bss_idx = mvif->idx,
200 		.ps_state = vif->cfg.ps ? 2 : 0,
201 	};
202 
203 	if (vif->type != NL80211_IFTYPE_STATION)
204 		return -EOPNOTSUPP;
205 
206 	return mt76_mcu_send_msg(dev, MCU_CE_CMD(SET_PS_PROFILE),
207 				 &req, sizeof(req), false);
208 }
209 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_vif_ps);
210 
211 int mt76_connac_mcu_set_rts_thresh(struct mt76_dev *dev, u32 val, u8 band)
212 {
213 	struct {
214 		u8 prot_idx;
215 		u8 band;
216 		u8 rsv[2];
217 		__le32 len_thresh;
218 		__le32 pkt_thresh;
219 	} __packed req = {
220 		.prot_idx = 1,
221 		.band = band,
222 		.len_thresh = cpu_to_le32(val),
223 		.pkt_thresh = cpu_to_le32(0x2),
224 	};
225 
226 	return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PROTECT_CTRL), &req,
227 				 sizeof(req), true);
228 }
229 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rts_thresh);
230 
231 void mt76_connac_mcu_beacon_loss_iter(void *priv, u8 *mac,
232 				      struct ieee80211_vif *vif)
233 {
234 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
235 	struct mt76_connac_beacon_loss_event *event = priv;
236 
237 	if (mvif->idx != event->bss_idx)
238 		return;
239 
240 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
241 		return;
242 
243 	ieee80211_beacon_loss(vif);
244 }
245 EXPORT_SYMBOL_GPL(mt76_connac_mcu_beacon_loss_iter);
246 
247 struct tlv *
248 mt76_connac_mcu_add_nested_tlv(struct sk_buff *skb, int tag, int len,
249 			       void *sta_ntlv, void *sta_wtbl)
250 {
251 	struct sta_ntlv_hdr *ntlv_hdr = sta_ntlv;
252 	struct tlv *sta_hdr = sta_wtbl;
253 	struct tlv *ptlv, tlv = {
254 		.tag = cpu_to_le16(tag),
255 		.len = cpu_to_le16(len),
256 	};
257 	u16 ntlv;
258 
259 	ptlv = skb_put(skb, len);
260 	memcpy(ptlv, &tlv, sizeof(tlv));
261 
262 	ntlv = le16_to_cpu(ntlv_hdr->tlv_num);
263 	ntlv_hdr->tlv_num = cpu_to_le16(ntlv + 1);
264 
265 	if (sta_hdr) {
266 		len += le16_to_cpu(sta_hdr->len);
267 		sta_hdr->len = cpu_to_le16(len);
268 	}
269 
270 	return ptlv;
271 }
272 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_nested_tlv);
273 
274 struct sk_buff *
275 __mt76_connac_mcu_alloc_sta_req(struct mt76_dev *dev, struct mt76_vif *mvif,
276 				struct mt76_wcid *wcid, int len)
277 {
278 	struct sta_req_hdr hdr = {
279 		.bss_idx = mvif->idx,
280 		.muar_idx = wcid ? mvif->omac_idx : 0,
281 		.is_tlv_append = 1,
282 	};
283 	struct sk_buff *skb;
284 
285 	mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
286 				     &hdr.wlan_idx_hi);
287 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
288 	if (!skb)
289 		return ERR_PTR(-ENOMEM);
290 
291 	skb_put_data(skb, &hdr, sizeof(hdr));
292 
293 	return skb;
294 }
295 EXPORT_SYMBOL_GPL(__mt76_connac_mcu_alloc_sta_req);
296 
297 struct wtbl_req_hdr *
298 mt76_connac_mcu_alloc_wtbl_req(struct mt76_dev *dev, struct mt76_wcid *wcid,
299 			       int cmd, void *sta_wtbl, struct sk_buff **skb)
300 {
301 	struct tlv *sta_hdr = sta_wtbl;
302 	struct wtbl_req_hdr hdr = {
303 		.operation = cmd,
304 	};
305 	struct sk_buff *nskb = *skb;
306 
307 	mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
308 				     &hdr.wlan_idx_hi);
309 	if (!nskb) {
310 		nskb = mt76_mcu_msg_alloc(dev, NULL,
311 					  MT76_CONNAC_WTBL_UPDATE_MAX_SIZE);
312 		if (!nskb)
313 			return ERR_PTR(-ENOMEM);
314 
315 		*skb = nskb;
316 	}
317 
318 	if (sta_hdr)
319 		le16_add_cpu(&sta_hdr->len, sizeof(hdr));
320 
321 	return skb_put_data(nskb, &hdr, sizeof(hdr));
322 }
323 EXPORT_SYMBOL_GPL(mt76_connac_mcu_alloc_wtbl_req);
324 
325 void mt76_connac_mcu_bss_omac_tlv(struct sk_buff *skb,
326 				  struct ieee80211_vif *vif)
327 {
328 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
329 	u8 omac_idx = mvif->omac_idx;
330 	struct bss_info_omac *omac;
331 	struct tlv *tlv;
332 	u32 type = 0;
333 
334 	switch (vif->type) {
335 	case NL80211_IFTYPE_MONITOR:
336 	case NL80211_IFTYPE_MESH_POINT:
337 	case NL80211_IFTYPE_AP:
338 		if (vif->p2p)
339 			type = CONNECTION_P2P_GO;
340 		else
341 			type = CONNECTION_INFRA_AP;
342 		break;
343 	case NL80211_IFTYPE_STATION:
344 		if (vif->p2p)
345 			type = CONNECTION_P2P_GC;
346 		else
347 			type = CONNECTION_INFRA_STA;
348 		break;
349 	case NL80211_IFTYPE_ADHOC:
350 		type = CONNECTION_IBSS_ADHOC;
351 		break;
352 	default:
353 		WARN_ON(1);
354 		break;
355 	}
356 
357 	tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_OMAC, sizeof(*omac));
358 
359 	omac = (struct bss_info_omac *)tlv;
360 	omac->conn_type = cpu_to_le32(type);
361 	omac->omac_idx = mvif->omac_idx;
362 	omac->band_idx = mvif->band_idx;
363 	omac->hw_bss_idx = omac_idx > EXT_BSSID_START ? HW_BSSID_0 : omac_idx;
364 }
365 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_omac_tlv);
366 
367 void mt76_connac_mcu_sta_basic_tlv(struct mt76_dev *dev, struct sk_buff *skb,
368 				   struct ieee80211_vif *vif,
369 				   struct ieee80211_sta *sta,
370 				   bool enable, bool newly)
371 {
372 	struct sta_rec_basic *basic;
373 	struct tlv *tlv;
374 	int conn_type;
375 
376 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BASIC, sizeof(*basic));
377 
378 	basic = (struct sta_rec_basic *)tlv;
379 	basic->extra_info = cpu_to_le16(EXTRA_INFO_VER);
380 
381 	if (enable) {
382 		if (newly)
383 			basic->extra_info |= cpu_to_le16(EXTRA_INFO_NEW);
384 		basic->conn_state = CONN_STATE_PORT_SECURE;
385 	} else {
386 		basic->conn_state = CONN_STATE_DISCONNECT;
387 	}
388 
389 	if (!sta) {
390 		basic->conn_type = cpu_to_le32(CONNECTION_INFRA_BC);
391 		eth_broadcast_addr(basic->peer_addr);
392 		return;
393 	}
394 
395 	switch (vif->type) {
396 	case NL80211_IFTYPE_MESH_POINT:
397 	case NL80211_IFTYPE_AP:
398 		if (vif->p2p && !is_mt7921(dev))
399 			conn_type = CONNECTION_P2P_GC;
400 		else
401 			conn_type = CONNECTION_INFRA_STA;
402 		basic->conn_type = cpu_to_le32(conn_type);
403 		basic->aid = cpu_to_le16(sta->aid);
404 		break;
405 	case NL80211_IFTYPE_STATION:
406 		if (vif->p2p && !is_mt7921(dev))
407 			conn_type = CONNECTION_P2P_GO;
408 		else
409 			conn_type = CONNECTION_INFRA_AP;
410 		basic->conn_type = cpu_to_le32(conn_type);
411 		basic->aid = cpu_to_le16(vif->cfg.aid);
412 		break;
413 	case NL80211_IFTYPE_ADHOC:
414 		basic->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
415 		basic->aid = cpu_to_le16(sta->aid);
416 		break;
417 	default:
418 		WARN_ON(1);
419 		break;
420 	}
421 
422 	memcpy(basic->peer_addr, sta->addr, ETH_ALEN);
423 	basic->qos = sta->wme;
424 }
425 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_basic_tlv);
426 
427 void mt76_connac_mcu_sta_uapsd(struct sk_buff *skb, struct ieee80211_vif *vif,
428 			       struct ieee80211_sta *sta)
429 {
430 	struct sta_rec_uapsd *uapsd;
431 	struct tlv *tlv;
432 
433 	if (vif->type != NL80211_IFTYPE_AP || !sta->wme)
434 		return;
435 
436 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_APPS, sizeof(*uapsd));
437 	uapsd = (struct sta_rec_uapsd *)tlv;
438 
439 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) {
440 		uapsd->dac_map |= BIT(3);
441 		uapsd->tac_map |= BIT(3);
442 	}
443 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) {
444 		uapsd->dac_map |= BIT(2);
445 		uapsd->tac_map |= BIT(2);
446 	}
447 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) {
448 		uapsd->dac_map |= BIT(1);
449 		uapsd->tac_map |= BIT(1);
450 	}
451 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) {
452 		uapsd->dac_map |= BIT(0);
453 		uapsd->tac_map |= BIT(0);
454 	}
455 	uapsd->max_sp = sta->max_sp;
456 }
457 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_uapsd);
458 
459 void mt76_connac_mcu_wtbl_hdr_trans_tlv(struct sk_buff *skb,
460 					struct ieee80211_vif *vif,
461 					struct mt76_wcid *wcid,
462 					void *sta_wtbl, void *wtbl_tlv)
463 {
464 	struct wtbl_hdr_trans *htr;
465 	struct tlv *tlv;
466 
467 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HDR_TRANS,
468 					     sizeof(*htr),
469 					     wtbl_tlv, sta_wtbl);
470 	htr = (struct wtbl_hdr_trans *)tlv;
471 	htr->no_rx_trans = true;
472 
473 	if (vif->type == NL80211_IFTYPE_STATION)
474 		htr->to_ds = true;
475 	else
476 		htr->from_ds = true;
477 
478 	if (!wcid)
479 		return;
480 
481 	htr->no_rx_trans = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
482 	if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
483 		htr->to_ds = true;
484 		htr->from_ds = true;
485 	}
486 }
487 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_hdr_trans_tlv);
488 
489 int mt76_connac_mcu_sta_update_hdr_trans(struct mt76_dev *dev,
490 					 struct ieee80211_vif *vif,
491 					 struct mt76_wcid *wcid, int cmd)
492 {
493 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
494 	struct wtbl_req_hdr *wtbl_hdr;
495 	struct tlv *sta_wtbl;
496 	struct sk_buff *skb;
497 
498 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
499 	if (IS_ERR(skb))
500 		return PTR_ERR(skb);
501 
502 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
503 					   sizeof(struct tlv));
504 
505 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
506 						  sta_wtbl, &skb);
507 	if (IS_ERR(wtbl_hdr))
508 		return PTR_ERR(wtbl_hdr);
509 
510 	mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, sta_wtbl, wtbl_hdr);
511 
512 	return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
513 }
514 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_update_hdr_trans);
515 
516 int mt76_connac_mcu_wtbl_update_hdr_trans(struct mt76_dev *dev,
517 					  struct ieee80211_vif *vif,
518 					  struct ieee80211_sta *sta)
519 {
520 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
521 	struct wtbl_req_hdr *wtbl_hdr;
522 	struct sk_buff *skb = NULL;
523 
524 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET, NULL,
525 						  &skb);
526 	if (IS_ERR(wtbl_hdr))
527 		return PTR_ERR(wtbl_hdr);
528 
529 	mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, vif, wcid, NULL, wtbl_hdr);
530 
531 	return mt76_mcu_skb_send_msg(dev, skb, MCU_EXT_CMD(WTBL_UPDATE), true);
532 }
533 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_update_hdr_trans);
534 
535 void mt76_connac_mcu_wtbl_generic_tlv(struct mt76_dev *dev,
536 				      struct sk_buff *skb,
537 				      struct ieee80211_vif *vif,
538 				      struct ieee80211_sta *sta,
539 				      void *sta_wtbl, void *wtbl_tlv)
540 {
541 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
542 	struct wtbl_generic *generic;
543 	struct wtbl_rx *rx;
544 	struct wtbl_spe *spe;
545 	struct tlv *tlv;
546 
547 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_GENERIC,
548 					     sizeof(*generic),
549 					     wtbl_tlv, sta_wtbl);
550 
551 	generic = (struct wtbl_generic *)tlv;
552 
553 	if (sta) {
554 		if (vif->type == NL80211_IFTYPE_STATION)
555 			generic->partial_aid = cpu_to_le16(vif->cfg.aid);
556 		else
557 			generic->partial_aid = cpu_to_le16(sta->aid);
558 		memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
559 		generic->muar_idx = mvif->omac_idx;
560 		generic->qos = sta->wme;
561 	} else {
562 		if (!is_connac_v1(dev) && vif->type == NL80211_IFTYPE_STATION)
563 			memcpy(generic->peer_addr, vif->bss_conf.bssid,
564 			       ETH_ALEN);
565 		else
566 			eth_broadcast_addr(generic->peer_addr);
567 
568 		generic->muar_idx = 0xe;
569 	}
570 
571 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
572 					     wtbl_tlv, sta_wtbl);
573 
574 	rx = (struct wtbl_rx *)tlv;
575 	rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
576 	rx->rca2 = 1;
577 	rx->rv = 1;
578 
579 	if (!is_connac_v1(dev))
580 		return;
581 
582 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SPE, sizeof(*spe),
583 					     wtbl_tlv, sta_wtbl);
584 	spe = (struct wtbl_spe *)tlv;
585 	spe->spe_idx = 24;
586 }
587 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_generic_tlv);
588 
589 static void
590 mt76_connac_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
591 			      struct ieee80211_vif *vif)
592 {
593 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
594 	struct sta_rec_amsdu *amsdu;
595 	struct tlv *tlv;
596 
597 	if (vif->type != NL80211_IFTYPE_AP &&
598 	    vif->type != NL80211_IFTYPE_STATION)
599 		return;
600 
601 	if (!sta->deflink.agg.max_amsdu_len)
602 		return;
603 
604 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
605 	amsdu = (struct sta_rec_amsdu *)tlv;
606 	amsdu->max_amsdu_num = 8;
607 	amsdu->amsdu_en = true;
608 	amsdu->max_mpdu_size = sta->deflink.agg.max_amsdu_len >=
609 			       IEEE80211_MAX_MPDU_LEN_VHT_7991;
610 
611 	wcid->amsdu = true;
612 }
613 
614 #define HE_PHY(p, c)	u8_get_bits(c, IEEE80211_HE_PHY_##p)
615 #define HE_MAC(m, c)	u8_get_bits(c, IEEE80211_HE_MAC_##m)
616 static void
617 mt76_connac_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
618 {
619 	struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
620 	struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
621 	struct sta_rec_he *he;
622 	struct tlv *tlv;
623 	u32 cap = 0;
624 
625 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
626 
627 	he = (struct sta_rec_he *)tlv;
628 
629 	if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
630 		cap |= STA_REC_HE_CAP_HTC;
631 
632 	if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
633 		cap |= STA_REC_HE_CAP_BSR;
634 
635 	if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
636 		cap |= STA_REC_HE_CAP_OM;
637 
638 	if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU)
639 		cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
640 
641 	if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
642 		cap |= STA_REC_HE_CAP_BQR;
643 
644 	if (elem->phy_cap_info[0] &
645 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
646 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
647 		cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
648 
649 	if (elem->phy_cap_info[1] &
650 	    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
651 		cap |= STA_REC_HE_CAP_LDPC;
652 
653 	if (elem->phy_cap_info[1] &
654 	    IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
655 		cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
656 
657 	if (elem->phy_cap_info[2] &
658 	    IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
659 		cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
660 
661 	if (elem->phy_cap_info[2] &
662 	    IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
663 		cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
664 
665 	if (elem->phy_cap_info[2] &
666 	    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
667 		cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
668 
669 	if (elem->phy_cap_info[6] &
670 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
671 		cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
672 
673 	if (elem->phy_cap_info[7] &
674 	    IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
675 		cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
676 
677 	if (elem->phy_cap_info[7] &
678 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
679 		cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
680 
681 	if (elem->phy_cap_info[7] &
682 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
683 		cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
684 
685 	if (elem->phy_cap_info[8] &
686 	    IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
687 		cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
688 
689 	if (elem->phy_cap_info[8] &
690 	    IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
691 		cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
692 
693 	if (elem->phy_cap_info[9] &
694 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
695 		cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
696 
697 	if (elem->phy_cap_info[9] &
698 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
699 		cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
700 
701 	if (elem->phy_cap_info[9] &
702 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
703 		cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
704 
705 	he->he_cap = cpu_to_le32(cap);
706 
707 	switch (sta->deflink.bandwidth) {
708 	case IEEE80211_STA_RX_BW_160:
709 		if (elem->phy_cap_info[0] &
710 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
711 			he->max_nss_mcs[CMD_HE_MCS_BW8080] =
712 				he_cap->he_mcs_nss_supp.rx_mcs_80p80;
713 
714 		he->max_nss_mcs[CMD_HE_MCS_BW160] =
715 				he_cap->he_mcs_nss_supp.rx_mcs_160;
716 		fallthrough;
717 	default:
718 		he->max_nss_mcs[CMD_HE_MCS_BW80] =
719 				he_cap->he_mcs_nss_supp.rx_mcs_80;
720 		break;
721 	}
722 
723 	he->t_frame_dur =
724 		HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
725 	he->max_ampdu_exp =
726 		HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
727 
728 	he->bw_set =
729 		HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
730 	he->device_class =
731 		HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
732 	he->punc_pream_rx =
733 		HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
734 
735 	he->dcm_tx_mode =
736 		HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
737 	he->dcm_tx_max_nss =
738 		HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
739 	he->dcm_rx_mode =
740 		HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
741 	he->dcm_rx_max_nss =
742 		HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
743 	he->dcm_rx_max_nss =
744 		HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
745 
746 	he->pkt_ext = 2;
747 }
748 
749 void
750 mt76_connac_mcu_sta_he_tlv_v2(struct sk_buff *skb, struct ieee80211_sta *sta)
751 {
752 	struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
753 	struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
754 	struct sta_rec_he_v2 *he;
755 	struct tlv *tlv;
756 
757 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_V2, sizeof(*he));
758 
759 	he = (struct sta_rec_he_v2 *)tlv;
760 	memcpy(he->he_phy_cap, elem->phy_cap_info, sizeof(he->he_phy_cap));
761 	memcpy(he->he_mac_cap, elem->mac_cap_info, sizeof(he->he_mac_cap));
762 
763 	switch (sta->deflink.bandwidth) {
764 	case IEEE80211_STA_RX_BW_160:
765 		if (elem->phy_cap_info[0] &
766 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
767 			he->max_nss_mcs[CMD_HE_MCS_BW8080] =
768 				he_cap->he_mcs_nss_supp.rx_mcs_80p80;
769 
770 		he->max_nss_mcs[CMD_HE_MCS_BW160] =
771 				he_cap->he_mcs_nss_supp.rx_mcs_160;
772 		fallthrough;
773 	default:
774 		he->max_nss_mcs[CMD_HE_MCS_BW80] =
775 				he_cap->he_mcs_nss_supp.rx_mcs_80;
776 		break;
777 	}
778 
779 	he->pkt_ext = IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US;
780 }
781 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_he_tlv_v2);
782 
783 u8
784 mt76_connac_get_phy_mode_v2(struct mt76_phy *mphy, struct ieee80211_vif *vif,
785 			    enum nl80211_band band, struct ieee80211_sta *sta)
786 {
787 	struct ieee80211_sta_ht_cap *ht_cap;
788 	struct ieee80211_sta_vht_cap *vht_cap;
789 	const struct ieee80211_sta_he_cap *he_cap;
790 	const struct ieee80211_sta_eht_cap *eht_cap;
791 	u8 mode = 0;
792 
793 	if (sta) {
794 		ht_cap = &sta->deflink.ht_cap;
795 		vht_cap = &sta->deflink.vht_cap;
796 		he_cap = &sta->deflink.he_cap;
797 		eht_cap = &sta->deflink.eht_cap;
798 	} else {
799 		struct ieee80211_supported_band *sband;
800 
801 		sband = mphy->hw->wiphy->bands[band];
802 		ht_cap = &sband->ht_cap;
803 		vht_cap = &sband->vht_cap;
804 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
805 		eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
806 	}
807 
808 	if (band == NL80211_BAND_2GHZ) {
809 		mode |= PHY_TYPE_BIT_HR_DSSS | PHY_TYPE_BIT_ERP;
810 
811 		if (ht_cap->ht_supported)
812 			mode |= PHY_TYPE_BIT_HT;
813 
814 		if (he_cap && he_cap->has_he)
815 			mode |= PHY_TYPE_BIT_HE;
816 
817 		if (eht_cap && eht_cap->has_eht)
818 			mode |= PHY_TYPE_BIT_BE;
819 	} else if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ) {
820 		mode |= PHY_TYPE_BIT_OFDM;
821 
822 		if (ht_cap->ht_supported)
823 			mode |= PHY_TYPE_BIT_HT;
824 
825 		if (vht_cap->vht_supported)
826 			mode |= PHY_TYPE_BIT_VHT;
827 
828 		if (he_cap && he_cap->has_he)
829 			mode |= PHY_TYPE_BIT_HE;
830 
831 		if (eht_cap && eht_cap->has_eht)
832 			mode |= PHY_TYPE_BIT_BE;
833 	}
834 
835 	return mode;
836 }
837 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_v2);
838 
839 void mt76_connac_mcu_sta_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
840 			     struct ieee80211_sta *sta,
841 			     struct ieee80211_vif *vif,
842 			     u8 rcpi, u8 sta_state)
843 {
844 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
845 	struct cfg80211_chan_def *chandef = mvif->ctx ?
846 					    &mvif->ctx->def : &mphy->chandef;
847 	enum nl80211_band band = chandef->chan->band;
848 	struct mt76_dev *dev = mphy->dev;
849 	struct sta_rec_ra_info *ra_info;
850 	struct sta_rec_state *state;
851 	struct sta_rec_phy *phy;
852 	struct tlv *tlv;
853 	u16 supp_rates;
854 
855 	/* starec ht */
856 	if (sta->deflink.ht_cap.ht_supported) {
857 		struct sta_rec_ht *ht;
858 
859 		tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
860 		ht = (struct sta_rec_ht *)tlv;
861 		ht->ht_cap = cpu_to_le16(sta->deflink.ht_cap.cap);
862 	}
863 
864 	/* starec vht */
865 	if (sta->deflink.vht_cap.vht_supported) {
866 		struct sta_rec_vht *vht;
867 		int len;
868 
869 		len = is_mt7921(dev) ? sizeof(*vht) : sizeof(*vht) - 4;
870 		tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, len);
871 		vht = (struct sta_rec_vht *)tlv;
872 		vht->vht_cap = cpu_to_le32(sta->deflink.vht_cap.cap);
873 		vht->vht_rx_mcs_map = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
874 		vht->vht_tx_mcs_map = sta->deflink.vht_cap.vht_mcs.tx_mcs_map;
875 	}
876 
877 	/* starec uapsd */
878 	mt76_connac_mcu_sta_uapsd(skb, vif, sta);
879 
880 	if (!is_mt7921(dev))
881 		return;
882 
883 	if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_cap.has_he)
884 		mt76_connac_mcu_sta_amsdu_tlv(skb, sta, vif);
885 
886 	/* starec he */
887 	if (sta->deflink.he_cap.has_he) {
888 		mt76_connac_mcu_sta_he_tlv(skb, sta);
889 		mt76_connac_mcu_sta_he_tlv_v2(skb, sta);
890 		if (band == NL80211_BAND_6GHZ &&
891 		    sta_state == MT76_STA_INFO_STATE_ASSOC) {
892 			struct sta_rec_he_6g_capa *he_6g_capa;
893 
894 			tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G,
895 						      sizeof(*he_6g_capa));
896 			he_6g_capa = (struct sta_rec_he_6g_capa *)tlv;
897 			he_6g_capa->capa = sta->deflink.he_6ghz_capa.capa;
898 		}
899 	}
900 
901 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
902 	phy = (struct sta_rec_phy *)tlv;
903 	phy->phy_type = mt76_connac_get_phy_mode_v2(mphy, vif, band, sta);
904 	phy->basic_rate = cpu_to_le16((u16)vif->bss_conf.basic_rates);
905 	phy->rcpi = rcpi;
906 	phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR,
907 				sta->deflink.ht_cap.ampdu_factor) |
908 		     FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY,
909 				sta->deflink.ht_cap.ampdu_density);
910 
911 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
912 	ra_info = (struct sta_rec_ra_info *)tlv;
913 
914 	supp_rates = sta->deflink.supp_rates[band];
915 	if (band == NL80211_BAND_2GHZ)
916 		supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates >> 4) |
917 			     FIELD_PREP(RA_LEGACY_CCK, supp_rates & 0xf);
918 	else
919 		supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates);
920 
921 	ra_info->legacy = cpu_to_le16(supp_rates);
922 
923 	if (sta->deflink.ht_cap.ht_supported)
924 		memcpy(ra_info->rx_mcs_bitmask,
925 		       sta->deflink.ht_cap.mcs.rx_mask,
926 		       HT_MCS_MASK_NUM);
927 
928 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
929 	state = (struct sta_rec_state *)tlv;
930 	state->state = sta_state;
931 
932 	if (sta->deflink.vht_cap.vht_supported) {
933 		state->vht_opmode = sta->deflink.bandwidth;
934 		state->vht_opmode |= (sta->deflink.rx_nss - 1) <<
935 			IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
936 	}
937 }
938 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
939 
940 void mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb,
941 				   struct ieee80211_sta *sta,
942 				   void *sta_wtbl, void *wtbl_tlv)
943 {
944 	struct wtbl_smps *smps;
945 	struct tlv *tlv;
946 
947 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
948 					     wtbl_tlv, sta_wtbl);
949 	smps = (struct wtbl_smps *)tlv;
950 	smps->smps = (sta->deflink.smps_mode == IEEE80211_SMPS_DYNAMIC);
951 }
952 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_smps_tlv);
953 
954 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
955 				 struct ieee80211_sta *sta, void *sta_wtbl,
956 				 void *wtbl_tlv, bool ht_ldpc, bool vht_ldpc)
957 {
958 	struct wtbl_ht *ht = NULL;
959 	struct tlv *tlv;
960 	u32 flags = 0;
961 
962 	if (sta->deflink.ht_cap.ht_supported || sta->deflink.he_6ghz_capa.capa) {
963 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
964 						     wtbl_tlv, sta_wtbl);
965 		ht = (struct wtbl_ht *)tlv;
966 		ht->ldpc = ht_ldpc &&
967 			   !!(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
968 
969 		if (sta->deflink.ht_cap.ht_supported) {
970 			ht->af = sta->deflink.ht_cap.ampdu_factor;
971 			ht->mm = sta->deflink.ht_cap.ampdu_density;
972 		} else {
973 			ht->af = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
974 					       IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
975 			ht->mm = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
976 					       IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
977 		}
978 
979 		ht->ht = true;
980 	}
981 
982 	if (sta->deflink.vht_cap.vht_supported || sta->deflink.he_6ghz_capa.capa) {
983 		struct wtbl_vht *vht;
984 		u8 af;
985 
986 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
987 						     sizeof(*vht), wtbl_tlv,
988 						     sta_wtbl);
989 		vht = (struct wtbl_vht *)tlv;
990 		vht->ldpc = vht_ldpc &&
991 			    !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
992 		vht->vht = true;
993 
994 		af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
995 			       sta->deflink.vht_cap.cap);
996 		if (ht)
997 			ht->af = max(ht->af, af);
998 	}
999 
1000 	mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
1001 
1002 	if (is_connac_v1(dev) && sta->deflink.ht_cap.ht_supported) {
1003 		/* sgi */
1004 		u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
1005 			  MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
1006 		struct wtbl_raw *raw;
1007 
1008 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
1009 						     sizeof(*raw), wtbl_tlv,
1010 						     sta_wtbl);
1011 
1012 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
1013 			flags |= MT_WTBL_W5_SHORT_GI_20;
1014 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
1015 			flags |= MT_WTBL_W5_SHORT_GI_40;
1016 
1017 		if (sta->deflink.vht_cap.vht_supported) {
1018 			if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
1019 				flags |= MT_WTBL_W5_SHORT_GI_80;
1020 			if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
1021 				flags |= MT_WTBL_W5_SHORT_GI_160;
1022 		}
1023 		raw = (struct wtbl_raw *)tlv;
1024 		raw->val = cpu_to_le32(flags);
1025 		raw->msk = cpu_to_le32(~msk);
1026 		raw->wtbl_idx = 1;
1027 		raw->dw = 5;
1028 	}
1029 }
1030 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
1031 
1032 int mt76_connac_mcu_sta_cmd(struct mt76_phy *phy,
1033 			    struct mt76_sta_cmd_info *info)
1034 {
1035 	struct mt76_vif *mvif = (struct mt76_vif *)info->vif->drv_priv;
1036 	struct mt76_dev *dev = phy->dev;
1037 	struct wtbl_req_hdr *wtbl_hdr;
1038 	struct tlv *sta_wtbl;
1039 	struct sk_buff *skb;
1040 
1041 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, info->wcid);
1042 	if (IS_ERR(skb))
1043 		return PTR_ERR(skb);
1044 
1045 	if (info->sta || !info->offload_fw)
1046 		mt76_connac_mcu_sta_basic_tlv(dev, skb, info->vif, info->sta,
1047 					      info->enable, info->newly);
1048 	if (info->sta && info->enable)
1049 		mt76_connac_mcu_sta_tlv(phy, skb, info->sta,
1050 					info->vif, info->rcpi,
1051 					info->state);
1052 
1053 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1054 					   sizeof(struct tlv));
1055 
1056 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, info->wcid,
1057 						  WTBL_RESET_AND_SET,
1058 						  sta_wtbl, &skb);
1059 	if (IS_ERR(wtbl_hdr))
1060 		return PTR_ERR(wtbl_hdr);
1061 
1062 	if (info->enable) {
1063 		mt76_connac_mcu_wtbl_generic_tlv(dev, skb, info->vif,
1064 						 info->sta, sta_wtbl,
1065 						 wtbl_hdr);
1066 		mt76_connac_mcu_wtbl_hdr_trans_tlv(skb, info->vif, info->wcid,
1067 						   sta_wtbl, wtbl_hdr);
1068 		if (info->sta)
1069 			mt76_connac_mcu_wtbl_ht_tlv(dev, skb, info->sta,
1070 						    sta_wtbl, wtbl_hdr,
1071 						    true, true);
1072 	}
1073 
1074 	return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
1075 }
1076 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_cmd);
1077 
1078 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
1079 				 struct ieee80211_ampdu_params *params,
1080 				 bool enable, bool tx, void *sta_wtbl,
1081 				 void *wtbl_tlv)
1082 {
1083 	struct wtbl_ba *ba;
1084 	struct tlv *tlv;
1085 
1086 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
1087 					     wtbl_tlv, sta_wtbl);
1088 
1089 	ba = (struct wtbl_ba *)tlv;
1090 	ba->tid = params->tid;
1091 
1092 	if (tx) {
1093 		ba->ba_type = MT_BA_TYPE_ORIGINATOR;
1094 		ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
1095 		ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1096 		ba->ba_en = enable;
1097 	} else {
1098 		memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
1099 		ba->ba_type = MT_BA_TYPE_RECIPIENT;
1100 		ba->rst_ba_tid = params->tid;
1101 		ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
1102 		ba->rst_ba_sb = 1;
1103 	}
1104 
1105 	if (!is_connac_v1(dev)) {
1106 		ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
1107 		return;
1108 	}
1109 
1110 	if (enable && tx) {
1111 		static const u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
1112 		int i;
1113 
1114 		for (i = 7; i > 0; i--) {
1115 			if (params->buf_size >= ba_range[i])
1116 				break;
1117 		}
1118 		ba->ba_winsize_idx = i;
1119 	}
1120 }
1121 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
1122 
1123 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
1124 				struct ieee80211_vif *vif,
1125 				struct mt76_wcid *wcid,
1126 				bool enable)
1127 {
1128 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1129 	struct mt76_dev *dev = phy->dev;
1130 	struct {
1131 		struct {
1132 			u8 omac_idx;
1133 			u8 band_idx;
1134 			__le16 pad;
1135 		} __packed hdr;
1136 		struct req_tlv {
1137 			__le16 tag;
1138 			__le16 len;
1139 			u8 active;
1140 			u8 pad;
1141 			u8 omac_addr[ETH_ALEN];
1142 		} __packed tlv;
1143 	} dev_req = {
1144 		.hdr = {
1145 			.omac_idx = mvif->omac_idx,
1146 			.band_idx = mvif->band_idx,
1147 		},
1148 		.tlv = {
1149 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
1150 			.len = cpu_to_le16(sizeof(struct req_tlv)),
1151 			.active = enable,
1152 		},
1153 	};
1154 	struct {
1155 		struct {
1156 			u8 bss_idx;
1157 			u8 pad[3];
1158 		} __packed hdr;
1159 		struct mt76_connac_bss_basic_tlv basic;
1160 	} basic_req = {
1161 		.hdr = {
1162 			.bss_idx = mvif->idx,
1163 		},
1164 		.basic = {
1165 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1166 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1167 			.omac_idx = mvif->omac_idx,
1168 			.band_idx = mvif->band_idx,
1169 			.wmm_idx = mvif->wmm_idx,
1170 			.active = enable,
1171 			.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
1172 			.sta_idx = cpu_to_le16(wcid->idx),
1173 			.conn_state = 1,
1174 		},
1175 	};
1176 	int err, idx, cmd, len;
1177 	void *data;
1178 
1179 	switch (vif->type) {
1180 	case NL80211_IFTYPE_MESH_POINT:
1181 	case NL80211_IFTYPE_MONITOR:
1182 	case NL80211_IFTYPE_AP:
1183 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
1184 		break;
1185 	case NL80211_IFTYPE_STATION:
1186 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
1187 		break;
1188 	case NL80211_IFTYPE_ADHOC:
1189 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1190 		break;
1191 	default:
1192 		WARN_ON(1);
1193 		break;
1194 	}
1195 
1196 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1197 	basic_req.basic.hw_bss_idx = idx;
1198 
1199 	memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
1200 
1201 	cmd = enable ? MCU_UNI_CMD(DEV_INFO_UPDATE) : MCU_UNI_CMD(BSS_INFO_UPDATE);
1202 	data = enable ? (void *)&dev_req : (void *)&basic_req;
1203 	len = enable ? sizeof(dev_req) : sizeof(basic_req);
1204 
1205 	err = mt76_mcu_send_msg(dev, cmd, data, len, true);
1206 	if (err < 0)
1207 		return err;
1208 
1209 	cmd = enable ? MCU_UNI_CMD(BSS_INFO_UPDATE) : MCU_UNI_CMD(DEV_INFO_UPDATE);
1210 	data = enable ? (void *)&basic_req : (void *)&dev_req;
1211 	len = enable ? sizeof(basic_req) : sizeof(dev_req);
1212 
1213 	return mt76_mcu_send_msg(dev, cmd, data, len, true);
1214 }
1215 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
1216 
1217 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
1218 				struct ieee80211_ampdu_params *params,
1219 				bool enable, bool tx)
1220 {
1221 	struct sta_rec_ba *ba;
1222 	struct tlv *tlv;
1223 
1224 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1225 
1226 	ba = (struct sta_rec_ba *)tlv;
1227 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1228 	ba->winsize = cpu_to_le16(params->buf_size);
1229 	ba->ssn = cpu_to_le16(params->ssn);
1230 	ba->ba_en = enable << params->tid;
1231 	ba->amsdu = params->amsdu;
1232 	ba->tid = params->tid;
1233 }
1234 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1235 
1236 int mt76_connac_mcu_sta_wed_update(struct mt76_dev *dev, struct sk_buff *skb)
1237 {
1238 	if (!mt76_is_mmio(dev))
1239 		return 0;
1240 
1241 	if (!mtk_wed_device_active(&dev->mmio.wed))
1242 		return 0;
1243 
1244 	return mtk_wed_device_update_msg(&dev->mmio.wed, WED_WO_STA_REC,
1245 					 skb->data, skb->len);
1246 }
1247 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_wed_update);
1248 
1249 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif *mvif,
1250 			   struct ieee80211_ampdu_params *params,
1251 			   int cmd, bool enable, bool tx)
1252 {
1253 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1254 	struct wtbl_req_hdr *wtbl_hdr;
1255 	struct tlv *sta_wtbl;
1256 	struct sk_buff *skb;
1257 	int ret;
1258 
1259 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1260 	if (IS_ERR(skb))
1261 		return PTR_ERR(skb);
1262 
1263 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1264 					   sizeof(struct tlv));
1265 
1266 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1267 						  sta_wtbl, &skb);
1268 	if (IS_ERR(wtbl_hdr))
1269 		return PTR_ERR(wtbl_hdr);
1270 
1271 	mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1272 				    wtbl_hdr);
1273 
1274 	ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1275 	if (ret)
1276 		return ret;
1277 
1278 	ret = mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1279 	if (ret)
1280 		return ret;
1281 
1282 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1283 	if (IS_ERR(skb))
1284 		return PTR_ERR(skb);
1285 
1286 	mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1287 
1288 	ret = mt76_connac_mcu_sta_wed_update(dev, skb);
1289 	if (ret)
1290 		return ret;
1291 
1292 	return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
1293 }
1294 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1295 
1296 u8 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1297 			    enum nl80211_band band, struct ieee80211_sta *sta)
1298 {
1299 	struct mt76_dev *dev = phy->dev;
1300 	const struct ieee80211_sta_he_cap *he_cap;
1301 	struct ieee80211_sta_vht_cap *vht_cap;
1302 	struct ieee80211_sta_ht_cap *ht_cap;
1303 	u8 mode = 0;
1304 
1305 	if (is_connac_v1(dev))
1306 		return 0x38;
1307 
1308 	if (sta) {
1309 		ht_cap = &sta->deflink.ht_cap;
1310 		vht_cap = &sta->deflink.vht_cap;
1311 		he_cap = &sta->deflink.he_cap;
1312 	} else {
1313 		struct ieee80211_supported_band *sband;
1314 
1315 		sband = phy->hw->wiphy->bands[band];
1316 		ht_cap = &sband->ht_cap;
1317 		vht_cap = &sband->vht_cap;
1318 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1319 	}
1320 
1321 	if (band == NL80211_BAND_2GHZ) {
1322 		mode |= PHY_MODE_B | PHY_MODE_G;
1323 
1324 		if (ht_cap->ht_supported)
1325 			mode |= PHY_MODE_GN;
1326 
1327 		if (he_cap && he_cap->has_he)
1328 			mode |= PHY_MODE_AX_24G;
1329 	} else if (band == NL80211_BAND_5GHZ) {
1330 		mode |= PHY_MODE_A;
1331 
1332 		if (ht_cap->ht_supported)
1333 			mode |= PHY_MODE_AN;
1334 
1335 		if (vht_cap->vht_supported)
1336 			mode |= PHY_MODE_AC;
1337 
1338 		if (he_cap && he_cap->has_he)
1339 			mode |= PHY_MODE_AX_5G;
1340 	} else if (band == NL80211_BAND_6GHZ) {
1341 		mode |= PHY_MODE_A | PHY_MODE_AN |
1342 			PHY_MODE_AC | PHY_MODE_AX_5G;
1343 	}
1344 
1345 	return mode;
1346 }
1347 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode);
1348 
1349 u8 mt76_connac_get_phy_mode_ext(struct mt76_phy *phy, struct ieee80211_vif *vif,
1350 				enum nl80211_band band)
1351 {
1352 	const struct ieee80211_sta_eht_cap *eht_cap;
1353 	struct ieee80211_supported_band *sband;
1354 	u8 mode = 0;
1355 
1356 	if (band == NL80211_BAND_6GHZ)
1357 		mode |= PHY_MODE_AX_6G;
1358 
1359 	sband = phy->hw->wiphy->bands[band];
1360 	eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
1361 
1362 	if (!eht_cap || !eht_cap->has_eht)
1363 		return mode;
1364 
1365 	switch (band) {
1366 	case NL80211_BAND_6GHZ:
1367 		mode |= PHY_MODE_BE_6G;
1368 		break;
1369 	case NL80211_BAND_5GHZ:
1370 		mode |= PHY_MODE_BE_5G;
1371 		break;
1372 	case NL80211_BAND_2GHZ:
1373 		mode |= PHY_MODE_BE_24G;
1374 		break;
1375 	default:
1376 		break;
1377 	}
1378 
1379 	return mode;
1380 }
1381 EXPORT_SYMBOL_GPL(mt76_connac_get_phy_mode_ext);
1382 
1383 const struct ieee80211_sta_he_cap *
1384 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1385 {
1386 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1387 	struct cfg80211_chan_def *chandef = mvif->ctx ?
1388 					    &mvif->ctx->def : &phy->chandef;
1389 	enum nl80211_band band = chandef->chan->band;
1390 	struct ieee80211_supported_band *sband;
1391 
1392 	sband = phy->hw->wiphy->bands[band];
1393 
1394 	return ieee80211_get_he_iftype_cap(sband, vif->type);
1395 }
1396 EXPORT_SYMBOL_GPL(mt76_connac_get_he_phy_cap);
1397 
1398 const struct ieee80211_sta_eht_cap *
1399 mt76_connac_get_eht_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1400 {
1401 	enum nl80211_band band = phy->chandef.chan->band;
1402 	struct ieee80211_supported_band *sband;
1403 
1404 	sband = phy->hw->wiphy->bands[band];
1405 
1406 	return ieee80211_get_eht_iftype_cap(sband, vif->type);
1407 }
1408 EXPORT_SYMBOL_GPL(mt76_connac_get_eht_phy_cap);
1409 
1410 #define DEFAULT_HE_PE_DURATION		4
1411 #define DEFAULT_HE_DURATION_RTS_THRES	1023
1412 static void
1413 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1414 			       struct tlv *tlv)
1415 {
1416 	const struct ieee80211_sta_he_cap *cap;
1417 	struct bss_info_uni_he *he;
1418 
1419 	cap = mt76_connac_get_he_phy_cap(phy, vif);
1420 
1421 	he = (struct bss_info_uni_he *)tlv;
1422 	he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1423 	if (!he->he_pe_duration)
1424 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1425 
1426 	he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1427 	if (!he->he_rts_thres)
1428 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1429 
1430 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1431 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1432 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1433 }
1434 
1435 int mt76_connac_mcu_uni_set_chctx(struct mt76_phy *phy, struct mt76_vif *mvif,
1436 				  struct ieee80211_chanctx_conf *ctx)
1437 {
1438 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1439 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1440 	enum nl80211_band band = chandef->chan->band;
1441 	struct mt76_dev *mdev = phy->dev;
1442 	struct {
1443 		struct {
1444 			u8 bss_idx;
1445 			u8 pad[3];
1446 		} __packed hdr;
1447 		struct rlm_tlv {
1448 			__le16 tag;
1449 			__le16 len;
1450 			u8 control_channel;
1451 			u8 center_chan;
1452 			u8 center_chan2;
1453 			u8 bw;
1454 			u8 tx_streams;
1455 			u8 rx_streams;
1456 			u8 short_st;
1457 			u8 ht_op_info;
1458 			u8 sco;
1459 			u8 band;
1460 			u8 pad[2];
1461 		} __packed rlm;
1462 	} __packed rlm_req = {
1463 		.hdr = {
1464 			.bss_idx = mvif->idx,
1465 		},
1466 		.rlm = {
1467 			.tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1468 			.len = cpu_to_le16(sizeof(struct rlm_tlv)),
1469 			.control_channel = chandef->chan->hw_value,
1470 			.center_chan = ieee80211_frequency_to_channel(freq1),
1471 			.center_chan2 = ieee80211_frequency_to_channel(freq2),
1472 			.tx_streams = hweight8(phy->antenna_mask),
1473 			.ht_op_info = 4, /* set HT 40M allowed */
1474 			.rx_streams = phy->chainmask,
1475 			.short_st = true,
1476 			.band = band,
1477 		},
1478 	};
1479 
1480 	switch (chandef->width) {
1481 	case NL80211_CHAN_WIDTH_40:
1482 		rlm_req.rlm.bw = CMD_CBW_40MHZ;
1483 		break;
1484 	case NL80211_CHAN_WIDTH_80:
1485 		rlm_req.rlm.bw = CMD_CBW_80MHZ;
1486 		break;
1487 	case NL80211_CHAN_WIDTH_80P80:
1488 		rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1489 		break;
1490 	case NL80211_CHAN_WIDTH_160:
1491 		rlm_req.rlm.bw = CMD_CBW_160MHZ;
1492 		break;
1493 	case NL80211_CHAN_WIDTH_5:
1494 		rlm_req.rlm.bw = CMD_CBW_5MHZ;
1495 		break;
1496 	case NL80211_CHAN_WIDTH_10:
1497 		rlm_req.rlm.bw = CMD_CBW_10MHZ;
1498 		break;
1499 	case NL80211_CHAN_WIDTH_20_NOHT:
1500 	case NL80211_CHAN_WIDTH_20:
1501 	default:
1502 		rlm_req.rlm.bw = CMD_CBW_20MHZ;
1503 		rlm_req.rlm.ht_op_info = 0;
1504 		break;
1505 	}
1506 
1507 	if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1508 		rlm_req.rlm.sco = 1; /* SCA */
1509 	else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1510 		rlm_req.rlm.sco = 3; /* SCB */
1511 
1512 	return mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &rlm_req,
1513 				 sizeof(rlm_req), true);
1514 }
1515 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_set_chctx);
1516 
1517 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1518 				struct ieee80211_vif *vif,
1519 				struct mt76_wcid *wcid,
1520 				bool enable,
1521 				struct ieee80211_chanctx_conf *ctx)
1522 {
1523 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1524 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &phy->chandef;
1525 	enum nl80211_band band = chandef->chan->band;
1526 	struct mt76_dev *mdev = phy->dev;
1527 	struct {
1528 		struct {
1529 			u8 bss_idx;
1530 			u8 pad[3];
1531 		} __packed hdr;
1532 		struct mt76_connac_bss_basic_tlv basic;
1533 		struct mt76_connac_bss_qos_tlv qos;
1534 	} basic_req = {
1535 		.hdr = {
1536 			.bss_idx = mvif->idx,
1537 		},
1538 		.basic = {
1539 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1540 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1541 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1542 			.dtim_period = vif->bss_conf.dtim_period,
1543 			.omac_idx = mvif->omac_idx,
1544 			.band_idx = mvif->band_idx,
1545 			.wmm_idx = mvif->wmm_idx,
1546 			.active = true, /* keep bss deactivated */
1547 			.phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1548 		},
1549 		.qos = {
1550 			.tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1551 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1552 			.qos = vif->bss_conf.qos,
1553 		},
1554 	};
1555 	int err, conn_type;
1556 	u8 idx, basic_phy;
1557 
1558 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1559 	basic_req.basic.hw_bss_idx = idx;
1560 	if (band == NL80211_BAND_6GHZ)
1561 		basic_req.basic.phymode_ext = PHY_MODE_AX_6G;
1562 
1563 	basic_phy = mt76_connac_get_phy_mode_v2(phy, vif, band, NULL);
1564 	basic_req.basic.nonht_basic_phy = cpu_to_le16(basic_phy);
1565 
1566 	switch (vif->type) {
1567 	case NL80211_IFTYPE_MESH_POINT:
1568 	case NL80211_IFTYPE_AP:
1569 		if (vif->p2p)
1570 			conn_type = CONNECTION_P2P_GO;
1571 		else
1572 			conn_type = CONNECTION_INFRA_AP;
1573 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1574 		/* Fully active/deactivate BSS network in AP mode only */
1575 		basic_req.basic.active = enable;
1576 		break;
1577 	case NL80211_IFTYPE_STATION:
1578 		if (vif->p2p)
1579 			conn_type = CONNECTION_P2P_GC;
1580 		else
1581 			conn_type = CONNECTION_INFRA_STA;
1582 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1583 		break;
1584 	case NL80211_IFTYPE_ADHOC:
1585 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1586 		break;
1587 	default:
1588 		WARN_ON(1);
1589 		break;
1590 	}
1591 
1592 	memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1593 	basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1594 	basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1595 	basic_req.basic.conn_state = !enable;
1596 
1597 	err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE), &basic_req,
1598 				sizeof(basic_req), true);
1599 	if (err < 0)
1600 		return err;
1601 
1602 	if (vif->bss_conf.he_support) {
1603 		struct {
1604 			struct {
1605 				u8 bss_idx;
1606 				u8 pad[3];
1607 			} __packed hdr;
1608 			struct bss_info_uni_he he;
1609 			struct bss_info_uni_bss_color bss_color;
1610 		} he_req = {
1611 			.hdr = {
1612 				.bss_idx = mvif->idx,
1613 			},
1614 			.he = {
1615 				.tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1616 				.len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1617 			},
1618 			.bss_color = {
1619 				.tag = cpu_to_le16(UNI_BSS_INFO_BSS_COLOR),
1620 				.len = cpu_to_le16(sizeof(struct bss_info_uni_bss_color)),
1621 				.enable = 0,
1622 				.bss_color = 0,
1623 			},
1624 		};
1625 
1626 		if (enable) {
1627 			he_req.bss_color.enable =
1628 				vif->bss_conf.he_bss_color.enabled;
1629 			he_req.bss_color.bss_color =
1630 				vif->bss_conf.he_bss_color.color;
1631 		}
1632 
1633 		mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1634 					       (struct tlv *)&he_req.he);
1635 		err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
1636 					&he_req, sizeof(he_req), true);
1637 		if (err < 0)
1638 			return err;
1639 	}
1640 
1641 	return mt76_connac_mcu_uni_set_chctx(phy, mvif, ctx);
1642 }
1643 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1644 
1645 #define MT76_CONNAC_SCAN_CHANNEL_TIME		60
1646 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1647 			    struct ieee80211_scan_request *scan_req)
1648 {
1649 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1650 	struct cfg80211_scan_request *sreq = &scan_req->req;
1651 	int n_ssids = 0, err, i, duration;
1652 	int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1653 	struct ieee80211_channel **scan_list = sreq->channels;
1654 	struct mt76_dev *mdev = phy->dev;
1655 	struct mt76_connac_mcu_scan_channel *chan;
1656 	struct mt76_connac_hw_scan_req *req;
1657 	struct sk_buff *skb;
1658 
1659 	if (test_bit(MT76_HW_SCANNING, &phy->state))
1660 		return -EBUSY;
1661 
1662 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1663 	if (!skb)
1664 		return -ENOMEM;
1665 
1666 	set_bit(MT76_HW_SCANNING, &phy->state);
1667 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1668 
1669 	req = (struct mt76_connac_hw_scan_req *)skb_put(skb, sizeof(*req));
1670 
1671 	req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1672 	req->bss_idx = mvif->idx;
1673 	req->scan_type = sreq->n_ssids ? 1 : 0;
1674 	req->probe_req_num = sreq->n_ssids ? 2 : 0;
1675 	req->version = 1;
1676 
1677 	for (i = 0; i < sreq->n_ssids; i++) {
1678 		if (!sreq->ssids[i].ssid_len)
1679 			continue;
1680 
1681 		req->ssids[i].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1682 		memcpy(req->ssids[i].ssid, sreq->ssids[i].ssid,
1683 		       sreq->ssids[i].ssid_len);
1684 		n_ssids++;
1685 	}
1686 	req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1687 	req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1688 	req->ssids_num = n_ssids;
1689 
1690 	duration = is_mt7921(phy->dev) ? 0 : MT76_CONNAC_SCAN_CHANNEL_TIME;
1691 	/* increase channel time for passive scan */
1692 	if (!sreq->n_ssids)
1693 		duration *= 2;
1694 	req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1695 	req->channel_min_dwell_time = cpu_to_le16(duration);
1696 	req->channel_dwell_time = cpu_to_le16(duration);
1697 
1698 	if (sreq->n_channels == 0 || sreq->n_channels > 64) {
1699 		req->channel_type = 0;
1700 		req->channels_num = 0;
1701 		req->ext_channels_num = 0;
1702 	} else {
1703 		req->channel_type = 4;
1704 		req->channels_num = min_t(u8, sreq->n_channels, 32);
1705 		req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1706 	}
1707 
1708 	for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1709 		if (i >= 32)
1710 			chan = &req->ext_channels[i - 32];
1711 		else
1712 			chan = &req->channels[i];
1713 
1714 		switch (scan_list[i]->band) {
1715 		case NL80211_BAND_2GHZ:
1716 			chan->band = 1;
1717 			break;
1718 		case NL80211_BAND_6GHZ:
1719 			chan->band = 3;
1720 			break;
1721 		default:
1722 			chan->band = 2;
1723 			break;
1724 		}
1725 		chan->channel_num = scan_list[i]->hw_value;
1726 	}
1727 
1728 	if (sreq->ie_len > 0) {
1729 		memcpy(req->ies, sreq->ie, sreq->ie_len);
1730 		req->ies_len = cpu_to_le16(sreq->ie_len);
1731 	}
1732 
1733 	if (is_mt7921(phy->dev))
1734 		req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
1735 
1736 	memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1737 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1738 		get_random_mask_addr(req->random_mac, sreq->mac_addr,
1739 				     sreq->mac_addr_mask);
1740 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
1741 	}
1742 
1743 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(START_HW_SCAN),
1744 				    false);
1745 	if (err < 0)
1746 		clear_bit(MT76_HW_SCANNING, &phy->state);
1747 
1748 	return err;
1749 }
1750 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1751 
1752 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1753 				   struct ieee80211_vif *vif)
1754 {
1755 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1756 	struct {
1757 		u8 seq_num;
1758 		u8 is_ext_channel;
1759 		u8 rsv[2];
1760 	} __packed req = {
1761 		.seq_num = mvif->scan_seq_num,
1762 	};
1763 
1764 	if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1765 		struct cfg80211_scan_info info = {
1766 			.aborted = true,
1767 		};
1768 
1769 		ieee80211_scan_completed(phy->hw, &info);
1770 	}
1771 
1772 	return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(CANCEL_HW_SCAN),
1773 				 &req, sizeof(req), false);
1774 }
1775 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1776 
1777 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1778 				   struct ieee80211_vif *vif,
1779 				   struct cfg80211_sched_scan_request *sreq)
1780 {
1781 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1782 	struct ieee80211_channel **scan_list = sreq->channels;
1783 	struct mt76_connac_mcu_scan_channel *chan;
1784 	struct mt76_connac_sched_scan_req *req;
1785 	struct mt76_dev *mdev = phy->dev;
1786 	struct cfg80211_match_set *match;
1787 	struct cfg80211_ssid *ssid;
1788 	struct sk_buff *skb;
1789 	int i;
1790 
1791 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1792 	if (!skb)
1793 		return -ENOMEM;
1794 
1795 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1796 
1797 	req = (struct mt76_connac_sched_scan_req *)skb_put(skb, sizeof(*req));
1798 	req->version = 1;
1799 	req->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
1800 
1801 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1802 		u8 *addr = is_mt7663(phy->dev) ? req->mt7663.random_mac
1803 					       : req->mt7921.random_mac;
1804 
1805 		req->scan_func = 1;
1806 		get_random_mask_addr(addr, sreq->mac_addr,
1807 				     sreq->mac_addr_mask);
1808 	}
1809 	if (is_mt7921(phy->dev)) {
1810 		req->mt7921.bss_idx = mvif->idx;
1811 		req->mt7921.delay = cpu_to_le32(sreq->delay);
1812 	}
1813 
1814 	req->ssids_num = sreq->n_ssids;
1815 	for (i = 0; i < req->ssids_num; i++) {
1816 		ssid = &sreq->ssids[i];
1817 		memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1818 		req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1819 	}
1820 
1821 	req->match_num = sreq->n_match_sets;
1822 	for (i = 0; i < req->match_num; i++) {
1823 		match = &sreq->match_sets[i];
1824 		memcpy(req->match[i].ssid, match->ssid.ssid,
1825 		       match->ssid.ssid_len);
1826 		req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1827 		req->match[i].ssid_len = match->ssid.ssid_len;
1828 	}
1829 
1830 	req->channel_type = sreq->n_channels ? 4 : 0;
1831 	req->channels_num = min_t(u8, sreq->n_channels, 64);
1832 	for (i = 0; i < req->channels_num; i++) {
1833 		chan = &req->channels[i];
1834 
1835 		switch (scan_list[i]->band) {
1836 		case NL80211_BAND_2GHZ:
1837 			chan->band = 1;
1838 			break;
1839 		case NL80211_BAND_6GHZ:
1840 			chan->band = 3;
1841 			break;
1842 		default:
1843 			chan->band = 2;
1844 			break;
1845 		}
1846 		chan->channel_num = scan_list[i]->hw_value;
1847 	}
1848 
1849 	req->intervals_num = sreq->n_scan_plans;
1850 	for (i = 0; i < req->intervals_num; i++)
1851 		req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1852 
1853 	if (sreq->ie_len > 0) {
1854 		req->ie_len = cpu_to_le16(sreq->ie_len);
1855 		memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1856 	}
1857 
1858 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_CE_CMD(SCHED_SCAN_REQ),
1859 				     false);
1860 }
1861 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1862 
1863 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1864 				      struct ieee80211_vif *vif,
1865 				      bool enable)
1866 {
1867 	struct {
1868 		u8 active; /* 0: enabled 1: disabled */
1869 		u8 rsv[3];
1870 	} __packed req = {
1871 		.active = !enable,
1872 	};
1873 
1874 	if (enable)
1875 		set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1876 	else
1877 		clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1878 
1879 	return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SCHED_SCAN_ENABLE),
1880 				 &req, sizeof(req), false);
1881 }
1882 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1883 
1884 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1885 {
1886 	struct mt76_connac_config req = {
1887 		.resp_type = 0,
1888 	};
1889 
1890 	memcpy(req.data, "assert", 7);
1891 
1892 	return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1893 				 &req, sizeof(req), false);
1894 }
1895 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1896 
1897 int mt76_connac_mcu_set_deep_sleep(struct mt76_dev *dev, bool enable)
1898 {
1899 	struct mt76_connac_config req = {
1900 		.resp_type = 0,
1901 	};
1902 
1903 	snprintf(req.data, sizeof(req.data), "KeepFullPwr %d", !enable);
1904 
1905 	return mt76_mcu_send_msg(dev, MCU_CE_CMD(CHIP_CONFIG),
1906 				 &req, sizeof(req), false);
1907 }
1908 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_deep_sleep);
1909 
1910 int mt76_connac_sta_state_dp(struct mt76_dev *dev,
1911 			     enum ieee80211_sta_state old_state,
1912 			     enum ieee80211_sta_state new_state)
1913 {
1914 	if ((old_state == IEEE80211_STA_ASSOC &&
1915 	     new_state == IEEE80211_STA_AUTHORIZED) ||
1916 	    (old_state == IEEE80211_STA_NONE &&
1917 	     new_state == IEEE80211_STA_NOTEXIST))
1918 		mt76_connac_mcu_set_deep_sleep(dev, true);
1919 
1920 	if ((old_state == IEEE80211_STA_NOTEXIST &&
1921 	     new_state == IEEE80211_STA_NONE) ||
1922 	    (old_state == IEEE80211_STA_AUTHORIZED &&
1923 	     new_state == IEEE80211_STA_ASSOC))
1924 		mt76_connac_mcu_set_deep_sleep(dev, false);
1925 
1926 	return 0;
1927 }
1928 EXPORT_SYMBOL_GPL(mt76_connac_sta_state_dp);
1929 
1930 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
1931 				    struct mt76_connac_coredump *coredump)
1932 {
1933 	spin_lock_bh(&dev->lock);
1934 	__skb_queue_tail(&coredump->msg_list, skb);
1935 	spin_unlock_bh(&dev->lock);
1936 
1937 	coredump->last_activity = jiffies;
1938 
1939 	queue_delayed_work(dev->wq, &coredump->work,
1940 			   MT76_CONNAC_COREDUMP_TIMEOUT);
1941 }
1942 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
1943 
1944 static void
1945 mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
1946 			  struct mt76_power_limits *limits,
1947 			  enum nl80211_band band)
1948 {
1949 	int max_power = is_mt7921(dev) ? 127 : 63;
1950 	int i, offset = sizeof(limits->cck);
1951 
1952 	memset(sku, max_power, MT_SKU_POWER_LIMIT);
1953 
1954 	if (band == NL80211_BAND_2GHZ) {
1955 		/* cck */
1956 		memcpy(sku, limits->cck, sizeof(limits->cck));
1957 	}
1958 
1959 	/* ofdm */
1960 	memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
1961 	offset += sizeof(limits->ofdm);
1962 
1963 	/* ht */
1964 	for (i = 0; i < 2; i++) {
1965 		memcpy(&sku[offset], limits->mcs[i], 8);
1966 		offset += 8;
1967 	}
1968 	sku[offset++] = limits->mcs[0][0];
1969 
1970 	/* vht */
1971 	for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
1972 		memcpy(&sku[offset], limits->mcs[i],
1973 		       ARRAY_SIZE(limits->mcs[i]));
1974 		offset += 12;
1975 	}
1976 
1977 	if (!is_mt7921(dev))
1978 		return;
1979 
1980 	/* he */
1981 	for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
1982 		memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
1983 		offset += ARRAY_SIZE(limits->ru[i]);
1984 	}
1985 }
1986 
1987 s8 mt76_connac_get_ch_power(struct mt76_phy *phy,
1988 			    struct ieee80211_channel *chan,
1989 			    s8 target_power)
1990 {
1991 	struct mt76_dev *dev = phy->dev;
1992 	struct ieee80211_supported_band *sband;
1993 	int i;
1994 
1995 	switch (chan->band) {
1996 	case NL80211_BAND_2GHZ:
1997 		sband = &phy->sband_2g.sband;
1998 		break;
1999 	case NL80211_BAND_5GHZ:
2000 		sband = &phy->sband_5g.sband;
2001 		break;
2002 	case NL80211_BAND_6GHZ:
2003 		sband = &phy->sband_6g.sband;
2004 		break;
2005 	default:
2006 		return target_power;
2007 	}
2008 
2009 	for (i = 0; i < sband->n_channels; i++) {
2010 		struct ieee80211_channel *ch = &sband->channels[i];
2011 
2012 		if (ch->hw_value == chan->hw_value) {
2013 			if (!(ch->flags & IEEE80211_CHAN_DISABLED)) {
2014 				int power = 2 * ch->max_reg_power;
2015 
2016 				if (is_mt7663(dev) && (power > 63 || power < -64))
2017 					power = 63;
2018 				target_power = min_t(s8, power, target_power);
2019 			}
2020 			break;
2021 		}
2022 	}
2023 
2024 	return target_power;
2025 }
2026 EXPORT_SYMBOL_GPL(mt76_connac_get_ch_power);
2027 
2028 static int
2029 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
2030 				  enum nl80211_band band)
2031 {
2032 	struct mt76_dev *dev = phy->dev;
2033 	int sku_len, batch_len = is_mt7921(dev) ? 8 : 16;
2034 	static const u8 chan_list_2ghz[] = {
2035 		1, 2,  3,  4,  5,  6,  7,
2036 		8, 9, 10, 11, 12, 13, 14
2037 	};
2038 	static const u8 chan_list_5ghz[] = {
2039 		 36,  38,  40,  42,  44,  46,  48,
2040 		 50,  52,  54,  56,  58,  60,  62,
2041 		 64, 100, 102, 104, 106, 108, 110,
2042 		112, 114, 116, 118, 120, 122, 124,
2043 		126, 128, 132, 134, 136, 138, 140,
2044 		142, 144, 149, 151, 153, 155, 157,
2045 		159, 161, 165, 169, 173, 177
2046 	};
2047 	static const u8 chan_list_6ghz[] = {
2048 		  1,   3,   5,   7,   9,  11,  13,
2049 		 15,  17,  19,  21,  23,  25,  27,
2050 		 29,  33,  35,  37,  39,  41,  43,
2051 		 45,  47,  49,  51,  53,  55,  57,
2052 		 59,  61,  65,  67,  69,  71,  73,
2053 		 75,  77,  79,  81,  83,  85,  87,
2054 		 89,  91,  93,  97,  99, 101, 103,
2055 		105, 107, 109, 111, 113, 115, 117,
2056 		119, 121, 123, 125, 129, 131, 133,
2057 		135, 137, 139, 141, 143, 145, 147,
2058 		149, 151, 153, 155, 157, 161, 163,
2059 		165, 167, 169, 171, 173, 175, 177,
2060 		179, 181, 183, 185, 187, 189, 193,
2061 		195, 197, 199, 201, 203, 205, 207,
2062 		209, 211, 213, 215, 217, 219, 221,
2063 		225, 227, 229, 233
2064 	};
2065 	int i, n_chan, batch_size, idx = 0, tx_power, last_ch, err = 0;
2066 	struct mt76_connac_sku_tlv sku_tlbv;
2067 	struct mt76_power_limits *limits;
2068 	const u8 *ch_list;
2069 
2070 	limits = devm_kmalloc(dev->dev, sizeof(*limits), GFP_KERNEL);
2071 	if (!limits)
2072 		return -ENOMEM;
2073 
2074 	sku_len = is_mt7921(dev) ? sizeof(sku_tlbv) : sizeof(sku_tlbv) - 92;
2075 	tx_power = 2 * phy->hw->conf.power_level;
2076 	if (!tx_power)
2077 		tx_power = 127;
2078 
2079 	if (band == NL80211_BAND_2GHZ) {
2080 		n_chan = ARRAY_SIZE(chan_list_2ghz);
2081 		ch_list = chan_list_2ghz;
2082 	} else if (band == NL80211_BAND_6GHZ) {
2083 		n_chan = ARRAY_SIZE(chan_list_6ghz);
2084 		ch_list = chan_list_6ghz;
2085 	} else {
2086 		n_chan = ARRAY_SIZE(chan_list_5ghz);
2087 		ch_list = chan_list_5ghz;
2088 	}
2089 	batch_size = DIV_ROUND_UP(n_chan, batch_len);
2090 
2091 	if (phy->cap.has_6ghz)
2092 		last_ch = chan_list_6ghz[ARRAY_SIZE(chan_list_6ghz) - 1];
2093 	else if (phy->cap.has_5ghz)
2094 		last_ch = chan_list_5ghz[ARRAY_SIZE(chan_list_5ghz) - 1];
2095 	else
2096 		last_ch = chan_list_2ghz[ARRAY_SIZE(chan_list_2ghz) - 1];
2097 
2098 	for (i = 0; i < batch_size; i++) {
2099 		struct mt76_connac_tx_power_limit_tlv tx_power_tlv = {};
2100 		int j, msg_len, num_ch;
2101 		struct sk_buff *skb;
2102 
2103 		num_ch = i == batch_size - 1 ? n_chan % batch_len : batch_len;
2104 		msg_len = sizeof(tx_power_tlv) + num_ch * sizeof(sku_tlbv);
2105 		skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
2106 		if (!skb) {
2107 			err = -ENOMEM;
2108 			goto out;
2109 		}
2110 
2111 		skb_reserve(skb, sizeof(tx_power_tlv));
2112 
2113 		BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv.alpha2));
2114 		memcpy(tx_power_tlv.alpha2, dev->alpha2, sizeof(dev->alpha2));
2115 		tx_power_tlv.n_chan = num_ch;
2116 
2117 		switch (band) {
2118 		case NL80211_BAND_2GHZ:
2119 			tx_power_tlv.band = 1;
2120 			break;
2121 		case NL80211_BAND_6GHZ:
2122 			tx_power_tlv.band = 3;
2123 			break;
2124 		default:
2125 			tx_power_tlv.band = 2;
2126 			break;
2127 		}
2128 
2129 		for (j = 0; j < num_ch; j++, idx++) {
2130 			struct ieee80211_channel chan = {
2131 				.hw_value = ch_list[idx],
2132 				.band = band,
2133 			};
2134 			s8 reg_power, sar_power;
2135 
2136 			reg_power = mt76_connac_get_ch_power(phy, &chan,
2137 							     tx_power);
2138 			sar_power = mt76_get_sar_power(phy, &chan, reg_power);
2139 
2140 			mt76_get_rate_power_limits(phy, &chan, limits,
2141 						   sar_power);
2142 
2143 			tx_power_tlv.last_msg = ch_list[idx] == last_ch;
2144 			sku_tlbv.channel = ch_list[idx];
2145 
2146 			mt76_connac_mcu_build_sku(dev, sku_tlbv.pwr_limit,
2147 						  limits, band);
2148 			skb_put_data(skb, &sku_tlbv, sku_len);
2149 		}
2150 		__skb_push(skb, sizeof(tx_power_tlv));
2151 		memcpy(skb->data, &tx_power_tlv, sizeof(tx_power_tlv));
2152 
2153 		err = mt76_mcu_skb_send_msg(dev, skb,
2154 					    MCU_CE_CMD(SET_RATE_TX_POWER),
2155 					    false);
2156 		if (err < 0)
2157 			goto out;
2158 	}
2159 
2160 out:
2161 	devm_kfree(dev->dev, limits);
2162 	return err;
2163 }
2164 
2165 int mt76_connac_mcu_set_rate_txpower(struct mt76_phy *phy)
2166 {
2167 	int err;
2168 
2169 	if (phy->cap.has_2ghz) {
2170 		err = mt76_connac_mcu_rate_txpower_band(phy,
2171 							NL80211_BAND_2GHZ);
2172 		if (err < 0)
2173 			return err;
2174 	}
2175 	if (phy->cap.has_5ghz) {
2176 		err = mt76_connac_mcu_rate_txpower_band(phy,
2177 							NL80211_BAND_5GHZ);
2178 		if (err < 0)
2179 			return err;
2180 	}
2181 	if (phy->cap.has_6ghz) {
2182 		err = mt76_connac_mcu_rate_txpower_band(phy,
2183 							NL80211_BAND_6GHZ);
2184 		if (err < 0)
2185 			return err;
2186 	}
2187 
2188 	return 0;
2189 }
2190 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rate_txpower);
2191 
2192 int mt76_connac_mcu_update_arp_filter(struct mt76_dev *dev,
2193 				      struct mt76_vif *vif,
2194 				      struct ieee80211_bss_conf *info)
2195 {
2196 	struct ieee80211_vif *mvif = container_of(info, struct ieee80211_vif,
2197 						  bss_conf);
2198 	struct sk_buff *skb;
2199 	int i, len = min_t(int, mvif->cfg.arp_addr_cnt,
2200 			   IEEE80211_BSS_ARP_ADDR_LIST_LEN);
2201 	struct {
2202 		struct {
2203 			u8 bss_idx;
2204 			u8 pad[3];
2205 		} __packed hdr;
2206 		struct mt76_connac_arpns_tlv arp;
2207 	} req_hdr = {
2208 		.hdr = {
2209 			.bss_idx = vif->idx,
2210 		},
2211 		.arp = {
2212 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2213 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2214 			.ips_num = len,
2215 			.mode = 2,  /* update */
2216 			.option = 1,
2217 		},
2218 	};
2219 
2220 	skb = mt76_mcu_msg_alloc(dev, NULL,
2221 				 sizeof(req_hdr) + len * sizeof(__be32));
2222 	if (!skb)
2223 		return -ENOMEM;
2224 
2225 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
2226 	for (i = 0; i < len; i++)
2227 		skb_put_data(skb, &mvif->cfg.arp_addr_list[i], sizeof(__be32));
2228 
2229 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(OFFLOAD), true);
2230 }
2231 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_arp_filter);
2232 
2233 int mt76_connac_mcu_set_p2p_oppps(struct ieee80211_hw *hw,
2234 				  struct ieee80211_vif *vif)
2235 {
2236 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2237 	int ct_window = vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
2238 	struct mt76_phy *phy = hw->priv;
2239 	struct {
2240 		__le32 ct_win;
2241 		u8 bss_idx;
2242 		u8 rsv[3];
2243 	} __packed req = {
2244 		.ct_win = cpu_to_le32(ct_window),
2245 		.bss_idx = mvif->idx,
2246 	};
2247 
2248 	return mt76_mcu_send_msg(phy->dev, MCU_CE_CMD(SET_P2P_OPPPS),
2249 				 &req, sizeof(req), false);
2250 }
2251 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_p2p_oppps);
2252 
2253 #ifdef CONFIG_PM
2254 
2255 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
2256 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
2257 		 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
2258 	.n_patterns = 1,
2259 	.pattern_min_len = 1,
2260 	.pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
2261 	.max_nd_match_sets = 10,
2262 };
2263 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
2264 
2265 static void
2266 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
2267 			 struct ieee80211_vif *vif,
2268 			 struct ieee80211_sta *sta,
2269 			 struct ieee80211_key_conf *key,
2270 			 void *data)
2271 {
2272 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
2273 	u32 cipher;
2274 
2275 	if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2276 	    key->cipher != WLAN_CIPHER_SUITE_CCMP &&
2277 	    key->cipher != WLAN_CIPHER_SUITE_TKIP)
2278 		return;
2279 
2280 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2281 		cipher = BIT(3);
2282 	else
2283 		cipher = BIT(4);
2284 
2285 	/* we are assuming here to have a single pairwise key */
2286 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2287 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2288 			gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
2289 		else
2290 			gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
2291 
2292 		gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
2293 		gtk_tlv->keyid = key->keyidx;
2294 	} else {
2295 		gtk_tlv->group_cipher = cpu_to_le32(cipher);
2296 	}
2297 }
2298 
2299 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
2300 				     struct ieee80211_vif *vif,
2301 				     struct cfg80211_gtk_rekey_data *key)
2302 {
2303 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2304 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
2305 	struct mt76_phy *phy = hw->priv;
2306 	struct sk_buff *skb;
2307 	struct {
2308 		u8 bss_idx;
2309 		u8 pad[3];
2310 	} __packed hdr = {
2311 		.bss_idx = mvif->idx,
2312 	};
2313 
2314 	skb = mt76_mcu_msg_alloc(phy->dev, NULL,
2315 				 sizeof(hdr) + sizeof(*gtk_tlv));
2316 	if (!skb)
2317 		return -ENOMEM;
2318 
2319 	skb_put_data(skb, &hdr, sizeof(hdr));
2320 	gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put(skb,
2321 							 sizeof(*gtk_tlv));
2322 	gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
2323 	gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
2324 	gtk_tlv->rekey_mode = 2;
2325 	gtk_tlv->option = 1;
2326 
2327 	rcu_read_lock();
2328 	ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
2329 	rcu_read_unlock();
2330 
2331 	memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
2332 	memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
2333 	memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
2334 
2335 	return mt76_mcu_skb_send_msg(phy->dev, skb,
2336 				     MCU_UNI_CMD(OFFLOAD), true);
2337 }
2338 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
2339 
2340 static int
2341 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
2342 			       bool suspend)
2343 {
2344 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2345 	struct {
2346 		struct {
2347 			u8 bss_idx;
2348 			u8 pad[3];
2349 		} __packed hdr;
2350 		struct mt76_connac_arpns_tlv arpns;
2351 	} req = {
2352 		.hdr = {
2353 			.bss_idx = mvif->idx,
2354 		},
2355 		.arpns = {
2356 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
2357 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
2358 			.mode = suspend,
2359 		},
2360 	};
2361 
2362 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2363 				 sizeof(req), true);
2364 }
2365 
2366 int
2367 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
2368 			      bool suspend)
2369 {
2370 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2371 	struct {
2372 		struct {
2373 			u8 bss_idx;
2374 			u8 pad[3];
2375 		} __packed hdr;
2376 		struct mt76_connac_gtk_rekey_tlv gtk_tlv;
2377 	} __packed req = {
2378 		.hdr = {
2379 			.bss_idx = mvif->idx,
2380 		},
2381 		.gtk_tlv = {
2382 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
2383 			.len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
2384 			.rekey_mode = !suspend,
2385 		},
2386 	};
2387 
2388 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(OFFLOAD), &req,
2389 				 sizeof(req), true);
2390 }
2391 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_gtk_rekey);
2392 
2393 int
2394 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
2395 				 struct ieee80211_vif *vif,
2396 				 bool enable, u8 mdtim,
2397 				 bool wow_suspend)
2398 {
2399 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2400 	struct {
2401 		struct {
2402 			u8 bss_idx;
2403 			u8 pad[3];
2404 		} __packed hdr;
2405 		struct mt76_connac_suspend_tlv suspend_tlv;
2406 	} req = {
2407 		.hdr = {
2408 			.bss_idx = mvif->idx,
2409 		},
2410 		.suspend_tlv = {
2411 			.tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
2412 			.len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
2413 			.enable = enable,
2414 			.mdtim = mdtim,
2415 			.wow_suspend = wow_suspend,
2416 		},
2417 	};
2418 
2419 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2420 				 sizeof(req), true);
2421 }
2422 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_mode);
2423 
2424 static int
2425 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
2426 				struct ieee80211_vif *vif,
2427 				u8 index, bool enable,
2428 				struct cfg80211_pkt_pattern *pattern)
2429 {
2430 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2431 	struct mt76_connac_wow_pattern_tlv *ptlv;
2432 	struct sk_buff *skb;
2433 	struct req_hdr {
2434 		u8 bss_idx;
2435 		u8 pad[3];
2436 	} __packed hdr = {
2437 		.bss_idx = mvif->idx,
2438 	};
2439 
2440 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
2441 	if (!skb)
2442 		return -ENOMEM;
2443 
2444 	skb_put_data(skb, &hdr, sizeof(hdr));
2445 	ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put(skb, sizeof(*ptlv));
2446 	ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
2447 	ptlv->len = cpu_to_le16(sizeof(*ptlv));
2448 	ptlv->data_len = pattern->pattern_len;
2449 	ptlv->enable = enable;
2450 	ptlv->index = index;
2451 
2452 	memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
2453 	memcpy(ptlv->mask, pattern->mask, DIV_ROUND_UP(pattern->pattern_len, 8));
2454 
2455 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SUSPEND), true);
2456 }
2457 
2458 int
2459 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
2460 			     bool suspend, struct cfg80211_wowlan *wowlan)
2461 {
2462 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2463 	struct mt76_dev *dev = phy->dev;
2464 	struct {
2465 		struct {
2466 			u8 bss_idx;
2467 			u8 pad[3];
2468 		} __packed hdr;
2469 		struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
2470 		struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
2471 	} req = {
2472 		.hdr = {
2473 			.bss_idx = mvif->idx,
2474 		},
2475 		.wow_ctrl_tlv = {
2476 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
2477 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
2478 			.cmd = suspend ? 1 : 2,
2479 		},
2480 		.gpio_tlv = {
2481 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
2482 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
2483 			.gpio_pin = 0xff, /* follow fw about GPIO pin */
2484 		},
2485 	};
2486 
2487 	if (wowlan->magic_pkt)
2488 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_MAGIC;
2489 	if (wowlan->disconnect)
2490 		req.wow_ctrl_tlv.trigger |= (UNI_WOW_DETECT_TYPE_DISCONNECT |
2491 					     UNI_WOW_DETECT_TYPE_BCN_LOST);
2492 	if (wowlan->nd_config) {
2493 		mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
2494 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_SCH_SCAN_HIT;
2495 		mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
2496 	}
2497 	if (wowlan->n_patterns)
2498 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_BITMAP;
2499 
2500 	if (mt76_is_mmio(dev))
2501 		req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
2502 	else if (mt76_is_usb(dev))
2503 		req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
2504 	else if (mt76_is_sdio(dev))
2505 		req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
2506 
2507 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
2508 				 sizeof(req), true);
2509 }
2510 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_wow_ctrl);
2511 
2512 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend)
2513 {
2514 	struct {
2515 		struct {
2516 			u8 hif_type; /* 0x0: HIF_SDIO
2517 				      * 0x1: HIF_USB
2518 				      * 0x2: HIF_PCIE
2519 				      */
2520 			u8 pad[3];
2521 		} __packed hdr;
2522 		struct hif_suspend_tlv {
2523 			__le16 tag;
2524 			__le16 len;
2525 			u8 suspend;
2526 		} __packed hif_suspend;
2527 	} req = {
2528 		.hif_suspend = {
2529 			.tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
2530 			.len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
2531 			.suspend = suspend,
2532 		},
2533 	};
2534 
2535 	if (mt76_is_mmio(dev))
2536 		req.hdr.hif_type = 2;
2537 	else if (mt76_is_usb(dev))
2538 		req.hdr.hif_type = 1;
2539 	else if (mt76_is_sdio(dev))
2540 		req.hdr.hif_type = 0;
2541 
2542 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(HIF_CTRL), &req,
2543 				 sizeof(req), true);
2544 }
2545 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
2546 
2547 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
2548 				      struct ieee80211_vif *vif)
2549 {
2550 	struct mt76_phy *phy = priv;
2551 	bool suspend = !test_bit(MT76_STATE_RUNNING, &phy->state);
2552 	struct ieee80211_hw *hw = phy->hw;
2553 	struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
2554 	int i;
2555 
2556 	mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
2557 	mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
2558 
2559 	mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
2560 
2561 	for (i = 0; i < wowlan->n_patterns; i++)
2562 		mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
2563 						&wowlan->patterns[i]);
2564 	mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
2565 }
2566 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
2567 #endif /* CONFIG_PM */
2568 
2569 u32 mt76_connac_mcu_reg_rr(struct mt76_dev *dev, u32 offset)
2570 {
2571 	struct {
2572 		__le32 addr;
2573 		__le32 val;
2574 	} __packed req = {
2575 		.addr = cpu_to_le32(offset),
2576 	};
2577 
2578 	return mt76_mcu_send_msg(dev, MCU_CE_QUERY(REG_READ), &req,
2579 				 sizeof(req), true);
2580 }
2581 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_rr);
2582 
2583 void mt76_connac_mcu_reg_wr(struct mt76_dev *dev, u32 offset, u32 val)
2584 {
2585 	struct {
2586 		__le32 addr;
2587 		__le32 val;
2588 	} __packed req = {
2589 		.addr = cpu_to_le32(offset),
2590 		.val = cpu_to_le32(val),
2591 	};
2592 
2593 	mt76_mcu_send_msg(dev, MCU_CE_CMD(REG_WRITE), &req,
2594 			  sizeof(req), false);
2595 }
2596 EXPORT_SYMBOL_GPL(mt76_connac_mcu_reg_wr);
2597 
2598 static int
2599 mt76_connac_mcu_sta_key_tlv(struct mt76_connac_sta_key_conf *sta_key_conf,
2600 			    struct sk_buff *skb,
2601 			    struct ieee80211_key_conf *key,
2602 			    enum set_key_cmd cmd)
2603 {
2604 	struct sta_rec_sec *sec;
2605 	u32 len = sizeof(*sec);
2606 	struct tlv *tlv;
2607 
2608 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V2, sizeof(*sec));
2609 	sec = (struct sta_rec_sec *)tlv;
2610 	sec->add = cmd;
2611 
2612 	if (cmd == SET_KEY) {
2613 		struct sec_key *sec_key;
2614 		u8 cipher;
2615 
2616 		cipher = mt76_connac_mcu_get_cipher(key->cipher);
2617 		if (cipher == MCU_CIPHER_NONE)
2618 			return -EOPNOTSUPP;
2619 
2620 		sec_key = &sec->key[0];
2621 		sec_key->cipher_len = sizeof(*sec_key);
2622 
2623 		if (cipher == MCU_CIPHER_BIP_CMAC_128) {
2624 			sec_key->cipher_id = MCU_CIPHER_AES_CCMP;
2625 			sec_key->key_id = sta_key_conf->keyidx;
2626 			sec_key->key_len = 16;
2627 			memcpy(sec_key->key, sta_key_conf->key, 16);
2628 
2629 			sec_key = &sec->key[1];
2630 			sec_key->cipher_id = MCU_CIPHER_BIP_CMAC_128;
2631 			sec_key->cipher_len = sizeof(*sec_key);
2632 			sec_key->key_len = 16;
2633 			memcpy(sec_key->key, key->key, 16);
2634 			sec->n_cipher = 2;
2635 		} else {
2636 			sec_key->cipher_id = cipher;
2637 			sec_key->key_id = key->keyidx;
2638 			sec_key->key_len = key->keylen;
2639 			memcpy(sec_key->key, key->key, key->keylen);
2640 
2641 			if (cipher == MCU_CIPHER_TKIP) {
2642 				/* Rx/Tx MIC keys are swapped */
2643 				memcpy(sec_key->key + 16, key->key + 24, 8);
2644 				memcpy(sec_key->key + 24, key->key + 16, 8);
2645 			}
2646 
2647 			/* store key_conf for BIP batch update */
2648 			if (cipher == MCU_CIPHER_AES_CCMP) {
2649 				memcpy(sta_key_conf->key, key->key, key->keylen);
2650 				sta_key_conf->keyidx = key->keyidx;
2651 			}
2652 
2653 			len -= sizeof(*sec_key);
2654 			sec->n_cipher = 1;
2655 		}
2656 	} else {
2657 		len -= sizeof(sec->key);
2658 		sec->n_cipher = 0;
2659 	}
2660 	sec->len = cpu_to_le16(len);
2661 
2662 	return 0;
2663 }
2664 
2665 int mt76_connac_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
2666 			    struct mt76_connac_sta_key_conf *sta_key_conf,
2667 			    struct ieee80211_key_conf *key, int mcu_cmd,
2668 			    struct mt76_wcid *wcid, enum set_key_cmd cmd)
2669 {
2670 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2671 	struct sk_buff *skb;
2672 	int ret;
2673 
2674 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
2675 	if (IS_ERR(skb))
2676 		return PTR_ERR(skb);
2677 
2678 	ret = mt76_connac_mcu_sta_key_tlv(sta_key_conf, skb, key, cmd);
2679 	if (ret)
2680 		return ret;
2681 
2682 	ret = mt76_connac_mcu_sta_wed_update(dev, skb);
2683 	if (ret)
2684 		return ret;
2685 
2686 	return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
2687 }
2688 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_key);
2689 
2690 /* SIFS 20us + 512 byte beacon transmitted by 1Mbps (3906us) */
2691 #define BCN_TX_ESTIMATE_TIME (4096 + 20)
2692 void mt76_connac_mcu_bss_ext_tlv(struct sk_buff *skb, struct mt76_vif *mvif)
2693 {
2694 	struct bss_info_ext_bss *ext;
2695 	int ext_bss_idx, tsf_offset;
2696 	struct tlv *tlv;
2697 
2698 	ext_bss_idx = mvif->omac_idx - EXT_BSSID_START;
2699 	if (ext_bss_idx < 0)
2700 		return;
2701 
2702 	tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_EXT_BSS, sizeof(*ext));
2703 
2704 	ext = (struct bss_info_ext_bss *)tlv;
2705 	tsf_offset = ext_bss_idx * BCN_TX_ESTIMATE_TIME;
2706 	ext->mbss_tsf_offset = cpu_to_le32(tsf_offset);
2707 }
2708 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_ext_tlv);
2709 
2710 int mt76_connac_mcu_bss_basic_tlv(struct sk_buff *skb,
2711 				  struct ieee80211_vif *vif,
2712 				  struct ieee80211_sta *sta,
2713 				  struct mt76_phy *phy, u16 wlan_idx,
2714 				  bool enable)
2715 {
2716 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
2717 	u32 type = vif->p2p ? NETWORK_P2P : NETWORK_INFRA;
2718 	struct bss_info_basic *bss;
2719 	struct tlv *tlv;
2720 
2721 	tlv = mt76_connac_mcu_add_tlv(skb, BSS_INFO_BASIC, sizeof(*bss));
2722 	bss = (struct bss_info_basic *)tlv;
2723 
2724 	switch (vif->type) {
2725 	case NL80211_IFTYPE_MESH_POINT:
2726 	case NL80211_IFTYPE_MONITOR:
2727 		break;
2728 	case NL80211_IFTYPE_AP:
2729 		if (ieee80211_hw_check(phy->hw, SUPPORTS_MULTI_BSSID)) {
2730 			u8 bssid_id = vif->bss_conf.bssid_indicator;
2731 			struct wiphy *wiphy = phy->hw->wiphy;
2732 
2733 			if (bssid_id > ilog2(wiphy->mbssid_max_interfaces))
2734 				return -EINVAL;
2735 
2736 			bss->non_tx_bssid = vif->bss_conf.bssid_index;
2737 			bss->max_bssid = bssid_id;
2738 		}
2739 		break;
2740 	case NL80211_IFTYPE_STATION:
2741 		if (enable) {
2742 			rcu_read_lock();
2743 			if (!sta)
2744 				sta = ieee80211_find_sta(vif,
2745 							 vif->bss_conf.bssid);
2746 			/* TODO: enable BSS_INFO_UAPSD & BSS_INFO_PM */
2747 			if (sta) {
2748 				struct mt76_wcid *wcid;
2749 
2750 				wcid = (struct mt76_wcid *)sta->drv_priv;
2751 				wlan_idx = wcid->idx;
2752 			}
2753 			rcu_read_unlock();
2754 		}
2755 		break;
2756 	case NL80211_IFTYPE_ADHOC:
2757 		type = NETWORK_IBSS;
2758 		break;
2759 	default:
2760 		WARN_ON(1);
2761 		break;
2762 	}
2763 
2764 	bss->network_type = cpu_to_le32(type);
2765 	bss->bmc_wcid_lo = to_wcid_lo(wlan_idx);
2766 	bss->bmc_wcid_hi = to_wcid_hi(wlan_idx);
2767 	bss->wmm_idx = mvif->wmm_idx;
2768 	bss->active = enable;
2769 	bss->cipher = mvif->cipher;
2770 
2771 	if (vif->type != NL80211_IFTYPE_MONITOR) {
2772 		struct cfg80211_chan_def *chandef = &phy->chandef;
2773 
2774 		memcpy(bss->bssid, vif->bss_conf.bssid, ETH_ALEN);
2775 		bss->bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int);
2776 		bss->dtim_period = vif->bss_conf.dtim_period;
2777 		bss->phy_mode = mt76_connac_get_phy_mode(phy, vif,
2778 							 chandef->chan->band, NULL);
2779 	} else {
2780 		memcpy(bss->bssid, phy->macaddr, ETH_ALEN);
2781 	}
2782 
2783 	return 0;
2784 }
2785 EXPORT_SYMBOL_GPL(mt76_connac_mcu_bss_basic_tlv);
2786 
2787 #define ENTER_PM_STATE		1
2788 #define EXIT_PM_STATE		2
2789 int mt76_connac_mcu_set_pm(struct mt76_dev *dev, int band, int enter)
2790 {
2791 	struct {
2792 		u8 pm_number;
2793 		u8 pm_state;
2794 		u8 bssid[ETH_ALEN];
2795 		u8 dtim_period;
2796 		u8 wlan_idx_lo;
2797 		__le16 bcn_interval;
2798 		__le32 aid;
2799 		__le32 rx_filter;
2800 		u8 band_idx;
2801 		u8 wlan_idx_hi;
2802 		u8 rsv[2];
2803 		__le32 feature;
2804 		u8 omac_idx;
2805 		u8 wmm_idx;
2806 		u8 bcn_loss_cnt;
2807 		u8 bcn_sp_duration;
2808 	} __packed req = {
2809 		.pm_number = 5,
2810 		.pm_state = enter ? ENTER_PM_STATE : EXIT_PM_STATE,
2811 		.band_idx = band,
2812 	};
2813 
2814 	return mt76_mcu_send_msg(dev, MCU_EXT_CMD(PM_STATE_CTRL), &req,
2815 				 sizeof(req), true);
2816 }
2817 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_pm);
2818 
2819 int mt76_connac_mcu_restart(struct mt76_dev *dev)
2820 {
2821 	struct {
2822 		u8 power_mode;
2823 		u8 rsv[3];
2824 	} req = {
2825 		.power_mode = 1,
2826 	};
2827 
2828 	return mt76_mcu_send_msg(dev, MCU_CMD(NIC_POWER_CTRL), &req,
2829 				 sizeof(req), false);
2830 }
2831 EXPORT_SYMBOL_GPL(mt76_connac_mcu_restart);
2832 
2833 int mt76_connac_mcu_rdd_cmd(struct mt76_dev *dev, int cmd, u8 index,
2834 			    u8 rx_sel, u8 val)
2835 {
2836 	struct {
2837 		u8 ctrl;
2838 		u8 rdd_idx;
2839 		u8 rdd_rx_sel;
2840 		u8 val;
2841 		u8 rsv[4];
2842 	} __packed req = {
2843 		.ctrl = cmd,
2844 		.rdd_idx = index,
2845 		.rdd_rx_sel = rx_sel,
2846 		.val = val,
2847 	};
2848 
2849 	return mt76_mcu_send_msg(dev, MCU_EXT_CMD(SET_RDD_CTRL), &req,
2850 				 sizeof(req), true);
2851 }
2852 EXPORT_SYMBOL_GPL(mt76_connac_mcu_rdd_cmd);
2853 
2854 static int
2855 mt76_connac_mcu_send_ram_firmware(struct mt76_dev *dev,
2856 				  const struct mt76_connac2_fw_trailer *hdr,
2857 				  const u8 *data, bool is_wa)
2858 {
2859 	int i, offset = 0, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
2860 	u32 override = 0, option = 0;
2861 
2862 	for (i = 0; i < hdr->n_region; i++) {
2863 		const struct mt76_connac2_fw_region *region;
2864 		u32 len, addr, mode;
2865 		int err;
2866 
2867 		region = (const void *)((const u8 *)hdr -
2868 					(hdr->n_region - i) * sizeof(*region));
2869 		mode = mt76_connac_mcu_gen_dl_mode(dev, region->feature_set,
2870 						   is_wa);
2871 		len = le32_to_cpu(region->len);
2872 		addr = le32_to_cpu(region->addr);
2873 
2874 		if (region->feature_set & FW_FEATURE_NON_DL)
2875 			goto next;
2876 
2877 		if (region->feature_set & FW_FEATURE_OVERRIDE_ADDR)
2878 			override = addr;
2879 
2880 		err = mt76_connac_mcu_init_download(dev, addr, len, mode);
2881 		if (err) {
2882 			dev_err(dev->dev, "Download request failed\n");
2883 			return err;
2884 		}
2885 
2886 		err = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
2887 					       data + offset, len, max_len);
2888 		if (err) {
2889 			dev_err(dev->dev, "Failed to send firmware.\n");
2890 			return err;
2891 		}
2892 
2893 next:
2894 		offset += len;
2895 	}
2896 
2897 	if (override)
2898 		option |= FW_START_OVERRIDE;
2899 	if (is_wa)
2900 		option |= FW_START_WORKING_PDA_CR4;
2901 
2902 	return mt76_connac_mcu_start_firmware(dev, override, option);
2903 }
2904 
2905 int mt76_connac2_load_ram(struct mt76_dev *dev, const char *fw_wm,
2906 			  const char *fw_wa)
2907 {
2908 	const struct mt76_connac2_fw_trailer *hdr;
2909 	const struct firmware *fw;
2910 	int ret;
2911 
2912 	ret = request_firmware(&fw, fw_wm, dev->dev);
2913 	if (ret)
2914 		return ret;
2915 
2916 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2917 		dev_err(dev->dev, "Invalid firmware\n");
2918 		ret = -EINVAL;
2919 		goto out;
2920 	}
2921 
2922 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
2923 	dev_info(dev->dev, "WM Firmware Version: %.10s, Build Time: %.15s\n",
2924 		 hdr->fw_ver, hdr->build_date);
2925 
2926 	ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, false);
2927 	if (ret) {
2928 		dev_err(dev->dev, "Failed to start WM firmware\n");
2929 		goto out;
2930 	}
2931 
2932 	snprintf(dev->hw->wiphy->fw_version,
2933 		 sizeof(dev->hw->wiphy->fw_version),
2934 		 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
2935 
2936 	release_firmware(fw);
2937 
2938 	if (!fw_wa)
2939 		return 0;
2940 
2941 	ret = request_firmware(&fw, fw_wa, dev->dev);
2942 	if (ret)
2943 		return ret;
2944 
2945 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
2946 		dev_err(dev->dev, "Invalid firmware\n");
2947 		ret = -EINVAL;
2948 		goto out;
2949 	}
2950 
2951 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
2952 	dev_info(dev->dev, "WA Firmware Version: %.10s, Build Time: %.15s\n",
2953 		 hdr->fw_ver, hdr->build_date);
2954 
2955 	ret = mt76_connac_mcu_send_ram_firmware(dev, hdr, fw->data, true);
2956 	if (ret) {
2957 		dev_err(dev->dev, "Failed to start WA firmware\n");
2958 		goto out;
2959 	}
2960 
2961 	snprintf(dev->hw->wiphy->fw_version,
2962 		 sizeof(dev->hw->wiphy->fw_version),
2963 		 "%.10s-%.15s", hdr->fw_ver, hdr->build_date);
2964 
2965 out:
2966 	release_firmware(fw);
2967 
2968 	return ret;
2969 }
2970 EXPORT_SYMBOL_GPL(mt76_connac2_load_ram);
2971 
2972 static u32 mt76_connac2_get_data_mode(struct mt76_dev *dev, u32 info)
2973 {
2974 	u32 mode = DL_MODE_NEED_RSP;
2975 
2976 	if ((!is_mt7921(dev) && !is_mt7925(dev)) || info == PATCH_SEC_NOT_SUPPORT)
2977 		return mode;
2978 
2979 	switch (FIELD_GET(PATCH_SEC_ENC_TYPE_MASK, info)) {
2980 	case PATCH_SEC_ENC_TYPE_PLAIN:
2981 		break;
2982 	case PATCH_SEC_ENC_TYPE_AES:
2983 		mode |= DL_MODE_ENCRYPT;
2984 		mode |= FIELD_PREP(DL_MODE_KEY_IDX,
2985 				(info & PATCH_SEC_ENC_AES_KEY_MASK)) & DL_MODE_KEY_IDX;
2986 		mode |= DL_MODE_RESET_SEC_IV;
2987 		break;
2988 	case PATCH_SEC_ENC_TYPE_SCRAMBLE:
2989 		mode |= DL_MODE_ENCRYPT;
2990 		mode |= DL_CONFIG_ENCRY_MODE_SEL;
2991 		mode |= DL_MODE_RESET_SEC_IV;
2992 		break;
2993 	default:
2994 		dev_err(dev->dev, "Encryption type not support!\n");
2995 	}
2996 
2997 	return mode;
2998 }
2999 
3000 int mt76_connac2_load_patch(struct mt76_dev *dev, const char *fw_name)
3001 {
3002 	int i, ret, sem, max_len = mt76_is_sdio(dev) ? 2048 : 4096;
3003 	const struct mt76_connac2_patch_hdr *hdr;
3004 	const struct firmware *fw = NULL;
3005 
3006 	sem = mt76_connac_mcu_patch_sem_ctrl(dev, true);
3007 	switch (sem) {
3008 	case PATCH_IS_DL:
3009 		return 0;
3010 	case PATCH_NOT_DL_SEM_SUCCESS:
3011 		break;
3012 	default:
3013 		dev_err(dev->dev, "Failed to get patch semaphore\n");
3014 		return -EAGAIN;
3015 	}
3016 
3017 	ret = request_firmware(&fw, fw_name, dev->dev);
3018 	if (ret)
3019 		goto out;
3020 
3021 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
3022 		dev_err(dev->dev, "Invalid firmware\n");
3023 		ret = -EINVAL;
3024 		goto out;
3025 	}
3026 
3027 	hdr = (const void *)fw->data;
3028 	dev_info(dev->dev, "HW/SW Version: 0x%x, Build Time: %.16s\n",
3029 		 be32_to_cpu(hdr->hw_sw_ver), hdr->build_date);
3030 
3031 	for (i = 0; i < be32_to_cpu(hdr->desc.n_region); i++) {
3032 		struct mt76_connac2_patch_sec *sec;
3033 		u32 len, addr, mode;
3034 		const u8 *dl;
3035 		u32 sec_info;
3036 
3037 		sec = (void *)(fw->data + sizeof(*hdr) + i * sizeof(*sec));
3038 		if ((be32_to_cpu(sec->type) & PATCH_SEC_TYPE_MASK) !=
3039 		    PATCH_SEC_TYPE_INFO) {
3040 			ret = -EINVAL;
3041 			goto out;
3042 		}
3043 
3044 		addr = be32_to_cpu(sec->info.addr);
3045 		len = be32_to_cpu(sec->info.len);
3046 		dl = fw->data + be32_to_cpu(sec->offs);
3047 		sec_info = be32_to_cpu(sec->info.sec_key_idx);
3048 		mode = mt76_connac2_get_data_mode(dev, sec_info);
3049 
3050 		ret = mt76_connac_mcu_init_download(dev, addr, len, mode);
3051 		if (ret) {
3052 			dev_err(dev->dev, "Download request failed\n");
3053 			goto out;
3054 		}
3055 
3056 		ret = __mt76_mcu_send_firmware(dev, MCU_CMD(FW_SCATTER),
3057 					       dl, len, max_len);
3058 		if (ret) {
3059 			dev_err(dev->dev, "Failed to send patch\n");
3060 			goto out;
3061 		}
3062 	}
3063 
3064 	ret = mt76_connac_mcu_start_patch(dev);
3065 	if (ret)
3066 		dev_err(dev->dev, "Failed to start patch\n");
3067 
3068 out:
3069 	sem = mt76_connac_mcu_patch_sem_ctrl(dev, false);
3070 	switch (sem) {
3071 	case PATCH_REL_SEM_SUCCESS:
3072 		break;
3073 	default:
3074 		ret = -EAGAIN;
3075 		dev_err(dev->dev, "Failed to release patch semaphore\n");
3076 		break;
3077 	}
3078 
3079 	release_firmware(fw);
3080 
3081 	return ret;
3082 }
3083 EXPORT_SYMBOL_GPL(mt76_connac2_load_patch);
3084 
3085 int mt76_connac2_mcu_fill_message(struct mt76_dev *dev, struct sk_buff *skb,
3086 				  int cmd, int *wait_seq)
3087 {
3088 	int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
3089 	struct mt76_connac2_mcu_uni_txd *uni_txd;
3090 	struct mt76_connac2_mcu_txd *mcu_txd;
3091 	__le32 *txd;
3092 	u32 val;
3093 	u8 seq;
3094 
3095 	/* TODO: make dynamic based on msg type */
3096 	dev->mcu.timeout = 20 * HZ;
3097 
3098 	seq = ++dev->mcu.msg_seq & 0xf;
3099 	if (!seq)
3100 		seq = ++dev->mcu.msg_seq & 0xf;
3101 
3102 	if (cmd == MCU_CMD(FW_SCATTER))
3103 		goto exit;
3104 
3105 	txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
3106 	txd = (__le32 *)skb_push(skb, txd_len);
3107 
3108 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
3109 	      FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
3110 	      FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
3111 	txd[0] = cpu_to_le32(val);
3112 
3113 	val = MT_TXD1_LONG_FORMAT |
3114 	      FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
3115 	txd[1] = cpu_to_le32(val);
3116 
3117 	if (cmd & __MCU_CMD_FIELD_UNI) {
3118 		uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
3119 		uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
3120 		uni_txd->option = MCU_CMD_UNI_EXT_ACK;
3121 		uni_txd->cid = cpu_to_le16(mcu_cmd);
3122 		uni_txd->s2d_index = MCU_S2D_H2N;
3123 		uni_txd->pkt_type = MCU_PKT_ID;
3124 		uni_txd->seq = seq;
3125 
3126 		goto exit;
3127 	}
3128 
3129 	mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
3130 	mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
3131 	mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
3132 					       MT_TX_MCU_PORT_RX_Q0));
3133 	mcu_txd->pkt_type = MCU_PKT_ID;
3134 	mcu_txd->seq = seq;
3135 	mcu_txd->cid = mcu_cmd;
3136 	mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
3137 
3138 	if (mcu_txd->ext_cid || (cmd & __MCU_CMD_FIELD_CE)) {
3139 		if (cmd & __MCU_CMD_FIELD_QUERY)
3140 			mcu_txd->set_query = MCU_Q_QUERY;
3141 		else
3142 			mcu_txd->set_query = MCU_Q_SET;
3143 		mcu_txd->ext_cid_ack = !!mcu_txd->ext_cid;
3144 	} else {
3145 		mcu_txd->set_query = MCU_Q_NA;
3146 	}
3147 
3148 	if (cmd & __MCU_CMD_FIELD_WA)
3149 		mcu_txd->s2d_index = MCU_S2D_H2C;
3150 	else
3151 		mcu_txd->s2d_index = MCU_S2D_H2N;
3152 
3153 exit:
3154 	if (wait_seq)
3155 		*wait_seq = seq;
3156 
3157 	return 0;
3158 }
3159 EXPORT_SYMBOL_GPL(mt76_connac2_mcu_fill_message);
3160 
3161 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
3162 MODULE_LICENSE("Dual BSD/GPL");
3163