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