firewire.c revision 103285
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 *
16103285Sikob *    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/firewire.c 103285 2002-09-13 12:31:56Z ikob $
34103285Sikob *
35103285Sikob */
36103285Sikob
37103285Sikob#include <sys/param.h>
38103285Sikob#include <sys/systm.h>
39103285Sikob#include <sys/types.h>
40103285Sikob#include <sys/mbuf.h>
41103285Sikob#include <sys/socket.h>
42103285Sikob#include <sys/socketvar.h>
43103285Sikob
44103285Sikob#include <sys/kernel.h>
45103285Sikob#include <sys/malloc.h>
46103285Sikob#include <sys/conf.h>
47103285Sikob#include <sys/uio.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob#include <sys/poll.h>
50103285Sikob
51103285Sikob#include <machine/cpufunc.h>    /* for rdtsc proto for clock.h below */
52103285Sikob#include <machine/clock.h>
53103285Sikob#include <pci/pcivar.h>
54103285Sikob#include <pci/pcireg.h>
55103285Sikob
56103285Sikob#include <vm/vm.h>
57103285Sikob#include <vm/pmap.h>            /* for vtophys proto */
58103285Sikob#include <vm/vm_extern.h>
59103285Sikob
60103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
61103285Sikob
62103285Sikob#include <machine/bus.h>	/* used by newbus */
63103285Sikob#include <sys/rman.h>		/* used by newbus */
64103285Sikob#include <machine/resource.h>	/* used by newbus */
65103285Sikob
66103285Sikob#include <sys/signal.h>
67103285Sikob#include <sys/mman.h>
68103285Sikob#include <sys/ioccom.h>
69103285Sikob
70103285Sikob#include <dev/firewire/firewire.h>
71103285Sikob#include <dev/firewire/firewirereg.h>
72103285Sikob#include <dev/firewire/fwmem.h>
73103285Sikob#include <dev/firewire/iec13213.h>
74103285Sikob#include <dev/firewire/iec68113.h>
75103285Sikob
76103285Sikobint firewire_debug=0;
77103285SikobSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "Firewire Subsystem");
78103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
79103285Sikob	"Firewire driver debug flag");
80103285Sikob
81103285Sikob#define CDEV_MAJOR 127
82103285Sikob#define FW_MAXASYRTY 4
83103285Sikob#define FW_MAXDEVRCNT 4
84103285Sikob#define	FWNODE_INVAL 0xffff
85103285Sikob
86103285Sikob#define XFER_TIMEOUT 0
87103285Sikob
88103285Sikobstatic	d_open_t	fw_open;
89103285Sikobstatic	d_close_t	fw_close;
90103285Sikobstatic	d_ioctl_t	fw_ioctl;
91103285Sikobstatic	d_poll_t	fw_poll;
92103285Sikobstatic	d_read_t	fw_read;	/* for Isochronous packet */
93103285Sikobstatic	d_write_t	fw_write;
94103285Sikobstatic	d_mmap_t	fw_mmap;
95103285Sikob
96103285Sikobdevclass_t firewire_devclass;
97103285Sikob
98103285Sikob
99103285Sikobstatic int firewire_match      __P((device_t));
100103285Sikobstatic int firewire_attach      __P((device_t));
101103285Sikobstatic int firewire_detach      __P((device_t));
102103285Sikob#if 0
103103285Sikobstatic int firewire_shutdown    __P((device_t));
104103285Sikob#endif
105103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
106103285Sikobstatic struct fw_bind *fw_bindlookup __P((struct firewire_comm *, u_int32_t, u_int32_t));
107103285Sikobstatic void fw_try_bmr __P((void *));
108103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
109103285Sikobstatic u_int16_t fw_noderesolve __P((struct firewire_comm *, struct fw_eui64));
110103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
111103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
112103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
113103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
114103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
115103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
116103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
117103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
118103285Sikob	u_int32_t, u_int32_t, void __P(*)));
119103285Sikob
120103285Sikobstatic device_method_t firewire_methods[] = {
121103285Sikob	/* Device interface */
122103285Sikob	DEVMETHOD(device_probe,		firewire_match),
123103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
124103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
125103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
126103285Sikob
127103285Sikob	/* Bus interface */
128103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
129103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
130103285Sikob
131103285Sikob	{ 0, 0 }
132103285Sikob};
133103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
134103285Sikobu_int maxrec[6]={512,1024,2048,4096,8192,0};
135103285Sikob
136103285Sikob#define MAX_GAPHOP  16
137103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
138103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
139103285Sikob/*
140103285Sikob * The probe routine.
141103285Sikob */
142103285Sikobstruct cdevsw firewire_cdevsw =
143103285Sikob{
144103285Sikob	fw_open, fw_close, fw_read, fw_write, fw_ioctl,
145103285Sikob	fw_poll, fw_mmap, nostrategy, "fw", CDEV_MAJOR, nodump, nopsize, D_MEM
146103285Sikob};
147103285Sikobstatic driver_t firewire_driver = {
148103285Sikob	"firewire",
149103285Sikob	firewire_methods,
150103285Sikob	sizeof(struct firewire_softc),
151103285Sikob};
152103285Sikob
153103285Sikobstatic int
154103285Sikobfw_open (dev_t dev, int flags, int fmt, fw_proc *td)
155103285Sikob{
156103285Sikob	struct firewire_softc *sc;
157103285Sikob	int unit = DEV2UNIT(dev);
158103285Sikob	int sub = DEV2DMACH(dev);
159103285Sikob
160103285Sikob	int err = 0;
161103285Sikob
162103285Sikob	if (DEV_FWMEM(dev))
163103285Sikob		return fwmem_open(dev, flags, fmt, td);
164103285Sikob
165103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
166103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_OPEN){
167103285Sikob		err = EBUSY;
168103285Sikob		return err;
169103285Sikob	}
170103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_OPEN){
171103285Sikob		err = EBUSY;
172103285Sikob		return err;
173103285Sikob	}
174103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_MODEMASK){
175103285Sikob		err = EBUSY;
176103285Sikob		return err;
177103285Sikob	}
178103285Sikob/* Default is per packet mode */
179103285Sikob	sc->fc->ir[sub]->flag |= FWXFERQ_OPEN;
180103285Sikob	sc->fc->it[sub]->flag |= FWXFERQ_OPEN;
181103285Sikob	sc->fc->ir[sub]->flag |= FWXFERQ_PACKET;
182103285Sikob	return err;
183103285Sikob}
184103285Sikobstatic int
185103285Sikobfw_close (dev_t dev, int flags, int fmt, fw_proc *td)
186103285Sikob{
187103285Sikob	struct firewire_softc *sc;
188103285Sikob	int unit = DEV2UNIT(dev);
189103285Sikob	int sub = DEV2DMACH(dev);
190103285Sikob	struct fw_xfer *xfer;
191103285Sikob	struct fw_dvbuf *dvbuf;
192103285Sikob	struct fw_bind *fwb;
193103285Sikob	int err = 0;
194103285Sikob
195103285Sikob	if (DEV_FWMEM(dev))
196103285Sikob		return fwmem_close(dev, flags, fmt, td);
197103285Sikob
198103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
199103285Sikob	if(!(sc->fc->ir[sub]->flag & FWXFERQ_OPEN)){
200103285Sikob		err = EINVAL;
201103285Sikob		return err;
202103285Sikob	}
203103285Sikob	sc->fc->ir[sub]->flag &= ~FWXFERQ_OPEN;
204103285Sikob	if(!(sc->fc->it[sub]->flag & FWXFERQ_OPEN)){
205103285Sikob		err = EINVAL;
206103285Sikob		return err;
207103285Sikob	}
208103285Sikob	sc->fc->it[sub]->flag &= ~FWXFERQ_OPEN;
209103285Sikob
210103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_RUNNING){
211103285Sikob		sc->fc->irx_disable(sc->fc, sub);
212103285Sikob	}
213103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_RUNNING){
214103285Sikob		sc->fc->it[sub]->flag &= ~FWXFERQ_RUNNING;
215103285Sikob		sc->fc->itx_disable(sc->fc, sub);
216103285Sikob	}
217103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_DV){
218103285Sikob		if((dvbuf = sc->fc->it[sub]->dvproc) != NULL){
219103285Sikob			free(dvbuf->buf, M_DEVBUF);
220103285Sikob			sc->fc->it[sub]->dvproc = NULL;
221103285Sikob		}
222103285Sikob		if((dvbuf = sc->fc->it[sub]->dvdma) != NULL){
223103285Sikob			free(dvbuf->buf, M_DEVBUF);
224103285Sikob			sc->fc->it[sub]->dvdma = NULL;
225103285Sikob		}
226103285Sikob		while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvvalid)) != NULL){
227103285Sikob			STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvvalid, link);
228103285Sikob			free(dvbuf->buf, M_DEVBUF);
229103285Sikob		}
230103285Sikob		while((dvbuf = STAILQ_FIRST(&sc->fc->it[sub]->dvfree)) != NULL){
231103285Sikob			STAILQ_REMOVE_HEAD(&sc->fc->it[sub]->dvfree, link);
232103285Sikob			free(dvbuf->buf, M_DEVBUF);
233103285Sikob		}
234103285Sikob		free(sc->fc->it[sub]->dvbuf, M_DEVBUF);
235103285Sikob		sc->fc->it[sub]->dvbuf = NULL;
236103285Sikob	}
237103285Sikob	if(sc->fc->ir[sub]->flag & FWXFERQ_EXTBUF){
238103285Sikob		free(sc->fc->ir[sub]->buf, M_DEVBUF);
239103285Sikob		sc->fc->ir[sub]->buf = NULL;
240103285Sikob		free(sc->fc->ir[sub]->bulkxfer, M_DEVBUF);
241103285Sikob		sc->fc->ir[sub]->bulkxfer = NULL;
242103285Sikob		sc->fc->ir[sub]->flag &= ~FWXFERQ_EXTBUF;
243103285Sikob		sc->fc->ir[sub]->psize = FWPMAX_S400;
244103285Sikob		sc->fc->ir[sub]->maxq = FWMAXQUEUE;
245103285Sikob	}
246103285Sikob	if(sc->fc->it[sub]->flag & FWXFERQ_EXTBUF){
247103285Sikob		free(sc->fc->it[sub]->buf, M_DEVBUF);
248103285Sikob		sc->fc->it[sub]->buf = NULL;
249103285Sikob		free(sc->fc->it[sub]->bulkxfer, M_DEVBUF);
250103285Sikob		sc->fc->it[sub]->bulkxfer = NULL;
251103285Sikob		sc->fc->it[sub]->dvbuf = NULL;
252103285Sikob		sc->fc->it[sub]->flag &= ~FWXFERQ_EXTBUF;
253103285Sikob		sc->fc->it[sub]->psize = FWPMAX_S400;
254103285Sikob		sc->fc->it[sub]->maxq = FWMAXQUEUE;
255103285Sikob	}
256103285Sikob	for(xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q);
257103285Sikob		xfer != NULL; xfer = STAILQ_FIRST(&sc->fc->ir[sub]->q)){
258103285Sikob		sc->fc->ir[sub]->queued--;
259103285Sikob		STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->q, link);
260103285Sikob
261103285Sikob		xfer->resp = 0;
262103285Sikob		switch(xfer->act_type){
263103285Sikob		case FWACT_XFER:
264103285Sikob			fw_xfer_done(xfer);
265103285Sikob			break;
266103285Sikob		default:
267103285Sikob			break;
268103285Sikob		}
269103285Sikob		fw_xfer_free(xfer);
270103285Sikob	}
271103285Sikob	for(fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds); fwb != NULL;
272103285Sikob		fwb = STAILQ_FIRST(&sc->fc->ir[sub]->binds)){
273103285Sikob		STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
274103285Sikob		STAILQ_REMOVE_HEAD(&sc->fc->ir[sub]->binds, chlist);
275103285Sikob		free(fwb, M_DEVBUF);
276103285Sikob	}
277103285Sikob	sc->fc->ir[sub]->flag &= ~FWXFERQ_MODEMASK;
278103285Sikob	sc->fc->it[sub]->flag &= ~FWXFERQ_MODEMASK;
279103285Sikob	return err;
280103285Sikob}
281103285Sikob/*
282103285Sikob * read request.
283103285Sikob */
284103285Sikobstatic int
285103285Sikobfw_read (dev_t dev, struct uio *uio, int ioflag)
286103285Sikob{
287103285Sikob	struct firewire_softc *sc;
288103285Sikob	struct fw_xferq *ir;
289103285Sikob	struct fw_xfer *xfer;
290103285Sikob	int err = 0, s, slept = 0;
291103285Sikob	int unit = DEV2UNIT(dev);
292103285Sikob	int sub = DEV2DMACH(dev);
293103285Sikob	struct fw_pkt *fp;
294103285Sikob
295103285Sikob	if (DEV_FWMEM(dev))
296103285Sikob		return fwmem_read(dev, uio, ioflag);
297103285Sikob
298103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
299103285Sikob
300103285Sikob	ir = sc->fc->ir[sub];
301103285Sikob
302103285Sikob	if(ir->flag & FWXFERQ_PACKET){
303103285Sikob		ir->stproc = NULL;
304103285Sikob	}
305103285Sikobreadloop:
306103285Sikob	xfer = STAILQ_FIRST(&ir->q);
307103285Sikob	if(!(ir->flag & FWXFERQ_PACKET) && ir->stproc == NULL){
308103285Sikob		ir->stproc = STAILQ_FIRST(&ir->stvalid);
309103285Sikob		if(ir->stproc != NULL){
310103285Sikob			s = splfw();
311103285Sikob			STAILQ_REMOVE_HEAD(&ir->stvalid, link);
312103285Sikob			splx(s);
313103285Sikob			ir->queued = 0;
314103285Sikob		}
315103285Sikob	}
316103285Sikob
317103285Sikob	if(xfer == NULL && ir->stproc == NULL){
318103285Sikob		if(slept == 0){
319103285Sikob			slept = 1;
320103285Sikob			if(!(ir->flag & FWXFERQ_RUNNING)
321103285Sikob				&& (ir->flag & FWXFERQ_PACKET)){
322103285Sikob				err = sc->fc->irx_enable(sc->fc, sub);
323103285Sikob			}
324103285Sikob			if(err){
325103285Sikob				return err;
326103285Sikob			}
327103285Sikob			ir->flag |= FWXFERQ_WAKEUP;
328103285Sikob			err = tsleep((caddr_t)ir, FWPRI, "fw_read", hz);
329103285Sikob			if(err){
330103285Sikob				ir->flag &= ~FWXFERQ_WAKEUP;
331103285Sikob				return err;
332103285Sikob			}
333103285Sikob			goto readloop;
334103285Sikob		}else{
335103285Sikob			err = EIO;
336103285Sikob			return err;
337103285Sikob		}
338103285Sikob	}else if(xfer != NULL){
339103285Sikob		s = splfw();
340103285Sikob		ir->queued --;
341103285Sikob		STAILQ_REMOVE_HEAD(&ir->q, link);
342103285Sikob		splx(s);
343103285Sikob		fp = (struct fw_pkt *)(xfer->recv.buf + xfer->recv.off);
344103285Sikob		if(sc->fc->irx_post != NULL)
345103285Sikob			sc->fc->irx_post(sc->fc, fp->mode.ld);
346103285Sikob		err = uiomove(xfer->recv.buf + xfer->recv.off, xfer->recv.len, uio);
347103285Sikob		fw_xfer_free( xfer);
348103285Sikob	}else if(ir->stproc != NULL){
349103285Sikob		fp = (struct fw_pkt *)(ir->stproc->buf + ir->queued * ir->psize);
350103285Sikob		if(sc->fc->irx_post != NULL)
351103285Sikob			sc->fc->irx_post(sc->fc, fp->mode.ld);
352103285Sikob		if(ntohs(fp->mode.stream.len) == 0){
353103285Sikob			err = EIO;
354103285Sikob			return err;
355103285Sikob		}
356103285Sikob		err = uiomove((caddr_t)fp, ntohs(fp->mode.stream.len) + sizeof(u_int32_t), uio);
357103285Sikob		fp->mode.stream.len = 0;
358103285Sikob		ir->queued ++;
359103285Sikob		if(ir->queued >= ir->bnpacket){
360103285Sikob			s = splfw();
361103285Sikob			ir->stproc->flag = 0;
362103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree, ir->stproc, link);
363103285Sikob			splx(s);
364103285Sikob			ir->stproc = NULL;
365103285Sikob		}
366103285Sikob	}
367103285Sikob#if 0
368103285Sikob	if(STAILQ_FIRST(&ir->q) == NULL &&
369103285Sikob		(ir->flag & FWXFERQ_RUNNING) && (ir->flag & FWXFERQ_PACKET)){
370103285Sikob		err = sc->fc->irx_enable(sc->fc, sub);
371103285Sikob	}
372103285Sikob#endif
373103285Sikob#if 0
374103285Sikob	if(STAILQ_FIRST(&ir->stvalid) == NULL &&
375103285Sikob		(ir->flag & FWXFERQ_RUNNING) && !(ir->flag & FWXFERQ_PACKET)){
376103285Sikob		err = sc->fc->irx_enable(sc->fc, sub);
377103285Sikob	}
378103285Sikob#endif
379103285Sikob	return err;
380103285Sikob}
381103285Sikobstatic int
382103285Sikobfw_write (dev_t dev, struct uio *uio, int ioflag)
383103285Sikob{
384103285Sikob	int err = 0;
385103285Sikob	struct firewire_softc *sc;
386103285Sikob	int unit = DEV2UNIT(dev);
387103285Sikob	int sub = DEV2DMACH(dev);
388103285Sikob	int tl, s, slept = 0;
389103285Sikob	struct fw_pkt *fp;
390103285Sikob	struct fw_xfer *xfer;
391103285Sikob	struct fw_xferq *xferq;
392103285Sikob	struct firewire_comm *fc;
393103285Sikob	struct fw_xferq *it;
394103285Sikob
395103285Sikob	if (DEV_FWMEM(dev))
396103285Sikob		return fwmem_write(dev, uio, ioflag);
397103285Sikob
398103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
399103285Sikob	fc = sc->fc;
400103285Sikob	it = sc->fc->it[sub];
401103285Sikob
402103285Sikob	fp = (struct fw_pkt *)uio->uio_iov->iov_base;
403103285Sikob	switch(fp->mode.common.tcode){
404103285Sikob	case FWTCODE_RREQQ:
405103285Sikob	case FWTCODE_RREQB:
406103285Sikob	case FWTCODE_LREQ:
407103285Sikob		err = EINVAL;
408103285Sikob		return err;
409103285Sikob	case FWTCODE_WREQQ:
410103285Sikob	case FWTCODE_WREQB:
411103285Sikob		xferq = fc->atq;
412103285Sikob		break;
413103285Sikob	case FWTCODE_STREAM:
414103285Sikob		if(it->flag & FWXFERQ_PACKET){
415103285Sikob			xferq = fc->atq;
416103285Sikob		}else{
417103285Sikob			xferq = NULL;
418103285Sikob		}
419103285Sikob		break;
420103285Sikob	case FWTCODE_WRES:
421103285Sikob	case FWTCODE_RRESQ:
422103285Sikob	case FWTCODE_RRESB:
423103285Sikob	case FWTCODE_LRES:
424103285Sikob		xferq = fc->ats;
425103285Sikob		break;
426103285Sikob	default:
427103285Sikob		err = EINVAL;
428103285Sikob		return err;
429103285Sikob	}
430103285Sikob	/* Discard unsent buffered stream packet, when sending Asyrequrst */
431103285Sikob	if(xferq != NULL && it->stproc != NULL){
432103285Sikob		s = splfw();
433103285Sikob		it->stproc->flag = 0;
434103285Sikob		STAILQ_INSERT_TAIL(&it->stfree, it->stproc, link);
435103285Sikob		splx(s);
436103285Sikob		it->stproc = NULL;
437103285Sikob	}
438103285Sikob	if(xferq == NULL && !(it->flag & FWXFERQ_DV)){
439103285Sikobisoloop:
440103285Sikob		if(it->stproc == NULL){
441103285Sikob			it->stproc = STAILQ_FIRST(&it->stfree);
442103285Sikob			if(it->stproc != NULL){
443103285Sikob				s = splfw();
444103285Sikob				STAILQ_REMOVE_HEAD(&it->stfree, link);
445103285Sikob				splx(s);
446103285Sikob				it->queued = 0;
447103285Sikob			}else if(slept == 0){
448103285Sikob				slept = 1;
449103285Sikob				err = sc->fc->itx_enable(sc->fc, sub);
450103285Sikob				if(err){
451103285Sikob					return err;
452103285Sikob				}
453103285Sikob				err = tsleep((caddr_t)it, FWPRI, "fw_write", hz);
454103285Sikob				if(err){
455103285Sikob					return err;
456103285Sikob				}
457103285Sikob				goto isoloop;
458103285Sikob			}else{
459103285Sikob				err = EIO;
460103285Sikob				return err;
461103285Sikob			}
462103285Sikob		}
463103285Sikob		fp = (struct fw_pkt *)(it->stproc->buf + it->queued * it->psize);
464103285Sikob		fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t));
465103285Sikob		err = uiomove(it->stproc->buf + it->queued * it->psize,
466103285Sikob							uio->uio_resid, uio);
467103285Sikob		it->queued ++;
468103285Sikob		if(it->queued >= it->btpacket){
469103285Sikob			s = splfw();
470103285Sikob			STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
471103285Sikob			splx(s);
472103285Sikob			it->stproc = NULL;
473103285Sikob			fw_tbuf_update(sc->fc, sub, 0);
474103285Sikob			err = sc->fc->itx_enable(sc->fc, sub);
475103285Sikob		}
476103285Sikob		return err;
477103285Sikob	} if(xferq == NULL && it->flag & FWXFERQ_DV){
478103285Sikobdvloop:
479103285Sikob		if(it->dvproc == NULL){
480103285Sikob			it->dvproc = STAILQ_FIRST(&it->dvfree);
481103285Sikob			if(it->dvproc != NULL){
482103285Sikob				s = splfw();
483103285Sikob				STAILQ_REMOVE_HEAD(&it->dvfree, link);
484103285Sikob				splx(s);
485103285Sikob				it->dvptr = 0;
486103285Sikob			}else if(slept == 0){
487103285Sikob				slept = 1;
488103285Sikob				err = sc->fc->itx_enable(sc->fc, sub);
489103285Sikob				if(err){
490103285Sikob					return err;
491103285Sikob				}
492103285Sikob				err = tsleep((caddr_t)it, FWPRI, "fw_write", hz);
493103285Sikob				if(err){
494103285Sikob					return err;
495103285Sikob				}
496103285Sikob				goto dvloop;
497103285Sikob			}else{
498103285Sikob				err = EIO;
499103285Sikob				return err;
500103285Sikob			}
501103285Sikob		}
502103285Sikob		fp = (struct fw_pkt *)(it->dvproc->buf + it->queued * it->psize);
503103285Sikob		fp->mode.stream.len = htons(uio->uio_resid - sizeof(u_int32_t));
504103285Sikob		err = uiomove(it->dvproc->buf + it->dvptr,
505103285Sikob							uio->uio_resid, uio);
506103285Sikob		it->dvptr += it->psize;
507103285Sikob		if(err){
508103285Sikob			return err;
509103285Sikob		}
510103285Sikob		if(it->dvptr >= it->psize * it->dvpacket){
511103285Sikob			s = splfw();
512103285Sikob			STAILQ_INSERT_TAIL(&it->dvvalid, it->dvproc, link);
513103285Sikob			splx(s);
514103285Sikob			it->dvproc = NULL;
515103285Sikob			err = fw_tbuf_update(sc->fc, sub, 0);
516103285Sikob			if(err){
517103285Sikob				return err;
518103285Sikob			}
519103285Sikob			err = sc->fc->itx_enable(sc->fc, sub);
520103285Sikob		}
521103285Sikob		return err;
522103285Sikob	}
523103285Sikob	if(xferq != NULL){
524103285Sikob		xfer = fw_xfer_alloc();
525103285Sikob		if(xfer == NULL){
526103285Sikob			err = ENOMEM;
527103285Sikob			return err;
528103285Sikob		}
529103285Sikob		xfer->send.buf = malloc(uio->uio_resid, M_DEVBUF, M_NOWAIT);
530103285Sikob		if(xfer->send.buf == NULL){
531103285Sikob			fw_xfer_free( xfer);
532103285Sikob			err = ENOBUFS;
533103285Sikob			return err;
534103285Sikob		}
535103285Sikob		xfer->dst = ntohs(fp->mode.hdr.dst);
536103285Sikob
537103285Sikob		switch(fp->mode.common.tcode){
538103285Sikob		case FWTCODE_WREQQ:
539103285Sikob		case FWTCODE_WREQB:
540103285Sikob			if((tl = fw_get_tlabel(fc, xfer)) == -1 ){
541103285Sikob				fw_xfer_free( xfer);
542103285Sikob				err = EAGAIN;
543103285Sikob				return err;
544103285Sikob			}
545103285Sikob			fp->mode.hdr.tlrt = tl << 2;
546103285Sikob		default:
547103285Sikob			break;
548103285Sikob		}
549103285Sikob
550103285Sikob		xfer->tl = fp->mode.hdr.tlrt >> 2;
551103285Sikob		xfer->send.len = uio->uio_resid;
552103285Sikob		xfer->send.off = 0;
553103285Sikob		xfer->tcode = fp->mode.common.tcode;
554103285Sikob		xfer->spd = 0;/* XXX: how to setup it */
555103285Sikob		xfer->fc = fc;
556103285Sikob		xfer->q = xferq;
557103285Sikob		xfer->act_type = FWACT_XFER;
558103285Sikob		xfer->act.hand = fw_asy_callback;
559103285Sikob		xfer->retry_req = fw_asybusy;
560103285Sikob
561103285Sikob		err = uiomove(xfer->send.buf, uio->uio_resid, uio);
562103285Sikob		if(err){
563103285Sikob			return err;
564103285Sikob		}
565103285Sikob		fw_asystart(xfer);
566103285Sikob		err = tsleep((caddr_t)xfer, FWPRI, "fw_write", hz);
567103285Sikob		if(xfer->resp == EBUSY)
568103285Sikob			return EBUSY;
569103285Sikob		fw_xfer_free( xfer);
570103285Sikob		return err;
571103285Sikob	}
572103285Sikob	return EINVAL;
573103285Sikob}
574103285Sikob/*
575103285Sikob * transmitter buffer update.
576103285Sikob */
577103285Sikobint
578103285Sikobfw_tbuf_update(struct firewire_comm *fc, int sub, int flag){
579103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
580103285Sikob	struct fw_dvbuf *dvbuf = NULL;
581103285Sikob	struct fw_xferq *it;
582103285Sikob	int s, err = 0, i, j, chtag;
583103285Sikob	struct fw_pkt *fp;
584103285Sikob	u_int64_t tmpsync, dvsync;
585103285Sikob
586103285Sikob	it = fc->it[sub];
587103285Sikob
588103285Sikob	s = splfw();
589103285Sikob	if(it->stdma == NULL){
590103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
591103285Sikob	}else if(flag != 0){
592103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
593103285Sikob		if(bulkxfer == it->stdma){
594103285Sikob			STAILQ_REMOVE_HEAD(&it->stvalid, link);
595103285Sikob			it->stdma->flag = 0;
596103285Sikob			STAILQ_INSERT_TAIL(&it->stfree, it->stdma, link);
597103285Sikob			if(!(it->flag & FWXFERQ_DV))
598103285Sikob				wakeup(it);
599103285Sikob		}
600103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
601103285Sikob	}else{
602103285Sikob		bulkxfer = it->stdma;
603103285Sikob	}
604103285Sikob	splx(s);
605103285Sikob	if(bulkxfer != NULL){
606103285Sikob		s = splfw();
607103285Sikob		bulkxfer2 = STAILQ_NEXT(bulkxfer, link);
608103285Sikob#if 0
609103285Sikob		if(it->flag & FWXFERQ_DV && bulkxfer2 == NULL){
610103285Sikob			bulkxfer2 = STAILQ_FIRST(&it->stfree);
611103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
612103285Sikob			splx(s);
613103285Sikob			bcopy(bulkxfer->buf, bulkxfer2->buf,
614103285Sikob					it->psize * it->btpacket);
615103285Sikob			s = splfw();
616103285Sikob			STAILQ_INSERT_TAIL(&it->stvalid, bulkxfer2, link);
617103285Sikob		}
618103285Sikob#endif
619103285Sikob		splx(s);
620103285Sikob	}
621103285Sikob	it->stdma = bulkxfer;
622103285Sikob	it->stdma2 = bulkxfer2;
623103285Sikob
624103285Sikob	if(it->flag & FWXFERQ_DV){
625103285Sikob		chtag = it->flag & 0xff;
626103285Sikobdvloop:
627103285Sikob		if(it->dvdma == NULL){
628103285Sikob			dvbuf = STAILQ_FIRST(&it->dvvalid);
629103285Sikob			if(dvbuf != NULL){
630103285Sikob				s = splfw();
631103285Sikob				STAILQ_REMOVE_HEAD(&it->dvvalid, link);
632103285Sikob				it->dvdma = dvbuf;
633103285Sikob				splx(s);
634103285Sikob				it->queued = 0;
635103285Sikob			}
636103285Sikob		}
637103285Sikob		if(it->dvdma == NULL)
638103285Sikob			return err;
639103285Sikob
640103285Sikob		it->stproc = STAILQ_FIRST(&it->stfree);
641103285Sikob		if(it->stproc != NULL){
642103285Sikob			s = splfw();
643103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
644103285Sikob			splx(s);
645103285Sikob		}else{
646103285Sikob			return err;
647103285Sikob		}
648103285Sikob/*
649103285Sikob * Insert least significant 12 bits timestamp value by computation.
650103285Sikob * Highest significant 4 bits is insert at just before packet sending.
651103285Sikob */
652103285Sikob		fp = (struct fw_pkt *)(it->stproc->buf);
653103285Sikob/* XXX: Parameter relies on NTSC type DV video */
654103285Sikob		tmpsync = 3072 * 8000 * 100 / 2997;
655103285Sikob		tmpsync *= it->dvsync;
656103285Sikob		dvsync = tmpsync;
657103285Sikob		dvsync %= 0xc00;
658103285Sikob		fp->mode.ld[2] = htonl(0x80000000 | (dvsync % 0xc00));
659103285Sikob		it->dvsync ++;
660103285Sikob		it->dvsync %= 2997;
661103285Sikob
662103285Sikob		for( i = 0, j = 0 ; i < it->dvpacket ; i++){
663103285Sikob			bcopy(it->dvdma->buf + it->queued * it->psize,
664103285Sikob				it->stproc->buf + j * it->psize, it->psize);
665103285Sikob			fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
666103285Sikob			fp->mode.stream.len = htons(488);
667103285Sikob			fp->mode.stream.chtag = chtag;
668103285Sikob			fp->mode.stream.tcode = FWTCODE_STREAM;
669103285Sikob			fp->mode.ld[1] = htonl((fc->nodeid << 24) | 0x00780000 | it->dvdbc);
670103285Sikob			it->dvdbc++;
671103285Sikob			it->dvdbc %= 256;
672103285Sikob			it->queued ++;
673103285Sikob			j++;
674103285Sikob/* XXX: Parameter relies on NTSC type DV video */
675103285Sikob#if 1
676103285Sikob#define DVDIFF 203
677103285Sikob#define DVFRAC 2997
678103285Sikob#else
679103285Sikob#define DVDIFF 127
680103285Sikob#define DVFRAC 1875
681103285Sikob#endif
682103285Sikob			it->dvdiff += DVDIFF;
683103285Sikob			if(it->dvdiff >= DVFRAC){
684103285Sikob				it->dvdiff %= DVFRAC;
685103285Sikob				fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
686103285Sikob
687103285Sikob				fp->mode.stream.len = htons(0x8);
688103285Sikob				fp->mode.stream.chtag = chtag;
689103285Sikob				fp->mode.stream.tcode = FWTCODE_STREAM;
690103285Sikob				fp->mode.ld[1] = htonl((fc->nodeid << 24) |
691103285Sikob					 0x00780000 | it->dvdbc);
692103285Sikob				j++;
693103285Sikob			}
694103285Sikob		}
695103285Sikob		it->stproc->npacket = j;
696103285Sikob		s = splfw();
697103285Sikob		STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
698103285Sikob		splx(s);
699103285Sikob		if(it->queued >= it->dvpacket){
700103285Sikob			s = splfw();
701103285Sikob			STAILQ_INSERT_TAIL(&it->dvfree, it->dvdma, link);
702103285Sikob			it->dvdma = NULL;
703103285Sikob			splx(s);
704103285Sikob			wakeup(it);
705103285Sikob			goto dvloop;
706103285Sikob		}
707103285Sikob	}
708103285Sikob	return err;
709103285Sikob}
710103285Sikob/*
711103285Sikob * receving buffer update.
712103285Sikob */
713103285Sikobint
714103285Sikobfw_rbuf_update(struct firewire_comm *fc, int sub, int flag){
715103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
716103285Sikob	struct fw_xferq *ir;
717103285Sikob	int s, err = 0;
718103285Sikob
719103285Sikob	ir = fc->ir[sub];
720103285Sikob	s = splfw();
721103285Sikob	if(ir->stdma != NULL){
722103285Sikob		if(flag != 0){
723103285Sikob			STAILQ_INSERT_TAIL(&ir->stvalid, ir->stdma, link);
724103285Sikob		}else{
725103285Sikob			ir->stdma->flag = 0;
726103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree, ir->stdma, link);
727103285Sikob		}
728103285Sikob	}
729103285Sikob	if(ir->stdma2 != NULL){
730103285Sikob		bulkxfer = ir->stdma2;
731103285Sikob		bulkxfer2 = STAILQ_FIRST(&ir->stfree);
732103285Sikob		if(bulkxfer2 != NULL){
733103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
734103285Sikob		}
735103285Sikob	}else{
736103285Sikob		bulkxfer = STAILQ_FIRST(&ir->stfree);
737103285Sikob		if(bulkxfer != NULL){
738103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
739103285Sikob			bulkxfer2 = STAILQ_FIRST(&ir->stfree);
740103285Sikob			if(bulkxfer2 != NULL){
741103285Sikob				STAILQ_REMOVE_HEAD(&ir->stfree, link);
742103285Sikob			}
743103285Sikob		}else{
744103285Sikob			bulkxfer = STAILQ_FIRST(&ir->stvalid);
745103285Sikob			STAILQ_REMOVE_HEAD(&ir->stvalid, link);
746103285Sikob		}
747103285Sikob	}
748103285Sikob	splx(s);
749103285Sikob	ir->stdma = bulkxfer;
750103285Sikob	ir->stdma2 = bulkxfer2;
751103285Sikob	return err;
752103285Sikob}
753103285Sikob/*
754103285Sikob * ioctl support.
755103285Sikob */
756103285Sikobint
757103285Sikobfw_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, fw_proc *td)
758103285Sikob{
759103285Sikob	struct firewire_softc *sc;
760103285Sikob	int unit = DEV2UNIT(dev);
761103285Sikob	int sub = DEV2DMACH(dev);
762103285Sikob	int i, len, err = 0;
763103285Sikob	struct fw_device *fwdev;
764103285Sikob	struct fw_bind *fwb;
765103285Sikob	struct fw_xferq *ir, *it;
766103285Sikob	struct fw_xfer *xfer;
767103285Sikob	struct fw_pkt *fp;
768103285Sikob
769103285Sikob	struct fw_devlstreq *fwdevlst = (struct fw_devlstreq *)data;
770103285Sikob	struct fw_asyreq *asyreq = (struct fw_asyreq *)data;
771103285Sikob	struct fw_isochreq *ichreq = (struct fw_isochreq *)data;
772103285Sikob	struct fw_isobufreq *ibufreq = (struct fw_isobufreq *)data;
773103285Sikob	struct fw_asybindreq *bindreq = (struct fw_asybindreq *)data;
774103285Sikob#if 0
775103285Sikob	struct fw_map_buf *map_buf = (struct fw_map_buf *)data;
776103285Sikob#endif
777103285Sikob	struct fw_crom_buf *crom_buf = (struct fw_crom_buf *)data;
778103285Sikob
779103285Sikob	if (DEV_FWMEM(dev))
780103285Sikob		return fwmem_ioctl(dev, cmd, data, flag, td);
781103285Sikob
782103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
783103285Sikob	if (!data)
784103285Sikob		return(EINVAL);
785103285Sikob
786103285Sikob	switch (cmd) {
787103285Sikob	case FW_STSTREAM:
788103285Sikob		sc->fc->it[sub]->flag &= ~0xff;
789103285Sikob		sc->fc->it[sub]->flag |= (0x3f & ichreq->ch);
790103285Sikob		sc->fc->it[sub]->flag |= ((0x3 & ichreq->tag) << 6);
791103285Sikob		err = 0;
792103285Sikob		break;
793103285Sikob	case FW_GTSTREAM:
794103285Sikob		ichreq->ch = sc->fc->it[sub]->flag & 0x3f;
795103285Sikob		ichreq->tag =(sc->fc->it[sub]->flag) >> 2 & 0x3;
796103285Sikob		err = 0;
797103285Sikob		break;
798103285Sikob	case FW_SRSTREAM:
799103285Sikob		sc->fc->ir[sub]->flag &= ~0xff;
800103285Sikob		sc->fc->ir[sub]->flag |= (0x3f & ichreq->ch);
801103285Sikob		sc->fc->ir[sub]->flag |= ((0x3 & ichreq->tag) << 6);
802103285Sikob		err = sc->fc->irx_enable(sc->fc, sub);
803103285Sikob		break;
804103285Sikob	case FW_GRSTREAM:
805103285Sikob		ichreq->ch = sc->fc->ir[sub]->flag & 0x3f;
806103285Sikob		ichreq->tag =(sc->fc->ir[sub]->flag) >> 2 & 0x3;
807103285Sikob		err = 0;
808103285Sikob		break;
809103285Sikob	case FW_SSTDV:
810103285Sikob		ibufreq = (struct fw_isobufreq *)
811103285Sikob			malloc(sizeof(struct fw_isobufreq), M_DEVBUF, M_NOWAIT);
812103285Sikob		if(ibufreq == NULL){
813103285Sikob			err = ENOMEM;
814103285Sikob			break;
815103285Sikob		}
816103285Sikob#define FWDVPACKET 250
817103285Sikob#define FWDVPMAX 512
818103285Sikob		ibufreq->rx.nchunk = 8;
819103285Sikob		ibufreq->rx.npacket = 50;
820103285Sikob		ibufreq->rx.psize = FWDVPMAX;
821103285Sikob
822103285Sikob		ibufreq->tx.nchunk = 5;
823103285Sikob		ibufreq->tx.npacket = 300;
824103285Sikob		ibufreq->tx.psize = FWDVPMAX;
825103285Sikob
826103285Sikob		err = fw_ioctl(dev, FW_SSTBUF, (caddr_t)ibufreq, flag, td);
827103285Sikob		sc->fc->it[sub]->dvpacket = FWDVPACKET;
828103285Sikob		free(ibufreq, M_DEVBUF);
829103285Sikob/* reserve a buffer space */
830103285Sikob#define NDVCHUNK 8
831103285Sikob		sc->fc->it[sub]->dvproc = NULL;
832103285Sikob		sc->fc->it[sub]->dvdma = NULL;
833103285Sikob		sc->fc->it[sub]->flag |= FWXFERQ_DV;
834103285Sikob		sc->fc->it[sub]->dvbuf
835103285Sikob			= (struct fw_dvbuf *)malloc(sizeof(struct fw_dvbuf) * NDVCHUNK, M_DEVBUF, M_DONTWAIT);
836103285Sikob		STAILQ_INIT(&sc->fc->it[sub]->dvvalid);
837103285Sikob		STAILQ_INIT(&sc->fc->it[sub]->dvfree);
838103285Sikob		for( i = 0 ; i < NDVCHUNK ; i++){
839103285Sikob			sc->fc->it[sub]->dvbuf[i].buf
840103285Sikob				= malloc(FWDVPMAX * sc->fc->it[sub]->dvpacket, M_DEVBUF, M_DONTWAIT);
841103285Sikob			STAILQ_INSERT_TAIL(&sc->fc->it[sub]->dvfree,
842103285Sikob					&sc->fc->it[sub]->dvbuf[i], link);
843103285Sikob		}
844103285Sikob		break;
845103285Sikob	case FW_SSTBUF:
846103285Sikob		ir = sc->fc->ir[sub];
847103285Sikob		it = sc->fc->it[sub];
848103285Sikob
849103285Sikob		if(ir->flag & FWXFERQ_RUNNING || it->flag & FWXFERQ_RUNNING){
850103285Sikob			return(EBUSY);
851103285Sikob		}
852103285Sikob		if((ir->flag & FWXFERQ_EXTBUF) || (it->flag & FWXFERQ_EXTBUF)){
853103285Sikob			return(EBUSY);
854103285Sikob		}
855103285Sikob		if((ibufreq->rx.nchunk *
856103285Sikob			ibufreq->rx.psize * ibufreq->rx.npacket) +
857103285Sikob		   (ibufreq->tx.nchunk *
858103285Sikob			ibufreq->tx.psize * ibufreq->tx.npacket) <= 0){
859103285Sikob				return(EINVAL);
860103285Sikob		}
861103285Sikob		if(ibufreq->rx.nchunk > FWSTMAXCHUNK ||
862103285Sikob				ibufreq->tx.nchunk > FWSTMAXCHUNK){
863103285Sikob			return(EINVAL);
864103285Sikob		}
865103285Sikob		ir->bulkxfer
866103285Sikob			= (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->rx.nchunk, M_DEVBUF, M_DONTWAIT);
867103285Sikob		if(ir->bulkxfer == NULL){
868103285Sikob			return(ENOMEM);
869103285Sikob		}
870103285Sikob		it->bulkxfer
871103285Sikob			= (struct fw_bulkxfer *)malloc(sizeof(struct fw_bulkxfer) * ibufreq->tx.nchunk, M_DEVBUF, M_DONTWAIT);
872103285Sikob		if(it->bulkxfer == NULL){
873103285Sikob			return(ENOMEM);
874103285Sikob		}
875103285Sikob		ir->buf = malloc(
876103285Sikob			ibufreq->rx.nchunk * ibufreq->rx.npacket
877103285Sikob			* ((ibufreq->rx.psize + 3) &~3),
878103285Sikob			M_DEVBUF, M_DONTWAIT);
879103285Sikob		if(ir->buf == NULL){
880103285Sikob			free(ir->bulkxfer, M_DEVBUF);
881103285Sikob			free(it->bulkxfer, M_DEVBUF);
882103285Sikob			ir->bulkxfer = NULL;
883103285Sikob			it->bulkxfer = NULL;
884103285Sikob			it->buf = NULL;
885103285Sikob			return(ENOMEM);
886103285Sikob		}
887103285Sikob		it->buf = malloc(
888103285Sikob			ibufreq->tx.nchunk * ibufreq->tx.npacket
889103285Sikob			* ((ibufreq->tx.psize + 3) &~3),
890103285Sikob			M_DEVBUF, M_DONTWAIT);
891103285Sikob		if(it->buf == NULL){
892103285Sikob			free(ir->bulkxfer, M_DEVBUF);
893103285Sikob			free(it->bulkxfer, M_DEVBUF);
894103285Sikob			free(ir->buf, M_DEVBUF);
895103285Sikob			ir->bulkxfer = NULL;
896103285Sikob			it->bulkxfer = NULL;
897103285Sikob			it->buf = NULL;
898103285Sikob			return(ENOMEM);
899103285Sikob		}
900103285Sikob
901103285Sikob		ir->bnchunk = ibufreq->rx.nchunk;
902103285Sikob		ir->bnpacket = ibufreq->rx.npacket;
903103285Sikob		ir->btpacket = ibufreq->rx.npacket;
904103285Sikob		ir->psize = (ibufreq->rx.psize + 3) & ~3;
905103285Sikob		ir->queued = 0;
906103285Sikob
907103285Sikob		it->bnchunk = ibufreq->tx.nchunk;
908103285Sikob		it->bnpacket = ibufreq->tx.npacket;
909103285Sikob		it->btpacket = ibufreq->tx.npacket;
910103285Sikob		it->psize = (ibufreq->tx.psize + 3) & ~3;
911103285Sikob		ir->queued = 0;
912103285Sikob		it->dvdbc = 0;
913103285Sikob		it->dvdiff = 0;
914103285Sikob		it->dvsync = 0;
915103285Sikob
916103285Sikob		STAILQ_INIT(&ir->stvalid);
917103285Sikob		STAILQ_INIT(&ir->stfree);
918103285Sikob		ir->stdma = NULL;
919103285Sikob		ir->stdma2 = NULL;
920103285Sikob		ir->stproc = NULL;
921103285Sikob
922103285Sikob		STAILQ_INIT(&it->stvalid);
923103285Sikob		STAILQ_INIT(&it->stfree);
924103285Sikob		it->stdma = NULL;
925103285Sikob		it->stdma2 = NULL;
926103285Sikob		it->stproc = NULL;
927103285Sikob
928103285Sikob		for(i = 0 ; i < sc->fc->ir[sub]->bnchunk; i++){
929103285Sikob			ir->bulkxfer[i].buf =
930103285Sikob				ir->buf +
931103285Sikob				i * sc->fc->ir[sub]->bnpacket *
932103285Sikob			  	sc->fc->ir[sub]->psize;
933103285Sikob			ir->bulkxfer[i].flag = 0;
934103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree,
935103285Sikob					&ir->bulkxfer[i], link);
936103285Sikob			ir->bulkxfer[i].npacket = ir->bnpacket;
937103285Sikob		}
938103285Sikob		for(i = 0 ; i < sc->fc->it[sub]->bnchunk; i++){
939103285Sikob			it->bulkxfer[i].buf =
940103285Sikob				it->buf +
941103285Sikob				i * sc->fc->it[sub]->bnpacket *
942103285Sikob			  	sc->fc->it[sub]->psize;
943103285Sikob			it->bulkxfer[i].flag = 0;
944103285Sikob			STAILQ_INSERT_TAIL(&it->stfree,
945103285Sikob					&it->bulkxfer[i], link);
946103285Sikob			it->bulkxfer[i].npacket = it->bnpacket;
947103285Sikob		}
948103285Sikob		ir->flag &= ~FWXFERQ_MODEMASK;
949103285Sikob		ir->flag |= FWXFERQ_STREAM;
950103285Sikob		ir->flag |= FWXFERQ_EXTBUF;
951103285Sikob
952103285Sikob		it->flag &= ~FWXFERQ_MODEMASK;
953103285Sikob		it->flag |= FWXFERQ_STREAM;
954103285Sikob		it->flag |= FWXFERQ_EXTBUF;
955103285Sikob		err = 0;
956103285Sikob		break;
957103285Sikob	case FW_GSTBUF:
958103285Sikob		ibufreq->rx.nchunk = sc->fc->ir[sub]->bnchunk;
959103285Sikob		ibufreq->rx.npacket = sc->fc->ir[sub]->bnpacket;
960103285Sikob		ibufreq->rx.psize = sc->fc->ir[sub]->psize;
961103285Sikob
962103285Sikob		ibufreq->tx.nchunk = sc->fc->it[sub]->bnchunk;
963103285Sikob		ibufreq->tx.npacket = sc->fc->it[sub]->bnpacket;
964103285Sikob		ibufreq->tx.psize = sc->fc->it[sub]->psize;
965103285Sikob		break;
966103285Sikob	case FW_ASYREQ:
967103285Sikob		xfer = fw_xfer_alloc();
968103285Sikob		if(xfer == NULL){
969103285Sikob			err = ENOMEM;
970103285Sikob			return err;
971103285Sikob		}
972103285Sikob		fp = &asyreq->pkt;
973103285Sikob		switch (asyreq->req.type) {
974103285Sikob		case FWASREQNODE:
975103285Sikob			xfer->dst = ntohs(fp->mode.hdr.dst);
976103285Sikob			break;
977103285Sikob		case FWASREQEUI:
978103285Sikob			xfer->dst = fw_noderesolve(sc->fc, asyreq->req.dst.eui);
979103285Sikob			if(xfer->dst == FWNODE_INVAL ){
980103285Sikob				printf("%s:cannot found node\n",
981103285Sikob					device_get_nameunit(sc->fc->dev));
982103285Sikob				err = EINVAL;
983103285Sikob				goto error;
984103285Sikob			}
985103285Sikob			fp->mode.hdr.dst = htons(FWLOCALBUS | xfer->dst);
986103285Sikob			break;
987103285Sikob		case FWASRESTL:
988103285Sikob			/* XXX what's this? */
989103285Sikob			break;
990103285Sikob		case FWASREQSTREAM:
991103285Sikob			/* nothing to do */
992103285Sikob			break;
993103285Sikob		}
994103285Sikob		xfer->spd = asyreq->req.sped;
995103285Sikob		xfer->send.len = asyreq->req.len;
996103285Sikob		xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT);
997103285Sikob		if(xfer->send.buf == NULL){
998103285Sikob			return ENOMEM;
999103285Sikob		}
1000103285Sikob		xfer->send.off = 0;
1001103285Sikob		bcopy(fp, xfer->send.buf, xfer->send.len);
1002103285Sikob		xfer->act.hand = fw_asy_callback;
1003103285Sikob		err = fw_asyreq(sc->fc, sub, xfer);
1004103285Sikob		if(err){
1005103285Sikob			fw_xfer_free( xfer);
1006103285Sikob			return err;
1007103285Sikob		}
1008103285Sikob		err = tsleep((caddr_t)xfer, FWPRI, "asyreq", hz);
1009103285Sikob		if(err == 0){
1010103285Sikob			if(asyreq->req.len >= xfer->recv.len){
1011103285Sikob				asyreq->req.len = xfer->recv.len;
1012103285Sikob			}else{
1013103285Sikob				err = EINVAL;
1014103285Sikob			}
1015103285Sikob			bcopy(xfer->recv.buf + xfer->recv.off, fp, asyreq->req.len);
1016103285Sikob		}
1017103285Sikoberror:
1018103285Sikob		fw_xfer_free( xfer);
1019103285Sikob		break;
1020103285Sikob	case FW_IBUSRST:
1021103285Sikob		sc->fc->ibr(sc->fc);
1022103285Sikob		break;
1023103285Sikob	case FW_CBINDADDR:
1024103285Sikob		fwb = fw_bindlookup(sc->fc,
1025103285Sikob				bindreq->start.hi, bindreq->start.lo);
1026103285Sikob		if(fwb == NULL){
1027103285Sikob			err = EINVAL;
1028103285Sikob			break;
1029103285Sikob		}
1030103285Sikob		STAILQ_REMOVE(&sc->fc->binds, fwb, fw_bind, fclist);
1031103285Sikob		STAILQ_REMOVE(&sc->fc->ir[sub]->binds, fwb, fw_bind, chlist);
1032103285Sikob		free(fwb, M_DEVBUF);
1033103285Sikob		break;
1034103285Sikob	case FW_SBINDADDR:
1035103285Sikob		if(bindreq->len <= 0 ){
1036103285Sikob			err = EINVAL;
1037103285Sikob			break;
1038103285Sikob		}
1039103285Sikob		if(bindreq->start.hi > 0xffff ){
1040103285Sikob			err = EINVAL;
1041103285Sikob			break;
1042103285Sikob		}
1043103285Sikob		fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
1044103285Sikob		if(fwb == NULL){
1045103285Sikob			err = ENOMEM;
1046103285Sikob			break;
1047103285Sikob		}
1048103285Sikob		fwb->start_hi = bindreq->start.hi;
1049103285Sikob		fwb->start_lo = bindreq->start.lo;
1050103285Sikob		fwb->addrlen = bindreq->len;
1051103285Sikob
1052103285Sikob		xfer = fw_xfer_alloc();
1053103285Sikob		if(xfer == NULL){
1054103285Sikob			err = ENOMEM;
1055103285Sikob			return err;
1056103285Sikob		}
1057103285Sikob		xfer->act_type = FWACT_CH;
1058103285Sikob		xfer->sub = sub;
1059103285Sikob		xfer->fc = sc->fc;
1060103285Sikob
1061103285Sikob		fwb->xfer = xfer;
1062103285Sikob		err = fw_bindadd(sc->fc, fwb);
1063103285Sikob		break;
1064103285Sikob	case FW_GDEVLST:
1065103285Sikob		i = 0;
1066103285Sikob		for(fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
1067103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1068103285Sikob			if(i < fwdevlst->n){
1069103285Sikob				fwdevlst->dst[i] = fwdev->dst;
1070103285Sikob				fwdevlst->status[i] =
1071103285Sikob					(fwdev->status == FWDEVATTACHED)?1:0;
1072103285Sikob				fwdevlst->eui[i].hi = fwdev->eui.hi;
1073103285Sikob				fwdevlst->eui[i].lo = fwdev->eui.lo;
1074103285Sikob			}
1075103285Sikob			i++;
1076103285Sikob		}
1077103285Sikob		fwdevlst->n = i;
1078103285Sikob		break;
1079103285Sikob	case FW_GTPMAP:
1080103285Sikob		bcopy(sc->fc->topology_map, data,
1081103285Sikob				(sc->fc->topology_map->crc_len + 1) * 4);
1082103285Sikob		break;
1083103285Sikob	case FW_GSPMAP:
1084103285Sikob		/* speed_map is larger than a page */
1085103285Sikob		err = copyout(sc->fc->speed_map, *(void **)data,
1086103285Sikob				(sc->fc->speed_map->crc_len + 1) * 4);
1087103285Sikob		break;
1088103285Sikob	case FW_GCROM:
1089103285Sikob		for (fwdev = TAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
1090103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)) {
1091103285Sikob			if (fwdev->eui.hi == crom_buf->eui.hi &&
1092103285Sikob					fwdev->eui.lo == crom_buf->eui.lo)
1093103285Sikob				break;
1094103285Sikob		}
1095103285Sikob		if (fwdev == NULL) {
1096103285Sikob			err = FWNODE_INVAL;
1097103285Sikob			break;
1098103285Sikob		}
1099103285Sikob#if 0
1100103285Sikob		if (fwdev->csrrom[0] >> 24 == 1)
1101103285Sikob			len = 4;
1102103285Sikob		else
1103103285Sikob			len = (1 + ((fwdev->csrrom[0] >> 16) & 0xff)) * 4;
1104103285Sikob#else
1105103285Sikob		if (fwdev->rommax < CSRROMOFF)
1106103285Sikob			len = 0;
1107103285Sikob		else
1108103285Sikob			len = fwdev->rommax - CSRROMOFF + 4;
1109103285Sikob#endif
1110103285Sikob		if (crom_buf->len < len)
1111103285Sikob			len = crom_buf->len;
1112103285Sikob		else
1113103285Sikob			crom_buf->len = len;
1114103285Sikob		err = copyout(&fwdev->csrrom[0], crom_buf->ptr, len);
1115103285Sikob		break;
1116103285Sikob	default:
1117103285Sikob		sc->fc->ioctl (dev, cmd, data, flag, td);
1118103285Sikob		break;
1119103285Sikob	}
1120103285Sikob	return err;
1121103285Sikob}
1122103285Sikobint
1123103285Sikobfw_poll(dev_t dev, int events, fw_proc *td)
1124103285Sikob{
1125103285Sikob	int revents;
1126103285Sikob	int tmp;
1127103285Sikob	int unit = DEV2UNIT(dev);
1128103285Sikob	int sub = DEV2DMACH(dev);
1129103285Sikob	struct firewire_softc *sc;
1130103285Sikob
1131103285Sikob	if (DEV_FWMEM(dev))
1132103285Sikob		return fwmem_poll(dev, events, td);
1133103285Sikob
1134103285Sikob	sc = devclass_get_softc(firewire_devclass, unit);
1135103285Sikob	revents = 0;
1136103285Sikob	tmp = POLLIN | POLLRDNORM;
1137103285Sikob	if (events & tmp) {
1138103285Sikob		if (STAILQ_FIRST(&sc->fc->ir[sub]->q) != NULL)
1139103285Sikob			revents |= tmp;
1140103285Sikob		else
1141103285Sikob			selrecord(td, &sc->fc->ir[sub]->rsel);
1142103285Sikob	}
1143103285Sikob	tmp = POLLOUT | POLLWRNORM;
1144103285Sikob	if (events & tmp) {
1145103285Sikob		/* XXX should be fixed */
1146103285Sikob		revents |= tmp;
1147103285Sikob	}
1148103285Sikob
1149103285Sikob	return revents;
1150103285Sikob}
1151103285Sikob
1152103285Sikob/*
1153103285Sikob * To lookup node id. from EUI64.
1154103285Sikob */
1155103285Sikobu_int16_t fw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui)
1156103285Sikob{
1157103285Sikob	struct fw_device *fwdev;
1158103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1159103285Sikob		fwdev = TAILQ_NEXT(fwdev, link)){
1160103285Sikob		if(fwdev->eui.hi == eui.hi && fwdev->eui.lo == eui.lo){
1161103285Sikob			break;
1162103285Sikob		}
1163103285Sikob	}
1164103285Sikob	if(fwdev == NULL) return FWNODE_INVAL;
1165103285Sikob	if(fwdev->status != FWDEVATTACHED) return FWNODE_INVAL;
1166103285Sikob	return fwdev->dst;
1167103285Sikob}
1168103285Sikob/*
1169103285Sikob * Async. request procedure for userland application.
1170103285Sikob */
1171103285Sikobint
1172103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
1173103285Sikob{
1174103285Sikob	int err = 0;
1175103285Sikob	struct fw_xferq *xferq;
1176103285Sikob	int tl = 0, len;
1177103285Sikob	struct fw_pkt *fp;
1178103285Sikob	int tcode;
1179103285Sikob	struct tcode_info *info;
1180103285Sikob
1181103285Sikob	if(xfer == NULL) return EINVAL;
1182103285Sikob	if(xfer->send.len > fc->maxrec){
1183103285Sikob		printf("send.len > maxrec\n");
1184103285Sikob		return EINVAL;
1185103285Sikob	}
1186103285Sikob	if(xfer->act.hand == NULL){
1187103285Sikob		printf("act.hand == NULL\n");
1188103285Sikob		return EINVAL;
1189103285Sikob	}
1190103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1191103285Sikob
1192103285Sikob#if 0
1193103285Sikob	switch(fp->mode.common.tcode){
1194103285Sikob	case FWTCODE_STREAM:
1195103285Sikob		len = ntohs(fp->mode.stream.len) + 4;
1196103285Sikob		break;
1197103285Sikob	case FWTCODE_RREQQ:
1198103285Sikob	case FWTCODE_WRES:
1199103285Sikob	case FWTCODE_PHY:
1200103285Sikob		len = 12;
1201103285Sikob		break;
1202103285Sikob	case FWTCODE_WREQQ:
1203103285Sikob	case FWTCODE_RRESQ:
1204103285Sikob		len = 16;
1205103285Sikob		break;
1206103285Sikob	default:
1207103285Sikob		len = ntohs(fp->mode.rresb.len) + 16;
1208103285Sikob		break;
1209103285Sikob	}
1210103285Sikob	if( len >  xfer->send.len ){
1211103285Sikob		printf("len > send.len\n");
1212103285Sikob		return EINVAL;
1213103285Sikob	}
1214103285Sikob	switch(fp->mode.common.tcode){
1215103285Sikob	case FWTCODE_WREQQ:
1216103285Sikob	case FWTCODE_WREQB:
1217103285Sikob	case FWTCODE_RREQQ:
1218103285Sikob	case FWTCODE_RREQB:
1219103285Sikob	case FWTCODE_LREQ:
1220103285Sikob	case FWTCODE_PHY:
1221103285Sikob	case FWTCODE_STREAM:
1222103285Sikob		xferq = fc->atq;
1223103285Sikob		break;
1224103285Sikob	case FWTCODE_WRES:
1225103285Sikob	case FWTCODE_RRESQ:
1226103285Sikob	case FWTCODE_RRESB:
1227103285Sikob	case FWTCODE_LRES:
1228103285Sikob		xferq = fc->ats;
1229103285Sikob		break;
1230103285Sikob	default:
1231103285Sikob		return EINVAL;
1232103285Sikob	}
1233103285Sikob#else
1234103285Sikob	tcode = fp->mode.common.tcode & 0xf;
1235103285Sikob	info = &fc->tcode[tcode];
1236103285Sikob	if (info->flag == 0) {
1237103285Sikob		printf("invalid tcode=%d\n", tcode);
1238103285Sikob		return EINVAL;
1239103285Sikob	}
1240103285Sikob	if (info->flag & FWTI_REQ)
1241103285Sikob		xferq = fc->atq;
1242103285Sikob	else
1243103285Sikob		xferq = fc->ats;
1244103285Sikob	len = info->hdr_len;
1245103285Sikob	if (info->flag & FWTI_BLOCK_STR)
1246103285Sikob		len += ntohs(fp->mode.stream.len);
1247103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
1248103285Sikob		len += ntohs(fp->mode.rresb.len);
1249103285Sikob	if( len >  xfer->send.len ){
1250103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
1251103285Sikob				len, xfer->send.len, tcode);
1252103285Sikob		return EINVAL;
1253103285Sikob	}
1254103285Sikob	xfer->send.len = len;
1255103285Sikob#endif
1256103285Sikob	if(xferq->start == NULL){
1257103285Sikob		printf("xferq->start == NULL\n");
1258103285Sikob		return EINVAL;
1259103285Sikob	}
1260103285Sikob	if(!(xferq->queued < xferq->maxq)){
1261103285Sikob		printf("%s:Discard a packet (queued=%d)\n",
1262103285Sikob			device_get_nameunit(fc->dev), xferq->queued);
1263103285Sikob		return EINVAL;
1264103285Sikob	}
1265103285Sikob
1266103285Sikob
1267103285Sikob#if 0
1268103285Sikob	switch(tcode){
1269103285Sikob	case FWTCODE_WREQQ:
1270103285Sikob	case FWTCODE_WREQB:
1271103285Sikob	case FWTCODE_RREQQ:
1272103285Sikob	case FWTCODE_RREQB:
1273103285Sikob	case FWTCODE_LREQ:
1274103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 ){
1275103285Sikob			return EIO;
1276103285Sikob		}
1277103285Sikob		fp->mode.hdr.tlrt = tl << 2;
1278103285Sikob		break;
1279103285Sikob	default:
1280103285Sikob		break;
1281103285Sikob	}
1282103285Sikob#else
1283103285Sikob	if (info->flag & FWTI_TLABEL) {
1284103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
1285103285Sikob			return EIO;
1286103285Sikob		fp->mode.hdr.tlrt = tl << 2;
1287103285Sikob	}
1288103285Sikob#endif
1289103285Sikob
1290103285Sikob	xfer->tl = tl;
1291103285Sikob	xfer->tcode = tcode;
1292103285Sikob	xfer->resp = 0;
1293103285Sikob	xfer->fc = fc;
1294103285Sikob	xfer->q = xferq;
1295103285Sikob	xfer->act_type = FWACT_XFER;
1296103285Sikob	xfer->retry_req = fw_asybusy;
1297103285Sikob
1298103285Sikob	fw_asystart(xfer);
1299103285Sikob	return err;
1300103285Sikob}
1301103285Sikob/*
1302103285Sikob * Wakeup blocked process.
1303103285Sikob */
1304103285Sikobvoid
1305103285Sikobfw_asy_callback(struct fw_xfer *xfer){
1306103285Sikob	wakeup(xfer);
1307103285Sikob	return;
1308103285Sikob}
1309103285Sikob/*
1310103285Sikob * Postpone to later retry.
1311103285Sikob */
1312103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
1313103285Sikob#if 0
1314103285Sikob	printf("fw_asybusy\n");
1315103285Sikob#endif
1316103285Sikob#if XFER_TIMEOUT
1317103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
1318103285Sikob#endif
1319103285Sikob/*
1320103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
1321103285Sikob*/
1322103285Sikob	DELAY(20000);
1323103285Sikob	fw_asystart(xfer);
1324103285Sikob	return;
1325103285Sikob}
1326103285Sikob#if XFER_TIMEOUT
1327103285Sikob/*
1328103285Sikob * Post timeout for async. request.
1329103285Sikob */
1330103285Sikobvoid
1331103285Sikobfw_xfer_timeout(void *arg)
1332103285Sikob{
1333103285Sikob	int s;
1334103285Sikob	struct fw_xfer *xfer;
1335103285Sikob
1336103285Sikob	xfer = (struct fw_xfer *)arg;
1337103285Sikob	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
1338103285Sikob	/* XXX set error code */
1339103285Sikob	s = splfw();
1340103285Sikob	xfer->act.hand(xfer);
1341103285Sikob	splx(s);
1342103285Sikob}
1343103285Sikob#endif
1344103285Sikob/*
1345103285Sikob * Async. request with given xfer structure.
1346103285Sikob */
1347103285Sikobstatic void fw_asystart(struct fw_xfer *xfer){
1348103285Sikob	struct firewire_comm *fc = xfer->fc;
1349103285Sikob	int s;
1350103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
1351103285Sikob		xfer->resp = EBUSY;
1352103285Sikob		xfer->state = FWXF_BUSY;
1353103285Sikob		xfer->act.hand(xfer);
1354103285Sikob		return;
1355103285Sikob	}
1356103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
1357103285Sikob	if (fc->status < FWBUSEXPLORE) {
1358103285Sikob		xfer->resp = EAGAIN;
1359103285Sikob		xfer->state = FWXF_BUSY;
1360103285Sikob		if (xfer->act.hand != NULL)
1361103285Sikob			xfer->act.hand(xfer);
1362103285Sikob		return;
1363103285Sikob	}
1364103285Sikob#endif
1365103285Sikob	s = splfw();
1366103285Sikob	xfer->state = FWXF_INQ;
1367103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
1368103285Sikob	xfer->q->queued ++;
1369103285Sikob	splx(s);
1370103285Sikob	/* XXX just queue for mbuf */
1371103285Sikob	if (xfer->mbuf == NULL)
1372103285Sikob		xfer->q->start(fc);
1373103285Sikob#if XFER_TIMEOUT
1374103285Sikob	if (xfer->act.hand != NULL)
1375103285Sikob		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
1376103285Sikob#endif
1377103285Sikob	return;
1378103285Sikob}
1379103285Sikobstatic int
1380103285Sikobfw_mmap (dev_t dev, vm_offset_t offset, int nproto)
1381103285Sikob{
1382103285Sikob	struct firewire_softc *fc;
1383103285Sikob	int unit = DEV2UNIT(dev);
1384103285Sikob
1385103285Sikob	if (DEV_FWMEM(dev))
1386103285Sikob		return fwmem_mmap(dev, offset, nproto);
1387103285Sikob
1388103285Sikob	fc = devclass_get_softc(firewire_devclass, unit);
1389103285Sikob
1390103285Sikob	return EINVAL;
1391103285Sikob}
1392103285Sikobstatic int
1393103285Sikobfirewire_match( device_t dev )
1394103285Sikob{
1395103285Sikob	device_set_desc(dev, "IEEE1394(Firewire) bus");
1396103285Sikob	return -140;
1397103285Sikob}
1398103285Sikob/*
1399103285Sikob * The attach routine.
1400103285Sikob */
1401103285Sikobstatic int
1402103285Sikobfirewire_attach( device_t dev )
1403103285Sikob{
1404103285Sikob	int i, unitmask, mn;
1405103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
1406103285Sikob	device_t pa = device_get_parent(dev);
1407103285Sikob	struct firewire_comm *fc;
1408103285Sikob	dev_t d;
1409103285Sikob
1410103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
1411103285Sikob	sc->fc = fc;
1412103285Sikob	sc->fc->dev = dev;
1413103285Sikob
1414103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
1415103285Sikob
1416103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
1417103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
1418103285Sikob		mn = unitmask | i;
1419103285Sikob		/* XXX device name should be improved */
1420103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
1421103285Sikob			UID_ROOT, GID_OPERATOR, 0770,
1422103285Sikob			"fw%x", mn);
1423103285Sikob#if __FreeBSD_version >= 500000
1424103285Sikob		if (i == 0)
1425103285Sikob			sc->dev = d;
1426103285Sikob		else
1427103285Sikob			dev_depends(sc->dev, d);
1428103285Sikob#else
1429103285Sikob		sc->dev[i] = d;
1430103285Sikob#endif
1431103285Sikob	}
1432103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
1433103285Sikob			UID_ROOT, GID_OPERATOR, 0770,
1434103285Sikob			"fwmem%d", device_get_unit(dev));
1435103285Sikob#if __FreeBSD_version >= 500000
1436103285Sikob	dev_depends(sc->dev, d);
1437103285Sikob#else
1438103285Sikob	sc->dev[i] = d;
1439103285Sikob#endif
1440103285Sikob	printf("%s: firewire bus attach\n", device_get_nameunit(sc->fc->dev));
1441103285Sikob	sc->fc->timeouthandle = timeout((timeout_t *)sc->fc->timeout, (void *)sc->fc, hz * 10);
1442103285Sikob
1443103285Sikob	/* Locate our children */
1444103285Sikob	bus_generic_probe(dev);
1445103285Sikob
1446103285Sikob	/* launch attachement of the added children */
1447103285Sikob	bus_generic_attach(dev);
1448103285Sikob
1449103285Sikob	/* bus_reset */
1450103285Sikob	fc->ibr(fc);
1451103285Sikob
1452103285Sikob	return 0;
1453103285Sikob}
1454103285Sikob
1455103285Sikob/*
1456103285Sikob * Attach it as child.
1457103285Sikob */
1458103285Sikobstatic device_t
1459103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
1460103285Sikob{
1461103285Sikob        device_t child;
1462103285Sikob	struct firewire_softc *sc;
1463103285Sikob
1464103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
1465103285Sikob	child = device_add_child(dev, name, unit);
1466103285Sikob	if (child) {
1467103285Sikob		device_set_ivars(child, sc->fc);
1468103285Sikob		device_probe_and_attach(child);
1469103285Sikob	}
1470103285Sikob
1471103285Sikob	return child;
1472103285Sikob}
1473103285Sikob/*
1474103285Sikob * Dettach it.
1475103285Sikob */
1476103285Sikobstatic int
1477103285Sikobfirewire_detach( device_t dev )
1478103285Sikob{
1479103285Sikob	struct firewire_softc *sc;
1480103285Sikob
1481103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
1482103285Sikob#if 0
1483103285Sikob	printf("%s:dettach prevented", device_get_nameunit(dev));
1484103285Sikob	return(EINVAL);
1485103285Sikob#endif
1486103285Sikob#if __FreeBSD_version >= 500000
1487103285Sikob	destroy_dev(sc->dev);
1488103285Sikob#else
1489103285Sikob	{
1490103285Sikob		int j;
1491103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
1492103285Sikob			destroy_dev(sc->dev[j]);
1493103285Sikob	}
1494103285Sikob#endif
1495103285Sikob	/* XXX xfree_free and untimeout on all xfers */
1496103285Sikob	untimeout((timeout_t *)sc->fc->timeout, sc->fc, sc->fc->timeouthandle);
1497103285Sikob	free(sc->fc->topology_map, M_DEVBUF);
1498103285Sikob	free(sc->fc->speed_map, M_DEVBUF);
1499103285Sikob	bus_generic_detach(dev);
1500103285Sikob	return(0);
1501103285Sikob}
1502103285Sikob#if 0
1503103285Sikobstatic int
1504103285Sikobfirewire_shutdown( device_t dev )
1505103285Sikob{
1506103285Sikob	return 0;
1507103285Sikob}
1508103285Sikob#endif
1509103285Sikob/*
1510103285Sikob * Call ater bus reset.
1511103285Sikob */
1512103285Sikobvoid fw_busreset(struct firewire_comm *fc)
1513103285Sikob{
1514103285Sikob	int i;
1515103285Sikob	struct fw_xfer *xfer;
1516103285Sikob
1517103285Sikob	switch(fc->status){
1518103285Sikob	case FWBUSMGRELECT:
1519103285Sikob		untimeout((timeout_t *)fw_try_bmr, (void *)fc, fc->bmrhandle);
1520103285Sikob		break;
1521103285Sikob	default:
1522103285Sikob		break;
1523103285Sikob	}
1524103285Sikob	fc->status = FWBUSRESET;
1525103285Sikob/* XXX: discard all queued packet */
1526103285Sikob	while((xfer = STAILQ_FIRST(&fc->atq->q)) != NULL){
1527103285Sikob		STAILQ_REMOVE_HEAD(&fc->atq->q, link);
1528103285Sikob		xfer->resp = EAGAIN;
1529103285Sikob		switch(xfer->act_type){
1530103285Sikob		case FWACT_XFER:
1531103285Sikob			fw_xfer_done(xfer);
1532103285Sikob			break;
1533103285Sikob		default:
1534103285Sikob			break;
1535103285Sikob		}
1536103285Sikob		fw_xfer_free( xfer);
1537103285Sikob	}
1538103285Sikob	while((xfer = STAILQ_FIRST(&fc->ats->q)) != NULL){
1539103285Sikob		STAILQ_REMOVE_HEAD(&fc->ats->q, link);
1540103285Sikob		xfer->resp = EAGAIN;
1541103285Sikob		switch(xfer->act_type){
1542103285Sikob		case FWACT_XFER:
1543103285Sikob			fw_xfer_done(xfer);
1544103285Sikob		default:
1545103285Sikob			break;
1546103285Sikob		}
1547103285Sikob		fw_xfer_free( xfer);
1548103285Sikob	}
1549103285Sikob	for(i = 0; i < fc->nisodma; i++)
1550103285Sikob		while((xfer = STAILQ_FIRST(&fc->it[i]->q)) != NULL){
1551103285Sikob			STAILQ_REMOVE_HEAD(&fc->it[i]->q, link);
1552103285Sikob			xfer->resp = 0;
1553103285Sikob			switch(xfer->act_type){
1554103285Sikob			case FWACT_XFER:
1555103285Sikob				fw_xfer_done(xfer);
1556103285Sikob				break;
1557103285Sikob			default:
1558103285Sikob				break;
1559103285Sikob			}
1560103285Sikob			fw_xfer_free( xfer);
1561103285Sikob		}
1562103285Sikob
1563103285Sikob	CSRARC(fc, STATE_CLEAR)
1564103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
1565103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1566103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
1567103285Sikob
1568103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
1569103285Sikob	fc->irm = -1;
1570103285Sikob
1571103285Sikob	fc->max_node = -1;
1572103285Sikob
1573103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
1574103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
1575103285Sikob	}
1576103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
1577103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1578103285Sikob	CSRARC(fc, RESET_START) = 0;
1579103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
1580103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
1581103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
1582103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
1583103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
1584103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
1585103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
1586103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
1587103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
1588103285Sikob
1589103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
1590103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
1591103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
1592103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
1593103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
1594103285Sikob
1595103285Sikob/* DV depend CSRs see blue book */
1596103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
1597103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
1598103285Sikob
1599103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
1600103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
1601103285Sikob}
1602103285Sikob/* Call once after reboot */
1603103285Sikobvoid fw_init(fc)
1604103285Sikob	struct firewire_comm *fc;
1605103285Sikob{
1606103285Sikob	int i;
1607103285Sikob	struct fw_xfer *xfer;
1608103285Sikob	struct fw_bind *fwb;
1609103285Sikob	struct csrdir *csrd;
1610103285Sikob
1611103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
1612103285Sikob
1613103285Sikob	fc->arq->queued = 0;
1614103285Sikob	fc->ars->queued = 0;
1615103285Sikob	fc->atq->queued = 0;
1616103285Sikob	fc->ats->queued = 0;
1617103285Sikob
1618103285Sikob	fc->arq->psize = FWPMAX_S400;
1619103285Sikob	fc->ars->psize = FWPMAX_S400;
1620103285Sikob	fc->atq->psize = FWPMAX_S400;
1621103285Sikob	fc->ats->psize = FWPMAX_S400;
1622103285Sikob
1623103285Sikob
1624103285Sikob	fc->arq->buf = NULL;
1625103285Sikob	fc->ars->buf = NULL;
1626103285Sikob	fc->atq->buf = NULL;
1627103285Sikob	fc->ats->buf = NULL;
1628103285Sikob
1629103285Sikob	fc->arq->flag = FWXFERQ_PACKET;
1630103285Sikob	fc->ars->flag = FWXFERQ_PACKET;
1631103285Sikob	fc->atq->flag = FWXFERQ_PACKET;
1632103285Sikob	fc->ats->flag = FWXFERQ_PACKET;
1633103285Sikob
1634103285Sikob	STAILQ_INIT(&fc->atq->q);
1635103285Sikob	STAILQ_INIT(&fc->ats->q);
1636103285Sikob
1637103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
1638103285Sikob		fc->it[i]->queued = 0;
1639103285Sikob		fc->ir[i]->queued = 0;
1640103285Sikob
1641103285Sikob		fc->it[i]->start = NULL;
1642103285Sikob		fc->ir[i]->start = NULL;
1643103285Sikob
1644103285Sikob		fc->it[i]->buf = NULL;
1645103285Sikob		fc->ir[i]->buf = NULL;
1646103285Sikob
1647103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
1648103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
1649103285Sikob
1650103285Sikob		STAILQ_INIT(&fc->it[i]->q);
1651103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
1652103285Sikob
1653103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
1654103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
1655103285Sikob	}
1656103285Sikob
1657103285Sikob	fc->arq->maxq = FWMAXQUEUE;
1658103285Sikob	fc->ars->maxq = FWMAXQUEUE;
1659103285Sikob	fc->atq->maxq = FWMAXQUEUE;
1660103285Sikob	fc->ats->maxq = FWMAXQUEUE;
1661103285Sikob
1662103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
1663103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
1664103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
1665103285Sikob	}
1666103285Sikob/* Initialize csr registers */
1667103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
1668103285Sikob				sizeof(struct fw_topology_map),
1669103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
1670103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
1671103285Sikob				sizeof(struct fw_speed_map),
1672103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
1673103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
1674103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
1675103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
1676103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
1677103285Sikob
1678103285Sikob	TAILQ_INIT(&fc->devices);
1679103285Sikob	STAILQ_INIT(&fc->pending);
1680103285Sikob
1681103285Sikob/* Initialize csr ROM work space */
1682103285Sikob	SLIST_INIT(&fc->ongocsr);
1683103285Sikob	SLIST_INIT(&fc->csrfree);
1684103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
1685103285Sikob		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_DEVBUF,M_DONTWAIT);
1686103285Sikob		if(csrd == NULL) break;
1687103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1688103285Sikob	}
1689103285Sikob
1690103285Sikob/* Initialize Async handlers */
1691103285Sikob	STAILQ_INIT(&fc->binds);
1692103285Sikob	for( i = 0 ; i < 0x40 ; i++){
1693103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
1694103285Sikob	}
1695103285Sikob
1696103285Sikob/* DV depend CSRs see blue book */
1697103285Sikob#if 0
1698103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
1699103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
1700103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
1701103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
1702103285Sikob	}
1703103285Sikob
1704103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
1705103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
1706103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
1707103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
1708103285Sikob	}
1709103285Sikob#endif
1710103285Sikob
1711103285Sikob
1712103285Sikob	xfer = fw_xfer_alloc();
1713103285Sikob	if(xfer == NULL) return;
1714103285Sikob
1715103285Sikob	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
1716103285Sikob	if(fwb == NULL){
1717103285Sikob		fw_xfer_free(xfer);
1718103285Sikob	}
1719103285Sikob	xfer->act.hand = fw_vmaccess;
1720103285Sikob	xfer->act_type = FWACT_XFER;
1721103285Sikob	xfer->fc = fc;
1722103285Sikob	xfer->sc = NULL;
1723103285Sikob
1724103285Sikob	fwb->start_hi = 0x2;
1725103285Sikob	fwb->start_lo = 0;
1726103285Sikob	fwb->addrlen = 0xffffffff;
1727103285Sikob	fwb->xfer = xfer;
1728103285Sikob	fw_bindadd(fc, fwb);
1729103285Sikob}
1730103285Sikob/*
1731103285Sikob * To lookup binded process from IEEE1394 address.
1732103285Sikob */
1733103285Sikobstatic struct fw_bind *
1734103285Sikobfw_bindlookup(fc, dest_hi, dest_lo)
1735103285Sikobstruct firewire_comm *fc;
1736103285Sikobu_int32_t dest_lo, dest_hi;
1737103285Sikob{
1738103285Sikob	struct fw_bind *tfw;
1739103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
1740103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
1741103285Sikob		if(tfw->xfer->act_type != FWACT_NULL &&
1742103285Sikob			tfw->start_hi == dest_hi &&
1743103285Sikob			tfw->start_lo <= dest_lo &&
1744103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
1745103285Sikob			return(tfw);
1746103285Sikob		}
1747103285Sikob	}
1748103285Sikob	return(NULL);
1749103285Sikob}
1750103285Sikob/*
1751103285Sikob * To bind IEEE1394 address block to process.
1752103285Sikob */
1753103285Sikobint fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
1754103285Sikob{
1755103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
1756103285Sikob	int err = 0;
1757103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
1758103285Sikob	if(tfw == NULL){
1759103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
1760103285Sikob		goto out;
1761103285Sikob	}
1762103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
1763103285Sikob		(tfw->start_hi == fwb->start_hi &&
1764103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
1765103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
1766103285Sikob		goto out;
1767103285Sikob	}
1768103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
1769103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
1770103285Sikob		   (tfw->start_hi == fwb->start_hi &&
1771103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
1772103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
1773103285Sikob			if(tfw2 == NULL)
1774103285Sikob				break;
1775103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
1776103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
1777103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
1778103285Sikob				break;
1779103285Sikob			}else{
1780103285Sikob				err = EBUSY;
1781103285Sikob				goto out;
1782103285Sikob			}
1783103285Sikob		}
1784103285Sikob	}
1785103285Sikob	if(tfw != NULL){
1786103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
1787103285Sikob	}else{
1788103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
1789103285Sikob	}
1790103285Sikobout:
1791103285Sikob	if(!err && fwb->xfer->act_type == FWACT_CH){
1792103285Sikob		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
1793103285Sikob	}
1794103285Sikob	return err;
1795103285Sikob}
1796103285Sikob
1797103285Sikob/*
1798103285Sikob * To free IEEE1394 address block.
1799103285Sikob */
1800103285Sikobint fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
1801103285Sikob{
1802103285Sikob	int s;
1803103285Sikob
1804103285Sikob	s = splfw();
1805103285Sikob	/* shall we check the existance? */
1806103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
1807103285Sikob	splx(s);
1808103285Sikob	if (fwb->xfer)
1809103285Sikob		fw_xfer_free(fwb->xfer);
1810103285Sikob
1811103285Sikob	return 0;
1812103285Sikob}
1813103285Sikob
1814103285Sikob/*
1815103285Sikob * To free transaction label.
1816103285Sikob */
1817103285Sikobstatic void fw_tl_free ( struct firewire_comm *fc, struct fw_xfer *xfer )
1818103285Sikob{
1819103285Sikob	struct tlabel *tl;
1820103285Sikob	int s = splfw();
1821103285Sikob
1822103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
1823103285Sikob		tl = STAILQ_NEXT(tl, link)){
1824103285Sikob		if(tl->xfer == xfer){
1825103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
1826103285Sikob			free(tl, M_DEVBUF);
1827103285Sikob			splx(s);
1828103285Sikob			return;
1829103285Sikob		}
1830103285Sikob	}
1831103285Sikob	splx(s);
1832103285Sikob	return;
1833103285Sikob}
1834103285Sikob/*
1835103285Sikob * To obtain XFER structure by transaction label.
1836103285Sikob */
1837103285Sikobstatic struct fw_xfer *fw_tl2xfer ( struct firewire_comm *fc, int node, int tlabel )
1838103285Sikob{
1839103285Sikob	struct fw_xfer *xfer;
1840103285Sikob	struct tlabel *tl;
1841103285Sikob	int s = splfw();
1842103285Sikob
1843103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
1844103285Sikob		tl = STAILQ_NEXT(tl, link)){
1845103285Sikob		if(tl->xfer->dst == node){
1846103285Sikob			xfer = tl->xfer;
1847103285Sikob#if 0
1848103285Sikob			STAILQ_REMOVE(&fc->tlabels[tlabel], tl, tlabel, link);
1849103285Sikob			free(tl, M_DEVBUF);
1850103285Sikob#endif
1851103285Sikob			splx(s);
1852103285Sikob			return(xfer);
1853103285Sikob		}
1854103285Sikob	}
1855103285Sikob	splx(s);
1856103285Sikob	return(NULL);
1857103285Sikob}
1858103285Sikob/*
1859103285Sikob * To allocate IEEE1394 XFER structure.
1860103285Sikob */
1861103285Sikobstruct fw_xfer *fw_xfer_alloc()
1862103285Sikob{
1863103285Sikob	struct fw_xfer *xfer;
1864103285Sikob#if 0
1865103285Sikob	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_DONTWAIT);
1866103285Sikob#else
1867103285Sikob	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_DONTWAIT | M_ZERO);
1868103285Sikob#endif
1869103285Sikob	if(xfer == NULL) return xfer;
1870103285Sikob#if 0 /* xfer->tl = 0 was missing.. */
1871103285Sikob	xfer->act_type = FWACT_NULL;
1872103285Sikob	xfer->fc = NULL;
1873103285Sikob	xfer->retry = 0;
1874103285Sikob	xfer->resp = 0;
1875103285Sikob	xfer->state = FWXF_INIT;
1876103285Sikob	xfer->time = time_second;
1877103285Sikob	xfer->sub = -1;
1878103285Sikob	xfer->send.buf = NULL;
1879103285Sikob	xfer->send.off = 0;
1880103285Sikob	xfer->send.len = 0;
1881103285Sikob	xfer->recv.buf = NULL;
1882103285Sikob	xfer->recv.off = 0;
1883103285Sikob	xfer->recv.len = 0;
1884103285Sikob	xfer->retry_req = NULL;
1885103285Sikob	xfer->act.hand = NULL;
1886103285Sikob	xfer->sc = NULL;
1887103285Sikob#else
1888103285Sikob	xfer->time = time_second;
1889103285Sikob	xfer->sub = -1;
1890103285Sikob#endif
1891103285Sikob	return xfer;
1892103285Sikob}
1893103285Sikob/*
1894103285Sikob * IEEE1394 XFER post process.
1895103285Sikob */
1896103285Sikobvoid
1897103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1898103285Sikob{
1899103285Sikob	if (xfer->act.hand == NULL)
1900103285Sikob		return;
1901103285Sikob
1902103285Sikob#if XFER_TIMEOUT
1903103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
1904103285Sikob#endif
1905103285Sikob
1906103285Sikob	if (xfer->fc->status != FWBUSRESET)
1907103285Sikob		xfer->act.hand(xfer);
1908103285Sikob	else {
1909103285Sikob		printf("fw_xfer_done: pending\n");
1910103285Sikob		if (xfer->fc != NULL)
1911103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
1912103285Sikob		else
1913103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
1914103285Sikob	}
1915103285Sikob}
1916103285Sikob
1917103285Sikob/*
1918103285Sikob * To free IEEE1394 XFER structure.
1919103285Sikob */
1920103285Sikobvoid fw_xfer_free( struct fw_xfer* xfer)
1921103285Sikob{
1922103285Sikob	int s;
1923103285Sikob	if(xfer == NULL ) return;
1924103285Sikob	if(xfer->state == FWXF_INQ){
1925103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1926103285Sikob		s = splfw();
1927103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1928103285Sikob		xfer->q->queued --;
1929103285Sikob		splx(s);
1930103285Sikob	}
1931103285Sikob	if(xfer->fc != NULL){
1932103285Sikob		if(xfer->state == FWXF_START){
1933103285Sikob#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
1934103285Sikob			printf("fw_xfer_free FWXF_START\n");
1935103285Sikob#endif
1936103285Sikob			s = splfw();
1937103285Sikob			xfer->q->drain(xfer->fc, xfer);
1938103285Sikob			splx(s);
1939103285Sikob		}
1940103285Sikob	}
1941103285Sikob	if(xfer->send.buf != NULL){
1942103285Sikob		free(xfer->send.buf, M_DEVBUF);
1943103285Sikob	}
1944103285Sikob	if(xfer->recv.buf != NULL){
1945103285Sikob		free(xfer->recv.buf, M_DEVBUF);
1946103285Sikob	}
1947103285Sikob	if(xfer->fc != NULL){
1948103285Sikob		fw_tl_free(xfer->fc, xfer);
1949103285Sikob	}
1950103285Sikob	free(xfer, M_DEVBUF);
1951103285Sikob}
1952103285Sikob
1953103285Sikob/*
1954103285Sikob * Callback for PHY configuration.
1955103285Sikob */
1956103285Sikobstatic void
1957103285Sikobfw_phy_config_callback(struct fw_xfer *xfer)
1958103285Sikob{
1959103285Sikob#if 0
1960103285Sikob	printf("phy_config done state=%d resp=%d\n",
1961103285Sikob				xfer->state, xfer->resp);
1962103285Sikob#endif
1963103285Sikob	fw_xfer_free(xfer);
1964103285Sikob	/* XXX need bus reset ?? */
1965103285Sikob	/* sc->fc->ibr(xfer->fc);  LOOP */
1966103285Sikob}
1967103285Sikob
1968103285Sikob/*
1969103285Sikob * To configure PHY.
1970103285Sikob */
1971103285Sikobstatic void
1972103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1973103285Sikob{
1974103285Sikob	struct fw_xfer *xfer;
1975103285Sikob	struct fw_pkt *fp;
1976103285Sikob
1977103285Sikob	fc->status = FWBUSPHYCONF;
1978103285Sikob
1979103285Sikob	DELAY(100000);
1980103285Sikob	xfer = fw_xfer_alloc();
1981103285Sikob	xfer->send.len = 12;
1982103285Sikob	xfer->send.off = 0;
1983103285Sikob	xfer->fc = fc;
1984103285Sikob	xfer->retry_req = fw_asybusy;
1985103285Sikob	xfer->act.hand = fw_phy_config_callback;
1986103285Sikob
1987103285Sikob	xfer->send.buf = malloc(sizeof(u_int32_t),
1988103285Sikob					M_DEVBUF, M_DONTWAIT | M_ZERO);
1989103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1990103285Sikob	fp->mode.ld[1] = 0;
1991103285Sikob	if (root_node >= 0)
1992103285Sikob		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
1993103285Sikob	if (gap_count >= 0)
1994103285Sikob		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
1995103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1996103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1997103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1998103285Sikob
1999103285Sikob	printf("send phy_config root_node=%d gap_count=%d\n",
2000103285Sikob						root_node, gap_count);
2001103285Sikob	fw_asyreq(fc, -1, xfer);
2002103285Sikob}
2003103285Sikob
2004103285Sikob#if 0
2005103285Sikob/*
2006103285Sikob * Dump self ID.
2007103285Sikob */
2008103285Sikobstatic void
2009103285Sikobfw_print_sid(u_int32_t sid)
2010103285Sikob{
2011103285Sikob#if 0
2012103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
2013103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
2014103285Sikob		FWPHYSIDNODE(sid), FWPHYSIDLINK(sid), FWPHYSIDGAP(sid),
2015103285Sikob		FWPHYSIDSPD(sid), FWPHYSIDDEL(sid), FWPHYSIDCON(sid),
2016103285Sikob		FWPHYSIDPWR(sid), FWPHYSIDP0(sid), FWPHYSIDP1(sid),
2017103285Sikob		FWPHYSIDP2(sid), FWPHYSIDIR(sid), FWPHYSIDMORE(sid));
2018103285Sikob#else
2019103285Sikob	union fw_self_id *s;
2020103285Sikob	s = (union fw_self_id *) &sid;
2021103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
2022103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
2023103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
2024103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
2025103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
2026103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
2027103285Sikob#endif
2028103285Sikob}
2029103285Sikob#endif
2030103285Sikob
2031103285Sikob/*
2032103285Sikob * To receive self ID.
2033103285Sikob */
2034103285Sikobvoid fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
2035103285Sikob{
2036103285Sikob	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
2037103285Sikob	union fw_self_id *self_id;
2038103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
2039103285Sikob
2040103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
2041103285Sikob	fc->status = FWBUSINIT;
2042103285Sikob	fc->max_node = fc->nodeid & 0x3f;
2043103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
2044103285Sikob	fc->status = FWBUSCYMELECT;
2045103285Sikob	fc->topology_map->crc_len = 2;
2046103285Sikob	fc->topology_map->generation ++;
2047103285Sikob	fc->topology_map->self_id_count = 0;
2048103285Sikob	fc->topology_map->node_count = 0;
2049103285Sikob	fc->speed_map->generation ++;
2050103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
2051103285Sikob	self_id = &fc->topology_map->self_id[0];
2052103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
2053103285Sikob		if (sid[1] != ~sid[0]) {
2054103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
2055103285Sikob			sid += 2;
2056103285Sikob			continue;
2057103285Sikob		}
2058103285Sikob		*self_id = *((union fw_self_id *)sid);
2059103285Sikob		fc->topology_map->crc_len++;
2060103285Sikob		if(self_id->p0.sequel == 0){
2061103285Sikob			fc->topology_map->node_count ++;
2062103285Sikob			c_port = 0;
2063103285Sikob#if 0
2064103285Sikob			fw_print_sid(sid[0]);
2065103285Sikob#endif
2066103285Sikob			node = self_id->p0.phy_id;
2067103285Sikob			if(fc->max_node < node){
2068103285Sikob				fc->max_node = self_id->p0.phy_id;
2069103285Sikob			}
2070103285Sikob			/* XXX I'm not sure this is the right speed_map */
2071103285Sikob			fc->speed_map->speed[node][node]
2072103285Sikob					= self_id->p0.phy_speed;
2073103285Sikob			for (j = 0; j < node; j ++) {
2074103285Sikob				fc->speed_map->speed[j][node]
2075103285Sikob					= fc->speed_map->speed[node][j]
2076103285Sikob					= min(fc->speed_map->speed[j][j],
2077103285Sikob							self_id->p0.phy_speed);
2078103285Sikob			}
2079103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
2080103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
2081103285Sikob				fc->irm = self_id->p0.phy_id;
2082103285Sikob			}
2083103285Sikob			if(self_id->p0.port0 >= 0x2){
2084103285Sikob				c_port++;
2085103285Sikob			}
2086103285Sikob			if(self_id->p0.port1 >= 0x2){
2087103285Sikob				c_port++;
2088103285Sikob			}
2089103285Sikob			if(self_id->p0.port2 >= 0x2){
2090103285Sikob				c_port++;
2091103285Sikob			}
2092103285Sikob		}
2093103285Sikob		if(c_port > 2){
2094103285Sikob			i_branch += (c_port - 2);
2095103285Sikob		}
2096103285Sikob		sid += 2;
2097103285Sikob		self_id++;
2098103285Sikob		fc->topology_map->self_id_count ++;
2099103285Sikob	}
2100103285Sikob	printf("%s: %d nodes", device_get_nameunit(fc->dev), fc->max_node + 1);
2101103285Sikob	/* CRC */
2102103285Sikob	fc->topology_map->crc = fw_crc16(
2103103285Sikob			(u_int32_t *)&fc->topology_map->generation,
2104103285Sikob			fc->topology_map->crc_len * 4);
2105103285Sikob	fc->speed_map->crc = fw_crc16(
2106103285Sikob			(u_int32_t *)&fc->speed_map->generation,
2107103285Sikob			fc->speed_map->crc_len * 4);
2108103285Sikob	/* byteswap and copy to CSR */
2109103285Sikob	p = (u_int32_t *)fc->topology_map;
2110103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
2111103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
2112103285Sikob	p = (u_int32_t *)fc->speed_map;
2113103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
2114103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
2115103285Sikob	/* don't byte-swap u_int8_t array */
2116103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
2117103285Sikob
2118103285Sikob	fc->max_hop = fc->max_node - i_branch;
2119103285Sikob#if 1
2120103285Sikob	printf(", maxhop <= %d", fc->max_hop);
2121103285Sikob#endif
2122103285Sikob
2123103285Sikob	if(fc->irm == -1 ){
2124103285Sikob		printf(", Not found IRM capable node");
2125103285Sikob	}else{
2126103285Sikob		printf(", cable IRM = %d", fc->irm);
2127103285Sikob		if (fc->irm == fc->nodeid)
2128103285Sikob			printf(" (me)\n");
2129103285Sikob		else
2130103285Sikob			printf("\n");
2131103285Sikob	}
2132103285Sikob
2133103285Sikob	if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){
2134103285Sikob		if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
2135103285Sikob			fc->status = FWBUSMGRDONE;
2136103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
2137103285Sikob		}else{
2138103285Sikob			fc->status = FWBUSMGRELECT;
2139103285Sikob			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8);
2140103285Sikob		}
2141103285Sikob	}else{
2142103285Sikob		fc->status = FWBUSMGRDONE;
2143103285Sikob		printf("%s: BMR = %x\n", device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID));
2144103285Sikob	}
2145103285Sikob	free(buf, M_DEVBUF);
2146103285Sikob#if 1
2147103285Sikob	/* XXX optimize gap_count, if I am BMGR */
2148103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
2149103285Sikob		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
2150103285Sikob	}
2151103285Sikob#endif
2152103285Sikob#if 1
2153103285Sikob	timeout((timeout_t *)fw_bus_probe, (void *)fc, hz/4);
2154103285Sikob#else
2155103285Sikob	fw_bus_probe(fc);
2156103285Sikob#endif
2157103285Sikob}
2158103285Sikob/*
2159103285Sikob * To probe devices on the IEEE1394 bus.
2160103285Sikob */
2161103285Sikobstatic void fw_bus_probe(struct firewire_comm *fc)
2162103285Sikob{
2163103285Sikob	int s;
2164103285Sikob	struct fw_device *fwdev, *next;
2165103285Sikob
2166103285Sikob	s = splfw();
2167103285Sikob	fc->status = FWBUSEXPLORE;
2168103285Sikob	fc->retry_count = 0;
2169103285Sikob
2170103285Sikob/*
2171103285Sikob * Invalidate all devices, just after bus reset. Devices
2172103285Sikob * to be removed has not been seen longer time.
2173103285Sikob */
2174103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
2175103285Sikob		next = TAILQ_NEXT(fwdev, link);
2176103285Sikob		if(fwdev->status != FWDEVINVAL){
2177103285Sikob			fwdev->status = FWDEVINVAL;
2178103285Sikob			fwdev->rcnt = 0;
2179103285Sikob		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
2180103285Sikob			fwdev->rcnt ++;
2181103285Sikob		}else{
2182103285Sikob			TAILQ_REMOVE(&fc->devices, fwdev, link);
2183103285Sikob			free(fwdev, M_DEVBUF);
2184103285Sikob		}
2185103285Sikob	}
2186103285Sikob	fc->ongonode = 0;
2187103285Sikob	fc->ongoaddr = CSRROMOFF;
2188103285Sikob	fc->ongodev = NULL;
2189103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
2190103285Sikob	fw_bus_explore(fc);
2191103285Sikob	splx(s);
2192103285Sikob}
2193103285Sikob/*
2194103285Sikob * To collect device informations on the IEEE1394 bus.
2195103285Sikob */
2196103285Sikobstatic void fw_bus_explore(struct firewire_comm *fc )
2197103285Sikob{
2198103285Sikob	int err = 0;
2199103285Sikob	struct fw_device *fwdev, *tfwdev;
2200103285Sikob	u_int32_t addr;
2201103285Sikob	struct fw_xfer *xfer;
2202103285Sikob	struct fw_pkt *fp;
2203103285Sikob
2204103285Sikob	if(fc->status != FWBUSEXPLORE)
2205103285Sikob		return;
2206103285Sikob
2207103285Sikobloop:
2208103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
2209103285Sikob
2210103285Sikob	if(fc->ongonode > fc->max_node) goto done;
2211103285Sikob	if(fc->ongonode >= 0x3f) goto done;
2212103285Sikob
2213103285Sikob	/* check link */
2214103285Sikob	/* XXX we need to check phy_id first */
2215103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
2216103285Sikob		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
2217103285Sikob		fc->ongonode++;
2218103285Sikob		goto loop;
2219103285Sikob	}
2220103285Sikob
2221103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
2222103285Sikob		fc->ongoeui.hi == 0xffffffff &&
2223103285Sikob		fc->ongoeui.lo == 0xffffffff ){
2224103285Sikob		fc->ongoaddr = CSRROMOFF;
2225103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2226103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
2227103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
2228103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2229103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
2230103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
2231103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2232103285Sikob	}else if(fc->ongodev == NULL){
2233103285Sikob		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
2234103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
2235103285Sikob			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
2236103285Sikob				break;
2237103285Sikob			}
2238103285Sikob		}
2239103285Sikob		if(fwdev != NULL){
2240103285Sikob			fwdev->dst = fc->ongonode;
2241103285Sikob			fwdev->status = FWDEVATTACHED;
2242103285Sikob			fc->ongonode++;
2243103285Sikob			fc->ongoaddr = CSRROMOFF;
2244103285Sikob			fc->ongodev = NULL;
2245103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
2246103285Sikob			goto loop;
2247103285Sikob		}
2248103285Sikob		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT);
2249103285Sikob		if(fwdev == NULL)
2250103285Sikob			return;
2251103285Sikob		fwdev->rommax = 0;
2252103285Sikob		fwdev->dst = fc->ongonode;
2253103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
2254103285Sikob		fwdev->status = FWDEVINIT;
2255103285Sikob#if 0
2256103285Sikob		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
2257103285Sikob			>> ((3 - (fc->ongonode % 4)) * 8);
2258103285Sikob#else
2259103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
2260103285Sikob#endif
2261103285Sikob
2262103285Sikob#if 0
2263103285Sikob		TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
2264103285Sikob#else
2265103285Sikob		tfwdev = TAILQ_FIRST(&fc->devices);
2266103285Sikob		while( tfwdev != NULL &&
2267103285Sikob			(tfwdev->eui.hi > fwdev->eui.hi) &&
2268103285Sikob			((tfwdev->eui.hi == fwdev->eui.hi) &&
2269103285Sikob				tfwdev->eui.lo > fwdev->eui.lo)){
2270103285Sikob			tfwdev = TAILQ_NEXT( tfwdev, link);
2271103285Sikob		}
2272103285Sikob		if(tfwdev == NULL){
2273103285Sikob			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
2274103285Sikob		}else{
2275103285Sikob			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
2276103285Sikob		}
2277103285Sikob#endif
2278103285Sikob
2279103285Sikob		printf("%s:Discover new %s device ID:%08x%08x\n", device_get_nameunit(fc->dev), linkspeed[fwdev->speed], fc->ongoeui.hi, fc->ongoeui.lo);
2280103285Sikob
2281103285Sikob		fc->ongodev = fwdev;
2282103285Sikob		fc->ongoaddr = CSRROMOFF;
2283103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2284103285Sikob	}else{
2285103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
2286103285Sikob	}
2287103285Sikob#if 0
2288103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
2289103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
2290103285Sikob		fw_bus_explore_callback);
2291103285Sikob	if(xfer == NULL) goto done;
2292103285Sikob#else
2293103285Sikob	xfer = fw_xfer_alloc();
2294103285Sikob	if(xfer == NULL){
2295103285Sikob		goto done;
2296103285Sikob	}
2297103285Sikob	xfer->send.len = 16;
2298103285Sikob	xfer->spd = 0;
2299103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2300103285Sikob	if(xfer->send.buf == NULL){
2301103285Sikob		fw_xfer_free( xfer);
2302103285Sikob		return;
2303103285Sikob	}
2304103285Sikob
2305103285Sikob	xfer->send.off = 0;
2306103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2307103285Sikob	fp->mode.rreqq.dest_hi = htons(0xffff);
2308103285Sikob	fp->mode.rreqq.tlrt = 0;
2309103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
2310103285Sikob	fp->mode.rreqq.pri = 0;
2311103285Sikob	fp->mode.rreqq.src = 0;
2312103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
2313103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
2314103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr);
2315103285Sikob	xfer->act.hand = fw_bus_explore_callback;
2316103285Sikob
2317103285Sikob	err = fw_asyreq(fc, -1, xfer);
2318103285Sikob	if(err){
2319103285Sikob		fw_xfer_free( xfer);
2320103285Sikob		return;
2321103285Sikob	}
2322103285Sikob#endif
2323103285Sikob	return;
2324103285Sikobdone:
2325103285Sikob	/* fw_attach_devs */
2326103285Sikob	fc->status = FWBUSEXPDONE;
2327103285Sikob	printf("bus_explore done\n");
2328103285Sikob	fw_attach_dev(fc);
2329103285Sikob	return;
2330103285Sikob
2331103285Sikob}
2332103285Sikob/* Portable Async. request read quad */
2333103285Sikobstruct fw_xfer *asyreqq(struct firewire_comm *fc,
2334103285Sikob	u_int8_t spd, u_int8_t tl, u_int8_t rt,
2335103285Sikob	u_int32_t addr_hi, u_int32_t addr_lo, void *hand __P())
2336103285Sikob{
2337103285Sikob	struct fw_xfer *xfer;
2338103285Sikob	struct fw_pkt *fp;
2339103285Sikob	int err;
2340103285Sikob
2341103285Sikob	xfer = fw_xfer_alloc();
2342103285Sikob	if(xfer == NULL){
2343103285Sikob		return NULL;
2344103285Sikob	}
2345103285Sikob	xfer->send.len = 16;
2346103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
2347103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2348103285Sikob	if(xfer->send.buf == NULL){
2349103285Sikob		fw_xfer_free( xfer);
2350103285Sikob		return NULL;
2351103285Sikob	}
2352103285Sikob
2353103285Sikob	xfer->send.off = 0;
2354103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2355103285Sikob	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
2356103285Sikob	if(tl & FWP_TL_VALID){
2357103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
2358103285Sikob	}else{
2359103285Sikob		fp->mode.rreqq.tlrt = 0;
2360103285Sikob	}
2361103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
2362103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
2363103285Sikob	fp->mode.rreqq.pri = 0;
2364103285Sikob	fp->mode.rreqq.src = 0;
2365103285Sikob	xfer->dst = addr_hi >> 16;
2366103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
2367103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
2368103285Sikob	xfer->act.hand = hand;
2369103285Sikob
2370103285Sikob	err = fw_asyreq(fc, -1, xfer);
2371103285Sikob	if(err){
2372103285Sikob		fw_xfer_free( xfer);
2373103285Sikob		return NULL;
2374103285Sikob	}
2375103285Sikob	return xfer;
2376103285Sikob}
2377103285Sikob/*
2378103285Sikob * Callback for the IEEE1394 bus information collection.
2379103285Sikob */
2380103285Sikobstatic void fw_bus_explore_callback(struct fw_xfer *xfer){
2381103285Sikob	struct firewire_comm *fc;
2382103285Sikob	struct fw_pkt *sfp,*rfp;
2383103285Sikob	struct csrhdr *chdr;
2384103285Sikob	struct csrdir *csrd;
2385103285Sikob	struct csrreg *csrreg;
2386103285Sikob	u_int32_t offset;
2387103285Sikob
2388103285Sikob
2389103285Sikob	if(xfer == NULL) return;
2390103285Sikob	fc = xfer->fc;
2391103285Sikob	if(xfer->resp != 0){
2392103285Sikob		printf("resp != 0: node=%d addr=0x%x\n",
2393103285Sikob			fc->ongonode, fc->ongoaddr);
2394103285Sikob		fc->retry_count++;
2395103285Sikob		goto nextnode;
2396103285Sikob	}
2397103285Sikob
2398103285Sikob	if(xfer->send.buf == NULL){
2399103285Sikob		printf("send.buf == NULL: node=%d addr=0x%x\n",
2400103285Sikob			fc->ongonode, fc->ongoaddr);
2401103285Sikob		printf("send.buf == NULL\n");
2402103285Sikob		fc->retry_count++;
2403103285Sikob		goto nextnode;
2404103285Sikob	}
2405103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
2406103285Sikob
2407103285Sikob	if(xfer->recv.buf == NULL){
2408103285Sikob		printf("recv.buf == NULL: node=%d addr=0x%x\n",
2409103285Sikob			fc->ongonode, fc->ongoaddr);
2410103285Sikob		fc->retry_count++;
2411103285Sikob		goto nextnode;
2412103285Sikob	}
2413103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2414103285Sikob#if 0
2415103285Sikob	{
2416103285Sikob		u_int32_t *qld;
2417103285Sikob		int i;
2418103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
2419103285Sikob		printf("len:%d\n", xfer->recv.len);
2420103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
2421103285Sikob			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
2422103285Sikob			if((i % 16) == 15) printf("\n");
2423103285Sikob		}
2424103285Sikob		if((i % 16) != 15) printf("\n");
2425103285Sikob	}
2426103285Sikob#endif
2427103285Sikob	if(fc->ongodev == NULL){
2428103285Sikob		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
2429103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
2430103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
2431103285Sikob/* If CSR is minimul confinguration, more investgation is not needed. */
2432103285Sikob			if(chdr->info_len == 1){
2433103285Sikob				goto nextnode;
2434103285Sikob			}else{
2435103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
2436103285Sikob			}
2437103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
2438103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
2439103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
2440103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
2441103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
2442103285Sikob			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
2443103285Sikob				goto nextnode;
2444103285Sikob			fc->ongoaddr = CSRROMOFF;
2445103285Sikob		}
2446103285Sikob	}else{
2447103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
2448103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
2449103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
2450103285Sikob		}
2451103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
2452103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
2453103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
2454103285Sikob			offset = CSRROMOFF;
2455103285Sikob		}else{
2456103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
2457103285Sikob			offset = csrd->off;
2458103285Sikob		}
2459103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
2460103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
2461103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
2462103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
2463103285Sikob				if(csrd == NULL){
2464103285Sikob					goto nextnode;
2465103285Sikob				}else{
2466103285Sikob					csrd->ongoaddr = fc->ongoaddr;
2467103285Sikob					fc->ongoaddr += csrreg->val * 4;
2468103285Sikob					csrd->off = fc->ongoaddr;
2469103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
2470103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
2471103285Sikob					goto nextaddr;
2472103285Sikob				}
2473103285Sikob			}
2474103285Sikob		}
2475103285Sikob		fc->ongoaddr += 4;
2476103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
2477103285Sikob				(fc->ongodev->rommax < 0x414)){
2478103285Sikob			if(fc->ongodev->rommax <= 0x414){
2479103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
2480103285Sikob				if(csrd == NULL) goto nextnode;
2481103285Sikob				csrd->off = fc->ongoaddr;
2482103285Sikob				csrd->ongoaddr = fc->ongoaddr;
2483103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
2484103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
2485103285Sikob			}
2486103285Sikob			goto nextaddr;
2487103285Sikob		}
2488103285Sikob
2489103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
2490103285Sikob			if(csrd == NULL){
2491103285Sikob				goto nextnode;
2492103285Sikob			};
2493103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
2494103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
2495103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
2496103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
2497103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
2498103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
2499103285Sikob				offset = CSRROMOFF;
2500103285Sikob			}else{
2501103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
2502103285Sikob				offset = csrd->off;
2503103285Sikob			}
2504103285Sikob		}
2505103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
2506103285Sikob			goto nextnode;
2507103285Sikob		}
2508103285Sikob	}
2509103285Sikobnextaddr:
2510103285Sikob	fw_xfer_free( xfer);
2511103285Sikob	fw_bus_explore(fc);
2512103285Sikob	return;
2513103285Sikobnextnode:
2514103285Sikob	fw_xfer_free( xfer);
2515103285Sikob	fc->ongonode++;
2516103285Sikob/* housekeeping work space */
2517103285Sikob	fc->ongoaddr = CSRROMOFF;
2518103285Sikob	fc->ongodev = NULL;
2519103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
2520103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
2521103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
2522103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
2523103285Sikob	}
2524103285Sikob	fw_bus_explore(fc);
2525103285Sikob	return;
2526103285Sikob}
2527103285Sikob/*
2528103285Sikob * Async. write responce support for kernel internal use.
2529103285Sikob */
2530103285Sikobint fw_writeres(struct firewire_comm *fc, u_int32_t dst, u_int32_t tlrt){
2531103285Sikob	int err = 0;
2532103285Sikob	struct fw_xfer *xfer;
2533103285Sikob	struct fw_pkt *fp;
2534103285Sikob
2535103285Sikob	xfer = fw_xfer_alloc();
2536103285Sikob	if(xfer == NULL){
2537103285Sikob		err = ENOMEM;
2538103285Sikob		return err;
2539103285Sikob	}
2540103285Sikob	xfer->send.len = 12;
2541103285Sikob	xfer->spd = 0;
2542103285Sikob	xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT);
2543103285Sikob	if(xfer->send.buf == NULL){
2544103285Sikob		return ENOMEM;
2545103285Sikob	}
2546103285Sikob	xfer->send.off = 0;
2547103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2548103285Sikob
2549103285Sikob	fp->mode.wres.tlrt = tlrt;
2550103285Sikob	fp->mode.wres.tcode = FWTCODE_WRES;
2551103285Sikob	fp->mode.wres.pri = 0;
2552103285Sikob	fp->mode.wres.dst = htons(dst);
2553103285Sikob
2554103285Sikob	xfer->act.hand = fw_asy_callback;
2555103285Sikob	err = fw_asyreq(fc, -1, xfer);
2556103285Sikob	if(err){
2557103285Sikob		fw_xfer_free( xfer);
2558103285Sikob		return err;
2559103285Sikob	}
2560103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2561103285Sikob	fw_xfer_free( xfer);
2562103285Sikob
2563103285Sikob	return err;
2564103285Sikob}
2565103285Sikob/*
2566103285Sikob * Async. read responce block support for kernel internal use.
2567103285Sikob */
2568103285Sikobint fw_readresb(struct firewire_comm *fc, u_int32_t dst, u_int32_t tlrt, u_int32_t len, u_int32_t *buf){
2569103285Sikob	int err = 0;
2570103285Sikob	struct fw_xfer *xfer ;
2571103285Sikob	struct fw_pkt *fp;
2572103285Sikob
2573103285Sikob	xfer = fw_xfer_alloc();
2574103285Sikob	if(xfer == NULL){
2575103285Sikob		err = ENOMEM;
2576103285Sikob		return err;
2577103285Sikob	}
2578103285Sikob	xfer->send.len = sizeof(struct fw_pkt) + len;
2579103285Sikob	xfer->spd = 0;
2580103285Sikob	xfer->send.buf = malloc(sizeof(struct fw_pkt) + 1024, M_DEVBUF, M_DONTWAIT);
2581103285Sikob	if(xfer->send.buf == NULL){
2582103285Sikob		return ENOMEM;
2583103285Sikob	}
2584103285Sikob	xfer->send.off = 0;
2585103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2586103285Sikob	fp->mode.rresb.tlrt = tlrt;
2587103285Sikob	fp->mode.rresb.tcode = FWTCODE_RRESB;
2588103285Sikob	fp->mode.rresb.pri = 0;
2589103285Sikob	fp->mode.rresb.dst = htons(dst);
2590103285Sikob	fp->mode.rresb.rtcode = 0;
2591103285Sikob	fp->mode.rresb.extcode = 0;
2592103285Sikob	fp->mode.rresb.len = htons(len);
2593103285Sikob	bcopy(buf, fp->mode.rresb.payload, len);
2594103285Sikob	xfer->act.hand = fw_asy_callback;
2595103285Sikob	err = fw_asyreq(fc, -1, xfer);
2596103285Sikob	if(err){
2597103285Sikob		fw_xfer_free( xfer);
2598103285Sikob		return err;
2599103285Sikob	}
2600103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2601103285Sikob
2602103285Sikob	fw_xfer_free( xfer);
2603103285Sikob	return err;
2604103285Sikob}
2605103285Sikob/*
2606103285Sikob * Async. write request block support for kernel internal use.
2607103285Sikob */
2608103285Sikobint fw_writereqb(struct firewire_comm *fc, u_int32_t addr_hi, u_int32_t addr_lo, u_int len, u_int32_t *buf){
2609103285Sikob	int err = 0;
2610103285Sikob	struct fw_xfer *xfer ;
2611103285Sikob	struct fw_pkt *fp;
2612103285Sikob
2613103285Sikob	xfer = fw_xfer_alloc();
2614103285Sikob	if(xfer == NULL){
2615103285Sikob		err = ENOMEM;
2616103285Sikob		return err;
2617103285Sikob	}
2618103285Sikob	xfer->send.len = sizeof(struct fw_pkt) + len;
2619103285Sikob	xfer->spd = 0;
2620103285Sikob	xfer->send.buf = malloc(sizeof(struct fw_pkt) + 1024, M_DEVBUF, M_DONTWAIT);
2621103285Sikob	if(xfer->send.buf == NULL){
2622103285Sikob		return ENOMEM;
2623103285Sikob	}
2624103285Sikob	xfer->send.off = 0;
2625103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2626103285Sikob	fp->mode.wreqb.dest_hi = htonl(addr_hi & 0xffff);
2627103285Sikob	fp->mode.wreqb.tlrt = 0;
2628103285Sikob	fp->mode.wreqb.tcode = FWTCODE_WREQB;
2629103285Sikob	fp->mode.wreqb.pri = 0;
2630103285Sikob	fp->mode.wreqb.dst = htons(addr_hi >> 16);
2631103285Sikob	fp->mode.wreqb.dest_lo = htonl(addr_lo);
2632103285Sikob	fp->mode.wreqb.len = htons(len);
2633103285Sikob	fp->mode.wreqb.extcode = 0;
2634103285Sikob	bcopy(buf, fp->mode.wreqb.payload, len);
2635103285Sikob	xfer->act.hand = fw_asy_callback;
2636103285Sikob	err = fw_asyreq(fc, -1, xfer);
2637103285Sikob	if(err){
2638103285Sikob		fw_xfer_free( xfer);
2639103285Sikob		return err;
2640103285Sikob	}
2641103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2642103285Sikob
2643103285Sikob	fw_xfer_free( xfer);
2644103285Sikob	return err;
2645103285Sikob}
2646103285Sikob/*
2647103285Sikob * Async. read request support for kernel internal use.
2648103285Sikob */
2649103285Sikobint fw_readreqq(struct firewire_comm *fc, u_int32_t addr_hi, u_int32_t addr_lo, u_int32_t *ret){
2650103285Sikob	int err = 0;
2651103285Sikob	struct fw_xfer *xfer ;
2652103285Sikob	struct fw_pkt *fp, *rfp;
2653103285Sikob
2654103285Sikob	xfer = fw_xfer_alloc();
2655103285Sikob	if(xfer == NULL){
2656103285Sikob		err = ENOMEM;
2657103285Sikob		return err;
2658103285Sikob	}
2659103285Sikob	xfer->send.len = 16;
2660103285Sikob	xfer->spd = 0;
2661103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2662103285Sikob	if(xfer->send.buf == NULL){
2663103285Sikob		return ENOMEM;
2664103285Sikob	}
2665103285Sikob	xfer->send.off = 0;
2666103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2667103285Sikob	fp->mode.rreqq.dest_hi = htonl(addr_hi & 0xffff);
2668103285Sikob	fp->mode.rreqq.tlrt = 0;
2669103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
2670103285Sikob	fp->mode.rreqq.pri = 0;
2671103285Sikob	xfer->dst = addr_hi >> 16;
2672103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
2673103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
2674103285Sikob	xfer->act.hand = fw_asy_callback;
2675103285Sikob	err = fw_asyreq(fc, -1, xfer);
2676103285Sikob	if(err){
2677103285Sikob		fw_xfer_free( xfer);
2678103285Sikob		return err;
2679103285Sikob	}
2680103285Sikob	err = tsleep((caddr_t)xfer, FWPRI, "asyreq", 0);
2681103285Sikob
2682103285Sikob	if(err == 0 && xfer->recv.buf != NULL){
2683103285Sikob		rfp = (struct fw_pkt *)xfer->recv.buf;
2684103285Sikob		*ret = ntohl(rfp->mode.rresq.data);
2685103285Sikob	}
2686103285Sikob	fw_xfer_free( xfer);
2687103285Sikob	return err;
2688103285Sikob}
2689103285Sikob/*
2690103285Sikob * To obtain CSR register values.
2691103285Sikob */
2692103285Sikobu_int32_t getcsrdata(struct fw_device *fwdev, u_int8_t key)
2693103285Sikob{
2694103285Sikob	int i;
2695103285Sikob	struct csrhdr *chdr;
2696103285Sikob	struct csrreg *creg;
2697103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
2698103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
2699103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
2700103285Sikob		if(creg->key == key){
2701103285Sikob			return (u_int32_t)creg->val;
2702103285Sikob		}
2703103285Sikob	}
2704103285Sikob	return 0;
2705103285Sikob}
2706103285Sikob/*
2707103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
2708103285Sikob */
2709103285Sikobstatic void fw_attach_dev(struct firewire_comm *fc)
2710103285Sikob{
2711103285Sikob	struct fw_device *fwdev;
2712103285Sikob	struct fw_xfer *xfer;
2713103285Sikob	int i, err;
2714103285Sikob	device_t *devlistp;
2715103285Sikob	int devcnt;
2716103285Sikob	struct firewire_dev_comm *fdc;
2717103285Sikob
2718103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
2719103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
2720103285Sikob		if(fwdev->status == FWDEVINIT){
2721103285Sikob			fwdev->spec = getcsrdata(fwdev, CSRKEY_SPEC);
2722103285Sikob			if(fwdev->spec == 0)
2723103285Sikob				continue;
2724103285Sikob			fwdev->ver = getcsrdata(fwdev, CSRKEY_VER);
2725103285Sikob			if(fwdev->ver == 0)
2726103285Sikob				continue;
2727103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
2728103285Sikob
2729103285Sikob			switch(fwdev->spec){
2730103285Sikob			case CSRVAL_ANSIT10:
2731103285Sikob				switch(fwdev->ver){
2732103285Sikob				case CSRVAL_T10SBP2:
2733103285Sikob					printf("Device SBP-II");
2734103285Sikob					break;
2735103285Sikob				default:
2736103285Sikob					break;
2737103285Sikob				}
2738103285Sikob				break;
2739103285Sikob			case CSRVAL_1394TA:
2740103285Sikob				switch(fwdev->ver){
2741103285Sikob				case CSR_PROTAVC:
2742103285Sikob					printf("Device AV/C");
2743103285Sikob					break;
2744103285Sikob				case CSR_PROTCAL:
2745103285Sikob					printf("Device CAL");
2746103285Sikob					break;
2747103285Sikob				case CSR_PROTEHS:
2748103285Sikob					printf("Device EHS");
2749103285Sikob					break;
2750103285Sikob				case CSR_PROTHAVI:
2751103285Sikob					printf("Device HAVi");
2752103285Sikob					break;
2753103285Sikob				case CSR_PROTCAM104:
2754103285Sikob					printf("Device 1394 Cam 1.04");
2755103285Sikob					break;
2756103285Sikob				case CSR_PROTCAM120:
2757103285Sikob					printf("Device 1394 Cam 1.20");
2758103285Sikob					break;
2759103285Sikob				case CSR_PROTCAM130:
2760103285Sikob					printf("Device 1394 Cam 1.30");
2761103285Sikob					break;
2762103285Sikob				case CSR_PROTDPP:
2763103285Sikob					printf("Device 1394 Direct print");
2764103285Sikob					break;
2765103285Sikob				case CSR_PROTIICP:
2766103285Sikob					printf("Device Industrial & Instrument");
2767103285Sikob					break;
2768103285Sikob				default:
2769103285Sikob					printf("Device unkwon 1394TA");
2770103285Sikob					break;
2771103285Sikob				}
2772103285Sikob				break;
2773103285Sikob			default:
2774103285Sikob				break;
2775103285Sikob			}
2776103285Sikob			fwdev->status = FWDEVATTACHED;
2777103285Sikob			printf("\n");
2778103285Sikob		}
2779103285Sikob	}
2780103285Sikob	err = device_get_children(fc->dev, &devlistp, &devcnt);
2781103285Sikob	if( err != 0 )
2782103285Sikob		return;
2783103285Sikob	for( i = 0 ; i < devcnt ; i++){
2784103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
2785103285Sikob			fdc = device_get_softc(devlistp[i]);
2786103285Sikob			if (fdc->post_explore != NULL)
2787103285Sikob				fdc->post_explore(fdc);
2788103285Sikob		}
2789103285Sikob	}
2790103285Sikob	free(devlistp, M_TEMP);
2791103285Sikob
2792103285Sikob	/* call pending handlers */
2793103285Sikob	i = 0;
2794103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
2795103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
2796103285Sikob		i++;
2797103285Sikob		if (xfer->act.hand)
2798103285Sikob			xfer->act.hand(xfer);
2799103285Sikob	}
2800103285Sikob	if (i > 0)
2801103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
2802103285Sikob	if (fc->retry_count > 0) {
2803103285Sikob		printf("retry_count = %d\n", fc->retry_count);
2804103285Sikob		fc->retry_probe_handle = timeout((timeout_t *)fc->ibr,
2805103285Sikob							(void *)fc, hz*2);
2806103285Sikob	}
2807103285Sikob	return;
2808103285Sikob}
2809103285Sikob/*
2810103285Sikob * To allocate uniq transaction label.
2811103285Sikob */
2812103285Sikobstatic int fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
2813103285Sikob{
2814103285Sikob	u_int i;
2815103285Sikob	struct tlabel *tl, *tmptl;
2816103285Sikob	int s;
2817103285Sikob	static u_int32_t label = 0;
2818103285Sikob
2819103285Sikob	s = splfw();
2820103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
2821103285Sikob		label = (label + 1) & 0x3f;
2822103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
2823103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
2824103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
2825103285Sikob		}
2826103285Sikob		if(tmptl == NULL) {
2827103285Sikob			tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_DONTWAIT);
2828103285Sikob			if (tl == NULL) {
2829103285Sikob				splx(s);
2830103285Sikob				return (-1);
2831103285Sikob			}
2832103285Sikob			tl->xfer = xfer;
2833103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
2834103285Sikob			splx(s);
2835103285Sikob			return(label);
2836103285Sikob		}
2837103285Sikob	}
2838103285Sikob	splx(s);
2839103285Sikob
2840103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
2841103285Sikob	return(-1);
2842103285Sikob}
2843103285Sikob/*
2844103285Sikob * Generic packet receving process.
2845103285Sikob */
2846103285Sikobvoid fw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
2847103285Sikob{
2848103285Sikob	struct fw_pkt *fp, *resfp;
2849103285Sikob	struct fw_xfer *xfer;
2850103285Sikob	struct fw_bind *bind;
2851103285Sikob	struct firewire_softc *sc;
2852103285Sikob	int s;
2853103285Sikob#if 0
2854103285Sikob	{
2855103285Sikob		u_int32_t *qld;
2856103285Sikob		int i;
2857103285Sikob		qld = (u_int32_t *)buf;
2858103285Sikob		printf("spd %d len:%d\n", spd, len);
2859103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
2860103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
2861103285Sikob			if((i % 16) == 15) printf("\n");
2862103285Sikob		}
2863103285Sikob		if((i % 16) != 15) printf("\n");
2864103285Sikob	}
2865103285Sikob#endif
2866103285Sikob	fp = (struct fw_pkt *)(buf + off);
2867103285Sikob	switch(fp->mode.common.tcode){
2868103285Sikob	case FWTCODE_WRES:
2869103285Sikob	case FWTCODE_RRESQ:
2870103285Sikob	case FWTCODE_RRESB:
2871103285Sikob	case FWTCODE_LRES:
2872103285Sikob		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
2873103285Sikob					fp->mode.hdr.tlrt >> 2);
2874103285Sikob		if(xfer == NULL) {
2875103285Sikob			printf("fw_rcv: unknown response "
2876103285Sikob					"tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n",
2877103285Sikob					fp->mode.common.tcode,
2878103285Sikob					ntohs(fp->mode.hdr.src),
2879103285Sikob					fp->mode.hdr.tlrt >> 2,
2880103285Sikob					fp->mode.hdr.tlrt & 3,
2881103285Sikob					fp->mode.rresq.data);
2882103285Sikob#if 1
2883103285Sikob			printf("try ad-hoc work around!!\n");
2884103285Sikob			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
2885103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
2886103285Sikob			if (xfer == NULL) {
2887103285Sikob				printf("no use...\n");
2888103285Sikob				goto err;
2889103285Sikob			}
2890103285Sikob#else
2891103285Sikob			goto err;
2892103285Sikob#endif
2893103285Sikob		}
2894103285Sikob		switch(xfer->act_type){
2895103285Sikob		case FWACT_XFER:
2896103285Sikob			if((xfer->sub >= 0) &&
2897103285Sikob				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
2898103285Sikob				xfer->resp = EINVAL;
2899103285Sikob				fw_xfer_done(xfer);
2900103285Sikob				goto err;
2901103285Sikob			}
2902103285Sikob			xfer->recv.len = len;
2903103285Sikob			xfer->recv.off = off;
2904103285Sikob			xfer->recv.buf = buf;
2905103285Sikob			xfer->resp = 0;
2906103285Sikob			fw_xfer_done(xfer);
2907103285Sikob			return;
2908103285Sikob			break;
2909103285Sikob		case FWACT_CH:
2910103285Sikob		default:
2911103285Sikob			goto err;
2912103285Sikob			break;
2913103285Sikob		}
2914103285Sikob		break;
2915103285Sikob	case FWTCODE_WREQQ:
2916103285Sikob	case FWTCODE_WREQB:
2917103285Sikob	case FWTCODE_RREQQ:
2918103285Sikob	case FWTCODE_RREQB:
2919103285Sikob	case FWTCODE_LREQ:
2920103285Sikob		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
2921103285Sikob			ntohl(fp->mode.rreqq.dest_lo));
2922103285Sikob		if(bind == NULL){
2923103285Sikob			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
2924103285Sikob				ntohs(fp->mode.rreqq.dest_hi),
2925103285Sikob				ntohl(fp->mode.rreqq.dest_lo),
2926103285Sikob				fp->mode.common.tcode);
2927103285Sikob			if (fc->status == FWBUSRESET) {
2928103285Sikob				printf("fw_rcv: cannot response(bus reset)!\n");
2929103285Sikob				goto err;
2930103285Sikob			}
2931103285Sikob			xfer = fw_xfer_alloc();
2932103285Sikob			if(xfer == NULL){
2933103285Sikob				return;
2934103285Sikob			}
2935103285Sikob			xfer->spd = spd;
2936103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
2937103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
2938103285Sikob			switch(fp->mode.common.tcode){
2939103285Sikob			case FWTCODE_WREQQ:
2940103285Sikob			case FWTCODE_WREQB:
2941103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
2942103285Sikob				xfer->send.len = 12;
2943103285Sikob				break;
2944103285Sikob			case FWTCODE_RREQQ:
2945103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
2946103285Sikob				xfer->send.len = 16;
2947103285Sikob				break;
2948103285Sikob			case FWTCODE_RREQB:
2949103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
2950103285Sikob				xfer->send.len = 16;
2951103285Sikob				break;
2952103285Sikob			case FWTCODE_LREQ:
2953103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
2954103285Sikob				xfer->send.len = 16;
2955103285Sikob				break;
2956103285Sikob			}
2957103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
2958103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
2959103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
2960103285Sikob			resfp->mode.rresb.rtcode = 7;
2961103285Sikob			resfp->mode.rresb.extcode = 0;
2962103285Sikob			resfp->mode.rresb.len = 0;
2963103285Sikob/*
2964103285Sikob			xfer->act.hand = fw_asy_callback;
2965103285Sikob*/
2966103285Sikob			xfer->act.hand = fw_xfer_free;
2967103285Sikob			if(fw_asyreq(fc, -1, xfer)){
2968103285Sikob				fw_xfer_free( xfer);
2969103285Sikob				return;
2970103285Sikob			}
2971103285Sikob			goto err;
2972103285Sikob		}
2973103285Sikob		switch(bind->xfer->act_type){
2974103285Sikob		case FWACT_XFER:
2975103285Sikob			xfer = fw_xfer_alloc();
2976103285Sikob			if(xfer == NULL) goto err;
2977103285Sikob			xfer->fc = bind->xfer->fc;
2978103285Sikob			xfer->sc = bind->xfer->sc;
2979103285Sikob			xfer->recv.buf = buf;
2980103285Sikob			xfer->recv.len = len;
2981103285Sikob			xfer->recv.off = off;
2982103285Sikob			xfer->spd = spd;
2983103285Sikob			xfer->act.hand = bind->xfer->act.hand;
2984103285Sikob			if (fc->status != FWBUSRESET)
2985103285Sikob				xfer->act.hand(xfer);
2986103285Sikob			else
2987103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
2988103285Sikob			return;
2989103285Sikob			break;
2990103285Sikob		case FWACT_CH:
2991103285Sikob			if(fc->ir[bind->xfer->sub]->queued >=
2992103285Sikob				fc->ir[bind->xfer->sub]->maxq){
2993103285Sikob				printf("%s:Discard a packet %x %d\n",
2994103285Sikob					device_get_nameunit(fc->dev),
2995103285Sikob					bind->xfer->sub,
2996103285Sikob					fc->ir[bind->xfer->sub]->queued);
2997103285Sikob				goto err;
2998103285Sikob			}
2999103285Sikob			xfer = fw_xfer_alloc();
3000103285Sikob			if(xfer == NULL) goto err;
3001103285Sikob			xfer->recv.buf = buf;
3002103285Sikob			xfer->recv.len = len;
3003103285Sikob			xfer->recv.off = off;
3004103285Sikob			xfer->spd = spd;
3005103285Sikob			s = splfw();
3006103285Sikob			fc->ir[bind->xfer->sub]->queued++;
3007103285Sikob			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
3008103285Sikob			splx(s);
3009103285Sikob
3010103285Sikob			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
3011103285Sikob
3012103285Sikob			return;
3013103285Sikob			break;
3014103285Sikob		default:
3015103285Sikob			goto err;
3016103285Sikob			break;
3017103285Sikob		}
3018103285Sikob		break;
3019103285Sikob	case FWTCODE_STREAM:
3020103285Sikob	{
3021103285Sikob		struct fw_xferq *xferq;
3022103285Sikob
3023103285Sikob		xferq = fc->ir[sub];
3024103285Sikob#if 0
3025103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
3026103285Sikob			sub, len, off, spd);
3027103285Sikob#endif
3028103285Sikob		if(xferq->queued >= xferq->maxq) {
3029103285Sikob			printf("receive queue is full\n");
3030103285Sikob			goto err;
3031103285Sikob		}
3032103285Sikob		xfer = fw_xfer_alloc();
3033103285Sikob		if(xfer == NULL) goto err;
3034103285Sikob		xfer->recv.buf = buf;
3035103285Sikob		xfer->recv.len = len;
3036103285Sikob		xfer->recv.off = off;
3037103285Sikob		xfer->spd = spd;
3038103285Sikob		s = splfw();
3039103285Sikob		xferq->queued++;
3040103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
3041103285Sikob		splx(s);
3042103285Sikob		sc = device_get_softc(fc->bdev);
3043103285Sikob#if __FreeBSD_version >= 500000
3044103285Sikob		if (SEL_WAITING(&xferq->rsel))
3045103285Sikob#else
3046103285Sikob		if (&xferq->rsel.si_pid != 0)
3047103285Sikob#endif
3048103285Sikob			selwakeup(&xferq->rsel);
3049103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
3050103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
3051103285Sikob			wakeup((caddr_t)xferq);
3052103285Sikob		}
3053103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
3054103285Sikob			xferq->hand(xferq);
3055103285Sikob		}
3056103285Sikob		return;
3057103285Sikob		break;
3058103285Sikob	}
3059103285Sikob	default:
3060103285Sikob		printf("fw_rcv: unknow tcode\n");
3061103285Sikob		break;
3062103285Sikob	}
3063103285Sikoberr:
3064103285Sikob	free(buf, M_DEVBUF);
3065103285Sikob}
3066103285Sikob/*
3067103285Sikob * Post process for Bus Manager election process.
3068103285Sikob */
3069103285Sikobstatic void
3070103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
3071103285Sikob{
3072103285Sikob	struct fw_pkt *sfp,*rfp;
3073103285Sikob	struct firewire_comm *fc;
3074103285Sikob
3075103285Sikob	if(xfer == NULL) return;
3076103285Sikob	fc = xfer->fc;
3077103285Sikob	if(xfer->resp != 0){
3078103285Sikob		goto error;
3079103285Sikob	}
3080103285Sikob
3081103285Sikob	if(xfer->send.buf == NULL){
3082103285Sikob		goto error;
3083103285Sikob	}
3084103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
3085103285Sikob
3086103285Sikob	if(xfer->recv.buf == NULL){
3087103285Sikob		goto error;
3088103285Sikob	}
3089103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
3090103285Sikob	CSRARC(fc, BUS_MGR_ID)
3091103285Sikob		= fc->set_bmr(fc, ntohl(rfp->mode.lres.payload[0]) & 0x3f);
3092103285Sikob	printf("%s: new bus manager %d ",
3093103285Sikob		device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID));
3094103285Sikob	if((htonl(rfp->mode.lres.payload[0]) & 0x3f) == fc->nodeid){
3095103285Sikob		printf("(me)\n");
3096103285Sikob/* If I am bus manager, optimize gapcount */
3097103285Sikob		if(fc->max_hop <= MAX_GAPHOP ){
3098103285Sikob			fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
3099103285Sikob		}
3100103285Sikob	}else{
3101103285Sikob		printf("\n");
3102103285Sikob	}
3103103285Sikoberror:
3104103285Sikob	fw_xfer_free(xfer);
3105103285Sikob}
3106103285Sikob/*
3107103285Sikob * To candidate Bus Manager election process.
3108103285Sikob */
3109103285Sikobvoid fw_try_bmr(void *arg)
3110103285Sikob{
3111103285Sikob	struct fw_xfer *xfer;
3112103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
3113103285Sikob	struct fw_pkt *fp;
3114103285Sikob	int err = 0;
3115103285Sikob
3116103285Sikob	xfer = fw_xfer_alloc();
3117103285Sikob	if(xfer == NULL){
3118103285Sikob		return;
3119103285Sikob	}
3120103285Sikob	xfer->send.len = 24;
3121103285Sikob	xfer->spd = 0;
3122103285Sikob	xfer->send.buf = malloc(24, M_DEVBUF, M_DONTWAIT);
3123103285Sikob	if(xfer->send.buf == NULL){
3124103285Sikob		fw_xfer_free( xfer);
3125103285Sikob		return;
3126103285Sikob	}
3127103285Sikob
3128103285Sikob	fc->status = FWBUSMGRELECT;
3129103285Sikob
3130103285Sikob	xfer->send.off = 0;
3131103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
3132103285Sikob	fp->mode.lreq.dest_hi = htons(0xffff);
3133103285Sikob	fp->mode.lreq.tlrt = 0;
3134103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
3135103285Sikob	fp->mode.lreq.pri = 0;
3136103285Sikob	fp->mode.lreq.src = 0;
3137103285Sikob	fp->mode.lreq.len = htons(8);
3138103285Sikob	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
3139103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
3140103285Sikob	fp->mode.lreq.dst = htons(xfer->dst);
3141103285Sikob	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
3142103285Sikob	fp->mode.lreq.payload[0] = 0x3f;
3143103285Sikob	fp->mode.lreq.payload[1] = fc->nodeid;
3144103285Sikob	xfer->act_type = FWACT_XFER;
3145103285Sikob	xfer->act.hand = fw_try_bmr_callback;
3146103285Sikob
3147103285Sikob	err = fw_asyreq(fc, -1, xfer);
3148103285Sikob	if(err){
3149103285Sikob		fw_xfer_free( xfer);
3150103285Sikob		return;
3151103285Sikob	}
3152103285Sikob	return;
3153103285Sikob}
3154103285Sikob/*
3155103285Sikob * Software implementation for physical memory block access.
3156103285Sikob * XXX:Too slow, usef for debug purpose only.
3157103285Sikob */
3158103285Sikobstatic void fw_vmaccess(struct fw_xfer *xfer){
3159103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
3160103285Sikob	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
3161103285Sikob
3162103285Sikob	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
3163103285Sikob			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
3164103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
3165103285Sikob	if(xfer->resp != 0){
3166103285Sikob		fw_xfer_free( xfer);
3167103285Sikob		return;
3168103285Sikob	}
3169103285Sikob	if(xfer->recv.buf == NULL){
3170103285Sikob		fw_xfer_free( xfer);
3171103285Sikob		return;
3172103285Sikob	}
3173103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
3174103285Sikob	switch(rfp->mode.hdr.tcode){
3175103285Sikob		/* XXX need fix for 64bit arch */
3176103285Sikob		case FWTCODE_WREQB:
3177103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
3178103285Sikob			xfer->send.len = 12;
3179103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
3180103285Sikob			bcopy(rfp->mode.wreqb.payload,
3181103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
3182103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
3183103285Sikob			sfp->mode.wres.rtcode = 0;
3184103285Sikob			break;
3185103285Sikob		case FWTCODE_WREQQ:
3186103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
3187103285Sikob			xfer->send.len = 12;
3188103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
3189103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
3190103285Sikob			sfp->mode.wres.rtcode = 0;
3191103285Sikob			break;
3192103285Sikob		case FWTCODE_RREQB:
3193103285Sikob			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT);
3194103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
3195103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
3196103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
3197103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
3198103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
3199103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
3200103285Sikob			sfp->mode.rresb.rtcode = 0;
3201103285Sikob			sfp->mode.rresb.extcode = 0;
3202103285Sikob			break;
3203103285Sikob		case FWTCODE_RREQQ:
3204103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
3205103285Sikob			xfer->send.len = 16;
3206103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
3207103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
3208103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
3209103285Sikob			sfp->mode.rresb.rtcode = 0;
3210103285Sikob			break;
3211103285Sikob		default:
3212103285Sikob			fw_xfer_free( xfer);
3213103285Sikob			return;
3214103285Sikob	}
3215103285Sikob	xfer->send.off = 0;
3216103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
3217103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
3218103285Sikob	xfer->act.hand = fw_xfer_free;
3219103285Sikob	xfer->retry_req = fw_asybusy;
3220103285Sikob
3221103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
3222103285Sikob	sfp->mode.hdr.pri = 0;
3223103285Sikob
3224103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
3225103285Sikob/**/
3226103285Sikob	return;
3227103285Sikob}
3228103285Sikob/*
3229103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
3230103285Sikob */
3231103285Sikobu_int16_t fw_crc16(u_int32_t *ptr, u_int32_t len){
3232103285Sikob	u_int32_t i, sum, crc = 0;
3233103285Sikob	int shift;
3234103285Sikob	len = (len + 3) & ~3;
3235103285Sikob	for(i = 0 ; i < len ; i+= 4){
3236103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
3237103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
3238103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
3239103285Sikob		}
3240103285Sikob		crc &= 0xffff;
3241103285Sikob	}
3242103285Sikob	return((u_int16_t) crc);
3243103285Sikob}
3244103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
3245103285SikobMODULE_VERSION(firewire, 1);
3246