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