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.
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>
34 #include "rt2x00lib.h"
36 struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry, gfp_t gfp)
38 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
40 struct skb_frame_desc *skbdesc;
41 unsigned int frame_size;
42 unsigned int head_size = 0;
43 unsigned int tail_size = 0;
46 * The frame size includes descriptor size, because the
47 * hardware directly receive the frame into the skbuffer.
49 frame_size = entry->queue->data_size + entry->queue->desc_size;
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.
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.
63 if (test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags)) {
71 skb = __dev_alloc_skb(frame_size + head_size + tail_size, gfp);
76 * Make sure we not have a frame with the requested bytes
77 * available in the head and tail.
79 skb_reserve(skb, head_size);
80 skb_put(skb, frame_size);
85 skbdesc = get_skb_frame_desc(skb);
86 memset(skbdesc, 0, sizeof(*skbdesc));
87 skbdesc->entry = entry;
89 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags)) {
92 skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
94 if (unlikely(dma_mapping_error(rt2x00dev->dev, skb_dma))) {
95 dev_kfree_skb_any(skb);
99 skbdesc->skb_dma = skb_dma;
100 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
106 int rt2x00queue_map_txskb(struct queue_entry *entry)
108 struct device *dev = entry->queue->rt2x00dev->dev;
109 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
112 dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
114 if (unlikely(dma_mapping_error(dev, skbdesc->skb_dma)))
117 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
120 EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
122 void rt2x00queue_unmap_skb(struct queue_entry *entry)
124 struct device *dev = entry->queue->rt2x00dev->dev;
125 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
127 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
128 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
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,
134 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
137 EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
139 void rt2x00queue_free_skb(struct queue_entry *entry)
144 rt2x00queue_unmap_skb(entry);
145 dev_kfree_skb_any(entry->skb);
149 void rt2x00queue_align_frame(struct sk_buff *skb)
151 unsigned int frame_length = skb->len;
152 unsigned int align = ALIGN_SIZE(skb, 0);
157 skb_push(skb, align);
158 memmove(skb->data, skb->data + align, frame_length);
159 skb_trim(skb, frame_length);
162 void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
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;
170 * Adjust the header alignment if the payload needs to be moved more
173 if (payload_align > header_align)
176 /* There is nothing to do if no alignment is needed */
180 /* Reserve the amount of space needed in front of the frame */
181 skb_push(skb, header_align);
186 memmove(skb->data, skb->data + header_align, header_length);
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,
194 /* Trim the skb to the correct size */
195 skb_trim(skb, header_length + l2pad + payload_length);
198 void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
201 * L2 padding is only present if the skb contains more than just the
202 * IEEE 802.11 header.
204 unsigned int l2pad = (skb->len > header_length) ?
205 L2PAD_SIZE(header_length) : 0;
210 memmove(skb->data + l2pad, skb->data, header_length);
211 skb_pull(skb, l2pad);
214 static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
216 struct txentry_desc *txdesc)
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);
223 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
226 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
228 if (!test_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags)) {
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
235 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
236 __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
238 /* H/W will generate sequence number */
243 * The hardware is not able to insert a sequence number. Assign a
244 * software generated one here.
246 * This is wrong because beacons are not getting sequence
247 * numbers assigned properly.
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.
253 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
254 seqno = atomic_add_return(0x10, &intf->seqno);
256 seqno = atomic_read(&intf->seqno);
258 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
259 hdr->seq_ctrl |= cpu_to_le16(seqno);
262 static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
264 struct txentry_desc *txdesc,
265 const struct rt2x00_rate *hwrate)
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;
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.
278 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
279 txdesc->u.plcp.ifs = IFS_BACKOFF;
281 txdesc->u.plcp.ifs = IFS_SIFS;
283 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
284 data_length = skb->len + 4;
285 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
289 * Length calculation depends on OFDM/CCK rate.
291 txdesc->u.plcp.signal = hwrate->plcp;
292 txdesc->u.plcp.service = 0x04;
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;
299 * Convert length to microseconds.
301 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
302 duration = GET_DURATION(data_length, hwrate->bitrate);
308 * Check if we need to set the Length Extension
310 if (hwrate->bitrate == 110 && residual <= 30)
311 txdesc->u.plcp.service |= 0x80;
314 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
315 txdesc->u.plcp.length_low = duration & 0xff;
318 * When preamble is enabled we should set the
319 * preamble bit for the signal.
321 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
322 txdesc->u.plcp.signal |= 0x08;
326 static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
328 struct txentry_desc *txdesc,
329 struct ieee80211_sta *sta,
330 const struct rt2x00_rate *hwrate)
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;
338 txdesc->u.ht.mpdu_density =
339 sta->ht_cap.ampdu_density;
341 sta_priv = sta_to_rt2x00_sta(sta);
342 txdesc->u.ht.wcid = sta_priv->wcid;
346 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
347 * mcs rate to be used
349 if (txrate->flags & IEEE80211_TX_RC_MCS) {
350 txdesc->u.ht.mcs = txrate->idx;
353 * MIMO PS should be set to 1 for STA's using dynamic SM PS
354 * when using more then one tx stream (>MCS7).
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);
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;
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;
369 txdesc->u.ht.txop = TXOP_BACKOFF;
371 /* Left zero on all other settings. */
375 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
378 * Only one STBC stream is supported for now.
380 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
381 txdesc->u.ht.stbc = 1;
384 * This frame is eligible for an AMPDU, however, don't aggregate
385 * frames that are intended to probe a specific tx rate.
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);
392 * Set 40Mhz mode if necessary (for legacy rates this will
393 * duplicate the frame to both channels).
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);
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
407 * Note: rt2800 devices won't use CTS protection (if used)
408 * for frames not transmitted with TXOP_HTTXOP
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;
416 txdesc->u.ht.txop = TXOP_HTTXOP;
419 static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
421 struct txentry_desc *txdesc,
422 struct ieee80211_sta *sta)
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;
430 memset(txdesc, 0, sizeof(*txdesc));
433 * Header and frame information.
435 txdesc->length = skb->len;
436 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
439 * Check whether this frame is to be acked.
441 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
442 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
445 * Check if this is a RTS/CTS frame
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);
453 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
454 if (tx_info->control.rts_cts_rate_idx >= 0)
456 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
460 * Determine retry information.
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);
467 * Check if more fragments are pending
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);
475 * Check if more frames (!= fragments) are pending
477 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
478 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
481 * Beacons and probe responses require the tsf timestamp
482 * to be inserted into the frame.
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);
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);
493 * Determine rate modulation.
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;
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;
505 txdesc->rate_mode = RATE_MODE_CCK;
509 * Apply TX descriptor handling by components
511 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
512 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
514 if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
515 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
518 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
522 static int rt2x00queue_write_tx_data(struct queue_entry *entry,
523 struct txentry_desc *txdesc)
525 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
528 * This should not happen, we already checked the entry
529 * was ours. When the hardware disagrees there has been
530 * a queue corruption!
532 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
533 rt2x00dev->ops->lib->get_entry_state(entry))) {
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);
542 * Add the requested extra tx headroom in front of the skb.
544 skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
545 memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
548 * Call the driver's write_tx_data function, if it exists.
550 if (rt2x00dev->ops->lib->write_tx_data)
551 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
554 * Map the skb to DMA.
556 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags) &&
557 rt2x00queue_map_txskb(entry))
563 static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
564 struct txentry_desc *txdesc)
566 struct data_queue *queue = entry->queue;
568 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
571 * All processing on the frame has been completed, this means
572 * it is now ready to be dumped to userspace through debugfs.
574 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
577 static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
578 struct txentry_desc *txdesc)
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.
589 if (rt2x00queue_threshold(queue) ||
590 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
591 queue->rt2x00dev->ops->lib->kick_queue(queue);
594 static void rt2x00queue_bar_check(struct queue_entry *entry)
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;
601 if (likely(!ieee80211_is_back_req(bar->frame_control)))
604 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
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
614 bar_entry->entry = entry;
615 bar_entry->block_acked = 0;
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
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;
629 * Insert BAR into our BAR check list.
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);
636 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
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;
647 * Copy all TX descriptor information into txdesc,
648 * after that we are free to use the skb->cb array
649 * for our information.
651 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, NULL);
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.
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;
667 skbdesc->flags |= SKBDESC_NOT_MAC80211;
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.
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);
679 rt2x00crypto_tx_remove_iv(skb, &txdesc);
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
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);
696 * That function must be called with bh disabled.
698 spin_lock(&queue->tx_lock);
700 if (unlikely(rt2x00queue_full(queue))) {
701 ERROR(queue->rt2x00dev,
702 "Dropping frame due to full tx queue %d.\n", queue->qid);
707 entry = rt2x00queue_get_entry(queue, Q_INDEX);
709 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
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);
719 skbdesc->entry = entry;
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.
727 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
728 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
735 * Put BlockAckReqs into our check list for driver BA processing.
737 rt2x00queue_bar_check(entry);
739 set_bit(ENTRY_DATA_PENDING, &entry->flags);
741 rt2x00queue_index_inc(entry, Q_INDEX);
742 rt2x00queue_write_tx_descriptor(entry, &txdesc);
743 rt2x00queue_kick_tx_queue(queue, &txdesc);
746 spin_unlock(&queue->tx_lock);
750 int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
751 struct ieee80211_vif *vif)
753 struct rt2x00_intf *intf = vif_to_intf(vif);
755 if (unlikely(!intf->beacon))
758 mutex_lock(&intf->beacon_skb_mutex);
761 * Clean up the beacon skb.
763 rt2x00queue_free_skb(intf->beacon);
766 * Clear beacon (single bssid devices don't need to clear the beacon
767 * since the beacon queue will get stopped anyway).
769 if (rt2x00dev->ops->lib->clear_beacon)
770 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
772 mutex_unlock(&intf->beacon_skb_mutex);
777 int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
778 struct ieee80211_vif *vif)
780 struct rt2x00_intf *intf = vif_to_intf(vif);
781 struct skb_frame_desc *skbdesc;
782 struct txentry_desc txdesc;
784 if (unlikely(!intf->beacon))
788 * Clean up the beacon skb.
790 rt2x00queue_free_skb(intf->beacon);
792 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
793 if (!intf->beacon->skb)
797 * Copy all TX descriptor information into txdesc,
798 * after that we are free to use the skb->cb array
799 * for our information.
801 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
804 * Fill in skb descriptor
806 skbdesc = get_skb_frame_desc(intf->beacon->skb);
807 memset(skbdesc, 0, sizeof(*skbdesc));
808 skbdesc->entry = intf->beacon;
811 * Send beacon to hardware.
813 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
819 int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
820 struct ieee80211_vif *vif)
822 struct rt2x00_intf *intf = vif_to_intf(vif);
825 mutex_lock(&intf->beacon_skb_mutex);
826 ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
827 mutex_unlock(&intf->beacon_skb_mutex);
832 bool rt2x00queue_for_each_entry(struct data_queue *queue,
833 enum queue_index start,
834 enum queue_index end,
836 bool (*fn)(struct queue_entry *entry,
839 unsigned long irqflags;
840 unsigned int index_start;
841 unsigned int index_end;
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",
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.
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);
863 * Start from the TX done pointer, this guarantees that we will
864 * send out all frames in the correct order.
866 if (index_start < index_end) {
867 for (i = index_start; i < index_end; i++) {
868 if (fn(&queue->entries[i], data))
872 for (i = index_start; i < queue->limit; i++) {
873 if (fn(&queue->entries[i], data))
877 for (i = 0; i < index_end; i++) {
878 if (fn(&queue->entries[i], data))
885 EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
887 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
888 enum queue_index index)
890 struct queue_entry *entry;
891 unsigned long irqflags;
893 if (unlikely(index >= Q_INDEX_MAX)) {
894 ERROR(queue->rt2x00dev,
895 "Entry requested from invalid index type (%d)\n", index);
899 spin_lock_irqsave(&queue->index_lock, irqflags);
901 entry = &queue->entries[queue->index[index]];
903 spin_unlock_irqrestore(&queue->index_lock, irqflags);
907 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
909 void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
911 struct data_queue *queue = entry->queue;
912 unsigned long irqflags;
914 if (unlikely(index >= Q_INDEX_MAX)) {
915 ERROR(queue->rt2x00dev,
916 "Index change on invalid index type (%d)\n", index);
920 spin_lock_irqsave(&queue->index_lock, irqflags);
922 queue->index[index]++;
923 if (queue->index[index] >= queue->limit)
924 queue->index[index] = 0;
926 entry->last_action = jiffies;
928 if (index == Q_INDEX) {
930 } else if (index == Q_INDEX_DONE) {
935 spin_unlock_irqrestore(&queue->index_lock, irqflags);
938 void rt2x00queue_pause_queue(struct data_queue *queue)
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))
945 switch (queue->qid) {
951 * For TX queues, we have to disable the queue
954 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
960 EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
962 void rt2x00queue_unpause_queue(struct data_queue *queue)
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))
969 switch (queue->qid) {
975 * For TX queues, we have to enable the queue
978 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
982 * For RX we need to kick the queue now in order to
985 queue->rt2x00dev->ops->lib->kick_queue(queue);
990 EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
992 void rt2x00queue_start_queue(struct data_queue *queue)
994 mutex_lock(&queue->status_lock);
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);
1002 set_bit(QUEUE_PAUSED, &queue->flags);
1004 queue->rt2x00dev->ops->lib->start_queue(queue);
1006 rt2x00queue_unpause_queue(queue);
1008 mutex_unlock(&queue->status_lock);
1010 EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
1012 void rt2x00queue_stop_queue(struct data_queue *queue)
1014 mutex_lock(&queue->status_lock);
1016 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
1017 mutex_unlock(&queue->status_lock);
1021 rt2x00queue_pause_queue(queue);
1023 queue->rt2x00dev->ops->lib->stop_queue(queue);
1025 mutex_unlock(&queue->status_lock);
1027 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
1029 void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
1033 (queue->qid == QID_AC_VO) ||
1034 (queue->qid == QID_AC_VI) ||
1035 (queue->qid == QID_AC_BE) ||
1036 (queue->qid == QID_AC_BK);
1038 mutex_lock(&queue->status_lock);
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.
1047 started = test_bit(QUEUE_STARTED, &queue->flags);
1052 rt2x00queue_pause_queue(queue);
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.
1060 if (!drop && tx_queue)
1061 queue->rt2x00dev->ops->lib->kick_queue(queue);
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.
1069 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1070 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
1073 * The queue flush has failed...
1075 if (unlikely(!rt2x00queue_empty(queue)))
1076 WARNING(queue->rt2x00dev, "Queue %d failed to flush\n", queue->qid);
1079 * Restore the queue to the previous status
1082 rt2x00queue_unpause_queue(queue);
1084 mutex_unlock(&queue->status_lock);
1086 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1088 void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1090 struct data_queue *queue;
1093 * rt2x00queue_start_queue will call ieee80211_wake_queue
1094 * for each queue after is has been properly initialized.
1096 tx_queue_for_each(rt2x00dev, queue)
1097 rt2x00queue_start_queue(queue);
1099 rt2x00queue_start_queue(rt2x00dev->rx);
1101 EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1103 void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1105 struct data_queue *queue;
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.
1113 ieee80211_stop_queues(rt2x00dev->hw);
1115 tx_queue_for_each(rt2x00dev, queue)
1116 rt2x00queue_stop_queue(queue);
1118 rt2x00queue_stop_queue(rt2x00dev->rx);
1120 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1122 void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1124 struct data_queue *queue;
1126 tx_queue_for_each(rt2x00dev, queue)
1127 rt2x00queue_flush_queue(queue, drop);
1129 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1131 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1133 static void rt2x00queue_reset(struct data_queue *queue)
1135 unsigned long irqflags;
1138 spin_lock_irqsave(&queue->index_lock, irqflags);
1143 for (i = 0; i < Q_INDEX_MAX; i++)
1144 queue->index[i] = 0;
1146 spin_unlock_irqrestore(&queue->index_lock, irqflags);
1149 void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
1151 struct data_queue *queue;
1154 queue_for_each(rt2x00dev, queue) {
1155 rt2x00queue_reset(queue);
1157 for (i = 0; i < queue->limit; i++)
1158 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
1162 static int rt2x00queue_alloc_entries(struct data_queue *queue,
1163 const struct data_queue_desc *qdesc)
1165 struct queue_entry *entries;
1166 unsigned int entry_size;
1169 rt2x00queue_reset(queue);
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;
1177 * Allocate all queue entries.
1179 entry_size = sizeof(*entries) + qdesc->priv_size;
1180 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
1184 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
1185 (((char *)(__base)) + ((__limit) * (__esize)) + \
1186 ((__index) * (__psize)))
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);
1198 #undef QUEUE_ENTRY_PRIV_OFFSET
1200 queue->entries = entries;
1205 static void rt2x00queue_free_skbs(struct data_queue *queue)
1209 if (!queue->entries)
1212 for (i = 0; i < queue->limit; i++) {
1213 rt2x00queue_free_skb(&queue->entries[i]);
1217 static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
1220 struct sk_buff *skb;
1222 for (i = 0; i < queue->limit; i++) {
1223 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
1226 queue->entries[i].skb = skb;
1232 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1234 struct data_queue *queue;
1237 status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
1241 tx_queue_for_each(rt2x00dev, queue) {
1242 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
1247 status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
1251 if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
1252 status = rt2x00queue_alloc_entries(rt2x00dev->atim,
1253 rt2x00dev->ops->atim);
1258 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
1265 ERROR(rt2x00dev, "Queue entries allocation failed.\n");
1267 rt2x00queue_uninitialize(rt2x00dev);
1272 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1274 struct data_queue *queue;
1276 rt2x00queue_free_skbs(rt2x00dev->rx);
1278 queue_for_each(rt2x00dev, queue) {
1279 kfree(queue->entries);
1280 queue->entries = NULL;
1284 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1285 struct data_queue *queue, enum data_queue_qid qid)
1287 mutex_init(&queue->status_lock);
1288 spin_lock_init(&queue->tx_lock);
1289 spin_lock_init(&queue->index_lock);
1291 queue->rt2x00dev = rt2x00dev;
1299 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
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);
1307 * We need the following queues:
1309 * TX: ops->tx_queues
1311 * Atim: 1 (if required)
1313 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
1315 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
1317 ERROR(rt2x00dev, "Queue allocation failed.\n");
1322 * Initialize pointers
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;
1330 * Initialize queue parameters.
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
1338 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1341 tx_queue_for_each(rt2x00dev, queue)
1342 rt2x00queue_init(rt2x00dev, queue, qid++);
1344 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
1346 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
1351 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1353 kfree(rt2x00dev->rx);
1354 rt2x00dev->rx = NULL;
1355 rt2x00dev->tx = NULL;
1356 rt2x00dev->bcn = NULL;