1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* Copyright (C) 2020 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 <net/ipv6.h> 9 #include "mt7921.h" 10 #include "regd.h" 11 #include "mcu.h" 12 13 static int 14 mt7921_init_he_caps(struct mt792x_phy *phy, enum nl80211_band band, 15 struct ieee80211_sband_iftype_data *data) 16 { 17 int i, idx = 0; 18 int nss = hweight8(phy->mt76->chainmask); 19 u16 mcs_map = 0; 20 21 for (i = 0; i < 8; i++) { 22 if (i < nss) 23 mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2)); 24 else 25 mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2)); 26 } 27 28 for (i = 0; i < NUM_NL80211_IFTYPES; i++) { 29 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap; 30 struct ieee80211_he_cap_elem *he_cap_elem = 31 &he_cap->he_cap_elem; 32 struct ieee80211_he_mcs_nss_supp *he_mcs = 33 &he_cap->he_mcs_nss_supp; 34 35 switch (i) { 36 case NL80211_IFTYPE_STATION: 37 case NL80211_IFTYPE_AP: 38 break; 39 default: 40 continue; 41 } 42 43 data[idx].types_mask = BIT(i); 44 he_cap->has_he = true; 45 46 he_cap_elem->mac_cap_info[0] = 47 IEEE80211_HE_MAC_CAP0_HTC_HE; 48 he_cap_elem->mac_cap_info[3] = 49 IEEE80211_HE_MAC_CAP3_OMI_CONTROL | 50 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_EXT_3; 51 he_cap_elem->mac_cap_info[4] = 52 IEEE80211_HE_MAC_CAP4_AMSDU_IN_AMPDU; 53 54 if (band == NL80211_BAND_2GHZ) 55 he_cap_elem->phy_cap_info[0] = 56 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G; 57 else 58 he_cap_elem->phy_cap_info[0] = 59 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G; 60 61 he_cap_elem->phy_cap_info[1] = 62 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD; 63 he_cap_elem->phy_cap_info[2] = 64 IEEE80211_HE_PHY_CAP2_NDP_4x_LTF_AND_3_2US | 65 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ | 66 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ | 67 IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO | 68 IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO; 69 70 switch (i) { 71 case NL80211_IFTYPE_AP: 72 he_cap_elem->mac_cap_info[2] |= 73 IEEE80211_HE_MAC_CAP2_BSR; 74 he_cap_elem->mac_cap_info[4] |= 75 IEEE80211_HE_MAC_CAP4_BQR; 76 he_cap_elem->mac_cap_info[5] |= 77 IEEE80211_HE_MAC_CAP5_OM_CTRL_UL_MU_DATA_DIS_RX; 78 he_cap_elem->phy_cap_info[3] |= 79 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK | 80 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK; 81 he_cap_elem->phy_cap_info[6] |= 82 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | 83 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT; 84 he_cap_elem->phy_cap_info[9] |= 85 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU | 86 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU; 87 88 if (is_mt7922(phy->mt76->dev)) { 89 he_cap_elem->phy_cap_info[0] |= 90 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 91 } 92 break; 93 case NL80211_IFTYPE_STATION: 94 he_cap_elem->mac_cap_info[1] |= 95 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US; 96 97 if (band == NL80211_BAND_2GHZ) 98 he_cap_elem->phy_cap_info[0] |= 99 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G; 100 else 101 he_cap_elem->phy_cap_info[0] |= 102 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G; 103 104 he_cap_elem->phy_cap_info[1] |= 105 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A | 106 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US; 107 he_cap_elem->phy_cap_info[3] |= 108 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK | 109 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK; 110 he_cap_elem->phy_cap_info[4] |= 111 IEEE80211_HE_PHY_CAP4_SU_BEAMFORMEE | 112 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_4; 113 he_cap_elem->phy_cap_info[5] |= 114 IEEE80211_HE_PHY_CAP5_NG16_SU_FEEDBACK | 115 IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK; 116 he_cap_elem->phy_cap_info[6] |= 117 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_42_SU | 118 IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU | 119 IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB | 120 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | 121 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT; 122 he_cap_elem->phy_cap_info[7] |= 123 IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP | 124 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI; 125 he_cap_elem->phy_cap_info[8] |= 126 IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G | 127 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484; 128 he_cap_elem->phy_cap_info[9] |= 129 IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM | 130 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK | 131 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU | 132 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU | 133 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB | 134 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB; 135 136 if (is_mt7922(phy->mt76->dev)) { 137 he_cap_elem->phy_cap_info[0] |= 138 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 139 he_cap_elem->phy_cap_info[4] |= 140 IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_4; 141 he_cap_elem->phy_cap_info[8] |= 142 IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU | 143 IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU; 144 } 145 break; 146 } 147 148 he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map); 149 he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map); 150 if (is_mt7922(phy->mt76->dev)) { 151 he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map); 152 he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map); 153 } 154 155 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres)); 156 if (he_cap_elem->phy_cap_info[6] & 157 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) { 158 mt76_connac_gen_ppe_thresh(he_cap->ppe_thres, nss, band); 159 } else { 160 he_cap_elem->phy_cap_info[9] |= 161 u8_encode_bits(IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_16US, 162 IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK); 163 } 164 165 if (band == NL80211_BAND_6GHZ) { 166 struct ieee80211_supported_band *sband = 167 &phy->mt76->sband_5g.sband; 168 struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap; 169 struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap; 170 u32 exp; 171 u16 cap; 172 173 cap = u16_encode_bits(ht_cap->ampdu_density, 174 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START); 175 exp = u32_get_bits(vht_cap->cap, 176 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK); 177 cap |= u16_encode_bits(exp, 178 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP); 179 exp = u32_get_bits(vht_cap->cap, 180 IEEE80211_VHT_CAP_MAX_MPDU_MASK); 181 cap |= u16_encode_bits(exp, 182 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN); 183 if (vht_cap->cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN) 184 cap |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS; 185 if (vht_cap->cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN) 186 cap |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS; 187 188 data[idx].he_6ghz_capa.capa = cpu_to_le16(cap); 189 } 190 idx++; 191 } 192 193 return idx; 194 } 195 196 void mt7921_set_stream_he_caps(struct mt792x_phy *phy) 197 { 198 struct ieee80211_sband_iftype_data *data; 199 struct ieee80211_supported_band *band; 200 int n; 201 202 if (phy->mt76->cap.has_2ghz) { 203 data = phy->iftype[NL80211_BAND_2GHZ]; 204 n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data); 205 206 band = &phy->mt76->sband_2g.sband; 207 _ieee80211_set_sband_iftype_data(band, data, n); 208 } 209 210 if (phy->mt76->cap.has_5ghz) { 211 data = phy->iftype[NL80211_BAND_5GHZ]; 212 n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data); 213 214 band = &phy->mt76->sband_5g.sband; 215 _ieee80211_set_sband_iftype_data(band, data, n); 216 217 if (phy->mt76->cap.has_6ghz) { 218 data = phy->iftype[NL80211_BAND_6GHZ]; 219 n = mt7921_init_he_caps(phy, NL80211_BAND_6GHZ, data); 220 221 band = &phy->mt76->sband_6g.sband; 222 _ieee80211_set_sband_iftype_data(band, data, n); 223 } 224 } 225 } 226 227 int __mt7921_start(struct mt792x_phy *phy) 228 { 229 struct mt76_phy *mphy = phy->mt76; 230 int err; 231 232 err = mt76_connac_mcu_set_mac_enable(mphy->dev, 0, true, false); 233 if (err) 234 return err; 235 236 err = mt76_connac_mcu_set_channel_domain(mphy); 237 if (err) 238 return err; 239 240 err = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH)); 241 if (err) 242 return err; 243 244 err = mt7921_set_tx_sar_pwr(mphy->hw, NULL); 245 if (err) 246 return err; 247 248 mt792x_mac_reset_counters(phy); 249 set_bit(MT76_STATE_RUNNING, &mphy->state); 250 251 ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work, 252 MT792x_WATCHDOG_TIME); 253 if (mt76_is_mmio(mphy->dev)) { 254 err = mt7921_mcu_radio_led_ctrl(phy->dev, EXT_CMD_RADIO_LED_CTRL_ENABLE); 255 if (err) 256 return err; 257 258 err = mt7921_mcu_radio_led_ctrl(phy->dev, EXT_CMD_RADIO_ON_LED); 259 if (err) 260 return err; 261 } 262 263 if (phy->chip_cap & MT792x_CHIP_CAP_WF_RF_PIN_CTRL_EVT_EN) { 264 mt7921_mcu_wf_rf_pin_ctrl(phy, WF_RF_PIN_INIT); 265 wiphy_rfkill_start_polling(mphy->hw->wiphy); 266 } 267 268 return 0; 269 } 270 EXPORT_SYMBOL_GPL(__mt7921_start); 271 272 static int mt7921_start(struct ieee80211_hw *hw) 273 { 274 struct mt792x_phy *phy = mt792x_hw_phy(hw); 275 int err; 276 277 mt792x_mutex_acquire(phy->dev); 278 err = __mt7921_start(phy); 279 mt792x_mutex_release(phy->dev); 280 281 return err; 282 } 283 284 static void mt7921_stop(struct ieee80211_hw *hw, bool suspend) 285 { 286 struct mt792x_dev *dev = mt792x_hw_dev(hw); 287 int err = 0; 288 289 if (mt76_is_mmio(&dev->mt76)) { 290 mt792x_mutex_acquire(dev); 291 err = mt7921_mcu_radio_led_ctrl(dev, EXT_CMD_RADIO_OFF_LED); 292 mt792x_mutex_release(dev); 293 if (err) 294 return; 295 } 296 297 mt792x_stop(hw, false); 298 } 299 300 static int 301 mt7921_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 302 { 303 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 304 struct mt792x_dev *dev = mt792x_hw_dev(hw); 305 struct mt792x_phy *phy = mt792x_hw_phy(hw); 306 struct mt76_txq *mtxq; 307 int idx, ret = 0; 308 309 mt792x_mutex_acquire(dev); 310 311 mvif->bss_conf.mt76.idx = __ffs64(~dev->mt76.vif_mask); 312 if (mvif->bss_conf.mt76.idx >= MT792x_MAX_INTERFACES) { 313 ret = -ENOSPC; 314 goto out; 315 } 316 317 mvif->bss_conf.mt76.omac_idx = mvif->bss_conf.mt76.idx; 318 mvif->phy = phy; 319 mvif->bss_conf.vif = mvif; 320 mvif->bss_conf.mt76.band_idx = 0; 321 mvif->bss_conf.mt76.wmm_idx = mvif->bss_conf.mt76.idx % MT76_CONNAC_MAX_WMM_SETS; 322 323 ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, &vif->bss_conf, 324 &mvif->bss_conf.mt76, 325 &mvif->sta.deflink.wcid, true); 326 if (ret) 327 goto out; 328 329 dev->mt76.vif_mask |= BIT_ULL(mvif->bss_conf.mt76.idx); 330 phy->omac_mask |= BIT_ULL(mvif->bss_conf.mt76.omac_idx); 331 332 idx = MT792x_WTBL_RESERVED - mvif->bss_conf.mt76.idx; 333 334 INIT_LIST_HEAD(&mvif->sta.deflink.wcid.poll_list); 335 mvif->sta.deflink.wcid.idx = idx; 336 mvif->sta.deflink.wcid.tx_info |= MT_WCID_TX_INFO_SET; 337 mt76_wcid_init(&mvif->sta.deflink.wcid, mvif->bss_conf.mt76.band_idx); 338 339 mt7921_mac_wtbl_update(dev, idx, 340 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 341 342 ewma_rssi_init(&mvif->bss_conf.rssi); 343 344 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.deflink.wcid); 345 if (vif->txq) { 346 mtxq = (struct mt76_txq *)vif->txq->drv_priv; 347 mtxq->wcid = idx; 348 } 349 350 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER; 351 if (phy->chip_cap & MT792x_CHIP_CAP_RSSI_NOTIFY_EVT_EN) 352 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_CQM_RSSI; 353 354 INIT_WORK(&mvif->csa_work, mt7921_csa_work); 355 timer_setup(&mvif->csa_timer, mt792x_csa_timer, 0); 356 out: 357 mt792x_mutex_release(dev); 358 359 return ret; 360 } 361 362 static void mt7921_roc_iter(void *priv, u8 *mac, 363 struct ieee80211_vif *vif) 364 { 365 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 366 struct mt792x_phy *phy = priv; 367 368 mt7921_mcu_abort_roc(phy, mvif, phy->roc_token_id); 369 } 370 371 void mt7921_roc_abort_sync(struct mt792x_dev *dev) 372 { 373 struct mt792x_phy *phy = &dev->phy; 374 375 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 376 return; 377 378 timer_delete_sync(&phy->roc_timer); 379 cancel_work(&phy->roc_work); 380 381 ieee80211_iterate_interfaces(mt76_hw(dev), 382 IEEE80211_IFACE_ITER_RESUME_ALL, 383 mt7921_roc_iter, (void *)phy); 384 } 385 EXPORT_SYMBOL_GPL(mt7921_roc_abort_sync); 386 387 void mt7921_roc_work(struct work_struct *work) 388 { 389 struct mt792x_phy *phy; 390 391 phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy, 392 roc_work); 393 394 mt792x_mutex_acquire(phy->dev); 395 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) { 396 mt792x_mutex_release(phy->dev); 397 return; 398 } 399 ieee80211_iterate_active_interfaces(phy->mt76->hw, 400 IEEE80211_IFACE_ITER_RESUME_ALL, 401 mt7921_roc_iter, phy); 402 mt792x_mutex_release(phy->dev); 403 ieee80211_remain_on_channel_expired(phy->mt76->hw); 404 } 405 406 static int mt7921_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif) 407 { 408 int err = 0; 409 410 timer_delete_sync(&phy->roc_timer); 411 cancel_work_sync(&phy->roc_work); 412 413 mt792x_mutex_acquire(phy->dev); 414 if (test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state)) 415 err = mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id); 416 mt792x_mutex_release(phy->dev); 417 418 return err; 419 } 420 421 static int mt7921_set_roc(struct mt792x_phy *phy, 422 struct mt792x_vif *vif, 423 struct ieee80211_channel *chan, 424 int duration, 425 enum mt7921_roc_req type) 426 { 427 int err; 428 429 if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state)) 430 return -EBUSY; 431 432 phy->roc_grant = false; 433 434 err = mt7921_mcu_set_roc(phy, vif, chan, duration, type, 435 ++phy->roc_token_id); 436 if (err < 0) { 437 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 438 goto out; 439 } 440 441 if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) { 442 mt7921_mcu_abort_roc(phy, vif, phy->roc_token_id); 443 clear_bit(MT76_STATE_ROC, &phy->mt76->state); 444 err = -ETIMEDOUT; 445 } 446 447 out: 448 return err; 449 } 450 451 static int mt7921_remain_on_channel(struct ieee80211_hw *hw, 452 struct ieee80211_vif *vif, 453 struct ieee80211_channel *chan, 454 int duration, 455 enum ieee80211_roc_type type) 456 { 457 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 458 struct mt792x_phy *phy = mt792x_hw_phy(hw); 459 int err; 460 461 mt792x_mutex_acquire(phy->dev); 462 err = mt7921_set_roc(phy, mvif, chan, duration, MT7921_ROC_REQ_ROC); 463 mt792x_mutex_release(phy->dev); 464 465 return err; 466 } 467 468 static int mt7921_cancel_remain_on_channel(struct ieee80211_hw *hw, 469 struct ieee80211_vif *vif) 470 { 471 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 472 struct mt792x_phy *phy = mt792x_hw_phy(hw); 473 474 return mt7921_abort_roc(phy, mvif); 475 } 476 477 int mt7921_set_channel(struct mt76_phy *mphy) 478 { 479 struct mt792x_phy *phy = mphy->priv; 480 struct mt792x_dev *dev = phy->dev; 481 int ret; 482 483 mt76_connac_pm_wake(mphy, &dev->pm); 484 ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH)); 485 if (ret) 486 goto out; 487 488 mt792x_mac_set_timeing(phy); 489 mt792x_mac_reset_counters(phy); 490 phy->noise = 0; 491 492 out: 493 mt76_connac_power_save_sched(mphy, &dev->pm); 494 495 ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work, 496 MT792x_WATCHDOG_TIME); 497 498 return ret; 499 } 500 EXPORT_SYMBOL_GPL(mt7921_set_channel); 501 502 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 503 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 504 struct ieee80211_key_conf *key) 505 { 506 struct mt792x_dev *dev = mt792x_hw_dev(hw); 507 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 508 struct mt792x_sta *msta = sta ? (struct mt792x_sta *)sta->drv_priv : 509 &mvif->sta; 510 struct mt76_wcid *wcid = &msta->deflink.wcid; 511 u8 *wcid_keyidx = &wcid->hw_key_idx; 512 int idx = key->keyidx, err = 0; 513 514 /* The hardware does not support per-STA RX GTK, fallback 515 * to software mode for these. 516 */ 517 if ((vif->type == NL80211_IFTYPE_ADHOC || 518 vif->type == NL80211_IFTYPE_MESH_POINT) && 519 (key->cipher == WLAN_CIPHER_SUITE_TKIP || 520 key->cipher == WLAN_CIPHER_SUITE_CCMP) && 521 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) 522 return -EOPNOTSUPP; 523 524 /* fall back to sw encryption for unsupported ciphers */ 525 switch (key->cipher) { 526 case WLAN_CIPHER_SUITE_AES_CMAC: 527 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE; 528 wcid_keyidx = &wcid->hw_key_idx2; 529 break; 530 case WLAN_CIPHER_SUITE_WEP40: 531 case WLAN_CIPHER_SUITE_WEP104: 532 if (!mvif->wep_sta) 533 return -EOPNOTSUPP; 534 break; 535 case WLAN_CIPHER_SUITE_TKIP: 536 case WLAN_CIPHER_SUITE_CCMP: 537 case WLAN_CIPHER_SUITE_CCMP_256: 538 case WLAN_CIPHER_SUITE_GCMP: 539 case WLAN_CIPHER_SUITE_GCMP_256: 540 case WLAN_CIPHER_SUITE_SMS4: 541 break; 542 default: 543 return -EOPNOTSUPP; 544 } 545 546 mt792x_mutex_acquire(dev); 547 548 if (cmd == SET_KEY) { 549 *wcid_keyidx = idx; 550 } else { 551 if (idx == *wcid_keyidx) 552 *wcid_keyidx = -1; 553 554 /* For security issue we don't trigger the key deletion when 555 * reassociating. But we should trigger the deletion process 556 * to avoid using incorrect cipher after disconnection, 557 */ 558 if (vif->type != NL80211_IFTYPE_STATION || vif->cfg.assoc) 559 goto out; 560 } 561 562 mt76_wcid_key_setup(&dev->mt76, wcid, key); 563 err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->deflink.bip, 564 key, MCU_UNI_CMD(STA_REC_UPDATE), 565 &msta->deflink.wcid, cmd); 566 if (err) 567 goto out; 568 569 if (key->cipher == WLAN_CIPHER_SUITE_WEP104 || 570 key->cipher == WLAN_CIPHER_SUITE_WEP40) 571 err = mt76_connac_mcu_add_key(&dev->mt76, vif, 572 &mvif->wep_sta->deflink.bip, 573 key, MCU_UNI_CMD(STA_REC_UPDATE), 574 &mvif->wep_sta->deflink.wcid, cmd); 575 out: 576 mt792x_mutex_release(dev); 577 578 return err; 579 } 580 581 static void 582 mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif) 583 { 584 struct mt792x_dev *dev = priv; 585 struct ieee80211_hw *hw = mt76_hw(dev); 586 bool pm_enable = dev->pm.enable; 587 int err; 588 589 err = mt7921_mcu_set_beacon_filter(dev, vif, pm_enable); 590 if (err < 0) 591 return; 592 593 if (pm_enable) { 594 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER; 595 ieee80211_hw_set(hw, CONNECTION_MONITOR); 596 } else { 597 vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER; 598 __clear_bit(IEEE80211_HW_CONNECTION_MONITOR, hw->flags); 599 } 600 } 601 602 static void 603 mt7921_sniffer_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif) 604 { 605 struct mt792x_dev *dev = priv; 606 struct ieee80211_hw *hw = mt76_hw(dev); 607 struct mt76_connac_pm *pm = &dev->pm; 608 bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR); 609 610 mt7921_mcu_set_sniffer(dev, vif, monitor); 611 pm->enable = pm->enable_user && !monitor; 612 pm->ds_enable = pm->ds_enable_user && !monitor; 613 614 mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable); 615 616 if (monitor) 617 mt7921_mcu_set_beacon_filter(dev, vif, false); 618 } 619 620 void mt7921_set_runtime_pm(struct mt792x_dev *dev) 621 { 622 struct ieee80211_hw *hw = mt76_hw(dev); 623 struct mt76_connac_pm *pm = &dev->pm; 624 bool monitor = !!(hw->conf.flags & IEEE80211_CONF_MONITOR); 625 626 pm->enable = pm->enable_user && !monitor; 627 ieee80211_iterate_active_interfaces(hw, 628 IEEE80211_IFACE_ITER_RESUME_ALL, 629 mt7921_pm_interface_iter, dev); 630 pm->ds_enable = pm->ds_enable_user && !monitor; 631 mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable); 632 } 633 634 static int mt7921_config(struct ieee80211_hw *hw, int radio_idx, u32 changed) 635 { 636 struct mt792x_dev *dev = mt792x_hw_dev(hw); 637 struct mt792x_phy *phy = mt792x_hw_phy(hw); 638 int ret = 0; 639 640 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) { 641 ret = mt76_update_channel(phy->mt76); 642 if (ret) 643 return ret; 644 } 645 646 mt792x_mutex_acquire(dev); 647 648 if (changed & IEEE80211_CONF_CHANGE_POWER) { 649 ret = mt7921_set_tx_sar_pwr(hw, NULL); 650 if (ret) 651 goto out; 652 } 653 654 if (changed & IEEE80211_CONF_CHANGE_MONITOR) { 655 ieee80211_iterate_active_interfaces_mtx(hw, 656 IEEE80211_IFACE_ITER_RESUME_ALL, 657 mt7921_sniffer_interface_iter, dev); 658 } 659 660 out: 661 mt792x_mutex_release(dev); 662 663 return ret; 664 } 665 666 static void mt7921_configure_filter(struct ieee80211_hw *hw, 667 unsigned int changed_flags, 668 unsigned int *total_flags, 669 u64 multicast) 670 { 671 struct mt792x_dev *dev = mt792x_hw_dev(hw); 672 struct mt792x_phy *phy = mt792x_hw_phy(hw); 673 u32 flags = MT7921_FILTER_ENABLE; 674 675 #define MT7921_FILTER(_fif, _type) do { \ 676 if (*total_flags & (_fif)) \ 677 flags |= MT7921_FILTER_##_type; \ 678 } while (0) 679 680 MT7921_FILTER(FIF_FCSFAIL, FCSFAIL); 681 MT7921_FILTER(FIF_CONTROL, CONTROL); 682 MT7921_FILTER(FIF_OTHER_BSS, OTHER_BSS); 683 684 phy->rxfilter = flags; 685 686 mt792x_mutex_acquire(dev); 687 mt7921_mcu_set_rxfilter(dev, flags, 0, 0); 688 mt792x_mutex_release(dev); 689 690 *total_flags &= (FIF_OTHER_BSS | FIF_FCSFAIL | FIF_CONTROL); 691 } 692 693 static void mt7921_bss_info_changed(struct ieee80211_hw *hw, 694 struct ieee80211_vif *vif, 695 struct ieee80211_bss_conf *info, 696 u64 changed) 697 { 698 struct mt792x_phy *phy = mt792x_hw_phy(hw); 699 struct mt792x_dev *dev = mt792x_hw_dev(hw); 700 701 mt792x_mutex_acquire(dev); 702 703 if (changed & BSS_CHANGED_ERP_SLOT) { 704 int slottime = info->use_short_slot ? 9 : 20; 705 706 if (slottime != phy->slottime) { 707 phy->slottime = slottime; 708 mt792x_mac_set_timeing(phy); 709 } 710 } 711 712 if (changed & (BSS_CHANGED_BEACON | 713 BSS_CHANGED_BEACON_ENABLED)) 714 mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, 715 info->enable_beacon); 716 717 /* ensure that enable txcmd_mode after bss_info */ 718 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED)) 719 mt7921_mcu_set_tx(dev, vif); 720 721 if (changed & BSS_CHANGED_PS) 722 mt7921_mcu_uni_bss_ps(dev, vif); 723 724 if (changed & BSS_CHANGED_CQM) 725 mt7921_mcu_set_rssimonitor(dev, vif); 726 727 if (changed & BSS_CHANGED_ASSOC) { 728 mt7921_mcu_sta_update(dev, NULL, vif, true, 729 MT76_STA_INFO_STATE_ASSOC); 730 mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc); 731 } 732 733 if (changed & BSS_CHANGED_ARP_FILTER) { 734 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 735 736 mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->bss_conf.mt76, 737 info); 738 } 739 740 mt792x_mutex_release(dev); 741 } 742 743 static void 744 mt7921_calc_vif_num(void *priv, u8 *mac, struct ieee80211_vif *vif) 745 { 746 u32 *num = priv; 747 748 if (!priv) 749 return; 750 751 switch (vif->type) { 752 case NL80211_IFTYPE_STATION: 753 case NL80211_IFTYPE_P2P_CLIENT: 754 case NL80211_IFTYPE_AP: 755 case NL80211_IFTYPE_P2P_GO: 756 *num += 1; 757 break; 758 default: 759 break; 760 } 761 } 762 763 static void 764 mt7921_regd_set_6ghz_power_type(struct ieee80211_vif *vif, bool is_add) 765 { 766 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 767 struct mt792x_phy *phy = mvif->phy; 768 struct mt792x_dev *dev = phy->dev; 769 u32 valid_vif_num = 0; 770 771 ieee80211_iterate_active_interfaces(mt76_hw(dev), 772 IEEE80211_IFACE_ITER_RESUME_ALL, 773 mt7921_calc_vif_num, &valid_vif_num); 774 775 if (valid_vif_num > 1) { 776 phy->power_type = MT_AP_DEFAULT; 777 goto out; 778 } 779 780 if (!is_add) 781 vif->bss_conf.power_type = IEEE80211_REG_UNSET_AP; 782 783 switch (vif->bss_conf.power_type) { 784 case IEEE80211_REG_SP_AP: 785 phy->power_type = MT_AP_SP; 786 break; 787 case IEEE80211_REG_VLP_AP: 788 phy->power_type = MT_AP_VLP; 789 break; 790 case IEEE80211_REG_LPI_AP: 791 phy->power_type = MT_AP_LPI; 792 break; 793 case IEEE80211_REG_UNSET_AP: 794 phy->power_type = MT_AP_UNSET; 795 break; 796 default: 797 phy->power_type = MT_AP_DEFAULT; 798 break; 799 } 800 801 out: 802 if (vif->bss_conf.chanreq.oper.chan->band == NL80211_BAND_6GHZ) 803 __mt7921_mcu_regd_update(dev, dev->mt76.alpha2, dev->country_ie_env); 804 } 805 806 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif, 807 struct ieee80211_sta *sta) 808 { 809 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 810 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 811 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 812 int ret, idx; 813 814 if (sta->aid > MT7921_MAX_AID) 815 return -ENOENT; 816 817 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT792x_WTBL_STA - 1); 818 if (idx < 0) 819 return -ENOSPC; 820 821 INIT_LIST_HEAD(&msta->deflink.wcid.poll_list); 822 msta->vif = mvif; 823 msta->deflink.wcid.sta = 1; 824 msta->deflink.wcid.idx = idx; 825 msta->deflink.wcid.phy_idx = mvif->bss_conf.mt76.band_idx; 826 msta->deflink.wcid.tx_info |= MT_WCID_TX_INFO_SET; 827 msta->deflink.last_txs = jiffies; 828 msta->deflink.sta = msta; 829 830 if (sta->tdls) 831 set_bit(MT_WCID_FLAG_TDLS_PEER, &msta->deflink.wcid.flags); 832 833 ret = mt76_connac_pm_wake(&dev->mphy, &dev->pm); 834 if (ret) 835 return ret; 836 837 if (vif->type == NL80211_IFTYPE_STATION) 838 mvif->wep_sta = msta; 839 840 mt7921_mac_wtbl_update(dev, idx, 841 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 842 843 ret = mt7921_mcu_sta_update(dev, sta, vif, true, 844 MT76_STA_INFO_STATE_NONE); 845 if (ret) 846 return ret; 847 848 mt7921_regd_set_6ghz_power_type(vif, true); 849 850 mt76_connac_power_save_sched(&dev->mphy, &dev->pm); 851 852 return 0; 853 } 854 EXPORT_SYMBOL_GPL(mt7921_mac_sta_add); 855 856 int mt7921_mac_sta_event(struct mt76_dev *mdev, struct ieee80211_vif *vif, 857 struct ieee80211_sta *sta, enum mt76_sta_event ev) 858 { 859 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 860 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 861 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 862 863 if (sta->aid > MT7921_MAX_AID) 864 return -ENOENT; 865 866 if (mvif->roc_join_held && !sta->tdls && 867 (ev == MT76_STA_EVENT_AUTHORIZE || ev == MT76_STA_EVENT_DISASSOC)) { 868 mvif->roc_join_held = false; 869 mt7921_abort_roc(mvif->phy, mvif); 870 } 871 872 if (ev != MT76_STA_EVENT_ASSOC) 873 return 0; 874 875 mt792x_mutex_acquire(dev); 876 877 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) 878 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.deflink.wcid, 879 true, mvif->bss_conf.mt76.ctx); 880 881 ewma_avg_signal_init(&msta->deflink.avg_ack_signal); 882 883 mt7921_mac_wtbl_update(dev, msta->deflink.wcid.idx, 884 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 885 memset(msta->deflink.airtime_ac, 0, sizeof(msta->deflink.airtime_ac)); 886 887 mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC); 888 889 mt792x_mutex_release(dev); 890 891 return 0; 892 } 893 EXPORT_SYMBOL_GPL(mt7921_mac_sta_event); 894 895 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 896 struct ieee80211_sta *sta) 897 { 898 struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev, mt76); 899 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 900 901 mt7921_roc_abort_sync(dev); 902 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->deflink.wcid); 903 mt76_connac_pm_wake(&dev->mphy, &dev->pm); 904 905 mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE); 906 mt7921_mac_wtbl_update(dev, msta->deflink.wcid.idx, 907 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 908 909 if (vif->type == NL80211_IFTYPE_STATION) { 910 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 911 912 mvif->wep_sta = NULL; 913 mvif->roc_join_held = false; 914 ewma_rssi_init(&mvif->bss_conf.rssi); 915 if (!sta->tdls) 916 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, 917 &mvif->sta.deflink.wcid, false, 918 mvif->bss_conf.mt76.ctx); 919 } 920 921 spin_lock_bh(&dev->mt76.sta_poll_lock); 922 if (!list_empty(&msta->deflink.wcid.poll_list)) 923 list_del_init(&msta->deflink.wcid.poll_list); 924 spin_unlock_bh(&dev->mt76.sta_poll_lock); 925 926 mt7921_regd_set_6ghz_power_type(vif, false); 927 928 mt76_connac_power_save_sched(&dev->mphy, &dev->pm); 929 } 930 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove); 931 932 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx, 933 u32 val) 934 { 935 struct mt792x_dev *dev = mt792x_hw_dev(hw); 936 937 mt792x_mutex_acquire(dev); 938 mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0); 939 mt792x_mutex_release(dev); 940 941 return 0; 942 } 943 944 static int 945 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 946 struct ieee80211_ampdu_params *params) 947 { 948 enum ieee80211_ampdu_mlme_action action = params->action; 949 struct mt792x_dev *dev = mt792x_hw_dev(hw); 950 struct ieee80211_sta *sta = params->sta; 951 struct ieee80211_txq *txq = sta->txq[params->tid]; 952 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 953 u16 tid = params->tid; 954 u16 ssn = params->ssn; 955 struct mt76_txq *mtxq; 956 int ret = 0; 957 958 if (!txq) 959 return -EINVAL; 960 961 mtxq = (struct mt76_txq *)txq->drv_priv; 962 963 mt792x_mutex_acquire(dev); 964 switch (action) { 965 case IEEE80211_AMPDU_RX_START: 966 mt76_rx_aggr_start(&dev->mt76, &msta->deflink.wcid, tid, ssn, 967 params->buf_size); 968 mt7921_mcu_uni_rx_ba(dev, params, true); 969 break; 970 case IEEE80211_AMPDU_RX_STOP: 971 mt76_rx_aggr_stop(&dev->mt76, &msta->deflink.wcid, tid); 972 mt7921_mcu_uni_rx_ba(dev, params, false); 973 break; 974 case IEEE80211_AMPDU_TX_OPERATIONAL: 975 mtxq->aggr = true; 976 mtxq->send_bar = false; 977 mt7921_mcu_uni_tx_ba(dev, params, true); 978 break; 979 case IEEE80211_AMPDU_TX_STOP_FLUSH: 980 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 981 mtxq->aggr = false; 982 clear_bit(tid, &msta->deflink.wcid.ampdu_state); 983 mt7921_mcu_uni_tx_ba(dev, params, false); 984 break; 985 case IEEE80211_AMPDU_TX_START: 986 set_bit(tid, &msta->deflink.wcid.ampdu_state); 987 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 988 break; 989 case IEEE80211_AMPDU_TX_STOP_CONT: 990 mtxq->aggr = false; 991 clear_bit(tid, &msta->deflink.wcid.ampdu_state); 992 mt7921_mcu_uni_tx_ba(dev, params, false); 993 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 994 break; 995 } 996 mt792x_mutex_release(dev); 997 998 return ret; 999 } 1000 1001 static int mt7921_sta_state(struct ieee80211_hw *hw, 1002 struct ieee80211_vif *vif, 1003 struct ieee80211_sta *sta, 1004 enum ieee80211_sta_state old_state, 1005 enum ieee80211_sta_state new_state) 1006 { 1007 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1008 1009 if (dev->pm.ds_enable) { 1010 mt792x_mutex_acquire(dev); 1011 mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state); 1012 mt792x_mutex_release(dev); 1013 } 1014 1015 return mt76_sta_state(hw, vif, sta, old_state, new_state); 1016 } 1017 1018 void mt7921_scan_work(struct work_struct *work) 1019 { 1020 struct mt792x_phy *phy; 1021 1022 phy = (struct mt792x_phy *)container_of(work, struct mt792x_phy, 1023 scan_work.work); 1024 1025 while (true) { 1026 struct mt76_connac2_mcu_rxd *rxd; 1027 struct sk_buff *skb; 1028 1029 spin_lock_bh(&phy->dev->mt76.lock); 1030 skb = __skb_dequeue(&phy->scan_event_list); 1031 spin_unlock_bh(&phy->dev->mt76.lock); 1032 1033 if (!skb) 1034 break; 1035 1036 rxd = (struct mt76_connac2_mcu_rxd *)skb->data; 1037 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) { 1038 ieee80211_sched_scan_results(phy->mt76->hw); 1039 } else if (rxd->eid == MCU_EVENT_SCAN_DONE) { 1040 struct mt76_connac_hw_scan_done *event = NULL; 1041 1042 skb_pull(skb, sizeof(*rxd)); 1043 event = (struct mt76_connac_hw_scan_done *)skb->data; 1044 mt7921_regd_change(phy, event->alpha2); 1045 } 1046 1047 if (test_and_clear_bit(MT76_HW_SCANNING, 1048 &phy->mt76->state)) { 1049 struct cfg80211_scan_info info = { 1050 .aborted = false, 1051 }; 1052 1053 ieee80211_scan_completed(phy->mt76->hw, &info); 1054 } 1055 dev_kfree_skb(skb); 1056 } 1057 } 1058 1059 static int 1060 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1061 struct ieee80211_scan_request *req) 1062 { 1063 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1064 struct mt76_phy *mphy = hw->priv; 1065 int err; 1066 1067 mt792x_mutex_acquire(dev); 1068 err = mt76_connac_mcu_hw_scan(mphy, vif, req); 1069 mt792x_mutex_release(dev); 1070 1071 return err; 1072 } 1073 1074 static void 1075 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1076 { 1077 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1078 struct mt76_phy *mphy = hw->priv; 1079 1080 mt792x_mutex_acquire(dev); 1081 mt76_connac_mcu_cancel_hw_scan(mphy, vif); 1082 mt792x_mutex_release(dev); 1083 } 1084 1085 static int 1086 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1087 struct cfg80211_sched_scan_request *req, 1088 struct ieee80211_scan_ies *ies) 1089 { 1090 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1091 struct mt76_phy *mphy = hw->priv; 1092 int err; 1093 1094 mt792x_mutex_acquire(dev); 1095 1096 err = mt76_connac_mcu_sched_scan_req(mphy, vif, req); 1097 if (err < 0) 1098 goto out; 1099 1100 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true); 1101 out: 1102 mt792x_mutex_release(dev); 1103 1104 return err; 1105 } 1106 1107 static int 1108 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1109 { 1110 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1111 struct mt76_phy *mphy = hw->priv; 1112 int err; 1113 1114 mt792x_mutex_acquire(dev); 1115 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false); 1116 mt792x_mutex_release(dev); 1117 1118 return err; 1119 } 1120 1121 static int 1122 mt7921_set_antenna(struct ieee80211_hw *hw, int radio_idx, 1123 u32 tx_ant, u32 rx_ant) 1124 { 1125 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1126 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1127 int max_nss = hweight8(hw->wiphy->available_antennas_tx); 1128 1129 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss) 1130 return -EINVAL; 1131 1132 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant) 1133 return -EINVAL; 1134 1135 mt792x_mutex_acquire(dev); 1136 1137 phy->mt76->antenna_mask = tx_ant; 1138 phy->mt76->chainmask = tx_ant; 1139 1140 mt76_set_stream_caps(phy->mt76, true); 1141 mt7921_set_stream_he_caps(phy); 1142 1143 mt792x_mutex_release(dev); 1144 1145 return 0; 1146 } 1147 1148 #ifdef CONFIG_PM 1149 static int mt7921_suspend(struct ieee80211_hw *hw, 1150 struct cfg80211_wowlan *wowlan) 1151 { 1152 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1153 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1154 1155 cancel_delayed_work_sync(&phy->scan_work); 1156 cancel_delayed_work_sync(&phy->mt76->mac_work); 1157 1158 cancel_delayed_work_sync(&dev->pm.ps_work); 1159 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL); 1160 1161 mt792x_mutex_acquire(dev); 1162 1163 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1164 ieee80211_iterate_active_interfaces_mtx(hw, 1165 IEEE80211_IFACE_ITER_RESUME_ALL, 1166 mt7921_mcu_set_suspend_iter, 1167 &dev->mphy); 1168 1169 mt792x_mutex_release(dev); 1170 1171 return 0; 1172 } 1173 1174 static int mt7921_resume(struct ieee80211_hw *hw) 1175 { 1176 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1177 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1178 1179 mt792x_mutex_acquire(dev); 1180 1181 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1182 ieee80211_iterate_active_interfaces_mtx(hw, 1183 IEEE80211_IFACE_ITER_RESUME_ALL, 1184 mt76_connac_mcu_set_suspend_iter, 1185 &dev->mphy); 1186 1187 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, 1188 MT792x_WATCHDOG_TIME); 1189 1190 mt792x_mutex_release(dev); 1191 1192 return 0; 1193 } 1194 1195 static void mt7921_set_rekey_data(struct ieee80211_hw *hw, 1196 struct ieee80211_vif *vif, 1197 struct cfg80211_gtk_rekey_data *data) 1198 { 1199 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1200 1201 mt792x_mutex_acquire(dev); 1202 mt76_connac_mcu_update_gtk_rekey(hw, vif, data); 1203 mt792x_mutex_release(dev); 1204 } 1205 #endif /* CONFIG_PM */ 1206 1207 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw, 1208 struct ieee80211_vif *vif, 1209 struct ieee80211_sta *sta, 1210 bool enabled) 1211 { 1212 struct mt792x_sta *msta = (struct mt792x_sta *)sta->drv_priv; 1213 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1214 1215 if (!msta->deflink.wcid.sta) 1216 return; 1217 1218 mt792x_mutex_acquire(dev); 1219 1220 if (enabled) 1221 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->deflink.wcid.flags); 1222 else 1223 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->deflink.wcid.flags); 1224 1225 mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->deflink.wcid, 1226 MCU_UNI_CMD(STA_REC_UPDATE)); 1227 1228 mt792x_mutex_release(dev); 1229 } 1230 1231 #if IS_ENABLED(CONFIG_IPV6) 1232 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw, 1233 struct ieee80211_vif *vif, 1234 struct inet6_dev *idev) 1235 { 1236 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1237 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1238 struct inet6_ifaddr *ifa; 1239 struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN]; 1240 struct sk_buff *skb; 1241 u8 i, idx = 0; 1242 1243 struct { 1244 struct { 1245 u8 bss_idx; 1246 u8 pad[3]; 1247 } __packed hdr; 1248 struct mt76_connac_arpns_tlv arpns; 1249 } req_hdr = { 1250 .hdr = { 1251 .bss_idx = mvif->bss_conf.mt76.idx, 1252 }, 1253 .arpns = { 1254 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND), 1255 .mode = 2, /* update */ 1256 .option = 1, /* update only */ 1257 }, 1258 }; 1259 1260 read_lock_bh(&idev->lock); 1261 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1262 if (ifa->flags & IFA_F_TENTATIVE) 1263 continue; 1264 ns_addrs[idx] = ifa->addr; 1265 if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN) 1266 break; 1267 } 1268 read_unlock_bh(&idev->lock); 1269 1270 if (!idx) 1271 return; 1272 1273 req_hdr.arpns.ips_num = idx; 1274 req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv) 1275 + idx * sizeof(struct in6_addr)); 1276 skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr, 1277 sizeof(req_hdr) + idx * sizeof(struct in6_addr), 1278 sizeof(req_hdr), GFP_ATOMIC); 1279 if (!skb) 1280 return; 1281 1282 for (i = 0; i < idx; i++) 1283 skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr)); 1284 1285 skb_queue_tail(&dev->ipv6_ns_list, skb); 1286 1287 ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work); 1288 } 1289 #endif 1290 1291 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw, 1292 const struct cfg80211_sar_specs *sar) 1293 { 1294 struct mt76_phy *mphy = hw->priv; 1295 1296 if (sar) { 1297 int err = mt76_init_sar_power(hw, sar); 1298 1299 if (err) 1300 return err; 1301 } 1302 mt792x_init_acpi_sar_power(mt792x_hw_phy(hw), !sar); 1303 1304 return mt76_connac_mcu_set_rate_txpower(mphy); 1305 } 1306 1307 static int mt7921_set_sar_specs(struct ieee80211_hw *hw, 1308 const struct cfg80211_sar_specs *sar) 1309 { 1310 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1311 int err; 1312 1313 mt792x_mutex_acquire(dev); 1314 err = mt7921_mcu_set_clc(dev, dev->mt76.alpha2, 1315 dev->country_ie_env); 1316 if (err < 0) 1317 goto out; 1318 1319 err = mt7921_set_tx_sar_pwr(hw, sar); 1320 out: 1321 mt792x_mutex_release(dev); 1322 1323 return err; 1324 } 1325 1326 static void 1327 mt7921_channel_switch_beacon(struct ieee80211_hw *hw, 1328 struct ieee80211_vif *vif, 1329 struct cfg80211_chan_def *chandef) 1330 { 1331 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1332 1333 mt792x_mutex_acquire(dev); 1334 mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true); 1335 mt792x_mutex_release(dev); 1336 } 1337 1338 static int 1339 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1340 struct ieee80211_bss_conf *link_conf) 1341 { 1342 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1343 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1344 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1345 int err; 1346 1347 mt792x_mutex_acquire(dev); 1348 1349 err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.deflink.wcid, 1350 true, mvif->bss_conf.mt76.ctx); 1351 if (err) 1352 goto out; 1353 1354 err = mt7921_mcu_set_bss_pm(dev, vif, true); 1355 if (err) 1356 goto out; 1357 1358 err = mt7921_mcu_sta_update(dev, NULL, vif, true, 1359 MT76_STA_INFO_STATE_NONE); 1360 out: 1361 mt792x_mutex_release(dev); 1362 1363 return err; 1364 } 1365 1366 static void 1367 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1368 struct ieee80211_bss_conf *link_conf) 1369 { 1370 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1371 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1372 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1373 int err; 1374 1375 mt792x_mutex_acquire(dev); 1376 1377 err = mt7921_mcu_set_bss_pm(dev, vif, false); 1378 if (err) 1379 goto out; 1380 1381 mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.deflink.wcid, false, 1382 mvif->bss_conf.mt76.ctx); 1383 1384 out: 1385 mt792x_mutex_release(dev); 1386 } 1387 1388 static int 1389 mt7921_add_chanctx(struct ieee80211_hw *hw, 1390 struct ieee80211_chanctx_conf *ctx) 1391 { 1392 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1393 1394 dev->new_ctx = ctx; 1395 return 0; 1396 } 1397 1398 static void 1399 mt7921_remove_chanctx(struct ieee80211_hw *hw, 1400 struct ieee80211_chanctx_conf *ctx) 1401 { 1402 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1403 1404 if (dev->new_ctx == ctx) 1405 dev->new_ctx = NULL; 1406 } 1407 1408 static void 1409 mt7921_change_chanctx(struct ieee80211_hw *hw, 1410 struct ieee80211_chanctx_conf *ctx, 1411 u32 changed) 1412 { 1413 struct mt792x_chanctx *mctx = (struct mt792x_chanctx *)ctx->drv_priv; 1414 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1415 struct ieee80211_vif *vif; 1416 struct mt792x_vif *mvif; 1417 1418 if (!mctx->bss_conf) 1419 return; 1420 1421 mvif = container_of(mctx->bss_conf, struct mt792x_vif, bss_conf); 1422 vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv); 1423 1424 mt792x_mutex_acquire(phy->dev); 1425 if (vif->type == NL80211_IFTYPE_MONITOR) { 1426 mt7921_mcu_set_sniffer(mvif->phy->dev, vif, true); 1427 mt7921_mcu_config_sniffer(mvif, ctx); 1428 } else { 1429 mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->bss_conf.mt76, ctx); 1430 } 1431 mt76_phy_chandef_set(mvif->phy->mt76, &ctx->def); 1432 mt792x_mutex_release(phy->dev); 1433 } 1434 1435 static void mt7921_mgd_prepare_tx(struct ieee80211_hw *hw, 1436 struct ieee80211_vif *vif, 1437 struct ieee80211_prep_tx_info *info) 1438 { 1439 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1440 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1441 u16 duration = info->duration ? info->duration : 1442 jiffies_to_msecs(HZ); 1443 1444 mt792x_mutex_acquire(dev); 1445 mt7921_set_roc(mvif->phy, mvif, mvif->bss_conf.mt76.ctx->def.chan, duration, 1446 MT7921_ROC_REQ_JOIN); 1447 mt792x_mutex_release(dev); 1448 } 1449 1450 static void mt7921_mgd_complete_tx(struct ieee80211_hw *hw, 1451 struct ieee80211_vif *vif, 1452 struct ieee80211_prep_tx_info *info) 1453 { 1454 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1455 1456 /* Keep the join ROC until the station is authorized, so that it 1457 * covers the 4-way handshake. mt7921_mac_sta_event() releases it. 1458 */ 1459 if (vif->type == NL80211_IFTYPE_STATION && info->success && 1460 (info->subtype == IEEE80211_STYPE_ASSOC_REQ || 1461 info->subtype == IEEE80211_STYPE_REASSOC_REQ)) { 1462 mvif->roc_join_held = true; 1463 return; 1464 } 1465 1466 mt7921_abort_roc(mvif->phy, mvif); 1467 } 1468 1469 static int mt7921_switch_vif_chanctx(struct ieee80211_hw *hw, 1470 struct ieee80211_vif_chanctx_switch *vifs, 1471 int n_vifs, 1472 enum ieee80211_chanctx_switch_mode mode) 1473 { 1474 return mt792x_assign_vif_chanctx(hw, vifs->vif, vifs->link_conf, 1475 vifs->new_ctx); 1476 } 1477 1478 void mt7921_csa_work(struct work_struct *work) 1479 { 1480 struct mt792x_vif *mvif; 1481 struct mt792x_dev *dev; 1482 struct ieee80211_vif *vif; 1483 int ret; 1484 1485 mvif = (struct mt792x_vif *)container_of(work, struct mt792x_vif, 1486 csa_work); 1487 dev = mvif->phy->dev; 1488 vif = container_of((void *)mvif, struct ieee80211_vif, drv_priv); 1489 1490 mt792x_mutex_acquire(dev); 1491 ret = mt76_connac_mcu_uni_set_chctx(mvif->phy->mt76, &mvif->bss_conf.mt76, 1492 dev->new_ctx); 1493 mt792x_mutex_release(dev); 1494 1495 ieee80211_chswitch_done(vif, !ret, 0); 1496 } 1497 1498 static int mt7921_pre_channel_switch(struct ieee80211_hw *hw, 1499 struct ieee80211_vif *vif, 1500 struct ieee80211_channel_switch *chsw) 1501 { 1502 if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc) 1503 return -EOPNOTSUPP; 1504 1505 if (!cfg80211_chandef_usable(hw->wiphy, &chsw->chandef, 1506 IEEE80211_CHAN_DISABLED)) 1507 return -EOPNOTSUPP; 1508 1509 return 0; 1510 } 1511 1512 static void mt7921_channel_switch(struct ieee80211_hw *hw, 1513 struct ieee80211_vif *vif, 1514 struct ieee80211_channel_switch *chsw) 1515 { 1516 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1517 u16 beacon_interval = vif->bss_conf.beacon_int; 1518 1519 mvif->csa_timer.expires = TU_TO_EXP_TIME(beacon_interval * chsw->count); 1520 add_timer(&mvif->csa_timer); 1521 } 1522 1523 static void mt7921_abort_channel_switch(struct ieee80211_hw *hw, 1524 struct ieee80211_vif *vif, 1525 struct ieee80211_bss_conf *link_conf) 1526 { 1527 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1528 1529 timer_delete_sync(&mvif->csa_timer); 1530 cancel_work_sync(&mvif->csa_work); 1531 } 1532 1533 static void mt7921_channel_switch_rx_beacon(struct ieee80211_hw *hw, 1534 struct ieee80211_vif *vif, 1535 struct ieee80211_channel_switch *chsw) 1536 { 1537 struct mt792x_dev *dev = mt792x_hw_dev(hw); 1538 struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv; 1539 u16 beacon_interval = vif->bss_conf.beacon_int; 1540 1541 if (!dev->new_ctx) 1542 return; 1543 1544 if (cfg80211_chandef_identical(&chsw->chandef, 1545 &dev->new_ctx->def) && 1546 chsw->count) { 1547 mod_timer(&mvif->csa_timer, 1548 TU_TO_EXP_TIME(beacon_interval * chsw->count)); 1549 } 1550 } 1551 1552 static void mt7921_rfkill_poll(struct ieee80211_hw *hw) 1553 { 1554 struct mt792x_phy *phy = mt792x_hw_phy(hw); 1555 int ret = 0; 1556 1557 mt792x_mutex_acquire(phy->dev); 1558 ret = mt7921_mcu_wf_rf_pin_ctrl(phy, WF_RF_PIN_POLL); 1559 mt792x_mutex_release(phy->dev); 1560 1561 wiphy_rfkill_set_hw_state(hw->wiphy, ret ? false : true); 1562 } 1563 1564 const struct ieee80211_ops mt7921_ops = { 1565 .tx = mt792x_tx, 1566 .start = mt7921_start, 1567 .stop = mt7921_stop, 1568 .add_interface = mt7921_add_interface, 1569 .remove_interface = mt792x_remove_interface, 1570 .config = mt7921_config, 1571 .conf_tx = mt792x_conf_tx, 1572 .configure_filter = mt7921_configure_filter, 1573 .bss_info_changed = mt7921_bss_info_changed, 1574 .start_ap = mt7921_start_ap, 1575 .stop_ap = mt7921_stop_ap, 1576 .sta_state = mt7921_sta_state, 1577 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 1578 .set_key = mt7921_set_key, 1579 .sta_set_decap_offload = mt7921_sta_set_decap_offload, 1580 #if IS_ENABLED(CONFIG_IPV6) 1581 .ipv6_addr_change = mt7921_ipv6_addr_change, 1582 #endif /* CONFIG_IPV6 */ 1583 .ampdu_action = mt7921_ampdu_action, 1584 .set_rts_threshold = mt7921_set_rts_threshold, 1585 .wake_tx_queue = mt76_wake_tx_queue, 1586 .release_buffered_frames = mt76_release_buffered_frames, 1587 .channel_switch_beacon = mt7921_channel_switch_beacon, 1588 .get_txpower = mt792x_get_txpower, 1589 .get_stats = mt792x_get_stats, 1590 .get_et_sset_count = mt792x_get_et_sset_count, 1591 .get_et_strings = mt792x_get_et_strings, 1592 .get_et_stats = mt792x_get_et_stats, 1593 .get_tsf = mt792x_get_tsf, 1594 .set_tsf = mt792x_set_tsf, 1595 .get_survey = mt76_get_survey, 1596 .get_antenna = mt76_get_antenna, 1597 .set_antenna = mt7921_set_antenna, 1598 .set_coverage_class = mt792x_set_coverage_class, 1599 .hw_scan = mt7921_hw_scan, 1600 .cancel_hw_scan = mt7921_cancel_hw_scan, 1601 .sta_statistics = mt792x_sta_statistics, 1602 .sched_scan_start = mt7921_start_sched_scan, 1603 .sched_scan_stop = mt7921_stop_sched_scan, 1604 CFG80211_TESTMODE_CMD(mt7921_testmode_cmd) 1605 CFG80211_TESTMODE_DUMP(mt7921_testmode_dump) 1606 #ifdef CONFIG_PM 1607 .suspend = mt7921_suspend, 1608 .resume = mt7921_resume, 1609 .set_wakeup = mt792x_set_wakeup, 1610 .set_rekey_data = mt7921_set_rekey_data, 1611 #endif /* CONFIG_PM */ 1612 .flush = mt792x_flush, 1613 .set_sar_specs = mt7921_set_sar_specs, 1614 .rfkill_poll = mt7921_rfkill_poll, 1615 .remain_on_channel = mt7921_remain_on_channel, 1616 .cancel_remain_on_channel = mt7921_cancel_remain_on_channel, 1617 .add_chanctx = mt7921_add_chanctx, 1618 .remove_chanctx = mt7921_remove_chanctx, 1619 .change_chanctx = mt7921_change_chanctx, 1620 .assign_vif_chanctx = mt792x_assign_vif_chanctx, 1621 .unassign_vif_chanctx = mt792x_unassign_vif_chanctx, 1622 .mgd_prepare_tx = mt7921_mgd_prepare_tx, 1623 .mgd_complete_tx = mt7921_mgd_complete_tx, 1624 .switch_vif_chanctx = mt7921_switch_vif_chanctx, 1625 .pre_channel_switch = mt7921_pre_channel_switch, 1626 .channel_switch = mt7921_channel_switch, 1627 .abort_channel_switch = mt7921_abort_channel_switch, 1628 .channel_switch_rx_beacon = mt7921_channel_switch_rx_beacon, 1629 }; 1630 EXPORT_SYMBOL_GPL(mt7921_ops); 1631 1632 MODULE_DESCRIPTION("MediaTek MT7921 core driver"); 1633 MODULE_LICENSE("Dual BSD/GPL"); 1634 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); 1635