blob: f875b7ab09fe9bc741eb5885bc38dd1d03b57c0b [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>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
Ivo van Doorn7050ec82008-05-10 13:46:13 +020032void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
Johannes Berge039fa42008-05-15 12:55:29 +020033 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +020034{
Johannes Berg2e92e6f2008-05-15 12:55:27 +020035 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Johannes Berge039fa42008-05-15 12:55:29 +020036 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
Ivo van Doorn7050ec82008-05-10 13:46:13 +020037 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020038 struct ieee80211_rate *rate =
Johannes Berge039fa42008-05-15 12:55:29 +020039 ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +020040 const struct rt2x00_rate *hwrate;
41 unsigned int data_length;
42 unsigned int duration;
43 unsigned int residual;
44 u16 frame_control;
45
46 memset(txdesc, 0, sizeof(*txdesc));
47
48 /*
49 * Initialize information from queue
50 */
51 txdesc->queue = entry->queue->qid;
52 txdesc->cw_min = entry->queue->cw_min;
53 txdesc->cw_max = entry->queue->cw_max;
54 txdesc->aifs = entry->queue->aifs;
55
56 /* Data length should be extended with 4 bytes for CRC */
57 data_length = entry->skb->len + 4;
58
59 /*
60 * Read required fields from ieee80211 header.
61 */
62 frame_control = le16_to_cpu(hdr->frame_control);
63
64 /*
65 * Check whether this frame is to be acked.
66 */
Johannes Berge039fa42008-05-15 12:55:29 +020067 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +020068 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
69
70 /*
71 * Check if this is a RTS/CTS frame
72 */
73 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
74 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +020075 if (is_rts_frame(frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +020076 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +020077 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +020078 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +020079 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +020080 rate =
Johannes Berge039fa42008-05-15 12:55:29 +020081 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +020082 }
83
84 /*
85 * Determine retry information.
86 */
Johannes Berge039fa42008-05-15 12:55:29 +020087 txdesc->retry_limit = tx_info->control.retry_limit;
88 if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT)
Ivo van Doorn7050ec82008-05-10 13:46:13 +020089 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
90
91 /*
92 * Check if more fragments are pending
93 */
94 if (ieee80211_get_morefrag(hdr)) {
95 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
96 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
97 }
98
99 /*
100 * Beacons and probe responses require the tsf timestamp
101 * to be inserted into the frame.
102 */
103 if (txdesc->queue == QID_BEACON || is_probe_resp(frame_control))
104 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
105
106 /*
107 * Determine with what IFS priority this frame should be send.
108 * Set ifs to IFS_SIFS when the this is not the first fragment,
109 * or this fragment came after RTS/CTS.
110 */
111 if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
112 txdesc->ifs = IFS_SIFS;
Johannes Berge039fa42008-05-15 12:55:29 +0200113 } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200114 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
115 txdesc->ifs = IFS_BACKOFF;
116 } else {
117 txdesc->ifs = IFS_SIFS;
118 }
119
120 /*
121 * PLCP setup
122 * Length calculation depends on OFDM/CCK rate.
123 */
124 hwrate = rt2x00_get_rate(rate->hw_value);
125 txdesc->signal = hwrate->plcp;
126 txdesc->service = 0x04;
127
128 if (hwrate->flags & DEV_RATE_OFDM) {
129 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
130
131 txdesc->length_high = (data_length >> 6) & 0x3f;
132 txdesc->length_low = data_length & 0x3f;
133 } else {
134 /*
135 * Convert length to microseconds.
136 */
137 residual = get_duration_res(data_length, hwrate->bitrate);
138 duration = get_duration(data_length, hwrate->bitrate);
139
140 if (residual != 0) {
141 duration++;
142
143 /*
144 * Check if we need to set the Length Extension
145 */
146 if (hwrate->bitrate == 110 && residual <= 30)
147 txdesc->service |= 0x80;
148 }
149
150 txdesc->length_high = (duration >> 8) & 0xff;
151 txdesc->length_low = duration & 0xff;
152
153 /*
154 * When preamble is enabled we should set the
155 * preamble bit for the signal.
156 */
157 if (rt2x00_get_rate_preamble(rate->hw_value))
158 txdesc->signal |= 0x08;
159 }
160}
161EXPORT_SYMBOL_GPL(rt2x00queue_create_tx_descriptor);
162
163void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
164 struct txentry_desc *txdesc)
165{
166 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
167 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
168
169 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
170
171 /*
172 * All processing on the frame has been completed, this means
173 * it is now ready to be dumped to userspace through debugfs.
174 */
175 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
176
177 /*
178 * We are done writing the frame to the queue entry,
Johannes Berge039fa42008-05-15 12:55:29 +0200179 * also kick the queue in case the correct flags are set,
180 * note that this will automatically filter beacons and
181 * RTS/CTS frames since those frames don't have this flag
182 * set.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200183 */
184 if (rt2x00dev->ops->lib->kick_tx_queue &&
185 !(skbdesc->flags & FRAME_DESC_DRIVER_GENERATED))
186 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev,
187 entry->queue->qid);
188}
189EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
190
Ivo van Doorn6db37862008-06-06 22:50:28 +0200191int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
192{
193 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
194 struct txentry_desc txdesc;
195
196 if (unlikely(rt2x00queue_full(queue)))
197 return -EINVAL;
198
199 if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
200 ERROR(queue->rt2x00dev,
201 "Arrived at non-free entry in the non-full queue %d.\n"
202 "Please file bug report to %s.\n",
203 queue->qid, DRV_PROJECT);
204 return -EINVAL;
205 }
206
207 /*
208 * Copy all TX descriptor information into txdesc,
209 * after that we are free to use the skb->cb array
210 * for our information.
211 */
212 entry->skb = skb;
213 rt2x00queue_create_tx_descriptor(entry, &txdesc);
214
215 if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
216 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
217 return -EIO;
218 }
219
220 __set_bit(ENTRY_DATA_PENDING, &entry->flags);
221
222 rt2x00queue_index_inc(queue, Q_INDEX);
223 rt2x00queue_write_tx_descriptor(entry, &txdesc);
224
225 return 0;
226}
227
Ivo van Doorn181d6902008-02-05 16:42:23 -0500228struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200229 const enum data_queue_qid queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500230{
231 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
232
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200233 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500234 return &rt2x00dev->tx[queue];
235
236 if (!rt2x00dev->bcn)
237 return NULL;
238
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200239 if (queue == QID_BEACON)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500240 return &rt2x00dev->bcn[0];
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200241 else if (queue == QID_ATIM && atim)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500242 return &rt2x00dev->bcn[1];
243
244 return NULL;
245}
246EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
247
248struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
249 enum queue_index index)
250{
251 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100252 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500253
254 if (unlikely(index >= Q_INDEX_MAX)) {
255 ERROR(queue->rt2x00dev,
256 "Entry requested from invalid index type (%d)\n", index);
257 return NULL;
258 }
259
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100260 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500261
262 entry = &queue->entries[queue->index[index]];
263
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100264 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500265
266 return entry;
267}
268EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
269
270void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
271{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100272 unsigned long irqflags;
273
Ivo van Doorn181d6902008-02-05 16:42:23 -0500274 if (unlikely(index >= Q_INDEX_MAX)) {
275 ERROR(queue->rt2x00dev,
276 "Index change on invalid index type (%d)\n", index);
277 return;
278 }
279
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100280 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500281
282 queue->index[index]++;
283 if (queue->index[index] >= queue->limit)
284 queue->index[index] = 0;
285
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100286 if (index == Q_INDEX) {
287 queue->length++;
288 } else if (index == Q_INDEX_DONE) {
289 queue->length--;
290 queue->count ++;
291 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500292
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100293 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500294}
295EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
296
297static void rt2x00queue_reset(struct data_queue *queue)
298{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100299 unsigned long irqflags;
300
301 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500302
303 queue->count = 0;
304 queue->length = 0;
305 memset(queue->index, 0, sizeof(queue->index));
306
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100307 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500308}
309
310void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
311{
312 struct data_queue *queue = rt2x00dev->rx;
313 unsigned int i;
314
315 rt2x00queue_reset(queue);
316
317 if (!rt2x00dev->ops->lib->init_rxentry)
318 return;
319
320 for (i = 0; i < queue->limit; i++)
321 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
322 &queue->entries[i]);
323}
324
325void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
326{
327 struct data_queue *queue;
328 unsigned int i;
329
330 txall_queue_for_each(rt2x00dev, queue) {
331 rt2x00queue_reset(queue);
332
333 if (!rt2x00dev->ops->lib->init_txentry)
334 continue;
335
336 for (i = 0; i < queue->limit; i++)
337 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
338 &queue->entries[i]);
339 }
340}
341
342static int rt2x00queue_alloc_entries(struct data_queue *queue,
343 const struct data_queue_desc *qdesc)
344{
345 struct queue_entry *entries;
346 unsigned int entry_size;
347 unsigned int i;
348
349 rt2x00queue_reset(queue);
350
351 queue->limit = qdesc->entry_num;
352 queue->data_size = qdesc->data_size;
353 queue->desc_size = qdesc->desc_size;
354
355 /*
356 * Allocate all queue entries.
357 */
358 entry_size = sizeof(*entries) + qdesc->priv_size;
359 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
360 if (!entries)
361 return -ENOMEM;
362
363#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Adam Baker231be4e2008-02-10 22:48:19 +0100364 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
365 ((__index) * (__psize)) )
Ivo van Doorn181d6902008-02-05 16:42:23 -0500366
367 for (i = 0; i < queue->limit; i++) {
368 entries[i].flags = 0;
369 entries[i].queue = queue;
370 entries[i].skb = NULL;
371 entries[i].entry_idx = i;
372 entries[i].priv_data =
373 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
374 sizeof(*entries), qdesc->priv_size);
375 }
376
377#undef QUEUE_ENTRY_PRIV_OFFSET
378
379 queue->entries = entries;
380
381 return 0;
382}
383
384int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
385{
386 struct data_queue *queue;
387 int status;
388
389
390 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
391 if (status)
392 goto exit;
393
394 tx_queue_for_each(rt2x00dev, queue) {
395 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
396 if (status)
397 goto exit;
398 }
399
400 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
401 if (status)
402 goto exit;
403
404 if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
405 return 0;
406
407 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
408 rt2x00dev->ops->atim);
409 if (status)
410 goto exit;
411
412 return 0;
413
414exit:
415 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
416
417 rt2x00queue_uninitialize(rt2x00dev);
418
419 return status;
420}
421
422void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
423{
424 struct data_queue *queue;
425
426 queue_for_each(rt2x00dev, queue) {
427 kfree(queue->entries);
428 queue->entries = NULL;
429 }
430}
431
Ivo van Doorn8f539272008-02-10 22:51:41 +0100432static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
433 struct data_queue *queue, enum data_queue_qid qid)
434{
435 spin_lock_init(&queue->lock);
436
437 queue->rt2x00dev = rt2x00dev;
438 queue->qid = qid;
439 queue->aifs = 2;
440 queue->cw_min = 5;
441 queue->cw_max = 10;
442}
443
Ivo van Doorn181d6902008-02-05 16:42:23 -0500444int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
445{
446 struct data_queue *queue;
447 enum data_queue_qid qid;
448 unsigned int req_atim =
449 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
450
451 /*
452 * We need the following queues:
453 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200454 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -0500455 * Beacon: 1
456 * Atim: 1 (if required)
457 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200458 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500459
460 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
461 if (!queue) {
462 ERROR(rt2x00dev, "Queue allocation failed.\n");
463 return -ENOMEM;
464 }
465
466 /*
467 * Initialize pointers
468 */
469 rt2x00dev->rx = queue;
470 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200471 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Ivo van Doorn181d6902008-02-05 16:42:23 -0500472
473 /*
474 * Initialize queue parameters.
475 * RX: qid = QID_RX
476 * TX: qid = QID_AC_BE + index
477 * TX: cw_min: 2^5 = 32.
478 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +0200479 * BCN: qid = QID_BEACON
480 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -0500481 */
Ivo van Doorn8f539272008-02-10 22:51:41 +0100482 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
483
Ivo van Doorn181d6902008-02-05 16:42:23 -0500484 qid = QID_AC_BE;
Ivo van Doorn8f539272008-02-10 22:51:41 +0100485 tx_queue_for_each(rt2x00dev, queue)
486 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500487
Ivo van Doorn565a0192008-06-03 20:29:05 +0200488 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500489 if (req_atim)
Ivo van Doorn565a0192008-06-03 20:29:05 +0200490 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500491
492 return 0;
493}
494
495void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
496{
497 kfree(rt2x00dev->rx);
498 rt2x00dev->rx = NULL;
499 rt2x00dev->tx = NULL;
500 rt2x00dev->bcn = NULL;
501}