1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
7 #include <linux/module.h>
12 mt7921_gen_ppe_thresh(u8 *he_ppet, int nss)
14 u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */
15 u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71};
17 he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) |
18 FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK,
21 ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE *
22 nss * hweight8(ru_bit_mask) * 2;
23 ppet_size = DIV_ROUND_UP(ppet_bits, 8);
25 for (i = 0; i < ppet_size - 1; i++)
26 he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3];
28 he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] &
29 (0xff >> (8 - (ppet_bits - 1) % 8));
33 mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band,
34 struct ieee80211_sband_iftype_data *data)
37 int nss = hweight8(phy->mt76->chainmask);
40 for (i = 0; i < 8; i++) {
42 mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2));
44 mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2));
47 for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
48 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
49 struct ieee80211_he_cap_elem *he_cap_elem =
51 struct ieee80211_he_mcs_nss_supp *he_mcs =
52 &he_cap->he_mcs_nss_supp;
55 case NL80211_IFTYPE_STATION:
61 data[idx].types_mask = BIT(i);
62 he_cap->has_he = true;
64 he_cap_elem->mac_cap_info[0] =
65 IEEE80211_HE_MAC_CAP0_HTC_HE;
66 he_cap_elem->mac_cap_info[3] =
67 IEEE80211_HE_MAC_CAP3_OMI_CONTROL |
68 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3;
69 he_cap_elem->mac_cap_info[4] =
70 IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU;
72 if (band == NL80211_BAND_2GHZ)
73 he_cap_elem->phy_cap_info[0] =
74 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
75 else if (band == NL80211_BAND_5GHZ)
76 he_cap_elem->phy_cap_info[0] =
77 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G;
79 he_cap_elem->phy_cap_info[1] =
80 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD;
81 he_cap_elem->phy_cap_info[2] =
82 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ |
83 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ;
86 case NL80211_IFTYPE_STATION:
87 he_cap_elem->mac_cap_info[0] |=
88 IEEE80211_HE_MAC_CAP0_TWT_REQ;
89 he_cap_elem->mac_cap_info[1] |=
90 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US;
92 if (band == NL80211_BAND_2GHZ)
93 he_cap_elem->phy_cap_info[0] |=
94 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G;
95 else if (band == NL80211_BAND_5GHZ)
96 he_cap_elem->phy_cap_info[0] |=
97 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G;
99 he_cap_elem->phy_cap_info[1] |=
100 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A |
101 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US;
102 he_cap_elem->phy_cap_info[3] |=
103 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK |
104 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK;
105 he_cap_elem->phy_cap_info[6] |=
106 IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
107 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE |
108 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT;
109 he_cap_elem->phy_cap_info[7] |=
110 IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
111 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI;
112 he_cap_elem->phy_cap_info[8] |=
113 IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
114 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484;
115 he_cap_elem->phy_cap_info[9] |=
116 IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
117 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
118 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
119 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
120 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
121 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
125 he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map);
126 he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map);
128 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
129 if (he_cap_elem->phy_cap_info[6] &
130 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
131 mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss);
133 he_cap_elem->phy_cap_info[9] |=
134 IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US;
142 void mt7921_set_stream_he_caps(struct mt7921_phy *phy)
144 struct ieee80211_sband_iftype_data *data;
145 struct ieee80211_supported_band *band;
148 if (phy->mt76->cap.has_2ghz) {
149 data = phy->iftype[NL80211_BAND_2GHZ];
150 n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data);
152 band = &phy->mt76->sband_2g.sband;
153 band->iftype_data = data;
154 band->n_iftype_data = n;
157 if (phy->mt76->cap.has_5ghz) {
158 data = phy->iftype[NL80211_BAND_5GHZ];
159 n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data);
161 band = &phy->mt76->sband_5g.sband;
162 band->iftype_data = data;
163 band->n_iftype_data = n;
167 int __mt7921_start(struct mt7921_phy *phy)
169 struct mt76_phy *mphy = phy->mt76;
172 err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false);
176 err = mt76_connac_mcu_set_channel_domain(mphy);
180 err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH);
184 err = mt76_connac_mcu_set_rate_txpower(phy->mt76);
188 mt7921_mac_reset_counters(phy);
189 set_bit(MT76_STATE_RUNNING, &mphy->state);
191 ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
192 MT7921_WATCHDOG_TIME);
197 static int mt7921_start(struct ieee80211_hw *hw)
199 struct mt7921_phy *phy = mt7921_hw_phy(hw);
202 mt7921_mutex_acquire(phy->dev);
203 err = __mt7921_start(phy);
204 mt7921_mutex_release(phy->dev);
209 static void mt7921_stop(struct ieee80211_hw *hw)
211 struct mt7921_dev *dev = mt7921_hw_dev(hw);
212 struct mt7921_phy *phy = mt7921_hw_phy(hw);
214 cancel_delayed_work_sync(&phy->mt76->mac_work);
216 cancel_delayed_work_sync(&dev->pm.ps_work);
217 cancel_work_sync(&dev->pm.wake_work);
218 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
220 mt7921_mutex_acquire(dev);
221 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
222 mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
223 mt7921_mutex_release(dev);
226 static inline int get_free_idx(u32 mask, u8 start, u8 end)
228 return ffs(~mask & GENMASK(end, start));
231 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
236 case NL80211_IFTYPE_STATION:
237 /* prefer hw bssid slot 1-3 */
238 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
242 /* next, try to find a free repeater entry for the sta */
243 i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
244 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
248 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
252 if (~mask & BIT(HW_BSSID_0))
256 case NL80211_IFTYPE_MONITOR:
257 /* ap uses hw bssid 0 and ext bssid */
258 if (~mask & BIT(HW_BSSID_0))
261 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
274 static int mt7921_add_interface(struct ieee80211_hw *hw,
275 struct ieee80211_vif *vif)
277 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
278 struct mt7921_dev *dev = mt7921_hw_dev(hw);
279 struct mt7921_phy *phy = mt7921_hw_phy(hw);
280 struct mt76_txq *mtxq;
283 mt7921_mutex_acquire(dev);
285 if (vif->type == NL80211_IFTYPE_MONITOR &&
286 is_zero_ether_addr(vif->addr))
287 phy->monitor_vif = vif;
289 mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
290 if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) {
295 idx = get_omac_idx(vif->type, phy->omac_mask);
300 mvif->mt76.omac_idx = idx;
302 mvif->mt76.band_idx = 0;
303 mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS;
305 ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid,
310 dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
311 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
313 idx = MT7921_WTBL_RESERVED - mvif->mt76.idx;
315 INIT_LIST_HEAD(&mvif->sta.stats_list);
316 INIT_LIST_HEAD(&mvif->sta.poll_list);
317 mvif->sta.wcid.idx = idx;
318 mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
319 mvif->sta.wcid.hw_key_idx = -1;
320 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
321 mt7921_mac_wtbl_update(dev, idx,
322 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
324 ewma_rssi_init(&mvif->rssi);
326 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
328 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
329 mtxq->wcid = &mvif->sta.wcid;
332 if (vif->type != NL80211_IFTYPE_AP &&
333 (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3))
334 vif->offload_flags = 0;
336 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
339 mt7921_mutex_release(dev);
344 static void mt7921_remove_interface(struct ieee80211_hw *hw,
345 struct ieee80211_vif *vif)
347 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
348 struct mt7921_sta *msta = &mvif->sta;
349 struct mt7921_dev *dev = mt7921_hw_dev(hw);
350 struct mt7921_phy *phy = mt7921_hw_phy(hw);
351 int idx = msta->wcid.idx;
353 if (vif == phy->monitor_vif)
354 phy->monitor_vif = NULL;
356 mt7921_mutex_acquire(dev);
357 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
358 mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false);
360 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
362 dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
363 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
364 mt7921_mutex_release(dev);
366 spin_lock_bh(&dev->sta_poll_lock);
367 if (!list_empty(&msta->poll_list))
368 list_del_init(&msta->poll_list);
369 spin_unlock_bh(&dev->sta_poll_lock);
372 int mt7921_set_channel(struct mt7921_phy *phy)
374 struct mt7921_dev *dev = phy->dev;
377 cancel_delayed_work_sync(&phy->mt76->mac_work);
379 mt7921_mutex_acquire(dev);
380 set_bit(MT76_RESET, &phy->mt76->state);
382 mt76_set_channel(phy->mt76);
384 ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
388 mt7921_mac_set_timing(phy);
390 mt7921_mac_reset_counters(phy);
394 clear_bit(MT76_RESET, &phy->mt76->state);
395 mt7921_mutex_release(dev);
397 mt76_worker_schedule(&dev->mt76.tx_worker);
398 ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work,
399 MT7921_WATCHDOG_TIME);
404 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
405 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
406 struct ieee80211_key_conf *key)
408 struct mt7921_dev *dev = mt7921_hw_dev(hw);
409 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
410 struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv :
412 struct mt76_wcid *wcid = &msta->wcid;
413 u8 *wcid_keyidx = &wcid->hw_key_idx;
414 int idx = key->keyidx, err = 0;
416 /* The hardware does not support per-STA RX GTK, fallback
417 * to software mode for these.
419 if ((vif->type == NL80211_IFTYPE_ADHOC ||
420 vif->type == NL80211_IFTYPE_MESH_POINT) &&
421 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
422 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
423 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
426 /* fall back to sw encryption for unsupported ciphers */
427 switch (key->cipher) {
428 case WLAN_CIPHER_SUITE_AES_CMAC:
429 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
430 wcid_keyidx = &wcid->hw_key_idx2;
432 case WLAN_CIPHER_SUITE_TKIP:
433 case WLAN_CIPHER_SUITE_CCMP:
434 case WLAN_CIPHER_SUITE_CCMP_256:
435 case WLAN_CIPHER_SUITE_GCMP:
436 case WLAN_CIPHER_SUITE_GCMP_256:
437 case WLAN_CIPHER_SUITE_SMS4:
439 case WLAN_CIPHER_SUITE_WEP40:
440 case WLAN_CIPHER_SUITE_WEP104:
445 mt7921_mutex_acquire(dev);
449 else if (idx == *wcid_keyidx)
454 mt76_wcid_key_setup(&dev->mt76, wcid,
455 cmd == SET_KEY ? key : NULL);
457 err = mt7921_mcu_add_key(dev, vif, msta, key, cmd);
459 mt7921_mutex_release(dev);
464 static int mt7921_config(struct ieee80211_hw *hw, u32 changed)
466 struct mt7921_dev *dev = mt7921_hw_dev(hw);
467 struct mt7921_phy *phy = mt7921_hw_phy(hw);
470 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
471 ieee80211_stop_queues(hw);
472 ret = mt7921_set_channel(phy);
475 ieee80211_wake_queues(hw);
478 mt7921_mutex_acquire(dev);
480 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
481 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
484 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
486 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
488 mt76_rmw_field(dev, MT_DMA_DCR0(0), MT_DMA_DCR0_RXD_G5_EN,
490 mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
493 mt7921_mutex_release(dev);
499 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
500 const struct ieee80211_tx_queue_params *params)
502 struct mt7921_dev *dev = mt7921_hw_dev(hw);
503 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
505 /* no need to update right away, we'll get BSS_CHANGED_QOS */
506 queue = mt7921_lmac_mapping(dev, queue);
507 mvif->queue_params[queue] = *params;
512 static void mt7921_configure_filter(struct ieee80211_hw *hw,
513 unsigned int changed_flags,
514 unsigned int *total_flags,
517 struct mt7921_dev *dev = mt7921_hw_dev(hw);
518 struct mt7921_phy *phy = mt7921_hw_phy(hw);
519 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
520 MT_WF_RFCR1_DROP_BF_POLL |
521 MT_WF_RFCR1_DROP_BA |
522 MT_WF_RFCR1_DROP_CFEND |
523 MT_WF_RFCR1_DROP_CFACK;
526 #define MT76_FILTER(_flag, _hw) do { \
527 flags |= *total_flags & FIF_##_flag; \
528 phy->rxfilter &= ~(_hw); \
529 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
532 mt7921_mutex_acquire(dev);
534 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
535 MT_WF_RFCR_DROP_OTHER_BEACON |
536 MT_WF_RFCR_DROP_FRAME_REPORT |
537 MT_WF_RFCR_DROP_PROBEREQ |
538 MT_WF_RFCR_DROP_MCAST_FILTERED |
539 MT_WF_RFCR_DROP_MCAST |
540 MT_WF_RFCR_DROP_BCAST |
541 MT_WF_RFCR_DROP_DUPLICATE |
542 MT_WF_RFCR_DROP_A2_BSSID |
543 MT_WF_RFCR_DROP_UNWANTED_CTL |
544 MT_WF_RFCR_DROP_STBC_MULTI);
546 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
547 MT_WF_RFCR_DROP_A3_MAC |
548 MT_WF_RFCR_DROP_A3_BSSID);
550 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
552 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
553 MT_WF_RFCR_DROP_RTS |
554 MT_WF_RFCR_DROP_CTL_RSV |
555 MT_WF_RFCR_DROP_NDPA);
557 *total_flags = flags;
558 mt76_wr(dev, MT_WF_RFCR(0), phy->rxfilter);
560 if (*total_flags & FIF_CONTROL)
561 mt76_clear(dev, MT_WF_RFCR1(0), ctl_flags);
563 mt76_set(dev, MT_WF_RFCR1(0), ctl_flags);
565 mt7921_mutex_release(dev);
569 mt7921_bss_bcnft_apply(struct mt7921_dev *dev, struct ieee80211_vif *vif,
578 ret = mt7921_mcu_uni_bss_bcnft(dev, vif, true);
582 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
583 mt76_set(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
588 ret = mt7921_mcu_set_bss_pm(dev, vif, false);
592 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
593 mt76_clear(dev, MT_WF_RFCR(0), MT_WF_RFCR_DROP_OTHER_BEACON);
598 static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
599 struct ieee80211_vif *vif,
600 struct ieee80211_bss_conf *info,
603 struct mt7921_phy *phy = mt7921_hw_phy(hw);
604 struct mt7921_dev *dev = mt7921_hw_dev(hw);
606 mt7921_mutex_acquire(dev);
608 if (changed & BSS_CHANGED_ERP_SLOT) {
609 int slottime = info->use_short_slot ? 9 : 20;
611 if (slottime != phy->slottime) {
612 phy->slottime = slottime;
613 mt7921_mac_set_timing(phy);
617 /* ensure that enable txcmd_mode after bss_info */
618 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
619 mt7921_mcu_set_tx(dev, vif);
621 if (changed & BSS_CHANGED_PS)
622 mt7921_mcu_uni_bss_ps(dev, vif);
624 if (changed & BSS_CHANGED_ASSOC) {
625 mt7921_mcu_sta_add(dev, NULL, vif, true);
626 mt7921_bss_bcnft_apply(dev, vif, info->assoc);
629 if (changed & BSS_CHANGED_ARP_FILTER) {
630 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
632 mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
636 mt7921_mutex_release(dev);
639 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
640 struct ieee80211_sta *sta)
642 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
643 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
644 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
647 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1);
651 INIT_LIST_HEAD(&msta->stats_list);
652 INIT_LIST_HEAD(&msta->poll_list);
655 msta->wcid.idx = idx;
656 msta->wcid.ext_phy = mvif->mt76.band_idx;
657 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
658 msta->stats.jiffies = jiffies;
660 ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm);
664 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
665 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid,
668 mt7921_mac_wtbl_update(dev, idx,
669 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
671 ret = mt7921_mcu_sta_add(dev, sta, vif, true);
675 mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
680 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
681 struct ieee80211_sta *sta)
683 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);
684 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
686 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
687 mt76_connac_pm_wake(&dev->mphy, &dev->pm);
689 mt7921_mcu_sta_add(dev, sta, vif, false);
690 mt7921_mac_wtbl_update(dev, msta->wcid.idx,
691 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
693 if (vif->type == NL80211_IFTYPE_STATION) {
694 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
696 ewma_rssi_init(&mvif->rssi);
698 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif,
699 &mvif->sta.wcid, false);
702 spin_lock_bh(&dev->sta_poll_lock);
703 if (!list_empty(&msta->poll_list))
704 list_del_init(&msta->poll_list);
705 if (!list_empty(&msta->stats_list))
706 list_del_init(&msta->stats_list);
707 spin_unlock_bh(&dev->sta_poll_lock);
709 mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
712 void mt7921_tx_worker(struct mt76_worker *w)
714 struct mt7921_dev *dev = container_of(w, struct mt7921_dev,
717 if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
718 queue_work(dev->mt76.wq, &dev->pm.wake_work);
722 mt76_txq_schedule_all(&dev->mphy);
723 mt76_connac_pm_unref(&dev->pm);
726 static void mt7921_tx(struct ieee80211_hw *hw,
727 struct ieee80211_tx_control *control,
730 struct mt7921_dev *dev = mt7921_hw_dev(hw);
731 struct mt76_phy *mphy = hw->priv;
732 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
733 struct ieee80211_vif *vif = info->control.vif;
734 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
738 struct mt7921_sta *sta;
740 sta = (struct mt7921_sta *)control->sta->drv_priv;
744 if (vif && !control->sta) {
745 struct mt7921_vif *mvif;
747 mvif = (struct mt7921_vif *)vif->drv_priv;
748 wcid = &mvif->sta.wcid;
751 if (mt76_connac_pm_ref(mphy, &dev->pm)) {
752 mt76_tx(mphy, control->sta, wcid, skb);
753 mt76_connac_pm_unref(&dev->pm);
757 qid = skb_get_queue_mapping(skb);
758 if (qid >= MT_TXQ_PSD) {
759 qid = IEEE80211_AC_BE;
760 skb_set_queue_mapping(skb, qid);
763 mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
766 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
768 struct mt7921_dev *dev = mt7921_hw_dev(hw);
770 mt7921_mutex_acquire(dev);
771 mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0);
772 mt7921_mutex_release(dev);
778 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
779 struct ieee80211_ampdu_params *params)
781 enum ieee80211_ampdu_mlme_action action = params->action;
782 struct mt7921_dev *dev = mt7921_hw_dev(hw);
783 struct ieee80211_sta *sta = params->sta;
784 struct ieee80211_txq *txq = sta->txq[params->tid];
785 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
786 u16 tid = params->tid;
787 u16 ssn = params->ssn;
788 struct mt76_txq *mtxq;
794 mtxq = (struct mt76_txq *)txq->drv_priv;
796 mt7921_mutex_acquire(dev);
798 case IEEE80211_AMPDU_RX_START:
799 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
801 mt7921_mcu_uni_rx_ba(dev, params, true);
803 case IEEE80211_AMPDU_RX_STOP:
804 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
805 mt7921_mcu_uni_rx_ba(dev, params, false);
807 case IEEE80211_AMPDU_TX_OPERATIONAL:
809 mtxq->send_bar = false;
810 mt7921_mcu_uni_tx_ba(dev, params, true);
812 case IEEE80211_AMPDU_TX_STOP_FLUSH:
813 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
815 clear_bit(tid, &msta->ampdu_state);
816 mt7921_mcu_uni_tx_ba(dev, params, false);
818 case IEEE80211_AMPDU_TX_START:
819 set_bit(tid, &msta->ampdu_state);
820 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
822 case IEEE80211_AMPDU_TX_STOP_CONT:
824 clear_bit(tid, &msta->ampdu_state);
825 mt7921_mcu_uni_tx_ba(dev, params, false);
826 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
829 mt7921_mutex_release(dev);
835 mt7921_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
836 struct ieee80211_sta *sta)
838 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
843 mt7921_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
844 struct ieee80211_sta *sta)
846 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
847 IEEE80211_STA_NOTEXIST);
851 mt7921_get_stats(struct ieee80211_hw *hw,
852 struct ieee80211_low_level_stats *stats)
854 struct mt7921_phy *phy = mt7921_hw_phy(hw);
855 struct mib_stats *mib = &phy->mib;
857 mt7921_mutex_acquire(phy->dev);
859 stats->dot11RTSSuccessCount = mib->rts_cnt;
860 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
861 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
862 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
864 memset(mib, 0, sizeof(*mib));
866 mt7921_mutex_release(phy->dev);
872 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
874 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
875 struct mt7921_dev *dev = mt7921_hw_dev(hw);
876 u8 omac_idx = mvif->mt76.omac_idx;
883 mt7921_mutex_acquire(dev);
885 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
886 /* TSF software read */
887 mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE);
888 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0));
889 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0));
891 mt7921_mutex_release(dev);
897 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
900 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv;
901 struct mt7921_dev *dev = mt7921_hw_dev(hw);
902 u8 omac_idx = mvif->mt76.omac_idx;
906 } tsf = { .t64 = timestamp, };
909 mt7921_mutex_acquire(dev);
911 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx;
912 mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]);
913 mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]);
914 /* TSF software overwrite */
915 mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE);
917 mt7921_mutex_release(dev);
921 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
923 struct mt7921_phy *phy = mt7921_hw_phy(hw);
924 struct mt7921_dev *dev = phy->dev;
926 mt7921_mutex_acquire(dev);
927 phy->coverage_class = max_t(s16, coverage_class, 0);
928 mt7921_mac_set_timing(phy);
929 mt7921_mutex_release(dev);
932 void mt7921_scan_work(struct work_struct *work)
934 struct mt7921_phy *phy;
936 phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy,
940 struct mt7921_mcu_rxd *rxd;
943 spin_lock_bh(&phy->dev->mt76.lock);
944 skb = __skb_dequeue(&phy->scan_event_list);
945 spin_unlock_bh(&phy->dev->mt76.lock);
950 rxd = (struct mt7921_mcu_rxd *)skb->data;
951 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
952 ieee80211_sched_scan_results(phy->mt76->hw);
953 } else if (test_and_clear_bit(MT76_HW_SCANNING,
954 &phy->mt76->state)) {
955 struct cfg80211_scan_info info = {
959 ieee80211_scan_completed(phy->mt76->hw, &info);
966 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
967 struct ieee80211_scan_request *req)
969 struct mt7921_dev *dev = mt7921_hw_dev(hw);
970 struct mt76_phy *mphy = hw->priv;
973 mt7921_mutex_acquire(dev);
974 err = mt76_connac_mcu_hw_scan(mphy, vif, req);
975 mt7921_mutex_release(dev);
981 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
983 struct mt7921_dev *dev = mt7921_hw_dev(hw);
984 struct mt76_phy *mphy = hw->priv;
986 mt7921_mutex_acquire(dev);
987 mt76_connac_mcu_cancel_hw_scan(mphy, vif);
988 mt7921_mutex_release(dev);
992 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
993 struct cfg80211_sched_scan_request *req,
994 struct ieee80211_scan_ies *ies)
996 struct mt7921_dev *dev = mt7921_hw_dev(hw);
997 struct mt76_phy *mphy = hw->priv;
1000 mt7921_mutex_acquire(dev);
1002 err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1006 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1008 mt7921_mutex_release(dev);
1014 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1016 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1017 struct mt76_phy *mphy = hw->priv;
1020 mt7921_mutex_acquire(dev);
1021 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1022 mt7921_mutex_release(dev);
1028 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1030 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1031 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1032 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
1034 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
1037 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
1038 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
1040 mt7921_mutex_acquire(dev);
1042 phy->mt76->antenna_mask = tx_ant;
1043 phy->mt76->chainmask = tx_ant;
1045 mt76_set_stream_caps(phy->mt76, true);
1046 mt7921_set_stream_he_caps(phy);
1048 mt7921_mutex_release(dev);
1053 static void mt7921_sta_statistics(struct ieee80211_hw *hw,
1054 struct ieee80211_vif *vif,
1055 struct ieee80211_sta *sta,
1056 struct station_info *sinfo)
1058 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv;
1059 struct mt7921_sta_stats *stats = &msta->stats;
1061 if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
1064 if (stats->tx_rate.legacy) {
1065 sinfo->txrate.legacy = stats->tx_rate.legacy;
1067 sinfo->txrate.mcs = stats->tx_rate.mcs;
1068 sinfo->txrate.nss = stats->tx_rate.nss;
1069 sinfo->txrate.bw = stats->tx_rate.bw;
1070 sinfo->txrate.he_gi = stats->tx_rate.he_gi;
1071 sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
1072 sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
1074 sinfo->txrate.flags = stats->tx_rate.flags;
1075 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1079 static int mt7921_suspend(struct ieee80211_hw *hw,
1080 struct cfg80211_wowlan *wowlan)
1082 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1083 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1086 cancel_delayed_work_sync(&phy->scan_work);
1087 cancel_delayed_work_sync(&phy->mt76->mac_work);
1089 cancel_delayed_work_sync(&dev->pm.ps_work);
1090 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1092 mt7921_mutex_acquire(dev);
1094 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1096 set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1097 ieee80211_iterate_active_interfaces(hw,
1098 IEEE80211_IFACE_ITER_RESUME_ALL,
1099 mt76_connac_mcu_set_suspend_iter,
1102 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1104 mt7921_mutex_release(dev);
1109 static int mt7921_resume(struct ieee80211_hw *hw)
1111 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1112 struct mt7921_phy *phy = mt7921_hw_phy(hw);
1115 mt7921_mutex_acquire(dev);
1117 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1121 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1122 clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1123 ieee80211_iterate_active_interfaces(hw,
1124 IEEE80211_IFACE_ITER_RESUME_ALL,
1125 mt76_connac_mcu_set_suspend_iter,
1128 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
1129 MT7921_WATCHDOG_TIME);
1132 mt7921_mutex_release(dev);
1137 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1139 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1140 struct mt76_dev *mdev = &dev->mt76;
1142 device_set_wakeup_enable(mdev->dev, enabled);
1145 static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
1146 struct ieee80211_vif *vif,
1147 struct cfg80211_gtk_rekey_data *data)
1149 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1151 mt7921_mutex_acquire(dev);
1152 mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1153 mt7921_mutex_release(dev);
1155 #endif /* CONFIG_PM */
1157 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1158 u32 queues, bool drop)
1160 struct mt7921_dev *dev = mt7921_hw_dev(hw);
1162 wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy),
1166 const struct ieee80211_ops mt7921_ops = {
1168 .start = mt7921_start,
1169 .stop = mt7921_stop,
1170 .add_interface = mt7921_add_interface,
1171 .remove_interface = mt7921_remove_interface,
1172 .config = mt7921_config,
1173 .conf_tx = mt7921_conf_tx,
1174 .configure_filter = mt7921_configure_filter,
1175 .bss_info_changed = mt7921_bss_info_changed,
1176 .sta_add = mt7921_sta_add,
1177 .sta_remove = mt7921_sta_remove,
1178 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1179 .set_key = mt7921_set_key,
1180 .ampdu_action = mt7921_ampdu_action,
1181 .set_rts_threshold = mt7921_set_rts_threshold,
1182 .wake_tx_queue = mt76_wake_tx_queue,
1183 .release_buffered_frames = mt76_release_buffered_frames,
1184 .get_txpower = mt76_get_txpower,
1185 .get_stats = mt7921_get_stats,
1186 .get_tsf = mt7921_get_tsf,
1187 .set_tsf = mt7921_set_tsf,
1188 .get_survey = mt76_get_survey,
1189 .get_antenna = mt76_get_antenna,
1190 .set_antenna = mt7921_set_antenna,
1191 .set_coverage_class = mt7921_set_coverage_class,
1192 .hw_scan = mt7921_hw_scan,
1193 .cancel_hw_scan = mt7921_cancel_hw_scan,
1194 .sta_statistics = mt7921_sta_statistics,
1195 .sched_scan_start = mt7921_start_sched_scan,
1196 .sched_scan_stop = mt7921_stop_sched_scan,
1198 .suspend = mt7921_suspend,
1199 .resume = mt7921_resume,
1200 .set_wakeup = mt7921_set_wakeup,
1201 .set_rekey_data = mt7921_set_rekey_data,
1202 #endif /* CONFIG_PM */
1203 .flush = mt7921_flush,