1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #include <linux/etherdevice.h> 5 #include "mt7921.h" 6 #include "mac.h" 7 #include "mcu.h" 8 #include "eeprom.h" 9 10 #define CCK_RATE(_idx, _rate) { \ 11 .bitrate = _rate, \ 12 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \ 13 .hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx), \ 14 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (4 + (_idx)), \ 15 } 16 17 #define OFDM_RATE(_idx, _rate) { \ 18 .bitrate = _rate, \ 19 .hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx), \ 20 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx), \ 21 } 22 23 static struct ieee80211_rate mt7921_rates[] = { 24 CCK_RATE(0, 10), 25 CCK_RATE(1, 20), 26 CCK_RATE(2, 55), 27 CCK_RATE(3, 110), 28 OFDM_RATE(11, 60), 29 OFDM_RATE(15, 90), 30 OFDM_RATE(10, 120), 31 OFDM_RATE(14, 180), 32 OFDM_RATE(9, 240), 33 OFDM_RATE(13, 360), 34 OFDM_RATE(8, 480), 35 OFDM_RATE(12, 540), 36 }; 37 38 static const struct ieee80211_iface_limit if_limits[] = { 39 { 40 .max = MT7921_MAX_INTERFACES, 41 .types = BIT(NL80211_IFTYPE_STATION) 42 } 43 }; 44 45 static const struct ieee80211_iface_combination if_comb[] = { 46 { 47 .limits = if_limits, 48 .n_limits = ARRAY_SIZE(if_limits), 49 .max_interfaces = MT7921_MAX_INTERFACES, 50 .num_different_channels = 1, 51 .beacon_int_infra_match = true, 52 } 53 }; 54 55 static void 56 mt7921_regd_notifier(struct wiphy *wiphy, 57 struct regulatory_request *request) 58 { 59 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 60 struct mt7921_dev *dev = mt7921_hw_dev(hw); 61 62 memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2)); 63 dev->mt76.region = request->dfs_region; 64 65 mt7921_mutex_acquire(dev); 66 mt76_connac_mcu_set_channel_domain(hw->priv); 67 mt7921_mutex_release(dev); 68 } 69 70 static void 71 mt7921_init_wiphy(struct ieee80211_hw *hw) 72 { 73 struct mt7921_phy *phy = mt7921_hw_phy(hw); 74 struct wiphy *wiphy = hw->wiphy; 75 76 hw->queues = 4; 77 hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF; 78 hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF; 79 80 phy->slottime = 9; 81 82 hw->sta_data_size = sizeof(struct mt7921_sta); 83 hw->vif_data_size = sizeof(struct mt7921_vif); 84 85 wiphy->iface_combinations = if_comb; 86 wiphy->n_iface_combinations = ARRAY_SIZE(if_comb); 87 wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN; 88 wiphy->max_scan_ssids = 4; 89 wiphy->max_sched_scan_plan_interval = 90 MT76_CONNAC_MAX_SCHED_SCAN_INTERVAL; 91 wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN; 92 wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID; 93 wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH; 94 wiphy->max_sched_scan_reqs = 1; 95 wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH; 96 wiphy->reg_notifier = mt7921_regd_notifier; 97 98 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL); 99 100 ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS); 101 ieee80211_hw_set(hw, HAS_RATE_CONTROL); 102 ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD); 103 ieee80211_hw_set(hw, WANT_MONITOR_VIF); 104 ieee80211_hw_set(hw, SUPPORTS_PS); 105 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS); 106 107 hw->max_tx_fragments = 4; 108 } 109 110 static void 111 mt7921_mac_init_band(struct mt7921_dev *dev, u8 band) 112 { 113 u32 mask, set; 114 115 mt76_rmw_field(dev, MT_TMAC_CTCR0(band), 116 MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f); 117 mt76_set(dev, MT_TMAC_CTCR0(band), 118 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN | 119 MT_TMAC_CTCR0_INS_DDLMT_EN); 120 121 mask = MT_MDP_RCFR0_MCU_RX_MGMT | 122 MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR | 123 MT_MDP_RCFR0_MCU_RX_CTL_BAR; 124 set = FIELD_PREP(MT_MDP_RCFR0_MCU_RX_MGMT, MT_MDP_TO_HIF) | 125 FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR, MT_MDP_TO_HIF) | 126 FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_BAR, MT_MDP_TO_HIF); 127 mt76_rmw(dev, MT_MDP_BNRCFR0(band), mask, set); 128 129 mask = MT_MDP_RCFR1_MCU_RX_BYPASS | 130 MT_MDP_RCFR1_RX_DROPPED_UCAST | 131 MT_MDP_RCFR1_RX_DROPPED_MCAST; 132 set = FIELD_PREP(MT_MDP_RCFR1_MCU_RX_BYPASS, MT_MDP_TO_HIF) | 133 FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_UCAST, MT_MDP_TO_HIF) | 134 FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_MCAST, MT_MDP_TO_HIF); 135 mt76_rmw(dev, MT_MDP_BNRCFR1(band), mask, set); 136 137 mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN); 138 mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN); 139 140 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536); 141 /* disable rx rate report by default due to hw issues */ 142 mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN); 143 } 144 145 static void mt7921_mac_init(struct mt7921_dev *dev) 146 { 147 int i; 148 149 mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); 150 /* disable hardware de-agg */ 151 mt76_clear(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); 152 mt76_clear(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN); 153 154 for (i = 0; i < MT7921_WTBL_SIZE; i++) 155 mt7921_mac_wtbl_update(dev, i, 156 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 157 for (i = 0; i < 2; i++) 158 mt7921_mac_init_band(dev, i); 159 160 mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0); 161 } 162 163 static void mt7921_init_work(struct work_struct *work) 164 { 165 struct mt7921_dev *dev = container_of(work, struct mt7921_dev, 166 init_work); 167 168 mt7921_mcu_set_eeprom(dev); 169 mt7921_mac_init(dev); 170 } 171 172 static int mt7921_init_hardware(struct mt7921_dev *dev) 173 { 174 int ret, idx; 175 176 INIT_WORK(&dev->init_work, mt7921_init_work); 177 spin_lock_init(&dev->token_lock); 178 idr_init(&dev->token); 179 180 ret = mt7921_dma_init(dev); 181 if (ret) 182 return ret; 183 184 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 185 186 /* force firmware operation mode into normal state, 187 * which should be set before firmware download stage. 188 */ 189 mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE); 190 191 ret = mt7921_mcu_init(dev); 192 if (ret) 193 return ret; 194 195 ret = mt7921_eeprom_init(dev); 196 if (ret < 0) 197 return ret; 198 199 /* Beacon and mgmt frames should occupy wcid 0 */ 200 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1); 201 if (idx) 202 return -ENOSPC; 203 204 dev->mt76.global_wcid.idx = idx; 205 dev->mt76.global_wcid.hw_key_idx = -1; 206 dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET; 207 rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid); 208 209 return 0; 210 } 211 212 int mt7921_register_device(struct mt7921_dev *dev) 213 { 214 struct ieee80211_hw *hw = mt76_hw(dev); 215 int ret; 216 217 dev->phy.dev = dev; 218 dev->phy.mt76 = &dev->mt76.phy; 219 dev->mt76.phy.priv = &dev->phy; 220 221 INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work); 222 INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work); 223 init_completion(&dev->pm.wake_cmpl); 224 spin_lock_init(&dev->pm.txq_lock); 225 set_bit(MT76_STATE_PM, &dev->mphy.state); 226 INIT_LIST_HEAD(&dev->phy.stats_list); 227 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work); 228 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work); 229 INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work); 230 skb_queue_head_init(&dev->phy.scan_event_list); 231 skb_queue_head_init(&dev->coredump.msg_list); 232 INIT_LIST_HEAD(&dev->sta_poll_list); 233 spin_lock_init(&dev->sta_poll_lock); 234 235 init_waitqueue_head(&dev->reset_wait); 236 INIT_WORK(&dev->reset_work, mt7921_mac_reset_work); 237 238 ret = mt7921_init_hardware(dev); 239 if (ret) 240 return ret; 241 242 mt7921_init_wiphy(hw); 243 dev->pm.idle_timeout = MT7921_PM_TIMEOUT; 244 dev->mphy.sband_2g.sband.ht_cap.cap |= 245 IEEE80211_HT_CAP_LDPC_CODING | 246 IEEE80211_HT_CAP_MAX_AMSDU; 247 dev->mphy.sband_5g.sband.ht_cap.cap |= 248 IEEE80211_HT_CAP_LDPC_CODING | 249 IEEE80211_HT_CAP_MAX_AMSDU; 250 dev->mphy.sband_5g.sband.vht_cap.cap |= 251 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 | 252 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK; 253 dev->mphy.sband_5g.sband.vht_cap.cap |= 254 IEEE80211_VHT_CAP_SHORT_GI_160 | 255 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ; 256 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; 257 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; 258 259 mt76_set_stream_caps(&dev->mphy, true); 260 mt7921_set_stream_he_caps(&dev->phy); 261 262 ret = mt76_register_device(&dev->mt76, true, mt7921_rates, 263 ARRAY_SIZE(mt7921_rates)); 264 if (ret) 265 return ret; 266 267 ieee80211_queue_work(mt76_hw(dev), &dev->init_work); 268 269 return mt7921_init_debugfs(dev); 270 } 271 272 void mt7921_unregister_device(struct mt7921_dev *dev) 273 { 274 mt76_unregister_device(&dev->mt76); 275 mt7921_mcu_exit(dev); 276 mt7921_dma_cleanup(dev); 277 278 mt7921_tx_token_put(dev); 279 280 tasklet_disable(&dev->irq_tasklet); 281 mt76_free_device(&dev->mt76); 282 } 283