Fixed Tx hang issue with DMA segment length = 0
[ralink_drivers/rt2860_fbsd8.git] / rt2860_amrr.c
blobc9871c54503775cf9a05b308420d2a999057d673
2 /*-
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>
25 #include <net/if.h>
26 #include <net/if_media.h>
28 #include <net80211/ieee80211_var.h>
30 #include "rt2860_amrr.h"
33 * Defines and macros
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);
50 * rt2860_amrr_init
52 void rt2860_amrr_init(struct rt2860_amrr *amrr, struct ieee80211vap *vap,
53 int min_success_threshold, int max_success_threshold, int msecs)
55 int t;
57 amrr->min_success_threshold = min_success_threshold;
58 amrr->max_success_threshold = max_success_threshold;
60 if (msecs < 100)
61 msecs = 100;
63 t = msecs_to_ticks(msecs);
65 amrr->interval = (t < 1) ? 1 : t;
69 * rt2860_amrr_cleanup
71 void rt2860_amrr_cleanup(struct rt2860_amrr *amrr)
76 * rt2860_amrr_node_init
78 void rt2860_amrr_node_init(struct rt2860_amrr *amrr,
79 struct rt2860_amrr_node *amrr_node, struct ieee80211_node *ni)
81 const struct ieee80211_rateset *rs;
83 amrr_node->amrr = amrr;
84 amrr_node->success = 0;
85 amrr_node->recovery = 0;
86 amrr_node->txcnt = 0;
87 amrr_node->retrycnt = 0;
88 amrr_node->success_threshold = amrr->min_success_threshold;
90 if (ni->ni_flags & IEEE80211_NODE_HT)
92 rs = (const struct ieee80211_rateset *) &ni->ni_htrates;
94 for (amrr_node->rate_index = rs->rs_nrates - 1;
95 amrr_node->rate_index > 0 && rs->rs_rates[amrr_node->rate_index] > 4;
96 amrr_node->rate_index--) ;
98 ni->ni_txrate = rs->rs_rates[amrr_node->rate_index] | IEEE80211_RATE_MCS;
100 else
102 rs = &ni->ni_rates;
104 for (amrr_node->rate_index = rs->rs_nrates - 1;
105 amrr_node->rate_index > 0 && (rs->rs_rates[amrr_node->rate_index] & IEEE80211_RATE_VAL) > 72;
106 amrr_node->rate_index--) ;
108 ni->ni_txrate = rs->rs_rates[amrr_node->rate_index] & IEEE80211_RATE_VAL;
111 amrr_node->ticks = ticks;
115 * rt2860_amrr_choose
117 int rt2860_amrr_choose(struct ieee80211_node *ni,
118 struct rt2860_amrr_node *amrr_node)
120 struct rt2860_amrr *amrr;
121 int rate_index;
123 amrr = amrr_node->amrr;
125 if (RT2860_AMRR_IS_ENOUGH(amrr_node) &&
126 (ticks - amrr_node->ticks) > amrr->interval)
128 rate_index = rt2860_amrr_update(amrr, amrr_node, ni);
129 if (rate_index != amrr_node->rate_index)
131 if (ni->ni_flags & IEEE80211_NODE_HT)
132 ni->ni_txrate = ni->ni_htrates.rs_rates[rate_index] | IEEE80211_RATE_MCS;
133 else
134 ni->ni_txrate = ni->ni_rates.rs_rates[rate_index] & IEEE80211_RATE_VAL;
136 amrr_node->rate_index = rate_index;
139 amrr_node->ticks = ticks;
141 else
143 rate_index = amrr_node->rate_index;
146 return rate_index;
150 * rt2860_amrr_update
152 static int rt2860_amrr_update(struct rt2860_amrr *amrr,
153 struct rt2860_amrr_node *amrr_node, struct ieee80211_node *ni)
155 const struct ieee80211_rateset *rs;
156 int rate_index;
158 KASSERT(RT2860_AMRR_IS_ENOUGH(amrr_node),
159 ("not enough Tx count: txcnt=%d",
160 amrr_node->txcnt));
162 if (ni->ni_flags & IEEE80211_NODE_HT)
163 rs = (const struct ieee80211_rateset *) &ni->ni_htrates;
164 else
165 rs = &ni->ni_rates;
167 rate_index = amrr_node->rate_index;
169 if (RT2860_AMRR_IS_SUCCESS(amrr_node))
171 amrr_node->success++;
172 if ((amrr_node->success >= amrr_node->success_threshold) &&
173 (rate_index + 1 < rs->rs_nrates))
175 amrr_node->recovery = 1;
176 amrr_node->success = 0;
178 rate_index++;
180 else
182 amrr_node->recovery = 0;
185 else if (RT2860_AMRR_IS_FAILURE(amrr_node))
187 amrr_node->success = 0;
189 if (rate_index > 0)
191 if (amrr_node->recovery)
193 amrr_node->success_threshold *= 2;
194 if (amrr_node->success_threshold > amrr->max_success_threshold)
195 amrr_node->success_threshold = amrr->max_success_threshold;
197 else
199 amrr_node->success_threshold = amrr->min_success_threshold;
202 rate_index--;
205 amrr_node->recovery = 0;
208 amrr_node->txcnt = 0;
209 amrr_node->retrycnt = 0;
211 return rate_index;