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