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