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