1 // SPDX-License-Identifier: GPL-2.0-only 2 /****************************************************************************** 3 * 4 * Copyright(c) 2005 - 2014, 2018 - 2023 Intel Corporation. All rights reserved. 5 * Copyright(c) 2025 Intel Corporation 6 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH 7 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH 8 *****************************************************************************/ 9 #include <linux/kernel.h> 10 #include <linux/skbuff.h> 11 #include <linux/slab.h> 12 #include <net/mac80211.h> 13 14 #include <linux/netdevice.h> 15 #include <linux/etherdevice.h> 16 #include <linux/delay.h> 17 18 #include <linux/workqueue.h> 19 #include "rs.h" 20 #include "fw-api.h" 21 #include "sta.h" 22 #include "iwl-op-mode.h" 23 #include "mvm.h" 24 #include "debugfs.h" 25 26 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */ 27 28 /* Calculations of success ratio are done in fixed point where 12800 is 100%. 29 * Use this macro when dealing with thresholds consts set as a percentage 30 */ 31 #define RS_PERCENT(x) (128 * x) 32 33 static u8 rs_ht_to_legacy[] = { 34 [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX, 35 [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX, 36 [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX, 37 [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX, 38 [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX, 39 [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX, 40 [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX, 41 [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX, 42 [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX, 43 [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX, 44 }; 45 46 static const u8 ant_toggle_lookup[] = { 47 [ANT_NONE] = ANT_NONE, 48 [ANT_A] = ANT_B, 49 [ANT_B] = ANT_A, 50 [ANT_AB] = ANT_AB, 51 }; 52 53 #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \ 54 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \ 55 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \ 56 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \ 57 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \ 58 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\ 59 IWL_RATE_##rp##M_INDEX, \ 60 IWL_RATE_##rn##M_INDEX } 61 62 #define IWL_DECLARE_MCS_RATE(s) \ 63 [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \ 64 IWL_RATE_HT_SISO_MCS_##s##_PLCP, \ 65 IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \ 66 IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \ 67 IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \ 68 IWL_RATE_INVM_INDEX, \ 69 IWL_RATE_INVM_INDEX } 70 71 /* 72 * Parameter order: 73 * rate, ht rate, prev rate, next rate 74 * 75 * If there isn't a valid next or previous rate then INV is used which 76 * maps to IWL_RATE_INVALID 77 * 78 */ 79 static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = { 80 IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */ 81 IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */ 82 IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */ 83 IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */ 84 IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */ 85 IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */ 86 IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */ 87 IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */ 88 IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */ 89 IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */ 90 IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */ 91 IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */ 92 IWL_DECLARE_MCS_RATE(7), /* MCS 7 */ 93 IWL_DECLARE_MCS_RATE(8), /* MCS 8 */ 94 IWL_DECLARE_MCS_RATE(9), /* MCS 9 */ 95 }; 96 97 enum rs_action { 98 RS_ACTION_STAY = 0, 99 RS_ACTION_DOWNSCALE = -1, 100 RS_ACTION_UPSCALE = 1, 101 }; 102 103 enum rs_column_mode { 104 RS_INVALID = 0, 105 RS_LEGACY, 106 RS_SISO, 107 RS_MIMO2, 108 }; 109 110 #define MAX_NEXT_COLUMNS 7 111 #define MAX_COLUMN_CHECKS 3 112 113 struct rs_tx_column; 114 115 typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm, 116 struct ieee80211_sta *sta, 117 struct rs_rate *rate, 118 const struct rs_tx_column *next_col); 119 120 struct rs_tx_column { 121 enum rs_column_mode mode; 122 u8 ant; 123 bool sgi; 124 enum rs_column next_columns[MAX_NEXT_COLUMNS]; 125 allow_column_func_t checks[MAX_COLUMN_CHECKS]; 126 }; 127 128 static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 129 struct rs_rate *rate, 130 const struct rs_tx_column *next_col) 131 { 132 return iwl_mvm_bt_coex_is_ant_avail(mvm, next_col->ant); 133 } 134 135 static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 136 struct rs_rate *rate, 137 const struct rs_tx_column *next_col) 138 { 139 if (!sta->deflink.ht_cap.ht_supported) 140 return false; 141 142 if (sta->deflink.smps_mode == IEEE80211_SMPS_STATIC) 143 return false; 144 145 if (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) < 2) 146 return false; 147 148 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) 149 return false; 150 151 if (mvm->nvm_data->sku_cap_mimo_disabled) 152 return false; 153 154 return true; 155 } 156 157 static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 158 struct rs_rate *rate, 159 const struct rs_tx_column *next_col) 160 { 161 if (!sta->deflink.ht_cap.ht_supported) 162 return false; 163 164 return true; 165 } 166 167 static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 168 struct rs_rate *rate, 169 const struct rs_tx_column *next_col) 170 { 171 struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap; 172 struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap; 173 174 if (is_ht20(rate) && (ht_cap->cap & 175 IEEE80211_HT_CAP_SGI_20)) 176 return true; 177 if (is_ht40(rate) && (ht_cap->cap & 178 IEEE80211_HT_CAP_SGI_40)) 179 return true; 180 if (is_ht80(rate) && (vht_cap->cap & 181 IEEE80211_VHT_CAP_SHORT_GI_80)) 182 return true; 183 if (is_ht160(rate) && (vht_cap->cap & 184 IEEE80211_VHT_CAP_SHORT_GI_160)) 185 return true; 186 187 return false; 188 } 189 190 static const struct rs_tx_column rs_tx_columns[] = { 191 [RS_COLUMN_LEGACY_ANT_A] = { 192 .mode = RS_LEGACY, 193 .ant = ANT_A, 194 .next_columns = { 195 RS_COLUMN_LEGACY_ANT_B, 196 RS_COLUMN_SISO_ANT_A, 197 RS_COLUMN_MIMO2, 198 RS_COLUMN_INVALID, 199 RS_COLUMN_INVALID, 200 RS_COLUMN_INVALID, 201 RS_COLUMN_INVALID, 202 }, 203 .checks = { 204 rs_ant_allow, 205 }, 206 }, 207 [RS_COLUMN_LEGACY_ANT_B] = { 208 .mode = RS_LEGACY, 209 .ant = ANT_B, 210 .next_columns = { 211 RS_COLUMN_LEGACY_ANT_A, 212 RS_COLUMN_SISO_ANT_B, 213 RS_COLUMN_MIMO2, 214 RS_COLUMN_INVALID, 215 RS_COLUMN_INVALID, 216 RS_COLUMN_INVALID, 217 RS_COLUMN_INVALID, 218 }, 219 .checks = { 220 rs_ant_allow, 221 }, 222 }, 223 [RS_COLUMN_SISO_ANT_A] = { 224 .mode = RS_SISO, 225 .ant = ANT_A, 226 .next_columns = { 227 RS_COLUMN_SISO_ANT_B, 228 RS_COLUMN_MIMO2, 229 RS_COLUMN_SISO_ANT_A_SGI, 230 RS_COLUMN_LEGACY_ANT_A, 231 RS_COLUMN_LEGACY_ANT_B, 232 RS_COLUMN_INVALID, 233 RS_COLUMN_INVALID, 234 }, 235 .checks = { 236 rs_siso_allow, 237 rs_ant_allow, 238 }, 239 }, 240 [RS_COLUMN_SISO_ANT_B] = { 241 .mode = RS_SISO, 242 .ant = ANT_B, 243 .next_columns = { 244 RS_COLUMN_SISO_ANT_A, 245 RS_COLUMN_MIMO2, 246 RS_COLUMN_SISO_ANT_B_SGI, 247 RS_COLUMN_LEGACY_ANT_A, 248 RS_COLUMN_LEGACY_ANT_B, 249 RS_COLUMN_INVALID, 250 RS_COLUMN_INVALID, 251 }, 252 .checks = { 253 rs_siso_allow, 254 rs_ant_allow, 255 }, 256 }, 257 [RS_COLUMN_SISO_ANT_A_SGI] = { 258 .mode = RS_SISO, 259 .ant = ANT_A, 260 .sgi = true, 261 .next_columns = { 262 RS_COLUMN_SISO_ANT_B_SGI, 263 RS_COLUMN_MIMO2_SGI, 264 RS_COLUMN_SISO_ANT_A, 265 RS_COLUMN_LEGACY_ANT_A, 266 RS_COLUMN_LEGACY_ANT_B, 267 RS_COLUMN_INVALID, 268 RS_COLUMN_INVALID, 269 }, 270 .checks = { 271 rs_siso_allow, 272 rs_ant_allow, 273 rs_sgi_allow, 274 }, 275 }, 276 [RS_COLUMN_SISO_ANT_B_SGI] = { 277 .mode = RS_SISO, 278 .ant = ANT_B, 279 .sgi = true, 280 .next_columns = { 281 RS_COLUMN_SISO_ANT_A_SGI, 282 RS_COLUMN_MIMO2_SGI, 283 RS_COLUMN_SISO_ANT_B, 284 RS_COLUMN_LEGACY_ANT_A, 285 RS_COLUMN_LEGACY_ANT_B, 286 RS_COLUMN_INVALID, 287 RS_COLUMN_INVALID, 288 }, 289 .checks = { 290 rs_siso_allow, 291 rs_ant_allow, 292 rs_sgi_allow, 293 }, 294 }, 295 [RS_COLUMN_MIMO2] = { 296 .mode = RS_MIMO2, 297 .ant = ANT_AB, 298 .next_columns = { 299 RS_COLUMN_SISO_ANT_A, 300 RS_COLUMN_MIMO2_SGI, 301 RS_COLUMN_LEGACY_ANT_A, 302 RS_COLUMN_LEGACY_ANT_B, 303 RS_COLUMN_INVALID, 304 RS_COLUMN_INVALID, 305 RS_COLUMN_INVALID, 306 }, 307 .checks = { 308 rs_mimo_allow, 309 }, 310 }, 311 [RS_COLUMN_MIMO2_SGI] = { 312 .mode = RS_MIMO2, 313 .ant = ANT_AB, 314 .sgi = true, 315 .next_columns = { 316 RS_COLUMN_SISO_ANT_A_SGI, 317 RS_COLUMN_MIMO2, 318 RS_COLUMN_LEGACY_ANT_A, 319 RS_COLUMN_LEGACY_ANT_B, 320 RS_COLUMN_INVALID, 321 RS_COLUMN_INVALID, 322 RS_COLUMN_INVALID, 323 }, 324 .checks = { 325 rs_mimo_allow, 326 rs_sgi_allow, 327 }, 328 }, 329 }; 330 331 static inline u8 rs_extract_rate(u32 rate_n_flags) 332 { 333 /* also works for HT because bits 7:6 are zero there */ 334 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK_V1); 335 } 336 337 static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags) 338 { 339 int idx = 0; 340 341 if (rate_n_flags & RATE_MCS_HT_MSK_V1) { 342 idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1; 343 idx += IWL_RATE_MCS_0_INDEX; 344 345 /* skip 9M not supported in HT*/ 346 if (idx >= IWL_RATE_9M_INDEX) 347 idx += 1; 348 if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE)) 349 return idx; 350 } else if (rate_n_flags & RATE_MCS_VHT_MSK_V1 || 351 rate_n_flags & RATE_MCS_HE_MSK_V1) { 352 idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK; 353 idx += IWL_RATE_MCS_0_INDEX; 354 355 /* skip 9M not supported in VHT*/ 356 if (idx >= IWL_RATE_9M_INDEX) 357 idx++; 358 if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE)) 359 return idx; 360 if ((rate_n_flags & RATE_MCS_HE_MSK_V1) && 361 idx <= IWL_LAST_HE_RATE) 362 return idx; 363 } else { 364 /* legacy rate format, search for match in table */ 365 366 u8 legacy_rate = rs_extract_rate(rate_n_flags); 367 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++) 368 if (iwl_rates[idx].plcp == legacy_rate) 369 return idx; 370 } 371 372 return IWL_RATE_INVALID; 373 } 374 375 static void rs_rate_scale_perform(struct iwl_mvm *mvm, 376 struct ieee80211_sta *sta, 377 struct iwl_lq_sta *lq_sta, 378 int tid, bool ndp); 379 static void rs_fill_lq_cmd(struct iwl_mvm *mvm, 380 struct ieee80211_sta *sta, 381 struct iwl_lq_sta *lq_sta, 382 const struct rs_rate *initial_rate); 383 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search); 384 385 /* 386 * The following tables contain the expected throughput metrics for all rates 387 * 388 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits 389 * 390 * where invalid entries are zeros. 391 * 392 * CCK rates are only valid in legacy table and will only be used in G 393 * (2.4 GHz) band. 394 */ 395 static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = { 396 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0 397 }; 398 399 /* Expected TpT tables. 4 indexes: 400 * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI 401 */ 402 static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = { 403 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0}, 404 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0}, 405 {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0}, 406 {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0}, 407 }; 408 409 static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = { 410 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275}, 411 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280}, 412 {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173}, 413 {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284}, 414 }; 415 416 static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = { 417 {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308}, 418 {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312}, 419 {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466}, 420 {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691}, 421 }; 422 423 static const u16 expected_tpt_siso_160MHz[4][IWL_RATE_COUNT] = { 424 {0, 0, 0, 0, 191, 0, 244, 288, 298, 308, 313, 318, 323, 328, 330}, 425 {0, 0, 0, 0, 200, 0, 251, 293, 302, 312, 317, 322, 327, 332, 334}, 426 {0, 0, 0, 0, 439, 0, 875, 1307, 1736, 2584, 3419, 3831, 4240, 5049, 5581}, 427 {0, 0, 0, 0, 488, 0, 972, 1451, 1925, 2864, 3785, 4240, 4691, 5581, 6165}, 428 }; 429 430 static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = { 431 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0}, 432 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0}, 433 {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0}, 434 {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0}, 435 }; 436 437 static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = { 438 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300}, 439 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303}, 440 {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053}, 441 {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221}, 442 }; 443 444 static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = { 445 {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319}, 446 {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320}, 447 {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219}, 448 {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545}, 449 }; 450 451 static const u16 expected_tpt_mimo2_160MHz[4][IWL_RATE_COUNT] = { 452 {0, 0, 0, 0, 240, 0, 278, 308, 313, 319, 322, 324, 328, 330, 334}, 453 {0, 0, 0, 0, 247, 0, 282, 310, 315, 320, 323, 325, 329, 332, 338}, 454 {0, 0, 0, 0, 875, 0, 1735, 2582, 3414, 5043, 6619, 7389, 8147, 9629, 10592}, 455 {0, 0, 0, 0, 971, 0, 1925, 2861, 3779, 5574, 7304, 8147, 8976, 10592, 11640}, 456 }; 457 458 static const char *rs_pretty_lq_type(enum iwl_table_type type) 459 { 460 static const char * const lq_types[] = { 461 [LQ_NONE] = "NONE", 462 [LQ_LEGACY_A] = "LEGACY_A", 463 [LQ_LEGACY_G] = "LEGACY_G", 464 [LQ_HT_SISO] = "HT SISO", 465 [LQ_HT_MIMO2] = "HT MIMO", 466 [LQ_VHT_SISO] = "VHT SISO", 467 [LQ_VHT_MIMO2] = "VHT MIMO", 468 [LQ_HE_SISO] = "HE SISO", 469 [LQ_HE_MIMO2] = "HE MIMO", 470 }; 471 472 if (type < LQ_NONE || type >= LQ_MAX) 473 return "UNKNOWN"; 474 475 return lq_types[type]; 476 } 477 478 static char *rs_pretty_rate(const struct rs_rate *rate) 479 { 480 static char buf[40]; 481 static const char * const legacy_rates[] = { 482 [IWL_RATE_1M_INDEX] = "1M", 483 [IWL_RATE_2M_INDEX] = "2M", 484 [IWL_RATE_5M_INDEX] = "5.5M", 485 [IWL_RATE_11M_INDEX] = "11M", 486 [IWL_RATE_6M_INDEX] = "6M", 487 [IWL_RATE_9M_INDEX] = "9M", 488 [IWL_RATE_12M_INDEX] = "12M", 489 [IWL_RATE_18M_INDEX] = "18M", 490 [IWL_RATE_24M_INDEX] = "24M", 491 [IWL_RATE_36M_INDEX] = "36M", 492 [IWL_RATE_48M_INDEX] = "48M", 493 [IWL_RATE_54M_INDEX] = "54M", 494 }; 495 static const char *const ht_vht_rates[] = { 496 [IWL_RATE_MCS_0_INDEX] = "MCS0", 497 [IWL_RATE_MCS_1_INDEX] = "MCS1", 498 [IWL_RATE_MCS_2_INDEX] = "MCS2", 499 [IWL_RATE_MCS_3_INDEX] = "MCS3", 500 [IWL_RATE_MCS_4_INDEX] = "MCS4", 501 [IWL_RATE_MCS_5_INDEX] = "MCS5", 502 [IWL_RATE_MCS_6_INDEX] = "MCS6", 503 [IWL_RATE_MCS_7_INDEX] = "MCS7", 504 [IWL_RATE_MCS_8_INDEX] = "MCS8", 505 [IWL_RATE_MCS_9_INDEX] = "MCS9", 506 }; 507 const char *rate_str; 508 509 if (is_type_legacy(rate->type) && (rate->index <= IWL_RATE_54M_INDEX)) 510 rate_str = legacy_rates[rate->index]; 511 else if ((is_type_ht(rate->type) || is_type_vht(rate->type)) && 512 (rate->index >= IWL_RATE_MCS_0_INDEX) && 513 (rate->index <= IWL_RATE_MCS_9_INDEX)) 514 rate_str = ht_vht_rates[rate->index]; 515 else 516 rate_str = NULL; 517 518 sprintf(buf, "(%s|%s|%s)", rs_pretty_lq_type(rate->type), 519 iwl_rs_pretty_ant(rate->ant), rate_str ?: "BAD_RATE"); 520 return buf; 521 } 522 523 static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate, 524 const char *prefix) 525 { 526 IWL_DEBUG_RATE(mvm, 527 "%s: %s BW: %d SGI: %d LDPC: %d STBC: %d\n", 528 prefix, rs_pretty_rate(rate), rate->bw, 529 rate->sgi, rate->ldpc, rate->stbc); 530 } 531 532 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window) 533 { 534 window->data = 0; 535 window->success_counter = 0; 536 window->success_ratio = IWL_INVALID_VALUE; 537 window->counter = 0; 538 window->average_tpt = IWL_INVALID_VALUE; 539 } 540 541 static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm, 542 struct iwl_scale_tbl_info *tbl) 543 { 544 int i; 545 546 IWL_DEBUG_RATE(mvm, "Clearing up window stats\n"); 547 for (i = 0; i < IWL_RATE_COUNT; i++) 548 rs_rate_scale_clear_window(&tbl->win[i]); 549 550 for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++) 551 rs_rate_scale_clear_window(&tbl->tpc_win[i]); 552 } 553 554 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type) 555 { 556 return (ant_type & valid_antenna) == ant_type; 557 } 558 559 static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm, 560 struct iwl_lq_sta *lq_data, u8 tid, 561 struct ieee80211_sta *sta) 562 { 563 int ret; 564 565 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n", 566 sta->addr, tid); 567 568 /* start BA session until the peer sends del BA */ 569 ret = ieee80211_start_tx_ba_session(sta, tid, 0); 570 if (ret == -EAGAIN) { 571 /* 572 * driver and mac80211 is out of sync 573 * this might be cause by reloading firmware 574 * stop the tx ba session here 575 */ 576 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n", 577 tid); 578 ieee80211_stop_tx_ba_session(sta, tid); 579 } 580 return ret; 581 } 582 583 static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, 584 u8 tid, struct iwl_lq_sta *lq_sta, 585 struct ieee80211_sta *sta) 586 { 587 struct iwl_mvm_tid_data *tid_data; 588 589 /* 590 * In AP mode, tid can be equal to IWL_MAX_TID_COUNT 591 * when the frame is not QoS 592 */ 593 if (WARN_ON_ONCE(tid > IWL_MAX_TID_COUNT)) { 594 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n", 595 tid, IWL_MAX_TID_COUNT); 596 return; 597 } else if (tid == IWL_MAX_TID_COUNT) { 598 return; 599 } 600 601 tid_data = &mvmsta->tid_data[tid]; 602 if (mvmsta->sta_state >= IEEE80211_STA_AUTHORIZED && 603 tid_data->state == IWL_AGG_OFF && 604 (lq_sta->tx_agg_tid_en & BIT(tid)) && 605 tid_data->tx_count_last >= IWL_MVM_RS_AGG_START_THRESHOLD) { 606 IWL_DEBUG_RATE(mvm, "try to aggregate tid %d\n", tid); 607 if (rs_tl_turn_on_agg_for_tid(mvm, lq_sta, tid, sta) == 0) 608 tid_data->state = IWL_AGG_QUEUED; 609 } 610 } 611 612 static inline int get_num_of_ant_from_rate(u32 rate_n_flags) 613 { 614 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) + 615 !!(rate_n_flags & RATE_MCS_ANT_B_MSK); 616 } 617 618 /* 619 * Static function to get the expected throughput from an iwl_scale_tbl_info 620 * that wraps a NULL pointer check 621 */ 622 static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index) 623 { 624 if (tbl->expected_tpt) 625 return tbl->expected_tpt[rs_index]; 626 return 0; 627 } 628 629 /* 630 * rs_collect_tx_data - Update the success/failure sliding window 631 * 632 * We keep a sliding window of the last 62 packets transmitted 633 * at this rate. window->data contains the bitmask of successful 634 * packets. 635 */ 636 static int _rs_collect_tx_data(struct iwl_mvm *mvm, 637 struct iwl_scale_tbl_info *tbl, 638 int scale_index, int attempts, int successes, 639 struct iwl_rate_scale_data *window) 640 { 641 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1)); 642 s32 fail_count, tpt; 643 644 /* Get expected throughput */ 645 tpt = get_expected_tpt(tbl, scale_index); 646 647 /* 648 * Keep track of only the latest 62 tx frame attempts in this rate's 649 * history window; anything older isn't really relevant any more. 650 * If we have filled up the sliding window, drop the oldest attempt; 651 * if the oldest attempt (highest bit in bitmap) shows "success", 652 * subtract "1" from the success counter (this is the main reason 653 * we keep these bitmaps!). 654 */ 655 while (attempts > 0) { 656 if (window->counter >= IWL_RATE_MAX_WINDOW) { 657 /* remove earliest */ 658 window->counter = IWL_RATE_MAX_WINDOW - 1; 659 660 if (window->data & mask) { 661 window->data &= ~mask; 662 window->success_counter--; 663 } 664 } 665 666 /* Increment frames-attempted counter */ 667 window->counter++; 668 669 /* Shift bitmap by one frame to throw away oldest history */ 670 window->data <<= 1; 671 672 /* Mark the most recent #successes attempts as successful */ 673 if (successes > 0) { 674 window->success_counter++; 675 window->data |= 0x1; 676 successes--; 677 } 678 679 attempts--; 680 } 681 682 /* Calculate current success ratio, avoid divide-by-0! */ 683 if (window->counter > 0) 684 window->success_ratio = 128 * (100 * window->success_counter) 685 / window->counter; 686 else 687 window->success_ratio = IWL_INVALID_VALUE; 688 689 fail_count = window->counter - window->success_counter; 690 691 /* Calculate average throughput, if we have enough history. */ 692 if ((fail_count >= IWL_MVM_RS_RATE_MIN_FAILURE_TH) || 693 (window->success_counter >= IWL_MVM_RS_RATE_MIN_SUCCESS_TH)) 694 window->average_tpt = (window->success_ratio * tpt + 64) / 128; 695 else 696 window->average_tpt = IWL_INVALID_VALUE; 697 698 return 0; 699 } 700 701 static int rs_collect_tpc_data(struct iwl_mvm *mvm, 702 struct iwl_lq_sta *lq_sta, 703 struct iwl_scale_tbl_info *tbl, 704 int scale_index, int attempts, int successes, 705 u8 reduced_txp) 706 { 707 struct iwl_rate_scale_data *window = NULL; 708 709 if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION)) 710 return -EINVAL; 711 712 window = &tbl->tpc_win[reduced_txp]; 713 return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes, 714 window); 715 } 716 717 static void rs_update_tid_tpt_stats(struct iwl_mvm *mvm, 718 struct iwl_mvm_sta *mvmsta, 719 u8 tid, int successes) 720 { 721 struct iwl_mvm_tid_data *tid_data; 722 723 if (tid >= IWL_MAX_TID_COUNT) 724 return; 725 726 tid_data = &mvmsta->tid_data[tid]; 727 728 /* 729 * Measure if there're enough successful transmits per second. 730 * These statistics are used only to decide if we can start a 731 * BA session, so it should be updated only when A-MPDU is 732 * off. 733 */ 734 if (tid_data->state != IWL_AGG_OFF) 735 return; 736 737 if (time_is_before_jiffies(tid_data->tpt_meas_start + HZ) || 738 (tid_data->tx_count >= IWL_MVM_RS_AGG_START_THRESHOLD)) { 739 tid_data->tx_count_last = tid_data->tx_count; 740 tid_data->tx_count = 0; 741 tid_data->tpt_meas_start = jiffies; 742 } else { 743 tid_data->tx_count += successes; 744 } 745 } 746 747 static int rs_collect_tlc_data(struct iwl_mvm *mvm, 748 struct iwl_mvm_sta *mvmsta, u8 tid, 749 struct iwl_scale_tbl_info *tbl, 750 int scale_index, int attempts, int successes) 751 { 752 struct iwl_rate_scale_data *window = NULL; 753 754 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT) 755 return -EINVAL; 756 757 if (tbl->column != RS_COLUMN_INVALID) { 758 struct lq_sta_pers *pers = &mvmsta->deflink.lq_sta.rs_drv.pers; 759 760 pers->tx_stats[tbl->column][scale_index].total += attempts; 761 pers->tx_stats[tbl->column][scale_index].success += successes; 762 } 763 764 rs_update_tid_tpt_stats(mvm, mvmsta, tid, successes); 765 766 /* Select window for current tx bit rate */ 767 window = &(tbl->win[scale_index]); 768 return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes, 769 window); 770 } 771 772 /* Convert rs_rate object into ucode rate bitmask */ 773 static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm, 774 struct rs_rate *rate) 775 { 776 u32 ucode_rate = 0; 777 int index = rate->index; 778 779 ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) & 780 RATE_MCS_ANT_AB_MSK); 781 782 if (is_legacy(rate)) { 783 ucode_rate |= iwl_rates[index].plcp; 784 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE) 785 ucode_rate |= RATE_MCS_CCK_MSK_V1; 786 return ucode_rate; 787 } 788 789 /* set RTS protection for all non legacy rates 790 * This helps with congested environments reducing the conflict cost to 791 * RTS retries only, instead of the entire BA packet. 792 */ 793 ucode_rate |= RATE_MCS_RTS_REQUIRED_MSK; 794 795 if (is_ht(rate)) { 796 if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) { 797 IWL_ERR(mvm, "Invalid HT rate index %d\n", index); 798 index = IWL_LAST_HT_RATE; 799 } 800 ucode_rate |= RATE_MCS_HT_MSK_V1; 801 802 if (is_ht_siso(rate)) 803 ucode_rate |= iwl_rates[index].plcp_ht_siso; 804 else if (is_ht_mimo2(rate)) 805 ucode_rate |= iwl_rates[index].plcp_ht_mimo2; 806 else 807 WARN_ON_ONCE(1); 808 } else if (is_vht(rate)) { 809 if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) { 810 IWL_ERR(mvm, "Invalid VHT rate index %d\n", index); 811 index = IWL_LAST_VHT_RATE; 812 } 813 ucode_rate |= RATE_MCS_VHT_MSK_V1; 814 if (is_vht_siso(rate)) 815 ucode_rate |= iwl_rates[index].plcp_vht_siso; 816 else if (is_vht_mimo2(rate)) 817 ucode_rate |= iwl_rates[index].plcp_vht_mimo2; 818 else 819 WARN_ON_ONCE(1); 820 821 } else { 822 IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type); 823 } 824 825 if (is_siso(rate) && rate->stbc) { 826 /* To enable STBC we need to set both a flag and ANT_AB */ 827 ucode_rate |= RATE_MCS_ANT_AB_MSK; 828 ucode_rate |= RATE_MCS_STBC_MSK; 829 } 830 831 ucode_rate |= rate->bw; 832 if (rate->sgi) 833 ucode_rate |= RATE_MCS_SGI_MSK_V1; 834 if (rate->ldpc) 835 ucode_rate |= RATE_MCS_LDPC_MSK_V1; 836 837 return ucode_rate; 838 } 839 840 /* Convert a ucode rate into an rs_rate object */ 841 static int rs_rate_from_ucode_rate(const u32 ucode_rate, 842 enum nl80211_band band, 843 struct rs_rate *rate) 844 { 845 u32 ant_msk = ucode_rate & RATE_MCS_ANT_AB_MSK; 846 u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate); 847 u8 nss; 848 849 memset(rate, 0, sizeof(*rate)); 850 rate->index = iwl_hwrate_to_plcp_idx(ucode_rate); 851 852 if (rate->index == IWL_RATE_INVALID) 853 return -EINVAL; 854 855 rate->ant = (ant_msk >> RATE_MCS_ANT_POS); 856 857 /* Legacy */ 858 if (!(ucode_rate & RATE_MCS_HT_MSK_V1) && 859 !(ucode_rate & RATE_MCS_VHT_MSK_V1) && 860 !(ucode_rate & RATE_MCS_HE_MSK_V1)) { 861 if (num_of_ant == 1) { 862 if (band == NL80211_BAND_5GHZ) 863 rate->type = LQ_LEGACY_A; 864 else 865 rate->type = LQ_LEGACY_G; 866 } 867 868 return 0; 869 } 870 871 /* HT, VHT or HE */ 872 if (ucode_rate & RATE_MCS_SGI_MSK_V1) 873 rate->sgi = true; 874 if (ucode_rate & RATE_MCS_LDPC_MSK_V1) 875 rate->ldpc = true; 876 if (ucode_rate & RATE_MCS_STBC_MSK) 877 rate->stbc = true; 878 if (ucode_rate & RATE_MCS_BF_MSK) 879 rate->bfer = true; 880 881 rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK_V1; 882 883 if (ucode_rate & RATE_MCS_HT_MSK_V1) { 884 nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK_V1) >> 885 RATE_HT_MCS_NSS_POS_V1) + 1; 886 887 if (nss == 1) { 888 rate->type = LQ_HT_SISO; 889 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1, 890 "stbc %d bfer %d", 891 rate->stbc, rate->bfer); 892 } else if (nss == 2) { 893 rate->type = LQ_HT_MIMO2; 894 WARN_ON_ONCE(num_of_ant != 2); 895 } else { 896 WARN_ON_ONCE(1); 897 } 898 } else if (ucode_rate & RATE_MCS_VHT_MSK_V1) { 899 nss = FIELD_GET(RATE_MCS_NSS_MSK, ucode_rate) + 1; 900 901 if (nss == 1) { 902 rate->type = LQ_VHT_SISO; 903 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1, 904 "stbc %d bfer %d", 905 rate->stbc, rate->bfer); 906 } else if (nss == 2) { 907 rate->type = LQ_VHT_MIMO2; 908 WARN_ON_ONCE(num_of_ant != 2); 909 } else { 910 WARN_ON_ONCE(1); 911 } 912 } else if (ucode_rate & RATE_MCS_HE_MSK_V1) { 913 nss = FIELD_GET(RATE_MCS_NSS_MSK, ucode_rate) + 1; 914 915 if (nss == 1) { 916 rate->type = LQ_HE_SISO; 917 WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1, 918 "stbc %d bfer %d", rate->stbc, rate->bfer); 919 } else if (nss == 2) { 920 rate->type = LQ_HE_MIMO2; 921 WARN_ON_ONCE(num_of_ant != 2); 922 } else { 923 WARN_ON_ONCE(1); 924 } 925 } 926 927 WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 && 928 !is_he(rate) && !is_vht(rate)); 929 930 return 0; 931 } 932 933 /* switch to another antenna/antennas and return 1 */ 934 /* if no other valid antenna found, return 0 */ 935 static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate) 936 { 937 u8 new_ant_type; 938 939 if (!rs_is_valid_ant(valid_ant, rate->ant)) 940 return 0; 941 942 new_ant_type = ant_toggle_lookup[rate->ant]; 943 944 while ((new_ant_type != rate->ant) && 945 !rs_is_valid_ant(valid_ant, new_ant_type)) 946 new_ant_type = ant_toggle_lookup[new_ant_type]; 947 948 if (new_ant_type == rate->ant) 949 return 0; 950 951 rate->ant = new_ant_type; 952 953 return 1; 954 } 955 956 static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta, 957 struct rs_rate *rate) 958 { 959 if (is_legacy(rate)) 960 return lq_sta->active_legacy_rate; 961 else if (is_siso(rate)) 962 return lq_sta->active_siso_rate; 963 else if (is_mimo2(rate)) 964 return lq_sta->active_mimo2_rate; 965 966 WARN_ON_ONCE(1); 967 return 0; 968 } 969 970 static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask, 971 int rate_type) 972 { 973 u8 high = IWL_RATE_INVALID; 974 u8 low = IWL_RATE_INVALID; 975 976 /* 802.11A or ht walks to the next literal adjacent rate in 977 * the rate table */ 978 if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) { 979 int i; 980 u32 mask; 981 982 /* Find the previous rate that is in the rate mask */ 983 i = index - 1; 984 if (i >= 0) 985 mask = BIT(i); 986 for (; i >= 0; i--, mask >>= 1) { 987 if (rate_mask & mask) { 988 low = i; 989 break; 990 } 991 } 992 993 /* Find the next rate that is in the rate mask */ 994 i = index + 1; 995 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) { 996 if (rate_mask & mask) { 997 high = i; 998 break; 999 } 1000 } 1001 1002 return (high << 8) | low; 1003 } 1004 1005 low = index; 1006 while (low != IWL_RATE_INVALID) { 1007 low = iwl_rates[low].prev_rs; 1008 if (low == IWL_RATE_INVALID) 1009 break; 1010 if (rate_mask & (1 << low)) 1011 break; 1012 } 1013 1014 high = index; 1015 while (high != IWL_RATE_INVALID) { 1016 high = iwl_rates[high].next_rs; 1017 if (high == IWL_RATE_INVALID) 1018 break; 1019 if (rate_mask & (1 << high)) 1020 break; 1021 } 1022 1023 return (high << 8) | low; 1024 } 1025 1026 static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta, 1027 struct rs_rate *rate) 1028 { 1029 return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate); 1030 } 1031 1032 /* Get the next supported lower rate in the current column. 1033 * Return true if bottom rate in the current column was reached 1034 */ 1035 static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta, 1036 struct rs_rate *rate) 1037 { 1038 u8 low; 1039 u16 high_low; 1040 u16 rate_mask; 1041 struct iwl_mvm *mvm = lq_sta->pers.drv; 1042 1043 rate_mask = rs_get_supported_rates(lq_sta, rate); 1044 high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask, 1045 rate->type); 1046 low = high_low & 0xff; 1047 1048 /* Bottom rate of column reached */ 1049 if (low == IWL_RATE_INVALID) 1050 return true; 1051 1052 rate->index = low; 1053 return false; 1054 } 1055 1056 /* Get the next rate to use following a column downgrade */ 1057 static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta, 1058 struct rs_rate *rate) 1059 { 1060 struct iwl_mvm *mvm = lq_sta->pers.drv; 1061 1062 if (is_legacy(rate)) { 1063 /* No column to downgrade from Legacy */ 1064 return; 1065 } else if (is_siso(rate)) { 1066 /* Downgrade to Legacy if we were in SISO */ 1067 if (lq_sta->band == NL80211_BAND_5GHZ) 1068 rate->type = LQ_LEGACY_A; 1069 else 1070 rate->type = LQ_LEGACY_G; 1071 1072 rate->bw = RATE_MCS_CHAN_WIDTH_20; 1073 1074 if (WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX)) 1075 rate->index = rs_ht_to_legacy[IWL_RATE_MCS_0_INDEX]; 1076 else if (WARN_ON_ONCE(rate->index > IWL_RATE_MCS_9_INDEX)) 1077 rate->index = rs_ht_to_legacy[IWL_RATE_MCS_9_INDEX]; 1078 else 1079 rate->index = rs_ht_to_legacy[rate->index]; 1080 1081 rate->ldpc = false; 1082 } else { 1083 /* Downgrade to SISO with same MCS if in MIMO */ 1084 rate->type = is_vht_mimo2(rate) ? 1085 LQ_VHT_SISO : LQ_HT_SISO; 1086 } 1087 1088 if (num_of_ant(rate->ant) > 1) 1089 rate->ant = first_antenna(iwl_mvm_get_valid_tx_ant(mvm)); 1090 1091 /* Relevant in both switching to SISO or Legacy */ 1092 rate->sgi = false; 1093 1094 if (!rs_rate_supported(lq_sta, rate)) 1095 rs_get_lower_rate_in_column(lq_sta, rate); 1096 } 1097 1098 /* Check if both rates share the same column */ 1099 static inline bool rs_rate_column_match(struct rs_rate *a, 1100 struct rs_rate *b) 1101 { 1102 bool ant_match; 1103 1104 if (a->stbc || a->bfer) 1105 ant_match = (b->ant == ANT_A || b->ant == ANT_B); 1106 else 1107 ant_match = (a->ant == b->ant); 1108 1109 return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi) 1110 && ant_match; 1111 } 1112 1113 static inline enum rs_column rs_get_column_from_rate(struct rs_rate *rate) 1114 { 1115 if (is_legacy(rate)) { 1116 if (rate->ant == ANT_A) 1117 return RS_COLUMN_LEGACY_ANT_A; 1118 1119 if (rate->ant == ANT_B) 1120 return RS_COLUMN_LEGACY_ANT_B; 1121 1122 goto err; 1123 } 1124 1125 if (is_siso(rate)) { 1126 if (rate->ant == ANT_A || rate->stbc || rate->bfer) 1127 return rate->sgi ? RS_COLUMN_SISO_ANT_A_SGI : 1128 RS_COLUMN_SISO_ANT_A; 1129 1130 if (rate->ant == ANT_B) 1131 return rate->sgi ? RS_COLUMN_SISO_ANT_B_SGI : 1132 RS_COLUMN_SISO_ANT_B; 1133 1134 goto err; 1135 } 1136 1137 if (is_mimo(rate)) 1138 return rate->sgi ? RS_COLUMN_MIMO2_SGI : RS_COLUMN_MIMO2; 1139 1140 err: 1141 return RS_COLUMN_INVALID; 1142 } 1143 1144 static u8 rs_get_tid(struct ieee80211_hdr *hdr) 1145 { 1146 u8 tid = IWL_MAX_TID_COUNT; 1147 1148 if (ieee80211_is_data_qos(hdr->frame_control)) { 1149 u8 *qc = ieee80211_get_qos_ctl(hdr); 1150 tid = qc[0] & 0xf; 1151 } 1152 1153 if (unlikely(tid > IWL_MAX_TID_COUNT)) 1154 tid = IWL_MAX_TID_COUNT; 1155 1156 return tid; 1157 } 1158 1159 /* 1160 * mac80211 sends us Tx status 1161 */ 1162 static void rs_drv_mac80211_tx_status(void *mvm_r, 1163 struct ieee80211_supported_band *sband, 1164 struct ieee80211_sta *sta, void *priv_sta, 1165 struct sk_buff *skb) 1166 { 1167 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 1168 struct iwl_op_mode *op_mode = mvm_r; 1169 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode); 1170 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1171 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 1172 1173 if (!mvmsta->vif) 1174 return; 1175 1176 if (!ieee80211_is_data(hdr->frame_control) || 1177 info->flags & IEEE80211_TX_CTL_NO_ACK) 1178 return; 1179 1180 iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info, 1181 ieee80211_is_qos_nullfunc(hdr->frame_control)); 1182 } 1183 1184 /* 1185 * Begin a period of staying with a selected modulation mode. 1186 * Set "stay_in_tbl" flag to prevent any mode switches. 1187 * Set frame tx success limits according to legacy vs. high-throughput, 1188 * and reset overall (spanning all rates) tx success history statistics. 1189 * These control how long we stay using same modulation mode before 1190 * searching for a new mode. 1191 */ 1192 static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy, 1193 struct iwl_lq_sta *lq_sta) 1194 { 1195 IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n"); 1196 lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN; 1197 if (is_legacy) { 1198 lq_sta->table_count_limit = IWL_MVM_RS_LEGACY_TABLE_COUNT; 1199 lq_sta->max_failure_limit = IWL_MVM_RS_LEGACY_FAILURE_LIMIT; 1200 lq_sta->max_success_limit = IWL_MVM_RS_LEGACY_SUCCESS_LIMIT; 1201 } else { 1202 lq_sta->table_count_limit = IWL_MVM_RS_NON_LEGACY_TABLE_COUNT; 1203 lq_sta->max_failure_limit = IWL_MVM_RS_NON_LEGACY_FAILURE_LIMIT; 1204 lq_sta->max_success_limit = IWL_MVM_RS_NON_LEGACY_SUCCESS_LIMIT; 1205 } 1206 lq_sta->table_count = 0; 1207 lq_sta->total_failed = 0; 1208 lq_sta->total_success = 0; 1209 lq_sta->flush_timer = jiffies; 1210 lq_sta->visited_columns = 0; 1211 } 1212 1213 static inline int rs_get_max_rate_from_mask(unsigned long rate_mask) 1214 { 1215 if (rate_mask) 1216 return find_last_bit(&rate_mask, BITS_PER_LONG); 1217 return IWL_RATE_INVALID; 1218 } 1219 1220 static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta, 1221 const struct rs_tx_column *column) 1222 { 1223 switch (column->mode) { 1224 case RS_LEGACY: 1225 return lq_sta->max_legacy_rate_idx; 1226 case RS_SISO: 1227 return lq_sta->max_siso_rate_idx; 1228 case RS_MIMO2: 1229 return lq_sta->max_mimo2_rate_idx; 1230 default: 1231 WARN_ON_ONCE(1); 1232 } 1233 1234 return lq_sta->max_legacy_rate_idx; 1235 } 1236 1237 static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta, 1238 const struct rs_tx_column *column, 1239 u32 bw) 1240 { 1241 /* Used to choose among HT tables */ 1242 const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT]; 1243 1244 if (WARN_ON_ONCE(column->mode != RS_LEGACY && 1245 column->mode != RS_SISO && 1246 column->mode != RS_MIMO2)) 1247 return expected_tpt_legacy; 1248 1249 /* Legacy rates have only one table */ 1250 if (column->mode == RS_LEGACY) 1251 return expected_tpt_legacy; 1252 1253 ht_tbl_pointer = expected_tpt_mimo2_20MHz; 1254 /* Choose among many HT tables depending on number of streams 1255 * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation 1256 * status */ 1257 if (column->mode == RS_SISO) { 1258 switch (bw) { 1259 case RATE_MCS_CHAN_WIDTH_20: 1260 ht_tbl_pointer = expected_tpt_siso_20MHz; 1261 break; 1262 case RATE_MCS_CHAN_WIDTH_40: 1263 ht_tbl_pointer = expected_tpt_siso_40MHz; 1264 break; 1265 case RATE_MCS_CHAN_WIDTH_80: 1266 ht_tbl_pointer = expected_tpt_siso_80MHz; 1267 break; 1268 case RATE_MCS_CHAN_WIDTH_160: 1269 ht_tbl_pointer = expected_tpt_siso_160MHz; 1270 break; 1271 default: 1272 WARN_ON_ONCE(1); 1273 } 1274 } else if (column->mode == RS_MIMO2) { 1275 switch (bw) { 1276 case RATE_MCS_CHAN_WIDTH_20: 1277 ht_tbl_pointer = expected_tpt_mimo2_20MHz; 1278 break; 1279 case RATE_MCS_CHAN_WIDTH_40: 1280 ht_tbl_pointer = expected_tpt_mimo2_40MHz; 1281 break; 1282 case RATE_MCS_CHAN_WIDTH_80: 1283 ht_tbl_pointer = expected_tpt_mimo2_80MHz; 1284 break; 1285 case RATE_MCS_CHAN_WIDTH_160: 1286 ht_tbl_pointer = expected_tpt_mimo2_160MHz; 1287 break; 1288 default: 1289 WARN_ON_ONCE(1); 1290 } 1291 } else { 1292 WARN_ON_ONCE(1); 1293 } 1294 1295 if (!column->sgi && !lq_sta->is_agg) /* Normal */ 1296 return ht_tbl_pointer[0]; 1297 else if (column->sgi && !lq_sta->is_agg) /* SGI */ 1298 return ht_tbl_pointer[1]; 1299 else if (!column->sgi && lq_sta->is_agg) /* AGG */ 1300 return ht_tbl_pointer[2]; 1301 else /* AGG+SGI */ 1302 return ht_tbl_pointer[3]; 1303 } 1304 1305 static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta, 1306 struct iwl_scale_tbl_info *tbl) 1307 { 1308 struct rs_rate *rate = &tbl->rate; 1309 const struct rs_tx_column *column = &rs_tx_columns[tbl->column]; 1310 1311 tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw); 1312 } 1313 1314 /* rs uses two tables, one is active and the second is for searching better 1315 * configuration. This function, according to the index of the currently 1316 * active table returns the search table, which is located at the 1317 * index complementary to 1 according to the active table (active = 1, 1318 * search = 0 or active = 0, search = 1). 1319 * Since lq_info is an arary of size 2, make sure index cannot be out of bounds. 1320 */ 1321 static inline u8 rs_search_tbl(u8 active_tbl) 1322 { 1323 return (active_tbl ^ 1) & 1; 1324 } 1325 1326 static s32 rs_get_best_rate(struct iwl_mvm *mvm, 1327 struct iwl_lq_sta *lq_sta, 1328 struct iwl_scale_tbl_info *tbl, /* "search" */ 1329 unsigned long rate_mask, s8 index) 1330 { 1331 struct iwl_scale_tbl_info *active_tbl = 1332 &(lq_sta->lq_info[lq_sta->active_tbl]); 1333 s32 success_ratio = active_tbl->win[index].success_ratio; 1334 u16 expected_current_tpt = active_tbl->expected_tpt[index]; 1335 const u16 *tpt_tbl = tbl->expected_tpt; 1336 u16 high_low; 1337 u32 target_tpt; 1338 int rate_idx; 1339 1340 if (success_ratio >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) { 1341 target_tpt = 100 * expected_current_tpt; 1342 IWL_DEBUG_RATE(mvm, 1343 "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n", 1344 success_ratio, target_tpt); 1345 } else { 1346 target_tpt = lq_sta->last_tpt; 1347 IWL_DEBUG_RATE(mvm, 1348 "SR %d not that good. Find rate exceeding ACTUAL_TPT %d\n", 1349 success_ratio, target_tpt); 1350 } 1351 1352 rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG); 1353 1354 while (rate_idx != IWL_RATE_INVALID) { 1355 if (target_tpt < (100 * tpt_tbl[rate_idx])) 1356 break; 1357 1358 high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask, 1359 tbl->rate.type); 1360 1361 rate_idx = (high_low >> 8) & 0xff; 1362 } 1363 1364 IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n", 1365 rate_idx, target_tpt, 1366 rate_idx != IWL_RATE_INVALID ? 1367 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE); 1368 1369 return rate_idx; 1370 } 1371 1372 static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta) 1373 { 1374 struct ieee80211_sta_vht_cap *sta_vht_cap = &sta->deflink.vht_cap; 1375 struct ieee80211_vht_cap vht_cap = { 1376 .vht_cap_info = cpu_to_le32(sta_vht_cap->cap), 1377 .supp_mcs = sta_vht_cap->vht_mcs, 1378 }; 1379 1380 switch (sta->deflink.bandwidth) { 1381 case IEEE80211_STA_RX_BW_160: 1382 /* 1383 * Don't use 160 MHz if VHT extended NSS support 1384 * says we cannot use 2 streams, we don't want to 1385 * deal with this. 1386 * We only check MCS 0 - they will support that if 1387 * we got here at all and we don't care which MCS, 1388 * we want to determine a more global state. 1389 */ 1390 if (ieee80211_get_vht_max_nss(&vht_cap, 1391 IEEE80211_VHT_CHANWIDTH_160MHZ, 1392 0, true, 1393 sta->deflink.rx_nss) < sta->deflink.rx_nss) 1394 return RATE_MCS_CHAN_WIDTH_80; 1395 return RATE_MCS_CHAN_WIDTH_160; 1396 case IEEE80211_STA_RX_BW_80: 1397 return RATE_MCS_CHAN_WIDTH_80; 1398 case IEEE80211_STA_RX_BW_40: 1399 return RATE_MCS_CHAN_WIDTH_40; 1400 case IEEE80211_STA_RX_BW_20: 1401 default: 1402 return RATE_MCS_CHAN_WIDTH_20; 1403 } 1404 } 1405 1406 /* 1407 * Check whether we should continue using same modulation mode, or 1408 * begin search for a new mode, based on: 1409 * 1) # tx successes or failures while using this mode 1410 * 2) # times calling this function 1411 * 3) elapsed time in this mode (not used, for now) 1412 */ 1413 static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search) 1414 { 1415 struct iwl_scale_tbl_info *tbl; 1416 int active_tbl; 1417 int flush_interval_passed = 0; 1418 struct iwl_mvm *mvm; 1419 1420 mvm = lq_sta->pers.drv; 1421 active_tbl = lq_sta->active_tbl; 1422 1423 tbl = &(lq_sta->lq_info[active_tbl]); 1424 1425 /* If we've been disallowing search, see if we should now allow it */ 1426 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) { 1427 /* Elapsed time using current modulation mode */ 1428 if (lq_sta->flush_timer) 1429 flush_interval_passed = 1430 time_after(jiffies, 1431 (unsigned long)(lq_sta->flush_timer + 1432 (IWL_MVM_RS_STAY_IN_COLUMN_TIMEOUT * HZ))); 1433 1434 /* 1435 * Check if we should allow search for new modulation mode. 1436 * If many frames have failed or succeeded, or we've used 1437 * this same modulation for a long time, allow search, and 1438 * reset history stats that keep track of whether we should 1439 * allow a new search. Also (below) reset all bitmaps and 1440 * stats in active history. 1441 */ 1442 if (force_search || 1443 (lq_sta->total_failed > lq_sta->max_failure_limit) || 1444 (lq_sta->total_success > lq_sta->max_success_limit) || 1445 ((!lq_sta->search_better_tbl) && 1446 (lq_sta->flush_timer) && (flush_interval_passed))) { 1447 IWL_DEBUG_RATE(mvm, 1448 "LQ: stay is expired %d %d %d\n", 1449 lq_sta->total_failed, 1450 lq_sta->total_success, 1451 flush_interval_passed); 1452 1453 /* Allow search for new mode */ 1454 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED; 1455 IWL_DEBUG_RATE(mvm, 1456 "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n"); 1457 lq_sta->total_failed = 0; 1458 lq_sta->total_success = 0; 1459 lq_sta->flush_timer = 0; 1460 /* mark the current column as visited */ 1461 lq_sta->visited_columns = BIT(tbl->column); 1462 /* 1463 * Else if we've used this modulation mode enough repetitions 1464 * (regardless of elapsed time or success/failure), reset 1465 * history bitmaps and rate-specific stats for all rates in 1466 * active table. 1467 */ 1468 } else { 1469 lq_sta->table_count++; 1470 if (lq_sta->table_count >= 1471 lq_sta->table_count_limit) { 1472 lq_sta->table_count = 0; 1473 1474 IWL_DEBUG_RATE(mvm, 1475 "LQ: stay in table clear win\n"); 1476 rs_rate_scale_clear_tbl_windows(mvm, tbl); 1477 } 1478 } 1479 1480 /* If transitioning to allow "search", reset all history 1481 * bitmaps and stats in active table (this will become the new 1482 * "search" table). */ 1483 if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) { 1484 rs_rate_scale_clear_tbl_windows(mvm, tbl); 1485 } 1486 } 1487 } 1488 1489 static void rs_set_amsdu_len(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 1490 struct iwl_scale_tbl_info *tbl, 1491 enum rs_action scale_action) 1492 { 1493 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 1494 struct ieee80211_bss_conf *bss_conf = &mvmsta->vif->bss_conf; 1495 int i; 1496 1497 sta->deflink.agg.max_amsdu_len = 1498 rs_fw_get_max_amsdu_len(sta, bss_conf, &sta->deflink); 1499 1500 /* 1501 * In case TLC offload is not active amsdu_enabled is either 0xFFFF 1502 * or 0, since there is no per-TID alg. 1503 */ 1504 if ((!is_vht(&tbl->rate) && !is_ht(&tbl->rate)) || 1505 tbl->rate.index < IWL_RATE_MCS_5_INDEX || 1506 scale_action == RS_ACTION_DOWNSCALE) 1507 mvmsta->amsdu_enabled = 0; 1508 else 1509 mvmsta->amsdu_enabled = 0xFFFF; 1510 1511 if (bss_conf->he_support && 1512 !iwlwifi_mod_params.disable_11ax) 1513 mvmsta->max_amsdu_len = sta->deflink.agg.max_amsdu_len; 1514 else 1515 mvmsta->max_amsdu_len = 1516 min_t(int, sta->deflink.agg.max_amsdu_len, 8500); 1517 1518 sta->deflink.agg.max_rc_amsdu_len = mvmsta->max_amsdu_len; 1519 1520 for (i = 0; i < IWL_MAX_TID_COUNT; i++) { 1521 if (mvmsta->amsdu_enabled) 1522 sta->deflink.agg.max_tid_amsdu_len[i] = 1523 iwl_mvm_max_amsdu_size(mvm, sta, i); 1524 else 1525 /* 1526 * Not so elegant, but this will effectively 1527 * prevent AMSDU on this TID 1528 */ 1529 sta->deflink.agg.max_tid_amsdu_len[i] = 1; 1530 } 1531 } 1532 1533 /* 1534 * setup rate table in uCode 1535 */ 1536 static void rs_update_rate_tbl(struct iwl_mvm *mvm, 1537 struct ieee80211_sta *sta, 1538 struct iwl_lq_sta *lq_sta, 1539 struct iwl_scale_tbl_info *tbl) 1540 { 1541 rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate); 1542 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq); 1543 } 1544 1545 static bool rs_tweak_rate_tbl(struct iwl_mvm *mvm, 1546 struct ieee80211_sta *sta, 1547 struct iwl_lq_sta *lq_sta, 1548 struct iwl_scale_tbl_info *tbl, 1549 enum rs_action scale_action) 1550 { 1551 if (rs_bw_from_sta_bw(sta) != RATE_MCS_CHAN_WIDTH_80) 1552 return false; 1553 1554 if (!is_vht_siso(&tbl->rate)) 1555 return false; 1556 1557 if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_80) && 1558 (tbl->rate.index == IWL_RATE_MCS_0_INDEX) && 1559 (scale_action == RS_ACTION_DOWNSCALE)) { 1560 tbl->rate.bw = RATE_MCS_CHAN_WIDTH_20; 1561 tbl->rate.index = IWL_RATE_MCS_4_INDEX; 1562 IWL_DEBUG_RATE(mvm, "Switch 80Mhz SISO MCS0 -> 20Mhz MCS4\n"); 1563 goto tweaked; 1564 } 1565 1566 /* Go back to 80Mhz MCS1 only if we've established that 20Mhz MCS5 is 1567 * sustainable, i.e. we're past the test window. We can't go back 1568 * if MCS5 is just tested as this will happen always after switching 1569 * to 20Mhz MCS4 because the rate stats are cleared. 1570 */ 1571 if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_20) && 1572 (((tbl->rate.index == IWL_RATE_MCS_5_INDEX) && 1573 (scale_action == RS_ACTION_STAY)) || 1574 ((tbl->rate.index > IWL_RATE_MCS_5_INDEX) && 1575 (scale_action == RS_ACTION_UPSCALE)))) { 1576 tbl->rate.bw = RATE_MCS_CHAN_WIDTH_80; 1577 tbl->rate.index = IWL_RATE_MCS_1_INDEX; 1578 IWL_DEBUG_RATE(mvm, "Switch 20Mhz SISO MCS5 -> 80Mhz MCS1\n"); 1579 goto tweaked; 1580 } 1581 1582 return false; 1583 1584 tweaked: 1585 rs_set_expected_tpt_table(lq_sta, tbl); 1586 rs_rate_scale_clear_tbl_windows(mvm, tbl); 1587 return true; 1588 } 1589 1590 static enum rs_column rs_get_next_column(struct iwl_mvm *mvm, 1591 struct iwl_lq_sta *lq_sta, 1592 struct ieee80211_sta *sta, 1593 struct iwl_scale_tbl_info *tbl) 1594 { 1595 int i, j, max_rate; 1596 enum rs_column next_col_id; 1597 const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column]; 1598 const struct rs_tx_column *next_col; 1599 allow_column_func_t allow_func; 1600 u8 valid_ants = iwl_mvm_get_valid_tx_ant(mvm); 1601 const u16 *expected_tpt_tbl; 1602 u16 tpt, max_expected_tpt; 1603 1604 for (i = 0; i < MAX_NEXT_COLUMNS; i++) { 1605 next_col_id = curr_col->next_columns[i]; 1606 1607 if (next_col_id == RS_COLUMN_INVALID) 1608 continue; 1609 1610 if (lq_sta->visited_columns & BIT(next_col_id)) { 1611 IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n", 1612 next_col_id); 1613 continue; 1614 } 1615 1616 next_col = &rs_tx_columns[next_col_id]; 1617 1618 if (!rs_is_valid_ant(valid_ants, next_col->ant)) { 1619 IWL_DEBUG_RATE(mvm, 1620 "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n", 1621 next_col_id, valid_ants, next_col->ant); 1622 continue; 1623 } 1624 1625 for (j = 0; j < MAX_COLUMN_CHECKS; j++) { 1626 allow_func = next_col->checks[j]; 1627 if (allow_func && !allow_func(mvm, sta, &tbl->rate, 1628 next_col)) 1629 break; 1630 } 1631 1632 if (j != MAX_COLUMN_CHECKS) { 1633 IWL_DEBUG_RATE(mvm, 1634 "Skip column %d: not allowed (check %d failed)\n", 1635 next_col_id, j); 1636 1637 continue; 1638 } 1639 1640 tpt = lq_sta->last_tpt / 100; 1641 expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col, 1642 rs_bw_from_sta_bw(sta)); 1643 if (WARN_ON_ONCE(!expected_tpt_tbl)) 1644 continue; 1645 1646 max_rate = rs_get_max_allowed_rate(lq_sta, next_col); 1647 if (max_rate == IWL_RATE_INVALID) { 1648 IWL_DEBUG_RATE(mvm, 1649 "Skip column %d: no rate is allowed in this column\n", 1650 next_col_id); 1651 continue; 1652 } 1653 1654 max_expected_tpt = expected_tpt_tbl[max_rate]; 1655 if (tpt >= max_expected_tpt) { 1656 IWL_DEBUG_RATE(mvm, 1657 "Skip column %d: can't beat current TPT. Max expected %d current %d\n", 1658 next_col_id, max_expected_tpt, tpt); 1659 continue; 1660 } 1661 1662 IWL_DEBUG_RATE(mvm, 1663 "Found potential column %d. Max expected %d current %d\n", 1664 next_col_id, max_expected_tpt, tpt); 1665 break; 1666 } 1667 1668 if (i == MAX_NEXT_COLUMNS) 1669 return RS_COLUMN_INVALID; 1670 1671 return next_col_id; 1672 } 1673 1674 static int rs_switch_to_column(struct iwl_mvm *mvm, 1675 struct iwl_lq_sta *lq_sta, 1676 struct ieee80211_sta *sta, 1677 enum rs_column col_id) 1678 { 1679 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl]; 1680 struct iwl_scale_tbl_info *search_tbl = 1681 &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)]; 1682 struct rs_rate *rate = &search_tbl->rate; 1683 const struct rs_tx_column *column = &rs_tx_columns[col_id]; 1684 const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column]; 1685 unsigned long rate_mask = 0; 1686 u32 rate_idx = 0; 1687 1688 memcpy(search_tbl, tbl, offsetof(struct iwl_scale_tbl_info, win)); 1689 1690 rate->sgi = column->sgi; 1691 rate->ant = column->ant; 1692 1693 if (column->mode == RS_LEGACY) { 1694 if (lq_sta->band == NL80211_BAND_5GHZ) 1695 rate->type = LQ_LEGACY_A; 1696 else 1697 rate->type = LQ_LEGACY_G; 1698 1699 rate->bw = RATE_MCS_CHAN_WIDTH_20; 1700 rate->ldpc = false; 1701 rate_mask = lq_sta->active_legacy_rate; 1702 } else if (column->mode == RS_SISO) { 1703 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO; 1704 rate_mask = lq_sta->active_siso_rate; 1705 } else if (column->mode == RS_MIMO2) { 1706 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2; 1707 rate_mask = lq_sta->active_mimo2_rate; 1708 } else { 1709 WARN_ONCE(1, "Bad column mode"); 1710 } 1711 1712 if (column->mode != RS_LEGACY) { 1713 rate->bw = rs_bw_from_sta_bw(sta); 1714 rate->ldpc = lq_sta->ldpc; 1715 } 1716 1717 search_tbl->column = col_id; 1718 rs_set_expected_tpt_table(lq_sta, search_tbl); 1719 1720 lq_sta->visited_columns |= BIT(col_id); 1721 1722 /* Get the best matching rate if we're changing modes. e.g. 1723 * SISO->MIMO, LEGACY->SISO, MIMO->SISO 1724 */ 1725 if (curr_column->mode != column->mode) { 1726 rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl, 1727 rate_mask, rate->index); 1728 1729 if ((rate_idx == IWL_RATE_INVALID) || 1730 !(BIT(rate_idx) & rate_mask)) { 1731 IWL_DEBUG_RATE(mvm, 1732 "can not switch with index %d" 1733 " rate mask %lx\n", 1734 rate_idx, rate_mask); 1735 1736 goto err; 1737 } 1738 1739 rate->index = rate_idx; 1740 } 1741 1742 IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n", 1743 col_id, rate->index); 1744 1745 return 0; 1746 1747 err: 1748 rate->type = LQ_NONE; 1749 return -1; 1750 } 1751 1752 static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm, 1753 struct iwl_scale_tbl_info *tbl, 1754 s32 sr, int low, int high, 1755 int current_tpt, 1756 int low_tpt, int high_tpt) 1757 { 1758 enum rs_action action = RS_ACTION_STAY; 1759 1760 if ((sr <= RS_PERCENT(IWL_MVM_RS_SR_FORCE_DECREASE)) || 1761 (current_tpt == 0)) { 1762 IWL_DEBUG_RATE(mvm, 1763 "Decrease rate because of low SR\n"); 1764 return RS_ACTION_DOWNSCALE; 1765 } 1766 1767 if ((low_tpt == IWL_INVALID_VALUE) && 1768 (high_tpt == IWL_INVALID_VALUE) && 1769 (high != IWL_RATE_INVALID)) { 1770 IWL_DEBUG_RATE(mvm, 1771 "No data about high/low rates. Increase rate\n"); 1772 return RS_ACTION_UPSCALE; 1773 } 1774 1775 if ((high_tpt == IWL_INVALID_VALUE) && 1776 (high != IWL_RATE_INVALID) && 1777 (low_tpt != IWL_INVALID_VALUE) && 1778 (low_tpt < current_tpt)) { 1779 IWL_DEBUG_RATE(mvm, 1780 "No data about high rate and low rate is worse. Increase rate\n"); 1781 return RS_ACTION_UPSCALE; 1782 } 1783 1784 if ((high_tpt != IWL_INVALID_VALUE) && 1785 (high_tpt > current_tpt)) { 1786 IWL_DEBUG_RATE(mvm, 1787 "Higher rate is better. Increase rate\n"); 1788 return RS_ACTION_UPSCALE; 1789 } 1790 1791 if ((low_tpt != IWL_INVALID_VALUE) && 1792 (high_tpt != IWL_INVALID_VALUE) && 1793 (low_tpt < current_tpt) && 1794 (high_tpt < current_tpt)) { 1795 IWL_DEBUG_RATE(mvm, 1796 "Both high and low are worse. Maintain rate\n"); 1797 return RS_ACTION_STAY; 1798 } 1799 1800 if ((low_tpt != IWL_INVALID_VALUE) && 1801 (low_tpt > current_tpt)) { 1802 IWL_DEBUG_RATE(mvm, 1803 "Lower rate is better\n"); 1804 action = RS_ACTION_DOWNSCALE; 1805 goto out; 1806 } 1807 1808 if ((low_tpt == IWL_INVALID_VALUE) && 1809 (low != IWL_RATE_INVALID)) { 1810 IWL_DEBUG_RATE(mvm, 1811 "No data about lower rate\n"); 1812 action = RS_ACTION_DOWNSCALE; 1813 goto out; 1814 } 1815 1816 IWL_DEBUG_RATE(mvm, "Maintain rate\n"); 1817 1818 out: 1819 if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) { 1820 if (sr >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) { 1821 IWL_DEBUG_RATE(mvm, 1822 "SR is above NO DECREASE. Avoid downscale\n"); 1823 action = RS_ACTION_STAY; 1824 } else if (current_tpt > (100 * tbl->expected_tpt[low])) { 1825 IWL_DEBUG_RATE(mvm, 1826 "Current TPT is higher than max expected in low rate. Avoid downscale\n"); 1827 action = RS_ACTION_STAY; 1828 } else { 1829 IWL_DEBUG_RATE(mvm, "Decrease rate\n"); 1830 } 1831 } 1832 1833 return action; 1834 } 1835 1836 static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 1837 struct iwl_lq_sta *lq_sta) 1838 { 1839 /* Our chip supports Tx STBC and the peer is an HT/VHT STA which 1840 * supports STBC of at least 1*SS 1841 */ 1842 if (!lq_sta->stbc_capable) 1843 return false; 1844 1845 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) 1846 return false; 1847 1848 return true; 1849 } 1850 1851 static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index, 1852 int *weaker, int *stronger) 1853 { 1854 *weaker = index + IWL_MVM_RS_TPC_TX_POWER_STEP; 1855 if (*weaker > TPC_MAX_REDUCTION) 1856 *weaker = TPC_INVALID; 1857 1858 *stronger = index - IWL_MVM_RS_TPC_TX_POWER_STEP; 1859 if (*stronger < 0) 1860 *stronger = TPC_INVALID; 1861 } 1862 1863 static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 1864 struct rs_rate *rate, enum nl80211_band band) 1865 { 1866 int index = rate->index; 1867 bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM); 1868 bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION && 1869 !vif->cfg.ps); 1870 1871 IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n", 1872 cam, sta_ps_disabled); 1873 /* 1874 * allow tpc only if power management is enabled, or bt coex 1875 * activity grade allows it and we are on 2.4Ghz. 1876 */ 1877 if ((cam || sta_ps_disabled) && 1878 !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band)) 1879 return false; 1880 1881 IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type); 1882 if (is_legacy(rate)) 1883 return index == IWL_RATE_54M_INDEX; 1884 if (is_ht(rate)) 1885 return index == IWL_RATE_MCS_7_INDEX; 1886 if (is_vht(rate)) 1887 return index == IWL_RATE_MCS_9_INDEX; 1888 1889 WARN_ON_ONCE(1); 1890 return false; 1891 } 1892 1893 enum tpc_action { 1894 TPC_ACTION_STAY, 1895 TPC_ACTION_DECREASE, 1896 TPC_ACTION_INCREASE, 1897 TPC_ACTION_NO_RESTIRCTION, 1898 }; 1899 1900 static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm, 1901 s32 sr, int weak, int strong, 1902 int current_tpt, 1903 int weak_tpt, int strong_tpt) 1904 { 1905 /* stay until we have valid tpt */ 1906 if (current_tpt == IWL_INVALID_VALUE) { 1907 IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n"); 1908 return TPC_ACTION_STAY; 1909 } 1910 1911 /* Too many failures, increase txp */ 1912 if (sr <= RS_PERCENT(IWL_MVM_RS_TPC_SR_FORCE_INCREASE) || 1913 current_tpt == 0) { 1914 IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n"); 1915 return TPC_ACTION_NO_RESTIRCTION; 1916 } 1917 1918 /* try decreasing first if applicable */ 1919 if (sr >= RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) && 1920 weak != TPC_INVALID) { 1921 if (weak_tpt == IWL_INVALID_VALUE && 1922 (strong_tpt == IWL_INVALID_VALUE || 1923 current_tpt >= strong_tpt)) { 1924 IWL_DEBUG_RATE(mvm, 1925 "no weak txp measurement. decrease txp\n"); 1926 return TPC_ACTION_DECREASE; 1927 } 1928 1929 if (weak_tpt > current_tpt) { 1930 IWL_DEBUG_RATE(mvm, 1931 "lower txp has better tpt. decrease txp\n"); 1932 return TPC_ACTION_DECREASE; 1933 } 1934 } 1935 1936 /* next, increase if needed */ 1937 if (sr < RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) && 1938 strong != TPC_INVALID) { 1939 if (weak_tpt == IWL_INVALID_VALUE && 1940 strong_tpt != IWL_INVALID_VALUE && 1941 current_tpt < strong_tpt) { 1942 IWL_DEBUG_RATE(mvm, 1943 "higher txp has better tpt. increase txp\n"); 1944 return TPC_ACTION_INCREASE; 1945 } 1946 1947 if (weak_tpt < current_tpt && 1948 (strong_tpt == IWL_INVALID_VALUE || 1949 strong_tpt > current_tpt)) { 1950 IWL_DEBUG_RATE(mvm, 1951 "lower txp has worse tpt. increase txp\n"); 1952 return TPC_ACTION_INCREASE; 1953 } 1954 } 1955 1956 IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n"); 1957 return TPC_ACTION_STAY; 1958 } 1959 1960 static bool rs_tpc_perform(struct iwl_mvm *mvm, 1961 struct ieee80211_sta *sta, 1962 struct iwl_lq_sta *lq_sta, 1963 struct iwl_scale_tbl_info *tbl) 1964 { 1965 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 1966 struct ieee80211_vif *vif = mvm_sta->vif; 1967 struct ieee80211_chanctx_conf *chanctx_conf; 1968 enum nl80211_band band; 1969 struct iwl_rate_scale_data *window; 1970 struct rs_rate *rate = &tbl->rate; 1971 enum tpc_action action; 1972 s32 sr; 1973 u8 cur = lq_sta->lq.reduced_tpc; 1974 int current_tpt; 1975 int weak, strong; 1976 int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE; 1977 1978 #ifdef CONFIG_MAC80211_DEBUGFS 1979 if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) { 1980 IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n", 1981 lq_sta->pers.dbg_fixed_txp_reduction); 1982 lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction; 1983 return cur != lq_sta->pers.dbg_fixed_txp_reduction; 1984 } 1985 #endif 1986 1987 rcu_read_lock(); 1988 chanctx_conf = rcu_dereference(vif->bss_conf.chanctx_conf); 1989 if (WARN_ON(!chanctx_conf)) 1990 band = NUM_NL80211_BANDS; 1991 else 1992 band = chanctx_conf->def.chan->band; 1993 rcu_read_unlock(); 1994 1995 if (!rs_tpc_allowed(mvm, vif, rate, band)) { 1996 IWL_DEBUG_RATE(mvm, 1997 "tpc is not allowed. remove txp restrictions\n"); 1998 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION; 1999 return cur != TPC_NO_REDUCTION; 2000 } 2001 2002 rs_get_adjacent_txp(mvm, cur, &weak, &strong); 2003 2004 /* Collect measured throughputs for current and adjacent rates */ 2005 window = tbl->tpc_win; 2006 sr = window[cur].success_ratio; 2007 current_tpt = window[cur].average_tpt; 2008 if (weak != TPC_INVALID) 2009 weak_tpt = window[weak].average_tpt; 2010 if (strong != TPC_INVALID) 2011 strong_tpt = window[strong].average_tpt; 2012 2013 IWL_DEBUG_RATE(mvm, 2014 "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n", 2015 cur, current_tpt, sr, weak, strong, 2016 weak_tpt, strong_tpt); 2017 2018 action = rs_get_tpc_action(mvm, sr, weak, strong, 2019 current_tpt, weak_tpt, strong_tpt); 2020 2021 /* override actions if we are on the edge */ 2022 if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) { 2023 IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n"); 2024 action = TPC_ACTION_STAY; 2025 } else if (strong == TPC_INVALID && 2026 (action == TPC_ACTION_INCREASE || 2027 action == TPC_ACTION_NO_RESTIRCTION)) { 2028 IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n"); 2029 action = TPC_ACTION_STAY; 2030 } 2031 2032 switch (action) { 2033 case TPC_ACTION_DECREASE: 2034 lq_sta->lq.reduced_tpc = weak; 2035 return true; 2036 case TPC_ACTION_INCREASE: 2037 lq_sta->lq.reduced_tpc = strong; 2038 return true; 2039 case TPC_ACTION_NO_RESTIRCTION: 2040 lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION; 2041 return true; 2042 case TPC_ACTION_STAY: 2043 /* do nothing */ 2044 break; 2045 } 2046 return false; 2047 } 2048 2049 /* 2050 * Do rate scaling and search for new modulation mode. 2051 */ 2052 static void rs_rate_scale_perform(struct iwl_mvm *mvm, 2053 struct ieee80211_sta *sta, 2054 struct iwl_lq_sta *lq_sta, 2055 int tid, bool ndp) 2056 { 2057 int low = IWL_RATE_INVALID; 2058 int high = IWL_RATE_INVALID; 2059 int index; 2060 struct iwl_rate_scale_data *window = NULL; 2061 int current_tpt = IWL_INVALID_VALUE; 2062 int low_tpt = IWL_INVALID_VALUE; 2063 int high_tpt = IWL_INVALID_VALUE; 2064 u32 fail_count; 2065 enum rs_action scale_action = RS_ACTION_STAY; 2066 u16 rate_mask; 2067 u8 update_lq = 0; 2068 struct iwl_scale_tbl_info *tbl, *tbl1; 2069 u8 active_tbl = 0; 2070 u8 done_search = 0; 2071 u16 high_low; 2072 s32 sr; 2073 u8 prev_agg = lq_sta->is_agg; 2074 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 2075 struct rs_rate *rate; 2076 2077 lq_sta->is_agg = !!mvmsta->agg_tids; 2078 2079 /* 2080 * Select rate-scale / modulation-mode table to work with in 2081 * the rest of this function: "search" if searching for better 2082 * modulation mode, or "active" if doing rate scaling within a mode. 2083 */ 2084 if (!lq_sta->search_better_tbl) 2085 active_tbl = lq_sta->active_tbl; 2086 else 2087 active_tbl = rs_search_tbl(lq_sta->active_tbl); 2088 2089 tbl = &(lq_sta->lq_info[active_tbl]); 2090 rate = &tbl->rate; 2091 2092 if (prev_agg != lq_sta->is_agg) { 2093 IWL_DEBUG_RATE(mvm, 2094 "Aggregation changed: prev %d current %d. Update expected TPT table\n", 2095 prev_agg, lq_sta->is_agg); 2096 rs_set_expected_tpt_table(lq_sta, tbl); 2097 rs_rate_scale_clear_tbl_windows(mvm, tbl); 2098 } 2099 2100 /* current tx rate */ 2101 index = rate->index; 2102 2103 /* rates available for this association, and for modulation mode */ 2104 rate_mask = rs_get_supported_rates(lq_sta, rate); 2105 2106 if (!(BIT(index) & rate_mask)) { 2107 IWL_ERR(mvm, "Current Rate is not valid\n"); 2108 if (lq_sta->search_better_tbl) { 2109 /* revert to active table if search table is not valid*/ 2110 rate->type = LQ_NONE; 2111 lq_sta->search_better_tbl = 0; 2112 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); 2113 rs_update_rate_tbl(mvm, sta, lq_sta, tbl); 2114 } 2115 return; 2116 } 2117 2118 /* Get expected throughput table and history window for current rate */ 2119 if (!tbl->expected_tpt) { 2120 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n"); 2121 return; 2122 } 2123 2124 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */ 2125 window = &(tbl->win[index]); 2126 2127 /* 2128 * If there is not enough history to calculate actual average 2129 * throughput, keep analyzing results of more tx frames, without 2130 * changing rate or mode (bypass most of the rest of this function). 2131 * Set up new rate table in uCode only if old rate is not supported 2132 * in current association (use new rate found above). 2133 */ 2134 fail_count = window->counter - window->success_counter; 2135 if ((fail_count < IWL_MVM_RS_RATE_MIN_FAILURE_TH) && 2136 (window->success_counter < IWL_MVM_RS_RATE_MIN_SUCCESS_TH)) { 2137 IWL_DEBUG_RATE(mvm, 2138 "%s: Test Window: succ %d total %d\n", 2139 rs_pretty_rate(rate), 2140 window->success_counter, window->counter); 2141 2142 /* Can't calculate this yet; not enough history */ 2143 window->average_tpt = IWL_INVALID_VALUE; 2144 2145 /* Should we stay with this modulation mode, 2146 * or search for a new one? */ 2147 rs_stay_in_table(lq_sta, false); 2148 2149 return; 2150 } 2151 2152 /* If we are searching for better modulation mode, check success. */ 2153 if (lq_sta->search_better_tbl) { 2154 /* If good success, continue using the "search" mode; 2155 * no need to send new link quality command, since we're 2156 * continuing to use the setup that we've been trying. */ 2157 if (window->average_tpt > lq_sta->last_tpt) { 2158 IWL_DEBUG_RATE(mvm, 2159 "SWITCHING TO NEW TABLE SR: %d " 2160 "cur-tpt %d old-tpt %d\n", 2161 window->success_ratio, 2162 window->average_tpt, 2163 lq_sta->last_tpt); 2164 2165 /* Swap tables; "search" becomes "active" */ 2166 lq_sta->active_tbl = active_tbl; 2167 current_tpt = window->average_tpt; 2168 /* Else poor success; go back to mode in "active" table */ 2169 } else { 2170 IWL_DEBUG_RATE(mvm, 2171 "GOING BACK TO THE OLD TABLE: SR %d " 2172 "cur-tpt %d old-tpt %d\n", 2173 window->success_ratio, 2174 window->average_tpt, 2175 lq_sta->last_tpt); 2176 2177 /* Nullify "search" table */ 2178 rate->type = LQ_NONE; 2179 2180 /* Revert to "active" table */ 2181 active_tbl = lq_sta->active_tbl; 2182 tbl = &(lq_sta->lq_info[active_tbl]); 2183 2184 /* Revert to "active" rate and throughput info */ 2185 index = tbl->rate.index; 2186 current_tpt = lq_sta->last_tpt; 2187 2188 /* Need to set up a new rate table in uCode */ 2189 update_lq = 1; 2190 } 2191 2192 /* Either way, we've made a decision; modulation mode 2193 * search is done, allow rate adjustment next time. */ 2194 lq_sta->search_better_tbl = 0; 2195 done_search = 1; /* Don't switch modes below! */ 2196 goto lq_update; 2197 } 2198 2199 /* (Else) not in search of better modulation mode, try for better 2200 * starting rate, while staying in this mode. */ 2201 high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type); 2202 low = high_low & 0xff; 2203 high = (high_low >> 8) & 0xff; 2204 2205 /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */ 2206 2207 sr = window->success_ratio; 2208 2209 /* Collect measured throughputs for current and adjacent rates */ 2210 current_tpt = window->average_tpt; 2211 if (low != IWL_RATE_INVALID) 2212 low_tpt = tbl->win[low].average_tpt; 2213 if (high != IWL_RATE_INVALID) 2214 high_tpt = tbl->win[high].average_tpt; 2215 2216 IWL_DEBUG_RATE(mvm, 2217 "%s: cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n", 2218 rs_pretty_rate(rate), current_tpt, sr, 2219 low, high, low_tpt, high_tpt); 2220 2221 scale_action = rs_get_rate_action(mvm, tbl, sr, low, high, 2222 current_tpt, low_tpt, high_tpt); 2223 2224 /* Force a search in case BT doesn't like us being in MIMO */ 2225 if (is_mimo(rate) && 2226 !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) { 2227 IWL_DEBUG_RATE(mvm, 2228 "BT Coex forbids MIMO. Search for new config\n"); 2229 rs_stay_in_table(lq_sta, true); 2230 goto lq_update; 2231 } 2232 2233 switch (scale_action) { 2234 case RS_ACTION_DOWNSCALE: 2235 /* Decrease starting rate, update uCode's rate table */ 2236 if (low != IWL_RATE_INVALID) { 2237 update_lq = 1; 2238 index = low; 2239 } else { 2240 IWL_DEBUG_RATE(mvm, 2241 "At the bottom rate. Can't decrease\n"); 2242 } 2243 2244 break; 2245 case RS_ACTION_UPSCALE: 2246 /* Increase starting rate, update uCode's rate table */ 2247 if (high != IWL_RATE_INVALID) { 2248 update_lq = 1; 2249 index = high; 2250 } else { 2251 IWL_DEBUG_RATE(mvm, 2252 "At the top rate. Can't increase\n"); 2253 } 2254 2255 break; 2256 case RS_ACTION_STAY: 2257 /* No change */ 2258 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) 2259 update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl); 2260 break; 2261 default: 2262 break; 2263 } 2264 2265 lq_update: 2266 /* Replace uCode's rate table for the destination station. */ 2267 if (update_lq) { 2268 tbl->rate.index = index; 2269 if (IWL_MVM_RS_80_20_FAR_RANGE_TWEAK) 2270 rs_tweak_rate_tbl(mvm, sta, lq_sta, tbl, scale_action); 2271 rs_set_amsdu_len(mvm, sta, tbl, scale_action); 2272 rs_update_rate_tbl(mvm, sta, lq_sta, tbl); 2273 } 2274 2275 rs_stay_in_table(lq_sta, false); 2276 2277 /* 2278 * Search for new modulation mode if we're: 2279 * 1) Not changing rates right now 2280 * 2) Not just finishing up a search 2281 * 3) Allowing a new search 2282 */ 2283 if (!update_lq && !done_search && 2284 lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED 2285 && window->counter) { 2286 enum rs_column next_column; 2287 2288 /* Save current throughput to compare with "search" throughput*/ 2289 lq_sta->last_tpt = current_tpt; 2290 2291 IWL_DEBUG_RATE(mvm, 2292 "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n", 2293 update_lq, done_search, lq_sta->rs_state, 2294 window->counter); 2295 2296 next_column = rs_get_next_column(mvm, lq_sta, sta, tbl); 2297 if (next_column != RS_COLUMN_INVALID) { 2298 int ret = rs_switch_to_column(mvm, lq_sta, sta, 2299 next_column); 2300 if (!ret) 2301 lq_sta->search_better_tbl = 1; 2302 } else { 2303 IWL_DEBUG_RATE(mvm, 2304 "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n"); 2305 lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED; 2306 } 2307 2308 /* If new "search" mode was selected, set up in uCode table */ 2309 if (lq_sta->search_better_tbl) { 2310 /* Access the "search" table, clear its history. */ 2311 tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)]; 2312 rs_rate_scale_clear_tbl_windows(mvm, tbl); 2313 2314 /* Use new "search" start rate */ 2315 index = tbl->rate.index; 2316 2317 rs_dump_rate(mvm, &tbl->rate, 2318 "Switch to SEARCH TABLE:"); 2319 rs_update_rate_tbl(mvm, sta, lq_sta, tbl); 2320 } else { 2321 done_search = 1; 2322 } 2323 } 2324 2325 if (!ndp) 2326 rs_tl_turn_on_agg(mvm, mvmsta, tid, lq_sta, sta); 2327 2328 if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) { 2329 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]); 2330 rs_set_stay_in_table(mvm, is_legacy(&tbl1->rate), lq_sta); 2331 } 2332 } 2333 2334 struct rs_init_rate_info { 2335 s8 rssi; 2336 u8 rate_idx; 2337 }; 2338 2339 static const struct rs_init_rate_info rs_optimal_rates_24ghz_legacy[] = { 2340 { -60, IWL_RATE_54M_INDEX }, 2341 { -64, IWL_RATE_48M_INDEX }, 2342 { -68, IWL_RATE_36M_INDEX }, 2343 { -80, IWL_RATE_24M_INDEX }, 2344 { -84, IWL_RATE_18M_INDEX }, 2345 { -85, IWL_RATE_12M_INDEX }, 2346 { -86, IWL_RATE_11M_INDEX }, 2347 { -88, IWL_RATE_5M_INDEX }, 2348 { -90, IWL_RATE_2M_INDEX }, 2349 { S8_MIN, IWL_RATE_1M_INDEX }, 2350 }; 2351 2352 static const struct rs_init_rate_info rs_optimal_rates_5ghz_legacy[] = { 2353 { -60, IWL_RATE_54M_INDEX }, 2354 { -64, IWL_RATE_48M_INDEX }, 2355 { -72, IWL_RATE_36M_INDEX }, 2356 { -80, IWL_RATE_24M_INDEX }, 2357 { -84, IWL_RATE_18M_INDEX }, 2358 { -85, IWL_RATE_12M_INDEX }, 2359 { -87, IWL_RATE_9M_INDEX }, 2360 { S8_MIN, IWL_RATE_6M_INDEX }, 2361 }; 2362 2363 static const struct rs_init_rate_info rs_optimal_rates_ht[] = { 2364 { -60, IWL_RATE_MCS_7_INDEX }, 2365 { -64, IWL_RATE_MCS_6_INDEX }, 2366 { -68, IWL_RATE_MCS_5_INDEX }, 2367 { -72, IWL_RATE_MCS_4_INDEX }, 2368 { -80, IWL_RATE_MCS_3_INDEX }, 2369 { -84, IWL_RATE_MCS_2_INDEX }, 2370 { -85, IWL_RATE_MCS_1_INDEX }, 2371 { S8_MIN, IWL_RATE_MCS_0_INDEX}, 2372 }; 2373 2374 /* MCS index 9 is not valid for 20MHz VHT channel width, 2375 * but is ok for 40, 80 and 160MHz channels. 2376 */ 2377 static const struct rs_init_rate_info rs_optimal_rates_vht_20mhz[] = { 2378 { -60, IWL_RATE_MCS_8_INDEX }, 2379 { -64, IWL_RATE_MCS_7_INDEX }, 2380 { -68, IWL_RATE_MCS_6_INDEX }, 2381 { -72, IWL_RATE_MCS_5_INDEX }, 2382 { -80, IWL_RATE_MCS_4_INDEX }, 2383 { -84, IWL_RATE_MCS_3_INDEX }, 2384 { -85, IWL_RATE_MCS_2_INDEX }, 2385 { -87, IWL_RATE_MCS_1_INDEX }, 2386 { S8_MIN, IWL_RATE_MCS_0_INDEX}, 2387 }; 2388 2389 static const struct rs_init_rate_info rs_optimal_rates_vht[] = { 2390 { -60, IWL_RATE_MCS_9_INDEX }, 2391 { -64, IWL_RATE_MCS_8_INDEX }, 2392 { -68, IWL_RATE_MCS_7_INDEX }, 2393 { -72, IWL_RATE_MCS_6_INDEX }, 2394 { -80, IWL_RATE_MCS_5_INDEX }, 2395 { -84, IWL_RATE_MCS_4_INDEX }, 2396 { -85, IWL_RATE_MCS_3_INDEX }, 2397 { -87, IWL_RATE_MCS_2_INDEX }, 2398 { -88, IWL_RATE_MCS_1_INDEX }, 2399 { S8_MIN, IWL_RATE_MCS_0_INDEX }, 2400 }; 2401 2402 #define IWL_RS_LOW_RSSI_THRESHOLD (-76) /* dBm */ 2403 2404 /* Init the optimal rate based on STA caps 2405 * This combined with rssi is used to report the last tx rate 2406 * to userspace when we haven't transmitted enough frames. 2407 */ 2408 static void rs_init_optimal_rate(struct iwl_mvm *mvm, 2409 struct ieee80211_sta *sta, 2410 struct iwl_lq_sta *lq_sta) 2411 { 2412 struct rs_rate *rate = &lq_sta->optimal_rate; 2413 2414 if (lq_sta->max_mimo2_rate_idx != IWL_RATE_INVALID) 2415 rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2; 2416 else if (lq_sta->max_siso_rate_idx != IWL_RATE_INVALID) 2417 rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO; 2418 else if (lq_sta->band == NL80211_BAND_5GHZ) 2419 rate->type = LQ_LEGACY_A; 2420 else 2421 rate->type = LQ_LEGACY_G; 2422 2423 rate->bw = rs_bw_from_sta_bw(sta); 2424 rate->sgi = rs_sgi_allow(mvm, sta, rate, NULL); 2425 2426 /* ANT/LDPC/STBC aren't relevant for the rate reported to userspace */ 2427 2428 if (is_mimo(rate)) { 2429 lq_sta->optimal_rate_mask = lq_sta->active_mimo2_rate; 2430 } else if (is_siso(rate)) { 2431 lq_sta->optimal_rate_mask = lq_sta->active_siso_rate; 2432 } else { 2433 lq_sta->optimal_rate_mask = lq_sta->active_legacy_rate; 2434 2435 if (lq_sta->band == NL80211_BAND_5GHZ) { 2436 lq_sta->optimal_rates = rs_optimal_rates_5ghz_legacy; 2437 lq_sta->optimal_nentries = 2438 ARRAY_SIZE(rs_optimal_rates_5ghz_legacy); 2439 } else { 2440 lq_sta->optimal_rates = rs_optimal_rates_24ghz_legacy; 2441 lq_sta->optimal_nentries = 2442 ARRAY_SIZE(rs_optimal_rates_24ghz_legacy); 2443 } 2444 } 2445 2446 if (is_vht(rate)) { 2447 if (rate->bw == RATE_MCS_CHAN_WIDTH_20) { 2448 lq_sta->optimal_rates = rs_optimal_rates_vht_20mhz; 2449 lq_sta->optimal_nentries = 2450 ARRAY_SIZE(rs_optimal_rates_vht_20mhz); 2451 } else { 2452 lq_sta->optimal_rates = rs_optimal_rates_vht; 2453 lq_sta->optimal_nentries = 2454 ARRAY_SIZE(rs_optimal_rates_vht); 2455 } 2456 } else if (is_ht(rate)) { 2457 lq_sta->optimal_rates = rs_optimal_rates_ht; 2458 lq_sta->optimal_nentries = ARRAY_SIZE(rs_optimal_rates_ht); 2459 } 2460 } 2461 2462 /* Compute the optimal rate index based on RSSI */ 2463 static struct rs_rate *rs_get_optimal_rate(struct iwl_mvm *mvm, 2464 struct iwl_lq_sta *lq_sta) 2465 { 2466 struct rs_rate *rate = &lq_sta->optimal_rate; 2467 int i; 2468 2469 rate->index = find_first_bit(&lq_sta->optimal_rate_mask, 2470 BITS_PER_LONG); 2471 2472 for (i = 0; i < lq_sta->optimal_nentries; i++) { 2473 int rate_idx = lq_sta->optimal_rates[i].rate_idx; 2474 2475 if ((lq_sta->pers.last_rssi >= lq_sta->optimal_rates[i].rssi) && 2476 (BIT(rate_idx) & lq_sta->optimal_rate_mask)) { 2477 rate->index = rate_idx; 2478 break; 2479 } 2480 } 2481 2482 return rate; 2483 } 2484 2485 /* Choose an initial legacy rate and antenna to use based on the RSSI 2486 * of last Rx 2487 */ 2488 static void rs_get_initial_rate(struct iwl_mvm *mvm, 2489 struct ieee80211_sta *sta, 2490 struct iwl_lq_sta *lq_sta, 2491 enum nl80211_band band, 2492 struct rs_rate *rate) 2493 { 2494 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 2495 int i, nentries; 2496 unsigned long active_rate; 2497 s8 best_rssi = S8_MIN; 2498 u8 best_ant = ANT_NONE; 2499 u8 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm); 2500 const struct rs_init_rate_info *initial_rates; 2501 2502 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) { 2503 if (!(lq_sta->pers.chains & BIT(i))) 2504 continue; 2505 2506 if (lq_sta->pers.chain_signal[i] > best_rssi) { 2507 best_rssi = lq_sta->pers.chain_signal[i]; 2508 best_ant = BIT(i); 2509 } 2510 } 2511 2512 IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n", 2513 iwl_rs_pretty_ant(best_ant), best_rssi); 2514 2515 if (best_ant != ANT_A && best_ant != ANT_B) 2516 rate->ant = first_antenna(valid_tx_ant); 2517 else 2518 rate->ant = best_ant; 2519 2520 rate->sgi = false; 2521 rate->ldpc = false; 2522 rate->bw = RATE_MCS_CHAN_WIDTH_20; 2523 2524 rate->index = find_first_bit(&lq_sta->active_legacy_rate, 2525 BITS_PER_LONG); 2526 2527 if (band == NL80211_BAND_5GHZ) { 2528 rate->type = LQ_LEGACY_A; 2529 initial_rates = rs_optimal_rates_5ghz_legacy; 2530 nentries = ARRAY_SIZE(rs_optimal_rates_5ghz_legacy); 2531 } else { 2532 rate->type = LQ_LEGACY_G; 2533 initial_rates = rs_optimal_rates_24ghz_legacy; 2534 nentries = ARRAY_SIZE(rs_optimal_rates_24ghz_legacy); 2535 } 2536 2537 if (!IWL_MVM_RS_RSSI_BASED_INIT_RATE) 2538 goto out; 2539 2540 /* Start from a higher rate if the corresponding debug capability 2541 * is enabled. The rate is chosen according to AP capabilities. 2542 * In case of VHT/HT when the rssi is low fallback to the case of 2543 * legacy rates. 2544 */ 2545 if (sta->deflink.vht_cap.vht_supported && 2546 best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) { 2547 /* 2548 * In AP mode, when a new station associates, rs is initialized 2549 * immediately upon association completion, before the phy 2550 * context is updated with the association parameters, so the 2551 * sta bandwidth might be wider than the phy context allows. 2552 * To avoid this issue, always initialize rs with 20mhz 2553 * bandwidth rate, and after authorization, when the phy context 2554 * is already up-to-date, re-init rs with the correct bw. 2555 */ 2556 u32 bw = mvmsta->sta_state < IEEE80211_STA_AUTHORIZED ? 2557 RATE_MCS_CHAN_WIDTH_20 : rs_bw_from_sta_bw(sta); 2558 2559 switch (bw) { 2560 case RATE_MCS_CHAN_WIDTH_40: 2561 case RATE_MCS_CHAN_WIDTH_80: 2562 case RATE_MCS_CHAN_WIDTH_160: 2563 initial_rates = rs_optimal_rates_vht; 2564 nentries = ARRAY_SIZE(rs_optimal_rates_vht); 2565 break; 2566 case RATE_MCS_CHAN_WIDTH_20: 2567 initial_rates = rs_optimal_rates_vht_20mhz; 2568 nentries = ARRAY_SIZE(rs_optimal_rates_vht_20mhz); 2569 break; 2570 default: 2571 IWL_ERR(mvm, "Invalid BW %d\n", 2572 sta->deflink.bandwidth); 2573 goto out; 2574 } 2575 2576 active_rate = lq_sta->active_siso_rate; 2577 rate->type = LQ_VHT_SISO; 2578 rate->bw = bw; 2579 } else if (sta->deflink.ht_cap.ht_supported && 2580 best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) { 2581 initial_rates = rs_optimal_rates_ht; 2582 nentries = ARRAY_SIZE(rs_optimal_rates_ht); 2583 active_rate = lq_sta->active_siso_rate; 2584 rate->type = LQ_HT_SISO; 2585 } else { 2586 active_rate = lq_sta->active_legacy_rate; 2587 } 2588 2589 for (i = 0; i < nentries; i++) { 2590 int rate_idx = initial_rates[i].rate_idx; 2591 2592 if ((best_rssi >= initial_rates[i].rssi) && 2593 (BIT(rate_idx) & active_rate)) { 2594 rate->index = rate_idx; 2595 break; 2596 } 2597 } 2598 2599 out: 2600 rs_dump_rate(mvm, rate, "INITIAL"); 2601 } 2602 2603 /* Save info about RSSI of last Rx */ 2604 void rs_update_last_rssi(struct iwl_mvm *mvm, 2605 struct iwl_mvm_sta *mvmsta, 2606 struct ieee80211_rx_status *rx_status) 2607 { 2608 struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv; 2609 int i; 2610 2611 lq_sta->pers.chains = rx_status->chains; 2612 lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0]; 2613 lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1]; 2614 lq_sta->pers.last_rssi = S8_MIN; 2615 2616 for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) { 2617 if (!(lq_sta->pers.chains & BIT(i))) 2618 continue; 2619 2620 if (lq_sta->pers.chain_signal[i] > lq_sta->pers.last_rssi) 2621 lq_sta->pers.last_rssi = lq_sta->pers.chain_signal[i]; 2622 } 2623 } 2624 2625 /* 2626 * rs_initialize_lq - Initialize a station's hardware rate table 2627 * 2628 * The uCode's station table contains a table of fallback rates 2629 * for automatic fallback during transmission. 2630 * 2631 * NOTE: This sets up a default set of values. These will be replaced later 2632 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of 2633 * rc80211_simple. 2634 * 2635 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before 2636 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD, 2637 * which requires station table entry to exist). 2638 */ 2639 static void rs_initialize_lq(struct iwl_mvm *mvm, 2640 struct ieee80211_sta *sta, 2641 struct iwl_lq_sta *lq_sta, 2642 enum nl80211_band band) 2643 { 2644 struct iwl_scale_tbl_info *tbl; 2645 struct rs_rate *rate; 2646 u8 active_tbl = 0; 2647 2648 if (!sta || !lq_sta) 2649 return; 2650 2651 if (!lq_sta->search_better_tbl) 2652 active_tbl = lq_sta->active_tbl; 2653 else 2654 active_tbl = rs_search_tbl(lq_sta->active_tbl); 2655 2656 tbl = &(lq_sta->lq_info[active_tbl]); 2657 rate = &tbl->rate; 2658 2659 rs_get_initial_rate(mvm, sta, lq_sta, band, rate); 2660 rs_init_optimal_rate(mvm, sta, lq_sta); 2661 2662 WARN_ONCE(rate->ant != ANT_A && rate->ant != ANT_B, 2663 "ant: 0x%x, chains 0x%x, fw tx ant: 0x%x, nvm tx ant: 0x%x\n", 2664 rate->ant, lq_sta->pers.chains, mvm->fw->valid_tx_ant, 2665 mvm->nvm_data ? mvm->nvm_data->valid_tx_ant : ANT_INVALID); 2666 2667 tbl->column = rs_get_column_from_rate(rate); 2668 2669 rs_set_expected_tpt_table(lq_sta, tbl); 2670 rs_fill_lq_cmd(mvm, sta, lq_sta, rate); 2671 /* TODO restore station should remember the lq cmd */ 2672 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq); 2673 } 2674 2675 static void rs_drv_get_rate(void *mvm_r, struct ieee80211_sta *sta, 2676 void *mvm_sta, 2677 struct ieee80211_tx_rate_control *txrc) 2678 { 2679 struct iwl_op_mode *op_mode = mvm_r; 2680 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode); 2681 struct sk_buff *skb = txrc->skb; 2682 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2683 struct iwl_lq_sta *lq_sta; 2684 struct rs_rate *optimal_rate; 2685 u32 last_ucode_rate; 2686 2687 if (sta && !iwl_mvm_sta_from_mac80211(sta)->vif) { 2688 /* if vif isn't initialized mvm doesn't know about 2689 * this station, so don't do anything with the it 2690 */ 2691 mvm_sta = NULL; 2692 } 2693 2694 if (!mvm_sta) 2695 return; 2696 2697 lq_sta = mvm_sta; 2698 2699 spin_lock_bh(&lq_sta->pers.lock); 2700 iwl_mvm_hwrate_to_tx_rate_v1(lq_sta->last_rate_n_flags, 2701 info->band, &info->control.rates[0]); 2702 info->control.rates[0].count = 1; 2703 2704 /* Report the optimal rate based on rssi and STA caps if we haven't 2705 * converged yet (too little traffic) or exploring other modulations 2706 */ 2707 if (lq_sta->rs_state != RS_STATE_STAY_IN_COLUMN) { 2708 optimal_rate = rs_get_optimal_rate(mvm, lq_sta); 2709 last_ucode_rate = ucode_rate_from_rs_rate(mvm, 2710 optimal_rate); 2711 iwl_mvm_hwrate_to_tx_rate_v1(last_ucode_rate, info->band, 2712 &txrc->reported_rate); 2713 txrc->reported_rate.count = 1; 2714 } 2715 spin_unlock_bh(&lq_sta->pers.lock); 2716 } 2717 2718 static void *rs_drv_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta, 2719 gfp_t gfp) 2720 { 2721 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 2722 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate; 2723 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode); 2724 struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv; 2725 2726 IWL_DEBUG_RATE(mvm, "create station rate scale window\n"); 2727 2728 lq_sta->pers.drv = mvm; 2729 #ifdef CONFIG_MAC80211_DEBUGFS 2730 lq_sta->pers.dbg_fixed_rate = 0; 2731 lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID; 2732 lq_sta->pers.ss_force = RS_SS_FORCE_NONE; 2733 #endif 2734 lq_sta->pers.chains = 0; 2735 memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal)); 2736 lq_sta->pers.last_rssi = S8_MIN; 2737 2738 return lq_sta; 2739 } 2740 2741 static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap, 2742 int nss) 2743 { 2744 u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) & 2745 (0x3 << (2 * (nss - 1))); 2746 rx_mcs >>= (2 * (nss - 1)); 2747 2748 if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7) 2749 return IWL_RATE_MCS_7_INDEX; 2750 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8) 2751 return IWL_RATE_MCS_8_INDEX; 2752 else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9) 2753 return IWL_RATE_MCS_9_INDEX; 2754 2755 WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED); 2756 return -1; 2757 } 2758 2759 static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta, 2760 struct ieee80211_sta_vht_cap *vht_cap, 2761 struct iwl_lq_sta *lq_sta) 2762 { 2763 int i; 2764 int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1); 2765 2766 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) { 2767 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) { 2768 if (i == IWL_RATE_9M_INDEX) 2769 continue; 2770 2771 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */ 2772 if (i == IWL_RATE_MCS_9_INDEX && 2773 sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) 2774 continue; 2775 2776 lq_sta->active_siso_rate |= BIT(i); 2777 } 2778 } 2779 2780 if (sta->deflink.rx_nss < 2) 2781 return; 2782 2783 highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2); 2784 if (highest_mcs >= IWL_RATE_MCS_0_INDEX) { 2785 for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) { 2786 if (i == IWL_RATE_9M_INDEX) 2787 continue; 2788 2789 /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */ 2790 if (i == IWL_RATE_MCS_9_INDEX && 2791 sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) 2792 continue; 2793 2794 lq_sta->active_mimo2_rate |= BIT(i); 2795 } 2796 } 2797 } 2798 2799 static void rs_ht_init(struct iwl_mvm *mvm, 2800 struct ieee80211_sta *sta, 2801 struct iwl_lq_sta *lq_sta, 2802 struct ieee80211_sta_ht_cap *ht_cap) 2803 { 2804 /* active_siso_rate mask includes 9 MBits (bit 5), 2805 * and CCK (bits 0-3), supp_rates[] does not; 2806 * shift to convert format, force 9 MBits off. 2807 */ 2808 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1; 2809 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1; 2810 lq_sta->active_siso_rate &= ~((u16)0x2); 2811 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE; 2812 2813 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1; 2814 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1; 2815 lq_sta->active_mimo2_rate &= ~((u16)0x2); 2816 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE; 2817 2818 if (mvm->cfg->ht_params->ldpc && 2819 (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)) 2820 lq_sta->ldpc = true; 2821 2822 if (mvm->cfg->ht_params->stbc && 2823 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) && 2824 (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC)) 2825 lq_sta->stbc_capable = true; 2826 2827 lq_sta->is_vht = false; 2828 } 2829 2830 static void rs_vht_init(struct iwl_mvm *mvm, 2831 struct ieee80211_sta *sta, 2832 struct iwl_lq_sta *lq_sta, 2833 struct ieee80211_sta_vht_cap *vht_cap) 2834 { 2835 rs_vht_set_enabled_rates(sta, vht_cap, lq_sta); 2836 2837 if (mvm->cfg->ht_params->ldpc && 2838 (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC)) 2839 lq_sta->ldpc = true; 2840 2841 if (mvm->cfg->ht_params->stbc && 2842 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) && 2843 (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK)) 2844 lq_sta->stbc_capable = true; 2845 2846 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_BEAMFORMER) && 2847 (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) && 2848 (vht_cap->cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) 2849 lq_sta->bfer_capable = true; 2850 2851 lq_sta->is_vht = true; 2852 } 2853 2854 #ifdef CONFIG_IWLWIFI_DEBUGFS 2855 void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm) 2856 { 2857 spin_lock_bh(&mvm->drv_stats_lock); 2858 memset(&mvm->drv_rx_stats, 0, sizeof(mvm->drv_rx_stats)); 2859 spin_unlock_bh(&mvm->drv_stats_lock); 2860 } 2861 2862 void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm, u32 rate, bool agg) 2863 { 2864 u8 nss = 0; 2865 2866 spin_lock(&mvm->drv_stats_lock); 2867 2868 if (agg) 2869 mvm->drv_rx_stats.agg_frames++; 2870 2871 mvm->drv_rx_stats.success_frames++; 2872 2873 switch (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) { 2874 case RATE_MCS_CHAN_WIDTH_20: 2875 mvm->drv_rx_stats.bw_20_frames++; 2876 break; 2877 case RATE_MCS_CHAN_WIDTH_40: 2878 mvm->drv_rx_stats.bw_40_frames++; 2879 break; 2880 case RATE_MCS_CHAN_WIDTH_80: 2881 mvm->drv_rx_stats.bw_80_frames++; 2882 break; 2883 case RATE_MCS_CHAN_WIDTH_160: 2884 mvm->drv_rx_stats.bw_160_frames++; 2885 break; 2886 default: 2887 WARN_ONCE(1, "bad BW. rate 0x%x", rate); 2888 } 2889 2890 if (rate & RATE_MCS_HT_MSK_V1) { 2891 mvm->drv_rx_stats.ht_frames++; 2892 nss = ((rate & RATE_HT_MCS_NSS_MSK_V1) >> RATE_HT_MCS_NSS_POS_V1) + 1; 2893 } else if (rate & RATE_MCS_VHT_MSK_V1) { 2894 mvm->drv_rx_stats.vht_frames++; 2895 nss = FIELD_GET(RATE_MCS_NSS_MSK, rate) + 1; 2896 } else { 2897 mvm->drv_rx_stats.legacy_frames++; 2898 } 2899 2900 if (nss == 1) 2901 mvm->drv_rx_stats.siso_frames++; 2902 else if (nss == 2) 2903 mvm->drv_rx_stats.mimo2_frames++; 2904 2905 if (rate & RATE_MCS_SGI_MSK_V1) 2906 mvm->drv_rx_stats.sgi_frames++; 2907 else 2908 mvm->drv_rx_stats.ngi_frames++; 2909 2910 mvm->drv_rx_stats.last_rates[mvm->drv_rx_stats.last_frame_idx] = rate; 2911 mvm->drv_rx_stats.last_frame_idx = 2912 (mvm->drv_rx_stats.last_frame_idx + 1) % 2913 ARRAY_SIZE(mvm->drv_rx_stats.last_rates); 2914 2915 spin_unlock(&mvm->drv_stats_lock); 2916 } 2917 #endif 2918 2919 /* 2920 * Called after adding a new station to initialize rate scaling 2921 */ 2922 static void rs_drv_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 2923 enum nl80211_band band) 2924 { 2925 int i, j; 2926 struct ieee80211_hw *hw = mvm->hw; 2927 struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap; 2928 struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap; 2929 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 2930 struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv; 2931 struct ieee80211_supported_band *sband; 2932 unsigned long supp; /* must be unsigned long for for_each_set_bit */ 2933 2934 lockdep_assert_held(&mvmsta->deflink.lq_sta.rs_drv.pers.lock); 2935 2936 /* clear all non-persistent lq data */ 2937 memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers)); 2938 2939 sband = hw->wiphy->bands[band]; 2940 2941 lq_sta->lq.sta_id = mvmsta->deflink.sta_id; 2942 mvmsta->amsdu_enabled = 0; 2943 mvmsta->max_amsdu_len = sta->cur->max_amsdu_len; 2944 2945 for (j = 0; j < LQ_SIZE; j++) 2946 rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]); 2947 2948 lq_sta->flush_timer = 0; 2949 lq_sta->last_tx = jiffies; 2950 2951 IWL_DEBUG_RATE(mvm, 2952 "LQ: *** rate scale station global init for station %d ***\n", 2953 mvmsta->deflink.sta_id); 2954 /* TODO: what is a good starting rate for STA? About middle? Maybe not 2955 * the lowest or the highest rate.. Could consider using RSSI from 2956 * previous packets? Need to have IEEE 802.1X auth succeed immediately 2957 * after assoc.. */ 2958 2959 lq_sta->missed_rate_counter = IWL_MVM_RS_MISSED_RATE_MAX; 2960 lq_sta->band = sband->band; 2961 /* 2962 * active legacy rates as per supported rates bitmap 2963 */ 2964 supp = sta->deflink.supp_rates[sband->band]; 2965 lq_sta->active_legacy_rate = 0; 2966 for_each_set_bit(i, &supp, BITS_PER_LONG) 2967 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value); 2968 2969 /* TODO: should probably account for rx_highest for both HT/VHT */ 2970 if (!vht_cap || !vht_cap->vht_supported) 2971 rs_ht_init(mvm, sta, lq_sta, ht_cap); 2972 else 2973 rs_vht_init(mvm, sta, lq_sta, vht_cap); 2974 2975 lq_sta->max_legacy_rate_idx = 2976 rs_get_max_rate_from_mask(lq_sta->active_legacy_rate); 2977 lq_sta->max_siso_rate_idx = 2978 rs_get_max_rate_from_mask(lq_sta->active_siso_rate); 2979 lq_sta->max_mimo2_rate_idx = 2980 rs_get_max_rate_from_mask(lq_sta->active_mimo2_rate); 2981 2982 IWL_DEBUG_RATE(mvm, 2983 "LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC=%d BFER=%d\n", 2984 lq_sta->active_legacy_rate, 2985 lq_sta->active_siso_rate, 2986 lq_sta->active_mimo2_rate, 2987 lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc_capable, 2988 lq_sta->bfer_capable); 2989 IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n", 2990 lq_sta->max_legacy_rate_idx, 2991 lq_sta->max_siso_rate_idx, 2992 lq_sta->max_mimo2_rate_idx); 2993 2994 /* These values will be overridden later */ 2995 lq_sta->lq.single_stream_ant_msk = 2996 iwl_mvm_bt_coex_get_single_ant_msk(mvm, iwl_mvm_get_valid_tx_ant(mvm)); 2997 lq_sta->lq.dual_stream_ant_msk = ANT_AB; 2998 2999 /* as default allow aggregation for all tids */ 3000 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID; 3001 lq_sta->is_agg = 0; 3002 #ifdef CONFIG_IWLWIFI_DEBUGFS 3003 iwl_mvm_reset_frame_stats(mvm); 3004 #endif 3005 rs_initialize_lq(mvm, sta, lq_sta, band); 3006 } 3007 3008 static void rs_drv_rate_update(void *mvm_r, 3009 struct ieee80211_supported_band *sband, 3010 struct cfg80211_chan_def *chandef, 3011 struct ieee80211_sta *sta, 3012 void *priv_sta, u32 changed) 3013 { 3014 struct iwl_op_mode *op_mode = mvm_r; 3015 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3016 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode); 3017 u8 tid; 3018 3019 if (!mvmsta->vif) 3020 return; 3021 3022 /* Stop any ongoing aggregations as rs starts off assuming no agg */ 3023 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) 3024 ieee80211_stop_tx_ba_session(sta, tid); 3025 3026 iwl_mvm_rs_rate_init(mvm, mvmsta->vif, sta, 3027 &mvmsta->vif->bss_conf, &sta->deflink, 3028 sband->band); 3029 } 3030 3031 static void __iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, 3032 struct ieee80211_sta *sta, 3033 int tid, struct ieee80211_tx_info *info, 3034 bool ndp) 3035 { 3036 int legacy_success; 3037 int retries; 3038 int i; 3039 struct iwl_lq_cmd *table; 3040 u32 lq_hwrate; 3041 struct rs_rate lq_rate, tx_resp_rate; 3042 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl; 3043 u32 tlc_info = (uintptr_t)info->status.status_driver_data[0]; 3044 u8 reduced_txp = tlc_info & RS_DRV_DATA_TXP_MSK; 3045 u8 lq_color = RS_DRV_DATA_LQ_COLOR_GET(tlc_info); 3046 u32 tx_resp_hwrate = (uintptr_t)info->status.status_driver_data[1]; 3047 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3048 struct iwl_lq_sta *lq_sta = &mvmsta->deflink.lq_sta.rs_drv; 3049 3050 if (!lq_sta->pers.drv) { 3051 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n"); 3052 return; 3053 } 3054 3055 /* This packet was aggregated but doesn't carry status info */ 3056 if ((info->flags & IEEE80211_TX_CTL_AMPDU) && 3057 !(info->flags & IEEE80211_TX_STAT_AMPDU)) 3058 return; 3059 3060 if (rs_rate_from_ucode_rate(tx_resp_hwrate, info->band, 3061 &tx_resp_rate)) { 3062 WARN_ON_ONCE(1); 3063 return; 3064 } 3065 3066 #ifdef CONFIG_MAC80211_DEBUGFS 3067 /* Disable last tx check if we are debugging with fixed rate but 3068 * update tx stats 3069 */ 3070 if (lq_sta->pers.dbg_fixed_rate) { 3071 int index = tx_resp_rate.index; 3072 enum rs_column column; 3073 int attempts, success; 3074 3075 column = rs_get_column_from_rate(&tx_resp_rate); 3076 if (WARN_ONCE(column == RS_COLUMN_INVALID, 3077 "Can't map rate 0x%x to column", 3078 tx_resp_hwrate)) 3079 return; 3080 3081 if (info->flags & IEEE80211_TX_STAT_AMPDU) { 3082 attempts = info->status.ampdu_len; 3083 success = info->status.ampdu_ack_len; 3084 } else { 3085 attempts = info->status.rates[0].count; 3086 success = !!(info->flags & IEEE80211_TX_STAT_ACK); 3087 } 3088 3089 lq_sta->pers.tx_stats[column][index].total += attempts; 3090 lq_sta->pers.tx_stats[column][index].success += success; 3091 3092 IWL_DEBUG_RATE(mvm, "Fixed rate 0x%x success %d attempts %d\n", 3093 tx_resp_hwrate, success, attempts); 3094 return; 3095 } 3096 #endif 3097 3098 if (time_after(jiffies, 3099 (unsigned long)(lq_sta->last_tx + 3100 (IWL_MVM_RS_IDLE_TIMEOUT * HZ)))) { 3101 IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n"); 3102 /* reach here only in case of driver RS, call directly 3103 * the unlocked version 3104 */ 3105 rs_drv_rate_init(mvm, sta, info->band); 3106 return; 3107 } 3108 lq_sta->last_tx = jiffies; 3109 3110 /* Ignore this Tx frame response if its initial rate doesn't match 3111 * that of latest Link Quality command. There may be stragglers 3112 * from a previous Link Quality command, but we're no longer interested 3113 * in those; they're either from the "active" mode while we're trying 3114 * to check "search" mode, or a prior "search" mode after we've moved 3115 * to a new "search" mode (which might become the new "active" mode). 3116 */ 3117 table = &lq_sta->lq; 3118 lq_hwrate = le32_to_cpu(table->rs_table[0]); 3119 if (rs_rate_from_ucode_rate(lq_hwrate, info->band, &lq_rate)) { 3120 WARN_ON_ONCE(1); 3121 return; 3122 } 3123 3124 /* Here we actually compare this rate to the latest LQ command */ 3125 if (lq_color != LQ_FLAG_COLOR_GET(table->flags)) { 3126 IWL_DEBUG_RATE(mvm, 3127 "tx resp color 0x%x does not match 0x%x\n", 3128 lq_color, LQ_FLAG_COLOR_GET(table->flags)); 3129 3130 /* Since rates mis-match, the last LQ command may have failed. 3131 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with 3132 * ... driver. 3133 */ 3134 lq_sta->missed_rate_counter++; 3135 if (lq_sta->missed_rate_counter > IWL_MVM_RS_MISSED_RATE_MAX) { 3136 lq_sta->missed_rate_counter = 0; 3137 IWL_DEBUG_RATE(mvm, 3138 "Too many rates mismatch. Send sync LQ. rs_state %d\n", 3139 lq_sta->rs_state); 3140 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq); 3141 } 3142 /* Regardless, ignore this status info for outdated rate */ 3143 return; 3144 } 3145 3146 /* Rate did match, so reset the missed_rate_counter */ 3147 lq_sta->missed_rate_counter = 0; 3148 3149 if (!lq_sta->search_better_tbl) { 3150 curr_tbl = &lq_sta->lq_info[lq_sta->active_tbl]; 3151 other_tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)]; 3152 } else { 3153 curr_tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)]; 3154 other_tbl = &lq_sta->lq_info[lq_sta->active_tbl]; 3155 } 3156 3157 if (WARN_ON_ONCE(!rs_rate_column_match(&lq_rate, &curr_tbl->rate))) { 3158 IWL_DEBUG_RATE(mvm, 3159 "Neither active nor search matches tx rate\n"); 3160 tmp_tbl = &lq_sta->lq_info[lq_sta->active_tbl]; 3161 rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE"); 3162 tmp_tbl = &lq_sta->lq_info[rs_search_tbl(lq_sta->active_tbl)]; 3163 rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH"); 3164 rs_dump_rate(mvm, &lq_rate, "ACTUAL"); 3165 3166 /* no matching table found, let's by-pass the data collection 3167 * and continue to perform rate scale to find the rate table 3168 */ 3169 rs_stay_in_table(lq_sta, true); 3170 goto done; 3171 } 3172 3173 /* Updating the frame history depends on whether packets were 3174 * aggregated. 3175 * 3176 * For aggregation, all packets were transmitted at the same rate, the 3177 * first index into rate scale table. 3178 */ 3179 if (info->flags & IEEE80211_TX_STAT_AMPDU) { 3180 rs_collect_tpc_data(mvm, lq_sta, curr_tbl, tx_resp_rate.index, 3181 info->status.ampdu_len, 3182 info->status.ampdu_ack_len, 3183 reduced_txp); 3184 3185 /* ampdu_ack_len = 0 marks no BA was received. For TLC, treat 3186 * it as a single frame loss as we don't want the success ratio 3187 * to dip too quickly because a BA wasn't received. 3188 * For TPC, there's no need for this optimisation since we want 3189 * to recover very quickly from a bad power reduction and, 3190 * therefore we'd like the success ratio to get an immediate hit 3191 * when failing to get a BA, so we'd switch back to a lower or 3192 * zero power reduction. When FW transmits agg with a rate 3193 * different from the initial rate, it will not use reduced txp 3194 * and will send BA notification twice (one empty with reduced 3195 * txp equal to the value from LQ and one with reduced txp 0). 3196 * We need to update counters for each txp level accordingly. 3197 */ 3198 if (info->status.ampdu_ack_len == 0) 3199 info->status.ampdu_len = 1; 3200 3201 rs_collect_tlc_data(mvm, mvmsta, tid, curr_tbl, 3202 tx_resp_rate.index, 3203 info->status.ampdu_len, 3204 info->status.ampdu_ack_len); 3205 3206 /* Update success/fail counts if not searching for new mode */ 3207 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) { 3208 lq_sta->total_success += info->status.ampdu_ack_len; 3209 lq_sta->total_failed += (info->status.ampdu_len - 3210 info->status.ampdu_ack_len); 3211 } 3212 } else { 3213 /* For legacy, update frame history with for each Tx retry. */ 3214 retries = info->status.rates[0].count - 1; 3215 /* HW doesn't send more than 15 retries */ 3216 retries = min(retries, 15); 3217 3218 /* The last transmission may have been successful */ 3219 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK); 3220 /* Collect data for each rate used during failed TX attempts */ 3221 for (i = 0; i <= retries; ++i) { 3222 lq_hwrate = le32_to_cpu(table->rs_table[i]); 3223 if (rs_rate_from_ucode_rate(lq_hwrate, info->band, 3224 &lq_rate)) { 3225 WARN_ON_ONCE(1); 3226 return; 3227 } 3228 3229 /* Only collect stats if retried rate is in the same RS 3230 * table as active/search. 3231 */ 3232 if (rs_rate_column_match(&lq_rate, &curr_tbl->rate)) 3233 tmp_tbl = curr_tbl; 3234 else if (rs_rate_column_match(&lq_rate, 3235 &other_tbl->rate)) 3236 tmp_tbl = other_tbl; 3237 else 3238 continue; 3239 3240 rs_collect_tpc_data(mvm, lq_sta, tmp_tbl, 3241 tx_resp_rate.index, 1, 3242 i < retries ? 0 : legacy_success, 3243 reduced_txp); 3244 rs_collect_tlc_data(mvm, mvmsta, tid, tmp_tbl, 3245 tx_resp_rate.index, 1, 3246 i < retries ? 0 : legacy_success); 3247 } 3248 3249 /* Update success/fail counts if not searching for new mode */ 3250 if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) { 3251 lq_sta->total_success += legacy_success; 3252 lq_sta->total_failed += retries + (1 - legacy_success); 3253 } 3254 } 3255 /* The last TX rate is cached in lq_sta; it's set in if/else above */ 3256 lq_sta->last_rate_n_flags = lq_hwrate; 3257 IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp); 3258 done: 3259 /* See if there's a better rate or modulation mode to try. */ 3260 if (sta->deflink.supp_rates[info->band]) 3261 rs_rate_scale_perform(mvm, sta, lq_sta, tid, ndp); 3262 } 3263 3264 void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 3265 int tid, struct ieee80211_tx_info *info, bool ndp) 3266 { 3267 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3268 3269 /* If it's locked we are in middle of init flow 3270 * just wait for next tx status to update the lq_sta data 3271 */ 3272 if (!spin_trylock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock)) 3273 return; 3274 3275 __iwl_mvm_rs_tx_status(mvm, sta, tid, info, ndp); 3276 spin_unlock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock); 3277 } 3278 3279 #ifdef CONFIG_MAC80211_DEBUGFS 3280 static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm, 3281 struct iwl_lq_cmd *lq_cmd, 3282 enum nl80211_band band, 3283 u32 ucode_rate) 3284 { 3285 struct rs_rate rate; 3286 int i; 3287 int num_rates = ARRAY_SIZE(lq_cmd->rs_table); 3288 __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate); 3289 u8 ant = (ucode_rate & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS; 3290 3291 for (i = 0; i < num_rates; i++) 3292 lq_cmd->rs_table[i] = ucode_rate_le32; 3293 3294 if (rs_rate_from_ucode_rate(ucode_rate, band, &rate)) { 3295 WARN_ON_ONCE(1); 3296 return; 3297 } 3298 3299 if (is_mimo(&rate)) 3300 lq_cmd->mimo_delim = num_rates - 1; 3301 else 3302 lq_cmd->mimo_delim = 0; 3303 3304 lq_cmd->reduced_tpc = 0; 3305 3306 if (num_of_ant(ant) == 1) 3307 lq_cmd->single_stream_ant_msk = ant; 3308 3309 if (!mvm->trans->trans_cfg->gen2) 3310 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF; 3311 else 3312 lq_cmd->agg_frame_cnt_limit = 3313 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF; 3314 } 3315 #endif /* CONFIG_MAC80211_DEBUGFS */ 3316 3317 static void rs_fill_rates_for_column(struct iwl_mvm *mvm, 3318 struct iwl_lq_sta *lq_sta, 3319 struct rs_rate *rate, 3320 __le32 *rs_table, int *rs_table_index, 3321 int num_rates, int num_retries, 3322 u8 valid_tx_ant, bool toggle_ant) 3323 { 3324 int i, j; 3325 __le32 ucode_rate; 3326 bool bottom_reached = false; 3327 int prev_rate_idx = rate->index; 3328 int end = LINK_QUAL_MAX_RETRY_NUM; 3329 int index = *rs_table_index; 3330 3331 for (i = 0; i < num_rates && index < end; i++) { 3332 for (j = 0; j < num_retries && index < end; j++, index++) { 3333 ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, 3334 rate)); 3335 rs_table[index] = ucode_rate; 3336 if (toggle_ant) 3337 rs_toggle_antenna(valid_tx_ant, rate); 3338 } 3339 3340 prev_rate_idx = rate->index; 3341 bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate); 3342 if (bottom_reached && !is_legacy(rate)) 3343 break; 3344 } 3345 3346 if (!bottom_reached && !is_legacy(rate)) 3347 rate->index = prev_rate_idx; 3348 3349 *rs_table_index = index; 3350 } 3351 3352 /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI 3353 * column the rate table should look like this: 3354 * 3355 * rate[0] 0x400F019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI 3356 * rate[1] 0x400F019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI 3357 * rate[2] 0x400F018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI 3358 * rate[3] 0x400F018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI 3359 * rate[4] 0x400F017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI 3360 * rate[5] 0x400F017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI 3361 * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI 3362 * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI 3363 * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI 3364 * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps 3365 * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps 3366 * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps 3367 * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps 3368 * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps 3369 * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps 3370 * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps 3371 */ 3372 static void rs_build_rates_table(struct iwl_mvm *mvm, 3373 struct ieee80211_sta *sta, 3374 struct iwl_lq_sta *lq_sta, 3375 const struct rs_rate *initial_rate) 3376 { 3377 struct rs_rate rate; 3378 int num_rates, num_retries, index = 0; 3379 u8 valid_tx_ant = 0; 3380 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq; 3381 bool toggle_ant = false; 3382 u32 color; 3383 3384 memcpy(&rate, initial_rate, sizeof(rate)); 3385 3386 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm); 3387 3388 /* TODO: remove old API when min FW API hits 14 */ 3389 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS) && 3390 rs_stbc_allow(mvm, sta, lq_sta)) 3391 rate.stbc = true; 3392 3393 if (is_siso(&rate)) { 3394 num_rates = IWL_MVM_RS_INITIAL_SISO_NUM_RATES; 3395 num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE; 3396 } else if (is_mimo(&rate)) { 3397 num_rates = IWL_MVM_RS_INITIAL_MIMO_NUM_RATES; 3398 num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE; 3399 } else { 3400 num_rates = IWL_MVM_RS_INITIAL_LEGACY_NUM_RATES; 3401 num_retries = IWL_MVM_RS_INITIAL_LEGACY_RETRIES; 3402 toggle_ant = true; 3403 } 3404 3405 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index, 3406 num_rates, num_retries, valid_tx_ant, 3407 toggle_ant); 3408 3409 rs_get_lower_rate_down_column(lq_sta, &rate); 3410 3411 if (is_siso(&rate)) { 3412 num_rates = IWL_MVM_RS_SECONDARY_SISO_NUM_RATES; 3413 num_retries = IWL_MVM_RS_SECONDARY_SISO_RETRIES; 3414 lq_cmd->mimo_delim = index; 3415 } else if (is_legacy(&rate)) { 3416 num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES; 3417 num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES; 3418 } else { 3419 WARN_ON_ONCE(1); 3420 } 3421 3422 toggle_ant = true; 3423 3424 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index, 3425 num_rates, num_retries, valid_tx_ant, 3426 toggle_ant); 3427 3428 rs_get_lower_rate_down_column(lq_sta, &rate); 3429 3430 num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES; 3431 num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES; 3432 3433 rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index, 3434 num_rates, num_retries, valid_tx_ant, 3435 toggle_ant); 3436 3437 /* update the color of the LQ command (as a counter at bits 1-3) */ 3438 color = LQ_FLAGS_COLOR_INC(LQ_FLAG_COLOR_GET(lq_cmd->flags)); 3439 lq_cmd->flags = LQ_FLAG_COLOR_SET(lq_cmd->flags, color); 3440 } 3441 3442 struct rs_bfer_active_iter_data { 3443 struct ieee80211_sta *exclude_sta; 3444 struct iwl_mvm_sta *bfer_mvmsta; 3445 }; 3446 3447 static void rs_bfer_active_iter(void *_data, 3448 struct ieee80211_sta *sta) 3449 { 3450 struct rs_bfer_active_iter_data *data = _data; 3451 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3452 struct iwl_lq_cmd *lq_cmd = &mvmsta->deflink.lq_sta.rs_drv.lq; 3453 u32 ss_params = le32_to_cpu(lq_cmd->ss_params); 3454 3455 if (sta == data->exclude_sta) 3456 return; 3457 3458 /* The current sta has BFER allowed */ 3459 if (ss_params & LQ_SS_BFER_ALLOWED) { 3460 WARN_ON_ONCE(data->bfer_mvmsta != NULL); 3461 3462 data->bfer_mvmsta = mvmsta; 3463 } 3464 } 3465 3466 static int rs_bfer_priority(struct iwl_mvm_sta *sta) 3467 { 3468 int prio = -1; 3469 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(sta->vif); 3470 3471 switch (viftype) { 3472 case NL80211_IFTYPE_AP: 3473 case NL80211_IFTYPE_P2P_GO: 3474 prio = 3; 3475 break; 3476 case NL80211_IFTYPE_P2P_CLIENT: 3477 prio = 2; 3478 break; 3479 case NL80211_IFTYPE_STATION: 3480 prio = 1; 3481 break; 3482 default: 3483 WARN_ONCE(true, "viftype %d sta_id %d", viftype, 3484 sta->deflink.sta_id); 3485 prio = -1; 3486 } 3487 3488 return prio; 3489 } 3490 3491 /* Returns >0 if sta1 has a higher BFER priority compared to sta2 */ 3492 static int rs_bfer_priority_cmp(struct iwl_mvm_sta *sta1, 3493 struct iwl_mvm_sta *sta2) 3494 { 3495 int prio1 = rs_bfer_priority(sta1); 3496 int prio2 = rs_bfer_priority(sta2); 3497 3498 if (prio1 > prio2) 3499 return 1; 3500 if (prio1 < prio2) 3501 return -1; 3502 return 0; 3503 } 3504 3505 static void rs_set_lq_ss_params(struct iwl_mvm *mvm, 3506 struct ieee80211_sta *sta, 3507 struct iwl_lq_sta *lq_sta, 3508 const struct rs_rate *initial_rate) 3509 { 3510 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq; 3511 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3512 struct rs_bfer_active_iter_data data = { 3513 .exclude_sta = sta, 3514 .bfer_mvmsta = NULL, 3515 }; 3516 struct iwl_mvm_sta *bfer_mvmsta = NULL; 3517 u32 ss_params = LQ_SS_PARAMS_VALID; 3518 3519 if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) 3520 goto out; 3521 3522 #ifdef CONFIG_MAC80211_DEBUGFS 3523 /* Check if forcing the decision is configured. 3524 * Note that SISO is forced by not allowing STBC or BFER 3525 */ 3526 if (lq_sta->pers.ss_force == RS_SS_FORCE_STBC) 3527 ss_params |= (LQ_SS_STBC_1SS_ALLOWED | LQ_SS_FORCE); 3528 else if (lq_sta->pers.ss_force == RS_SS_FORCE_BFER) 3529 ss_params |= (LQ_SS_BFER_ALLOWED | LQ_SS_FORCE); 3530 3531 if (lq_sta->pers.ss_force != RS_SS_FORCE_NONE) { 3532 IWL_DEBUG_RATE(mvm, "Forcing single stream Tx decision %d\n", 3533 lq_sta->pers.ss_force); 3534 goto out; 3535 } 3536 #endif 3537 3538 if (lq_sta->stbc_capable) 3539 ss_params |= LQ_SS_STBC_1SS_ALLOWED; 3540 3541 if (!lq_sta->bfer_capable) 3542 goto out; 3543 3544 ieee80211_iterate_stations_atomic(mvm->hw, 3545 rs_bfer_active_iter, 3546 &data); 3547 bfer_mvmsta = data.bfer_mvmsta; 3548 3549 /* This code is safe as it doesn't run concurrently for different 3550 * stations. This is guaranteed by the fact that calls to 3551 * ieee80211_tx_status wouldn't run concurrently for a single HW. 3552 */ 3553 if (!bfer_mvmsta) { 3554 IWL_DEBUG_RATE(mvm, "No sta with BFER allowed found. Allow\n"); 3555 3556 ss_params |= LQ_SS_BFER_ALLOWED; 3557 goto out; 3558 } 3559 3560 IWL_DEBUG_RATE(mvm, "Found existing sta %d with BFER activated\n", 3561 bfer_mvmsta->deflink.sta_id); 3562 3563 /* Disallow BFER on another STA if active and we're a higher priority */ 3564 if (rs_bfer_priority_cmp(mvmsta, bfer_mvmsta) > 0) { 3565 struct iwl_lq_cmd *bfersta_lq_cmd = 3566 &bfer_mvmsta->deflink.lq_sta.rs_drv.lq; 3567 u32 bfersta_ss_params = le32_to_cpu(bfersta_lq_cmd->ss_params); 3568 3569 bfersta_ss_params &= ~LQ_SS_BFER_ALLOWED; 3570 bfersta_lq_cmd->ss_params = cpu_to_le32(bfersta_ss_params); 3571 iwl_mvm_send_lq_cmd(mvm, bfersta_lq_cmd); 3572 3573 ss_params |= LQ_SS_BFER_ALLOWED; 3574 IWL_DEBUG_RATE(mvm, 3575 "Lower priority BFER sta found (%d). Switch BFER\n", 3576 bfer_mvmsta->deflink.sta_id); 3577 } 3578 out: 3579 lq_cmd->ss_params = cpu_to_le32(ss_params); 3580 } 3581 3582 static void rs_fill_lq_cmd(struct iwl_mvm *mvm, 3583 struct ieee80211_sta *sta, 3584 struct iwl_lq_sta *lq_sta, 3585 const struct rs_rate *initial_rate) 3586 { 3587 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq; 3588 struct iwl_mvm_sta *mvmsta; 3589 struct iwl_mvm_vif *mvmvif; 3590 3591 lq_cmd->agg_disable_start_th = IWL_MVM_RS_AGG_DISABLE_START; 3592 lq_cmd->agg_time_limit = 3593 cpu_to_le16(IWL_MVM_RS_AGG_TIME_LIMIT); 3594 3595 #ifdef CONFIG_MAC80211_DEBUGFS 3596 if (lq_sta->pers.dbg_fixed_rate) { 3597 rs_build_rates_table_from_fixed(mvm, lq_cmd, 3598 lq_sta->band, 3599 lq_sta->pers.dbg_fixed_rate); 3600 return; 3601 } 3602 #endif 3603 if (WARN_ON_ONCE(!sta || !initial_rate)) 3604 return; 3605 3606 rs_build_rates_table(mvm, sta, lq_sta, initial_rate); 3607 3608 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS)) 3609 rs_set_lq_ss_params(mvm, sta, lq_sta, initial_rate); 3610 3611 mvmsta = iwl_mvm_sta_from_mac80211(sta); 3612 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif); 3613 3614 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_COEX_SCHEMA_2) && 3615 num_of_ant(initial_rate->ant) == 1) 3616 lq_cmd->single_stream_ant_msk = initial_rate->ant; 3617 3618 lq_cmd->agg_frame_cnt_limit = lq_sta->pers.max_agg_bufsize; 3619 3620 /* 3621 * In case of low latency, tell the firmware to leave a frame in the 3622 * Tx Fifo so that it can start a transaction in the same TxOP. This 3623 * basically allows the firmware to send bursts. 3624 */ 3625 if (iwl_mvm_vif_low_latency(mvmvif)) 3626 lq_cmd->agg_frame_cnt_limit--; 3627 3628 if (mvmsta->vif->p2p) 3629 lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK; 3630 3631 lq_cmd->agg_time_limit = 3632 cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta)); 3633 } 3634 3635 static void *rs_alloc(struct ieee80211_hw *hw) 3636 { 3637 return hw->priv; 3638 } 3639 3640 /* rate scale requires free function to be implemented */ 3641 static void rs_free(void *mvm_rate) 3642 { 3643 return; 3644 } 3645 3646 static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta) 3647 { 3648 struct iwl_op_mode *op_mode __maybe_unused = mvm_r; 3649 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode); 3650 3651 IWL_DEBUG_RATE(mvm, "enter\n"); 3652 IWL_DEBUG_RATE(mvm, "leave\n"); 3653 } 3654 3655 int rs_pretty_print_rate_v1(char *buf, int bufsz, const u32 rate) 3656 { 3657 3658 char *type; 3659 u8 mcs = 0, nss = 0; 3660 u8 ant = (rate & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS; 3661 u32 bw = (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) >> 3662 RATE_MCS_CHAN_WIDTH_POS; 3663 3664 if (!(rate & RATE_MCS_HT_MSK_V1) && 3665 !(rate & RATE_MCS_VHT_MSK_V1) && 3666 !(rate & RATE_MCS_HE_MSK_V1)) { 3667 int index = iwl_hwrate_to_plcp_idx(rate); 3668 3669 return scnprintf(buf, bufsz, "Legacy | ANT: %s Rate: %s Mbps", 3670 iwl_rs_pretty_ant(ant), 3671 index == IWL_RATE_INVALID ? "BAD" : 3672 iwl_rate_mcs(index)->mbps); 3673 } 3674 3675 if (rate & RATE_MCS_VHT_MSK_V1) { 3676 type = "VHT"; 3677 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK; 3678 nss = FIELD_GET(RATE_MCS_NSS_MSK, rate) + 1; 3679 } else if (rate & RATE_MCS_HT_MSK_V1) { 3680 type = "HT"; 3681 mcs = rate & RATE_HT_MCS_INDEX_MSK_V1; 3682 nss = ((rate & RATE_HT_MCS_NSS_MSK_V1) 3683 >> RATE_HT_MCS_NSS_POS_V1) + 1; 3684 } else if (rate & RATE_MCS_HE_MSK_V1) { 3685 type = "HE"; 3686 mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK; 3687 nss = FIELD_GET(RATE_MCS_NSS_MSK, rate) + 1; 3688 } else { 3689 type = "Unknown"; /* shouldn't happen */ 3690 } 3691 3692 return scnprintf(buf, bufsz, 3693 "0x%x: %s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s", 3694 rate, type, iwl_rs_pretty_ant(ant), iwl_rs_pretty_bw(bw), mcs, nss, 3695 (rate & RATE_MCS_SGI_MSK_V1) ? "SGI " : "NGI ", 3696 (rate & RATE_MCS_STBC_MSK) ? "STBC " : "", 3697 (rate & RATE_MCS_LDPC_MSK_V1) ? "LDPC " : "", 3698 (rate & RATE_HE_DUAL_CARRIER_MODE_MSK) ? "DCM " : "", 3699 (rate & RATE_MCS_BF_MSK) ? "BF " : ""); 3700 } 3701 3702 #ifdef CONFIG_MAC80211_DEBUGFS 3703 /* 3704 * Program the device to use fixed rate for frame transmit 3705 * This is for debugging/testing only 3706 * once the device start use fixed rate, we need to reload the module 3707 * to being back the normal operation. 3708 */ 3709 static void rs_program_fix_rate(struct iwl_mvm *mvm, 3710 struct iwl_lq_sta *lq_sta) 3711 { 3712 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */ 3713 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */ 3714 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */ 3715 3716 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n", 3717 lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate); 3718 3719 if (lq_sta->pers.dbg_fixed_rate) { 3720 rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL); 3721 iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq); 3722 } 3723 } 3724 3725 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file, 3726 const char __user *user_buf, size_t count, loff_t *ppos) 3727 { 3728 struct iwl_lq_sta *lq_sta = file->private_data; 3729 struct iwl_mvm *mvm; 3730 char buf[64]; 3731 size_t buf_size; 3732 u32 parsed_rate; 3733 3734 mvm = lq_sta->pers.drv; 3735 memset(buf, 0, sizeof(buf)); 3736 buf_size = min(count, sizeof(buf) - 1); 3737 if (copy_from_user(buf, user_buf, buf_size)) 3738 return -EFAULT; 3739 3740 if (sscanf(buf, "%x", &parsed_rate) == 1) 3741 lq_sta->pers.dbg_fixed_rate = parsed_rate; 3742 else 3743 lq_sta->pers.dbg_fixed_rate = 0; 3744 3745 rs_program_fix_rate(mvm, lq_sta); 3746 3747 return count; 3748 } 3749 3750 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file, 3751 char __user *user_buf, size_t count, loff_t *ppos) 3752 { 3753 char *buff; 3754 int desc = 0; 3755 int i = 0; 3756 ssize_t ret; 3757 static const size_t bufsz = 2048; 3758 3759 struct iwl_lq_sta *lq_sta = file->private_data; 3760 struct iwl_mvm_sta *mvmsta = 3761 container_of(lq_sta, struct iwl_mvm_sta, deflink.lq_sta.rs_drv); 3762 struct iwl_mvm *mvm; 3763 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); 3764 struct rs_rate *rate = &tbl->rate; 3765 u32 ss_params; 3766 3767 mvm = lq_sta->pers.drv; 3768 buff = kmalloc(bufsz, GFP_KERNEL); 3769 if (!buff) 3770 return -ENOMEM; 3771 3772 desc += scnprintf(buff + desc, bufsz - desc, 3773 "sta_id %d\n", lq_sta->lq.sta_id); 3774 desc += scnprintf(buff + desc, bufsz - desc, 3775 "failed=%d success=%d rate=0%lX\n", 3776 lq_sta->total_failed, lq_sta->total_success, 3777 lq_sta->active_legacy_rate); 3778 desc += scnprintf(buff + desc, bufsz - desc, "fixed rate 0x%X\n", 3779 lq_sta->pers.dbg_fixed_rate); 3780 desc += scnprintf(buff + desc, bufsz - desc, "valid_tx_ant %s%s\n", 3781 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "", 3782 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : ""); 3783 desc += scnprintf(buff + desc, bufsz - desc, "lq type %s\n", 3784 (is_legacy(rate)) ? "legacy" : 3785 is_vht(rate) ? "VHT" : "HT"); 3786 if (!is_legacy(rate)) { 3787 desc += scnprintf(buff + desc, bufsz - desc, " %s", 3788 (is_siso(rate)) ? "SISO" : "MIMO2"); 3789 desc += scnprintf(buff + desc, bufsz - desc, " %s", 3790 (is_ht20(rate)) ? "20MHz" : 3791 (is_ht40(rate)) ? "40MHz" : 3792 (is_ht80(rate)) ? "80MHz" : 3793 (is_ht160(rate)) ? "160MHz" : "BAD BW"); 3794 desc += scnprintf(buff + desc, bufsz - desc, " %s %s %s %s\n", 3795 (rate->sgi) ? "SGI" : "NGI", 3796 (rate->ldpc) ? "LDPC" : "BCC", 3797 (lq_sta->is_agg) ? "AGG on" : "", 3798 (mvmsta->amsdu_enabled) ? "AMSDU on" : ""); 3799 } 3800 desc += scnprintf(buff + desc, bufsz - desc, "last tx rate=0x%X\n", 3801 lq_sta->last_rate_n_flags); 3802 desc += scnprintf(buff + desc, bufsz - desc, 3803 "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n", 3804 lq_sta->lq.flags, 3805 lq_sta->lq.mimo_delim, 3806 lq_sta->lq.single_stream_ant_msk, 3807 lq_sta->lq.dual_stream_ant_msk); 3808 3809 desc += scnprintf(buff + desc, bufsz - desc, 3810 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n", 3811 le16_to_cpu(lq_sta->lq.agg_time_limit), 3812 lq_sta->lq.agg_disable_start_th, 3813 lq_sta->lq.agg_frame_cnt_limit); 3814 3815 desc += scnprintf(buff + desc, bufsz - desc, "reduced tpc=%d\n", 3816 lq_sta->lq.reduced_tpc); 3817 ss_params = le32_to_cpu(lq_sta->lq.ss_params); 3818 desc += scnprintf(buff + desc, bufsz - desc, 3819 "single stream params: %s%s%s%s\n", 3820 (ss_params & LQ_SS_PARAMS_VALID) ? 3821 "VALID" : "INVALID", 3822 (ss_params & LQ_SS_BFER_ALLOWED) ? 3823 ", BFER" : "", 3824 (ss_params & LQ_SS_STBC_1SS_ALLOWED) ? 3825 ", STBC" : "", 3826 (ss_params & LQ_SS_FORCE) ? 3827 ", FORCE" : ""); 3828 desc += scnprintf(buff + desc, bufsz - desc, 3829 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n", 3830 lq_sta->lq.initial_rate_index[0], 3831 lq_sta->lq.initial_rate_index[1], 3832 lq_sta->lq.initial_rate_index[2], 3833 lq_sta->lq.initial_rate_index[3]); 3834 3835 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) { 3836 u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]); 3837 3838 desc += scnprintf(buff + desc, bufsz - desc, 3839 " rate[%d] 0x%X ", i, r); 3840 desc += rs_pretty_print_rate_v1(buff + desc, bufsz - desc, r); 3841 if (desc < bufsz - 1) 3842 buff[desc++] = '\n'; 3843 } 3844 3845 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc); 3846 kfree(buff); 3847 return ret; 3848 } 3849 3850 static const struct file_operations rs_sta_dbgfs_scale_table_ops = { 3851 .write = rs_sta_dbgfs_scale_table_write, 3852 .read = rs_sta_dbgfs_scale_table_read, 3853 .open = simple_open, 3854 .llseek = default_llseek, 3855 }; 3856 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file, 3857 char __user *user_buf, size_t count, loff_t *ppos) 3858 { 3859 char *buff; 3860 int desc = 0; 3861 int i, j; 3862 ssize_t ret; 3863 struct iwl_scale_tbl_info *tbl; 3864 struct rs_rate *rate; 3865 struct iwl_lq_sta *lq_sta = file->private_data; 3866 3867 buff = kmalloc(1024, GFP_KERNEL); 3868 if (!buff) 3869 return -ENOMEM; 3870 3871 for (i = 0; i < LQ_SIZE; i++) { 3872 tbl = &(lq_sta->lq_info[i]); 3873 rate = &tbl->rate; 3874 desc += sprintf(buff+desc, 3875 "%s type=%d SGI=%d BW=%s DUP=0\n" 3876 "index=%d\n", 3877 lq_sta->active_tbl == i ? "*" : "x", 3878 rate->type, 3879 rate->sgi, 3880 is_ht20(rate) ? "20MHz" : 3881 is_ht40(rate) ? "40MHz" : 3882 is_ht80(rate) ? "80MHz" : 3883 is_ht160(rate) ? "160MHz" : "ERR", 3884 rate->index); 3885 for (j = 0; j < IWL_RATE_COUNT; j++) { 3886 desc += sprintf(buff+desc, 3887 "counter=%d success=%d %%=%d\n", 3888 tbl->win[j].counter, 3889 tbl->win[j].success_counter, 3890 tbl->win[j].success_ratio); 3891 } 3892 } 3893 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc); 3894 kfree(buff); 3895 return ret; 3896 } 3897 3898 static const struct file_operations rs_sta_dbgfs_stats_table_ops = { 3899 .read = rs_sta_dbgfs_stats_table_read, 3900 .open = simple_open, 3901 .llseek = default_llseek, 3902 }; 3903 3904 static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file, 3905 char __user *user_buf, 3906 size_t count, loff_t *ppos) 3907 { 3908 static const char * const column_name[] = { 3909 [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A", 3910 [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B", 3911 [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A", 3912 [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B", 3913 [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI", 3914 [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI", 3915 [RS_COLUMN_MIMO2] = "MIMO2", 3916 [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI", 3917 }; 3918 3919 static const char * const rate_name[] = { 3920 [IWL_RATE_1M_INDEX] = "1M", 3921 [IWL_RATE_2M_INDEX] = "2M", 3922 [IWL_RATE_5M_INDEX] = "5.5M", 3923 [IWL_RATE_11M_INDEX] = "11M", 3924 [IWL_RATE_6M_INDEX] = "6M|MCS0", 3925 [IWL_RATE_9M_INDEX] = "9M", 3926 [IWL_RATE_12M_INDEX] = "12M|MCS1", 3927 [IWL_RATE_18M_INDEX] = "18M|MCS2", 3928 [IWL_RATE_24M_INDEX] = "24M|MCS3", 3929 [IWL_RATE_36M_INDEX] = "36M|MCS4", 3930 [IWL_RATE_48M_INDEX] = "48M|MCS5", 3931 [IWL_RATE_54M_INDEX] = "54M|MCS6", 3932 [IWL_RATE_MCS_7_INDEX] = "MCS7", 3933 [IWL_RATE_MCS_8_INDEX] = "MCS8", 3934 [IWL_RATE_MCS_9_INDEX] = "MCS9", 3935 [IWL_RATE_MCS_10_INDEX] = "MCS10", 3936 [IWL_RATE_MCS_11_INDEX] = "MCS11", 3937 }; 3938 3939 char *buff, *pos, *endpos; 3940 int col, rate; 3941 ssize_t ret; 3942 struct iwl_lq_sta *lq_sta = file->private_data; 3943 struct rs_rate_stats *stats; 3944 static const size_t bufsz = 1024; 3945 3946 buff = kmalloc(bufsz, GFP_KERNEL); 3947 if (!buff) 3948 return -ENOMEM; 3949 3950 pos = buff; 3951 endpos = pos + bufsz; 3952 3953 pos += scnprintf(pos, endpos - pos, "COLUMN,"); 3954 for (rate = 0; rate < IWL_RATE_COUNT; rate++) 3955 pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]); 3956 pos += scnprintf(pos, endpos - pos, "\n"); 3957 3958 for (col = 0; col < RS_COLUMN_COUNT; col++) { 3959 pos += scnprintf(pos, endpos - pos, 3960 "%s,", column_name[col]); 3961 3962 for (rate = 0; rate < IWL_RATE_COUNT; rate++) { 3963 stats = &(lq_sta->pers.tx_stats[col][rate]); 3964 pos += scnprintf(pos, endpos - pos, 3965 "%llu/%llu,", 3966 stats->success, 3967 stats->total); 3968 } 3969 pos += scnprintf(pos, endpos - pos, "\n"); 3970 } 3971 3972 ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff); 3973 kfree(buff); 3974 return ret; 3975 } 3976 3977 static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file, 3978 const char __user *user_buf, 3979 size_t count, loff_t *ppos) 3980 { 3981 struct iwl_lq_sta *lq_sta = file->private_data; 3982 memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats)); 3983 3984 return count; 3985 } 3986 3987 static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = { 3988 .read = rs_sta_dbgfs_drv_tx_stats_read, 3989 .write = rs_sta_dbgfs_drv_tx_stats_write, 3990 .open = simple_open, 3991 .llseek = default_llseek, 3992 }; 3993 3994 static ssize_t iwl_dbgfs_ss_force_read(struct file *file, 3995 char __user *user_buf, 3996 size_t count, loff_t *ppos) 3997 { 3998 struct iwl_lq_sta *lq_sta = file->private_data; 3999 char buf[12]; 4000 int bufsz = sizeof(buf); 4001 int pos = 0; 4002 static const char * const ss_force_name[] = { 4003 [RS_SS_FORCE_NONE] = "none", 4004 [RS_SS_FORCE_STBC] = "stbc", 4005 [RS_SS_FORCE_BFER] = "bfer", 4006 [RS_SS_FORCE_SISO] = "siso", 4007 }; 4008 4009 pos += scnprintf(buf+pos, bufsz-pos, "%s\n", 4010 ss_force_name[lq_sta->pers.ss_force]); 4011 return simple_read_from_buffer(user_buf, count, ppos, buf, pos); 4012 } 4013 4014 static ssize_t iwl_dbgfs_ss_force_write(struct iwl_lq_sta *lq_sta, char *buf, 4015 size_t count, loff_t *ppos) 4016 { 4017 struct iwl_mvm *mvm = lq_sta->pers.drv; 4018 int ret = 0; 4019 4020 if (!strncmp("none", buf, 4)) { 4021 lq_sta->pers.ss_force = RS_SS_FORCE_NONE; 4022 } else if (!strncmp("siso", buf, 4)) { 4023 lq_sta->pers.ss_force = RS_SS_FORCE_SISO; 4024 } else if (!strncmp("stbc", buf, 4)) { 4025 if (lq_sta->stbc_capable) { 4026 lq_sta->pers.ss_force = RS_SS_FORCE_STBC; 4027 } else { 4028 IWL_ERR(mvm, 4029 "can't force STBC. peer doesn't support\n"); 4030 ret = -EINVAL; 4031 } 4032 } else if (!strncmp("bfer", buf, 4)) { 4033 if (lq_sta->bfer_capable) { 4034 lq_sta->pers.ss_force = RS_SS_FORCE_BFER; 4035 } else { 4036 IWL_ERR(mvm, 4037 "can't force BFER. peer doesn't support\n"); 4038 ret = -EINVAL; 4039 } 4040 } else { 4041 IWL_ERR(mvm, "valid values none|siso|stbc|bfer\n"); 4042 ret = -EINVAL; 4043 } 4044 return ret ?: count; 4045 } 4046 4047 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \ 4048 _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_lq_sta) 4049 #define MVM_DEBUGFS_ADD_FILE_RS(name, parent, mode) do { \ 4050 debugfs_create_file(#name, mode, parent, lq_sta, \ 4051 &iwl_dbgfs_##name##_ops); \ 4052 } while (0) 4053 4054 MVM_DEBUGFS_READ_WRITE_FILE_OPS(ss_force, 32); 4055 4056 static void rs_drv_add_sta_debugfs(void *mvm, void *priv_sta, 4057 struct dentry *dir) 4058 { 4059 struct iwl_lq_sta *lq_sta = priv_sta; 4060 struct iwl_mvm_sta *mvmsta; 4061 4062 mvmsta = container_of(lq_sta, struct iwl_mvm_sta, 4063 deflink.lq_sta.rs_drv); 4064 4065 if (!mvmsta->vif) 4066 return; 4067 4068 debugfs_create_file("rate_scale_table", 0600, dir, 4069 lq_sta, &rs_sta_dbgfs_scale_table_ops); 4070 debugfs_create_file("rate_stats_table", 0400, dir, 4071 lq_sta, &rs_sta_dbgfs_stats_table_ops); 4072 debugfs_create_file("drv_tx_stats", 0600, dir, 4073 lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops); 4074 debugfs_create_u8("tx_agg_tid_enable", 0600, dir, 4075 &lq_sta->tx_agg_tid_en); 4076 debugfs_create_u8("reduced_tpc", 0600, dir, 4077 &lq_sta->pers.dbg_fixed_txp_reduction); 4078 4079 MVM_DEBUGFS_ADD_FILE_RS(ss_force, dir, 0600); 4080 } 4081 #endif 4082 4083 /* 4084 * Initialization of rate scaling information is done by driver after 4085 * the station is added. Since mac80211 calls this function before a 4086 * station is added we ignore it. 4087 */ 4088 static void rs_rate_init_ops(void *mvm_r, 4089 struct ieee80211_supported_band *sband, 4090 struct cfg80211_chan_def *chandef, 4091 struct ieee80211_sta *sta, void *mvm_sta) 4092 { 4093 } 4094 4095 /* ops for rate scaling implemented in the driver */ 4096 static const struct rate_control_ops rs_mvm_ops_drv = { 4097 .name = RS_NAME, 4098 .tx_status = rs_drv_mac80211_tx_status, 4099 .get_rate = rs_drv_get_rate, 4100 .rate_init = rs_rate_init_ops, 4101 .alloc = rs_alloc, 4102 .free = rs_free, 4103 .alloc_sta = rs_drv_alloc_sta, 4104 .free_sta = rs_free_sta, 4105 .rate_update = rs_drv_rate_update, 4106 #ifdef CONFIG_MAC80211_DEBUGFS 4107 .add_sta_debugfs = rs_drv_add_sta_debugfs, 4108 #endif 4109 .capa = RATE_CTRL_CAPA_VHT_EXT_NSS_BW, 4110 }; 4111 4112 void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, 4113 struct ieee80211_vif *vif, 4114 struct ieee80211_sta *sta, 4115 struct ieee80211_bss_conf *link_conf, 4116 struct ieee80211_link_sta *link_sta, 4117 enum nl80211_band band) 4118 { 4119 if (iwl_mvm_has_tlc_offload(mvm)) { 4120 iwl_mvm_rs_fw_rate_init(mvm, vif, sta, link_conf, 4121 link_sta, band); 4122 } else { 4123 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 4124 4125 spin_lock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock); 4126 rs_drv_rate_init(mvm, sta, band); 4127 spin_unlock_bh(&mvmsta->deflink.lq_sta.rs_drv.pers.lock); 4128 } 4129 } 4130 4131 int iwl_mvm_rate_control_register(void) 4132 { 4133 return ieee80211_rate_control_register(&rs_mvm_ops_drv); 4134 } 4135 4136 void iwl_mvm_rate_control_unregister(void) 4137 { 4138 ieee80211_rate_control_unregister(&rs_mvm_ops_drv); 4139 } 4140 4141 static int rs_drv_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, 4142 bool enable) 4143 { 4144 struct iwl_lq_cmd *lq = &mvmsta->deflink.lq_sta.rs_drv.lq; 4145 4146 lockdep_assert_held(&mvm->mutex); 4147 4148 if (enable) { 4149 if (mvmsta->tx_protection == 0) 4150 lq->flags |= LQ_FLAG_USE_RTS_MSK; 4151 mvmsta->tx_protection++; 4152 } else { 4153 mvmsta->tx_protection--; 4154 if (mvmsta->tx_protection == 0) 4155 lq->flags &= ~LQ_FLAG_USE_RTS_MSK; 4156 } 4157 4158 return iwl_mvm_send_lq_cmd(mvm, lq); 4159 } 4160 4161 /** 4162 * iwl_mvm_tx_protection - ask FW to enable RTS/CTS protection 4163 * @mvm: The mvm component 4164 * @mvmsta: The station 4165 * @enable: Enable Tx protection? 4166 * 4167 * Returns: an error code 4168 */ 4169 int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, 4170 bool enable) 4171 { 4172 if (iwl_mvm_has_tlc_offload(mvm)) 4173 return rs_fw_tx_protection(mvm, mvmsta, enable); 4174 else 4175 return rs_drv_tx_protection(mvm, mvmsta, enable); 4176 } 4177