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