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