rt2x00: Optimize IV/EIV handling
[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  * Under normal load without fragmentation 12 entries are sufficient
46  * without the queue being filled up to the maximum. When using fragmentation
47  * and the queue threshold code we need to add some additional margins to
48  * make sure the queue will never (or only under extreme load) fill up
49  * completely.
50  * Since we don't use preallocated DMA having a large number of queue entries
51  * will have only minimal impact on the memory requirements for the queue.
52  */
53 #define RX_ENTRIES      24
54 #define TX_ENTRIES      24
55 #define BEACON_ENTRIES  1
56 #define ATIM_ENTRIES    8
57
58 /**
59  * enum data_queue_qid: Queue identification
60  *
61  * @QID_AC_BE: AC BE queue
62  * @QID_AC_BK: AC BK queue
63  * @QID_AC_VI: AC VI queue
64  * @QID_AC_VO: AC VO queue
65  * @QID_HCCA: HCCA queue
66  * @QID_MGMT: MGMT queue (prio queue)
67  * @QID_RX: RX queue
68  * @QID_OTHER: None of the above (don't use, only present for completeness)
69  * @QID_BEACON: Beacon queue (value unspecified, don't send it to device)
70  * @QID_ATIM: Atim queue (value unspeficied, don't send it to device)
71  */
72 enum data_queue_qid {
73         QID_AC_BE = 0,
74         QID_AC_BK = 1,
75         QID_AC_VI = 2,
76         QID_AC_VO = 3,
77         QID_HCCA = 4,
78         QID_MGMT = 13,
79         QID_RX = 14,
80         QID_OTHER = 15,
81         QID_BEACON,
82         QID_ATIM,
83 };
84
85 /**
86  * enum skb_frame_desc_flags: Flags for &struct skb_frame_desc
87  *
88  * @SKBDESC_DMA_MAPPED_RX: &skb_dma field has been mapped for RX
89  * @SKBDESC_DMA_MAPPED_TX: &skb_dma field has been mapped for TX
90  * @FRAME_DESC_IV_STRIPPED: Frame contained a IV/EIV provided by
91  *      mac80211 but was stripped for processing by the driver.
92  */
93 enum skb_frame_desc_flags {
94         SKBDESC_DMA_MAPPED_RX = 1 << 0,
95         SKBDESC_DMA_MAPPED_TX = 1 << 1,
96         FRAME_DESC_IV_STRIPPED = 1 << 2,
97 };
98
99 /**
100  * struct skb_frame_desc: Descriptor information for the skb buffer
101  *
102  * This structure is placed over the driver_data array, this means that
103  * this structure should not exceed the size of that array (40 bytes).
104  *
105  * @flags: Frame flags, see &enum skb_frame_desc_flags.
106  * @desc_len: Length of the frame descriptor.
107  * @tx_rate_idx: the index of the TX rate, used for TX status reporting
108  * @tx_rate_flags: the TX rate flags, used for TX status reporting
109  * @desc: Pointer to descriptor part of the frame.
110  *      Note that this pointer could point to something outside
111  *      of the scope of the skb->data pointer.
112  * @iv: IV/EIV data used during encryption/decryption.
113  * @skb_dma: (PCI-only) the DMA address associated with the sk buffer.
114  * @entry: The entry to which this sk buffer belongs.
115  */
116 struct skb_frame_desc {
117         u8 flags;
118
119         u8 desc_len;
120         u8 tx_rate_idx;
121         u8 tx_rate_flags;
122
123         void *desc;
124
125         __le32 iv[2];
126
127         dma_addr_t skb_dma;
128
129         struct queue_entry *entry;
130 };
131
132 /**
133  * get_skb_frame_desc - Obtain the rt2x00 frame descriptor from a sk_buff.
134  * @skb: &struct sk_buff from where we obtain the &struct skb_frame_desc
135  */
136 static inline struct skb_frame_desc* get_skb_frame_desc(struct sk_buff *skb)
137 {
138         BUILD_BUG_ON(sizeof(struct skb_frame_desc) >
139                      IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
140         return (struct skb_frame_desc *)&IEEE80211_SKB_CB(skb)->driver_data;
141 }
142
143 /**
144  * enum rxdone_entry_desc_flags: Flags for &struct rxdone_entry_desc
145  *
146  * @RXDONE_SIGNAL_PLCP: Signal field contains the plcp value.
147  * @RXDONE_SIGNAL_BITRATE: Signal field contains the bitrate value.
148  * @RXDONE_MY_BSS: Does this frame originate from device's BSS.
149  */
150 enum rxdone_entry_desc_flags {
151         RXDONE_SIGNAL_PLCP = 1 << 0,
152         RXDONE_SIGNAL_BITRATE = 1 << 1,
153         RXDONE_MY_BSS = 1 << 2,
154 };
155
156 /**
157  * struct rxdone_entry_desc: RX Entry descriptor
158  *
159  * Summary of information that has been read from the RX frame descriptor.
160  *
161  * @timestamp: RX Timestamp
162  * @signal: Signal of the received frame.
163  * @rssi: RSSI of the received frame.
164  * @size: Data size of the received frame.
165  * @flags: MAC80211 receive flags (See &enum mac80211_rx_flags).
166  * @dev_flags: Ralink receive flags (See &enum rxdone_entry_desc_flags).
167  * @cipher: Cipher type used during decryption.
168  * @cipher_status: Decryption status.
169  * @iv: IV/EIV data used during decryption.
170  * @icv: ICV data used during decryption.
171  */
172 struct rxdone_entry_desc {
173         u64 timestamp;
174         int signal;
175         int rssi;
176         int size;
177         int flags;
178         int dev_flags;
179         u8 cipher;
180         u8 cipher_status;
181
182         __le32 iv[2];
183         __le32 icv;
184 };
185
186 /**
187  * enum txdone_entry_desc_flags: Flags for &struct txdone_entry_desc
188  *
189  * @TXDONE_UNKNOWN: Hardware could not determine success of transmission.
190  * @TXDONE_SUCCESS: Frame was successfully send
191  * @TXDONE_FAILURE: Frame was not successfully send
192  * @TXDONE_EXCESSIVE_RETRY: In addition to &TXDONE_FAILURE, the
193  *      frame transmission failed due to excessive retries.
194  */
195 enum txdone_entry_desc_flags {
196         TXDONE_UNKNOWN,
197         TXDONE_SUCCESS,
198         TXDONE_FAILURE,
199         TXDONE_EXCESSIVE_RETRY,
200 };
201
202 /**
203  * struct txdone_entry_desc: TX done entry descriptor
204  *
205  * Summary of information that has been read from the TX frame descriptor
206  * after the device is done with transmission.
207  *
208  * @flags: TX done flags (See &enum txdone_entry_desc_flags).
209  * @retry: Retry count.
210  */
211 struct txdone_entry_desc {
212         unsigned long flags;
213         int retry;
214 };
215
216 /**
217  * enum txentry_desc_flags: Status flags for TX entry descriptor
218  *
219  * @ENTRY_TXD_RTS_FRAME: This frame is a RTS frame.
220  * @ENTRY_TXD_CTS_FRAME: This frame is a CTS-to-self frame.
221  * @ENTRY_TXD_OFDM_RATE: This frame is send out with an OFDM rate.
222  * @ENTRY_TXD_GENERATE_SEQ: This frame requires sequence counter.
223  * @ENTRY_TXD_FIRST_FRAGMENT: This is the first frame.
224  * @ENTRY_TXD_MORE_FRAG: This frame is followed by another fragment.
225  * @ENTRY_TXD_REQ_TIMESTAMP: Require timestamp to be inserted.
226  * @ENTRY_TXD_BURST: This frame belongs to the same burst event.
227  * @ENTRY_TXD_ACK: An ACK is required for this frame.
228  * @ENTRY_TXD_RETRY_MODE: When set, the long retry count is used.
229  * @ENTRY_TXD_ENCRYPT: This frame should be encrypted.
230  * @ENTRY_TXD_ENCRYPT_PAIRWISE: Use pairwise key table (instead of shared).
231  * @ENTRY_TXD_ENCRYPT_IV: Generate IV/EIV in hardware.
232  * @ENTRY_TXD_ENCRYPT_MMIC: Generate MIC in hardware.
233  */
234 enum txentry_desc_flags {
235         ENTRY_TXD_RTS_FRAME,
236         ENTRY_TXD_CTS_FRAME,
237         ENTRY_TXD_OFDM_RATE,
238         ENTRY_TXD_GENERATE_SEQ,
239         ENTRY_TXD_FIRST_FRAGMENT,
240         ENTRY_TXD_MORE_FRAG,
241         ENTRY_TXD_REQ_TIMESTAMP,
242         ENTRY_TXD_BURST,
243         ENTRY_TXD_ACK,
244         ENTRY_TXD_RETRY_MODE,
245         ENTRY_TXD_ENCRYPT,
246         ENTRY_TXD_ENCRYPT_PAIRWISE,
247         ENTRY_TXD_ENCRYPT_IV,
248         ENTRY_TXD_ENCRYPT_MMIC,
249 };
250
251 /**
252  * struct txentry_desc: TX Entry descriptor
253  *
254  * Summary of information for the frame descriptor before sending a TX frame.
255  *
256  * @flags: Descriptor flags (See &enum queue_entry_flags).
257  * @queue: Queue identification (See &enum data_queue_qid).
258  * @length_high: PLCP length high word.
259  * @length_low: PLCP length low word.
260  * @signal: PLCP signal.
261  * @service: PLCP service.
262  * @retry_limit: Max number of retries.
263  * @aifs: AIFS value.
264  * @ifs: IFS value.
265  * @cw_min: cwmin value.
266  * @cw_max: cwmax value.
267  * @cipher: Cipher type used for encryption.
268  * @key_idx: Key index used for encryption.
269  * @iv_offset: Position where IV should be inserted by hardware.
270  */
271 struct txentry_desc {
272         unsigned long flags;
273
274         enum data_queue_qid queue;
275
276         u16 length_high;
277         u16 length_low;
278         u16 signal;
279         u16 service;
280
281         short retry_limit;
282         short aifs;
283         short ifs;
284         short cw_min;
285         short cw_max;
286
287         enum cipher cipher;
288         u16 key_idx;
289         u16 iv_offset;
290 };
291
292 /**
293  * enum queue_entry_flags: Status flags for queue entry
294  *
295  * @ENTRY_BCN_ASSIGNED: This entry has been assigned to an interface.
296  *      As long as this bit is set, this entry may only be touched
297  *      through the interface structure.
298  * @ENTRY_OWNER_DEVICE_DATA: This entry is owned by the device for data
299  *      transfer (either TX or RX depending on the queue). The entry should
300  *      only be touched after the device has signaled it is done with it.
301  * @ENTRY_OWNER_DEVICE_CRYPTO: This entry is owned by the device for data
302  *      encryption or decryption. The entry should only be touched after
303  *      the device has signaled it is done with it.
304  * @ENTRY_DATA_PENDING: This entry contains a valid frame and is waiting
305  *      for the signal to start sending.
306  */
307 enum queue_entry_flags {
308         ENTRY_BCN_ASSIGNED,
309         ENTRY_OWNER_DEVICE_DATA,
310         ENTRY_OWNER_DEVICE_CRYPTO,
311         ENTRY_DATA_PENDING,
312 };
313
314 /**
315  * struct queue_entry: Entry inside the &struct data_queue
316  *
317  * @flags: Entry flags, see &enum queue_entry_flags.
318  * @queue: The data queue (&struct data_queue) to which this entry belongs.
319  * @skb: The buffer which is currently being transmitted (for TX queue),
320  *      or used to directly recieve data in (for RX queue).
321  * @entry_idx: The entry index number.
322  * @priv_data: Private data belonging to this queue entry. The pointer
323  *      points to data specific to a particular driver and queue type.
324  */
325 struct queue_entry {
326         unsigned long flags;
327
328         struct data_queue *queue;
329
330         struct sk_buff *skb;
331
332         unsigned int entry_idx;
333
334         void *priv_data;
335 };
336
337 /**
338  * enum queue_index: Queue index type
339  *
340  * @Q_INDEX: Index pointer to the current entry in the queue, if this entry is
341  *      owned by the hardware then the queue is considered to be full.
342  * @Q_INDEX_DONE: Index pointer to the next entry which will be completed by
343  *      the hardware and for which we need to run the txdone handler. If this
344  *      entry is not owned by the hardware the queue is considered to be empty.
345  * @Q_INDEX_CRYPTO: Index pointer to the next entry which encryption/decription
346  *      will be completed by the hardware next.
347  * @Q_INDEX_MAX: Keep last, used in &struct data_queue to determine the size
348  *      of the index array.
349  */
350 enum queue_index {
351         Q_INDEX,
352         Q_INDEX_DONE,
353         Q_INDEX_CRYPTO,
354         Q_INDEX_MAX,
355 };
356
357 /**
358  * struct data_queue: Data queue
359  *
360  * @rt2x00dev: Pointer to main &struct rt2x00dev where this queue belongs to.
361  * @entries: Base address of the &struct queue_entry which are
362  *      part of this queue.
363  * @qid: The queue identification, see &enum data_queue_qid.
364  * @lock: Spinlock to protect index handling. Whenever @index, @index_done or
365  *      @index_crypt needs to be changed this lock should be grabbed to prevent
366  *      index corruption due to concurrency.
367  * @count: Number of frames handled in the queue.
368  * @limit: Maximum number of entries in the queue.
369  * @threshold: Minimum number of free entries before queue is kicked by force.
370  * @length: Number of frames in queue.
371  * @index: Index pointers to entry positions in the queue,
372  *      use &enum queue_index to get a specific index field.
373  * @txop: maximum burst time.
374  * @aifs: The aifs value for outgoing frames (field ignored in RX queue).
375  * @cw_min: The cw min value for outgoing frames (field ignored in RX queue).
376  * @cw_max: The cw max value for outgoing frames (field ignored in RX queue).
377  * @data_size: Maximum data size for the frames in this queue.
378  * @desc_size: Hardware descriptor size for the data in this queue.
379  * @usb_endpoint: Device endpoint used for communication (USB only)
380  * @usb_maxpacket: Max packet size for given endpoint (USB only)
381  */
382 struct data_queue {
383         struct rt2x00_dev *rt2x00dev;
384         struct queue_entry *entries;
385
386         enum data_queue_qid qid;
387
388         spinlock_t lock;
389         unsigned int count;
390         unsigned short limit;
391         unsigned short threshold;
392         unsigned short length;
393         unsigned short index[Q_INDEX_MAX];
394
395         unsigned short txop;
396         unsigned short aifs;
397         unsigned short cw_min;
398         unsigned short cw_max;
399
400         unsigned short data_size;
401         unsigned short desc_size;
402
403         unsigned short usb_endpoint;
404         unsigned short usb_maxpacket;
405 };
406
407 /**
408  * struct data_queue_desc: Data queue description
409  *
410  * The information in this structure is used by drivers
411  * to inform rt2x00lib about the creation of the data queue.
412  *
413  * @entry_num: Maximum number of entries for a queue.
414  * @data_size: Maximum data size for the frames in this queue.
415  * @desc_size: Hardware descriptor size for the data in this queue.
416  * @priv_size: Size of per-queue_entry private data.
417  */
418 struct data_queue_desc {
419         unsigned short entry_num;
420         unsigned short data_size;
421         unsigned short desc_size;
422         unsigned short priv_size;
423 };
424
425 /**
426  * queue_end - Return pointer to the last queue (HELPER MACRO).
427  * @__dev: Pointer to &struct rt2x00_dev
428  *
429  * Using the base rx pointer and the maximum number of available queues,
430  * this macro will return the address of 1 position beyond  the end of the
431  * queues array.
432  */
433 #define queue_end(__dev) \
434         &(__dev)->rx[(__dev)->data_queues]
435
436 /**
437  * tx_queue_end - Return pointer to the last TX queue (HELPER MACRO).
438  * @__dev: Pointer to &struct rt2x00_dev
439  *
440  * Using the base tx pointer and the maximum number of available TX
441  * queues, this macro will return the address of 1 position beyond
442  * the end of the TX queue array.
443  */
444 #define tx_queue_end(__dev) \
445         &(__dev)->tx[(__dev)->ops->tx_queues]
446
447 /**
448  * queue_next - Return pointer to next queue in list (HELPER MACRO).
449  * @__queue: Current queue for which we need the next queue
450  *
451  * Using the current queue address we take the address directly
452  * after the queue to take the next queue. Note that this macro
453  * should be used carefully since it does not protect against
454  * moving past the end of the list. (See macros &queue_end and
455  * &tx_queue_end for determining the end of the queue).
456  */
457 #define queue_next(__queue) \
458         &(__queue)[1]
459
460 /**
461  * queue_loop - Loop through the queues within a specific range (HELPER MACRO).
462  * @__entry: Pointer where the current queue entry will be stored in.
463  * @__start: Start queue pointer.
464  * @__end: End queue pointer.
465  *
466  * This macro will loop through all queues between &__start and &__end.
467  */
468 #define queue_loop(__entry, __start, __end)                     \
469         for ((__entry) = (__start);                             \
470              prefetch(queue_next(__entry)), (__entry) != (__end);\
471              (__entry) = queue_next(__entry))
472
473 /**
474  * queue_for_each - Loop through all queues
475  * @__dev: Pointer to &struct rt2x00_dev
476  * @__entry: Pointer where the current queue entry will be stored in.
477  *
478  * This macro will loop through all available queues.
479  */
480 #define queue_for_each(__dev, __entry) \
481         queue_loop(__entry, (__dev)->rx, queue_end(__dev))
482
483 /**
484  * tx_queue_for_each - Loop through the TX queues
485  * @__dev: Pointer to &struct rt2x00_dev
486  * @__entry: Pointer where the current queue entry will be stored in.
487  *
488  * This macro will loop through all TX related queues excluding
489  * the Beacon and Atim queues.
490  */
491 #define tx_queue_for_each(__dev, __entry) \
492         queue_loop(__entry, (__dev)->tx, tx_queue_end(__dev))
493
494 /**
495  * txall_queue_for_each - Loop through all TX related queues
496  * @__dev: Pointer to &struct rt2x00_dev
497  * @__entry: Pointer where the current queue entry will be stored in.
498  *
499  * This macro will loop through all TX related queues including
500  * the Beacon and Atim queues.
501  */
502 #define txall_queue_for_each(__dev, __entry) \
503         queue_loop(__entry, (__dev)->tx, queue_end(__dev))
504
505 /**
506  * rt2x00queue_empty - Check if the queue is empty.
507  * @queue: Queue to check if empty.
508  */
509 static inline int rt2x00queue_empty(struct data_queue *queue)
510 {
511         return queue->length == 0;
512 }
513
514 /**
515  * rt2x00queue_full - Check if the queue is full.
516  * @queue: Queue to check if full.
517  */
518 static inline int rt2x00queue_full(struct data_queue *queue)
519 {
520         return queue->length == queue->limit;
521 }
522
523 /**
524  * rt2x00queue_free - Check the number of available entries in queue.
525  * @queue: Queue to check.
526  */
527 static inline int rt2x00queue_available(struct data_queue *queue)
528 {
529         return queue->limit - queue->length;
530 }
531
532 /**
533  * rt2x00queue_threshold - Check if the queue is below threshold
534  * @queue: Queue to check.
535  */
536 static inline int rt2x00queue_threshold(struct data_queue *queue)
537 {
538         return rt2x00queue_available(queue) < queue->threshold;
539 }
540
541 /**
542  * _rt2x00_desc_read - Read a word from the hardware descriptor.
543  * @desc: Base descriptor address
544  * @word: Word index from where the descriptor should be read.
545  * @value: Address where the descriptor value should be written into.
546  */
547 static inline void _rt2x00_desc_read(__le32 *desc, const u8 word, __le32 *value)
548 {
549         *value = desc[word];
550 }
551
552 /**
553  * rt2x00_desc_read - Read a word from the hardware descriptor, this
554  * function will take care of the byte ordering.
555  * @desc: Base descriptor address
556  * @word: Word index from where the descriptor should be read.
557  * @value: Address where the descriptor value should be written into.
558  */
559 static inline void rt2x00_desc_read(__le32 *desc, const u8 word, u32 *value)
560 {
561         __le32 tmp;
562         _rt2x00_desc_read(desc, word, &tmp);
563         *value = le32_to_cpu(tmp);
564 }
565
566 /**
567  * rt2x00_desc_write - write a word to the hardware descriptor, this
568  * function will take care of the byte ordering.
569  * @desc: Base descriptor address
570  * @word: Word index from where the descriptor should be written.
571  * @value: Value that should be written into the descriptor.
572  */
573 static inline void _rt2x00_desc_write(__le32 *desc, const u8 word, __le32 value)
574 {
575         desc[word] = value;
576 }
577
578 /**
579  * rt2x00_desc_write - write a word to the hardware descriptor.
580  * @desc: Base descriptor address
581  * @word: Word index from where the descriptor should be written.
582  * @value: Value that should be written into the descriptor.
583  */
584 static inline void rt2x00_desc_write(__le32 *desc, const u8 word, u32 value)
585 {
586         _rt2x00_desc_write(desc, word, cpu_to_le32(value));
587 }
588
589 #endif /* RT2X00QUEUE_H */