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 unsigned long tmp; 252 unsigned long supp; /* 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 supp = 0; 259 tmp = sta->supp_rates[sband->band]; 260 for_each_set_bit(i, &tmp, BITS_PER_LONG) 261 supp |= BIT(sband->bitrates[i].hw_value); 262 263 cmd->non_ht_rates = cpu_to_le16(supp); 264 cmd->mode = IWL_TLC_MNG_MODE_NON_HT; 265 266 /* HT/VHT rates */ 267 if (he_cap->has_he) { 268 cmd->mode = IWL_TLC_MNG_MODE_HE; 269 rs_fw_he_set_enabled_rates(sta, sband, cmd); 270 } else if (vht_cap->vht_supported) { 271 cmd->mode = IWL_TLC_MNG_MODE_VHT; 272 rs_fw_vht_set_enabled_rates(sta, vht_cap, cmd); 273 } else if (ht_cap->ht_supported) { 274 cmd->mode = IWL_TLC_MNG_MODE_HT; 275 cmd->ht_rates[IWL_TLC_NSS_1][IWL_TLC_HT_BW_NONE_160] = 276 cpu_to_le16(ht_cap->mcs.rx_mask[0]); 277 278 /* the station support only a single receive chain */ 279 if (sta->smps_mode == IEEE80211_SMPS_STATIC) 280 cmd->ht_rates[IWL_TLC_NSS_2][IWL_TLC_HT_BW_NONE_160] = 281 0; 282 else 283 cmd->ht_rates[IWL_TLC_NSS_2][IWL_TLC_HT_BW_NONE_160] = 284 cpu_to_le16(ht_cap->mcs.rx_mask[1]); 285 } 286 } 287 288 void iwl_mvm_tlc_update_notif(struct iwl_mvm *mvm, 289 struct iwl_rx_cmd_buffer *rxb) 290 { 291 struct iwl_rx_packet *pkt = rxb_addr(rxb); 292 struct iwl_tlc_update_notif *notif; 293 struct ieee80211_sta *sta; 294 struct iwl_mvm_sta *mvmsta; 295 struct iwl_lq_sta_rs_fw *lq_sta; 296 u32 flags; 297 298 rcu_read_lock(); 299 300 notif = (void *)pkt->data; 301 sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]); 302 if (IS_ERR_OR_NULL(sta)) { 303 IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n", 304 notif->sta_id); 305 goto out; 306 } 307 308 mvmsta = iwl_mvm_sta_from_mac80211(sta); 309 310 if (!mvmsta) { 311 IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n", 312 notif->sta_id); 313 goto out; 314 } 315 316 flags = le32_to_cpu(notif->flags); 317 318 lq_sta = &mvmsta->lq_sta.rs_fw; 319 320 if (flags & IWL_TLC_NOTIF_FLAG_RATE) { 321 char pretty_rate[100]; 322 lq_sta->last_rate_n_flags = le32_to_cpu(notif->rate); 323 rs_pretty_print_rate(pretty_rate, sizeof(pretty_rate), 324 lq_sta->last_rate_n_flags); 325 IWL_DEBUG_RATE(mvm, "new rate: %s\n", pretty_rate); 326 } 327 328 if (flags & IWL_TLC_NOTIF_FLAG_AMSDU && !mvmsta->orig_amsdu_len) { 329 u16 size = le32_to_cpu(notif->amsdu_size); 330 int i; 331 332 if (sta->max_amsdu_len < size) { 333 /* 334 * In debug sta->max_amsdu_len < size 335 * so also check with orig_amsdu_len which holds the 336 * original data before debugfs changed the value 337 */ 338 WARN_ON(mvmsta->orig_amsdu_len < size); 339 goto out; 340 } 341 342 mvmsta->amsdu_enabled = le32_to_cpu(notif->amsdu_enabled); 343 mvmsta->max_amsdu_len = size; 344 sta->max_rc_amsdu_len = mvmsta->max_amsdu_len; 345 346 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 347 if (mvmsta->amsdu_enabled & BIT(i)) 348 sta->max_tid_amsdu_len[i] = 349 iwl_mvm_max_amsdu_size(mvm, sta, i); 350 else 351 /* 352 * Not so elegant, but this will effectively 353 * prevent AMSDU on this TID 354 */ 355 sta->max_tid_amsdu_len[i] = 1; 356 } 357 358 IWL_DEBUG_RATE(mvm, 359 "AMSDU update. AMSDU size: %d, AMSDU selected size: %d, AMSDU TID bitmap 0x%X\n", 360 le32_to_cpu(notif->amsdu_size), size, 361 mvmsta->amsdu_enabled); 362 } 363 out: 364 rcu_read_unlock(); 365 } 366 367 u16 rs_fw_get_max_amsdu_len(struct ieee80211_sta *sta) 368 { 369 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 370 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; 371 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; 372 373 if (mvmsta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) { 374 switch (le16_get_bits(sta->he_6ghz_capa.capa, 375 IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN)) { 376 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454: 377 return IEEE80211_MAX_MPDU_LEN_VHT_11454; 378 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991: 379 return IEEE80211_MAX_MPDU_LEN_VHT_7991; 380 default: 381 return IEEE80211_MAX_MPDU_LEN_VHT_3895; 382 } 383 } else 384 if (vht_cap->vht_supported) { 385 switch (vht_cap->cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) { 386 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454: 387 return IEEE80211_MAX_MPDU_LEN_VHT_11454; 388 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991: 389 return IEEE80211_MAX_MPDU_LEN_VHT_7991; 390 default: 391 return IEEE80211_MAX_MPDU_LEN_VHT_3895; 392 } 393 } else if (ht_cap->ht_supported) { 394 if (ht_cap->cap & IEEE80211_HT_CAP_MAX_AMSDU) 395 /* 396 * agg is offloaded so we need to assume that agg 397 * are enabled and max mpdu in ampdu is 4095 398 * (spec 802.11-2016 9.3.2.1) 399 */ 400 return IEEE80211_MAX_MPDU_LEN_HT_BA; 401 else 402 return IEEE80211_MAX_MPDU_LEN_HT_3839; 403 } 404 405 /* in legacy mode no amsdu is enabled so return zero */ 406 return 0; 407 } 408 409 void rs_fw_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 410 enum nl80211_band band, bool update) 411 { 412 struct ieee80211_hw *hw = mvm->hw; 413 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 414 struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw; 415 u32 cmd_id = iwl_cmd_id(TLC_MNG_CONFIG_CMD, DATA_PATH_GROUP, 0); 416 struct ieee80211_supported_band *sband = hw->wiphy->bands[band]; 417 u16 max_amsdu_len = rs_fw_get_max_amsdu_len(sta); 418 struct iwl_tlc_config_cmd cfg_cmd = { 419 .sta_id = mvmsta->sta_id, 420 .max_ch_width = update ? 421 rs_fw_bw_from_sta_bw(sta) : RATE_MCS_CHAN_WIDTH_20, 422 .flags = cpu_to_le16(rs_fw_get_config_flags(mvm, sta, sband)), 423 .chains = rs_fw_set_active_chains(iwl_mvm_get_valid_tx_ant(mvm)), 424 .sgi_ch_width_supp = rs_fw_sgi_cw_support(sta), 425 .max_mpdu_len = cpu_to_le16(max_amsdu_len), 426 .amsdu = iwl_mvm_is_csum_supported(mvm), 427 }; 428 int ret; 429 u16 cmd_size = sizeof(cfg_cmd); 430 431 /* In old versions of the API the struct is 4 bytes smaller */ 432 if (iwl_fw_lookup_cmd_ver(mvm->fw, DATA_PATH_GROUP, 433 TLC_MNG_CONFIG_CMD, 0) < 3) 434 cmd_size -= 4; 435 436 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers)); 437 438 #ifdef CONFIG_IWLWIFI_DEBUGFS 439 iwl_mvm_reset_frame_stats(mvm); 440 #endif 441 rs_fw_set_supp_rates(sta, sband, &cfg_cmd); 442 443 /* 444 * since TLC offload works with one mode we can assume 445 * that only vht/ht is used and also set it as station max amsdu 446 */ 447 sta->max_amsdu_len = max_amsdu_len; 448 449 ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, CMD_ASYNC, cmd_size, 450 &cfg_cmd); 451 if (ret) 452 IWL_ERR(mvm, "Failed to send rate scale config (%d)\n", ret); 453 } 454 455 int rs_fw_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, 456 bool enable) 457 { 458 /* TODO: need to introduce a new FW cmd since LQ cmd is not relevant */ 459 IWL_DEBUG_RATE(mvm, "tx protection - not implemented yet.\n"); 460 return 0; 461 } 462 463 void iwl_mvm_rs_add_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta) 464 { 465 struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw; 466 467 IWL_DEBUG_RATE(mvm, "create station rate scale window\n"); 468 469 lq_sta->pers.drv = mvm; 470 lq_sta->pers.sta_id = mvmsta->sta_id; 471 lq_sta->pers.chains = 0; 472 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal)); 473 lq_sta->pers.last_rssi = S8_MIN; 474 lq_sta->last_rate_n_flags = 0; 475 476 #ifdef CONFIG_MAC80211_DEBUGFS 477 lq_sta->pers.dbg_fixed_rate = 0; 478 #endif 479 } 480