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