update ar7 flash map for squashfs 3.0
[openwrt.git] / target / linux / package / ieee80211-dscape / src / rate_control.c
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005, Devicescape Software, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10 #include <linux/config.h>
11 #include <linux/version.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/netdevice.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/compiler.h>
19
20 #include <net/ieee80211.h>
21 #include "ieee80211_i.h"
22 #include "rate_control.h"
23
24
25 /* This is a minimal implementation of TX rate controlling that can be used
26 * as the default when no improved mechanisms are available. */
27
28
29 #define RATE_CONTROL_EMERG_DEC 2
30 #define RATE_CONTROL_INTERVAL (HZ / 20)
31 #define RATE_CONTROL_MIN_TX 10
32
33
34 static void rate_control_rate_inc(struct ieee80211_local *local,
35 struct sta_info *sta)
36 {
37 struct ieee80211_sub_if_data *sdata;
38 int i = sta->txrate;
39 int maxrate;
40
41 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
42 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
43 /* forced unicast rate - do not change STA rate */
44 return;
45 }
46
47 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
48
49 if (i > local->num_curr_rates)
50 i = local->num_curr_rates - 2;
51
52 while (i + 1 < local->num_curr_rates) {
53 i++;
54 if (sta->supp_rates & BIT(i) &&
55 local->curr_rates[i].flags & IEEE80211_RATE_SUPPORTED &&
56 (maxrate < 0 || i <= maxrate)) {
57 sta->txrate = i;
58 break;
59 }
60 }
61 }
62
63
64 static void rate_control_rate_dec(struct ieee80211_local *local,
65 struct sta_info *sta)
66 {
67 struct ieee80211_sub_if_data *sdata;
68 int i = sta->txrate;
69
70 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
71 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
72 /* forced unicast rate - do not change STA rate */
73 return;
74 }
75
76 if (i > local->num_curr_rates)
77 i = local->num_curr_rates;
78
79 while (i > 0) {
80 i--;
81 if (sta->supp_rates & BIT(i) &&
82 local->curr_rates[i].flags & IEEE80211_RATE_SUPPORTED) {
83 sta->txrate = i;
84 break;
85 }
86 }
87 }
88
89
90 static struct ieee80211_rate *
91 rate_control_lowest_rate(struct ieee80211_local *local)
92 {
93 int i;
94
95 for (i = 0; i < local->num_curr_rates; i++) {
96 struct ieee80211_rate *rate = &local->curr_rates[i];
97
98 if (rate->flags & IEEE80211_RATE_SUPPORTED
99 )
100 return rate;
101 }
102
103 printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
104 "found\n");
105 return &local->curr_rates[0];
106 }
107
108
109 struct global_rate_control {
110 int dummy;
111 };
112
113 struct sta_rate_control {
114 unsigned long last_rate_change;
115 u32 tx_num_failures;
116 u32 tx_num_xmit;
117
118 unsigned long avg_rate_update;
119 u32 tx_avg_rate_sum;
120 u32 tx_avg_rate_num;
121 };
122
123
124 static void rate_control_simple_tx_status(struct net_device *dev,
125 struct sk_buff *skb,
126 struct ieee80211_tx_status *status)
127 {
128 struct ieee80211_local *local = dev->priv;
129 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
130 struct sta_info *sta;
131 struct sta_rate_control *srctrl;
132
133 sta = sta_info_get(local, hdr->addr1);
134
135 if (!sta)
136 return;
137
138 srctrl = sta->rate_ctrl_priv;
139 srctrl->tx_num_xmit++;
140 if (status->excessive_retries) {
141 sta->antenna_sel = sta->antenna_sel == 1 ? 2 : 1;
142 if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) {
143 printk(KERN_DEBUG "%s: " MACSTR " TX antenna --> %d "
144 "(@%lu)\n",
145 dev->name, MAC2STR(hdr->addr1),
146 sta->antenna_sel, jiffies);
147 }
148 srctrl->tx_num_failures++;
149 sta->tx_retry_failed++;
150 sta->tx_num_consecutive_failures++;
151 sta->tx_num_mpdu_fail++;
152 } else {
153 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
154 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
155 sta->last_ack_rssi[2] = status->ack_signal;
156 sta->tx_num_consecutive_failures = 0;
157 sta->tx_num_mpdu_ok++;
158 }
159 sta->tx_retry_count += status->retry_count;
160 sta->tx_num_mpdu_fail += status->retry_count;
161
162 if (time_after(jiffies,
163 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
164 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
165 u32 per_failed;
166 srctrl->last_rate_change = jiffies;
167
168 per_failed = (100 * sta->tx_num_mpdu_fail) /
169 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
170 /* TODO: calculate average per_failed to make adjusting
171 * parameters easier */
172 #if 0
173 if (net_ratelimit()) {
174 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
175 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
176 per_failed);
177 }
178 #endif
179
180 if (per_failed > local->rate_ctrl_num_down) {
181 rate_control_rate_dec(local, sta);
182 } else if (per_failed < local->rate_ctrl_num_up) {
183 rate_control_rate_inc(local, sta);
184 }
185 srctrl->tx_avg_rate_sum += local->curr_rates[sta->txrate].rate;
186 srctrl->tx_avg_rate_num++;
187 srctrl->tx_num_failures = 0;
188 srctrl->tx_num_xmit = 0;
189 } else if (sta->tx_num_consecutive_failures >=
190 RATE_CONTROL_EMERG_DEC) {
191 rate_control_rate_dec(local, sta);
192 }
193
194 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
195 srctrl->avg_rate_update = jiffies;
196 if (srctrl->tx_avg_rate_num > 0) {
197 #ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
198 printk(KERN_DEBUG "%s: STA " MACSTR " Average rate: "
199 "%d (%d/%d)\n",
200 dev->name, MAC2STR(sta->addr),
201 srctrl->tx_avg_rate_sum /
202 srctrl->tx_avg_rate_num,
203 srctrl->tx_avg_rate_sum,
204 srctrl->tx_avg_rate_num);
205 #endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
206 srctrl->tx_avg_rate_sum = 0;
207 srctrl->tx_avg_rate_num = 0;
208 }
209 }
210
211 sta_info_release(local, sta);
212 }
213
214
215 static struct ieee80211_rate *
216 rate_control_simple_get_rate(struct net_device *dev, struct sk_buff *skb,
217 struct rate_control_extra *extra)
218 {
219 struct ieee80211_local *local = dev->priv;
220 struct ieee80211_sub_if_data *sdata;
221 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
222 struct sta_info *sta;
223 int rateidx, nonerp_idx;
224 u16 fc;
225
226 memset(extra, 0, sizeof(*extra));
227
228 fc = le16_to_cpu(hdr->frame_control);
229 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_DATA ||
230 (hdr->addr1[0] & 0x01)) {
231 /* Send management frames and broadcast/multicast data using
232 * lowest rate. */
233 /* TODO: this could probably be improved.. */
234 return rate_control_lowest_rate(local);
235 }
236
237 sta = sta_info_get(local, hdr->addr1);
238
239 if (!sta)
240 return rate_control_lowest_rate(local);
241
242 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
243 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
244 sta->txrate = sdata->bss->force_unicast_rateidx;
245
246 rateidx = sta->txrate;
247
248 if (rateidx >= local->num_curr_rates)
249 rateidx = local->num_curr_rates - 1;
250
251 sta->last_txrate = rateidx;
252 nonerp_idx = rateidx;
253 while (nonerp_idx > 0 &&
254 ((local->curr_rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
255 !(local->curr_rates[nonerp_idx].flags &
256 IEEE80211_RATE_SUPPORTED) ||
257 !(sta->supp_rates & BIT(nonerp_idx))))
258 nonerp_idx--;
259 extra->nonerp_idx = nonerp_idx;
260 extra->nonerp = &local->curr_rates[extra->nonerp_idx];
261
262 sta_info_release(local, sta);
263
264 return &local->curr_rates[rateidx];
265 }
266
267
268 static void rate_control_simple_rate_init(struct ieee80211_local *local,
269 struct sta_info *sta)
270 {
271 int i;
272 sta->txrate = 0;
273 /* TODO: what is a good starting rate for STA? About middle? Maybe not
274 * the lowest or the highest rate.. Could consider using RSSI from
275 * previous packets? Need to have IEEE 802.1X auth succeed immediately
276 * after assoc.. */
277 for (i = 0; i < local->num_curr_rates; i++) {
278 if ((sta->supp_rates & BIT(i)) &&
279 (local->curr_rates[i].flags & IEEE80211_RATE_SUPPORTED))
280 sta->txrate = i;
281 }
282 }
283
284
285 static void * rate_control_simple_alloc(struct ieee80211_local *local)
286 {
287 struct global_rate_control *rctrl;
288
289 rctrl = kmalloc(sizeof(*rctrl), GFP_ATOMIC);
290 if (rctrl == NULL) {
291 return NULL;
292 }
293 memset(rctrl, 0, sizeof(*rctrl));
294 return rctrl;
295 }
296
297
298 static void rate_control_simple_free(void *priv)
299 {
300 struct global_rate_control *rctrl = priv;
301 kfree(rctrl);
302 }
303
304
305 static void rate_control_simple_clear(void *priv)
306 {
307 }
308
309
310 static void * rate_control_simple_alloc_sta(void)
311 {
312 struct sta_rate_control *rctrl;
313
314 rctrl = kmalloc(sizeof(*rctrl), GFP_ATOMIC);
315 if (rctrl == NULL) {
316 return NULL;
317 }
318 memset(rctrl, 0, sizeof(*rctrl));
319 return rctrl;
320 }
321
322
323 static void rate_control_simple_free_sta(void *priv)
324 {
325 struct sta_rate_control *rctrl = priv;
326 kfree(rctrl);
327 }
328
329
330 static int rate_control_simple_status_sta(struct ieee80211_local *local,
331 struct sta_info *sta, char *buf)
332 {
333 char *p = buf;
334 struct sta_rate_control *srctrl = sta->rate_ctrl_priv;
335
336 p += sprintf(p, "tx_avg_rate_sum=%d\n", srctrl->tx_avg_rate_sum);
337 p += sprintf(p, "tx_avg_rate_num=%d\n", srctrl->tx_avg_rate_num);
338 if (srctrl->tx_avg_rate_num)
339 p += sprintf(p, "tx_avg_rate_avg=%d\n",
340 srctrl->tx_avg_rate_sum /
341 srctrl->tx_avg_rate_num);
342 return p - buf;
343 }
344
345
346 static int rate_control_simple_status_global(struct ieee80211_local *local,
347 char *buf)
348 {
349 return 0;
350 }
351
352
353 static struct rate_control_ops rate_control_simple = {
354 .name = "simple",
355 .tx_status = rate_control_simple_tx_status,
356 .get_rate = rate_control_simple_get_rate,
357 .rate_init = rate_control_simple_rate_init,
358 .clear = rate_control_simple_clear,
359 .status_sta = rate_control_simple_status_sta,
360 .status_global = rate_control_simple_status_global,
361 .alloc = rate_control_simple_alloc,
362 .free = rate_control_simple_free,
363 .alloc_sta = rate_control_simple_alloc_sta,
364 .free_sta = rate_control_simple_free_sta,
365 };
366
367
368 int __init rate_control_simple_init(void)
369 {
370 return ieee80211_rate_control_register(&rate_control_simple);
371 }
372
373
374 static void __exit rate_control_simple_exit(void)
375 {
376 ieee80211_rate_control_unregister(&rate_control_simple);
377 }
378
379
380 module_init(rate_control_simple_init);
381 module_exit(rate_control_simple_exit);
This page took 0.063517 seconds and 5 git commands to generate.