17b6bb034ebfefbe6f25181c03167fa9f4952437
[safe/jmp/linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00mac.c
1 /*
2         Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
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 as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2x00mac
23         Abstract: rt2x00 generic mac80211 routines.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31
32 static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
33                                 struct data_queue *queue,
34                                 struct sk_buff *frag_skb,
35                                 struct ieee80211_tx_control *control)
36 {
37         struct skb_frame_desc *skbdesc;
38         struct sk_buff *skb;
39         int size;
40
41         if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
42                 size = sizeof(struct ieee80211_cts);
43         else
44                 size = sizeof(struct ieee80211_rts);
45
46         skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
47         if (!skb) {
48                 WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
49                 return NETDEV_TX_BUSY;
50         }
51
52         skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
53         skb_put(skb, size);
54
55         if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
56                 ieee80211_ctstoself_get(rt2x00dev->hw, control->vif,
57                                         frag_skb->data, frag_skb->len, control,
58                                         (struct ieee80211_cts *)(skb->data));
59         else
60                 ieee80211_rts_get(rt2x00dev->hw, control->vif,
61                                   frag_skb->data, frag_skb->len, control,
62                                   (struct ieee80211_rts *)(skb->data));
63
64         /*
65          * Initialize skb descriptor
66          */
67         skbdesc = get_skb_frame_desc(skb);
68         memset(skbdesc, 0, sizeof(*skbdesc));
69         skbdesc->flags |= FRAME_DESC_DRIVER_GENERATED;
70
71         if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, queue, skb, control)) {
72                 WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
73                 return NETDEV_TX_BUSY;
74         }
75
76         return NETDEV_TX_OK;
77 }
78
79 int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
80                  struct ieee80211_tx_control *control)
81 {
82         struct rt2x00_dev *rt2x00dev = hw->priv;
83         struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
84         struct data_queue *queue;
85         struct skb_frame_desc *skbdesc;
86         u16 frame_control;
87
88         /*
89          * Mac80211 might be calling this function while we are trying
90          * to remove the device or perhaps suspending it.
91          * Note that we can only stop the TX queues inside the TX path
92          * due to possible race conditions in mac80211.
93          */
94         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
95                 ieee80211_stop_queues(hw);
96                 return NETDEV_TX_OK;
97         }
98
99         /*
100          * Determine which queue to put packet on.
101          */
102         if (control->flags & IEEE80211_TXCTL_SEND_AFTER_DTIM &&
103             test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
104                 queue = rt2x00queue_get_queue(rt2x00dev, RT2X00_BCN_QUEUE_ATIM);
105         else
106                 queue = rt2x00queue_get_queue(rt2x00dev, control->queue);
107         if (unlikely(!queue)) {
108                 ERROR(rt2x00dev,
109                       "Attempt to send packet over invalid queue %d.\n"
110                       "Please file bug report to %s.\n",
111                       control->queue, DRV_PROJECT);
112                 dev_kfree_skb_any(skb);
113                 return NETDEV_TX_OK;
114         }
115
116         /*
117          * If CTS/RTS is required. and this frame is not CTS or RTS,
118          * create and queue that frame first. But make sure we have
119          * at least enough entries available to send this CTS/RTS
120          * frame as well as the data frame.
121          */
122         frame_control = le16_to_cpu(ieee80211hdr->frame_control);
123         if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
124             (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
125                                IEEE80211_TXCTL_USE_CTS_PROTECT))) {
126                 if (rt2x00queue_available(queue) <= 1) {
127                         ieee80211_stop_queue(rt2x00dev->hw, control->queue);
128                         return NETDEV_TX_BUSY;
129                 }
130
131                 if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb, control)) {
132                         ieee80211_stop_queue(rt2x00dev->hw, control->queue);
133                         return NETDEV_TX_BUSY;
134                 }
135         }
136
137         /*
138          * Initialize skb descriptor
139          */
140         skbdesc = get_skb_frame_desc(skb);
141         memset(skbdesc, 0, sizeof(*skbdesc));
142
143         if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, queue, skb, control)) {
144                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
145                 return NETDEV_TX_BUSY;
146         }
147
148         if (rt2x00queue_full(queue))
149                 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
150
151         if (rt2x00dev->ops->lib->kick_tx_queue)
152                 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
153
154         return NETDEV_TX_OK;
155 }
156 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
157
158 int rt2x00mac_start(struct ieee80211_hw *hw)
159 {
160         struct rt2x00_dev *rt2x00dev = hw->priv;
161
162         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
163                 return 0;
164
165         return rt2x00lib_start(rt2x00dev);
166 }
167 EXPORT_SYMBOL_GPL(rt2x00mac_start);
168
169 void rt2x00mac_stop(struct ieee80211_hw *hw)
170 {
171         struct rt2x00_dev *rt2x00dev = hw->priv;
172
173         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
174                 return;
175
176         rt2x00lib_stop(rt2x00dev);
177 }
178 EXPORT_SYMBOL_GPL(rt2x00mac_stop);
179
180 int rt2x00mac_add_interface(struct ieee80211_hw *hw,
181                             struct ieee80211_if_init_conf *conf)
182 {
183         struct rt2x00_dev *rt2x00dev = hw->priv;
184         struct rt2x00_intf *intf = vif_to_intf(conf->vif);
185         struct data_queue *queue =
186             rt2x00queue_get_queue(rt2x00dev, RT2X00_BCN_QUEUE_BEACON);
187         struct queue_entry *entry = NULL;
188         unsigned int i;
189
190         /*
191          * Don't allow interfaces to be added
192          * the device has disappeared.
193          */
194         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
195             !test_bit(DEVICE_STARTED, &rt2x00dev->flags))
196                 return -ENODEV;
197
198         /*
199          * When we don't support mixed interfaces (a combination
200          * of sta and ap virtual interfaces) then we can only
201          * add this interface when the rival interface count is 0.
202          */
203         if (!test_bit(DRIVER_SUPPORT_MIXED_INTERFACES, &rt2x00dev->flags) &&
204             ((conf->type == IEEE80211_IF_TYPE_AP && rt2x00dev->intf_sta_count) ||
205              (conf->type != IEEE80211_IF_TYPE_AP && rt2x00dev->intf_ap_count)))
206                 return -ENOBUFS;
207
208         /*
209          * Check if we exceeded the maximum amount of supported interfaces.
210          */
211         if ((conf->type == IEEE80211_IF_TYPE_AP &&
212              rt2x00dev->intf_ap_count >= rt2x00dev->ops->max_ap_intf) ||
213             (conf->type != IEEE80211_IF_TYPE_AP &&
214              rt2x00dev->intf_sta_count >= rt2x00dev->ops->max_sta_intf))
215                 return -ENOBUFS;
216
217         /*
218          * Loop through all beacon queues to find a free
219          * entry. Since there are as much beacon entries
220          * as the maximum interfaces, this search shouldn't
221          * fail.
222          */
223         for (i = 0; i < queue->limit; i++) {
224                 entry = &queue->entries[i];
225                 if (!__test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
226                         break;
227         }
228
229         if (unlikely(i == queue->limit))
230                 return -ENOBUFS;
231
232         /*
233          * We are now absolutely sure the interface can be created,
234          * increase interface count and start initialization.
235          */
236
237         if (conf->type == IEEE80211_IF_TYPE_AP)
238                 rt2x00dev->intf_ap_count++;
239         else
240                 rt2x00dev->intf_sta_count++;
241
242         spin_lock_init(&intf->lock);
243         intf->beacon = entry;
244
245         if (conf->type == IEEE80211_IF_TYPE_AP)
246                 memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
247         memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
248
249         /*
250          * The MAC adddress must be configured after the device
251          * has been initialized. Otherwise the device can reset
252          * the MAC registers.
253          */
254         rt2x00lib_config_intf(rt2x00dev, intf, conf->type, intf->mac, NULL);
255
256         return 0;
257 }
258 EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
259
260 void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
261                                 struct ieee80211_if_init_conf *conf)
262 {
263         struct rt2x00_dev *rt2x00dev = hw->priv;
264         struct rt2x00_intf *intf = vif_to_intf(conf->vif);
265
266         /*
267          * Don't allow interfaces to be remove while
268          * either the device has disappeared or when
269          * no interface is present.
270          */
271         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
272             (conf->type == IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_ap_count) ||
273             (conf->type != IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_sta_count))
274                 return;
275
276         if (conf->type == IEEE80211_IF_TYPE_AP)
277                 rt2x00dev->intf_ap_count--;
278         else
279                 rt2x00dev->intf_sta_count--;
280
281         /*
282          * Release beacon entry so it is available for
283          * new interfaces again.
284          */
285         __clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
286
287         /*
288          * Make sure the bssid and mac address registers
289          * are cleared to prevent false ACKing of frames.
290          */
291         rt2x00lib_config_intf(rt2x00dev, intf,
292                               IEEE80211_IF_TYPE_INVALID, NULL, NULL);
293 }
294 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
295
296 int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
297 {
298         struct rt2x00_dev *rt2x00dev = hw->priv;
299
300         /*
301          * Mac80211 might be calling this function while we are trying
302          * to remove the device or perhaps suspending it.
303          */
304         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
305                 return 0;
306
307         /*
308          * Check if we need to disable the radio,
309          * if this is not the case, at least the RX must be disabled.
310          */
311         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
312                 if (!conf->radio_enabled)
313                         rt2x00lib_disable_radio(rt2x00dev);
314                 else
315                         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
316         }
317
318         rt2x00lib_config(rt2x00dev, conf, 0);
319
320         /*
321          * Reenable RX only if the radio should be on.
322          */
323         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
324                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
325         else if (conf->radio_enabled)
326                 return rt2x00lib_enable_radio(rt2x00dev);
327
328         return 0;
329 }
330 EXPORT_SYMBOL_GPL(rt2x00mac_config);
331
332 int rt2x00mac_config_interface(struct ieee80211_hw *hw,
333                                struct ieee80211_vif *vif,
334                                struct ieee80211_if_conf *conf)
335 {
336         struct rt2x00_dev *rt2x00dev = hw->priv;
337         struct rt2x00_intf *intf = vif_to_intf(vif);
338         int status;
339
340         /*
341          * Mac80211 might be calling this function while we are trying
342          * to remove the device or perhaps suspending it.
343          */
344         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
345                 return 0;
346
347         spin_lock(&intf->lock);
348
349         /*
350          * If the interface does not work in master mode,
351          * then the bssid value in the interface structure
352          * should now be set.
353          */
354         if (conf->type != IEEE80211_IF_TYPE_AP)
355                 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
356
357         spin_unlock(&intf->lock);
358
359         /*
360          * Call rt2x00_config_intf() outside of the spinlock context since
361          * the call will sleep for USB drivers. By using the ieee80211_if_conf
362          * values as arguments we make keep access to rt2x00_intf thread safe
363          * even without the lock.
364          */
365         rt2x00lib_config_intf(rt2x00dev, intf, conf->type, NULL, conf->bssid);
366
367         /*
368          * We only need to initialize the beacon when master mode is enabled.
369          */
370         if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
371                 return 0;
372
373         status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
374                                                    conf->beacon,
375                                                    conf->beacon_control);
376         if (status)
377                 dev_kfree_skb(conf->beacon);
378
379         return status;
380 }
381 EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
382
383 void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
384                                 unsigned int changed_flags,
385                                 unsigned int *total_flags,
386                                 int mc_count, struct dev_addr_list *mc_list)
387 {
388         struct rt2x00_dev *rt2x00dev = hw->priv;
389
390         /*
391          * Mask off any flags we are going to ignore
392          * from the total_flags field.
393          */
394         *total_flags &=
395             FIF_ALLMULTI |
396             FIF_FCSFAIL |
397             FIF_PLCPFAIL |
398             FIF_CONTROL |
399             FIF_OTHER_BSS |
400             FIF_PROMISC_IN_BSS;
401
402         /*
403          * Apply some rules to the filters:
404          * - Some filters imply different filters to be set.
405          * - Some things we can't filter out at all.
406          * - Multicast filter seems to kill broadcast traffic so never use it.
407          */
408         *total_flags |= FIF_ALLMULTI;
409         if (*total_flags & FIF_OTHER_BSS ||
410             *total_flags & FIF_PROMISC_IN_BSS)
411                 *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
412
413         /*
414          * Check if there is any work left for us.
415          */
416         if (rt2x00dev->packet_filter == *total_flags)
417                 return;
418         rt2x00dev->packet_filter = *total_flags;
419
420         if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
421                 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->filter_work);
422         else
423                 rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
424 }
425 EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
426
427 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
428                         struct ieee80211_low_level_stats *stats)
429 {
430         struct rt2x00_dev *rt2x00dev = hw->priv;
431
432         /*
433          * The dot11ACKFailureCount, dot11RTSFailureCount and
434          * dot11RTSSuccessCount are updated in interrupt time.
435          * dot11FCSErrorCount is updated in the link tuner.
436          */
437         memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
438
439         return 0;
440 }
441 EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
442
443 int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
444                            struct ieee80211_tx_queue_stats *stats)
445 {
446         struct rt2x00_dev *rt2x00dev = hw->priv;
447         unsigned int i;
448
449         for (i = 0; i < hw->queues; i++) {
450                 stats->data[i].len = rt2x00dev->tx[i].length;
451                 stats->data[i].limit = rt2x00dev->tx[i].limit;
452                 stats->data[i].count = rt2x00dev->tx[i].count;
453         }
454
455         return 0;
456 }
457 EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
458
459 void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
460                                 struct ieee80211_vif *vif,
461                                 struct ieee80211_bss_conf *bss_conf,
462                                 u32 changes)
463 {
464         struct rt2x00_dev *rt2x00dev = hw->priv;
465         struct rt2x00_intf *intf = vif_to_intf(vif);
466         unsigned int delayed = 0;
467
468         /*
469          * When the association status has changed we must reset the link
470          * tuner counter. This is because some drivers determine if they
471          * should perform link tuning based on the number of seconds
472          * while associated or not associated.
473          */
474         if (changes & BSS_CHANGED_ASSOC) {
475                 rt2x00dev->link.count = 0;
476
477                 if (bss_conf->assoc)
478                         rt2x00dev->intf_associated++;
479                 else
480                         rt2x00dev->intf_associated--;
481         }
482
483         /*
484          * When the erp information has changed, we should perform
485          * additional configuration steps. For all other changes we are done.
486          */
487         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
488                 if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
489                         rt2x00lib_config_erp(rt2x00dev, intf, bss_conf);
490                 else
491                         delayed |= DELAYED_CONFIG_ERP;
492         }
493
494         spin_lock(&intf->lock);
495         memcpy(&intf->conf, bss_conf, sizeof(*bss_conf));
496         if (delayed) {
497                 intf->delayed_flags |= delayed;
498                 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
499         }
500         spin_unlock(&intf->lock);
501 }
502 EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
503
504 int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue_idx,
505                       const struct ieee80211_tx_queue_params *params)
506 {
507         struct rt2x00_dev *rt2x00dev = hw->priv;
508         struct data_queue *queue;
509
510         queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
511         if (unlikely(!queue))
512                 return -EINVAL;
513
514         /*
515          * The passed variables are stored as real value ((2^n)-1).
516          * Ralink registers require to know the bit number 'n'.
517          */
518         if (params->cw_min > 0)
519                 queue->cw_min = fls(params->cw_min);
520         else
521                 queue->cw_min = 5; /* cw_min: 2^5 = 32. */
522
523         if (params->cw_max > 0)
524                 queue->cw_max = fls(params->cw_max);
525         else
526                 queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
527
528         if (params->aifs >= 0)
529                 queue->aifs = params->aifs;
530         else
531                 queue->aifs = 2;
532
533         INFO(rt2x00dev,
534              "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
535              queue_idx, queue->cw_min, queue->cw_max, queue->aifs);
536
537         return 0;
538 }
539 EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);