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