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