blob: 68b620b2462f1d149387feb4f33cde8359e929c2 [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
Fred Choub9d305c2014-12-26 16:19:18 +080088 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DMA)) {
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
Stanislaw Gruszkacfd91672014-11-11 14:28:47 +0100161/*
162 * H/W needs L2 padding between the header and the paylod if header size
163 * is not 4 bytes aligned.
164 */
165void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int hdr_len)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200166{
Stanislaw Gruszkacfd91672014-11-11 14:28:47 +0100167 unsigned int l2pad = (skb->len > hdr_len) ? L2PAD_SIZE(hdr_len) : 0;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200168
Gertjan van Wingerde354e39d2009-12-04 23:47:02 +0100169 if (!l2pad)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200170 return;
171
Stanislaw Gruszkacfd91672014-11-11 14:28:47 +0100172 skb_push(skb, l2pad);
173 memmove(skb->data, skb->data + l2pad, hdr_len);
174}
175
176void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int hdr_len)
177{
178 unsigned int l2pad = (skb->len > hdr_len) ? L2PAD_SIZE(hdr_len) : 0;
179
180 if (!l2pad)
181 return;
182
183 memmove(skb->data + l2pad, skb->data, hdr_len);
Gertjan van Wingerdea061a932010-12-13 12:33:12 +0100184 skb_pull(skb, l2pad);
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200185}
186
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200187static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
188 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100189 struct txentry_desc *txdesc)
190{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200191 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
192 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100193 struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200194 u16 seqno;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100195
Helmut Schaac262e082011-03-03 19:39:56 +0100196 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
Ivo van Doorn7b409822008-12-20 10:58:33 +0100197 return;
198
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100199 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
200
Fred Choub9d305c2014-12-26 16:19:18 +0800201 if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_SW_SEQNO)) {
Stanislaw Gruszkae66a8dd2012-04-02 13:21:06 +0200202 /*
203 * rt2800 has a H/W (or F/W) bug, device incorrectly increase
204 * seqno on retransmited data (non-QOS) frames. To workaround
205 * the problem let's generate seqno in software if QOS is
206 * disabled.
207 */
208 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
209 __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
210 else
211 /* H/W will generate sequence number */
212 return;
213 }
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100214
Ivo van Doorn7b409822008-12-20 10:58:33 +0100215 /*
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100216 * The hardware is not able to insert a sequence number. Assign a
217 * software generated one here.
Ivo van Doorn7b409822008-12-20 10:58:33 +0100218 *
219 * This is wrong because beacons are not getting sequence
220 * numbers assigned properly.
221 *
222 * A secondary problem exists for drivers that cannot toggle
223 * sequence counting per-frame, since those will override the
224 * sequence counter given by mac80211.
225 */
Ivo van Doorn7b409822008-12-20 10:58:33 +0100226 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200227 seqno = atomic_add_return(0x10, &intf->seqno);
228 else
229 seqno = atomic_read(&intf->seqno);
230
Ivo van Doorn7b409822008-12-20 10:58:33 +0100231 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200232 hdr->seq_ctrl |= cpu_to_le16(seqno);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100233}
234
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200235static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
236 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100237 struct txentry_desc *txdesc,
238 const struct rt2x00_rate *hwrate)
239{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200240 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100241 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
242 unsigned int data_length;
243 unsigned int duration;
244 unsigned int residual;
245
Helmut Schaa25177942011-03-03 19:43:25 +0100246 /*
247 * Determine with what IFS priority this frame should be send.
248 * Set ifs to IFS_SIFS when the this is not the first fragment,
249 * or this fragment came after RTS/CTS.
250 */
251 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
252 txdesc->u.plcp.ifs = IFS_BACKOFF;
253 else
254 txdesc->u.plcp.ifs = IFS_SIFS;
255
Ivo van Doorn7b409822008-12-20 10:58:33 +0100256 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200257 data_length = skb->len + 4;
258 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100259
260 /*
261 * PLCP setup
262 * Length calculation depends on OFDM/CCK rate.
263 */
Helmut Schaa26a1d072011-03-03 19:42:35 +0100264 txdesc->u.plcp.signal = hwrate->plcp;
265 txdesc->u.plcp.service = 0x04;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100266
267 if (hwrate->flags & DEV_RATE_OFDM) {
Helmut Schaa26a1d072011-03-03 19:42:35 +0100268 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
269 txdesc->u.plcp.length_low = data_length & 0x3f;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100270 } else {
271 /*
272 * Convert length to microseconds.
273 */
274 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
275 duration = GET_DURATION(data_length, hwrate->bitrate);
276
277 if (residual != 0) {
278 duration++;
279
280 /*
281 * Check if we need to set the Length Extension
282 */
283 if (hwrate->bitrate == 110 && residual <= 30)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100284 txdesc->u.plcp.service |= 0x80;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100285 }
286
Helmut Schaa26a1d072011-03-03 19:42:35 +0100287 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
288 txdesc->u.plcp.length_low = duration & 0xff;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100289
290 /*
291 * When preamble is enabled we should set the
292 * preamble bit for the signal.
293 */
294 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100295 txdesc->u.plcp.signal |= 0x08;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100296 }
297}
298
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200299static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
300 struct sk_buff *skb,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200301 struct txentry_desc *txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200302 struct ieee80211_sta *sta,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200303 const struct rt2x00_rate *hwrate)
304{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200305 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200306 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200307 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaaead2bb62011-09-08 14:37:19 +0200308 struct rt2x00_sta *sta_priv = NULL;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200309
Thomas Huehn36323f82012-07-23 21:33:42 +0200310 if (sta) {
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200311 txdesc->u.ht.mpdu_density =
Thomas Huehn36323f82012-07-23 21:33:42 +0200312 sta->ht_cap.ampdu_density;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200313
Thomas Huehn36323f82012-07-23 21:33:42 +0200314 sta_priv = sta_to_rt2x00_sta(sta);
Helmut Schaaead2bb62011-09-08 14:37:19 +0200315 txdesc->u.ht.wcid = sta_priv->wcid;
316 }
317
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200318 /*
319 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
320 * mcs rate to be used
321 */
322 if (txrate->flags & IEEE80211_TX_RC_MCS) {
323 txdesc->u.ht.mcs = txrate->idx;
324
325 /*
326 * MIMO PS should be set to 1 for STA's using dynamic SM PS
327 * when using more then one tx stream (>MCS7).
328 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200329 if (sta && txdesc->u.ht.mcs > 7 &&
Johannes Bergaf0ed692013-02-12 14:21:00 +0100330 sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200331 __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
332 } else {
333 txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
334 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
335 txdesc->u.ht.mcs |= 0x08;
336 }
337
Stanislaw Gruszkada40f402012-04-04 16:15:33 +0200338 if (test_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags)) {
339 if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
340 txdesc->u.ht.txop = TXOP_SIFS;
341 else
342 txdesc->u.ht.txop = TXOP_BACKOFF;
343
344 /* Left zero on all other settings. */
345 return;
346 }
347
348 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
349
350 /*
351 * Only one STBC stream is supported for now.
352 */
353 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
354 txdesc->u.ht.stbc = 1;
355
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200356 /*
357 * This frame is eligible for an AMPDU, however, don't aggregate
358 * frames that are intended to probe a specific tx rate.
359 */
360 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
361 !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
362 __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
363
364 /*
365 * Set 40Mhz mode if necessary (for legacy rates this will
366 * duplicate the frame to both channels).
367 */
368 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
369 txrate->flags & IEEE80211_TX_RC_DUP_DATA)
370 __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
371 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
372 __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
373
374 /*
375 * Determine IFS values
376 * - Use TXOP_BACKOFF for management frames except beacons
377 * - Use TXOP_SIFS for fragment bursts
378 * - Use TXOP_HTTXOP for everything else
379 *
380 * Note: rt2800 devices won't use CTS protection (if used)
381 * for frames not transmitted with TXOP_HTTXOP
382 */
383 if (ieee80211_is_mgmt(hdr->frame_control) &&
384 !ieee80211_is_beacon(hdr->frame_control))
385 txdesc->u.ht.txop = TXOP_BACKOFF;
386 else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
387 txdesc->u.ht.txop = TXOP_SIFS;
388 else
389 txdesc->u.ht.txop = TXOP_HTTXOP;
390}
391
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200392static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
393 struct sk_buff *skb,
Thomas Huehn36323f82012-07-23 21:33:42 +0200394 struct txentry_desc *txdesc,
395 struct ieee80211_sta *sta)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200396{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200397 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
398 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaa55b585e2011-03-03 19:43:49 +0100399 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
400 struct ieee80211_rate *rate;
401 const struct rt2x00_rate *hwrate = NULL;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200402
403 memset(txdesc, 0, sizeof(*txdesc));
404
405 /*
Gertjan van Wingerdedf624ca2010-05-03 22:43:05 +0200406 * Header and frame information.
Ivo van Doorn9f166172009-04-26 16:08:50 +0200407 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200408 txdesc->length = skb->len;
409 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200410
411 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200412 * Check whether this frame is to be acked.
413 */
Johannes Berge039fa42008-05-15 12:55:29 +0200414 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200415 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
416
417 /*
418 * Check if this is a RTS/CTS frame
419 */
Ivo van Doornac104462008-06-16 19:54:57 +0200420 if (ieee80211_is_rts(hdr->frame_control) ||
421 ieee80211_is_cts(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200422 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Ivo van Doornac104462008-06-16 19:54:57 +0200423 if (ieee80211_is_rts(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200424 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200425 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200426 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200427 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200428 rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200429 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200430 }
431
432 /*
433 * Determine retry information.
434 */
Johannes Berge6a98542008-10-21 12:40:02 +0200435 txdesc->retry_limit = tx_info->control.rates[0].count - 1;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100436 if (txdesc->retry_limit >= rt2x00dev->long_retry)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200437 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
438
439 /*
440 * Check if more fragments are pending
441 */
Helmut Schaa2606e422010-06-14 22:10:09 +0200442 if (ieee80211_has_morefrags(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200443 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
444 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
445 }
446
447 /*
Helmut Schaa2606e422010-06-14 22:10:09 +0200448 * Check if more frames (!= fragments) are pending
449 */
450 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
451 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
452
453 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200454 * Beacons and probe responses require the tsf timestamp
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100455 * to be inserted into the frame.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200456 */
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100457 if (ieee80211_is_beacon(hdr->frame_control) ||
458 ieee80211_is_probe_resp(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200459 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
460
Ivo van Doorn7b409822008-12-20 10:58:33 +0100461 if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
Helmut Schaa25177942011-03-03 19:43:25 +0100462 !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200463 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200464
Ivo van Doorn076f9582008-12-20 10:59:02 +0100465 /*
466 * Determine rate modulation.
467 */
Helmut Schaa55b585e2011-03-03 19:43:49 +0100468 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
469 txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
470 else if (txrate->flags & IEEE80211_TX_RC_MCS)
471 txdesc->rate_mode = RATE_MODE_HT_MIX;
472 else {
473 rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
474 hwrate = rt2x00_get_rate(rate->hw_value);
475 if (hwrate->flags & DEV_RATE_OFDM)
476 txdesc->rate_mode = RATE_MODE_OFDM;
477 else
478 txdesc->rate_mode = RATE_MODE_CCK;
479 }
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200480
Ivo van Doorn7b409822008-12-20 10:58:33 +0100481 /*
482 * Apply TX descriptor handling by components
483 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200484 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
485 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100486
Fred Choub9d305c2014-12-26 16:19:18 +0800487 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_HT_TX_DESC))
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200488 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200489 sta, hwrate);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100490 else
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200491 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
492 hwrate);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200493}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200494
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200495static int rt2x00queue_write_tx_data(struct queue_entry *entry,
496 struct txentry_desc *txdesc)
497{
498 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
499
500 /*
501 * This should not happen, we already checked the entry
502 * was ours. When the hardware disagrees there has been
503 * a queue corruption!
504 */
505 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
506 rt2x00dev->ops->lib->get_entry_state(entry))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700507 rt2x00_err(rt2x00dev,
508 "Corrupt queue %d, accessing entry which is not ours\n"
509 "Please file bug report to %s\n",
510 entry->queue->qid, DRV_PROJECT);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200511 return -EINVAL;
512 }
513
514 /*
515 * Add the requested extra tx headroom in front of the skb.
516 */
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200517 skb_push(entry->skb, rt2x00dev->extra_tx_headroom);
518 memset(entry->skb->data, 0, rt2x00dev->extra_tx_headroom);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200519
520 /*
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200521 * Call the driver's write_tx_data function, if it exists.
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200522 */
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200523 if (rt2x00dev->ops->lib->write_tx_data)
524 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200525
526 /*
527 * Map the skb to DMA.
528 */
Fred Choub9d305c2014-12-26 16:19:18 +0800529 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DMA) &&
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100530 rt2x00queue_map_txskb(entry))
531 return -ENOMEM;
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200532
533 return 0;
534}
535
Ivo van Doornbd88a782008-07-09 15:12:44 +0200536static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
537 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200538{
Ivo van Doornb8697672008-06-06 22:53:14 +0200539 struct data_queue *queue = entry->queue;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200540
Ivo van Doorn93331452010-08-23 19:53:39 +0200541 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200542
543 /*
544 * All processing on the frame has been completed, this means
545 * it is now ready to be dumped to userspace through debugfs.
546 */
Ivo van Doorn93331452010-08-23 19:53:39 +0200547 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200548}
549
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100550static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200551 struct txentry_desc *txdesc)
552{
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200553 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200554 * Check if we need to kick the queue, there are however a few rules
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200555 * 1) Don't kick unless this is the last in frame in a burst.
Ivo van Doornb8697672008-06-06 22:53:14 +0200556 * When the burst flag is set, this frame is always followed
557 * by another frame which in some way are related to eachother.
558 * This is true for fragments, RTS or CTS-to-self frames.
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200559 * 2) Rule 1 can be broken when the available entries
Ivo van Doornb8697672008-06-06 22:53:14 +0200560 * in the queue are less then a certain threshold.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200561 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200562 if (rt2x00queue_threshold(queue) ||
563 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
Ivo van Doorndbba3062010-12-13 12:34:54 +0100564 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200565}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200566
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100567static void rt2x00queue_bar_check(struct queue_entry *entry)
568{
569 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
570 struct ieee80211_bar *bar = (void *) (entry->skb->data +
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200571 rt2x00dev->extra_tx_headroom);
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100572 struct rt2x00_bar_list_entry *bar_entry;
573
574 if (likely(!ieee80211_is_back_req(bar->frame_control)))
575 return;
576
577 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
578
579 /*
580 * If the alloc fails we still send the BAR out but just don't track
581 * it in our bar list. And as a result we will report it to mac80211
582 * back as failed.
583 */
584 if (!bar_entry)
585 return;
586
587 bar_entry->entry = entry;
588 bar_entry->block_acked = 0;
589
590 /*
591 * Copy the relevant parts of the 802.11 BAR into out check list
592 * such that we can use RCU for less-overhead in the RX path since
593 * sending BARs and processing the according BlockAck should be
594 * the exception.
595 */
596 memcpy(bar_entry->ra, bar->ra, sizeof(bar->ra));
597 memcpy(bar_entry->ta, bar->ta, sizeof(bar->ta));
598 bar_entry->control = bar->control;
599 bar_entry->start_seq_num = bar->start_seq_num;
600
601 /*
602 * Insert BAR into our BAR check list.
603 */
604 spin_lock_bh(&rt2x00dev->bar_list_lock);
605 list_add_tail_rcu(&bar_entry->list, &rt2x00dev->bar_list);
606 spin_unlock_bh(&rt2x00dev->bar_list_lock);
607}
608
Johannes Berg7351c6b2009-11-19 01:08:30 +0100609int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
Stanislaw Gruszka3d8bfe12013-10-31 11:23:57 +0100610 struct ieee80211_sta *sta, bool local)
Ivo van Doorn6db37862008-06-06 22:50:28 +0200611{
Johannes Berge6a98542008-10-21 12:40:02 +0200612 struct ieee80211_tx_info *tx_info;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200613 struct queue_entry *entry;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200614 struct txentry_desc txdesc;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200615 struct skb_frame_desc *skbdesc;
Johannes Berge6a98542008-10-21 12:40:02 +0200616 u8 rate_idx, rate_flags;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200617 int ret = 0;
618
Ivo van Doorn6db37862008-06-06 22:50:28 +0200619 /*
620 * Copy all TX descriptor information into txdesc,
621 * after that we are free to use the skb->cb array
622 * for our information.
623 */
Stanislaw Gruszka3d8bfe12013-10-31 11:23:57 +0100624 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, sta);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200625
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200626 /*
Johannes Berge6a98542008-10-21 12:40:02 +0200627 * All information is retrieved from the skb->cb array,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200628 * now we should claim ownership of the driver part of that
Johannes Berge6a98542008-10-21 12:40:02 +0200629 * array, preserving the bitrate index and flags.
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200630 */
Johannes Berge6a98542008-10-21 12:40:02 +0200631 tx_info = IEEE80211_SKB_CB(skb);
632 rate_idx = tx_info->control.rates[0].idx;
633 rate_flags = tx_info->control.rates[0].flags;
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100634 skbdesc = get_skb_frame_desc(skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200635 memset(skbdesc, 0, sizeof(*skbdesc));
Johannes Berge6a98542008-10-21 12:40:02 +0200636 skbdesc->tx_rate_idx = rate_idx;
637 skbdesc->tx_rate_flags = rate_flags;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200638
Johannes Berg7351c6b2009-11-19 01:08:30 +0100639 if (local)
640 skbdesc->flags |= SKBDESC_NOT_MAC80211;
641
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200642 /*
643 * When hardware encryption is supported, and this frame
644 * is to be encrypted, we should strip the IV/EIV data from
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800645 * the frame so we can provide it to the driver separately.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200646 */
647 if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
Ivo van Doorndddfb472008-12-02 18:20:42 +0100648 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
Fred Choub9d305c2014-12-26 16:19:18 +0800649 if (rt2x00_has_cap_flag(queue->rt2x00dev, REQUIRE_COPY_IV))
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200650 rt2x00crypto_tx_copy_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100651 else
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200652 rt2x00crypto_tx_remove_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100653 }
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200654
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200655 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300656 * When DMA allocation is required we should guarantee to the
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200657 * driver that the DMA is aligned to a 4-byte boundary.
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200658 * However some drivers require L2 padding to pad the payload
659 * rather then the header. This could be a requirement for
660 * PCI and USB devices, while header alignment only is valid
661 * for PCI devices.
662 */
Fred Choub9d305c2014-12-26 16:19:18 +0800663 if (rt2x00_has_cap_flag(queue->rt2x00dev, REQUIRE_L2PAD))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200664 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
Fred Choub9d305c2014-12-26 16:19:18 +0800665 else if (rt2x00_has_cap_flag(queue->rt2x00dev, REQUIRE_DMA))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200666 rt2x00queue_align_frame(skb);
667
Stanislaw Gruszka3780d032012-03-09 12:39:54 +0100668 /*
669 * That function must be called with bh disabled.
670 */
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200671 spin_lock(&queue->tx_lock);
672
673 if (unlikely(rt2x00queue_full(queue))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700674 rt2x00_err(queue->rt2x00dev, "Dropping frame due to full tx queue %d\n",
675 queue->qid);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200676 ret = -ENOBUFS;
677 goto out;
678 }
679
680 entry = rt2x00queue_get_entry(queue, Q_INDEX);
681
682 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
683 &entry->flags))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700684 rt2x00_err(queue->rt2x00dev,
685 "Arrived at non-free entry in the non-full queue %d\n"
686 "Please file bug report to %s\n",
687 queue->qid, DRV_PROJECT);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200688 ret = -EINVAL;
689 goto out;
690 }
691
692 skbdesc->entry = entry;
693 entry->skb = skb;
Ivo van Doorn9f166172009-04-26 16:08:50 +0200694
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200695 /*
696 * It could be possible that the queue was corrupted and this
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100697 * call failed. Since we always return NETDEV_TX_OK to mac80211,
698 * this frame will simply be dropped.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200699 */
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200700 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200701 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200702 entry->skb = NULL;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200703 ret = -EIO;
704 goto out;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200705 }
706
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100707 /*
708 * Put BlockAckReqs into our check list for driver BA processing.
709 */
710 rt2x00queue_bar_check(entry);
711
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200712 set_bit(ENTRY_DATA_PENDING, &entry->flags);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200713
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200714 rt2x00queue_index_inc(entry, Q_INDEX);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200715 rt2x00queue_write_tx_descriptor(entry, &txdesc);
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100716 rt2x00queue_kick_tx_queue(queue, &txdesc);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200717
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200718out:
719 spin_unlock(&queue->tx_lock);
720 return ret;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200721}
722
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100723int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
724 struct ieee80211_vif *vif)
725{
726 struct rt2x00_intf *intf = vif_to_intf(vif);
727
728 if (unlikely(!intf->beacon))
729 return -ENOBUFS;
730
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100731 /*
732 * Clean up the beacon skb.
733 */
734 rt2x00queue_free_skb(intf->beacon);
735
736 /*
737 * Clear beacon (single bssid devices don't need to clear the beacon
738 * since the beacon queue will get stopped anyway).
739 */
740 if (rt2x00dev->ops->lib->clear_beacon)
741 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
742
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100743 return 0;
744}
745
Stanislaw Gruszka283dafa2014-06-05 13:52:23 +0200746int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
747 struct ieee80211_vif *vif)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200748{
749 struct rt2x00_intf *intf = vif_to_intf(vif);
750 struct skb_frame_desc *skbdesc;
751 struct txentry_desc txdesc;
Ivo van Doornbd88a782008-07-09 15:12:44 +0200752
753 if (unlikely(!intf->beacon))
754 return -ENOBUFS;
755
Igor Perminov17512dc2009-08-08 23:55:18 +0200756 /*
757 * Clean up the beacon skb.
758 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200759 rt2x00queue_free_skb(intf->beacon);
Igor Perminov17512dc2009-08-08 23:55:18 +0200760
Ivo van Doornbd88a782008-07-09 15:12:44 +0200761 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
Helmut Schaa8414ff02011-01-30 13:16:28 +0100762 if (!intf->beacon->skb)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200763 return -ENOMEM;
764
765 /*
766 * Copy all TX descriptor information into txdesc,
767 * after that we are free to use the skb->cb array
768 * for our information.
769 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200770 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200771
772 /*
Ivo van Doornbd88a782008-07-09 15:12:44 +0200773 * Fill in skb descriptor
774 */
775 skbdesc = get_skb_frame_desc(intf->beacon->skb);
776 memset(skbdesc, 0, sizeof(*skbdesc));
Ivo van Doornbd88a782008-07-09 15:12:44 +0200777 skbdesc->entry = intf->beacon;
778
779 /*
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100780 * Send beacon to hardware.
Ivo van Doornbd88a782008-07-09 15:12:44 +0200781 */
Gertjan van Wingerdef224f4e2010-05-08 23:40:25 +0200782 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200783
Helmut Schaa8414ff02011-01-30 13:16:28 +0100784 return 0;
785
786}
787
Helmut Schaa10e11562011-04-18 15:27:43 +0200788bool rt2x00queue_for_each_entry(struct data_queue *queue,
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200789 enum queue_index start,
790 enum queue_index end,
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100791 void *data,
792 bool (*fn)(struct queue_entry *entry,
793 void *data))
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200794{
795 unsigned long irqflags;
796 unsigned int index_start;
797 unsigned int index_end;
798 unsigned int i;
799
800 if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700801 rt2x00_err(queue->rt2x00dev,
802 "Entry requested from invalid index range (%d - %d)\n",
803 start, end);
Helmut Schaa10e11562011-04-18 15:27:43 +0200804 return true;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200805 }
806
807 /*
808 * Only protect the range we are going to loop over,
809 * if during our loop a extra entry is set to pending
810 * it should not be kicked during this run, since it
811 * is part of another TX operation.
812 */
Ivo van Doorn813f0332010-11-06 15:48:05 +0100813 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200814 index_start = queue->index[start];
815 index_end = queue->index[end];
Ivo van Doorn813f0332010-11-06 15:48:05 +0100816 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200817
818 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300819 * Start from the TX done pointer, this guarantees that we will
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200820 * send out all frames in the correct order.
821 */
822 if (index_start < index_end) {
Helmut Schaa10e11562011-04-18 15:27:43 +0200823 for (i = index_start; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100824 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200825 return true;
826 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200827 } else {
Helmut Schaa10e11562011-04-18 15:27:43 +0200828 for (i = index_start; i < queue->limit; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100829 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200830 return true;
831 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200832
Helmut Schaa10e11562011-04-18 15:27:43 +0200833 for (i = 0; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100834 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200835 return true;
836 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200837 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200838
839 return false;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200840}
841EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
842
Ivo van Doorn181d6902008-02-05 16:42:23 -0500843struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
844 enum queue_index index)
845{
846 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100847 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500848
849 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700850 rt2x00_err(queue->rt2x00dev, "Entry requested from invalid index type (%d)\n",
851 index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500852 return NULL;
853 }
854
Ivo van Doorn813f0332010-11-06 15:48:05 +0100855 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500856
857 entry = &queue->entries[queue->index[index]];
858
Ivo van Doorn813f0332010-11-06 15:48:05 +0100859 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500860
861 return entry;
862}
863EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
864
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200865void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500866{
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200867 struct data_queue *queue = entry->queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100868 unsigned long irqflags;
869
Ivo van Doorn181d6902008-02-05 16:42:23 -0500870 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700871 rt2x00_err(queue->rt2x00dev,
872 "Index change on invalid index type (%d)\n", index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500873 return;
874 }
875
Ivo van Doorn813f0332010-11-06 15:48:05 +0100876 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500877
878 queue->index[index]++;
879 if (queue->index[index] >= queue->limit)
880 queue->index[index] = 0;
881
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200882 entry->last_action = jiffies;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200883
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100884 if (index == Q_INDEX) {
885 queue->length++;
886 } else if (index == Q_INDEX_DONE) {
887 queue->length--;
John Daiker55887512008-10-17 12:16:17 -0700888 queue->count++;
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100889 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500890
Ivo van Doorn813f0332010-11-06 15:48:05 +0100891 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500892}
Ivo van Doorn181d6902008-02-05 16:42:23 -0500893
Jingoo Han6cdfc1d2013-08-06 17:36:03 +0900894static void rt2x00queue_pause_queue_nocheck(struct data_queue *queue)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100895{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100896 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100897 case QID_AC_VO:
898 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100899 case QID_AC_BE:
900 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100901 /*
902 * For TX queues, we have to disable the queue
903 * inside mac80211.
904 */
905 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
906 break;
907 default:
908 break;
909 }
910}
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +0200911void rt2x00queue_pause_queue(struct data_queue *queue)
912{
913 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
914 !test_bit(QUEUE_STARTED, &queue->flags) ||
915 test_and_set_bit(QUEUE_PAUSED, &queue->flags))
916 return;
917
918 rt2x00queue_pause_queue_nocheck(queue);
919}
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100920EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
921
922void rt2x00queue_unpause_queue(struct data_queue *queue)
923{
924 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
925 !test_bit(QUEUE_STARTED, &queue->flags) ||
926 !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
927 return;
928
929 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100930 case QID_AC_VO:
931 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100932 case QID_AC_BE:
933 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100934 /*
935 * For TX queues, we have to enable the queue
936 * inside mac80211.
937 */
938 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
939 break;
Ivo van Doorn5be65602010-12-13 12:35:40 +0100940 case QID_RX:
941 /*
942 * For RX we need to kick the queue now in order to
943 * receive frames.
944 */
945 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100946 default:
947 break;
948 }
949}
950EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
951
952void rt2x00queue_start_queue(struct data_queue *queue)
953{
954 mutex_lock(&queue->status_lock);
955
956 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
957 test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
958 mutex_unlock(&queue->status_lock);
959 return;
960 }
961
962 set_bit(QUEUE_PAUSED, &queue->flags);
963
964 queue->rt2x00dev->ops->lib->start_queue(queue);
965
966 rt2x00queue_unpause_queue(queue);
967
968 mutex_unlock(&queue->status_lock);
969}
970EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
971
972void rt2x00queue_stop_queue(struct data_queue *queue)
973{
974 mutex_lock(&queue->status_lock);
975
976 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
977 mutex_unlock(&queue->status_lock);
978 return;
979 }
980
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +0200981 rt2x00queue_pause_queue_nocheck(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100982
983 queue->rt2x00dev->ops->lib->stop_queue(queue);
984
985 mutex_unlock(&queue->status_lock);
986}
987EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
988
Ivo van Doorn5be65602010-12-13 12:35:40 +0100989void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
990{
Ivo van Doorn5be65602010-12-13 12:35:40 +0100991 bool tx_queue =
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100992 (queue->qid == QID_AC_VO) ||
Ivo van Doorn5be65602010-12-13 12:35:40 +0100993 (queue->qid == QID_AC_VI) ||
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100994 (queue->qid == QID_AC_BE) ||
995 (queue->qid == QID_AC_BK);
Ivo van Doorn5be65602010-12-13 12:35:40 +0100996
Ivo van Doorn5be65602010-12-13 12:35:40 +0100997
998 /*
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +0200999 * If we are not supposed to drop any pending
1000 * frames, this means we must force a start (=kick)
1001 * to the queue to make sure the hardware will
1002 * start transmitting.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001003 */
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001004 if (!drop && tx_queue)
1005 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001006
1007 /*
Ivo van Doorn152a5992011-04-18 15:31:02 +02001008 * Check if driver supports flushing, if that is the case we can
1009 * defer the flushing to the driver. Otherwise we must use the
1010 * alternative which just waits for the queue to become empty.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001011 */
Ivo van Doorn152a5992011-04-18 15:31:02 +02001012 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1013 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001014
1015 /*
1016 * The queue flush has failed...
1017 */
1018 if (unlikely(!rt2x00queue_empty(queue)))
Joe Perchesec9c4982013-04-19 08:33:40 -07001019 rt2x00_warn(queue->rt2x00dev, "Queue %d failed to flush\n",
1020 queue->qid);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001021}
1022EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1023
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001024void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1025{
1026 struct data_queue *queue;
1027
1028 /*
1029 * rt2x00queue_start_queue will call ieee80211_wake_queue
1030 * for each queue after is has been properly initialized.
1031 */
1032 tx_queue_for_each(rt2x00dev, queue)
1033 rt2x00queue_start_queue(queue);
1034
1035 rt2x00queue_start_queue(rt2x00dev->rx);
1036}
1037EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1038
1039void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1040{
1041 struct data_queue *queue;
1042
1043 /*
1044 * rt2x00queue_stop_queue will call ieee80211_stop_queue
1045 * as well, but we are completely shutting doing everything
1046 * now, so it is much safer to stop all TX queues at once,
1047 * and use rt2x00queue_stop_queue for cleaning up.
1048 */
1049 ieee80211_stop_queues(rt2x00dev->hw);
1050
1051 tx_queue_for_each(rt2x00dev, queue)
1052 rt2x00queue_stop_queue(queue);
1053
1054 rt2x00queue_stop_queue(rt2x00dev->rx);
1055}
1056EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1057
Ivo van Doorn5be65602010-12-13 12:35:40 +01001058void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1059{
1060 struct data_queue *queue;
1061
1062 tx_queue_for_each(rt2x00dev, queue)
1063 rt2x00queue_flush_queue(queue, drop);
1064
1065 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1066}
1067EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1068
Ivo van Doorn181d6902008-02-05 16:42:23 -05001069static void rt2x00queue_reset(struct data_queue *queue)
1070{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001071 unsigned long irqflags;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001072 unsigned int i;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001073
Ivo van Doorn813f0332010-11-06 15:48:05 +01001074 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001075
1076 queue->count = 0;
1077 queue->length = 0;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001078
Johannes Stezenbach75256f02011-04-18 15:29:38 +02001079 for (i = 0; i < Q_INDEX_MAX; i++)
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001080 queue->index[i] = 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001081
Ivo van Doorn813f0332010-11-06 15:48:05 +01001082 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001083}
1084
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001085void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001086{
1087 struct data_queue *queue;
1088 unsigned int i;
1089
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001090 queue_for_each(rt2x00dev, queue) {
Ivo van Doorn181d6902008-02-05 16:42:23 -05001091 rt2x00queue_reset(queue);
1092
Ivo van Doorn64e7d722010-12-13 12:36:00 +01001093 for (i = 0; i < queue->limit; i++)
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001094 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001095 }
1096}
1097
Gabor Juhos15d6c072013-06-04 13:40:39 +02001098static int rt2x00queue_alloc_entries(struct data_queue *queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001099{
1100 struct queue_entry *entries;
1101 unsigned int entry_size;
1102 unsigned int i;
1103
1104 rt2x00queue_reset(queue);
1105
Ivo van Doorn181d6902008-02-05 16:42:23 -05001106 /*
1107 * Allocate all queue entries.
1108 */
Gabor Juhos568f7a42013-06-04 13:40:38 +02001109 entry_size = sizeof(*entries) + queue->priv_size;
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001110 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001111 if (!entries)
1112 return -ENOMEM;
1113
1114#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Mark Einonf8bfbc32010-11-06 15:47:25 +01001115 (((char *)(__base)) + ((__limit) * (__esize)) + \
1116 ((__index) * (__psize)))
Ivo van Doorn181d6902008-02-05 16:42:23 -05001117
1118 for (i = 0; i < queue->limit; i++) {
1119 entries[i].flags = 0;
1120 entries[i].queue = queue;
1121 entries[i].skb = NULL;
1122 entries[i].entry_idx = i;
1123 entries[i].priv_data =
1124 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
Gabor Juhos568f7a42013-06-04 13:40:38 +02001125 sizeof(*entries), queue->priv_size);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001126 }
1127
1128#undef QUEUE_ENTRY_PRIV_OFFSET
1129
1130 queue->entries = entries;
1131
1132 return 0;
1133}
1134
Ivo van Doornfa695602010-10-11 15:37:25 +02001135static void rt2x00queue_free_skbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001136{
1137 unsigned int i;
1138
1139 if (!queue->entries)
1140 return;
1141
1142 for (i = 0; i < queue->limit; i++) {
Ivo van Doornfa695602010-10-11 15:37:25 +02001143 rt2x00queue_free_skb(&queue->entries[i]);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001144 }
1145}
1146
Ivo van Doornfa695602010-10-11 15:37:25 +02001147static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001148{
1149 unsigned int i;
1150 struct sk_buff *skb;
1151
1152 for (i = 0; i < queue->limit; i++) {
Helmut Schaa88211022012-04-19 13:24:10 +02001153 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001154 if (!skb)
Ivo van Doorn61243d82008-06-20 22:10:53 +02001155 return -ENOMEM;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001156 queue->entries[i].skb = skb;
1157 }
1158
1159 return 0;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001160}
1161
Ivo van Doorn181d6902008-02-05 16:42:23 -05001162int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1163{
1164 struct data_queue *queue;
1165 int status;
1166
Gabor Juhos15d6c072013-06-04 13:40:39 +02001167 status = rt2x00queue_alloc_entries(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001168 if (status)
1169 goto exit;
1170
1171 tx_queue_for_each(rt2x00dev, queue) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001172 status = rt2x00queue_alloc_entries(queue);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001173 if (status)
1174 goto exit;
1175 }
1176
Gabor Juhos15d6c072013-06-04 13:40:39 +02001177 status = rt2x00queue_alloc_entries(rt2x00dev->bcn);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001178 if (status)
1179 goto exit;
1180
Fred Choub9d305c2014-12-26 16:19:18 +08001181 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE)) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001182 status = rt2x00queue_alloc_entries(rt2x00dev->atim);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001183 if (status)
1184 goto exit;
1185 }
Ivo van Doorn181d6902008-02-05 16:42:23 -05001186
Ivo van Doornfa695602010-10-11 15:37:25 +02001187 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001188 if (status)
1189 goto exit;
1190
1191 return 0;
1192
1193exit:
Joe Perchesec9c4982013-04-19 08:33:40 -07001194 rt2x00_err(rt2x00dev, "Queue entries allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001195
1196 rt2x00queue_uninitialize(rt2x00dev);
1197
1198 return status;
1199}
1200
1201void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1202{
1203 struct data_queue *queue;
1204
Ivo van Doornfa695602010-10-11 15:37:25 +02001205 rt2x00queue_free_skbs(rt2x00dev->rx);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001206
Ivo van Doorn181d6902008-02-05 16:42:23 -05001207 queue_for_each(rt2x00dev, queue) {
1208 kfree(queue->entries);
1209 queue->entries = NULL;
1210 }
1211}
1212
Ivo van Doorn8f539272008-02-10 22:51:41 +01001213static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1214 struct data_queue *queue, enum data_queue_qid qid)
1215{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001216 mutex_init(&queue->status_lock);
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +02001217 spin_lock_init(&queue->tx_lock);
Ivo van Doorn813f0332010-11-06 15:48:05 +01001218 spin_lock_init(&queue->index_lock);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001219
1220 queue->rt2x00dev = rt2x00dev;
1221 queue->qid = qid;
Ivo van Doorn2af0a572008-08-29 21:05:45 +02001222 queue->txop = 0;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001223 queue->aifs = 2;
1224 queue->cw_min = 5;
1225 queue->cw_max = 10;
Gabor Juhos10af87c2013-06-03 23:39:53 +02001226
Gabor Juhos705802b2013-06-04 13:40:50 +02001227 rt2x00dev->ops->queue_init(queue);
Gabor Juhos04453e92013-06-04 13:40:41 +02001228
1229 queue->threshold = DIV_ROUND_UP(queue->limit, 10);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001230}
1231
Ivo van Doorn181d6902008-02-05 16:42:23 -05001232int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1233{
1234 struct data_queue *queue;
1235 enum data_queue_qid qid;
1236 unsigned int req_atim =
Fred Choub9d305c2014-12-26 16:19:18 +08001237 rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001238
1239 /*
1240 * We need the following queues:
1241 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001242 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -05001243 * Beacon: 1
1244 * Atim: 1 (if required)
1245 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001246 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001247
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001248 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001249 if (!queue) {
Joe Perchesec9c4982013-04-19 08:33:40 -07001250 rt2x00_err(rt2x00dev, "Queue allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001251 return -ENOMEM;
1252 }
1253
1254 /*
1255 * Initialize pointers
1256 */
1257 rt2x00dev->rx = queue;
1258 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001259 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001260 rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001261
1262 /*
1263 * Initialize queue parameters.
1264 * RX: qid = QID_RX
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001265 * TX: qid = QID_AC_VO + index
Ivo van Doorn181d6902008-02-05 16:42:23 -05001266 * TX: cw_min: 2^5 = 32.
1267 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +02001268 * BCN: qid = QID_BEACON
1269 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -05001270 */
Ivo van Doorn8f539272008-02-10 22:51:41 +01001271 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1272
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001273 qid = QID_AC_VO;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001274 tx_queue_for_each(rt2x00dev, queue)
1275 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001276
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001277 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001278 if (req_atim)
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001279 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001280
1281 return 0;
1282}
1283
1284void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1285{
1286 kfree(rt2x00dev->rx);
1287 rt2x00dev->rx = NULL;
1288 rt2x00dev->tx = NULL;
1289 rt2x00dev->bcn = NULL;
1290}