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