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 /* Bcm4301-ring to mac80211-queue mapping */
217 static inline int txring_to_priority(struct b43legacy_dmaring
*ring
)
219 static const u8 idx_to_prio
[] =
220 { 3, 2, 1, 0, 4, 5, };
222 /*FIXME: have only one queue, for now */
225 return idx_to_prio
[ring
->index
];
229 static u16
b43legacy_dmacontroller_base(enum b43legacy_dmatype type
,
232 static const u16 map32
[] = {
233 B43legacy_MMIO_DMA32_BASE0
,
234 B43legacy_MMIO_DMA32_BASE1
,
235 B43legacy_MMIO_DMA32_BASE2
,
236 B43legacy_MMIO_DMA32_BASE3
,
237 B43legacy_MMIO_DMA32_BASE4
,
238 B43legacy_MMIO_DMA32_BASE5
,
241 B43legacy_WARN_ON(!(controller_idx
>= 0 &&
242 controller_idx
< ARRAY_SIZE(map32
)));
243 return map32
[controller_idx
];
247 dma_addr_t
map_descbuffer(struct b43legacy_dmaring
*ring
,
255 dmaaddr
= dma_map_single(ring
->dev
->dev
->dma_dev
,
259 dmaaddr
= dma_map_single(ring
->dev
->dev
->dma_dev
,
267 void unmap_descbuffer(struct b43legacy_dmaring
*ring
,
273 dma_unmap_single(ring
->dev
->dev
->dma_dev
,
277 dma_unmap_single(ring
->dev
->dev
->dma_dev
,
283 void sync_descbuffer_for_cpu(struct b43legacy_dmaring
*ring
,
287 B43legacy_WARN_ON(ring
->tx
);
289 dma_sync_single_for_cpu(ring
->dev
->dev
->dma_dev
,
290 addr
, len
, DMA_FROM_DEVICE
);
294 void sync_descbuffer_for_device(struct b43legacy_dmaring
*ring
,
298 B43legacy_WARN_ON(ring
->tx
);
300 dma_sync_single_for_device(ring
->dev
->dev
->dma_dev
,
301 addr
, len
, DMA_FROM_DEVICE
);
305 void free_descriptor_buffer(struct b43legacy_dmaring
*ring
,
306 struct b43legacy_dmadesc_meta
*meta
,
311 dev_kfree_skb_irq(meta
->skb
);
313 dev_kfree_skb(meta
->skb
);
318 static int alloc_ringmemory(struct b43legacy_dmaring
*ring
)
320 /* GFP flags must match the flags in free_ringmemory()! */
321 ring
->descbase
= dma_alloc_coherent(ring
->dev
->dev
->dma_dev
,
322 B43legacy_DMA_RINGMEMSIZE
,
323 &(ring
->dmabase
), GFP_KERNEL
);
330 static void free_ringmemory(struct b43legacy_dmaring
*ring
)
332 dma_free_coherent(ring
->dev
->dev
->dma_dev
, B43legacy_DMA_RINGMEMSIZE
,
333 ring
->descbase
, ring
->dmabase
);
336 /* Reset the RX DMA channel */
337 static int b43legacy_dmacontroller_rx_reset(struct b43legacy_wldev
*dev
,
339 enum b43legacy_dmatype type
)
347 offset
= B43legacy_DMA32_RXCTL
;
348 b43legacy_write32(dev
, mmio_base
+ offset
, 0);
349 for (i
= 0; i
< 10; i
++) {
350 offset
= B43legacy_DMA32_RXSTATUS
;
351 value
= b43legacy_read32(dev
, mmio_base
+ offset
);
352 value
&= B43legacy_DMA32_RXSTATE
;
353 if (value
== B43legacy_DMA32_RXSTAT_DISABLED
) {
360 b43legacyerr(dev
->wl
, "DMA RX reset timed out\n");
367 /* Reset the RX DMA channel */
368 static int b43legacy_dmacontroller_tx_reset(struct b43legacy_wldev
*dev
,
370 enum b43legacy_dmatype type
)
378 for (i
= 0; i
< 10; i
++) {
379 offset
= B43legacy_DMA32_TXSTATUS
;
380 value
= b43legacy_read32(dev
, mmio_base
+ offset
);
381 value
&= B43legacy_DMA32_TXSTATE
;
382 if (value
== B43legacy_DMA32_TXSTAT_DISABLED
||
383 value
== B43legacy_DMA32_TXSTAT_IDLEWAIT
||
384 value
== B43legacy_DMA32_TXSTAT_STOPPED
)
388 offset
= B43legacy_DMA32_TXCTL
;
389 b43legacy_write32(dev
, mmio_base
+ offset
, 0);
390 for (i
= 0; i
< 10; i
++) {
391 offset
= B43legacy_DMA32_TXSTATUS
;
392 value
= b43legacy_read32(dev
, mmio_base
+ offset
);
393 value
&= B43legacy_DMA32_TXSTATE
;
394 if (value
== B43legacy_DMA32_TXSTAT_DISABLED
) {
401 b43legacyerr(dev
->wl
, "DMA TX reset timed out\n");
404 /* ensure the reset is completed. */
410 /* Check if a DMA mapping address is invalid. */
411 static bool b43legacy_dma_mapping_error(struct b43legacy_dmaring
*ring
,
416 if (unlikely(dma_mapping_error(ring
->dev
->dev
->dma_dev
, addr
)))
419 switch (ring
->type
) {
420 case B43legacy_DMA_30BIT
:
421 if ((u64
)addr
+ buffersize
> (1ULL << 30))
424 case B43legacy_DMA_32BIT
:
425 if ((u64
)addr
+ buffersize
> (1ULL << 32))
430 /* The address is OK. */
434 /* We can't support this address. Unmap it again. */
435 unmap_descbuffer(ring
, addr
, buffersize
, dma_to_device
);
440 static int setup_rx_descbuffer(struct b43legacy_dmaring
*ring
,
441 struct b43legacy_dmadesc32
*desc
,
442 struct b43legacy_dmadesc_meta
*meta
,
445 struct b43legacy_rxhdr_fw3
*rxhdr
;
446 struct b43legacy_hwtxstatus
*txstat
;
450 B43legacy_WARN_ON(ring
->tx
);
452 skb
= __dev_alloc_skb(ring
->rx_buffersize
, gfp_flags
);
455 dmaaddr
= map_descbuffer(ring
, skb
->data
,
456 ring
->rx_buffersize
, 0);
457 if (b43legacy_dma_mapping_error(ring
, dmaaddr
, ring
->rx_buffersize
, 0)) {
458 /* ugh. try to realloc in zone_dma */
459 gfp_flags
|= GFP_DMA
;
461 dev_kfree_skb_any(skb
);
463 skb
= __dev_alloc_skb(ring
->rx_buffersize
, gfp_flags
);
466 dmaaddr
= map_descbuffer(ring
, skb
->data
,
467 ring
->rx_buffersize
, 0);
470 if (b43legacy_dma_mapping_error(ring
, dmaaddr
, ring
->rx_buffersize
, 0)) {
471 dev_kfree_skb_any(skb
);
476 meta
->dmaaddr
= dmaaddr
;
477 op32_fill_descriptor(ring
, desc
, dmaaddr
, ring
->rx_buffersize
, 0, 0, 0);
479 rxhdr
= (struct b43legacy_rxhdr_fw3
*)(skb
->data
);
480 rxhdr
->frame_len
= 0;
481 txstat
= (struct b43legacy_hwtxstatus
*)(skb
->data
);
487 /* Allocate the initial descbuffers.
488 * This is used for an RX ring only.
490 static int alloc_initial_descbuffers(struct b43legacy_dmaring
*ring
)
494 struct b43legacy_dmadesc32
*desc
;
495 struct b43legacy_dmadesc_meta
*meta
;
497 for (i
= 0; i
< ring
->nr_slots
; i
++) {
498 desc
= op32_idx2desc(ring
, i
, &meta
);
500 err
= setup_rx_descbuffer(ring
, desc
, meta
, GFP_KERNEL
);
502 b43legacyerr(ring
->dev
->wl
,
503 "Failed to allocate initial descbuffers\n");
507 mb(); /* all descbuffer setup before next line */
508 ring
->used_slots
= ring
->nr_slots
;
514 for (i
--; i
>= 0; i
--) {
515 desc
= op32_idx2desc(ring
, i
, &meta
);
517 unmap_descbuffer(ring
, meta
->dmaaddr
, ring
->rx_buffersize
, 0);
518 dev_kfree_skb(meta
->skb
);
523 /* Do initial setup of the DMA controller.
524 * Reset the controller, write the ring busaddress
525 * and switch the "enable" bit on.
527 static int dmacontroller_setup(struct b43legacy_dmaring
*ring
)
532 u32 trans
= ring
->dev
->dma
.translation
;
533 u32 ringbase
= (u32
)(ring
->dmabase
);
536 addrext
= (ringbase
& SSB_DMA_TRANSLATION_MASK
)
537 >> SSB_DMA_TRANSLATION_SHIFT
;
538 value
= B43legacy_DMA32_TXENABLE
;
539 value
|= (addrext
<< B43legacy_DMA32_TXADDREXT_SHIFT
)
540 & B43legacy_DMA32_TXADDREXT_MASK
;
541 b43legacy_dma_write(ring
, B43legacy_DMA32_TXCTL
, value
);
542 b43legacy_dma_write(ring
, B43legacy_DMA32_TXRING
,
543 (ringbase
& ~SSB_DMA_TRANSLATION_MASK
)
546 err
= alloc_initial_descbuffers(ring
);
550 addrext
= (ringbase
& SSB_DMA_TRANSLATION_MASK
)
551 >> SSB_DMA_TRANSLATION_SHIFT
;
552 value
= (ring
->frameoffset
<<
553 B43legacy_DMA32_RXFROFF_SHIFT
);
554 value
|= B43legacy_DMA32_RXENABLE
;
555 value
|= (addrext
<< B43legacy_DMA32_RXADDREXT_SHIFT
)
556 & B43legacy_DMA32_RXADDREXT_MASK
;
557 b43legacy_dma_write(ring
, B43legacy_DMA32_RXCTL
, value
);
558 b43legacy_dma_write(ring
, B43legacy_DMA32_RXRING
,
559 (ringbase
& ~SSB_DMA_TRANSLATION_MASK
)
561 b43legacy_dma_write(ring
, B43legacy_DMA32_RXINDEX
, 200);
568 /* Shutdown the DMA controller. */
569 static void dmacontroller_cleanup(struct b43legacy_dmaring
*ring
)
572 b43legacy_dmacontroller_tx_reset(ring
->dev
, ring
->mmio_base
,
574 b43legacy_dma_write(ring
, B43legacy_DMA32_TXRING
, 0);
576 b43legacy_dmacontroller_rx_reset(ring
->dev
, ring
->mmio_base
,
578 b43legacy_dma_write(ring
, B43legacy_DMA32_RXRING
, 0);
582 static void free_all_descbuffers(struct b43legacy_dmaring
*ring
)
584 struct b43legacy_dmadesc_meta
*meta
;
587 if (!ring
->used_slots
)
589 for (i
= 0; i
< ring
->nr_slots
; i
++) {
590 op32_idx2desc(ring
, i
, &meta
);
593 B43legacy_WARN_ON(!ring
->tx
);
597 unmap_descbuffer(ring
, meta
->dmaaddr
,
600 unmap_descbuffer(ring
, meta
->dmaaddr
,
601 ring
->rx_buffersize
, 0);
602 free_descriptor_buffer(ring
, meta
, 0);
606 static enum b43legacy_dmatype
b43legacy_engine_type(struct b43legacy_wldev
*dev
)
611 mmio_base
= b43legacy_dmacontroller_base(0, 0);
612 b43legacy_write32(dev
,
613 mmio_base
+ B43legacy_DMA32_TXCTL
,
614 B43legacy_DMA32_TXADDREXT_MASK
);
615 tmp
= b43legacy_read32(dev
, mmio_base
+
616 B43legacy_DMA32_TXCTL
);
617 if (tmp
& B43legacy_DMA32_TXADDREXT_MASK
)
618 return B43legacy_DMA_32BIT
;
619 return B43legacy_DMA_30BIT
;
622 /* Main initialization function. */
624 struct b43legacy_dmaring
*b43legacy_setup_dmaring(struct b43legacy_wldev
*dev
,
625 int controller_index
,
627 enum b43legacy_dmatype type
)
629 struct b43legacy_dmaring
*ring
;
634 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
640 nr_slots
= B43legacy_RXRING_SLOTS
;
642 nr_slots
= B43legacy_TXRING_SLOTS
;
644 ring
->meta
= kcalloc(nr_slots
, sizeof(struct b43legacy_dmadesc_meta
),
649 ring
->txhdr_cache
= kcalloc(nr_slots
,
650 sizeof(struct b43legacy_txhdr_fw3
),
652 if (!ring
->txhdr_cache
)
655 /* test for ability to dma to txhdr_cache */
656 dma_test
= dma_map_single(dev
->dev
->dma_dev
, ring
->txhdr_cache
,
657 sizeof(struct b43legacy_txhdr_fw3
),
660 if (b43legacy_dma_mapping_error(ring
, dma_test
,
661 sizeof(struct b43legacy_txhdr_fw3
), 1)) {
663 kfree(ring
->txhdr_cache
);
664 ring
->txhdr_cache
= kcalloc(nr_slots
,
665 sizeof(struct b43legacy_txhdr_fw3
),
666 GFP_KERNEL
| GFP_DMA
);
667 if (!ring
->txhdr_cache
)
670 dma_test
= dma_map_single(dev
->dev
->dma_dev
,
672 sizeof(struct b43legacy_txhdr_fw3
),
675 if (b43legacy_dma_mapping_error(ring
, dma_test
,
676 sizeof(struct b43legacy_txhdr_fw3
), 1))
677 goto err_kfree_txhdr_cache
;
680 dma_unmap_single(dev
->dev
->dma_dev
, dma_test
,
681 sizeof(struct b43legacy_txhdr_fw3
),
685 ring
->nr_slots
= nr_slots
;
686 ring
->mmio_base
= b43legacy_dmacontroller_base(type
, controller_index
);
687 ring
->index
= controller_index
;
690 ring
->current_slot
= -1;
692 if (ring
->index
== 0) {
693 ring
->rx_buffersize
= B43legacy_DMA0_RX_BUFFERSIZE
;
694 ring
->frameoffset
= B43legacy_DMA0_RX_FRAMEOFFSET
;
695 } else if (ring
->index
== 3) {
696 ring
->rx_buffersize
= B43legacy_DMA3_RX_BUFFERSIZE
;
697 ring
->frameoffset
= B43legacy_DMA3_RX_FRAMEOFFSET
;
699 B43legacy_WARN_ON(1);
701 #ifdef CONFIG_B43LEGACY_DEBUG
702 ring
->last_injected_overflow
= jiffies
;
705 err
= alloc_ringmemory(ring
);
707 goto err_kfree_txhdr_cache
;
708 err
= dmacontroller_setup(ring
);
710 goto err_free_ringmemory
;
716 free_ringmemory(ring
);
717 err_kfree_txhdr_cache
:
718 kfree(ring
->txhdr_cache
);
727 /* Main cleanup function. */
728 static void b43legacy_destroy_dmaring(struct b43legacy_dmaring
*ring
)
733 b43legacydbg(ring
->dev
->wl
, "DMA-%u 0x%04X (%s) max used slots:"
734 " %d/%d\n", (unsigned int)(ring
->type
), ring
->mmio_base
,
735 (ring
->tx
) ? "TX" : "RX", ring
->max_used_slots
,
737 /* Device IRQs are disabled prior entering this function,
738 * so no need to take care of concurrency with rx handler stuff.
740 dmacontroller_cleanup(ring
);
741 free_all_descbuffers(ring
);
742 free_ringmemory(ring
);
744 kfree(ring
->txhdr_cache
);
749 void b43legacy_dma_free(struct b43legacy_wldev
*dev
)
751 struct b43legacy_dma
*dma
;
753 if (b43legacy_using_pio(dev
))
757 b43legacy_destroy_dmaring(dma
->rx_ring3
);
758 dma
->rx_ring3
= NULL
;
759 b43legacy_destroy_dmaring(dma
->rx_ring0
);
760 dma
->rx_ring0
= NULL
;
762 b43legacy_destroy_dmaring(dma
->tx_ring5
);
763 dma
->tx_ring5
= NULL
;
764 b43legacy_destroy_dmaring(dma
->tx_ring4
);
765 dma
->tx_ring4
= NULL
;
766 b43legacy_destroy_dmaring(dma
->tx_ring3
);
767 dma
->tx_ring3
= NULL
;
768 b43legacy_destroy_dmaring(dma
->tx_ring2
);
769 dma
->tx_ring2
= NULL
;
770 b43legacy_destroy_dmaring(dma
->tx_ring1
);
771 dma
->tx_ring1
= NULL
;
772 b43legacy_destroy_dmaring(dma
->tx_ring0
);
773 dma
->tx_ring0
= NULL
;
776 int b43legacy_dma_init(struct b43legacy_wldev
*dev
)
778 struct b43legacy_dma
*dma
= &dev
->dma
;
779 struct b43legacy_dmaring
*ring
;
780 enum b43legacy_dmatype type
= b43legacy_engine_type(dev
);
783 err
= dma_set_mask_and_coherent(dev
->dev
->dma_dev
, DMA_BIT_MASK(type
));
785 #ifdef CONFIG_B43LEGACY_PIO
786 b43legacywarn(dev
->wl
, "DMA for this device not supported. "
787 "Falling back to PIO\n");
788 dev
->__using_pio
= true;
791 b43legacyerr(dev
->wl
, "DMA for this device not supported and "
792 "no PIO support compiled in\n");
796 dma
->translation
= ssb_dma_translation(dev
->dev
);
799 /* setup TX DMA channels. */
800 ring
= b43legacy_setup_dmaring(dev
, 0, 1, type
);
803 dma
->tx_ring0
= ring
;
805 ring
= b43legacy_setup_dmaring(dev
, 1, 1, type
);
807 goto err_destroy_tx0
;
808 dma
->tx_ring1
= ring
;
810 ring
= b43legacy_setup_dmaring(dev
, 2, 1, type
);
812 goto err_destroy_tx1
;
813 dma
->tx_ring2
= ring
;
815 ring
= b43legacy_setup_dmaring(dev
, 3, 1, type
);
817 goto err_destroy_tx2
;
818 dma
->tx_ring3
= ring
;
820 ring
= b43legacy_setup_dmaring(dev
, 4, 1, type
);
822 goto err_destroy_tx3
;
823 dma
->tx_ring4
= ring
;
825 ring
= b43legacy_setup_dmaring(dev
, 5, 1, type
);
827 goto err_destroy_tx4
;
828 dma
->tx_ring5
= ring
;
830 /* setup RX DMA channels. */
831 ring
= b43legacy_setup_dmaring(dev
, 0, 0, type
);
833 goto err_destroy_tx5
;
834 dma
->rx_ring0
= ring
;
836 if (dev
->dev
->id
.revision
< 5) {
837 ring
= b43legacy_setup_dmaring(dev
, 3, 0, type
);
839 goto err_destroy_rx0
;
840 dma
->rx_ring3
= ring
;
843 b43legacydbg(dev
->wl
, "%u-bit DMA initialized\n", (unsigned int)type
);
849 b43legacy_destroy_dmaring(dma
->rx_ring0
);
850 dma
->rx_ring0
= NULL
;
852 b43legacy_destroy_dmaring(dma
->tx_ring5
);
853 dma
->tx_ring5
= NULL
;
855 b43legacy_destroy_dmaring(dma
->tx_ring4
);
856 dma
->tx_ring4
= NULL
;
858 b43legacy_destroy_dmaring(dma
->tx_ring3
);
859 dma
->tx_ring3
= NULL
;
861 b43legacy_destroy_dmaring(dma
->tx_ring2
);
862 dma
->tx_ring2
= NULL
;
864 b43legacy_destroy_dmaring(dma
->tx_ring1
);
865 dma
->tx_ring1
= NULL
;
867 b43legacy_destroy_dmaring(dma
->tx_ring0
);
868 dma
->tx_ring0
= NULL
;
872 /* Generate a cookie for the TX header. */
873 static u16
generate_cookie(struct b43legacy_dmaring
*ring
,
878 /* Use the upper 4 bits of the cookie as
879 * DMA controller ID and store the slot number
880 * in the lower 12 bits.
881 * Note that the cookie must never be 0, as this
882 * is a special value used in RX path.
884 switch (ring
->index
) {
904 B43legacy_WARN_ON(!(((u16
)slot
& 0xF000) == 0x0000));
910 /* Inspect a cookie and find out to which controller/slot it belongs. */
912 struct b43legacy_dmaring
*parse_cookie(struct b43legacy_wldev
*dev
,
913 u16 cookie
, int *slot
)
915 struct b43legacy_dma
*dma
= &dev
->dma
;
916 struct b43legacy_dmaring
*ring
= NULL
;
918 switch (cookie
& 0xF000) {
920 ring
= dma
->tx_ring0
;
923 ring
= dma
->tx_ring1
;
926 ring
= dma
->tx_ring2
;
929 ring
= dma
->tx_ring3
;
932 ring
= dma
->tx_ring4
;
935 ring
= dma
->tx_ring5
;
938 B43legacy_WARN_ON(1);
940 *slot
= (cookie
& 0x0FFF);
941 B43legacy_WARN_ON(!(ring
&& *slot
>= 0 && *slot
< ring
->nr_slots
));
946 static int dma_tx_fragment(struct b43legacy_dmaring
*ring
,
947 struct sk_buff
**in_skb
)
949 struct sk_buff
*skb
= *in_skb
;
950 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
952 int slot
, old_top_slot
, old_used_slots
;
954 struct b43legacy_dmadesc32
*desc
;
955 struct b43legacy_dmadesc_meta
*meta
;
956 struct b43legacy_dmadesc_meta
*meta_hdr
;
957 struct sk_buff
*bounce_skb
;
959 #define SLOTS_PER_PACKET 2
960 B43legacy_WARN_ON(skb_shinfo(skb
)->nr_frags
!= 0);
962 old_top_slot
= ring
->current_slot
;
963 old_used_slots
= ring
->used_slots
;
965 /* Get a slot for the header. */
966 slot
= request_slot(ring
);
967 desc
= op32_idx2desc(ring
, slot
, &meta_hdr
);
968 memset(meta_hdr
, 0, sizeof(*meta_hdr
));
970 header
= &(ring
->txhdr_cache
[slot
* sizeof(
971 struct b43legacy_txhdr_fw3
)]);
972 err
= b43legacy_generate_txhdr(ring
->dev
, header
,
973 skb
->data
, skb
->len
, info
,
974 generate_cookie(ring
, slot
));
976 ring
->current_slot
= old_top_slot
;
977 ring
->used_slots
= old_used_slots
;
981 meta_hdr
->dmaaddr
= map_descbuffer(ring
, (unsigned char *)header
,
982 sizeof(struct b43legacy_txhdr_fw3
), 1);
983 if (b43legacy_dma_mapping_error(ring
, meta_hdr
->dmaaddr
,
984 sizeof(struct b43legacy_txhdr_fw3
), 1)) {
985 ring
->current_slot
= old_top_slot
;
986 ring
->used_slots
= old_used_slots
;
989 op32_fill_descriptor(ring
, desc
, meta_hdr
->dmaaddr
,
990 sizeof(struct b43legacy_txhdr_fw3
), 1, 0, 0);
992 /* Get a slot for the payload. */
993 slot
= request_slot(ring
);
994 desc
= op32_idx2desc(ring
, slot
, &meta
);
995 memset(meta
, 0, sizeof(*meta
));
998 meta
->is_last_fragment
= true;
1000 meta
->dmaaddr
= map_descbuffer(ring
, skb
->data
, skb
->len
, 1);
1001 /* create a bounce buffer in zone_dma on mapping failure. */
1002 if (b43legacy_dma_mapping_error(ring
, meta
->dmaaddr
, skb
->len
, 1)) {
1003 bounce_skb
= alloc_skb(skb
->len
, GFP_KERNEL
| GFP_DMA
);
1005 ring
->current_slot
= old_top_slot
;
1006 ring
->used_slots
= old_used_slots
;
1011 skb_put_data(bounce_skb
, skb
->data
, skb
->len
);
1012 memcpy(bounce_skb
->cb
, skb
->cb
, sizeof(skb
->cb
));
1013 bounce_skb
->dev
= skb
->dev
;
1014 skb_set_queue_mapping(bounce_skb
, skb_get_queue_mapping(skb
));
1015 info
= IEEE80211_SKB_CB(bounce_skb
);
1017 dev_kfree_skb_any(skb
);
1019 *in_skb
= bounce_skb
;
1021 meta
->dmaaddr
= map_descbuffer(ring
, skb
->data
, skb
->len
, 1);
1022 if (b43legacy_dma_mapping_error(ring
, meta
->dmaaddr
, skb
->len
, 1)) {
1023 ring
->current_slot
= old_top_slot
;
1024 ring
->used_slots
= old_used_slots
;
1026 goto out_free_bounce
;
1030 op32_fill_descriptor(ring
, desc
, meta
->dmaaddr
,
1033 wmb(); /* previous stuff MUST be done */
1034 /* Now transfer the whole frame. */
1035 op32_poke_tx(ring
, next_slot(ring
, slot
));
1039 dev_kfree_skb_any(skb
);
1041 unmap_descbuffer(ring
, meta_hdr
->dmaaddr
,
1042 sizeof(struct b43legacy_txhdr_fw3
), 1);
1047 int should_inject_overflow(struct b43legacy_dmaring
*ring
)
1049 #ifdef CONFIG_B43LEGACY_DEBUG
1050 if (unlikely(b43legacy_debug(ring
->dev
,
1051 B43legacy_DBG_DMAOVERFLOW
))) {
1052 /* Check if we should inject another ringbuffer overflow
1053 * to test handling of this situation in the stack. */
1054 unsigned long next_overflow
;
1056 next_overflow
= ring
->last_injected_overflow
+ HZ
;
1057 if (time_after(jiffies
, next_overflow
)) {
1058 ring
->last_injected_overflow
= jiffies
;
1059 b43legacydbg(ring
->dev
->wl
,
1060 "Injecting TX ring overflow on "
1061 "DMA controller %d\n", ring
->index
);
1065 #endif /* CONFIG_B43LEGACY_DEBUG */
1069 int b43legacy_dma_tx(struct b43legacy_wldev
*dev
,
1070 struct sk_buff
*skb
)
1072 struct b43legacy_dmaring
*ring
;
1075 ring
= priority_to_txring(dev
, skb_get_queue_mapping(skb
));
1076 B43legacy_WARN_ON(!ring
->tx
);
1078 if (unlikely(ring
->stopped
)) {
1079 /* We get here only because of a bug in mac80211.
1080 * Because of a race, one packet may be queued after
1081 * the queue is stopped, thus we got called when we shouldn't.
1082 * For now, just refuse the transmit. */
1083 if (b43legacy_debug(dev
, B43legacy_DBG_DMAVERBOSE
))
1084 b43legacyerr(dev
->wl
, "Packet after queue stopped\n");
1088 if (WARN_ON(free_slots(ring
) < SLOTS_PER_PACKET
)) {
1089 /* If we get here, we have a real error with the queue
1090 * full, but queues not stopped. */
1091 b43legacyerr(dev
->wl
, "DMA queue overflow\n");
1095 /* dma_tx_fragment might reallocate the skb, so invalidate pointers pointing
1096 * into the skb data or cb now. */
1097 err
= dma_tx_fragment(ring
, &skb
);
1098 if (unlikely(err
== -ENOKEY
)) {
1099 /* Drop this packet, as we don't have the encryption key
1100 * anymore and must not transmit it unencrypted. */
1101 dev_kfree_skb_any(skb
);
1104 if (unlikely(err
)) {
1105 b43legacyerr(dev
->wl
, "DMA tx mapping failure\n");
1108 if ((free_slots(ring
) < SLOTS_PER_PACKET
) ||
1109 should_inject_overflow(ring
)) {
1110 /* This TX ring is full. */
1111 unsigned int skb_mapping
= skb_get_queue_mapping(skb
);
1112 ieee80211_stop_queue(dev
->wl
->hw
, skb_mapping
);
1113 dev
->wl
->tx_queue_stopped
[skb_mapping
] = 1;
1114 ring
->stopped
= true;
1115 if (b43legacy_debug(dev
, B43legacy_DBG_DMAVERBOSE
))
1116 b43legacydbg(dev
->wl
, "Stopped TX ring %d\n",
1122 void b43legacy_dma_handle_txstatus(struct b43legacy_wldev
*dev
,
1123 const struct b43legacy_txstatus
*status
)
1125 struct b43legacy_dmaring
*ring
;
1126 struct b43legacy_dmadesc_meta
*meta
;
1131 ring
= parse_cookie(dev
, status
->cookie
, &slot
);
1132 if (unlikely(!ring
))
1134 B43legacy_WARN_ON(!ring
->tx
);
1136 /* Sanity check: TX packets are processed in-order on one ring.
1137 * Check if the slot deduced from the cookie really is the first
1139 firstused
= ring
->current_slot
- ring
->used_slots
+ 1;
1141 firstused
= ring
->nr_slots
+ firstused
;
1142 if (unlikely(slot
!= firstused
)) {
1143 /* This possibly is a firmware bug and will result in
1144 * malfunction, memory leaks and/or stall of DMA functionality.
1146 b43legacydbg(dev
->wl
, "Out of order TX status report on DMA "
1147 "ring %d. Expected %d, but got %d\n",
1148 ring
->index
, firstused
, slot
);
1153 B43legacy_WARN_ON(!(slot
>= 0 && slot
< ring
->nr_slots
));
1154 op32_idx2desc(ring
, slot
, &meta
);
1157 unmap_descbuffer(ring
, meta
->dmaaddr
,
1160 unmap_descbuffer(ring
, meta
->dmaaddr
,
1161 sizeof(struct b43legacy_txhdr_fw3
),
1164 if (meta
->is_last_fragment
) {
1165 struct ieee80211_tx_info
*info
;
1167 info
= IEEE80211_SKB_CB(meta
->skb
);
1169 /* preserve the confiured retry limit before clearing the status
1170 * The xmit function has overwritten the rc's value with the actual
1171 * retry limit done by the hardware */
1172 retry_limit
= info
->status
.rates
[0].count
;
1173 ieee80211_tx_info_clear_status(info
);
1176 info
->flags
|= IEEE80211_TX_STAT_ACK
;
1178 if (status
->rts_count
> dev
->wl
->hw
->conf
.short_frame_max_tx_count
) {
1180 * If the short retries (RTS, not data frame) have exceeded
1181 * the limit, the hw will not have tried the selected rate,
1182 * but will have used the fallback rate instead.
1183 * Don't let the rate control count attempts for the selected
1184 * rate in this case, otherwise the statistics will be off.
1186 info
->status
.rates
[0].count
= 0;
1187 info
->status
.rates
[1].count
= status
->frame_count
;
1189 if (status
->frame_count
> retry_limit
) {
1190 info
->status
.rates
[0].count
= retry_limit
;
1191 info
->status
.rates
[1].count
= status
->frame_count
-
1195 info
->status
.rates
[0].count
= status
->frame_count
;
1196 info
->status
.rates
[1].idx
= -1;
1200 /* Call back to inform the ieee80211 subsystem about the
1201 * status of the transmission.
1202 * Some fields of txstat are already filled in dma_tx().
1204 ieee80211_tx_status_irqsafe(dev
->wl
->hw
, meta
->skb
);
1205 /* skb is freed by ieee80211_tx_status_irqsafe() */
1208 /* No need to call free_descriptor_buffer here, as
1209 * this is only the txhdr, which is not allocated.
1211 B43legacy_WARN_ON(meta
->skb
!= NULL
);
1214 /* Everything unmapped and free'd. So it's not used anymore. */
1217 if (meta
->is_last_fragment
)
1219 slot
= next_slot(ring
, slot
);
1221 dev
->stats
.last_tx
= jiffies
;
1222 if (ring
->stopped
) {
1223 B43legacy_WARN_ON(free_slots(ring
) < SLOTS_PER_PACKET
);
1224 ring
->stopped
= false;
1227 if (dev
->wl
->tx_queue_stopped
[ring
->queue_prio
]) {
1228 dev
->wl
->tx_queue_stopped
[ring
->queue_prio
] = 0;
1230 /* If the driver queue is running wake the corresponding
1231 * mac80211 queue. */
1232 ieee80211_wake_queue(dev
->wl
->hw
, ring
->queue_prio
);
1233 if (b43legacy_debug(dev
, B43legacy_DBG_DMAVERBOSE
))
1234 b43legacydbg(dev
->wl
, "Woke up TX ring %d\n",
1237 /* Add work to the queue. */
1238 ieee80211_queue_work(dev
->wl
->hw
, &dev
->wl
->tx_work
);
1241 static void dma_rx(struct b43legacy_dmaring
*ring
,
1244 struct b43legacy_dmadesc32
*desc
;
1245 struct b43legacy_dmadesc_meta
*meta
;
1246 struct b43legacy_rxhdr_fw3
*rxhdr
;
1247 struct sk_buff
*skb
;
1252 desc
= op32_idx2desc(ring
, *slot
, &meta
);
1254 sync_descbuffer_for_cpu(ring
, meta
->dmaaddr
, ring
->rx_buffersize
);
1257 if (ring
->index
== 3) {
1258 /* We received an xmit status. */
1259 struct b43legacy_hwtxstatus
*hw
=
1260 (struct b43legacy_hwtxstatus
*)skb
->data
;
1263 while (hw
->cookie
== 0) {
1270 b43legacy_handle_hwtxstatus(ring
->dev
, hw
);
1271 /* recycle the descriptor buffer. */
1272 sync_descbuffer_for_device(ring
, meta
->dmaaddr
,
1273 ring
->rx_buffersize
);
1277 rxhdr
= (struct b43legacy_rxhdr_fw3
*)skb
->data
;
1278 len
= le16_to_cpu(rxhdr
->frame_len
);
1285 len
= le16_to_cpu(rxhdr
->frame_len
);
1286 } while (len
== 0 && i
++ < 5);
1287 if (unlikely(len
== 0)) {
1288 /* recycle the descriptor buffer. */
1289 sync_descbuffer_for_device(ring
, meta
->dmaaddr
,
1290 ring
->rx_buffersize
);
1294 if (unlikely(len
> ring
->rx_buffersize
)) {
1295 /* The data did not fit into one descriptor buffer
1296 * and is split over multiple buffers.
1297 * This should never happen, as we try to allocate buffers
1298 * big enough. So simply ignore this packet.
1304 desc
= op32_idx2desc(ring
, *slot
, &meta
);
1305 /* recycle the descriptor buffer. */
1306 sync_descbuffer_for_device(ring
, meta
->dmaaddr
,
1307 ring
->rx_buffersize
);
1308 *slot
= next_slot(ring
, *slot
);
1310 tmp
-= ring
->rx_buffersize
;
1314 b43legacyerr(ring
->dev
->wl
, "DMA RX buffer too small "
1315 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1316 len
, ring
->rx_buffersize
, cnt
);
1320 dmaaddr
= meta
->dmaaddr
;
1321 err
= setup_rx_descbuffer(ring
, desc
, meta
, GFP_ATOMIC
);
1322 if (unlikely(err
)) {
1323 b43legacydbg(ring
->dev
->wl
, "DMA RX: setup_rx_descbuffer()"
1325 sync_descbuffer_for_device(ring
, dmaaddr
,
1326 ring
->rx_buffersize
);
1330 unmap_descbuffer(ring
, dmaaddr
, ring
->rx_buffersize
, 0);
1331 skb_put(skb
, len
+ ring
->frameoffset
);
1332 skb_pull(skb
, ring
->frameoffset
);
1334 b43legacy_rx(ring
->dev
, skb
, rxhdr
);
1339 void b43legacy_dma_rx(struct b43legacy_dmaring
*ring
)
1345 B43legacy_WARN_ON(ring
->tx
);
1346 current_slot
= op32_get_current_rxslot(ring
);
1347 B43legacy_WARN_ON(!(current_slot
>= 0 && current_slot
<
1350 slot
= ring
->current_slot
;
1351 for (; slot
!= current_slot
; slot
= next_slot(ring
, slot
)) {
1352 dma_rx(ring
, &slot
);
1353 update_max_used_slots(ring
, ++used_slots
);
1355 op32_set_current_rxslot(ring
, slot
);
1356 ring
->current_slot
= slot
;
1359 static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring
*ring
)
1361 B43legacy_WARN_ON(!ring
->tx
);
1362 op32_tx_suspend(ring
);
1365 static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring
*ring
)
1367 B43legacy_WARN_ON(!ring
->tx
);
1368 op32_tx_resume(ring
);
1371 void b43legacy_dma_tx_suspend(struct b43legacy_wldev
*dev
)
1373 b43legacy_power_saving_ctl_bits(dev
, -1, 1);
1374 b43legacy_dma_tx_suspend_ring(dev
->dma
.tx_ring0
);
1375 b43legacy_dma_tx_suspend_ring(dev
->dma
.tx_ring1
);
1376 b43legacy_dma_tx_suspend_ring(dev
->dma
.tx_ring2
);
1377 b43legacy_dma_tx_suspend_ring(dev
->dma
.tx_ring3
);
1378 b43legacy_dma_tx_suspend_ring(dev
->dma
.tx_ring4
);
1379 b43legacy_dma_tx_suspend_ring(dev
->dma
.tx_ring5
);
1382 void b43legacy_dma_tx_resume(struct b43legacy_wldev
*dev
)
1384 b43legacy_dma_tx_resume_ring(dev
->dma
.tx_ring5
);
1385 b43legacy_dma_tx_resume_ring(dev
->dma
.tx_ring4
);
1386 b43legacy_dma_tx_resume_ring(dev
->dma
.tx_ring3
);
1387 b43legacy_dma_tx_resume_ring(dev
->dma
.tx_ring2
);
1388 b43legacy_dma_tx_resume_ring(dev
->dma
.tx_ring1
);
1389 b43legacy_dma_tx_resume_ring(dev
->dma
.tx_ring0
);
1390 b43legacy_power_saving_ctl_bits(dev
, -1, -1);