]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ath/ath_rate/onoe/onoe.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / ath / ath_rate / onoe / onoe.c
1 /*-
2  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
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  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * Atsushi Onoe's rate control algorithm.
35  */
36 #include "opt_inet.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h> 
40 #include <sys/sysctl.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/errno.h>
45
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <sys/bus.h>
49
50 #include <sys/socket.h>
51  
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h>               /* XXX for ether_sprintf */
56
57 #include <net80211/ieee80211_var.h>
58
59 #include <net/bpf.h>
60
61 #ifdef INET
62 #include <netinet/in.h> 
63 #include <netinet/if_ether.h>
64 #endif
65
66 #include <dev/ath/if_athvar.h>
67 #include <dev/ath/ath_rate/onoe/onoe.h>
68 #include <dev/ath/ath_hal/ah_desc.h>
69
70 #define ONOE_DEBUG
71 #ifdef ONOE_DEBUG
72 enum {
73         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
74 };
75 #define DPRINTF(sc, _fmt, ...) do {                             \
76         if (sc->sc_debug & ATH_DEBUG_RATE)                      \
77                 printf(_fmt, __VA_ARGS__);                      \
78 } while (0)
79 #else
80 #define DPRINTF(sc, _fmt, ...)
81 #endif
82
83 /*
84  * Default parameters for the rate control algorithm.  These are
85  * all tunable with sysctls.  The rate controller runs periodically
86  * (each ath_rateinterval ms) analyzing transmit statistics for each
87  * neighbor/station (when operating in station mode this is only the AP).
88  * If transmits look to be working well over a sampling period then
89  * it gives a "raise rate credit".  If transmits look to not be working
90  * well than it deducts a credit.  If the credits cross a threshold then
91  * the transmit rate is raised.  Various error conditions force the
92  * the transmit rate to be dropped.
93  *
94  * The decision to issue/deduct a credit is based on the errors and
95  * retries accumulated over the sampling period.  ath_rate_raise defines
96  * the percent of retransmits for which a credit is issued/deducted.
97  * ath_rate_raise_threshold defines the threshold on credits at which
98  * the transmit rate is increased.
99  *
100  * XXX this algorithm is flawed.
101  */
102 static  int ath_rateinterval = 1000;            /* rate ctl interval (ms)  */
103 static  int ath_rate_raise = 10;                /* add credit threshold */
104 static  int ath_rate_raise_threshold = 10;      /* rate ctl raise threshold */
105
106 static void     ath_ratectl(void *);
107 static void     ath_rate_update(struct ath_softc *, struct ieee80211_node *,
108                         int rate);
109 static void     ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
110 static void     ath_rate_ctl(void *, struct ieee80211_node *);
111
112 void
113 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
114 {
115         /* NB: assumed to be zero'd by caller */
116         ath_rate_update(sc, &an->an_node, 0);
117 }
118
119 void
120 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
121 {
122 }
123
124 void
125 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
126         int shortPreamble, size_t frameLen,
127         u_int8_t *rix, int *try0, u_int8_t *txrate)
128 {
129         struct onoe_node *on = ATH_NODE_ONOE(an);
130
131         *rix = on->on_tx_rix0;
132         *try0 = on->on_tx_try0;
133         if (shortPreamble)
134                 *txrate = on->on_tx_rate0sp;
135         else
136                 *txrate = on->on_tx_rate0;
137 }
138
139 void
140 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
141         struct ath_desc *ds, int shortPreamble, u_int8_t rix)
142 {
143         struct onoe_node *on = ATH_NODE_ONOE(an);
144
145         ath_hal_setupxtxdesc(sc->sc_ah, ds
146                 , on->on_tx_rate1sp, 2  /* series 1 */
147                 , on->on_tx_rate2sp, 2  /* series 2 */
148                 , on->on_tx_rate3sp, 2  /* series 3 */
149         );
150 }
151
152 void
153 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
154         const struct ath_buf *bf)
155 {
156         struct onoe_node *on = ATH_NODE_ONOE(an);
157         const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
158
159         if (ts->ts_status == 0)
160                 on->on_tx_ok++;
161         else
162                 on->on_tx_err++;
163         on->on_tx_retr += ts->ts_shortretry
164                         + ts->ts_longretry;
165 }
166
167 void
168 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
169 {
170         if (isnew)
171                 ath_rate_ctl_start(sc, &an->an_node);
172 }
173
174 static void
175 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
176 {
177         struct ath_node *an = ATH_NODE(ni);
178         struct onoe_node *on = ATH_NODE_ONOE(an);
179         const HAL_RATE_TABLE *rt = sc->sc_currates;
180         u_int8_t rix;
181
182         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
183
184         DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
185             __func__, ether_sprintf(ni->ni_macaddr),
186             ni->ni_rates.rs_nrates > 0 ?
187                 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
188
189         ni->ni_txrate = rate;
190         /*
191          * Before associating a node has no rate set setup
192          * so we can't calculate any transmit codes to use.
193          * This is ok since we should never be sending anything
194          * but management frames and those always go at the
195          * lowest hardware rate.
196          */
197         if (ni->ni_rates.rs_nrates == 0)
198                 goto done;
199         on->on_tx_rix0 = sc->sc_rixmap[
200                 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
201         on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
202         
203         on->on_tx_rate0sp = on->on_tx_rate0 |
204                 rt->info[on->on_tx_rix0].shortPreamble;
205         if (sc->sc_mrretry) {
206                 /*
207                  * Hardware supports multi-rate retry; setup two
208                  * step-down retry rates and make the lowest rate
209                  * be the ``last chance''.  We use 4, 2, 2, 2 tries
210                  * respectively (4 is set here, the rest are fixed
211                  * in the xmit routine).
212                  */
213                 on->on_tx_try0 = 1 + 3;         /* 4 tries at rate 0 */
214                 if (--rate >= 0) {
215                         rix = sc->sc_rixmap[
216                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
217                         on->on_tx_rate1 = rt->info[rix].rateCode;
218                         on->on_tx_rate1sp = on->on_tx_rate1 |
219                                 rt->info[rix].shortPreamble;
220                 } else {
221                         on->on_tx_rate1 = on->on_tx_rate1sp = 0;
222                 }
223                 if (--rate >= 0) {
224                         rix = sc->sc_rixmap[
225                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
226                         on->on_tx_rate2 = rt->info[rix].rateCode;
227                         on->on_tx_rate2sp = on->on_tx_rate2 |
228                                 rt->info[rix].shortPreamble;
229                 } else {
230                         on->on_tx_rate2 = on->on_tx_rate2sp = 0;
231                 }
232                 if (rate > 0) {
233                         /* NB: only do this if we didn't already do it above */
234                         on->on_tx_rate3 = rt->info[0].rateCode;
235                         on->on_tx_rate3sp =
236                                 on->on_tx_rate3 | rt->info[0].shortPreamble;
237                 } else {
238                         on->on_tx_rate3 = on->on_tx_rate3sp = 0;
239                 }
240         } else {
241                 on->on_tx_try0 = ATH_TXMAXTRY;  /* max tries at rate 0 */
242                 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
243                 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
244                 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
245         }
246 done:
247         on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
248 }
249
250 /*
251  * Set the starting transmit rate for a node.
252  */
253 static void
254 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
255 {
256 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
257         struct ieee80211com *ic = &sc->sc_ic;
258         int srate;
259
260         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
261         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
262                 /*
263                  * No fixed rate is requested. For 11b start with
264                  * the highest negotiated rate; otherwise, for 11g
265                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
266                  */
267                 srate = ni->ni_rates.rs_nrates - 1;
268                 if (sc->sc_curmode != IEEE80211_MODE_11B) {
269                         /*
270                          * Scan the negotiated rate set to find the
271                          * closest rate.
272                          */
273                         /* NB: the rate set is assumed sorted */
274                         for (; srate >= 0 && RATE(srate) > 72; srate--)
275                                 ;
276                 }
277         } else {
278                 /*
279                  * A fixed rate is to be used; ic_fixed_rate is the
280                  * IEEE code for this rate (sans basic bit).  Convert this
281                  * to the index into the negotiated rate set for
282                  * the node.  We know the rate is there because the
283                  * rate set is checked when the station associates.
284                  */
285                 /* NB: the rate set is assumed sorted */
286                 srate = ni->ni_rates.rs_nrates - 1;
287                 for (; srate >= 0 && RATE(srate) != ic->ic_fixed_rate; srate--)
288                         ;
289         }
290         /*
291          * The selected rate may not be available due to races
292          * and mode settings.  Also orphaned nodes created in
293          * adhoc mode may not have any rate set so this lookup
294          * can fail.  This is not fatal.
295          */
296         ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
297 #undef RATE
298 }
299
300 static void
301 ath_rate_cb(void *arg, struct ieee80211_node *ni)
302 {
303         struct ath_softc *sc = arg;
304
305         ath_rate_update(sc, ni, 0);
306 }
307
308 /*
309  * Reset the rate control state for each 802.11 state transition.
310  */
311 void
312 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
313 {
314         struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
315         struct ieee80211com *ic = &sc->sc_ic;
316         struct ieee80211_node *ni;
317
318         if (state == IEEE80211_S_INIT) {
319                 callout_stop(&osc->timer);
320                 return;
321         }
322         if (ic->ic_opmode == IEEE80211_M_STA) {
323                 /*
324                  * Reset local xmit state; this is really only
325                  * meaningful when operating in station mode.
326                  */
327                 ni = ic->ic_bss;
328                 if (state == IEEE80211_S_RUN) {
329                         ath_rate_ctl_start(sc, ni);
330                 } else {
331                         ath_rate_update(sc, ni, 0);
332                 }
333         } else {
334                 /*
335                  * When operating as a station the node table holds
336                  * the AP's that were discovered during scanning.
337                  * For any other operating mode we want to reset the
338                  * tx rate state of each node.
339                  */
340                 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
341                 ath_rate_update(sc, ic->ic_bss, 0);
342         }
343         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
344             state == IEEE80211_S_RUN) {
345                 int interval;
346                 /*
347                  * Start the background rate control thread if we
348                  * are not configured to use a fixed xmit rate.
349                  */
350                 interval = ath_rateinterval;
351                 if (ic->ic_opmode == IEEE80211_M_STA)
352                         interval /= 2;
353                 callout_reset(&osc->timer, (interval * hz) / 1000,
354                         ath_ratectl, sc->sc_ifp);
355         }
356 }
357
358 /* 
359  * Examine and potentially adjust the transmit rate.
360  */
361 static void
362 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
363 {
364         struct ath_softc *sc = arg;
365         struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
366         struct ieee80211_rateset *rs = &ni->ni_rates;
367         int dir = 0, nrate, enough;
368
369         /*
370          * Rate control
371          * XXX: very primitive version.
372          */
373         enough = (on->on_tx_ok + on->on_tx_err >= 10);
374
375         /* no packet reached -> down */
376         if (on->on_tx_err > 0 && on->on_tx_ok == 0)
377                 dir = -1;
378
379         /* all packets needs retry in average -> down */
380         if (enough && on->on_tx_ok < on->on_tx_retr)
381                 dir = -1;
382
383         /* no error and less than rate_raise% of packets need retry -> up */
384         if (enough && on->on_tx_err == 0 &&
385             on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
386                 dir = 1;
387
388         DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
389                 ether_sprintf(ni->ni_macaddr),
390                 on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
391                 on->on_tx_upper, dir);
392
393         nrate = ni->ni_txrate;
394         switch (dir) {
395         case 0:
396                 if (enough && on->on_tx_upper > 0)
397                         on->on_tx_upper--;
398                 break;
399         case -1:
400                 if (nrate > 0) {
401                         nrate--;
402                         sc->sc_stats.ast_rate_drop++;
403                 }
404                 on->on_tx_upper = 0;
405                 break;
406         case 1:
407                 /* raise rate if we hit rate_raise_threshold */
408                 if (++on->on_tx_upper < ath_rate_raise_threshold)
409                         break;
410                 on->on_tx_upper = 0;
411                 if (nrate + 1 < rs->rs_nrates) {
412                         nrate++;
413                         sc->sc_stats.ast_rate_raise++;
414                 }
415                 break;
416         }
417
418         if (nrate != ni->ni_txrate) {
419                 DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
420                     __func__,
421                     (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
422                     (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
423                     on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
424                 ath_rate_update(sc, ni, nrate);
425         } else if (enough)
426                 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
427 }
428
429 static void
430 ath_ratectl(void *arg)
431 {
432         struct ifnet *ifp = arg;
433         struct ath_softc *sc = ifp->if_softc;
434         struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
435         struct ieee80211com *ic = &sc->sc_ic;
436         int interval;
437
438         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
439                 sc->sc_stats.ast_rate_calls++;
440
441                 if (ic->ic_opmode == IEEE80211_M_STA)
442                         ath_rate_ctl(sc, ic->ic_bss);   /* NB: no reference */
443                 else
444                         ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
445         }
446         interval = ath_rateinterval;
447         if (ic->ic_opmode == IEEE80211_M_STA)
448                 interval /= 2;
449         callout_reset(&osc->timer, (interval * hz) / 1000,
450                 ath_ratectl, sc->sc_ifp);
451 }
452
453 static void
454 ath_rate_sysctlattach(struct ath_softc *sc)
455 {
456         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
457         struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
458
459         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
460                 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
461                 "rate control: operation interval (ms)");
462         /* XXX bounds check values */
463         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
464                 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
465                 "rate control: retry threshold to credit rate raise (%%)");
466         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
467                 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
468                 "rate control: # good periods before raising rate");
469 }
470
471 struct ath_ratectrl *
472 ath_rate_attach(struct ath_softc *sc)
473 {
474         struct onoe_softc *osc;
475
476         osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
477         if (osc == NULL)
478                 return NULL;
479         osc->arc.arc_space = sizeof(struct onoe_node);
480         callout_init(&osc->timer, CALLOUT_MPSAFE);
481         ath_rate_sysctlattach(sc);
482
483         return &osc->arc;
484 }
485
486 void
487 ath_rate_detach(struct ath_ratectrl *arc)
488 {
489         struct onoe_softc *osc = (struct onoe_softc *) arc;
490
491         callout_drain(&osc->timer);
492         free(osc, M_DEVBUF);
493 }