1 From: =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= <toke@redhat.com>
2 Date: Mon, 18 Nov 2019 22:06:08 -0800
3 Subject: [PATCH] mac80211: Import airtime calculation code from mt76
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
8 Felix recently added code to calculate airtime of packets to the mt76
9 driver. Import this into mac80211 so we can use it for airtime queue limit
12 The airtime.c file is copied verbatim from the mt76 driver, and adjusted to
13 be usable in mac80211. This involves:
15 - Switching to mac80211 data structures.
16 - Adding support for 160 MHz channels and HE mode.
17 - Moving the symbol and duration calculations around a bit to avoid
18 rounding with the higher rates and longer symbol times used for HE rates.
20 The per-rate TX rate calculation is also split out to its own function so
21 it can be used directly for the AQL calculations later.
23 Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
24 Link: https://lore.kernel.org/r/20191119060610.76681-3-kyan@google.com
25 [fix HE_GROUP_IDX() to use 3 * bw, since there are 3 _gi values]
26 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
28 create mode 100644 net/mac80211/airtime.c
30 --- a/include/net/mac80211.h
31 +++ b/include/net/mac80211.h
32 @@ -6417,4 +6417,33 @@ void ieee80211_nan_func_match(struct iee
33 struct cfg80211_nan_match_params *match,
37 + * ieee80211_calc_rx_airtime - calculate estimated transmission airtime for RX.
39 + * This function calculates the estimated airtime usage of a frame based on the
40 + * rate information in the RX status struct and the frame length.
42 + * @hw: pointer as obtained from ieee80211_alloc_hw()
43 + * @status: &struct ieee80211_rx_status containing the transmission rate
45 + * @len: frame length in bytes
47 +u32 ieee80211_calc_rx_airtime(struct ieee80211_hw *hw,
48 + struct ieee80211_rx_status *status,
52 + * ieee80211_calc_tx_airtime - calculate estimated transmission airtime for TX.
54 + * This function calculates the estimated airtime usage of a frame based on the
55 + * rate information in the TX info struct and the frame length.
57 + * @hw: pointer as obtained from ieee80211_alloc_hw()
58 + * @info: &struct ieee80211_tx_info of the frame.
59 + * @len: frame length in bytes
61 +u32 ieee80211_calc_tx_airtime(struct ieee80211_hw *hw,
62 + struct ieee80211_tx_info *info,
65 #endif /* MAC80211_H */
66 --- a/net/mac80211/Makefile
67 +++ b/net/mac80211/Makefile
68 @@ -31,7 +31,8 @@ mac80211-y := \
76 mac80211-$(CPTCFG_MAC80211_LEDS) += led.o
77 mac80211-$(CPTCFG_MAC80211_DEBUGFS) += \
79 +++ b/net/mac80211/airtime.c
81 +// SPDX-License-Identifier: ISC
83 + * Copyright (C) 2019 Felix Fietkau <nbd@nbd.name>
86 +#include <net/mac80211.h>
87 +#include "ieee80211_i.h"
88 +#include "sta_info.h"
90 +#define AVG_PKT_SIZE 1024
92 +/* Number of bits for an average sized packet */
93 +#define MCS_NBITS (AVG_PKT_SIZE << 3)
95 +/* Number of kilo-symbols (symbols * 1024) for a packet with (bps) bits per
96 + * symbol. We use k-symbols to avoid rounding in the _TIME macros below.
98 +#define MCS_N_KSYMS(bps) DIV_ROUND_UP(MCS_NBITS << 10, (bps))
100 +/* Transmission time (in 1024 * usec) for a packet containing (ksyms) * 1024
103 +#define MCS_SYMBOL_TIME(sgi, ksyms) \
105 + ((ksyms) * 4 * 18) / 20 : /* 3.6 us per sym */ \
106 + ((ksyms) * 4) /* 4.0 us per sym */ \
109 +/* Transmit duration for the raw data part of an average sized packet */
110 +#define MCS_DURATION(streams, sgi, bps) \
111 + ((u32)MCS_SYMBOL_TIME(sgi, MCS_N_KSYMS((streams) * (bps))))
113 +#define MCS_DURATION_S(shift, streams, sgi, bps) \
114 + ((u16)((MCS_DURATION(streams, sgi, bps) >> shift)))
116 +/* These should match the values in enum nl80211_he_gi */
121 +/* Transmission time (1024 usec) for a packet containing (ksyms) * k-symbols */
122 +#define HE_SYMBOL_TIME(gi, ksyms) \
123 + (gi == HE_GI_08 ? \
124 + ((ksyms) * 16 * 17) / 20 : /* 13.6 us per sym */ \
125 + (gi == HE_GI_16 ? \
126 + ((ksyms) * 16 * 18) / 20 : /* 14.4 us per sym */ \
127 + ((ksyms) * 16) /* 16.0 us per sym */ \
130 +/* Transmit duration for the raw data part of an average sized packet */
131 +#define HE_DURATION(streams, gi, bps) \
132 + ((u32)HE_SYMBOL_TIME(gi, MCS_N_KSYMS((streams) * (bps))))
134 +#define HE_DURATION_S(shift, streams, gi, bps) \
135 + (HE_DURATION(streams, gi, bps) >> shift)
143 + * Define group sort order: HT40 -> SGI -> #streams
145 +#define IEEE80211_MAX_STREAMS 4
146 +#define IEEE80211_HT_STREAM_GROUPS 4 /* BW(=2) * SGI(=2) */
147 +#define IEEE80211_VHT_STREAM_GROUPS 8 /* BW(=4) * SGI(=2) */
149 +#define IEEE80211_HE_MAX_STREAMS 8
150 +#define IEEE80211_HE_STREAM_GROUPS 12 /* BW(=4) * GI(=3) */
152 +#define IEEE80211_HT_GROUPS_NB (IEEE80211_MAX_STREAMS * \
153 + IEEE80211_HT_STREAM_GROUPS)
154 +#define IEEE80211_VHT_GROUPS_NB (IEEE80211_MAX_STREAMS * \
155 + IEEE80211_VHT_STREAM_GROUPS)
156 +#define IEEE80211_HE_GROUPS_NB (IEEE80211_HE_MAX_STREAMS * \
157 + IEEE80211_HE_STREAM_GROUPS)
158 +#define IEEE80211_GROUPS_NB (IEEE80211_HT_GROUPS_NB + \
159 + IEEE80211_VHT_GROUPS_NB + \
160 + IEEE80211_HE_GROUPS_NB)
162 +#define IEEE80211_HT_GROUP_0 0
163 +#define IEEE80211_VHT_GROUP_0 (IEEE80211_HT_GROUP_0 + IEEE80211_HT_GROUPS_NB)
164 +#define IEEE80211_HE_GROUP_0 (IEEE80211_VHT_GROUP_0 + IEEE80211_VHT_GROUPS_NB)
166 +#define MCS_GROUP_RATES 12
168 +#define HT_GROUP_IDX(_streams, _sgi, _ht40) \
169 + IEEE80211_HT_GROUP_0 + \
170 + IEEE80211_MAX_STREAMS * 2 * _ht40 + \
171 + IEEE80211_MAX_STREAMS * _sgi + \
174 +#define _MAX(a, b) (((a)>(b))?(a):(b))
176 +#define GROUP_SHIFT(duration) \
177 + _MAX(0, 16 - __builtin_clz(duration))
179 +/* MCS rate information for an MCS group */
180 +#define __MCS_GROUP(_streams, _sgi, _ht40, _s) \
181 + [HT_GROUP_IDX(_streams, _sgi, _ht40)] = { \
184 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 54 : 26), \
185 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 108 : 52), \
186 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 162 : 78), \
187 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 216 : 104), \
188 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 324 : 156), \
189 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 432 : 208), \
190 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 486 : 234), \
191 + MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 540 : 260) \
195 +#define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \
196 + GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
198 +#define MCS_GROUP(_streams, _sgi, _ht40) \
199 + __MCS_GROUP(_streams, _sgi, _ht40, \
200 + MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
202 +#define VHT_GROUP_IDX(_streams, _sgi, _bw) \
203 + (IEEE80211_VHT_GROUP_0 + \
204 + IEEE80211_MAX_STREAMS * 2 * (_bw) + \
205 + IEEE80211_MAX_STREAMS * (_sgi) + \
208 +#define BW2VBPS(_bw, r4, r3, r2, r1) \
209 + (_bw == BW_160 ? r4 : _bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
211 +#define __VHT_GROUP(_streams, _sgi, _bw, _s) \
212 + [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
215 + MCS_DURATION_S(_s, _streams, _sgi, \
216 + BW2VBPS(_bw, 234, 117, 54, 26)), \
217 + MCS_DURATION_S(_s, _streams, _sgi, \
218 + BW2VBPS(_bw, 468, 234, 108, 52)), \
219 + MCS_DURATION_S(_s, _streams, _sgi, \
220 + BW2VBPS(_bw, 702, 351, 162, 78)), \
221 + MCS_DURATION_S(_s, _streams, _sgi, \
222 + BW2VBPS(_bw, 936, 468, 216, 104)), \
223 + MCS_DURATION_S(_s, _streams, _sgi, \
224 + BW2VBPS(_bw, 1404, 702, 324, 156)), \
225 + MCS_DURATION_S(_s, _streams, _sgi, \
226 + BW2VBPS(_bw, 1872, 936, 432, 208)), \
227 + MCS_DURATION_S(_s, _streams, _sgi, \
228 + BW2VBPS(_bw, 2106, 1053, 486, 234)), \
229 + MCS_DURATION_S(_s, _streams, _sgi, \
230 + BW2VBPS(_bw, 2340, 1170, 540, 260)), \
231 + MCS_DURATION_S(_s, _streams, _sgi, \
232 + BW2VBPS(_bw, 2808, 1404, 648, 312)), \
233 + MCS_DURATION_S(_s, _streams, _sgi, \
234 + BW2VBPS(_bw, 3120, 1560, 720, 346)) \
238 +#define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \
239 + GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \
240 + BW2VBPS(_bw, 243, 117, 54, 26)))
242 +#define VHT_GROUP(_streams, _sgi, _bw) \
243 + __VHT_GROUP(_streams, _sgi, _bw, \
244 + VHT_GROUP_SHIFT(_streams, _sgi, _bw))
247 +#define HE_GROUP_IDX(_streams, _gi, _bw) \
248 + (IEEE80211_HE_GROUP_0 + \
249 + IEEE80211_HE_MAX_STREAMS * 3 * (_bw) + \
250 + IEEE80211_HE_MAX_STREAMS * (_gi) + \
253 +#define __HE_GROUP(_streams, _gi, _bw, _s) \
254 + [HE_GROUP_IDX(_streams, _gi, _bw)] = { \
257 + HE_DURATION_S(_s, _streams, _gi, \
258 + BW2VBPS(_bw, 979, 489, 230, 115)), \
259 + HE_DURATION_S(_s, _streams, _gi, \
260 + BW2VBPS(_bw, 1958, 979, 475, 230)), \
261 + HE_DURATION_S(_s, _streams, _gi, \
262 + BW2VBPS(_bw, 2937, 1468, 705, 345)), \
263 + HE_DURATION_S(_s, _streams, _gi, \
264 + BW2VBPS(_bw, 3916, 1958, 936, 475)), \
265 + HE_DURATION_S(_s, _streams, _gi, \
266 + BW2VBPS(_bw, 5875, 2937, 1411, 705)), \
267 + HE_DURATION_S(_s, _streams, _gi, \
268 + BW2VBPS(_bw, 7833, 3916, 1872, 936)), \
269 + HE_DURATION_S(_s, _streams, _gi, \
270 + BW2VBPS(_bw, 8827, 4406, 2102, 1051)), \
271 + HE_DURATION_S(_s, _streams, _gi, \
272 + BW2VBPS(_bw, 9806, 4896, 2347, 1166)), \
273 + HE_DURATION_S(_s, _streams, _gi, \
274 + BW2VBPS(_bw, 11764, 5875, 2808, 1411)), \
275 + HE_DURATION_S(_s, _streams, _gi, \
276 + BW2VBPS(_bw, 13060, 6523, 3124, 1555)), \
277 + HE_DURATION_S(_s, _streams, _gi, \
278 + BW2VBPS(_bw, 14702, 7344, 3513, 1756)), \
279 + HE_DURATION_S(_s, _streams, _gi, \
280 + BW2VBPS(_bw, 16329, 8164, 3902, 1944)) \
284 +#define HE_GROUP_SHIFT(_streams, _gi, _bw) \
285 + GROUP_SHIFT(HE_DURATION(_streams, _gi, \
286 + BW2VBPS(_bw, 979, 489, 230, 115)))
288 +#define HE_GROUP(_streams, _gi, _bw) \
289 + __HE_GROUP(_streams, _gi, _bw, \
290 + HE_GROUP_SHIFT(_streams, _gi, _bw))
293 + u16 duration[MCS_GROUP_RATES];
296 +static const struct mcs_group airtime_mcs_groups[] = {
297 + MCS_GROUP(1, 0, BW_20),
298 + MCS_GROUP(2, 0, BW_20),
299 + MCS_GROUP(3, 0, BW_20),
300 + MCS_GROUP(4, 0, BW_20),
302 + MCS_GROUP(1, 1, BW_20),
303 + MCS_GROUP(2, 1, BW_20),
304 + MCS_GROUP(3, 1, BW_20),
305 + MCS_GROUP(4, 1, BW_20),
307 + MCS_GROUP(1, 0, BW_40),
308 + MCS_GROUP(2, 0, BW_40),
309 + MCS_GROUP(3, 0, BW_40),
310 + MCS_GROUP(4, 0, BW_40),
312 + MCS_GROUP(1, 1, BW_40),
313 + MCS_GROUP(2, 1, BW_40),
314 + MCS_GROUP(3, 1, BW_40),
315 + MCS_GROUP(4, 1, BW_40),
317 + VHT_GROUP(1, 0, BW_20),
318 + VHT_GROUP(2, 0, BW_20),
319 + VHT_GROUP(3, 0, BW_20),
320 + VHT_GROUP(4, 0, BW_20),
322 + VHT_GROUP(1, 1, BW_20),
323 + VHT_GROUP(2, 1, BW_20),
324 + VHT_GROUP(3, 1, BW_20),
325 + VHT_GROUP(4, 1, BW_20),
327 + VHT_GROUP(1, 0, BW_40),
328 + VHT_GROUP(2, 0, BW_40),
329 + VHT_GROUP(3, 0, BW_40),
330 + VHT_GROUP(4, 0, BW_40),
332 + VHT_GROUP(1, 1, BW_40),
333 + VHT_GROUP(2, 1, BW_40),
334 + VHT_GROUP(3, 1, BW_40),
335 + VHT_GROUP(4, 1, BW_40),
337 + VHT_GROUP(1, 0, BW_80),
338 + VHT_GROUP(2, 0, BW_80),
339 + VHT_GROUP(3, 0, BW_80),
340 + VHT_GROUP(4, 0, BW_80),
342 + VHT_GROUP(1, 1, BW_80),
343 + VHT_GROUP(2, 1, BW_80),
344 + VHT_GROUP(3, 1, BW_80),
345 + VHT_GROUP(4, 1, BW_80),
347 + VHT_GROUP(1, 0, BW_160),
348 + VHT_GROUP(2, 0, BW_160),
349 + VHT_GROUP(3, 0, BW_160),
350 + VHT_GROUP(4, 0, BW_160),
352 + VHT_GROUP(1, 1, BW_160),
353 + VHT_GROUP(2, 1, BW_160),
354 + VHT_GROUP(3, 1, BW_160),
355 + VHT_GROUP(4, 1, BW_160),
357 + HE_GROUP(1, HE_GI_08, BW_20),
358 + HE_GROUP(2, HE_GI_08, BW_20),
359 + HE_GROUP(3, HE_GI_08, BW_20),
360 + HE_GROUP(4, HE_GI_08, BW_20),
361 + HE_GROUP(5, HE_GI_08, BW_20),
362 + HE_GROUP(6, HE_GI_08, BW_20),
363 + HE_GROUP(7, HE_GI_08, BW_20),
364 + HE_GROUP(8, HE_GI_08, BW_20),
366 + HE_GROUP(1, HE_GI_16, BW_20),
367 + HE_GROUP(2, HE_GI_16, BW_20),
368 + HE_GROUP(3, HE_GI_16, BW_20),
369 + HE_GROUP(4, HE_GI_16, BW_20),
370 + HE_GROUP(5, HE_GI_16, BW_20),
371 + HE_GROUP(6, HE_GI_16, BW_20),
372 + HE_GROUP(7, HE_GI_16, BW_20),
373 + HE_GROUP(8, HE_GI_16, BW_20),
375 + HE_GROUP(1, HE_GI_32, BW_20),
376 + HE_GROUP(2, HE_GI_32, BW_20),
377 + HE_GROUP(3, HE_GI_32, BW_20),
378 + HE_GROUP(4, HE_GI_32, BW_20),
379 + HE_GROUP(5, HE_GI_32, BW_20),
380 + HE_GROUP(6, HE_GI_32, BW_20),
381 + HE_GROUP(7, HE_GI_32, BW_20),
382 + HE_GROUP(8, HE_GI_32, BW_20),
384 + HE_GROUP(1, HE_GI_08, BW_40),
385 + HE_GROUP(2, HE_GI_08, BW_40),
386 + HE_GROUP(3, HE_GI_08, BW_40),
387 + HE_GROUP(4, HE_GI_08, BW_40),
388 + HE_GROUP(5, HE_GI_08, BW_40),
389 + HE_GROUP(6, HE_GI_08, BW_40),
390 + HE_GROUP(7, HE_GI_08, BW_40),
391 + HE_GROUP(8, HE_GI_08, BW_40),
393 + HE_GROUP(1, HE_GI_16, BW_40),
394 + HE_GROUP(2, HE_GI_16, BW_40),
395 + HE_GROUP(3, HE_GI_16, BW_40),
396 + HE_GROUP(4, HE_GI_16, BW_40),
397 + HE_GROUP(5, HE_GI_16, BW_40),
398 + HE_GROUP(6, HE_GI_16, BW_40),
399 + HE_GROUP(7, HE_GI_16, BW_40),
400 + HE_GROUP(8, HE_GI_16, BW_40),
402 + HE_GROUP(1, HE_GI_32, BW_40),
403 + HE_GROUP(2, HE_GI_32, BW_40),
404 + HE_GROUP(3, HE_GI_32, BW_40),
405 + HE_GROUP(4, HE_GI_32, BW_40),
406 + HE_GROUP(5, HE_GI_32, BW_40),
407 + HE_GROUP(6, HE_GI_32, BW_40),
408 + HE_GROUP(7, HE_GI_32, BW_40),
409 + HE_GROUP(8, HE_GI_32, BW_40),
411 + HE_GROUP(1, HE_GI_08, BW_80),
412 + HE_GROUP(2, HE_GI_08, BW_80),
413 + HE_GROUP(3, HE_GI_08, BW_80),
414 + HE_GROUP(4, HE_GI_08, BW_80),
415 + HE_GROUP(5, HE_GI_08, BW_80),
416 + HE_GROUP(6, HE_GI_08, BW_80),
417 + HE_GROUP(7, HE_GI_08, BW_80),
418 + HE_GROUP(8, HE_GI_08, BW_80),
420 + HE_GROUP(1, HE_GI_16, BW_80),
421 + HE_GROUP(2, HE_GI_16, BW_80),
422 + HE_GROUP(3, HE_GI_16, BW_80),
423 + HE_GROUP(4, HE_GI_16, BW_80),
424 + HE_GROUP(5, HE_GI_16, BW_80),
425 + HE_GROUP(6, HE_GI_16, BW_80),
426 + HE_GROUP(7, HE_GI_16, BW_80),
427 + HE_GROUP(8, HE_GI_16, BW_80),
429 + HE_GROUP(1, HE_GI_32, BW_80),
430 + HE_GROUP(2, HE_GI_32, BW_80),
431 + HE_GROUP(3, HE_GI_32, BW_80),
432 + HE_GROUP(4, HE_GI_32, BW_80),
433 + HE_GROUP(5, HE_GI_32, BW_80),
434 + HE_GROUP(6, HE_GI_32, BW_80),
435 + HE_GROUP(7, HE_GI_32, BW_80),
436 + HE_GROUP(8, HE_GI_32, BW_80),
438 + HE_GROUP(1, HE_GI_08, BW_160),
439 + HE_GROUP(2, HE_GI_08, BW_160),
440 + HE_GROUP(3, HE_GI_08, BW_160),
441 + HE_GROUP(4, HE_GI_08, BW_160),
442 + HE_GROUP(5, HE_GI_08, BW_160),
443 + HE_GROUP(6, HE_GI_08, BW_160),
444 + HE_GROUP(7, HE_GI_08, BW_160),
445 + HE_GROUP(8, HE_GI_08, BW_160),
447 + HE_GROUP(1, HE_GI_16, BW_160),
448 + HE_GROUP(2, HE_GI_16, BW_160),
449 + HE_GROUP(3, HE_GI_16, BW_160),
450 + HE_GROUP(4, HE_GI_16, BW_160),
451 + HE_GROUP(5, HE_GI_16, BW_160),
452 + HE_GROUP(6, HE_GI_16, BW_160),
453 + HE_GROUP(7, HE_GI_16, BW_160),
454 + HE_GROUP(8, HE_GI_16, BW_160),
456 + HE_GROUP(1, HE_GI_32, BW_160),
457 + HE_GROUP(2, HE_GI_32, BW_160),
458 + HE_GROUP(3, HE_GI_32, BW_160),
459 + HE_GROUP(4, HE_GI_32, BW_160),
460 + HE_GROUP(5, HE_GI_32, BW_160),
461 + HE_GROUP(6, HE_GI_32, BW_160),
462 + HE_GROUP(7, HE_GI_32, BW_160),
463 + HE_GROUP(8, HE_GI_32, BW_160),
467 +ieee80211_calc_legacy_rate_duration(u16 bitrate, bool short_pre,
473 + duration = 144 + 48; /* preamble + PLCP */
477 + duration += 10; /* SIFS */
479 + duration = 20 + 16; /* premable + SIFS */
483 + duration += (len * 10) / bitrate;
488 +u32 ieee80211_calc_rx_airtime(struct ieee80211_hw *hw,
489 + struct ieee80211_rx_status *status,
492 + struct ieee80211_supported_band *sband;
493 + const struct ieee80211_rate *rate;
494 + bool sgi = status->enc_flags & RX_ENC_FLAG_SHORT_GI;
495 + bool sp = status->enc_flags & RX_ENC_FLAG_SHORTPRE;
501 + switch (status->bw) {
502 + case RATE_INFO_BW_20:
505 + case RATE_INFO_BW_40:
508 + case RATE_INFO_BW_80:
511 + case RATE_INFO_BW_160:
519 + switch (status->encoding) {
520 + case RX_ENC_LEGACY:
521 + if (WARN_ON_ONCE(status->band > NL80211_BAND_5GHZ))
524 + sband = hw->wiphy->bands[status->band];
525 + if (!sband || status->rate_idx > sband->n_bitrates)
528 + rate = &sband->bitrates[status->rate_idx];
529 + cck = rate->flags & IEEE80211_RATE_MANDATORY_B;
531 + return ieee80211_calc_legacy_rate_duration(rate->bitrate, sp,
535 + streams = status->nss;
536 + idx = status->rate_idx;
537 + group = VHT_GROUP_IDX(streams, sgi, bw);
540 + streams = ((status->rate_idx >> 3) & 3) + 1;
541 + idx = status->rate_idx & 7;
542 + group = HT_GROUP_IDX(streams, sgi, bw);
545 + streams = status->nss;
546 + idx = status->rate_idx;
547 + group = HE_GROUP_IDX(streams, status->he_gi, bw);
554 + if (WARN_ON_ONCE((status->encoding != RX_ENC_HE && streams > 4) ||
555 + (status->encoding == RX_ENC_HE && streams > 8)))
558 + duration = airtime_mcs_groups[group].duration[idx];
559 + duration <<= airtime_mcs_groups[group].shift;
561 + duration /= AVG_PKT_SIZE;
564 + duration += 36 + (streams << 2);
568 +EXPORT_SYMBOL_GPL(ieee80211_calc_rx_airtime);
570 +static u32 ieee80211_calc_tx_airtime_rate(struct ieee80211_hw *hw,
571 + struct ieee80211_tx_rate *rate,
574 + struct ieee80211_rx_status stat = {
578 + if (rate->idx < 0 || !rate->count)
581 + if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
582 + stat.bw = RATE_INFO_BW_80;
583 + else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
584 + stat.bw = RATE_INFO_BW_40;
586 + stat.bw = RATE_INFO_BW_20;
588 + stat.enc_flags = 0;
589 + if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
590 + stat.enc_flags |= RX_ENC_FLAG_SHORTPRE;
591 + if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
592 + stat.enc_flags |= RX_ENC_FLAG_SHORT_GI;
594 + stat.rate_idx = rate->idx;
595 + if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
596 + stat.encoding = RX_ENC_VHT;
597 + stat.rate_idx = ieee80211_rate_get_vht_mcs(rate);
598 + stat.nss = ieee80211_rate_get_vht_nss(rate);
599 + } else if (rate->flags & IEEE80211_TX_RC_MCS) {
600 + stat.encoding = RX_ENC_HT;
602 + stat.encoding = RX_ENC_LEGACY;
605 + return ieee80211_calc_rx_airtime(hw, &stat, len);
608 +u32 ieee80211_calc_tx_airtime(struct ieee80211_hw *hw,
609 + struct ieee80211_tx_info *info,
615 + for (i = 0; i < ARRAY_SIZE(info->status.rates); i++) {
616 + struct ieee80211_tx_rate *rate = &info->status.rates[i];
619 + cur_duration = ieee80211_calc_tx_airtime_rate(hw, rate,
624 + duration += cur_duration * rate->count;
629 +EXPORT_SYMBOL_GPL(ieee80211_calc_tx_airtime);
631 +u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
632 + struct ieee80211_vif *vif,
633 + struct ieee80211_sta *pubsta,
636 + struct ieee80211_supported_band *sband;
637 + struct ieee80211_chanctx_conf *conf;
638 + int rateidx, shift = 0;
639 + bool cck, short_pream;
644 + len += 38; /* Ethernet header length */
646 + conf = rcu_dereference(vif->chanctx_conf);
648 + band = conf->def.chan->band;
649 + shift = ieee80211_chandef_get_shift(&conf->def);
653 + struct sta_info *sta = container_of(pubsta, struct sta_info,
656 + return ieee80211_calc_tx_airtime_rate(hw,
657 + &sta->tx_stats.last_rate,
664 + /* No station to get latest rate from, so calculate the worst-case
665 + * duration using the lowest configured basic rate.
667 + sband = hw->wiphy->bands[band];
669 + basic_rates = vif->bss_conf.basic_rates;
670 + short_pream = vif->bss_conf.use_short_preamble;
672 + rateidx = basic_rates ? ffs(basic_rates) - 1 : 0;
673 + rate = sband->bitrates[rateidx].bitrate << shift;
674 + cck = sband->bitrates[rateidx].flags & IEEE80211_RATE_MANDATORY_B;
676 + return ieee80211_calc_legacy_rate_duration(rate, short_pream, cck, len);
678 --- a/net/mac80211/ieee80211_i.h
679 +++ b/net/mac80211/ieee80211_i.h
680 @@ -2253,6 +2253,10 @@ const char *ieee80211_get_reason_code_st
682 extern const struct ethtool_ops ieee80211_ethtool_ops;
684 +u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
685 + struct ieee80211_vif *vif,
686 + struct ieee80211_sta *pubsta,
688 #ifdef CPTCFG_MAC80211_NOINLINE
689 #define debug_noinline noinline