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