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