xref: /openbmc/linux/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.c (revision f8a11425075ff11b4b5784f077cb84f3d2dfb3f0)
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 	phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR,
725 				sta->ht_cap.ampdu_factor) |
726 		     FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY,
727 				sta->ht_cap.ampdu_density);
728 
729 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
730 	ra_info = (struct sta_rec_ra_info *)tlv;
731 	ra_info->legacy = cpu_to_le16((u16)sta->supp_rates[band]);
732 
733 	if (sta->ht_cap.ht_supported)
734 		memcpy(ra_info->rx_mcs_bitmask, sta->ht_cap.mcs.rx_mask,
735 		       HT_MCS_MASK_NUM);
736 
737 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
738 	state = (struct sta_rec_state *)tlv;
739 	state->state = 2;
740 
741 	if (sta->vht_cap.vht_supported) {
742 		state->vht_opmode = sta->bandwidth;
743 		state->vht_opmode |= (sta->rx_nss - 1) <<
744 			IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
745 	}
746 }
747 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
748 
749 static void
750 mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
751 			      void *sta_wtbl, void *wtbl_tlv)
752 {
753 	struct wtbl_smps *smps;
754 	struct tlv *tlv;
755 
756 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
757 					     wtbl_tlv, sta_wtbl);
758 	smps = (struct wtbl_smps *)tlv;
759 
760 	if (sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
761 		smps->smps = true;
762 }
763 
764 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
765 				 struct ieee80211_sta *sta, void *sta_wtbl,
766 				 void *wtbl_tlv)
767 {
768 	struct wtbl_ht *ht = NULL;
769 	struct tlv *tlv;
770 	u32 flags = 0;
771 
772 	if (sta->ht_cap.ht_supported) {
773 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
774 						     wtbl_tlv, sta_wtbl);
775 		ht = (struct wtbl_ht *)tlv;
776 		ht->ldpc = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
777 		ht->af = sta->ht_cap.ampdu_factor;
778 		ht->mm = sta->ht_cap.ampdu_density;
779 		ht->ht = true;
780 	}
781 
782 	if (sta->vht_cap.vht_supported) {
783 		struct wtbl_vht *vht;
784 		u8 af;
785 
786 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
787 						     sizeof(*vht), wtbl_tlv,
788 						     sta_wtbl);
789 		vht = (struct wtbl_vht *)tlv;
790 		vht->ldpc = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
791 		vht->vht = true;
792 
793 		af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
794 			       sta->vht_cap.cap);
795 		if (ht)
796 			ht->af = max(ht->af, af);
797 	}
798 
799 	mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
800 
801 	if (!is_mt7921(dev) && sta->ht_cap.ht_supported) {
802 		/* sgi */
803 		u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
804 			  MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
805 		struct wtbl_raw *raw;
806 
807 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
808 						     sizeof(*raw), wtbl_tlv,
809 						     sta_wtbl);
810 
811 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
812 			flags |= MT_WTBL_W5_SHORT_GI_20;
813 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
814 			flags |= MT_WTBL_W5_SHORT_GI_40;
815 
816 		if (sta->vht_cap.vht_supported) {
817 			if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
818 				flags |= MT_WTBL_W5_SHORT_GI_80;
819 			if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
820 				flags |= MT_WTBL_W5_SHORT_GI_160;
821 		}
822 		raw = (struct wtbl_raw *)tlv;
823 		raw->val = cpu_to_le32(flags);
824 		raw->msk = cpu_to_le32(~msk);
825 		raw->wtbl_idx = 1;
826 		raw->dw = 5;
827 	}
828 }
829 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
830 
831 int mt76_connac_mcu_add_sta_cmd(struct mt76_phy *phy,
832 				struct mt76_sta_cmd_info *info)
833 {
834 	struct mt76_vif *mvif = (struct mt76_vif *)info->vif->drv_priv;
835 	struct mt76_dev *dev = phy->dev;
836 	struct wtbl_req_hdr *wtbl_hdr;
837 	struct tlv *sta_wtbl;
838 	struct sk_buff *skb;
839 
840 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, info->wcid);
841 	if (IS_ERR(skb))
842 		return PTR_ERR(skb);
843 
844 	mt76_connac_mcu_sta_basic_tlv(skb, info->vif, info->sta, info->enable);
845 	if (info->enable && info->sta)
846 		mt76_connac_mcu_sta_tlv(phy, skb, info->sta, info->vif,
847 					info->rcpi);
848 
849 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
850 					   sizeof(struct tlv));
851 
852 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, info->wcid,
853 						  WTBL_RESET_AND_SET,
854 						  sta_wtbl, &skb);
855 	if (IS_ERR(wtbl_hdr))
856 		return PTR_ERR(wtbl_hdr);
857 
858 	if (info->enable) {
859 		mt76_connac_mcu_wtbl_generic_tlv(dev, skb, info->vif,
860 						 info->sta, sta_wtbl,
861 						 wtbl_hdr);
862 		if (info->sta)
863 			mt76_connac_mcu_wtbl_ht_tlv(dev, skb, info->sta,
864 						    sta_wtbl, wtbl_hdr);
865 	}
866 
867 	return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
868 }
869 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_sta_cmd);
870 
871 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
872 				 struct ieee80211_ampdu_params *params,
873 				 bool enable, bool tx, void *sta_wtbl,
874 				 void *wtbl_tlv)
875 {
876 	struct wtbl_ba *ba;
877 	struct tlv *tlv;
878 
879 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
880 					     wtbl_tlv, sta_wtbl);
881 
882 	ba = (struct wtbl_ba *)tlv;
883 	ba->tid = params->tid;
884 
885 	if (tx) {
886 		ba->ba_type = MT_BA_TYPE_ORIGINATOR;
887 		ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
888 		ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
889 		ba->ba_en = enable;
890 	} else {
891 		memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
892 		ba->ba_type = MT_BA_TYPE_RECIPIENT;
893 		ba->rst_ba_tid = params->tid;
894 		ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
895 		ba->rst_ba_sb = 1;
896 	}
897 
898 	if (is_mt7921(dev))
899 		return;
900 
901 	if (enable && tx) {
902 		u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
903 		int i;
904 
905 		for (i = 7; i > 0; i--) {
906 			if (params->buf_size >= ba_range[i])
907 				break;
908 		}
909 		ba->ba_winsize_idx = i;
910 	}
911 }
912 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
913 
914 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
915 				struct ieee80211_vif *vif,
916 				struct mt76_wcid *wcid,
917 				bool enable)
918 {
919 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
920 	struct mt76_dev *dev = phy->dev;
921 	struct {
922 		struct {
923 			u8 omac_idx;
924 			u8 band_idx;
925 			__le16 pad;
926 		} __packed hdr;
927 		struct req_tlv {
928 			__le16 tag;
929 			__le16 len;
930 			u8 active;
931 			u8 pad;
932 			u8 omac_addr[ETH_ALEN];
933 		} __packed tlv;
934 	} dev_req = {
935 		.hdr = {
936 			.omac_idx = mvif->omac_idx,
937 			.band_idx = mvif->band_idx,
938 		},
939 		.tlv = {
940 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
941 			.len = cpu_to_le16(sizeof(struct req_tlv)),
942 			.active = enable,
943 		},
944 	};
945 	struct {
946 		struct {
947 			u8 bss_idx;
948 			u8 pad[3];
949 		} __packed hdr;
950 		struct mt76_connac_bss_basic_tlv basic;
951 	} basic_req = {
952 		.hdr = {
953 			.bss_idx = mvif->idx,
954 		},
955 		.basic = {
956 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
957 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
958 			.omac_idx = mvif->omac_idx,
959 			.band_idx = mvif->band_idx,
960 			.wmm_idx = mvif->wmm_idx,
961 			.active = enable,
962 			.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
963 			.sta_idx = cpu_to_le16(wcid->idx),
964 			.conn_state = 1,
965 		},
966 	};
967 	int err, idx, cmd, len;
968 	void *data;
969 
970 	switch (vif->type) {
971 	case NL80211_IFTYPE_MESH_POINT:
972 	case NL80211_IFTYPE_MONITOR:
973 	case NL80211_IFTYPE_AP:
974 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
975 		break;
976 	case NL80211_IFTYPE_STATION:
977 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
978 		break;
979 	case NL80211_IFTYPE_ADHOC:
980 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
981 		break;
982 	default:
983 		WARN_ON(1);
984 		break;
985 	}
986 
987 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
988 	basic_req.basic.hw_bss_idx = idx;
989 
990 	memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
991 
992 	cmd = enable ? MCU_UNI_CMD_DEV_INFO_UPDATE : MCU_UNI_CMD_BSS_INFO_UPDATE;
993 	data = enable ? (void *)&dev_req : (void *)&basic_req;
994 	len = enable ? sizeof(dev_req) : sizeof(basic_req);
995 
996 	err = mt76_mcu_send_msg(dev, cmd, data, len, true);
997 	if (err < 0)
998 		return err;
999 
1000 	cmd = enable ? MCU_UNI_CMD_BSS_INFO_UPDATE : MCU_UNI_CMD_DEV_INFO_UPDATE;
1001 	data = enable ? (void *)&basic_req : (void *)&dev_req;
1002 	len = enable ? sizeof(basic_req) : sizeof(dev_req);
1003 
1004 	return mt76_mcu_send_msg(dev, cmd, data, len, true);
1005 }
1006 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
1007 
1008 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
1009 				struct ieee80211_ampdu_params *params,
1010 				bool enable, bool tx)
1011 {
1012 	struct sta_rec_ba *ba;
1013 	struct tlv *tlv;
1014 
1015 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
1016 
1017 	ba = (struct sta_rec_ba *)tlv;
1018 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
1019 	ba->winsize = cpu_to_le16(params->buf_size);
1020 	ba->ssn = cpu_to_le16(params->ssn);
1021 	ba->ba_en = enable << params->tid;
1022 	ba->amsdu = params->amsdu;
1023 	ba->tid = params->tid;
1024 }
1025 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1026 
1027 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif *mvif,
1028 			   struct ieee80211_ampdu_params *params,
1029 			   bool enable, bool tx)
1030 {
1031 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1032 	struct wtbl_req_hdr *wtbl_hdr;
1033 	struct tlv *sta_wtbl;
1034 	struct sk_buff *skb;
1035 	int ret;
1036 
1037 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1038 	if (IS_ERR(skb))
1039 		return PTR_ERR(skb);
1040 
1041 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1042 					   sizeof(struct tlv));
1043 
1044 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1045 						  sta_wtbl, &skb);
1046 	if (IS_ERR(wtbl_hdr))
1047 		return PTR_ERR(wtbl_hdr);
1048 
1049 	mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1050 				    wtbl_hdr);
1051 
1052 	ret = mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_STA_REC_UPDATE, true);
1053 	if (ret)
1054 		return ret;
1055 
1056 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1057 	if (IS_ERR(skb))
1058 		return PTR_ERR(skb);
1059 
1060 	mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1061 
1062 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_STA_REC_UPDATE,
1063 				     true);
1064 }
1065 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1066 
1067 static u8
1068 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1069 			 enum nl80211_band band,
1070 			 struct ieee80211_sta *sta)
1071 {
1072 	struct mt76_dev *dev = phy->dev;
1073 	const struct ieee80211_sta_he_cap *he_cap;
1074 	struct ieee80211_sta_vht_cap *vht_cap;
1075 	struct ieee80211_sta_ht_cap *ht_cap;
1076 	u8 mode = 0;
1077 
1078 	if (!is_mt7921(dev))
1079 		return 0x38;
1080 
1081 	if (sta) {
1082 		ht_cap = &sta->ht_cap;
1083 		vht_cap = &sta->vht_cap;
1084 		he_cap = &sta->he_cap;
1085 	} else {
1086 		struct ieee80211_supported_band *sband;
1087 
1088 		sband = phy->hw->wiphy->bands[band];
1089 		ht_cap = &sband->ht_cap;
1090 		vht_cap = &sband->vht_cap;
1091 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1092 	}
1093 
1094 	if (band == NL80211_BAND_2GHZ) {
1095 		mode |= PHY_MODE_B | PHY_MODE_G;
1096 
1097 		if (ht_cap->ht_supported)
1098 			mode |= PHY_MODE_GN;
1099 
1100 		if (he_cap->has_he)
1101 			mode |= PHY_MODE_AX_24G;
1102 	} else if (band == NL80211_BAND_5GHZ) {
1103 		mode |= PHY_MODE_A;
1104 
1105 		if (ht_cap->ht_supported)
1106 			mode |= PHY_MODE_AN;
1107 
1108 		if (vht_cap->vht_supported)
1109 			mode |= PHY_MODE_AC;
1110 
1111 		if (he_cap->has_he)
1112 			mode |= PHY_MODE_AX_5G;
1113 	}
1114 
1115 	return mode;
1116 }
1117 
1118 static const struct ieee80211_sta_he_cap *
1119 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1120 {
1121 	enum nl80211_band band = phy->chandef.chan->band;
1122 	struct ieee80211_supported_band *sband;
1123 
1124 	sband = phy->hw->wiphy->bands[band];
1125 
1126 	return ieee80211_get_he_iftype_cap(sband, vif->type);
1127 }
1128 
1129 #define DEFAULT_HE_PE_DURATION		4
1130 #define DEFAULT_HE_DURATION_RTS_THRES	1023
1131 static void
1132 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1133 			       struct tlv *tlv)
1134 {
1135 	const struct ieee80211_sta_he_cap *cap;
1136 	struct bss_info_uni_he *he;
1137 
1138 	cap = mt76_connac_get_he_phy_cap(phy, vif);
1139 
1140 	he = (struct bss_info_uni_he *)tlv;
1141 	he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1142 	if (!he->he_pe_duration)
1143 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1144 
1145 	he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1146 	if (!he->he_rts_thres)
1147 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1148 
1149 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1150 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1151 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1152 }
1153 
1154 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1155 				struct ieee80211_vif *vif,
1156 				struct mt76_wcid *wcid,
1157 				bool enable)
1158 {
1159 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1160 	struct cfg80211_chan_def *chandef = &phy->chandef;
1161 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1162 	enum nl80211_band band = chandef->chan->band;
1163 	struct mt76_dev *mdev = phy->dev;
1164 	struct {
1165 		struct {
1166 			u8 bss_idx;
1167 			u8 pad[3];
1168 		} __packed hdr;
1169 		struct mt76_connac_bss_basic_tlv basic;
1170 		struct mt76_connac_bss_qos_tlv qos;
1171 	} basic_req = {
1172 		.hdr = {
1173 			.bss_idx = mvif->idx,
1174 		},
1175 		.basic = {
1176 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1177 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1178 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1179 			.dtim_period = vif->bss_conf.dtim_period,
1180 			.omac_idx = mvif->omac_idx,
1181 			.band_idx = mvif->band_idx,
1182 			.wmm_idx = mvif->wmm_idx,
1183 			.active = true, /* keep bss deactivated */
1184 			.phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1185 		},
1186 		.qos = {
1187 			.tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1188 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1189 			.qos = vif->bss_conf.qos,
1190 		},
1191 	};
1192 	struct {
1193 		struct {
1194 			u8 bss_idx;
1195 			u8 pad[3];
1196 		} __packed hdr;
1197 		struct rlm_tlv {
1198 			__le16 tag;
1199 			__le16 len;
1200 			u8 control_channel;
1201 			u8 center_chan;
1202 			u8 center_chan2;
1203 			u8 bw;
1204 			u8 tx_streams;
1205 			u8 rx_streams;
1206 			u8 short_st;
1207 			u8 ht_op_info;
1208 			u8 sco;
1209 			u8 pad[3];
1210 		} __packed rlm;
1211 	} __packed rlm_req = {
1212 		.hdr = {
1213 			.bss_idx = mvif->idx,
1214 		},
1215 		.rlm = {
1216 			.tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1217 			.len = cpu_to_le16(sizeof(struct rlm_tlv)),
1218 			.control_channel = chandef->chan->hw_value,
1219 			.center_chan = ieee80211_frequency_to_channel(freq1),
1220 			.center_chan2 = ieee80211_frequency_to_channel(freq2),
1221 			.tx_streams = hweight8(phy->antenna_mask),
1222 			.ht_op_info = 4, /* set HT 40M allowed */
1223 			.rx_streams = phy->chainmask,
1224 			.short_st = true,
1225 		},
1226 	};
1227 	int err, conn_type;
1228 	u8 idx;
1229 
1230 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1231 	basic_req.basic.hw_bss_idx = idx;
1232 
1233 	switch (vif->type) {
1234 	case NL80211_IFTYPE_MESH_POINT:
1235 	case NL80211_IFTYPE_AP:
1236 		if (vif->p2p)
1237 			conn_type = CONNECTION_P2P_GO;
1238 		else
1239 			conn_type = CONNECTION_INFRA_AP;
1240 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1241 		break;
1242 	case NL80211_IFTYPE_STATION:
1243 		if (vif->p2p)
1244 			conn_type = CONNECTION_P2P_GC;
1245 		else
1246 			conn_type = CONNECTION_INFRA_STA;
1247 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1248 		break;
1249 	case NL80211_IFTYPE_ADHOC:
1250 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1251 		break;
1252 	default:
1253 		WARN_ON(1);
1254 		break;
1255 	}
1256 
1257 	memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1258 	basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1259 	basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1260 	basic_req.basic.conn_state = !enable;
1261 
1262 	err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE, &basic_req,
1263 				sizeof(basic_req), true);
1264 	if (err < 0)
1265 		return err;
1266 
1267 	if (vif->bss_conf.he_support) {
1268 		struct {
1269 			struct {
1270 				u8 bss_idx;
1271 				u8 pad[3];
1272 			} __packed hdr;
1273 			struct bss_info_uni_he he;
1274 		} he_req = {
1275 			.hdr = {
1276 				.bss_idx = mvif->idx,
1277 			},
1278 			.he = {
1279 				.tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1280 				.len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1281 			},
1282 		};
1283 
1284 		mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1285 					       (struct tlv *)&he_req.he);
1286 		err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE,
1287 					&he_req, sizeof(he_req), true);
1288 		if (err < 0)
1289 			return err;
1290 	}
1291 
1292 	switch (chandef->width) {
1293 	case NL80211_CHAN_WIDTH_40:
1294 		rlm_req.rlm.bw = CMD_CBW_40MHZ;
1295 		break;
1296 	case NL80211_CHAN_WIDTH_80:
1297 		rlm_req.rlm.bw = CMD_CBW_80MHZ;
1298 		break;
1299 	case NL80211_CHAN_WIDTH_80P80:
1300 		rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1301 		break;
1302 	case NL80211_CHAN_WIDTH_160:
1303 		rlm_req.rlm.bw = CMD_CBW_160MHZ;
1304 		break;
1305 	case NL80211_CHAN_WIDTH_5:
1306 		rlm_req.rlm.bw = CMD_CBW_5MHZ;
1307 		break;
1308 	case NL80211_CHAN_WIDTH_10:
1309 		rlm_req.rlm.bw = CMD_CBW_10MHZ;
1310 		break;
1311 	case NL80211_CHAN_WIDTH_20_NOHT:
1312 	case NL80211_CHAN_WIDTH_20:
1313 	default:
1314 		rlm_req.rlm.bw = CMD_CBW_20MHZ;
1315 		rlm_req.rlm.ht_op_info = 0;
1316 		break;
1317 	}
1318 
1319 	if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1320 		rlm_req.rlm.sco = 1; /* SCA */
1321 	else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1322 		rlm_req.rlm.sco = 3; /* SCB */
1323 
1324 	return mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE, &rlm_req,
1325 				 sizeof(rlm_req), true);
1326 }
1327 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1328 
1329 #define MT76_CONNAC_SCAN_CHANNEL_TIME		60
1330 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1331 			    struct ieee80211_scan_request *scan_req)
1332 {
1333 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1334 	struct cfg80211_scan_request *sreq = &scan_req->req;
1335 	int n_ssids = 0, err, i, duration;
1336 	int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1337 	struct ieee80211_channel **scan_list = sreq->channels;
1338 	struct mt76_dev *mdev = phy->dev;
1339 	bool ext_phy = phy == mdev->phy2;
1340 	struct mt76_connac_mcu_scan_channel *chan;
1341 	struct mt76_connac_hw_scan_req *req;
1342 	struct sk_buff *skb;
1343 
1344 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1345 	if (!skb)
1346 		return -ENOMEM;
1347 
1348 	set_bit(MT76_HW_SCANNING, &phy->state);
1349 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1350 
1351 	req = (struct mt76_connac_hw_scan_req *)skb_put(skb, sizeof(*req));
1352 
1353 	req->seq_num = mvif->scan_seq_num | ext_phy << 7;
1354 	req->bss_idx = mvif->idx;
1355 	req->scan_type = sreq->n_ssids ? 1 : 0;
1356 	req->probe_req_num = sreq->n_ssids ? 2 : 0;
1357 	req->version = 1;
1358 
1359 	for (i = 0; i < sreq->n_ssids; i++) {
1360 		if (!sreq->ssids[i].ssid_len)
1361 			continue;
1362 
1363 		req->ssids[i].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1364 		memcpy(req->ssids[i].ssid, sreq->ssids[i].ssid,
1365 		       sreq->ssids[i].ssid_len);
1366 		n_ssids++;
1367 	}
1368 	req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1369 	req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1370 	req->ssids_num = n_ssids;
1371 
1372 	duration = is_mt7921(phy->dev) ? 0 : MT76_CONNAC_SCAN_CHANNEL_TIME;
1373 	/* increase channel time for passive scan */
1374 	if (!sreq->n_ssids)
1375 		duration *= 2;
1376 	req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1377 	req->channel_min_dwell_time = cpu_to_le16(duration);
1378 	req->channel_dwell_time = cpu_to_le16(duration);
1379 
1380 	req->channels_num = min_t(u8, sreq->n_channels, 32);
1381 	req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1382 	for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1383 		if (i >= 32)
1384 			chan = &req->ext_channels[i - 32];
1385 		else
1386 			chan = &req->channels[i];
1387 
1388 		chan->band = scan_list[i]->band == NL80211_BAND_2GHZ ? 1 : 2;
1389 		chan->channel_num = scan_list[i]->hw_value;
1390 	}
1391 	req->channel_type = sreq->n_channels ? 4 : 0;
1392 
1393 	if (sreq->ie_len > 0) {
1394 		memcpy(req->ies, sreq->ie, sreq->ie_len);
1395 		req->ies_len = cpu_to_le16(sreq->ie_len);
1396 	}
1397 
1398 	if (is_mt7921(phy->dev))
1399 		req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
1400 
1401 	memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1402 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1403 		get_random_mask_addr(req->random_mac, sreq->mac_addr,
1404 				     sreq->mac_addr_mask);
1405 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
1406 	}
1407 
1408 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CMD_START_HW_SCAN, false);
1409 	if (err < 0)
1410 		clear_bit(MT76_HW_SCANNING, &phy->state);
1411 
1412 	return err;
1413 }
1414 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1415 
1416 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1417 				   struct ieee80211_vif *vif)
1418 {
1419 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1420 	struct {
1421 		u8 seq_num;
1422 		u8 is_ext_channel;
1423 		u8 rsv[2];
1424 	} __packed req = {
1425 		.seq_num = mvif->scan_seq_num,
1426 	};
1427 
1428 	if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1429 		struct cfg80211_scan_info info = {
1430 			.aborted = true,
1431 		};
1432 
1433 		ieee80211_scan_completed(phy->hw, &info);
1434 	}
1435 
1436 	return mt76_mcu_send_msg(phy->dev, MCU_CMD_CANCEL_HW_SCAN, &req,
1437 				 sizeof(req), false);
1438 }
1439 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1440 
1441 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1442 				   struct ieee80211_vif *vif,
1443 				   struct cfg80211_sched_scan_request *sreq)
1444 {
1445 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1446 	struct ieee80211_channel **scan_list = sreq->channels;
1447 	struct mt76_connac_mcu_scan_channel *chan;
1448 	struct mt76_connac_sched_scan_req *req;
1449 	struct mt76_dev *mdev = phy->dev;
1450 	bool ext_phy = phy == mdev->phy2;
1451 	struct cfg80211_match_set *match;
1452 	struct cfg80211_ssid *ssid;
1453 	struct sk_buff *skb;
1454 	int i;
1455 
1456 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1457 	if (!skb)
1458 		return -ENOMEM;
1459 
1460 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1461 
1462 	req = (struct mt76_connac_sched_scan_req *)skb_put(skb, sizeof(*req));
1463 	req->version = 1;
1464 	req->seq_num = mvif->scan_seq_num | ext_phy << 7;
1465 
1466 	if (is_mt7663(phy->dev) &&
1467 	    (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) {
1468 		get_random_mask_addr(req->mt7663.random_mac, sreq->mac_addr,
1469 				     sreq->mac_addr_mask);
1470 		req->scan_func = 1;
1471 	} else if (is_mt7921(phy->dev)) {
1472 		req->mt7921.bss_idx = mvif->idx;
1473 	}
1474 
1475 	req->ssids_num = sreq->n_ssids;
1476 	for (i = 0; i < req->ssids_num; i++) {
1477 		ssid = &sreq->ssids[i];
1478 		memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1479 		req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1480 	}
1481 
1482 	req->match_num = sreq->n_match_sets;
1483 	for (i = 0; i < req->match_num; i++) {
1484 		match = &sreq->match_sets[i];
1485 		memcpy(req->match[i].ssid, match->ssid.ssid,
1486 		       match->ssid.ssid_len);
1487 		req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1488 		req->match[i].ssid_len = match->ssid.ssid_len;
1489 	}
1490 
1491 	req->channel_type = sreq->n_channels ? 4 : 0;
1492 	req->channels_num = min_t(u8, sreq->n_channels, 64);
1493 	for (i = 0; i < req->channels_num; i++) {
1494 		chan = &req->channels[i];
1495 		chan->band = scan_list[i]->band == NL80211_BAND_2GHZ ? 1 : 2;
1496 		chan->channel_num = scan_list[i]->hw_value;
1497 	}
1498 
1499 	req->intervals_num = sreq->n_scan_plans;
1500 	for (i = 0; i < req->intervals_num; i++)
1501 		req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1502 
1503 	if (sreq->ie_len > 0) {
1504 		req->ie_len = cpu_to_le16(sreq->ie_len);
1505 		memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1506 	}
1507 
1508 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_CMD_SCHED_SCAN_REQ, false);
1509 }
1510 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1511 
1512 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1513 				      struct ieee80211_vif *vif,
1514 				      bool enable)
1515 {
1516 	struct {
1517 		u8 active; /* 0: enabled 1: disabled */
1518 		u8 rsv[3];
1519 	} __packed req = {
1520 		.active = !enable,
1521 	};
1522 
1523 	if (enable)
1524 		set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1525 	else
1526 		clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1527 
1528 	return mt76_mcu_send_msg(phy->dev, MCU_CMD_SCHED_SCAN_ENABLE, &req,
1529 				 sizeof(req), false);
1530 }
1531 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1532 
1533 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1534 {
1535 	struct mt76_connac_config req = {
1536 		.resp_type = 0,
1537 	};
1538 
1539 	memcpy(req.data, "assert", 7);
1540 
1541 	return mt76_mcu_send_msg(dev, MCU_CMD_CHIP_CONFIG, &req, sizeof(req),
1542 				 false);
1543 }
1544 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1545 
1546 int mt76_connac_mcu_set_deep_sleep(struct mt76_dev *dev, bool enable)
1547 {
1548 	struct mt76_connac_config req = {
1549 		.resp_type = 0,
1550 	};
1551 
1552 	snprintf(req.data, sizeof(req.data), "KeepFullPwr %d", !enable);
1553 
1554 	return mt76_mcu_send_msg(dev, MCU_CMD_CHIP_CONFIG, &req, sizeof(req),
1555 				 false);
1556 }
1557 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_deep_sleep);
1558 
1559 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
1560 				    struct mt76_connac_coredump *coredump)
1561 {
1562 	spin_lock_bh(&dev->lock);
1563 	__skb_queue_tail(&coredump->msg_list, skb);
1564 	spin_unlock_bh(&dev->lock);
1565 
1566 	coredump->last_activity = jiffies;
1567 
1568 	queue_delayed_work(dev->wq, &coredump->work,
1569 			   MT76_CONNAC_COREDUMP_TIMEOUT);
1570 }
1571 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
1572 
1573 static void
1574 mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
1575 			  struct mt76_power_limits *limits,
1576 			  enum nl80211_band band)
1577 {
1578 	int max_power = is_mt7921(dev) ? 127 : 63;
1579 	int i, offset = sizeof(limits->cck);
1580 
1581 	memset(sku, max_power, MT_SKU_POWER_LIMIT);
1582 
1583 	if (band == NL80211_BAND_2GHZ) {
1584 		/* cck */
1585 		memcpy(sku, limits->cck, sizeof(limits->cck));
1586 	}
1587 
1588 	/* ofdm */
1589 	memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
1590 	offset += sizeof(limits->ofdm);
1591 
1592 	/* ht */
1593 	for (i = 0; i < 2; i++) {
1594 		memcpy(&sku[offset], limits->mcs[i], 8);
1595 		offset += 8;
1596 	}
1597 	sku[offset++] = limits->mcs[0][0];
1598 
1599 	/* vht */
1600 	for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
1601 		memcpy(&sku[offset], limits->mcs[i],
1602 		       ARRAY_SIZE(limits->mcs[i]));
1603 		offset += 12;
1604 	}
1605 
1606 	if (!is_mt7921(dev))
1607 		return;
1608 
1609 	/* he */
1610 	for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
1611 		memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
1612 		offset += ARRAY_SIZE(limits->ru[i]);
1613 	}
1614 }
1615 
1616 static int
1617 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
1618 				  enum nl80211_band band)
1619 {
1620 	struct mt76_dev *dev = phy->dev;
1621 	int sku_len, batch_len = is_mt7921(dev) ? 8 : 16;
1622 	static const u8 chan_list_2ghz[] = {
1623 		1, 2,  3,  4,  5,  6,  7,
1624 		8, 9, 10, 11, 12, 13, 14
1625 	};
1626 	static const u8 chan_list_5ghz[] = {
1627 		 36,  38,  40,  42,  44,  46,  48,
1628 		 50,  52,  54,  56,  58,  60,  62,
1629 		 64, 100, 102, 104, 106, 108, 110,
1630 		112, 114, 116, 118, 120, 122, 124,
1631 		126, 128, 132, 134, 136, 138, 140,
1632 		142, 144, 149, 151, 153, 155, 157,
1633 		159, 161, 165
1634 	};
1635 	struct mt76_connac_sku_tlv sku_tlbv;
1636 	int i, n_chan, batch_size, idx = 0;
1637 	struct mt76_power_limits limits;
1638 	const u8 *ch_list;
1639 
1640 	sku_len = is_mt7921(dev) ? sizeof(sku_tlbv) : sizeof(sku_tlbv) - 92;
1641 
1642 	if (band == NL80211_BAND_2GHZ) {
1643 		n_chan = ARRAY_SIZE(chan_list_2ghz);
1644 		ch_list = chan_list_2ghz;
1645 	} else {
1646 		n_chan = ARRAY_SIZE(chan_list_5ghz);
1647 		ch_list = chan_list_5ghz;
1648 	}
1649 	batch_size = DIV_ROUND_UP(n_chan, batch_len);
1650 
1651 	for (i = 0; i < batch_size; i++) {
1652 		bool last_msg = i == batch_size - 1;
1653 		int num_ch = last_msg ? n_chan % batch_len : batch_len;
1654 		struct mt76_connac_tx_power_limit_tlv tx_power_tlv = {
1655 			.band = band == NL80211_BAND_2GHZ ? 1 : 2,
1656 			.n_chan = num_ch,
1657 			.last_msg = last_msg,
1658 		};
1659 		struct sk_buff *skb;
1660 		int j, err, msg_len;
1661 
1662 		msg_len = sizeof(tx_power_tlv) + num_ch * sizeof(sku_tlbv);
1663 		skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
1664 		if (!skb)
1665 			return -ENOMEM;
1666 
1667 		BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv.alpha2));
1668 		memcpy(tx_power_tlv.alpha2, dev->alpha2, sizeof(dev->alpha2));
1669 
1670 		skb_put_data(skb, &tx_power_tlv, sizeof(tx_power_tlv));
1671 		for (j = 0; j < num_ch; j++, idx++) {
1672 			struct ieee80211_channel chan = {
1673 				.hw_value = ch_list[idx],
1674 				.band = band,
1675 			};
1676 
1677 			mt76_get_rate_power_limits(phy, &chan, &limits, 127);
1678 
1679 			sku_tlbv.channel = ch_list[idx];
1680 			mt76_connac_mcu_build_sku(dev, sku_tlbv.pwr_limit,
1681 						  &limits, band);
1682 			skb_put_data(skb, &sku_tlbv, sku_len);
1683 		}
1684 
1685 		err = mt76_mcu_skb_send_msg(dev, skb,
1686 					    MCU_CMD_SET_RATE_TX_POWER, false);
1687 		if (err < 0)
1688 			return err;
1689 	}
1690 
1691 	return 0;
1692 }
1693 
1694 int mt76_connac_mcu_set_rate_txpower(struct mt76_phy *phy)
1695 {
1696 	int err;
1697 
1698 	err = mt76_connac_mcu_rate_txpower_band(phy, NL80211_BAND_2GHZ);
1699 	if (err < 0)
1700 		return err;
1701 
1702 	return mt76_connac_mcu_rate_txpower_band(phy, NL80211_BAND_5GHZ);
1703 }
1704 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_rate_txpower);
1705 
1706 int mt76_connac_mcu_update_arp_filter(struct mt76_dev *dev,
1707 				      struct mt76_vif *vif,
1708 				      struct ieee80211_bss_conf *info)
1709 {
1710 	struct sk_buff *skb;
1711 	int i, len = min_t(int, info->arp_addr_cnt,
1712 			   IEEE80211_BSS_ARP_ADDR_LIST_LEN);
1713 	struct {
1714 		struct {
1715 			u8 bss_idx;
1716 			u8 pad[3];
1717 		} __packed hdr;
1718 		struct mt76_connac_arpns_tlv arp;
1719 	} req_hdr = {
1720 		.hdr = {
1721 			.bss_idx = vif->idx,
1722 		},
1723 		.arp = {
1724 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
1725 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
1726 			.ips_num = len,
1727 			.mode = 2,  /* update */
1728 			.option = 1,
1729 		},
1730 	};
1731 
1732 	skb = mt76_mcu_msg_alloc(dev, NULL,
1733 				 sizeof(req_hdr) + len * sizeof(__be32));
1734 	if (!skb)
1735 		return -ENOMEM;
1736 
1737 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
1738 	for (i = 0; i < len; i++) {
1739 		u8 *addr = (u8 *)skb_put(skb, sizeof(__be32));
1740 
1741 		memcpy(addr, &info->arp_addr_list[i], sizeof(__be32));
1742 	}
1743 
1744 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_OFFLOAD, true);
1745 }
1746 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_arp_filter);
1747 
1748 #ifdef CONFIG_PM
1749 
1750 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
1751 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
1752 		 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
1753 	.n_patterns = 1,
1754 	.pattern_min_len = 1,
1755 	.pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
1756 	.max_nd_match_sets = 10,
1757 };
1758 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
1759 
1760 static void
1761 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
1762 			 struct ieee80211_vif *vif,
1763 			 struct ieee80211_sta *sta,
1764 			 struct ieee80211_key_conf *key,
1765 			 void *data)
1766 {
1767 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
1768 	u32 cipher;
1769 
1770 	if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
1771 	    key->cipher != WLAN_CIPHER_SUITE_CCMP &&
1772 	    key->cipher != WLAN_CIPHER_SUITE_TKIP)
1773 		return;
1774 
1775 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1776 		gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
1777 		cipher = BIT(3);
1778 	} else {
1779 		gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
1780 		cipher = BIT(4);
1781 	}
1782 
1783 	/* we are assuming here to have a single pairwise key */
1784 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
1785 		gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
1786 		gtk_tlv->group_cipher = cpu_to_le32(cipher);
1787 		gtk_tlv->keyid = key->keyidx;
1788 	}
1789 }
1790 
1791 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
1792 				     struct ieee80211_vif *vif,
1793 				     struct cfg80211_gtk_rekey_data *key)
1794 {
1795 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1796 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
1797 	struct mt76_phy *phy = hw->priv;
1798 	struct sk_buff *skb;
1799 	struct {
1800 		u8 bss_idx;
1801 		u8 pad[3];
1802 	} __packed hdr = {
1803 		.bss_idx = mvif->idx,
1804 	};
1805 
1806 	skb = mt76_mcu_msg_alloc(phy->dev, NULL,
1807 				 sizeof(hdr) + sizeof(*gtk_tlv));
1808 	if (!skb)
1809 		return -ENOMEM;
1810 
1811 	skb_put_data(skb, &hdr, sizeof(hdr));
1812 	gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put(skb,
1813 							 sizeof(*gtk_tlv));
1814 	gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
1815 	gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
1816 	gtk_tlv->rekey_mode = 2;
1817 	gtk_tlv->option = 1;
1818 
1819 	rcu_read_lock();
1820 	ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
1821 	rcu_read_unlock();
1822 
1823 	memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
1824 	memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
1825 	memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
1826 
1827 	return mt76_mcu_skb_send_msg(phy->dev, skb, MCU_UNI_CMD_OFFLOAD, true);
1828 }
1829 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
1830 
1831 static int
1832 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
1833 			       bool suspend)
1834 {
1835 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1836 	struct {
1837 		struct {
1838 			u8 bss_idx;
1839 			u8 pad[3];
1840 		} __packed hdr;
1841 		struct mt76_connac_arpns_tlv arpns;
1842 	} req = {
1843 		.hdr = {
1844 			.bss_idx = mvif->idx,
1845 		},
1846 		.arpns = {
1847 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
1848 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
1849 			.mode = suspend,
1850 		},
1851 	};
1852 
1853 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
1854 				 true);
1855 }
1856 
1857 static int
1858 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
1859 			      bool suspend)
1860 {
1861 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1862 	struct {
1863 		struct {
1864 			u8 bss_idx;
1865 			u8 pad[3];
1866 		} __packed hdr;
1867 		struct mt76_connac_gtk_rekey_tlv gtk_tlv;
1868 	} __packed req = {
1869 		.hdr = {
1870 			.bss_idx = mvif->idx,
1871 		},
1872 		.gtk_tlv = {
1873 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
1874 			.len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
1875 			.rekey_mode = !suspend,
1876 		},
1877 	};
1878 
1879 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
1880 				 true);
1881 }
1882 
1883 static int
1884 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
1885 				 struct ieee80211_vif *vif,
1886 				 bool enable, u8 mdtim,
1887 				 bool wow_suspend)
1888 {
1889 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1890 	struct {
1891 		struct {
1892 			u8 bss_idx;
1893 			u8 pad[3];
1894 		} __packed hdr;
1895 		struct mt76_connac_suspend_tlv suspend_tlv;
1896 	} req = {
1897 		.hdr = {
1898 			.bss_idx = mvif->idx,
1899 		},
1900 		.suspend_tlv = {
1901 			.tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
1902 			.len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
1903 			.enable = enable,
1904 			.mdtim = mdtim,
1905 			.wow_suspend = wow_suspend,
1906 		},
1907 	};
1908 
1909 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_SUSPEND, &req, sizeof(req),
1910 				 true);
1911 }
1912 
1913 static int
1914 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
1915 				struct ieee80211_vif *vif,
1916 				u8 index, bool enable,
1917 				struct cfg80211_pkt_pattern *pattern)
1918 {
1919 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1920 	struct mt76_connac_wow_pattern_tlv *ptlv;
1921 	struct sk_buff *skb;
1922 	struct req_hdr {
1923 		u8 bss_idx;
1924 		u8 pad[3];
1925 	} __packed hdr = {
1926 		.bss_idx = mvif->idx,
1927 	};
1928 
1929 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
1930 	if (!skb)
1931 		return -ENOMEM;
1932 
1933 	skb_put_data(skb, &hdr, sizeof(hdr));
1934 	ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put(skb, sizeof(*ptlv));
1935 	ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
1936 	ptlv->len = cpu_to_le16(sizeof(*ptlv));
1937 	ptlv->data_len = pattern->pattern_len;
1938 	ptlv->enable = enable;
1939 	ptlv->index = index;
1940 
1941 	memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
1942 	memcpy(ptlv->mask, pattern->mask, pattern->pattern_len / 8);
1943 
1944 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_SUSPEND, true);
1945 }
1946 
1947 static int
1948 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
1949 			     bool suspend, struct cfg80211_wowlan *wowlan)
1950 {
1951 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1952 	struct mt76_dev *dev = phy->dev;
1953 	struct {
1954 		struct {
1955 			u8 bss_idx;
1956 			u8 pad[3];
1957 		} __packed hdr;
1958 		struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
1959 		struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
1960 	} req = {
1961 		.hdr = {
1962 			.bss_idx = mvif->idx,
1963 		},
1964 		.wow_ctrl_tlv = {
1965 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
1966 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
1967 			.cmd = suspend ? 1 : 2,
1968 		},
1969 		.gpio_tlv = {
1970 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
1971 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
1972 			.gpio_pin = 0xff, /* follow fw about GPIO pin */
1973 		},
1974 	};
1975 
1976 	if (wowlan->magic_pkt)
1977 		req.wow_ctrl_tlv.trigger |= BIT(0);
1978 	if (wowlan->disconnect)
1979 		req.wow_ctrl_tlv.trigger |= BIT(2);
1980 	if (wowlan->nd_config) {
1981 		mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
1982 		req.wow_ctrl_tlv.trigger |= BIT(5);
1983 		mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
1984 	}
1985 
1986 	if (mt76_is_mmio(dev))
1987 		req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
1988 	else if (mt76_is_usb(dev))
1989 		req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
1990 	else if (mt76_is_sdio(dev))
1991 		req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
1992 
1993 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_SUSPEND, &req, sizeof(req),
1994 				 true);
1995 }
1996 
1997 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend)
1998 {
1999 	struct {
2000 		struct {
2001 			u8 hif_type; /* 0x0: HIF_SDIO
2002 				      * 0x1: HIF_USB
2003 				      * 0x2: HIF_PCIE
2004 				      */
2005 			u8 pad[3];
2006 		} __packed hdr;
2007 		struct hif_suspend_tlv {
2008 			__le16 tag;
2009 			__le16 len;
2010 			u8 suspend;
2011 		} __packed hif_suspend;
2012 	} req = {
2013 		.hif_suspend = {
2014 			.tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
2015 			.len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
2016 			.suspend = suspend,
2017 		},
2018 	};
2019 
2020 	if (mt76_is_mmio(dev))
2021 		req.hdr.hif_type = 2;
2022 	else if (mt76_is_usb(dev))
2023 		req.hdr.hif_type = 1;
2024 	else if (mt76_is_sdio(dev))
2025 		req.hdr.hif_type = 0;
2026 
2027 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_HIF_CTRL, &req, sizeof(req),
2028 				 true);
2029 }
2030 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
2031 
2032 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
2033 				      struct ieee80211_vif *vif)
2034 {
2035 	struct mt76_phy *phy = priv;
2036 	bool suspend = test_bit(MT76_STATE_SUSPEND, &phy->state);
2037 	struct ieee80211_hw *hw = phy->hw;
2038 	struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
2039 	int i;
2040 
2041 	mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
2042 	mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
2043 
2044 	mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
2045 
2046 	for (i = 0; i < wowlan->n_patterns; i++)
2047 		mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
2048 						&wowlan->patterns[i]);
2049 	mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
2050 }
2051 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
2052 
2053 #endif /* CONFIG_PM */
2054 
2055 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
2056 MODULE_LICENSE("Dual BSD/GPL");
2057