1fa20c234SSam Leffler /*-
2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3718cf2ccSPedro F. Giffuni *
4fa20c234SSam Leffler * Copyright (c) 2005 John Bicket
5fa20c234SSam Leffler * All rights reserved.
6fa20c234SSam Leffler *
7fa20c234SSam Leffler * Redistribution and use in source and binary forms, with or without
8fa20c234SSam Leffler * modification, are permitted provided that the following conditions
9fa20c234SSam Leffler * are met:
10fa20c234SSam Leffler * 1. Redistributions of source code must retain the above copyright
11fa20c234SSam Leffler * notice, this list of conditions and the following disclaimer,
12fa20c234SSam Leffler * without modification.
13fa20c234SSam Leffler * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14fa20c234SSam Leffler * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15fa20c234SSam Leffler * redistribution must be conditioned upon including a substantially
16fa20c234SSam Leffler * similar Disclaimer requirement for further binary redistribution.
17fa20c234SSam Leffler * 3. Neither the names of the above-listed copyright holders nor the names
18fa20c234SSam Leffler * of any contributors may be used to endorse or promote products derived
19fa20c234SSam Leffler * from this software without specific prior written permission.
20fa20c234SSam Leffler *
21fa20c234SSam Leffler * Alternatively, this software may be distributed under the terms of the
22fa20c234SSam Leffler * GNU General Public License ("GPL") version 2 as published by the Free
23fa20c234SSam Leffler * Software Foundation.
24fa20c234SSam Leffler *
25fa20c234SSam Leffler * NO WARRANTY
26fa20c234SSam Leffler * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27fa20c234SSam Leffler * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28fa20c234SSam Leffler * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29fa20c234SSam Leffler * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30fa20c234SSam Leffler * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31fa20c234SSam Leffler * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32fa20c234SSam Leffler * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33fa20c234SSam Leffler * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34fa20c234SSam Leffler * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35fa20c234SSam Leffler * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36fa20c234SSam Leffler * THE POSSIBILITY OF SUCH DAMAGES.
37fa20c234SSam Leffler */
38fa20c234SSam Leffler
39fa20c234SSam Leffler /*
40fa20c234SSam Leffler * Defintions for the Atheros Wireless LAN controller driver.
41fa20c234SSam Leffler */
42fa20c234SSam Leffler #ifndef _DEV_ATH_RATE_SAMPLE_H
43fa20c234SSam Leffler #define _DEV_ATH_RATE_SAMPLE_H
44fa20c234SSam Leffler
45fa20c234SSam Leffler /* per-device state */
46fa20c234SSam Leffler struct sample_softc {
47c1565b61SSam Leffler struct ath_ratectrl arc; /* base class */
48c1565b61SSam Leffler int smoothing_rate; /* ewma percentage [0..99] */
49c1565b61SSam Leffler int smoothing_minpackets;
50c1565b61SSam Leffler int sample_rate; /* %time to try different tx rates */
51c1565b61SSam Leffler int max_successive_failures;
52c1565b61SSam Leffler int stale_failure_timeout; /* how long to honor max_successive_failures */
53c1565b61SSam Leffler int min_switch; /* min time between rate changes */
54eb6f0de0SAdrian Chadd int min_good_pct; /* min good percentage for a rate to be considered */
55fa20c234SSam Leffler };
56fa20c234SSam Leffler #define ATH_SOFTC_SAMPLE(sc) ((struct sample_softc *)sc->sc_rc)
57fa20c234SSam Leffler
58fa20c234SSam Leffler struct rate_stats {
59b2763056SSam Leffler unsigned average_tx_time;
60fa20c234SSam Leffler int successive_failures;
6187acb7d5SAdrian Chadd uint64_t tries;
6287acb7d5SAdrian Chadd uint64_t total_packets; /* pkts total since assoc */
6387acb7d5SAdrian Chadd uint64_t packets_acked; /* pkts acked since assoc */
64eb6f0de0SAdrian Chadd int ewma_pct; /* EWMA percentage */
65b2763056SSam Leffler unsigned perfect_tx_time; /* transmit time for 0 retries */
66fa20c234SSam Leffler int last_tx;
67fa20c234SSam Leffler };
68fa20c234SSam Leffler
69c1565b61SSam Leffler struct txschedule {
70c1565b61SSam Leffler uint8_t t0, r0; /* series 0: tries, rate code */
71c1565b61SSam Leffler uint8_t t1, r1; /* series 1: tries, rate code */
72c1565b61SSam Leffler uint8_t t2, r2; /* series 2: tries, rate code */
73c1565b61SSam Leffler uint8_t t3, r3; /* series 3: tries, rate code */
74c1565b61SSam Leffler };
75c1565b61SSam Leffler
76fa20c234SSam Leffler /*
77cce63444SAdrian Chadd * We track performance for eight different packet size buckets.
78fa20c234SSam Leffler */
79cce63444SAdrian Chadd #define NUM_PACKET_SIZE_BINS 7
80fa20c234SSam Leffler
81cce63444SAdrian Chadd static const int packet_size_bins[NUM_PACKET_SIZE_BINS] = { 250, 1600, 4096, 8192, 16384, 32768, 65536 };
822d20d655SAdrian Chadd
832d20d655SAdrian Chadd static inline int
bin_to_size(int index)842d20d655SAdrian Chadd bin_to_size(int index)
852d20d655SAdrian Chadd {
862d20d655SAdrian Chadd return packet_size_bins[index];
872d20d655SAdrian Chadd }
882d20d655SAdrian Chadd
89fa20c234SSam Leffler /* per-node state */
90fa20c234SSam Leffler struct sample_node {
91c1565b61SSam Leffler int static_rix; /* rate index of fixed tx rate */
9242420dccSAdrian Chadd #define SAMPLE_MAXRATES 64 /* NB: corresponds to hal info[32] */
93193bfa21SAdrian Chadd uint64_t ratemask; /* bit mask of valid rate indices */
94c1565b61SSam Leffler const struct txschedule *sched; /* tx schedule table */
95fa20c234SSam Leffler
96cc86f1eaSAdrian Chadd const HAL_RATE_TABLE *currates;
97cc86f1eaSAdrian Chadd
98c1565b61SSam Leffler struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES];
99c1565b61SSam Leffler int last_sample_rix[NUM_PACKET_SIZE_BINS];
100fa20c234SSam Leffler
101c1565b61SSam Leffler int current_sample_rix[NUM_PACKET_SIZE_BINS];
102fa20c234SSam Leffler int packets_sent[NUM_PACKET_SIZE_BINS];
103fa20c234SSam Leffler
104c1565b61SSam Leffler int current_rix[NUM_PACKET_SIZE_BINS];
105b2763056SSam Leffler int packets_since_switch[NUM_PACKET_SIZE_BINS];
106*9f716a64SAdrian Chadd int ticks_since_switch[NUM_PACKET_SIZE_BINS];
107b2763056SSam Leffler
108b2763056SSam Leffler int packets_since_sample[NUM_PACKET_SIZE_BINS];
109b2763056SSam Leffler unsigned sample_tt[NUM_PACKET_SIZE_BINS];
110fa20c234SSam Leffler };
111dd9f5bbaSAdrian Chadd
112dd9f5bbaSAdrian Chadd #ifdef _KERNEL
113dd9f5bbaSAdrian Chadd
114c1565b61SSam Leffler #define ATH_NODE_SAMPLE(an) ((struct sample_node *)&(an)[1])
115d5d2dbefSAdrian Chadd #define IS_RATE_DEFINED(sn, rix) (((uint64_t) (sn)->ratemask & (1ULL<<((uint64_t) rix))) != 0)
116fa20c234SSam Leffler
117fa20c234SSam Leffler #ifndef MIN
118fa20c234SSam Leffler #define MIN(a,b) ((a) < (b) ? (a) : (b))
119fa20c234SSam Leffler #endif
120fa20c234SSam Leffler #ifndef MAX
121fa20c234SSam Leffler #define MAX(a,b) ((a) > (b) ? (a) : (b))
122fa20c234SSam Leffler #endif
123fa20c234SSam Leffler
124fa20c234SSam Leffler #define WIFI_CW_MIN 31
125fa20c234SSam Leffler #define WIFI_CW_MAX 1023
126fa20c234SSam Leffler
127b2763056SSam Leffler /*
128fa20c234SSam Leffler * Calculate the transmit duration of a frame.
129fa20c234SSam Leffler */
calc_usecs_unicast_packet(struct ath_softc * sc,int length,int rix,int short_retries,int long_retries,int is_ht40)130fa20c234SSam Leffler static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,
131fa20c234SSam Leffler int length,
132e09c8c4cSAdrian Chadd int rix, int short_retries,
133e09c8c4cSAdrian Chadd int long_retries, int is_ht40)
134e09c8c4cSAdrian Chadd {
135fa20c234SSam Leffler const HAL_RATE_TABLE *rt = sc->sc_currates;
1367a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
137b91bf513SSam Leffler int rts, cts;
138fa20c234SSam Leffler
139fa20c234SSam Leffler unsigned t_slot = 20;
140fa20c234SSam Leffler unsigned t_difs = 50;
141fa20c234SSam Leffler unsigned t_sifs = 10;
142fa20c234SSam Leffler int tt = 0;
143fa20c234SSam Leffler int x = 0;
144fa20c234SSam Leffler int cw = WIFI_CW_MIN;
14565f9edeeSSam Leffler int cix;
14657a4a758SSam Leffler
147fa20c234SSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
148fa20c234SSam Leffler
14965f9edeeSSam Leffler if (rix >= rt->rateCount) {
15065f9edeeSSam Leffler printf("bogus rix %d, max %u, mode %u\n",
15165f9edeeSSam Leffler rix, rt->rateCount, sc->sc_curmode);
152b91bf513SSam Leffler return 0;
153fa20c234SSam Leffler }
15465f9edeeSSam Leffler cix = rt->info[rix].controlRate;
155b91bf513SSam Leffler /*
156b91bf513SSam Leffler * XXX getting mac/phy level timings should be fixed for turbo
157b91bf513SSam Leffler * rates, and there is probably a way to get this from the
158b91bf513SSam Leffler * hal...
159b91bf513SSam Leffler */
160b91bf513SSam Leffler switch (rt->info[rix].phy) {
161b91bf513SSam Leffler case IEEE80211_T_OFDM:
162b91bf513SSam Leffler t_slot = 9;
163b91bf513SSam Leffler t_sifs = 16;
164b91bf513SSam Leffler t_difs = 28;
165b91bf513SSam Leffler /* fall through */
166b91bf513SSam Leffler case IEEE80211_T_TURBO:
167b91bf513SSam Leffler t_slot = 9;
168b91bf513SSam Leffler t_sifs = 8;
169b91bf513SSam Leffler t_difs = 28;
170b91bf513SSam Leffler break;
1715e960809SAdrian Chadd case IEEE80211_T_HT:
1725e960809SAdrian Chadd t_slot = 9;
1735e960809SAdrian Chadd t_sifs = 8;
1745e960809SAdrian Chadd t_difs = 28;
1755e960809SAdrian Chadd break;
176b91bf513SSam Leffler case IEEE80211_T_DS:
177b91bf513SSam Leffler /* fall through to default */
178b91bf513SSam Leffler default:
179b91bf513SSam Leffler /* pg 205 ieee.802.11.pdf */
180b91bf513SSam Leffler t_slot = 20;
181b91bf513SSam Leffler t_difs = 50;
182b91bf513SSam Leffler t_sifs = 10;
183b91bf513SSam Leffler }
184b91bf513SSam Leffler
185b91bf513SSam Leffler rts = cts = 0;
186b2763056SSam Leffler
187b2763056SSam Leffler if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
188b2763056SSam Leffler rt->info[rix].phy == IEEE80211_T_OFDM) {
189b2763056SSam Leffler if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
190b2763056SSam Leffler rts = 1;
191b2763056SSam Leffler else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
192b2763056SSam Leffler cts = 1;
193b2763056SSam Leffler
194b2763056SSam Leffler cix = rt->info[sc->sc_protrix].controlRate;
195b2763056SSam Leffler }
196b2763056SSam Leffler
197b91bf513SSam Leffler if (0 /*length > ic->ic_rtsthreshold */) {
198b2763056SSam Leffler rts = 1;
199b2763056SSam Leffler }
200b2763056SSam Leffler
201b2763056SSam Leffler if (rts || cts) {
20265f9edeeSSam Leffler int ctsrate;
203b2763056SSam Leffler int ctsduration = 0;
204b91bf513SSam Leffler
20565f9edeeSSam Leffler /* NB: this is intentionally not a runtime check */
20665f9edeeSSam Leffler KASSERT(cix < rt->rateCount,
20765f9edeeSSam Leffler ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,
20865f9edeeSSam Leffler sc->sc_curmode));
209b91bf513SSam Leffler
21065f9edeeSSam Leffler ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;
211b2763056SSam Leffler if (rts) /* SIFS + CTS */
212b2763056SSam Leffler ctsduration += rt->info[cix].spAckDuration;
213b2763056SSam Leffler
2147ff1939dSAdrian Chadd /* XXX assumes short preamble, include SIFS */
215637b8c6dSAdrian Chadd ctsduration += ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
2167ff1939dSAdrian Chadd is_ht40, 0, 1);
217b2763056SSam Leffler
218b2763056SSam Leffler if (cts) /* SIFS + ACK */
219b2763056SSam Leffler ctsduration += rt->info[cix].spAckDuration;
220b2763056SSam Leffler
221b2763056SSam Leffler tt += (short_retries + 1) * ctsduration;
222b2763056SSam Leffler }
223fa20c234SSam Leffler tt += t_difs;
2245e960809SAdrian Chadd
2257ff1939dSAdrian Chadd /* XXX assumes short preamble, include SIFS */
226637b8c6dSAdrian Chadd tt += (long_retries+1)*ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,
2277ff1939dSAdrian Chadd is_ht40, 0, 1);
228637b8c6dSAdrian Chadd
229b91bf513SSam Leffler tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);
2305e960809SAdrian Chadd
231b2763056SSam Leffler for (x = 0; x <= short_retries + long_retries; x++) {
232fa20c234SSam Leffler cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);
233fa20c234SSam Leffler tt += (t_slot * cw/2);
234fa20c234SSam Leffler }
235fa20c234SSam Leffler return tt;
236fa20c234SSam Leffler }
237dd9f5bbaSAdrian Chadd
238dd9f5bbaSAdrian Chadd #endif /* _KERNEL */
239dd9f5bbaSAdrian Chadd
240fa20c234SSam Leffler #endif /* _DEV_ATH_RATE_SAMPLE_H */
241