blob: 7ddee980048bcc51e8ca60e4717508927bb72b16 [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));
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020086
Fred Choub9d305c2014-12-26 16:19:18 +080087 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DMA)) {
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +010088 dma_addr_t skb_dma;
89
90 skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len,
91 DMA_FROM_DEVICE);
92 if (unlikely(dma_mapping_error(rt2x00dev->dev, skb_dma))) {
93 dev_kfree_skb_any(skb);
94 return NULL;
95 }
96
97 skbdesc->skb_dma = skb_dma;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020098 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
99 }
100
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200101 return skb;
102}
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200103
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100104int rt2x00queue_map_txskb(struct queue_entry *entry)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200105{
Ivo van Doornfa695602010-10-11 15:37:25 +0200106 struct device *dev = entry->queue->rt2x00dev->dev;
107 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200108
Ivo van Doorn3ee54a02008-08-29 21:04:50 +0200109 skbdesc->skb_dma =
Ivo van Doornfa695602010-10-11 15:37:25 +0200110 dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100111
112 if (unlikely(dma_mapping_error(dev, skbdesc->skb_dma)))
113 return -ENOMEM;
114
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200115 skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100116 return 0;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200117}
118EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
119
Ivo van Doornfa695602010-10-11 15:37:25 +0200120void rt2x00queue_unmap_skb(struct queue_entry *entry)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200121{
Ivo van Doornfa695602010-10-11 15:37:25 +0200122 struct device *dev = entry->queue->rt2x00dev->dev;
123 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200124
125 if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200126 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200127 DMA_FROM_DEVICE);
128 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
Helmut Schaa546adf22010-10-09 13:33:43 +0200129 } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
Ivo van Doornfa695602010-10-11 15:37:25 +0200130 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200131 DMA_TO_DEVICE);
132 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
133 }
134}
Gertjan van Wingerde0b8004a2010-06-03 10:51:45 +0200135EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200136
Ivo van Doornfa695602010-10-11 15:37:25 +0200137void rt2x00queue_free_skb(struct queue_entry *entry)
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200138{
Ivo van Doornfa695602010-10-11 15:37:25 +0200139 if (!entry->skb)
Ivo van Doorn9a613192008-07-05 15:11:57 +0200140 return;
141
Ivo van Doornfa695602010-10-11 15:37:25 +0200142 rt2x00queue_unmap_skb(entry);
143 dev_kfree_skb_any(entry->skb);
144 entry->skb = NULL;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +0200145}
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200146
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200147void rt2x00queue_align_frame(struct sk_buff *skb)
Ivo van Doorn9f166172009-04-26 16:08:50 +0200148{
Ivo van Doorn9f166172009-04-26 16:08:50 +0200149 unsigned int frame_length = skb->len;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200150 unsigned int align = ALIGN_SIZE(skb, 0);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200151
152 if (!align)
153 return;
154
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200155 skb_push(skb, align);
156 memmove(skb->data, skb->data + align, frame_length);
157 skb_trim(skb, frame_length);
158}
159
Stanislaw Gruszkacfd91672014-11-11 14:28:47 +0100160/*
161 * H/W needs L2 padding between the header and the paylod if header size
162 * is not 4 bytes aligned.
163 */
164void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int hdr_len)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200165{
Stanislaw Gruszkacfd91672014-11-11 14:28:47 +0100166 unsigned int l2pad = (skb->len > hdr_len) ? L2PAD_SIZE(hdr_len) : 0;
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200167
Gertjan van Wingerde354e39d2009-12-04 23:47:02 +0100168 if (!l2pad)
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200169 return;
170
Stanislaw Gruszkacfd91672014-11-11 14:28:47 +0100171 skb_push(skb, l2pad);
172 memmove(skb->data, skb->data + l2pad, hdr_len);
173}
174
175void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int hdr_len)
176{
177 unsigned int l2pad = (skb->len > hdr_len) ? L2PAD_SIZE(hdr_len) : 0;
178
179 if (!l2pad)
180 return;
181
182 memmove(skb->data + l2pad, skb->data, hdr_len);
Gertjan van Wingerdea061a932010-12-13 12:33:12 +0100183 skb_pull(skb, l2pad);
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200184}
185
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200186static void rt2x00queue_create_tx_descriptor_seq(struct rt2x00_dev *rt2x00dev,
187 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100188 struct txentry_desc *txdesc)
189{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200190 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
191 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100192 struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200193 u16 seqno;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100194
Helmut Schaac262e082011-03-03 19:39:56 +0100195 if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
Ivo van Doorn7b409822008-12-20 10:58:33 +0100196 return;
197
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100198 __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
199
Fred Choub9d305c2014-12-26 16:19:18 +0800200 if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_SW_SEQNO)) {
Stanislaw Gruszkae66a8dd2012-04-02 13:21:06 +0200201 /*
202 * rt2800 has a H/W (or F/W) bug, device incorrectly increase
203 * seqno on retransmited data (non-QOS) frames. To workaround
204 * the problem let's generate seqno in software if QOS is
205 * disabled.
206 */
207 if (test_bit(CONFIG_QOS_DISABLED, &rt2x00dev->flags))
208 __clear_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
209 else
210 /* H/W will generate sequence number */
211 return;
212 }
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100213
Ivo van Doorn7b409822008-12-20 10:58:33 +0100214 /*
Helmut Schaa7fe7ee72011-03-03 19:42:01 +0100215 * The hardware is not able to insert a sequence number. Assign a
216 * software generated one here.
Ivo van Doorn7b409822008-12-20 10:58:33 +0100217 *
218 * This is wrong because beacons are not getting sequence
219 * numbers assigned properly.
220 *
221 * A secondary problem exists for drivers that cannot toggle
222 * sequence counting per-frame, since those will override the
223 * sequence counter given by mac80211.
224 */
Ivo van Doorn7b409822008-12-20 10:58:33 +0100225 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200226 seqno = atomic_add_return(0x10, &intf->seqno);
227 else
228 seqno = atomic_read(&intf->seqno);
229
Ivo van Doorn7b409822008-12-20 10:58:33 +0100230 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkae5851da2012-06-01 11:29:40 +0200231 hdr->seq_ctrl |= cpu_to_le16(seqno);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100232}
233
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200234static void rt2x00queue_create_tx_descriptor_plcp(struct rt2x00_dev *rt2x00dev,
235 struct sk_buff *skb,
Ivo van Doorn7b409822008-12-20 10:58:33 +0100236 struct txentry_desc *txdesc,
237 const struct rt2x00_rate *hwrate)
238{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200239 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100240 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
241 unsigned int data_length;
242 unsigned int duration;
243 unsigned int residual;
244
Helmut Schaa25177942011-03-03 19:43:25 +0100245 /*
246 * Determine with what IFS priority this frame should be send.
247 * Set ifs to IFS_SIFS when the this is not the first fragment,
248 * or this fragment came after RTS/CTS.
249 */
250 if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
251 txdesc->u.plcp.ifs = IFS_BACKOFF;
252 else
253 txdesc->u.plcp.ifs = IFS_SIFS;
254
Ivo van Doorn7b409822008-12-20 10:58:33 +0100255 /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200256 data_length = skb->len + 4;
257 data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
Ivo van Doorn7b409822008-12-20 10:58:33 +0100258
259 /*
260 * PLCP setup
261 * Length calculation depends on OFDM/CCK rate.
262 */
Helmut Schaa26a1d072011-03-03 19:42:35 +0100263 txdesc->u.plcp.signal = hwrate->plcp;
264 txdesc->u.plcp.service = 0x04;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100265
266 if (hwrate->flags & DEV_RATE_OFDM) {
Helmut Schaa26a1d072011-03-03 19:42:35 +0100267 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
268 txdesc->u.plcp.length_low = data_length & 0x3f;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100269 } else {
270 /*
271 * Convert length to microseconds.
272 */
273 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
274 duration = GET_DURATION(data_length, hwrate->bitrate);
275
276 if (residual != 0) {
277 duration++;
278
279 /*
280 * Check if we need to set the Length Extension
281 */
282 if (hwrate->bitrate == 110 && residual <= 30)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100283 txdesc->u.plcp.service |= 0x80;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100284 }
285
Helmut Schaa26a1d072011-03-03 19:42:35 +0100286 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
287 txdesc->u.plcp.length_low = duration & 0xff;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100288
289 /*
290 * When preamble is enabled we should set the
291 * preamble bit for the signal.
292 */
293 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Helmut Schaa26a1d072011-03-03 19:42:35 +0100294 txdesc->u.plcp.signal |= 0x08;
Ivo van Doorn7b409822008-12-20 10:58:33 +0100295 }
296}
297
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200298static void rt2x00queue_create_tx_descriptor_ht(struct rt2x00_dev *rt2x00dev,
299 struct sk_buff *skb,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200300 struct txentry_desc *txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200301 struct ieee80211_sta *sta,
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200302 const struct rt2x00_rate *hwrate)
303{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200304 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200305 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200306 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaaead2bb62011-09-08 14:37:19 +0200307 struct rt2x00_sta *sta_priv = NULL;
Stanislaw Gruszkae49abb12016-12-19 11:52:48 +0100308 u8 density = 0;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200309
Thomas Huehn36323f82012-07-23 21:33:42 +0200310 if (sta) {
Thomas Huehn36323f82012-07-23 21:33:42 +0200311 sta_priv = sta_to_rt2x00_sta(sta);
Helmut Schaaead2bb62011-09-08 14:37:19 +0200312 txdesc->u.ht.wcid = sta_priv->wcid;
Stanislaw Gruszkae49abb12016-12-19 11:52:48 +0100313 density = sta->ht_cap.ampdu_density;
Helmut Schaaead2bb62011-09-08 14:37:19 +0200314 }
315
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200316 /*
317 * If IEEE80211_TX_RC_MCS is set txrate->idx just contains the
318 * mcs rate to be used
319 */
320 if (txrate->flags & IEEE80211_TX_RC_MCS) {
321 txdesc->u.ht.mcs = txrate->idx;
322
323 /*
324 * MIMO PS should be set to 1 for STA's using dynamic SM PS
325 * when using more then one tx stream (>MCS7).
326 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200327 if (sta && txdesc->u.ht.mcs > 7 &&
Johannes Bergaf0ed692013-02-12 14:21:00 +0100328 sta->smps_mode == IEEE80211_SMPS_DYNAMIC)
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200329 __set_bit(ENTRY_TXD_HT_MIMO_PS, &txdesc->flags);
330 } else {
331 txdesc->u.ht.mcs = rt2x00_get_rate_mcs(hwrate->mcs);
332 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
333 txdesc->u.ht.mcs |= 0x08;
334 }
335
Stanislaw Gruszkada40f402012-04-04 16:15:33 +0200336 if (test_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags)) {
337 if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
338 txdesc->u.ht.txop = TXOP_SIFS;
339 else
340 txdesc->u.ht.txop = TXOP_BACKOFF;
341
342 /* Left zero on all other settings. */
343 return;
344 }
345
Stanislaw Gruszkada40f402012-04-04 16:15:33 +0200346 /*
347 * Only one STBC stream is supported for now.
348 */
349 if (tx_info->flags & IEEE80211_TX_CTL_STBC)
350 txdesc->u.ht.stbc = 1;
351
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200352 /*
353 * This frame is eligible for an AMPDU, however, don't aggregate
354 * frames that are intended to probe a specific tx rate.
355 */
356 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU &&
Stanislaw Gruszkae49abb12016-12-19 11:52:48 +0100357 !(tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)) {
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200358 __set_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags);
Stanislaw Gruszkae49abb12016-12-19 11:52:48 +0100359 txdesc->u.ht.mpdu_density = density;
360 txdesc->u.ht.ba_size = 7; /* FIXME: What value is needed? */
361 }
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200362
363 /*
364 * Set 40Mhz mode if necessary (for legacy rates this will
365 * duplicate the frame to both channels).
366 */
367 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH ||
368 txrate->flags & IEEE80211_TX_RC_DUP_DATA)
369 __set_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags);
370 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
371 __set_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags);
372
373 /*
374 * Determine IFS values
Stanislaw Gruszka52a19232018-05-28 13:25:06 +0200375 * - Use TXOP_BACKOFF for management frames except beacons
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200376 * - Use TXOP_SIFS for fragment bursts
377 * - Use TXOP_HTTXOP for everything else
378 *
379 * Note: rt2800 devices won't use CTS protection (if used)
380 * for frames not transmitted with TXOP_HTTXOP
381 */
Stanislaw Gruszka52a19232018-05-28 13:25:06 +0200382 if (ieee80211_is_mgmt(hdr->frame_control) &&
383 !ieee80211_is_beacon(hdr->frame_control))
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200384 txdesc->u.ht.txop = TXOP_BACKOFF;
385 else if (!(tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT))
386 txdesc->u.ht.txop = TXOP_SIFS;
387 else
388 txdesc->u.ht.txop = TXOP_HTTXOP;
389}
390
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200391static void rt2x00queue_create_tx_descriptor(struct rt2x00_dev *rt2x00dev,
392 struct sk_buff *skb,
Thomas Huehn36323f82012-07-23 21:33:42 +0200393 struct txentry_desc *txdesc,
394 struct ieee80211_sta *sta)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200395{
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200396 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
397 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Helmut Schaa55b585e2011-03-03 19:43:49 +0100398 struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
399 struct ieee80211_rate *rate;
400 const struct rt2x00_rate *hwrate = NULL;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200401
402 memset(txdesc, 0, sizeof(*txdesc));
403
404 /*
Gertjan van Wingerdedf624ca2010-05-03 22:43:05 +0200405 * Header and frame information.
Ivo van Doorn9f166172009-04-26 16:08:50 +0200406 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200407 txdesc->length = skb->len;
408 txdesc->header_length = ieee80211_get_hdrlen_from_skb(skb);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200409
410 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200411 * Check whether this frame is to be acked.
412 */
Johannes Berge039fa42008-05-15 12:55:29 +0200413 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200414 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
415
416 /*
417 * Check if this is a RTS/CTS frame
418 */
Ivo van Doornac104462008-06-16 19:54:57 +0200419 if (ieee80211_is_rts(hdr->frame_control) ||
420 ieee80211_is_cts(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200421 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
Ivo van Doornac104462008-06-16 19:54:57 +0200422 if (ieee80211_is_rts(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200423 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200424 else
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200425 __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200426 if (tx_info->control.rts_cts_rate_idx >= 0)
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200427 rate =
Johannes Berge039fa42008-05-15 12:55:29 +0200428 ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200429 }
430
431 /*
432 * Determine retry information.
433 */
Johannes Berge6a98542008-10-21 12:40:02 +0200434 txdesc->retry_limit = tx_info->control.rates[0].count - 1;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100435 if (txdesc->retry_limit >= rt2x00dev->long_retry)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200436 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
437
438 /*
439 * Check if more fragments are pending
440 */
Helmut Schaa2606e422010-06-14 22:10:09 +0200441 if (ieee80211_has_morefrags(hdr->frame_control)) {
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200442 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
443 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
444 }
445
446 /*
Helmut Schaa2606e422010-06-14 22:10:09 +0200447 * Check if more frames (!= fragments) are pending
448 */
449 if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
450 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
451
452 /*
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200453 * Beacons and probe responses require the tsf timestamp
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100454 * to be inserted into the frame.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200455 */
Helmut Schaa1bce85cf2011-02-20 13:56:07 +0100456 if (ieee80211_is_beacon(hdr->frame_control) ||
457 ieee80211_is_probe_resp(hdr->frame_control))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200458 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
459
Ivo van Doorn7b409822008-12-20 10:58:33 +0100460 if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
Helmut Schaa25177942011-03-03 19:43:25 +0100461 !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200462 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200463
Ivo van Doorn076f9582008-12-20 10:59:02 +0100464 /*
465 * Determine rate modulation.
466 */
Helmut Schaa55b585e2011-03-03 19:43:49 +0100467 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
468 txdesc->rate_mode = RATE_MODE_HT_GREENFIELD;
469 else if (txrate->flags & IEEE80211_TX_RC_MCS)
470 txdesc->rate_mode = RATE_MODE_HT_MIX;
471 else {
472 rate = ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
473 hwrate = rt2x00_get_rate(rate->hw_value);
474 if (hwrate->flags & DEV_RATE_OFDM)
475 txdesc->rate_mode = RATE_MODE_OFDM;
476 else
477 txdesc->rate_mode = RATE_MODE_CCK;
478 }
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200479
Ivo van Doorn7b409822008-12-20 10:58:33 +0100480 /*
481 * Apply TX descriptor handling by components
482 */
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200483 rt2x00crypto_create_tx_descriptor(rt2x00dev, skb, txdesc);
484 rt2x00queue_create_tx_descriptor_seq(rt2x00dev, skb, txdesc);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100485
Fred Choub9d305c2014-12-26 16:19:18 +0800486 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_HT_TX_DESC))
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200487 rt2x00queue_create_tx_descriptor_ht(rt2x00dev, skb, txdesc,
Thomas Huehn36323f82012-07-23 21:33:42 +0200488 sta, hwrate);
Helmut Schaa26a1d072011-03-03 19:42:35 +0100489 else
Gertjan van Wingerde77b56212011-07-06 22:57:00 +0200490 rt2x00queue_create_tx_descriptor_plcp(rt2x00dev, skb, txdesc,
491 hwrate);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200492}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200493
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200494static int rt2x00queue_write_tx_data(struct queue_entry *entry,
495 struct txentry_desc *txdesc)
496{
497 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
498
499 /*
500 * This should not happen, we already checked the entry
501 * was ours. When the hardware disagrees there has been
502 * a queue corruption!
503 */
504 if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
505 rt2x00dev->ops->lib->get_entry_state(entry))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700506 rt2x00_err(rt2x00dev,
507 "Corrupt queue %d, accessing entry which is not ours\n"
508 "Please file bug report to %s\n",
509 entry->queue->qid, DRV_PROJECT);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200510 return -EINVAL;
511 }
512
513 /*
514 * Add the requested extra tx headroom in front of the skb.
515 */
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200516 skb_push(entry->skb, rt2x00dev->extra_tx_headroom);
517 memset(entry->skb->data, 0, rt2x00dev->extra_tx_headroom);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200518
519 /*
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200520 * Call the driver's write_tx_data function, if it exists.
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200521 */
Gertjan van Wingerde76dd5dd2010-06-29 21:42:23 +0200522 if (rt2x00dev->ops->lib->write_tx_data)
523 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200524
525 /*
526 * Map the skb to DMA.
527 */
Fred Choub9d305c2014-12-26 16:19:18 +0800528 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DMA) &&
Stanislaw Gruszka4ea545d2013-02-13 14:27:05 +0100529 rt2x00queue_map_txskb(entry))
530 return -ENOMEM;
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200531
532 return 0;
533}
534
Ivo van Doornbd88a782008-07-09 15:12:44 +0200535static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
536 struct txentry_desc *txdesc)
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200537{
Ivo van Doornb8697672008-06-06 22:53:14 +0200538 struct data_queue *queue = entry->queue;
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200539
Ivo van Doorn93331452010-08-23 19:53:39 +0200540 queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200541
542 /*
543 * All processing on the frame has been completed, this means
544 * it is now ready to be dumped to userspace through debugfs.
545 */
Stanislaw Gruszka2ceb8132017-02-08 13:51:30 +0100546 rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry);
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200547}
548
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100549static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200550 struct txentry_desc *txdesc)
551{
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200552 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200553 * Check if we need to kick the queue, there are however a few rules
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200554 * 1) Don't kick unless this is the last in frame in a burst.
Ivo van Doornb8697672008-06-06 22:53:14 +0200555 * When the burst flag is set, this frame is always followed
556 * by another frame which in some way are related to eachother.
557 * This is true for fragments, RTS or CTS-to-self frames.
Gertjan van Wingerde6295d812010-05-09 21:24:22 +0200558 * 2) Rule 1 can be broken when the available entries
Ivo van Doornb8697672008-06-06 22:53:14 +0200559 * in the queue are less then a certain threshold.
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200560 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200561 if (rt2x00queue_threshold(queue) ||
562 !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
Ivo van Doorndbba3062010-12-13 12:34:54 +0100563 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200564}
Ivo van Doorn7050ec82008-05-10 13:46:13 +0200565
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100566static void rt2x00queue_bar_check(struct queue_entry *entry)
567{
568 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
569 struct ieee80211_bar *bar = (void *) (entry->skb->data +
Gabor Juhos5616a6e2013-06-06 09:36:19 +0200570 rt2x00dev->extra_tx_headroom);
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100571 struct rt2x00_bar_list_entry *bar_entry;
572
573 if (likely(!ieee80211_is_back_req(bar->frame_control)))
574 return;
575
576 bar_entry = kmalloc(sizeof(*bar_entry), GFP_ATOMIC);
577
578 /*
579 * If the alloc fails we still send the BAR out but just don't track
580 * it in our bar list. And as a result we will report it to mac80211
581 * back as failed.
582 */
583 if (!bar_entry)
584 return;
585
586 bar_entry->entry = entry;
587 bar_entry->block_acked = 0;
588
589 /*
590 * Copy the relevant parts of the 802.11 BAR into out check list
591 * such that we can use RCU for less-overhead in the RX path since
592 * sending BARs and processing the according BlockAck should be
593 * the exception.
594 */
595 memcpy(bar_entry->ra, bar->ra, sizeof(bar->ra));
596 memcpy(bar_entry->ta, bar->ta, sizeof(bar->ta));
597 bar_entry->control = bar->control;
598 bar_entry->start_seq_num = bar->start_seq_num;
599
600 /*
601 * Insert BAR into our BAR check list.
602 */
603 spin_lock_bh(&rt2x00dev->bar_list_lock);
604 list_add_tail_rcu(&bar_entry->list, &rt2x00dev->bar_list);
605 spin_unlock_bh(&rt2x00dev->bar_list_lock);
606}
607
Johannes Berg7351c6b2009-11-19 01:08:30 +0100608int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
Stanislaw Gruszka3d8bfe12013-10-31 11:23:57 +0100609 struct ieee80211_sta *sta, bool local)
Ivo van Doorn6db37862008-06-06 22:50:28 +0200610{
Johannes Berge6a98542008-10-21 12:40:02 +0200611 struct ieee80211_tx_info *tx_info;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200612 struct queue_entry *entry;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200613 struct txentry_desc txdesc;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200614 struct skb_frame_desc *skbdesc;
Johannes Berge6a98542008-10-21 12:40:02 +0200615 u8 rate_idx, rate_flags;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200616 int ret = 0;
617
Ivo van Doorn6db37862008-06-06 22:50:28 +0200618 /*
619 * Copy all TX descriptor information into txdesc,
620 * after that we are free to use the skb->cb array
621 * for our information.
622 */
Stanislaw Gruszka3d8bfe12013-10-31 11:23:57 +0100623 rt2x00queue_create_tx_descriptor(queue->rt2x00dev, skb, &txdesc, sta);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200624
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200625 /*
Johannes Berge6a98542008-10-21 12:40:02 +0200626 * All information is retrieved from the skb->cb array,
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200627 * now we should claim ownership of the driver part of that
Johannes Berge6a98542008-10-21 12:40:02 +0200628 * array, preserving the bitrate index and flags.
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200629 */
Johannes Berge6a98542008-10-21 12:40:02 +0200630 tx_info = IEEE80211_SKB_CB(skb);
631 rate_idx = tx_info->control.rates[0].idx;
632 rate_flags = tx_info->control.rates[0].flags;
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100633 skbdesc = get_skb_frame_desc(skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200634 memset(skbdesc, 0, sizeof(*skbdesc));
Johannes Berge6a98542008-10-21 12:40:02 +0200635 skbdesc->tx_rate_idx = rate_idx;
636 skbdesc->tx_rate_flags = rate_flags;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200637
Johannes Berg7351c6b2009-11-19 01:08:30 +0100638 if (local)
639 skbdesc->flags |= SKBDESC_NOT_MAC80211;
640
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200641 /*
642 * When hardware encryption is supported, and this frame
643 * is to be encrypted, we should strip the IV/EIV data from
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800644 * the frame so we can provide it to the driver separately.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200645 */
646 if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
Ivo van Doorndddfb472008-12-02 18:20:42 +0100647 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
Fred Choub9d305c2014-12-26 16:19:18 +0800648 if (rt2x00_has_cap_flag(queue->rt2x00dev, REQUIRE_COPY_IV))
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200649 rt2x00crypto_tx_copy_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100650 else
Ivo van Doorn9eb4e212009-04-26 16:08:30 +0200651 rt2x00crypto_tx_remove_iv(skb, &txdesc);
Ivo van Doorndddfb472008-12-02 18:20:42 +0100652 }
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200653
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200654 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300655 * When DMA allocation is required we should guarantee to the
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200656 * driver that the DMA is aligned to a 4-byte boundary.
Ivo van Doorn93354cb2009-08-08 23:53:47 +0200657 * However some drivers require L2 padding to pad the payload
658 * rather then the header. This could be a requirement for
659 * PCI and USB devices, while header alignment only is valid
660 * for PCI devices.
661 */
Fred Choub9d305c2014-12-26 16:19:18 +0800662 if (rt2x00_has_cap_flag(queue->rt2x00dev, REQUIRE_L2PAD))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200663 rt2x00queue_insert_l2pad(skb, txdesc.header_length);
Fred Choub9d305c2014-12-26 16:19:18 +0800664 else if (rt2x00_has_cap_flag(queue->rt2x00dev, REQUIRE_DMA))
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200665 rt2x00queue_align_frame(skb);
666
Stanislaw Gruszka3780d032012-03-09 12:39:54 +0100667 /*
668 * That function must be called with bh disabled.
669 */
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200670 spin_lock(&queue->tx_lock);
671
672 if (unlikely(rt2x00queue_full(queue))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700673 rt2x00_err(queue->rt2x00dev, "Dropping frame due to full tx queue %d\n",
674 queue->qid);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200675 ret = -ENOBUFS;
676 goto out;
677 }
678
679 entry = rt2x00queue_get_entry(queue, Q_INDEX);
680
681 if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
682 &entry->flags))) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700683 rt2x00_err(queue->rt2x00dev,
684 "Arrived at non-free entry in the non-full queue %d\n"
685 "Please file bug report to %s\n",
686 queue->qid, DRV_PROJECT);
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200687 ret = -EINVAL;
688 goto out;
689 }
690
Gertjan van Wingerde128f8f72011-07-06 22:57:37 +0200691 entry->skb = skb;
Ivo van Doorn9f166172009-04-26 16:08:50 +0200692
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200693 /*
694 * It could be possible that the queue was corrupted and this
Ivo van Doorn0e3de992008-11-12 00:01:37 +0100695 * call failed. Since we always return NETDEV_TX_OK to mac80211,
696 * this frame will simply be dropped.
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200697 */
Gertjan van Wingerde78eea112010-06-29 21:41:05 +0200698 if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200699 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200700 entry->skb = NULL;
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200701 ret = -EIO;
702 goto out;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200703 }
704
Helmut Schaa84e9e8ebd2013-01-17 17:34:32 +0100705 /*
706 * Put BlockAckReqs into our check list for driver BA processing.
707 */
708 rt2x00queue_bar_check(entry);
709
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200710 set_bit(ENTRY_DATA_PENDING, &entry->flags);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200711
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200712 rt2x00queue_index_inc(entry, Q_INDEX);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200713 rt2x00queue_write_tx_descriptor(entry, &txdesc);
Ivo van Doorn8be4eed2010-11-06 15:48:23 +0100714 rt2x00queue_kick_tx_queue(queue, &txdesc);
Ivo van Doorn6db37862008-06-06 22:50:28 +0200715
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200716out:
Stanislaw Gruszka3d8f1622017-12-19 12:33:55 +0100717 /*
718 * Pausing queue has to be serialized with rt2x00lib_txdone(), so we
719 * do this under queue->tx_lock. Bottom halve was already disabled
720 * before ieee80211_xmit() call.
721 */
722 if (rt2x00queue_threshold(queue))
723 rt2x00queue_pause_queue(queue);
724
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +0200725 spin_unlock(&queue->tx_lock);
726 return ret;
Ivo van Doorn6db37862008-06-06 22:50:28 +0200727}
728
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100729int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
730 struct ieee80211_vif *vif)
731{
732 struct rt2x00_intf *intf = vif_to_intf(vif);
733
734 if (unlikely(!intf->beacon))
735 return -ENOBUFS;
736
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100737 /*
738 * Clean up the beacon skb.
739 */
740 rt2x00queue_free_skb(intf->beacon);
741
742 /*
743 * Clear beacon (single bssid devices don't need to clear the beacon
744 * since the beacon queue will get stopped anyway).
745 */
746 if (rt2x00dev->ops->lib->clear_beacon)
747 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
748
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100749 return 0;
750}
751
Stanislaw Gruszka283dafa2014-06-05 13:52:23 +0200752int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
753 struct ieee80211_vif *vif)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200754{
755 struct rt2x00_intf *intf = vif_to_intf(vif);
756 struct skb_frame_desc *skbdesc;
757 struct txentry_desc txdesc;
Ivo van Doornbd88a782008-07-09 15:12:44 +0200758
759 if (unlikely(!intf->beacon))
760 return -ENOBUFS;
761
Igor Perminov17512dc2009-08-08 23:55:18 +0200762 /*
763 * Clean up the beacon skb.
764 */
Ivo van Doornfa695602010-10-11 15:37:25 +0200765 rt2x00queue_free_skb(intf->beacon);
Igor Perminov17512dc2009-08-08 23:55:18 +0200766
Ivo van Doornbd88a782008-07-09 15:12:44 +0200767 intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
Helmut Schaa8414ff02011-01-30 13:16:28 +0100768 if (!intf->beacon->skb)
Ivo van Doornbd88a782008-07-09 15:12:44 +0200769 return -ENOMEM;
770
771 /*
772 * Copy all TX descriptor information into txdesc,
773 * after that we are free to use the skb->cb array
774 * for our information.
775 */
Thomas Huehn36323f82012-07-23 21:33:42 +0200776 rt2x00queue_create_tx_descriptor(rt2x00dev, intf->beacon->skb, &txdesc, NULL);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200777
778 /*
Ivo van Doornbd88a782008-07-09 15:12:44 +0200779 * Fill in skb descriptor
780 */
781 skbdesc = get_skb_frame_desc(intf->beacon->skb);
782 memset(skbdesc, 0, sizeof(*skbdesc));
Ivo van Doornbd88a782008-07-09 15:12:44 +0200783
784 /*
Helmut Schaa69cf36a2011-01-30 13:16:03 +0100785 * Send beacon to hardware.
Ivo van Doornbd88a782008-07-09 15:12:44 +0200786 */
Gertjan van Wingerdef224f4e2010-05-08 23:40:25 +0200787 rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
Ivo van Doornbd88a782008-07-09 15:12:44 +0200788
Helmut Schaa8414ff02011-01-30 13:16:28 +0100789 return 0;
790
791}
792
Helmut Schaa10e11562011-04-18 15:27:43 +0200793bool rt2x00queue_for_each_entry(struct data_queue *queue,
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200794 enum queue_index start,
795 enum queue_index end,
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100796 void *data,
797 bool (*fn)(struct queue_entry *entry,
798 void *data))
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200799{
800 unsigned long irqflags;
801 unsigned int index_start;
802 unsigned int index_end;
803 unsigned int i;
804
805 if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700806 rt2x00_err(queue->rt2x00dev,
807 "Entry requested from invalid index range (%d - %d)\n",
808 start, end);
Helmut Schaa10e11562011-04-18 15:27:43 +0200809 return true;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200810 }
811
812 /*
813 * Only protect the range we are going to loop over,
814 * if during our loop a extra entry is set to pending
815 * it should not be kicked during this run, since it
816 * is part of another TX operation.
817 */
Ivo van Doorn813f0332010-11-06 15:48:05 +0100818 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200819 index_start = queue->index[start];
820 index_end = queue->index[end];
Ivo van Doorn813f0332010-11-06 15:48:05 +0100821 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200822
823 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300824 * Start from the TX done pointer, this guarantees that we will
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200825 * send out all frames in the correct order.
826 */
827 if (index_start < index_end) {
Helmut Schaa10e11562011-04-18 15:27:43 +0200828 for (i = index_start; i < index_end; 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 } else {
Helmut Schaa10e11562011-04-18 15:27:43 +0200833 for (i = index_start; i < queue->limit; 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 for (i = 0; i < index_end; i++) {
Helmut Schaa1dd0dbb2013-03-15 09:57:56 +0100839 if (fn(&queue->entries[i], data))
Helmut Schaa10e11562011-04-18 15:27:43 +0200840 return true;
841 }
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200842 }
Helmut Schaa10e11562011-04-18 15:27:43 +0200843
844 return false;
Ivo van Doorn5eb7efe2010-08-23 19:54:21 +0200845}
846EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
847
Ivo van Doorn181d6902008-02-05 16:42:23 -0500848struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
849 enum queue_index index)
850{
851 struct queue_entry *entry;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100852 unsigned long irqflags;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500853
854 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700855 rt2x00_err(queue->rt2x00dev, "Entry requested from invalid index type (%d)\n",
856 index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500857 return NULL;
858 }
859
Ivo van Doorn813f0332010-11-06 15:48:05 +0100860 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500861
862 entry = &queue->entries[queue->index[index]];
863
Ivo van Doorn813f0332010-11-06 15:48:05 +0100864 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500865
866 return entry;
867}
868EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
869
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200870void rt2x00queue_index_inc(struct queue_entry *entry, enum queue_index index)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500871{
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200872 struct data_queue *queue = entry->queue;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +0100873 unsigned long irqflags;
874
Ivo van Doorn181d6902008-02-05 16:42:23 -0500875 if (unlikely(index >= Q_INDEX_MAX)) {
Joe Perchesec9c4982013-04-19 08:33:40 -0700876 rt2x00_err(queue->rt2x00dev,
877 "Index change on invalid index type (%d)\n", index);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500878 return;
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 queue->index[index]++;
884 if (queue->index[index] >= queue->limit)
885 queue->index[index] = 0;
886
Johannes Stezenbach75256f02011-04-18 15:29:38 +0200887 entry->last_action = jiffies;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200888
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100889 if (index == Q_INDEX) {
890 queue->length++;
891 } else if (index == Q_INDEX_DONE) {
892 queue->length--;
John Daiker55887512008-10-17 12:16:17 -0700893 queue->count++;
Ivo van Doorn10b6b802008-02-03 15:55:21 +0100894 }
Ivo van Doorn181d6902008-02-05 16:42:23 -0500895
Ivo van Doorn813f0332010-11-06 15:48:05 +0100896 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500897}
Ivo van Doorn181d6902008-02-05 16:42:23 -0500898
Jingoo Han6cdfc1d2013-08-06 17:36:03 +0900899static void rt2x00queue_pause_queue_nocheck(struct data_queue *queue)
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100900{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100901 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100902 case QID_AC_VO:
903 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100904 case QID_AC_BE:
905 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100906 /*
907 * For TX queues, we have to disable the queue
908 * inside mac80211.
909 */
910 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
911 break;
912 default:
913 break;
914 }
915}
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +0200916void rt2x00queue_pause_queue(struct data_queue *queue)
917{
918 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
919 !test_bit(QUEUE_STARTED, &queue->flags) ||
920 test_and_set_bit(QUEUE_PAUSED, &queue->flags))
921 return;
922
923 rt2x00queue_pause_queue_nocheck(queue);
924}
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100925EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
926
927void rt2x00queue_unpause_queue(struct data_queue *queue)
928{
929 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
930 !test_bit(QUEUE_STARTED, &queue->flags) ||
931 !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
932 return;
933
934 switch (queue->qid) {
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100935 case QID_AC_VO:
936 case QID_AC_VI:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100937 case QID_AC_BE:
938 case QID_AC_BK:
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100939 /*
940 * For TX queues, we have to enable the queue
941 * inside mac80211.
942 */
943 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
944 break;
Ivo van Doorn5be65602010-12-13 12:35:40 +0100945 case QID_RX:
946 /*
947 * For RX we need to kick the queue now in order to
948 * receive frames.
949 */
950 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100951 default:
952 break;
953 }
954}
955EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
956
957void rt2x00queue_start_queue(struct data_queue *queue)
958{
959 mutex_lock(&queue->status_lock);
960
961 if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
962 test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
963 mutex_unlock(&queue->status_lock);
964 return;
965 }
966
967 set_bit(QUEUE_PAUSED, &queue->flags);
968
969 queue->rt2x00dev->ops->lib->start_queue(queue);
970
971 rt2x00queue_unpause_queue(queue);
972
973 mutex_unlock(&queue->status_lock);
974}
975EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
976
977void rt2x00queue_stop_queue(struct data_queue *queue)
978{
979 mutex_lock(&queue->status_lock);
980
981 if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
982 mutex_unlock(&queue->status_lock);
983 return;
984 }
985
Stanislaw Gruszkae2288b62013-07-28 13:17:22 +0200986 rt2x00queue_pause_queue_nocheck(queue);
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100987
988 queue->rt2x00dev->ops->lib->stop_queue(queue);
989
990 mutex_unlock(&queue->status_lock);
991}
992EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
993
Ivo van Doorn5be65602010-12-13 12:35:40 +0100994void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
995{
Ivo van Doorn5be65602010-12-13 12:35:40 +0100996 bool tx_queue =
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100997 (queue->qid == QID_AC_VO) ||
Ivo van Doorn5be65602010-12-13 12:35:40 +0100998 (queue->qid == QID_AC_VI) ||
Ivo van Doornf615e9a2010-12-13 12:36:38 +0100999 (queue->qid == QID_AC_BE) ||
1000 (queue->qid == QID_AC_BK);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001001
Ivo van Doorn5be65602010-12-13 12:35:40 +01001002
1003 /*
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001004 * If we are not supposed to drop any pending
1005 * frames, this means we must force a start (=kick)
1006 * to the queue to make sure the hardware will
1007 * start transmitting.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001008 */
Stanislaw Gruszkafdbdd252013-10-05 18:15:33 +02001009 if (!drop && tx_queue)
1010 queue->rt2x00dev->ops->lib->kick_queue(queue);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001011
1012 /*
Ivo van Doorn152a5992011-04-18 15:31:02 +02001013 * Check if driver supports flushing, if that is the case we can
1014 * defer the flushing to the driver. Otherwise we must use the
1015 * alternative which just waits for the queue to become empty.
Ivo van Doorn5be65602010-12-13 12:35:40 +01001016 */
Ivo van Doorn152a5992011-04-18 15:31:02 +02001017 if (likely(queue->rt2x00dev->ops->lib->flush_queue))
1018 queue->rt2x00dev->ops->lib->flush_queue(queue, drop);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001019
1020 /*
1021 * The queue flush has failed...
1022 */
1023 if (unlikely(!rt2x00queue_empty(queue)))
Joe Perchesec9c4982013-04-19 08:33:40 -07001024 rt2x00_warn(queue->rt2x00dev, "Queue %d failed to flush\n",
1025 queue->qid);
Ivo van Doorn5be65602010-12-13 12:35:40 +01001026}
1027EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
1028
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001029void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
1030{
1031 struct data_queue *queue;
1032
1033 /*
1034 * rt2x00queue_start_queue will call ieee80211_wake_queue
1035 * for each queue after is has been properly initialized.
1036 */
1037 tx_queue_for_each(rt2x00dev, queue)
1038 rt2x00queue_start_queue(queue);
1039
1040 rt2x00queue_start_queue(rt2x00dev->rx);
1041}
1042EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
1043
1044void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
1045{
1046 struct data_queue *queue;
1047
1048 /*
1049 * rt2x00queue_stop_queue will call ieee80211_stop_queue
1050 * as well, but we are completely shutting doing everything
1051 * now, so it is much safer to stop all TX queues at once,
1052 * and use rt2x00queue_stop_queue for cleaning up.
1053 */
1054 ieee80211_stop_queues(rt2x00dev->hw);
1055
1056 tx_queue_for_each(rt2x00dev, queue)
1057 rt2x00queue_stop_queue(queue);
1058
1059 rt2x00queue_stop_queue(rt2x00dev->rx);
1060}
1061EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
1062
Ivo van Doorn5be65602010-12-13 12:35:40 +01001063void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
1064{
1065 struct data_queue *queue;
1066
1067 tx_queue_for_each(rt2x00dev, queue)
1068 rt2x00queue_flush_queue(queue, drop);
1069
1070 rt2x00queue_flush_queue(rt2x00dev->rx, drop);
1071}
1072EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
1073
Ivo van Doorn181d6902008-02-05 16:42:23 -05001074static void rt2x00queue_reset(struct data_queue *queue)
1075{
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001076 unsigned long irqflags;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001077 unsigned int i;
Ivo van Doorn5f46c4d2008-03-09 22:44:30 +01001078
Ivo van Doorn813f0332010-11-06 15:48:05 +01001079 spin_lock_irqsave(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001080
1081 queue->count = 0;
1082 queue->length = 0;
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001083
Johannes Stezenbach75256f02011-04-18 15:29:38 +02001084 for (i = 0; i < Q_INDEX_MAX; i++)
Ivo van Doorn652a9dd2010-08-30 21:15:19 +02001085 queue->index[i] = 0;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001086
Ivo van Doorn813f0332010-11-06 15:48:05 +01001087 spin_unlock_irqrestore(&queue->index_lock, irqflags);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001088}
1089
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001090void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001091{
1092 struct data_queue *queue;
1093 unsigned int i;
1094
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001095 queue_for_each(rt2x00dev, queue) {
Ivo van Doorn181d6902008-02-05 16:42:23 -05001096 rt2x00queue_reset(queue);
1097
Ivo van Doorn64e7d722010-12-13 12:36:00 +01001098 for (i = 0; i < queue->limit; i++)
Ivo van Doorn798b7ad2008-11-08 15:25:33 +01001099 rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001100 }
1101}
1102
Gabor Juhos15d6c072013-06-04 13:40:39 +02001103static int rt2x00queue_alloc_entries(struct data_queue *queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001104{
1105 struct queue_entry *entries;
1106 unsigned int entry_size;
1107 unsigned int i;
1108
1109 rt2x00queue_reset(queue);
1110
Ivo van Doorn181d6902008-02-05 16:42:23 -05001111 /*
1112 * Allocate all queue entries.
1113 */
Gabor Juhos568f7a42013-06-04 13:40:38 +02001114 entry_size = sizeof(*entries) + queue->priv_size;
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001115 entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001116 if (!entries)
1117 return -ENOMEM;
1118
1119#define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
Mark Einonf8bfbc32010-11-06 15:47:25 +01001120 (((char *)(__base)) + ((__limit) * (__esize)) + \
1121 ((__index) * (__psize)))
Ivo van Doorn181d6902008-02-05 16:42:23 -05001122
1123 for (i = 0; i < queue->limit; i++) {
1124 entries[i].flags = 0;
1125 entries[i].queue = queue;
1126 entries[i].skb = NULL;
1127 entries[i].entry_idx = i;
1128 entries[i].priv_data =
1129 QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
Gabor Juhos568f7a42013-06-04 13:40:38 +02001130 sizeof(*entries), queue->priv_size);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001131 }
1132
1133#undef QUEUE_ENTRY_PRIV_OFFSET
1134
1135 queue->entries = entries;
1136
1137 return 0;
1138}
1139
Ivo van Doornfa695602010-10-11 15:37:25 +02001140static void rt2x00queue_free_skbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001141{
1142 unsigned int i;
1143
1144 if (!queue->entries)
1145 return;
1146
1147 for (i = 0; i < queue->limit; i++) {
Ivo van Doornfa695602010-10-11 15:37:25 +02001148 rt2x00queue_free_skb(&queue->entries[i]);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001149 }
1150}
1151
Ivo van Doornfa695602010-10-11 15:37:25 +02001152static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001153{
1154 unsigned int i;
1155 struct sk_buff *skb;
1156
1157 for (i = 0; i < queue->limit; i++) {
Helmut Schaa88211022012-04-19 13:24:10 +02001158 skb = rt2x00queue_alloc_rxskb(&queue->entries[i], GFP_KERNEL);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001159 if (!skb)
Ivo van Doorn61243d82008-06-20 22:10:53 +02001160 return -ENOMEM;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001161 queue->entries[i].skb = skb;
1162 }
1163
1164 return 0;
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001165}
1166
Ivo van Doorn181d6902008-02-05 16:42:23 -05001167int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1168{
1169 struct data_queue *queue;
1170 int status;
1171
Gabor Juhos15d6c072013-06-04 13:40:39 +02001172 status = rt2x00queue_alloc_entries(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001173 if (status)
1174 goto exit;
1175
1176 tx_queue_for_each(rt2x00dev, queue) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001177 status = rt2x00queue_alloc_entries(queue);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001178 if (status)
1179 goto exit;
1180 }
1181
Gabor Juhos15d6c072013-06-04 13:40:39 +02001182 status = rt2x00queue_alloc_entries(rt2x00dev->bcn);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001183 if (status)
1184 goto exit;
1185
Fred Choub9d305c2014-12-26 16:19:18 +08001186 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE)) {
Gabor Juhos15d6c072013-06-04 13:40:39 +02001187 status = rt2x00queue_alloc_entries(rt2x00dev->atim);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001188 if (status)
1189 goto exit;
1190 }
Ivo van Doorn181d6902008-02-05 16:42:23 -05001191
Ivo van Doornfa695602010-10-11 15:37:25 +02001192 status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001193 if (status)
1194 goto exit;
1195
1196 return 0;
1197
1198exit:
Joe Perchesec9c4982013-04-19 08:33:40 -07001199 rt2x00_err(rt2x00dev, "Queue entries allocation failed\n");
Ivo van Doorn181d6902008-02-05 16:42:23 -05001200
1201 rt2x00queue_uninitialize(rt2x00dev);
1202
1203 return status;
1204}
1205
1206void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1207{
1208 struct data_queue *queue;
1209
Ivo van Doornfa695602010-10-11 15:37:25 +02001210 rt2x00queue_free_skbs(rt2x00dev->rx);
Gertjan van Wingerde30caa6e2008-06-16 19:56:08 +02001211
Ivo van Doorn181d6902008-02-05 16:42:23 -05001212 queue_for_each(rt2x00dev, queue) {
1213 kfree(queue->entries);
1214 queue->entries = NULL;
1215 }
1216}
1217
Ivo van Doorn8f539272008-02-10 22:51:41 +01001218static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1219 struct data_queue *queue, enum data_queue_qid qid)
1220{
Ivo van Doorn0b7fde52010-12-13 12:35:17 +01001221 mutex_init(&queue->status_lock);
Gertjan van Wingerde77a861c2011-07-06 22:56:24 +02001222 spin_lock_init(&queue->tx_lock);
Ivo van Doorn813f0332010-11-06 15:48:05 +01001223 spin_lock_init(&queue->index_lock);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001224
1225 queue->rt2x00dev = rt2x00dev;
1226 queue->qid = qid;
Ivo van Doorn2af0a572008-08-29 21:05:45 +02001227 queue->txop = 0;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001228 queue->aifs = 2;
1229 queue->cw_min = 5;
1230 queue->cw_max = 10;
Gabor Juhos10af87c2013-06-03 23:39:53 +02001231
Gabor Juhos705802b2013-06-04 13:40:50 +02001232 rt2x00dev->ops->queue_init(queue);
Gabor Juhos04453e92013-06-04 13:40:41 +02001233
1234 queue->threshold = DIV_ROUND_UP(queue->limit, 10);
Ivo van Doorn8f539272008-02-10 22:51:41 +01001235}
1236
Ivo van Doorn181d6902008-02-05 16:42:23 -05001237int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1238{
1239 struct data_queue *queue;
1240 enum data_queue_qid qid;
1241 unsigned int req_atim =
Fred Choub9d305c2014-12-26 16:19:18 +08001242 rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001243
1244 /*
1245 * We need the following queues:
1246 * RX: 1
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001247 * TX: ops->tx_queues
Ivo van Doorn181d6902008-02-05 16:42:23 -05001248 * Beacon: 1
1249 * Atim: 1 (if required)
1250 */
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001251 rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001252
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001253 queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
Markus Elfringcd7c0cd2017-12-29 22:11:42 +01001254 if (!queue)
Ivo van Doorn181d6902008-02-05 16:42:23 -05001255 return -ENOMEM;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001256
1257 /*
1258 * Initialize pointers
1259 */
1260 rt2x00dev->rx = queue;
1261 rt2x00dev->tx = &queue[1];
Gertjan van Wingerde61448f82008-05-10 13:43:33 +02001262 rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001263 rt2x00dev->atim = req_atim ? &queue[2 + rt2x00dev->ops->tx_queues] : NULL;
Ivo van Doorn181d6902008-02-05 16:42:23 -05001264
1265 /*
1266 * Initialize queue parameters.
1267 * RX: qid = QID_RX
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001268 * TX: qid = QID_AC_VO + index
Ivo van Doorn181d6902008-02-05 16:42:23 -05001269 * TX: cw_min: 2^5 = 32.
1270 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn565a0192008-06-03 20:29:05 +02001271 * BCN: qid = QID_BEACON
1272 * ATIM: qid = QID_ATIM
Ivo van Doorn181d6902008-02-05 16:42:23 -05001273 */
Ivo van Doorn8f539272008-02-10 22:51:41 +01001274 rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1275
Ivo van Doornf615e9a2010-12-13 12:36:38 +01001276 qid = QID_AC_VO;
Ivo van Doorn8f539272008-02-10 22:51:41 +01001277 tx_queue_for_each(rt2x00dev, queue)
1278 rt2x00queue_init(rt2x00dev, queue, qid++);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001279
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001280 rt2x00queue_init(rt2x00dev, rt2x00dev->bcn, QID_BEACON);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001281 if (req_atim)
Gertjan van Wingerdee74df4a2011-03-03 19:46:09 +01001282 rt2x00queue_init(rt2x00dev, rt2x00dev->atim, QID_ATIM);
Ivo van Doorn181d6902008-02-05 16:42:23 -05001283
1284 return 0;
1285}
1286
1287void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1288{
1289 kfree(rt2x00dev->rx);
1290 rt2x00dev->rx = NULL;
1291 rt2x00dev->tx = NULL;
1292 rt2x00dev->bcn = NULL;
1293}