1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2017 Intel Deutschland GmbH 4 * Copyright (C) 2018-2020 Intel Corporation 5 */ 6 #include "rs.h" 7 #include "fw-api.h" 8 #include "sta.h" 9 #include "iwl-op-mode.h" 10 #include "mvm.h" 11 12 static u8 rs_fw_bw_from_sta_bw(struct ieee80211_sta *sta) 13 { 14 switch (sta->bandwidth) { 15 case IEEE80211_STA_RX_BW_160: 16 return IWL_TLC_MNG_CH_WIDTH_160MHZ; 17 case IEEE80211_STA_RX_BW_80: 18 return IWL_TLC_MNG_CH_WIDTH_80MHZ; 19 case IEEE80211_STA_RX_BW_40: 20 return IWL_TLC_MNG_CH_WIDTH_40MHZ; 21 case IEEE80211_STA_RX_BW_20: 22 default: 23 return IWL_TLC_MNG_CH_WIDTH_20MHZ; 24 } 25 } 26 27 static u8 rs_fw_set_active_chains(u8 chains) 28 { 29 u8 fw_chains = 0; 30 31 if (chains & ANT_A) 32 fw_chains |= IWL_TLC_MNG_CHAIN_A_MSK; 33 if (chains & ANT_B) 34 fw_chains |= IWL_TLC_MNG_CHAIN_B_MSK; 35 if (chains & ANT_C) 36 WARN(false, 37 "tlc offload doesn't support antenna C. chains: 0x%x\n", 38 chains); 39 40 return fw_chains; 41 } 42 43 static u8 rs_fw_sgi_cw_support(struct ieee80211_sta *sta) 44 { 45 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; 46 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; 47 struct ieee80211_sta_he_cap *he_cap = &sta->he_cap; 48 u8 supp = 0; 49 50 if (he_cap->has_he) 51 return 0; 52 53 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20) 54 supp |= BIT(IWL_TLC_MNG_CH_WIDTH_20MHZ); 55 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40) 56 supp |= BIT(IWL_TLC_MNG_CH_WIDTH_40MHZ); 57 if (vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80) 58 supp |= BIT(IWL_TLC_MNG_CH_WIDTH_80MHZ); 59 if (vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_160) 60 supp |= BIT(IWL_TLC_MNG_CH_WIDTH_160MHZ); 61 62 return supp; 63 } 64 65 static u16 rs_fw_get_config_flags(struct iwl_mvm *mvm, 66 struct ieee80211_sta *sta, 67 struct ieee80211_supported_band *sband) 68 { 69 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; 70 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; 71 struct ieee80211_sta_he_cap *he_cap = &sta->he_cap; 72 bool vht_ena = vht_cap->vht_supported; 73 u16 flags = 0; 74 75 if (mvm->cfg->ht_params->stbc && 76 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1)) { 77 if (he_cap->has_he) { 78 if (he_cap->he_cap_elem.phy_cap_info[2] & 79 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ) 80 flags |= IWL_TLC_MNG_CFG_FLAGS_STBC_MSK; 81 82 if (he_cap->he_cap_elem.phy_cap_info[7] & 83 IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ) 84 flags |= IWL_TLC_MNG_CFG_FLAGS_HE_STBC_160MHZ_MSK; 85 } else if ((ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) || 86 (vht_ena && 87 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))) 88 flags |= IWL_TLC_MNG_CFG_FLAGS_STBC_MSK; 89 } 90 91 if (mvm->cfg->ht_params->ldpc && 92 ((ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING) || 93 (vht_ena && (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC)))) 94 flags |= IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK; 95 96 /* consider LDPC support in case of HE */ 97 if (he_cap->has_he && (he_cap->he_cap_elem.phy_cap_info[1] & 98 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)) 99 flags |= IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK; 100 101 if (sband->iftype_data && sband->iftype_data->he_cap.has_he && 102 !(sband->iftype_data->he_cap.he_cap_elem.phy_cap_info[1] & 103 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)) 104 flags &= ~IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK; 105 106 if (he_cap->has_he && 107 (he_cap->he_cap_elem.phy_cap_info[3] & 108 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK)) 109 flags |= IWL_TLC_MNG_CFG_FLAGS_HE_DCM_NSS_1_MSK; 110 111 return flags; 112 } 113 114 static 115 int rs_fw_vht_highest_rx_mcs_index(const struct ieee80211_sta_vht_cap *vht_cap, 116 int nss) 117 { 118 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) & 119 (0x3 << (2 * (nss - 1))); 120 rx_mcs >>= (2 * (nss - 1)); 121 122 switch (rx_mcs) { 123 case IEEE80211_VHT_MCS_SUPPORT_0_7: 124 return IWL_TLC_MNG_HT_RATE_MCS7; 125 case IEEE80211_VHT_MCS_SUPPORT_0_8: 126 return IWL_TLC_MNG_HT_RATE_MCS8; 127 case IEEE80211_VHT_MCS_SUPPORT_0_9: 128 return IWL_TLC_MNG_HT_RATE_MCS9; 129 default: 130 WARN_ON_ONCE(1); 131 break; 132 } 133 134 return 0; 135 } 136 137 static void 138 rs_fw_vht_set_enabled_rates(const struct ieee80211_sta *sta, 139 const struct ieee80211_sta_vht_cap *vht_cap, 140 struct iwl_tlc_config_cmd *cmd) 141 { 142 u16 supp; 143 int i, highest_mcs; 144 u8 max_nss = sta->rx_nss; 145 struct ieee80211_vht_cap ieee_vht_cap = { 146 .vht_cap_info = cpu_to_le32(vht_cap->cap), 147 .supp_mcs = vht_cap->vht_mcs, 148 }; 149 150 /* the station support only a single receive chain */ 151 if (sta->smps_mode == IEEE80211_SMPS_STATIC) 152 max_nss = 1; 153 154 for (i = 0; i < max_nss && i < IWL_TLC_NSS_MAX; i++) { 155 int nss = i + 1; 156 157 highest_mcs = rs_fw_vht_highest_rx_mcs_index(vht_cap, nss); 158 if (!highest_mcs) 159 continue; 160 161 supp = BIT(highest_mcs + 1) - 1; 162 if (sta->bandwidth == IEEE80211_STA_RX_BW_20) 163 supp &= ~BIT(IWL_TLC_MNG_HT_RATE_MCS9); 164 165 cmd->ht_rates[i][IWL_TLC_HT_BW_NONE_160] = cpu_to_le16(supp); 166 /* 167 * Check if VHT extended NSS indicates that the bandwidth/NSS 168 * configuration is supported - only for MCS 0 since we already 169 * decoded the MCS bits anyway ourselves. 170 */ 171 if (sta->bandwidth == IEEE80211_STA_RX_BW_160 && 172 ieee80211_get_vht_max_nss(&ieee_vht_cap, 173 IEEE80211_VHT_CHANWIDTH_160MHZ, 174 0, true, nss) >= nss) 175 cmd->ht_rates[i][IWL_TLC_HT_BW_160] = 176 cmd->ht_rates[i][IWL_TLC_HT_BW_NONE_160]; 177 } 178 } 179 180 static u16 rs_fw_he_ieee80211_mcs_to_rs_mcs(u16 mcs) 181 { 182 switch (mcs) { 183 case IEEE80211_HE_MCS_SUPPORT_0_7: 184 return BIT(IWL_TLC_MNG_HT_RATE_MCS7 + 1) - 1; 185 case IEEE80211_HE_MCS_SUPPORT_0_9: 186 return BIT(IWL_TLC_MNG_HT_RATE_MCS9 + 1) - 1; 187 case IEEE80211_HE_MCS_SUPPORT_0_11: 188 return BIT(IWL_TLC_MNG_HT_RATE_MCS11 + 1) - 1; 189 case IEEE80211_HE_MCS_NOT_SUPPORTED: 190 return 0; 191 } 192 193 WARN(1, "invalid HE MCS %d\n", mcs); 194 return 0; 195 } 196 197 static void 198 rs_fw_he_set_enabled_rates(const struct ieee80211_sta *sta, 199 struct ieee80211_supported_band *sband, 200 struct iwl_tlc_config_cmd *cmd) 201 { 202 const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap; 203 u16 mcs_160 = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160); 204 u16 mcs_80 = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80); 205 u16 tx_mcs_80 = 206 le16_to_cpu(sband->iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_80); 207 u16 tx_mcs_160 = 208 le16_to_cpu(sband->iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_160); 209 int i; 210 u8 nss = sta->rx_nss; 211 212 /* the station support only a single receive chain */ 213 if (sta->smps_mode == IEEE80211_SMPS_STATIC) 214 nss = 1; 215 216 for (i = 0; i < nss && i < IWL_TLC_NSS_MAX; i++) { 217 u16 _mcs_160 = (mcs_160 >> (2 * i)) & 0x3; 218 u16 _mcs_80 = (mcs_80 >> (2 * i)) & 0x3; 219 u16 _tx_mcs_160 = (tx_mcs_160 >> (2 * i)) & 0x3; 220 u16 _tx_mcs_80 = (tx_mcs_80 >> (2 * i)) & 0x3; 221 222 /* If one side doesn't support - mark both as not supporting */ 223 if (_mcs_80 == IEEE80211_HE_MCS_NOT_SUPPORTED || 224 _tx_mcs_80 == IEEE80211_HE_MCS_NOT_SUPPORTED) { 225 _mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED; 226 _tx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED; 227 } 228 if (_mcs_80 > _tx_mcs_80) 229 _mcs_80 = _tx_mcs_80; 230 cmd->ht_rates[i][IWL_TLC_HT_BW_NONE_160] = 231 cpu_to_le16(rs_fw_he_ieee80211_mcs_to_rs_mcs(_mcs_80)); 232 233 /* If one side doesn't support - mark both as not supporting */ 234 if (_mcs_160 == IEEE80211_HE_MCS_NOT_SUPPORTED || 235 _tx_mcs_160 == IEEE80211_HE_MCS_NOT_SUPPORTED) { 236 _mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED; 237 _tx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED; 238 } 239 if (_mcs_160 > _tx_mcs_160) 240 _mcs_160 = _tx_mcs_160; 241 cmd->ht_rates[i][IWL_TLC_HT_BW_160] = 242 cpu_to_le16(rs_fw_he_ieee80211_mcs_to_rs_mcs(_mcs_160)); 243 } 244 } 245 246 static void rs_fw_set_supp_rates(struct ieee80211_sta *sta, 247 struct ieee80211_supported_band *sband, 248 struct iwl_tlc_config_cmd *cmd) 249 { 250 int i; 251 u16 supp = 0; 252 unsigned long tmp; /* must be unsigned long for for_each_set_bit */ 253 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; 254 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; 255 const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap; 256 257 /* non HT rates */ 258 tmp = sta->supp_rates[sband->band]; 259 for_each_set_bit(i, &tmp, BITS_PER_LONG) 260 supp |= BIT(sband->bitrates[i].hw_value); 261 262 cmd->non_ht_rates = cpu_to_le16(supp); 263 cmd->mode = IWL_TLC_MNG_MODE_NON_HT; 264 265 /* HT/VHT rates */ 266 if (he_cap->has_he) { 267 cmd->mode = IWL_TLC_MNG_MODE_HE; 268 rs_fw_he_set_enabled_rates(sta, sband, cmd); 269 } else if (vht_cap->vht_supported) { 270 cmd->mode = IWL_TLC_MNG_MODE_VHT; 271 rs_fw_vht_set_enabled_rates(sta, vht_cap, cmd); 272 } else if (ht_cap->ht_supported) { 273 cmd->mode = IWL_TLC_MNG_MODE_HT; 274 cmd->ht_rates[IWL_TLC_NSS_1][IWL_TLC_HT_BW_NONE_160] = 275 cpu_to_le16(ht_cap->mcs.rx_mask[0]); 276 277 /* the station support only a single receive chain */ 278 if (sta->smps_mode == IEEE80211_SMPS_STATIC) 279 cmd->ht_rates[IWL_TLC_NSS_2][IWL_TLC_HT_BW_NONE_160] = 280 0; 281 else 282 cmd->ht_rates[IWL_TLC_NSS_2][IWL_TLC_HT_BW_NONE_160] = 283 cpu_to_le16(ht_cap->mcs.rx_mask[1]); 284 } 285 } 286 287 void iwl_mvm_tlc_update_notif(struct iwl_mvm *mvm, 288 struct iwl_rx_cmd_buffer *rxb) 289 { 290 struct iwl_rx_packet *pkt = rxb_addr(rxb); 291 struct iwl_tlc_update_notif *notif; 292 struct ieee80211_sta *sta; 293 struct iwl_mvm_sta *mvmsta; 294 struct iwl_lq_sta_rs_fw *lq_sta; 295 u32 flags; 296 297 rcu_read_lock(); 298 299 notif = (void *)pkt->data; 300 sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]); 301 if (IS_ERR_OR_NULL(sta)) { 302 IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n", 303 notif->sta_id); 304 goto out; 305 } 306 307 mvmsta = iwl_mvm_sta_from_mac80211(sta); 308 309 if (!mvmsta) { 310 IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n", 311 notif->sta_id); 312 goto out; 313 } 314 315 flags = le32_to_cpu(notif->flags); 316 317 lq_sta = &mvmsta->lq_sta.rs_fw; 318 319 if (flags & IWL_TLC_NOTIF_FLAG_RATE) { 320 char pretty_rate[100]; 321 lq_sta->last_rate_n_flags = le32_to_cpu(notif->rate); 322 rs_pretty_print_rate(pretty_rate, sizeof(pretty_rate), 323 lq_sta->last_rate_n_flags); 324 IWL_DEBUG_RATE(mvm, "new rate: %s\n", pretty_rate); 325 } 326 327 if (flags & IWL_TLC_NOTIF_FLAG_AMSDU && !mvmsta->orig_amsdu_len) { 328 u16 size = le32_to_cpu(notif->amsdu_size); 329 int i; 330 331 if (sta->max_amsdu_len < size) { 332 /* 333 * In debug sta->max_amsdu_len < size 334 * so also check with orig_amsdu_len which holds the 335 * original data before debugfs changed the value 336 */ 337 WARN_ON(mvmsta->orig_amsdu_len < size); 338 goto out; 339 } 340 341 mvmsta->amsdu_enabled = le32_to_cpu(notif->amsdu_enabled); 342 mvmsta->max_amsdu_len = size; 343 sta->max_rc_amsdu_len = mvmsta->max_amsdu_len; 344 345 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 346 if (mvmsta->amsdu_enabled & BIT(i)) 347 sta->max_tid_amsdu_len[i] = 348 iwl_mvm_max_amsdu_size(mvm, sta, i); 349 else 350 /* 351 * Not so elegant, but this will effectively 352 * prevent AMSDU on this TID 353 */ 354 sta->max_tid_amsdu_len[i] = 1; 355 } 356 357 IWL_DEBUG_RATE(mvm, 358 "AMSDU update. AMSDU size: %d, AMSDU selected size: %d, AMSDU TID bitmap 0x%X\n", 359 le32_to_cpu(notif->amsdu_size), size, 360 mvmsta->amsdu_enabled); 361 } 362 out: 363 rcu_read_unlock(); 364 } 365 366 u16 rs_fw_get_max_amsdu_len(struct ieee80211_sta *sta) 367 { 368 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 369 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; 370 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; 371 372 if (mvmsta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) { 373 switch (le16_get_bits(sta->he_6ghz_capa.capa, 374 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN)) { 375 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454: 376 return IEEE80211_MAX_MPDU_LEN_VHT_11454; 377 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991: 378 return IEEE80211_MAX_MPDU_LEN_VHT_7991; 379 default: 380 return IEEE80211_MAX_MPDU_LEN_VHT_3895; 381 } 382 } else 383 if (vht_cap->vht_supported) { 384 switch (vht_cap->cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) { 385 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454: 386 return IEEE80211_MAX_MPDU_LEN_VHT_11454; 387 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991: 388 return IEEE80211_MAX_MPDU_LEN_VHT_7991; 389 default: 390 return IEEE80211_MAX_MPDU_LEN_VHT_3895; 391 } 392 } else if (ht_cap->ht_supported) { 393 if (ht_cap->cap & IEEE80211_HT_CAP_MAX_AMSDU) 394 /* 395 * agg is offloaded so we need to assume that agg 396 * are enabled and max mpdu in ampdu is 4095 397 * (spec 802.11-2016 9.3.2.1) 398 */ 399 return IEEE80211_MAX_MPDU_LEN_HT_BA; 400 else 401 return IEEE80211_MAX_MPDU_LEN_HT_3839; 402 } 403 404 /* in legacy mode no amsdu is enabled so return zero */ 405 return 0; 406 } 407 408 void rs_fw_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 409 enum nl80211_band band, bool update) 410 { 411 struct ieee80211_hw *hw = mvm->hw; 412 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 413 struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw; 414 u32 cmd_id = iwl_cmd_id(TLC_MNG_CONFIG_CMD, DATA_PATH_GROUP, 0); 415 struct ieee80211_supported_band *sband = hw->wiphy->bands[band]; 416 u16 max_amsdu_len = rs_fw_get_max_amsdu_len(sta); 417 struct iwl_tlc_config_cmd cfg_cmd = { 418 .sta_id = mvmsta->sta_id, 419 .max_ch_width = update ? 420 rs_fw_bw_from_sta_bw(sta) : RATE_MCS_CHAN_WIDTH_20, 421 .flags = cpu_to_le16(rs_fw_get_config_flags(mvm, sta, sband)), 422 .chains = rs_fw_set_active_chains(iwl_mvm_get_valid_tx_ant(mvm)), 423 .sgi_ch_width_supp = rs_fw_sgi_cw_support(sta), 424 .max_mpdu_len = cpu_to_le16(max_amsdu_len), 425 .amsdu = iwl_mvm_is_csum_supported(mvm), 426 }; 427 int ret; 428 u16 cmd_size = sizeof(cfg_cmd); 429 430 /* In old versions of the API the struct is 4 bytes smaller */ 431 if (iwl_fw_lookup_cmd_ver(mvm->fw, DATA_PATH_GROUP, 432 TLC_MNG_CONFIG_CMD, 0) < 3) 433 cmd_size -= 4; 434 435 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers)); 436 437 #ifdef CONFIG_IWLWIFI_DEBUGFS 438 iwl_mvm_reset_frame_stats(mvm); 439 #endif 440 rs_fw_set_supp_rates(sta, sband, &cfg_cmd); 441 442 /* 443 * since TLC offload works with one mode we can assume 444 * that only vht/ht is used and also set it as station max amsdu 445 */ 446 sta->max_amsdu_len = max_amsdu_len; 447 448 ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, CMD_ASYNC, cmd_size, 449 &cfg_cmd); 450 if (ret) 451 IWL_ERR(mvm, "Failed to send rate scale config (%d)\n", ret); 452 } 453 454 int rs_fw_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, 455 bool enable) 456 { 457 /* TODO: need to introduce a new FW cmd since LQ cmd is not relevant */ 458 IWL_DEBUG_RATE(mvm, "tx protection - not implemented yet.\n"); 459 return 0; 460 } 461 462 void iwl_mvm_rs_add_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta) 463 { 464 struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw; 465 466 IWL_DEBUG_RATE(mvm, "create station rate scale window\n"); 467 468 lq_sta->pers.drv = mvm; 469 lq_sta->pers.sta_id = mvmsta->sta_id; 470 lq_sta->pers.chains = 0; 471 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal)); 472 lq_sta->pers.last_rssi = S8_MIN; 473 lq_sta->last_rate_n_flags = 0; 474 475 #ifdef CONFIG_MAC80211_DEBUGFS 476 lq_sta->pers.dbg_fixed_rate = 0; 477 #endif 478 } 479