WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / wireless / mediatek / mt76 / mt7915 / main.c
blob0c82aa2ef219d20cd094517ecfc404a2a861423b
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
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"
11 static bool mt7915_dev_running(struct mt7915_dev *dev)
13 struct mt7915_phy *phy;
15 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
16 return true;
18 phy = mt7915_ext_phy(dev);
20 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
23 static int mt7915_start(struct ieee80211_hw *hw)
25 struct mt7915_dev *dev = mt7915_hw_dev(hw);
26 struct mt7915_phy *phy = mt7915_hw_phy(hw);
27 bool running;
29 mutex_lock(&dev->mt76.mutex);
31 running = mt7915_dev_running(dev);
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 mt7915_mac_enable_nf(dev, 0);
40 if (phy != &dev->phy) {
41 mt7915_mcu_set_pm(dev, 1, 0);
42 mt7915_mcu_set_mac(dev, 1, true, false);
43 mt7915_mcu_set_scs(dev, 1, true);
44 mt7915_mac_enable_nf(dev, 1);
47 mt7915_mcu_set_sku_en(phy, !mt76_testmode_enabled(&dev->mt76));
48 mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
50 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
52 if (!mt76_testmode_enabled(&dev->mt76))
53 ieee80211_queue_delayed_work(hw, &phy->mac_work,
54 MT7915_WATCHDOG_TIME);
56 if (!running)
57 mt7915_mac_reset_counters(phy);
59 mutex_unlock(&dev->mt76.mutex);
61 return 0;
64 static void mt7915_stop(struct ieee80211_hw *hw)
66 struct mt7915_dev *dev = mt7915_hw_dev(hw);
67 struct mt7915_phy *phy = mt7915_hw_phy(hw);
69 cancel_delayed_work_sync(&phy->mac_work);
71 mutex_lock(&dev->mt76.mutex);
73 mt76_testmode_reset(&dev->mt76, true);
75 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
77 if (phy != &dev->phy) {
78 mt7915_mcu_set_pm(dev, 1, 1);
79 mt7915_mcu_set_mac(dev, 1, false, false);
82 if (!mt7915_dev_running(dev)) {
83 mt7915_mcu_set_pm(dev, 0, 1);
84 mt7915_mcu_set_mac(dev, 0, false, false);
87 mutex_unlock(&dev->mt76.mutex);
90 static inline int get_free_idx(u32 mask, u8 start, u8 end)
92 return ffs(~mask & GENMASK(end, start));
95 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
97 int i;
99 switch (type) {
100 case NL80211_IFTYPE_MESH_POINT:
101 case NL80211_IFTYPE_ADHOC:
102 case NL80211_IFTYPE_STATION:
103 /* prefer hw bssid slot 1-3 */
104 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
105 if (i)
106 return i - 1;
108 if (type != NL80211_IFTYPE_STATION)
109 break;
111 /* next, try to find a free repeater entry for the sta */
112 i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
113 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
114 if (i)
115 return i + 32 - 1;
117 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
118 if (i)
119 return i - 1;
121 if (~mask & BIT(HW_BSSID_0))
122 return HW_BSSID_0;
124 break;
125 case NL80211_IFTYPE_MONITOR:
126 case NL80211_IFTYPE_AP:
127 /* ap uses hw bssid 0 and ext bssid */
128 if (~mask & BIT(HW_BSSID_0))
129 return HW_BSSID_0;
131 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
132 if (i)
133 return i - 1;
135 break;
136 default:
137 WARN_ON(1);
138 break;
141 return -1;
144 static int mt7915_add_interface(struct ieee80211_hw *hw,
145 struct ieee80211_vif *vif)
147 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
148 struct mt7915_dev *dev = mt7915_hw_dev(hw);
149 struct mt7915_phy *phy = mt7915_hw_phy(hw);
150 struct mt76_txq *mtxq;
151 bool ext_phy = phy != &dev->phy;
152 int idx, ret = 0;
154 mutex_lock(&dev->mt76.mutex);
156 mt76_testmode_reset(&dev->mt76, true);
158 if (vif->type == NL80211_IFTYPE_MONITOR &&
159 is_zero_ether_addr(vif->addr))
160 phy->monitor_vif = vif;
162 mvif->idx = ffs(~phy->mt76->vif_mask) - 1;
163 if (mvif->idx >= MT7915_MAX_INTERFACES) {
164 ret = -ENOSPC;
165 goto out;
168 idx = get_omac_idx(vif->type, phy->omac_mask);
169 if (idx < 0) {
170 ret = -ENOSPC;
171 goto out;
173 mvif->omac_idx = idx;
174 mvif->phy = phy;
175 mvif->band_idx = ext_phy;
177 if (ext_phy)
178 mvif->wmm_idx = ext_phy * (MT7915_MAX_WMM_SETS / 2) +
179 mvif->idx % (MT7915_MAX_WMM_SETS / 2);
180 else
181 mvif->wmm_idx = mvif->idx % MT7915_MAX_WMM_SETS;
183 ret = mt7915_mcu_add_dev_info(phy, vif, true);
184 if (ret)
185 goto out;
187 phy->mt76->vif_mask |= BIT(mvif->idx);
188 phy->omac_mask |= BIT_ULL(mvif->omac_idx);
190 idx = MT7915_WTBL_RESERVED - mvif->idx;
192 INIT_LIST_HEAD(&mvif->sta.rc_list);
193 INIT_LIST_HEAD(&mvif->sta.stats_list);
194 INIT_LIST_HEAD(&mvif->sta.poll_list);
195 mvif->sta.wcid.idx = idx;
196 mvif->sta.wcid.ext_phy = mvif->band_idx;
197 mvif->sta.wcid.hw_key_idx = -1;
198 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
199 mt7915_mac_wtbl_update(dev, idx,
200 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
202 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
203 if (vif->txq) {
204 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
205 mtxq->wcid = &mvif->sta.wcid;
208 if (vif->type != NL80211_IFTYPE_AP &&
209 (!mvif->omac_idx || mvif->omac_idx > 3))
210 vif->offload_flags = 0;
211 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
213 out:
214 mutex_unlock(&dev->mt76.mutex);
216 return ret;
219 static void mt7915_remove_interface(struct ieee80211_hw *hw,
220 struct ieee80211_vif *vif)
222 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
223 struct mt7915_sta *msta = &mvif->sta;
224 struct mt7915_dev *dev = mt7915_hw_dev(hw);
225 struct mt7915_phy *phy = mt7915_hw_phy(hw);
226 int idx = msta->wcid.idx;
228 /* TODO: disable beacon for the bss */
230 mutex_lock(&dev->mt76.mutex);
231 mt76_testmode_reset(&dev->mt76, true);
232 mutex_unlock(&dev->mt76.mutex);
234 if (vif == phy->monitor_vif)
235 phy->monitor_vif = NULL;
237 mt7915_mcu_add_dev_info(phy, vif, false);
239 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
241 mutex_lock(&dev->mt76.mutex);
242 phy->mt76->vif_mask &= ~BIT(mvif->idx);
243 phy->omac_mask &= ~BIT_ULL(mvif->omac_idx);
244 mutex_unlock(&dev->mt76.mutex);
246 spin_lock_bh(&dev->sta_poll_lock);
247 if (!list_empty(&msta->poll_list))
248 list_del_init(&msta->poll_list);
249 spin_unlock_bh(&dev->sta_poll_lock);
252 static void mt7915_init_dfs_state(struct mt7915_phy *phy)
254 struct mt76_phy *mphy = phy->mt76;
255 struct ieee80211_hw *hw = mphy->hw;
256 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
258 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
259 return;
261 if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR))
262 return;
264 if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
265 mphy->chandef.width == chandef->width)
266 return;
268 phy->dfs_state = -1;
271 int mt7915_set_channel(struct mt7915_phy *phy)
273 struct mt7915_dev *dev = phy->dev;
274 int ret;
276 cancel_delayed_work_sync(&phy->mac_work);
278 mutex_lock(&dev->mt76.mutex);
279 set_bit(MT76_RESET, &phy->mt76->state);
281 mt7915_init_dfs_state(phy);
282 mt76_set_channel(phy->mt76);
284 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
285 if (ret)
286 goto out;
288 mt7915_mac_set_timing(phy);
289 ret = mt7915_dfs_init_radar_detector(phy);
290 mt7915_mac_cca_stats_reset(phy);
292 mt7915_mac_reset_counters(phy);
293 phy->noise = 0;
295 out:
296 clear_bit(MT76_RESET, &phy->mt76->state);
297 mutex_unlock(&dev->mt76.mutex);
299 mt76_txq_schedule_all(phy->mt76);
301 if (!mt76_testmode_enabled(&dev->mt76))
302 ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mac_work,
303 MT7915_WATCHDOG_TIME);
305 return ret;
308 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
309 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
310 struct ieee80211_key_conf *key)
312 struct mt7915_dev *dev = mt7915_hw_dev(hw);
313 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
314 struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
315 &mvif->sta;
316 struct mt76_wcid *wcid = &msta->wcid;
317 int idx = key->keyidx;
319 /* The hardware does not support per-STA RX GTK, fallback
320 * to software mode for these.
322 if ((vif->type == NL80211_IFTYPE_ADHOC ||
323 vif->type == NL80211_IFTYPE_MESH_POINT) &&
324 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
325 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
326 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
327 return -EOPNOTSUPP;
329 /* fall back to sw encryption for unsupported ciphers */
330 switch (key->cipher) {
331 case WLAN_CIPHER_SUITE_AES_CMAC:
332 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
333 break;
334 case WLAN_CIPHER_SUITE_TKIP:
335 case WLAN_CIPHER_SUITE_CCMP:
336 case WLAN_CIPHER_SUITE_CCMP_256:
337 case WLAN_CIPHER_SUITE_GCMP:
338 case WLAN_CIPHER_SUITE_GCMP_256:
339 case WLAN_CIPHER_SUITE_SMS4:
340 break;
341 case WLAN_CIPHER_SUITE_WEP40:
342 case WLAN_CIPHER_SUITE_WEP104:
343 default:
344 return -EOPNOTSUPP;
347 if (cmd == SET_KEY) {
348 key->hw_key_idx = wcid->idx;
349 wcid->hw_key_idx = idx;
350 } else if (idx == wcid->hw_key_idx) {
351 wcid->hw_key_idx = -1;
353 mt76_wcid_key_setup(&dev->mt76, wcid,
354 cmd == SET_KEY ? key : NULL);
356 return mt7915_mcu_add_key(dev, vif, msta, key, cmd);
359 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
361 struct mt7915_dev *dev = mt7915_hw_dev(hw);
362 struct mt7915_phy *phy = mt7915_hw_phy(hw);
363 bool band = phy != &dev->phy;
364 int ret;
366 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
367 #ifdef CONFIG_NL80211_TESTMODE
368 if (dev->mt76.test.state != MT76_TM_STATE_OFF) {
369 mutex_lock(&dev->mt76.mutex);
370 mt76_testmode_reset(&dev->mt76, false);
371 mutex_unlock(&dev->mt76.mutex);
373 #endif
374 ieee80211_stop_queues(hw);
375 ret = mt7915_set_channel(phy);
376 if (ret)
377 return ret;
378 ieee80211_wake_queues(hw);
381 if (changed & IEEE80211_CONF_CHANGE_POWER) {
382 ret = mt7915_mcu_set_sku(phy);
383 if (ret)
384 return ret;
387 mutex_lock(&dev->mt76.mutex);
389 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
390 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
392 if (!enabled)
393 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
394 else
395 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
397 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
398 enabled);
399 mt76_testmode_reset(&dev->mt76, true);
400 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
403 mutex_unlock(&dev->mt76.mutex);
405 return 0;
408 static int
409 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
410 const struct ieee80211_tx_queue_params *params)
412 struct mt7915_dev *dev = mt7915_hw_dev(hw);
413 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
415 /* no need to update right away, we'll get BSS_CHANGED_QOS */
416 queue = mt7915_lmac_mapping(dev, queue);
417 mvif->queue_params[queue] = *params;
419 return 0;
422 static void mt7915_configure_filter(struct ieee80211_hw *hw,
423 unsigned int changed_flags,
424 unsigned int *total_flags,
425 u64 multicast)
427 struct mt7915_dev *dev = mt7915_hw_dev(hw);
428 struct mt7915_phy *phy = mt7915_hw_phy(hw);
429 bool band = phy != &dev->phy;
431 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
432 MT_WF_RFCR1_DROP_BF_POLL |
433 MT_WF_RFCR1_DROP_BA |
434 MT_WF_RFCR1_DROP_CFEND |
435 MT_WF_RFCR1_DROP_CFACK;
436 u32 flags = 0;
438 #define MT76_FILTER(_flag, _hw) do { \
439 flags |= *total_flags & FIF_##_flag; \
440 phy->rxfilter &= ~(_hw); \
441 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
442 } while (0)
444 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
445 MT_WF_RFCR_DROP_OTHER_BEACON |
446 MT_WF_RFCR_DROP_FRAME_REPORT |
447 MT_WF_RFCR_DROP_PROBEREQ |
448 MT_WF_RFCR_DROP_MCAST_FILTERED |
449 MT_WF_RFCR_DROP_MCAST |
450 MT_WF_RFCR_DROP_BCAST |
451 MT_WF_RFCR_DROP_DUPLICATE |
452 MT_WF_RFCR_DROP_A2_BSSID |
453 MT_WF_RFCR_DROP_UNWANTED_CTL |
454 MT_WF_RFCR_DROP_STBC_MULTI);
456 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
457 MT_WF_RFCR_DROP_A3_MAC |
458 MT_WF_RFCR_DROP_A3_BSSID);
460 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
462 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
463 MT_WF_RFCR_DROP_RTS |
464 MT_WF_RFCR_DROP_CTL_RSV |
465 MT_WF_RFCR_DROP_NDPA);
467 *total_flags = flags;
468 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
470 if (*total_flags & FIF_CONTROL)
471 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
472 else
473 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
476 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
477 struct ieee80211_vif *vif,
478 struct ieee80211_bss_conf *info,
479 u32 changed)
481 struct mt7915_phy *phy = mt7915_hw_phy(hw);
482 struct mt7915_dev *dev = mt7915_hw_dev(hw);
484 mutex_lock(&dev->mt76.mutex);
487 * station mode uses BSSID to map the wlan entry to a peer,
488 * and then peer references bss_info_rfch to set bandwidth cap.
490 if (changed & BSS_CHANGED_BSSID &&
491 vif->type == NL80211_IFTYPE_STATION) {
492 bool join = !is_zero_ether_addr(info->bssid);
494 mt7915_mcu_add_bss_info(phy, vif, join);
495 mt7915_mcu_add_sta(dev, vif, NULL, join);
498 if (changed & BSS_CHANGED_ASSOC) {
499 mt7915_mcu_add_bss_info(phy, vif, info->assoc);
500 mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
503 if (changed & BSS_CHANGED_ERP_SLOT) {
504 int slottime = info->use_short_slot ? 9 : 20;
506 if (slottime != phy->slottime) {
507 phy->slottime = slottime;
508 mt7915_mac_set_timing(phy);
512 if (changed & BSS_CHANGED_BEACON_ENABLED) {
513 mt7915_mcu_add_bss_info(phy, vif, info->enable_beacon);
514 mt7915_mcu_add_sta(dev, vif, NULL, info->enable_beacon);
517 /* ensure that enable txcmd_mode after bss_info */
518 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
519 mt7915_mcu_set_tx(dev, vif);
521 if (changed & BSS_CHANGED_HE_OBSS_PD)
522 mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
524 if (changed & (BSS_CHANGED_BEACON |
525 BSS_CHANGED_BEACON_ENABLED))
526 mt7915_mcu_add_beacon(hw, vif, info->enable_beacon);
528 mutex_unlock(&dev->mt76.mutex);
531 static void
532 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
533 struct ieee80211_vif *vif,
534 struct cfg80211_chan_def *chandef)
536 struct mt7915_dev *dev = mt7915_hw_dev(hw);
538 mutex_lock(&dev->mt76.mutex);
539 mt7915_mcu_add_beacon(hw, vif, true);
540 mutex_unlock(&dev->mt76.mutex);
543 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
544 struct ieee80211_sta *sta)
546 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
547 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
548 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
549 int ret, idx;
551 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA - 1);
552 if (idx < 0)
553 return -ENOSPC;
555 INIT_LIST_HEAD(&msta->rc_list);
556 INIT_LIST_HEAD(&msta->stats_list);
557 INIT_LIST_HEAD(&msta->poll_list);
558 msta->vif = mvif;
559 msta->wcid.sta = 1;
560 msta->wcid.idx = idx;
561 msta->wcid.ext_phy = mvif->band_idx;
562 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
563 msta->stats.jiffies = jiffies;
565 mt7915_mac_wtbl_update(dev, idx,
566 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
568 ret = mt7915_mcu_add_sta(dev, vif, sta, true);
569 if (ret)
570 return ret;
572 return mt7915_mcu_add_sta_adv(dev, vif, sta, true);
575 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
576 struct ieee80211_sta *sta)
578 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
579 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
581 mt7915_mcu_add_sta_adv(dev, vif, sta, false);
582 mt7915_mcu_add_sta(dev, vif, sta, false);
584 mt7915_mac_wtbl_update(dev, msta->wcid.idx,
585 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
587 spin_lock_bh(&dev->sta_poll_lock);
588 if (!list_empty(&msta->poll_list))
589 list_del_init(&msta->poll_list);
590 if (!list_empty(&msta->stats_list))
591 list_del_init(&msta->stats_list);
592 if (!list_empty(&msta->rc_list))
593 list_del_init(&msta->rc_list);
594 spin_unlock_bh(&dev->sta_poll_lock);
597 static void mt7915_tx(struct ieee80211_hw *hw,
598 struct ieee80211_tx_control *control,
599 struct sk_buff *skb)
601 struct mt7915_dev *dev = mt7915_hw_dev(hw);
602 struct mt76_phy *mphy = hw->priv;
603 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
604 struct ieee80211_vif *vif = info->control.vif;
605 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
607 if (control->sta) {
608 struct mt7915_sta *sta;
610 sta = (struct mt7915_sta *)control->sta->drv_priv;
611 wcid = &sta->wcid;
614 if (vif && !control->sta) {
615 struct mt7915_vif *mvif;
617 mvif = (struct mt7915_vif *)vif->drv_priv;
618 wcid = &mvif->sta.wcid;
621 mt76_tx(mphy, control->sta, wcid, skb);
624 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
626 struct mt7915_dev *dev = mt7915_hw_dev(hw);
627 struct mt7915_phy *phy = mt7915_hw_phy(hw);
629 mutex_lock(&dev->mt76.mutex);
630 mt7915_mcu_set_rts_thresh(phy, val);
631 mutex_unlock(&dev->mt76.mutex);
633 return 0;
636 static int
637 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
638 struct ieee80211_ampdu_params *params)
640 enum ieee80211_ampdu_mlme_action action = params->action;
641 struct mt7915_dev *dev = mt7915_hw_dev(hw);
642 struct ieee80211_sta *sta = params->sta;
643 struct ieee80211_txq *txq = sta->txq[params->tid];
644 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
645 u16 tid = params->tid;
646 u16 ssn = params->ssn;
647 struct mt76_txq *mtxq;
648 int ret = 0;
650 if (!txq)
651 return -EINVAL;
653 mtxq = (struct mt76_txq *)txq->drv_priv;
655 mutex_lock(&dev->mt76.mutex);
656 switch (action) {
657 case IEEE80211_AMPDU_RX_START:
658 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
659 params->buf_size);
660 mt7915_mcu_add_rx_ba(dev, params, true);
661 break;
662 case IEEE80211_AMPDU_RX_STOP:
663 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
664 mt7915_mcu_add_rx_ba(dev, params, false);
665 break;
666 case IEEE80211_AMPDU_TX_OPERATIONAL:
667 mtxq->aggr = true;
668 mtxq->send_bar = false;
669 mt7915_mcu_add_tx_ba(dev, params, true);
670 break;
671 case IEEE80211_AMPDU_TX_STOP_FLUSH:
672 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
673 mtxq->aggr = false;
674 clear_bit(tid, &msta->ampdu_state);
675 mt7915_mcu_add_tx_ba(dev, params, false);
676 break;
677 case IEEE80211_AMPDU_TX_START:
678 set_bit(tid, &msta->ampdu_state);
679 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
680 break;
681 case IEEE80211_AMPDU_TX_STOP_CONT:
682 mtxq->aggr = false;
683 clear_bit(tid, &msta->ampdu_state);
684 mt7915_mcu_add_tx_ba(dev, params, false);
685 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
686 break;
688 mutex_unlock(&dev->mt76.mutex);
690 return ret;
693 static int
694 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
695 struct ieee80211_sta *sta)
697 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
698 IEEE80211_STA_NONE);
701 static int
702 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
703 struct ieee80211_sta *sta)
705 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
706 IEEE80211_STA_NOTEXIST);
709 static int
710 mt7915_get_stats(struct ieee80211_hw *hw,
711 struct ieee80211_low_level_stats *stats)
713 struct mt7915_phy *phy = mt7915_hw_phy(hw);
714 struct mib_stats *mib = &phy->mib;
716 stats->dot11RTSSuccessCount = mib->rts_cnt;
717 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
718 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
719 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
721 return 0;
724 static u64
725 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
727 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
728 struct mt7915_dev *dev = mt7915_hw_dev(hw);
729 struct mt7915_phy *phy = mt7915_hw_phy(hw);
730 bool band = phy != &dev->phy;
731 union {
732 u64 t64;
733 u32 t32[2];
734 } tsf;
735 u16 n;
737 mutex_lock(&dev->mt76.mutex);
739 n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
740 /* TSF software read */
741 mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE);
742 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
743 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
745 mutex_unlock(&dev->mt76.mutex);
747 return tsf.t64;
750 static void
751 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
752 u64 timestamp)
754 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
755 struct mt7915_dev *dev = mt7915_hw_dev(hw);
756 struct mt7915_phy *phy = mt7915_hw_phy(hw);
757 bool band = phy != &dev->phy;
758 union {
759 u64 t64;
760 u32 t32[2];
761 } tsf = { .t64 = timestamp, };
762 u16 n;
764 mutex_lock(&dev->mt76.mutex);
766 n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
767 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
768 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
769 /* TSF software overwrite */
770 mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_WRITE);
772 mutex_unlock(&dev->mt76.mutex);
775 static void
776 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
778 struct mt7915_phy *phy = mt7915_hw_phy(hw);
779 struct mt7915_dev *dev = phy->dev;
781 mutex_lock(&dev->mt76.mutex);
782 phy->coverage_class = max_t(s16, coverage_class, 0);
783 mt7915_mac_set_timing(phy);
784 mutex_unlock(&dev->mt76.mutex);
787 static int
788 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
790 struct mt7915_dev *dev = mt7915_hw_dev(hw);
791 struct mt7915_phy *phy = mt7915_hw_phy(hw);
792 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
793 bool ext_phy = phy != &dev->phy;
795 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
796 return -EINVAL;
798 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
799 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
801 mutex_lock(&dev->mt76.mutex);
803 phy->mt76->antenna_mask = tx_ant;
805 if (ext_phy) {
806 if (dev->chainmask == 0xf)
807 tx_ant <<= 2;
808 else
809 tx_ant <<= 1;
811 phy->chainmask = tx_ant;
813 mt76_set_stream_caps(phy->mt76, true);
814 mt7915_set_stream_vht_txbf_caps(phy);
815 mt7915_set_stream_he_caps(phy);
817 mutex_unlock(&dev->mt76.mutex);
819 return 0;
822 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
823 struct ieee80211_vif *vif,
824 struct ieee80211_sta *sta,
825 struct station_info *sinfo)
827 struct mt7915_phy *phy = mt7915_hw_phy(hw);
828 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
829 struct mt7915_sta_stats *stats = &msta->stats;
831 if (mt7915_mcu_get_rx_rate(phy, vif, sta, &sinfo->rxrate) == 0)
832 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
834 if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
835 return;
837 if (stats->tx_rate.legacy) {
838 sinfo->txrate.legacy = stats->tx_rate.legacy;
839 } else {
840 sinfo->txrate.mcs = stats->tx_rate.mcs;
841 sinfo->txrate.nss = stats->tx_rate.nss;
842 sinfo->txrate.bw = stats->tx_rate.bw;
843 sinfo->txrate.he_gi = stats->tx_rate.he_gi;
844 sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
845 sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
847 sinfo->txrate.flags = stats->tx_rate.flags;
848 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
851 static void
852 mt7915_sta_rc_update(struct ieee80211_hw *hw,
853 struct ieee80211_vif *vif,
854 struct ieee80211_sta *sta,
855 u32 changed)
857 struct mt7915_dev *dev = mt7915_hw_dev(hw);
858 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
860 spin_lock_bh(&dev->sta_poll_lock);
861 msta->stats.changed |= changed;
862 if (list_empty(&msta->rc_list))
863 list_add_tail(&msta->rc_list, &dev->sta_rc_list);
864 spin_unlock_bh(&dev->sta_poll_lock);
866 ieee80211_queue_work(hw, &dev->rc_work);
869 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
870 struct ieee80211_vif *vif,
871 struct ieee80211_sta *sta,
872 bool enabled)
874 struct mt7915_dev *dev = mt7915_hw_dev(hw);
875 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
877 if (enabled)
878 set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
879 else
880 clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
882 mt7915_mcu_sta_update_hdr_trans(dev, vif, sta);
885 const struct ieee80211_ops mt7915_ops = {
886 .tx = mt7915_tx,
887 .start = mt7915_start,
888 .stop = mt7915_stop,
889 .add_interface = mt7915_add_interface,
890 .remove_interface = mt7915_remove_interface,
891 .config = mt7915_config,
892 .conf_tx = mt7915_conf_tx,
893 .configure_filter = mt7915_configure_filter,
894 .bss_info_changed = mt7915_bss_info_changed,
895 .sta_add = mt7915_sta_add,
896 .sta_remove = mt7915_sta_remove,
897 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
898 .sta_rc_update = mt7915_sta_rc_update,
899 .set_key = mt7915_set_key,
900 .ampdu_action = mt7915_ampdu_action,
901 .set_rts_threshold = mt7915_set_rts_threshold,
902 .wake_tx_queue = mt76_wake_tx_queue,
903 .sw_scan_start = mt76_sw_scan,
904 .sw_scan_complete = mt76_sw_scan_complete,
905 .release_buffered_frames = mt76_release_buffered_frames,
906 .get_txpower = mt76_get_txpower,
907 .channel_switch_beacon = mt7915_channel_switch_beacon,
908 .get_stats = mt7915_get_stats,
909 .get_tsf = mt7915_get_tsf,
910 .set_tsf = mt7915_set_tsf,
911 .get_survey = mt76_get_survey,
912 .get_antenna = mt76_get_antenna,
913 .set_antenna = mt7915_set_antenna,
914 .set_coverage_class = mt7915_set_coverage_class,
915 .sta_statistics = mt7915_sta_statistics,
916 .sta_set_4addr = mt7915_sta_set_4addr,
917 CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
918 CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
919 #ifdef CONFIG_MAC80211_DEBUGFS
920 .sta_add_debugfs = mt7915_sta_add_debugfs,
921 #endif