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