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 class */ 48 int smoothing_rate; /* ewma percentage [0..99] */ 49 int smoothing_minpackets; 50 int sample_rate; /* %time to try different tx rates */ 51 int max_successive_failures; 52 int stale_failure_timeout; /* how long to honor max_successive_failures */ 53 int min_switch; /* min time between rate changes */ 54 int min_good_pct; /* min good percentage for a rate to be considered */ 55 }; 56 #define ATH_SOFTC_SAMPLE(sc) ((struct sample_softc *)sc->sc_rc) 57 58 struct rate_stats { 59 unsigned average_tx_time; 60 int successive_failures; 61 uint64_t tries; 62 uint64_t total_packets; /* pkts total since assoc */ 63 uint64_t packets_acked; /* pkts acked since assoc */ 64 int ewma_pct; /* EWMA percentage */ 65 unsigned perfect_tx_time; /* transmit time for 0 retries */ 66 int last_tx; 67 }; 68 69 struct txschedule { 70 uint8_t t0, r0; /* series 0: tries, rate code */ 71 uint8_t t1, r1; /* series 1: tries, rate code */ 72 uint8_t t2, r2; /* series 2: tries, rate code */ 73 uint8_t t3, r3; /* series 3: tries, rate code */ 74 }; 75 76 /* 77 * for now, we track performance for three different packet 78 * size buckets 79 */ 80 #define NUM_PACKET_SIZE_BINS 2 81 82 static const int packet_size_bins[NUM_PACKET_SIZE_BINS] = { 250, 1600 }; 83 84 static inline int 85 bin_to_size(int index) 86 { 87 return packet_size_bins[index]; 88 } 89 90 /* per-node state */ 91 struct sample_node { 92 int static_rix; /* rate index of fixed tx rate */ 93 #define SAMPLE_MAXRATES 64 /* NB: corresponds to hal info[32] */ 94 uint64_t ratemask; /* bit mask of valid rate indices */ 95 const struct txschedule *sched; /* tx schedule table */ 96 97 const HAL_RATE_TABLE *currates; 98 99 struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES]; 100 int last_sample_rix[NUM_PACKET_SIZE_BINS]; 101 102 int current_sample_rix[NUM_PACKET_SIZE_BINS]; 103 int packets_sent[NUM_PACKET_SIZE_BINS]; 104 105 int current_rix[NUM_PACKET_SIZE_BINS]; 106 int packets_since_switch[NUM_PACKET_SIZE_BINS]; 107 unsigned ticks_since_switch[NUM_PACKET_SIZE_BINS]; 108 109 int packets_since_sample[NUM_PACKET_SIZE_BINS]; 110 unsigned sample_tt[NUM_PACKET_SIZE_BINS]; 111 }; 112 113 #ifdef _KERNEL 114 115 #define ATH_NODE_SAMPLE(an) ((struct sample_node *)&(an)[1]) 116 #define IS_RATE_DEFINED(sn, rix) (((uint64_t) (sn)->ratemask & (1ULL<<((uint64_t) rix))) != 0) 117 118 #ifndef MIN 119 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 120 #endif 121 #ifndef MAX 122 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 123 #endif 124 125 #define WIFI_CW_MIN 31 126 #define WIFI_CW_MAX 1023 127 128 /* 129 * Calculate the transmit duration of a frame. 130 */ 131 static unsigned calc_usecs_unicast_packet(struct ath_softc *sc, 132 int length, 133 int rix, int short_retries, 134 int long_retries, int is_ht40) 135 { 136 const HAL_RATE_TABLE *rt = sc->sc_currates; 137 struct ieee80211com *ic = &sc->sc_ic; 138 int rts, cts; 139 140 unsigned t_slot = 20; 141 unsigned t_difs = 50; 142 unsigned t_sifs = 10; 143 int tt = 0; 144 int x = 0; 145 int cw = WIFI_CW_MIN; 146 int cix; 147 148 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 149 150 if (rix >= rt->rateCount) { 151 printf("bogus rix %d, max %u, mode %u\n", 152 rix, rt->rateCount, sc->sc_curmode); 153 return 0; 154 } 155 cix = rt->info[rix].controlRate; 156 /* 157 * XXX getting mac/phy level timings should be fixed for turbo 158 * rates, and there is probably a way to get this from the 159 * hal... 160 */ 161 switch (rt->info[rix].phy) { 162 case IEEE80211_T_OFDM: 163 t_slot = 9; 164 t_sifs = 16; 165 t_difs = 28; 166 /* fall through */ 167 case IEEE80211_T_TURBO: 168 t_slot = 9; 169 t_sifs = 8; 170 t_difs = 28; 171 break; 172 case IEEE80211_T_HT: 173 t_slot = 9; 174 t_sifs = 8; 175 t_difs = 28; 176 break; 177 case IEEE80211_T_DS: 178 /* fall through to default */ 179 default: 180 /* pg 205 ieee.802.11.pdf */ 181 t_slot = 20; 182 t_difs = 50; 183 t_sifs = 10; 184 } 185 186 rts = cts = 0; 187 188 if ((ic->ic_flags & IEEE80211_F_USEPROT) && 189 rt->info[rix].phy == IEEE80211_T_OFDM) { 190 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 191 rts = 1; 192 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 193 cts = 1; 194 195 cix = rt->info[sc->sc_protrix].controlRate; 196 } 197 198 if (0 /*length > ic->ic_rtsthreshold */) { 199 rts = 1; 200 } 201 202 if (rts || cts) { 203 int ctsrate; 204 int ctsduration = 0; 205 206 /* NB: this is intentionally not a runtime check */ 207 KASSERT(cix < rt->rateCount, 208 ("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount, 209 sc->sc_curmode)); 210 211 ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble; 212 if (rts) /* SIFS + CTS */ 213 ctsduration += rt->info[cix].spAckDuration; 214 215 /* XXX assumes short preamble */ 216 ctsduration += ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix, 217 is_ht40, 0); 218 219 if (cts) /* SIFS + ACK */ 220 ctsduration += rt->info[cix].spAckDuration; 221 222 tt += (short_retries + 1) * ctsduration; 223 } 224 tt += t_difs; 225 226 /* XXX assumes short preamble */ 227 tt += (long_retries+1)*ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix, 228 is_ht40, 0); 229 230 tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration); 231 232 for (x = 0; x <= short_retries + long_retries; x++) { 233 cw = MIN(WIFI_CW_MAX, (cw + 1) * 2); 234 tt += (t_slot * cw/2); 235 } 236 return tt; 237 } 238 239 #endif /* _KERNEL */ 240 241 #endif /* _DEV_ATH_RATE_SAMPLE_H */ 242