blob: 1f7731fcfbd57a22dffcb3cab6809921baf0b361 [file] [log] [blame]
John W. Linvillef2223132006-01-23 16:59:58 -05001/*
2
3 Broadcom BCM43xx wireless driver
4
5 DMA ringbuffer and descriptor allocation/management
6
Michael Buesch9218e022006-08-16 00:25:16 +02007 Copyright (c) 2005, 2006 Michael Buesch <mbuesch@freenet.de>
John W. Linvillef2223132006-01-23 16:59:58 -05008
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
12
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.
17
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.
22
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.
27
28*/
29
30#include "bcm43xx.h"
31#include "bcm43xx_dma.h"
32#include "bcm43xx_main.h"
33#include "bcm43xx_debugfs.h"
34#include "bcm43xx_power.h"
Michael Bueschf398f022006-02-23 21:15:39 +010035#include "bcm43xx_xmit.h"
John W. Linvillef2223132006-01-23 16:59:58 -050036
Michael Bueschd1ca6c42006-03-25 15:43:18 +010037#include <linux/dma-mapping.h>
John W. Linvillef2223132006-01-23 16:59:58 -050038#include <linux/pci.h>
39#include <linux/delay.h>
40#include <linux/skbuff.h>
John W. Linvillef2223132006-01-23 16:59:58 -050041
42
43static inline int free_slots(struct bcm43xx_dmaring *ring)
44{
45 return (ring->nr_slots - ring->used_slots);
46}
47
48static inline int next_slot(struct bcm43xx_dmaring *ring, int slot)
49{
50 assert(slot >= -1 && slot <= ring->nr_slots - 1);
51 if (slot == ring->nr_slots - 1)
52 return 0;
53 return slot + 1;
54}
55
56static inline int prev_slot(struct bcm43xx_dmaring *ring, int slot)
57{
58 assert(slot >= 0 && slot <= ring->nr_slots - 1);
59 if (slot == 0)
60 return ring->nr_slots - 1;
61 return slot - 1;
62}
63
64/* Request a slot for usage. */
65static inline
66int request_slot(struct bcm43xx_dmaring *ring)
67{
68 int slot;
69
70 assert(ring->tx);
71 assert(!ring->suspended);
72 assert(free_slots(ring) != 0);
73
74 slot = next_slot(ring, ring->current_slot);
75 ring->current_slot = slot;
76 ring->used_slots++;
77
78 /* Check the number of available slots and suspend TX,
79 * if we are running low on free slots.
80 */
81 if (unlikely(free_slots(ring) < ring->suspend_mark)) {
82 netif_stop_queue(ring->bcm->net_dev);
83 ring->suspended = 1;
84 }
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*/
89
90 return slot;
91}
92
93/* Return a slot to the free slots. */
94static inline
95void return_slot(struct bcm43xx_dmaring *ring, int slot)
96{
97 assert(ring->tx);
98
99 ring->used_slots--;
100
101 /* Check if TX is suspended and check if we have
102 * enough free slots to resume it again.
103 */
104 if (unlikely(ring->suspended)) {
105 if (free_slots(ring) >= ring->resume_mark) {
106 ring->suspended = 0;
107 netif_wake_queue(ring->bcm->net_dev);
108 }
109 }
110}
111
Michael Buesch9218e022006-08-16 00:25:16 +0200112u16 bcm43xx_dmacontroller_base(int dma64bit, int controller_idx)
113{
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,
121 };
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,
129 };
130
131 if (dma64bit) {
132 assert(controller_idx >= 0 &&
133 controller_idx < ARRAY_SIZE(map64));
134 return map64[controller_idx];
135 }
136 assert(controller_idx >= 0 &&
137 controller_idx < ARRAY_SIZE(map32));
138 return map32[controller_idx];
139}
140
John W. Linvillef2223132006-01-23 16:59:58 -0500141static inline
142dma_addr_t map_descbuffer(struct bcm43xx_dmaring *ring,
143 unsigned char *buf,
144 size_t len,
145 int tx)
146{
147 dma_addr_t dmaaddr;
Larry Finger47103032007-01-21 22:27:35 -0600148 int direction = PCI_DMA_FROMDEVICE;
John W. Linvillef2223132006-01-23 16:59:58 -0500149
Larry Finger47103032007-01-21 22:27:35 -0600150 if (tx)
151 direction = PCI_DMA_TODEVICE;
152
153 dmaaddr = pci_map_single(ring->bcm->pci_dev,
John W. Linvillef2223132006-01-23 16:59:58 -0500154 buf, len,
Larry Finger47103032007-01-21 22:27:35 -0600155 direction);
John W. Linvillef2223132006-01-23 16:59:58 -0500156
157 return dmaaddr;
158}
159
160static inline
161void unmap_descbuffer(struct bcm43xx_dmaring *ring,
162 dma_addr_t addr,
163 size_t len,
164 int tx)
165{
166 if (tx) {
Larry Finger47103032007-01-21 22:27:35 -0600167 pci_unmap_single(ring->bcm->pci_dev,
John W. Linvillef2223132006-01-23 16:59:58 -0500168 addr, len,
Larry Finger47103032007-01-21 22:27:35 -0600169 PCI_DMA_TODEVICE);
John W. Linvillef2223132006-01-23 16:59:58 -0500170 } else {
Larry Finger47103032007-01-21 22:27:35 -0600171 pci_unmap_single(ring->bcm->pci_dev,
John W. Linvillef2223132006-01-23 16:59:58 -0500172 addr, len,
Larry Finger47103032007-01-21 22:27:35 -0600173 PCI_DMA_FROMDEVICE);
John W. Linvillef2223132006-01-23 16:59:58 -0500174 }
175}
176
177static inline
178void sync_descbuffer_for_cpu(struct bcm43xx_dmaring *ring,
179 dma_addr_t addr,
180 size_t len)
181{
182 assert(!ring->tx);
183
Larry Finger47103032007-01-21 22:27:35 -0600184 pci_dma_sync_single_for_cpu(ring->bcm->pci_dev,
185 addr, len, PCI_DMA_FROMDEVICE);
John W. Linvillef2223132006-01-23 16:59:58 -0500186}
187
188static inline
189void sync_descbuffer_for_device(struct bcm43xx_dmaring *ring,
190 dma_addr_t addr,
191 size_t len)
192{
193 assert(!ring->tx);
194
Larry Finger47103032007-01-21 22:27:35 -0600195 pci_dma_sync_single_for_cpu(ring->bcm->pci_dev,
196 addr, len, PCI_DMA_TODEVICE);
John W. Linvillef2223132006-01-23 16:59:58 -0500197}
198
John W. Linvillef2223132006-01-23 16:59:58 -0500199/* Unmap and free a descriptor buffer. */
200static inline
201void free_descriptor_buffer(struct bcm43xx_dmaring *ring,
John W. Linvillef2223132006-01-23 16:59:58 -0500202 struct bcm43xx_dmadesc_meta *meta,
203 int irq_context)
204{
205 assert(meta->skb);
Pete Zaitcev512a8092006-03-07 01:37:51 +0100206 if (irq_context)
207 dev_kfree_skb_irq(meta->skb);
208 else
209 dev_kfree_skb(meta->skb);
John W. Linvillef2223132006-01-23 16:59:58 -0500210 meta->skb = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500211}
212
213static int alloc_ringmemory(struct bcm43xx_dmaring *ring)
214{
Larry Finger47103032007-01-21 22:27:35 -0600215 ring->descbase = pci_alloc_consistent(ring->bcm->pci_dev, BCM43xx_DMA_RINGMEMSIZE,
216 &(ring->dmabase));
Michael Buesch9218e022006-08-16 00:25:16 +0200217 if (!ring->descbase) {
Larry Finger47103032007-01-21 22:27:35 -0600218 /* Allocation may have failed due to pci_alloc_consistent
219 insisting on use of GFP_DMA, which is more restrictive
220 than necessary... */
221 struct dma_desc *rx_ring;
222 dma_addr_t rx_ring_dma;
223
224 rx_ring = kzalloc(BCM43xx_DMA_RINGMEMSIZE, GFP_KERNEL);
225 if (!rx_ring)
226 goto out_err;
227
228 rx_ring_dma = pci_map_single(ring->bcm->pci_dev, rx_ring,
229 BCM43xx_DMA_RINGMEMSIZE,
230 PCI_DMA_BIDIRECTIONAL);
231
232 if (pci_dma_mapping_error(rx_ring_dma) ||
233 rx_ring_dma + BCM43xx_DMA_RINGMEMSIZE > ring->bcm->dma_mask) {
234 /* Sigh... */
235 if (!pci_dma_mapping_error(rx_ring_dma))
236 pci_unmap_single(ring->bcm->pci_dev,
237 rx_ring_dma, BCM43xx_DMA_RINGMEMSIZE,
238 PCI_DMA_BIDIRECTIONAL);
239 rx_ring_dma = pci_map_single(ring->bcm->pci_dev,
240 rx_ring, BCM43xx_DMA_RINGMEMSIZE,
241 PCI_DMA_BIDIRECTIONAL);
242 if (pci_dma_mapping_error(rx_ring_dma) ||
243 rx_ring_dma + BCM43xx_DMA_RINGMEMSIZE > ring->bcm->dma_mask) {
244 assert(0);
245 if (!pci_dma_mapping_error(rx_ring_dma))
246 pci_unmap_single(ring->bcm->pci_dev,
247 rx_ring_dma, BCM43xx_DMA_RINGMEMSIZE,
248 PCI_DMA_BIDIRECTIONAL);
249 goto out_err;
250 }
251 }
252
253 ring->descbase = rx_ring;
254 ring->dmabase = rx_ring_dma;
John W. Linvillef2223132006-01-23 16:59:58 -0500255 }
Michael Buesch9218e022006-08-16 00:25:16 +0200256 memset(ring->descbase, 0, BCM43xx_DMA_RINGMEMSIZE);
John W. Linvillef2223132006-01-23 16:59:58 -0500257
258 return 0;
Larry Finger47103032007-01-21 22:27:35 -0600259out_err:
260 printk(KERN_ERR PFX "DMA ringmemory allocation failed\n");
261 return -ENOMEM;
John W. Linvillef2223132006-01-23 16:59:58 -0500262}
263
264static void free_ringmemory(struct bcm43xx_dmaring *ring)
265{
266 struct device *dev = &(ring->bcm->pci_dev->dev);
267
268 dma_free_coherent(dev, BCM43xx_DMA_RINGMEMSIZE,
Michael Buesch9218e022006-08-16 00:25:16 +0200269 ring->descbase, ring->dmabase);
John W. Linvillef2223132006-01-23 16:59:58 -0500270}
271
272/* Reset the RX DMA channel */
273int bcm43xx_dmacontroller_rx_reset(struct bcm43xx_private *bcm,
Michael Buesch9218e022006-08-16 00:25:16 +0200274 u16 mmio_base, int dma64)
John W. Linvillef2223132006-01-23 16:59:58 -0500275{
276 int i;
277 u32 value;
Michael Buesch9218e022006-08-16 00:25:16 +0200278 u16 offset;
John W. Linvillef2223132006-01-23 16:59:58 -0500279
Michael Buesch9218e022006-08-16 00:25:16 +0200280 offset = dma64 ? BCM43xx_DMA64_RXCTL : BCM43xx_DMA32_RXCTL;
281 bcm43xx_write32(bcm, mmio_base + offset, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500282 for (i = 0; i < 1000; i++) {
Michael Buesch9218e022006-08-16 00:25:16 +0200283 offset = dma64 ? BCM43xx_DMA64_RXSTATUS : BCM43xx_DMA32_RXSTATUS;
284 value = bcm43xx_read32(bcm, mmio_base + offset);
285 if (dma64) {
286 value &= BCM43xx_DMA64_RXSTAT;
287 if (value == BCM43xx_DMA64_RXSTAT_DISABLED) {
288 i = -1;
289 break;
290 }
291 } else {
292 value &= BCM43xx_DMA32_RXSTATE;
293 if (value == BCM43xx_DMA32_RXSTAT_DISABLED) {
294 i = -1;
295 break;
296 }
John W. Linvillef2223132006-01-23 16:59:58 -0500297 }
298 udelay(10);
299 }
300 if (i != -1) {
301 printk(KERN_ERR PFX "Error: Wait on DMA RX status timed out.\n");
302 return -ENODEV;
303 }
304
305 return 0;
306}
307
John W. Linvillef2223132006-01-23 16:59:58 -0500308/* Reset the RX DMA channel */
309int bcm43xx_dmacontroller_tx_reset(struct bcm43xx_private *bcm,
Michael Buesch9218e022006-08-16 00:25:16 +0200310 u16 mmio_base, int dma64)
John W. Linvillef2223132006-01-23 16:59:58 -0500311{
312 int i;
313 u32 value;
Michael Buesch9218e022006-08-16 00:25:16 +0200314 u16 offset;
John W. Linvillef2223132006-01-23 16:59:58 -0500315
316 for (i = 0; i < 1000; i++) {
Michael Buesch9218e022006-08-16 00:25:16 +0200317 offset = dma64 ? BCM43xx_DMA64_TXSTATUS : BCM43xx_DMA32_TXSTATUS;
318 value = bcm43xx_read32(bcm, mmio_base + offset);
319 if (dma64) {
320 value &= BCM43xx_DMA64_TXSTAT;
321 if (value == BCM43xx_DMA64_TXSTAT_DISABLED ||
322 value == BCM43xx_DMA64_TXSTAT_IDLEWAIT ||
323 value == BCM43xx_DMA64_TXSTAT_STOPPED)
324 break;
325 } else {
326 value &= BCM43xx_DMA32_TXSTATE;
327 if (value == BCM43xx_DMA32_TXSTAT_DISABLED ||
328 value == BCM43xx_DMA32_TXSTAT_IDLEWAIT ||
329 value == BCM43xx_DMA32_TXSTAT_STOPPED)
330 break;
331 }
John W. Linvillef2223132006-01-23 16:59:58 -0500332 udelay(10);
333 }
Michael Buesch9218e022006-08-16 00:25:16 +0200334 offset = dma64 ? BCM43xx_DMA64_TXCTL : BCM43xx_DMA32_TXCTL;
335 bcm43xx_write32(bcm, mmio_base + offset, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500336 for (i = 0; i < 1000; i++) {
Michael Buesch9218e022006-08-16 00:25:16 +0200337 offset = dma64 ? BCM43xx_DMA64_TXSTATUS : BCM43xx_DMA32_TXSTATUS;
338 value = bcm43xx_read32(bcm, mmio_base + offset);
339 if (dma64) {
340 value &= BCM43xx_DMA64_TXSTAT;
341 if (value == BCM43xx_DMA64_TXSTAT_DISABLED) {
342 i = -1;
343 break;
344 }
345 } else {
346 value &= BCM43xx_DMA32_TXSTATE;
347 if (value == BCM43xx_DMA32_TXSTAT_DISABLED) {
348 i = -1;
349 break;
350 }
John W. Linvillef2223132006-01-23 16:59:58 -0500351 }
352 udelay(10);
353 }
354 if (i != -1) {
355 printk(KERN_ERR PFX "Error: Wait on DMA TX status timed out.\n");
356 return -ENODEV;
357 }
358 /* ensure the reset is completed. */
359 udelay(300);
360
361 return 0;
362}
363
Michael Buesch9218e022006-08-16 00:25:16 +0200364static void fill_descriptor(struct bcm43xx_dmaring *ring,
365 struct bcm43xx_dmadesc_generic *desc,
366 dma_addr_t dmaaddr,
367 u16 bufsize,
368 int start, int end, int irq)
369{
370 int slot;
371
372 slot = bcm43xx_dma_desc2idx(ring, desc);
373 assert(slot >= 0 && slot < ring->nr_slots);
374
375 if (ring->dma64) {
376 u32 ctl0 = 0, ctl1 = 0;
377 u32 addrlo, addrhi;
378 u32 addrext;
379
380 addrlo = (u32)(dmaaddr & 0xFFFFFFFF);
381 addrhi = (((u64)dmaaddr >> 32) & ~BCM43xx_DMA64_ROUTING);
382 addrext = (((u64)dmaaddr >> 32) >> BCM43xx_DMA64_ROUTING_SHIFT);
383 addrhi |= ring->routing;
384 if (slot == ring->nr_slots - 1)
385 ctl0 |= BCM43xx_DMA64_DCTL0_DTABLEEND;
386 if (start)
387 ctl0 |= BCM43xx_DMA64_DCTL0_FRAMESTART;
388 if (end)
389 ctl0 |= BCM43xx_DMA64_DCTL0_FRAMEEND;
390 if (irq)
391 ctl0 |= BCM43xx_DMA64_DCTL0_IRQ;
392 ctl1 |= (bufsize - ring->frameoffset)
393 & BCM43xx_DMA64_DCTL1_BYTECNT;
394 ctl1 |= (addrext << BCM43xx_DMA64_DCTL1_ADDREXT_SHIFT)
395 & BCM43xx_DMA64_DCTL1_ADDREXT_MASK;
396
397 desc->dma64.control0 = cpu_to_le32(ctl0);
398 desc->dma64.control1 = cpu_to_le32(ctl1);
399 desc->dma64.address_low = cpu_to_le32(addrlo);
400 desc->dma64.address_high = cpu_to_le32(addrhi);
401 } else {
402 u32 ctl;
403 u32 addr;
404 u32 addrext;
405
406 addr = (u32)(dmaaddr & ~BCM43xx_DMA32_ROUTING);
407 addrext = (u32)(dmaaddr & BCM43xx_DMA32_ROUTING)
408 >> BCM43xx_DMA32_ROUTING_SHIFT;
409 addr |= ring->routing;
410 ctl = (bufsize - ring->frameoffset)
411 & BCM43xx_DMA32_DCTL_BYTECNT;
412 if (slot == ring->nr_slots - 1)
413 ctl |= BCM43xx_DMA32_DCTL_DTABLEEND;
414 if (start)
415 ctl |= BCM43xx_DMA32_DCTL_FRAMESTART;
416 if (end)
417 ctl |= BCM43xx_DMA32_DCTL_FRAMEEND;
418 if (irq)
419 ctl |= BCM43xx_DMA32_DCTL_IRQ;
420 ctl |= (addrext << BCM43xx_DMA32_DCTL_ADDREXT_SHIFT)
421 & BCM43xx_DMA32_DCTL_ADDREXT_MASK;
422
423 desc->dma32.control = cpu_to_le32(ctl);
424 desc->dma32.address = cpu_to_le32(addr);
425 }
426}
427
John W. Linvillef2223132006-01-23 16:59:58 -0500428static int setup_rx_descbuffer(struct bcm43xx_dmaring *ring,
Michael Buesch9218e022006-08-16 00:25:16 +0200429 struct bcm43xx_dmadesc_generic *desc,
John W. Linvillef2223132006-01-23 16:59:58 -0500430 struct bcm43xx_dmadesc_meta *meta,
431 gfp_t gfp_flags)
432{
433 struct bcm43xx_rxhdr *rxhdr;
Michael Buesch9218e022006-08-16 00:25:16 +0200434 struct bcm43xx_hwxmitstatus *xmitstat;
John W. Linvillef2223132006-01-23 16:59:58 -0500435 dma_addr_t dmaaddr;
John W. Linvillef2223132006-01-23 16:59:58 -0500436 struct sk_buff *skb;
437
John W. Linvillef2223132006-01-23 16:59:58 -0500438 assert(!ring->tx);
439
440 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
441 if (unlikely(!skb))
442 return -ENOMEM;
443 dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0);
Larry Finger47103032007-01-21 22:27:35 -0600444 /* This hardware bug work-around adapted from the b44 driver.
445 The chip may be unable to do PCI DMA to/from anything above 1GB */
446 if (pci_dma_mapping_error(dmaaddr) ||
447 dmaaddr + ring->rx_buffersize > ring->bcm->dma_mask) {
448 /* This one has 30-bit addressing... */
449 if (!pci_dma_mapping_error(dmaaddr))
450 pci_unmap_single(ring->bcm->pci_dev,
451 dmaaddr, ring->rx_buffersize,
452 PCI_DMA_FROMDEVICE);
453 dev_kfree_skb_any(skb);
454 skb = __dev_alloc_skb(ring->rx_buffersize,GFP_DMA);
455 if (skb == NULL)
456 return -ENOMEM;
457 dmaaddr = pci_map_single(ring->bcm->pci_dev,
458 skb->data, ring->rx_buffersize,
459 PCI_DMA_FROMDEVICE);
460 if (pci_dma_mapping_error(dmaaddr) ||
461 dmaaddr + ring->rx_buffersize > ring->bcm->dma_mask) {
462 assert(0);
463 dev_kfree_skb_any(skb);
464 return -ENOMEM;
465 }
466 }
John W. Linvillef2223132006-01-23 16:59:58 -0500467 meta->skb = skb;
468 meta->dmaaddr = dmaaddr;
469 skb->dev = ring->bcm->net_dev;
Michael Buesch9218e022006-08-16 00:25:16 +0200470
471 fill_descriptor(ring, desc, dmaaddr,
472 ring->rx_buffersize, 0, 0, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500473
474 rxhdr = (struct bcm43xx_rxhdr *)(skb->data);
475 rxhdr->frame_length = 0;
476 rxhdr->flags1 = 0;
Michael Buesch9218e022006-08-16 00:25:16 +0200477 xmitstat = (struct bcm43xx_hwxmitstatus *)(skb->data);
478 xmitstat->cookie = 0;
John W. Linvillef2223132006-01-23 16:59:58 -0500479
480 return 0;
481}
482
483/* Allocate the initial descbuffers.
484 * This is used for an RX ring only.
485 */
486static int alloc_initial_descbuffers(struct bcm43xx_dmaring *ring)
487{
488 int i, err = -ENOMEM;
Michael Buesch9218e022006-08-16 00:25:16 +0200489 struct bcm43xx_dmadesc_generic *desc;
John W. Linvillef2223132006-01-23 16:59:58 -0500490 struct bcm43xx_dmadesc_meta *meta;
491
492 for (i = 0; i < ring->nr_slots; i++) {
Michael Buesch9218e022006-08-16 00:25:16 +0200493 desc = bcm43xx_dma_idx2desc(ring, i, &meta);
John W. Linvillef2223132006-01-23 16:59:58 -0500494
495 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
496 if (err)
497 goto err_unwind;
John W. Linvillef2223132006-01-23 16:59:58 -0500498 }
Michael Buesch9218e022006-08-16 00:25:16 +0200499 mb();
John W. Linvillef2223132006-01-23 16:59:58 -0500500 ring->used_slots = ring->nr_slots;
John W. Linvillef2223132006-01-23 16:59:58 -0500501 err = 0;
502out:
503 return err;
504
505err_unwind:
Michael Bueschea72ab22006-01-27 17:26:20 +0100506 for (i--; i >= 0; i--) {
Michael Buesch9218e022006-08-16 00:25:16 +0200507 desc = bcm43xx_dma_idx2desc(ring, i, &meta);
John W. Linvillef2223132006-01-23 16:59:58 -0500508
509 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
510 dev_kfree_skb(meta->skb);
511 }
John W. Linvillef2223132006-01-23 16:59:58 -0500512 goto out;
513}
514
515/* Do initial setup of the DMA controller.
516 * Reset the controller, write the ring busaddress
517 * and switch the "enable" bit on.
518 */
519static int dmacontroller_setup(struct bcm43xx_dmaring *ring)
520{
521 int err = 0;
522 u32 value;
Michael Buesch9218e022006-08-16 00:25:16 +0200523 u32 addrext;
John W. Linvillef2223132006-01-23 16:59:58 -0500524
525 if (ring->tx) {
Michael Buesch9218e022006-08-16 00:25:16 +0200526 if (ring->dma64) {
527 u64 ringbase = (u64)(ring->dmabase);
528
529 addrext = ((ringbase >> 32) >> BCM43xx_DMA64_ROUTING_SHIFT);
530 value = BCM43xx_DMA64_TXENABLE;
531 value |= (addrext << BCM43xx_DMA64_TXADDREXT_SHIFT)
532 & BCM43xx_DMA64_TXADDREXT_MASK;
533 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXCTL, value);
534 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGLO,
535 (ringbase & 0xFFFFFFFF));
536 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGHI,
537 ((ringbase >> 32) & ~BCM43xx_DMA64_ROUTING)
538 | ring->routing);
539 } else {
540 u32 ringbase = (u32)(ring->dmabase);
541
542 addrext = (ringbase >> BCM43xx_DMA32_ROUTING_SHIFT);
543 value = BCM43xx_DMA32_TXENABLE;
544 value |= (addrext << BCM43xx_DMA32_TXADDREXT_SHIFT)
545 & BCM43xx_DMA32_TXADDREXT_MASK;
546 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXCTL, value);
547 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXRING,
548 (ringbase & ~BCM43xx_DMA32_ROUTING)
549 | ring->routing);
550 }
John W. Linvillef2223132006-01-23 16:59:58 -0500551 } else {
552 err = alloc_initial_descbuffers(ring);
553 if (err)
554 goto out;
Michael Buesch9218e022006-08-16 00:25:16 +0200555 if (ring->dma64) {
556 u64 ringbase = (u64)(ring->dmabase);
557
558 addrext = ((ringbase >> 32) >> BCM43xx_DMA64_ROUTING_SHIFT);
559 value = (ring->frameoffset << BCM43xx_DMA64_RXFROFF_SHIFT);
560 value |= BCM43xx_DMA64_RXENABLE;
561 value |= (addrext << BCM43xx_DMA64_RXADDREXT_SHIFT)
562 & BCM43xx_DMA64_RXADDREXT_MASK;
563 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXCTL, value);
564 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGLO,
565 (ringbase & 0xFFFFFFFF));
566 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGHI,
567 ((ringbase >> 32) & ~BCM43xx_DMA64_ROUTING)
568 | ring->routing);
569 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXINDEX, 200);
570 } else {
571 u32 ringbase = (u32)(ring->dmabase);
572
573 addrext = (ringbase >> BCM43xx_DMA32_ROUTING_SHIFT);
574 value = (ring->frameoffset << BCM43xx_DMA32_RXFROFF_SHIFT);
575 value |= BCM43xx_DMA32_RXENABLE;
576 value |= (addrext << BCM43xx_DMA32_RXADDREXT_SHIFT)
577 & BCM43xx_DMA32_RXADDREXT_MASK;
578 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXCTL, value);
579 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXRING,
580 (ringbase & ~BCM43xx_DMA32_ROUTING)
581 | ring->routing);
582 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXINDEX, 200);
583 }
John W. Linvillef2223132006-01-23 16:59:58 -0500584 }
585
586out:
587 return err;
588}
589
590/* Shutdown the DMA controller. */
591static void dmacontroller_cleanup(struct bcm43xx_dmaring *ring)
592{
593 if (ring->tx) {
Michael Buesch9218e022006-08-16 00:25:16 +0200594 bcm43xx_dmacontroller_tx_reset(ring->bcm, ring->mmio_base, ring->dma64);
595 if (ring->dma64) {
596 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGLO, 0);
597 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXRINGHI, 0);
598 } else
599 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXRING, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500600 } else {
Michael Buesch9218e022006-08-16 00:25:16 +0200601 bcm43xx_dmacontroller_rx_reset(ring->bcm, ring->mmio_base, ring->dma64);
602 if (ring->dma64) {
603 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGLO, 0);
604 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXRINGHI, 0);
605 } else
606 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXRING, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500607 }
608}
609
610static void free_all_descbuffers(struct bcm43xx_dmaring *ring)
611{
Michael Buesch9218e022006-08-16 00:25:16 +0200612 struct bcm43xx_dmadesc_generic *desc;
John W. Linvillef2223132006-01-23 16:59:58 -0500613 struct bcm43xx_dmadesc_meta *meta;
614 int i;
615
616 if (!ring->used_slots)
617 return;
618 for (i = 0; i < ring->nr_slots; i++) {
Michael Buesch9218e022006-08-16 00:25:16 +0200619 desc = bcm43xx_dma_idx2desc(ring, i, &meta);
John W. Linvillef2223132006-01-23 16:59:58 -0500620
621 if (!meta->skb) {
622 assert(ring->tx);
John W. Linvillef2223132006-01-23 16:59:58 -0500623 continue;
624 }
625 if (ring->tx) {
626 unmap_descbuffer(ring, meta->dmaaddr,
Michael Buesch9218e022006-08-16 00:25:16 +0200627 meta->skb->len, 1);
John W. Linvillef2223132006-01-23 16:59:58 -0500628 } else {
629 unmap_descbuffer(ring, meta->dmaaddr,
Michael Buesch9218e022006-08-16 00:25:16 +0200630 ring->rx_buffersize, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500631 }
Michael Buesch9218e022006-08-16 00:25:16 +0200632 free_descriptor_buffer(ring, meta, 0);
John W. Linvillef2223132006-01-23 16:59:58 -0500633 }
634}
635
636/* Main initialization function. */
637static
638struct bcm43xx_dmaring * bcm43xx_setup_dmaring(struct bcm43xx_private *bcm,
Michael Buesch9218e022006-08-16 00:25:16 +0200639 int controller_index,
640 int for_tx,
641 int dma64)
John W. Linvillef2223132006-01-23 16:59:58 -0500642{
643 struct bcm43xx_dmaring *ring;
644 int err;
Michael Buesch9218e022006-08-16 00:25:16 +0200645 int nr_slots;
John W. Linvillef2223132006-01-23 16:59:58 -0500646
647 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
648 if (!ring)
649 goto out;
650
Michael Buesch9218e022006-08-16 00:25:16 +0200651 nr_slots = BCM43xx_RXRING_SLOTS;
652 if (for_tx)
653 nr_slots = BCM43xx_TXRING_SLOTS;
654
655 ring->meta = kcalloc(nr_slots, sizeof(struct bcm43xx_dmadesc_meta),
John W. Linvillef2223132006-01-23 16:59:58 -0500656 GFP_KERNEL);
657 if (!ring->meta)
658 goto err_kfree_ring;
659
Michael Buesch9218e022006-08-16 00:25:16 +0200660 ring->routing = BCM43xx_DMA32_CLIENTTRANS;
661 if (dma64)
662 ring->routing = BCM43xx_DMA64_CLIENTTRANS;
Michael Bueschea72ab22006-01-27 17:26:20 +0100663
John W. Linvillef2223132006-01-23 16:59:58 -0500664 ring->bcm = bcm;
Michael Buesch9218e022006-08-16 00:25:16 +0200665 ring->nr_slots = nr_slots;
John W. Linvillef2223132006-01-23 16:59:58 -0500666 ring->suspend_mark = ring->nr_slots * BCM43xx_TXSUSPEND_PERCENT / 100;
667 ring->resume_mark = ring->nr_slots * BCM43xx_TXRESUME_PERCENT / 100;
668 assert(ring->suspend_mark < ring->resume_mark);
Michael Buesch9218e022006-08-16 00:25:16 +0200669 ring->mmio_base = bcm43xx_dmacontroller_base(dma64, controller_index);
670 ring->index = controller_index;
671 ring->dma64 = !!dma64;
672 if (for_tx) {
John W. Linvillef2223132006-01-23 16:59:58 -0500673 ring->tx = 1;
674 ring->current_slot = -1;
675 } else {
Michael Buesch9218e022006-08-16 00:25:16 +0200676 if (ring->index == 0) {
677 ring->rx_buffersize = BCM43xx_DMA0_RX_BUFFERSIZE;
678 ring->frameoffset = BCM43xx_DMA0_RX_FRAMEOFFSET;
679 } else if (ring->index == 3) {
680 ring->rx_buffersize = BCM43xx_DMA3_RX_BUFFERSIZE;
681 ring->frameoffset = BCM43xx_DMA3_RX_FRAMEOFFSET;
682 } else
John W. Linvillef2223132006-01-23 16:59:58 -0500683 assert(0);
John W. Linvillef2223132006-01-23 16:59:58 -0500684 }
685
686 err = alloc_ringmemory(ring);
687 if (err)
688 goto err_kfree_meta;
689 err = dmacontroller_setup(ring);
690 if (err)
691 goto err_free_ringmemory;
Larry Finger47103032007-01-21 22:27:35 -0600692 return ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500693
694out:
Larry Finger47103032007-01-21 22:27:35 -0600695 printk(KERN_ERR PFX "Error in bcm43xx_setup_dmaring\n");
John W. Linvillef2223132006-01-23 16:59:58 -0500696 return ring;
697
698err_free_ringmemory:
699 free_ringmemory(ring);
700err_kfree_meta:
701 kfree(ring->meta);
702err_kfree_ring:
703 kfree(ring);
704 ring = NULL;
705 goto out;
706}
707
708/* Main cleanup function. */
709static void bcm43xx_destroy_dmaring(struct bcm43xx_dmaring *ring)
710{
711 if (!ring)
712 return;
713
Michael Buesch9218e022006-08-16 00:25:16 +0200714 dprintk(KERN_INFO PFX "DMA-%s 0x%04X (%s) max used slots: %d/%d\n",
715 (ring->dma64) ? "64" : "32",
John W. Linvillef2223132006-01-23 16:59:58 -0500716 ring->mmio_base,
717 (ring->tx) ? "TX" : "RX",
718 ring->max_used_slots, ring->nr_slots);
719 /* Device IRQs are disabled prior entering this function,
720 * so no need to take care of concurrency with rx handler stuff.
721 */
722 dmacontroller_cleanup(ring);
723 free_all_descbuffers(ring);
724 free_ringmemory(ring);
725
726 kfree(ring->meta);
727 kfree(ring);
728}
729
730void bcm43xx_dma_free(struct bcm43xx_private *bcm)
731{
Michael Buesch49f29efa2006-03-14 16:05:26 +0100732 struct bcm43xx_dma *dma;
733
734 if (bcm43xx_using_pio(bcm))
735 return;
736 dma = bcm43xx_current_dma(bcm);
Michael Bueschea72ab22006-01-27 17:26:20 +0100737
Michael Buesch9218e022006-08-16 00:25:16 +0200738 bcm43xx_destroy_dmaring(dma->rx_ring3);
739 dma->rx_ring3 = NULL;
Michael Bueschea72ab22006-01-27 17:26:20 +0100740 bcm43xx_destroy_dmaring(dma->rx_ring0);
741 dma->rx_ring0 = NULL;
Michael Buesch9218e022006-08-16 00:25:16 +0200742
743 bcm43xx_destroy_dmaring(dma->tx_ring5);
744 dma->tx_ring5 = NULL;
745 bcm43xx_destroy_dmaring(dma->tx_ring4);
746 dma->tx_ring4 = NULL;
Michael Bueschea72ab22006-01-27 17:26:20 +0100747 bcm43xx_destroy_dmaring(dma->tx_ring3);
748 dma->tx_ring3 = NULL;
749 bcm43xx_destroy_dmaring(dma->tx_ring2);
750 dma->tx_ring2 = NULL;
751 bcm43xx_destroy_dmaring(dma->tx_ring1);
752 dma->tx_ring1 = NULL;
753 bcm43xx_destroy_dmaring(dma->tx_ring0);
754 dma->tx_ring0 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500755}
756
757int bcm43xx_dma_init(struct bcm43xx_private *bcm)
758{
Michael Buesche9357c02006-03-13 19:27:34 +0100759 struct bcm43xx_dma *dma = bcm43xx_current_dma(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500760 struct bcm43xx_dmaring *ring;
761 int err = -ENOMEM;
Michael Buesch9218e022006-08-16 00:25:16 +0200762 int dma64 = 0;
Michael Buesch9218e022006-08-16 00:25:16 +0200763
Larry Finger47103032007-01-21 22:27:35 -0600764 bcm->dma_mask = bcm43xx_get_supported_dma_mask(bcm);
765 if (bcm->dma_mask == DMA_64BIT_MASK)
Michael Buesch9218e022006-08-16 00:25:16 +0200766 dma64 = 1;
Larry Finger47103032007-01-21 22:27:35 -0600767 err = pci_set_dma_mask(bcm->pci_dev, bcm->dma_mask);
768 if (err)
769 goto no_dma;
770 err = pci_set_consistent_dma_mask(bcm->pci_dev, bcm->dma_mask);
771 if (err)
772 goto no_dma;
John W. Linvillef2223132006-01-23 16:59:58 -0500773
774 /* setup TX DMA channels. */
Michael Buesch9218e022006-08-16 00:25:16 +0200775 ring = bcm43xx_setup_dmaring(bcm, 0, 1, dma64);
John W. Linvillef2223132006-01-23 16:59:58 -0500776 if (!ring)
777 goto out;
Michael Bueschea72ab22006-01-27 17:26:20 +0100778 dma->tx_ring0 = ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500779
Michael Buesch9218e022006-08-16 00:25:16 +0200780 ring = bcm43xx_setup_dmaring(bcm, 1, 1, dma64);
John W. Linvillef2223132006-01-23 16:59:58 -0500781 if (!ring)
782 goto err_destroy_tx0;
Michael Bueschea72ab22006-01-27 17:26:20 +0100783 dma->tx_ring1 = ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500784
Michael Buesch9218e022006-08-16 00:25:16 +0200785 ring = bcm43xx_setup_dmaring(bcm, 2, 1, dma64);
John W. Linvillef2223132006-01-23 16:59:58 -0500786 if (!ring)
787 goto err_destroy_tx1;
Michael Bueschea72ab22006-01-27 17:26:20 +0100788 dma->tx_ring2 = ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500789
Michael Buesch9218e022006-08-16 00:25:16 +0200790 ring = bcm43xx_setup_dmaring(bcm, 3, 1, dma64);
John W. Linvillef2223132006-01-23 16:59:58 -0500791 if (!ring)
792 goto err_destroy_tx2;
Michael Bueschea72ab22006-01-27 17:26:20 +0100793 dma->tx_ring3 = ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500794
Michael Buesch9218e022006-08-16 00:25:16 +0200795 ring = bcm43xx_setup_dmaring(bcm, 4, 1, dma64);
John W. Linvillef2223132006-01-23 16:59:58 -0500796 if (!ring)
797 goto err_destroy_tx3;
Michael Buesch9218e022006-08-16 00:25:16 +0200798 dma->tx_ring4 = ring;
799
800 ring = bcm43xx_setup_dmaring(bcm, 5, 1, dma64);
801 if (!ring)
802 goto err_destroy_tx4;
803 dma->tx_ring5 = ring;
804
805 /* setup RX DMA channels. */
806 ring = bcm43xx_setup_dmaring(bcm, 0, 0, dma64);
807 if (!ring)
808 goto err_destroy_tx5;
Michael Bueschea72ab22006-01-27 17:26:20 +0100809 dma->rx_ring0 = ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500810
811 if (bcm->current_core->rev < 5) {
Michael Buesch9218e022006-08-16 00:25:16 +0200812 ring = bcm43xx_setup_dmaring(bcm, 3, 0, dma64);
John W. Linvillef2223132006-01-23 16:59:58 -0500813 if (!ring)
814 goto err_destroy_rx0;
Michael Buesch9218e022006-08-16 00:25:16 +0200815 dma->rx_ring3 = ring;
John W. Linvillef2223132006-01-23 16:59:58 -0500816 }
817
Larry Finger47103032007-01-21 22:27:35 -0600818 dprintk(KERN_INFO PFX "%d-bit DMA initialized\n",
819 (bcm->dma_mask == DMA_64BIT_MASK) ? 64 :
820 (bcm->dma_mask == DMA_32BIT_MASK) ? 32 : 30);
John W. Linvillef2223132006-01-23 16:59:58 -0500821 err = 0;
822out:
823 return err;
824
825err_destroy_rx0:
Michael Bueschea72ab22006-01-27 17:26:20 +0100826 bcm43xx_destroy_dmaring(dma->rx_ring0);
827 dma->rx_ring0 = NULL;
Michael Buesch9218e022006-08-16 00:25:16 +0200828err_destroy_tx5:
829 bcm43xx_destroy_dmaring(dma->tx_ring5);
830 dma->tx_ring5 = NULL;
831err_destroy_tx4:
832 bcm43xx_destroy_dmaring(dma->tx_ring4);
833 dma->tx_ring4 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500834err_destroy_tx3:
Michael Bueschea72ab22006-01-27 17:26:20 +0100835 bcm43xx_destroy_dmaring(dma->tx_ring3);
836 dma->tx_ring3 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500837err_destroy_tx2:
Michael Bueschea72ab22006-01-27 17:26:20 +0100838 bcm43xx_destroy_dmaring(dma->tx_ring2);
839 dma->tx_ring2 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500840err_destroy_tx1:
Michael Bueschea72ab22006-01-27 17:26:20 +0100841 bcm43xx_destroy_dmaring(dma->tx_ring1);
842 dma->tx_ring1 = NULL;
John W. Linvillef2223132006-01-23 16:59:58 -0500843err_destroy_tx0:
Michael Bueschea72ab22006-01-27 17:26:20 +0100844 bcm43xx_destroy_dmaring(dma->tx_ring0);
845 dma->tx_ring0 = NULL;
Larry Finger47103032007-01-21 22:27:35 -0600846no_dma:
847#ifdef CONFIG_BCM43XX_PIO
848 printk(KERN_WARNING PFX "DMA not supported on this device."
849 " Falling back to PIO.\n");
850 bcm->__using_pio = 1;
851 return -ENOSYS;
852#else
853 printk(KERN_ERR PFX "FATAL: DMA not supported and PIO not configured. "
854 "Please recompile the driver with PIO support.\n");
855 return -ENODEV;
856#endif /* CONFIG_BCM43XX_PIO */
John W. Linvillef2223132006-01-23 16:59:58 -0500857}
858
859/* Generate a cookie for the TX header. */
Michael Bueschea72ab22006-01-27 17:26:20 +0100860static u16 generate_cookie(struct bcm43xx_dmaring *ring,
861 int slot)
John W. Linvillef2223132006-01-23 16:59:58 -0500862{
Michael Buesch9218e022006-08-16 00:25:16 +0200863 u16 cookie = 0x1000;
John W. Linvillef2223132006-01-23 16:59:58 -0500864
865 /* Use the upper 4 bits of the cookie as
866 * DMA controller ID and store the slot number
Michael Bueschea9a7712006-06-04 02:20:42 +0200867 * in the lower 12 bits.
868 * Note that the cookie must never be 0, as this
869 * is a special value used in RX path.
John W. Linvillef2223132006-01-23 16:59:58 -0500870 */
Michael Buesch9218e022006-08-16 00:25:16 +0200871 switch (ring->index) {
872 case 0:
Michael Bueschea9a7712006-06-04 02:20:42 +0200873 cookie = 0xA000;
John W. Linvillef2223132006-01-23 16:59:58 -0500874 break;
Michael Buesch9218e022006-08-16 00:25:16 +0200875 case 1:
Michael Bueschea9a7712006-06-04 02:20:42 +0200876 cookie = 0xB000;
John W. Linvillef2223132006-01-23 16:59:58 -0500877 break;
Michael Buesch9218e022006-08-16 00:25:16 +0200878 case 2:
Michael Bueschea9a7712006-06-04 02:20:42 +0200879 cookie = 0xC000;
John W. Linvillef2223132006-01-23 16:59:58 -0500880 break;
Michael Buesch9218e022006-08-16 00:25:16 +0200881 case 3:
Michael Bueschea9a7712006-06-04 02:20:42 +0200882 cookie = 0xD000;
John W. Linvillef2223132006-01-23 16:59:58 -0500883 break;
Michael Buesch9218e022006-08-16 00:25:16 +0200884 case 4:
885 cookie = 0xE000;
886 break;
887 case 5:
888 cookie = 0xF000;
889 break;
John W. Linvillef2223132006-01-23 16:59:58 -0500890 }
891 assert(((u16)slot & 0xF000) == 0x0000);
892 cookie |= (u16)slot;
893
894 return cookie;
895}
896
897/* Inspect a cookie and find out to which controller/slot it belongs. */
Michael Bueschea72ab22006-01-27 17:26:20 +0100898static
John W. Linvillef2223132006-01-23 16:59:58 -0500899struct bcm43xx_dmaring * parse_cookie(struct bcm43xx_private *bcm,
900 u16 cookie, int *slot)
901{
Michael Buesche9357c02006-03-13 19:27:34 +0100902 struct bcm43xx_dma *dma = bcm43xx_current_dma(bcm);
John W. Linvillef2223132006-01-23 16:59:58 -0500903 struct bcm43xx_dmaring *ring = NULL;
904
905 switch (cookie & 0xF000) {
Michael Bueschea9a7712006-06-04 02:20:42 +0200906 case 0xA000:
Michael Bueschea72ab22006-01-27 17:26:20 +0100907 ring = dma->tx_ring0;
John W. Linvillef2223132006-01-23 16:59:58 -0500908 break;
Michael Bueschea9a7712006-06-04 02:20:42 +0200909 case 0xB000:
Michael Bueschea72ab22006-01-27 17:26:20 +0100910 ring = dma->tx_ring1;
John W. Linvillef2223132006-01-23 16:59:58 -0500911 break;
Michael Bueschea9a7712006-06-04 02:20:42 +0200912 case 0xC000:
Michael Bueschea72ab22006-01-27 17:26:20 +0100913 ring = dma->tx_ring2;
John W. Linvillef2223132006-01-23 16:59:58 -0500914 break;
Michael Bueschea9a7712006-06-04 02:20:42 +0200915 case 0xD000:
Michael Bueschea72ab22006-01-27 17:26:20 +0100916 ring = dma->tx_ring3;
John W. Linvillef2223132006-01-23 16:59:58 -0500917 break;
Michael Buesch9218e022006-08-16 00:25:16 +0200918 case 0xE000:
919 ring = dma->tx_ring4;
920 break;
921 case 0xF000:
922 ring = dma->tx_ring5;
923 break;
John W. Linvillef2223132006-01-23 16:59:58 -0500924 default:
925 assert(0);
926 }
927 *slot = (cookie & 0x0FFF);
928 assert(*slot >= 0 && *slot < ring->nr_slots);
929
930 return ring;
931}
932
Michael Bueschea72ab22006-01-27 17:26:20 +0100933static void dmacontroller_poke_tx(struct bcm43xx_dmaring *ring,
934 int slot)
John W. Linvillef2223132006-01-23 16:59:58 -0500935{
Michael Buesch9218e022006-08-16 00:25:16 +0200936 u16 offset;
937 int descsize;
938
John W. Linvillef2223132006-01-23 16:59:58 -0500939 /* Everything is ready to start. Buffers are DMA mapped and
940 * associated with slots.
941 * "slot" is the last slot of the new frame we want to transmit.
942 * Close your seat belts now, please.
943 */
944 wmb();
945 slot = next_slot(ring, slot);
Michael Buesch9218e022006-08-16 00:25:16 +0200946 offset = (ring->dma64) ? BCM43xx_DMA64_TXINDEX : BCM43xx_DMA32_TXINDEX;
947 descsize = (ring->dma64) ? sizeof(struct bcm43xx_dmadesc64)
948 : sizeof(struct bcm43xx_dmadesc32);
949 bcm43xx_dma_write(ring, offset,
950 (u32)(slot * descsize));
John W. Linvillef2223132006-01-23 16:59:58 -0500951}
952
Michael Buesch9218e022006-08-16 00:25:16 +0200953static void dma_tx_fragment(struct bcm43xx_dmaring *ring,
954 struct sk_buff *skb,
955 u8 cur_frag)
John W. Linvillef2223132006-01-23 16:59:58 -0500956{
957 int slot;
Michael Buesch9218e022006-08-16 00:25:16 +0200958 struct bcm43xx_dmadesc_generic *desc;
John W. Linvillef2223132006-01-23 16:59:58 -0500959 struct bcm43xx_dmadesc_meta *meta;
Michael Buesch9218e022006-08-16 00:25:16 +0200960 dma_addr_t dmaaddr;
Larry Finger47103032007-01-21 22:27:35 -0600961 struct sk_buff *bounce_skb;
John W. Linvillef2223132006-01-23 16:59:58 -0500962
963 assert(skb_shinfo(skb)->nr_frags == 0);
964
965 slot = request_slot(ring);
Michael Buesch9218e022006-08-16 00:25:16 +0200966 desc = bcm43xx_dma_idx2desc(ring, slot, &meta);
John W. Linvillef2223132006-01-23 16:59:58 -0500967
John W. Linvillef2223132006-01-23 16:59:58 -0500968 /* Add a device specific TX header. */
969 assert(skb_headroom(skb) >= sizeof(struct bcm43xx_txhdr));
970 /* Reserve enough headroom for the device tx header. */
971 __skb_push(skb, sizeof(struct bcm43xx_txhdr));
972 /* Now calculate and add the tx header.
973 * The tx header includes the PLCP header.
974 */
975 bcm43xx_generate_txhdr(ring->bcm,
976 (struct bcm43xx_txhdr *)skb->data,
977 skb->data + sizeof(struct bcm43xx_txhdr),
978 skb->len - sizeof(struct bcm43xx_txhdr),
979 (cur_frag == 0),
980 generate_cookie(ring, slot));
Larry Finger47103032007-01-21 22:27:35 -0600981 dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
982 if (dma_mapping_error(dmaaddr) || dmaaddr + skb->len > ring->bcm->dma_mask) {
983 /* chip cannot handle DMA to/from > 1GB, use bounce buffer (copied from b44 driver) */
984 if (!dma_mapping_error(dmaaddr))
985 unmap_descbuffer(ring, dmaaddr, skb->len, 1);
986 bounce_skb = __dev_alloc_skb(skb->len, GFP_ATOMIC|GFP_DMA);
987 if (!bounce_skb)
988 return;
989 dmaaddr = map_descbuffer(ring, bounce_skb->data, bounce_skb->len, 1);
990 if (dma_mapping_error(dmaaddr) || dmaaddr + skb->len > ring->bcm->dma_mask) {
991 if (!dma_mapping_error(dmaaddr))
992 unmap_descbuffer(ring, dmaaddr, skb->len, 1);
993 dev_kfree_skb_any(bounce_skb);
994 assert(0);
995 return;
996 }
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300997 skb_copy_from_linear_data(skb, skb_put(bounce_skb, skb->len),
998 skb->len);
Larry Finger47103032007-01-21 22:27:35 -0600999 dev_kfree_skb_any(skb);
1000 skb = bounce_skb;
1001 }
John W. Linvillef2223132006-01-23 16:59:58 -05001002
1003 meta->skb = skb;
Michael Buesch9218e022006-08-16 00:25:16 +02001004 meta->dmaaddr = dmaaddr;
John W. Linvillef2223132006-01-23 16:59:58 -05001005
Michael Buesch9218e022006-08-16 00:25:16 +02001006 fill_descriptor(ring, desc, dmaaddr,
1007 skb->len, 1, 1, 1);
John W. Linvillef2223132006-01-23 16:59:58 -05001008
John W. Linvillef2223132006-01-23 16:59:58 -05001009 /* Now transfer the whole frame. */
1010 dmacontroller_poke_tx(ring, slot);
John W. Linvillef2223132006-01-23 16:59:58 -05001011}
1012
Michael Bueschea72ab22006-01-27 17:26:20 +01001013int bcm43xx_dma_tx(struct bcm43xx_private *bcm,
1014 struct ieee80211_txb *txb)
John W. Linvillef2223132006-01-23 16:59:58 -05001015{
1016 /* We just received a packet from the kernel network subsystem.
1017 * Add headers and DMA map the memory. Poke
1018 * the device to send the stuff.
1019 * Note that this is called from atomic context.
1020 */
Michael Buesche9357c02006-03-13 19:27:34 +01001021 struct bcm43xx_dmaring *ring = bcm43xx_current_dma(bcm)->tx_ring1;
John W. Linvillef2223132006-01-23 16:59:58 -05001022 u8 i;
1023 struct sk_buff *skb;
1024
1025 assert(ring->tx);
1026 if (unlikely(free_slots(ring) < txb->nr_frags)) {
1027 /* The queue should be stopped,
1028 * if we are low on free slots.
1029 * If this ever triggers, we have to lower the suspend_mark.
1030 */
1031 dprintkl(KERN_ERR PFX "Out of DMA descriptor slots!\n");
1032 return -ENOMEM;
1033 }
1034
John W. Linvillef2223132006-01-23 16:59:58 -05001035 for (i = 0; i < txb->nr_frags; i++) {
1036 skb = txb->fragments[i];
Pete Zaitcev512a8092006-03-07 01:37:51 +01001037 /* Take skb from ieee80211_txb_free */
1038 txb->fragments[i] = NULL;
1039 dma_tx_fragment(ring, skb, i);
John W. Linvillef2223132006-01-23 16:59:58 -05001040 }
Pete Zaitcev512a8092006-03-07 01:37:51 +01001041 ieee80211_txb_free(txb);
John W. Linvillef2223132006-01-23 16:59:58 -05001042
1043 return 0;
1044}
1045
Michael Bueschea72ab22006-01-27 17:26:20 +01001046void bcm43xx_dma_handle_xmitstatus(struct bcm43xx_private *bcm,
1047 struct bcm43xx_xmitstatus *status)
John W. Linvillef2223132006-01-23 16:59:58 -05001048{
1049 struct bcm43xx_dmaring *ring;
Michael Buesch9218e022006-08-16 00:25:16 +02001050 struct bcm43xx_dmadesc_generic *desc;
John W. Linvillef2223132006-01-23 16:59:58 -05001051 struct bcm43xx_dmadesc_meta *meta;
1052 int is_last_fragment;
1053 int slot;
Michael Buesch9218e022006-08-16 00:25:16 +02001054 u32 tmp;
John W. Linvillef2223132006-01-23 16:59:58 -05001055
1056 ring = parse_cookie(bcm, status->cookie, &slot);
1057 assert(ring);
1058 assert(ring->tx);
John W. Linvillef2223132006-01-23 16:59:58 -05001059 while (1) {
1060 assert(slot >= 0 && slot < ring->nr_slots);
Michael Buesch9218e022006-08-16 00:25:16 +02001061 desc = bcm43xx_dma_idx2desc(ring, slot, &meta);
John W. Linvillef2223132006-01-23 16:59:58 -05001062
Michael Buesch9218e022006-08-16 00:25:16 +02001063 if (ring->dma64) {
1064 tmp = le32_to_cpu(desc->dma64.control0);
1065 is_last_fragment = !!(tmp & BCM43xx_DMA64_DCTL0_FRAMEEND);
1066 } else {
1067 tmp = le32_to_cpu(desc->dma32.control);
1068 is_last_fragment = !!(tmp & BCM43xx_DMA32_DCTL_FRAMEEND);
1069 }
John W. Linvillef2223132006-01-23 16:59:58 -05001070 unmap_descbuffer(ring, meta->dmaaddr, meta->skb->len, 1);
Michael Buesch9218e022006-08-16 00:25:16 +02001071 free_descriptor_buffer(ring, meta, 1);
John W. Linvillef2223132006-01-23 16:59:58 -05001072 /* Everything belonging to the slot is unmapped
1073 * and freed, so we can return it.
1074 */
1075 return_slot(ring, slot);
1076
1077 if (is_last_fragment)
1078 break;
1079 slot = next_slot(ring, slot);
1080 }
1081 bcm->stats.last_tx = jiffies;
John W. Linvillef2223132006-01-23 16:59:58 -05001082}
1083
Michael Bueschea72ab22006-01-27 17:26:20 +01001084static void dma_rx(struct bcm43xx_dmaring *ring,
1085 int *slot)
John W. Linvillef2223132006-01-23 16:59:58 -05001086{
Michael Buesch9218e022006-08-16 00:25:16 +02001087 struct bcm43xx_dmadesc_generic *desc;
John W. Linvillef2223132006-01-23 16:59:58 -05001088 struct bcm43xx_dmadesc_meta *meta;
1089 struct bcm43xx_rxhdr *rxhdr;
1090 struct sk_buff *skb;
1091 u16 len;
1092 int err;
1093 dma_addr_t dmaaddr;
1094
Michael Buesch9218e022006-08-16 00:25:16 +02001095 desc = bcm43xx_dma_idx2desc(ring, *slot, &meta);
John W. Linvillef2223132006-01-23 16:59:58 -05001096
1097 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1098 skb = meta->skb;
1099
Michael Buesch9218e022006-08-16 00:25:16 +02001100 if (ring->index == 3) {
John W. Linvillef2223132006-01-23 16:59:58 -05001101 /* We received an xmit status. */
1102 struct bcm43xx_hwxmitstatus *hw = (struct bcm43xx_hwxmitstatus *)skb->data;
1103 struct bcm43xx_xmitstatus stat;
Michael Bueschea9a7712006-06-04 02:20:42 +02001104 int i = 0;
John W. Linvillef2223132006-01-23 16:59:58 -05001105
1106 stat.cookie = le16_to_cpu(hw->cookie);
Michael Bueschea9a7712006-06-04 02:20:42 +02001107 while (stat.cookie == 0) {
1108 if (unlikely(++i >= 10000)) {
1109 assert(0);
1110 break;
1111 }
1112 udelay(2);
1113 barrier();
1114 stat.cookie = le16_to_cpu(hw->cookie);
1115 }
John W. Linvillef2223132006-01-23 16:59:58 -05001116 stat.flags = hw->flags;
1117 stat.cnt1 = hw->cnt1;
1118 stat.cnt2 = hw->cnt2;
1119 stat.seq = le16_to_cpu(hw->seq);
1120 stat.unknown = le16_to_cpu(hw->unknown);
1121
1122 bcm43xx_debugfs_log_txstat(ring->bcm, &stat);
1123 bcm43xx_dma_handle_xmitstatus(ring->bcm, &stat);
1124 /* recycle the descriptor buffer. */
1125 sync_descbuffer_for_device(ring, meta->dmaaddr, ring->rx_buffersize);
1126
1127 return;
1128 }
1129 rxhdr = (struct bcm43xx_rxhdr *)skb->data;
1130 len = le16_to_cpu(rxhdr->frame_length);
1131 if (len == 0) {
1132 int i = 0;
1133
1134 do {
1135 udelay(2);
1136 barrier();
1137 len = le16_to_cpu(rxhdr->frame_length);
1138 } while (len == 0 && i++ < 5);
Michael Bueschea72ab22006-01-27 17:26:20 +01001139 if (unlikely(len == 0)) {
1140 /* recycle the descriptor buffer. */
1141 sync_descbuffer_for_device(ring, meta->dmaaddr,
1142 ring->rx_buffersize);
John W. Linvillef2223132006-01-23 16:59:58 -05001143 goto drop;
Michael Bueschea72ab22006-01-27 17:26:20 +01001144 }
John W. Linvillef2223132006-01-23 16:59:58 -05001145 }
1146 if (unlikely(len > ring->rx_buffersize)) {
1147 /* The data did not fit into one descriptor buffer
1148 * and is split over multiple buffers.
1149 * This should never happen, as we try to allocate buffers
1150 * big enough. So simply ignore this packet.
1151 */
Michael Bueschea72ab22006-01-27 17:26:20 +01001152 int cnt = 0;
1153 s32 tmp = len;
John W. Linvillef2223132006-01-23 16:59:58 -05001154
Michael Bueschea72ab22006-01-27 17:26:20 +01001155 while (1) {
Michael Buesch9218e022006-08-16 00:25:16 +02001156 desc = bcm43xx_dma_idx2desc(ring, *slot, &meta);
Michael Bueschea72ab22006-01-27 17:26:20 +01001157 /* recycle the descriptor buffer. */
1158 sync_descbuffer_for_device(ring, meta->dmaaddr,
1159 ring->rx_buffersize);
John W. Linvillef2223132006-01-23 16:59:58 -05001160 *slot = next_slot(ring, *slot);
1161 cnt++;
Michael Bueschea72ab22006-01-27 17:26:20 +01001162 tmp -= ring->rx_buffersize;
1163 if (tmp <= 0)
1164 break;
John W. Linvillef2223132006-01-23 16:59:58 -05001165 }
Michael Bueschea72ab22006-01-27 17:26:20 +01001166 printkl(KERN_ERR PFX "DMA RX buffer too small "
Michael Buesch9218e022006-08-16 00:25:16 +02001167 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1168 len, ring->rx_buffersize, cnt);
John W. Linvillef2223132006-01-23 16:59:58 -05001169 goto drop;
1170 }
1171 len -= IEEE80211_FCS_LEN;
1172
1173 dmaaddr = meta->dmaaddr;
1174 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1175 if (unlikely(err)) {
1176 dprintkl(KERN_ERR PFX "DMA RX: setup_rx_descbuffer() failed\n");
Michael Bueschea72ab22006-01-27 17:26:20 +01001177 sync_descbuffer_for_device(ring, dmaaddr,
1178 ring->rx_buffersize);
John W. Linvillef2223132006-01-23 16:59:58 -05001179 goto drop;
1180 }
1181
1182 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1183 skb_put(skb, len + ring->frameoffset);
1184 skb_pull(skb, ring->frameoffset);
1185
1186 err = bcm43xx_rx(ring->bcm, skb, rxhdr);
1187 if (err) {
1188 dev_kfree_skb_irq(skb);
1189 goto drop;
1190 }
1191
1192drop:
1193 return;
1194}
1195
Michael Bueschea72ab22006-01-27 17:26:20 +01001196void bcm43xx_dma_rx(struct bcm43xx_dmaring *ring)
John W. Linvillef2223132006-01-23 16:59:58 -05001197{
1198 u32 status;
1199 u16 descptr;
1200 int slot, current_slot;
1201#ifdef CONFIG_BCM43XX_DEBUG
1202 int used_slots = 0;
1203#endif
1204
1205 assert(!ring->tx);
Michael Buesch9218e022006-08-16 00:25:16 +02001206 if (ring->dma64) {
1207 status = bcm43xx_dma_read(ring, BCM43xx_DMA64_RXSTATUS);
1208 descptr = (status & BCM43xx_DMA64_RXSTATDPTR);
1209 current_slot = descptr / sizeof(struct bcm43xx_dmadesc64);
1210 } else {
1211 status = bcm43xx_dma_read(ring, BCM43xx_DMA32_RXSTATUS);
1212 descptr = (status & BCM43xx_DMA32_RXDPTR);
1213 current_slot = descptr / sizeof(struct bcm43xx_dmadesc32);
1214 }
John W. Linvillef2223132006-01-23 16:59:58 -05001215 assert(current_slot >= 0 && current_slot < ring->nr_slots);
1216
1217 slot = ring->current_slot;
1218 for ( ; slot != current_slot; slot = next_slot(ring, slot)) {
1219 dma_rx(ring, &slot);
1220#ifdef CONFIG_BCM43XX_DEBUG
1221 if (++used_slots > ring->max_used_slots)
1222 ring->max_used_slots = used_slots;
1223#endif
1224 }
Michael Buesch9218e022006-08-16 00:25:16 +02001225 if (ring->dma64) {
1226 bcm43xx_dma_write(ring, BCM43xx_DMA64_RXINDEX,
1227 (u32)(slot * sizeof(struct bcm43xx_dmadesc64)));
1228 } else {
1229 bcm43xx_dma_write(ring, BCM43xx_DMA32_RXINDEX,
1230 (u32)(slot * sizeof(struct bcm43xx_dmadesc32)));
1231 }
John W. Linvillef2223132006-01-23 16:59:58 -05001232 ring->current_slot = slot;
John W. Linvillef2223132006-01-23 16:59:58 -05001233}
1234
Michael Bueschaae37782006-03-13 15:54:56 +01001235void bcm43xx_dma_tx_suspend(struct bcm43xx_dmaring *ring)
1236{
1237 assert(ring->tx);
1238 bcm43xx_power_saving_ctl_bits(ring->bcm, -1, 1);
Michael Buesch9218e022006-08-16 00:25:16 +02001239 if (ring->dma64) {
1240 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXCTL,
1241 bcm43xx_dma_read(ring, BCM43xx_DMA64_TXCTL)
1242 | BCM43xx_DMA64_TXSUSPEND);
1243 } else {
1244 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXCTL,
1245 bcm43xx_dma_read(ring, BCM43xx_DMA32_TXCTL)
1246 | BCM43xx_DMA32_TXSUSPEND);
1247 }
Michael Bueschaae37782006-03-13 15:54:56 +01001248}
1249
1250void bcm43xx_dma_tx_resume(struct bcm43xx_dmaring *ring)
1251{
1252 assert(ring->tx);
Michael Buesch9218e022006-08-16 00:25:16 +02001253 if (ring->dma64) {
1254 bcm43xx_dma_write(ring, BCM43xx_DMA64_TXCTL,
1255 bcm43xx_dma_read(ring, BCM43xx_DMA64_TXCTL)
1256 & ~BCM43xx_DMA64_TXSUSPEND);
1257 } else {
1258 bcm43xx_dma_write(ring, BCM43xx_DMA32_TXCTL,
1259 bcm43xx_dma_read(ring, BCM43xx_DMA32_TXCTL)
1260 & ~BCM43xx_DMA32_TXSUSPEND);
1261 }
Michael Bueschaae37782006-03-13 15:54:56 +01001262 bcm43xx_power_saving_ctl_bits(ring->bcm, -1, -1);
1263}