xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.h (revision cddbc3b40812213ff00041f79174cac0be360a2a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2005 John Bicket
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  * 3. Neither the names of the above-listed copyright holders nor the names
18  *    of any contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * Alternatively, this software may be distributed under the terms of the
22  * GNU General Public License ("GPL") version 2 as published by the Free
23  * Software Foundation.
24  *
25  * NO WARRANTY
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36  * THE POSSIBILITY OF SUCH DAMAGES.
37  *
38  * $FreeBSD$
39  */
40 
41 /*
42  * Defintions for the Atheros Wireless LAN controller driver.
43  */
44 #ifndef _DEV_ATH_RATE_SAMPLE_H
45 #define _DEV_ATH_RATE_SAMPLE_H
46 
47 /* per-device state */
48 struct sample_softc {
49 	struct ath_ratectrl arc;	/* base class */
50 	int	smoothing_rate;		/* ewma percentage [0..99] */
51 	int	smoothing_minpackets;
52 	int	sample_rate;		/* %time to try different tx rates */
53 	int	max_successive_failures;
54 	int	stale_failure_timeout;	/* how long to honor max_successive_failures */
55 	int	min_switch;		/* min time between rate changes */
56 	int	min_good_pct;		/* min good percentage for a rate to be considered */
57 };
58 #define	ATH_SOFTC_SAMPLE(sc)	((struct sample_softc *)sc->sc_rc)
59 
60 struct rate_stats {
61 	unsigned average_tx_time;
62 	int successive_failures;
63 	uint64_t tries;
64 	uint64_t total_packets;	/* pkts total since assoc */
65 	uint64_t packets_acked;	/* pkts acked since assoc */
66 	int ewma_pct;	/* EWMA percentage */
67 	unsigned perfect_tx_time; /* transmit time for 0 retries */
68 	int last_tx;
69 };
70 
71 struct txschedule {
72 	uint8_t	t0, r0;		/* series 0: tries, rate code */
73 	uint8_t	t1, r1;		/* series 1: tries, rate code */
74 	uint8_t	t2, r2;		/* series 2: tries, rate code */
75 	uint8_t	t3, r3;		/* series 3: tries, rate code */
76 };
77 
78 /*
79  * for now, we track performance for three different packet
80  * size buckets
81  */
82 #define NUM_PACKET_SIZE_BINS 2
83 
84 static const int packet_size_bins[NUM_PACKET_SIZE_BINS]  = { 250, 1600 };
85 
86 static inline int
87 bin_to_size(int index)
88 {
89 	return packet_size_bins[index];
90 }
91 
92 /* per-node state */
93 struct sample_node {
94 	int static_rix;			/* rate index of fixed tx rate */
95 #define	SAMPLE_MAXRATES	64		/* NB: corresponds to hal info[32] */
96 	uint64_t ratemask;		/* bit mask of valid rate indices */
97 	const struct txschedule *sched;	/* tx schedule table */
98 
99 	const HAL_RATE_TABLE *currates;
100 
101 	struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES];
102 	int last_sample_rix[NUM_PACKET_SIZE_BINS];
103 
104 	int current_sample_rix[NUM_PACKET_SIZE_BINS];
105 	int packets_sent[NUM_PACKET_SIZE_BINS];
106 
107 	int current_rix[NUM_PACKET_SIZE_BINS];
108 	int packets_since_switch[NUM_PACKET_SIZE_BINS];
109 	unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS];
110 
111 	int packets_since_sample[NUM_PACKET_SIZE_BINS];
112 	unsigned sample_tt[NUM_PACKET_SIZE_BINS];
113 };
114 
115 #ifdef	_KERNEL
116 
117 #define	ATH_NODE_SAMPLE(an)	((struct sample_node *)&(an)[1])
118 #define	IS_RATE_DEFINED(sn, rix)	(((uint64_t) (sn)->ratemask & (1ULL<<((uint64_t) rix))) != 0)
119 
120 #ifndef MIN
121 #define	MIN(a,b)	((a) < (b) ? (a) : (b))
122 #endif
123 #ifndef MAX
124 #define	MAX(a,b)	((a) > (b) ? (a) : (b))
125 #endif
126 
127 #define WIFI_CW_MIN 31
128 #define WIFI_CW_MAX 1023
129 
130 /*
131  * Calculate the transmit duration of a frame.
132  */
133 static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
134 				int length,
135 				int rix, int short_retries,
136 				int long_retries, int is_ht40)
137 {
138 	const HAL_RATE_TABLE *rt = sc->sc_currates;
139 	struct ieee80211com *ic = &sc->sc_ic;
140 	int rts, cts;
141 
142 	unsigned t_slot = 20;
143 	unsigned t_difs = 50;
144 	unsigned t_sifs = 10;
145 	int tt = 0;
146 	int x = 0;
147 	int cw = WIFI_CW_MIN;
148 	int cix;
149 
150 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
151 
152 	if (rix >= rt->rateCount) {
153 		printf("bogus rix %d, max %u, mode %u\n",
154 		       rix, rt->rateCount, sc->sc_curmode);
155 		return 0;
156 	}
157 	cix = rt->info[rix].controlRate;
158 	/*
159 	 * XXX getting mac/phy level timings should be fixed for turbo
160 	 * rates, and there is probably a way to get this from the
161 	 * hal...
162 	 */
163 	switch (rt->info[rix].phy) {
164 	case IEEE80211_T_OFDM:
165 		t_slot = 9;
166 		t_sifs = 16;
167 		t_difs = 28;
168 		/* fall through */
169 	case IEEE80211_T_TURBO:
170 		t_slot = 9;
171 		t_sifs = 8;
172 		t_difs = 28;
173 		break;
174 	case IEEE80211_T_HT:
175 		t_slot = 9;
176 		t_sifs = 8;
177 		t_difs = 28;
178 		break;
179 	case IEEE80211_T_DS:
180 		/* fall through to default */
181 	default:
182 		/* pg 205 ieee.802.11.pdf */
183 		t_slot = 20;
184 		t_difs = 50;
185 		t_sifs = 10;
186 	}
187 
188 	rts = cts = 0;
189 
190 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
191 	    rt->info[rix].phy == IEEE80211_T_OFDM) {
192 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
193 			rts = 1;
194 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
195 			cts = 1;
196 
197 		cix = rt->info[sc->sc_protrix].controlRate;
198 	}
199 
200 	if (0 /*length > ic->ic_rtsthreshold */) {
201 		rts = 1;
202 	}
203 
204 	if (rts || cts) {
205 		int ctsrate;
206 		int ctsduration = 0;
207 
208 		/* NB: this is intentionally not a runtime check */
209 		KASSERT(cix < rt->rateCount,
210 		    ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,
211 		     sc->sc_curmode));
212 
213 		ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;
214 		if (rts)		/* SIFS + CTS */
215 			ctsduration += rt->info[cix].spAckDuration;
216 
217 		/* XXX assumes short preamble, include SIFS */
218 		ctsduration += ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
219 		    is_ht40, 0, 1);
220 
221 		if (cts)	/* SIFS + ACK */
222 			ctsduration += rt->info[cix].spAckDuration;
223 
224 		tt += (short_retries + 1) * ctsduration;
225 	}
226 	tt += t_difs;
227 
228 	/* XXX assumes short preamble, include SIFS */
229 	tt += (long_retries+1)*ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
230 	    is_ht40, 0, 1);
231 
232 	tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
233 
234 	for (x = 0; x <= short_retries + long_retries; x++) {
235 		cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);
236 		tt += (t_slot * cw/2);
237 	}
238 	return tt;
239 }
240 
241 #endif	/* _KERNEL */
242 
243 #endif /* _DEV_ATH_RATE_SAMPLE_H */
244