3 Broadcom BCM43xx wireless driver
5 DMA ringbuffer and descriptor allocation/management
7 Copyright (c) 2005, 2006 Michael Buesch <mbuesch@freenet.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>
43 static inline int free_slots(struct bcm43xx_dmaring *ring)
45 return (ring->nr_slots - ring->used_slots);
48 static inline int next_slot(struct bcm43xx_dmaring *ring, int slot)
50 assert(slot >= -1 && slot <= ring->nr_slots - 1);
51 if (slot == ring->nr_slots - 1)
56 static inline int prev_slot(struct bcm43xx_dmaring *ring, int slot)
58 assert(slot >= 0 && slot <= ring->nr_slots - 1);
60 return ring->nr_slots - 1;
64 /* Request a slot for usage. */
66 int request_slot(struct bcm43xx_dmaring *ring)
71 assert(!ring->suspended);
72 assert(free_slots(ring) != 0);
74 slot = next_slot(ring, ring->current_slot);
75 ring->current_slot = slot;
78 /* Check the number of available slots and suspend TX,
79 * if we are running low on free slots.
81 if (unlikely(free_slots(ring) < ring->suspend_mark)) {
82 netif_stop_queue(ring->bcm->net_dev);
85 #ifdef CONFIG_BCM43XX_DEBUG
86 if (ring->used_slots > ring->max_used_slots)
87 ring->max_used_slots = ring->used_slots;
88 #endif /* CONFIG_BCM43XX_DEBUG*/
93 /* Return a slot to the free slots. */
95 void return_slot(struct bcm43xx_dmaring *ring, int slot)
101 /* Check if TX is suspended and check if we have
102 * enough free slots to resume it again.
104 if (unlikely(ring->suspended)) {
105 if (free_slots(ring) >= ring->resume_mark) {
107 netif_wake_queue(ring->bcm->net_dev);
112 u16 bcm43xx_dmacontroller_base(int dma64bit, int controller_idx)
114 static const u16 map64[] = {
115 BCM43xx_MMIO_DMA64_BASE0,
116 BCM43xx_MMIO_DMA64_BASE1,
117 BCM43xx_MMIO_DMA64_BASE2,
118 BCM43xx_MMIO_DMA64_BASE3,
119 BCM43xx_MMIO_DMA64_BASE4,
120 BCM43xx_MMIO_DMA64_BASE5,
122 static const u16 map32[] = {
123 BCM43xx_MMIO_DMA32_BASE0,
124 BCM43xx_MMIO_DMA32_BASE1,
125 BCM43xx_MMIO_DMA32_BASE2,
126 BCM43xx_MMIO_DMA32_BASE3,
127 BCM43xx_MMIO_DMA32_BASE4,
128 BCM43xx_MMIO_DMA32_BASE5,
132 assert(controller_idx >= 0 &&
133 controller_idx < ARRAY_SIZE(map64));
134 return map64[controller_idx];
136 assert(controller_idx >= 0 &&
137 controller_idx < ARRAY_SIZE(map32));
138 return map32[controller_idx];
142 dma_addr_t map_descbuffer(struct bcm43xx_dmaring *ring,
150 dmaaddr = dma_map_single(&ring->bcm->pci_dev->dev,
154 dmaaddr = dma_map_single(&ring->bcm->pci_dev->dev,
163 void unmap_descbuffer(struct bcm43xx_dmaring *ring,
169 dma_unmap_single(&ring->bcm->pci_dev->dev,
173 dma_unmap_single(&ring->bcm->pci_dev->dev,
180 void sync_descbuffer_for_cpu(struct bcm43xx_dmaring *ring,
186 dma_sync_single_for_cpu(&ring->bcm->pci_dev->dev,
187 addr, len, DMA_FROM_DEVICE);
191 void sync_descbuffer_for_device(struct bcm43xx_dmaring *ring,
197 dma_sync_single_for_device(&ring->bcm->pci_dev->dev,
198 addr, len, DMA_FROM_DEVICE);
201 /* Unmap and free a descriptor buffer. */
203 void free_descriptor_buffer(struct bcm43xx_dmaring *ring,
204 struct bcm43xx_dmadesc_meta *meta,
209 dev_kfree_skb_irq(meta->skb);
211 dev_kfree_skb(meta->skb);
215 static int alloc_ringmemory(struct bcm43xx_dmaring *ring)
217 struct device *dev = &(ring->bcm->pci_dev->dev);
219 ring->descbase = dma_alloc_coherent(dev, BCM43xx_DMA_RINGMEMSIZE,
220 &(ring->dmabase), GFP_KERNEL);
221 if (!ring->descbase) {
222 printk(KERN_ERR PFX "DMA ringmemory allocation failed\n");
225 memset(ring->descbase, 0, BCM43xx_DMA_RINGMEMSIZE);
230 static void free_ringmemory(struct bcm43xx_dmaring *ring)
232 struct device *dev = &(ring->bcm->pci_dev->dev);
234 dma_free_coherent(dev, BCM43xx_DMA_RINGMEMSIZE,
235 ring->descbase, ring->dmabase);
238 /* Reset the RX DMA channel */
239 int bcm43xx_dmacontroller_rx_reset(struct bcm43xx_private *bcm,
240 u16 mmio_base, int dma64)
246 offset = dma64 ? BCM43xx_DMA64_RXCTL : BCM43xx_DMA32_RXCTL;
247 bcm43xx_write32(bcm, mmio_base + offset, 0);
248 for (i = 0; i < 1000; i++) {
249 offset = dma64 ? BCM43xx_DMA64_RXSTATUS : BCM43xx_DMA32_RXSTATUS;
250 value = bcm43xx_read32(bcm, mmio_base + offset);
252 value &= BCM43xx_DMA64_RXSTAT;
253 if (value == BCM43xx_DMA64_RXSTAT_DISABLED) {
258 value &= BCM43xx_DMA32_RXSTATE;
259 if (value == BCM43xx_DMA32_RXSTAT_DISABLED) {
267 printk(KERN_ERR PFX "Error: Wait on DMA RX status timed out.\n");
274 /* Reset the RX DMA channel */
275 int bcm43xx_dmacontroller_tx_reset(struct bcm43xx_private *bcm,
276 u16 mmio_base, int dma64)
282 for (i = 0; i < 1000; i++) {
283 offset = dma64 ? BCM43xx_DMA64_TXSTATUS : BCM43xx_DMA32_TXSTATUS;
284 value = bcm43xx_read32(bcm, mmio_base + offset);
286 value &= BCM43xx_DMA64_TXSTAT;
287 if (value == BCM43xx_DMA64_TXSTAT_DISABLED ||
288 value == BCM43xx_DMA64_TXSTAT_IDLEWAIT ||
289 value == BCM43xx_DMA64_TXSTAT_STOPPED)
292 value &= BCM43xx_DMA32_TXSTATE;
293 if (value == BCM43xx_DMA32_TXSTAT_DISABLED ||
294 value == BCM43xx_DMA32_TXSTAT_IDLEWAIT ||
295 value == BCM43xx_DMA32_TXSTAT_STOPPED)
300 offset = dma64 ? BCM43xx_DMA64_TXCTL : BCM43xx_DMA32_TXCTL;
301 bcm43xx_write32(bcm, mmio_base + offset, 0);
302 for (i = 0; i < 1000; i++) {
303 offset = dma64 ? BCM43xx_DMA64_TXSTATUS : BCM43xx_DMA32_TXSTATUS;
304 value = bcm43xx_read32(bcm, mmio_base + offset);
306 value &= BCM43xx_DMA64_TXSTAT;
307 if (value == BCM43xx_DMA64_TXSTAT_DISABLED) {
312 value &= BCM43xx_DMA32_TXSTATE;
313 if (value == BCM43xx_DMA32_TXSTAT_DISABLED) {
321 printk(KERN_ERR PFX "Error: Wait on DMA TX status timed out.\n");
324 /* ensure the reset is completed. */
330 static void fill_descriptor(struct bcm43xx_dmaring *ring,
331 struct bcm43xx_dmadesc_generic *desc,
334 int start, int end, int irq)
338 slot = bcm43xx_dma_desc2idx(ring, desc);
339 assert(slot >= 0 && slot < ring->nr_slots);
342 u32 ctl0 = 0, ctl1 = 0;
346 addrlo = (u32)(dmaaddr & 0xFFFFFFFF);
347 addrhi = (((u64)dmaaddr >> 32) & ~BCM43xx_DMA64_ROUTING);
348 addrext = (((u64)dmaaddr >> 32) >> BCM43xx_DMA64_ROUTING_SHIFT);
349 addrhi |= ring->routing;
350 if (slot == ring->nr_slots - 1)
351 ctl0 |= BCM43xx_DMA64_DCTL0_DTABLEEND;
353 ctl0 |= BCM43xx_DMA64_DCTL0_FRAMESTART;
355 ctl0 |= BCM43xx_DMA64_DCTL0_FRAMEEND;
357 ctl0 |= BCM43xx_DMA64_DCTL0_IRQ;
358 ctl1 |= (bufsize - ring->frameoffset)
359 & BCM43xx_DMA64_DCTL1_BYTECNT;
360 ctl1 |= (addrext << BCM43xx_DMA64_DCTL1_ADDREXT_SHIFT)
361 & BCM43xx_DMA64_DCTL1_ADDREXT_MASK;
363 desc->dma64.control0 = cpu_to_le32(ctl0);
364 desc->dma64.control1 = cpu_to_le32(ctl1);
365 desc->dma64.address_low = cpu_to_le32(addrlo);
366 desc->dma64.address_high = cpu_to_le32(addrhi);
372 addr = (u32)(dmaaddr & ~BCM43xx_DMA32_ROUTING);
373 addrext = (u32)(dmaaddr & BCM43xx_DMA32_ROUTING)
374 >> BCM43xx_DMA32_ROUTING_SHIFT;
375 addr |= ring->routing;
376 ctl = (bufsize - ring->frameoffset)
377 & BCM43xx_DMA32_DCTL_BYTECNT;
378 if (slot == ring->nr_slots - 1)
379 ctl |= BCM43xx_DMA32_DCTL_DTABLEEND;
381 ctl |= BCM43xx_DMA32_DCTL_FRAMESTART;
383 ctl |= BCM43xx_DMA32_DCTL_FRAMEEND;
385 ctl |= BCM43xx_DMA32_DCTL_IRQ;
386 ctl |= (addrext << BCM43xx_DMA32_DCTL_ADDREXT_SHIFT)
387 & BCM43xx_DMA32_DCTL_ADDREXT_MASK;
389 desc->dma32.control = cpu_to_le32(ctl);
390 desc->dma32.address = cpu_to_le32(addr);
394 static int setup_rx_descbuffer(struct bcm43xx_dmaring *ring,
395 struct bcm43xx_dmadesc_generic *desc,
396 struct bcm43xx_dmadesc_meta *meta,
399 struct bcm43xx_rxhdr *rxhdr;
400 struct bcm43xx_hwxmitstatus *xmitstat;
406 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
409 dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0);
411 meta->dmaaddr = dmaaddr;
412 skb->dev = ring->bcm->net_dev;
414 fill_descriptor(ring, desc, dmaaddr,
415 ring->rx_buffersize, 0, 0, 0);
417 rxhdr = (struct bcm43xx_rxhdr *)(skb->data);
418 rxhdr->frame_length = 0;
420 xmitstat = (struct bcm43xx_hwxmitstatus *)(skb->data);
421 xmitstat->cookie = 0;
426 /* Allocate the initial descbuffers.
427 * This is used for an RX ring only.
429 static int alloc_initial_descbuffers(struct bcm43xx_dmaring *ring)
431 int i, err = -ENOMEM;
432 struct bcm43xx_dmadesc_generic *desc;
433 struct bcm43xx_dmadesc_meta *meta;
435 for (i = 0; i < ring->nr_slots; i++) {
436 desc = bcm43xx_dma_idx2desc(ring, i, &meta);
438 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
443 ring->used_slots = ring->nr_slots;
449 for (i--; i >= 0; i--) {
450 desc = bcm43xx_dma_idx2desc(ring, i, &meta);
452 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
453 dev_kfree_skb(meta->skb);
458 /* Do initial setup of the DMA controller.
459 * Reset the controller, write the ring busaddress
460 * and switch the "enable" bit on.
462 static int dmacontroller_setup(struct bcm43xx_dmaring *ring)
470 u64 ringbase = (u64)(ring->dmabase);
472 addrext = ((ringbase >> 32) >> BCM43xx_DMA64_ROUTING_SHIFT);
473 value = BCM43xx_DMA64_TXENABLE;
474 value |= (addrext << BCM43xx_DMA64_TXADDREXT_SHIFT)
475 & BCM43xx_DMA64_TXADDREXT_MASK;
476 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXCTL, value);
477 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGLO,
478 (ringbase & 0xFFFFFFFF));
479 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGHI,
480 ((ringbase >> 32) & ~BCM43xx_DMA64_ROUTING)
483 u32 ringbase = (u32)(ring->dmabase);
485 addrext = (ringbase >> BCM43xx_DMA32_ROUTING_SHIFT);
486 value = BCM43xx_DMA32_TXENABLE;
487 value |= (addrext << BCM43xx_DMA32_TXADDREXT_SHIFT)
488 & BCM43xx_DMA32_TXADDREXT_MASK;
489 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXCTL, value);
490 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXRING,
491 (ringbase & ~BCM43xx_DMA32_ROUTING)
495 err = alloc_initial_descbuffers(ring);
499 u64 ringbase = (u64)(ring->dmabase);
501 addrext = ((ringbase >> 32) >> BCM43xx_DMA64_ROUTING_SHIFT);
502 value = (ring->frameoffset << BCM43xx_DMA64_RXFROFF_SHIFT);
503 value |= BCM43xx_DMA64_RXENABLE;
504 value |= (addrext << BCM43xx_DMA64_RXADDREXT_SHIFT)
505 & BCM43xx_DMA64_RXADDREXT_MASK;
506 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXCTL, value);
507 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGLO,
508 (ringbase & 0xFFFFFFFF));
509 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGHI,
510 ((ringbase >> 32) & ~BCM43xx_DMA64_ROUTING)
512 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXINDEX, 200);
514 u32 ringbase = (u32)(ring->dmabase);
516 addrext = (ringbase >> BCM43xx_DMA32_ROUTING_SHIFT);
517 value = (ring->frameoffset << BCM43xx_DMA32_RXFROFF_SHIFT);
518 value |= BCM43xx_DMA32_RXENABLE;
519 value |= (addrext << BCM43xx_DMA32_RXADDREXT_SHIFT)
520 & BCM43xx_DMA32_RXADDREXT_MASK;
521 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXCTL, value);
522 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXRING,
523 (ringbase & ~BCM43xx_DMA32_ROUTING)
525 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXINDEX, 200);
533 /* Shutdown the DMA controller. */
534 static void dmacontroller_cleanup(struct bcm43xx_dmaring *ring)
537 bcm43xx_dmacontroller_tx_reset(ring->bcm, ring->mmio_base, ring->dma64);
539 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGLO, 0);
540 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGHI, 0);
542 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXRING, 0);
544 bcm43xx_dmacontroller_rx_reset(ring->bcm, ring->mmio_base, ring->dma64);
546 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGLO, 0);
547 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGHI, 0);
549 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXRING, 0);
553 static void free_all_descbuffers(struct bcm43xx_dmaring *ring)
555 struct bcm43xx_dmadesc_generic *desc;
556 struct bcm43xx_dmadesc_meta *meta;
559 if (!ring->used_slots)
561 for (i = 0; i < ring->nr_slots; i++) {
562 desc = bcm43xx_dma_idx2desc(ring, i, &meta);
569 unmap_descbuffer(ring, meta->dmaaddr,
572 unmap_descbuffer(ring, meta->dmaaddr,
573 ring->rx_buffersize, 0);
575 free_descriptor_buffer(ring, meta, 0);
579 /* Main initialization function. */
581 struct bcm43xx_dmaring * bcm43xx_setup_dmaring(struct bcm43xx_private *bcm,
582 int controller_index,
586 struct bcm43xx_dmaring *ring;
590 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
594 nr_slots = BCM43xx_RXRING_SLOTS;
596 nr_slots = BCM43xx_TXRING_SLOTS;
598 ring->meta = kcalloc(nr_slots, sizeof(struct bcm43xx_dmadesc_meta),
603 ring->routing = BCM43xx_DMA32_CLIENTTRANS;
605 ring->routing = BCM43xx_DMA64_CLIENTTRANS;
606 #ifdef CONFIG_BCM947XX
607 if (bcm->pci_dev->bus->number == 0)
608 ring->routing = dma64 ? BCM43xx_DMA64_NOTRANS : BCM43xx_DMA32_NOTRANS;
612 ring->nr_slots = nr_slots;
613 ring->suspend_mark = ring->nr_slots * BCM43xx_TXSUSPEND_PERCENT / 100;
614 ring->resume_mark = ring->nr_slots * BCM43xx_TXRESUME_PERCENT / 100;
615 assert(ring->suspend_mark < ring->resume_mark);
616 ring->mmio_base = bcm43xx_dmacontroller_base(dma64, controller_index);
617 ring->index = controller_index;
618 ring->dma64 = !!dma64;
621 ring->current_slot = -1;
623 if (ring->index == 0) {
624 ring->rx_buffersize = BCM43xx_DMA0_RX_BUFFERSIZE;
625 ring->frameoffset = BCM43xx_DMA0_RX_FRAMEOFFSET;
626 } else if (ring->index == 3) {
627 ring->rx_buffersize = BCM43xx_DMA3_RX_BUFFERSIZE;
628 ring->frameoffset = BCM43xx_DMA3_RX_FRAMEOFFSET;
633 err = alloc_ringmemory(ring);
636 err = dmacontroller_setup(ring);
638 goto err_free_ringmemory;
644 free_ringmemory(ring);
653 /* Main cleanup function. */
654 static void bcm43xx_destroy_dmaring(struct bcm43xx_dmaring *ring)
659 dprintk(KERN_INFO PFX "DMA-%s 0x%04X (%s) max used slots: %d/%d\n",
660 (ring->dma64) ? "64" : "32",
662 (ring->tx) ? "TX" : "RX",
663 ring->max_used_slots, ring->nr_slots);
664 /* Device IRQs are disabled prior entering this function,
665 * so no need to take care of concurrency with rx handler stuff.
667 dmacontroller_cleanup(ring);
668 free_all_descbuffers(ring);
669 free_ringmemory(ring);
675 void bcm43xx_dma_free(struct bcm43xx_private *bcm)
677 struct bcm43xx_dma *dma;
679 if (bcm43xx_using_pio(bcm))
681 dma = bcm43xx_current_dma(bcm);
683 bcm43xx_destroy_dmaring(dma->rx_ring3);
684 dma->rx_ring3 = NULL;
685 bcm43xx_destroy_dmaring(dma->rx_ring0);
686 dma->rx_ring0 = NULL;
688 bcm43xx_destroy_dmaring(dma->tx_ring5);
689 dma->tx_ring5 = NULL;
690 bcm43xx_destroy_dmaring(dma->tx_ring4);
691 dma->tx_ring4 = NULL;
692 bcm43xx_destroy_dmaring(dma->tx_ring3);
693 dma->tx_ring3 = NULL;
694 bcm43xx_destroy_dmaring(dma->tx_ring2);
695 dma->tx_ring2 = NULL;
696 bcm43xx_destroy_dmaring(dma->tx_ring1);
697 dma->tx_ring1 = NULL;
698 bcm43xx_destroy_dmaring(dma->tx_ring0);
699 dma->tx_ring0 = NULL;
702 int bcm43xx_dma_init(struct bcm43xx_private *bcm)
704 struct bcm43xx_dma *dma = bcm43xx_current_dma(bcm);
705 struct bcm43xx_dmaring *ring;
710 sbtmstatehi = bcm43xx_read32(bcm, BCM43xx_CIR_SBTMSTATEHIGH);
711 if (sbtmstatehi & BCM43xx_SBTMSTATEHIGH_DMA64BIT)
714 /* setup TX DMA channels. */
715 ring = bcm43xx_setup_dmaring(bcm, 0, 1, dma64);
718 dma->tx_ring0 = ring;
720 ring = bcm43xx_setup_dmaring(bcm, 1, 1, dma64);
722 goto err_destroy_tx0;
723 dma->tx_ring1 = ring;
725 ring = bcm43xx_setup_dmaring(bcm, 2, 1, dma64);
727 goto err_destroy_tx1;
728 dma->tx_ring2 = ring;
730 ring = bcm43xx_setup_dmaring(bcm, 3, 1, dma64);
732 goto err_destroy_tx2;
733 dma->tx_ring3 = ring;
735 ring = bcm43xx_setup_dmaring(bcm, 4, 1, dma64);
737 goto err_destroy_tx3;
738 dma->tx_ring4 = ring;
740 ring = bcm43xx_setup_dmaring(bcm, 5, 1, dma64);
742 goto err_destroy_tx4;
743 dma->tx_ring5 = ring;
745 /* setup RX DMA channels. */
746 ring = bcm43xx_setup_dmaring(bcm, 0, 0, dma64);
748 goto err_destroy_tx5;
749 dma->rx_ring0 = ring;
751 if (bcm->current_core->rev < 5) {
752 ring = bcm43xx_setup_dmaring(bcm, 3, 0, dma64);
754 goto err_destroy_rx0;
755 dma->rx_ring3 = ring;
758 dprintk(KERN_INFO PFX "%s DMA initialized\n",
759 dma64 ? "64-bit" : "32-bit");
765 bcm43xx_destroy_dmaring(dma->rx_ring0);
766 dma->rx_ring0 = NULL;
768 bcm43xx_destroy_dmaring(dma->tx_ring5);
769 dma->tx_ring5 = NULL;
771 bcm43xx_destroy_dmaring(dma->tx_ring4);
772 dma->tx_ring4 = NULL;
774 bcm43xx_destroy_dmaring(dma->tx_ring3);
775 dma->tx_ring3 = NULL;
777 bcm43xx_destroy_dmaring(dma->tx_ring2);
778 dma->tx_ring2 = NULL;
780 bcm43xx_destroy_dmaring(dma->tx_ring1);
781 dma->tx_ring1 = NULL;
783 bcm43xx_destroy_dmaring(dma->tx_ring0);
784 dma->tx_ring0 = NULL;
788 /* Generate a cookie for the TX header. */
789 static u16 generate_cookie(struct bcm43xx_dmaring *ring,
794 /* Use the upper 4 bits of the cookie as
795 * DMA controller ID and store the slot number
796 * in the lower 12 bits.
797 * Note that the cookie must never be 0, as this
798 * is a special value used in RX path.
800 switch (ring->index) {
820 assert(((u16)slot & 0xF000) == 0x0000);
826 /* Inspect a cookie and find out to which controller/slot it belongs. */
828 struct bcm43xx_dmaring * parse_cookie(struct bcm43xx_private *bcm,
829 u16 cookie, int *slot)
831 struct bcm43xx_dma *dma = bcm43xx_current_dma(bcm);
832 struct bcm43xx_dmaring *ring = NULL;
834 switch (cookie & 0xF000) {
836 ring = dma->tx_ring0;
839 ring = dma->tx_ring1;
842 ring = dma->tx_ring2;
845 ring = dma->tx_ring3;
848 ring = dma->tx_ring4;
851 ring = dma->tx_ring5;
856 *slot = (cookie & 0x0FFF);
857 assert(*slot >= 0 && *slot < ring->nr_slots);
862 static void dmacontroller_poke_tx(struct bcm43xx_dmaring *ring,
868 /* Everything is ready to start. Buffers are DMA mapped and
869 * associated with slots.
870 * "slot" is the last slot of the new frame we want to transmit.
871 * Close your seat belts now, please.
874 slot = next_slot(ring, slot);
875 offset = (ring->dma64) ? BCM43xx_DMA64_TXINDEX : BCM43xx_DMA32_TXINDEX;
876 descsize = (ring->dma64) ? sizeof(struct bcm43xx_dmadesc64)
877 : sizeof(struct bcm43xx_dmadesc32);
878 bcm43xx_dma_write(ring, offset,
879 (u32)(slot * descsize));
882 static void dma_tx_fragment(struct bcm43xx_dmaring *ring,
887 struct bcm43xx_dmadesc_generic *desc;
888 struct bcm43xx_dmadesc_meta *meta;
891 assert(skb_shinfo(skb)->nr_frags == 0);
893 slot = request_slot(ring);
894 desc = bcm43xx_dma_idx2desc(ring, slot, &meta);
896 /* Add a device specific TX header. */
897 assert(skb_headroom(skb) >= sizeof(struct bcm43xx_txhdr));
898 /* Reserve enough headroom for the device tx header. */
899 __skb_push(skb, sizeof(struct bcm43xx_txhdr));
900 /* Now calculate and add the tx header.
901 * The tx header includes the PLCP header.
903 bcm43xx_generate_txhdr(ring->bcm,
904 (struct bcm43xx_txhdr *)skb->data,
905 skb->data + sizeof(struct bcm43xx_txhdr),
906 skb->len - sizeof(struct bcm43xx_txhdr),
908 generate_cookie(ring, slot));
911 dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
912 meta->dmaaddr = dmaaddr;
914 fill_descriptor(ring, desc, dmaaddr,
917 /* Now transfer the whole frame. */
918 dmacontroller_poke_tx(ring, slot);
921 int bcm43xx_dma_tx(struct bcm43xx_private *bcm,
922 struct ieee80211_txb *txb)
924 /* We just received a packet from the kernel network subsystem.
925 * Add headers and DMA map the memory. Poke
926 * the device to send the stuff.
927 * Note that this is called from atomic context.
929 struct bcm43xx_dmaring *ring = bcm43xx_current_dma(bcm)->tx_ring1;
934 if (unlikely(free_slots(ring) < txb->nr_frags)) {
935 /* The queue should be stopped,
936 * if we are low on free slots.
937 * If this ever triggers, we have to lower the suspend_mark.
939 dprintkl(KERN_ERR PFX "Out of DMA descriptor slots!\n");
943 for (i = 0; i < txb->nr_frags; i++) {
944 skb = txb->fragments[i];
945 /* Take skb from ieee80211_txb_free */
946 txb->fragments[i] = NULL;
947 dma_tx_fragment(ring, skb, i);
949 ieee80211_txb_free(txb);
954 void bcm43xx_dma_handle_xmitstatus(struct bcm43xx_private *bcm,
955 struct bcm43xx_xmitstatus *status)
957 struct bcm43xx_dmaring *ring;
958 struct bcm43xx_dmadesc_generic *desc;
959 struct bcm43xx_dmadesc_meta *meta;
960 int is_last_fragment;
964 ring = parse_cookie(bcm, status->cookie, &slot);
968 assert(slot >= 0 && slot < ring->nr_slots);
969 desc = bcm43xx_dma_idx2desc(ring, slot, &meta);
972 tmp = le32_to_cpu(desc->dma64.control0);
973 is_last_fragment = !!(tmp & BCM43xx_DMA64_DCTL0_FRAMEEND);
975 tmp = le32_to_cpu(desc->dma32.control);
976 is_last_fragment = !!(tmp & BCM43xx_DMA32_DCTL_FRAMEEND);
978 unmap_descbuffer(ring, meta->dmaaddr, meta->skb->len, 1);
979 free_descriptor_buffer(ring, meta, 1);
980 /* Everything belonging to the slot is unmapped
981 * and freed, so we can return it.
983 return_slot(ring, slot);
985 if (is_last_fragment)
987 slot = next_slot(ring, slot);
989 bcm->stats.last_tx = jiffies;
992 static void dma_rx(struct bcm43xx_dmaring *ring,
995 struct bcm43xx_dmadesc_generic *desc;
996 struct bcm43xx_dmadesc_meta *meta;
997 struct bcm43xx_rxhdr *rxhdr;
1003 desc = bcm43xx_dma_idx2desc(ring, *slot, &meta);
1005 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1008 if (ring->index == 3) {
1009 /* We received an xmit status. */
1010 struct bcm43xx_hwxmitstatus *hw = (struct bcm43xx_hwxmitstatus *)skb->data;
1011 struct bcm43xx_xmitstatus stat;
1014 stat.cookie = le16_to_cpu(hw->cookie);
1015 while (stat.cookie == 0) {
1016 if (unlikely(++i >= 10000)) {
1022 stat.cookie = le16_to_cpu(hw->cookie);
1024 stat.flags = hw->flags;
1025 stat.cnt1 = hw->cnt1;
1026 stat.cnt2 = hw->cnt2;
1027 stat.seq = le16_to_cpu(hw->seq);
1028 stat.unknown = le16_to_cpu(hw->unknown);
1030 bcm43xx_debugfs_log_txstat(ring->bcm, &stat);
1031 bcm43xx_dma_handle_xmitstatus(ring->bcm, &stat);
1032 /* recycle the descriptor buffer. */
1033 sync_descbuffer_for_device(ring, meta->dmaaddr, ring->rx_buffersize);
1037 rxhdr = (struct bcm43xx_rxhdr *)skb->data;
1038 len = le16_to_cpu(rxhdr->frame_length);
1045 len = le16_to_cpu(rxhdr->frame_length);
1046 } while (len == 0 && i++ < 5);
1047 if (unlikely(len == 0)) {
1048 /* recycle the descriptor buffer. */
1049 sync_descbuffer_for_device(ring, meta->dmaaddr,
1050 ring->rx_buffersize);
1054 if (unlikely(len > ring->rx_buffersize)) {
1055 /* The data did not fit into one descriptor buffer
1056 * and is split over multiple buffers.
1057 * This should never happen, as we try to allocate buffers
1058 * big enough. So simply ignore this packet.
1064 desc = bcm43xx_dma_idx2desc(ring, *slot, &meta);
1065 /* recycle the descriptor buffer. */
1066 sync_descbuffer_for_device(ring, meta->dmaaddr,
1067 ring->rx_buffersize);
1068 *slot = next_slot(ring, *slot);
1070 tmp -= ring->rx_buffersize;
1074 printkl(KERN_ERR PFX "DMA RX buffer too small "
1075 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1076 len, ring->rx_buffersize, cnt);
1079 len -= IEEE80211_FCS_LEN;
1081 dmaaddr = meta->dmaaddr;
1082 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1083 if (unlikely(err)) {
1084 dprintkl(KERN_ERR PFX "DMA RX: setup_rx_descbuffer() failed\n");
1085 sync_descbuffer_for_device(ring, dmaaddr,
1086 ring->rx_buffersize);
1090 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1091 skb_put(skb, len + ring->frameoffset);
1092 skb_pull(skb, ring->frameoffset);
1094 err = bcm43xx_rx(ring->bcm, skb, rxhdr);
1096 dev_kfree_skb_irq(skb);
1104 void bcm43xx_dma_rx(struct bcm43xx_dmaring *ring)
1108 int slot, current_slot;
1109 #ifdef CONFIG_BCM43XX_DEBUG
1115 status = bcm43xx_dma_read(ring, BCM43xx_DMA64_RXSTATUS);
1116 descptr = (status & BCM43xx_DMA64_RXSTATDPTR);
1117 current_slot = descptr / sizeof(struct bcm43xx_dmadesc64);
1119 status = bcm43xx_dma_read(ring, BCM43xx_DMA32_RXSTATUS);
1120 descptr = (status & BCM43xx_DMA32_RXDPTR);
1121 current_slot = descptr / sizeof(struct bcm43xx_dmadesc32);
1123 assert(current_slot >= 0 && current_slot < ring->nr_slots);
1125 slot = ring->current_slot;
1126 for ( ; slot != current_slot; slot = next_slot(ring, slot)) {
1127 dma_rx(ring, &slot);
1128 #ifdef CONFIG_BCM43XX_DEBUG
1129 if (++used_slots > ring->max_used_slots)
1130 ring->max_used_slots = used_slots;
1134 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXINDEX,
1135 (u32)(slot * sizeof(struct bcm43xx_dmadesc64)));
1137 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXINDEX,
1138 (u32)(slot * sizeof(struct bcm43xx_dmadesc32)));
1140 ring->current_slot = slot;
1143 void bcm43xx_dma_tx_suspend(struct bcm43xx_dmaring *ring)
1146 bcm43xx_power_saving_ctl_bits(ring->bcm, -1, 1);
1148 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXCTL,
1149 bcm43xx_dma_read(ring, BCM43xx_DMA64_TXCTL)
1150 | BCM43xx_DMA64_TXSUSPEND);
1152 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXCTL,
1153 bcm43xx_dma_read(ring, BCM43xx_DMA32_TXCTL)
1154 | BCM43xx_DMA32_TXSUSPEND);
1158 void bcm43xx_dma_tx_resume(struct bcm43xx_dmaring *ring)
1162 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXCTL,
1163 bcm43xx_dma_read(ring, BCM43xx_DMA64_TXCTL)
1164 & ~BCM43xx_DMA64_TXSUSPEND);
1166 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXCTL,
1167 bcm43xx_dma_read(ring, BCM43xx_DMA32_TXCTL)
1168 & ~BCM43xx_DMA32_TXSUSPEND);
1170 bcm43xx_power_saving_ctl_bits(ring->bcm, -1, -1);