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