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 memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac)); 756 757 mt7921_mcu_sta_update(dev, sta, vif, true, MT76_STA_INFO_STATE_ASSOC); 758 759 mt7921_mutex_release(dev); 760 } 761 EXPORT_SYMBOL_GPL(mt7921_mac_sta_assoc); 762 763 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif, 764 struct ieee80211_sta *sta) 765 { 766 struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76); 767 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 768 769 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid); 770 mt76_connac_pm_wake(&dev->mphy, &dev->pm); 771 772 mt7921_mcu_sta_update(dev, sta, vif, false, MT76_STA_INFO_STATE_NONE); 773 mt7921_mac_wtbl_update(dev, msta->wcid.idx, 774 MT_WTBL_UPDATE_ADM_COUNT_CLEAR); 775 776 if (vif->type == NL80211_IFTYPE_STATION) { 777 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 778 779 mvif->wep_sta = NULL; 780 ewma_rssi_init(&mvif->rssi); 781 if (!sta->tdls) 782 mt76_connac_mcu_uni_add_bss(&dev->mphy, vif, 783 &mvif->sta.wcid, false); 784 } 785 786 spin_lock_bh(&dev->sta_poll_lock); 787 if (!list_empty(&msta->poll_list)) 788 list_del_init(&msta->poll_list); 789 spin_unlock_bh(&dev->sta_poll_lock); 790 791 mt76_connac_power_save_sched(&dev->mphy, &dev->pm); 792 } 793 EXPORT_SYMBOL_GPL(mt7921_mac_sta_remove); 794 795 void mt7921_tx_worker(struct mt76_worker *w) 796 { 797 struct mt7921_dev *dev = container_of(w, struct mt7921_dev, 798 mt76.tx_worker); 799 800 if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) { 801 queue_work(dev->mt76.wq, &dev->pm.wake_work); 802 return; 803 } 804 805 mt76_txq_schedule_all(&dev->mphy); 806 mt76_connac_pm_unref(&dev->mphy, &dev->pm); 807 } 808 809 static void mt7921_tx(struct ieee80211_hw *hw, 810 struct ieee80211_tx_control *control, 811 struct sk_buff *skb) 812 { 813 struct mt7921_dev *dev = mt7921_hw_dev(hw); 814 struct mt76_phy *mphy = hw->priv; 815 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 816 struct ieee80211_vif *vif = info->control.vif; 817 struct mt76_wcid *wcid = &dev->mt76.global_wcid; 818 int qid; 819 820 if (control->sta) { 821 struct mt7921_sta *sta; 822 823 sta = (struct mt7921_sta *)control->sta->drv_priv; 824 wcid = &sta->wcid; 825 } 826 827 if (vif && !control->sta) { 828 struct mt7921_vif *mvif; 829 830 mvif = (struct mt7921_vif *)vif->drv_priv; 831 wcid = &mvif->sta.wcid; 832 } 833 834 if (mt76_connac_pm_ref(mphy, &dev->pm)) { 835 mt76_tx(mphy, control->sta, wcid, skb); 836 mt76_connac_pm_unref(mphy, &dev->pm); 837 return; 838 } 839 840 qid = skb_get_queue_mapping(skb); 841 if (qid >= MT_TXQ_PSD) { 842 qid = IEEE80211_AC_BE; 843 skb_set_queue_mapping(skb, qid); 844 } 845 846 mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb); 847 } 848 849 static int mt7921_set_rts_threshold(struct ieee80211_hw *hw, u32 val) 850 { 851 struct mt7921_dev *dev = mt7921_hw_dev(hw); 852 853 mt7921_mutex_acquire(dev); 854 mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, 0); 855 mt7921_mutex_release(dev); 856 857 return 0; 858 } 859 860 static int 861 mt7921_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 862 struct ieee80211_ampdu_params *params) 863 { 864 enum ieee80211_ampdu_mlme_action action = params->action; 865 struct mt7921_dev *dev = mt7921_hw_dev(hw); 866 struct ieee80211_sta *sta = params->sta; 867 struct ieee80211_txq *txq = sta->txq[params->tid]; 868 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 869 u16 tid = params->tid; 870 u16 ssn = params->ssn; 871 struct mt76_txq *mtxq; 872 int ret = 0; 873 874 if (!txq) 875 return -EINVAL; 876 877 mtxq = (struct mt76_txq *)txq->drv_priv; 878 879 mt7921_mutex_acquire(dev); 880 switch (action) { 881 case IEEE80211_AMPDU_RX_START: 882 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn, 883 params->buf_size); 884 mt7921_mcu_uni_rx_ba(dev, params, true); 885 break; 886 case IEEE80211_AMPDU_RX_STOP: 887 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid); 888 mt7921_mcu_uni_rx_ba(dev, params, false); 889 break; 890 case IEEE80211_AMPDU_TX_OPERATIONAL: 891 mtxq->aggr = true; 892 mtxq->send_bar = false; 893 mt7921_mcu_uni_tx_ba(dev, params, true); 894 break; 895 case IEEE80211_AMPDU_TX_STOP_FLUSH: 896 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 897 mtxq->aggr = false; 898 clear_bit(tid, &msta->ampdu_state); 899 mt7921_mcu_uni_tx_ba(dev, params, false); 900 break; 901 case IEEE80211_AMPDU_TX_START: 902 set_bit(tid, &msta->ampdu_state); 903 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 904 break; 905 case IEEE80211_AMPDU_TX_STOP_CONT: 906 mtxq->aggr = false; 907 clear_bit(tid, &msta->ampdu_state); 908 mt7921_mcu_uni_tx_ba(dev, params, false); 909 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 910 break; 911 } 912 mt7921_mutex_release(dev); 913 914 return ret; 915 } 916 917 static int mt7921_sta_state(struct ieee80211_hw *hw, 918 struct ieee80211_vif *vif, 919 struct ieee80211_sta *sta, 920 enum ieee80211_sta_state old_state, 921 enum ieee80211_sta_state new_state) 922 { 923 struct mt7921_dev *dev = mt7921_hw_dev(hw); 924 925 if (dev->pm.ds_enable) { 926 mt7921_mutex_acquire(dev); 927 mt76_connac_sta_state_dp(&dev->mt76, old_state, new_state); 928 mt7921_mutex_release(dev); 929 } 930 931 return mt76_sta_state(hw, vif, sta, old_state, new_state); 932 } 933 934 static int 935 mt7921_get_stats(struct ieee80211_hw *hw, 936 struct ieee80211_low_level_stats *stats) 937 { 938 struct mt7921_phy *phy = mt7921_hw_phy(hw); 939 struct mib_stats *mib = &phy->mib; 940 941 mt7921_mutex_acquire(phy->dev); 942 943 stats->dot11RTSSuccessCount = mib->rts_cnt; 944 stats->dot11RTSFailureCount = mib->rts_retries_cnt; 945 stats->dot11FCSErrorCount = mib->fcs_err_cnt; 946 stats->dot11ACKFailureCount = mib->ack_fail_cnt; 947 948 mt7921_mutex_release(phy->dev); 949 950 return 0; 951 } 952 953 static const char mt7921_gstrings_stats[][ETH_GSTRING_LEN] = { 954 /* tx counters */ 955 "tx_ampdu_cnt", 956 "tx_mpdu_attempts", 957 "tx_mpdu_success", 958 "tx_pkt_ebf_cnt", 959 "tx_pkt_ibf_cnt", 960 "tx_ampdu_len:0-1", 961 "tx_ampdu_len:2-10", 962 "tx_ampdu_len:11-19", 963 "tx_ampdu_len:20-28", 964 "tx_ampdu_len:29-37", 965 "tx_ampdu_len:38-46", 966 "tx_ampdu_len:47-55", 967 "tx_ampdu_len:56-79", 968 "tx_ampdu_len:80-103", 969 "tx_ampdu_len:104-127", 970 "tx_ampdu_len:128-151", 971 "tx_ampdu_len:152-175", 972 "tx_ampdu_len:176-199", 973 "tx_ampdu_len:200-223", 974 "tx_ampdu_len:224-247", 975 "ba_miss_count", 976 "tx_beamformer_ppdu_iBF", 977 "tx_beamformer_ppdu_eBF", 978 "tx_beamformer_rx_feedback_all", 979 "tx_beamformer_rx_feedback_he", 980 "tx_beamformer_rx_feedback_vht", 981 "tx_beamformer_rx_feedback_ht", 982 "tx_msdu_pack_1", 983 "tx_msdu_pack_2", 984 "tx_msdu_pack_3", 985 "tx_msdu_pack_4", 986 "tx_msdu_pack_5", 987 "tx_msdu_pack_6", 988 "tx_msdu_pack_7", 989 "tx_msdu_pack_8", 990 /* rx counters */ 991 "rx_mpdu_cnt", 992 "rx_ampdu_cnt", 993 "rx_ampdu_bytes_cnt", 994 "rx_ba_cnt", 995 /* per vif counters */ 996 "v_tx_mode_cck", 997 "v_tx_mode_ofdm", 998 "v_tx_mode_ht", 999 "v_tx_mode_ht_gf", 1000 "v_tx_mode_vht", 1001 "v_tx_mode_he_su", 1002 "v_tx_mode_he_ext_su", 1003 "v_tx_mode_he_tb", 1004 "v_tx_mode_he_mu", 1005 "v_tx_bw_20", 1006 "v_tx_bw_40", 1007 "v_tx_bw_80", 1008 "v_tx_bw_160", 1009 "v_tx_mcs_0", 1010 "v_tx_mcs_1", 1011 "v_tx_mcs_2", 1012 "v_tx_mcs_3", 1013 "v_tx_mcs_4", 1014 "v_tx_mcs_5", 1015 "v_tx_mcs_6", 1016 "v_tx_mcs_7", 1017 "v_tx_mcs_8", 1018 "v_tx_mcs_9", 1019 "v_tx_mcs_10", 1020 "v_tx_mcs_11", 1021 }; 1022 1023 static void 1024 mt7921_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1025 u32 sset, u8 *data) 1026 { 1027 if (sset != ETH_SS_STATS) 1028 return; 1029 1030 memcpy(data, *mt7921_gstrings_stats, sizeof(mt7921_gstrings_stats)); 1031 } 1032 1033 static int 1034 mt7921_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1035 int sset) 1036 { 1037 return sset == ETH_SS_STATS ? ARRAY_SIZE(mt7921_gstrings_stats) : 0; 1038 } 1039 1040 static void 1041 mt7921_ethtool_worker(void *wi_data, struct ieee80211_sta *sta) 1042 { 1043 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 1044 struct mt76_ethtool_worker_info *wi = wi_data; 1045 1046 if (msta->vif->mt76.idx != wi->idx) 1047 return; 1048 1049 mt76_ethtool_worker(wi, &msta->wcid.stats); 1050 } 1051 1052 static 1053 void mt7921_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1054 struct ethtool_stats *stats, u64 *data) 1055 { 1056 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1057 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1058 struct mt7921_dev *dev = phy->dev; 1059 struct mib_stats *mib = &phy->mib; 1060 struct mt76_ethtool_worker_info wi = { 1061 .data = data, 1062 .idx = mvif->mt76.idx, 1063 }; 1064 int i, ei = 0; 1065 1066 mt7921_mutex_acquire(dev); 1067 1068 mt7921_mac_update_mib_stats(phy); 1069 1070 data[ei++] = mib->tx_ampdu_cnt; 1071 data[ei++] = mib->tx_mpdu_attempts_cnt; 1072 data[ei++] = mib->tx_mpdu_success_cnt; 1073 data[ei++] = mib->tx_pkt_ebf_cnt; 1074 data[ei++] = mib->tx_pkt_ibf_cnt; 1075 1076 /* Tx ampdu stat */ 1077 for (i = 0; i < 15; i++) 1078 data[ei++] = dev->mt76.aggr_stats[i]; 1079 1080 data[ei++] = phy->mib.ba_miss_cnt; 1081 1082 /* Tx Beamformer monitor */ 1083 data[ei++] = mib->tx_bf_ibf_ppdu_cnt; 1084 data[ei++] = mib->tx_bf_ebf_ppdu_cnt; 1085 1086 /* Tx Beamformer Rx feedback monitor */ 1087 data[ei++] = mib->tx_bf_rx_fb_all_cnt; 1088 data[ei++] = mib->tx_bf_rx_fb_he_cnt; 1089 data[ei++] = mib->tx_bf_rx_fb_vht_cnt; 1090 data[ei++] = mib->tx_bf_rx_fb_ht_cnt; 1091 1092 /* Tx amsdu info (pack-count histogram) */ 1093 for (i = 0; i < ARRAY_SIZE(mib->tx_amsdu); i++) 1094 data[ei++] = mib->tx_amsdu[i]; 1095 1096 /* rx counters */ 1097 data[ei++] = mib->rx_mpdu_cnt; 1098 data[ei++] = mib->rx_ampdu_cnt; 1099 data[ei++] = mib->rx_ampdu_bytes_cnt; 1100 data[ei++] = mib->rx_ba_cnt; 1101 1102 /* Add values for all stations owned by this vif */ 1103 wi.initial_stat_idx = ei; 1104 ieee80211_iterate_stations_atomic(hw, mt7921_ethtool_worker, &wi); 1105 1106 mt7921_mutex_release(dev); 1107 1108 if (!wi.sta_count) 1109 return; 1110 1111 ei += wi.worker_stat_count; 1112 if (ei != ARRAY_SIZE(mt7921_gstrings_stats)) 1113 dev_err(dev->mt76.dev, "ei: %d SSTATS_LEN: %zu", 1114 ei, ARRAY_SIZE(mt7921_gstrings_stats)); 1115 } 1116 1117 static u64 1118 mt7921_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1119 { 1120 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1121 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1122 u8 omac_idx = mvif->mt76.omac_idx; 1123 union { 1124 u64 t64; 1125 u32 t32[2]; 1126 } tsf; 1127 u16 n; 1128 1129 mt7921_mutex_acquire(dev); 1130 1131 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx; 1132 /* TSF software read */ 1133 mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_MODE); 1134 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(0)); 1135 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(0)); 1136 1137 mt7921_mutex_release(dev); 1138 1139 return tsf.t64; 1140 } 1141 1142 static void 1143 mt7921_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1144 u64 timestamp) 1145 { 1146 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1147 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1148 u8 omac_idx = mvif->mt76.omac_idx; 1149 union { 1150 u64 t64; 1151 u32 t32[2]; 1152 } tsf = { .t64 = timestamp, }; 1153 u16 n; 1154 1155 mt7921_mutex_acquire(dev); 1156 1157 n = omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : omac_idx; 1158 mt76_wr(dev, MT_LPON_UTTR0(0), tsf.t32[0]); 1159 mt76_wr(dev, MT_LPON_UTTR1(0), tsf.t32[1]); 1160 /* TSF software overwrite */ 1161 mt76_set(dev, MT_LPON_TCR(0, n), MT_LPON_TCR_SW_WRITE); 1162 1163 mt7921_mutex_release(dev); 1164 } 1165 1166 static void 1167 mt7921_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class) 1168 { 1169 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1170 struct mt7921_dev *dev = phy->dev; 1171 1172 mt7921_mutex_acquire(dev); 1173 phy->coverage_class = max_t(s16, coverage_class, 0); 1174 mt7921_mac_set_timing(phy); 1175 mt7921_mutex_release(dev); 1176 } 1177 1178 void mt7921_scan_work(struct work_struct *work) 1179 { 1180 struct mt7921_phy *phy; 1181 1182 phy = (struct mt7921_phy *)container_of(work, struct mt7921_phy, 1183 scan_work.work); 1184 1185 while (true) { 1186 struct mt76_connac2_mcu_rxd *rxd; 1187 struct sk_buff *skb; 1188 1189 spin_lock_bh(&phy->dev->mt76.lock); 1190 skb = __skb_dequeue(&phy->scan_event_list); 1191 spin_unlock_bh(&phy->dev->mt76.lock); 1192 1193 if (!skb) 1194 break; 1195 1196 rxd = (struct mt76_connac2_mcu_rxd *)skb->data; 1197 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) { 1198 ieee80211_sched_scan_results(phy->mt76->hw); 1199 } else if (test_and_clear_bit(MT76_HW_SCANNING, 1200 &phy->mt76->state)) { 1201 struct cfg80211_scan_info info = { 1202 .aborted = false, 1203 }; 1204 1205 ieee80211_scan_completed(phy->mt76->hw, &info); 1206 } 1207 dev_kfree_skb(skb); 1208 } 1209 } 1210 1211 static int 1212 mt7921_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1213 struct ieee80211_scan_request *req) 1214 { 1215 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1216 struct mt76_phy *mphy = hw->priv; 1217 int err; 1218 1219 mt7921_mutex_acquire(dev); 1220 err = mt76_connac_mcu_hw_scan(mphy, vif, req); 1221 mt7921_mutex_release(dev); 1222 1223 return err; 1224 } 1225 1226 static void 1227 mt7921_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1228 { 1229 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1230 struct mt76_phy *mphy = hw->priv; 1231 1232 mt7921_mutex_acquire(dev); 1233 mt76_connac_mcu_cancel_hw_scan(mphy, vif); 1234 mt7921_mutex_release(dev); 1235 } 1236 1237 static int 1238 mt7921_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1239 struct cfg80211_sched_scan_request *req, 1240 struct ieee80211_scan_ies *ies) 1241 { 1242 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1243 struct mt76_phy *mphy = hw->priv; 1244 int err; 1245 1246 mt7921_mutex_acquire(dev); 1247 1248 err = mt76_connac_mcu_sched_scan_req(mphy, vif, req); 1249 if (err < 0) 1250 goto out; 1251 1252 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true); 1253 out: 1254 mt7921_mutex_release(dev); 1255 1256 return err; 1257 } 1258 1259 static int 1260 mt7921_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1261 { 1262 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1263 struct mt76_phy *mphy = hw->priv; 1264 int err; 1265 1266 mt7921_mutex_acquire(dev); 1267 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false); 1268 mt7921_mutex_release(dev); 1269 1270 return err; 1271 } 1272 1273 static int 1274 mt7921_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant) 1275 { 1276 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1277 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1278 int max_nss = hweight8(hw->wiphy->available_antennas_tx); 1279 1280 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss) 1281 return -EINVAL; 1282 1283 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant) 1284 tx_ant = BIT(ffs(tx_ant) - 1) - 1; 1285 1286 mt7921_mutex_acquire(dev); 1287 1288 phy->mt76->antenna_mask = tx_ant; 1289 phy->mt76->chainmask = tx_ant; 1290 1291 mt76_set_stream_caps(phy->mt76, true); 1292 mt7921_set_stream_he_caps(phy); 1293 1294 mt7921_mutex_release(dev); 1295 1296 return 0; 1297 } 1298 1299 static void mt7921_sta_statistics(struct ieee80211_hw *hw, 1300 struct ieee80211_vif *vif, 1301 struct ieee80211_sta *sta, 1302 struct station_info *sinfo) 1303 { 1304 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 1305 struct rate_info *txrate = &msta->wcid.rate; 1306 1307 if (!txrate->legacy && !txrate->flags) 1308 return; 1309 1310 if (txrate->legacy) { 1311 sinfo->txrate.legacy = txrate->legacy; 1312 } else { 1313 sinfo->txrate.mcs = txrate->mcs; 1314 sinfo->txrate.nss = txrate->nss; 1315 sinfo->txrate.bw = txrate->bw; 1316 sinfo->txrate.he_gi = txrate->he_gi; 1317 sinfo->txrate.he_dcm = txrate->he_dcm; 1318 sinfo->txrate.he_ru_alloc = txrate->he_ru_alloc; 1319 } 1320 sinfo->txrate.flags = txrate->flags; 1321 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 1322 } 1323 1324 #ifdef CONFIG_PM 1325 static int mt7921_suspend(struct ieee80211_hw *hw, 1326 struct cfg80211_wowlan *wowlan) 1327 { 1328 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1329 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1330 1331 cancel_delayed_work_sync(&phy->scan_work); 1332 cancel_delayed_work_sync(&phy->mt76->mac_work); 1333 1334 cancel_delayed_work_sync(&dev->pm.ps_work); 1335 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL); 1336 1337 mt7921_mutex_acquire(dev); 1338 1339 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1340 ieee80211_iterate_active_interfaces(hw, 1341 IEEE80211_IFACE_ITER_RESUME_ALL, 1342 mt7921_mcu_set_suspend_iter, 1343 &dev->mphy); 1344 1345 mt7921_mutex_release(dev); 1346 1347 return 0; 1348 } 1349 1350 static int mt7921_resume(struct ieee80211_hw *hw) 1351 { 1352 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1353 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1354 1355 mt7921_mutex_acquire(dev); 1356 1357 set_bit(MT76_STATE_RUNNING, &phy->mt76->state); 1358 ieee80211_iterate_active_interfaces(hw, 1359 IEEE80211_IFACE_ITER_RESUME_ALL, 1360 mt76_connac_mcu_set_suspend_iter, 1361 &dev->mphy); 1362 1363 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, 1364 MT7921_WATCHDOG_TIME); 1365 1366 mt7921_mutex_release(dev); 1367 1368 return 0; 1369 } 1370 1371 static void mt7921_set_wakeup(struct ieee80211_hw *hw, bool enabled) 1372 { 1373 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1374 struct mt76_dev *mdev = &dev->mt76; 1375 1376 device_set_wakeup_enable(mdev->dev, enabled); 1377 } 1378 1379 static void mt7921_set_rekey_data(struct ieee80211_hw *hw, 1380 struct ieee80211_vif *vif, 1381 struct cfg80211_gtk_rekey_data *data) 1382 { 1383 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1384 1385 mt7921_mutex_acquire(dev); 1386 mt76_connac_mcu_update_gtk_rekey(hw, vif, data); 1387 mt7921_mutex_release(dev); 1388 } 1389 #endif /* CONFIG_PM */ 1390 1391 static void mt7921_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1392 u32 queues, bool drop) 1393 { 1394 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1395 1396 wait_event_timeout(dev->mt76.tx_wait, !mt76_has_tx_pending(&dev->mphy), 1397 HZ / 2); 1398 } 1399 1400 static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw, 1401 struct ieee80211_vif *vif, 1402 struct ieee80211_sta *sta, 1403 bool enabled) 1404 { 1405 struct mt7921_sta *msta = (struct mt7921_sta *)sta->drv_priv; 1406 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1407 1408 mt7921_mutex_acquire(dev); 1409 1410 if (enabled) 1411 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags); 1412 else 1413 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags); 1414 1415 mt76_connac_mcu_sta_update_hdr_trans(&dev->mt76, vif, &msta->wcid, 1416 MCU_UNI_CMD(STA_REC_UPDATE)); 1417 1418 mt7921_mutex_release(dev); 1419 } 1420 1421 #if IS_ENABLED(CONFIG_IPV6) 1422 static void mt7921_ipv6_addr_change(struct ieee80211_hw *hw, 1423 struct ieee80211_vif *vif, 1424 struct inet6_dev *idev) 1425 { 1426 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1427 struct mt7921_dev *dev = mvif->phy->dev; 1428 struct inet6_ifaddr *ifa; 1429 struct in6_addr ns_addrs[IEEE80211_BSS_ARP_ADDR_LIST_LEN]; 1430 struct sk_buff *skb; 1431 u8 i, idx = 0; 1432 1433 struct { 1434 struct { 1435 u8 bss_idx; 1436 u8 pad[3]; 1437 } __packed hdr; 1438 struct mt76_connac_arpns_tlv arpns; 1439 } req_hdr = { 1440 .hdr = { 1441 .bss_idx = mvif->mt76.idx, 1442 }, 1443 .arpns = { 1444 .tag = cpu_to_le16(UNI_OFFLOAD_OFFLOAD_ND), 1445 .mode = 2, /* update */ 1446 .option = 1, /* update only */ 1447 }, 1448 }; 1449 1450 read_lock_bh(&idev->lock); 1451 list_for_each_entry(ifa, &idev->addr_list, if_list) { 1452 if (ifa->flags & IFA_F_TENTATIVE) 1453 continue; 1454 ns_addrs[idx] = ifa->addr; 1455 if (++idx >= IEEE80211_BSS_ARP_ADDR_LIST_LEN) 1456 break; 1457 } 1458 read_unlock_bh(&idev->lock); 1459 1460 if (!idx) 1461 return; 1462 1463 req_hdr.arpns.ips_num = idx; 1464 req_hdr.arpns.len = cpu_to_le16(sizeof(struct mt76_connac_arpns_tlv) 1465 + idx * sizeof(struct in6_addr)); 1466 skb = __mt76_mcu_msg_alloc(&dev->mt76, &req_hdr, 1467 sizeof(req_hdr) + idx * sizeof(struct in6_addr), 1468 sizeof(req_hdr), GFP_ATOMIC); 1469 if (!skb) 1470 return; 1471 1472 for (i = 0; i < idx; i++) 1473 skb_put_data(skb, &ns_addrs[i].in6_u, sizeof(struct in6_addr)); 1474 1475 skb_queue_tail(&dev->ipv6_ns_list, skb); 1476 1477 ieee80211_queue_work(dev->mt76.hw, &dev->ipv6_ns_work); 1478 } 1479 #endif 1480 1481 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw, 1482 const struct cfg80211_sar_specs *sar) 1483 { 1484 struct mt76_phy *mphy = hw->priv; 1485 int err; 1486 1487 if (sar) { 1488 err = mt76_init_sar_power(hw, sar); 1489 if (err) 1490 return err; 1491 } 1492 1493 mt7921_init_acpi_sar_power(mt7921_hw_phy(hw), !sar); 1494 1495 err = mt76_connac_mcu_set_rate_txpower(mphy); 1496 1497 return err; 1498 } 1499 1500 static int mt7921_set_sar_specs(struct ieee80211_hw *hw, 1501 const struct cfg80211_sar_specs *sar) 1502 { 1503 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1504 int err; 1505 1506 mt7921_mutex_acquire(dev); 1507 err = mt7921_set_tx_sar_pwr(hw, sar); 1508 mt7921_mutex_release(dev); 1509 1510 return err; 1511 } 1512 1513 static void 1514 mt7921_channel_switch_beacon(struct ieee80211_hw *hw, 1515 struct ieee80211_vif *vif, 1516 struct cfg80211_chan_def *chandef) 1517 { 1518 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1519 1520 mt7921_mutex_acquire(dev); 1521 mt7921_mcu_uni_add_beacon_offload(dev, hw, vif, true); 1522 mt7921_mutex_release(dev); 1523 } 1524 1525 static int 1526 mt7921_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1527 struct ieee80211_bss_conf *link_conf) 1528 { 1529 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1530 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1531 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1532 int err; 1533 1534 mt7921_mutex_acquire(dev); 1535 1536 err = mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, 1537 true); 1538 if (err) 1539 goto out; 1540 1541 err = mt7921_mcu_set_bss_pm(dev, vif, true); 1542 if (err) 1543 goto out; 1544 1545 err = mt7921_mcu_sta_update(dev, NULL, vif, true, 1546 MT76_STA_INFO_STATE_NONE); 1547 out: 1548 mt7921_mutex_release(dev); 1549 1550 return err; 1551 } 1552 1553 static void 1554 mt7921_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1555 struct ieee80211_bss_conf *link_conf) 1556 { 1557 struct mt7921_vif *mvif = (struct mt7921_vif *)vif->drv_priv; 1558 struct mt7921_phy *phy = mt7921_hw_phy(hw); 1559 struct mt7921_dev *dev = mt7921_hw_dev(hw); 1560 int err; 1561 1562 mt7921_mutex_acquire(dev); 1563 1564 err = mt7921_mcu_set_bss_pm(dev, vif, false); 1565 if (err) 1566 goto out; 1567 1568 mt76_connac_mcu_uni_add_bss(phy->mt76, vif, &mvif->sta.wcid, false); 1569 1570 out: 1571 mt7921_mutex_release(dev); 1572 } 1573 1574 const struct ieee80211_ops mt7921_ops = { 1575 .tx = mt7921_tx, 1576 .start = mt7921_start, 1577 .stop = mt7921_stop, 1578 .add_interface = mt7921_add_interface, 1579 .remove_interface = mt7921_remove_interface, 1580 .config = mt7921_config, 1581 .conf_tx = mt7921_conf_tx, 1582 .configure_filter = mt7921_configure_filter, 1583 .bss_info_changed = mt7921_bss_info_changed, 1584 .start_ap = mt7921_start_ap, 1585 .stop_ap = mt7921_stop_ap, 1586 .sta_state = mt7921_sta_state, 1587 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove, 1588 .set_key = mt7921_set_key, 1589 .sta_set_decap_offload = mt7921_sta_set_decap_offload, 1590 #if IS_ENABLED(CONFIG_IPV6) 1591 .ipv6_addr_change = mt7921_ipv6_addr_change, 1592 #endif /* CONFIG_IPV6 */ 1593 .ampdu_action = mt7921_ampdu_action, 1594 .set_rts_threshold = mt7921_set_rts_threshold, 1595 .wake_tx_queue = mt76_wake_tx_queue, 1596 .release_buffered_frames = mt76_release_buffered_frames, 1597 .channel_switch_beacon = mt7921_channel_switch_beacon, 1598 .get_txpower = mt76_get_txpower, 1599 .get_stats = mt7921_get_stats, 1600 .get_et_sset_count = mt7921_get_et_sset_count, 1601 .get_et_strings = mt7921_get_et_strings, 1602 .get_et_stats = mt7921_get_et_stats, 1603 .get_tsf = mt7921_get_tsf, 1604 .set_tsf = mt7921_set_tsf, 1605 .get_survey = mt76_get_survey, 1606 .get_antenna = mt76_get_antenna, 1607 .set_antenna = mt7921_set_antenna, 1608 .set_coverage_class = mt7921_set_coverage_class, 1609 .hw_scan = mt7921_hw_scan, 1610 .cancel_hw_scan = mt7921_cancel_hw_scan, 1611 .sta_statistics = mt7921_sta_statistics, 1612 .sched_scan_start = mt7921_start_sched_scan, 1613 .sched_scan_stop = mt7921_stop_sched_scan, 1614 CFG80211_TESTMODE_CMD(mt7921_testmode_cmd) 1615 CFG80211_TESTMODE_DUMP(mt7921_testmode_dump) 1616 #ifdef CONFIG_PM 1617 .suspend = mt7921_suspend, 1618 .resume = mt7921_resume, 1619 .set_wakeup = mt7921_set_wakeup, 1620 .set_rekey_data = mt7921_set_rekey_data, 1621 #endif /* CONFIG_PM */ 1622 .flush = mt7921_flush, 1623 .set_sar_specs = mt7921_set_sar_specs, 1624 }; 1625 EXPORT_SYMBOL_GPL(mt7921_ops); 1626 1627 MODULE_LICENSE("Dual BSD/GPL"); 1628 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); 1629