blob: 8e68f87ab13c3081f062acc69fb71f59601e836f [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
Jeff Kirshera05b8c52013-12-06 03:32:11 -080018 along with this program; if not, see <http://www.gnu.org/licenses/>.
Ivo van Doorn181d6902008-02-05 16:42:23 -050019 */
20
21/*
22 Module: rt2x00lib
23 Abstract: rt2x00 queue specific routines.
24 */
25
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ivo van Doorn181d6902008-02-05 16:42:23 -050027#include <linux/kernel.h>
28#include <linux/module.h>
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020029#include <linux/dma-mapping.h>
Ivo van Doorn181d6902008-02-05 16:42:23 -050030
31#include "rt2x00.h"
32#include "rt2x00lib.h"
33
Helmut Schaa88211022012-04-19 13:24:10 +020034struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry, gfp_t gfp)
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020035{
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +020036 struct data_queue *queue = entry->queue;
37 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020038 struct sk_buff *skb;
39 struct skb_frame_desc *skbdesc;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020040 unsigned int frame_size;
41 unsigned int head_size = 0;
42 unsigned int tail_size = 0;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020043
44 /*
45 * The frame size includes descriptor size, because the
46 * hardware directly receive the frame into the skbuffer.
47 */
Stanislaw Gruszkaf0bda572013-04-17 14:30:47 +020048 frame_size = queue->data_size + queue->desc_size + queue->winfo_size;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020049
50 /*
Ivo van Doornff352392008-07-04 14:56:07 +020051 * The payload should be aligned to a 4-byte boundary,
52 * this means we need at least 3 bytes for moving the frame
53 * into the correct offset.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020054 */
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020055 head_size = 4;
56
57 /*
58 * For IV/EIV/ICV assembly we must make sure there is
59 * at least 8 bytes bytes available in headroom for IV/EIV
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010060 * and 8 bytes for ICV data as tailroon.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020061 */
Gabor Juhos7b8a00d2013-10-11 13:18:41 +020062 if (rt2x00_has_cap_hw_crypto(rt2x00dev)) {
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020063 head_size += 8;
Ivo van Doorn9c3444d2008-12-03 17:29:48 +010064 tail_size += 8;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020065 }
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020066
67 /*
68 * Allocate skbuffer.
69 */
Helmut Schaa88211022012-04-19 13:24:10 +020070 skb = __dev_alloc_skb(frame_size + head_size + tail_size, gfp);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020071 if (!skb)
72 return NULL;
73
Ivo van Doorn2bb057d2008-08-04 16:37:44 +020074 /*
75 * Make sure we not have a frame with the requested bytes
76 * available in the head and tail.
77 */
78 skb_reserve(skb, head_size);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +020079 skb_put(skb, frame_size);
80
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020081 /*
82 * Populate skbdesc.
83 */
84 skbdesc = get_skb_frame_desc(skb);
85 memset(skbdesc, 0, sizeof(*skbdesc));
86 skbdesc->entry = entry;
87
Ivo van Doorn7dab73b2011-04-18 15:27:06 +020088 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags)) {
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +010089 dma_addr_t skb_dma;
90
91 skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
92 DMA_FROM_DEVICE);
93 if (unlikely(dma_mapping_error(rt2x00dev->dev, skb_dma))) {
94 dev_kfree_skb_any(skb);
95 return NULL;
96 }
97
98 skbdesc->skb_dma = skb_dma;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020099 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
100 }
101
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200102 return skb;
103}
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200104
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100105int rt2x00queue_map_txskb(struct queue_entry *entry)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200106{
Ivo van Doornfa695602010-10-11 15:37:25 +0200107 struct device *dev = entry->queue->rt2x00dev->dev;
108 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200109
Ivo van Doorn3ee54a02008-08-29 21:04:50 +0200110 skbdesc->skb_dma =
Ivo van Doornfa695602010-10-11 15:37:25 +0200111 dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100112
113 if (unlikely(dma_mapping_error(dev, skbdesc->skb_dma)))
114 return -ENOMEM;
115
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200116 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100117 return 0;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200118}
119EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
120
Ivo van Doornfa695602010-10-11 15:37:25 +0200121void rt2x00queue_unmap_skb(struct queue_entry *entry)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200122{
Ivo van Doornfa695602010-10-11 15:37:25 +0200123 struct device *dev = entry->queue->rt2x00dev->dev;
124 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200125
126 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200127 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200128 DMA_FROM_DEVICE);
129 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
Helmut Schaa546adf22010-10-09 13:33:43 +0200130 } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200131 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200132 DMA_TO_DEVICE);
133 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
134 }
135}
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200136EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200137
Ivo van Doornfa695602010-10-11 15:37:25 +0200138void rt2x00queue_free_skb(struct queue_entry *entry)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200139{
Ivo van Doornfa695602010-10-11 15:37:25 +0200140 if (!entry->skb)
Ivo van Doorn9a613192008-07-05 15:11:57 +0200141 return;
142
Ivo van Doornfa695602010-10-11 15:37:25 +0200143 rt2x00queue_unmap_skb(entry);
144 dev_kfree_skb_any(entry->skb);
145 entry->skb = NULL;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200146}
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200147
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200148void rt2x00queue_align_frame(struct sk_buff *skb)
Ivo van Doorn9f166172009-04-26 16:08:50 +0200149{
Ivo van Doorn9f166172009-04-26 16:08:50 +0200150 unsigned int frame_length = skb->len;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200151 unsigned int align = ALIGN_SIZE(skb, 0);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200152
153 if (!align)
154 return;
155
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200156 skb_push(skb, align);
157 memmove(skb->data, skb->data + align, frame_length);
158 skb_trim(skb, frame_length);
159}
160
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200161void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
162{
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100163 unsigned int payload_length = skb->len - header_length;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200164 unsigned int header_align = ALIGN_SIZE(skb, 0);
165 unsigned int payload_align = ALIGN_SIZE(skb, header_length);
Gertjan van Wingerdee54be4e2009-12-04 23:47:07 +0100166 unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200167
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100168 /*
169 * Adjust the header alignment if the payload needs to be moved more
170 * than the header.
171 */
172 if (payload_align > header_align)
173 header_align += 4;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200174
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100175 /* There is nothing to do if no alignment is needed */
176 if (!header_align)
177 return;
178
179 /* Reserve the amount of space needed in front of the frame */
180 skb_push(skb, header_align);
181
182 /*
183 * Move the header.
184 */
185 memmove(skb->data, skb->data + header_align, header_length);
186
187 /* Move the payload, if present and if required */
188 if (payload_length && payload_align)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200189 memmove(skb->data + header_length + l2pad,
Gertjan van Wingerdea5186e92009-11-24 23:11:32 +0100190 skb->data + header_length + l2pad + payload_align,
Gertjan van Wingerde2e331462009-12-04 23:47:03 +0100191 payload_length);
192
193 /* Trim the skb to the correct size */
194 skb_trim(skb, header_length + l2pad + payload_length);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200195}
196
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200197void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
198{
Gertjan van Wingerdea061a932010-12-13 12:33:12 +0100199 /*
200 * L2 padding is only present if the skb contains more than just the
201 * IEEE 802.11 header.
202 */
203 unsigned int l2pad = (skb->len > header_length) ?
204 L2PAD_SIZE(header_length) : 0;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200205
Gertjan van Wingerde354e39d2009-12-04 23:47:02 +0100206 if (!l2pad)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200207 return;
208
Gertjan van Wingerdea061a932010-12-13 12:33:12 +0100209 memmove(skb->data + l2pad, skb->data, header_length);
210 skb_pull(skb, l2pad);
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200211}
212
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200213static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
214 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100215 struct txentry_desc *txdesc)
216{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200217 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
218 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100219 struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200220 u16 seqno;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100221
Helmut Schaac262e082011-03-03 19:39:56 +0100222 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
Ivo van Doorn7b409822008-12-20 10:58:33 +0100223 return;
224
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100225 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
226
Stanislaw Gruszkae66a8dd2012-04-02 13:21:06 +0200227 if (!test_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags)) {
228 /*
229 * rt2800 has a H/W (or F/W) bug, device incorrectly increase
230 * seqno on retransmited data (non-QOS) frames. To workaround
231 * the problem let's generate seqno in software if QOS is
232 * disabled.
233 */
234 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
235 __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
236 else
237 /* H/W will generate sequence number */
238 return;
239 }
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100240
Ivo van Doorn7b409822008-12-20 10:58:33 +0100241 /*
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100242 * The hardware is not able to insert a sequence number. Assign a
243 * software generated one here.
Ivo van Doorn7b409822008-12-20 10:58:33 +0100244 *
245 * This is wrong because beacons are not getting sequence
246 * numbers assigned properly.
247 *
248 * A secondary problem exists for drivers that cannot toggle
249 * sequence counting per-frame, since those will override the
250 * sequence counter given by mac80211.
251 */
Ivo van Doorn7b409822008-12-20 10:58:33 +0100252 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200253 seqno = atomic_add_return(0x10, &intf->seqno);
254 else
255 seqno = atomic_read(&intf->seqno);
256
Ivo van Doorn7b409822008-12-20 10:58:33 +0100257 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200258 hdr->seq_ctrl |= cpu_to_le16(seqno);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100259}
260
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200261static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
262 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100263 struct txentry_desc *txdesc,
264 const struct rt2x00_rate *hwrate)
265{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200266 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100267 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
268 unsigned int data_length;
269 unsigned int duration;
270 unsigned int residual;
271
Helmut Schaa25177942011-03-03 19:43:25 +0100272 /*
273 * Determine with what IFS priority this frame should be send.
274 * Set ifs to IFS_SIFS when the this is not the first fragment,
275 * or this fragment came after RTS/CTS.
276 */
277 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
278 txdesc->u.plcp.ifs = IFS_BACKOFF;
279 else
280 txdesc->u.plcp.ifs = IFS_SIFS;
281
Ivo van Doorn7b409822008-12-20 10:58:33 +0100282 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200283 data_length = skb->len + 4;
284 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100285
286 /*
287 * PLCP setup
288 * Length calculation depends on OFDM/CCK rate.
289 */
Helmut Schaa26a1d072011-03-03 19:42:35 +0100290 txdesc->u.plcp.signal = hwrate->plcp;
291 txdesc->u.plcp.service = 0x04;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100292
293 if (hwrate->flags & DEV_RATE_OFDM) {
Helmut Schaa26a1d072011-03-03 19:42:35 +0100294 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
295 txdesc->u.plcp.length_low = data_length & 0x3f;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100296 } else {
297 /*
298 * Convert length to microseconds.
299 */
300 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
301 duration = GET_DURATION(data_length, hwrate->bitrate);
302
303 if (residual != 0) {
304 duration++;
305
306 /*
307 * Check if we need to set the Length Extension
308 */
309 if (hwrate->bitrate == 110 && residual <= 30)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100310 txdesc->u.plcp.service |= 0x80;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100311 }
312
Helmut Schaa26a1d072011-03-03 19:42:35 +0100313 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
314 txdesc->u.plcp.length_low = duration & 0xff;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100315
316 /*
317 * When preamble is enabled we should set the
318 * preamble bit for the signal.
319 */
320 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100321 txdesc->u.plcp.signal |= 0x08;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100322 }
323}
324
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200325static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
326 struct sk_buff *skb,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200327 struct txentry_desc *txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200328 struct ieee80211_sta *sta,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200329 const struct rt2x00_rate *hwrate)
330{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200331 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200332 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200333 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaaead2bb62011-09-08 14:37:19 +0200334 struct rt2x00_sta *sta_priv = NULL;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200335
Thomas Huehn36323f82012-07-23 21:33:42 +0200336 if (sta) {
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200337 txdesc->u.ht.mpdu_density =
Thomas Huehn36323f82012-07-23 21:33:42 +0200338 sta->ht_cap.ampdu_density;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200339
Thomas Huehn36323f82012-07-23 21:33:42 +0200340 sta_priv = sta_to_rt2x00_sta(sta);
Helmut Schaaead2bb62011-09-08 14:37:19 +0200341 txdesc->u.ht.wcid = sta_priv->wcid;
342 }
343
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200344 /*
345 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
346 * mcs rate to be used
347 */
348 if (txrate->flags & IEEE80211_TX_RC_MCS) {
349 txdesc->u.ht.mcs = txrate->idx;
350
351 /*
352 * MIMO PS should be set to 1 for STA's using dynamic SM PS
353 * when using more then one tx stream (>MCS7).
354 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200355 if (sta && txdesc->u.ht.mcs > 7 &&
Johannes Bergaf0ed692013-02-12 14:21:00 +0100356 sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200357 __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
358 } else {
359 txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
360 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
361 txdesc->u.ht.mcs |= 0x08;
362 }
363
Stanislaw Gruszkada40f402012-04-04 16:15:33 +0200364 if (test_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags)) {
365 if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
366 txdesc->u.ht.txop = TXOP_SIFS;
367 else
368 txdesc->u.ht.txop = TXOP_BACKOFF;
369
370 /* Left zero on all other settings. */
371 return;
372 }
373
374 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
375
376 /*
377 * Only one STBC stream is supported for now.
378 */
379 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
380 txdesc->u.ht.stbc = 1;
381
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200382 /*
383 * This frame is eligible for an AMPDU, however, don't aggregate
384 * frames that are intended to probe a specific tx rate.
385 */
386 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
387 !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
388 __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
389
390 /*
391 * Set 40Mhz mode if necessary (for legacy rates this will
392 * duplicate the frame to both channels).
393 */
394 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
395 txrate->flags & IEEE80211_TX_RC_DUP_DATA)
396 __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
397 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
398 __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
399
400 /*
401 * Determine IFS values
402 * - Use TXOP_BACKOFF for management frames except beacons
403 * - Use TXOP_SIFS for fragment bursts
404 * - Use TXOP_HTTXOP for everything else
405 *
406 * Note: rt2800 devices won't use CTS protection (if used)
407 * for frames not transmitted with TXOP_HTTXOP
408 */
409 if (ieee80211_is_mgmt(hdr->frame_control) &&
410 !ieee80211_is_beacon(hdr->frame_control))
411 txdesc->u.ht.txop = TXOP_BACKOFF;
412 else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
413 txdesc->u.ht.txop = TXOP_SIFS;
414 else
415 txdesc->u.ht.txop = TXOP_HTTXOP;
416}
417
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200418static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
419 struct sk_buff *skb,
Thomas Huehn36323f82012-07-23 21:33:42 +0200420 struct txentry_desc *txdesc,
421 struct ieee80211_sta *sta)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200422{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200423 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
424 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaa55b585e2011-03-03 19:43:49 +0100425 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
426 struct ieee80211_rate *rate;
427 const struct rt2x00_rate *hwrate = NULL;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200428
429 memset(txdesc, 0, sizeof(*txdesc));
430
431 /*
Gertjan van Wingerdedf624ca2010-05-03 22:43:05 +0200432 * Header and frame information.
Ivo van Doorn9f166172009-04-26 16:08:50 +0200433 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200434 txdesc->length = skb->len;
435 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200436
437 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200438 * Check whether this frame is to be acked.
439 */
Johannes Berge039fa42008-05-15 12:55:29 +0200440 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200441 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
442
443 /*
444 * Check if this is a RTS/CTS frame
445 */
Ivo van Doornac104462008-06-16 19:54:57 +0200446 if (ieee80211_is_rts(hdr->frame_control) ||
447 ieee80211_is_cts(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200448 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Ivo van Doornac104462008-06-16 19:54:57 +0200449 if (ieee80211_is_rts(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200450 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200451 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200452 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200453 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200454 rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200455 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200456 }
457
458 /*
459 * Determine retry information.
460 */
Johannes Berge6a98542008-10-21 12:40:02 +0200461 txdesc->retry_limit = tx_info->control.rates[0].count - 1;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100462 if (txdesc->retry_limit >= rt2x00dev->long_retry)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200463 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
464
465 /*
466 * Check if more fragments are pending
467 */
Helmut Schaa2606e422010-06-14 22:10:09 +0200468 if (ieee80211_has_morefrags(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200469 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
470 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
471 }
472
473 /*
Helmut Schaa2606e422010-06-14 22:10:09 +0200474 * Check if more frames (!= fragments) are pending
475 */
476 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
477 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
478
479 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200480 * Beacons and probe responses require the tsf timestamp
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100481 * to be inserted into the frame.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200482 */
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100483 if (ieee80211_is_beacon(hdr->frame_control) ||
484 ieee80211_is_probe_resp(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200485 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
486
Ivo van Doorn7b409822008-12-20 10:58:33 +0100487 if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
Helmut Schaa25177942011-03-03 19:43:25 +0100488 !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200489 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200490
Ivo van Doorn076f9582008-12-20 10:59:02 +0100491 /*
492 * Determine rate modulation.
493 */
Helmut Schaa55b585e2011-03-03 19:43:49 +0100494 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
495 txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
496 else if (txrate->flags & IEEE80211_TX_RC_MCS)
497 txdesc->rate_mode = RATE_MODE_HT_MIX;
498 else {
499 rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
500 hwrate = rt2x00_get_rate(rate->hw_value);
501 if (hwrate->flags & DEV_RATE_OFDM)
502 txdesc->rate_mode = RATE_MODE_OFDM;
503 else
504 txdesc->rate_mode = RATE_MODE_CCK;
505 }
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200506
Ivo van Doorn7b409822008-12-20 10:58:33 +0100507 /*
508 * Apply TX descriptor handling by components
509 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200510 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
511 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100512
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200513 if (test_bit(REQUIRE_HT_TX_DESC, &rt2x00dev->cap_flags))
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200514 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200515 sta, hwrate);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100516 else
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200517 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
518 hwrate);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200519}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200520
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200521static int rt2x00queue_write_tx_data(struct queue_entry *entry,
522 struct txentry_desc *txdesc)
523{
524 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
525
526 /*
527 * This should not happen, we already checked the entry
528 * was ours. When the hardware disagrees there has been
529 * a queue corruption!
530 */
531 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
532 rt2x00dev->ops->lib->get_entry_state(entry))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700533 rt2x00_err(rt2x00dev,
534 "Corrupt queue %d, accessing entry which is not ours\n"
535 "Please file bug report to %s\n",
536 entry->queue->qid, DRV_PROJECT);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200537 return -EINVAL;
538 }
539
540 /*
541 * Add the requested extra tx headroom in front of the skb.
542 */
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200543 skb_push(entry->skb, rt2x00dev->extra_tx_headroom);
544 memset(entry->skb->data, 0, rt2x00dev->extra_tx_headroom);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200545
546 /*
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200547 * Call the driver's write_tx_data function, if it exists.
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200548 */
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200549 if (rt2x00dev->ops->lib->write_tx_data)
550 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200551
552 /*
553 * Map the skb to DMA.
554 */
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100555 if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags) &&
556 rt2x00queue_map_txskb(entry))
557 return -ENOMEM;
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200558
559 return 0;
560}
561
Ivo van Doornbd88a782008-07-09 15:12:44 +0200562static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
563 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200564{
Ivo van Doornb8697672008-06-06 22:53:14 +0200565 struct data_queue *queue = entry->queue;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200566
Ivo van Doorn93331452010-08-23 19:53:39 +0200567 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200568
569 /*
570 * All processing on the frame has been completed, this means
571 * it is now ready to be dumped to userspace through debugfs.
572 */
Ivo van Doorn93331452010-08-23 19:53:39 +0200573 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200574}
575
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100576static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200577 struct txentry_desc *txdesc)
578{
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200579 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200580 * Check if we need to kick the queue, there are however a few rules
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200581 * 1) Don't kick unless this is the last in frame in a burst.
Ivo van Doornb8697672008-06-06 22:53:14 +0200582 * When the burst flag is set, this frame is always followed
583 * by another frame which in some way are related to eachother.
584 * This is true for fragments, RTS or CTS-to-self frames.
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200585 * 2) Rule 1 can be broken when the available entries
Ivo van Doornb8697672008-06-06 22:53:14 +0200586 * in the queue are less then a certain threshold.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200587 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200588 if (rt2x00queue_threshold(queue) ||
589 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
Ivo van Doorndbba3062010-12-13 12:34:54 +0100590 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200591}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200592
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100593static void rt2x00queue_bar_check(struct queue_entry *entry)
594{
595 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
596 struct ieee80211_bar *bar = (void *) (entry->skb->data +
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200597 rt2x00dev->extra_tx_headroom);
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100598 struct rt2x00_bar_list_entry *bar_entry;
599
600 if (likely(!ieee80211_is_back_req(bar->frame_control)))
601 return;
602
603 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
604
605 /*
606 * If the alloc fails we still send the BAR out but just don't track
607 * it in our bar list. And as a result we will report it to mac80211
608 * back as failed.
609 */
610 if (!bar_entry)
611 return;
612
613 bar_entry->entry = entry;
614 bar_entry->block_acked = 0;
615
616 /*
617 * Copy the relevant parts of the 802.11 BAR into out check list
618 * such that we can use RCU for less-overhead in the RX path since
619 * sending BARs and processing the according BlockAck should be
620 * the exception.
621 */
622 memcpy(bar_entry->ra, bar->ra, sizeof(bar->ra));
623 memcpy(bar_entry->ta, bar->ta, sizeof(bar->ta));
624 bar_entry->control = bar->control;
625 bar_entry->start_seq_num = bar->start_seq_num;
626
627 /*
628 * Insert BAR into our BAR check list.
629 */
630 spin_lock_bh(&rt2x00dev->bar_list_lock);
631 list_add_tail_rcu(&bar_entry->list, &rt2x00dev->bar_list);
632 spin_unlock_bh(&rt2x00dev->bar_list_lock);
633}
634
Johannes Berg7351c6b2009-11-19 01:08:30 +0100635int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
Stanislaw Gruszka3d8bfe12013-10-31 11:23:57 +0100636 struct ieee80211_sta *sta, bool local)
Ivo van Doorn6db37862008-06-06 22:50:28 +0200637{
Johannes Berge6a98542008-10-21 12:40:02 +0200638 struct ieee80211_tx_info *tx_info;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200639 struct queue_entry *entry;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200640 struct txentry_desc txdesc;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200641 struct skb_frame_desc *skbdesc;
Johannes Berge6a98542008-10-21 12:40:02 +0200642 u8 rate_idx, rate_flags;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200643 int ret = 0;
644
Ivo van Doorn6db37862008-06-06 22:50:28 +0200645 /*
646 * Copy all TX descriptor information into txdesc,
647 * after that we are free to use the skb->cb array
648 * for our information.
649 */
Stanislaw Gruszka3d8bfe12013-10-31 11:23:57 +0100650 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, sta);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200651
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200652 /*
Johannes Berge6a98542008-10-21 12:40:02 +0200653 * All information is retrieved from the skb->cb array,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200654 * now we should claim ownership of the driver part of that
Johannes Berge6a98542008-10-21 12:40:02 +0200655 * array, preserving the bitrate index and flags.
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200656 */
Johannes Berge6a98542008-10-21 12:40:02 +0200657 tx_info = IEEE80211_SKB_CB(skb);
658 rate_idx = tx_info->control.rates[0].idx;
659 rate_flags = tx_info->control.rates[0].flags;
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100660 skbdesc = get_skb_frame_desc(skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200661 memset(skbdesc, 0, sizeof(*skbdesc));
Johannes Berge6a98542008-10-21 12:40:02 +0200662 skbdesc->tx_rate_idx = rate_idx;
663 skbdesc->tx_rate_flags = rate_flags;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200664
Johannes Berg7351c6b2009-11-19 01:08:30 +0100665 if (local)
666 skbdesc->flags |= SKBDESC_NOT_MAC80211;
667
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200668 /*
669 * When hardware encryption is supported, and this frame
670 * is to be encrypted, we should strip the IV/EIV data from
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800671 * the frame so we can provide it to the driver separately.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200672 */
673 if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
Ivo van Doorndddfb472008-12-02 18:20:42 +0100674 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200675 if (test_bit(REQUIRE_COPY_IV, &queue->rt2x00dev->cap_flags))
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200676 rt2x00crypto_tx_copy_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100677 else
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200678 rt2x00crypto_tx_remove_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100679 }
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200680
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200681 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300682 * When DMA allocation is required we should guarantee to the
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200683 * driver that the DMA is aligned to a 4-byte boundary.
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200684 * However some drivers require L2 padding to pad the payload
685 * rather then the header. This could be a requirement for
686 * PCI and USB devices, while header alignment only is valid
687 * for PCI devices.
688 */
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200689 if (test_bit(REQUIRE_L2PAD, &queue->rt2x00dev->cap_flags))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200690 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200691 else if (test_bit(REQUIRE_DMA, &queue->rt2x00dev->cap_flags))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200692 rt2x00queue_align_frame(skb);
693
Stanislaw Gruszka3780d032012-03-09 12:39:54 +0100694 /*
695 * That function must be called with bh disabled.
696 */
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200697 spin_lock(&queue->tx_lock);
698
699 if (unlikely(rt2x00queue_full(queue))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700700 rt2x00_err(queue->rt2x00dev, "Dropping frame due to full tx queue %d\n",
701 queue->qid);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200702 ret = -ENOBUFS;
703 goto out;
704 }
705
706 entry = rt2x00queue_get_entry(queue, Q_INDEX);
707
708 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
709 &entry->flags))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700710 rt2x00_err(queue->rt2x00dev,
711 "Arrived at non-free entry in the non-full queue %d\n"
712 "Please file bug report to %s\n",
713 queue->qid, DRV_PROJECT);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200714 ret = -EINVAL;
715 goto out;
716 }
717
718 skbdesc->entry = entry;
719 entry->skb = skb;
Ivo van Doorn9f166172009-04-26 16:08:50 +0200720
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200721 /*
722 * It could be possible that the queue was corrupted and this
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100723 * call failed. Since we always return NETDEV_TX_OK to mac80211,
724 * this frame will simply be dropped.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200725 */
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200726 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200727 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200728 entry->skb = NULL;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200729 ret = -EIO;
730 goto out;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200731 }
732
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100733 /*
734 * Put BlockAckReqs into our check list for driver BA processing.
735 */
736 rt2x00queue_bar_check(entry);
737
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200738 set_bit(ENTRY_DATA_PENDING, &entry->flags);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200739
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200740 rt2x00queue_index_inc(entry, Q_INDEX);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200741 rt2x00queue_write_tx_descriptor(entry, &txdesc);
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100742 rt2x00queue_kick_tx_queue(queue, &txdesc);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200743
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200744out:
745 spin_unlock(&queue->tx_lock);
746 return ret;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200747}
748
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100749int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
750 struct ieee80211_vif *vif)
751{
752 struct rt2x00_intf *intf = vif_to_intf(vif);
753
754 if (unlikely(!intf->beacon))
755 return -ENOBUFS;
756
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100757 /*
758 * Clean up the beacon skb.
759 */
760 rt2x00queue_free_skb(intf->beacon);
761
762 /*
763 * Clear beacon (single bssid devices don't need to clear the beacon
764 * since the beacon queue will get stopped anyway).
765 */
766 if (rt2x00dev->ops->lib->clear_beacon)
767 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
768
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100769 return 0;
770}
771
Stanislaw Gruszka283dafa2014-06-05 13:52:23 +0200772int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
773 struct ieee80211_vif *vif)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200774{
775 struct rt2x00_intf *intf = vif_to_intf(vif);
776 struct skb_frame_desc *skbdesc;
777 struct txentry_desc txdesc;
Ivo van Doornbd88a782008-07-09 15:12:44 +0200778
779 if (unlikely(!intf->beacon))
780 return -ENOBUFS;
781
Igor Perminov17512dc2009-08-08 23:55:18 +0200782 /*
783 * Clean up the beacon skb.
784 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200785 rt2x00queue_free_skb(intf->beacon);
Igor Perminov17512dc2009-08-08 23:55:18 +0200786
Ivo van Doornbd88a782008-07-09 15:12:44 +0200787 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
Helmut Schaa8414ff02011-01-30 13:16:28 +0100788 if (!intf->beacon->skb)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200789 return -ENOMEM;
790
791 /*
792 * Copy all TX descriptor information into txdesc,
793 * after that we are free to use the skb->cb array
794 * for our information.
795 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200796 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200797
798 /*
Ivo van Doornbd88a782008-07-09 15:12:44 +0200799 * Fill in skb descriptor
800 */
801 skbdesc = get_skb_frame_desc(intf->beacon->skb);
802 memset(skbdesc, 0, sizeof(*skbdesc));
Ivo van Doornbd88a782008-07-09 15:12:44 +0200803 skbdesc->entry = intf->beacon;
804
805 /*
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100806 * Send beacon to hardware.
Ivo van Doornbd88a782008-07-09 15:12:44 +0200807 */
Gertjan van Wingerdef224f4e2010-05-08 23:40:25 +0200808 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200809
Helmut Schaa8414ff02011-01-30 13:16:28 +0100810 return 0;
811
812}
813
Helmut Schaa10e11562011-04-18 15:27:43 +0200814bool rt2x00queue_for_each_entry(struct data_queue *queue,
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200815 enum queue_index start,
816 enum queue_index end,
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100817 void *data,
818 bool (*fn)(struct queue_entry *entry,
819 void *data))
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200820{
821 unsigned long irqflags;
822 unsigned int index_start;
823 unsigned int index_end;
824 unsigned int i;
825
826 if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700827 rt2x00_err(queue->rt2x00dev,
828 "Entry requested from invalid index range (%d - %d)\n",
829 start, end);
Helmut Schaa10e11562011-04-18 15:27:43 +0200830 return true;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200831 }
832
833 /*
834 * Only protect the range we are going to loop over,
835 * if during our loop a extra entry is set to pending
836 * it should not be kicked during this run, since it
837 * is part of another TX operation.
838 */
Ivo van Doorn813f0332010-11-06 15:48:05 +0100839 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200840 index_start = queue->index[start];
841 index_end = queue->index[end];
Ivo van Doorn813f0332010-11-06 15:48:05 +0100842 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200843
844 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300845 * Start from the TX done pointer, this guarantees that we will
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200846 * send out all frames in the correct order.
847 */
848 if (index_start < index_end) {
Helmut Schaa10e11562011-04-18 15:27:43 +0200849 for (i = index_start; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100850 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200851 return true;
852 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200853 } else {
Helmut Schaa10e11562011-04-18 15:27:43 +0200854 for (i = index_start; i < queue->limit; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100855 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200856 return true;
857 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200858
Helmut Schaa10e11562011-04-18 15:27:43 +0200859 for (i = 0; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100860 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200861 return true;
862 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200863 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200864
865 return false;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200866}
867EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
868
Ivo van Doorn181d6902008-02-05 16:42:23 -0500869struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
870 enum queue_index index)
871{
872 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100873 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500874
875 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700876 rt2x00_err(queue->rt2x00dev, "Entry requested from invalid index type (%d)\n",
877 index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500878 return NULL;
879 }
880
Ivo van Doorn813f0332010-11-06 15:48:05 +0100881 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500882
883 entry = &queue->entries[queue->index[index]];
884
Ivo van Doorn813f0332010-11-06 15:48:05 +0100885 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500886
887 return entry;
888}
889EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
890
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200891void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500892{
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200893 struct data_queue *queue = entry->queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100894 unsigned long irqflags;
895
Ivo van Doorn181d6902008-02-05 16:42:23 -0500896 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700897 rt2x00_err(queue->rt2x00dev,
898 "Index change on invalid index type (%d)\n", index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500899 return;
900 }
901
Ivo van Doorn813f0332010-11-06 15:48:05 +0100902 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500903
904 queue->index[index]++;
905 if (queue->index[index] >= queue->limit)
906 queue->index[index] = 0;
907
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200908 entry->last_action = jiffies;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200909
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100910 if (index == Q_INDEX) {
911 queue->length++;
912 } else if (index == Q_INDEX_DONE) {
913 queue->length--;
John Daiker55887512008-10-17 12:16:17 -0700914 queue->count++;
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100915 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500916
Ivo van Doorn813f0332010-11-06 15:48:05 +0100917 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500918}
Ivo van Doorn181d6902008-02-05 16:42:23 -0500919
Jingoo Han6cdfc1d2013-08-06 17:36:03 +0900920static void rt2x00queue_pause_queue_nocheck(struct data_queue *queue)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100921{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100922 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100923 case QID_AC_VO:
924 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100925 case QID_AC_BE:
926 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100927 /*
928 * For TX queues, we have to disable the queue
929 * inside mac80211.
930 */
931 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
932 break;
933 default:
934 break;
935 }
936}
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +0200937void rt2x00queue_pause_queue(struct data_queue *queue)
938{
939 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
940 !test_bit(QUEUE_STARTED, &queue->flags) ||
941 test_and_set_bit(QUEUE_PAUSED, &queue->flags))
942 return;
943
944 rt2x00queue_pause_queue_nocheck(queue);
945}
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100946EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
947
948void rt2x00queue_unpause_queue(struct data_queue *queue)
949{
950 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
951 !test_bit(QUEUE_STARTED, &queue->flags) ||
952 !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
953 return;
954
955 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100956 case QID_AC_VO:
957 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100958 case QID_AC_BE:
959 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100960 /*
961 * For TX queues, we have to enable the queue
962 * inside mac80211.
963 */
964 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
965 break;
Ivo van Doorn5be65602010-12-13 12:35:40 +0100966 case QID_RX:
967 /*
968 * For RX we need to kick the queue now in order to
969 * receive frames.
970 */
971 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100972 default:
973 break;
974 }
975}
976EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
977
978void rt2x00queue_start_queue(struct data_queue *queue)
979{
980 mutex_lock(&queue->status_lock);
981
982 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
983 test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
984 mutex_unlock(&queue->status_lock);
985 return;
986 }
987
988 set_bit(QUEUE_PAUSED, &queue->flags);
989
990 queue->rt2x00dev->ops->lib->start_queue(queue);
991
992 rt2x00queue_unpause_queue(queue);
993
994 mutex_unlock(&queue->status_lock);
995}
996EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
997
998void rt2x00queue_stop_queue(struct data_queue *queue)
999{
1000 mutex_lock(&queue->status_lock);
1001
1002 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
1003 mutex_unlock(&queue->status_lock);
1004 return;
1005 }
1006
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +02001007 rt2x00queue_pause_queue_nocheck(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001008
1009 queue->rt2x00dev->ops->lib->stop_queue(queue);
1010
1011 mutex_unlock(&queue->status_lock);
1012}
1013EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
1014
Ivo van Doorn5be65602010-12-13 12:35:40 +01001015void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
1016{
Ivo van Doorn5be65602010-12-13 12:35:40 +01001017 bool tx_queue =
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001018 (queue->qid == QID_AC_VO) ||
Ivo van Doorn5be65602010-12-13 12:35:40 +01001019 (queue->qid == QID_AC_VI) ||
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001020 (queue->qid == QID_AC_BE) ||
1021 (queue->qid == QID_AC_BK);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001022
Ivo van Doorn5be65602010-12-13 12:35:40 +01001023
1024 /*
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001025 * If we are not supposed to drop any pending
1026 * frames, this means we must force a start (=kick)
1027 * to the queue to make sure the hardware will
1028 * start transmitting.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001029 */
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001030 if (!drop && tx_queue)
1031 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001032
1033 /*
Ivo van Doorn152a5992011-04-18 15:31:02 +02001034 * Check if driver supports flushing, if that is the case we can
1035 * defer the flushing to the driver. Otherwise we must use the
1036 * alternative which just waits for the queue to become empty.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001037 */
Ivo van Doorn152a5992011-04-18 15:31:02 +02001038 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1039 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001040
1041 /*
1042 * The queue flush has failed...
1043 */
1044 if (unlikely(!rt2x00queue_empty(queue)))
Joe Perchesec9c4982013-04-19 08:33:40 -07001045 rt2x00_warn(queue->rt2x00dev, "Queue %d failed to flush\n",
1046 queue->qid);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001047}
1048EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1049
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001050void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1051{
1052 struct data_queue *queue;
1053
1054 /*
1055 * rt2x00queue_start_queue will call ieee80211_wake_queue
1056 * for each queue after is has been properly initialized.
1057 */
1058 tx_queue_for_each(rt2x00dev, queue)
1059 rt2x00queue_start_queue(queue);
1060
1061 rt2x00queue_start_queue(rt2x00dev->rx);
1062}
1063EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1064
1065void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1066{
1067 struct data_queue *queue;
1068
1069 /*
1070 * rt2x00queue_stop_queue will call ieee80211_stop_queue
1071 * as well, but we are completely shutting doing everything
1072 * now, so it is much safer to stop all TX queues at once,
1073 * and use rt2x00queue_stop_queue for cleaning up.
1074 */
1075 ieee80211_stop_queues(rt2x00dev->hw);
1076
1077 tx_queue_for_each(rt2x00dev, queue)
1078 rt2x00queue_stop_queue(queue);
1079
1080 rt2x00queue_stop_queue(rt2x00dev->rx);
1081}
1082EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1083
Ivo van Doorn5be65602010-12-13 12:35:40 +01001084void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1085{
1086 struct data_queue *queue;
1087
1088 tx_queue_for_each(rt2x00dev, queue)
1089 rt2x00queue_flush_queue(queue, drop);
1090
1091 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1092}
1093EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1094
Ivo van Doorn181d6902008-02-05 16:42:23 -05001095static void rt2x00queue_reset(struct data_queue *queue)
1096{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001097 unsigned long irqflags;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001098 unsigned int i;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001099
Ivo van Doorn813f0332010-11-06 15:48:05 +01001100 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001101
1102 queue->count = 0;
1103 queue->length = 0;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001104
Johannes Stezenbach75256f02011-04-18 15:29:38 +02001105 for (i = 0; i < Q_INDEX_MAX; i++)
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001106 queue->index[i] = 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001107
Ivo van Doorn813f0332010-11-06 15:48:05 +01001108 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001109}
1110
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001111void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001112{
1113 struct data_queue *queue;
1114 unsigned int i;
1115
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001116 queue_for_each(rt2x00dev, queue) {
Ivo van Doorn181d6902008-02-05 16:42:23 -05001117 rt2x00queue_reset(queue);
1118
Ivo van Doorn64e7d722010-12-13 12:36:00 +01001119 for (i = 0; i < queue->limit; i++)
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001120 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001121 }
1122}
1123
Gabor Juhos15d6c072013-06-04 13:40:39 +02001124static int rt2x00queue_alloc_entries(struct data_queue *queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001125{
1126 struct queue_entry *entries;
1127 unsigned int entry_size;
1128 unsigned int i;
1129
1130 rt2x00queue_reset(queue);
1131
Ivo van Doorn181d6902008-02-05 16:42:23 -05001132 /*
1133 * Allocate all queue entries.
1134 */
Gabor Juhos568f7a42013-06-04 13:40:38 +02001135 entry_size = sizeof(*entries) + queue->priv_size;
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001136 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001137 if (!entries)
1138 return -ENOMEM;
1139
1140#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Mark Einonf8bfbc32010-11-06 15:47:25 +01001141 (((char *)(__base)) + ((__limit) * (__esize)) + \
1142 ((__index) * (__psize)))
Ivo van Doorn181d6902008-02-05 16:42:23 -05001143
1144 for (i = 0; i < queue->limit; i++) {
1145 entries[i].flags = 0;
1146 entries[i].queue = queue;
1147 entries[i].skb = NULL;
1148 entries[i].entry_idx = i;
1149 entries[i].priv_data =
1150 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
Gabor Juhos568f7a42013-06-04 13:40:38 +02001151 sizeof(*entries), queue->priv_size);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001152 }
1153
1154#undef QUEUE_ENTRY_PRIV_OFFSET
1155
1156 queue->entries = entries;
1157
1158 return 0;
1159}
1160
Ivo van Doornfa695602010-10-11 15:37:25 +02001161static void rt2x00queue_free_skbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001162{
1163 unsigned int i;
1164
1165 if (!queue->entries)
1166 return;
1167
1168 for (i = 0; i < queue->limit; i++) {
Ivo van Doornfa695602010-10-11 15:37:25 +02001169 rt2x00queue_free_skb(&queue->entries[i]);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001170 }
1171}
1172
Ivo van Doornfa695602010-10-11 15:37:25 +02001173static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001174{
1175 unsigned int i;
1176 struct sk_buff *skb;
1177
1178 for (i = 0; i < queue->limit; i++) {
Helmut Schaa88211022012-04-19 13:24:10 +02001179 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001180 if (!skb)
Ivo van Doorn61243d82008-06-20 22:10:53 +02001181 return -ENOMEM;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001182 queue->entries[i].skb = skb;
1183 }
1184
1185 return 0;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001186}
1187
Ivo van Doorn181d6902008-02-05 16:42:23 -05001188int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1189{
1190 struct data_queue *queue;
1191 int status;
1192
Gabor Juhos15d6c072013-06-04 13:40:39 +02001193 status = rt2x00queue_alloc_entries(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001194 if (status)
1195 goto exit;
1196
1197 tx_queue_for_each(rt2x00dev, queue) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001198 status = rt2x00queue_alloc_entries(queue);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001199 if (status)
1200 goto exit;
1201 }
1202
Gabor Juhos15d6c072013-06-04 13:40:39 +02001203 status = rt2x00queue_alloc_entries(rt2x00dev->bcn);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001204 if (status)
1205 goto exit;
1206
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02001207 if (test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags)) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001208 status = rt2x00queue_alloc_entries(rt2x00dev->atim);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001209 if (status)
1210 goto exit;
1211 }
Ivo van Doorn181d6902008-02-05 16:42:23 -05001212
Ivo van Doornfa695602010-10-11 15:37:25 +02001213 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001214 if (status)
1215 goto exit;
1216
1217 return 0;
1218
1219exit:
Joe Perchesec9c4982013-04-19 08:33:40 -07001220 rt2x00_err(rt2x00dev, "Queue entries allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001221
1222 rt2x00queue_uninitialize(rt2x00dev);
1223
1224 return status;
1225}
1226
1227void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1228{
1229 struct data_queue *queue;
1230
Ivo van Doornfa695602010-10-11 15:37:25 +02001231 rt2x00queue_free_skbs(rt2x00dev->rx);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001232
Ivo van Doorn181d6902008-02-05 16:42:23 -05001233 queue_for_each(rt2x00dev, queue) {
1234 kfree(queue->entries);
1235 queue->entries = NULL;
1236 }
1237}
1238
Ivo van Doorn8f539272008-02-10 22:51:41 +01001239static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1240 struct data_queue *queue, enum data_queue_qid qid)
1241{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001242 mutex_init(&queue->status_lock);
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +02001243 spin_lock_init(&queue->tx_lock);
Ivo van Doorn813f0332010-11-06 15:48:05 +01001244 spin_lock_init(&queue->index_lock);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001245
1246 queue->rt2x00dev = rt2x00dev;
1247 queue->qid = qid;
Ivo van Doorn2af0a572008-08-29 21:05:45 +02001248 queue->txop = 0;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001249 queue->aifs = 2;
1250 queue->cw_min = 5;
1251 queue->cw_max = 10;
Gabor Juhos10af87c2013-06-03 23:39:53 +02001252
Gabor Juhos705802b2013-06-04 13:40:50 +02001253 rt2x00dev->ops->queue_init(queue);
Gabor Juhos04453e92013-06-04 13:40:41 +02001254
1255 queue->threshold = DIV_ROUND_UP(queue->limit, 10);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001256}
1257
Ivo van Doorn181d6902008-02-05 16:42:23 -05001258int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1259{
1260 struct data_queue *queue;
1261 enum data_queue_qid qid;
1262 unsigned int req_atim =
Ivo van Doorn7dab73b2011-04-18 15:27:06 +02001263 !!test_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001264
1265 /*
1266 * We need the following queues:
1267 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001268 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -05001269 * Beacon: 1
1270 * Atim: 1 (if required)
1271 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001272 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001273
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001274 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001275 if (!queue) {
Joe Perchesec9c4982013-04-19 08:33:40 -07001276 rt2x00_err(rt2x00dev, "Queue allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001277 return -ENOMEM;
1278 }
1279
1280 /*
1281 * Initialize pointers
1282 */
1283 rt2x00dev->rx = queue;
1284 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001285 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001286 rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001287
1288 /*
1289 * Initialize queue parameters.
1290 * RX: qid = QID_RX
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001291 * TX: qid = QID_AC_VO + index
Ivo van Doorn181d6902008-02-05 16:42:23 -05001292 * TX: cw_min: 2^5 = 32.
1293 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +02001294 * BCN: qid = QID_BEACON
1295 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -05001296 */
Ivo van Doorn8f539272008-02-10 22:51:41 +01001297 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1298
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001299 qid = QID_AC_VO;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001300 tx_queue_for_each(rt2x00dev, queue)
1301 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001302
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001303 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001304 if (req_atim)
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001305 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001306
1307 return 0;
1308}
1309
1310void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1311{
1312 kfree(rt2x00dev->rx);
1313 rt2x00dev->rx = NULL;
1314 rt2x00dev->tx = NULL;
1315 rt2x00dev->bcn = NULL;
1316}