1 /*	$NetBSD: athrate-amrr.c,v 1.12 2012/11/08 20:43:55 dyoung 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.12 2012/11/08 20:43:55 dyoung 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/errno.h>
62 #include <sys/bus.h>
63 #include <sys/socket.h>
64 
65 #include <net/if.h>
66 #include <net/if_media.h>
67 #include <net/if_arp.h>
68 #include <net/if_ether.h>		/* XXX for ether_sprintf */
69 
70 #include <net80211/ieee80211_var.h>
71 
72 #include <net/bpf.h>
73 
74 #ifdef INET
75 #include <netinet/in.h>
76 #endif
77 
78 #include <dev/ic/athvar.h>
79 #include <dev/ic/athrate-amrr.h>
80 
81 #include <external/isc/atheros_hal/dist/ah.h>
82 
83 #define	AMRR_DEBUG
84 #ifdef AMRR_DEBUG
85 #define	DPRINTF(sc, _fmt, ...) do {					\
86 	if (sc->sc_debug & 0x10)					\
87 		printf(_fmt, __VA_ARGS__);				\
88 } while (0)
89 #else
90 #define	DPRINTF(sc, _fmt, ...)
91 #endif
92 
93 static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
94 static	int ath_rate_max_success_threshold = 10;
95 static	int ath_rate_min_success_threshold = 1;
96 
97 static void	ath_ratectl(void *);
98 static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
99 			int rate);
100 static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
101 static void	ath_rate_ctl(void *, struct ieee80211_node *);
102 
103 void
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)104 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
105 {
106 	/* NB: assumed to be zero'd by caller */
107 	ath_rate_update(sc, &an->an_node, 0);
108 }
109 
110 void
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)111 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
112 {
113 }
114 
115 void
ath_rate_findrate(struct ath_softc * sc,struct ath_node * an,int shortPreamble,size_t frameLen,u_int8_t * rix,int * try0,u_int8_t * txrate)116 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
117 	int shortPreamble, size_t frameLen,
118 	u_int8_t *rix, int *try0, u_int8_t *txrate)
119 {
120 	struct amrr_node *amn = ATH_NODE_AMRR(an);
121 
122 	*rix = amn->amn_tx_rix0;
123 	*try0 = amn->amn_tx_try0;
124 	if (shortPreamble)
125 		*txrate = amn->amn_tx_rate0sp;
126 	else
127 		*txrate = amn->amn_tx_rate0;
128 }
129 
130 void
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)131 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
132 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
133 {
134 	struct amrr_node *amn = ATH_NODE_AMRR(an);
135 
136 	ath_hal_setupxtxdesc(sc->sc_ah, ds
137 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
138 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
139 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
140 	);
141 }
142 
143 void
ath_rate_tx_complete(struct ath_softc * sc,struct ath_node * an,const struct ath_desc * ds,const struct ath_desc * ds0)144 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
145 	const struct ath_desc *ds, const struct ath_desc *ds0)
146 {
147 	struct amrr_node *amn = ATH_NODE_AMRR(an);
148 	int sr = ds->ds_txstat.ts_shortretry;
149 	int lr = ds->ds_txstat.ts_longretry;
150 	int retry_count = sr + lr;
151 
152 	amn->amn_tx_try0_cnt++;
153 	if (retry_count == 1) {
154 		amn->amn_tx_try1_cnt++;
155 	} else if (retry_count == 2) {
156 		amn->amn_tx_try1_cnt++;
157 		amn->amn_tx_try2_cnt++;
158 	} else if (retry_count == 3) {
159 		amn->amn_tx_try1_cnt++;
160 		amn->amn_tx_try2_cnt++;
161 		amn->amn_tx_try3_cnt++;
162 	} else if (retry_count > 3) {
163 		amn->amn_tx_try1_cnt++;
164 		amn->amn_tx_try2_cnt++;
165 		amn->amn_tx_try3_cnt++;
166 		amn->amn_tx_failure_cnt++;
167 	}
168 }
169 
170 void
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)171 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
172 {
173 	if (isnew)
174 		ath_rate_ctl_start(sc, &an->an_node);
175 }
176 
177 static void
node_reset(struct amrr_node * amn)178 node_reset (struct amrr_node *amn)
179 {
180 	amn->amn_tx_try0_cnt = 0;
181 	amn->amn_tx_try1_cnt = 0;
182 	amn->amn_tx_try2_cnt = 0;
183 	amn->amn_tx_try3_cnt = 0;
184 	amn->amn_tx_failure_cnt = 0;
185   	amn->amn_success = 0;
186   	amn->amn_recovery = 0;
187   	amn->amn_success_threshold = ath_rate_min_success_threshold;
188 }
189 
190 
191 /**
192  * The code below assumes that we are dealing with hardware multi rate retry
193  * I have no idea what will happen if you try to use this module with another
194  * type of hardware. Your machine might catch fire or it might work with
195  * horrible performance...
196  */
197 static void
ath_rate_update(struct ath_softc * sc,struct ieee80211_node * ni,int rate)198 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
199 {
200 	struct ath_node *an = ATH_NODE(ni);
201 	struct amrr_node *amn = ATH_NODE_AMRR(an);
202 	const HAL_RATE_TABLE *rt = sc->sc_currates;
203 	u_int8_t rix;
204 
205 	KASSERTMSG(rt != NULL, "no rate table, mode %u", sc->sc_curmode);
206 
207 	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
208 	    __func__, ether_sprintf(ni->ni_macaddr),
209 	    ni->ni_rates.rs_nrates > 0 ?
210 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
211 
212 	ni->ni_txrate = rate;
213 	/*
214 	 * Before associating a node has no rate set setup
215 	 * so we can't calculate any transmit codes to use.
216 	 * This is ok since we should never be sending anything
217 	 * but management frames and those always go at the
218 	 * lowest hardware rate.
219 	 */
220 	if (ni->ni_rates.rs_nrates > 0) {
221 		amn->amn_tx_rix0 = sc->sc_rixmap[
222 					       ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
223 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
224 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
225 			rt->info[amn->amn_tx_rix0].shortPreamble;
226 		if (sc->sc_mrretry) {
227 			amn->amn_tx_try0 = 1;
228 			amn->amn_tx_try1 = 1;
229 			amn->amn_tx_try2 = 1;
230 			amn->amn_tx_try3 = 1;
231 			if (--rate >= 0) {
232 				rix = sc->sc_rixmap[
233 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
234 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
235 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
236 					rt->info[rix].shortPreamble;
237 			} else {
238 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
239 			}
240 			if (--rate >= 0) {
241 				rix = sc->sc_rixmap[
242 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
243 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
244 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
245 					rt->info[rix].shortPreamble;
246 			} else {
247 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
248 			}
249 			if (rate > 0) {
250 				/* NB: only do this if we didn't already do it above */
251 				amn->amn_tx_rate3 = rt->info[0].rateCode;
252 				amn->amn_tx_rate3sp =
253 					an->an_tx_rate3 | rt->info[0].shortPreamble;
254 			} else {
255 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
256 			}
257 		} else {
258 			amn->amn_tx_try0 = ATH_TXMAXTRY;
259 			/* theorically, these statements are useless because
260 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
261 			 */
262 			amn->amn_tx_try1 = 0;
263 			amn->amn_tx_try2 = 0;
264 			amn->amn_tx_try3 = 0;
265 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
266 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
267 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
268 		}
269 	}
270 	node_reset (amn);
271 }
272 
273 /*
274  * Set the starting transmit rate for a node.
275  */
276 static void
ath_rate_ctl_start(struct ath_softc * sc,struct ieee80211_node * ni)277 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
278 {
279 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
280 	struct ieee80211com *ic = &sc->sc_ic;
281 	int srate;
282 
283 	KASSERTMSG(ni->ni_rates.rs_nrates > 0, "no rates");
284 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
285 		/*
286 		 * No fixed rate is requested. For 11b start with
287 		 * the highest negotiated rate; otherwise, for 11g
288 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
289 		 */
290 		srate = ni->ni_rates.rs_nrates - 1;
291 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
292 			/*
293 			 * Scan the negotiated rate set to find the
294 			 * closest rate.
295 			 */
296 			/* NB: the rate set is assumed sorted */
297 			for (; srate >= 0 && RATE(srate) > 72; srate--)
298 				;
299 			KASSERTMSG(srate >= 0, "bogus rate set");
300 		}
301 	} else {
302 		/*
303 		 * A fixed rate is to be used; ic_fixed_rate is an
304 		 * index into the supported rate set.  Convert this
305 		 * to the index into the negotiated rate set for
306 		 * the node.  We know the rate is there because the
307 		 * rate set is checked when the station associates.
308 		 */
309 		const struct ieee80211_rateset *rs =
310 			&ic->ic_sup_rates[ic->ic_curmode];
311 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
312 		/* NB: the rate set is assumed sorted */
313 		srate = ni->ni_rates.rs_nrates - 1;
314 		for (; srate >= 0 && RATE(srate) != r; srate--)
315 			;
316 		KASSERTMSG(srate >= 0,
317 			"fixed rate %d not in rate set", ic->ic_fixed_rate);
318 	}
319 	ath_rate_update(sc, ni, srate);
320 #undef RATE
321 }
322 
323 static void
ath_rate_cb(void * arg,struct ieee80211_node * ni)324 ath_rate_cb(void *arg, struct ieee80211_node *ni)
325 {
326 	struct ath_softc *sc = arg;
327 
328 	ath_rate_update(sc, ni, 0);
329 }
330 
331 /*
332  * Reset the rate control state for each 802.11 state transition.
333  */
334 void
ath_rate_newstate(struct ath_softc * sc,enum ieee80211_state state)335 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
336 {
337 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
338 	struct ieee80211com *ic = &sc->sc_ic;
339 	struct ieee80211_node *ni;
340 
341 	if (state == IEEE80211_S_INIT) {
342 		callout_stop(&asc->timer);
343 		return;
344 	}
345 	if (ic->ic_opmode == IEEE80211_M_STA) {
346 		/*
347 		 * Reset local xmit state; this is really only
348 		 * meaningful when operating in station mode.
349 		 */
350 		ni = ic->ic_bss;
351 		if (state == IEEE80211_S_RUN) {
352 			ath_rate_ctl_start(sc, ni);
353 		} else {
354 			ath_rate_update(sc, ni, 0);
355 		}
356 	} else {
357 		/*
358 		 * When operating as a station the node table holds
359 		 * the AP's that were discovered during scanning.
360 		 * For any other operating mode we want to reset the
361 		 * tx rate state of each node.
362 		 */
363 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
364 		ath_rate_update(sc, ic->ic_bss, 0);
365 	}
366 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
367 	    state == IEEE80211_S_RUN) {
368 		int interval;
369 		/*
370 		 * Start the background rate control thread if we
371 		 * are not configured to use a fixed xmit rate.
372 		 */
373 		interval = ath_rateinterval;
374 		if (ic->ic_opmode == IEEE80211_M_STA)
375 			interval /= 2;
376 		callout_reset(&asc->timer, (interval * hz) / 1000,
377 			ath_ratectl, &sc->sc_if);
378 	}
379 }
380 
381 /*
382  * Examine and potentially adjust the transmit rate.
383  */
384 static void
ath_rate_ctl(void * arg,struct ieee80211_node * ni)385 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
386 {
387 	struct ath_softc *sc = arg;
388 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
389 	int old_rate;
390 
391 #define is_success(amn) \
392 (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
393 #define is_enough(amn) \
394 (amn->amn_tx_try0_cnt > 10)
395 #define is_failure(amn) \
396 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
397 #define is_max_rate(ni) \
398 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
399 #define is_min_rate(ni) \
400 (ni->ni_txrate == 0)
401 
402 	old_rate = ni->ni_txrate;
403 
404   	DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
405 		 amn->amn_tx_try0_cnt,
406 		 amn->amn_tx_try1_cnt,
407 		 amn->amn_tx_try2_cnt,
408 		 amn->amn_tx_try3_cnt,
409 		 amn->amn_success_threshold);
410   	if (is_success (amn) && is_enough (amn)) {
411 		amn->amn_success++;
412 		if (amn->amn_success == amn->amn_success_threshold &&
413   		    !is_max_rate (ni)) {
414   			amn->amn_recovery = 1;
415   			amn->amn_success = 0;
416   			ni->ni_txrate++;
417 			DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
418   		} else {
419 			amn->amn_recovery = 0;
420 		}
421   	} else if (is_failure (amn)) {
422   		amn->amn_success = 0;
423   		if (!is_min_rate (ni)) {
424   			if (amn->amn_recovery) {
425   				/* recovery failure. */
426   				amn->amn_success_threshold *= 2;
427   				amn->amn_success_threshold = min (amn->amn_success_threshold,
428 								  (u_int)ath_rate_max_success_threshold);
429  				DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
430   			} else {
431   				/* simple failure. */
432  				amn->amn_success_threshold = ath_rate_min_success_threshold;
433  				DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
434   			}
435 			amn->amn_recovery = 0;
436   			ni->ni_txrate--;
437    		} else {
438 			amn->amn_recovery = 0;
439 		}
440 
441    	}
442 	if (is_enough (amn) || old_rate != ni->ni_txrate) {
443 		/* reset counters. */
444 		amn->amn_tx_try0_cnt = 0;
445 		amn->amn_tx_try1_cnt = 0;
446 		amn->amn_tx_try2_cnt = 0;
447 		amn->amn_tx_try3_cnt = 0;
448 		amn->amn_tx_failure_cnt = 0;
449 	}
450 	if (old_rate != ni->ni_txrate) {
451 		ath_rate_update(sc, ni, ni->ni_txrate);
452 	}
453 }
454 
455 static void
ath_ratectl(void * arg)456 ath_ratectl(void *arg)
457 {
458 	struct ifnet *ifp = arg;
459 	struct ath_softc *sc = ifp->if_softc;
460 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
461 	struct ieee80211com *ic = &sc->sc_ic;
462 	int interval;
463 
464 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
465 		sc->sc_stats.ast_rate_calls++;
466 
467 		if (ic->ic_opmode == IEEE80211_M_STA)
468 			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
469 		else
470 			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
471 	}
472 	interval = ath_rateinterval;
473 	if (ic->ic_opmode == IEEE80211_M_STA)
474 		interval /= 2;
475 	callout_reset(&asc->timer, (interval * hz) / 1000,
476 		ath_ratectl, &sc->sc_if);
477 }
478 
479 static void
ath_rate_sysctlattach(struct ath_softc * sc)480 ath_rate_sysctlattach(struct ath_softc *sc)
481 {
482 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
483 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
484 
485 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
486 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
487 		"rate control: operation interval (ms)");
488 	/* XXX bounds check values */
489 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
490 		"max_sucess_threshold", CTLFLAG_RW,
491 		&ath_rate_max_success_threshold, 0, "");
492 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
493 		"min_sucess_threshold", CTLFLAG_RW,
494 		&ath_rate_min_success_threshold, 0, "");
495 }
496 
497 struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)498 ath_rate_attach(struct ath_softc *sc)
499 {
500 	struct amrr_softc *asc;
501 
502 	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
503 	if (asc == NULL)
504 		return NULL;
505 	asc->arc.arc_space = sizeof(struct amrr_node);
506 	callout_init(&asc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
507 	ath_rate_sysctlattach(sc);
508 
509 	return &asc->arc;
510 }
511 
512 void
ath_rate_detach(struct ath_ratectrl * arc)513 ath_rate_detach(struct ath_ratectrl *arc)
514 {
515 	struct amrr_softc *asc = (struct amrr_softc *) arc;
516 
517 	callout_drain(&asc->timer);
518 	free(asc, M_DEVBUF);
519 }
520