xref: /netbsd-src/sys/dev/ic/athrate-sample.h (revision 216b022d43be22708e444b6a7a29da59f25cfc71)
1*216b022dSdyoung /*	$NetBSD: athrate-sample.h,v 1.4 2012/11/08 20:43:55 dyoung Exp $ */
2fd8cfc5eSxtraeme 
3d1f00611Sdyoung /*-
4d1f00611Sdyoung  * Copyright (c) 2005 John Bicket
5d1f00611Sdyoung  * All rights reserved.
6d1f00611Sdyoung  *
7d1f00611Sdyoung  * Redistribution and use in source and binary forms, with or without
8d1f00611Sdyoung  * modification, are permitted provided that the following conditions
9d1f00611Sdyoung  * are met:
10d1f00611Sdyoung  * 1. Redistributions of source code must retain the above copyright
11d1f00611Sdyoung  *    notice, this list of conditions and the following disclaimer,
12d1f00611Sdyoung  *    without modification.
13d1f00611Sdyoung  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14d1f00611Sdyoung  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15d1f00611Sdyoung  *    redistribution must be conditioned upon including a substantially
16d1f00611Sdyoung  *    similar Disclaimer requirement for further binary redistribution.
17d1f00611Sdyoung  * 3. Neither the names of the above-listed copyright holders nor the names
18d1f00611Sdyoung  *    of any contributors may be used to endorse or promote products derived
19d1f00611Sdyoung  *    from this software without specific prior written permission.
20d1f00611Sdyoung  *
21d1f00611Sdyoung  * Alternatively, this software may be distributed under the terms of the
22d1f00611Sdyoung  * GNU General Public License ("GPL") version 2 as published by the Free
23d1f00611Sdyoung  * Software Foundation.
24d1f00611Sdyoung  *
25d1f00611Sdyoung  * NO WARRANTY
26d1f00611Sdyoung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27d1f00611Sdyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28d1f00611Sdyoung  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29d1f00611Sdyoung  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30d1f00611Sdyoung  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31d1f00611Sdyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32d1f00611Sdyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33d1f00611Sdyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34d1f00611Sdyoung  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35d1f00611Sdyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36d1f00611Sdyoung  * THE POSSIBILITY OF SUCH DAMAGES.
37d1f00611Sdyoung  *
38d1f00611Sdyoung  * $FreeBSD: src/sys/dev/ath/ath_rate/sample/sample.h,v 1.3 2005/03/20 01:27:33 sam Exp $
39d1f00611Sdyoung  */
40d1f00611Sdyoung 
41d1f00611Sdyoung /*
42d1f00611Sdyoung  * Defintions for the Atheros Wireless LAN controller driver.
43d1f00611Sdyoung  */
44d1f00611Sdyoung #ifndef _DEV_ATH_RATE_SAMPLE_H
45d1f00611Sdyoung #define _DEV_ATH_RATE_SAMPLE_H
46d1f00611Sdyoung 
47d1f00611Sdyoung /* per-device state */
48d1f00611Sdyoung struct sample_softc {
49d1f00611Sdyoung 	struct ath_ratectrl arc;	/* base state */
50d1f00611Sdyoung 	int	ath_smoothing_rate;	/* ewma percentage (out of 100) */
51d1f00611Sdyoung 	int	ath_sample_rate;	/* send a different bit-rate 1/X packets */
52d1f00611Sdyoung };
53d1f00611Sdyoung #define	ATH_SOFTC_SAMPLE(sc)	((struct sample_softc *)sc->sc_rc)
54d1f00611Sdyoung 
55d1f00611Sdyoung struct rate_info {
56d1f00611Sdyoung 	int rate;
57d1f00611Sdyoung 	int rix;
58d1f00611Sdyoung 	int rateCode;
59d1f00611Sdyoung 	int shortPreambleRateCode;
60d1f00611Sdyoung };
61d1f00611Sdyoung 
62d1f00611Sdyoung 
63d1f00611Sdyoung struct rate_stats {
64d1f00611Sdyoung 	unsigned average_tx_time;
65d1f00611Sdyoung 	int successive_failures;
66d1f00611Sdyoung 	int tries;
67d1f00611Sdyoung 	int total_packets;
68d1f00611Sdyoung 	int packets_acked;
69d1f00611Sdyoung 	unsigned perfect_tx_time; /* transmit time for 0 retries */
70d1f00611Sdyoung 	int last_tx;
71d1f00611Sdyoung };
72d1f00611Sdyoung 
73d1f00611Sdyoung /*
74d1f00611Sdyoung  * for now, we track performance for three different packet
75d1f00611Sdyoung  * size buckets
76d1f00611Sdyoung  */
77d1f00611Sdyoung #define NUM_PACKET_SIZE_BINS 3
78d1f00611Sdyoung static int packet_size_bins[NUM_PACKET_SIZE_BINS] = {250, 1600, 3000};
79d1f00611Sdyoung 
80d1f00611Sdyoung /* per-node state */
81d1f00611Sdyoung struct sample_node {
82d1f00611Sdyoung 	int static_rate_ndx;
83d1f00611Sdyoung 	int num_rates;
84d1f00611Sdyoung 
85d1f00611Sdyoung 	struct rate_info rates[IEEE80211_RATE_MAXSIZE];
86d1f00611Sdyoung 
87d1f00611Sdyoung 	struct rate_stats stats[NUM_PACKET_SIZE_BINS][IEEE80211_RATE_MAXSIZE];
88d1f00611Sdyoung 	int last_sample_ndx[NUM_PACKET_SIZE_BINS];
89d1f00611Sdyoung 
90d1f00611Sdyoung 	int current_sample_ndx[NUM_PACKET_SIZE_BINS];
91d1f00611Sdyoung 	int packets_sent[NUM_PACKET_SIZE_BINS];
92d1f00611Sdyoung 
93d1f00611Sdyoung 	int current_rate[NUM_PACKET_SIZE_BINS];
94d1f00611Sdyoung 	int packets_since_switch[NUM_PACKET_SIZE_BINS];
9525e9e914Sdyoung 	unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS];
96d1f00611Sdyoung 
97d1f00611Sdyoung 	int packets_since_sample[NUM_PACKET_SIZE_BINS];
98d1f00611Sdyoung 	unsigned sample_tt[NUM_PACKET_SIZE_BINS];
99d1f00611Sdyoung };
100d1f00611Sdyoung #define	ATH_NODE_SAMPLE(an)	((struct sample_node *)&an[1])
101d1f00611Sdyoung 
102d1f00611Sdyoung #ifndef MIN
103d1f00611Sdyoung #define	MIN(a,b)	((a) < (b) ? (a) : (b))
104d1f00611Sdyoung #endif
105d1f00611Sdyoung #ifndef MAX
106d1f00611Sdyoung #define	MAX(a,b)	((a) > (b) ? (a) : (b))
107d1f00611Sdyoung #endif
108d1f00611Sdyoung 
109d1f00611Sdyoung #define WIFI_CW_MIN 31
110d1f00611Sdyoung #define WIFI_CW_MAX 1023
111d1f00611Sdyoung 
112d1f00611Sdyoung struct ar5212_desc {
113d1f00611Sdyoung 	/*
114d1f00611Sdyoung 	 * tx_control_0
115d1f00611Sdyoung 	 */
116d1f00611Sdyoung 	u_int32_t	frame_len:12;
117d1f00611Sdyoung 	u_int32_t	reserved_12_15:4;
118d1f00611Sdyoung 	u_int32_t	xmit_power:6;
119d1f00611Sdyoung 	u_int32_t	rts_cts_enable:1;
120d1f00611Sdyoung 	u_int32_t	veol:1;
121d1f00611Sdyoung 	u_int32_t	clear_dest_mask:1;
122d1f00611Sdyoung 	u_int32_t	ant_mode_xmit:4;
123d1f00611Sdyoung 	u_int32_t	inter_req:1;
124d1f00611Sdyoung 	u_int32_t	encrypt_key_valid:1;
125d1f00611Sdyoung 	u_int32_t	cts_enable:1;
126d1f00611Sdyoung 
127d1f00611Sdyoung 	/*
128d1f00611Sdyoung 	 * tx_control_1
129d1f00611Sdyoung 	 */
130d1f00611Sdyoung 	u_int32_t	buf_len:12;
131d1f00611Sdyoung 	u_int32_t	more:1;
132d1f00611Sdyoung 	u_int32_t	encrypt_key_index:7;
133d1f00611Sdyoung 	u_int32_t	frame_type:4;
134d1f00611Sdyoung 	u_int32_t	no_ack:1;
135d1f00611Sdyoung 	u_int32_t	comp_proc:2;
136d1f00611Sdyoung 	u_int32_t	comp_iv_len:2;
137d1f00611Sdyoung 	u_int32_t	comp_icv_len:2;
138d1f00611Sdyoung 	u_int32_t	reserved_31:1;
139d1f00611Sdyoung 
140d1f00611Sdyoung 	/*
141d1f00611Sdyoung 	 * tx_control_2
142d1f00611Sdyoung 	 */
143d1f00611Sdyoung 	u_int32_t	rts_duration:15;
144d1f00611Sdyoung 	u_int32_t	duration_update_enable:1;
145d1f00611Sdyoung 	u_int32_t	xmit_tries0:4;
146d1f00611Sdyoung 	u_int32_t	xmit_tries1:4;
147d1f00611Sdyoung 	u_int32_t	xmit_tries2:4;
148d1f00611Sdyoung 	u_int32_t	xmit_tries3:4;
149d1f00611Sdyoung 
150d1f00611Sdyoung 	/*
151d1f00611Sdyoung 	 * tx_control_3
152d1f00611Sdyoung 	 */
153d1f00611Sdyoung 	u_int32_t	xmit_rate0:5;
154d1f00611Sdyoung 	u_int32_t	xmit_rate1:5;
155d1f00611Sdyoung 	u_int32_t	xmit_rate2:5;
156d1f00611Sdyoung 	u_int32_t	xmit_rate3:5;
157d1f00611Sdyoung 	u_int32_t	rts_cts_rate:5;
158d1f00611Sdyoung 	u_int32_t	reserved_25_31:7;
159d1f00611Sdyoung 
160d1f00611Sdyoung 	/*
161d1f00611Sdyoung 	 * tx_status_0
162d1f00611Sdyoung 	 */
163d1f00611Sdyoung 	u_int32_t	frame_xmit_ok:1;
164d1f00611Sdyoung 	u_int32_t	excessive_retries:1;
165d1f00611Sdyoung 	u_int32_t	fifo_underrun:1;
166d1f00611Sdyoung 	u_int32_t	filtered:1;
167d1f00611Sdyoung 	u_int32_t	rts_fail_count:4;
168d1f00611Sdyoung 	u_int32_t	data_fail_count:4;
169d1f00611Sdyoung 	u_int32_t	virt_coll_count:4;
170d1f00611Sdyoung 	u_int32_t	send_timestamp:16;
171d1f00611Sdyoung 
172d1f00611Sdyoung 	/*
173d1f00611Sdyoung 	 * tx_status_1
174d1f00611Sdyoung 	 */
175d1f00611Sdyoung 	u_int32_t	done:1;
176d1f00611Sdyoung 	u_int32_t	seq_num:12;
177d1f00611Sdyoung 	u_int32_t	ack_sig_strength:8;
178d1f00611Sdyoung 	u_int32_t	final_ts_index:2;
179d1f00611Sdyoung 	u_int32_t	comp_success:1;
180d1f00611Sdyoung 	u_int32_t	xmit_antenna:1;
181d1f00611Sdyoung 	u_int32_t	reserved_25_31_x:7;
182d1f00611Sdyoung } __packed;
183d1f00611Sdyoung 
184d1f00611Sdyoung /*
185d1f00611Sdyoung  * Calculate the transmit duration of a frame.
186d1f00611Sdyoung  */
calc_usecs_unicast_packet(struct ath_softc * sc,int length,int rix,int short_retries,int long_retries)187d1f00611Sdyoung static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
188d1f00611Sdyoung 				int length,
189d1f00611Sdyoung 				int rix, int short_retries, int long_retries) {
190d1f00611Sdyoung 	const HAL_RATE_TABLE *rt = sc->sc_currates;
19125e9e914Sdyoung 	int rts, cts;
192d1f00611Sdyoung 
193d1f00611Sdyoung 	unsigned t_slot = 20;
194d1f00611Sdyoung 	unsigned t_difs = 50;
195d1f00611Sdyoung 	unsigned t_sifs = 10;
196d1f00611Sdyoung 	struct ieee80211com *ic = &sc->sc_ic;
197d1f00611Sdyoung 	int tt = 0;
198d1f00611Sdyoung 	int x = 0;
199d1f00611Sdyoung 	int cw = WIFI_CW_MIN;
200d1f00611Sdyoung 	int cix = rt->info[rix].controlRate;
201d1f00611Sdyoung 
202*216b022dSdyoung 	KASSERTMSG(rt != NULL, "no rate table, mode %u", sc->sc_curmode);
203d1f00611Sdyoung 
20425e9e914Sdyoung 	if (!rt->info[rix].rateKbps) {
20525e9e914Sdyoung 		printf("rix %d (%d) bad ratekbps %d mode %u",
20625e9e914Sdyoung 		       rix, rt->info[rix].dot11Rate,
20725e9e914Sdyoung 		       rt->info[rix].rateKbps,
20825e9e914Sdyoung 		       sc->sc_curmode);
20925e9e914Sdyoung 
21025e9e914Sdyoung 		return 0;
211d1f00611Sdyoung 	}
21225e9e914Sdyoung 	/*
21325e9e914Sdyoung 	 * XXX getting mac/phy level timings should be fixed for turbo
21425e9e914Sdyoung 	 * rates, and there is probably a way to get this from the
21525e9e914Sdyoung 	 * hal...
21625e9e914Sdyoung 	 */
21725e9e914Sdyoung 	switch (rt->info[rix].phy) {
21825e9e914Sdyoung 	case IEEE80211_T_OFDM:
21925e9e914Sdyoung 		t_slot = 9;
22025e9e914Sdyoung 		t_sifs = 16;
22125e9e914Sdyoung 		t_difs = 28;
22225e9e914Sdyoung 		/* fall through */
22325e9e914Sdyoung 	case IEEE80211_T_TURBO:
22425e9e914Sdyoung 		t_slot = 9;
22525e9e914Sdyoung 		t_sifs = 8;
22625e9e914Sdyoung 		t_difs = 28;
22725e9e914Sdyoung 		break;
22825e9e914Sdyoung 	case IEEE80211_T_DS:
22925e9e914Sdyoung 		/* fall through to default */
23025e9e914Sdyoung 	default:
23125e9e914Sdyoung 		/* pg 205 ieee.802.11.pdf */
23225e9e914Sdyoung 		t_slot = 20;
23325e9e914Sdyoung 		t_difs = 50;
23425e9e914Sdyoung 		t_sifs = 10;
23525e9e914Sdyoung 	}
23625e9e914Sdyoung 
23725e9e914Sdyoung 	rts = cts = 0;
238d1f00611Sdyoung 
239d1f00611Sdyoung 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
240d1f00611Sdyoung 	    rt->info[rix].phy == IEEE80211_T_OFDM) {
241d1f00611Sdyoung 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
242d1f00611Sdyoung 			rts = 1;
243d1f00611Sdyoung 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
244d1f00611Sdyoung 			cts = 1;
245d1f00611Sdyoung 
246d1f00611Sdyoung 		cix = rt->info[sc->sc_protrix].controlRate;
247d1f00611Sdyoung 
248d1f00611Sdyoung 	}
249d1f00611Sdyoung 
25025e9e914Sdyoung 	if (0 /*length > ic->ic_rtsthreshold */) {
251d1f00611Sdyoung 		rts = 1;
252d1f00611Sdyoung 	}
253d1f00611Sdyoung 
254d1f00611Sdyoung 	if (rts || cts) {
255d1f00611Sdyoung 		int ctsrate = rt->info[cix].rateCode;
256d1f00611Sdyoung 		int ctsduration = 0;
25725e9e914Sdyoung 
25825e9e914Sdyoung 		if (!rt->info[cix].rateKbps) {
25925e9e914Sdyoung 			printf("cix %d (%d) bad ratekbps %d mode %u",
26025e9e914Sdyoung 			       cix, rt->info[cix].dot11Rate,
26125e9e914Sdyoung 			       rt->info[cix].rateKbps,
26225e9e914Sdyoung 			       sc->sc_curmode);
26325e9e914Sdyoung 			return 0;
26425e9e914Sdyoung 		}
26525e9e914Sdyoung 
266d1f00611Sdyoung 		ctsrate |= rt->info[cix].shortPreamble;
267d1f00611Sdyoung 		if (rts)		/* SIFS + CTS */
268d1f00611Sdyoung 			ctsduration += rt->info[cix].spAckDuration;
269d1f00611Sdyoung 
270d1f00611Sdyoung 		ctsduration += ath_hal_computetxtime(sc->sc_ah,
271d1f00611Sdyoung 						     rt, length, rix, AH_TRUE);
272d1f00611Sdyoung 
273d1f00611Sdyoung 		if (cts)	/* SIFS + ACK */
274d1f00611Sdyoung 			ctsduration += rt->info[cix].spAckDuration;
275d1f00611Sdyoung 
276d1f00611Sdyoung 		tt += (short_retries + 1) * ctsduration;
277d1f00611Sdyoung 	}
278d1f00611Sdyoung 	tt += t_difs;
27925e9e914Sdyoung 	tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
280d1f00611Sdyoung 	tt += (long_retries+1)*ath_hal_computetxtime(sc->sc_ah, rt, length,
281d1f00611Sdyoung 						rix, AH_TRUE);
282d1f00611Sdyoung 	for (x = 0; x <= short_retries + long_retries; x++) {
283d1f00611Sdyoung 		cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);
284d1f00611Sdyoung 		tt += (t_slot * cw/2);
285d1f00611Sdyoung 	}
286d1f00611Sdyoung 	return tt;
287d1f00611Sdyoung }
288d1f00611Sdyoung #endif /* _DEV_ATH_RATE_SAMPLE_H */
289