xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 
39 /*
40  * Defintions for the Atheros Wireless LAN controller driver.
41  */
42 #ifndef _DEV_ATH_RATE_SAMPLE_H
43 #define _DEV_ATH_RATE_SAMPLE_H
44 
45 /* per-device state */
46 struct sample_softc {
47 	struct ath_ratectrl arc;	/* base class */
48 	int	smoothing_rate;		/* ewma percentage [0..99] */
49 	int	smoothing_minpackets;
50 	int	sample_rate;		/* %time to try different tx rates */
51 	int	max_successive_failures;
52 	int	stale_failure_timeout;	/* how long to honor max_successive_failures */
53 	int	min_switch;		/* min time between rate changes */
54 	int	min_good_pct;		/* min good percentage for a rate to be considered */
55 };
56 #define	ATH_SOFTC_SAMPLE(sc)	((struct sample_softc *)sc->sc_rc)
57 
58 struct rate_stats {
59 	unsigned average_tx_time;
60 	int successive_failures;
61 	uint64_t tries;
62 	uint64_t total_packets;	/* pkts total since assoc */
63 	uint64_t packets_acked;	/* pkts acked since assoc */
64 	int ewma_pct;	/* EWMA percentage */
65 	unsigned perfect_tx_time; /* transmit time for 0 retries */
66 	int last_tx;
67 };
68 
69 struct txschedule {
70 	uint8_t	t0, r0;		/* series 0: tries, rate code */
71 	uint8_t	t1, r1;		/* series 1: tries, rate code */
72 	uint8_t	t2, r2;		/* series 2: tries, rate code */
73 	uint8_t	t3, r3;		/* series 3: tries, rate code */
74 };
75 
76 /*
77  * We track performance for eight different packet size buckets.
78  */
79 #define NUM_PACKET_SIZE_BINS 7
80 
81 static const int packet_size_bins[NUM_PACKET_SIZE_BINS]  = { 250, 1600, 4096, 8192, 16384, 32768, 65536 };
82 
83 static inline int
bin_to_size(int index)84 bin_to_size(int index)
85 {
86 	return packet_size_bins[index];
87 }
88 
89 /* per-node state */
90 struct sample_node {
91 	int static_rix;			/* rate index of fixed tx rate */
92 #define	SAMPLE_MAXRATES	64		/* NB: corresponds to hal info[32] */
93 	uint64_t ratemask;		/* bit mask of valid rate indices */
94 	const struct txschedule *sched;	/* tx schedule table */
95 
96 	const HAL_RATE_TABLE *currates;
97 
98 	struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES];
99 	int last_sample_rix[NUM_PACKET_SIZE_BINS];
100 
101 	int current_sample_rix[NUM_PACKET_SIZE_BINS];
102 	int packets_sent[NUM_PACKET_SIZE_BINS];
103 
104 	int current_rix[NUM_PACKET_SIZE_BINS];
105 	int packets_since_switch[NUM_PACKET_SIZE_BINS];
106 	int ticks_since_switch[NUM_PACKET_SIZE_BINS];
107 
108 	int packets_since_sample[NUM_PACKET_SIZE_BINS];
109 	unsigned sample_tt[NUM_PACKET_SIZE_BINS];
110 };
111 
112 #ifdef	_KERNEL
113 
114 #define	ATH_NODE_SAMPLE(an)	((struct sample_node *)&(an)[1])
115 #define	IS_RATE_DEFINED(sn, rix)	(((uint64_t) (sn)->ratemask & (1ULL<<((uint64_t) rix))) != 0)
116 
117 #ifndef MIN
118 #define	MIN(a,b)	((a) < (b) ? (a) : (b))
119 #endif
120 #ifndef MAX
121 #define	MAX(a,b)	((a) > (b) ? (a) : (b))
122 #endif
123 
124 #define WIFI_CW_MIN 31
125 #define WIFI_CW_MAX 1023
126 
127 /*
128  * Calculate the transmit duration of a frame.
129  */
calc_usecs_unicast_packet(struct ath_softc * sc,int length,int rix,int short_retries,int long_retries,int is_ht40)130 static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
131 				int length,
132 				int rix, int short_retries,
133 				int long_retries, int is_ht40)
134 {
135 	const HAL_RATE_TABLE *rt = sc->sc_currates;
136 	struct ieee80211com *ic = &sc->sc_ic;
137 	int rts, cts;
138 
139 	unsigned t_slot = 20;
140 	unsigned t_difs = 50;
141 	unsigned t_sifs = 10;
142 	int tt = 0;
143 	int x = 0;
144 	int cw = WIFI_CW_MIN;
145 	int cix;
146 
147 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
148 
149 	if (rix >= rt->rateCount) {
150 		printf("bogus rix %d, max %u, mode %u\n",
151 		       rix, rt->rateCount, sc->sc_curmode);
152 		return 0;
153 	}
154 	cix = rt->info[rix].controlRate;
155 	/*
156 	 * XXX getting mac/phy level timings should be fixed for turbo
157 	 * rates, and there is probably a way to get this from the
158 	 * hal...
159 	 */
160 	switch (rt->info[rix].phy) {
161 	case IEEE80211_T_OFDM:
162 		t_slot = 9;
163 		t_sifs = 16;
164 		t_difs = 28;
165 		/* fall through */
166 	case IEEE80211_T_TURBO:
167 		t_slot = 9;
168 		t_sifs = 8;
169 		t_difs = 28;
170 		break;
171 	case IEEE80211_T_HT:
172 		t_slot = 9;
173 		t_sifs = 8;
174 		t_difs = 28;
175 		break;
176 	case IEEE80211_T_DS:
177 		/* fall through to default */
178 	default:
179 		/* pg 205 ieee.802.11.pdf */
180 		t_slot = 20;
181 		t_difs = 50;
182 		t_sifs = 10;
183 	}
184 
185 	rts = cts = 0;
186 
187 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
188 	    rt->info[rix].phy == IEEE80211_T_OFDM) {
189 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
190 			rts = 1;
191 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
192 			cts = 1;
193 
194 		cix = rt->info[sc->sc_protrix].controlRate;
195 	}
196 
197 	if (0 /*length > ic->ic_rtsthreshold */) {
198 		rts = 1;
199 	}
200 
201 	if (rts || cts) {
202 		int ctsrate;
203 		int ctsduration = 0;
204 
205 		/* NB: this is intentionally not a runtime check */
206 		KASSERT(cix < rt->rateCount,
207 		    ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,
208 		     sc->sc_curmode));
209 
210 		ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;
211 		if (rts)		/* SIFS + CTS */
212 			ctsduration += rt->info[cix].spAckDuration;
213 
214 		/* XXX assumes short preamble, include SIFS */
215 		ctsduration += ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
216 		    is_ht40, 0, 1);
217 
218 		if (cts)	/* SIFS + ACK */
219 			ctsduration += rt->info[cix].spAckDuration;
220 
221 		tt += (short_retries + 1) * ctsduration;
222 	}
223 	tt += t_difs;
224 
225 	/* XXX assumes short preamble, include SIFS */
226 	tt += (long_retries+1)*ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
227 	    is_ht40, 0, 1);
228 
229 	tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
230 
231 	for (x = 0; x <= short_retries + long_retries; x++) {
232 		cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);
233 		tt += (t_slot * cw/2);
234 	}
235 	return tt;
236 }
237 
238 #endif	/* _KERNEL */
239 
240 #endif /* _DEV_ATH_RATE_SAMPLE_H */
241