rt2x00: Add per-interface structure
[linux-flexiantxendom0-natty.git] / drivers / net / wireless / rt2x00 / rt2x00queue.h
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: rt2x00
23         Abstract: rt2x00 queue datastructures and routines
24  */
25
26 #ifndef RT2X00QUEUE_H
27 #define RT2X00QUEUE_H
28
29 #include <linux/prefetch.h>
30
31 /**
32  * DOC: Entrie frame size
33  *
34  * Ralink PCI devices demand the Frame size to be a multiple of 128 bytes,
35  * for USB devices this restriction does not apply, but the value of
36  * 2432 makes sense since it is big enough to contain the maximum fragment
37  * size according to the ieee802.11 specs.
38  */
39 #define DATA_FRAME_SIZE 2432
40 #define MGMT_FRAME_SIZE 256
41
42 /**
43  * DOC: Number of entries per queue
44  *
45  * After research it was concluded that 12 entries in a RX and TX
46  * queue would be sufficient. Although this is almost one third of
47  * the amount the legacy driver allocated, the queues aren't getting
48  * filled to the maximum even when working with the maximum rate.
49  */
50 #define RX_ENTRIES      12
51 #define TX_ENTRIES      12
52 #define BEACON_ENTRIES  1
53 #define ATIM_ENTRIES    1
54
55 /**
56  * enum data_queue_qid: Queue identification
57  */
58 enum data_queue_qid {
59         QID_AC_BE = 0,
60         QID_AC_BK = 1,
61         QID_AC_VI = 2,
62         QID_AC_VO = 3,
63         QID_HCCA = 4,
64         QID_MGMT = 13,
65         QID_RX = 14,
66         QID_OTHER = 15,
67 };
68
69 /**
70  * struct skb_frame_desc: Descriptor information for the skb buffer
71  *
72  * This structure is placed over the skb->cb array, this means that
73  * this structure should not exceed the size of that array (48 bytes).
74  *
75  * @flags: Frame flags.
76  * @frame_type: Frame type, see &enum rt2x00_dump_type.
77  * @data: Pointer to data part of frame (Start of ieee80211 header).
78  * @desc: Pointer to descriptor part of the frame.
79  *      Note that this pointer could point to something outside
80  *      of the scope of the skb->data pointer.
81  * @data_len: Length of the frame data.
82  * @desc_len: Length of the frame descriptor.
83
84  * @entry: The entry to which this sk buffer belongs.
85  */
86 struct skb_frame_desc {
87         unsigned int flags;
88
89         unsigned int frame_type;
90
91         void *data;
92         void *desc;
93
94         unsigned int data_len;
95         unsigned int desc_len;
96
97         struct queue_entry *entry;
98 };
99
100 static inline struct skb_frame_desc* get_skb_frame_desc(struct sk_buff *skb)
101 {
102         BUILD_BUG_ON(sizeof(struct skb_frame_desc) > sizeof(skb->cb));
103         return (struct skb_frame_desc *)&skb->cb[0];
104 }
105
106 /**
107  * struct rxdone_entry_desc: RX Entry descriptor
108  *
109  * Summary of information that has been read from the RX frame descriptor.
110  *
111  * @signal: Signal of the received frame.
112  * @rssi: RSSI of the received frame.
113  * @ofdm: Was frame send with an OFDM rate.
114  * @size: Data size of the received frame.
115  * @flags: MAC80211 receive flags (See &enum mac80211_rx_flags).
116  * @my_bss: Does this frame originate from device's BSS.
117  */
118 struct rxdone_entry_desc {
119         int signal;
120         int rssi;
121         int ofdm;
122         int size;
123         int flags;
124         int my_bss;
125 };
126
127 /**
128  * struct txdone_entry_desc: TX done entry descriptor
129  *
130  * Summary of information that has been read from the TX frame descriptor
131  * after the device is done with transmission.
132  *
133  * @control: Control structure which was used to transmit the frame.
134  * @status: TX status (See &enum tx_status).
135  * @retry: Retry count.
136  */
137 struct txdone_entry_desc {
138         struct ieee80211_tx_control *control;
139         int status;
140         int retry;
141 };
142
143 /**
144  * enum txentry_desc_flags: Status flags for TX entry descriptor
145  *
146  * @ENTRY_TXD_RTS_FRAME: This frame is a RTS frame.
147  * @ENTRY_TXD_OFDM_RATE: This frame is send out with an OFDM rate.
148  * @ENTRY_TXD_MORE_FRAG: This frame is followed by another fragment.
149  * @ENTRY_TXD_REQ_TIMESTAMP: Require timestamp to be inserted.
150  * @ENTRY_TXD_BURST: This frame belongs to the same burst event.
151  * @ENTRY_TXD_ACK: An ACK is required for this frame.
152  */
153 enum txentry_desc_flags {
154         ENTRY_TXD_RTS_FRAME,
155         ENTRY_TXD_OFDM_RATE,
156         ENTRY_TXD_MORE_FRAG,
157         ENTRY_TXD_REQ_TIMESTAMP,
158         ENTRY_TXD_BURST,
159         ENTRY_TXD_ACK,
160 };
161
162 /**
163  * struct txentry_desc: TX Entry descriptor
164  *
165  * Summary of information for the frame descriptor before sending a TX frame.
166  *
167  * @flags: Descriptor flags (See &enum queue_entry_flags).
168  * @queue: Queue identification (See &enum data_queue_qid).
169  * @length_high: PLCP length high word.
170  * @length_low: PLCP length low word.
171  * @signal: PLCP signal.
172  * @service: PLCP service.
173  * @aifs: AIFS value.
174  * @ifs: IFS value.
175  * @cw_min: cwmin value.
176  * @cw_max: cwmax value.
177  */
178 struct txentry_desc {
179         unsigned long flags;
180
181         enum data_queue_qid queue;
182
183         u16 length_high;
184         u16 length_low;
185         u16 signal;
186         u16 service;
187
188         int aifs;
189         int ifs;
190         int cw_min;
191         int cw_max;
192 };
193
194 /**
195  * enum queue_entry_flags: Status flags for queue entry
196  *
197  * @ENTRY_BCN_ASSIGNED: This entry has been assigned to an interface.
198  *      As long as this bit is set, this entry may only be touched
199  *      through the interface structure.
200  * @ENTRY_OWNER_DEVICE_DATA: This entry is owned by the device for data
201  *      transfer (either TX or RX depending on the queue). The entry should
202  *      only be touched after the device has signaled it is done with it.
203  * @ENTRY_OWNER_DEVICE_CRYPTO: This entry is owned by the device for data
204  *      encryption or decryption. The entry should only be touched after
205  *      the device has signaled it is done with it.
206  */
207
208 enum queue_entry_flags {
209         ENTRY_BCN_ASSIGNED,
210         ENTRY_OWNER_DEVICE_DATA,
211         ENTRY_OWNER_DEVICE_CRYPTO,
212 };
213
214 /**
215  * struct queue_entry: Entry inside the &struct data_queue
216  *
217  * @flags: Entry flags, see &enum queue_entry_flags.
218  * @queue: The data queue (&struct data_queue) to which this entry belongs.
219  * @skb: The buffer which is currently being transmitted (for TX queue),
220  *      or used to directly recieve data in (for RX queue).
221  * @entry_idx: The entry index number.
222  * @priv_data: Private data belonging to this queue entry. The pointer
223  *      points to data specific to a particular driver and queue type.
224  */
225 struct queue_entry {
226         unsigned long flags;
227
228         struct data_queue *queue;
229
230         struct sk_buff *skb;
231
232         unsigned int entry_idx;
233
234         void *priv_data;
235 };
236
237 /**
238  * enum queue_index: Queue index type
239  *
240  * @Q_INDEX: Index pointer to the current entry in the queue, if this entry is
241  *      owned by the hardware then the queue is considered to be full.
242  * @Q_INDEX_DONE: Index pointer to the next entry which will be completed by
243  *      the hardware and for which we need to run the txdone handler. If this
244  *      entry is not owned by the hardware the queue is considered to be empty.
245  * @Q_INDEX_CRYPTO: Index pointer to the next entry which encryption/decription
246  *      will be completed by the hardware next.
247  * @Q_INDEX_MAX: Keep last, used in &struct data_queue to determine the size
248  *      of the index array.
249  */
250 enum queue_index {
251         Q_INDEX,
252         Q_INDEX_DONE,
253         Q_INDEX_CRYPTO,
254         Q_INDEX_MAX,
255 };
256
257 /**
258  * struct data_queue: Data queue
259  *
260  * @rt2x00dev: Pointer to main &struct rt2x00dev where this queue belongs to.
261  * @entries: Base address of the &struct queue_entry which are
262  *      part of this queue.
263  * @qid: The queue identification, see &enum data_queue_qid.
264  * @lock: Spinlock to protect index handling. Whenever @index, @index_done or
265  *      @index_crypt needs to be changed this lock should be grabbed to prevent
266  *      index corruption due to concurrency.
267  * @count: Number of frames handled in the queue.
268  * @limit: Maximum number of entries in the queue.
269  * @length: Number of frames in queue.
270  * @index: Index pointers to entry positions in the queue,
271  *      use &enum queue_index to get a specific index field.
272  * @aifs: The aifs value for outgoing frames (field ignored in RX queue).
273  * @cw_min: The cw min value for outgoing frames (field ignored in RX queue).
274  * @cw_max: The cw max value for outgoing frames (field ignored in RX queue).
275  * @data_size: Maximum data size for the frames in this queue.
276  * @desc_size: Hardware descriptor size for the data in this queue.
277  */
278 struct data_queue {
279         struct rt2x00_dev *rt2x00dev;
280         struct queue_entry *entries;
281
282         enum data_queue_qid qid;
283
284         spinlock_t lock;
285         unsigned int count;
286         unsigned short limit;
287         unsigned short length;
288         unsigned short index[Q_INDEX_MAX];
289
290         unsigned short aifs;
291         unsigned short cw_min;
292         unsigned short cw_max;
293
294         unsigned short data_size;
295         unsigned short desc_size;
296 };
297
298 /**
299  * struct data_queue_desc: Data queue description
300  *
301  * The information in this structure is used by drivers
302  * to inform rt2x00lib about the creation of the data queue.
303  *
304  * @entry_num: Maximum number of entries for a queue.
305  * @data_size: Maximum data size for the frames in this queue.
306  * @desc_size: Hardware descriptor size for the data in this queue.
307  * @priv_size: Size of per-queue_entry private data.
308  */
309 struct data_queue_desc {
310         unsigned short entry_num;
311         unsigned short data_size;
312         unsigned short desc_size;
313         unsigned short priv_size;
314 };
315
316 /**
317  * queue_end - Return pointer to the last queue (HELPER MACRO).
318  * @__dev: Pointer to &struct rt2x00_dev
319  *
320  * Using the base rx pointer and the maximum number of available queues,
321  * this macro will return the address of 1 position beyond  the end of the
322  * queues array.
323  */
324 #define queue_end(__dev) \
325         &(__dev)->rx[(__dev)->data_queues]
326
327 /**
328  * tx_queue_end - Return pointer to the last TX queue (HELPER MACRO).
329  * @__dev: Pointer to &struct rt2x00_dev
330  *
331  * Using the base tx pointer and the maximum number of available TX
332  * queues, this macro will return the address of 1 position beyond
333  * the end of the TX queue array.
334  */
335 #define tx_queue_end(__dev) \
336         &(__dev)->tx[(__dev)->hw->queues]
337
338 /**
339  * queue_loop - Loop through the queues within a specific range (HELPER MACRO).
340  * @__entry: Pointer where the current queue entry will be stored in.
341  * @__start: Start queue pointer.
342  * @__end: End queue pointer.
343  *
344  * This macro will loop through all queues between &__start and &__end.
345  */
346 #define queue_loop(__entry, __start, __end)                     \
347         for ((__entry) = (__start);                             \
348              prefetch(&(__entry)[1]), (__entry) != (__end);     \
349              (__entry) = &(__entry)[1])
350
351 /**
352  * queue_for_each - Loop through all queues
353  * @__dev: Pointer to &struct rt2x00_dev
354  * @__entry: Pointer where the current queue entry will be stored in.
355  *
356  * This macro will loop through all available queues.
357  */
358 #define queue_for_each(__dev, __entry) \
359         queue_loop(__entry, (__dev)->rx, queue_end(__dev))
360
361 /**
362  * tx_queue_for_each - Loop through the TX queues
363  * @__dev: Pointer to &struct rt2x00_dev
364  * @__entry: Pointer where the current queue entry will be stored in.
365  *
366  * This macro will loop through all TX related queues excluding
367  * the Beacon and Atim queues.
368  */
369 #define tx_queue_for_each(__dev, __entry) \
370         queue_loop(__entry, (__dev)->tx, tx_queue_end(__dev))
371
372 /**
373  * txall_queue_for_each - Loop through all TX related queues
374  * @__dev: Pointer to &struct rt2x00_dev
375  * @__entry: Pointer where the current queue entry will be stored in.
376  *
377  * This macro will loop through all TX related queues including
378  * the Beacon and Atim queues.
379  */
380 #define txall_queue_for_each(__dev, __entry) \
381         queue_loop(__entry, (__dev)->tx, queue_end(__dev))
382
383 /**
384  * rt2x00queue_empty - Check if the queue is empty.
385  * @queue: Queue to check if empty.
386  */
387 static inline int rt2x00queue_empty(struct data_queue *queue)
388 {
389         return queue->length == 0;
390 }
391
392 /**
393  * rt2x00queue_full - Check if the queue is full.
394  * @queue: Queue to check if full.
395  */
396 static inline int rt2x00queue_full(struct data_queue *queue)
397 {
398         return queue->length == queue->limit;
399 }
400
401 /**
402  * rt2x00queue_free - Check the number of available entries in queue.
403  * @queue: Queue to check.
404  */
405 static inline int rt2x00queue_available(struct data_queue *queue)
406 {
407         return queue->limit - queue->length;
408 }
409
410 /**
411  * rt2x00_desc_read - Read a word from the hardware descriptor.
412  * @desc: Base descriptor address
413  * @word: Word index from where the descriptor should be read.
414  * @value: Address where the descriptor value should be written into.
415  */
416 static inline void rt2x00_desc_read(__le32 *desc, const u8 word, u32 *value)
417 {
418         *value = le32_to_cpu(desc[word]);
419 }
420
421 /**
422  * rt2x00_desc_write - wrote a word to the hardware descriptor.
423  * @desc: Base descriptor address
424  * @word: Word index from where the descriptor should be written.
425  * @value: Value that should be written into the descriptor.
426  */
427 static inline void rt2x00_desc_write(__le32 *desc, const u8 word, u32 value)
428 {
429         desc[word] = cpu_to_le32(value);
430 }
431
432 #endif /* RT2X00QUEUE_H */