Deleted Added
full compact
amrr.c (186804) amrr.c (218160)
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 */
38
39#include <sys/cdefs.h>
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 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/amrr/amrr.c 186804 2009-01-06 01:36:36Z sam $");
40__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/amrr/amrr.c 218160 2011-02-01 08:10:18Z adrian $");
41
42/*
43 * AMRR rate control. See:
44 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
45 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
46 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
47 */
48#include "opt_inet.h"
49#include "opt_wlan.h"
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/sysctl.h>
54#include <sys/kernel.h>
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#include <sys/errno.h>
58
59#include <machine/bus.h>
60#include <machine/resource.h>
61#include <sys/bus.h>
62
63#include <sys/socket.h>
64
65#include <net/if.h>
66#include <net/if_media.h>
67#include <net/if_arp.h>
68
69#include <net80211/ieee80211_var.h>
70
71#include <net/bpf.h>
72
73#ifdef INET
74#include <netinet/in.h>
75#include <netinet/if_ether.h>
76#endif
77
78#include <dev/ath/if_athvar.h>
79#include <dev/ath/ath_rate/amrr/amrr.h>
80#include <dev/ath/ath_hal/ah_desc.h>
81
82static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
83static int ath_rate_max_success_threshold = 10;
84static int ath_rate_min_success_threshold = 1;
85
86static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
87 int rate);
88static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
89static void ath_rate_ctl(void *, struct ieee80211_node *);
90
91void
92ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
93{
94 /* NB: assumed to be zero'd by caller */
95}
96
97void
98ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
99{
100}
101
102void
103ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
104 int shortPreamble, size_t frameLen,
105 u_int8_t *rix, int *try0, u_int8_t *txrate)
106{
107 struct amrr_node *amn = ATH_NODE_AMRR(an);
108
109 *rix = amn->amn_tx_rix0;
110 *try0 = amn->amn_tx_try0;
111 if (shortPreamble)
112 *txrate = amn->amn_tx_rate0sp;
113 else
114 *txrate = amn->amn_tx_rate0;
115}
116
41
42/*
43 * AMRR rate control. See:
44 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
45 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
46 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
47 */
48#include "opt_inet.h"
49#include "opt_wlan.h"
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/sysctl.h>
54#include <sys/kernel.h>
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#include <sys/errno.h>
58
59#include <machine/bus.h>
60#include <machine/resource.h>
61#include <sys/bus.h>
62
63#include <sys/socket.h>
64
65#include <net/if.h>
66#include <net/if_media.h>
67#include <net/if_arp.h>
68
69#include <net80211/ieee80211_var.h>
70
71#include <net/bpf.h>
72
73#ifdef INET
74#include <netinet/in.h>
75#include <netinet/if_ether.h>
76#endif
77
78#include <dev/ath/if_athvar.h>
79#include <dev/ath/ath_rate/amrr/amrr.h>
80#include <dev/ath/ath_hal/ah_desc.h>
81
82static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
83static int ath_rate_max_success_threshold = 10;
84static int ath_rate_min_success_threshold = 1;
85
86static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
87 int rate);
88static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
89static void ath_rate_ctl(void *, struct ieee80211_node *);
90
91void
92ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
93{
94 /* NB: assumed to be zero'd by caller */
95}
96
97void
98ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
99{
100}
101
102void
103ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
104 int shortPreamble, size_t frameLen,
105 u_int8_t *rix, int *try0, u_int8_t *txrate)
106{
107 struct amrr_node *amn = ATH_NODE_AMRR(an);
108
109 *rix = amn->amn_tx_rix0;
110 *try0 = amn->amn_tx_try0;
111 if (shortPreamble)
112 *txrate = amn->amn_tx_rate0sp;
113 else
114 *txrate = amn->amn_tx_rate0;
115}
116
117/*
118 * Get the TX rates.
119 *
120 * The short preamble bits aren't set here; the caller should augment
121 * the returned rate with the relevant preamble rate flag.
122 */
117void
123void
124ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
125 uint8_t rix0, uint8_t *rix, uint8_t *try)
126{
127 struct amrr_node *amn = ATH_NODE_AMRR(an);
128
129/* rix[0] = amn->amn_tx_rate0; */
130 rix[1] = amn->amn_tx_rate1;
131 rix[2] = amn->amn_tx_rate2;
132 rix[3] = amn->amn_tx_rate3;
133
134 try[0] = amn->amn_tx_try0;
135 try[1] = amn->amn_tx_try1;
136 try[2] = amn->amn_tx_try2;
137 try[3] = amn->amn_tx_try3;
138}
139
140
141void
118ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
119 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
120{
121 struct amrr_node *amn = ATH_NODE_AMRR(an);
122
123 ath_hal_setupxtxdesc(sc->sc_ah, ds
124 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
125 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
126 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
127 );
128}
129
130void
131ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
132 const struct ath_buf *bf)
133{
134 struct amrr_node *amn = ATH_NODE_AMRR(an);
135 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
136 int sr = ts->ts_shortretry;
137 int lr = ts->ts_longretry;
138 int retry_count = sr + lr;
139
140 amn->amn_tx_try0_cnt++;
141 if (retry_count == 1) {
142 amn->amn_tx_try1_cnt++;
143 } else if (retry_count == 2) {
144 amn->amn_tx_try1_cnt++;
145 amn->amn_tx_try2_cnt++;
146 } else if (retry_count == 3) {
147 amn->amn_tx_try1_cnt++;
148 amn->amn_tx_try2_cnt++;
149 amn->amn_tx_try3_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 amn->amn_tx_failure_cnt++;
155 }
156 if (amn->amn_interval != 0 &&
157 ticks - amn->amn_ticks > amn->amn_interval) {
158 ath_rate_ctl(sc, &an->an_node);
159 amn->amn_ticks = ticks;
160 }
161}
162
163void
164ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
165{
166 if (isnew)
167 ath_rate_ctl_start(sc, &an->an_node);
168}
169
170static void
171node_reset(struct amrr_node *amn)
172{
173 amn->amn_tx_try0_cnt = 0;
174 amn->amn_tx_try1_cnt = 0;
175 amn->amn_tx_try2_cnt = 0;
176 amn->amn_tx_try3_cnt = 0;
177 amn->amn_tx_failure_cnt = 0;
178 amn->amn_success = 0;
179 amn->amn_recovery = 0;
180 amn->amn_success_threshold = ath_rate_min_success_threshold;
181}
182
183
184/**
185 * The code below assumes that we are dealing with hardware multi rate retry
186 * I have no idea what will happen if you try to use this module with another
187 * type of hardware. Your machine might catch fire or it might work with
188 * horrible performance...
189 */
190static void
191ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
192{
193 struct ath_node *an = ATH_NODE(ni);
194 struct amrr_node *amn = ATH_NODE_AMRR(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 amn->amn_rix = rate;
207 /*
208 * Before associating a node has no rate set setup
209 * so we can't calculate any transmit codes to use.
210 * This is ok since we should never be sending anything
211 * but management frames and those always go at the
212 * lowest hardware rate.
213 */
214 if (ni->ni_rates.rs_nrates > 0) {
215 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
216 amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
217 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
218 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
219 rt->info[amn->amn_tx_rix0].shortPreamble;
220 if (sc->sc_mrretry) {
221 amn->amn_tx_try0 = 1;
222 amn->amn_tx_try1 = 1;
223 amn->amn_tx_try2 = 1;
224 amn->amn_tx_try3 = 1;
225 if (--rate >= 0) {
226 rix = sc->sc_rixmap[
227 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
228 amn->amn_tx_rate1 = rt->info[rix].rateCode;
229 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
230 rt->info[rix].shortPreamble;
231 } else {
232 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
233 }
234 if (--rate >= 0) {
235 rix = sc->sc_rixmap[
236 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
237 amn->amn_tx_rate2 = rt->info[rix].rateCode;
238 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
239 rt->info[rix].shortPreamble;
240 } else {
241 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
242 }
243 if (rate > 0) {
244 /* NB: only do this if we didn't already do it above */
245 amn->amn_tx_rate3 = rt->info[0].rateCode;
246 amn->amn_tx_rate3sp =
247 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
248 } else {
249 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
250 }
251 } else {
252 amn->amn_tx_try0 = ATH_TXMAXTRY;
253 /* theorically, these statements are useless because
254 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
255 */
256 amn->amn_tx_try1 = 0;
257 amn->amn_tx_try2 = 0;
258 amn->amn_tx_try3 = 0;
259 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
260 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
261 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
262 }
263 }
264 node_reset(amn);
265
266 amn->amn_interval = ath_rateinterval;
267 if (vap->iv_opmode == IEEE80211_M_STA)
268 amn->amn_interval /= 2;
269 amn->amn_interval = (amn->amn_interval * hz) / 1000;
270}
271
272/*
273 * Set the starting transmit rate for a node.
274 */
275static void
276ath_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 */
325static void
326ath_rate_ctl(void *arg, struct ieee80211_node *ni)
327{
328 struct ath_softc *sc = arg;
329 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
330 int rix;
331
332#define is_success(amn) \
333(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
334#define is_enough(amn) \
335(amn->amn_tx_try0_cnt > 10)
336#define is_failure(amn) \
337(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
338
339 rix = amn->amn_rix;
340
341 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
342 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
343 amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
344 amn->amn_tx_try3_cnt, amn->amn_success_threshold);
345 if (is_success (amn) && is_enough (amn)) {
346 amn->amn_success++;
347 if (amn->amn_success == amn->amn_success_threshold &&
348 rix + 1 < ni->ni_rates.rs_nrates) {
349 amn->amn_recovery = 1;
350 amn->amn_success = 0;
351 rix++;
352 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
353 "increase rate to %d", rix);
354 } else {
355 amn->amn_recovery = 0;
356 }
357 } else if (is_failure (amn)) {
358 amn->amn_success = 0;
359 if (rix > 0) {
360 if (amn->amn_recovery) {
361 /* recovery failure. */
362 amn->amn_success_threshold *= 2;
363 amn->amn_success_threshold = min (amn->amn_success_threshold,
364 (u_int)ath_rate_max_success_threshold);
365 IEEE80211_NOTE(ni->ni_vap,
366 IEEE80211_MSG_RATECTL, ni,
367 "decrease rate recovery thr: %d",
368 amn->amn_success_threshold);
369 } else {
370 /* simple failure. */
371 amn->amn_success_threshold = ath_rate_min_success_threshold;
372 IEEE80211_NOTE(ni->ni_vap,
373 IEEE80211_MSG_RATECTL, ni,
374 "decrease rate normal thr: %d",
375 amn->amn_success_threshold);
376 }
377 amn->amn_recovery = 0;
378 rix--;
379 } else {
380 amn->amn_recovery = 0;
381 }
382
383 }
384 if (is_enough (amn) || rix != amn->amn_rix) {
385 /* reset counters. */
386 amn->amn_tx_try0_cnt = 0;
387 amn->amn_tx_try1_cnt = 0;
388 amn->amn_tx_try2_cnt = 0;
389 amn->amn_tx_try3_cnt = 0;
390 amn->amn_tx_failure_cnt = 0;
391 }
392 if (rix != amn->amn_rix) {
393 ath_rate_update(sc, ni, rix);
394 }
395}
396
397static void
398ath_rate_sysctlattach(struct ath_softc *sc)
399{
400 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
401 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
402
403 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
404 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
405 "rate control: operation interval (ms)");
406 /* XXX bounds check values */
407 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
408 "max_sucess_threshold", CTLFLAG_RW,
409 &ath_rate_max_success_threshold, 0, "");
410 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
411 "min_sucess_threshold", CTLFLAG_RW,
412 &ath_rate_min_success_threshold, 0, "");
413}
414
415struct ath_ratectrl *
416ath_rate_attach(struct ath_softc *sc)
417{
418 struct amrr_softc *asc;
419
420 asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
421 if (asc == NULL)
422 return NULL;
423 asc->arc.arc_space = sizeof(struct amrr_node);
424 ath_rate_sysctlattach(sc);
425
426 return &asc->arc;
427}
428
429void
430ath_rate_detach(struct ath_ratectrl *arc)
431{
432 struct amrr_softc *asc = (struct amrr_softc *) arc;
433
434 free(asc, M_DEVBUF);
435}
142ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
143 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
144{
145 struct amrr_node *amn = ATH_NODE_AMRR(an);
146
147 ath_hal_setupxtxdesc(sc->sc_ah, ds
148 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
149 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
150 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
151 );
152}
153
154void
155ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
156 const struct ath_buf *bf)
157{
158 struct amrr_node *amn = ATH_NODE_AMRR(an);
159 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
160 int sr = ts->ts_shortretry;
161 int lr = ts->ts_longretry;
162 int retry_count = sr + lr;
163
164 amn->amn_tx_try0_cnt++;
165 if (retry_count == 1) {
166 amn->amn_tx_try1_cnt++;
167 } else if (retry_count == 2) {
168 amn->amn_tx_try1_cnt++;
169 amn->amn_tx_try2_cnt++;
170 } else if (retry_count == 3) {
171 amn->amn_tx_try1_cnt++;
172 amn->amn_tx_try2_cnt++;
173 amn->amn_tx_try3_cnt++;
174 } else if (retry_count > 3) {
175 amn->amn_tx_try1_cnt++;
176 amn->amn_tx_try2_cnt++;
177 amn->amn_tx_try3_cnt++;
178 amn->amn_tx_failure_cnt++;
179 }
180 if (amn->amn_interval != 0 &&
181 ticks - amn->amn_ticks > amn->amn_interval) {
182 ath_rate_ctl(sc, &an->an_node);
183 amn->amn_ticks = ticks;
184 }
185}
186
187void
188ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
189{
190 if (isnew)
191 ath_rate_ctl_start(sc, &an->an_node);
192}
193
194static void
195node_reset(struct amrr_node *amn)
196{
197 amn->amn_tx_try0_cnt = 0;
198 amn->amn_tx_try1_cnt = 0;
199 amn->amn_tx_try2_cnt = 0;
200 amn->amn_tx_try3_cnt = 0;
201 amn->amn_tx_failure_cnt = 0;
202 amn->amn_success = 0;
203 amn->amn_recovery = 0;
204 amn->amn_success_threshold = ath_rate_min_success_threshold;
205}
206
207
208/**
209 * The code below assumes that we are dealing with hardware multi rate retry
210 * I have no idea what will happen if you try to use this module with another
211 * type of hardware. Your machine might catch fire or it might work with
212 * horrible performance...
213 */
214static void
215ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
216{
217 struct ath_node *an = ATH_NODE(ni);
218 struct amrr_node *amn = ATH_NODE_AMRR(an);
219 struct ieee80211vap *vap = ni->ni_vap;
220 const HAL_RATE_TABLE *rt = sc->sc_currates;
221 u_int8_t rix;
222
223 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
224
225 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
226 "%s: set xmit rate to %dM", __func__,
227 ni->ni_rates.rs_nrates > 0 ?
228 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
229
230 amn->amn_rix = rate;
231 /*
232 * Before associating a node has no rate set setup
233 * so we can't calculate any transmit codes to use.
234 * This is ok since we should never be sending anything
235 * but management frames and those always go at the
236 * lowest hardware rate.
237 */
238 if (ni->ni_rates.rs_nrates > 0) {
239 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
240 amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
241 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
242 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
243 rt->info[amn->amn_tx_rix0].shortPreamble;
244 if (sc->sc_mrretry) {
245 amn->amn_tx_try0 = 1;
246 amn->amn_tx_try1 = 1;
247 amn->amn_tx_try2 = 1;
248 amn->amn_tx_try3 = 1;
249 if (--rate >= 0) {
250 rix = sc->sc_rixmap[
251 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
252 amn->amn_tx_rate1 = rt->info[rix].rateCode;
253 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
254 rt->info[rix].shortPreamble;
255 } else {
256 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
257 }
258 if (--rate >= 0) {
259 rix = sc->sc_rixmap[
260 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
261 amn->amn_tx_rate2 = rt->info[rix].rateCode;
262 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
263 rt->info[rix].shortPreamble;
264 } else {
265 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
266 }
267 if (rate > 0) {
268 /* NB: only do this if we didn't already do it above */
269 amn->amn_tx_rate3 = rt->info[0].rateCode;
270 amn->amn_tx_rate3sp =
271 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
272 } else {
273 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
274 }
275 } else {
276 amn->amn_tx_try0 = ATH_TXMAXTRY;
277 /* theorically, these statements are useless because
278 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
279 */
280 amn->amn_tx_try1 = 0;
281 amn->amn_tx_try2 = 0;
282 amn->amn_tx_try3 = 0;
283 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
284 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
285 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
286 }
287 }
288 node_reset(amn);
289
290 amn->amn_interval = ath_rateinterval;
291 if (vap->iv_opmode == IEEE80211_M_STA)
292 amn->amn_interval /= 2;
293 amn->amn_interval = (amn->amn_interval * hz) / 1000;
294}
295
296/*
297 * Set the starting transmit rate for a node.
298 */
299static void
300ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
301{
302#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
303 const struct ieee80211_txparam *tp = ni->ni_txparms;
304 int srate;
305
306 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
307 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
308 /*
309 * No fixed rate is requested. For 11b start with
310 * the highest negotiated rate; otherwise, for 11g
311 * and 11a, we start "in the middle" at 24Mb or 36Mb.
312 */
313 srate = ni->ni_rates.rs_nrates - 1;
314 if (sc->sc_curmode != IEEE80211_MODE_11B) {
315 /*
316 * Scan the negotiated rate set to find the
317 * closest rate.
318 */
319 /* NB: the rate set is assumed sorted */
320 for (; srate >= 0 && RATE(srate) > 72; srate--)
321 ;
322 }
323 } else {
324 /*
325 * A fixed rate is to be used; ic_fixed_rate is the
326 * IEEE code for this rate (sans basic bit). Convert this
327 * to the index into the negotiated rate set for
328 * the node. We know the rate is there because the
329 * rate set is checked when the station associates.
330 */
331 /* NB: the rate set is assumed sorted */
332 srate = ni->ni_rates.rs_nrates - 1;
333 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
334 ;
335 }
336 /*
337 * The selected rate may not be available due to races
338 * and mode settings. Also orphaned nodes created in
339 * adhoc mode may not have any rate set so this lookup
340 * can fail. This is not fatal.
341 */
342 ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
343#undef RATE
344}
345
346/*
347 * Examine and potentially adjust the transmit rate.
348 */
349static void
350ath_rate_ctl(void *arg, struct ieee80211_node *ni)
351{
352 struct ath_softc *sc = arg;
353 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
354 int rix;
355
356#define is_success(amn) \
357(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
358#define is_enough(amn) \
359(amn->amn_tx_try0_cnt > 10)
360#define is_failure(amn) \
361(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
362
363 rix = amn->amn_rix;
364
365 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
366 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
367 amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
368 amn->amn_tx_try3_cnt, amn->amn_success_threshold);
369 if (is_success (amn) && is_enough (amn)) {
370 amn->amn_success++;
371 if (amn->amn_success == amn->amn_success_threshold &&
372 rix + 1 < ni->ni_rates.rs_nrates) {
373 amn->amn_recovery = 1;
374 amn->amn_success = 0;
375 rix++;
376 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
377 "increase rate to %d", rix);
378 } else {
379 amn->amn_recovery = 0;
380 }
381 } else if (is_failure (amn)) {
382 amn->amn_success = 0;
383 if (rix > 0) {
384 if (amn->amn_recovery) {
385 /* recovery failure. */
386 amn->amn_success_threshold *= 2;
387 amn->amn_success_threshold = min (amn->amn_success_threshold,
388 (u_int)ath_rate_max_success_threshold);
389 IEEE80211_NOTE(ni->ni_vap,
390 IEEE80211_MSG_RATECTL, ni,
391 "decrease rate recovery thr: %d",
392 amn->amn_success_threshold);
393 } else {
394 /* simple failure. */
395 amn->amn_success_threshold = ath_rate_min_success_threshold;
396 IEEE80211_NOTE(ni->ni_vap,
397 IEEE80211_MSG_RATECTL, ni,
398 "decrease rate normal thr: %d",
399 amn->amn_success_threshold);
400 }
401 amn->amn_recovery = 0;
402 rix--;
403 } else {
404 amn->amn_recovery = 0;
405 }
406
407 }
408 if (is_enough (amn) || rix != amn->amn_rix) {
409 /* reset counters. */
410 amn->amn_tx_try0_cnt = 0;
411 amn->amn_tx_try1_cnt = 0;
412 amn->amn_tx_try2_cnt = 0;
413 amn->amn_tx_try3_cnt = 0;
414 amn->amn_tx_failure_cnt = 0;
415 }
416 if (rix != amn->amn_rix) {
417 ath_rate_update(sc, ni, rix);
418 }
419}
420
421static void
422ath_rate_sysctlattach(struct ath_softc *sc)
423{
424 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
425 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
426
427 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
428 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
429 "rate control: operation interval (ms)");
430 /* XXX bounds check values */
431 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
432 "max_sucess_threshold", CTLFLAG_RW,
433 &ath_rate_max_success_threshold, 0, "");
434 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
435 "min_sucess_threshold", CTLFLAG_RW,
436 &ath_rate_min_success_threshold, 0, "");
437}
438
439struct ath_ratectrl *
440ath_rate_attach(struct ath_softc *sc)
441{
442 struct amrr_softc *asc;
443
444 asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
445 if (asc == NULL)
446 return NULL;
447 asc->arc.arc_space = sizeof(struct amrr_node);
448 ath_rate_sysctlattach(sc);
449
450 return &asc->arc;
451}
452
453void
454ath_rate_detach(struct ath_ratectrl *arc)
455{
456 struct amrr_softc *asc = (struct amrr_softc *) arc;
457
458 free(asc, M_DEVBUF);
459}