iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / net / wireless / rt2x00 / rt2x00queue.c
blob659e9f44c40c76e1bac4a4c05fab2c342ff6d161
1 /*
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.
22 Module: rt2x00lib
23 Abstract: rt2x00 queue specific routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
32 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
33 const unsigned int queue)
35 int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
37 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
38 return &rt2x00dev->tx[queue];
40 if (!rt2x00dev->bcn)
41 return NULL;
43 if (queue == RT2X00_BCN_QUEUE_BEACON)
44 return &rt2x00dev->bcn[0];
45 else if (queue == RT2X00_BCN_QUEUE_ATIM && atim)
46 return &rt2x00dev->bcn[1];
48 return NULL;
50 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
52 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
53 enum queue_index index)
55 struct queue_entry *entry;
56 unsigned long irqflags;
58 if (unlikely(index >= Q_INDEX_MAX)) {
59 ERROR(queue->rt2x00dev,
60 "Entry requested from invalid index type (%d)\n", index);
61 return NULL;
64 spin_lock_irqsave(&queue->lock, irqflags);
66 entry = &queue->entries[queue->index[index]];
68 spin_unlock_irqrestore(&queue->lock, irqflags);
70 return entry;
72 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
74 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
76 unsigned long irqflags;
78 if (unlikely(index >= Q_INDEX_MAX)) {
79 ERROR(queue->rt2x00dev,
80 "Index change on invalid index type (%d)\n", index);
81 return;
84 spin_lock_irqsave(&queue->lock, irqflags);
86 queue->index[index]++;
87 if (queue->index[index] >= queue->limit)
88 queue->index[index] = 0;
90 if (index == Q_INDEX) {
91 queue->length++;
92 } else if (index == Q_INDEX_DONE) {
93 queue->length--;
94 queue->count ++;
97 spin_unlock_irqrestore(&queue->lock, irqflags);
99 EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
101 static void rt2x00queue_reset(struct data_queue *queue)
103 unsigned long irqflags;
105 spin_lock_irqsave(&queue->lock, irqflags);
107 queue->count = 0;
108 queue->length = 0;
109 memset(queue->index, 0, sizeof(queue->index));
111 spin_unlock_irqrestore(&queue->lock, irqflags);
114 void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
116 struct data_queue *queue = rt2x00dev->rx;
117 unsigned int i;
119 rt2x00queue_reset(queue);
121 if (!rt2x00dev->ops->lib->init_rxentry)
122 return;
124 for (i = 0; i < queue->limit; i++)
125 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
126 &queue->entries[i]);
129 void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
131 struct data_queue *queue;
132 unsigned int i;
134 txall_queue_for_each(rt2x00dev, queue) {
135 rt2x00queue_reset(queue);
137 if (!rt2x00dev->ops->lib->init_txentry)
138 continue;
140 for (i = 0; i < queue->limit; i++)
141 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
142 &queue->entries[i]);
146 static int rt2x00queue_alloc_entries(struct data_queue *queue,
147 const struct data_queue_desc *qdesc)
149 struct queue_entry *entries;
150 unsigned int entry_size;
151 unsigned int i;
153 rt2x00queue_reset(queue);
155 queue->limit = qdesc->entry_num;
156 queue->data_size = qdesc->data_size;
157 queue->desc_size = qdesc->desc_size;
160 * Allocate all queue entries.
162 entry_size = sizeof(*entries) + qdesc->priv_size;
163 entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
164 if (!entries)
165 return -ENOMEM;
167 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
168 ( ((char *)(__base)) + ((__limit) * (__esize)) + \
169 ((__index) * (__psize)) )
171 for (i = 0; i < queue->limit; i++) {
172 entries[i].flags = 0;
173 entries[i].queue = queue;
174 entries[i].skb = NULL;
175 entries[i].entry_idx = i;
176 entries[i].priv_data =
177 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
178 sizeof(*entries), qdesc->priv_size);
181 #undef QUEUE_ENTRY_PRIV_OFFSET
183 queue->entries = entries;
185 return 0;
188 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
190 struct data_queue *queue;
191 int status;
194 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
195 if (status)
196 goto exit;
198 tx_queue_for_each(rt2x00dev, queue) {
199 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
200 if (status)
201 goto exit;
204 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
205 if (status)
206 goto exit;
208 if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
209 return 0;
211 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
212 rt2x00dev->ops->atim);
213 if (status)
214 goto exit;
216 return 0;
218 exit:
219 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
221 rt2x00queue_uninitialize(rt2x00dev);
223 return status;
226 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
228 struct data_queue *queue;
230 queue_for_each(rt2x00dev, queue) {
231 kfree(queue->entries);
232 queue->entries = NULL;
236 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
237 struct data_queue *queue, enum data_queue_qid qid)
239 spin_lock_init(&queue->lock);
241 queue->rt2x00dev = rt2x00dev;
242 queue->qid = qid;
243 queue->aifs = 2;
244 queue->cw_min = 5;
245 queue->cw_max = 10;
248 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
250 struct data_queue *queue;
251 enum data_queue_qid qid;
252 unsigned int req_atim =
253 !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
256 * We need the following queues:
257 * RX: 1
258 * TX: hw->queues
259 * Beacon: 1
260 * Atim: 1 (if required)
262 rt2x00dev->data_queues = 2 + rt2x00dev->hw->queues + req_atim;
264 queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
265 if (!queue) {
266 ERROR(rt2x00dev, "Queue allocation failed.\n");
267 return -ENOMEM;
271 * Initialize pointers
273 rt2x00dev->rx = queue;
274 rt2x00dev->tx = &queue[1];
275 rt2x00dev->bcn = &queue[1 + rt2x00dev->hw->queues];
278 * Initialize queue parameters.
279 * RX: qid = QID_RX
280 * TX: qid = QID_AC_BE + index
281 * TX: cw_min: 2^5 = 32.
282 * TX: cw_max: 2^10 = 1024.
283 * BCN & Atim: qid = QID_MGMT
285 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
287 qid = QID_AC_BE;
288 tx_queue_for_each(rt2x00dev, queue)
289 rt2x00queue_init(rt2x00dev, queue, qid++);
291 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_MGMT);
292 if (req_atim)
293 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_MGMT);
295 return 0;
298 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
300 kfree(rt2x00dev->rx);
301 rt2x00dev->rx = NULL;
302 rt2x00dev->tx = NULL;
303 rt2x00dev->bcn = NULL;