1572ff6f6SMatthew Dillon /*-
2572ff6f6SMatthew Dillon  * Copyright (c) 2005 John Bicket
3572ff6f6SMatthew Dillon  * All rights reserved.
4572ff6f6SMatthew Dillon  *
5572ff6f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6572ff6f6SMatthew Dillon  * modification, are permitted provided that the following conditions
7572ff6f6SMatthew Dillon  * are met:
8572ff6f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
9572ff6f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer,
10572ff6f6SMatthew Dillon  *    without modification.
11572ff6f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12572ff6f6SMatthew Dillon  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13572ff6f6SMatthew Dillon  *    redistribution must be conditioned upon including a substantially
14572ff6f6SMatthew Dillon  *    similar Disclaimer requirement for further binary redistribution.
15572ff6f6SMatthew Dillon  * 3. Neither the names of the above-listed copyright holders nor the names
16572ff6f6SMatthew Dillon  *    of any contributors may be used to endorse or promote products derived
17572ff6f6SMatthew Dillon  *    from this software without specific prior written permission.
18572ff6f6SMatthew Dillon  *
19572ff6f6SMatthew Dillon  * Alternatively, this software may be distributed under the terms of the
20572ff6f6SMatthew Dillon  * GNU General Public License ("GPL") version 2 as published by the Free
21572ff6f6SMatthew Dillon  * Software Foundation.
22572ff6f6SMatthew Dillon  *
23572ff6f6SMatthew Dillon  * NO WARRANTY
24572ff6f6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25572ff6f6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26*df052c2aSSascha Wildner  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY
27572ff6f6SMatthew Dillon  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28572ff6f6SMatthew Dillon  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29572ff6f6SMatthew Dillon  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30572ff6f6SMatthew Dillon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31572ff6f6SMatthew Dillon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32572ff6f6SMatthew Dillon  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33572ff6f6SMatthew Dillon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34572ff6f6SMatthew Dillon  * THE POSSIBILITY OF SUCH DAMAGES.
35572ff6f6SMatthew Dillon  *
36572ff6f6SMatthew Dillon  * $FreeBSD$
37572ff6f6SMatthew Dillon  */
38572ff6f6SMatthew Dillon 
39572ff6f6SMatthew Dillon /*
40572ff6f6SMatthew Dillon  * Defintions for the Atheros Wireless LAN controller driver.
41572ff6f6SMatthew Dillon  */
42572ff6f6SMatthew Dillon #ifndef _DEV_ATH_RATE_SAMPLE_H
43572ff6f6SMatthew Dillon #define _DEV_ATH_RATE_SAMPLE_H
44572ff6f6SMatthew Dillon 
45572ff6f6SMatthew Dillon /* per-device state */
46572ff6f6SMatthew Dillon struct sample_softc {
47572ff6f6SMatthew Dillon 	struct ath_ratectrl arc;	/* base class */
48572ff6f6SMatthew Dillon 	int	smoothing_rate;		/* ewma percentage [0..99] */
49572ff6f6SMatthew Dillon 	int	smoothing_minpackets;
50572ff6f6SMatthew Dillon 	int	sample_rate;		/* %time to try different tx rates */
51572ff6f6SMatthew Dillon 	int	max_successive_failures;
52572ff6f6SMatthew Dillon 	int	stale_failure_timeout;	/* how long to honor max_successive_failures */
53572ff6f6SMatthew Dillon 	int	min_switch;		/* min time between rate changes */
54572ff6f6SMatthew Dillon 	int	min_good_pct;		/* min good percentage for a rate to be considered */
55572ff6f6SMatthew Dillon };
56572ff6f6SMatthew Dillon #define	ATH_SOFTC_SAMPLE(sc)	((struct sample_softc *)sc->sc_rc)
57572ff6f6SMatthew Dillon 
58572ff6f6SMatthew Dillon struct rate_stats {
59572ff6f6SMatthew Dillon 	unsigned average_tx_time;
60572ff6f6SMatthew Dillon 	int successive_failures;
61572ff6f6SMatthew Dillon 	uint64_t tries;
62572ff6f6SMatthew Dillon 	uint64_t total_packets;	/* pkts total since assoc */
63572ff6f6SMatthew Dillon 	uint64_t packets_acked;	/* pkts acked since assoc */
64572ff6f6SMatthew Dillon 	int ewma_pct;	/* EWMA percentage */
65572ff6f6SMatthew Dillon 	unsigned perfect_tx_time; /* transmit time for 0 retries */
66572ff6f6SMatthew Dillon 	int last_tx;
67572ff6f6SMatthew Dillon };
68572ff6f6SMatthew Dillon 
69572ff6f6SMatthew Dillon struct txschedule {
70572ff6f6SMatthew Dillon 	uint8_t	t0, r0;		/* series 0: tries, rate code */
71572ff6f6SMatthew Dillon 	uint8_t	t1, r1;		/* series 1: tries, rate code */
72572ff6f6SMatthew Dillon 	uint8_t	t2, r2;		/* series 2: tries, rate code */
73572ff6f6SMatthew Dillon 	uint8_t	t3, r3;		/* series 3: tries, rate code */
74572ff6f6SMatthew Dillon };
75572ff6f6SMatthew Dillon 
76572ff6f6SMatthew Dillon /*
77572ff6f6SMatthew Dillon  * for now, we track performance for three different packet
78572ff6f6SMatthew Dillon  * size buckets
79572ff6f6SMatthew Dillon  */
80572ff6f6SMatthew Dillon #define NUM_PACKET_SIZE_BINS 2
81572ff6f6SMatthew Dillon 
82572ff6f6SMatthew Dillon static const int packet_size_bins[NUM_PACKET_SIZE_BINS]  = { 250, 1600 };
83572ff6f6SMatthew Dillon 
84572ff6f6SMatthew Dillon static inline int
bin_to_size(int index)85572ff6f6SMatthew Dillon bin_to_size(int index)
86572ff6f6SMatthew Dillon {
87572ff6f6SMatthew Dillon 	return packet_size_bins[index];
88572ff6f6SMatthew Dillon }
89572ff6f6SMatthew Dillon 
90572ff6f6SMatthew Dillon /* per-node state */
91572ff6f6SMatthew Dillon struct sample_node {
92572ff6f6SMatthew Dillon 	int static_rix;			/* rate index of fixed tx rate */
93572ff6f6SMatthew Dillon #define	SAMPLE_MAXRATES	64		/* NB: corresponds to hal info[32] */
94572ff6f6SMatthew Dillon 	uint64_t ratemask;		/* bit mask of valid rate indices */
95572ff6f6SMatthew Dillon 	const struct txschedule *sched;	/* tx schedule table */
96572ff6f6SMatthew Dillon 
97572ff6f6SMatthew Dillon 	const HAL_RATE_TABLE *currates;
98572ff6f6SMatthew Dillon 
99572ff6f6SMatthew Dillon 	struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES];
100572ff6f6SMatthew Dillon 	int last_sample_rix[NUM_PACKET_SIZE_BINS];
101572ff6f6SMatthew Dillon 
102572ff6f6SMatthew Dillon 	int current_sample_rix[NUM_PACKET_SIZE_BINS];
103572ff6f6SMatthew Dillon 	int packets_sent[NUM_PACKET_SIZE_BINS];
104572ff6f6SMatthew Dillon 
105572ff6f6SMatthew Dillon 	int current_rix[NUM_PACKET_SIZE_BINS];
106572ff6f6SMatthew Dillon 	int packets_since_switch[NUM_PACKET_SIZE_BINS];
107572ff6f6SMatthew Dillon 	unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS];
108572ff6f6SMatthew Dillon 
109572ff6f6SMatthew Dillon 	int packets_since_sample[NUM_PACKET_SIZE_BINS];
110572ff6f6SMatthew Dillon 	unsigned sample_tt[NUM_PACKET_SIZE_BINS];
111572ff6f6SMatthew Dillon };
112572ff6f6SMatthew Dillon 
113572ff6f6SMatthew Dillon #ifdef	_KERNEL
114572ff6f6SMatthew Dillon 
115572ff6f6SMatthew Dillon #define	ATH_NODE_SAMPLE(an)	((struct sample_node *)&(an)[1])
116b14ca477SMatthew Dillon #define	IS_RATE_DEFINED(sn, rix)	(((uint64_t)(sn)->ratemask & (1ULL<<((uint64_t) rix))) != 0)
117572ff6f6SMatthew Dillon 
118572ff6f6SMatthew Dillon #ifndef MIN
119572ff6f6SMatthew Dillon #define	MIN(a,b)	((a) < (b) ? (a) : (b))
120572ff6f6SMatthew Dillon #endif
121572ff6f6SMatthew Dillon #ifndef MAX
122572ff6f6SMatthew Dillon #define	MAX(a,b)	((a) > (b) ? (a) : (b))
123572ff6f6SMatthew Dillon #endif
124572ff6f6SMatthew Dillon 
125572ff6f6SMatthew Dillon #define WIFI_CW_MIN 31
126572ff6f6SMatthew Dillon #define WIFI_CW_MAX 1023
127572ff6f6SMatthew Dillon 
128572ff6f6SMatthew Dillon /*
129572ff6f6SMatthew Dillon  * Calculate the transmit duration of a frame.
130572ff6f6SMatthew Dillon  */
calc_usecs_unicast_packet(struct ath_softc * sc,int length,int rix,int short_retries,int long_retries,int is_ht40)131572ff6f6SMatthew Dillon static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
132572ff6f6SMatthew Dillon 				int length,
133572ff6f6SMatthew Dillon 				int rix, int short_retries,
134572ff6f6SMatthew Dillon 				int long_retries, int is_ht40)
135572ff6f6SMatthew Dillon {
136572ff6f6SMatthew Dillon 	const HAL_RATE_TABLE *rt = sc->sc_currates;
137b14ca477SMatthew Dillon 	struct ieee80211com *ic = &sc->sc_ic;
138572ff6f6SMatthew Dillon 	int rts, cts;
139572ff6f6SMatthew Dillon 
140572ff6f6SMatthew Dillon 	unsigned t_slot = 20;
141572ff6f6SMatthew Dillon 	unsigned t_difs = 50;
142572ff6f6SMatthew Dillon 	unsigned t_sifs = 10;
143572ff6f6SMatthew Dillon 	int tt = 0;
144572ff6f6SMatthew Dillon 	int x = 0;
145572ff6f6SMatthew Dillon 	int cw = WIFI_CW_MIN;
146572ff6f6SMatthew Dillon 	int cix;
147572ff6f6SMatthew Dillon 
148572ff6f6SMatthew Dillon 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
149572ff6f6SMatthew Dillon 
150572ff6f6SMatthew Dillon 	if (rix >= rt->rateCount) {
151dc249793SMatthew Dillon 		kprintf("bogus rix %d, max %u, mode %u\n",
152572ff6f6SMatthew Dillon 		       rix, rt->rateCount, sc->sc_curmode);
153572ff6f6SMatthew Dillon 		return 0;
154572ff6f6SMatthew Dillon 	}
155572ff6f6SMatthew Dillon 	cix = rt->info[rix].controlRate;
156572ff6f6SMatthew Dillon 	/*
157572ff6f6SMatthew Dillon 	 * XXX getting mac/phy level timings should be fixed for turbo
158572ff6f6SMatthew Dillon 	 * rates, and there is probably a way to get this from the
159572ff6f6SMatthew Dillon 	 * hal...
160572ff6f6SMatthew Dillon 	 */
161572ff6f6SMatthew Dillon 	switch (rt->info[rix].phy) {
162572ff6f6SMatthew Dillon 	case IEEE80211_T_OFDM:
163572ff6f6SMatthew Dillon 		t_slot = 9;
164572ff6f6SMatthew Dillon 		t_sifs = 16;
165572ff6f6SMatthew Dillon 		t_difs = 28;
166572ff6f6SMatthew Dillon 		/* fall through */
167572ff6f6SMatthew Dillon 	case IEEE80211_T_TURBO:
168572ff6f6SMatthew Dillon 		t_slot = 9;
169572ff6f6SMatthew Dillon 		t_sifs = 8;
170572ff6f6SMatthew Dillon 		t_difs = 28;
171572ff6f6SMatthew Dillon 		break;
172572ff6f6SMatthew Dillon 	case IEEE80211_T_HT:
173572ff6f6SMatthew Dillon 		t_slot = 9;
174572ff6f6SMatthew Dillon 		t_sifs = 8;
175572ff6f6SMatthew Dillon 		t_difs = 28;
176572ff6f6SMatthew Dillon 		break;
177572ff6f6SMatthew Dillon 	case IEEE80211_T_DS:
178572ff6f6SMatthew Dillon 		/* fall through to default */
179572ff6f6SMatthew Dillon 	default:
180572ff6f6SMatthew Dillon 		/* pg 205 ieee.802.11.pdf */
181572ff6f6SMatthew Dillon 		t_slot = 20;
182572ff6f6SMatthew Dillon 		t_difs = 50;
183572ff6f6SMatthew Dillon 		t_sifs = 10;
184572ff6f6SMatthew Dillon 	}
185572ff6f6SMatthew Dillon 
186572ff6f6SMatthew Dillon 	rts = cts = 0;
187572ff6f6SMatthew Dillon 
188572ff6f6SMatthew Dillon 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
189572ff6f6SMatthew Dillon 	    rt->info[rix].phy == IEEE80211_T_OFDM) {
190572ff6f6SMatthew Dillon 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
191572ff6f6SMatthew Dillon 			rts = 1;
192572ff6f6SMatthew Dillon 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
193572ff6f6SMatthew Dillon 			cts = 1;
194572ff6f6SMatthew Dillon 
195572ff6f6SMatthew Dillon 		cix = rt->info[sc->sc_protrix].controlRate;
196572ff6f6SMatthew Dillon 	}
197572ff6f6SMatthew Dillon 
198572ff6f6SMatthew Dillon 	if (0 /*length > ic->ic_rtsthreshold */) {
199572ff6f6SMatthew Dillon 		rts = 1;
200572ff6f6SMatthew Dillon 	}
201572ff6f6SMatthew Dillon 
202572ff6f6SMatthew Dillon 	if (rts || cts) {
203572ff6f6SMatthew Dillon 		int ctsrate;
204572ff6f6SMatthew Dillon 		int ctsduration = 0;
205572ff6f6SMatthew Dillon 
206572ff6f6SMatthew Dillon 		/* NB: this is intentionally not a runtime check */
207572ff6f6SMatthew Dillon 		KASSERT(cix < rt->rateCount,
208572ff6f6SMatthew Dillon 		    ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,
209572ff6f6SMatthew Dillon 		     sc->sc_curmode));
210572ff6f6SMatthew Dillon 
211572ff6f6SMatthew Dillon 		ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;
212572ff6f6SMatthew Dillon 		if (rts)		/* SIFS + CTS */
213572ff6f6SMatthew Dillon 			ctsduration += rt->info[cix].spAckDuration;
214572ff6f6SMatthew Dillon 
215572ff6f6SMatthew Dillon 		/* XXX assumes short preamble */
216572ff6f6SMatthew Dillon 		ctsduration += ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
217572ff6f6SMatthew Dillon 		    is_ht40, 0);
218572ff6f6SMatthew Dillon 
219572ff6f6SMatthew Dillon 		if (cts)	/* SIFS + ACK */
220572ff6f6SMatthew Dillon 			ctsduration += rt->info[cix].spAckDuration;
221572ff6f6SMatthew Dillon 
222572ff6f6SMatthew Dillon 		tt += (short_retries + 1) * ctsduration;
223572ff6f6SMatthew Dillon 	}
224572ff6f6SMatthew Dillon 	tt += t_difs;
225572ff6f6SMatthew Dillon 
226572ff6f6SMatthew Dillon 	/* XXX assumes short preamble */
227572ff6f6SMatthew Dillon 	tt += (long_retries+1)*ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
228572ff6f6SMatthew Dillon 	    is_ht40, 0);
229572ff6f6SMatthew Dillon 
230572ff6f6SMatthew Dillon 	tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
231572ff6f6SMatthew Dillon 
232572ff6f6SMatthew Dillon 	for (x = 0; x <= short_retries + long_retries; x++) {
233572ff6f6SMatthew Dillon 		cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);
234572ff6f6SMatthew Dillon 		tt += (t_slot * cw/2);
235572ff6f6SMatthew Dillon 	}
236572ff6f6SMatthew Dillon 	return tt;
237572ff6f6SMatthew Dillon }
238572ff6f6SMatthew Dillon 
239572ff6f6SMatthew Dillon #endif	/* _KERNEL */
240572ff6f6SMatthew Dillon 
241572ff6f6SMatthew Dillon #endif /* _DEV_ATH_RATE_SAMPLE_H */
242