]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/rt2x00/rt2x00queue.c
rt2x00: Move TX descriptor field "ifs" into plcp substruct
[~andy/linux] / drivers / net / wireless / rt2x00 / rt2x00queue.c
1 /*
2         Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3         Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
4         Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
5         <http://rt2x00.serialmonkey.com>
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the
19         Free Software Foundation, Inc.,
20         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 /*
24         Module: rt2x00lib
25         Abstract: rt2x00 queue specific routines.
26  */
27
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/dma-mapping.h>
32
33 #include "rt2x00.h"
34 #include "rt2x00lib.h"
35
36 struct sk_buff *rt2x00queue_alloc_rxskb(struct queue_entry *entry)
37 {
38         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
39         struct sk_buff *skb;
40         struct skb_frame_desc *skbdesc;
41         unsigned int frame_size;
42         unsigned int head_size = 0;
43         unsigned int tail_size = 0;
44
45         /*
46          * The frame size includes descriptor size, because the
47          * hardware directly receive the frame into the skbuffer.
48          */
49         frame_size = entry->queue->data_size + entry->queue->desc_size;
50
51         /*
52          * The payload should be aligned to a 4-byte boundary,
53          * this means we need at least 3 bytes for moving the frame
54          * into the correct offset.
55          */
56         head_size = 4;
57
58         /*
59          * For IV/EIV/ICV assembly we must make sure there is
60          * at least 8 bytes bytes available in headroom for IV/EIV
61          * and 8 bytes for ICV data as tailroon.
62          */
63         if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
64                 head_size += 8;
65                 tail_size += 8;
66         }
67
68         /*
69          * Allocate skbuffer.
70          */
71         skb = dev_alloc_skb(frame_size + head_size + tail_size);
72         if (!skb)
73                 return NULL;
74
75         /*
76          * Make sure we not have a frame with the requested bytes
77          * available in the head and tail.
78          */
79         skb_reserve(skb, head_size);
80         skb_put(skb, frame_size);
81
82         /*
83          * Populate skbdesc.
84          */
85         skbdesc = get_skb_frame_desc(skb);
86         memset(skbdesc, 0, sizeof(*skbdesc));
87         skbdesc->entry = entry;
88
89         if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) {
90                 skbdesc->skb_dma = dma_map_single(rt2x00dev->dev,
91                                                   skb->data,
92                                                   skb->len,
93                                                   DMA_FROM_DEVICE);
94                 skbdesc->flags |= SKBDESC_DMA_MAPPED_RX;
95         }
96
97         return skb;
98 }
99
100 void rt2x00queue_map_txskb(struct queue_entry *entry)
101 {
102         struct device *dev = entry->queue->rt2x00dev->dev;
103         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
104
105         skbdesc->skb_dma =
106             dma_map_single(dev, entry->skb->data, entry->skb->len, DMA_TO_DEVICE);
107         skbdesc->flags |= SKBDESC_DMA_MAPPED_TX;
108 }
109 EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb);
110
111 void rt2x00queue_unmap_skb(struct queue_entry *entry)
112 {
113         struct device *dev = entry->queue->rt2x00dev->dev;
114         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
115
116         if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) {
117                 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
118                                  DMA_FROM_DEVICE);
119                 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX;
120         } else if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) {
121                 dma_unmap_single(dev, skbdesc->skb_dma, entry->skb->len,
122                                  DMA_TO_DEVICE);
123                 skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX;
124         }
125 }
126 EXPORT_SYMBOL_GPL(rt2x00queue_unmap_skb);
127
128 void rt2x00queue_free_skb(struct queue_entry *entry)
129 {
130         if (!entry->skb)
131                 return;
132
133         rt2x00queue_unmap_skb(entry);
134         dev_kfree_skb_any(entry->skb);
135         entry->skb = NULL;
136 }
137
138 void rt2x00queue_align_frame(struct sk_buff *skb)
139 {
140         unsigned int frame_length = skb->len;
141         unsigned int align = ALIGN_SIZE(skb, 0);
142
143         if (!align)
144                 return;
145
146         skb_push(skb, align);
147         memmove(skb->data, skb->data + align, frame_length);
148         skb_trim(skb, frame_length);
149 }
150
151 void rt2x00queue_align_payload(struct sk_buff *skb, unsigned int header_length)
152 {
153         unsigned int frame_length = skb->len;
154         unsigned int align = ALIGN_SIZE(skb, header_length);
155
156         if (!align)
157                 return;
158
159         skb_push(skb, align);
160         memmove(skb->data, skb->data + align, frame_length);
161         skb_trim(skb, frame_length);
162 }
163
164 void rt2x00queue_insert_l2pad(struct sk_buff *skb, unsigned int header_length)
165 {
166         unsigned int payload_length = skb->len - header_length;
167         unsigned int header_align = ALIGN_SIZE(skb, 0);
168         unsigned int payload_align = ALIGN_SIZE(skb, header_length);
169         unsigned int l2pad = payload_length ? L2PAD_SIZE(header_length) : 0;
170
171         /*
172          * Adjust the header alignment if the payload needs to be moved more
173          * than the header.
174          */
175         if (payload_align > header_align)
176                 header_align += 4;
177
178         /* There is nothing to do if no alignment is needed */
179         if (!header_align)
180                 return;
181
182         /* Reserve the amount of space needed in front of the frame */
183         skb_push(skb, header_align);
184
185         /*
186          * Move the header.
187          */
188         memmove(skb->data, skb->data + header_align, header_length);
189
190         /* Move the payload, if present and if required */
191         if (payload_length && payload_align)
192                 memmove(skb->data + header_length + l2pad,
193                         skb->data + header_length + l2pad + payload_align,
194                         payload_length);
195
196         /* Trim the skb to the correct size */
197         skb_trim(skb, header_length + l2pad + payload_length);
198 }
199
200 void rt2x00queue_remove_l2pad(struct sk_buff *skb, unsigned int header_length)
201 {
202         /*
203          * L2 padding is only present if the skb contains more than just the
204          * IEEE 802.11 header.
205          */
206         unsigned int l2pad = (skb->len > header_length) ?
207                                 L2PAD_SIZE(header_length) : 0;
208
209         if (!l2pad)
210                 return;
211
212         memmove(skb->data + l2pad, skb->data, header_length);
213         skb_pull(skb, l2pad);
214 }
215
216 static void rt2x00queue_create_tx_descriptor_seq(struct queue_entry *entry,
217                                                  struct txentry_desc *txdesc)
218 {
219         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
220         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
221         struct rt2x00_intf *intf = vif_to_intf(tx_info->control.vif);
222         unsigned long irqflags;
223
224         if (!(tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
225                 return;
226
227         __set_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags);
228
229         if (!test_bit(DRIVER_REQUIRE_SW_SEQNO, &entry->queue->rt2x00dev->flags))
230                 return;
231
232         /*
233          * The hardware is not able to insert a sequence number. Assign a
234          * software generated one here.
235          *
236          * This is wrong because beacons are not getting sequence
237          * numbers assigned properly.
238          *
239          * A secondary problem exists for drivers that cannot toggle
240          * sequence counting per-frame, since those will override the
241          * sequence counter given by mac80211.
242          */
243         spin_lock_irqsave(&intf->seqlock, irqflags);
244
245         if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
246                 intf->seqno += 0x10;
247         hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
248         hdr->seq_ctrl |= cpu_to_le16(intf->seqno);
249
250         spin_unlock_irqrestore(&intf->seqlock, irqflags);
251
252 }
253
254 static void rt2x00queue_create_tx_descriptor_plcp(struct queue_entry *entry,
255                                                   struct txentry_desc *txdesc,
256                                                   const struct rt2x00_rate *hwrate)
257 {
258         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
259         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
260         struct ieee80211_tx_rate *txrate = &tx_info->control.rates[0];
261         unsigned int data_length;
262         unsigned int duration;
263         unsigned int residual;
264
265         /*
266          * Determine with what IFS priority this frame should be send.
267          * Set ifs to IFS_SIFS when the this is not the first fragment,
268          * or this fragment came after RTS/CTS.
269          */
270         if (test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags))
271                 txdesc->u.plcp.ifs = IFS_BACKOFF;
272         else
273                 txdesc->u.plcp.ifs = IFS_SIFS;
274
275         /* Data length + CRC + Crypto overhead (IV/EIV/ICV/MIC) */
276         data_length = entry->skb->len + 4;
277         data_length += rt2x00crypto_tx_overhead(rt2x00dev, entry->skb);
278
279         /*
280          * PLCP setup
281          * Length calculation depends on OFDM/CCK rate.
282          */
283         txdesc->u.plcp.signal = hwrate->plcp;
284         txdesc->u.plcp.service = 0x04;
285
286         if (hwrate->flags & DEV_RATE_OFDM) {
287                 txdesc->u.plcp.length_high = (data_length >> 6) & 0x3f;
288                 txdesc->u.plcp.length_low = data_length & 0x3f;
289         } else {
290                 /*
291                  * Convert length to microseconds.
292                  */
293                 residual = GET_DURATION_RES(data_length, hwrate->bitrate);
294                 duration = GET_DURATION(data_length, hwrate->bitrate);
295
296                 if (residual != 0) {
297                         duration++;
298
299                         /*
300                          * Check if we need to set the Length Extension
301                          */
302                         if (hwrate->bitrate == 110 && residual <= 30)
303                                 txdesc->u.plcp.service |= 0x80;
304                 }
305
306                 txdesc->u.plcp.length_high = (duration >> 8) & 0xff;
307                 txdesc->u.plcp.length_low = duration & 0xff;
308
309                 /*
310                  * When preamble is enabled we should set the
311                  * preamble bit for the signal.
312                  */
313                 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
314                         txdesc->u.plcp.signal |= 0x08;
315         }
316 }
317
318 static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry,
319                                              struct txentry_desc *txdesc)
320 {
321         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
322         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
323         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data;
324         struct ieee80211_rate *rate =
325             ieee80211_get_tx_rate(rt2x00dev->hw, tx_info);
326         const struct rt2x00_rate *hwrate;
327
328         memset(txdesc, 0, sizeof(*txdesc));
329
330         /*
331          * Header and frame information.
332          */
333         txdesc->length = entry->skb->len;
334         txdesc->header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
335
336         /*
337          * Check whether this frame is to be acked.
338          */
339         if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK))
340                 __set_bit(ENTRY_TXD_ACK, &txdesc->flags);
341
342         /*
343          * Check if this is a RTS/CTS frame
344          */
345         if (ieee80211_is_rts(hdr->frame_control) ||
346             ieee80211_is_cts(hdr->frame_control)) {
347                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
348                 if (ieee80211_is_rts(hdr->frame_control))
349                         __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags);
350                 else
351                         __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags);
352                 if (tx_info->control.rts_cts_rate_idx >= 0)
353                         rate =
354                             ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info);
355         }
356
357         /*
358          * Determine retry information.
359          */
360         txdesc->retry_limit = tx_info->control.rates[0].count - 1;
361         if (txdesc->retry_limit >= rt2x00dev->long_retry)
362                 __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags);
363
364         /*
365          * Check if more fragments are pending
366          */
367         if (ieee80211_has_morefrags(hdr->frame_control)) {
368                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
369                 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags);
370         }
371
372         /*
373          * Check if more frames (!= fragments) are pending
374          */
375         if (tx_info->flags & IEEE80211_TX_CTL_MORE_FRAMES)
376                 __set_bit(ENTRY_TXD_BURST, &txdesc->flags);
377
378         /*
379          * Beacons and probe responses require the tsf timestamp
380          * to be inserted into the frame.
381          */
382         if (ieee80211_is_beacon(hdr->frame_control) ||
383             ieee80211_is_probe_resp(hdr->frame_control))
384                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags);
385
386         if ((tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) &&
387             !test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags))
388                 __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags);
389
390         /*
391          * Determine rate modulation.
392          */
393         hwrate = rt2x00_get_rate(rate->hw_value);
394         txdesc->rate_mode = RATE_MODE_CCK;
395         if (hwrate->flags & DEV_RATE_OFDM)
396                 txdesc->rate_mode = RATE_MODE_OFDM;
397
398         /*
399          * Apply TX descriptor handling by components
400          */
401         rt2x00crypto_create_tx_descriptor(entry, txdesc);
402         rt2x00queue_create_tx_descriptor_seq(entry, txdesc);
403
404         if (test_bit(DRIVER_REQUIRE_HT_TX_DESC, &rt2x00dev->flags))
405                 rt2x00ht_create_tx_descriptor(entry, txdesc, hwrate);
406         else
407                 rt2x00queue_create_tx_descriptor_plcp(entry, txdesc, hwrate);
408 }
409
410 static int rt2x00queue_write_tx_data(struct queue_entry *entry,
411                                      struct txentry_desc *txdesc)
412 {
413         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
414
415         /*
416          * This should not happen, we already checked the entry
417          * was ours. When the hardware disagrees there has been
418          * a queue corruption!
419          */
420         if (unlikely(rt2x00dev->ops->lib->get_entry_state &&
421                      rt2x00dev->ops->lib->get_entry_state(entry))) {
422                 ERROR(rt2x00dev,
423                       "Corrupt queue %d, accessing entry which is not ours.\n"
424                       "Please file bug report to %s.\n",
425                       entry->queue->qid, DRV_PROJECT);
426                 return -EINVAL;
427         }
428
429         /*
430          * Add the requested extra tx headroom in front of the skb.
431          */
432         skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
433         memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
434
435         /*
436          * Call the driver's write_tx_data function, if it exists.
437          */
438         if (rt2x00dev->ops->lib->write_tx_data)
439                 rt2x00dev->ops->lib->write_tx_data(entry, txdesc);
440
441         /*
442          * Map the skb to DMA.
443          */
444         if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
445                 rt2x00queue_map_txskb(entry);
446
447         return 0;
448 }
449
450 static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry,
451                                             struct txentry_desc *txdesc)
452 {
453         struct data_queue *queue = entry->queue;
454
455         queue->rt2x00dev->ops->lib->write_tx_desc(entry, txdesc);
456
457         /*
458          * All processing on the frame has been completed, this means
459          * it is now ready to be dumped to userspace through debugfs.
460          */
461         rt2x00debug_dump_frame(queue->rt2x00dev, DUMP_FRAME_TX, entry->skb);
462 }
463
464 static void rt2x00queue_kick_tx_queue(struct data_queue *queue,
465                                       struct txentry_desc *txdesc)
466 {
467         /*
468          * Check if we need to kick the queue, there are however a few rules
469          *      1) Don't kick unless this is the last in frame in a burst.
470          *         When the burst flag is set, this frame is always followed
471          *         by another frame which in some way are related to eachother.
472          *         This is true for fragments, RTS or CTS-to-self frames.
473          *      2) Rule 1 can be broken when the available entries
474          *         in the queue are less then a certain threshold.
475          */
476         if (rt2x00queue_threshold(queue) ||
477             !test_bit(ENTRY_TXD_BURST, &txdesc->flags))
478                 queue->rt2x00dev->ops->lib->kick_queue(queue);
479 }
480
481 int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb,
482                                bool local)
483 {
484         struct ieee80211_tx_info *tx_info;
485         struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
486         struct txentry_desc txdesc;
487         struct skb_frame_desc *skbdesc;
488         u8 rate_idx, rate_flags;
489
490         if (unlikely(rt2x00queue_full(queue)))
491                 return -ENOBUFS;
492
493         if (unlikely(test_and_set_bit(ENTRY_OWNER_DEVICE_DATA,
494                                       &entry->flags))) {
495                 ERROR(queue->rt2x00dev,
496                       "Arrived at non-free entry in the non-full queue %d.\n"
497                       "Please file bug report to %s.\n",
498                       queue->qid, DRV_PROJECT);
499                 return -EINVAL;
500         }
501
502         /*
503          * Copy all TX descriptor information into txdesc,
504          * after that we are free to use the skb->cb array
505          * for our information.
506          */
507         entry->skb = skb;
508         rt2x00queue_create_tx_descriptor(entry, &txdesc);
509
510         /*
511          * All information is retrieved from the skb->cb array,
512          * now we should claim ownership of the driver part of that
513          * array, preserving the bitrate index and flags.
514          */
515         tx_info = IEEE80211_SKB_CB(skb);
516         rate_idx = tx_info->control.rates[0].idx;
517         rate_flags = tx_info->control.rates[0].flags;
518         skbdesc = get_skb_frame_desc(skb);
519         memset(skbdesc, 0, sizeof(*skbdesc));
520         skbdesc->entry = entry;
521         skbdesc->tx_rate_idx = rate_idx;
522         skbdesc->tx_rate_flags = rate_flags;
523
524         if (local)
525                 skbdesc->flags |= SKBDESC_NOT_MAC80211;
526
527         /*
528          * When hardware encryption is supported, and this frame
529          * is to be encrypted, we should strip the IV/EIV data from
530          * the frame so we can provide it to the driver separately.
531          */
532         if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc.flags) &&
533             !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc.flags)) {
534                 if (test_bit(DRIVER_REQUIRE_COPY_IV, &queue->rt2x00dev->flags))
535                         rt2x00crypto_tx_copy_iv(skb, &txdesc);
536                 else
537                         rt2x00crypto_tx_remove_iv(skb, &txdesc);
538         }
539
540         /*
541          * When DMA allocation is required we should guarentee to the
542          * driver that the DMA is aligned to a 4-byte boundary.
543          * However some drivers require L2 padding to pad the payload
544          * rather then the header. This could be a requirement for
545          * PCI and USB devices, while header alignment only is valid
546          * for PCI devices.
547          */
548         if (test_bit(DRIVER_REQUIRE_L2PAD, &queue->rt2x00dev->flags))
549                 rt2x00queue_insert_l2pad(entry->skb, txdesc.header_length);
550         else if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags))
551                 rt2x00queue_align_frame(entry->skb);
552
553         /*
554          * It could be possible that the queue was corrupted and this
555          * call failed. Since we always return NETDEV_TX_OK to mac80211,
556          * this frame will simply be dropped.
557          */
558         if (unlikely(rt2x00queue_write_tx_data(entry, &txdesc))) {
559                 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
560                 entry->skb = NULL;
561                 return -EIO;
562         }
563
564         set_bit(ENTRY_DATA_PENDING, &entry->flags);
565
566         rt2x00queue_index_inc(queue, Q_INDEX);
567         rt2x00queue_write_tx_descriptor(entry, &txdesc);
568         rt2x00queue_kick_tx_queue(queue, &txdesc);
569
570         return 0;
571 }
572
573 int rt2x00queue_clear_beacon(struct rt2x00_dev *rt2x00dev,
574                              struct ieee80211_vif *vif)
575 {
576         struct rt2x00_intf *intf = vif_to_intf(vif);
577
578         if (unlikely(!intf->beacon))
579                 return -ENOBUFS;
580
581         mutex_lock(&intf->beacon_skb_mutex);
582
583         /*
584          * Clean up the beacon skb.
585          */
586         rt2x00queue_free_skb(intf->beacon);
587
588         /*
589          * Clear beacon (single bssid devices don't need to clear the beacon
590          * since the beacon queue will get stopped anyway).
591          */
592         if (rt2x00dev->ops->lib->clear_beacon)
593                 rt2x00dev->ops->lib->clear_beacon(intf->beacon);
594
595         mutex_unlock(&intf->beacon_skb_mutex);
596
597         return 0;
598 }
599
600 int rt2x00queue_update_beacon_locked(struct rt2x00_dev *rt2x00dev,
601                                      struct ieee80211_vif *vif)
602 {
603         struct rt2x00_intf *intf = vif_to_intf(vif);
604         struct skb_frame_desc *skbdesc;
605         struct txentry_desc txdesc;
606
607         if (unlikely(!intf->beacon))
608                 return -ENOBUFS;
609
610         /*
611          * Clean up the beacon skb.
612          */
613         rt2x00queue_free_skb(intf->beacon);
614
615         intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
616         if (!intf->beacon->skb)
617                 return -ENOMEM;
618
619         /*
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          */
624         rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc);
625
626         /*
627          * Fill in skb descriptor
628          */
629         skbdesc = get_skb_frame_desc(intf->beacon->skb);
630         memset(skbdesc, 0, sizeof(*skbdesc));
631         skbdesc->entry = intf->beacon;
632
633         /*
634          * Send beacon to hardware.
635          */
636         rt2x00dev->ops->lib->write_beacon(intf->beacon, &txdesc);
637
638         return 0;
639
640 }
641
642 int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev,
643                               struct ieee80211_vif *vif)
644 {
645         struct rt2x00_intf *intf = vif_to_intf(vif);
646         int ret;
647
648         mutex_lock(&intf->beacon_skb_mutex);
649         ret = rt2x00queue_update_beacon_locked(rt2x00dev, vif);
650         mutex_unlock(&intf->beacon_skb_mutex);
651
652         return ret;
653 }
654
655 void rt2x00queue_for_each_entry(struct data_queue *queue,
656                                 enum queue_index start,
657                                 enum queue_index end,
658                                 void (*fn)(struct queue_entry *entry))
659 {
660         unsigned long irqflags;
661         unsigned int index_start;
662         unsigned int index_end;
663         unsigned int i;
664
665         if (unlikely(start >= Q_INDEX_MAX || end >= Q_INDEX_MAX)) {
666                 ERROR(queue->rt2x00dev,
667                       "Entry requested from invalid index range (%d - %d)\n",
668                       start, end);
669                 return;
670         }
671
672         /*
673          * Only protect the range we are going to loop over,
674          * if during our loop a extra entry is set to pending
675          * it should not be kicked during this run, since it
676          * is part of another TX operation.
677          */
678         spin_lock_irqsave(&queue->index_lock, irqflags);
679         index_start = queue->index[start];
680         index_end = queue->index[end];
681         spin_unlock_irqrestore(&queue->index_lock, irqflags);
682
683         /*
684          * Start from the TX done pointer, this guarentees that we will
685          * send out all frames in the correct order.
686          */
687         if (index_start < index_end) {
688                 for (i = index_start; i < index_end; i++)
689                         fn(&queue->entries[i]);
690         } else {
691                 for (i = index_start; i < queue->limit; i++)
692                         fn(&queue->entries[i]);
693
694                 for (i = 0; i < index_end; i++)
695                         fn(&queue->entries[i]);
696         }
697 }
698 EXPORT_SYMBOL_GPL(rt2x00queue_for_each_entry);
699
700 struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev,
701                                          const enum data_queue_qid queue)
702 {
703         int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
704
705         if (queue == QID_RX)
706                 return rt2x00dev->rx;
707
708         if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx)
709                 return &rt2x00dev->tx[queue];
710
711         if (!rt2x00dev->bcn)
712                 return NULL;
713
714         if (queue == QID_BEACON)
715                 return &rt2x00dev->bcn[0];
716         else if (queue == QID_ATIM && atim)
717                 return &rt2x00dev->bcn[1];
718
719         return NULL;
720 }
721 EXPORT_SYMBOL_GPL(rt2x00queue_get_queue);
722
723 struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue,
724                                           enum queue_index index)
725 {
726         struct queue_entry *entry;
727         unsigned long irqflags;
728
729         if (unlikely(index >= Q_INDEX_MAX)) {
730                 ERROR(queue->rt2x00dev,
731                       "Entry requested from invalid index type (%d)\n", index);
732                 return NULL;
733         }
734
735         spin_lock_irqsave(&queue->index_lock, irqflags);
736
737         entry = &queue->entries[queue->index[index]];
738
739         spin_unlock_irqrestore(&queue->index_lock, irqflags);
740
741         return entry;
742 }
743 EXPORT_SYMBOL_GPL(rt2x00queue_get_entry);
744
745 void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index)
746 {
747         unsigned long irqflags;
748
749         if (unlikely(index >= Q_INDEX_MAX)) {
750                 ERROR(queue->rt2x00dev,
751                       "Index change on invalid index type (%d)\n", index);
752                 return;
753         }
754
755         spin_lock_irqsave(&queue->index_lock, irqflags);
756
757         queue->index[index]++;
758         if (queue->index[index] >= queue->limit)
759                 queue->index[index] = 0;
760
761         queue->last_action[index] = jiffies;
762
763         if (index == Q_INDEX) {
764                 queue->length++;
765         } else if (index == Q_INDEX_DONE) {
766                 queue->length--;
767                 queue->count++;
768         }
769
770         spin_unlock_irqrestore(&queue->index_lock, irqflags);
771 }
772
773 void rt2x00queue_pause_queue(struct data_queue *queue)
774 {
775         if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
776             !test_bit(QUEUE_STARTED, &queue->flags) ||
777             test_and_set_bit(QUEUE_PAUSED, &queue->flags))
778                 return;
779
780         switch (queue->qid) {
781         case QID_AC_VO:
782         case QID_AC_VI:
783         case QID_AC_BE:
784         case QID_AC_BK:
785                 /*
786                  * For TX queues, we have to disable the queue
787                  * inside mac80211.
788                  */
789                 ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
790                 break;
791         default:
792                 break;
793         }
794 }
795 EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
796
797 void rt2x00queue_unpause_queue(struct data_queue *queue)
798 {
799         if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
800             !test_bit(QUEUE_STARTED, &queue->flags) ||
801             !test_and_clear_bit(QUEUE_PAUSED, &queue->flags))
802                 return;
803
804         switch (queue->qid) {
805         case QID_AC_VO:
806         case QID_AC_VI:
807         case QID_AC_BE:
808         case QID_AC_BK:
809                 /*
810                  * For TX queues, we have to enable the queue
811                  * inside mac80211.
812                  */
813                 ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
814                 break;
815         case QID_RX:
816                 /*
817                  * For RX we need to kick the queue now in order to
818                  * receive frames.
819                  */
820                 queue->rt2x00dev->ops->lib->kick_queue(queue);
821         default:
822                 break;
823         }
824 }
825 EXPORT_SYMBOL_GPL(rt2x00queue_unpause_queue);
826
827 void rt2x00queue_start_queue(struct data_queue *queue)
828 {
829         mutex_lock(&queue->status_lock);
830
831         if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
832             test_and_set_bit(QUEUE_STARTED, &queue->flags)) {
833                 mutex_unlock(&queue->status_lock);
834                 return;
835         }
836
837         set_bit(QUEUE_PAUSED, &queue->flags);
838
839         queue->rt2x00dev->ops->lib->start_queue(queue);
840
841         rt2x00queue_unpause_queue(queue);
842
843         mutex_unlock(&queue->status_lock);
844 }
845 EXPORT_SYMBOL_GPL(rt2x00queue_start_queue);
846
847 void rt2x00queue_stop_queue(struct data_queue *queue)
848 {
849         mutex_lock(&queue->status_lock);
850
851         if (!test_and_clear_bit(QUEUE_STARTED, &queue->flags)) {
852                 mutex_unlock(&queue->status_lock);
853                 return;
854         }
855
856         rt2x00queue_pause_queue(queue);
857
858         queue->rt2x00dev->ops->lib->stop_queue(queue);
859
860         mutex_unlock(&queue->status_lock);
861 }
862 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queue);
863
864 void rt2x00queue_flush_queue(struct data_queue *queue, bool drop)
865 {
866         unsigned int i;
867         bool started;
868         bool tx_queue =
869                 (queue->qid == QID_AC_VO) ||
870                 (queue->qid == QID_AC_VI) ||
871                 (queue->qid == QID_AC_BE) ||
872                 (queue->qid == QID_AC_BK);
873
874         mutex_lock(&queue->status_lock);
875
876         /*
877          * If the queue has been started, we must stop it temporarily
878          * to prevent any new frames to be queued on the device. If
879          * we are not dropping the pending frames, the queue must
880          * only be stopped in the software and not the hardware,
881          * otherwise the queue will never become empty on its own.
882          */
883         started = test_bit(QUEUE_STARTED, &queue->flags);
884         if (started) {
885                 /*
886                  * Pause the queue
887                  */
888                 rt2x00queue_pause_queue(queue);
889
890                 /*
891                  * If we are not supposed to drop any pending
892                  * frames, this means we must force a start (=kick)
893                  * to the queue to make sure the hardware will
894                  * start transmitting.
895                  */
896                 if (!drop && tx_queue)
897                         queue->rt2x00dev->ops->lib->kick_queue(queue);
898         }
899
900         /*
901          * Check if driver supports flushing, we can only guarentee
902          * full support for flushing if the driver is able
903          * to cancel all pending frames (drop = true).
904          */
905         if (drop && queue->rt2x00dev->ops->lib->flush_queue)
906                 queue->rt2x00dev->ops->lib->flush_queue(queue);
907
908         /*
909          * When we don't want to drop any frames, or when
910          * the driver doesn't fully flush the queue correcly,
911          * we must wait for the queue to become empty.
912          */
913         for (i = 0; !rt2x00queue_empty(queue) && i < 100; i++)
914                 msleep(10);
915
916         /*
917          * The queue flush has failed...
918          */
919         if (unlikely(!rt2x00queue_empty(queue)))
920                 WARNING(queue->rt2x00dev, "Queue %d failed to flush\n", queue->qid);
921
922         /*
923          * Restore the queue to the previous status
924          */
925         if (started)
926                 rt2x00queue_unpause_queue(queue);
927
928         mutex_unlock(&queue->status_lock);
929 }
930 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queue);
931
932 void rt2x00queue_start_queues(struct rt2x00_dev *rt2x00dev)
933 {
934         struct data_queue *queue;
935
936         /*
937          * rt2x00queue_start_queue will call ieee80211_wake_queue
938          * for each queue after is has been properly initialized.
939          */
940         tx_queue_for_each(rt2x00dev, queue)
941                 rt2x00queue_start_queue(queue);
942
943         rt2x00queue_start_queue(rt2x00dev->rx);
944 }
945 EXPORT_SYMBOL_GPL(rt2x00queue_start_queues);
946
947 void rt2x00queue_stop_queues(struct rt2x00_dev *rt2x00dev)
948 {
949         struct data_queue *queue;
950
951         /*
952          * rt2x00queue_stop_queue will call ieee80211_stop_queue
953          * as well, but we are completely shutting doing everything
954          * now, so it is much safer to stop all TX queues at once,
955          * and use rt2x00queue_stop_queue for cleaning up.
956          */
957         ieee80211_stop_queues(rt2x00dev->hw);
958
959         tx_queue_for_each(rt2x00dev, queue)
960                 rt2x00queue_stop_queue(queue);
961
962         rt2x00queue_stop_queue(rt2x00dev->rx);
963 }
964 EXPORT_SYMBOL_GPL(rt2x00queue_stop_queues);
965
966 void rt2x00queue_flush_queues(struct rt2x00_dev *rt2x00dev, bool drop)
967 {
968         struct data_queue *queue;
969
970         tx_queue_for_each(rt2x00dev, queue)
971                 rt2x00queue_flush_queue(queue, drop);
972
973         rt2x00queue_flush_queue(rt2x00dev->rx, drop);
974 }
975 EXPORT_SYMBOL_GPL(rt2x00queue_flush_queues);
976
977 static void rt2x00queue_reset(struct data_queue *queue)
978 {
979         unsigned long irqflags;
980         unsigned int i;
981
982         spin_lock_irqsave(&queue->index_lock, irqflags);
983
984         queue->count = 0;
985         queue->length = 0;
986
987         for (i = 0; i < Q_INDEX_MAX; i++) {
988                 queue->index[i] = 0;
989                 queue->last_action[i] = jiffies;
990         }
991
992         spin_unlock_irqrestore(&queue->index_lock, irqflags);
993 }
994
995 void rt2x00queue_init_queues(struct rt2x00_dev *rt2x00dev)
996 {
997         struct data_queue *queue;
998         unsigned int i;
999
1000         queue_for_each(rt2x00dev, queue) {
1001                 rt2x00queue_reset(queue);
1002
1003                 for (i = 0; i < queue->limit; i++)
1004                         rt2x00dev->ops->lib->clear_entry(&queue->entries[i]);
1005         }
1006 }
1007
1008 static int rt2x00queue_alloc_entries(struct data_queue *queue,
1009                                      const struct data_queue_desc *qdesc)
1010 {
1011         struct queue_entry *entries;
1012         unsigned int entry_size;
1013         unsigned int i;
1014
1015         rt2x00queue_reset(queue);
1016
1017         queue->limit = qdesc->entry_num;
1018         queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10);
1019         queue->data_size = qdesc->data_size;
1020         queue->desc_size = qdesc->desc_size;
1021
1022         /*
1023          * Allocate all queue entries.
1024          */
1025         entry_size = sizeof(*entries) + qdesc->priv_size;
1026         entries = kcalloc(queue->limit, entry_size, GFP_KERNEL);
1027         if (!entries)
1028                 return -ENOMEM;
1029
1030 #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \
1031         (((char *)(__base)) + ((__limit) * (__esize)) + \
1032             ((__index) * (__psize)))
1033
1034         for (i = 0; i < queue->limit; i++) {
1035                 entries[i].flags = 0;
1036                 entries[i].queue = queue;
1037                 entries[i].skb = NULL;
1038                 entries[i].entry_idx = i;
1039                 entries[i].priv_data =
1040                     QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit,
1041                                             sizeof(*entries), qdesc->priv_size);
1042         }
1043
1044 #undef QUEUE_ENTRY_PRIV_OFFSET
1045
1046         queue->entries = entries;
1047
1048         return 0;
1049 }
1050
1051 static void rt2x00queue_free_skbs(struct data_queue *queue)
1052 {
1053         unsigned int i;
1054
1055         if (!queue->entries)
1056                 return;
1057
1058         for (i = 0; i < queue->limit; i++) {
1059                 rt2x00queue_free_skb(&queue->entries[i]);
1060         }
1061 }
1062
1063 static int rt2x00queue_alloc_rxskbs(struct data_queue *queue)
1064 {
1065         unsigned int i;
1066         struct sk_buff *skb;
1067
1068         for (i = 0; i < queue->limit; i++) {
1069                 skb = rt2x00queue_alloc_rxskb(&queue->entries[i]);
1070                 if (!skb)
1071                         return -ENOMEM;
1072                 queue->entries[i].skb = skb;
1073         }
1074
1075         return 0;
1076 }
1077
1078 int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev)
1079 {
1080         struct data_queue *queue;
1081         int status;
1082
1083         status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx);
1084         if (status)
1085                 goto exit;
1086
1087         tx_queue_for_each(rt2x00dev, queue) {
1088                 status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx);
1089                 if (status)
1090                         goto exit;
1091         }
1092
1093         status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn);
1094         if (status)
1095                 goto exit;
1096
1097         if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) {
1098                 status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1],
1099                                                    rt2x00dev->ops->atim);
1100                 if (status)
1101                         goto exit;
1102         }
1103
1104         status = rt2x00queue_alloc_rxskbs(rt2x00dev->rx);
1105         if (status)
1106                 goto exit;
1107
1108         return 0;
1109
1110 exit:
1111         ERROR(rt2x00dev, "Queue entries allocation failed.\n");
1112
1113         rt2x00queue_uninitialize(rt2x00dev);
1114
1115         return status;
1116 }
1117
1118 void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev)
1119 {
1120         struct data_queue *queue;
1121
1122         rt2x00queue_free_skbs(rt2x00dev->rx);
1123
1124         queue_for_each(rt2x00dev, queue) {
1125                 kfree(queue->entries);
1126                 queue->entries = NULL;
1127         }
1128 }
1129
1130 static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev,
1131                              struct data_queue *queue, enum data_queue_qid qid)
1132 {
1133         mutex_init(&queue->status_lock);
1134         spin_lock_init(&queue->index_lock);
1135
1136         queue->rt2x00dev = rt2x00dev;
1137         queue->qid = qid;
1138         queue->txop = 0;
1139         queue->aifs = 2;
1140         queue->cw_min = 5;
1141         queue->cw_max = 10;
1142 }
1143
1144 int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev)
1145 {
1146         struct data_queue *queue;
1147         enum data_queue_qid qid;
1148         unsigned int req_atim =
1149             !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
1150
1151         /*
1152          * We need the following queues:
1153          * RX: 1
1154          * TX: ops->tx_queues
1155          * Beacon: 1
1156          * Atim: 1 (if required)
1157          */
1158         rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim;
1159
1160         queue = kcalloc(rt2x00dev->data_queues, sizeof(*queue), GFP_KERNEL);
1161         if (!queue) {
1162                 ERROR(rt2x00dev, "Queue allocation failed.\n");
1163                 return -ENOMEM;
1164         }
1165
1166         /*
1167          * Initialize pointers
1168          */
1169         rt2x00dev->rx = queue;
1170         rt2x00dev->tx = &queue[1];
1171         rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues];
1172
1173         /*
1174          * Initialize queue parameters.
1175          * RX: qid = QID_RX
1176          * TX: qid = QID_AC_VO + index
1177          * TX: cw_min: 2^5 = 32.
1178          * TX: cw_max: 2^10 = 1024.
1179          * BCN: qid = QID_BEACON
1180          * ATIM: qid = QID_ATIM
1181          */
1182         rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX);
1183
1184         qid = QID_AC_VO;
1185         tx_queue_for_each(rt2x00dev, queue)
1186                 rt2x00queue_init(rt2x00dev, queue, qid++);
1187
1188         rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON);
1189         if (req_atim)
1190                 rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM);
1191
1192         return 0;
1193 }
1194
1195 void rt2x00queue_free(struct rt2x00_dev *rt2x00dev)
1196 {
1197         kfree(rt2x00dev->rx);
1198         rt2x00dev->rx = NULL;
1199         rt2x00dev->tx = NULL;
1200         rt2x00dev->bcn = NULL;
1201 }