1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (C) 2019 Felix Fietkau <nbd@nbd.name> 4 * Copyright (C) 2021-2022 Intel Corporation 5 */ 6 7 #include <net/mac80211.h> 8 #include "ieee80211_i.h" 9 #include "sta_info.h" 10 11 #define AVG_PKT_SIZE 1024 12 13 /* Number of bits for an average sized packet */ 14 #define MCS_NBITS (AVG_PKT_SIZE << 3) 15 16 /* Number of kilo-symbols (symbols * 1024) for a packet with (bps) bits per 17 * symbol. We use k-symbols to avoid rounding in the _TIME macros below. 18 */ 19 #define MCS_N_KSYMS(bps) DIV_ROUND_UP(MCS_NBITS << 10, (bps)) 20 21 /* Transmission time (in 1024 * usec) for a packet containing (ksyms) * 1024 22 * symbols. 23 */ 24 #define MCS_SYMBOL_TIME(sgi, ksyms) \ 25 (sgi ? \ 26 ((ksyms) * 4 * 18) / 20 : /* 3.6 us per sym */ \ 27 ((ksyms) * 4) /* 4.0 us per sym */ \ 28 ) 29 30 /* Transmit duration for the raw data part of an average sized packet */ 31 #define MCS_DURATION(streams, sgi, bps) \ 32 ((u32)MCS_SYMBOL_TIME(sgi, MCS_N_KSYMS((streams) * (bps)))) 33 34 #define MCS_DURATION_S(shift, streams, sgi, bps) \ 35 ((u16)((MCS_DURATION(streams, sgi, bps) >> shift))) 36 37 /* These should match the values in enum nl80211_he_gi */ 38 #define HE_GI_08 0 39 #define HE_GI_16 1 40 #define HE_GI_32 2 41 42 /* Transmission time (1024 usec) for a packet containing (ksyms) * k-symbols */ 43 #define HE_SYMBOL_TIME(gi, ksyms) \ 44 (gi == HE_GI_08 ? \ 45 ((ksyms) * 16 * 17) / 20 : /* 13.6 us per sym */ \ 46 (gi == HE_GI_16 ? \ 47 ((ksyms) * 16 * 18) / 20 : /* 14.4 us per sym */ \ 48 ((ksyms) * 16) /* 16.0 us per sym */ \ 49 )) 50 51 /* Transmit duration for the raw data part of an average sized packet */ 52 #define HE_DURATION(streams, gi, bps) \ 53 ((u32)HE_SYMBOL_TIME(gi, MCS_N_KSYMS((streams) * (bps)))) 54 55 #define HE_DURATION_S(shift, streams, gi, bps) \ 56 (HE_DURATION(streams, gi, bps) >> shift) 57 58 #define BW_20 0 59 #define BW_40 1 60 #define BW_80 2 61 #define BW_160 3 62 63 /* 64 * Define group sort order: HT40 -> SGI -> #streams 65 */ 66 #define IEEE80211_MAX_STREAMS 4 67 #define IEEE80211_HT_STREAM_GROUPS 4 /* BW(=2) * SGI(=2) */ 68 #define IEEE80211_VHT_STREAM_GROUPS 8 /* BW(=4) * SGI(=2) */ 69 70 #define IEEE80211_HE_MAX_STREAMS 8 71 72 #define IEEE80211_HT_GROUPS_NB (IEEE80211_MAX_STREAMS * \ 73 IEEE80211_HT_STREAM_GROUPS) 74 #define IEEE80211_VHT_GROUPS_NB (IEEE80211_MAX_STREAMS * \ 75 IEEE80211_VHT_STREAM_GROUPS) 76 77 #define IEEE80211_HT_GROUP_0 0 78 #define IEEE80211_VHT_GROUP_0 (IEEE80211_HT_GROUP_0 + IEEE80211_HT_GROUPS_NB) 79 #define IEEE80211_HE_GROUP_0 (IEEE80211_VHT_GROUP_0 + IEEE80211_VHT_GROUPS_NB) 80 81 #define MCS_GROUP_RATES 12 82 83 #define HT_GROUP_IDX(_streams, _sgi, _ht40) \ 84 IEEE80211_HT_GROUP_0 + \ 85 IEEE80211_MAX_STREAMS * 2 * _ht40 + \ 86 IEEE80211_MAX_STREAMS * _sgi + \ 87 _streams - 1 88 89 #define _MAX(a, b) (((a)>(b))?(a):(b)) 90 91 #define GROUP_SHIFT(duration) \ 92 _MAX(0, 16 - __builtin_clz(duration)) 93 94 /* MCS rate information for an MCS group */ 95 #define __MCS_GROUP(_streams, _sgi, _ht40, _s) \ 96 [HT_GROUP_IDX(_streams, _sgi, _ht40)] = { \ 97 .shift = _s, \ 98 .duration = { \ 99 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 54 : 26), \ 100 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 108 : 52), \ 101 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 162 : 78), \ 102 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 216 : 104), \ 103 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 324 : 156), \ 104 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 432 : 208), \ 105 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 486 : 234), \ 106 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 540 : 260) \ 107 } \ 108 } 109 110 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \ 111 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26)) 112 113 #define MCS_GROUP(_streams, _sgi, _ht40) \ 114 __MCS_GROUP(_streams, _sgi, _ht40, \ 115 MCS_GROUP_SHIFT(_streams, _sgi, _ht40)) 116 117 #define VHT_GROUP_IDX(_streams, _sgi, _bw) \ 118 (IEEE80211_VHT_GROUP_0 + \ 119 IEEE80211_MAX_STREAMS * 2 * (_bw) + \ 120 IEEE80211_MAX_STREAMS * (_sgi) + \ 121 (_streams) - 1) 122 123 #define BW2VBPS(_bw, r4, r3, r2, r1) \ 124 (_bw == BW_160 ? r4 : _bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1) 125 126 #define __VHT_GROUP(_streams, _sgi, _bw, _s) \ 127 [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \ 128 .shift = _s, \ 129 .duration = { \ 130 MCS_DURATION_S(_s, _streams, _sgi, \ 131 BW2VBPS(_bw, 234, 117, 54, 26)), \ 132 MCS_DURATION_S(_s, _streams, _sgi, \ 133 BW2VBPS(_bw, 468, 234, 108, 52)), \ 134 MCS_DURATION_S(_s, _streams, _sgi, \ 135 BW2VBPS(_bw, 702, 351, 162, 78)), \ 136 MCS_DURATION_S(_s, _streams, _sgi, \ 137 BW2VBPS(_bw, 936, 468, 216, 104)), \ 138 MCS_DURATION_S(_s, _streams, _sgi, \ 139 BW2VBPS(_bw, 1404, 702, 324, 156)), \ 140 MCS_DURATION_S(_s, _streams, _sgi, \ 141 BW2VBPS(_bw, 1872, 936, 432, 208)), \ 142 MCS_DURATION_S(_s, _streams, _sgi, \ 143 BW2VBPS(_bw, 2106, 1053, 486, 234)), \ 144 MCS_DURATION_S(_s, _streams, _sgi, \ 145 BW2VBPS(_bw, 2340, 1170, 540, 260)), \ 146 MCS_DURATION_S(_s, _streams, _sgi, \ 147 BW2VBPS(_bw, 2808, 1404, 648, 312)), \ 148 MCS_DURATION_S(_s, _streams, _sgi, \ 149 BW2VBPS(_bw, 3120, 1560, 720, 346)) \ 150 } \ 151 } 152 153 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \ 154 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \ 155 BW2VBPS(_bw, 243, 117, 54, 26))) 156 157 #define VHT_GROUP(_streams, _sgi, _bw) \ 158 __VHT_GROUP(_streams, _sgi, _bw, \ 159 VHT_GROUP_SHIFT(_streams, _sgi, _bw)) 160 161 162 #define HE_GROUP_IDX(_streams, _gi, _bw) \ 163 (IEEE80211_HE_GROUP_0 + \ 164 IEEE80211_HE_MAX_STREAMS * 3 * (_bw) + \ 165 IEEE80211_HE_MAX_STREAMS * (_gi) + \ 166 (_streams) - 1) 167 168 #define __HE_GROUP(_streams, _gi, _bw, _s) \ 169 [HE_GROUP_IDX(_streams, _gi, _bw)] = { \ 170 .shift = _s, \ 171 .duration = { \ 172 HE_DURATION_S(_s, _streams, _gi, \ 173 BW2VBPS(_bw, 979, 489, 230, 115)), \ 174 HE_DURATION_S(_s, _streams, _gi, \ 175 BW2VBPS(_bw, 1958, 979, 475, 230)), \ 176 HE_DURATION_S(_s, _streams, _gi, \ 177 BW2VBPS(_bw, 2937, 1468, 705, 345)), \ 178 HE_DURATION_S(_s, _streams, _gi, \ 179 BW2VBPS(_bw, 3916, 1958, 936, 475)), \ 180 HE_DURATION_S(_s, _streams, _gi, \ 181 BW2VBPS(_bw, 5875, 2937, 1411, 705)), \ 182 HE_DURATION_S(_s, _streams, _gi, \ 183 BW2VBPS(_bw, 7833, 3916, 1872, 936)), \ 184 HE_DURATION_S(_s, _streams, _gi, \ 185 BW2VBPS(_bw, 8827, 4406, 2102, 1051)), \ 186 HE_DURATION_S(_s, _streams, _gi, \ 187 BW2VBPS(_bw, 9806, 4896, 2347, 1166)), \ 188 HE_DURATION_S(_s, _streams, _gi, \ 189 BW2VBPS(_bw, 11764, 5875, 2808, 1411)), \ 190 HE_DURATION_S(_s, _streams, _gi, \ 191 BW2VBPS(_bw, 13060, 6523, 3124, 1555)), \ 192 HE_DURATION_S(_s, _streams, _gi, \ 193 BW2VBPS(_bw, 14702, 7344, 3513, 1756)), \ 194 HE_DURATION_S(_s, _streams, _gi, \ 195 BW2VBPS(_bw, 16329, 8164, 3902, 1944)) \ 196 } \ 197 } 198 199 #define HE_GROUP_SHIFT(_streams, _gi, _bw) \ 200 GROUP_SHIFT(HE_DURATION(_streams, _gi, \ 201 BW2VBPS(_bw, 979, 489, 230, 115))) 202 203 #define HE_GROUP(_streams, _gi, _bw) \ 204 __HE_GROUP(_streams, _gi, _bw, \ 205 HE_GROUP_SHIFT(_streams, _gi, _bw)) 206 struct mcs_group { 207 u8 shift; 208 u16 duration[MCS_GROUP_RATES]; 209 }; 210 211 static const struct mcs_group airtime_mcs_groups[] = { 212 MCS_GROUP(1, 0, BW_20), 213 MCS_GROUP(2, 0, BW_20), 214 MCS_GROUP(3, 0, BW_20), 215 MCS_GROUP(4, 0, BW_20), 216 217 MCS_GROUP(1, 1, BW_20), 218 MCS_GROUP(2, 1, BW_20), 219 MCS_GROUP(3, 1, BW_20), 220 MCS_GROUP(4, 1, BW_20), 221 222 MCS_GROUP(1, 0, BW_40), 223 MCS_GROUP(2, 0, BW_40), 224 MCS_GROUP(3, 0, BW_40), 225 MCS_GROUP(4, 0, BW_40), 226 227 MCS_GROUP(1, 1, BW_40), 228 MCS_GROUP(2, 1, BW_40), 229 MCS_GROUP(3, 1, BW_40), 230 MCS_GROUP(4, 1, BW_40), 231 232 VHT_GROUP(1, 0, BW_20), 233 VHT_GROUP(2, 0, BW_20), 234 VHT_GROUP(3, 0, BW_20), 235 VHT_GROUP(4, 0, BW_20), 236 237 VHT_GROUP(1, 1, BW_20), 238 VHT_GROUP(2, 1, BW_20), 239 VHT_GROUP(3, 1, BW_20), 240 VHT_GROUP(4, 1, BW_20), 241 242 VHT_GROUP(1, 0, BW_40), 243 VHT_GROUP(2, 0, BW_40), 244 VHT_GROUP(3, 0, BW_40), 245 VHT_GROUP(4, 0, BW_40), 246 247 VHT_GROUP(1, 1, BW_40), 248 VHT_GROUP(2, 1, BW_40), 249 VHT_GROUP(3, 1, BW_40), 250 VHT_GROUP(4, 1, BW_40), 251 252 VHT_GROUP(1, 0, BW_80), 253 VHT_GROUP(2, 0, BW_80), 254 VHT_GROUP(3, 0, BW_80), 255 VHT_GROUP(4, 0, BW_80), 256 257 VHT_GROUP(1, 1, BW_80), 258 VHT_GROUP(2, 1, BW_80), 259 VHT_GROUP(3, 1, BW_80), 260 VHT_GROUP(4, 1, BW_80), 261 262 VHT_GROUP(1, 0, BW_160), 263 VHT_GROUP(2, 0, BW_160), 264 VHT_GROUP(3, 0, BW_160), 265 VHT_GROUP(4, 0, BW_160), 266 267 VHT_GROUP(1, 1, BW_160), 268 VHT_GROUP(2, 1, BW_160), 269 VHT_GROUP(3, 1, BW_160), 270 VHT_GROUP(4, 1, BW_160), 271 272 HE_GROUP(1, HE_GI_08, BW_20), 273 HE_GROUP(2, HE_GI_08, BW_20), 274 HE_GROUP(3, HE_GI_08, BW_20), 275 HE_GROUP(4, HE_GI_08, BW_20), 276 HE_GROUP(5, HE_GI_08, BW_20), 277 HE_GROUP(6, HE_GI_08, BW_20), 278 HE_GROUP(7, HE_GI_08, BW_20), 279 HE_GROUP(8, HE_GI_08, BW_20), 280 281 HE_GROUP(1, HE_GI_16, BW_20), 282 HE_GROUP(2, HE_GI_16, BW_20), 283 HE_GROUP(3, HE_GI_16, BW_20), 284 HE_GROUP(4, HE_GI_16, BW_20), 285 HE_GROUP(5, HE_GI_16, BW_20), 286 HE_GROUP(6, HE_GI_16, BW_20), 287 HE_GROUP(7, HE_GI_16, BW_20), 288 HE_GROUP(8, HE_GI_16, BW_20), 289 290 HE_GROUP(1, HE_GI_32, BW_20), 291 HE_GROUP(2, HE_GI_32, BW_20), 292 HE_GROUP(3, HE_GI_32, BW_20), 293 HE_GROUP(4, HE_GI_32, BW_20), 294 HE_GROUP(5, HE_GI_32, BW_20), 295 HE_GROUP(6, HE_GI_32, BW_20), 296 HE_GROUP(7, HE_GI_32, BW_20), 297 HE_GROUP(8, HE_GI_32, BW_20), 298 299 HE_GROUP(1, HE_GI_08, BW_40), 300 HE_GROUP(2, HE_GI_08, BW_40), 301 HE_GROUP(3, HE_GI_08, BW_40), 302 HE_GROUP(4, HE_GI_08, BW_40), 303 HE_GROUP(5, HE_GI_08, BW_40), 304 HE_GROUP(6, HE_GI_08, BW_40), 305 HE_GROUP(7, HE_GI_08, BW_40), 306 HE_GROUP(8, HE_GI_08, BW_40), 307 308 HE_GROUP(1, HE_GI_16, BW_40), 309 HE_GROUP(2, HE_GI_16, BW_40), 310 HE_GROUP(3, HE_GI_16, BW_40), 311 HE_GROUP(4, HE_GI_16, BW_40), 312 HE_GROUP(5, HE_GI_16, BW_40), 313 HE_GROUP(6, HE_GI_16, BW_40), 314 HE_GROUP(7, HE_GI_16, BW_40), 315 HE_GROUP(8, HE_GI_16, BW_40), 316 317 HE_GROUP(1, HE_GI_32, BW_40), 318 HE_GROUP(2, HE_GI_32, BW_40), 319 HE_GROUP(3, HE_GI_32, BW_40), 320 HE_GROUP(4, HE_GI_32, BW_40), 321 HE_GROUP(5, HE_GI_32, BW_40), 322 HE_GROUP(6, HE_GI_32, BW_40), 323 HE_GROUP(7, HE_GI_32, BW_40), 324 HE_GROUP(8, HE_GI_32, BW_40), 325 326 HE_GROUP(1, HE_GI_08, BW_80), 327 HE_GROUP(2, HE_GI_08, BW_80), 328 HE_GROUP(3, HE_GI_08, BW_80), 329 HE_GROUP(4, HE_GI_08, BW_80), 330 HE_GROUP(5, HE_GI_08, BW_80), 331 HE_GROUP(6, HE_GI_08, BW_80), 332 HE_GROUP(7, HE_GI_08, BW_80), 333 HE_GROUP(8, HE_GI_08, BW_80), 334 335 HE_GROUP(1, HE_GI_16, BW_80), 336 HE_GROUP(2, HE_GI_16, BW_80), 337 HE_GROUP(3, HE_GI_16, BW_80), 338 HE_GROUP(4, HE_GI_16, BW_80), 339 HE_GROUP(5, HE_GI_16, BW_80), 340 HE_GROUP(6, HE_GI_16, BW_80), 341 HE_GROUP(7, HE_GI_16, BW_80), 342 HE_GROUP(8, HE_GI_16, BW_80), 343 344 HE_GROUP(1, HE_GI_32, BW_80), 345 HE_GROUP(2, HE_GI_32, BW_80), 346 HE_GROUP(3, HE_GI_32, BW_80), 347 HE_GROUP(4, HE_GI_32, BW_80), 348 HE_GROUP(5, HE_GI_32, BW_80), 349 HE_GROUP(6, HE_GI_32, BW_80), 350 HE_GROUP(7, HE_GI_32, BW_80), 351 HE_GROUP(8, HE_GI_32, BW_80), 352 353 HE_GROUP(1, HE_GI_08, BW_160), 354 HE_GROUP(2, HE_GI_08, BW_160), 355 HE_GROUP(3, HE_GI_08, BW_160), 356 HE_GROUP(4, HE_GI_08, BW_160), 357 HE_GROUP(5, HE_GI_08, BW_160), 358 HE_GROUP(6, HE_GI_08, BW_160), 359 HE_GROUP(7, HE_GI_08, BW_160), 360 HE_GROUP(8, HE_GI_08, BW_160), 361 362 HE_GROUP(1, HE_GI_16, BW_160), 363 HE_GROUP(2, HE_GI_16, BW_160), 364 HE_GROUP(3, HE_GI_16, BW_160), 365 HE_GROUP(4, HE_GI_16, BW_160), 366 HE_GROUP(5, HE_GI_16, BW_160), 367 HE_GROUP(6, HE_GI_16, BW_160), 368 HE_GROUP(7, HE_GI_16, BW_160), 369 HE_GROUP(8, HE_GI_16, BW_160), 370 371 HE_GROUP(1, HE_GI_32, BW_160), 372 HE_GROUP(2, HE_GI_32, BW_160), 373 HE_GROUP(3, HE_GI_32, BW_160), 374 HE_GROUP(4, HE_GI_32, BW_160), 375 HE_GROUP(5, HE_GI_32, BW_160), 376 HE_GROUP(6, HE_GI_32, BW_160), 377 HE_GROUP(7, HE_GI_32, BW_160), 378 HE_GROUP(8, HE_GI_32, BW_160), 379 }; 380 381 static u32 382 ieee80211_calc_legacy_rate_duration(u16 bitrate, bool short_pre, 383 bool cck, int len) 384 { 385 u32 duration; 386 387 if (cck) { 388 duration = 144 + 48; /* preamble + PLCP */ 389 if (short_pre) 390 duration >>= 1; 391 392 duration += 10; /* SIFS */ 393 } else { 394 duration = 20 + 16; /* premable + SIFS */ 395 } 396 397 len <<= 3; 398 duration += (len * 10) / bitrate; 399 400 return duration; 401 } 402 403 static u32 ieee80211_get_rate_duration(struct ieee80211_hw *hw, 404 struct ieee80211_rx_status *status, 405 u32 *overhead) 406 { 407 bool sgi = status->enc_flags & RX_ENC_FLAG_SHORT_GI; 408 int bw, streams; 409 int group, idx; 410 u32 duration; 411 412 switch (status->bw) { 413 case RATE_INFO_BW_20: 414 bw = BW_20; 415 break; 416 case RATE_INFO_BW_40: 417 bw = BW_40; 418 break; 419 case RATE_INFO_BW_80: 420 bw = BW_80; 421 break; 422 case RATE_INFO_BW_160: 423 bw = BW_160; 424 break; 425 default: 426 WARN_ON_ONCE(1); 427 return 0; 428 } 429 430 switch (status->encoding) { 431 case RX_ENC_VHT: 432 streams = status->nss; 433 idx = status->rate_idx; 434 group = VHT_GROUP_IDX(streams, sgi, bw); 435 break; 436 case RX_ENC_HT: 437 streams = ((status->rate_idx >> 3) & 3) + 1; 438 idx = status->rate_idx & 7; 439 group = HT_GROUP_IDX(streams, sgi, bw); 440 break; 441 case RX_ENC_HE: 442 streams = status->nss; 443 idx = status->rate_idx; 444 group = HE_GROUP_IDX(streams, status->he_gi, bw); 445 break; 446 default: 447 WARN_ON_ONCE(1); 448 return 0; 449 } 450 451 if (WARN_ON_ONCE((status->encoding != RX_ENC_HE && streams > 4) || 452 (status->encoding == RX_ENC_HE && streams > 8))) 453 return 0; 454 455 if (idx >= MCS_GROUP_RATES) 456 return 0; 457 458 duration = airtime_mcs_groups[group].duration[idx]; 459 duration <<= airtime_mcs_groups[group].shift; 460 *overhead = 36 + (streams << 2); 461 462 return duration; 463 } 464 465 466 u32 ieee80211_calc_rx_airtime(struct ieee80211_hw *hw, 467 struct ieee80211_rx_status *status, 468 int len) 469 { 470 struct ieee80211_supported_band *sband; 471 u32 duration, overhead = 0; 472 473 if (status->encoding == RX_ENC_LEGACY) { 474 const struct ieee80211_rate *rate; 475 bool sp = status->enc_flags & RX_ENC_FLAG_SHORTPRE; 476 bool cck; 477 478 /* on 60GHz or sub-1GHz band, there are no legacy rates */ 479 if (WARN_ON_ONCE(status->band == NL80211_BAND_60GHZ || 480 status->band == NL80211_BAND_S1GHZ)) 481 return 0; 482 483 sband = hw->wiphy->bands[status->band]; 484 if (!sband || status->rate_idx >= sband->n_bitrates) 485 return 0; 486 487 rate = &sband->bitrates[status->rate_idx]; 488 cck = rate->flags & IEEE80211_RATE_MANDATORY_B; 489 490 return ieee80211_calc_legacy_rate_duration(rate->bitrate, sp, 491 cck, len); 492 } 493 494 duration = ieee80211_get_rate_duration(hw, status, &overhead); 495 if (!duration) 496 return 0; 497 498 duration *= len; 499 duration /= AVG_PKT_SIZE; 500 duration /= 1024; 501 502 return duration + overhead; 503 } 504 EXPORT_SYMBOL_GPL(ieee80211_calc_rx_airtime); 505 506 static bool ieee80211_fill_rate_info(struct ieee80211_hw *hw, 507 struct ieee80211_rx_status *stat, u8 band, 508 struct rate_info *ri) 509 { 510 struct ieee80211_supported_band *sband = hw->wiphy->bands[band]; 511 int i; 512 513 if (!ri || !sband) 514 return false; 515 516 stat->bw = ri->bw; 517 stat->nss = ri->nss; 518 stat->rate_idx = ri->mcs; 519 520 if (ri->flags & RATE_INFO_FLAGS_HE_MCS) 521 stat->encoding = RX_ENC_HE; 522 else if (ri->flags & RATE_INFO_FLAGS_VHT_MCS) 523 stat->encoding = RX_ENC_VHT; 524 else if (ri->flags & RATE_INFO_FLAGS_MCS) 525 stat->encoding = RX_ENC_HT; 526 else 527 stat->encoding = RX_ENC_LEGACY; 528 529 if (ri->flags & RATE_INFO_FLAGS_SHORT_GI) 530 stat->enc_flags |= RX_ENC_FLAG_SHORT_GI; 531 532 stat->he_gi = ri->he_gi; 533 534 if (stat->encoding != RX_ENC_LEGACY) 535 return true; 536 537 stat->rate_idx = 0; 538 for (i = 0; i < sband->n_bitrates; i++) { 539 if (ri->legacy != sband->bitrates[i].bitrate) 540 continue; 541 542 stat->rate_idx = i; 543 return true; 544 } 545 546 return false; 547 } 548 549 static int ieee80211_fill_rx_status(struct ieee80211_rx_status *stat, 550 struct ieee80211_hw *hw, 551 struct ieee80211_tx_rate *rate, 552 struct rate_info *ri, u8 band, int len) 553 { 554 memset(stat, 0, sizeof(*stat)); 555 stat->band = band; 556 557 if (ieee80211_fill_rate_info(hw, stat, band, ri)) 558 return 0; 559 560 if (!ieee80211_rate_valid(rate)) 561 return -1; 562 563 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH) 564 stat->bw = RATE_INFO_BW_160; 565 else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH) 566 stat->bw = RATE_INFO_BW_80; 567 else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) 568 stat->bw = RATE_INFO_BW_40; 569 else 570 stat->bw = RATE_INFO_BW_20; 571 572 stat->enc_flags = 0; 573 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) 574 stat->enc_flags |= RX_ENC_FLAG_SHORTPRE; 575 if (rate->flags & IEEE80211_TX_RC_SHORT_GI) 576 stat->enc_flags |= RX_ENC_FLAG_SHORT_GI; 577 578 stat->rate_idx = rate->idx; 579 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) { 580 stat->encoding = RX_ENC_VHT; 581 stat->rate_idx = ieee80211_rate_get_vht_mcs(rate); 582 stat->nss = ieee80211_rate_get_vht_nss(rate); 583 } else if (rate->flags & IEEE80211_TX_RC_MCS) { 584 stat->encoding = RX_ENC_HT; 585 } else { 586 stat->encoding = RX_ENC_LEGACY; 587 } 588 589 return 0; 590 } 591 592 static u32 ieee80211_calc_tx_airtime_rate(struct ieee80211_hw *hw, 593 struct ieee80211_tx_rate *rate, 594 struct rate_info *ri, 595 u8 band, int len) 596 { 597 struct ieee80211_rx_status stat; 598 599 if (ieee80211_fill_rx_status(&stat, hw, rate, ri, band, len)) 600 return 0; 601 602 return ieee80211_calc_rx_airtime(hw, &stat, len); 603 } 604 605 u32 ieee80211_calc_tx_airtime(struct ieee80211_hw *hw, 606 struct ieee80211_tx_info *info, 607 int len) 608 { 609 u32 duration = 0; 610 int i; 611 612 for (i = 0; i < ARRAY_SIZE(info->status.rates); i++) { 613 struct ieee80211_tx_rate *rate = &info->status.rates[i]; 614 u32 cur_duration; 615 616 cur_duration = ieee80211_calc_tx_airtime_rate(hw, rate, NULL, 617 info->band, len); 618 if (!cur_duration) 619 break; 620 621 duration += cur_duration * rate->count; 622 } 623 624 return duration; 625 } 626 EXPORT_SYMBOL_GPL(ieee80211_calc_tx_airtime); 627 628 u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw, 629 struct ieee80211_vif *vif, 630 struct ieee80211_sta *pubsta, 631 int len, bool ampdu) 632 { 633 struct ieee80211_supported_band *sband; 634 struct ieee80211_chanctx_conf *conf; 635 int rateidx; 636 bool cck, short_pream; 637 u32 basic_rates; 638 u8 band = 0; 639 u16 rate; 640 641 len += 38; /* Ethernet header length */ 642 643 conf = rcu_dereference(vif->bss_conf.chanctx_conf); 644 if (conf) 645 band = conf->def.chan->band; 646 647 if (pubsta) { 648 struct sta_info *sta = container_of(pubsta, struct sta_info, 649 sta); 650 struct ieee80211_rx_status stat; 651 struct ieee80211_tx_rate *tx_rate = &sta->deflink.tx_stats.last_rate; 652 struct rate_info *ri = &sta->deflink.tx_stats.last_rate_info; 653 u32 duration, overhead; 654 u8 agg_shift; 655 656 if (ieee80211_fill_rx_status(&stat, hw, tx_rate, ri, band, len)) 657 return 0; 658 659 if (stat.encoding == RX_ENC_LEGACY || !ampdu) 660 return ieee80211_calc_rx_airtime(hw, &stat, len); 661 662 duration = ieee80211_get_rate_duration(hw, &stat, &overhead); 663 /* 664 * Assume that HT/VHT transmission on any AC except VO will 665 * use aggregation. Since we don't have reliable reporting 666 * of aggregation length, assume an average size based on the 667 * tx rate. 668 * This will not be very accurate, but much better than simply 669 * assuming un-aggregated tx in all cases. 670 */ 671 if (duration > 400 * 1024) /* <= VHT20 MCS2 1S */ 672 agg_shift = 1; 673 else if (duration > 250 * 1024) /* <= VHT20 MCS3 1S or MCS1 2S */ 674 agg_shift = 2; 675 else if (duration > 150 * 1024) /* <= VHT20 MCS5 1S or MCS2 2S */ 676 agg_shift = 3; 677 else if (duration > 70 * 1024) /* <= VHT20 MCS5 2S */ 678 agg_shift = 4; 679 else if (stat.encoding != RX_ENC_HE || 680 duration > 20 * 1024) /* <= HE40 MCS6 2S */ 681 agg_shift = 5; 682 else 683 agg_shift = 6; 684 685 duration *= len; 686 duration /= AVG_PKT_SIZE; 687 duration /= 1024; 688 duration += (overhead >> agg_shift); 689 690 return max_t(u32, duration, 4); 691 } 692 693 if (!conf) 694 return 0; 695 696 /* No station to get latest rate from, so calculate the worst-case 697 * duration using the lowest configured basic rate. 698 */ 699 sband = hw->wiphy->bands[band]; 700 701 basic_rates = vif->bss_conf.basic_rates; 702 short_pream = vif->bss_conf.use_short_preamble; 703 704 rateidx = basic_rates ? ffs(basic_rates) - 1 : 0; 705 rate = sband->bitrates[rateidx].bitrate; 706 cck = sband->bitrates[rateidx].flags & IEEE80211_RATE_MANDATORY_B; 707 708 return ieee80211_calc_legacy_rate_duration(rate, short_pream, cck, len); 709 } 710