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>
28 #include <linux/dma-mapping.h>
31 #include "rt2x00lib.h"
33 struct sk_buff *rt2x00queue_alloc_rxskb(struct rt2x00_dev *rt2x00dev,
34 struct queue_entry *entry)
36 unsigned int frame_size;
37 unsigned int reserved_size;
39 struct skb_frame_desc *skbdesc;
42 * The frame size includes descriptor size, because the
43 * hardware directly receive the frame into the skbuffer.
45 frame_size = entry->queue->data_size + entry->queue->desc_size;
48 * Reserve a few bytes extra headroom to allow drivers some moving
49 * space (e.g. for alignment), while keeping the skb aligned.
56 skb = dev_alloc_skb(frame_size + reserved_size);
60 skb_reserve(skb, reserved_size);
61 skb_put(skb, frame_size);
66 skbdesc = get_skb_frame_desc(skb);
67 memset(skbdesc, 0, sizeof(*skbdesc));
68 skbdesc->entry = entry;
70 if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
71 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
75 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
81 void rt2x00queue_map_txskb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
83 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
85 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
87 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
89 EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
91 void rt2x00queue_unmap_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
93 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
95 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
96 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
98 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
101 if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
102 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
104 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
108 void rt2x00queue_free_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
110 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
112 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
113 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
117 if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
118 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
122 dev_kfree_skb_any(skb);
125 void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
126 struct txentry_desc *txdesc)
128 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
129 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
130 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
131 struct ieee80211_rate *rate =
132 ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
133 const struct rt2x00_rate *hwrate;
134 unsigned int data_length;
135 unsigned int duration;
136 unsigned int residual;
138 memset(txdesc, 0, sizeof(*txdesc));
141 * Initialize information from queue
143 txdesc->queue = entry->queue->qid;
144 txdesc->cw_min = entry->queue->cw_min;
145 txdesc->cw_max = entry->queue->cw_max;
146 txdesc->aifs = entry->queue->aifs;
148 /* Data length should be extended with 4 bytes for CRC */
149 data_length = entry->skb->len + 4;
152 * Check whether this frame is to be acked.
154 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
155 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
158 * Check if this is a RTS/CTS frame
160 if (ieee80211_is_rts(hdr->frame_control) ||
161 ieee80211_is_cts(hdr->frame_control)) {
162 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
163 if (ieee80211_is_rts(hdr->frame_control))
164 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
166 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
167 if (tx_info->control.rts_cts_rate_idx >= 0)
169 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
173 * Determine retry information.
175 txdesc->retry_limit = tx_info->control.retry_limit;
176 if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
177 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
180 * Check if more fragments are pending
182 if (ieee80211_has_morefrags(hdr->frame_control)) {
183 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
184 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
188 * Beacons and probe responses require the tsf timestamp
189 * to be inserted into the frame.
191 if (ieee80211_is_beacon(hdr->frame_control) ||
192 ieee80211_is_probe_resp(hdr->frame_control))
193 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
196 * Determine with what IFS priority this frame should be send.
197 * Set ifs to IFS_SIFS when the this is not the first fragment,
198 * or this fragment came after RTS/CTS.
200 if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
201 txdesc->ifs = IFS_SIFS;
202 } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
203 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
204 txdesc->ifs = IFS_BACKOFF;
206 txdesc->ifs = IFS_SIFS;
211 * Length calculation depends on OFDM/CCK rate.
213 hwrate = rt2x00_get_rate(rate->hw_value);
214 txdesc->signal = hwrate->plcp;
215 txdesc->service = 0x04;
217 if (hwrate->flags & DEV_RATE_OFDM) {
218 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
220 txdesc->length_high = (data_length >> 6) & 0x3f;
221 txdesc->length_low = data_length & 0x3f;
224 * Convert length to microseconds.
226 residual = get_duration_res(data_length, hwrate->bitrate);
227 duration = get_duration(data_length, hwrate->bitrate);
233 * Check if we need to set the Length Extension
235 if (hwrate->bitrate == 110 && residual <= 30)
236 txdesc->service |= 0x80;
239 txdesc->length_high = (duration >> 8) & 0xff;
240 txdesc->length_low = duration & 0xff;
243 * When preamble is enabled we should set the
244 * preamble bit for the signal.
246 if (rt2x00_get_rate_preamble(rate->hw_value))
247 txdesc->signal |= 0x08;
250 EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
252 void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
253 struct txentry_desc *txdesc)
255 struct data_queue *queue = entry->queue;
256 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
258 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
261 * All processing on the frame has been completed, this means
262 * it is now ready to be dumped to userspace through debugfs.
264 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
267 * Check if we need to kick the queue, there are however a few rules
268 * 1) Don't kick beacon queue
269 * 2) Don't kick unless this is the last in frame in a burst.
270 * When the burst flag is set, this frame is always followed
271 * by another frame which in some way are related to eachother.
272 * This is true for fragments, RTS or CTS-to-self frames.
273 * 3) Rule 2 can be broken when the available entries
274 * in the queue are less then a certain threshold.
276 if (entry->queue->qid == QID_BEACON)
279 if (rt2x00queue_threshold(queue) ||
280 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
281 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
283 EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
285 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
287 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
288 struct txentry_desc txdesc;
289 struct skb_frame_desc *skbdesc;
291 if (unlikely(rt2x00queue_full(queue)))
294 if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
295 ERROR(queue->rt2x00dev,
296 "Arrived at non-free entry in the non-full queue %d.\n"
297 "Please file bug report to %s.\n",
298 queue->qid, DRV_PROJECT);
303 * Copy all TX descriptor information into txdesc,
304 * after that we are free to use the skb->cb array
305 * for our information.
308 rt2x00queue_create_tx_descriptor(entry, &txdesc);
311 * skb->cb array is now ours and we are free to use it.
313 skbdesc = get_skb_frame_desc(entry->skb);
314 memset(skbdesc, 0, sizeof(*skbdesc));
315 skbdesc->entry = entry;
317 if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
318 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
322 if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
323 rt2x00queue_map_txskb(queue->rt2x00dev, skb);
325 __set_bit(ENTRY_DATA_PENDING, &entry->flags);
327 rt2x00queue_index_inc(queue, Q_INDEX);
328 rt2x00queue_write_tx_descriptor(entry, &txdesc);
333 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
334 const enum data_queue_qid queue)
336 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
338 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
339 return &rt2x00dev->tx[queue];
344 if (queue == QID_BEACON)
345 return &rt2x00dev->bcn[0];
346 else if (queue == QID_ATIM && atim)
347 return &rt2x00dev->bcn[1];
351 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
353 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
354 enum queue_index index)
356 struct queue_entry *entry;
357 unsigned long irqflags;
359 if (unlikely(index >= Q_INDEX_MAX)) {
360 ERROR(queue->rt2x00dev,
361 "Entry requested from invalid index type (%d)\n", index);
365 spin_lock_irqsave(&queue->lock, irqflags);
367 entry = &queue->entries[queue->index[index]];
369 spin_unlock_irqrestore(&queue->lock, irqflags);
373 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
375 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
377 unsigned long irqflags;
379 if (unlikely(index >= Q_INDEX_MAX)) {
380 ERROR(queue->rt2x00dev,
381 "Index change on invalid index type (%d)\n", index);
385 spin_lock_irqsave(&queue->lock, irqflags);
387 queue->index[index]++;
388 if (queue->index[index] >= queue->limit)
389 queue->index[index] = 0;
391 if (index == Q_INDEX) {
393 } else if (index == Q_INDEX_DONE) {
398 spin_unlock_irqrestore(&queue->lock, irqflags);
401 static void rt2x00queue_reset(struct data_queue *queue)
403 unsigned long irqflags;
405 spin_lock_irqsave(&queue->lock, irqflags);
409 memset(queue->index, 0, sizeof(queue->index));
411 spin_unlock_irqrestore(&queue->lock, irqflags);
414 void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
416 struct data_queue *queue = rt2x00dev->rx;
419 rt2x00queue_reset(queue);
421 if (!rt2x00dev->ops->lib->init_rxentry)
424 for (i = 0; i < queue->limit; i++)
425 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
429 void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
431 struct data_queue *queue;
434 txall_queue_for_each(rt2x00dev, queue) {
435 rt2x00queue_reset(queue);
437 if (!rt2x00dev->ops->lib->init_txentry)
440 for (i = 0; i < queue->limit; i++)
441 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
446 static int rt2x00queue_alloc_entries(struct data_queue *queue,
447 const struct data_queue_desc *qdesc)
449 struct queue_entry *entries;
450 unsigned int entry_size;
453 rt2x00queue_reset(queue);
455 queue->limit = qdesc->entry_num;
456 queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
457 queue->data_size = qdesc->data_size;
458 queue->desc_size = qdesc->desc_size;
461 * Allocate all queue entries.
463 entry_size = sizeof(*entries) + qdesc->priv_size;
464 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
468 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
469 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
470 ((__index) * (__psize)) )
472 for (i = 0; i < queue->limit; i++) {
473 entries[i].flags = 0;
474 entries[i].queue = queue;
475 entries[i].skb = NULL;
476 entries[i].entry_idx = i;
477 entries[i].priv_data =
478 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
479 sizeof(*entries), qdesc->priv_size);
482 #undef QUEUE_ENTRY_PRIV_OFFSET
484 queue->entries = entries;
489 static void rt2x00queue_free_skbs(struct rt2x00_dev *rt2x00dev,
490 struct data_queue *queue)
497 for (i = 0; i < queue->limit; i++) {
498 if (queue->entries[i].skb)
499 rt2x00queue_free_skb(rt2x00dev, queue->entries[i].skb);
503 static int rt2x00queue_alloc_rxskbs(struct rt2x00_dev *rt2x00dev,
504 struct data_queue *queue)
509 for (i = 0; i < queue->limit; i++) {
510 skb = rt2x00queue_alloc_rxskb(rt2x00dev, &queue->entries[i]);
513 queue->entries[i].skb = skb;
519 rt2x00queue_free_skbs(rt2x00dev, queue);
524 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
526 struct data_queue *queue;
529 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
533 tx_queue_for_each(rt2x00dev, queue) {
534 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
539 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
543 if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
544 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
545 rt2x00dev->ops->atim);
550 status = rt2x00queue_alloc_rxskbs(rt2x00dev, rt2x00dev->rx);
557 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
559 rt2x00queue_uninitialize(rt2x00dev);
564 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
566 struct data_queue *queue;
568 rt2x00queue_free_skbs(rt2x00dev, rt2x00dev->rx);
570 queue_for_each(rt2x00dev, queue) {
571 kfree(queue->entries);
572 queue->entries = NULL;
576 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
577 struct data_queue *queue, enum data_queue_qid qid)
579 spin_lock_init(&queue->lock);
581 queue->rt2x00dev = rt2x00dev;
588 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
590 struct data_queue *queue;
591 enum data_queue_qid qid;
592 unsigned int req_atim =
593 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
596 * We need the following queues:
600 * Atim: 1 (if required)
602 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
604 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
606 ERROR(rt2x00dev, "Queue allocation failed.\n");
611 * Initialize pointers
613 rt2x00dev->rx = queue;
614 rt2x00dev->tx = &queue[1];
615 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
618 * Initialize queue parameters.
620 * TX: qid = QID_AC_BE + index
621 * TX: cw_min: 2^5 = 32.
622 * TX: cw_max: 2^10 = 1024.
623 * BCN: qid = QID_BEACON
624 * ATIM: qid = QID_ATIM
626 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
629 tx_queue_for_each(rt2x00dev, queue)
630 rt2x00queue_init(rt2x00dev, queue, qid++);
632 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
634 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
639 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
641 kfree(rt2x00dev->rx);
642 rt2x00dev->rx = NULL;
643 rt2x00dev->tx = NULL;
644 rt2x00dev->bcn = NULL;