blob: 49d3bb84ab6b0563c66ad839600759391b9cfcfc [file] [log] [blame]
Ivo van Doorn181d6902008-02-05 16:42:23 -05001/*
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: rt2x00lib
23 Abstract: rt2x00 queue specific routines.
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020028#include <linux/dma-mapping.h>
Ivo van Doorn181d6902008-02-05 16:42:23 -050029
30#include "rt2x00.h"
31#include "rt2x00lib.h"
32
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020033struct sk_buff *rt2x00queue_alloc_rxskb(struct rt2x00_dev *rt2x00dev,
34 struct queue_entry *entry)
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020035{
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020036 unsigned int frame_size;
37 unsigned int reserved_size;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020038 struct sk_buff *skb;
39 struct skb_frame_desc *skbdesc;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020040
41 /*
42 * The frame size includes descriptor size, because the
43 * hardware directly receive the frame into the skbuffer.
44 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020045 frame_size = entry->queue->data_size + entry->queue->desc_size;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020046
47 /*
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +020048 * Reserve a few bytes extra headroom to allow drivers some moving
49 * space (e.g. for alignment), while keeping the skb aligned.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020050 */
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +020051 reserved_size = 8;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020052
53 /*
54 * Allocate skbuffer.
55 */
56 skb = dev_alloc_skb(frame_size + reserved_size);
57 if (!skb)
58 return NULL;
59
60 skb_reserve(skb, reserved_size);
61 skb_put(skb, frame_size);
62
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020063 /*
64 * Populate skbdesc.
65 */
66 skbdesc = get_skb_frame_desc(skb);
67 memset(skbdesc, 0, sizeof(*skbdesc));
68 skbdesc->entry = entry;
69
70 if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
71 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
72 skb->data,
73 skb->len,
74 DMA_FROM_DEVICE);
75 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
76 }
77
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020078 return skb;
79}
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +020080
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020081void rt2x00queue_map_txskb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +020082{
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020083 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
84
85 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
86 DMA_TO_DEVICE);
87 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
88}
89EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
90
91void rt2x00queue_unmap_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
92{
93 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
94
95 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
96 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
97 DMA_FROM_DEVICE);
98 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
99 }
100
101 if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
102 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
103 DMA_TO_DEVICE);
104 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
105 }
106}
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200107
108void rt2x00queue_free_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
109{
110 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
111
112 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
113 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
114 DMA_FROM_DEVICE);
115 }
116
117 if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
118 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
119 DMA_TO_DEVICE);
120 }
121
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200122 dev_kfree_skb_any(skb);
123}
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200124
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200125void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
Johannes Berge039fa42008-05-15 12:55:29 +0200126 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200127{
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200128 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Johannes Berge039fa42008-05-15 12:55:29 +0200129 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200130 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200131 struct ieee80211_rate *rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200132 ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200133 const struct rt2x00_rate *hwrate;
134 unsigned int data_length;
135 unsigned int duration;
136 unsigned int residual;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200137
138 memset(txdesc, 0, sizeof(*txdesc));
139
140 /*
141 * Initialize information from queue
142 */
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;
147
148 /* Data length should be extended with 4 bytes for CRC */
149 data_length = entry->skb->len + 4;
150
151 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200152 * Check whether this frame is to be acked.
153 */
Johannes Berge039fa42008-05-15 12:55:29 +0200154 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200155 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
156
157 /*
158 * Check if this is a RTS/CTS frame
159 */
Ivo van Doornac104462008-06-16 19:54:57 +0200160 if (ieee80211_is_rts(hdr->frame_control) ||
161 ieee80211_is_cts(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200162 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Ivo van Doornac104462008-06-16 19:54:57 +0200163 if (ieee80211_is_rts(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200164 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200165 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200166 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200167 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200168 rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200169 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200170 }
171
172 /*
173 * Determine retry information.
174 */
Johannes Berge039fa42008-05-15 12:55:29 +0200175 txdesc->retry_limit = tx_info->control.retry_limit;
176 if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200177 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
178
179 /*
180 * Check if more fragments are pending
181 */
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700182 if (ieee80211_has_morefrags(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200183 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
184 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
185 }
186
187 /*
188 * Beacons and probe responses require the tsf timestamp
189 * to be inserted into the frame.
190 */
Ivo van Doornac104462008-06-16 19:54:57 +0200191 if (ieee80211_is_beacon(hdr->frame_control) ||
192 ieee80211_is_probe_resp(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200193 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
194
195 /*
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.
199 */
200 if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
201 txdesc->ifs = IFS_SIFS;
Johannes Berge039fa42008-05-15 12:55:29 +0200202 } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200203 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
204 txdesc->ifs = IFS_BACKOFF;
205 } else {
206 txdesc->ifs = IFS_SIFS;
207 }
208
209 /*
210 * PLCP setup
211 * Length calculation depends on OFDM/CCK rate.
212 */
213 hwrate = rt2x00_get_rate(rate->hw_value);
214 txdesc->signal = hwrate->plcp;
215 txdesc->service = 0x04;
216
217 if (hwrate->flags & DEV_RATE_OFDM) {
218 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
219
220 txdesc->length_high = (data_length >> 6) & 0x3f;
221 txdesc->length_low = data_length & 0x3f;
222 } else {
223 /*
224 * Convert length to microseconds.
225 */
226 residual = get_duration_res(data_length, hwrate->bitrate);
227 duration = get_duration(data_length, hwrate->bitrate);
228
229 if (residual != 0) {
230 duration++;
231
232 /*
233 * Check if we need to set the Length Extension
234 */
235 if (hwrate->bitrate == 110 && residual <= 30)
236 txdesc->service |= 0x80;
237 }
238
239 txdesc->length_high = (duration >> 8) & 0xff;
240 txdesc->length_low = duration & 0xff;
241
242 /*
243 * When preamble is enabled we should set the
244 * preamble bit for the signal.
245 */
246 if (rt2x00_get_rate_preamble(rate->hw_value))
247 txdesc->signal |= 0x08;
248 }
249}
250EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
251
252void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
253 struct txentry_desc *txdesc)
254{
Ivo van Doornb8697672008-06-06 22:53:14 +0200255 struct data_queue *queue = entry->queue;
256 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200257
258 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
259
260 /*
261 * All processing on the frame has been completed, this means
262 * it is now ready to be dumped to userspace through debugfs.
263 */
264 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
265
266 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200267 * 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.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200275 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200276 if (entry->queue->qid == QID_BEACON)
277 return;
278
279 if (rt2x00queue_threshold(queue) ||
280 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
281 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200282}
283EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
284
Ivo van Doorn6db37862008-06-06 22:50:28 +0200285int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
286{
287 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
288 struct txentry_desc txdesc;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200289 struct skb_frame_desc *skbdesc;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200290
291 if (unlikely(rt2x00queue_full(queue)))
292 return -EINVAL;
293
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);
299 return -EINVAL;
300 }
301
302 /*
303 * Copy all TX descriptor information into txdesc,
304 * after that we are free to use the skb->cb array
305 * for our information.
306 */
307 entry->skb = skb;
308 rt2x00queue_create_tx_descriptor(entry, &txdesc);
309
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200310 /*
311 * skb->cb array is now ours and we are free to use it.
312 */
313 skbdesc = get_skb_frame_desc(entry->skb);
314 memset(skbdesc, 0, sizeof(*skbdesc));
315 skbdesc->entry = entry;
316
Ivo van Doorn6db37862008-06-06 22:50:28 +0200317 if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
318 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
319 return -EIO;
320 }
321
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200322 if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
323 rt2x00queue_map_txskb(queue->rt2x00dev, skb);
324
Ivo van Doorn6db37862008-06-06 22:50:28 +0200325 __set_bit(ENTRY_DATA_PENDING, &entry->flags);
326
327 rt2x00queue_index_inc(queue, Q_INDEX);
328 rt2x00queue_write_tx_descriptor(entry, &txdesc);
329
330 return 0;
331}
332
Ivo van Doorn181d6902008-02-05 16:42:23 -0500333struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200334 const enum data_queue_qid queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500335{
336 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
337
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200338 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500339 return &rt2x00dev->tx[queue];
340
341 if (!rt2x00dev->bcn)
342 return NULL;
343
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200344 if (queue == QID_BEACON)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500345 return &rt2x00dev->bcn[0];
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200346 else if (queue == QID_ATIM && atim)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500347 return &rt2x00dev->bcn[1];
348
349 return NULL;
350}
351EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
352
353struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
354 enum queue_index index)
355{
356 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100357 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500358
359 if (unlikely(index >= Q_INDEX_MAX)) {
360 ERROR(queue->rt2x00dev,
361 "Entry requested from invalid index type (%d)\n", index);
362 return NULL;
363 }
364
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100365 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500366
367 entry = &queue->entries[queue->index[index]];
368
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100369 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500370
371 return entry;
372}
373EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
374
375void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
376{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100377 unsigned long irqflags;
378
Ivo van Doorn181d6902008-02-05 16:42:23 -0500379 if (unlikely(index >= Q_INDEX_MAX)) {
380 ERROR(queue->rt2x00dev,
381 "Index change on invalid index type (%d)\n", index);
382 return;
383 }
384
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100385 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500386
387 queue->index[index]++;
388 if (queue->index[index] >= queue->limit)
389 queue->index[index] = 0;
390
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100391 if (index == Q_INDEX) {
392 queue->length++;
393 } else if (index == Q_INDEX_DONE) {
394 queue->length--;
395 queue->count ++;
396 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500397
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100398 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500399}
Ivo van Doorn181d6902008-02-05 16:42:23 -0500400
401static void rt2x00queue_reset(struct data_queue *queue)
402{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100403 unsigned long irqflags;
404
405 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500406
407 queue->count = 0;
408 queue->length = 0;
409 memset(queue->index, 0, sizeof(queue->index));
410
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100411 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500412}
413
414void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
415{
416 struct data_queue *queue = rt2x00dev->rx;
417 unsigned int i;
418
419 rt2x00queue_reset(queue);
420
421 if (!rt2x00dev->ops->lib->init_rxentry)
422 return;
423
424 for (i = 0; i < queue->limit; i++)
425 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
426 &queue->entries[i]);
427}
428
429void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
430{
431 struct data_queue *queue;
432 unsigned int i;
433
434 txall_queue_for_each(rt2x00dev, queue) {
435 rt2x00queue_reset(queue);
436
437 if (!rt2x00dev->ops->lib->init_txentry)
438 continue;
439
440 for (i = 0; i < queue->limit; i++)
441 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
442 &queue->entries[i]);
443 }
444}
445
446static int rt2x00queue_alloc_entries(struct data_queue *queue,
447 const struct data_queue_desc *qdesc)
448{
449 struct queue_entry *entries;
450 unsigned int entry_size;
451 unsigned int i;
452
453 rt2x00queue_reset(queue);
454
455 queue->limit = qdesc->entry_num;
Ivo van Doornb8697672008-06-06 22:53:14 +0200456 queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500457 queue->data_size = qdesc->data_size;
458 queue->desc_size = qdesc->desc_size;
459
460 /*
461 * Allocate all queue entries.
462 */
463 entry_size = sizeof(*entries) + qdesc->priv_size;
464 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
465 if (!entries)
466 return -ENOMEM;
467
468#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Adam Baker231be4e2008-02-10 22:48:19 +0100469 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
470 ((__index) * (__psize)) )
Ivo van Doorn181d6902008-02-05 16:42:23 -0500471
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);
480 }
481
482#undef QUEUE_ENTRY_PRIV_OFFSET
483
484 queue->entries = entries;
485
486 return 0;
487}
488
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200489static void rt2x00queue_free_skbs(struct rt2x00_dev *rt2x00dev,
490 struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200491{
492 unsigned int i;
493
494 if (!queue->entries)
495 return;
496
497 for (i = 0; i < queue->limit; i++) {
498 if (queue->entries[i].skb)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200499 rt2x00queue_free_skb(rt2x00dev, queue->entries[i].skb);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200500 }
501}
502
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200503static int rt2x00queue_alloc_rxskbs(struct rt2x00_dev *rt2x00dev,
504 struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200505{
506 unsigned int i;
507 struct sk_buff *skb;
508
509 for (i = 0; i < queue->limit; i++) {
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200510 skb = rt2x00queue_alloc_rxskb(rt2x00dev, &queue->entries[i]);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200511 if (!skb)
512 goto exit;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200513 queue->entries[i].skb = skb;
514 }
515
516 return 0;
517
518exit:
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200519 rt2x00queue_free_skbs(rt2x00dev, queue);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200520
521 return -ENOMEM;
522}
523
Ivo van Doorn181d6902008-02-05 16:42:23 -0500524int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
525{
526 struct data_queue *queue;
527 int status;
528
Ivo van Doorn181d6902008-02-05 16:42:23 -0500529 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
530 if (status)
531 goto exit;
532
533 tx_queue_for_each(rt2x00dev, queue) {
534 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
535 if (status)
536 goto exit;
537 }
538
539 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
540 if (status)
541 goto exit;
542
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200543 if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
544 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
545 rt2x00dev->ops->atim);
546 if (status)
547 goto exit;
548 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500549
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200550 status = rt2x00queue_alloc_rxskbs(rt2x00dev, rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500551 if (status)
552 goto exit;
553
554 return 0;
555
556exit:
557 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
558
559 rt2x00queue_uninitialize(rt2x00dev);
560
561 return status;
562}
563
564void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
565{
566 struct data_queue *queue;
567
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200568 rt2x00queue_free_skbs(rt2x00dev, rt2x00dev->rx);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200569
Ivo van Doorn181d6902008-02-05 16:42:23 -0500570 queue_for_each(rt2x00dev, queue) {
571 kfree(queue->entries);
572 queue->entries = NULL;
573 }
574}
575
Ivo van Doorn8f539272008-02-10 22:51:41 +0100576static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
577 struct data_queue *queue, enum data_queue_qid qid)
578{
579 spin_lock_init(&queue->lock);
580
581 queue->rt2x00dev = rt2x00dev;
582 queue->qid = qid;
583 queue->aifs = 2;
584 queue->cw_min = 5;
585 queue->cw_max = 10;
586}
587
Ivo van Doorn181d6902008-02-05 16:42:23 -0500588int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
589{
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);
594
595 /*
596 * We need the following queues:
597 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200598 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -0500599 * Beacon: 1
600 * Atim: 1 (if required)
601 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200602 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500603
604 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
605 if (!queue) {
606 ERROR(rt2x00dev, "Queue allocation failed.\n");
607 return -ENOMEM;
608 }
609
610 /*
611 * Initialize pointers
612 */
613 rt2x00dev->rx = queue;
614 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200615 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Ivo van Doorn181d6902008-02-05 16:42:23 -0500616
617 /*
618 * Initialize queue parameters.
619 * RX: qid = QID_RX
620 * TX: qid = QID_AC_BE + index
621 * TX: cw_min: 2^5 = 32.
622 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +0200623 * BCN: qid = QID_BEACON
624 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -0500625 */
Ivo van Doorn8f539272008-02-10 22:51:41 +0100626 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
627
Ivo van Doorn181d6902008-02-05 16:42:23 -0500628 qid = QID_AC_BE;
Ivo van Doorn8f539272008-02-10 22:51:41 +0100629 tx_queue_for_each(rt2x00dev, queue)
630 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500631
Ivo van Doorn565a0192008-06-03 20:29:05 +0200632 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500633 if (req_atim)
Ivo van Doorn565a0192008-06-03 20:29:05 +0200634 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500635
636 return 0;
637}
638
639void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
640{
641 kfree(rt2x00dev->rx);
642 rt2x00dev->rx = NULL;
643 rt2x00dev->tx = NULL;
644 rt2x00dev->bcn = NULL;
645}