xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7925/mcu.c (revision 14b53301e8d482654f94c23e6884fe96b3d26825)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #include <linux/fs.h>
5 #include <linux/firmware.h>
6 #include "mt7925.h"
7 #include "mcu.h"
8 #include "mac.h"
9 
10 #define MT_STA_BFER			BIT(0)
11 #define MT_STA_BFEE			BIT(1)
12 
13 static bool mt7925_disable_clc;
14 module_param_named(disable_clc, mt7925_disable_clc, bool, 0644);
15 MODULE_PARM_DESC(disable_clc, "disable CLC support");
16 
mt7925_mcu_parse_response(struct mt76_dev * mdev,int cmd,struct sk_buff * skb,int seq)17 int mt7925_mcu_parse_response(struct mt76_dev *mdev, int cmd,
18 			      struct sk_buff *skb, int seq)
19 {
20 	int mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
21 	struct mt7925_mcu_rxd *rxd;
22 	int ret = 0;
23 
24 	if (!skb) {
25 		dev_err(mdev->dev, "Message %08x (seq %d) timeout\n", cmd, seq);
26 		mt792x_reset(mdev);
27 
28 		return -ETIMEDOUT;
29 	}
30 
31 	rxd = (struct mt7925_mcu_rxd *)skb->data;
32 	if (seq != rxd->seq)
33 		return -EAGAIN;
34 
35 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL) ||
36 	    cmd == MCU_CMD(PATCH_FINISH_REQ)) {
37 		skb_pull(skb, sizeof(*rxd) - 4);
38 		ret = *skb->data;
39 	} else if (cmd == MCU_UNI_CMD(DEV_INFO_UPDATE) ||
40 		   cmd == MCU_UNI_CMD(BSS_INFO_UPDATE) ||
41 		   cmd == MCU_UNI_CMD(STA_REC_UPDATE) ||
42 		   cmd == MCU_UNI_CMD(OFFLOAD) ||
43 		   cmd == MCU_UNI_CMD(SUSPEND)) {
44 		struct mt7925_mcu_uni_event *event;
45 
46 		skb_pull(skb, sizeof(*rxd));
47 		event = (struct mt7925_mcu_uni_event *)skb->data;
48 		ret = le32_to_cpu(event->status);
49 		/* skip invalid event */
50 		if (mcu_cmd != event->cid)
51 			ret = -EAGAIN;
52 	} else {
53 		skb_pull(skb, sizeof(*rxd));
54 	}
55 
56 	return ret;
57 }
58 EXPORT_SYMBOL_GPL(mt7925_mcu_parse_response);
59 
mt7925_mcu_regval(struct mt792x_dev * dev,u32 regidx,u32 * val,bool set)60 int mt7925_mcu_regval(struct mt792x_dev *dev, u32 regidx, u32 *val, bool set)
61 {
62 #define MT_RF_REG_HDR           GENMASK(31, 24)
63 #define MT_RF_REG_ANT           GENMASK(23, 16)
64 #define RF_REG_PREFIX           0x99
65 	struct {
66 		u8 __rsv[4];
67 		union {
68 			struct uni_cmd_access_reg_basic {
69 				__le16 tag;
70 				__le16 len;
71 				__le32 idx;
72 				__le32 data;
73 			} __packed reg;
74 			struct uni_cmd_access_rf_reg_basic {
75 				__le16 tag;
76 				__le16 len;
77 				__le16 ant;
78 				u8 __rsv[2];
79 				__le32 idx;
80 				__le32 data;
81 			} __packed rf_reg;
82 		};
83 	} __packed * res, req;
84 	struct sk_buff *skb;
85 	int ret;
86 
87 	if (u32_get_bits(regidx, MT_RF_REG_HDR) == RF_REG_PREFIX) {
88 		req.rf_reg.tag = cpu_to_le16(UNI_CMD_ACCESS_RF_REG_BASIC);
89 		req.rf_reg.len = cpu_to_le16(sizeof(req.rf_reg));
90 		req.rf_reg.ant = cpu_to_le16(u32_get_bits(regidx, MT_RF_REG_ANT));
91 		req.rf_reg.idx = cpu_to_le32(regidx);
92 		req.rf_reg.data = set ? cpu_to_le32(*val) : 0;
93 	} else {
94 		req.reg.tag = cpu_to_le16(UNI_CMD_ACCESS_REG_BASIC);
95 		req.reg.len = cpu_to_le16(sizeof(req.reg));
96 		req.reg.idx = cpu_to_le32(regidx);
97 		req.reg.data = set ? cpu_to_le32(*val) : 0;
98 	}
99 
100 	if (set)
101 		return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(REG_ACCESS),
102 					 &req, sizeof(req), true);
103 
104 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
105 					MCU_WM_UNI_CMD_QUERY(REG_ACCESS),
106 					&req, sizeof(req), true, &skb);
107 	if (ret)
108 		return ret;
109 
110 	res = (void *)skb->data;
111 	if (u32_get_bits(regidx, MT_RF_REG_HDR) == RF_REG_PREFIX)
112 		*val = le32_to_cpu(res->rf_reg.data);
113 	else
114 		*val = le32_to_cpu(res->reg.data);
115 
116 	dev_kfree_skb(skb);
117 
118 	return 0;
119 }
120 EXPORT_SYMBOL_GPL(mt7925_mcu_regval);
121 
mt7925_mcu_update_arp_filter(struct mt76_dev * dev,struct ieee80211_bss_conf * link_conf)122 int mt7925_mcu_update_arp_filter(struct mt76_dev *dev,
123 				 struct ieee80211_bss_conf *link_conf)
124 {
125 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
126 	struct ieee80211_vif *mvif = link_conf->vif;
127 	struct sk_buff *skb;
128 	int i, len = min_t(int, mvif->cfg.arp_addr_cnt,
129 			   IEEE80211_BSS_ARP_ADDR_LIST_LEN);
130 	struct {
131 		struct {
132 			u8 bss_idx;
133 			u8 pad[3];
134 		} __packed hdr;
135 		struct mt7925_arpns_tlv arp;
136 	} req = {
137 		.hdr = {
138 			.bss_idx = mconf->mt76.idx,
139 		},
140 		.arp = {
141 			.tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ARP),
142 			.len = cpu_to_le16(sizeof(req) - 4 + len * 2 * sizeof(__be32)),
143 			.ips_num = len,
144 			.enable = true,
145 		},
146 	};
147 
148 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(req) + len * 2 * sizeof(__be32));
149 	if (!skb)
150 		return -ENOMEM;
151 
152 	skb_put_data(skb, &req, sizeof(req));
153 	for (i = 0; i < len; i++) {
154 		skb_put_data(skb, &mvif->cfg.arp_addr_list[i], sizeof(__be32));
155 		skb_put_zero(skb, sizeof(__be32));
156 	}
157 
158 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(OFFLOAD), true);
159 }
160 
161 #ifdef CONFIG_PM
162 static int
mt7925_connac_mcu_set_wow_ctrl(struct mt76_phy * phy,struct ieee80211_vif * vif,bool suspend,struct cfg80211_wowlan * wowlan)163 mt7925_connac_mcu_set_wow_ctrl(struct mt76_phy *phy, struct ieee80211_vif *vif,
164 			       bool suspend, struct cfg80211_wowlan *wowlan)
165 {
166 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
167 	struct ieee80211_scan_ies ies = {};
168 	struct mt76_dev *dev = phy->dev;
169 	struct {
170 		struct {
171 			u8 bss_idx;
172 			u8 pad[3];
173 		} __packed hdr;
174 		struct mt76_connac_wow_ctrl_tlv wow_ctrl_tlv;
175 		struct mt76_connac_wow_gpio_param_tlv gpio_tlv;
176 	} req = {
177 		.hdr = {
178 			.bss_idx = mvif->idx,
179 		},
180 		.wow_ctrl_tlv = {
181 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_CTRL),
182 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_ctrl_tlv)),
183 			.cmd = suspend ? 1 : 2,
184 		},
185 		.gpio_tlv = {
186 			.tag = cpu_to_le16(UNI_SUSPEND_WOW_GPIO_PARAM),
187 			.len = cpu_to_le16(sizeof(struct mt76_connac_wow_gpio_param_tlv)),
188 			.gpio_pin = 0xff, /* follow fw about GPIO pin */
189 		},
190 	};
191 
192 	if (wowlan->magic_pkt)
193 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_MAGIC;
194 	if (wowlan->disconnect)
195 		req.wow_ctrl_tlv.trigger |= (UNI_WOW_DETECT_TYPE_DISCONNECT |
196 					     UNI_WOW_DETECT_TYPE_BCN_LOST);
197 	if (wowlan->nd_config) {
198 		mt7925_mcu_sched_scan_req(phy, vif, wowlan->nd_config, &ies);
199 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_SCH_SCAN_HIT;
200 		mt7925_mcu_sched_scan_enable(phy, vif, suspend);
201 	}
202 	if (wowlan->n_patterns)
203 		req.wow_ctrl_tlv.trigger |= UNI_WOW_DETECT_TYPE_BITMAP;
204 
205 	if (mt76_is_mmio(dev))
206 		req.wow_ctrl_tlv.wakeup_hif = WOW_PCIE;
207 	else if (mt76_is_usb(dev))
208 		req.wow_ctrl_tlv.wakeup_hif = WOW_USB;
209 	else if (mt76_is_sdio(dev))
210 		req.wow_ctrl_tlv.wakeup_hif = WOW_GPIO;
211 
212 	return mt76_mcu_send_msg(dev, MCU_UNI_CMD(SUSPEND), &req,
213 				 sizeof(req), true);
214 }
215 
216 static int
mt7925_mcu_set_wow_pattern(struct mt76_dev * dev,struct ieee80211_vif * vif,u8 index,bool enable,struct cfg80211_pkt_pattern * pattern)217 mt7925_mcu_set_wow_pattern(struct mt76_dev *dev,
218 			   struct ieee80211_vif *vif,
219 			   u8 index, bool enable,
220 			   struct cfg80211_pkt_pattern *pattern)
221 {
222 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
223 	struct mt7925_wow_pattern_tlv *tlv;
224 	struct sk_buff *skb;
225 	struct {
226 		u8 bss_idx;
227 		u8 pad[3];
228 	} __packed hdr = {
229 		.bss_idx = mvif->idx,
230 	};
231 
232 	skb = mt76_mcu_msg_alloc(dev, NULL, sizeof(hdr) + sizeof(*tlv));
233 	if (!skb)
234 		return -ENOMEM;
235 
236 	skb_put_data(skb, &hdr, sizeof(hdr));
237 	tlv = (struct mt7925_wow_pattern_tlv *)skb_put(skb, sizeof(*tlv));
238 	tlv->tag = cpu_to_le16(UNI_SUSPEND_WOW_PATTERN);
239 	tlv->len = cpu_to_le16(sizeof(*tlv));
240 	tlv->bss_idx = 0xF;
241 	tlv->data_len = pattern->pattern_len;
242 	tlv->enable = enable;
243 	tlv->index = index;
244 	tlv->offset = 0;
245 
246 	memcpy(tlv->pattern, pattern->pattern, pattern->pattern_len);
247 	memcpy(tlv->mask, pattern->mask, DIV_ROUND_UP(pattern->pattern_len, 8));
248 
249 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SUSPEND), true);
250 }
251 
mt7925_mcu_set_suspend_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)252 void mt7925_mcu_set_suspend_iter(void *priv, u8 *mac,
253 				 struct ieee80211_vif *vif)
254 {
255 	struct mt76_phy *phy = priv;
256 	bool suspend = !test_bit(MT76_STATE_RUNNING, &phy->state);
257 	struct ieee80211_hw *hw = phy->hw;
258 	struct cfg80211_wowlan *wowlan = hw->wiphy->wowlan_config;
259 	int i;
260 
261 	mt76_connac_mcu_set_gtk_rekey(phy->dev, vif, suspend);
262 
263 	mt76_connac_mcu_set_suspend_mode(phy->dev, vif, suspend, 1, true);
264 
265 	for (i = 0; i < wowlan->n_patterns; i++)
266 		mt7925_mcu_set_wow_pattern(phy->dev, vif, i, suspend,
267 					   &wowlan->patterns[i]);
268 	mt7925_connac_mcu_set_wow_ctrl(phy, vif, suspend, wowlan);
269 }
270 
271 #endif /* CONFIG_PM */
272 
273 static void
mt7925_mcu_connection_loss_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)274 mt7925_mcu_connection_loss_iter(void *priv, u8 *mac,
275 				struct ieee80211_vif *vif)
276 {
277 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
278 	struct mt7925_uni_beacon_loss_event *event = priv;
279 
280 	if (mvif->idx != event->hdr.bss_idx)
281 		return;
282 
283 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER) ||
284 	    vif->type != NL80211_IFTYPE_STATION)
285 		return;
286 
287 	ieee80211_connection_loss(vif);
288 }
289 
290 static void
mt7925_mcu_connection_loss_event(struct mt792x_dev * dev,struct sk_buff * skb)291 mt7925_mcu_connection_loss_event(struct mt792x_dev *dev, struct sk_buff *skb)
292 {
293 	struct mt7925_uni_beacon_loss_event *event;
294 	struct mt76_phy *mphy = &dev->mt76.phy;
295 
296 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd));
297 	event = (struct mt7925_uni_beacon_loss_event *)skb->data;
298 
299 	ieee80211_iterate_active_interfaces_atomic(mphy->hw,
300 					IEEE80211_IFACE_ITER_RESUME_ALL,
301 					mt7925_mcu_connection_loss_iter, event);
302 }
303 
304 static void
mt7925_mcu_roc_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)305 mt7925_mcu_roc_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
306 {
307 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
308 	struct mt7925_roc_grant_tlv *grant = priv;
309 
310 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION)
311 		return;
312 
313 	if (mvif->idx != grant->bss_idx)
314 		return;
315 
316 	mvif->band_idx = grant->dbdcband;
317 }
318 
mt7925_mcu_roc_handle_grant(struct mt792x_dev * dev,struct tlv * tlv)319 static void mt7925_mcu_roc_handle_grant(struct mt792x_dev *dev,
320 					struct tlv *tlv)
321 {
322 	struct ieee80211_hw *hw = dev->mt76.hw;
323 	struct mt7925_roc_grant_tlv *grant;
324 	int duration;
325 
326 	grant = (struct mt7925_roc_grant_tlv *)tlv;
327 
328 	/* should never happen */
329 	WARN_ON_ONCE((le16_to_cpu(grant->tag) != UNI_EVENT_ROC_GRANT));
330 
331 	if (grant->reqtype == MT7925_ROC_REQ_ROC)
332 		ieee80211_ready_on_channel(hw);
333 	else if (grant->reqtype == MT7925_ROC_REQ_JOIN)
334 		ieee80211_iterate_active_interfaces_atomic(hw,
335 						IEEE80211_IFACE_ITER_RESUME_ALL,
336 						mt7925_mcu_roc_iter, grant);
337 	dev->phy.roc_grant = true;
338 	wake_up(&dev->phy.roc_wait);
339 	duration = le32_to_cpu(grant->max_interval);
340 	mod_timer(&dev->phy.roc_timer,
341 		  jiffies + msecs_to_jiffies(duration));
342 }
343 
344 static void
mt7925_mcu_handle_hif_ctrl_basic(struct mt792x_dev * dev,struct tlv * tlv)345 mt7925_mcu_handle_hif_ctrl_basic(struct mt792x_dev *dev, struct tlv *tlv)
346 {
347 	struct mt7925_mcu_hif_ctrl_basic_tlv *basic;
348 
349 	basic = (struct mt7925_mcu_hif_ctrl_basic_tlv *)tlv;
350 
351 	if (basic->hifsuspend) {
352 		dev->hif_idle = true;
353 		if (!(basic->hif_tx_traffic_status == HIF_TRAFFIC_IDLE &&
354 		      basic->hif_rx_traffic_status == HIF_TRAFFIC_IDLE))
355 			dev_info(dev->mt76.dev, "Hif traffic not idle.\n");
356 	} else {
357 		dev->hif_resumed = true;
358 	}
359 	wake_up(&dev->wait);
360 }
361 
362 static void
mt7925_mcu_uni_hif_ctrl_event(struct mt792x_dev * dev,struct sk_buff * skb)363 mt7925_mcu_uni_hif_ctrl_event(struct mt792x_dev *dev, struct sk_buff *skb)
364 {
365 	struct tlv *tlv;
366 	u32 tlv_len;
367 
368 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
369 	tlv = (struct tlv *)skb->data;
370 	tlv_len = skb->len;
371 
372 	while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
373 		switch (le16_to_cpu(tlv->tag)) {
374 		case UNI_EVENT_HIF_CTRL_BASIC:
375 			mt7925_mcu_handle_hif_ctrl_basic(dev, tlv);
376 			break;
377 		default:
378 			break;
379 		}
380 		tlv_len -= le16_to_cpu(tlv->len);
381 		tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
382 	}
383 }
384 
385 static void
mt7925_mcu_uni_roc_event(struct mt792x_dev * dev,struct sk_buff * skb)386 mt7925_mcu_uni_roc_event(struct mt792x_dev *dev, struct sk_buff *skb)
387 {
388 	struct tlv *tlv;
389 	int i = 0;
390 
391 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
392 
393 	while (i < skb->len) {
394 		tlv = (struct tlv *)(skb->data + i);
395 
396 		switch (le16_to_cpu(tlv->tag)) {
397 		case UNI_EVENT_ROC_GRANT:
398 			mt7925_mcu_roc_handle_grant(dev, tlv);
399 			break;
400 		case UNI_EVENT_ROC_GRANT_SUB_LINK:
401 			break;
402 		}
403 
404 		i += le16_to_cpu(tlv->len);
405 	}
406 }
407 
408 static void
mt7925_mcu_scan_event(struct mt792x_dev * dev,struct sk_buff * skb)409 mt7925_mcu_scan_event(struct mt792x_dev *dev, struct sk_buff *skb)
410 {
411 	struct mt76_phy *mphy = &dev->mt76.phy;
412 	struct mt792x_phy *phy = mphy->priv;
413 
414 	spin_lock_bh(&dev->mt76.lock);
415 	__skb_queue_tail(&phy->scan_event_list, skb);
416 	spin_unlock_bh(&dev->mt76.lock);
417 
418 	ieee80211_queue_delayed_work(mphy->hw, &phy->scan_work,
419 				     MT792x_HW_SCAN_TIMEOUT);
420 }
421 
422 static void
mt7925_mcu_tx_done_event(struct mt792x_dev * dev,struct sk_buff * skb)423 mt7925_mcu_tx_done_event(struct mt792x_dev *dev, struct sk_buff *skb)
424 {
425 #define UNI_EVENT_TX_DONE_MSG 0
426 #define UNI_EVENT_TX_DONE_RAW 1
427 	struct mt7925_mcu_txs_event {
428 		u8 ver;
429 		u8 rsv[3];
430 		u8 data[];
431 	} __packed * txs;
432 	struct tlv *tlv;
433 	u32 tlv_len;
434 
435 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
436 	tlv = (struct tlv *)skb->data;
437 	tlv_len = skb->len;
438 
439 	while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) {
440 		switch (le16_to_cpu(tlv->tag)) {
441 		case UNI_EVENT_TX_DONE_RAW:
442 			txs = (struct mt7925_mcu_txs_event *)tlv->data;
443 			mt7925_mac_add_txs(dev, txs->data);
444 			break;
445 		default:
446 			break;
447 		}
448 		tlv_len -= le16_to_cpu(tlv->len);
449 		tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len));
450 	}
451 }
452 
453 static void
mt7925_mcu_uni_debug_msg_event(struct mt792x_dev * dev,struct sk_buff * skb)454 mt7925_mcu_uni_debug_msg_event(struct mt792x_dev *dev, struct sk_buff *skb)
455 {
456 	struct mt7925_uni_debug_msg {
457 		__le16 tag;
458 		__le16 len;
459 		u8 fmt;
460 		u8 rsv[3];
461 		u8 id;
462 		u8 type:3;
463 		u8 nr_args:5;
464 		union {
465 			struct idxlog {
466 				__le16 rsv;
467 				__le32 ts;
468 				__le32 idx;
469 				u8 data[];
470 			} __packed idx;
471 			struct txtlog {
472 				u8 len;
473 				u8 rsv;
474 				__le32 ts;
475 				u8 data[];
476 			} __packed txt;
477 		};
478 	} __packed * hdr;
479 
480 	skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4);
481 	hdr = (struct mt7925_uni_debug_msg *)skb->data;
482 
483 	if (hdr->id == 0x28) {
484 		skb_pull(skb, offsetof(struct mt7925_uni_debug_msg, id));
485 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", skb->len, skb->data);
486 		return;
487 	} else if (hdr->id != 0xa8) {
488 		return;
489 	}
490 
491 	if (hdr->type == 0) { /* idx log */
492 		int i, ret, len = PAGE_SIZE - 1, nr_val;
493 		struct page *page = dev_alloc_pages(get_order(len));
494 		__le32 *val;
495 		char *buf, *cur;
496 
497 		if (!page)
498 			return;
499 
500 		buf = page_address(page);
501 		cur = buf;
502 
503 		nr_val = (le16_to_cpu(hdr->len) - sizeof(*hdr)) / 4;
504 		val = (__le32 *)hdr->idx.data;
505 		for (i = 0; i < nr_val && len > 0; i++) {
506 			ret = snprintf(cur, len, "0x%x,", le32_to_cpu(val[i]));
507 			if (ret <= 0)
508 				break;
509 
510 			cur += ret;
511 			len -= ret;
512 		}
513 		if (cur > buf)
514 			wiphy_info(mt76_hw(dev)->wiphy, "idx: 0x%X,%d,%s",
515 				   le32_to_cpu(hdr->idx.idx), nr_val, buf);
516 		put_page(page);
517 	} else if (hdr->type == 2) { /* str log */
518 		wiphy_info(mt76_hw(dev)->wiphy, "%.*s", hdr->txt.len, hdr->txt.data);
519 	}
520 }
521 
522 static void
mt7925_mcu_uni_rx_unsolicited_event(struct mt792x_dev * dev,struct sk_buff * skb)523 mt7925_mcu_uni_rx_unsolicited_event(struct mt792x_dev *dev,
524 				    struct sk_buff *skb)
525 {
526 	struct mt7925_mcu_rxd *rxd;
527 
528 	rxd = (struct mt7925_mcu_rxd *)skb->data;
529 
530 	switch (rxd->eid) {
531 	case MCU_UNI_EVENT_HIF_CTRL:
532 		mt7925_mcu_uni_hif_ctrl_event(dev, skb);
533 		break;
534 	case MCU_UNI_EVENT_FW_LOG_2_HOST:
535 		mt7925_mcu_uni_debug_msg_event(dev, skb);
536 		break;
537 	case MCU_UNI_EVENT_ROC:
538 		mt7925_mcu_uni_roc_event(dev, skb);
539 		break;
540 	case MCU_UNI_EVENT_SCAN_DONE:
541 		mt7925_mcu_scan_event(dev, skb);
542 		return;
543 	case MCU_UNI_EVENT_TX_DONE:
544 		mt7925_mcu_tx_done_event(dev, skb);
545 		break;
546 	case MCU_UNI_EVENT_BSS_BEACON_LOSS:
547 		mt7925_mcu_connection_loss_event(dev, skb);
548 		break;
549 	case MCU_UNI_EVENT_COREDUMP:
550 		dev->fw_assert = true;
551 		mt76_connac_mcu_coredump_event(&dev->mt76, skb, &dev->coredump);
552 		return;
553 	default:
554 		break;
555 	}
556 	dev_kfree_skb(skb);
557 }
558 
mt7925_mcu_rx_event(struct mt792x_dev * dev,struct sk_buff * skb)559 void mt7925_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb)
560 {
561 	struct mt7925_mcu_rxd *rxd = (struct mt7925_mcu_rxd *)skb->data;
562 
563 	if (skb_linearize(skb))
564 		return;
565 
566 	if (rxd->option & MCU_UNI_CMD_UNSOLICITED_EVENT) {
567 		mt7925_mcu_uni_rx_unsolicited_event(dev, skb);
568 		return;
569 	}
570 
571 	mt76_mcu_rx_event(&dev->mt76, skb);
572 }
573 
574 static int
mt7925_mcu_sta_ba(struct mt76_dev * dev,struct mt76_vif_link * mvif,struct ieee80211_ampdu_params * params,bool enable,bool tx)575 mt7925_mcu_sta_ba(struct mt76_dev *dev, struct mt76_vif_link *mvif,
576 		  struct ieee80211_ampdu_params *params,
577 		  bool enable, bool tx)
578 {
579 	struct mt76_wcid *wcid = (struct mt76_wcid *)params->sta->drv_priv;
580 	struct sta_rec_ba_uni *ba;
581 	struct sk_buff *skb;
582 	struct tlv *tlv;
583 	int len;
584 
585 	len = sizeof(struct sta_req_hdr) + sizeof(*ba);
586 	skb = __mt76_connac_mcu_alloc_sta_req(dev, mvif, wcid,
587 					      len);
588 	if (IS_ERR(skb))
589 		return PTR_ERR(skb);
590 
591 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_BA, sizeof(*ba));
592 
593 	ba = (struct sta_rec_ba_uni *)tlv;
594 	ba->ba_type = tx ? MT_BA_TYPE_ORIGINATOR : MT_BA_TYPE_RECIPIENT;
595 	ba->winsize = cpu_to_le16(params->buf_size);
596 	ba->ssn = cpu_to_le16(params->ssn);
597 	ba->ba_en = enable << params->tid;
598 	ba->amsdu = params->amsdu;
599 	ba->tid = params->tid;
600 
601 	return mt76_mcu_skb_send_msg(dev, skb,
602 				     MCU_UNI_CMD(STA_REC_UPDATE), true);
603 }
604 
605 /** starec & wtbl **/
mt7925_mcu_uni_tx_ba(struct mt792x_dev * dev,struct ieee80211_ampdu_params * params,bool enable)606 int mt7925_mcu_uni_tx_ba(struct mt792x_dev *dev,
607 			 struct ieee80211_ampdu_params *params,
608 			 bool enable)
609 {
610 	struct mt792x_sta *msta = (struct mt792x_sta *)params->sta->drv_priv;
611 	struct mt792x_vif *mvif = msta->vif;
612 
613 	if (enable && !params->amsdu)
614 		msta->deflink.wcid.amsdu = false;
615 
616 	return mt7925_mcu_sta_ba(&dev->mt76, &mvif->bss_conf.mt76, params,
617 				 enable, true);
618 }
619 
mt7925_mcu_uni_rx_ba(struct mt792x_dev * dev,struct ieee80211_ampdu_params * params,bool enable)620 int mt7925_mcu_uni_rx_ba(struct mt792x_dev *dev,
621 			 struct ieee80211_ampdu_params *params,
622 			 bool enable)
623 {
624 	struct mt792x_sta *msta = (struct mt792x_sta *)params->sta->drv_priv;
625 	struct mt792x_vif *mvif = msta->vif;
626 
627 	return mt7925_mcu_sta_ba(&dev->mt76, &mvif->bss_conf.mt76, params,
628 				 enable, false);
629 }
630 
mt7925_mcu_read_eeprom(struct mt792x_dev * dev,u32 offset,u8 * val)631 static int mt7925_mcu_read_eeprom(struct mt792x_dev *dev, u32 offset, u8 *val)
632 {
633 	struct {
634 		u8 rsv[4];
635 
636 		__le16 tag;
637 		__le16 len;
638 
639 		__le32 addr;
640 		__le32 valid;
641 		u8 data[MT7925_EEPROM_BLOCK_SIZE];
642 	} __packed req = {
643 		.tag = cpu_to_le16(1),
644 		.len = cpu_to_le16(sizeof(req) - 4),
645 		.addr = cpu_to_le32(round_down(offset,
646 				    MT7925_EEPROM_BLOCK_SIZE)),
647 	};
648 	struct evt {
649 		u8 rsv[4];
650 
651 		__le16 tag;
652 		__le16 len;
653 
654 		__le32 ver;
655 		__le32 addr;
656 		__le32 valid;
657 		__le32 size;
658 		__le32 magic_num;
659 		__le32 type;
660 		__le32 rsv1[4];
661 		u8 data[32];
662 	} __packed *res;
663 	struct sk_buff *skb;
664 	int ret;
665 
666 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD_QUERY(EFUSE_CTRL),
667 					&req, sizeof(req), true, &skb);
668 	if (ret)
669 		return ret;
670 
671 	res = (struct evt *)skb->data;
672 	*val = res->data[offset % MT7925_EEPROM_BLOCK_SIZE];
673 
674 	dev_kfree_skb(skb);
675 
676 	return 0;
677 }
678 
mt7925_load_clc(struct mt792x_dev * dev,const char * fw_name)679 static int mt7925_load_clc(struct mt792x_dev *dev, const char *fw_name)
680 {
681 	const struct mt76_connac2_fw_trailer *hdr;
682 	const struct mt76_connac2_fw_region *region;
683 	const struct mt7925_clc *clc;
684 	struct mt76_dev *mdev = &dev->mt76;
685 	struct mt792x_phy *phy = &dev->phy;
686 	const struct firmware *fw;
687 #if defined(__linux__)
688 	u8 *clc_base = NULL, hw_encap = 0;
689 #elif defined(__FreeBSD__)
690 	const u8 *clc_base = NULL;
691 	u8 hw_encap = 0;
692 #endif
693 	int ret, i, len, offset = 0;
694 
695 	dev->phy.clc_chan_conf = 0xff;
696 	if (mt7925_disable_clc ||
697 	    mt76_is_usb(&dev->mt76))
698 		return 0;
699 
700 	if (mt76_is_mmio(&dev->mt76)) {
701 		ret = mt7925_mcu_read_eeprom(dev, MT_EE_HW_TYPE, &hw_encap);
702 		if (ret)
703 			return ret;
704 		hw_encap = u8_get_bits(hw_encap, MT_EE_HW_TYPE_ENCAP);
705 	}
706 
707 	ret = request_firmware(&fw, fw_name, mdev->dev);
708 	if (ret)
709 		return ret;
710 
711 	if (!fw || !fw->data || fw->size < sizeof(*hdr)) {
712 		dev_err(mdev->dev, "Invalid firmware\n");
713 		ret = -EINVAL;
714 		goto out;
715 	}
716 
717 	hdr = (const void *)(fw->data + fw->size - sizeof(*hdr));
718 	for (i = 0; i < hdr->n_region; i++) {
719 		region = (const void *)((const u8 *)hdr -
720 					(hdr->n_region - i) * sizeof(*region));
721 		len = le32_to_cpu(region->len);
722 
723 		/* check if we have valid buffer size */
724 		if (offset + len > fw->size) {
725 			dev_err(mdev->dev, "Invalid firmware region\n");
726 			ret = -EINVAL;
727 			goto out;
728 		}
729 
730 		if ((region->feature_set & FW_FEATURE_NON_DL) &&
731 		    region->type == FW_TYPE_CLC) {
732 #if defined(__linux__)
733 			clc_base = (u8 *)(fw->data + offset);
734 #elif defined(__FreeBSD__)
735 			clc_base = (const u8 *)(fw->data + offset);
736 #endif
737 			break;
738 		}
739 		offset += len;
740 	}
741 
742 	if (!clc_base)
743 		goto out;
744 
745 	for (offset = 0; offset < len; offset += le32_to_cpu(clc->len)) {
746 		clc = (const struct mt7925_clc *)(clc_base + offset);
747 
748 		if (clc->idx >= ARRAY_SIZE(phy->clc))
749 			break;
750 
751 		/* do not init buf again if chip reset triggered */
752 		if (phy->clc[clc->idx])
753 			continue;
754 
755 		/* header content sanity */
756 		if ((clc->idx == MT792x_CLC_BE_CTRL &&
757 		     u8_get_bits(clc->t2.type, MT_EE_HW_TYPE_ENCAP) != hw_encap) ||
758 		    u8_get_bits(clc->t0.type, MT_EE_HW_TYPE_ENCAP) != hw_encap)
759 			continue;
760 
761 		phy->clc[clc->idx] = devm_kmemdup(mdev->dev, clc,
762 						  le32_to_cpu(clc->len),
763 						  GFP_KERNEL);
764 
765 		if (!phy->clc[clc->idx]) {
766 			ret = -ENOMEM;
767 			goto out;
768 		}
769 	}
770 
771 	ret = mt7925_mcu_set_clc(dev, "00", ENVIRON_INDOOR);
772 out:
773 	release_firmware(fw);
774 
775 	return ret;
776 }
777 
mt7925_mcu_fw_log_2_host(struct mt792x_dev * dev,u8 ctrl)778 int mt7925_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl)
779 {
780 	struct {
781 		u8 _rsv[4];
782 
783 		__le16 tag;
784 		__le16 len;
785 		u8 ctrl;
786 		u8 interval;
787 		u8 _rsv2[2];
788 	} __packed req = {
789 		.tag = cpu_to_le16(UNI_WSYS_CONFIG_FW_LOG_CTRL),
790 		.len = cpu_to_le16(sizeof(req) - 4),
791 		.ctrl = ctrl,
792 	};
793 	int ret;
794 
795 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(WSYS_CONFIG),
796 					&req, sizeof(req), true, NULL);
797 	return ret;
798 }
799 
mt7925_mcu_get_temperature(struct mt792x_phy * phy)800 int mt7925_mcu_get_temperature(struct mt792x_phy *phy)
801 {
802 	struct {
803 		u8 _rsv[4];
804 
805 		__le16 tag;
806 		__le16 len;
807 		u8 _rsv2[4];
808 	} __packed req = {
809 		.tag = cpu_to_le16(0x0),
810 		.len = cpu_to_le16(sizeof(req) - 4),
811 	};
812 	struct mt7925_thermal_evt {
813 		u8 rsv[4];
814 		__le32 temperature;
815 	} __packed * evt;
816 	struct mt792x_dev *dev = phy->dev;
817 	int temperature, ret;
818 	struct sk_buff *skb;
819 
820 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
821 					MCU_WM_UNI_CMD_QUERY(THERMAL),
822 					&req, sizeof(req), true, &skb);
823 	if (ret)
824 		return ret;
825 
826 	skb_pull(skb, 4 + sizeof(struct tlv));
827 	evt = (struct mt7925_thermal_evt *)skb->data;
828 
829 	temperature = le32_to_cpu(evt->temperature);
830 
831 	dev_kfree_skb(skb);
832 
833 	return temperature;
834 }
835 
836 static void
mt7925_mcu_parse_phy_cap(struct mt792x_dev * dev,char * data)837 mt7925_mcu_parse_phy_cap(struct mt792x_dev *dev, char *data)
838 {
839 	struct mt76_phy *mphy = &dev->mt76.phy;
840 	struct mt76_dev *mdev = mphy->dev;
841 	struct mt7925_mcu_phy_cap {
842 		u8 ht;
843 		u8 vht;
844 		u8 _5g;
845 		u8 max_bw;
846 		u8 nss;
847 		u8 dbdc;
848 		u8 tx_ldpc;
849 		u8 rx_ldpc;
850 		u8 tx_stbc;
851 		u8 rx_stbc;
852 		u8 hw_path;
853 		u8 he;
854 		u8 eht;
855 	} __packed * cap;
856 	enum {
857 		WF0_24G,
858 		WF0_5G
859 	};
860 
861 	cap = (struct mt7925_mcu_phy_cap *)data;
862 
863 	mdev->phy.antenna_mask = BIT(cap->nss) - 1;
864 	mdev->phy.chainmask = mdev->phy.antenna_mask;
865 	mdev->phy.cap.has_2ghz = cap->hw_path & BIT(WF0_24G);
866 	mdev->phy.cap.has_5ghz = cap->hw_path & BIT(WF0_5G);
867 }
868 
869 static void
mt7925_mcu_parse_eml_cap(struct mt792x_dev * dev,char * data)870 mt7925_mcu_parse_eml_cap(struct mt792x_dev *dev, char *data)
871 {
872 	struct mt7925_mcu_eml_cap {
873 		u8 rsv[4];
874 		__le16 eml_cap;
875 		u8 rsv2[6];
876 	} __packed * cap;
877 
878 	cap = (struct mt7925_mcu_eml_cap *)data;
879 
880 	dev->phy.eml_cap = le16_to_cpu(cap->eml_cap);
881 }
882 
883 static int
mt7925_mcu_get_nic_capability(struct mt792x_dev * dev)884 mt7925_mcu_get_nic_capability(struct mt792x_dev *dev)
885 {
886 	struct mt76_phy *mphy = &dev->mt76.phy;
887 	struct {
888 		u8 _rsv[4];
889 
890 		__le16 tag;
891 		__le16 len;
892 	} __packed req = {
893 		.tag = cpu_to_le16(UNI_CHIP_CONFIG_NIC_CAPA),
894 		.len = cpu_to_le16(sizeof(req) - 4),
895 	};
896 	struct mt76_connac_cap_hdr {
897 		__le16 n_element;
898 		u8 rsv[2];
899 	} __packed * hdr;
900 	struct sk_buff *skb;
901 	int ret, i;
902 
903 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(CHIP_CONFIG),
904 					&req, sizeof(req), true, &skb);
905 	if (ret)
906 		return ret;
907 
908 	hdr = (struct mt76_connac_cap_hdr *)skb->data;
909 	if (skb->len < sizeof(*hdr)) {
910 		ret = -EINVAL;
911 		goto out;
912 	}
913 
914 	skb_pull(skb, sizeof(*hdr));
915 
916 	for (i = 0; i < le16_to_cpu(hdr->n_element); i++) {
917 		struct tlv *tlv = (struct tlv *)skb->data;
918 		int len;
919 
920 		if (skb->len < sizeof(*tlv))
921 			break;
922 
923 		len = le16_to_cpu(tlv->len);
924 		if (skb->len < len)
925 			break;
926 
927 		switch (le16_to_cpu(tlv->tag)) {
928 		case MT_NIC_CAP_6G:
929 			mphy->cap.has_6ghz = !!tlv->data[0];
930 			break;
931 		case MT_NIC_CAP_MAC_ADDR:
932 			memcpy(mphy->macaddr, (void *)tlv->data, ETH_ALEN);
933 			break;
934 		case MT_NIC_CAP_PHY:
935 			mt7925_mcu_parse_phy_cap(dev, tlv->data);
936 			break;
937 		case MT_NIC_CAP_CHIP_CAP:
938 			dev->phy.chip_cap = le64_to_cpu(*(__le64 *)tlv->data);
939 			break;
940 		case MT_NIC_CAP_EML_CAP:
941 			mt7925_mcu_parse_eml_cap(dev, tlv->data);
942 			break;
943 		default:
944 			break;
945 		}
946 		skb_pull(skb, len);
947 	}
948 out:
949 	dev_kfree_skb(skb);
950 	return ret;
951 }
952 
mt7925_mcu_chip_config(struct mt792x_dev * dev,const char * cmd)953 int mt7925_mcu_chip_config(struct mt792x_dev *dev, const char *cmd)
954 {
955 	u16 len = strlen(cmd) + 1;
956 	struct {
957 		u8 _rsv[4];
958 		__le16 tag;
959 		__le16 len;
960 		struct mt76_connac_config config;
961 	} __packed req = {
962 		.tag = cpu_to_le16(UNI_CHIP_CONFIG_CHIP_CFG),
963 		.len = cpu_to_le16(sizeof(req) - 4),
964 		.config = {
965 			.resp_type = 0,
966 			.type = 0,
967 			.data_size = cpu_to_le16(len),
968 		},
969 	};
970 
971 	memcpy(req.config.data, cmd, len);
972 
973 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(CHIP_CONFIG),
974 				 &req, sizeof(req), false);
975 }
976 
mt7925_mcu_set_deep_sleep(struct mt792x_dev * dev,bool enable)977 int mt7925_mcu_set_deep_sleep(struct mt792x_dev *dev, bool enable)
978 {
979 	char cmd[16];
980 
981 	snprintf(cmd, sizeof(cmd), "KeepFullPwr %d", !enable);
982 
983 	return mt7925_mcu_chip_config(dev, cmd);
984 }
985 EXPORT_SYMBOL_GPL(mt7925_mcu_set_deep_sleep);
986 
mt7925_mcu_set_thermal_protect(struct mt792x_dev * dev)987 int mt7925_mcu_set_thermal_protect(struct mt792x_dev *dev)
988 {
989 	char cmd[64];
990 	int ret = 0;
991 
992 	snprintf(cmd, sizeof(cmd), "ThermalProtGband %d %d %d %d %d %d %d %d %d %d",
993 		 0, 100, 90, 80, 30, 1, 1, 115, 105, 5);
994 	ret = mt7925_mcu_chip_config(dev, cmd);
995 
996 	snprintf(cmd, sizeof(cmd), "ThermalProtAband %d %d %d %d %d %d %d %d %d %d",
997 		 1, 100, 90, 80, 30, 1, 1, 115, 105, 5);
998 	ret |= mt7925_mcu_chip_config(dev, cmd);
999 
1000 	return ret;
1001 }
1002 EXPORT_SYMBOL_GPL(mt7925_mcu_set_thermal_protect);
1003 
mt7925_run_firmware(struct mt792x_dev * dev)1004 int mt7925_run_firmware(struct mt792x_dev *dev)
1005 {
1006 	int err;
1007 
1008 	err = mt792x_load_firmware(dev);
1009 	if (err)
1010 		return err;
1011 
1012 	err = mt7925_mcu_get_nic_capability(dev);
1013 	if (err)
1014 		return err;
1015 
1016 	set_bit(MT76_STATE_MCU_RUNNING, &dev->mphy.state);
1017 	err = mt7925_load_clc(dev, mt792x_ram_name(dev));
1018 	if (err)
1019 		return err;
1020 
1021 	return mt7925_mcu_fw_log_2_host(dev, 1);
1022 }
1023 EXPORT_SYMBOL_GPL(mt7925_run_firmware);
1024 
1025 static void
mt7925_mcu_sta_hdr_trans_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1026 mt7925_mcu_sta_hdr_trans_tlv(struct sk_buff *skb,
1027 			     struct ieee80211_vif *vif,
1028 			     struct ieee80211_link_sta *link_sta)
1029 {
1030 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1031 	struct sta_rec_hdr_trans *hdr_trans;
1032 	struct mt76_wcid *wcid;
1033 	struct tlv *tlv;
1034 
1035 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HDR_TRANS, sizeof(*hdr_trans));
1036 	hdr_trans = (struct sta_rec_hdr_trans *)tlv;
1037 	hdr_trans->dis_rx_hdr_tran = true;
1038 
1039 	if (vif->type == NL80211_IFTYPE_STATION)
1040 		hdr_trans->to_ds = true;
1041 	else
1042 		hdr_trans->from_ds = true;
1043 
1044 	if (link_sta) {
1045 		struct mt792x_sta *msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1046 		struct mt792x_link_sta *mlink;
1047 
1048 		mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1049 		wcid = &mlink->wcid;
1050 	} else {
1051 		wcid = &mvif->sta.deflink.wcid;
1052 	}
1053 
1054 	if (!wcid)
1055 		return;
1056 
1057 	hdr_trans->dis_rx_hdr_tran = !test_bit(MT_WCID_FLAG_HDR_TRANS, &wcid->flags);
1058 	if (test_bit(MT_WCID_FLAG_4ADDR, &wcid->flags)) {
1059 		hdr_trans->to_ds = true;
1060 		hdr_trans->from_ds = true;
1061 	}
1062 }
1063 
mt7925_mcu_wtbl_update_hdr_trans(struct mt792x_dev * dev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,int link_id)1064 int mt7925_mcu_wtbl_update_hdr_trans(struct mt792x_dev *dev,
1065 				     struct ieee80211_vif *vif,
1066 				     struct ieee80211_sta *sta,
1067 				     int link_id)
1068 {
1069 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1070 	struct ieee80211_link_sta *link_sta = sta ? &sta->deflink : NULL;
1071 	struct mt792x_link_sta *mlink;
1072 	struct mt792x_bss_conf *mconf;
1073 	struct mt792x_sta *msta;
1074 	struct sk_buff *skb;
1075 
1076 	msta = sta ? (struct mt792x_sta *)sta->drv_priv : &mvif->sta;
1077 
1078 	mlink = mt792x_sta_to_link(msta, link_id);
1079 	link_sta = mt792x_sta_to_link_sta(vif, sta, link_id);
1080 	mconf = mt792x_vif_to_link(mvif, link_id);
1081 
1082 	skb = __mt76_connac_mcu_alloc_sta_req(&dev->mt76, &mconf->mt76,
1083 					      &mlink->wcid,
1084 					      MT7925_STA_UPDATE_MAX_SIZE);
1085 	if (IS_ERR(skb))
1086 		return PTR_ERR(skb);
1087 
1088 	/* starec hdr trans */
1089 	mt7925_mcu_sta_hdr_trans_tlv(skb, vif, link_sta);
1090 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1091 				     MCU_WMWA_UNI_CMD(STA_REC_UPDATE), true);
1092 }
1093 
mt7925_mcu_set_tx(struct mt792x_dev * dev,struct ieee80211_bss_conf * bss_conf)1094 int mt7925_mcu_set_tx(struct mt792x_dev *dev,
1095 		      struct ieee80211_bss_conf *bss_conf)
1096 {
1097 #define MCU_EDCA_AC_PARAM	0
1098 #define WMM_AIFS_SET		BIT(0)
1099 #define WMM_CW_MIN_SET		BIT(1)
1100 #define WMM_CW_MAX_SET		BIT(2)
1101 #define WMM_TXOP_SET		BIT(3)
1102 #define WMM_PARAM_SET		(WMM_AIFS_SET | WMM_CW_MIN_SET | \
1103 				 WMM_CW_MAX_SET | WMM_TXOP_SET)
1104 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(bss_conf);
1105 	struct {
1106 		u8 bss_idx;
1107 		u8 __rsv[3];
1108 	} __packed hdr = {
1109 		.bss_idx = mconf->mt76.idx,
1110 	};
1111 	struct sk_buff *skb;
1112 	int len = sizeof(hdr) + IEEE80211_NUM_ACS * sizeof(struct edca);
1113 	int ac;
1114 
1115 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, len);
1116 	if (!skb)
1117 		return -ENOMEM;
1118 
1119 	skb_put_data(skb, &hdr, sizeof(hdr));
1120 
1121 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1122 		struct ieee80211_tx_queue_params *q = &mconf->queue_params[ac];
1123 		struct edca *e;
1124 		struct tlv *tlv;
1125 
1126 		tlv = mt76_connac_mcu_add_tlv(skb, MCU_EDCA_AC_PARAM, sizeof(*e));
1127 
1128 		e = (struct edca *)tlv;
1129 		e->set = WMM_PARAM_SET;
1130 		e->queue = ac;
1131 		e->aifs = q->aifs;
1132 		e->txop = cpu_to_le16(q->txop);
1133 
1134 		if (q->cw_min)
1135 			e->cw_min = fls(q->cw_min);
1136 		else
1137 			e->cw_min = 5;
1138 
1139 		if (q->cw_max)
1140 			e->cw_max = fls(q->cw_max);
1141 		else
1142 			e->cw_max = 10;
1143 	}
1144 
1145 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
1146 				     MCU_UNI_CMD(EDCA_UPDATE), true);
1147 }
1148 
1149 static int
mt7925_mcu_sta_key_tlv(struct mt76_wcid * wcid,struct mt76_connac_sta_key_conf * sta_key_conf,struct sk_buff * skb,struct ieee80211_key_conf * key,enum set_key_cmd cmd,struct mt792x_sta * msta)1150 mt7925_mcu_sta_key_tlv(struct mt76_wcid *wcid,
1151 		       struct mt76_connac_sta_key_conf *sta_key_conf,
1152 		       struct sk_buff *skb,
1153 		       struct ieee80211_key_conf *key,
1154 		       enum set_key_cmd cmd,
1155 		       struct mt792x_sta *msta)
1156 {
1157 	struct mt792x_vif *mvif = msta->vif;
1158 	struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, wcid->link_id);
1159 	struct sta_rec_sec_uni *sec;
1160 	struct ieee80211_sta *sta;
1161 	struct ieee80211_vif *vif;
1162 	struct tlv *tlv;
1163 
1164 	sta = msta == &mvif->sta ?
1165 		      NULL :
1166 		      container_of((void *)msta, struct ieee80211_sta, drv_priv);
1167 	vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv);
1168 
1169 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_KEY_V3, sizeof(*sec));
1170 	sec = (struct sta_rec_sec_uni *)tlv;
1171 	sec->bss_idx = mconf->mt76.idx;
1172 	sec->is_authenticator = 0;
1173 	sec->mgmt_prot = 1; /* only used in MLO mode */
1174 	sec->wlan_idx = (u8)wcid->idx;
1175 
1176 	if (sta) {
1177 		struct ieee80211_link_sta *link_sta;
1178 
1179 		sec->tx_key = 1;
1180 		sec->key_type = 1;
1181 		link_sta = mt792x_sta_to_link_sta(vif, sta, wcid->link_id);
1182 
1183 		if (link_sta)
1184 			memcpy(sec->peer_addr, link_sta->addr, ETH_ALEN);
1185 	} else {
1186 		struct ieee80211_bss_conf *link_conf;
1187 
1188 		link_conf = mt792x_vif_to_bss_conf(vif, wcid->link_id);
1189 
1190 		if (link_conf)
1191 			memcpy(sec->peer_addr, link_conf->bssid, ETH_ALEN);
1192 	}
1193 
1194 	if (cmd == SET_KEY) {
1195 		u8 cipher;
1196 
1197 		sec->add = 1;
1198 		cipher = mt7925_mcu_get_cipher(key->cipher);
1199 		if (cipher == CONNAC3_CIPHER_NONE)
1200 			return -EOPNOTSUPP;
1201 
1202 		if (cipher == CONNAC3_CIPHER_BIP_CMAC_128) {
1203 			sec->cipher_id = CONNAC3_CIPHER_BIP_CMAC_128;
1204 			sec->key_id = sta_key_conf->keyidx;
1205 			sec->key_len = 32;
1206 			memcpy(sec->key, sta_key_conf->key, 16);
1207 			memcpy(sec->key + 16, key->key, 16);
1208 		} else {
1209 			sec->cipher_id = cipher;
1210 			sec->key_id = key->keyidx;
1211 			sec->key_len = key->keylen;
1212 			memcpy(sec->key, key->key, key->keylen);
1213 
1214 			if (cipher == CONNAC3_CIPHER_TKIP) {
1215 				/* Rx/Tx MIC keys are swapped */
1216 				memcpy(sec->key + 16, key->key + 24, 8);
1217 				memcpy(sec->key + 24, key->key + 16, 8);
1218 			}
1219 
1220 			/* store key_conf for BIP batch update */
1221 			if (cipher == CONNAC3_CIPHER_AES_CCMP) {
1222 				memcpy(sta_key_conf->key, key->key, key->keylen);
1223 				sta_key_conf->keyidx = key->keyidx;
1224 			}
1225 		}
1226 	} else {
1227 		sec->add = 0;
1228 	}
1229 
1230 	return 0;
1231 }
1232 
mt7925_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,struct mt792x_sta * msta)1233 int mt7925_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
1234 		       struct mt76_connac_sta_key_conf *sta_key_conf,
1235 		       struct ieee80211_key_conf *key, int mcu_cmd,
1236 		       struct mt76_wcid *wcid, enum set_key_cmd cmd,
1237 		       struct mt792x_sta *msta)
1238 {
1239 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1240 	struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, wcid->link_id);
1241 	struct sk_buff *skb;
1242 	int ret;
1243 
1244 	skb = __mt76_connac_mcu_alloc_sta_req(dev, &mconf->mt76, wcid,
1245 					      MT7925_STA_UPDATE_MAX_SIZE);
1246 	if (IS_ERR(skb))
1247 		return PTR_ERR(skb);
1248 
1249 	ret = mt7925_mcu_sta_key_tlv(wcid, sta_key_conf, skb, key, cmd, msta);
1250 	if (ret)
1251 		return ret;
1252 
1253 	return mt76_mcu_skb_send_msg(dev, skb, mcu_cmd, true);
1254 }
1255 
mt7925_mcu_set_mlo_roc(struct mt792x_bss_conf * mconf,u16 sel_links,int duration,u8 token_id)1256 int mt7925_mcu_set_mlo_roc(struct mt792x_bss_conf *mconf, u16 sel_links,
1257 			   int duration, u8 token_id)
1258 {
1259 	struct mt792x_vif *mvif = mconf->vif;
1260 	struct ieee80211_vif *vif = container_of((void *)mvif,
1261 						 struct ieee80211_vif, drv_priv);
1262 	struct ieee80211_bss_conf *link_conf;
1263 	struct ieee80211_channel *chan;
1264 	const u8 ch_band[] = {
1265 		[NL80211_BAND_2GHZ] = 1,
1266 		[NL80211_BAND_5GHZ] = 2,
1267 		[NL80211_BAND_6GHZ] = 3,
1268 	};
1269 	enum mt7925_roc_req type;
1270 	int center_ch, i = 0;
1271 	bool is_AG_band = false;
1272 	struct {
1273 		u8 id;
1274 		u8 bss_idx;
1275 		u16 tag;
1276 		struct mt792x_bss_conf *mconf;
1277 		struct ieee80211_channel *chan;
1278 	} links[2];
1279 
1280 	struct {
1281 		struct {
1282 			u8 rsv[4];
1283 		} __packed hdr;
1284 		struct roc_acquire_tlv roc[2];
1285 	} __packed req = {
1286 			.roc[0].tag = cpu_to_le16(UNI_ROC_NUM),
1287 			.roc[0].len = cpu_to_le16(sizeof(struct roc_acquire_tlv)),
1288 			.roc[1].tag = cpu_to_le16(UNI_ROC_NUM),
1289 			.roc[1].len = cpu_to_le16(sizeof(struct roc_acquire_tlv))
1290 	};
1291 
1292 	if (!mconf || hweight16(vif->valid_links) < 2 ||
1293 	    hweight16(sel_links) != 2)
1294 		return -EPERM;
1295 
1296 	for (i = 0; i < ARRAY_SIZE(links); i++) {
1297 		links[i].id = i ? __ffs(~BIT(mconf->link_id) & sel_links) :
1298 				 mconf->link_id;
1299 		link_conf = mt792x_vif_to_bss_conf(vif, links[i].id);
1300 		if (WARN_ON_ONCE(!link_conf))
1301 			return -EPERM;
1302 
1303 		links[i].chan = link_conf->chanreq.oper.chan;
1304 		if (WARN_ON_ONCE(!links[i].chan))
1305 			return -EPERM;
1306 
1307 		links[i].mconf = mt792x_vif_to_link(mvif, links[i].id);
1308 		links[i].tag = links[i].id == mconf->link_id ?
1309 			       UNI_ROC_ACQUIRE : UNI_ROC_SUB_LINK;
1310 
1311 		is_AG_band |= links[i].chan->band == NL80211_BAND_2GHZ;
1312 	}
1313 
1314 	if (vif->cfg.eml_cap & IEEE80211_EML_CAP_EMLSR_SUPP)
1315 		type = is_AG_band ? MT7925_ROC_REQ_MLSR_AG :
1316 				    MT7925_ROC_REQ_MLSR_AA;
1317 	else
1318 		type = MT7925_ROC_REQ_JOIN;
1319 
1320 	for (i = 0; i < ARRAY_SIZE(links) && i < hweight16(vif->active_links); i++) {
1321 		if (WARN_ON_ONCE(!links[i].mconf || !links[i].chan))
1322 			continue;
1323 
1324 		chan = links[i].chan;
1325 		center_ch = ieee80211_frequency_to_channel(chan->center_freq);
1326 		req.roc[i].len = cpu_to_le16(sizeof(struct roc_acquire_tlv));
1327 		req.roc[i].tag = cpu_to_le16(links[i].tag);
1328 		req.roc[i].tokenid = token_id;
1329 		req.roc[i].reqtype = type;
1330 		req.roc[i].maxinterval = cpu_to_le32(duration);
1331 		req.roc[i].bss_idx = links[i].mconf->mt76.idx;
1332 		req.roc[i].control_channel = chan->hw_value;
1333 		req.roc[i].bw = CMD_CBW_20MHZ;
1334 		req.roc[i].bw_from_ap = CMD_CBW_20MHZ;
1335 		req.roc[i].center_chan = center_ch;
1336 		req.roc[i].center_chan_from_ap = center_ch;
1337 		req.roc[i].center_chan2 = 0;
1338 		req.roc[i].center_chan2_from_ap = 0;
1339 
1340 		/* STR : 0xfe indicates BAND_ALL with enabling DBDC
1341 		 * EMLSR : 0xff indicates (BAND_AUTO) without DBDC
1342 		 */
1343 		req.roc[i].dbdcband = type == MT7925_ROC_REQ_JOIN ? 0xfe : 0xff;
1344 
1345 		if (chan->hw_value < center_ch)
1346 			req.roc[i].sco = 1; /* SCA */
1347 		else if (chan->hw_value > center_ch)
1348 			req.roc[i].sco = 3; /* SCB */
1349 
1350 		req.roc[i].band = ch_band[chan->band];
1351 	}
1352 
1353 	return mt76_mcu_send_msg(&mvif->phy->dev->mt76, MCU_UNI_CMD(ROC),
1354 				 &req, sizeof(req), true);
1355 }
1356 
mt7925_mcu_set_roc(struct mt792x_phy * phy,struct mt792x_bss_conf * mconf,struct ieee80211_channel * chan,int duration,enum mt7925_roc_req type,u8 token_id)1357 int mt7925_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_bss_conf *mconf,
1358 		       struct ieee80211_channel *chan, int duration,
1359 		       enum mt7925_roc_req type, u8 token_id)
1360 {
1361 	int center_ch = ieee80211_frequency_to_channel(chan->center_freq);
1362 	struct mt792x_dev *dev = phy->dev;
1363 	struct {
1364 		struct {
1365 			u8 rsv[4];
1366 		} __packed hdr;
1367 		struct roc_acquire_tlv roc;
1368 	} __packed req = {
1369 		.roc = {
1370 			.tag = cpu_to_le16(UNI_ROC_ACQUIRE),
1371 			.len = cpu_to_le16(sizeof(struct roc_acquire_tlv)),
1372 			.tokenid = token_id,
1373 			.reqtype = type,
1374 			.maxinterval = cpu_to_le32(duration),
1375 			.bss_idx = mconf->mt76.idx,
1376 			.control_channel = chan->hw_value,
1377 			.bw = CMD_CBW_20MHZ,
1378 			.bw_from_ap = CMD_CBW_20MHZ,
1379 			.center_chan = center_ch,
1380 			.center_chan_from_ap = center_ch,
1381 			.dbdcband = 0xff, /* auto */
1382 		},
1383 	};
1384 
1385 	if (chan->hw_value < center_ch)
1386 		req.roc.sco = 1; /* SCA */
1387 	else if (chan->hw_value > center_ch)
1388 		req.roc.sco = 3; /* SCB */
1389 
1390 	switch (chan->band) {
1391 	case NL80211_BAND_6GHZ:
1392 		req.roc.band = 3;
1393 		break;
1394 	case NL80211_BAND_5GHZ:
1395 		req.roc.band = 2;
1396 		break;
1397 	default:
1398 		req.roc.band = 1;
1399 		break;
1400 	}
1401 
1402 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
1403 				 &req, sizeof(req), true);
1404 }
1405 
mt7925_mcu_abort_roc(struct mt792x_phy * phy,struct mt792x_bss_conf * mconf,u8 token_id)1406 int mt7925_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_bss_conf *mconf,
1407 			 u8 token_id)
1408 {
1409 	struct mt792x_dev *dev = phy->dev;
1410 	struct {
1411 		struct {
1412 			u8 rsv[4];
1413 		} __packed hdr;
1414 		struct roc_abort_tlv {
1415 			__le16 tag;
1416 			__le16 len;
1417 			u8 bss_idx;
1418 			u8 tokenid;
1419 			u8 dbdcband;
1420 			u8 rsv[5];
1421 		} __packed abort;
1422 	} __packed req = {
1423 		.abort = {
1424 			.tag = cpu_to_le16(UNI_ROC_ABORT),
1425 			.len = cpu_to_le16(sizeof(struct roc_abort_tlv)),
1426 			.tokenid = token_id,
1427 			.bss_idx = mconf->mt76.idx,
1428 			.dbdcband = 0xff, /* auto*/
1429 		},
1430 	};
1431 
1432 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(ROC),
1433 				 &req, sizeof(req), true);
1434 }
1435 
mt7925_mcu_set_eeprom(struct mt792x_dev * dev)1436 int mt7925_mcu_set_eeprom(struct mt792x_dev *dev)
1437 {
1438 	struct {
1439 		u8 _rsv[4];
1440 
1441 		__le16 tag;
1442 		__le16 len;
1443 		u8 buffer_mode;
1444 		u8 format;
1445 		__le16 buf_len;
1446 	} __packed req = {
1447 		.tag = cpu_to_le16(UNI_EFUSE_BUFFER_MODE),
1448 		.len = cpu_to_le16(sizeof(req) - 4),
1449 		.buffer_mode = EE_MODE_EFUSE,
1450 		.format = EE_FORMAT_WHOLE
1451 	};
1452 
1453 	return mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(EFUSE_CTRL),
1454 					 &req, sizeof(req), true, NULL);
1455 }
1456 EXPORT_SYMBOL_GPL(mt7925_mcu_set_eeprom);
1457 
mt7925_mcu_uni_bss_ps(struct mt792x_dev * dev,struct ieee80211_bss_conf * link_conf)1458 int mt7925_mcu_uni_bss_ps(struct mt792x_dev *dev,
1459 			  struct ieee80211_bss_conf *link_conf)
1460 {
1461 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1462 	struct {
1463 		struct {
1464 			u8 bss_idx;
1465 			u8 pad[3];
1466 		} __packed hdr;
1467 		struct ps_tlv {
1468 			__le16 tag;
1469 			__le16 len;
1470 			u8 ps_state; /* 0: device awake
1471 				      * 1: static power save
1472 				      * 2: dynamic power saving
1473 				      * 3: enter TWT power saving
1474 				      * 4: leave TWT power saving
1475 				      */
1476 			u8 pad[3];
1477 		} __packed ps;
1478 	} __packed ps_req = {
1479 		.hdr = {
1480 			.bss_idx = mconf->mt76.idx,
1481 		},
1482 		.ps = {
1483 			.tag = cpu_to_le16(UNI_BSS_INFO_PS),
1484 			.len = cpu_to_le16(sizeof(struct ps_tlv)),
1485 			.ps_state = link_conf->vif->cfg.ps ? 2 : 0,
1486 		},
1487 	};
1488 
1489 	if (link_conf->vif->type != NL80211_IFTYPE_STATION)
1490 		return -EOPNOTSUPP;
1491 
1492 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1493 				 &ps_req, sizeof(ps_req), true);
1494 }
1495 
1496 int
mt7925_mcu_uni_bss_bcnft(struct mt792x_dev * dev,struct ieee80211_bss_conf * link_conf,bool enable)1497 mt7925_mcu_uni_bss_bcnft(struct mt792x_dev *dev,
1498 			 struct ieee80211_bss_conf *link_conf, bool enable)
1499 {
1500 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1501 	struct {
1502 		struct {
1503 			u8 bss_idx;
1504 			u8 pad[3];
1505 		} __packed hdr;
1506 		struct bcnft_tlv {
1507 			__le16 tag;
1508 			__le16 len;
1509 			__le16 bcn_interval;
1510 			u8 dtim_period;
1511 			u8 bmc_delivered_ac;
1512 			u8 bmc_triggered_ac;
1513 			u8 pad[3];
1514 		} __packed bcnft;
1515 	} __packed bcnft_req = {
1516 		.hdr = {
1517 			.bss_idx = mconf->mt76.idx,
1518 		},
1519 		.bcnft = {
1520 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
1521 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
1522 			.bcn_interval = cpu_to_le16(link_conf->beacon_int),
1523 			.dtim_period = link_conf->dtim_period,
1524 		},
1525 	};
1526 
1527 	if (link_conf->vif->type != NL80211_IFTYPE_STATION)
1528 		return 0;
1529 
1530 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1531 				 &bcnft_req, sizeof(bcnft_req), true);
1532 }
1533 
1534 int
mt7925_mcu_set_bss_pm(struct mt792x_dev * dev,struct ieee80211_bss_conf * link_conf,bool enable)1535 mt7925_mcu_set_bss_pm(struct mt792x_dev *dev,
1536 		      struct ieee80211_bss_conf *link_conf,
1537 		      bool enable)
1538 {
1539 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
1540 	struct {
1541 		struct {
1542 			u8 bss_idx;
1543 			u8 pad[3];
1544 		} __packed hdr;
1545 		struct bcnft_tlv {
1546 			__le16 tag;
1547 			__le16 len;
1548 			__le16 bcn_interval;
1549 			u8 dtim_period;
1550 			u8 bmc_delivered_ac;
1551 			u8 bmc_triggered_ac;
1552 			u8 pad[3];
1553 		} __packed enable;
1554 	} req = {
1555 		.hdr = {
1556 			.bss_idx = mconf->mt76.idx,
1557 		},
1558 		.enable = {
1559 			.tag = cpu_to_le16(UNI_BSS_INFO_BCNFT),
1560 			.len = cpu_to_le16(sizeof(struct bcnft_tlv)),
1561 			.dtim_period = link_conf->dtim_period,
1562 			.bcn_interval = cpu_to_le16(link_conf->beacon_int),
1563 		},
1564 	};
1565 	struct {
1566 		struct {
1567 			u8 bss_idx;
1568 			u8 pad[3];
1569 		} __packed hdr;
1570 		struct pm_disable {
1571 			__le16 tag;
1572 			__le16 len;
1573 		} __packed disable;
1574 	} req1 = {
1575 		.hdr = {
1576 			.bss_idx = mconf->mt76.idx,
1577 		},
1578 		.disable = {
1579 			.tag = cpu_to_le16(UNI_BSS_INFO_PM_DISABLE),
1580 			.len = cpu_to_le16(sizeof(struct pm_disable))
1581 		},
1582 	};
1583 	int err;
1584 
1585 	err = mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1586 				&req1, sizeof(req1), true);
1587 	if (err < 0 || !enable)
1588 		return err;
1589 
1590 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
1591 				 &req, sizeof(req), true);
1592 }
1593 
1594 static void
mt7925_mcu_sta_he_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1595 mt7925_mcu_sta_he_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1596 {
1597 	if (!link_sta->he_cap.has_he)
1598 		return;
1599 
1600 	mt76_connac_mcu_sta_he_tlv_v2(skb, link_sta->sta);
1601 }
1602 
1603 static void
mt7925_mcu_sta_he_6g_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1604 mt7925_mcu_sta_he_6g_tlv(struct sk_buff *skb,
1605 			 struct ieee80211_link_sta *link_sta)
1606 {
1607 	struct sta_rec_he_6g_capa *he_6g;
1608 	struct tlv *tlv;
1609 
1610 	if (!link_sta->he_6ghz_capa.capa)
1611 		return;
1612 
1613 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HE_6G, sizeof(*he_6g));
1614 
1615 	he_6g = (struct sta_rec_he_6g_capa *)tlv;
1616 	he_6g->capa = link_sta->he_6ghz_capa.capa;
1617 }
1618 
1619 static void
mt7925_mcu_sta_eht_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1620 mt7925_mcu_sta_eht_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1621 {
1622 	struct ieee80211_eht_mcs_nss_supp *mcs_map;
1623 	struct ieee80211_eht_cap_elem_fixed *elem;
1624 	struct sta_rec_eht *eht;
1625 	struct tlv *tlv;
1626 
1627 	if (!link_sta->eht_cap.has_eht)
1628 		return;
1629 
1630 	mcs_map = &link_sta->eht_cap.eht_mcs_nss_supp;
1631 	elem = &link_sta->eht_cap.eht_cap_elem;
1632 
1633 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_EHT, sizeof(*eht));
1634 
1635 	eht = (struct sta_rec_eht *)tlv;
1636 	eht->tid_bitmap = 0xff;
1637 	eht->mac_cap = cpu_to_le16(*(u16 *)elem->mac_cap_info);
1638 	eht->phy_cap = cpu_to_le64(*(u64 *)elem->phy_cap_info);
1639 	eht->phy_cap_ext = cpu_to_le64(elem->phy_cap_info[8]);
1640 
1641 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
1642 		memcpy(eht->mcs_map_bw20, &mcs_map->only_20mhz, sizeof(eht->mcs_map_bw20));
1643 	memcpy(eht->mcs_map_bw80, &mcs_map->bw._80, sizeof(eht->mcs_map_bw80));
1644 	memcpy(eht->mcs_map_bw160, &mcs_map->bw._160, sizeof(eht->mcs_map_bw160));
1645 }
1646 
1647 static void
mt7925_mcu_sta_ht_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1648 mt7925_mcu_sta_ht_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1649 {
1650 	struct sta_rec_ht *ht;
1651 	struct tlv *tlv;
1652 
1653 	if (!link_sta->ht_cap.ht_supported)
1654 		return;
1655 
1656 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
1657 
1658 	ht = (struct sta_rec_ht *)tlv;
1659 	ht->ht_cap = cpu_to_le16(link_sta->ht_cap.cap);
1660 }
1661 
1662 static void
mt7925_mcu_sta_vht_tlv(struct sk_buff * skb,struct ieee80211_link_sta * link_sta)1663 mt7925_mcu_sta_vht_tlv(struct sk_buff *skb, struct ieee80211_link_sta *link_sta)
1664 {
1665 	struct sta_rec_vht *vht;
1666 	struct tlv *tlv;
1667 
1668 	/* For 6G band, this tlv is necessary to let hw work normally */
1669 	if (!link_sta->he_6ghz_capa.capa && !link_sta->vht_cap.vht_supported)
1670 		return;
1671 
1672 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_VHT, sizeof(*vht));
1673 
1674 	vht = (struct sta_rec_vht *)tlv;
1675 	vht->vht_cap = cpu_to_le32(link_sta->vht_cap.cap);
1676 	vht->vht_rx_mcs_map = link_sta->vht_cap.vht_mcs.rx_mcs_map;
1677 	vht->vht_tx_mcs_map = link_sta->vht_cap.vht_mcs.tx_mcs_map;
1678 }
1679 
1680 static void
mt7925_mcu_sta_amsdu_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1681 mt7925_mcu_sta_amsdu_tlv(struct sk_buff *skb,
1682 			 struct ieee80211_vif *vif,
1683 			 struct ieee80211_link_sta *link_sta)
1684 {
1685 	struct mt792x_sta *msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1686 	struct mt792x_link_sta *mlink;
1687 	struct sta_rec_amsdu *amsdu;
1688 	struct tlv *tlv;
1689 
1690 	if (vif->type != NL80211_IFTYPE_STATION &&
1691 	    vif->type != NL80211_IFTYPE_AP)
1692 		return;
1693 
1694 	if (!link_sta->agg.max_amsdu_len)
1695 		return;
1696 
1697 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HW_AMSDU, sizeof(*amsdu));
1698 	amsdu = (struct sta_rec_amsdu *)tlv;
1699 	amsdu->max_amsdu_num = 8;
1700 	amsdu->amsdu_en = true;
1701 
1702 	mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1703 	mlink->wcid.amsdu = true;
1704 
1705 	switch (link_sta->agg.max_amsdu_len) {
1706 	case IEEE80211_MAX_MPDU_LEN_VHT_11454:
1707 		amsdu->max_mpdu_size =
1708 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454;
1709 		return;
1710 	case IEEE80211_MAX_MPDU_LEN_HT_7935:
1711 	case IEEE80211_MAX_MPDU_LEN_VHT_7991:
1712 		amsdu->max_mpdu_size = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991;
1713 		return;
1714 	default:
1715 		amsdu->max_mpdu_size = IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895;
1716 		return;
1717 	}
1718 }
1719 
1720 static void
mt7925_mcu_sta_phy_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1721 mt7925_mcu_sta_phy_tlv(struct sk_buff *skb,
1722 		       struct ieee80211_vif *vif,
1723 		       struct ieee80211_link_sta *link_sta)
1724 {
1725 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1726 	struct ieee80211_bss_conf *link_conf;
1727 	struct cfg80211_chan_def *chandef;
1728 	struct mt792x_bss_conf *mconf;
1729 	struct sta_rec_phy *phy;
1730 	struct tlv *tlv;
1731 	u8 af = 0, mm = 0;
1732 
1733 	link_conf = mt792x_vif_to_bss_conf(vif, link_sta->link_id);
1734 	mconf = mt792x_vif_to_link(mvif, link_sta->link_id);
1735 	chandef = mconf->mt76.ctx ? &mconf->mt76.ctx->def :
1736 				    &link_conf->chanreq.oper;
1737 
1738 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_PHY, sizeof(*phy));
1739 	phy = (struct sta_rec_phy *)tlv;
1740 	phy->phy_type = mt76_connac_get_phy_mode_v2(mvif->phy->mt76, vif,
1741 						    chandef->chan->band,
1742 						    link_sta);
1743 	phy->basic_rate = cpu_to_le16((u16)link_conf->basic_rates);
1744 	if (link_sta->ht_cap.ht_supported) {
1745 		af = link_sta->ht_cap.ampdu_factor;
1746 		mm = link_sta->ht_cap.ampdu_density;
1747 	}
1748 
1749 	if (link_sta->vht_cap.vht_supported) {
1750 		u8 vht_af = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
1751 				      link_sta->vht_cap.cap);
1752 
1753 		af = max_t(u8, af, vht_af);
1754 	}
1755 
1756 	if (link_sta->he_6ghz_capa.capa) {
1757 		af = le16_get_bits(link_sta->he_6ghz_capa.capa,
1758 				   IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
1759 		mm = le16_get_bits(link_sta->he_6ghz_capa.capa,
1760 				   IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
1761 	}
1762 
1763 	phy->ampdu = FIELD_PREP(IEEE80211_HT_AMPDU_PARM_FACTOR, af) |
1764 		     FIELD_PREP(IEEE80211_HT_AMPDU_PARM_DENSITY, mm);
1765 	phy->max_ampdu_len = af;
1766 }
1767 
1768 static void
mt7925_mcu_sta_state_v2_tlv(struct mt76_phy * mphy,struct sk_buff * skb,struct ieee80211_link_sta * link_sta,struct ieee80211_vif * vif,u8 rcpi,u8 sta_state)1769 mt7925_mcu_sta_state_v2_tlv(struct mt76_phy *mphy, struct sk_buff *skb,
1770 			    struct ieee80211_link_sta *link_sta,
1771 			    struct ieee80211_vif *vif,
1772 			    u8 rcpi, u8 sta_state)
1773 {
1774 	struct sta_rec_state_v2 {
1775 		__le16 tag;
1776 		__le16 len;
1777 		u8 state;
1778 		u8 rsv[3];
1779 		__le32 flags;
1780 		u8 vht_opmode;
1781 		u8 action;
1782 		u8 rsv2[2];
1783 	} __packed * state;
1784 	struct tlv *tlv;
1785 
1786 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_STATE, sizeof(*state));
1787 	state = (struct sta_rec_state_v2 *)tlv;
1788 	state->state = sta_state;
1789 
1790 	if (link_sta->vht_cap.vht_supported) {
1791 		state->vht_opmode = link_sta->bandwidth;
1792 		state->vht_opmode |= link_sta->rx_nss <<
1793 			IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
1794 	}
1795 }
1796 
1797 static void
mt7925_mcu_sta_rate_ctrl_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta)1798 mt7925_mcu_sta_rate_ctrl_tlv(struct sk_buff *skb,
1799 			     struct ieee80211_vif *vif,
1800 			     struct ieee80211_link_sta *link_sta)
1801 {
1802 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1803 	struct ieee80211_bss_conf *link_conf;
1804 	struct cfg80211_chan_def *chandef;
1805 	struct sta_rec_ra_info *ra_info;
1806 	struct mt792x_bss_conf *mconf;
1807 	enum nl80211_band band;
1808 	struct tlv *tlv;
1809 	u16 supp_rates;
1810 
1811 	link_conf = mt792x_vif_to_bss_conf(vif, link_sta->link_id);
1812 	mconf = mt792x_vif_to_link(mvif, link_sta->link_id);
1813 	chandef = mconf->mt76.ctx ? &mconf->mt76.ctx->def :
1814 				    &link_conf->chanreq.oper;
1815 	band = chandef->chan->band;
1816 
1817 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_RA, sizeof(*ra_info));
1818 	ra_info = (struct sta_rec_ra_info *)tlv;
1819 
1820 	supp_rates = link_sta->supp_rates[band];
1821 	if (band == NL80211_BAND_2GHZ)
1822 		supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates >> 4) |
1823 			     FIELD_PREP(RA_LEGACY_CCK, supp_rates & 0xf);
1824 	else
1825 		supp_rates = FIELD_PREP(RA_LEGACY_OFDM, supp_rates);
1826 
1827 	ra_info->legacy = cpu_to_le16(supp_rates);
1828 
1829 	if (link_sta->ht_cap.ht_supported)
1830 		memcpy(ra_info->rx_mcs_bitmask,
1831 		       link_sta->ht_cap.mcs.rx_mask,
1832 		       HT_MCS_MASK_NUM);
1833 }
1834 
1835 static void
mt7925_mcu_sta_eht_mld_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1836 mt7925_mcu_sta_eht_mld_tlv(struct sk_buff *skb,
1837 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1838 {
1839 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1840 	struct wiphy *wiphy = mvif->phy->mt76->hw->wiphy;
1841 	const struct wiphy_iftype_ext_capab *ext_capa;
1842 	struct sta_rec_eht_mld *eht_mld;
1843 	struct tlv *tlv;
1844 	u16 eml_cap;
1845 
1846 	if (!ieee80211_vif_is_mld(vif))
1847 		return;
1848 
1849 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_EHT_MLD, sizeof(*eht_mld));
1850 	eht_mld = (struct sta_rec_eht_mld *)tlv;
1851 	eht_mld->mld_type = 0xff;
1852 
1853 	ext_capa = cfg80211_get_iftype_ext_capa(wiphy,
1854 						ieee80211_vif_type_p2p(vif));
1855 	if (!ext_capa)
1856 		return;
1857 
1858 	eml_cap = (vif->cfg.eml_cap & (IEEE80211_EML_CAP_EMLSR_SUPP |
1859 				       IEEE80211_EML_CAP_TRANSITION_TIMEOUT)) |
1860 		  (ext_capa->eml_capabilities & (IEEE80211_EML_CAP_EMLSR_PADDING_DELAY |
1861 						IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY));
1862 
1863 	if (eml_cap & IEEE80211_EML_CAP_EMLSR_SUPP) {
1864 		eht_mld->eml_cap[0] = u16_get_bits(eml_cap, GENMASK(7, 0));
1865 		eht_mld->eml_cap[1] = u16_get_bits(eml_cap, GENMASK(15, 8));
1866 	} else {
1867 		eht_mld->str_cap[0] = BIT(1);
1868 	}
1869 }
1870 
1871 static void
mt7925_mcu_sta_mld_tlv(struct sk_buff * skb,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1872 mt7925_mcu_sta_mld_tlv(struct sk_buff *skb,
1873 		       struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1874 {
1875 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1876 	struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv;
1877 	unsigned long valid = mvif->valid_links;
1878 	struct mt792x_bss_conf *mconf;
1879 	struct mt792x_link_sta *mlink;
1880 	struct sta_rec_mld *mld;
1881 	struct tlv *tlv;
1882 	int i, cnt = 0;
1883 
1884 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_MLD, sizeof(*mld));
1885 	mld = (struct sta_rec_mld *)tlv;
1886 	memcpy(mld->mac_addr, sta->addr, ETH_ALEN);
1887 	mld->primary_id = cpu_to_le16(msta->deflink.wcid.idx);
1888 	mld->wlan_id = cpu_to_le16(msta->deflink.wcid.idx);
1889 	mld->link_num = min_t(u8, hweight16(mvif->valid_links), 2);
1890 
1891 	for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
1892 		if (cnt == mld->link_num)
1893 			break;
1894 
1895 		mconf = mt792x_vif_to_link(mvif, i);
1896 		mlink = mt792x_sta_to_link(msta, i);
1897 		mld->link[cnt].wlan_id = cpu_to_le16(mlink->wcid.idx);
1898 		mld->link[cnt++].bss_idx = mconf->mt76.idx;
1899 
1900 		if (mlink != &msta->deflink)
1901 			mld->secondary_id = cpu_to_le16(mlink->wcid.idx);
1902 	}
1903 }
1904 
1905 static void
mt7925_mcu_sta_remove_tlv(struct sk_buff * skb)1906 mt7925_mcu_sta_remove_tlv(struct sk_buff *skb)
1907 {
1908 	struct sta_rec_remove *rem;
1909 	struct tlv *tlv;
1910 
1911 	tlv = mt76_connac_mcu_add_tlv(skb, 0x25, sizeof(*rem));
1912 	rem = (struct sta_rec_remove *)tlv;
1913 	rem->action = 0;
1914 }
1915 
1916 static int
mt7925_mcu_sta_cmd(struct mt76_phy * phy,struct mt76_sta_cmd_info * info)1917 mt7925_mcu_sta_cmd(struct mt76_phy *phy,
1918 		   struct mt76_sta_cmd_info *info)
1919 {
1920 	struct mt792x_vif *mvif = (struct mt792x_vif *)info->vif->drv_priv;
1921 	struct mt76_dev *dev = phy->dev;
1922 	struct mt792x_bss_conf *mconf;
1923 	struct sk_buff *skb;
1924 	int conn_state;
1925 
1926 	mconf = mt792x_vif_to_link(mvif, info->wcid->link_id);
1927 
1928 	skb = __mt76_connac_mcu_alloc_sta_req(dev, &mconf->mt76, info->wcid,
1929 					      MT7925_STA_UPDATE_MAX_SIZE);
1930 	if (IS_ERR(skb))
1931 		return PTR_ERR(skb);
1932 
1933 	conn_state = info->enable ? CONN_STATE_PORT_SECURE :
1934 				    CONN_STATE_DISCONNECT;
1935 
1936 	if (info->enable && info->link_sta) {
1937 		mt76_connac_mcu_sta_basic_tlv(dev, skb, info->link_conf,
1938 					      info->link_sta,
1939 					      conn_state, info->newly);
1940 		mt7925_mcu_sta_phy_tlv(skb, info->vif, info->link_sta);
1941 		mt7925_mcu_sta_ht_tlv(skb, info->link_sta);
1942 		mt7925_mcu_sta_vht_tlv(skb, info->link_sta);
1943 		mt76_connac_mcu_sta_uapsd(skb, info->vif, info->link_sta->sta);
1944 		mt7925_mcu_sta_amsdu_tlv(skb, info->vif, info->link_sta);
1945 		mt7925_mcu_sta_he_tlv(skb, info->link_sta);
1946 		mt7925_mcu_sta_he_6g_tlv(skb, info->link_sta);
1947 		mt7925_mcu_sta_eht_tlv(skb, info->link_sta);
1948 		mt7925_mcu_sta_rate_ctrl_tlv(skb, info->vif,
1949 					     info->link_sta);
1950 		mt7925_mcu_sta_state_v2_tlv(phy, skb, info->link_sta,
1951 					    info->vif, info->rcpi,
1952 					    info->state);
1953 
1954 		if (info->state != MT76_STA_INFO_STATE_NONE) {
1955 			mt7925_mcu_sta_mld_tlv(skb, info->vif, info->link_sta->sta);
1956 			mt7925_mcu_sta_eht_mld_tlv(skb, info->vif, info->link_sta->sta);
1957 		}
1958 	}
1959 
1960 	if (!info->enable) {
1961 		mt7925_mcu_sta_remove_tlv(skb);
1962 		mt76_connac_mcu_add_tlv(skb, STA_REC_MLD_OFF,
1963 					sizeof(struct tlv));
1964 	} else {
1965 		mt7925_mcu_sta_hdr_trans_tlv(skb, info->vif, info->link_sta);
1966 	}
1967 
1968 	return mt76_mcu_skb_send_msg(dev, skb, info->cmd, true);
1969 }
1970 
mt7925_mcu_sta_update(struct mt792x_dev * dev,struct ieee80211_link_sta * link_sta,struct ieee80211_vif * vif,bool enable,enum mt76_sta_info_state state)1971 int mt7925_mcu_sta_update(struct mt792x_dev *dev,
1972 			  struct ieee80211_link_sta *link_sta,
1973 			  struct ieee80211_vif *vif, bool enable,
1974 			  enum mt76_sta_info_state state)
1975 {
1976 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
1977 	int rssi = -ewma_rssi_read(&mvif->bss_conf.rssi);
1978 	struct mt76_sta_cmd_info info = {
1979 		.link_sta = link_sta,
1980 		.vif = vif,
1981 		.link_conf = &vif->bss_conf,
1982 		.enable = enable,
1983 		.cmd = MCU_UNI_CMD(STA_REC_UPDATE),
1984 		.state = state,
1985 		.offload_fw = true,
1986 		.rcpi = to_rcpi(rssi),
1987 	};
1988 	struct mt792x_sta *msta;
1989 	struct mt792x_link_sta *mlink;
1990 
1991 	if (link_sta) {
1992 		msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
1993 		mlink = mt792x_sta_to_link(msta, link_sta->link_id);
1994 	}
1995 	info.wcid = link_sta ? &mlink->wcid : &mvif->sta.deflink.wcid;
1996 	info.newly = state != MT76_STA_INFO_STATE_ASSOC;
1997 
1998 	return mt7925_mcu_sta_cmd(&dev->mphy, &info);
1999 }
2000 
mt7925_mcu_set_beacon_filter(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)2001 int mt7925_mcu_set_beacon_filter(struct mt792x_dev *dev,
2002 				 struct ieee80211_vif *vif,
2003 				 bool enable)
2004 {
2005 #define MT7925_FIF_BIT_CLR		BIT(1)
2006 #define MT7925_FIF_BIT_SET		BIT(0)
2007 	int err = 0;
2008 
2009 	if (enable) {
2010 		err = mt7925_mcu_uni_bss_bcnft(dev, &vif->bss_conf, true);
2011 		if (err < 0)
2012 			return err;
2013 
2014 		return mt7925_mcu_set_rxfilter(dev, 0,
2015 					       MT7925_FIF_BIT_SET,
2016 					       MT_WF_RFCR_DROP_OTHER_BEACON);
2017 	}
2018 
2019 	err = mt7925_mcu_set_bss_pm(dev, &vif->bss_conf, false);
2020 	if (err < 0)
2021 		return err;
2022 
2023 	return mt7925_mcu_set_rxfilter(dev, 0,
2024 				       MT7925_FIF_BIT_CLR,
2025 				       MT_WF_RFCR_DROP_OTHER_BEACON);
2026 }
2027 
mt7925_get_txpwr_info(struct mt792x_dev * dev,u8 band_idx,struct mt7925_txpwr * txpwr)2028 int mt7925_get_txpwr_info(struct mt792x_dev *dev, u8 band_idx, struct mt7925_txpwr *txpwr)
2029 {
2030 #define TX_POWER_SHOW_INFO 0x7
2031 #define TXPOWER_ALL_RATE_POWER_INFO 0x2
2032 	struct mt7925_txpwr_event *event;
2033 	struct mt7925_txpwr_req req = {
2034 		.tag = cpu_to_le16(TX_POWER_SHOW_INFO),
2035 		.len = cpu_to_le16(sizeof(req) - 4),
2036 		.catg = TXPOWER_ALL_RATE_POWER_INFO,
2037 		.band_idx = band_idx,
2038 	};
2039 	struct sk_buff *skb;
2040 	int ret;
2041 
2042 	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_UNI_CMD(TXPOWER),
2043 					&req, sizeof(req), true, &skb);
2044 	if (ret)
2045 		return ret;
2046 
2047 	event = (struct mt7925_txpwr_event *)skb->data;
2048 	memcpy(txpwr, &event->txpwr, sizeof(event->txpwr));
2049 
2050 	dev_kfree_skb(skb);
2051 
2052 	return 0;
2053 }
2054 
mt7925_mcu_set_sniffer(struct mt792x_dev * dev,struct ieee80211_vif * vif,bool enable)2055 int mt7925_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
2056 			   bool enable)
2057 {
2058 	struct {
2059 		struct {
2060 			u8 band_idx;
2061 			u8 pad[3];
2062 		} __packed hdr;
2063 		struct sniffer_enable_tlv {
2064 			__le16 tag;
2065 			__le16 len;
2066 			u8 enable;
2067 			u8 pad[3];
2068 		} __packed enable;
2069 	} __packed req = {
2070 		.hdr = {
2071 			.band_idx = 0,
2072 		},
2073 		.enable = {
2074 			.tag = cpu_to_le16(UNI_SNIFFER_ENABLE),
2075 			.len = cpu_to_le16(sizeof(struct sniffer_enable_tlv)),
2076 			.enable = enable,
2077 		},
2078 	};
2079 
2080 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(SNIFFER), &req, sizeof(req),
2081 				 true);
2082 }
2083 
mt7925_mcu_config_sniffer(struct mt792x_vif * vif,struct ieee80211_chanctx_conf * ctx)2084 int mt7925_mcu_config_sniffer(struct mt792x_vif *vif,
2085 			      struct ieee80211_chanctx_conf *ctx)
2086 {
2087 	struct mt76_phy *mphy = vif->phy->mt76;
2088 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def : &mphy->chandef;
2089 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
2090 
2091 	static const u8 ch_band[] = {
2092 		[NL80211_BAND_2GHZ] = 1,
2093 		[NL80211_BAND_5GHZ] = 2,
2094 		[NL80211_BAND_6GHZ] = 3,
2095 	};
2096 	static const u8 ch_width[] = {
2097 		[NL80211_CHAN_WIDTH_20_NOHT] = 0,
2098 		[NL80211_CHAN_WIDTH_20] = 0,
2099 		[NL80211_CHAN_WIDTH_40] = 0,
2100 		[NL80211_CHAN_WIDTH_80] = 1,
2101 		[NL80211_CHAN_WIDTH_160] = 2,
2102 		[NL80211_CHAN_WIDTH_80P80] = 3,
2103 		[NL80211_CHAN_WIDTH_5] = 4,
2104 		[NL80211_CHAN_WIDTH_10] = 5,
2105 		[NL80211_CHAN_WIDTH_320] = 6,
2106 	};
2107 
2108 	struct {
2109 		struct {
2110 			u8 band_idx;
2111 			u8 pad[3];
2112 		} __packed hdr;
2113 		struct config_tlv {
2114 			__le16 tag;
2115 			__le16 len;
2116 			u16 aid;
2117 			u8 ch_band;
2118 			u8 bw;
2119 			u8 control_ch;
2120 			u8 sco;
2121 			u8 center_ch;
2122 			u8 center_ch2;
2123 			u8 drop_err;
2124 			u8 pad[3];
2125 		} __packed tlv;
2126 	} __packed req = {
2127 		.hdr = {
2128 			.band_idx = 0,
2129 		},
2130 		.tlv = {
2131 			.tag = cpu_to_le16(UNI_SNIFFER_CONFIG),
2132 			.len = cpu_to_le16(sizeof(req.tlv)),
2133 			.control_ch = chandef->chan->hw_value,
2134 			.center_ch = ieee80211_frequency_to_channel(freq1),
2135 			.drop_err = 1,
2136 		},
2137 	};
2138 
2139 	if (chandef->chan->band < ARRAY_SIZE(ch_band))
2140 		req.tlv.ch_band = ch_band[chandef->chan->band];
2141 	if (chandef->width < ARRAY_SIZE(ch_width))
2142 		req.tlv.bw = ch_width[chandef->width];
2143 
2144 	if (freq2)
2145 		req.tlv.center_ch2 = ieee80211_frequency_to_channel(freq2);
2146 
2147 	if (req.tlv.control_ch < req.tlv.center_ch)
2148 		req.tlv.sco = 1; /* SCA */
2149 	else if (req.tlv.control_ch > req.tlv.center_ch)
2150 		req.tlv.sco = 3; /* SCB */
2151 
2152 	return mt76_mcu_send_msg(mphy->dev, MCU_UNI_CMD(SNIFFER),
2153 				 &req, sizeof(req), true);
2154 }
2155 
2156 int
mt7925_mcu_uni_add_beacon_offload(struct mt792x_dev * dev,struct ieee80211_hw * hw,struct ieee80211_vif * vif,bool enable)2157 mt7925_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
2158 				  struct ieee80211_hw *hw,
2159 				  struct ieee80211_vif *vif,
2160 				  bool enable)
2161 {
2162 	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
2163 	struct ieee80211_mutable_offsets offs;
2164 	struct {
2165 		struct req_hdr {
2166 			u8 bss_idx;
2167 			u8 pad[3];
2168 		} __packed hdr;
2169 		struct bcn_content_tlv {
2170 			__le16 tag;
2171 			__le16 len;
2172 			__le16 tim_ie_pos;
2173 			__le16 csa_ie_pos;
2174 			__le16 bcc_ie_pos;
2175 			/* 0: disable beacon offload
2176 			 * 1: enable beacon offload
2177 			 * 2: update probe respond offload
2178 			 */
2179 			u8 enable;
2180 			/* 0: legacy format (TXD + payload)
2181 			 * 1: only cap field IE
2182 			 */
2183 			u8 type;
2184 			__le16 pkt_len;
2185 			u8 pkt[512];
2186 		} __packed beacon_tlv;
2187 	} req = {
2188 		.hdr = {
2189 			.bss_idx = mvif->bss_conf.mt76.idx,
2190 		},
2191 		.beacon_tlv = {
2192 			.tag = cpu_to_le16(UNI_BSS_INFO_BCN_CONTENT),
2193 			.len = cpu_to_le16(sizeof(struct bcn_content_tlv)),
2194 			.enable = enable,
2195 			.type = 1,
2196 		},
2197 	};
2198 	struct sk_buff *skb;
2199 	u8 cap_offs;
2200 
2201 	/* support enable/update process only
2202 	 * disable flow would be handled in bss stop handler automatically
2203 	 */
2204 	if (!enable)
2205 		return -EOPNOTSUPP;
2206 
2207 	skb = ieee80211_beacon_get_template(mt76_hw(dev), vif, &offs, 0);
2208 	if (!skb)
2209 		return -EINVAL;
2210 
2211 	cap_offs = offsetof(struct ieee80211_mgmt, u.beacon.capab_info);
2212 	if (!skb_pull(skb, cap_offs)) {
2213 		dev_err(dev->mt76.dev, "beacon format err\n");
2214 		dev_kfree_skb(skb);
2215 		return -EINVAL;
2216 	}
2217 
2218 	if (skb->len > 512) {
2219 		dev_err(dev->mt76.dev, "beacon size limit exceed\n");
2220 		dev_kfree_skb(skb);
2221 		return -EINVAL;
2222 	}
2223 
2224 	memcpy(req.beacon_tlv.pkt, skb->data, skb->len);
2225 	req.beacon_tlv.pkt_len = cpu_to_le16(skb->len);
2226 	offs.tim_offset -= cap_offs;
2227 	req.beacon_tlv.tim_ie_pos = cpu_to_le16(offs.tim_offset);
2228 
2229 	if (offs.cntdwn_counter_offs[0]) {
2230 		u16 csa_offs;
2231 
2232 		csa_offs = offs.cntdwn_counter_offs[0] - cap_offs - 4;
2233 		req.beacon_tlv.csa_ie_pos = cpu_to_le16(csa_offs);
2234 	}
2235 	dev_kfree_skb(skb);
2236 
2237 	return mt76_mcu_send_msg(&dev->mt76, MCU_UNI_CMD(BSS_INFO_UPDATE),
2238 				 &req, sizeof(req), true);
2239 }
2240 
2241 static
mt7925_mcu_bss_rlm_tlv(struct sk_buff * skb,struct mt76_phy * phy,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2242 void mt7925_mcu_bss_rlm_tlv(struct sk_buff *skb, struct mt76_phy *phy,
2243 			    struct ieee80211_bss_conf *link_conf,
2244 			    struct ieee80211_chanctx_conf *ctx)
2245 {
2246 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2247 						  &link_conf->chanreq.oper;
2248 	int freq1 = chandef->center_freq1, freq2 = chandef->center_freq2;
2249 	enum nl80211_band band = chandef->chan->band;
2250 	struct bss_rlm_tlv *req;
2251 	struct tlv *tlv;
2252 
2253 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_RLM, sizeof(*req));
2254 	req = (struct bss_rlm_tlv *)tlv;
2255 	req->control_channel = chandef->chan->hw_value;
2256 	req->center_chan = ieee80211_frequency_to_channel(freq1);
2257 	req->center_chan2 = 0;
2258 	req->tx_streams = hweight8(phy->antenna_mask);
2259 	req->ht_op_info = 4; /* set HT 40M allowed */
2260 	req->rx_streams = hweight8(phy->antenna_mask);
2261 	req->center_chan2 = 0;
2262 	req->sco = 0;
2263 	req->band = 1;
2264 
2265 	switch (band) {
2266 	case NL80211_BAND_2GHZ:
2267 		req->band = 1;
2268 		break;
2269 	case NL80211_BAND_5GHZ:
2270 		req->band = 2;
2271 		break;
2272 	case NL80211_BAND_6GHZ:
2273 		req->band = 3;
2274 		break;
2275 	default:
2276 		break;
2277 	}
2278 
2279 	switch (chandef->width) {
2280 	case NL80211_CHAN_WIDTH_40:
2281 		req->bw = CMD_CBW_40MHZ;
2282 		break;
2283 	case NL80211_CHAN_WIDTH_80:
2284 		req->bw = CMD_CBW_80MHZ;
2285 		break;
2286 	case NL80211_CHAN_WIDTH_80P80:
2287 		req->bw = CMD_CBW_8080MHZ;
2288 		req->center_chan2 = ieee80211_frequency_to_channel(freq2);
2289 		break;
2290 	case NL80211_CHAN_WIDTH_160:
2291 		req->bw = CMD_CBW_160MHZ;
2292 		break;
2293 	case NL80211_CHAN_WIDTH_5:
2294 		req->bw = CMD_CBW_5MHZ;
2295 		break;
2296 	case NL80211_CHAN_WIDTH_10:
2297 		req->bw = CMD_CBW_10MHZ;
2298 		break;
2299 	case NL80211_CHAN_WIDTH_20_NOHT:
2300 	case NL80211_CHAN_WIDTH_20:
2301 	default:
2302 		req->bw = CMD_CBW_20MHZ;
2303 		req->ht_op_info = 0;
2304 		break;
2305 	}
2306 
2307 	if (req->control_channel < req->center_chan)
2308 		req->sco = 1; /* SCA */
2309 	else if (req->control_channel > req->center_chan)
2310 		req->sco = 3; /* SCB */
2311 }
2312 
2313 static struct sk_buff *
__mt7925_mcu_alloc_bss_req(struct mt76_dev * dev,struct mt76_vif_link * mvif,int len)2314 __mt7925_mcu_alloc_bss_req(struct mt76_dev *dev, struct mt76_vif_link *mvif, int len)
2315 {
2316 	struct bss_req_hdr hdr = {
2317 		.bss_idx = mvif->idx,
2318 	};
2319 	struct sk_buff *skb;
2320 
2321 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
2322 	if (!skb)
2323 		return ERR_PTR(-ENOMEM);
2324 
2325 	skb_put_data(skb, &hdr, sizeof(hdr));
2326 
2327 	return skb;
2328 }
2329 
2330 static
mt7925_mcu_bss_eht_tlv(struct sk_buff * skb,struct mt76_phy * phy,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2331 void mt7925_mcu_bss_eht_tlv(struct sk_buff *skb, struct mt76_phy *phy,
2332 			    struct ieee80211_bss_conf *link_conf,
2333 			    struct ieee80211_chanctx_conf *ctx)
2334 {
2335 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2336 						  &link_conf->chanreq.oper;
2337 
2338 	struct bss_eht_tlv *req;
2339 	struct tlv *tlv;
2340 
2341 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_EHT, sizeof(*req));
2342 	req = (struct bss_eht_tlv *)tlv;
2343 	req->is_eth_dscb_present = chandef->punctured ? 1 : 0;
2344 	req->eht_dis_sub_chan_bitmap = cpu_to_le16(chandef->punctured);
2345 }
2346 
mt7925_mcu_set_eht_pp(struct mt76_phy * phy,struct mt76_vif_link * mvif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2347 int mt7925_mcu_set_eht_pp(struct mt76_phy *phy, struct mt76_vif_link *mvif,
2348 			  struct ieee80211_bss_conf *link_conf,
2349 			  struct ieee80211_chanctx_conf *ctx)
2350 {
2351 	struct sk_buff *skb;
2352 
2353 	skb = __mt7925_mcu_alloc_bss_req(phy->dev, mvif,
2354 					 MT7925_BSS_UPDATE_MAX_SIZE);
2355 	if (IS_ERR(skb))
2356 		return PTR_ERR(skb);
2357 
2358 	mt7925_mcu_bss_eht_tlv(skb, phy, link_conf, ctx);
2359 
2360 	return mt76_mcu_skb_send_msg(phy->dev, skb,
2361 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2362 }
2363 
mt7925_mcu_set_chctx(struct mt76_phy * phy,struct mt76_vif_link * mvif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)2364 int mt7925_mcu_set_chctx(struct mt76_phy *phy, struct mt76_vif_link *mvif,
2365 			 struct ieee80211_bss_conf *link_conf,
2366 			 struct ieee80211_chanctx_conf *ctx)
2367 {
2368 	struct sk_buff *skb;
2369 
2370 	skb = __mt7925_mcu_alloc_bss_req(phy->dev, mvif,
2371 					 MT7925_BSS_UPDATE_MAX_SIZE);
2372 	if (IS_ERR(skb))
2373 		return PTR_ERR(skb);
2374 
2375 	mt7925_mcu_bss_rlm_tlv(skb, phy, link_conf, ctx);
2376 
2377 	return mt76_mcu_skb_send_msg(phy->dev, skb,
2378 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2379 }
2380 
2381 static u8
mt7925_get_phy_mode_ext(struct mt76_phy * phy,struct ieee80211_vif * vif,enum nl80211_band band,struct ieee80211_link_sta * link_sta)2382 mt7925_get_phy_mode_ext(struct mt76_phy *phy, struct ieee80211_vif *vif,
2383 			enum nl80211_band band,
2384 			struct ieee80211_link_sta *link_sta)
2385 {
2386 	struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2387 	const struct ieee80211_sta_eht_cap *eht_cap;
2388 	__le16 capa = 0;
2389 	u8 mode = 0;
2390 
2391 	if (link_sta) {
2392 		he_6ghz_capa = &link_sta->he_6ghz_capa;
2393 		eht_cap = &link_sta->eht_cap;
2394 	} else {
2395 		struct ieee80211_supported_band *sband;
2396 
2397 		sband = phy->hw->wiphy->bands[band];
2398 		capa = ieee80211_get_he_6ghz_capa(sband, vif->type);
2399 		he_6ghz_capa = (struct ieee80211_he_6ghz_capa *)&capa;
2400 
2401 		eht_cap = ieee80211_get_eht_iftype_cap(sband, vif->type);
2402 	}
2403 
2404 	switch (band) {
2405 	case NL80211_BAND_2GHZ:
2406 		if (eht_cap && eht_cap->has_eht)
2407 			mode |= PHY_MODE_BE_24G;
2408 		break;
2409 	case NL80211_BAND_5GHZ:
2410 		if (eht_cap && eht_cap->has_eht)
2411 			mode |= PHY_MODE_BE_5G;
2412 		break;
2413 	case NL80211_BAND_6GHZ:
2414 		if (he_6ghz_capa && he_6ghz_capa->capa)
2415 			mode |= PHY_MODE_AX_6G;
2416 
2417 		if (eht_cap && eht_cap->has_eht)
2418 			mode |= PHY_MODE_BE_6G;
2419 		break;
2420 	default:
2421 		break;
2422 	}
2423 
2424 	return mode;
2425 }
2426 
2427 static void
mt7925_mcu_bss_basic_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,struct ieee80211_link_sta * link_sta,struct ieee80211_chanctx_conf * ctx,struct mt76_phy * phy,u16 wlan_idx,bool enable)2428 mt7925_mcu_bss_basic_tlv(struct sk_buff *skb,
2429 			 struct ieee80211_bss_conf *link_conf,
2430 			 struct ieee80211_link_sta *link_sta,
2431 			 struct ieee80211_chanctx_conf *ctx,
2432 			 struct mt76_phy *phy, u16 wlan_idx,
2433 			 bool enable)
2434 {
2435 	struct ieee80211_vif *vif = link_conf->vif;
2436 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2437 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2438 						  &link_conf->chanreq.oper;
2439 	enum nl80211_band band = chandef->chan->band;
2440 	struct mt76_connac_bss_basic_tlv *basic_req;
2441 	struct mt792x_link_sta *mlink;
2442 	struct tlv *tlv;
2443 	int conn_type;
2444 	u8 idx;
2445 
2446 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_BASIC, sizeof(*basic_req));
2447 	basic_req = (struct mt76_connac_bss_basic_tlv *)tlv;
2448 
2449 	idx = mconf->mt76.omac_idx > EXT_BSSID_START ? HW_BSSID_0 :
2450 						      mconf->mt76.omac_idx;
2451 	basic_req->hw_bss_idx = idx;
2452 
2453 	basic_req->phymode_ext = mt7925_get_phy_mode_ext(phy, vif, band,
2454 							 link_sta);
2455 
2456 	if (band == NL80211_BAND_2GHZ)
2457 		basic_req->nonht_basic_phy = cpu_to_le16(PHY_TYPE_ERP_INDEX);
2458 	else
2459 		basic_req->nonht_basic_phy = cpu_to_le16(PHY_TYPE_OFDM_INDEX);
2460 
2461 	memcpy(basic_req->bssid, link_conf->bssid, ETH_ALEN);
2462 	basic_req->phymode = mt76_connac_get_phy_mode(phy, vif, band, link_sta);
2463 	basic_req->bcn_interval = cpu_to_le16(link_conf->beacon_int);
2464 	basic_req->dtim_period = link_conf->dtim_period;
2465 	basic_req->bmc_tx_wlan_idx = cpu_to_le16(wlan_idx);
2466 	basic_req->link_idx = mconf->mt76.idx;
2467 
2468 	if (link_sta) {
2469 		struct mt792x_sta *msta;
2470 
2471 		msta = (struct mt792x_sta *)link_sta->sta->drv_priv;
2472 		mlink = mt792x_sta_to_link(msta, link_sta->link_id);
2473 
2474 	} else {
2475 		mlink = &mconf->vif->sta.deflink;
2476 	}
2477 
2478 	basic_req->sta_idx = cpu_to_le16(mlink->wcid.idx);
2479 	basic_req->omac_idx = mconf->mt76.omac_idx;
2480 	basic_req->band_idx = mconf->mt76.band_idx;
2481 	basic_req->wmm_idx = mconf->mt76.wmm_idx;
2482 	basic_req->conn_state = !enable;
2483 
2484 	switch (vif->type) {
2485 	case NL80211_IFTYPE_MESH_POINT:
2486 	case NL80211_IFTYPE_AP:
2487 		if (vif->p2p)
2488 			conn_type = CONNECTION_P2P_GO;
2489 		else
2490 			conn_type = CONNECTION_INFRA_AP;
2491 		basic_req->conn_type = cpu_to_le32(conn_type);
2492 		basic_req->active = enable;
2493 		break;
2494 	case NL80211_IFTYPE_STATION:
2495 		if (vif->p2p)
2496 			conn_type = CONNECTION_P2P_GC;
2497 		else
2498 			conn_type = CONNECTION_INFRA_STA;
2499 		basic_req->conn_type = cpu_to_le32(conn_type);
2500 		basic_req->active = true;
2501 		break;
2502 	case NL80211_IFTYPE_ADHOC:
2503 		basic_req->conn_type = cpu_to_le32(CONNECTION_IBSS_ADHOC);
2504 		basic_req->active = true;
2505 		break;
2506 	default:
2507 		WARN_ON(1);
2508 		break;
2509 	}
2510 }
2511 
2512 static void
mt7925_mcu_bss_sec_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2513 mt7925_mcu_bss_sec_tlv(struct sk_buff *skb,
2514 		       struct ieee80211_bss_conf *link_conf)
2515 {
2516 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2517 	struct mt76_vif_link *mvif = &mconf->mt76;
2518 	struct bss_sec_tlv {
2519 		__le16 tag;
2520 		__le16 len;
2521 		u8 mode;
2522 		u8 status;
2523 		u8 cipher;
2524 		u8 __rsv;
2525 	} __packed * sec;
2526 	struct tlv *tlv;
2527 
2528 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_SEC, sizeof(*sec));
2529 	sec = (struct bss_sec_tlv *)tlv;
2530 
2531 	switch (mvif->cipher) {
2532 	case CONNAC3_CIPHER_GCMP_256:
2533 	case CONNAC3_CIPHER_GCMP:
2534 		sec->mode = MODE_WPA3_SAE;
2535 		sec->status = 8;
2536 		break;
2537 	case CONNAC3_CIPHER_AES_CCMP:
2538 		sec->mode = MODE_WPA2_PSK;
2539 		sec->status = 6;
2540 		break;
2541 	case CONNAC3_CIPHER_TKIP:
2542 		sec->mode = MODE_WPA2_PSK;
2543 		sec->status = 4;
2544 		break;
2545 	case CONNAC3_CIPHER_WEP104:
2546 	case CONNAC3_CIPHER_WEP40:
2547 		sec->mode = MODE_SHARED;
2548 		sec->status = 0;
2549 		break;
2550 	default:
2551 		sec->mode = MODE_OPEN;
2552 		sec->status = 1;
2553 		break;
2554 	}
2555 
2556 	sec->cipher = mvif->cipher;
2557 }
2558 
2559 static void
mt7925_mcu_bss_bmc_tlv(struct sk_buff * skb,struct mt792x_phy * phy,struct ieee80211_chanctx_conf * ctx,struct ieee80211_bss_conf * link_conf)2560 mt7925_mcu_bss_bmc_tlv(struct sk_buff *skb, struct mt792x_phy *phy,
2561 		       struct ieee80211_chanctx_conf *ctx,
2562 		       struct ieee80211_bss_conf *link_conf)
2563 {
2564 	struct cfg80211_chan_def *chandef = ctx ? &ctx->def :
2565 						  &link_conf->chanreq.oper;
2566 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2567 	enum nl80211_band band = chandef->chan->band;
2568 	struct mt76_vif_link *mvif = &mconf->mt76;
2569 	struct bss_rate_tlv *bmc;
2570 	struct tlv *tlv;
2571 	u8 idx = mvif->mcast_rates_idx ?
2572 		 mvif->mcast_rates_idx : mvif->basic_rates_idx;
2573 
2574 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_RATE, sizeof(*bmc));
2575 
2576 	bmc = (struct bss_rate_tlv *)tlv;
2577 
2578 	if (band == NL80211_BAND_2GHZ)
2579 		bmc->basic_rate = cpu_to_le16(HR_DSSS_ERP_BASIC_RATE);
2580 	else
2581 		bmc->basic_rate = cpu_to_le16(OFDM_BASIC_RATE);
2582 
2583 	bmc->short_preamble = (band == NL80211_BAND_2GHZ);
2584 	bmc->bc_fixed_rate = idx;
2585 	bmc->mc_fixed_rate = idx;
2586 }
2587 
2588 static void
mt7925_mcu_bss_mld_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2589 mt7925_mcu_bss_mld_tlv(struct sk_buff *skb,
2590 		       struct ieee80211_bss_conf *link_conf)
2591 {
2592 	struct ieee80211_vif *vif = link_conf->vif;
2593 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2594 	struct mt792x_vif *mvif = (struct mt792x_vif *)link_conf->vif->drv_priv;
2595 	struct mt792x_phy *phy = mvif->phy;
2596 	struct bss_mld_tlv *mld;
2597 	struct tlv *tlv;
2598 	bool is_mld;
2599 
2600 	is_mld = ieee80211_vif_is_mld(link_conf->vif) ||
2601 		 (hweight16(mvif->valid_links) > 1);
2602 
2603 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_MLD, sizeof(*mld));
2604 	mld = (struct bss_mld_tlv *)tlv;
2605 
2606 	mld->link_id = is_mld ? link_conf->link_id : 0xff;
2607 	/* apply the index of the primary link */
2608 	mld->group_mld_id = is_mld ? mvif->bss_conf.mt76.idx : 0xff;
2609 	mld->own_mld_id = mconf->mt76.idx + 32;
2610 	mld->remap_idx = 0xff;
2611 
2612 	if (phy->chip_cap & MT792x_CHIP_CAP_MLO_EML_EN) {
2613 		mld->eml_enable = !!(link_conf->vif->cfg.eml_cap &
2614 				     IEEE80211_EML_CAP_EMLSR_SUPP);
2615 	} else {
2616 		mld->eml_enable = 0;
2617 	}
2618 
2619 	memcpy(mld->mac_addr, vif->addr, ETH_ALEN);
2620 }
2621 
2622 static void
mt7925_mcu_bss_qos_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2623 mt7925_mcu_bss_qos_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf)
2624 {
2625 	struct mt76_connac_bss_qos_tlv *qos;
2626 	struct tlv *tlv;
2627 
2628 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_QBSS, sizeof(*qos));
2629 	qos = (struct mt76_connac_bss_qos_tlv *)tlv;
2630 	qos->qos = link_conf->qos;
2631 }
2632 
2633 static void
mt7925_mcu_bss_he_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,struct mt792x_phy * phy)2634 mt7925_mcu_bss_he_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf,
2635 		      struct mt792x_phy *phy)
2636 {
2637 #define DEFAULT_HE_PE_DURATION		4
2638 #define DEFAULT_HE_DURATION_RTS_THRES	1023
2639 	const struct ieee80211_sta_he_cap *cap;
2640 	struct bss_info_uni_he *he;
2641 	struct tlv *tlv;
2642 
2643 	cap = mt76_connac_get_he_phy_cap(phy->mt76, link_conf->vif);
2644 
2645 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_HE_BASIC, sizeof(*he));
2646 
2647 	he = (struct bss_info_uni_he *)tlv;
2648 	he->he_pe_duration = link_conf->htc_trig_based_pkt_ext;
2649 	if (!he->he_pe_duration)
2650 		he->he_pe_duration = DEFAULT_HE_PE_DURATION;
2651 
2652 	he->he_rts_thres = cpu_to_le16(link_conf->frame_time_rts_th);
2653 	if (!he->he_rts_thres)
2654 		he->he_rts_thres = cpu_to_le16(DEFAULT_HE_DURATION_RTS_THRES);
2655 
2656 	he->max_nss_mcs[CMD_HE_MCS_BW80] = cap->he_mcs_nss_supp.tx_mcs_80;
2657 	he->max_nss_mcs[CMD_HE_MCS_BW160] = cap->he_mcs_nss_supp.tx_mcs_160;
2658 	he->max_nss_mcs[CMD_HE_MCS_BW8080] = cap->he_mcs_nss_supp.tx_mcs_80p80;
2659 }
2660 
2661 static void
mt7925_mcu_bss_color_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf,bool enable)2662 mt7925_mcu_bss_color_tlv(struct sk_buff *skb, struct ieee80211_bss_conf *link_conf,
2663 			 bool enable)
2664 {
2665 	struct bss_info_uni_bss_color *color;
2666 	struct tlv *tlv;
2667 
2668 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_BSS_COLOR, sizeof(*color));
2669 	color = (struct bss_info_uni_bss_color *)tlv;
2670 
2671 	color->enable = enable ?
2672 		link_conf->he_bss_color.enabled : 0;
2673 	color->bss_color = enable ?
2674 		link_conf->he_bss_color.color : 0;
2675 }
2676 
2677 static void
mt7925_mcu_bss_ifs_tlv(struct sk_buff * skb,struct ieee80211_bss_conf * link_conf)2678 mt7925_mcu_bss_ifs_tlv(struct sk_buff *skb,
2679 		       struct ieee80211_bss_conf *link_conf)
2680 {
2681 	struct mt792x_vif *mvif = (struct mt792x_vif *)link_conf->vif->drv_priv;
2682 	struct mt792x_phy *phy = mvif->phy;
2683 	struct bss_ifs_time_tlv *ifs_time;
2684 	struct tlv *tlv;
2685 
2686 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_BSS_INFO_IFS_TIME, sizeof(*ifs_time));
2687 	ifs_time = (struct bss_ifs_time_tlv *)tlv;
2688 	ifs_time->slot_valid = true;
2689 	ifs_time->slot_time = cpu_to_le16(phy->slottime);
2690 }
2691 
mt7925_mcu_set_timing(struct mt792x_phy * phy,struct ieee80211_bss_conf * link_conf)2692 int mt7925_mcu_set_timing(struct mt792x_phy *phy,
2693 			  struct ieee80211_bss_conf *link_conf)
2694 {
2695 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2696 	struct mt792x_dev *dev = phy->dev;
2697 	struct sk_buff *skb;
2698 
2699 	skb = __mt7925_mcu_alloc_bss_req(&dev->mt76, &mconf->mt76,
2700 					 MT7925_BSS_UPDATE_MAX_SIZE);
2701 	if (IS_ERR(skb))
2702 		return PTR_ERR(skb);
2703 
2704 	mt7925_mcu_bss_ifs_tlv(skb, link_conf);
2705 
2706 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
2707 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2708 }
2709 
mt7925_mcu_del_dev(struct mt76_dev * mdev,struct ieee80211_vif * vif)2710 void mt7925_mcu_del_dev(struct mt76_dev *mdev,
2711 			struct ieee80211_vif *vif)
2712 {
2713 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2714 	struct {
2715 		struct {
2716 			u8 omac_idx;
2717 			u8 band_idx;
2718 			__le16 pad;
2719 		} __packed hdr;
2720 		struct req_tlv {
2721 			__le16 tag;
2722 			__le16 len;
2723 			u8 active;
2724 			u8 link_idx; /* hw link idx */
2725 			u8 omac_addr[ETH_ALEN];
2726 		} __packed tlv;
2727 	} dev_req = {
2728 		.tlv = {
2729 			.tag = cpu_to_le16(DEV_INFO_ACTIVE),
2730 			.len = cpu_to_le16(sizeof(struct req_tlv)),
2731 			.active = true,
2732 		},
2733 	};
2734 	struct {
2735 		struct {
2736 			u8 bss_idx;
2737 			u8 pad[3];
2738 		} __packed hdr;
2739 		struct mt76_connac_bss_basic_tlv basic;
2740 	} basic_req = {
2741 		.basic = {
2742 			.tag = cpu_to_le16(UNI_BSS_INFO_BASIC),
2743 			.len = cpu_to_le16(sizeof(struct mt76_connac_bss_basic_tlv)),
2744 			.active = true,
2745 			.conn_state = 1,
2746 		},
2747 	};
2748 
2749 	dev_req.hdr.omac_idx = mvif->omac_idx;
2750 	dev_req.hdr.band_idx = mvif->band_idx;
2751 
2752 	basic_req.hdr.bss_idx = mvif->idx;
2753 	basic_req.basic.omac_idx = mvif->omac_idx;
2754 	basic_req.basic.band_idx = mvif->band_idx;
2755 	basic_req.basic.link_idx = mvif->link_idx;
2756 
2757 	mt76_mcu_send_msg(mdev, MCU_UNI_CMD(BSS_INFO_UPDATE),
2758 			  &basic_req, sizeof(basic_req), true);
2759 
2760 	/* recovery omac address for the legacy interface */
2761 	memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN);
2762 	mt76_mcu_send_msg(mdev, MCU_UNI_CMD(DEV_INFO_UPDATE),
2763 			  &dev_req, sizeof(dev_req), true);
2764 }
2765 
mt7925_mcu_add_bss_info(struct mt792x_phy * phy,struct ieee80211_chanctx_conf * ctx,struct ieee80211_bss_conf * link_conf,struct ieee80211_link_sta * link_sta,int enable)2766 int mt7925_mcu_add_bss_info(struct mt792x_phy *phy,
2767 			    struct ieee80211_chanctx_conf *ctx,
2768 			    struct ieee80211_bss_conf *link_conf,
2769 			    struct ieee80211_link_sta *link_sta,
2770 			    int enable)
2771 {
2772 	struct mt792x_vif *mvif = (struct mt792x_vif *)link_conf->vif->drv_priv;
2773 	struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf);
2774 	struct mt792x_dev *dev = phy->dev;
2775 	struct mt792x_link_sta *mlink_bc;
2776 	struct sk_buff *skb;
2777 
2778 	skb = __mt7925_mcu_alloc_bss_req(&dev->mt76, &mconf->mt76,
2779 					 MT7925_BSS_UPDATE_MAX_SIZE);
2780 	if (IS_ERR(skb))
2781 		return PTR_ERR(skb);
2782 
2783 	mlink_bc = mt792x_sta_to_link(&mvif->sta, mconf->link_id);
2784 
2785 	/* bss_basic must be first */
2786 	mt7925_mcu_bss_basic_tlv(skb, link_conf, link_sta, ctx, phy->mt76,
2787 				 mlink_bc->wcid.idx, enable);
2788 	mt7925_mcu_bss_sec_tlv(skb, link_conf);
2789 	mt7925_mcu_bss_bmc_tlv(skb, phy, ctx, link_conf);
2790 	mt7925_mcu_bss_qos_tlv(skb, link_conf);
2791 	mt7925_mcu_bss_mld_tlv(skb, link_conf);
2792 	mt7925_mcu_bss_ifs_tlv(skb, link_conf);
2793 
2794 	if (link_conf->he_support) {
2795 		mt7925_mcu_bss_he_tlv(skb, link_conf, phy);
2796 		mt7925_mcu_bss_color_tlv(skb, link_conf, enable);
2797 	}
2798 
2799 	if (enable)
2800 		mt7925_mcu_bss_rlm_tlv(skb, phy->mt76, link_conf, ctx);
2801 
2802 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
2803 				     MCU_UNI_CMD(BSS_INFO_UPDATE), true);
2804 }
2805 
mt7925_mcu_set_dbdc(struct mt76_phy * phy,bool enable)2806 int mt7925_mcu_set_dbdc(struct mt76_phy *phy, bool enable)
2807 {
2808 	struct mt76_dev *mdev = phy->dev;
2809 
2810 	struct mbmc_conf_tlv *conf;
2811 	struct mbmc_set_req *hdr;
2812 	struct sk_buff *skb;
2813 	struct tlv *tlv;
2814 	int max_len, err;
2815 
2816 	max_len = sizeof(*hdr) + sizeof(*conf);
2817 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
2818 	if (!skb)
2819 		return -ENOMEM;
2820 
2821 	hdr = (struct mbmc_set_req *)skb_put(skb, sizeof(*hdr));
2822 
2823 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_MBMC_SETTING, sizeof(*conf));
2824 	conf = (struct mbmc_conf_tlv *)tlv;
2825 
2826 	conf->mbmc_en = enable;
2827 	conf->band = 0; /* unused */
2828 
2829 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SET_DBDC_PARMS),
2830 				    true);
2831 
2832 	return err;
2833 }
2834 
2835 static void
mt7925_mcu_build_scan_ie_tlv(struct mt76_dev * mdev,struct sk_buff * skb,struct ieee80211_scan_ies * scan_ies)2836 mt7925_mcu_build_scan_ie_tlv(struct mt76_dev *mdev,
2837 			     struct sk_buff *skb,
2838 			     struct ieee80211_scan_ies *scan_ies)
2839 {
2840 	u32 max_len = sizeof(struct scan_ie_tlv) + MT76_CONNAC_SCAN_IE_LEN;
2841 	struct scan_ie_tlv *ie;
2842 	enum nl80211_band i;
2843 	struct tlv *tlv;
2844 	const u8 *ies;
2845 	u16 ies_len;
2846 
2847 	for (i = 0; i <= NL80211_BAND_6GHZ; i++) {
2848 		if (i == NL80211_BAND_60GHZ)
2849 			continue;
2850 
2851 		ies = scan_ies->ies[i];
2852 		ies_len = scan_ies->len[i];
2853 
2854 		if (!ies || !ies_len)
2855 			continue;
2856 
2857 		if (ies_len > max_len)
2858 			return;
2859 
2860 		tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_IE,
2861 					      sizeof(*ie) + ies_len);
2862 		ie = (struct scan_ie_tlv *)tlv;
2863 
2864 		memcpy(ie->ies, ies, ies_len);
2865 		ie->ies_len = cpu_to_le16(ies_len);
2866 
2867 		switch (i) {
2868 		case NL80211_BAND_2GHZ:
2869 			ie->band = 1;
2870 			break;
2871 		case NL80211_BAND_6GHZ:
2872 			ie->band = 3;
2873 			break;
2874 		default:
2875 			ie->band = 2;
2876 			break;
2877 		}
2878 
2879 		max_len -= (sizeof(*ie) + ies_len);
2880 	}
2881 }
2882 
mt7925_mcu_hw_scan(struct mt76_phy * phy,struct ieee80211_vif * vif,struct ieee80211_scan_request * scan_req)2883 int mt7925_mcu_hw_scan(struct mt76_phy *phy, struct ieee80211_vif *vif,
2884 		       struct ieee80211_scan_request *scan_req)
2885 {
2886 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
2887 	struct cfg80211_scan_request *sreq = &scan_req->req;
2888 	int n_ssids = 0, err, i;
2889 	struct ieee80211_channel **scan_list = sreq->channels;
2890 	struct mt76_dev *mdev = phy->dev;
2891 	struct mt76_connac_mcu_scan_channel *chan;
2892 	struct sk_buff *skb;
2893 	struct scan_hdr_tlv *hdr;
2894 	struct scan_req_tlv *req;
2895 	struct scan_ssid_tlv *ssid;
2896 	struct scan_bssid_tlv *bssid;
2897 	struct scan_chan_info_tlv *chan_info;
2898 	struct scan_ie_tlv *ie;
2899 	struct scan_misc_tlv *misc;
2900 	struct tlv *tlv;
2901 	int max_len;
2902 
2903 	if (test_bit(MT76_HW_SCANNING, &phy->state))
2904 		return -EBUSY;
2905 
2906 	max_len = sizeof(*hdr) + sizeof(*req) + sizeof(*ssid) +
2907 		  sizeof(*bssid) * MT7925_RNR_SCAN_MAX_BSSIDS +
2908 		  sizeof(*chan_info) + sizeof(*misc) + sizeof(*ie) +
2909 		  MT76_CONNAC_SCAN_IE_LEN;
2910 
2911 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
2912 	if (!skb)
2913 		return -ENOMEM;
2914 
2915 	set_bit(MT76_HW_SCANNING, &phy->state);
2916 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
2917 
2918 	hdr = (struct scan_hdr_tlv *)skb_put(skb, sizeof(*hdr));
2919 	hdr->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
2920 	hdr->bss_idx = mvif->idx;
2921 
2922 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_REQ, sizeof(*req));
2923 	req = (struct scan_req_tlv *)tlv;
2924 	req->scan_type = sreq->n_ssids ? 1 : 0;
2925 	req->probe_req_num = sreq->n_ssids ? 2 : 0;
2926 
2927 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SSID, sizeof(*ssid));
2928 	ssid = (struct scan_ssid_tlv *)tlv;
2929 	for (i = 0; i < sreq->n_ssids; i++) {
2930 		if (!sreq->ssids[i].ssid_len)
2931 			continue;
2932 		if (i >= MT7925_RNR_SCAN_MAX_BSSIDS)
2933 			break;
2934 
2935 		ssid->ssids[n_ssids].ssid_len = cpu_to_le32(sreq->ssids[i].ssid_len);
2936 		memcpy(ssid->ssids[n_ssids].ssid, sreq->ssids[i].ssid,
2937 		       sreq->ssids[i].ssid_len);
2938 		n_ssids++;
2939 	}
2940 	ssid->ssid_type = n_ssids ? BIT(2) : BIT(0);
2941 	ssid->ssids_num = n_ssids;
2942 
2943 	if (sreq->n_6ghz_params) {
2944 		u8 j;
2945 
2946 		mt76_connac_mcu_build_rnr_scan_param(mdev, sreq);
2947 
2948 		for (j = 0; j < mdev->rnr.bssid_num; j++) {
2949 			if (j >= MT7925_RNR_SCAN_MAX_BSSIDS)
2950 				break;
2951 
2952 			tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_BSSID,
2953 						      sizeof(*bssid));
2954 			bssid = (struct scan_bssid_tlv *)tlv;
2955 
2956 			ether_addr_copy(bssid->bssid, mdev->rnr.bssid[j]);
2957 			bssid->match_ch = mdev->rnr.channel[j];
2958 			bssid->match_ssid_ind = MT7925_RNR_SCAN_MAX_BSSIDS;
2959 			bssid->match_short_ssid_ind = MT7925_RNR_SCAN_MAX_BSSIDS;
2960 		}
2961 		req->scan_func |= SCAN_FUNC_RNR_SCAN;
2962 	} else {
2963 		tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_BSSID, sizeof(*bssid));
2964 		bssid = (struct scan_bssid_tlv *)tlv;
2965 
2966 		ether_addr_copy(bssid->bssid, sreq->bssid);
2967 	}
2968 
2969 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_CHANNEL, sizeof(*chan_info));
2970 	chan_info = (struct scan_chan_info_tlv *)tlv;
2971 	chan_info->channels_num = min_t(u8, sreq->n_channels,
2972 					ARRAY_SIZE(chan_info->channels));
2973 	for (i = 0; i < chan_info->channels_num; i++) {
2974 		chan = &chan_info->channels[i];
2975 
2976 		switch (scan_list[i]->band) {
2977 		case NL80211_BAND_2GHZ:
2978 			chan->band = 1;
2979 			break;
2980 		case NL80211_BAND_6GHZ:
2981 			chan->band = 3;
2982 			break;
2983 		default:
2984 			chan->band = 2;
2985 			break;
2986 		}
2987 		chan->channel_num = scan_list[i]->hw_value;
2988 	}
2989 	chan_info->channel_type = sreq->n_channels ? 4 : 0;
2990 
2991 	req->scan_func |= SCAN_FUNC_SPLIT_SCAN;
2992 
2993 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_MISC, sizeof(*misc));
2994 	misc = (struct scan_misc_tlv *)tlv;
2995 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2996 		get_random_mask_addr(misc->random_mac, sreq->mac_addr,
2997 				     sreq->mac_addr_mask);
2998 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
2999 	}
3000 
3001 	/* Append scan probe IEs as the last tlv */
3002 	mt7925_mcu_build_scan_ie_tlv(mdev, skb, &scan_req->ies);
3003 
3004 	err = mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SCAN_REQ),
3005 				    true);
3006 	if (err < 0)
3007 		clear_bit(MT76_HW_SCANNING, &phy->state);
3008 
3009 	return err;
3010 }
3011 EXPORT_SYMBOL_GPL(mt7925_mcu_hw_scan);
3012 
mt7925_mcu_sched_scan_req(struct mt76_phy * phy,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * sreq,struct ieee80211_scan_ies * ies)3013 int mt7925_mcu_sched_scan_req(struct mt76_phy *phy,
3014 			      struct ieee80211_vif *vif,
3015 			      struct cfg80211_sched_scan_request *sreq,
3016 			      struct ieee80211_scan_ies *ies)
3017 {
3018 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
3019 	struct ieee80211_channel **scan_list = sreq->channels;
3020 	struct mt76_connac_mcu_scan_channel *chan;
3021 	struct mt76_dev *mdev = phy->dev;
3022 	struct cfg80211_match_set *cfg_match;
3023 	struct cfg80211_ssid *cfg_ssid;
3024 
3025 	struct scan_hdr_tlv *hdr;
3026 	struct scan_sched_req *req;
3027 	struct scan_ssid_tlv *ssid;
3028 	struct scan_chan_info_tlv *chan_info;
3029 	struct scan_ie_tlv *ie;
3030 	struct scan_sched_ssid_match_sets *match;
3031 	struct sk_buff *skb;
3032 	struct tlv *tlv;
3033 	int i, max_len;
3034 
3035 	max_len = sizeof(*hdr) + sizeof(*req) + sizeof(*ssid) +
3036 		  sizeof(*chan_info) + sizeof(*ie) +
3037 		  sizeof(*match);
3038 
3039 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
3040 	if (!skb)
3041 		return -ENOMEM;
3042 
3043 	mvif->scan_seq_num = (mvif->scan_seq_num + 1) & 0x7f;
3044 
3045 	hdr = (struct scan_hdr_tlv *)skb_put(skb, sizeof(*hdr));
3046 	hdr->seq_num = mvif->scan_seq_num | mvif->band_idx << 7;
3047 	hdr->bss_idx = mvif->idx;
3048 
3049 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SCHED_REQ, sizeof(*req));
3050 	req = (struct scan_sched_req *)tlv;
3051 	req->version = 1;
3052 
3053 	if (sreq->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
3054 		req->scan_func |= SCAN_FUNC_RANDOM_MAC;
3055 
3056 	req->intervals_num = sreq->n_scan_plans;
3057 	for (i = 0; i < req->intervals_num; i++)
3058 		req->intervals[i] = cpu_to_le16(sreq->scan_plans[i].interval);
3059 
3060 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SSID, sizeof(*ssid));
3061 	ssid = (struct scan_ssid_tlv *)tlv;
3062 
3063 	ssid->ssids_num = sreq->n_ssids;
3064 	ssid->ssid_type = BIT(2);
3065 	for (i = 0; i < ssid->ssids_num; i++) {
3066 		cfg_ssid = &sreq->ssids[i];
3067 		memcpy(ssid->ssids[i].ssid, cfg_ssid->ssid, cfg_ssid->ssid_len);
3068 		ssid->ssids[i].ssid_len = cpu_to_le32(cfg_ssid->ssid_len);
3069 	}
3070 
3071 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SSID_MATCH_SETS, sizeof(*match));
3072 	match = (struct scan_sched_ssid_match_sets *)tlv;
3073 	match->match_num = sreq->n_match_sets;
3074 	for (i = 0; i < match->match_num; i++) {
3075 		cfg_match = &sreq->match_sets[i];
3076 		memcpy(match->match[i].ssid, cfg_match->ssid.ssid,
3077 		       cfg_match->ssid.ssid_len);
3078 		match->match[i].rssi_th = cpu_to_le32(cfg_match->rssi_thold);
3079 		match->match[i].ssid_len = cfg_match->ssid.ssid_len;
3080 	}
3081 
3082 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_CHANNEL, sizeof(*chan_info));
3083 	chan_info = (struct scan_chan_info_tlv *)tlv;
3084 	chan_info->channels_num = min_t(u8, sreq->n_channels,
3085 					ARRAY_SIZE(chan_info->channels));
3086 	for (i = 0; i < chan_info->channels_num; i++) {
3087 		chan = &chan_info->channels[i];
3088 
3089 		switch (scan_list[i]->band) {
3090 		case NL80211_BAND_2GHZ:
3091 			chan->band = 1;
3092 			break;
3093 		case NL80211_BAND_6GHZ:
3094 			chan->band = 3;
3095 			break;
3096 		default:
3097 			chan->band = 2;
3098 			break;
3099 		}
3100 		chan->channel_num = scan_list[i]->hw_value;
3101 	}
3102 	chan_info->channel_type = sreq->n_channels ? 4 : 0;
3103 
3104 	/* Append scan probe IEs as the last tlv */
3105 	mt7925_mcu_build_scan_ie_tlv(mdev, skb, ies);
3106 
3107 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SCAN_REQ),
3108 				     true);
3109 }
3110 EXPORT_SYMBOL_GPL(mt7925_mcu_sched_scan_req);
3111 
3112 int
mt7925_mcu_sched_scan_enable(struct mt76_phy * phy,struct ieee80211_vif * vif,bool enable)3113 mt7925_mcu_sched_scan_enable(struct mt76_phy *phy,
3114 			     struct ieee80211_vif *vif,
3115 			     bool enable)
3116 {
3117 	struct mt76_dev *mdev = phy->dev;
3118 	struct scan_sched_enable *req;
3119 	struct scan_hdr_tlv *hdr;
3120 	struct sk_buff *skb;
3121 	struct tlv *tlv;
3122 	int max_len;
3123 
3124 	max_len = sizeof(*hdr) + sizeof(*req);
3125 
3126 	skb = mt76_mcu_msg_alloc(mdev, NULL, max_len);
3127 	if (!skb)
3128 		return -ENOMEM;
3129 
3130 	hdr = (struct scan_hdr_tlv *)skb_put(skb, sizeof(*hdr));
3131 	hdr->seq_num = 0;
3132 	hdr->bss_idx = 0;
3133 
3134 	tlv = mt76_connac_mcu_add_tlv(skb, UNI_SCAN_SCHED_ENABLE, sizeof(*req));
3135 	req = (struct scan_sched_enable *)tlv;
3136 	req->active = !enable;
3137 
3138 	if (enable)
3139 		set_bit(MT76_HW_SCHED_SCANNING, &phy->state);
3140 	else
3141 		clear_bit(MT76_HW_SCHED_SCANNING, &phy->state);
3142 
3143 	return mt76_mcu_skb_send_msg(mdev, skb, MCU_UNI_CMD(SCAN_REQ),
3144 				     true);
3145 }
3146 
mt7925_mcu_cancel_hw_scan(struct mt76_phy * phy,struct ieee80211_vif * vif)3147 int mt7925_mcu_cancel_hw_scan(struct mt76_phy *phy,
3148 			      struct ieee80211_vif *vif)
3149 {
3150 	struct mt76_vif_link *mvif = (struct mt76_vif_link *)vif->drv_priv;
3151 	struct {
3152 		struct scan_hdr {
3153 			u8 seq_num;
3154 			u8 bss_idx;
3155 			u8 pad[2];
3156 		} __packed hdr;
3157 		struct scan_cancel_tlv {
3158 			__le16 tag;
3159 			__le16 len;
3160 			u8 is_ext_channel;
3161 			u8 rsv[3];
3162 		} __packed cancel;
3163 	} req = {
3164 		.hdr = {
3165 			.seq_num = mvif->scan_seq_num,
3166 			.bss_idx = mvif->idx,
3167 		},
3168 		.cancel = {
3169 			.tag = cpu_to_le16(UNI_SCAN_CANCEL),
3170 			.len = cpu_to_le16(sizeof(struct scan_cancel_tlv)),
3171 		},
3172 	};
3173 
3174 	if (test_and_clear_bit(MT76_HW_SCANNING, &phy->state)) {
3175 		struct cfg80211_scan_info info = {
3176 			.aborted = true,
3177 		};
3178 
3179 		ieee80211_scan_completed(phy->hw, &info);
3180 	}
3181 
3182 	return mt76_mcu_send_msg(phy->dev, MCU_UNI_CMD(SCAN_REQ),
3183 				 &req, sizeof(req), true);
3184 }
3185 EXPORT_SYMBOL_GPL(mt7925_mcu_cancel_hw_scan);
3186 
mt7925_mcu_set_channel_domain(struct mt76_phy * phy)3187 int mt7925_mcu_set_channel_domain(struct mt76_phy *phy)
3188 {
3189 	int len, i, n_max_channels, n_2ch = 0, n_5ch = 0, n_6ch = 0;
3190 	struct {
3191 		struct {
3192 			u8 alpha2[4]; /* regulatory_request.alpha2 */
3193 			u8 bw_2g; /* BW_20_40M		0
3194 				   * BW_20M		1
3195 				   * BW_20_40_80M	2
3196 				   * BW_20_40_80_160M	3
3197 				   * BW_20_40_80_8080M	4
3198 				   */
3199 			u8 bw_5g;
3200 			u8 bw_6g;
3201 			u8 pad;
3202 		} __packed hdr;
3203 		struct n_chan {
3204 			__le16 tag;
3205 			__le16 len;
3206 			u8 n_2ch;
3207 			u8 n_5ch;
3208 			u8 n_6ch;
3209 			u8 pad;
3210 		} __packed n_ch;
3211 	} req = {
3212 		.hdr = {
3213 			.bw_2g = 0,
3214 			.bw_5g = 3, /* BW_20_40_80_160M */
3215 			.bw_6g = 3,
3216 		},
3217 		.n_ch = {
3218 			.tag = cpu_to_le16(2),
3219 		},
3220 	};
3221 	struct mt76_connac_mcu_chan {
3222 		__le16 hw_value;
3223 		__le16 pad;
3224 		__le32 flags;
3225 	} __packed channel;
3226 	struct mt76_dev *dev = phy->dev;
3227 	struct ieee80211_channel *chan;
3228 	struct sk_buff *skb;
3229 
3230 	n_max_channels = phy->sband_2g.sband.n_channels +
3231 			 phy->sband_5g.sband.n_channels +
3232 			 phy->sband_6g.sband.n_channels;
3233 	len = sizeof(req) + n_max_channels * sizeof(channel);
3234 
3235 	skb = mt76_mcu_msg_alloc(dev, NULL, len);
3236 	if (!skb)
3237 		return -ENOMEM;
3238 
3239 	skb_reserve(skb, sizeof(req));
3240 
3241 	for (i = 0; i < phy->sband_2g.sband.n_channels; i++) {
3242 		chan = &phy->sband_2g.sband.channels[i];
3243 		if (chan->flags & IEEE80211_CHAN_DISABLED)
3244 			continue;
3245 
3246 		channel.hw_value = cpu_to_le16(chan->hw_value);
3247 		channel.flags = cpu_to_le32(chan->flags);
3248 		channel.pad = 0;
3249 
3250 		skb_put_data(skb, &channel, sizeof(channel));
3251 		n_2ch++;
3252 	}
3253 	for (i = 0; i < phy->sband_5g.sband.n_channels; i++) {
3254 		chan = &phy->sband_5g.sband.channels[i];
3255 		if (chan->flags & IEEE80211_CHAN_DISABLED)
3256 			continue;
3257 
3258 		channel.hw_value = cpu_to_le16(chan->hw_value);
3259 		channel.flags = cpu_to_le32(chan->flags);
3260 		channel.pad = 0;
3261 
3262 		skb_put_data(skb, &channel, sizeof(channel));
3263 		n_5ch++;
3264 	}
3265 	for (i = 0; i < phy->sband_6g.sband.n_channels; i++) {
3266 		chan = &phy->sband_6g.sband.channels[i];
3267 		if (chan->flags & IEEE80211_CHAN_DISABLED)
3268 			continue;
3269 
3270 		channel.hw_value = cpu_to_le16(chan->hw_value);
3271 		channel.flags = cpu_to_le32(chan->flags);
3272 		channel.pad = 0;
3273 
3274 		skb_put_data(skb, &channel, sizeof(channel));
3275 		n_6ch++;
3276 	}
3277 
3278 	BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(req.hdr.alpha2));
3279 	memcpy(req.hdr.alpha2, dev->alpha2, sizeof(dev->alpha2));
3280 	req.n_ch.n_2ch = n_2ch;
3281 	req.n_ch.n_5ch = n_5ch;
3282 	req.n_ch.n_6ch = n_6ch;
3283 	len = sizeof(struct n_chan) + (n_2ch + n_5ch + n_6ch) * sizeof(channel);
3284 	req.n_ch.len = cpu_to_le16(len);
3285 	memcpy(__skb_push(skb, sizeof(req)), &req, sizeof(req));
3286 
3287 	return mt76_mcu_skb_send_msg(dev, skb, MCU_UNI_CMD(SET_DOMAIN_INFO),
3288 				     true);
3289 }
3290 EXPORT_SYMBOL_GPL(mt7925_mcu_set_channel_domain);
3291 
3292 static int
__mt7925_mcu_set_clc(struct mt792x_dev * dev,u8 * alpha2,enum environment_cap env_cap,struct mt7925_clc * clc,u8 idx)3293 __mt7925_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
3294 		     enum environment_cap env_cap,
3295 		     struct mt7925_clc *clc, u8 idx)
3296 {
3297 	struct mt7925_clc_segment *seg;
3298 	struct sk_buff *skb;
3299 	struct {
3300 		u8 rsv[4];
3301 		__le16 tag;
3302 		__le16 len;
3303 
3304 		u8 ver;
3305 		u8 pad0;
3306 		__le16 size;
3307 		u8 idx;
3308 		u8 env;
3309 		u8 acpi_conf;
3310 		u8 pad1;
3311 		u8 alpha2[2];
3312 		u8 type[2];
3313 		u8 rsvd[64];
3314 	} __packed req = {
3315 		.tag = cpu_to_le16(0x3),
3316 		.len = cpu_to_le16(sizeof(req) - 4),
3317 
3318 		.idx = idx,
3319 		.env = env_cap,
3320 	};
3321 	int ret, valid_cnt = 0;
3322 	u8 *pos, *last_pos;
3323 
3324 	if (!clc)
3325 		return 0;
3326 
3327 	req.ver = clc->ver;
3328 	pos = clc->data + sizeof(*seg) * clc->t0.nr_seg;
3329 	last_pos = clc->data + le32_to_cpu(*(__le32 *)(clc->data + 4));
3330 	while (pos < last_pos) {
3331 		struct mt7925_clc_rule *rule = (struct mt7925_clc_rule *)pos;
3332 
3333 		pos += sizeof(*rule);
3334 		if (rule->alpha2[0] != alpha2[0] ||
3335 		    rule->alpha2[1] != alpha2[1])
3336 			continue;
3337 
3338 		seg = (struct mt7925_clc_segment *)clc->data
3339 			  + rule->seg_idx - 1;
3340 
3341 		memcpy(req.alpha2, rule->alpha2, 2);
3342 		memcpy(req.type, rule->type, 2);
3343 
3344 		req.size = cpu_to_le16(seg->len);
3345 		dev->phy.clc_chan_conf = clc->ver == 1 ? 0xff : rule->flag;
3346 		skb = __mt76_mcu_msg_alloc(&dev->mt76, &req,
3347 					   le16_to_cpu(req.size) + sizeof(req),
3348 					   sizeof(req), GFP_KERNEL);
3349 		if (!skb)
3350 			return -ENOMEM;
3351 		skb_put_data(skb, clc->data + seg->offset, seg->len);
3352 
3353 		ret = mt76_mcu_skb_send_msg(&dev->mt76, skb,
3354 					    MCU_UNI_CMD(SET_POWER_LIMIT),
3355 					    true);
3356 		if (ret < 0)
3357 			return ret;
3358 		valid_cnt++;
3359 	}
3360 
3361 	if (!valid_cnt) {
3362 		dev->phy.clc_chan_conf = 0xff;
3363 		return -ENOENT;
3364 	}
3365 
3366 	return 0;
3367 }
3368 
mt7925_mcu_set_clc(struct mt792x_dev * dev,u8 * alpha2,enum environment_cap env_cap)3369 int mt7925_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
3370 		       enum environment_cap env_cap)
3371 {
3372 	struct mt792x_phy *phy = (struct mt792x_phy *)&dev->phy;
3373 	int i, ret;
3374 
3375 	/* submit all clc config */
3376 	for (i = 0; i < ARRAY_SIZE(phy->clc); i++) {
3377 		if (i == MT792x_CLC_BE_CTRL)
3378 			continue;
3379 
3380 		ret = __mt7925_mcu_set_clc(dev, alpha2, env_cap,
3381 					   phy->clc[i], i);
3382 
3383 		/* If no country found, set "00" as default */
3384 		if (ret == -ENOENT)
3385 			ret = __mt7925_mcu_set_clc(dev, "00",
3386 						   ENVIRON_INDOOR,
3387 						   phy->clc[i], i);
3388 		if (ret < 0)
3389 			return ret;
3390 	}
3391 	return 0;
3392 }
3393 
mt7925_mcu_fill_message(struct mt76_dev * mdev,struct sk_buff * skb,int cmd,int * wait_seq)3394 int mt7925_mcu_fill_message(struct mt76_dev *mdev, struct sk_buff *skb,
3395 			    int cmd, int *wait_seq)
3396 {
3397 	int txd_len, mcu_cmd = FIELD_GET(__MCU_CMD_FIELD_ID, cmd);
3398 	struct mt76_connac2_mcu_uni_txd *uni_txd;
3399 	struct mt76_connac2_mcu_txd *mcu_txd;
3400 	__le32 *txd;
3401 	u32 val;
3402 	u8 seq;
3403 
3404 	/* TODO: make dynamic based on msg type */
3405 	mdev->mcu.timeout = 20 * HZ;
3406 
3407 	seq = ++mdev->mcu.msg_seq & 0xf;
3408 	if (!seq)
3409 		seq = ++mdev->mcu.msg_seq & 0xf;
3410 
3411 	if (cmd == MCU_CMD(FW_SCATTER))
3412 		goto exit;
3413 
3414 	txd_len = cmd & __MCU_CMD_FIELD_UNI ? sizeof(*uni_txd) : sizeof(*mcu_txd);
3415 	txd = (__le32 *)skb_push(skb, txd_len);
3416 
3417 	val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len) |
3418 	      FIELD_PREP(MT_TXD0_PKT_FMT, MT_TX_TYPE_CMD) |
3419 	      FIELD_PREP(MT_TXD0_Q_IDX, MT_TX_MCU_PORT_RX_Q0);
3420 	txd[0] = cpu_to_le32(val);
3421 
3422 	val = FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_CMD);
3423 	txd[1] = cpu_to_le32(val);
3424 
3425 	if (cmd & __MCU_CMD_FIELD_UNI) {
3426 		uni_txd = (struct mt76_connac2_mcu_uni_txd *)txd;
3427 		uni_txd->len = cpu_to_le16(skb->len - sizeof(uni_txd->txd));
3428 		uni_txd->cid = cpu_to_le16(mcu_cmd);
3429 		uni_txd->s2d_index = MCU_S2D_H2N;
3430 		uni_txd->pkt_type = MCU_PKT_ID;
3431 		uni_txd->seq = seq;
3432 
3433 		if (cmd & __MCU_CMD_FIELD_QUERY)
3434 			uni_txd->option = MCU_CMD_UNI_QUERY_ACK;
3435 		else
3436 			uni_txd->option = MCU_CMD_UNI_EXT_ACK;
3437 
3438 		if (cmd == MCU_UNI_CMD(HIF_CTRL) ||
3439 		    cmd == MCU_UNI_CMD(CHIP_CONFIG))
3440 			uni_txd->option &= ~MCU_CMD_ACK;
3441 
3442 		if (mcu_cmd == MCU_UNI_CMD_TESTMODE_CTRL ||
3443 		    mcu_cmd == MCU_UNI_CMD_TESTMODE_RX_STAT) {
3444 			if (cmd & __MCU_CMD_FIELD_QUERY)
3445 				uni_txd->option = 0x2;
3446 			else
3447 				uni_txd->option = 0x6;
3448 		}
3449 
3450 		goto exit;
3451 	}
3452 
3453 	mcu_txd = (struct mt76_connac2_mcu_txd *)txd;
3454 	mcu_txd->len = cpu_to_le16(skb->len - sizeof(mcu_txd->txd));
3455 	mcu_txd->pq_id = cpu_to_le16(MCU_PQ_ID(MT_TX_PORT_IDX_MCU,
3456 					       MT_TX_MCU_PORT_RX_Q0));
3457 	mcu_txd->pkt_type = MCU_PKT_ID;
3458 	mcu_txd->seq = seq;
3459 	mcu_txd->cid = mcu_cmd;
3460 	mcu_txd->ext_cid = FIELD_GET(__MCU_CMD_FIELD_EXT_ID, cmd);
3461 
3462 	if (mcu_txd->ext_cid || (cmd & __MCU_CMD_FIELD_CE)) {
3463 		if (cmd & __MCU_CMD_FIELD_QUERY)
3464 			mcu_txd->set_query = MCU_Q_QUERY;
3465 		else
3466 			mcu_txd->set_query = MCU_Q_SET;
3467 		mcu_txd->ext_cid_ack = !!mcu_txd->ext_cid;
3468 	} else {
3469 		mcu_txd->set_query = MCU_Q_NA;
3470 	}
3471 
3472 	if (cmd & __MCU_CMD_FIELD_WA)
3473 		mcu_txd->s2d_index = MCU_S2D_H2C;
3474 	else
3475 		mcu_txd->s2d_index = MCU_S2D_H2N;
3476 
3477 exit:
3478 	if (wait_seq)
3479 		*wait_seq = seq;
3480 
3481 	return 0;
3482 }
3483 EXPORT_SYMBOL_GPL(mt7925_mcu_fill_message);
3484 
mt7925_mcu_set_rts_thresh(struct mt792x_phy * phy,u32 val)3485 int mt7925_mcu_set_rts_thresh(struct mt792x_phy *phy, u32 val)
3486 {
3487 	struct {
3488 		u8 band_idx;
3489 		u8 _rsv[3];
3490 
3491 		__le16 tag;
3492 		__le16 len;
3493 		__le32 len_thresh;
3494 		__le32 pkt_thresh;
3495 	} __packed req = {
3496 		.band_idx = phy->mt76->band_idx,
3497 		.tag = cpu_to_le16(UNI_BAND_CONFIG_RTS_THRESHOLD),
3498 		.len = cpu_to_le16(sizeof(req) - 4),
3499 		.len_thresh = cpu_to_le32(val),
3500 		.pkt_thresh = cpu_to_le32(0x2),
3501 	};
3502 
3503 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_UNI_CMD(BAND_CONFIG),
3504 				 &req, sizeof(req), true);
3505 }
3506 
mt7925_mcu_set_radio_en(struct mt792x_phy * phy,bool enable)3507 int mt7925_mcu_set_radio_en(struct mt792x_phy *phy, bool enable)
3508 {
3509 	struct {
3510 		u8 band_idx;
3511 		u8 _rsv[3];
3512 
3513 		__le16 tag;
3514 		__le16 len;
3515 		u8 enable;
3516 		u8 _rsv2[3];
3517 	} __packed req = {
3518 		.band_idx = phy->mt76->band_idx,
3519 		.tag = cpu_to_le16(UNI_BAND_CONFIG_RADIO_ENABLE),
3520 		.len = cpu_to_le16(sizeof(req) - 4),
3521 		.enable = enable,
3522 	};
3523 
3524 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_UNI_CMD(BAND_CONFIG),
3525 				 &req, sizeof(req), true);
3526 }
3527 
3528 static void
mt7925_mcu_build_sku(struct mt76_dev * dev,s8 * sku,struct mt76_power_limits * limits,enum nl80211_band band)3529 mt7925_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
3530 		     struct mt76_power_limits *limits,
3531 		     enum nl80211_band band)
3532 {
3533 	int i, offset = sizeof(limits->cck);
3534 
3535 	memset(sku, 127, MT_CONNAC3_SKU_POWER_LIMIT);
3536 
3537 	if (band == NL80211_BAND_2GHZ) {
3538 		/* cck */
3539 		memcpy(sku, limits->cck, sizeof(limits->cck));
3540 	}
3541 
3542 	/* ofdm */
3543 	memcpy(&sku[offset], limits->ofdm, sizeof(limits->ofdm));
3544 	offset += (sizeof(limits->ofdm) * 5);
3545 
3546 	/* ht */
3547 	for (i = 0; i < 2; i++) {
3548 		memcpy(&sku[offset], limits->mcs[i], 8);
3549 		offset += 8;
3550 	}
3551 	sku[offset++] = limits->mcs[0][0];
3552 
3553 	/* vht */
3554 	for (i = 0; i < ARRAY_SIZE(limits->mcs); i++) {
3555 		memcpy(&sku[offset], limits->mcs[i],
3556 		       ARRAY_SIZE(limits->mcs[i]));
3557 		offset += 12;
3558 	}
3559 
3560 	/* he */
3561 	for (i = 0; i < ARRAY_SIZE(limits->ru); i++) {
3562 		memcpy(&sku[offset], limits->ru[i], ARRAY_SIZE(limits->ru[i]));
3563 		offset += ARRAY_SIZE(limits->ru[i]);
3564 	}
3565 
3566 	/* eht */
3567 	for (i = 0; i < ARRAY_SIZE(limits->eht); i++) {
3568 		memcpy(&sku[offset], limits->eht[i], ARRAY_SIZE(limits->eht[i]));
3569 		offset += ARRAY_SIZE(limits->eht[i]);
3570 	}
3571 }
3572 
3573 static int
mt7925_mcu_rate_txpower_band(struct mt76_phy * phy,enum nl80211_band band)3574 mt7925_mcu_rate_txpower_band(struct mt76_phy *phy,
3575 			     enum nl80211_band band)
3576 {
3577 	int tx_power, n_chan, last_ch, err = 0, idx = 0;
3578 	int i, sku_len, batch_size, batch_len = 3;
3579 	struct mt76_dev *dev = phy->dev;
3580 	static const u8 chan_list_2ghz[] = {
3581 		1, 2,  3,  4,  5,  6,  7,
3582 		8, 9, 10, 11, 12, 13, 14
3583 	};
3584 	static const u8 chan_list_5ghz[] = {
3585 		 36,  38,  40,  42,  44,  46,  48,
3586 		 50,  52,  54,  56,  58,  60,  62,
3587 		 64, 100, 102, 104, 106, 108, 110,
3588 		112, 114, 116, 118, 120, 122, 124,
3589 		126, 128, 132, 134, 136, 138, 140,
3590 		142, 144, 149, 151, 153, 155, 157,
3591 		159, 161, 165, 167
3592 	};
3593 	static const u8 chan_list_6ghz[] = {
3594 		  1,   3,   5,   7,   9,  11,  13,
3595 		 15,  17,  19,  21,  23,  25,  27,
3596 		 29,  33,  35,  37,  39,  41,  43,
3597 		 45,  47,  49,  51,  53,  55,  57,
3598 		 59,  61,  65,  67,  69,  71,  73,
3599 		 75,  77,  79,  81,  83,  85,  87,
3600 		 89,  91,  93,  97,  99, 101, 103,
3601 		105, 107, 109, 111, 113, 115, 117,
3602 		119, 121, 123, 125, 129, 131, 133,
3603 		135, 137, 139, 141, 143, 145, 147,
3604 		149, 151, 153, 155, 157, 161, 163,
3605 		165, 167, 169, 171, 173, 175, 177,
3606 		179, 181, 183, 185, 187, 189, 193,
3607 		195, 197, 199, 201, 203, 205, 207,
3608 		209, 211, 213, 215, 217, 219, 221,
3609 		225, 227, 229, 233
3610 	};
3611 	struct mt76_power_limits *limits;
3612 	struct mt7925_sku_tlv *sku_tlbv;
3613 	const u8 *ch_list;
3614 
3615 	sku_len = sizeof(*sku_tlbv);
3616 	tx_power = 2 * phy->hw->conf.power_level;
3617 	if (!tx_power)
3618 		tx_power = 127;
3619 
3620 	if (band == NL80211_BAND_2GHZ) {
3621 		n_chan = ARRAY_SIZE(chan_list_2ghz);
3622 		ch_list = chan_list_2ghz;
3623 		last_ch = chan_list_2ghz[ARRAY_SIZE(chan_list_2ghz) - 1];
3624 	} else if (band == NL80211_BAND_6GHZ) {
3625 		n_chan = ARRAY_SIZE(chan_list_6ghz);
3626 		ch_list = chan_list_6ghz;
3627 		last_ch = chan_list_6ghz[ARRAY_SIZE(chan_list_6ghz) - 1];
3628 	} else {
3629 		n_chan = ARRAY_SIZE(chan_list_5ghz);
3630 		ch_list = chan_list_5ghz;
3631 		last_ch = chan_list_5ghz[ARRAY_SIZE(chan_list_5ghz) - 1];
3632 	}
3633 	batch_size = DIV_ROUND_UP(n_chan, batch_len);
3634 
3635 	limits = devm_kmalloc(dev->dev, sizeof(*limits), GFP_KERNEL);
3636 	if (!limits)
3637 		return -ENOMEM;
3638 
3639 	sku_tlbv = devm_kmalloc(dev->dev, sku_len, GFP_KERNEL);
3640 	if (!sku_tlbv) {
3641 		devm_kfree(dev->dev, limits);
3642 		return -ENOMEM;
3643 	}
3644 
3645 	for (i = 0; i < batch_size; i++) {
3646 		struct mt7925_tx_power_limit_tlv *tx_power_tlv;
3647 		int j, msg_len, num_ch;
3648 		struct sk_buff *skb;
3649 
3650 		num_ch = i == batch_size - 1 ? n_chan % batch_len : batch_len;
3651 		msg_len = sizeof(*tx_power_tlv) + num_ch * sku_len;
3652 		skb = mt76_mcu_msg_alloc(dev, NULL, msg_len);
3653 		if (!skb) {
3654 			err = -ENOMEM;
3655 			goto out;
3656 		}
3657 
3658 		tx_power_tlv = (struct mt7925_tx_power_limit_tlv *)
3659 			       skb_put(skb, sizeof(*tx_power_tlv));
3660 
3661 		BUILD_BUG_ON(sizeof(dev->alpha2) > sizeof(tx_power_tlv->alpha2));
3662 		memcpy(tx_power_tlv->alpha2, dev->alpha2, sizeof(dev->alpha2));
3663 		tx_power_tlv->n_chan = num_ch;
3664 		tx_power_tlv->tag = cpu_to_le16(0x1);
3665 		tx_power_tlv->len = cpu_to_le16(sizeof(*tx_power_tlv));
3666 
3667 		switch (band) {
3668 		case NL80211_BAND_2GHZ:
3669 			tx_power_tlv->band = 1;
3670 			break;
3671 		case NL80211_BAND_6GHZ:
3672 			tx_power_tlv->band = 3;
3673 			break;
3674 		default:
3675 			tx_power_tlv->band = 2;
3676 			break;
3677 		}
3678 
3679 		for (j = 0; j < num_ch; j++, idx++) {
3680 			struct ieee80211_channel chan = {
3681 				.hw_value = ch_list[idx],
3682 				.band = band,
3683 			};
3684 			s8 reg_power, sar_power;
3685 
3686 			reg_power = mt76_connac_get_ch_power(phy, &chan,
3687 							     tx_power);
3688 			sar_power = mt76_get_sar_power(phy, &chan, reg_power);
3689 
3690 			mt76_get_rate_power_limits(phy, &chan, limits,
3691 						   sar_power);
3692 
3693 			tx_power_tlv->last_msg = ch_list[idx] == last_ch;
3694 			sku_tlbv->channel = ch_list[idx];
3695 
3696 			mt7925_mcu_build_sku(dev, sku_tlbv->pwr_limit,
3697 					     limits, band);
3698 			skb_put_data(skb, sku_tlbv, sku_len);
3699 		}
3700 		err = mt76_mcu_skb_send_msg(dev, skb,
3701 					    MCU_UNI_CMD(SET_POWER_LIMIT),
3702 					    true);
3703 		if (err < 0)
3704 			goto out;
3705 	}
3706 
3707 out:
3708 	devm_kfree(dev->dev, sku_tlbv);
3709 	devm_kfree(dev->dev, limits);
3710 	return err;
3711 }
3712 
mt7925_mcu_set_rate_txpower(struct mt76_phy * phy)3713 int mt7925_mcu_set_rate_txpower(struct mt76_phy *phy)
3714 {
3715 	int err;
3716 
3717 	if (phy->cap.has_2ghz) {
3718 		err = mt7925_mcu_rate_txpower_band(phy,
3719 						   NL80211_BAND_2GHZ);
3720 		if (err < 0)
3721 			return err;
3722 	}
3723 
3724 	if (phy->cap.has_5ghz) {
3725 		err = mt7925_mcu_rate_txpower_band(phy,
3726 						   NL80211_BAND_5GHZ);
3727 		if (err < 0)
3728 			return err;
3729 	}
3730 
3731 	if (phy->cap.has_6ghz) {
3732 		err = mt7925_mcu_rate_txpower_band(phy,
3733 						   NL80211_BAND_6GHZ);
3734 		if (err < 0)
3735 			return err;
3736 	}
3737 
3738 	return 0;
3739 }
3740 
mt7925_mcu_wf_rf_pin_ctrl(struct mt792x_phy * phy)3741 int mt7925_mcu_wf_rf_pin_ctrl(struct mt792x_phy *phy)
3742 {
3743 #define UNI_CMD_RADIO_STATUS_GET	0
3744 	struct mt792x_dev *dev = phy->dev;
3745 	struct sk_buff *skb;
3746 	int ret;
3747 	struct {
3748 		__le16 tag;
3749 		__le16 len;
3750 		u8 rsv[4];
3751 	} __packed req = {
3752 		.tag = UNI_CMD_RADIO_STATUS_GET,
3753 		.len = cpu_to_le16(sizeof(req)),
3754 	};
3755 	struct mt7925_radio_status_event {
3756 		__le16 tag;
3757 		__le16 len;
3758 
3759 		u8 data;
3760 		u8 rsv[3];
3761 	} __packed *status;
3762 
3763 	ret = mt76_mcu_send_and_get_msg(&dev->mt76,
3764 					MCU_UNI_CMD(RADIO_STATUS),
3765 					&req, sizeof(req), true, &skb);
3766 	if (ret)
3767 		return ret;
3768 
3769 	skb_pull(skb, sizeof(struct tlv));
3770 	status = (struct mt7925_radio_status_event *)skb->data;
3771 	ret = status->data;
3772 
3773 	dev_kfree_skb(skb);
3774 
3775 	return ret;
3776 }
3777 
mt7925_mcu_set_rxfilter(struct mt792x_dev * dev,u32 fif,u8 bit_op,u32 bit_map)3778 int mt7925_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
3779 			    u8 bit_op, u32 bit_map)
3780 {
3781 	struct mt792x_phy *phy = &dev->phy;
3782 	struct {
3783 		u8 band_idx;
3784 		u8 rsv1[3];
3785 
3786 		__le16 tag;
3787 		__le16 len;
3788 		u8 mode;
3789 		u8 rsv2[3];
3790 		__le32 fif;
3791 		__le32 bit_map; /* bit_* for bitmap update */
3792 		u8 bit_op;
3793 		u8 pad[51];
3794 	} __packed req = {
3795 		.band_idx = phy->mt76->band_idx,
3796 		.tag = cpu_to_le16(UNI_BAND_CONFIG_SET_MAC80211_RX_FILTER),
3797 		.len = cpu_to_le16(sizeof(req) - 4),
3798 
3799 		.mode = fif ? 0 : 1,
3800 		.fif = cpu_to_le32(fif),
3801 		.bit_map = cpu_to_le32(bit_map),
3802 		.bit_op = bit_op,
3803 	};
3804 
3805 	return mt76_mcu_send_msg(&phy->dev->mt76, MCU_UNI_CMD(BAND_CONFIG),
3806 				 &req, sizeof(req), true);
3807 }
3808