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