fwohci.c revision 110016
1103285Sikob/*
2103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3103285Sikob * All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the acknowledgement as bellow:
15103285Sikob *
16106802Ssimokawa *    This product includes software developed by K. Kobayashi and H. Shimokawa
17103285Sikob *
18103285Sikob * 4. The name of the author may not be used to endorse or promote products
19103285Sikob *    derived from this software without specific prior written permission.
20103285Sikob *
21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31103285Sikob * POSSIBILITY OF SUCH DAMAGE.
32103285Sikob *
33103285Sikob * $FreeBSD: head/sys/dev/firewire/fwohci.c 110016 2003-01-29 02:13:31Z simokawa $
34103285Sikob *
35103285Sikob */
36106802Ssimokawa
37103285Sikob#define ATRQ_CH 0
38103285Sikob#define ATRS_CH 1
39103285Sikob#define ARRQ_CH 2
40103285Sikob#define ARRS_CH 3
41103285Sikob#define ITX_CH 4
42103285Sikob#define IRX_CH 0x24
43103285Sikob
44103285Sikob#include <sys/param.h>
45109890Ssimokawa#include <sys/proc.h>
46103285Sikob#include <sys/systm.h>
47103285Sikob#include <sys/types.h>
48103285Sikob#include <sys/mbuf.h>
49103285Sikob#include <sys/mman.h>
50103285Sikob#include <sys/socket.h>
51103285Sikob#include <sys/socketvar.h>
52103285Sikob#include <sys/signalvar.h>
53103285Sikob#include <sys/malloc.h>
54103285Sikob#include <sys/uio.h>
55103285Sikob#include <sys/sockio.h>
56103285Sikob#include <sys/bus.h>
57103285Sikob#include <sys/kernel.h>
58103285Sikob#include <sys/conf.h>
59103285Sikob
60103285Sikob#include <machine/bus.h>
61103285Sikob#include <machine/resource.h>
62103285Sikob#include <sys/rman.h>
63103285Sikob
64103285Sikob#include <machine/cpufunc.h>            /* for rdtsc proto for clock.h below */
65103285Sikob#include <machine/clock.h>
66103285Sikob#include <pci/pcivar.h>
67103285Sikob#include <pci/pcireg.h>
68103285Sikob#include <vm/vm.h>
69103285Sikob#include <vm/vm_extern.h>
70103285Sikob#include <vm/pmap.h>            /* for vtophys proto */
71103285Sikob
72103285Sikob#include <dev/firewire/firewire.h>
73103285Sikob#include <dev/firewire/firewirereg.h>
74103285Sikob#include <dev/firewire/fwohcireg.h>
75103285Sikob#include <dev/firewire/fwohcivar.h>
76103285Sikob#include <dev/firewire/firewire_phy.h>
77103285Sikob
78109179Ssimokawa#include <dev/firewire/iec68113.h>
79109179Ssimokawa
80103285Sikob#undef OHCI_DEBUG
81106802Ssimokawa
82103285Sikobstatic char dbcode[16][0x10]={"OUTM", "OUTL","INPM","INPL",
83103285Sikob		"STOR","LOAD","NOP ","STOP",};
84103285Sikobstatic char dbkey[8][0x10]={"ST0", "ST1","ST2","ST3",
85103285Sikob		"UNDEF","REG","SYS","DEV"};
86103285Sikobchar fwohcicode[32][0x20]={
87103285Sikob	"No stat","Undef","long","miss Ack err",
88103285Sikob	"underrun","overrun","desc err", "data read err",
89103285Sikob	"data write err","bus reset","timeout","tcode err",
90103285Sikob	"Undef","Undef","unknown event","flushed",
91103285Sikob	"Undef","ack complete","ack pend","Undef",
92103285Sikob	"ack busy_X","ack busy_A","ack busy_B","Undef",
93103285Sikob	"Undef","Undef","Undef","ack tardy",
94103285Sikob	"Undef","ack data_err","ack type_err",""};
95103285Sikob#define MAX_SPEED 2
96103285Sikobextern char linkspeed[MAX_SPEED+1][0x10];
97103285Sikobstatic char dbcond[4][0x10]={"NEV","C=1", "C=0", "ALL"};
98103285Sikobu_int32_t tagbit[4] = { 1 << 28, 1 << 29, 1 << 30, 1 << 31};
99103285Sikob
100103285Sikobstatic struct tcode_info tinfo[] = {
101103285Sikob/*		hdr_len block 	flag*/
102103285Sikob/* 0 WREQQ  */ {16,	FWTI_REQ | FWTI_TLABEL},
103103285Sikob/* 1 WREQB  */ {16,	FWTI_REQ | FWTI_TLABEL | FWTI_BLOCK_ASY},
104103285Sikob/* 2 WRES   */ {12,	FWTI_RES},
105103285Sikob/* 3 XXX    */ { 0,	0},
106103285Sikob/* 4 RREQQ  */ {12,	FWTI_REQ | FWTI_TLABEL},
107103285Sikob/* 5 RREQB  */ {16,	FWTI_REQ | FWTI_TLABEL},
108103285Sikob/* 6 RRESQ  */ {16,	FWTI_RES},
109103285Sikob/* 7 RRESB  */ {16,	FWTI_RES | FWTI_BLOCK_ASY},
110103285Sikob/* 8 CYCS   */ { 0,	0},
111103285Sikob/* 9 LREQ   */ {16,	FWTI_REQ | FWTI_TLABEL | FWTI_BLOCK_ASY},
112103285Sikob/* a STREAM */ { 4,	FWTI_REQ | FWTI_BLOCK_STR},
113103285Sikob/* b LRES   */ {16,	FWTI_RES | FWTI_BLOCK_ASY},
114103285Sikob/* c XXX    */ { 0,	0},
115103285Sikob/* d XXX    */ { 0, 	0},
116103285Sikob/* e PHY    */ {12,	FWTI_REQ},
117103285Sikob/* f XXX    */ { 0,	0}
118103285Sikob};
119103285Sikob
120103285Sikob#define OHCI_WRITE_SIGMASK 0xffff0000
121103285Sikob#define OHCI_READ_SIGMASK 0xffff0000
122103285Sikob
123103285Sikob#define OWRITE(sc, r, x) bus_space_write_4((sc)->bst, (sc)->bsh, (r), (x))
124103285Sikob#define OREAD(sc, r) bus_space_read_4((sc)->bst, (sc)->bsh, (r))
125103285Sikob
126103285Sikobstatic void fwohci_ibr __P((struct firewire_comm *));
127103285Sikobstatic void fwohci_db_init __P((struct fwohci_dbch *));
128103285Sikobstatic void fwohci_db_free __P((struct fwohci_dbch *));
129106789Ssimokawastatic void fwohci_arcv __P((struct fwohci_softc *, struct fwohci_dbch *, int));
130106789Ssimokawastatic void fwohci_ircv __P((struct fwohci_softc *, struct fwohci_dbch *, int));
131103285Sikobstatic void fwohci_txd __P((struct fwohci_softc *, struct fwohci_dbch *));
132103285Sikobstatic void fwohci_start_atq __P((struct firewire_comm *));
133103285Sikobstatic void fwohci_start_ats __P((struct firewire_comm *));
134103285Sikobstatic void fwohci_start __P((struct fwohci_softc *, struct fwohci_dbch *));
135103285Sikobstatic void fwohci_drain_atq __P((struct firewire_comm *, struct fw_xfer *));
136103285Sikobstatic void fwohci_drain_ats __P((struct firewire_comm *, struct fw_xfer *));
137103285Sikobstatic void fwohci_drain __P((struct firewire_comm *, struct fw_xfer *, struct fwohci_dbch *));
138103285Sikobstatic u_int32_t fwphy_wrdata __P(( struct fwohci_softc *, u_int32_t, u_int32_t));
139103285Sikobstatic u_int32_t fwphy_rddata __P(( struct fwohci_softc *, u_int32_t));
140103285Sikobstatic int fwohci_rx_enable __P((struct fwohci_softc *, struct fwohci_dbch *));
141103285Sikobstatic int fwohci_tx_enable __P((struct fwohci_softc *, struct fwohci_dbch *));
142103285Sikobstatic int fwohci_irx_enable __P((struct firewire_comm *, int));
143103285Sikobstatic int fwohci_irxpp_enable __P((struct firewire_comm *, int));
144103285Sikobstatic int fwohci_irxbuf_enable __P((struct firewire_comm *, int));
145103285Sikobstatic int fwohci_irx_disable __P((struct firewire_comm *, int));
146103285Sikobstatic void fwohci_irx_post __P((struct firewire_comm *, u_int32_t *));
147103285Sikobstatic int fwohci_itxbuf_enable __P((struct firewire_comm *, int));
148103285Sikobstatic int fwohci_itx_disable __P((struct firewire_comm *, int));
149103285Sikobstatic void fwohci_timeout __P((void *));
150103285Sikobstatic void fwohci_poll __P((struct firewire_comm *, int, int));
151103285Sikobstatic void fwohci_set_intr __P((struct firewire_comm *, int));
152103285Sikobstatic int fwohci_add_rx_buf __P((struct fwohcidb_tr *, unsigned short, int, void *, void *));
153103285Sikobstatic int fwohci_add_tx_buf __P((struct fwohcidb_tr *, unsigned short, int, void *));
154103285Sikobstatic void	dump_db __P((struct fwohci_softc *, u_int32_t));
155103285Sikobstatic void 	print_db __P((volatile struct fwohcidb *, u_int32_t , u_int32_t));
156103285Sikobstatic void	dump_dma __P((struct fwohci_softc *, u_int32_t));
157103285Sikobstatic u_int32_t fwohci_cyctimer __P((struct firewire_comm *));
158103285Sikobstatic void fwohci_rbuf_update __P((struct fwohci_softc *, int));
159103285Sikobstatic void fwohci_tbuf_update __P((struct fwohci_softc *, int));
160103285Sikobvoid fwohci_txbufdb __P((struct fwohci_softc *, int , struct fw_bulkxfer *));
161103285Sikob
162103285Sikob/*
163103285Sikob * memory allocated for DMA programs
164103285Sikob */
165103285Sikob#define DMA_PROG_ALLOC		(8 * PAGE_SIZE)
166103285Sikob
167103285Sikob/* #define NDB 1024 */
168103285Sikob#define NDB FWMAXQUEUE
169103285Sikob#define NDVDB (DVBUF * NDB)
170103285Sikob
171103285Sikob#define	OHCI_VERSION		0x00
172103285Sikob#define	OHCI_CROMHDR		0x18
173103285Sikob#define	OHCI_BUS_OPT		0x20
174103285Sikob#define	OHCI_BUSIRMC		(1 << 31)
175103285Sikob#define	OHCI_BUSCMC		(1 << 30)
176103285Sikob#define	OHCI_BUSISC		(1 << 29)
177103285Sikob#define	OHCI_BUSBMC		(1 << 28)
178103285Sikob#define	OHCI_BUSPMC		(1 << 27)
179103285Sikob#define OHCI_BUSFNC		OHCI_BUSIRMC | OHCI_BUSCMC | OHCI_BUSISC |\
180103285Sikob				OHCI_BUSBMC | OHCI_BUSPMC
181103285Sikob
182103285Sikob#define	OHCI_EUID_HI		0x24
183103285Sikob#define	OHCI_EUID_LO		0x28
184103285Sikob
185103285Sikob#define	OHCI_CROMPTR		0x34
186103285Sikob#define	OHCI_HCCCTL		0x50
187103285Sikob#define	OHCI_HCCCTLCLR		0x54
188103285Sikob#define	OHCI_AREQHI		0x100
189103285Sikob#define	OHCI_AREQHICLR		0x104
190103285Sikob#define	OHCI_AREQLO		0x108
191103285Sikob#define	OHCI_AREQLOCLR		0x10c
192103285Sikob#define	OHCI_PREQHI		0x110
193103285Sikob#define	OHCI_PREQHICLR		0x114
194103285Sikob#define	OHCI_PREQLO		0x118
195103285Sikob#define	OHCI_PREQLOCLR		0x11c
196103285Sikob#define	OHCI_PREQUPPER		0x120
197103285Sikob
198103285Sikob#define	OHCI_SID_BUF		0x64
199103285Sikob#define	OHCI_SID_CNT		0x68
200103285Sikob#define OHCI_SID_CNT_MASK	0xffc
201103285Sikob
202103285Sikob#define	OHCI_IT_STAT		0x90
203103285Sikob#define	OHCI_IT_STATCLR		0x94
204103285Sikob#define	OHCI_IT_MASK		0x98
205103285Sikob#define	OHCI_IT_MASKCLR		0x9c
206103285Sikob
207103285Sikob#define	OHCI_IR_STAT		0xa0
208103285Sikob#define	OHCI_IR_STATCLR		0xa4
209103285Sikob#define	OHCI_IR_MASK		0xa8
210103285Sikob#define	OHCI_IR_MASKCLR		0xac
211103285Sikob
212103285Sikob#define	OHCI_LNKCTL		0xe0
213103285Sikob#define	OHCI_LNKCTLCLR		0xe4
214103285Sikob
215103285Sikob#define	OHCI_PHYACCESS		0xec
216103285Sikob#define	OHCI_CYCLETIMER		0xf0
217103285Sikob
218103285Sikob#define	OHCI_DMACTL(off)	(off)
219103285Sikob#define	OHCI_DMACTLCLR(off)	(off + 4)
220103285Sikob#define	OHCI_DMACMD(off)	(off + 0xc)
221103285Sikob#define	OHCI_DMAMATCH(off)	(off + 0x10)
222103285Sikob
223103285Sikob#define OHCI_ATQOFF		0x180
224103285Sikob#define OHCI_ATQCTL		OHCI_ATQOFF
225103285Sikob#define OHCI_ATQCTLCLR		(OHCI_ATQOFF + 4)
226103285Sikob#define OHCI_ATQCMD		(OHCI_ATQOFF + 0xc)
227103285Sikob#define OHCI_ATQMATCH		(OHCI_ATQOFF + 0x10)
228103285Sikob
229103285Sikob#define OHCI_ATSOFF		0x1a0
230103285Sikob#define OHCI_ATSCTL		OHCI_ATSOFF
231103285Sikob#define OHCI_ATSCTLCLR		(OHCI_ATSOFF + 4)
232103285Sikob#define OHCI_ATSCMD		(OHCI_ATSOFF + 0xc)
233103285Sikob#define OHCI_ATSMATCH		(OHCI_ATSOFF + 0x10)
234103285Sikob
235103285Sikob#define OHCI_ARQOFF		0x1c0
236103285Sikob#define OHCI_ARQCTL		OHCI_ARQOFF
237103285Sikob#define OHCI_ARQCTLCLR		(OHCI_ARQOFF + 4)
238103285Sikob#define OHCI_ARQCMD		(OHCI_ARQOFF + 0xc)
239103285Sikob#define OHCI_ARQMATCH		(OHCI_ARQOFF + 0x10)
240103285Sikob
241103285Sikob#define OHCI_ARSOFF		0x1e0
242103285Sikob#define OHCI_ARSCTL		OHCI_ARSOFF
243103285Sikob#define OHCI_ARSCTLCLR		(OHCI_ARSOFF + 4)
244103285Sikob#define OHCI_ARSCMD		(OHCI_ARSOFF + 0xc)
245103285Sikob#define OHCI_ARSMATCH		(OHCI_ARSOFF + 0x10)
246103285Sikob
247103285Sikob#define OHCI_ITOFF(CH)		(0x200 + 0x10 * (CH))
248103285Sikob#define OHCI_ITCTL(CH)		(OHCI_ITOFF(CH))
249103285Sikob#define OHCI_ITCTLCLR(CH)	(OHCI_ITOFF(CH) + 4)
250103285Sikob#define OHCI_ITCMD(CH)		(OHCI_ITOFF(CH) + 0xc)
251103285Sikob
252103285Sikob#define OHCI_IROFF(CH)		(0x400 + 0x20 * (CH))
253103285Sikob#define OHCI_IRCTL(CH)		(OHCI_IROFF(CH))
254103285Sikob#define OHCI_IRCTLCLR(CH)	(OHCI_IROFF(CH) + 4)
255103285Sikob#define OHCI_IRCMD(CH)		(OHCI_IROFF(CH) + 0xc)
256103285Sikob#define OHCI_IRMATCH(CH)	(OHCI_IROFF(CH) + 0x10)
257103285Sikob
258103285Sikobd_ioctl_t fwohci_ioctl;
259103285Sikob
260103285Sikob/*
261103285Sikob * Communication with PHY device
262103285Sikob */
263106790Ssimokawastatic u_int32_t
264106790Ssimokawafwphy_wrdata( struct fwohci_softc *sc, u_int32_t addr, u_int32_t data)
265103285Sikob{
266103285Sikob	u_int32_t fun;
267103285Sikob
268103285Sikob	addr &= 0xf;
269103285Sikob	data &= 0xff;
270103285Sikob
271103285Sikob	fun = (PHYDEV_WRCMD | (addr << PHYDEV_REGADDR) | (data << PHYDEV_WRDATA));
272103285Sikob	OWRITE(sc, OHCI_PHYACCESS, fun);
273103285Sikob	DELAY(100);
274103285Sikob
275103285Sikob	return(fwphy_rddata( sc, addr));
276103285Sikob}
277103285Sikob
278103285Sikobstatic u_int32_t
279103285Sikobfwohci_set_bus_manager(struct firewire_comm *fc, u_int node)
280103285Sikob{
281103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
282103285Sikob	int i;
283103285Sikob	u_int32_t bm;
284103285Sikob
285103285Sikob#define OHCI_CSR_DATA	0x0c
286103285Sikob#define OHCI_CSR_COMP	0x10
287103285Sikob#define OHCI_CSR_CONT	0x14
288103285Sikob#define OHCI_BUS_MANAGER_ID	0
289103285Sikob
290103285Sikob	OWRITE(sc, OHCI_CSR_DATA, node);
291103285Sikob	OWRITE(sc, OHCI_CSR_COMP, 0x3f);
292103285Sikob	OWRITE(sc, OHCI_CSR_CONT, OHCI_BUS_MANAGER_ID);
293103285Sikob 	for (i = 0; !(OREAD(sc, OHCI_CSR_CONT) & (1<<31)) && (i < 1000); i++)
294109280Ssimokawa		DELAY(10);
295103285Sikob	bm = OREAD(sc, OHCI_CSR_DATA);
296107653Ssimokawa	if((bm & 0x3f) == 0x3f)
297103285Sikob		bm = node;
298107653Ssimokawa	if (bootverbose)
299107653Ssimokawa		device_printf(sc->fc.dev,
300107653Ssimokawa			"fw_set_bus_manager: %d->%d (loop=%d)\n", bm, node, i);
301103285Sikob
302103285Sikob	return(bm);
303103285Sikob}
304103285Sikob
305106790Ssimokawastatic u_int32_t
306106790Ssimokawafwphy_rddata(struct fwohci_softc *sc,  u_int addr)
307103285Sikob{
308108500Ssimokawa	u_int32_t fun, stat;
309108500Ssimokawa	u_int i, retry = 0;
310103285Sikob
311103285Sikob	addr &= 0xf;
312108500Ssimokawa#define MAX_RETRY 100
313108500Ssimokawaagain:
314108500Ssimokawa	OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_REG_FAIL);
315103285Sikob	fun = PHYDEV_RDCMD | (addr << PHYDEV_REGADDR);
316103285Sikob	OWRITE(sc, OHCI_PHYACCESS, fun);
317108500Ssimokawa	for ( i = 0 ; i < MAX_RETRY ; i ++ ){
318103285Sikob		fun = OREAD(sc, OHCI_PHYACCESS);
319103285Sikob		if ((fun & PHYDEV_RDCMD) == 0 && (fun & PHYDEV_RDDONE) != 0)
320103285Sikob			break;
321109280Ssimokawa		DELAY(100);
322103285Sikob	}
323108500Ssimokawa	if(i >= MAX_RETRY) {
324109280Ssimokawa		if (bootverbose)
325109280Ssimokawa			device_printf(sc->fc.dev, "phy read failed(1).\n");
326108527Ssimokawa		if (++retry < MAX_RETRY) {
327109280Ssimokawa			DELAY(100);
328108527Ssimokawa			goto again;
329108527Ssimokawa		}
330108500Ssimokawa	}
331108500Ssimokawa	/* Make sure that SCLK is started */
332108500Ssimokawa	stat = OREAD(sc, FWOHCI_INTSTAT);
333108500Ssimokawa	if ((stat & OHCI_INT_REG_FAIL) != 0 ||
334108500Ssimokawa			((fun >> PHYDEV_REGADDR) & 0xf) != addr) {
335109280Ssimokawa		if (bootverbose)
336109280Ssimokawa			device_printf(sc->fc.dev, "phy read failed(2).\n");
337108500Ssimokawa		if (++retry < MAX_RETRY) {
338109280Ssimokawa			DELAY(100);
339108500Ssimokawa			goto again;
340108500Ssimokawa		}
341108500Ssimokawa	}
342108500Ssimokawa	if (bootverbose || retry >= MAX_RETRY)
343108500Ssimokawa		device_printf(sc->fc.dev,
344108500Ssimokawa			"fwphy_rddata: loop=%d, retry=%d\n", i, retry);
345108500Ssimokawa#undef MAX_RETRY
346103285Sikob	return((fun >> PHYDEV_RDDATA )& 0xff);
347103285Sikob}
348103285Sikob/* Device specific ioctl. */
349103285Sikobint
350103285Sikobfwohci_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
351103285Sikob{
352103285Sikob	struct firewire_softc *sc;
353103285Sikob	struct fwohci_softc *fc;
354103285Sikob	int unit = DEV2UNIT(dev);
355103285Sikob	int err = 0;
356103285Sikob	struct fw_reg_req_t *reg  = (struct fw_reg_req_t *) data;
357103285Sikob	u_int32_t *dmach = (u_int32_t *) data;
358103285Sikob
359103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
360103285Sikob	if(sc == NULL){
361103285Sikob		return(EINVAL);
362103285Sikob	}
363103285Sikob	fc = (struct fwohci_softc *)sc->fc;
364103285Sikob
365103285Sikob	if (!data)
366103285Sikob		return(EINVAL);
367103285Sikob
368103285Sikob	switch (cmd) {
369103285Sikob	case FWOHCI_WRREG:
370103285Sikob#define OHCI_MAX_REG 0x800
371103285Sikob		if(reg->addr <= OHCI_MAX_REG){
372103285Sikob			OWRITE(fc, reg->addr, reg->data);
373103285Sikob			reg->data = OREAD(fc, reg->addr);
374103285Sikob		}else{
375103285Sikob			err = EINVAL;
376103285Sikob		}
377103285Sikob		break;
378103285Sikob	case FWOHCI_RDREG:
379103285Sikob		if(reg->addr <= OHCI_MAX_REG){
380103285Sikob			reg->data = OREAD(fc, reg->addr);
381103285Sikob		}else{
382103285Sikob			err = EINVAL;
383103285Sikob		}
384103285Sikob		break;
385103285Sikob/* Read DMA descriptors for debug  */
386103285Sikob	case DUMPDMA:
387103285Sikob		if(*dmach <= OHCI_MAX_DMA_CH ){
388103285Sikob			dump_dma(fc, *dmach);
389103285Sikob			dump_db(fc, *dmach);
390103285Sikob		}else{
391103285Sikob			err = EINVAL;
392103285Sikob		}
393103285Sikob		break;
394103285Sikob	default:
395103285Sikob		break;
396103285Sikob	}
397103285Sikob	return err;
398103285Sikob}
399106790Ssimokawa
400108530Ssimokawastatic int
401108530Ssimokawafwohci_probe_phy(struct fwohci_softc *sc, device_t dev)
402103285Sikob{
403108530Ssimokawa	u_int32_t reg, reg2;
404108530Ssimokawa	int e1394a = 1;
405108530Ssimokawa/*
406108530Ssimokawa * probe PHY parameters
407108530Ssimokawa * 0. to prove PHY version, whether compliance of 1394a.
408108530Ssimokawa * 1. to probe maximum speed supported by the PHY and
409108530Ssimokawa *    number of port supported by core-logic.
410108530Ssimokawa *    It is not actually available port on your PC .
411108530Ssimokawa */
412108530Ssimokawa	OWRITE(sc, OHCI_HCCCTL, OHCI_HCC_LPS);
413108530Ssimokawa#if 0
414108530Ssimokawa	/* XXX wait for SCLK. */
415108530Ssimokawa	DELAY(100000);
416108530Ssimokawa#endif
417108530Ssimokawa	reg = fwphy_rddata(sc, FW_PHY_SPD_REG);
418108530Ssimokawa
419108530Ssimokawa	if((reg >> 5) != 7 ){
420108530Ssimokawa		sc->fc.mode &= ~FWPHYASYST;
421108530Ssimokawa		sc->fc.nport = reg & FW_PHY_NP;
422108530Ssimokawa		sc->fc.speed = reg & FW_PHY_SPD >> 6;
423108530Ssimokawa		if (sc->fc.speed > MAX_SPEED) {
424108530Ssimokawa			device_printf(dev, "invalid speed %d (fixed to %d).\n",
425108530Ssimokawa				sc->fc.speed, MAX_SPEED);
426108530Ssimokawa			sc->fc.speed = MAX_SPEED;
427108530Ssimokawa		}
428108530Ssimokawa		device_printf(dev,
429108701Ssimokawa			"Phy 1394 only %s, %d ports.\n",
430108701Ssimokawa			linkspeed[sc->fc.speed], sc->fc.nport);
431108530Ssimokawa	}else{
432108530Ssimokawa		reg2 = fwphy_rddata(sc, FW_PHY_ESPD_REG);
433108530Ssimokawa		sc->fc.mode |= FWPHYASYST;
434108530Ssimokawa		sc->fc.nport = reg & FW_PHY_NP;
435108530Ssimokawa		sc->fc.speed = (reg2 & FW_PHY_ESPD) >> 5;
436108530Ssimokawa		if (sc->fc.speed > MAX_SPEED) {
437108530Ssimokawa			device_printf(dev, "invalid speed %d (fixed to %d).\n",
438108530Ssimokawa				sc->fc.speed, MAX_SPEED);
439108530Ssimokawa			sc->fc.speed = MAX_SPEED;
440108530Ssimokawa		}
441108530Ssimokawa		device_printf(dev,
442108701Ssimokawa			"Phy 1394a available %s, %d ports.\n",
443108701Ssimokawa			linkspeed[sc->fc.speed], sc->fc.nport);
444108530Ssimokawa
445108530Ssimokawa		/* check programPhyEnable */
446108530Ssimokawa		reg2 = fwphy_rddata(sc, 5);
447108530Ssimokawa#if 0
448108530Ssimokawa		if (e1394a && (OREAD(sc, OHCI_HCCCTL) & OHCI_HCC_PRPHY)) {
449108530Ssimokawa#else	/* XXX force to enable 1394a */
450108530Ssimokawa		if (e1394a) {
451108530Ssimokawa#endif
452108530Ssimokawa			if (bootverbose)
453108530Ssimokawa				device_printf(dev,
454108530Ssimokawa					"Enable 1394a Enhancements\n");
455108530Ssimokawa			/* enable EAA EMC */
456108530Ssimokawa			reg2 |= 0x03;
457108530Ssimokawa			/* set aPhyEnhanceEnable */
458108530Ssimokawa			OWRITE(sc, OHCI_HCCCTL, OHCI_HCC_PHYEN);
459108530Ssimokawa			OWRITE(sc, OHCI_HCCCTLCLR, OHCI_HCC_PRPHY);
460108530Ssimokawa		} else {
461108530Ssimokawa			/* for safe */
462108530Ssimokawa			reg2 &= ~0x83;
463108530Ssimokawa		}
464108530Ssimokawa		reg2 = fwphy_wrdata(sc, 5, reg2);
465108530Ssimokawa	}
466108530Ssimokawa
467108530Ssimokawa	reg = fwphy_rddata(sc, FW_PHY_SPD_REG);
468108530Ssimokawa	if((reg >> 5) == 7 ){
469108530Ssimokawa		reg = fwphy_rddata(sc, 4);
470108530Ssimokawa		reg |= 1 << 6;
471108530Ssimokawa		fwphy_wrdata(sc, 4, reg);
472108530Ssimokawa		reg = fwphy_rddata(sc, 4);
473108530Ssimokawa	}
474108530Ssimokawa	return 0;
475108530Ssimokawa}
476108530Ssimokawa
477108530Ssimokawa
478108530Ssimokawavoid
479108530Ssimokawafwohci_reset(struct fwohci_softc *sc, device_t dev)
480108530Ssimokawa{
481108701Ssimokawa	int i, max_rec, speed;
482103285Sikob	u_int32_t reg, reg2;
483103285Sikob	struct fwohcidb_tr *db_tr;
484103285Sikob
485108701Ssimokawa	/* Disable interrupt */
486108530Ssimokawa	OWRITE(sc, FWOHCI_INTMASKCLR, ~0);
487108530Ssimokawa
488108701Ssimokawa	/* Now stopping all DMA channel */
489108530Ssimokawa	OWRITE(sc,  OHCI_ARQCTLCLR, OHCI_CNTL_DMA_RUN);
490108530Ssimokawa	OWRITE(sc,  OHCI_ARSCTLCLR, OHCI_CNTL_DMA_RUN);
491108530Ssimokawa	OWRITE(sc,  OHCI_ATQCTLCLR, OHCI_CNTL_DMA_RUN);
492108530Ssimokawa	OWRITE(sc,  OHCI_ATSCTLCLR, OHCI_CNTL_DMA_RUN);
493108530Ssimokawa
494108530Ssimokawa	OWRITE(sc,  OHCI_IR_MASKCLR, ~0);
495108530Ssimokawa	for( i = 0 ; i < sc->fc.nisodma ; i ++ ){
496108530Ssimokawa		OWRITE(sc,  OHCI_IRCTLCLR(i), OHCI_CNTL_DMA_RUN);
497108530Ssimokawa		OWRITE(sc,  OHCI_ITCTLCLR(i), OHCI_CNTL_DMA_RUN);
498108530Ssimokawa	}
499108530Ssimokawa
500108701Ssimokawa	/* FLUSH FIFO and reset Transmitter/Reciever */
501108530Ssimokawa	OWRITE(sc, OHCI_HCCCTL, OHCI_HCC_RESET);
502108530Ssimokawa	if (bootverbose)
503108530Ssimokawa		device_printf(dev, "resetting OHCI...");
504108530Ssimokawa	i = 0;
505108530Ssimokawa	while(OREAD(sc, OHCI_HCCCTL) & OHCI_HCC_RESET) {
506108530Ssimokawa		if (i++ > 100) break;
507108530Ssimokawa		DELAY(1000);
508108530Ssimokawa	}
509108530Ssimokawa	if (bootverbose)
510108530Ssimokawa		printf("done (loop=%d)\n", i);
511108530Ssimokawa
512108701Ssimokawa	/* Probe phy */
513108701Ssimokawa	fwohci_probe_phy(sc, dev);
514108701Ssimokawa
515108701Ssimokawa	/* Probe link */
516108530Ssimokawa	reg = OREAD(sc,  OHCI_BUS_OPT);
517108530Ssimokawa	reg2 = reg | OHCI_BUSFNC;
518108701Ssimokawa	max_rec = (reg & 0x0000f000) >> 12;
519108701Ssimokawa	speed = (reg & 0x00000007);
520108701Ssimokawa	device_printf(dev, "Link %s, max_rec %d bytes.\n",
521108701Ssimokawa			linkspeed[speed], MAXREC(max_rec));
522108701Ssimokawa	/* XXX fix max_rec */
523108701Ssimokawa	sc->fc.maxrec = sc->fc.speed + 8;
524108701Ssimokawa	if (max_rec != sc->fc.maxrec) {
525108701Ssimokawa		reg2 = (reg2 & 0xffff0fff) | (sc->fc.maxrec << 12);
526108701Ssimokawa		device_printf(dev, "max_rec %d -> %d\n",
527108701Ssimokawa				MAXREC(max_rec), MAXREC(sc->fc.maxrec));
528108701Ssimokawa	}
529108530Ssimokawa	if (bootverbose)
530108530Ssimokawa		device_printf(dev, "BUS_OPT 0x%x -> 0x%x\n", reg, reg2);
531108530Ssimokawa	OWRITE(sc,  OHCI_BUS_OPT, reg2);
532108530Ssimokawa
533108701Ssimokawa	/* Initialize registers */
534108530Ssimokawa	OWRITE(sc, OHCI_CROMHDR, sc->fc.config_rom[0]);
535108530Ssimokawa	OWRITE(sc, OHCI_CROMPTR, vtophys(&sc->fc.config_rom[0]));
536108530Ssimokawa	OWRITE(sc, OHCI_HCCCTLCLR, OHCI_HCC_BIGEND);
537108530Ssimokawa	OWRITE(sc, OHCI_HCCCTL, OHCI_HCC_POSTWR);
538108642Ssimokawa	OWRITE(sc, OHCI_SID_BUF, vtophys(sc->fc.sid_buf));
539108530Ssimokawa	OWRITE(sc, OHCI_LNKCTL, OHCI_CNTL_SID);
540108701Ssimokawa	fw_busreset(&sc->fc);
541108530Ssimokawa
542108701Ssimokawa	/* Enable link */
543108530Ssimokawa	OWRITE(sc, OHCI_HCCCTL, OHCI_HCC_LINKEN);
544108642Ssimokawa
545108701Ssimokawa	/* Force to start async RX DMA */
546108642Ssimokawa	sc->arrq.xferq.flag &= ~FWXFERQ_RUNNING;
547108642Ssimokawa	sc->arrs.xferq.flag &= ~FWXFERQ_RUNNING;
548108530Ssimokawa	fwohci_rx_enable(sc, &sc->arrq);
549108530Ssimokawa	fwohci_rx_enable(sc, &sc->arrs);
550108530Ssimokawa
551108701Ssimokawa	/* Initialize async TX */
552108701Ssimokawa	OWRITE(sc, OHCI_ATQCTLCLR, OHCI_CNTL_DMA_RUN | OHCI_CNTL_DMA_DEAD);
553108701Ssimokawa	OWRITE(sc, OHCI_ATSCTLCLR, OHCI_CNTL_DMA_RUN | OHCI_CNTL_DMA_DEAD);
554108701Ssimokawa	/* AT Retries */
555108701Ssimokawa	OWRITE(sc, FWOHCI_RETRY,
556108701Ssimokawa		/* CycleLimit   PhyRespRetries ATRespRetries ATReqRetries */
557108701Ssimokawa		(0xffff << 16 ) | (0x0f << 8) | (0x0f << 4) | 0x0f) ;
558108530Ssimokawa	for( i = 0, db_tr = sc->atrq.top; i < sc->atrq.ndb ;
559108530Ssimokawa				i ++, db_tr = STAILQ_NEXT(db_tr, link)){
560108530Ssimokawa		db_tr->xfer = NULL;
561108530Ssimokawa	}
562108530Ssimokawa	for( i = 0, db_tr = sc->atrs.top; i < sc->atrs.ndb ;
563108530Ssimokawa				i ++, db_tr = STAILQ_NEXT(db_tr, link)){
564108530Ssimokawa		db_tr->xfer = NULL;
565108530Ssimokawa	}
566108530Ssimokawa
567108701Ssimokawa
568108701Ssimokawa	/* Enable interrupt */
569108530Ssimokawa	OWRITE(sc, FWOHCI_INTMASK,
570108530Ssimokawa			OHCI_INT_ERR  | OHCI_INT_PHY_SID
571108530Ssimokawa			| OHCI_INT_DMA_ATRQ | OHCI_INT_DMA_ATRS
572108530Ssimokawa			| OHCI_INT_DMA_PRRQ | OHCI_INT_DMA_PRRS
573108530Ssimokawa			| OHCI_INT_PHY_BUS_R | OHCI_INT_PW_ERR);
574108530Ssimokawa	fwohci_set_intr(&sc->fc, 1);
575108530Ssimokawa
576108530Ssimokawa}
577108530Ssimokawa
578108530Ssimokawaint
579108530Ssimokawafwohci_init(struct fwohci_softc *sc, device_t dev)
580108530Ssimokawa{
581108530Ssimokawa	int i;
582108530Ssimokawa	u_int32_t reg;
583109814Ssimokawa	u_int8_t ui[8];
584108530Ssimokawa
585103285Sikob	reg = OREAD(sc, OHCI_VERSION);
586103285Sikob	device_printf(dev, "OHCI version %x.%x (ROM=%d)\n",
587103285Sikob			(reg>>16) & 0xff, reg & 0xff, (reg>>24) & 1);
588103285Sikob
589103285Sikob/* XXX: Available Isochrounous DMA channel probe */
590103285Sikob	for( i = 0 ; i < 0x20 ; i ++ ){
591103285Sikob		OWRITE(sc,  OHCI_IRCTL(i), OHCI_CNTL_DMA_RUN);
592103285Sikob		reg = OREAD(sc, OHCI_IRCTL(i));
593103285Sikob		if(!(reg & OHCI_CNTL_DMA_RUN)) break;
594103285Sikob		OWRITE(sc,  OHCI_ITCTL(i), OHCI_CNTL_DMA_RUN);
595103285Sikob		reg = OREAD(sc, OHCI_ITCTL(i));
596103285Sikob		if(!(reg & OHCI_CNTL_DMA_RUN)) break;
597103285Sikob	}
598103285Sikob	sc->fc.nisodma = i;
599103285Sikob	device_printf(dev, "No. of Isochronous channel is %d.\n", i);
600103285Sikob
601103285Sikob	sc->fc.arq = &sc->arrq.xferq;
602103285Sikob	sc->fc.ars = &sc->arrs.xferq;
603103285Sikob	sc->fc.atq = &sc->atrq.xferq;
604103285Sikob	sc->fc.ats = &sc->atrs.xferq;
605103285Sikob
606103285Sikob	sc->arrq.xferq.start = NULL;
607103285Sikob	sc->arrs.xferq.start = NULL;
608103285Sikob	sc->atrq.xferq.start = fwohci_start_atq;
609103285Sikob	sc->atrs.xferq.start = fwohci_start_ats;
610103285Sikob
611103285Sikob	sc->arrq.xferq.drain = NULL;
612103285Sikob	sc->arrs.xferq.drain = NULL;
613103285Sikob	sc->atrq.xferq.drain = fwohci_drain_atq;
614103285Sikob	sc->atrs.xferq.drain = fwohci_drain_ats;
615103285Sikob
616103285Sikob	sc->arrq.ndesc = 1;
617103285Sikob	sc->arrs.ndesc = 1;
618108655Ssimokawa	sc->atrq.ndesc = 6;	/* equal to maximum of mbuf chains */
619108655Ssimokawa	sc->atrs.ndesc = 6 / 2;
620103285Sikob
621103285Sikob	sc->arrq.ndb = NDB;
622103285Sikob	sc->arrs.ndb = NDB / 2;
623103285Sikob	sc->atrq.ndb = NDB;
624103285Sikob	sc->atrs.ndb = NDB / 2;
625103285Sikob
626103285Sikob	sc->arrq.dummy = NULL;
627103285Sikob	sc->arrs.dummy = NULL;
628103285Sikob	sc->atrq.dummy = NULL;
629103285Sikob	sc->atrs.dummy = NULL;
630103285Sikob	for( i = 0 ; i < sc->fc.nisodma ; i ++ ){
631103285Sikob		sc->fc.it[i] = &sc->it[i].xferq;
632103285Sikob		sc->fc.ir[i] = &sc->ir[i].xferq;
633103285Sikob		sc->it[i].ndb = 0;
634103285Sikob		sc->ir[i].ndb = 0;
635103285Sikob	}
636103285Sikob
637103285Sikob	sc->fc.tcode = tinfo;
638103285Sikob
639109379Ssimokawa	sc->cromptr = (u_int32_t *) malloc(CROMSIZE * 2, M_DEVBUF, M_NOWAIT);
640103285Sikob
641103285Sikob	if(sc->cromptr == NULL){
642108527Ssimokawa		device_printf(dev, "cromptr alloc failed.");
643103285Sikob		return ENOMEM;
644103285Sikob	}
645103285Sikob	sc->fc.dev = dev;
646103285Sikob	sc->fc.config_rom = &(sc->cromptr[CROMSIZE/4]);
647103285Sikob
648103285Sikob	sc->fc.config_rom[1] = 0x31333934;
649103285Sikob	sc->fc.config_rom[2] = 0xf000a002;
650103285Sikob	sc->fc.config_rom[3] = OREAD(sc, OHCI_EUID_HI);
651103285Sikob	sc->fc.config_rom[4] = OREAD(sc, OHCI_EUID_LO);
652103285Sikob	sc->fc.config_rom[5] = 0;
653103285Sikob	sc->fc.config_rom[0] = (4 << 24) | (5 << 16);
654103285Sikob
655103285Sikob	sc->fc.config_rom[0] |= fw_crc16(&sc->fc.config_rom[1], 5*4);
656103285Sikob
657103285Sikob
658103285Sikob/* SID recieve buffer must allign 2^11 */
659103285Sikob#define	OHCI_SIDSIZE	(1 << 11)
660109379Ssimokawa	sc->fc.sid_buf = (u_int32_t *) malloc(OHCI_SIDSIZE, M_DEVBUF, M_NOWAIT);
661108527Ssimokawa	if (sc->fc.sid_buf == NULL) {
662108527Ssimokawa		device_printf(dev, "sid_buf alloc failed.\n");
663108527Ssimokawa		return ENOMEM;
664108527Ssimokawa	}
665110016Ssimokawa	if (((vm_offset_t) sc->fc.sid_buf & (OHCI_SIDSIZE - 1)) != 0) {
666109736Ssimokawa		device_printf(dev, "sid_buf(%p) not aligned.\n",
667109736Ssimokawa							sc->fc.sid_buf);
668109736Ssimokawa		return ENOMEM;
669109736Ssimokawa	}
670108530Ssimokawa
671103285Sikob	fwohci_db_init(&sc->arrq);
672108527Ssimokawa	if ((sc->arrq.flags & FWOHCI_DBCH_INIT) == 0)
673108527Ssimokawa		return ENOMEM;
674108527Ssimokawa
675103285Sikob	fwohci_db_init(&sc->arrs);
676108527Ssimokawa	if ((sc->arrs.flags & FWOHCI_DBCH_INIT) == 0)
677108527Ssimokawa		return ENOMEM;
678103285Sikob
679103285Sikob	fwohci_db_init(&sc->atrq);
680108527Ssimokawa	if ((sc->atrq.flags & FWOHCI_DBCH_INIT) == 0)
681108527Ssimokawa		return ENOMEM;
682108527Ssimokawa
683103285Sikob	fwohci_db_init(&sc->atrs);
684108527Ssimokawa	if ((sc->atrs.flags & FWOHCI_DBCH_INIT) == 0)
685108527Ssimokawa		return ENOMEM;
686103285Sikob
687109814Ssimokawa	sc->fc.eui.hi = OREAD(sc, FWOHCIGUID_H);
688109814Ssimokawa	sc->fc.eui.lo = OREAD(sc, FWOHCIGUID_L);
689109814Ssimokawa	for( i = 0 ; i < 8 ; i ++)
690109814Ssimokawa		ui[i] = FW_EUI64_BYTE(&sc->fc.eui,i);
691103285Sikob	device_printf(dev, "EUI64 %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
692109814Ssimokawa		ui[0], ui[1], ui[2], ui[3], ui[4], ui[5], ui[6], ui[7]);
693109814Ssimokawa
694103285Sikob	sc->fc.ioctl = fwohci_ioctl;
695103285Sikob	sc->fc.cyctimer = fwohci_cyctimer;
696103285Sikob	sc->fc.set_bmr = fwohci_set_bus_manager;
697103285Sikob	sc->fc.ibr = fwohci_ibr;
698103285Sikob	sc->fc.irx_enable = fwohci_irx_enable;
699103285Sikob	sc->fc.irx_disable = fwohci_irx_disable;
700103285Sikob
701103285Sikob	sc->fc.itx_enable = fwohci_itxbuf_enable;
702103285Sikob	sc->fc.itx_disable = fwohci_itx_disable;
703103285Sikob	sc->fc.irx_post = fwohci_irx_post;
704103285Sikob	sc->fc.itx_post = NULL;
705103285Sikob	sc->fc.timeout = fwohci_timeout;
706103285Sikob	sc->fc.poll = fwohci_poll;
707103285Sikob	sc->fc.set_intr = fwohci_set_intr;
708106790Ssimokawa
709108530Ssimokawa	fw_init(&sc->fc);
710108530Ssimokawa	fwohci_reset(sc, dev);
711103285Sikob
712108530Ssimokawa	return 0;
713103285Sikob}
714106790Ssimokawa
715106790Ssimokawavoid
716106790Ssimokawafwohci_timeout(void *arg)
717103285Sikob{
718103285Sikob	struct fwohci_softc *sc;
719103285Sikob
720103285Sikob	sc = (struct fwohci_softc *)arg;
721103285Sikob	sc->fc.timeouthandle = timeout(fwohci_timeout,
722103285Sikob				(void *)sc, FW_XFERTIMEOUT * hz * 10);
723103285Sikob}
724106790Ssimokawa
725106790Ssimokawau_int32_t
726106790Ssimokawafwohci_cyctimer(struct firewire_comm *fc)
727103285Sikob{
728103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
729103285Sikob	return(OREAD(sc, OHCI_CYCLETIMER));
730103285Sikob}
731103285Sikob
732108527Ssimokawaint
733108527Ssimokawafwohci_detach(struct fwohci_softc *sc, device_t dev)
734108527Ssimokawa{
735108527Ssimokawa	int i;
736108527Ssimokawa
737108527Ssimokawa	if (sc->fc.sid_buf != NULL)
738109379Ssimokawa		free((void *)(uintptr_t)sc->fc.sid_buf, M_DEVBUF);
739108527Ssimokawa	if (sc->cromptr != NULL)
740109379Ssimokawa		free((void *)sc->cromptr, M_DEVBUF);
741108527Ssimokawa
742108527Ssimokawa	fwohci_db_free(&sc->arrq);
743108527Ssimokawa	fwohci_db_free(&sc->arrs);
744108527Ssimokawa
745108527Ssimokawa	fwohci_db_free(&sc->atrq);
746108527Ssimokawa	fwohci_db_free(&sc->atrs);
747108527Ssimokawa
748108527Ssimokawa	for( i = 0 ; i < sc->fc.nisodma ; i ++ ){
749108527Ssimokawa		fwohci_db_free(&sc->it[i]);
750108527Ssimokawa		fwohci_db_free(&sc->ir[i]);
751108527Ssimokawa	}
752108527Ssimokawa
753108527Ssimokawa	return 0;
754108527Ssimokawa}
755108527Ssimokawa
756108655Ssimokawa#define LAST_DB(dbtr, db) do {						\
757108655Ssimokawa	struct fwohcidb_tr *_dbtr = (dbtr);				\
758108655Ssimokawa	int _cnt = _dbtr->dbcnt;					\
759108655Ssimokawa	db = &_dbtr->db[ (_cnt > 2) ? (_cnt -1) : 0];			\
760108655Ssimokawa} while (0)
761108655Ssimokawa
762106790Ssimokawastatic void
763106790Ssimokawafwohci_start(struct fwohci_softc *sc, struct fwohci_dbch *dbch)
764103285Sikob{
765103285Sikob	int i, s;
766103285Sikob	int tcode, hdr_len, hdr_off, len;
767103285Sikob	int fsegment = -1;
768103285Sikob	u_int32_t off;
769103285Sikob	struct fw_xfer *xfer;
770103285Sikob	struct fw_pkt *fp;
771103285Sikob	volatile struct fwohci_txpkthdr *ohcifp;
772103285Sikob	struct fwohcidb_tr *db_tr;
773103285Sikob	volatile struct fwohcidb *db;
774103285Sikob	struct mbuf *m;
775103285Sikob	struct tcode_info *info;
776108655Ssimokawa	static int maxdesc=0;
777103285Sikob
778103285Sikob	if(&sc->atrq == dbch){
779103285Sikob		off = OHCI_ATQOFF;
780103285Sikob	}else if(&sc->atrs == dbch){
781103285Sikob		off = OHCI_ATSOFF;
782103285Sikob	}else{
783103285Sikob		return;
784103285Sikob	}
785103285Sikob
786103285Sikob	if (dbch->flags & FWOHCI_DBCH_FULL)
787103285Sikob		return;
788103285Sikob
789103285Sikob	s = splfw();
790103285Sikob	db_tr = dbch->top;
791103285Sikobtxloop:
792103285Sikob	xfer = STAILQ_FIRST(&dbch->xferq.q);
793103285Sikob	if(xfer == NULL){
794103285Sikob		goto kick;
795103285Sikob	}
796103285Sikob	if(dbch->xferq.queued == 0 ){
797103285Sikob		device_printf(sc->fc.dev, "TX queue empty\n");
798103285Sikob	}
799103285Sikob	STAILQ_REMOVE_HEAD(&dbch->xferq.q, link);
800103285Sikob	db_tr->xfer = xfer;
801103285Sikob	xfer->state = FWXF_START;
802103285Sikob	dbch->xferq.packets++;
803103285Sikob
804103285Sikob	fp = (struct fw_pkt *)(xfer->send.buf + xfer->send.off);
805103285Sikob	tcode = fp->mode.common.tcode;
806103285Sikob
807103285Sikob	ohcifp = (volatile struct fwohci_txpkthdr *) db_tr->db[1].db.immed;
808103285Sikob	info = &tinfo[tcode];
809103285Sikob	hdr_len = hdr_off = info->hdr_len;
810103285Sikob	/* fw_asyreq must pass valid send.len */
811103285Sikob	len = xfer->send.len;
812103285Sikob	for( i = 0 ; i < hdr_off ; i+= 4){
813103285Sikob		ohcifp->mode.ld[i/4] = ntohl(fp->mode.ld[i/4]);
814103285Sikob	}
815103285Sikob	ohcifp->mode.common.spd = xfer->spd;
816103285Sikob	if (tcode == FWTCODE_STREAM ){
817103285Sikob		hdr_len = 8;
818103285Sikob		ohcifp->mode.stream.len = ntohs(fp->mode.stream.len);
819103285Sikob	} else if (tcode == FWTCODE_PHY) {
820103285Sikob		hdr_len = 12;
821103285Sikob		ohcifp->mode.ld[1] = ntohl(fp->mode.ld[1]);
822103285Sikob		ohcifp->mode.ld[2] = ntohl(fp->mode.ld[2]);
823103285Sikob		ohcifp->mode.common.spd = 0;
824103285Sikob		ohcifp->mode.common.tcode = FWOHCITCODE_PHY;
825103285Sikob	} else {
826103285Sikob		ohcifp->mode.asycomm.dst = ntohs(fp->mode.hdr.dst);
827103285Sikob		ohcifp->mode.asycomm.srcbus = OHCI_ASYSRCBUS;
828103285Sikob		ohcifp->mode.asycomm.tlrt |= FWRETRY_X;
829103285Sikob	}
830103285Sikob	db = &db_tr->db[0];
831109892Ssimokawa 	db->db.desc.control = OHCI_OUTPUT_MORE | OHCI_KEY_ST2;
832109892Ssimokawa	db->db.desc.reqcount = hdr_len;
833103285Sikob 	db->db.desc.status = 0;
834103285Sikob/* Specify bound timer of asy. responce */
835103285Sikob	if(&sc->atrs == dbch){
836103285Sikob 		db->db.desc.count
837103285Sikob			 = (OREAD(sc, OHCI_CYCLETIMER) >> 12) + (1 << 13);
838103285Sikob	}
839103285Sikob
840103285Sikob	db_tr->dbcnt = 2;
841103285Sikob	db = &db_tr->db[db_tr->dbcnt];
842103285Sikob	if(len > hdr_off){
843103285Sikob		if (xfer->mbuf == NULL) {
844103285Sikob			db->db.desc.addr
845103285Sikob				= vtophys(xfer->send.buf + xfer->send.off) + hdr_off;
846109892Ssimokawa			db->db.desc.control = OHCI_OUTPUT_MORE;
847109892Ssimokawa			db->db.desc.reqcount = len - hdr_off;
848103285Sikob 			db->db.desc.status = 0;
849103285Sikob
850103285Sikob			db_tr->dbcnt++;
851103285Sikob		} else {
852109890Ssimokawa			int mchain=0;
853103285Sikob			/* XXX we assume mbuf chain is shorter than ndesc */
854108655Ssimokawa			for (m = xfer->mbuf; m != NULL; m = m->m_next) {
855108655Ssimokawa				if (m->m_len == 0)
856109890Ssimokawa					/* unrecoverable error could occur. */
857108655Ssimokawa					continue;
858109890Ssimokawa				mchain++;
859109890Ssimokawa				if (db_tr->dbcnt >= dbch->ndesc)
860109890Ssimokawa					continue;
861103285Sikob				db->db.desc.addr
862103285Sikob					= vtophys(mtod(m, caddr_t));
863109892Ssimokawa				db->db.desc.control = OHCI_OUTPUT_MORE;
864109892Ssimokawa				db->db.desc.reqcount = m->m_len;
865103285Sikob 				db->db.desc.status = 0;
866103285Sikob				db++;
867103285Sikob				db_tr->dbcnt++;
868108655Ssimokawa			}
869109890Ssimokawa			if (mchain > dbch->ndesc - 2)
870109890Ssimokawa				device_printf(sc->fc.dev,
871109890Ssimokawa					"dbch->ndesc(%d) is too small for"
872109890Ssimokawa					" mbuf chain(%d), trancated.\n",
873109890Ssimokawa					dbch->ndesc, mchain);
874103285Sikob		}
875103285Sikob	}
876108655Ssimokawa	if (maxdesc < db_tr->dbcnt) {
877108655Ssimokawa		maxdesc = db_tr->dbcnt;
878108655Ssimokawa		if (bootverbose)
879108655Ssimokawa			device_printf(sc->fc.dev, "maxdesc: %d\n", maxdesc);
880108655Ssimokawa	}
881103285Sikob	/* last db */
882103285Sikob	LAST_DB(db_tr, db);
883109892Ssimokawa 	db->db.desc.control |= OHCI_OUTPUT_LAST
884103285Sikob			| OHCI_INTERRUPT_ALWAYS
885103285Sikob			| OHCI_BRANCH_ALWAYS;
886103285Sikob 	db->db.desc.depend = vtophys(STAILQ_NEXT(db_tr, link)->db);
887103285Sikob
888103285Sikob	if(fsegment == -1 )
889103285Sikob		fsegment = db_tr->dbcnt;
890103285Sikob	if (dbch->pdb_tr != NULL) {
891103285Sikob		LAST_DB(dbch->pdb_tr, db);
892103285Sikob 		db->db.desc.depend |= db_tr->dbcnt;
893103285Sikob	}
894103285Sikob	dbch->pdb_tr = db_tr;
895103285Sikob	db_tr = STAILQ_NEXT(db_tr, link);
896103285Sikob	if(db_tr != dbch->bottom){
897103285Sikob		goto txloop;
898103285Sikob	} else {
899107653Ssimokawa		device_printf(sc->fc.dev, "fwohci_start: lack of db_trq\n");
900103285Sikob		dbch->flags |= FWOHCI_DBCH_FULL;
901103285Sikob	}
902103285Sikobkick:
903103285Sikob	if (firewire_debug) printf("kick\n");
904103285Sikob	/* kick asy q */
905103285Sikob
906103285Sikob	if(dbch->xferq.flag & FWXFERQ_RUNNING) {
907103285Sikob		OWRITE(sc, OHCI_DMACTL(off), OHCI_CNTL_DMA_WAKE);
908103285Sikob	} else {
909107653Ssimokawa		if (bootverbose)
910107653Ssimokawa			device_printf(sc->fc.dev, "start AT DMA status=%x\n",
911103285Sikob					OREAD(sc, OHCI_DMACTL(off)));
912103285Sikob		OWRITE(sc, OHCI_DMACMD(off), vtophys(dbch->top->db) | fsegment);
913103285Sikob		OWRITE(sc, OHCI_DMACTL(off), OHCI_CNTL_DMA_RUN);
914103285Sikob		dbch->xferq.flag |= FWXFERQ_RUNNING;
915103285Sikob	}
916106790Ssimokawa
917103285Sikob	dbch->top = db_tr;
918103285Sikob	splx(s);
919103285Sikob	return;
920103285Sikob}
921106790Ssimokawa
922106790Ssimokawastatic void
923106790Ssimokawafwohci_drain_atq(struct firewire_comm *fc, struct fw_xfer *xfer)
924103285Sikob{
925103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
926103285Sikob	fwohci_drain(&sc->fc, xfer, &(sc->atrq));
927103285Sikob	return;
928103285Sikob}
929106790Ssimokawa
930106790Ssimokawastatic void
931106790Ssimokawafwohci_drain_ats(struct firewire_comm *fc, struct fw_xfer *xfer)
932103285Sikob{
933103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
934103285Sikob	fwohci_drain(&sc->fc, xfer, &(sc->atrs));
935103285Sikob	return;
936103285Sikob}
937106790Ssimokawa
938106790Ssimokawastatic void
939106790Ssimokawafwohci_start_atq(struct firewire_comm *fc)
940103285Sikob{
941103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
942103285Sikob	fwohci_start( sc, &(sc->atrq));
943103285Sikob	return;
944103285Sikob}
945106790Ssimokawa
946106790Ssimokawastatic void
947106790Ssimokawafwohci_start_ats(struct firewire_comm *fc)
948103285Sikob{
949103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
950103285Sikob	fwohci_start( sc, &(sc->atrs));
951103285Sikob	return;
952103285Sikob}
953106790Ssimokawa
954106790Ssimokawavoid
955106790Ssimokawafwohci_txd(struct fwohci_softc *sc, struct fwohci_dbch *dbch)
956103285Sikob{
957103285Sikob	int s, err = 0;
958103285Sikob	struct fwohcidb_tr *tr;
959103285Sikob	volatile struct fwohcidb *db;
960103285Sikob	struct fw_xfer *xfer;
961103285Sikob	u_int32_t off;
962103285Sikob	u_int stat;
963103285Sikob	int	packets;
964103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)sc;
965103285Sikob	if(&sc->atrq == dbch){
966103285Sikob		off = OHCI_ATQOFF;
967103285Sikob	}else if(&sc->atrs == dbch){
968103285Sikob		off = OHCI_ATSOFF;
969103285Sikob	}else{
970103285Sikob		return;
971103285Sikob	}
972103285Sikob	s = splfw();
973103285Sikob	tr = dbch->bottom;
974103285Sikob	packets = 0;
975103285Sikob	while(dbch->xferq.queued > 0){
976103285Sikob		LAST_DB(tr, db);
977103285Sikob		if(!(db->db.desc.status & OHCI_CNTL_DMA_ACTIVE)){
978103285Sikob			if (fc->status != FWBUSRESET)
979103285Sikob				/* maybe out of order?? */
980103285Sikob				goto out;
981103285Sikob		}
982103285Sikob		if(db->db.desc.status & OHCI_CNTL_DMA_DEAD) {
983103285Sikob#ifdef OHCI_DEBUG
984103285Sikob			dump_dma(sc, ch);
985103285Sikob			dump_db(sc, ch);
986103285Sikob#endif
987103285Sikob/* Stop DMA */
988103285Sikob			OWRITE(sc, OHCI_DMACTLCLR(off), OHCI_CNTL_DMA_RUN);
989103285Sikob			device_printf(sc->fc.dev, "force reset AT FIFO\n");
990103285Sikob			OWRITE(sc, OHCI_HCCCTLCLR, OHCI_HCC_LINKEN);
991103285Sikob			OWRITE(sc, OHCI_HCCCTL, OHCI_HCC_LPS | OHCI_HCC_LINKEN);
992103285Sikob			OWRITE(sc, OHCI_DMACTLCLR(off), OHCI_CNTL_DMA_RUN);
993103285Sikob		}
994103285Sikob		stat = db->db.desc.status & FWOHCIEV_MASK;
995103285Sikob		switch(stat){
996103285Sikob		case FWOHCIEV_ACKCOMPL:
997103285Sikob		case FWOHCIEV_ACKPEND:
998103285Sikob			err = 0;
999103285Sikob			break;
1000103285Sikob		case FWOHCIEV_ACKBSA:
1001103285Sikob		case FWOHCIEV_ACKBSB:
1002103285Sikob			device_printf(sc->fc.dev, "txd err=%2x %s\n", stat, fwohcicode[stat]);
1003103285Sikob		case FWOHCIEV_ACKBSX:
1004103285Sikob			err = EBUSY;
1005103285Sikob			break;
1006103285Sikob		case FWOHCIEV_FLUSHED:
1007103285Sikob		case FWOHCIEV_ACKTARD:
1008103285Sikob			device_printf(sc->fc.dev, "txd err=%2x %s\n", stat, fwohcicode[stat]);
1009103285Sikob			err = EAGAIN;
1010103285Sikob			break;
1011103285Sikob		case FWOHCIEV_MISSACK:
1012103285Sikob		case FWOHCIEV_UNDRRUN:
1013103285Sikob		case FWOHCIEV_OVRRUN:
1014103285Sikob		case FWOHCIEV_DESCERR:
1015103285Sikob		case FWOHCIEV_DTRDERR:
1016103285Sikob		case FWOHCIEV_TIMEOUT:
1017103285Sikob		case FWOHCIEV_TCODERR:
1018103285Sikob		case FWOHCIEV_UNKNOWN:
1019103285Sikob		case FWOHCIEV_ACKDERR:
1020103285Sikob		case FWOHCIEV_ACKTERR:
1021103285Sikob		default:
1022103285Sikob			device_printf(sc->fc.dev, "txd err=%2x %s\n",
1023103285Sikob							stat, fwohcicode[stat]);
1024103285Sikob			err = EINVAL;
1025103285Sikob			break;
1026103285Sikob		}
1027103285Sikob		if(tr->xfer != NULL){
1028103285Sikob			xfer = tr->xfer;
1029103285Sikob			xfer->state = FWXF_SENT;
1030103285Sikob			if(err == EBUSY && fc->status != FWBUSRESET){
1031103285Sikob				xfer->state = FWXF_BUSY;
1032103285Sikob				switch(xfer->act_type){
1033103285Sikob				case FWACT_XFER:
1034103285Sikob					xfer->resp = err;
1035103285Sikob					if(xfer->retry_req != NULL){
1036103285Sikob						xfer->retry_req(xfer);
1037103285Sikob					}
1038103285Sikob					break;
1039103285Sikob				default:
1040103285Sikob					break;
1041103285Sikob				}
1042103285Sikob			} else if( stat != FWOHCIEV_ACKPEND){
1043103285Sikob				if (stat != FWOHCIEV_ACKCOMPL)
1044103285Sikob					xfer->state = FWXF_SENTERR;
1045103285Sikob				xfer->resp = err;
1046103285Sikob				switch(xfer->act_type){
1047103285Sikob				case FWACT_XFER:
1048103285Sikob					fw_xfer_done(xfer);
1049103285Sikob					break;
1050103285Sikob				default:
1051103285Sikob					break;
1052103285Sikob				}
1053103285Sikob			}
1054103285Sikob			dbch->xferq.queued --;
1055103285Sikob		}
1056103285Sikob		tr->xfer = NULL;
1057103285Sikob
1058103285Sikob		packets ++;
1059103285Sikob		tr = STAILQ_NEXT(tr, link);
1060103285Sikob		dbch->bottom = tr;
1061103285Sikob	}
1062103285Sikobout:
1063103285Sikob	if ((dbch->flags & FWOHCI_DBCH_FULL) && packets > 0) {
1064103285Sikob		printf("make free slot\n");
1065103285Sikob		dbch->flags &= ~FWOHCI_DBCH_FULL;
1066103285Sikob		fwohci_start(sc, dbch);
1067103285Sikob	}
1068103285Sikob	splx(s);
1069103285Sikob}
1070106790Ssimokawa
1071106790Ssimokawastatic void
1072106790Ssimokawafwohci_drain(struct firewire_comm *fc, struct fw_xfer *xfer, struct fwohci_dbch *dbch)
1073103285Sikob{
1074103285Sikob	int i, s;
1075103285Sikob	struct fwohcidb_tr *tr;
1076103285Sikob
1077103285Sikob	if(xfer->state != FWXF_START) return;
1078103285Sikob
1079103285Sikob	s = splfw();
1080103285Sikob	tr = dbch->bottom;
1081103285Sikob	for( i = 0 ; i <= dbch->xferq.queued  ; i ++){
1082103285Sikob		if(tr->xfer == xfer){
1083103285Sikob			s = splfw();
1084103285Sikob			tr->xfer = NULL;
1085103285Sikob			dbch->xferq.queued --;
1086103285Sikob#if 1
1087103285Sikob			/* XXX */
1088103285Sikob			if (tr == dbch->bottom)
1089103285Sikob				dbch->bottom = STAILQ_NEXT(tr, link);
1090103285Sikob#endif
1091103285Sikob			if (dbch->flags & FWOHCI_DBCH_FULL) {
1092103285Sikob				printf("fwohci_drain: make slot\n");
1093103285Sikob				dbch->flags &= ~FWOHCI_DBCH_FULL;
1094103285Sikob				fwohci_start((struct fwohci_softc *)fc, dbch);
1095103285Sikob			}
1096103285Sikob
1097103285Sikob			splx(s);
1098103285Sikob			break;
1099103285Sikob		}
1100103285Sikob		tr = STAILQ_NEXT(tr, link);
1101103285Sikob	}
1102103285Sikob	splx(s);
1103103285Sikob	return;
1104103285Sikob}
1105103285Sikob
1106106790Ssimokawastatic void
1107106790Ssimokawafwohci_db_free(struct fwohci_dbch *dbch)
1108103285Sikob{
1109103285Sikob	struct fwohcidb_tr *db_tr;
1110109379Ssimokawa	int idb, i;
1111103285Sikob
1112108527Ssimokawa	if ((dbch->flags & FWOHCI_DBCH_INIT) == 0)
1113108527Ssimokawa		return;
1114108527Ssimokawa
1115103285Sikob	if(!(dbch->xferq.flag & FWXFERQ_EXTBUF)){
1116103285Sikob		for(db_tr = STAILQ_FIRST(&dbch->db_trq), idb = 0;
1117103285Sikob			idb < dbch->ndb;
1118103285Sikob			db_tr = STAILQ_NEXT(db_tr, link), idb++){
1119108527Ssimokawa			if (db_tr->buf != NULL) {
1120108527Ssimokawa				free(db_tr->buf, M_DEVBUF);
1121108527Ssimokawa				db_tr->buf = NULL;
1122108527Ssimokawa			}
1123103285Sikob		}
1124103285Sikob	}
1125103285Sikob	dbch->ndb = 0;
1126103285Sikob	db_tr = STAILQ_FIRST(&dbch->db_trq);
1127109379Ssimokawa	for (i = 0; i < dbch->npages; i++)
1128109379Ssimokawa		free(dbch->pages[i], M_DEVBUF);
1129103285Sikob	free(db_tr, M_DEVBUF);
1130103285Sikob	STAILQ_INIT(&dbch->db_trq);
1131108527Ssimokawa	dbch->flags &= ~FWOHCI_DBCH_INIT;
1132103285Sikob}
1133106790Ssimokawa
1134106790Ssimokawastatic void
1135106790Ssimokawafwohci_db_init(struct fwohci_dbch *dbch)
1136103285Sikob{
1137103285Sikob	int	idb;
1138103285Sikob	struct fwohcidb_tr *db_tr;
1139109379Ssimokawa	int	ndbpp, i, j;
1140108642Ssimokawa
1141108642Ssimokawa	if ((dbch->flags & FWOHCI_DBCH_INIT) != 0)
1142108642Ssimokawa		goto out;
1143108642Ssimokawa
1144103285Sikob	/* allocate DB entries and attach one to each DMA channels */
1145103285Sikob	/* DB entry must start at 16 bytes bounary. */
1146103285Sikob	STAILQ_INIT(&dbch->db_trq);
1147103285Sikob	db_tr = (struct fwohcidb_tr *)
1148103285Sikob		malloc(sizeof(struct fwohcidb_tr) * dbch->ndb,
1149109424Ssimokawa		M_DEVBUF, M_NOWAIT | M_ZERO);
1150103285Sikob	if(db_tr == NULL){
1151109379Ssimokawa		printf("fwohci_db_init: malloc(1) failed\n");
1152103285Sikob		return;
1153103285Sikob	}
1154109379Ssimokawa
1155109379Ssimokawa	ndbpp = PAGE_SIZE / (sizeof(struct fwohcidb) * dbch->ndesc);
1156109379Ssimokawa	dbch->npages = (dbch->ndb + ndbpp - 1)/ ndbpp;
1157109403Ssimokawa	if (firewire_debug)
1158109403Ssimokawa		printf("ndesc: %d, ndbpp: %d, ndb: %d, npages: %d\n",
1159109403Ssimokawa			dbch->ndesc, ndbpp, dbch->ndb, dbch->npages);
1160109379Ssimokawa	if (dbch->npages > FWOHCI_DBCH_MAX_PAGES) {
1161109379Ssimokawa		printf("npages(%d) > DBCH_MAX_PAGES(%d)\n",
1162109379Ssimokawa				dbch->npages, FWOHCI_DBCH_MAX_PAGES);
1163103285Sikob		return;
1164103285Sikob	}
1165109379Ssimokawa	for (i = 0; i < dbch->npages; i++) {
1166109379Ssimokawa		dbch->pages[i] = malloc(PAGE_SIZE, M_DEVBUF,
1167109424Ssimokawa						M_NOWAIT | M_ZERO);
1168109379Ssimokawa		if (dbch->pages[i] == NULL) {
1169109379Ssimokawa			printf("fwohci_db_init: malloc(2) failed\n");
1170109379Ssimokawa			for (j = 0; j < i; j ++)
1171109379Ssimokawa				free(dbch->pages[j], M_DEVBUF);
1172109379Ssimokawa			free(db_tr, M_DEVBUF);
1173109379Ssimokawa			return;
1174109379Ssimokawa		}
1175109379Ssimokawa	}
1176103285Sikob	/* Attach DB to DMA ch. */
1177103285Sikob	for(idb = 0 ; idb < dbch->ndb ; idb++){
1178103285Sikob		db_tr->dbcnt = 0;
1179109379Ssimokawa		db_tr->db = (struct fwohcidb *)dbch->pages[idb/ndbpp]
1180109379Ssimokawa					+ dbch->ndesc * (idb % ndbpp);
1181103285Sikob		STAILQ_INSERT_TAIL(&dbch->db_trq, db_tr, link);
1182108530Ssimokawa		if (!(dbch->xferq.flag & FWXFERQ_PACKET) &&
1183108530Ssimokawa					dbch->xferq.bnpacket != 0) {
1184108530Ssimokawa			if (idb % dbch->xferq.bnpacket == 0)
1185108530Ssimokawa				dbch->xferq.bulkxfer[idb / dbch->xferq.bnpacket
1186108530Ssimokawa						].start = (caddr_t)db_tr;
1187108530Ssimokawa			if ((idb + 1) % dbch->xferq.bnpacket == 0)
1188108530Ssimokawa				dbch->xferq.bulkxfer[idb / dbch->xferq.bnpacket
1189108530Ssimokawa						].end = (caddr_t)db_tr;
1190103285Sikob		}
1191103285Sikob		db_tr++;
1192103285Sikob	}
1193103285Sikob	STAILQ_LAST(&dbch->db_trq, fwohcidb_tr,link)->link.stqe_next
1194103285Sikob			= STAILQ_FIRST(&dbch->db_trq);
1195108642Ssimokawaout:
1196108642Ssimokawa	dbch->frag.buf = NULL;
1197108642Ssimokawa	dbch->frag.len = 0;
1198108642Ssimokawa	dbch->frag.plen = 0;
1199108642Ssimokawa	dbch->xferq.queued = 0;
1200108642Ssimokawa	dbch->pdb_tr = NULL;
1201103285Sikob	dbch->top = STAILQ_FIRST(&dbch->db_trq);
1202103285Sikob	dbch->bottom = dbch->top;
1203108527Ssimokawa	dbch->flags = FWOHCI_DBCH_INIT;
1204103285Sikob}
1205106790Ssimokawa
1206106790Ssimokawastatic int
1207106790Ssimokawafwohci_itx_disable(struct firewire_comm *fc, int dmach)
1208103285Sikob{
1209103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
1210109890Ssimokawa	int dummy;
1211109890Ssimokawa
1212103285Sikob	OWRITE(sc, OHCI_ITCTLCLR(dmach), OHCI_CNTL_DMA_RUN);
1213103285Sikob	OWRITE(sc, OHCI_IT_MASKCLR, 1 << dmach);
1214103285Sikob	OWRITE(sc, OHCI_IT_STATCLR, 1 << dmach);
1215109890Ssimokawa	/* XXX we cannot free buffers until the DMA really stops */
1216109890Ssimokawa	tsleep((void *)&dummy, FWPRI, "fwitxd", hz);
1217103285Sikob	fwohci_db_free(&sc->it[dmach]);
1218103285Sikob	sc->it[dmach].xferq.flag &= ~FWXFERQ_RUNNING;
1219103285Sikob	return 0;
1220103285Sikob}
1221106790Ssimokawa
1222106790Ssimokawastatic int
1223106790Ssimokawafwohci_irx_disable(struct firewire_comm *fc, int dmach)
1224103285Sikob{
1225103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
1226109890Ssimokawa	int dummy;
1227103285Sikob
1228103285Sikob	OWRITE(sc, OHCI_IRCTLCLR(dmach), OHCI_CNTL_DMA_RUN);
1229103285Sikob	OWRITE(sc, OHCI_IR_MASKCLR, 1 << dmach);
1230103285Sikob	OWRITE(sc, OHCI_IR_STATCLR, 1 << dmach);
1231109890Ssimokawa	/* XXX we cannot free buffers until the DMA really stops */
1232109890Ssimokawa	tsleep((void *)&dummy, FWPRI, "fwirxd", hz);
1233103285Sikob	if(sc->ir[dmach].dummy != NULL){
1234103285Sikob		free(sc->ir[dmach].dummy, M_DEVBUF);
1235103285Sikob	}
1236103285Sikob	sc->ir[dmach].dummy = NULL;
1237103285Sikob	fwohci_db_free(&sc->ir[dmach]);
1238103285Sikob	sc->ir[dmach].xferq.flag &= ~FWXFERQ_RUNNING;
1239103285Sikob	return 0;
1240103285Sikob}
1241106790Ssimokawa
1242106790Ssimokawastatic void
1243106790Ssimokawafwohci_irx_post (struct firewire_comm *fc , u_int32_t *qld)
1244103285Sikob{
1245103285Sikob	qld[0] = ntohl(qld[0]);
1246103285Sikob	return;
1247103285Sikob}
1248106790Ssimokawa
1249106790Ssimokawastatic int
1250106790Ssimokawafwohci_irxpp_enable(struct firewire_comm *fc, int dmach)
1251103285Sikob{
1252103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
1253103285Sikob	int err = 0;
1254103285Sikob	unsigned short tag, ich;
1255103285Sikob
1256103285Sikob	tag = (sc->ir[dmach].xferq.flag >> 6) & 3;
1257103285Sikob	ich = sc->ir[dmach].xferq.flag & 0x3f;
1258103285Sikob
1259103285Sikob#if 0
1260103285Sikob	if(STAILQ_FIRST(&fc->ir[dmach]->q) != NULL){
1261103285Sikob		wakeup(fc->ir[dmach]);
1262103285Sikob		return err;
1263103285Sikob	}
1264103285Sikob#endif
1265103285Sikob
1266103285Sikob	OWRITE(sc, OHCI_IRMATCH(dmach), tagbit[tag] | ich);
1267103285Sikob	if(!(sc->ir[dmach].xferq.flag & FWXFERQ_RUNNING)){
1268103285Sikob		sc->ir[dmach].xferq.queued = 0;
1269103285Sikob		sc->ir[dmach].ndb = NDB;
1270109379Ssimokawa		sc->ir[dmach].xferq.psize = PAGE_SIZE;
1271103285Sikob		sc->ir[dmach].ndesc = 1;
1272103285Sikob		fwohci_db_init(&sc->ir[dmach]);
1273109179Ssimokawa		if ((sc->ir[dmach].flags & FWOHCI_DBCH_INIT) == 0)
1274109179Ssimokawa			return ENOMEM;
1275103285Sikob		err = fwohci_rx_enable(sc, &sc->ir[dmach]);
1276103285Sikob	}
1277103285Sikob	if(err){
1278103285Sikob		device_printf(sc->fc.dev, "err in IRX setting\n");
1279103285Sikob		return err;
1280103285Sikob	}
1281103285Sikob	if(!(OREAD(sc, OHCI_IRCTL(dmach)) & OHCI_CNTL_DMA_ACTIVE)){
1282103285Sikob		OWRITE(sc, OHCI_IRCTLCLR(dmach), OHCI_CNTL_DMA_RUN);
1283103285Sikob		OWRITE(sc, OHCI_IR_MASKCLR, 1 << dmach);
1284103285Sikob		OWRITE(sc, OHCI_IR_STATCLR, 1 << dmach);
1285103285Sikob		OWRITE(sc, OHCI_IR_MASK, 1 << dmach);
1286103285Sikob		OWRITE(sc, OHCI_IRCTLCLR(dmach), 0xf8000000);
1287103285Sikob		OWRITE(sc, OHCI_IRCTL(dmach), OHCI_CNTL_ISOHDR);
1288103285Sikob		OWRITE(sc, OHCI_IRCMD(dmach),
1289103285Sikob			vtophys(sc->ir[dmach].top->db) | 1);
1290103285Sikob		OWRITE(sc, OHCI_IRCTL(dmach), OHCI_CNTL_DMA_RUN);
1291103285Sikob		OWRITE(sc, FWOHCI_INTMASK, OHCI_INT_DMA_IR);
1292103285Sikob	}
1293103285Sikob	return err;
1294103285Sikob}
1295106790Ssimokawa
1296106790Ssimokawastatic int
1297106790Ssimokawafwohci_tx_enable(struct fwohci_softc *sc, struct fwohci_dbch *dbch)
1298103285Sikob{
1299103285Sikob	int err = 0;
1300103285Sikob	int idb, z, i, dmach = 0;
1301103285Sikob	u_int32_t off = NULL;
1302103285Sikob	struct fwohcidb_tr *db_tr;
1303109892Ssimokawa	volatile struct fwohcidb *db;
1304103285Sikob
1305103285Sikob	if(!(dbch->xferq.flag & FWXFERQ_EXTBUF)){
1306103285Sikob		err = EINVAL;
1307103285Sikob		return err;
1308103285Sikob	}
1309103285Sikob	z = dbch->ndesc;
1310103285Sikob	for(dmach = 0 ; dmach < sc->fc.nisodma ; dmach++){
1311103285Sikob		if( &sc->it[dmach] == dbch){
1312103285Sikob			off = OHCI_ITOFF(dmach);
1313103285Sikob			break;
1314103285Sikob		}
1315103285Sikob	}
1316103285Sikob	if(off == NULL){
1317103285Sikob		err = EINVAL;
1318103285Sikob		return err;
1319103285Sikob	}
1320103285Sikob	if(dbch->xferq.flag & FWXFERQ_RUNNING)
1321103285Sikob		return err;
1322103285Sikob	dbch->xferq.flag |= FWXFERQ_RUNNING;
1323103285Sikob	for( i = 0, dbch->bottom = dbch->top; i < (dbch->ndb - 1); i++){
1324103285Sikob		dbch->bottom = STAILQ_NEXT(dbch->bottom, link);
1325103285Sikob	}
1326103285Sikob	db_tr = dbch->top;
1327103285Sikob	for( idb = 0 ; idb < dbch->ndb ; idb ++){
1328103285Sikob		fwohci_add_tx_buf(db_tr,
1329103285Sikob			dbch->xferq.psize, dbch->xferq.flag,
1330103285Sikob			dbch->xferq.buf + dbch->xferq.psize * idb);
1331103285Sikob		if(STAILQ_NEXT(db_tr, link) == NULL){
1332103285Sikob			break;
1333103285Sikob		}
1334109892Ssimokawa		db = db_tr->db;
1335109892Ssimokawa		db[0].db.desc.depend = db[db_tr->dbcnt - 1].db.desc.depend
1336103285Sikob			= vtophys(STAILQ_NEXT(db_tr, link)->db) | z;
1337103285Sikob		if(dbch->xferq.flag & FWXFERQ_EXTBUF){
1338103285Sikob			if(((idb + 1 ) % dbch->xferq.bnpacket) == 0){
1339109892Ssimokawa				db[db_tr->dbcnt - 1].db.desc.control
1340103285Sikob					|= OHCI_INTERRUPT_ALWAYS;
1341109280Ssimokawa				/* OHCI 1.1 and above */
1342109892Ssimokawa				db[0].db.desc.control |= OHCI_INTERRUPT_ALWAYS;
1343109892Ssimokawa#if 0
1344109892Ssimokawa				db[0].db.desc.depend &= ~0xf;
1345109892Ssimokawa				db[db_tr->dbcnt - 1].db.desc.depend &= ~0xf;
1346109892Ssimokawa#endif
1347103285Sikob			}
1348103285Sikob		}
1349103285Sikob		db_tr = STAILQ_NEXT(db_tr, link);
1350103285Sikob	}
1351103285Sikob	dbch->bottom->db[db_tr->dbcnt - 1].db.desc.depend &= 0xfffffff0;
1352103285Sikob	return err;
1353103285Sikob}
1354106790Ssimokawa
1355106790Ssimokawastatic int
1356106790Ssimokawafwohci_rx_enable(struct fwohci_softc *sc, struct fwohci_dbch *dbch)
1357103285Sikob{
1358103285Sikob	int err = 0;
1359109892Ssimokawa	int idb, z, i, dmach = 0, ldesc;
1360103285Sikob	u_int32_t off = NULL;
1361103285Sikob	struct fwohcidb_tr *db_tr;
1362109892Ssimokawa	volatile struct fwohcidb *db;
1363103285Sikob
1364103285Sikob	z = dbch->ndesc;
1365103285Sikob	if(&sc->arrq == dbch){
1366103285Sikob		off = OHCI_ARQOFF;
1367103285Sikob	}else if(&sc->arrs == dbch){
1368103285Sikob		off = OHCI_ARSOFF;
1369103285Sikob	}else{
1370103285Sikob		for(dmach = 0 ; dmach < sc->fc.nisodma ; dmach++){
1371103285Sikob			if( &sc->ir[dmach] == dbch){
1372103285Sikob				off = OHCI_IROFF(dmach);
1373103285Sikob				break;
1374103285Sikob			}
1375103285Sikob		}
1376103285Sikob	}
1377103285Sikob	if(off == NULL){
1378103285Sikob		err = EINVAL;
1379103285Sikob		return err;
1380103285Sikob	}
1381103285Sikob	if(dbch->xferq.flag & FWXFERQ_STREAM){
1382103285Sikob		if(dbch->xferq.flag & FWXFERQ_RUNNING)
1383103285Sikob			return err;
1384103285Sikob	}else{
1385103285Sikob		if(dbch->xferq.flag & FWXFERQ_RUNNING){
1386103285Sikob			err = EBUSY;
1387103285Sikob			return err;
1388103285Sikob		}
1389103285Sikob	}
1390103285Sikob	dbch->xferq.flag |= FWXFERQ_RUNNING;
1391108642Ssimokawa	dbch->top = STAILQ_FIRST(&dbch->db_trq);
1392103285Sikob	for( i = 0, dbch->bottom = dbch->top; i < (dbch->ndb - 1); i++){
1393103285Sikob		dbch->bottom = STAILQ_NEXT(dbch->bottom, link);
1394103285Sikob	}
1395103285Sikob	db_tr = dbch->top;
1396103285Sikob	for( idb = 0 ; idb < dbch->ndb ; idb ++){
1397103285Sikob		if(!(dbch->xferq.flag & FWXFERQ_EXTBUF)){
1398103285Sikob			fwohci_add_rx_buf(db_tr,
1399103285Sikob				dbch->xferq.psize, dbch->xferq.flag, 0, NULL);
1400103285Sikob		}else{
1401103285Sikob			fwohci_add_rx_buf(db_tr,
1402103285Sikob				dbch->xferq.psize, dbch->xferq.flag,
1403103285Sikob				dbch->xferq.buf + dbch->xferq.psize * idb,
1404103285Sikob				dbch->dummy + sizeof(u_int32_t) * idb);
1405103285Sikob		}
1406103285Sikob		if(STAILQ_NEXT(db_tr, link) == NULL){
1407103285Sikob			break;
1408103285Sikob		}
1409109892Ssimokawa		db = db_tr->db;
1410109892Ssimokawa		ldesc = db_tr->dbcnt - 1;
1411109892Ssimokawa		db[ldesc].db.desc.depend
1412103285Sikob			= vtophys(STAILQ_NEXT(db_tr, link)->db) | z;
1413103285Sikob		if(dbch->xferq.flag & FWXFERQ_EXTBUF){
1414103285Sikob			if(((idb + 1 ) % dbch->xferq.bnpacket) == 0){
1415109892Ssimokawa				db[ldesc].db.desc.control
1416103285Sikob					|= OHCI_INTERRUPT_ALWAYS;
1417109892Ssimokawa				db[ldesc].db.desc.depend &= ~0xf;
1418103285Sikob			}
1419103285Sikob		}
1420103285Sikob		db_tr = STAILQ_NEXT(db_tr, link);
1421103285Sikob	}
1422103285Sikob	dbch->bottom->db[db_tr->dbcnt - 1].db.desc.depend &= 0xfffffff0;
1423103285Sikob	dbch->buf_offset = 0;
1424103285Sikob	if(dbch->xferq.flag & FWXFERQ_STREAM){
1425103285Sikob		return err;
1426103285Sikob	}else{
1427103285Sikob		OWRITE(sc, OHCI_DMACMD(off), vtophys(dbch->top->db) | z);
1428103285Sikob	}
1429103285Sikob	OWRITE(sc, OHCI_DMACTL(off), OHCI_CNTL_DMA_RUN);
1430103285Sikob	return err;
1431103285Sikob}
1432106790Ssimokawa
1433106790Ssimokawastatic int
1434109890Ssimokawafwochi_next_cycle(struct firewire_comm *fc, int cycle_now)
1435109890Ssimokawa{
1436109890Ssimokawa	int sec, cycle, cycle_match;
1437109890Ssimokawa
1438109890Ssimokawa	cycle = cycle_now & 0x1fff;
1439109890Ssimokawa	sec = cycle_now >> 13;
1440109890Ssimokawa#define CYCLE_MOD	0x10
1441109890Ssimokawa#define CYCLE_DELAY	8	/* min delay to start DMA */
1442109890Ssimokawa	cycle = cycle + CYCLE_DELAY;
1443109890Ssimokawa	if (cycle >= 8000) {
1444109890Ssimokawa		sec ++;
1445109890Ssimokawa		cycle -= 8000;
1446109890Ssimokawa	}
1447109890Ssimokawa	cycle = ((cycle + CYCLE_MOD - 1) / CYCLE_MOD) * CYCLE_MOD;
1448109890Ssimokawa	if (cycle >= 8000) {
1449109890Ssimokawa		sec ++;
1450109890Ssimokawa		if (cycle == 8000)
1451109890Ssimokawa			cycle = 0;
1452109890Ssimokawa		else
1453109890Ssimokawa			cycle = CYCLE_MOD;
1454109890Ssimokawa	}
1455109890Ssimokawa	cycle_match = ((sec << 13) | cycle) & 0x7ffff;
1456109890Ssimokawa
1457109890Ssimokawa	return(cycle_match);
1458109890Ssimokawa}
1459109890Ssimokawa
1460109890Ssimokawastatic int
1461106790Ssimokawafwohci_itxbuf_enable(struct firewire_comm *fc, int dmach)
1462103285Sikob{
1463103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
1464103285Sikob	int err = 0;
1465103285Sikob	unsigned short tag, ich;
1466103285Sikob	struct fwohci_dbch *dbch;
1467109890Ssimokawa	int cycle_match, cycle_now, s, ldesc;
1468109356Ssimokawa	u_int32_t stat;
1469109890Ssimokawa	struct fw_bulkxfer *first, *chunk, *prev;
1470109890Ssimokawa	struct fw_xferq *it;
1471103285Sikob
1472103285Sikob	dbch = &sc->it[dmach];
1473109890Ssimokawa	it = &dbch->xferq;
1474109890Ssimokawa
1475109890Ssimokawa	tag = (it->flag >> 6) & 3;
1476109890Ssimokawa	ich = it->flag & 0x3f;
1477109179Ssimokawa	if ((dbch->flags & FWOHCI_DBCH_INIT) == 0) {
1478109890Ssimokawa		dbch->ndb = it->bnpacket * it->bnchunk;
1479103285Sikob		dbch->ndesc = 3;
1480103285Sikob		fwohci_db_init(dbch);
1481109179Ssimokawa		if ((dbch->flags & FWOHCI_DBCH_INIT) == 0)
1482109179Ssimokawa			return ENOMEM;
1483103285Sikob		err = fwohci_tx_enable(sc, dbch);
1484103285Sikob	}
1485103285Sikob	if(err)
1486103285Sikob		return err;
1487109890Ssimokawa
1488109892Ssimokawa	ldesc = dbch->ndesc - 1;
1489109890Ssimokawa	s = splfw();
1490109890Ssimokawa	prev = STAILQ_LAST(&it->stdma, fw_bulkxfer, link);
1491109890Ssimokawa	while  ((chunk = STAILQ_FIRST(&it->stvalid)) != NULL) {
1492109890Ssimokawa		volatile struct fwohcidb *db;
1493109890Ssimokawa
1494109890Ssimokawa		fwohci_txbufdb(sc, dmach, chunk);
1495109892Ssimokawa#if 0
1496109890Ssimokawa		db = ((struct fwohcidb_tr *)(chunk->end))->db;
1497109890Ssimokawa		db[ldesc].db.desc.status = db[0].db.desc.status = 0;
1498109890Ssimokawa		db[ldesc].db.desc.count = db[0].db.desc.count = 0;
1499109890Ssimokawa		db[ldesc].db.desc.depend &= ~0xf;
1500109890Ssimokawa		db[0].db.desc.depend &= ~0xf;
1501109892Ssimokawa#endif
1502109890Ssimokawa		if (prev != NULL) {
1503109890Ssimokawa			db = ((struct fwohcidb_tr *)(prev->end))->db;
1504109892Ssimokawa			db[ldesc].db.desc.control |= OHCI_BRANCH_ALWAYS;
1505109892Ssimokawa#if 0 /* if bulkxfer->npacket changes */
1506109890Ssimokawa			db[ldesc].db.desc.depend = db[0].db.desc.depend =
1507109890Ssimokawa				vtophys(((struct fwohcidb_tr *)
1508109890Ssimokawa					(chunk->start))->db) | dbch->ndesc;
1509109892Ssimokawa#else
1510109892Ssimokawa			db[0].db.desc.depend |=  dbch->ndesc;
1511109892Ssimokawa			db[ldesc].db.desc.depend |= dbch->ndesc;
1512109892Ssimokawa#endif
1513103285Sikob		}
1514109890Ssimokawa		STAILQ_REMOVE_HEAD(&it->stvalid, link);
1515109890Ssimokawa		STAILQ_INSERT_TAIL(&it->stdma, chunk, link);
1516109890Ssimokawa		prev = chunk;
1517109403Ssimokawa	}
1518109890Ssimokawa	splx(s);
1519109890Ssimokawa	stat = OREAD(sc, OHCI_ITCTL(dmach));
1520109890Ssimokawa	if (stat & (OHCI_CNTL_DMA_ACTIVE | OHCI_CNTL_CYCMATCH_S))
1521109890Ssimokawa		return 0;
1522109890Ssimokawa
1523109890Ssimokawa	OWRITE(sc, OHCI_ITCTLCLR(dmach), OHCI_CNTL_DMA_RUN);
1524109403Ssimokawa	OWRITE(sc, OHCI_IT_MASKCLR, 1 << dmach);
1525109403Ssimokawa	OWRITE(sc, OHCI_IT_STATCLR, 1 << dmach);
1526109403Ssimokawa	OWRITE(sc, OHCI_IT_MASK, 1 << dmach);
1527109890Ssimokawa
1528109890Ssimokawa	first = STAILQ_FIRST(&it->stdma);
1529109890Ssimokawa	OWRITE(sc, OHCI_ITCMD(dmach), vtophys(((struct fwohcidb_tr *)
1530109890Ssimokawa					(first->start))->db) | dbch->ndesc);
1531109890Ssimokawa	if (firewire_debug)
1532109890Ssimokawa		printf("fwohci_itxbuf_enable: kick 0x%08x\n", stat);
1533109403Ssimokawa	if ((stat & OHCI_CNTL_DMA_RUN) == 0) {
1534109890Ssimokawa#if 1
1535109890Ssimokawa		/* Don't start until all chunks are buffered */
1536109890Ssimokawa		if (STAILQ_FIRST(&it->stfree) != NULL)
1537109890Ssimokawa			goto out;
1538109890Ssimokawa#endif
1539109802Ssimokawa#ifdef FWXFERQ_DV
1540109890Ssimokawa#define CYCLE_OFFSET	1
1541103285Sikob		if(dbch->xferq.flag & FWXFERQ_DV){
1542109802Ssimokawa			struct fw_pkt *fp;
1543109802Ssimokawa			struct fwohcidb_tr *db_tr;
1544109802Ssimokawa
1545103285Sikob			db_tr = (struct fwohcidb_tr *)dbch->xferq.stdma->start;
1546103285Sikob			fp = (struct fw_pkt *)db_tr->buf;
1547109356Ssimokawa			dbch->xferq.dvoffset = CYCLE_OFFSET;
1548109179Ssimokawa			fp->mode.ld[2] |= htonl(dbch->xferq.dvoffset << 12);
1549103285Sikob		}
1550109802Ssimokawa#endif
1551109890Ssimokawa		/* Clear cycle match counter bits */
1552109890Ssimokawa		OWRITE(sc, OHCI_ITCTLCLR(dmach), 0xffff0000);
1553109890Ssimokawa		OWRITE(sc, FWOHCI_INTMASK, OHCI_INT_DMA_IT);
1554109890Ssimokawa
1555109356Ssimokawa		/* 2bit second + 13bit cycle */
1556109356Ssimokawa		cycle_now = (fc->cyctimer(fc) >> 12) & 0x7fff;
1557109890Ssimokawa		cycle_match = fwochi_next_cycle(fc, cycle_now);
1558109890Ssimokawa
1559109356Ssimokawa		OWRITE(sc, OHCI_ITCTL(dmach),
1560109356Ssimokawa				OHCI_CNTL_CYCMATCH_S | (cycle_match << 16)
1561109356Ssimokawa				| OHCI_CNTL_DMA_RUN);
1562109403Ssimokawa		if (firewire_debug)
1563109403Ssimokawa			printf("cycle_match: 0x%04x->0x%04x\n",
1564109403Ssimokawa						cycle_now, cycle_match);
1565109403Ssimokawa	} else if ((stat & OHCI_CNTL_CYCMATCH_S) == 0) {
1566109890Ssimokawa		device_printf(sc->fc.dev,
1567109890Ssimokawa			"IT DMA underrun (0x%08x)\n", stat);
1568109403Ssimokawa		OWRITE(sc, OHCI_ITCTL(dmach), OHCI_CNTL_DMA_RUN);
1569103285Sikob	}
1570109890Ssimokawaout:
1571103285Sikob	return err;
1572103285Sikob}
1573106790Ssimokawa
1574106790Ssimokawastatic int
1575106790Ssimokawafwohci_irxbuf_enable(struct firewire_comm *fc, int dmach)
1576103285Sikob{
1577103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
1578109890Ssimokawa	int err = 0, s, ldesc;
1579103285Sikob	unsigned short tag, ich;
1580109736Ssimokawa	u_int32_t stat;
1581109890Ssimokawa	struct fwohci_dbch *dbch;
1582109890Ssimokawa	struct fw_bulkxfer *first, *prev, *chunk;
1583109890Ssimokawa	struct fw_xferq *ir;
1584103285Sikob
1585109890Ssimokawa	dbch = &sc->ir[dmach];
1586109890Ssimokawa	ir = &dbch->xferq;
1587109890Ssimokawa	ldesc = dbch->ndesc - 1;
1588109890Ssimokawa
1589109890Ssimokawa	if ((ir->flag & FWXFERQ_RUNNING) == 0) {
1590109890Ssimokawa		tag = (ir->flag >> 6) & 3;
1591109890Ssimokawa		ich = ir->flag & 0x3f;
1592108995Ssimokawa		OWRITE(sc, OHCI_IRMATCH(dmach), tagbit[tag] | ich);
1593108995Ssimokawa
1594109890Ssimokawa		ir->queued = 0;
1595109890Ssimokawa		dbch->ndb = ir->bnpacket * ir->bnchunk;
1596109890Ssimokawa		dbch->dummy = malloc(sizeof(u_int32_t) * dbch->ndb,
1597109890Ssimokawa			   	M_DEVBUF, M_NOWAIT);
1598109890Ssimokawa		if (dbch->dummy == NULL) {
1599103285Sikob			err = ENOMEM;
1600103285Sikob			return err;
1601103285Sikob		}
1602109890Ssimokawa		dbch->ndesc = 2;
1603109890Ssimokawa		fwohci_db_init(dbch);
1604109890Ssimokawa		if ((dbch->flags & FWOHCI_DBCH_INIT) == 0)
1605109179Ssimokawa			return ENOMEM;
1606109890Ssimokawa		err = fwohci_rx_enable(sc, dbch);
1607103285Sikob	}
1608103285Sikob	if(err)
1609103285Sikob		return err;
1610103285Sikob
1611109890Ssimokawa	s = splfw();
1612103285Sikob
1613109890Ssimokawa	first = STAILQ_FIRST(&ir->stfree);
1614109890Ssimokawa	if (first == NULL) {
1615109890Ssimokawa		device_printf(fc->dev, "IR DMA no free chunk\n");
1616109890Ssimokawa		splx(s);
1617109890Ssimokawa		return 0;
1618109890Ssimokawa	}
1619109890Ssimokawa
1620109890Ssimokawa	prev = STAILQ_LAST(&ir->stdma, fw_bulkxfer, link);
1621109890Ssimokawa	while  ((chunk = STAILQ_FIRST(&ir->stfree)) != NULL) {
1622109890Ssimokawa		volatile struct fwohcidb *db;
1623109890Ssimokawa
1624109890Ssimokawa		db = ((struct fwohcidb_tr *)(chunk->end))->db;
1625109890Ssimokawa		db[ldesc].db.desc.status = db[ldesc].db.desc.count = 0;
1626109890Ssimokawa		db[ldesc].db.desc.depend &= ~0xf;
1627109890Ssimokawa		if (prev != NULL) {
1628109890Ssimokawa			db = ((struct fwohcidb_tr *)(prev->end))->db;
1629109892Ssimokawa#if 0
1630109890Ssimokawa			db[ldesc].db.desc.depend =
1631109890Ssimokawa				vtophys(((struct fwohcidb_tr *)
1632109890Ssimokawa					(chunk->start))->db) | dbch->ndesc;
1633109892Ssimokawa#else
1634109892Ssimokawa			db[ldesc].db.desc.depend |= dbch->ndesc;
1635109892Ssimokawa#endif
1636103285Sikob		}
1637109890Ssimokawa		STAILQ_REMOVE_HEAD(&ir->stfree, link);
1638109890Ssimokawa		STAILQ_INSERT_TAIL(&ir->stdma, chunk, link);
1639109890Ssimokawa		prev = chunk;
1640103285Sikob	}
1641109890Ssimokawa	splx(s);
1642109890Ssimokawa	stat = OREAD(sc, OHCI_IRCTL(dmach));
1643109890Ssimokawa	if (stat & OHCI_CNTL_DMA_ACTIVE)
1644109890Ssimokawa		return 0;
1645109890Ssimokawa	if (stat & OHCI_CNTL_DMA_RUN) {
1646109890Ssimokawa		OWRITE(sc, OHCI_IRCTLCLR(dmach), OHCI_CNTL_DMA_RUN);
1647109890Ssimokawa		device_printf(sc->fc.dev, "IR DMA overrun (0x%08x)\n", stat);
1648109890Ssimokawa	}
1649109890Ssimokawa
1650109890Ssimokawa	OWRITE(sc, OHCI_IR_MASKCLR, 1 << dmach);
1651109890Ssimokawa	OWRITE(sc, OHCI_IR_STATCLR, 1 << dmach);
1652109890Ssimokawa	OWRITE(sc, OHCI_IR_MASK, 1 << dmach);
1653109890Ssimokawa	OWRITE(sc, OHCI_IRCTLCLR(dmach), 0xf0000000);
1654109890Ssimokawa	OWRITE(sc, OHCI_IRCTL(dmach), OHCI_CNTL_ISOHDR);
1655109890Ssimokawa	OWRITE(sc, OHCI_IRCMD(dmach),
1656109890Ssimokawa		vtophys(((struct fwohcidb_tr *)(first->start))->db)
1657109890Ssimokawa							| dbch->ndesc);
1658109890Ssimokawa	OWRITE(sc, OHCI_IRCTL(dmach), OHCI_CNTL_DMA_RUN);
1659109890Ssimokawa	OWRITE(sc, FWOHCI_INTMASK, OHCI_INT_DMA_IR);
1660103285Sikob	return err;
1661103285Sikob}
1662106790Ssimokawa
1663106790Ssimokawastatic int
1664106790Ssimokawafwohci_irx_enable(struct firewire_comm *fc, int dmach)
1665103285Sikob{
1666103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)fc;
1667103285Sikob	int err = 0;
1668103285Sikob
1669103285Sikob	if(sc->ir[dmach].xferq.flag & FWXFERQ_PACKET){
1670103285Sikob		err = fwohci_irxpp_enable(fc, dmach);
1671103285Sikob		return err;
1672103285Sikob	}else{
1673103285Sikob		err = fwohci_irxbuf_enable(fc, dmach);
1674103285Sikob		return err;
1675103285Sikob	}
1676103285Sikob}
1677106790Ssimokawa
1678106790Ssimokawaint
1679108642Ssimokawafwohci_shutdown(struct fwohci_softc *sc, device_t dev)
1680103285Sikob{
1681103285Sikob	u_int i;
1682103285Sikob
1683103285Sikob/* Now stopping all DMA channel */
1684103285Sikob	OWRITE(sc,  OHCI_ARQCTLCLR, OHCI_CNTL_DMA_RUN);
1685103285Sikob	OWRITE(sc,  OHCI_ARSCTLCLR, OHCI_CNTL_DMA_RUN);
1686103285Sikob	OWRITE(sc,  OHCI_ATQCTLCLR, OHCI_CNTL_DMA_RUN);
1687103285Sikob	OWRITE(sc,  OHCI_ATSCTLCLR, OHCI_CNTL_DMA_RUN);
1688103285Sikob
1689103285Sikob	for( i = 0 ; i < sc->fc.nisodma ; i ++ ){
1690103285Sikob		OWRITE(sc,  OHCI_IRCTLCLR(i), OHCI_CNTL_DMA_RUN);
1691103285Sikob		OWRITE(sc,  OHCI_ITCTLCLR(i), OHCI_CNTL_DMA_RUN);
1692103285Sikob	}
1693103285Sikob
1694103285Sikob/* FLUSH FIFO and reset Transmitter/Reciever */
1695103285Sikob	OWRITE(sc,  OHCI_HCCCTL, OHCI_HCC_RESET);
1696103285Sikob
1697103285Sikob/* Stop interrupt */
1698103285Sikob	OWRITE(sc, FWOHCI_INTMASKCLR,
1699103285Sikob			OHCI_INT_EN | OHCI_INT_ERR | OHCI_INT_PHY_SID
1700103285Sikob			| OHCI_INT_PHY_INT
1701103285Sikob			| OHCI_INT_DMA_ATRQ | OHCI_INT_DMA_ATRS
1702103285Sikob			| OHCI_INT_DMA_PRRQ | OHCI_INT_DMA_PRRS
1703103285Sikob			| OHCI_INT_DMA_ARRQ | OHCI_INT_DMA_ARRS
1704103285Sikob			| OHCI_INT_PHY_BUS_R);
1705108642Ssimokawa/* XXX Link down?  Bus reset? */
1706103285Sikob	return 0;
1707103285Sikob}
1708103285Sikob
1709108642Ssimokawaint
1710108642Ssimokawafwohci_resume(struct fwohci_softc *sc, device_t dev)
1711108642Ssimokawa{
1712108642Ssimokawa	int i;
1713108642Ssimokawa
1714108642Ssimokawa	fwohci_reset(sc, dev);
1715108642Ssimokawa	/* XXX resume isochronus receive automatically. (how about TX?) */
1716108642Ssimokawa	for(i = 0; i < sc->fc.nisodma; i ++) {
1717108642Ssimokawa		if((sc->ir[i].xferq.flag & FWXFERQ_RUNNING) != 0) {
1718108642Ssimokawa			device_printf(sc->fc.dev,
1719108642Ssimokawa				"resume iso receive ch: %d\n", i);
1720108642Ssimokawa			sc->ir[i].xferq.flag &= ~FWXFERQ_RUNNING;
1721108642Ssimokawa			sc->fc.irx_enable(&sc->fc, i);
1722108642Ssimokawa		}
1723108642Ssimokawa	}
1724108642Ssimokawa
1725108642Ssimokawa	bus_generic_resume(dev);
1726108642Ssimokawa	sc->fc.ibr(&sc->fc);
1727108642Ssimokawa	return 0;
1728108642Ssimokawa}
1729108642Ssimokawa
1730103285Sikob#define ACK_ALL
1731103285Sikobstatic void
1732106789Ssimokawafwohci_intr_body(struct fwohci_softc *sc, u_int32_t stat, int count)
1733103285Sikob{
1734103285Sikob	u_int32_t irstat, itstat;
1735103285Sikob	u_int i;
1736103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)sc;
1737103285Sikob
1738103285Sikob#ifdef OHCI_DEBUG
1739103285Sikob	if(stat & OREAD(sc, FWOHCI_INTMASK))
1740103285Sikob		device_printf(fc->dev, "INTERRUPT < %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s> 0x%08x, 0x%08x\n",
1741103285Sikob			stat & OHCI_INT_EN ? "DMA_EN ":"",
1742103285Sikob			stat & OHCI_INT_PHY_REG ? "PHY_REG ":"",
1743103285Sikob			stat & OHCI_INT_CYC_LONG ? "CYC_LONG ":"",
1744103285Sikob			stat & OHCI_INT_ERR ? "INT_ERR ":"",
1745103285Sikob			stat & OHCI_INT_CYC_ERR ? "CYC_ERR ":"",
1746103285Sikob			stat & OHCI_INT_CYC_LOST ? "CYC_LOST ":"",
1747103285Sikob			stat & OHCI_INT_CYC_64SECOND ? "CYC_64SECOND ":"",
1748103285Sikob			stat & OHCI_INT_CYC_START ? "CYC_START ":"",
1749103285Sikob			stat & OHCI_INT_PHY_INT ? "PHY_INT ":"",
1750103285Sikob			stat & OHCI_INT_PHY_BUS_R ? "BUS_RESET ":"",
1751103285Sikob			stat & OHCI_INT_PHY_SID ? "SID ":"",
1752103285Sikob			stat & OHCI_INT_LR_ERR ? "DMA_LR_ERR ":"",
1753103285Sikob			stat & OHCI_INT_PW_ERR ? "DMA_PW_ERR ":"",
1754103285Sikob			stat & OHCI_INT_DMA_IR ? "DMA_IR ":"",
1755103285Sikob			stat & OHCI_INT_DMA_IT  ? "DMA_IT " :"",
1756103285Sikob			stat & OHCI_INT_DMA_PRRS  ? "DMA_PRRS " :"",
1757103285Sikob			stat & OHCI_INT_DMA_PRRQ  ? "DMA_PRRQ " :"",
1758103285Sikob			stat & OHCI_INT_DMA_ARRS  ? "DMA_ARRS " :"",
1759103285Sikob			stat & OHCI_INT_DMA_ARRQ  ? "DMA_ARRQ " :"",
1760103285Sikob			stat & OHCI_INT_DMA_ATRS  ? "DMA_ATRS " :"",
1761103285Sikob			stat & OHCI_INT_DMA_ATRQ  ? "DMA_ATRQ " :"",
1762103285Sikob			stat, OREAD(sc, FWOHCI_INTMASK)
1763103285Sikob		);
1764103285Sikob#endif
1765103285Sikob/* Bus reset */
1766103285Sikob	if(stat & OHCI_INT_PHY_BUS_R ){
1767103285Sikob		device_printf(fc->dev, "BUS reset\n");
1768103285Sikob		OWRITE(sc, FWOHCI_INTMASKCLR,  OHCI_INT_CYC_LOST);
1769103285Sikob		OWRITE(sc, OHCI_LNKCTLCLR, OHCI_CNTL_CYCSRC);
1770103285Sikob
1771103285Sikob		OWRITE(sc,  OHCI_ATQCTLCLR, OHCI_CNTL_DMA_RUN);
1772103285Sikob		sc->atrq.xferq.flag &= ~FWXFERQ_RUNNING;
1773103285Sikob		OWRITE(sc,  OHCI_ATSCTLCLR, OHCI_CNTL_DMA_RUN);
1774103285Sikob		sc->atrs.xferq.flag &= ~FWXFERQ_RUNNING;
1775103285Sikob
1776103285Sikob#if 0
1777103285Sikob		for( i = 0 ; i < fc->nisodma ; i ++ ){
1778103285Sikob			OWRITE(sc,  OHCI_IRCTLCLR(i), OHCI_CNTL_DMA_RUN);
1779103285Sikob			OWRITE(sc,  OHCI_ITCTLCLR(i), OHCI_CNTL_DMA_RUN);
1780103285Sikob		}
1781103285Sikob
1782103285Sikob#endif
1783103285Sikob		fw_busreset(fc);
1784103285Sikob
1785103285Sikob		/* XXX need to wait DMA to stop */
1786103285Sikob#ifndef ACK_ALL
1787103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_PHY_BUS_R);
1788103285Sikob#endif
1789103285Sikob#if 1
1790103285Sikob		/* pending all pre-bus_reset packets */
1791103285Sikob		fwohci_txd(sc, &sc->atrq);
1792103285Sikob		fwohci_txd(sc, &sc->atrs);
1793106789Ssimokawa		fwohci_arcv(sc, &sc->arrs, -1);
1794106789Ssimokawa		fwohci_arcv(sc, &sc->arrq, -1);
1795103285Sikob#endif
1796103285Sikob
1797103285Sikob
1798103285Sikob		OWRITE(sc, OHCI_AREQHI, 1 << 31);
1799103285Sikob		/* XXX insecure ?? */
1800103285Sikob		OWRITE(sc, OHCI_PREQHI, 0x7fffffff);
1801103285Sikob		OWRITE(sc, OHCI_PREQLO, 0xffffffff);
1802103285Sikob		OWRITE(sc, OHCI_PREQUPPER, 0x10000);
1803103285Sikob
1804103285Sikob	}
1805103285Sikob	if((stat & OHCI_INT_DMA_IR )){
1806103285Sikob#ifndef ACK_ALL
1807103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_DMA_IR);
1808103285Sikob#endif
1809103285Sikob		irstat = OREAD(sc, OHCI_IR_STAT);
1810109280Ssimokawa		OWRITE(sc, OHCI_IR_STATCLR, irstat);
1811103285Sikob		for(i = 0; i < fc->nisodma ; i++){
1812109644Ssimokawa			struct fwohci_dbch *dbch;
1813109644Ssimokawa
1814103285Sikob			if((irstat & (1 << i)) != 0){
1815109644Ssimokawa				dbch = &sc->ir[i];
1816109644Ssimokawa				if ((dbch->xferq.flag & FWXFERQ_OPEN) == 0) {
1817109644Ssimokawa					device_printf(sc->fc.dev,
1818109644Ssimokawa						"dma(%d) not active\n", i);
1819109644Ssimokawa					continue;
1820109644Ssimokawa				}
1821109644Ssimokawa				if (dbch->xferq.flag & FWXFERQ_PACKET) {
1822109644Ssimokawa					fwohci_ircv(sc, dbch, count);
1823109644Ssimokawa				} else {
1824103285Sikob					fwohci_rbuf_update(sc, i);
1825103285Sikob				}
1826103285Sikob			}
1827103285Sikob		}
1828103285Sikob	}
1829103285Sikob	if((stat & OHCI_INT_DMA_IT )){
1830103285Sikob#ifndef ACK_ALL
1831103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_DMA_IT);
1832103285Sikob#endif
1833103285Sikob		itstat = OREAD(sc, OHCI_IT_STAT);
1834109280Ssimokawa		OWRITE(sc, OHCI_IT_STATCLR, itstat);
1835103285Sikob		for(i = 0; i < fc->nisodma ; i++){
1836103285Sikob			if((itstat & (1 << i)) != 0){
1837103285Sikob				fwohci_tbuf_update(sc, i);
1838103285Sikob			}
1839103285Sikob		}
1840103285Sikob	}
1841103285Sikob	if((stat & OHCI_INT_DMA_PRRS )){
1842103285Sikob#ifndef ACK_ALL
1843103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_DMA_PRRS);
1844103285Sikob#endif
1845103285Sikob#if 0
1846103285Sikob		dump_dma(sc, ARRS_CH);
1847103285Sikob		dump_db(sc, ARRS_CH);
1848103285Sikob#endif
1849106789Ssimokawa		fwohci_arcv(sc, &sc->arrs, count);
1850103285Sikob	}
1851103285Sikob	if((stat & OHCI_INT_DMA_PRRQ )){
1852103285Sikob#ifndef ACK_ALL
1853103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_DMA_PRRQ);
1854103285Sikob#endif
1855103285Sikob#if 0
1856103285Sikob		dump_dma(sc, ARRQ_CH);
1857103285Sikob		dump_db(sc, ARRQ_CH);
1858103285Sikob#endif
1859106789Ssimokawa		fwohci_arcv(sc, &sc->arrq, count);
1860103285Sikob	}
1861103285Sikob	if(stat & OHCI_INT_PHY_SID){
1862103285Sikob		caddr_t buf;
1863103285Sikob		int plen;
1864103285Sikob
1865103285Sikob#ifndef ACK_ALL
1866103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_PHY_SID);
1867103285Sikob#endif
1868103285Sikob/*
1869103285Sikob** Checking whether the node is root or not. If root, turn on
1870103285Sikob** cycle master.
1871103285Sikob*/
1872103285Sikob		device_printf(fc->dev, "node_id = 0x%08x, ", OREAD(sc, FWOHCI_NODEID));
1873103285Sikob		if(!(OREAD(sc, FWOHCI_NODEID) & OHCI_NODE_VALID)){
1874103285Sikob			printf("Bus reset failure\n");
1875103285Sikob			goto sidout;
1876103285Sikob		}
1877103285Sikob		if( OREAD(sc, FWOHCI_NODEID) & OHCI_NODE_ROOT ){
1878103285Sikob			printf("CYCLEMASTER mode\n");
1879103285Sikob			OWRITE(sc, OHCI_LNKCTL,
1880103285Sikob				OHCI_CNTL_CYCMTR | OHCI_CNTL_CYCTIMER);
1881103285Sikob		}else{
1882103285Sikob			printf("non CYCLEMASTER mode\n");
1883103285Sikob			OWRITE(sc, OHCI_LNKCTLCLR, OHCI_CNTL_CYCMTR);
1884103285Sikob			OWRITE(sc, OHCI_LNKCTL, OHCI_CNTL_CYCTIMER);
1885103285Sikob		}
1886103285Sikob		fc->nodeid = OREAD(sc, FWOHCI_NODEID) & 0x3f;
1887103285Sikob
1888103285Sikob		plen = OREAD(sc, OHCI_SID_CNT) & OHCI_SID_CNT_MASK;
1889109736Ssimokawa		if (plen < 4 || plen > OHCI_SIDSIZE) {
1890109736Ssimokawa			device_printf(fc->dev, "invalid SID len = %d\n", plen);
1891109736Ssimokawa			goto sidout;
1892109736Ssimokawa		}
1893103285Sikob		plen -= 4; /* chop control info */
1894109379Ssimokawa		buf = malloc(OHCI_SIDSIZE, M_DEVBUF, M_NOWAIT);
1895103285Sikob		if(buf == NULL) goto sidout;
1896108530Ssimokawa		bcopy((void *)(uintptr_t)(volatile void *)(fc->sid_buf + 1),
1897103285Sikob								buf, plen);
1898103285Sikob		fw_sidrcv(fc, buf, plen, 0);
1899103285Sikob	}
1900103285Sikobsidout:
1901103285Sikob	if((stat & OHCI_INT_DMA_ATRQ )){
1902103285Sikob#ifndef ACK_ALL
1903103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_DMA_ATRQ);
1904103285Sikob#endif
1905103285Sikob		fwohci_txd(sc, &(sc->atrq));
1906103285Sikob	}
1907103285Sikob	if((stat & OHCI_INT_DMA_ATRS )){
1908103285Sikob#ifndef ACK_ALL
1909103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_DMA_ATRS);
1910103285Sikob#endif
1911103285Sikob		fwohci_txd(sc, &(sc->atrs));
1912103285Sikob	}
1913103285Sikob	if((stat & OHCI_INT_PW_ERR )){
1914103285Sikob#ifndef ACK_ALL
1915103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_PW_ERR);
1916103285Sikob#endif
1917103285Sikob		device_printf(fc->dev, "posted write error\n");
1918103285Sikob	}
1919103285Sikob	if((stat & OHCI_INT_ERR )){
1920103285Sikob#ifndef ACK_ALL
1921103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_ERR);
1922103285Sikob#endif
1923103285Sikob		device_printf(fc->dev, "unrecoverable error\n");
1924103285Sikob	}
1925103285Sikob	if((stat & OHCI_INT_PHY_INT)) {
1926103285Sikob#ifndef ACK_ALL
1927103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, OHCI_INT_PHY_INT);
1928103285Sikob#endif
1929103285Sikob		device_printf(fc->dev, "phy int\n");
1930103285Sikob	}
1931103285Sikob
1932103285Sikob	return;
1933103285Sikob}
1934103285Sikob
1935103285Sikobvoid
1936103285Sikobfwohci_intr(void *arg)
1937103285Sikob{
1938103285Sikob	struct fwohci_softc *sc = (struct fwohci_softc *)arg;
1939103285Sikob	u_int32_t stat;
1940103285Sikob
1941103285Sikob	if (!(sc->intmask & OHCI_INT_EN)) {
1942103285Sikob		/* polling mode */
1943103285Sikob		return;
1944103285Sikob	}
1945103285Sikob
1946103285Sikob	while ((stat = OREAD(sc, FWOHCI_INTSTAT)) != 0) {
1947103285Sikob		if (stat == 0xffffffff) {
1948103285Sikob			device_printf(sc->fc.dev,
1949103285Sikob				"device physically ejected?\n");
1950103285Sikob			return;
1951103285Sikob		}
1952103285Sikob#ifdef ACK_ALL
1953103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, stat);
1954103285Sikob#endif
1955106789Ssimokawa		fwohci_intr_body(sc, stat, -1);
1956103285Sikob	}
1957103285Sikob}
1958103285Sikob
1959103285Sikobstatic void
1960103285Sikobfwohci_poll(struct firewire_comm *fc, int quick, int count)
1961103285Sikob{
1962103285Sikob	int s;
1963103285Sikob	u_int32_t stat;
1964103285Sikob	struct fwohci_softc *sc;
1965103285Sikob
1966103285Sikob
1967103285Sikob	sc = (struct fwohci_softc *)fc;
1968103285Sikob	stat = OHCI_INT_DMA_IR | OHCI_INT_DMA_IT |
1969103285Sikob		OHCI_INT_DMA_PRRS | OHCI_INT_DMA_PRRQ |
1970103285Sikob		OHCI_INT_DMA_ATRQ | OHCI_INT_DMA_ATRS;
1971103285Sikob#if 0
1972103285Sikob	if (!quick) {
1973103285Sikob#else
1974103285Sikob	if (1) {
1975103285Sikob#endif
1976103285Sikob		stat = OREAD(sc, FWOHCI_INTSTAT);
1977103285Sikob		if (stat == 0)
1978103285Sikob			return;
1979103285Sikob		if (stat == 0xffffffff) {
1980103285Sikob			device_printf(sc->fc.dev,
1981103285Sikob				"device physically ejected?\n");
1982103285Sikob			return;
1983103285Sikob		}
1984103285Sikob#ifdef ACK_ALL
1985103285Sikob		OWRITE(sc, FWOHCI_INTSTATCLR, stat);
1986103285Sikob#endif
1987103285Sikob	}
1988103285Sikob	s = splfw();
1989106789Ssimokawa	fwohci_intr_body(sc, stat, count);
1990103285Sikob	splx(s);
1991103285Sikob}
1992103285Sikob
1993103285Sikobstatic void
1994103285Sikobfwohci_set_intr(struct firewire_comm *fc, int enable)
1995103285Sikob{
1996103285Sikob	struct fwohci_softc *sc;
1997103285Sikob
1998103285Sikob	sc = (struct fwohci_softc *)fc;
1999107653Ssimokawa	if (bootverbose)
2000108642Ssimokawa		device_printf(sc->fc.dev, "fwohci_set_intr: %d\n", enable);
2001103285Sikob	if (enable) {
2002103285Sikob		sc->intmask |= OHCI_INT_EN;
2003103285Sikob		OWRITE(sc, FWOHCI_INTMASK, OHCI_INT_EN);
2004103285Sikob	} else {
2005103285Sikob		sc->intmask &= ~OHCI_INT_EN;
2006103285Sikob		OWRITE(sc, FWOHCI_INTMASKCLR, OHCI_INT_EN);
2007103285Sikob	}
2008103285Sikob}
2009103285Sikob
2010106790Ssimokawastatic void
2011106790Ssimokawafwohci_tbuf_update(struct fwohci_softc *sc, int dmach)
2012103285Sikob{
2013103285Sikob	struct firewire_comm *fc = &sc->fc;
2014109890Ssimokawa	volatile struct fwohcidb *db;
2015109890Ssimokawa	struct fw_bulkxfer *chunk;
2016109890Ssimokawa	struct fw_xferq *it;
2017109890Ssimokawa	u_int32_t stat, count;
2018109890Ssimokawa	int s, w=0;
2019103285Sikob
2020109890Ssimokawa	it = fc->it[dmach];
2021109890Ssimokawa	s = splfw(); /* unnecessary ? */
2022109890Ssimokawa	while ((chunk = STAILQ_FIRST(&it->stdma)) != NULL) {
2023109890Ssimokawa		db = ((struct fwohcidb_tr *)(chunk->end))->db;
2024109890Ssimokawa		stat = db[sc->it[dmach].ndesc - 1].db.desc.status;
2025109890Ssimokawa		db = ((struct fwohcidb_tr *)(chunk->start))->db;
2026109890Ssimokawa		count = db[sc->it[dmach].ndesc - 1].db.desc.count;
2027109890Ssimokawa		if (stat == 0)
2028109890Ssimokawa			break;
2029109890Ssimokawa		STAILQ_REMOVE_HEAD(&it->stdma, link);
2030109890Ssimokawa		switch (stat & FWOHCIEV_MASK){
2031109890Ssimokawa		case FWOHCIEV_ACKCOMPL:
2032109890Ssimokawa#if 0
2033109890Ssimokawa			device_printf(fc->dev, "0x%08x\n", count);
2034109179Ssimokawa#endif
2035109890Ssimokawa			break;
2036109890Ssimokawa		default:
2037109423Ssimokawa			device_printf(fc->dev,
2038109890Ssimokawa				"Isochronous transmit err %02x\n", stat);
2039109890Ssimokawa		}
2040109890Ssimokawa		STAILQ_INSERT_TAIL(&it->stfree, chunk, link);
2041109890Ssimokawa		w++;
2042109403Ssimokawa	}
2043109890Ssimokawa	splx(s);
2044109890Ssimokawa	if (w)
2045109890Ssimokawa		wakeup(it);
2046103285Sikob}
2047106790Ssimokawa
2048106790Ssimokawastatic void
2049106790Ssimokawafwohci_rbuf_update(struct fwohci_softc *sc, int dmach)
2050103285Sikob{
2051109179Ssimokawa	struct firewire_comm *fc = &sc->fc;
2052109890Ssimokawa	volatile struct fwohcidb *db;
2053109890Ssimokawa	struct fw_bulkxfer *chunk;
2054109890Ssimokawa	struct fw_xferq *ir;
2055109890Ssimokawa	u_int32_t stat;
2056109890Ssimokawa	int s, w=0;
2057109179Ssimokawa
2058109890Ssimokawa	ir = fc->ir[dmach];
2059109890Ssimokawa	s = splfw();
2060109890Ssimokawa	while ((chunk = STAILQ_FIRST(&ir->stdma)) != NULL) {
2061109890Ssimokawa		db = ((struct fwohcidb_tr *)(chunk->end))->db;
2062109890Ssimokawa		stat = db[sc->ir[dmach].ndesc - 1].db.desc.status;
2063109890Ssimokawa		if (stat == 0)
2064109890Ssimokawa			break;
2065109890Ssimokawa		STAILQ_REMOVE_HEAD(&ir->stdma, link);
2066109890Ssimokawa		STAILQ_INSERT_TAIL(&ir->stvalid, chunk, link);
2067109890Ssimokawa		switch (stat & FWOHCIEV_MASK) {
2068109890Ssimokawa		case FWOHCIEV_ACKCOMPL:
2069109890Ssimokawa			break;
2070109890Ssimokawa		default:
2071109890Ssimokawa			device_printf(fc->dev,
2072109890Ssimokawa				"Isochronous receive err %02x\n", stat);
2073109890Ssimokawa		}
2074109890Ssimokawa		w++;
2075103285Sikob	}
2076109890Ssimokawa	splx(s);
2077109890Ssimokawa	if (w)
2078109890Ssimokawa		wakeup(ir);
2079103285Sikob}
2080106790Ssimokawa
2081106790Ssimokawavoid
2082106790Ssimokawadump_dma(struct fwohci_softc *sc, u_int32_t ch)
2083106790Ssimokawa{
2084103285Sikob	u_int32_t off, cntl, stat, cmd, match;
2085103285Sikob
2086103285Sikob	if(ch == 0){
2087103285Sikob		off = OHCI_ATQOFF;
2088103285Sikob	}else if(ch == 1){
2089103285Sikob		off = OHCI_ATSOFF;
2090103285Sikob	}else if(ch == 2){
2091103285Sikob		off = OHCI_ARQOFF;
2092103285Sikob	}else if(ch == 3){
2093103285Sikob		off = OHCI_ARSOFF;
2094103285Sikob	}else if(ch < IRX_CH){
2095103285Sikob		off = OHCI_ITCTL(ch - ITX_CH);
2096103285Sikob	}else{
2097103285Sikob		off = OHCI_IRCTL(ch - IRX_CH);
2098103285Sikob	}
2099103285Sikob	cntl = stat = OREAD(sc, off);
2100103285Sikob	cmd = OREAD(sc, off + 0xc);
2101103285Sikob	match = OREAD(sc, off + 0x10);
2102103285Sikob
2103103285Sikob	device_printf(sc->fc.dev, "dma ch %1x:dma regs 0x%08x 0x%08x 0x%08x 0x%08x \n",
2104103285Sikob		ch,
2105103285Sikob		cntl,
2106103285Sikob		stat,
2107103285Sikob		cmd,
2108103285Sikob		match);
2109103285Sikob	stat &= 0xffff ;
2110103285Sikob	if(stat & 0xff00){
2111103285Sikob		device_printf(sc->fc.dev, "dma %d ch:%s%s%s%s%s%s %s(%x)\n",
2112103285Sikob			ch,
2113103285Sikob			stat & OHCI_CNTL_DMA_RUN ? "RUN," : "",
2114103285Sikob			stat & OHCI_CNTL_DMA_WAKE ? "WAKE," : "",
2115103285Sikob			stat & OHCI_CNTL_DMA_DEAD ? "DEAD," : "",
2116103285Sikob			stat & OHCI_CNTL_DMA_ACTIVE ? "ACTIVE," : "",
2117103285Sikob			stat & OHCI_CNTL_DMA_BT ? "BRANCH," : "",
2118103285Sikob			stat & OHCI_CNTL_DMA_BAD ? "BADDMA," : "",
2119103285Sikob			fwohcicode[stat & 0x1f],
2120103285Sikob			stat & 0x1f
2121103285Sikob		);
2122103285Sikob	}else{
2123103285Sikob		device_printf(sc->fc.dev, "dma %d ch: Nostat\n", ch);
2124103285Sikob	}
2125103285Sikob}
2126106790Ssimokawa
2127106790Ssimokawavoid
2128106790Ssimokawadump_db(struct fwohci_softc *sc, u_int32_t ch)
2129106790Ssimokawa{
2130103285Sikob	struct fwohci_dbch *dbch;
2131103285Sikob	struct fwohcidb_tr *cp = NULL, *pp, *np;
2132103285Sikob	volatile struct fwohcidb *curr = NULL, *prev, *next = NULL;
2133103285Sikob	int idb, jdb;
2134103285Sikob	u_int32_t cmd, off;
2135103285Sikob	if(ch == 0){
2136103285Sikob		off = OHCI_ATQOFF;
2137103285Sikob		dbch = &sc->atrq;
2138103285Sikob	}else if(ch == 1){
2139103285Sikob		off = OHCI_ATSOFF;
2140103285Sikob		dbch = &sc->atrs;
2141103285Sikob	}else if(ch == 2){
2142103285Sikob		off = OHCI_ARQOFF;
2143103285Sikob		dbch = &sc->arrq;
2144103285Sikob	}else if(ch == 3){
2145103285Sikob		off = OHCI_ARSOFF;
2146103285Sikob		dbch = &sc->arrs;
2147103285Sikob	}else if(ch < IRX_CH){
2148103285Sikob		off = OHCI_ITCTL(ch - ITX_CH);
2149103285Sikob		dbch = &sc->it[ch - ITX_CH];
2150103285Sikob	}else {
2151103285Sikob		off = OHCI_IRCTL(ch - IRX_CH);
2152103285Sikob		dbch = &sc->ir[ch - IRX_CH];
2153103285Sikob	}
2154103285Sikob	cmd = OREAD(sc, off + 0xc);
2155103285Sikob
2156103285Sikob	if( dbch->ndb == 0 ){
2157103285Sikob		device_printf(sc->fc.dev, "No DB is attached ch=%d\n", ch);
2158103285Sikob		return;
2159103285Sikob	}
2160103285Sikob	pp = dbch->top;
2161103285Sikob	prev = pp->db;
2162103285Sikob	for(idb = 0 ; idb < dbch->ndb ; idb ++ ){
2163103285Sikob		if(pp == NULL){
2164103285Sikob			curr = NULL;
2165103285Sikob			goto outdb;
2166103285Sikob		}
2167103285Sikob		cp = STAILQ_NEXT(pp, link);
2168103285Sikob		if(cp == NULL){
2169103285Sikob			curr = NULL;
2170103285Sikob			goto outdb;
2171103285Sikob		}
2172103285Sikob		np = STAILQ_NEXT(cp, link);
2173103285Sikob		if(cp == NULL) break;
2174103285Sikob		for(jdb = 0 ; jdb < dbch->ndesc ; jdb ++ ){
2175103285Sikob			if((cmd  & 0xfffffff0)
2176103285Sikob				== vtophys(&(cp->db[jdb]))){
2177103285Sikob				curr = cp->db;
2178103285Sikob				if(np != NULL){
2179103285Sikob					next = np->db;
2180103285Sikob				}else{
2181103285Sikob					next = NULL;
2182103285Sikob				}
2183103285Sikob				goto outdb;
2184103285Sikob			}
2185103285Sikob		}
2186103285Sikob		pp = STAILQ_NEXT(pp, link);
2187103285Sikob		prev = pp->db;
2188103285Sikob	}
2189103285Sikoboutdb:
2190103285Sikob	if( curr != NULL){
2191103285Sikob		printf("Prev DB %d\n", ch);
2192103285Sikob		print_db(prev, ch, dbch->ndesc);
2193103285Sikob		printf("Current DB %d\n", ch);
2194103285Sikob		print_db(curr, ch, dbch->ndesc);
2195103285Sikob		printf("Next DB %d\n", ch);
2196103285Sikob		print_db(next, ch, dbch->ndesc);
2197103285Sikob	}else{
2198103285Sikob		printf("dbdump err ch = %d cmd = 0x%08x\n", ch, cmd);
2199103285Sikob	}
2200103285Sikob	return;
2201103285Sikob}
2202106790Ssimokawa
2203106790Ssimokawavoid
2204106790Ssimokawaprint_db(volatile struct fwohcidb *db, u_int32_t ch, u_int32_t max)
2205106790Ssimokawa{
2206103285Sikob	fwohcireg_t stat;
2207103285Sikob	int i, key;
2208103285Sikob
2209103285Sikob	if(db == NULL){
2210103285Sikob		printf("No Descriptor is found\n");
2211103285Sikob		return;
2212103285Sikob	}
2213103285Sikob
2214103285Sikob	printf("ch = %d\n%8s %s %s %s %s %4s %8s %8s %4s:%4s\n",
2215103285Sikob		ch,
2216103285Sikob		"Current",
2217103285Sikob		"OP  ",
2218103285Sikob		"KEY",
2219103285Sikob		"INT",
2220103285Sikob		"BR ",
2221103285Sikob		"len",
2222103285Sikob		"Addr",
2223103285Sikob		"Depend",
2224103285Sikob		"Stat",
2225103285Sikob		"Cnt");
2226103285Sikob	for( i = 0 ; i <= max ; i ++){
2227109892Ssimokawa		key = db[i].db.desc.control & OHCI_KEY_MASK;
2228108712Ssimokawa#if __FreeBSD_version >= 500000
2229106543Ssimokawa		printf("%08tx %s %s %s %s %5d %08x %08x %04x:%04x",
2230108712Ssimokawa#else
2231108712Ssimokawa		printf("%08x %s %s %s %s %5d %08x %08x %04x:%04x",
2232108712Ssimokawa#endif
2233103285Sikob				vtophys(&db[i]),
2234109892Ssimokawa				dbcode[(db[i].db.desc.control >> 12) & 0xf],
2235109892Ssimokawa				dbkey[(db[i].db.desc.control >> 8) & 0x7],
2236109892Ssimokawa				dbcond[(db[i].db.desc.control >> 4) & 0x3],
2237109892Ssimokawa				dbcond[(db[i].db.desc.control >> 2) & 0x3],
2238109892Ssimokawa				db[i].db.desc.reqcount,
2239103285Sikob				db[i].db.desc.addr,
2240103285Sikob				db[i].db.desc.depend,
2241103285Sikob				db[i].db.desc.status,
2242103285Sikob				db[i].db.desc.count);
2243103285Sikob		stat = db[i].db.desc.status;
2244103285Sikob		if(stat & 0xff00){
2245103285Sikob			printf(" %s%s%s%s%s%s %s(%x)\n",
2246103285Sikob				stat & OHCI_CNTL_DMA_RUN ? "RUN," : "",
2247103285Sikob				stat & OHCI_CNTL_DMA_WAKE ? "WAKE," : "",
2248103285Sikob				stat & OHCI_CNTL_DMA_DEAD ? "DEAD," : "",
2249103285Sikob				stat & OHCI_CNTL_DMA_ACTIVE ? "ACTIVE," : "",
2250103285Sikob				stat & OHCI_CNTL_DMA_BT ? "BRANCH," : "",
2251103285Sikob				stat & OHCI_CNTL_DMA_BAD ? "BADDMA," : "",
2252103285Sikob				fwohcicode[stat & 0x1f],
2253103285Sikob				stat & 0x1f
2254103285Sikob			);
2255103285Sikob		}else{
2256103285Sikob			printf(" Nostat\n");
2257103285Sikob		}
2258103285Sikob		if(key == OHCI_KEY_ST2 ){
2259103285Sikob			printf("0x%08x 0x%08x 0x%08x 0x%08x\n",
2260103285Sikob				db[i+1].db.immed[0],
2261103285Sikob				db[i+1].db.immed[1],
2262103285Sikob				db[i+1].db.immed[2],
2263103285Sikob				db[i+1].db.immed[3]);
2264103285Sikob		}
2265103285Sikob		if(key == OHCI_KEY_DEVICE){
2266103285Sikob			return;
2267103285Sikob		}
2268109892Ssimokawa		if((db[i].db.desc.control & OHCI_BRANCH_MASK)
2269103285Sikob				== OHCI_BRANCH_ALWAYS){
2270103285Sikob			return;
2271103285Sikob		}
2272109892Ssimokawa		if((db[i].db.desc.control & OHCI_CMD_MASK)
2273103285Sikob				== OHCI_OUTPUT_LAST){
2274103285Sikob			return;
2275103285Sikob		}
2276109892Ssimokawa		if((db[i].db.desc.control & OHCI_CMD_MASK)
2277103285Sikob				== OHCI_INPUT_LAST){
2278103285Sikob			return;
2279103285Sikob		}
2280103285Sikob		if(key == OHCI_KEY_ST2 ){
2281103285Sikob			i++;
2282103285Sikob		}
2283103285Sikob	}
2284103285Sikob	return;
2285103285Sikob}
2286106790Ssimokawa
2287106790Ssimokawavoid
2288106790Ssimokawafwohci_ibr(struct firewire_comm *fc)
2289103285Sikob{
2290103285Sikob	struct fwohci_softc *sc;
2291103285Sikob	u_int32_t fun;
2292103285Sikob
2293103285Sikob	sc = (struct fwohci_softc *)fc;
2294108276Ssimokawa
2295108276Ssimokawa	/*
2296108276Ssimokawa	 * Set root hold-off bit so that non cyclemaster capable node
2297108276Ssimokawa	 * shouldn't became the root node.
2298108276Ssimokawa	 */
2299103285Sikob#if 1
2300103285Sikob	fun = fwphy_rddata(sc, FW_PHY_IBR_REG);
2301109280Ssimokawa	fun |= FW_PHY_IBR | FW_PHY_RHB;
2302103285Sikob	fun = fwphy_wrdata(sc, FW_PHY_IBR_REG, fun);
2303109280Ssimokawa#else	/* Short bus reset */
2304103285Sikob	fun = fwphy_rddata(sc, FW_PHY_ISBR_REG);
2305109280Ssimokawa	fun |= FW_PHY_ISBR | FW_PHY_RHB;
2306103285Sikob	fun = fwphy_wrdata(sc, FW_PHY_ISBR_REG, fun);
2307103285Sikob#endif
2308103285Sikob}
2309106790Ssimokawa
2310106790Ssimokawavoid
2311106790Ssimokawafwohci_txbufdb(struct fwohci_softc *sc, int dmach, struct fw_bulkxfer *bulkxfer)
2312103285Sikob{
2313103285Sikob	struct fwohcidb_tr *db_tr, *fdb_tr;
2314103285Sikob	struct fwohci_dbch *dbch;
2315109892Ssimokawa	volatile struct fwohcidb *db;
2316103285Sikob	struct fw_pkt *fp;
2317103285Sikob	volatile struct fwohci_txpkthdr *ohcifp;
2318103285Sikob	unsigned short chtag;
2319103285Sikob	int idb;
2320103285Sikob
2321103285Sikob	dbch = &sc->it[dmach];
2322103285Sikob	chtag = sc->it[dmach].xferq.flag & 0xff;
2323103285Sikob
2324103285Sikob	db_tr = (struct fwohcidb_tr *)(bulkxfer->start);
2325103285Sikob	fdb_tr = (struct fwohcidb_tr *)(bulkxfer->end);
2326103285Sikob/*
2327103285Sikobdevice_printf(sc->fc.dev, "DB %08x %08x %08x\n", bulkxfer, vtophys(db_tr->db), vtophys(fdb_tr->db));
2328103285Sikob*/
2329103285Sikob	for( idb = 0 ; idb < bulkxfer->npacket ; idb ++){
2330109892Ssimokawa		db = db_tr->db;
2331109892Ssimokawa#if 0
2332109892Ssimokawa		db[0].db.desc.control
2333109892Ssimokawa			= OHCI_OUTPUT_MORE | OHCI_KEY_ST2;
2334109892Ssimokawa		db[0].db.desc.reqcount = 8;
2335109892Ssimokawa#endif
2336103285Sikob		fp = (struct fw_pkt *)db_tr->buf;
2337109892Ssimokawa		ohcifp = (volatile struct fwohci_txpkthdr *) db[1].db.immed;
2338103285Sikob		ohcifp->mode.ld[0] = ntohl(fp->mode.ld[0]);
2339103285Sikob		ohcifp->mode.stream.len = ntohs(fp->mode.stream.len);
2340103285Sikob		ohcifp->mode.stream.chtag = chtag;
2341103285Sikob		ohcifp->mode.stream.tcode = 0xa;
2342109890Ssimokawa		ohcifp->mode.stream.spd = 0;
2343103285Sikob
2344109892Ssimokawa		db[2].db.desc.reqcount = ntohs(fp->mode.stream.len);
2345109892Ssimokawa		db[2].db.desc.status = 0;
2346109892Ssimokawa		db[2].db.desc.count = 0;
2347109892Ssimokawa#if 0 /* if bulkxfer->npackets changes */
2348109892Ssimokawa		db[2].db.desc.control = OHCI_OUTPUT_LAST
2349103285Sikob			| OHCI_UPDATE
2350109892Ssimokawa			| OHCI_BRANCH_ALWAYS;
2351109892Ssimokawa		db[0].db.desc.depend =
2352109892Ssimokawa			= db[dbch->ndesc - 1].db.desc.depend
2353109280Ssimokawa			= vtophys(STAILQ_NEXT(db_tr, link)->db) | dbch->ndesc;
2354109892Ssimokawa#else
2355109892Ssimokawa		db[0].db.desc.depend |= dbch->ndesc;
2356109892Ssimokawa		db[dbch->ndesc - 1].db.desc.depend |= dbch->ndesc;
2357109892Ssimokawa#endif
2358103285Sikob		bulkxfer->end = (caddr_t)db_tr;
2359103285Sikob		db_tr = STAILQ_NEXT(db_tr, link);
2360103285Sikob	}
2361109892Ssimokawa	db = ((struct fwohcidb_tr *)bulkxfer->end)->db;
2362109892Ssimokawa	db[0].db.desc.depend &= ~0xf;
2363109892Ssimokawa	db[dbch->ndesc - 1].db.desc.depend &= ~0xf;
2364109892Ssimokawa#if 0 /* if bulkxfer->npackets changes */
2365109892Ssimokawa	db[dbch->ndesc - 1].db.desc.control |= OHCI_INTERRUPT_ALWAYS;
2366109280Ssimokawa	/* OHCI 1.1 and above */
2367109892Ssimokawa	db[0].db.desc.control |= OHCI_INTERRUPT_ALWAYS;
2368109892Ssimokawa#endif
2369109892Ssimokawa/*
2370103285Sikob	db_tr = (struct fwohcidb_tr *)bulkxfer->start;
2371103285Sikob	fdb_tr = (struct fwohcidb_tr *)bulkxfer->end;
2372103285Sikobdevice_printf(sc->fc.dev, "DB %08x %3d %08x %08x\n", bulkxfer, bulkxfer->npacket, vtophys(db_tr->db), vtophys(fdb_tr->db));
2373103285Sikob*/
2374103285Sikob	return;
2375103285Sikob}
2376106790Ssimokawa
2377106790Ssimokawastatic int
2378106790Ssimokawafwohci_add_tx_buf(struct fwohcidb_tr *db_tr, unsigned short size,
2379106790Ssimokawa	int mode, void *buf)
2380103285Sikob{
2381103285Sikob	volatile struct fwohcidb *db = db_tr->db;
2382103285Sikob	int err = 0;
2383103285Sikob	if(buf == 0){
2384103285Sikob		err = EINVAL;
2385103285Sikob		return err;
2386103285Sikob	}
2387103285Sikob	db_tr->buf = buf;
2388103285Sikob	db_tr->dbcnt = 3;
2389103285Sikob	db_tr->dummy = NULL;
2390103285Sikob
2391109892Ssimokawa	db[0].db.desc.control = OHCI_OUTPUT_MORE | OHCI_KEY_ST2;
2392109892Ssimokawa	db[0].db.desc.reqcount = 8;
2393103285Sikob	db[2].db.desc.addr = vtophys(buf) + sizeof(u_int32_t);
2394109892Ssimokawa	db[2].db.desc.control =
2395109892Ssimokawa		OHCI_OUTPUT_LAST | OHCI_UPDATE | OHCI_BRANCH_ALWAYS;
2396109892Ssimokawa#if 1
2397103285Sikob	db[0].db.desc.status = 0;
2398103285Sikob	db[0].db.desc.count = 0;
2399103285Sikob	db[2].db.desc.status = 0;
2400103285Sikob	db[2].db.desc.count = 0;
2401109892Ssimokawa#endif
2402103285Sikob	if( mode & FWXFERQ_STREAM ){
2403103285Sikob		if(mode & FWXFERQ_PACKET ){
2404109892Ssimokawa			db[2].db.desc.control |= OHCI_INTERRUPT_ALWAYS;
2405103285Sikob		}
2406109892Ssimokawa	} else {
2407109892Ssimokawa		printf("fwohci_add_tx_buf: who calls me?");
2408103285Sikob	}
2409103285Sikob	return 1;
2410103285Sikob}
2411106790Ssimokawa
2412106790Ssimokawaint
2413106790Ssimokawafwohci_add_rx_buf(struct fwohcidb_tr *db_tr, unsigned short size, int mode,
2414106790Ssimokawa	void *buf, void *dummy)
2415103285Sikob{
2416103285Sikob	volatile struct fwohcidb *db = db_tr->db;
2417103285Sikob	int i;
2418103285Sikob	void *dbuf[2];
2419103285Sikob	int dsiz[2];
2420103285Sikob
2421103285Sikob	if(buf == 0){
2422103285Sikob		buf = malloc(size, M_DEVBUF, M_NOWAIT);
2423103285Sikob		if(buf == NULL) return 0;
2424103285Sikob		db_tr->buf = buf;
2425103285Sikob		db_tr->dbcnt = 1;
2426103285Sikob		db_tr->dummy = NULL;
2427103285Sikob		dsiz[0] = size;
2428103285Sikob		dbuf[0] = buf;
2429103285Sikob	}else if(dummy == NULL){
2430103285Sikob		db_tr->buf = buf;
2431103285Sikob		db_tr->dbcnt = 1;
2432103285Sikob		db_tr->dummy = NULL;
2433103285Sikob		dsiz[0] = size;
2434103285Sikob		dbuf[0] = buf;
2435103285Sikob	}else{
2436103285Sikob		db_tr->buf = buf;
2437103285Sikob		db_tr->dbcnt = 2;
2438103285Sikob		db_tr->dummy = dummy;
2439103285Sikob		dsiz[0] = sizeof(u_int32_t);
2440103285Sikob		dsiz[1] = size;
2441103285Sikob		dbuf[0] = dummy;
2442103285Sikob		dbuf[1] = buf;
2443103285Sikob	}
2444103285Sikob	for(i = 0 ; i < db_tr->dbcnt ; i++){
2445103285Sikob		db[i].db.desc.addr = vtophys(dbuf[i]) ;
2446109892Ssimokawa		db[i].db.desc.control = OHCI_INPUT_MORE;
2447109892Ssimokawa		db[i].db.desc.reqcount = dsiz[i];
2448103285Sikob		if( mode & FWXFERQ_STREAM ){
2449109892Ssimokawa			db[i].db.desc.control |= OHCI_UPDATE;
2450103285Sikob		}
2451103285Sikob		db[i].db.desc.status = 0;
2452103285Sikob		db[i].db.desc.count = dsiz[i];
2453103285Sikob	}
2454103285Sikob	if( mode & FWXFERQ_STREAM ){
2455109892Ssimokawa		db[db_tr->dbcnt - 1].db.desc.control |= OHCI_INPUT_LAST;
2456103285Sikob		if(mode & FWXFERQ_PACKET ){
2457109892Ssimokawa			db[db_tr->dbcnt - 1].db.desc.control
2458103285Sikob					|= OHCI_INTERRUPT_ALWAYS;
2459103285Sikob		}
2460103285Sikob	}
2461109892Ssimokawa	db[db_tr->dbcnt - 1].db.desc.control |= OHCI_BRANCH_ALWAYS;
2462103285Sikob	return 1;
2463103285Sikob}
2464106790Ssimokawa
2465106790Ssimokawastatic void
2466106790Ssimokawafwohci_ircv(struct fwohci_softc *sc, struct fwohci_dbch *dbch, int count)
2467103285Sikob{
2468103285Sikob	struct fwohcidb_tr *db_tr = dbch->top, *odb_tr;
2469103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)sc;
2470103285Sikob	int z = 1;
2471103285Sikob	struct fw_pkt *fp;
2472103285Sikob	u_int8_t *ld;
2473103285Sikob	u_int32_t off = NULL;
2474103285Sikob	u_int32_t stat;
2475103285Sikob	u_int32_t *qld;
2476103285Sikob	u_int32_t reg;
2477103285Sikob	u_int spd;
2478103285Sikob	u_int dmach;
2479103285Sikob	int len, i, plen;
2480103285Sikob	caddr_t buf;
2481103285Sikob
2482103285Sikob	for(dmach = 0 ; dmach < sc->fc.nisodma ; dmach++){
2483103285Sikob		if( &sc->ir[dmach] == dbch){
2484103285Sikob			off = OHCI_IROFF(dmach);
2485103285Sikob			break;
2486103285Sikob		}
2487103285Sikob	}
2488103285Sikob	if(off == NULL){
2489103285Sikob		return;
2490103285Sikob	}
2491103285Sikob	if(!(dbch->xferq.flag & FWXFERQ_RUNNING)){
2492103285Sikob		fwohci_irx_disable(&sc->fc, dmach);
2493103285Sikob		return;
2494103285Sikob	}
2495103285Sikob
2496103285Sikob	odb_tr = NULL;
2497103285Sikob	db_tr = dbch->top;
2498103285Sikob	i = 0;
2499103285Sikob	while ((reg = db_tr->db[0].db.desc.status) & 0x1f) {
2500106789Ssimokawa		if (count >= 0 && count-- == 0)
2501106789Ssimokawa			break;
2502103285Sikob		ld = (u_int8_t *)db_tr->buf;
2503103285Sikob		if (dbch->xferq.flag & FWXFERQ_PACKET) {
2504103285Sikob			/* skip timeStamp */
2505103285Sikob			ld += sizeof(struct fwohci_trailer);
2506103285Sikob		}
2507103285Sikob		qld = (u_int32_t *)ld;
2508103285Sikob		len = dbch->xferq.psize - (db_tr->db[0].db.desc.count);
2509103285Sikob/*
2510103285Sikob{
2511103285Sikobdevice_printf(sc->fc.dev, "%04x %2x 0x%08x 0x%08x 0x%08x 0x%08x\n", len,
2512103285Sikob		db_tr->db[0].db.desc.status & 0x1f, qld[0],qld[1],qld[2],qld[3]);
2513103285Sikob}
2514103285Sikob*/
2515103285Sikob		fp=(struct fw_pkt *)ld;
2516103285Sikob		qld[0] = htonl(qld[0]);
2517103285Sikob		plen = sizeof(struct fw_isohdr)
2518103285Sikob			+ ntohs(fp->mode.stream.len) + sizeof(u_int32_t);
2519103285Sikob		ld += plen;
2520103285Sikob		len -= plen;
2521103285Sikob		buf = db_tr->buf;
2522103285Sikob		db_tr->buf = NULL;
2523103285Sikob		stat = reg & 0x1f;
2524103285Sikob		spd =  reg & 0x3;
2525103285Sikob		switch(stat){
2526103285Sikob			case FWOHCIEV_ACKCOMPL:
2527103285Sikob			case FWOHCIEV_ACKPEND:
2528103285Sikob				fw_rcv(&sc->fc, buf, plen - sizeof(u_int32_t), dmach, sizeof(u_int32_t), spd);
2529103285Sikob				break;
2530103285Sikob			default:
2531103285Sikob				free(buf, M_DEVBUF);
2532103285Sikob				device_printf(sc->fc.dev, "Isochronous receive err %02x\n", stat);
2533103285Sikob				break;
2534103285Sikob		}
2535103285Sikob		i++;
2536103285Sikob		fwohci_add_rx_buf(db_tr, dbch->xferq.psize,
2537103285Sikob					dbch->xferq.flag, 0, NULL);
2538103285Sikob		db_tr->db[0].db.desc.depend &= ~0xf;
2539103285Sikob		if(dbch->pdb_tr != NULL){
2540103285Sikob			dbch->pdb_tr->db[0].db.desc.depend |= z;
2541103285Sikob		} else {
2542103285Sikob			/* XXX should be rewritten in better way */
2543103285Sikob			dbch->bottom->db[0].db.desc.depend |= z;
2544103285Sikob		}
2545103285Sikob		dbch->pdb_tr = db_tr;
2546103285Sikob		db_tr = STAILQ_NEXT(db_tr, link);
2547103285Sikob	}
2548103285Sikob	dbch->top = db_tr;
2549103285Sikob	reg = OREAD(sc, OHCI_DMACTL(off));
2550103285Sikob	if (reg & OHCI_CNTL_DMA_ACTIVE)
2551103285Sikob		return;
2552103285Sikob	device_printf(sc->fc.dev, "IR DMA %d stopped at %x status=%x (%d)\n",
2553103285Sikob			dmach, OREAD(sc, OHCI_DMACMD(off)), reg, i);
2554103285Sikob	dbch->top = db_tr;
2555103285Sikob	fwohci_irx_enable(fc, dmach);
2556103285Sikob}
2557103285Sikob
2558103285Sikob#define PLEN(x)	(((ntohs(x))+0x3) & ~0x3)
2559103285Sikobstatic int
2560103285Sikobfwohci_get_plen(struct fwohci_softc *sc, struct fw_pkt *fp, int hlen)
2561103285Sikob{
2562103285Sikob	int i;
2563103285Sikob
2564103285Sikob	for( i = 4; i < hlen ; i+=4){
2565103285Sikob		fp->mode.ld[i/4] = htonl(fp->mode.ld[i/4]);
2566103285Sikob	}
2567103285Sikob
2568103285Sikob	switch(fp->mode.common.tcode){
2569103285Sikob	case FWTCODE_RREQQ:
2570103285Sikob		return sizeof(fp->mode.rreqq) + sizeof(u_int32_t);
2571103285Sikob	case FWTCODE_WRES:
2572103285Sikob		return sizeof(fp->mode.wres) + sizeof(u_int32_t);
2573103285Sikob	case FWTCODE_WREQQ:
2574103285Sikob		return sizeof(fp->mode.wreqq) + sizeof(u_int32_t);
2575103285Sikob	case FWTCODE_RREQB:
2576103285Sikob		return sizeof(fp->mode.rreqb) + sizeof(u_int32_t);
2577103285Sikob	case FWTCODE_RRESQ:
2578103285Sikob		return sizeof(fp->mode.rresq) + sizeof(u_int32_t);
2579103285Sikob	case FWTCODE_WREQB:
2580103285Sikob		return sizeof(struct fw_asyhdr) + PLEN(fp->mode.wreqb.len)
2581103285Sikob						+ sizeof(u_int32_t);
2582103285Sikob	case FWTCODE_LREQ:
2583103285Sikob		return sizeof(struct fw_asyhdr) + PLEN(fp->mode.lreq.len)
2584103285Sikob						+ sizeof(u_int32_t);
2585103285Sikob	case FWTCODE_RRESB:
2586103285Sikob		return sizeof(struct fw_asyhdr) + PLEN(fp->mode.rresb.len)
2587103285Sikob						+ sizeof(u_int32_t);
2588103285Sikob	case FWTCODE_LRES:
2589103285Sikob		return sizeof(struct fw_asyhdr) + PLEN(fp->mode.lres.len)
2590103285Sikob						+ sizeof(u_int32_t);
2591103285Sikob	case FWOHCITCODE_PHY:
2592103285Sikob		return 16;
2593103285Sikob	}
2594103285Sikob	device_printf(sc->fc.dev, "Unknown tcode %d\n", fp->mode.common.tcode);
2595103285Sikob	return 0;
2596103285Sikob}
2597103285Sikob
2598106790Ssimokawastatic void
2599106790Ssimokawafwohci_arcv(struct fwohci_softc *sc, struct fwohci_dbch *dbch, int count)
2600103285Sikob{
2601103285Sikob	struct fwohcidb_tr *db_tr;
2602103285Sikob	int z = 1;
2603103285Sikob	struct fw_pkt *fp;
2604103285Sikob	u_int8_t *ld;
2605103285Sikob	u_int32_t stat, off;
2606103285Sikob	u_int spd;
2607103285Sikob	int len, plen, hlen, pcnt, poff = 0, rlen;
2608103285Sikob	int s;
2609103285Sikob	caddr_t buf;
2610103285Sikob	int resCount;
2611103285Sikob
2612103285Sikob	if(&sc->arrq == dbch){
2613103285Sikob		off = OHCI_ARQOFF;
2614103285Sikob	}else if(&sc->arrs == dbch){
2615103285Sikob		off = OHCI_ARSOFF;
2616103285Sikob	}else{
2617103285Sikob		return;
2618103285Sikob	}
2619103285Sikob
2620103285Sikob	s = splfw();
2621103285Sikob	db_tr = dbch->top;
2622103285Sikob	pcnt = 0;
2623103285Sikob	/* XXX we cannot handle a packet which lies in more than two buf */
2624103285Sikob	while (db_tr->db[0].db.desc.status & OHCI_CNTL_DMA_ACTIVE) {
2625103285Sikob		ld = (u_int8_t *)db_tr->buf + dbch->buf_offset;
2626103285Sikob		resCount = db_tr->db[0].db.desc.count;
2627103285Sikob		len = dbch->xferq.psize - resCount
2628103285Sikob					- dbch->buf_offset;
2629103285Sikob		while (len > 0 ) {
2630106789Ssimokawa			if (count >= 0 && count-- == 0)
2631106789Ssimokawa				goto out;
2632103285Sikob			if(dbch->frag.buf != NULL){
2633103285Sikob				buf = dbch->frag.buf;
2634103285Sikob				if (dbch->frag.plen < 0) {
2635103285Sikob					/* incomplete header */
2636103285Sikob					int hlen;
2637103285Sikob
2638103285Sikob					hlen = - dbch->frag.plen;
2639103285Sikob					rlen = hlen - dbch->frag.len;
2640103285Sikob					bcopy(ld, dbch->frag.buf + dbch->frag.len, rlen);
2641103285Sikob					ld += rlen;
2642103285Sikob					len -= rlen;
2643103285Sikob					dbch->frag.len += rlen;
2644103285Sikob#if 0
2645103285Sikob					printf("(1)frag.plen=%d frag.len=%d rlen=%d len=%d\n", dbch->frag.plen, dbch->frag.len, rlen, len);
2646103285Sikob#endif
2647103285Sikob					fp=(struct fw_pkt *)dbch->frag.buf;
2648103285Sikob					dbch->frag.plen
2649103285Sikob						= fwohci_get_plen(sc, fp, hlen);
2650103285Sikob					if (dbch->frag.plen == 0)
2651103285Sikob						goto out;
2652103285Sikob				}
2653103285Sikob				rlen = dbch->frag.plen - dbch->frag.len;
2654103285Sikob#if 0
2655103285Sikob				printf("(2)frag.plen=%d frag.len=%d rlen=%d len=%d\n", dbch->frag.plen, dbch->frag.len, rlen, len);
2656103285Sikob#endif
2657103285Sikob				bcopy(ld, dbch->frag.buf + dbch->frag.len,
2658103285Sikob						rlen);
2659103285Sikob				ld += rlen;
2660103285Sikob				len -= rlen;
2661103285Sikob				plen = dbch->frag.plen;
2662103285Sikob				dbch->frag.buf = NULL;
2663103285Sikob				dbch->frag.plen = 0;
2664103285Sikob				dbch->frag.len = 0;
2665103285Sikob				poff = 0;
2666103285Sikob			}else{
2667103285Sikob				fp=(struct fw_pkt *)ld;
2668103285Sikob				fp->mode.ld[0] = htonl(fp->mode.ld[0]);
2669103285Sikob				switch(fp->mode.common.tcode){
2670103285Sikob				case FWTCODE_RREQQ:
2671103285Sikob				case FWTCODE_WRES:
2672103285Sikob				case FWTCODE_WREQQ:
2673103285Sikob				case FWTCODE_RRESQ:
2674103285Sikob				case FWOHCITCODE_PHY:
2675103285Sikob					hlen = 12;
2676103285Sikob					break;
2677103285Sikob				case FWTCODE_RREQB:
2678103285Sikob				case FWTCODE_WREQB:
2679103285Sikob				case FWTCODE_LREQ:
2680103285Sikob				case FWTCODE_RRESB:
2681103285Sikob				case FWTCODE_LRES:
2682103285Sikob					hlen = 16;
2683103285Sikob					break;
2684103285Sikob				default:
2685103285Sikob					device_printf(sc->fc.dev, "Unknown tcode %d\n", fp->mode.common.tcode);
2686103285Sikob					goto out;
2687103285Sikob				}
2688103285Sikob				if (len >= hlen) {
2689103285Sikob					plen = fwohci_get_plen(sc, fp, hlen);
2690103285Sikob					if (plen == 0)
2691103285Sikob						goto out;
2692103285Sikob					plen = (plen + 3) & ~3;
2693103285Sikob					len -= plen;
2694103285Sikob				} else {
2695103285Sikob					plen = -hlen;
2696103285Sikob					len -= hlen;
2697103285Sikob				}
2698103285Sikob				if(resCount > 0 || len > 0){
2699103285Sikob					buf = malloc( dbch->xferq.psize,
2700103285Sikob							M_DEVBUF, M_NOWAIT);
2701103285Sikob					if(buf == NULL){
2702103285Sikob						printf("cannot malloc!\n");
2703103285Sikob						free(db_tr->buf, M_DEVBUF);
2704103285Sikob						goto out;
2705103285Sikob					}
2706103285Sikob					bcopy(ld, buf, plen);
2707103285Sikob					poff = 0;
2708103285Sikob					dbch->frag.buf = NULL;
2709103285Sikob					dbch->frag.plen = 0;
2710103285Sikob					dbch->frag.len = 0;
2711103285Sikob				}else if(len < 0){
2712103285Sikob					dbch->frag.buf = db_tr->buf;
2713103285Sikob					if (plen < 0) {
2714103285Sikob#if 0
2715103285Sikob						printf("plen < 0:"
2716103285Sikob						"hlen: %d  len: %d\n",
2717103285Sikob						hlen, len);
2718103285Sikob#endif
2719103285Sikob						dbch->frag.len = hlen + len;
2720103285Sikob						dbch->frag.plen = -hlen;
2721103285Sikob					} else {
2722103285Sikob						dbch->frag.len = plen + len;
2723103285Sikob						dbch->frag.plen = plen;
2724103285Sikob					}
2725103285Sikob					bcopy(ld, db_tr->buf, dbch->frag.len);
2726103285Sikob					buf = NULL;
2727103285Sikob				}else{
2728103285Sikob					buf = db_tr->buf;
2729103285Sikob					poff = ld - (u_int8_t *)buf;
2730103285Sikob					dbch->frag.buf = NULL;
2731103285Sikob					dbch->frag.plen = 0;
2732103285Sikob					dbch->frag.len = 0;
2733103285Sikob				}
2734103285Sikob				ld += plen;
2735103285Sikob			}
2736103285Sikob			if( buf != NULL){
2737103285Sikob/* DMA result-code will be written at the tail of packet */
2738103285Sikob				stat = ((struct fwohci_trailer *)(ld - sizeof(struct fwohci_trailer)))->stat;
2739103285Sikob				spd = (stat >> 5) & 0x3;
2740103285Sikob				stat &= 0x1f;
2741103285Sikob				switch(stat){
2742103285Sikob				case FWOHCIEV_ACKPEND:
2743103285Sikob#if 0
2744103285Sikob					printf("fwohci_arcv: ack pending..\n");
2745103285Sikob#endif
2746103285Sikob					/* fall through */
2747103285Sikob				case FWOHCIEV_ACKCOMPL:
2748103285Sikob					if( poff != 0 )
2749103285Sikob						bcopy(buf+poff, buf, plen - 4);
2750103285Sikob					fw_rcv(&sc->fc, buf, plen - sizeof(struct fwohci_trailer), 0, 0, spd);
2751103285Sikob					break;
2752103285Sikob				case FWOHCIEV_BUSRST:
2753103285Sikob					free(buf, M_DEVBUF);
2754103285Sikob					if (sc->fc.status != FWBUSRESET)
2755103285Sikob						printf("got BUSRST packet!?\n");
2756103285Sikob					break;
2757103285Sikob				default:
2758103285Sikob					device_printf(sc->fc.dev, "Async DMA Receive error err = %02x %s\n", stat, fwohcicode[stat]);
2759103285Sikob#if 0 /* XXX */
2760103285Sikob					goto out;
2761103285Sikob#endif
2762103285Sikob					break;
2763103285Sikob				}
2764103285Sikob			}
2765103285Sikob			pcnt ++;
2766103285Sikob		};
2767103285Sikobout:
2768103285Sikob		if (resCount == 0) {
2769103285Sikob			/* done on this buffer */
2770103285Sikob			fwohci_add_rx_buf(db_tr, dbch->xferq.psize,
2771103285Sikob						dbch->xferq.flag, 0, NULL);
2772103285Sikob			dbch->bottom->db[0].db.desc.depend |= z;
2773103285Sikob			dbch->bottom = db_tr;
2774103285Sikob			db_tr = STAILQ_NEXT(db_tr, link);
2775103285Sikob			dbch->top = db_tr;
2776103285Sikob			dbch->buf_offset = 0;
2777103285Sikob		} else {
2778103285Sikob			dbch->buf_offset = dbch->xferq.psize - resCount;
2779103285Sikob			break;
2780103285Sikob		}
2781103285Sikob		/* XXX make sure DMA is not dead */
2782103285Sikob	}
2783103285Sikob#if 0
2784103285Sikob	if (pcnt < 1)
2785103285Sikob		printf("fwohci_arcv: no packets\n");
2786103285Sikob#endif
2787103285Sikob	splx(s);
2788103285Sikob}
2789