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