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->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->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->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_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
354 
355 	/* no need to update right away, we'll get BSS_CHANGED_QOS */
356 	mvif->wmm[queue].cw_min = params->cw_min;
357 	mvif->wmm[queue].cw_max = params->cw_max;
358 	mvif->wmm[queue].aifs = params->aifs;
359 	mvif->wmm[queue].txop = params->txop;
360 
361 	return 0;
362 }
363 
364 static void mt7915_configure_filter(struct ieee80211_hw *hw,
365 				    unsigned int changed_flags,
366 				    unsigned int *total_flags,
367 				    u64 multicast)
368 {
369 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
370 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
371 	bool band = phy != &dev->phy;
372 
373 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
374 			MT_WF_RFCR1_DROP_BF_POLL |
375 			MT_WF_RFCR1_DROP_BA |
376 			MT_WF_RFCR1_DROP_CFEND |
377 			MT_WF_RFCR1_DROP_CFACK;
378 	u32 flags = 0;
379 
380 #define MT76_FILTER(_flag, _hw) do {					\
381 		flags |= *total_flags & FIF_##_flag;			\
382 		phy->rxfilter &= ~(_hw);				\
383 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
384 	} while (0)
385 
386 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
387 			   MT_WF_RFCR_DROP_OTHER_BEACON |
388 			   MT_WF_RFCR_DROP_FRAME_REPORT |
389 			   MT_WF_RFCR_DROP_PROBEREQ |
390 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
391 			   MT_WF_RFCR_DROP_MCAST |
392 			   MT_WF_RFCR_DROP_BCAST |
393 			   MT_WF_RFCR_DROP_DUPLICATE |
394 			   MT_WF_RFCR_DROP_A2_BSSID |
395 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
396 			   MT_WF_RFCR_DROP_STBC_MULTI);
397 
398 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
399 			       MT_WF_RFCR_DROP_A3_MAC |
400 			       MT_WF_RFCR_DROP_A3_BSSID);
401 
402 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
403 
404 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
405 			     MT_WF_RFCR_DROP_RTS |
406 			     MT_WF_RFCR_DROP_CTL_RSV |
407 			     MT_WF_RFCR_DROP_NDPA);
408 
409 	*total_flags = flags;
410 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
411 
412 	if (*total_flags & FIF_CONTROL)
413 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
414 	else
415 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
416 }
417 
418 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
419 				    struct ieee80211_vif *vif,
420 				    struct ieee80211_bss_conf *info,
421 				    u32 changed)
422 {
423 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
424 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
425 
426 	mutex_lock(&dev->mt76.mutex);
427 
428 	/*
429 	 * station mode uses BSSID to map the wlan entry to a peer,
430 	 * and then peer references bss_info_rfch to set bandwidth cap.
431 	 */
432 	if (changed & BSS_CHANGED_BSSID &&
433 	    vif->type == NL80211_IFTYPE_STATION) {
434 		bool join = !is_zero_ether_addr(info->bssid);
435 
436 		mt7915_mcu_add_bss_info(phy, vif, join);
437 		mt7915_mcu_add_sta(dev, vif, NULL, join);
438 	}
439 
440 	if (changed & BSS_CHANGED_ASSOC) {
441 		mt7915_mcu_add_bss_info(phy, vif, info->assoc);
442 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
443 	}
444 
445 	if (changed & BSS_CHANGED_ERP_SLOT) {
446 		int slottime = info->use_short_slot ? 9 : 20;
447 
448 		if (slottime != phy->slottime) {
449 			phy->slottime = slottime;
450 			mt7915_mac_set_timing(phy);
451 		}
452 	}
453 
454 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
455 		mt7915_mcu_add_bss_info(phy, vif, info->enable_beacon);
456 		mt7915_mcu_add_sta(dev, vif, NULL, info->enable_beacon);
457 	}
458 
459 	/* ensure that enable txcmd_mode after bss_info */
460 	if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
461 		mt7915_mcu_set_tx(dev, vif);
462 
463 	if (changed & BSS_CHANGED_HE_OBSS_PD)
464 		mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
465 
466 	if (changed & (BSS_CHANGED_BEACON |
467 		       BSS_CHANGED_BEACON_ENABLED))
468 		mt7915_mcu_add_beacon(hw, vif, info->enable_beacon);
469 
470 	mutex_unlock(&dev->mt76.mutex);
471 }
472 
473 static void
474 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
475 			     struct ieee80211_vif *vif,
476 			     struct cfg80211_chan_def *chandef)
477 {
478 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
479 
480 	mutex_lock(&dev->mt76.mutex);
481 	mt7915_mcu_add_beacon(hw, vif, true);
482 	mutex_unlock(&dev->mt76.mutex);
483 }
484 
485 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
486 		       struct ieee80211_sta *sta)
487 {
488 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
489 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
490 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
491 	int ret, idx;
492 
493 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA - 1);
494 	if (idx < 0)
495 		return -ENOSPC;
496 
497 	INIT_LIST_HEAD(&msta->poll_list);
498 	INIT_WORK(&msta->stats_work, mt7915_mac_sta_stats_work);
499 	spin_lock_init(&msta->ampdu_lock);
500 	msta->vif = mvif;
501 	msta->wcid.sta = 1;
502 	msta->wcid.idx = idx;
503 	msta->wcid.ext_phy = mvif->band_idx;
504 	msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
505 	msta->stats.jiffies = jiffies;
506 
507 	mt7915_mac_wtbl_update(dev, idx,
508 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
509 
510 	ret = mt7915_mcu_add_sta(dev, vif, sta, true);
511 	if (ret)
512 		return ret;
513 
514 	return mt7915_mcu_add_sta_adv(dev, vif, sta, true);
515 }
516 
517 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
518 			   struct ieee80211_sta *sta)
519 {
520 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
521 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
522 
523 	mt7915_mcu_add_sta_adv(dev, vif, sta, false);
524 	mt7915_mcu_add_sta(dev, vif, sta, false);
525 
526 	mt7915_mac_wtbl_update(dev, msta->wcid.idx,
527 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
528 
529 	spin_lock_bh(&dev->sta_poll_lock);
530 	if (!list_empty(&msta->poll_list))
531 		list_del_init(&msta->poll_list);
532 	spin_unlock_bh(&dev->sta_poll_lock);
533 }
534 
535 static void mt7915_tx(struct ieee80211_hw *hw,
536 		      struct ieee80211_tx_control *control,
537 		      struct sk_buff *skb)
538 {
539 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
540 	struct mt76_phy *mphy = hw->priv;
541 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
542 	struct ieee80211_vif *vif = info->control.vif;
543 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
544 
545 	if (control->sta) {
546 		struct mt7915_sta *sta;
547 
548 		sta = (struct mt7915_sta *)control->sta->drv_priv;
549 		wcid = &sta->wcid;
550 	}
551 
552 	if (vif && !control->sta) {
553 		struct mt7915_vif *mvif;
554 
555 		mvif = (struct mt7915_vif *)vif->drv_priv;
556 		wcid = &mvif->sta.wcid;
557 	}
558 
559 	mt76_tx(mphy, control->sta, wcid, skb);
560 }
561 
562 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
563 {
564 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
565 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
566 
567 	mutex_lock(&dev->mt76.mutex);
568 	mt7915_mcu_set_rts_thresh(phy, val);
569 	mutex_unlock(&dev->mt76.mutex);
570 
571 	return 0;
572 }
573 
574 static int
575 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
576 		    struct ieee80211_ampdu_params *params)
577 {
578 	enum ieee80211_ampdu_mlme_action action = params->action;
579 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
580 	struct ieee80211_sta *sta = params->sta;
581 	struct ieee80211_txq *txq = sta->txq[params->tid];
582 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
583 	u16 tid = params->tid;
584 	u16 ssn = params->ssn;
585 	struct mt76_txq *mtxq;
586 	int ret = 0;
587 
588 	if (!txq)
589 		return -EINVAL;
590 
591 	mtxq = (struct mt76_txq *)txq->drv_priv;
592 
593 	mutex_lock(&dev->mt76.mutex);
594 	switch (action) {
595 	case IEEE80211_AMPDU_RX_START:
596 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
597 				   params->buf_size);
598 		mt7915_mcu_add_rx_ba(dev, params, true);
599 		break;
600 	case IEEE80211_AMPDU_RX_STOP:
601 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
602 		mt7915_mcu_add_rx_ba(dev, params, false);
603 		break;
604 	case IEEE80211_AMPDU_TX_OPERATIONAL:
605 		mtxq->aggr = true;
606 		mtxq->send_bar = false;
607 		mt7915_set_aggr_state(msta, tid, MT7915_AGGR_OPERATIONAL);
608 		mt7915_mcu_add_tx_ba(dev, params, true);
609 		break;
610 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
611 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
612 		mtxq->aggr = false;
613 		mt7915_set_aggr_state(msta, tid, MT7915_AGGR_STOP);
614 		mt7915_mcu_add_tx_ba(dev, params, false);
615 		break;
616 	case IEEE80211_AMPDU_TX_START:
617 		mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
618 		mt7915_set_aggr_state(msta, tid, MT7915_AGGR_START);
619 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
620 		break;
621 	case IEEE80211_AMPDU_TX_STOP_CONT:
622 		mtxq->aggr = false;
623 		mt7915_set_aggr_state(msta, tid, MT7915_AGGR_STOP);
624 		mt7915_mcu_add_tx_ba(dev, params, false);
625 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
626 		break;
627 	}
628 	mutex_unlock(&dev->mt76.mutex);
629 
630 	return ret;
631 }
632 
633 static int
634 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
635 	       struct ieee80211_sta *sta)
636 {
637 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
638 			      IEEE80211_STA_NONE);
639 }
640 
641 static int
642 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
643 		  struct ieee80211_sta *sta)
644 {
645 	return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
646 			      IEEE80211_STA_NOTEXIST);
647 }
648 
649 static int
650 mt7915_get_stats(struct ieee80211_hw *hw,
651 		 struct ieee80211_low_level_stats *stats)
652 {
653 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
654 	struct mib_stats *mib = &phy->mib;
655 
656 	stats->dot11RTSSuccessCount = mib->rts_cnt;
657 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
658 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
659 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
660 
661 	return 0;
662 }
663 
664 static u64
665 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
666 {
667 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
668 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
669 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
670 	bool band = phy != &dev->phy;
671 	union {
672 		u64 t64;
673 		u32 t32[2];
674 	} tsf;
675 	u16 n;
676 
677 	mutex_lock(&dev->mt76.mutex);
678 
679 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
680 	/* TSF software read */
681 	mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE);
682 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
683 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
684 
685 	mutex_unlock(&dev->mt76.mutex);
686 
687 	return tsf.t64;
688 }
689 
690 static void
691 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
692 	       u64 timestamp)
693 {
694 	struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
695 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
696 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
697 	bool band = phy != &dev->phy;
698 	union {
699 		u64 t64;
700 		u32 t32[2];
701 	} tsf = { .t64 = timestamp, };
702 	u16 n;
703 
704 	mutex_lock(&dev->mt76.mutex);
705 
706 	n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
707 	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
708 	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
709 	/* TSF software overwrite */
710 	mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_WRITE);
711 
712 	mutex_unlock(&dev->mt76.mutex);
713 }
714 
715 static void
716 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
717 {
718 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
719 
720 	phy->coverage_class = max_t(s16, coverage_class, 0);
721 	mt7915_mac_set_timing(phy);
722 }
723 
724 static int
725 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
726 {
727 	struct mt7915_dev *dev = mt7915_hw_dev(hw);
728 	struct mt7915_phy *phy = mt7915_hw_phy(hw);
729 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
730 	bool ext_phy = phy != &dev->phy;
731 
732 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
733 		return -EINVAL;
734 
735 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
736 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
737 
738 	mutex_lock(&dev->mt76.mutex);
739 
740 	phy->mt76->antenna_mask = tx_ant;
741 
742 	if (ext_phy) {
743 		if (dev->chainmask == 0xf)
744 			tx_ant <<= 2;
745 		else
746 			tx_ant <<= 1;
747 	}
748 	phy->chainmask = tx_ant;
749 
750 	mt76_set_stream_caps(phy->mt76, true);
751 	mt7915_set_stream_vht_txbf_caps(phy);
752 	mt7915_set_stream_he_caps(phy);
753 
754 	mutex_unlock(&dev->mt76.mutex);
755 
756 	return 0;
757 }
758 
759 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
760 				  struct ieee80211_vif *vif,
761 				  struct ieee80211_sta *sta,
762 				  struct station_info *sinfo)
763 {
764 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
765 	struct mt7915_sta_stats *stats = &msta->stats;
766 
767 	if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
768 		return;
769 
770 	if (stats->tx_rate.legacy) {
771 		sinfo->txrate.legacy = stats->tx_rate.legacy;
772 	} else {
773 		sinfo->txrate.mcs = stats->tx_rate.mcs;
774 		sinfo->txrate.nss = stats->tx_rate.nss;
775 		sinfo->txrate.bw = stats->tx_rate.bw;
776 		sinfo->txrate.he_gi = stats->tx_rate.he_gi;
777 		sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
778 		sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
779 	}
780 	sinfo->txrate.flags = stats->tx_rate.flags;
781 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
782 }
783 
784 static void
785 mt7915_sta_rc_update(struct ieee80211_hw *hw,
786 		     struct ieee80211_vif *vif,
787 		     struct ieee80211_sta *sta,
788 		     u32 changed)
789 {
790 	struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
791 
792 	rcu_read_lock();
793 	sta = ieee80211_find_sta(vif, sta->addr);
794 	if (!sta) {
795 		rcu_read_unlock();
796 		return;
797 	}
798 	rcu_read_unlock();
799 
800 	set_bit(changed, &msta->stats.changed);
801 	ieee80211_queue_work(hw, &msta->stats_work);
802 }
803 
804 const struct ieee80211_ops mt7915_ops = {
805 	.tx = mt7915_tx,
806 	.start = mt7915_start,
807 	.stop = mt7915_stop,
808 	.add_interface = mt7915_add_interface,
809 	.remove_interface = mt7915_remove_interface,
810 	.config = mt7915_config,
811 	.conf_tx = mt7915_conf_tx,
812 	.configure_filter = mt7915_configure_filter,
813 	.bss_info_changed = mt7915_bss_info_changed,
814 	.sta_add = mt7915_sta_add,
815 	.sta_remove = mt7915_sta_remove,
816 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
817 	.sta_rc_update = mt7915_sta_rc_update,
818 	.set_key = mt7915_set_key,
819 	.ampdu_action = mt7915_ampdu_action,
820 	.set_rts_threshold = mt7915_set_rts_threshold,
821 	.wake_tx_queue = mt76_wake_tx_queue,
822 	.sw_scan_start = mt76_sw_scan,
823 	.sw_scan_complete = mt76_sw_scan_complete,
824 	.release_buffered_frames = mt76_release_buffered_frames,
825 	.get_txpower = mt76_get_txpower,
826 	.channel_switch_beacon = mt7915_channel_switch_beacon,
827 	.get_stats = mt7915_get_stats,
828 	.get_tsf = mt7915_get_tsf,
829 	.set_tsf = mt7915_set_tsf,
830 	.get_survey = mt76_get_survey,
831 	.get_antenna = mt76_get_antenna,
832 	.set_antenna = mt7915_set_antenna,
833 	.set_coverage_class = mt7915_set_coverage_class,
834 	.sta_statistics = mt7915_sta_statistics,
835 #ifdef CONFIG_MAC80211_DEBUGFS
836 	.sta_add_debugfs = mt7915_sta_add_debugfs,
837 #endif
838 };
839