rt2x00: Upgrade queue->lock to use irqsave
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00queue.c
1 /*
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
32 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
33                                          const unsigned int queue)
34 {
35         int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
36
37         if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
38                 return &rt2x00dev->tx[queue];
39
40         if (!rt2x00dev->bcn)
41                 return NULL;
42
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];
47
48         return NULL;
49 }
50 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
51
52 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
53                                           enum queue_index index)
54 {
55         struct queue_entry *entry;
56         unsigned long irqflags;
57
58         if (unlikely(index >= Q_INDEX_MAX)) {
59                 ERROR(queue->rt2x00dev,
60                       "Entry requested from invalid index type (%d)\n", index);
61                 return NULL;
62         }
63
64         spin_lock_irqsave(&queue->lock, irqflags);
65
66         entry = &queue->entries[queue->index[index]];
67
68         spin_unlock_irqrestore(&queue->lock, irqflags);
69
70         return entry;
71 }
72 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
73
74 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
75 {
76         unsigned long irqflags;
77
78         if (unlikely(index >= Q_INDEX_MAX)) {
79                 ERROR(queue->rt2x00dev,
80                       "Index change on invalid index type (%d)\n", index);
81                 return;
82         }
83
84         spin_lock_irqsave(&queue->lock, irqflags);
85
86         queue->index[index]++;
87         if (queue->index[index] >= queue->limit)
88                 queue->index[index] = 0;
89
90         if (index == Q_INDEX) {
91                 queue->length++;
92         } else if (index == Q_INDEX_DONE) {
93                 queue->length--;
94                 queue->count ++;
95         }
96
97         spin_unlock_irqrestore(&queue->lock, irqflags);
98 }
99 EXPORT_SYMBOL_GPL(rt2x00queue_index_inc);
100
101 static void rt2x00queue_reset(struct data_queue *queue)
102 {
103         unsigned long irqflags;
104
105         spin_lock_irqsave(&queue->lock, irqflags);
106
107         queue->count = 0;
108         queue->length = 0;
109         memset(queue->index, 0, sizeof(queue->index));
110
111         spin_unlock_irqrestore(&queue->lock, irqflags);
112 }
113
114 void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev)
115 {
116         struct data_queue *queue = rt2x00dev->rx;
117         unsigned int i;
118
119         rt2x00queue_reset(queue);
120
121         if (!rt2x00dev->ops->lib->init_rxentry)
122                 return;
123
124         for (i = 0; i < queue->limit; i++)
125                 rt2x00dev->ops->lib->init_rxentry(rt2x00dev,
126                                                   &queue->entries[i]);
127 }
128
129 void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev)
130 {
131         struct data_queue *queue;
132         unsigned int i;
133
134         txall_queue_for_each(rt2x00dev, queue) {
135                 rt2x00queue_reset(queue);
136
137                 if (!rt2x00dev->ops->lib->init_txentry)
138                         continue;
139
140                 for (i = 0; i < queue->limit; i++)
141                         rt2x00dev->ops->lib->init_txentry(rt2x00dev,
142                                                           &queue->entries[i]);
143         }
144 }
145
146 static int rt2x00queue_alloc_entries(struct data_queue *queue,
147                                      const struct data_queue_desc *qdesc)
148 {
149         struct queue_entry *entries;
150         unsigned int entry_size;
151         unsigned int i;
152
153         rt2x00queue_reset(queue);
154
155         queue->limit = qdesc->entry_num;
156         queue->data_size = qdesc->data_size;
157         queue->desc_size = qdesc->desc_size;
158
159         /*
160          * Allocate all queue entries.
161          */
162         entry_size = sizeof(*entries) + qdesc->priv_size;
163         entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
164         if (!entries)
165                 return -ENOMEM;
166
167 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
168         ( ((char *)(__base)) + ((__limit) * (__esize)) + \
169             ((__index) * (__psize)) )
170
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);
179         }
180
181 #undef QUEUE_ENTRY_PRIV_OFFSET
182
183         queue->entries = entries;
184
185         return 0;
186 }
187
188 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
189 {
190         struct data_queue *queue;
191         int status;
192
193
194         status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
195         if (status)
196                 goto exit;
197
198         tx_queue_for_each(rt2x00dev, queue) {
199                 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
200                 if (status)
201                         goto exit;
202         }
203
204         status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
205         if (status)
206                 goto exit;
207
208         if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
209                 return 0;
210
211         status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
212                                            rt2x00dev->ops->atim);
213         if (status)
214                 goto exit;
215
216         return 0;
217
218 exit:
219         ERROR(rt2x00dev, "Queue entries allocation failed.\n");
220
221         rt2x00queue_uninitialize(rt2x00dev);
222
223         return status;
224 }
225
226 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
227 {
228         struct data_queue *queue;
229
230         queue_for_each(rt2x00dev, queue) {
231                 kfree(queue->entries);
232                 queue->entries = NULL;
233         }
234 }
235
236 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
237                              struct data_queue *queue, enum data_queue_qid qid)
238 {
239         spin_lock_init(&queue->lock);
240
241         queue->rt2x00dev = rt2x00dev;
242         queue->qid = qid;
243         queue->aifs = 2;
244         queue->cw_min = 5;
245         queue->cw_max = 10;
246 }
247
248 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
249 {
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);
254
255         /*
256          * We need the following queues:
257          * RX: 1
258          * TX: hw->queues
259          * Beacon: 1
260          * Atim: 1 (if required)
261          */
262         rt2x00dev->data_queues = 2 + rt2x00dev->hw->queues + req_atim;
263
264         queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
265         if (!queue) {
266                 ERROR(rt2x00dev, "Queue allocation failed.\n");
267                 return -ENOMEM;
268         }
269
270         /*
271          * Initialize pointers
272          */
273         rt2x00dev->rx = queue;
274         rt2x00dev->tx = &queue[1];
275         rt2x00dev->bcn = &queue[1 + rt2x00dev->hw->queues];
276
277         /*
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
284          */
285         rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
286
287         qid = QID_AC_BE;
288         tx_queue_for_each(rt2x00dev, queue)
289                 rt2x00queue_init(rt2x00dev, queue, qid++);
290
291         rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_MGMT);
292         if (req_atim)
293                 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_MGMT);
294
295         return 0;
296 }
297
298 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
299 {
300         kfree(rt2x00dev->rx);
301         rt2x00dev->rx = NULL;
302         rt2x00dev->tx = NULL;
303         rt2x00dev->bcn = NULL;
304 }