Upgrade b43 and mac80211.
[openwrt/staging/lynxis/omap.git] / package / mac80211 / src / net / mac80211 / rc80211_simple.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/init.h>
11 #include <linux/netdevice.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/skbuff.h>
15 #include <linux/compiler.h>
16 #include <linux/module.h>
17
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
21 #include "debugfs.h"
22
23
24 /* This is a minimal implementation of TX rate controlling that can be used
25 * as the default when no improved mechanisms are available. */
26
27 #define RATE_CONTROL_NUM_DOWN 20
28 #define RATE_CONTROL_NUM_UP 15
29
30 #define RATE_CONTROL_EMERG_DEC 2
31 #define RATE_CONTROL_INTERVAL (HZ / 20)
32 #define RATE_CONTROL_MIN_TX 10
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 struct ieee80211_supported_band *sband;
39 int i = sta->txrate_idx;
40 int maxrate;
41
42 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
43 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
44 /* forced unicast rate - do not change STA rate */
45 return;
46 }
47
48 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
49 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
50
51 if (i > sband->n_bitrates)
52 i = sband->n_bitrates - 2;
53
54 while (i + 1 < sband->n_bitrates) {
55 i++;
56 if (rate_supported(sta, sband->band, i) &&
57 (maxrate < 0 || i <= maxrate)) {
58 sta->txrate_idx = i;
59 break;
60 }
61 }
62 }
63
64
65 static void rate_control_rate_dec(struct ieee80211_local *local,
66 struct sta_info *sta)
67 {
68 struct ieee80211_sub_if_data *sdata;
69 struct ieee80211_supported_band *sband;
70 int i = sta->txrate_idx;
71
72 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
73 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
74 /* forced unicast rate - do not change STA rate */
75 return;
76 }
77
78 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
79 if (i > sband->n_bitrates)
80 i = sband->n_bitrates;
81
82 while (i > 0) {
83 i--;
84 if (rate_supported(sta, sband->band, i)) {
85 sta->txrate_idx = i;
86 break;
87 }
88 }
89 }
90
91 struct global_rate_control {
92 int dummy;
93 };
94
95 struct sta_rate_control {
96 unsigned long last_rate_change;
97 u32 tx_num_failures;
98 u32 tx_num_xmit;
99
100 unsigned long avg_rate_update;
101 u32 tx_avg_rate_sum;
102 u32 tx_avg_rate_num;
103
104 #ifdef CONFIG_MAC80211_DEBUGFS
105 struct dentry *tx_avg_rate_sum_dentry;
106 struct dentry *tx_avg_rate_num_dentry;
107 #endif
108 };
109
110
111 static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
112 struct sk_buff *skb,
113 struct ieee80211_tx_status *status)
114 {
115 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
116 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
117 struct sta_info *sta;
118 struct sta_rate_control *srctrl;
119
120 sta = sta_info_get(local, hdr->addr1);
121
122 if (!sta)
123 return;
124
125 srctrl = sta->rate_ctrl_priv;
126 srctrl->tx_num_xmit++;
127 if (status->excessive_retries) {
128 srctrl->tx_num_failures++;
129 sta->tx_retry_failed++;
130 sta->tx_num_consecutive_failures++;
131 sta->tx_num_mpdu_fail++;
132 } else {
133 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
134 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
135 sta->last_ack_rssi[2] = status->ack_signal;
136 sta->tx_num_consecutive_failures = 0;
137 sta->tx_num_mpdu_ok++;
138 }
139 sta->tx_retry_count += status->retry_count;
140 sta->tx_num_mpdu_fail += status->retry_count;
141
142 if (time_after(jiffies,
143 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
144 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
145 u32 per_failed;
146 srctrl->last_rate_change = jiffies;
147
148 per_failed = (100 * sta->tx_num_mpdu_fail) /
149 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
150 /* TODO: calculate average per_failed to make adjusting
151 * parameters easier */
152 #if 0
153 if (net_ratelimit()) {
154 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
155 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
156 per_failed);
157 }
158 #endif
159
160 /*
161 * XXX: Make these configurable once we have an
162 * interface to the rate control algorithms
163 */
164 if (per_failed > RATE_CONTROL_NUM_DOWN) {
165 rate_control_rate_dec(local, sta);
166 } else if (per_failed < RATE_CONTROL_NUM_UP) {
167 rate_control_rate_inc(local, sta);
168 }
169 srctrl->tx_avg_rate_sum += status->control.tx_rate->bitrate;
170 srctrl->tx_avg_rate_num++;
171 srctrl->tx_num_failures = 0;
172 srctrl->tx_num_xmit = 0;
173 } else if (sta->tx_num_consecutive_failures >=
174 RATE_CONTROL_EMERG_DEC) {
175 rate_control_rate_dec(local, sta);
176 }
177
178 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
179 srctrl->avg_rate_update = jiffies;
180 if (srctrl->tx_avg_rate_num > 0) {
181 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
182 DECLARE_MAC_BUF(mac);
183 printk(KERN_DEBUG "%s: STA %s Average rate: "
184 "%d (%d/%d)\n",
185 dev->name, print_mac(mac, sta->addr),
186 srctrl->tx_avg_rate_sum /
187 srctrl->tx_avg_rate_num,
188 srctrl->tx_avg_rate_sum,
189 srctrl->tx_avg_rate_num);
190 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
191 srctrl->tx_avg_rate_sum = 0;
192 srctrl->tx_avg_rate_num = 0;
193 }
194 }
195
196 sta_info_put(sta);
197 }
198
199
200 static void
201 rate_control_simple_get_rate(void *priv, struct net_device *dev,
202 struct ieee80211_supported_band *sband,
203 struct sk_buff *skb,
204 struct rate_selection *sel)
205 {
206 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
207 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
208 struct ieee80211_sub_if_data *sdata;
209 struct sta_info *sta;
210 int rateidx;
211 u16 fc;
212
213 sta = sta_info_get(local, hdr->addr1);
214
215 /* Send management frames and broadcast/multicast data using lowest
216 * rate. */
217 fc = le16_to_cpu(hdr->frame_control);
218 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
219 is_multicast_ether_addr(hdr->addr1) || !sta) {
220 sel->rate = rate_lowest(local, sband, sta);
221 if (sta)
222 sta_info_put(sta);
223 return;
224 }
225
226 /* If a forced rate is in effect, select it. */
227 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
228 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
229 sta->txrate_idx = sdata->bss->force_unicast_rateidx;
230
231 rateidx = sta->txrate_idx;
232
233 if (rateidx >= sband->n_bitrates)
234 rateidx = sband->n_bitrates - 1;
235
236 sta->last_txrate_idx = rateidx;
237
238 sta_info_put(sta);
239
240 sel->rate = &sband->bitrates[rateidx];
241 }
242
243
244 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
245 struct ieee80211_local *local,
246 struct sta_info *sta)
247 {
248 struct ieee80211_supported_band *sband;
249
250 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
251
252 /* TODO: This routine should consider using RSSI from previous packets
253 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
254 * Until that method is implemented, we will use the lowest supported rate
255 * as a workaround, */
256 sta->txrate_idx = rate_lowest_index(local, sband, sta);
257 }
258
259
260 static void * rate_control_simple_alloc(struct ieee80211_local *local)
261 {
262 struct global_rate_control *rctrl;
263
264 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
265
266 return rctrl;
267 }
268
269
270 static void rate_control_simple_free(void *priv)
271 {
272 struct global_rate_control *rctrl = priv;
273 kfree(rctrl);
274 }
275
276
277 static void rate_control_simple_clear(void *priv)
278 {
279 }
280
281
282 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
283 {
284 struct sta_rate_control *rctrl;
285
286 rctrl = kzalloc(sizeof(*rctrl), gfp);
287
288 return rctrl;
289 }
290
291
292 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
293 {
294 struct sta_rate_control *rctrl = priv_sta;
295 kfree(rctrl);
296 }
297
298 #ifdef CONFIG_MAC80211_DEBUGFS
299
300 static int open_file_generic(struct inode *inode, struct file *file)
301 {
302 file->private_data = inode->i_private;
303 return 0;
304 }
305
306 static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
307 char __user *userbuf,
308 size_t count, loff_t *ppos)
309 {
310 struct sta_rate_control *srctrl = file->private_data;
311 char buf[20];
312
313 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
314 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
315 }
316
317 static const struct file_operations sta_tx_avg_rate_sum_ops = {
318 .read = sta_tx_avg_rate_sum_read,
319 .open = open_file_generic,
320 };
321
322 static ssize_t sta_tx_avg_rate_num_read(struct file *file,
323 char __user *userbuf,
324 size_t count, loff_t *ppos)
325 {
326 struct sta_rate_control *srctrl = file->private_data;
327 char buf[20];
328
329 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
330 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
331 }
332
333 static const struct file_operations sta_tx_avg_rate_num_ops = {
334 .read = sta_tx_avg_rate_num_read,
335 .open = open_file_generic,
336 };
337
338 static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
339 struct dentry *dir)
340 {
341 struct sta_rate_control *srctrl = priv_sta;
342
343 srctrl->tx_avg_rate_num_dentry =
344 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
345 dir, srctrl, &sta_tx_avg_rate_num_ops);
346 srctrl->tx_avg_rate_sum_dentry =
347 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
348 dir, srctrl, &sta_tx_avg_rate_sum_ops);
349 }
350
351 static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
352 {
353 struct sta_rate_control *srctrl = priv_sta;
354
355 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
356 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
357 }
358 #endif
359
360 static struct rate_control_ops mac80211_rcsimple = {
361 .name = "simple",
362 .tx_status = rate_control_simple_tx_status,
363 .get_rate = rate_control_simple_get_rate,
364 .rate_init = rate_control_simple_rate_init,
365 .clear = rate_control_simple_clear,
366 .alloc = rate_control_simple_alloc,
367 .free = rate_control_simple_free,
368 .alloc_sta = rate_control_simple_alloc_sta,
369 .free_sta = rate_control_simple_free_sta,
370 #ifdef CONFIG_MAC80211_DEBUGFS
371 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
372 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
373 #endif
374 };
375
376 MODULE_LICENSE("GPL");
377 MODULE_DESCRIPTION("Simple rate control algorithm");
378
379 int __init rc80211_simple_init(void)
380 {
381 return ieee80211_rate_control_register(&mac80211_rcsimple);
382 }
383
384 void rc80211_simple_exit(void)
385 {
386 ieee80211_rate_control_unregister(&mac80211_rcsimple);
387 }
388
389 #ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
390 module_init(rc80211_simple_init);
391 module_exit(rc80211_simple_exit);
392 #endif