1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2023 MediaTek Inc. */ 3 4 #include <linux/etherdevice.h> 5 #include <linux/platform_device.h> 6 #include <linux/pci.h> 7 #include <linux/module.h> 8 #include <linux/ctype.h> 9 #include <net/ipv6.h> 10 #include "mt7925.h" 11 #include "mcu.h" 12 #include "mac.h" 13 14 static void 15 mt7925_init_he_caps(struct mt792x_phy *phy, enum nl80211_band band, 16 struct ieee80211_sband_iftype_data *data, 17 enum nl80211_iftype iftype) 18 { 19 struct ieee80211_sta_he_cap *he_cap = &data->he_cap; 20 struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem; 21 struct ieee80211_he_mcs_nss_supp *he_mcs = &he_cap->he_mcs_nss_supp; 22 int i, nss = hweight8(phy->mt76->antenna_mask); 23 u16 mcs_map = 0; 24 25 for (i = 0; i < 8; i++) { 26 if (i < nss) 27 mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2)); 28 else 29 mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2)); 30 } 31 32 he_cap->has_he = true; 33 34 he_cap_elem->mac_cap_info[0] = IEEE80211_HE_MAC_CAP0_HTC_HE; 35 he_cap_elem->mac_cap_info[3] = IEEE80211_HE_MAC_CAP3_OMI_CONTROL | 36 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3; 37 he_cap_elem->mac_cap_info[4] = IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU; 38 39 if (band == NL80211_BAND_2GHZ) 40 he_cap_elem->phy_cap_info[0] = 41 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G; 42 else 43 he_cap_elem->phy_cap_info[0] = 44 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G | 45 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 46 47 he_cap_elem->phy_cap_info[1] = 48 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD; 49 he_cap_elem->phy_cap_info[2] = 50 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US | 51 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ | 52 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ | 53 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO | 54 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO; 55 56 switch (iftype) { 57 case NL80211_IFTYPE_AP: 58 he_cap_elem->mac_cap_info[2] |= 59 IEEE80211_HE_MAC_CAP2_BSR; 60 he_cap_elem->mac_cap_info[4] |= 61 IEEE80211_HE_MAC_CAP4_BQR; 62 he_cap_elem->mac_cap_info[5] |= 63 IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX; 64 he_cap_elem->phy_cap_info[3] |= 65 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK | 66 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK; 67 he_cap_elem->phy_cap_info[6] |= 68 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | 69 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT; 70 he_cap_elem->phy_cap_info[9] |= 71 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU | 72 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU; 73 break; 74 case NL80211_IFTYPE_STATION: 75 he_cap_elem->mac_cap_info[1] |= 76 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US; 77 78 if (band == NL80211_BAND_2GHZ) 79 he_cap_elem->phy_cap_info[0] |= 80 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G; 81 else 82 he_cap_elem->phy_cap_info[0] |= 83 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G; 84 85 he_cap_elem->phy_cap_info[1] |= 86 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A | 87 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US; 88 he_cap_elem->phy_cap_info[3] |= 89 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK | 90 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK; 91 he_cap_elem->phy_cap_info[4] |= 92 IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE | 93 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4 | 94 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4; 95 he_cap_elem->phy_cap_info[5] |= 96 IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK | 97 IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK; 98 he_cap_elem->phy_cap_info[6] |= 99 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU | 100 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU | 101 IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB | 102 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | 103 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT; 104 he_cap_elem->phy_cap_info[7] |= 105 IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP | 106 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI; 107 he_cap_elem->phy_cap_info[8] |= 108 IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G | 109 IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU | 110 IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU | 111 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484; 112 he_cap_elem->phy_cap_info[9] |= 113 IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM | 114 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK | 115 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU | 116 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU | 117 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB | 118 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB; 119 break; 120 default: 121 break; 122 } 123 124 he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map); 125 he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map); 126 he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map); 127 he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map); 128 129 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres)); 130 131 if (he_cap_elem->phy_cap_info[6] & 132 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) { 133 mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss); 134 } else { 135 he_cap_elem->phy_cap_info[9] |= 136 u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US, 137 IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK); 138 } 139 140 if (band == NL80211_BAND_6GHZ) { 141 u16 cap = IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS | 142 IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS; 143 144 cap |= u16_encode_bits(IEEE80211_HT_MPDU_DENSITY_0_5, 145 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START) | 146 u16_encode_bits(IEEE80211_VHT_MAX_AMPDU_1024K, 147 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) | 148 u16_encode_bits(IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454, 149 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN); 150 151 data->he_6ghz_capa.capa = cpu_to_le16(cap); 152 } 153 } 154 155 static void 156 mt7925_init_eht_caps(struct mt792x_phy *phy, enum nl80211_band band, 157 struct ieee80211_sband_iftype_data *data) 158 { 159 struct ieee80211_sta_eht_cap *eht_cap = &data->eht_cap; 160 struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem; 161 struct ieee80211_eht_mcs_nss_supp *eht_nss = &eht_cap->eht_mcs_nss_supp; 162 enum nl80211_chan_width width = phy->mt76->chandef.width; 163 int nss = hweight8(phy->mt76->antenna_mask); 164 int sts = hweight16(phy->mt76->chainmask); 165 u8 val; 166 167 if (!phy->dev->has_eht) 168 return; 169 170 eht_cap->has_eht = true; 171 172 eht_cap_elem->mac_cap_info[0] = 173 IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS | 174 IEEE80211_EHT_MAC_CAP0_OM_CONTROL; 175 176 eht_cap_elem->phy_cap_info[0] = 177 IEEE80211_EHT_PHY_CAP0_NDP_4_EHT_LFT_32_GI | 178 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMER | 179 IEEE80211_EHT_PHY_CAP0_SU_BEAMFORMEE; 180 181 eht_cap_elem->phy_cap_info[0] |= 182 u8_encode_bits(u8_get_bits(sts - 1, BIT(0)), 183 IEEE80211_EHT_PHY_CAP0_BEAMFORMEE_SS_80MHZ_MASK); 184 185 eht_cap_elem->phy_cap_info[1] = 186 u8_encode_bits(u8_get_bits(sts - 1, GENMASK(2, 1)), 187 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_80MHZ_MASK) | 188 u8_encode_bits(sts - 1, 189 IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK); 190 191 eht_cap_elem->phy_cap_info[2] = 192 u8_encode_bits(sts - 1, IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_80MHZ_MASK) | 193 u8_encode_bits(sts - 1, IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK); 194 195 eht_cap_elem->phy_cap_info[3] = 196 IEEE80211_EHT_PHY_CAP3_NG_16_SU_FEEDBACK | 197 IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK | 198 IEEE80211_EHT_PHY_CAP3_CODEBOOK_4_2_SU_FDBK | 199 IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK | 200 IEEE80211_EHT_PHY_CAP3_TRIG_SU_BF_FDBK | 201 IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK | 202 IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK; 203 204 eht_cap_elem->phy_cap_info[4] = 205 u8_encode_bits(min_t(int, sts - 1, 2), 206 IEEE80211_EHT_PHY_CAP4_MAX_NC_MASK); 207 208 eht_cap_elem->phy_cap_info[5] = 209 IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK | 210 u8_encode_bits(IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US, 211 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK) | 212 u8_encode_bits(u8_get_bits(0x11, GENMASK(1, 0)), 213 IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK); 214 215 val = width == NL80211_CHAN_WIDTH_160 ? 0x7 : 216 width == NL80211_CHAN_WIDTH_80 ? 0x3 : 0x1; 217 eht_cap_elem->phy_cap_info[6] = 218 u8_encode_bits(u8_get_bits(0x11, GENMASK(4, 2)), 219 IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK) | 220 u8_encode_bits(val, IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK); 221 222 eht_cap_elem->phy_cap_info[7] = 223 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ | 224 IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ | 225 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ | 226 IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ; 227 228 val = u8_encode_bits(nss, IEEE80211_EHT_MCS_NSS_RX) | 229 u8_encode_bits(nss, IEEE80211_EHT_MCS_NSS_TX); 230 231 eht_nss->bw._80.rx_tx_mcs9_max_nss = val; 232 eht_nss->bw._80.rx_tx_mcs11_max_nss = val; 233 eht_nss->bw._80.rx_tx_mcs13_max_nss = val; 234 eht_nss->bw._160.rx_tx_mcs9_max_nss = val; 235 eht_nss->bw._160.rx_tx_mcs11_max_nss = val; 236 eht_nss->bw._160.rx_tx_mcs13_max_nss = val; 237 } 238 239 int mt7925_init_mlo_caps(struct mt792x_phy *phy) 240 { 241 struct wiphy *wiphy = phy->mt76->hw->wiphy; 242 static const u8 ext_capa_sta[] = { 243 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF, 244 }; 245 static struct wiphy_iftype_ext_capab ext_capab[] = { 246 { 247 .iftype = NL80211_IFTYPE_STATION, 248 .extended_capabilities = ext_capa_sta, 249 .extended_capabilities_mask = ext_capa_sta, 250 .extended_capabilities_len = sizeof(ext_capa_sta), 251 }, 252 }; 253 254 if (!(phy->chip_cap & MT792x_CHIP_CAP_MLO_EVT_EN)) 255 return 0; 256 257 ext_capab[0].eml_capabilities = phy->eml_cap; 258 ext_capab[0].mld_capa_and_ops = 259 u16_encode_bits(1, IEEE80211_MLD_CAP_OP_MAX_SIMUL_LINKS); 260 261 wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO; 262 wiphy->iftype_ext_capab = ext_capab; 263 wiphy->num_iftype_ext_capab = ARRAY_SIZE(ext_capab); 264 265 return 0; 266 } 267 268 static void 269 __mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy, 270 struct ieee80211_supported_band *sband, 271 enum nl80211_band band) 272 { 273 struct ieee80211_sband_iftype_data *data = phy->iftype[band]; 274 int i, n = 0; 275 276 for (i = 0; i < NUM_NL80211_IFTYPES; i++) { 277 switch (i) { 278 case NL80211_IFTYPE_STATION: 279 case NL80211_IFTYPE_AP: 280 break; 281 default: 282 continue; 283 } 284 285 data[n].types_mask = BIT(i); 286 mt7925_init_he_caps(phy, band, &data[n], i); 287 mt7925_init_eht_caps(phy, band, &data[n]); 288 289 n++; 290 } 291 292 _ieee80211_set_sband_iftype_data(sband, data, n); 293 } 294 295 void mt7925_set_stream_he_eht_caps(struct mt792x_phy *phy) 296 { 297 if (phy->mt76->cap.has_2ghz) 298 __mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_2g.sband, 299 NL80211_BAND_2GHZ); 300 301 if (phy->mt76->cap.has_5ghz) 302 __mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_5g.sband, 303 NL80211_BAND_5GHZ); 304 305 if (phy->mt76->cap.has_6ghz) 306 __mt7925_set_stream_he_eht_caps(phy, &phy->mt76->sband_6g.sband, 307 NL80211_BAND_6GHZ); 308 } 309 310 int __mt7925_start(struct mt792x_phy *phy) 311 { 312 struct mt76_phy *mphy = phy->mt76; 313 int err; 314 315 err = mt7925_mcu_set_channel_domain(mphy); 316 if (err) 317 return err; 318 319 err = mt7925_mcu_set_rts_thresh(phy, 0x92b); 320 if (err) 321 return err; 322 323 err = mt7925_set_tx_sar_pwr(mphy->hw, NULL); 324 if (err) 325 return err; 326 327 mt792x_mac_reset_counters(phy); 328 set_bit(MT76_STATE_RUNNING, &mphy->state); 329 330 ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work, 331 MT792x_WATCHDOG_TIME); 332 333 return 0; 334 } 335 EXPORT_SYMBOL_GPL(__mt7925_start); 336 337 static int mt7925_start(struct ieee80211_hw *hw) 338 { 339 struct mt792x_phy *phy = mt792x_hw_phy(hw); 340 int err; 341 342 mt792x_mutex_acquire(phy->dev); 343 err = __mt7925_start(phy); 344 mt792x_mutex_release(phy->dev); 345 346 return err; 347 } 348 349 static int mt7925_mac_link_bss_add(struct mt792x_dev *dev, 350 struct ieee80211_bss_conf *link_conf, 351 struct mt792x_link_sta *mlink) 352 { 353 struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf); 354 struct ieee80211_vif *vif = link_conf->vif; 355 struct mt792x_vif *mvif = mconf->vif; 356 struct mt76_txq *mtxq; 357 int idx, ret = 0; 358 359 mconf->mt76.idx = __ffs64(~dev->mt76.vif_mask); 360 if (mconf->mt76.idx >= MT792x_MAX_INTERFACES) { 361 ret = -ENOSPC; 362 goto out; 363 } 364 365 mconf->mt76.omac_idx = ieee80211_vif_is_mld(vif) ? 366 0 : mconf->mt76.idx; 367 mconf->mt76.band_idx = 0xff; 368 mconf->mt76.wmm_idx = ieee80211_vif_is_mld(vif) ? 369 0 : mconf->mt76.idx % MT76_CONNAC_MAX_WMM_SETS; 370 371 if (mvif->phy->mt76->chandef.chan->band != NL80211_BAND_2GHZ) 372 mconf->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL + 4; 373 else 374 mconf->mt76.basic_rates_idx = MT792x_BASIC_RATES_TBL; 375 376 dev->mt76.vif_mask |= BIT_ULL(mconf->mt76.idx); 377 mvif->phy->omac_mask |= BIT_ULL(mconf->mt76.omac_idx); 378 379 idx = MT792x_WTBL_RESERVED - mconf->mt76.idx; 380 381 INIT_LIST_HEAD(&mlink->wcid.poll_list); 382 mlink->wcid.idx = idx; 383 mlink->wcid.phy_idx = 0; 384 mlink->wcid.hw_key_idx = -1; 385 mlink->wcid.tx_info |= MT_WCID_TX_INFO_SET; 386 mt76_wcid_init(&mlink->wcid); 387 388 mt7925_mac_wtbl_update(dev, idx, 389 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 390 391 ewma_rssi_init(&mconf->rssi); 392 393 rcu_assign_pointer(dev->mt76.wcid[idx], &mlink->wcid); 394 395 ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, link_conf, &mconf->mt76, 396 &mlink->wcid, true); 397 if (ret) 398 goto out; 399 400 if (vif->txq) { 401 mtxq = (struct mt76_txq *)vif->txq->drv_priv; 402 mtxq->wcid = idx; 403 } 404 405 out: 406 return ret; 407 } 408 409 static int 410 mt7925_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 411 { 412 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 413 struct mt792x_dev *dev = mt792x_hw_dev(hw); 414 struct mt792x_phy *phy = mt792x_hw_phy(hw); 415 int ret = 0; 416 417 mt792x_mutex_acquire(dev); 418 419 mvif->phy = phy; 420 mvif->bss_conf.vif = mvif; 421 mvif->sta.vif = mvif; 422 mvif->deflink_id = IEEE80211_LINK_UNSPECIFIED; 423 424 ret = mt7925_mac_link_bss_add(dev, &vif->bss_conf, &mvif->sta.deflink); 425 if (ret < 0) 426 goto out; 427 428 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER; 429 out: 430 mt792x_mutex_release(dev); 431 432 return ret; 433 } 434 435 static void mt7925_roc_iter(void *priv, u8 *mac, 436 struct ieee80211_vif *vif) 437 { 438 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 439 struct mt792x_phy *phy = priv; 440 441 mt7925_mcu_abort_roc(phy, &mvif->bss_conf, phy->roc_token_id); 442 } 443 444 void mt7925_roc_abort_sync(struct mt792x_dev *dev) 445 { 446 struct mt792x_phy *phy = &dev->phy; 447 448 del_timer_sync(&phy->roc_timer); 449 cancel_work_sync(&phy->roc_work); 450 if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 451 ieee80211_iterate_interfaces(mt76_hw(dev), 452 IEEE80211_IFACE_ITER_RESUME_ALL, 453 mt7925_roc_iter, (void *)phy); 454 } 455 EXPORT_SYMBOL_GPL(mt7925_roc_abort_sync); 456 457 void mt7925_roc_work(struct work_struct *work) 458 { 459 struct mt792x_phy *phy; 460 461 phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy, 462 roc_work); 463 464 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 465 return; 466 467 mt792x_mutex_acquire(phy->dev); 468 ieee80211_iterate_active_interfaces(phy->mt76->hw, 469 IEEE80211_IFACE_ITER_RESUME_ALL, 470 mt7925_roc_iter, phy); 471 mt792x_mutex_release(phy->dev); 472 ieee80211_remain_on_channel_expired(phy->mt76->hw); 473 } 474 475 static int mt7925_abort_roc(struct mt792x_phy *phy, 476 struct mt792x_bss_conf *mconf) 477 { 478 int err = 0; 479 480 del_timer_sync(&phy->roc_timer); 481 cancel_work_sync(&phy->roc_work); 482 483 mt792x_mutex_acquire(phy->dev); 484 if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 485 err = mt7925_mcu_abort_roc(phy, mconf, phy->roc_token_id); 486 mt792x_mutex_release(phy->dev); 487 488 return err; 489 } 490 491 static int mt7925_set_roc(struct mt792x_phy *phy, 492 struct mt792x_bss_conf *mconf, 493 struct ieee80211_channel *chan, 494 int duration, 495 enum mt7925_roc_req type) 496 { 497 int err; 498 499 if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state)) 500 return -EBUSY; 501 502 phy->roc_grant = false; 503 504 err = mt7925_mcu_set_roc(phy, mconf, chan, duration, type, 505 ++phy->roc_token_id); 506 if (err < 0) { 507 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 508 goto out; 509 } 510 511 if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, 4 * HZ)) { 512 mt7925_mcu_abort_roc(phy, mconf, phy->roc_token_id); 513 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 514 err = -ETIMEDOUT; 515 } 516 517 out: 518 return err; 519 } 520 521 static int mt7925_set_mlo_roc(struct mt792x_phy *phy, 522 struct mt792x_bss_conf *mconf, 523 u16 sel_links) 524 { 525 int err; 526 527 if (WARN_ON_ONCE(test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))) 528 return -EBUSY; 529 530 phy->roc_grant = false; 531 532 err = mt7925_mcu_set_mlo_roc(mconf, sel_links, 5, ++phy->roc_token_id); 533 if (err < 0) { 534 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 535 goto out; 536 } 537 538 if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, 4 * HZ)) { 539 mt7925_mcu_abort_roc(phy, mconf, phy->roc_token_id); 540 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 541 err = -ETIMEDOUT; 542 } 543 544 out: 545 return err; 546 } 547 548 static int mt7925_remain_on_channel(struct ieee80211_hw *hw, 549 struct ieee80211_vif *vif, 550 struct ieee80211_channel *chan, 551 int duration, 552 enum ieee80211_roc_type type) 553 { 554 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 555 struct mt792x_phy *phy = mt792x_hw_phy(hw); 556 int err; 557 558 mt792x_mutex_acquire(phy->dev); 559 err = mt7925_set_roc(phy, &mvif->bss_conf, 560 chan, duration, MT7925_ROC_REQ_ROC); 561 mt792x_mutex_release(phy->dev); 562 563 return err; 564 } 565 566 static int mt7925_cancel_remain_on_channel(struct ieee80211_hw *hw, 567 struct ieee80211_vif *vif) 568 { 569 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 570 struct mt792x_phy *phy = mt792x_hw_phy(hw); 571 572 return mt7925_abort_roc(phy, &mvif->bss_conf); 573 } 574 575 static int mt7925_set_link_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 576 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 577 struct ieee80211_key_conf *key, int link_id) 578 { 579 struct mt792x_dev *dev = mt792x_hw_dev(hw); 580 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 581 struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv : 582 &mvif->sta; 583 struct ieee80211_bss_conf *link_conf; 584 struct ieee80211_link_sta *link_sta; 585 int idx = key->keyidx, err = 0; 586 struct mt792x_link_sta *mlink; 587 struct mt792x_bss_conf *mconf; 588 struct mt76_wcid *wcid; 589 u8 *wcid_keyidx; 590 591 link_conf = mt792x_vif_to_bss_conf(vif, link_id); 592 link_sta = sta ? mt792x_sta_to_link_sta(vif, sta, link_id) : NULL; 593 mconf = mt792x_vif_to_link(mvif, link_id); 594 mlink = mt792x_sta_to_link(msta, link_id); 595 wcid = &mlink->wcid; 596 wcid_keyidx = &wcid->hw_key_idx; 597 598 /* fall back to sw encryption for unsupported ciphers */ 599 switch (key->cipher) { 600 case WLAN_CIPHER_SUITE_AES_CMAC: 601 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE; 602 wcid_keyidx = &wcid->hw_key_idx2; 603 break; 604 case WLAN_CIPHER_SUITE_WEP40: 605 case WLAN_CIPHER_SUITE_WEP104: 606 if (!mvif->wep_sta) 607 return -EOPNOTSUPP; 608 break; 609 case WLAN_CIPHER_SUITE_TKIP: 610 case WLAN_CIPHER_SUITE_CCMP: 611 case WLAN_CIPHER_SUITE_CCMP_256: 612 case WLAN_CIPHER_SUITE_GCMP: 613 case WLAN_CIPHER_SUITE_GCMP_256: 614 case WLAN_CIPHER_SUITE_SMS4: 615 break; 616 default: 617 return -EOPNOTSUPP; 618 } 619 620 if (cmd == SET_KEY && !mconf->mt76.cipher) { 621 struct mt792x_phy *phy = mt792x_hw_phy(hw); 622 623 mconf->mt76.cipher = mt7925_mcu_get_cipher(key->cipher); 624 mt7925_mcu_add_bss_info(phy, mconf->mt76.ctx, link_conf, 625 link_sta, true); 626 } 627 628 if (cmd == SET_KEY) 629 *wcid_keyidx = idx; 630 else if (idx == *wcid_keyidx) 631 *wcid_keyidx = -1; 632 else 633 goto out; 634 635 mt76_wcid_key_setup(&dev->mt76, wcid, 636 cmd == SET_KEY ? key : NULL); 637 638 err = mt7925_mcu_add_key(&dev->mt76, vif, &mlink->bip, 639 key, MCU_UNI_CMD(STA_REC_UPDATE), 640 &mlink->wcid, cmd, msta); 641 642 if (err) 643 goto out; 644 645 if (key->cipher == WLAN_CIPHER_SUITE_WEP104 || 646 key->cipher == WLAN_CIPHER_SUITE_WEP40) 647 err = mt7925_mcu_add_key(&dev->mt76, vif, &mvif->wep_sta->deflink.bip, 648 key, MCU_WMWA_UNI_CMD(STA_REC_UPDATE), 649 &mvif->wep_sta->deflink.wcid, cmd, msta); 650 out: 651 return err; 652 } 653 654 static int mt7925_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 655 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 656 struct ieee80211_key_conf *key) 657 { 658 struct mt792x_dev *dev = mt792x_hw_dev(hw); 659 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 660 struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv : 661 &mvif->sta; 662 int err; 663 664 /* The hardware does not support per-STA RX GTK, fallback 665 * to software mode for these. 666 */ 667 if ((vif->type == NL80211_IFTYPE_ADHOC || 668 vif->type == NL80211_IFTYPE_MESH_POINT) && 669 (key->cipher == WLAN_CIPHER_SUITE_TKIP || 670 key->cipher == WLAN_CIPHER_SUITE_CCMP) && 671 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) 672 return -EOPNOTSUPP; 673 674 mt792x_mutex_acquire(dev); 675 676 if (ieee80211_vif_is_mld(vif)) { 677 unsigned int link_id; 678 unsigned long add; 679 680 add = key->link_id != -1 ? BIT(key->link_id) : msta->valid_links; 681 682 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 683 err = mt7925_set_link_key(hw, cmd, vif, sta, key, link_id); 684 if (err < 0) 685 break; 686 } 687 } else { 688 err = mt7925_set_link_key(hw, cmd, vif, sta, key, vif->bss_conf.link_id); 689 } 690 691 mt792x_mutex_release(dev); 692 693 return err; 694 } 695 696 static void 697 mt7925_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif) 698 { 699 struct mt792x_dev *dev = priv; 700 struct ieee80211_hw *hw = mt76_hw(dev); 701 bool pm_enable = dev->pm.enable; 702 int err; 703 704 err = mt7925_mcu_set_beacon_filter(dev, vif, pm_enable); 705 if (err < 0) 706 return; 707 708 if (pm_enable) { 709 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER; 710 ieee80211_hw_set(hw, CONNECTION_MONITOR); 711 } else { 712 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER; 713 __clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags); 714 } 715 } 716 717 static void 718 mt7925_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif) 719 { 720 struct mt792x_dev *dev = priv; 721 struct ieee80211_hw *hw = mt76_hw(dev); 722 struct mt76_connac_pm *pm = &dev->pm; 723 bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR); 724 725 mt7925_mcu_set_sniffer(dev, vif, monitor); 726 pm->enable = pm->enable_user && !monitor; 727 pm->ds_enable = pm->ds_enable_user && !monitor; 728 729 mt7925_mcu_set_deep_sleep(dev, pm->ds_enable); 730 731 if (monitor) 732 mt7925_mcu_set_beacon_filter(dev, vif, false); 733 } 734 735 void mt7925_set_runtime_pm(struct mt792x_dev *dev) 736 { 737 struct ieee80211_hw *hw = mt76_hw(dev); 738 struct mt76_connac_pm *pm = &dev->pm; 739 bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR); 740 741 pm->enable = pm->enable_user && !monitor; 742 ieee80211_iterate_active_interfaces(hw, 743 IEEE80211_IFACE_ITER_RESUME_ALL, 744 mt7925_pm_interface_iter, dev); 745 pm->ds_enable = pm->ds_enable_user && !monitor; 746 mt7925_mcu_set_deep_sleep(dev, pm->ds_enable); 747 } 748 749 static int mt7925_config(struct ieee80211_hw *hw, u32 changed) 750 { 751 struct mt792x_dev *dev = mt792x_hw_dev(hw); 752 int ret = 0; 753 754 mt792x_mutex_acquire(dev); 755 756 if (changed & IEEE80211_CONF_CHANGE_POWER) { 757 ret = mt7925_set_tx_sar_pwr(hw, NULL); 758 if (ret) 759 goto out; 760 } 761 762 if (changed & IEEE80211_CONF_CHANGE_MONITOR) { 763 ieee80211_iterate_active_interfaces(hw, 764 IEEE80211_IFACE_ITER_RESUME_ALL, 765 mt7925_sniffer_interface_iter, dev); 766 } 767 768 out: 769 mt792x_mutex_release(dev); 770 771 return ret; 772 } 773 774 static void mt7925_configure_filter(struct ieee80211_hw *hw, 775 unsigned int changed_flags, 776 unsigned int *total_flags, 777 u64 multicast) 778 { 779 #define MT7925_FILTER_FCSFAIL BIT(2) 780 #define MT7925_FILTER_CONTROL BIT(5) 781 #define MT7925_FILTER_OTHER_BSS BIT(6) 782 #define MT7925_FILTER_ENABLE BIT(31) 783 struct mt792x_dev *dev = mt792x_hw_dev(hw); 784 u32 flags = MT7925_FILTER_ENABLE; 785 786 #define MT7925_FILTER(_fif, _type) do { \ 787 if (*total_flags & (_fif)) \ 788 flags |= MT7925_FILTER_##_type; \ 789 } while (0) 790 791 MT7925_FILTER(FIF_FCSFAIL, FCSFAIL); 792 MT7925_FILTER(FIF_CONTROL, CONTROL); 793 MT7925_FILTER(FIF_OTHER_BSS, OTHER_BSS); 794 795 mt792x_mutex_acquire(dev); 796 mt7925_mcu_set_rxfilter(dev, flags, 0, 0); 797 mt792x_mutex_release(dev); 798 799 *total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL); 800 } 801 802 static u8 803 mt7925_get_rates_table(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 804 bool beacon, bool mcast) 805 { 806 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv; 807 struct mt76_phy *mphy = hw->priv; 808 u16 rate; 809 u8 i, idx, ht; 810 811 rate = mt76_connac2_mac_tx_rate_val(mphy, vif, beacon, mcast); 812 ht = FIELD_GET(MT_TX_RATE_MODE, rate) > MT_PHY_TYPE_OFDM; 813 814 if (beacon && ht) { 815 struct mt792x_dev *dev = mt792x_hw_dev(hw); 816 817 /* must odd index */ 818 idx = MT7925_BEACON_RATES_TBL + 2 * (mvif->idx % 20); 819 mt7925_mac_set_fixed_rate_table(dev, idx, rate); 820 return idx; 821 } 822 823 idx = FIELD_GET(MT_TX_RATE_IDX, rate); 824 for (i = 0; i < ARRAY_SIZE(mt76_rates); i++) 825 if ((mt76_rates[i].hw_value & GENMASK(7, 0)) == idx) 826 return MT792x_BASIC_RATES_TBL + i; 827 828 return mvif->basic_rates_idx; 829 } 830 831 static int mt7925_mac_link_sta_add(struct mt76_dev *mdev, 832 struct ieee80211_vif *vif, 833 struct ieee80211_link_sta *link_sta) 834 { 835 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 836 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 837 struct ieee80211_bss_conf *link_conf; 838 struct mt792x_bss_conf *mconf; 839 u8 link_id = link_sta->link_id; 840 struct mt792x_link_sta *mlink; 841 struct mt792x_sta *msta; 842 struct mt76_wcid *wcid; 843 int ret, idx; 844 845 msta = (struct mt792x_sta *)link_sta->sta->drv_priv; 846 mlink = mt792x_sta_to_link(msta, link_id); 847 848 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1); 849 if (idx < 0) 850 return -ENOSPC; 851 852 mconf = mt792x_vif_to_link(mvif, link_id); 853 INIT_LIST_HEAD(&mlink->wcid.poll_list); 854 mlink->wcid.sta = 1; 855 mlink->wcid.idx = idx; 856 mlink->wcid.phy_idx = 0; 857 mlink->wcid.tx_info |= MT_WCID_TX_INFO_SET; 858 mlink->last_txs = jiffies; 859 mlink->wcid.link_id = link_sta->link_id; 860 mlink->wcid.link_valid = !!link_sta->sta->valid_links; 861 mlink->sta = msta; 862 863 wcid = &mlink->wcid; 864 ewma_signal_init(&wcid->rssi); 865 rcu_assign_pointer(dev->mt76.wcid[wcid->idx], wcid); 866 mt76_wcid_init(wcid); 867 ewma_avg_signal_init(&mlink->avg_ack_signal); 868 memset(mlink->airtime_ac, 0, 869 sizeof(msta->deflink.airtime_ac)); 870 871 ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm); 872 if (ret) 873 return ret; 874 875 mt7925_mac_wtbl_update(dev, idx, 876 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 877 878 link_conf = mt792x_vif_to_bss_conf(vif, link_id); 879 880 /* should update bss info before STA add */ 881 if (vif->type == NL80211_IFTYPE_STATION && !link_sta->sta->tdls) { 882 if (ieee80211_vif_is_mld(vif)) 883 mt7925_mcu_add_bss_info(&dev->phy, mconf->mt76.ctx, 884 link_conf, link_sta, link_sta != mlink->pri_link); 885 else 886 mt7925_mcu_add_bss_info(&dev->phy, mconf->mt76.ctx, 887 link_conf, link_sta, false); 888 } 889 890 if (ieee80211_vif_is_mld(vif) && 891 link_sta == mlink->pri_link) { 892 ret = mt7925_mcu_sta_update(dev, link_sta, vif, true, 893 MT76_STA_INFO_STATE_NONE); 894 if (ret) 895 return ret; 896 } else if (ieee80211_vif_is_mld(vif) && 897 link_sta != mlink->pri_link) { 898 ret = mt7925_mcu_sta_update(dev, mlink->pri_link, vif, 899 true, MT76_STA_INFO_STATE_ASSOC); 900 if (ret) 901 return ret; 902 903 ret = mt7925_mcu_sta_update(dev, link_sta, vif, true, 904 MT76_STA_INFO_STATE_ASSOC); 905 if (ret) 906 return ret; 907 } else { 908 ret = mt7925_mcu_sta_update(dev, link_sta, vif, true, 909 MT76_STA_INFO_STATE_NONE); 910 if (ret) 911 return ret; 912 } 913 914 mt76_connac_power_save_sched(&dev->mphy, &dev->pm); 915 916 return 0; 917 } 918 919 static int 920 mt7925_mac_sta_add_links(struct mt792x_dev *dev, struct ieee80211_vif *vif, 921 struct ieee80211_sta *sta, unsigned long new_links) 922 { 923 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 924 unsigned int link_id; 925 int err = 0; 926 927 for_each_set_bit(link_id, &new_links, IEEE80211_MLD_MAX_NUM_LINKS) { 928 struct ieee80211_link_sta *link_sta; 929 struct mt792x_link_sta *mlink; 930 931 if (msta->deflink_id == IEEE80211_LINK_UNSPECIFIED) { 932 mlink = &msta->deflink; 933 msta->deflink_id = link_id; 934 } else { 935 mlink = devm_kzalloc(dev->mt76.dev, sizeof(*mlink), GFP_KERNEL); 936 if (!mlink) { 937 err = -ENOMEM; 938 break; 939 } 940 } 941 942 msta->valid_links |= BIT(link_id); 943 rcu_assign_pointer(msta->link[link_id], mlink); 944 mlink->sta = msta; 945 mlink->pri_link = &sta->deflink; 946 mlink->wcid.def_wcid = &msta->deflink.wcid; 947 948 link_sta = mt792x_sta_to_link_sta(vif, sta, link_id); 949 mt7925_mac_link_sta_add(&dev->mt76, vif, link_sta); 950 } 951 952 return err; 953 } 954 955 int mt7925_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif, 956 struct ieee80211_sta *sta) 957 { 958 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 959 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 960 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 961 int err; 962 963 msta->vif = mvif; 964 965 if (vif->type == NL80211_IFTYPE_STATION) 966 mvif->wep_sta = msta; 967 968 if (ieee80211_vif_is_mld(vif)) { 969 msta->deflink_id = IEEE80211_LINK_UNSPECIFIED; 970 971 err = mt7925_mac_sta_add_links(dev, vif, sta, sta->valid_links); 972 } else { 973 err = mt7925_mac_link_sta_add(mdev, vif, &sta->deflink); 974 } 975 976 return err; 977 } 978 EXPORT_SYMBOL_GPL(mt7925_mac_sta_add); 979 980 static u16 981 mt7925_mac_select_links(struct mt76_dev *mdev, struct ieee80211_vif *vif) 982 { 983 unsigned long usable_links = ieee80211_vif_usable_links(vif); 984 struct { 985 u8 link_id; 986 enum nl80211_band band; 987 } data[IEEE80211_MLD_MAX_NUM_LINKS]; 988 u8 link_id, i, j, n_data = 0; 989 u16 sel_links = 0; 990 991 if (!ieee80211_vif_is_mld(vif)) 992 return 0; 993 994 if (vif->active_links == usable_links) 995 return vif->active_links; 996 997 rcu_read_lock(); 998 for_each_set_bit(link_id, &usable_links, IEEE80211_MLD_MAX_NUM_LINKS) { 999 struct ieee80211_bss_conf *link_conf = 1000 rcu_dereference(vif->link_conf[link_id]); 1001 1002 if (WARN_ON_ONCE(!link_conf)) 1003 continue; 1004 1005 data[n_data].link_id = link_id; 1006 data[n_data].band = link_conf->chanreq.oper.chan->band; 1007 n_data++; 1008 } 1009 rcu_read_unlock(); 1010 1011 for (i = 0; i < n_data; i++) { 1012 if (!(BIT(data[i].link_id) & vif->active_links)) 1013 continue; 1014 1015 sel_links = BIT(data[i].link_id); 1016 1017 for (j = 0; j < n_data; j++) { 1018 if (data[i].band != data[j].band) { 1019 sel_links |= BIT(data[j].link_id); 1020 break; 1021 } 1022 } 1023 1024 break; 1025 } 1026 1027 return sel_links; 1028 } 1029 1030 static void 1031 mt7925_mac_set_links(struct mt76_dev *mdev, struct ieee80211_vif *vif) 1032 { 1033 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 1034 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1035 struct ieee80211_bss_conf *link_conf = 1036 mt792x_vif_to_bss_conf(vif, mvif->deflink_id); 1037 struct cfg80211_chan_def *chandef = &link_conf->chanreq.oper; 1038 enum nl80211_band band = chandef->chan->band, secondary_band; 1039 1040 u16 sel_links = mt7925_mac_select_links(mdev, vif); 1041 u8 secondary_link_id = __ffs(~BIT(mvif->deflink_id) & sel_links); 1042 1043 if (!ieee80211_vif_is_mld(vif) || hweight16(sel_links) < 2) 1044 return; 1045 1046 link_conf = mt792x_vif_to_bss_conf(vif, secondary_link_id); 1047 secondary_band = link_conf->chanreq.oper.chan->band; 1048 1049 if (band == NL80211_BAND_2GHZ || 1050 (band == NL80211_BAND_5GHZ && secondary_band == NL80211_BAND_6GHZ)) { 1051 mt7925_abort_roc(mvif->phy, &mvif->bss_conf); 1052 1053 mt792x_mutex_acquire(dev); 1054 1055 mt7925_set_mlo_roc(mvif->phy, &mvif->bss_conf, sel_links); 1056 1057 mt792x_mutex_release(dev); 1058 } 1059 1060 ieee80211_set_active_links_async(vif, sel_links); 1061 } 1062 1063 static void mt7925_mac_link_sta_assoc(struct mt76_dev *mdev, 1064 struct ieee80211_vif *vif, 1065 struct ieee80211_link_sta *link_sta) 1066 { 1067 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 1068 struct ieee80211_bss_conf *link_conf; 1069 struct mt792x_link_sta *mlink; 1070 struct mt792x_sta *msta; 1071 1072 msta = (struct mt792x_sta *)link_sta->sta->drv_priv; 1073 mlink = mt792x_sta_to_link(msta, link_sta->link_id); 1074 1075 mt792x_mutex_acquire(dev); 1076 1077 if (ieee80211_vif_is_mld(vif)) { 1078 link_conf = mt792x_vif_to_bss_conf(vif, msta->deflink_id); 1079 } else { 1080 link_conf = mt792x_vif_to_bss_conf(vif, vif->bss_conf.link_id); 1081 } 1082 1083 if (vif->type == NL80211_IFTYPE_STATION && !link_sta->sta->tdls) { 1084 struct mt792x_bss_conf *mconf; 1085 1086 mconf = mt792x_link_conf_to_mconf(link_conf); 1087 mt7925_mcu_add_bss_info(&dev->phy, mconf->mt76.ctx, 1088 link_conf, link_sta, true); 1089 } 1090 1091 ewma_avg_signal_init(&mlink->avg_ack_signal); 1092 1093 mt7925_mac_wtbl_update(dev, mlink->wcid.idx, 1094 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 1095 memset(mlink->airtime_ac, 0, sizeof(mlink->airtime_ac)); 1096 1097 mt7925_mcu_sta_update(dev, link_sta, vif, true, MT76_STA_INFO_STATE_ASSOC); 1098 1099 mt792x_mutex_release(dev); 1100 } 1101 1102 int mt7925_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif, 1103 struct ieee80211_sta *sta, enum mt76_sta_event ev) 1104 { 1105 struct ieee80211_link_sta *link_sta = &sta->deflink; 1106 1107 if (ev != MT76_STA_EVENT_ASSOC) 1108 return 0; 1109 1110 if (ieee80211_vif_is_mld(vif)) { 1111 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 1112 1113 link_sta = mt792x_sta_to_link_sta(vif, sta, msta->deflink_id); 1114 mt7925_mac_set_links(mdev, vif); 1115 } 1116 1117 mt7925_mac_link_sta_assoc(mdev, vif, link_sta); 1118 1119 return 0; 1120 } 1121 EXPORT_SYMBOL_GPL(mt7925_mac_sta_event); 1122 1123 static void mt7925_mac_link_sta_remove(struct mt76_dev *mdev, 1124 struct ieee80211_vif *vif, 1125 struct ieee80211_link_sta *link_sta) 1126 { 1127 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 1128 struct ieee80211_bss_conf *link_conf; 1129 u8 link_id = link_sta->link_id; 1130 struct mt792x_link_sta *mlink; 1131 struct mt792x_sta *msta; 1132 1133 msta = (struct mt792x_sta *)link_sta->sta->drv_priv; 1134 mlink = mt792x_sta_to_link(msta, link_id); 1135 1136 mt7925_roc_abort_sync(dev); 1137 1138 mt76_connac_free_pending_tx_skbs(&dev->pm, &mlink->wcid); 1139 mt76_connac_pm_wake(&dev->mphy, &dev->pm); 1140 1141 mt7925_mcu_sta_update(dev, link_sta, vif, false, 1142 MT76_STA_INFO_STATE_NONE); 1143 mt7925_mac_wtbl_update(dev, mlink->wcid.idx, 1144 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 1145 1146 link_conf = mt792x_vif_to_bss_conf(vif, link_id); 1147 1148 if (vif->type == NL80211_IFTYPE_STATION && !link_sta->sta->tdls) { 1149 struct mt792x_bss_conf *mconf; 1150 1151 mconf = mt792x_link_conf_to_mconf(link_conf); 1152 mt792x_mac_link_bss_remove(dev, mconf, mlink); 1153 } 1154 1155 spin_lock_bh(&mdev->sta_poll_lock); 1156 if (!list_empty(&mlink->wcid.poll_list)) 1157 list_del_init(&mlink->wcid.poll_list); 1158 spin_unlock_bh(&mdev->sta_poll_lock); 1159 1160 mt76_connac_power_save_sched(&dev->mphy, &dev->pm); 1161 } 1162 1163 static int 1164 mt7925_mac_sta_remove_links(struct mt792x_dev *dev, struct ieee80211_vif *vif, 1165 struct ieee80211_sta *sta, unsigned long old_links) 1166 { 1167 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 1168 struct mt76_dev *mdev = &dev->mt76; 1169 struct mt76_wcid *wcid; 1170 unsigned int link_id; 1171 1172 for_each_set_bit(link_id, &old_links, IEEE80211_MLD_MAX_NUM_LINKS) { 1173 struct ieee80211_link_sta *link_sta; 1174 struct mt792x_link_sta *mlink; 1175 1176 link_sta = mt792x_sta_to_link_sta(vif, sta, link_id); 1177 if (!link_sta) 1178 continue; 1179 1180 mlink = mt792x_sta_to_link(msta, link_id); 1181 if (!mlink) 1182 continue; 1183 1184 mt7925_mac_link_sta_remove(&dev->mt76, vif, link_sta); 1185 1186 wcid = &mlink->wcid; 1187 rcu_assign_pointer(msta->link[link_id], NULL); 1188 msta->valid_links &= ~BIT(link_id); 1189 mlink->sta = NULL; 1190 mlink->pri_link = NULL; 1191 1192 if (link_sta != mlink->pri_link) { 1193 mt76_wcid_cleanup(mdev, wcid); 1194 mt76_wcid_mask_clear(mdev->wcid_mask, wcid->idx); 1195 mt76_wcid_mask_clear(mdev->wcid_phy_mask, wcid->idx); 1196 } 1197 1198 if (msta->deflink_id == link_id) 1199 msta->deflink_id = IEEE80211_LINK_UNSPECIFIED; 1200 } 1201 1202 return 0; 1203 } 1204 1205 void mt7925_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 1206 struct ieee80211_sta *sta) 1207 { 1208 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 1209 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 1210 struct { 1211 struct { 1212 u8 omac_idx; 1213 u8 band_idx; 1214 __le16 pad; 1215 } __packed hdr; 1216 struct req_tlv { 1217 __le16 tag; 1218 __le16 len; 1219 u8 active; 1220 u8 link_idx; /* hw link idx */ 1221 u8 omac_addr[ETH_ALEN]; 1222 } __packed tlv; 1223 } dev_req = { 1224 .hdr = { 1225 .omac_idx = 0, 1226 .band_idx = 0, 1227 }, 1228 .tlv = { 1229 .tag = cpu_to_le16(DEV_INFO_ACTIVE), 1230 .len = cpu_to_le16(sizeof(struct req_tlv)), 1231 .active = true, 1232 }, 1233 }; 1234 unsigned long rem; 1235 1236 rem = ieee80211_vif_is_mld(vif) ? msta->valid_links : BIT(0); 1237 1238 mt7925_mac_sta_remove_links(dev, vif, sta, rem); 1239 1240 if (ieee80211_vif_is_mld(vif)) { 1241 mt7925_mcu_set_dbdc(&dev->mphy, false); 1242 1243 /* recovery omac address for the legacy interface */ 1244 memcpy(dev_req.tlv.omac_addr, vif->addr, ETH_ALEN); 1245 mt76_mcu_send_msg(mdev, MCU_UNI_CMD(DEV_INFO_UPDATE), 1246 &dev_req, sizeof(dev_req), true); 1247 } 1248 1249 if (vif->type == NL80211_IFTYPE_STATION) { 1250 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1251 1252 mvif->wep_sta = NULL; 1253 ewma_rssi_init(&mvif->bss_conf.rssi); 1254 } 1255 } 1256 EXPORT_SYMBOL_GPL(mt7925_mac_sta_remove); 1257 1258 static int mt7925_set_rts_threshold(struct ieee80211_hw *hw, u32 val) 1259 { 1260 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1261 1262 mt792x_mutex_acquire(dev); 1263 mt7925_mcu_set_rts_thresh(&dev->phy, val); 1264 mt792x_mutex_release(dev); 1265 1266 return 0; 1267 } 1268 1269 static int 1270 mt7925_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1271 struct ieee80211_ampdu_params *params) 1272 { 1273 enum ieee80211_ampdu_mlme_action action = params->action; 1274 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1275 struct ieee80211_sta *sta = params->sta; 1276 struct ieee80211_txq *txq = sta->txq[params->tid]; 1277 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 1278 u16 tid = params->tid; 1279 u16 ssn = params->ssn; 1280 struct mt76_txq *mtxq; 1281 int ret = 0; 1282 1283 if (!txq) 1284 return -EINVAL; 1285 1286 mtxq = (struct mt76_txq *)txq->drv_priv; 1287 1288 mt792x_mutex_acquire(dev); 1289 switch (action) { 1290 case IEEE80211_AMPDU_RX_START: 1291 mt76_rx_aggr_start(&dev->mt76, &msta->deflink.wcid, tid, ssn, 1292 params->buf_size); 1293 mt7925_mcu_uni_rx_ba(dev, vif, params, true); 1294 break; 1295 case IEEE80211_AMPDU_RX_STOP: 1296 mt76_rx_aggr_stop(&dev->mt76, &msta->deflink.wcid, tid); 1297 mt7925_mcu_uni_rx_ba(dev, vif, params, false); 1298 break; 1299 case IEEE80211_AMPDU_TX_OPERATIONAL: 1300 mtxq->aggr = true; 1301 mtxq->send_bar = false; 1302 mt7925_mcu_uni_tx_ba(dev, vif, params, true); 1303 break; 1304 case IEEE80211_AMPDU_TX_STOP_FLUSH: 1305 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 1306 mtxq->aggr = false; 1307 clear_bit(tid, &msta->deflink.wcid.ampdu_state); 1308 mt7925_mcu_uni_tx_ba(dev, vif, params, false); 1309 break; 1310 case IEEE80211_AMPDU_TX_START: 1311 set_bit(tid, &msta->deflink.wcid.ampdu_state); 1312 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 1313 break; 1314 case IEEE80211_AMPDU_TX_STOP_CONT: 1315 mtxq->aggr = false; 1316 clear_bit(tid, &msta->deflink.wcid.ampdu_state); 1317 mt7925_mcu_uni_tx_ba(dev, vif, params, false); 1318 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 1319 break; 1320 } 1321 mt792x_mutex_release(dev); 1322 1323 return ret; 1324 } 1325 1326 static bool is_valid_alpha2(const char *alpha2) 1327 { 1328 if (!alpha2) 1329 return false; 1330 1331 if (alpha2[0] == '0' && alpha2[1] == '0') 1332 return true; 1333 1334 if (isalpha(alpha2[0]) && isalpha(alpha2[1])) 1335 return true; 1336 1337 return false; 1338 } 1339 1340 void mt7925_scan_work(struct work_struct *work) 1341 { 1342 struct mt792x_phy *phy; 1343 1344 phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy, 1345 scan_work.work); 1346 1347 while (true) { 1348 struct mt76_dev *mdev = &phy->dev->mt76; 1349 struct sk_buff *skb; 1350 struct tlv *tlv; 1351 int tlv_len; 1352 1353 spin_lock_bh(&phy->dev->mt76.lock); 1354 skb = __skb_dequeue(&phy->scan_event_list); 1355 spin_unlock_bh(&phy->dev->mt76.lock); 1356 1357 if (!skb) 1358 break; 1359 1360 skb_pull(skb, sizeof(struct mt7925_mcu_rxd) + 4); 1361 tlv = (struct tlv *)skb->data; 1362 tlv_len = skb->len; 1363 1364 while (tlv_len > 0 && le16_to_cpu(tlv->len) <= tlv_len) { 1365 struct mt7925_mcu_scan_chinfo_event *evt; 1366 1367 switch (le16_to_cpu(tlv->tag)) { 1368 case UNI_EVENT_SCAN_DONE_BASIC: 1369 if (test_and_clear_bit(MT76_HW_SCANNING, &phy->mt76->state)) { 1370 struct cfg80211_scan_info info = { 1371 .aborted = false, 1372 }; 1373 ieee80211_scan_completed(phy->mt76->hw, &info); 1374 } 1375 break; 1376 case UNI_EVENT_SCAN_DONE_CHNLINFO: 1377 evt = (struct mt7925_mcu_scan_chinfo_event *)tlv->data; 1378 1379 if (!is_valid_alpha2(evt->alpha2)) 1380 break; 1381 1382 if (mdev->alpha2[0] != '0' && mdev->alpha2[1] != '0') 1383 break; 1384 1385 mt7925_mcu_set_clc(phy->dev, evt->alpha2, ENVIRON_INDOOR); 1386 1387 break; 1388 case UNI_EVENT_SCAN_DONE_NLO: 1389 ieee80211_sched_scan_results(phy->mt76->hw); 1390 break; 1391 default: 1392 break; 1393 } 1394 1395 tlv_len -= le16_to_cpu(tlv->len); 1396 tlv = (struct tlv *)((char *)(tlv) + le16_to_cpu(tlv->len)); 1397 } 1398 1399 dev_kfree_skb(skb); 1400 } 1401 } 1402 1403 static int 1404 mt7925_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1405 struct ieee80211_scan_request *req) 1406 { 1407 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1408 struct mt76_phy *mphy = hw->priv; 1409 int err; 1410 1411 mt792x_mutex_acquire(dev); 1412 err = mt7925_mcu_hw_scan(mphy, vif, req); 1413 mt792x_mutex_release(dev); 1414 1415 return err; 1416 } 1417 1418 static void 1419 mt7925_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1420 { 1421 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1422 struct mt76_phy *mphy = hw->priv; 1423 1424 mt792x_mutex_acquire(dev); 1425 mt7925_mcu_cancel_hw_scan(mphy, vif); 1426 mt792x_mutex_release(dev); 1427 } 1428 1429 static int 1430 mt7925_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1431 struct cfg80211_sched_scan_request *req, 1432 struct ieee80211_scan_ies *ies) 1433 { 1434 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1435 struct mt76_phy *mphy = hw->priv; 1436 int err; 1437 1438 mt792x_mutex_acquire(dev); 1439 1440 err = mt7925_mcu_sched_scan_req(mphy, vif, req); 1441 if (err < 0) 1442 goto out; 1443 1444 err = mt7925_mcu_sched_scan_enable(mphy, vif, true); 1445 out: 1446 mt792x_mutex_release(dev); 1447 1448 return err; 1449 } 1450 1451 static int 1452 mt7925_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1453 { 1454 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1455 struct mt76_phy *mphy = hw->priv; 1456 int err; 1457 1458 mt792x_mutex_acquire(dev); 1459 err = mt7925_mcu_sched_scan_enable(mphy, vif, false); 1460 mt792x_mutex_release(dev); 1461 1462 return err; 1463 } 1464 1465 static int 1466 mt7925_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant) 1467 { 1468 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1469 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1470 int max_nss = hweight8(hw->wiphy->available_antennas_tx); 1471 1472 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss) 1473 return -EINVAL; 1474 1475 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant) 1476 tx_ant = BIT(ffs(tx_ant) - 1) - 1; 1477 1478 mt792x_mutex_acquire(dev); 1479 1480 phy->mt76->antenna_mask = tx_ant; 1481 phy->mt76->chainmask = tx_ant; 1482 1483 mt76_set_stream_caps(phy->mt76, true); 1484 mt7925_set_stream_he_eht_caps(phy); 1485 1486 /* TODO: update bmc_wtbl spe_idx when antenna changes */ 1487 mt792x_mutex_release(dev); 1488 1489 return 0; 1490 } 1491 1492 #ifdef CONFIG_PM 1493 static int mt7925_suspend(struct ieee80211_hw *hw, 1494 struct cfg80211_wowlan *wowlan) 1495 { 1496 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1497 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1498 1499 cancel_delayed_work_sync(&phy->scan_work); 1500 cancel_delayed_work_sync(&phy->mt76->mac_work); 1501 1502 cancel_delayed_work_sync(&dev->pm.ps_work); 1503 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL); 1504 1505 mt792x_mutex_acquire(dev); 1506 1507 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1508 ieee80211_iterate_active_interfaces(hw, 1509 IEEE80211_IFACE_ITER_RESUME_ALL, 1510 mt7925_mcu_set_suspend_iter, 1511 &dev->mphy); 1512 1513 mt792x_mutex_release(dev); 1514 1515 return 0; 1516 } 1517 1518 static int mt7925_resume(struct ieee80211_hw *hw) 1519 { 1520 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1521 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1522 1523 mt792x_mutex_acquire(dev); 1524 1525 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1526 ieee80211_iterate_active_interfaces(hw, 1527 IEEE80211_IFACE_ITER_RESUME_ALL, 1528 mt7925_mcu_set_suspend_iter, 1529 &dev->mphy); 1530 1531 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, 1532 MT792x_WATCHDOG_TIME); 1533 1534 mt792x_mutex_release(dev); 1535 1536 return 0; 1537 } 1538 1539 static void mt7925_set_rekey_data(struct ieee80211_hw *hw, 1540 struct ieee80211_vif *vif, 1541 struct cfg80211_gtk_rekey_data *data) 1542 { 1543 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1544 1545 mt792x_mutex_acquire(dev); 1546 mt76_connac_mcu_update_gtk_rekey(hw, vif, data); 1547 mt792x_mutex_release(dev); 1548 } 1549 #endif /* CONFIG_PM */ 1550 1551 static void mt7925_sta_set_decap_offload(struct ieee80211_hw *hw, 1552 struct ieee80211_vif *vif, 1553 struct ieee80211_sta *sta, 1554 bool enabled) 1555 { 1556 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 1557 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1558 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1559 unsigned long valid = mvif->valid_links; 1560 u8 i; 1561 1562 mt792x_mutex_acquire(dev); 1563 1564 valid = ieee80211_vif_is_mld(vif) ? mvif->valid_links : BIT(0); 1565 1566 for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) { 1567 struct mt792x_link_sta *mlink; 1568 1569 mlink = mt792x_sta_to_link(msta, i); 1570 1571 if (enabled) 1572 set_bit(MT_WCID_FLAG_HDR_TRANS, &mlink->wcid.flags); 1573 else 1574 clear_bit(MT_WCID_FLAG_HDR_TRANS, &mlink->wcid.flags); 1575 1576 mt7925_mcu_wtbl_update_hdr_trans(dev, vif, sta, i); 1577 } 1578 1579 mt792x_mutex_release(dev); 1580 } 1581 1582 #if IS_ENABLED(CONFIG_IPV6) 1583 static void __mt7925_ipv6_addr_change(struct ieee80211_hw *hw, 1584 struct ieee80211_bss_conf *link_conf, 1585 struct inet6_dev *idev) 1586 { 1587 struct mt792x_bss_conf *mconf = mt792x_link_conf_to_mconf(link_conf); 1588 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1589 struct inet6_ifaddr *ifa; 1590 struct sk_buff *skb; 1591 u8 idx = 0; 1592 1593 struct { 1594 struct { 1595 u8 bss_idx; 1596 u8 pad[3]; 1597 } __packed hdr; 1598 struct mt7925_arpns_tlv arpns; 1599 struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN]; 1600 } req_hdr = { 1601 .hdr = { 1602 .bss_idx = mconf->mt76.idx, 1603 }, 1604 .arpns = { 1605 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND), 1606 .len = cpu_to_le16(sizeof(req_hdr) - 4), 1607 .enable = true, 1608 }, 1609 }; 1610 1611 read_lock_bh(&idev->lock); 1612 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1613 if (ifa->flags & IFA_F_TENTATIVE) 1614 continue; 1615 req_hdr.ns_addrs[idx] = ifa->addr; 1616 if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN) 1617 break; 1618 } 1619 read_unlock_bh(&idev->lock); 1620 1621 if (!idx) 1622 return; 1623 1624 req_hdr.arpns.ips_num = idx; 1625 1626 skb = __mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr), 1627 0, GFP_ATOMIC); 1628 if (!skb) 1629 return; 1630 1631 skb_put_data(skb, &req_hdr, sizeof(req_hdr)); 1632 1633 skb_queue_tail(&dev->ipv6_ns_list, skb); 1634 1635 ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work); 1636 } 1637 1638 static void mt7925_ipv6_addr_change(struct ieee80211_hw *hw, 1639 struct ieee80211_vif *vif, 1640 struct inet6_dev *idev) 1641 { 1642 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1643 unsigned long valid = ieee80211_vif_is_mld(vif) ? 1644 mvif->valid_links : BIT(0); 1645 struct ieee80211_bss_conf *bss_conf; 1646 int i; 1647 1648 for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) { 1649 bss_conf = mt792x_vif_to_bss_conf(vif, i); 1650 __mt7925_ipv6_addr_change(hw, bss_conf, idev); 1651 } 1652 } 1653 1654 #endif 1655 1656 int mt7925_set_tx_sar_pwr(struct ieee80211_hw *hw, 1657 const struct cfg80211_sar_specs *sar) 1658 { 1659 struct mt76_phy *mphy = hw->priv; 1660 1661 if (sar) { 1662 int err = mt76_init_sar_power(hw, sar); 1663 1664 if (err) 1665 return err; 1666 } 1667 mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar); 1668 1669 return mt7925_mcu_set_rate_txpower(mphy); 1670 } 1671 1672 static int mt7925_set_sar_specs(struct ieee80211_hw *hw, 1673 const struct cfg80211_sar_specs *sar) 1674 { 1675 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1676 int err; 1677 1678 mt792x_mutex_acquire(dev); 1679 err = mt7925_mcu_set_clc(dev, dev->mt76.alpha2, 1680 dev->country_ie_env); 1681 if (err < 0) 1682 goto out; 1683 1684 err = mt7925_set_tx_sar_pwr(hw, sar); 1685 out: 1686 mt792x_mutex_release(dev); 1687 1688 return err; 1689 } 1690 1691 static void 1692 mt7925_channel_switch_beacon(struct ieee80211_hw *hw, 1693 struct ieee80211_vif *vif, 1694 struct cfg80211_chan_def *chandef) 1695 { 1696 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1697 1698 mt792x_mutex_acquire(dev); 1699 mt7925_mcu_uni_add_beacon_offload(dev, hw, vif, true); 1700 mt792x_mutex_release(dev); 1701 } 1702 1703 static int 1704 mt7925_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1705 unsigned int link_id, u16 queue, 1706 const struct ieee80211_tx_queue_params *params) 1707 { 1708 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1709 struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, link_id); 1710 static const u8 mq_to_aci[] = { 1711 [IEEE80211_AC_VO] = 3, 1712 [IEEE80211_AC_VI] = 2, 1713 [IEEE80211_AC_BE] = 0, 1714 [IEEE80211_AC_BK] = 1, 1715 }; 1716 1717 /* firmware uses access class index */ 1718 mconf->queue_params[mq_to_aci[queue]] = *params; 1719 1720 return 0; 1721 } 1722 1723 static int 1724 mt7925_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1725 struct ieee80211_bss_conf *link_conf) 1726 { 1727 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1728 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1729 int err; 1730 1731 mt792x_mutex_acquire(dev); 1732 1733 err = mt7925_mcu_add_bss_info(&dev->phy, mvif->bss_conf.mt76.ctx, 1734 link_conf, NULL, true); 1735 if (err) 1736 goto out; 1737 1738 err = mt7925_mcu_set_bss_pm(dev, link_conf, true); 1739 if (err) 1740 goto out; 1741 1742 err = mt7925_mcu_sta_update(dev, NULL, vif, true, 1743 MT76_STA_INFO_STATE_NONE); 1744 out: 1745 mt792x_mutex_release(dev); 1746 1747 return err; 1748 } 1749 1750 static void 1751 mt7925_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1752 struct ieee80211_bss_conf *link_conf) 1753 { 1754 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1755 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1756 int err; 1757 1758 mt792x_mutex_acquire(dev); 1759 1760 err = mt7925_mcu_set_bss_pm(dev, link_conf, false); 1761 if (err) 1762 goto out; 1763 1764 mt7925_mcu_add_bss_info(&dev->phy, mvif->bss_conf.mt76.ctx, link_conf, 1765 NULL, false); 1766 1767 out: 1768 mt792x_mutex_release(dev); 1769 } 1770 1771 static int 1772 mt7925_add_chanctx(struct ieee80211_hw *hw, 1773 struct ieee80211_chanctx_conf *ctx) 1774 { 1775 return 0; 1776 } 1777 1778 static void 1779 mt7925_remove_chanctx(struct ieee80211_hw *hw, 1780 struct ieee80211_chanctx_conf *ctx) 1781 { 1782 } 1783 1784 static void 1785 mt7925_change_chanctx(struct ieee80211_hw *hw, 1786 struct ieee80211_chanctx_conf *ctx, 1787 u32 changed) 1788 { 1789 struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv; 1790 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1791 struct mt792x_bss_conf *mconf; 1792 struct ieee80211_vif *vif; 1793 struct mt792x_vif *mvif; 1794 1795 if (!mctx->bss_conf) 1796 return; 1797 1798 mconf = mctx->bss_conf; 1799 mvif = mconf->vif; 1800 vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv); 1801 1802 mt792x_mutex_acquire(phy->dev); 1803 if (vif->type == NL80211_IFTYPE_MONITOR) { 1804 mt7925_mcu_set_sniffer(mvif->phy->dev, vif, true); 1805 mt7925_mcu_config_sniffer(mvif, ctx); 1806 } else { 1807 if (ieee80211_vif_is_mld(vif)) { 1808 unsigned long valid = mvif->valid_links; 1809 u8 i; 1810 1811 for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) { 1812 mconf = mt792x_vif_to_link(mvif, i); 1813 if (mconf && mconf->mt76.ctx == ctx) 1814 break; 1815 } 1816 1817 } else { 1818 mconf = &mvif->bss_conf; 1819 } 1820 1821 if (mconf) { 1822 struct ieee80211_bss_conf *link_conf; 1823 1824 link_conf = mt792x_vif_to_bss_conf(vif, mconf->link_id); 1825 mt7925_mcu_set_chctx(mvif->phy->mt76, &mconf->mt76, 1826 link_conf, ctx); 1827 } 1828 } 1829 1830 mt792x_mutex_release(phy->dev); 1831 } 1832 1833 static void mt7925_mgd_prepare_tx(struct ieee80211_hw *hw, 1834 struct ieee80211_vif *vif, 1835 struct ieee80211_prep_tx_info *info) 1836 { 1837 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1838 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1839 u16 duration = info->duration ? info->duration : 1840 jiffies_to_msecs(HZ); 1841 1842 mt792x_mutex_acquire(dev); 1843 mt7925_set_roc(mvif->phy, &mvif->bss_conf, 1844 mvif->bss_conf.mt76.ctx->def.chan, duration, 1845 MT7925_ROC_REQ_JOIN); 1846 mt792x_mutex_release(dev); 1847 } 1848 1849 static void mt7925_mgd_complete_tx(struct ieee80211_hw *hw, 1850 struct ieee80211_vif *vif, 1851 struct ieee80211_prep_tx_info *info) 1852 { 1853 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1854 1855 mt7925_abort_roc(mvif->phy, &mvif->bss_conf); 1856 } 1857 1858 static void mt7925_vif_cfg_changed(struct ieee80211_hw *hw, 1859 struct ieee80211_vif *vif, 1860 u64 changed) 1861 { 1862 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1863 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1864 unsigned long valid = ieee80211_vif_is_mld(vif) ? 1865 mvif->valid_links : BIT(0); 1866 struct ieee80211_bss_conf *bss_conf; 1867 int i; 1868 1869 mt792x_mutex_acquire(dev); 1870 1871 if (changed & BSS_CHANGED_ASSOC) { 1872 mt7925_mcu_sta_update(dev, NULL, vif, true, 1873 MT76_STA_INFO_STATE_ASSOC); 1874 mt7925_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc); 1875 } 1876 1877 if (changed & BSS_CHANGED_ARP_FILTER) { 1878 for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) { 1879 bss_conf = mt792x_vif_to_bss_conf(vif, i); 1880 mt7925_mcu_update_arp_filter(&dev->mt76, bss_conf); 1881 } 1882 } 1883 1884 if (changed & BSS_CHANGED_PS) { 1885 for_each_set_bit(i, &valid, IEEE80211_MLD_MAX_NUM_LINKS) { 1886 bss_conf = mt792x_vif_to_bss_conf(vif, i); 1887 mt7925_mcu_uni_bss_ps(dev, bss_conf); 1888 } 1889 } 1890 1891 mt792x_mutex_release(dev); 1892 } 1893 1894 static void mt7925_link_info_changed(struct ieee80211_hw *hw, 1895 struct ieee80211_vif *vif, 1896 struct ieee80211_bss_conf *info, 1897 u64 changed) 1898 { 1899 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1900 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1901 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1902 struct mt792x_bss_conf *mconf; 1903 1904 mconf = mt792x_vif_to_link(mvif, info->link_id); 1905 1906 mt792x_mutex_acquire(dev); 1907 1908 if (changed & BSS_CHANGED_ERP_SLOT) { 1909 int slottime = info->use_short_slot ? 9 : 20; 1910 1911 if (slottime != phy->slottime) { 1912 phy->slottime = slottime; 1913 mt7925_mcu_set_timing(phy, info); 1914 } 1915 } 1916 1917 if (changed & BSS_CHANGED_MCAST_RATE) 1918 mconf->mt76.mcast_rates_idx = 1919 mt7925_get_rates_table(hw, vif, false, true); 1920 1921 if (changed & BSS_CHANGED_BASIC_RATES) 1922 mconf->mt76.basic_rates_idx = 1923 mt7925_get_rates_table(hw, vif, false, false); 1924 1925 if (changed & (BSS_CHANGED_BEACON | 1926 BSS_CHANGED_BEACON_ENABLED)) { 1927 mconf->mt76.beacon_rates_idx = 1928 mt7925_get_rates_table(hw, vif, true, false); 1929 1930 mt7925_mcu_uni_add_beacon_offload(dev, hw, vif, 1931 info->enable_beacon); 1932 } 1933 1934 /* ensure that enable txcmd_mode after bss_info */ 1935 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED)) 1936 mt7925_mcu_set_tx(dev, info); 1937 1938 if (changed & BSS_CHANGED_BSSID) { 1939 if (ieee80211_vif_is_mld(vif) && 1940 hweight16(mvif->valid_links) == 2) 1941 /* Indicate the secondary setup done */ 1942 mt7925_mcu_uni_bss_bcnft(dev, info, true); 1943 } 1944 1945 mt792x_mutex_release(dev); 1946 } 1947 1948 static int 1949 mt7925_change_vif_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1950 u16 old_links, u16 new_links, 1951 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS]) 1952 { 1953 struct mt792x_bss_conf *mconfs[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *mconf; 1954 struct mt792x_link_sta *mlinks[IEEE80211_MLD_MAX_NUM_LINKS] = {}, *mlink; 1955 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1956 unsigned long add = new_links & ~old_links; 1957 unsigned long rem = old_links & ~new_links; 1958 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1959 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1960 struct ieee80211_bss_conf *link_conf; 1961 unsigned int link_id; 1962 int err; 1963 1964 if (old_links == new_links) 1965 return 0; 1966 1967 mt792x_mutex_acquire(dev); 1968 1969 for_each_set_bit(link_id, &rem, IEEE80211_MLD_MAX_NUM_LINKS) { 1970 mconf = mt792x_vif_to_link(mvif, link_id); 1971 mlink = mt792x_sta_to_link(&mvif->sta, link_id); 1972 1973 if (!mconf || !mlink) 1974 continue; 1975 1976 if (mconf != &mvif->bss_conf) { 1977 mt792x_mac_link_bss_remove(dev, mconf, mlink); 1978 devm_kfree(dev->mt76.dev, mconf); 1979 devm_kfree(dev->mt76.dev, mlink); 1980 } 1981 1982 rcu_assign_pointer(mvif->link_conf[link_id], NULL); 1983 rcu_assign_pointer(mvif->sta.link[link_id], NULL); 1984 } 1985 1986 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 1987 if (!old_links) { 1988 mvif->deflink_id = link_id; 1989 mconf = &mvif->bss_conf; 1990 mlink = &mvif->sta.deflink; 1991 } else { 1992 mconf = devm_kzalloc(dev->mt76.dev, sizeof(*mconf), 1993 GFP_KERNEL); 1994 mlink = devm_kzalloc(dev->mt76.dev, sizeof(*mlink), 1995 GFP_KERNEL); 1996 if (!mconf || !mlink) 1997 return -ENOMEM; 1998 } 1999 2000 mconfs[link_id] = mconf; 2001 mlinks[link_id] = mlink; 2002 mconf->link_id = link_id; 2003 mconf->vif = mvif; 2004 mlink->wcid.link_id = link_id; 2005 mlink->wcid.link_valid = !!vif->valid_links; 2006 mlink->wcid.def_wcid = &mvif->sta.deflink.wcid; 2007 } 2008 2009 if (hweight16(mvif->valid_links) == 0) 2010 mt792x_mac_link_bss_remove(dev, &mvif->bss_conf, 2011 &mvif->sta.deflink); 2012 2013 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 2014 mconf = mconfs[link_id]; 2015 mlink = mlinks[link_id]; 2016 link_conf = mt792x_vif_to_bss_conf(vif, link_id); 2017 2018 rcu_assign_pointer(mvif->link_conf[link_id], mconf); 2019 rcu_assign_pointer(mvif->sta.link[link_id], mlink); 2020 2021 err = mt7925_mac_link_bss_add(dev, link_conf, mlink); 2022 if (err < 0) 2023 goto free; 2024 2025 if (mconf != &mvif->bss_conf) { 2026 mt7925_mcu_set_bss_pm(dev, link_conf, true); 2027 2028 err = mt7925_set_mlo_roc(phy, &mvif->bss_conf, 2029 vif->active_links); 2030 if (err < 0) 2031 goto free; 2032 } 2033 } 2034 2035 mvif->valid_links = new_links; 2036 2037 mt792x_mutex_release(dev); 2038 2039 return 0; 2040 2041 free: 2042 for_each_set_bit(link_id, &add, IEEE80211_MLD_MAX_NUM_LINKS) { 2043 rcu_assign_pointer(mvif->link_conf[link_id], NULL); 2044 rcu_assign_pointer(mvif->sta.link[link_id], NULL); 2045 2046 if (mconf != &mvif->bss_conf) 2047 devm_kfree(dev->mt76.dev, mconfs[link_id]); 2048 if (mlink != &mvif->sta.deflink) 2049 devm_kfree(dev->mt76.dev, mlinks[link_id]); 2050 } 2051 2052 mt792x_mutex_release(dev); 2053 2054 return err; 2055 } 2056 2057 static int 2058 mt7925_change_sta_links(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 2059 struct ieee80211_sta *sta, u16 old_links, u16 new_links) 2060 { 2061 unsigned long add = new_links & ~old_links; 2062 unsigned long rem = old_links & ~new_links; 2063 struct mt792x_dev *dev = mt792x_hw_dev(hw); 2064 int err = 0; 2065 2066 if (old_links == new_links) 2067 return 0; 2068 2069 mt792x_mutex_acquire(dev); 2070 2071 err = mt7925_mac_sta_remove_links(dev, vif, sta, rem); 2072 if (err < 0) 2073 goto out; 2074 2075 err = mt7925_mac_sta_add_links(dev, vif, sta, add); 2076 if (err < 0) 2077 goto out; 2078 2079 out: 2080 mt792x_mutex_release(dev); 2081 2082 return err; 2083 } 2084 2085 static int mt7925_assign_vif_chanctx(struct ieee80211_hw *hw, 2086 struct ieee80211_vif *vif, 2087 struct ieee80211_bss_conf *link_conf, 2088 struct ieee80211_chanctx_conf *ctx) 2089 { 2090 struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv; 2091 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 2092 struct mt792x_dev *dev = mt792x_hw_dev(hw); 2093 struct ieee80211_bss_conf *pri_link_conf; 2094 struct mt792x_bss_conf *mconf; 2095 2096 mutex_lock(&dev->mt76.mutex); 2097 2098 if (ieee80211_vif_is_mld(vif)) { 2099 mconf = mt792x_vif_to_link(mvif, link_conf->link_id); 2100 pri_link_conf = mt792x_vif_to_bss_conf(vif, mvif->deflink_id); 2101 2102 if (vif->type == NL80211_IFTYPE_STATION && 2103 mconf == &mvif->bss_conf) 2104 mt7925_mcu_add_bss_info(&dev->phy, NULL, pri_link_conf, 2105 NULL, true); 2106 } else { 2107 mconf = &mvif->bss_conf; 2108 } 2109 2110 mconf->mt76.ctx = ctx; 2111 mctx->bss_conf = mconf; 2112 mutex_unlock(&dev->mt76.mutex); 2113 2114 return 0; 2115 } 2116 2117 static void mt7925_unassign_vif_chanctx(struct ieee80211_hw *hw, 2118 struct ieee80211_vif *vif, 2119 struct ieee80211_bss_conf *link_conf, 2120 struct ieee80211_chanctx_conf *ctx) 2121 { 2122 struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv; 2123 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 2124 struct mt792x_dev *dev = mt792x_hw_dev(hw); 2125 struct mt792x_bss_conf *mconf; 2126 2127 mutex_lock(&dev->mt76.mutex); 2128 2129 if (ieee80211_vif_is_mld(vif)) { 2130 mconf = mt792x_vif_to_link(mvif, link_conf->link_id); 2131 2132 if (vif->type == NL80211_IFTYPE_STATION && 2133 mconf == &mvif->bss_conf) 2134 mt7925_mcu_add_bss_info(&dev->phy, NULL, link_conf, 2135 NULL, false); 2136 } else { 2137 mconf = &mvif->bss_conf; 2138 } 2139 2140 mctx->bss_conf = NULL; 2141 mconf->mt76.ctx = NULL; 2142 mutex_unlock(&dev->mt76.mutex); 2143 } 2144 2145 const struct ieee80211_ops mt7925_ops = { 2146 .tx = mt792x_tx, 2147 .start = mt7925_start, 2148 .stop = mt792x_stop, 2149 .add_interface = mt7925_add_interface, 2150 .remove_interface = mt792x_remove_interface, 2151 .config = mt7925_config, 2152 .conf_tx = mt7925_conf_tx, 2153 .configure_filter = mt7925_configure_filter, 2154 .start_ap = mt7925_start_ap, 2155 .stop_ap = mt7925_stop_ap, 2156 .sta_state = mt76_sta_state, 2157 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 2158 .set_key = mt7925_set_key, 2159 .sta_set_decap_offload = mt7925_sta_set_decap_offload, 2160 #if IS_ENABLED(CONFIG_IPV6) 2161 .ipv6_addr_change = mt7925_ipv6_addr_change, 2162 #endif /* CONFIG_IPV6 */ 2163 .ampdu_action = mt7925_ampdu_action, 2164 .set_rts_threshold = mt7925_set_rts_threshold, 2165 .wake_tx_queue = mt76_wake_tx_queue, 2166 .release_buffered_frames = mt76_release_buffered_frames, 2167 .channel_switch_beacon = mt7925_channel_switch_beacon, 2168 .get_txpower = mt76_get_txpower, 2169 .get_stats = mt792x_get_stats, 2170 .get_et_sset_count = mt792x_get_et_sset_count, 2171 .get_et_strings = mt792x_get_et_strings, 2172 .get_et_stats = mt792x_get_et_stats, 2173 .get_tsf = mt792x_get_tsf, 2174 .set_tsf = mt792x_set_tsf, 2175 .get_survey = mt76_get_survey, 2176 .get_antenna = mt76_get_antenna, 2177 .set_antenna = mt7925_set_antenna, 2178 .set_coverage_class = mt792x_set_coverage_class, 2179 .hw_scan = mt7925_hw_scan, 2180 .cancel_hw_scan = mt7925_cancel_hw_scan, 2181 .sta_statistics = mt792x_sta_statistics, 2182 .sched_scan_start = mt7925_start_sched_scan, 2183 .sched_scan_stop = mt7925_stop_sched_scan, 2184 #ifdef CONFIG_PM 2185 .suspend = mt7925_suspend, 2186 .resume = mt7925_resume, 2187 .set_wakeup = mt792x_set_wakeup, 2188 .set_rekey_data = mt7925_set_rekey_data, 2189 #endif /* CONFIG_PM */ 2190 .flush = mt792x_flush, 2191 .set_sar_specs = mt7925_set_sar_specs, 2192 .remain_on_channel = mt7925_remain_on_channel, 2193 .cancel_remain_on_channel = mt7925_cancel_remain_on_channel, 2194 .add_chanctx = mt7925_add_chanctx, 2195 .remove_chanctx = mt7925_remove_chanctx, 2196 .change_chanctx = mt7925_change_chanctx, 2197 .assign_vif_chanctx = mt7925_assign_vif_chanctx, 2198 .unassign_vif_chanctx = mt7925_unassign_vif_chanctx, 2199 .mgd_prepare_tx = mt7925_mgd_prepare_tx, 2200 .mgd_complete_tx = mt7925_mgd_complete_tx, 2201 .vif_cfg_changed = mt7925_vif_cfg_changed, 2202 .link_info_changed = mt7925_link_info_changed, 2203 .change_vif_links = mt7925_change_vif_links, 2204 .change_sta_links = mt7925_change_sta_links, 2205 }; 2206 EXPORT_SYMBOL_GPL(mt7925_ops); 2207 2208 MODULE_AUTHOR("Deren Wu <deren.wu@mediatek.com>"); 2209 MODULE_DESCRIPTION("MediaTek MT7925 core driver"); 2210 MODULE_LICENSE("Dual BSD/GPL"); 2211