3 Broadcom BCM43xx wireless driver
5 DMA ringbuffer and descriptor allocation/management
7 Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
9 Some code in this file is derived from the b44.c driver
10 Copyright (C) 2002 David S. Miller
11 Copyright (C) Pekka Pietikainen
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
26 Boston, MA 02110-1301, USA.
31 #include "bcm43xx_dma.h"
32 #include "bcm43xx_main.h"
33 #include "bcm43xx_debugfs.h"
34 #include "bcm43xx_power.h"
35 #include "bcm43xx_xmit.h"
37 #include <linux/dma-mapping.h>
38 #include <linux/pci.h>
39 #include <linux/delay.h>
40 #include <linux/skbuff.h>
45 struct bcm43xx_dmadesc_generic
* op32_idx2desc(struct bcm43xx_dmaring
*ring
,
47 struct bcm43xx_dmadesc_meta
**meta
)
49 struct bcm43xx_dmadesc32
*desc
;
51 *meta
= &(ring
->meta
[slot
]);
52 desc
= ring
->descbase
;
55 return (struct bcm43xx_dmadesc_generic
*)desc
;
58 static void op32_fill_descriptor(struct bcm43xx_dmaring
*ring
,
59 struct bcm43xx_dmadesc_generic
*desc
,
60 dma_addr_t dmaaddr
, u16 bufsize
,
61 int start
, int end
, int irq
)
63 struct bcm43xx_dmadesc32
*descbase
= ring
->descbase
;
69 slot
= (int)(&(desc
->dma32
) - descbase
);
70 assert(slot
>= 0 && slot
< ring
->nr_slots
);
72 addr
= (u32
)(dmaaddr
& ~SSB_DMA_TRANSLATION_MASK
);
73 addrext
= (u32
)(dmaaddr
& SSB_DMA_TRANSLATION_MASK
)
74 >> SSB_DMA_TRANSLATION_SHIFT
;
75 addr
|= ssb_dma_translation(ring
->dev
->dev
);
76 ctl
= (bufsize
- ring
->frameoffset
)
77 & BCM43xx_DMA32_DCTL_BYTECNT
;
78 if (slot
== ring
->nr_slots
- 1)
79 ctl
|= BCM43xx_DMA32_DCTL_DTABLEEND
;
81 ctl
|= BCM43xx_DMA32_DCTL_FRAMESTART
;
83 ctl
|= BCM43xx_DMA32_DCTL_FRAMEEND
;
85 ctl
|= BCM43xx_DMA32_DCTL_IRQ
;
86 ctl
|= (addrext
<< BCM43xx_DMA32_DCTL_ADDREXT_SHIFT
)
87 & BCM43xx_DMA32_DCTL_ADDREXT_MASK
;
89 desc
->dma32
.control
= cpu_to_le32(ctl
);
90 desc
->dma32
.address
= cpu_to_le32(addr
);
93 static void op32_poke_tx(struct bcm43xx_dmaring
*ring
, int slot
)
95 bcm43xx_dma_write(ring
, BCM43xx_DMA32_TXINDEX
,
96 (u32
)(slot
* sizeof(struct bcm43xx_dmadesc32
)));
99 static void op32_tx_suspend(struct bcm43xx_dmaring
*ring
)
101 bcm43xx_dma_write(ring
, BCM43xx_DMA32_TXCTL
,
102 bcm43xx_dma_read(ring
, BCM43xx_DMA32_TXCTL
)
103 | BCM43xx_DMA32_TXSUSPEND
);
106 static void op32_tx_resume(struct bcm43xx_dmaring
*ring
)
108 bcm43xx_dma_write(ring
, BCM43xx_DMA32_TXCTL
,
109 bcm43xx_dma_read(ring
, BCM43xx_DMA32_TXCTL
)
110 & ~BCM43xx_DMA32_TXSUSPEND
);
113 static int op32_get_current_rxslot(struct bcm43xx_dmaring
*ring
)
117 val
= bcm43xx_dma_read(ring
, BCM43xx_DMA32_RXSTATUS
);
118 val
&= BCM43xx_DMA32_RXDPTR
;
120 return (val
/ sizeof(struct bcm43xx_dmadesc32
));
123 static void op32_set_current_rxslot(struct bcm43xx_dmaring
*ring
,
126 bcm43xx_dma_write(ring
, BCM43xx_DMA32_RXINDEX
,
127 (u32
)(slot
* sizeof(struct bcm43xx_dmadesc32
)));
130 static const struct bcm43xx_dma_ops dma32_ops
= {
131 .idx2desc
= op32_idx2desc
,
132 .fill_descriptor
= op32_fill_descriptor
,
133 .poke_tx
= op32_poke_tx
,
134 .tx_suspend
= op32_tx_suspend
,
135 .tx_resume
= op32_tx_resume
,
136 .get_current_rxslot
= op32_get_current_rxslot
,
137 .set_current_rxslot
= op32_set_current_rxslot
,
142 struct bcm43xx_dmadesc_generic
* op64_idx2desc(struct bcm43xx_dmaring
*ring
,
144 struct bcm43xx_dmadesc_meta
**meta
)
146 struct bcm43xx_dmadesc64
*desc
;
148 *meta
= &(ring
->meta
[slot
]);
149 desc
= ring
->descbase
;
150 desc
= &(desc
[slot
]);
152 return (struct bcm43xx_dmadesc_generic
*)desc
;
155 static void op64_fill_descriptor(struct bcm43xx_dmaring
*ring
,
156 struct bcm43xx_dmadesc_generic
*desc
,
157 dma_addr_t dmaaddr
, u16 bufsize
,
158 int start
, int end
, int irq
)
160 struct bcm43xx_dmadesc64
*descbase
= ring
->descbase
;
162 u32 ctl0
= 0, ctl1
= 0;
166 slot
= (int)(&(desc
->dma64
) - descbase
);
167 assert(slot
>= 0 && slot
< ring
->nr_slots
);
169 addrlo
= (u32
)(dmaaddr
& 0xFFFFFFFF);
170 addrhi
= (((u64
)dmaaddr
>> 32) & ~SSB_DMA_TRANSLATION_MASK
);
171 addrext
= (((u64
)dmaaddr
>> 32) & SSB_DMA_TRANSLATION_MASK
)
172 >> SSB_DMA_TRANSLATION_SHIFT
;
173 addrhi
|= ssb_dma_translation(ring
->dev
->dev
);
174 if (slot
== ring
->nr_slots
- 1)
175 ctl0
|= BCM43xx_DMA64_DCTL0_DTABLEEND
;
177 ctl0
|= BCM43xx_DMA64_DCTL0_FRAMESTART
;
179 ctl0
|= BCM43xx_DMA64_DCTL0_FRAMEEND
;
181 ctl0
|= BCM43xx_DMA64_DCTL0_IRQ
;
182 ctl1
|= (bufsize
- ring
->frameoffset
)
183 & BCM43xx_DMA64_DCTL1_BYTECNT
;
184 ctl1
|= (addrext
<< BCM43xx_DMA64_DCTL1_ADDREXT_SHIFT
)
185 & BCM43xx_DMA64_DCTL1_ADDREXT_MASK
;
187 desc
->dma64
.control0
= cpu_to_le32(ctl0
);
188 desc
->dma64
.control1
= cpu_to_le32(ctl1
);
189 desc
->dma64
.address_low
= cpu_to_le32(addrlo
);
190 desc
->dma64
.address_high
= cpu_to_le32(addrhi
);
193 static void op64_poke_tx(struct bcm43xx_dmaring
*ring
, int slot
)
195 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXINDEX
,
196 (u32
)(slot
* sizeof(struct bcm43xx_dmadesc64
)));
199 static void op64_tx_suspend(struct bcm43xx_dmaring
*ring
)
201 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXCTL
,
202 bcm43xx_dma_read(ring
, BCM43xx_DMA64_TXCTL
)
203 | BCM43xx_DMA64_TXSUSPEND
);
206 static void op64_tx_resume(struct bcm43xx_dmaring
*ring
)
208 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXCTL
,
209 bcm43xx_dma_read(ring
, BCM43xx_DMA64_TXCTL
)
210 & ~BCM43xx_DMA64_TXSUSPEND
);
213 static int op64_get_current_rxslot(struct bcm43xx_dmaring
*ring
)
217 val
= bcm43xx_dma_read(ring
, BCM43xx_DMA64_RXSTATUS
);
218 val
&= BCM43xx_DMA64_RXSTATDPTR
;
220 return (val
/ sizeof(struct bcm43xx_dmadesc64
));
223 static void op64_set_current_rxslot(struct bcm43xx_dmaring
*ring
,
226 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXINDEX
,
227 (u32
)(slot
* sizeof(struct bcm43xx_dmadesc64
)));
230 static const struct bcm43xx_dma_ops dma64_ops
= {
231 .idx2desc
= op64_idx2desc
,
232 .fill_descriptor
= op64_fill_descriptor
,
233 .poke_tx
= op64_poke_tx
,
234 .tx_suspend
= op64_tx_suspend
,
235 .tx_resume
= op64_tx_resume
,
236 .get_current_rxslot
= op64_get_current_rxslot
,
237 .set_current_rxslot
= op64_set_current_rxslot
,
241 static inline int free_slots(struct bcm43xx_dmaring
*ring
)
243 return (ring
->nr_slots
- ring
->used_slots
);
246 static inline int next_slot(struct bcm43xx_dmaring
*ring
, int slot
)
248 assert(slot
>= -1 && slot
<= ring
->nr_slots
- 1);
249 if (slot
== ring
->nr_slots
- 1)
254 static inline int prev_slot(struct bcm43xx_dmaring
*ring
, int slot
)
256 assert(slot
>= 0 && slot
<= ring
->nr_slots
- 1);
258 return ring
->nr_slots
- 1;
262 /* Request a slot for usage. */
264 int request_slot(struct bcm43xx_dmaring
*ring
)
269 assert(!ring
->stopped
);
270 assert(free_slots(ring
) != 0);
272 slot
= next_slot(ring
, ring
->current_slot
);
273 ring
->current_slot
= slot
;
276 #ifdef CONFIG_BCM43XX_MAC80211_DEBUG
277 if (ring
->used_slots
> ring
->max_used_slots
)
278 ring
->max_used_slots
= ring
->used_slots
;
279 #endif /* CONFIG_BCM43XX_MAC80211_DEBUG*/
284 /* Return a slot to the free slots. */
286 void return_slot(struct bcm43xx_dmaring
*ring
, int slot
)
293 u16
bcm43xx_dmacontroller_base(int dma64bit
, int controller_idx
)
295 static const u16 map64
[] = {
296 BCM43xx_MMIO_DMA64_BASE0
,
297 BCM43xx_MMIO_DMA64_BASE1
,
298 BCM43xx_MMIO_DMA64_BASE2
,
299 BCM43xx_MMIO_DMA64_BASE3
,
300 BCM43xx_MMIO_DMA64_BASE4
,
301 BCM43xx_MMIO_DMA64_BASE5
,
303 static const u16 map32
[] = {
304 BCM43xx_MMIO_DMA32_BASE0
,
305 BCM43xx_MMIO_DMA32_BASE1
,
306 BCM43xx_MMIO_DMA32_BASE2
,
307 BCM43xx_MMIO_DMA32_BASE3
,
308 BCM43xx_MMIO_DMA32_BASE4
,
309 BCM43xx_MMIO_DMA32_BASE5
,
313 assert(controller_idx
>= 0 &&
314 controller_idx
< ARRAY_SIZE(map64
));
315 return map64
[controller_idx
];
317 assert(controller_idx
>= 0 &&
318 controller_idx
< ARRAY_SIZE(map32
));
319 return map32
[controller_idx
];
323 dma_addr_t
map_descbuffer(struct bcm43xx_dmaring
*ring
,
331 dmaaddr
= dma_map_single(ring
->dev
->dev
->dev
,
335 dmaaddr
= dma_map_single(ring
->dev
->dev
->dev
,
344 void unmap_descbuffer(struct bcm43xx_dmaring
*ring
,
350 dma_unmap_single(ring
->dev
->dev
->dev
,
354 dma_unmap_single(ring
->dev
->dev
->dev
,
361 void sync_descbuffer_for_cpu(struct bcm43xx_dmaring
*ring
,
367 dma_sync_single_for_cpu(ring
->dev
->dev
->dev
,
368 addr
, len
, DMA_FROM_DEVICE
);
372 void sync_descbuffer_for_device(struct bcm43xx_dmaring
*ring
,
378 dma_sync_single_for_device(ring
->dev
->dev
->dev
,
379 addr
, len
, DMA_FROM_DEVICE
);
383 void free_descriptor_buffer(struct bcm43xx_dmaring
*ring
,
384 struct bcm43xx_dmadesc_meta
*meta
,
389 dev_kfree_skb_irq(meta
->skb
);
391 dev_kfree_skb(meta
->skb
);
396 static int alloc_ringmemory(struct bcm43xx_dmaring
*ring
)
398 struct device
*dev
= ring
->dev
->dev
->dev
;
400 ring
->descbase
= dma_alloc_coherent(dev
, BCM43xx_DMA_RINGMEMSIZE
,
401 &(ring
->dmabase
), GFP_KERNEL
);
402 if (!ring
->descbase
) {
403 printk(KERN_ERR PFX
"DMA ringmemory allocation failed\n");
406 memset(ring
->descbase
, 0, BCM43xx_DMA_RINGMEMSIZE
);
411 static void free_ringmemory(struct bcm43xx_dmaring
*ring
)
413 struct device
*dev
= ring
->dev
->dev
->dev
;
415 dma_free_coherent(dev
, BCM43xx_DMA_RINGMEMSIZE
,
416 ring
->descbase
, ring
->dmabase
);
419 /* Reset the RX DMA channel */
420 int bcm43xx_dmacontroller_rx_reset(struct bcm43xx_wldev
*dev
,
421 u16 mmio_base
, int dma64
)
427 offset
= dma64
? BCM43xx_DMA64_RXCTL
: BCM43xx_DMA32_RXCTL
;
428 bcm43xx_write32(dev
, mmio_base
+ offset
, 0);
429 for (i
= 0; i
< 1000; i
++) {
430 offset
= dma64
? BCM43xx_DMA64_RXSTATUS
: BCM43xx_DMA32_RXSTATUS
;
431 value
= bcm43xx_read32(dev
, mmio_base
+ offset
);
433 value
&= BCM43xx_DMA64_RXSTAT
;
434 if (value
== BCM43xx_DMA64_RXSTAT_DISABLED
) {
439 value
&= BCM43xx_DMA32_RXSTATE
;
440 if (value
== BCM43xx_DMA32_RXSTAT_DISABLED
) {
448 printk(KERN_ERR PFX
"Error: Wait on DMA RX status timed out.\n");
455 /* Reset the RX DMA channel */
456 int bcm43xx_dmacontroller_tx_reset(struct bcm43xx_wldev
*dev
,
457 u16 mmio_base
, int dma64
)
463 for (i
= 0; i
< 1000; i
++) {
464 offset
= dma64
? BCM43xx_DMA64_TXSTATUS
: BCM43xx_DMA32_TXSTATUS
;
465 value
= bcm43xx_read32(dev
, mmio_base
+ offset
);
467 value
&= BCM43xx_DMA64_TXSTAT
;
468 if (value
== BCM43xx_DMA64_TXSTAT_DISABLED
||
469 value
== BCM43xx_DMA64_TXSTAT_IDLEWAIT
||
470 value
== BCM43xx_DMA64_TXSTAT_STOPPED
)
473 value
&= BCM43xx_DMA32_TXSTATE
;
474 if (value
== BCM43xx_DMA32_TXSTAT_DISABLED
||
475 value
== BCM43xx_DMA32_TXSTAT_IDLEWAIT
||
476 value
== BCM43xx_DMA32_TXSTAT_STOPPED
)
481 offset
= dma64
? BCM43xx_DMA64_TXCTL
: BCM43xx_DMA32_TXCTL
;
482 bcm43xx_write32(dev
, mmio_base
+ offset
, 0);
483 for (i
= 0; i
< 1000; i
++) {
484 offset
= dma64
? BCM43xx_DMA64_TXSTATUS
: BCM43xx_DMA32_TXSTATUS
;
485 value
= bcm43xx_read32(dev
, mmio_base
+ offset
);
487 value
&= BCM43xx_DMA64_TXSTAT
;
488 if (value
== BCM43xx_DMA64_TXSTAT_DISABLED
) {
493 value
&= BCM43xx_DMA32_TXSTATE
;
494 if (value
== BCM43xx_DMA32_TXSTAT_DISABLED
) {
502 printk(KERN_ERR PFX
"Error: Wait on DMA TX status timed out.\n");
505 /* ensure the reset is completed. */
511 static int setup_rx_descbuffer(struct bcm43xx_dmaring
*ring
,
512 struct bcm43xx_dmadesc_generic
*desc
,
513 struct bcm43xx_dmadesc_meta
*meta
,
516 struct bcm43xx_rxhdr_fw4
*rxhdr
;
517 struct bcm43xx_hwtxstatus
*txstat
;
523 skb
= __dev_alloc_skb(ring
->rx_buffersize
, gfp_flags
);
526 dmaaddr
= map_descbuffer(ring
, skb
->data
,
527 ring
->rx_buffersize
, 0);
528 if (dma_mapping_error(dmaaddr
)) {
529 /* ugh. try to realloc in zone_dma */
530 gfp_flags
|= GFP_DMA
;
532 dev_kfree_skb_any(skb
);
534 skb
= __dev_alloc_skb(ring
->rx_buffersize
, gfp_flags
);
537 dmaaddr
= map_descbuffer(ring
, skb
->data
,
538 ring
->rx_buffersize
, 0);
541 if (dma_mapping_error(dmaaddr
)) {
542 dev_kfree_skb_any(skb
);
547 meta
->dmaaddr
= dmaaddr
;
548 ring
->ops
->fill_descriptor(ring
, desc
, dmaaddr
,
549 ring
->rx_buffersize
, 0, 0, 0);
551 rxhdr
= (struct bcm43xx_rxhdr_fw4
*)(skb
->data
);
552 rxhdr
->frame_len
= 0;
553 txstat
= (struct bcm43xx_hwtxstatus
*)(skb
->data
);
559 /* Allocate the initial descbuffers.
560 * This is used for an RX ring only.
562 static int alloc_initial_descbuffers(struct bcm43xx_dmaring
*ring
)
564 int i
, err
= -ENOMEM
;
565 struct bcm43xx_dmadesc_generic
*desc
;
566 struct bcm43xx_dmadesc_meta
*meta
;
568 for (i
= 0; i
< ring
->nr_slots
; i
++) {
569 desc
= ring
->ops
->idx2desc(ring
, i
, &meta
);
571 err
= setup_rx_descbuffer(ring
, desc
, meta
, GFP_KERNEL
);
573 printk(KERN_ERR PFX
"Failed to allocate initial descbuffers\n");
578 ring
->used_slots
= ring
->nr_slots
;
584 for (i
--; i
>= 0; i
--) {
585 desc
= ring
->ops
->idx2desc(ring
, i
, &meta
);
587 unmap_descbuffer(ring
, meta
->dmaaddr
, ring
->rx_buffersize
, 0);
588 dev_kfree_skb(meta
->skb
);
593 /* Do initial setup of the DMA controller.
594 * Reset the controller, write the ring busaddress
595 * and switch the "enable" bit on.
597 static int dmacontroller_setup(struct bcm43xx_dmaring
*ring
)
602 u32 trans
= ssb_dma_translation(ring
->dev
->dev
);
606 u64 ringbase
= (u64
)(ring
->dmabase
);
608 addrext
= ((ringbase
>> 32) & SSB_DMA_TRANSLATION_MASK
)
609 >> SSB_DMA_TRANSLATION_SHIFT
;
610 value
= BCM43xx_DMA64_TXENABLE
;
611 value
|= (addrext
<< BCM43xx_DMA64_TXADDREXT_SHIFT
)
612 & BCM43xx_DMA64_TXADDREXT_MASK
;
613 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXCTL
, value
);
614 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXRINGLO
,
615 (ringbase
& 0xFFFFFFFF));
616 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXRINGHI
,
617 ((ringbase
>> 32) & ~SSB_DMA_TRANSLATION_MASK
)
620 u32 ringbase
= (u32
)(ring
->dmabase
);
622 addrext
= (ringbase
& SSB_DMA_TRANSLATION_MASK
)
623 >> SSB_DMA_TRANSLATION_SHIFT
;
624 value
= BCM43xx_DMA32_TXENABLE
;
625 value
|= (addrext
<< BCM43xx_DMA32_TXADDREXT_SHIFT
)
626 & BCM43xx_DMA32_TXADDREXT_MASK
;
627 bcm43xx_dma_write(ring
, BCM43xx_DMA32_TXCTL
, value
);
628 bcm43xx_dma_write(ring
, BCM43xx_DMA32_TXRING
,
629 (ringbase
& ~SSB_DMA_TRANSLATION_MASK
)
633 err
= alloc_initial_descbuffers(ring
);
637 u64 ringbase
= (u64
)(ring
->dmabase
);
639 addrext
= ((ringbase
>> 32) & SSB_DMA_TRANSLATION_MASK
)
640 >> SSB_DMA_TRANSLATION_SHIFT
;
641 value
= (ring
->frameoffset
<< BCM43xx_DMA64_RXFROFF_SHIFT
);
642 value
|= BCM43xx_DMA64_RXENABLE
;
643 value
|= (addrext
<< BCM43xx_DMA64_RXADDREXT_SHIFT
)
644 & BCM43xx_DMA64_RXADDREXT_MASK
;
645 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXCTL
, value
);
646 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXRINGLO
,
647 (ringbase
& 0xFFFFFFFF));
648 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXRINGHI
,
649 ((ringbase
>> 32) & ~SSB_DMA_TRANSLATION_MASK
)
651 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXINDEX
, 200);
653 u32 ringbase
= (u32
)(ring
->dmabase
);
655 addrext
= (ringbase
& SSB_DMA_TRANSLATION_MASK
)
656 >> SSB_DMA_TRANSLATION_SHIFT
;
657 value
= (ring
->frameoffset
<< BCM43xx_DMA32_RXFROFF_SHIFT
);
658 value
|= BCM43xx_DMA32_RXENABLE
;
659 value
|= (addrext
<< BCM43xx_DMA32_RXADDREXT_SHIFT
)
660 & BCM43xx_DMA32_RXADDREXT_MASK
;
661 bcm43xx_dma_write(ring
, BCM43xx_DMA32_RXCTL
, value
);
662 bcm43xx_dma_write(ring
, BCM43xx_DMA32_RXRING
,
663 (ringbase
& ~SSB_DMA_TRANSLATION_MASK
)
665 bcm43xx_dma_write(ring
, BCM43xx_DMA32_RXINDEX
, 200);
673 /* Shutdown the DMA controller. */
674 static void dmacontroller_cleanup(struct bcm43xx_dmaring
*ring
)
677 bcm43xx_dmacontroller_tx_reset(ring
->dev
, ring
->mmio_base
, ring
->dma64
);
679 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXRINGLO
, 0);
680 bcm43xx_dma_write(ring
, BCM43xx_DMA64_TXRINGHI
, 0);
682 bcm43xx_dma_write(ring
, BCM43xx_DMA32_TXRING
, 0);
684 bcm43xx_dmacontroller_rx_reset(ring
->dev
, ring
->mmio_base
, ring
->dma64
);
686 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXRINGLO
, 0);
687 bcm43xx_dma_write(ring
, BCM43xx_DMA64_RXRINGHI
, 0);
689 bcm43xx_dma_write(ring
, BCM43xx_DMA32_RXRING
, 0);
693 static void free_all_descbuffers(struct bcm43xx_dmaring
*ring
)
695 struct bcm43xx_dmadesc_generic
*desc
;
696 struct bcm43xx_dmadesc_meta
*meta
;
699 if (!ring
->used_slots
)
701 for (i
= 0; i
< ring
->nr_slots
; i
++) {
702 desc
= ring
->ops
->idx2desc(ring
, i
, &meta
);
709 unmap_descbuffer(ring
, meta
->dmaaddr
,
712 unmap_descbuffer(ring
, meta
->dmaaddr
,
713 ring
->rx_buffersize
, 0);
715 free_descriptor_buffer(ring
, meta
, 0);
719 static u64
supported_dma_mask(struct bcm43xx_wldev
*dev
)
724 tmp
= bcm43xx_read32(dev
, SSB_TMSHIGH
);
725 if (tmp
& SSB_TMSHIGH_DMA64
)
726 return DMA_64BIT_MASK
;
727 mmio_base
= bcm43xx_dmacontroller_base(0, 0);
729 mmio_base
+ BCM43xx_DMA32_TXCTL
,
730 BCM43xx_DMA32_TXADDREXT_MASK
);
731 tmp
= bcm43xx_read32(dev
,
732 mmio_base
+ BCM43xx_DMA32_TXCTL
);
733 if (tmp
& BCM43xx_DMA32_TXADDREXT_MASK
)
734 return DMA_32BIT_MASK
;
736 return DMA_30BIT_MASK
;
739 /* Main initialization function. */
741 struct bcm43xx_dmaring
* bcm43xx_setup_dmaring(struct bcm43xx_wldev
*dev
,
742 int controller_index
,
746 struct bcm43xx_dmaring
*ring
;
751 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
755 nr_slots
= BCM43xx_RXRING_SLOTS
;
757 nr_slots
= BCM43xx_TXRING_SLOTS
;
759 ring
->meta
= kcalloc(nr_slots
, sizeof(struct bcm43xx_dmadesc_meta
),
764 ring
->txhdr_cache
= kcalloc(nr_slots
,
765 sizeof(struct bcm43xx_txhdr_fw4
),
767 if (!ring
->txhdr_cache
)
770 /* test for ability to dma to txhdr_cache */
771 dma_test
= dma_map_single(dev
->dev
->dev
,
772 ring
->txhdr_cache
, sizeof(struct bcm43xx_txhdr_fw4
),
775 if (dma_mapping_error(dma_test
)) {
777 kfree(ring
->txhdr_cache
);
778 ring
->txhdr_cache
= kcalloc(nr_slots
,
779 sizeof(struct bcm43xx_txhdr_fw4
),
780 GFP_KERNEL
| GFP_DMA
);
781 if (!ring
->txhdr_cache
)
784 dma_test
= dma_map_single(dev
->dev
->dev
,
785 ring
->txhdr_cache
, sizeof(struct bcm43xx_txhdr_fw4
),
788 if (dma_mapping_error(dma_test
))
789 goto err_kfree_txhdr_cache
;
792 dma_unmap_single(dev
->dev
->dev
,
793 dma_test
, sizeof(struct bcm43xx_txhdr_fw4
),
798 ring
->nr_slots
= nr_slots
;
799 ring
->mmio_base
= bcm43xx_dmacontroller_base(dma64
, controller_index
);
800 ring
->index
= controller_index
;
801 ring
->dma64
= !!dma64
;
803 ring
->ops
= &dma64_ops
;
805 ring
->ops
= &dma32_ops
;
808 ring
->current_slot
= -1;
810 if (ring
->index
== 0) {
811 ring
->rx_buffersize
= BCM43xx_DMA0_RX_BUFFERSIZE
;
812 ring
->frameoffset
= BCM43xx_DMA0_RX_FRAMEOFFSET
;
813 } else if (ring
->index
== 3) {
814 ring
->rx_buffersize
= BCM43xx_DMA3_RX_BUFFERSIZE
;
815 ring
->frameoffset
= BCM43xx_DMA3_RX_FRAMEOFFSET
;
820 err
= alloc_ringmemory(ring
);
822 goto err_kfree_txhdr_cache
;
823 err
= dmacontroller_setup(ring
);
825 goto err_free_ringmemory
;
831 free_ringmemory(ring
);
832 err_kfree_txhdr_cache
:
833 kfree(ring
->txhdr_cache
);
842 /* Main cleanup function. */
843 static void bcm43xx_destroy_dmaring(struct bcm43xx_dmaring
*ring
)
848 dprintk(KERN_INFO PFX
"DMA-%s 0x%04X (%s) max used slots: %d/%d\n",
849 (ring
->dma64
) ? "64" : "32",
851 (ring
->tx
) ? "TX" : "RX",
852 ring
->max_used_slots
, ring
->nr_slots
);
853 /* Device IRQs are disabled prior entering this function,
854 * so no need to take care of concurrency with rx handler stuff.
856 dmacontroller_cleanup(ring
);
857 free_all_descbuffers(ring
);
858 free_ringmemory(ring
);
860 kfree(ring
->txhdr_cache
);
865 void bcm43xx_dma_free(struct bcm43xx_wldev
*dev
)
867 struct bcm43xx_dma
*dma
;
869 if (bcm43xx_using_pio(dev
))
873 bcm43xx_destroy_dmaring(dma
->rx_ring3
);
874 dma
->rx_ring3
= NULL
;
875 bcm43xx_destroy_dmaring(dma
->rx_ring0
);
876 dma
->rx_ring0
= NULL
;
878 bcm43xx_destroy_dmaring(dma
->tx_ring5
);
879 dma
->tx_ring5
= NULL
;
880 bcm43xx_destroy_dmaring(dma
->tx_ring4
);
881 dma
->tx_ring4
= NULL
;
882 bcm43xx_destroy_dmaring(dma
->tx_ring3
);
883 dma
->tx_ring3
= NULL
;
884 bcm43xx_destroy_dmaring(dma
->tx_ring2
);
885 dma
->tx_ring2
= NULL
;
886 bcm43xx_destroy_dmaring(dma
->tx_ring1
);
887 dma
->tx_ring1
= NULL
;
888 bcm43xx_destroy_dmaring(dma
->tx_ring0
);
889 dma
->tx_ring0
= NULL
;
892 int bcm43xx_dma_init(struct bcm43xx_wldev
*dev
)
894 struct bcm43xx_dma
*dma
= &dev
->dma
;
895 struct bcm43xx_dmaring
*ring
;
900 dmamask
= supported_dma_mask(dev
);
901 if (dmamask
== DMA_64BIT_MASK
)
904 err
= ssb_dma_set_mask(dev
->dev
, dmamask
);
906 #ifdef BCM43XX_MAC80211_PIO
907 printk(KERN_WARNING PFX
"DMA for this device not supported. "
908 "Falling back to PIO\n");
909 dev
->__using_pio
= 1;
912 printk(KERN_ERR PFX
"DMA for this device not supported and "
913 "no PIO support compiled in\n");
919 /* setup TX DMA channels. */
920 ring
= bcm43xx_setup_dmaring(dev
, 0, 1, dma64
);
923 dma
->tx_ring0
= ring
;
925 ring
= bcm43xx_setup_dmaring(dev
, 1, 1, dma64
);
927 goto err_destroy_tx0
;
928 dma
->tx_ring1
= ring
;
930 ring
= bcm43xx_setup_dmaring(dev
, 2, 1, dma64
);
932 goto err_destroy_tx1
;
933 dma
->tx_ring2
= ring
;
935 ring
= bcm43xx_setup_dmaring(dev
, 3, 1, dma64
);
937 goto err_destroy_tx2
;
938 dma
->tx_ring3
= ring
;
940 ring
= bcm43xx_setup_dmaring(dev
, 4, 1, dma64
);
942 goto err_destroy_tx3
;
943 dma
->tx_ring4
= ring
;
945 ring
= bcm43xx_setup_dmaring(dev
, 5, 1, dma64
);
947 goto err_destroy_tx4
;
948 dma
->tx_ring5
= ring
;
950 /* setup RX DMA channels. */
951 ring
= bcm43xx_setup_dmaring(dev
, 0, 0, dma64
);
953 goto err_destroy_tx5
;
954 dma
->rx_ring0
= ring
;
956 if (dev
->dev
->id
.revision
< 5) {
957 ring
= bcm43xx_setup_dmaring(dev
, 3, 0, dma64
);
959 goto err_destroy_rx0
;
960 dma
->rx_ring3
= ring
;
963 dprintk(KERN_INFO PFX
"%d-bit DMA initialized\n",
964 (dmamask
== DMA_64BIT_MASK
) ? 64 :
965 (dmamask
== DMA_32BIT_MASK
) ? 32 : 30);
971 bcm43xx_destroy_dmaring(dma
->rx_ring0
);
972 dma
->rx_ring0
= NULL
;
974 bcm43xx_destroy_dmaring(dma
->tx_ring5
);
975 dma
->tx_ring5
= NULL
;
977 bcm43xx_destroy_dmaring(dma
->tx_ring4
);
978 dma
->tx_ring4
= NULL
;
980 bcm43xx_destroy_dmaring(dma
->tx_ring3
);
981 dma
->tx_ring3
= NULL
;
983 bcm43xx_destroy_dmaring(dma
->tx_ring2
);
984 dma
->tx_ring2
= NULL
;
986 bcm43xx_destroy_dmaring(dma
->tx_ring1
);
987 dma
->tx_ring1
= NULL
;
989 bcm43xx_destroy_dmaring(dma
->tx_ring0
);
990 dma
->tx_ring0
= NULL
;
994 /* Generate a cookie for the TX header. */
995 static u16
generate_cookie(struct bcm43xx_dmaring
*ring
,
1000 /* Use the upper 4 bits of the cookie as
1001 * DMA controller ID and store the slot number
1002 * in the lower 12 bits.
1003 * Note that the cookie must never be 0, as this
1004 * is a special value used in RX path.
1006 switch (ring
->index
) {
1026 assert(((u16
)slot
& 0xF000) == 0x0000);
1027 cookie
|= (u16
)slot
;
1032 /* Inspect a cookie and find out to which controller/slot it belongs. */
1034 struct bcm43xx_dmaring
* parse_cookie(struct bcm43xx_wldev
*dev
,
1035 u16 cookie
, int *slot
)
1037 struct bcm43xx_dma
*dma
= &dev
->dma
;
1038 struct bcm43xx_dmaring
*ring
= NULL
;
1040 switch (cookie
& 0xF000) {
1042 ring
= dma
->tx_ring0
;
1045 ring
= dma
->tx_ring1
;
1048 ring
= dma
->tx_ring2
;
1051 ring
= dma
->tx_ring3
;
1054 ring
= dma
->tx_ring4
;
1057 ring
= dma
->tx_ring5
;
1062 *slot
= (cookie
& 0x0FFF);
1063 assert(ring
&& *slot
>= 0 && *slot
< ring
->nr_slots
);
1068 static int dma_tx_fragment(struct bcm43xx_dmaring
*ring
,
1069 struct sk_buff
*skb
,
1070 struct ieee80211_tx_control
*ctl
)
1072 const struct bcm43xx_dma_ops
*ops
= ring
->ops
;
1076 struct bcm43xx_dmadesc_generic
*desc
;
1077 struct bcm43xx_dmadesc_meta
*meta
;
1078 struct bcm43xx_dmadesc_meta
*meta_hdr
;
1079 struct sk_buff
*bounce_skb
;
1081 #define SLOTS_PER_PACKET 2
1082 assert(skb_shinfo(skb
)->nr_frags
== 0);
1084 /* Get a slot for the header. */
1085 slot
= request_slot(ring
);
1086 desc
= ops
->idx2desc(ring
, slot
, &meta_hdr
);
1087 memset(meta_hdr
, 0, sizeof(*meta_hdr
));
1089 header
= &(ring
->txhdr_cache
[slot
* sizeof(struct bcm43xx_txhdr_fw4
)]);
1090 bcm43xx_generate_txhdr(ring
->dev
, header
,
1091 skb
->data
, skb
->len
, ctl
,
1092 generate_cookie(ring
, slot
));
1094 meta_hdr
->dmaaddr
= map_descbuffer(ring
, (unsigned char *)header
,
1095 sizeof(struct bcm43xx_txhdr_fw4
), 1);
1096 if (dma_mapping_error(meta_hdr
->dmaaddr
))
1098 ops
->fill_descriptor(ring
, desc
, meta_hdr
->dmaaddr
,
1099 sizeof(struct bcm43xx_txhdr_fw4
), 1, 0, 0);
1101 /* Get a slot for the payload. */
1102 slot
= request_slot(ring
);
1103 desc
= ops
->idx2desc(ring
, slot
, &meta
);
1104 memset(meta
, 0, sizeof(*meta
));
1106 memcpy(&meta
->txstat
.control
, ctl
, sizeof(*ctl
));
1108 meta
->is_last_fragment
= 1;
1110 meta
->dmaaddr
= map_descbuffer(ring
, skb
->data
, skb
->len
, 1);
1111 /* create a bounce buffer in zone_dma on mapping failure. */
1112 if (dma_mapping_error(meta
->dmaaddr
)) {
1113 bounce_skb
= __dev_alloc_skb(skb
->len
, GFP_ATOMIC
| GFP_DMA
);
1119 memcpy(skb_put(bounce_skb
, skb
->len
), skb
->data
, skb
->len
);
1120 dev_kfree_skb_any(skb
);
1123 meta
->dmaaddr
= map_descbuffer(ring
, skb
->data
, skb
->len
, 1);
1124 if (dma_mapping_error(meta
->dmaaddr
)) {
1126 goto out_free_bounce
;
1130 ops
->fill_descriptor(ring
, desc
, meta
->dmaaddr
,
1133 /* Now transfer the whole frame. */
1135 ops
->poke_tx(ring
, next_slot(ring
, slot
));
1139 dev_kfree_skb_any(skb
);
1141 unmap_descbuffer(ring
, meta_hdr
->dmaaddr
,
1142 sizeof(struct bcm43xx_txhdr_fw4
), 1);
1146 int bcm43xx_dma_tx(struct bcm43xx_wldev
*dev
,
1147 struct sk_buff
*skb
,
1148 struct ieee80211_tx_control
*ctl
)
1150 struct bcm43xx_dmaring
*ring
= dev
->dma
.tx_ring1
;
1154 if (unlikely(free_slots(ring
) < SLOTS_PER_PACKET
)) {
1155 /* This should never trigger, as we call
1156 * ieee80211_stop_queue() when it's full.
1158 printkl(KERN_ERR PFX
"DMA queue overflow\n");
1159 return NETDEV_TX_BUSY
;
1162 err
= dma_tx_fragment(ring
, skb
, ctl
);
1163 if (unlikely(err
)) {
1164 printkl(KERN_ERR PFX
"DMA tx mapping failure\n");
1165 return NETDEV_TX_BUSY
;
1168 ring
->nr_tx_packets
++;
1169 if (free_slots(ring
) < SLOTS_PER_PACKET
) {
1170 /* FIXME: we currently only have one queue */
1171 ieee80211_stop_queue(dev
->wl
->hw
, 0);
1178 void bcm43xx_dma_handle_txstatus(struct bcm43xx_wldev
*dev
,
1179 const struct bcm43xx_txstatus
*status
)
1181 const struct bcm43xx_dma_ops
*ops
;
1182 struct bcm43xx_dmaring
*ring
;
1183 struct bcm43xx_dmadesc_generic
*desc
;
1184 struct bcm43xx_dmadesc_meta
*meta
;
1187 ring
= parse_cookie(dev
, status
->cookie
, &slot
);
1188 if (unlikely(!ring
))
1193 assert(slot
>= 0 && slot
< ring
->nr_slots
);
1194 desc
= ops
->idx2desc(ring
, slot
, &meta
);
1197 unmap_descbuffer(ring
, meta
->dmaaddr
, meta
->skb
->len
, 1);
1199 unmap_descbuffer(ring
, meta
->dmaaddr
, sizeof(struct bcm43xx_txhdr_fw4
), 1);
1201 if (meta
->is_last_fragment
) {
1203 /* Call back to inform the ieee80211 subsystem about the
1204 * status of the transmission.
1205 * Some fields of txstat are already filled in dma_tx().
1208 meta
->txstat
.flags
|= IEEE80211_TX_STATUS_ACK
;
1209 meta
->txstat
.retry_count
= status
->frame_count
- 1;
1210 ieee80211_tx_status_irqsafe(dev
->wl
->hw
, meta
->skb
, &(meta
->txstat
));
1211 /* skb is freed by ieee80211_tx_status_irqsafe() */
1214 /* No need to call free_descriptor_buffer here, as
1215 * this is only the txhdr, which is not allocated.
1217 assert(meta
->skb
== NULL
);
1219 /* Everything belonging to the slot is unmapped
1220 * and freed, so we can return it.
1222 return_slot(ring
, slot
);
1224 if (meta
->is_last_fragment
)
1226 slot
= next_slot(ring
, slot
);
1228 dev
->stats
.last_tx
= jiffies
;
1229 if (ring
->stopped
) {
1230 assert(free_slots(ring
) >= SLOTS_PER_PACKET
);
1231 /* FIXME: we currently only have one queue */
1232 ieee80211_wake_queue(dev
->wl
->hw
, 0);
1237 void bcm43xx_dma_get_tx_stats(struct bcm43xx_wldev
*dev
,
1238 struct ieee80211_tx_queue_stats
*stats
)
1240 struct bcm43xx_dma
*dma
= &dev
->dma
;
1241 struct bcm43xx_dmaring
*ring
;
1242 struct ieee80211_tx_queue_stats_data
*data
;
1244 ring
= dma
->tx_ring1
;
1245 data
= &(stats
->data
[0]);
1246 data
->len
= ring
->used_slots
/ SLOTS_PER_PACKET
;
1247 data
->limit
= ring
->nr_slots
/ SLOTS_PER_PACKET
;
1248 data
->count
= ring
->nr_tx_packets
;
1251 static void dma_rx(struct bcm43xx_dmaring
*ring
,
1254 const struct bcm43xx_dma_ops
*ops
= ring
->ops
;
1255 struct bcm43xx_dmadesc_generic
*desc
;
1256 struct bcm43xx_dmadesc_meta
*meta
;
1257 struct bcm43xx_rxhdr_fw4
*rxhdr
;
1258 struct sk_buff
*skb
;
1263 desc
= ops
->idx2desc(ring
, *slot
, &meta
);
1265 sync_descbuffer_for_cpu(ring
, meta
->dmaaddr
, ring
->rx_buffersize
);
1268 if (ring
->index
== 3) {
1269 /* We received an xmit status. */
1270 struct bcm43xx_hwtxstatus
*hw
= (struct bcm43xx_hwtxstatus
*)skb
->data
;
1273 while (hw
->cookie
== 0) {
1280 bcm43xx_handle_hwtxstatus(ring
->dev
, hw
);
1281 /* recycle the descriptor buffer. */
1282 sync_descbuffer_for_device(ring
, meta
->dmaaddr
, ring
->rx_buffersize
);
1286 rxhdr
= (struct bcm43xx_rxhdr_fw4
*)skb
->data
;
1287 len
= le16_to_cpu(rxhdr
->frame_len
);
1294 len
= le16_to_cpu(rxhdr
->frame_len
);
1295 } while (len
== 0 && i
++ < 5);
1296 if (unlikely(len
== 0)) {
1297 /* recycle the descriptor buffer. */
1298 sync_descbuffer_for_device(ring
, meta
->dmaaddr
,
1299 ring
->rx_buffersize
);
1303 if (unlikely(len
> ring
->rx_buffersize
)) {
1304 /* The data did not fit into one descriptor buffer
1305 * and is split over multiple buffers.
1306 * This should never happen, as we try to allocate buffers
1307 * big enough. So simply ignore this packet.
1313 desc
= ops
->idx2desc(ring
, *slot
, &meta
);
1314 /* recycle the descriptor buffer. */
1315 sync_descbuffer_for_device(ring
, meta
->dmaaddr
,
1316 ring
->rx_buffersize
);
1317 *slot
= next_slot(ring
, *slot
);
1319 tmp
-= ring
->rx_buffersize
;
1323 printkl(KERN_ERR PFX
"DMA RX buffer too small "
1324 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1325 len
, ring
->rx_buffersize
, cnt
);
1329 dmaaddr
= meta
->dmaaddr
;
1330 err
= setup_rx_descbuffer(ring
, desc
, meta
, GFP_ATOMIC
);
1331 if (unlikely(err
)) {
1332 dprintkl(KERN_ERR PFX
"DMA RX: setup_rx_descbuffer() failed\n");
1333 sync_descbuffer_for_device(ring
, dmaaddr
,
1334 ring
->rx_buffersize
);
1338 unmap_descbuffer(ring
, dmaaddr
, ring
->rx_buffersize
, 0);
1339 skb_put(skb
, len
+ ring
->frameoffset
);
1340 skb_pull(skb
, ring
->frameoffset
);
1342 bcm43xx_rx(ring
->dev
, skb
, rxhdr
);
1347 void bcm43xx_dma_rx(struct bcm43xx_dmaring
*ring
)
1349 const struct bcm43xx_dma_ops
*ops
= ring
->ops
;
1350 int slot
, current_slot
;
1351 #ifdef CONFIG_BCM43XX_MAC80211_DEBUG
1356 current_slot
= ops
->get_current_rxslot(ring
);
1357 assert(current_slot
>= 0 && current_slot
< ring
->nr_slots
);
1359 slot
= ring
->current_slot
;
1360 for ( ; slot
!= current_slot
; slot
= next_slot(ring
, slot
)) {
1361 dma_rx(ring
, &slot
);
1362 #ifdef CONFIG_BCM43XX_MAC80211_DEBUG
1363 if (++used_slots
> ring
->max_used_slots
)
1364 ring
->max_used_slots
= used_slots
;
1367 ops
->set_current_rxslot(ring
, slot
);
1368 ring
->current_slot
= slot
;
1371 void bcm43xx_dma_tx_suspend(struct bcm43xx_dmaring
*ring
)
1374 bcm43xx_power_saving_ctl_bits(ring
->dev
, -1, 1);
1375 ring
->ops
->tx_suspend(ring
);
1378 void bcm43xx_dma_tx_resume(struct bcm43xx_dmaring
*ring
)
1381 ring
->ops
->tx_resume(ring
);
1382 bcm43xx_power_saving_ctl_bits(ring
->dev
, -1, -1);