3 Broadcom B43 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.
36 #include <linux/dma-mapping.h>
37 #include <linux/pci.h>
38 #include <linux/delay.h>
39 #include <linux/skbuff.h>
40 #include <linux/etherdevice.h>
41 #include <asm/div64.h>
46 struct b43_dmadesc_generic *op32_idx2desc(struct b43_dmaring *ring,
48 struct b43_dmadesc_meta **meta)
50 struct b43_dmadesc32 *desc;
52 *meta = &(ring->meta[slot]);
53 desc = ring->descbase;
56 return (struct b43_dmadesc_generic *)desc;
59 static void op32_fill_descriptor(struct b43_dmaring *ring,
60 struct b43_dmadesc_generic *desc,
61 dma_addr_t dmaaddr, u16 bufsize,
62 int start, int end, int irq)
64 struct b43_dmadesc32 *descbase = ring->descbase;
70 slot = (int)(&(desc->dma32) - descbase);
71 B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
73 addr = (u32) (dmaaddr & ~SSB_DMA_TRANSLATION_MASK);
74 addrext = (u32) (dmaaddr & SSB_DMA_TRANSLATION_MASK)
75 >> SSB_DMA_TRANSLATION_SHIFT;
76 addr |= ssb_dma_translation(ring->dev->dev);
77 ctl = (bufsize - ring->frameoffset)
78 & B43_DMA32_DCTL_BYTECNT;
79 if (slot == ring->nr_slots - 1)
80 ctl |= B43_DMA32_DCTL_DTABLEEND;
82 ctl |= B43_DMA32_DCTL_FRAMESTART;
84 ctl |= B43_DMA32_DCTL_FRAMEEND;
86 ctl |= B43_DMA32_DCTL_IRQ;
87 ctl |= (addrext << B43_DMA32_DCTL_ADDREXT_SHIFT)
88 & B43_DMA32_DCTL_ADDREXT_MASK;
90 desc->dma32.control = cpu_to_le32(ctl);
91 desc->dma32.address = cpu_to_le32(addr);
94 static void op32_poke_tx(struct b43_dmaring *ring, int slot)
96 b43_dma_write(ring, B43_DMA32_TXINDEX,
97 (u32) (slot * sizeof(struct b43_dmadesc32)));
100 static void op32_tx_suspend(struct b43_dmaring *ring)
102 b43_dma_write(ring, B43_DMA32_TXCTL, b43_dma_read(ring, B43_DMA32_TXCTL)
103 | B43_DMA32_TXSUSPEND);
106 static void op32_tx_resume(struct b43_dmaring *ring)
108 b43_dma_write(ring, B43_DMA32_TXCTL, b43_dma_read(ring, B43_DMA32_TXCTL)
109 & ~B43_DMA32_TXSUSPEND);
112 static int op32_get_current_rxslot(struct b43_dmaring *ring)
116 val = b43_dma_read(ring, B43_DMA32_RXSTATUS);
117 val &= B43_DMA32_RXDPTR;
119 return (val / sizeof(struct b43_dmadesc32));
122 static void op32_set_current_rxslot(struct b43_dmaring *ring, int slot)
124 b43_dma_write(ring, B43_DMA32_RXINDEX,
125 (u32) (slot * sizeof(struct b43_dmadesc32)));
128 static const struct b43_dma_ops dma32_ops = {
129 .idx2desc = op32_idx2desc,
130 .fill_descriptor = op32_fill_descriptor,
131 .poke_tx = op32_poke_tx,
132 .tx_suspend = op32_tx_suspend,
133 .tx_resume = op32_tx_resume,
134 .get_current_rxslot = op32_get_current_rxslot,
135 .set_current_rxslot = op32_set_current_rxslot,
140 struct b43_dmadesc_generic *op64_idx2desc(struct b43_dmaring *ring,
142 struct b43_dmadesc_meta **meta)
144 struct b43_dmadesc64 *desc;
146 *meta = &(ring->meta[slot]);
147 desc = ring->descbase;
148 desc = &(desc[slot]);
150 return (struct b43_dmadesc_generic *)desc;
153 static void op64_fill_descriptor(struct b43_dmaring *ring,
154 struct b43_dmadesc_generic *desc,
155 dma_addr_t dmaaddr, u16 bufsize,
156 int start, int end, int irq)
158 struct b43_dmadesc64 *descbase = ring->descbase;
160 u32 ctl0 = 0, ctl1 = 0;
164 slot = (int)(&(desc->dma64) - descbase);
165 B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
167 addrlo = (u32) (dmaaddr & 0xFFFFFFFF);
168 addrhi = (((u64) dmaaddr >> 32) & ~SSB_DMA_TRANSLATION_MASK);
169 addrext = (((u64) dmaaddr >> 32) & SSB_DMA_TRANSLATION_MASK)
170 >> SSB_DMA_TRANSLATION_SHIFT;
171 addrhi |= (ssb_dma_translation(ring->dev->dev) << 1);
172 if (slot == ring->nr_slots - 1)
173 ctl0 |= B43_DMA64_DCTL0_DTABLEEND;
175 ctl0 |= B43_DMA64_DCTL0_FRAMESTART;
177 ctl0 |= B43_DMA64_DCTL0_FRAMEEND;
179 ctl0 |= B43_DMA64_DCTL0_IRQ;
180 ctl1 |= (bufsize - ring->frameoffset)
181 & B43_DMA64_DCTL1_BYTECNT;
182 ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT)
183 & B43_DMA64_DCTL1_ADDREXT_MASK;
185 desc->dma64.control0 = cpu_to_le32(ctl0);
186 desc->dma64.control1 = cpu_to_le32(ctl1);
187 desc->dma64.address_low = cpu_to_le32(addrlo);
188 desc->dma64.address_high = cpu_to_le32(addrhi);
191 static void op64_poke_tx(struct b43_dmaring *ring, int slot)
193 b43_dma_write(ring, B43_DMA64_TXINDEX,
194 (u32) (slot * sizeof(struct b43_dmadesc64)));
197 static void op64_tx_suspend(struct b43_dmaring *ring)
199 b43_dma_write(ring, B43_DMA64_TXCTL, b43_dma_read(ring, B43_DMA64_TXCTL)
200 | B43_DMA64_TXSUSPEND);
203 static void op64_tx_resume(struct b43_dmaring *ring)
205 b43_dma_write(ring, B43_DMA64_TXCTL, b43_dma_read(ring, B43_DMA64_TXCTL)
206 & ~B43_DMA64_TXSUSPEND);
209 static int op64_get_current_rxslot(struct b43_dmaring *ring)
213 val = b43_dma_read(ring, B43_DMA64_RXSTATUS);
214 val &= B43_DMA64_RXSTATDPTR;
216 return (val / sizeof(struct b43_dmadesc64));
219 static void op64_set_current_rxslot(struct b43_dmaring *ring, int slot)
221 b43_dma_write(ring, B43_DMA64_RXINDEX,
222 (u32) (slot * sizeof(struct b43_dmadesc64)));
225 static const struct b43_dma_ops dma64_ops = {
226 .idx2desc = op64_idx2desc,
227 .fill_descriptor = op64_fill_descriptor,
228 .poke_tx = op64_poke_tx,
229 .tx_suspend = op64_tx_suspend,
230 .tx_resume = op64_tx_resume,
231 .get_current_rxslot = op64_get_current_rxslot,
232 .set_current_rxslot = op64_set_current_rxslot,
235 static inline int free_slots(struct b43_dmaring *ring)
237 return (ring->nr_slots - ring->used_slots);
240 static inline int next_slot(struct b43_dmaring *ring, int slot)
242 B43_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1));
243 if (slot == ring->nr_slots - 1)
248 static inline int prev_slot(struct b43_dmaring *ring, int slot)
250 B43_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1));
252 return ring->nr_slots - 1;
256 #ifdef CONFIG_B43_DEBUG
257 static void update_max_used_slots(struct b43_dmaring *ring,
258 int current_used_slots)
260 if (current_used_slots <= ring->max_used_slots)
262 ring->max_used_slots = current_used_slots;
263 if (b43_debug(ring->dev, B43_DBG_DMAVERBOSE)) {
264 b43dbg(ring->dev->wl,
265 "max_used_slots increased to %d on %s ring %d\n",
266 ring->max_used_slots,
267 ring->tx ? "TX" : "RX", ring->index);
272 void update_max_used_slots(struct b43_dmaring *ring, int current_used_slots)
277 /* Request a slot for usage. */
278 static inline int request_slot(struct b43_dmaring *ring)
282 B43_WARN_ON(!ring->tx);
283 B43_WARN_ON(ring->stopped);
284 B43_WARN_ON(free_slots(ring) == 0);
286 slot = next_slot(ring, ring->current_slot);
287 ring->current_slot = slot;
290 update_max_used_slots(ring, ring->used_slots);
295 static u16 b43_dmacontroller_base(enum b43_dmatype type, int controller_idx)
297 static const u16 map64[] = {
298 B43_MMIO_DMA64_BASE0,
299 B43_MMIO_DMA64_BASE1,
300 B43_MMIO_DMA64_BASE2,
301 B43_MMIO_DMA64_BASE3,
302 B43_MMIO_DMA64_BASE4,
303 B43_MMIO_DMA64_BASE5,
305 static const u16 map32[] = {
306 B43_MMIO_DMA32_BASE0,
307 B43_MMIO_DMA32_BASE1,
308 B43_MMIO_DMA32_BASE2,
309 B43_MMIO_DMA32_BASE3,
310 B43_MMIO_DMA32_BASE4,
311 B43_MMIO_DMA32_BASE5,
314 if (type == B43_DMA_64BIT) {
315 B43_WARN_ON(!(controller_idx >= 0 &&
316 controller_idx < ARRAY_SIZE(map64)));
317 return map64[controller_idx];
319 B43_WARN_ON(!(controller_idx >= 0 &&
320 controller_idx < ARRAY_SIZE(map32)));
321 return map32[controller_idx];
325 dma_addr_t map_descbuffer(struct b43_dmaring *ring,
326 unsigned char *buf, size_t len, int tx)
331 dmaaddr = dma_map_single(ring->dev->dev->dev,
332 buf, len, DMA_TO_DEVICE);
334 dmaaddr = dma_map_single(ring->dev->dev->dev,
335 buf, len, DMA_FROM_DEVICE);
342 void unmap_descbuffer(struct b43_dmaring *ring,
343 dma_addr_t addr, size_t len, int tx)
346 dma_unmap_single(ring->dev->dev->dev, addr, len, DMA_TO_DEVICE);
348 dma_unmap_single(ring->dev->dev->dev,
349 addr, len, DMA_FROM_DEVICE);
354 void sync_descbuffer_for_cpu(struct b43_dmaring *ring,
355 dma_addr_t addr, size_t len)
357 B43_WARN_ON(ring->tx);
358 dma_sync_single_for_cpu(ring->dev->dev->dev,
359 addr, len, DMA_FROM_DEVICE);
363 void sync_descbuffer_for_device(struct b43_dmaring *ring,
364 dma_addr_t addr, size_t len)
366 B43_WARN_ON(ring->tx);
367 dma_sync_single_for_device(ring->dev->dev->dev,
368 addr, len, DMA_FROM_DEVICE);
372 void free_descriptor_buffer(struct b43_dmaring *ring,
373 struct b43_dmadesc_meta *meta)
376 dev_kfree_skb_any(meta->skb);
381 static int alloc_ringmemory(struct b43_dmaring *ring)
383 struct device *dev = ring->dev->dev->dev;
384 gfp_t flags = GFP_KERNEL;
386 /* The specs call for 4K buffers for 30- and 32-bit DMA with 4K
387 * alignment and 8K buffers for 64-bit DMA with 8K alignment. Testing
388 * has shown that 4K is sufficient for the latter as long as the buffer
389 * does not cross an 8K boundary.
391 * For unknown reasons - possibly a hardware error - the BCM4311 rev
392 * 02, which uses 64-bit DMA, needs the ring buffer in very low memory,
393 * which accounts for the GFP_DMA flag below.
395 if (ring->type == B43_DMA_64BIT)
397 ring->descbase = dma_alloc_coherent(dev, B43_DMA_RINGMEMSIZE,
398 &(ring->dmabase), flags);
399 if (!ring->descbase) {
400 b43err(ring->dev->wl, "DMA ringmemory allocation failed\n");
403 memset(ring->descbase, 0, B43_DMA_RINGMEMSIZE);
408 static void free_ringmemory(struct b43_dmaring *ring)
410 struct device *dev = ring->dev->dev->dev;
412 dma_free_coherent(dev, B43_DMA_RINGMEMSIZE,
413 ring->descbase, ring->dmabase);
416 /* Reset the RX DMA channel */
417 static int b43_dmacontroller_rx_reset(struct b43_wldev *dev, u16 mmio_base,
418 enum b43_dmatype type)
426 offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXCTL : B43_DMA32_RXCTL;
427 b43_write32(dev, mmio_base + offset, 0);
428 for (i = 0; i < 10; i++) {
429 offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXSTATUS :
431 value = b43_read32(dev, mmio_base + offset);
432 if (type == B43_DMA_64BIT) {
433 value &= B43_DMA64_RXSTAT;
434 if (value == B43_DMA64_RXSTAT_DISABLED) {
439 value &= B43_DMA32_RXSTATE;
440 if (value == B43_DMA32_RXSTAT_DISABLED) {
448 b43err(dev->wl, "DMA RX reset timed out\n");
455 /* Reset the TX DMA channel */
456 static int b43_dmacontroller_tx_reset(struct b43_wldev *dev, u16 mmio_base,
457 enum b43_dmatype type)
465 for (i = 0; i < 10; i++) {
466 offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS :
468 value = b43_read32(dev, mmio_base + offset);
469 if (type == B43_DMA_64BIT) {
470 value &= B43_DMA64_TXSTAT;
471 if (value == B43_DMA64_TXSTAT_DISABLED ||
472 value == B43_DMA64_TXSTAT_IDLEWAIT ||
473 value == B43_DMA64_TXSTAT_STOPPED)
476 value &= B43_DMA32_TXSTATE;
477 if (value == B43_DMA32_TXSTAT_DISABLED ||
478 value == B43_DMA32_TXSTAT_IDLEWAIT ||
479 value == B43_DMA32_TXSTAT_STOPPED)
484 offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXCTL : B43_DMA32_TXCTL;
485 b43_write32(dev, mmio_base + offset, 0);
486 for (i = 0; i < 10; i++) {
487 offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS :
489 value = b43_read32(dev, mmio_base + offset);
490 if (type == B43_DMA_64BIT) {
491 value &= B43_DMA64_TXSTAT;
492 if (value == B43_DMA64_TXSTAT_DISABLED) {
497 value &= B43_DMA32_TXSTATE;
498 if (value == B43_DMA32_TXSTAT_DISABLED) {
506 b43err(dev->wl, "DMA TX reset timed out\n");
509 /* ensure the reset is completed. */
515 /* Check if a DMA mapping address is invalid. */
516 static bool b43_dma_mapping_error(struct b43_dmaring *ring,
520 if (unlikely(dma_mapping_error(addr)))
523 switch (ring->type) {
525 if ((u64)addr + buffersize > (1ULL << 30))
529 if ((u64)addr + buffersize > (1ULL << 32))
533 /* Currently we can't have addresses beyond
534 * 64bit in the kernel. */
538 /* The address is OK. */
542 static int setup_rx_descbuffer(struct b43_dmaring *ring,
543 struct b43_dmadesc_generic *desc,
544 struct b43_dmadesc_meta *meta, gfp_t gfp_flags)
546 struct b43_rxhdr_fw4 *rxhdr;
547 struct b43_hwtxstatus *txstat;
551 B43_WARN_ON(ring->tx);
553 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
556 dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0);
557 if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize)) {
558 /* ugh. try to realloc in zone_dma */
559 gfp_flags |= GFP_DMA;
561 dev_kfree_skb_any(skb);
563 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
566 dmaaddr = map_descbuffer(ring, skb->data,
567 ring->rx_buffersize, 0);
570 if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize)) {
571 dev_kfree_skb_any(skb);
576 meta->dmaaddr = dmaaddr;
577 ring->ops->fill_descriptor(ring, desc, dmaaddr,
578 ring->rx_buffersize, 0, 0, 0);
580 rxhdr = (struct b43_rxhdr_fw4 *)(skb->data);
581 rxhdr->frame_len = 0;
582 txstat = (struct b43_hwtxstatus *)(skb->data);
588 /* Allocate the initial descbuffers.
589 * This is used for an RX ring only.
591 static int alloc_initial_descbuffers(struct b43_dmaring *ring)
593 int i, err = -ENOMEM;
594 struct b43_dmadesc_generic *desc;
595 struct b43_dmadesc_meta *meta;
597 for (i = 0; i < ring->nr_slots; i++) {
598 desc = ring->ops->idx2desc(ring, i, &meta);
600 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
602 b43err(ring->dev->wl,
603 "Failed to allocate initial descbuffers\n");
608 ring->used_slots = ring->nr_slots;
614 for (i--; i >= 0; i--) {
615 desc = ring->ops->idx2desc(ring, i, &meta);
617 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
618 dev_kfree_skb(meta->skb);
623 /* Do initial setup of the DMA controller.
624 * Reset the controller, write the ring busaddress
625 * and switch the "enable" bit on.
627 static int dmacontroller_setup(struct b43_dmaring *ring)
632 u32 trans = ssb_dma_translation(ring->dev->dev);
635 if (ring->type == B43_DMA_64BIT) {
636 u64 ringbase = (u64) (ring->dmabase);
638 addrext = ((ringbase >> 32) & SSB_DMA_TRANSLATION_MASK)
639 >> SSB_DMA_TRANSLATION_SHIFT;
640 value = B43_DMA64_TXENABLE;
641 value |= (addrext << B43_DMA64_TXADDREXT_SHIFT)
642 & B43_DMA64_TXADDREXT_MASK;
643 b43_dma_write(ring, B43_DMA64_TXCTL, value);
644 b43_dma_write(ring, B43_DMA64_TXRINGLO,
645 (ringbase & 0xFFFFFFFF));
646 b43_dma_write(ring, B43_DMA64_TXRINGHI,
648 ~SSB_DMA_TRANSLATION_MASK)
651 u32 ringbase = (u32) (ring->dmabase);
653 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
654 >> SSB_DMA_TRANSLATION_SHIFT;
655 value = B43_DMA32_TXENABLE;
656 value |= (addrext << B43_DMA32_TXADDREXT_SHIFT)
657 & B43_DMA32_TXADDREXT_MASK;
658 b43_dma_write(ring, B43_DMA32_TXCTL, value);
659 b43_dma_write(ring, B43_DMA32_TXRING,
660 (ringbase & ~SSB_DMA_TRANSLATION_MASK)
664 err = alloc_initial_descbuffers(ring);
667 if (ring->type == B43_DMA_64BIT) {
668 u64 ringbase = (u64) (ring->dmabase);
670 addrext = ((ringbase >> 32) & SSB_DMA_TRANSLATION_MASK)
671 >> SSB_DMA_TRANSLATION_SHIFT;
672 value = (ring->frameoffset << B43_DMA64_RXFROFF_SHIFT);
673 value |= B43_DMA64_RXENABLE;
674 value |= (addrext << B43_DMA64_RXADDREXT_SHIFT)
675 & B43_DMA64_RXADDREXT_MASK;
676 b43_dma_write(ring, B43_DMA64_RXCTL, value);
677 b43_dma_write(ring, B43_DMA64_RXRINGLO,
678 (ringbase & 0xFFFFFFFF));
679 b43_dma_write(ring, B43_DMA64_RXRINGHI,
681 ~SSB_DMA_TRANSLATION_MASK)
683 b43_dma_write(ring, B43_DMA64_RXINDEX, ring->nr_slots *
684 sizeof(struct b43_dmadesc64));
686 u32 ringbase = (u32) (ring->dmabase);
688 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
689 >> SSB_DMA_TRANSLATION_SHIFT;
690 value = (ring->frameoffset << B43_DMA32_RXFROFF_SHIFT);
691 value |= B43_DMA32_RXENABLE;
692 value |= (addrext << B43_DMA32_RXADDREXT_SHIFT)
693 & B43_DMA32_RXADDREXT_MASK;
694 b43_dma_write(ring, B43_DMA32_RXCTL, value);
695 b43_dma_write(ring, B43_DMA32_RXRING,
696 (ringbase & ~SSB_DMA_TRANSLATION_MASK)
698 b43_dma_write(ring, B43_DMA32_RXINDEX, ring->nr_slots *
699 sizeof(struct b43_dmadesc32));
707 /* Shutdown the DMA controller. */
708 static void dmacontroller_cleanup(struct b43_dmaring *ring)
711 b43_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
713 if (ring->type == B43_DMA_64BIT) {
714 b43_dma_write(ring, B43_DMA64_TXRINGLO, 0);
715 b43_dma_write(ring, B43_DMA64_TXRINGHI, 0);
717 b43_dma_write(ring, B43_DMA32_TXRING, 0);
719 b43_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
721 if (ring->type == B43_DMA_64BIT) {
722 b43_dma_write(ring, B43_DMA64_RXRINGLO, 0);
723 b43_dma_write(ring, B43_DMA64_RXRINGHI, 0);
725 b43_dma_write(ring, B43_DMA32_RXRING, 0);
729 static void free_all_descbuffers(struct b43_dmaring *ring)
731 struct b43_dmadesc_generic *desc;
732 struct b43_dmadesc_meta *meta;
735 if (!ring->used_slots)
737 for (i = 0; i < ring->nr_slots; i++) {
738 desc = ring->ops->idx2desc(ring, i, &meta);
741 B43_WARN_ON(!ring->tx);
745 unmap_descbuffer(ring, meta->dmaaddr,
748 unmap_descbuffer(ring, meta->dmaaddr,
749 ring->rx_buffersize, 0);
751 free_descriptor_buffer(ring, meta);
755 static u64 supported_dma_mask(struct b43_wldev *dev)
760 tmp = b43_read32(dev, SSB_TMSHIGH);
761 if (tmp & SSB_TMSHIGH_DMA64)
762 return DMA_64BIT_MASK;
763 mmio_base = b43_dmacontroller_base(0, 0);
764 b43_write32(dev, mmio_base + B43_DMA32_TXCTL, B43_DMA32_TXADDREXT_MASK);
765 tmp = b43_read32(dev, mmio_base + B43_DMA32_TXCTL);
766 if (tmp & B43_DMA32_TXADDREXT_MASK)
767 return DMA_32BIT_MASK;
769 return DMA_30BIT_MASK;
772 /* Main initialization function. */
774 struct b43_dmaring *b43_setup_dmaring(struct b43_wldev *dev,
775 int controller_index,
777 enum b43_dmatype type)
779 struct b43_dmaring *ring;
784 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
789 nr_slots = B43_RXRING_SLOTS;
791 nr_slots = B43_TXRING_SLOTS;
793 ring->meta = kcalloc(nr_slots, sizeof(struct b43_dmadesc_meta),
798 ring->txhdr_cache = kcalloc(nr_slots,
801 if (!ring->txhdr_cache)
804 /* test for ability to dma to txhdr_cache */
805 dma_test = dma_map_single(dev->dev->dev,
810 if (b43_dma_mapping_error(ring, dma_test, b43_txhdr_size(dev))) {
812 kfree(ring->txhdr_cache);
813 ring->txhdr_cache = kcalloc(nr_slots,
815 GFP_KERNEL | GFP_DMA);
816 if (!ring->txhdr_cache)
819 dma_test = dma_map_single(dev->dev->dev,
824 if (b43_dma_mapping_error(ring, dma_test,
825 b43_txhdr_size(dev)))
826 goto err_kfree_txhdr_cache;
829 dma_unmap_single(dev->dev->dev,
830 dma_test, b43_txhdr_size(dev),
835 ring->nr_slots = nr_slots;
836 ring->mmio_base = b43_dmacontroller_base(type, controller_index);
837 ring->index = controller_index;
838 if (type == B43_DMA_64BIT)
839 ring->ops = &dma64_ops;
841 ring->ops = &dma32_ops;
844 ring->current_slot = -1;
846 if (ring->index == 0) {
847 ring->rx_buffersize = B43_DMA0_RX_BUFFERSIZE;
848 ring->frameoffset = B43_DMA0_RX_FRAMEOFFSET;
849 } else if (ring->index == 3) {
850 ring->rx_buffersize = B43_DMA3_RX_BUFFERSIZE;
851 ring->frameoffset = B43_DMA3_RX_FRAMEOFFSET;
855 spin_lock_init(&ring->lock);
856 #ifdef CONFIG_B43_DEBUG
857 ring->last_injected_overflow = jiffies;
860 err = alloc_ringmemory(ring);
862 goto err_kfree_txhdr_cache;
863 err = dmacontroller_setup(ring);
865 goto err_free_ringmemory;
871 free_ringmemory(ring);
872 err_kfree_txhdr_cache:
873 kfree(ring->txhdr_cache);
882 #define divide(a, b) ({ \
888 #define modulo(a, b) ({ \
893 /* Main cleanup function. */
894 static void b43_destroy_dmaring(struct b43_dmaring *ring,
895 const char *ringname)
900 #ifdef CONFIG_B43_DEBUG
902 /* Print some statistics. */
903 u64 failed_packets = ring->nr_failed_tx_packets;
904 u64 succeed_packets = ring->nr_succeed_tx_packets;
905 u64 nr_packets = failed_packets + succeed_packets;
906 u64 permille_failed = 0, average_tries = 0;
909 permille_failed = divide(failed_packets * 1000, nr_packets);
911 average_tries = divide(ring->nr_total_packet_tries * 100, nr_packets);
913 b43dbg(ring->dev->wl, "DMA-%u %s: "
914 "Used slots %d/%d, Failed frames %llu/%llu = %llu.%01llu%%, "
915 "Average tries %llu.%02llu\n",
916 (unsigned int)(ring->type), ringname,
917 ring->max_used_slots,
919 (unsigned long long)failed_packets,
920 (unsigned long long)nr_packets,
921 (unsigned long long)divide(permille_failed, 10),
922 (unsigned long long)modulo(permille_failed, 10),
923 (unsigned long long)divide(average_tries, 100),
924 (unsigned long long)modulo(average_tries, 100));
928 /* Device IRQs are disabled prior entering this function,
929 * so no need to take care of concurrency with rx handler stuff.
931 dmacontroller_cleanup(ring);
932 free_all_descbuffers(ring);
933 free_ringmemory(ring);
935 kfree(ring->txhdr_cache);
940 #define destroy_ring(dma, ring) do { \
941 b43_destroy_dmaring((dma)->ring, __stringify(ring)); \
942 (dma)->ring = NULL; \
945 void b43_dma_free(struct b43_wldev *dev)
947 struct b43_dma *dma = &dev->dma;
949 destroy_ring(dma, rx_ring);
950 destroy_ring(dma, tx_ring_AC_BK);
951 destroy_ring(dma, tx_ring_AC_BE);
952 destroy_ring(dma, tx_ring_AC_VI);
953 destroy_ring(dma, tx_ring_AC_VO);
954 destroy_ring(dma, tx_ring_mcast);
957 int b43_dma_init(struct b43_wldev *dev)
959 struct b43_dma *dma = &dev->dma;
962 enum b43_dmatype type;
964 dmamask = supported_dma_mask(dev);
969 type = B43_DMA_30BIT;
972 type = B43_DMA_32BIT;
975 type = B43_DMA_64BIT;
978 err = ssb_dma_set_mask(dev->dev, dmamask);
980 b43err(dev->wl, "The machine/kernel does not support "
981 "the required DMA mask (0x%08X%08X)\n",
982 (unsigned int)((dmamask & 0xFFFFFFFF00000000ULL) >> 32),
983 (unsigned int)(dmamask & 0x00000000FFFFFFFFULL));
988 /* setup TX DMA channels. */
989 dma->tx_ring_AC_BK = b43_setup_dmaring(dev, 0, 1, type);
990 if (!dma->tx_ring_AC_BK)
993 dma->tx_ring_AC_BE = b43_setup_dmaring(dev, 1, 1, type);
994 if (!dma->tx_ring_AC_BE)
997 dma->tx_ring_AC_VI = b43_setup_dmaring(dev, 2, 1, type);
998 if (!dma->tx_ring_AC_VI)
1001 dma->tx_ring_AC_VO = b43_setup_dmaring(dev, 3, 1, type);
1002 if (!dma->tx_ring_AC_VO)
1003 goto err_destroy_vi;
1005 dma->tx_ring_mcast = b43_setup_dmaring(dev, 4, 1, type);
1006 if (!dma->tx_ring_mcast)
1007 goto err_destroy_vo;
1009 /* setup RX DMA channel. */
1010 dma->rx_ring = b43_setup_dmaring(dev, 0, 0, type);
1012 goto err_destroy_mcast;
1014 /* No support for the TX status DMA ring. */
1015 B43_WARN_ON(dev->dev->id.revision < 5);
1017 b43dbg(dev->wl, "%u-bit DMA initialized\n",
1018 (unsigned int)type);
1024 destroy_ring(dma, tx_ring_mcast);
1026 destroy_ring(dma, tx_ring_AC_VO);
1028 destroy_ring(dma, tx_ring_AC_VI);
1030 destroy_ring(dma, tx_ring_AC_BE);
1032 destroy_ring(dma, tx_ring_AC_BK);
1036 /* Generate a cookie for the TX header. */
1037 static u16 generate_cookie(struct b43_dmaring *ring, int slot)
1041 /* Use the upper 4 bits of the cookie as
1042 * DMA controller ID and store the slot number
1043 * in the lower 12 bits.
1044 * Note that the cookie must never be 0, as this
1045 * is a special value used in RX path.
1046 * It can also not be 0xFFFF because that is special
1047 * for multicast frames.
1049 cookie = (((u16)ring->index + 1) << 12);
1050 B43_WARN_ON(slot & ~0x0FFF);
1051 cookie |= (u16)slot;
1056 /* Inspect a cookie and find out to which controller/slot it belongs. */
1058 struct b43_dmaring *parse_cookie(struct b43_wldev *dev, u16 cookie, int *slot)
1060 struct b43_dma *dma = &dev->dma;
1061 struct b43_dmaring *ring = NULL;
1063 switch (cookie & 0xF000) {
1065 ring = dma->tx_ring_AC_BK;
1068 ring = dma->tx_ring_AC_BE;
1071 ring = dma->tx_ring_AC_VI;
1074 ring = dma->tx_ring_AC_VO;
1077 ring = dma->tx_ring_mcast;
1082 *slot = (cookie & 0x0FFF);
1083 B43_WARN_ON(!(ring && *slot >= 0 && *slot < ring->nr_slots));
1088 static int dma_tx_fragment(struct b43_dmaring *ring,
1089 struct sk_buff *skb,
1090 struct ieee80211_tx_control *ctl)
1092 const struct b43_dma_ops *ops = ring->ops;
1094 int slot, old_top_slot, old_used_slots;
1096 struct b43_dmadesc_generic *desc;
1097 struct b43_dmadesc_meta *meta;
1098 struct b43_dmadesc_meta *meta_hdr;
1099 struct sk_buff *bounce_skb;
1101 size_t hdrsize = b43_txhdr_size(ring->dev);
1103 #define SLOTS_PER_PACKET 2
1104 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
1106 old_top_slot = ring->current_slot;
1107 old_used_slots = ring->used_slots;
1109 /* Get a slot for the header. */
1110 slot = request_slot(ring);
1111 desc = ops->idx2desc(ring, slot, &meta_hdr);
1112 memset(meta_hdr, 0, sizeof(*meta_hdr));
1114 header = &(ring->txhdr_cache[slot * hdrsize]);
1115 cookie = generate_cookie(ring, slot);
1116 err = b43_generate_txhdr(ring->dev, header,
1117 skb->data, skb->len, ctl, cookie);
1118 if (unlikely(err)) {
1119 ring->current_slot = old_top_slot;
1120 ring->used_slots = old_used_slots;
1124 meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
1126 if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize)) {
1127 ring->current_slot = old_top_slot;
1128 ring->used_slots = old_used_slots;
1131 ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr,
1134 /* Get a slot for the payload. */
1135 slot = request_slot(ring);
1136 desc = ops->idx2desc(ring, slot, &meta);
1137 memset(meta, 0, sizeof(*meta));
1139 memcpy(&meta->txstat.control, ctl, sizeof(*ctl));
1141 meta->is_last_fragment = 1;
1143 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1144 /* create a bounce buffer in zone_dma on mapping failure. */
1145 if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len)) {
1146 bounce_skb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
1148 ring->current_slot = old_top_slot;
1149 ring->used_slots = old_used_slots;
1154 memcpy(skb_put(bounce_skb, skb->len), skb->data, skb->len);
1155 dev_kfree_skb_any(skb);
1158 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1159 if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len)) {
1160 ring->current_slot = old_top_slot;
1161 ring->used_slots = old_used_slots;
1163 goto out_free_bounce;
1167 ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1);
1169 if (ctl->flags & IEEE80211_TXCTL_SEND_AFTER_DTIM) {
1170 /* Tell the firmware about the cookie of the last
1171 * mcast frame, so it can clear the more-data bit in it. */
1172 b43_shm_write16(ring->dev, B43_SHM_SHARED,
1173 B43_SHM_SH_MCASTCOOKIE, cookie);
1175 /* Now transfer the whole frame. */
1177 ops->poke_tx(ring, next_slot(ring, slot));
1181 dev_kfree_skb_any(skb);
1183 unmap_descbuffer(ring, meta_hdr->dmaaddr,
1188 static inline int should_inject_overflow(struct b43_dmaring *ring)
1190 #ifdef CONFIG_B43_DEBUG
1191 if (unlikely(b43_debug(ring->dev, B43_DBG_DMAOVERFLOW))) {
1192 /* Check if we should inject another ringbuffer overflow
1193 * to test handling of this situation in the stack. */
1194 unsigned long next_overflow;
1196 next_overflow = ring->last_injected_overflow + HZ;
1197 if (time_after(jiffies, next_overflow)) {
1198 ring->last_injected_overflow = jiffies;
1199 b43dbg(ring->dev->wl,
1200 "Injecting TX ring overflow on "
1201 "DMA controller %d\n", ring->index);
1205 #endif /* CONFIG_B43_DEBUG */
1209 /* Static mapping of mac80211's queues (priorities) to b43 DMA rings. */
1210 static struct b43_dmaring * select_ring_by_priority(struct b43_wldev *dev,
1213 struct b43_dmaring *ring;
1215 if (b43_modparam_qos) {
1216 /* 0 = highest priority */
1217 switch (queue_prio) {
1222 ring = dev->dma.tx_ring_AC_VO;
1225 ring = dev->dma.tx_ring_AC_VI;
1228 ring = dev->dma.tx_ring_AC_BE;
1231 ring = dev->dma.tx_ring_AC_BK;
1235 ring = dev->dma.tx_ring_AC_BE;
1240 int b43_dma_tx(struct b43_wldev *dev,
1241 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1243 struct b43_dmaring *ring;
1244 struct ieee80211_hdr *hdr;
1246 unsigned long flags;
1248 if (unlikely(skb->len < 2 + 2 + 6)) {
1249 /* Too short, this can't be a valid frame. */
1253 hdr = (struct ieee80211_hdr *)skb->data;
1254 if (ctl->flags & IEEE80211_TXCTL_SEND_AFTER_DTIM) {
1255 /* The multicast ring will be sent after the DTIM */
1256 ring = dev->dma.tx_ring_mcast;
1257 /* Set the more-data bit. Ucode will clear it on
1258 * the last frame for us. */
1259 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1261 /* Decide by priority where to put this frame. */
1262 ring = select_ring_by_priority(dev, ctl->queue);
1265 spin_lock_irqsave(&ring->lock, flags);
1266 B43_WARN_ON(!ring->tx);
1267 if (unlikely(free_slots(ring) < SLOTS_PER_PACKET)) {
1268 b43warn(dev->wl, "DMA queue overflow\n");
1272 /* Check if the queue was stopped in mac80211,
1273 * but we got called nevertheless.
1274 * That would be a mac80211 bug. */
1275 B43_WARN_ON(ring->stopped);
1277 /* Assign the queue number to the ring (if not already done before)
1278 * so TX status handling can use it. The queue to ring mapping is
1279 * static, so we don't need to store it per frame. */
1280 ring->queue_prio = ctl->queue;
1282 err = dma_tx_fragment(ring, skb, ctl);
1283 if (unlikely(err == -ENOKEY)) {
1284 /* Drop this packet, as we don't have the encryption key
1285 * anymore and must not transmit it unencrypted. */
1286 dev_kfree_skb_any(skb);
1290 if (unlikely(err)) {
1291 b43err(dev->wl, "DMA tx mapping failure\n");
1294 ring->nr_tx_packets++;
1295 if ((free_slots(ring) < SLOTS_PER_PACKET) ||
1296 should_inject_overflow(ring)) {
1297 /* This TX ring is full. */
1298 ieee80211_stop_queue(dev->wl->hw, ctl->queue);
1300 if (b43_debug(dev, B43_DBG_DMAVERBOSE)) {
1301 b43dbg(dev->wl, "Stopped TX ring %d\n", ring->index);
1305 spin_unlock_irqrestore(&ring->lock, flags);
1310 static void b43_fill_txstatus_report(struct b43_dmaring *ring,
1311 struct ieee80211_tx_status *report,
1312 const struct b43_txstatus *status)
1314 bool frame_failed = 0;
1316 if (status->acked) {
1317 /* The frame was ACKed. */
1318 report->flags |= IEEE80211_TX_STATUS_ACK;
1320 /* The frame was not ACKed... */
1321 if (!(report->control.flags & IEEE80211_TXCTL_NO_ACK)) {
1322 /* ...but we expected an ACK. */
1324 report->excessive_retries = 1;
1327 if (status->frame_count == 0) {
1328 /* The frame was not transmitted at all. */
1329 report->retry_count = 0;
1331 report->retry_count = status->frame_count - 1;
1332 #ifdef CONFIG_B43_DEBUG
1334 ring->nr_failed_tx_packets++;
1336 ring->nr_succeed_tx_packets++;
1337 ring->nr_total_packet_tries += status->frame_count;
1342 void b43_dma_handle_txstatus(struct b43_wldev *dev,
1343 const struct b43_txstatus *status)
1345 const struct b43_dma_ops *ops;
1346 struct b43_dmaring *ring;
1347 struct b43_dmadesc_generic *desc;
1348 struct b43_dmadesc_meta *meta;
1351 ring = parse_cookie(dev, status->cookie, &slot);
1352 if (unlikely(!ring))
1354 B43_WARN_ON(!irqs_disabled());
1355 spin_lock(&ring->lock);
1357 B43_WARN_ON(!ring->tx);
1360 B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
1361 desc = ops->idx2desc(ring, slot, &meta);
1364 unmap_descbuffer(ring, meta->dmaaddr, meta->skb->len,
1367 unmap_descbuffer(ring, meta->dmaaddr,
1368 b43_txhdr_size(dev), 1);
1370 if (meta->is_last_fragment) {
1371 B43_WARN_ON(!meta->skb);
1372 /* Call back to inform the ieee80211 subsystem about the
1373 * status of the transmission.
1374 * Some fields of txstat are already filled in dma_tx().
1376 b43_fill_txstatus_report(ring, &(meta->txstat), status);
1377 ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb,
1379 /* skb is freed by ieee80211_tx_status_irqsafe() */
1382 /* No need to call free_descriptor_buffer here, as
1383 * this is only the txhdr, which is not allocated.
1385 B43_WARN_ON(meta->skb);
1388 /* Everything unmapped and free'd. So it's not used anymore. */
1391 if (meta->is_last_fragment)
1393 slot = next_slot(ring, slot);
1395 dev->stats.last_tx = jiffies;
1396 if (ring->stopped) {
1397 B43_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
1398 ieee80211_wake_queue(dev->wl->hw, ring->queue_prio);
1400 if (b43_debug(dev, B43_DBG_DMAVERBOSE)) {
1401 b43dbg(dev->wl, "Woke up TX ring %d\n", ring->index);
1405 spin_unlock(&ring->lock);
1408 void b43_dma_get_tx_stats(struct b43_wldev *dev,
1409 struct ieee80211_tx_queue_stats *stats)
1411 const int nr_queues = dev->wl->hw->queues;
1412 struct b43_dmaring *ring;
1413 struct ieee80211_tx_queue_stats_data *data;
1414 unsigned long flags;
1417 for (i = 0; i < nr_queues; i++) {
1418 data = &(stats->data[i]);
1419 ring = select_ring_by_priority(dev, i);
1421 spin_lock_irqsave(&ring->lock, flags);
1422 data->len = ring->used_slots / SLOTS_PER_PACKET;
1423 data->limit = ring->nr_slots / SLOTS_PER_PACKET;
1424 data->count = ring->nr_tx_packets;
1425 spin_unlock_irqrestore(&ring->lock, flags);
1429 static void dma_rx(struct b43_dmaring *ring, int *slot)
1431 const struct b43_dma_ops *ops = ring->ops;
1432 struct b43_dmadesc_generic *desc;
1433 struct b43_dmadesc_meta *meta;
1434 struct b43_rxhdr_fw4 *rxhdr;
1435 struct sk_buff *skb;
1440 desc = ops->idx2desc(ring, *slot, &meta);
1442 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1445 rxhdr = (struct b43_rxhdr_fw4 *)skb->data;
1446 len = le16_to_cpu(rxhdr->frame_len);
1453 len = le16_to_cpu(rxhdr->frame_len);
1454 } while (len == 0 && i++ < 5);
1455 if (unlikely(len == 0)) {
1456 /* recycle the descriptor buffer. */
1457 sync_descbuffer_for_device(ring, meta->dmaaddr,
1458 ring->rx_buffersize);
1462 if (unlikely(len > ring->rx_buffersize)) {
1463 /* The data did not fit into one descriptor buffer
1464 * and is split over multiple buffers.
1465 * This should never happen, as we try to allocate buffers
1466 * big enough. So simply ignore this packet.
1472 desc = ops->idx2desc(ring, *slot, &meta);
1473 /* recycle the descriptor buffer. */
1474 sync_descbuffer_for_device(ring, meta->dmaaddr,
1475 ring->rx_buffersize);
1476 *slot = next_slot(ring, *slot);
1478 tmp -= ring->rx_buffersize;
1482 b43err(ring->dev->wl, "DMA RX buffer too small "
1483 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1484 len, ring->rx_buffersize, cnt);
1488 dmaaddr = meta->dmaaddr;
1489 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1490 if (unlikely(err)) {
1491 b43dbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer() failed\n");
1492 sync_descbuffer_for_device(ring, dmaaddr, ring->rx_buffersize);
1496 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1497 skb_put(skb, len + ring->frameoffset);
1498 skb_pull(skb, ring->frameoffset);
1500 b43_rx(ring->dev, skb, rxhdr);
1505 void b43_dma_rx(struct b43_dmaring *ring)
1507 const struct b43_dma_ops *ops = ring->ops;
1508 int slot, current_slot;
1511 B43_WARN_ON(ring->tx);
1512 current_slot = ops->get_current_rxslot(ring);
1513 B43_WARN_ON(!(current_slot >= 0 && current_slot < ring->nr_slots));
1515 slot = ring->current_slot;
1516 for (; slot != current_slot; slot = next_slot(ring, slot)) {
1517 dma_rx(ring, &slot);
1518 update_max_used_slots(ring, ++used_slots);
1520 ops->set_current_rxslot(ring, slot);
1521 ring->current_slot = slot;
1524 static void b43_dma_tx_suspend_ring(struct b43_dmaring *ring)
1526 unsigned long flags;
1528 spin_lock_irqsave(&ring->lock, flags);
1529 B43_WARN_ON(!ring->tx);
1530 ring->ops->tx_suspend(ring);
1531 spin_unlock_irqrestore(&ring->lock, flags);
1534 static void b43_dma_tx_resume_ring(struct b43_dmaring *ring)
1536 unsigned long flags;
1538 spin_lock_irqsave(&ring->lock, flags);
1539 B43_WARN_ON(!ring->tx);
1540 ring->ops->tx_resume(ring);
1541 spin_unlock_irqrestore(&ring->lock, flags);
1544 void b43_dma_tx_suspend(struct b43_wldev *dev)
1546 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
1547 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BK);
1548 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BE);
1549 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VI);
1550 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VO);
1551 b43_dma_tx_suspend_ring(dev->dma.tx_ring_mcast);
1554 void b43_dma_tx_resume(struct b43_wldev *dev)
1556 b43_dma_tx_resume_ring(dev->dma.tx_ring_mcast);
1557 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VO);
1558 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VI);
1559 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BE);
1560 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BK);
1561 b43_power_saving_ctl_bits(dev, 0);