blob: ecf57f8f34b28063fbe240d993a5f92657a9e3e1 [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 /*
Ivo van Doornff352392008-07-04 14:56:07 +020048 * The payload should be aligned to a 4-byte boundary,
49 * this means we need at least 3 bytes for moving the frame
50 * into the correct offset.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020051 */
Ivo van Doornff352392008-07-04 14:56:07 +020052 reserved_size = 4;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020053
54 /*
55 * Allocate skbuffer.
56 */
57 skb = dev_alloc_skb(frame_size + reserved_size);
58 if (!skb)
59 return NULL;
60
61 skb_reserve(skb, reserved_size);
62 skb_put(skb, frame_size);
63
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020064 /*
65 * Populate skbdesc.
66 */
67 skbdesc = get_skb_frame_desc(skb);
68 memset(skbdesc, 0, sizeof(*skbdesc));
69 skbdesc->entry = entry;
70
71 if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
72 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
73 skb->data,
74 skb->len,
75 DMA_FROM_DEVICE);
76 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
77 }
78
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020079 return skb;
80}
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +020081
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020082void rt2x00queue_map_txskb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +020083{
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020084 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
85
86 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
87 DMA_TO_DEVICE);
88 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
89}
90EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
91
92void rt2x00queue_unmap_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
93{
94 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
95
96 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
97 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
98 DMA_FROM_DEVICE);
99 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
100 }
101
102 if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
103 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
104 DMA_TO_DEVICE);
105 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
106 }
107}
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200108
109void rt2x00queue_free_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
110{
Ivo van Doorn9a613192008-07-05 15:11:57 +0200111 if (!skb)
112 return;
113
Ivo van Doorn61243d82008-06-20 22:10:53 +0200114 rt2x00queue_unmap_skb(rt2x00dev, skb);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200115 dev_kfree_skb_any(skb);
116}
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200117
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200118void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
Johannes Berge039fa42008-05-15 12:55:29 +0200119 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200120{
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200121 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Johannes Berge039fa42008-05-15 12:55:29 +0200122 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200123 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200124 struct ieee80211_rate *rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200125 ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200126 const struct rt2x00_rate *hwrate;
127 unsigned int data_length;
128 unsigned int duration;
129 unsigned int residual;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200130
131 memset(txdesc, 0, sizeof(*txdesc));
132
133 /*
134 * Initialize information from queue
135 */
136 txdesc->queue = entry->queue->qid;
137 txdesc->cw_min = entry->queue->cw_min;
138 txdesc->cw_max = entry->queue->cw_max;
139 txdesc->aifs = entry->queue->aifs;
140
141 /* Data length should be extended with 4 bytes for CRC */
142 data_length = entry->skb->len + 4;
143
144 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200145 * Check whether this frame is to be acked.
146 */
Johannes Berge039fa42008-05-15 12:55:29 +0200147 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200148 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
149
150 /*
151 * Check if this is a RTS/CTS frame
152 */
Ivo van Doornac104462008-06-16 19:54:57 +0200153 if (ieee80211_is_rts(hdr->frame_control) ||
154 ieee80211_is_cts(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200155 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Ivo van Doornac104462008-06-16 19:54:57 +0200156 if (ieee80211_is_rts(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200157 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200158 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200159 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200160 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200161 rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200162 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200163 }
164
165 /*
166 * Determine retry information.
167 */
Johannes Berge039fa42008-05-15 12:55:29 +0200168 txdesc->retry_limit = tx_info->control.retry_limit;
169 if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200170 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
171
172 /*
173 * Check if more fragments are pending
174 */
Harvey Harrison8b7b1e02008-06-11 14:21:56 -0700175 if (ieee80211_has_morefrags(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200176 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
177 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
178 }
179
180 /*
181 * Beacons and probe responses require the tsf timestamp
182 * to be inserted into the frame.
183 */
Ivo van Doornac104462008-06-16 19:54:57 +0200184 if (ieee80211_is_beacon(hdr->frame_control) ||
185 ieee80211_is_probe_resp(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200186 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
187
188 /*
189 * Determine with what IFS priority this frame should be send.
190 * Set ifs to IFS_SIFS when the this is not the first fragment,
191 * or this fragment came after RTS/CTS.
192 */
193 if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
194 txdesc->ifs = IFS_SIFS;
Johannes Berge039fa42008-05-15 12:55:29 +0200195 } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200196 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
197 txdesc->ifs = IFS_BACKOFF;
198 } else {
199 txdesc->ifs = IFS_SIFS;
200 }
201
202 /*
203 * PLCP setup
204 * Length calculation depends on OFDM/CCK rate.
205 */
206 hwrate = rt2x00_get_rate(rate->hw_value);
207 txdesc->signal = hwrate->plcp;
208 txdesc->service = 0x04;
209
210 if (hwrate->flags & DEV_RATE_OFDM) {
211 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
212
213 txdesc->length_high = (data_length >> 6) & 0x3f;
214 txdesc->length_low = data_length & 0x3f;
215 } else {
216 /*
217 * Convert length to microseconds.
218 */
219 residual = get_duration_res(data_length, hwrate->bitrate);
220 duration = get_duration(data_length, hwrate->bitrate);
221
222 if (residual != 0) {
223 duration++;
224
225 /*
226 * Check if we need to set the Length Extension
227 */
228 if (hwrate->bitrate == 110 && residual <= 30)
229 txdesc->service |= 0x80;
230 }
231
232 txdesc->length_high = (duration >> 8) & 0xff;
233 txdesc->length_low = duration & 0xff;
234
235 /*
236 * When preamble is enabled we should set the
237 * preamble bit for the signal.
238 */
239 if (rt2x00_get_rate_preamble(rate->hw_value))
240 txdesc->signal |= 0x08;
241 }
242}
243EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
244
245void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
246 struct txentry_desc *txdesc)
247{
Ivo van Doornb8697672008-06-06 22:53:14 +0200248 struct data_queue *queue = entry->queue;
249 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200250
251 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
252
253 /*
254 * All processing on the frame has been completed, this means
255 * it is now ready to be dumped to userspace through debugfs.
256 */
257 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
258
259 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200260 * Check if we need to kick the queue, there are however a few rules
261 * 1) Don't kick beacon queue
262 * 2) Don't kick unless this is the last in frame in a burst.
263 * When the burst flag is set, this frame is always followed
264 * by another frame which in some way are related to eachother.
265 * This is true for fragments, RTS or CTS-to-self frames.
266 * 3) Rule 2 can be broken when the available entries
267 * in the queue are less then a certain threshold.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200268 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200269 if (entry->queue->qid == QID_BEACON)
270 return;
271
272 if (rt2x00queue_threshold(queue) ||
273 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
274 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200275}
276EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
277
Ivo van Doorn6db37862008-06-06 22:50:28 +0200278int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
279{
280 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
281 struct txentry_desc txdesc;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200282 struct skb_frame_desc *skbdesc;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200283
284 if (unlikely(rt2x00queue_full(queue)))
285 return -EINVAL;
286
287 if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
288 ERROR(queue->rt2x00dev,
289 "Arrived at non-free entry in the non-full queue %d.\n"
290 "Please file bug report to %s.\n",
291 queue->qid, DRV_PROJECT);
292 return -EINVAL;
293 }
294
295 /*
296 * Copy all TX descriptor information into txdesc,
297 * after that we are free to use the skb->cb array
298 * for our information.
299 */
300 entry->skb = skb;
301 rt2x00queue_create_tx_descriptor(entry, &txdesc);
302
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200303 /*
304 * skb->cb array is now ours and we are free to use it.
305 */
306 skbdesc = get_skb_frame_desc(entry->skb);
307 memset(skbdesc, 0, sizeof(*skbdesc));
308 skbdesc->entry = entry;
309
Ivo van Doorn6db37862008-06-06 22:50:28 +0200310 if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
311 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
312 return -EIO;
313 }
314
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200315 if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
316 rt2x00queue_map_txskb(queue->rt2x00dev, skb);
317
Ivo van Doorn6db37862008-06-06 22:50:28 +0200318 __set_bit(ENTRY_DATA_PENDING, &entry->flags);
319
320 rt2x00queue_index_inc(queue, Q_INDEX);
321 rt2x00queue_write_tx_descriptor(entry, &txdesc);
322
323 return 0;
324}
325
Ivo van Doorn181d6902008-02-05 16:42:23 -0500326struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200327 const enum data_queue_qid queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500328{
329 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
330
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200331 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500332 return &rt2x00dev->tx[queue];
333
334 if (!rt2x00dev->bcn)
335 return NULL;
336
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200337 if (queue == QID_BEACON)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500338 return &rt2x00dev->bcn[0];
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200339 else if (queue == QID_ATIM && atim)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500340 return &rt2x00dev->bcn[1];
341
342 return NULL;
343}
344EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
345
346struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
347 enum queue_index index)
348{
349 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100350 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500351
352 if (unlikely(index >= Q_INDEX_MAX)) {
353 ERROR(queue->rt2x00dev,
354 "Entry requested from invalid index type (%d)\n", index);
355 return NULL;
356 }
357
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100358 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500359
360 entry = &queue->entries[queue->index[index]];
361
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100362 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500363
364 return entry;
365}
366EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
367
368void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
369{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100370 unsigned long irqflags;
371
Ivo van Doorn181d6902008-02-05 16:42:23 -0500372 if (unlikely(index >= Q_INDEX_MAX)) {
373 ERROR(queue->rt2x00dev,
374 "Index change on invalid index type (%d)\n", index);
375 return;
376 }
377
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100378 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500379
380 queue->index[index]++;
381 if (queue->index[index] >= queue->limit)
382 queue->index[index] = 0;
383
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100384 if (index == Q_INDEX) {
385 queue->length++;
386 } else if (index == Q_INDEX_DONE) {
387 queue->length--;
388 queue->count ++;
389 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500390
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100391 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500392}
Ivo van Doorn181d6902008-02-05 16:42:23 -0500393
394static void rt2x00queue_reset(struct data_queue *queue)
395{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100396 unsigned long irqflags;
397
398 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500399
400 queue->count = 0;
401 queue->length = 0;
402 memset(queue->index, 0, sizeof(queue->index));
403
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100404 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500405}
406
407void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
408{
409 struct data_queue *queue = rt2x00dev->rx;
410 unsigned int i;
411
412 rt2x00queue_reset(queue);
413
414 if (!rt2x00dev->ops->lib->init_rxentry)
415 return;
416
417 for (i = 0; i < queue->limit; i++)
418 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
419 &queue->entries[i]);
420}
421
422void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
423{
424 struct data_queue *queue;
425 unsigned int i;
426
427 txall_queue_for_each(rt2x00dev, queue) {
428 rt2x00queue_reset(queue);
429
430 if (!rt2x00dev->ops->lib->init_txentry)
431 continue;
432
433 for (i = 0; i < queue->limit; i++)
434 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
435 &queue->entries[i]);
436 }
437}
438
439static int rt2x00queue_alloc_entries(struct data_queue *queue,
440 const struct data_queue_desc *qdesc)
441{
442 struct queue_entry *entries;
443 unsigned int entry_size;
444 unsigned int i;
445
446 rt2x00queue_reset(queue);
447
448 queue->limit = qdesc->entry_num;
Ivo van Doornb8697672008-06-06 22:53:14 +0200449 queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500450 queue->data_size = qdesc->data_size;
451 queue->desc_size = qdesc->desc_size;
452
453 /*
454 * Allocate all queue entries.
455 */
456 entry_size = sizeof(*entries) + qdesc->priv_size;
457 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
458 if (!entries)
459 return -ENOMEM;
460
461#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Adam Baker231be4e2008-02-10 22:48:19 +0100462 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
463 ((__index) * (__psize)) )
Ivo van Doorn181d6902008-02-05 16:42:23 -0500464
465 for (i = 0; i < queue->limit; i++) {
466 entries[i].flags = 0;
467 entries[i].queue = queue;
468 entries[i].skb = NULL;
469 entries[i].entry_idx = i;
470 entries[i].priv_data =
471 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
472 sizeof(*entries), qdesc->priv_size);
473 }
474
475#undef QUEUE_ENTRY_PRIV_OFFSET
476
477 queue->entries = entries;
478
479 return 0;
480}
481
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200482static void rt2x00queue_free_skbs(struct rt2x00_dev *rt2x00dev,
483 struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200484{
485 unsigned int i;
486
487 if (!queue->entries)
488 return;
489
490 for (i = 0; i < queue->limit; i++) {
491 if (queue->entries[i].skb)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200492 rt2x00queue_free_skb(rt2x00dev, queue->entries[i].skb);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200493 }
494}
495
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200496static int rt2x00queue_alloc_rxskbs(struct rt2x00_dev *rt2x00dev,
497 struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200498{
499 unsigned int i;
500 struct sk_buff *skb;
501
502 for (i = 0; i < queue->limit; i++) {
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200503 skb = rt2x00queue_alloc_rxskb(rt2x00dev, &queue->entries[i]);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200504 if (!skb)
Ivo van Doorn61243d82008-06-20 22:10:53 +0200505 return -ENOMEM;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200506 queue->entries[i].skb = skb;
507 }
508
509 return 0;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200510}
511
Ivo van Doorn181d6902008-02-05 16:42:23 -0500512int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
513{
514 struct data_queue *queue;
515 int status;
516
Ivo van Doorn181d6902008-02-05 16:42:23 -0500517 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
518 if (status)
519 goto exit;
520
521 tx_queue_for_each(rt2x00dev, queue) {
522 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
523 if (status)
524 goto exit;
525 }
526
527 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
528 if (status)
529 goto exit;
530
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200531 if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
532 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
533 rt2x00dev->ops->atim);
534 if (status)
535 goto exit;
536 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500537
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200538 status = rt2x00queue_alloc_rxskbs(rt2x00dev, rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500539 if (status)
540 goto exit;
541
542 return 0;
543
544exit:
545 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
546
547 rt2x00queue_uninitialize(rt2x00dev);
548
549 return status;
550}
551
552void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
553{
554 struct data_queue *queue;
555
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200556 rt2x00queue_free_skbs(rt2x00dev, rt2x00dev->rx);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200557
Ivo van Doorn181d6902008-02-05 16:42:23 -0500558 queue_for_each(rt2x00dev, queue) {
559 kfree(queue->entries);
560 queue->entries = NULL;
561 }
562}
563
Ivo van Doorn8f539272008-02-10 22:51:41 +0100564static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
565 struct data_queue *queue, enum data_queue_qid qid)
566{
567 spin_lock_init(&queue->lock);
568
569 queue->rt2x00dev = rt2x00dev;
570 queue->qid = qid;
571 queue->aifs = 2;
572 queue->cw_min = 5;
573 queue->cw_max = 10;
574}
575
Ivo van Doorn181d6902008-02-05 16:42:23 -0500576int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
577{
578 struct data_queue *queue;
579 enum data_queue_qid qid;
580 unsigned int req_atim =
581 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
582
583 /*
584 * We need the following queues:
585 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200586 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -0500587 * Beacon: 1
588 * Atim: 1 (if required)
589 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200590 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500591
592 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
593 if (!queue) {
594 ERROR(rt2x00dev, "Queue allocation failed.\n");
595 return -ENOMEM;
596 }
597
598 /*
599 * Initialize pointers
600 */
601 rt2x00dev->rx = queue;
602 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200603 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Ivo van Doorn181d6902008-02-05 16:42:23 -0500604
605 /*
606 * Initialize queue parameters.
607 * RX: qid = QID_RX
608 * TX: qid = QID_AC_BE + index
609 * TX: cw_min: 2^5 = 32.
610 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +0200611 * BCN: qid = QID_BEACON
612 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -0500613 */
Ivo van Doorn8f539272008-02-10 22:51:41 +0100614 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
615
Ivo van Doorn181d6902008-02-05 16:42:23 -0500616 qid = QID_AC_BE;
Ivo van Doorn8f539272008-02-10 22:51:41 +0100617 tx_queue_for_each(rt2x00dev, queue)
618 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500619
Ivo van Doorn565a0192008-06-03 20:29:05 +0200620 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500621 if (req_atim)
Ivo van Doorn565a0192008-06-03 20:29:05 +0200622 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500623
624 return 0;
625}
626
627void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
628{
629 kfree(rt2x00dev->rx);
630 rt2x00dev->rx = NULL;
631 rt2x00dev->tx = NULL;
632 rt2x00dev->bcn = NULL;
633}