3 * Copyright (c) 2009-2010 Alexander Egorenkov <egorenar@gmail.com>
4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
20 #include <sys/kernel.h>
21 #include <sys/module.h>
22 #include <sys/socket.h>
23 #include <sys/sysctl.h>
26 #include <net/if_media.h>
28 #include <net80211/ieee80211_var.h>
30 #include "rt2860_amrr.h"
36 #define RT2860_AMRR_IS_SUCCESS(amrr_node) ((amrr_node)->retrycnt < (amrr_node)->txcnt / 10)
38 #define RT2860_AMRR_IS_FAILURE(amrr_node) ((amrr_node)->retrycnt > (amrr_node)->txcnt / 3)
40 #define RT2860_AMRR_IS_ENOUGH(amrr_node) ((amrr_node)->txcnt > 10)
43 * Static function prototypes
46 static int rt2860_amrr_update(struct rt2860_amrr
*amrr
,
47 struct rt2860_amrr_node
*amrr_node
, struct ieee80211_node
*ni
);
52 void rt2860_amrr_init(struct rt2860_amrr
*amrr
, struct ieee80211com
*ic
,
53 int ntxpath
, int min_success_threshold
, int max_success_threshold
, int msecs
)
57 amrr
->ntxpath
= ntxpath
;
59 amrr
->min_success_threshold
= min_success_threshold
;
60 amrr
->max_success_threshold
= max_success_threshold
;
65 t
= msecs_to_ticks(msecs
);
67 amrr
->interval
= (t
< 1) ? 1 : t
;
73 void rt2860_amrr_cleanup(struct rt2860_amrr
*amrr
)
78 * rt2860_amrr_node_init
80 void rt2860_amrr_node_init(struct rt2860_amrr
*amrr
,
81 struct rt2860_amrr_node
*amrr_node
, struct ieee80211_node
*ni
)
83 const struct ieee80211_rateset
*rs
;
85 amrr_node
->amrr
= amrr
;
86 amrr_node
->success
= 0;
87 amrr_node
->recovery
= 0;
89 amrr_node
->retrycnt
= 0;
90 amrr_node
->success_threshold
= amrr
->min_success_threshold
;
92 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
94 rs
= (const struct ieee80211_rateset
*) &ni
->ni_htrates
;
96 for (ni
->ni_txrate
= rs
->rs_nrates
- 1;
97 ni
->ni_txrate
> 0 && (rs
->rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
) > 4;
104 for (ni
->ni_txrate
= rs
->rs_nrates
- 1;
105 ni
->ni_txrate
> 0 && (rs
->rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
) > 72;
109 amrr_node
->ticks
= ticks
;
115 int rt2860_amrr_choose(struct ieee80211_node
*ni
,
116 struct rt2860_amrr_node
*amrr_node
)
118 struct rt2860_amrr
*amrr
;
121 amrr
= amrr_node
->amrr
;
123 if (RT2860_AMRR_IS_ENOUGH(amrr_node
) &&
124 (ticks
- amrr_node
->ticks
) > amrr
->interval
)
126 rate_index
= rt2860_amrr_update(amrr
, amrr_node
, ni
);
127 if (rate_index
!= ni
->ni_txrate
)
128 ni
->ni_txrate
= rate_index
;
130 amrr_node
->ticks
= ticks
;
134 rate_index
= ni
->ni_txrate
;
143 static int rt2860_amrr_update(struct rt2860_amrr
*amrr
,
144 struct rt2860_amrr_node
*amrr_node
, struct ieee80211_node
*ni
)
146 const struct ieee80211_rateset
*rs
;
149 KASSERT(RT2860_AMRR_IS_ENOUGH(amrr_node
),
150 ("not enough Tx count: txcnt=%d",
153 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
154 rs
= (const struct ieee80211_rateset
*) &ni
->ni_htrates
;
158 rate_index
= ni
->ni_txrate
;
160 if (RT2860_AMRR_IS_SUCCESS(amrr_node
))
162 amrr_node
->success
++;
163 if ((amrr_node
->success
>= amrr_node
->success_threshold
) &&
164 (rate_index
+ 1 < rs
->rs_nrates
) &&
165 (!(ni
->ni_flags
& IEEE80211_NODE_HT
) || (rs
->rs_rates
[rate_index
+ 1] & IEEE80211_RATE_VAL
) < (amrr
->ntxpath
* 8)))
167 amrr_node
->recovery
= 1;
168 amrr_node
->success
= 0;
174 amrr_node
->recovery
= 0;
177 else if (RT2860_AMRR_IS_FAILURE(amrr_node
))
179 amrr_node
->success
= 0;
183 if (amrr_node
->recovery
)
185 amrr_node
->success_threshold
*= 2;
186 if (amrr_node
->success_threshold
> amrr
->max_success_threshold
)
187 amrr_node
->success_threshold
= amrr
->max_success_threshold
;
191 amrr_node
->success_threshold
= amrr
->min_success_threshold
;
197 amrr_node
->recovery
= 0;
200 amrr_node
->txcnt
= 0;
201 amrr_node
->retrycnt
= 0;