xref: /linux/drivers/net/wireless/mediatek/mt76/mt7915/main.c (revision 6015fb905d89063231ed33bc15be19ef0fc339b8)
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include "mt7915.h"
9 #include "mcu.h"
10 
11 static bool mt7915_dev_running(struct mt7915_dev *dev)
12 {
13 	struct mt7915_phy *phy;
14 
15 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
16 		return true;
17 
18 	phy = mt7915_ext_phy(dev);
19 
20 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
21 }
22 
23 static int mt7915_start(struct ieee80211_hw *hw)
24 {
25 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
26 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
27 	bool running;
28 	int ret;
29 
30 	flush_work(&dev->init_work);
31 
32 	mutex_lock(&dev->mt76.mutex);
33 
34 	running = mt7915_dev_running(dev);
35 
36 	if (!running) {
37 		ret = mt76_connac_mcu_set_pm(&dev->mt76, 0, 0);
38 		if (ret)
39 			goto out;
40 
41 		ret = mt7915_mcu_set_mac(dev, 0, true, true);
42 		if (ret)
43 			goto out;
44 
45 		ret = mt7915_mcu_set_scs(dev, 0, true);
46 		if (ret)
47 			goto out;
48 
49 		mt7915_mac_enable_nf(dev, 0);
50 	}
51 
52 	if (phy != &dev->phy) {
53 		ret = mt76_connac_mcu_set_pm(&dev->mt76, 1, 0);
54 		if (ret)
55 			goto out;
56 
57 		ret = mt7915_mcu_set_mac(dev, 1, true, true);
58 		if (ret)
59 			goto out;
60 
61 		ret = mt7915_mcu_set_scs(dev, 1, true);
62 		if (ret)
63 			goto out;
64 
65 		mt7915_mac_enable_nf(dev, 1);
66 	}
67 
68 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b,
69 					     phy != &dev->phy);
70 	if (ret)
71 		goto out;
72 
73 	ret = mt7915_mcu_set_sku_en(phy, true);
74 	if (ret)
75 		goto out;
76 
77 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
78 	if (ret)
79 		goto out;
80 
81 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
82 
83 	if (!mt76_testmode_enabled(phy->mt76))
84 		ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
85 					     MT7915_WATCHDOG_TIME);
86 
87 	if (!running)
88 		mt7915_mac_reset_counters(phy);
89 
90 out:
91 	mutex_unlock(&dev->mt76.mutex);
92 
93 	return ret;
94 }
95 
96 static void mt7915_stop(struct ieee80211_hw *hw)
97 {
98 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
99 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
100 
101 	cancel_delayed_work_sync(&phy->mt76->mac_work);
102 
103 	mutex_lock(&dev->mt76.mutex);
104 
105 	mt76_testmode_reset(phy->mt76, true);
106 
107 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
108 
109 	if (phy != &dev->phy) {
110 		mt76_connac_mcu_set_pm(&dev->mt76, 1, 1);
111 		mt7915_mcu_set_mac(dev, 1, false, false);
112 	}
113 
114 	if (!mt7915_dev_running(dev)) {
115 		mt76_connac_mcu_set_pm(&dev->mt76, 0, 1);
116 		mt7915_mcu_set_mac(dev, 0, false, false);
117 	}
118 
119 	mutex_unlock(&dev->mt76.mutex);
120 }
121 
122 static inline int get_free_idx(u32 mask, u8 start, u8 end)
123 {
124 	return ffs(~mask & GENMASK(end, start));
125 }
126 
127 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
128 {
129 	int i;
130 
131 	switch (type) {
132 	case NL80211_IFTYPE_MESH_POINT:
133 	case NL80211_IFTYPE_ADHOC:
134 	case NL80211_IFTYPE_STATION:
135 		/* prefer hw bssid slot 1-3 */
136 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
137 		if (i)
138 			return i - 1;
139 
140 		if (type != NL80211_IFTYPE_STATION)
141 			break;
142 
143 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
144 		if (i)
145 			return i - 1;
146 
147 		if (~mask & BIT(HW_BSSID_0))
148 			return HW_BSSID_0;
149 
150 		break;
151 	case NL80211_IFTYPE_MONITOR:
152 	case NL80211_IFTYPE_AP:
153 		/* ap uses hw bssid 0 and ext bssid */
154 		if (~mask & BIT(HW_BSSID_0))
155 			return HW_BSSID_0;
156 
157 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
158 		if (i)
159 			return i - 1;
160 
161 		break;
162 	default:
163 		WARN_ON(1);
164 		break;
165 	}
166 
167 	return -1;
168 }
169 
170 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
171 {
172 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
173 	int i;
174 
175 	for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
176 		mvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
177 		mvif->bitrate_mask.control[i].he_gi = GENMASK(7, 0);
178 		mvif->bitrate_mask.control[i].he_ltf = GENMASK(7, 0);
179 		mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
180 		memset(mvif->bitrate_mask.control[i].ht_mcs, GENMASK(7, 0),
181 		       sizeof(mvif->bitrate_mask.control[i].ht_mcs));
182 		memset(mvif->bitrate_mask.control[i].vht_mcs, GENMASK(15, 0),
183 		       sizeof(mvif->bitrate_mask.control[i].vht_mcs));
184 		memset(mvif->bitrate_mask.control[i].he_mcs, GENMASK(15, 0),
185 		       sizeof(mvif->bitrate_mask.control[i].he_mcs));
186 	}
187 }
188 
189 static int mt7915_add_interface(struct ieee80211_hw *hw,
190 				struct ieee80211_vif *vif)
191 {
192 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
193 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
194 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
195 	struct mt76_txq *mtxq;
196 	bool ext_phy = phy != &dev->phy;
197 	int idx, ret = 0;
198 
199 	mutex_lock(&dev->mt76.mutex);
200 
201 	mt76_testmode_reset(phy->mt76, true);
202 
203 	if (vif->type == NL80211_IFTYPE_MONITOR &&
204 	    is_zero_ether_addr(vif->addr))
205 		phy->monitor_vif = vif;
206 
207 	mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
208 	if (mvif->mt76.idx >= MT7915_MAX_INTERFACES) {
209 		ret = -ENOSPC;
210 		goto out;
211 	}
212 
213 	idx = get_omac_idx(vif->type, phy->omac_mask);
214 	if (idx < 0) {
215 		ret = -ENOSPC;
216 		goto out;
217 	}
218 	mvif->mt76.omac_idx = idx;
219 	mvif->phy = phy;
220 	mvif->mt76.band_idx = ext_phy;
221 
222 	mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
223 	if (ext_phy)
224 		mvif->mt76.wmm_idx += 2;
225 
226 	ret = mt7915_mcu_add_dev_info(phy, vif, true);
227 	if (ret)
228 		goto out;
229 
230 	dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
231 	phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
232 
233 	idx = MT7915_WTBL_RESERVED - mvif->mt76.idx;
234 
235 	INIT_LIST_HEAD(&mvif->sta.rc_list);
236 	INIT_LIST_HEAD(&mvif->sta.poll_list);
237 	mvif->sta.wcid.idx = idx;
238 	mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
239 	mvif->sta.wcid.hw_key_idx = -1;
240 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
241 	mt76_packet_id_init(&mvif->sta.wcid);
242 
243 	mt7915_mac_wtbl_update(dev, idx,
244 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
245 
246 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
247 	if (vif->txq) {
248 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
249 		mtxq->wcid = &mvif->sta.wcid;
250 	}
251 
252 	if (vif->type != NL80211_IFTYPE_AP &&
253 	    (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
254 		vif->offload_flags = 0;
255 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
256 
257 	mt7915_init_bitrate_mask(vif);
258 	memset(&mvif->cap, -1, sizeof(mvif->cap));
259 
260 	mt7915_mcu_add_bss_info(phy, vif, true);
261 	mt7915_mcu_add_sta(dev, vif, NULL, true);
262 
263 out:
264 	mutex_unlock(&dev->mt76.mutex);
265 
266 	return ret;
267 }
268 
269 static void mt7915_remove_interface(struct ieee80211_hw *hw,
270 				    struct ieee80211_vif *vif)
271 {
272 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
273 	struct mt7915_sta *msta = &mvif->sta;
274 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
275 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
276 	int idx = msta->wcid.idx;
277 
278 	mt7915_mcu_add_bss_info(phy, vif, false);
279 	mt7915_mcu_add_sta(dev, vif, NULL, false);
280 
281 	mutex_lock(&dev->mt76.mutex);
282 	mt76_testmode_reset(phy->mt76, true);
283 	mutex_unlock(&dev->mt76.mutex);
284 
285 	if (vif == phy->monitor_vif)
286 		phy->monitor_vif = NULL;
287 
288 	mt7915_mcu_add_dev_info(phy, vif, false);
289 
290 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
291 
292 	mutex_lock(&dev->mt76.mutex);
293 	dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
294 	phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
295 	mutex_unlock(&dev->mt76.mutex);
296 
297 	spin_lock_bh(&dev->sta_poll_lock);
298 	if (!list_empty(&msta->poll_list))
299 		list_del_init(&msta->poll_list);
300 	spin_unlock_bh(&dev->sta_poll_lock);
301 
302 	mt76_packet_id_flush(&dev->mt76, &msta->wcid);
303 }
304 
305 int mt7915_set_channel(struct mt7915_phy *phy)
306 {
307 	struct mt7915_dev *dev = phy->dev;
308 	int ret;
309 
310 	cancel_delayed_work_sync(&phy->mt76->mac_work);
311 
312 	mutex_lock(&dev->mt76.mutex);
313 	set_bit(MT76_RESET, &phy->mt76->state);
314 
315 	mt76_set_channel(phy->mt76);
316 
317 	if (dev->flash_mode) {
318 		ret = mt7915_mcu_apply_tx_dpd(phy);
319 		if (ret)
320 			goto out;
321 	}
322 
323 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
324 	if (ret)
325 		goto out;
326 
327 	mt7915_mac_set_timing(phy);
328 	ret = mt7915_dfs_init_radar_detector(phy);
329 	mt7915_mac_cca_stats_reset(phy);
330 
331 	mt7915_mac_reset_counters(phy);
332 	phy->noise = 0;
333 
334 out:
335 	clear_bit(MT76_RESET, &phy->mt76->state);
336 	mutex_unlock(&dev->mt76.mutex);
337 
338 	mt76_txq_schedule_all(phy->mt76);
339 
340 	if (!mt76_testmode_enabled(phy->mt76))
341 		ieee80211_queue_delayed_work(phy->mt76->hw,
342 					     &phy->mt76->mac_work,
343 					     MT7915_WATCHDOG_TIME);
344 
345 	return ret;
346 }
347 
348 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
349 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
350 			  struct ieee80211_key_conf *key)
351 {
352 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
353 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
354 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
355 	struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
356 				  &mvif->sta;
357 	struct mt76_wcid *wcid = &msta->wcid;
358 	u8 *wcid_keyidx = &wcid->hw_key_idx;
359 	int idx = key->keyidx;
360 	int err = 0;
361 
362 	/* The hardware does not support per-STA RX GTK, fallback
363 	 * to software mode for these.
364 	 */
365 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
366 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
367 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
368 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
369 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
370 		return -EOPNOTSUPP;
371 
372 	/* fall back to sw encryption for unsupported ciphers */
373 	switch (key->cipher) {
374 	case WLAN_CIPHER_SUITE_AES_CMAC:
375 		wcid_keyidx = &wcid->hw_key_idx2;
376 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
377 		break;
378 	case WLAN_CIPHER_SUITE_TKIP:
379 	case WLAN_CIPHER_SUITE_CCMP:
380 	case WLAN_CIPHER_SUITE_CCMP_256:
381 	case WLAN_CIPHER_SUITE_GCMP:
382 	case WLAN_CIPHER_SUITE_GCMP_256:
383 	case WLAN_CIPHER_SUITE_SMS4:
384 		break;
385 	case WLAN_CIPHER_SUITE_WEP40:
386 	case WLAN_CIPHER_SUITE_WEP104:
387 	default:
388 		return -EOPNOTSUPP;
389 	}
390 
391 	mutex_lock(&dev->mt76.mutex);
392 
393 	if (cmd == SET_KEY && !sta && !mvif->mt76.cipher) {
394 		mvif->mt76.cipher = mt76_connac_mcu_get_cipher(key->cipher);
395 		mt7915_mcu_add_bss_info(phy, vif, true);
396 	}
397 
398 	if (cmd == SET_KEY)
399 		*wcid_keyidx = idx;
400 	else if (idx == *wcid_keyidx)
401 		*wcid_keyidx = -1;
402 	else
403 		goto out;
404 
405 	mt76_wcid_key_setup(&dev->mt76, wcid,
406 			    cmd == SET_KEY ? key : NULL);
407 
408 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->bip,
409 				      key, MCU_EXT_CMD(STA_REC_UPDATE),
410 				      &msta->wcid, cmd);
411 out:
412 	mutex_unlock(&dev->mt76.mutex);
413 
414 	return err;
415 }
416 
417 static int mt7915_set_sar_specs(struct ieee80211_hw *hw,
418 				const struct cfg80211_sar_specs *sar)
419 {
420 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
421 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
422 	int err = -EINVAL;
423 
424 	mutex_lock(&dev->mt76.mutex);
425 	if (!cfg80211_chandef_valid(&phy->mt76->chandef))
426 		goto out;
427 
428 	err = mt76_init_sar_power(hw, sar);
429 	if (err)
430 		goto out;
431 
432 	err = mt7915_mcu_set_txpower_sku(phy);
433 out:
434 	mutex_unlock(&dev->mt76.mutex);
435 
436 	return err;
437 }
438 
439 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
440 {
441 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
442 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
443 	bool band = phy != &dev->phy;
444 	int ret;
445 
446 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
447 #ifdef CONFIG_NL80211_TESTMODE
448 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
449 			mutex_lock(&dev->mt76.mutex);
450 			mt76_testmode_reset(phy->mt76, false);
451 			mutex_unlock(&dev->mt76.mutex);
452 		}
453 #endif
454 		ieee80211_stop_queues(hw);
455 		ret = mt7915_set_channel(phy);
456 		if (ret)
457 			return ret;
458 		ieee80211_wake_queues(hw);
459 	}
460 
461 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
462 		ret = mt7915_mcu_set_txpower_sku(phy);
463 		if (ret)
464 			return ret;
465 	}
466 
467 	mutex_lock(&dev->mt76.mutex);
468 
469 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
470 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
471 
472 		if (!enabled)
473 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
474 		else
475 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
476 
477 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
478 			       enabled);
479 		mt76_testmode_reset(phy->mt76, true);
480 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
481 	}
482 
483 	mutex_unlock(&dev->mt76.mutex);
484 
485 	return 0;
486 }
487 
488 static int
489 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
490 	       const struct ieee80211_tx_queue_params *params)
491 {
492 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
493 
494 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
495 	queue = mt76_connac_lmac_mapping(queue);
496 	mvif->queue_params[queue] = *params;
497 
498 	return 0;
499 }
500 
501 static void mt7915_configure_filter(struct ieee80211_hw *hw,
502 				    unsigned int changed_flags,
503 				    unsigned int *total_flags,
504 				    u64 multicast)
505 {
506 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
507 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
508 	bool band = phy != &dev->phy;
509 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
510 			MT_WF_RFCR1_DROP_BF_POLL |
511 			MT_WF_RFCR1_DROP_BA |
512 			MT_WF_RFCR1_DROP_CFEND |
513 			MT_WF_RFCR1_DROP_CFACK;
514 	u32 flags = 0;
515 
516 #define MT76_FILTER(_flag, _hw) do {					\
517 		flags |= *total_flags & FIF_##_flag;			\
518 		phy->rxfilter &= ~(_hw);				\
519 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
520 	} while (0)
521 
522 	mutex_lock(&dev->mt76.mutex);
523 
524 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
525 			   MT_WF_RFCR_DROP_OTHER_BEACON |
526 			   MT_WF_RFCR_DROP_FRAME_REPORT |
527 			   MT_WF_RFCR_DROP_PROBEREQ |
528 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
529 			   MT_WF_RFCR_DROP_MCAST |
530 			   MT_WF_RFCR_DROP_BCAST |
531 			   MT_WF_RFCR_DROP_DUPLICATE |
532 			   MT_WF_RFCR_DROP_A2_BSSID |
533 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
534 			   MT_WF_RFCR_DROP_STBC_MULTI);
535 
536 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
537 			       MT_WF_RFCR_DROP_A3_MAC |
538 			       MT_WF_RFCR_DROP_A3_BSSID);
539 
540 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
541 
542 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
543 			     MT_WF_RFCR_DROP_RTS |
544 			     MT_WF_RFCR_DROP_CTL_RSV |
545 			     MT_WF_RFCR_DROP_NDPA);
546 
547 	*total_flags = flags;
548 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
549 
550 	if (*total_flags & FIF_CONTROL)
551 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
552 	else
553 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
554 
555 	mutex_unlock(&dev->mt76.mutex);
556 }
557 
558 static void
559 mt7915_update_bss_color(struct ieee80211_hw *hw,
560 			struct ieee80211_vif *vif,
561 			struct cfg80211_he_bss_color *bss_color)
562 {
563 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
564 
565 	switch (vif->type) {
566 	case NL80211_IFTYPE_AP: {
567 		struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
568 
569 		if (mvif->mt76.omac_idx > HW_BSSID_MAX)
570 			return;
571 		fallthrough;
572 	}
573 	case NL80211_IFTYPE_STATION:
574 		mt7915_mcu_update_bss_color(dev, vif, bss_color);
575 		break;
576 	default:
577 		break;
578 	}
579 }
580 
581 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
582 				    struct ieee80211_vif *vif,
583 				    struct ieee80211_bss_conf *info,
584 				    u32 changed)
585 {
586 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
587 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
588 
589 	mutex_lock(&dev->mt76.mutex);
590 
591 	/*
592 	 * station mode uses BSSID to map the wlan entry to a peer,
593 	 * and then peer references bss_info_rfch to set bandwidth cap.
594 	 */
595 	if (changed & BSS_CHANGED_BSSID &&
596 	    vif->type == NL80211_IFTYPE_STATION) {
597 		bool join = !is_zero_ether_addr(info->bssid);
598 
599 		mt7915_mcu_add_bss_info(phy, vif, join);
600 		mt7915_mcu_add_sta(dev, vif, NULL, join);
601 	}
602 
603 	if (changed & BSS_CHANGED_ASSOC) {
604 		mt7915_mcu_add_bss_info(phy, vif, info->assoc);
605 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
606 	}
607 
608 	if (changed & BSS_CHANGED_ERP_SLOT) {
609 		int slottime = info->use_short_slot ? 9 : 20;
610 
611 		if (slottime != phy->slottime) {
612 			phy->slottime = slottime;
613 			mt7915_mac_set_timing(phy);
614 		}
615 	}
616 
617 	if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
618 		mt7915_mcu_add_bss_info(phy, vif, true);
619 		mt7915_mcu_add_sta(dev, vif, NULL, true);
620 	}
621 
622 	/* ensure that enable txcmd_mode after bss_info */
623 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
624 		mt7915_mcu_set_tx(dev, vif);
625 
626 	if (changed & BSS_CHANGED_HE_OBSS_PD)
627 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
628 
629 	if (changed & BSS_CHANGED_HE_BSS_COLOR)
630 		mt7915_update_bss_color(hw, vif, &info->he_bss_color);
631 
632 	if (changed & (BSS_CHANGED_BEACON |
633 		       BSS_CHANGED_BEACON_ENABLED))
634 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon);
635 
636 	mutex_unlock(&dev->mt76.mutex);
637 }
638 
639 static void
640 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
641 			     struct ieee80211_vif *vif,
642 			     struct cfg80211_chan_def *chandef)
643 {
644 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
645 
646 	mutex_lock(&dev->mt76.mutex);
647 	mt7915_mcu_add_beacon(hw, vif, true);
648 	mutex_unlock(&dev->mt76.mutex);
649 }
650 
651 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
652 		       struct ieee80211_sta *sta)
653 {
654 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
655 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
656 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
657 	int ret, idx;
658 
659 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA);
660 	if (idx < 0)
661 		return -ENOSPC;
662 
663 	INIT_LIST_HEAD(&msta->rc_list);
664 	INIT_LIST_HEAD(&msta->poll_list);
665 	msta->vif = mvif;
666 	msta->wcid.sta = 1;
667 	msta->wcid.idx = idx;
668 	msta->wcid.ext_phy = mvif->mt76.band_idx;
669 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
670 	msta->jiffies = jiffies;
671 
672 	mt7915_mac_wtbl_update(dev, idx,
673 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
674 
675 	ret = mt7915_mcu_add_sta(dev, vif, sta, true);
676 	if (ret)
677 		return ret;
678 
679 	return mt7915_mcu_add_rate_ctrl(dev, vif, sta, false);
680 }
681 
682 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
683 			   struct ieee80211_sta *sta)
684 {
685 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
686 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
687 	int i;
688 
689 	mt7915_mcu_add_sta(dev, vif, sta, false);
690 
691 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
692 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
693 
694 	for (i = 0; i < ARRAY_SIZE(msta->twt.flow); i++)
695 		mt7915_mac_twt_teardown_flow(dev, msta, i);
696 
697 	spin_lock_bh(&dev->sta_poll_lock);
698 	if (!list_empty(&msta->poll_list))
699 		list_del_init(&msta->poll_list);
700 	if (!list_empty(&msta->rc_list))
701 		list_del_init(&msta->rc_list);
702 	spin_unlock_bh(&dev->sta_poll_lock);
703 }
704 
705 static void mt7915_tx(struct ieee80211_hw *hw,
706 		      struct ieee80211_tx_control *control,
707 		      struct sk_buff *skb)
708 {
709 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
710 	struct mt76_phy *mphy = hw->priv;
711 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
712 	struct ieee80211_vif *vif = info->control.vif;
713 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
714 
715 	if (control->sta) {
716 		struct mt7915_sta *sta;
717 
718 		sta = (struct mt7915_sta *)control->sta->drv_priv;
719 		wcid = &sta->wcid;
720 	}
721 
722 	if (vif && !control->sta) {
723 		struct mt7915_vif *mvif;
724 
725 		mvif = (struct mt7915_vif *)vif->drv_priv;
726 		wcid = &mvif->sta.wcid;
727 	}
728 
729 	mt76_tx(mphy, control->sta, wcid, skb);
730 }
731 
732 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
733 {
734 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
735 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
736 	int ret;
737 
738 	mutex_lock(&dev->mt76.mutex);
739 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, phy != &dev->phy);
740 	mutex_unlock(&dev->mt76.mutex);
741 
742 	return ret;
743 }
744 
745 static int
746 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
747 		    struct ieee80211_ampdu_params *params)
748 {
749 	enum ieee80211_ampdu_mlme_action action = params->action;
750 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
751 	struct ieee80211_sta *sta = params->sta;
752 	struct ieee80211_txq *txq = sta->txq[params->tid];
753 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
754 	u16 tid = params->tid;
755 	u16 ssn = params->ssn;
756 	struct mt76_txq *mtxq;
757 	int ret = 0;
758 
759 	if (!txq)
760 		return -EINVAL;
761 
762 	mtxq = (struct mt76_txq *)txq->drv_priv;
763 
764 	mutex_lock(&dev->mt76.mutex);
765 	switch (action) {
766 	case IEEE80211_AMPDU_RX_START:
767 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
768 				   params->buf_size);
769 		ret = mt7915_mcu_add_rx_ba(dev, params, true);
770 		break;
771 	case IEEE80211_AMPDU_RX_STOP:
772 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
773 		ret = mt7915_mcu_add_rx_ba(dev, params, false);
774 		break;
775 	case IEEE80211_AMPDU_TX_OPERATIONAL:
776 		mtxq->aggr = true;
777 		mtxq->send_bar = false;
778 		ret = mt7915_mcu_add_tx_ba(dev, params, true);
779 		break;
780 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
781 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
782 		mtxq->aggr = false;
783 		clear_bit(tid, &msta->ampdu_state);
784 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
785 		break;
786 	case IEEE80211_AMPDU_TX_START:
787 		set_bit(tid, &msta->ampdu_state);
788 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
789 		break;
790 	case IEEE80211_AMPDU_TX_STOP_CONT:
791 		mtxq->aggr = false;
792 		clear_bit(tid, &msta->ampdu_state);
793 		ret = mt7915_mcu_add_tx_ba(dev, params, false);
794 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
795 		break;
796 	}
797 	mutex_unlock(&dev->mt76.mutex);
798 
799 	return ret;
800 }
801 
802 static int
803 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
804 	       struct ieee80211_sta *sta)
805 {
806 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
807 			      IEEE80211_STA_NONE);
808 }
809 
810 static int
811 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
812 		  struct ieee80211_sta *sta)
813 {
814 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
815 			      IEEE80211_STA_NOTEXIST);
816 }
817 
818 static int
819 mt7915_get_stats(struct ieee80211_hw *hw,
820 		 struct ieee80211_low_level_stats *stats)
821 {
822 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
823 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
824 	struct mib_stats *mib = &phy->mib;
825 
826 	mutex_lock(&dev->mt76.mutex);
827 
828 	stats->dot11RTSSuccessCount = mib->rts_cnt;
829 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
830 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
831 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
832 
833 	mutex_unlock(&dev->mt76.mutex);
834 
835 	return 0;
836 }
837 
838 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif)
839 {
840 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
841 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
842 	bool band = phy != &dev->phy;
843 	union {
844 		u64 t64;
845 		u32 t32[2];
846 	} tsf;
847 	u16 n;
848 
849 	lockdep_assert_held(&dev->mt76.mutex);
850 
851 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
852 					       : mvif->mt76.omac_idx;
853 	/* TSF software read */
854 	if (is_mt7915(&dev->mt76))
855 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
856 			 MT_LPON_TCR_SW_READ);
857 	else
858 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
859 			 MT_LPON_TCR_SW_READ);
860 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
861 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
862 
863 	return tsf.t64;
864 }
865 
866 static u64
867 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
868 {
869 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
870 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
871 	u64 ret;
872 
873 	mutex_lock(&dev->mt76.mutex);
874 	ret = __mt7915_get_tsf(hw, mvif);
875 	mutex_unlock(&dev->mt76.mutex);
876 
877 	return ret;
878 }
879 
880 static void
881 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
882 	       u64 timestamp)
883 {
884 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
885 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
886 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
887 	bool band = phy != &dev->phy;
888 	union {
889 		u64 t64;
890 		u32 t32[2];
891 	} tsf = { .t64 = timestamp, };
892 	u16 n;
893 
894 	mutex_lock(&dev->mt76.mutex);
895 
896 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
897 					       : mvif->mt76.omac_idx;
898 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
899 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
900 	/* TSF software overwrite */
901 	if (is_mt7915(&dev->mt76))
902 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
903 			 MT_LPON_TCR_SW_WRITE);
904 	else
905 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
906 			 MT_LPON_TCR_SW_WRITE);
907 
908 	mutex_unlock(&dev->mt76.mutex);
909 }
910 
911 static void
912 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
913 		  s64 timestamp)
914 {
915 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
916 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
917 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
918 	bool band = phy != &dev->phy;
919 	union {
920 		u64 t64;
921 		u32 t32[2];
922 	} tsf = { .t64 = timestamp, };
923 	u16 n;
924 
925 	mutex_lock(&dev->mt76.mutex);
926 
927 	n = mvif->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
928 					       : mvif->mt76.omac_idx;
929 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
930 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
931 	/* TSF software adjust*/
932 	if (is_mt7915(&dev->mt76))
933 		mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
934 			 MT_LPON_TCR_SW_ADJUST);
935 	else
936 		mt76_rmw(dev, MT_LPON_TCR_MT7916(band, n), MT_LPON_TCR_SW_MODE,
937 			 MT_LPON_TCR_SW_ADJUST);
938 
939 	mutex_unlock(&dev->mt76.mutex);
940 }
941 
942 static void
943 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
944 {
945 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
946 	struct mt7915_dev *dev = phy->dev;
947 
948 	mutex_lock(&dev->mt76.mutex);
949 	phy->coverage_class = max_t(s16, coverage_class, 0);
950 	mt7915_mac_set_timing(phy);
951 	mutex_unlock(&dev->mt76.mutex);
952 }
953 
954 static int
955 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
956 {
957 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
958 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
959 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
960 	bool ext_phy = phy != &dev->phy;
961 
962 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
963 		return -EINVAL;
964 
965 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
966 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
967 
968 	mutex_lock(&dev->mt76.mutex);
969 
970 	phy->mt76->antenna_mask = tx_ant;
971 
972 	if (ext_phy) {
973 		if (dev->chainmask == 0xf)
974 			tx_ant <<= 2;
975 		else
976 			tx_ant <<= 1;
977 	}
978 	phy->mt76->chainmask = tx_ant;
979 
980 	mt76_set_stream_caps(phy->mt76, true);
981 	mt7915_set_stream_vht_txbf_caps(phy);
982 	mt7915_set_stream_he_caps(phy);
983 
984 	mutex_unlock(&dev->mt76.mutex);
985 
986 	return 0;
987 }
988 
989 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
990 				  struct ieee80211_vif *vif,
991 				  struct ieee80211_sta *sta,
992 				  struct station_info *sinfo)
993 {
994 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
995 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
996 	struct rate_info *txrate = &msta->wcid.rate;
997 	struct rate_info rxrate = {};
998 
999 	if (is_mt7915(&phy->dev->mt76) &&
1000 	    !mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
1001 		sinfo->rxrate = rxrate;
1002 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
1003 	}
1004 
1005 	if (!txrate->legacy && !txrate->flags)
1006 		return;
1007 
1008 	if (txrate->legacy) {
1009 		sinfo->txrate.legacy = txrate->legacy;
1010 	} else {
1011 		sinfo->txrate.mcs = txrate->mcs;
1012 		sinfo->txrate.nss = txrate->nss;
1013 		sinfo->txrate.bw = txrate->bw;
1014 		sinfo->txrate.he_gi = txrate->he_gi;
1015 		sinfo->txrate.he_dcm = txrate->he_dcm;
1016 		sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc;
1017 	}
1018 	sinfo->txrate.flags = txrate->flags;
1019 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1020 }
1021 
1022 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
1023 {
1024 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1025 	struct mt7915_dev *dev = msta->vif->phy->dev;
1026 	u32 *changed = data;
1027 
1028 	spin_lock_bh(&dev->sta_poll_lock);
1029 	msta->changed |= *changed;
1030 	if (list_empty(&msta->rc_list))
1031 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
1032 	spin_unlock_bh(&dev->sta_poll_lock);
1033 }
1034 
1035 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
1036 				 struct ieee80211_vif *vif,
1037 				 struct ieee80211_sta *sta,
1038 				 u32 changed)
1039 {
1040 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1041 	struct mt7915_dev *dev = phy->dev;
1042 
1043 	mt7915_sta_rc_work(&changed, sta);
1044 	ieee80211_queue_work(hw, &dev->rc_work);
1045 }
1046 
1047 static int
1048 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1049 			const struct cfg80211_bitrate_mask *mask)
1050 {
1051 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1052 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1053 	struct mt7915_dev *dev = phy->dev;
1054 	u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1055 
1056 	mvif->bitrate_mask = *mask;
1057 
1058 	/* if multiple rates across different preambles are given we can
1059 	 * reconfigure this info with all peers using sta_rec command with
1060 	 * the below exception cases.
1061 	 * - single rate : if a rate is passed along with different preambles,
1062 	 * we select the highest one as fixed rate. i.e VHT MCS for VHT peers.
1063 	 * - multiple rates: if it's not in range format i.e 0-{7,8,9} for VHT
1064 	 * then multiple MCS setting (MCS 4,5,6) is not supported.
1065 	 */
1066 	ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1067 	ieee80211_queue_work(hw, &dev->rc_work);
1068 
1069 	return 0;
1070 }
1071 
1072 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1073 				 struct ieee80211_vif *vif,
1074 				 struct ieee80211_sta *sta,
1075 				 bool enabled)
1076 {
1077 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1078 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1079 
1080 	if (enabled)
1081 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1082 	else
1083 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1084 
1085 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1086 }
1087 
1088 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1089 				 struct ieee80211_vif *vif,
1090 				 struct ieee80211_sta *sta,
1091 				 bool enabled)
1092 {
1093 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1094 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1095 
1096 	if (enabled)
1097 		set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1098 	else
1099 		clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1100 
1101 	mt76_connac_mcu_wtbl_update_hdr_trans(&dev->mt76, vif, sta);
1102 }
1103 
1104 static const char mt7915_gstrings_stats[][ETH_GSTRING_LEN] = {
1105 	"tx_ampdu_cnt",
1106 	"tx_stop_q_empty_cnt",
1107 	"tx_mpdu_attempts",
1108 	"tx_mpdu_success",
1109 	"tx_rwp_fail_cnt",
1110 	"tx_rwp_need_cnt",
1111 	"tx_pkt_ebf_cnt",
1112 	"tx_pkt_ibf_cnt",
1113 	"tx_ampdu_len:0-1",
1114 	"tx_ampdu_len:2-10",
1115 	"tx_ampdu_len:11-19",
1116 	"tx_ampdu_len:20-28",
1117 	"tx_ampdu_len:29-37",
1118 	"tx_ampdu_len:38-46",
1119 	"tx_ampdu_len:47-55",
1120 	"tx_ampdu_len:56-79",
1121 	"tx_ampdu_len:80-103",
1122 	"tx_ampdu_len:104-127",
1123 	"tx_ampdu_len:128-151",
1124 	"tx_ampdu_len:152-175",
1125 	"tx_ampdu_len:176-199",
1126 	"tx_ampdu_len:200-223",
1127 	"tx_ampdu_len:224-247",
1128 	"ba_miss_count",
1129 	"tx_beamformer_ppdu_iBF",
1130 	"tx_beamformer_ppdu_eBF",
1131 	"tx_beamformer_rx_feedback_all",
1132 	"tx_beamformer_rx_feedback_he",
1133 	"tx_beamformer_rx_feedback_vht",
1134 	"tx_beamformer_rx_feedback_ht",
1135 	"tx_beamformer_rx_feedback_bw", /* zero based idx: 20, 40, 80, 160 */
1136 	"tx_beamformer_rx_feedback_nc",
1137 	"tx_beamformer_rx_feedback_nr",
1138 	"tx_beamformee_ok_feedback_pkts",
1139 	"tx_beamformee_feedback_trig",
1140 	"tx_mu_beamforming",
1141 	"tx_mu_mpdu",
1142 	"tx_mu_successful_mpdu",
1143 	"tx_su_successful_mpdu",
1144 	"tx_msdu_pack_1",
1145 	"tx_msdu_pack_2",
1146 	"tx_msdu_pack_3",
1147 	"tx_msdu_pack_4",
1148 	"tx_msdu_pack_5",
1149 	"tx_msdu_pack_6",
1150 	"tx_msdu_pack_7",
1151 	"tx_msdu_pack_8",
1152 
1153 	/* rx counters */
1154 	"rx_fifo_full_cnt",
1155 	"rx_mpdu_cnt",
1156 	"channel_idle_cnt",
1157 	"rx_vector_mismatch_cnt",
1158 	"rx_delimiter_fail_cnt",
1159 	"rx_len_mismatch_cnt",
1160 	"rx_ampdu_cnt",
1161 	"rx_ampdu_bytes_cnt",
1162 	"rx_ampdu_valid_subframe_cnt",
1163 	"rx_ampdu_valid_subframe_b_cnt",
1164 	"rx_pfdrop_cnt",
1165 	"rx_vec_queue_overflow_drop_cnt",
1166 	"rx_ba_cnt",
1167 
1168 	/* per vif counters */
1169 	"v_tx_mode_cck",
1170 	"v_tx_mode_ofdm",
1171 	"v_tx_mode_ht",
1172 	"v_tx_mode_ht_gf",
1173 	"v_tx_mode_vht",
1174 	"v_tx_mode_he_su",
1175 	"v_tx_mode_he_ext_su",
1176 	"v_tx_mode_he_tb",
1177 	"v_tx_mode_he_mu",
1178 	"v_tx_bw_20",
1179 	"v_tx_bw_40",
1180 	"v_tx_bw_80",
1181 	"v_tx_bw_160",
1182 	"v_tx_mcs_0",
1183 	"v_tx_mcs_1",
1184 	"v_tx_mcs_2",
1185 	"v_tx_mcs_3",
1186 	"v_tx_mcs_4",
1187 	"v_tx_mcs_5",
1188 	"v_tx_mcs_6",
1189 	"v_tx_mcs_7",
1190 	"v_tx_mcs_8",
1191 	"v_tx_mcs_9",
1192 	"v_tx_mcs_10",
1193 	"v_tx_mcs_11",
1194 };
1195 
1196 #define MT7915_SSTATS_LEN ARRAY_SIZE(mt7915_gstrings_stats)
1197 
1198 /* Ethtool related API */
1199 static
1200 void mt7915_get_et_strings(struct ieee80211_hw *hw,
1201 			   struct ieee80211_vif *vif,
1202 			   u32 sset, u8 *data)
1203 {
1204 	if (sset == ETH_SS_STATS)
1205 		memcpy(data, *mt7915_gstrings_stats,
1206 		       sizeof(mt7915_gstrings_stats));
1207 }
1208 
1209 static
1210 int mt7915_get_et_sset_count(struct ieee80211_hw *hw,
1211 			     struct ieee80211_vif *vif, int sset)
1212 {
1213 	if (sset == ETH_SS_STATS)
1214 		return MT7915_SSTATS_LEN;
1215 
1216 	return 0;
1217 }
1218 
1219 static void mt7915_ethtool_worker(void *wi_data, struct ieee80211_sta *sta)
1220 {
1221 	struct mt76_ethtool_worker_info *wi = wi_data;
1222 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1223 
1224 	if (msta->vif->mt76.idx != wi->idx)
1225 		return;
1226 
1227 	mt76_ethtool_worker(wi, &msta->stats);
1228 }
1229 
1230 static
1231 void mt7915_get_et_stats(struct ieee80211_hw *hw,
1232 			 struct ieee80211_vif *vif,
1233 			 struct ethtool_stats *stats, u64 *data)
1234 {
1235 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1236 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1237 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
1238 	struct mt76_ethtool_worker_info wi = {
1239 		.data = data,
1240 		.idx = mvif->mt76.idx,
1241 	};
1242 	struct mib_stats *mib = &phy->mib;
1243 	/* See mt7915_ampdu_stat_read_phy, etc */
1244 	bool ext_phy = phy != &dev->phy;
1245 	int i, n, ei = 0;
1246 
1247 	mutex_lock(&dev->mt76.mutex);
1248 
1249 	mt7915_mac_update_stats(phy);
1250 
1251 	data[ei++] = mib->tx_ampdu_cnt;
1252 	data[ei++] = mib->tx_stop_q_empty_cnt;
1253 	data[ei++] = mib->tx_mpdu_attempts_cnt;
1254 	data[ei++] = mib->tx_mpdu_success_cnt;
1255 	data[ei++] = mib->tx_rwp_fail_cnt;
1256 	data[ei++] = mib->tx_rwp_need_cnt;
1257 	data[ei++] = mib->tx_pkt_ebf_cnt;
1258 	data[ei++] = mib->tx_pkt_ibf_cnt;
1259 
1260 	/* Tx ampdu stat */
1261 	n = ext_phy ? ARRAY_SIZE(dev->mt76.aggr_stats) / 2 : 0;
1262 	for (i = 0; i < 15 /*ARRAY_SIZE(bound)*/; i++)
1263 		data[ei++] = dev->mt76.aggr_stats[i + n];
1264 
1265 	data[ei++] = phy->mib.ba_miss_cnt;
1266 
1267 	/* Tx Beamformer monitor */
1268 	data[ei++] = mib->tx_bf_ibf_ppdu_cnt;
1269 	data[ei++] = mib->tx_bf_ebf_ppdu_cnt;
1270 
1271 	/* Tx Beamformer Rx feedback monitor */
1272 	data[ei++] = mib->tx_bf_rx_fb_all_cnt;
1273 	data[ei++] = mib->tx_bf_rx_fb_he_cnt;
1274 	data[ei++] = mib->tx_bf_rx_fb_vht_cnt;
1275 	data[ei++] = mib->tx_bf_rx_fb_ht_cnt;
1276 
1277 	data[ei++] = mib->tx_bf_rx_fb_bw;
1278 	data[ei++] = mib->tx_bf_rx_fb_nc_cnt;
1279 	data[ei++] = mib->tx_bf_rx_fb_nr_cnt;
1280 
1281 	/* Tx Beamformee Rx NDPA & Tx feedback report */
1282 	data[ei++] = mib->tx_bf_fb_cpl_cnt;
1283 	data[ei++] = mib->tx_bf_fb_trig_cnt;
1284 
1285 	/* Tx SU & MU counters */
1286 	data[ei++] = mib->tx_bf_cnt;
1287 	data[ei++] = mib->tx_mu_mpdu_cnt;
1288 	data[ei++] = mib->tx_mu_acked_mpdu_cnt;
1289 	data[ei++] = mib->tx_su_acked_mpdu_cnt;
1290 
1291 	/* Tx amsdu info (pack-count histogram) */
1292 	for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++)
1293 		data[ei++] = mib->tx_amsdu[i];
1294 
1295 	/* rx counters */
1296 	data[ei++] = mib->rx_fifo_full_cnt;
1297 	data[ei++] = mib->rx_mpdu_cnt;
1298 	data[ei++] = mib->channel_idle_cnt;
1299 	data[ei++] = mib->rx_vector_mismatch_cnt;
1300 	data[ei++] = mib->rx_delimiter_fail_cnt;
1301 	data[ei++] = mib->rx_len_mismatch_cnt;
1302 	data[ei++] = mib->rx_ampdu_cnt;
1303 	data[ei++] = mib->rx_ampdu_bytes_cnt;
1304 	data[ei++] = mib->rx_ampdu_valid_subframe_cnt;
1305 	data[ei++] = mib->rx_ampdu_valid_subframe_bytes_cnt;
1306 	data[ei++] = mib->rx_pfdrop_cnt;
1307 	data[ei++] = mib->rx_vec_queue_overflow_drop_cnt;
1308 	data[ei++] = mib->rx_ba_cnt;
1309 
1310 	/* Add values for all stations owned by this vif */
1311 	wi.initial_stat_idx = ei;
1312 	ieee80211_iterate_stations_atomic(hw, mt7915_ethtool_worker, &wi);
1313 
1314 	mutex_unlock(&dev->mt76.mutex);
1315 
1316 	if (wi.sta_count == 0)
1317 		return;
1318 
1319 	ei += wi.worker_stat_count;
1320 	if (ei != MT7915_SSTATS_LEN)
1321 		dev_err(dev->mt76.dev, "ei: %d  MT7915_SSTATS_LEN: %d",
1322 			ei, (int)MT7915_SSTATS_LEN);
1323 }
1324 
1325 static void
1326 mt7915_twt_teardown_request(struct ieee80211_hw *hw,
1327 			    struct ieee80211_sta *sta,
1328 			    u8 flowid)
1329 {
1330 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1331 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
1332 
1333 	mutex_lock(&dev->mt76.mutex);
1334 	mt7915_mac_twt_teardown_flow(dev, msta, flowid);
1335 	mutex_unlock(&dev->mt76.mutex);
1336 }
1337 
1338 static int
1339 mt7915_set_radar_background(struct ieee80211_hw *hw,
1340 			    struct cfg80211_chan_def *chandef)
1341 {
1342 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
1343 	struct mt7915_dev *dev = phy->dev;
1344 	int ret = -EINVAL;
1345 	bool running;
1346 
1347 	mutex_lock(&dev->mt76.mutex);
1348 
1349 	if (dev->mt76.region == NL80211_DFS_UNSET)
1350 		goto out;
1351 
1352 	if (dev->rdd2_phy && dev->rdd2_phy != phy) {
1353 		/* rdd2 is already locked */
1354 		ret = -EBUSY;
1355 		goto out;
1356 	}
1357 
1358 	/* rdd2 already configured on a radar channel */
1359 	running = dev->rdd2_phy &&
1360 		  cfg80211_chandef_valid(&dev->rdd2_chandef) &&
1361 		  !!(dev->rdd2_chandef.chan->flags & IEEE80211_CHAN_RADAR);
1362 
1363 	if (!chandef || running ||
1364 	    !(chandef->chan->flags & IEEE80211_CHAN_RADAR)) {
1365 		ret = mt7915_mcu_rdd_background_enable(phy, NULL);
1366 		if (ret)
1367 			goto out;
1368 
1369 		if (!running)
1370 			goto update_phy;
1371 	}
1372 
1373 	ret = mt7915_mcu_rdd_background_enable(phy, chandef);
1374 	if (ret)
1375 		goto out;
1376 
1377 update_phy:
1378 	dev->rdd2_phy = chandef ? phy : NULL;
1379 	if (chandef)
1380 		dev->rdd2_chandef = *chandef;
1381 out:
1382 	mutex_unlock(&dev->mt76.mutex);
1383 
1384 	return ret;
1385 }
1386 
1387 const struct ieee80211_ops mt7915_ops = {
1388 	.tx = mt7915_tx,
1389 	.start = mt7915_start,
1390 	.stop = mt7915_stop,
1391 	.add_interface = mt7915_add_interface,
1392 	.remove_interface = mt7915_remove_interface,
1393 	.config = mt7915_config,
1394 	.conf_tx = mt7915_conf_tx,
1395 	.configure_filter = mt7915_configure_filter,
1396 	.bss_info_changed = mt7915_bss_info_changed,
1397 	.sta_add = mt7915_sta_add,
1398 	.sta_remove = mt7915_sta_remove,
1399 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1400 	.sta_rc_update = mt7915_sta_rc_update,
1401 	.set_key = mt7915_set_key,
1402 	.ampdu_action = mt7915_ampdu_action,
1403 	.set_rts_threshold = mt7915_set_rts_threshold,
1404 	.wake_tx_queue = mt76_wake_tx_queue,
1405 	.sw_scan_start = mt76_sw_scan,
1406 	.sw_scan_complete = mt76_sw_scan_complete,
1407 	.release_buffered_frames = mt76_release_buffered_frames,
1408 	.get_txpower = mt76_get_txpower,
1409 	.set_sar_specs = mt7915_set_sar_specs,
1410 	.channel_switch_beacon = mt7915_channel_switch_beacon,
1411 	.get_stats = mt7915_get_stats,
1412 	.get_et_sset_count = mt7915_get_et_sset_count,
1413 	.get_et_stats = mt7915_get_et_stats,
1414 	.get_et_strings = mt7915_get_et_strings,
1415 	.get_tsf = mt7915_get_tsf,
1416 	.set_tsf = mt7915_set_tsf,
1417 	.offset_tsf = mt7915_offset_tsf,
1418 	.get_survey = mt76_get_survey,
1419 	.get_antenna = mt76_get_antenna,
1420 	.set_antenna = mt7915_set_antenna,
1421 	.set_bitrate_mask = mt7915_set_bitrate_mask,
1422 	.set_coverage_class = mt7915_set_coverage_class,
1423 	.sta_statistics = mt7915_sta_statistics,
1424 	.sta_set_4addr = mt7915_sta_set_4addr,
1425 	.sta_set_decap_offload = mt7915_sta_set_decap_offload,
1426 	.add_twt_setup = mt7915_mac_add_twt_setup,
1427 	.twt_teardown_request = mt7915_twt_teardown_request,
1428 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1429 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1430 #ifdef CONFIG_MAC80211_DEBUGFS
1431 	.sta_add_debugfs = mt7915_sta_add_debugfs,
1432 #endif
1433 	.set_radar_background = mt7915_set_radar_background,
1434 };
1435