3 Broadcom B43legacy 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.
30 #include "b43legacy.h"
36 #include <linux/dma-mapping.h>
37 #include <linux/pci.h>
38 #include <linux/delay.h>
39 #include <linux/skbuff.h>
44 struct b43legacy_dmadesc_generic *op32_idx2desc(
45 struct b43legacy_dmaring *ring,
47 struct b43legacy_dmadesc_meta **meta)
49 struct b43legacy_dmadesc32 *desc;
51 *meta = &(ring->meta[slot]);
52 desc = ring->descbase;
55 return (struct b43legacy_dmadesc_generic *)desc;
58 static void op32_fill_descriptor(struct b43legacy_dmaring *ring,
59 struct b43legacy_dmadesc_generic *desc,
60 dma_addr_t dmaaddr, u16 bufsize,
61 int start, int end, int irq)
63 struct b43legacy_dmadesc32 *descbase = ring->descbase;
69 slot = (int)(&(desc->dma32) - descbase);
70 B43legacy_WARN_ON(!(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 & B43legacy_DMA32_DCTL_BYTECNT;
78 if (slot == ring->nr_slots - 1)
79 ctl |= B43legacy_DMA32_DCTL_DTABLEEND;
81 ctl |= B43legacy_DMA32_DCTL_FRAMESTART;
83 ctl |= B43legacy_DMA32_DCTL_FRAMEEND;
85 ctl |= B43legacy_DMA32_DCTL_IRQ;
86 ctl |= (addrext << B43legacy_DMA32_DCTL_ADDREXT_SHIFT)
87 & B43legacy_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 b43legacy_dmaring *ring, int slot)
95 b43legacy_dma_write(ring, B43legacy_DMA32_TXINDEX,
96 (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
99 static void op32_tx_suspend(struct b43legacy_dmaring *ring)
101 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
102 b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
103 | B43legacy_DMA32_TXSUSPEND);
106 static void op32_tx_resume(struct b43legacy_dmaring *ring)
108 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
109 b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
110 & ~B43legacy_DMA32_TXSUSPEND);
113 static int op32_get_current_rxslot(struct b43legacy_dmaring *ring)
117 val = b43legacy_dma_read(ring, B43legacy_DMA32_RXSTATUS);
118 val &= B43legacy_DMA32_RXDPTR;
120 return (val / sizeof(struct b43legacy_dmadesc32));
123 static void op32_set_current_rxslot(struct b43legacy_dmaring *ring,
126 b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX,
127 (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
130 static const struct b43legacy_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 b43legacy_dmadesc_generic *op64_idx2desc(
143 struct b43legacy_dmaring *ring,
145 struct b43legacy_dmadesc_meta
148 struct b43legacy_dmadesc64 *desc;
150 *meta = &(ring->meta[slot]);
151 desc = ring->descbase;
152 desc = &(desc[slot]);
154 return (struct b43legacy_dmadesc_generic *)desc;
157 static void op64_fill_descriptor(struct b43legacy_dmaring *ring,
158 struct b43legacy_dmadesc_generic *desc,
159 dma_addr_t dmaaddr, u16 bufsize,
160 int start, int end, int irq)
162 struct b43legacy_dmadesc64 *descbase = ring->descbase;
170 slot = (int)(&(desc->dma64) - descbase);
171 B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
173 addrlo = (u32)(dmaaddr & 0xFFFFFFFF);
174 addrhi = (((u64)dmaaddr >> 32) & ~SSB_DMA_TRANSLATION_MASK);
175 addrext = (((u64)dmaaddr >> 32) & SSB_DMA_TRANSLATION_MASK)
176 >> SSB_DMA_TRANSLATION_SHIFT;
177 addrhi |= ssb_dma_translation(ring->dev->dev);
178 if (slot == ring->nr_slots - 1)
179 ctl0 |= B43legacy_DMA64_DCTL0_DTABLEEND;
181 ctl0 |= B43legacy_DMA64_DCTL0_FRAMESTART;
183 ctl0 |= B43legacy_DMA64_DCTL0_FRAMEEND;
185 ctl0 |= B43legacy_DMA64_DCTL0_IRQ;
186 ctl1 |= (bufsize - ring->frameoffset)
187 & B43legacy_DMA64_DCTL1_BYTECNT;
188 ctl1 |= (addrext << B43legacy_DMA64_DCTL1_ADDREXT_SHIFT)
189 & B43legacy_DMA64_DCTL1_ADDREXT_MASK;
191 desc->dma64.control0 = cpu_to_le32(ctl0);
192 desc->dma64.control1 = cpu_to_le32(ctl1);
193 desc->dma64.address_low = cpu_to_le32(addrlo);
194 desc->dma64.address_high = cpu_to_le32(addrhi);
197 static void op64_poke_tx(struct b43legacy_dmaring *ring, int slot)
199 b43legacy_dma_write(ring, B43legacy_DMA64_TXINDEX,
200 (u32)(slot * sizeof(struct b43legacy_dmadesc64)));
203 static void op64_tx_suspend(struct b43legacy_dmaring *ring)
205 b43legacy_dma_write(ring, B43legacy_DMA64_TXCTL,
206 b43legacy_dma_read(ring, B43legacy_DMA64_TXCTL)
207 | B43legacy_DMA64_TXSUSPEND);
210 static void op64_tx_resume(struct b43legacy_dmaring *ring)
212 b43legacy_dma_write(ring, B43legacy_DMA64_TXCTL,
213 b43legacy_dma_read(ring, B43legacy_DMA64_TXCTL)
214 & ~B43legacy_DMA64_TXSUSPEND);
217 static int op64_get_current_rxslot(struct b43legacy_dmaring *ring)
221 val = b43legacy_dma_read(ring, B43legacy_DMA64_RXSTATUS);
222 val &= B43legacy_DMA64_RXSTATDPTR;
224 return (val / sizeof(struct b43legacy_dmadesc64));
227 static void op64_set_current_rxslot(struct b43legacy_dmaring *ring,
230 b43legacy_dma_write(ring, B43legacy_DMA64_RXINDEX,
231 (u32)(slot * sizeof(struct b43legacy_dmadesc64)));
234 static const struct b43legacy_dma_ops dma64_ops = {
235 .idx2desc = op64_idx2desc,
236 .fill_descriptor = op64_fill_descriptor,
237 .poke_tx = op64_poke_tx,
238 .tx_suspend = op64_tx_suspend,
239 .tx_resume = op64_tx_resume,
240 .get_current_rxslot = op64_get_current_rxslot,
241 .set_current_rxslot = op64_set_current_rxslot,
245 static inline int free_slots(struct b43legacy_dmaring *ring)
247 return (ring->nr_slots - ring->used_slots);
250 static inline int next_slot(struct b43legacy_dmaring *ring, int slot)
252 B43legacy_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1));
253 if (slot == ring->nr_slots - 1)
258 static inline int prev_slot(struct b43legacy_dmaring *ring, int slot)
260 B43legacy_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1));
262 return ring->nr_slots - 1;
266 #ifdef CONFIG_B43LEGACY_DEBUG
267 static void update_max_used_slots(struct b43legacy_dmaring *ring,
268 int current_used_slots)
270 if (current_used_slots <= ring->max_used_slots)
272 ring->max_used_slots = current_used_slots;
273 if (b43legacy_debug(ring->dev, B43legacy_DBG_DMAVERBOSE))
274 b43legacydbg(ring->dev->wl,
275 "max_used_slots increased to %d on %s ring %d\n",
276 ring->max_used_slots,
277 ring->tx ? "TX" : "RX",
282 void update_max_used_slots(struct b43legacy_dmaring *ring,
283 int current_used_slots)
287 /* Request a slot for usage. */
289 int request_slot(struct b43legacy_dmaring *ring)
293 B43legacy_WARN_ON(!ring->tx);
294 B43legacy_WARN_ON(ring->stopped);
295 B43legacy_WARN_ON(free_slots(ring) == 0);
297 slot = next_slot(ring, ring->current_slot);
298 ring->current_slot = slot;
301 update_max_used_slots(ring, ring->used_slots);
306 /* Mac80211-queue to b43legacy-ring mapping */
307 static struct b43legacy_dmaring *priority_to_txring(
308 struct b43legacy_wldev *dev,
311 struct b43legacy_dmaring *ring;
313 /*FIXME: For now we always run on TX-ring-1 */
314 return dev->dma.tx_ring1;
316 /* 0 = highest priority */
317 switch (queue_priority) {
319 B43legacy_WARN_ON(1);
322 ring = dev->dma.tx_ring3;
325 ring = dev->dma.tx_ring2;
328 ring = dev->dma.tx_ring1;
331 ring = dev->dma.tx_ring0;
334 ring = dev->dma.tx_ring4;
337 ring = dev->dma.tx_ring5;
344 /* Bcm4301-ring to mac80211-queue mapping */
345 static inline int txring_to_priority(struct b43legacy_dmaring *ring)
347 static const u8 idx_to_prio[] =
348 { 3, 2, 1, 0, 4, 5, };
350 /*FIXME: have only one queue, for now */
353 return idx_to_prio[ring->index];
357 u16 b43legacy_dmacontroller_base(int dma64bit, int controller_idx)
359 static const u16 map64[] = {
360 B43legacy_MMIO_DMA64_BASE0,
361 B43legacy_MMIO_DMA64_BASE1,
362 B43legacy_MMIO_DMA64_BASE2,
363 B43legacy_MMIO_DMA64_BASE3,
364 B43legacy_MMIO_DMA64_BASE4,
365 B43legacy_MMIO_DMA64_BASE5,
367 static const u16 map32[] = {
368 B43legacy_MMIO_DMA32_BASE0,
369 B43legacy_MMIO_DMA32_BASE1,
370 B43legacy_MMIO_DMA32_BASE2,
371 B43legacy_MMIO_DMA32_BASE3,
372 B43legacy_MMIO_DMA32_BASE4,
373 B43legacy_MMIO_DMA32_BASE5,
377 B43legacy_WARN_ON(!(controller_idx >= 0 &&
378 controller_idx < ARRAY_SIZE(map64)));
379 return map64[controller_idx];
381 B43legacy_WARN_ON(!(controller_idx >= 0 &&
382 controller_idx < ARRAY_SIZE(map32)));
383 return map32[controller_idx];
387 dma_addr_t map_descbuffer(struct b43legacy_dmaring *ring,
395 dmaaddr = dma_map_single(ring->dev->dev->dev,
399 dmaaddr = dma_map_single(ring->dev->dev->dev,
407 void unmap_descbuffer(struct b43legacy_dmaring *ring,
413 dma_unmap_single(ring->dev->dev->dev,
417 dma_unmap_single(ring->dev->dev->dev,
423 void sync_descbuffer_for_cpu(struct b43legacy_dmaring *ring,
427 B43legacy_WARN_ON(ring->tx);
429 dma_sync_single_for_cpu(ring->dev->dev->dev,
430 addr, len, DMA_FROM_DEVICE);
434 void sync_descbuffer_for_device(struct b43legacy_dmaring *ring,
438 B43legacy_WARN_ON(ring->tx);
440 dma_sync_single_for_device(ring->dev->dev->dev,
441 addr, len, DMA_FROM_DEVICE);
445 void free_descriptor_buffer(struct b43legacy_dmaring *ring,
446 struct b43legacy_dmadesc_meta *meta,
451 dev_kfree_skb_irq(meta->skb);
453 dev_kfree_skb(meta->skb);
458 static int alloc_ringmemory(struct b43legacy_dmaring *ring)
460 struct device *dev = ring->dev->dev->dev;
462 ring->descbase = dma_alloc_coherent(dev, B43legacy_DMA_RINGMEMSIZE,
463 &(ring->dmabase), GFP_KERNEL);
464 if (!ring->descbase) {
465 b43legacyerr(ring->dev->wl, "DMA ringmemory allocation"
469 memset(ring->descbase, 0, B43legacy_DMA_RINGMEMSIZE);
474 static void free_ringmemory(struct b43legacy_dmaring *ring)
476 struct device *dev = ring->dev->dev->dev;
478 dma_free_coherent(dev, B43legacy_DMA_RINGMEMSIZE,
479 ring->descbase, ring->dmabase);
482 /* Reset the RX DMA channel */
483 int b43legacy_dmacontroller_rx_reset(struct b43legacy_wldev *dev,
484 u16 mmio_base, int dma64)
492 offset = dma64 ? B43legacy_DMA64_RXCTL : B43legacy_DMA32_RXCTL;
493 b43legacy_write32(dev, mmio_base + offset, 0);
494 for (i = 0; i < 10; i++) {
495 offset = dma64 ? B43legacy_DMA64_RXSTATUS :
496 B43legacy_DMA32_RXSTATUS;
497 value = b43legacy_read32(dev, mmio_base + offset);
499 value &= B43legacy_DMA64_RXSTAT;
500 if (value == B43legacy_DMA64_RXSTAT_DISABLED) {
505 value &= B43legacy_DMA32_RXSTATE;
506 if (value == B43legacy_DMA32_RXSTAT_DISABLED) {
514 b43legacyerr(dev->wl, "DMA RX reset timed out\n");
521 /* Reset the RX DMA channel */
522 int b43legacy_dmacontroller_tx_reset(struct b43legacy_wldev *dev,
523 u16 mmio_base, int dma64)
531 for (i = 0; i < 10; i++) {
532 offset = dma64 ? B43legacy_DMA64_TXSTATUS :
533 B43legacy_DMA32_TXSTATUS;
534 value = b43legacy_read32(dev, mmio_base + offset);
536 value &= B43legacy_DMA64_TXSTAT;
537 if (value == B43legacy_DMA64_TXSTAT_DISABLED ||
538 value == B43legacy_DMA64_TXSTAT_IDLEWAIT ||
539 value == B43legacy_DMA64_TXSTAT_STOPPED)
542 value &= B43legacy_DMA32_TXSTATE;
543 if (value == B43legacy_DMA32_TXSTAT_DISABLED ||
544 value == B43legacy_DMA32_TXSTAT_IDLEWAIT ||
545 value == B43legacy_DMA32_TXSTAT_STOPPED)
550 offset = dma64 ? B43legacy_DMA64_TXCTL : B43legacy_DMA32_TXCTL;
551 b43legacy_write32(dev, mmio_base + offset, 0);
552 for (i = 0; i < 10; i++) {
553 offset = dma64 ? B43legacy_DMA64_TXSTATUS :
554 B43legacy_DMA32_TXSTATUS;
555 value = b43legacy_read32(dev, mmio_base + offset);
557 value &= B43legacy_DMA64_TXSTAT;
558 if (value == B43legacy_DMA64_TXSTAT_DISABLED) {
563 value &= B43legacy_DMA32_TXSTATE;
564 if (value == B43legacy_DMA32_TXSTAT_DISABLED) {
572 b43legacyerr(dev->wl, "DMA TX reset timed out\n");
575 /* ensure the reset is completed. */
581 static int setup_rx_descbuffer(struct b43legacy_dmaring *ring,
582 struct b43legacy_dmadesc_generic *desc,
583 struct b43legacy_dmadesc_meta *meta,
586 struct b43legacy_rxhdr_fw3 *rxhdr;
587 struct b43legacy_hwtxstatus *txstat;
591 B43legacy_WARN_ON(ring->tx);
593 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
596 dmaaddr = map_descbuffer(ring, skb->data,
597 ring->rx_buffersize, 0);
598 if (dma_mapping_error(dmaaddr)) {
599 /* ugh. try to realloc in zone_dma */
600 gfp_flags |= GFP_DMA;
602 dev_kfree_skb_any(skb);
604 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
607 dmaaddr = map_descbuffer(ring, skb->data,
608 ring->rx_buffersize, 0);
611 if (dma_mapping_error(dmaaddr)) {
612 dev_kfree_skb_any(skb);
617 meta->dmaaddr = dmaaddr;
618 ring->ops->fill_descriptor(ring, desc, dmaaddr,
619 ring->rx_buffersize, 0, 0, 0);
621 rxhdr = (struct b43legacy_rxhdr_fw3 *)(skb->data);
622 rxhdr->frame_len = 0;
623 txstat = (struct b43legacy_hwtxstatus *)(skb->data);
629 /* Allocate the initial descbuffers.
630 * This is used for an RX ring only.
632 static int alloc_initial_descbuffers(struct b43legacy_dmaring *ring)
636 struct b43legacy_dmadesc_generic *desc;
637 struct b43legacy_dmadesc_meta *meta;
639 for (i = 0; i < ring->nr_slots; i++) {
640 desc = ring->ops->idx2desc(ring, i, &meta);
642 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
644 b43legacyerr(ring->dev->wl,
645 "Failed to allocate initial descbuffers\n");
649 mb(); /* all descbuffer setup before next line */
650 ring->used_slots = ring->nr_slots;
656 for (i--; i >= 0; i--) {
657 desc = ring->ops->idx2desc(ring, i, &meta);
659 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
660 dev_kfree_skb(meta->skb);
665 /* Do initial setup of the DMA controller.
666 * Reset the controller, write the ring busaddress
667 * and switch the "enable" bit on.
669 static int dmacontroller_setup(struct b43legacy_dmaring *ring)
674 u32 trans = ssb_dma_translation(ring->dev->dev);
678 u64 ringbase = (u64)(ring->dmabase);
680 addrext = ((ringbase >> 32) & SSB_DMA_TRANSLATION_MASK)
681 >> SSB_DMA_TRANSLATION_SHIFT;
682 value = B43legacy_DMA64_TXENABLE;
683 value |= (addrext << B43legacy_DMA64_TXADDREXT_SHIFT)
684 & B43legacy_DMA64_TXADDREXT_MASK;
685 b43legacy_dma_write(ring, B43legacy_DMA64_TXCTL,
687 b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGLO,
688 (ringbase & 0xFFFFFFFF));
689 b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGHI,
691 & ~SSB_DMA_TRANSLATION_MASK)
694 u32 ringbase = (u32)(ring->dmabase);
696 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
697 >> SSB_DMA_TRANSLATION_SHIFT;
698 value = B43legacy_DMA32_TXENABLE;
699 value |= (addrext << B43legacy_DMA32_TXADDREXT_SHIFT)
700 & B43legacy_DMA32_TXADDREXT_MASK;
701 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
703 b43legacy_dma_write(ring, B43legacy_DMA32_TXRING,
705 ~SSB_DMA_TRANSLATION_MASK)
709 err = alloc_initial_descbuffers(ring);
713 u64 ringbase = (u64)(ring->dmabase);
715 addrext = ((ringbase >> 32) & SSB_DMA_TRANSLATION_MASK)
716 >> SSB_DMA_TRANSLATION_SHIFT;
717 value = (ring->frameoffset <<
718 B43legacy_DMA64_RXFROFF_SHIFT);
719 value |= B43legacy_DMA64_RXENABLE;
720 value |= (addrext << B43legacy_DMA64_RXADDREXT_SHIFT)
721 & B43legacy_DMA64_RXADDREXT_MASK;
722 b43legacy_dma_write(ring, B43legacy_DMA64_RXCTL,
724 b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGLO,
725 (ringbase & 0xFFFFFFFF));
726 b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGHI,
728 ~SSB_DMA_TRANSLATION_MASK) |
730 b43legacy_dma_write(ring, B43legacy_DMA64_RXINDEX,
733 u32 ringbase = (u32)(ring->dmabase);
735 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
736 >> SSB_DMA_TRANSLATION_SHIFT;
737 value = (ring->frameoffset <<
738 B43legacy_DMA32_RXFROFF_SHIFT);
739 value |= B43legacy_DMA32_RXENABLE;
741 B43legacy_DMA32_RXADDREXT_SHIFT)
742 & B43legacy_DMA32_RXADDREXT_MASK;
743 b43legacy_dma_write(ring, B43legacy_DMA32_RXCTL,
745 b43legacy_dma_write(ring, B43legacy_DMA32_RXRING,
747 ~SSB_DMA_TRANSLATION_MASK)
749 b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX,
758 /* Shutdown the DMA controller. */
759 static void dmacontroller_cleanup(struct b43legacy_dmaring *ring)
762 b43legacy_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
765 b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGLO, 0);
766 b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGHI, 0);
768 b43legacy_dma_write(ring, B43legacy_DMA32_TXRING, 0);
770 b43legacy_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
773 b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGLO, 0);
774 b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGHI, 0);
776 b43legacy_dma_write(ring, B43legacy_DMA32_RXRING, 0);
780 static void free_all_descbuffers(struct b43legacy_dmaring *ring)
782 struct b43legacy_dmadesc_generic *desc;
783 struct b43legacy_dmadesc_meta *meta;
786 if (!ring->used_slots)
788 for (i = 0; i < ring->nr_slots; i++) {
789 desc = ring->ops->idx2desc(ring, i, &meta);
792 B43legacy_WARN_ON(!ring->tx);
796 unmap_descbuffer(ring, meta->dmaaddr,
799 unmap_descbuffer(ring, meta->dmaaddr,
800 ring->rx_buffersize, 0);
801 free_descriptor_buffer(ring, meta, 0);
805 static u64 supported_dma_mask(struct b43legacy_wldev *dev)
810 tmp = b43legacy_read32(dev, SSB_TMSHIGH);
811 if (tmp & SSB_TMSHIGH_DMA64)
812 return DMA_64BIT_MASK;
813 mmio_base = b43legacy_dmacontroller_base(0, 0);
814 b43legacy_write32(dev,
815 mmio_base + B43legacy_DMA32_TXCTL,
816 B43legacy_DMA32_TXADDREXT_MASK);
817 tmp = b43legacy_read32(dev, mmio_base +
818 B43legacy_DMA32_TXCTL);
819 if (tmp & B43legacy_DMA32_TXADDREXT_MASK)
820 return DMA_32BIT_MASK;
822 return DMA_30BIT_MASK;
825 /* Main initialization function. */
827 struct b43legacy_dmaring *b43legacy_setup_dmaring(
828 struct b43legacy_wldev *dev,
829 int controller_index,
833 struct b43legacy_dmaring *ring;
838 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
842 nr_slots = B43legacy_RXRING_SLOTS;
844 nr_slots = B43legacy_TXRING_SLOTS;
846 ring->meta = kcalloc(nr_slots, sizeof(struct b43legacy_dmadesc_meta),
851 ring->txhdr_cache = kcalloc(nr_slots,
852 sizeof(struct b43legacy_txhdr_fw3),
854 if (!ring->txhdr_cache)
857 /* test for ability to dma to txhdr_cache */
858 dma_test = dma_map_single(dev->dev->dev,
860 sizeof(struct b43legacy_txhdr_fw3),
863 if (dma_mapping_error(dma_test)) {
865 kfree(ring->txhdr_cache);
866 ring->txhdr_cache = kcalloc(nr_slots,
867 sizeof(struct b43legacy_txhdr_fw3),
868 GFP_KERNEL | GFP_DMA);
869 if (!ring->txhdr_cache)
872 dma_test = dma_map_single(dev->dev->dev,
874 sizeof(struct b43legacy_txhdr_fw3),
877 if (dma_mapping_error(dma_test))
878 goto err_kfree_txhdr_cache;
881 dma_unmap_single(dev->dev->dev,
882 dma_test, sizeof(struct b43legacy_txhdr_fw3),
887 ring->nr_slots = nr_slots;
888 ring->mmio_base = b43legacy_dmacontroller_base(dma64,
890 ring->index = controller_index;
891 ring->dma64 = !!dma64;
893 ring->ops = &dma64_ops;
895 ring->ops = &dma32_ops;
898 ring->current_slot = -1;
900 if (ring->index == 0) {
901 ring->rx_buffersize = B43legacy_DMA0_RX_BUFFERSIZE;
902 ring->frameoffset = B43legacy_DMA0_RX_FRAMEOFFSET;
903 } else if (ring->index == 3) {
904 ring->rx_buffersize = B43legacy_DMA3_RX_BUFFERSIZE;
905 ring->frameoffset = B43legacy_DMA3_RX_FRAMEOFFSET;
907 B43legacy_WARN_ON(1);
909 spin_lock_init(&ring->lock);
910 #ifdef CONFIG_B43LEGACY_DEBUG
911 ring->last_injected_overflow = jiffies;
914 err = alloc_ringmemory(ring);
916 goto err_kfree_txhdr_cache;
917 err = dmacontroller_setup(ring);
919 goto err_free_ringmemory;
925 free_ringmemory(ring);
926 err_kfree_txhdr_cache:
927 kfree(ring->txhdr_cache);
936 /* Main cleanup function. */
937 static void b43legacy_destroy_dmaring(struct b43legacy_dmaring *ring)
942 b43legacydbg(ring->dev->wl, "DMA-%s 0x%04X (%s) max used slots:"
943 " %d/%d\n", (ring->dma64) ? "64" : "32", ring->mmio_base,
944 (ring->tx) ? "TX" : "RX",
945 ring->max_used_slots, ring->nr_slots);
946 /* Device IRQs are disabled prior entering this function,
947 * so no need to take care of concurrency with rx handler stuff.
949 dmacontroller_cleanup(ring);
950 free_all_descbuffers(ring);
951 free_ringmemory(ring);
953 kfree(ring->txhdr_cache);
958 void b43legacy_dma_free(struct b43legacy_wldev *dev)
960 struct b43legacy_dma *dma;
962 if (b43legacy_using_pio(dev))
966 b43legacy_destroy_dmaring(dma->rx_ring3);
967 dma->rx_ring3 = NULL;
968 b43legacy_destroy_dmaring(dma->rx_ring0);
969 dma->rx_ring0 = NULL;
971 b43legacy_destroy_dmaring(dma->tx_ring5);
972 dma->tx_ring5 = NULL;
973 b43legacy_destroy_dmaring(dma->tx_ring4);
974 dma->tx_ring4 = NULL;
975 b43legacy_destroy_dmaring(dma->tx_ring3);
976 dma->tx_ring3 = NULL;
977 b43legacy_destroy_dmaring(dma->tx_ring2);
978 dma->tx_ring2 = NULL;
979 b43legacy_destroy_dmaring(dma->tx_ring1);
980 dma->tx_ring1 = NULL;
981 b43legacy_destroy_dmaring(dma->tx_ring0);
982 dma->tx_ring0 = NULL;
985 int b43legacy_dma_init(struct b43legacy_wldev *dev)
987 struct b43legacy_dma *dma = &dev->dma;
988 struct b43legacy_dmaring *ring;
993 dmamask = supported_dma_mask(dev);
994 if (dmamask == DMA_64BIT_MASK)
997 err = ssb_dma_set_mask(dev->dev, dmamask);
999 #ifdef CONFIG_B43LEGACY_PIO
1000 b43legacywarn(dev->wl, "DMA for this device not supported. "
1001 "Falling back to PIO\n");
1002 dev->__using_pio = 1;
1005 b43legacyerr(dev->wl, "DMA for this device not supported and "
1006 "no PIO support compiled in\n");
1012 /* setup TX DMA channels. */
1013 ring = b43legacy_setup_dmaring(dev, 0, 1, dma64);
1016 dma->tx_ring0 = ring;
1018 ring = b43legacy_setup_dmaring(dev, 1, 1, dma64);
1020 goto err_destroy_tx0;
1021 dma->tx_ring1 = ring;
1023 ring = b43legacy_setup_dmaring(dev, 2, 1, dma64);
1025 goto err_destroy_tx1;
1026 dma->tx_ring2 = ring;
1028 ring = b43legacy_setup_dmaring(dev, 3, 1, dma64);
1030 goto err_destroy_tx2;
1031 dma->tx_ring3 = ring;
1033 ring = b43legacy_setup_dmaring(dev, 4, 1, dma64);
1035 goto err_destroy_tx3;
1036 dma->tx_ring4 = ring;
1038 ring = b43legacy_setup_dmaring(dev, 5, 1, dma64);
1040 goto err_destroy_tx4;
1041 dma->tx_ring5 = ring;
1043 /* setup RX DMA channels. */
1044 ring = b43legacy_setup_dmaring(dev, 0, 0, dma64);
1046 goto err_destroy_tx5;
1047 dma->rx_ring0 = ring;
1049 if (dev->dev->id.revision < 5) {
1050 ring = b43legacy_setup_dmaring(dev, 3, 0, dma64);
1052 goto err_destroy_rx0;
1053 dma->rx_ring3 = ring;
1056 b43legacydbg(dev->wl, "%d-bit DMA initialized\n",
1057 (dmamask == DMA_64BIT_MASK) ? 64 :
1058 (dmamask == DMA_32BIT_MASK) ? 32 : 30);
1064 b43legacy_destroy_dmaring(dma->rx_ring0);
1065 dma->rx_ring0 = NULL;
1067 b43legacy_destroy_dmaring(dma->tx_ring5);
1068 dma->tx_ring5 = NULL;
1070 b43legacy_destroy_dmaring(dma->tx_ring4);
1071 dma->tx_ring4 = NULL;
1073 b43legacy_destroy_dmaring(dma->tx_ring3);
1074 dma->tx_ring3 = NULL;
1076 b43legacy_destroy_dmaring(dma->tx_ring2);
1077 dma->tx_ring2 = NULL;
1079 b43legacy_destroy_dmaring(dma->tx_ring1);
1080 dma->tx_ring1 = NULL;
1082 b43legacy_destroy_dmaring(dma->tx_ring0);
1083 dma->tx_ring0 = NULL;
1087 /* Generate a cookie for the TX header. */
1088 static u16 generate_cookie(struct b43legacy_dmaring *ring,
1091 u16 cookie = 0x1000;
1093 /* Use the upper 4 bits of the cookie as
1094 * DMA controller ID and store the slot number
1095 * in the lower 12 bits.
1096 * Note that the cookie must never be 0, as this
1097 * is a special value used in RX path.
1099 switch (ring->index) {
1119 B43legacy_WARN_ON(!(((u16)slot & 0xF000) == 0x0000));
1120 cookie |= (u16)slot;
1125 /* Inspect a cookie and find out to which controller/slot it belongs. */
1127 struct b43legacy_dmaring *parse_cookie(struct b43legacy_wldev *dev,
1128 u16 cookie, int *slot)
1130 struct b43legacy_dma *dma = &dev->dma;
1131 struct b43legacy_dmaring *ring = NULL;
1133 switch (cookie & 0xF000) {
1135 ring = dma->tx_ring0;
1138 ring = dma->tx_ring1;
1141 ring = dma->tx_ring2;
1144 ring = dma->tx_ring3;
1147 ring = dma->tx_ring4;
1150 ring = dma->tx_ring5;
1153 B43legacy_WARN_ON(1);
1155 *slot = (cookie & 0x0FFF);
1156 B43legacy_WARN_ON(!(ring && *slot >= 0 && *slot < ring->nr_slots));
1161 static int dma_tx_fragment(struct b43legacy_dmaring *ring,
1162 struct sk_buff *skb,
1163 struct ieee80211_tx_control *ctl)
1165 const struct b43legacy_dma_ops *ops = ring->ops;
1167 int slot, old_top_slot, old_used_slots;
1169 struct b43legacy_dmadesc_generic *desc;
1170 struct b43legacy_dmadesc_meta *meta;
1171 struct b43legacy_dmadesc_meta *meta_hdr;
1172 struct sk_buff *bounce_skb;
1174 #define SLOTS_PER_PACKET 2
1175 B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags != 0);
1177 old_top_slot = ring->current_slot;
1178 old_used_slots = ring->used_slots;
1180 /* Get a slot for the header. */
1181 slot = request_slot(ring);
1182 desc = ops->idx2desc(ring, slot, &meta_hdr);
1183 memset(meta_hdr, 0, sizeof(*meta_hdr));
1185 header = &(ring->txhdr_cache[slot * sizeof(
1186 struct b43legacy_txhdr_fw3)]);
1187 err = b43legacy_generate_txhdr(ring->dev, header,
1188 skb->data, skb->len, ctl,
1189 generate_cookie(ring, slot));
1190 if (unlikely(err)) {
1191 ring->current_slot = old_top_slot;
1192 ring->used_slots = old_used_slots;
1196 meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
1197 sizeof(struct b43legacy_txhdr_fw3), 1);
1198 if (dma_mapping_error(meta_hdr->dmaaddr))
1200 ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr,
1201 sizeof(struct b43legacy_txhdr_fw3), 1, 0, 0);
1203 /* Get a slot for the payload. */
1204 slot = request_slot(ring);
1205 desc = ops->idx2desc(ring, slot, &meta);
1206 memset(meta, 0, sizeof(*meta));
1208 memcpy(&meta->txstat.control, ctl, sizeof(*ctl));
1210 meta->is_last_fragment = 1;
1212 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1213 /* create a bounce buffer in zone_dma on mapping failure. */
1214 if (dma_mapping_error(meta->dmaaddr)) {
1215 bounce_skb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
1217 ring->current_slot = old_top_slot;
1218 ring->used_slots = old_used_slots;
1223 memcpy(skb_put(bounce_skb, skb->len), skb->data, skb->len);
1224 dev_kfree_skb_any(skb);
1227 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1228 if (dma_mapping_error(meta->dmaaddr)) {
1229 ring->current_slot = old_top_slot;
1230 ring->used_slots = old_used_slots;
1232 goto out_free_bounce;
1236 ops->fill_descriptor(ring, desc, meta->dmaaddr,
1239 wmb(); /* previous stuff MUST be done */
1240 /* Now transfer the whole frame. */
1241 ops->poke_tx(ring, next_slot(ring, slot));
1245 dev_kfree_skb_any(skb);
1247 unmap_descbuffer(ring, meta_hdr->dmaaddr,
1248 sizeof(struct b43legacy_txhdr_fw3), 1);
1253 int should_inject_overflow(struct b43legacy_dmaring *ring)
1255 #ifdef CONFIG_B43LEGACY_DEBUG
1256 if (unlikely(b43legacy_debug(ring->dev,
1257 B43legacy_DBG_DMAOVERFLOW))) {
1258 /* Check if we should inject another ringbuffer overflow
1259 * to test handling of this situation in the stack. */
1260 unsigned long next_overflow;
1262 next_overflow = ring->last_injected_overflow + HZ;
1263 if (time_after(jiffies, next_overflow)) {
1264 ring->last_injected_overflow = jiffies;
1265 b43legacydbg(ring->dev->wl,
1266 "Injecting TX ring overflow on "
1267 "DMA controller %d\n", ring->index);
1271 #endif /* CONFIG_B43LEGACY_DEBUG */
1275 int b43legacy_dma_tx(struct b43legacy_wldev *dev,
1276 struct sk_buff *skb,
1277 struct ieee80211_tx_control *ctl)
1279 struct b43legacy_dmaring *ring;
1281 unsigned long flags;
1283 ring = priority_to_txring(dev, ctl->queue);
1284 spin_lock_irqsave(&ring->lock, flags);
1285 B43legacy_WARN_ON(!ring->tx);
1286 if (unlikely(free_slots(ring) < SLOTS_PER_PACKET)) {
1287 b43legacywarn(dev->wl, "DMA queue overflow\n");
1291 /* Check if the queue was stopped in mac80211,
1292 * but we got called nevertheless.
1293 * That would be a mac80211 bug. */
1294 B43legacy_BUG_ON(ring->stopped);
1296 err = dma_tx_fragment(ring, skb, ctl);
1297 if (unlikely(err == -ENOKEY)) {
1298 /* Drop this packet, as we don't have the encryption key
1299 * anymore and must not transmit it unencrypted. */
1300 dev_kfree_skb_any(skb);
1304 if (unlikely(err)) {
1305 b43legacyerr(dev->wl, "DMA tx mapping failure\n");
1308 ring->nr_tx_packets++;
1309 if ((free_slots(ring) < SLOTS_PER_PACKET) ||
1310 should_inject_overflow(ring)) {
1311 /* This TX ring is full. */
1312 ieee80211_stop_queue(dev->wl->hw, txring_to_priority(ring));
1314 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1315 b43legacydbg(dev->wl, "Stopped TX ring %d\n",
1319 spin_unlock_irqrestore(&ring->lock, flags);
1324 void b43legacy_dma_handle_txstatus(struct b43legacy_wldev *dev,
1325 const struct b43legacy_txstatus *status)
1327 const struct b43legacy_dma_ops *ops;
1328 struct b43legacy_dmaring *ring;
1329 struct b43legacy_dmadesc_generic *desc;
1330 struct b43legacy_dmadesc_meta *meta;
1333 ring = parse_cookie(dev, status->cookie, &slot);
1334 if (unlikely(!ring))
1336 B43legacy_WARN_ON(!irqs_disabled());
1337 spin_lock(&ring->lock);
1339 B43legacy_WARN_ON(!ring->tx);
1342 B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
1343 desc = ops->idx2desc(ring, slot, &meta);
1346 unmap_descbuffer(ring, meta->dmaaddr,
1349 unmap_descbuffer(ring, meta->dmaaddr,
1350 sizeof(struct b43legacy_txhdr_fw3),
1353 if (meta->is_last_fragment) {
1354 B43legacy_WARN_ON(!meta->skb);
1355 /* Call back to inform the ieee80211 subsystem about the
1356 * status of the transmission.
1357 * Some fields of txstat are already filled in dma_tx().
1359 if (status->acked) {
1360 meta->txstat.flags |= IEEE80211_TX_STATUS_ACK;
1362 if (!(meta->txstat.control.flags
1363 & IEEE80211_TXCTL_NO_ACK))
1364 meta->txstat.excessive_retries = 1;
1366 if (status->frame_count == 0) {
1367 /* The frame was not transmitted at all. */
1368 meta->txstat.retry_count = 0;
1370 meta->txstat.retry_count = status->frame_count
1372 ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb,
1374 /* skb is freed by ieee80211_tx_status_irqsafe() */
1377 /* No need to call free_descriptor_buffer here, as
1378 * this is only the txhdr, which is not allocated.
1380 B43legacy_WARN_ON(meta->skb != NULL);
1383 /* Everything unmapped and free'd. So it's not used anymore. */
1386 if (meta->is_last_fragment)
1388 slot = next_slot(ring, slot);
1390 dev->stats.last_tx = jiffies;
1391 if (ring->stopped) {
1392 B43legacy_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
1393 ieee80211_wake_queue(dev->wl->hw, txring_to_priority(ring));
1395 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1396 b43legacydbg(dev->wl, "Woke up TX ring %d\n",
1400 spin_unlock(&ring->lock);
1403 void b43legacy_dma_get_tx_stats(struct b43legacy_wldev *dev,
1404 struct ieee80211_tx_queue_stats *stats)
1406 const int nr_queues = dev->wl->hw->queues;
1407 struct b43legacy_dmaring *ring;
1408 struct ieee80211_tx_queue_stats_data *data;
1409 unsigned long flags;
1412 for (i = 0; i < nr_queues; i++) {
1413 data = &(stats->data[i]);
1414 ring = priority_to_txring(dev, i);
1416 spin_lock_irqsave(&ring->lock, flags);
1417 data->len = ring->used_slots / SLOTS_PER_PACKET;
1418 data->limit = ring->nr_slots / SLOTS_PER_PACKET;
1419 data->count = ring->nr_tx_packets;
1420 spin_unlock_irqrestore(&ring->lock, flags);
1424 static void dma_rx(struct b43legacy_dmaring *ring,
1427 const struct b43legacy_dma_ops *ops = ring->ops;
1428 struct b43legacy_dmadesc_generic *desc;
1429 struct b43legacy_dmadesc_meta *meta;
1430 struct b43legacy_rxhdr_fw3 *rxhdr;
1431 struct sk_buff *skb;
1436 desc = ops->idx2desc(ring, *slot, &meta);
1438 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1441 if (ring->index == 3) {
1442 /* We received an xmit status. */
1443 struct b43legacy_hwtxstatus *hw =
1444 (struct b43legacy_hwtxstatus *)skb->data;
1447 while (hw->cookie == 0) {
1454 b43legacy_handle_hwtxstatus(ring->dev, hw);
1455 /* recycle the descriptor buffer. */
1456 sync_descbuffer_for_device(ring, meta->dmaaddr,
1457 ring->rx_buffersize);
1461 rxhdr = (struct b43legacy_rxhdr_fw3 *)skb->data;
1462 len = le16_to_cpu(rxhdr->frame_len);
1469 len = le16_to_cpu(rxhdr->frame_len);
1470 } while (len == 0 && i++ < 5);
1471 if (unlikely(len == 0)) {
1472 /* recycle the descriptor buffer. */
1473 sync_descbuffer_for_device(ring, meta->dmaaddr,
1474 ring->rx_buffersize);
1478 if (unlikely(len > ring->rx_buffersize)) {
1479 /* The data did not fit into one descriptor buffer
1480 * and is split over multiple buffers.
1481 * This should never happen, as we try to allocate buffers
1482 * big enough. So simply ignore this packet.
1488 desc = ops->idx2desc(ring, *slot, &meta);
1489 /* recycle the descriptor buffer. */
1490 sync_descbuffer_for_device(ring, meta->dmaaddr,
1491 ring->rx_buffersize);
1492 *slot = next_slot(ring, *slot);
1494 tmp -= ring->rx_buffersize;
1498 b43legacyerr(ring->dev->wl, "DMA RX buffer too small "
1499 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1500 len, ring->rx_buffersize, cnt);
1504 dmaaddr = meta->dmaaddr;
1505 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1506 if (unlikely(err)) {
1507 b43legacydbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer()"
1509 sync_descbuffer_for_device(ring, dmaaddr,
1510 ring->rx_buffersize);
1514 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1515 skb_put(skb, len + ring->frameoffset);
1516 skb_pull(skb, ring->frameoffset);
1518 b43legacy_rx(ring->dev, skb, rxhdr);
1523 void b43legacy_dma_rx(struct b43legacy_dmaring *ring)
1525 const struct b43legacy_dma_ops *ops = ring->ops;
1530 B43legacy_WARN_ON(ring->tx);
1531 current_slot = ops->get_current_rxslot(ring);
1532 B43legacy_WARN_ON(!(current_slot >= 0 && current_slot <
1535 slot = ring->current_slot;
1536 for (; slot != current_slot; slot = next_slot(ring, slot)) {
1537 dma_rx(ring, &slot);
1538 update_max_used_slots(ring, ++used_slots);
1540 ops->set_current_rxslot(ring, slot);
1541 ring->current_slot = slot;
1544 static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring *ring)
1546 unsigned long flags;
1548 spin_lock_irqsave(&ring->lock, flags);
1549 B43legacy_WARN_ON(!ring->tx);
1550 ring->ops->tx_suspend(ring);
1551 spin_unlock_irqrestore(&ring->lock, flags);
1554 static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring *ring)
1556 unsigned long flags;
1558 spin_lock_irqsave(&ring->lock, flags);
1559 B43legacy_WARN_ON(!ring->tx);
1560 ring->ops->tx_resume(ring);
1561 spin_unlock_irqrestore(&ring->lock, flags);
1564 void b43legacy_dma_tx_suspend(struct b43legacy_wldev *dev)
1566 b43legacy_power_saving_ctl_bits(dev, -1, 1);
1567 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring0);
1568 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring1);
1569 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring2);
1570 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring3);
1571 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring4);
1572 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring5);
1575 void b43legacy_dma_tx_resume(struct b43legacy_wldev *dev)
1577 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring5);
1578 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring4);
1579 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring3);
1580 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring2);
1581 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring1);
1582 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring0);
1583 b43legacy_power_saving_ctl_bits(dev, -1, -1);