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