mac80211: Update to version 5.4.36-1
[oweals/openwrt.git] / package / kernel / mac80211 / patches / subsys / 309-mac80211-Import-airtime-calculation-code-from-mt76.patch
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
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
7
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
10 calculations.
11
12 The airtime.c file is copied verbatim from the mt76 driver, and adjusted to
13 be usable in mac80211. This involves:
14
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.
19
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.
22
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>
27 ---
28  create mode 100644 net/mac80211/airtime.c
29
30 --- a/include/net/mac80211.h
31 +++ b/include/net/mac80211.h
32 @@ -6419,4 +6419,33 @@ void ieee80211_nan_func_match(struct iee
33                               struct cfg80211_nan_match_params *match,
34                               gfp_t gfp);
35  
36 +/**
37 + * ieee80211_calc_rx_airtime - calculate estimated transmission airtime for RX.
38 + *
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.
41 + *
42 + * @hw: pointer as obtained from ieee80211_alloc_hw()
43 + * @status: &struct ieee80211_rx_status containing the transmission rate
44 + *          information.
45 + * @len: frame length in bytes
46 + */
47 +u32 ieee80211_calc_rx_airtime(struct ieee80211_hw *hw,
48 +                             struct ieee80211_rx_status *status,
49 +                             int len);
50 +
51 +/**
52 + * ieee80211_calc_tx_airtime - calculate estimated transmission airtime for TX.
53 + *
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.
56 + *
57 + * @hw: pointer as obtained from ieee80211_alloc_hw()
58 + * @info: &struct ieee80211_tx_info of the frame.
59 + * @len: frame length in bytes
60 + */
61 +u32 ieee80211_calc_tx_airtime(struct ieee80211_hw *hw,
62 +                             struct ieee80211_tx_info *info,
63 +                             int len);
64 +
65  #endif /* MAC80211_H */
66 --- a/net/mac80211/Makefile
67 +++ b/net/mac80211/Makefile
68 @@ -31,7 +31,8 @@ mac80211-y := \
69         chan.o \
70         trace.o mlme.o \
71         tdls.o \
72 -       ocb.o
73 +       ocb.o \
74 +       airtime.o
75  
76  mac80211-$(CPTCFG_MAC80211_LEDS) += led.o
77  mac80211-$(CPTCFG_MAC80211_DEBUGFS) += \
78 --- /dev/null
79 +++ b/net/mac80211/airtime.c
80 @@ -0,0 +1,597 @@
81 +// SPDX-License-Identifier: ISC
82 +/*
83 + * Copyright (C) 2019 Felix Fietkau <nbd@nbd.name>
84 + */
85 +
86 +#include <net/mac80211.h>
87 +#include "ieee80211_i.h"
88 +#include "sta_info.h"
89 +
90 +#define AVG_PKT_SIZE   1024
91 +
92 +/* Number of bits for an average sized packet */
93 +#define MCS_NBITS (AVG_PKT_SIZE << 3)
94 +
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.
97 + */
98 +#define MCS_N_KSYMS(bps) DIV_ROUND_UP(MCS_NBITS << 10, (bps))
99 +
100 +/* Transmission time (in 1024 * usec) for a packet containing (ksyms) * 1024
101 + * symbols.
102 + */
103 +#define MCS_SYMBOL_TIME(sgi, ksyms)                                    \
104 +       (sgi ?                                                          \
105 +         ((ksyms) * 4 * 18) / 20 :             /* 3.6 us per sym */    \
106 +         ((ksyms) * 4)                 /* 4.0 us per sym */    \
107 +       )
108 +
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))))
112 +
113 +#define MCS_DURATION_S(shift, streams, sgi, bps)               \
114 +       ((u16)((MCS_DURATION(streams, sgi, bps) >> shift)))
115 +
116 +/* These should match the values in enum nl80211_he_gi */
117 +#define HE_GI_08 0
118 +#define HE_GI_16 1
119 +#define HE_GI_32 2
120 +
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 */   \
128 +        ))
129 +
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))))
133 +
134 +#define HE_DURATION_S(shift, streams, gi, bps)         \
135 +       (HE_DURATION(streams, gi, bps) >> shift)
136 +
137 +#define BW_20                  0
138 +#define BW_40                  1
139 +#define BW_80                  2
140 +#define BW_160                 3
141 +
142 +/*
143 + * Define group sort order: HT40 -> SGI -> #streams
144 + */
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) */
148 +
149 +#define IEEE80211_HE_MAX_STREAMS       8
150 +#define IEEE80211_HE_STREAM_GROUPS     12 /* BW(=4) * GI(=3) */
151 +
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)
161 +
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)
165 +
166 +#define MCS_GROUP_RATES                12
167 +
168 +#define HT_GROUP_IDX(_streams, _sgi, _ht40)    \
169 +       IEEE80211_HT_GROUP_0 +                  \
170 +       IEEE80211_MAX_STREAMS * 2 * _ht40 +     \
171 +       IEEE80211_MAX_STREAMS * _sgi +          \
172 +       _streams - 1
173 +
174 +#define _MAX(a, b) (((a)>(b))?(a):(b))
175 +
176 +#define GROUP_SHIFT(duration)                                          \
177 +       _MAX(0, 16 - __builtin_clz(duration))
178 +
179 +/* MCS rate information for an MCS group */
180 +#define __MCS_GROUP(_streams, _sgi, _ht40, _s)                         \
181 +       [HT_GROUP_IDX(_streams, _sgi, _ht40)] = {                       \
182 +       .shift = _s,                                                    \
183 +       .duration = {                                                   \
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)   \
192 +       }                                                               \
193 +}
194 +
195 +#define MCS_GROUP_SHIFT(_streams, _sgi, _ht40)                         \
196 +       GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
197 +
198 +#define MCS_GROUP(_streams, _sgi, _ht40)                               \
199 +       __MCS_GROUP(_streams, _sgi, _ht40,                              \
200 +                   MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
201 +
202 +#define VHT_GROUP_IDX(_streams, _sgi, _bw)                             \
203 +       (IEEE80211_VHT_GROUP_0 +                                        \
204 +        IEEE80211_MAX_STREAMS * 2 * (_bw) +                            \
205 +        IEEE80211_MAX_STREAMS * (_sgi) +                               \
206 +        (_streams) - 1)
207 +
208 +#define BW2VBPS(_bw, r4, r3, r2, r1)                                   \
209 +       (_bw == BW_160 ? r4 : _bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
210 +
211 +#define __VHT_GROUP(_streams, _sgi, _bw, _s)                           \
212 +       [VHT_GROUP_IDX(_streams, _sgi, _bw)] = {                        \
213 +       .shift = _s,                                                    \
214 +       .duration = {                                                   \
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))      \
235 +        }                                                              \
236 +}
237 +
238 +#define VHT_GROUP_SHIFT(_streams, _sgi, _bw)                           \
239 +       GROUP_SHIFT(MCS_DURATION(_streams, _sgi,                        \
240 +                                BW2VBPS(_bw, 243, 117,  54,  26)))
241 +
242 +#define VHT_GROUP(_streams, _sgi, _bw)                                 \
243 +       __VHT_GROUP(_streams, _sgi, _bw,                                \
244 +                   VHT_GROUP_SHIFT(_streams, _sgi, _bw))
245 +
246 +
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) +                             \
251 +        (_streams) - 1)
252 +
253 +#define __HE_GROUP(_streams, _gi, _bw, _s)                             \
254 +       [HE_GROUP_IDX(_streams, _gi, _bw)] = {                  \
255 +       .shift = _s,                                                    \
256 +       .duration = {                                                   \
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))    \
281 +        }                                                              \
282 +}
283 +
284 +#define HE_GROUP_SHIFT(_streams, _gi, _bw)                             \
285 +       GROUP_SHIFT(HE_DURATION(_streams, _gi,                  \
286 +                               BW2VBPS(_bw,   979,  489,  230,  115)))
287 +
288 +#define HE_GROUP(_streams, _gi, _bw)                                   \
289 +       __HE_GROUP(_streams, _gi, _bw,                          \
290 +                  HE_GROUP_SHIFT(_streams, _gi, _bw))
291 +struct mcs_group {
292 +       u8 shift;
293 +       u16 duration[MCS_GROUP_RATES];
294 +};
295 +
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),
301 +
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),
306 +
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),
311 +
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),
316 +
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),
321 +
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),
326 +
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),
331 +
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),
336 +
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),
341 +
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),
346 +
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),
351 +
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),
356 +
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),
365 +
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),
374 +
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),
383 +
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),
392 +
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),
401 +
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),
410 +
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),
419 +
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),
428 +
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),
437 +
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),
446 +
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),
455 +
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),
464 +};
465 +
466 +static u32
467 +ieee80211_calc_legacy_rate_duration(u16 bitrate, bool short_pre,
468 +                                   bool cck, int len)
469 +{
470 +       u32 duration;
471 +
472 +       if (cck) {
473 +               duration = 144 + 48; /* preamble + PLCP */
474 +               if (short_pre)
475 +                       duration >>= 1;
476 +
477 +               duration += 10; /* SIFS */
478 +       } else {
479 +               duration = 20 + 16; /* premable + SIFS */
480 +       }
481 +
482 +       len <<= 3;
483 +       duration += (len * 10) / bitrate;
484 +
485 +       return duration;
486 +}
487 +
488 +u32 ieee80211_calc_rx_airtime(struct ieee80211_hw *hw,
489 +                             struct ieee80211_rx_status *status,
490 +                             int len)
491 +{
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;
496 +       int bw, streams;
497 +       int group, idx;
498 +       u32 duration;
499 +       bool cck;
500 +
501 +       switch (status->bw) {
502 +       case RATE_INFO_BW_20:
503 +               bw = BW_20;
504 +               break;
505 +       case RATE_INFO_BW_40:
506 +               bw = BW_40;
507 +               break;
508 +       case RATE_INFO_BW_80:
509 +               bw = BW_80;
510 +               break;
511 +       case RATE_INFO_BW_160:
512 +               bw = BW_160;
513 +               break;
514 +       default:
515 +               WARN_ON_ONCE(1);
516 +               return 0;
517 +       }
518 +
519 +       switch (status->encoding) {
520 +       case RX_ENC_LEGACY:
521 +               if (WARN_ON_ONCE(status->band > NL80211_BAND_5GHZ))
522 +                       return 0;
523 +
524 +               sband = hw->wiphy->bands[status->band];
525 +               if (!sband || status->rate_idx > sband->n_bitrates)
526 +                       return 0;
527 +
528 +               rate = &sband->bitrates[status->rate_idx];
529 +               cck = rate->flags & IEEE80211_RATE_MANDATORY_B;
530 +
531 +               return ieee80211_calc_legacy_rate_duration(rate->bitrate, sp,
532 +                                                          cck, len);
533 +
534 +       case RX_ENC_VHT:
535 +               streams = status->nss;
536 +               idx = status->rate_idx;
537 +               group = VHT_GROUP_IDX(streams, sgi, bw);
538 +               break;
539 +       case RX_ENC_HT:
540 +               streams = ((status->rate_idx >> 3) & 3) + 1;
541 +               idx = status->rate_idx & 7;
542 +               group = HT_GROUP_IDX(streams, sgi, bw);
543 +               break;
544 +       case RX_ENC_HE:
545 +               streams = status->nss;
546 +               idx = status->rate_idx;
547 +               group = HE_GROUP_IDX(streams, status->he_gi, bw);
548 +               break;
549 +       default:
550 +               WARN_ON_ONCE(1);
551 +               return 0;
552 +       }
553 +
554 +       if (WARN_ON_ONCE((status->encoding != RX_ENC_HE && streams > 4) ||
555 +                        (status->encoding == RX_ENC_HE && streams > 8)))
556 +               return 0;
557 +
558 +       duration = airtime_mcs_groups[group].duration[idx];
559 +       duration <<= airtime_mcs_groups[group].shift;
560 +       duration *= len;
561 +       duration /= AVG_PKT_SIZE;
562 +       duration /= 1024;
563 +
564 +       duration += 36 + (streams << 2);
565 +
566 +       return duration;
567 +}
568 +EXPORT_SYMBOL_GPL(ieee80211_calc_rx_airtime);
569 +
570 +static u32 ieee80211_calc_tx_airtime_rate(struct ieee80211_hw *hw,
571 +                                         struct ieee80211_tx_rate *rate,
572 +                                         u8 band, int len)
573 +{
574 +       struct ieee80211_rx_status stat = {
575 +               .band = band,
576 +       };
577 +
578 +       if (rate->idx < 0 || !rate->count)
579 +               return 0;
580 +
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;
585 +       else
586 +               stat.bw = RATE_INFO_BW_20;
587 +
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;
593 +
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;
601 +       } else {
602 +               stat.encoding = RX_ENC_LEGACY;
603 +       }
604 +
605 +       return ieee80211_calc_rx_airtime(hw, &stat, len);
606 +}
607 +
608 +u32 ieee80211_calc_tx_airtime(struct ieee80211_hw *hw,
609 +                             struct ieee80211_tx_info *info,
610 +                             int len)
611 +{
612 +       u32 duration = 0;
613 +       int i;
614 +
615 +       for (i = 0; i < ARRAY_SIZE(info->status.rates); i++) {
616 +               struct ieee80211_tx_rate *rate = &info->status.rates[i];
617 +               u32 cur_duration;
618 +
619 +               cur_duration = ieee80211_calc_tx_airtime_rate(hw, rate,
620 +                                                             info->band, len);
621 +               if (!cur_duration)
622 +                       break;
623 +
624 +               duration += cur_duration * rate->count;
625 +       }
626 +
627 +       return duration;
628 +}
629 +EXPORT_SYMBOL_GPL(ieee80211_calc_tx_airtime);
630 +
631 +u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
632 +                                      struct ieee80211_vif *vif,
633 +                                      struct ieee80211_sta *pubsta,
634 +                                      int len)
635 +{
636 +       struct ieee80211_supported_band *sband;
637 +       struct ieee80211_chanctx_conf *conf;
638 +       int rateidx, shift = 0;
639 +       bool cck, short_pream;
640 +       u32 basic_rates;
641 +       u8 band = 0;
642 +       u16 rate;
643 +
644 +       len += 38; /* Ethernet header length */
645 +
646 +       conf = rcu_dereference(vif->chanctx_conf);
647 +       if (conf) {
648 +               band = conf->def.chan->band;
649 +               shift = ieee80211_chandef_get_shift(&conf->def);
650 +       }
651 +
652 +       if (pubsta) {
653 +               struct sta_info *sta = container_of(pubsta, struct sta_info,
654 +                                                   sta);
655 +
656 +               return ieee80211_calc_tx_airtime_rate(hw,
657 +                                                     &sta->tx_stats.last_rate,
658 +                                                     band, len);
659 +       }
660 +
661 +       if (!conf)
662 +               return 0;
663 +
664 +       /* No station to get latest rate from, so calculate the worst-case
665 +        * duration using the lowest configured basic rate.
666 +        */
667 +       sband = hw->wiphy->bands[band];
668 +
669 +       basic_rates = vif->bss_conf.basic_rates;
670 +       short_pream = vif->bss_conf.use_short_preamble;
671 +
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;
675 +
676 +       return ieee80211_calc_legacy_rate_duration(rate, short_pream, cck, len);
677 +}
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
681  
682  extern const struct ethtool_ops ieee80211_ethtool_ops;
683  
684 +u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
685 +                                      struct ieee80211_vif *vif,
686 +                                      struct ieee80211_sta *pubsta,
687 +                                      int len);
688  #ifdef CPTCFG_MAC80211_NOINLINE
689  #define debug_noinline noinline
690  #else