blob: 50590b1420a516863845249c96f689b28e766964 [file] [log] [blame]
Ivo van Doorn181d6902008-02-05 16:42:23 -05001/*
Ivo van Doorn7e613e12010-08-06 20:45:38 +02002 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01004 Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
Ivo van Doorn181d6902008-02-05 16:42:23 -05005 <http://rt2x00.serialmonkey.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the
19 Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23/*
24 Module: rt2x00lib
25 Abstract: rt2x00 queue specific routines.
26 */
27
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Ivo van Doorn181d6902008-02-05 16:42:23 -050029#include <linux/kernel.h>
30#include <linux/module.h>
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020031#include <linux/dma-mapping.h>
Ivo van Doorn181d6902008-02-05 16:42:23 -050032
33#include "rt2x00.h"
34#include "rt2x00lib.h"
35
Helmut Schaa88211022012-04-19 13:24:10 +020036struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry, gfp_t gfp)
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020037{
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +020038 struct data_queue *queue = entry->queue;
39 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020040 struct sk_buff *skb;
41 struct skb_frame_desc *skbdesc;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020042 unsigned int frame_size;
43 unsigned int head_size = 0;
44 unsigned int tail_size = 0;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020045
46 /*
47 * The frame size includes descriptor size, because the
48 * hardware directly receive the frame into the skbuffer.
49 */
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +020050 frame_size = queue->data_size + queue->desc_size + queue->winfo_size;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020051
52 /*
Ivo van Doornff352392008-07-04 14:56:07 +020053 * 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.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020056 */
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020057 head_size = 4;
58
59 /*
60 * For IV/EIV/ICV assembly we must make sure there is
61 * at least 8 bytes bytes available in headroom for IV/EIV
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010062 * and 8 bytes for ICV data as tailroon.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020063 */
Gabor Juhos7b8a00d2013-10-11 13:18:41 +020064 if (rt2x00_has_cap_hw_crypto(rt2x00dev)) {
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020065 head_size += 8;
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010066 tail_size += 8;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020067 }
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020068
69 /*
70 * Allocate skbuffer.
71 */
Helmut Schaa88211022012-04-19 13:24:10 +020072 skb = __dev_alloc_skb(frame_size + head_size + tail_size, gfp);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020073 if (!skb)
74 return NULL;
75
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020076 /*
77 * Make sure we not have a frame with the requested bytes
78 * available in the head and tail.
79 */
80 skb_reserve(skb, head_size);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020081 skb_put(skb, frame_size);
82
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020083 /*
84 * Populate skbdesc.
85 */
86 skbdesc = get_skb_frame_desc(skb);
87 memset(skbdesc, 0, sizeof(*skbdesc));
88 skbdesc->entry = entry;
89
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020090 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags)) {
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +010091 dma_addr_t skb_dma;
92
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;
98 }
99
100 skbdesc->skb_dma = skb_dma;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200101 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
102 }
103
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200104 return skb;
105}
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200106
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100107int rt2x00queue_map_txskb(struct queue_entry *entry)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200108{
Ivo van Doornfa695602010-10-11 15:37:25 +0200109 struct device *dev = entry->queue->rt2x00dev->dev;
110 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200111
Ivo van Doorn3ee54a02008-08-29 21:04:50 +0200112 skbdesc->skb_dma =
Ivo van Doornfa695602010-10-11 15:37:25 +0200113 dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100114
115 if (unlikely(dma_mapping_error(dev, skbdesc->skb_dma)))
116 return -ENOMEM;
117
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200118 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100119 return 0;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200120}
121EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
122
Ivo van Doornfa695602010-10-11 15:37:25 +0200123void rt2x00queue_unmap_skb(struct queue_entry *entry)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200124{
Ivo van Doornfa695602010-10-11 15:37:25 +0200125 struct device *dev = entry->queue->rt2x00dev->dev;
126 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200127
128 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200129 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200130 DMA_FROM_DEVICE);
131 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
Helmut Schaa546adf22010-10-09 13:33:43 +0200132 } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200133 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200134 DMA_TO_DEVICE);
135 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
136 }
137}
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200138EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200139
Ivo van Doornfa695602010-10-11 15:37:25 +0200140void rt2x00queue_free_skb(struct queue_entry *entry)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200141{
Ivo van Doornfa695602010-10-11 15:37:25 +0200142 if (!entry->skb)
Ivo van Doorn9a613192008-07-05 15:11:57 +0200143 return;
144
Ivo van Doornfa695602010-10-11 15:37:25 +0200145 rt2x00queue_unmap_skb(entry);
146 dev_kfree_skb_any(entry->skb);
147 entry->skb = NULL;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200148}
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200149
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200150void rt2x00queue_align_frame(struct sk_buff *skb)
Ivo van Doorn9f166172009-04-26 16:08:50 +0200151{
Ivo van Doorn9f166172009-04-26 16:08:50 +0200152 unsigned int frame_length = skb->len;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200153 unsigned int align = ALIGN_SIZE(skb, 0);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200154
155 if (!align)
156 return;
157
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200158 skb_push(skb, align);
159 memmove(skb->data, skb->data + align, frame_length);
160 skb_trim(skb, frame_length);
161}
162
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200163void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
164{
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100165 unsigned int payload_length = skb->len - header_length;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200166 unsigned int header_align = ALIGN_SIZE(skb, 0);
167 unsigned int payload_align = ALIGN_SIZE(skb, header_length);
Gertjan van Wingerdee54be4e2009-12-04 23:47:07 +0100168 unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200169
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100170 /*
171 * Adjust the header alignment if the payload needs to be moved more
172 * than the header.
173 */
174 if (payload_align > header_align)
175 header_align += 4;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200176
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100177 /* There is nothing to do if no alignment is needed */
178 if (!header_align)
179 return;
180
181 /* Reserve the amount of space needed in front of the frame */
182 skb_push(skb, header_align);
183
184 /*
185 * Move the header.
186 */
187 memmove(skb->data, skb->data + header_align, header_length);
188
189 /* Move the payload, if present and if required */
190 if (payload_length && payload_align)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200191 memmove(skb->data + header_length + l2pad,
Gertjan van Wingerdea5186e92009-11-24 23:11:32 +0100192 skb->data + header_length + l2pad + payload_align,
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100193 payload_length);
194
195 /* Trim the skb to the correct size */
196 skb_trim(skb, header_length + l2pad + payload_length);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200197}
198
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200199void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
200{
Gertjan van Wingerdea061a932010-12-13 12:33:12 +0100201 /*
202 * L2 padding is only present if the skb contains more than just the
203 * IEEE 802.11 header.
204 */
205 unsigned int l2pad = (skb->len > header_length) ?
206 L2PAD_SIZE(header_length) : 0;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200207
Gertjan van Wingerde354e39d2009-12-04 23:47:02 +0100208 if (!l2pad)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200209 return;
210
Gertjan van Wingerdea061a932010-12-13 12:33:12 +0100211 memmove(skb->data + l2pad, skb->data, header_length);
212 skb_pull(skb, l2pad);
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200213}
214
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200215static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
216 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100217 struct txentry_desc *txdesc)
218{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200219 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
220 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100221 struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200222 u16 seqno;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100223
Helmut Schaac262e082011-03-03 19:39:56 +0100224 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
Ivo van Doorn7b409822008-12-20 10:58:33 +0100225 return;
226
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100227 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
228
Stanislaw Gruszkae66a8dd2012-04-02 13:21:06 +0200229 if (!test_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags)) {
230 /*
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.
235 */
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;
241 }
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100242
Ivo van Doorn7b409822008-12-20 10:58:33 +0100243 /*
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100244 * The hardware is not able to insert a sequence number. Assign a
245 * software generated one here.
Ivo van Doorn7b409822008-12-20 10:58:33 +0100246 *
247 * This is wrong because beacons are not getting sequence
248 * numbers assigned properly.
249 *
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.
253 */
Ivo van Doorn7b409822008-12-20 10:58:33 +0100254 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200255 seqno = atomic_add_return(0x10, &intf->seqno);
256 else
257 seqno = atomic_read(&intf->seqno);
258
Ivo van Doorn7b409822008-12-20 10:58:33 +0100259 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200260 hdr->seq_ctrl |= cpu_to_le16(seqno);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100261}
262
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200263static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
264 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100265 struct txentry_desc *txdesc,
266 const struct rt2x00_rate *hwrate)
267{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200268 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100269 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
270 unsigned int data_length;
271 unsigned int duration;
272 unsigned int residual;
273
Helmut Schaa25177942011-03-03 19:43:25 +0100274 /*
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.
278 */
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;
283
Ivo van Doorn7b409822008-12-20 10:58:33 +0100284 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200285 data_length = skb->len + 4;
286 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100287
288 /*
289 * PLCP setup
290 * Length calculation depends on OFDM/CCK rate.
291 */
Helmut Schaa26a1d072011-03-03 19:42:35 +0100292 txdesc->u.plcp.signal = hwrate->plcp;
293 txdesc->u.plcp.service = 0x04;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100294
295 if (hwrate->flags & DEV_RATE_OFDM) {
Helmut Schaa26a1d072011-03-03 19:42:35 +0100296 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
297 txdesc->u.plcp.length_low = data_length & 0x3f;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100298 } else {
299 /*
300 * Convert length to microseconds.
301 */
302 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
303 duration = GET_DURATION(data_length, hwrate->bitrate);
304
305 if (residual != 0) {
306 duration++;
307
308 /*
309 * Check if we need to set the Length Extension
310 */
311 if (hwrate->bitrate == 110 && residual <= 30)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100312 txdesc->u.plcp.service |= 0x80;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100313 }
314
Helmut Schaa26a1d072011-03-03 19:42:35 +0100315 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
316 txdesc->u.plcp.length_low = duration & 0xff;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100317
318 /*
319 * When preamble is enabled we should set the
320 * preamble bit for the signal.
321 */
322 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100323 txdesc->u.plcp.signal |= 0x08;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100324 }
325}
326
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200327static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
328 struct sk_buff *skb,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200329 struct txentry_desc *txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200330 struct ieee80211_sta *sta,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200331 const struct rt2x00_rate *hwrate)
332{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200333 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200334 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200335 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaaead2bb62011-09-08 14:37:19 +0200336 struct rt2x00_sta *sta_priv = NULL;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200337
Thomas Huehn36323f82012-07-23 21:33:42 +0200338 if (sta) {
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200339 txdesc->u.ht.mpdu_density =
Thomas Huehn36323f82012-07-23 21:33:42 +0200340 sta->ht_cap.ampdu_density;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200341
Thomas Huehn36323f82012-07-23 21:33:42 +0200342 sta_priv = sta_to_rt2x00_sta(sta);
Helmut Schaaead2bb62011-09-08 14:37:19 +0200343 txdesc->u.ht.wcid = sta_priv->wcid;
344 }
345
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200346 /*
347 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
348 * mcs rate to be used
349 */
350 if (txrate->flags & IEEE80211_TX_RC_MCS) {
351 txdesc->u.ht.mcs = txrate->idx;
352
353 /*
354 * MIMO PS should be set to 1 for STA's using dynamic SM PS
355 * when using more then one tx stream (>MCS7).
356 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200357 if (sta && txdesc->u.ht.mcs > 7 &&
Johannes Bergaf0ed692013-02-12 14:21:00 +0100358 sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200359 __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;
364 }
365
Stanislaw Gruszkada40f402012-04-04 16:15:33 +0200366 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;
371
372 /* Left zero on all other settings. */
373 return;
374 }
375
376 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
377
378 /*
379 * Only one STBC stream is supported for now.
380 */
381 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
382 txdesc->u.ht.stbc = 1;
383
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200384 /*
385 * This frame is eligible for an AMPDU, however, don't aggregate
386 * frames that are intended to probe a specific tx rate.
387 */
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);
391
392 /*
393 * Set 40Mhz mode if necessary (for legacy rates this will
394 * duplicate the frame to both channels).
395 */
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);
401
402 /*
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
407 *
408 * Note: rt2800 devices won't use CTS protection (if used)
409 * for frames not transmitted with TXOP_HTTXOP
410 */
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;
418}
419
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200420static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
421 struct sk_buff *skb,
Thomas Huehn36323f82012-07-23 21:33:42 +0200422 struct txentry_desc *txdesc,
423 struct ieee80211_sta *sta)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200424{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200425 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
426 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaa55b585e2011-03-03 19:43:49 +0100427 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
428 struct ieee80211_rate *rate;
429 const struct rt2x00_rate *hwrate = NULL;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200430
431 memset(txdesc, 0, sizeof(*txdesc));
432
433 /*
Gertjan van Wingerdedf624ca2010-05-03 22:43:05 +0200434 * Header and frame information.
Ivo van Doorn9f166172009-04-26 16:08:50 +0200435 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200436 txdesc->length = skb->len;
437 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200438
439 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200440 * Check whether this frame is to be acked.
441 */
Johannes Berge039fa42008-05-15 12:55:29 +0200442 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200443 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
444
445 /*
446 * Check if this is a RTS/CTS frame
447 */
Ivo van Doornac104462008-06-16 19:54:57 +0200448 if (ieee80211_is_rts(hdr->frame_control) ||
449 ieee80211_is_cts(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200450 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Ivo van Doornac104462008-06-16 19:54:57 +0200451 if (ieee80211_is_rts(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200452 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200453 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200454 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200455 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200456 rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200457 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200458 }
459
460 /*
461 * Determine retry information.
462 */
Johannes Berge6a98542008-10-21 12:40:02 +0200463 txdesc->retry_limit = tx_info->control.rates[0].count - 1;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100464 if (txdesc->retry_limit >= rt2x00dev->long_retry)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200465 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
466
467 /*
468 * Check if more fragments are pending
469 */
Helmut Schaa2606e422010-06-14 22:10:09 +0200470 if (ieee80211_has_morefrags(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200471 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
472 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
473 }
474
475 /*
Helmut Schaa2606e422010-06-14 22:10:09 +0200476 * Check if more frames (!= fragments) are pending
477 */
478 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
479 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
480
481 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200482 * Beacons and probe responses require the tsf timestamp
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100483 * to be inserted into the frame.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200484 */
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100485 if (ieee80211_is_beacon(hdr->frame_control) ||
486 ieee80211_is_probe_resp(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200487 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
488
Ivo van Doorn7b409822008-12-20 10:58:33 +0100489 if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
Helmut Schaa25177942011-03-03 19:43:25 +0100490 !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200491 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200492
Ivo van Doorn076f9582008-12-20 10:59:02 +0100493 /*
494 * Determine rate modulation.
495 */
Helmut Schaa55b585e2011-03-03 19:43:49 +0100496 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;
507 }
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200508
Ivo van Doorn7b409822008-12-20 10:58:33 +0100509 /*
510 * Apply TX descriptor handling by components
511 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200512 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
513 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100514
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200515 if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200516 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200517 sta, hwrate);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100518 else
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200519 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
520 hwrate);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200521}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200522
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200523static int rt2x00queue_write_tx_data(struct queue_entry *entry,
524 struct txentry_desc *txdesc)
525{
526 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
527
528 /*
529 * This should not happen, we already checked the entry
530 * was ours. When the hardware disagrees there has been
531 * a queue corruption!
532 */
533 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
534 rt2x00dev->ops->lib->get_entry_state(entry))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700535 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);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200539 return -EINVAL;
540 }
541
542 /*
543 * Add the requested extra tx headroom in front of the skb.
544 */
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200545 skb_push(entry->skb, rt2x00dev->extra_tx_headroom);
546 memset(entry->skb->data, 0, rt2x00dev->extra_tx_headroom);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200547
548 /*
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200549 * Call the driver's write_tx_data function, if it exists.
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200550 */
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200551 if (rt2x00dev->ops->lib->write_tx_data)
552 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200553
554 /*
555 * Map the skb to DMA.
556 */
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100557 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags) &&
558 rt2x00queue_map_txskb(entry))
559 return -ENOMEM;
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200560
561 return 0;
562}
563
Ivo van Doornbd88a782008-07-09 15:12:44 +0200564static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
565 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200566{
Ivo van Doornb8697672008-06-06 22:53:14 +0200567 struct data_queue *queue = entry->queue;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200568
Ivo van Doorn93331452010-08-23 19:53:39 +0200569 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200570
571 /*
572 * All processing on the frame has been completed, this means
573 * it is now ready to be dumped to userspace through debugfs.
574 */
Ivo van Doorn93331452010-08-23 19:53:39 +0200575 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200576}
577
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100578static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200579 struct txentry_desc *txdesc)
580{
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200581 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200582 * Check if we need to kick the queue, there are however a few rules
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200583 * 1) Don't kick unless this is the last in frame in a burst.
Ivo van Doornb8697672008-06-06 22:53:14 +0200584 * 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.
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200587 * 2) Rule 1 can be broken when the available entries
Ivo van Doornb8697672008-06-06 22:53:14 +0200588 * in the queue are less then a certain threshold.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200589 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200590 if (rt2x00queue_threshold(queue) ||
591 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
Ivo van Doorndbba3062010-12-13 12:34:54 +0100592 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200593}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200594
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100595static void rt2x00queue_bar_check(struct queue_entry *entry)
596{
597 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
598 struct ieee80211_bar *bar = (void *) (entry->skb->data +
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200599 rt2x00dev->extra_tx_headroom);
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100600 struct rt2x00_bar_list_entry *bar_entry;
601
602 if (likely(!ieee80211_is_back_req(bar->frame_control)))
603 return;
604
605 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
606
607 /*
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.
611 */
612 if (!bar_entry)
613 return;
614
615 bar_entry->entry = entry;
616 bar_entry->block_acked = 0;
617
618 /*
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.
623 */
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;
628
629 /*
630 * Insert BAR into our BAR check list.
631 */
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);
635}
636
Johannes Berg7351c6b2009-11-19 01:08:30 +0100637int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
638 bool local)
Ivo van Doorn6db37862008-06-06 22:50:28 +0200639{
Johannes Berge6a98542008-10-21 12:40:02 +0200640 struct ieee80211_tx_info *tx_info;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200641 struct queue_entry *entry;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200642 struct txentry_desc txdesc;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200643 struct skb_frame_desc *skbdesc;
Johannes Berge6a98542008-10-21 12:40:02 +0200644 u8 rate_idx, rate_flags;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200645 int ret = 0;
646
Ivo van Doorn6db37862008-06-06 22:50:28 +0200647 /*
648 * Copy all TX descriptor information into txdesc,
649 * after that we are free to use the skb->cb array
650 * for our information.
651 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200652 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, NULL);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200653
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200654 /*
Johannes Berge6a98542008-10-21 12:40:02 +0200655 * All information is retrieved from the skb->cb array,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200656 * now we should claim ownership of the driver part of that
Johannes Berge6a98542008-10-21 12:40:02 +0200657 * array, preserving the bitrate index and flags.
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200658 */
Johannes Berge6a98542008-10-21 12:40:02 +0200659 tx_info = IEEE80211_SKB_CB(skb);
660 rate_idx = tx_info->control.rates[0].idx;
661 rate_flags = tx_info->control.rates[0].flags;
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100662 skbdesc = get_skb_frame_desc(skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200663 memset(skbdesc, 0, sizeof(*skbdesc));
Johannes Berge6a98542008-10-21 12:40:02 +0200664 skbdesc->tx_rate_idx = rate_idx;
665 skbdesc->tx_rate_flags = rate_flags;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200666
Johannes Berg7351c6b2009-11-19 01:08:30 +0100667 if (local)
668 skbdesc->flags |= SKBDESC_NOT_MAC80211;
669
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200670 /*
671 * When hardware encryption is supported, and this frame
672 * is to be encrypted, we should strip the IV/EIV data from
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800673 * the frame so we can provide it to the driver separately.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200674 */
675 if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
Ivo van Doorndddfb472008-12-02 18:20:42 +0100676 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200677 if (test_bit(REQUIRE_COPY_IV, &queue->rt2x00dev->cap_flags))
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200678 rt2x00crypto_tx_copy_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100679 else
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200680 rt2x00crypto_tx_remove_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100681 }
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200682
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200683 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300684 * When DMA allocation is required we should guarantee to the
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200685 * driver that the DMA is aligned to a 4-byte boundary.
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200686 * 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.
690 */
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200691 if (test_bit(REQUIRE_L2PAD, &queue->rt2x00dev->cap_flags))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200692 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200693 else if (test_bit(REQUIRE_DMA, &queue->rt2x00dev->cap_flags))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200694 rt2x00queue_align_frame(skb);
695
Stanislaw Gruszka3780d032012-03-09 12:39:54 +0100696 /*
697 * That function must be called with bh disabled.
698 */
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200699 spin_lock(&queue->tx_lock);
700
701 if (unlikely(rt2x00queue_full(queue))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700702 rt2x00_err(queue->rt2x00dev, "Dropping frame due to full tx queue %d\n",
703 queue->qid);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200704 ret = -ENOBUFS;
705 goto out;
706 }
707
708 entry = rt2x00queue_get_entry(queue, Q_INDEX);
709
710 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
711 &entry->flags))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700712 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);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200716 ret = -EINVAL;
717 goto out;
718 }
719
720 skbdesc->entry = entry;
721 entry->skb = skb;
Ivo van Doorn9f166172009-04-26 16:08:50 +0200722
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200723 /*
724 * It could be possible that the queue was corrupted and this
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100725 * call failed. Since we always return NETDEV_TX_OK to mac80211,
726 * this frame will simply be dropped.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200727 */
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200728 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200729 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200730 entry->skb = NULL;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200731 ret = -EIO;
732 goto out;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200733 }
734
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100735 /*
736 * Put BlockAckReqs into our check list for driver BA processing.
737 */
738 rt2x00queue_bar_check(entry);
739
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200740 set_bit(ENTRY_DATA_PENDING, &entry->flags);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200741
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200742 rt2x00queue_index_inc(entry, Q_INDEX);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200743 rt2x00queue_write_tx_descriptor(entry, &txdesc);
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100744 rt2x00queue_kick_tx_queue(queue, &txdesc);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200745
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200746out:
747 spin_unlock(&queue->tx_lock);
748 return ret;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200749}
750
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100751int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
752 struct ieee80211_vif *vif)
753{
754 struct rt2x00_intf *intf = vif_to_intf(vif);
755
756 if (unlikely(!intf->beacon))
757 return -ENOBUFS;
758
759 mutex_lock(&intf->beacon_skb_mutex);
760
761 /*
762 * Clean up the beacon skb.
763 */
764 rt2x00queue_free_skb(intf->beacon);
765
766 /*
767 * Clear beacon (single bssid devices don't need to clear the beacon
768 * since the beacon queue will get stopped anyway).
769 */
770 if (rt2x00dev->ops->lib->clear_beacon)
771 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
772
773 mutex_unlock(&intf->beacon_skb_mutex);
774
775 return 0;
776}
777
Helmut Schaa8414ff02011-01-30 13:16:28 +0100778int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
779 struct ieee80211_vif *vif)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200780{
781 struct rt2x00_intf *intf = vif_to_intf(vif);
782 struct skb_frame_desc *skbdesc;
783 struct txentry_desc txdesc;
Ivo van Doornbd88a782008-07-09 15:12:44 +0200784
785 if (unlikely(!intf->beacon))
786 return -ENOBUFS;
787
Igor Perminov17512dc2009-08-08 23:55:18 +0200788 /*
789 * Clean up the beacon skb.
790 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200791 rt2x00queue_free_skb(intf->beacon);
Igor Perminov17512dc2009-08-08 23:55:18 +0200792
Ivo van Doornbd88a782008-07-09 15:12:44 +0200793 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
Helmut Schaa8414ff02011-01-30 13:16:28 +0100794 if (!intf->beacon->skb)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200795 return -ENOMEM;
796
797 /*
798 * Copy all TX descriptor information into txdesc,
799 * after that we are free to use the skb->cb array
800 * for our information.
801 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200802 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200803
804 /*
Ivo van Doornbd88a782008-07-09 15:12:44 +0200805 * Fill in skb descriptor
806 */
807 skbdesc = get_skb_frame_desc(intf->beacon->skb);
808 memset(skbdesc, 0, sizeof(*skbdesc));
Ivo van Doornbd88a782008-07-09 15:12:44 +0200809 skbdesc->entry = intf->beacon;
810
811 /*
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100812 * Send beacon to hardware.
Ivo van Doornbd88a782008-07-09 15:12:44 +0200813 */
Gertjan van Wingerdef224f4e2010-05-08 23:40:25 +0200814 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200815
Helmut Schaa8414ff02011-01-30 13:16:28 +0100816 return 0;
817
818}
819
820int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
821 struct ieee80211_vif *vif)
822{
823 struct rt2x00_intf *intf = vif_to_intf(vif);
824 int ret;
825
826 mutex_lock(&intf->beacon_skb_mutex);
827 ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
Igor Perminov17512dc2009-08-08 23:55:18 +0200828 mutex_unlock(&intf->beacon_skb_mutex);
829
Helmut Schaa8414ff02011-01-30 13:16:28 +0100830 return ret;
Ivo van Doornbd88a782008-07-09 15:12:44 +0200831}
832
Helmut Schaa10e11562011-04-18 15:27:43 +0200833bool rt2x00queue_for_each_entry(struct data_queue *queue,
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200834 enum queue_index start,
835 enum queue_index end,
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100836 void *data,
837 bool (*fn)(struct queue_entry *entry,
838 void *data))
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200839{
840 unsigned long irqflags;
841 unsigned int index_start;
842 unsigned int index_end;
843 unsigned int i;
844
845 if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700846 rt2x00_err(queue->rt2x00dev,
847 "Entry requested from invalid index range (%d - %d)\n",
848 start, end);
Helmut Schaa10e11562011-04-18 15:27:43 +0200849 return true;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200850 }
851
852 /*
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.
857 */
Ivo van Doorn813f0332010-11-06 15:48:05 +0100858 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200859 index_start = queue->index[start];
860 index_end = queue->index[end];
Ivo van Doorn813f0332010-11-06 15:48:05 +0100861 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200862
863 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300864 * Start from the TX done pointer, this guarantees that we will
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200865 * send out all frames in the correct order.
866 */
867 if (index_start < index_end) {
Helmut Schaa10e11562011-04-18 15:27:43 +0200868 for (i = index_start; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100869 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200870 return true;
871 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200872 } else {
Helmut Schaa10e11562011-04-18 15:27:43 +0200873 for (i = index_start; i < queue->limit; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100874 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200875 return true;
876 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200877
Helmut Schaa10e11562011-04-18 15:27:43 +0200878 for (i = 0; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100879 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200880 return true;
881 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200882 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200883
884 return false;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200885}
886EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
887
Ivo van Doorn181d6902008-02-05 16:42:23 -0500888struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
889 enum queue_index index)
890{
891 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100892 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500893
894 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700895 rt2x00_err(queue->rt2x00dev, "Entry requested from invalid index type (%d)\n",
896 index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500897 return NULL;
898 }
899
Ivo van Doorn813f0332010-11-06 15:48:05 +0100900 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500901
902 entry = &queue->entries[queue->index[index]];
903
Ivo van Doorn813f0332010-11-06 15:48:05 +0100904 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500905
906 return entry;
907}
908EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
909
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200910void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500911{
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200912 struct data_queue *queue = entry->queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100913 unsigned long irqflags;
914
Ivo van Doorn181d6902008-02-05 16:42:23 -0500915 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700916 rt2x00_err(queue->rt2x00dev,
917 "Index change on invalid index type (%d)\n", index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500918 return;
919 }
920
Ivo van Doorn813f0332010-11-06 15:48:05 +0100921 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500922
923 queue->index[index]++;
924 if (queue->index[index] >= queue->limit)
925 queue->index[index] = 0;
926
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200927 entry->last_action = jiffies;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200928
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100929 if (index == Q_INDEX) {
930 queue->length++;
931 } else if (index == Q_INDEX_DONE) {
932 queue->length--;
John Daiker55887512008-10-17 12:16:17 -0700933 queue->count++;
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100934 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500935
Ivo van Doorn813f0332010-11-06 15:48:05 +0100936 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500937}
Ivo van Doorn181d6902008-02-05 16:42:23 -0500938
Jingoo Han6cdfc1d2013-08-06 17:36:03 +0900939static void rt2x00queue_pause_queue_nocheck(struct data_queue *queue)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100940{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100941 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100942 case QID_AC_VO:
943 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100944 case QID_AC_BE:
945 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100946 /*
947 * For TX queues, we have to disable the queue
948 * inside mac80211.
949 */
950 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
951 break;
952 default:
953 break;
954 }
955}
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +0200956void rt2x00queue_pause_queue(struct data_queue *queue)
957{
958 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
959 !test_bit(QUEUE_STARTED, &queue->flags) ||
960 test_and_set_bit(QUEUE_PAUSED, &queue->flags))
961 return;
962
963 rt2x00queue_pause_queue_nocheck(queue);
964}
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100965EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
966
967void rt2x00queue_unpause_queue(struct data_queue *queue)
968{
969 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
970 !test_bit(QUEUE_STARTED, &queue->flags) ||
971 !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
972 return;
973
974 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100975 case QID_AC_VO:
976 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100977 case QID_AC_BE:
978 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100979 /*
980 * For TX queues, we have to enable the queue
981 * inside mac80211.
982 */
983 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
984 break;
Ivo van Doorn5be65602010-12-13 12:35:40 +0100985 case QID_RX:
986 /*
987 * For RX we need to kick the queue now in order to
988 * receive frames.
989 */
990 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100991 default:
992 break;
993 }
994}
995EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
996
997void rt2x00queue_start_queue(struct data_queue *queue)
998{
999 mutex_lock(&queue->status_lock);
1000
1001 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
1002 test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
1003 mutex_unlock(&queue->status_lock);
1004 return;
1005 }
1006
1007 set_bit(QUEUE_PAUSED, &queue->flags);
1008
1009 queue->rt2x00dev->ops->lib->start_queue(queue);
1010
1011 rt2x00queue_unpause_queue(queue);
1012
1013 mutex_unlock(&queue->status_lock);
1014}
1015EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
1016
1017void rt2x00queue_stop_queue(struct data_queue *queue)
1018{
1019 mutex_lock(&queue->status_lock);
1020
1021 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
1022 mutex_unlock(&queue->status_lock);
1023 return;
1024 }
1025
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +02001026 rt2x00queue_pause_queue_nocheck(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001027
1028 queue->rt2x00dev->ops->lib->stop_queue(queue);
1029
1030 mutex_unlock(&queue->status_lock);
1031}
1032EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
1033
Ivo van Doorn5be65602010-12-13 12:35:40 +01001034void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
1035{
Ivo van Doorn5be65602010-12-13 12:35:40 +01001036 bool tx_queue =
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001037 (queue->qid == QID_AC_VO) ||
Ivo van Doorn5be65602010-12-13 12:35:40 +01001038 (queue->qid == QID_AC_VI) ||
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001039 (queue->qid == QID_AC_BE) ||
1040 (queue->qid == QID_AC_BK);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001041
Ivo van Doorn5be65602010-12-13 12:35:40 +01001042
1043 /*
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001044 * If we are not supposed to drop any pending
1045 * frames, this means we must force a start (=kick)
1046 * to the queue to make sure the hardware will
1047 * start transmitting.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001048 */
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001049 if (!drop && tx_queue)
1050 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001051
1052 /*
Ivo van Doorn152a5992011-04-18 15:31:02 +02001053 * Check if driver supports flushing, if that is the case we can
1054 * defer the flushing to the driver. Otherwise we must use the
1055 * alternative which just waits for the queue to become empty.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001056 */
Ivo van Doorn152a5992011-04-18 15:31:02 +02001057 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1058 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001059
1060 /*
1061 * The queue flush has failed...
1062 */
1063 if (unlikely(!rt2x00queue_empty(queue)))
Joe Perchesec9c4982013-04-19 08:33:40 -07001064 rt2x00_warn(queue->rt2x00dev, "Queue %d failed to flush\n",
1065 queue->qid);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001066}
1067EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1068
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001069void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1070{
1071 struct data_queue *queue;
1072
1073 /*
1074 * rt2x00queue_start_queue will call ieee80211_wake_queue
1075 * for each queue after is has been properly initialized.
1076 */
1077 tx_queue_for_each(rt2x00dev, queue)
1078 rt2x00queue_start_queue(queue);
1079
1080 rt2x00queue_start_queue(rt2x00dev->rx);
1081}
1082EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1083
1084void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1085{
1086 struct data_queue *queue;
1087
1088 /*
1089 * rt2x00queue_stop_queue will call ieee80211_stop_queue
1090 * as well, but we are completely shutting doing everything
1091 * now, so it is much safer to stop all TX queues at once,
1092 * and use rt2x00queue_stop_queue for cleaning up.
1093 */
1094 ieee80211_stop_queues(rt2x00dev->hw);
1095
1096 tx_queue_for_each(rt2x00dev, queue)
1097 rt2x00queue_stop_queue(queue);
1098
1099 rt2x00queue_stop_queue(rt2x00dev->rx);
1100}
1101EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1102
Ivo van Doorn5be65602010-12-13 12:35:40 +01001103void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1104{
1105 struct data_queue *queue;
1106
1107 tx_queue_for_each(rt2x00dev, queue)
1108 rt2x00queue_flush_queue(queue, drop);
1109
1110 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1111}
1112EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1113
Ivo van Doorn181d6902008-02-05 16:42:23 -05001114static void rt2x00queue_reset(struct data_queue *queue)
1115{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001116 unsigned long irqflags;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001117 unsigned int i;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001118
Ivo van Doorn813f0332010-11-06 15:48:05 +01001119 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001120
1121 queue->count = 0;
1122 queue->length = 0;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001123
Johannes Stezenbach75256f02011-04-18 15:29:38 +02001124 for (i = 0; i < Q_INDEX_MAX; i++)
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001125 queue->index[i] = 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001126
Ivo van Doorn813f0332010-11-06 15:48:05 +01001127 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001128}
1129
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001130void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001131{
1132 struct data_queue *queue;
1133 unsigned int i;
1134
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001135 queue_for_each(rt2x00dev, queue) {
Ivo van Doorn181d6902008-02-05 16:42:23 -05001136 rt2x00queue_reset(queue);
1137
Ivo van Doorn64e7d722010-12-13 12:36:00 +01001138 for (i = 0; i < queue->limit; i++)
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001139 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001140 }
1141}
1142
Gabor Juhos15d6c072013-06-04 13:40:39 +02001143static int rt2x00queue_alloc_entries(struct data_queue *queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001144{
1145 struct queue_entry *entries;
1146 unsigned int entry_size;
1147 unsigned int i;
1148
1149 rt2x00queue_reset(queue);
1150
Ivo van Doorn181d6902008-02-05 16:42:23 -05001151 /*
1152 * Allocate all queue entries.
1153 */
Gabor Juhos568f7a42013-06-04 13:40:38 +02001154 entry_size = sizeof(*entries) + queue->priv_size;
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001155 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001156 if (!entries)
1157 return -ENOMEM;
1158
1159#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Mark Einonf8bfbc32010-11-06 15:47:25 +01001160 (((char *)(__base)) + ((__limit) * (__esize)) + \
1161 ((__index) * (__psize)))
Ivo van Doorn181d6902008-02-05 16:42:23 -05001162
1163 for (i = 0; i < queue->limit; i++) {
1164 entries[i].flags = 0;
1165 entries[i].queue = queue;
1166 entries[i].skb = NULL;
1167 entries[i].entry_idx = i;
1168 entries[i].priv_data =
1169 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
Gabor Juhos568f7a42013-06-04 13:40:38 +02001170 sizeof(*entries), queue->priv_size);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001171 }
1172
1173#undef QUEUE_ENTRY_PRIV_OFFSET
1174
1175 queue->entries = entries;
1176
1177 return 0;
1178}
1179
Ivo van Doornfa695602010-10-11 15:37:25 +02001180static void rt2x00queue_free_skbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001181{
1182 unsigned int i;
1183
1184 if (!queue->entries)
1185 return;
1186
1187 for (i = 0; i < queue->limit; i++) {
Ivo van Doornfa695602010-10-11 15:37:25 +02001188 rt2x00queue_free_skb(&queue->entries[i]);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001189 }
1190}
1191
Ivo van Doornfa695602010-10-11 15:37:25 +02001192static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001193{
1194 unsigned int i;
1195 struct sk_buff *skb;
1196
1197 for (i = 0; i < queue->limit; i++) {
Helmut Schaa88211022012-04-19 13:24:10 +02001198 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001199 if (!skb)
Ivo van Doorn61243d82008-06-20 22:10:53 +02001200 return -ENOMEM;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001201 queue->entries[i].skb = skb;
1202 }
1203
1204 return 0;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001205}
1206
Ivo van Doorn181d6902008-02-05 16:42:23 -05001207int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1208{
1209 struct data_queue *queue;
1210 int status;
1211
Gabor Juhos15d6c072013-06-04 13:40:39 +02001212 status = rt2x00queue_alloc_entries(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001213 if (status)
1214 goto exit;
1215
1216 tx_queue_for_each(rt2x00dev, queue) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001217 status = rt2x00queue_alloc_entries(queue);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001218 if (status)
1219 goto exit;
1220 }
1221
Gabor Juhos15d6c072013-06-04 13:40:39 +02001222 status = rt2x00queue_alloc_entries(rt2x00dev->bcn);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001223 if (status)
1224 goto exit;
1225
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02001226 if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001227 status = rt2x00queue_alloc_entries(rt2x00dev->atim);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001228 if (status)
1229 goto exit;
1230 }
Ivo van Doorn181d6902008-02-05 16:42:23 -05001231
Ivo van Doornfa695602010-10-11 15:37:25 +02001232 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001233 if (status)
1234 goto exit;
1235
1236 return 0;
1237
1238exit:
Joe Perchesec9c4982013-04-19 08:33:40 -07001239 rt2x00_err(rt2x00dev, "Queue entries allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001240
1241 rt2x00queue_uninitialize(rt2x00dev);
1242
1243 return status;
1244}
1245
1246void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1247{
1248 struct data_queue *queue;
1249
Ivo van Doornfa695602010-10-11 15:37:25 +02001250 rt2x00queue_free_skbs(rt2x00dev->rx);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001251
Ivo van Doorn181d6902008-02-05 16:42:23 -05001252 queue_for_each(rt2x00dev, queue) {
1253 kfree(queue->entries);
1254 queue->entries = NULL;
1255 }
1256}
1257
Ivo van Doorn8f539272008-02-10 22:51:41 +01001258static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1259 struct data_queue *queue, enum data_queue_qid qid)
1260{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001261 mutex_init(&queue->status_lock);
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +02001262 spin_lock_init(&queue->tx_lock);
Ivo van Doorn813f0332010-11-06 15:48:05 +01001263 spin_lock_init(&queue->index_lock);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001264
1265 queue->rt2x00dev = rt2x00dev;
1266 queue->qid = qid;
Ivo van Doorn2af0a572008-08-29 21:05:45 +02001267 queue->txop = 0;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001268 queue->aifs = 2;
1269 queue->cw_min = 5;
1270 queue->cw_max = 10;
Gabor Juhos10af87c2013-06-03 23:39:53 +02001271
Gabor Juhos705802b2013-06-04 13:40:50 +02001272 rt2x00dev->ops->queue_init(queue);
Gabor Juhos04453e92013-06-04 13:40:41 +02001273
1274 queue->threshold = DIV_ROUND_UP(queue->limit, 10);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001275}
1276
Ivo van Doorn181d6902008-02-05 16:42:23 -05001277int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1278{
1279 struct data_queue *queue;
1280 enum data_queue_qid qid;
1281 unsigned int req_atim =
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02001282 !!test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001283
1284 /*
1285 * We need the following queues:
1286 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001287 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -05001288 * Beacon: 1
1289 * Atim: 1 (if required)
1290 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001291 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001292
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001293 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001294 if (!queue) {
Joe Perchesec9c4982013-04-19 08:33:40 -07001295 rt2x00_err(rt2x00dev, "Queue allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001296 return -ENOMEM;
1297 }
1298
1299 /*
1300 * Initialize pointers
1301 */
1302 rt2x00dev->rx = queue;
1303 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001304 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001305 rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001306
1307 /*
1308 * Initialize queue parameters.
1309 * RX: qid = QID_RX
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001310 * TX: qid = QID_AC_VO + index
Ivo van Doorn181d6902008-02-05 16:42:23 -05001311 * TX: cw_min: 2^5 = 32.
1312 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +02001313 * BCN: qid = QID_BEACON
1314 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -05001315 */
Ivo van Doorn8f539272008-02-10 22:51:41 +01001316 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1317
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001318 qid = QID_AC_VO;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001319 tx_queue_for_each(rt2x00dev, queue)
1320 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001321
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001322 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001323 if (req_atim)
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001324 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001325
1326 return 0;
1327}
1328
1329void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1330{
1331 kfree(rt2x00dev->rx);
1332 rt2x00dev->rx = NULL;
1333 rt2x00dev->tx = NULL;
1334 rt2x00dev->bcn = NULL;
1335}