Path: blob/main/sys/dev/ath/ath_rate/sample/sample.h
39565 views
/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2005 John Bicket4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer,11* without modification.12* 2. Redistributions in binary form must reproduce at minimum a disclaimer13* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any14* redistribution must be conditioned upon including a substantially15* similar Disclaimer requirement for further binary redistribution.16* 3. Neither the names of the above-listed copyright holders nor the names17* of any contributors may be used to endorse or promote products derived18* from this software without specific prior written permission.19*20* Alternatively, this software may be distributed under the terms of the21* GNU General Public License ("GPL") version 2 as published by the Free22* Software Foundation.23*24* NO WARRANTY25* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS26* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT27* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY28* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL29* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,30* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF31* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS32* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER33* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)34* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF35* THE POSSIBILITY OF SUCH DAMAGES.36*/3738/*39* Defintions for the Atheros Wireless LAN controller driver.40*/41#ifndef _DEV_ATH_RATE_SAMPLE_H42#define _DEV_ATH_RATE_SAMPLE_H4344/* per-device state */45struct sample_softc {46struct ath_ratectrl arc; /* base class */47int smoothing_rate; /* ewma percentage [0..99] */48int smoothing_minpackets;49int sample_rate; /* %time to try different tx rates */50int max_successive_failures;51int stale_failure_timeout; /* how long to honor max_successive_failures */52int min_switch; /* min time between rate changes */53int min_good_pct; /* min good percentage for a rate to be considered */54};55#define ATH_SOFTC_SAMPLE(sc) ((struct sample_softc *)sc->sc_rc)5657struct rate_stats {58unsigned average_tx_time;59int successive_failures;60uint64_t tries;61uint64_t total_packets; /* pkts total since assoc */62uint64_t packets_acked; /* pkts acked since assoc */63int ewma_pct; /* EWMA percentage */64unsigned perfect_tx_time; /* transmit time for 0 retries */65int last_tx;66};6768struct txschedule {69uint8_t t0, r0; /* series 0: tries, rate code */70uint8_t t1, r1; /* series 1: tries, rate code */71uint8_t t2, r2; /* series 2: tries, rate code */72uint8_t t3, r3; /* series 3: tries, rate code */73};7475/*76* We track performance for eight different packet size buckets.77*/78#define NUM_PACKET_SIZE_BINS 77980static const int packet_size_bins[NUM_PACKET_SIZE_BINS] = { 250, 1600, 4096, 8192, 16384, 32768, 65536 };8182static inline int83bin_to_size(int index)84{85return packet_size_bins[index];86}8788/* per-node state */89struct sample_node {90int static_rix; /* rate index of fixed tx rate */91#define SAMPLE_MAXRATES 64 /* NB: corresponds to hal info[32] */92uint64_t ratemask; /* bit mask of valid rate indices */93const struct txschedule *sched; /* tx schedule table */9495const HAL_RATE_TABLE *currates;9697struct rate_stats stats[NUM_PACKET_SIZE_BINS][SAMPLE_MAXRATES];98int last_sample_rix[NUM_PACKET_SIZE_BINS];99100int current_sample_rix[NUM_PACKET_SIZE_BINS];101int packets_sent[NUM_PACKET_SIZE_BINS];102103int current_rix[NUM_PACKET_SIZE_BINS];104int packets_since_switch[NUM_PACKET_SIZE_BINS];105int ticks_since_switch[NUM_PACKET_SIZE_BINS];106107int packets_since_sample[NUM_PACKET_SIZE_BINS];108unsigned sample_tt[NUM_PACKET_SIZE_BINS];109};110111#ifdef _KERNEL112113#define ATH_NODE_SAMPLE(an) ((struct sample_node *)&(an)[1])114#define IS_RATE_DEFINED(sn, rix) (((uint64_t) (sn)->ratemask & (1ULL<<((uint64_t) rix))) != 0)115116#ifndef MIN117#define MIN(a,b) ((a) < (b) ? (a) : (b))118#endif119#ifndef MAX120#define MAX(a,b) ((a) > (b) ? (a) : (b))121#endif122123#define WIFI_CW_MIN 31124#define WIFI_CW_MAX 1023125126/*127* Calculate the transmit duration of a frame.128*/129static unsigned calc_usecs_unicast_packet(struct ath_softc *sc,130int length,131int rix, int short_retries,132int long_retries, int is_ht40)133{134const HAL_RATE_TABLE *rt = sc->sc_currates;135struct ieee80211com *ic = &sc->sc_ic;136int rts, cts;137138unsigned t_slot = 20;139unsigned t_difs = 50;140unsigned t_sifs = 10;141int tt = 0;142int x = 0;143int cw = WIFI_CW_MIN;144int cix;145146KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));147148if (rix >= rt->rateCount) {149printf("bogus rix %d, max %u, mode %u\n",150rix, rt->rateCount, sc->sc_curmode);151return 0;152}153cix = rt->info[rix].controlRate;154/*155* XXX getting mac/phy level timings should be fixed for turbo156* rates, and there is probably a way to get this from the157* hal...158*/159switch (rt->info[rix].phy) {160case IEEE80211_T_OFDM:161t_slot = 9;162t_sifs = 16;163t_difs = 28;164/* fall through */165case IEEE80211_T_TURBO:166t_slot = 9;167t_sifs = 8;168t_difs = 28;169break;170case IEEE80211_T_HT:171t_slot = 9;172t_sifs = 8;173t_difs = 28;174break;175case IEEE80211_T_DS:176/* fall through to default */177default:178/* pg 205 ieee.802.11.pdf */179t_slot = 20;180t_difs = 50;181t_sifs = 10;182}183184rts = cts = 0;185186if ((ic->ic_flags & IEEE80211_F_USEPROT) &&187rt->info[rix].phy == IEEE80211_T_OFDM) {188if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)189rts = 1;190else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)191cts = 1;192193cix = rt->info[sc->sc_protrix].controlRate;194}195196if (0 /*length > ic->ic_rtsthreshold */) {197rts = 1;198}199200if (rts || cts) {201int ctsrate;202int ctsduration = 0;203204/* NB: this is intentionally not a runtime check */205KASSERT(cix < rt->rateCount,206("bogus cix %d, max %u, mode %u\n", cix, rt->rateCount,207sc->sc_curmode));208209ctsrate = rt->info[cix].rateCode | rt->info[cix].shortPreamble;210if (rts) /* SIFS + CTS */211ctsduration += rt->info[cix].spAckDuration;212213/* XXX assumes short preamble, include SIFS */214ctsduration += ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,215is_ht40, 0, 1);216217if (cts) /* SIFS + ACK */218ctsduration += rt->info[cix].spAckDuration;219220tt += (short_retries + 1) * ctsduration;221}222tt += t_difs;223224/* XXX assumes short preamble, include SIFS */225tt += (long_retries+1)*ath_hal_pkt_txtime(sc->sc_ah, rt, length, rix,226is_ht40, 0, 1);227228tt += (long_retries+1)*(t_sifs + rt->info[rix].spAckDuration);229230for (x = 0; x <= short_retries + long_retries; x++) {231cw = MIN(WIFI_CW_MAX, (cw + 1) * 2);232tt += (t_slot * cw/2);233}234return tt;235}236237#endif /* _KERNEL */238239#endif /* _DEV_ATH_RATE_SAMPLE_H */240241242