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 /* gi in HE/EHT is identical. It matches enum nl80211_eht_gi as well */
59 #define EHT_GI_08 HE_GI_08
60 #define EHT_GI_16 HE_GI_16
61 #define EHT_GI_32 HE_GI_32
62
63 #define EHT_DURATION(streams, gi, bps) \
64 HE_DURATION(streams, gi, bps)
65 #define EHT_DURATION_S(shift, streams, gi, bps) \
66 HE_DURATION_S(shift, streams, gi, bps)
67
68 #define BW_20 0
69 #define BW_40 1
70 #define BW_80 2
71 #define BW_160 3
72 #define BW_320 4
73
74 /*
75 * Define group sort order: HT40 -> SGI -> #streams
76 */
77 #define IEEE80211_MAX_STREAMS 4
78 #define IEEE80211_HT_STREAM_GROUPS 4 /* BW(=2) * SGI(=2) */
79 #define IEEE80211_VHT_STREAM_GROUPS 8 /* BW(=4) * SGI(=2) */
80
81 #define IEEE80211_HE_MAX_STREAMS 8
82 #define IEEE80211_HE_STREAM_GROUPS 12 /* BW(=4) * GI(=3) */
83
84 #define IEEE80211_EHT_MAX_STREAMS 8
85 #define IEEE80211_EHT_STREAM_GROUPS 15 /* BW(=5) * GI(=3) */
86
87 #define IEEE80211_HT_GROUPS_NB (IEEE80211_MAX_STREAMS * \
88 IEEE80211_HT_STREAM_GROUPS)
89 #define IEEE80211_VHT_GROUPS_NB (IEEE80211_MAX_STREAMS * \
90 IEEE80211_VHT_STREAM_GROUPS)
91 #define IEEE80211_HE_GROUPS_NB (IEEE80211_HE_MAX_STREAMS * \
92 IEEE80211_HE_STREAM_GROUPS)
93 #define IEEE80211_EHT_GROUPS_NB (IEEE80211_EHT_MAX_STREAMS * \
94 IEEE80211_EHT_STREAM_GROUPS)
95
96 #define IEEE80211_HT_GROUP_0 0
97 #define IEEE80211_VHT_GROUP_0 (IEEE80211_HT_GROUP_0 + IEEE80211_HT_GROUPS_NB)
98 #define IEEE80211_HE_GROUP_0 (IEEE80211_VHT_GROUP_0 + IEEE80211_VHT_GROUPS_NB)
99 #define IEEE80211_EHT_GROUP_0 (IEEE80211_HE_GROUP_0 + IEEE80211_HE_GROUPS_NB)
100
101 #define MCS_GROUP_RATES 14
102
103 #define HT_GROUP_IDX(_streams, _sgi, _ht40) \
104 IEEE80211_HT_GROUP_0 + \
105 IEEE80211_MAX_STREAMS * 2 * _ht40 + \
106 IEEE80211_MAX_STREAMS * _sgi + \
107 _streams - 1
108
109 #define _MAX(a, b) (((a)>(b))?(a):(b))
110
111 #define GROUP_SHIFT(duration) \
112 _MAX(0, 16 - __builtin_clz(duration))
113
114 /* MCS rate information for an MCS group */
115 #define __MCS_GROUP(_streams, _sgi, _ht40, _s) \
116 [HT_GROUP_IDX(_streams, _sgi, _ht40)] = { \
117 .shift = _s, \
118 .duration = { \
119 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 54 : 26), \
120 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 108 : 52), \
121 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 162 : 78), \
122 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 216 : 104), \
123 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 324 : 156), \
124 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 432 : 208), \
125 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 486 : 234), \
126 MCS_DURATION_S(_s, _streams, _sgi, _ht40 ? 540 : 260) \
127 } \
128 }
129
130 #define MCS_GROUP_SHIFT(_streams, _sgi, _ht40) \
131 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, _ht40 ? 54 : 26))
132
133 #define MCS_GROUP(_streams, _sgi, _ht40) \
134 __MCS_GROUP(_streams, _sgi, _ht40, \
135 MCS_GROUP_SHIFT(_streams, _sgi, _ht40))
136
137 #define VHT_GROUP_IDX(_streams, _sgi, _bw) \
138 (IEEE80211_VHT_GROUP_0 + \
139 IEEE80211_MAX_STREAMS * 2 * (_bw) + \
140 IEEE80211_MAX_STREAMS * (_sgi) + \
141 (_streams) - 1)
142
143 #define BW2VBPS(_bw, r4, r3, r2, r1) \
144 (_bw == BW_160 ? r4 : _bw == BW_80 ? r3 : _bw == BW_40 ? r2 : r1)
145
146 #define __VHT_GROUP(_streams, _sgi, _bw, _s) \
147 [VHT_GROUP_IDX(_streams, _sgi, _bw)] = { \
148 .shift = _s, \
149 .duration = { \
150 MCS_DURATION_S(_s, _streams, _sgi, \
151 BW2VBPS(_bw, 234, 117, 54, 26)), \
152 MCS_DURATION_S(_s, _streams, _sgi, \
153 BW2VBPS(_bw, 468, 234, 108, 52)), \
154 MCS_DURATION_S(_s, _streams, _sgi, \
155 BW2VBPS(_bw, 702, 351, 162, 78)), \
156 MCS_DURATION_S(_s, _streams, _sgi, \
157 BW2VBPS(_bw, 936, 468, 216, 104)), \
158 MCS_DURATION_S(_s, _streams, _sgi, \
159 BW2VBPS(_bw, 1404, 702, 324, 156)), \
160 MCS_DURATION_S(_s, _streams, _sgi, \
161 BW2VBPS(_bw, 1872, 936, 432, 208)), \
162 MCS_DURATION_S(_s, _streams, _sgi, \
163 BW2VBPS(_bw, 2106, 1053, 486, 234)), \
164 MCS_DURATION_S(_s, _streams, _sgi, \
165 BW2VBPS(_bw, 2340, 1170, 540, 260)), \
166 MCS_DURATION_S(_s, _streams, _sgi, \
167 BW2VBPS(_bw, 2808, 1404, 648, 312)), \
168 MCS_DURATION_S(_s, _streams, _sgi, \
169 BW2VBPS(_bw, 3120, 1560, 720, 346)) \
170 } \
171 }
172
173 #define VHT_GROUP_SHIFT(_streams, _sgi, _bw) \
174 GROUP_SHIFT(MCS_DURATION(_streams, _sgi, \
175 BW2VBPS(_bw, 243, 117, 54, 26)))
176
177 #define VHT_GROUP(_streams, _sgi, _bw) \
178 __VHT_GROUP(_streams, _sgi, _bw, \
179 VHT_GROUP_SHIFT(_streams, _sgi, _bw))
180
181
182 #define HE_GROUP_IDX(_streams, _gi, _bw) \
183 (IEEE80211_HE_GROUP_0 + \
184 IEEE80211_HE_MAX_STREAMS * 3 * (_bw) + \
185 IEEE80211_HE_MAX_STREAMS * (_gi) + \
186 (_streams) - 1)
187
188 #define __HE_GROUP(_streams, _gi, _bw, _s) \
189 [HE_GROUP_IDX(_streams, _gi, _bw)] = { \
190 .shift = _s, \
191 .duration = { \
192 HE_DURATION_S(_s, _streams, _gi, \
193 BW2VBPS(_bw, 979, 489, 230, 115)), \
194 HE_DURATION_S(_s, _streams, _gi, \
195 BW2VBPS(_bw, 1958, 979, 475, 230)), \
196 HE_DURATION_S(_s, _streams, _gi, \
197 BW2VBPS(_bw, 2937, 1468, 705, 345)), \
198 HE_DURATION_S(_s, _streams, _gi, \
199 BW2VBPS(_bw, 3916, 1958, 936, 475)), \
200 HE_DURATION_S(_s, _streams, _gi, \
201 BW2VBPS(_bw, 5875, 2937, 1411, 705)), \
202 HE_DURATION_S(_s, _streams, _gi, \
203 BW2VBPS(_bw, 7833, 3916, 1872, 936)), \
204 HE_DURATION_S(_s, _streams, _gi, \
205 BW2VBPS(_bw, 8827, 4406, 2102, 1051)), \
206 HE_DURATION_S(_s, _streams, _gi, \
207 BW2VBPS(_bw, 9806, 4896, 2347, 1166)), \
208 HE_DURATION_S(_s, _streams, _gi, \
209 BW2VBPS(_bw, 11764, 5875, 2808, 1411)), \
210 HE_DURATION_S(_s, _streams, _gi, \
211 BW2VBPS(_bw, 13060, 6523, 3124, 1555)), \
212 HE_DURATION_S(_s, _streams, _gi, \
213 BW2VBPS(_bw, 14702, 7344, 3513, 1756)), \
214 HE_DURATION_S(_s, _streams, _gi, \
215 BW2VBPS(_bw, 16329, 8164, 3902, 1944)) \
216 } \
217 }
218
219 #define HE_GROUP_SHIFT(_streams, _gi, _bw) \
220 GROUP_SHIFT(HE_DURATION(_streams, _gi, \
221 BW2VBPS(_bw, 979, 489, 230, 115)))
222
223 #define HE_GROUP(_streams, _gi, _bw) \
224 __HE_GROUP(_streams, _gi, _bw, \
225 HE_GROUP_SHIFT(_streams, _gi, _bw))
226
227 #define EHT_BW2VBPS(_bw, r5, r4, r3, r2, r1) \
228 ((_bw) == BW_320 ? r5 : BW2VBPS(_bw, r4, r3, r2, r1))
229
230 #define EHT_GROUP_IDX(_streams, _gi, _bw) \
231 (IEEE80211_EHT_GROUP_0 + \
232 IEEE80211_EHT_MAX_STREAMS * 3 * (_bw) + \
233 IEEE80211_EHT_MAX_STREAMS * (_gi) + \
234 (_streams) - 1)
235
236 #define __EHT_GROUP(_streams, _gi, _bw, _s) \
237 [EHT_GROUP_IDX(_streams, _gi, _bw)] = { \
238 .shift = _s, \
239 .duration = { \
240 EHT_DURATION_S(_s, _streams, _gi, \
241 EHT_BW2VBPS(_bw, 1960, 980, 490, 234, 117)), \
242 EHT_DURATION_S(_s, _streams, _gi, \
243 EHT_BW2VBPS(_bw, 3920, 1960, 980, 468, 234)), \
244 EHT_DURATION_S(_s, _streams, _gi, \
245 EHT_BW2VBPS(_bw, 5880, 2937, 1470, 702, 351)), \
246 EHT_DURATION_S(_s, _streams, _gi, \
247 EHT_BW2VBPS(_bw, 7840, 3920, 1960, 936, 468)), \
248 EHT_DURATION_S(_s, _streams, _gi, \
249 EHT_BW2VBPS(_bw, 11760, 5880, 2940, 1404, 702)), \
250 EHT_DURATION_S(_s, _streams, _gi, \
251 EHT_BW2VBPS(_bw, 15680, 7840, 3920, 1872, 936)), \
252 EHT_DURATION_S(_s, _streams, _gi, \
253 EHT_BW2VBPS(_bw, 17640, 8820, 4410, 2106, 1053)), \
254 EHT_DURATION_S(_s, _streams, _gi, \
255 EHT_BW2VBPS(_bw, 19600, 9800, 4900, 2340, 1170)), \
256 EHT_DURATION_S(_s, _streams, _gi, \
257 EHT_BW2VBPS(_bw, 23520, 11760, 5880, 2808, 1404)), \
258 EHT_DURATION_S(_s, _streams, _gi, \
259 EHT_BW2VBPS(_bw, 26133, 13066, 6533, 3120, 1560)), \
260 EHT_DURATION_S(_s, _streams, _gi, \
261 EHT_BW2VBPS(_bw, 29400, 14700, 7350, 3510, 1755)), \
262 EHT_DURATION_S(_s, _streams, _gi, \
263 EHT_BW2VBPS(_bw, 32666, 16333, 8166, 3900, 1950)), \
264 EHT_DURATION_S(_s, _streams, _gi, \
265 EHT_BW2VBPS(_bw, 35280, 17640, 8820, 4212, 2106)), \
266 EHT_DURATION_S(_s, _streams, _gi, \
267 EHT_BW2VBPS(_bw, 39200, 19600, 9800, 4680, 2340)) \
268 } \
269 }
270
271 #define EHT_GROUP_SHIFT(_streams, _gi, _bw) \
272 GROUP_SHIFT(EHT_DURATION(_streams, _gi, \
273 EHT_BW2VBPS(_bw, 1960, 980, 490, 234, 117)))
274
275 #define EHT_GROUP(_streams, _gi, _bw) \
276 __EHT_GROUP(_streams, _gi, _bw, \
277 EHT_GROUP_SHIFT(_streams, _gi, _bw))
278
279 #define EHT_GROUP_RANGE(_gi, _bw) \
280 EHT_GROUP(1, _gi, _bw), \
281 EHT_GROUP(2, _gi, _bw), \
282 EHT_GROUP(3, _gi, _bw), \
283 EHT_GROUP(4, _gi, _bw), \
284 EHT_GROUP(5, _gi, _bw), \
285 EHT_GROUP(6, _gi, _bw), \
286 EHT_GROUP(7, _gi, _bw), \
287 EHT_GROUP(8, _gi, _bw)
288
289 struct mcs_group {
290 u8 shift;
291 u16 duration[MCS_GROUP_RATES];
292 };
293
294 static const struct mcs_group airtime_mcs_groups[] = {
295 MCS_GROUP(1, 0, BW_20),
296 MCS_GROUP(2, 0, BW_20),
297 MCS_GROUP(3, 0, BW_20),
298 MCS_GROUP(4, 0, BW_20),
299
300 MCS_GROUP(1, 1, BW_20),
301 MCS_GROUP(2, 1, BW_20),
302 MCS_GROUP(3, 1, BW_20),
303 MCS_GROUP(4, 1, BW_20),
304
305 MCS_GROUP(1, 0, BW_40),
306 MCS_GROUP(2, 0, BW_40),
307 MCS_GROUP(3, 0, BW_40),
308 MCS_GROUP(4, 0, BW_40),
309
310 MCS_GROUP(1, 1, BW_40),
311 MCS_GROUP(2, 1, BW_40),
312 MCS_GROUP(3, 1, BW_40),
313 MCS_GROUP(4, 1, BW_40),
314
315 VHT_GROUP(1, 0, BW_20),
316 VHT_GROUP(2, 0, BW_20),
317 VHT_GROUP(3, 0, BW_20),
318 VHT_GROUP(4, 0, BW_20),
319
320 VHT_GROUP(1, 1, BW_20),
321 VHT_GROUP(2, 1, BW_20),
322 VHT_GROUP(3, 1, BW_20),
323 VHT_GROUP(4, 1, BW_20),
324
325 VHT_GROUP(1, 0, BW_40),
326 VHT_GROUP(2, 0, BW_40),
327 VHT_GROUP(3, 0, BW_40),
328 VHT_GROUP(4, 0, BW_40),
329
330 VHT_GROUP(1, 1, BW_40),
331 VHT_GROUP(2, 1, BW_40),
332 VHT_GROUP(3, 1, BW_40),
333 VHT_GROUP(4, 1, BW_40),
334
335 VHT_GROUP(1, 0, BW_80),
336 VHT_GROUP(2, 0, BW_80),
337 VHT_GROUP(3, 0, BW_80),
338 VHT_GROUP(4, 0, BW_80),
339
340 VHT_GROUP(1, 1, BW_80),
341 VHT_GROUP(2, 1, BW_80),
342 VHT_GROUP(3, 1, BW_80),
343 VHT_GROUP(4, 1, BW_80),
344
345 VHT_GROUP(1, 0, BW_160),
346 VHT_GROUP(2, 0, BW_160),
347 VHT_GROUP(3, 0, BW_160),
348 VHT_GROUP(4, 0, BW_160),
349
350 VHT_GROUP(1, 1, BW_160),
351 VHT_GROUP(2, 1, BW_160),
352 VHT_GROUP(3, 1, BW_160),
353 VHT_GROUP(4, 1, BW_160),
354
355 HE_GROUP(1, HE_GI_08, BW_20),
356 HE_GROUP(2, HE_GI_08, BW_20),
357 HE_GROUP(3, HE_GI_08, BW_20),
358 HE_GROUP(4, HE_GI_08, BW_20),
359 HE_GROUP(5, HE_GI_08, BW_20),
360 HE_GROUP(6, HE_GI_08, BW_20),
361 HE_GROUP(7, HE_GI_08, BW_20),
362 HE_GROUP(8, HE_GI_08, BW_20),
363
364 HE_GROUP(1, HE_GI_16, BW_20),
365 HE_GROUP(2, HE_GI_16, BW_20),
366 HE_GROUP(3, HE_GI_16, BW_20),
367 HE_GROUP(4, HE_GI_16, BW_20),
368 HE_GROUP(5, HE_GI_16, BW_20),
369 HE_GROUP(6, HE_GI_16, BW_20),
370 HE_GROUP(7, HE_GI_16, BW_20),
371 HE_GROUP(8, HE_GI_16, BW_20),
372
373 HE_GROUP(1, HE_GI_32, BW_20),
374 HE_GROUP(2, HE_GI_32, BW_20),
375 HE_GROUP(3, HE_GI_32, BW_20),
376 HE_GROUP(4, HE_GI_32, BW_20),
377 HE_GROUP(5, HE_GI_32, BW_20),
378 HE_GROUP(6, HE_GI_32, BW_20),
379 HE_GROUP(7, HE_GI_32, BW_20),
380 HE_GROUP(8, HE_GI_32, BW_20),
381
382 HE_GROUP(1, HE_GI_08, BW_40),
383 HE_GROUP(2, HE_GI_08, BW_40),
384 HE_GROUP(3, HE_GI_08, BW_40),
385 HE_GROUP(4, HE_GI_08, BW_40),
386 HE_GROUP(5, HE_GI_08, BW_40),
387 HE_GROUP(6, HE_GI_08, BW_40),
388 HE_GROUP(7, HE_GI_08, BW_40),
389 HE_GROUP(8, HE_GI_08, BW_40),
390
391 HE_GROUP(1, HE_GI_16, BW_40),
392 HE_GROUP(2, HE_GI_16, BW_40),
393 HE_GROUP(3, HE_GI_16, BW_40),
394 HE_GROUP(4, HE_GI_16, BW_40),
395 HE_GROUP(5, HE_GI_16, BW_40),
396 HE_GROUP(6, HE_GI_16, BW_40),
397 HE_GROUP(7, HE_GI_16, BW_40),
398 HE_GROUP(8, HE_GI_16, BW_40),
399
400 HE_GROUP(1, HE_GI_32, BW_40),
401 HE_GROUP(2, HE_GI_32, BW_40),
402 HE_GROUP(3, HE_GI_32, BW_40),
403 HE_GROUP(4, HE_GI_32, BW_40),
404 HE_GROUP(5, HE_GI_32, BW_40),
405 HE_GROUP(6, HE_GI_32, BW_40),
406 HE_GROUP(7, HE_GI_32, BW_40),
407 HE_GROUP(8, HE_GI_32, BW_40),
408
409 HE_GROUP(1, HE_GI_08, BW_80),
410 HE_GROUP(2, HE_GI_08, BW_80),
411 HE_GROUP(3, HE_GI_08, BW_80),
412 HE_GROUP(4, HE_GI_08, BW_80),
413 HE_GROUP(5, HE_GI_08, BW_80),
414 HE_GROUP(6, HE_GI_08, BW_80),
415 HE_GROUP(7, HE_GI_08, BW_80),
416 HE_GROUP(8, HE_GI_08, BW_80),
417
418 HE_GROUP(1, HE_GI_16, BW_80),
419 HE_GROUP(2, HE_GI_16, BW_80),
420 HE_GROUP(3, HE_GI_16, BW_80),
421 HE_GROUP(4, HE_GI_16, BW_80),
422 HE_GROUP(5, HE_GI_16, BW_80),
423 HE_GROUP(6, HE_GI_16, BW_80),
424 HE_GROUP(7, HE_GI_16, BW_80),
425 HE_GROUP(8, HE_GI_16, BW_80),
426
427 HE_GROUP(1, HE_GI_32, BW_80),
428 HE_GROUP(2, HE_GI_32, BW_80),
429 HE_GROUP(3, HE_GI_32, BW_80),
430 HE_GROUP(4, HE_GI_32, BW_80),
431 HE_GROUP(5, HE_GI_32, BW_80),
432 HE_GROUP(6, HE_GI_32, BW_80),
433 HE_GROUP(7, HE_GI_32, BW_80),
434 HE_GROUP(8, HE_GI_32, BW_80),
435
436 HE_GROUP(1, HE_GI_08, BW_160),
437 HE_GROUP(2, HE_GI_08, BW_160),
438 HE_GROUP(3, HE_GI_08, BW_160),
439 HE_GROUP(4, HE_GI_08, BW_160),
440 HE_GROUP(5, HE_GI_08, BW_160),
441 HE_GROUP(6, HE_GI_08, BW_160),
442 HE_GROUP(7, HE_GI_08, BW_160),
443 HE_GROUP(8, HE_GI_08, BW_160),
444
445 HE_GROUP(1, HE_GI_16, BW_160),
446 HE_GROUP(2, HE_GI_16, BW_160),
447 HE_GROUP(3, HE_GI_16, BW_160),
448 HE_GROUP(4, HE_GI_16, BW_160),
449 HE_GROUP(5, HE_GI_16, BW_160),
450 HE_GROUP(6, HE_GI_16, BW_160),
451 HE_GROUP(7, HE_GI_16, BW_160),
452 HE_GROUP(8, HE_GI_16, BW_160),
453
454 HE_GROUP(1, HE_GI_32, BW_160),
455 HE_GROUP(2, HE_GI_32, BW_160),
456 HE_GROUP(3, HE_GI_32, BW_160),
457 HE_GROUP(4, HE_GI_32, BW_160),
458 HE_GROUP(5, HE_GI_32, BW_160),
459 HE_GROUP(6, HE_GI_32, BW_160),
460 HE_GROUP(7, HE_GI_32, BW_160),
461 HE_GROUP(8, HE_GI_32, BW_160),
462
463 EHT_GROUP_RANGE(EHT_GI_08, BW_20),
464 EHT_GROUP_RANGE(EHT_GI_16, BW_20),
465 EHT_GROUP_RANGE(EHT_GI_32, BW_20),
466
467 EHT_GROUP_RANGE(EHT_GI_08, BW_40),
468 EHT_GROUP_RANGE(EHT_GI_16, BW_40),
469 EHT_GROUP_RANGE(EHT_GI_32, BW_40),
470
471 EHT_GROUP_RANGE(EHT_GI_08, BW_80),
472 EHT_GROUP_RANGE(EHT_GI_16, BW_80),
473 EHT_GROUP_RANGE(EHT_GI_32, BW_80),
474
475 EHT_GROUP_RANGE(EHT_GI_08, BW_160),
476 EHT_GROUP_RANGE(EHT_GI_16, BW_160),
477 EHT_GROUP_RANGE(EHT_GI_32, BW_160),
478
479 EHT_GROUP_RANGE(EHT_GI_08, BW_320),
480 EHT_GROUP_RANGE(EHT_GI_16, BW_320),
481 EHT_GROUP_RANGE(EHT_GI_32, BW_320),
482 };
483
484 static u32
ieee80211_calc_legacy_rate_duration(u16 bitrate,bool short_pre,bool cck,int len)485 ieee80211_calc_legacy_rate_duration(u16 bitrate, bool short_pre,
486 bool cck, int len)
487 {
488 u32 duration;
489
490 if (cck) {
491 duration = 144 + 48; /* preamble + PLCP */
492 if (short_pre)
493 duration >>= 1;
494
495 duration += 10; /* SIFS */
496 } else {
497 duration = 20 + 16; /* premable + SIFS */
498 }
499
500 len <<= 3;
501 duration += (len * 10) / bitrate;
502
503 return duration;
504 }
505
ieee80211_get_rate_duration(struct ieee80211_hw * hw,struct ieee80211_rx_status * status,u32 * overhead)506 static u32 ieee80211_get_rate_duration(struct ieee80211_hw *hw,
507 struct ieee80211_rx_status *status,
508 u32 *overhead)
509 {
510 bool sgi = status->enc_flags & RX_ENC_FLAG_SHORT_GI;
511 int bw, streams;
512 int group, idx;
513 u32 duration;
514
515 switch (status->bw) {
516 case RATE_INFO_BW_20:
517 bw = BW_20;
518 break;
519 case RATE_INFO_BW_40:
520 bw = BW_40;
521 break;
522 case RATE_INFO_BW_80:
523 bw = BW_80;
524 break;
525 case RATE_INFO_BW_160:
526 bw = BW_160;
527 break;
528 case RATE_INFO_BW_320:
529 bw = BW_320;
530 break;
531 default:
532 WARN_ON_ONCE(1);
533 return 0;
534 }
535
536 switch (status->encoding) {
537 case RX_ENC_VHT:
538 streams = status->nss;
539 idx = status->rate_idx;
540 group = VHT_GROUP_IDX(streams, sgi, bw);
541 break;
542 case RX_ENC_HT:
543 streams = ((status->rate_idx >> 3) & 3) + 1;
544 idx = status->rate_idx & 7;
545 group = HT_GROUP_IDX(streams, sgi, bw);
546 break;
547 case RX_ENC_HE:
548 streams = status->nss;
549 idx = status->rate_idx;
550 group = HE_GROUP_IDX(streams, status->he_gi, bw);
551 break;
552 case RX_ENC_EHT:
553 streams = status->nss;
554 idx = status->rate_idx;
555 group = EHT_GROUP_IDX(streams, status->eht.gi, bw);
556 break;
557 default:
558 WARN_ON_ONCE(1);
559 return 0;
560 }
561
562 switch (status->encoding) {
563 case RX_ENC_EHT:
564 case RX_ENC_HE:
565 if (WARN_ON_ONCE(streams > 8))
566 return 0;
567 break;
568 default:
569 if (WARN_ON_ONCE(streams > 4))
570 return 0;
571 break;
572 }
573
574 if (idx >= MCS_GROUP_RATES)
575 return 0;
576
577 duration = airtime_mcs_groups[group].duration[idx];
578 duration <<= airtime_mcs_groups[group].shift;
579 *overhead = 36 + (streams << 2);
580
581 return duration;
582 }
583
584
ieee80211_calc_rx_airtime(struct ieee80211_hw * hw,struct ieee80211_rx_status * status,int len)585 u32 ieee80211_calc_rx_airtime(struct ieee80211_hw *hw,
586 struct ieee80211_rx_status *status,
587 int len)
588 {
589 struct ieee80211_supported_band *sband;
590 u32 duration, overhead = 0;
591
592 if (status->encoding == RX_ENC_LEGACY) {
593 const struct ieee80211_rate *rate;
594 bool sp = status->enc_flags & RX_ENC_FLAG_SHORTPRE;
595 bool cck;
596
597 /* on 60GHz or sub-1GHz band, there are no legacy rates */
598 if (WARN_ON_ONCE(status->band == NL80211_BAND_60GHZ ||
599 status->band == NL80211_BAND_S1GHZ))
600 return 0;
601
602 sband = hw->wiphy->bands[status->band];
603 if (!sband || status->rate_idx >= sband->n_bitrates)
604 return 0;
605
606 rate = &sband->bitrates[status->rate_idx];
607 cck = rate->flags & IEEE80211_RATE_MANDATORY_B;
608
609 return ieee80211_calc_legacy_rate_duration(rate->bitrate, sp,
610 cck, len);
611 }
612
613 duration = ieee80211_get_rate_duration(hw, status, &overhead);
614 if (!duration)
615 return 0;
616
617 duration *= len;
618 duration /= AVG_PKT_SIZE;
619 duration /= 1024;
620
621 return duration + overhead;
622 }
623 EXPORT_SYMBOL_GPL(ieee80211_calc_rx_airtime);
624
ieee80211_fill_rate_info(struct ieee80211_hw * hw,struct ieee80211_rx_status * stat,u8 band,struct rate_info * ri)625 static bool ieee80211_fill_rate_info(struct ieee80211_hw *hw,
626 struct ieee80211_rx_status *stat, u8 band,
627 struct rate_info *ri)
628 {
629 struct ieee80211_supported_band *sband = hw->wiphy->bands[band];
630 int i;
631
632 if (!ri || !sband)
633 return false;
634
635 stat->bw = ri->bw;
636 stat->nss = ri->nss;
637 stat->rate_idx = ri->mcs;
638
639 if (ri->flags & RATE_INFO_FLAGS_EHT_MCS)
640 stat->encoding = RX_ENC_EHT;
641 else if (ri->flags & RATE_INFO_FLAGS_HE_MCS)
642 stat->encoding = RX_ENC_HE;
643 else if (ri->flags & RATE_INFO_FLAGS_VHT_MCS)
644 stat->encoding = RX_ENC_VHT;
645 else if (ri->flags & RATE_INFO_FLAGS_MCS)
646 stat->encoding = RX_ENC_HT;
647 else
648 stat->encoding = RX_ENC_LEGACY;
649
650 if (ri->flags & RATE_INFO_FLAGS_SHORT_GI)
651 stat->enc_flags |= RX_ENC_FLAG_SHORT_GI;
652
653 switch (stat->encoding) {
654 case RX_ENC_EHT:
655 stat->eht.gi = ri->eht_gi;
656 break;
657 default:
658 stat->he_gi = ri->he_gi;
659 break;
660 }
661
662 if (stat->encoding != RX_ENC_LEGACY)
663 return true;
664
665 stat->rate_idx = 0;
666 for (i = 0; i < sband->n_bitrates; i++) {
667 if (ri->legacy != sband->bitrates[i].bitrate)
668 continue;
669
670 stat->rate_idx = i;
671 return true;
672 }
673
674 return false;
675 }
676
ieee80211_fill_rx_status(struct ieee80211_rx_status * stat,struct ieee80211_hw * hw,struct ieee80211_tx_rate * rate,struct rate_info * ri,u8 band,int len)677 static int ieee80211_fill_rx_status(struct ieee80211_rx_status *stat,
678 struct ieee80211_hw *hw,
679 struct ieee80211_tx_rate *rate,
680 struct rate_info *ri, u8 band, int len)
681 {
682 memset(stat, 0, sizeof(*stat));
683 stat->band = band;
684
685 if (ieee80211_fill_rate_info(hw, stat, band, ri))
686 return 0;
687
688 if (!ieee80211_rate_valid(rate))
689 return -1;
690
691 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
692 stat->bw = RATE_INFO_BW_160;
693 else if (rate->flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
694 stat->bw = RATE_INFO_BW_80;
695 else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
696 stat->bw = RATE_INFO_BW_40;
697 else
698 stat->bw = RATE_INFO_BW_20;
699
700 stat->enc_flags = 0;
701 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
702 stat->enc_flags |= RX_ENC_FLAG_SHORTPRE;
703 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
704 stat->enc_flags |= RX_ENC_FLAG_SHORT_GI;
705
706 stat->rate_idx = rate->idx;
707 if (rate->flags & IEEE80211_TX_RC_VHT_MCS) {
708 stat->encoding = RX_ENC_VHT;
709 stat->rate_idx = ieee80211_rate_get_vht_mcs(rate);
710 stat->nss = ieee80211_rate_get_vht_nss(rate);
711 } else if (rate->flags & IEEE80211_TX_RC_MCS) {
712 stat->encoding = RX_ENC_HT;
713 } else {
714 stat->encoding = RX_ENC_LEGACY;
715 }
716
717 return 0;
718 }
719
ieee80211_calc_tx_airtime_rate(struct ieee80211_hw * hw,struct ieee80211_tx_rate * rate,struct rate_info * ri,u8 band,int len)720 static u32 ieee80211_calc_tx_airtime_rate(struct ieee80211_hw *hw,
721 struct ieee80211_tx_rate *rate,
722 struct rate_info *ri,
723 u8 band, int len)
724 {
725 struct ieee80211_rx_status stat;
726
727 if (ieee80211_fill_rx_status(&stat, hw, rate, ri, band, len))
728 return 0;
729
730 return ieee80211_calc_rx_airtime(hw, &stat, len);
731 }
732
ieee80211_calc_tx_airtime(struct ieee80211_hw * hw,struct ieee80211_tx_info * info,int len)733 u32 ieee80211_calc_tx_airtime(struct ieee80211_hw *hw,
734 struct ieee80211_tx_info *info,
735 int len)
736 {
737 u32 duration = 0;
738 int i;
739
740 for (i = 0; i < ARRAY_SIZE(info->status.rates); i++) {
741 struct ieee80211_tx_rate *rate = &info->status.rates[i];
742 u32 cur_duration;
743
744 cur_duration = ieee80211_calc_tx_airtime_rate(hw, rate, NULL,
745 info->band, len);
746 if (!cur_duration)
747 break;
748
749 duration += cur_duration * rate->count;
750 }
751
752 return duration;
753 }
754 EXPORT_SYMBOL_GPL(ieee80211_calc_tx_airtime);
755
ieee80211_calc_expected_tx_airtime(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * pubsta,int len,bool ampdu)756 u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
757 struct ieee80211_vif *vif,
758 struct ieee80211_sta *pubsta,
759 int len, bool ampdu)
760 {
761 struct ieee80211_supported_band *sband;
762 struct ieee80211_chanctx_conf *conf;
763 int rateidx;
764 bool cck, short_pream;
765 u32 basic_rates;
766 u8 band = 0;
767 u16 rate;
768
769 len += 38; /* Ethernet header length */
770
771 conf = rcu_dereference(vif->bss_conf.chanctx_conf);
772 if (conf)
773 band = conf->def.chan->band;
774
775 if (pubsta) {
776 struct sta_info *sta = container_of(pubsta, struct sta_info,
777 sta);
778 struct ieee80211_rx_status stat;
779 struct ieee80211_tx_rate *tx_rate = &sta->deflink.tx_stats.last_rate;
780 struct rate_info *ri = &sta->deflink.tx_stats.last_rate_info;
781 u32 duration, overhead;
782 u8 agg_shift;
783
784 if (ieee80211_fill_rx_status(&stat, hw, tx_rate, ri, band, len))
785 return 0;
786
787 if (stat.encoding == RX_ENC_LEGACY || !ampdu)
788 return ieee80211_calc_rx_airtime(hw, &stat, len);
789
790 duration = ieee80211_get_rate_duration(hw, &stat, &overhead);
791 /*
792 * Assume that HT/VHT transmission on any AC except VO will
793 * use aggregation. Since we don't have reliable reporting
794 * of aggregation length, assume an average size based on the
795 * tx rate.
796 * This will not be very accurate, but much better than simply
797 * assuming un-aggregated tx in all cases.
798 */
799 if (duration > 400 * 1024) /* <= VHT20 MCS2 1S */
800 agg_shift = 1;
801 else if (duration > 250 * 1024) /* <= VHT20 MCS3 1S or MCS1 2S */
802 agg_shift = 2;
803 else if (duration > 150 * 1024) /* <= VHT20 MCS5 1S or MCS2 2S */
804 agg_shift = 3;
805 else if (duration > 70 * 1024) /* <= VHT20 MCS5 2S */
806 agg_shift = 4;
807 else if (stat.encoding != RX_ENC_HE ||
808 duration > 20 * 1024) /* <= HE40 MCS6 2S */
809 agg_shift = 5;
810 else
811 agg_shift = 6;
812
813 duration *= len;
814 duration /= AVG_PKT_SIZE;
815 duration /= 1024;
816 duration += (overhead >> agg_shift);
817
818 return max_t(u32, duration, 4);
819 }
820
821 if (!conf)
822 return 0;
823
824 /* No station to get latest rate from, so calculate the worst-case
825 * duration using the lowest configured basic rate.
826 */
827 sband = hw->wiphy->bands[band];
828
829 basic_rates = vif->bss_conf.basic_rates;
830 short_pream = vif->bss_conf.use_short_preamble;
831
832 rateidx = basic_rates ? ffs(basic_rates) - 1 : 0;
833 rate = sband->bitrates[rateidx].bitrate;
834 cck = sband->bitrates[rateidx].flags & IEEE80211_RATE_MANDATORY_B;
835
836 return ieee80211_calc_legacy_rate_duration(rate, short_pream, cck, len);
837 }
838