xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.h (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (c) 2005 John Bicket
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD$
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 };
55 #define	ATH_SOFTC_SAMPLE(sc)	((struct sample_softc *)sc->sc_rc)
56 
57 struct rate_stats {
58 	unsigned average_tx_time;
59 	int successive_failures;
60 	int tries;
61 	int total_packets;
62 	int packets_acked;
63 	unsigned perfect_tx_time; /* transmit time for 0 retries */
64 	int last_tx;
65 };
66 
67 struct txschedule {
68 	uint8_t	t0, r0;		/* series 0: tries, rate code */
69 	uint8_t	t1, r1;		/* series 1: tries, rate code */
70 	uint8_t	t2, r2;		/* series 2: tries, rate code */
71 	uint8_t	t3, r3;		/* series 3: tries, rate code */
72 };
73 
74 /*
75  * for now, we track performance for three different packet
76  * size buckets
77  */
78 #define NUM_PACKET_SIZE_BINS 2
79 
80 /* per-node state */
81 struct sample_node {
82 	int static_rix;			/* rate index of fixed tx rate */
83 #define	SAMPLE_MAXRATES	32		/* NB: corresponds to hal info[32] */
84 	uint32_t ratemask;		/* bit mask of valid rate indices */
85 	const struct txschedule *sched;	/* tx schedule table */
86 
87 	struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES];
88 	int last_sample_rix[NUM_PACKET_SIZE_BINS];
89 
90 	int current_sample_rix[NUM_PACKET_SIZE_BINS];
91 	int packets_sent[NUM_PACKET_SIZE_BINS];
92 
93 	int current_rix[NUM_PACKET_SIZE_BINS];
94 	int packets_since_switch[NUM_PACKET_SIZE_BINS];
95 	unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS];
96 
97 	int packets_since_sample[NUM_PACKET_SIZE_BINS];
98 	unsigned sample_tt[NUM_PACKET_SIZE_BINS];
99 };
100 #define	ATH_NODE_SAMPLE(an)	((struct sample_node *)&(an)[1])
101 #define	IS_RATE_DEFINED(sn, rix)	(((sn)->ratemask & (1<<(rix))) != 0)
102 
103 #ifndef MIN
104 #define	MIN(a,b)	((a) < (b) ? (a) : (b))
105 #endif
106 #ifndef MAX
107 #define	MAX(a,b)	((a) > (b) ? (a) : (b))
108 #endif
109 
110 #define WIFI_CW_MIN 31
111 #define WIFI_CW_MAX 1023
112 
113 /*
114  * Definitions for pulling the rate and trie counts from
115  * a 5212 h/w descriptor.  These Don't belong here; the
116  * driver should record this information so the rate control
117  * code doesn't go groveling around in the descriptor bits.
118  */
119 #define	ds_ctl2		ds_hw[0]
120 #define	ds_ctl3		ds_hw[1]
121 
122 /* TX ds_ctl2 */
123 #define	AR_XmitDataTries0	0x000f0000	/* series 0 max attempts */
124 #define	AR_XmitDataTries0_S	16
125 #define	AR_XmitDataTries1	0x00f00000	/* series 1 max attempts */
126 #define	AR_XmitDataTries1_S	20
127 #define	AR_XmitDataTries2	0x0f000000	/* series 2 max attempts */
128 #define	AR_XmitDataTries2_S	24
129 #define	AR_XmitDataTries3	0xf0000000	/* series 3 max attempts */
130 #define	AR_XmitDataTries3_S	28
131 
132 /* TX ds_ctl3 */
133 #define	AR_XmitRate0		0x0000001f	/* series 0 tx rate */
134 #define	AR_XmitRate0_S		0
135 #define	AR_XmitRate1		0x000003e0	/* series 1 tx rate */
136 #define	AR_XmitRate1_S		5
137 #define	AR_XmitRate2		0x00007c00	/* series 2 tx rate */
138 #define	AR_XmitRate2_S		10
139 #define	AR_XmitRate3		0x000f8000	/* series 3 tx rate */
140 #define	AR_XmitRate3_S		15
141 
142 /* TX ds_ctl3 for 5416 */
143 #define	AR5416_XmitRate0	0x000000ff	/* series 0 tx rate */
144 #define	AR5416_XmitRate0_S	0
145 #define	AR5416_XmitRate1	0x0000ff00	/* series 1 tx rate */
146 #define	AR5416_XmitRate1_S	8
147 #define	AR5416_XmitRate2	0x00ff0000	/* series 2 tx rate */
148 #define	AR5416_XmitRate2_S	16
149 #define	AR5416_XmitRate3	0xff000000	/* series 3 tx rate */
150 #define	AR5416_XmitRate3_S	24
151 
152 #define MS(_v, _f)	(((_v) & (_f)) >> _f##_S)
153 
154 /*
155  * Calculate the transmit duration of a frame.
156  */
157 static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
158 				int length,
159 				int rix, int short_retries, int long_retries) {
160 	const HAL_RATE_TABLE *rt = sc->sc_currates;
161 	struct ifnet *ifp = sc->sc_ifp;
162 	struct ieee80211com *ic = ifp->if_l2com;
163 	int rts, cts;
164 
165 	unsigned t_slot = 20;
166 	unsigned t_difs = 50;
167 	unsigned t_sifs = 10;
168 	int tt = 0;
169 	int x = 0;
170 	int cw = WIFI_CW_MIN;
171 	int cix;
172 
173 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
174 
175 	if (rix >= rt->rateCount) {
176 		printf("bogus rix %d, max %u, mode %u\n",
177 		       rix, rt->rateCount, sc->sc_curmode);
178 		return 0;
179 	}
180 	cix = rt->info[rix].controlRate;
181 	/*
182 	 * XXX getting mac/phy level timings should be fixed for turbo
183 	 * rates, and there is probably a way to get this from the
184 	 * hal...
185 	 */
186 	switch (rt->info[rix].phy) {
187 	case IEEE80211_T_OFDM:
188 		t_slot = 9;
189 		t_sifs = 16;
190 		t_difs = 28;
191 		/* fall through */
192 	case IEEE80211_T_TURBO:
193 		t_slot = 9;
194 		t_sifs = 8;
195 		t_difs = 28;
196 		break;
197 	case IEEE80211_T_DS:
198 		/* fall through to default */
199 	default:
200 		/* pg 205 ieee.802.11.pdf */
201 		t_slot = 20;
202 		t_difs = 50;
203 		t_sifs = 10;
204 	}
205 
206 	rts = cts = 0;
207 
208 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
209 	    rt->info[rix].phy == IEEE80211_T_OFDM) {
210 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
211 			rts = 1;
212 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
213 			cts = 1;
214 
215 		cix = rt->info[sc->sc_protrix].controlRate;
216 
217 	}
218 
219 	if (0 /*length > ic->ic_rtsthreshold */) {
220 		rts = 1;
221 	}
222 
223 	if (rts || cts) {
224 		int ctsrate;
225 		int ctsduration = 0;
226 
227 		/* NB: this is intentionally not a runtime check */
228 		KASSERT(cix < rt->rateCount,
229 		    ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,
230 		     sc->sc_curmode));
231 
232 		ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;
233 		if (rts)		/* SIFS + CTS */
234 			ctsduration += rt->info[cix].spAckDuration;
235 
236 		ctsduration += ath_hal_computetxtime(sc->sc_ah,
237 						     rt, length, rix, AH_TRUE);
238 
239 		if (cts)	/* SIFS + ACK */
240 			ctsduration += rt->info[cix].spAckDuration;
241 
242 		tt += (short_retries + 1) * ctsduration;
243 	}
244 	tt += t_difs;
245 	tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
246 	tt += (long_retries+1)*ath_hal_computetxtime(sc->sc_ah, rt, length,
247 						rix, AH_TRUE);
248 	for (x = 0; x <= short_retries + long_retries; x++) {
249 		cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);
250 		tt += (t_slot * cw/2);
251 	}
252 	return tt;
253 }
254 #endif /* _DEV_ATH_RATE_SAMPLE_H */
255