blob: 19c10629c767513ca510e66c82732ce5e91584da [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,
33 struct txentry_desc *txdesc,
34 struct ieee80211_tx_control *control)
35{
36 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
37 struct ieee80211_rate *rate = control->tx_rate;
38 const struct rt2x00_rate *hwrate;
39 unsigned int data_length;
40 unsigned int duration;
41 unsigned int residual;
42 u16 frame_control;
43
44 memset(txdesc, 0, sizeof(*txdesc));
45
46 /*
47 * Initialize information from queue
48 */
49 txdesc->queue = entry->queue->qid;
50 txdesc->cw_min = entry->queue->cw_min;
51 txdesc->cw_max = entry->queue->cw_max;
52 txdesc->aifs = entry->queue->aifs;
53
54 /* Data length should be extended with 4 bytes for CRC */
55 data_length = entry->skb->len + 4;
56
57 /*
58 * Read required fields from ieee80211 header.
59 */
60 frame_control = le16_to_cpu(hdr->frame_control);
61
62 /*
63 * Check whether this frame is to be acked.
64 */
65 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
66 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
67
68 /*
69 * Check if this is a RTS/CTS frame
70 */
71 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
72 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
73 if (is_rts_frame(frame_control)) {
74 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
75 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
76 } else {
77 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
78 __clear_bit(ENTRY_TXD_ACK, &txdesc->flags);
79 }
80 if (control->rts_cts_rate)
81 rate = control->rts_cts_rate;
82 }
83
84 /*
85 * Determine retry information.
86 */
87 txdesc->retry_limit = control->retry_limit;
88 if (control->flags & IEEE80211_TXCTL_LONG_RETRY_LIMIT)
89 __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;
113 } else if (control->flags & IEEE80211_TXCTL_FIRST_FRAGMENT) {
114 __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,
179 * if this entry is a RTS of CTS-to-self frame we are done,
180 * otherwise we need to kick the queue.
181 */
182 if (rt2x00dev->ops->lib->kick_tx_queue &&
183 !(skbdesc->flags & FRAME_DESC_DRIVER_GENERATED))
184 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev,
185 entry->queue->qid);
186}
187EXPORT_SYMBOL_GPL(rt2x00queue_write_tx_descriptor);
188
Ivo van Doorn181d6902008-02-05 16:42:23 -0500189struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200190 const enum data_queue_qid queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500191{
192 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
193
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200194 if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500195 return &rt2x00dev->tx[queue];
196
197 if (!rt2x00dev->bcn)
198 return NULL;
199
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200200 if (queue == QID_BEACON)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500201 return &rt2x00dev->bcn[0];
Ivo van Doorne58c6ac2008-04-21 19:00:47 +0200202 else if (queue == QID_ATIM && atim)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500203 return &rt2x00dev->bcn[1];
204
205 return NULL;
206}
207EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
208
209struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
210 enum queue_index index)
211{
212 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100213 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500214
215 if (unlikely(index >= Q_INDEX_MAX)) {
216 ERROR(queue->rt2x00dev,
217 "Entry requested from invalid index type (%d)\n", index);
218 return NULL;
219 }
220
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100221 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500222
223 entry = &queue->entries[queue->index[index]];
224
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100225 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500226
227 return entry;
228}
229EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
230
231void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
232{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100233 unsigned long irqflags;
234
Ivo van Doorn181d6902008-02-05 16:42:23 -0500235 if (unlikely(index >= Q_INDEX_MAX)) {
236 ERROR(queue->rt2x00dev,
237 "Index change on invalid index type (%d)\n", index);
238 return;
239 }
240
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100241 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500242
243 queue->index[index]++;
244 if (queue->index[index] >= queue->limit)
245 queue->index[index] = 0;
246
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100247 if (index == Q_INDEX) {
248 queue->length++;
249 } else if (index == Q_INDEX_DONE) {
250 queue->length--;
251 queue->count ++;
252 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500253
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100254 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500255}
256EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
257
258static void rt2x00queue_reset(struct data_queue *queue)
259{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100260 unsigned long irqflags;
261
262 spin_lock_irqsave(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500263
264 queue->count = 0;
265 queue->length = 0;
266 memset(queue->index, 0, sizeof(queue->index));
267
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100268 spin_unlock_irqrestore(&queue->lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500269}
270
271void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
272{
273 struct data_queue *queue = rt2x00dev->rx;
274 unsigned int i;
275
276 rt2x00queue_reset(queue);
277
278 if (!rt2x00dev->ops->lib->init_rxentry)
279 return;
280
281 for (i = 0; i < queue->limit; i++)
282 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
283 &queue->entries[i]);
284}
285
286void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
287{
288 struct data_queue *queue;
289 unsigned int i;
290
291 txall_queue_for_each(rt2x00dev, queue) {
292 rt2x00queue_reset(queue);
293
294 if (!rt2x00dev->ops->lib->init_txentry)
295 continue;
296
297 for (i = 0; i < queue->limit; i++)
298 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
299 &queue->entries[i]);
300 }
301}
302
303static int rt2x00queue_alloc_entries(struct data_queue *queue,
304 const struct data_queue_desc *qdesc)
305{
306 struct queue_entry *entries;
307 unsigned int entry_size;
308 unsigned int i;
309
310 rt2x00queue_reset(queue);
311
312 queue->limit = qdesc->entry_num;
313 queue->data_size = qdesc->data_size;
314 queue->desc_size = qdesc->desc_size;
315
316 /*
317 * Allocate all queue entries.
318 */
319 entry_size = sizeof(*entries) + qdesc->priv_size;
320 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
321 if (!entries)
322 return -ENOMEM;
323
324#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Adam Baker231be4e2008-02-10 22:48:19 +0100325 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
326 ((__index) * (__psize)) )
Ivo van Doorn181d6902008-02-05 16:42:23 -0500327
328 for (i = 0; i < queue->limit; i++) {
329 entries[i].flags = 0;
330 entries[i].queue = queue;
331 entries[i].skb = NULL;
332 entries[i].entry_idx = i;
333 entries[i].priv_data =
334 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
335 sizeof(*entries), qdesc->priv_size);
336 }
337
338#undef QUEUE_ENTRY_PRIV_OFFSET
339
340 queue->entries = entries;
341
342 return 0;
343}
344
345int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
346{
347 struct data_queue *queue;
348 int status;
349
350
351 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
352 if (status)
353 goto exit;
354
355 tx_queue_for_each(rt2x00dev, queue) {
356 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
357 if (status)
358 goto exit;
359 }
360
361 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
362 if (status)
363 goto exit;
364
365 if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
366 return 0;
367
368 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
369 rt2x00dev->ops->atim);
370 if (status)
371 goto exit;
372
373 return 0;
374
375exit:
376 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
377
378 rt2x00queue_uninitialize(rt2x00dev);
379
380 return status;
381}
382
383void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
384{
385 struct data_queue *queue;
386
387 queue_for_each(rt2x00dev, queue) {
388 kfree(queue->entries);
389 queue->entries = NULL;
390 }
391}
392
Ivo van Doorn8f539272008-02-10 22:51:41 +0100393static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
394 struct data_queue *queue, enum data_queue_qid qid)
395{
396 spin_lock_init(&queue->lock);
397
398 queue->rt2x00dev = rt2x00dev;
399 queue->qid = qid;
400 queue->aifs = 2;
401 queue->cw_min = 5;
402 queue->cw_max = 10;
403}
404
Ivo van Doorn181d6902008-02-05 16:42:23 -0500405int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
406{
407 struct data_queue *queue;
408 enum data_queue_qid qid;
409 unsigned int req_atim =
410 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
411
412 /*
413 * We need the following queues:
414 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200415 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -0500416 * Beacon: 1
417 * Atim: 1 (if required)
418 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200419 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500420
421 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
422 if (!queue) {
423 ERROR(rt2x00dev, "Queue allocation failed.\n");
424 return -ENOMEM;
425 }
426
427 /*
428 * Initialize pointers
429 */
430 rt2x00dev->rx = queue;
431 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200432 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Ivo van Doorn181d6902008-02-05 16:42:23 -0500433
434 /*
435 * Initialize queue parameters.
436 * RX: qid = QID_RX
437 * TX: qid = QID_AC_BE + index
438 * TX: cw_min: 2^5 = 32.
439 * TX: cw_max: 2^10 = 1024.
440 * BCN & Atim: qid = QID_MGMT
441 */
Ivo van Doorn8f539272008-02-10 22:51:41 +0100442 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
443
Ivo van Doorn181d6902008-02-05 16:42:23 -0500444 qid = QID_AC_BE;
Ivo van Doorn8f539272008-02-10 22:51:41 +0100445 tx_queue_for_each(rt2x00dev, queue)
446 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500447
Ivo van Doorn8f539272008-02-10 22:51:41 +0100448 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_MGMT);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500449 if (req_atim)
Ivo van Doorn8f539272008-02-10 22:51:41 +0100450 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_MGMT);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500451
452 return 0;
453}
454
455void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
456{
457 kfree(rt2x00dev->rx);
458 rt2x00dev->rx = NULL;
459 rt2x00dev->tx = NULL;
460 rt2x00dev->bcn = NULL;
461}