mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / wireless / rt2x00 / rt2x00queue.c
blobe6182170e7918f4ec31ad636dde989c1b5602334
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);
164 * H/W needs L2 padding between the header and the paylod if header size
165 * is not 4 bytes aligned.
167 void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int hdr_len)
169 unsigned int l2pad = (skb->len > hdr_len) ? L2PAD_SIZE(hdr_len) : 0;
171 if (!l2pad)
172 return;
174 skb_push(skb, l2pad);
175 memmove(skb->data, skb->data + l2pad, hdr_len);
178 void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int hdr_len)
180 unsigned int l2pad = (skb->len > hdr_len) ? L2PAD_SIZE(hdr_len) : 0;
182 if (!l2pad)
183 return;
185 memmove(skb->data + l2pad, skb->data, hdr_len);
186 skb_pull(skb, l2pad);
189 static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
190 struct sk_buff *skb,
191 struct txentry_desc *txdesc)
193 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
194 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
195 struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
196 u16 seqno;
198 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
199 return;
201 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
203 if (!test_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags)) {
205 * rt2800 has a H/W (or F/W) bug, device incorrectly increase
206 * seqno on retransmited data (non-QOS) frames. To workaround
207 * the problem let's generate seqno in software if QOS is
208 * disabled.
210 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
211 __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
212 else
213 /* H/W will generate sequence number */
214 return;
218 * The hardware is not able to insert a sequence number. Assign a
219 * software generated one here.
221 * This is wrong because beacons are not getting sequence
222 * numbers assigned properly.
224 * A secondary problem exists for drivers that cannot toggle
225 * sequence counting per-frame, since those will override the
226 * sequence counter given by mac80211.
228 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
229 seqno = atomic_add_return(0x10, &intf->seqno);
230 else
231 seqno = atomic_read(&intf->seqno);
233 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
234 hdr->seq_ctrl |= cpu_to_le16(seqno);
237 static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
238 struct sk_buff *skb,
239 struct txentry_desc *txdesc,
240 const struct rt2x00_rate *hwrate)
242 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
243 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
244 unsigned int data_length;
245 unsigned int duration;
246 unsigned int residual;
249 * Determine with what IFS priority this frame should be send.
250 * Set ifs to IFS_SIFS when the this is not the first fragment,
251 * or this fragment came after RTS/CTS.
253 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
254 txdesc->u.plcp.ifs = IFS_BACKOFF;
255 else
256 txdesc->u.plcp.ifs = IFS_SIFS;
258 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
259 data_length = skb->len + 4;
260 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
263 * PLCP setup
264 * Length calculation depends on OFDM/CCK rate.
266 txdesc->u.plcp.signal = hwrate->plcp;
267 txdesc->u.plcp.service = 0x04;
269 if (hwrate->flags & DEV_RATE_OFDM) {
270 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
271 txdesc->u.plcp.length_low = data_length & 0x3f;
272 } else {
274 * Convert length to microseconds.
276 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
277 duration = GET_DURATION(data_length, hwrate->bitrate);
279 if (residual != 0) {
280 duration++;
283 * Check if we need to set the Length Extension
285 if (hwrate->bitrate == 110 && residual <= 30)
286 txdesc->u.plcp.service |= 0x80;
289 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
290 txdesc->u.plcp.length_low = duration & 0xff;
293 * When preamble is enabled we should set the
294 * preamble bit for the signal.
296 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
297 txdesc->u.plcp.signal |= 0x08;
301 static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
302 struct sk_buff *skb,
303 struct txentry_desc *txdesc,
304 struct ieee80211_sta *sta,
305 const struct rt2x00_rate *hwrate)
307 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
308 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
309 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
310 struct rt2x00_sta *sta_priv = NULL;
312 if (sta) {
313 txdesc->u.ht.mpdu_density =
314 sta->ht_cap.ampdu_density;
316 sta_priv = sta_to_rt2x00_sta(sta);
317 txdesc->u.ht.wcid = sta_priv->wcid;
321 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
322 * mcs rate to be used
324 if (txrate->flags & IEEE80211_TX_RC_MCS) {
325 txdesc->u.ht.mcs = txrate->idx;
328 * MIMO PS should be set to 1 for STA's using dynamic SM PS
329 * when using more then one tx stream (>MCS7).
331 if (sta && txdesc->u.ht.mcs > 7 &&
332 sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
333 __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
334 } else {
335 txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
336 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
337 txdesc->u.ht.mcs |= 0x08;
340 if (test_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags)) {
341 if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
342 txdesc->u.ht.txop = TXOP_SIFS;
343 else
344 txdesc->u.ht.txop = TXOP_BACKOFF;
346 /* Left zero on all other settings. */
347 return;
350 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
353 * Only one STBC stream is supported for now.
355 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
356 txdesc->u.ht.stbc = 1;
359 * This frame is eligible for an AMPDU, however, don't aggregate
360 * frames that are intended to probe a specific tx rate.
362 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
363 !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
364 __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
367 * Set 40Mhz mode if necessary (for legacy rates this will
368 * duplicate the frame to both channels).
370 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
371 txrate->flags & IEEE80211_TX_RC_DUP_DATA)
372 __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
373 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
374 __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
377 * Determine IFS values
378 * - Use TXOP_BACKOFF for management frames except beacons
379 * - Use TXOP_SIFS for fragment bursts
380 * - Use TXOP_HTTXOP for everything else
382 * Note: rt2800 devices won't use CTS protection (if used)
383 * for frames not transmitted with TXOP_HTTXOP
385 if (ieee80211_is_mgmt(hdr->frame_control) &&
386 !ieee80211_is_beacon(hdr->frame_control))
387 txdesc->u.ht.txop = TXOP_BACKOFF;
388 else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
389 txdesc->u.ht.txop = TXOP_SIFS;
390 else
391 txdesc->u.ht.txop = TXOP_HTTXOP;
394 static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
395 struct sk_buff *skb,
396 struct txentry_desc *txdesc,
397 struct ieee80211_sta *sta)
399 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
400 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
401 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
402 struct ieee80211_rate *rate;
403 const struct rt2x00_rate *hwrate = NULL;
405 memset(txdesc, 0, sizeof(*txdesc));
408 * Header and frame information.
410 txdesc->length = skb->len;
411 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
414 * Check whether this frame is to be acked.
416 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
417 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
420 * Check if this is a RTS/CTS frame
422 if (ieee80211_is_rts(hdr->frame_control) ||
423 ieee80211_is_cts(hdr->frame_control)) {
424 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
425 if (ieee80211_is_rts(hdr->frame_control))
426 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
427 else
428 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
429 if (tx_info->control.rts_cts_rate_idx >= 0)
430 rate =
431 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
435 * Determine retry information.
437 txdesc->retry_limit = tx_info->control.rates[0].count - 1;
438 if (txdesc->retry_limit >= rt2x00dev->long_retry)
439 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
442 * Check if more fragments are pending
444 if (ieee80211_has_morefrags(hdr->frame_control)) {
445 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
446 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
450 * Check if more frames (!= fragments) are pending
452 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
453 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
456 * Beacons and probe responses require the tsf timestamp
457 * to be inserted into the frame.
459 if (ieee80211_is_beacon(hdr->frame_control) ||
460 ieee80211_is_probe_resp(hdr->frame_control))
461 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
463 if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
464 !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
465 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
468 * Determine rate modulation.
470 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
471 txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
472 else if (txrate->flags & IEEE80211_TX_RC_MCS)
473 txdesc->rate_mode = RATE_MODE_HT_MIX;
474 else {
475 rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
476 hwrate = rt2x00_get_rate(rate->hw_value);
477 if (hwrate->flags & DEV_RATE_OFDM)
478 txdesc->rate_mode = RATE_MODE_OFDM;
479 else
480 txdesc->rate_mode = RATE_MODE_CCK;
484 * Apply TX descriptor handling by components
486 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
487 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
489 if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
490 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
491 sta, hwrate);
492 else
493 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
494 hwrate);
497 static int rt2x00queue_write_tx_data(struct queue_entry *entry,
498 struct txentry_desc *txdesc)
500 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
503 * This should not happen, we already checked the entry
504 * was ours. When the hardware disagrees there has been
505 * a queue corruption!
507 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
508 rt2x00dev->ops->lib->get_entry_state(entry))) {
509 rt2x00_err(rt2x00dev,
510 "Corrupt queue %d, accessing entry which is not ours\n"
511 "Please file bug report to %s\n",
512 entry->queue->qid, DRV_PROJECT);
513 return -EINVAL;
517 * Add the requested extra tx headroom in front of the skb.
519 skb_push(entry->skb, rt2x00dev->extra_tx_headroom);
520 memset(entry->skb->data, 0, rt2x00dev->extra_tx_headroom);
523 * Call the driver's write_tx_data function, if it exists.
525 if (rt2x00dev->ops->lib->write_tx_data)
526 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
529 * Map the skb to DMA.
531 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags) &&
532 rt2x00queue_map_txskb(entry))
533 return -ENOMEM;
535 return 0;
538 static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
539 struct txentry_desc *txdesc)
541 struct data_queue *queue = entry->queue;
543 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
546 * All processing on the frame has been completed, this means
547 * it is now ready to be dumped to userspace through debugfs.
549 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
552 static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
553 struct txentry_desc *txdesc)
556 * Check if we need to kick the queue, there are however a few rules
557 * 1) Don't kick unless this is the last in frame in a burst.
558 * When the burst flag is set, this frame is always followed
559 * by another frame which in some way are related to eachother.
560 * This is true for fragments, RTS or CTS-to-self frames.
561 * 2) Rule 1 can be broken when the available entries
562 * in the queue are less then a certain threshold.
564 if (rt2x00queue_threshold(queue) ||
565 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
566 queue->rt2x00dev->ops->lib->kick_queue(queue);
569 static void rt2x00queue_bar_check(struct queue_entry *entry)
571 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
572 struct ieee80211_bar *bar = (void *) (entry->skb->data +
573 rt2x00dev->extra_tx_headroom);
574 struct rt2x00_bar_list_entry *bar_entry;
576 if (likely(!ieee80211_is_back_req(bar->frame_control)))
577 return;
579 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
582 * If the alloc fails we still send the BAR out but just don't track
583 * it in our bar list. And as a result we will report it to mac80211
584 * back as failed.
586 if (!bar_entry)
587 return;
589 bar_entry->entry = entry;
590 bar_entry->block_acked = 0;
593 * Copy the relevant parts of the 802.11 BAR into out check list
594 * such that we can use RCU for less-overhead in the RX path since
595 * sending BARs and processing the according BlockAck should be
596 * the exception.
598 memcpy(bar_entry->ra, bar->ra, sizeof(bar->ra));
599 memcpy(bar_entry->ta, bar->ta, sizeof(bar->ta));
600 bar_entry->control = bar->control;
601 bar_entry->start_seq_num = bar->start_seq_num;
604 * Insert BAR into our BAR check list.
606 spin_lock_bh(&rt2x00dev->bar_list_lock);
607 list_add_tail_rcu(&bar_entry->list, &rt2x00dev->bar_list);
608 spin_unlock_bh(&rt2x00dev->bar_list_lock);
611 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
612 struct ieee80211_sta *sta, bool local)
614 struct ieee80211_tx_info *tx_info;
615 struct queue_entry *entry;
616 struct txentry_desc txdesc;
617 struct skb_frame_desc *skbdesc;
618 u8 rate_idx, rate_flags;
619 int ret = 0;
622 * Copy all TX descriptor information into txdesc,
623 * after that we are free to use the skb->cb array
624 * for our information.
626 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, sta);
629 * All information is retrieved from the skb->cb array,
630 * now we should claim ownership of the driver part of that
631 * array, preserving the bitrate index and flags.
633 tx_info = IEEE80211_SKB_CB(skb);
634 rate_idx = tx_info->control.rates[0].idx;
635 rate_flags = tx_info->control.rates[0].flags;
636 skbdesc = get_skb_frame_desc(skb);
637 memset(skbdesc, 0, sizeof(*skbdesc));
638 skbdesc->tx_rate_idx = rate_idx;
639 skbdesc->tx_rate_flags = rate_flags;
641 if (local)
642 skbdesc->flags |= SKBDESC_NOT_MAC80211;
645 * When hardware encryption is supported, and this frame
646 * is to be encrypted, we should strip the IV/EIV data from
647 * the frame so we can provide it to the driver separately.
649 if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
650 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
651 if (test_bit(REQUIRE_COPY_IV, &queue->rt2x00dev->cap_flags))
652 rt2x00crypto_tx_copy_iv(skb, &txdesc);
653 else
654 rt2x00crypto_tx_remove_iv(skb, &txdesc);
658 * When DMA allocation is required we should guarantee to the
659 * driver that the DMA is aligned to a 4-byte boundary.
660 * However some drivers require L2 padding to pad the payload
661 * rather then the header. This could be a requirement for
662 * PCI and USB devices, while header alignment only is valid
663 * for PCI devices.
665 if (test_bit(REQUIRE_L2PAD, &queue->rt2x00dev->cap_flags))
666 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
667 else if (test_bit(REQUIRE_DMA, &queue->rt2x00dev->cap_flags))
668 rt2x00queue_align_frame(skb);
671 * That function must be called with bh disabled.
673 spin_lock(&queue->tx_lock);
675 if (unlikely(rt2x00queue_full(queue))) {
676 rt2x00_err(queue->rt2x00dev, "Dropping frame due to full tx queue %d\n",
677 queue->qid);
678 ret = -ENOBUFS;
679 goto out;
682 entry = rt2x00queue_get_entry(queue, Q_INDEX);
684 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
685 &entry->flags))) {
686 rt2x00_err(queue->rt2x00dev,
687 "Arrived at non-free entry in the non-full queue %d\n"
688 "Please file bug report to %s\n",
689 queue->qid, DRV_PROJECT);
690 ret = -EINVAL;
691 goto out;
694 skbdesc->entry = entry;
695 entry->skb = skb;
698 * It could be possible that the queue was corrupted and this
699 * call failed. Since we always return NETDEV_TX_OK to mac80211,
700 * this frame will simply be dropped.
702 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
703 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
704 entry->skb = NULL;
705 ret = -EIO;
706 goto out;
710 * Put BlockAckReqs into our check list for driver BA processing.
712 rt2x00queue_bar_check(entry);
714 set_bit(ENTRY_DATA_PENDING, &entry->flags);
716 rt2x00queue_index_inc(entry, Q_INDEX);
717 rt2x00queue_write_tx_descriptor(entry, &txdesc);
718 rt2x00queue_kick_tx_queue(queue, &txdesc);
720 out:
721 spin_unlock(&queue->tx_lock);
722 return ret;
725 int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
726 struct ieee80211_vif *vif)
728 struct rt2x00_intf *intf = vif_to_intf(vif);
730 if (unlikely(!intf->beacon))
731 return -ENOBUFS;
733 mutex_lock(&intf->beacon_skb_mutex);
736 * Clean up the beacon skb.
738 rt2x00queue_free_skb(intf->beacon);
741 * Clear beacon (single bssid devices don't need to clear the beacon
742 * since the beacon queue will get stopped anyway).
744 if (rt2x00dev->ops->lib->clear_beacon)
745 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
747 mutex_unlock(&intf->beacon_skb_mutex);
749 return 0;
752 int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
753 struct ieee80211_vif *vif)
755 struct rt2x00_intf *intf = vif_to_intf(vif);
756 struct skb_frame_desc *skbdesc;
757 struct txentry_desc txdesc;
759 if (unlikely(!intf->beacon))
760 return -ENOBUFS;
763 * Clean up the beacon skb.
765 rt2x00queue_free_skb(intf->beacon);
767 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
768 if (!intf->beacon->skb)
769 return -ENOMEM;
772 * Copy all TX descriptor information into txdesc,
773 * after that we are free to use the skb->cb array
774 * for our information.
776 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
779 * Fill in skb descriptor
781 skbdesc = get_skb_frame_desc(intf->beacon->skb);
782 memset(skbdesc, 0, sizeof(*skbdesc));
783 skbdesc->entry = intf->beacon;
786 * Send beacon to hardware.
788 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
790 return 0;
794 int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
795 struct ieee80211_vif *vif)
797 struct rt2x00_intf *intf = vif_to_intf(vif);
798 int ret;
800 mutex_lock(&intf->beacon_skb_mutex);
801 ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
802 mutex_unlock(&intf->beacon_skb_mutex);
804 return ret;
807 bool rt2x00queue_for_each_entry(struct data_queue *queue,
808 enum queue_index start,
809 enum queue_index end,
810 void *data,
811 bool (*fn)(struct queue_entry *entry,
812 void *data))
814 unsigned long irqflags;
815 unsigned int index_start;
816 unsigned int index_end;
817 unsigned int i;
819 if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
820 rt2x00_err(queue->rt2x00dev,
821 "Entry requested from invalid index range (%d - %d)\n",
822 start, end);
823 return true;
827 * Only protect the range we are going to loop over,
828 * if during our loop a extra entry is set to pending
829 * it should not be kicked during this run, since it
830 * is part of another TX operation.
832 spin_lock_irqsave(&queue->index_lock, irqflags);
833 index_start = queue->index[start];
834 index_end = queue->index[end];
835 spin_unlock_irqrestore(&queue->index_lock, irqflags);
838 * Start from the TX done pointer, this guarantees that we will
839 * send out all frames in the correct order.
841 if (index_start < index_end) {
842 for (i = index_start; i < index_end; i++) {
843 if (fn(&queue->entries[i], data))
844 return true;
846 } else {
847 for (i = index_start; i < queue->limit; i++) {
848 if (fn(&queue->entries[i], data))
849 return true;
852 for (i = 0; i < index_end; i++) {
853 if (fn(&queue->entries[i], data))
854 return true;
858 return false;
860 EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
862 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
863 enum queue_index index)
865 struct queue_entry *entry;
866 unsigned long irqflags;
868 if (unlikely(index >= Q_INDEX_MAX)) {
869 rt2x00_err(queue->rt2x00dev, "Entry requested from invalid index type (%d)\n",
870 index);
871 return NULL;
874 spin_lock_irqsave(&queue->index_lock, irqflags);
876 entry = &queue->entries[queue->index[index]];
878 spin_unlock_irqrestore(&queue->index_lock, irqflags);
880 return entry;
882 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
884 void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
886 struct data_queue *queue = entry->queue;
887 unsigned long irqflags;
889 if (unlikely(index >= Q_INDEX_MAX)) {
890 rt2x00_err(queue->rt2x00dev,
891 "Index change on invalid index type (%d)\n", index);
892 return;
895 spin_lock_irqsave(&queue->index_lock, irqflags);
897 queue->index[index]++;
898 if (queue->index[index] >= queue->limit)
899 queue->index[index] = 0;
901 entry->last_action = jiffies;
903 if (index == Q_INDEX) {
904 queue->length++;
905 } else if (index == Q_INDEX_DONE) {
906 queue->length--;
907 queue->count++;
910 spin_unlock_irqrestore(&queue->index_lock, irqflags);
913 static void rt2x00queue_pause_queue_nocheck(struct data_queue *queue)
915 switch (queue->qid) {
916 case QID_AC_VO:
917 case QID_AC_VI:
918 case QID_AC_BE:
919 case QID_AC_BK:
921 * For TX queues, we have to disable the queue
922 * inside mac80211.
924 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
925 break;
926 default:
927 break;
930 void rt2x00queue_pause_queue(struct data_queue *queue)
932 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
933 !test_bit(QUEUE_STARTED, &queue->flags) ||
934 test_and_set_bit(QUEUE_PAUSED, &queue->flags))
935 return;
937 rt2x00queue_pause_queue_nocheck(queue);
939 EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
941 void rt2x00queue_unpause_queue(struct data_queue *queue)
943 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
944 !test_bit(QUEUE_STARTED, &queue->flags) ||
945 !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
946 return;
948 switch (queue->qid) {
949 case QID_AC_VO:
950 case QID_AC_VI:
951 case QID_AC_BE:
952 case QID_AC_BK:
954 * For TX queues, we have to enable the queue
955 * inside mac80211.
957 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
958 break;
959 case QID_RX:
961 * For RX we need to kick the queue now in order to
962 * receive frames.
964 queue->rt2x00dev->ops->lib->kick_queue(queue);
965 default:
966 break;
969 EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
971 void rt2x00queue_start_queue(struct data_queue *queue)
973 mutex_lock(&queue->status_lock);
975 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
976 test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
977 mutex_unlock(&queue->status_lock);
978 return;
981 set_bit(QUEUE_PAUSED, &queue->flags);
983 queue->rt2x00dev->ops->lib->start_queue(queue);
985 rt2x00queue_unpause_queue(queue);
987 mutex_unlock(&queue->status_lock);
989 EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
991 void rt2x00queue_stop_queue(struct data_queue *queue)
993 mutex_lock(&queue->status_lock);
995 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
996 mutex_unlock(&queue->status_lock);
997 return;
1000 rt2x00queue_pause_queue_nocheck(queue);
1002 queue->rt2x00dev->ops->lib->stop_queue(queue);
1004 mutex_unlock(&queue->status_lock);
1006 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
1008 void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
1010 bool started;
1011 bool tx_queue =
1012 (queue->qid == QID_AC_VO) ||
1013 (queue->qid == QID_AC_VI) ||
1014 (queue->qid == QID_AC_BE) ||
1015 (queue->qid == QID_AC_BK);
1017 mutex_lock(&queue->status_lock);
1020 * If the queue has been started, we must stop it temporarily
1021 * to prevent any new frames to be queued on the device. If
1022 * we are not dropping the pending frames, the queue must
1023 * only be stopped in the software and not the hardware,
1024 * otherwise the queue will never become empty on its own.
1026 started = test_bit(QUEUE_STARTED, &queue->flags);
1027 if (started) {
1029 * Pause the queue
1031 rt2x00queue_pause_queue(queue);
1034 * If we are not supposed to drop any pending
1035 * frames, this means we must force a start (=kick)
1036 * to the queue to make sure the hardware will
1037 * start transmitting.
1039 if (!drop && tx_queue)
1040 queue->rt2x00dev->ops->lib->kick_queue(queue);
1044 * Check if driver supports flushing, if that is the case we can
1045 * defer the flushing to the driver. Otherwise we must use the
1046 * alternative which just waits for the queue to become empty.
1048 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1049 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
1052 * The queue flush has failed...
1054 if (unlikely(!rt2x00queue_empty(queue)))
1055 rt2x00_warn(queue->rt2x00dev, "Queue %d failed to flush\n",
1056 queue->qid);
1059 * Restore the queue to the previous status
1061 if (started)
1062 rt2x00queue_unpause_queue(queue);
1064 mutex_unlock(&queue->status_lock);
1066 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1068 void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1070 struct data_queue *queue;
1073 * rt2x00queue_start_queue will call ieee80211_wake_queue
1074 * for each queue after is has been properly initialized.
1076 tx_queue_for_each(rt2x00dev, queue)
1077 rt2x00queue_start_queue(queue);
1079 rt2x00queue_start_queue(rt2x00dev->rx);
1081 EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1083 void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1085 struct data_queue *queue;
1088 * rt2x00queue_stop_queue will call ieee80211_stop_queue
1089 * as well, but we are completely shutting doing everything
1090 * now, so it is much safer to stop all TX queues at once,
1091 * and use rt2x00queue_stop_queue for cleaning up.
1093 ieee80211_stop_queues(rt2x00dev->hw);
1095 tx_queue_for_each(rt2x00dev, queue)
1096 rt2x00queue_stop_queue(queue);
1098 rt2x00queue_stop_queue(rt2x00dev->rx);
1100 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1102 void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1104 struct data_queue *queue;
1106 tx_queue_for_each(rt2x00dev, queue)
1107 rt2x00queue_flush_queue(queue, drop);
1109 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1111 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1113 static void rt2x00queue_reset(struct data_queue *queue)
1115 unsigned long irqflags;
1116 unsigned int i;
1118 spin_lock_irqsave(&queue->index_lock, irqflags);
1120 queue->count = 0;
1121 queue->length = 0;
1123 for (i = 0; i < Q_INDEX_MAX; i++)
1124 queue->index[i] = 0;
1126 spin_unlock_irqrestore(&queue->index_lock, irqflags);
1129 void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
1131 struct data_queue *queue;
1132 unsigned int i;
1134 queue_for_each(rt2x00dev, queue) {
1135 rt2x00queue_reset(queue);
1137 for (i = 0; i < queue->limit; i++)
1138 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
1142 static int rt2x00queue_alloc_entries(struct data_queue *queue)
1144 struct queue_entry *entries;
1145 unsigned int entry_size;
1146 unsigned int i;
1148 rt2x00queue_reset(queue);
1151 * Allocate all queue entries.
1153 entry_size = sizeof(*entries) + queue->priv_size;
1154 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
1155 if (!entries)
1156 return -ENOMEM;
1158 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
1159 (((char *)(__base)) + ((__limit) * (__esize)) + \
1160 ((__index) * (__psize)))
1162 for (i = 0; i < queue->limit; i++) {
1163 entries[i].flags = 0;
1164 entries[i].queue = queue;
1165 entries[i].skb = NULL;
1166 entries[i].entry_idx = i;
1167 entries[i].priv_data =
1168 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
1169 sizeof(*entries), queue->priv_size);
1172 #undef QUEUE_ENTRY_PRIV_OFFSET
1174 queue->entries = entries;
1176 return 0;
1179 static void rt2x00queue_free_skbs(struct data_queue *queue)
1181 unsigned int i;
1183 if (!queue->entries)
1184 return;
1186 for (i = 0; i < queue->limit; i++) {
1187 rt2x00queue_free_skb(&queue->entries[i]);
1191 static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
1193 unsigned int i;
1194 struct sk_buff *skb;
1196 for (i = 0; i < queue->limit; i++) {
1197 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
1198 if (!skb)
1199 return -ENOMEM;
1200 queue->entries[i].skb = skb;
1203 return 0;
1206 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1208 struct data_queue *queue;
1209 int status;
1211 status = rt2x00queue_alloc_entries(rt2x00dev->rx);
1212 if (status)
1213 goto exit;
1215 tx_queue_for_each(rt2x00dev, queue) {
1216 status = rt2x00queue_alloc_entries(queue);
1217 if (status)
1218 goto exit;
1221 status = rt2x00queue_alloc_entries(rt2x00dev->bcn);
1222 if (status)
1223 goto exit;
1225 if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
1226 status = rt2x00queue_alloc_entries(rt2x00dev->atim);
1227 if (status)
1228 goto exit;
1231 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
1232 if (status)
1233 goto exit;
1235 return 0;
1237 exit:
1238 rt2x00_err(rt2x00dev, "Queue entries allocation failed\n");
1240 rt2x00queue_uninitialize(rt2x00dev);
1242 return status;
1245 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1247 struct data_queue *queue;
1249 rt2x00queue_free_skbs(rt2x00dev->rx);
1251 queue_for_each(rt2x00dev, queue) {
1252 kfree(queue->entries);
1253 queue->entries = NULL;
1257 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1258 struct data_queue *queue, enum data_queue_qid qid)
1260 mutex_init(&queue->status_lock);
1261 spin_lock_init(&queue->tx_lock);
1262 spin_lock_init(&queue->index_lock);
1264 queue->rt2x00dev = rt2x00dev;
1265 queue->qid = qid;
1266 queue->txop = 0;
1267 queue->aifs = 2;
1268 queue->cw_min = 5;
1269 queue->cw_max = 10;
1271 rt2x00dev->ops->queue_init(queue);
1273 queue->threshold = DIV_ROUND_UP(queue->limit, 10);
1276 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1278 struct data_queue *queue;
1279 enum data_queue_qid qid;
1280 unsigned int req_atim =
1281 !!test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
1284 * We need the following queues:
1285 * RX: 1
1286 * TX: ops->tx_queues
1287 * Beacon: 1
1288 * Atim: 1 (if required)
1290 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
1292 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
1293 if (!queue) {
1294 rt2x00_err(rt2x00dev, "Queue allocation failed\n");
1295 return -ENOMEM;
1299 * Initialize pointers
1301 rt2x00dev->rx = queue;
1302 rt2x00dev->tx = &queue[1];
1303 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
1304 rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
1307 * Initialize queue parameters.
1308 * RX: qid = QID_RX
1309 * TX: qid = QID_AC_VO + index
1310 * TX: cw_min: 2^5 = 32.
1311 * TX: cw_max: 2^10 = 1024.
1312 * BCN: qid = QID_BEACON
1313 * ATIM: qid = QID_ATIM
1315 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1317 qid = QID_AC_VO;
1318 tx_queue_for_each(rt2x00dev, queue)
1319 rt2x00queue_init(rt2x00dev, queue, qid++);
1321 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
1322 if (req_atim)
1323 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
1325 return 0;
1328 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1330 kfree(rt2x00dev->rx);
1331 rt2x00dev->rx = NULL;
1332 rt2x00dev->tx = NULL;
1333 rt2x00dev->bcn = NULL;