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