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