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 struct mt7921_phy *phy = mt7921_hw_phy(hw); 62 63 memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2)); 64 dev->mt76.region = request->dfs_region; 65 66 mt7921_mutex_acquire(dev); 67 mt76_connac_mcu_set_channel_domain(hw->priv); 68 mt76_connac_mcu_set_rate_txpower(phy->mt76); 69 mt7921_mutex_release(dev); 70 } 71 72 static void 73 mt7921_init_wiphy(struct ieee80211_hw *hw) 74 { 75 struct mt7921_phy *phy = mt7921_hw_phy(hw); 76 struct wiphy *wiphy = hw->wiphy; 77 78 hw->queues = 4; 79 hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF; 80 hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF; 81 82 hw->radiotap_timestamp.units_pos = 83 IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US; 84 85 phy->slottime = 9; 86 87 hw->sta_data_size = sizeof(struct mt7921_sta); 88 hw->vif_data_size = sizeof(struct mt7921_vif); 89 90 wiphy->iface_combinations = if_comb; 91 wiphy->n_iface_combinations = ARRAY_SIZE(if_comb); 92 wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN; 93 wiphy->max_scan_ssids = 4; 94 wiphy->max_sched_scan_plan_interval = 95 MT76_CONNAC_MAX_SCHED_SCAN_INTERVAL; 96 wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN; 97 wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID; 98 wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH; 99 wiphy->max_sched_scan_reqs = 1; 100 wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH; 101 wiphy->reg_notifier = mt7921_regd_notifier; 102 103 wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR; 104 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL); 105 106 ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS); 107 ieee80211_hw_set(hw, HAS_RATE_CONTROL); 108 ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD); 109 ieee80211_hw_set(hw, WANT_MONITOR_VIF); 110 ieee80211_hw_set(hw, SUPPORTS_PS); 111 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS); 112 113 hw->max_tx_fragments = 4; 114 } 115 116 static void 117 mt7921_mac_init_band(struct mt7921_dev *dev, u8 band) 118 { 119 u32 mask, set; 120 121 mt76_rmw_field(dev, MT_TMAC_CTCR0(band), 122 MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f); 123 mt76_set(dev, MT_TMAC_CTCR0(band), 124 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN | 125 MT_TMAC_CTCR0_INS_DDLMT_EN); 126 127 mask = MT_MDP_RCFR0_MCU_RX_MGMT | 128 MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR | 129 MT_MDP_RCFR0_MCU_RX_CTL_BAR; 130 set = FIELD_PREP(MT_MDP_RCFR0_MCU_RX_MGMT, MT_MDP_TO_HIF) | 131 FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_NON_BAR, MT_MDP_TO_HIF) | 132 FIELD_PREP(MT_MDP_RCFR0_MCU_RX_CTL_BAR, MT_MDP_TO_HIF); 133 mt76_rmw(dev, MT_MDP_BNRCFR0(band), mask, set); 134 135 mask = MT_MDP_RCFR1_MCU_RX_BYPASS | 136 MT_MDP_RCFR1_RX_DROPPED_UCAST | 137 MT_MDP_RCFR1_RX_DROPPED_MCAST; 138 set = FIELD_PREP(MT_MDP_RCFR1_MCU_RX_BYPASS, MT_MDP_TO_HIF) | 139 FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_UCAST, MT_MDP_TO_HIF) | 140 FIELD_PREP(MT_MDP_RCFR1_RX_DROPPED_MCAST, MT_MDP_TO_HIF); 141 mt76_rmw(dev, MT_MDP_BNRCFR1(band), mask, set); 142 143 mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN); 144 mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN); 145 146 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536); 147 /* disable rx rate report by default due to hw issues */ 148 mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN); 149 } 150 151 void mt7921_mac_init(struct mt7921_dev *dev) 152 { 153 int i; 154 155 mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); 156 /* disable hardware de-agg */ 157 mt76_clear(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); 158 mt76_clear(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN); 159 160 for (i = 0; i < MT7921_WTBL_SIZE; i++) 161 mt7921_mac_wtbl_update(dev, i, 162 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 163 for (i = 0; i < 2; i++) 164 mt7921_mac_init_band(dev, i); 165 166 mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0); 167 } 168 169 static int mt7921_init_hardware(struct mt7921_dev *dev) 170 { 171 int ret, idx; 172 173 ret = mt7921_dma_init(dev); 174 if (ret) 175 return ret; 176 177 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 178 179 /* force firmware operation mode into normal state, 180 * which should be set before firmware download stage. 181 */ 182 mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE); 183 184 ret = mt7921_mcu_init(dev); 185 if (ret) 186 return ret; 187 188 ret = mt7921_eeprom_init(dev); 189 if (ret < 0) 190 return ret; 191 192 ret = mt7921_mcu_set_eeprom(dev); 193 if (ret) 194 return ret; 195 196 /* Beacon and mgmt frames should occupy wcid 0 */ 197 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1); 198 if (idx) 199 return -ENOSPC; 200 201 dev->mt76.global_wcid.idx = idx; 202 dev->mt76.global_wcid.hw_key_idx = -1; 203 dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET; 204 rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid); 205 206 mt7921_mac_init(dev); 207 208 return 0; 209 } 210 211 int mt7921_register_device(struct mt7921_dev *dev) 212 { 213 struct ieee80211_hw *hw = mt76_hw(dev); 214 int ret; 215 216 dev->phy.dev = dev; 217 dev->phy.mt76 = &dev->mt76.phy; 218 dev->mt76.phy.priv = &dev->phy; 219 dev->mt76.tx_worker.fn = mt7921_tx_worker; 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 spin_lock_init(&dev->pm.wake.lock); 224 mutex_init(&dev->pm.mutex); 225 init_waitqueue_head(&dev->pm.wait); 226 spin_lock_init(&dev->pm.txq_lock); 227 set_bit(MT76_STATE_PM, &dev->mphy.state); 228 INIT_LIST_HEAD(&dev->phy.stats_list); 229 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work); 230 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work); 231 INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work); 232 skb_queue_head_init(&dev->phy.scan_event_list); 233 skb_queue_head_init(&dev->coredump.msg_list); 234 INIT_LIST_HEAD(&dev->sta_poll_list); 235 spin_lock_init(&dev->sta_poll_lock); 236 237 INIT_WORK(&dev->reset_work, mt7921_mac_reset_work); 238 239 dev->pm.idle_timeout = MT7921_PM_TIMEOUT; 240 dev->pm.stats.last_wake_event = jiffies; 241 dev->pm.stats.last_doze_event = jiffies; 242 243 ret = mt7921_init_hardware(dev); 244 if (ret) 245 return ret; 246 247 mt7921_init_wiphy(hw); 248 dev->mphy.sband_2g.sband.ht_cap.cap |= 249 IEEE80211_HT_CAP_LDPC_CODING | 250 IEEE80211_HT_CAP_MAX_AMSDU; 251 dev->mphy.sband_5g.sband.ht_cap.cap |= 252 IEEE80211_HT_CAP_LDPC_CODING | 253 IEEE80211_HT_CAP_MAX_AMSDU; 254 dev->mphy.sband_5g.sband.vht_cap.cap |= 255 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 | 256 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK; 257 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; 258 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; 259 260 mt76_set_stream_caps(&dev->mphy, true); 261 mt7921_set_stream_he_caps(&dev->phy); 262 263 ret = mt76_register_device(&dev->mt76, true, mt7921_rates, 264 ARRAY_SIZE(mt7921_rates)); 265 if (ret) 266 return ret; 267 268 return mt7921_init_debugfs(dev); 269 } 270 271 void mt7921_unregister_device(struct mt7921_dev *dev) 272 { 273 mt76_unregister_device(&dev->mt76); 274 mt7921_tx_token_put(dev); 275 mt7921_dma_cleanup(dev); 276 mt7921_mcu_exit(dev); 277 278 tasklet_disable(&dev->irq_tasklet); 279 mt76_free_device(&dev->mt76); 280 } 281