1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org> 4 * Copyright (C) 2019-2021 Intel Corporation 5 */ 6 #include <linux/netdevice.h> 7 #include <linux/types.h> 8 #include <linux/skbuff.h> 9 #include <linux/debugfs.h> 10 #include <linux/random.h> 11 #include <linux/moduleparam.h> 12 #include <linux/ieee80211.h> 13 #include <net/mac80211.h> 14 #include "rate.h" 15 #include "sta_info.h" 16 #include "rc80211_minstrel_ht.h" 17 18 #define AVG_AMPDU_SIZE 16 19 #define AVG_PKT_SIZE 1200 20 21 /* Number of bits for an average sized packet */ 22 #define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3) 23 24 /* Number of symbols for a packet with (bps) bits per symbol */ 25 #define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps)) 26 27 /* Transmission time (nanoseconds) for a packet containing (syms) symbols */ 28 #define MCS_SYMBOL_TIME(sgi, syms) \ 29 (sgi ? \ 30 ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */ \ 31 ((syms) * 1000) << 2 /* syms * 4 us */ \ 32 ) 33 34 /* Transmit duration for the raw data part of an average sized packet */ 35 #define MCS_DURATION(streams, sgi, bps) \ 36 (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE) 37 38 #define BW_20 0 39 #define BW_40 1 40 #define BW_80 2 41 42 /* 43 * Define group sort order: HT40 -> SGI -> #streams 44 */ 45 #define GROUP_IDX(_streams, _sgi, _ht40) \ 46 MINSTREL_HT_GROUP_0 + \ 47 MINSTREL_MAX_STREAMS * 2 * _ht40 + \ 48 MINSTREL_MAX_STREAMS * _sgi + \ 49 _streams - 1 50 51 #define _MAX(a, b) (((a)>(b))?(a):(b)) 52 53 #define GROUP_SHIFT(duration) \ 54 _MAX(0, 16 - __builtin_clz(duration)) 55 56 /* MCS rate information for an MCS group */ 57 #define __MCS_GROUP(_streams, _sgi, _ht40, _s) \ 58 [GROUP_IDX(_streams, _sgi, _ht40)] = { \ 59 .streams = _streams, \ 60 .shift = _s, \ 61 .bw = _ht40, \ 62 .flags = \ 63 IEEE80211_TX_RC_MCS | \ 64 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \ 65 (_ht40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \ 66 .duration = { \ 67 MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26) >> _s, \ 68 MCS_DURATION(_streams, _sgi, _ht40 ? 108 : 52) >> _s, \ 69 MCS_DURATION(_streams, _sgi, _ht40 ? 162 : 78) >> _s, \ 70 MCS_DURATION(_streams, _sgi, _ht40 ? 216 : 104) >> _s, \ 71 MCS_DURATION(_streams, _sgi, _ht40 ? 324 : 156) >> _s, \ 72 MCS_DURATION(_streams, _sgi, _ht40 ? 432 : 208) >> _s, \ 73 MCS_DURATION(_streams, _sgi, _ht40 ? 486 : 234) >> _s, \ 74 MCS_DURATION(_streams, _sgi, _ht40 ? 540 : 260) >> _s \ 75 } \ 76 } 77 78 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \ 79 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26)) 80 81 #define MCS_GROUP(_streams, _sgi, _ht40) \ 82 __MCS_GROUP(_streams, _sgi, _ht40, \ 83 MCS_GROUP_SHIFT(_streams, _sgi, _ht40)) 84 85 #define VHT_GROUP_IDX(_streams, _sgi, _bw) \ 86 (MINSTREL_VHT_GROUP_0 + \ 87 MINSTREL_MAX_STREAMS * 2 * (_bw) + \ 88 MINSTREL_MAX_STREAMS * (_sgi) + \ 89 (_streams) - 1) 90 91 #define BW2VBPS(_bw, r3, r2, r1) \ 92 (_bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1) 93 94 #define __VHT_GROUP(_streams, _sgi, _bw, _s) \ 95 [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \ 96 .streams = _streams, \ 97 .shift = _s, \ 98 .bw = _bw, \ 99 .flags = \ 100 IEEE80211_TX_RC_VHT_MCS | \ 101 (_sgi ? IEEE80211_TX_RC_SHORT_GI : 0) | \ 102 (_bw == BW_80 ? IEEE80211_TX_RC_80_MHZ_WIDTH : \ 103 _bw == BW_40 ? IEEE80211_TX_RC_40_MHZ_WIDTH : 0), \ 104 .duration = { \ 105 MCS_DURATION(_streams, _sgi, \ 106 BW2VBPS(_bw, 117, 54, 26)) >> _s, \ 107 MCS_DURATION(_streams, _sgi, \ 108 BW2VBPS(_bw, 234, 108, 52)) >> _s, \ 109 MCS_DURATION(_streams, _sgi, \ 110 BW2VBPS(_bw, 351, 162, 78)) >> _s, \ 111 MCS_DURATION(_streams, _sgi, \ 112 BW2VBPS(_bw, 468, 216, 104)) >> _s, \ 113 MCS_DURATION(_streams, _sgi, \ 114 BW2VBPS(_bw, 702, 324, 156)) >> _s, \ 115 MCS_DURATION(_streams, _sgi, \ 116 BW2VBPS(_bw, 936, 432, 208)) >> _s, \ 117 MCS_DURATION(_streams, _sgi, \ 118 BW2VBPS(_bw, 1053, 486, 234)) >> _s, \ 119 MCS_DURATION(_streams, _sgi, \ 120 BW2VBPS(_bw, 1170, 540, 260)) >> _s, \ 121 MCS_DURATION(_streams, _sgi, \ 122 BW2VBPS(_bw, 1404, 648, 312)) >> _s, \ 123 MCS_DURATION(_streams, _sgi, \ 124 BW2VBPS(_bw, 1560, 720, 346)) >> _s \ 125 } \ 126 } 127 128 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \ 129 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \ 130 BW2VBPS(_bw, 117, 54, 26))) 131 132 #define VHT_GROUP(_streams, _sgi, _bw) \ 133 __VHT_GROUP(_streams, _sgi, _bw, \ 134 VHT_GROUP_SHIFT(_streams, _sgi, _bw)) 135 136 #define CCK_DURATION(_bitrate, _short) \ 137 (1000 * (10 /* SIFS */ + \ 138 (_short ? 72 + 24 : 144 + 48) + \ 139 (8 * (AVG_PKT_SIZE + 4) * 10) / (_bitrate))) 140 141 #define CCK_DURATION_LIST(_short, _s) \ 142 CCK_DURATION(10, _short) >> _s, \ 143 CCK_DURATION(20, _short) >> _s, \ 144 CCK_DURATION(55, _short) >> _s, \ 145 CCK_DURATION(110, _short) >> _s 146 147 #define __CCK_GROUP(_s) \ 148 [MINSTREL_CCK_GROUP] = { \ 149 .streams = 1, \ 150 .flags = 0, \ 151 .shift = _s, \ 152 .duration = { \ 153 CCK_DURATION_LIST(false, _s), \ 154 CCK_DURATION_LIST(true, _s) \ 155 } \ 156 } 157 158 #define CCK_GROUP_SHIFT \ 159 GROUP_SHIFT(CCK_DURATION(10, false)) 160 161 #define CCK_GROUP __CCK_GROUP(CCK_GROUP_SHIFT) 162 163 #define OFDM_DURATION(_bitrate) \ 164 (1000 * (16 /* SIFS + signal ext */ + \ 165 16 /* T_PREAMBLE */ + \ 166 4 /* T_SIGNAL */ + \ 167 4 * (((16 + 80 * (AVG_PKT_SIZE + 4) + 6) / \ 168 ((_bitrate) * 4))))) 169 170 #define OFDM_DURATION_LIST(_s) \ 171 OFDM_DURATION(60) >> _s, \ 172 OFDM_DURATION(90) >> _s, \ 173 OFDM_DURATION(120) >> _s, \ 174 OFDM_DURATION(180) >> _s, \ 175 OFDM_DURATION(240) >> _s, \ 176 OFDM_DURATION(360) >> _s, \ 177 OFDM_DURATION(480) >> _s, \ 178 OFDM_DURATION(540) >> _s 179 180 #define __OFDM_GROUP(_s) \ 181 [MINSTREL_OFDM_GROUP] = { \ 182 .streams = 1, \ 183 .flags = 0, \ 184 .shift = _s, \ 185 .duration = { \ 186 OFDM_DURATION_LIST(_s), \ 187 } \ 188 } 189 190 #define OFDM_GROUP_SHIFT \ 191 GROUP_SHIFT(OFDM_DURATION(60)) 192 193 #define OFDM_GROUP __OFDM_GROUP(OFDM_GROUP_SHIFT) 194 195 196 static bool minstrel_vht_only = true; 197 module_param(minstrel_vht_only, bool, 0644); 198 MODULE_PARM_DESC(minstrel_vht_only, 199 "Use only VHT rates when VHT is supported by sta."); 200 201 /* 202 * To enable sufficiently targeted rate sampling, MCS rates are divided into 203 * groups, based on the number of streams and flags (HT40, SGI) that they 204 * use. 205 * 206 * Sortorder has to be fixed for GROUP_IDX macro to be applicable: 207 * BW -> SGI -> #streams 208 */ 209 const struct mcs_group minstrel_mcs_groups[] = { 210 MCS_GROUP(1, 0, BW_20), 211 MCS_GROUP(2, 0, BW_20), 212 MCS_GROUP(3, 0, BW_20), 213 MCS_GROUP(4, 0, BW_20), 214 215 MCS_GROUP(1, 1, BW_20), 216 MCS_GROUP(2, 1, BW_20), 217 MCS_GROUP(3, 1, BW_20), 218 MCS_GROUP(4, 1, BW_20), 219 220 MCS_GROUP(1, 0, BW_40), 221 MCS_GROUP(2, 0, BW_40), 222 MCS_GROUP(3, 0, BW_40), 223 MCS_GROUP(4, 0, BW_40), 224 225 MCS_GROUP(1, 1, BW_40), 226 MCS_GROUP(2, 1, BW_40), 227 MCS_GROUP(3, 1, BW_40), 228 MCS_GROUP(4, 1, BW_40), 229 230 CCK_GROUP, 231 OFDM_GROUP, 232 233 VHT_GROUP(1, 0, BW_20), 234 VHT_GROUP(2, 0, BW_20), 235 VHT_GROUP(3, 0, BW_20), 236 VHT_GROUP(4, 0, BW_20), 237 238 VHT_GROUP(1, 1, BW_20), 239 VHT_GROUP(2, 1, BW_20), 240 VHT_GROUP(3, 1, BW_20), 241 VHT_GROUP(4, 1, BW_20), 242 243 VHT_GROUP(1, 0, BW_40), 244 VHT_GROUP(2, 0, BW_40), 245 VHT_GROUP(3, 0, BW_40), 246 VHT_GROUP(4, 0, BW_40), 247 248 VHT_GROUP(1, 1, BW_40), 249 VHT_GROUP(2, 1, BW_40), 250 VHT_GROUP(3, 1, BW_40), 251 VHT_GROUP(4, 1, BW_40), 252 253 VHT_GROUP(1, 0, BW_80), 254 VHT_GROUP(2, 0, BW_80), 255 VHT_GROUP(3, 0, BW_80), 256 VHT_GROUP(4, 0, BW_80), 257 258 VHT_GROUP(1, 1, BW_80), 259 VHT_GROUP(2, 1, BW_80), 260 VHT_GROUP(3, 1, BW_80), 261 VHT_GROUP(4, 1, BW_80), 262 }; 263 264 const s16 minstrel_cck_bitrates[4] = { 10, 20, 55, 110 }; 265 const s16 minstrel_ofdm_bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 }; 266 static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES] __read_mostly; 267 static const u8 minstrel_sample_seq[] = { 268 MINSTREL_SAMPLE_TYPE_INC, 269 MINSTREL_SAMPLE_TYPE_JUMP, 270 MINSTREL_SAMPLE_TYPE_INC, 271 MINSTREL_SAMPLE_TYPE_JUMP, 272 MINSTREL_SAMPLE_TYPE_INC, 273 MINSTREL_SAMPLE_TYPE_SLOW, 274 }; 275 276 static void 277 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi); 278 279 /* 280 * Some VHT MCSes are invalid (when Ndbps / Nes is not an integer) 281 * e.g for MCS9@20MHzx1Nss: Ndbps=8x52*(5/6) Nes=1 282 * 283 * Returns the valid mcs map for struct minstrel_mcs_group_data.supported 284 */ 285 static u16 286 minstrel_get_valid_vht_rates(int bw, int nss, __le16 mcs_map) 287 { 288 u16 mask = 0; 289 290 if (bw == BW_20) { 291 if (nss != 3 && nss != 6) 292 mask = BIT(9); 293 } else if (bw == BW_80) { 294 if (nss == 3 || nss == 7) 295 mask = BIT(6); 296 else if (nss == 6) 297 mask = BIT(9); 298 } else { 299 WARN_ON(bw != BW_40); 300 } 301 302 switch ((le16_to_cpu(mcs_map) >> (2 * (nss - 1))) & 3) { 303 case IEEE80211_VHT_MCS_SUPPORT_0_7: 304 mask |= 0x300; 305 break; 306 case IEEE80211_VHT_MCS_SUPPORT_0_8: 307 mask |= 0x200; 308 break; 309 case IEEE80211_VHT_MCS_SUPPORT_0_9: 310 break; 311 default: 312 mask = 0x3ff; 313 } 314 315 return 0x3ff & ~mask; 316 } 317 318 static bool 319 minstrel_ht_is_legacy_group(int group) 320 { 321 return group == MINSTREL_CCK_GROUP || 322 group == MINSTREL_OFDM_GROUP; 323 } 324 325 /* 326 * Look up an MCS group index based on mac80211 rate information 327 */ 328 static int 329 minstrel_ht_get_group_idx(struct ieee80211_tx_rate *rate) 330 { 331 return GROUP_IDX((rate->idx / 8) + 1, 332 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI), 333 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)); 334 } 335 336 static int 337 minstrel_vht_get_group_idx(struct ieee80211_tx_rate *rate) 338 { 339 return VHT_GROUP_IDX(ieee80211_rate_get_vht_nss(rate), 340 !!(rate->flags & IEEE80211_TX_RC_SHORT_GI), 341 !!(rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) + 342 2*!!(rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)); 343 } 344 345 static struct minstrel_rate_stats * 346 minstrel_ht_get_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 347 struct ieee80211_tx_rate *rate) 348 { 349 int group, idx; 350 351 if (rate->flags & IEEE80211_TX_RC_MCS) { 352 group = minstrel_ht_get_group_idx(rate); 353 idx = rate->idx % 8; 354 goto out; 355 } 356 357 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) { 358 group = minstrel_vht_get_group_idx(rate); 359 idx = ieee80211_rate_get_vht_mcs(rate); 360 goto out; 361 } 362 363 group = MINSTREL_CCK_GROUP; 364 for (idx = 0; idx < ARRAY_SIZE(mp->cck_rates); idx++) { 365 if (!(mi->supported[group] & BIT(idx))) 366 continue; 367 368 if (rate->idx != mp->cck_rates[idx]) 369 continue; 370 371 /* short preamble */ 372 if ((mi->supported[group] & BIT(idx + 4)) && 373 (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)) 374 idx += 4; 375 goto out; 376 } 377 378 group = MINSTREL_OFDM_GROUP; 379 for (idx = 0; idx < ARRAY_SIZE(mp->ofdm_rates[0]); idx++) 380 if (rate->idx == mp->ofdm_rates[mi->band][idx]) 381 goto out; 382 383 idx = 0; 384 out: 385 return &mi->groups[group].rates[idx]; 386 } 387 388 static inline struct minstrel_rate_stats * 389 minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index) 390 { 391 return &mi->groups[MI_RATE_GROUP(index)].rates[MI_RATE_IDX(index)]; 392 } 393 394 static inline int minstrel_get_duration(int index) 395 { 396 const struct mcs_group *group = &minstrel_mcs_groups[MI_RATE_GROUP(index)]; 397 unsigned int duration = group->duration[MI_RATE_IDX(index)]; 398 399 return duration << group->shift; 400 } 401 402 static unsigned int 403 minstrel_ht_avg_ampdu_len(struct minstrel_ht_sta *mi) 404 { 405 int duration; 406 407 if (mi->avg_ampdu_len) 408 return MINSTREL_TRUNC(mi->avg_ampdu_len); 409 410 if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(mi->max_tp_rate[0]))) 411 return 1; 412 413 duration = minstrel_get_duration(mi->max_tp_rate[0]); 414 415 if (duration > 400 * 1000) 416 return 2; 417 418 if (duration > 250 * 1000) 419 return 4; 420 421 if (duration > 150 * 1000) 422 return 8; 423 424 return 16; 425 } 426 427 /* 428 * Return current throughput based on the average A-MPDU length, taking into 429 * account the expected number of retransmissions and their expected length 430 */ 431 int 432 minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate, 433 int prob_avg) 434 { 435 unsigned int nsecs = 0, overhead = mi->overhead; 436 unsigned int ampdu_len = 1; 437 438 /* do not account throughput if success prob is below 10% */ 439 if (prob_avg < MINSTREL_FRAC(10, 100)) 440 return 0; 441 442 if (minstrel_ht_is_legacy_group(group)) 443 overhead = mi->overhead_legacy; 444 else 445 ampdu_len = minstrel_ht_avg_ampdu_len(mi); 446 447 nsecs = 1000 * overhead / ampdu_len; 448 nsecs += minstrel_mcs_groups[group].duration[rate] << 449 minstrel_mcs_groups[group].shift; 450 451 /* 452 * For the throughput calculation, limit the probability value to 90% to 453 * account for collision related packet error rate fluctuation 454 * (prob is scaled - see MINSTREL_FRAC above) 455 */ 456 if (prob_avg > MINSTREL_FRAC(90, 100)) 457 prob_avg = MINSTREL_FRAC(90, 100); 458 459 return MINSTREL_TRUNC(100 * ((prob_avg * 1000000) / nsecs)); 460 } 461 462 /* 463 * Find & sort topmost throughput rates 464 * 465 * If multiple rates provide equal throughput the sorting is based on their 466 * current success probability. Higher success probability is preferred among 467 * MCS groups, CCK rates do not provide aggregation and are therefore at last. 468 */ 469 static void 470 minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index, 471 u16 *tp_list) 472 { 473 int cur_group, cur_idx, cur_tp_avg, cur_prob; 474 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob; 475 int j = MAX_THR_RATES; 476 477 cur_group = MI_RATE_GROUP(index); 478 cur_idx = MI_RATE_IDX(index); 479 cur_prob = mi->groups[cur_group].rates[cur_idx].prob_avg; 480 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob); 481 482 do { 483 tmp_group = MI_RATE_GROUP(tp_list[j - 1]); 484 tmp_idx = MI_RATE_IDX(tp_list[j - 1]); 485 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg; 486 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, 487 tmp_prob); 488 if (cur_tp_avg < tmp_tp_avg || 489 (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob)) 490 break; 491 j--; 492 } while (j > 0); 493 494 if (j < MAX_THR_RATES - 1) { 495 memmove(&tp_list[j + 1], &tp_list[j], (sizeof(*tp_list) * 496 (MAX_THR_RATES - (j + 1)))); 497 } 498 if (j < MAX_THR_RATES) 499 tp_list[j] = index; 500 } 501 502 /* 503 * Find and set the topmost probability rate per sta and per group 504 */ 505 static void 506 minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 *dest, u16 index) 507 { 508 struct minstrel_mcs_group_data *mg; 509 struct minstrel_rate_stats *mrs; 510 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob; 511 int max_tp_group, max_tp_idx, max_tp_prob; 512 int cur_tp_avg, cur_group, cur_idx; 513 int max_gpr_group, max_gpr_idx; 514 int max_gpr_tp_avg, max_gpr_prob; 515 516 cur_group = MI_RATE_GROUP(index); 517 cur_idx = MI_RATE_IDX(index); 518 mg = &mi->groups[cur_group]; 519 mrs = &mg->rates[cur_idx]; 520 521 tmp_group = MI_RATE_GROUP(*dest); 522 tmp_idx = MI_RATE_IDX(*dest); 523 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg; 524 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob); 525 526 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from 527 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */ 528 max_tp_group = MI_RATE_GROUP(mi->max_tp_rate[0]); 529 max_tp_idx = MI_RATE_IDX(mi->max_tp_rate[0]); 530 max_tp_prob = mi->groups[max_tp_group].rates[max_tp_idx].prob_avg; 531 532 if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index)) && 533 !minstrel_ht_is_legacy_group(max_tp_group)) 534 return; 535 536 /* skip rates faster than max tp rate with lower prob */ 537 if (minstrel_get_duration(mi->max_tp_rate[0]) > minstrel_get_duration(index) && 538 mrs->prob_avg < max_tp_prob) 539 return; 540 541 max_gpr_group = MI_RATE_GROUP(mg->max_group_prob_rate); 542 max_gpr_idx = MI_RATE_IDX(mg->max_group_prob_rate); 543 max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_avg; 544 545 if (mrs->prob_avg > MINSTREL_FRAC(75, 100)) { 546 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, 547 mrs->prob_avg); 548 if (cur_tp_avg > tmp_tp_avg) 549 *dest = index; 550 551 max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group, 552 max_gpr_idx, 553 max_gpr_prob); 554 if (cur_tp_avg > max_gpr_tp_avg) 555 mg->max_group_prob_rate = index; 556 } else { 557 if (mrs->prob_avg > tmp_prob) 558 *dest = index; 559 if (mrs->prob_avg > max_gpr_prob) 560 mg->max_group_prob_rate = index; 561 } 562 } 563 564 565 /* 566 * Assign new rate set per sta and use CCK rates only if the fastest 567 * rate (max_tp_rate[0]) is from CCK group. This prohibits such sorted 568 * rate sets where MCS and CCK rates are mixed, because CCK rates can 569 * not use aggregation. 570 */ 571 static void 572 minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi, 573 u16 tmp_mcs_tp_rate[MAX_THR_RATES], 574 u16 tmp_legacy_tp_rate[MAX_THR_RATES]) 575 { 576 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob; 577 int i; 578 579 tmp_group = MI_RATE_GROUP(tmp_legacy_tp_rate[0]); 580 tmp_idx = MI_RATE_IDX(tmp_legacy_tp_rate[0]); 581 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg; 582 tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob); 583 584 tmp_group = MI_RATE_GROUP(tmp_mcs_tp_rate[0]); 585 tmp_idx = MI_RATE_IDX(tmp_mcs_tp_rate[0]); 586 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_avg; 587 tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob); 588 589 if (tmp_cck_tp > tmp_mcs_tp) { 590 for(i = 0; i < MAX_THR_RATES; i++) { 591 minstrel_ht_sort_best_tp_rates(mi, tmp_legacy_tp_rate[i], 592 tmp_mcs_tp_rate); 593 } 594 } 595 596 } 597 598 /* 599 * Try to increase robustness of max_prob rate by decrease number of 600 * streams if possible. 601 */ 602 static inline void 603 minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi) 604 { 605 struct minstrel_mcs_group_data *mg; 606 int tmp_max_streams, group, tmp_idx, tmp_prob; 607 int tmp_tp = 0; 608 609 if (!mi->sta->deflink.ht_cap.ht_supported) 610 return; 611 612 group = MI_RATE_GROUP(mi->max_tp_rate[0]); 613 tmp_max_streams = minstrel_mcs_groups[group].streams; 614 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) { 615 mg = &mi->groups[group]; 616 if (!mi->supported[group] || group == MINSTREL_CCK_GROUP) 617 continue; 618 619 tmp_idx = MI_RATE_IDX(mg->max_group_prob_rate); 620 tmp_prob = mi->groups[group].rates[tmp_idx].prob_avg; 621 622 if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) && 623 (minstrel_mcs_groups[group].streams < tmp_max_streams)) { 624 mi->max_prob_rate = mg->max_group_prob_rate; 625 tmp_tp = minstrel_ht_get_tp_avg(mi, group, 626 tmp_idx, 627 tmp_prob); 628 } 629 } 630 } 631 632 static u16 633 __minstrel_ht_get_sample_rate(struct minstrel_ht_sta *mi, 634 enum minstrel_sample_type type) 635 { 636 u16 *rates = mi->sample[type].sample_rates; 637 u16 cur; 638 int i; 639 640 for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) { 641 if (!rates[i]) 642 continue; 643 644 cur = rates[i]; 645 rates[i] = 0; 646 return cur; 647 } 648 649 return 0; 650 } 651 652 static inline int 653 minstrel_ewma(int old, int new, int weight) 654 { 655 int diff, incr; 656 657 diff = new - old; 658 incr = (EWMA_DIV - weight) * diff / EWMA_DIV; 659 660 return old + incr; 661 } 662 663 static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in) 664 { 665 s32 out_1 = *prev_1; 666 s32 out_2 = *prev_2; 667 s32 val; 668 669 if (!in) 670 in += 1; 671 672 if (!out_1) { 673 val = out_1 = in; 674 goto out; 675 } 676 677 val = MINSTREL_AVG_COEFF1 * in; 678 val += MINSTREL_AVG_COEFF2 * out_1; 679 val += MINSTREL_AVG_COEFF3 * out_2; 680 val >>= MINSTREL_SCALE; 681 682 if (val > 1 << MINSTREL_SCALE) 683 val = 1 << MINSTREL_SCALE; 684 if (val < 0) 685 val = 1; 686 687 out: 688 *prev_2 = out_1; 689 *prev_1 = val; 690 691 return val; 692 } 693 694 /* 695 * Recalculate statistics and counters of a given rate 696 */ 697 static void 698 minstrel_ht_calc_rate_stats(struct minstrel_priv *mp, 699 struct minstrel_rate_stats *mrs) 700 { 701 unsigned int cur_prob; 702 703 if (unlikely(mrs->attempts > 0)) { 704 cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts); 705 minstrel_filter_avg_add(&mrs->prob_avg, 706 &mrs->prob_avg_1, cur_prob); 707 mrs->att_hist += mrs->attempts; 708 mrs->succ_hist += mrs->success; 709 } 710 711 mrs->last_success = mrs->success; 712 mrs->last_attempts = mrs->attempts; 713 mrs->success = 0; 714 mrs->attempts = 0; 715 } 716 717 static bool 718 minstrel_ht_find_sample_rate(struct minstrel_ht_sta *mi, int type, int idx) 719 { 720 int i; 721 722 for (i = 0; i < MINSTREL_SAMPLE_RATES; i++) { 723 u16 cur = mi->sample[type].sample_rates[i]; 724 725 if (cur == idx) 726 return true; 727 728 if (!cur) 729 break; 730 } 731 732 return false; 733 } 734 735 static int 736 minstrel_ht_move_sample_rates(struct minstrel_ht_sta *mi, int type, 737 u32 fast_rate_dur, u32 slow_rate_dur) 738 { 739 u16 *rates = mi->sample[type].sample_rates; 740 int i, j; 741 742 for (i = 0, j = 0; i < MINSTREL_SAMPLE_RATES; i++) { 743 u32 duration; 744 bool valid = false; 745 u16 cur; 746 747 cur = rates[i]; 748 if (!cur) 749 continue; 750 751 duration = minstrel_get_duration(cur); 752 switch (type) { 753 case MINSTREL_SAMPLE_TYPE_SLOW: 754 valid = duration > fast_rate_dur && 755 duration < slow_rate_dur; 756 break; 757 case MINSTREL_SAMPLE_TYPE_INC: 758 case MINSTREL_SAMPLE_TYPE_JUMP: 759 valid = duration < fast_rate_dur; 760 break; 761 default: 762 valid = false; 763 break; 764 } 765 766 if (!valid) { 767 rates[i] = 0; 768 continue; 769 } 770 771 if (i == j) 772 continue; 773 774 rates[j++] = cur; 775 rates[i] = 0; 776 } 777 778 return j; 779 } 780 781 static int 782 minstrel_ht_group_min_rate_offset(struct minstrel_ht_sta *mi, int group, 783 u32 max_duration) 784 { 785 u16 supported = mi->supported[group]; 786 int i; 787 788 for (i = 0; i < MCS_GROUP_RATES && supported; i++, supported >>= 1) { 789 if (!(supported & BIT(0))) 790 continue; 791 792 if (minstrel_get_duration(MI_RATE(group, i)) >= max_duration) 793 continue; 794 795 return i; 796 } 797 798 return -1; 799 } 800 801 /* 802 * Incremental update rates: 803 * Flip through groups and pick the first group rate that is faster than the 804 * highest currently selected rate 805 */ 806 static u16 807 minstrel_ht_next_inc_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur) 808 { 809 u8 type = MINSTREL_SAMPLE_TYPE_INC; 810 int i, index = 0; 811 u8 group; 812 813 group = mi->sample[type].sample_group; 814 for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) { 815 group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups); 816 817 index = minstrel_ht_group_min_rate_offset(mi, group, 818 fast_rate_dur); 819 if (index < 0) 820 continue; 821 822 index = MI_RATE(group, index & 0xf); 823 if (!minstrel_ht_find_sample_rate(mi, type, index)) 824 goto out; 825 } 826 index = 0; 827 828 out: 829 mi->sample[type].sample_group = group; 830 831 return index; 832 } 833 834 static int 835 minstrel_ht_next_group_sample_rate(struct minstrel_ht_sta *mi, int group, 836 u16 supported, int offset) 837 { 838 struct minstrel_mcs_group_data *mg = &mi->groups[group]; 839 u16 idx; 840 int i; 841 842 for (i = 0; i < MCS_GROUP_RATES; i++) { 843 idx = sample_table[mg->column][mg->index]; 844 if (++mg->index >= MCS_GROUP_RATES) { 845 mg->index = 0; 846 if (++mg->column >= ARRAY_SIZE(sample_table)) 847 mg->column = 0; 848 } 849 850 if (idx < offset) 851 continue; 852 853 if (!(supported & BIT(idx))) 854 continue; 855 856 return MI_RATE(group, idx); 857 } 858 859 return -1; 860 } 861 862 /* 863 * Jump rates: 864 * Sample random rates, use those that are faster than the highest 865 * currently selected rate. Rates between the fastest and the slowest 866 * get sorted into the slow sample bucket, but only if it has room 867 */ 868 static u16 869 minstrel_ht_next_jump_rate(struct minstrel_ht_sta *mi, u32 fast_rate_dur, 870 u32 slow_rate_dur, int *slow_rate_ofs) 871 { 872 struct minstrel_rate_stats *mrs; 873 u32 max_duration = slow_rate_dur; 874 int i, index, offset; 875 u16 *slow_rates; 876 u16 supported; 877 u32 duration; 878 u8 group; 879 880 if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES) 881 max_duration = fast_rate_dur; 882 883 slow_rates = mi->sample[MINSTREL_SAMPLE_TYPE_SLOW].sample_rates; 884 group = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group; 885 for (i = 0; i < ARRAY_SIZE(minstrel_mcs_groups); i++) { 886 u8 type; 887 888 group = (group + 1) % ARRAY_SIZE(minstrel_mcs_groups); 889 890 supported = mi->supported[group]; 891 if (!supported) 892 continue; 893 894 offset = minstrel_ht_group_min_rate_offset(mi, group, 895 max_duration); 896 if (offset < 0) 897 continue; 898 899 index = minstrel_ht_next_group_sample_rate(mi, group, supported, 900 offset); 901 if (index < 0) 902 continue; 903 904 duration = minstrel_get_duration(index); 905 if (duration < fast_rate_dur) 906 type = MINSTREL_SAMPLE_TYPE_JUMP; 907 else 908 type = MINSTREL_SAMPLE_TYPE_SLOW; 909 910 if (minstrel_ht_find_sample_rate(mi, type, index)) 911 continue; 912 913 if (type == MINSTREL_SAMPLE_TYPE_JUMP) 914 goto found; 915 916 if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES) 917 continue; 918 919 if (duration >= slow_rate_dur) 920 continue; 921 922 /* skip slow rates with high success probability */ 923 mrs = minstrel_get_ratestats(mi, index); 924 if (mrs->prob_avg > MINSTREL_FRAC(95, 100)) 925 continue; 926 927 slow_rates[(*slow_rate_ofs)++] = index; 928 if (*slow_rate_ofs >= MINSTREL_SAMPLE_RATES) 929 max_duration = fast_rate_dur; 930 } 931 index = 0; 932 933 found: 934 mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_group = group; 935 936 return index; 937 } 938 939 static void 940 minstrel_ht_refill_sample_rates(struct minstrel_ht_sta *mi) 941 { 942 u32 prob_dur = minstrel_get_duration(mi->max_prob_rate); 943 u32 tp_dur = minstrel_get_duration(mi->max_tp_rate[0]); 944 u32 tp2_dur = minstrel_get_duration(mi->max_tp_rate[1]); 945 u32 fast_rate_dur = min(min(tp_dur, tp2_dur), prob_dur); 946 u32 slow_rate_dur = max(max(tp_dur, tp2_dur), prob_dur); 947 u16 *rates; 948 int i, j; 949 950 rates = mi->sample[MINSTREL_SAMPLE_TYPE_INC].sample_rates; 951 i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_INC, 952 fast_rate_dur, slow_rate_dur); 953 while (i < MINSTREL_SAMPLE_RATES) { 954 rates[i] = minstrel_ht_next_inc_rate(mi, tp_dur); 955 if (!rates[i]) 956 break; 957 958 i++; 959 } 960 961 rates = mi->sample[MINSTREL_SAMPLE_TYPE_JUMP].sample_rates; 962 i = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_JUMP, 963 fast_rate_dur, slow_rate_dur); 964 j = minstrel_ht_move_sample_rates(mi, MINSTREL_SAMPLE_TYPE_SLOW, 965 fast_rate_dur, slow_rate_dur); 966 while (i < MINSTREL_SAMPLE_RATES) { 967 rates[i] = minstrel_ht_next_jump_rate(mi, fast_rate_dur, 968 slow_rate_dur, &j); 969 if (!rates[i]) 970 break; 971 972 i++; 973 } 974 975 for (i = 0; i < ARRAY_SIZE(mi->sample); i++) 976 memcpy(mi->sample[i].cur_sample_rates, mi->sample[i].sample_rates, 977 sizeof(mi->sample[i].cur_sample_rates)); 978 } 979 980 981 /* 982 * Update rate statistics and select new primary rates 983 * 984 * Rules for rate selection: 985 * - max_prob_rate must use only one stream, as a tradeoff between delivery 986 * probability and throughput during strong fluctuations 987 * - as long as the max prob rate has a probability of more than 75%, pick 988 * higher throughput rates, even if the probablity is a bit lower 989 */ 990 static void 991 minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) 992 { 993 struct minstrel_mcs_group_data *mg; 994 struct minstrel_rate_stats *mrs; 995 int group, i, j, cur_prob; 996 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES]; 997 u16 tmp_legacy_tp_rate[MAX_THR_RATES], tmp_max_prob_rate; 998 u16 index; 999 bool ht_supported = mi->sta->deflink.ht_cap.ht_supported; 1000 1001 if (mi->ampdu_packets > 0) { 1002 if (!ieee80211_hw_check(mp->hw, TX_STATUS_NO_AMPDU_LEN)) 1003 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len, 1004 MINSTREL_FRAC(mi->ampdu_len, mi->ampdu_packets), 1005 EWMA_LEVEL); 1006 else 1007 mi->avg_ampdu_len = 0; 1008 mi->ampdu_len = 0; 1009 mi->ampdu_packets = 0; 1010 } 1011 1012 if (mi->supported[MINSTREL_CCK_GROUP]) 1013 group = MINSTREL_CCK_GROUP; 1014 else if (mi->supported[MINSTREL_OFDM_GROUP]) 1015 group = MINSTREL_OFDM_GROUP; 1016 else 1017 group = 0; 1018 1019 index = MI_RATE(group, 0); 1020 for (j = 0; j < ARRAY_SIZE(tmp_legacy_tp_rate); j++) 1021 tmp_legacy_tp_rate[j] = index; 1022 1023 if (mi->supported[MINSTREL_VHT_GROUP_0]) 1024 group = MINSTREL_VHT_GROUP_0; 1025 else if (ht_supported) 1026 group = MINSTREL_HT_GROUP_0; 1027 else if (mi->supported[MINSTREL_CCK_GROUP]) 1028 group = MINSTREL_CCK_GROUP; 1029 else 1030 group = MINSTREL_OFDM_GROUP; 1031 1032 index = MI_RATE(group, 0); 1033 tmp_max_prob_rate = index; 1034 for (j = 0; j < ARRAY_SIZE(tmp_mcs_tp_rate); j++) 1035 tmp_mcs_tp_rate[j] = index; 1036 1037 /* Find best rate sets within all MCS groups*/ 1038 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) { 1039 u16 *tp_rate = tmp_mcs_tp_rate; 1040 u16 last_prob = 0; 1041 1042 mg = &mi->groups[group]; 1043 if (!mi->supported[group]) 1044 continue; 1045 1046 /* (re)Initialize group rate indexes */ 1047 for(j = 0; j < MAX_THR_RATES; j++) 1048 tmp_group_tp_rate[j] = MI_RATE(group, 0); 1049 1050 if (group == MINSTREL_CCK_GROUP && ht_supported) 1051 tp_rate = tmp_legacy_tp_rate; 1052 1053 for (i = MCS_GROUP_RATES - 1; i >= 0; i--) { 1054 if (!(mi->supported[group] & BIT(i))) 1055 continue; 1056 1057 index = MI_RATE(group, i); 1058 1059 mrs = &mg->rates[i]; 1060 mrs->retry_updated = false; 1061 minstrel_ht_calc_rate_stats(mp, mrs); 1062 1063 if (mrs->att_hist) 1064 last_prob = max(last_prob, mrs->prob_avg); 1065 else 1066 mrs->prob_avg = max(last_prob, mrs->prob_avg); 1067 cur_prob = mrs->prob_avg; 1068 1069 if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0) 1070 continue; 1071 1072 /* Find max throughput rate set */ 1073 minstrel_ht_sort_best_tp_rates(mi, index, tp_rate); 1074 1075 /* Find max throughput rate set within a group */ 1076 minstrel_ht_sort_best_tp_rates(mi, index, 1077 tmp_group_tp_rate); 1078 } 1079 1080 memcpy(mg->max_group_tp_rate, tmp_group_tp_rate, 1081 sizeof(mg->max_group_tp_rate)); 1082 } 1083 1084 /* Assign new rate set per sta */ 1085 minstrel_ht_assign_best_tp_rates(mi, tmp_mcs_tp_rate, 1086 tmp_legacy_tp_rate); 1087 memcpy(mi->max_tp_rate, tmp_mcs_tp_rate, sizeof(mi->max_tp_rate)); 1088 1089 for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) { 1090 if (!mi->supported[group]) 1091 continue; 1092 1093 mg = &mi->groups[group]; 1094 mg->max_group_prob_rate = MI_RATE(group, 0); 1095 1096 for (i = 0; i < MCS_GROUP_RATES; i++) { 1097 if (!(mi->supported[group] & BIT(i))) 1098 continue; 1099 1100 index = MI_RATE(group, i); 1101 1102 /* Find max probability rate per group and global */ 1103 minstrel_ht_set_best_prob_rate(mi, &tmp_max_prob_rate, 1104 index); 1105 } 1106 } 1107 1108 mi->max_prob_rate = tmp_max_prob_rate; 1109 1110 /* Try to increase robustness of max_prob_rate*/ 1111 minstrel_ht_prob_rate_reduce_streams(mi); 1112 minstrel_ht_refill_sample_rates(mi); 1113 1114 #ifdef CONFIG_MAC80211_DEBUGFS 1115 /* use fixed index if set */ 1116 if (mp->fixed_rate_idx != -1) { 1117 for (i = 0; i < 4; i++) 1118 mi->max_tp_rate[i] = mp->fixed_rate_idx; 1119 mi->max_prob_rate = mp->fixed_rate_idx; 1120 } 1121 #endif 1122 1123 /* Reset update timer */ 1124 mi->last_stats_update = jiffies; 1125 mi->sample_time = jiffies; 1126 } 1127 1128 static bool 1129 minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 1130 struct ieee80211_tx_rate *rate) 1131 { 1132 int i; 1133 1134 if (rate->idx < 0) 1135 return false; 1136 1137 if (!rate->count) 1138 return false; 1139 1140 if (rate->flags & IEEE80211_TX_RC_MCS || 1141 rate->flags & IEEE80211_TX_RC_VHT_MCS) 1142 return true; 1143 1144 for (i = 0; i < ARRAY_SIZE(mp->cck_rates); i++) 1145 if (rate->idx == mp->cck_rates[i]) 1146 return true; 1147 1148 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) 1149 if (rate->idx == mp->ofdm_rates[mi->band][i]) 1150 return true; 1151 1152 return false; 1153 } 1154 1155 static void 1156 minstrel_downgrade_rate(struct minstrel_ht_sta *mi, u16 *idx, bool primary) 1157 { 1158 int group, orig_group; 1159 1160 orig_group = group = MI_RATE_GROUP(*idx); 1161 while (group > 0) { 1162 group--; 1163 1164 if (!mi->supported[group]) 1165 continue; 1166 1167 if (minstrel_mcs_groups[group].streams > 1168 minstrel_mcs_groups[orig_group].streams) 1169 continue; 1170 1171 if (primary) 1172 *idx = mi->groups[group].max_group_tp_rate[0]; 1173 else 1174 *idx = mi->groups[group].max_group_tp_rate[1]; 1175 break; 1176 } 1177 } 1178 1179 static void 1180 minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband, 1181 void *priv_sta, struct ieee80211_tx_status *st) 1182 { 1183 struct ieee80211_tx_info *info = st->info; 1184 struct minstrel_ht_sta *mi = priv_sta; 1185 struct ieee80211_tx_rate *ar = info->status.rates; 1186 struct minstrel_rate_stats *rate, *rate2; 1187 struct minstrel_priv *mp = priv; 1188 u32 update_interval = mp->update_interval; 1189 bool last, update = false; 1190 int i; 1191 1192 /* Ignore packet that was sent with noAck flag */ 1193 if (info->flags & IEEE80211_TX_CTL_NO_ACK) 1194 return; 1195 1196 /* This packet was aggregated but doesn't carry status info */ 1197 if ((info->flags & IEEE80211_TX_CTL_AMPDU) && 1198 !(info->flags & IEEE80211_TX_STAT_AMPDU)) 1199 return; 1200 1201 if (!(info->flags & IEEE80211_TX_STAT_AMPDU)) { 1202 info->status.ampdu_ack_len = 1203 (info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0); 1204 info->status.ampdu_len = 1; 1205 } 1206 1207 /* wraparound */ 1208 if (mi->total_packets >= ~0 - info->status.ampdu_len) { 1209 mi->total_packets = 0; 1210 mi->sample_packets = 0; 1211 } 1212 1213 mi->total_packets += info->status.ampdu_len; 1214 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) 1215 mi->sample_packets += info->status.ampdu_len; 1216 1217 mi->ampdu_packets++; 1218 mi->ampdu_len += info->status.ampdu_len; 1219 1220 last = !minstrel_ht_txstat_valid(mp, mi, &ar[0]); 1221 for (i = 0; !last; i++) { 1222 last = (i == IEEE80211_TX_MAX_RATES - 1) || 1223 !minstrel_ht_txstat_valid(mp, mi, &ar[i + 1]); 1224 1225 rate = minstrel_ht_get_stats(mp, mi, &ar[i]); 1226 if (last) 1227 rate->success += info->status.ampdu_ack_len; 1228 1229 rate->attempts += ar[i].count * info->status.ampdu_len; 1230 } 1231 1232 if (mp->hw->max_rates > 1) { 1233 /* 1234 * check for sudden death of spatial multiplexing, 1235 * downgrade to a lower number of streams if necessary. 1236 */ 1237 rate = minstrel_get_ratestats(mi, mi->max_tp_rate[0]); 1238 if (rate->attempts > 30 && 1239 rate->success < rate->attempts / 4) { 1240 minstrel_downgrade_rate(mi, &mi->max_tp_rate[0], true); 1241 update = true; 1242 } 1243 1244 rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate[1]); 1245 if (rate2->attempts > 30 && 1246 rate2->success < rate2->attempts / 4) { 1247 minstrel_downgrade_rate(mi, &mi->max_tp_rate[1], false); 1248 update = true; 1249 } 1250 } 1251 1252 if (time_after(jiffies, mi->last_stats_update + update_interval)) { 1253 update = true; 1254 minstrel_ht_update_stats(mp, mi); 1255 } 1256 1257 if (update) 1258 minstrel_ht_update_rates(mp, mi); 1259 } 1260 1261 static void 1262 minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 1263 int index) 1264 { 1265 struct minstrel_rate_stats *mrs; 1266 unsigned int tx_time, tx_time_rtscts, tx_time_data; 1267 unsigned int cw = mp->cw_min; 1268 unsigned int ctime = 0; 1269 unsigned int t_slot = 9; /* FIXME */ 1270 unsigned int ampdu_len = minstrel_ht_avg_ampdu_len(mi); 1271 unsigned int overhead = 0, overhead_rtscts = 0; 1272 1273 mrs = minstrel_get_ratestats(mi, index); 1274 if (mrs->prob_avg < MINSTREL_FRAC(1, 10)) { 1275 mrs->retry_count = 1; 1276 mrs->retry_count_rtscts = 1; 1277 return; 1278 } 1279 1280 mrs->retry_count = 2; 1281 mrs->retry_count_rtscts = 2; 1282 mrs->retry_updated = true; 1283 1284 tx_time_data = minstrel_get_duration(index) * ampdu_len / 1000; 1285 1286 /* Contention time for first 2 tries */ 1287 ctime = (t_slot * cw) >> 1; 1288 cw = min((cw << 1) | 1, mp->cw_max); 1289 ctime += (t_slot * cw) >> 1; 1290 cw = min((cw << 1) | 1, mp->cw_max); 1291 1292 if (minstrel_ht_is_legacy_group(MI_RATE_GROUP(index))) { 1293 overhead = mi->overhead_legacy; 1294 overhead_rtscts = mi->overhead_legacy_rtscts; 1295 } else { 1296 overhead = mi->overhead; 1297 overhead_rtscts = mi->overhead_rtscts; 1298 } 1299 1300 /* Total TX time for data and Contention after first 2 tries */ 1301 tx_time = ctime + 2 * (overhead + tx_time_data); 1302 tx_time_rtscts = ctime + 2 * (overhead_rtscts + tx_time_data); 1303 1304 /* See how many more tries we can fit inside segment size */ 1305 do { 1306 /* Contention time for this try */ 1307 ctime = (t_slot * cw) >> 1; 1308 cw = min((cw << 1) | 1, mp->cw_max); 1309 1310 /* Total TX time after this try */ 1311 tx_time += ctime + overhead + tx_time_data; 1312 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data; 1313 1314 if (tx_time_rtscts < mp->segment_size) 1315 mrs->retry_count_rtscts++; 1316 } while ((tx_time < mp->segment_size) && 1317 (++mrs->retry_count < mp->max_retry)); 1318 } 1319 1320 1321 static void 1322 minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 1323 struct ieee80211_sta_rates *ratetbl, int offset, int index) 1324 { 1325 int group_idx = MI_RATE_GROUP(index); 1326 const struct mcs_group *group = &minstrel_mcs_groups[group_idx]; 1327 struct minstrel_rate_stats *mrs; 1328 u8 idx; 1329 u16 flags = group->flags; 1330 1331 mrs = minstrel_get_ratestats(mi, index); 1332 if (!mrs->retry_updated) 1333 minstrel_calc_retransmit(mp, mi, index); 1334 1335 if (mrs->prob_avg < MINSTREL_FRAC(20, 100) || !mrs->retry_count) { 1336 ratetbl->rate[offset].count = 2; 1337 ratetbl->rate[offset].count_rts = 2; 1338 ratetbl->rate[offset].count_cts = 2; 1339 } else { 1340 ratetbl->rate[offset].count = mrs->retry_count; 1341 ratetbl->rate[offset].count_cts = mrs->retry_count; 1342 ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts; 1343 } 1344 1345 index = MI_RATE_IDX(index); 1346 if (group_idx == MINSTREL_CCK_GROUP) 1347 idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)]; 1348 else if (group_idx == MINSTREL_OFDM_GROUP) 1349 idx = mp->ofdm_rates[mi->band][index % 1350 ARRAY_SIZE(mp->ofdm_rates[0])]; 1351 else if (flags & IEEE80211_TX_RC_VHT_MCS) 1352 idx = ((group->streams - 1) << 4) | 1353 (index & 0xF); 1354 else 1355 idx = index + (group->streams - 1) * 8; 1356 1357 /* enable RTS/CTS if needed: 1358 * - if station is in dynamic SMPS (and streams > 1) 1359 * - for fallback rates, to increase chances of getting through 1360 */ 1361 if (offset > 0 || 1362 (mi->sta->smps_mode == IEEE80211_SMPS_DYNAMIC && 1363 group->streams > 1)) { 1364 ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts; 1365 flags |= IEEE80211_TX_RC_USE_RTS_CTS; 1366 } 1367 1368 ratetbl->rate[offset].idx = idx; 1369 ratetbl->rate[offset].flags = flags; 1370 } 1371 1372 static inline int 1373 minstrel_ht_get_prob_avg(struct minstrel_ht_sta *mi, int rate) 1374 { 1375 int group = MI_RATE_GROUP(rate); 1376 rate = MI_RATE_IDX(rate); 1377 return mi->groups[group].rates[rate].prob_avg; 1378 } 1379 1380 static int 1381 minstrel_ht_get_max_amsdu_len(struct minstrel_ht_sta *mi) 1382 { 1383 int group = MI_RATE_GROUP(mi->max_prob_rate); 1384 const struct mcs_group *g = &minstrel_mcs_groups[group]; 1385 int rate = MI_RATE_IDX(mi->max_prob_rate); 1386 unsigned int duration; 1387 1388 /* Disable A-MSDU if max_prob_rate is bad */ 1389 if (mi->groups[group].rates[rate].prob_avg < MINSTREL_FRAC(50, 100)) 1390 return 1; 1391 1392 duration = g->duration[rate]; 1393 duration <<= g->shift; 1394 1395 /* If the rate is slower than single-stream MCS1, make A-MSDU limit small */ 1396 if (duration > MCS_DURATION(1, 0, 52)) 1397 return 500; 1398 1399 /* 1400 * If the rate is slower than single-stream MCS4, limit A-MSDU to usual 1401 * data packet size 1402 */ 1403 if (duration > MCS_DURATION(1, 0, 104)) 1404 return 1600; 1405 1406 /* 1407 * If the rate is slower than single-stream MCS7, or if the max throughput 1408 * rate success probability is less than 75%, limit A-MSDU to twice the usual 1409 * data packet size 1410 */ 1411 if (duration > MCS_DURATION(1, 0, 260) || 1412 (minstrel_ht_get_prob_avg(mi, mi->max_tp_rate[0]) < 1413 MINSTREL_FRAC(75, 100))) 1414 return 3200; 1415 1416 /* 1417 * HT A-MPDU limits maximum MPDU size under BA agreement to 4095 bytes. 1418 * Since aggregation sessions are started/stopped without txq flush, use 1419 * the limit here to avoid the complexity of having to de-aggregate 1420 * packets in the queue. 1421 */ 1422 if (!mi->sta->deflink.vht_cap.vht_supported) 1423 return IEEE80211_MAX_MPDU_LEN_HT_BA; 1424 1425 /* unlimited */ 1426 return 0; 1427 } 1428 1429 static void 1430 minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) 1431 { 1432 struct ieee80211_sta_rates *rates; 1433 int i = 0; 1434 1435 rates = kzalloc(sizeof(*rates), GFP_ATOMIC); 1436 if (!rates) 1437 return; 1438 1439 /* Start with max_tp_rate[0] */ 1440 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[0]); 1441 1442 if (mp->hw->max_rates >= 3) { 1443 /* At least 3 tx rates supported, use max_tp_rate[1] next */ 1444 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate[1]); 1445 } 1446 1447 if (mp->hw->max_rates >= 2) { 1448 minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate); 1449 } 1450 1451 mi->sta->max_rc_amsdu_len = minstrel_ht_get_max_amsdu_len(mi); 1452 rates->rate[i].idx = -1; 1453 rate_control_set_rates(mp->hw, mi->sta, rates); 1454 } 1455 1456 static u16 1457 minstrel_ht_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) 1458 { 1459 u8 seq; 1460 1461 if (mp->hw->max_rates > 1) { 1462 seq = mi->sample_seq; 1463 mi->sample_seq = (seq + 1) % ARRAY_SIZE(minstrel_sample_seq); 1464 seq = minstrel_sample_seq[seq]; 1465 } else { 1466 seq = MINSTREL_SAMPLE_TYPE_INC; 1467 } 1468 1469 return __minstrel_ht_get_sample_rate(mi, seq); 1470 } 1471 1472 static void 1473 minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta, 1474 struct ieee80211_tx_rate_control *txrc) 1475 { 1476 const struct mcs_group *sample_group; 1477 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb); 1478 struct ieee80211_tx_rate *rate = &info->status.rates[0]; 1479 struct minstrel_ht_sta *mi = priv_sta; 1480 struct minstrel_priv *mp = priv; 1481 u16 sample_idx; 1482 1483 info->flags |= mi->tx_flags; 1484 1485 #ifdef CONFIG_MAC80211_DEBUGFS 1486 if (mp->fixed_rate_idx != -1) 1487 return; 1488 #endif 1489 1490 /* Don't use EAPOL frames for sampling on non-mrr hw */ 1491 if (mp->hw->max_rates == 1 && 1492 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO)) 1493 return; 1494 1495 if (time_is_after_jiffies(mi->sample_time)) 1496 return; 1497 1498 mi->sample_time = jiffies + MINSTREL_SAMPLE_INTERVAL; 1499 sample_idx = minstrel_ht_get_sample_rate(mp, mi); 1500 if (!sample_idx) 1501 return; 1502 1503 sample_group = &minstrel_mcs_groups[MI_RATE_GROUP(sample_idx)]; 1504 sample_idx = MI_RATE_IDX(sample_idx); 1505 1506 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP] && 1507 (sample_idx >= 4) != txrc->short_preamble) 1508 return; 1509 1510 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE; 1511 rate->count = 1; 1512 1513 if (sample_group == &minstrel_mcs_groups[MINSTREL_CCK_GROUP]) { 1514 int idx = sample_idx % ARRAY_SIZE(mp->cck_rates); 1515 rate->idx = mp->cck_rates[idx]; 1516 } else if (sample_group == &minstrel_mcs_groups[MINSTREL_OFDM_GROUP]) { 1517 int idx = sample_idx % ARRAY_SIZE(mp->ofdm_rates[0]); 1518 rate->idx = mp->ofdm_rates[mi->band][idx]; 1519 } else if (sample_group->flags & IEEE80211_TX_RC_VHT_MCS) { 1520 ieee80211_rate_set_vht(rate, MI_RATE_IDX(sample_idx), 1521 sample_group->streams); 1522 } else { 1523 rate->idx = sample_idx + (sample_group->streams - 1) * 8; 1524 } 1525 1526 rate->flags = sample_group->flags; 1527 } 1528 1529 static void 1530 minstrel_ht_update_cck(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 1531 struct ieee80211_supported_band *sband, 1532 struct ieee80211_sta *sta) 1533 { 1534 int i; 1535 1536 if (sband->band != NL80211_BAND_2GHZ) 1537 return; 1538 1539 if (sta->deflink.ht_cap.ht_supported && 1540 !ieee80211_hw_check(mp->hw, SUPPORTS_HT_CCK_RATES)) 1541 return; 1542 1543 for (i = 0; i < 4; i++) { 1544 if (mp->cck_rates[i] == 0xff || 1545 !rate_supported(sta, sband->band, mp->cck_rates[i])) 1546 continue; 1547 1548 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i); 1549 if (sband->bitrates[i].flags & IEEE80211_RATE_SHORT_PREAMBLE) 1550 mi->supported[MINSTREL_CCK_GROUP] |= BIT(i + 4); 1551 } 1552 } 1553 1554 static void 1555 minstrel_ht_update_ofdm(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 1556 struct ieee80211_supported_band *sband, 1557 struct ieee80211_sta *sta) 1558 { 1559 const u8 *rates; 1560 int i; 1561 1562 if (sta->deflink.ht_cap.ht_supported) 1563 return; 1564 1565 rates = mp->ofdm_rates[sband->band]; 1566 for (i = 0; i < ARRAY_SIZE(mp->ofdm_rates[0]); i++) { 1567 if (rates[i] == 0xff || 1568 !rate_supported(sta, sband->band, rates[i])) 1569 continue; 1570 1571 mi->supported[MINSTREL_OFDM_GROUP] |= BIT(i); 1572 } 1573 } 1574 1575 static void 1576 minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband, 1577 struct cfg80211_chan_def *chandef, 1578 struct ieee80211_sta *sta, void *priv_sta) 1579 { 1580 struct minstrel_priv *mp = priv; 1581 struct minstrel_ht_sta *mi = priv_sta; 1582 struct ieee80211_mcs_info *mcs = &sta->deflink.ht_cap.mcs; 1583 u16 ht_cap = sta->deflink.ht_cap.cap; 1584 struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap; 1585 const struct ieee80211_rate *ctl_rate; 1586 bool ldpc, erp; 1587 int use_vht; 1588 int n_supported = 0; 1589 int ack_dur; 1590 int stbc; 1591 int i; 1592 1593 BUILD_BUG_ON(ARRAY_SIZE(minstrel_mcs_groups) != MINSTREL_GROUPS_NB); 1594 1595 if (vht_cap->vht_supported) 1596 use_vht = vht_cap->vht_mcs.tx_mcs_map != cpu_to_le16(~0); 1597 else 1598 use_vht = 0; 1599 1600 memset(mi, 0, sizeof(*mi)); 1601 1602 mi->sta = sta; 1603 mi->band = sband->band; 1604 mi->last_stats_update = jiffies; 1605 1606 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0); 1607 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0); 1608 mi->overhead += ack_dur; 1609 mi->overhead_rtscts = mi->overhead + 2 * ack_dur; 1610 1611 ctl_rate = &sband->bitrates[rate_lowest_index(sband, sta)]; 1612 erp = ctl_rate->flags & IEEE80211_RATE_ERP_G; 1613 ack_dur = ieee80211_frame_duration(sband->band, 10, 1614 ctl_rate->bitrate, erp, 1, 1615 ieee80211_chandef_get_shift(chandef)); 1616 mi->overhead_legacy = ack_dur; 1617 mi->overhead_legacy_rtscts = mi->overhead_legacy + 2 * ack_dur; 1618 1619 mi->avg_ampdu_len = MINSTREL_FRAC(1, 1); 1620 1621 if (!use_vht) { 1622 stbc = (ht_cap & IEEE80211_HT_CAP_RX_STBC) >> 1623 IEEE80211_HT_CAP_RX_STBC_SHIFT; 1624 1625 ldpc = ht_cap & IEEE80211_HT_CAP_LDPC_CODING; 1626 } else { 1627 stbc = (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK) >> 1628 IEEE80211_VHT_CAP_RXSTBC_SHIFT; 1629 1630 ldpc = vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC; 1631 } 1632 1633 mi->tx_flags |= stbc << IEEE80211_TX_CTL_STBC_SHIFT; 1634 if (ldpc) 1635 mi->tx_flags |= IEEE80211_TX_CTL_LDPC; 1636 1637 for (i = 0; i < ARRAY_SIZE(mi->groups); i++) { 1638 u32 gflags = minstrel_mcs_groups[i].flags; 1639 int bw, nss; 1640 1641 mi->supported[i] = 0; 1642 if (minstrel_ht_is_legacy_group(i)) 1643 continue; 1644 1645 if (gflags & IEEE80211_TX_RC_SHORT_GI) { 1646 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) { 1647 if (!(ht_cap & IEEE80211_HT_CAP_SGI_40)) 1648 continue; 1649 } else { 1650 if (!(ht_cap & IEEE80211_HT_CAP_SGI_20)) 1651 continue; 1652 } 1653 } 1654 1655 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH && 1656 sta->deflink.bandwidth < IEEE80211_STA_RX_BW_40) 1657 continue; 1658 1659 nss = minstrel_mcs_groups[i].streams; 1660 1661 /* Mark MCS > 7 as unsupported if STA is in static SMPS mode */ 1662 if (sta->smps_mode == IEEE80211_SMPS_STATIC && nss > 1) 1663 continue; 1664 1665 /* HT rate */ 1666 if (gflags & IEEE80211_TX_RC_MCS) { 1667 if (use_vht && minstrel_vht_only) 1668 continue; 1669 1670 mi->supported[i] = mcs->rx_mask[nss - 1]; 1671 if (mi->supported[i]) 1672 n_supported++; 1673 continue; 1674 } 1675 1676 /* VHT rate */ 1677 if (!vht_cap->vht_supported || 1678 WARN_ON(!(gflags & IEEE80211_TX_RC_VHT_MCS)) || 1679 WARN_ON(gflags & IEEE80211_TX_RC_160_MHZ_WIDTH)) 1680 continue; 1681 1682 if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) { 1683 if (sta->deflink.bandwidth < IEEE80211_STA_RX_BW_80 || 1684 ((gflags & IEEE80211_TX_RC_SHORT_GI) && 1685 !(vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80))) { 1686 continue; 1687 } 1688 } 1689 1690 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH) 1691 bw = BW_40; 1692 else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH) 1693 bw = BW_80; 1694 else 1695 bw = BW_20; 1696 1697 mi->supported[i] = minstrel_get_valid_vht_rates(bw, nss, 1698 vht_cap->vht_mcs.tx_mcs_map); 1699 1700 if (mi->supported[i]) 1701 n_supported++; 1702 } 1703 1704 minstrel_ht_update_cck(mp, mi, sband, sta); 1705 minstrel_ht_update_ofdm(mp, mi, sband, sta); 1706 1707 /* create an initial rate table with the lowest supported rates */ 1708 minstrel_ht_update_stats(mp, mi); 1709 minstrel_ht_update_rates(mp, mi); 1710 } 1711 1712 static void 1713 minstrel_ht_rate_init(void *priv, struct ieee80211_supported_band *sband, 1714 struct cfg80211_chan_def *chandef, 1715 struct ieee80211_sta *sta, void *priv_sta) 1716 { 1717 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta); 1718 } 1719 1720 static void 1721 minstrel_ht_rate_update(void *priv, struct ieee80211_supported_band *sband, 1722 struct cfg80211_chan_def *chandef, 1723 struct ieee80211_sta *sta, void *priv_sta, 1724 u32 changed) 1725 { 1726 minstrel_ht_update_caps(priv, sband, chandef, sta, priv_sta); 1727 } 1728 1729 static void * 1730 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp) 1731 { 1732 struct ieee80211_supported_band *sband; 1733 struct minstrel_ht_sta *mi; 1734 struct minstrel_priv *mp = priv; 1735 struct ieee80211_hw *hw = mp->hw; 1736 int max_rates = 0; 1737 int i; 1738 1739 for (i = 0; i < NUM_NL80211_BANDS; i++) { 1740 sband = hw->wiphy->bands[i]; 1741 if (sband && sband->n_bitrates > max_rates) 1742 max_rates = sband->n_bitrates; 1743 } 1744 1745 return kzalloc(sizeof(*mi), gfp); 1746 } 1747 1748 static void 1749 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta) 1750 { 1751 kfree(priv_sta); 1752 } 1753 1754 static void 1755 minstrel_ht_fill_rate_array(u8 *dest, struct ieee80211_supported_band *sband, 1756 const s16 *bitrates, int n_rates, u32 rate_flags) 1757 { 1758 int i, j; 1759 1760 for (i = 0; i < sband->n_bitrates; i++) { 1761 struct ieee80211_rate *rate = &sband->bitrates[i]; 1762 1763 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 1764 continue; 1765 1766 for (j = 0; j < n_rates; j++) { 1767 if (rate->bitrate != bitrates[j]) 1768 continue; 1769 1770 dest[j] = i; 1771 break; 1772 } 1773 } 1774 } 1775 1776 static void 1777 minstrel_ht_init_cck_rates(struct minstrel_priv *mp) 1778 { 1779 static const s16 bitrates[4] = { 10, 20, 55, 110 }; 1780 struct ieee80211_supported_band *sband; 1781 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef); 1782 1783 memset(mp->cck_rates, 0xff, sizeof(mp->cck_rates)); 1784 sband = mp->hw->wiphy->bands[NL80211_BAND_2GHZ]; 1785 if (!sband) 1786 return; 1787 1788 BUILD_BUG_ON(ARRAY_SIZE(mp->cck_rates) != ARRAY_SIZE(bitrates)); 1789 minstrel_ht_fill_rate_array(mp->cck_rates, sband, 1790 minstrel_cck_bitrates, 1791 ARRAY_SIZE(minstrel_cck_bitrates), 1792 rate_flags); 1793 } 1794 1795 static void 1796 minstrel_ht_init_ofdm_rates(struct minstrel_priv *mp, enum nl80211_band band) 1797 { 1798 static const s16 bitrates[8] = { 60, 90, 120, 180, 240, 360, 480, 540 }; 1799 struct ieee80211_supported_band *sband; 1800 u32 rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef); 1801 1802 memset(mp->ofdm_rates[band], 0xff, sizeof(mp->ofdm_rates[band])); 1803 sband = mp->hw->wiphy->bands[band]; 1804 if (!sband) 1805 return; 1806 1807 BUILD_BUG_ON(ARRAY_SIZE(mp->ofdm_rates[band]) != ARRAY_SIZE(bitrates)); 1808 minstrel_ht_fill_rate_array(mp->ofdm_rates[band], sband, 1809 minstrel_ofdm_bitrates, 1810 ARRAY_SIZE(minstrel_ofdm_bitrates), 1811 rate_flags); 1812 } 1813 1814 static void * 1815 minstrel_ht_alloc(struct ieee80211_hw *hw) 1816 { 1817 struct minstrel_priv *mp; 1818 int i; 1819 1820 mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC); 1821 if (!mp) 1822 return NULL; 1823 1824 /* contention window settings 1825 * Just an approximation. Using the per-queue values would complicate 1826 * the calculations and is probably unnecessary */ 1827 mp->cw_min = 15; 1828 mp->cw_max = 1023; 1829 1830 /* maximum time that the hw is allowed to stay in one MRR segment */ 1831 mp->segment_size = 6000; 1832 1833 if (hw->max_rate_tries > 0) 1834 mp->max_retry = hw->max_rate_tries; 1835 else 1836 /* safe default, does not necessarily have to match hw properties */ 1837 mp->max_retry = 7; 1838 1839 if (hw->max_rates >= 4) 1840 mp->has_mrr = true; 1841 1842 mp->hw = hw; 1843 mp->update_interval = HZ / 20; 1844 1845 minstrel_ht_init_cck_rates(mp); 1846 for (i = 0; i < ARRAY_SIZE(mp->hw->wiphy->bands); i++) 1847 minstrel_ht_init_ofdm_rates(mp, i); 1848 1849 return mp; 1850 } 1851 1852 #ifdef CONFIG_MAC80211_DEBUGFS 1853 static void minstrel_ht_add_debugfs(struct ieee80211_hw *hw, void *priv, 1854 struct dentry *debugfsdir) 1855 { 1856 struct minstrel_priv *mp = priv; 1857 1858 mp->fixed_rate_idx = (u32) -1; 1859 debugfs_create_u32("fixed_rate_idx", S_IRUGO | S_IWUGO, debugfsdir, 1860 &mp->fixed_rate_idx); 1861 } 1862 #endif 1863 1864 static void 1865 minstrel_ht_free(void *priv) 1866 { 1867 kfree(priv); 1868 } 1869 1870 static u32 minstrel_ht_get_expected_throughput(void *priv_sta) 1871 { 1872 struct minstrel_ht_sta *mi = priv_sta; 1873 int i, j, prob, tp_avg; 1874 1875 i = MI_RATE_GROUP(mi->max_tp_rate[0]); 1876 j = MI_RATE_IDX(mi->max_tp_rate[0]); 1877 prob = mi->groups[i].rates[j].prob_avg; 1878 1879 /* convert tp_avg from pkt per second in kbps */ 1880 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * 10; 1881 tp_avg = tp_avg * AVG_PKT_SIZE * 8 / 1024; 1882 1883 return tp_avg; 1884 } 1885 1886 static const struct rate_control_ops mac80211_minstrel_ht = { 1887 .name = "minstrel_ht", 1888 .capa = RATE_CTRL_CAPA_AMPDU_TRIGGER, 1889 .tx_status_ext = minstrel_ht_tx_status, 1890 .get_rate = minstrel_ht_get_rate, 1891 .rate_init = minstrel_ht_rate_init, 1892 .rate_update = minstrel_ht_rate_update, 1893 .alloc_sta = minstrel_ht_alloc_sta, 1894 .free_sta = minstrel_ht_free_sta, 1895 .alloc = minstrel_ht_alloc, 1896 .free = minstrel_ht_free, 1897 #ifdef CONFIG_MAC80211_DEBUGFS 1898 .add_debugfs = minstrel_ht_add_debugfs, 1899 .add_sta_debugfs = minstrel_ht_add_sta_debugfs, 1900 #endif 1901 .get_expected_throughput = minstrel_ht_get_expected_throughput, 1902 }; 1903 1904 1905 static void __init init_sample_table(void) 1906 { 1907 int col, i, new_idx; 1908 u8 rnd[MCS_GROUP_RATES]; 1909 1910 memset(sample_table, 0xff, sizeof(sample_table)); 1911 for (col = 0; col < SAMPLE_COLUMNS; col++) { 1912 prandom_bytes(rnd, sizeof(rnd)); 1913 for (i = 0; i < MCS_GROUP_RATES; i++) { 1914 new_idx = (i + rnd[i]) % MCS_GROUP_RATES; 1915 while (sample_table[col][new_idx] != 0xff) 1916 new_idx = (new_idx + 1) % MCS_GROUP_RATES; 1917 1918 sample_table[col][new_idx] = i; 1919 } 1920 } 1921 } 1922 1923 int __init 1924 rc80211_minstrel_init(void) 1925 { 1926 init_sample_table(); 1927 return ieee80211_rate_control_register(&mac80211_minstrel_ht); 1928 } 1929 1930 void 1931 rc80211_minstrel_exit(void) 1932 { 1933 ieee80211_rate_control_unregister(&mac80211_minstrel_ht); 1934 } 1935