Do a major clean-up of the BUSDMA architecture. A large number of
[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.8.2.3 2006/02/24 19:51:11 sam Exp $
38  * $DragonFly: src/sys/dev/netif/ath/rate_amrr/amrr.c,v 1.3 2006/10/25 20:55:55 dillon Exp $
39  */
40
41 /*
42  * AMRR rate control. See:
43  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
44  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
45  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h> 
50 #include <sys/sysctl.h>
51 #include <sys/module.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/errno.h>
55 #include <sys/serialize.h>
56 #include <sys/bus.h>
57
58 #include <sys/socket.h>
59  
60 #include <net/if.h>
61 #include <net/if_media.h>
62 #include <net/if_arp.h>
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_amrr/amrr.h>
70 #include <contrib/dev/ath/ah_desc.h>
71
72 #define AMRR_DEBUG
73 #ifdef AMRR_DEBUG
74 #define DPRINTF(sc, _fmt, ...) do {                                     \
75         if (sc->sc_debug & 0x10)                                        \
76                 printf(_fmt, __VA_ARGS__);                              \
77 } while (0)
78 #else
79 #define DPRINTF(sc, _fmt, ...)
80 #endif
81
82 static  int ath_rateinterval = 1000;            /* rate ctl interval (ms)  */
83 static  int ath_rate_max_success_threshold = 10;
84 static  int ath_rate_min_success_threshold = 1;
85
86 static void     ath_ratectl(void *);
87 static void     ath_rate_update(struct ath_softc *, struct ieee80211_node *,
88                         int rate);
89 static void     ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
90 static void     ath_rate_ctl(void *, struct ieee80211_node *);
91
92 void
93 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
94 {
95         /* NB: assumed to be zero'd by caller */
96         ath_rate_update(sc, &an->an_node, 0);
97 }
98
99 void
100 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
101 {
102 }
103
104 void
105 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
106         int shortPreamble, size_t frameLen,
107         uint8_t *rix, int *try0, uint8_t *txrate)
108 {
109         struct amrr_node *amn = ATH_NODE_AMRR(an);
110
111         *rix = amn->amn_tx_rix0;
112         *try0 = amn->amn_tx_try0;
113         if (shortPreamble)
114                 *txrate = amn->amn_tx_rate0sp;
115         else
116                 *txrate = amn->amn_tx_rate0;
117 }
118
119 void
120 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
121         struct ath_desc *ds, int shortPreamble, uint8_t rix)
122 {
123         struct amrr_node *amn = ATH_NODE_AMRR(an);
124
125         ath_hal_setupxtxdesc(sc->sc_ah, ds
126                 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
127                 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
128                 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
129         );
130 }
131
132 void
133 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
134         const struct ath_desc *ds, const struct ath_desc *ds0)
135 {
136         struct amrr_node *amn = ATH_NODE_AMRR(an);
137         int sr = ds->ds_txstat.ts_shortretry;
138         int lr = ds->ds_txstat.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         uint8_t rix;
193
194         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
195
196         DPRINTF(sc, "%s: set xmit rate for %6D to %dM\n",
197             __func__, ni->ni_macaddr, ":",
198             ni->ni_rates.rs_nrates > 0 ?
199                 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
200
201         ni->ni_txrate = rate;
202         /*
203          * Before associating a node has no rate set setup
204          * so we can't calculate any transmit codes to use.
205          * This is ok since we should never be sending anything
206          * but management frames and those always go at the
207          * lowest hardware rate.
208          */
209         if (ni->ni_rates.rs_nrates > 0) {
210                 amn->amn_tx_rix0 = sc->sc_rixmap[
211                                                ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
212                 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
213                 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
214                         rt->info[amn->amn_tx_rix0].shortPreamble;
215                 if (sc->sc_mrretry) {
216                         amn->amn_tx_try0 = 1;
217                         amn->amn_tx_try1 = 1;
218                         amn->amn_tx_try2 = 1;
219                         amn->amn_tx_try3 = 1;
220                         if (--rate >= 0) {
221                                 rix = sc->sc_rixmap[
222                                                     ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
223                                 amn->amn_tx_rate1 = rt->info[rix].rateCode;
224                                 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
225                                         rt->info[rix].shortPreamble;
226                         } else {
227                                 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
228                         }
229                         if (--rate >= 0) {
230                                 rix = sc->sc_rixmap[
231                                                     ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
232                                 amn->amn_tx_rate2 = rt->info[rix].rateCode;
233                                 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
234                                         rt->info[rix].shortPreamble;
235                         } else {
236                                 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
237                         }
238                         if (rate > 0) {
239                                 /* NB: only do this if we didn't already do it above */
240                                 amn->amn_tx_rate3 = rt->info[0].rateCode;
241                                 amn->amn_tx_rate3sp =
242                                         amn->amn_tx_rate3 | rt->info[0].shortPreamble;
243                         } else {
244                                 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
245                         }
246                 } else {
247                         amn->amn_tx_try0 = ATH_TXMAXTRY;
248                         /* theorically, these statements are useless because
249                          *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
250                          */
251                         amn->amn_tx_try1 = 0;
252                         amn->amn_tx_try2 = 0;
253                         amn->amn_tx_try3 = 0;
254                         amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
255                         amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
256                         amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
257                 }
258         }
259         node_reset (amn);
260 }
261
262 /*
263  * Set the starting transmit rate for a node.
264  */
265 static void
266 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
267 {
268 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
269         struct ieee80211com *ic = &sc->sc_ic;
270         int srate;
271
272         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
273         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
274                 /*
275                  * No fixed rate is requested. For 11b start with
276                  * the highest negotiated rate; otherwise, for 11g
277                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
278                  */
279                 srate = ni->ni_rates.rs_nrates - 1;
280                 if (sc->sc_curmode != IEEE80211_MODE_11B) {
281                         /*
282                          * Scan the negotiated rate set to find the
283                          * closest rate.
284                          */
285                         /* NB: the rate set is assumed sorted */
286                         for (; srate >= 0 && RATE(srate) > 72; srate--)
287                                 ;
288                         KASSERT(srate >= 0, ("bogus rate set"));
289                 }
290         } else {
291                 /*
292                  * A fixed rate is to be used; ic_fixed_rate is an
293                  * index into the supported rate set.  Convert this
294                  * to the index into the negotiated rate set for
295                  * the node.  We know the rate is there because the
296                  * rate set is checked when the station associates.
297                  */
298                 const struct ieee80211_rateset *rs =
299                         &ic->ic_sup_rates[ic->ic_curmode];
300                 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
301                 /* NB: the rate set is assumed sorted */
302                 srate = ni->ni_rates.rs_nrates - 1;
303                 for (; srate >= 0 && RATE(srate) != r; srate--)
304                         ;
305                 KASSERT(srate >= 0,
306                         ("fixed rate %d not in rate set", ic->ic_fixed_rate));
307         }
308         ath_rate_update(sc, ni, srate);
309 #undef RATE
310 }
311
312 static void
313 ath_rate_cb(void *arg, struct ieee80211_node *ni)
314 {
315         struct ath_softc *sc = arg;
316
317         ath_rate_update(sc, ni, 0);
318 }
319
320 /*
321  * Reset the rate control state for each 802.11 state transition.
322  */
323 void
324 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
325 {
326         struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
327         struct ieee80211com *ic = &sc->sc_ic;
328         struct ifnet *ifp = &ic->ic_if;
329         struct ieee80211_node *ni;
330
331         ASSERT_SERIALIZED(ifp->if_serializer);
332
333         if (state == IEEE80211_S_INIT) {
334                 callout_stop(&asc->timer);
335                 return;
336         }
337         if (ic->ic_opmode == IEEE80211_M_STA) {
338                 /*
339                  * Reset local xmit state; this is really only
340                  * meaningful when operating in station mode.
341                  */
342                 ni = ic->ic_bss;
343                 if (state == IEEE80211_S_RUN) {
344                         ath_rate_ctl_start(sc, ni);
345                 } else {
346                         ath_rate_update(sc, ni, 0);
347                 }
348         } else {
349                 /*
350                  * When operating as a station the node table holds
351                  * the AP's that were discovered during scanning.
352                  * For any other operating mode we want to reset the
353                  * tx rate state of each node.
354                  */
355                 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
356                 ath_rate_update(sc, ic->ic_bss, 0);
357         }
358         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
359             state == IEEE80211_S_RUN) {
360                 int interval;
361                 /*
362                  * Start the background rate control thread if we
363                  * are not configured to use a fixed xmit rate.
364                  */
365                 interval = ath_rateinterval;
366                 if (ic->ic_opmode == IEEE80211_M_STA)
367                         interval /= 2;
368                 callout_reset(&asc->timer, (interval * hz) / 1000,
369                               ath_ratectl, ifp);
370         }
371 }
372
373 /* 
374  * Examine and potentially adjust the transmit rate.
375  */
376 static void
377 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
378 {
379         struct ath_softc *sc = arg;
380         struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
381         int old_rate;
382
383 #define is_success(amn) \
384 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
385 #define is_enough(amn) \
386 (amn->amn_tx_try0_cnt > 10)
387 #define is_failure(amn) \
388 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
389 #define is_max_rate(ni) \
390 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
391 #define is_min_rate(ni) \
392 (ni->ni_txrate == 0)
393
394         old_rate = ni->ni_txrate;
395   
396         DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
397                  amn->amn_tx_try0_cnt,
398                  amn->amn_tx_try1_cnt,
399                  amn->amn_tx_try2_cnt,
400                  amn->amn_tx_try3_cnt,
401                  amn->amn_success_threshold);
402         if (is_success (amn) && is_enough (amn)) {
403                 amn->amn_success++;
404                 if (amn->amn_success == amn->amn_success_threshold &&
405                     !is_max_rate (ni)) {
406                         amn->amn_recovery = 1;
407                         amn->amn_success = 0;
408                         ni->ni_txrate++;
409                         DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
410                 } else {
411                         amn->amn_recovery = 0;
412                 }
413         } else if (is_failure (amn)) {
414                 amn->amn_success = 0;
415                 if (!is_min_rate (ni)) {
416                         if (amn->amn_recovery) {
417                                 /* recovery failure. */
418                                 amn->amn_success_threshold *= 2;
419                                 amn->amn_success_threshold = min (amn->amn_success_threshold,
420                                                                   (u_int)ath_rate_max_success_threshold);
421                                 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
422                         } else {
423                                 /* simple failure. */
424                                 amn->amn_success_threshold = ath_rate_min_success_threshold;
425                                 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
426                         }
427                         amn->amn_recovery = 0;
428                         ni->ni_txrate--;
429                 } else {
430                         amn->amn_recovery = 0;
431                 }
432
433         }
434         if (is_enough (amn) || old_rate != ni->ni_txrate) {
435                 /* reset counters. */
436                 amn->amn_tx_try0_cnt = 0;
437                 amn->amn_tx_try1_cnt = 0;
438                 amn->amn_tx_try2_cnt = 0;
439                 amn->amn_tx_try3_cnt = 0;
440                 amn->amn_tx_failure_cnt = 0;
441         }
442         if (old_rate != ni->ni_txrate) {
443                 ath_rate_update(sc, ni, ni->ni_txrate);
444         }
445 }
446
447 static void
448 ath_ratectl(void *arg)
449 {
450         struct ifnet *ifp = arg;
451         struct ath_softc *sc = ifp->if_softc;
452         struct amrr_softc *asc = (struct amrr_softc *)sc->sc_rc;
453         struct ieee80211com *ic = &sc->sc_ic;
454         int interval;
455
456         lwkt_serialize_enter(ifp->if_serializer);
457
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, ath_ratectl, ifp);
470
471         lwkt_serialize_exit(ifp->if_serializer);
472 }
473
474 static void
475 ath_rate_sysctlattach(struct ath_softc *sc)
476 {
477         struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
478         struct sysctl_oid *tree = sc->sc_sysctl_tree;
479
480         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
481                 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
482                 "rate control: operation interval (ms)");
483         /* XXX bounds check values */
484         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
485                 "max_sucess_threshold", CTLFLAG_RW,
486                 &ath_rate_max_success_threshold, 0, "");
487         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
488                 "min_sucess_threshold", CTLFLAG_RW,
489                 &ath_rate_min_success_threshold, 0, "");
490 }
491
492 struct ath_ratectrl *
493 ath_rate_attach(struct ath_softc *sc)
494 {
495         struct amrr_softc *asc;
496
497         asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
498         if (asc == NULL)
499                 return NULL;
500         asc->arc.arc_space = sizeof(struct amrr_node);
501         callout_init(&asc->timer);
502         ath_rate_sysctlattach(sc);
503
504         return &asc->arc;
505 }
506
507 void
508 ath_rate_detach(struct ath_ratectrl *arc)
509 {
510         struct amrr_softc *asc = (struct amrr_softc *)arc;
511
512         kfree(asc, M_DEVBUF);
513 }
514
515 void
516 ath_rate_stop(struct ath_ratectrl *arc)
517 {
518         struct amrr_softc *asc = (struct amrr_softc *)arc;
519
520         /* ASSERT_SERIALIZED */
521         callout_stop(&asc->timer);
522 }
523
524 /*
525  * Module glue.
526  */
527 static int
528 amrr_modevent(module_t mod, int type, void *unused)
529 {
530         switch (type) {
531         case MOD_LOAD:
532                 if (bootverbose)
533                         printf("ath_rate: <AMRR rate control algorithm> version 0.1\n");
534                 return 0;
535         case MOD_UNLOAD:
536                 return 0;
537         }
538         return EINVAL;
539 }
540
541 static moduledata_t amrr_mod = {
542         "ath_rate",
543         amrr_modevent,
544         0
545 };
546 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
547 MODULE_VERSION(ath_rate, 1);
548 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);