blob: ba3344310f07fb1c814eda34846277207b5c2e36 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
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 */
Joe Perches8505a7e2011-11-13 11:41:04 -080016
Arend van Spriel5b435de2011-10-05 13:19:03 +020017#include <linux/slab.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020018#include <linux/delay.h>
19#include <linux/pci.h>
Seth Forsheee041f652012-11-15 08:07:56 -060020#include <net/cfg80211.h>
21#include <net/mac80211.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020022
23#include <brcmu_utils.h>
24#include <aiutils.h>
25#include "types.h"
Seth Forsheee041f652012-11-15 08:07:56 -060026#include "main.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020027#include "dma.h"
Alwin Beukers23038212011-10-18 14:02:58 +020028#include "soc.h"
Seth Forsheee041f652012-11-15 08:07:56 -060029#include "scb.h"
30#include "ampdu.h"
Seth Forshee90123e02012-11-15 08:08:07 -060031#include "debug.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020032
33/*
Arend van Spriele81da652011-12-08 15:06:53 -080034 * dma register field offset calculation
35 */
36#define DMA64REGOFFS(field) offsetof(struct dma64regs, field)
37#define DMA64TXREGOFFS(di, field) (di->d64txregbase + DMA64REGOFFS(field))
38#define DMA64RXREGOFFS(di, field) (di->d64rxregbase + DMA64REGOFFS(field))
39
40/*
Arend van Spriel5b435de2011-10-05 13:19:03 +020041 * DMA hardware requires each descriptor ring to be 8kB aligned, and fit within
42 * a contiguous 8kB physical address.
43 */
44#define D64RINGALIGN_BITS 13
45#define D64MAXRINGSZ (1 << D64RINGALIGN_BITS)
46#define D64RINGALIGN (1 << D64RINGALIGN_BITS)
47
48#define D64MAXDD (D64MAXRINGSZ / sizeof(struct dma64desc))
49
50/* transmit channel control */
51#define D64_XC_XE 0x00000001 /* transmit enable */
52#define D64_XC_SE 0x00000002 /* transmit suspend request */
53#define D64_XC_LE 0x00000004 /* loopback enable */
54#define D64_XC_FL 0x00000010 /* flush request */
55#define D64_XC_PD 0x00000800 /* parity check disable */
56#define D64_XC_AE 0x00030000 /* address extension bits */
57#define D64_XC_AE_SHIFT 16
58
59/* transmit descriptor table pointer */
60#define D64_XP_LD_MASK 0x00000fff /* last valid descriptor */
61
62/* transmit channel status */
63#define D64_XS0_CD_MASK 0x00001fff /* current descriptor pointer */
64#define D64_XS0_XS_MASK 0xf0000000 /* transmit state */
65#define D64_XS0_XS_SHIFT 28
66#define D64_XS0_XS_DISABLED 0x00000000 /* disabled */
67#define D64_XS0_XS_ACTIVE 0x10000000 /* active */
68#define D64_XS0_XS_IDLE 0x20000000 /* idle wait */
69#define D64_XS0_XS_STOPPED 0x30000000 /* stopped */
70#define D64_XS0_XS_SUSP 0x40000000 /* suspend pending */
71
72#define D64_XS1_AD_MASK 0x00001fff /* active descriptor */
73#define D64_XS1_XE_MASK 0xf0000000 /* transmit errors */
74#define D64_XS1_XE_SHIFT 28
75#define D64_XS1_XE_NOERR 0x00000000 /* no error */
76#define D64_XS1_XE_DPE 0x10000000 /* descriptor protocol error */
77#define D64_XS1_XE_DFU 0x20000000 /* data fifo underrun */
78#define D64_XS1_XE_DTE 0x30000000 /* data transfer error */
79#define D64_XS1_XE_DESRE 0x40000000 /* descriptor read error */
80#define D64_XS1_XE_COREE 0x50000000 /* core error */
81
82/* receive channel control */
83/* receive enable */
84#define D64_RC_RE 0x00000001
85/* receive frame offset */
86#define D64_RC_RO_MASK 0x000000fe
87#define D64_RC_RO_SHIFT 1
88/* direct fifo receive (pio) mode */
89#define D64_RC_FM 0x00000100
90/* separate rx header descriptor enable */
91#define D64_RC_SH 0x00000200
92/* overflow continue */
93#define D64_RC_OC 0x00000400
94/* parity check disable */
95#define D64_RC_PD 0x00000800
96/* address extension bits */
97#define D64_RC_AE 0x00030000
98#define D64_RC_AE_SHIFT 16
99
100/* flags for dma controller */
101/* partity enable */
102#define DMA_CTRL_PEN (1 << 0)
103/* rx overflow continue */
104#define DMA_CTRL_ROC (1 << 1)
105/* allow rx scatter to multiple descriptors */
106#define DMA_CTRL_RXMULTI (1 << 2)
107/* Unframed Rx/Tx data */
108#define DMA_CTRL_UNFRAMED (1 << 3)
109
110/* receive descriptor table pointer */
111#define D64_RP_LD_MASK 0x00000fff /* last valid descriptor */
112
113/* receive channel status */
114#define D64_RS0_CD_MASK 0x00001fff /* current descriptor pointer */
115#define D64_RS0_RS_MASK 0xf0000000 /* receive state */
116#define D64_RS0_RS_SHIFT 28
117#define D64_RS0_RS_DISABLED 0x00000000 /* disabled */
118#define D64_RS0_RS_ACTIVE 0x10000000 /* active */
119#define D64_RS0_RS_IDLE 0x20000000 /* idle wait */
120#define D64_RS0_RS_STOPPED 0x30000000 /* stopped */
121#define D64_RS0_RS_SUSP 0x40000000 /* suspend pending */
122
123#define D64_RS1_AD_MASK 0x0001ffff /* active descriptor */
124#define D64_RS1_RE_MASK 0xf0000000 /* receive errors */
125#define D64_RS1_RE_SHIFT 28
126#define D64_RS1_RE_NOERR 0x00000000 /* no error */
127#define D64_RS1_RE_DPO 0x10000000 /* descriptor protocol error */
128#define D64_RS1_RE_DFU 0x20000000 /* data fifo overflow */
129#define D64_RS1_RE_DTE 0x30000000 /* data transfer error */
130#define D64_RS1_RE_DESRE 0x40000000 /* descriptor read error */
131#define D64_RS1_RE_COREE 0x50000000 /* core error */
132
133/* fifoaddr */
134#define D64_FA_OFF_MASK 0xffff /* offset */
135#define D64_FA_SEL_MASK 0xf0000 /* select */
136#define D64_FA_SEL_SHIFT 16
137#define D64_FA_SEL_XDD 0x00000 /* transmit dma data */
138#define D64_FA_SEL_XDP 0x10000 /* transmit dma pointers */
139#define D64_FA_SEL_RDD 0x40000 /* receive dma data */
140#define D64_FA_SEL_RDP 0x50000 /* receive dma pointers */
141#define D64_FA_SEL_XFD 0x80000 /* transmit fifo data */
142#define D64_FA_SEL_XFP 0x90000 /* transmit fifo pointers */
143#define D64_FA_SEL_RFD 0xc0000 /* receive fifo data */
144#define D64_FA_SEL_RFP 0xd0000 /* receive fifo pointers */
145#define D64_FA_SEL_RSD 0xe0000 /* receive frame status data */
146#define D64_FA_SEL_RSP 0xf0000 /* receive frame status pointers */
147
148/* descriptor control flags 1 */
149#define D64_CTRL_COREFLAGS 0x0ff00000 /* core specific flags */
150#define D64_CTRL1_EOT ((u32)1 << 28) /* end of descriptor table */
151#define D64_CTRL1_IOC ((u32)1 << 29) /* interrupt on completion */
152#define D64_CTRL1_EOF ((u32)1 << 30) /* end of frame */
153#define D64_CTRL1_SOF ((u32)1 << 31) /* start of frame */
154
155/* descriptor control flags 2 */
156/* buffer byte count. real data len must <= 16KB */
157#define D64_CTRL2_BC_MASK 0x00007fff
158/* address extension bits */
159#define D64_CTRL2_AE 0x00030000
160#define D64_CTRL2_AE_SHIFT 16
161/* parity bit */
162#define D64_CTRL2_PARITY 0x00040000
163
164/* control flags in the range [27:20] are core-specific and not defined here */
165#define D64_CTRL_CORE_MASK 0x0ff00000
166
167#define D64_RX_FRM_STS_LEN 0x0000ffff /* frame length mask */
168#define D64_RX_FRM_STS_OVFL 0x00800000 /* RxOverFlow */
169#define D64_RX_FRM_STS_DSCRCNT 0x0f000000 /* no. of descriptors used - 1 */
170#define D64_RX_FRM_STS_DATATYPE 0xf0000000 /* core-dependent data type */
171
172/*
173 * packet headroom necessary to accommodate the largest header
174 * in the system, (i.e TXOFF). By doing, we avoid the need to
175 * allocate an extra buffer for the header when bridging to WL.
176 * There is a compile time check in wlc.c which ensure that this
177 * value is at least as big as TXOFF. This value is used in
178 * dma_rxfill().
179 */
180
181#define BCMEXTRAHDROOM 172
182
Arend van Spriel5b435de2011-10-05 13:19:03 +0200183#define MAXNAMEL 8 /* 8 char names */
184
185/* macros to convert between byte offsets and indexes */
186#define B2I(bytes, type) ((bytes) / sizeof(type))
187#define I2B(index, type) ((index) * sizeof(type))
188
189#define PCI32ADDR_HIGH 0xc0000000 /* address[31:30] */
190#define PCI32ADDR_HIGH_SHIFT 30 /* address[31:30] */
191
192#define PCI64ADDR_HIGH 0x80000000 /* address[63] */
193#define PCI64ADDR_HIGH_SHIFT 31 /* address[63] */
194
195/*
196 * DMA Descriptor
197 * Descriptors are only read by the hardware, never written back.
198 */
199struct dma64desc {
200 __le32 ctrl1; /* misc control bits & bufcount */
201 __le32 ctrl2; /* buffer count and address extension */
202 __le32 addrlow; /* memory address of the date buffer, bits 31:0 */
203 __le32 addrhigh; /* memory address of the date buffer, bits 63:32 */
204};
205
206/* dma engine software state */
207struct dma_info {
208 struct dma_pub dma; /* exported structure */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200209 char name[MAXNAMEL]; /* callers name for diag msgs */
210
Arend van Spriel3b758a62011-12-12 15:15:09 -0800211 struct bcma_device *core;
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800212 struct device *dmadev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200213
Seth Forsheee041f652012-11-15 08:07:56 -0600214 /* session information for AMPDU */
215 struct brcms_ampdu_session ampdu_session;
216
Arend van Spriel5b435de2011-10-05 13:19:03 +0200217 bool dma64; /* this dma engine is operating in 64-bit mode */
218 bool addrext; /* this dma engine supports DmaExtendedAddrChanges */
219
220 /* 64-bit dma tx engine registers */
Arend van Spriele81da652011-12-08 15:06:53 -0800221 uint d64txregbase;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200222 /* 64-bit dma rx engine registers */
Arend van Spriele81da652011-12-08 15:06:53 -0800223 uint d64rxregbase;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200224 /* pointer to dma64 tx descriptor ring */
225 struct dma64desc *txd64;
226 /* pointer to dma64 rx descriptor ring */
227 struct dma64desc *rxd64;
228
229 u16 dmadesc_align; /* alignment requirement for dma descriptors */
230
231 u16 ntxd; /* # tx descriptors tunable */
232 u16 txin; /* index of next descriptor to reclaim */
233 u16 txout; /* index of next descriptor to post */
234 /* pointer to parallel array of pointers to packets */
235 struct sk_buff **txp;
236 /* Aligned physical address of descriptor ring */
237 dma_addr_t txdpa;
238 /* Original physical address of descriptor ring */
239 dma_addr_t txdpaorig;
240 u16 txdalign; /* #bytes added to alloc'd mem to align txd */
241 u32 txdalloc; /* #bytes allocated for the ring */
242 u32 xmtptrbase; /* When using unaligned descriptors, the ptr register
243 * is not just an index, it needs all 13 bits to be
244 * an offset from the addr register.
245 */
246
247 u16 nrxd; /* # rx descriptors tunable */
248 u16 rxin; /* index of next descriptor to reclaim */
249 u16 rxout; /* index of next descriptor to post */
250 /* pointer to parallel array of pointers to packets */
251 struct sk_buff **rxp;
252 /* Aligned physical address of descriptor ring */
253 dma_addr_t rxdpa;
254 /* Original physical address of descriptor ring */
255 dma_addr_t rxdpaorig;
256 u16 rxdalign; /* #bytes added to alloc'd mem to align rxd */
257 u32 rxdalloc; /* #bytes allocated for the ring */
258 u32 rcvptrbase; /* Base for ptr reg when using unaligned descriptors */
259
260 /* tunables */
261 unsigned int rxbufsize; /* rx buffer size in bytes, not including
262 * the extra headroom
263 */
264 uint rxextrahdrroom; /* extra rx headroom, reverseved to assist upper
265 * stack, e.g. some rx pkt buffers will be
266 * bridged to tx side without byte copying.
267 * The extra headroom needs to be large enough
268 * to fit txheader needs. Some dongle driver may
269 * not need it.
270 */
271 uint nrxpost; /* # rx buffers to keep posted */
272 unsigned int rxoffset; /* rxcontrol offset */
273 /* add to get dma address of descriptor ring, low 32 bits */
274 uint ddoffsetlow;
275 /* high 32 bits */
276 uint ddoffsethigh;
277 /* add to get dma address of data buffer, low 32 bits */
278 uint dataoffsetlow;
279 /* high 32 bits */
280 uint dataoffsethigh;
281 /* descriptor base need to be aligned or not */
282 bool aligndesc_4k;
283};
284
Arend van Spriel5b435de2011-10-05 13:19:03 +0200285/* Check for odd number of 1's */
286static u32 parity32(__le32 data)
287{
288 /* no swap needed for counting 1's */
289 u32 par_data = *(u32 *)&data;
290
291 par_data ^= par_data >> 16;
292 par_data ^= par_data >> 8;
293 par_data ^= par_data >> 4;
294 par_data ^= par_data >> 2;
295 par_data ^= par_data >> 1;
296
297 return par_data & 1;
298}
299
300static bool dma64_dd_parity(struct dma64desc *dd)
301{
302 return parity32(dd->addrlow ^ dd->addrhigh ^ dd->ctrl1 ^ dd->ctrl2);
303}
304
305/* descriptor bumping functions */
306
307static uint xxd(uint x, uint n)
308{
309 return x & (n - 1); /* faster than %, but n must be power of 2 */
310}
311
312static uint txd(struct dma_info *di, uint x)
313{
314 return xxd(x, di->ntxd);
315}
316
317static uint rxd(struct dma_info *di, uint x)
318{
319 return xxd(x, di->nrxd);
320}
321
322static uint nexttxd(struct dma_info *di, uint i)
323{
324 return txd(di, i + 1);
325}
326
327static uint prevtxd(struct dma_info *di, uint i)
328{
329 return txd(di, i - 1);
330}
331
332static uint nextrxd(struct dma_info *di, uint i)
333{
Seth Forsheeb05618d2012-11-15 08:07:57 -0600334 return rxd(di, i + 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200335}
336
337static uint ntxdactive(struct dma_info *di, uint h, uint t)
338{
339 return txd(di, t-h);
340}
341
342static uint nrxdactive(struct dma_info *di, uint h, uint t)
343{
344 return rxd(di, t-h);
345}
346
347static uint _dma_ctrlflags(struct dma_info *di, uint mask, uint flags)
348{
Arend van Sprielae8e4672011-10-29 11:30:15 +0200349 uint dmactrlflags;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200350
351 if (di == NULL) {
Seth Forshee90123e02012-11-15 08:08:07 -0600352 brcms_dbg_dma(di->core, "NULL dma handle\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200353 return 0;
354 }
355
Arend van Sprielae8e4672011-10-29 11:30:15 +0200356 dmactrlflags = di->dma.dmactrlflags;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200357 dmactrlflags &= ~mask;
358 dmactrlflags |= flags;
359
360 /* If trying to enable parity, check if parity is actually supported */
361 if (dmactrlflags & DMA_CTRL_PEN) {
362 u32 control;
363
Arend van Spriel3b758a62011-12-12 15:15:09 -0800364 control = bcma_read32(di->core, DMA64TXREGOFFS(di, control));
365 bcma_write32(di->core, DMA64TXREGOFFS(di, control),
Arend van Spriel5b435de2011-10-05 13:19:03 +0200366 control | D64_XC_PD);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800367 if (bcma_read32(di->core, DMA64TXREGOFFS(di, control)) &
Arend van Spriele81da652011-12-08 15:06:53 -0800368 D64_XC_PD)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200369 /* We *can* disable it so it is supported,
370 * restore control register
371 */
Arend van Spriel3b758a62011-12-12 15:15:09 -0800372 bcma_write32(di->core, DMA64TXREGOFFS(di, control),
Arend van Spriele81da652011-12-08 15:06:53 -0800373 control);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200374 else
375 /* Not supported, don't allow it to be enabled */
376 dmactrlflags &= ~DMA_CTRL_PEN;
377 }
378
379 di->dma.dmactrlflags = dmactrlflags;
380
381 return dmactrlflags;
382}
383
Arend van Spriele81da652011-12-08 15:06:53 -0800384static bool _dma64_addrext(struct dma_info *di, uint ctrl_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200385{
386 u32 w;
Arend van Spriel3b758a62011-12-12 15:15:09 -0800387 bcma_set32(di->core, ctrl_offset, D64_XC_AE);
388 w = bcma_read32(di->core, ctrl_offset);
389 bcma_mask32(di->core, ctrl_offset, ~D64_XC_AE);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200390 return (w & D64_XC_AE) == D64_XC_AE;
391}
392
393/*
394 * return true if this dma engine supports DmaExtendedAddrChanges,
395 * otherwise false
396 */
397static bool _dma_isaddrext(struct dma_info *di)
398{
399 /* DMA64 supports full 32- or 64-bit operation. AE is always valid */
400
401 /* not all tx or rx channel are available */
Arend van Spriele81da652011-12-08 15:06:53 -0800402 if (di->d64txregbase != 0) {
403 if (!_dma64_addrext(di, DMA64TXREGOFFS(di, control)))
Seth Forshee90123e02012-11-15 08:08:07 -0600404 brcms_dbg_dma(di->core,
405 "%s: DMA64 tx doesn't have AE set\n",
406 di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200407 return true;
Arend van Spriele81da652011-12-08 15:06:53 -0800408 } else if (di->d64rxregbase != 0) {
409 if (!_dma64_addrext(di, DMA64RXREGOFFS(di, control)))
Seth Forshee90123e02012-11-15 08:08:07 -0600410 brcms_dbg_dma(di->core,
411 "%s: DMA64 rx doesn't have AE set\n",
412 di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200413 return true;
414 }
415
416 return false;
417}
418
419static bool _dma_descriptor_align(struct dma_info *di)
420{
421 u32 addrl;
422
423 /* Check to see if the descriptors need to be aligned on 4K/8K or not */
Arend van Spriele81da652011-12-08 15:06:53 -0800424 if (di->d64txregbase != 0) {
Arend van Spriel3b758a62011-12-12 15:15:09 -0800425 bcma_write32(di->core, DMA64TXREGOFFS(di, addrlow), 0xff0);
426 addrl = bcma_read32(di->core, DMA64TXREGOFFS(di, addrlow));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200427 if (addrl != 0)
428 return false;
Arend van Spriele81da652011-12-08 15:06:53 -0800429 } else if (di->d64rxregbase != 0) {
Arend van Spriel3b758a62011-12-12 15:15:09 -0800430 bcma_write32(di->core, DMA64RXREGOFFS(di, addrlow), 0xff0);
431 addrl = bcma_read32(di->core, DMA64RXREGOFFS(di, addrlow));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200432 if (addrl != 0)
433 return false;
434 }
435 return true;
436}
437
438/*
439 * Descriptor table must start at the DMA hardware dictated alignment, so
440 * allocated memory must be large enough to support this requirement.
441 */
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800442static void *dma_alloc_consistent(struct dma_info *di, uint size,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200443 u16 align_bits, uint *alloced,
444 dma_addr_t *pap)
445{
446 if (align_bits) {
447 u16 align = (1 << align_bits);
448 if (!IS_ALIGNED(PAGE_SIZE, align))
449 size += align;
450 *alloced = size;
451 }
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800452 return dma_alloc_coherent(di->dmadev, size, pap, GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200453}
454
455static
456u8 dma_align_sizetobits(uint size)
457{
458 u8 bitpos = 0;
459 while (size >>= 1)
460 bitpos++;
461 return bitpos;
462}
463
464/* This function ensures that the DMA descriptor ring will not get allocated
465 * across Page boundary. If the allocation is done across the page boundary
466 * at the first time, then it is freed and the allocation is done at
467 * descriptor ring size aligned location. This will ensure that the ring will
468 * not cross page boundary
469 */
470static void *dma_ringalloc(struct dma_info *di, u32 boundary, uint size,
471 u16 *alignbits, uint *alloced,
472 dma_addr_t *descpa)
473{
474 void *va;
475 u32 desc_strtaddr;
476 u32 alignbytes = 1 << *alignbits;
477
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800478 va = dma_alloc_consistent(di, size, *alignbits, alloced, descpa);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200479
480 if (NULL == va)
481 return NULL;
482
483 desc_strtaddr = (u32) roundup((unsigned long)va, alignbytes);
484 if (((desc_strtaddr + size - 1) & boundary) != (desc_strtaddr
485 & boundary)) {
486 *alignbits = dma_align_sizetobits(size);
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800487 dma_free_coherent(di->dmadev, size, va, *descpa);
488 va = dma_alloc_consistent(di, size, *alignbits,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200489 alloced, descpa);
490 }
491 return va;
492}
493
494static bool dma64_alloc(struct dma_info *di, uint direction)
495{
496 u16 size;
497 uint ddlen;
498 void *va;
499 uint alloced = 0;
500 u16 align;
501 u16 align_bits;
502
503 ddlen = sizeof(struct dma64desc);
504
505 size = (direction == DMA_TX) ? (di->ntxd * ddlen) : (di->nrxd * ddlen);
506 align_bits = di->dmadesc_align;
507 align = (1 << align_bits);
508
509 if (direction == DMA_TX) {
510 va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
511 &alloced, &di->txdpaorig);
512 if (va == NULL) {
Seth Forshee90123e02012-11-15 08:08:07 -0600513 brcms_dbg_dma(di->core,
514 "%s: DMA_ALLOC_CONSISTENT(ntxd) failed\n",
515 di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200516 return false;
517 }
518 align = (1 << align_bits);
519 di->txd64 = (struct dma64desc *)
520 roundup((unsigned long)va, align);
521 di->txdalign = (uint) ((s8 *)di->txd64 - (s8 *) va);
522 di->txdpa = di->txdpaorig + di->txdalign;
523 di->txdalloc = alloced;
524 } else {
525 va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
526 &alloced, &di->rxdpaorig);
527 if (va == NULL) {
Seth Forshee90123e02012-11-15 08:08:07 -0600528 brcms_dbg_dma(di->core,
529 "%s: DMA_ALLOC_CONSISTENT(nrxd) failed\n",
530 di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200531 return false;
532 }
533 align = (1 << align_bits);
534 di->rxd64 = (struct dma64desc *)
535 roundup((unsigned long)va, align);
536 di->rxdalign = (uint) ((s8 *)di->rxd64 - (s8 *) va);
537 di->rxdpa = di->rxdpaorig + di->rxdalign;
538 di->rxdalloc = alloced;
539 }
540
541 return true;
542}
543
544static bool _dma_alloc(struct dma_info *di, uint direction)
545{
546 return dma64_alloc(di, direction);
547}
548
Seth Forsheee041f652012-11-15 08:07:56 -0600549struct dma_pub *dma_attach(char *name, struct brcms_c_info *wlc,
Arend van Spriele81da652011-12-08 15:06:53 -0800550 uint txregbase, uint rxregbase, uint ntxd, uint nrxd,
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800551 uint rxbufsize, int rxextheadroom,
Seth Forshee90123e02012-11-15 08:08:07 -0600552 uint nrxpost, uint rxoffset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200553{
Seth Forsheee041f652012-11-15 08:07:56 -0600554 struct si_pub *sih = wlc->hw->sih;
555 struct bcma_device *core = wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200556 struct dma_info *di;
Arend van Spriel3b758a62011-12-12 15:15:09 -0800557 u8 rev = core->id.rev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200558 uint size;
Hauke Mehrtensec5ab1d2012-06-30 15:16:18 +0200559 struct si_info *sii = container_of(sih, struct si_info, pub);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200560
561 /* allocate private info structure */
562 di = kzalloc(sizeof(struct dma_info), GFP_ATOMIC);
563 if (di == NULL)
564 return NULL;
565
Arend van Spriela8779e42011-12-08 15:06:58 -0800566 di->dma64 =
Arend van Spriel3b758a62011-12-12 15:15:09 -0800567 ((bcma_aread32(core, BCMA_IOST) & SISF_DMA64) == SISF_DMA64);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200568
Arend van Spriele81da652011-12-08 15:06:53 -0800569 /* init dma reg info */
Arend van Spriel3b758a62011-12-12 15:15:09 -0800570 di->core = core;
Arend van Spriele81da652011-12-08 15:06:53 -0800571 di->d64txregbase = txregbase;
572 di->d64rxregbase = rxregbase;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200573
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
Seth Forshee90123e02012-11-15 08:08:07 -0600581 brcms_dbg_dma(di->core, "%s: %s flags 0x%x ntxd %d nrxd %d "
582 "rxbufsize %d rxextheadroom %d nrxpost %d rxoffset %d "
583 "txregbase %u rxregbase %u\n", name, "DMA64",
584 di->dma.dmactrlflags, ntxd, nrxd, rxbufsize,
585 rxextheadroom, nrxpost, rxoffset, txregbase, rxregbase);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200586
587 /* make a private copy of our callers name */
588 strncpy(di->name, name, MAXNAMEL);
589 di->name[MAXNAMEL - 1] = '\0';
590
Arend van Spriel3b758a62011-12-12 15:15:09 -0800591 di->dmadev = core->dma_dev;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200592
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;
Hauke Mehrtensec5ab1d2012-06-30 15:16:18 +0200617 /* for pci bus, add offset */
618 if (sii->icbus->hosttype == BCMA_HOSTTYPE_PCI) {
619 /* add offset for pcie with DMA64 bus */
620 di->ddoffsetlow = 0;
621 di->ddoffsethigh = SI_PCIE_DMA_H32;
622 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200623 di->dataoffsetlow = di->ddoffsetlow;
624 di->dataoffsethigh = di->ddoffsethigh;
Hauke Mehrtensec5ab1d2012-06-30 15:16:18 +0200625
Arend van Spriel5b435de2011-10-05 13:19:03 +0200626 /* WAR64450 : DMACtl.Addr ext fields are not supported in SDIOD core. */
Hauke Mehrtens780b9c42012-06-30 15:16:12 +0200627 if ((core->id.id == BCMA_CORE_SDIO_DEV)
Arend van Spriel3b758a62011-12-12 15:15:09 -0800628 && ((rev > 0) && (rev <= 2)))
Rusty Russell3db1cd52011-12-19 13:56:45 +0000629 di->addrext = false;
Hauke Mehrtens780b9c42012-06-30 15:16:12 +0200630 else if ((core->id.id == BCMA_CORE_I2S) &&
Arend van Spriel3b758a62011-12-12 15:15:09 -0800631 ((rev == 0) || (rev == 1)))
Rusty Russell3db1cd52011-12-19 13:56:45 +0000632 di->addrext = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200633 else
634 di->addrext = _dma_isaddrext(di);
635
636 /* does the descriptor need to be aligned and if yes, on 4K/8K or not */
637 di->aligndesc_4k = _dma_descriptor_align(di);
638 if (di->aligndesc_4k) {
639 di->dmadesc_align = D64RINGALIGN_BITS;
640 if ((ntxd < D64MAXDD / 2) && (nrxd < D64MAXDD / 2))
641 /* for smaller dd table, HW relax alignment reqmnt */
642 di->dmadesc_align = D64RINGALIGN_BITS - 1;
643 } else {
644 di->dmadesc_align = 4; /* 16 byte alignment */
645 }
646
Seth Forshee90123e02012-11-15 08:08:07 -0600647 brcms_dbg_dma(di->core, "DMA descriptor align_needed %d, align %d\n",
648 di->aligndesc_4k, di->dmadesc_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200649
650 /* allocate tx packet pointer vector */
651 if (ntxd) {
652 size = ntxd * sizeof(void *);
653 di->txp = kzalloc(size, GFP_ATOMIC);
654 if (di->txp == NULL)
655 goto fail;
656 }
657
658 /* allocate rx packet pointer vector */
659 if (nrxd) {
660 size = nrxd * sizeof(void *);
661 di->rxp = kzalloc(size, GFP_ATOMIC);
662 if (di->rxp == NULL)
663 goto fail;
664 }
665
666 /*
667 * allocate transmit descriptor ring, only need ntxd descriptors
668 * but it must be aligned
669 */
670 if (ntxd) {
671 if (!_dma_alloc(di, DMA_TX))
672 goto fail;
673 }
674
675 /*
676 * allocate receive descriptor ring, only need nrxd descriptors
677 * but it must be aligned
678 */
679 if (nrxd) {
680 if (!_dma_alloc(di, DMA_RX))
681 goto fail;
682 }
683
684 if ((di->ddoffsetlow != 0) && !di->addrext) {
685 if (di->txdpa > SI_PCI_DMA_SZ) {
Seth Forshee90123e02012-11-15 08:08:07 -0600686 brcms_dbg_dma(di->core,
687 "%s: txdpa 0x%x: addrext not supported\n",
688 di->name, (u32)di->txdpa);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200689 goto fail;
690 }
691 if (di->rxdpa > SI_PCI_DMA_SZ) {
Seth Forshee90123e02012-11-15 08:08:07 -0600692 brcms_dbg_dma(di->core,
693 "%s: rxdpa 0x%x: addrext not supported\n",
694 di->name, (u32)di->rxdpa);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200695 goto fail;
696 }
697 }
698
Seth Forsheee041f652012-11-15 08:07:56 -0600699 /* Initialize AMPDU session */
700 brcms_c_ampdu_reset_session(&di->ampdu_session, wlc);
701
Seth Forshee90123e02012-11-15 08:08:07 -0600702 brcms_dbg_dma(di->core,
703 "ddoffsetlow 0x%x ddoffsethigh 0x%x dataoffsetlow 0x%x dataoffsethigh 0x%x addrext %d\n",
704 di->ddoffsetlow, di->ddoffsethigh,
705 di->dataoffsetlow, di->dataoffsethigh,
706 di->addrext);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200707
708 return (struct dma_pub *) di;
709
710 fail:
711 dma_detach((struct dma_pub *)di);
712 return NULL;
713}
714
715static inline void
716dma64_dd_upd(struct dma_info *di, struct dma64desc *ddring,
717 dma_addr_t pa, uint outidx, u32 *flags, u32 bufcount)
718{
719 u32 ctrl2 = bufcount & D64_CTRL2_BC_MASK;
720
721 /* PCI bus with big(>1G) physical address, use address extension */
722 if ((di->dataoffsetlow == 0) || !(pa & PCI32ADDR_HIGH)) {
723 ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
724 ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
725 ddring[outidx].ctrl1 = cpu_to_le32(*flags);
726 ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
727 } else {
728 /* address extension for 32-bit PCI */
729 u32 ae;
730
731 ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
732 pa &= ~PCI32ADDR_HIGH;
733
734 ctrl2 |= (ae << D64_CTRL2_AE_SHIFT) & D64_CTRL2_AE;
735 ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
736 ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
737 ddring[outidx].ctrl1 = cpu_to_le32(*flags);
738 ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
739 }
740 if (di->dma.dmactrlflags & DMA_CTRL_PEN) {
741 if (dma64_dd_parity(&ddring[outidx]))
742 ddring[outidx].ctrl2 =
743 cpu_to_le32(ctrl2 | D64_CTRL2_PARITY);
744 }
745}
746
747/* !! may be called with core in reset */
748void dma_detach(struct dma_pub *pub)
749{
750 struct dma_info *di = (struct dma_info *)pub;
751
Seth Forshee90123e02012-11-15 08:08:07 -0600752 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200753
754 /* free dma descriptor rings */
755 if (di->txd64)
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800756 dma_free_coherent(di->dmadev, di->txdalloc,
757 ((s8 *)di->txd64 - di->txdalign),
758 (di->txdpaorig));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200759 if (di->rxd64)
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800760 dma_free_coherent(di->dmadev, di->rxdalloc,
761 ((s8 *)di->rxd64 - di->rxdalign),
762 (di->rxdpaorig));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200763
764 /* free packet pointer vectors */
765 kfree(di->txp);
766 kfree(di->rxp);
767
768 /* free our private info structure */
769 kfree(di);
770
771}
772
773/* initialize descriptor table base address */
774static void
775_dma_ddtable_init(struct dma_info *di, uint direction, dma_addr_t pa)
776{
777 if (!di->aligndesc_4k) {
778 if (direction == DMA_TX)
779 di->xmtptrbase = pa;
780 else
781 di->rcvptrbase = pa;
782 }
783
784 if ((di->ddoffsetlow == 0)
785 || !(pa & PCI32ADDR_HIGH)) {
786 if (direction == DMA_TX) {
Arend van Spriel3b758a62011-12-12 15:15:09 -0800787 bcma_write32(di->core, DMA64TXREGOFFS(di, addrlow),
Arend van Spriele81da652011-12-08 15:06:53 -0800788 pa + di->ddoffsetlow);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800789 bcma_write32(di->core, DMA64TXREGOFFS(di, addrhigh),
Arend van Spriele81da652011-12-08 15:06:53 -0800790 di->ddoffsethigh);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200791 } else {
Arend van Spriel3b758a62011-12-12 15:15:09 -0800792 bcma_write32(di->core, DMA64RXREGOFFS(di, addrlow),
Arend van Spriele81da652011-12-08 15:06:53 -0800793 pa + di->ddoffsetlow);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800794 bcma_write32(di->core, DMA64RXREGOFFS(di, addrhigh),
Arend van Spriele81da652011-12-08 15:06:53 -0800795 di->ddoffsethigh);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200796 }
797 } else {
798 /* DMA64 32bits address extension */
799 u32 ae;
800
801 /* shift the high bit(s) from pa to ae */
802 ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
803 pa &= ~PCI32ADDR_HIGH;
804
805 if (direction == DMA_TX) {
Arend van Spriel3b758a62011-12-12 15:15:09 -0800806 bcma_write32(di->core, DMA64TXREGOFFS(di, addrlow),
Arend van Spriele81da652011-12-08 15:06:53 -0800807 pa + di->ddoffsetlow);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800808 bcma_write32(di->core, DMA64TXREGOFFS(di, addrhigh),
Arend van Spriele81da652011-12-08 15:06:53 -0800809 di->ddoffsethigh);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800810 bcma_maskset32(di->core, DMA64TXREGOFFS(di, control),
Arend van Spriele81da652011-12-08 15:06:53 -0800811 D64_XC_AE, (ae << D64_XC_AE_SHIFT));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200812 } else {
Arend van Spriel3b758a62011-12-12 15:15:09 -0800813 bcma_write32(di->core, DMA64RXREGOFFS(di, addrlow),
Arend van Spriele81da652011-12-08 15:06:53 -0800814 pa + di->ddoffsetlow);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800815 bcma_write32(di->core, DMA64RXREGOFFS(di, addrhigh),
Arend van Spriele81da652011-12-08 15:06:53 -0800816 di->ddoffsethigh);
Arend van Spriel3b758a62011-12-12 15:15:09 -0800817 bcma_maskset32(di->core, DMA64RXREGOFFS(di, control),
Arend van Spriele81da652011-12-08 15:06:53 -0800818 D64_RC_AE, (ae << D64_RC_AE_SHIFT));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200819 }
820 }
821}
822
823static void _dma_rxenable(struct dma_info *di)
824{
825 uint dmactrlflags = di->dma.dmactrlflags;
826 u32 control;
827
Seth Forshee90123e02012-11-15 08:08:07 -0600828 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200829
Arend van Spriel3b758a62011-12-12 15:15:09 -0800830 control = D64_RC_RE | (bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -0800831 DMA64RXREGOFFS(di, control)) &
832 D64_RC_AE);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200833
834 if ((dmactrlflags & DMA_CTRL_PEN) == 0)
835 control |= D64_RC_PD;
836
837 if (dmactrlflags & DMA_CTRL_ROC)
838 control |= D64_RC_OC;
839
Arend van Spriel3b758a62011-12-12 15:15:09 -0800840 bcma_write32(di->core, DMA64RXREGOFFS(di, control),
Arend van Spriel5b435de2011-10-05 13:19:03 +0200841 ((di->rxoffset << D64_RC_RO_SHIFT) | control));
842}
843
844void dma_rxinit(struct dma_pub *pub)
845{
846 struct dma_info *di = (struct dma_info *)pub;
847
Seth Forshee90123e02012-11-15 08:08:07 -0600848 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200849
850 if (di->nrxd == 0)
851 return;
852
853 di->rxin = di->rxout = 0;
854
855 /* clear rx descriptor ring */
856 memset(di->rxd64, '\0', di->nrxd * sizeof(struct dma64desc));
857
858 /* DMA engine with out alignment requirement requires table to be inited
859 * before enabling the engine
860 */
861 if (!di->aligndesc_4k)
862 _dma_ddtable_init(di, DMA_RX, di->rxdpa);
863
864 _dma_rxenable(di);
865
866 if (di->aligndesc_4k)
867 _dma_ddtable_init(di, DMA_RX, di->rxdpa);
868}
869
870static struct sk_buff *dma64_getnextrxp(struct dma_info *di, bool forceall)
871{
872 uint i, curr;
873 struct sk_buff *rxp;
874 dma_addr_t pa;
875
876 i = di->rxin;
877
878 /* return if no packets posted */
879 if (i == di->rxout)
880 return NULL;
881
882 curr =
Arend van Spriel3b758a62011-12-12 15:15:09 -0800883 B2I(((bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -0800884 DMA64RXREGOFFS(di, status0)) & D64_RS0_CD_MASK) -
Arend van Spriel5b435de2011-10-05 13:19:03 +0200885 di->rcvptrbase) & D64_RS0_CD_MASK, struct dma64desc);
886
887 /* ignore curr if forceall */
888 if (!forceall && (i == curr))
889 return NULL;
890
891 /* get the packet pointer that corresponds to the rx descriptor */
892 rxp = di->rxp[i];
893 di->rxp[i] = NULL;
894
895 pa = le32_to_cpu(di->rxd64[i].addrlow) - di->dataoffsetlow;
896
897 /* clear this packet from the descriptor ring */
Arend van Spriel2e81b9b2011-12-08 15:06:52 -0800898 dma_unmap_single(di->dmadev, pa, di->rxbufsize, DMA_FROM_DEVICE);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200899
900 di->rxd64[i].addrlow = cpu_to_le32(0xdeadbeef);
901 di->rxd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
902
903 di->rxin = nextrxd(di, i);
904
905 return rxp;
906}
907
908static struct sk_buff *_dma_getnextrxp(struct dma_info *di, bool forceall)
909{
910 if (di->nrxd == 0)
911 return NULL;
912
913 return dma64_getnextrxp(di, forceall);
914}
915
916/*
917 * !! rx entry routine
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200918 * returns the number packages in the next frame, or 0 if there are no more
Arend van Spriel5b435de2011-10-05 13:19:03 +0200919 * if DMA_CTRL_RXMULTI is defined, DMA scattering(multiple buffers) is
920 * supported with pkts chain
921 * otherwise, it's treated as giant pkt and will be tossed.
922 * The DMA scattering starts with normal DMA header, followed by first
923 * buffer data. After it reaches the max size of buffer, the data continues
924 * in next DMA descriptor buffer WITHOUT DMA header
925 */
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200926int dma_rx(struct dma_pub *pub, struct sk_buff_head *skb_list)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200927{
928 struct dma_info *di = (struct dma_info *)pub;
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200929 struct sk_buff_head dma_frames;
930 struct sk_buff *p, *next;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200931 uint len;
932 uint pkt_len;
933 int resid = 0;
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200934 int pktcnt = 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200935
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200936 skb_queue_head_init(&dma_frames);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200937 next_frame:
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200938 p = _dma_getnextrxp(di, false);
939 if (p == NULL)
940 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200941
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200942 len = le16_to_cpu(*(__le16 *) (p->data));
Seth Forshee90123e02012-11-15 08:08:07 -0600943 brcms_dbg_dma(di->core, "%s: dma_rx len %d\n", di->name, len);
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200944 dma_spin_for_len(len, p);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200945
946 /* set actual length */
947 pkt_len = min((di->rxoffset + len), di->rxbufsize);
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200948 __skb_trim(p, pkt_len);
949 skb_queue_tail(&dma_frames, p);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200950 resid = len - (di->rxbufsize - di->rxoffset);
951
952 /* check for single or multi-buffer rx */
953 if (resid > 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200954 while ((resid > 0) && (p = _dma_getnextrxp(di, false))) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200955 pkt_len = min_t(uint, resid, di->rxbufsize);
956 __skb_trim(p, pkt_len);
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200957 skb_queue_tail(&dma_frames, p);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200958 resid -= di->rxbufsize;
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200959 pktcnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200960 }
961
Joe Perches8ae74652012-01-15 00:38:38 -0800962#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200963 if (resid > 0) {
964 uint cur;
965 cur =
Arend van Spriel3b758a62011-12-12 15:15:09 -0800966 B2I(((bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -0800967 DMA64RXREGOFFS(di, status0)) &
968 D64_RS0_CD_MASK) - di->rcvptrbase) &
969 D64_RS0_CD_MASK, struct dma64desc);
Seth Forshee90123e02012-11-15 08:08:07 -0600970 brcms_dbg_dma(di->core,
971 "rxin %d rxout %d, hw_curr %d\n",
972 di->rxin, di->rxout, cur);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200973 }
Joe Perches8ae74652012-01-15 00:38:38 -0800974#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200975
976 if ((di->dma.dmactrlflags & DMA_CTRL_RXMULTI) == 0) {
Seth Forshee90123e02012-11-15 08:08:07 -0600977 brcms_dbg_dma(di->core, "%s: bad frame length (%d)\n",
978 di->name, len);
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200979 skb_queue_walk_safe(&dma_frames, p, next) {
980 skb_unlink(p, &dma_frames);
981 brcmu_pkt_buf_free_skb(p);
982 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200983 di->dma.rxgiants++;
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200984 pktcnt = 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200985 goto next_frame;
986 }
987 }
988
Arend van Spriel3fd172d2011-10-21 16:16:31 +0200989 skb_queue_splice_tail(&dma_frames, skb_list);
990 return pktcnt;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200991}
992
993static bool dma64_rxidle(struct dma_info *di)
994{
Seth Forshee90123e02012-11-15 08:08:07 -0600995 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200996
997 if (di->nrxd == 0)
998 return true;
999
Arend van Spriel3b758a62011-12-12 15:15:09 -08001000 return ((bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -08001001 DMA64RXREGOFFS(di, status0)) & D64_RS0_CD_MASK) ==
Arend van Spriel3b758a62011-12-12 15:15:09 -08001002 (bcma_read32(di->core, DMA64RXREGOFFS(di, ptr)) &
Arend van Spriele81da652011-12-08 15:06:53 -08001003 D64_RS0_CD_MASK));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001004}
1005
Seth Forsheee041f652012-11-15 08:07:56 -06001006static bool dma64_txidle(struct dma_info *di)
1007{
1008 if (di->ntxd == 0)
1009 return true;
1010
1011 return ((bcma_read32(di->core,
1012 DMA64TXREGOFFS(di, status0)) & D64_XS0_CD_MASK) ==
1013 (bcma_read32(di->core, DMA64TXREGOFFS(di, ptr)) &
1014 D64_XS0_CD_MASK));
1015}
1016
Arend van Spriel5b435de2011-10-05 13:19:03 +02001017/*
1018 * post receive buffers
1019 * return false is refill failed completely and ring is empty this will stall
1020 * the rx dma and user might want to call rxfill again asap. This unlikely
1021 * happens on memory-rich NIC, but often on memory-constrained dongle
1022 */
1023bool dma_rxfill(struct dma_pub *pub)
1024{
1025 struct dma_info *di = (struct dma_info *)pub;
1026 struct sk_buff *p;
1027 u16 rxin, rxout;
1028 u32 flags = 0;
1029 uint n;
1030 uint i;
1031 dma_addr_t pa;
1032 uint extra_offset = 0;
1033 bool ring_empty;
1034
1035 ring_empty = false;
1036
1037 /*
1038 * Determine how many receive buffers we're lacking
1039 * from the full complement, allocate, initialize,
1040 * and post them, then update the chip rx lastdscr.
1041 */
1042
1043 rxin = di->rxin;
1044 rxout = di->rxout;
1045
1046 n = di->nrxpost - nrxdactive(di, rxin, rxout);
1047
Seth Forshee90123e02012-11-15 08:08:07 -06001048 brcms_dbg_dma(di->core, "%s: post %d\n", di->name, n);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001049
1050 if (di->rxbufsize > BCMEXTRAHDROOM)
1051 extra_offset = di->rxextrahdrroom;
1052
1053 for (i = 0; i < n; i++) {
1054 /*
1055 * the di->rxbufsize doesn't include the extra headroom,
1056 * we need to add it to the size to be allocated
1057 */
1058 p = brcmu_pkt_buf_get_skb(di->rxbufsize + extra_offset);
1059
1060 if (p == NULL) {
Seth Forshee90123e02012-11-15 08:08:07 -06001061 brcms_dbg_dma(di->core, "%s: out of rxbufs\n",
1062 di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001063 if (i == 0 && dma64_rxidle(di)) {
Seth Forshee90123e02012-11-15 08:08:07 -06001064 brcms_dbg_dma(di->core, "%s: ring is empty !\n",
1065 di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001066 ring_empty = true;
1067 }
1068 di->dma.rxnobuf++;
1069 break;
1070 }
1071 /* reserve an extra headroom, if applicable */
1072 if (extra_offset)
1073 skb_pull(p, extra_offset);
1074
1075 /* Do a cached write instead of uncached write since DMA_MAP
1076 * will flush the cache.
1077 */
1078 *(u32 *) (p->data) = 0;
1079
Arend van Spriel2e81b9b2011-12-08 15:06:52 -08001080 pa = dma_map_single(di->dmadev, p->data, di->rxbufsize,
1081 DMA_FROM_DEVICE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001082
1083 /* save the free packet pointer */
1084 di->rxp[rxout] = p;
1085
1086 /* reset flags for each descriptor */
1087 flags = 0;
1088 if (rxout == (di->nrxd - 1))
1089 flags = D64_CTRL1_EOT;
1090
1091 dma64_dd_upd(di, di->rxd64, pa, rxout, &flags,
1092 di->rxbufsize);
1093 rxout = nextrxd(di, rxout);
1094 }
1095
1096 di->rxout = rxout;
1097
1098 /* update the chip lastdscr pointer */
Arend van Spriel3b758a62011-12-12 15:15:09 -08001099 bcma_write32(di->core, DMA64RXREGOFFS(di, ptr),
Arend van Spriel5b435de2011-10-05 13:19:03 +02001100 di->rcvptrbase + I2B(rxout, struct dma64desc));
1101
1102 return ring_empty;
1103}
1104
1105void dma_rxreclaim(struct dma_pub *pub)
1106{
1107 struct dma_info *di = (struct dma_info *)pub;
1108 struct sk_buff *p;
1109
Seth Forshee90123e02012-11-15 08:08:07 -06001110 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001111
1112 while ((p = _dma_getnextrxp(di, true)))
1113 brcmu_pkt_buf_free_skb(p);
1114}
1115
1116void dma_counterreset(struct dma_pub *pub)
1117{
1118 /* reset all software counters */
1119 pub->rxgiants = 0;
1120 pub->rxnobuf = 0;
1121 pub->txnobuf = 0;
1122}
1123
1124/* get the address of the var in order to change later */
1125unsigned long dma_getvar(struct dma_pub *pub, const char *name)
1126{
1127 struct dma_info *di = (struct dma_info *)pub;
1128
1129 if (!strcmp(name, "&txavail"))
1130 return (unsigned long)&(di->dma.txavail);
1131 return 0;
1132}
1133
1134/* 64-bit DMA functions */
1135
1136void dma_txinit(struct dma_pub *pub)
1137{
1138 struct dma_info *di = (struct dma_info *)pub;
1139 u32 control = D64_XC_XE;
1140
Seth Forshee90123e02012-11-15 08:08:07 -06001141 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001142
1143 if (di->ntxd == 0)
1144 return;
1145
1146 di->txin = di->txout = 0;
1147 di->dma.txavail = di->ntxd - 1;
1148
1149 /* clear tx descriptor ring */
1150 memset(di->txd64, '\0', (di->ntxd * sizeof(struct dma64desc)));
1151
1152 /* DMA engine with out alignment requirement requires table to be inited
1153 * before enabling the engine
1154 */
1155 if (!di->aligndesc_4k)
1156 _dma_ddtable_init(di, DMA_TX, di->txdpa);
1157
1158 if ((di->dma.dmactrlflags & DMA_CTRL_PEN) == 0)
1159 control |= D64_XC_PD;
Arend van Spriel3b758a62011-12-12 15:15:09 -08001160 bcma_set32(di->core, DMA64TXREGOFFS(di, control), control);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001161
1162 /* DMA engine with alignment requirement requires table to be inited
1163 * before enabling the engine
1164 */
1165 if (di->aligndesc_4k)
1166 _dma_ddtable_init(di, DMA_TX, di->txdpa);
1167}
1168
1169void dma_txsuspend(struct dma_pub *pub)
1170{
1171 struct dma_info *di = (struct dma_info *)pub;
1172
Seth Forshee90123e02012-11-15 08:08:07 -06001173 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001174
1175 if (di->ntxd == 0)
1176 return;
1177
Arend van Spriel3b758a62011-12-12 15:15:09 -08001178 bcma_set32(di->core, DMA64TXREGOFFS(di, control), D64_XC_SE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001179}
1180
1181void dma_txresume(struct dma_pub *pub)
1182{
1183 struct dma_info *di = (struct dma_info *)pub;
1184
Seth Forshee90123e02012-11-15 08:08:07 -06001185 brcms_dbg_dma(di->core, "%s:\n", di->name);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001186
1187 if (di->ntxd == 0)
1188 return;
1189
Arend van Spriel3b758a62011-12-12 15:15:09 -08001190 bcma_mask32(di->core, DMA64TXREGOFFS(di, control), ~D64_XC_SE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001191}
1192
1193bool dma_txsuspended(struct dma_pub *pub)
1194{
1195 struct dma_info *di = (struct dma_info *)pub;
1196
1197 return (di->ntxd == 0) ||
Arend van Spriel3b758a62011-12-12 15:15:09 -08001198 ((bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -08001199 DMA64TXREGOFFS(di, control)) & D64_XC_SE) ==
1200 D64_XC_SE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001201}
1202
1203void dma_txreclaim(struct dma_pub *pub, enum txd_range range)
1204{
1205 struct dma_info *di = (struct dma_info *)pub;
1206 struct sk_buff *p;
1207
Seth Forshee90123e02012-11-15 08:08:07 -06001208 brcms_dbg_dma(di->core, "%s: %s\n",
1209 di->name,
1210 range == DMA_RANGE_ALL ? "all" :
1211 range == DMA_RANGE_TRANSMITTED ? "transmitted" :
1212 "transferred");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001213
1214 if (di->txin == di->txout)
1215 return;
1216
1217 while ((p = dma_getnexttxp(pub, range))) {
1218 /* For unframed data, we don't have any packets to free */
1219 if (!(di->dma.dmactrlflags & DMA_CTRL_UNFRAMED))
1220 brcmu_pkt_buf_free_skb(p);
1221 }
1222}
1223
1224bool dma_txreset(struct dma_pub *pub)
1225{
1226 struct dma_info *di = (struct dma_info *)pub;
1227 u32 status;
1228
1229 if (di->ntxd == 0)
1230 return true;
1231
1232 /* suspend tx DMA first */
Arend van Spriel3b758a62011-12-12 15:15:09 -08001233 bcma_write32(di->core, DMA64TXREGOFFS(di, control), D64_XC_SE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001234 SPINWAIT(((status =
Arend van Spriel3b758a62011-12-12 15:15:09 -08001235 (bcma_read32(di->core, DMA64TXREGOFFS(di, status0)) &
Arend van Spriele81da652011-12-08 15:06:53 -08001236 D64_XS0_XS_MASK)) != D64_XS0_XS_DISABLED) &&
1237 (status != D64_XS0_XS_IDLE) && (status != D64_XS0_XS_STOPPED),
1238 10000);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001239
Arend van Spriel3b758a62011-12-12 15:15:09 -08001240 bcma_write32(di->core, DMA64TXREGOFFS(di, control), 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001241 SPINWAIT(((status =
Arend van Spriel3b758a62011-12-12 15:15:09 -08001242 (bcma_read32(di->core, DMA64TXREGOFFS(di, status0)) &
Arend van Spriele81da652011-12-08 15:06:53 -08001243 D64_XS0_XS_MASK)) != D64_XS0_XS_DISABLED), 10000);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001244
1245 /* wait for the last transaction to complete */
1246 udelay(300);
1247
1248 return status == D64_XS0_XS_DISABLED;
1249}
1250
1251bool dma_rxreset(struct dma_pub *pub)
1252{
1253 struct dma_info *di = (struct dma_info *)pub;
1254 u32 status;
1255
1256 if (di->nrxd == 0)
1257 return true;
1258
Arend van Spriel3b758a62011-12-12 15:15:09 -08001259 bcma_write32(di->core, DMA64RXREGOFFS(di, control), 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001260 SPINWAIT(((status =
Arend van Spriel3b758a62011-12-12 15:15:09 -08001261 (bcma_read32(di->core, DMA64RXREGOFFS(di, status0)) &
Arend van Spriele81da652011-12-08 15:06:53 -08001262 D64_RS0_RS_MASK)) != D64_RS0_RS_DISABLED), 10000);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001263
1264 return status == D64_RS0_RS_DISABLED;
1265}
1266
Seth Forsheee041f652012-11-15 08:07:56 -06001267static void dma_txenq(struct dma_info *di, struct sk_buff *p)
Seth Forshee05f8a612012-11-15 08:07:53 -06001268{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001269 unsigned char *data;
1270 uint len;
1271 u16 txout;
1272 u32 flags = 0;
1273 dma_addr_t pa;
1274
Arend van Spriel5b435de2011-10-05 13:19:03 +02001275 txout = di->txout;
1276
Seth Forsheee041f652012-11-15 08:07:56 -06001277 if (WARN_ON(nexttxd(di, txout) == di->txin))
1278 return;
1279
Arend van Spriel5b435de2011-10-05 13:19:03 +02001280 /*
Arend van Spriel30307942011-11-22 17:21:37 -08001281 * obtain and initialize transmit descriptor entry.
Arend van Spriel5b435de2011-10-05 13:19:03 +02001282 */
Arend van Spriel30307942011-11-22 17:21:37 -08001283 data = p->data;
1284 len = p->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001285
Arend van Spriel30307942011-11-22 17:21:37 -08001286 /* get physical address of buffer start */
Arend van Spriel2e81b9b2011-12-08 15:06:52 -08001287 pa = dma_map_single(di->dmadev, data, len, DMA_TO_DEVICE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001288
Arend van Spriel30307942011-11-22 17:21:37 -08001289 /* With a DMA segment list, Descriptor table is filled
1290 * using the segment list instead of looping over
1291 * buffers in multi-chain DMA. Therefore, EOF for SGLIST
1292 * is when end of segment list is reached.
1293 */
1294 flags = D64_CTRL1_SOF | D64_CTRL1_IOC | D64_CTRL1_EOF;
1295 if (txout == (di->ntxd - 1))
1296 flags |= D64_CTRL1_EOT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001297
Arend van Spriel30307942011-11-22 17:21:37 -08001298 dma64_dd_upd(di, di->txd64, pa, txout, &flags, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001299
Arend van Spriel30307942011-11-22 17:21:37 -08001300 txout = nexttxd(di, txout);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001301
1302 /* save the packet */
Arend van Spriel30307942011-11-22 17:21:37 -08001303 di->txp[prevtxd(di, txout)] = p;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001304
1305 /* bump the tx descriptor index */
1306 di->txout = txout;
Seth Forsheee041f652012-11-15 08:07:56 -06001307}
Arend van Spriel5b435de2011-10-05 13:19:03 +02001308
Seth Forsheee041f652012-11-15 08:07:56 -06001309static void ampdu_finalize(struct dma_info *di)
1310{
1311 struct brcms_ampdu_session *session = &di->ampdu_session;
1312 struct sk_buff *p;
1313
1314 if (WARN_ON(skb_queue_empty(&session->skb_list)))
1315 return;
1316
1317 brcms_c_ampdu_finalize(session);
1318
1319 while (!skb_queue_empty(&session->skb_list)) {
1320 p = skb_dequeue(&session->skb_list);
1321 dma_txenq(di, p);
1322 }
1323
1324 bcma_write32(di->core, DMA64TXREGOFFS(di, ptr),
1325 di->xmtptrbase + I2B(di->txout, struct dma64desc));
1326 brcms_c_ampdu_reset_session(session, session->wlc);
1327}
1328
1329static void prep_ampdu_frame(struct dma_info *di, struct sk_buff *p)
1330{
1331 struct brcms_ampdu_session *session = &di->ampdu_session;
1332 int ret;
1333
1334 ret = brcms_c_ampdu_add_frame(session, p);
1335 if (ret == -ENOSPC) {
1336 /*
1337 * AMPDU cannot accomodate this frame. Close out the in-
1338 * progress AMPDU session and start a new one.
1339 */
1340 ampdu_finalize(di);
1341 ret = brcms_c_ampdu_add_frame(session, p);
1342 }
1343
1344 WARN_ON(ret);
1345}
1346
1347/* Update count of available tx descriptors based on current DMA state */
1348static void dma_update_txavail(struct dma_info *di)
1349{
1350 /*
1351 * Available space is number of descriptors less the number of
1352 * active descriptors and the number of queued AMPDU frames.
1353 */
1354 di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) -
1355 skb_queue_len(&di->ampdu_session.skb_list) - 1;
1356}
1357
1358/*
1359 * !! tx entry routine
1360 * WARNING: call must check the return value for error.
1361 * the error(toss frames) could be fatal and cause many subsequent hard
1362 * to debug problems
1363 */
1364int dma_txfast(struct brcms_c_info *wlc, struct dma_pub *pub,
1365 struct sk_buff *p)
1366{
1367 struct dma_info *di = (struct dma_info *)pub;
1368 struct brcms_ampdu_session *session = &di->ampdu_session;
1369 struct ieee80211_tx_info *tx_info;
1370 bool is_ampdu;
1371
Seth Forshee90123e02012-11-15 08:08:07 -06001372 brcms_dbg_dma(di->core, "%s:\n", di->name);
Seth Forsheee041f652012-11-15 08:07:56 -06001373
1374 /* no use to transmit a zero length packet */
1375 if (p->len == 0)
1376 return 0;
1377
1378 /* return nonzero if out of tx descriptors */
1379 if (di->dma.txavail == 0 || nexttxd(di, di->txout) == di->txin)
1380 goto outoftxd;
1381
1382 tx_info = IEEE80211_SKB_CB(p);
1383 is_ampdu = tx_info->flags & IEEE80211_TX_CTL_AMPDU;
1384 if (is_ampdu)
1385 prep_ampdu_frame(di, p);
1386 else
1387 dma_txenq(di, p);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001388
1389 /* tx flow control */
Seth Forshee05f8a612012-11-15 08:07:53 -06001390 dma_update_txavail(di);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001391
Seth Forsheee041f652012-11-15 08:07:56 -06001392 /* kick the chip */
1393 if (is_ampdu) {
1394 /*
1395 * Start sending data if we've got a full AMPDU, there's
1396 * no more space in the DMA ring, or the ring isn't
1397 * currently transmitting.
1398 */
1399 if (skb_queue_len(&session->skb_list) == session->max_ampdu_frames ||
1400 di->dma.txavail == 0 || dma64_txidle(di))
1401 ampdu_finalize(di);
1402 } else {
1403 bcma_write32(di->core, DMA64TXREGOFFS(di, ptr),
1404 di->xmtptrbase + I2B(di->txout, struct dma64desc));
1405 }
1406
Arend van Spriel5b435de2011-10-05 13:19:03 +02001407 return 0;
1408
1409 outoftxd:
Seth Forshee90123e02012-11-15 08:08:07 -06001410 brcms_dbg_dma(di->core, "%s: out of txds !!!\n", di->name);
Arend van Spriel30307942011-11-22 17:21:37 -08001411 brcmu_pkt_buf_free_skb(p);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001412 di->dma.txavail = 0;
1413 di->dma.txnobuf++;
Seth Forsheee041f652012-11-15 08:07:56 -06001414 return -ENOSPC;
1415}
1416
1417void dma_txflush(struct dma_pub *pub)
1418{
1419 struct dma_info *di = (struct dma_info *)pub;
1420 struct brcms_ampdu_session *session = &di->ampdu_session;
1421
1422 if (!skb_queue_empty(&session->skb_list))
1423 ampdu_finalize(di);
1424}
1425
1426int dma_txpending(struct dma_pub *pub)
1427{
1428 struct dma_info *di = (struct dma_info *)pub;
1429 return ntxdactive(di, di->txin, di->txout);
1430}
1431
1432/*
1433 * If we have an active AMPDU session and are not transmitting,
1434 * this function will force tx to start.
1435 */
1436void dma_kick_tx(struct dma_pub *pub)
1437{
1438 struct dma_info *di = (struct dma_info *)pub;
1439 struct brcms_ampdu_session *session = &di->ampdu_session;
1440
1441 if (!skb_queue_empty(&session->skb_list) && dma64_txidle(di))
1442 ampdu_finalize(di);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001443}
1444
1445/*
1446 * Reclaim next completed txd (txds if using chained buffers) in the range
1447 * specified and return associated packet.
1448 * If range is DMA_RANGE_TRANSMITTED, reclaim descriptors that have be
1449 * transmitted as noted by the hardware "CurrDescr" pointer.
1450 * If range is DMA_RANGE_TRANSFERED, reclaim descriptors that have be
1451 * transferred by the DMA as noted by the hardware "ActiveDescr" pointer.
1452 * If range is DMA_RANGE_ALL, reclaim all txd(s) posted to the ring and
1453 * return associated packet regardless of the value of hardware pointers.
1454 */
1455struct sk_buff *dma_getnexttxp(struct dma_pub *pub, enum txd_range range)
1456{
1457 struct dma_info *di = (struct dma_info *)pub;
1458 u16 start, end, i;
1459 u16 active_desc;
1460 struct sk_buff *txp;
1461
Seth Forshee90123e02012-11-15 08:08:07 -06001462 brcms_dbg_dma(di->core, "%s: %s\n",
1463 di->name,
1464 range == DMA_RANGE_ALL ? "all" :
1465 range == DMA_RANGE_TRANSMITTED ? "transmitted" :
1466 "transferred");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001467
1468 if (di->ntxd == 0)
1469 return NULL;
1470
1471 txp = NULL;
1472
1473 start = di->txin;
1474 if (range == DMA_RANGE_ALL)
1475 end = di->txout;
1476 else {
Arend van Spriel3b758a62011-12-12 15:15:09 -08001477 end = (u16) (B2I(((bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -08001478 DMA64TXREGOFFS(di, status0)) &
1479 D64_XS0_CD_MASK) - di->xmtptrbase) &
1480 D64_XS0_CD_MASK, struct dma64desc));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001481
1482 if (range == DMA_RANGE_TRANSFERED) {
1483 active_desc =
Arend van Spriel3b758a62011-12-12 15:15:09 -08001484 (u16)(bcma_read32(di->core,
Arend van Spriele81da652011-12-08 15:06:53 -08001485 DMA64TXREGOFFS(di, status1)) &
Arend van Spriel5b435de2011-10-05 13:19:03 +02001486 D64_XS1_AD_MASK);
1487 active_desc =
1488 (active_desc - di->xmtptrbase) & D64_XS0_CD_MASK;
1489 active_desc = B2I(active_desc, struct dma64desc);
1490 if (end != active_desc)
1491 end = prevtxd(di, active_desc);
1492 }
1493 }
1494
1495 if ((start == 0) && (end > di->txout))
1496 goto bogus;
1497
1498 for (i = start; i != end && !txp; i = nexttxd(di, i)) {
1499 dma_addr_t pa;
1500 uint size;
1501
1502 pa = le32_to_cpu(di->txd64[i].addrlow) - di->dataoffsetlow;
1503
1504 size =
1505 (le32_to_cpu(di->txd64[i].ctrl2) &
1506 D64_CTRL2_BC_MASK);
1507
1508 di->txd64[i].addrlow = cpu_to_le32(0xdeadbeef);
1509 di->txd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
1510
1511 txp = di->txp[i];
1512 di->txp[i] = NULL;
1513
Arend van Spriel2e81b9b2011-12-08 15:06:52 -08001514 dma_unmap_single(di->dmadev, pa, size, DMA_TO_DEVICE);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001515 }
1516
1517 di->txin = i;
1518
1519 /* tx flow control */
Seth Forshee05f8a612012-11-15 08:07:53 -06001520 dma_update_txavail(di);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001521
1522 return txp;
1523
1524 bogus:
Seth Forshee90123e02012-11-15 08:08:07 -06001525 brcms_dbg_dma(di->core, "bogus curr: start %d end %d txout %d\n",
1526 start, end, di->txout);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001527 return NULL;
1528}
1529
1530/*
1531 * Mac80211 initiated actions sometimes require packets in the DMA queue to be
1532 * modified. The modified portion of the packet is not under control of the DMA
1533 * engine. This function calls a caller-supplied function for each packet in
1534 * the caller specified dma chain.
1535 */
1536void dma_walk_packets(struct dma_pub *dmah, void (*callback_fnc)
1537 (void *pkt, void *arg_a), void *arg_a)
1538{
1539 struct dma_info *di = (struct dma_info *) dmah;
1540 uint i = di->txin;
1541 uint end = di->txout;
1542 struct sk_buff *skb;
1543 struct ieee80211_tx_info *tx_info;
1544
1545 while (i != end) {
Joe Perches2c208892012-06-04 12:44:17 +00001546 skb = di->txp[i];
Arend van Spriel5b435de2011-10-05 13:19:03 +02001547 if (skb != NULL) {
1548 tx_info = (struct ieee80211_tx_info *)skb->cb;
1549 (callback_fnc)(tx_info, arg_a);
1550 }
1551 i = nexttxd(di, i);
1552 }
1553}