xref: /netbsd-src/sys/dev/ic/athrate-amrr.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: athrate-amrr.c,v 1.9 2007/10/19 11:59:47 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 2004 INRIA
5  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16  *    redistribution must be conditioned upon including a substantially
17  *    similar Disclaimer requirement for further binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
30  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
31  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
32  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
35  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37  * THE POSSIBILITY OF SUCH DAMAGES.
38  *
39  */
40 
41 #include <sys/cdefs.h>
42 #ifdef __FreeBSD__
43 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
44 #endif
45 #ifdef __NetBSD__
46 __KERNEL_RCSID(0, "$NetBSD: athrate-amrr.c,v 1.9 2007/10/19 11:59:47 ad Exp $");
47 #endif
48 
49 /*
50  * AMRR rate control. See:
51  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
52  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
53  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
54  */
55 #include "opt_inet.h"
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/sysctl.h>
60 #include <sys/kernel.h>
61 #include <sys/lock.h>
62 #include <sys/errno.h>
63 
64 #include <sys/bus.h>
65 
66 #include <sys/socket.h>
67 
68 #include <net/if.h>
69 #include <net/if_media.h>
70 #include <net/if_arp.h>
71 #include <net/if_ether.h>		/* XXX for ether_sprintf */
72 
73 #include <net80211/ieee80211_var.h>
74 
75 #include <net/bpf.h>
76 
77 #ifdef INET
78 #include <netinet/in.h>
79 #endif
80 
81 #include <dev/ic/athvar.h>
82 #include <dev/ic/athrate-amrr.h>
83 #include <contrib/dev/ath/ah_desc.h>
84 
85 #define	AMRR_DEBUG
86 #ifdef AMRR_DEBUG
87 #define	DPRINTF(sc, _fmt, ...) do {					\
88 	if (sc->sc_debug & 0x10)					\
89 		printf(_fmt, __VA_ARGS__);				\
90 } while (0)
91 #else
92 #define	DPRINTF(sc, _fmt, ...)
93 #endif
94 
95 static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
96 static	int ath_rate_max_success_threshold = 10;
97 static	int ath_rate_min_success_threshold = 1;
98 
99 static void	ath_ratectl(void *);
100 static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
101 			int rate);
102 static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
103 static void	ath_rate_ctl(void *, struct ieee80211_node *);
104 
105 void
106 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
107 {
108 	/* NB: assumed to be zero'd by caller */
109 	ath_rate_update(sc, &an->an_node, 0);
110 }
111 
112 void
113 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
114 {
115 }
116 
117 void
118 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
119 	int shortPreamble, size_t frameLen,
120 	u_int8_t *rix, int *try0, u_int8_t *txrate)
121 {
122 	struct amrr_node *amn = ATH_NODE_AMRR(an);
123 
124 	*rix = amn->amn_tx_rix0;
125 	*try0 = amn->amn_tx_try0;
126 	if (shortPreamble)
127 		*txrate = amn->amn_tx_rate0sp;
128 	else
129 		*txrate = amn->amn_tx_rate0;
130 }
131 
132 void
133 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
134 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
135 {
136 	struct amrr_node *amn = ATH_NODE_AMRR(an);
137 
138 	ath_hal_setupxtxdesc(sc->sc_ah, ds
139 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
140 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
141 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
142 	);
143 }
144 
145 void
146 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
147 	const struct ath_desc *ds, const struct ath_desc *ds0)
148 {
149 	struct amrr_node *amn = ATH_NODE_AMRR(an);
150 	int sr = ds->ds_txstat.ts_shortretry;
151 	int lr = ds->ds_txstat.ts_longretry;
152 	int retry_count = sr + lr;
153 
154 	amn->amn_tx_try0_cnt++;
155 	if (retry_count == 1) {
156 		amn->amn_tx_try1_cnt++;
157 	} else if (retry_count == 2) {
158 		amn->amn_tx_try1_cnt++;
159 		amn->amn_tx_try2_cnt++;
160 	} else if (retry_count == 3) {
161 		amn->amn_tx_try1_cnt++;
162 		amn->amn_tx_try2_cnt++;
163 		amn->amn_tx_try3_cnt++;
164 	} else if (retry_count > 3) {
165 		amn->amn_tx_try1_cnt++;
166 		amn->amn_tx_try2_cnt++;
167 		amn->amn_tx_try3_cnt++;
168 		amn->amn_tx_failure_cnt++;
169 	}
170 }
171 
172 void
173 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
174 {
175 	if (isnew)
176 		ath_rate_ctl_start(sc, &an->an_node);
177 }
178 
179 static void
180 node_reset (struct amrr_node *amn)
181 {
182 	amn->amn_tx_try0_cnt = 0;
183 	amn->amn_tx_try1_cnt = 0;
184 	amn->amn_tx_try2_cnt = 0;
185 	amn->amn_tx_try3_cnt = 0;
186 	amn->amn_tx_failure_cnt = 0;
187   	amn->amn_success = 0;
188   	amn->amn_recovery = 0;
189   	amn->amn_success_threshold = ath_rate_min_success_threshold;
190 }
191 
192 
193 /**
194  * The code below assumes that we are dealing with hardware multi rate retry
195  * I have no idea what will happen if you try to use this module with another
196  * type of hardware. Your machine might catch fire or it might work with
197  * horrible performance...
198  */
199 static void
200 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
201 {
202 	struct ath_node *an = ATH_NODE(ni);
203 	struct amrr_node *amn = ATH_NODE_AMRR(an);
204 	const HAL_RATE_TABLE *rt = sc->sc_currates;
205 	u_int8_t rix;
206 
207 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
208 
209 	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
210 	    __func__, ether_sprintf(ni->ni_macaddr),
211 	    ni->ni_rates.rs_nrates > 0 ?
212 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
213 
214 	ni->ni_txrate = rate;
215 	/*
216 	 * Before associating a node has no rate set setup
217 	 * so we can't calculate any transmit codes to use.
218 	 * This is ok since we should never be sending anything
219 	 * but management frames and those always go at the
220 	 * lowest hardware rate.
221 	 */
222 	if (ni->ni_rates.rs_nrates > 0) {
223 		amn->amn_tx_rix0 = sc->sc_rixmap[
224 					       ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
225 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
226 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
227 			rt->info[amn->amn_tx_rix0].shortPreamble;
228 		if (sc->sc_mrretry) {
229 			amn->amn_tx_try0 = 1;
230 			amn->amn_tx_try1 = 1;
231 			amn->amn_tx_try2 = 1;
232 			amn->amn_tx_try3 = 1;
233 			if (--rate >= 0) {
234 				rix = sc->sc_rixmap[
235 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
236 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
237 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
238 					rt->info[rix].shortPreamble;
239 			} else {
240 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
241 			}
242 			if (--rate >= 0) {
243 				rix = sc->sc_rixmap[
244 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
245 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
246 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
247 					rt->info[rix].shortPreamble;
248 			} else {
249 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
250 			}
251 			if (rate > 0) {
252 				/* NB: only do this if we didn't already do it above */
253 				amn->amn_tx_rate3 = rt->info[0].rateCode;
254 				amn->amn_tx_rate3sp =
255 					an->an_tx_rate3 | rt->info[0].shortPreamble;
256 			} else {
257 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
258 			}
259 		} else {
260 			amn->amn_tx_try0 = ATH_TXMAXTRY;
261 			/* theorically, these statements are useless because
262 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
263 			 */
264 			amn->amn_tx_try1 = 0;
265 			amn->amn_tx_try2 = 0;
266 			amn->amn_tx_try3 = 0;
267 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
268 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
269 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
270 		}
271 	}
272 	node_reset (amn);
273 }
274 
275 /*
276  * Set the starting transmit rate for a node.
277  */
278 static void
279 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
280 {
281 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
282 	struct ieee80211com *ic = &sc->sc_ic;
283 	int srate;
284 
285 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
286 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
287 		/*
288 		 * No fixed rate is requested. For 11b start with
289 		 * the highest negotiated rate; otherwise, for 11g
290 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
291 		 */
292 		srate = ni->ni_rates.rs_nrates - 1;
293 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
294 			/*
295 			 * Scan the negotiated rate set to find the
296 			 * closest rate.
297 			 */
298 			/* NB: the rate set is assumed sorted */
299 			for (; srate >= 0 && RATE(srate) > 72; srate--)
300 				;
301 			KASSERT(srate >= 0, ("bogus rate set"));
302 		}
303 	} else {
304 		/*
305 		 * A fixed rate is to be used; ic_fixed_rate is an
306 		 * index into the supported rate set.  Convert this
307 		 * to the index into the negotiated rate set for
308 		 * the node.  We know the rate is there because the
309 		 * rate set is checked when the station associates.
310 		 */
311 		const struct ieee80211_rateset *rs =
312 			&ic->ic_sup_rates[ic->ic_curmode];
313 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
314 		/* NB: the rate set is assumed sorted */
315 		srate = ni->ni_rates.rs_nrates - 1;
316 		for (; srate >= 0 && RATE(srate) != r; srate--)
317 			;
318 		KASSERT(srate >= 0,
319 			("fixed rate %d not in rate set", ic->ic_fixed_rate));
320 	}
321 	ath_rate_update(sc, ni, srate);
322 #undef RATE
323 }
324 
325 static void
326 ath_rate_cb(void *arg, struct ieee80211_node *ni)
327 {
328 	struct ath_softc *sc = arg;
329 
330 	ath_rate_update(sc, ni, 0);
331 }
332 
333 /*
334  * Reset the rate control state for each 802.11 state transition.
335  */
336 void
337 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
338 {
339 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
340 	struct ieee80211com *ic = &sc->sc_ic;
341 	struct ieee80211_node *ni;
342 
343 	if (state == IEEE80211_S_INIT) {
344 		callout_stop(&asc->timer);
345 		return;
346 	}
347 	if (ic->ic_opmode == IEEE80211_M_STA) {
348 		/*
349 		 * Reset local xmit state; this is really only
350 		 * meaningful when operating in station mode.
351 		 */
352 		ni = ic->ic_bss;
353 		if (state == IEEE80211_S_RUN) {
354 			ath_rate_ctl_start(sc, ni);
355 		} else {
356 			ath_rate_update(sc, ni, 0);
357 		}
358 	} else {
359 		/*
360 		 * When operating as a station the node table holds
361 		 * the AP's that were discovered during scanning.
362 		 * For any other operating mode we want to reset the
363 		 * tx rate state of each node.
364 		 */
365 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
366 		ath_rate_update(sc, ic->ic_bss, 0);
367 	}
368 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
369 	    state == IEEE80211_S_RUN) {
370 		int interval;
371 		/*
372 		 * Start the background rate control thread if we
373 		 * are not configured to use a fixed xmit rate.
374 		 */
375 		interval = ath_rateinterval;
376 		if (ic->ic_opmode == IEEE80211_M_STA)
377 			interval /= 2;
378 		callout_reset(&asc->timer, (interval * hz) / 1000,
379 			ath_ratectl, &sc->sc_if);
380 	}
381 }
382 
383 /*
384  * Examine and potentially adjust the transmit rate.
385  */
386 static void
387 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
388 {
389 	struct ath_softc *sc = arg;
390 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
391 	int old_rate;
392 
393 #define is_success(amn) \
394 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
395 #define is_enough(amn) \
396 (amn->amn_tx_try0_cnt > 10)
397 #define is_failure(amn) \
398 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
399 #define is_max_rate(ni) \
400 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
401 #define is_min_rate(ni) \
402 (ni->ni_txrate == 0)
403 
404 	old_rate = ni->ni_txrate;
405 
406   	DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
407 		 amn->amn_tx_try0_cnt,
408 		 amn->amn_tx_try1_cnt,
409 		 amn->amn_tx_try2_cnt,
410 		 amn->amn_tx_try3_cnt,
411 		 amn->amn_success_threshold);
412   	if (is_success (amn) && is_enough (amn)) {
413 		amn->amn_success++;
414 		if (amn->amn_success == amn->amn_success_threshold &&
415   		    !is_max_rate (ni)) {
416   			amn->amn_recovery = 1;
417   			amn->amn_success = 0;
418   			ni->ni_txrate++;
419 			DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
420   		} else {
421 			amn->amn_recovery = 0;
422 		}
423   	} else if (is_failure (amn)) {
424   		amn->amn_success = 0;
425   		if (!is_min_rate (ni)) {
426   			if (amn->amn_recovery) {
427   				/* recovery failure. */
428   				amn->amn_success_threshold *= 2;
429   				amn->amn_success_threshold = min (amn->amn_success_threshold,
430 								  (u_int)ath_rate_max_success_threshold);
431  				DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
432   			} else {
433   				/* simple failure. */
434  				amn->amn_success_threshold = ath_rate_min_success_threshold;
435  				DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
436   			}
437 			amn->amn_recovery = 0;
438   			ni->ni_txrate--;
439    		} else {
440 			amn->amn_recovery = 0;
441 		}
442 
443    	}
444 	if (is_enough (amn) || old_rate != ni->ni_txrate) {
445 		/* reset counters. */
446 		amn->amn_tx_try0_cnt = 0;
447 		amn->amn_tx_try1_cnt = 0;
448 		amn->amn_tx_try2_cnt = 0;
449 		amn->amn_tx_try3_cnt = 0;
450 		amn->amn_tx_failure_cnt = 0;
451 	}
452 	if (old_rate != ni->ni_txrate) {
453 		ath_rate_update(sc, ni, ni->ni_txrate);
454 	}
455 }
456 
457 static void
458 ath_ratectl(void *arg)
459 {
460 	struct ifnet *ifp = arg;
461 	struct ath_softc *sc = ifp->if_softc;
462 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
463 	struct ieee80211com *ic = &sc->sc_ic;
464 	int interval;
465 
466 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
467 		sc->sc_stats.ast_rate_calls++;
468 
469 		if (ic->ic_opmode == IEEE80211_M_STA)
470 			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
471 		else
472 			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
473 	}
474 	interval = ath_rateinterval;
475 	if (ic->ic_opmode == IEEE80211_M_STA)
476 		interval /= 2;
477 	callout_reset(&asc->timer, (interval * hz) / 1000,
478 		ath_ratectl, &sc->sc_if);
479 }
480 
481 static void
482 ath_rate_sysctlattach(struct ath_softc *sc)
483 {
484 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
485 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
486 
487 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
488 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
489 		"rate control: operation interval (ms)");
490 	/* XXX bounds check values */
491 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
492 		"max_sucess_threshold", CTLFLAG_RW,
493 		&ath_rate_max_success_threshold, 0, "");
494 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
495 		"min_sucess_threshold", CTLFLAG_RW,
496 		&ath_rate_min_success_threshold, 0, "");
497 }
498 
499 struct ath_ratectrl *
500 ath_rate_attach(struct ath_softc *sc)
501 {
502 	struct amrr_softc *asc;
503 
504 	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
505 	if (asc == NULL)
506 		return NULL;
507 	asc->arc.arc_space = sizeof(struct amrr_node);
508 	callout_init(&asc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
509 	ath_rate_sysctlattach(sc);
510 
511 	return &asc->arc;
512 }
513 
514 void
515 ath_rate_detach(struct ath_ratectrl *arc)
516 {
517 	struct amrr_softc *asc = (struct amrr_softc *) arc;
518 
519 	callout_drain(&asc->timer);
520 	free(asc, M_DEVBUF);
521 }
522