rt2x00: Store queue idx and entry idx in data_ring and data_entry
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00mac.c
CommitLineData
95ea3627
ID
1/*
2 Copyright (C) 2004 - 2007 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
95ea3627
ID
26#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
32static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
33 struct data_ring *ring,
34 struct sk_buff *frag_skb,
35 struct ieee80211_tx_control *control)
36{
37 struct sk_buff *skb;
38 int size;
39
40 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
41 size = sizeof(struct ieee80211_cts);
42 else
43 size = sizeof(struct ieee80211_rts);
44
45 skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
46 if (!skb) {
47 WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
48 return NETDEV_TX_BUSY;
49 }
50
51 skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
52 skb_put(skb, size);
53
54 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
55 ieee80211_ctstoself_get(rt2x00dev->hw, rt2x00dev->interface.id,
56 frag_skb->data, frag_skb->len, control,
57 (struct ieee80211_cts *)(skb->data));
58 else
59 ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
60 frag_skb->data, frag_skb->len, control,
61 (struct ieee80211_rts *)(skb->data));
62
63 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
64 WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
65 return NETDEV_TX_BUSY;
66 }
67
68 return NETDEV_TX_OK;
69}
70
71int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
72 struct ieee80211_tx_control *control)
73{
74 struct rt2x00_dev *rt2x00dev = hw->priv;
75 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
76 struct data_ring *ring;
77 u16 frame_control;
78
066cb637
ID
79 /*
80 * Mac80211 might be calling this function while we are trying
81 * to remove the device or perhaps suspending it.
82 * Note that we can only stop the TX queues inside the TX path
83 * due to possible race conditions in mac80211.
84 */
85 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
86 ieee80211_stop_queues(hw);
1230cb83 87 return NETDEV_TX_OK;
066cb637
ID
88 }
89
95ea3627
ID
90 /*
91 * Determine which ring to put packet on.
92 */
93 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
94 if (unlikely(!ring)) {
95 ERROR(rt2x00dev,
96 "Attempt to send packet over invalid queue %d.\n"
97 "Please file bug report to %s.\n",
98 control->queue, DRV_PROJECT);
99 dev_kfree_skb_any(skb);
100 return NETDEV_TX_OK;
101 }
102
103 /*
104 * If CTS/RTS is required. and this frame is not CTS or RTS,
105 * create and queue that frame first. But make sure we have
106 * at least enough entries available to send this CTS/RTS
107 * frame as well as the data frame.
108 */
109 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
110 if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
111 (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
112 IEEE80211_TXCTL_USE_CTS_PROTECT))) {
1230cb83
ID
113 if (rt2x00_ring_free(ring) <= 1) {
114 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
95ea3627 115 return NETDEV_TX_BUSY;
1230cb83 116 }
95ea3627 117
1230cb83
ID
118 if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control)) {
119 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
95ea3627 120 return NETDEV_TX_BUSY;
1230cb83 121 }
95ea3627
ID
122 }
123
1230cb83
ID
124 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
125 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
95ea3627 126 return NETDEV_TX_BUSY;
1230cb83
ID
127 }
128
129 if (rt2x00_ring_full(ring))
130 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
95ea3627
ID
131
132 if (rt2x00dev->ops->lib->kick_tx_queue)
133 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
134
135 return NETDEV_TX_OK;
136}
137EXPORT_SYMBOL_GPL(rt2x00mac_tx);
138
139int rt2x00mac_start(struct ieee80211_hw *hw)
140{
141 struct rt2x00_dev *rt2x00dev = hw->priv;
142 int status;
143
066cb637
ID
144 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
145 test_bit(DEVICE_STARTED, &rt2x00dev->flags))
95ea3627
ID
146 return 0;
147
148 /*
149 * If this is the first interface which is added,
150 * we should load the firmware now.
151 */
066cb637 152 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
95ea3627
ID
153 status = rt2x00lib_load_firmware(rt2x00dev);
154 if (status)
155 return status;
156 }
157
158 /*
159 * Initialize the device.
160 */
161 status = rt2x00lib_initialize(rt2x00dev);
162 if (status)
163 return status;
164
165 /*
166 * Enable radio.
167 */
168 status = rt2x00lib_enable_radio(rt2x00dev);
169 if (status) {
170 rt2x00lib_uninitialize(rt2x00dev);
171 return status;
172 }
173
066cb637
ID
174 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
175
95ea3627
ID
176 return 0;
177}
178EXPORT_SYMBOL_GPL(rt2x00mac_start);
179
180void rt2x00mac_stop(struct ieee80211_hw *hw)
181{
182 struct rt2x00_dev *rt2x00dev = hw->priv;
183
066cb637
ID
184 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
185 return;
186
95ea3627
ID
187 /*
188 * Perhaps we can add something smarter here,
189 * but for now just disabling the radio should do.
190 */
191 rt2x00lib_disable_radio(rt2x00dev);
066cb637
ID
192
193 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
95ea3627
ID
194}
195EXPORT_SYMBOL_GPL(rt2x00mac_stop);
196
197int rt2x00mac_add_interface(struct ieee80211_hw *hw,
198 struct ieee80211_if_init_conf *conf)
199{
200 struct rt2x00_dev *rt2x00dev = hw->priv;
201 struct interface *intf = &rt2x00dev->interface;
95ea3627 202
453a3fb9
ID
203 /* FIXME: Beaconing is broken in rt2x00. */
204 if (conf->type == IEEE80211_IF_TYPE_IBSS ||
205 conf->type == IEEE80211_IF_TYPE_AP) {
206 ERROR(rt2x00dev,
207 "rt2x00 does not support Adhoc or Master mode");
208 return -EOPNOTSUPP;
209 }
210
95ea3627 211 /*
066cb637
ID
212 * Don't allow interfaces to be added while
213 * either the device has disappeared or when
214 * another interface is already present.
95ea3627 215 */
066cb637
ID
216 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
217 is_interface_present(intf))
95ea3627
ID
218 return -ENOBUFS;
219
4150c572
JB
220 intf->id = conf->if_id;
221 intf->type = conf->type;
222 if (conf->type == IEEE80211_IF_TYPE_AP)
223 memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
224 memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
95ea3627
ID
225
226 /*
95ea3627 227 * The MAC adddress must be configured after the device
4150c572
JB
228 * has been initialized. Otherwise the device can reset
229 * the MAC registers.
95ea3627
ID
230 */
231 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
232 rt2x00lib_config_type(rt2x00dev, conf->type);
95ea3627
ID
233
234 return 0;
235}
236EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
237
238void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
239 struct ieee80211_if_init_conf *conf)
240{
241 struct rt2x00_dev *rt2x00dev = hw->priv;
242 struct interface *intf = &rt2x00dev->interface;
243
244 /*
066cb637
ID
245 * Don't allow interfaces to be remove while
246 * either the device has disappeared or when
247 * no interface is present.
95ea3627 248 */
066cb637
ID
249 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
250 !is_interface_present(intf))
95ea3627
ID
251 return;
252
4150c572 253 intf->id = 0;
d28c2561 254 intf->type = IEEE80211_IF_TYPE_INVALID;
4150c572
JB
255 memset(&intf->bssid, 0x00, ETH_ALEN);
256 memset(&intf->mac, 0x00, ETH_ALEN);
95ea3627
ID
257
258 /*
259 * Make sure the bssid and mac address registers
260 * are cleared to prevent false ACKing of frames.
261 */
262 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
263 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
264 rt2x00lib_config_type(rt2x00dev, intf->type);
95ea3627
ID
265}
266EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
267
268int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
269{
270 struct rt2x00_dev *rt2x00dev = hw->priv;
271
272 /*
066cb637
ID
273 * Mac80211 might be calling this function while we are trying
274 * to remove the device or perhaps suspending it.
95ea3627 275 */
066cb637 276 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
95ea3627
ID
277 return 0;
278
279 /*
280 * Check if we need to disable the radio,
281 * if this is not the case, at least the RX must be disabled.
282 */
283 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
284 if (!conf->radio_enabled)
285 rt2x00lib_disable_radio(rt2x00dev);
286 else
5cbf830e 287 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
95ea3627
ID
288 }
289
066cb637 290 rt2x00lib_config(rt2x00dev, conf, 0);
95ea3627 291
95ea3627
ID
292 /*
293 * Reenable RX only if the radio should be on.
294 */
295 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
5cbf830e 296 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
95ea3627
ID
297 else if (conf->radio_enabled)
298 return rt2x00lib_enable_radio(rt2x00dev);
299
300 return 0;
301}
302EXPORT_SYMBOL_GPL(rt2x00mac_config);
303
304int rt2x00mac_config_interface(struct ieee80211_hw *hw, int if_id,
305 struct ieee80211_if_conf *conf)
306{
307 struct rt2x00_dev *rt2x00dev = hw->priv;
308 struct interface *intf = &rt2x00dev->interface;
309 int status;
310
311 /*
066cb637
ID
312 * Mac80211 might be calling this function while we are trying
313 * to remove the device or perhaps suspending it.
95ea3627 314 */
066cb637 315 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
95ea3627
ID
316 return 0;
317
318 /*
95ea3627
ID
319 * If the given type does not match the configured type,
320 * there has been a problem.
321 */
4150c572 322 if (conf->type != intf->type)
95ea3627
ID
323 return -EINVAL;
324
325 /*
326 * If the interface does not work in master mode,
327 * then the bssid value in the interface structure
328 * should now be set.
329 */
330 if (conf->type != IEEE80211_IF_TYPE_AP)
331 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
332 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
333
334 /*
335 * We only need to initialize the beacon when master mode is enabled.
336 */
337 if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
338 return 0;
339
340 status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
341 conf->beacon,
342 conf->beacon_control);
343 if (status)
344 dev_kfree_skb(conf->beacon);
345
346 return status;
347}
348EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
349
95ea3627
ID
350int rt2x00mac_get_stats(struct ieee80211_hw *hw,
351 struct ieee80211_low_level_stats *stats)
352{
353 struct rt2x00_dev *rt2x00dev = hw->priv;
354
355 /*
356 * The dot11ACKFailureCount, dot11RTSFailureCount and
357 * dot11RTSSuccessCount are updated in interrupt time.
358 * dot11FCSErrorCount is updated in the link tuner.
359 */
360 memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
361
362 return 0;
363}
364EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
365
366int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
367 struct ieee80211_tx_queue_stats *stats)
368{
369 struct rt2x00_dev *rt2x00dev = hw->priv;
370 unsigned int i;
371
372 for (i = 0; i < hw->queues; i++)
373 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
374 sizeof(rt2x00dev->tx[i].stats));
375
376 return 0;
377}
378EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
379
5c58ee51
ID
380void rt2x00mac_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
381 int cts_protection, int preamble)
382{
383 struct rt2x00_dev *rt2x00dev = hw->priv;
384 int short_preamble;
385 int ack_timeout;
386 int ack_consume_time;
387 int difs;
388
389 /*
390 * We only support changing preamble mode.
391 */
392 if (!(changes & IEEE80211_ERP_CHANGE_PREAMBLE))
393 return;
394
395 short_preamble = !preamble;
396 preamble = !!(preamble) ? PREAMBLE : SHORT_PREAMBLE;
397
398 difs = (hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) ?
399 SHORT_DIFS : DIFS;
400 ack_timeout = difs + PLCP + preamble + get_duration(ACK_SIZE, 10);
401
402 ack_consume_time = SIFS + PLCP + preamble + get_duration(ACK_SIZE, 10);
403
404 if (short_preamble)
405 __set_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
406 else
407 __clear_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
408
409 rt2x00dev->ops->lib->config_preamble(rt2x00dev, short_preamble,
410 ack_timeout, ack_consume_time);
411}
412EXPORT_SYMBOL_GPL(rt2x00mac_erp_ie_changed);
413
95ea3627
ID
414int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue,
415 const struct ieee80211_tx_queue_params *params)
416{
417 struct rt2x00_dev *rt2x00dev = hw->priv;
418 struct data_ring *ring;
419
420 ring = rt2x00lib_get_ring(rt2x00dev, queue);
421 if (unlikely(!ring))
422 return -EINVAL;
423
424 /*
425 * The passed variables are stored as real value ((2^n)-1).
426 * Ralink registers require to know the bit number 'n'.
427 */
428 if (params->cw_min)
429 ring->tx_params.cw_min = fls(params->cw_min);
430 else
431 ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
432
433 if (params->cw_max)
434 ring->tx_params.cw_max = fls(params->cw_max);
435 else
436 ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
437
438 if (params->aifs)
439 ring->tx_params.aifs = params->aifs;
440 else
441 ring->tx_params.aifs = 2;
442
443 INFO(rt2x00dev,
444 "Configured TX ring %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
445 queue, ring->tx_params.cw_min, ring->tx_params.cw_max,
446 ring->tx_params.aifs);
447
448 return 0;
449}
450EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);
This page took 0.122398 seconds and 5 git commands to generate.