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