Linux 3.11-rc3
[cris-mirror.git] / drivers / net / wireless / rt2x00 / rt2x00queue.c
blob6c0a91ff963c6833bb7d325cac7954c7c10b41fc
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>
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.
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.
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.
24 Module: rt2x00lib
25 Abstract: rt2x00 queue specific routines.
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/dma-mapping.h>
33 #include "rt2x00.h"
34 #include "rt2x00lib.h"
36 struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry, gfp_t gfp)
38 struct data_queue *queue = entry->queue;
39 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
40 struct sk_buff *skb;
41 struct skb_frame_desc *skbdesc;
42 unsigned int frame_size;
43 unsigned int head_size = 0;
44 unsigned int tail_size = 0;
47 * The frame size includes descriptor size, because the
48 * hardware directly receive the frame into the skbuffer.
50 frame_size = queue->data_size + queue->desc_size + queue->winfo_size;
53 * The payload should be aligned to a 4-byte boundary,
54 * this means we need at least 3 bytes for moving the frame
55 * into the correct offset.
57 head_size = 4;
60 * For IV/EIV/ICV assembly we must make sure there is
61 * at least 8 bytes bytes available in headroom for IV/EIV
62 * and 8 bytes for ICV data as tailroon.
64 if (test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags)) {
65 head_size += 8;
66 tail_size += 8;
70 * Allocate skbuffer.
72 skb = __dev_alloc_skb(frame_size + head_size + tail_size, gfp);
73 if (!skb)
74 return NULL;
77 * Make sure we not have a frame with the requested bytes
78 * available in the head and tail.
80 skb_reserve(skb, head_size);
81 skb_put(skb, frame_size);
84 * Populate skbdesc.
86 skbdesc = get_skb_frame_desc(skb);
87 memset(skbdesc, 0, sizeof(*skbdesc));
88 skbdesc->entry = entry;
90 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags)) {
91 dma_addr_t skb_dma;
93 skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
94 DMA_FROM_DEVICE);
95 if (unlikely(dma_mapping_error(rt2x00dev->dev, skb_dma))) {
96 dev_kfree_skb_any(skb);
97 return NULL;
100 skbdesc->skb_dma = skb_dma;
101 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
104 return skb;
107 int rt2x00queue_map_txskb(struct queue_entry *entry)
109 struct device *dev = entry->queue->rt2x00dev->dev;
110 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
112 skbdesc->skb_dma =
113 dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
115 if (unlikely(dma_mapping_error(dev, skbdesc->skb_dma)))
116 return -ENOMEM;
118 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
119 return 0;
121 EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
123 void rt2x00queue_unmap_skb(struct queue_entry *entry)
125 struct device *dev = entry->queue->rt2x00dev->dev;
126 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
128 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
129 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
130 DMA_FROM_DEVICE);
131 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
132 } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
133 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
134 DMA_TO_DEVICE);
135 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
138 EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
140 void rt2x00queue_free_skb(struct queue_entry *entry)
142 if (!entry->skb)
143 return;
145 rt2x00queue_unmap_skb(entry);
146 dev_kfree_skb_any(entry->skb);
147 entry->skb = NULL;
150 void rt2x00queue_align_frame(struct sk_buff *skb)
152 unsigned int frame_length = skb->len;
153 unsigned int align = ALIGN_SIZE(skb, 0);
155 if (!align)
156 return;
158 skb_push(skb, align);
159 memmove(skb->data, skb->data + align, frame_length);
160 skb_trim(skb, frame_length);
163 void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
165 unsigned int payload_length = skb->len - header_length;
166 unsigned int header_align = ALIGN_SIZE(skb, 0);
167 unsigned int payload_align = ALIGN_SIZE(skb, header_length);
168 unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
171 * Adjust the header alignment if the payload needs to be moved more
172 * than the header.
174 if (payload_align > header_align)
175 header_align += 4;
177 /* There is nothing to do if no alignment is needed */
178 if (!header_align)
179 return;
181 /* Reserve the amount of space needed in front of the frame */
182 skb_push(skb, header_align);
185 * Move the header.
187 memmove(skb->data, skb->data + header_align, header_length);
189 /* Move the payload, if present and if required */
190 if (payload_length && payload_align)
191 memmove(skb->data + header_length + l2pad,
192 skb->data + header_length + l2pad + payload_align,
193 payload_length);
195 /* Trim the skb to the correct size */
196 skb_trim(skb, header_length + l2pad + payload_length);
199 void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
202 * L2 padding is only present if the skb contains more than just the
203 * IEEE 802.11 header.
205 unsigned int l2pad = (skb->len > header_length) ?
206 L2PAD_SIZE(header_length) : 0;
208 if (!l2pad)
209 return;
211 memmove(skb->data + l2pad, skb->data, header_length);
212 skb_pull(skb, l2pad);
215 static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
216 struct sk_buff *skb,
217 struct txentry_desc *txdesc)
219 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
220 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
221 struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
222 u16 seqno;
224 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
225 return;
227 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
229 if (!test_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags)) {
231 * rt2800 has a H/W (or F/W) bug, device incorrectly increase
232 * seqno on retransmited data (non-QOS) frames. To workaround
233 * the problem let's generate seqno in software if QOS is
234 * disabled.
236 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
237 __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
238 else
239 /* H/W will generate sequence number */
240 return;
244 * The hardware is not able to insert a sequence number. Assign a
245 * software generated one here.
247 * This is wrong because beacons are not getting sequence
248 * numbers assigned properly.
250 * A secondary problem exists for drivers that cannot toggle
251 * sequence counting per-frame, since those will override the
252 * sequence counter given by mac80211.
254 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
255 seqno = atomic_add_return(0x10, &intf->seqno);
256 else
257 seqno = atomic_read(&intf->seqno);
259 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
260 hdr->seq_ctrl |= cpu_to_le16(seqno);
263 static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
264 struct sk_buff *skb,
265 struct txentry_desc *txdesc,
266 const struct rt2x00_rate *hwrate)
268 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
269 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
270 unsigned int data_length;
271 unsigned int duration;
272 unsigned int residual;
275 * Determine with what IFS priority this frame should be send.
276 * Set ifs to IFS_SIFS when the this is not the first fragment,
277 * or this fragment came after RTS/CTS.
279 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
280 txdesc->u.plcp.ifs = IFS_BACKOFF;
281 else
282 txdesc->u.plcp.ifs = IFS_SIFS;
284 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
285 data_length = skb->len + 4;
286 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
289 * PLCP setup
290 * Length calculation depends on OFDM/CCK rate.
292 txdesc->u.plcp.signal = hwrate->plcp;
293 txdesc->u.plcp.service = 0x04;
295 if (hwrate->flags & DEV_RATE_OFDM) {
296 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
297 txdesc->u.plcp.length_low = data_length & 0x3f;
298 } else {
300 * Convert length to microseconds.
302 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
303 duration = GET_DURATION(data_length, hwrate->bitrate);
305 if (residual != 0) {
306 duration++;
309 * Check if we need to set the Length Extension
311 if (hwrate->bitrate == 110 && residual <= 30)
312 txdesc->u.plcp.service |= 0x80;
315 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
316 txdesc->u.plcp.length_low = duration & 0xff;
319 * When preamble is enabled we should set the
320 * preamble bit for the signal.
322 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
323 txdesc->u.plcp.signal |= 0x08;
327 static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
328 struct sk_buff *skb,
329 struct txentry_desc *txdesc,
330 struct ieee80211_sta *sta,
331 const struct rt2x00_rate *hwrate)
333 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
334 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
335 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
336 struct rt2x00_sta *sta_priv = NULL;
338 if (sta) {
339 txdesc->u.ht.mpdu_density =
340 sta->ht_cap.ampdu_density;
342 sta_priv = sta_to_rt2x00_sta(sta);
343 txdesc->u.ht.wcid = sta_priv->wcid;
347 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
348 * mcs rate to be used
350 if (txrate->flags & IEEE80211_TX_RC_MCS) {
351 txdesc->u.ht.mcs = txrate->idx;
354 * MIMO PS should be set to 1 for STA's using dynamic SM PS
355 * when using more then one tx stream (>MCS7).
357 if (sta && txdesc->u.ht.mcs > 7 &&
358 sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
359 __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
360 } else {
361 txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
362 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
363 txdesc->u.ht.mcs |= 0x08;
366 if (test_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags)) {
367 if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
368 txdesc->u.ht.txop = TXOP_SIFS;
369 else
370 txdesc->u.ht.txop = TXOP_BACKOFF;
372 /* Left zero on all other settings. */
373 return;
376 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
379 * Only one STBC stream is supported for now.
381 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
382 txdesc->u.ht.stbc = 1;
385 * This frame is eligible for an AMPDU, however, don't aggregate
386 * frames that are intended to probe a specific tx rate.
388 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
389 !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
390 __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
393 * Set 40Mhz mode if necessary (for legacy rates this will
394 * duplicate the frame to both channels).
396 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
397 txrate->flags & IEEE80211_TX_RC_DUP_DATA)
398 __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
399 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
400 __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
403 * Determine IFS values
404 * - Use TXOP_BACKOFF for management frames except beacons
405 * - Use TXOP_SIFS for fragment bursts
406 * - Use TXOP_HTTXOP for everything else
408 * Note: rt2800 devices won't use CTS protection (if used)
409 * for frames not transmitted with TXOP_HTTXOP
411 if (ieee80211_is_mgmt(hdr->frame_control) &&
412 !ieee80211_is_beacon(hdr->frame_control))
413 txdesc->u.ht.txop = TXOP_BACKOFF;
414 else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
415 txdesc->u.ht.txop = TXOP_SIFS;
416 else
417 txdesc->u.ht.txop = TXOP_HTTXOP;
420 static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
421 struct sk_buff *skb,
422 struct txentry_desc *txdesc,
423 struct ieee80211_sta *sta)
425 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
426 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
427 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
428 struct ieee80211_rate *rate;
429 const struct rt2x00_rate *hwrate = NULL;
431 memset(txdesc, 0, sizeof(*txdesc));
434 * Header and frame information.
436 txdesc->length = skb->len;
437 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
440 * Check whether this frame is to be acked.
442 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
443 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
446 * Check if this is a RTS/CTS frame
448 if (ieee80211_is_rts(hdr->frame_control) ||
449 ieee80211_is_cts(hdr->frame_control)) {
450 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
451 if (ieee80211_is_rts(hdr->frame_control))
452 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
453 else
454 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
455 if (tx_info->control.rts_cts_rate_idx >= 0)
456 rate =
457 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
461 * Determine retry information.
463 txdesc->retry_limit = tx_info->control.rates[0].count - 1;
464 if (txdesc->retry_limit >= rt2x00dev->long_retry)
465 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
468 * Check if more fragments are pending
470 if (ieee80211_has_morefrags(hdr->frame_control)) {
471 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
472 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
476 * Check if more frames (!= fragments) are pending
478 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
479 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
482 * Beacons and probe responses require the tsf timestamp
483 * to be inserted into the frame.
485 if (ieee80211_is_beacon(hdr->frame_control) ||
486 ieee80211_is_probe_resp(hdr->frame_control))
487 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
489 if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
490 !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
491 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
494 * Determine rate modulation.
496 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
497 txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
498 else if (txrate->flags & IEEE80211_TX_RC_MCS)
499 txdesc->rate_mode = RATE_MODE_HT_MIX;
500 else {
501 rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
502 hwrate = rt2x00_get_rate(rate->hw_value);
503 if (hwrate->flags & DEV_RATE_OFDM)
504 txdesc->rate_mode = RATE_MODE_OFDM;
505 else
506 txdesc->rate_mode = RATE_MODE_CCK;
510 * Apply TX descriptor handling by components
512 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
513 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
515 if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
516 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
517 sta, hwrate);
518 else
519 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
520 hwrate);
523 static int rt2x00queue_write_tx_data(struct queue_entry *entry,
524 struct txentry_desc *txdesc)
526 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
529 * This should not happen, we already checked the entry
530 * was ours. When the hardware disagrees there has been
531 * a queue corruption!
533 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
534 rt2x00dev->ops->lib->get_entry_state(entry))) {
535 rt2x00_err(rt2x00dev,
536 "Corrupt queue %d, accessing entry which is not ours\n"
537 "Please file bug report to %s\n",
538 entry->queue->qid, DRV_PROJECT);
539 return -EINVAL;
543 * Add the requested extra tx headroom in front of the skb.
545 skb_push(entry->skb, rt2x00dev->extra_tx_headroom);
546 memset(entry->skb->data, 0, rt2x00dev->extra_tx_headroom);
549 * Call the driver's write_tx_data function, if it exists.
551 if (rt2x00dev->ops->lib->write_tx_data)
552 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
555 * Map the skb to DMA.
557 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags) &&
558 rt2x00queue_map_txskb(entry))
559 return -ENOMEM;
561 return 0;
564 static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
565 struct txentry_desc *txdesc)
567 struct data_queue *queue = entry->queue;
569 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
572 * All processing on the frame has been completed, this means
573 * it is now ready to be dumped to userspace through debugfs.
575 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
578 static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
579 struct txentry_desc *txdesc)
582 * Check if we need to kick the queue, there are however a few rules
583 * 1) Don't kick unless this is the last in frame in a burst.
584 * When the burst flag is set, this frame is always followed
585 * by another frame which in some way are related to eachother.
586 * This is true for fragments, RTS or CTS-to-self frames.
587 * 2) Rule 1 can be broken when the available entries
588 * in the queue are less then a certain threshold.
590 if (rt2x00queue_threshold(queue) ||
591 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
592 queue->rt2x00dev->ops->lib->kick_queue(queue);
595 static void rt2x00queue_bar_check(struct queue_entry *entry)
597 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
598 struct ieee80211_bar *bar = (void *) (entry->skb->data +
599 rt2x00dev->extra_tx_headroom);
600 struct rt2x00_bar_list_entry *bar_entry;
602 if (likely(!ieee80211_is_back_req(bar->frame_control)))
603 return;
605 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
608 * If the alloc fails we still send the BAR out but just don't track
609 * it in our bar list. And as a result we will report it to mac80211
610 * back as failed.
612 if (!bar_entry)
613 return;
615 bar_entry->entry = entry;
616 bar_entry->block_acked = 0;
619 * Copy the relevant parts of the 802.11 BAR into out check list
620 * such that we can use RCU for less-overhead in the RX path since
621 * sending BARs and processing the according BlockAck should be
622 * the exception.
624 memcpy(bar_entry->ra, bar->ra, sizeof(bar->ra));
625 memcpy(bar_entry->ta, bar->ta, sizeof(bar->ta));
626 bar_entry->control = bar->control;
627 bar_entry->start_seq_num = bar->start_seq_num;
630 * Insert BAR into our BAR check list.
632 spin_lock_bh(&rt2x00dev->bar_list_lock);
633 list_add_tail_rcu(&bar_entry->list, &rt2x00dev->bar_list);
634 spin_unlock_bh(&rt2x00dev->bar_list_lock);
637 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
638 bool local)
640 struct ieee80211_tx_info *tx_info;
641 struct queue_entry *entry;
642 struct txentry_desc txdesc;
643 struct skb_frame_desc *skbdesc;
644 u8 rate_idx, rate_flags;
645 int ret = 0;
648 * Copy all TX descriptor information into txdesc,
649 * after that we are free to use the skb->cb array
650 * for our information.
652 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, NULL);
655 * All information is retrieved from the skb->cb array,
656 * now we should claim ownership of the driver part of that
657 * array, preserving the bitrate index and flags.
659 tx_info = IEEE80211_SKB_CB(skb);
660 rate_idx = tx_info->control.rates[0].idx;
661 rate_flags = tx_info->control.rates[0].flags;
662 skbdesc = get_skb_frame_desc(skb);
663 memset(skbdesc, 0, sizeof(*skbdesc));
664 skbdesc->tx_rate_idx = rate_idx;
665 skbdesc->tx_rate_flags = rate_flags;
667 if (local)
668 skbdesc->flags |= SKBDESC_NOT_MAC80211;
671 * When hardware encryption is supported, and this frame
672 * is to be encrypted, we should strip the IV/EIV data from
673 * the frame so we can provide it to the driver separately.
675 if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
676 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
677 if (test_bit(REQUIRE_COPY_IV, &queue->rt2x00dev->cap_flags))
678 rt2x00crypto_tx_copy_iv(skb, &txdesc);
679 else
680 rt2x00crypto_tx_remove_iv(skb, &txdesc);
684 * When DMA allocation is required we should guarantee to the
685 * driver that the DMA is aligned to a 4-byte boundary.
686 * However some drivers require L2 padding to pad the payload
687 * rather then the header. This could be a requirement for
688 * PCI and USB devices, while header alignment only is valid
689 * for PCI devices.
691 if (test_bit(REQUIRE_L2PAD, &queue->rt2x00dev->cap_flags))
692 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
693 else if (test_bit(REQUIRE_DMA, &queue->rt2x00dev->cap_flags))
694 rt2x00queue_align_frame(skb);
697 * That function must be called with bh disabled.
699 spin_lock(&queue->tx_lock);
701 if (unlikely(rt2x00queue_full(queue))) {
702 rt2x00_err(queue->rt2x00dev, "Dropping frame due to full tx queue %d\n",
703 queue->qid);
704 ret = -ENOBUFS;
705 goto out;
708 entry = rt2x00queue_get_entry(queue, Q_INDEX);
710 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
711 &entry->flags))) {
712 rt2x00_err(queue->rt2x00dev,
713 "Arrived at non-free entry in the non-full queue %d\n"
714 "Please file bug report to %s\n",
715 queue->qid, DRV_PROJECT);
716 ret = -EINVAL;
717 goto out;
720 skbdesc->entry = entry;
721 entry->skb = skb;
724 * It could be possible that the queue was corrupted and this
725 * call failed. Since we always return NETDEV_TX_OK to mac80211,
726 * this frame will simply be dropped.
728 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
729 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
730 entry->skb = NULL;
731 ret = -EIO;
732 goto out;
736 * Put BlockAckReqs into our check list for driver BA processing.
738 rt2x00queue_bar_check(entry);
740 set_bit(ENTRY_DATA_PENDING, &entry->flags);
742 rt2x00queue_index_inc(entry, Q_INDEX);
743 rt2x00queue_write_tx_descriptor(entry, &txdesc);
744 rt2x00queue_kick_tx_queue(queue, &txdesc);
746 out:
747 spin_unlock(&queue->tx_lock);
748 return ret;
751 int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
752 struct ieee80211_vif *vif)
754 struct rt2x00_intf *intf = vif_to_intf(vif);
756 if (unlikely(!intf->beacon))
757 return -ENOBUFS;
759 mutex_lock(&intf->beacon_skb_mutex);
762 * Clean up the beacon skb.
764 rt2x00queue_free_skb(intf->beacon);
767 * Clear beacon (single bssid devices don't need to clear the beacon
768 * since the beacon queue will get stopped anyway).
770 if (rt2x00dev->ops->lib->clear_beacon)
771 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
773 mutex_unlock(&intf->beacon_skb_mutex);
775 return 0;
778 int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
779 struct ieee80211_vif *vif)
781 struct rt2x00_intf *intf = vif_to_intf(vif);
782 struct skb_frame_desc *skbdesc;
783 struct txentry_desc txdesc;
785 if (unlikely(!intf->beacon))
786 return -ENOBUFS;
789 * Clean up the beacon skb.
791 rt2x00queue_free_skb(intf->beacon);
793 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
794 if (!intf->beacon->skb)
795 return -ENOMEM;
798 * Copy all TX descriptor information into txdesc,
799 * after that we are free to use the skb->cb array
800 * for our information.
802 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
805 * Fill in skb descriptor
807 skbdesc = get_skb_frame_desc(intf->beacon->skb);
808 memset(skbdesc, 0, sizeof(*skbdesc));
809 skbdesc->entry = intf->beacon;
812 * Send beacon to hardware.
814 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
816 return 0;
820 int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
821 struct ieee80211_vif *vif)
823 struct rt2x00_intf *intf = vif_to_intf(vif);
824 int ret;
826 mutex_lock(&intf->beacon_skb_mutex);
827 ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
828 mutex_unlock(&intf->beacon_skb_mutex);
830 return ret;
833 bool rt2x00queue_for_each_entry(struct data_queue *queue,
834 enum queue_index start,
835 enum queue_index end,
836 void *data,
837 bool (*fn)(struct queue_entry *entry,
838 void *data))
840 unsigned long irqflags;
841 unsigned int index_start;
842 unsigned int index_end;
843 unsigned int i;
845 if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
846 rt2x00_err(queue->rt2x00dev,
847 "Entry requested from invalid index range (%d - %d)\n",
848 start, end);
849 return true;
853 * Only protect the range we are going to loop over,
854 * if during our loop a extra entry is set to pending
855 * it should not be kicked during this run, since it
856 * is part of another TX operation.
858 spin_lock_irqsave(&queue->index_lock, irqflags);
859 index_start = queue->index[start];
860 index_end = queue->index[end];
861 spin_unlock_irqrestore(&queue->index_lock, irqflags);
864 * Start from the TX done pointer, this guarantees that we will
865 * send out all frames in the correct order.
867 if (index_start < index_end) {
868 for (i = index_start; i < index_end; i++) {
869 if (fn(&queue->entries[i], data))
870 return true;
872 } else {
873 for (i = index_start; i < queue->limit; i++) {
874 if (fn(&queue->entries[i], data))
875 return true;
878 for (i = 0; i < index_end; i++) {
879 if (fn(&queue->entries[i], data))
880 return true;
884 return false;
886 EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
888 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
889 enum queue_index index)
891 struct queue_entry *entry;
892 unsigned long irqflags;
894 if (unlikely(index >= Q_INDEX_MAX)) {
895 rt2x00_err(queue->rt2x00dev, "Entry requested from invalid index type (%d)\n",
896 index);
897 return NULL;
900 spin_lock_irqsave(&queue->index_lock, irqflags);
902 entry = &queue->entries[queue->index[index]];
904 spin_unlock_irqrestore(&queue->index_lock, irqflags);
906 return entry;
908 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
910 void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
912 struct data_queue *queue = entry->queue;
913 unsigned long irqflags;
915 if (unlikely(index >= Q_INDEX_MAX)) {
916 rt2x00_err(queue->rt2x00dev,
917 "Index change on invalid index type (%d)\n", index);
918 return;
921 spin_lock_irqsave(&queue->index_lock, irqflags);
923 queue->index[index]++;
924 if (queue->index[index] >= queue->limit)
925 queue->index[index] = 0;
927 entry->last_action = jiffies;
929 if (index == Q_INDEX) {
930 queue->length++;
931 } else if (index == Q_INDEX_DONE) {
932 queue->length--;
933 queue->count++;
936 spin_unlock_irqrestore(&queue->index_lock, irqflags);
939 void rt2x00queue_pause_queue(struct data_queue *queue)
941 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
942 !test_bit(QUEUE_STARTED, &queue->flags) ||
943 test_and_set_bit(QUEUE_PAUSED, &queue->flags))
944 return;
946 switch (queue->qid) {
947 case QID_AC_VO:
948 case QID_AC_VI:
949 case QID_AC_BE:
950 case QID_AC_BK:
952 * For TX queues, we have to disable the queue
953 * inside mac80211.
955 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
956 break;
957 default:
958 break;
961 EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
963 void rt2x00queue_unpause_queue(struct data_queue *queue)
965 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
966 !test_bit(QUEUE_STARTED, &queue->flags) ||
967 !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
968 return;
970 switch (queue->qid) {
971 case QID_AC_VO:
972 case QID_AC_VI:
973 case QID_AC_BE:
974 case QID_AC_BK:
976 * For TX queues, we have to enable the queue
977 * inside mac80211.
979 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
980 break;
981 case QID_RX:
983 * For RX we need to kick the queue now in order to
984 * receive frames.
986 queue->rt2x00dev->ops->lib->kick_queue(queue);
987 default:
988 break;
991 EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
993 void rt2x00queue_start_queue(struct data_queue *queue)
995 mutex_lock(&queue->status_lock);
997 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
998 test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
999 mutex_unlock(&queue->status_lock);
1000 return;
1003 set_bit(QUEUE_PAUSED, &queue->flags);
1005 queue->rt2x00dev->ops->lib->start_queue(queue);
1007 rt2x00queue_unpause_queue(queue);
1009 mutex_unlock(&queue->status_lock);
1011 EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
1013 void rt2x00queue_stop_queue(struct data_queue *queue)
1015 mutex_lock(&queue->status_lock);
1017 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
1018 mutex_unlock(&queue->status_lock);
1019 return;
1022 rt2x00queue_pause_queue(queue);
1024 queue->rt2x00dev->ops->lib->stop_queue(queue);
1026 mutex_unlock(&queue->status_lock);
1028 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
1030 void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
1032 bool started;
1033 bool tx_queue =
1034 (queue->qid == QID_AC_VO) ||
1035 (queue->qid == QID_AC_VI) ||
1036 (queue->qid == QID_AC_BE) ||
1037 (queue->qid == QID_AC_BK);
1039 mutex_lock(&queue->status_lock);
1042 * If the queue has been started, we must stop it temporarily
1043 * to prevent any new frames to be queued on the device. If
1044 * we are not dropping the pending frames, the queue must
1045 * only be stopped in the software and not the hardware,
1046 * otherwise the queue will never become empty on its own.
1048 started = test_bit(QUEUE_STARTED, &queue->flags);
1049 if (started) {
1051 * Pause the queue
1053 rt2x00queue_pause_queue(queue);
1056 * If we are not supposed to drop any pending
1057 * frames, this means we must force a start (=kick)
1058 * to the queue to make sure the hardware will
1059 * start transmitting.
1061 if (!drop && tx_queue)
1062 queue->rt2x00dev->ops->lib->kick_queue(queue);
1066 * Check if driver supports flushing, if that is the case we can
1067 * defer the flushing to the driver. Otherwise we must use the
1068 * alternative which just waits for the queue to become empty.
1070 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1071 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
1074 * The queue flush has failed...
1076 if (unlikely(!rt2x00queue_empty(queue)))
1077 rt2x00_warn(queue->rt2x00dev, "Queue %d failed to flush\n",
1078 queue->qid);
1081 * Restore the queue to the previous status
1083 if (started)
1084 rt2x00queue_unpause_queue(queue);
1086 mutex_unlock(&queue->status_lock);
1088 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1090 void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1092 struct data_queue *queue;
1095 * rt2x00queue_start_queue will call ieee80211_wake_queue
1096 * for each queue after is has been properly initialized.
1098 tx_queue_for_each(rt2x00dev, queue)
1099 rt2x00queue_start_queue(queue);
1101 rt2x00queue_start_queue(rt2x00dev->rx);
1103 EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1105 void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1107 struct data_queue *queue;
1110 * rt2x00queue_stop_queue will call ieee80211_stop_queue
1111 * as well, but we are completely shutting doing everything
1112 * now, so it is much safer to stop all TX queues at once,
1113 * and use rt2x00queue_stop_queue for cleaning up.
1115 ieee80211_stop_queues(rt2x00dev->hw);
1117 tx_queue_for_each(rt2x00dev, queue)
1118 rt2x00queue_stop_queue(queue);
1120 rt2x00queue_stop_queue(rt2x00dev->rx);
1122 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1124 void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1126 struct data_queue *queue;
1128 tx_queue_for_each(rt2x00dev, queue)
1129 rt2x00queue_flush_queue(queue, drop);
1131 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1133 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1135 static void rt2x00queue_reset(struct data_queue *queue)
1137 unsigned long irqflags;
1138 unsigned int i;
1140 spin_lock_irqsave(&queue->index_lock, irqflags);
1142 queue->count = 0;
1143 queue->length = 0;
1145 for (i = 0; i < Q_INDEX_MAX; i++)
1146 queue->index[i] = 0;
1148 spin_unlock_irqrestore(&queue->index_lock, irqflags);
1151 void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
1153 struct data_queue *queue;
1154 unsigned int i;
1156 queue_for_each(rt2x00dev, queue) {
1157 rt2x00queue_reset(queue);
1159 for (i = 0; i < queue->limit; i++)
1160 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
1164 static int rt2x00queue_alloc_entries(struct data_queue *queue)
1166 struct queue_entry *entries;
1167 unsigned int entry_size;
1168 unsigned int i;
1170 rt2x00queue_reset(queue);
1173 * Allocate all queue entries.
1175 entry_size = sizeof(*entries) + queue->priv_size;
1176 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
1177 if (!entries)
1178 return -ENOMEM;
1180 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
1181 (((char *)(__base)) + ((__limit) * (__esize)) + \
1182 ((__index) * (__psize)))
1184 for (i = 0; i < queue->limit; i++) {
1185 entries[i].flags = 0;
1186 entries[i].queue = queue;
1187 entries[i].skb = NULL;
1188 entries[i].entry_idx = i;
1189 entries[i].priv_data =
1190 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
1191 sizeof(*entries), queue->priv_size);
1194 #undef QUEUE_ENTRY_PRIV_OFFSET
1196 queue->entries = entries;
1198 return 0;
1201 static void rt2x00queue_free_skbs(struct data_queue *queue)
1203 unsigned int i;
1205 if (!queue->entries)
1206 return;
1208 for (i = 0; i < queue->limit; i++) {
1209 rt2x00queue_free_skb(&queue->entries[i]);
1213 static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
1215 unsigned int i;
1216 struct sk_buff *skb;
1218 for (i = 0; i < queue->limit; i++) {
1219 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
1220 if (!skb)
1221 return -ENOMEM;
1222 queue->entries[i].skb = skb;
1225 return 0;
1228 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1230 struct data_queue *queue;
1231 int status;
1233 status = rt2x00queue_alloc_entries(rt2x00dev->rx);
1234 if (status)
1235 goto exit;
1237 tx_queue_for_each(rt2x00dev, queue) {
1238 status = rt2x00queue_alloc_entries(queue);
1239 if (status)
1240 goto exit;
1243 status = rt2x00queue_alloc_entries(rt2x00dev->bcn);
1244 if (status)
1245 goto exit;
1247 if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
1248 status = rt2x00queue_alloc_entries(rt2x00dev->atim);
1249 if (status)
1250 goto exit;
1253 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
1254 if (status)
1255 goto exit;
1257 return 0;
1259 exit:
1260 rt2x00_err(rt2x00dev, "Queue entries allocation failed\n");
1262 rt2x00queue_uninitialize(rt2x00dev);
1264 return status;
1267 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1269 struct data_queue *queue;
1271 rt2x00queue_free_skbs(rt2x00dev->rx);
1273 queue_for_each(rt2x00dev, queue) {
1274 kfree(queue->entries);
1275 queue->entries = NULL;
1279 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1280 struct data_queue *queue, enum data_queue_qid qid)
1282 mutex_init(&queue->status_lock);
1283 spin_lock_init(&queue->tx_lock);
1284 spin_lock_init(&queue->index_lock);
1286 queue->rt2x00dev = rt2x00dev;
1287 queue->qid = qid;
1288 queue->txop = 0;
1289 queue->aifs = 2;
1290 queue->cw_min = 5;
1291 queue->cw_max = 10;
1293 rt2x00dev->ops->queue_init(queue);
1295 queue->threshold = DIV_ROUND_UP(queue->limit, 10);
1298 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1300 struct data_queue *queue;
1301 enum data_queue_qid qid;
1302 unsigned int req_atim =
1303 !!test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
1306 * We need the following queues:
1307 * RX: 1
1308 * TX: ops->tx_queues
1309 * Beacon: 1
1310 * Atim: 1 (if required)
1312 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
1314 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
1315 if (!queue) {
1316 rt2x00_err(rt2x00dev, "Queue allocation failed\n");
1317 return -ENOMEM;
1321 * Initialize pointers
1323 rt2x00dev->rx = queue;
1324 rt2x00dev->tx = &queue[1];
1325 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
1326 rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
1329 * Initialize queue parameters.
1330 * RX: qid = QID_RX
1331 * TX: qid = QID_AC_VO + index
1332 * TX: cw_min: 2^5 = 32.
1333 * TX: cw_max: 2^10 = 1024.
1334 * BCN: qid = QID_BEACON
1335 * ATIM: qid = QID_ATIM
1337 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1339 qid = QID_AC_VO;
1340 tx_queue_for_each(rt2x00dev, queue)
1341 rt2x00queue_init(rt2x00dev, queue, qid++);
1343 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
1344 if (req_atim)
1345 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
1347 return 0;
1350 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1352 kfree(rt2x00dev->rx);
1353 rt2x00dev->rx = NULL;
1354 rt2x00dev->tx = NULL;
1355 rt2x00dev->bcn = NULL;