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;
85 memset(txdesc, 0, sizeof(*txdesc));
88 * Initialize information from queue
90 txdesc->queue = entry->queue->qid;
91 txdesc->cw_min = entry->queue->cw_min;
92 txdesc->cw_max = entry->queue->cw_max;
93 txdesc->aifs = entry->queue->aifs;
95 /* Data length should be extended with 4 bytes for CRC */
96 data_length = entry->skb->len + 4;
99 * Read required fields from ieee80211 header.
101 frame_control = le16_to_cpu(hdr->frame_control);
104 * Check whether this frame is to be acked.
106 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
107 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
110 * Check if this is a RTS/CTS frame
112 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
113 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
114 if (is_rts_frame(frame_control))
115 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
117 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
118 if (tx_info->control.rts_cts_rate_idx >= 0)
120 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
124 * Determine retry information.
126 txdesc->retry_limit = tx_info->control.retry_limit;
127 if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
128 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
131 * Check if more fragments are pending
133 if (ieee80211_has_morefrags(hdr->frame_control)) {
134 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
135 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
139 * Beacons and probe responses require the tsf timestamp
140 * to be inserted into the frame.
142 if (txdesc->queue == QID_BEACON || is_probe_resp(frame_control))
143 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
146 * Determine with what IFS priority this frame should be send.
147 * Set ifs to IFS_SIFS when the this is not the first fragment,
148 * or this fragment came after RTS/CTS.
150 if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
151 txdesc->ifs = IFS_SIFS;
152 } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
153 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
154 txdesc->ifs = IFS_BACKOFF;
156 txdesc->ifs = IFS_SIFS;
161 * Length calculation depends on OFDM/CCK rate.
163 hwrate = rt2x00_get_rate(rate->hw_value);
164 txdesc->signal = hwrate->plcp;
165 txdesc->service = 0x04;
167 if (hwrate->flags & DEV_RATE_OFDM) {
168 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
170 txdesc->length_high = (data_length >> 6) & 0x3f;
171 txdesc->length_low = data_length & 0x3f;
174 * Convert length to microseconds.
176 residual = get_duration_res(data_length, hwrate->bitrate);
177 duration = get_duration(data_length, hwrate->bitrate);
183 * Check if we need to set the Length Extension
185 if (hwrate->bitrate == 110 && residual <= 30)
186 txdesc->service |= 0x80;
189 txdesc->length_high = (duration >> 8) & 0xff;
190 txdesc->length_low = duration & 0xff;
193 * When preamble is enabled we should set the
194 * preamble bit for the signal.
196 if (rt2x00_get_rate_preamble(rate->hw_value))
197 txdesc->signal |= 0x08;
200 EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
202 void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
203 struct txentry_desc *txdesc)
205 struct data_queue *queue = entry->queue;
206 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
208 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
211 * All processing on the frame has been completed, this means
212 * it is now ready to be dumped to userspace through debugfs.
214 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
217 * Check if we need to kick the queue, there are however a few rules
218 * 1) Don't kick beacon queue
219 * 2) Don't kick unless this is the last in frame in a burst.
220 * When the burst flag is set, this frame is always followed
221 * by another frame which in some way are related to eachother.
222 * This is true for fragments, RTS or CTS-to-self frames.
223 * 3) Rule 2 can be broken when the available entries
224 * in the queue are less then a certain threshold.
226 if (entry->queue->qid == QID_BEACON)
229 if (rt2x00queue_threshold(queue) ||
230 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
231 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
233 EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
235 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
237 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
238 struct txentry_desc txdesc;
240 if (unlikely(rt2x00queue_full(queue)))
243 if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
244 ERROR(queue->rt2x00dev,
245 "Arrived at non-free entry in the non-full queue %d.\n"
246 "Please file bug report to %s.\n",
247 queue->qid, DRV_PROJECT);
252 * Copy all TX descriptor information into txdesc,
253 * after that we are free to use the skb->cb array
254 * for our information.
257 rt2x00queue_create_tx_descriptor(entry, &txdesc);
259 if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
260 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
264 __set_bit(ENTRY_DATA_PENDING, &entry->flags);
266 rt2x00queue_index_inc(queue, Q_INDEX);
267 rt2x00queue_write_tx_descriptor(entry, &txdesc);
272 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
273 const enum data_queue_qid queue)
275 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
277 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
278 return &rt2x00dev->tx[queue];
283 if (queue == QID_BEACON)
284 return &rt2x00dev->bcn[0];
285 else if (queue == QID_ATIM && atim)
286 return &rt2x00dev->bcn[1];
290 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
292 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
293 enum queue_index index)
295 struct queue_entry *entry;
296 unsigned long irqflags;
298 if (unlikely(index >= Q_INDEX_MAX)) {
299 ERROR(queue->rt2x00dev,
300 "Entry requested from invalid index type (%d)\n", index);
304 spin_lock_irqsave(&queue->lock, irqflags);
306 entry = &queue->entries[queue->index[index]];
308 spin_unlock_irqrestore(&queue->lock, irqflags);
312 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
314 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
316 unsigned long irqflags;
318 if (unlikely(index >= Q_INDEX_MAX)) {
319 ERROR(queue->rt2x00dev,
320 "Index change on invalid index type (%d)\n", index);
324 spin_lock_irqsave(&queue->lock, irqflags);
326 queue->index[index]++;
327 if (queue->index[index] >= queue->limit)
328 queue->index[index] = 0;
330 if (index == Q_INDEX) {
332 } else if (index == Q_INDEX_DONE) {
337 spin_unlock_irqrestore(&queue->lock, irqflags);
339 EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
341 static void rt2x00queue_reset(struct data_queue *queue)
343 unsigned long irqflags;
345 spin_lock_irqsave(&queue->lock, irqflags);
349 memset(queue->index, 0, sizeof(queue->index));
351 spin_unlock_irqrestore(&queue->lock, irqflags);
354 void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
356 struct data_queue *queue = rt2x00dev->rx;
359 rt2x00queue_reset(queue);
361 if (!rt2x00dev->ops->lib->init_rxentry)
364 for (i = 0; i < queue->limit; i++)
365 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
369 void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
371 struct data_queue *queue;
374 txall_queue_for_each(rt2x00dev, queue) {
375 rt2x00queue_reset(queue);
377 if (!rt2x00dev->ops->lib->init_txentry)
380 for (i = 0; i < queue->limit; i++)
381 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
386 static int rt2x00queue_alloc_entries(struct data_queue *queue,
387 const struct data_queue_desc *qdesc)
389 struct queue_entry *entries;
390 unsigned int entry_size;
393 rt2x00queue_reset(queue);
395 queue->limit = qdesc->entry_num;
396 queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
397 queue->data_size = qdesc->data_size;
398 queue->desc_size = qdesc->desc_size;
401 * Allocate all queue entries.
403 entry_size = sizeof(*entries) + qdesc->priv_size;
404 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
408 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
409 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
410 ((__index) * (__psize)) )
412 for (i = 0; i < queue->limit; i++) {
413 entries[i].flags = 0;
414 entries[i].queue = queue;
415 entries[i].skb = NULL;
416 entries[i].entry_idx = i;
417 entries[i].priv_data =
418 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
419 sizeof(*entries), qdesc->priv_size);
422 #undef QUEUE_ENTRY_PRIV_OFFSET
424 queue->entries = entries;
429 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
431 struct data_queue *queue;
435 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
439 tx_queue_for_each(rt2x00dev, queue) {
440 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
445 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
449 if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
452 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
453 rt2x00dev->ops->atim);
460 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
462 rt2x00queue_uninitialize(rt2x00dev);
467 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
469 struct data_queue *queue;
471 queue_for_each(rt2x00dev, queue) {
472 kfree(queue->entries);
473 queue->entries = NULL;
477 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
478 struct data_queue *queue, enum data_queue_qid qid)
480 spin_lock_init(&queue->lock);
482 queue->rt2x00dev = rt2x00dev;
489 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
491 struct data_queue *queue;
492 enum data_queue_qid qid;
493 unsigned int req_atim =
494 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
497 * We need the following queues:
501 * Atim: 1 (if required)
503 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
505 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
507 ERROR(rt2x00dev, "Queue allocation failed.\n");
512 * Initialize pointers
514 rt2x00dev->rx = queue;
515 rt2x00dev->tx = &queue[1];
516 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
519 * Initialize queue parameters.
521 * TX: qid = QID_AC_BE + index
522 * TX: cw_min: 2^5 = 32.
523 * TX: cw_max: 2^10 = 1024.
524 * BCN: qid = QID_BEACON
525 * ATIM: qid = QID_ATIM
527 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
530 tx_queue_for_each(rt2x00dev, queue)
531 rt2x00queue_init(rt2x00dev, queue, qid++);
533 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
535 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
540 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
542 kfree(rt2x00dev->rx);
543 rt2x00dev->rx = NULL;
544 rt2x00dev->tx = NULL;
545 rt2x00dev->bcn = NULL;