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