xref: /linux/drivers/net/wireless/mediatek/mt76/mt7915/main.c (revision 0b8061c340b643e01da431dd60c75a41bb1d31ec)
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 
29 	flush_work(&dev->init_work);
30 
31 	mutex_lock(&dev->mt76.mutex);
32 
33 	running = mt7915_dev_running(dev);
34 
35 	if (!running) {
36 		mt7915_mcu_set_pm(dev, 0, 0);
37 		mt7915_mcu_set_mac(dev, 0, true, false);
38 		mt7915_mcu_set_scs(dev, 0, true);
39 		mt7915_mac_enable_nf(dev, 0);
40 	}
41 
42 	if (phy != &dev->phy) {
43 		mt7915_mcu_set_pm(dev, 1, 0);
44 		mt7915_mcu_set_mac(dev, 1, true, false);
45 		mt7915_mcu_set_scs(dev, 1, true);
46 		mt7915_mac_enable_nf(dev, 1);
47 	}
48 
49 	mt7915_mcu_set_sku_en(phy, true);
50 	mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
51 
52 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
53 
54 	if (!mt76_testmode_enabled(phy->mt76))
55 		ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
56 					     MT7915_WATCHDOG_TIME);
57 
58 	if (!running)
59 		mt7915_mac_reset_counters(phy);
60 
61 	mutex_unlock(&dev->mt76.mutex);
62 
63 	return 0;
64 }
65 
66 static void mt7915_stop(struct ieee80211_hw *hw)
67 {
68 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
69 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
70 
71 	cancel_delayed_work_sync(&phy->mt76->mac_work);
72 
73 	mutex_lock(&dev->mt76.mutex);
74 
75 	mt76_testmode_reset(phy->mt76, true);
76 
77 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
78 
79 	if (phy != &dev->phy) {
80 		mt7915_mcu_set_pm(dev, 1, 1);
81 		mt7915_mcu_set_mac(dev, 1, false, false);
82 	}
83 
84 	if (!mt7915_dev_running(dev)) {
85 		mt7915_mcu_set_pm(dev, 0, 1);
86 		mt7915_mcu_set_mac(dev, 0, false, false);
87 	}
88 
89 	mutex_unlock(&dev->mt76.mutex);
90 }
91 
92 static inline int get_free_idx(u32 mask, u8 start, u8 end)
93 {
94 	return ffs(~mask & GENMASK(end, start));
95 }
96 
97 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
98 {
99 	int i;
100 
101 	switch (type) {
102 	case NL80211_IFTYPE_MESH_POINT:
103 	case NL80211_IFTYPE_ADHOC:
104 	case NL80211_IFTYPE_STATION:
105 		/* prefer hw bssid slot 1-3 */
106 		i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
107 		if (i)
108 			return i - 1;
109 
110 		if (type != NL80211_IFTYPE_STATION)
111 			break;
112 
113 		/* next, try to find a free repeater entry for the sta */
114 		i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
115 				 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
116 		if (i)
117 			return i + 32 - 1;
118 
119 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
120 		if (i)
121 			return i - 1;
122 
123 		if (~mask & BIT(HW_BSSID_0))
124 			return HW_BSSID_0;
125 
126 		break;
127 	case NL80211_IFTYPE_MONITOR:
128 	case NL80211_IFTYPE_AP:
129 		/* ap uses hw bssid 0 and ext bssid */
130 		if (~mask & BIT(HW_BSSID_0))
131 			return HW_BSSID_0;
132 
133 		i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
134 		if (i)
135 			return i - 1;
136 
137 		break;
138 	default:
139 		WARN_ON(1);
140 		break;
141 	}
142 
143 	return -1;
144 }
145 
146 static int mt7915_add_interface(struct ieee80211_hw *hw,
147 				struct ieee80211_vif *vif)
148 {
149 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
150 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
151 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
152 	struct mt76_txq *mtxq;
153 	bool ext_phy = phy != &dev->phy;
154 	int idx, ret = 0;
155 
156 	mutex_lock(&dev->mt76.mutex);
157 
158 	mt76_testmode_reset(phy->mt76, true);
159 
160 	if (vif->type == NL80211_IFTYPE_MONITOR &&
161 	    is_zero_ether_addr(vif->addr))
162 		phy->monitor_vif = vif;
163 
164 	mvif->idx = ffs(~dev->mt76.vif_mask) - 1;
165 	if (mvif->idx >= MT7915_MAX_INTERFACES) {
166 		ret = -ENOSPC;
167 		goto out;
168 	}
169 
170 	idx = get_omac_idx(vif->type, phy->omac_mask);
171 	if (idx < 0) {
172 		ret = -ENOSPC;
173 		goto out;
174 	}
175 	mvif->omac_idx = idx;
176 	mvif->phy = phy;
177 	mvif->band_idx = ext_phy;
178 
179 	if (ext_phy)
180 		mvif->wmm_idx = ext_phy * (MT7915_MAX_WMM_SETS / 2) +
181 				mvif->idx % (MT7915_MAX_WMM_SETS / 2);
182 	else
183 		mvif->wmm_idx = mvif->idx % MT7915_MAX_WMM_SETS;
184 
185 	ret = mt7915_mcu_add_dev_info(phy, vif, true);
186 	if (ret)
187 		goto out;
188 
189 	dev->mt76.vif_mask |= BIT(mvif->idx);
190 	phy->omac_mask |= BIT_ULL(mvif->omac_idx);
191 
192 	idx = MT7915_WTBL_RESERVED - mvif->idx;
193 
194 	INIT_LIST_HEAD(&mvif->sta.rc_list);
195 	INIT_LIST_HEAD(&mvif->sta.stats_list);
196 	INIT_LIST_HEAD(&mvif->sta.poll_list);
197 	mvif->sta.wcid.idx = idx;
198 	mvif->sta.wcid.ext_phy = mvif->band_idx;
199 	mvif->sta.wcid.hw_key_idx = -1;
200 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
201 	mt7915_mac_wtbl_update(dev, idx,
202 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
203 
204 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
205 	if (vif->txq) {
206 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
207 		mtxq->wcid = &mvif->sta.wcid;
208 	}
209 
210 	if (vif->type != NL80211_IFTYPE_AP &&
211 	    (!mvif->omac_idx || mvif->omac_idx > 3))
212 		vif->offload_flags = 0;
213 	vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
214 
215 out:
216 	mutex_unlock(&dev->mt76.mutex);
217 
218 	return ret;
219 }
220 
221 static void mt7915_remove_interface(struct ieee80211_hw *hw,
222 				    struct ieee80211_vif *vif)
223 {
224 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
225 	struct mt7915_sta *msta = &mvif->sta;
226 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
227 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
228 	int idx = msta->wcid.idx;
229 
230 	/* TODO: disable beacon for the bss */
231 
232 	mutex_lock(&dev->mt76.mutex);
233 	mt76_testmode_reset(phy->mt76, true);
234 	mutex_unlock(&dev->mt76.mutex);
235 
236 	if (vif == phy->monitor_vif)
237 		phy->monitor_vif = NULL;
238 
239 	mt7915_mcu_add_dev_info(phy, vif, false);
240 
241 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
242 
243 	mutex_lock(&dev->mt76.mutex);
244 	dev->mt76.vif_mask &= ~BIT(mvif->idx);
245 	phy->omac_mask &= ~BIT_ULL(mvif->omac_idx);
246 	mutex_unlock(&dev->mt76.mutex);
247 
248 	spin_lock_bh(&dev->sta_poll_lock);
249 	if (!list_empty(&msta->poll_list))
250 		list_del_init(&msta->poll_list);
251 	spin_unlock_bh(&dev->sta_poll_lock);
252 }
253 
254 static void mt7915_init_dfs_state(struct mt7915_phy *phy)
255 {
256 	struct mt76_phy *mphy = phy->mt76;
257 	struct ieee80211_hw *hw = mphy->hw;
258 	struct cfg80211_chan_def *chandef = &hw->conf.chandef;
259 
260 	if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
261 		return;
262 
263 	if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
264 		return;
265 
266 	if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
267 	    mphy->chandef.width == chandef->width)
268 		return;
269 
270 	phy->dfs_state = -1;
271 }
272 
273 int mt7915_set_channel(struct mt7915_phy *phy)
274 {
275 	struct mt7915_dev *dev = phy->dev;
276 	int ret;
277 
278 	cancel_delayed_work_sync(&phy->mt76->mac_work);
279 
280 	mutex_lock(&dev->mt76.mutex);
281 	set_bit(MT76_RESET, &phy->mt76->state);
282 
283 	mt7915_init_dfs_state(phy);
284 	mt76_set_channel(phy->mt76);
285 
286 	ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
287 	if (ret)
288 		goto out;
289 
290 	mt7915_mac_set_timing(phy);
291 	ret = mt7915_dfs_init_radar_detector(phy);
292 	mt7915_mac_cca_stats_reset(phy);
293 
294 	mt7915_mac_reset_counters(phy);
295 	phy->noise = 0;
296 
297 out:
298 	clear_bit(MT76_RESET, &phy->mt76->state);
299 	mutex_unlock(&dev->mt76.mutex);
300 
301 	mt76_txq_schedule_all(phy->mt76);
302 
303 	if (!mt76_testmode_enabled(phy->mt76))
304 		ieee80211_queue_delayed_work(phy->mt76->hw,
305 					     &phy->mt76->mac_work,
306 					     MT7915_WATCHDOG_TIME);
307 
308 	return ret;
309 }
310 
311 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
312 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
313 			  struct ieee80211_key_conf *key)
314 {
315 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
316 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
317 	struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
318 				  &mvif->sta;
319 	struct mt76_wcid *wcid = &msta->wcid;
320 	int idx = key->keyidx;
321 
322 	/* The hardware does not support per-STA RX GTK, fallback
323 	 * to software mode for these.
324 	 */
325 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
326 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
327 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
328 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
329 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
330 		return -EOPNOTSUPP;
331 
332 	/* fall back to sw encryption for unsupported ciphers */
333 	switch (key->cipher) {
334 	case WLAN_CIPHER_SUITE_AES_CMAC:
335 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
336 		break;
337 	case WLAN_CIPHER_SUITE_TKIP:
338 	case WLAN_CIPHER_SUITE_CCMP:
339 	case WLAN_CIPHER_SUITE_CCMP_256:
340 	case WLAN_CIPHER_SUITE_GCMP:
341 	case WLAN_CIPHER_SUITE_GCMP_256:
342 	case WLAN_CIPHER_SUITE_SMS4:
343 		break;
344 	case WLAN_CIPHER_SUITE_WEP40:
345 	case WLAN_CIPHER_SUITE_WEP104:
346 	default:
347 		return -EOPNOTSUPP;
348 	}
349 
350 	if (cmd == SET_KEY) {
351 		key->hw_key_idx = wcid->idx;
352 		wcid->hw_key_idx = idx;
353 	} else if (idx == wcid->hw_key_idx) {
354 		wcid->hw_key_idx = -1;
355 	}
356 	mt76_wcid_key_setup(&dev->mt76, wcid,
357 			    cmd == SET_KEY ? key : NULL);
358 
359 	return mt7915_mcu_add_key(dev, vif, msta, key, cmd);
360 }
361 
362 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
363 {
364 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
365 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
366 	bool band = phy != &dev->phy;
367 	int ret;
368 
369 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
370 #ifdef CONFIG_NL80211_TESTMODE
371 		if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
372 			mutex_lock(&dev->mt76.mutex);
373 			mt76_testmode_reset(phy->mt76, false);
374 			mutex_unlock(&dev->mt76.mutex);
375 		}
376 #endif
377 		ieee80211_stop_queues(hw);
378 		ret = mt7915_set_channel(phy);
379 		if (ret)
380 			return ret;
381 		ieee80211_wake_queues(hw);
382 	}
383 
384 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
385 		ret = mt7915_mcu_set_sku(phy);
386 		if (ret)
387 			return ret;
388 	}
389 
390 	mutex_lock(&dev->mt76.mutex);
391 
392 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
393 		bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
394 
395 		if (!enabled)
396 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
397 		else
398 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
399 
400 		mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
401 			       enabled);
402 		mt76_testmode_reset(phy->mt76, true);
403 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
404 	}
405 
406 	mutex_unlock(&dev->mt76.mutex);
407 
408 	return 0;
409 }
410 
411 static int
412 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
413 	       const struct ieee80211_tx_queue_params *params)
414 {
415 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
416 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
417 
418 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
419 	queue = mt7915_lmac_mapping(dev, queue);
420 	mvif->queue_params[queue] = *params;
421 
422 	return 0;
423 }
424 
425 static void mt7915_configure_filter(struct ieee80211_hw *hw,
426 				    unsigned int changed_flags,
427 				    unsigned int *total_flags,
428 				    u64 multicast)
429 {
430 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
431 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
432 	bool band = phy != &dev->phy;
433 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
434 			MT_WF_RFCR1_DROP_BF_POLL |
435 			MT_WF_RFCR1_DROP_BA |
436 			MT_WF_RFCR1_DROP_CFEND |
437 			MT_WF_RFCR1_DROP_CFACK;
438 	u32 flags = 0;
439 
440 #define MT76_FILTER(_flag, _hw) do {					\
441 		flags |= *total_flags & FIF_##_flag;			\
442 		phy->rxfilter &= ~(_hw);				\
443 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
444 	} while (0)
445 
446 	mutex_lock(&dev->mt76.mutex);
447 
448 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
449 			   MT_WF_RFCR_DROP_OTHER_BEACON |
450 			   MT_WF_RFCR_DROP_FRAME_REPORT |
451 			   MT_WF_RFCR_DROP_PROBEREQ |
452 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
453 			   MT_WF_RFCR_DROP_MCAST |
454 			   MT_WF_RFCR_DROP_BCAST |
455 			   MT_WF_RFCR_DROP_DUPLICATE |
456 			   MT_WF_RFCR_DROP_A2_BSSID |
457 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
458 			   MT_WF_RFCR_DROP_STBC_MULTI);
459 
460 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
461 			       MT_WF_RFCR_DROP_A3_MAC |
462 			       MT_WF_RFCR_DROP_A3_BSSID);
463 
464 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
465 
466 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
467 			     MT_WF_RFCR_DROP_RTS |
468 			     MT_WF_RFCR_DROP_CTL_RSV |
469 			     MT_WF_RFCR_DROP_NDPA);
470 
471 	*total_flags = flags;
472 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
473 
474 	if (*total_flags & FIF_CONTROL)
475 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
476 	else
477 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
478 
479 	mutex_unlock(&dev->mt76.mutex);
480 }
481 
482 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
483 				    struct ieee80211_vif *vif,
484 				    struct ieee80211_bss_conf *info,
485 				    u32 changed)
486 {
487 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
488 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
489 
490 	mutex_lock(&dev->mt76.mutex);
491 
492 	/*
493 	 * station mode uses BSSID to map the wlan entry to a peer,
494 	 * and then peer references bss_info_rfch to set bandwidth cap.
495 	 */
496 	if (changed & BSS_CHANGED_BSSID &&
497 	    vif->type == NL80211_IFTYPE_STATION) {
498 		bool join = !is_zero_ether_addr(info->bssid);
499 
500 		mt7915_mcu_add_bss_info(phy, vif, join);
501 		mt7915_mcu_add_sta(dev, vif, NULL, join);
502 	}
503 
504 	if (changed & BSS_CHANGED_ASSOC) {
505 		mt7915_mcu_add_bss_info(phy, vif, info->assoc);
506 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
507 	}
508 
509 	if (changed & BSS_CHANGED_ERP_SLOT) {
510 		int slottime = info->use_short_slot ? 9 : 20;
511 
512 		if (slottime != phy->slottime) {
513 			phy->slottime = slottime;
514 			mt7915_mac_set_timing(phy);
515 		}
516 	}
517 
518 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
519 		mt7915_mcu_add_bss_info(phy, vif, info->enable_beacon);
520 		mt7915_mcu_add_sta(dev, vif, NULL, info->enable_beacon);
521 	}
522 
523 	/* ensure that enable txcmd_mode after bss_info */
524 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
525 		mt7915_mcu_set_tx(dev, vif);
526 
527 	if (changed & BSS_CHANGED_HE_OBSS_PD)
528 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
529 
530 	if (changed & (BSS_CHANGED_BEACON |
531 		       BSS_CHANGED_BEACON_ENABLED))
532 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon);
533 
534 	mutex_unlock(&dev->mt76.mutex);
535 }
536 
537 static void
538 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
539 			     struct ieee80211_vif *vif,
540 			     struct cfg80211_chan_def *chandef)
541 {
542 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
543 
544 	mutex_lock(&dev->mt76.mutex);
545 	mt7915_mcu_add_beacon(hw, vif, true);
546 	mutex_unlock(&dev->mt76.mutex);
547 }
548 
549 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
550 		       struct ieee80211_sta *sta)
551 {
552 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
553 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
554 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
555 	int ret, idx;
556 
557 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA - 1);
558 	if (idx < 0)
559 		return -ENOSPC;
560 
561 	INIT_LIST_HEAD(&msta->rc_list);
562 	INIT_LIST_HEAD(&msta->stats_list);
563 	INIT_LIST_HEAD(&msta->poll_list);
564 	msta->vif = mvif;
565 	msta->wcid.sta = 1;
566 	msta->wcid.idx = idx;
567 	msta->wcid.ext_phy = mvif->band_idx;
568 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
569 	msta->stats.jiffies = jiffies;
570 
571 	mt7915_mac_wtbl_update(dev, idx,
572 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
573 
574 	ret = mt7915_mcu_add_sta(dev, vif, sta, true);
575 	if (ret)
576 		return ret;
577 
578 	return mt7915_mcu_add_sta_adv(dev, vif, sta, true);
579 }
580 
581 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
582 			   struct ieee80211_sta *sta)
583 {
584 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
585 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
586 
587 	mt7915_mcu_add_sta_adv(dev, vif, sta, false);
588 	mt7915_mcu_add_sta(dev, vif, sta, false);
589 
590 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
591 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
592 
593 	spin_lock_bh(&dev->sta_poll_lock);
594 	if (!list_empty(&msta->poll_list))
595 		list_del_init(&msta->poll_list);
596 	if (!list_empty(&msta->stats_list))
597 		list_del_init(&msta->stats_list);
598 	if (!list_empty(&msta->rc_list))
599 		list_del_init(&msta->rc_list);
600 	spin_unlock_bh(&dev->sta_poll_lock);
601 }
602 
603 static void mt7915_tx(struct ieee80211_hw *hw,
604 		      struct ieee80211_tx_control *control,
605 		      struct sk_buff *skb)
606 {
607 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
608 	struct mt76_phy *mphy = hw->priv;
609 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
610 	struct ieee80211_vif *vif = info->control.vif;
611 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
612 
613 	if (control->sta) {
614 		struct mt7915_sta *sta;
615 
616 		sta = (struct mt7915_sta *)control->sta->drv_priv;
617 		wcid = &sta->wcid;
618 	}
619 
620 	if (vif && !control->sta) {
621 		struct mt7915_vif *mvif;
622 
623 		mvif = (struct mt7915_vif *)vif->drv_priv;
624 		wcid = &mvif->sta.wcid;
625 	}
626 
627 	mt76_tx(mphy, control->sta, wcid, skb);
628 }
629 
630 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
631 {
632 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
633 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
634 
635 	mutex_lock(&dev->mt76.mutex);
636 	mt7915_mcu_set_rts_thresh(phy, val);
637 	mutex_unlock(&dev->mt76.mutex);
638 
639 	return 0;
640 }
641 
642 static int
643 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
644 		    struct ieee80211_ampdu_params *params)
645 {
646 	enum ieee80211_ampdu_mlme_action action = params->action;
647 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
648 	struct ieee80211_sta *sta = params->sta;
649 	struct ieee80211_txq *txq = sta->txq[params->tid];
650 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
651 	u16 tid = params->tid;
652 	u16 ssn = params->ssn;
653 	struct mt76_txq *mtxq;
654 	int ret = 0;
655 
656 	if (!txq)
657 		return -EINVAL;
658 
659 	mtxq = (struct mt76_txq *)txq->drv_priv;
660 
661 	mutex_lock(&dev->mt76.mutex);
662 	switch (action) {
663 	case IEEE80211_AMPDU_RX_START:
664 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
665 				   params->buf_size);
666 		mt7915_mcu_add_rx_ba(dev, params, true);
667 		break;
668 	case IEEE80211_AMPDU_RX_STOP:
669 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
670 		mt7915_mcu_add_rx_ba(dev, params, false);
671 		break;
672 	case IEEE80211_AMPDU_TX_OPERATIONAL:
673 		mtxq->aggr = true;
674 		mtxq->send_bar = false;
675 		mt7915_mcu_add_tx_ba(dev, params, true);
676 		break;
677 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
678 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
679 		mtxq->aggr = false;
680 		clear_bit(tid, &msta->ampdu_state);
681 		mt7915_mcu_add_tx_ba(dev, params, false);
682 		break;
683 	case IEEE80211_AMPDU_TX_START:
684 		set_bit(tid, &msta->ampdu_state);
685 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
686 		break;
687 	case IEEE80211_AMPDU_TX_STOP_CONT:
688 		mtxq->aggr = false;
689 		clear_bit(tid, &msta->ampdu_state);
690 		mt7915_mcu_add_tx_ba(dev, params, false);
691 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
692 		break;
693 	}
694 	mutex_unlock(&dev->mt76.mutex);
695 
696 	return ret;
697 }
698 
699 static int
700 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
701 	       struct ieee80211_sta *sta)
702 {
703 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
704 			      IEEE80211_STA_NONE);
705 }
706 
707 static int
708 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
709 		  struct ieee80211_sta *sta)
710 {
711 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
712 			      IEEE80211_STA_NOTEXIST);
713 }
714 
715 static int
716 mt7915_get_stats(struct ieee80211_hw *hw,
717 		 struct ieee80211_low_level_stats *stats)
718 {
719 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
720 	struct mib_stats *mib = &phy->mib;
721 
722 	stats->dot11RTSSuccessCount = mib->rts_cnt;
723 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
724 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
725 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
726 
727 	return 0;
728 }
729 
730 static u64
731 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
732 {
733 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
734 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
735 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
736 	bool band = phy != &dev->phy;
737 	union {
738 		u64 t64;
739 		u32 t32[2];
740 	} tsf;
741 	u16 n;
742 
743 	mutex_lock(&dev->mt76.mutex);
744 
745 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
746 	/* TSF software read */
747 	mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE);
748 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
749 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
750 
751 	mutex_unlock(&dev->mt76.mutex);
752 
753 	return tsf.t64;
754 }
755 
756 static void
757 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
758 	       u64 timestamp)
759 {
760 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
761 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
762 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
763 	bool band = phy != &dev->phy;
764 	union {
765 		u64 t64;
766 		u32 t32[2];
767 	} tsf = { .t64 = timestamp, };
768 	u16 n;
769 
770 	mutex_lock(&dev->mt76.mutex);
771 
772 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
773 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
774 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
775 	/* TSF software overwrite */
776 	mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_WRITE);
777 
778 	mutex_unlock(&dev->mt76.mutex);
779 }
780 
781 static void
782 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
783 {
784 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
785 	struct mt7915_dev *dev = phy->dev;
786 
787 	mutex_lock(&dev->mt76.mutex);
788 	phy->coverage_class = max_t(s16, coverage_class, 0);
789 	mt7915_mac_set_timing(phy);
790 	mutex_unlock(&dev->mt76.mutex);
791 }
792 
793 static int
794 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
795 {
796 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
797 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
798 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
799 	bool ext_phy = phy != &dev->phy;
800 
801 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
802 		return -EINVAL;
803 
804 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
805 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
806 
807 	mutex_lock(&dev->mt76.mutex);
808 
809 	phy->mt76->antenna_mask = tx_ant;
810 
811 	if (ext_phy) {
812 		if (dev->chainmask == 0xf)
813 			tx_ant <<= 2;
814 		else
815 			tx_ant <<= 1;
816 	}
817 	phy->mt76->chainmask = tx_ant;
818 
819 	mt76_set_stream_caps(phy->mt76, true);
820 	mt7915_set_stream_vht_txbf_caps(phy);
821 	mt7915_set_stream_he_caps(phy);
822 
823 	mutex_unlock(&dev->mt76.mutex);
824 
825 	return 0;
826 }
827 
828 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
829 				  struct ieee80211_vif *vif,
830 				  struct ieee80211_sta *sta,
831 				  struct station_info *sinfo)
832 {
833 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
834 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
835 	struct mt7915_sta_stats *stats = &msta->stats;
836 
837 	if (mt7915_mcu_get_rx_rate(phy, vif, sta, &sinfo->rxrate) == 0)
838 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
839 
840 	if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
841 		return;
842 
843 	if (stats->tx_rate.legacy) {
844 		sinfo->txrate.legacy = stats->tx_rate.legacy;
845 	} else {
846 		sinfo->txrate.mcs = stats->tx_rate.mcs;
847 		sinfo->txrate.nss = stats->tx_rate.nss;
848 		sinfo->txrate.bw = stats->tx_rate.bw;
849 		sinfo->txrate.he_gi = stats->tx_rate.he_gi;
850 		sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
851 		sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
852 	}
853 	sinfo->txrate.flags = stats->tx_rate.flags;
854 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
855 }
856 
857 static void
858 mt7915_sta_rc_update(struct ieee80211_hw *hw,
859 		     struct ieee80211_vif *vif,
860 		     struct ieee80211_sta *sta,
861 		     u32 changed)
862 {
863 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
864 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
865 
866 	spin_lock_bh(&dev->sta_poll_lock);
867 	msta->stats.changed |= changed;
868 	if (list_empty(&msta->rc_list))
869 		list_add_tail(&msta->rc_list, &dev->sta_rc_list);
870 	spin_unlock_bh(&dev->sta_poll_lock);
871 
872 	ieee80211_queue_work(hw, &dev->rc_work);
873 }
874 
875 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
876 				 struct ieee80211_vif *vif,
877 				 struct ieee80211_sta *sta,
878 				 bool enabled)
879 {
880 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
881 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
882 
883 	if (enabled)
884 		set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
885 	else
886 		clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
887 
888 	mt7915_mcu_sta_update_hdr_trans(dev, vif, sta);
889 }
890 
891 const struct ieee80211_ops mt7915_ops = {
892 	.tx = mt7915_tx,
893 	.start = mt7915_start,
894 	.stop = mt7915_stop,
895 	.add_interface = mt7915_add_interface,
896 	.remove_interface = mt7915_remove_interface,
897 	.config = mt7915_config,
898 	.conf_tx = mt7915_conf_tx,
899 	.configure_filter = mt7915_configure_filter,
900 	.bss_info_changed = mt7915_bss_info_changed,
901 	.sta_add = mt7915_sta_add,
902 	.sta_remove = mt7915_sta_remove,
903 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
904 	.sta_rc_update = mt7915_sta_rc_update,
905 	.set_key = mt7915_set_key,
906 	.ampdu_action = mt7915_ampdu_action,
907 	.set_rts_threshold = mt7915_set_rts_threshold,
908 	.wake_tx_queue = mt76_wake_tx_queue,
909 	.sw_scan_start = mt76_sw_scan,
910 	.sw_scan_complete = mt76_sw_scan_complete,
911 	.release_buffered_frames = mt76_release_buffered_frames,
912 	.get_txpower = mt76_get_txpower,
913 	.channel_switch_beacon = mt7915_channel_switch_beacon,
914 	.get_stats = mt7915_get_stats,
915 	.get_tsf = mt7915_get_tsf,
916 	.set_tsf = mt7915_set_tsf,
917 	.get_survey = mt76_get_survey,
918 	.get_antenna = mt76_get_antenna,
919 	.set_antenna = mt7915_set_antenna,
920 	.set_coverage_class = mt7915_set_coverage_class,
921 	.sta_statistics = mt7915_sta_statistics,
922 	.sta_set_4addr = mt7915_sta_set_4addr,
923 	CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
924 	CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
925 #ifdef CONFIG_MAC80211_DEBUGFS
926 	.sta_add_debugfs = mt7915_sta_add_debugfs,
927 #endif
928 };
929