[MAC80211]: Add mac80211 wireless stack.
[powerpc.git] / 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/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/compiler.h>
17
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
21
22
23 /* This is a minimal implementation of TX rate controlling that can be used
24  * as the default when no improved mechanisms are available. */
25
26
27 #define RATE_CONTROL_EMERG_DEC 2
28 #define RATE_CONTROL_INTERVAL (HZ / 20)
29 #define RATE_CONTROL_MIN_TX 10
30
31 MODULE_ALIAS("rc80211_default");
32
33 static void rate_control_rate_inc(struct ieee80211_local *local,
34                                   struct sta_info *sta)
35 {
36         struct ieee80211_sub_if_data *sdata;
37         struct ieee80211_hw_mode *mode;
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         mode = local->oper_hw_mode;
48         maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
49
50         if (i > mode->num_rates)
51                 i = mode->num_rates - 2;
52
53         while (i + 1 < mode->num_rates) {
54                 i++;
55                 if (sta->supp_rates & BIT(i) &&
56                     mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
57                     (maxrate < 0 || i <= maxrate)) {
58                         sta->txrate = 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_hw_mode *mode;
70         int i = sta->txrate;
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         mode = local->oper_hw_mode;
79         if (i > mode->num_rates)
80                 i = mode->num_rates;
81
82         while (i > 0) {
83                 i--;
84                 if (sta->supp_rates & BIT(i) &&
85                     mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
86                         sta->txrate = i;
87                         break;
88                 }
89         }
90 }
91
92
93 static struct ieee80211_rate *
94 rate_control_lowest_rate(struct ieee80211_local *local,
95                          struct ieee80211_hw_mode *mode)
96 {
97         int i;
98
99         for (i = 0; i < mode->num_rates; i++) {
100                 struct ieee80211_rate *rate = &mode->rates[i];
101
102                 if (rate->flags & IEEE80211_RATE_SUPPORTED)
103                         return rate;
104         }
105
106         printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
107                "found\n");
108         return &mode->rates[0];
109 }
110
111
112 struct global_rate_control {
113         int dummy;
114 };
115
116 struct sta_rate_control {
117         unsigned long last_rate_change;
118         u32 tx_num_failures;
119         u32 tx_num_xmit;
120
121         unsigned long avg_rate_update;
122         u32 tx_avg_rate_sum;
123         u32 tx_avg_rate_num;
124 };
125
126
127 static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
128                                           struct sk_buff *skb,
129                                           struct ieee80211_tx_status *status)
130 {
131         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
132         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
133         struct sta_info *sta;
134         struct sta_rate_control *srctrl;
135
136         sta = sta_info_get(local, hdr->addr1);
137
138         if (!sta)
139             return;
140
141         srctrl = sta->rate_ctrl_priv;
142         srctrl->tx_num_xmit++;
143         if (status->excessive_retries) {
144                 sta->antenna_sel_tx = sta->antenna_sel_tx == 1 ? 2 : 1;
145                 sta->antenna_sel_rx = sta->antenna_sel_rx == 1 ? 2 : 1;
146                 if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) {
147                         printk(KERN_DEBUG "%s: " MAC_FMT " TX antenna --> %d "
148                                "RX antenna --> %d (@%lu)\n",
149                                dev->name, MAC_ARG(hdr->addr1),
150                                sta->antenna_sel_tx, sta->antenna_sel_rx, jiffies);
151                 }
152                 srctrl->tx_num_failures++;
153                 sta->tx_retry_failed++;
154                 sta->tx_num_consecutive_failures++;
155                 sta->tx_num_mpdu_fail++;
156         } else {
157                 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
158                 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
159                 sta->last_ack_rssi[2] = status->ack_signal;
160                 sta->tx_num_consecutive_failures = 0;
161                 sta->tx_num_mpdu_ok++;
162         }
163         sta->tx_retry_count += status->retry_count;
164         sta->tx_num_mpdu_fail += status->retry_count;
165
166         if (time_after(jiffies,
167                        srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
168                 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
169                 u32 per_failed;
170                 srctrl->last_rate_change = jiffies;
171
172                 per_failed = (100 * sta->tx_num_mpdu_fail) /
173                         (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
174                 /* TODO: calculate average per_failed to make adjusting
175                  * parameters easier */
176 #if 0
177                 if (net_ratelimit()) {
178                         printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
179                                sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
180                                per_failed);
181                 }
182 #endif
183
184                 if (per_failed > local->rate_ctrl_num_down) {
185                         rate_control_rate_dec(local, sta);
186                 } else if (per_failed < local->rate_ctrl_num_up) {
187                         rate_control_rate_inc(local, sta);
188                 }
189                 srctrl->tx_avg_rate_sum += status->control.rate->rate;
190                 srctrl->tx_avg_rate_num++;
191                 srctrl->tx_num_failures = 0;
192                 srctrl->tx_num_xmit = 0;
193         } else if (sta->tx_num_consecutive_failures >=
194                    RATE_CONTROL_EMERG_DEC) {
195                 rate_control_rate_dec(local, sta);
196         }
197
198         if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
199                 srctrl->avg_rate_update = jiffies;
200                 if (srctrl->tx_avg_rate_num > 0) {
201 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
202                         printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: "
203                                "%d (%d/%d)\n",
204                                dev->name, MAC_ARG(sta->addr),
205                                srctrl->tx_avg_rate_sum /
206                                srctrl->tx_avg_rate_num,
207                                srctrl->tx_avg_rate_sum,
208                                srctrl->tx_avg_rate_num);
209 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
210                         srctrl->tx_avg_rate_sum = 0;
211                         srctrl->tx_avg_rate_num = 0;
212                 }
213         }
214
215         sta_info_put(sta);
216 }
217
218
219 static struct ieee80211_rate *
220 rate_control_simple_get_rate(void *priv, struct net_device *dev,
221                              struct sk_buff *skb,
222                              struct rate_control_extra *extra)
223 {
224         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
225         struct ieee80211_sub_if_data *sdata;
226         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
227         struct ieee80211_hw_mode *mode = extra->mode;
228         struct sta_info *sta;
229         int rateidx, nonerp_idx;
230         u16 fc;
231
232         memset(extra, 0, sizeof(*extra));
233
234         fc = le16_to_cpu(hdr->frame_control);
235         if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
236             (hdr->addr1[0] & 0x01)) {
237                 /* Send management frames and broadcast/multicast data using
238                  * lowest rate. */
239                 /* TODO: this could probably be improved.. */
240                 return rate_control_lowest_rate(local, mode);
241         }
242
243         sta = sta_info_get(local, hdr->addr1);
244
245         if (!sta)
246                 return rate_control_lowest_rate(local, mode);
247
248         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
249         if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
250                 sta->txrate = sdata->bss->force_unicast_rateidx;
251
252         rateidx = sta->txrate;
253
254         if (rateidx >= mode->num_rates)
255                 rateidx = mode->num_rates - 1;
256
257         sta->last_txrate = rateidx;
258         nonerp_idx = rateidx;
259         while (nonerp_idx > 0 &&
260                ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
261                 !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) ||
262                 !(sta->supp_rates & BIT(nonerp_idx))))
263                 nonerp_idx--;
264         extra->nonerp = &mode->rates[nonerp_idx];
265
266         sta_info_put(sta);
267
268         return &mode->rates[rateidx];
269 }
270
271
272 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
273                                           struct ieee80211_local *local,
274                                           struct sta_info *sta)
275 {
276         struct ieee80211_hw_mode *mode;
277         int i;
278         sta->txrate = 0;
279         mode = local->oper_hw_mode;
280         /* TODO: what is a good starting rate for STA? About middle? Maybe not
281          * the lowest or the highest rate.. Could consider using RSSI from
282          * previous packets? Need to have IEEE 802.1X auth succeed immediately
283          * after assoc.. */
284         for (i = 0; i < mode->num_rates; i++) {
285                 if ((sta->supp_rates & BIT(i)) &&
286                     (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
287                         sta->txrate = i;
288         }
289 }
290
291
292 static void * rate_control_simple_alloc(struct ieee80211_local *local)
293 {
294         struct global_rate_control *rctrl;
295
296         rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
297
298         return rctrl;
299 }
300
301
302 static void rate_control_simple_free(void *priv)
303 {
304         struct global_rate_control *rctrl = priv;
305         kfree(rctrl);
306 }
307
308
309 static void rate_control_simple_clear(void *priv)
310 {
311 }
312
313
314 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
315 {
316         struct sta_rate_control *rctrl;
317
318         rctrl = kzalloc(sizeof(*rctrl), gfp);
319
320         return rctrl;
321 }
322
323
324 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
325 {
326         struct sta_rate_control *rctrl = priv_sta;
327         kfree(rctrl);
328 }
329
330
331 static struct rate_control_ops rate_control_simple = {
332         .module = THIS_MODULE,
333         .name = "simple",
334         .tx_status = rate_control_simple_tx_status,
335         .get_rate = rate_control_simple_get_rate,
336         .rate_init = rate_control_simple_rate_init,
337         .clear = rate_control_simple_clear,
338         .alloc = rate_control_simple_alloc,
339         .free = rate_control_simple_free,
340         .alloc_sta = rate_control_simple_alloc_sta,
341         .free_sta = rate_control_simple_free_sta,
342 };
343
344
345 static int __init rate_control_simple_init(void)
346 {
347         return ieee80211_rate_control_register(&rate_control_simple);
348 }
349
350
351 static void __exit rate_control_simple_exit(void)
352 {
353         ieee80211_rate_control_unregister(&rate_control_simple);
354 }
355
356
357 module_init(rate_control_simple_init);
358 module_exit(rate_control_simple_exit);
359
360 MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
361 MODULE_LICENSE("GPL");