]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/rt2x00/rt2x00queue.c
rt2x00: Fix TX failure path
[linux-2.6-omap-h63xx.git] / 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 #include <linux/dma-mapping.h>
29
30 #include "rt2x00.h"
31 #include "rt2x00lib.h"
32
33 struct sk_buff *rt2x00queue_alloc_rxskb(struct rt2x00_dev *rt2x00dev,
34                                         struct queue_entry *entry)
35 {
36         struct sk_buff *skb;
37         struct skb_frame_desc *skbdesc;
38         unsigned int frame_size;
39         unsigned int head_size = 0;
40         unsigned int tail_size = 0;
41
42         /*
43          * The frame size includes descriptor size, because the
44          * hardware directly receive the frame into the skbuffer.
45          */
46         frame_size = entry->queue->data_size + entry->queue->desc_size;
47
48         /*
49          * The payload should be aligned to a 4-byte boundary,
50          * this means we need at least 3 bytes for moving the frame
51          * into the correct offset.
52          */
53         head_size = 4;
54
55         /*
56          * For IV/EIV/ICV assembly we must make sure there is
57          * at least 8 bytes bytes available in headroom for IV/EIV
58          * and 4 bytes for ICV data as tailroon.
59          */
60 #ifdef CONFIG_RT2X00_LIB_CRYPTO
61         if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
62                 head_size += 8;
63                 tail_size += 4;
64         }
65 #endif /* CONFIG_RT2X00_LIB_CRYPTO */
66
67         /*
68          * Allocate skbuffer.
69          */
70         skb = dev_alloc_skb(frame_size + head_size + tail_size);
71         if (!skb)
72                 return NULL;
73
74         /*
75          * Make sure we not have a frame with the requested bytes
76          * available in the head and tail.
77          */
78         skb_reserve(skb, head_size);
79         skb_put(skb, frame_size);
80
81         /*
82          * Populate skbdesc.
83          */
84         skbdesc = get_skb_frame_desc(skb);
85         memset(skbdesc, 0, sizeof(*skbdesc));
86         skbdesc->entry = entry;
87
88         if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
89                 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
90                                                   skb->data,
91                                                   skb->len,
92                                                   DMA_FROM_DEVICE);
93                 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
94         }
95
96         return skb;
97 }
98
99 void rt2x00queue_map_txskb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
100 {
101         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
102
103         /*
104          * If device has requested headroom, we should make sure that
105          * is also mapped to the DMA so it can be used for transfering
106          * additional descriptor information to the hardware.
107          */
108         skb_push(skb, rt2x00dev->hw->extra_tx_headroom);
109
110         skbdesc->skb_dma =
111             dma_map_single(rt2x00dev->dev, skb->data, skb->len, DMA_TO_DEVICE);
112
113         /*
114          * Restore data pointer to original location again.
115          */
116         skb_pull(skb, rt2x00dev->hw->extra_tx_headroom);
117
118         skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
119 }
120 EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
121
122 void rt2x00queue_unmap_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
123 {
124         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
125
126         if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
127                 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len,
128                                  DMA_FROM_DEVICE);
129                 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
130         }
131
132         if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
133                 /*
134                  * Add headroom to the skb length, it has been removed
135                  * by the driver, but it was actually mapped to DMA.
136                  */
137                 dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma,
138                                  skb->len + rt2x00dev->hw->extra_tx_headroom,
139                                  DMA_TO_DEVICE);
140                 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
141         }
142 }
143
144 void rt2x00queue_free_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb)
145 {
146         if (!skb)
147                 return;
148
149         rt2x00queue_unmap_skb(rt2x00dev, skb);
150         dev_kfree_skb_any(skb);
151 }
152
153 static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
154                                              struct txentry_desc *txdesc)
155 {
156         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
157         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
158         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
159         struct ieee80211_rate *rate =
160             ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
161         const struct rt2x00_rate *hwrate;
162         unsigned int data_length;
163         unsigned int duration;
164         unsigned int residual;
165         unsigned long irqflags;
166
167         memset(txdesc, 0, sizeof(*txdesc));
168
169         /*
170          * Initialize information from queue
171          */
172         txdesc->queue = entry->queue->qid;
173         txdesc->cw_min = entry->queue->cw_min;
174         txdesc->cw_max = entry->queue->cw_max;
175         txdesc->aifs = entry->queue->aifs;
176
177         /* Data length + CRC + IV/EIV/ICV/MMIC (when using encryption) */
178         data_length = entry->skb->len + 4;
179
180         /*
181          * Check whether this frame is to be acked.
182          */
183         if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
184                 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
185
186 #ifdef CONFIG_RT2X00_LIB_CRYPTO
187         if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) &&
188             !entry->skb->do_not_encrypt) {
189                 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
190
191                 __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
192
193                 txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
194
195                 if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
196                         __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
197
198                 txdesc->key_idx = hw_key->hw_key_idx;
199                 txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb);
200
201                 /*
202                  * Extend frame length to include all encryption overhead
203                  * that will be added by the hardware.
204                  */
205                 data_length += rt2x00crypto_tx_overhead(tx_info);
206
207                 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
208                         __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
209
210                 if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
211                         __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
212         }
213 #endif /* CONFIG_RT2X00_LIB_CRYPTO */
214
215         /*
216          * Check if this is a RTS/CTS frame
217          */
218         if (ieee80211_is_rts(hdr->frame_control) ||
219             ieee80211_is_cts(hdr->frame_control)) {
220                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
221                 if (ieee80211_is_rts(hdr->frame_control))
222                         __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
223                 else
224                         __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
225                 if (tx_info->control.rts_cts_rate_idx >= 0)
226                         rate =
227                             ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
228         }
229
230         /*
231          * Determine retry information.
232          */
233         txdesc->retry_limit = tx_info->control.rates[0].count - 1;
234         /*
235          * XXX: If at this point we knew whether the HW is going to use
236          *      the RETRY_MODE bit or the retry_limit (currently all
237          *      use the RETRY_MODE bit) we could do something like b43
238          *      does, set the RETRY_MODE bit when the RC algorithm is
239          *      requesting more than the long retry limit.
240          */
241         if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
242                 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
243
244         /*
245          * Check if more fragments are pending
246          */
247         if (ieee80211_has_morefrags(hdr->frame_control)) {
248                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
249                 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
250         }
251
252         /*
253          * Beacons and probe responses require the tsf timestamp
254          * to be inserted into the frame.
255          */
256         if (ieee80211_is_beacon(hdr->frame_control) ||
257             ieee80211_is_probe_resp(hdr->frame_control))
258                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
259
260         /*
261          * Determine with what IFS priority this frame should be send.
262          * Set ifs to IFS_SIFS when the this is not the first fragment,
263          * or this fragment came after RTS/CTS.
264          */
265         if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) {
266                 txdesc->ifs = IFS_SIFS;
267         } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) {
268                 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
269                 txdesc->ifs = IFS_BACKOFF;
270         } else {
271                 txdesc->ifs = IFS_SIFS;
272         }
273
274         /*
275          * Hardware should insert sequence counter.
276          * FIXME: We insert a software sequence counter first for
277          * hardware that doesn't support hardware sequence counting.
278          *
279          * This is wrong because beacons are not getting sequence
280          * numbers assigned properly.
281          *
282          * A secondary problem exists for drivers that cannot toggle
283          * sequence counting per-frame, since those will override the
284          * sequence counter given by mac80211.
285          */
286         if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
287                 if (likely(tx_info->control.vif)) {
288                         struct rt2x00_intf *intf;
289
290                         intf = vif_to_intf(tx_info->control.vif);
291
292                         spin_lock_irqsave(&intf->seqlock, irqflags);
293
294                         if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
295                                 intf->seqno += 0x10;
296                         hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
297                         hdr->seq_ctrl |= cpu_to_le16(intf->seqno);
298
299                         spin_unlock_irqrestore(&intf->seqlock, irqflags);
300
301                         __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
302                 }
303         }
304
305         /*
306          * PLCP setup
307          * Length calculation depends on OFDM/CCK rate.
308          */
309         hwrate = rt2x00_get_rate(rate->hw_value);
310         txdesc->signal = hwrate->plcp;
311         txdesc->service = 0x04;
312
313         if (hwrate->flags & DEV_RATE_OFDM) {
314                 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags);
315
316                 txdesc->length_high = (data_length >> 6) & 0x3f;
317                 txdesc->length_low = data_length & 0x3f;
318         } else {
319                 /*
320                  * Convert length to microseconds.
321                  */
322                 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
323                 duration = GET_DURATION(data_length, hwrate->bitrate);
324
325                 if (residual != 0) {
326                         duration++;
327
328                         /*
329                          * Check if we need to set the Length Extension
330                          */
331                         if (hwrate->bitrate == 110 && residual <= 30)
332                                 txdesc->service |= 0x80;
333                 }
334
335                 txdesc->length_high = (duration >> 8) & 0xff;
336                 txdesc->length_low = duration & 0xff;
337
338                 /*
339                  * When preamble is enabled we should set the
340                  * preamble bit for the signal.
341                  */
342                 if (rt2x00_get_rate_preamble(rate->hw_value))
343                         txdesc->signal |= 0x08;
344         }
345 }
346
347 static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
348                                             struct txentry_desc *txdesc)
349 {
350         struct data_queue *queue = entry->queue;
351         struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
352
353         rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc);
354
355         /*
356          * All processing on the frame has been completed, this means
357          * it is now ready to be dumped to userspace through debugfs.
358          */
359         rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb);
360
361         /*
362          * Check if we need to kick the queue, there are however a few rules
363          *      1) Don't kick beacon queue
364          *      2) Don't kick unless this is the last in frame in a burst.
365          *         When the burst flag is set, this frame is always followed
366          *         by another frame which in some way are related to eachother.
367          *         This is true for fragments, RTS or CTS-to-self frames.
368          *      3) Rule 2 can be broken when the available entries
369          *         in the queue are less then a certain threshold.
370          */
371         if (entry->queue->qid == QID_BEACON)
372                 return;
373
374         if (rt2x00queue_threshold(queue) ||
375             !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
376                 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid);
377 }
378
379 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb)
380 {
381         struct ieee80211_tx_info *tx_info;
382         struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
383         struct txentry_desc txdesc;
384         struct skb_frame_desc *skbdesc;
385         unsigned int iv_len = 0;
386         u8 rate_idx, rate_flags;
387
388         if (unlikely(rt2x00queue_full(queue)))
389                 return -ENOBUFS;
390
391         if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
392                 ERROR(queue->rt2x00dev,
393                       "Arrived at non-free entry in the non-full queue %d.\n"
394                       "Please file bug report to %s.\n",
395                       queue->qid, DRV_PROJECT);
396                 return -EINVAL;
397         }
398
399         /*
400          * Copy all TX descriptor information into txdesc,
401          * after that we are free to use the skb->cb array
402          * for our information.
403          */
404         entry->skb = skb;
405         rt2x00queue_create_tx_descriptor(entry, &txdesc);
406
407         if (IEEE80211_SKB_CB(skb)->control.hw_key != NULL)
408                 iv_len = IEEE80211_SKB_CB(skb)->control.hw_key->iv_len;
409
410         /*
411          * All information is retrieved from the skb->cb array,
412          * now we should claim ownership of the driver part of that
413          * array, preserving the bitrate index and flags.
414          */
415         tx_info = IEEE80211_SKB_CB(skb);
416         rate_idx = tx_info->control.rates[0].idx;
417         rate_flags = tx_info->control.rates[0].flags;
418         skbdesc = get_skb_frame_desc(skb);
419         memset(skbdesc, 0, sizeof(*skbdesc));
420         skbdesc->entry = entry;
421         skbdesc->tx_rate_idx = rate_idx;
422         skbdesc->tx_rate_flags = rate_flags;
423
424         /*
425          * When hardware encryption is supported, and this frame
426          * is to be encrypted, we should strip the IV/EIV data from
427          * the frame so we can provide it to the driver seperately.
428          */
429         if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
430             !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags))
431                 rt2x00crypto_tx_remove_iv(skb, iv_len);
432
433         /*
434          * It could be possible that the queue was corrupted and this
435          * call failed. Since we always return NETDEV_TX_OK to mac80211,
436          * this frame will simply be dropped.
437          */
438         if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) {
439                 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
440                 entry->skb = NULL;
441                 return -EIO;
442         }
443
444         if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
445                 rt2x00queue_map_txskb(queue->rt2x00dev, skb);
446
447         set_bit(ENTRY_DATA_PENDING, &entry->flags);
448
449         rt2x00queue_index_inc(queue, Q_INDEX);
450         rt2x00queue_write_tx_descriptor(entry, &txdesc);
451
452         return 0;
453 }
454
455 int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
456                               struct ieee80211_vif *vif)
457 {
458         struct rt2x00_intf *intf = vif_to_intf(vif);
459         struct skb_frame_desc *skbdesc;
460         struct txentry_desc txdesc;
461         __le32 desc[16];
462
463         if (unlikely(!intf->beacon))
464                 return -ENOBUFS;
465
466         intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
467         if (!intf->beacon->skb)
468                 return -ENOMEM;
469
470         /*
471          * Copy all TX descriptor information into txdesc,
472          * after that we are free to use the skb->cb array
473          * for our information.
474          */
475         rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
476
477         /*
478          * For the descriptor we use a local array from where the
479          * driver can move it to the correct location required for
480          * the hardware.
481          */
482         memset(desc, 0, sizeof(desc));
483
484         /*
485          * Fill in skb descriptor
486          */
487         skbdesc = get_skb_frame_desc(intf->beacon->skb);
488         memset(skbdesc, 0, sizeof(*skbdesc));
489         skbdesc->desc = desc;
490         skbdesc->desc_len = intf->beacon->queue->desc_size;
491         skbdesc->entry = intf->beacon;
492
493         /*
494          * Write TX descriptor into reserved room in front of the beacon.
495          */
496         rt2x00queue_write_tx_descriptor(intf->beacon, &txdesc);
497
498         /*
499          * Send beacon to hardware.
500          * Also enable beacon generation, which might have been disabled
501          * by the driver during the config_beacon() callback function.
502          */
503         rt2x00dev->ops->lib->write_beacon(intf->beacon);
504         rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, QID_BEACON);
505
506         return 0;
507 }
508
509 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
510                                          const enum data_queue_qid queue)
511 {
512         int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
513
514         if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
515                 return &rt2x00dev->tx[queue];
516
517         if (!rt2x00dev->bcn)
518                 return NULL;
519
520         if (queue == QID_BEACON)
521                 return &rt2x00dev->bcn[0];
522         else if (queue == QID_ATIM && atim)
523                 return &rt2x00dev->bcn[1];
524
525         return NULL;
526 }
527 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
528
529 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
530                                           enum queue_index index)
531 {
532         struct queue_entry *entry;
533         unsigned long irqflags;
534
535         if (unlikely(index >= Q_INDEX_MAX)) {
536                 ERROR(queue->rt2x00dev,
537                       "Entry requested from invalid index type (%d)\n", index);
538                 return NULL;
539         }
540
541         spin_lock_irqsave(&queue->lock, irqflags);
542
543         entry = &queue->entries[queue->index[index]];
544
545         spin_unlock_irqrestore(&queue->lock, irqflags);
546
547         return entry;
548 }
549 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
550
551 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
552 {
553         unsigned long irqflags;
554
555         if (unlikely(index >= Q_INDEX_MAX)) {
556                 ERROR(queue->rt2x00dev,
557                       "Index change on invalid index type (%d)\n", index);
558                 return;
559         }
560
561         spin_lock_irqsave(&queue->lock, irqflags);
562
563         queue->index[index]++;
564         if (queue->index[index] >= queue->limit)
565                 queue->index[index] = 0;
566
567         if (index == Q_INDEX) {
568                 queue->length++;
569         } else if (index == Q_INDEX_DONE) {
570                 queue->length--;
571                 queue->count++;
572         }
573
574         spin_unlock_irqrestore(&queue->lock, irqflags);
575 }
576
577 static void rt2x00queue_reset(struct data_queue *queue)
578 {
579         unsigned long irqflags;
580
581         spin_lock_irqsave(&queue->lock, irqflags);
582
583         queue->count = 0;
584         queue->length = 0;
585         memset(queue->index, 0, sizeof(queue->index));
586
587         spin_unlock_irqrestore(&queue->lock, irqflags);
588 }
589
590 void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
591 {
592         struct data_queue *queue;
593         unsigned int i;
594
595         queue_for_each(rt2x00dev, queue) {
596                 rt2x00queue_reset(queue);
597
598                 for (i = 0; i < queue->limit; i++) {
599                         queue->entries[i].flags = 0;
600
601                         rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
602                 }
603         }
604 }
605
606 static int rt2x00queue_alloc_entries(struct data_queue *queue,
607                                      const struct data_queue_desc *qdesc)
608 {
609         struct queue_entry *entries;
610         unsigned int entry_size;
611         unsigned int i;
612
613         rt2x00queue_reset(queue);
614
615         queue->limit = qdesc->entry_num;
616         queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
617         queue->data_size = qdesc->data_size;
618         queue->desc_size = qdesc->desc_size;
619
620         /*
621          * Allocate all queue entries.
622          */
623         entry_size = sizeof(*entries) + qdesc->priv_size;
624         entries = kzalloc(queue->limit * entry_size, GFP_KERNEL);
625         if (!entries)
626                 return -ENOMEM;
627
628 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
629         ( ((char *)(__base)) + ((__limit) * (__esize)) + \
630             ((__index) * (__psize)) )
631
632         for (i = 0; i < queue->limit; i++) {
633                 entries[i].flags = 0;
634                 entries[i].queue = queue;
635                 entries[i].skb = NULL;
636                 entries[i].entry_idx = i;
637                 entries[i].priv_data =
638                     QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
639                                             sizeof(*entries), qdesc->priv_size);
640         }
641
642 #undef QUEUE_ENTRY_PRIV_OFFSET
643
644         queue->entries = entries;
645
646         return 0;
647 }
648
649 static void rt2x00queue_free_skbs(struct rt2x00_dev *rt2x00dev,
650                                   struct data_queue *queue)
651 {
652         unsigned int i;
653
654         if (!queue->entries)
655                 return;
656
657         for (i = 0; i < queue->limit; i++) {
658                 if (queue->entries[i].skb)
659                         rt2x00queue_free_skb(rt2x00dev, queue->entries[i].skb);
660         }
661 }
662
663 static int rt2x00queue_alloc_rxskbs(struct rt2x00_dev *rt2x00dev,
664                                     struct data_queue *queue)
665 {
666         unsigned int i;
667         struct sk_buff *skb;
668
669         for (i = 0; i < queue->limit; i++) {
670                 skb = rt2x00queue_alloc_rxskb(rt2x00dev, &queue->entries[i]);
671                 if (!skb)
672                         return -ENOMEM;
673                 queue->entries[i].skb = skb;
674         }
675
676         return 0;
677 }
678
679 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
680 {
681         struct data_queue *queue;
682         int status;
683
684         status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
685         if (status)
686                 goto exit;
687
688         tx_queue_for_each(rt2x00dev, queue) {
689                 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
690                 if (status)
691                         goto exit;
692         }
693
694         status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
695         if (status)
696                 goto exit;
697
698         if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
699                 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
700                                                    rt2x00dev->ops->atim);
701                 if (status)
702                         goto exit;
703         }
704
705         status = rt2x00queue_alloc_rxskbs(rt2x00dev, rt2x00dev->rx);
706         if (status)
707                 goto exit;
708
709         return 0;
710
711 exit:
712         ERROR(rt2x00dev, "Queue entries allocation failed.\n");
713
714         rt2x00queue_uninitialize(rt2x00dev);
715
716         return status;
717 }
718
719 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
720 {
721         struct data_queue *queue;
722
723         rt2x00queue_free_skbs(rt2x00dev, rt2x00dev->rx);
724
725         queue_for_each(rt2x00dev, queue) {
726                 kfree(queue->entries);
727                 queue->entries = NULL;
728         }
729 }
730
731 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
732                              struct data_queue *queue, enum data_queue_qid qid)
733 {
734         spin_lock_init(&queue->lock);
735
736         queue->rt2x00dev = rt2x00dev;
737         queue->qid = qid;
738         queue->txop = 0;
739         queue->aifs = 2;
740         queue->cw_min = 5;
741         queue->cw_max = 10;
742 }
743
744 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
745 {
746         struct data_queue *queue;
747         enum data_queue_qid qid;
748         unsigned int req_atim =
749             !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
750
751         /*
752          * We need the following queues:
753          * RX: 1
754          * TX: ops->tx_queues
755          * Beacon: 1
756          * Atim: 1 (if required)
757          */
758         rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
759
760         queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL);
761         if (!queue) {
762                 ERROR(rt2x00dev, "Queue allocation failed.\n");
763                 return -ENOMEM;
764         }
765
766         /*
767          * Initialize pointers
768          */
769         rt2x00dev->rx = queue;
770         rt2x00dev->tx = &queue[1];
771         rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
772
773         /*
774          * Initialize queue parameters.
775          * RX: qid = QID_RX
776          * TX: qid = QID_AC_BE + index
777          * TX: cw_min: 2^5 = 32.
778          * TX: cw_max: 2^10 = 1024.
779          * BCN: qid = QID_BEACON
780          * ATIM: qid = QID_ATIM
781          */
782         rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
783
784         qid = QID_AC_BE;
785         tx_queue_for_each(rt2x00dev, queue)
786                 rt2x00queue_init(rt2x00dev, queue, qid++);
787
788         rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
789         if (req_atim)
790                 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
791
792         return 0;
793 }
794
795 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
796 {
797         kfree(rt2x00dev->rx);
798         rt2x00dev->rx = NULL;
799         rt2x00dev->tx = NULL;
800         rt2x00dev->bcn = NULL;
801 }