1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2020 MediaTek Inc. */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/firmware.h> 6 #include "mt7921.h" 7 #include "mac.h" 8 #include "mcu.h" 9 #include "eeprom.h" 10 11 static const struct ieee80211_iface_limit if_limits[] = { 12 { 13 .max = MT7921_MAX_INTERFACES, 14 .types = BIT(NL80211_IFTYPE_STATION) 15 }, 16 { 17 .max = 1, 18 .types = BIT(NL80211_IFTYPE_AP) 19 } 20 }; 21 22 static const struct ieee80211_iface_combination if_comb[] = { 23 { 24 .limits = if_limits, 25 .n_limits = ARRAY_SIZE(if_limits), 26 .max_interfaces = MT7921_MAX_INTERFACES, 27 .num_different_channels = 1, 28 .beacon_int_infra_match = true, 29 }, 30 }; 31 32 static const struct ieee80211_iface_limit if_limits_chanctx[] = { 33 { 34 .max = 2, 35 .types = BIT(NL80211_IFTYPE_STATION), 36 }, 37 { 38 .max = 1, 39 .types = BIT(NL80211_IFTYPE_AP), 40 } 41 }; 42 43 static const struct ieee80211_iface_combination if_comb_chanctx[] = { 44 { 45 .limits = if_limits_chanctx, 46 .n_limits = ARRAY_SIZE(if_limits_chanctx), 47 .max_interfaces = 2, 48 .num_different_channels = 2, 49 .beacon_int_infra_match = false, 50 } 51 }; 52 53 static void 54 mt7921_regd_notifier(struct wiphy *wiphy, 55 struct regulatory_request *request) 56 { 57 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy); 58 struct mt7921_dev *dev = mt7921_hw_dev(hw); 59 60 memcpy(dev->mt76.alpha2, request->alpha2, sizeof(dev->mt76.alpha2)); 61 dev->mt76.region = request->dfs_region; 62 dev->country_ie_env = request->country_ie_env; 63 64 mt7921_mutex_acquire(dev); 65 mt7921_mcu_set_clc(dev, request->alpha2, request->country_ie_env); 66 mt76_connac_mcu_set_channel_domain(hw->priv); 67 mt7921_set_tx_sar_pwr(hw, NULL); 68 mt7921_mutex_release(dev); 69 } 70 71 static int 72 mt7921_init_wiphy(struct ieee80211_hw *hw) 73 { 74 struct mt7921_phy *phy = mt7921_hw_phy(hw); 75 struct mt7921_dev *dev = phy->dev; 76 struct wiphy *wiphy = hw->wiphy; 77 78 hw->queues = 4; 79 hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE; 80 hw->max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE; 81 hw->netdev_features = NETIF_F_RXCSUM; 82 83 hw->radiotap_timestamp.units_pos = 84 IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US; 85 86 phy->slottime = 9; 87 88 hw->sta_data_size = sizeof(struct mt7921_sta); 89 hw->vif_data_size = sizeof(struct mt7921_vif); 90 91 if (dev->fw_features & MT7921_FW_CAP_CNM) { 92 wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL; 93 wiphy->iface_combinations = if_comb_chanctx; 94 wiphy->n_iface_combinations = ARRAY_SIZE(if_comb_chanctx); 95 } else { 96 wiphy->flags &= ~WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL; 97 wiphy->iface_combinations = if_comb; 98 wiphy->n_iface_combinations = ARRAY_SIZE(if_comb); 99 } 100 wiphy->flags &= ~(WIPHY_FLAG_IBSS_RSN | WIPHY_FLAG_4ADDR_AP | 101 WIPHY_FLAG_4ADDR_STATION); 102 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | 103 BIT(NL80211_IFTYPE_AP); 104 wiphy->max_remain_on_channel_duration = 5000; 105 wiphy->max_scan_ie_len = MT76_CONNAC_SCAN_IE_LEN; 106 wiphy->max_scan_ssids = 4; 107 wiphy->max_sched_scan_plan_interval = 108 MT76_CONNAC_MAX_TIME_SCHED_SCAN_INTERVAL; 109 wiphy->max_sched_scan_ie_len = IEEE80211_MAX_DATA_LEN; 110 wiphy->max_sched_scan_ssids = MT76_CONNAC_MAX_SCHED_SCAN_SSID; 111 wiphy->max_match_sets = MT76_CONNAC_MAX_SCAN_MATCH; 112 wiphy->max_sched_scan_reqs = 1; 113 wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH; 114 wiphy->reg_notifier = mt7921_regd_notifier; 115 116 wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR | 117 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR; 118 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL); 119 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_LEGACY); 120 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HT); 121 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_VHT); 122 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_HE); 123 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT); 124 125 ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS); 126 ieee80211_hw_set(hw, HAS_RATE_CONTROL); 127 ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD); 128 ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD); 129 ieee80211_hw_set(hw, WANT_MONITOR_VIF); 130 ieee80211_hw_set(hw, SUPPORTS_PS); 131 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS); 132 ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW); 133 ieee80211_hw_set(hw, CONNECTION_MONITOR); 134 135 if (dev->pm.enable) 136 ieee80211_hw_set(hw, CONNECTION_MONITOR); 137 138 hw->max_tx_fragments = 4; 139 140 return 0; 141 } 142 143 static void 144 mt7921_mac_init_band(struct mt7921_dev *dev, u8 band) 145 { 146 u32 mask, set; 147 148 mt76_rmw_field(dev, MT_TMAC_CTCR0(band), 149 MT_TMAC_CTCR0_INS_DDLMT_REFTIME, 0x3f); 150 mt76_set(dev, MT_TMAC_CTCR0(band), 151 MT_TMAC_CTCR0_INS_DDLMT_VHT_SMPDU_EN | 152 MT_TMAC_CTCR0_INS_DDLMT_EN); 153 154 mt76_set(dev, MT_WF_RMAC_MIB_TIME0(band), MT_WF_RMAC_MIB_RXTIME_EN); 155 mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(band), MT_WF_RMAC_MIB_RXTIME_EN); 156 157 /* enable MIB tx-rx time reporting */ 158 mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_TXDUR_EN); 159 mt76_set(dev, MT_MIB_SCR1(band), MT_MIB_RXDUR_EN); 160 161 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_MAX_RX_LEN, 1536); 162 /* disable rx rate report by default due to hw issues */ 163 mt76_clear(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN); 164 165 /* filter out non-resp frames and get instantaneous signal reporting */ 166 mask = MT_WTBLOFF_TOP_RSCR_RCPI_MODE | MT_WTBLOFF_TOP_RSCR_RCPI_PARAM; 167 set = FIELD_PREP(MT_WTBLOFF_TOP_RSCR_RCPI_MODE, 0) | 168 FIELD_PREP(MT_WTBLOFF_TOP_RSCR_RCPI_PARAM, 0x3); 169 mt76_rmw(dev, MT_WTBLOFF_TOP_RSCR(band), mask, set); 170 } 171 172 u8 mt7921_check_offload_capability(struct device *dev, const char *fw_wm) 173 { 174 struct mt7921_fw_features *features = NULL; 175 const struct mt76_connac2_fw_trailer *hdr; 176 struct mt7921_realease_info *rel_info; 177 const struct firmware *fw; 178 int ret, i, offset = 0; 179 const u8 *data, *end; 180 181 ret = request_firmware(&fw, fw_wm, dev); 182 if (ret) 183 return ret; 184 185 if (!fw || !fw->data || fw->size < sizeof(*hdr)) { 186 dev_err(dev, "Invalid firmware\n"); 187 goto out; 188 } 189 190 data = fw->data; 191 hdr = (const void *)(fw->data + fw->size - sizeof(*hdr)); 192 193 for (i = 0; i < hdr->n_region; i++) { 194 const struct mt76_connac2_fw_region *region; 195 196 region = (const void *)((const u8 *)hdr - 197 (hdr->n_region - i) * sizeof(*region)); 198 offset += le32_to_cpu(region->len); 199 } 200 201 data += offset + 16; 202 rel_info = (struct mt7921_realease_info *)data; 203 data += sizeof(*rel_info); 204 end = data + le16_to_cpu(rel_info->len); 205 206 while (data < end) { 207 rel_info = (struct mt7921_realease_info *)data; 208 data += sizeof(*rel_info); 209 210 if (rel_info->tag == MT7921_FW_TAG_FEATURE) { 211 features = (struct mt7921_fw_features *)data; 212 break; 213 } 214 215 data += le16_to_cpu(rel_info->len) + rel_info->pad_len; 216 } 217 218 out: 219 release_firmware(fw); 220 221 return features ? features->data : 0; 222 } 223 EXPORT_SYMBOL_GPL(mt7921_check_offload_capability); 224 225 int mt7921_mac_init(struct mt7921_dev *dev) 226 { 227 int i; 228 229 mt76_rmw_field(dev, MT_MDP_DCR1, MT_MDP_DCR1_MAX_RX_LEN, 1536); 230 /* enable hardware de-agg */ 231 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_DAMSDU_EN); 232 /* enable hardware rx header translation */ 233 mt76_set(dev, MT_MDP_DCR0, MT_MDP_DCR0_RX_HDR_TRANS_EN); 234 235 for (i = 0; i < MT7921_WTBL_SIZE; i++) 236 mt7921_mac_wtbl_update(dev, i, 237 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 238 for (i = 0; i < 2; i++) 239 mt7921_mac_init_band(dev, i); 240 241 return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0); 242 } 243 EXPORT_SYMBOL_GPL(mt7921_mac_init); 244 245 static int __mt7921_init_hardware(struct mt7921_dev *dev) 246 { 247 int ret; 248 249 /* force firmware operation mode into normal state, 250 * which should be set before firmware download stage. 251 */ 252 mt76_wr(dev, MT_SWDEF_MODE, MT_SWDEF_NORMAL_MODE); 253 ret = mt7921_mcu_init(dev); 254 if (ret) 255 goto out; 256 257 mt76_eeprom_override(&dev->mphy); 258 259 ret = mt7921_mcu_set_eeprom(dev); 260 if (ret) 261 goto out; 262 263 ret = mt7921_mac_init(dev); 264 out: 265 return ret; 266 } 267 268 static int mt7921_init_hardware(struct mt7921_dev *dev) 269 { 270 int ret, i; 271 272 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 273 274 for (i = 0; i < MT7921_MCU_INIT_RETRY_COUNT; i++) { 275 ret = __mt7921_init_hardware(dev); 276 if (!ret) 277 break; 278 279 mt7921_init_reset(dev); 280 } 281 282 if (i == MT7921_MCU_INIT_RETRY_COUNT) { 283 dev_err(dev->mt76.dev, "hardware init failed\n"); 284 return ret; 285 } 286 287 return 0; 288 } 289 290 static int mt7921_init_wcid(struct mt7921_dev *dev) 291 { 292 int idx; 293 294 /* Beacon and mgmt frames should occupy wcid 0 */ 295 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1); 296 if (idx) 297 return -ENOSPC; 298 299 dev->mt76.global_wcid.idx = idx; 300 dev->mt76.global_wcid.hw_key_idx = -1; 301 dev->mt76.global_wcid.tx_info |= MT_WCID_TX_INFO_SET; 302 rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid); 303 304 return 0; 305 } 306 307 static void mt7921_init_work(struct work_struct *work) 308 { 309 struct mt7921_dev *dev = container_of(work, struct mt7921_dev, 310 init_work); 311 int ret; 312 313 ret = mt7921_init_hardware(dev); 314 if (ret) 315 return; 316 317 mt76_set_stream_caps(&dev->mphy, true); 318 mt7921_set_stream_he_caps(&dev->phy); 319 320 ret = mt76_register_device(&dev->mt76, true, mt76_rates, 321 ARRAY_SIZE(mt76_rates)); 322 if (ret) { 323 dev_err(dev->mt76.dev, "register device failed\n"); 324 return; 325 } 326 327 ret = mt7921_init_debugfs(dev); 328 if (ret) { 329 dev_err(dev->mt76.dev, "register debugfs failed\n"); 330 return; 331 } 332 333 /* we support chip reset now */ 334 dev->hw_init_done = true; 335 336 mt76_connac_mcu_set_deep_sleep(&dev->mt76, dev->pm.ds_enable); 337 } 338 339 int mt7921_register_device(struct mt7921_dev *dev) 340 { 341 struct ieee80211_hw *hw = mt76_hw(dev); 342 int ret; 343 344 dev->phy.dev = dev; 345 dev->phy.mt76 = &dev->mt76.phy; 346 dev->mt76.phy.priv = &dev->phy; 347 dev->mt76.tx_worker.fn = mt7921_tx_worker; 348 349 INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work); 350 INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work); 351 spin_lock_init(&dev->pm.wake.lock); 352 mutex_init(&dev->pm.mutex); 353 init_waitqueue_head(&dev->pm.wait); 354 if (mt76_is_sdio(&dev->mt76)) 355 init_waitqueue_head(&dev->mt76.sdio.wait); 356 spin_lock_init(&dev->pm.txq_lock); 357 INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work); 358 INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work); 359 INIT_DELAYED_WORK(&dev->coredump.work, mt7921_coredump_work); 360 #if IS_ENABLED(CONFIG_IPV6) 361 INIT_WORK(&dev->ipv6_ns_work, mt7921_set_ipv6_ns_work); 362 skb_queue_head_init(&dev->ipv6_ns_list); 363 #endif 364 skb_queue_head_init(&dev->phy.scan_event_list); 365 skb_queue_head_init(&dev->coredump.msg_list); 366 INIT_LIST_HEAD(&dev->sta_poll_list); 367 spin_lock_init(&dev->sta_poll_lock); 368 369 INIT_WORK(&dev->reset_work, mt7921_mac_reset_work); 370 INIT_WORK(&dev->init_work, mt7921_init_work); 371 372 INIT_WORK(&dev->phy.roc_work, mt7921_roc_work); 373 timer_setup(&dev->phy.roc_timer, mt7921_roc_timer, 0); 374 init_waitqueue_head(&dev->phy.roc_wait); 375 376 dev->pm.idle_timeout = MT7921_PM_TIMEOUT; 377 dev->pm.stats.last_wake_event = jiffies; 378 dev->pm.stats.last_doze_event = jiffies; 379 if (!mt76_is_usb(&dev->mt76)) { 380 dev->pm.enable_user = true; 381 dev->pm.enable = true; 382 dev->pm.ds_enable_user = true; 383 dev->pm.ds_enable = true; 384 } 385 386 if (!mt76_is_mmio(&dev->mt76)) 387 hw->extra_tx_headroom += MT_SDIO_TXD_SIZE + MT_SDIO_HDR_SIZE; 388 389 mt7921_init_acpi_sar(dev); 390 391 ret = mt7921_init_wcid(dev); 392 if (ret) 393 return ret; 394 395 ret = mt7921_init_wiphy(hw); 396 if (ret) 397 return ret; 398 399 dev->mphy.sband_2g.sband.ht_cap.cap |= 400 IEEE80211_HT_CAP_LDPC_CODING | 401 IEEE80211_HT_CAP_MAX_AMSDU; 402 dev->mphy.sband_5g.sband.ht_cap.cap |= 403 IEEE80211_HT_CAP_LDPC_CODING | 404 IEEE80211_HT_CAP_MAX_AMSDU; 405 dev->mphy.sband_5g.sband.vht_cap.cap |= 406 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 | 407 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK | 408 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE | 409 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE | 410 (3 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT); 411 if (is_mt7922(&dev->mt76)) 412 dev->mphy.sband_5g.sband.vht_cap.cap |= 413 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ | 414 IEEE80211_VHT_CAP_SHORT_GI_160; 415 416 dev->mphy.hw->wiphy->available_antennas_rx = dev->mphy.chainmask; 417 dev->mphy.hw->wiphy->available_antennas_tx = dev->mphy.chainmask; 418 419 queue_work(system_wq, &dev->init_work); 420 421 return 0; 422 } 423 EXPORT_SYMBOL_GPL(mt7921_register_device); 424