xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.h (revision ee7093a6400feef3d8a99579f28577817d70f6ad)
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 state */
48 	int	ath_smoothing_rate;	/* ewma percentage (out of 100) */
49 	int	ath_sample_rate;	/* send a different bit-rate 1/X packets */
50 };
51 #define	ATH_SOFTC_SAMPLE(sc)	((struct sample_softc *)sc->sc_rc)
52 
53 struct rate_info {
54 	int rate;
55 	int rix;
56 	int rateCode;
57 	int shortPreambleRateCode;
58 };
59 
60 
61 struct rate_stats {
62 	unsigned average_tx_time;
63 	int successive_failures;
64 	int tries;
65 	int total_packets;
66 	int packets_acked;
67 	unsigned perfect_tx_time; /* transmit time for 0 retries */
68 	int last_tx;
69 };
70 
71 /*
72  * for now, we track performance for three different packet
73  * size buckets
74  */
75 #define NUM_PACKET_SIZE_BINS 3
76 static int packet_size_bins[NUM_PACKET_SIZE_BINS] = {250, 1600, 3000};
77 
78 /* per-node state */
79 struct sample_node {
80 	int static_rate_ndx;
81 	int num_rates;
82 
83 	struct rate_info rates[IEEE80211_RATE_MAXSIZE];
84 
85 	struct rate_stats stats[NUM_PACKET_SIZE_BINS][IEEE80211_RATE_MAXSIZE];
86 	int last_sample_ndx[NUM_PACKET_SIZE_BINS];
87 
88 	int current_sample_ndx[NUM_PACKET_SIZE_BINS];
89 	int packets_sent[NUM_PACKET_SIZE_BINS];
90 
91 	int current_rate[NUM_PACKET_SIZE_BINS];
92 	int packets_since_switch[NUM_PACKET_SIZE_BINS];
93 	unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS];
94 
95 	int packets_since_sample[NUM_PACKET_SIZE_BINS];
96 	unsigned sample_tt[NUM_PACKET_SIZE_BINS];
97 };
98 #define	ATH_NODE_SAMPLE(an)	((struct sample_node *)&an[1])
99 
100 #ifndef MIN
101 #define	MIN(a,b)	((a) < (b) ? (a) : (b))
102 #endif
103 #ifndef MAX
104 #define	MAX(a,b)	((a) > (b) ? (a) : (b))
105 #endif
106 
107 #define WIFI_CW_MIN 31
108 #define WIFI_CW_MAX 1023
109 
110 /*
111  * Definitions for pulling the rate and trie counts from
112  * a 5212 h/w descriptor.  These Don't belong here; the
113  * driver should record this information so the rate control
114  * code doesn't go groveling around in the descriptor bits.
115  */
116 #define	ds_ctl2		ds_hw[0]
117 #define	ds_ctl3		ds_hw[1]
118 
119 /* TX ds_ctl2 */
120 #define	AR_XmitDataTries0	0x000f0000	/* series 0 max attempts */
121 #define	AR_XmitDataTries0_S	16
122 #define	AR_XmitDataTries1	0x00f00000	/* series 1 max attempts */
123 #define	AR_XmitDataTries1_S	20
124 #define	AR_XmitDataTries2	0x0f000000	/* series 2 max attempts */
125 #define	AR_XmitDataTries2_S	24
126 #define	AR_XmitDataTries3	0xf0000000	/* series 3 max attempts */
127 #define	AR_XmitDataTries3_S	28
128 
129 /* TX ds_ctl3 */
130 #define	AR_XmitRate0		0x0000001f	/* series 0 tx rate */
131 #define	AR_XmitRate0_S		0
132 #define	AR_XmitRate1		0x000003e0	/* series 1 tx rate */
133 #define	AR_XmitRate1_S		5
134 #define	AR_XmitRate2		0x00007c00	/* series 2 tx rate */
135 #define	AR_XmitRate2_S		10
136 #define	AR_XmitRate3		0x000f8000	/* series 3 tx rate */
137 #define	AR_XmitRate3_S		15
138 
139 #define MS(_v, _f)	(((_v) & (_f)) >> _f##_S)
140 
141 /*
142  * Calculate the transmit duration of a frame.
143  */
144 static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
145 				int length,
146 				int rix, int short_retries, int long_retries) {
147 	const HAL_RATE_TABLE *rt = sc->sc_currates;
148 	int rts, cts;
149 
150 	unsigned t_slot = 20;
151 	unsigned t_difs = 50;
152 	unsigned t_sifs = 10;
153 	struct ieee80211com *ic = &sc->sc_ic;
154 	int tt = 0;
155 	int x = 0;
156 	int cw = WIFI_CW_MIN;
157 	int cix;
158 
159 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
160 
161 	if (rix >= rt->rateCount) {
162 		printf("bogus rix %d, max %u, mode %u\n",
163 		       rix, rt->rateCount, sc->sc_curmode);
164 		return 0;
165 	}
166 	cix = rt->info[rix].controlRate;
167 	/*
168 	 * XXX getting mac/phy level timings should be fixed for turbo
169 	 * rates, and there is probably a way to get this from the
170 	 * hal...
171 	 */
172 	switch (rt->info[rix].phy) {
173 	case IEEE80211_T_OFDM:
174 		t_slot = 9;
175 		t_sifs = 16;
176 		t_difs = 28;
177 		/* fall through */
178 	case IEEE80211_T_TURBO:
179 		t_slot = 9;
180 		t_sifs = 8;
181 		t_difs = 28;
182 		break;
183 	case IEEE80211_T_DS:
184 		/* fall through to default */
185 	default:
186 		/* pg 205 ieee.802.11.pdf */
187 		t_slot = 20;
188 		t_difs = 50;
189 		t_sifs = 10;
190 	}
191 
192 	rts = cts = 0;
193 
194 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
195 	    rt->info[rix].phy == IEEE80211_T_OFDM) {
196 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
197 			rts = 1;
198 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
199 			cts = 1;
200 
201 		cix = rt->info[sc->sc_protrix].controlRate;
202 
203 	}
204 
205 	if (0 /*length > ic->ic_rtsthreshold */) {
206 		rts = 1;
207 	}
208 
209 	if (rts || cts) {
210 		int ctsrate;
211 		int ctsduration = 0;
212 
213 		/* NB: this is intentionally not a runtime check */
214 		KASSERT(cix < rt->rateCount,
215 		    ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,
216 		     sc->sc_curmode));
217 
218 		ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;
219 		if (rts)		/* SIFS + CTS */
220 			ctsduration += rt->info[cix].spAckDuration;
221 
222 		ctsduration += ath_hal_computetxtime(sc->sc_ah,
223 						     rt, length, rix, AH_TRUE);
224 
225 		if (cts)	/* SIFS + ACK */
226 			ctsduration += rt->info[cix].spAckDuration;
227 
228 		tt += (short_retries + 1) * ctsduration;
229 	}
230 	tt += t_difs;
231 	tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
232 	tt += (long_retries+1)*ath_hal_computetxtime(sc->sc_ah, rt, length,
233 						rix, AH_TRUE);
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 #endif /* _DEV_ATH_RATE_SAMPLE_H */
241