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