1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Roy Luo <royluo@google.com>
5  *         Ryder Lee <ryder.lee@mediatek.com>
6  *         Felix Fietkau <nbd@nbd.name>
7  */
8 
9 #include <linux/etherdevice.h>
10 #include <linux/platform_device.h>
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include "mt7615.h"
14 #include "mcu.h"
15 
16 static bool mt7615_dev_running(struct mt7615_dev *dev)
17 {
18 	struct mt7615_phy *phy;
19 
20 	if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
21 		return true;
22 
23 	phy = mt7615_ext_phy(dev);
24 
25 	return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
26 }
27 
28 static int mt7615_start(struct ieee80211_hw *hw)
29 {
30 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
31 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
32 	bool running;
33 
34 	if (!mt7615_wait_for_mcu_init(dev))
35 		return -EIO;
36 
37 	mutex_lock(&dev->mt76.mutex);
38 
39 	running = mt7615_dev_running(dev);
40 
41 	if (!running) {
42 		mt7615_mcu_set_pm(dev, 0, 0);
43 		mt7615_mcu_set_mac_enable(dev, 0, true);
44 		mt7615_mac_enable_nf(dev, 0);
45 	}
46 
47 	if (phy != &dev->phy) {
48 		mt7615_mcu_set_pm(dev, 1, 0);
49 		mt7615_mcu_set_mac_enable(dev, 1, true);
50 		mt7615_mac_enable_nf(dev, 1);
51 	}
52 
53 	mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
54 
55 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
56 
57 	if (running)
58 		goto out;
59 
60 	mt7615_mac_reset_counters(dev);
61 
62 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
63 				     MT7615_WATCHDOG_TIME);
64 
65 out:
66 	mutex_unlock(&dev->mt76.mutex);
67 
68 	return 0;
69 }
70 
71 static void mt7615_stop(struct ieee80211_hw *hw)
72 {
73 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
74 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
75 
76 	mutex_lock(&dev->mt76.mutex);
77 
78 	clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
79 
80 	if (phy != &dev->phy) {
81 		mt7615_mcu_set_pm(dev, 1, 1);
82 		mt7615_mcu_set_mac_enable(dev, 1, false);
83 	}
84 
85 	if (!mt7615_dev_running(dev)) {
86 		cancel_delayed_work_sync(&dev->mt76.mac_work);
87 
88 		mt7615_mcu_set_pm(dev, 0, 1);
89 		mt7615_mcu_set_mac_enable(dev, 0, false);
90 	}
91 
92 	mutex_unlock(&dev->mt76.mutex);
93 }
94 
95 static int get_omac_idx(enum nl80211_iftype type, u32 mask)
96 {
97 	int i;
98 
99 	switch (type) {
100 	case NL80211_IFTYPE_MONITOR:
101 	case NL80211_IFTYPE_AP:
102 	case NL80211_IFTYPE_MESH_POINT:
103 	case NL80211_IFTYPE_ADHOC:
104 		/* ap use hw bssid 0 and ext bssid */
105 		if (~mask & BIT(HW_BSSID_0))
106 			return HW_BSSID_0;
107 
108 		for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
109 			if (~mask & BIT(i))
110 				return i;
111 
112 		break;
113 	case NL80211_IFTYPE_STATION:
114 		/* sta use hw bssid other than 0 */
115 		for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
116 			if (~mask & BIT(i))
117 				return i;
118 
119 		break;
120 	default:
121 		WARN_ON(1);
122 		break;
123 	}
124 
125 	return -1;
126 }
127 
128 static int mt7615_add_interface(struct ieee80211_hw *hw,
129 				struct ieee80211_vif *vif)
130 {
131 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
132 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
133 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
134 	struct mt76_txq *mtxq;
135 	bool ext_phy = phy != &dev->phy;
136 	int idx, ret = 0;
137 
138 	mutex_lock(&dev->mt76.mutex);
139 
140 	mvif->idx = ffs(~dev->vif_mask) - 1;
141 	if (mvif->idx >= MT7615_MAX_INTERFACES) {
142 		ret = -ENOSPC;
143 		goto out;
144 	}
145 
146 	idx = get_omac_idx(vif->type, dev->omac_mask);
147 	if (idx < 0) {
148 		ret = -ENOSPC;
149 		goto out;
150 	}
151 	mvif->omac_idx = idx;
152 
153 	mvif->band_idx = ext_phy;
154 	if (mt7615_ext_phy(dev))
155 		mvif->wmm_idx = ext_phy * (MT7615_MAX_WMM_SETS / 2) +
156 				mvif->idx % (MT7615_MAX_WMM_SETS / 2);
157 	else
158 		mvif->wmm_idx = mvif->idx % MT7615_MAX_WMM_SETS;
159 
160 	ret = mt7615_mcu_add_dev_info(dev, vif, true);
161 	if (ret)
162 		goto out;
163 
164 	dev->vif_mask |= BIT(mvif->idx);
165 	dev->omac_mask |= BIT(mvif->omac_idx);
166 	phy->omac_mask |= BIT(mvif->omac_idx);
167 
168 	mt7615_mcu_set_dbdc(dev);
169 
170 	idx = MT7615_WTBL_RESERVED - mvif->idx;
171 
172 	INIT_LIST_HEAD(&mvif->sta.poll_list);
173 	mvif->sta.wcid.idx = idx;
174 	mvif->sta.wcid.ext_phy = mvif->band_idx;
175 	mvif->sta.wcid.hw_key_idx = -1;
176 	mt7615_mac_wtbl_update(dev, idx,
177 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
178 
179 	rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
180 	if (vif->txq) {
181 		mtxq = (struct mt76_txq *)vif->txq->drv_priv;
182 		mtxq->wcid = &mvif->sta.wcid;
183 		mt76_txq_init(&dev->mt76, vif->txq);
184 	}
185 
186 out:
187 	mutex_unlock(&dev->mt76.mutex);
188 
189 	return ret;
190 }
191 
192 static void mt7615_remove_interface(struct ieee80211_hw *hw,
193 				    struct ieee80211_vif *vif)
194 {
195 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
196 	struct mt7615_sta *msta = &mvif->sta;
197 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
198 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
199 	int idx = msta->wcid.idx;
200 
201 	/* TODO: disable beacon for the bss */
202 
203 	mt7615_mcu_add_dev_info(dev, vif, false);
204 
205 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
206 	if (vif->txq)
207 		mt76_txq_remove(&dev->mt76, vif->txq);
208 
209 	mutex_lock(&dev->mt76.mutex);
210 	dev->vif_mask &= ~BIT(mvif->idx);
211 	dev->omac_mask &= ~BIT(mvif->omac_idx);
212 	phy->omac_mask &= ~BIT(mvif->omac_idx);
213 	mutex_unlock(&dev->mt76.mutex);
214 
215 	spin_lock_bh(&dev->sta_poll_lock);
216 	if (!list_empty(&msta->poll_list))
217 		list_del_init(&msta->poll_list);
218 	spin_unlock_bh(&dev->sta_poll_lock);
219 }
220 
221 static int mt7615_set_channel(struct mt7615_phy *phy)
222 {
223 	struct mt7615_dev *dev = phy->dev;
224 	bool ext_phy = phy != &dev->phy;
225 	int ret;
226 
227 	cancel_delayed_work_sync(&dev->mt76.mac_work);
228 
229 	mutex_lock(&dev->mt76.mutex);
230 	set_bit(MT76_RESET, &phy->mt76->state);
231 
232 	phy->dfs_state = -1;
233 	mt76_set_channel(phy->mt76);
234 
235 	ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
236 	if (ret)
237 		goto out;
238 
239 	mt7615_mac_set_timing(phy);
240 	ret = mt7615_dfs_init_radar_detector(phy);
241 	mt7615_mac_cca_stats_reset(phy);
242 	mt7615_mcu_set_sku_en(phy, true);
243 
244 	mt7615_mac_reset_counters(dev);
245 	phy->noise = 0;
246 	phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
247 
248 out:
249 	clear_bit(MT76_RESET, &phy->mt76->state);
250 	mutex_unlock(&dev->mt76.mutex);
251 
252 	mt76_txq_schedule_all(phy->mt76);
253 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
254 				     MT7615_WATCHDOG_TIME);
255 	return ret;
256 }
257 
258 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
259 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
260 			  struct ieee80211_key_conf *key)
261 {
262 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
263 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
264 	struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
265 				  &mvif->sta;
266 	struct mt76_wcid *wcid = &msta->wcid;
267 	int idx = key->keyidx;
268 
269 	/* The hardware does not support per-STA RX GTK, fallback
270 	 * to software mode for these.
271 	 */
272 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
273 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
274 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
275 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
276 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
277 		return -EOPNOTSUPP;
278 
279 	/* fall back to sw encryption for unsupported ciphers */
280 	switch (key->cipher) {
281 	case WLAN_CIPHER_SUITE_AES_CMAC:
282 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
283 		break;
284 	case WLAN_CIPHER_SUITE_WEP40:
285 	case WLAN_CIPHER_SUITE_WEP104:
286 	case WLAN_CIPHER_SUITE_TKIP:
287 	case WLAN_CIPHER_SUITE_CCMP:
288 	case WLAN_CIPHER_SUITE_CCMP_256:
289 	case WLAN_CIPHER_SUITE_GCMP:
290 	case WLAN_CIPHER_SUITE_GCMP_256:
291 	case WLAN_CIPHER_SUITE_SMS4:
292 		break;
293 	default:
294 		return -EOPNOTSUPP;
295 	}
296 
297 	if (cmd == SET_KEY) {
298 		key->hw_key_idx = wcid->idx;
299 		wcid->hw_key_idx = idx;
300 	} else if (idx == wcid->hw_key_idx) {
301 		wcid->hw_key_idx = -1;
302 	}
303 	mt76_wcid_key_setup(&dev->mt76, wcid,
304 			    cmd == SET_KEY ? key : NULL);
305 
306 	return mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
307 }
308 
309 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
310 {
311 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
312 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
313 	bool band = phy != &dev->phy;
314 	int ret = 0;
315 
316 	if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
317 		       IEEE80211_CONF_CHANGE_POWER)) {
318 		ieee80211_stop_queues(hw);
319 		ret = mt7615_set_channel(phy);
320 		ieee80211_wake_queues(hw);
321 	}
322 
323 	mutex_lock(&dev->mt76.mutex);
324 
325 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
326 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
327 			phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
328 		else
329 			phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
330 
331 		mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
332 	}
333 
334 	mutex_unlock(&dev->mt76.mutex);
335 
336 	return ret;
337 }
338 
339 static int
340 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
341 	       const struct ieee80211_tx_queue_params *params)
342 {
343 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
344 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
345 
346 	queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
347 
348 	return mt7615_mcu_set_wmm(dev, queue, params);
349 }
350 
351 static void mt7615_configure_filter(struct ieee80211_hw *hw,
352 				    unsigned int changed_flags,
353 				    unsigned int *total_flags,
354 				    u64 multicast)
355 {
356 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
357 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
358 	bool band = phy != &dev->phy;
359 
360 	u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
361 			MT_WF_RFCR1_DROP_BF_POLL |
362 			MT_WF_RFCR1_DROP_BA |
363 			MT_WF_RFCR1_DROP_CFEND |
364 			MT_WF_RFCR1_DROP_CFACK;
365 	u32 flags = 0;
366 
367 #define MT76_FILTER(_flag, _hw) do { \
368 		flags |= *total_flags & FIF_##_flag;			\
369 		phy->rxfilter &= ~(_hw);				\
370 		phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
371 	} while (0)
372 
373 	phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
374 			   MT_WF_RFCR_DROP_OTHER_BEACON |
375 			   MT_WF_RFCR_DROP_FRAME_REPORT |
376 			   MT_WF_RFCR_DROP_PROBEREQ |
377 			   MT_WF_RFCR_DROP_MCAST_FILTERED |
378 			   MT_WF_RFCR_DROP_MCAST |
379 			   MT_WF_RFCR_DROP_BCAST |
380 			   MT_WF_RFCR_DROP_DUPLICATE |
381 			   MT_WF_RFCR_DROP_A2_BSSID |
382 			   MT_WF_RFCR_DROP_UNWANTED_CTL |
383 			   MT_WF_RFCR_DROP_STBC_MULTI);
384 
385 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
386 			       MT_WF_RFCR_DROP_A3_MAC |
387 			       MT_WF_RFCR_DROP_A3_BSSID);
388 
389 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
390 
391 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
392 			     MT_WF_RFCR_DROP_RTS |
393 			     MT_WF_RFCR_DROP_CTL_RSV |
394 			     MT_WF_RFCR_DROP_NDPA);
395 
396 	*total_flags = flags;
397 	mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
398 
399 	if (*total_flags & FIF_CONTROL)
400 		mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
401 	else
402 		mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
403 }
404 
405 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
406 				    struct ieee80211_vif *vif,
407 				    struct ieee80211_bss_conf *info,
408 				    u32 changed)
409 {
410 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
411 
412 	mutex_lock(&dev->mt76.mutex);
413 
414 	if (changed & BSS_CHANGED_ASSOC)
415 		mt7615_mcu_add_bss_info(dev, vif, info->assoc);
416 
417 	if (changed & BSS_CHANGED_ERP_SLOT) {
418 		int slottime = info->use_short_slot ? 9 : 20;
419 		struct mt7615_phy *phy = mt7615_hw_phy(hw);
420 
421 		if (slottime != phy->slottime) {
422 			phy->slottime = slottime;
423 			mt7615_mac_set_timing(phy);
424 		}
425 	}
426 
427 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
428 		mt7615_mcu_add_bss_info(dev, vif, info->enable_beacon);
429 		mt7615_mcu_sta_add(dev, vif, NULL, info->enable_beacon);
430 	}
431 
432 	if (changed & (BSS_CHANGED_BEACON |
433 		       BSS_CHANGED_BEACON_ENABLED))
434 		mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
435 
436 	mutex_unlock(&dev->mt76.mutex);
437 }
438 
439 static void
440 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
441 			     struct ieee80211_vif *vif,
442 			     struct cfg80211_chan_def *chandef)
443 {
444 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
445 
446 	mutex_lock(&dev->mt76.mutex);
447 	mt7615_mcu_add_beacon(dev, hw, vif, true);
448 	mutex_unlock(&dev->mt76.mutex);
449 }
450 
451 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
452 		       struct ieee80211_sta *sta)
453 {
454 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
455 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
456 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
457 	int idx;
458 
459 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
460 	if (idx < 0)
461 		return -ENOSPC;
462 
463 	INIT_LIST_HEAD(&msta->poll_list);
464 	msta->vif = mvif;
465 	msta->wcid.sta = 1;
466 	msta->wcid.idx = idx;
467 	msta->wcid.ext_phy = mvif->band_idx;
468 
469 	mt7615_mac_wtbl_update(dev, idx,
470 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
471 
472 	mt7615_mcu_sta_add(dev, vif, sta, true);
473 
474 	return 0;
475 }
476 
477 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
478 			   struct ieee80211_sta *sta)
479 {
480 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
481 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
482 
483 	mt7615_mcu_sta_add(dev, vif, sta, false);
484 	mt7615_mac_wtbl_update(dev, msta->wcid.idx,
485 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
486 
487 	spin_lock_bh(&dev->sta_poll_lock);
488 	if (!list_empty(&msta->poll_list))
489 		list_del_init(&msta->poll_list);
490 	spin_unlock_bh(&dev->sta_poll_lock);
491 }
492 
493 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
494 				       struct ieee80211_vif *vif,
495 				       struct ieee80211_sta *sta)
496 {
497 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
498 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
499 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
500 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
501 	int i;
502 
503 	spin_lock_bh(&dev->mt76.lock);
504 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
505 		msta->rates[i].idx = sta_rates->rate[i].idx;
506 		msta->rates[i].count = sta_rates->rate[i].count;
507 		msta->rates[i].flags = sta_rates->rate[i].flags;
508 
509 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
510 			break;
511 	}
512 	msta->n_rates = i;
513 	mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
514 	msta->rate_probe = false;
515 	spin_unlock_bh(&dev->mt76.lock);
516 }
517 
518 static void mt7615_tx(struct ieee80211_hw *hw,
519 		      struct ieee80211_tx_control *control,
520 		      struct sk_buff *skb)
521 {
522 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
523 	struct mt76_phy *mphy = hw->priv;
524 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
525 	struct ieee80211_vif *vif = info->control.vif;
526 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
527 
528 	if (control->sta) {
529 		struct mt7615_sta *sta;
530 
531 		sta = (struct mt7615_sta *)control->sta->drv_priv;
532 		wcid = &sta->wcid;
533 	}
534 
535 	if (vif && !control->sta) {
536 		struct mt7615_vif *mvif;
537 
538 		mvif = (struct mt7615_vif *)vif->drv_priv;
539 		wcid = &mvif->sta.wcid;
540 	}
541 
542 	mt76_tx(mphy, control->sta, wcid, skb);
543 }
544 
545 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
546 {
547 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
548 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
549 
550 	mutex_lock(&dev->mt76.mutex);
551 	mt7615_mcu_set_rts_thresh(phy, val);
552 	mutex_unlock(&dev->mt76.mutex);
553 
554 	return 0;
555 }
556 
557 static int
558 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
559 		    struct ieee80211_ampdu_params *params)
560 {
561 	enum ieee80211_ampdu_mlme_action action = params->action;
562 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
563 	struct ieee80211_sta *sta = params->sta;
564 	struct ieee80211_txq *txq = sta->txq[params->tid];
565 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
566 	u16 tid = params->tid;
567 	u16 ssn = params->ssn;
568 	struct mt76_txq *mtxq;
569 	int ret = 0;
570 
571 	if (!txq)
572 		return -EINVAL;
573 
574 	mtxq = (struct mt76_txq *)txq->drv_priv;
575 
576 	mutex_lock(&dev->mt76.mutex);
577 	switch (action) {
578 	case IEEE80211_AMPDU_RX_START:
579 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
580 				   params->buf_size);
581 		mt7615_mcu_add_rx_ba(dev, params, true);
582 		break;
583 	case IEEE80211_AMPDU_RX_STOP:
584 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
585 		mt7615_mcu_add_rx_ba(dev, params, false);
586 		break;
587 	case IEEE80211_AMPDU_TX_OPERATIONAL:
588 		mtxq->aggr = true;
589 		mtxq->send_bar = false;
590 		mt7615_mcu_add_tx_ba(dev, params, true);
591 		break;
592 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
593 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
594 		mtxq->aggr = false;
595 		mt7615_mcu_add_tx_ba(dev, params, false);
596 		break;
597 	case IEEE80211_AMPDU_TX_START:
598 		mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
599 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
600 		break;
601 	case IEEE80211_AMPDU_TX_STOP_CONT:
602 		mtxq->aggr = false;
603 		mt7615_mcu_add_tx_ba(dev, params, false);
604 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
605 		break;
606 	}
607 	mutex_unlock(&dev->mt76.mutex);
608 
609 	return ret;
610 }
611 
612 static int
613 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
614 	       struct ieee80211_sta *sta)
615 {
616     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
617 			  IEEE80211_STA_NONE);
618 }
619 
620 static int
621 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
622 		  struct ieee80211_sta *sta)
623 {
624     return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
625 			  IEEE80211_STA_NOTEXIST);
626 }
627 
628 static int
629 mt7615_get_stats(struct ieee80211_hw *hw,
630 		 struct ieee80211_low_level_stats *stats)
631 {
632 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
633 	struct mib_stats *mib = &phy->mib;
634 
635 	stats->dot11RTSSuccessCount = mib->rts_cnt;
636 	stats->dot11RTSFailureCount = mib->rts_retries_cnt;
637 	stats->dot11FCSErrorCount = mib->fcs_err_cnt;
638 	stats->dot11ACKFailureCount = mib->ack_fail_cnt;
639 
640 	return 0;
641 }
642 
643 static u64
644 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
645 {
646 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
647 	union {
648 		u64 t64;
649 		u32 t32[2];
650 	} tsf;
651 
652 	mutex_lock(&dev->mt76.mutex);
653 
654 	mt76_set(dev, MT_LPON_T0CR, MT_LPON_T0CR_MODE); /* TSF read */
655 	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
656 	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
657 
658 	mutex_unlock(&dev->mt76.mutex);
659 
660 	return tsf.t64;
661 }
662 
663 static void
664 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
665 {
666 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
667 
668 	phy->coverage_class = max_t(s16, coverage_class, 0);
669 	mt7615_mac_set_timing(phy);
670 }
671 
672 static int
673 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
674 {
675 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
676 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
677 	int max_nss = hweight8(hw->wiphy->available_antennas_tx);
678 	bool ext_phy = phy != &dev->phy;
679 
680 	if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
681 		return -EINVAL;
682 
683 	if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
684 		tx_ant = BIT(ffs(tx_ant) - 1) - 1;
685 
686 	mutex_lock(&dev->mt76.mutex);
687 
688 	phy->mt76->antenna_mask = tx_ant;
689 	if (ext_phy) {
690 		if (dev->chainmask == 0xf)
691 			tx_ant <<= 2;
692 		else
693 			tx_ant <<= 1;
694 	}
695 	phy->chainmask = tx_ant;
696 
697 	mt76_set_stream_caps(&dev->mt76, true);
698 
699 	mutex_unlock(&dev->mt76.mutex);
700 
701 	return 0;
702 }
703 
704 const struct ieee80211_ops mt7615_ops = {
705 	.tx = mt7615_tx,
706 	.start = mt7615_start,
707 	.stop = mt7615_stop,
708 	.add_interface = mt7615_add_interface,
709 	.remove_interface = mt7615_remove_interface,
710 	.config = mt7615_config,
711 	.conf_tx = mt7615_conf_tx,
712 	.configure_filter = mt7615_configure_filter,
713 	.bss_info_changed = mt7615_bss_info_changed,
714 	.sta_add = mt7615_sta_add,
715 	.sta_remove = mt7615_sta_remove,
716 	.sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
717 	.set_key = mt7615_set_key,
718 	.ampdu_action = mt7615_ampdu_action,
719 	.set_rts_threshold = mt7615_set_rts_threshold,
720 	.wake_tx_queue = mt76_wake_tx_queue,
721 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
722 	.sw_scan_start = mt76_sw_scan,
723 	.sw_scan_complete = mt76_sw_scan_complete,
724 	.release_buffered_frames = mt76_release_buffered_frames,
725 	.get_txpower = mt76_get_txpower,
726 	.channel_switch_beacon = mt7615_channel_switch_beacon,
727 	.get_stats = mt7615_get_stats,
728 	.get_tsf = mt7615_get_tsf,
729 	.get_survey = mt76_get_survey,
730 	.get_antenna = mt76_get_antenna,
731 	.set_antenna = mt7615_set_antenna,
732 	.set_coverage_class = mt7615_set_coverage_class,
733 };
734 
735 static int __init mt7615_init(void)
736 {
737 	int ret;
738 
739 	ret = pci_register_driver(&mt7615_pci_driver);
740 	if (ret)
741 		return ret;
742 
743 	if (IS_ENABLED(CONFIG_MT7622_WMAC)) {
744 		ret = platform_driver_register(&mt7622_wmac_driver);
745 		if (ret)
746 			pci_unregister_driver(&mt7615_pci_driver);
747 	}
748 
749 	return ret;
750 }
751 
752 static void __exit mt7615_exit(void)
753 {
754 	if (IS_ENABLED(CONFIG_MT7622_WMAC))
755 		platform_driver_unregister(&mt7622_wmac_driver);
756 	pci_unregister_driver(&mt7615_pci_driver);
757 }
758 
759 module_init(mt7615_init);
760 module_exit(mt7615_exit);
761 MODULE_LICENSE("Dual BSD/GPL");
762