1 // SPDX-License-Identifier: ISC 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 "mt7921.h" 9 #include "mcu.h" 10 11 static void 12 mt7921_gen_ppe_thresh(u8 *he_ppet, int nss) 13 { 14 u8 i, ppet_bits, ppet_size, ru_bit_mask = 0x7; /* HE80 */ 15 u8 ppet16_ppet8_ru3_ru0[] = {0x1c, 0xc7, 0x71}; 16 17 he_ppet[0] = FIELD_PREP(IEEE80211_PPE_THRES_NSS_MASK, nss - 1) | 18 FIELD_PREP(IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK, 19 ru_bit_mask); 20 21 ppet_bits = IEEE80211_PPE_THRES_INFO_PPET_SIZE * 22 nss * hweight8(ru_bit_mask) * 2; 23 ppet_size = DIV_ROUND_UP(ppet_bits, 8); 24 25 for (i = 0; i < ppet_size - 1; i++) 26 he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3]; 27 28 he_ppet[i + 1] = ppet16_ppet8_ru3_ru0[i % 3] & 29 (0xff >> (8 - (ppet_bits - 1) % 8)); 30 } 31 32 static int 33 mt7921_init_he_caps(struct mt7921_phy *phy, enum nl80211_band band, 34 struct ieee80211_sband_iftype_data *data) 35 { 36 int i, idx = 0; 37 int nss = hweight8(phy->mt76->chainmask); 38 u16 mcs_map = 0; 39 40 for (i = 0; i < 8; i++) { 41 if (i < nss) 42 mcs_map |= (IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2)); 43 else 44 mcs_map |= (IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2)); 45 } 46 47 for (i = 0; i < NUM_NL80211_IFTYPES; i++) { 48 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap; 49 struct ieee80211_he_cap_elem *he_cap_elem = 50 &he_cap->he_cap_elem; 51 struct ieee80211_he_mcs_nss_supp *he_mcs = 52 &he_cap->he_mcs_nss_supp; 53 54 switch (i) { 55 case NL80211_IFTYPE_STATION: 56 break; 57 default: 58 continue; 59 } 60 61 data[idx].types_mask = BIT(i); 62 he_cap->has_he = true; 63 64 he_cap_elem->mac_cap_info[0] = 65 IEEE80211_HE_MAC_CAP0_HTC_HE; 66 he_cap_elem->mac_cap_info[3] = 67 IEEE80211_HE_MAC_CAP3_OMI_CONTROL | 68 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_RESERVED; 69 he_cap_elem->mac_cap_info[4] = 70 IEEE80211_HE_MAC_CAP4_AMDSU_IN_AMPDU; 71 72 if (band == NL80211_BAND_2GHZ) 73 he_cap_elem->phy_cap_info[0] = 74 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G; 75 else if (band == NL80211_BAND_5GHZ) 76 he_cap_elem->phy_cap_info[0] = 77 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G | 78 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G; 79 80 he_cap_elem->phy_cap_info[1] = 81 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD; 82 he_cap_elem->phy_cap_info[2] = 83 IEEE80211_HE_PHY_CAP2_STBC_TX_UNDER_80MHZ | 84 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ; 85 86 switch (i) { 87 case NL80211_IFTYPE_STATION: 88 he_cap_elem->mac_cap_info[0] |= 89 IEEE80211_HE_MAC_CAP0_TWT_REQ; 90 he_cap_elem->mac_cap_info[1] |= 91 IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_16US; 92 93 if (band == NL80211_BAND_2GHZ) 94 he_cap_elem->phy_cap_info[0] |= 95 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G; 96 else if (band == NL80211_BAND_5GHZ) 97 he_cap_elem->phy_cap_info[0] |= 98 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_5G; 99 100 he_cap_elem->phy_cap_info[1] |= 101 IEEE80211_HE_PHY_CAP1_DEVICE_CLASS_A | 102 IEEE80211_HE_PHY_CAP1_HE_LTF_AND_GI_FOR_HE_PPDUS_0_8US; 103 he_cap_elem->phy_cap_info[3] |= 104 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_QPSK | 105 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_QPSK; 106 he_cap_elem->phy_cap_info[6] |= 107 IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB | 108 IEEE80211_HE_PHY_CAP6_PARTIAL_BW_EXT_RANGE | 109 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT; 110 he_cap_elem->phy_cap_info[7] |= 111 IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_AR | 112 IEEE80211_HE_PHY_CAP7_HE_SU_MU_PPDU_4XLTF_AND_08_US_GI; 113 he_cap_elem->phy_cap_info[8] |= 114 IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G | 115 IEEE80211_HE_PHY_CAP8_DCM_MAX_RU_484; 116 he_cap_elem->phy_cap_info[9] |= 117 IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM | 118 IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK | 119 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU | 120 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU | 121 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB | 122 IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB; 123 break; 124 } 125 126 he_mcs->rx_mcs_80 = cpu_to_le16(mcs_map); 127 he_mcs->tx_mcs_80 = cpu_to_le16(mcs_map); 128 he_mcs->rx_mcs_160 = cpu_to_le16(mcs_map); 129 he_mcs->tx_mcs_160 = cpu_to_le16(mcs_map); 130 he_mcs->rx_mcs_80p80 = cpu_to_le16(mcs_map); 131 he_mcs->tx_mcs_80p80 = cpu_to_le16(mcs_map); 132 133 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres)); 134 if (he_cap_elem->phy_cap_info[6] & 135 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) { 136 mt7921_gen_ppe_thresh(he_cap->ppe_thres, nss); 137 } else { 138 he_cap_elem->phy_cap_info[9] |= 139 IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US; 140 } 141 idx++; 142 } 143 144 return idx; 145 } 146 147 void mt7921_set_stream_he_caps(struct mt7921_phy *phy) 148 { 149 struct ieee80211_sband_iftype_data *data; 150 struct ieee80211_supported_band *band; 151 int n; 152 153 if (phy->mt76->cap.has_2ghz) { 154 data = phy->iftype[NL80211_BAND_2GHZ]; 155 n = mt7921_init_he_caps(phy, NL80211_BAND_2GHZ, data); 156 157 band = &phy->mt76->sband_2g.sband; 158 band->iftype_data = data; 159 band->n_iftype_data = n; 160 } 161 162 if (phy->mt76->cap.has_5ghz) { 163 data = phy->iftype[NL80211_BAND_5GHZ]; 164 n = mt7921_init_he_caps(phy, NL80211_BAND_5GHZ, data); 165 166 band = &phy->mt76->sband_5g.sband; 167 band->iftype_data = data; 168 band->n_iftype_data = n; 169 } 170 } 171 172 static int mt7921_start(struct ieee80211_hw *hw) 173 { 174 struct mt7921_dev *dev = mt7921_hw_dev(hw); 175 struct mt7921_phy *phy = mt7921_hw_phy(hw); 176 177 mutex_lock(&dev->mt76.mutex); 178 179 mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false); 180 mt76_connac_mcu_set_channel_domain(phy->mt76); 181 182 mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_SET_RX_PATH); 183 mt7921_mac_reset_counters(phy); 184 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 185 186 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, 187 MT7921_WATCHDOG_TIME); 188 189 mutex_unlock(&dev->mt76.mutex); 190 191 return 0; 192 } 193 194 static void mt7921_stop(struct ieee80211_hw *hw) 195 { 196 struct mt7921_dev *dev = mt7921_hw_dev(hw); 197 struct mt7921_phy *phy = mt7921_hw_phy(hw); 198 199 cancel_delayed_work_sync(&phy->mt76->mac_work); 200 201 mutex_lock(&dev->mt76.mutex); 202 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 203 mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false); 204 mutex_unlock(&dev->mt76.mutex); 205 } 206 207 static inline int get_free_idx(u32 mask, u8 start, u8 end) 208 { 209 return ffs(~mask & GENMASK(end, start)); 210 } 211 212 static int get_omac_idx(enum nl80211_iftype type, u64 mask) 213 { 214 int i; 215 216 switch (type) { 217 case NL80211_IFTYPE_STATION: 218 /* prefer hw bssid slot 1-3 */ 219 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3); 220 if (i) 221 return i - 1; 222 223 if (type != NL80211_IFTYPE_STATION) 224 break; 225 226 /* next, try to find a free repeater entry for the sta */ 227 i = get_free_idx(mask >> REPEATER_BSSID_START, 0, 228 REPEATER_BSSID_MAX - REPEATER_BSSID_START); 229 if (i) 230 return i + 32 - 1; 231 232 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 233 if (i) 234 return i - 1; 235 236 if (~mask & BIT(HW_BSSID_0)) 237 return HW_BSSID_0; 238 239 break; 240 case NL80211_IFTYPE_MONITOR: 241 /* ap uses hw bssid 0 and ext bssid */ 242 if (~mask & BIT(HW_BSSID_0)) 243 return HW_BSSID_0; 244 245 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX); 246 if (i) 247 return i - 1; 248 249 break; 250 default: 251 WARN_ON(1); 252 break; 253 } 254 255 return -1; 256 } 257 258 static int mt7921_add_interface(struct ieee80211_hw *hw, 259 struct ieee80211_vif *vif) 260 { 261 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 262 struct mt7921_dev *dev = mt7921_hw_dev(hw); 263 struct mt7921_phy *phy = mt7921_hw_phy(hw); 264 struct mt76_txq *mtxq; 265 int idx, ret = 0; 266 267 mutex_lock(&dev->mt76.mutex); 268 269 if (vif->type == NL80211_IFTYPE_MONITOR && 270 is_zero_ether_addr(vif->addr)) 271 phy->monitor_vif = vif; 272 273 mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1; 274 if (mvif->mt76.idx >= MT7921_MAX_INTERFACES) { 275 ret = -ENOSPC; 276 goto out; 277 } 278 279 idx = get_omac_idx(vif->type, phy->omac_mask); 280 if (idx < 0) { 281 ret = -ENOSPC; 282 goto out; 283 } 284 mvif->mt76.omac_idx = idx; 285 mvif->phy = phy; 286 mvif->mt76.band_idx = 0; 287 mvif->mt76.wmm_idx = mvif->mt76.idx % MT7921_MAX_WMM_SETS; 288 289 ret = mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, 290 true); 291 if (ret) 292 goto out; 293 294 dev->mt76.vif_mask |= BIT(mvif->mt76.idx); 295 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx); 296 297 idx = MT7921_WTBL_RESERVED - mvif->mt76.idx; 298 299 INIT_LIST_HEAD(&mvif->sta.stats_list); 300 INIT_LIST_HEAD(&mvif->sta.poll_list); 301 mvif->sta.wcid.idx = idx; 302 mvif->sta.wcid.ext_phy = mvif->mt76.band_idx; 303 mvif->sta.wcid.hw_key_idx = -1; 304 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET; 305 mt7921_mac_wtbl_update(dev, idx, 306 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 307 308 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid); 309 if (vif->txq) { 310 mtxq = (struct mt76_txq *)vif->txq->drv_priv; 311 mtxq->wcid = &mvif->sta.wcid; 312 } 313 314 if (vif->type != NL80211_IFTYPE_AP && 315 (!mvif->mt76.omac_idx || mvif->mt76.omac_idx > 3)) 316 vif->offload_flags = 0; 317 318 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR; 319 320 out: 321 mutex_unlock(&dev->mt76.mutex); 322 323 return ret; 324 } 325 326 static void mt7921_remove_interface(struct ieee80211_hw *hw, 327 struct ieee80211_vif *vif) 328 { 329 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 330 struct mt7921_sta *msta = &mvif->sta; 331 struct mt7921_dev *dev = mt7921_hw_dev(hw); 332 struct mt7921_phy *phy = mt7921_hw_phy(hw); 333 int idx = msta->wcid.idx; 334 335 if (vif == phy->monitor_vif) 336 phy->monitor_vif = NULL; 337 338 mt76_connac_mcu_uni_add_dev(&dev->mphy, vif, &mvif->sta.wcid, false); 339 340 rcu_assign_pointer(dev->mt76.wcid[idx], NULL); 341 342 mutex_lock(&dev->mt76.mutex); 343 dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx); 344 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx); 345 mutex_unlock(&dev->mt76.mutex); 346 347 spin_lock_bh(&dev->sta_poll_lock); 348 if (!list_empty(&msta->poll_list)) 349 list_del_init(&msta->poll_list); 350 spin_unlock_bh(&dev->sta_poll_lock); 351 } 352 353 int mt7921_set_channel(struct mt7921_phy *phy) 354 { 355 struct mt7921_dev *dev = phy->dev; 356 int ret; 357 358 cancel_delayed_work_sync(&phy->mt76->mac_work); 359 360 mutex_lock(&dev->mt76.mutex); 361 set_bit(MT76_RESET, &phy->mt76->state); 362 363 mt76_set_channel(phy->mt76); 364 365 ret = mt7921_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH); 366 if (ret) 367 goto out; 368 369 mt7921_mac_set_timing(phy); 370 371 mt7921_mac_reset_counters(phy); 372 phy->noise = 0; 373 374 out: 375 clear_bit(MT76_RESET, &phy->mt76->state); 376 mutex_unlock(&dev->mt76.mutex); 377 378 mt76_txq_schedule_all(phy->mt76); 379 380 ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mt76->mac_work, 381 MT7921_WATCHDOG_TIME); 382 383 return ret; 384 } 385 386 static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 387 struct ieee80211_vif *vif, struct ieee80211_sta *sta, 388 struct ieee80211_key_conf *key) 389 { 390 struct mt7921_dev *dev = mt7921_hw_dev(hw); 391 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 392 struct mt7921_sta *msta = sta ? (struct mt7921_sta *)sta->drv_priv : 393 &mvif->sta; 394 struct mt76_wcid *wcid = &msta->wcid; 395 int idx = key->keyidx; 396 397 /* The hardware does not support per-STA RX GTK, fallback 398 * to software mode for these. 399 */ 400 if ((vif->type == NL80211_IFTYPE_ADHOC || 401 vif->type == NL80211_IFTYPE_MESH_POINT) && 402 (key->cipher == WLAN_CIPHER_SUITE_TKIP || 403 key->cipher == WLAN_CIPHER_SUITE_CCMP) && 404 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) 405 return -EOPNOTSUPP; 406 407 /* fall back to sw encryption for unsupported ciphers */ 408 switch (key->cipher) { 409 case WLAN_CIPHER_SUITE_AES_CMAC: 410 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE; 411 break; 412 case WLAN_CIPHER_SUITE_TKIP: 413 case WLAN_CIPHER_SUITE_CCMP: 414 case WLAN_CIPHER_SUITE_CCMP_256: 415 case WLAN_CIPHER_SUITE_GCMP: 416 case WLAN_CIPHER_SUITE_GCMP_256: 417 case WLAN_CIPHER_SUITE_SMS4: 418 break; 419 case WLAN_CIPHER_SUITE_WEP40: 420 case WLAN_CIPHER_SUITE_WEP104: 421 default: 422 return -EOPNOTSUPP; 423 } 424 425 if (cmd == SET_KEY) { 426 key->hw_key_idx = wcid->idx; 427 wcid->hw_key_idx = idx; 428 } else if (idx == wcid->hw_key_idx) { 429 wcid->hw_key_idx = -1; 430 } 431 mt76_wcid_key_setup(&dev->mt76, wcid, 432 cmd == SET_KEY ? key : NULL); 433 434 return mt7921_mcu_add_key(dev, vif, msta, key, cmd); 435 } 436 437 static int mt7921_config(struct ieee80211_hw *hw, u32 changed) 438 { 439 struct mt7921_dev *dev = mt7921_hw_dev(hw); 440 struct mt7921_phy *phy = mt7921_hw_phy(hw); 441 bool band = phy != &dev->phy; 442 int ret; 443 444 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) { 445 ieee80211_stop_queues(hw); 446 ret = mt7921_set_channel(phy); 447 if (ret) 448 return ret; 449 ieee80211_wake_queues(hw); 450 } 451 452 mutex_lock(&dev->mt76.mutex); 453 454 if (changed & IEEE80211_CONF_CHANGE_MONITOR) { 455 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR); 456 457 if (!enabled) 458 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC; 459 else 460 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC; 461 462 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN, 463 enabled); 464 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter); 465 } 466 467 mutex_unlock(&dev->mt76.mutex); 468 469 return 0; 470 } 471 472 static int 473 mt7921_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue, 474 const struct ieee80211_tx_queue_params *params) 475 { 476 struct mt7921_dev *dev = mt7921_hw_dev(hw); 477 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 478 479 /* no need to update right away, we'll get BSS_CHANGED_QOS */ 480 queue = mt7921_lmac_mapping(dev, queue); 481 mvif->queue_params[queue] = *params; 482 483 return 0; 484 } 485 486 static void mt7921_configure_filter(struct ieee80211_hw *hw, 487 unsigned int changed_flags, 488 unsigned int *total_flags, 489 u64 multicast) 490 { 491 struct mt7921_dev *dev = mt7921_hw_dev(hw); 492 struct mt7921_phy *phy = mt7921_hw_phy(hw); 493 bool band = phy != &dev->phy; 494 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK | 495 MT_WF_RFCR1_DROP_BF_POLL | 496 MT_WF_RFCR1_DROP_BA | 497 MT_WF_RFCR1_DROP_CFEND | 498 MT_WF_RFCR1_DROP_CFACK; 499 u32 flags = 0; 500 501 #define MT76_FILTER(_flag, _hw) do { \ 502 flags |= *total_flags & FIF_##_flag; \ 503 phy->rxfilter &= ~(_hw); \ 504 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \ 505 } while (0) 506 507 mutex_lock(&dev->mt76.mutex); 508 509 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS | 510 MT_WF_RFCR_DROP_OTHER_BEACON | 511 MT_WF_RFCR_DROP_FRAME_REPORT | 512 MT_WF_RFCR_DROP_PROBEREQ | 513 MT_WF_RFCR_DROP_MCAST_FILTERED | 514 MT_WF_RFCR_DROP_MCAST | 515 MT_WF_RFCR_DROP_BCAST | 516 MT_WF_RFCR_DROP_DUPLICATE | 517 MT_WF_RFCR_DROP_A2_BSSID | 518 MT_WF_RFCR_DROP_UNWANTED_CTL | 519 MT_WF_RFCR_DROP_STBC_MULTI); 520 521 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM | 522 MT_WF_RFCR_DROP_A3_MAC | 523 MT_WF_RFCR_DROP_A3_BSSID); 524 525 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL); 526 527 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS | 528 MT_WF_RFCR_DROP_RTS | 529 MT_WF_RFCR_DROP_CTL_RSV | 530 MT_WF_RFCR_DROP_NDPA); 531 532 *total_flags = flags; 533 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter); 534 535 if (*total_flags & FIF_CONTROL) 536 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags); 537 else 538 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags); 539 540 mutex_unlock(&dev->mt76.mutex); 541 } 542 543 static void mt7921_bss_info_changed(struct ieee80211_hw *hw, 544 struct ieee80211_vif *vif, 545 struct ieee80211_bss_conf *info, 546 u32 changed) 547 { 548 struct mt7921_phy *phy = mt7921_hw_phy(hw); 549 struct mt7921_dev *dev = mt7921_hw_dev(hw); 550 551 mutex_lock(&dev->mt76.mutex); 552 553 if (changed & BSS_CHANGED_ERP_SLOT) { 554 int slottime = info->use_short_slot ? 9 : 20; 555 556 if (slottime != phy->slottime) { 557 phy->slottime = slottime; 558 mt7921_mac_set_timing(phy); 559 } 560 } 561 562 /* ensure that enable txcmd_mode after bss_info */ 563 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED)) 564 mt7921_mcu_set_tx(dev, vif); 565 566 if (changed & BSS_CHANGED_PS) 567 mt7921_mcu_uni_bss_ps(dev, vif); 568 569 mutex_unlock(&dev->mt76.mutex); 570 } 571 572 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif, 573 struct ieee80211_sta *sta) 574 { 575 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76); 576 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 577 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 578 int ret, idx; 579 580 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7921_WTBL_STA - 1); 581 if (idx < 0) 582 return -ENOSPC; 583 584 INIT_LIST_HEAD(&msta->stats_list); 585 INIT_LIST_HEAD(&msta->poll_list); 586 msta->vif = mvif; 587 msta->wcid.sta = 1; 588 msta->wcid.idx = idx; 589 msta->wcid.ext_phy = mvif->mt76.band_idx; 590 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET; 591 msta->stats.jiffies = jiffies; 592 593 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) 594 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid, 595 true); 596 597 mt7921_mac_wtbl_update(dev, idx, 598 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 599 600 ret = mt76_connac_mcu_add_sta_cmd(&dev->mphy, vif, sta, &msta->wcid, 601 true, MCU_UNI_CMD_STA_REC_UPDATE); 602 if (ret) 603 return ret; 604 605 return 0; 606 } 607 608 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 609 struct ieee80211_sta *sta) 610 { 611 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76); 612 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 613 614 mt76_connac_mcu_add_sta_cmd(&dev->mphy, vif, sta, &msta->wcid, false, 615 MCU_UNI_CMD_STA_REC_UPDATE); 616 mt7921_mac_wtbl_update(dev, msta->wcid.idx, 617 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 618 619 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) { 620 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 621 622 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, &mvif->sta.wcid, 623 false); 624 } 625 626 spin_lock_bh(&dev->sta_poll_lock); 627 if (!list_empty(&msta->poll_list)) 628 list_del_init(&msta->poll_list); 629 if (!list_empty(&msta->stats_list)) 630 list_del_init(&msta->stats_list); 631 spin_unlock_bh(&dev->sta_poll_lock); 632 } 633 634 static void mt7921_tx(struct ieee80211_hw *hw, 635 struct ieee80211_tx_control *control, 636 struct sk_buff *skb) 637 { 638 struct mt7921_dev *dev = mt7921_hw_dev(hw); 639 struct mt76_phy *mphy = hw->priv; 640 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 641 struct ieee80211_vif *vif = info->control.vif; 642 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 643 644 if (control->sta) { 645 struct mt7921_sta *sta; 646 647 sta = (struct mt7921_sta *)control->sta->drv_priv; 648 wcid = &sta->wcid; 649 } 650 651 if (vif && !control->sta) { 652 struct mt7921_vif *mvif; 653 654 mvif = (struct mt7921_vif *)vif->drv_priv; 655 wcid = &mvif->sta.wcid; 656 } 657 658 mt76_tx(mphy, control->sta, wcid, skb); 659 } 660 661 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val) 662 { 663 struct mt7921_dev *dev = mt7921_hw_dev(hw); 664 665 mutex_lock(&dev->mt76.mutex); 666 mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0); 667 mutex_unlock(&dev->mt76.mutex); 668 669 return 0; 670 } 671 672 static int 673 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 674 struct ieee80211_ampdu_params *params) 675 { 676 enum ieee80211_ampdu_mlme_action action = params->action; 677 struct mt7921_dev *dev = mt7921_hw_dev(hw); 678 struct ieee80211_sta *sta = params->sta; 679 struct ieee80211_txq *txq = sta->txq[params->tid]; 680 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 681 u16 tid = params->tid; 682 u16 ssn = params->ssn; 683 struct mt76_txq *mtxq; 684 int ret = 0; 685 686 if (!txq) 687 return -EINVAL; 688 689 mtxq = (struct mt76_txq *)txq->drv_priv; 690 691 mutex_lock(&dev->mt76.mutex); 692 switch (action) { 693 case IEEE80211_AMPDU_RX_START: 694 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn, 695 params->buf_size); 696 mt7921_mcu_uni_rx_ba(dev, params, true); 697 break; 698 case IEEE80211_AMPDU_RX_STOP: 699 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid); 700 mt7921_mcu_uni_rx_ba(dev, params, false); 701 break; 702 case IEEE80211_AMPDU_TX_OPERATIONAL: 703 mtxq->aggr = true; 704 mtxq->send_bar = false; 705 mt7921_mcu_uni_tx_ba(dev, params, true); 706 break; 707 case IEEE80211_AMPDU_TX_STOP_FLUSH: 708 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 709 mtxq->aggr = false; 710 clear_bit(tid, &msta->ampdu_state); 711 mt7921_mcu_uni_tx_ba(dev, params, false); 712 break; 713 case IEEE80211_AMPDU_TX_START: 714 set_bit(tid, &msta->ampdu_state); 715 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 716 break; 717 case IEEE80211_AMPDU_TX_STOP_CONT: 718 mtxq->aggr = false; 719 clear_bit(tid, &msta->ampdu_state); 720 mt7921_mcu_uni_tx_ba(dev, params, false); 721 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 722 break; 723 } 724 mutex_unlock(&dev->mt76.mutex); 725 726 return ret; 727 } 728 729 static int 730 mt7921_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 731 struct ieee80211_sta *sta) 732 { 733 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST, 734 IEEE80211_STA_NONE); 735 } 736 737 static int 738 mt7921_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 739 struct ieee80211_sta *sta) 740 { 741 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE, 742 IEEE80211_STA_NOTEXIST); 743 } 744 745 static int 746 mt7921_get_stats(struct ieee80211_hw *hw, 747 struct ieee80211_low_level_stats *stats) 748 { 749 struct mt7921_phy *phy = mt7921_hw_phy(hw); 750 struct mib_stats *mib = &phy->mib; 751 752 stats->dot11RTSSuccessCount = mib->rts_cnt; 753 stats->dot11RTSFailureCount = mib->rts_retries_cnt; 754 stats->dot11FCSErrorCount = mib->fcs_err_cnt; 755 stats->dot11ACKFailureCount = mib->ack_fail_cnt; 756 757 return 0; 758 } 759 760 static u64 761 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 762 { 763 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 764 struct mt7921_dev *dev = mt7921_hw_dev(hw); 765 struct mt7921_phy *phy = mt7921_hw_phy(hw); 766 u8 omac_idx = mvif->mt76.omac_idx; 767 bool band = phy != &dev->phy; 768 union { 769 u64 t64; 770 u32 t32[2]; 771 } tsf; 772 u16 n; 773 774 mutex_lock(&dev->mt76.mutex); 775 776 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx; 777 /* TSF software read */ 778 mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE); 779 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band)); 780 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band)); 781 782 mutex_unlock(&dev->mt76.mutex); 783 784 return tsf.t64; 785 } 786 787 static void 788 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 789 u64 timestamp) 790 { 791 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 792 struct mt7921_dev *dev = mt7921_hw_dev(hw); 793 struct mt7921_phy *phy = mt7921_hw_phy(hw); 794 u8 omac_idx = mvif->mt76.omac_idx; 795 bool band = phy != &dev->phy; 796 union { 797 u64 t64; 798 u32 t32[2]; 799 } tsf = { .t64 = timestamp, }; 800 u16 n; 801 802 mutex_lock(&dev->mt76.mutex); 803 804 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx; 805 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]); 806 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]); 807 /* TSF software overwrite */ 808 mt76_set(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_WRITE); 809 810 mutex_unlock(&dev->mt76.mutex); 811 } 812 813 static void 814 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class) 815 { 816 struct mt7921_phy *phy = mt7921_hw_phy(hw); 817 struct mt7921_dev *dev = phy->dev; 818 819 mutex_lock(&dev->mt76.mutex); 820 phy->coverage_class = max_t(s16, coverage_class, 0); 821 mt7921_mac_set_timing(phy); 822 mutex_unlock(&dev->mt76.mutex); 823 } 824 825 void mt7921_scan_work(struct work_struct *work) 826 { 827 struct mt7921_phy *phy; 828 829 phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy, 830 scan_work.work); 831 832 while (true) { 833 struct mt7921_mcu_rxd *rxd; 834 struct sk_buff *skb; 835 836 spin_lock_bh(&phy->dev->mt76.lock); 837 skb = __skb_dequeue(&phy->scan_event_list); 838 spin_unlock_bh(&phy->dev->mt76.lock); 839 840 if (!skb) 841 break; 842 843 rxd = (struct mt7921_mcu_rxd *)skb->data; 844 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) { 845 ieee80211_sched_scan_results(phy->mt76->hw); 846 } else if (test_and_clear_bit(MT76_HW_SCANNING, 847 &phy->mt76->state)) { 848 struct cfg80211_scan_info info = { 849 .aborted = false, 850 }; 851 852 ieee80211_scan_completed(phy->mt76->hw, &info); 853 } 854 dev_kfree_skb(skb); 855 } 856 } 857 858 static int 859 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 860 struct ieee80211_scan_request *req) 861 { 862 struct mt7921_dev *dev = mt7921_hw_dev(hw); 863 struct mt76_phy *mphy = hw->priv; 864 int err; 865 866 mutex_lock(&dev->mt76.mutex); 867 err = mt7921_mcu_hw_scan(mphy->priv, vif, req); 868 mutex_unlock(&dev->mt76.mutex); 869 870 return err; 871 } 872 873 static void 874 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 875 { 876 struct mt7921_dev *dev = mt7921_hw_dev(hw); 877 struct mt76_phy *mphy = hw->priv; 878 879 mutex_lock(&dev->mt76.mutex); 880 mt7921_mcu_cancel_hw_scan(mphy->priv, vif); 881 mutex_unlock(&dev->mt76.mutex); 882 } 883 884 static int 885 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 886 struct cfg80211_sched_scan_request *req, 887 struct ieee80211_scan_ies *ies) 888 { 889 struct mt7921_dev *dev = mt7921_hw_dev(hw); 890 struct mt76_phy *mphy = hw->priv; 891 int err; 892 893 mutex_lock(&dev->mt76.mutex); 894 895 err = mt7921_mcu_sched_scan_req(mphy->priv, vif, req); 896 if (err < 0) 897 goto out; 898 899 err = mt7921_mcu_sched_scan_enable(mphy->priv, vif, true); 900 out: 901 mutex_unlock(&dev->mt76.mutex); 902 903 return err; 904 } 905 906 static int 907 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 908 { 909 struct mt7921_dev *dev = mt7921_hw_dev(hw); 910 struct mt76_phy *mphy = hw->priv; 911 int err; 912 913 mutex_lock(&dev->mt76.mutex); 914 err = mt7921_mcu_sched_scan_enable(mphy->priv, vif, false); 915 mutex_unlock(&dev->mt76.mutex); 916 917 return err; 918 } 919 920 static int 921 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant) 922 { 923 struct mt7921_dev *dev = mt7921_hw_dev(hw); 924 struct mt7921_phy *phy = mt7921_hw_phy(hw); 925 int max_nss = hweight8(hw->wiphy->available_antennas_tx); 926 927 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss) 928 return -EINVAL; 929 930 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant) 931 tx_ant = BIT(ffs(tx_ant) - 1) - 1; 932 933 mutex_lock(&dev->mt76.mutex); 934 935 phy->mt76->antenna_mask = tx_ant; 936 phy->mt76->chainmask = tx_ant; 937 938 mt76_set_stream_caps(phy->mt76, true); 939 mt7921_set_stream_he_caps(phy); 940 941 mutex_unlock(&dev->mt76.mutex); 942 943 return 0; 944 } 945 946 static void 947 mt7921_sta_rc_update(struct ieee80211_hw *hw, 948 struct ieee80211_vif *vif, 949 struct ieee80211_sta *sta, 950 u32 changed) 951 { 952 } 953 954 static void mt7921_sta_statistics(struct ieee80211_hw *hw, 955 struct ieee80211_vif *vif, 956 struct ieee80211_sta *sta, 957 struct station_info *sinfo) 958 { 959 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 960 struct mt7921_sta_stats *stats = &msta->stats; 961 962 if (!stats->tx_rate.legacy && !stats->tx_rate.flags) 963 return; 964 965 if (stats->tx_rate.legacy) { 966 sinfo->txrate.legacy = stats->tx_rate.legacy; 967 } else { 968 sinfo->txrate.mcs = stats->tx_rate.mcs; 969 sinfo->txrate.nss = stats->tx_rate.nss; 970 sinfo->txrate.bw = stats->tx_rate.bw; 971 sinfo->txrate.he_gi = stats->tx_rate.he_gi; 972 sinfo->txrate.he_dcm = stats->tx_rate.he_dcm; 973 sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc; 974 } 975 sinfo->txrate.flags = stats->tx_rate.flags; 976 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 977 } 978 979 #ifdef CONFIG_PM 980 static int mt7921_suspend(struct ieee80211_hw *hw, 981 struct cfg80211_wowlan *wowlan) 982 { 983 struct mt7921_dev *dev = mt7921_hw_dev(hw); 984 struct mt7921_phy *phy = mt7921_hw_phy(hw); 985 int err; 986 987 cancel_delayed_work_sync(&phy->scan_work); 988 cancel_delayed_work_sync(&phy->mt76->mac_work); 989 990 mutex_lock(&dev->mt76.mutex); 991 992 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 993 994 set_bit(MT76_STATE_SUSPEND, &phy->mt76->state); 995 ieee80211_iterate_active_interfaces(hw, 996 IEEE80211_IFACE_ITER_RESUME_ALL, 997 mt7921_mcu_set_suspend_iter, phy); 998 999 err = mt7921_mcu_set_hif_suspend(dev, true); 1000 1001 mutex_unlock(&dev->mt76.mutex); 1002 1003 return err; 1004 } 1005 1006 static int mt7921_resume(struct ieee80211_hw *hw) 1007 { 1008 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1009 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1010 int err; 1011 1012 mutex_lock(&dev->mt76.mutex); 1013 1014 err = mt7921_mcu_set_hif_suspend(dev, false); 1015 if (err < 0) 1016 goto out; 1017 1018 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1019 clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state); 1020 ieee80211_iterate_active_interfaces(hw, 1021 IEEE80211_IFACE_ITER_RESUME_ALL, 1022 mt7921_mcu_set_suspend_iter, phy); 1023 1024 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, 1025 MT7921_WATCHDOG_TIME); 1026 out: 1027 mutex_unlock(&dev->mt76.mutex); 1028 1029 return err; 1030 } 1031 1032 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled) 1033 { 1034 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1035 struct mt76_dev *mdev = &dev->mt76; 1036 1037 device_set_wakeup_enable(mdev->dev, enabled); 1038 } 1039 1040 static void mt7921_set_rekey_data(struct ieee80211_hw *hw, 1041 struct ieee80211_vif *vif, 1042 struct cfg80211_gtk_rekey_data *data) 1043 { 1044 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1045 1046 mutex_lock(&dev->mt76.mutex); 1047 mt7921_mcu_update_gtk_rekey(hw, vif, data); 1048 mutex_unlock(&dev->mt76.mutex); 1049 } 1050 #endif /* CONFIG_PM */ 1051 1052 const struct ieee80211_ops mt7921_ops = { 1053 .tx = mt7921_tx, 1054 .start = mt7921_start, 1055 .stop = mt7921_stop, 1056 .add_interface = mt7921_add_interface, 1057 .remove_interface = mt7921_remove_interface, 1058 .config = mt7921_config, 1059 .conf_tx = mt7921_conf_tx, 1060 .configure_filter = mt7921_configure_filter, 1061 .bss_info_changed = mt7921_bss_info_changed, 1062 .sta_add = mt7921_sta_add, 1063 .sta_remove = mt7921_sta_remove, 1064 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 1065 .sta_rc_update = mt7921_sta_rc_update, 1066 .set_key = mt7921_set_key, 1067 .ampdu_action = mt7921_ampdu_action, 1068 .set_rts_threshold = mt7921_set_rts_threshold, 1069 .wake_tx_queue = mt76_wake_tx_queue, 1070 .release_buffered_frames = mt76_release_buffered_frames, 1071 .get_txpower = mt76_get_txpower, 1072 .get_stats = mt7921_get_stats, 1073 .get_tsf = mt7921_get_tsf, 1074 .set_tsf = mt7921_set_tsf, 1075 .get_survey = mt76_get_survey, 1076 .get_antenna = mt76_get_antenna, 1077 .set_antenna = mt7921_set_antenna, 1078 .set_coverage_class = mt7921_set_coverage_class, 1079 .hw_scan = mt7921_hw_scan, 1080 .cancel_hw_scan = mt7921_cancel_hw_scan, 1081 .sta_statistics = mt7921_sta_statistics, 1082 .sched_scan_start = mt7921_start_sched_scan, 1083 .sched_scan_stop = mt7921_stop_sched_scan, 1084 #ifdef CONFIG_PM 1085 .suspend = mt7921_suspend, 1086 .resume = mt7921_resume, 1087 .set_wakeup = mt7921_set_wakeup, 1088 .set_rekey_data = mt7921_set_rekey_data, 1089 #endif /* CONFIG_PM */ 1090 }; 1091