1 // SPDX-License-Identifier: GPL-2.0-or-later
4 Broadcom B43legacy wireless driver
6 DMA ringbuffer and descriptor allocation/management
8 Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch>
10 Some code in this file is derived from the b44.c driver
11 Copyright (C) 2002 David S. Miller
12 Copyright (C) Pekka Pietikainen
17 #include "b43legacy.h"
23 #include <linux/dma-mapping.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
26 #include <linux/skbuff.h>
27 #include <linux/slab.h>
32 struct b43legacy_dmadesc32 *op32_idx2desc(struct b43legacy_dmaring *ring,
34 struct b43legacy_dmadesc_meta **meta)
36 struct b43legacy_dmadesc32 *desc;
38 *meta = &(ring->meta[slot]);
39 desc = ring->descbase;
45 static void op32_fill_descriptor(struct b43legacy_dmaring *ring,
46 struct b43legacy_dmadesc32 *desc,
47 dma_addr_t dmaaddr, u16 bufsize,
48 int start, int end, int irq)
50 struct b43legacy_dmadesc32 *descbase = ring->descbase;
56 slot = (int)(desc - descbase);
57 B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
59 addr = (u32)(dmaaddr & ~SSB_DMA_TRANSLATION_MASK);
60 addrext = (u32)(dmaaddr & SSB_DMA_TRANSLATION_MASK)
61 >> SSB_DMA_TRANSLATION_SHIFT;
62 addr |= ring->dev->dma.translation;
63 ctl = (bufsize - ring->frameoffset)
64 & B43legacy_DMA32_DCTL_BYTECNT;
65 if (slot == ring->nr_slots - 1)
66 ctl |= B43legacy_DMA32_DCTL_DTABLEEND;
68 ctl |= B43legacy_DMA32_DCTL_FRAMESTART;
70 ctl |= B43legacy_DMA32_DCTL_FRAMEEND;
72 ctl |= B43legacy_DMA32_DCTL_IRQ;
73 ctl |= (addrext << B43legacy_DMA32_DCTL_ADDREXT_SHIFT)
74 & B43legacy_DMA32_DCTL_ADDREXT_MASK;
76 desc->control = cpu_to_le32(ctl);
77 desc->address = cpu_to_le32(addr);
80 static void op32_poke_tx(struct b43legacy_dmaring *ring, int slot)
82 b43legacy_dma_write(ring, B43legacy_DMA32_TXINDEX,
83 (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
86 static void op32_tx_suspend(struct b43legacy_dmaring *ring)
88 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
89 b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
90 | B43legacy_DMA32_TXSUSPEND);
93 static void op32_tx_resume(struct b43legacy_dmaring *ring)
95 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
96 b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
97 & ~B43legacy_DMA32_TXSUSPEND);
100 static int op32_get_current_rxslot(struct b43legacy_dmaring *ring)
104 val = b43legacy_dma_read(ring, B43legacy_DMA32_RXSTATUS);
105 val &= B43legacy_DMA32_RXDPTR;
107 return (val / sizeof(struct b43legacy_dmadesc32));
110 static void op32_set_current_rxslot(struct b43legacy_dmaring *ring,
113 b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX,
114 (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
117 static inline int free_slots(struct b43legacy_dmaring *ring)
119 return (ring->nr_slots - ring->used_slots);
122 static inline int next_slot(struct b43legacy_dmaring *ring, int slot)
124 B43legacy_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1));
125 if (slot == ring->nr_slots - 1)
130 static inline int prev_slot(struct b43legacy_dmaring *ring, int slot)
132 B43legacy_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1));
134 return ring->nr_slots - 1;
138 #ifdef CONFIG_B43LEGACY_DEBUG
139 static void update_max_used_slots(struct b43legacy_dmaring *ring,
140 int current_used_slots)
142 if (current_used_slots <= ring->max_used_slots)
144 ring->max_used_slots = current_used_slots;
145 if (b43legacy_debug(ring->dev, B43legacy_DBG_DMAVERBOSE))
146 b43legacydbg(ring->dev->wl,
147 "max_used_slots increased to %d on %s ring %d\n",
148 ring->max_used_slots,
149 ring->tx ? "TX" : "RX",
154 void update_max_used_slots(struct b43legacy_dmaring *ring,
155 int current_used_slots)
159 /* Request a slot for usage. */
161 int request_slot(struct b43legacy_dmaring *ring)
165 B43legacy_WARN_ON(!ring->tx);
166 B43legacy_WARN_ON(ring->stopped);
167 B43legacy_WARN_ON(free_slots(ring) == 0);
169 slot = next_slot(ring, ring->current_slot);
170 ring->current_slot = slot;
173 update_max_used_slots(ring, ring->used_slots);
178 /* Mac80211-queue to b43legacy-ring mapping */
179 static struct b43legacy_dmaring *priority_to_txring(
180 struct b43legacy_wldev *dev,
183 struct b43legacy_dmaring *ring;
185 /*FIXME: For now we always run on TX-ring-1 */
186 return dev->dma.tx_ring1;
188 /* 0 = highest priority */
189 switch (queue_priority) {
191 B43legacy_WARN_ON(1);
194 ring = dev->dma.tx_ring3;
197 ring = dev->dma.tx_ring2;
200 ring = dev->dma.tx_ring1;
203 ring = dev->dma.tx_ring0;
206 ring = dev->dma.tx_ring4;
209 ring = dev->dma.tx_ring5;
216 static u16 b43legacy_dmacontroller_base(enum b43legacy_dmatype type,
219 static const u16 map32[] = {
220 B43legacy_MMIO_DMA32_BASE0,
221 B43legacy_MMIO_DMA32_BASE1,
222 B43legacy_MMIO_DMA32_BASE2,
223 B43legacy_MMIO_DMA32_BASE3,
224 B43legacy_MMIO_DMA32_BASE4,
225 B43legacy_MMIO_DMA32_BASE5,
228 B43legacy_WARN_ON(!(controller_idx >= 0 &&
229 controller_idx < ARRAY_SIZE(map32)));
230 return map32[controller_idx];
234 dma_addr_t map_descbuffer(struct b43legacy_dmaring *ring,
242 dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
246 dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
254 void unmap_descbuffer(struct b43legacy_dmaring *ring,
260 dma_unmap_single(ring->dev->dev->dma_dev,
264 dma_unmap_single(ring->dev->dev->dma_dev,
270 void sync_descbuffer_for_cpu(struct b43legacy_dmaring *ring,
274 B43legacy_WARN_ON(ring->tx);
276 dma_sync_single_for_cpu(ring->dev->dev->dma_dev,
277 addr, len, DMA_FROM_DEVICE);
281 void sync_descbuffer_for_device(struct b43legacy_dmaring *ring,
285 B43legacy_WARN_ON(ring->tx);
287 dma_sync_single_for_device(ring->dev->dev->dma_dev,
288 addr, len, DMA_FROM_DEVICE);
292 void free_descriptor_buffer(struct b43legacy_dmaring *ring,
293 struct b43legacy_dmadesc_meta *meta,
298 dev_kfree_skb_irq(meta->skb);
300 dev_kfree_skb(meta->skb);
305 static int alloc_ringmemory(struct b43legacy_dmaring *ring)
307 /* GFP flags must match the flags in free_ringmemory()! */
308 ring->descbase = dma_alloc_coherent(ring->dev->dev->dma_dev,
309 B43legacy_DMA_RINGMEMSIZE,
310 &(ring->dmabase), GFP_KERNEL);
317 static void free_ringmemory(struct b43legacy_dmaring *ring)
319 dma_free_coherent(ring->dev->dev->dma_dev, B43legacy_DMA_RINGMEMSIZE,
320 ring->descbase, ring->dmabase);
323 /* Reset the RX DMA channel */
324 static int b43legacy_dmacontroller_rx_reset(struct b43legacy_wldev *dev,
326 enum b43legacy_dmatype type)
334 offset = B43legacy_DMA32_RXCTL;
335 b43legacy_write32(dev, mmio_base + offset, 0);
336 for (i = 0; i < 10; i++) {
337 offset = B43legacy_DMA32_RXSTATUS;
338 value = b43legacy_read32(dev, mmio_base + offset);
339 value &= B43legacy_DMA32_RXSTATE;
340 if (value == B43legacy_DMA32_RXSTAT_DISABLED) {
347 b43legacyerr(dev->wl, "DMA RX reset timed out\n");
354 /* Reset the RX DMA channel */
355 static int b43legacy_dmacontroller_tx_reset(struct b43legacy_wldev *dev,
357 enum b43legacy_dmatype type)
365 for (i = 0; i < 10; i++) {
366 offset = B43legacy_DMA32_TXSTATUS;
367 value = b43legacy_read32(dev, mmio_base + offset);
368 value &= B43legacy_DMA32_TXSTATE;
369 if (value == B43legacy_DMA32_TXSTAT_DISABLED ||
370 value == B43legacy_DMA32_TXSTAT_IDLEWAIT ||
371 value == B43legacy_DMA32_TXSTAT_STOPPED)
375 offset = B43legacy_DMA32_TXCTL;
376 b43legacy_write32(dev, mmio_base + offset, 0);
377 for (i = 0; i < 10; i++) {
378 offset = B43legacy_DMA32_TXSTATUS;
379 value = b43legacy_read32(dev, mmio_base + offset);
380 value &= B43legacy_DMA32_TXSTATE;
381 if (value == B43legacy_DMA32_TXSTAT_DISABLED) {
388 b43legacyerr(dev->wl, "DMA TX reset timed out\n");
391 /* ensure the reset is completed. */
397 /* Check if a DMA mapping address is invalid. */
398 static bool b43legacy_dma_mapping_error(struct b43legacy_dmaring *ring,
403 if (unlikely(dma_mapping_error(ring->dev->dev->dma_dev, addr)))
406 switch (ring->type) {
407 case B43legacy_DMA_30BIT:
408 if ((u64)addr + buffersize > (1ULL << 30))
411 case B43legacy_DMA_32BIT:
412 if ((u64)addr + buffersize > (1ULL << 32))
417 /* The address is OK. */
421 /* We can't support this address. Unmap it again. */
422 unmap_descbuffer(ring, addr, buffersize, dma_to_device);
427 static int setup_rx_descbuffer(struct b43legacy_dmaring *ring,
428 struct b43legacy_dmadesc32 *desc,
429 struct b43legacy_dmadesc_meta *meta,
432 struct b43legacy_rxhdr_fw3 *rxhdr;
433 struct b43legacy_hwtxstatus *txstat;
437 B43legacy_WARN_ON(ring->tx);
439 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
442 dmaaddr = map_descbuffer(ring, skb->data,
443 ring->rx_buffersize, 0);
444 if (b43legacy_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
445 /* ugh. try to realloc in zone_dma */
446 gfp_flags |= GFP_DMA;
448 dev_kfree_skb_any(skb);
450 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
453 dmaaddr = map_descbuffer(ring, skb->data,
454 ring->rx_buffersize, 0);
457 if (b43legacy_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
458 dev_kfree_skb_any(skb);
463 meta->dmaaddr = dmaaddr;
464 op32_fill_descriptor(ring, desc, dmaaddr, ring->rx_buffersize, 0, 0, 0);
466 rxhdr = (struct b43legacy_rxhdr_fw3 *)(skb->data);
467 rxhdr->frame_len = 0;
468 txstat = (struct b43legacy_hwtxstatus *)(skb->data);
474 /* Allocate the initial descbuffers.
475 * This is used for an RX ring only.
477 static int alloc_initial_descbuffers(struct b43legacy_dmaring *ring)
481 struct b43legacy_dmadesc32 *desc;
482 struct b43legacy_dmadesc_meta *meta;
484 for (i = 0; i < ring->nr_slots; i++) {
485 desc = op32_idx2desc(ring, i, &meta);
487 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
489 b43legacyerr(ring->dev->wl,
490 "Failed to allocate initial descbuffers\n");
494 mb(); /* all descbuffer setup before next line */
495 ring->used_slots = ring->nr_slots;
501 for (i--; i >= 0; i--) {
502 desc = op32_idx2desc(ring, i, &meta);
504 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
505 dev_kfree_skb(meta->skb);
510 /* Do initial setup of the DMA controller.
511 * Reset the controller, write the ring busaddress
512 * and switch the "enable" bit on.
514 static int dmacontroller_setup(struct b43legacy_dmaring *ring)
519 u32 trans = ring->dev->dma.translation;
520 u32 ringbase = (u32)(ring->dmabase);
523 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
524 >> SSB_DMA_TRANSLATION_SHIFT;
525 value = B43legacy_DMA32_TXENABLE;
526 value |= (addrext << B43legacy_DMA32_TXADDREXT_SHIFT)
527 & B43legacy_DMA32_TXADDREXT_MASK;
528 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL, value);
529 b43legacy_dma_write(ring, B43legacy_DMA32_TXRING,
530 (ringbase & ~SSB_DMA_TRANSLATION_MASK)
533 err = alloc_initial_descbuffers(ring);
537 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
538 >> SSB_DMA_TRANSLATION_SHIFT;
539 value = (ring->frameoffset <<
540 B43legacy_DMA32_RXFROFF_SHIFT);
541 value |= B43legacy_DMA32_RXENABLE;
542 value |= (addrext << B43legacy_DMA32_RXADDREXT_SHIFT)
543 & B43legacy_DMA32_RXADDREXT_MASK;
544 b43legacy_dma_write(ring, B43legacy_DMA32_RXCTL, value);
545 b43legacy_dma_write(ring, B43legacy_DMA32_RXRING,
546 (ringbase & ~SSB_DMA_TRANSLATION_MASK)
548 b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX, 200);
555 /* Shutdown the DMA controller. */
556 static void dmacontroller_cleanup(struct b43legacy_dmaring *ring)
559 b43legacy_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
561 b43legacy_dma_write(ring, B43legacy_DMA32_TXRING, 0);
563 b43legacy_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
565 b43legacy_dma_write(ring, B43legacy_DMA32_RXRING, 0);
569 static void free_all_descbuffers(struct b43legacy_dmaring *ring)
571 struct b43legacy_dmadesc_meta *meta;
574 if (!ring->used_slots)
576 for (i = 0; i < ring->nr_slots; i++) {
577 op32_idx2desc(ring, i, &meta);
580 B43legacy_WARN_ON(!ring->tx);
584 unmap_descbuffer(ring, meta->dmaaddr,
587 unmap_descbuffer(ring, meta->dmaaddr,
588 ring->rx_buffersize, 0);
589 free_descriptor_buffer(ring, meta, 0);
593 static enum b43legacy_dmatype b43legacy_engine_type(struct b43legacy_wldev *dev)
598 mmio_base = b43legacy_dmacontroller_base(0, 0);
599 b43legacy_write32(dev,
600 mmio_base + B43legacy_DMA32_TXCTL,
601 B43legacy_DMA32_TXADDREXT_MASK);
602 tmp = b43legacy_read32(dev, mmio_base +
603 B43legacy_DMA32_TXCTL);
604 if (tmp & B43legacy_DMA32_TXADDREXT_MASK)
605 return B43legacy_DMA_32BIT;
606 return B43legacy_DMA_30BIT;
609 /* Main initialization function. */
611 struct b43legacy_dmaring *b43legacy_setup_dmaring(struct b43legacy_wldev *dev,
612 int controller_index,
614 enum b43legacy_dmatype type)
616 struct b43legacy_dmaring *ring;
621 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
627 nr_slots = B43legacy_RXRING_SLOTS;
629 nr_slots = B43legacy_TXRING_SLOTS;
631 ring->meta = kcalloc(nr_slots, sizeof(struct b43legacy_dmadesc_meta),
636 ring->txhdr_cache = kcalloc(nr_slots,
637 sizeof(struct b43legacy_txhdr_fw3),
639 if (!ring->txhdr_cache)
642 /* test for ability to dma to txhdr_cache */
643 dma_test = dma_map_single(dev->dev->dma_dev, ring->txhdr_cache,
644 sizeof(struct b43legacy_txhdr_fw3),
647 if (b43legacy_dma_mapping_error(ring, dma_test,
648 sizeof(struct b43legacy_txhdr_fw3), 1)) {
650 kfree(ring->txhdr_cache);
651 ring->txhdr_cache = kcalloc(nr_slots,
652 sizeof(struct b43legacy_txhdr_fw3),
653 GFP_KERNEL | GFP_DMA);
654 if (!ring->txhdr_cache)
657 dma_test = dma_map_single(dev->dev->dma_dev,
659 sizeof(struct b43legacy_txhdr_fw3),
662 if (b43legacy_dma_mapping_error(ring, dma_test,
663 sizeof(struct b43legacy_txhdr_fw3), 1))
664 goto err_kfree_txhdr_cache;
667 dma_unmap_single(dev->dev->dma_dev, dma_test,
668 sizeof(struct b43legacy_txhdr_fw3),
672 ring->nr_slots = nr_slots;
673 ring->mmio_base = b43legacy_dmacontroller_base(type, controller_index);
674 ring->index = controller_index;
677 ring->current_slot = -1;
679 if (ring->index == 0) {
680 ring->rx_buffersize = B43legacy_DMA0_RX_BUFFERSIZE;
681 ring->frameoffset = B43legacy_DMA0_RX_FRAMEOFFSET;
682 } else if (ring->index == 3) {
683 ring->rx_buffersize = B43legacy_DMA3_RX_BUFFERSIZE;
684 ring->frameoffset = B43legacy_DMA3_RX_FRAMEOFFSET;
686 B43legacy_WARN_ON(1);
688 #ifdef CONFIG_B43LEGACY_DEBUG
689 ring->last_injected_overflow = jiffies;
692 err = alloc_ringmemory(ring);
694 goto err_kfree_txhdr_cache;
695 err = dmacontroller_setup(ring);
697 goto err_free_ringmemory;
703 free_ringmemory(ring);
704 err_kfree_txhdr_cache:
705 kfree(ring->txhdr_cache);
714 /* Main cleanup function. */
715 static void b43legacy_destroy_dmaring(struct b43legacy_dmaring *ring)
720 b43legacydbg(ring->dev->wl, "DMA-%u 0x%04X (%s) max used slots:"
721 " %d/%d\n", (unsigned int)(ring->type), ring->mmio_base,
722 (ring->tx) ? "TX" : "RX", ring->max_used_slots,
724 /* Device IRQs are disabled prior entering this function,
725 * so no need to take care of concurrency with rx handler stuff.
727 dmacontroller_cleanup(ring);
728 free_all_descbuffers(ring);
729 free_ringmemory(ring);
731 kfree(ring->txhdr_cache);
736 void b43legacy_dma_free(struct b43legacy_wldev *dev)
738 struct b43legacy_dma *dma;
740 if (b43legacy_using_pio(dev))
744 b43legacy_destroy_dmaring(dma->rx_ring3);
745 dma->rx_ring3 = NULL;
746 b43legacy_destroy_dmaring(dma->rx_ring0);
747 dma->rx_ring0 = NULL;
749 b43legacy_destroy_dmaring(dma->tx_ring5);
750 dma->tx_ring5 = NULL;
751 b43legacy_destroy_dmaring(dma->tx_ring4);
752 dma->tx_ring4 = NULL;
753 b43legacy_destroy_dmaring(dma->tx_ring3);
754 dma->tx_ring3 = NULL;
755 b43legacy_destroy_dmaring(dma->tx_ring2);
756 dma->tx_ring2 = NULL;
757 b43legacy_destroy_dmaring(dma->tx_ring1);
758 dma->tx_ring1 = NULL;
759 b43legacy_destroy_dmaring(dma->tx_ring0);
760 dma->tx_ring0 = NULL;
763 int b43legacy_dma_init(struct b43legacy_wldev *dev)
765 struct b43legacy_dma *dma = &dev->dma;
766 struct b43legacy_dmaring *ring;
767 enum b43legacy_dmatype type = b43legacy_engine_type(dev);
770 err = dma_set_mask_and_coherent(dev->dev->dma_dev, DMA_BIT_MASK(type));
772 #ifdef CONFIG_B43LEGACY_PIO
773 b43legacywarn(dev->wl, "DMA for this device not supported. "
774 "Falling back to PIO\n");
775 dev->__using_pio = true;
778 b43legacyerr(dev->wl, "DMA for this device not supported and "
779 "no PIO support compiled in\n");
783 dma->translation = ssb_dma_translation(dev->dev);
786 /* setup TX DMA channels. */
787 ring = b43legacy_setup_dmaring(dev, 0, 1, type);
790 dma->tx_ring0 = ring;
792 ring = b43legacy_setup_dmaring(dev, 1, 1, type);
794 goto err_destroy_tx0;
795 dma->tx_ring1 = ring;
797 ring = b43legacy_setup_dmaring(dev, 2, 1, type);
799 goto err_destroy_tx1;
800 dma->tx_ring2 = ring;
802 ring = b43legacy_setup_dmaring(dev, 3, 1, type);
804 goto err_destroy_tx2;
805 dma->tx_ring3 = ring;
807 ring = b43legacy_setup_dmaring(dev, 4, 1, type);
809 goto err_destroy_tx3;
810 dma->tx_ring4 = ring;
812 ring = b43legacy_setup_dmaring(dev, 5, 1, type);
814 goto err_destroy_tx4;
815 dma->tx_ring5 = ring;
817 /* setup RX DMA channels. */
818 ring = b43legacy_setup_dmaring(dev, 0, 0, type);
820 goto err_destroy_tx5;
821 dma->rx_ring0 = ring;
823 if (dev->dev->id.revision < 5) {
824 ring = b43legacy_setup_dmaring(dev, 3, 0, type);
826 goto err_destroy_rx0;
827 dma->rx_ring3 = ring;
830 b43legacydbg(dev->wl, "%u-bit DMA initialized\n", (unsigned int)type);
836 b43legacy_destroy_dmaring(dma->rx_ring0);
837 dma->rx_ring0 = NULL;
839 b43legacy_destroy_dmaring(dma->tx_ring5);
840 dma->tx_ring5 = NULL;
842 b43legacy_destroy_dmaring(dma->tx_ring4);
843 dma->tx_ring4 = NULL;
845 b43legacy_destroy_dmaring(dma->tx_ring3);
846 dma->tx_ring3 = NULL;
848 b43legacy_destroy_dmaring(dma->tx_ring2);
849 dma->tx_ring2 = NULL;
851 b43legacy_destroy_dmaring(dma->tx_ring1);
852 dma->tx_ring1 = NULL;
854 b43legacy_destroy_dmaring(dma->tx_ring0);
855 dma->tx_ring0 = NULL;
859 /* Generate a cookie for the TX header. */
860 static u16 generate_cookie(struct b43legacy_dmaring *ring,
865 /* Use the upper 4 bits of the cookie as
866 * DMA controller ID and store the slot number
867 * in the lower 12 bits.
868 * Note that the cookie must never be 0, as this
869 * is a special value used in RX path.
871 switch (ring->index) {
891 B43legacy_WARN_ON(!(((u16)slot & 0xF000) == 0x0000));
897 /* Inspect a cookie and find out to which controller/slot it belongs. */
899 struct b43legacy_dmaring *parse_cookie(struct b43legacy_wldev *dev,
900 u16 cookie, int *slot)
902 struct b43legacy_dma *dma = &dev->dma;
903 struct b43legacy_dmaring *ring = NULL;
905 switch (cookie & 0xF000) {
907 ring = dma->tx_ring0;
910 ring = dma->tx_ring1;
913 ring = dma->tx_ring2;
916 ring = dma->tx_ring3;
919 ring = dma->tx_ring4;
922 ring = dma->tx_ring5;
925 B43legacy_WARN_ON(1);
927 *slot = (cookie & 0x0FFF);
928 B43legacy_WARN_ON(!(ring && *slot >= 0 && *slot < ring->nr_slots));
933 static int dma_tx_fragment(struct b43legacy_dmaring *ring,
934 struct sk_buff **in_skb)
936 struct sk_buff *skb = *in_skb;
937 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
939 int slot, old_top_slot, old_used_slots;
941 struct b43legacy_dmadesc32 *desc;
942 struct b43legacy_dmadesc_meta *meta;
943 struct b43legacy_dmadesc_meta *meta_hdr;
944 struct sk_buff *bounce_skb;
946 #define SLOTS_PER_PACKET 2
947 B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags != 0);
949 old_top_slot = ring->current_slot;
950 old_used_slots = ring->used_slots;
952 /* Get a slot for the header. */
953 slot = request_slot(ring);
954 desc = op32_idx2desc(ring, slot, &meta_hdr);
955 memset(meta_hdr, 0, sizeof(*meta_hdr));
957 header = &(ring->txhdr_cache[slot * sizeof(
958 struct b43legacy_txhdr_fw3)]);
959 err = b43legacy_generate_txhdr(ring->dev, header,
960 skb->data, skb->len, info,
961 generate_cookie(ring, slot));
963 ring->current_slot = old_top_slot;
964 ring->used_slots = old_used_slots;
968 meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
969 sizeof(struct b43legacy_txhdr_fw3), 1);
970 if (b43legacy_dma_mapping_error(ring, meta_hdr->dmaaddr,
971 sizeof(struct b43legacy_txhdr_fw3), 1)) {
972 ring->current_slot = old_top_slot;
973 ring->used_slots = old_used_slots;
976 op32_fill_descriptor(ring, desc, meta_hdr->dmaaddr,
977 sizeof(struct b43legacy_txhdr_fw3), 1, 0, 0);
979 /* Get a slot for the payload. */
980 slot = request_slot(ring);
981 desc = op32_idx2desc(ring, slot, &meta);
982 memset(meta, 0, sizeof(*meta));
985 meta->is_last_fragment = true;
987 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
988 /* create a bounce buffer in zone_dma on mapping failure. */
989 if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
990 bounce_skb = alloc_skb(skb->len, GFP_KERNEL | GFP_DMA);
992 ring->current_slot = old_top_slot;
993 ring->used_slots = old_used_slots;
998 skb_put_data(bounce_skb, skb->data, skb->len);
999 memcpy(bounce_skb->cb, skb->cb, sizeof(skb->cb));
1000 bounce_skb->dev = skb->dev;
1001 skb_set_queue_mapping(bounce_skb, skb_get_queue_mapping(skb));
1002 info = IEEE80211_SKB_CB(bounce_skb);
1004 dev_kfree_skb_any(skb);
1006 *in_skb = bounce_skb;
1008 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1009 if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
1010 ring->current_slot = old_top_slot;
1011 ring->used_slots = old_used_slots;
1013 goto out_free_bounce;
1017 op32_fill_descriptor(ring, desc, meta->dmaaddr,
1020 wmb(); /* previous stuff MUST be done */
1021 /* Now transfer the whole frame. */
1022 op32_poke_tx(ring, next_slot(ring, slot));
1026 dev_kfree_skb_any(skb);
1028 unmap_descbuffer(ring, meta_hdr->dmaaddr,
1029 sizeof(struct b43legacy_txhdr_fw3), 1);
1034 int should_inject_overflow(struct b43legacy_dmaring *ring)
1036 #ifdef CONFIG_B43LEGACY_DEBUG
1037 if (unlikely(b43legacy_debug(ring->dev,
1038 B43legacy_DBG_DMAOVERFLOW))) {
1039 /* Check if we should inject another ringbuffer overflow
1040 * to test handling of this situation in the stack. */
1041 unsigned long next_overflow;
1043 next_overflow = ring->last_injected_overflow + HZ;
1044 if (time_after(jiffies, next_overflow)) {
1045 ring->last_injected_overflow = jiffies;
1046 b43legacydbg(ring->dev->wl,
1047 "Injecting TX ring overflow on "
1048 "DMA controller %d\n", ring->index);
1052 #endif /* CONFIG_B43LEGACY_DEBUG */
1056 int b43legacy_dma_tx(struct b43legacy_wldev *dev,
1057 struct sk_buff *skb)
1059 struct b43legacy_dmaring *ring;
1062 ring = priority_to_txring(dev, skb_get_queue_mapping(skb));
1063 B43legacy_WARN_ON(!ring->tx);
1065 if (unlikely(ring->stopped)) {
1066 /* We get here only because of a bug in mac80211.
1067 * Because of a race, one packet may be queued after
1068 * the queue is stopped, thus we got called when we shouldn't.
1069 * For now, just refuse the transmit. */
1070 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1071 b43legacyerr(dev->wl, "Packet after queue stopped\n");
1075 if (WARN_ON(free_slots(ring) < SLOTS_PER_PACKET)) {
1076 /* If we get here, we have a real error with the queue
1077 * full, but queues not stopped. */
1078 b43legacyerr(dev->wl, "DMA queue overflow\n");
1082 /* dma_tx_fragment might reallocate the skb, so invalidate pointers pointing
1083 * into the skb data or cb now. */
1084 err = dma_tx_fragment(ring, &skb);
1085 if (unlikely(err == -ENOKEY)) {
1086 /* Drop this packet, as we don't have the encryption key
1087 * anymore and must not transmit it unencrypted. */
1088 dev_kfree_skb_any(skb);
1091 if (unlikely(err)) {
1092 b43legacyerr(dev->wl, "DMA tx mapping failure\n");
1095 if ((free_slots(ring) < SLOTS_PER_PACKET) ||
1096 should_inject_overflow(ring)) {
1097 /* This TX ring is full. */
1098 unsigned int skb_mapping = skb_get_queue_mapping(skb);
1099 ieee80211_stop_queue(dev->wl->hw, skb_mapping);
1100 dev->wl->tx_queue_stopped[skb_mapping] = 1;
1101 ring->stopped = true;
1102 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1103 b43legacydbg(dev->wl, "Stopped TX ring %d\n",
1109 void b43legacy_dma_handle_txstatus(struct b43legacy_wldev *dev,
1110 const struct b43legacy_txstatus *status)
1112 struct b43legacy_dmaring *ring;
1113 struct b43legacy_dmadesc_meta *meta;
1118 ring = parse_cookie(dev, status->cookie, &slot);
1119 if (unlikely(!ring))
1121 B43legacy_WARN_ON(!ring->tx);
1123 /* Sanity check: TX packets are processed in-order on one ring.
1124 * Check if the slot deduced from the cookie really is the first
1126 firstused = ring->current_slot - ring->used_slots + 1;
1128 firstused = ring->nr_slots + firstused;
1129 if (unlikely(slot != firstused)) {
1130 /* This possibly is a firmware bug and will result in
1131 * malfunction, memory leaks and/or stall of DMA functionality.
1133 b43legacydbg(dev->wl, "Out of order TX status report on DMA "
1134 "ring %d. Expected %d, but got %d\n",
1135 ring->index, firstused, slot);
1140 B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
1141 op32_idx2desc(ring, slot, &meta);
1144 unmap_descbuffer(ring, meta->dmaaddr,
1147 unmap_descbuffer(ring, meta->dmaaddr,
1148 sizeof(struct b43legacy_txhdr_fw3),
1151 if (meta->is_last_fragment) {
1152 struct ieee80211_tx_info *info;
1154 info = IEEE80211_SKB_CB(meta->skb);
1156 /* preserve the confiured retry limit before clearing the status
1157 * The xmit function has overwritten the rc's value with the actual
1158 * retry limit done by the hardware */
1159 retry_limit = info->status.rates[0].count;
1160 ieee80211_tx_info_clear_status(info);
1163 info->flags |= IEEE80211_TX_STAT_ACK;
1165 if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
1167 * If the short retries (RTS, not data frame) have exceeded
1168 * the limit, the hw will not have tried the selected rate,
1169 * but will have used the fallback rate instead.
1170 * Don't let the rate control count attempts for the selected
1171 * rate in this case, otherwise the statistics will be off.
1173 info->status.rates[0].count = 0;
1174 info->status.rates[1].count = status->frame_count;
1176 if (status->frame_count > retry_limit) {
1177 info->status.rates[0].count = retry_limit;
1178 info->status.rates[1].count = status->frame_count -
1182 info->status.rates[0].count = status->frame_count;
1183 info->status.rates[1].idx = -1;
1187 /* Call back to inform the ieee80211 subsystem about the
1188 * status of the transmission.
1189 * Some fields of txstat are already filled in dma_tx().
1191 ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb);
1192 /* skb is freed by ieee80211_tx_status_irqsafe() */
1195 /* No need to call free_descriptor_buffer here, as
1196 * this is only the txhdr, which is not allocated.
1198 B43legacy_WARN_ON(meta->skb != NULL);
1201 /* Everything unmapped and free'd. So it's not used anymore. */
1204 if (meta->is_last_fragment)
1206 slot = next_slot(ring, slot);
1208 dev->stats.last_tx = jiffies;
1209 if (ring->stopped) {
1210 B43legacy_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
1211 ring->stopped = false;
1214 if (dev->wl->tx_queue_stopped[ring->queue_prio]) {
1215 dev->wl->tx_queue_stopped[ring->queue_prio] = 0;
1217 /* If the driver queue is running wake the corresponding
1218 * mac80211 queue. */
1219 ieee80211_wake_queue(dev->wl->hw, ring->queue_prio);
1220 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1221 b43legacydbg(dev->wl, "Woke up TX ring %d\n",
1224 /* Add work to the queue. */
1225 ieee80211_queue_work(dev->wl->hw, &dev->wl->tx_work);
1228 static void dma_rx(struct b43legacy_dmaring *ring,
1231 struct b43legacy_dmadesc32 *desc;
1232 struct b43legacy_dmadesc_meta *meta;
1233 struct b43legacy_rxhdr_fw3 *rxhdr;
1234 struct sk_buff *skb;
1239 desc = op32_idx2desc(ring, *slot, &meta);
1241 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1244 if (ring->index == 3) {
1245 /* We received an xmit status. */
1246 struct b43legacy_hwtxstatus *hw =
1247 (struct b43legacy_hwtxstatus *)skb->data;
1250 while (hw->cookie == 0) {
1257 b43legacy_handle_hwtxstatus(ring->dev, hw);
1258 /* recycle the descriptor buffer. */
1259 sync_descbuffer_for_device(ring, meta->dmaaddr,
1260 ring->rx_buffersize);
1264 rxhdr = (struct b43legacy_rxhdr_fw3 *)skb->data;
1265 len = le16_to_cpu(rxhdr->frame_len);
1272 len = le16_to_cpu(rxhdr->frame_len);
1273 } while (len == 0 && i++ < 5);
1274 if (unlikely(len == 0)) {
1275 /* recycle the descriptor buffer. */
1276 sync_descbuffer_for_device(ring, meta->dmaaddr,
1277 ring->rx_buffersize);
1281 if (unlikely(len > ring->rx_buffersize)) {
1282 /* The data did not fit into one descriptor buffer
1283 * and is split over multiple buffers.
1284 * This should never happen, as we try to allocate buffers
1285 * big enough. So simply ignore this packet.
1291 desc = op32_idx2desc(ring, *slot, &meta);
1292 /* recycle the descriptor buffer. */
1293 sync_descbuffer_for_device(ring, meta->dmaaddr,
1294 ring->rx_buffersize);
1295 *slot = next_slot(ring, *slot);
1297 tmp -= ring->rx_buffersize;
1301 b43legacyerr(ring->dev->wl, "DMA RX buffer too small "
1302 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1303 len, ring->rx_buffersize, cnt);
1307 dmaaddr = meta->dmaaddr;
1308 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1309 if (unlikely(err)) {
1310 b43legacydbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer()"
1312 sync_descbuffer_for_device(ring, dmaaddr,
1313 ring->rx_buffersize);
1317 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1318 skb_put(skb, len + ring->frameoffset);
1319 skb_pull(skb, ring->frameoffset);
1321 b43legacy_rx(ring->dev, skb, rxhdr);
1326 void b43legacy_dma_rx(struct b43legacy_dmaring *ring)
1332 B43legacy_WARN_ON(ring->tx);
1333 current_slot = op32_get_current_rxslot(ring);
1334 B43legacy_WARN_ON(!(current_slot >= 0 && current_slot <
1337 slot = ring->current_slot;
1338 for (; slot != current_slot; slot = next_slot(ring, slot)) {
1339 dma_rx(ring, &slot);
1340 update_max_used_slots(ring, ++used_slots);
1342 op32_set_current_rxslot(ring, slot);
1343 ring->current_slot = slot;
1346 static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring *ring)
1348 B43legacy_WARN_ON(!ring->tx);
1349 op32_tx_suspend(ring);
1352 static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring *ring)
1354 B43legacy_WARN_ON(!ring->tx);
1355 op32_tx_resume(ring);
1358 void b43legacy_dma_tx_suspend(struct b43legacy_wldev *dev)
1360 b43legacy_power_saving_ctl_bits(dev, -1, 1);
1361 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring0);
1362 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring1);
1363 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring2);
1364 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring3);
1365 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring4);
1366 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring5);
1369 void b43legacy_dma_tx_resume(struct b43legacy_wldev *dev)
1371 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring5);
1372 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring4);
1373 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring3);
1374 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring2);
1375 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring1);
1376 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring0);
1377 b43legacy_power_saving_ctl_bits(dev, -1, -1);