Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rt2x00 / rt2x00queue.c
1 /*
2         Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3         Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
4         Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
5         <http://rt2x00.serialmonkey.com>
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the
19         Free Software Foundation, Inc.,
20         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 /*
24         Module: rt2x00lib
25         Abstract: rt2x00 queue specific routines.
26  */
27
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/dma-mapping.h>
32
33 #include "rt2x00.h"
34 #include "rt2x00lib.h"
35
36 struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry, gfp_t gfp)
37 {
38         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
39         struct sk_buff *skb;
40         struct skb_frame_desc *skbdesc;
41         unsigned int frame_size;
42         unsigned int head_size = 0;
43         unsigned int tail_size = 0;
44
45         /*
46          * The frame size includes descriptor size, because the
47          * hardware directly receive the frame into the skbuffer.
48          */
49         frame_size = entry->queue->data_size + entry->queue->desc_size;
50
51         /*
52          * The payload should be aligned to a 4-byte boundary,
53          * this means we need at least 3 bytes for moving the frame
54          * into the correct offset.
55          */
56         head_size = 4;
57
58         /*
59          * For IV/EIV/ICV assembly we must make sure there is
60          * at least 8 bytes bytes available in headroom for IV/EIV
61          * and 8 bytes for ICV data as tailroon.
62          */
63         if (test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags)) {
64                 head_size += 8;
65                 tail_size += 8;
66         }
67
68         /*
69          * Allocate skbuffer.
70          */
71         skb = __dev_alloc_skb(frame_size + head_size + tail_size, gfp);
72         if (!skb)
73                 return NULL;
74
75         /*
76          * Make sure we not have a frame with the requested bytes
77          * available in the head and tail.
78          */
79         skb_reserve(skb, head_size);
80         skb_put(skb, frame_size);
81
82         /*
83          * Populate skbdesc.
84          */
85         skbdesc = get_skb_frame_desc(skb);
86         memset(skbdesc, 0, sizeof(*skbdesc));
87         skbdesc->entry = entry;
88
89         if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags)) {
90                 dma_addr_t skb_dma;
91
92                 skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
93                                          DMA_FROM_DEVICE);
94                 if (unlikely(dma_mapping_error(rt2x00dev->dev, skb_dma))) {
95                         dev_kfree_skb_any(skb);
96                         return NULL;
97                 }
98
99                 skbdesc->skb_dma = skb_dma;
100                 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
101         }
102
103         return skb;
104 }
105
106 int rt2x00queue_map_txskb(struct queue_entry *entry)
107 {
108         struct device *dev = entry->queue->rt2x00dev->dev;
109         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
110
111         skbdesc->skb_dma =
112             dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
113
114         if (unlikely(dma_mapping_error(dev, skbdesc->skb_dma)))
115                 return -ENOMEM;
116
117         skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
118         return 0;
119 }
120 EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
121
122 void rt2x00queue_unmap_skb(struct queue_entry *entry)
123 {
124         struct device *dev = entry->queue->rt2x00dev->dev;
125         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
126
127         if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
128                 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
129                                  DMA_FROM_DEVICE);
130                 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
131         } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
132                 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
133                                  DMA_TO_DEVICE);
134                 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
135         }
136 }
137 EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
138
139 void rt2x00queue_free_skb(struct queue_entry *entry)
140 {
141         if (!entry->skb)
142                 return;
143
144         rt2x00queue_unmap_skb(entry);
145         dev_kfree_skb_any(entry->skb);
146         entry->skb = NULL;
147 }
148
149 void rt2x00queue_align_frame(struct sk_buff *skb)
150 {
151         unsigned int frame_length = skb->len;
152         unsigned int align = ALIGN_SIZE(skb, 0);
153
154         if (!align)
155                 return;
156
157         skb_push(skb, align);
158         memmove(skb->data, skb->data + align, frame_length);
159         skb_trim(skb, frame_length);
160 }
161
162 void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
163 {
164         unsigned int payload_length = skb->len - header_length;
165         unsigned int header_align = ALIGN_SIZE(skb, 0);
166         unsigned int payload_align = ALIGN_SIZE(skb, header_length);
167         unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
168
169         /*
170          * Adjust the header alignment if the payload needs to be moved more
171          * than the header.
172          */
173         if (payload_align > header_align)
174                 header_align += 4;
175
176         /* There is nothing to do if no alignment is needed */
177         if (!header_align)
178                 return;
179
180         /* Reserve the amount of space needed in front of the frame */
181         skb_push(skb, header_align);
182
183         /*
184          * Move the header.
185          */
186         memmove(skb->data, skb->data + header_align, header_length);
187
188         /* Move the payload, if present and if required */
189         if (payload_length && payload_align)
190                 memmove(skb->data + header_length + l2pad,
191                         skb->data + header_length + l2pad + payload_align,
192                         payload_length);
193
194         /* Trim the skb to the correct size */
195         skb_trim(skb, header_length + l2pad + payload_length);
196 }
197
198 void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
199 {
200         /*
201          * L2 padding is only present if the skb contains more than just the
202          * IEEE 802.11 header.
203          */
204         unsigned int l2pad = (skb->len > header_length) ?
205                                 L2PAD_SIZE(header_length) : 0;
206
207         if (!l2pad)
208                 return;
209
210         memmove(skb->data + l2pad, skb->data, header_length);
211         skb_pull(skb, l2pad);
212 }
213
214 static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
215                                                  struct sk_buff *skb,
216                                                  struct txentry_desc *txdesc)
217 {
218         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
219         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
220         struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
221         u16 seqno;
222
223         if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
224                 return;
225
226         __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
227
228         if (!test_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags)) {
229                 /*
230                  * rt2800 has a H/W (or F/W) bug, device incorrectly increase
231                  * seqno on retransmited data (non-QOS) frames. To workaround
232                  * the problem let's generate seqno in software if QOS is
233                  * disabled.
234                  */
235                 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
236                         __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
237                 else
238                         /* H/W will generate sequence number */
239                         return;
240         }
241
242         /*
243          * The hardware is not able to insert a sequence number. Assign a
244          * software generated one here.
245          *
246          * This is wrong because beacons are not getting sequence
247          * numbers assigned properly.
248          *
249          * A secondary problem exists for drivers that cannot toggle
250          * sequence counting per-frame, since those will override the
251          * sequence counter given by mac80211.
252          */
253         if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
254                 seqno = atomic_add_return(0x10, &intf->seqno);
255         else
256                 seqno = atomic_read(&intf->seqno);
257
258         hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
259         hdr->seq_ctrl |= cpu_to_le16(seqno);
260 }
261
262 static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
263                                                   struct sk_buff *skb,
264                                                   struct txentry_desc *txdesc,
265                                                   const struct rt2x00_rate *hwrate)
266 {
267         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
268         struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
269         unsigned int data_length;
270         unsigned int duration;
271         unsigned int residual;
272
273         /*
274          * Determine with what IFS priority this frame should be send.
275          * Set ifs to IFS_SIFS when the this is not the first fragment,
276          * or this fragment came after RTS/CTS.
277          */
278         if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
279                 txdesc->u.plcp.ifs = IFS_BACKOFF;
280         else
281                 txdesc->u.plcp.ifs = IFS_SIFS;
282
283         /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
284         data_length = skb->len + 4;
285         data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
286
287         /*
288          * PLCP setup
289          * Length calculation depends on OFDM/CCK rate.
290          */
291         txdesc->u.plcp.signal = hwrate->plcp;
292         txdesc->u.plcp.service = 0x04;
293
294         if (hwrate->flags & DEV_RATE_OFDM) {
295                 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
296                 txdesc->u.plcp.length_low = data_length & 0x3f;
297         } else {
298                 /*
299                  * Convert length to microseconds.
300                  */
301                 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
302                 duration = GET_DURATION(data_length, hwrate->bitrate);
303
304                 if (residual != 0) {
305                         duration++;
306
307                         /*
308                          * Check if we need to set the Length Extension
309                          */
310                         if (hwrate->bitrate == 110 && residual <= 30)
311                                 txdesc->u.plcp.service |= 0x80;
312                 }
313
314                 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
315                 txdesc->u.plcp.length_low = duration & 0xff;
316
317                 /*
318                  * When preamble is enabled we should set the
319                  * preamble bit for the signal.
320                  */
321                 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
322                         txdesc->u.plcp.signal |= 0x08;
323         }
324 }
325
326 static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
327                                                 struct sk_buff *skb,
328                                                 struct txentry_desc *txdesc,
329                                                 struct ieee80211_sta *sta,
330                                                 const struct rt2x00_rate *hwrate)
331 {
332         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
333         struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
334         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
335         struct rt2x00_sta *sta_priv = NULL;
336
337         if (sta) {
338                 txdesc->u.ht.mpdu_density =
339                     sta->ht_cap.ampdu_density;
340
341                 sta_priv = sta_to_rt2x00_sta(sta);
342                 txdesc->u.ht.wcid = sta_priv->wcid;
343         }
344
345         /*
346          * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
347          * mcs rate to be used
348          */
349         if (txrate->flags & IEEE80211_TX_RC_MCS) {
350                 txdesc->u.ht.mcs = txrate->idx;
351
352                 /*
353                  * MIMO PS should be set to 1 for STA's using dynamic SM PS
354                  * when using more then one tx stream (>MCS7).
355                  */
356                 if (sta && txdesc->u.ht.mcs > 7 &&
357                     sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
358                         __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
359         } else {
360                 txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
361                 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
362                         txdesc->u.ht.mcs |= 0x08;
363         }
364
365         if (test_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags)) {
366                 if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
367                         txdesc->u.ht.txop = TXOP_SIFS;
368                 else
369                         txdesc->u.ht.txop = TXOP_BACKOFF;
370
371                 /* Left zero on all other settings. */
372                 return;
373         }
374
375         txdesc->u.ht.ba_size = 7;       /* FIXME: What value is needed? */
376
377         /*
378          * Only one STBC stream is supported for now.
379          */
380         if (tx_info->flags & IEEE80211_TX_CTL_STBC)
381                 txdesc->u.ht.stbc = 1;
382
383         /*
384          * This frame is eligible for an AMPDU, however, don't aggregate
385          * frames that are intended to probe a specific tx rate.
386          */
387         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
388             !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
389                 __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
390
391         /*
392          * Set 40Mhz mode if necessary (for legacy rates this will
393          * duplicate the frame to both channels).
394          */
395         if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
396             txrate->flags & IEEE80211_TX_RC_DUP_DATA)
397                 __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
398         if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
399                 __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
400
401         /*
402          * Determine IFS values
403          * - Use TXOP_BACKOFF for management frames except beacons
404          * - Use TXOP_SIFS for fragment bursts
405          * - Use TXOP_HTTXOP for everything else
406          *
407          * Note: rt2800 devices won't use CTS protection (if used)
408          * for frames not transmitted with TXOP_HTTXOP
409          */
410         if (ieee80211_is_mgmt(hdr->frame_control) &&
411             !ieee80211_is_beacon(hdr->frame_control))
412                 txdesc->u.ht.txop = TXOP_BACKOFF;
413         else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
414                 txdesc->u.ht.txop = TXOP_SIFS;
415         else
416                 txdesc->u.ht.txop = TXOP_HTTXOP;
417 }
418
419 static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
420                                              struct sk_buff *skb,
421                                              struct txentry_desc *txdesc,
422                                              struct ieee80211_sta *sta)
423 {
424         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
425         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
426         struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
427         struct ieee80211_rate *rate;
428         const struct rt2x00_rate *hwrate = NULL;
429
430         memset(txdesc, 0, sizeof(*txdesc));
431
432         /*
433          * Header and frame information.
434          */
435         txdesc->length = skb->len;
436         txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
437
438         /*
439          * Check whether this frame is to be acked.
440          */
441         if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
442                 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
443
444         /*
445          * Check if this is a RTS/CTS frame
446          */
447         if (ieee80211_is_rts(hdr->frame_control) ||
448             ieee80211_is_cts(hdr->frame_control)) {
449                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
450                 if (ieee80211_is_rts(hdr->frame_control))
451                         __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
452                 else
453                         __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
454                 if (tx_info->control.rts_cts_rate_idx >= 0)
455                         rate =
456                             ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
457         }
458
459         /*
460          * Determine retry information.
461          */
462         txdesc->retry_limit = tx_info->control.rates[0].count - 1;
463         if (txdesc->retry_limit >= rt2x00dev->long_retry)
464                 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
465
466         /*
467          * Check if more fragments are pending
468          */
469         if (ieee80211_has_morefrags(hdr->frame_control)) {
470                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
471                 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
472         }
473
474         /*
475          * Check if more frames (!= fragments) are pending
476          */
477         if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
478                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
479
480         /*
481          * Beacons and probe responses require the tsf timestamp
482          * to be inserted into the frame.
483          */
484         if (ieee80211_is_beacon(hdr->frame_control) ||
485             ieee80211_is_probe_resp(hdr->frame_control))
486                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
487
488         if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
489             !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
490                 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
491
492         /*
493          * Determine rate modulation.
494          */
495         if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
496                 txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
497         else if (txrate->flags & IEEE80211_TX_RC_MCS)
498                 txdesc->rate_mode = RATE_MODE_HT_MIX;
499         else {
500                 rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
501                 hwrate = rt2x00_get_rate(rate->hw_value);
502                 if (hwrate->flags & DEV_RATE_OFDM)
503                         txdesc->rate_mode = RATE_MODE_OFDM;
504                 else
505                         txdesc->rate_mode = RATE_MODE_CCK;
506         }
507
508         /*
509          * Apply TX descriptor handling by components
510          */
511         rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
512         rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
513
514         if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
515                 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
516                                                    sta, hwrate);
517         else
518                 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
519                                                       hwrate);
520 }
521
522 static int rt2x00queue_write_tx_data(struct queue_entry *entry,
523                                      struct txentry_desc *txdesc)
524 {
525         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
526
527         /*
528          * This should not happen, we already checked the entry
529          * was ours. When the hardware disagrees there has been
530          * a queue corruption!
531          */
532         if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
533                      rt2x00dev->ops->lib->get_entry_state(entry))) {
534                 ERROR(rt2x00dev,
535                       "Corrupt queue %d, accessing entry which is not ours.\n"
536                       "Please file bug report to %s.\n",
537                       entry->queue->qid, DRV_PROJECT);
538                 return -EINVAL;
539         }
540
541         /*
542          * Add the requested extra tx headroom in front of the skb.
543          */
544         skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
545         memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
546
547         /*
548          * Call the driver's write_tx_data function, if it exists.
549          */
550         if (rt2x00dev->ops->lib->write_tx_data)
551                 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
552
553         /*
554          * Map the skb to DMA.
555          */
556         if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags) &&
557             rt2x00queue_map_txskb(entry))
558                 return -ENOMEM;
559
560         return 0;
561 }
562
563 static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
564                                             struct txentry_desc *txdesc)
565 {
566         struct data_queue *queue = entry->queue;
567
568         queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
569
570         /*
571          * All processing on the frame has been completed, this means
572          * it is now ready to be dumped to userspace through debugfs.
573          */
574         rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
575 }
576
577 static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
578                                       struct txentry_desc *txdesc)
579 {
580         /*
581          * Check if we need to kick the queue, there are however a few rules
582          *      1) Don't kick unless this is the last in frame in a burst.
583          *         When the burst flag is set, this frame is always followed
584          *         by another frame which in some way are related to eachother.
585          *         This is true for fragments, RTS or CTS-to-self frames.
586          *      2) Rule 1 can be broken when the available entries
587          *         in the queue are less then a certain threshold.
588          */
589         if (rt2x00queue_threshold(queue) ||
590             !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
591                 queue->rt2x00dev->ops->lib->kick_queue(queue);
592 }
593
594 static void rt2x00queue_bar_check(struct queue_entry *entry)
595 {
596         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
597         struct ieee80211_bar *bar = (void *) (entry->skb->data +
598                                     rt2x00dev->ops->extra_tx_headroom);
599         struct rt2x00_bar_list_entry *bar_entry;
600
601         if (likely(!ieee80211_is_back_req(bar->frame_control)))
602                 return;
603
604         bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
605
606         /*
607          * If the alloc fails we still send the BAR out but just don't track
608          * it in our bar list. And as a result we will report it to mac80211
609          * back as failed.
610          */
611         if (!bar_entry)
612                 return;
613
614         bar_entry->entry = entry;
615         bar_entry->block_acked = 0;
616
617         /*
618          * Copy the relevant parts of the 802.11 BAR into out check list
619          * such that we can use RCU for less-overhead in the RX path since
620          * sending BARs and processing the according BlockAck should be
621          * the exception.
622          */
623         memcpy(bar_entry->ra, bar->ra, sizeof(bar->ra));
624         memcpy(bar_entry->ta, bar->ta, sizeof(bar->ta));
625         bar_entry->control = bar->control;
626         bar_entry->start_seq_num = bar->start_seq_num;
627
628         /*
629          * Insert BAR into our BAR check list.
630          */
631         spin_lock_bh(&rt2x00dev->bar_list_lock);
632         list_add_tail_rcu(&bar_entry->list, &rt2x00dev->bar_list);
633         spin_unlock_bh(&rt2x00dev->bar_list_lock);
634 }
635
636 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
637                                bool local)
638 {
639         struct ieee80211_tx_info *tx_info;
640         struct queue_entry *entry;
641         struct txentry_desc txdesc;
642         struct skb_frame_desc *skbdesc;
643         u8 rate_idx, rate_flags;
644         int ret = 0;
645
646         /*
647          * Copy all TX descriptor information into txdesc,
648          * after that we are free to use the skb->cb array
649          * for our information.
650          */
651         rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, NULL);
652
653         /*
654          * All information is retrieved from the skb->cb array,
655          * now we should claim ownership of the driver part of that
656          * array, preserving the bitrate index and flags.
657          */
658         tx_info = IEEE80211_SKB_CB(skb);
659         rate_idx = tx_info->control.rates[0].idx;
660         rate_flags = tx_info->control.rates[0].flags;
661         skbdesc = get_skb_frame_desc(skb);
662         memset(skbdesc, 0, sizeof(*skbdesc));
663         skbdesc->tx_rate_idx = rate_idx;
664         skbdesc->tx_rate_flags = rate_flags;
665
666         if (local)
667                 skbdesc->flags |= SKBDESC_NOT_MAC80211;
668
669         /*
670          * When hardware encryption is supported, and this frame
671          * is to be encrypted, we should strip the IV/EIV data from
672          * the frame so we can provide it to the driver separately.
673          */
674         if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
675             !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
676                 if (test_bit(REQUIRE_COPY_IV, &queue->rt2x00dev->cap_flags))
677                         rt2x00crypto_tx_copy_iv(skb, &txdesc);
678                 else
679                         rt2x00crypto_tx_remove_iv(skb, &txdesc);
680         }
681
682         /*
683          * When DMA allocation is required we should guarantee to the
684          * driver that the DMA is aligned to a 4-byte boundary.
685          * However some drivers require L2 padding to pad the payload
686          * rather then the header. This could be a requirement for
687          * PCI and USB devices, while header alignment only is valid
688          * for PCI devices.
689          */
690         if (test_bit(REQUIRE_L2PAD, &queue->rt2x00dev->cap_flags))
691                 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
692         else if (test_bit(REQUIRE_DMA, &queue->rt2x00dev->cap_flags))
693                 rt2x00queue_align_frame(skb);
694
695         /*
696          * That function must be called with bh disabled.
697          */
698         spin_lock(&queue->tx_lock);
699
700         if (unlikely(rt2x00queue_full(queue))) {
701                 ERROR(queue->rt2x00dev,
702                       "Dropping frame due to full tx queue %d.\n", queue->qid);
703                 ret = -ENOBUFS;
704                 goto out;
705         }
706
707         entry = rt2x00queue_get_entry(queue, Q_INDEX);
708
709         if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
710                                       &entry->flags))) {
711                 ERROR(queue->rt2x00dev,
712                       "Arrived at non-free entry in the non-full queue %d.\n"
713                       "Please file bug report to %s.\n",
714                       queue->qid, DRV_PROJECT);
715                 ret = -EINVAL;
716                 goto out;
717         }
718
719         skbdesc->entry = entry;
720         entry->skb = skb;
721
722         /*
723          * It could be possible that the queue was corrupted and this
724          * call failed. Since we always return NETDEV_TX_OK to mac80211,
725          * this frame will simply be dropped.
726          */
727         if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
728                 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
729                 entry->skb = NULL;
730                 ret = -EIO;
731                 goto out;
732         }
733
734         /*
735          * Put BlockAckReqs into our check list for driver BA processing.
736          */
737         rt2x00queue_bar_check(entry);
738
739         set_bit(ENTRY_DATA_PENDING, &entry->flags);
740
741         rt2x00queue_index_inc(entry, Q_INDEX);
742         rt2x00queue_write_tx_descriptor(entry, &txdesc);
743         rt2x00queue_kick_tx_queue(queue, &txdesc);
744
745 out:
746         spin_unlock(&queue->tx_lock);
747         return ret;
748 }
749
750 int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
751                              struct ieee80211_vif *vif)
752 {
753         struct rt2x00_intf *intf = vif_to_intf(vif);
754
755         if (unlikely(!intf->beacon))
756                 return -ENOBUFS;
757
758         mutex_lock(&intf->beacon_skb_mutex);
759
760         /*
761          * Clean up the beacon skb.
762          */
763         rt2x00queue_free_skb(intf->beacon);
764
765         /*
766          * Clear beacon (single bssid devices don't need to clear the beacon
767          * since the beacon queue will get stopped anyway).
768          */
769         if (rt2x00dev->ops->lib->clear_beacon)
770                 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
771
772         mutex_unlock(&intf->beacon_skb_mutex);
773
774         return 0;
775 }
776
777 int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
778                                      struct ieee80211_vif *vif)
779 {
780         struct rt2x00_intf *intf = vif_to_intf(vif);
781         struct skb_frame_desc *skbdesc;
782         struct txentry_desc txdesc;
783
784         if (unlikely(!intf->beacon))
785                 return -ENOBUFS;
786
787         /*
788          * Clean up the beacon skb.
789          */
790         rt2x00queue_free_skb(intf->beacon);
791
792         intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
793         if (!intf->beacon->skb)
794                 return -ENOMEM;
795
796         /*
797          * Copy all TX descriptor information into txdesc,
798          * after that we are free to use the skb->cb array
799          * for our information.
800          */
801         rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
802
803         /*
804          * Fill in skb descriptor
805          */
806         skbdesc = get_skb_frame_desc(intf->beacon->skb);
807         memset(skbdesc, 0, sizeof(*skbdesc));
808         skbdesc->entry = intf->beacon;
809
810         /*
811          * Send beacon to hardware.
812          */
813         rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
814
815         return 0;
816
817 }
818
819 int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
820                               struct ieee80211_vif *vif)
821 {
822         struct rt2x00_intf *intf = vif_to_intf(vif);
823         int ret;
824
825         mutex_lock(&intf->beacon_skb_mutex);
826         ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
827         mutex_unlock(&intf->beacon_skb_mutex);
828
829         return ret;
830 }
831
832 bool rt2x00queue_for_each_entry(struct data_queue *queue,
833                                 enum queue_index start,
834                                 enum queue_index end,
835                                 void *data,
836                                 bool (*fn)(struct queue_entry *entry,
837                                            void *data))
838 {
839         unsigned long irqflags;
840         unsigned int index_start;
841         unsigned int index_end;
842         unsigned int i;
843
844         if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
845                 ERROR(queue->rt2x00dev,
846                       "Entry requested from invalid index range (%d - %d)\n",
847                       start, end);
848                 return true;
849         }
850
851         /*
852          * Only protect the range we are going to loop over,
853          * if during our loop a extra entry is set to pending
854          * it should not be kicked during this run, since it
855          * is part of another TX operation.
856          */
857         spin_lock_irqsave(&queue->index_lock, irqflags);
858         index_start = queue->index[start];
859         index_end = queue->index[end];
860         spin_unlock_irqrestore(&queue->index_lock, irqflags);
861
862         /*
863          * Start from the TX done pointer, this guarantees that we will
864          * send out all frames in the correct order.
865          */
866         if (index_start < index_end) {
867                 for (i = index_start; i < index_end; i++) {
868                         if (fn(&queue->entries[i], data))
869                                 return true;
870                 }
871         } else {
872                 for (i = index_start; i < queue->limit; i++) {
873                         if (fn(&queue->entries[i], data))
874                                 return true;
875                 }
876
877                 for (i = 0; i < index_end; i++) {
878                         if (fn(&queue->entries[i], data))
879                                 return true;
880                 }
881         }
882
883         return false;
884 }
885 EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
886
887 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
888                                           enum queue_index index)
889 {
890         struct queue_entry *entry;
891         unsigned long irqflags;
892
893         if (unlikely(index >= Q_INDEX_MAX)) {
894                 ERROR(queue->rt2x00dev,
895                       "Entry requested from invalid index type (%d)\n", index);
896                 return NULL;
897         }
898
899         spin_lock_irqsave(&queue->index_lock, irqflags);
900
901         entry = &queue->entries[queue->index[index]];
902
903         spin_unlock_irqrestore(&queue->index_lock, irqflags);
904
905         return entry;
906 }
907 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
908
909 void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
910 {
911         struct data_queue *queue = entry->queue;
912         unsigned long irqflags;
913
914         if (unlikely(index >= Q_INDEX_MAX)) {
915                 ERROR(queue->rt2x00dev,
916                       "Index change on invalid index type (%d)\n", index);
917                 return;
918         }
919
920         spin_lock_irqsave(&queue->index_lock, irqflags);
921
922         queue->index[index]++;
923         if (queue->index[index] >= queue->limit)
924                 queue->index[index] = 0;
925
926         entry->last_action = jiffies;
927
928         if (index == Q_INDEX) {
929                 queue->length++;
930         } else if (index == Q_INDEX_DONE) {
931                 queue->length--;
932                 queue->count++;
933         }
934
935         spin_unlock_irqrestore(&queue->index_lock, irqflags);
936 }
937
938 void rt2x00queue_pause_queue(struct data_queue *queue)
939 {
940         if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
941             !test_bit(QUEUE_STARTED, &queue->flags) ||
942             test_and_set_bit(QUEUE_PAUSED, &queue->flags))
943                 return;
944
945         switch (queue->qid) {
946         case QID_AC_VO:
947         case QID_AC_VI:
948         case QID_AC_BE:
949         case QID_AC_BK:
950                 /*
951                  * For TX queues, we have to disable the queue
952                  * inside mac80211.
953                  */
954                 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
955                 break;
956         default:
957                 break;
958         }
959 }
960 EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
961
962 void rt2x00queue_unpause_queue(struct data_queue *queue)
963 {
964         if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
965             !test_bit(QUEUE_STARTED, &queue->flags) ||
966             !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
967                 return;
968
969         switch (queue->qid) {
970         case QID_AC_VO:
971         case QID_AC_VI:
972         case QID_AC_BE:
973         case QID_AC_BK:
974                 /*
975                  * For TX queues, we have to enable the queue
976                  * inside mac80211.
977                  */
978                 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
979                 break;
980         case QID_RX:
981                 /*
982                  * For RX we need to kick the queue now in order to
983                  * receive frames.
984                  */
985                 queue->rt2x00dev->ops->lib->kick_queue(queue);
986         default:
987                 break;
988         }
989 }
990 EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
991
992 void rt2x00queue_start_queue(struct data_queue *queue)
993 {
994         mutex_lock(&queue->status_lock);
995
996         if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
997             test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
998                 mutex_unlock(&queue->status_lock);
999                 return;
1000         }
1001
1002         set_bit(QUEUE_PAUSED, &queue->flags);
1003
1004         queue->rt2x00dev->ops->lib->start_queue(queue);
1005
1006         rt2x00queue_unpause_queue(queue);
1007
1008         mutex_unlock(&queue->status_lock);
1009 }
1010 EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
1011
1012 void rt2x00queue_stop_queue(struct data_queue *queue)
1013 {
1014         mutex_lock(&queue->status_lock);
1015
1016         if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
1017                 mutex_unlock(&queue->status_lock);
1018                 return;
1019         }
1020
1021         rt2x00queue_pause_queue(queue);
1022
1023         queue->rt2x00dev->ops->lib->stop_queue(queue);
1024
1025         mutex_unlock(&queue->status_lock);
1026 }
1027 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
1028
1029 void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
1030 {
1031         bool started;
1032         bool tx_queue =
1033                 (queue->qid == QID_AC_VO) ||
1034                 (queue->qid == QID_AC_VI) ||
1035                 (queue->qid == QID_AC_BE) ||
1036                 (queue->qid == QID_AC_BK);
1037
1038         mutex_lock(&queue->status_lock);
1039
1040         /*
1041          * If the queue has been started, we must stop it temporarily
1042          * to prevent any new frames to be queued on the device. If
1043          * we are not dropping the pending frames, the queue must
1044          * only be stopped in the software and not the hardware,
1045          * otherwise the queue will never become empty on its own.
1046          */
1047         started = test_bit(QUEUE_STARTED, &queue->flags);
1048         if (started) {
1049                 /*
1050                  * Pause the queue
1051                  */
1052                 rt2x00queue_pause_queue(queue);
1053
1054                 /*
1055                  * If we are not supposed to drop any pending
1056                  * frames, this means we must force a start (=kick)
1057                  * to the queue to make sure the hardware will
1058                  * start transmitting.
1059                  */
1060                 if (!drop && tx_queue)
1061                         queue->rt2x00dev->ops->lib->kick_queue(queue);
1062         }
1063
1064         /*
1065          * Check if driver supports flushing, if that is the case we can
1066          * defer the flushing to the driver. Otherwise we must use the
1067          * alternative which just waits for the queue to become empty.
1068          */
1069         if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1070                 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
1071
1072         /*
1073          * The queue flush has failed...
1074          */
1075         if (unlikely(!rt2x00queue_empty(queue)))
1076                 WARNING(queue->rt2x00dev, "Queue %d failed to flush\n", queue->qid);
1077
1078         /*
1079          * Restore the queue to the previous status
1080          */
1081         if (started)
1082                 rt2x00queue_unpause_queue(queue);
1083
1084         mutex_unlock(&queue->status_lock);
1085 }
1086 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1087
1088 void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1089 {
1090         struct data_queue *queue;
1091
1092         /*
1093          * rt2x00queue_start_queue will call ieee80211_wake_queue
1094          * for each queue after is has been properly initialized.
1095          */
1096         tx_queue_for_each(rt2x00dev, queue)
1097                 rt2x00queue_start_queue(queue);
1098
1099         rt2x00queue_start_queue(rt2x00dev->rx);
1100 }
1101 EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1102
1103 void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1104 {
1105         struct data_queue *queue;
1106
1107         /*
1108          * rt2x00queue_stop_queue will call ieee80211_stop_queue
1109          * as well, but we are completely shutting doing everything
1110          * now, so it is much safer to stop all TX queues at once,
1111          * and use rt2x00queue_stop_queue for cleaning up.
1112          */
1113         ieee80211_stop_queues(rt2x00dev->hw);
1114
1115         tx_queue_for_each(rt2x00dev, queue)
1116                 rt2x00queue_stop_queue(queue);
1117
1118         rt2x00queue_stop_queue(rt2x00dev->rx);
1119 }
1120 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1121
1122 void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1123 {
1124         struct data_queue *queue;
1125
1126         tx_queue_for_each(rt2x00dev, queue)
1127                 rt2x00queue_flush_queue(queue, drop);
1128
1129         rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1130 }
1131 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1132
1133 static void rt2x00queue_reset(struct data_queue *queue)
1134 {
1135         unsigned long irqflags;
1136         unsigned int i;
1137
1138         spin_lock_irqsave(&queue->index_lock, irqflags);
1139
1140         queue->count = 0;
1141         queue->length = 0;
1142
1143         for (i = 0; i < Q_INDEX_MAX; i++)
1144                 queue->index[i] = 0;
1145
1146         spin_unlock_irqrestore(&queue->index_lock, irqflags);
1147 }
1148
1149 void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
1150 {
1151         struct data_queue *queue;
1152         unsigned int i;
1153
1154         queue_for_each(rt2x00dev, queue) {
1155                 rt2x00queue_reset(queue);
1156
1157                 for (i = 0; i < queue->limit; i++)
1158                         rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
1159         }
1160 }
1161
1162 static int rt2x00queue_alloc_entries(struct data_queue *queue,
1163                                      const struct data_queue_desc *qdesc)
1164 {
1165         struct queue_entry *entries;
1166         unsigned int entry_size;
1167         unsigned int i;
1168
1169         rt2x00queue_reset(queue);
1170
1171         queue->limit = qdesc->entry_num;
1172         queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
1173         queue->data_size = qdesc->data_size;
1174         queue->desc_size = qdesc->desc_size;
1175
1176         /*
1177          * Allocate all queue entries.
1178          */
1179         entry_size = sizeof(*entries) + qdesc->priv_size;
1180         entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
1181         if (!entries)
1182                 return -ENOMEM;
1183
1184 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
1185         (((char *)(__base)) + ((__limit) * (__esize)) + \
1186             ((__index) * (__psize)))
1187
1188         for (i = 0; i < queue->limit; i++) {
1189                 entries[i].flags = 0;
1190                 entries[i].queue = queue;
1191                 entries[i].skb = NULL;
1192                 entries[i].entry_idx = i;
1193                 entries[i].priv_data =
1194                     QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
1195                                             sizeof(*entries), qdesc->priv_size);
1196         }
1197
1198 #undef QUEUE_ENTRY_PRIV_OFFSET
1199
1200         queue->entries = entries;
1201
1202         return 0;
1203 }
1204
1205 static void rt2x00queue_free_skbs(struct data_queue *queue)
1206 {
1207         unsigned int i;
1208
1209         if (!queue->entries)
1210                 return;
1211
1212         for (i = 0; i < queue->limit; i++) {
1213                 rt2x00queue_free_skb(&queue->entries[i]);
1214         }
1215 }
1216
1217 static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
1218 {
1219         unsigned int i;
1220         struct sk_buff *skb;
1221
1222         for (i = 0; i < queue->limit; i++) {
1223                 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
1224                 if (!skb)
1225                         return -ENOMEM;
1226                 queue->entries[i].skb = skb;
1227         }
1228
1229         return 0;
1230 }
1231
1232 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1233 {
1234         struct data_queue *queue;
1235         int status;
1236
1237         status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
1238         if (status)
1239                 goto exit;
1240
1241         tx_queue_for_each(rt2x00dev, queue) {
1242                 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
1243                 if (status)
1244                         goto exit;
1245         }
1246
1247         status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
1248         if (status)
1249                 goto exit;
1250
1251         if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
1252                 status = rt2x00queue_alloc_entries(rt2x00dev->atim,
1253                                                    rt2x00dev->ops->atim);
1254                 if (status)
1255                         goto exit;
1256         }
1257
1258         status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
1259         if (status)
1260                 goto exit;
1261
1262         return 0;
1263
1264 exit:
1265         ERROR(rt2x00dev, "Queue entries allocation failed.\n");
1266
1267         rt2x00queue_uninitialize(rt2x00dev);
1268
1269         return status;
1270 }
1271
1272 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1273 {
1274         struct data_queue *queue;
1275
1276         rt2x00queue_free_skbs(rt2x00dev->rx);
1277
1278         queue_for_each(rt2x00dev, queue) {
1279                 kfree(queue->entries);
1280                 queue->entries = NULL;
1281         }
1282 }
1283
1284 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1285                              struct data_queue *queue, enum data_queue_qid qid)
1286 {
1287         mutex_init(&queue->status_lock);
1288         spin_lock_init(&queue->tx_lock);
1289         spin_lock_init(&queue->index_lock);
1290
1291         queue->rt2x00dev = rt2x00dev;
1292         queue->qid = qid;
1293         queue->txop = 0;
1294         queue->aifs = 2;
1295         queue->cw_min = 5;
1296         queue->cw_max = 10;
1297 }
1298
1299 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1300 {
1301         struct data_queue *queue;
1302         enum data_queue_qid qid;
1303         unsigned int req_atim =
1304             !!test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
1305
1306         /*
1307          * We need the following queues:
1308          * RX: 1
1309          * TX: ops->tx_queues
1310          * Beacon: 1
1311          * Atim: 1 (if required)
1312          */
1313         rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
1314
1315         queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
1316         if (!queue) {
1317                 ERROR(rt2x00dev, "Queue allocation failed.\n");
1318                 return -ENOMEM;
1319         }
1320
1321         /*
1322          * Initialize pointers
1323          */
1324         rt2x00dev->rx = queue;
1325         rt2x00dev->tx = &queue[1];
1326         rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
1327         rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
1328
1329         /*
1330          * Initialize queue parameters.
1331          * RX: qid = QID_RX
1332          * TX: qid = QID_AC_VO + index
1333          * TX: cw_min: 2^5 = 32.
1334          * TX: cw_max: 2^10 = 1024.
1335          * BCN: qid = QID_BEACON
1336          * ATIM: qid = QID_ATIM
1337          */
1338         rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1339
1340         qid = QID_AC_VO;
1341         tx_queue_for_each(rt2x00dev, queue)
1342                 rt2x00queue_init(rt2x00dev, queue, qid++);
1343
1344         rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
1345         if (req_atim)
1346                 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
1347
1348         return 0;
1349 }
1350
1351 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1352 {
1353         kfree(rt2x00dev->rx);
1354         rt2x00dev->rx = NULL;
1355         rt2x00dev->tx = NULL;
1356         rt2x00dev->bcn = NULL;
1357 }