kern - Utilize new way of printing MAC addresses.
[dragonfly.git] / sys / dev / netif / ath / rate_amrr / amrr.c
1 /*
2  * Copyright (c) 2004 INRIA
3  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    without modification.
12  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14  *    redistribution must be conditioned upon including a substantially
15  *    similar Disclaimer requirement for further binary redistribution.
16  * 3. Neither the names of the above-listed copyright holders nor the names
17  *    of any contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * NO WARRANTY
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
28  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
30  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33  * 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 OF
35  * THE POSSIBILITY OF SUCH DAMAGES.
36  *
37  * $FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.12 2006/12/13 19:34:34 sam Exp $
38  */
39
40 /*
41  * AMRR rate control. See:
42  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
43  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
44  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h> 
49 #include <sys/sysctl.h>
50 #include <sys/module.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/errno.h>
54 #include <sys/serialize.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
63 #include <netproto/802_11/ieee80211_var.h>
64
65 #include <net/bpf.h>
66
67 #include <dev/netif/ath/ath/if_athvar.h>
68 #include <dev/netif/ath/rate_amrr/amrr.h>
69 #include <dev/netif/ath/hal/ath_hal/ah_desc.h>
70
71 #define AMRR_DEBUG
72 #ifdef AMRR_DEBUG
73 #define DPRINTF(sc, _fmt, ...) do {                                     \
74         if (sc->sc_debug & 0x10)                                        \
75                 kprintf(_fmt, __VA_ARGS__);                             \
76 } while (0)
77 #else
78 #define DPRINTF(sc, _fmt, ...)
79 #endif
80
81 static  int ath_rateinterval = 1000;            /* rate ctl interval (ms)  */
82 static  int ath_rate_max_success_threshold = 10;
83 static  int ath_rate_min_success_threshold = 1;
84
85 static void     ath_ratectl_callout(void *);
86 static void     ath_rate_update(struct ath_softc *, struct ieee80211_node *,
87                         int rate);
88 static void     ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
89 static void     ath_rate_ctl(void *, struct ieee80211_node *);
90
91 void
92 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
93 {
94         /* NB: assumed to be zero'd by caller */
95         ath_rate_update(sc, &an->an_node, 0);
96 }
97
98 void
99 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
100 {
101 }
102
103 void
104 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
105         int shortPreamble, size_t frameLen,
106         uint8_t *rix, int *try0, uint8_t *txrate)
107 {
108         struct amrr_node *amn = ATH_NODE_AMRR(an);
109
110         *rix = amn->amn_tx_rix0;
111         *try0 = amn->amn_tx_try0;
112         if (shortPreamble)
113                 *txrate = amn->amn_tx_rate0sp;
114         else
115                 *txrate = amn->amn_tx_rate0;
116 }
117
118 void
119 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
120         struct ath_desc *ds, int shortPreamble, uint8_t rix)
121 {
122         struct amrr_node *amn = ATH_NODE_AMRR(an);
123
124         ath_hal_setupxtxdesc(sc->sc_ah, ds
125                 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
126                 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
127                 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
128         );
129 }
130
131 void
132 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
133         const struct ath_buf *bf)
134 {
135         struct amrr_node *amn = ATH_NODE_AMRR(an);
136         const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
137         int sr = ts->ts_shortretry;
138         int lr = ts->ts_longretry;
139         int retry_count = sr + lr;
140
141         amn->amn_tx_try0_cnt++;
142         if (retry_count == 1) {
143                 amn->amn_tx_try1_cnt++;
144         } else if (retry_count == 2) {
145                 amn->amn_tx_try1_cnt++;
146                 amn->amn_tx_try2_cnt++;
147         } else if (retry_count == 3) {
148                 amn->amn_tx_try1_cnt++;
149                 amn->amn_tx_try2_cnt++;
150                 amn->amn_tx_try3_cnt++;
151         } else if (retry_count > 3) {
152                 amn->amn_tx_try1_cnt++;
153                 amn->amn_tx_try2_cnt++;
154                 amn->amn_tx_try3_cnt++;
155                 amn->amn_tx_failure_cnt++;
156         }
157 }
158
159 void
160 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
161 {
162         if (isnew)
163                 ath_rate_ctl_start(sc, &an->an_node);
164 }
165
166 static void 
167 node_reset (struct amrr_node *amn)
168 {
169         amn->amn_tx_try0_cnt = 0;
170         amn->amn_tx_try1_cnt = 0;
171         amn->amn_tx_try2_cnt = 0;
172         amn->amn_tx_try3_cnt = 0;
173         amn->amn_tx_failure_cnt = 0;
174         amn->amn_success = 0;
175         amn->amn_recovery = 0;
176         amn->amn_success_threshold = ath_rate_min_success_threshold;
177 }
178
179
180 /**
181  * The code below assumes that we are dealing with hardware multi rate retry
182  * I have no idea what will happen if you try to use this module with another
183  * type of hardware. Your machine might catch fire or it might work with
184  * horrible performance...
185  */
186 static void
187 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
188 {
189         struct ath_node *an = ATH_NODE(ni);
190         struct amrr_node *amn = ATH_NODE_AMRR(an);
191         const HAL_RATE_TABLE *rt = sc->sc_currates;
192         char ethstr[ETHER_ADDRSTRLEN + 1];
193         uint8_t rix;
194
195         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
196
197         DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
198             __func__, kether_ntoa(ni->ni_macaddr, ethstr),
199             ni->ni_rates.rs_nrates > 0 ?
200                 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
201
202         ni->ni_txrate = rate;
203         /*
204          * Before associating a node has no rate set setup
205          * so we can't calculate any transmit codes to use.
206          * This is ok since we should never be sending anything
207          * but management frames and those always go at the
208          * lowest hardware rate.
209          */
210         if (ni->ni_rates.rs_nrates > 0) {
211                 amn->amn_tx_rix0 = sc->sc_rixmap[
212                                                ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
213                 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
214                 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
215                         rt->info[amn->amn_tx_rix0].shortPreamble;
216                 if (sc->sc_mrretry) {
217                         amn->amn_tx_try0 = 1;
218                         amn->amn_tx_try1 = 1;
219                         amn->amn_tx_try2 = 1;
220                         amn->amn_tx_try3 = 1;
221                         if (--rate >= 0) {
222                                 rix = sc->sc_rixmap[
223                                                     ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
224                                 amn->amn_tx_rate1 = rt->info[rix].rateCode;
225                                 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
226                                         rt->info[rix].shortPreamble;
227                         } else {
228                                 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
229                         }
230                         if (--rate >= 0) {
231                                 rix = sc->sc_rixmap[
232                                                     ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
233                                 amn->amn_tx_rate2 = rt->info[rix].rateCode;
234                                 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
235                                         rt->info[rix].shortPreamble;
236                         } else {
237                                 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
238                         }
239                         if (rate > 0) {
240                                 /* NB: only do this if we didn't already do it above */
241                                 amn->amn_tx_rate3 = rt->info[0].rateCode;
242                                 amn->amn_tx_rate3sp =
243                                         amn->amn_tx_rate3 | rt->info[0].shortPreamble;
244                         } else {
245                                 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
246                         }
247                 } else {
248                         amn->amn_tx_try0 = ATH_TXMAXTRY;
249                         /* theorically, these statements are useless because
250                          *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
251                          */
252                         amn->amn_tx_try1 = 0;
253                         amn->amn_tx_try2 = 0;
254                         amn->amn_tx_try3 = 0;
255                         amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
256                         amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
257                         amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
258                 }
259         }
260         node_reset (amn);
261 }
262
263 /*
264  * Set the starting transmit rate for a node.
265  */
266 static void
267 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
268 {
269 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
270         struct ieee80211com *ic = &sc->sc_ic;
271         int srate;
272
273         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
274         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
275                 /*
276                  * No fixed rate is requested. For 11b start with
277                  * the highest negotiated rate; otherwise, for 11g
278                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
279                  */
280                 srate = ni->ni_rates.rs_nrates - 1;
281                 if (sc->sc_curmode != IEEE80211_MODE_11B) {
282                         /*
283                          * Scan the negotiated rate set to find the
284                          * closest rate.
285                          */
286                         /* NB: the rate set is assumed sorted */
287                         for (; srate >= 0 && RATE(srate) > 72; srate--)
288                                 ;
289                         KASSERT(srate >= 0, ("bogus rate set"));
290                 }
291         } else {
292                 /*
293                  * A fixed rate is to be used; ic_fixed_rate is an
294                  * index into the supported rate set.  Convert this
295                  * to the index into the negotiated rate set for
296                  * the node.  We know the rate is there because the
297                  * rate set is checked when the station associates.
298                  */
299                 const struct ieee80211_rateset *rs =
300                         &ic->ic_sup_rates[ic->ic_curmode];
301                 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
302                 /* NB: the rate set is assumed sorted */
303                 srate = ni->ni_rates.rs_nrates - 1;
304                 for (; srate >= 0 && RATE(srate) != r; srate--)
305                         ;
306                 KASSERT(srate >= 0,
307                         ("fixed rate %d not in rate set", ic->ic_fixed_rate));
308         }
309         ath_rate_update(sc, ni, srate);
310 #undef RATE
311 }
312
313 static void
314 ath_rate_cb(void *arg, struct ieee80211_node *ni)
315 {
316         struct ath_softc *sc = arg;
317
318         ath_rate_update(sc, ni, 0);
319 }
320
321 /*
322  * Reset the rate control state for each 802.11 state transition.
323  */
324 void
325 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
326 {
327         struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
328         struct ieee80211com *ic = &sc->sc_ic;
329         struct ifnet *ifp = &ic->ic_if;
330         struct ieee80211_node *ni;
331
332         ASSERT_SERIALIZED(ifp->if_serializer);
333
334         if (state == IEEE80211_S_INIT) {
335                 callout_stop(&asc->timer);
336                 return;
337         }
338         if (ic->ic_opmode == IEEE80211_M_STA) {
339                 /*
340                  * Reset local xmit state; this is really only
341                  * meaningful when operating in station mode.
342                  */
343                 ni = ic->ic_bss;
344                 if (state == IEEE80211_S_RUN) {
345                         ath_rate_ctl_start(sc, ni);
346                 } else {
347                         ath_rate_update(sc, ni, 0);
348                 }
349         } else {
350                 /*
351                  * When operating as a station the node table holds
352                  * the AP's that were discovered during scanning.
353                  * For any other operating mode we want to reset the
354                  * tx rate state of each node.
355                  */
356                 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
357                 ath_rate_update(sc, ic->ic_bss, 0);
358         }
359         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
360             state == IEEE80211_S_RUN) {
361                 int interval;
362                 /*
363                  * Start the background rate control thread if we
364                  * are not configured to use a fixed xmit rate.
365                  */
366                 interval = ath_rateinterval;
367                 if (ic->ic_opmode == IEEE80211_M_STA)
368                         interval /= 2;
369                 callout_reset(&asc->timer, (interval * hz) / 1000,
370                               ath_ratectl_callout, ifp);
371         }
372 }
373
374 /* 
375  * Examine and potentially adjust the transmit rate.
376  */
377 static void
378 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
379 {
380         struct ath_softc *sc = arg;
381         struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
382         int old_rate;
383
384 #define is_success(amn) \
385 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
386 #define is_enough(amn) \
387 (amn->amn_tx_try0_cnt > 10)
388 #define is_failure(amn) \
389 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
390 #define is_max_rate(ni) \
391 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
392 #define is_min_rate(ni) \
393 (ni->ni_txrate == 0)
394
395         old_rate = ni->ni_txrate;
396   
397         DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
398                  amn->amn_tx_try0_cnt,
399                  amn->amn_tx_try1_cnt,
400                  amn->amn_tx_try2_cnt,
401                  amn->amn_tx_try3_cnt,
402                  amn->amn_success_threshold);
403         if (is_success (amn) && is_enough (amn)) {
404                 amn->amn_success++;
405                 if (amn->amn_success == amn->amn_success_threshold &&
406                     !is_max_rate (ni)) {
407                         amn->amn_recovery = 1;
408                         amn->amn_success = 0;
409                         ni->ni_txrate++;
410                         DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
411                 } else {
412                         amn->amn_recovery = 0;
413                 }
414         } else if (is_failure (amn)) {
415                 amn->amn_success = 0;
416                 if (!is_min_rate (ni)) {
417                         if (amn->amn_recovery) {
418                                 /* recovery failure. */
419                                 amn->amn_success_threshold *= 2;
420                                 amn->amn_success_threshold = min (amn->amn_success_threshold,
421                                                                   (u_int)ath_rate_max_success_threshold);
422                                 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
423                         } else {
424                                 /* simple failure. */
425                                 amn->amn_success_threshold = ath_rate_min_success_threshold;
426                                 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
427                         }
428                         amn->amn_recovery = 0;
429                         ni->ni_txrate--;
430                 } else {
431                         amn->amn_recovery = 0;
432                 }
433
434         }
435         if (is_enough (amn) || old_rate != ni->ni_txrate) {
436                 /* reset counters. */
437                 amn->amn_tx_try0_cnt = 0;
438                 amn->amn_tx_try1_cnt = 0;
439                 amn->amn_tx_try2_cnt = 0;
440                 amn->amn_tx_try3_cnt = 0;
441                 amn->amn_tx_failure_cnt = 0;
442         }
443         if (old_rate != ni->ni_txrate) {
444                 ath_rate_update(sc, ni, ni->ni_txrate);
445         }
446 }
447
448 static void
449 ath_ratectl_callout(void *arg)
450 {
451         struct ifnet *ifp = arg;
452         struct ath_softc *sc = ifp->if_softc;
453         struct amrr_softc *asc = (struct amrr_softc *)sc->sc_rc;
454         struct ieee80211com *ic = &sc->sc_ic;
455         int interval;
456
457         wlan_serialize_enter();
458         if (ifp->if_flags & IFF_RUNNING) {
459                 sc->sc_stats.ast_rate_calls++;
460
461                 if (ic->ic_opmode == IEEE80211_M_STA)
462                         ath_rate_ctl(sc, ic->ic_bss);   /* NB: no reference */
463                 else
464                         ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
465         }
466         interval = ath_rateinterval;
467         if (ic->ic_opmode == IEEE80211_M_STA)
468                 interval /= 2;
469         callout_reset(&asc->timer, (interval * hz) / 1000,
470                         ath_ratectl_callout, ifp);
471
472         wlan_serialize_exit();
473 }
474
475 static void
476 ath_rate_sysctlattach(struct ath_softc *sc)
477 {
478         struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
479         struct sysctl_oid *tree = sc->sc_sysctl_tree;
480
481         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
482                 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
483                 "rate control: operation interval (ms)");
484         /* XXX bounds check values */
485         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
486                 "max_sucess_threshold", CTLFLAG_RW,
487                 &ath_rate_max_success_threshold, 0, "");
488         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
489                 "min_sucess_threshold", CTLFLAG_RW,
490                 &ath_rate_min_success_threshold, 0, "");
491 }
492
493 struct ath_ratectrl *
494 ath_rate_attach(struct ath_softc *sc)
495 {
496         struct amrr_softc *asc;
497
498         asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
499         if (asc == NULL)
500                 return NULL;
501         asc->arc.arc_space = sizeof(struct amrr_node);
502         callout_init(&asc->timer);
503         ath_rate_sysctlattach(sc);
504
505         return &asc->arc;
506 }
507
508 void
509 ath_rate_detach(struct ath_ratectrl *arc)
510 {
511         struct amrr_softc *asc = (struct amrr_softc *)arc;
512
513         kfree(asc, M_DEVBUF);
514 }
515
516 void
517 ath_rate_stop(struct ath_ratectrl *arc)
518 {
519         struct amrr_softc *asc = (struct amrr_softc *)arc;
520
521         /* ASSERT_SERIALIZED */
522         callout_stop(&asc->timer);
523 }
524
525 /*
526  * Module glue.
527  */
528 static int
529 amrr_modevent(module_t mod, int type, void *unused)
530 {
531         int error;
532
533         wlan_serialize_enter();
534
535         switch (type) {
536         case MOD_LOAD:
537                 if (bootverbose) {
538                         kprintf("ath_rate: <AMRR rate control "
539                                 "algorithm> version 0.1\n");
540                 }
541                 error = 0;
542                 break;
543         case MOD_UNLOAD:
544                 error = 0;
545                 break;
546         default:
547                 error = EINVAL;
548                 break;
549         }
550         wlan_serialize_exit();
551         return error;
552 }
553
554 static moduledata_t amrr_mod = {
555         "ath_rate",
556         amrr_modevent,
557         0
558 };
559 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
560 MODULE_VERSION(ath_rate, 1);
561 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);