2 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
23 Abstract: rt2x00 queue specific routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
30 #include "rt2x00lib.h"
32 struct sk_buff *rt2x00queue_alloc_rxskb(struct data_queue *queue)
35 unsigned int frame_size;
36 unsigned int reserved_size;
39 * The frame size includes descriptor size, because the
40 * hardware directly receive the frame into the skbuffer.
42 frame_size = queue->data_size + queue->desc_size;
45 * For the allocation we should keep a few things in mind:
46 * 1) 4byte alignment of 802.11 payload
48 * For (1) we need at most 4 bytes to guarentee the correct
49 * alignment. We are going to optimize the fact that the chance
50 * that the 802.11 header_size % 4 == 2 is much bigger then
51 * anything else. However since we need to move the frame up
52 * to 3 bytes to the front, which means we need to preallocate
60 skb = dev_alloc_skb(frame_size + reserved_size);
64 skb_reserve(skb, reserved_size);
65 skb_put(skb, frame_size);
69 EXPORT_SYMBOL_GPL(rt2x00queue_alloc_rxskb);
71 void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
72 struct txentry_desc *txdesc)
74 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
75 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
76 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
77 struct ieee80211_rate *rate =
78 ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
79 const struct rt2x00_rate *hwrate;
80 unsigned int data_length;
81 unsigned int duration;
82 unsigned int residual;
84 memset(txdesc, 0, sizeof(*txdesc));
87 * Initialize information from queue
89 txdesc->queue = entry->queue->qid;
90 txdesc->cw_min = entry->queue->cw_min;
91 txdesc->cw_max = entry->queue->cw_max;
92 txdesc->aifs = entry->queue->aifs;
94 /* Data length should be extended with 4 bytes for CRC */
95 data_length = entry->skb->len + 4;
98 * Check whether this frame is to be acked.
100 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
101 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
104 * Check if this is a RTS/CTS frame
106 if (ieee80211_is_rts(hdr->frame_control) ||
107 ieee80211_is_cts(hdr->frame_control)) {
108 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
109 if (ieee80211_is_rts(hdr->frame_control))
110 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
112 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
113 if (tx_info->control.rts_cts_rate_idx >= 0)
115 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
119 * Determine retry information.
121 txdesc->retry_limit = tx_info->control.retry_limit;
122 if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
123 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
126 * Check if more fragments are pending
128 if (ieee80211_has_morefrags(hdr->frame_control)) {
129 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
130 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
134 * Beacons and probe responses require the tsf timestamp
135 * to be inserted into the frame.
137 if (ieee80211_is_beacon(hdr->frame_control) ||
138 ieee80211_is_probe_resp(hdr->frame_control))
139 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
142 * Determine with what IFS priority this frame should be send.
143 * Set ifs to IFS_SIFS when the this is not the first fragment,
144 * or this fragment came after RTS/CTS.
146 if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
147 txdesc->ifs = IFS_SIFS;
148 } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
149 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
150 txdesc->ifs = IFS_BACKOFF;
152 txdesc->ifs = IFS_SIFS;
157 * Length calculation depends on OFDM/CCK rate.
159 hwrate = rt2x00_get_rate(rate->hw_value);
160 txdesc->signal = hwrate->plcp;
161 txdesc->service = 0x04;
163 if (hwrate->flags & DEV_RATE_OFDM) {
164 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
166 txdesc->length_high = (data_length >> 6) & 0x3f;
167 txdesc->length_low = data_length & 0x3f;
170 * Convert length to microseconds.
172 residual = get_duration_res(data_length, hwrate->bitrate);
173 duration = get_duration(data_length, hwrate->bitrate);
179 * Check if we need to set the Length Extension
181 if (hwrate->bitrate == 110 && residual <= 30)
182 txdesc->service |= 0x80;
185 txdesc->length_high = (duration >> 8) & 0xff;
186 txdesc->length_low = duration & 0xff;
189 * When preamble is enabled we should set the
190 * preamble bit for the signal.
192 if (rt2x00_get_rate_preamble(rate->hw_value))
193 txdesc->signal |= 0x08;
196 EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
198 void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
199 struct txentry_desc *txdesc)
201 struct data_queue *queue = entry->queue;
202 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
204 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
207 * All processing on the frame has been completed, this means
208 * it is now ready to be dumped to userspace through debugfs.
210 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
213 * Check if we need to kick the queue, there are however a few rules
214 * 1) Don't kick beacon queue
215 * 2) Don't kick unless this is the last in frame in a burst.
216 * When the burst flag is set, this frame is always followed
217 * by another frame which in some way are related to eachother.
218 * This is true for fragments, RTS or CTS-to-self frames.
219 * 3) Rule 2 can be broken when the available entries
220 * in the queue are less then a certain threshold.
222 if (entry->queue->qid == QID_BEACON)
225 if (rt2x00queue_threshold(queue) ||
226 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
227 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
229 EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
231 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
233 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
234 struct txentry_desc txdesc;
236 if (unlikely(rt2x00queue_full(queue)))
239 if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
240 ERROR(queue->rt2x00dev,
241 "Arrived at non-free entry in the non-full queue %d.\n"
242 "Please file bug report to %s.\n",
243 queue->qid, DRV_PROJECT);
248 * Copy all TX descriptor information into txdesc,
249 * after that we are free to use the skb->cb array
250 * for our information.
253 rt2x00queue_create_tx_descriptor(entry, &txdesc);
255 if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
256 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
260 __set_bit(ENTRY_DATA_PENDING, &entry->flags);
262 rt2x00queue_index_inc(queue, Q_INDEX);
263 rt2x00queue_write_tx_descriptor(entry, &txdesc);
268 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
269 const enum data_queue_qid queue)
271 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
273 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
274 return &rt2x00dev->tx[queue];
279 if (queue == QID_BEACON)
280 return &rt2x00dev->bcn[0];
281 else if (queue == QID_ATIM && atim)
282 return &rt2x00dev->bcn[1];
286 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
288 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
289 enum queue_index index)
291 struct queue_entry *entry;
292 unsigned long irqflags;
294 if (unlikely(index >= Q_INDEX_MAX)) {
295 ERROR(queue->rt2x00dev,
296 "Entry requested from invalid index type (%d)\n", index);
300 spin_lock_irqsave(&queue->lock, irqflags);
302 entry = &queue->entries[queue->index[index]];
304 spin_unlock_irqrestore(&queue->lock, irqflags);
308 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
310 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
312 unsigned long irqflags;
314 if (unlikely(index >= Q_INDEX_MAX)) {
315 ERROR(queue->rt2x00dev,
316 "Index change on invalid index type (%d)\n", index);
320 spin_lock_irqsave(&queue->lock, irqflags);
322 queue->index[index]++;
323 if (queue->index[index] >= queue->limit)
324 queue->index[index] = 0;
326 if (index == Q_INDEX) {
328 } else if (index == Q_INDEX_DONE) {
333 spin_unlock_irqrestore(&queue->lock, irqflags);
335 EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
337 static void rt2x00queue_reset(struct data_queue *queue)
339 unsigned long irqflags;
341 spin_lock_irqsave(&queue->lock, irqflags);
345 memset(queue->index, 0, sizeof(queue->index));
347 spin_unlock_irqrestore(&queue->lock, irqflags);
350 void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
352 struct data_queue *queue = rt2x00dev->rx;
355 rt2x00queue_reset(queue);
357 if (!rt2x00dev->ops->lib->init_rxentry)
360 for (i = 0; i < queue->limit; i++)
361 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
365 void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
367 struct data_queue *queue;
370 txall_queue_for_each(rt2x00dev, queue) {
371 rt2x00queue_reset(queue);
373 if (!rt2x00dev->ops->lib->init_txentry)
376 for (i = 0; i < queue->limit; i++)
377 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
382 static int rt2x00queue_alloc_entries(struct data_queue *queue,
383 const struct data_queue_desc *qdesc)
385 struct queue_entry *entries;
386 unsigned int entry_size;
389 rt2x00queue_reset(queue);
391 queue->limit = qdesc->entry_num;
392 queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
393 queue->data_size = qdesc->data_size;
394 queue->desc_size = qdesc->desc_size;
397 * Allocate all queue entries.
399 entry_size = sizeof(*entries) + qdesc->priv_size;
400 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
404 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
405 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
406 ((__index) * (__psize)) )
408 for (i = 0; i < queue->limit; i++) {
409 entries[i].flags = 0;
410 entries[i].queue = queue;
411 entries[i].skb = NULL;
412 entries[i].entry_idx = i;
413 entries[i].priv_data =
414 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
415 sizeof(*entries), qdesc->priv_size);
418 #undef QUEUE_ENTRY_PRIV_OFFSET
420 queue->entries = entries;
425 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
427 struct data_queue *queue;
431 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
435 tx_queue_for_each(rt2x00dev, queue) {
436 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
441 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
445 if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
448 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
449 rt2x00dev->ops->atim);
456 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
458 rt2x00queue_uninitialize(rt2x00dev);
463 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
465 struct data_queue *queue;
467 queue_for_each(rt2x00dev, queue) {
468 kfree(queue->entries);
469 queue->entries = NULL;
473 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
474 struct data_queue *queue, enum data_queue_qid qid)
476 spin_lock_init(&queue->lock);
478 queue->rt2x00dev = rt2x00dev;
485 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
487 struct data_queue *queue;
488 enum data_queue_qid qid;
489 unsigned int req_atim =
490 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
493 * We need the following queues:
497 * Atim: 1 (if required)
499 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
501 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
503 ERROR(rt2x00dev, "Queue allocation failed.\n");
508 * Initialize pointers
510 rt2x00dev->rx = queue;
511 rt2x00dev->tx = &queue[1];
512 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
515 * Initialize queue parameters.
517 * TX: qid = QID_AC_BE + index
518 * TX: cw_min: 2^5 = 32.
519 * TX: cw_max: 2^10 = 1024.
520 * BCN: qid = QID_BEACON
521 * ATIM: qid = QID_ATIM
523 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
526 tx_queue_for_each(rt2x00dev, queue)
527 rt2x00queue_init(rt2x00dev, queue, qid++);
529 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
531 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
536 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
538 kfree(rt2x00dev->rx);
539 rt2x00dev->rx = NULL;
540 rt2x00dev->tx = NULL;
541 rt2x00dev->bcn = NULL;