xref: /linux/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include "mt76_connac_mcu.h"
5 
6 int mt76_connac_mcu_start_firmware(struct mt76_dev *dev, u32 addr, u32 option)
7 {
8 	struct {
9 		__le32 option;
10 		__le32 addr;
11 	} req = {
12 		.option = cpu_to_le32(option),
13 		.addr = cpu_to_le32(addr),
14 	};
15 
16 	return mt76_mcu_send_msg(dev, MCU_CMD_FW_START_REQ, &req, sizeof(req),
17 				 true);
18 }
19 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_firmware);
20 
21 int mt76_connac_mcu_patch_sem_ctrl(struct mt76_dev *dev, bool get)
22 {
23 	u32 op = get ? PATCH_SEM_GET : PATCH_SEM_RELEASE;
24 	struct {
25 		__le32 op;
26 	} req = {
27 		.op = cpu_to_le32(op),
28 	};
29 
30 	return mt76_mcu_send_msg(dev, MCU_CMD_PATCH_SEM_CONTROL, &req,
31 				 sizeof(req), true);
32 }
33 EXPORT_SYMBOL_GPL(mt76_connac_mcu_patch_sem_ctrl);
34 
35 int mt76_connac_mcu_start_patch(struct mt76_dev *dev)
36 {
37 	struct {
38 		u8 check_crc;
39 		u8 reserved[3];
40 	} req = {
41 		.check_crc = 0,
42 	};
43 
44 	return mt76_mcu_send_msg(dev, MCU_CMD_PATCH_FINISH_REQ, &req,
45 				 sizeof(req), true);
46 }
47 EXPORT_SYMBOL_GPL(mt76_connac_mcu_start_patch);
48 
49 #define MCU_PATCH_ADDRESS	0x200000
50 
51 int mt76_connac_mcu_init_download(struct mt76_dev *dev, u32 addr, u32 len,
52 				  u32 mode)
53 {
54 	struct {
55 		__le32 addr;
56 		__le32 len;
57 		__le32 mode;
58 	} req = {
59 		.addr = cpu_to_le32(addr),
60 		.len = cpu_to_le32(len),
61 		.mode = cpu_to_le32(mode),
62 	};
63 	int cmd;
64 
65 	if (is_mt7921(dev) &&
66 	    (req.addr == cpu_to_le32(MCU_PATCH_ADDRESS) || addr == 0x900000))
67 		cmd = MCU_CMD_PATCH_START_REQ;
68 	else
69 		cmd = MCU_CMD_TARGET_ADDRESS_LEN_REQ;
70 
71 	return mt76_mcu_send_msg(dev, cmd, &req, sizeof(req), true);
72 }
73 EXPORT_SYMBOL_GPL(mt76_connac_mcu_init_download);
74 
75 int mt76_connac_mcu_set_channel_domain(struct mt76_phy *phy)
76 {
77 	struct mt76_dev *dev = phy->dev;
78 	struct mt76_connac_mcu_channel_domain {
79 		u8 alpha2[4]; /* regulatory_request.alpha2 */
80 		u8 bw_2g; /* BW_20_40M		0
81 			   * BW_20M		1
82 			   * BW_20_40_80M	2
83 			   * BW_20_40_80_160M	3
84 			   * BW_20_40_80_8080M	4
85 			   */
86 		u8 bw_5g;
87 		__le16 pad;
88 		u8 n_2ch;
89 		u8 n_5ch;
90 		__le16 pad2;
91 	} __packed hdr = {
92 		.bw_2g = 0,
93 		.bw_5g = 3,
94 	};
95 	struct mt76_connac_mcu_chan {
96 		__le16 hw_value;
97 		__le16 pad;
98 		__le32 flags;
99 	} __packed channel;
100 	int len, i, n_max_channels, n_2ch = 0, n_5ch = 0;
101 	struct ieee80211_channel *chan;
102 	struct sk_buff *skb;
103 
104 	n_max_channels = phy->sband_2g.sband.n_channels +
105 			 phy->sband_5g.sband.n_channels;
106 	len = sizeof(hdr) + n_max_channels * sizeof(channel);
107 
108 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
109 	if (!skb)
110 		return -ENOMEM;
111 
112 	skb_reserve(skb, sizeof(hdr));
113 
114 	for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
115 		chan = &phy->sband_2g.sband.channels[i];
116 		if (chan->flags & IEEE80211_CHAN_DISABLED)
117 			continue;
118 
119 		channel.hw_value = cpu_to_le16(chan->hw_value);
120 		channel.flags = cpu_to_le32(chan->flags);
121 		channel.pad = 0;
122 
123 		skb_put_data(skb, &channel, sizeof(channel));
124 		n_2ch++;
125 	}
126 	for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
127 		chan = &phy->sband_5g.sband.channels[i];
128 		if (chan->flags & IEEE80211_CHAN_DISABLED)
129 			continue;
130 
131 		channel.hw_value = cpu_to_le16(chan->hw_value);
132 		channel.flags = cpu_to_le32(chan->flags);
133 		channel.pad = 0;
134 
135 		skb_put_data(skb, &channel, sizeof(channel));
136 		n_5ch++;
137 	}
138 
139 	BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(hdr.alpha2));
140 	memcpy(hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
141 	hdr.n_2ch = n_2ch;
142 	hdr.n_5ch = n_5ch;
143 
144 	memcpy(__skb_push(skb, sizeof(hdr)), &hdr, sizeof(hdr));
145 
146 	return mt76_mcu_skb_send_msg(dev, skb, MCU_CMD_SET_CHAN_DOMAIN, false);
147 }
148 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_channel_domain);
149 
150 int mt76_connac_mcu_set_mac_enable(struct mt76_dev *dev, int band, bool enable,
151 				   bool hdr_trans)
152 {
153 	struct {
154 		u8 enable;
155 		u8 band;
156 		u8 rsv[2];
157 	} __packed req_mac = {
158 		.enable = enable,
159 		.band = band,
160 	};
161 
162 	return mt76_mcu_send_msg(dev, MCU_EXT_CMD_MAC_INIT_CTRL, &req_mac,
163 				 sizeof(req_mac), true);
164 }
165 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_mac_enable);
166 
167 int mt76_connac_mcu_set_vif_ps(struct mt76_dev *dev, struct ieee80211_vif *vif)
168 {
169 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
170 	struct {
171 		u8 bss_idx;
172 		u8 ps_state; /* 0: device awake
173 			      * 1: static power save
174 			      * 2: dynamic power saving
175 			      */
176 	} req = {
177 		.bss_idx = mvif->idx,
178 		.ps_state = vif->bss_conf.ps ? 2 : 0,
179 	};
180 
181 	if (vif->type != NL80211_IFTYPE_STATION)
182 		return -EOPNOTSUPP;
183 
184 	return mt76_mcu_send_msg(dev, MCU_CMD_SET_PS_PROFILE, &req,
185 				 sizeof(req), false);
186 }
187 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_vif_ps);
188 
189 int mt76_connac_mcu_set_rts_thresh(struct mt76_dev *dev, u32 val, u8 band)
190 {
191 	struct {
192 		u8 prot_idx;
193 		u8 band;
194 		u8 rsv[2];
195 		__le32 len_thresh;
196 		__le32 pkt_thresh;
197 	} __packed req = {
198 		.prot_idx = 1,
199 		.band = band,
200 		.len_thresh = cpu_to_le32(val),
201 		.pkt_thresh = cpu_to_le32(0x2),
202 	};
203 
204 	return mt76_mcu_send_msg(dev, MCU_EXT_CMD_PROTECT_CTRL, &req,
205 				 sizeof(req), true);
206 }
207 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rts_thresh);
208 
209 void mt76_connac_mcu_beacon_loss_iter(void *priv, u8 *mac,
210 				      struct ieee80211_vif *vif)
211 {
212 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
213 	struct mt76_connac_beacon_loss_event *event = priv;
214 
215 	if (mvif->idx != event->bss_idx)
216 		return;
217 
218 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
219 		return;
220 
221 	ieee80211_beacon_loss(vif);
222 }
223 EXPORT_SYMBOL_GPL(mt76_connac_mcu_beacon_loss_iter);
224 
225 struct tlv *
226 mt76_connac_mcu_add_nested_tlv(struct sk_buff *skb, int tag, int len,
227 			       void *sta_ntlv, void *sta_wtbl)
228 {
229 	struct sta_ntlv_hdr *ntlv_hdr = sta_ntlv;
230 	struct tlv *sta_hdr = sta_wtbl;
231 	struct tlv *ptlv, tlv = {
232 		.tag = cpu_to_le16(tag),
233 		.len = cpu_to_le16(len),
234 	};
235 	u16 ntlv;
236 
237 	ptlv = skb_put(skb, len);
238 	memcpy(ptlv, &tlv, sizeof(tlv));
239 
240 	ntlv = le16_to_cpu(ntlv_hdr->tlv_num);
241 	ntlv_hdr->tlv_num = cpu_to_le16(ntlv + 1);
242 
243 	if (sta_hdr) {
244 		u16 size = le16_to_cpu(sta_hdr->len);
245 
246 		sta_hdr->len = cpu_to_le16(size + len);
247 	}
248 
249 	return ptlv;
250 }
251 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_nested_tlv);
252 
253 struct sk_buff *
254 mt76_connac_mcu_alloc_sta_req(struct mt76_dev *dev, struct mt76_vif *mvif,
255 			      struct mt76_wcid *wcid)
256 {
257 	struct sta_req_hdr hdr = {
258 		.bss_idx = mvif->idx,
259 		.muar_idx = wcid ? mvif->omac_idx : 0,
260 		.is_tlv_append = 1,
261 	};
262 	struct sk_buff *skb;
263 
264 	mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
265 				     &hdr.wlan_idx_hi);
266 	skb = mt76_mcu_msg_alloc(dev, NULL, MT76_CONNAC_STA_UPDATE_MAX_SIZE);
267 	if (!skb)
268 		return ERR_PTR(-ENOMEM);
269 
270 	skb_put_data(skb, &hdr, sizeof(hdr));
271 
272 	return skb;
273 }
274 EXPORT_SYMBOL_GPL(mt76_connac_mcu_alloc_sta_req);
275 
276 struct wtbl_req_hdr *
277 mt76_connac_mcu_alloc_wtbl_req(struct mt76_dev *dev, struct mt76_wcid *wcid,
278 			       int cmd, void *sta_wtbl, struct sk_buff **skb)
279 {
280 	struct tlv *sta_hdr = sta_wtbl;
281 	struct wtbl_req_hdr hdr = {
282 		.operation = cmd,
283 	};
284 	struct sk_buff *nskb = *skb;
285 
286 	mt76_connac_mcu_get_wlan_idx(dev, wcid, &hdr.wlan_idx_lo,
287 				     &hdr.wlan_idx_hi);
288 	if (!nskb) {
289 		nskb = mt76_mcu_msg_alloc(dev, NULL,
290 					  MT76_CONNAC_WTBL_UPDATE_MAX_SIZE);
291 		if (!nskb)
292 			return ERR_PTR(-ENOMEM);
293 
294 		*skb = nskb;
295 	}
296 
297 	if (sta_hdr)
298 		sta_hdr->len = cpu_to_le16(sizeof(hdr));
299 
300 	return skb_put_data(nskb, &hdr, sizeof(hdr));
301 }
302 EXPORT_SYMBOL_GPL(mt76_connac_mcu_alloc_wtbl_req);
303 
304 void mt76_connac_mcu_sta_basic_tlv(struct sk_buff *skb,
305 				   struct ieee80211_vif *vif,
306 				   struct ieee80211_sta *sta,
307 				   bool enable)
308 {
309 	struct sta_rec_basic *basic;
310 	struct tlv *tlv;
311 	int conn_type;
312 
313 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BASIC, sizeof(*basic));
314 
315 	basic = (struct sta_rec_basic *)tlv;
316 	basic->extra_info = cpu_to_le16(EXTRA_INFO_VER);
317 
318 	if (enable) {
319 		basic->extra_info |= cpu_to_le16(EXTRA_INFO_NEW);
320 		basic->conn_state = CONN_STATE_PORT_SECURE;
321 	} else {
322 		basic->conn_state = CONN_STATE_DISCONNECT;
323 	}
324 
325 	if (!sta) {
326 		basic->conn_type = cpu_to_le32(CONNECTION_INFRA_BC);
327 		eth_broadcast_addr(basic->peer_addr);
328 		return;
329 	}
330 
331 	switch (vif->type) {
332 	case NL80211_IFTYPE_MESH_POINT:
333 	case NL80211_IFTYPE_AP:
334 		if (vif->p2p)
335 			conn_type = CONNECTION_P2P_GC;
336 		else
337 			conn_type = CONNECTION_INFRA_STA;
338 		basic->conn_type = cpu_to_le32(conn_type);
339 		basic->aid = cpu_to_le16(sta->aid);
340 		break;
341 	case NL80211_IFTYPE_STATION:
342 		if (vif->p2p)
343 			conn_type = CONNECTION_P2P_GO;
344 		else
345 			conn_type = CONNECTION_INFRA_AP;
346 		basic->conn_type = cpu_to_le32(conn_type);
347 		basic->aid = cpu_to_le16(vif->bss_conf.aid);
348 		break;
349 	case NL80211_IFTYPE_ADHOC:
350 		basic->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
351 		basic->aid = cpu_to_le16(sta->aid);
352 		break;
353 	default:
354 		WARN_ON(1);
355 		break;
356 	}
357 
358 	memcpy(basic->peer_addr, sta->addr, ETH_ALEN);
359 	basic->qos = sta->wme;
360 }
361 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_basic_tlv);
362 
363 static void
364 mt76_connac_mcu_sta_uapsd(struct sk_buff *skb, struct ieee80211_vif *vif,
365 			  struct ieee80211_sta *sta)
366 {
367 	struct sta_rec_uapsd *uapsd;
368 	struct tlv *tlv;
369 
370 	if (vif->type != NL80211_IFTYPE_AP || !sta->wme)
371 		return;
372 
373 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_APPS, sizeof(*uapsd));
374 	uapsd = (struct sta_rec_uapsd *)tlv;
375 
376 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) {
377 		uapsd->dac_map |= BIT(3);
378 		uapsd->tac_map |= BIT(3);
379 	}
380 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) {
381 		uapsd->dac_map |= BIT(2);
382 		uapsd->tac_map |= BIT(2);
383 	}
384 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) {
385 		uapsd->dac_map |= BIT(1);
386 		uapsd->tac_map |= BIT(1);
387 	}
388 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) {
389 		uapsd->dac_map |= BIT(0);
390 		uapsd->tac_map |= BIT(0);
391 	}
392 	uapsd->max_sp = sta->max_sp;
393 }
394 
395 void mt76_connac_mcu_wtbl_hdr_trans_tlv(struct sk_buff *skb,
396 					struct mt76_wcid *wcid,
397 					void *sta_wtbl, void *wtbl_tlv)
398 {
399 	struct wtbl_hdr_trans *htr;
400 	struct tlv *tlv;
401 
402 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HDR_TRANS,
403 					     sizeof(*htr),
404 					     wtbl_tlv, sta_wtbl);
405 	htr = (struct wtbl_hdr_trans *)tlv;
406 	htr->no_rx_trans = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
407 }
408 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_hdr_trans_tlv);
409 
410 void mt76_connac_mcu_wtbl_generic_tlv(struct mt76_dev *dev,
411 				      struct sk_buff *skb,
412 				      struct ieee80211_vif *vif,
413 				      struct ieee80211_sta *sta,
414 				      void *sta_wtbl, void *wtbl_tlv)
415 {
416 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
417 	struct wtbl_generic *generic;
418 	struct wtbl_rx *rx;
419 	struct wtbl_spe *spe;
420 	struct tlv *tlv;
421 
422 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_GENERIC,
423 					     sizeof(*generic),
424 					     wtbl_tlv, sta_wtbl);
425 
426 	generic = (struct wtbl_generic *)tlv;
427 
428 	if (sta) {
429 		if (vif->type == NL80211_IFTYPE_STATION)
430 			generic->partial_aid = cpu_to_le16(vif->bss_conf.aid);
431 		else
432 			generic->partial_aid = cpu_to_le16(sta->aid);
433 		memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
434 		generic->muar_idx = mvif->omac_idx;
435 		generic->qos = sta->wme;
436 	} else {
437 		if (is_mt7921(dev) &&
438 		    vif->type == NL80211_IFTYPE_STATION)
439 			memcpy(generic->peer_addr, vif->bss_conf.bssid,
440 			       ETH_ALEN);
441 		else
442 			eth_broadcast_addr(generic->peer_addr);
443 
444 		generic->muar_idx = 0xe;
445 	}
446 
447 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
448 					     wtbl_tlv, sta_wtbl);
449 
450 	rx = (struct wtbl_rx *)tlv;
451 	rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
452 	rx->rca2 = 1;
453 	rx->rv = 1;
454 
455 	if (is_mt7921(dev))
456 		return;
457 
458 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SPE, sizeof(*spe),
459 					     wtbl_tlv, sta_wtbl);
460 	spe = (struct wtbl_spe *)tlv;
461 	spe->spe_idx = 24;
462 }
463 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_generic_tlv);
464 
465 static void
466 mt76_connac_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
467 			      struct ieee80211_vif *vif)
468 {
469 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
470 	struct sta_rec_amsdu *amsdu;
471 	struct tlv *tlv;
472 
473 	if (vif->type != NL80211_IFTYPE_AP &&
474 	    vif->type != NL80211_IFTYPE_STATION)
475 		return;
476 
477 	if (!sta->max_amsdu_len)
478 		return;
479 
480 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
481 	amsdu = (struct sta_rec_amsdu *)tlv;
482 	amsdu->max_amsdu_num = 8;
483 	amsdu->amsdu_en = true;
484 	amsdu->max_mpdu_size = sta->max_amsdu_len >=
485 			       IEEE80211_MAX_MPDU_LEN_VHT_7991;
486 
487 	wcid->amsdu = true;
488 }
489 
490 #define HE_PHY(p, c)	u8_get_bits(c, IEEE80211_HE_PHY_##p)
491 #define HE_MAC(m, c)	u8_get_bits(c, IEEE80211_HE_MAC_##m)
492 static void
493 mt76_connac_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
494 {
495 	struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
496 	struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
497 	struct sta_rec_he *he;
498 	struct tlv *tlv;
499 	u32 cap = 0;
500 
501 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
502 
503 	he = (struct sta_rec_he *)tlv;
504 
505 	if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
506 		cap |= STA_REC_HE_CAP_HTC;
507 
508 	if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
509 		cap |= STA_REC_HE_CAP_BSR;
510 
511 	if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
512 		cap |= STA_REC_HE_CAP_OM;
513 
514 	if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU)
515 		cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
516 
517 	if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
518 		cap |= STA_REC_HE_CAP_BQR;
519 
520 	if (elem->phy_cap_info[0] &
521 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
522 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
523 		cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
524 
525 	if (elem->phy_cap_info[1] &
526 	    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
527 		cap |= STA_REC_HE_CAP_LDPC;
528 
529 	if (elem->phy_cap_info[1] &
530 	    IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
531 		cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
532 
533 	if (elem->phy_cap_info[2] &
534 	    IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
535 		cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
536 
537 	if (elem->phy_cap_info[2] &
538 	    IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
539 		cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
540 
541 	if (elem->phy_cap_info[2] &
542 	    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
543 		cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
544 
545 	if (elem->phy_cap_info[6] &
546 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
547 		cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
548 
549 	if (elem->phy_cap_info[7] &
550 	    IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
551 		cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
552 
553 	if (elem->phy_cap_info[7] &
554 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
555 		cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
556 
557 	if (elem->phy_cap_info[7] &
558 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
559 		cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
560 
561 	if (elem->phy_cap_info[8] &
562 	    IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
563 		cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
564 
565 	if (elem->phy_cap_info[8] &
566 	    IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
567 		cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
568 
569 	if (elem->phy_cap_info[9] &
570 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
571 		cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
572 
573 	if (elem->phy_cap_info[9] &
574 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
575 		cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
576 
577 	if (elem->phy_cap_info[9] &
578 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
579 		cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
580 
581 	he->he_cap = cpu_to_le32(cap);
582 
583 	switch (sta->bandwidth) {
584 	case IEEE80211_STA_RX_BW_160:
585 		if (elem->phy_cap_info[0] &
586 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
587 			he->max_nss_mcs[CMD_HE_MCS_BW8080] =
588 				he_cap->he_mcs_nss_supp.rx_mcs_80p80;
589 
590 		he->max_nss_mcs[CMD_HE_MCS_BW160] =
591 				he_cap->he_mcs_nss_supp.rx_mcs_160;
592 		fallthrough;
593 	default:
594 		he->max_nss_mcs[CMD_HE_MCS_BW80] =
595 				he_cap->he_mcs_nss_supp.rx_mcs_80;
596 		break;
597 	}
598 
599 	he->t_frame_dur =
600 		HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
601 	he->max_ampdu_exp =
602 		HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
603 
604 	he->bw_set =
605 		HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
606 	he->device_class =
607 		HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
608 	he->punc_pream_rx =
609 		HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
610 
611 	he->dcm_tx_mode =
612 		HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
613 	he->dcm_tx_max_nss =
614 		HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
615 	he->dcm_rx_mode =
616 		HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
617 	he->dcm_rx_max_nss =
618 		HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
619 	he->dcm_rx_max_nss =
620 		HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
621 
622 	he->pkt_ext = 2;
623 }
624 
625 static u8
626 mt76_connac_get_phy_mode_v2(struct mt76_phy *mphy, struct ieee80211_vif *vif,
627 			    enum nl80211_band band, struct ieee80211_sta *sta)
628 {
629 	struct ieee80211_sta_ht_cap *ht_cap;
630 	struct ieee80211_sta_vht_cap *vht_cap;
631 	const struct ieee80211_sta_he_cap *he_cap;
632 	u8 mode = 0;
633 
634 	if (sta) {
635 		ht_cap = &sta->ht_cap;
636 		vht_cap = &sta->vht_cap;
637 		he_cap = &sta->he_cap;
638 	} else {
639 		struct ieee80211_supported_band *sband;
640 
641 		sband = mphy->hw->wiphy->bands[band];
642 		ht_cap = &sband->ht_cap;
643 		vht_cap = &sband->vht_cap;
644 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
645 	}
646 
647 	if (band == NL80211_BAND_2GHZ) {
648 		mode |= PHY_TYPE_BIT_HR_DSSS | PHY_TYPE_BIT_ERP;
649 
650 		if (ht_cap->ht_supported)
651 			mode |= PHY_TYPE_BIT_HT;
652 
653 		if (he_cap->has_he)
654 			mode |= PHY_TYPE_BIT_HE;
655 	} else if (band == NL80211_BAND_5GHZ) {
656 		mode |= PHY_TYPE_BIT_OFDM;
657 
658 		if (ht_cap->ht_supported)
659 			mode |= PHY_TYPE_BIT_HT;
660 
661 		if (vht_cap->vht_supported)
662 			mode |= PHY_TYPE_BIT_VHT;
663 
664 		if (he_cap->has_he)
665 			mode |= PHY_TYPE_BIT_HE;
666 	}
667 
668 	return mode;
669 }
670 
671 void mt76_connac_mcu_sta_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
672 			     struct ieee80211_sta *sta,
673 			     struct ieee80211_vif *vif,
674 			     u8 rcpi)
675 {
676 	struct cfg80211_chan_def *chandef = &mphy->chandef;
677 	enum nl80211_band band = chandef->chan->band;
678 	struct mt76_dev *dev = mphy->dev;
679 	struct sta_rec_ra_info *ra_info;
680 	struct sta_rec_state *state;
681 	struct sta_rec_phy *phy;
682 	struct tlv *tlv;
683 
684 	/* starec ht */
685 	if (sta->ht_cap.ht_supported) {
686 		struct sta_rec_ht *ht;
687 
688 		tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
689 		ht = (struct sta_rec_ht *)tlv;
690 		ht->ht_cap = cpu_to_le16(sta->ht_cap.cap);
691 	}
692 
693 	/* starec vht */
694 	if (sta->vht_cap.vht_supported) {
695 		struct sta_rec_vht *vht;
696 		int len;
697 
698 		len = is_mt7921(dev) ? sizeof(*vht) : sizeof(*vht) - 4;
699 		tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, len);
700 		vht = (struct sta_rec_vht *)tlv;
701 		vht->vht_cap = cpu_to_le32(sta->vht_cap.cap);
702 		vht->vht_rx_mcs_map = sta->vht_cap.vht_mcs.rx_mcs_map;
703 		vht->vht_tx_mcs_map = sta->vht_cap.vht_mcs.tx_mcs_map;
704 	}
705 
706 	/* starec uapsd */
707 	mt76_connac_mcu_sta_uapsd(skb, vif, sta);
708 
709 	if (!is_mt7921(dev))
710 		return;
711 
712 	if (sta->ht_cap.ht_supported)
713 		mt76_connac_mcu_sta_amsdu_tlv(skb, sta, vif);
714 
715 	/* starec he */
716 	if (sta->he_cap.has_he)
717 		mt76_connac_mcu_sta_he_tlv(skb, sta);
718 
719 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
720 	phy = (struct sta_rec_phy *)tlv;
721 	phy->phy_type = mt76_connac_get_phy_mode_v2(mphy, vif, band, sta);
722 	phy->basic_rate = cpu_to_le16((u16)vif->bss_conf.basic_rates);
723 	phy->rcpi = rcpi;
724 
725 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
726 	ra_info = (struct sta_rec_ra_info *)tlv;
727 	ra_info->legacy = cpu_to_le16((u16)sta->supp_rates[band]);
728 
729 	if (sta->ht_cap.ht_supported)
730 		memcpy(ra_info->rx_mcs_bitmask, sta->ht_cap.mcs.rx_mask,
731 		       HT_MCS_MASK_NUM);
732 
733 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
734 	state = (struct sta_rec_state *)tlv;
735 	state->state = 2;
736 
737 	if (sta->vht_cap.vht_supported) {
738 		state->vht_opmode = sta->bandwidth;
739 		state->vht_opmode |= (sta->rx_nss - 1) <<
740 			IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
741 	}
742 }
743 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
744 
745 static void
746 mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
747 			      void *sta_wtbl, void *wtbl_tlv)
748 {
749 	struct wtbl_smps *smps;
750 	struct tlv *tlv;
751 
752 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
753 					     wtbl_tlv, sta_wtbl);
754 	smps = (struct wtbl_smps *)tlv;
755 
756 	if (sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
757 		smps->smps = true;
758 }
759 
760 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
761 				 struct ieee80211_sta *sta, void *sta_wtbl,
762 				 void *wtbl_tlv)
763 {
764 	struct wtbl_ht *ht = NULL;
765 	struct tlv *tlv;
766 	u32 flags = 0;
767 
768 	if (sta->ht_cap.ht_supported) {
769 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
770 						     wtbl_tlv, sta_wtbl);
771 		ht = (struct wtbl_ht *)tlv;
772 		ht->ldpc = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
773 		ht->af = sta->ht_cap.ampdu_factor;
774 		ht->mm = sta->ht_cap.ampdu_density;
775 		ht->ht = true;
776 	}
777 
778 	if (sta->vht_cap.vht_supported) {
779 		struct wtbl_vht *vht;
780 		u8 af;
781 
782 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
783 						     sizeof(*vht), wtbl_tlv,
784 						     sta_wtbl);
785 		vht = (struct wtbl_vht *)tlv;
786 		vht->ldpc = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
787 		vht->vht = true;
788 
789 		af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
790 			       sta->vht_cap.cap);
791 		if (ht)
792 			ht->af = max(ht->af, af);
793 	}
794 
795 	mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
796 
797 	if (!is_mt7921(dev) && sta->ht_cap.ht_supported) {
798 		/* sgi */
799 		u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
800 			  MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
801 		struct wtbl_raw *raw;
802 
803 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
804 						     sizeof(*raw), wtbl_tlv,
805 						     sta_wtbl);
806 
807 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
808 			flags |= MT_WTBL_W5_SHORT_GI_20;
809 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
810 			flags |= MT_WTBL_W5_SHORT_GI_40;
811 
812 		if (sta->vht_cap.vht_supported) {
813 			if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
814 				flags |= MT_WTBL_W5_SHORT_GI_80;
815 			if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
816 				flags |= MT_WTBL_W5_SHORT_GI_160;
817 		}
818 		raw = (struct wtbl_raw *)tlv;
819 		raw->val = cpu_to_le32(flags);
820 		raw->msk = cpu_to_le32(~msk);
821 		raw->wtbl_idx = 1;
822 		raw->dw = 5;
823 	}
824 }
825 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
826 
827 int mt76_connac_mcu_add_sta_cmd(struct mt76_phy *phy,
828 				struct mt76_sta_cmd_info *info)
829 {
830 	struct mt76_vif *mvif = (struct mt76_vif *)info->vif->drv_priv;
831 	struct mt76_dev *dev = phy->dev;
832 	struct wtbl_req_hdr *wtbl_hdr;
833 	struct tlv *sta_wtbl;
834 	struct sk_buff *skb;
835 
836 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, info->wcid);
837 	if (IS_ERR(skb))
838 		return PTR_ERR(skb);
839 
840 	mt76_connac_mcu_sta_basic_tlv(skb, info->vif, info->sta, info->enable);
841 	if (info->enable && info->sta)
842 		mt76_connac_mcu_sta_tlv(phy, skb, info->sta, info->vif,
843 					info->rcpi);
844 
845 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
846 					   sizeof(struct tlv));
847 
848 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, info->wcid,
849 						  WTBL_RESET_AND_SET,
850 						  sta_wtbl, &skb);
851 	if (IS_ERR(wtbl_hdr))
852 		return PTR_ERR(wtbl_hdr);
853 
854 	if (info->enable) {
855 		mt76_connac_mcu_wtbl_generic_tlv(dev, skb, info->vif,
856 						 info->sta, sta_wtbl,
857 						 wtbl_hdr);
858 		if (info->sta)
859 			mt76_connac_mcu_wtbl_ht_tlv(dev, skb, info->sta,
860 						    sta_wtbl, wtbl_hdr);
861 	}
862 
863 	return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
864 }
865 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_sta_cmd);
866 
867 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
868 				 struct ieee80211_ampdu_params *params,
869 				 bool enable, bool tx, void *sta_wtbl,
870 				 void *wtbl_tlv)
871 {
872 	struct wtbl_ba *ba;
873 	struct tlv *tlv;
874 
875 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
876 					     wtbl_tlv, sta_wtbl);
877 
878 	ba = (struct wtbl_ba *)tlv;
879 	ba->tid = params->tid;
880 
881 	if (tx) {
882 		ba->ba_type = MT_BA_TYPE_ORIGINATOR;
883 		ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
884 		ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
885 		ba->ba_en = enable;
886 	} else {
887 		memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
888 		ba->ba_type = MT_BA_TYPE_RECIPIENT;
889 		ba->rst_ba_tid = params->tid;
890 		ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
891 		ba->rst_ba_sb = 1;
892 	}
893 
894 	if (is_mt7921(dev))
895 		return;
896 
897 	if (enable && tx) {
898 		u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
899 		int i;
900 
901 		for (i = 7; i > 0; i--) {
902 			if (params->buf_size >= ba_range[i])
903 				break;
904 		}
905 		ba->ba_winsize_idx = i;
906 	}
907 }
908 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
909 
910 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
911 				struct ieee80211_vif *vif,
912 				struct mt76_wcid *wcid,
913 				bool enable)
914 {
915 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
916 	struct mt76_dev *dev = phy->dev;
917 	struct {
918 		struct {
919 			u8 omac_idx;
920 			u8 band_idx;
921 			__le16 pad;
922 		} __packed hdr;
923 		struct req_tlv {
924 			__le16 tag;
925 			__le16 len;
926 			u8 active;
927 			u8 pad;
928 			u8 omac_addr[ETH_ALEN];
929 		} __packed tlv;
930 	} dev_req = {
931 		.hdr = {
932 			.omac_idx = mvif->omac_idx,
933 			.band_idx = mvif->band_idx,
934 		},
935 		.tlv = {
936 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
937 			.len = cpu_to_le16(sizeof(struct req_tlv)),
938 			.active = enable,
939 		},
940 	};
941 	struct {
942 		struct {
943 			u8 bss_idx;
944 			u8 pad[3];
945 		} __packed hdr;
946 		struct mt76_connac_bss_basic_tlv basic;
947 	} basic_req = {
948 		.hdr = {
949 			.bss_idx = mvif->idx,
950 		},
951 		.basic = {
952 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
953 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
954 			.omac_idx = mvif->omac_idx,
955 			.band_idx = mvif->band_idx,
956 			.wmm_idx = mvif->wmm_idx,
957 			.active = enable,
958 			.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
959 			.sta_idx = cpu_to_le16(wcid->idx),
960 			.conn_state = 1,
961 		},
962 	};
963 	int err, idx, cmd, len;
964 	void *data;
965 
966 	switch (vif->type) {
967 	case NL80211_IFTYPE_MESH_POINT:
968 	case NL80211_IFTYPE_MONITOR:
969 	case NL80211_IFTYPE_AP:
970 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
971 		break;
972 	case NL80211_IFTYPE_STATION:
973 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
974 		break;
975 	case NL80211_IFTYPE_ADHOC:
976 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
977 		break;
978 	default:
979 		WARN_ON(1);
980 		break;
981 	}
982 
983 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
984 	basic_req.basic.hw_bss_idx = idx;
985 
986 	memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
987 
988 	cmd = enable ? MCU_UNI_CMD_DEV_INFO_UPDATE : MCU_UNI_CMD_BSS_INFO_UPDATE;
989 	data = enable ? (void *)&dev_req : (void *)&basic_req;
990 	len = enable ? sizeof(dev_req) : sizeof(basic_req);
991 
992 	err = mt76_mcu_send_msg(dev, cmd, data, len, true);
993 	if (err < 0)
994 		return err;
995 
996 	cmd = enable ? MCU_UNI_CMD_BSS_INFO_UPDATE : MCU_UNI_CMD_DEV_INFO_UPDATE;
997 	data = enable ? (void *)&basic_req : (void *)&dev_req;
998 	len = enable ? sizeof(basic_req) : sizeof(dev_req);
999 
1000 	return mt76_mcu_send_msg(dev, cmd, data, len, true);
1001 }
1002 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
1003 
1004 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
1005 				struct ieee80211_ampdu_params *params,
1006 				bool enable, bool tx)
1007 {
1008 	struct sta_rec_ba *ba;
1009 	struct tlv *tlv;
1010 
1011 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1012 
1013 	ba = (struct sta_rec_ba *)tlv;
1014 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1015 	ba->winsize = cpu_to_le16(params->buf_size);
1016 	ba->ssn = cpu_to_le16(params->ssn);
1017 	ba->ba_en = enable << params->tid;
1018 	ba->amsdu = params->amsdu;
1019 	ba->tid = params->tid;
1020 }
1021 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1022 
1023 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif *mvif,
1024 			   struct ieee80211_ampdu_params *params,
1025 			   bool enable, bool tx)
1026 {
1027 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1028 	struct wtbl_req_hdr *wtbl_hdr;
1029 	struct tlv *sta_wtbl;
1030 	struct sk_buff *skb;
1031 	int ret;
1032 
1033 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1034 	if (IS_ERR(skb))
1035 		return PTR_ERR(skb);
1036 
1037 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1038 					   sizeof(struct tlv));
1039 
1040 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1041 						  sta_wtbl, &skb);
1042 	if (IS_ERR(wtbl_hdr))
1043 		return PTR_ERR(wtbl_hdr);
1044 
1045 	mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1046 				    wtbl_hdr);
1047 
1048 	ret = mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_STA_REC_UPDATE, true);
1049 	if (ret)
1050 		return ret;
1051 
1052 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1053 	if (IS_ERR(skb))
1054 		return PTR_ERR(skb);
1055 
1056 	mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1057 
1058 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_STA_REC_UPDATE,
1059 				     true);
1060 }
1061 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1062 
1063 static u8
1064 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1065 			 enum nl80211_band band,
1066 			 struct ieee80211_sta *sta)
1067 {
1068 	struct mt76_dev *dev = phy->dev;
1069 	const struct ieee80211_sta_he_cap *he_cap;
1070 	struct ieee80211_sta_vht_cap *vht_cap;
1071 	struct ieee80211_sta_ht_cap *ht_cap;
1072 	u8 mode = 0;
1073 
1074 	if (!is_mt7921(dev))
1075 		return 0x38;
1076 
1077 	if (sta) {
1078 		ht_cap = &sta->ht_cap;
1079 		vht_cap = &sta->vht_cap;
1080 		he_cap = &sta->he_cap;
1081 	} else {
1082 		struct ieee80211_supported_band *sband;
1083 
1084 		sband = phy->hw->wiphy->bands[band];
1085 		ht_cap = &sband->ht_cap;
1086 		vht_cap = &sband->vht_cap;
1087 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1088 	}
1089 
1090 	if (band == NL80211_BAND_2GHZ) {
1091 		mode |= PHY_MODE_B | PHY_MODE_G;
1092 
1093 		if (ht_cap->ht_supported)
1094 			mode |= PHY_MODE_GN;
1095 
1096 		if (he_cap->has_he)
1097 			mode |= PHY_MODE_AX_24G;
1098 	} else if (band == NL80211_BAND_5GHZ) {
1099 		mode |= PHY_MODE_A;
1100 
1101 		if (ht_cap->ht_supported)
1102 			mode |= PHY_MODE_AN;
1103 
1104 		if (vht_cap->vht_supported)
1105 			mode |= PHY_MODE_AC;
1106 
1107 		if (he_cap->has_he)
1108 			mode |= PHY_MODE_AX_5G;
1109 	}
1110 
1111 	return mode;
1112 }
1113 
1114 static const struct ieee80211_sta_he_cap *
1115 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1116 {
1117 	enum nl80211_band band = phy->chandef.chan->band;
1118 	struct ieee80211_supported_band *sband;
1119 
1120 	sband = phy->hw->wiphy->bands[band];
1121 
1122 	return ieee80211_get_he_iftype_cap(sband, vif->type);
1123 }
1124 
1125 #define DEFAULT_HE_PE_DURATION		4
1126 #define DEFAULT_HE_DURATION_RTS_THRES	1023
1127 static void
1128 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1129 			       struct tlv *tlv)
1130 {
1131 	const struct ieee80211_sta_he_cap *cap;
1132 	struct bss_info_uni_he *he;
1133 
1134 	cap = mt76_connac_get_he_phy_cap(phy, vif);
1135 
1136 	he = (struct bss_info_uni_he *)tlv;
1137 	he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1138 	if (!he->he_pe_duration)
1139 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1140 
1141 	he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1142 	if (!he->he_rts_thres)
1143 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1144 
1145 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1146 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1147 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1148 }
1149 
1150 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1151 				struct ieee80211_vif *vif,
1152 				struct mt76_wcid *wcid,
1153 				bool enable)
1154 {
1155 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1156 	struct cfg80211_chan_def *chandef = &phy->chandef;
1157 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1158 	enum nl80211_band band = chandef->chan->band;
1159 	struct mt76_dev *mdev = phy->dev;
1160 	struct {
1161 		struct {
1162 			u8 bss_idx;
1163 			u8 pad[3];
1164 		} __packed hdr;
1165 		struct mt76_connac_bss_basic_tlv basic;
1166 		struct mt76_connac_bss_qos_tlv qos;
1167 	} basic_req = {
1168 		.hdr = {
1169 			.bss_idx = mvif->idx,
1170 		},
1171 		.basic = {
1172 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1173 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1174 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1175 			.dtim_period = vif->bss_conf.dtim_period,
1176 			.omac_idx = mvif->omac_idx,
1177 			.band_idx = mvif->band_idx,
1178 			.wmm_idx = mvif->wmm_idx,
1179 			.active = true, /* keep bss deactivated */
1180 			.phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1181 		},
1182 		.qos = {
1183 			.tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1184 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1185 			.qos = vif->bss_conf.qos,
1186 		},
1187 	};
1188 	struct {
1189 		struct {
1190 			u8 bss_idx;
1191 			u8 pad[3];
1192 		} __packed hdr;
1193 		struct rlm_tlv {
1194 			__le16 tag;
1195 			__le16 len;
1196 			u8 control_channel;
1197 			u8 center_chan;
1198 			u8 center_chan2;
1199 			u8 bw;
1200 			u8 tx_streams;
1201 			u8 rx_streams;
1202 			u8 short_st;
1203 			u8 ht_op_info;
1204 			u8 sco;
1205 			u8 pad[3];
1206 		} __packed rlm;
1207 	} __packed rlm_req = {
1208 		.hdr = {
1209 			.bss_idx = mvif->idx,
1210 		},
1211 		.rlm = {
1212 			.tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1213 			.len = cpu_to_le16(sizeof(struct rlm_tlv)),
1214 			.control_channel = chandef->chan->hw_value,
1215 			.center_chan = ieee80211_frequency_to_channel(freq1),
1216 			.center_chan2 = ieee80211_frequency_to_channel(freq2),
1217 			.tx_streams = hweight8(phy->antenna_mask),
1218 			.ht_op_info = 4, /* set HT 40M allowed */
1219 			.rx_streams = phy->chainmask,
1220 			.short_st = true,
1221 		},
1222 	};
1223 	int err, conn_type;
1224 	u8 idx;
1225 
1226 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1227 	basic_req.basic.hw_bss_idx = idx;
1228 
1229 	switch (vif->type) {
1230 	case NL80211_IFTYPE_MESH_POINT:
1231 	case NL80211_IFTYPE_AP:
1232 		if (vif->p2p)
1233 			conn_type = CONNECTION_P2P_GO;
1234 		else
1235 			conn_type = CONNECTION_INFRA_AP;
1236 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1237 		break;
1238 	case NL80211_IFTYPE_STATION:
1239 		if (vif->p2p)
1240 			conn_type = CONNECTION_P2P_GC;
1241 		else
1242 			conn_type = CONNECTION_INFRA_STA;
1243 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1244 		break;
1245 	case NL80211_IFTYPE_ADHOC:
1246 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1247 		break;
1248 	default:
1249 		WARN_ON(1);
1250 		break;
1251 	}
1252 
1253 	memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1254 	basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1255 	basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1256 	basic_req.basic.conn_state = !enable;
1257 
1258 	err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE, &basic_req,
1259 				sizeof(basic_req), true);
1260 	if (err < 0)
1261 		return err;
1262 
1263 	if (vif->bss_conf.he_support) {
1264 		struct {
1265 			struct {
1266 				u8 bss_idx;
1267 				u8 pad[3];
1268 			} __packed hdr;
1269 			struct bss_info_uni_he he;
1270 		} he_req = {
1271 			.hdr = {
1272 				.bss_idx = mvif->idx,
1273 			},
1274 			.he = {
1275 				.tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1276 				.len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1277 			},
1278 		};
1279 
1280 		mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1281 					       (struct tlv *)&he_req.he);
1282 		err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE,
1283 					&he_req, sizeof(he_req), true);
1284 		if (err < 0)
1285 			return err;
1286 	}
1287 
1288 	switch (chandef->width) {
1289 	case NL80211_CHAN_WIDTH_40:
1290 		rlm_req.rlm.bw = CMD_CBW_40MHZ;
1291 		break;
1292 	case NL80211_CHAN_WIDTH_80:
1293 		rlm_req.rlm.bw = CMD_CBW_80MHZ;
1294 		break;
1295 	case NL80211_CHAN_WIDTH_80P80:
1296 		rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1297 		break;
1298 	case NL80211_CHAN_WIDTH_160:
1299 		rlm_req.rlm.bw = CMD_CBW_160MHZ;
1300 		break;
1301 	case NL80211_CHAN_WIDTH_5:
1302 		rlm_req.rlm.bw = CMD_CBW_5MHZ;
1303 		break;
1304 	case NL80211_CHAN_WIDTH_10:
1305 		rlm_req.rlm.bw = CMD_CBW_10MHZ;
1306 		break;
1307 	case NL80211_CHAN_WIDTH_20_NOHT:
1308 	case NL80211_CHAN_WIDTH_20:
1309 	default:
1310 		rlm_req.rlm.bw = CMD_CBW_20MHZ;
1311 		rlm_req.rlm.ht_op_info = 0;
1312 		break;
1313 	}
1314 
1315 	if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1316 		rlm_req.rlm.sco = 1; /* SCA */
1317 	else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1318 		rlm_req.rlm.sco = 3; /* SCB */
1319 
1320 	return mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE, &rlm_req,
1321 				 sizeof(rlm_req), true);
1322 }
1323 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1324 
1325 #define MT76_CONNAC_SCAN_CHANNEL_TIME		60
1326 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1327 			    struct ieee80211_scan_request *scan_req)
1328 {
1329 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1330 	struct cfg80211_scan_request *sreq = &scan_req->req;
1331 	int n_ssids = 0, err, i, duration;
1332 	int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1333 	struct ieee80211_channel **scan_list = sreq->channels;
1334 	struct mt76_dev *mdev = phy->dev;
1335 	bool ext_phy = phy == mdev->phy2;
1336 	struct mt76_connac_mcu_scan_channel *chan;
1337 	struct mt76_connac_hw_scan_req *req;
1338 	struct sk_buff *skb;
1339 
1340 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1341 	if (!skb)
1342 		return -ENOMEM;
1343 
1344 	set_bit(MT76_HW_SCANNING, &phy->state);
1345 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1346 
1347 	req = (struct mt76_connac_hw_scan_req *)skb_put(skb, sizeof(*req));
1348 
1349 	req->seq_num = mvif->scan_seq_num | ext_phy << 7;
1350 	req->bss_idx = mvif->idx;
1351 	req->scan_type = sreq->n_ssids ? 1 : 0;
1352 	req->probe_req_num = sreq->n_ssids ? 2 : 0;
1353 	req->version = 1;
1354 
1355 	for (i = 0; i < sreq->n_ssids; i++) {
1356 		if (!sreq->ssids[i].ssid_len)
1357 			continue;
1358 
1359 		req->ssids[i].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1360 		memcpy(req->ssids[i].ssid, sreq->ssids[i].ssid,
1361 		       sreq->ssids[i].ssid_len);
1362 		n_ssids++;
1363 	}
1364 	req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1365 	req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1366 	req->ssids_num = n_ssids;
1367 
1368 	duration = is_mt7921(phy->dev) ? 0 : MT76_CONNAC_SCAN_CHANNEL_TIME;
1369 	/* increase channel time for passive scan */
1370 	if (!sreq->n_ssids)
1371 		duration *= 2;
1372 	req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1373 	req->channel_min_dwell_time = cpu_to_le16(duration);
1374 	req->channel_dwell_time = cpu_to_le16(duration);
1375 
1376 	req->channels_num = min_t(u8, sreq->n_channels, 32);
1377 	req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1378 	for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1379 		if (i >= 32)
1380 			chan = &req->ext_channels[i - 32];
1381 		else
1382 			chan = &req->channels[i];
1383 
1384 		chan->band = scan_list[i]->band == NL80211_BAND_2GHZ ? 1 : 2;
1385 		chan->channel_num = scan_list[i]->hw_value;
1386 	}
1387 	req->channel_type = sreq->n_channels ? 4 : 0;
1388 
1389 	if (sreq->ie_len > 0) {
1390 		memcpy(req->ies, sreq->ie, sreq->ie_len);
1391 		req->ies_len = cpu_to_le16(sreq->ie_len);
1392 	}
1393 
1394 	if (is_mt7921(phy->dev))
1395 		req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
1396 
1397 	memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1398 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1399 		get_random_mask_addr(req->random_mac, sreq->mac_addr,
1400 				     sreq->mac_addr_mask);
1401 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
1402 	}
1403 
1404 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CMD_START_HW_SCAN, false);
1405 	if (err < 0)
1406 		clear_bit(MT76_HW_SCANNING, &phy->state);
1407 
1408 	return err;
1409 }
1410 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1411 
1412 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1413 				   struct ieee80211_vif *vif)
1414 {
1415 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1416 	struct {
1417 		u8 seq_num;
1418 		u8 is_ext_channel;
1419 		u8 rsv[2];
1420 	} __packed req = {
1421 		.seq_num = mvif->scan_seq_num,
1422 	};
1423 
1424 	if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1425 		struct cfg80211_scan_info info = {
1426 			.aborted = true,
1427 		};
1428 
1429 		ieee80211_scan_completed(phy->hw, &info);
1430 	}
1431 
1432 	return mt76_mcu_send_msg(phy->dev, MCU_CMD_CANCEL_HW_SCAN, &req,
1433 				 sizeof(req), false);
1434 }
1435 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1436 
1437 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1438 				   struct ieee80211_vif *vif,
1439 				   struct cfg80211_sched_scan_request *sreq)
1440 {
1441 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1442 	struct ieee80211_channel **scan_list = sreq->channels;
1443 	struct mt76_connac_mcu_scan_channel *chan;
1444 	struct mt76_connac_sched_scan_req *req;
1445 	struct mt76_dev *mdev = phy->dev;
1446 	bool ext_phy = phy == mdev->phy2;
1447 	struct cfg80211_match_set *match;
1448 	struct cfg80211_ssid *ssid;
1449 	struct sk_buff *skb;
1450 	int i;
1451 
1452 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1453 	if (!skb)
1454 		return -ENOMEM;
1455 
1456 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1457 
1458 	req = (struct mt76_connac_sched_scan_req *)skb_put(skb, sizeof(*req));
1459 	req->version = 1;
1460 	req->seq_num = mvif->scan_seq_num | ext_phy << 7;
1461 
1462 	if (is_mt7663(phy->dev) &&
1463 	    (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) {
1464 		get_random_mask_addr(req->mt7663.random_mac, sreq->mac_addr,
1465 				     sreq->mac_addr_mask);
1466 		req->scan_func = 1;
1467 	} else if (is_mt7921(phy->dev)) {
1468 		req->mt7921.bss_idx = mvif->idx;
1469 	}
1470 
1471 	req->ssids_num = sreq->n_ssids;
1472 	for (i = 0; i < req->ssids_num; i++) {
1473 		ssid = &sreq->ssids[i];
1474 		memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1475 		req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1476 	}
1477 
1478 	req->match_num = sreq->n_match_sets;
1479 	for (i = 0; i < req->match_num; i++) {
1480 		match = &sreq->match_sets[i];
1481 		memcpy(req->match[i].ssid, match->ssid.ssid,
1482 		       match->ssid.ssid_len);
1483 		req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1484 		req->match[i].ssid_len = match->ssid.ssid_len;
1485 	}
1486 
1487 	req->channel_type = sreq->n_channels ? 4 : 0;
1488 	req->channels_num = min_t(u8, sreq->n_channels, 64);
1489 	for (i = 0; i < req->channels_num; i++) {
1490 		chan = &req->channels[i];
1491 		chan->band = scan_list[i]->band == NL80211_BAND_2GHZ ? 1 : 2;
1492 		chan->channel_num = scan_list[i]->hw_value;
1493 	}
1494 
1495 	req->intervals_num = sreq->n_scan_plans;
1496 	for (i = 0; i < req->intervals_num; i++)
1497 		req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1498 
1499 	if (sreq->ie_len > 0) {
1500 		req->ie_len = cpu_to_le16(sreq->ie_len);
1501 		memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1502 	}
1503 
1504 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_CMD_SCHED_SCAN_REQ, false);
1505 }
1506 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1507 
1508 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1509 				      struct ieee80211_vif *vif,
1510 				      bool enable)
1511 {
1512 	struct {
1513 		u8 active; /* 0: enabled 1: disabled */
1514 		u8 rsv[3];
1515 	} __packed req = {
1516 		.active = !enable,
1517 	};
1518 
1519 	if (enable)
1520 		set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1521 	else
1522 		clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1523 
1524 	return mt76_mcu_send_msg(phy->dev, MCU_CMD_SCHED_SCAN_ENABLE, &req,
1525 				 sizeof(req), false);
1526 }
1527 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1528 
1529 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1530 {
1531 	struct mt76_connac_config req = {
1532 		.resp_type = 0,
1533 	};
1534 
1535 	memcpy(req.data, "assert", 7);
1536 
1537 	return mt76_mcu_send_msg(dev, MCU_CMD_CHIP_CONFIG, &req, sizeof(req),
1538 				 false);
1539 }
1540 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1541 
1542 int mt76_connac_mcu_set_deep_sleep(struct mt76_dev *dev, bool enable)
1543 {
1544 	struct mt76_connac_config req = {
1545 		.resp_type = 0,
1546 	};
1547 
1548 	snprintf(req.data, sizeof(req.data), "KeepFullPwr %d", !enable);
1549 
1550 	return mt76_mcu_send_msg(dev, MCU_CMD_CHIP_CONFIG, &req, sizeof(req),
1551 				 false);
1552 }
1553 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_deep_sleep);
1554 
1555 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
1556 				    struct mt76_connac_coredump *coredump)
1557 {
1558 	spin_lock_bh(&dev->lock);
1559 	__skb_queue_tail(&coredump->msg_list, skb);
1560 	spin_unlock_bh(&dev->lock);
1561 
1562 	coredump->last_activity = jiffies;
1563 
1564 	queue_delayed_work(dev->wq, &coredump->work,
1565 			   MT76_CONNAC_COREDUMP_TIMEOUT);
1566 }
1567 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
1568 
1569 static void
1570 mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
1571 			  struct mt76_power_limits *limits,
1572 			  enum nl80211_band band)
1573 {
1574 	int max_power = is_mt7921(dev) ? 127 : 63;
1575 	int i, offset = sizeof(limits->cck);
1576 
1577 	memset(sku, max_power, MT_SKU_POWER_LIMIT);
1578 
1579 	if (band == NL80211_BAND_2GHZ) {
1580 		/* cck */
1581 		memcpy(sku, limits->cck, sizeof(limits->cck));
1582 	}
1583 
1584 	/* ofdm */
1585 	memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
1586 	offset += sizeof(limits->ofdm);
1587 
1588 	/* ht */
1589 	for (i = 0; i < 2; i++) {
1590 		memcpy(&sku[offset], limits->mcs[i], 8);
1591 		offset += 8;
1592 	}
1593 	sku[offset++] = limits->mcs[0][0];
1594 
1595 	/* vht */
1596 	for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
1597 		memcpy(&sku[offset], limits->mcs[i],
1598 		       ARRAY_SIZE(limits->mcs[i]));
1599 		offset += 12;
1600 	}
1601 
1602 	if (!is_mt7921(dev))
1603 		return;
1604 
1605 	/* he */
1606 	for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
1607 		memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
1608 		offset += ARRAY_SIZE(limits->ru[i]);
1609 	}
1610 }
1611 
1612 static int
1613 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
1614 				  enum nl80211_band band)
1615 {
1616 	struct mt76_dev *dev = phy->dev;
1617 	int sku_len, batch_len = is_mt7921(dev) ? 8 : 16;
1618 	static const u8 chan_list_2ghz[] = {
1619 		1, 2,  3,  4,  5,  6,  7,
1620 		8, 9, 10, 11, 12, 13, 14
1621 	};
1622 	static const u8 chan_list_5ghz[] = {
1623 		 36,  38,  40,  42,  44,  46,  48,
1624 		 50,  52,  54,  56,  58,  60,  62,
1625 		 64, 100, 102, 104, 106, 108, 110,
1626 		112, 114, 116, 118, 120, 122, 124,
1627 		126, 128, 132, 134, 136, 138, 140,
1628 		142, 144, 149, 151, 153, 155, 157,
1629 		159, 161, 165
1630 	};
1631 	struct mt76_connac_sku_tlv sku_tlbv;
1632 	int i, n_chan, batch_size, idx = 0;
1633 	struct mt76_power_limits limits;
1634 	const u8 *ch_list;
1635 
1636 	sku_len = is_mt7921(dev) ? sizeof(sku_tlbv) : sizeof(sku_tlbv) - 92;
1637 
1638 	if (band == NL80211_BAND_2GHZ) {
1639 		n_chan = ARRAY_SIZE(chan_list_2ghz);
1640 		ch_list = chan_list_2ghz;
1641 	} else {
1642 		n_chan = ARRAY_SIZE(chan_list_5ghz);
1643 		ch_list = chan_list_5ghz;
1644 	}
1645 	batch_size = DIV_ROUND_UP(n_chan, batch_len);
1646 
1647 	for (i = 0; i < batch_size; i++) {
1648 		bool last_msg = i == batch_size - 1;
1649 		int num_ch = last_msg ? n_chan % batch_len : batch_len;
1650 		struct mt76_connac_tx_power_limit_tlv tx_power_tlv = {
1651 			.band = band == NL80211_BAND_2GHZ ? 1 : 2,
1652 			.n_chan = num_ch,
1653 			.last_msg = last_msg,
1654 		};
1655 		struct sk_buff *skb;
1656 		int j, err, msg_len;
1657 
1658 		msg_len = sizeof(tx_power_tlv) + num_ch * sizeof(sku_tlbv);
1659 		skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
1660 		if (!skb)
1661 			return -ENOMEM;
1662 
1663 		BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv.alpha2));
1664 		memcpy(tx_power_tlv.alpha2, dev->alpha2, sizeof(dev->alpha2));
1665 
1666 		skb_put_data(skb, &tx_power_tlv, sizeof(tx_power_tlv));
1667 		for (j = 0; j < num_ch; j++, idx++) {
1668 			struct ieee80211_channel chan = {
1669 				.hw_value = ch_list[idx],
1670 				.band = band,
1671 			};
1672 
1673 			mt76_get_rate_power_limits(phy, &chan, &limits, 127);
1674 
1675 			sku_tlbv.channel = ch_list[idx];
1676 			mt76_connac_mcu_build_sku(dev, sku_tlbv.pwr_limit,
1677 						  &limits, band);
1678 			skb_put_data(skb, &sku_tlbv, sku_len);
1679 		}
1680 
1681 		err = mt76_mcu_skb_send_msg(dev, skb,
1682 					    MCU_CMD_SET_RATE_TX_POWER, false);
1683 		if (err < 0)
1684 			return err;
1685 	}
1686 
1687 	return 0;
1688 }
1689 
1690 int mt76_connac_mcu_set_rate_txpower(struct mt76_phy *phy)
1691 {
1692 	int err;
1693 
1694 	err = mt76_connac_mcu_rate_txpower_band(phy, NL80211_BAND_2GHZ);
1695 	if (err < 0)
1696 		return err;
1697 
1698 	return mt76_connac_mcu_rate_txpower_band(phy, NL80211_BAND_5GHZ);
1699 }
1700 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rate_txpower);
1701 
1702 int mt76_connac_mcu_update_arp_filter(struct mt76_dev *dev,
1703 				      struct mt76_vif *vif,
1704 				      struct ieee80211_bss_conf *info)
1705 {
1706 	struct sk_buff *skb;
1707 	int i, len = min_t(int, info->arp_addr_cnt,
1708 			   IEEE80211_BSS_ARP_ADDR_LIST_LEN);
1709 	struct {
1710 		struct {
1711 			u8 bss_idx;
1712 			u8 pad[3];
1713 		} __packed hdr;
1714 		struct mt76_connac_arpns_tlv arp;
1715 	} req_hdr = {
1716 		.hdr = {
1717 			.bss_idx = vif->idx,
1718 		},
1719 		.arp = {
1720 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
1721 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
1722 			.ips_num = len,
1723 			.mode = 2,  /* update */
1724 			.option = 1,
1725 		},
1726 	};
1727 
1728 	skb = mt76_mcu_msg_alloc(dev, NULL,
1729 				 sizeof(req_hdr) + len * sizeof(__be32));
1730 	if (!skb)
1731 		return -ENOMEM;
1732 
1733 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1734 	for (i = 0; i < len; i++) {
1735 		u8 *addr = (u8 *)skb_put(skb, sizeof(__be32));
1736 
1737 		memcpy(addr, &info->arp_addr_list[i], sizeof(__be32));
1738 	}
1739 
1740 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_OFFLOAD, true);
1741 }
1742 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_arp_filter);
1743 
1744 #ifdef CONFIG_PM
1745 
1746 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
1747 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
1748 		 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
1749 	.n_patterns = 1,
1750 	.pattern_min_len = 1,
1751 	.pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
1752 	.max_nd_match_sets = 10,
1753 };
1754 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
1755 
1756 static void
1757 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
1758 			 struct ieee80211_vif *vif,
1759 			 struct ieee80211_sta *sta,
1760 			 struct ieee80211_key_conf *key,
1761 			 void *data)
1762 {
1763 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
1764 	u32 cipher;
1765 
1766 	if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
1767 	    key->cipher != WLAN_CIPHER_SUITE_CCMP &&
1768 	    key->cipher != WLAN_CIPHER_SUITE_TKIP)
1769 		return;
1770 
1771 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1772 		gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
1773 		cipher = BIT(3);
1774 	} else {
1775 		gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
1776 		cipher = BIT(4);
1777 	}
1778 
1779 	/* we are assuming here to have a single pairwise key */
1780 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
1781 		gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
1782 		gtk_tlv->group_cipher = cpu_to_le32(cipher);
1783 		gtk_tlv->keyid = key->keyidx;
1784 	}
1785 }
1786 
1787 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
1788 				     struct ieee80211_vif *vif,
1789 				     struct cfg80211_gtk_rekey_data *key)
1790 {
1791 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1792 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
1793 	struct mt76_phy *phy = hw->priv;
1794 	struct sk_buff *skb;
1795 	struct {
1796 		u8 bss_idx;
1797 		u8 pad[3];
1798 	} __packed hdr = {
1799 		.bss_idx = mvif->idx,
1800 	};
1801 
1802 	skb = mt76_mcu_msg_alloc(phy->dev, NULL,
1803 				 sizeof(hdr) + sizeof(*gtk_tlv));
1804 	if (!skb)
1805 		return -ENOMEM;
1806 
1807 	skb_put_data(skb, &hdr, sizeof(hdr));
1808 	gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put(skb,
1809 							 sizeof(*gtk_tlv));
1810 	gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
1811 	gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
1812 	gtk_tlv->rekey_mode = 2;
1813 	gtk_tlv->option = 1;
1814 
1815 	rcu_read_lock();
1816 	ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
1817 	rcu_read_unlock();
1818 
1819 	memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
1820 	memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
1821 	memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
1822 
1823 	return mt76_mcu_skb_send_msg(phy->dev, skb, MCU_UNI_CMD_OFFLOAD, true);
1824 }
1825 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
1826 
1827 static int
1828 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
1829 			       bool suspend)
1830 {
1831 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1832 	struct {
1833 		struct {
1834 			u8 bss_idx;
1835 			u8 pad[3];
1836 		} __packed hdr;
1837 		struct mt76_connac_arpns_tlv arpns;
1838 	} req = {
1839 		.hdr = {
1840 			.bss_idx = mvif->idx,
1841 		},
1842 		.arpns = {
1843 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
1844 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
1845 			.mode = suspend,
1846 		},
1847 	};
1848 
1849 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
1850 				 true);
1851 }
1852 
1853 static int
1854 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
1855 			      bool suspend)
1856 {
1857 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1858 	struct {
1859 		struct {
1860 			u8 bss_idx;
1861 			u8 pad[3];
1862 		} __packed hdr;
1863 		struct mt76_connac_gtk_rekey_tlv gtk_tlv;
1864 	} __packed req = {
1865 		.hdr = {
1866 			.bss_idx = mvif->idx,
1867 		},
1868 		.gtk_tlv = {
1869 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
1870 			.len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
1871 			.rekey_mode = !suspend,
1872 		},
1873 	};
1874 
1875 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
1876 				 true);
1877 }
1878 
1879 static int
1880 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
1881 				 struct ieee80211_vif *vif,
1882 				 bool enable, u8 mdtim,
1883 				 bool wow_suspend)
1884 {
1885 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1886 	struct {
1887 		struct {
1888 			u8 bss_idx;
1889 			u8 pad[3];
1890 		} __packed hdr;
1891 		struct mt76_connac_suspend_tlv suspend_tlv;
1892 	} req = {
1893 		.hdr = {
1894 			.bss_idx = mvif->idx,
1895 		},
1896 		.suspend_tlv = {
1897 			.tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
1898 			.len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
1899 			.enable = enable,
1900 			.mdtim = mdtim,
1901 			.wow_suspend = wow_suspend,
1902 		},
1903 	};
1904 
1905 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_SUSPEND, &req, sizeof(req),
1906 				 true);
1907 }
1908 
1909 static int
1910 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
1911 				struct ieee80211_vif *vif,
1912 				u8 index, bool enable,
1913 				struct cfg80211_pkt_pattern *pattern)
1914 {
1915 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1916 	struct mt76_connac_wow_pattern_tlv *ptlv;
1917 	struct sk_buff *skb;
1918 	struct req_hdr {
1919 		u8 bss_idx;
1920 		u8 pad[3];
1921 	} __packed hdr = {
1922 		.bss_idx = mvif->idx,
1923 	};
1924 
1925 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
1926 	if (!skb)
1927 		return -ENOMEM;
1928 
1929 	skb_put_data(skb, &hdr, sizeof(hdr));
1930 	ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put(skb, sizeof(*ptlv));
1931 	ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
1932 	ptlv->len = cpu_to_le16(sizeof(*ptlv));
1933 	ptlv->data_len = pattern->pattern_len;
1934 	ptlv->enable = enable;
1935 	ptlv->index = index;
1936 
1937 	memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
1938 	memcpy(ptlv->mask, pattern->mask, pattern->pattern_len / 8);
1939 
1940 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_SUSPEND, true);
1941 }
1942 
1943 static int
1944 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
1945 			     bool suspend, struct cfg80211_wowlan *wowlan)
1946 {
1947 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1948 	struct mt76_dev *dev = phy->dev;
1949 	struct {
1950 		struct {
1951 			u8 bss_idx;
1952 			u8 pad[3];
1953 		} __packed hdr;
1954 		struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
1955 		struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
1956 	} req = {
1957 		.hdr = {
1958 			.bss_idx = mvif->idx,
1959 		},
1960 		.wow_ctrl_tlv = {
1961 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
1962 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
1963 			.cmd = suspend ? 1 : 2,
1964 		},
1965 		.gpio_tlv = {
1966 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
1967 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
1968 			.gpio_pin = 0xff, /* follow fw about GPIO pin */
1969 		},
1970 	};
1971 
1972 	if (wowlan->magic_pkt)
1973 		req.wow_ctrl_tlv.trigger |= BIT(0);
1974 	if (wowlan->disconnect)
1975 		req.wow_ctrl_tlv.trigger |= BIT(2);
1976 	if (wowlan->nd_config) {
1977 		mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
1978 		req.wow_ctrl_tlv.trigger |= BIT(5);
1979 		mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
1980 	}
1981 
1982 	if (mt76_is_mmio(dev))
1983 		req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
1984 	else if (mt76_is_usb(dev))
1985 		req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
1986 	else if (mt76_is_sdio(dev))
1987 		req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
1988 
1989 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_SUSPEND, &req, sizeof(req),
1990 				 true);
1991 }
1992 
1993 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend)
1994 {
1995 	struct {
1996 		struct {
1997 			u8 hif_type; /* 0x0: HIF_SDIO
1998 				      * 0x1: HIF_USB
1999 				      * 0x2: HIF_PCIE
2000 				      */
2001 			u8 pad[3];
2002 		} __packed hdr;
2003 		struct hif_suspend_tlv {
2004 			__le16 tag;
2005 			__le16 len;
2006 			u8 suspend;
2007 		} __packed hif_suspend;
2008 	} req = {
2009 		.hif_suspend = {
2010 			.tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
2011 			.len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
2012 			.suspend = suspend,
2013 		},
2014 	};
2015 
2016 	if (mt76_is_mmio(dev))
2017 		req.hdr.hif_type = 2;
2018 	else if (mt76_is_usb(dev))
2019 		req.hdr.hif_type = 1;
2020 	else if (mt76_is_sdio(dev))
2021 		req.hdr.hif_type = 0;
2022 
2023 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_HIF_CTRL, &req, sizeof(req),
2024 				 true);
2025 }
2026 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
2027 
2028 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
2029 				      struct ieee80211_vif *vif)
2030 {
2031 	struct mt76_phy *phy = priv;
2032 	bool suspend = test_bit(MT76_STATE_SUSPEND, &phy->state);
2033 	struct ieee80211_hw *hw = phy->hw;
2034 	struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
2035 	int i;
2036 
2037 	mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
2038 	mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
2039 
2040 	mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
2041 
2042 	for (i = 0; i < wowlan->n_patterns; i++)
2043 		mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
2044 						&wowlan->patterns[i]);
2045 	mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
2046 }
2047 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
2048 
2049 #endif /* CONFIG_PM */
2050 
2051 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
2052 MODULE_LICENSE("Dual BSD/GPL");
2053