staging: brcm80211: removed log after kzalloc()/kmalloc() failure
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / dma.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/delay.h>
19 #include <linux/pci.h>
20
21 #include <brcmu_utils.h>
22 #include <aiutils.h>
23 #include "types.h"
24 #include "dma.h"
25
26 /*
27  * DMA hardware requires each descriptor ring to be 8kB aligned, and fit within
28  * a contiguous 8kB physical address.
29  */
30 #define D64RINGALIGN_BITS       13
31 #define D64MAXRINGSZ            (1 << D64RINGALIGN_BITS)
32 #define D64RINGALIGN            (1 << D64RINGALIGN_BITS)
33
34 #define D64MAXDD        (D64MAXRINGSZ / sizeof(struct dma64desc))
35
36 /* transmit channel control */
37 #define D64_XC_XE               0x00000001      /* transmit enable */
38 #define D64_XC_SE               0x00000002      /* transmit suspend request */
39 #define D64_XC_LE               0x00000004      /* loopback enable */
40 #define D64_XC_FL               0x00000010      /* flush request */
41 #define D64_XC_PD               0x00000800      /* parity check disable */
42 #define D64_XC_AE               0x00030000      /* address extension bits */
43 #define D64_XC_AE_SHIFT         16
44
45 /* transmit descriptor table pointer */
46 #define D64_XP_LD_MASK          0x00000fff      /* last valid descriptor */
47
48 /* transmit channel status */
49 #define D64_XS0_CD_MASK         0x00001fff      /* current descriptor pointer */
50 #define D64_XS0_XS_MASK         0xf0000000      /* transmit state */
51 #define D64_XS0_XS_SHIFT                28
52 #define D64_XS0_XS_DISABLED     0x00000000      /* disabled */
53 #define D64_XS0_XS_ACTIVE       0x10000000      /* active */
54 #define D64_XS0_XS_IDLE         0x20000000      /* idle wait */
55 #define D64_XS0_XS_STOPPED      0x30000000      /* stopped */
56 #define D64_XS0_XS_SUSP         0x40000000      /* suspend pending */
57
58 #define D64_XS1_AD_MASK         0x00001fff      /* active descriptor */
59 #define D64_XS1_XE_MASK         0xf0000000      /* transmit errors */
60 #define D64_XS1_XE_SHIFT                28
61 #define D64_XS1_XE_NOERR        0x00000000      /* no error */
62 #define D64_XS1_XE_DPE          0x10000000      /* descriptor protocol error */
63 #define D64_XS1_XE_DFU          0x20000000      /* data fifo underrun */
64 #define D64_XS1_XE_DTE          0x30000000      /* data transfer error */
65 #define D64_XS1_XE_DESRE        0x40000000      /* descriptor read error */
66 #define D64_XS1_XE_COREE        0x50000000      /* core error */
67
68 /* receive channel control */
69 /* receive enable */
70 #define D64_RC_RE               0x00000001
71 /* receive frame offset */
72 #define D64_RC_RO_MASK          0x000000fe
73 #define D64_RC_RO_SHIFT         1
74 /* direct fifo receive (pio) mode */
75 #define D64_RC_FM               0x00000100
76 /* separate rx header descriptor enable */
77 #define D64_RC_SH               0x00000200
78 /* overflow continue */
79 #define D64_RC_OC               0x00000400
80 /* parity check disable */
81 #define D64_RC_PD               0x00000800
82 /* address extension bits */
83 #define D64_RC_AE               0x00030000
84 #define D64_RC_AE_SHIFT         16
85
86 /* flags for dma controller */
87 /* partity enable */
88 #define DMA_CTRL_PEN            (1 << 0)
89 /* rx overflow continue */
90 #define DMA_CTRL_ROC            (1 << 1)
91 /* allow rx scatter to multiple descriptors */
92 #define DMA_CTRL_RXMULTI        (1 << 2)
93 /* Unframed Rx/Tx data */
94 #define DMA_CTRL_UNFRAMED       (1 << 3)
95
96 /* receive descriptor table pointer */
97 #define D64_RP_LD_MASK          0x00000fff      /* last valid descriptor */
98
99 /* receive channel status */
100 #define D64_RS0_CD_MASK         0x00001fff      /* current descriptor pointer */
101 #define D64_RS0_RS_MASK         0xf0000000      /* receive state */
102 #define D64_RS0_RS_SHIFT                28
103 #define D64_RS0_RS_DISABLED     0x00000000      /* disabled */
104 #define D64_RS0_RS_ACTIVE       0x10000000      /* active */
105 #define D64_RS0_RS_IDLE         0x20000000      /* idle wait */
106 #define D64_RS0_RS_STOPPED      0x30000000      /* stopped */
107 #define D64_RS0_RS_SUSP         0x40000000      /* suspend pending */
108
109 #define D64_RS1_AD_MASK         0x0001ffff      /* active descriptor */
110 #define D64_RS1_RE_MASK         0xf0000000      /* receive errors */
111 #define D64_RS1_RE_SHIFT                28
112 #define D64_RS1_RE_NOERR        0x00000000      /* no error */
113 #define D64_RS1_RE_DPO          0x10000000      /* descriptor protocol error */
114 #define D64_RS1_RE_DFU          0x20000000      /* data fifo overflow */
115 #define D64_RS1_RE_DTE          0x30000000      /* data transfer error */
116 #define D64_RS1_RE_DESRE        0x40000000      /* descriptor read error */
117 #define D64_RS1_RE_COREE        0x50000000      /* core error */
118
119 /* fifoaddr */
120 #define D64_FA_OFF_MASK         0xffff  /* offset */
121 #define D64_FA_SEL_MASK         0xf0000 /* select */
122 #define D64_FA_SEL_SHIFT        16
123 #define D64_FA_SEL_XDD          0x00000 /* transmit dma data */
124 #define D64_FA_SEL_XDP          0x10000 /* transmit dma pointers */
125 #define D64_FA_SEL_RDD          0x40000 /* receive dma data */
126 #define D64_FA_SEL_RDP          0x50000 /* receive dma pointers */
127 #define D64_FA_SEL_XFD          0x80000 /* transmit fifo data */
128 #define D64_FA_SEL_XFP          0x90000 /* transmit fifo pointers */
129 #define D64_FA_SEL_RFD          0xc0000 /* receive fifo data */
130 #define D64_FA_SEL_RFP          0xd0000 /* receive fifo pointers */
131 #define D64_FA_SEL_RSD          0xe0000 /* receive frame status data */
132 #define D64_FA_SEL_RSP          0xf0000 /* receive frame status pointers */
133
134 /* descriptor control flags 1 */
135 #define D64_CTRL_COREFLAGS      0x0ff00000      /* core specific flags */
136 #define D64_CTRL1_EOT           ((u32)1 << 28)  /* end of descriptor table */
137 #define D64_CTRL1_IOC           ((u32)1 << 29)  /* interrupt on completion */
138 #define D64_CTRL1_EOF           ((u32)1 << 30)  /* end of frame */
139 #define D64_CTRL1_SOF           ((u32)1 << 31)  /* start of frame */
140
141 /* descriptor control flags 2 */
142 /* buffer byte count. real data len must <= 16KB */
143 #define D64_CTRL2_BC_MASK       0x00007fff
144 /* address extension bits */
145 #define D64_CTRL2_AE            0x00030000
146 #define D64_CTRL2_AE_SHIFT      16
147 /* parity bit */
148 #define D64_CTRL2_PARITY        0x00040000
149
150 /* control flags in the range [27:20] are core-specific and not defined here */
151 #define D64_CTRL_CORE_MASK      0x0ff00000
152
153 #define D64_RX_FRM_STS_LEN      0x0000ffff      /* frame length mask */
154 #define D64_RX_FRM_STS_OVFL     0x00800000      /* RxOverFlow */
155 #define D64_RX_FRM_STS_DSCRCNT  0x0f000000  /* no. of descriptors used - 1 */
156 #define D64_RX_FRM_STS_DATATYPE 0xf0000000      /* core-dependent data type */
157
158 /*
159  * packet headroom necessary to accommodate the largest header
160  * in the system, (i.e TXOFF). By doing, we avoid the need to
161  * allocate an extra buffer for the header when bridging to WL.
162  * There is a compile time check in wlc.c which ensure that this
163  * value is at least as big as TXOFF. This value is used in
164  * dma_rxfill().
165  */
166
167 #define BCMEXTRAHDROOM 172
168
169 /* debug/trace */
170 #ifdef BCMDBG
171 #define DMA_ERROR(args) \
172         do { \
173                 if (!(*di->msg_level & 1)) \
174                         ; \
175                 else \
176                         printk args; \
177         } while (0)
178 #define DMA_TRACE(args) \
179         do { \
180                 if (!(*di->msg_level & 2)) \
181                         ; \
182                 else \
183                         printk args; \
184         } while (0)
185 #else
186 #define DMA_ERROR(args)
187 #define DMA_TRACE(args)
188 #endif                          /* BCMDBG */
189
190 #define DMA_NONE(args)
191
192 #define MAXNAMEL        8       /* 8 char names */
193
194 /* macros to convert between byte offsets and indexes */
195 #define B2I(bytes, type)        ((bytes) / sizeof(type))
196 #define I2B(index, type)        ((index) * sizeof(type))
197
198 #define PCI32ADDR_HIGH          0xc0000000      /* address[31:30] */
199 #define PCI32ADDR_HIGH_SHIFT    30      /* address[31:30] */
200
201 #define PCI64ADDR_HIGH          0x80000000      /* address[63] */
202 #define PCI64ADDR_HIGH_SHIFT    31      /* address[63] */
203
204 /*
205  * DMA Descriptor
206  * Descriptors are only read by the hardware, never written back.
207  */
208 struct dma64desc {
209         u32 ctrl1;      /* misc control bits & bufcount */
210         u32 ctrl2;      /* buffer count and address extension */
211         u32 addrlow;    /* memory address of the date buffer, bits 31:0 */
212         u32 addrhigh;   /* memory address of the date buffer, bits 63:32 */
213 };
214
215 /* dma engine software state */
216 struct dma_info {
217         struct dma_pub dma; /* exported structure */
218         uint *msg_level;        /* message level pointer */
219         char name[MAXNAMEL];    /* callers name for diag msgs */
220
221         struct pci_dev *pbus;           /* bus handle */
222
223         bool dma64;     /* this dma engine is operating in 64-bit mode */
224         bool addrext;   /* this dma engine supports DmaExtendedAddrChanges */
225
226         /* 64-bit dma tx engine registers */
227         struct dma64regs *d64txregs;
228         /* 64-bit dma rx engine registers */
229         struct dma64regs *d64rxregs;
230         /* pointer to dma64 tx descriptor ring */
231         struct dma64desc *txd64;
232         /* pointer to dma64 rx descriptor ring */
233         struct dma64desc *rxd64;
234
235         u16 dmadesc_align;      /* alignment requirement for dma descriptors */
236
237         u16 ntxd;               /* # tx descriptors tunable */
238         u16 txin;               /* index of next descriptor to reclaim */
239         u16 txout;              /* index of next descriptor to post */
240         /* pointer to parallel array of pointers to packets */
241         struct sk_buff **txp;
242         /* Aligned physical address of descriptor ring */
243         dma_addr_t txdpa;
244         /* Original physical address of descriptor ring */
245         dma_addr_t txdpaorig;
246         u16 txdalign;   /* #bytes added to alloc'd mem to align txd */
247         u32 txdalloc;   /* #bytes allocated for the ring */
248         u32 xmtptrbase; /* When using unaligned descriptors, the ptr register
249                          * is not just an index, it needs all 13 bits to be
250                          * an offset from the addr register.
251                          */
252
253         u16 nrxd;       /* # rx descriptors tunable */
254         u16 rxin;       /* index of next descriptor to reclaim */
255         u16 rxout;      /* index of next descriptor to post */
256         /* pointer to parallel array of pointers to packets */
257         struct sk_buff **rxp;
258         /* Aligned physical address of descriptor ring */
259         dma_addr_t rxdpa;
260         /* Original physical address of descriptor ring */
261         dma_addr_t rxdpaorig;
262         u16 rxdalign;   /* #bytes added to alloc'd mem to align rxd */
263         u32 rxdalloc;   /* #bytes allocated for the ring */
264         u32 rcvptrbase; /* Base for ptr reg when using unaligned descriptors */
265
266         /* tunables */
267         unsigned int rxbufsize; /* rx buffer size in bytes, not including
268                                  * the extra headroom
269                                  */
270         uint rxextrahdrroom;    /* extra rx headroom, reverseved to assist upper
271                                  * stack, e.g. some rx pkt buffers will be
272                                  * bridged to tx side without byte copying.
273                                  * The extra headroom needs to be large enough
274                                  * to fit txheader needs. Some dongle driver may
275                                  * not need it.
276                                  */
277         uint nrxpost;           /* # rx buffers to keep posted */
278         unsigned int rxoffset;  /* rxcontrol offset */
279         /* add to get dma address of descriptor ring, low 32 bits */
280         uint ddoffsetlow;
281         /*   high 32 bits */
282         uint ddoffsethigh;
283         /* add to get dma address of data buffer, low 32 bits */
284         uint dataoffsetlow;
285         /*   high 32 bits */
286         uint dataoffsethigh;
287         /* descriptor base need to be aligned or not */
288         bool aligndesc_4k;
289 };
290
291 /*
292  * default dma message level (if input msg_level
293  * pointer is null in dma_attach())
294  */
295 static uint dma_msg_level;
296
297 /* Check for odd number of 1's */
298 static u32 parity32(u32 data)
299 {
300         data ^= data >> 16;
301         data ^= data >> 8;
302         data ^= data >> 4;
303         data ^= data >> 2;
304         data ^= data >> 1;
305
306         return data & 1;
307 }
308
309 static bool dma64_dd_parity(struct dma64desc *dd)
310 {
311         return parity32(dd->addrlow ^ dd->addrhigh ^ dd->ctrl1 ^ dd->ctrl2);
312 }
313
314 /* descriptor bumping functions */
315
316 static uint xxd(uint x, uint n)
317 {
318         return x & (n - 1); /* faster than %, but n must be power of 2 */
319 }
320
321 static uint txd(struct dma_info *di, uint x)
322 {
323         return xxd(x, di->ntxd);
324 }
325
326 static uint rxd(struct dma_info *di, uint x)
327 {
328         return xxd(x, di->nrxd);
329 }
330
331 static uint nexttxd(struct dma_info *di, uint i)
332 {
333         return txd(di, i + 1);
334 }
335
336 static uint prevtxd(struct dma_info *di, uint i)
337 {
338         return txd(di, i - 1);
339 }
340
341 static uint nextrxd(struct dma_info *di, uint i)
342 {
343         return txd(di, i + 1);
344 }
345
346 static uint ntxdactive(struct dma_info *di, uint h, uint t)
347 {
348         return txd(di, t-h);
349 }
350
351 static uint nrxdactive(struct dma_info *di, uint h, uint t)
352 {
353         return rxd(di, t-h);
354 }
355
356 static uint _dma_ctrlflags(struct dma_info *di, uint mask, uint flags)
357 {
358         uint dmactrlflags = di->dma.dmactrlflags;
359
360         if (di == NULL) {
361                 DMA_ERROR(("%s: _dma_ctrlflags: NULL dma handle\n", di->name));
362                 return 0;
363         }
364
365         dmactrlflags &= ~mask;
366         dmactrlflags |= flags;
367
368         /* If trying to enable parity, check if parity is actually supported */
369         if (dmactrlflags & DMA_CTRL_PEN) {
370                 u32 control;
371
372                 control = R_REG(&di->d64txregs->control);
373                 W_REG(&di->d64txregs->control,
374                       control | D64_XC_PD);
375                 if (R_REG(&di->d64txregs->control) & D64_XC_PD)
376                         /* We *can* disable it so it is supported,
377                          * restore control register
378                          */
379                         W_REG(&di->d64txregs->control,
380                         control);
381                 else
382                         /* Not supported, don't allow it to be enabled */
383                         dmactrlflags &= ~DMA_CTRL_PEN;
384         }
385
386         di->dma.dmactrlflags = dmactrlflags;
387
388         return dmactrlflags;
389 }
390
391 static bool _dma64_addrext(struct dma64regs *dma64regs)
392 {
393         u32 w;
394         OR_REG(&dma64regs->control, D64_XC_AE);
395         w = R_REG(&dma64regs->control);
396         AND_REG(&dma64regs->control, ~D64_XC_AE);
397         return (w & D64_XC_AE) == D64_XC_AE;
398 }
399
400 /*
401  * return true if this dma engine supports DmaExtendedAddrChanges,
402  * otherwise false
403  */
404 static bool _dma_isaddrext(struct dma_info *di)
405 {
406         /* DMA64 supports full 32- or 64-bit operation. AE is always valid */
407
408         /* not all tx or rx channel are available */
409         if (di->d64txregs != NULL) {
410                 if (!_dma64_addrext(di->d64txregs))
411                         DMA_ERROR(("%s: _dma_isaddrext: DMA64 tx doesn't have "
412                                    "AE set\n", di->name));
413                 return true;
414         } else if (di->d64rxregs != NULL) {
415                 if (!_dma64_addrext(di->d64rxregs))
416                         DMA_ERROR(("%s: _dma_isaddrext: DMA64 rx doesn't have "
417                                    "AE set\n", di->name));
418                 return true;
419         }
420
421         return false;
422 }
423
424 static bool _dma_descriptor_align(struct dma_info *di)
425 {
426         u32 addrl;
427
428         /* Check to see if the descriptors need to be aligned on 4K/8K or not */
429         if (di->d64txregs != NULL) {
430                 W_REG(&di->d64txregs->addrlow, 0xff0);
431                 addrl = R_REG(&di->d64txregs->addrlow);
432                 if (addrl != 0)
433                         return false;
434         } else if (di->d64rxregs != NULL) {
435                 W_REG(&di->d64rxregs->addrlow, 0xff0);
436                 addrl = R_REG(&di->d64rxregs->addrlow);
437                 if (addrl != 0)
438                         return false;
439         }
440         return true;
441 }
442
443 /*
444  * Descriptor table must start at the DMA hardware dictated alignment, so
445  * allocated memory must be large enough to support this requirement.
446  */
447 static void *dma_alloc_consistent(struct pci_dev *pdev, uint size,
448                                   u16 align_bits, uint *alloced,
449                                   dma_addr_t *pap)
450 {
451         if (align_bits) {
452                 u16 align = (1 << align_bits);
453                 if (!IS_ALIGNED(PAGE_SIZE, align))
454                         size += align;
455                 *alloced = size;
456         }
457         return pci_alloc_consistent(pdev, size, pap);
458 }
459
460 static
461 u8 dma_align_sizetobits(uint size)
462 {
463         u8 bitpos = 0;
464         while (size >>= 1)
465                 bitpos++;
466         return bitpos;
467 }
468
469 /* This function ensures that the DMA descriptor ring will not get allocated
470  * across Page boundary. If the allocation is done across the page boundary
471  * at the first time, then it is freed and the allocation is done at
472  * descriptor ring size aligned location. This will ensure that the ring will
473  * not cross page boundary
474  */
475 static void *dma_ringalloc(struct dma_info *di, u32 boundary, uint size,
476                            u16 *alignbits, uint *alloced,
477                            dma_addr_t *descpa)
478 {
479         void *va;
480         u32 desc_strtaddr;
481         u32 alignbytes = 1 << *alignbits;
482
483         va = dma_alloc_consistent(di->pbus, size, *alignbits, alloced, descpa);
484
485         if (NULL == va)
486                 return NULL;
487
488         desc_strtaddr = (u32) roundup((unsigned long)va, alignbytes);
489         if (((desc_strtaddr + size - 1) & boundary) != (desc_strtaddr
490                                                         & boundary)) {
491                 *alignbits = dma_align_sizetobits(size);
492                 pci_free_consistent(di->pbus, size, va, *descpa);
493                 va = dma_alloc_consistent(di->pbus, size, *alignbits,
494                         alloced, descpa);
495         }
496         return va;
497 }
498
499 static bool dma64_alloc(struct dma_info *di, uint direction)
500 {
501         u16 size;
502         uint ddlen;
503         void *va;
504         uint alloced = 0;
505         u16 align;
506         u16 align_bits;
507
508         ddlen = sizeof(struct dma64desc);
509
510         size = (direction == DMA_TX) ? (di->ntxd * ddlen) : (di->nrxd * ddlen);
511         align_bits = di->dmadesc_align;
512         align = (1 << align_bits);
513
514         if (direction == DMA_TX) {
515                 va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
516                         &alloced, &di->txdpaorig);
517                 if (va == NULL) {
518                         DMA_ERROR(("%s: dma64_alloc: DMA_ALLOC_CONSISTENT(ntxd)"
519                                    " failed\n", di->name));
520                         return false;
521                 }
522                 align = (1 << align_bits);
523                 di->txd64 = (struct dma64desc *)
524                                         roundup((unsigned long)va, align);
525                 di->txdalign = (uint) ((s8 *)di->txd64 - (s8 *) va);
526                 di->txdpa = di->txdpaorig + di->txdalign;
527                 di->txdalloc = alloced;
528         } else {
529                 va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
530                         &alloced, &di->rxdpaorig);
531                 if (va == NULL) {
532                         DMA_ERROR(("%s: dma64_alloc: DMA_ALLOC_CONSISTENT(nrxd)"
533                                    " failed\n", di->name));
534                         return false;
535                 }
536                 align = (1 << align_bits);
537                 di->rxd64 = (struct dma64desc *)
538                                         roundup((unsigned long)va, align);
539                 di->rxdalign = (uint) ((s8 *)di->rxd64 - (s8 *) va);
540                 di->rxdpa = di->rxdpaorig + di->rxdalign;
541                 di->rxdalloc = alloced;
542         }
543
544         return true;
545 }
546
547 static bool _dma_alloc(struct dma_info *di, uint direction)
548 {
549         return dma64_alloc(di, direction);
550 }
551
552 struct dma_pub *dma_attach(char *name, struct si_pub *sih,
553                      void *dmaregstx, void *dmaregsrx, uint ntxd,
554                      uint nrxd, uint rxbufsize, int rxextheadroom,
555                      uint nrxpost, uint rxoffset, uint *msg_level)
556 {
557         struct dma_info *di;
558         uint size;
559
560         /* allocate private info structure */
561         di = kzalloc(sizeof(struct dma_info), GFP_ATOMIC);
562         if (di == NULL)
563                 return NULL;
564
565         di->msg_level = msg_level ? msg_level : &dma_msg_level;
566
567
568         di->dma64 = ((ai_core_sflags(sih, 0, 0) & SISF_DMA64) == SISF_DMA64);
569
570         /* init dma reg pointer */
571         di->d64txregs = (struct dma64regs *) dmaregstx;
572         di->d64rxregs = (struct dma64regs *) dmaregsrx;
573
574         /*
575          * Default flags (which can be changed by the driver calling
576          * dma_ctrlflags before enable): For backwards compatibility
577          * both Rx Overflow Continue and Parity are DISABLED.
578          */
579         _dma_ctrlflags(di, DMA_CTRL_ROC | DMA_CTRL_PEN, 0);
580
581         DMA_TRACE(("%s: dma_attach: %s flags 0x%x ntxd %d nrxd %d "
582                    "rxbufsize %d rxextheadroom %d nrxpost %d rxoffset %d "
583                    "dmaregstx %p dmaregsrx %p\n", name, "DMA64",
584                    di->dma.dmactrlflags, ntxd, nrxd, rxbufsize,
585                    rxextheadroom, nrxpost, rxoffset, dmaregstx, dmaregsrx));
586
587         /* make a private copy of our callers name */
588         strncpy(di->name, name, MAXNAMEL);
589         di->name[MAXNAMEL - 1] = '\0';
590
591         di->pbus = ((struct si_info *)sih)->pbus;
592
593         /* save tunables */
594         di->ntxd = (u16) ntxd;
595         di->nrxd = (u16) nrxd;
596
597         /* the actual dma size doesn't include the extra headroom */
598         di->rxextrahdrroom =
599             (rxextheadroom == -1) ? BCMEXTRAHDROOM : rxextheadroom;
600         if (rxbufsize > BCMEXTRAHDROOM)
601                 di->rxbufsize = (u16) (rxbufsize - di->rxextrahdrroom);
602         else
603                 di->rxbufsize = (u16) rxbufsize;
604
605         di->nrxpost = (u16) nrxpost;
606         di->rxoffset = (u8) rxoffset;
607
608         /*
609          * figure out the DMA physical address offset for dd and data
610          *     PCI/PCIE: they map silicon backplace address to zero
611          *     based memory, need offset
612          *     Other bus: use zero SI_BUS BIGENDIAN kludge: use sdram
613          *     swapped region for data buffer, not descriptor
614          */
615         di->ddoffsetlow = 0;
616         di->dataoffsetlow = 0;
617         /* add offset for pcie with DMA64 bus */
618         di->ddoffsetlow = 0;
619         di->ddoffsethigh = SI_PCIE_DMA_H32;
620         di->dataoffsetlow = di->ddoffsetlow;
621         di->dataoffsethigh = di->ddoffsethigh;
622         /* WAR64450 : DMACtl.Addr ext fields are not supported in SDIOD core. */
623         if ((ai_coreid(sih) == SDIOD_CORE_ID)
624             && ((ai_corerev(sih) > 0) && (ai_corerev(sih) <= 2)))
625                 di->addrext = 0;
626         else if ((ai_coreid(sih) == I2S_CORE_ID) &&
627                  ((ai_corerev(sih) == 0) || (ai_corerev(sih) == 1)))
628                 di->addrext = 0;
629         else
630                 di->addrext = _dma_isaddrext(di);
631
632         /* does the descriptor need to be aligned and if yes, on 4K/8K or not */
633         di->aligndesc_4k = _dma_descriptor_align(di);
634         if (di->aligndesc_4k) {
635                 di->dmadesc_align = D64RINGALIGN_BITS;
636                 if ((ntxd < D64MAXDD / 2) && (nrxd < D64MAXDD / 2))
637                         /* for smaller dd table, HW relax alignment reqmnt */
638                         di->dmadesc_align = D64RINGALIGN_BITS - 1;
639         } else {
640                 di->dmadesc_align = 4;  /* 16 byte alignment */
641         }
642
643         DMA_NONE(("DMA descriptor align_needed %d, align %d\n",
644                   di->aligndesc_4k, di->dmadesc_align));
645
646         /* allocate tx packet pointer vector */
647         if (ntxd) {
648                 size = ntxd * sizeof(void *);
649                 di->txp = kzalloc(size, GFP_ATOMIC);
650                 if (di->txp == NULL)
651                         goto fail;
652         }
653
654         /* allocate rx packet pointer vector */
655         if (nrxd) {
656                 size = nrxd * sizeof(void *);
657                 di->rxp = kzalloc(size, GFP_ATOMIC);
658                 if (di->rxp == NULL)
659                         goto fail;
660         }
661
662         /*
663          * allocate transmit descriptor ring, only need ntxd descriptors
664          * but it must be aligned
665          */
666         if (ntxd) {
667                 if (!_dma_alloc(di, DMA_TX))
668                         goto fail;
669         }
670
671         /*
672          * allocate receive descriptor ring, only need nrxd descriptors
673          * but it must be aligned
674          */
675         if (nrxd) {
676                 if (!_dma_alloc(di, DMA_RX))
677                         goto fail;
678         }
679
680         if ((di->ddoffsetlow != 0) && !di->addrext) {
681                 if (di->txdpa > SI_PCI_DMA_SZ) {
682                         DMA_ERROR(("%s: dma_attach: txdpa 0x%x: addrext not "
683                                    "supported\n", di->name, (u32)di->txdpa));
684                         goto fail;
685                 }
686                 if (di->rxdpa > SI_PCI_DMA_SZ) {
687                         DMA_ERROR(("%s: dma_attach: rxdpa 0x%x: addrext not "
688                                    "supported\n", di->name, (u32)di->rxdpa));
689                         goto fail;
690                 }
691         }
692
693         DMA_TRACE(("ddoffsetlow 0x%x ddoffsethigh 0x%x dataoffsetlow 0x%x "
694                    "dataoffsethigh " "0x%x addrext %d\n", di->ddoffsetlow,
695                    di->ddoffsethigh, di->dataoffsetlow, di->dataoffsethigh,
696                    di->addrext));
697
698         return (struct dma_pub *) di;
699
700  fail:
701         dma_detach((struct dma_pub *)di);
702         return NULL;
703 }
704
705 static inline void
706 dma64_dd_upd(struct dma_info *di, struct dma64desc *ddring,
707              dma_addr_t pa, uint outidx, u32 *flags, u32 bufcount)
708 {
709         u32 ctrl2 = bufcount & D64_CTRL2_BC_MASK;
710
711         /* PCI bus with big(>1G) physical address, use address extension */
712         if ((di->dataoffsetlow == 0) || !(pa & PCI32ADDR_HIGH)) {
713                 ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
714                 ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
715                 ddring[outidx].ctrl1 = cpu_to_le32(*flags);
716                 ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
717         } else {
718                 /* address extension for 32-bit PCI */
719                 u32 ae;
720
721                 ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
722                 pa &= ~PCI32ADDR_HIGH;
723
724                 ctrl2 |= (ae << D64_CTRL2_AE_SHIFT) & D64_CTRL2_AE;
725                 ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
726                 ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
727                 ddring[outidx].ctrl1 = cpu_to_le32(*flags);
728                 ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
729         }
730         if (di->dma.dmactrlflags & DMA_CTRL_PEN) {
731                 if (dma64_dd_parity(&ddring[outidx]))
732                         ddring[outidx].ctrl2 =
733                              cpu_to_le32(ctrl2 | D64_CTRL2_PARITY);
734         }
735 }
736
737 /* !! may be called with core in reset */
738 void dma_detach(struct dma_pub *pub)
739 {
740         struct dma_info *di = (struct dma_info *)pub;
741
742         DMA_TRACE(("%s: dma_detach\n", di->name));
743
744         /* free dma descriptor rings */
745         if (di->txd64)
746                 pci_free_consistent(di->pbus, di->txdalloc,
747                                     ((s8 *)di->txd64 - di->txdalign),
748                                     (di->txdpaorig));
749         if (di->rxd64)
750                 pci_free_consistent(di->pbus, di->rxdalloc,
751                                     ((s8 *)di->rxd64 - di->rxdalign),
752                                     (di->rxdpaorig));
753
754         /* free packet pointer vectors */
755         kfree(di->txp);
756         kfree(di->rxp);
757
758         /* free our private info structure */
759         kfree(di);
760
761 }
762
763 /* initialize descriptor table base address */
764 static void
765 _dma_ddtable_init(struct dma_info *di, uint direction, dma_addr_t pa)
766 {
767         if (!di->aligndesc_4k) {
768                 if (direction == DMA_TX)
769                         di->xmtptrbase = pa;
770                 else
771                         di->rcvptrbase = pa;
772         }
773
774         if ((di->ddoffsetlow == 0)
775             || !(pa & PCI32ADDR_HIGH)) {
776                 if (direction == DMA_TX) {
777                         W_REG(&di->d64txregs->addrlow, pa + di->ddoffsetlow);
778                         W_REG(&di->d64txregs->addrhigh, di->ddoffsethigh);
779                 } else {
780                         W_REG(&di->d64rxregs->addrlow, pa + di->ddoffsetlow);
781                         W_REG(&di->d64rxregs->addrhigh, di->ddoffsethigh);
782                 }
783         } else {
784                 /* DMA64 32bits address extension */
785                 u32 ae;
786
787                 /* shift the high bit(s) from pa to ae */
788                 ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
789                 pa &= ~PCI32ADDR_HIGH;
790
791                 if (direction == DMA_TX) {
792                         W_REG(&di->d64txregs->addrlow, pa + di->ddoffsetlow);
793                         W_REG(&di->d64txregs->addrhigh, di->ddoffsethigh);
794                         SET_REG(&di->d64txregs->control,
795                                 D64_XC_AE, (ae << D64_XC_AE_SHIFT));
796                 } else {
797                         W_REG(&di->d64rxregs->addrlow, pa + di->ddoffsetlow);
798                         W_REG(&di->d64rxregs->addrhigh, di->ddoffsethigh);
799                         SET_REG(&di->d64rxregs->control,
800                                 D64_RC_AE, (ae << D64_RC_AE_SHIFT));
801                 }
802         }
803 }
804
805 static void _dma_rxenable(struct dma_info *di)
806 {
807         uint dmactrlflags = di->dma.dmactrlflags;
808         u32 control;
809
810         DMA_TRACE(("%s: dma_rxenable\n", di->name));
811
812         control =
813             (R_REG(&di->d64rxregs->control) & D64_RC_AE) |
814             D64_RC_RE;
815
816         if ((dmactrlflags & DMA_CTRL_PEN) == 0)
817                 control |= D64_RC_PD;
818
819         if (dmactrlflags & DMA_CTRL_ROC)
820                 control |= D64_RC_OC;
821
822         W_REG(&di->d64rxregs->control,
823                 ((di->rxoffset << D64_RC_RO_SHIFT) | control));
824 }
825
826 void dma_rxinit(struct dma_pub *pub)
827 {
828         struct dma_info *di = (struct dma_info *)pub;
829
830         DMA_TRACE(("%s: dma_rxinit\n", di->name));
831
832         if (di->nrxd == 0)
833                 return;
834
835         di->rxin = di->rxout = 0;
836
837         /* clear rx descriptor ring */
838         memset(di->rxd64, '\0', di->nrxd * sizeof(struct dma64desc));
839
840         /* DMA engine with out alignment requirement requires table to be inited
841          * before enabling the engine
842          */
843         if (!di->aligndesc_4k)
844                 _dma_ddtable_init(di, DMA_RX, di->rxdpa);
845
846         _dma_rxenable(di);
847
848         if (di->aligndesc_4k)
849                 _dma_ddtable_init(di, DMA_RX, di->rxdpa);
850 }
851
852 static struct sk_buff *dma64_getnextrxp(struct dma_info *di, bool forceall)
853 {
854         uint i, curr;
855         struct sk_buff *rxp;
856         dma_addr_t pa;
857
858         i = di->rxin;
859
860         /* return if no packets posted */
861         if (i == di->rxout)
862                 return NULL;
863
864         curr =
865             B2I(((R_REG(&di->d64rxregs->status0) & D64_RS0_CD_MASK) -
866                  di->rcvptrbase) & D64_RS0_CD_MASK, struct dma64desc);
867
868         /* ignore curr if forceall */
869         if (!forceall && (i == curr))
870                 return NULL;
871
872         /* get the packet pointer that corresponds to the rx descriptor */
873         rxp = di->rxp[i];
874         di->rxp[i] = NULL;
875
876         pa = cpu_to_le32(di->rxd64[i].addrlow) - di->dataoffsetlow;
877
878         /* clear this packet from the descriptor ring */
879         pci_unmap_single(di->pbus, pa, di->rxbufsize, PCI_DMA_FROMDEVICE);
880
881         di->rxd64[i].addrlow = 0xdeadbeef;
882         di->rxd64[i].addrhigh = 0xdeadbeef;
883
884         di->rxin = nextrxd(di, i);
885
886         return rxp;
887 }
888
889 static struct sk_buff *_dma_getnextrxp(struct dma_info *di, bool forceall)
890 {
891         if (di->nrxd == 0)
892                 return NULL;
893
894         return dma64_getnextrxp(di, forceall);
895 }
896
897 /*
898  * !! rx entry routine
899  * returns a pointer to the next frame received, or NULL if there are no more
900  *   if DMA_CTRL_RXMULTI is defined, DMA scattering(multiple buffers) is
901  *   supported with pkts chain
902  *   otherwise, it's treated as giant pkt and will be tossed.
903  *   The DMA scattering starts with normal DMA header, followed by first
904  *   buffer data. After it reaches the max size of buffer, the data continues
905  *   in next DMA descriptor buffer WITHOUT DMA header
906  */
907 struct sk_buff *dma_rx(struct dma_pub *pub)
908 {
909         struct dma_info *di = (struct dma_info *)pub;
910         struct sk_buff *p, *head, *tail;
911         uint len;
912         uint pkt_len;
913         int resid = 0;
914
915  next_frame:
916         head = _dma_getnextrxp(di, false);
917         if (head == NULL)
918                 return NULL;
919
920         len = le16_to_cpu(*(u16 *) (head->data));
921         DMA_TRACE(("%s: dma_rx len %d\n", di->name, len));
922         dma_spin_for_len(len, head);
923
924         /* set actual length */
925         pkt_len = min((di->rxoffset + len), di->rxbufsize);
926         __skb_trim(head, pkt_len);
927         resid = len - (di->rxbufsize - di->rxoffset);
928
929         /* check for single or multi-buffer rx */
930         if (resid > 0) {
931                 tail = head;
932                 while ((resid > 0) && (p = _dma_getnextrxp(di, false))) {
933                         tail->next = p;
934                         pkt_len = min_t(uint, resid, di->rxbufsize);
935                         __skb_trim(p, pkt_len);
936
937                         tail = p;
938                         resid -= di->rxbufsize;
939                 }
940
941 #ifdef BCMDBG
942                 if (resid > 0) {
943                         uint cur;
944                         cur =
945                             B2I(((R_REG(&di->d64rxregs->status0) &
946                                   D64_RS0_CD_MASK) -
947                                  di->rcvptrbase) & D64_RS0_CD_MASK,
948                                 struct dma64desc);
949                         DMA_ERROR(("dma_rx, rxin %d rxout %d, hw_curr %d\n",
950                                    di->rxin, di->rxout, cur));
951                 }
952 #endif                          /* BCMDBG */
953
954                 if ((di->dma.dmactrlflags & DMA_CTRL_RXMULTI) == 0) {
955                         DMA_ERROR(("%s: dma_rx: bad frame length (%d)\n",
956                                    di->name, len));
957                         brcmu_pkt_buf_free_skb(head);
958                         di->dma.rxgiants++;
959                         goto next_frame;
960                 }
961         }
962
963         return head;
964 }
965
966 static bool dma64_rxidle(struct dma_info *di)
967 {
968         DMA_TRACE(("%s: dma_rxidle\n", di->name));
969
970         if (di->nrxd == 0)
971                 return true;
972
973         return ((R_REG(&di->d64rxregs->status0) & D64_RS0_CD_MASK) ==
974                 (R_REG(&di->d64rxregs->ptr) & D64_RS0_CD_MASK));
975 }
976
977 /*
978  * post receive buffers
979  *  return false is refill failed completely and ring is empty this will stall
980  *  the rx dma and user might want to call rxfill again asap. This unlikely
981  *  happens on memory-rich NIC, but often on memory-constrained dongle
982  */
983 bool dma_rxfill(struct dma_pub *pub)
984 {
985         struct dma_info *di = (struct dma_info *)pub;
986         struct sk_buff *p;
987         u16 rxin, rxout;
988         u32 flags = 0;
989         uint n;
990         uint i;
991         dma_addr_t pa;
992         uint extra_offset = 0;
993         bool ring_empty;
994
995         ring_empty = false;
996
997         /*
998          * Determine how many receive buffers we're lacking
999          * from the full complement, allocate, initialize,
1000          * and post them, then update the chip rx lastdscr.
1001          */
1002
1003         rxin = di->rxin;
1004         rxout = di->rxout;
1005
1006         n = di->nrxpost - nrxdactive(di, rxin, rxout);
1007
1008         DMA_TRACE(("%s: dma_rxfill: post %d\n", di->name, n));
1009
1010         if (di->rxbufsize > BCMEXTRAHDROOM)
1011                 extra_offset = di->rxextrahdrroom;
1012
1013         for (i = 0; i < n; i++) {
1014                 /*
1015                  * the di->rxbufsize doesn't include the extra headroom,
1016                  * we need to add it to the size to be allocated
1017                  */
1018                 p = brcmu_pkt_buf_get_skb(di->rxbufsize + extra_offset);
1019
1020                 if (p == NULL) {
1021                         DMA_ERROR(("%s: dma_rxfill: out of rxbufs\n",
1022                                    di->name));
1023                         if (i == 0 && dma64_rxidle(di)) {
1024                                 DMA_ERROR(("%s: rxfill64: ring is empty !\n",
1025                                            di->name));
1026                                 ring_empty = true;
1027                         }
1028                         di->dma.rxnobuf++;
1029                         break;
1030                 }
1031                 /* reserve an extra headroom, if applicable */
1032                 if (extra_offset)
1033                         skb_pull(p, extra_offset);
1034
1035                 /* Do a cached write instead of uncached write since DMA_MAP
1036                  * will flush the cache.
1037                  */
1038                 *(u32 *) (p->data) = 0;
1039
1040                 pa = pci_map_single(di->pbus, p->data,
1041                         di->rxbufsize, PCI_DMA_FROMDEVICE);
1042
1043                 /* save the free packet pointer */
1044                 di->rxp[rxout] = p;
1045
1046                 /* reset flags for each descriptor */
1047                 flags = 0;
1048                 if (rxout == (di->nrxd - 1))
1049                         flags = D64_CTRL1_EOT;
1050
1051                 dma64_dd_upd(di, di->rxd64, pa, rxout, &flags,
1052                              di->rxbufsize);
1053                 rxout = nextrxd(di, rxout);
1054         }
1055
1056         di->rxout = rxout;
1057
1058         /* update the chip lastdscr pointer */
1059         W_REG(&di->d64rxregs->ptr,
1060               di->rcvptrbase + I2B(rxout, struct dma64desc));
1061
1062         return ring_empty;
1063 }
1064
1065 void dma_rxreclaim(struct dma_pub *pub)
1066 {
1067         struct dma_info *di = (struct dma_info *)pub;
1068         struct sk_buff *p;
1069
1070         DMA_TRACE(("%s: dma_rxreclaim\n", di->name));
1071
1072         while ((p = _dma_getnextrxp(di, true)))
1073                 brcmu_pkt_buf_free_skb(p);
1074 }
1075
1076 void dma_counterreset(struct dma_pub *pub)
1077 {
1078         /* reset all software counters */
1079         pub->rxgiants = 0;
1080         pub->rxnobuf = 0;
1081         pub->txnobuf = 0;
1082 }
1083
1084 /* get the address of the var in order to change later */
1085 unsigned long dma_getvar(struct dma_pub *pub, const char *name)
1086 {
1087         struct dma_info *di = (struct dma_info *)pub;
1088
1089         if (!strcmp(name, "&txavail"))
1090                 return (unsigned long)&(di->dma.txavail);
1091         return 0;
1092 }
1093
1094 /* 64-bit DMA functions */
1095
1096 void dma_txinit(struct dma_pub *pub)
1097 {
1098         struct dma_info *di = (struct dma_info *)pub;
1099         u32 control = D64_XC_XE;
1100
1101         DMA_TRACE(("%s: dma_txinit\n", di->name));
1102
1103         if (di->ntxd == 0)
1104                 return;
1105
1106         di->txin = di->txout = 0;
1107         di->dma.txavail = di->ntxd - 1;
1108
1109         /* clear tx descriptor ring */
1110         memset(di->txd64, '\0', (di->ntxd * sizeof(struct dma64desc)));
1111
1112         /* DMA engine with out alignment requirement requires table to be inited
1113          * before enabling the engine
1114          */
1115         if (!di->aligndesc_4k)
1116                 _dma_ddtable_init(di, DMA_TX, di->txdpa);
1117
1118         if ((di->dma.dmactrlflags & DMA_CTRL_PEN) == 0)
1119                 control |= D64_XC_PD;
1120         OR_REG(&di->d64txregs->control, control);
1121
1122         /* DMA engine with alignment requirement requires table to be inited
1123          * before enabling the engine
1124          */
1125         if (di->aligndesc_4k)
1126                 _dma_ddtable_init(di, DMA_TX, di->txdpa);
1127 }
1128
1129 void dma_txsuspend(struct dma_pub *pub)
1130 {
1131         struct dma_info *di = (struct dma_info *)pub;
1132
1133         DMA_TRACE(("%s: dma_txsuspend\n", di->name));
1134
1135         if (di->ntxd == 0)
1136                 return;
1137
1138         OR_REG(&di->d64txregs->control, D64_XC_SE);
1139 }
1140
1141 void dma_txresume(struct dma_pub *pub)
1142 {
1143         struct dma_info *di = (struct dma_info *)pub;
1144
1145         DMA_TRACE(("%s: dma_txresume\n", di->name));
1146
1147         if (di->ntxd == 0)
1148                 return;
1149
1150         AND_REG(&di->d64txregs->control, ~D64_XC_SE);
1151 }
1152
1153 bool dma_txsuspended(struct dma_pub *pub)
1154 {
1155         struct dma_info *di = (struct dma_info *)pub;
1156
1157         return (di->ntxd == 0) ||
1158             ((R_REG(&di->d64txregs->control) & D64_XC_SE) ==
1159              D64_XC_SE);
1160 }
1161
1162 void dma_txreclaim(struct dma_pub *pub, enum txd_range range)
1163 {
1164         struct dma_info *di = (struct dma_info *)pub;
1165         struct sk_buff *p;
1166
1167         DMA_TRACE(("%s: dma_txreclaim %s\n", di->name,
1168                    (range == DMA_RANGE_ALL) ? "all" :
1169                    ((range ==
1170                      DMA_RANGE_TRANSMITTED) ? "transmitted" :
1171                     "transferred")));
1172
1173         if (di->txin == di->txout)
1174                 return;
1175
1176         while ((p = dma_getnexttxp(pub, range))) {
1177                 /* For unframed data, we don't have any packets to free */
1178                 if (!(di->dma.dmactrlflags & DMA_CTRL_UNFRAMED))
1179                         brcmu_pkt_buf_free_skb(p);
1180         }
1181 }
1182
1183 bool dma_txreset(struct dma_pub *pub)
1184 {
1185         struct dma_info *di = (struct dma_info *)pub;
1186         u32 status;
1187
1188         if (di->ntxd == 0)
1189                 return true;
1190
1191         /* suspend tx DMA first */
1192         W_REG(&di->d64txregs->control, D64_XC_SE);
1193         SPINWAIT(((status =
1194                    (R_REG(&di->d64txregs->status0) & D64_XS0_XS_MASK))
1195                   != D64_XS0_XS_DISABLED) && (status != D64_XS0_XS_IDLE)
1196                  && (status != D64_XS0_XS_STOPPED), 10000);
1197
1198         W_REG(&di->d64txregs->control, 0);
1199         SPINWAIT(((status =
1200                    (R_REG(&di->d64txregs->status0) & D64_XS0_XS_MASK))
1201                   != D64_XS0_XS_DISABLED), 10000);
1202
1203         /* wait for the last transaction to complete */
1204         udelay(300);
1205
1206         return status == D64_XS0_XS_DISABLED;
1207 }
1208
1209 bool dma_rxreset(struct dma_pub *pub)
1210 {
1211         struct dma_info *di = (struct dma_info *)pub;
1212         u32 status;
1213
1214         if (di->nrxd == 0)
1215                 return true;
1216
1217         W_REG(&di->d64rxregs->control, 0);
1218         SPINWAIT(((status =
1219                    (R_REG(&di->d64rxregs->status0) & D64_RS0_RS_MASK))
1220                   != D64_RS0_RS_DISABLED), 10000);
1221
1222         return status == D64_RS0_RS_DISABLED;
1223 }
1224
1225 /*
1226  * !! tx entry routine
1227  * WARNING: call must check the return value for error.
1228  *   the error(toss frames) could be fatal and cause many subsequent hard
1229  *   to debug problems
1230  */
1231 int dma_txfast(struct dma_pub *pub, struct sk_buff *p0, bool commit)
1232 {
1233         struct dma_info *di = (struct dma_info *)pub;
1234         struct sk_buff *p, *next;
1235         unsigned char *data;
1236         uint len;
1237         u16 txout;
1238         u32 flags = 0;
1239         dma_addr_t pa;
1240
1241         DMA_TRACE(("%s: dma_txfast\n", di->name));
1242
1243         txout = di->txout;
1244
1245         /*
1246          * Walk the chain of packet buffers
1247          * allocating and initializing transmit descriptor entries.
1248          */
1249         for (p = p0; p; p = next) {
1250                 data = p->data;
1251                 len = p->len;
1252                 next = p->next;
1253
1254                 /* return nonzero if out of tx descriptors */
1255                 if (nexttxd(di, txout) == di->txin)
1256                         goto outoftxd;
1257
1258                 if (len == 0)
1259                         continue;
1260
1261                 /* get physical address of buffer start */
1262                 pa = pci_map_single(di->pbus, data, len, PCI_DMA_TODEVICE);
1263
1264                 flags = 0;
1265                 if (p == p0)
1266                         flags |= D64_CTRL1_SOF;
1267
1268                 /* With a DMA segment list, Descriptor table is filled
1269                  * using the segment list instead of looping over
1270                  * buffers in multi-chain DMA. Therefore, EOF for SGLIST
1271                  * is when end of segment list is reached.
1272                  */
1273                 if (next == NULL)
1274                         flags |= (D64_CTRL1_IOC | D64_CTRL1_EOF);
1275                 if (txout == (di->ntxd - 1))
1276                         flags |= D64_CTRL1_EOT;
1277
1278                 dma64_dd_upd(di, di->txd64, pa, txout, &flags, len);
1279
1280                 txout = nexttxd(di, txout);
1281         }
1282
1283         /* if last txd eof not set, fix it */
1284         if (!(flags & D64_CTRL1_EOF))
1285                 di->txd64[prevtxd(di, txout)].ctrl1 =
1286                      cpu_to_le32(flags | D64_CTRL1_IOC | D64_CTRL1_EOF);
1287
1288         /* save the packet */
1289         di->txp[prevtxd(di, txout)] = p0;
1290
1291         /* bump the tx descriptor index */
1292         di->txout = txout;
1293
1294         /* kick the chip */
1295         if (commit)
1296                 W_REG(&di->d64txregs->ptr,
1297                       di->xmtptrbase + I2B(txout, struct dma64desc));
1298
1299         /* tx flow control */
1300         di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) - 1;
1301
1302         return 0;
1303
1304  outoftxd:
1305         DMA_ERROR(("%s: dma_txfast: out of txds !!!\n", di->name));
1306         brcmu_pkt_buf_free_skb(p0);
1307         di->dma.txavail = 0;
1308         di->dma.txnobuf++;
1309         return -1;
1310 }
1311
1312 /*
1313  * Reclaim next completed txd (txds if using chained buffers) in the range
1314  * specified and return associated packet.
1315  * If range is DMA_RANGE_TRANSMITTED, reclaim descriptors that have be
1316  * transmitted as noted by the hardware "CurrDescr" pointer.
1317  * If range is DMA_RANGE_TRANSFERED, reclaim descriptors that have be
1318  * transferred by the DMA as noted by the hardware "ActiveDescr" pointer.
1319  * If range is DMA_RANGE_ALL, reclaim all txd(s) posted to the ring and
1320  * return associated packet regardless of the value of hardware pointers.
1321  */
1322 struct sk_buff *dma_getnexttxp(struct dma_pub *pub, enum txd_range range)
1323 {
1324         struct dma_info *di = (struct dma_info *)pub;
1325         u16 start, end, i;
1326         u16 active_desc;
1327         struct sk_buff *txp;
1328
1329         DMA_TRACE(("%s: dma_getnexttxp %s\n", di->name,
1330                    (range == DMA_RANGE_ALL) ? "all" :
1331                    ((range ==
1332                      DMA_RANGE_TRANSMITTED) ? "transmitted" :
1333                     "transferred")));
1334
1335         if (di->ntxd == 0)
1336                 return NULL;
1337
1338         txp = NULL;
1339
1340         start = di->txin;
1341         if (range == DMA_RANGE_ALL)
1342                 end = di->txout;
1343         else {
1344                 struct dma64regs *dregs = di->d64txregs;
1345
1346                 end = (u16) (B2I(((R_REG(&dregs->status0) &
1347                                  D64_XS0_CD_MASK) -
1348                                  di->xmtptrbase) & D64_XS0_CD_MASK,
1349                                  struct dma64desc));
1350
1351                 if (range == DMA_RANGE_TRANSFERED) {
1352                         active_desc =
1353                             (u16) (R_REG(&dregs->status1) &
1354                                       D64_XS1_AD_MASK);
1355                         active_desc =
1356                             (active_desc - di->xmtptrbase) & D64_XS0_CD_MASK;
1357                         active_desc = B2I(active_desc, struct dma64desc);
1358                         if (end != active_desc)
1359                                 end = prevtxd(di, active_desc);
1360                 }
1361         }
1362
1363         if ((start == 0) && (end > di->txout))
1364                 goto bogus;
1365
1366         for (i = start; i != end && !txp; i = nexttxd(di, i)) {
1367                 dma_addr_t pa;
1368                 uint size;
1369
1370                 pa = cpu_to_le32(di->txd64[i].addrlow) - di->dataoffsetlow;
1371
1372                 size =
1373                     (cpu_to_le32(di->txd64[i].ctrl2) &
1374                      D64_CTRL2_BC_MASK);
1375
1376                 di->txd64[i].addrlow = 0xdeadbeef;
1377                 di->txd64[i].addrhigh = 0xdeadbeef;
1378
1379                 txp = di->txp[i];
1380                 di->txp[i] = NULL;
1381
1382                 pci_unmap_single(di->pbus, pa, size, PCI_DMA_TODEVICE);
1383         }
1384
1385         di->txin = i;
1386
1387         /* tx flow control */
1388         di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) - 1;
1389
1390         return txp;
1391
1392  bogus:
1393         DMA_NONE(("dma_getnexttxp: bogus curr: start %d end %d txout %d "
1394                   "force %d\n", start, end, di->txout, forceall));
1395         return NULL;
1396 }
1397
1398 /*
1399  * Mac80211 initiated actions sometimes require packets in the DMA queue to be
1400  * modified. The modified portion of the packet is not under control of the DMA
1401  * engine. This function calls a caller-supplied function for each packet in
1402  * the caller specified dma chain.
1403  */
1404 void dma_walk_packets(struct dma_pub *dmah, void (*callback_fnc)
1405                       (void *pkt, void *arg_a), void *arg_a)
1406 {
1407         struct dma_info *di = (struct dma_info *) dmah;
1408         uint i =   di->txin;
1409         uint end = di->txout;
1410         struct sk_buff *skb;
1411         struct ieee80211_tx_info *tx_info;
1412
1413         while (i != end) {
1414                 skb = (struct sk_buff *)di->txp[i];
1415                 if (skb != NULL) {
1416                         tx_info = (struct ieee80211_tx_info *)skb->cb;
1417                         (callback_fnc)(tx_info, arg_a);
1418                 }
1419                 i = nexttxd(di, i);
1420         }
1421 }