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