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_BA_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_generic_tlv(struct mt76_dev *dev,
396 				      struct sk_buff *skb,
397 				      struct ieee80211_vif *vif,
398 				      struct ieee80211_sta *sta,
399 				      void *sta_wtbl, void *wtbl_tlv)
400 {
401 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
402 	struct wtbl_generic *generic;
403 	struct wtbl_rx *rx;
404 	struct wtbl_spe *spe;
405 	struct tlv *tlv;
406 
407 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_GENERIC,
408 					     sizeof(*generic),
409 					     wtbl_tlv, sta_wtbl);
410 
411 	generic = (struct wtbl_generic *)tlv;
412 
413 	if (sta) {
414 		if (vif->type == NL80211_IFTYPE_STATION)
415 			generic->partial_aid = cpu_to_le16(vif->bss_conf.aid);
416 		else
417 			generic->partial_aid = cpu_to_le16(sta->aid);
418 		memcpy(generic->peer_addr, sta->addr, ETH_ALEN);
419 		generic->muar_idx = mvif->omac_idx;
420 		generic->qos = sta->wme;
421 	} else {
422 		if (is_mt7921(dev) &&
423 		    vif->type == NL80211_IFTYPE_STATION)
424 			memcpy(generic->peer_addr, vif->bss_conf.bssid,
425 			       ETH_ALEN);
426 		else
427 			eth_broadcast_addr(generic->peer_addr);
428 
429 		generic->muar_idx = 0xe;
430 	}
431 
432 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RX, sizeof(*rx),
433 					     wtbl_tlv, sta_wtbl);
434 
435 	rx = (struct wtbl_rx *)tlv;
436 	rx->rca1 = sta ? vif->type != NL80211_IFTYPE_AP : 1;
437 	rx->rca2 = 1;
438 	rx->rv = 1;
439 
440 	if (is_mt7921(dev))
441 		return;
442 
443 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SPE, sizeof(*spe),
444 					     wtbl_tlv, sta_wtbl);
445 	spe = (struct wtbl_spe *)tlv;
446 	spe->spe_idx = 24;
447 }
448 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_generic_tlv);
449 
450 static void
451 mt76_connac_mcu_sta_amsdu_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
452 			      struct ieee80211_vif *vif)
453 {
454 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
455 	struct sta_rec_amsdu *amsdu;
456 	struct tlv *tlv;
457 
458 	if (vif->type != NL80211_IFTYPE_AP &&
459 	    vif->type != NL80211_IFTYPE_STATION)
460 		return;
461 
462 	if (!sta->max_amsdu_len)
463 		return;
464 
465 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
466 	amsdu = (struct sta_rec_amsdu *)tlv;
467 	amsdu->max_amsdu_num = 8;
468 	amsdu->amsdu_en = true;
469 	amsdu->max_mpdu_size = sta->max_amsdu_len >=
470 			       IEEE80211_MAX_MPDU_LEN_VHT_7991;
471 
472 	wcid->amsdu = true;
473 }
474 
475 #define HE_PHY(p, c)	u8_get_bits(c, IEEE80211_HE_PHY_##p)
476 #define HE_MAC(m, c)	u8_get_bits(c, IEEE80211_HE_MAC_##m)
477 static void
478 mt76_connac_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_sta *sta)
479 {
480 	struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
481 	struct ieee80211_he_cap_elem *elem = &he_cap->he_cap_elem;
482 	struct sta_rec_he *he;
483 	struct tlv *tlv;
484 	u32 cap = 0;
485 
486 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE, sizeof(*he));
487 
488 	he = (struct sta_rec_he *)tlv;
489 
490 	if (elem->mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
491 		cap |= STA_REC_HE_CAP_HTC;
492 
493 	if (elem->mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
494 		cap |= STA_REC_HE_CAP_BSR;
495 
496 	if (elem->mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
497 		cap |= STA_REC_HE_CAP_OM;
498 
499 	if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU)
500 		cap |= STA_REC_HE_CAP_AMSDU_IN_AMPDU;
501 
502 	if (elem->mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
503 		cap |= STA_REC_HE_CAP_BQR;
504 
505 	if (elem->phy_cap_info[0] &
506 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G |
507 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G))
508 		cap |= STA_REC_HE_CAP_BW20_RU242_SUPPORT;
509 
510 	if (elem->phy_cap_info[1] &
511 	    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
512 		cap |= STA_REC_HE_CAP_LDPC;
513 
514 	if (elem->phy_cap_info[1] &
515 	    IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US)
516 		cap |= STA_REC_HE_CAP_SU_PPDU_1LTF_8US_GI;
517 
518 	if (elem->phy_cap_info[2] &
519 	    IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US)
520 		cap |= STA_REC_HE_CAP_NDP_4LTF_3DOT2MS_GI;
521 
522 	if (elem->phy_cap_info[2] &
523 	    IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ)
524 		cap |= STA_REC_HE_CAP_LE_EQ_80M_TX_STBC;
525 
526 	if (elem->phy_cap_info[2] &
527 	    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
528 		cap |= STA_REC_HE_CAP_LE_EQ_80M_RX_STBC;
529 
530 	if (elem->phy_cap_info[6] &
531 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE)
532 		cap |= STA_REC_HE_CAP_PARTIAL_BW_EXT_RANGE;
533 
534 	if (elem->phy_cap_info[7] &
535 	    IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI)
536 		cap |= STA_REC_HE_CAP_SU_MU_PPDU_4LTF_8US_GI;
537 
538 	if (elem->phy_cap_info[7] &
539 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ)
540 		cap |= STA_REC_HE_CAP_GT_80M_TX_STBC;
541 
542 	if (elem->phy_cap_info[7] &
543 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ)
544 		cap |= STA_REC_HE_CAP_GT_80M_RX_STBC;
545 
546 	if (elem->phy_cap_info[8] &
547 	    IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI)
548 		cap |= STA_REC_HE_CAP_ER_SU_PPDU_4LTF_8US_GI;
549 
550 	if (elem->phy_cap_info[8] &
551 	    IEEE80211_HE_PHY_CAP8_HE_ER_SU_1XLTF_AND_08_US_GI)
552 		cap |= STA_REC_HE_CAP_ER_SU_PPDU_1LTF_8US_GI;
553 
554 	if (elem->phy_cap_info[9] &
555 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK)
556 		cap |= STA_REC_HE_CAP_TRIG_CQI_FK;
557 
558 	if (elem->phy_cap_info[9] &
559 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU)
560 		cap |= STA_REC_HE_CAP_TX_1024QAM_UNDER_RU242;
561 
562 	if (elem->phy_cap_info[9] &
563 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU)
564 		cap |= STA_REC_HE_CAP_RX_1024QAM_UNDER_RU242;
565 
566 	he->he_cap = cpu_to_le32(cap);
567 
568 	switch (sta->bandwidth) {
569 	case IEEE80211_STA_RX_BW_160:
570 		if (elem->phy_cap_info[0] &
571 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
572 			he->max_nss_mcs[CMD_HE_MCS_BW8080] =
573 				he_cap->he_mcs_nss_supp.rx_mcs_80p80;
574 
575 		he->max_nss_mcs[CMD_HE_MCS_BW160] =
576 				he_cap->he_mcs_nss_supp.rx_mcs_160;
577 		fallthrough;
578 	default:
579 		he->max_nss_mcs[CMD_HE_MCS_BW80] =
580 				he_cap->he_mcs_nss_supp.rx_mcs_80;
581 		break;
582 	}
583 
584 	he->t_frame_dur =
585 		HE_MAC(CAP1_TF_MAC_PAD_DUR_MASK, elem->mac_cap_info[1]);
586 	he->max_ampdu_exp =
587 		HE_MAC(CAP3_MAX_AMPDU_LEN_EXP_MASK, elem->mac_cap_info[3]);
588 
589 	he->bw_set =
590 		HE_PHY(CAP0_CHANNEL_WIDTH_SET_MASK, elem->phy_cap_info[0]);
591 	he->device_class =
592 		HE_PHY(CAP1_DEVICE_CLASS_A, elem->phy_cap_info[1]);
593 	he->punc_pream_rx =
594 		HE_PHY(CAP1_PREAMBLE_PUNC_RX_MASK, elem->phy_cap_info[1]);
595 
596 	he->dcm_tx_mode =
597 		HE_PHY(CAP3_DCM_MAX_CONST_TX_MASK, elem->phy_cap_info[3]);
598 	he->dcm_tx_max_nss =
599 		HE_PHY(CAP3_DCM_MAX_TX_NSS_2, elem->phy_cap_info[3]);
600 	he->dcm_rx_mode =
601 		HE_PHY(CAP3_DCM_MAX_CONST_RX_MASK, elem->phy_cap_info[3]);
602 	he->dcm_rx_max_nss =
603 		HE_PHY(CAP3_DCM_MAX_RX_NSS_2, elem->phy_cap_info[3]);
604 	he->dcm_rx_max_nss =
605 		HE_PHY(CAP8_DCM_MAX_RU_MASK, elem->phy_cap_info[8]);
606 
607 	he->pkt_ext = 2;
608 }
609 
610 static u8
611 mt76_connac_get_phy_mode_v2(struct mt76_phy *mphy, struct ieee80211_vif *vif,
612 			    enum nl80211_band band, struct ieee80211_sta *sta)
613 {
614 	struct ieee80211_sta_ht_cap *ht_cap;
615 	struct ieee80211_sta_vht_cap *vht_cap;
616 	const struct ieee80211_sta_he_cap *he_cap;
617 	u8 mode = 0;
618 
619 	if (sta) {
620 		ht_cap = &sta->ht_cap;
621 		vht_cap = &sta->vht_cap;
622 		he_cap = &sta->he_cap;
623 	} else {
624 		struct ieee80211_supported_band *sband;
625 
626 		sband = mphy->hw->wiphy->bands[band];
627 		ht_cap = &sband->ht_cap;
628 		vht_cap = &sband->vht_cap;
629 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
630 	}
631 
632 	if (band == NL80211_BAND_2GHZ) {
633 		mode |= PHY_TYPE_BIT_HR_DSSS | PHY_TYPE_BIT_ERP;
634 
635 		if (ht_cap->ht_supported)
636 			mode |= PHY_TYPE_BIT_HT;
637 
638 		if (he_cap->has_he)
639 			mode |= PHY_TYPE_BIT_HE;
640 	} else if (band == NL80211_BAND_5GHZ) {
641 		mode |= PHY_TYPE_BIT_OFDM;
642 
643 		if (ht_cap->ht_supported)
644 			mode |= PHY_TYPE_BIT_HT;
645 
646 		if (vht_cap->vht_supported)
647 			mode |= PHY_TYPE_BIT_VHT;
648 
649 		if (he_cap->has_he)
650 			mode |= PHY_TYPE_BIT_HE;
651 	}
652 
653 	return mode;
654 }
655 
656 void mt76_connac_mcu_sta_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
657 			     struct ieee80211_sta *sta,
658 			     struct ieee80211_vif *vif)
659 {
660 	struct cfg80211_chan_def *chandef = &mphy->chandef;
661 	enum nl80211_band band = chandef->chan->band;
662 	struct mt76_dev *dev = mphy->dev;
663 	struct sta_rec_ra_info *ra_info;
664 	struct sta_rec_state *state;
665 	struct sta_rec_phy *phy;
666 	struct tlv *tlv;
667 
668 	/* starec ht */
669 	if (sta->ht_cap.ht_supported) {
670 		struct sta_rec_ht *ht;
671 
672 		tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
673 		ht = (struct sta_rec_ht *)tlv;
674 		ht->ht_cap = cpu_to_le16(sta->ht_cap.cap);
675 	}
676 
677 	/* starec vht */
678 	if (sta->vht_cap.vht_supported) {
679 		struct sta_rec_vht *vht;
680 		int len;
681 
682 		len = is_mt7921(dev) ? sizeof(*vht) : sizeof(*vht) - 4;
683 		tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, len);
684 		vht = (struct sta_rec_vht *)tlv;
685 		vht->vht_cap = cpu_to_le32(sta->vht_cap.cap);
686 		vht->vht_rx_mcs_map = sta->vht_cap.vht_mcs.rx_mcs_map;
687 		vht->vht_tx_mcs_map = sta->vht_cap.vht_mcs.tx_mcs_map;
688 	}
689 
690 	/* starec uapsd */
691 	mt76_connac_mcu_sta_uapsd(skb, vif, sta);
692 
693 	if (!is_mt7921(dev))
694 		return;
695 
696 	if (sta->ht_cap.ht_supported)
697 		mt76_connac_mcu_sta_amsdu_tlv(skb, sta, vif);
698 
699 	/* starec he */
700 	if (sta->he_cap.has_he)
701 		mt76_connac_mcu_sta_he_tlv(skb, sta);
702 
703 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
704 	phy = (struct sta_rec_phy *)tlv;
705 	phy->phy_type = mt76_connac_get_phy_mode_v2(mphy, vif, band, sta);
706 	phy->basic_rate = cpu_to_le16((u16)vif->bss_conf.basic_rates);
707 
708 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
709 	ra_info = (struct sta_rec_ra_info *)tlv;
710 	ra_info->legacy = cpu_to_le16((u16)sta->supp_rates[band]);
711 
712 	if (sta->ht_cap.ht_supported)
713 		memcpy(ra_info->rx_mcs_bitmask, sta->ht_cap.mcs.rx_mask,
714 		       HT_MCS_MASK_NUM);
715 
716 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
717 	state = (struct sta_rec_state *)tlv;
718 	state->state = 2;
719 
720 	if (sta->vht_cap.vht_supported) {
721 		state->vht_opmode = sta->bandwidth;
722 		state->vht_opmode |= (sta->rx_nss - 1) <<
723 			IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
724 	}
725 }
726 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_tlv);
727 
728 static void
729 mt76_connac_mcu_wtbl_smps_tlv(struct sk_buff *skb, struct ieee80211_sta *sta,
730 			      void *sta_wtbl, void *wtbl_tlv)
731 {
732 	struct wtbl_smps *smps;
733 	struct tlv *tlv;
734 
735 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_SMPS, sizeof(*smps),
736 					     wtbl_tlv, sta_wtbl);
737 	smps = (struct wtbl_smps *)tlv;
738 
739 	if (sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
740 		smps->smps = true;
741 }
742 
743 void mt76_connac_mcu_wtbl_ht_tlv(struct mt76_dev *dev, struct sk_buff *skb,
744 				 struct ieee80211_sta *sta, void *sta_wtbl,
745 				 void *wtbl_tlv)
746 {
747 	struct wtbl_ht *ht = NULL;
748 	struct tlv *tlv;
749 	u32 flags = 0;
750 
751 	if (sta->ht_cap.ht_supported) {
752 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_HT, sizeof(*ht),
753 						     wtbl_tlv, sta_wtbl);
754 		ht = (struct wtbl_ht *)tlv;
755 		ht->ldpc = !!(sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING);
756 		ht->af = sta->ht_cap.ampdu_factor;
757 		ht->mm = sta->ht_cap.ampdu_density;
758 		ht->ht = true;
759 	}
760 
761 	if (sta->vht_cap.vht_supported) {
762 		struct wtbl_vht *vht;
763 		u8 af;
764 
765 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_VHT,
766 						     sizeof(*vht), wtbl_tlv,
767 						     sta_wtbl);
768 		vht = (struct wtbl_vht *)tlv;
769 		vht->ldpc = !!(sta->vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
770 		vht->vht = true;
771 
772 		af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
773 			       sta->vht_cap.cap);
774 		if (ht)
775 			ht->af = max(ht->af, af);
776 	}
777 
778 	mt76_connac_mcu_wtbl_smps_tlv(skb, sta, sta_wtbl, wtbl_tlv);
779 
780 	if (!is_mt7921(dev) && sta->ht_cap.ht_supported) {
781 		/* sgi */
782 		u32 msk = MT_WTBL_W5_SHORT_GI_20 | MT_WTBL_W5_SHORT_GI_40 |
783 			  MT_WTBL_W5_SHORT_GI_80 | MT_WTBL_W5_SHORT_GI_160;
784 		struct wtbl_raw *raw;
785 
786 		tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_RAW_DATA,
787 						     sizeof(*raw), wtbl_tlv,
788 						     sta_wtbl);
789 
790 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20)
791 			flags |= MT_WTBL_W5_SHORT_GI_20;
792 		if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
793 			flags |= MT_WTBL_W5_SHORT_GI_40;
794 
795 		if (sta->vht_cap.vht_supported) {
796 			if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80)
797 				flags |= MT_WTBL_W5_SHORT_GI_80;
798 			if (sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160)
799 				flags |= MT_WTBL_W5_SHORT_GI_160;
800 		}
801 		raw = (struct wtbl_raw *)tlv;
802 		raw->val = cpu_to_le32(flags);
803 		raw->msk = cpu_to_le32(~msk);
804 		raw->wtbl_idx = 1;
805 		raw->dw = 5;
806 	}
807 }
808 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ht_tlv);
809 
810 int mt76_connac_mcu_add_sta_cmd(struct mt76_phy *phy,
811 				struct ieee80211_vif *vif,
812 				struct ieee80211_sta *sta,
813 				struct mt76_wcid *wcid,
814 				bool enable, int cmd)
815 {
816 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
817 	struct mt76_dev *dev = phy->dev;
818 	struct wtbl_req_hdr *wtbl_hdr;
819 	struct tlv *sta_wtbl;
820 	struct sk_buff *skb;
821 
822 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
823 	if (IS_ERR(skb))
824 		return PTR_ERR(skb);
825 
826 	mt76_connac_mcu_sta_basic_tlv(skb, vif, sta, enable);
827 	if (enable && sta)
828 		mt76_connac_mcu_sta_tlv(phy, skb, sta, vif);
829 
830 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
831 					   sizeof(struct tlv));
832 
833 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid,
834 						  WTBL_RESET_AND_SET,
835 						  sta_wtbl, &skb);
836 	if (enable) {
837 		mt76_connac_mcu_wtbl_generic_tlv(dev, skb, vif, sta, sta_wtbl,
838 						 wtbl_hdr);
839 		if (sta)
840 			mt76_connac_mcu_wtbl_ht_tlv(dev, skb, sta, sta_wtbl,
841 						    wtbl_hdr);
842 	}
843 
844 	return mt76_mcu_skb_send_msg(dev, skb, cmd, true);
845 }
846 EXPORT_SYMBOL_GPL(mt76_connac_mcu_add_sta_cmd);
847 
848 void mt76_connac_mcu_wtbl_ba_tlv(struct mt76_dev *dev, struct sk_buff *skb,
849 				 struct ieee80211_ampdu_params *params,
850 				 bool enable, bool tx, void *sta_wtbl,
851 				 void *wtbl_tlv)
852 {
853 	struct wtbl_ba *ba;
854 	struct tlv *tlv;
855 
856 	tlv = mt76_connac_mcu_add_nested_tlv(skb, WTBL_BA, sizeof(*ba),
857 					     wtbl_tlv, sta_wtbl);
858 
859 	ba = (struct wtbl_ba *)tlv;
860 	ba->tid = params->tid;
861 
862 	if (tx) {
863 		ba->ba_type = MT_BA_TYPE_ORIGINATOR;
864 		ba->sn = enable ? cpu_to_le16(params->ssn) : 0;
865 		ba->ba_winsize = enable ? cpu_to_le16(params->buf_size) : 0;
866 		ba->ba_en = enable;
867 	} else {
868 		memcpy(ba->peer_addr, params->sta->addr, ETH_ALEN);
869 		ba->ba_type = MT_BA_TYPE_RECIPIENT;
870 		ba->rst_ba_tid = params->tid;
871 		ba->rst_ba_sel = RST_BA_MAC_TID_MATCH;
872 		ba->rst_ba_sb = 1;
873 	}
874 
875 	if (is_mt7921(dev))
876 		return;
877 
878 	if (enable && tx) {
879 		u8 ba_range[] = { 4, 8, 12, 24, 36, 48, 54, 64 };
880 		int i;
881 
882 		for (i = 7; i > 0; i--) {
883 			if (params->buf_size >= ba_range[i])
884 				break;
885 		}
886 		ba->ba_winsize_idx = i;
887 	}
888 }
889 EXPORT_SYMBOL_GPL(mt76_connac_mcu_wtbl_ba_tlv);
890 
891 int mt76_connac_mcu_uni_add_dev(struct mt76_phy *phy,
892 				struct ieee80211_vif *vif,
893 				struct mt76_wcid *wcid,
894 				bool enable)
895 {
896 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
897 	struct mt76_dev *dev = phy->dev;
898 	struct {
899 		struct {
900 			u8 omac_idx;
901 			u8 band_idx;
902 			__le16 pad;
903 		} __packed hdr;
904 		struct req_tlv {
905 			__le16 tag;
906 			__le16 len;
907 			u8 active;
908 			u8 pad;
909 			u8 omac_addr[ETH_ALEN];
910 		} __packed tlv;
911 	} dev_req = {
912 		.hdr = {
913 			.omac_idx = mvif->omac_idx,
914 			.band_idx = mvif->band_idx,
915 		},
916 		.tlv = {
917 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
918 			.len = cpu_to_le16(sizeof(struct req_tlv)),
919 			.active = enable,
920 		},
921 	};
922 	struct {
923 		struct {
924 			u8 bss_idx;
925 			u8 pad[3];
926 		} __packed hdr;
927 		struct mt76_connac_bss_basic_tlv basic;
928 	} basic_req = {
929 		.hdr = {
930 			.bss_idx = mvif->idx,
931 		},
932 		.basic = {
933 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
934 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
935 			.omac_idx = mvif->omac_idx,
936 			.band_idx = mvif->band_idx,
937 			.wmm_idx = mvif->wmm_idx,
938 			.active = enable,
939 			.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx),
940 			.sta_idx = cpu_to_le16(wcid->idx),
941 			.conn_state = 1,
942 		},
943 	};
944 	int err, idx, cmd, len;
945 	void *data;
946 
947 	switch (vif->type) {
948 	case NL80211_IFTYPE_MESH_POINT:
949 	case NL80211_IFTYPE_AP:
950 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_AP);
951 		break;
952 	case NL80211_IFTYPE_STATION:
953 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_INFRA_STA);
954 		break;
955 	case NL80211_IFTYPE_ADHOC:
956 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
957 		break;
958 	default:
959 		WARN_ON(1);
960 		break;
961 	}
962 
963 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
964 	basic_req.basic.hw_bss_idx = idx;
965 
966 	memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
967 
968 	cmd = enable ? MCU_UNI_CMD_DEV_INFO_UPDATE : MCU_UNI_CMD_BSS_INFO_UPDATE;
969 	data = enable ? (void *)&dev_req : (void *)&basic_req;
970 	len = enable ? sizeof(dev_req) : sizeof(basic_req);
971 
972 	err = mt76_mcu_send_msg(dev, cmd, data, len, true);
973 	if (err < 0)
974 		return err;
975 
976 	cmd = enable ? MCU_UNI_CMD_BSS_INFO_UPDATE : MCU_UNI_CMD_DEV_INFO_UPDATE;
977 	data = enable ? (void *)&basic_req : (void *)&dev_req;
978 	len = enable ? sizeof(basic_req) : sizeof(dev_req);
979 
980 	return mt76_mcu_send_msg(dev, cmd, data, len, true);
981 }
982 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_dev);
983 
984 void mt76_connac_mcu_sta_ba_tlv(struct sk_buff *skb,
985 				struct ieee80211_ampdu_params *params,
986 				bool enable, bool tx)
987 {
988 	struct sta_rec_ba *ba;
989 	struct tlv *tlv;
990 
991 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
992 
993 	ba = (struct sta_rec_ba *)tlv;
994 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
995 	ba->winsize = cpu_to_le16(params->buf_size);
996 	ba->ssn = cpu_to_le16(params->ssn);
997 	ba->ba_en = enable << params->tid;
998 	ba->amsdu = params->amsdu;
999 	ba->tid = params->tid;
1000 }
1001 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba_tlv);
1002 
1003 int mt76_connac_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif *mvif,
1004 			   struct ieee80211_ampdu_params *params,
1005 			   bool enable, bool tx)
1006 {
1007 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
1008 	struct wtbl_req_hdr *wtbl_hdr;
1009 	struct tlv *sta_wtbl;
1010 	struct sk_buff *skb;
1011 	int ret;
1012 
1013 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1014 	if (IS_ERR(skb))
1015 		return PTR_ERR(skb);
1016 
1017 	sta_wtbl = mt76_connac_mcu_add_tlv(skb, STA_REC_WTBL,
1018 					   sizeof(struct tlv));
1019 
1020 	wtbl_hdr = mt76_connac_mcu_alloc_wtbl_req(dev, wcid, WTBL_SET,
1021 						  sta_wtbl, &skb);
1022 	if (IS_ERR(wtbl_hdr))
1023 		return PTR_ERR(wtbl_hdr);
1024 
1025 	mt76_connac_mcu_wtbl_ba_tlv(dev, skb, params, enable, tx, sta_wtbl,
1026 				    wtbl_hdr);
1027 
1028 	ret = mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_STA_REC_UPDATE, true);
1029 	if (ret)
1030 		return ret;
1031 
1032 	skb = mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid);
1033 	if (IS_ERR(skb))
1034 		return PTR_ERR(skb);
1035 
1036 	mt76_connac_mcu_sta_ba_tlv(skb, params, enable, tx);
1037 
1038 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_STA_REC_UPDATE,
1039 				     true);
1040 }
1041 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sta_ba);
1042 
1043 static u8
1044 mt76_connac_get_phy_mode(struct mt76_phy *phy, struct ieee80211_vif *vif,
1045 			 enum nl80211_band band,
1046 			 struct ieee80211_sta *sta)
1047 {
1048 	struct mt76_dev *dev = phy->dev;
1049 	const struct ieee80211_sta_he_cap *he_cap;
1050 	struct ieee80211_sta_vht_cap *vht_cap;
1051 	struct ieee80211_sta_ht_cap *ht_cap;
1052 	u8 mode = 0;
1053 
1054 	if (!is_mt7921(dev))
1055 		return 0x38;
1056 
1057 	if (sta) {
1058 		ht_cap = &sta->ht_cap;
1059 		vht_cap = &sta->vht_cap;
1060 		he_cap = &sta->he_cap;
1061 	} else {
1062 		struct ieee80211_supported_band *sband;
1063 
1064 		sband = phy->hw->wiphy->bands[band];
1065 		ht_cap = &sband->ht_cap;
1066 		vht_cap = &sband->vht_cap;
1067 		he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
1068 	}
1069 
1070 	if (band == NL80211_BAND_2GHZ) {
1071 		mode |= PHY_MODE_B | PHY_MODE_G;
1072 
1073 		if (ht_cap->ht_supported)
1074 			mode |= PHY_MODE_GN;
1075 
1076 		if (he_cap->has_he)
1077 			mode |= PHY_MODE_AX_24G;
1078 	} else if (band == NL80211_BAND_5GHZ) {
1079 		mode |= PHY_MODE_A;
1080 
1081 		if (ht_cap->ht_supported)
1082 			mode |= PHY_MODE_AN;
1083 
1084 		if (vht_cap->vht_supported)
1085 			mode |= PHY_MODE_AC;
1086 
1087 		if (he_cap->has_he)
1088 			mode |= PHY_MODE_AX_5G;
1089 	}
1090 
1091 	return mode;
1092 }
1093 
1094 static const struct ieee80211_sta_he_cap *
1095 mt76_connac_get_he_phy_cap(struct mt76_phy *phy, struct ieee80211_vif *vif)
1096 {
1097 	enum nl80211_band band = phy->chandef.chan->band;
1098 	struct ieee80211_supported_band *sband;
1099 
1100 	sband = phy->hw->wiphy->bands[band];
1101 
1102 	return ieee80211_get_he_iftype_cap(sband, vif->type);
1103 }
1104 
1105 #define DEFAULT_HE_PE_DURATION		4
1106 #define DEFAULT_HE_DURATION_RTS_THRES	1023
1107 static void
1108 mt76_connac_mcu_uni_bss_he_tlv(struct mt76_phy *phy, struct ieee80211_vif *vif,
1109 			       struct tlv *tlv)
1110 {
1111 	const struct ieee80211_sta_he_cap *cap;
1112 	struct bss_info_uni_he *he;
1113 
1114 	cap = mt76_connac_get_he_phy_cap(phy, vif);
1115 
1116 	he = (struct bss_info_uni_he *)tlv;
1117 	he->he_pe_duration = vif->bss_conf.htc_trig_based_pkt_ext;
1118 	if (!he->he_pe_duration)
1119 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
1120 
1121 	he->he_rts_thres = cpu_to_le16(vif->bss_conf.frame_time_rts_th);
1122 	if (!he->he_rts_thres)
1123 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
1124 
1125 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
1126 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
1127 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
1128 }
1129 
1130 int mt76_connac_mcu_uni_add_bss(struct mt76_phy *phy,
1131 				struct ieee80211_vif *vif,
1132 				struct mt76_wcid *wcid,
1133 				bool enable)
1134 {
1135 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1136 	struct cfg80211_chan_def *chandef = &phy->chandef;
1137 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
1138 	enum nl80211_band band = chandef->chan->band;
1139 	struct mt76_dev *mdev = phy->dev;
1140 	struct {
1141 		struct {
1142 			u8 bss_idx;
1143 			u8 pad[3];
1144 		} __packed hdr;
1145 		struct mt76_connac_bss_basic_tlv basic;
1146 		struct mt76_connac_bss_qos_tlv qos;
1147 	} basic_req = {
1148 		.hdr = {
1149 			.bss_idx = mvif->idx,
1150 		},
1151 		.basic = {
1152 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
1153 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
1154 			.bcn_interval = cpu_to_le16(vif->bss_conf.beacon_int),
1155 			.dtim_period = vif->bss_conf.dtim_period,
1156 			.omac_idx = mvif->omac_idx,
1157 			.band_idx = mvif->band_idx,
1158 			.wmm_idx = mvif->wmm_idx,
1159 			.active = true, /* keep bss deactivated */
1160 			.phymode = mt76_connac_get_phy_mode(phy, vif, band, NULL),
1161 		},
1162 		.qos = {
1163 			.tag = cpu_to_le16(UNI_BSS_INFO_QBSS),
1164 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_qos_tlv)),
1165 			.qos = vif->bss_conf.qos,
1166 		},
1167 	};
1168 	struct {
1169 		struct {
1170 			u8 bss_idx;
1171 			u8 pad[3];
1172 		} __packed hdr;
1173 		struct rlm_tlv {
1174 			__le16 tag;
1175 			__le16 len;
1176 			u8 control_channel;
1177 			u8 center_chan;
1178 			u8 center_chan2;
1179 			u8 bw;
1180 			u8 tx_streams;
1181 			u8 rx_streams;
1182 			u8 short_st;
1183 			u8 ht_op_info;
1184 			u8 sco;
1185 			u8 pad[3];
1186 		} __packed rlm;
1187 	} __packed rlm_req = {
1188 		.hdr = {
1189 			.bss_idx = mvif->idx,
1190 		},
1191 		.rlm = {
1192 			.tag = cpu_to_le16(UNI_BSS_INFO_RLM),
1193 			.len = cpu_to_le16(sizeof(struct rlm_tlv)),
1194 			.control_channel = chandef->chan->hw_value,
1195 			.center_chan = ieee80211_frequency_to_channel(freq1),
1196 			.center_chan2 = ieee80211_frequency_to_channel(freq2),
1197 			.tx_streams = hweight8(phy->antenna_mask),
1198 			.rx_streams = phy->chainmask,
1199 			.short_st = true,
1200 		},
1201 	};
1202 	int err, conn_type;
1203 	u8 idx;
1204 
1205 	idx = mvif->omac_idx > EXT_BSSID_START ? HW_BSSID_0 : mvif->omac_idx;
1206 	basic_req.basic.hw_bss_idx = idx;
1207 
1208 	switch (vif->type) {
1209 	case NL80211_IFTYPE_MESH_POINT:
1210 	case NL80211_IFTYPE_AP:
1211 		if (vif->p2p)
1212 			conn_type = CONNECTION_P2P_GO;
1213 		else
1214 			conn_type = CONNECTION_INFRA_AP;
1215 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1216 		break;
1217 	case NL80211_IFTYPE_STATION:
1218 		if (vif->p2p)
1219 			conn_type = CONNECTION_P2P_GC;
1220 		else
1221 			conn_type = CONNECTION_INFRA_STA;
1222 		basic_req.basic.conn_type = cpu_to_le32(conn_type);
1223 		break;
1224 	case NL80211_IFTYPE_ADHOC:
1225 		basic_req.basic.conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
1226 		break;
1227 	default:
1228 		WARN_ON(1);
1229 		break;
1230 	}
1231 
1232 	memcpy(basic_req.basic.bssid, vif->bss_conf.bssid, ETH_ALEN);
1233 	basic_req.basic.bmc_tx_wlan_idx = cpu_to_le16(wcid->idx);
1234 	basic_req.basic.sta_idx = cpu_to_le16(wcid->idx);
1235 	basic_req.basic.conn_state = !enable;
1236 
1237 	err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE, &basic_req,
1238 				sizeof(basic_req), true);
1239 	if (err < 0)
1240 		return err;
1241 
1242 	if (vif->bss_conf.he_support) {
1243 		struct {
1244 			struct {
1245 				u8 bss_idx;
1246 				u8 pad[3];
1247 			} __packed hdr;
1248 			struct bss_info_uni_he he;
1249 		} he_req = {
1250 			.hdr = {
1251 				.bss_idx = mvif->idx,
1252 			},
1253 			.he = {
1254 				.tag = cpu_to_le16(UNI_BSS_INFO_HE_BASIC),
1255 				.len = cpu_to_le16(sizeof(struct bss_info_uni_he)),
1256 			},
1257 		};
1258 
1259 		mt76_connac_mcu_uni_bss_he_tlv(phy, vif,
1260 					       (struct tlv *)&he_req.he);
1261 		err = mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE,
1262 					&he_req, sizeof(he_req), true);
1263 		if (err < 0)
1264 			return err;
1265 	}
1266 
1267 	switch (chandef->width) {
1268 	case NL80211_CHAN_WIDTH_40:
1269 		rlm_req.rlm.bw = CMD_CBW_40MHZ;
1270 		break;
1271 	case NL80211_CHAN_WIDTH_80:
1272 		rlm_req.rlm.bw = CMD_CBW_80MHZ;
1273 		break;
1274 	case NL80211_CHAN_WIDTH_80P80:
1275 		rlm_req.rlm.bw = CMD_CBW_8080MHZ;
1276 		break;
1277 	case NL80211_CHAN_WIDTH_160:
1278 		rlm_req.rlm.bw = CMD_CBW_160MHZ;
1279 		break;
1280 	case NL80211_CHAN_WIDTH_5:
1281 		rlm_req.rlm.bw = CMD_CBW_5MHZ;
1282 		break;
1283 	case NL80211_CHAN_WIDTH_10:
1284 		rlm_req.rlm.bw = CMD_CBW_10MHZ;
1285 		break;
1286 	case NL80211_CHAN_WIDTH_20_NOHT:
1287 	case NL80211_CHAN_WIDTH_20:
1288 	default:
1289 		rlm_req.rlm.bw = CMD_CBW_20MHZ;
1290 		break;
1291 	}
1292 
1293 	if (rlm_req.rlm.control_channel < rlm_req.rlm.center_chan)
1294 		rlm_req.rlm.sco = 1; /* SCA */
1295 	else if (rlm_req.rlm.control_channel > rlm_req.rlm.center_chan)
1296 		rlm_req.rlm.sco = 3; /* SCB */
1297 
1298 	return mt76_mcu_send_msg(mdev, MCU_UNI_CMD_BSS_INFO_UPDATE, &rlm_req,
1299 				 sizeof(rlm_req), true);
1300 }
1301 EXPORT_SYMBOL_GPL(mt76_connac_mcu_uni_add_bss);
1302 
1303 #define MT76_CONNAC_SCAN_CHANNEL_TIME		60
1304 int mt76_connac_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
1305 			    struct ieee80211_scan_request *scan_req)
1306 {
1307 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1308 	struct cfg80211_scan_request *sreq = &scan_req->req;
1309 	int n_ssids = 0, err, i, duration = MT76_CONNAC_SCAN_CHANNEL_TIME;
1310 	int ext_channels_num = max_t(int, sreq->n_channels - 32, 0);
1311 	struct ieee80211_channel **scan_list = sreq->channels;
1312 	struct mt76_dev *mdev = phy->dev;
1313 	bool ext_phy = phy == mdev->phy2;
1314 	struct mt76_connac_mcu_scan_channel *chan;
1315 	struct mt76_connac_hw_scan_req *req;
1316 	struct sk_buff *skb;
1317 
1318 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req));
1319 	if (!skb)
1320 		return -ENOMEM;
1321 
1322 	set_bit(MT76_HW_SCANNING, &phy->state);
1323 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1324 
1325 	req = (struct mt76_connac_hw_scan_req *)skb_put(skb, sizeof(*req));
1326 
1327 	req->seq_num = mvif->scan_seq_num | ext_phy << 7;
1328 	req->bss_idx = mvif->idx;
1329 	req->scan_type = sreq->n_ssids ? 1 : 0;
1330 	req->probe_req_num = sreq->n_ssids ? 2 : 0;
1331 	req->version = 1;
1332 
1333 	for (i = 0; i < sreq->n_ssids; i++) {
1334 		if (!sreq->ssids[i].ssid_len)
1335 			continue;
1336 
1337 		req->ssids[i].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
1338 		memcpy(req->ssids[i].ssid, sreq->ssids[i].ssid,
1339 		       sreq->ssids[i].ssid_len);
1340 		n_ssids++;
1341 	}
1342 	req->ssid_type = n_ssids ? BIT(2) : BIT(0);
1343 	req->ssid_type_ext = n_ssids ? BIT(0) : 0;
1344 	req->ssids_num = n_ssids;
1345 
1346 	/* increase channel time for passive scan */
1347 	if (!sreq->n_ssids)
1348 		duration *= 2;
1349 	req->timeout_value = cpu_to_le16(sreq->n_channels * duration);
1350 	req->channel_min_dwell_time = cpu_to_le16(duration);
1351 	req->channel_dwell_time = cpu_to_le16(duration);
1352 
1353 	req->channels_num = min_t(u8, sreq->n_channels, 32);
1354 	req->ext_channels_num = min_t(u8, ext_channels_num, 32);
1355 	for (i = 0; i < req->channels_num + req->ext_channels_num; i++) {
1356 		if (i >= 32)
1357 			chan = &req->ext_channels[i - 32];
1358 		else
1359 			chan = &req->channels[i];
1360 
1361 		chan->band = scan_list[i]->band == NL80211_BAND_2GHZ ? 1 : 2;
1362 		chan->channel_num = scan_list[i]->hw_value;
1363 	}
1364 	req->channel_type = sreq->n_channels ? 4 : 0;
1365 
1366 	if (sreq->ie_len > 0) {
1367 		memcpy(req->ies, sreq->ie, sreq->ie_len);
1368 		req->ies_len = cpu_to_le16(sreq->ie_len);
1369 	}
1370 
1371 	memcpy(req->bssid, sreq->bssid, ETH_ALEN);
1372 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1373 		get_random_mask_addr(req->random_mac, sreq->mac_addr,
1374 				     sreq->mac_addr_mask);
1375 		req->scan_func = 1;
1376 	}
1377 
1378 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_CMD_START_HW_SCAN, false);
1379 	if (err < 0)
1380 		clear_bit(MT76_HW_SCANNING, &phy->state);
1381 
1382 	return err;
1383 }
1384 EXPORT_SYMBOL_GPL(mt76_connac_mcu_hw_scan);
1385 
1386 int mt76_connac_mcu_cancel_hw_scan(struct mt76_phy *phy,
1387 				   struct ieee80211_vif *vif)
1388 {
1389 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1390 	struct {
1391 		u8 seq_num;
1392 		u8 is_ext_channel;
1393 		u8 rsv[2];
1394 	} __packed req = {
1395 		.seq_num = mvif->scan_seq_num,
1396 	};
1397 
1398 	if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
1399 		struct cfg80211_scan_info info = {
1400 			.aborted = true,
1401 		};
1402 
1403 		ieee80211_scan_completed(phy->hw, &info);
1404 	}
1405 
1406 	return mt76_mcu_send_msg(phy->dev, MCU_CMD_CANCEL_HW_SCAN, &req,
1407 				 sizeof(req), false);
1408 }
1409 EXPORT_SYMBOL_GPL(mt76_connac_mcu_cancel_hw_scan);
1410 
1411 int mt76_connac_mcu_sched_scan_req(struct mt76_phy *phy,
1412 				   struct ieee80211_vif *vif,
1413 				   struct cfg80211_sched_scan_request *sreq)
1414 {
1415 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1416 	struct ieee80211_channel **scan_list = sreq->channels;
1417 	struct mt76_connac_mcu_scan_channel *chan;
1418 	struct mt76_connac_sched_scan_req *req;
1419 	struct mt76_dev *mdev = phy->dev;
1420 	bool ext_phy = phy == mdev->phy2;
1421 	struct cfg80211_match_set *match;
1422 	struct cfg80211_ssid *ssid;
1423 	struct sk_buff *skb;
1424 	int i;
1425 
1426 	skb = mt76_mcu_msg_alloc(mdev, NULL, sizeof(*req) + sreq->ie_len);
1427 	if (!skb)
1428 		return -ENOMEM;
1429 
1430 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
1431 
1432 	req = (struct mt76_connac_sched_scan_req *)skb_put(skb, sizeof(*req));
1433 	req->version = 1;
1434 	req->seq_num = mvif->scan_seq_num | ext_phy << 7;
1435 
1436 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
1437 		get_random_mask_addr(req->random_mac, sreq->mac_addr,
1438 				     sreq->mac_addr_mask);
1439 		req->scan_func = 1;
1440 	}
1441 
1442 	req->ssids_num = sreq->n_ssids;
1443 	for (i = 0; i < req->ssids_num; i++) {
1444 		ssid = &sreq->ssids[i];
1445 		memcpy(req->ssids[i].ssid, ssid->ssid, ssid->ssid_len);
1446 		req->ssids[i].ssid_len = cpu_to_le32(ssid->ssid_len);
1447 	}
1448 
1449 	req->match_num = sreq->n_match_sets;
1450 	for (i = 0; i < req->match_num; i++) {
1451 		match = &sreq->match_sets[i];
1452 		memcpy(req->match[i].ssid, match->ssid.ssid,
1453 		       match->ssid.ssid_len);
1454 		req->match[i].rssi_th = cpu_to_le32(match->rssi_thold);
1455 		req->match[i].ssid_len = match->ssid.ssid_len;
1456 	}
1457 
1458 	req->channel_type = sreq->n_channels ? 4 : 0;
1459 	req->channels_num = min_t(u8, sreq->n_channels, 64);
1460 	for (i = 0; i < req->channels_num; i++) {
1461 		chan = &req->channels[i];
1462 		chan->band = scan_list[i]->band == NL80211_BAND_2GHZ ? 1 : 2;
1463 		chan->channel_num = scan_list[i]->hw_value;
1464 	}
1465 
1466 	req->intervals_num = sreq->n_scan_plans;
1467 	for (i = 0; i < req->intervals_num; i++)
1468 		req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
1469 
1470 	if (sreq->ie_len > 0) {
1471 		req->ie_len = cpu_to_le16(sreq->ie_len);
1472 		memcpy(skb_put(skb, sreq->ie_len), sreq->ie, sreq->ie_len);
1473 	}
1474 
1475 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_CMD_SCHED_SCAN_REQ, false);
1476 }
1477 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_req);
1478 
1479 int mt76_connac_mcu_sched_scan_enable(struct mt76_phy *phy,
1480 				      struct ieee80211_vif *vif,
1481 				      bool enable)
1482 {
1483 	struct {
1484 		u8 active; /* 0: enabled 1: disabled */
1485 		u8 rsv[3];
1486 	} __packed req = {
1487 		.active = !enable,
1488 	};
1489 
1490 	if (enable)
1491 		set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1492 	else
1493 		clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
1494 
1495 	return mt76_mcu_send_msg(phy->dev, MCU_CMD_SCHED_SCAN_ENABLE, &req,
1496 				 sizeof(req), false);
1497 }
1498 EXPORT_SYMBOL_GPL(mt76_connac_mcu_sched_scan_enable);
1499 
1500 int mt76_connac_mcu_chip_config(struct mt76_dev *dev)
1501 {
1502 	struct {
1503 		__le16 id;
1504 		u8 type;
1505 		u8 resp_type;
1506 		__le16 data_size;
1507 		__le16 resv;
1508 		u8 data[320];
1509 	} req = {
1510 		.resp_type = 0,
1511 	};
1512 
1513 	memcpy(req.data, "assert", 7);
1514 
1515 	return mt76_mcu_send_msg(dev, MCU_CMD_CHIP_CONFIG, &req, sizeof(req),
1516 				 false);
1517 }
1518 EXPORT_SYMBOL_GPL(mt76_connac_mcu_chip_config);
1519 
1520 void mt76_connac_mcu_coredump_event(struct mt76_dev *dev, struct sk_buff *skb,
1521 				    struct mt76_connac_coredump *coredump)
1522 {
1523 	spin_lock_bh(&dev->lock);
1524 	__skb_queue_tail(&coredump->msg_list, skb);
1525 	spin_unlock_bh(&dev->lock);
1526 
1527 	coredump->last_activity = jiffies;
1528 
1529 	queue_delayed_work(dev->wq, &coredump->work,
1530 			   MT76_CONNAC_COREDUMP_TIMEOUT);
1531 }
1532 EXPORT_SYMBOL_GPL(mt76_connac_mcu_coredump_event);
1533 
1534 #ifdef CONFIG_PM
1535 
1536 const struct wiphy_wowlan_support mt76_connac_wowlan_support = {
1537 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT |
1538 		 WIPHY_WOWLAN_SUPPORTS_GTK_REKEY | WIPHY_WOWLAN_NET_DETECT,
1539 	.n_patterns = 1,
1540 	.pattern_min_len = 1,
1541 	.pattern_max_len = MT76_CONNAC_WOW_PATTEN_MAX_LEN,
1542 	.max_nd_match_sets = 10,
1543 };
1544 EXPORT_SYMBOL_GPL(mt76_connac_wowlan_support);
1545 
1546 static void
1547 mt76_connac_mcu_key_iter(struct ieee80211_hw *hw,
1548 			 struct ieee80211_vif *vif,
1549 			 struct ieee80211_sta *sta,
1550 			 struct ieee80211_key_conf *key,
1551 			 void *data)
1552 {
1553 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv = data;
1554 	u32 cipher;
1555 
1556 	if (key->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
1557 	    key->cipher != WLAN_CIPHER_SUITE_CCMP &&
1558 	    key->cipher != WLAN_CIPHER_SUITE_TKIP)
1559 		return;
1560 
1561 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1562 		gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_1);
1563 		cipher = BIT(3);
1564 	} else {
1565 		gtk_tlv->proto = cpu_to_le32(NL80211_WPA_VERSION_2);
1566 		cipher = BIT(4);
1567 	}
1568 
1569 	/* we are assuming here to have a single pairwise key */
1570 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
1571 		gtk_tlv->pairwise_cipher = cpu_to_le32(cipher);
1572 		gtk_tlv->group_cipher = cpu_to_le32(cipher);
1573 		gtk_tlv->keyid = key->keyidx;
1574 	}
1575 }
1576 
1577 int mt76_connac_mcu_update_gtk_rekey(struct ieee80211_hw *hw,
1578 				     struct ieee80211_vif *vif,
1579 				     struct cfg80211_gtk_rekey_data *key)
1580 {
1581 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1582 	struct mt76_connac_gtk_rekey_tlv *gtk_tlv;
1583 	struct mt76_phy *phy = hw->priv;
1584 	struct sk_buff *skb;
1585 	struct {
1586 		u8 bss_idx;
1587 		u8 pad[3];
1588 	} __packed hdr = {
1589 		.bss_idx = mvif->idx,
1590 	};
1591 
1592 	skb = mt76_mcu_msg_alloc(phy->dev, NULL,
1593 				 sizeof(hdr) + sizeof(*gtk_tlv));
1594 	if (!skb)
1595 		return -ENOMEM;
1596 
1597 	skb_put_data(skb, &hdr, sizeof(hdr));
1598 	gtk_tlv = (struct mt76_connac_gtk_rekey_tlv *)skb_put(skb,
1599 							 sizeof(*gtk_tlv));
1600 	gtk_tlv->tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY);
1601 	gtk_tlv->len = cpu_to_le16(sizeof(*gtk_tlv));
1602 	gtk_tlv->rekey_mode = 2;
1603 	gtk_tlv->option = 1;
1604 
1605 	rcu_read_lock();
1606 	ieee80211_iter_keys_rcu(hw, vif, mt76_connac_mcu_key_iter, gtk_tlv);
1607 	rcu_read_unlock();
1608 
1609 	memcpy(gtk_tlv->kek, key->kek, NL80211_KEK_LEN);
1610 	memcpy(gtk_tlv->kck, key->kck, NL80211_KCK_LEN);
1611 	memcpy(gtk_tlv->replay_ctr, key->replay_ctr, NL80211_REPLAY_CTR_LEN);
1612 
1613 	return mt76_mcu_skb_send_msg(phy->dev, skb, MCU_UNI_CMD_OFFLOAD, true);
1614 }
1615 EXPORT_SYMBOL_GPL(mt76_connac_mcu_update_gtk_rekey);
1616 
1617 static int
1618 mt76_connac_mcu_set_arp_filter(struct mt76_dev *dev, struct ieee80211_vif *vif,
1619 			       bool suspend)
1620 {
1621 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1622 	struct {
1623 		struct {
1624 			u8 bss_idx;
1625 			u8 pad[3];
1626 		} __packed hdr;
1627 		struct mt76_connac_arpns_tlv arpns;
1628 	} req = {
1629 		.hdr = {
1630 			.bss_idx = mvif->idx,
1631 		},
1632 		.arpns = {
1633 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
1634 			.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv)),
1635 			.mode = suspend,
1636 		},
1637 	};
1638 
1639 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
1640 				 true);
1641 }
1642 
1643 static int
1644 mt76_connac_mcu_set_gtk_rekey(struct mt76_dev *dev, struct ieee80211_vif *vif,
1645 			      bool suspend)
1646 {
1647 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1648 	struct {
1649 		struct {
1650 			u8 bss_idx;
1651 			u8 pad[3];
1652 		} __packed hdr;
1653 		struct mt76_connac_gtk_rekey_tlv gtk_tlv;
1654 	} __packed req = {
1655 		.hdr = {
1656 			.bss_idx = mvif->idx,
1657 		},
1658 		.gtk_tlv = {
1659 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_GTK_REKEY),
1660 			.len = cpu_to_le16(sizeof(struct mt76_connac_gtk_rekey_tlv)),
1661 			.rekey_mode = !suspend,
1662 		},
1663 	};
1664 
1665 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_OFFLOAD, &req, sizeof(req),
1666 				 true);
1667 }
1668 
1669 static int
1670 mt76_connac_mcu_set_suspend_mode(struct mt76_dev *dev,
1671 				 struct ieee80211_vif *vif,
1672 				 bool enable, u8 mdtim,
1673 				 bool wow_suspend)
1674 {
1675 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1676 	struct {
1677 		struct {
1678 			u8 bss_idx;
1679 			u8 pad[3];
1680 		} __packed hdr;
1681 		struct mt76_connac_suspend_tlv suspend_tlv;
1682 	} req = {
1683 		.hdr = {
1684 			.bss_idx = mvif->idx,
1685 		},
1686 		.suspend_tlv = {
1687 			.tag = cpu_to_le16(UNI_SUSPEND_MODE_SETTING),
1688 			.len = cpu_to_le16(sizeof(struct mt76_connac_suspend_tlv)),
1689 			.enable = enable,
1690 			.mdtim = mdtim,
1691 			.wow_suspend = wow_suspend,
1692 		},
1693 	};
1694 
1695 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_SUSPEND, &req, sizeof(req),
1696 				 true);
1697 }
1698 
1699 static int
1700 mt76_connac_mcu_set_wow_pattern(struct mt76_dev *dev,
1701 				struct ieee80211_vif *vif,
1702 				u8 index, bool enable,
1703 				struct cfg80211_pkt_pattern *pattern)
1704 {
1705 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1706 	struct mt76_connac_wow_pattern_tlv *ptlv;
1707 	struct sk_buff *skb;
1708 	struct req_hdr {
1709 		u8 bss_idx;
1710 		u8 pad[3];
1711 	} __packed hdr = {
1712 		.bss_idx = mvif->idx,
1713 	};
1714 
1715 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*ptlv));
1716 	if (!skb)
1717 		return -ENOMEM;
1718 
1719 	skb_put_data(skb, &hdr, sizeof(hdr));
1720 	ptlv = (struct mt76_connac_wow_pattern_tlv *)skb_put(skb, sizeof(*ptlv));
1721 	ptlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
1722 	ptlv->len = cpu_to_le16(sizeof(*ptlv));
1723 	ptlv->data_len = pattern->pattern_len;
1724 	ptlv->enable = enable;
1725 	ptlv->index = index;
1726 
1727 	memcpy(ptlv->pattern, pattern->pattern, pattern->pattern_len);
1728 	memcpy(ptlv->mask, pattern->mask, pattern->pattern_len / 8);
1729 
1730 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD_SUSPEND, true);
1731 }
1732 
1733 static int
1734 mt76_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
1735 			     bool suspend, struct cfg80211_wowlan *wowlan)
1736 {
1737 	struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
1738 	struct mt76_dev *dev = phy->dev;
1739 	struct {
1740 		struct {
1741 			u8 bss_idx;
1742 			u8 pad[3];
1743 		} __packed hdr;
1744 		struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
1745 		struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
1746 	} req = {
1747 		.hdr = {
1748 			.bss_idx = mvif->idx,
1749 		},
1750 		.wow_ctrl_tlv = {
1751 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
1752 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
1753 			.cmd = suspend ? 1 : 2,
1754 		},
1755 		.gpio_tlv = {
1756 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
1757 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
1758 			.gpio_pin = 0xff, /* follow fw about GPIO pin */
1759 		},
1760 	};
1761 
1762 	if (wowlan->magic_pkt)
1763 		req.wow_ctrl_tlv.trigger |= BIT(0);
1764 	if (wowlan->disconnect)
1765 		req.wow_ctrl_tlv.trigger |= BIT(2);
1766 	if (wowlan->nd_config) {
1767 		mt76_connac_mcu_sched_scan_req(phy, vif, wowlan->nd_config);
1768 		req.wow_ctrl_tlv.trigger |= BIT(5);
1769 		mt76_connac_mcu_sched_scan_enable(phy, vif, suspend);
1770 	}
1771 
1772 	if (mt76_is_mmio(dev))
1773 		req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
1774 	else if (mt76_is_usb(dev))
1775 		req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
1776 	else if (mt76_is_sdio(dev))
1777 		req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
1778 
1779 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_SUSPEND, &req, sizeof(req),
1780 				 true);
1781 }
1782 
1783 int mt76_connac_mcu_set_hif_suspend(struct mt76_dev *dev, bool suspend)
1784 {
1785 	struct {
1786 		struct {
1787 			u8 hif_type; /* 0x0: HIF_SDIO
1788 				      * 0x1: HIF_USB
1789 				      * 0x2: HIF_PCIE
1790 				      */
1791 			u8 pad[3];
1792 		} __packed hdr;
1793 		struct hif_suspend_tlv {
1794 			__le16 tag;
1795 			__le16 len;
1796 			u8 suspend;
1797 		} __packed hif_suspend;
1798 	} req = {
1799 		.hif_suspend = {
1800 			.tag = cpu_to_le16(0), /* 0: UNI_HIF_CTRL_BASIC */
1801 			.len = cpu_to_le16(sizeof(struct hif_suspend_tlv)),
1802 			.suspend = suspend,
1803 		},
1804 	};
1805 
1806 	if (mt76_is_mmio(dev))
1807 		req.hdr.hif_type = 2;
1808 	else if (mt76_is_usb(dev))
1809 		req.hdr.hif_type = 1;
1810 	else if (mt76_is_sdio(dev))
1811 		req.hdr.hif_type = 0;
1812 
1813 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD_HIF_CTRL, &req, sizeof(req),
1814 				 true);
1815 }
1816 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_hif_suspend);
1817 
1818 void mt76_connac_mcu_set_suspend_iter(void *priv, u8 *mac,
1819 				      struct ieee80211_vif *vif)
1820 {
1821 	struct mt76_phy *phy = priv;
1822 	bool suspend = test_bit(MT76_STATE_SUSPEND, &phy->state);
1823 	struct ieee80211_hw *hw = phy->hw;
1824 	struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
1825 	int i;
1826 
1827 	mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
1828 	mt76_connac_mcu_set_arp_filter(phy->dev, vif, suspend);
1829 
1830 	mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
1831 
1832 	for (i = 0; i < wowlan->n_patterns; i++)
1833 		mt76_connac_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
1834 						&wowlan->patterns[i]);
1835 	mt76_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
1836 }
1837 EXPORT_SYMBOL_GPL(mt76_connac_mcu_set_suspend_iter);
1838 
1839 #endif /* CONFIG_PM */
1840 
1841 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo@kernel.org>");
1842 MODULE_LICENSE("Dual BSD/GPL");
1843