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