8022cabb24c1faa92b8e2dd56c11ae786f920320
[dragonfly.git] / sys / dev / netif / ath / ath_rate / onoe / onoe.c
1 /*-
2  * Copyright (c) 2002-2007 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  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29
30 #include <sys/cdefs.h>
31
32 /*
33  * Atsushi Onoe's rate control algorithm.
34  */
35 #include "opt_ath.h"
36 #include "opt_inet.h"
37 #include "opt_wlan.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h> 
41 #include <sys/sysctl.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/errno.h>
46
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/bus.h>
50
51 #include <sys/socket.h>
52  
53 #include <net/if.h>
54 #include <net/if_media.h>
55 #include <net/if_arp.h>
56 #include <net/ethernet.h>               /* XXX for ether_sprintf */
57
58 #include <net80211/ieee80211_var.h>
59
60 #include <net/bpf.h>
61
62 #ifdef INET
63 #include <netinet/in.h> 
64 #include <netinet/if_ether.h>
65 #endif
66
67 #include <dev/ath/if_athvar.h>
68 #include <dev/ath/ath_rate/onoe/onoe.h>
69 #include <dev/ath/ath_hal/ah_desc.h>
70
71 /*
72  * Default parameters for the rate control algorithm.  These are
73  * all tunable with sysctls.  The rate controller runs periodically
74  * (each ath_rateinterval ms) analyzing transmit statistics for each
75  * neighbor/station (when operating in station mode this is only the AP).
76  * If transmits look to be working well over a sampling period then
77  * it gives a "raise rate credit".  If transmits look to not be working
78  * well than it deducts a credit.  If the credits cross a threshold then
79  * the transmit rate is raised.  Various error conditions force the
80  * the transmit rate to be dropped.
81  *
82  * The decision to issue/deduct a credit is based on the errors and
83  * retries accumulated over the sampling period.  ath_rate_raise defines
84  * the percent of retransmits for which a credit is issued/deducted.
85  * ath_rate_raise_threshold defines the threshold on credits at which
86  * the transmit rate is increased.
87  *
88  * XXX this algorithm is flawed.
89  */
90 static  int ath_rateinterval = 1000;            /* rate ctl interval (ms)  */
91 static  int ath_rate_raise = 10;                /* add credit threshold */
92 static  int ath_rate_raise_threshold = 10;      /* rate ctl raise threshold */
93
94 static void     ath_rate_update(struct ath_softc *, struct ieee80211_node *,
95                         int rate);
96 static void     ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
97 static void     ath_rate_ctl(void *, struct ieee80211_node *);
98
99 void
100 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
101 {
102         /* NB: assumed to be zero'd by caller */
103 }
104
105 void
106 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
107 {
108 }
109
110 void
111 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
112         int shortPreamble, size_t frameLen,
113         u_int8_t *rix, int *try0, u_int8_t *txrate)
114 {
115         struct onoe_node *on = ATH_NODE_ONOE(an);
116
117         *rix = on->on_tx_rix0;
118         *try0 = on->on_tx_try0;
119         if (shortPreamble)
120                 *txrate = on->on_tx_rate0sp;
121         else
122                 *txrate = on->on_tx_rate0;
123 }
124
125 /*
126  * Get the TX rates.
127  *
128  * The short preamble bits aren't set here; the caller should augment
129  * the returned rate with the relevant preamble rate flag.
130  */
131 void
132 ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
133     uint8_t rix0, struct ath_rc_series *rc)
134 {
135         struct onoe_node *on = ATH_NODE_ONOE(an);
136
137         rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
138
139         rc[0].rix = on->on_tx_rate0;
140         rc[1].rix = on->on_tx_rate1;
141         rc[2].rix = on->on_tx_rate2;
142         rc[3].rix = on->on_tx_rate3;
143
144         rc[0].tries = on->on_tx_try0;
145         rc[1].tries = 2;
146         rc[2].tries = 2;
147         rc[3].tries = 2;
148 }
149
150 void
151 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
152         struct ath_desc *ds, int shortPreamble, u_int8_t rix)
153 {
154         struct onoe_node *on = ATH_NODE_ONOE(an);
155
156         ath_hal_setupxtxdesc(sc->sc_ah, ds
157                 , on->on_tx_rate1sp, 2  /* series 1 */
158                 , on->on_tx_rate2sp, 2  /* series 2 */
159                 , on->on_tx_rate3sp, 2  /* series 3 */
160         );
161 }
162
163 void
164 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
165         const struct ath_rc_series *rc, const struct ath_tx_status *ts,
166         int frame_size, int nframes, int nbad)
167 {
168         struct onoe_node *on = ATH_NODE_ONOE(an);
169
170         if (ts->ts_status == 0)
171                 on->on_tx_ok++;
172         else
173                 on->on_tx_err++;
174         on->on_tx_retr += ts->ts_shortretry
175                         + ts->ts_longretry;
176         if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
177                 ath_rate_ctl(sc, &an->an_node);
178                 on->on_ticks = ticks;
179         }
180 }
181
182 void
183 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
184 {
185         if (isnew)
186                 ath_rate_ctl_start(sc, &an->an_node);
187 }
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 onoe_node *on = ATH_NODE_ONOE(an);
194         struct ieee80211vap *vap = ni->ni_vap;
195         const HAL_RATE_TABLE *rt = sc->sc_currates;
196         u_int8_t rix;
197
198         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
199
200         IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
201              "%s: set xmit rate to %dM", __func__,
202              ni->ni_rates.rs_nrates > 0 ?
203                 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
204
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                 goto done;
214         on->on_rix = rate;
215         ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
216         on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
217         on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
218         
219         on->on_tx_rate0sp = on->on_tx_rate0 |
220                 rt->info[on->on_tx_rix0].shortPreamble;
221         if (sc->sc_mrretry) {
222                 /*
223                  * Hardware supports multi-rate retry; setup two
224                  * step-down retry rates and make the lowest rate
225                  * be the ``last chance''.  We use 4, 2, 2, 2 tries
226                  * respectively (4 is set here, the rest are fixed
227                  * in the xmit routine).
228                  */
229                 on->on_tx_try0 = 1 + 3;         /* 4 tries at rate 0 */
230                 if (--rate >= 0) {
231                         rix = sc->sc_rixmap[
232                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
233                         on->on_tx_rate1 = rt->info[rix].rateCode;
234                         on->on_tx_rate1sp = on->on_tx_rate1 |
235                                 rt->info[rix].shortPreamble;
236                 } else {
237                         on->on_tx_rate1 = on->on_tx_rate1sp = 0;
238                 }
239                 if (--rate >= 0) {
240                         rix = sc->sc_rixmap[
241                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
242                         on->on_tx_rate2 = rt->info[rix].rateCode;
243                         on->on_tx_rate2sp = on->on_tx_rate2 |
244                                 rt->info[rix].shortPreamble;
245                 } else {
246                         on->on_tx_rate2 = on->on_tx_rate2sp = 0;
247                 }
248                 if (rate > 0) {
249                         /* NB: only do this if we didn't already do it above */
250                         on->on_tx_rate3 = rt->info[0].rateCode;
251                         on->on_tx_rate3sp =
252                                 on->on_tx_rate3 | rt->info[0].shortPreamble;
253                 } else {
254                         on->on_tx_rate3 = on->on_tx_rate3sp = 0;
255                 }
256         } else {
257                 on->on_tx_try0 = ATH_TXMAXTRY;  /* max tries at rate 0 */
258                 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
259                 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
260                 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
261         }
262 done:
263         on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
264
265         on->on_interval = ath_rateinterval;
266         if (vap->iv_opmode == IEEE80211_M_STA)
267                 on->on_interval /= 2;
268         on->on_interval = (on->on_interval * hz) / 1000;
269 }
270
271 /*
272  * Set the starting transmit rate for a node.
273  */
274 static void
275 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
276 {
277 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
278         const struct ieee80211_txparam *tp = ni->ni_txparms;
279         int srate;
280
281         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
282         if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
283                 /*
284                  * No fixed rate is requested. For 11b start with
285                  * the highest negotiated rate; otherwise, for 11g
286                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
287                  */
288                 srate = ni->ni_rates.rs_nrates - 1;
289                 if (sc->sc_curmode != IEEE80211_MODE_11B) {
290                         /*
291                          * Scan the negotiated rate set to find the
292                          * closest rate.
293                          */
294                         /* NB: the rate set is assumed sorted */
295                         for (; srate >= 0 && RATE(srate) > 72; srate--)
296                                 ;
297                 }
298         } else {
299                 /*
300                  * A fixed rate is to be used; ic_fixed_rate is the
301                  * IEEE code for this rate (sans basic bit).  Convert this
302                  * to the index into the negotiated rate set for
303                  * the node.  We know the rate is there because the
304                  * rate set is checked when the station associates.
305                  */
306                 /* NB: the rate set is assumed sorted */
307                 srate = ni->ni_rates.rs_nrates - 1;
308                 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
309                         ;
310         }
311         /*
312          * The selected rate may not be available due to races
313          * and mode settings.  Also orphaned nodes created in
314          * adhoc mode may not have any rate set so this lookup
315          * can fail.  This is not fatal.
316          */
317         ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
318 #undef RATE
319 }
320
321 /* 
322  * Examine and potentially adjust the transmit rate.
323  */
324 static void
325 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
326 {
327         struct ath_softc *sc = arg;
328         struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
329         struct ieee80211_rateset *rs = &ni->ni_rates;
330         int dir = 0, nrate, enough;
331
332         /*
333          * Rate control
334          * XXX: very primitive version.
335          */
336         enough = (on->on_tx_ok + on->on_tx_err >= 10);
337
338         /* no packet reached -> down */
339         if (on->on_tx_err > 0 && on->on_tx_ok == 0)
340                 dir = -1;
341
342         /* all packets needs retry in average -> down */
343         if (enough && on->on_tx_ok < on->on_tx_retr)
344                 dir = -1;
345
346         /* no error and less than rate_raise% of packets need retry -> up */
347         if (enough && on->on_tx_err == 0 &&
348             on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
349                 dir = 1;
350
351         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
352             "ok %d err %d retr %d upper %d dir %d",
353             on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
354
355         nrate = on->on_rix;
356         switch (dir) {
357         case 0:
358                 if (enough && on->on_tx_upper > 0)
359                         on->on_tx_upper--;
360                 break;
361         case -1:
362                 if (nrate > 0) {
363                         nrate--;
364                         sc->sc_stats.ast_rate_drop++;
365                 }
366                 on->on_tx_upper = 0;
367                 break;
368         case 1:
369                 /* raise rate if we hit rate_raise_threshold */
370                 if (++on->on_tx_upper < ath_rate_raise_threshold)
371                         break;
372                 on->on_tx_upper = 0;
373                 if (nrate + 1 < rs->rs_nrates) {
374                         nrate++;
375                         sc->sc_stats.ast_rate_raise++;
376                 }
377                 break;
378         }
379
380         if (nrate != on->on_rix) {
381                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
382                     "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
383                     ni->ni_txrate / 2,
384                     (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
385                     on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
386                 ath_rate_update(sc, ni, nrate);
387         } else if (enough)
388                 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
389 }
390
391 static void
392 ath_rate_sysctlattach(struct ath_softc *sc)
393 {
394         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
395         struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
396
397         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
398                 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
399                 "rate control: operation interval (ms)");
400         /* XXX bounds check values */
401         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
402                 "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
403                 "rate control: retry threshold to credit rate raise (%%)");
404         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
405                 "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
406                 "rate control: # good periods before raising rate");
407 }
408
409 static int
410 ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
411     struct ath_rateioctl *re)
412 {
413
414         return (EINVAL);
415 }
416
417 struct ath_ratectrl *
418 ath_rate_attach(struct ath_softc *sc)
419 {
420         struct onoe_softc *osc;
421
422         osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
423         if (osc == NULL)
424                 return NULL;
425         osc->arc.arc_space = sizeof(struct onoe_node);
426         ath_rate_sysctlattach(sc);
427
428         return &osc->arc;
429 }
430
431 void
432 ath_rate_detach(struct ath_ratectrl *arc)
433 {
434         struct onoe_softc *osc = (struct onoe_softc *) arc;
435
436         free(osc, M_DEVBUF);
437 }