1 // SPDX-License-Identifier: GPL-2.0-or-later
4 Broadcom B43 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
23 #include <linux/dma-mapping.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
26 #include <linux/skbuff.h>
27 #include <linux/etherdevice.h>
28 #include <linux/slab.h>
29 #include <asm/div64.h>
32 /* Required number of TX DMA slots per TX frame.
33 * This currently is 2, because we put the header and the ieee80211 frame
34 * into separate slots. */
35 #define TX_SLOTS_PER_FRAME 2
37 static u32
b43_dma_address(struct b43_dma
*dma
, dma_addr_t dmaaddr
,
38 enum b43_addrtype addrtype
)
43 case B43_DMA_ADDR_LOW
:
44 addr
= lower_32_bits(dmaaddr
);
45 if (dma
->translation_in_low
) {
46 addr
&= ~SSB_DMA_TRANSLATION_MASK
;
47 addr
|= dma
->translation
;
50 case B43_DMA_ADDR_HIGH
:
51 addr
= upper_32_bits(dmaaddr
);
52 if (!dma
->translation_in_low
) {
53 addr
&= ~SSB_DMA_TRANSLATION_MASK
;
54 addr
|= dma
->translation
;
57 case B43_DMA_ADDR_EXT
:
58 if (dma
->translation_in_low
)
59 addr
= lower_32_bits(dmaaddr
);
61 addr
= upper_32_bits(dmaaddr
);
62 addr
&= SSB_DMA_TRANSLATION_MASK
;
63 addr
>>= SSB_DMA_TRANSLATION_SHIFT
;
72 struct b43_dmadesc_generic
*op32_idx2desc(struct b43_dmaring
*ring
,
74 struct b43_dmadesc_meta
**meta
)
76 struct b43_dmadesc32
*desc
;
78 *meta
= &(ring
->meta
[slot
]);
79 desc
= ring
->descbase
;
82 return (struct b43_dmadesc_generic
*)desc
;
85 static void op32_fill_descriptor(struct b43_dmaring
*ring
,
86 struct b43_dmadesc_generic
*desc
,
87 dma_addr_t dmaaddr
, u16 bufsize
,
88 int start
, int end
, int irq
)
90 struct b43_dmadesc32
*descbase
= ring
->descbase
;
96 slot
= (int)(&(desc
->dma32
) - descbase
);
97 B43_WARN_ON(!(slot
>= 0 && slot
< ring
->nr_slots
));
99 addr
= b43_dma_address(&ring
->dev
->dma
, dmaaddr
, B43_DMA_ADDR_LOW
);
100 addrext
= b43_dma_address(&ring
->dev
->dma
, dmaaddr
, B43_DMA_ADDR_EXT
);
102 ctl
= bufsize
& B43_DMA32_DCTL_BYTECNT
;
103 if (slot
== ring
->nr_slots
- 1)
104 ctl
|= B43_DMA32_DCTL_DTABLEEND
;
106 ctl
|= B43_DMA32_DCTL_FRAMESTART
;
108 ctl
|= B43_DMA32_DCTL_FRAMEEND
;
110 ctl
|= B43_DMA32_DCTL_IRQ
;
111 ctl
|= (addrext
<< B43_DMA32_DCTL_ADDREXT_SHIFT
)
112 & B43_DMA32_DCTL_ADDREXT_MASK
;
114 desc
->dma32
.control
= cpu_to_le32(ctl
);
115 desc
->dma32
.address
= cpu_to_le32(addr
);
118 static void op32_poke_tx(struct b43_dmaring
*ring
, int slot
)
120 b43_dma_write(ring
, B43_DMA32_TXINDEX
,
121 (u32
) (slot
* sizeof(struct b43_dmadesc32
)));
124 static void op32_tx_suspend(struct b43_dmaring
*ring
)
126 b43_dma_write(ring
, B43_DMA32_TXCTL
, b43_dma_read(ring
, B43_DMA32_TXCTL
)
127 | B43_DMA32_TXSUSPEND
);
130 static void op32_tx_resume(struct b43_dmaring
*ring
)
132 b43_dma_write(ring
, B43_DMA32_TXCTL
, b43_dma_read(ring
, B43_DMA32_TXCTL
)
133 & ~B43_DMA32_TXSUSPEND
);
136 static int op32_get_current_rxslot(struct b43_dmaring
*ring
)
140 val
= b43_dma_read(ring
, B43_DMA32_RXSTATUS
);
141 val
&= B43_DMA32_RXDPTR
;
143 return (val
/ sizeof(struct b43_dmadesc32
));
146 static void op32_set_current_rxslot(struct b43_dmaring
*ring
, int slot
)
148 b43_dma_write(ring
, B43_DMA32_RXINDEX
,
149 (u32
) (slot
* sizeof(struct b43_dmadesc32
)));
152 static const struct b43_dma_ops dma32_ops
= {
153 .idx2desc
= op32_idx2desc
,
154 .fill_descriptor
= op32_fill_descriptor
,
155 .poke_tx
= op32_poke_tx
,
156 .tx_suspend
= op32_tx_suspend
,
157 .tx_resume
= op32_tx_resume
,
158 .get_current_rxslot
= op32_get_current_rxslot
,
159 .set_current_rxslot
= op32_set_current_rxslot
,
164 struct b43_dmadesc_generic
*op64_idx2desc(struct b43_dmaring
*ring
,
166 struct b43_dmadesc_meta
**meta
)
168 struct b43_dmadesc64
*desc
;
170 *meta
= &(ring
->meta
[slot
]);
171 desc
= ring
->descbase
;
172 desc
= &(desc
[slot
]);
174 return (struct b43_dmadesc_generic
*)desc
;
177 static void op64_fill_descriptor(struct b43_dmaring
*ring
,
178 struct b43_dmadesc_generic
*desc
,
179 dma_addr_t dmaaddr
, u16 bufsize
,
180 int start
, int end
, int irq
)
182 struct b43_dmadesc64
*descbase
= ring
->descbase
;
184 u32 ctl0
= 0, ctl1
= 0;
188 slot
= (int)(&(desc
->dma64
) - descbase
);
189 B43_WARN_ON(!(slot
>= 0 && slot
< ring
->nr_slots
));
191 addrlo
= b43_dma_address(&ring
->dev
->dma
, dmaaddr
, B43_DMA_ADDR_LOW
);
192 addrhi
= b43_dma_address(&ring
->dev
->dma
, dmaaddr
, B43_DMA_ADDR_HIGH
);
193 addrext
= b43_dma_address(&ring
->dev
->dma
, dmaaddr
, B43_DMA_ADDR_EXT
);
195 if (slot
== ring
->nr_slots
- 1)
196 ctl0
|= B43_DMA64_DCTL0_DTABLEEND
;
198 ctl0
|= B43_DMA64_DCTL0_FRAMESTART
;
200 ctl0
|= B43_DMA64_DCTL0_FRAMEEND
;
202 ctl0
|= B43_DMA64_DCTL0_IRQ
;
203 ctl1
|= bufsize
& B43_DMA64_DCTL1_BYTECNT
;
204 ctl1
|= (addrext
<< B43_DMA64_DCTL1_ADDREXT_SHIFT
)
205 & B43_DMA64_DCTL1_ADDREXT_MASK
;
207 desc
->dma64
.control0
= cpu_to_le32(ctl0
);
208 desc
->dma64
.control1
= cpu_to_le32(ctl1
);
209 desc
->dma64
.address_low
= cpu_to_le32(addrlo
);
210 desc
->dma64
.address_high
= cpu_to_le32(addrhi
);
213 static void op64_poke_tx(struct b43_dmaring
*ring
, int slot
)
215 b43_dma_write(ring
, B43_DMA64_TXINDEX
,
216 (u32
) (slot
* sizeof(struct b43_dmadesc64
)));
219 static void op64_tx_suspend(struct b43_dmaring
*ring
)
221 b43_dma_write(ring
, B43_DMA64_TXCTL
, b43_dma_read(ring
, B43_DMA64_TXCTL
)
222 | B43_DMA64_TXSUSPEND
);
225 static void op64_tx_resume(struct b43_dmaring
*ring
)
227 b43_dma_write(ring
, B43_DMA64_TXCTL
, b43_dma_read(ring
, B43_DMA64_TXCTL
)
228 & ~B43_DMA64_TXSUSPEND
);
231 static int op64_get_current_rxslot(struct b43_dmaring
*ring
)
235 val
= b43_dma_read(ring
, B43_DMA64_RXSTATUS
);
236 val
&= B43_DMA64_RXSTATDPTR
;
238 return (val
/ sizeof(struct b43_dmadesc64
));
241 static void op64_set_current_rxslot(struct b43_dmaring
*ring
, int slot
)
243 b43_dma_write(ring
, B43_DMA64_RXINDEX
,
244 (u32
) (slot
* sizeof(struct b43_dmadesc64
)));
247 static const struct b43_dma_ops dma64_ops
= {
248 .idx2desc
= op64_idx2desc
,
249 .fill_descriptor
= op64_fill_descriptor
,
250 .poke_tx
= op64_poke_tx
,
251 .tx_suspend
= op64_tx_suspend
,
252 .tx_resume
= op64_tx_resume
,
253 .get_current_rxslot
= op64_get_current_rxslot
,
254 .set_current_rxslot
= op64_set_current_rxslot
,
257 static inline int free_slots(struct b43_dmaring
*ring
)
259 return (ring
->nr_slots
- ring
->used_slots
);
262 static inline int next_slot(struct b43_dmaring
*ring
, int slot
)
264 B43_WARN_ON(!(slot
>= -1 && slot
<= ring
->nr_slots
- 1));
265 if (slot
== ring
->nr_slots
- 1)
270 static inline int prev_slot(struct b43_dmaring
*ring
, int slot
)
272 B43_WARN_ON(!(slot
>= 0 && slot
<= ring
->nr_slots
- 1));
274 return ring
->nr_slots
- 1;
278 #ifdef CONFIG_B43_DEBUG
279 static void update_max_used_slots(struct b43_dmaring
*ring
,
280 int current_used_slots
)
282 if (current_used_slots
<= ring
->max_used_slots
)
284 ring
->max_used_slots
= current_used_slots
;
285 if (b43_debug(ring
->dev
, B43_DBG_DMAVERBOSE
)) {
286 b43dbg(ring
->dev
->wl
,
287 "max_used_slots increased to %d on %s ring %d\n",
288 ring
->max_used_slots
,
289 ring
->tx
? "TX" : "RX", ring
->index
);
294 void update_max_used_slots(struct b43_dmaring
*ring
, int current_used_slots
)
299 /* Request a slot for usage. */
300 static inline int request_slot(struct b43_dmaring
*ring
)
304 B43_WARN_ON(!ring
->tx
);
305 B43_WARN_ON(ring
->stopped
);
306 B43_WARN_ON(free_slots(ring
) == 0);
308 slot
= next_slot(ring
, ring
->current_slot
);
309 ring
->current_slot
= slot
;
312 update_max_used_slots(ring
, ring
->used_slots
);
317 static u16
b43_dmacontroller_base(enum b43_dmatype type
, int controller_idx
)
319 static const u16 map64
[] = {
320 B43_MMIO_DMA64_BASE0
,
321 B43_MMIO_DMA64_BASE1
,
322 B43_MMIO_DMA64_BASE2
,
323 B43_MMIO_DMA64_BASE3
,
324 B43_MMIO_DMA64_BASE4
,
325 B43_MMIO_DMA64_BASE5
,
327 static const u16 map32
[] = {
328 B43_MMIO_DMA32_BASE0
,
329 B43_MMIO_DMA32_BASE1
,
330 B43_MMIO_DMA32_BASE2
,
331 B43_MMIO_DMA32_BASE3
,
332 B43_MMIO_DMA32_BASE4
,
333 B43_MMIO_DMA32_BASE5
,
336 if (type
== B43_DMA_64BIT
) {
337 B43_WARN_ON(!(controller_idx
>= 0 &&
338 controller_idx
< ARRAY_SIZE(map64
)));
339 return map64
[controller_idx
];
341 B43_WARN_ON(!(controller_idx
>= 0 &&
342 controller_idx
< ARRAY_SIZE(map32
)));
343 return map32
[controller_idx
];
347 dma_addr_t
map_descbuffer(struct b43_dmaring
*ring
,
348 unsigned char *buf
, size_t len
, int tx
)
353 dmaaddr
= dma_map_single(ring
->dev
->dev
->dma_dev
,
354 buf
, len
, DMA_TO_DEVICE
);
356 dmaaddr
= dma_map_single(ring
->dev
->dev
->dma_dev
,
357 buf
, len
, DMA_FROM_DEVICE
);
364 void unmap_descbuffer(struct b43_dmaring
*ring
,
365 dma_addr_t addr
, size_t len
, int tx
)
368 dma_unmap_single(ring
->dev
->dev
->dma_dev
,
369 addr
, len
, DMA_TO_DEVICE
);
371 dma_unmap_single(ring
->dev
->dev
->dma_dev
,
372 addr
, len
, DMA_FROM_DEVICE
);
377 void sync_descbuffer_for_cpu(struct b43_dmaring
*ring
,
378 dma_addr_t addr
, size_t len
)
380 B43_WARN_ON(ring
->tx
);
381 dma_sync_single_for_cpu(ring
->dev
->dev
->dma_dev
,
382 addr
, len
, DMA_FROM_DEVICE
);
386 void sync_descbuffer_for_device(struct b43_dmaring
*ring
,
387 dma_addr_t addr
, size_t len
)
389 B43_WARN_ON(ring
->tx
);
390 dma_sync_single_for_device(ring
->dev
->dev
->dma_dev
,
391 addr
, len
, DMA_FROM_DEVICE
);
395 void free_descriptor_buffer(struct b43_dmaring
*ring
,
396 struct b43_dmadesc_meta
*meta
)
400 ieee80211_free_txskb(ring
->dev
->wl
->hw
, meta
->skb
);
402 dev_kfree_skb_any(meta
->skb
);
407 static int alloc_ringmemory(struct b43_dmaring
*ring
)
409 /* The specs call for 4K buffers for 30- and 32-bit DMA with 4K
410 * alignment and 8K buffers for 64-bit DMA with 8K alignment.
411 * In practice we could use smaller buffers for the latter, but the
412 * alignment is really important because of the hardware bug. If bit
413 * 0x00001000 is used in DMA address, some hardware (like BCM4331)
414 * copies that bit into B43_DMA64_RXSTATUS and we get false values from
415 * B43_DMA64_RXSTATDPTR. Let's just use 8K buffers even if we don't use
416 * more than 256 slots for ring.
418 u16 ring_mem_size
= (ring
->type
== B43_DMA_64BIT
) ?
419 B43_DMA64_RINGMEMSIZE
: B43_DMA32_RINGMEMSIZE
;
421 ring
->descbase
= dma_alloc_coherent(ring
->dev
->dev
->dma_dev
,
422 ring_mem_size
, &(ring
->dmabase
),
430 static void free_ringmemory(struct b43_dmaring
*ring
)
432 u16 ring_mem_size
= (ring
->type
== B43_DMA_64BIT
) ?
433 B43_DMA64_RINGMEMSIZE
: B43_DMA32_RINGMEMSIZE
;
434 dma_free_coherent(ring
->dev
->dev
->dma_dev
, ring_mem_size
,
435 ring
->descbase
, ring
->dmabase
);
438 /* Reset the RX DMA channel */
439 static int b43_dmacontroller_rx_reset(struct b43_wldev
*dev
, u16 mmio_base
,
440 enum b43_dmatype type
)
448 offset
= (type
== B43_DMA_64BIT
) ? B43_DMA64_RXCTL
: B43_DMA32_RXCTL
;
449 b43_write32(dev
, mmio_base
+ offset
, 0);
450 for (i
= 0; i
< 10; i
++) {
451 offset
= (type
== B43_DMA_64BIT
) ? B43_DMA64_RXSTATUS
:
453 value
= b43_read32(dev
, mmio_base
+ offset
);
454 if (type
== B43_DMA_64BIT
) {
455 value
&= B43_DMA64_RXSTAT
;
456 if (value
== B43_DMA64_RXSTAT_DISABLED
) {
461 value
&= B43_DMA32_RXSTATE
;
462 if (value
== B43_DMA32_RXSTAT_DISABLED
) {
470 b43err(dev
->wl
, "DMA RX reset timed out\n");
477 /* Reset the TX DMA channel */
478 static int b43_dmacontroller_tx_reset(struct b43_wldev
*dev
, u16 mmio_base
,
479 enum b43_dmatype type
)
487 for (i
= 0; i
< 10; i
++) {
488 offset
= (type
== B43_DMA_64BIT
) ? B43_DMA64_TXSTATUS
:
490 value
= b43_read32(dev
, mmio_base
+ offset
);
491 if (type
== B43_DMA_64BIT
) {
492 value
&= B43_DMA64_TXSTAT
;
493 if (value
== B43_DMA64_TXSTAT_DISABLED
||
494 value
== B43_DMA64_TXSTAT_IDLEWAIT
||
495 value
== B43_DMA64_TXSTAT_STOPPED
)
498 value
&= B43_DMA32_TXSTATE
;
499 if (value
== B43_DMA32_TXSTAT_DISABLED
||
500 value
== B43_DMA32_TXSTAT_IDLEWAIT
||
501 value
== B43_DMA32_TXSTAT_STOPPED
)
506 offset
= (type
== B43_DMA_64BIT
) ? B43_DMA64_TXCTL
: B43_DMA32_TXCTL
;
507 b43_write32(dev
, mmio_base
+ offset
, 0);
508 for (i
= 0; i
< 10; i
++) {
509 offset
= (type
== B43_DMA_64BIT
) ? B43_DMA64_TXSTATUS
:
511 value
= b43_read32(dev
, mmio_base
+ offset
);
512 if (type
== B43_DMA_64BIT
) {
513 value
&= B43_DMA64_TXSTAT
;
514 if (value
== B43_DMA64_TXSTAT_DISABLED
) {
519 value
&= B43_DMA32_TXSTATE
;
520 if (value
== B43_DMA32_TXSTAT_DISABLED
) {
528 b43err(dev
->wl
, "DMA TX reset timed out\n");
531 /* ensure the reset is completed. */
537 /* Check if a DMA mapping address is invalid. */
538 static bool b43_dma_mapping_error(struct b43_dmaring
*ring
,
540 size_t buffersize
, bool dma_to_device
)
542 if (unlikely(dma_mapping_error(ring
->dev
->dev
->dma_dev
, addr
)))
545 switch (ring
->type
) {
547 if ((u64
)addr
+ buffersize
> (1ULL << 30))
551 if ((u64
)addr
+ buffersize
> (1ULL << 32))
555 /* Currently we can't have addresses beyond
556 * 64bit in the kernel. */
560 /* The address is OK. */
564 /* We can't support this address. Unmap it again. */
565 unmap_descbuffer(ring
, addr
, buffersize
, dma_to_device
);
570 static bool b43_rx_buffer_is_poisoned(struct b43_dmaring
*ring
, struct sk_buff
*skb
)
572 unsigned char *f
= skb
->data
+ ring
->frameoffset
;
574 return ((f
[0] & f
[1] & f
[2] & f
[3] & f
[4] & f
[5] & f
[6] & f
[7]) == 0xFF);
577 static void b43_poison_rx_buffer(struct b43_dmaring
*ring
, struct sk_buff
*skb
)
579 struct b43_rxhdr_fw4
*rxhdr
;
580 unsigned char *frame
;
582 /* This poisons the RX buffer to detect DMA failures. */
584 rxhdr
= (struct b43_rxhdr_fw4
*)(skb
->data
);
585 rxhdr
->frame_len
= 0;
587 B43_WARN_ON(ring
->rx_buffersize
< ring
->frameoffset
+ sizeof(struct b43_plcp_hdr6
) + 2);
588 frame
= skb
->data
+ ring
->frameoffset
;
589 memset(frame
, 0xFF, sizeof(struct b43_plcp_hdr6
) + 2 /* padding */);
592 static int setup_rx_descbuffer(struct b43_dmaring
*ring
,
593 struct b43_dmadesc_generic
*desc
,
594 struct b43_dmadesc_meta
*meta
, gfp_t gfp_flags
)
599 B43_WARN_ON(ring
->tx
);
601 skb
= __dev_alloc_skb(ring
->rx_buffersize
, gfp_flags
);
604 b43_poison_rx_buffer(ring
, skb
);
605 dmaaddr
= map_descbuffer(ring
, skb
->data
, ring
->rx_buffersize
, 0);
606 if (b43_dma_mapping_error(ring
, dmaaddr
, ring
->rx_buffersize
, 0)) {
607 /* ugh. try to realloc in zone_dma */
608 gfp_flags
|= GFP_DMA
;
610 dev_kfree_skb_any(skb
);
612 skb
= __dev_alloc_skb(ring
->rx_buffersize
, gfp_flags
);
615 b43_poison_rx_buffer(ring
, skb
);
616 dmaaddr
= map_descbuffer(ring
, skb
->data
,
617 ring
->rx_buffersize
, 0);
618 if (b43_dma_mapping_error(ring
, dmaaddr
, ring
->rx_buffersize
, 0)) {
619 b43err(ring
->dev
->wl
, "RX DMA buffer allocation failed\n");
620 dev_kfree_skb_any(skb
);
626 meta
->dmaaddr
= dmaaddr
;
627 ring
->ops
->fill_descriptor(ring
, desc
, dmaaddr
,
628 ring
->rx_buffersize
, 0, 0, 0);
633 /* Allocate the initial descbuffers.
634 * This is used for an RX ring only.
636 static int alloc_initial_descbuffers(struct b43_dmaring
*ring
)
638 int i
, err
= -ENOMEM
;
639 struct b43_dmadesc_generic
*desc
;
640 struct b43_dmadesc_meta
*meta
;
642 for (i
= 0; i
< ring
->nr_slots
; i
++) {
643 desc
= ring
->ops
->idx2desc(ring
, i
, &meta
);
645 err
= setup_rx_descbuffer(ring
, desc
, meta
, GFP_KERNEL
);
647 b43err(ring
->dev
->wl
,
648 "Failed to allocate initial descbuffers\n");
653 ring
->used_slots
= ring
->nr_slots
;
659 for (i
--; i
>= 0; i
--) {
660 desc
= ring
->ops
->idx2desc(ring
, i
, &meta
);
662 unmap_descbuffer(ring
, meta
->dmaaddr
, ring
->rx_buffersize
, 0);
663 dev_kfree_skb(meta
->skb
);
668 /* Do initial setup of the DMA controller.
669 * Reset the controller, write the ring busaddress
670 * and switch the "enable" bit on.
672 static int dmacontroller_setup(struct b43_dmaring
*ring
)
677 bool parity
= ring
->dev
->dma
.parity
;
682 if (ring
->type
== B43_DMA_64BIT
) {
683 u64 ringbase
= (u64
) (ring
->dmabase
);
684 addrext
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_EXT
);
685 addrlo
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_LOW
);
686 addrhi
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_HIGH
);
688 value
= B43_DMA64_TXENABLE
;
689 value
|= (addrext
<< B43_DMA64_TXADDREXT_SHIFT
)
690 & B43_DMA64_TXADDREXT_MASK
;
692 value
|= B43_DMA64_TXPARITYDISABLE
;
693 b43_dma_write(ring
, B43_DMA64_TXCTL
, value
);
694 b43_dma_write(ring
, B43_DMA64_TXRINGLO
, addrlo
);
695 b43_dma_write(ring
, B43_DMA64_TXRINGHI
, addrhi
);
697 u32 ringbase
= (u32
) (ring
->dmabase
);
698 addrext
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_EXT
);
699 addrlo
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_LOW
);
701 value
= B43_DMA32_TXENABLE
;
702 value
|= (addrext
<< B43_DMA32_TXADDREXT_SHIFT
)
703 & B43_DMA32_TXADDREXT_MASK
;
705 value
|= B43_DMA32_TXPARITYDISABLE
;
706 b43_dma_write(ring
, B43_DMA32_TXCTL
, value
);
707 b43_dma_write(ring
, B43_DMA32_TXRING
, addrlo
);
710 err
= alloc_initial_descbuffers(ring
);
713 if (ring
->type
== B43_DMA_64BIT
) {
714 u64 ringbase
= (u64
) (ring
->dmabase
);
715 addrext
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_EXT
);
716 addrlo
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_LOW
);
717 addrhi
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_HIGH
);
719 value
= (ring
->frameoffset
<< B43_DMA64_RXFROFF_SHIFT
);
720 value
|= B43_DMA64_RXENABLE
;
721 value
|= (addrext
<< B43_DMA64_RXADDREXT_SHIFT
)
722 & B43_DMA64_RXADDREXT_MASK
;
724 value
|= B43_DMA64_RXPARITYDISABLE
;
725 b43_dma_write(ring
, B43_DMA64_RXCTL
, value
);
726 b43_dma_write(ring
, B43_DMA64_RXRINGLO
, addrlo
);
727 b43_dma_write(ring
, B43_DMA64_RXRINGHI
, addrhi
);
728 b43_dma_write(ring
, B43_DMA64_RXINDEX
, ring
->nr_slots
*
729 sizeof(struct b43_dmadesc64
));
731 u32 ringbase
= (u32
) (ring
->dmabase
);
732 addrext
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_EXT
);
733 addrlo
= b43_dma_address(&ring
->dev
->dma
, ringbase
, B43_DMA_ADDR_LOW
);
735 value
= (ring
->frameoffset
<< B43_DMA32_RXFROFF_SHIFT
);
736 value
|= B43_DMA32_RXENABLE
;
737 value
|= (addrext
<< B43_DMA32_RXADDREXT_SHIFT
)
738 & B43_DMA32_RXADDREXT_MASK
;
740 value
|= B43_DMA32_RXPARITYDISABLE
;
741 b43_dma_write(ring
, B43_DMA32_RXCTL
, value
);
742 b43_dma_write(ring
, B43_DMA32_RXRING
, addrlo
);
743 b43_dma_write(ring
, B43_DMA32_RXINDEX
, ring
->nr_slots
*
744 sizeof(struct b43_dmadesc32
));
752 /* Shutdown the DMA controller. */
753 static void dmacontroller_cleanup(struct b43_dmaring
*ring
)
756 b43_dmacontroller_tx_reset(ring
->dev
, ring
->mmio_base
,
758 if (ring
->type
== B43_DMA_64BIT
) {
759 b43_dma_write(ring
, B43_DMA64_TXRINGLO
, 0);
760 b43_dma_write(ring
, B43_DMA64_TXRINGHI
, 0);
762 b43_dma_write(ring
, B43_DMA32_TXRING
, 0);
764 b43_dmacontroller_rx_reset(ring
->dev
, ring
->mmio_base
,
766 if (ring
->type
== B43_DMA_64BIT
) {
767 b43_dma_write(ring
, B43_DMA64_RXRINGLO
, 0);
768 b43_dma_write(ring
, B43_DMA64_RXRINGHI
, 0);
770 b43_dma_write(ring
, B43_DMA32_RXRING
, 0);
774 static void free_all_descbuffers(struct b43_dmaring
*ring
)
776 struct b43_dmadesc_meta
*meta
;
779 if (!ring
->used_slots
)
781 for (i
= 0; i
< ring
->nr_slots
; i
++) {
782 /* get meta - ignore returned value */
783 ring
->ops
->idx2desc(ring
, i
, &meta
);
785 if (!meta
->skb
|| b43_dma_ptr_is_poisoned(meta
->skb
)) {
786 B43_WARN_ON(!ring
->tx
);
790 unmap_descbuffer(ring
, meta
->dmaaddr
,
793 unmap_descbuffer(ring
, meta
->dmaaddr
,
794 ring
->rx_buffersize
, 0);
796 free_descriptor_buffer(ring
, meta
);
800 static enum b43_dmatype
b43_engine_type(struct b43_wldev
*dev
)
805 switch (dev
->dev
->bus_type
) {
806 #ifdef CONFIG_B43_BCMA
808 tmp
= bcma_aread32(dev
->dev
->bdev
, BCMA_IOST
);
809 if (tmp
& BCMA_IOST_DMA64
)
810 return B43_DMA_64BIT
;
813 #ifdef CONFIG_B43_SSB
815 tmp
= ssb_read32(dev
->dev
->sdev
, SSB_TMSHIGH
);
816 if (tmp
& SSB_TMSHIGH_DMA64
)
817 return B43_DMA_64BIT
;
822 mmio_base
= b43_dmacontroller_base(0, 0);
823 b43_write32(dev
, mmio_base
+ B43_DMA32_TXCTL
, B43_DMA32_TXADDREXT_MASK
);
824 tmp
= b43_read32(dev
, mmio_base
+ B43_DMA32_TXCTL
);
825 if (tmp
& B43_DMA32_TXADDREXT_MASK
)
826 return B43_DMA_32BIT
;
827 return B43_DMA_30BIT
;
830 /* Main initialization function. */
832 struct b43_dmaring
*b43_setup_dmaring(struct b43_wldev
*dev
,
833 int controller_index
,
835 enum b43_dmatype type
)
837 struct b43_dmaring
*ring
;
841 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
845 ring
->nr_slots
= B43_RXRING_SLOTS
;
847 ring
->nr_slots
= B43_TXRING_SLOTS
;
849 ring
->meta
= kcalloc(ring
->nr_slots
, sizeof(struct b43_dmadesc_meta
),
853 for (i
= 0; i
< ring
->nr_slots
; i
++)
854 ring
->meta
->skb
= B43_DMA_PTR_POISON
;
858 ring
->mmio_base
= b43_dmacontroller_base(type
, controller_index
);
859 ring
->index
= controller_index
;
860 if (type
== B43_DMA_64BIT
)
861 ring
->ops
= &dma64_ops
;
863 ring
->ops
= &dma32_ops
;
866 ring
->current_slot
= -1;
868 if (ring
->index
== 0) {
869 switch (dev
->fw
.hdr_format
) {
871 ring
->rx_buffersize
= B43_DMA0_RX_FW598_BUFSIZE
;
872 ring
->frameoffset
= B43_DMA0_RX_FW598_FO
;
876 ring
->rx_buffersize
= B43_DMA0_RX_FW351_BUFSIZE
;
877 ring
->frameoffset
= B43_DMA0_RX_FW351_FO
;
883 #ifdef CONFIG_B43_DEBUG
884 ring
->last_injected_overflow
= jiffies
;
888 /* Assumption: B43_TXRING_SLOTS can be divided by TX_SLOTS_PER_FRAME */
889 BUILD_BUG_ON(B43_TXRING_SLOTS
% TX_SLOTS_PER_FRAME
!= 0);
891 ring
->txhdr_cache
= kcalloc(ring
->nr_slots
/ TX_SLOTS_PER_FRAME
,
894 if (!ring
->txhdr_cache
)
897 /* test for ability to dma to txhdr_cache */
898 dma_test
= dma_map_single(dev
->dev
->dma_dev
,
903 if (b43_dma_mapping_error(ring
, dma_test
,
904 b43_txhdr_size(dev
), 1)) {
906 kfree(ring
->txhdr_cache
);
907 ring
->txhdr_cache
= kcalloc(ring
->nr_slots
/ TX_SLOTS_PER_FRAME
,
909 GFP_KERNEL
| GFP_DMA
);
910 if (!ring
->txhdr_cache
)
913 dma_test
= dma_map_single(dev
->dev
->dma_dev
,
918 if (b43_dma_mapping_error(ring
, dma_test
,
919 b43_txhdr_size(dev
), 1)) {
922 "TXHDR DMA allocation failed\n");
923 goto err_kfree_txhdr_cache
;
927 dma_unmap_single(dev
->dev
->dma_dev
,
928 dma_test
, b43_txhdr_size(dev
),
932 err
= alloc_ringmemory(ring
);
934 goto err_kfree_txhdr_cache
;
935 err
= dmacontroller_setup(ring
);
937 goto err_free_ringmemory
;
943 free_ringmemory(ring
);
944 err_kfree_txhdr_cache
:
945 kfree(ring
->txhdr_cache
);
954 #define divide(a, b) ({ \
960 #define modulo(a, b) ({ \
965 /* Main cleanup function. */
966 static void b43_destroy_dmaring(struct b43_dmaring
*ring
,
967 const char *ringname
)
972 #ifdef CONFIG_B43_DEBUG
974 /* Print some statistics. */
975 u64 failed_packets
= ring
->nr_failed_tx_packets
;
976 u64 succeed_packets
= ring
->nr_succeed_tx_packets
;
977 u64 nr_packets
= failed_packets
+ succeed_packets
;
978 u64 permille_failed
= 0, average_tries
= 0;
981 permille_failed
= divide(failed_packets
* 1000, nr_packets
);
983 average_tries
= divide(ring
->nr_total_packet_tries
* 100, nr_packets
);
985 b43dbg(ring
->dev
->wl
, "DMA-%u %s: "
986 "Used slots %d/%d, Failed frames %llu/%llu = %llu.%01llu%%, "
987 "Average tries %llu.%02llu\n",
988 (unsigned int)(ring
->type
), ringname
,
989 ring
->max_used_slots
,
991 (unsigned long long)failed_packets
,
992 (unsigned long long)nr_packets
,
993 (unsigned long long)divide(permille_failed
, 10),
994 (unsigned long long)modulo(permille_failed
, 10),
995 (unsigned long long)divide(average_tries
, 100),
996 (unsigned long long)modulo(average_tries
, 100));
1000 /* Device IRQs are disabled prior entering this function,
1001 * so no need to take care of concurrency with rx handler stuff.
1003 dmacontroller_cleanup(ring
);
1004 free_all_descbuffers(ring
);
1005 free_ringmemory(ring
);
1007 kfree(ring
->txhdr_cache
);
1012 #define destroy_ring(dma, ring) do { \
1013 b43_destroy_dmaring((dma)->ring, __stringify(ring)); \
1014 (dma)->ring = NULL; \
1017 void b43_dma_free(struct b43_wldev
*dev
)
1019 struct b43_dma
*dma
;
1021 if (b43_using_pio_transfers(dev
))
1025 destroy_ring(dma
, rx_ring
);
1026 destroy_ring(dma
, tx_ring_AC_BK
);
1027 destroy_ring(dma
, tx_ring_AC_BE
);
1028 destroy_ring(dma
, tx_ring_AC_VI
);
1029 destroy_ring(dma
, tx_ring_AC_VO
);
1030 destroy_ring(dma
, tx_ring_mcast
);
1033 /* Some hardware with 64-bit DMA seems to be bugged and looks for translation
1034 * bit in low address word instead of high one.
1036 static bool b43_dma_translation_in_low_word(struct b43_wldev
*dev
,
1037 enum b43_dmatype type
)
1039 if (type
!= B43_DMA_64BIT
)
1042 #ifdef CONFIG_B43_SSB
1043 if (dev
->dev
->bus_type
== B43_BUS_SSB
&&
1044 dev
->dev
->sdev
->bus
->bustype
== SSB_BUSTYPE_PCI
&&
1045 !(pci_is_pcie(dev
->dev
->sdev
->bus
->host_pci
) &&
1046 ssb_read32(dev
->dev
->sdev
, SSB_TMSHIGH
) & SSB_TMSHIGH_DMA64
))
1052 int b43_dma_init(struct b43_wldev
*dev
)
1054 struct b43_dma
*dma
= &dev
->dma
;
1055 enum b43_dmatype type
= b43_engine_type(dev
);
1058 err
= dma_set_mask_and_coherent(dev
->dev
->dma_dev
, DMA_BIT_MASK(type
));
1060 b43err(dev
->wl
, "The machine/kernel does not support "
1061 "the required %u-bit DMA mask\n", type
);
1065 switch (dev
->dev
->bus_type
) {
1066 #ifdef CONFIG_B43_BCMA
1068 dma
->translation
= bcma_core_dma_translation(dev
->dev
->bdev
);
1071 #ifdef CONFIG_B43_SSB
1073 dma
->translation
= ssb_dma_translation(dev
->dev
->sdev
);
1077 dma
->translation_in_low
= b43_dma_translation_in_low_word(dev
, type
);
1080 #ifdef CONFIG_B43_BCMA
1081 /* TODO: find out which SSB devices need disabling parity */
1082 if (dev
->dev
->bus_type
== B43_BUS_BCMA
)
1083 dma
->parity
= false;
1087 /* setup TX DMA channels. */
1088 dma
->tx_ring_AC_BK
= b43_setup_dmaring(dev
, 0, 1, type
);
1089 if (!dma
->tx_ring_AC_BK
)
1092 dma
->tx_ring_AC_BE
= b43_setup_dmaring(dev
, 1, 1, type
);
1093 if (!dma
->tx_ring_AC_BE
)
1094 goto err_destroy_bk
;
1096 dma
->tx_ring_AC_VI
= b43_setup_dmaring(dev
, 2, 1, type
);
1097 if (!dma
->tx_ring_AC_VI
)
1098 goto err_destroy_be
;
1100 dma
->tx_ring_AC_VO
= b43_setup_dmaring(dev
, 3, 1, type
);
1101 if (!dma
->tx_ring_AC_VO
)
1102 goto err_destroy_vi
;
1104 dma
->tx_ring_mcast
= b43_setup_dmaring(dev
, 4, 1, type
);
1105 if (!dma
->tx_ring_mcast
)
1106 goto err_destroy_vo
;
1108 /* setup RX DMA channel. */
1109 dma
->rx_ring
= b43_setup_dmaring(dev
, 0, 0, type
);
1111 goto err_destroy_mcast
;
1113 /* No support for the TX status DMA ring. */
1114 B43_WARN_ON(dev
->dev
->core_rev
< 5);
1116 b43dbg(dev
->wl
, "%u-bit DMA initialized\n",
1117 (unsigned int)type
);
1123 destroy_ring(dma
, tx_ring_mcast
);
1125 destroy_ring(dma
, tx_ring_AC_VO
);
1127 destroy_ring(dma
, tx_ring_AC_VI
);
1129 destroy_ring(dma
, tx_ring_AC_BE
);
1131 destroy_ring(dma
, tx_ring_AC_BK
);
1135 /* Generate a cookie for the TX header. */
1136 static u16
generate_cookie(struct b43_dmaring
*ring
, int slot
)
1140 /* Use the upper 4 bits of the cookie as
1141 * DMA controller ID and store the slot number
1142 * in the lower 12 bits.
1143 * Note that the cookie must never be 0, as this
1144 * is a special value used in RX path.
1145 * It can also not be 0xFFFF because that is special
1146 * for multicast frames.
1148 cookie
= (((u16
)ring
->index
+ 1) << 12);
1149 B43_WARN_ON(slot
& ~0x0FFF);
1150 cookie
|= (u16
)slot
;
1155 /* Inspect a cookie and find out to which controller/slot it belongs. */
1157 struct b43_dmaring
*parse_cookie(struct b43_wldev
*dev
, u16 cookie
, int *slot
)
1159 struct b43_dma
*dma
= &dev
->dma
;
1160 struct b43_dmaring
*ring
= NULL
;
1162 switch (cookie
& 0xF000) {
1164 ring
= dma
->tx_ring_AC_BK
;
1167 ring
= dma
->tx_ring_AC_BE
;
1170 ring
= dma
->tx_ring_AC_VI
;
1173 ring
= dma
->tx_ring_AC_VO
;
1176 ring
= dma
->tx_ring_mcast
;
1179 *slot
= (cookie
& 0x0FFF);
1180 if (unlikely(!ring
|| *slot
< 0 || *slot
>= ring
->nr_slots
)) {
1181 b43dbg(dev
->wl
, "TX-status contains "
1182 "invalid cookie: 0x%04X\n", cookie
);
1189 static int dma_tx_fragment(struct b43_dmaring
*ring
,
1190 struct sk_buff
*skb
)
1192 const struct b43_dma_ops
*ops
= ring
->ops
;
1193 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1194 struct b43_private_tx_info
*priv_info
= b43_get_priv_tx_info(info
);
1196 int slot
, old_top_slot
, old_used_slots
;
1198 struct b43_dmadesc_generic
*desc
;
1199 struct b43_dmadesc_meta
*meta
;
1200 struct b43_dmadesc_meta
*meta_hdr
;
1202 size_t hdrsize
= b43_txhdr_size(ring
->dev
);
1204 /* Important note: If the number of used DMA slots per TX frame
1205 * is changed here, the TX_SLOTS_PER_FRAME definition at the top of
1206 * the file has to be updated, too!
1209 old_top_slot
= ring
->current_slot
;
1210 old_used_slots
= ring
->used_slots
;
1212 /* Get a slot for the header. */
1213 slot
= request_slot(ring
);
1214 desc
= ops
->idx2desc(ring
, slot
, &meta_hdr
);
1215 memset(meta_hdr
, 0, sizeof(*meta_hdr
));
1217 header
= &(ring
->txhdr_cache
[(slot
/ TX_SLOTS_PER_FRAME
) * hdrsize
]);
1218 cookie
= generate_cookie(ring
, slot
);
1219 err
= b43_generate_txhdr(ring
->dev
, header
,
1221 if (unlikely(err
)) {
1222 ring
->current_slot
= old_top_slot
;
1223 ring
->used_slots
= old_used_slots
;
1227 meta_hdr
->dmaaddr
= map_descbuffer(ring
, (unsigned char *)header
,
1229 if (b43_dma_mapping_error(ring
, meta_hdr
->dmaaddr
, hdrsize
, 1)) {
1230 ring
->current_slot
= old_top_slot
;
1231 ring
->used_slots
= old_used_slots
;
1234 ops
->fill_descriptor(ring
, desc
, meta_hdr
->dmaaddr
,
1237 /* Get a slot for the payload. */
1238 slot
= request_slot(ring
);
1239 desc
= ops
->idx2desc(ring
, slot
, &meta
);
1240 memset(meta
, 0, sizeof(*meta
));
1243 meta
->is_last_fragment
= true;
1244 priv_info
->bouncebuffer
= NULL
;
1246 meta
->dmaaddr
= map_descbuffer(ring
, skb
->data
, skb
->len
, 1);
1247 /* create a bounce buffer in zone_dma on mapping failure. */
1248 if (b43_dma_mapping_error(ring
, meta
->dmaaddr
, skb
->len
, 1)) {
1249 priv_info
->bouncebuffer
= kmemdup(skb
->data
, skb
->len
,
1250 GFP_ATOMIC
| GFP_DMA
);
1251 if (!priv_info
->bouncebuffer
) {
1252 ring
->current_slot
= old_top_slot
;
1253 ring
->used_slots
= old_used_slots
;
1258 meta
->dmaaddr
= map_descbuffer(ring
, priv_info
->bouncebuffer
, skb
->len
, 1);
1259 if (b43_dma_mapping_error(ring
, meta
->dmaaddr
, skb
->len
, 1)) {
1260 kfree(priv_info
->bouncebuffer
);
1261 priv_info
->bouncebuffer
= NULL
;
1262 ring
->current_slot
= old_top_slot
;
1263 ring
->used_slots
= old_used_slots
;
1269 ops
->fill_descriptor(ring
, desc
, meta
->dmaaddr
, skb
->len
, 0, 1, 1);
1271 if (info
->flags
& IEEE80211_TX_CTL_SEND_AFTER_DTIM
) {
1272 /* Tell the firmware about the cookie of the last
1273 * mcast frame, so it can clear the more-data bit in it. */
1274 b43_shm_write16(ring
->dev
, B43_SHM_SHARED
,
1275 B43_SHM_SH_MCASTCOOKIE
, cookie
);
1277 /* Now transfer the whole frame. */
1279 ops
->poke_tx(ring
, next_slot(ring
, slot
));
1283 unmap_descbuffer(ring
, meta_hdr
->dmaaddr
,
1288 static inline int should_inject_overflow(struct b43_dmaring
*ring
)
1290 #ifdef CONFIG_B43_DEBUG
1291 if (unlikely(b43_debug(ring
->dev
, B43_DBG_DMAOVERFLOW
))) {
1292 /* Check if we should inject another ringbuffer overflow
1293 * to test handling of this situation in the stack. */
1294 unsigned long next_overflow
;
1296 next_overflow
= ring
->last_injected_overflow
+ HZ
;
1297 if (time_after(jiffies
, next_overflow
)) {
1298 ring
->last_injected_overflow
= jiffies
;
1299 b43dbg(ring
->dev
->wl
,
1300 "Injecting TX ring overflow on "
1301 "DMA controller %d\n", ring
->index
);
1305 #endif /* CONFIG_B43_DEBUG */
1309 /* Static mapping of mac80211's queues (priorities) to b43 DMA rings. */
1310 static struct b43_dmaring
*select_ring_by_priority(struct b43_wldev
*dev
,
1313 struct b43_dmaring
*ring
;
1315 if (dev
->qos_enabled
) {
1316 /* 0 = highest priority */
1317 switch (queue_prio
) {
1322 ring
= dev
->dma
.tx_ring_AC_VO
;
1325 ring
= dev
->dma
.tx_ring_AC_VI
;
1328 ring
= dev
->dma
.tx_ring_AC_BE
;
1331 ring
= dev
->dma
.tx_ring_AC_BK
;
1335 ring
= dev
->dma
.tx_ring_AC_BE
;
1340 int b43_dma_tx(struct b43_wldev
*dev
, struct sk_buff
*skb
)
1342 struct b43_dmaring
*ring
;
1343 struct ieee80211_hdr
*hdr
;
1345 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
1347 hdr
= (struct ieee80211_hdr
*)skb
->data
;
1348 if (info
->flags
& IEEE80211_TX_CTL_SEND_AFTER_DTIM
) {
1349 /* The multicast ring will be sent after the DTIM */
1350 ring
= dev
->dma
.tx_ring_mcast
;
1351 /* Set the more-data bit. Ucode will clear it on
1352 * the last frame for us. */
1353 hdr
->frame_control
|= cpu_to_le16(IEEE80211_FCTL_MOREDATA
);
1355 /* Decide by priority where to put this frame. */
1356 ring
= select_ring_by_priority(
1357 dev
, skb_get_queue_mapping(skb
));
1360 B43_WARN_ON(!ring
->tx
);
1362 if (unlikely(ring
->stopped
)) {
1363 /* We get here only because of a bug in mac80211.
1364 * Because of a race, one packet may be queued after
1365 * the queue is stopped, thus we got called when we shouldn't.
1366 * For now, just refuse the transmit. */
1367 if (b43_debug(dev
, B43_DBG_DMAVERBOSE
))
1368 b43err(dev
->wl
, "Packet after queue stopped\n");
1373 if (WARN_ON(free_slots(ring
) < TX_SLOTS_PER_FRAME
)) {
1374 /* If we get here, we have a real error with the queue
1375 * full, but queues not stopped. */
1376 b43err(dev
->wl
, "DMA queue overflow\n");
1381 /* Assign the queue number to the ring (if not already done before)
1382 * so TX status handling can use it. The queue to ring mapping is
1383 * static, so we don't need to store it per frame. */
1384 ring
->queue_prio
= skb_get_queue_mapping(skb
);
1386 err
= dma_tx_fragment(ring
, skb
);
1387 if (unlikely(err
== -ENOKEY
)) {
1388 /* Drop this packet, as we don't have the encryption key
1389 * anymore and must not transmit it unencrypted. */
1390 ieee80211_free_txskb(dev
->wl
->hw
, skb
);
1394 if (unlikely(err
)) {
1395 b43err(dev
->wl
, "DMA tx mapping failure\n");
1398 if ((free_slots(ring
) < TX_SLOTS_PER_FRAME
) ||
1399 should_inject_overflow(ring
)) {
1400 /* This TX ring is full. */
1401 unsigned int skb_mapping
= skb_get_queue_mapping(skb
);
1402 ieee80211_stop_queue(dev
->wl
->hw
, skb_mapping
);
1403 dev
->wl
->tx_queue_stopped
[skb_mapping
] = true;
1404 ring
->stopped
= true;
1405 if (b43_debug(dev
, B43_DBG_DMAVERBOSE
)) {
1406 b43dbg(dev
->wl
, "Stopped TX ring %d\n", ring
->index
);
1414 void b43_dma_handle_txstatus(struct b43_wldev
*dev
,
1415 const struct b43_txstatus
*status
)
1417 const struct b43_dma_ops
*ops
;
1418 struct b43_dmaring
*ring
;
1419 struct b43_dmadesc_meta
*meta
;
1420 static const struct b43_txstatus fake
; /* filled with 0 */
1421 const struct b43_txstatus
*txstat
;
1422 int slot
, firstused
;
1427 ring
= parse_cookie(dev
, status
->cookie
, &slot
);
1428 if (unlikely(!ring
))
1430 B43_WARN_ON(!ring
->tx
);
1432 /* Sanity check: TX packets are processed in-order on one ring.
1433 * Check if the slot deduced from the cookie really is the first
1435 firstused
= ring
->current_slot
- ring
->used_slots
+ 1;
1437 firstused
= ring
->nr_slots
+ firstused
;
1440 if (unlikely(slot
!= firstused
)) {
1441 /* This possibly is a firmware bug and will result in
1442 * malfunction, memory leaks and/or stall of DMA functionality.
1444 if (slot
== next_slot(ring
, next_slot(ring
, firstused
))) {
1445 /* If a single header/data pair was missed, skip over
1446 * the first two slots in an attempt to recover.
1451 /* Report the error once. */
1453 "Skip on DMA ring %d slot %d.\n",
1458 /* More than a single header/data pair were missed.
1459 * Report this error. If running with open-source
1460 * firmware, then reset the controller to
1464 "Out of order TX status report on DMA ring %d. Expected %d, but got %d\n",
1465 ring
->index
, firstused
, slot
);
1466 if (dev
->fw
.opensource
)
1467 b43_controller_restart(dev
, "Out of order TX");
1474 B43_WARN_ON(slot
< 0 || slot
>= ring
->nr_slots
);
1475 /* get meta - ignore returned value */
1476 ops
->idx2desc(ring
, slot
, &meta
);
1478 if (b43_dma_ptr_is_poisoned(meta
->skb
)) {
1479 b43dbg(dev
->wl
, "Poisoned TX slot %d (first=%d) "
1481 slot
, firstused
, ring
->index
);
1486 struct b43_private_tx_info
*priv_info
=
1487 b43_get_priv_tx_info(IEEE80211_SKB_CB(meta
->skb
));
1489 unmap_descbuffer(ring
, meta
->dmaaddr
,
1491 kfree(priv_info
->bouncebuffer
);
1492 priv_info
->bouncebuffer
= NULL
;
1494 unmap_descbuffer(ring
, meta
->dmaaddr
,
1495 b43_txhdr_size(dev
), 1);
1498 if (meta
->is_last_fragment
) {
1499 struct ieee80211_tx_info
*info
;
1501 if (unlikely(!meta
->skb
)) {
1502 /* This is a scatter-gather fragment of a frame,
1503 * so the skb pointer must not be NULL.
1505 b43dbg(dev
->wl
, "TX status unexpected NULL skb "
1506 "at slot %d (first=%d) on ring %d\n",
1507 slot
, firstused
, ring
->index
);
1511 info
= IEEE80211_SKB_CB(meta
->skb
);
1514 * Call back to inform the ieee80211 subsystem about
1515 * the status of the transmission. When skipping over
1516 * a missed TX status report, use a status structure
1517 * filled with zeros to indicate that the frame was not
1518 * sent (frame_count 0) and not acknowledged
1525 frame_succeed
= b43_fill_txstatus_report(dev
, info
,
1527 #ifdef CONFIG_B43_DEBUG
1529 ring
->nr_succeed_tx_packets
++;
1531 ring
->nr_failed_tx_packets
++;
1532 ring
->nr_total_packet_tries
+= status
->frame_count
;
1534 ieee80211_tx_status(dev
->wl
->hw
, meta
->skb
);
1536 /* skb will be freed by ieee80211_tx_status().
1537 * Poison our pointer. */
1538 meta
->skb
= B43_DMA_PTR_POISON
;
1540 /* No need to call free_descriptor_buffer here, as
1541 * this is only the txhdr, which is not allocated.
1543 if (unlikely(meta
->skb
)) {
1544 b43dbg(dev
->wl
, "TX status unexpected non-NULL skb "
1545 "at slot %d (first=%d) on ring %d\n",
1546 slot
, firstused
, ring
->index
);
1551 /* Everything unmapped and free'd. So it's not used anymore. */
1554 if (meta
->is_last_fragment
&& !skip
) {
1555 /* This is the last scatter-gather
1556 * fragment of the frame. We are done. */
1559 slot
= next_slot(ring
, slot
);
1563 if (ring
->stopped
) {
1564 B43_WARN_ON(free_slots(ring
) < TX_SLOTS_PER_FRAME
);
1565 ring
->stopped
= false;
1568 if (dev
->wl
->tx_queue_stopped
[ring
->queue_prio
]) {
1569 dev
->wl
->tx_queue_stopped
[ring
->queue_prio
] = false;
1571 /* If the driver queue is running wake the corresponding
1572 * mac80211 queue. */
1573 ieee80211_wake_queue(dev
->wl
->hw
, ring
->queue_prio
);
1574 if (b43_debug(dev
, B43_DBG_DMAVERBOSE
)) {
1575 b43dbg(dev
->wl
, "Woke up TX ring %d\n", ring
->index
);
1578 /* Add work to the queue. */
1579 ieee80211_queue_work(dev
->wl
->hw
, &dev
->wl
->tx_work
);
1582 static void dma_rx(struct b43_dmaring
*ring
, int *slot
)
1584 const struct b43_dma_ops
*ops
= ring
->ops
;
1585 struct b43_dmadesc_generic
*desc
;
1586 struct b43_dmadesc_meta
*meta
;
1587 struct b43_rxhdr_fw4
*rxhdr
;
1588 struct sk_buff
*skb
;
1593 desc
= ops
->idx2desc(ring
, *slot
, &meta
);
1595 sync_descbuffer_for_cpu(ring
, meta
->dmaaddr
, ring
->rx_buffersize
);
1598 rxhdr
= (struct b43_rxhdr_fw4
*)skb
->data
;
1599 len
= le16_to_cpu(rxhdr
->frame_len
);
1606 len
= le16_to_cpu(rxhdr
->frame_len
);
1607 } while (len
== 0 && i
++ < 5);
1608 if (unlikely(len
== 0)) {
1609 dmaaddr
= meta
->dmaaddr
;
1610 goto drop_recycle_buffer
;
1613 if (unlikely(b43_rx_buffer_is_poisoned(ring
, skb
))) {
1614 /* Something went wrong with the DMA.
1615 * The device did not touch the buffer and did not overwrite the poison. */
1616 b43dbg(ring
->dev
->wl
, "DMA RX: Dropping poisoned buffer.\n");
1617 dmaaddr
= meta
->dmaaddr
;
1618 goto drop_recycle_buffer
;
1620 if (unlikely(len
+ ring
->frameoffset
> ring
->rx_buffersize
)) {
1621 /* The data did not fit into one descriptor buffer
1622 * and is split over multiple buffers.
1623 * This should never happen, as we try to allocate buffers
1624 * big enough. So simply ignore this packet.
1630 desc
= ops
->idx2desc(ring
, *slot
, &meta
);
1631 /* recycle the descriptor buffer. */
1632 b43_poison_rx_buffer(ring
, meta
->skb
);
1633 sync_descbuffer_for_device(ring
, meta
->dmaaddr
,
1634 ring
->rx_buffersize
);
1635 *slot
= next_slot(ring
, *slot
);
1637 tmp
-= ring
->rx_buffersize
;
1641 b43err(ring
->dev
->wl
, "DMA RX buffer too small "
1642 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1643 len
, ring
->rx_buffersize
, cnt
);
1647 dmaaddr
= meta
->dmaaddr
;
1648 err
= setup_rx_descbuffer(ring
, desc
, meta
, GFP_ATOMIC
);
1649 if (unlikely(err
)) {
1650 b43dbg(ring
->dev
->wl
, "DMA RX: setup_rx_descbuffer() failed\n");
1651 goto drop_recycle_buffer
;
1654 unmap_descbuffer(ring
, dmaaddr
, ring
->rx_buffersize
, 0);
1655 skb_put(skb
, len
+ ring
->frameoffset
);
1656 skb_pull(skb
, ring
->frameoffset
);
1658 b43_rx(ring
->dev
, skb
, rxhdr
);
1662 drop_recycle_buffer
:
1663 /* Poison and recycle the RX buffer. */
1664 b43_poison_rx_buffer(ring
, skb
);
1665 sync_descbuffer_for_device(ring
, dmaaddr
, ring
->rx_buffersize
);
1668 void b43_dma_handle_rx_overflow(struct b43_dmaring
*ring
)
1670 int current_slot
, previous_slot
;
1672 B43_WARN_ON(ring
->tx
);
1674 /* Device has filled all buffers, drop all packets and let TCP
1676 * Decrement RX index by one will let the device to see all slots
1680 *TODO: How to increase rx_drop in mac80211?
1682 current_slot
= ring
->ops
->get_current_rxslot(ring
);
1683 previous_slot
= prev_slot(ring
, current_slot
);
1684 ring
->ops
->set_current_rxslot(ring
, previous_slot
);
1687 void b43_dma_rx(struct b43_dmaring
*ring
)
1689 const struct b43_dma_ops
*ops
= ring
->ops
;
1690 int slot
, current_slot
;
1693 B43_WARN_ON(ring
->tx
);
1694 current_slot
= ops
->get_current_rxslot(ring
);
1695 B43_WARN_ON(!(current_slot
>= 0 && current_slot
< ring
->nr_slots
));
1697 slot
= ring
->current_slot
;
1698 for (; slot
!= current_slot
; slot
= next_slot(ring
, slot
)) {
1699 dma_rx(ring
, &slot
);
1700 update_max_used_slots(ring
, ++used_slots
);
1703 ops
->set_current_rxslot(ring
, slot
);
1704 ring
->current_slot
= slot
;
1707 static void b43_dma_tx_suspend_ring(struct b43_dmaring
*ring
)
1709 B43_WARN_ON(!ring
->tx
);
1710 ring
->ops
->tx_suspend(ring
);
1713 static void b43_dma_tx_resume_ring(struct b43_dmaring
*ring
)
1715 B43_WARN_ON(!ring
->tx
);
1716 ring
->ops
->tx_resume(ring
);
1719 void b43_dma_tx_suspend(struct b43_wldev
*dev
)
1721 b43_power_saving_ctl_bits(dev
, B43_PS_AWAKE
);
1722 b43_dma_tx_suspend_ring(dev
->dma
.tx_ring_AC_BK
);
1723 b43_dma_tx_suspend_ring(dev
->dma
.tx_ring_AC_BE
);
1724 b43_dma_tx_suspend_ring(dev
->dma
.tx_ring_AC_VI
);
1725 b43_dma_tx_suspend_ring(dev
->dma
.tx_ring_AC_VO
);
1726 b43_dma_tx_suspend_ring(dev
->dma
.tx_ring_mcast
);
1729 void b43_dma_tx_resume(struct b43_wldev
*dev
)
1731 b43_dma_tx_resume_ring(dev
->dma
.tx_ring_mcast
);
1732 b43_dma_tx_resume_ring(dev
->dma
.tx_ring_AC_VO
);
1733 b43_dma_tx_resume_ring(dev
->dma
.tx_ring_AC_VI
);
1734 b43_dma_tx_resume_ring(dev
->dma
.tx_ring_AC_BE
);
1735 b43_dma_tx_resume_ring(dev
->dma
.tx_ring_AC_BK
);
1736 b43_power_saving_ctl_bits(dev
, 0);
1739 static void direct_fifo_rx(struct b43_wldev
*dev
, enum b43_dmatype type
,
1740 u16 mmio_base
, bool enable
)
1744 if (type
== B43_DMA_64BIT
) {
1745 ctl
= b43_read32(dev
, mmio_base
+ B43_DMA64_RXCTL
);
1746 ctl
&= ~B43_DMA64_RXDIRECTFIFO
;
1748 ctl
|= B43_DMA64_RXDIRECTFIFO
;
1749 b43_write32(dev
, mmio_base
+ B43_DMA64_RXCTL
, ctl
);
1751 ctl
= b43_read32(dev
, mmio_base
+ B43_DMA32_RXCTL
);
1752 ctl
&= ~B43_DMA32_RXDIRECTFIFO
;
1754 ctl
|= B43_DMA32_RXDIRECTFIFO
;
1755 b43_write32(dev
, mmio_base
+ B43_DMA32_RXCTL
, ctl
);
1759 /* Enable/Disable Direct FIFO Receive Mode (PIO) on a RX engine.
1760 * This is called from PIO code, so DMA structures are not available. */
1761 void b43_dma_direct_fifo_rx(struct b43_wldev
*dev
,
1762 unsigned int engine_index
, bool enable
)
1764 enum b43_dmatype type
;
1767 type
= b43_engine_type(dev
);
1769 mmio_base
= b43_dmacontroller_base(type
, engine_index
);
1770 direct_fifo_rx(dev
, type
, mmio_base
, enable
);