b63213eb1f3f2b22e8bbb74fe0cdfb6e8ba38b1c
[dragonfly.git] / sys / dev / netif / ath / rate_onoe / onoe.c
1 /*
2  * Copyright (c) 2002-2005 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  * 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/onoe/onoe.c,v 1.8.2.3 2006/02/24 19:51:11 sam Exp $
37  * $DragonFly: src/sys/dev/netif/ath/rate_onoe/onoe.c,v 1.2 2006/09/05 00:55:39 dillon Exp $
38  */
39
40 /*
41  * Atsushi Onoe's rate control algorithm.
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h> 
46 #include <sys/sysctl.h>
47 #include <sys/module.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/errno.h>
51 #include <sys/serialize.h>
52
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <sys/bus.h>
56
57 #include <sys/socket.h>
58  
59 #include <net/if.h>
60 #include <net/if_media.h>
61 #include <net/if_arp.h>
62 #include <net/ethernet.h>               /* XXX for ether_sprintf */
63
64 #include <netproto/802_11/ieee80211_var.h>
65
66 #include <net/bpf.h>
67
68 #include <dev/netif/ath/ath/if_athvar.h>
69 #include <dev/netif/ath/rate_onoe/onoe.h>
70 #include <contrib/dev/ath/ah_desc.h>
71
72 #define ONOE_DEBUG
73 #ifdef ONOE_DEBUG
74 enum {
75         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
76 };
77 #define DPRINTF(sc, _fmt, ...) do {                             \
78         if (sc->sc_debug & ATH_DEBUG_RATE)                      \
79                 printf(_fmt, __VA_ARGS__);                      \
80 } while (0)
81 #else
82 #define DPRINTF(sc, _fmt, ...)
83 #endif
84
85 /*
86  * Default parameters for the rate control algorithm.  These are
87  * all tunable with sysctls.  The rate controller runs periodically
88  * (each ath_rateinterval ms) analyzing transmit statistics for each
89  * neighbor/station (when operating in station mode this is only the AP).
90  * If transmits look to be working well over a sampling period then
91  * it gives a "raise rate credit".  If transmits look to not be working
92  * well than it deducts a credit.  If the credits cross a threshold then
93  * the transmit rate is raised.  Various error conditions force the
94  * the transmit rate to be dropped.
95  *
96  * The decision to issue/deduct a credit is based on the errors and
97  * retries accumulated over the sampling period.  ath_rate_raise defines
98  * the percent of retransmits for which a credit is issued/deducted.
99  * ath_rate_raise_threshold defines the threshold on credits at which
100  * the transmit rate is increased.
101  *
102  * XXX this algorithm is flawed.
103  */
104 static  int ath_rateinterval = 1000;            /* rate ctl interval (ms)  */
105 static  int ath_rate_raise = 10;                /* add credit threshold */
106 static  int ath_rate_raise_threshold = 10;      /* rate ctl raise threshold */
107
108 static void     ath_ratectl(void *);
109 static void     ath_rate_update(struct ath_softc *, struct ieee80211_node *,
110                         int rate);
111 static void     ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
112 static void     ath_rate_ctl(void *, struct ieee80211_node *);
113
114 void
115 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
116 {
117         /* NB: assumed to be zero'd by caller */
118         ath_rate_update(sc, &an->an_node, 0);
119 }
120
121 void
122 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
123 {
124 }
125
126 void
127 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
128         int shortPreamble, size_t frameLen,
129         uint8_t *rix, int *try0, uint8_t *txrate)
130 {
131         struct onoe_node *on = ATH_NODE_ONOE(an);
132
133         *rix = on->on_tx_rix0;
134         *try0 = on->on_tx_try0;
135         if (shortPreamble)
136                 *txrate = on->on_tx_rate0sp;
137         else
138                 *txrate = on->on_tx_rate0;
139 }
140
141 void
142 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
143         struct ath_desc *ds, int shortPreamble, uint8_t rix)
144 {
145         struct onoe_node *on = ATH_NODE_ONOE(an);
146
147         ath_hal_setupxtxdesc(sc->sc_ah, ds
148                 , on->on_tx_rate1sp, 2  /* series 1 */
149                 , on->on_tx_rate2sp, 2  /* series 2 */
150                 , on->on_tx_rate3sp, 2  /* series 3 */
151         );
152 }
153
154 void
155 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
156         const struct ath_desc *ds, const struct ath_desc *ds0)
157 {
158         struct onoe_node *on = ATH_NODE_ONOE(an);
159
160         if (ds->ds_txstat.ts_status == 0)
161                 on->on_tx_ok++;
162         else
163                 on->on_tx_err++;
164         on->on_tx_retr += ds->ds_txstat.ts_shortretry
165                         + ds->ds_txstat.ts_longretry;
166 }
167
168 void
169 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
170 {
171         if (isnew)
172                 ath_rate_ctl_start(sc, &an->an_node);
173 }
174
175 static void
176 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
177 {
178         struct ath_node *an = ATH_NODE(ni);
179         struct onoe_node *on = ATH_NODE_ONOE(an);
180         const HAL_RATE_TABLE *rt = sc->sc_currates;
181         uint8_t rix;
182
183         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
184
185         DPRINTF(sc, "%s: set xmit rate for %6D to %dM\n",
186             __func__, ni->ni_macaddr, ":",
187             ni->ni_rates.rs_nrates > 0 ?
188                 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
189
190         ni->ni_txrate = rate;
191         /*
192          * Before associating a node has no rate set setup
193          * so we can't calculate any transmit codes to use.
194          * This is ok since we should never be sending anything
195          * but management frames and those always go at the
196          * lowest hardware rate.
197          */
198         if (ni->ni_rates.rs_nrates == 0)
199                 goto done;
200         on->on_tx_rix0 = sc->sc_rixmap[
201                 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
202         on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
203         
204         on->on_tx_rate0sp = on->on_tx_rate0 |
205                 rt->info[on->on_tx_rix0].shortPreamble;
206         if (sc->sc_mrretry) {
207                 /*
208                  * Hardware supports multi-rate retry; setup two
209                  * step-down retry rates and make the lowest rate
210                  * be the ``last chance''.  We use 4, 2, 2, 2 tries
211                  * respectively (4 is set here, the rest are fixed
212                  * in the xmit routine).
213                  */
214                 on->on_tx_try0 = 1 + 3;         /* 4 tries at rate 0 */
215                 if (--rate >= 0) {
216                         rix = sc->sc_rixmap[
217                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
218                         on->on_tx_rate1 = rt->info[rix].rateCode;
219                         on->on_tx_rate1sp = on->on_tx_rate1 |
220                                 rt->info[rix].shortPreamble;
221                 } else {
222                         on->on_tx_rate1 = on->on_tx_rate1sp = 0;
223                 }
224                 if (--rate >= 0) {
225                         rix = sc->sc_rixmap[
226                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
227                         on->on_tx_rate2 = rt->info[rix].rateCode;
228                         on->on_tx_rate2sp = on->on_tx_rate2 |
229                                 rt->info[rix].shortPreamble;
230                 } else {
231                         on->on_tx_rate2 = on->on_tx_rate2sp = 0;
232                 }
233                 if (rate > 0) {
234                         /* NB: only do this if we didn't already do it above */
235                         on->on_tx_rate3 = rt->info[0].rateCode;
236                         on->on_tx_rate3sp =
237                                 on->on_tx_rate3 | rt->info[0].shortPreamble;
238                 } else {
239                         on->on_tx_rate3 = on->on_tx_rate3sp = 0;
240                 }
241         } else {
242                 on->on_tx_try0 = ATH_TXMAXTRY;  /* max tries at rate 0 */
243                 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
244                 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
245                 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
246         }
247 done:
248         on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
249 }
250
251 /*
252  * Set the starting transmit rate for a node.
253  */
254 static void
255 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
256 {
257 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
258         struct ieee80211com *ic = &sc->sc_ic;
259         int srate;
260
261         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
262         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
263                 /*
264                  * No fixed rate is requested. For 11b start with
265                  * the highest negotiated rate; otherwise, for 11g
266                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
267                  */
268                 srate = ni->ni_rates.rs_nrates - 1;
269                 if (sc->sc_curmode != IEEE80211_MODE_11B) {
270                         /*
271                          * Scan the negotiated rate set to find the
272                          * closest rate.
273                          */
274                         /* NB: the rate set is assumed sorted */
275                         for (; srate >= 0 && RATE(srate) > 72; srate--)
276                                 ;
277                         KASSERT(srate >= 0, ("bogus rate set"));
278                 }
279         } else {
280                 /*
281                  * A fixed rate is to be used; ic_fixed_rate is an
282                  * index into the supported rate set.  Convert this
283                  * to the index into the negotiated rate set for
284                  * the node.  We know the rate is there because the
285                  * rate set is checked when the station associates.
286                  */
287                 const struct ieee80211_rateset *rs =
288                         &ic->ic_sup_rates[ic->ic_curmode];
289                 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
290                 /* NB: the rate set is assumed sorted */
291                 srate = ni->ni_rates.rs_nrates - 1;
292                 for (; srate >= 0 && RATE(srate) != r; srate--)
293                         ;
294                 KASSERT(srate >= 0,
295                         ("fixed rate %d not in rate set", ic->ic_fixed_rate));
296         }
297         ath_rate_update(sc, ni, srate);
298 #undef RATE
299 }
300
301 static void
302 ath_rate_cb(void *arg, struct ieee80211_node *ni)
303 {
304         struct ath_softc *sc = arg;
305
306         ath_rate_update(sc, ni, 0);
307 }
308
309 /*
310  * Reset the rate control state for each 802.11 state transition.
311  */
312 void
313 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
314 {
315         struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
316         struct ieee80211com *ic = &sc->sc_ic;
317         struct ifnet *ifp = &ic->ic_if;
318         struct ieee80211_node *ni;
319
320         ASSERT_SERIALIZED(ifp->if_serializer);
321
322         if (state == IEEE80211_S_INIT) {
323                 callout_stop(&osc->timer);
324                 return;
325         }
326         if (ic->ic_opmode == IEEE80211_M_STA) {
327                 /*
328                  * Reset local xmit state; this is really only
329                  * meaningful when operating in station mode.
330                  */
331                 ni = ic->ic_bss;
332                 if (state == IEEE80211_S_RUN) {
333                         ath_rate_ctl_start(sc, ni);
334                 } else {
335                         ath_rate_update(sc, ni, 0);
336                 }
337         } else {
338                 /*
339                  * When operating as a station the node table holds
340                  * the AP's that were discovered during scanning.
341                  * For any other operating mode we want to reset the
342                  * tx rate state of each node.
343                  */
344                 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
345                 ath_rate_update(sc, ic->ic_bss, 0);
346         }
347         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
348             state == IEEE80211_S_RUN) {
349                 int interval;
350                 /*
351                  * Start the background rate control thread if we
352                  * are not configured to use a fixed xmit rate.
353                  */
354                 interval = ath_rateinterval;
355                 if (ic->ic_opmode == IEEE80211_M_STA)
356                         interval /= 2;
357                 callout_reset(&osc->timer, (interval * hz) / 1000,
358                               ath_ratectl, ifp);
359         }
360 }
361
362 /* 
363  * Examine and potentially adjust the transmit rate.
364  */
365 static void
366 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
367 {
368         struct ath_softc *sc = arg;
369         struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
370         struct ieee80211_rateset *rs = &ni->ni_rates;
371         int dir = 0, nrate, enough;
372
373         /*
374          * Rate control
375          * XXX: very primitive version.
376          */
377         enough = (on->on_tx_ok + on->on_tx_err >= 10);
378
379         /* no packet reached -> down */
380         if (on->on_tx_err > 0 && on->on_tx_ok == 0)
381                 dir = -1;
382
383         /* all packets needs retry in average -> down */
384         if (enough && on->on_tx_ok < on->on_tx_retr)
385                 dir = -1;
386
387         /* no error and less than rate_raise% of packets need retry -> up */
388         if (enough && on->on_tx_err == 0 &&
389             on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
390                 dir = 1;
391
392         DPRINTF(sc, "%6D: ok %d err %d retr %d upper %d dir %d\n",
393                 ni->ni_macaddr, ":",
394                 on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
395                 on->on_tx_upper, dir);
396
397         nrate = ni->ni_txrate;
398         switch (dir) {
399         case 0:
400                 if (enough && on->on_tx_upper > 0)
401                         on->on_tx_upper--;
402                 break;
403         case -1:
404                 if (nrate > 0) {
405                         nrate--;
406                         sc->sc_stats.ast_rate_drop++;
407                 }
408                 on->on_tx_upper = 0;
409                 break;
410         case 1:
411                 /* raise rate if we hit rate_raise_threshold */
412                 if (++on->on_tx_upper < ath_rate_raise_threshold)
413                         break;
414                 on->on_tx_upper = 0;
415                 if (nrate + 1 < rs->rs_nrates) {
416                         nrate++;
417                         sc->sc_stats.ast_rate_raise++;
418                 }
419                 break;
420         }
421
422         if (nrate != ni->ni_txrate) {
423                 DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
424                     __func__,
425                     (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
426                     (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
427                     on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
428                 ath_rate_update(sc, ni, nrate);
429         } else if (enough) {
430                 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
431         }
432 }
433
434 static void
435 ath_ratectl(void *arg)
436 {
437         struct ifnet *ifp = arg;
438         struct ath_softc *sc = ifp->if_softc;
439         struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
440         struct ieee80211com *ic = &sc->sc_ic;
441         int interval;
442
443         lwkt_serialize_enter(ifp->if_serializer);
444
445         if (ifp->if_flags & IFF_RUNNING) {
446                 sc->sc_stats.ast_rate_calls++;
447
448                 if (ic->ic_opmode == IEEE80211_M_STA)
449                         ath_rate_ctl(sc, ic->ic_bss);   /* NB: no reference */
450                 else
451                         ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
452         }
453         interval = ath_rateinterval;
454         if (ic->ic_opmode == IEEE80211_M_STA)
455                 interval /= 2;
456         callout_reset(&osc->timer, (interval * hz) / 1000, ath_ratectl, ifp);
457
458         lwkt_serialize_exit(ifp->if_serializer);
459 }
460
461 static void
462 ath_rate_sysctlattach(struct ath_softc *sc)
463 {
464         struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
465         struct sysctl_oid *tree = sc->sc_sysctl_tree;
466
467         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
468                 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
469                 "rate control: operation interval (ms)");
470         /* XXX bounds check values */
471         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
472                 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
473                 "rate control: retry threshold to credit rate raise (%%)");
474         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
475                 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
476                 "rate control: # good periods before raising rate");
477 }
478
479 struct ath_ratectrl *
480 ath_rate_attach(struct ath_softc *sc)
481 {
482         struct onoe_softc *osc;
483
484         osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_WAITOK | M_ZERO);
485         osc->arc.arc_space = sizeof(struct onoe_node);
486         callout_init(&osc->timer);
487         ath_rate_sysctlattach(sc);
488         return &osc->arc;
489 }
490
491 void
492 ath_rate_detach(struct ath_ratectrl *arc)
493 {
494         struct onoe_softc *osc = (struct onoe_softc *) arc;
495
496         kfree(osc, M_DEVBUF);
497 }
498
499 void
500 ath_rate_stop(struct ath_ratectrl *arc)
501 {
502         struct onoe_softc *osc = (struct onoe_softc *) arc;
503
504         /* ASSERT_SERIALIZED */
505         callout_stop(&osc->timer);
506 }
507
508 /*
509  * Module glue.
510  */
511 static int
512 onoe_modevent(module_t mod, int type, void *unused)
513 {
514         switch (type) {
515         case MOD_LOAD:
516                 if (bootverbose)
517                         printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n");
518                 return 0;
519         case MOD_UNLOAD:
520                 return 0;
521         }
522         return EINVAL;
523 }
524
525 static moduledata_t onoe_mod = {
526         "ath_rate",
527         onoe_modevent,
528         0
529 };
530 DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
531 MODULE_VERSION(ath_rate, 1);
532 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);