firewire.c revision 117067
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 117067 2003-06-30 06:33:18Z simokawa $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/types.h>
41103285Sikob#include <sys/mbuf.h>
42103285Sikob#include <sys/socket.h>
43103285Sikob#include <sys/socketvar.h>
44103285Sikob
45103285Sikob#include <sys/kernel.h>
46103285Sikob#include <sys/malloc.h>
47103285Sikob#include <sys/conf.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob
50117067Ssimokawa#if __FreeBSD_version < 500000
51117067Ssimokawa#include <machine/clock.h>	/* for DELAY() */
52117067Ssimokawa#endif
53103285Sikob
54103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
55113584Ssimokawa#include <machine/bus.h>
56103285Sikob
57103285Sikob#include <dev/firewire/firewire.h>
58103285Sikob#include <dev/firewire/firewirereg.h>
59110072Ssimokawa#include <dev/firewire/fwmem.h>
60103285Sikob#include <dev/firewire/iec13213.h>
61103285Sikob#include <dev/firewire/iec68113.h>
62103285Sikob
63116376Ssimokawastruct crom_src_buf {
64116376Ssimokawa	struct crom_src	src;
65116376Ssimokawa	struct crom_chunk root;
66116376Ssimokawa	struct crom_chunk vendor;
67116376Ssimokawa	struct crom_chunk hw;
68116376Ssimokawa};
69116376Ssimokawa
70109736Ssimokawaint firewire_debug=0, try_bmr=1;
71103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
72108281Ssimokawa	"FireWire driver debug flag");
73109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
74109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
75109736Ssimokawa	"Try to be a bus manager");
76103285Sikob
77110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
78110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
79110195Ssimokawa
80103285Sikob#define FW_MAXASYRTY 4
81103285Sikob#define FW_MAXDEVRCNT 4
82103285Sikob
83103285Sikobdevclass_t firewire_devclass;
84103285Sikob
85103285Sikobstatic int firewire_match      __P((device_t));
86103285Sikobstatic int firewire_attach      __P((device_t));
87103285Sikobstatic int firewire_detach      __P((device_t));
88116978Ssimokawastatic int firewire_resume      __P((device_t));
89103285Sikob#if 0
90103285Sikobstatic int firewire_shutdown    __P((device_t));
91103285Sikob#endif
92103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
93103285Sikobstatic void fw_try_bmr __P((void *));
94103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
95103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
96103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
97103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
98103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
99103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
100103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
101106543Ssimokawa#ifdef FW_VMACCESS
102103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
103106543Ssimokawa#endif
104103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
105105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
106110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *));
107103285Sikob
108103285Sikobstatic device_method_t firewire_methods[] = {
109103285Sikob	/* Device interface */
110103285Sikob	DEVMETHOD(device_probe,		firewire_match),
111103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
112103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
113108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
114116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
115103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
116103285Sikob
117103285Sikob	/* Bus interface */
118103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
119103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
120103285Sikob
121103285Sikob	{ 0, 0 }
122103285Sikob};
123103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
124103285Sikob
125114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
126114909Ssimokawa#define MAX_GAPHOP 15
127114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
128114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
129106813Ssimokawa
130106813Ssimokawaextern struct cdevsw firewire_cdevsw;
131106813Ssimokawa
132103285Sikobstatic driver_t firewire_driver = {
133103285Sikob	"firewire",
134103285Sikob	firewire_methods,
135103285Sikob	sizeof(struct firewire_softc),
136103285Sikob};
137103285Sikob
138103285Sikob/*
139110072Ssimokawa * Lookup fwdev by node id.
140103285Sikob */
141106810Ssimokawastruct fw_device *
142110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
143103285Sikob{
144103285Sikob	struct fw_device *fwdev;
145110072Ssimokawa	int s;
146110072Ssimokawa
147110072Ssimokawa	s = splfw();
148110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
149110072Ssimokawa		if (fwdev->dst == dst)
150103285Sikob			break;
151110072Ssimokawa	splx(s);
152110072Ssimokawa
153106810Ssimokawa	if(fwdev == NULL) return NULL;
154106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
155106810Ssimokawa	return fwdev;
156103285Sikob}
157106813Ssimokawa
158103285Sikob/*
159110072Ssimokawa * Lookup fwdev by EUI64.
160110072Ssimokawa */
161110072Ssimokawastruct fw_device *
162110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
163110072Ssimokawa{
164110072Ssimokawa	struct fw_device *fwdev;
165110072Ssimokawa	int s;
166110072Ssimokawa
167110072Ssimokawa	s = splfw();
168110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
169110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
170110072Ssimokawa			break;
171110072Ssimokawa	splx(s);
172110072Ssimokawa
173110072Ssimokawa	if(fwdev == NULL) return NULL;
174110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
175110072Ssimokawa	return fwdev;
176110072Ssimokawa}
177110072Ssimokawa
178110072Ssimokawa/*
179103285Sikob * Async. request procedure for userland application.
180103285Sikob */
181103285Sikobint
182103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
183103285Sikob{
184103285Sikob	int err = 0;
185103285Sikob	struct fw_xferq *xferq;
186103285Sikob	int tl = 0, len;
187103285Sikob	struct fw_pkt *fp;
188103285Sikob	int tcode;
189103285Sikob	struct tcode_info *info;
190103285Sikob
191103285Sikob	if(xfer == NULL) return EINVAL;
192108701Ssimokawa	if(xfer->send.len > MAXREC(fc->maxrec)){
193103285Sikob		printf("send.len > maxrec\n");
194103285Sikob		return EINVAL;
195103285Sikob	}
196103285Sikob	if(xfer->act.hand == NULL){
197103285Sikob		printf("act.hand == NULL\n");
198103285Sikob		return EINVAL;
199103285Sikob	}
200103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
201103285Sikob
202103285Sikob	tcode = fp->mode.common.tcode & 0xf;
203103285Sikob	info = &fc->tcode[tcode];
204103285Sikob	if (info->flag == 0) {
205103285Sikob		printf("invalid tcode=%d\n", tcode);
206103285Sikob		return EINVAL;
207103285Sikob	}
208103285Sikob	if (info->flag & FWTI_REQ)
209103285Sikob		xferq = fc->atq;
210103285Sikob	else
211103285Sikob		xferq = fc->ats;
212103285Sikob	len = info->hdr_len;
213103285Sikob	if (info->flag & FWTI_BLOCK_STR)
214113584Ssimokawa		len += fp->mode.stream.len;
215103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
216113584Ssimokawa		len += fp->mode.rresb.len;
217103285Sikob	if( len >  xfer->send.len ){
218103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
219103285Sikob				len, xfer->send.len, tcode);
220103285Sikob		return EINVAL;
221103285Sikob	}
222103285Sikob	xfer->send.len = len;
223106790Ssimokawa
224103285Sikob	if(xferq->start == NULL){
225103285Sikob		printf("xferq->start == NULL\n");
226103285Sikob		return EINVAL;
227103285Sikob	}
228103285Sikob	if(!(xferq->queued < xferq->maxq)){
229108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
230108655Ssimokawa			xferq->queued);
231103285Sikob		return EINVAL;
232103285Sikob	}
233103285Sikob
234103285Sikob
235103285Sikob	if (info->flag & FWTI_TLABEL) {
236103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
237103285Sikob			return EIO;
238103285Sikob		fp->mode.hdr.tlrt = tl << 2;
239103285Sikob	}
240103285Sikob
241103285Sikob	xfer->tl = tl;
242103285Sikob	xfer->resp = 0;
243103285Sikob	xfer->fc = fc;
244103285Sikob	xfer->q = xferq;
245103285Sikob	xfer->retry_req = fw_asybusy;
246103285Sikob
247103285Sikob	fw_asystart(xfer);
248103285Sikob	return err;
249103285Sikob}
250103285Sikob/*
251103285Sikob * Wakeup blocked process.
252103285Sikob */
253103285Sikobvoid
254103285Sikobfw_asy_callback(struct fw_xfer *xfer){
255103285Sikob	wakeup(xfer);
256103285Sikob	return;
257103285Sikob}
258103285Sikob/*
259103285Sikob * Postpone to later retry.
260103285Sikob */
261103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
262103285Sikob	printf("fw_asybusy\n");
263103285Sikob/*
264103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
265103285Sikob*/
266103285Sikob	DELAY(20000);
267103285Sikob	fw_asystart(xfer);
268103285Sikob	return;
269103285Sikob}
270103285Sikob
271103285Sikob/*
272103285Sikob * Async. request with given xfer structure.
273103285Sikob */
274106790Ssimokawastatic void
275106790Ssimokawafw_asystart(struct fw_xfer *xfer)
276106790Ssimokawa{
277103285Sikob	struct firewire_comm *fc = xfer->fc;
278103285Sikob	int s;
279103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
280113584Ssimokawa		device_printf(fc->bdev, "max_asyretry exceeded\n");
281103285Sikob		xfer->resp = EBUSY;
282103285Sikob		xfer->state = FWXF_BUSY;
283103285Sikob		xfer->act.hand(xfer);
284103285Sikob		return;
285103285Sikob	}
286103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
287103285Sikob	if (fc->status < FWBUSEXPLORE) {
288103285Sikob		xfer->resp = EAGAIN;
289103285Sikob		xfer->state = FWXF_BUSY;
290103285Sikob		if (xfer->act.hand != NULL)
291103285Sikob			xfer->act.hand(xfer);
292103285Sikob		return;
293103285Sikob	}
294103285Sikob#endif
295103285Sikob	s = splfw();
296103285Sikob	xfer->state = FWXF_INQ;
297103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
298103285Sikob	xfer->q->queued ++;
299103285Sikob	splx(s);
300103285Sikob	/* XXX just queue for mbuf */
301103285Sikob	if (xfer->mbuf == NULL)
302103285Sikob		xfer->q->start(fc);
303103285Sikob	return;
304103285Sikob}
305106790Ssimokawa
306103285Sikobstatic int
307103285Sikobfirewire_match( device_t dev )
308103285Sikob{
309108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
310103285Sikob	return -140;
311103285Sikob}
312106790Ssimokawa
313110577Ssimokawastatic void
314110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
315110577Ssimokawa{
316110577Ssimokawa	struct fw_xfer *xfer;
317110577Ssimokawa	struct tlabel *tl;
318110577Ssimokawa	struct timeval tv;
319110577Ssimokawa	struct timeval split_timeout;
320111040Ssimokawa	int i, s;
321110577Ssimokawa
322110891Ssimokawa	split_timeout.tv_sec = 6;
323110577Ssimokawa	split_timeout.tv_usec = 0;
324110577Ssimokawa
325110577Ssimokawa	microtime(&tv);
326110577Ssimokawa	timevalsub(&tv, &split_timeout);
327110577Ssimokawa
328111040Ssimokawa	s = splfw();
329110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
330110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
331110577Ssimokawa			xfer = tl->xfer;
332110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
333110577Ssimokawa				/* the rests are newer than this */
334110577Ssimokawa				break;
335110577Ssimokawa			device_printf(fc->bdev,
336110891Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x\n",
337110577Ssimokawa				xfer->dst, i);
338110577Ssimokawa			xfer->resp = ETIMEDOUT;
339110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
340113584Ssimokawa			fw_xfer_done(xfer);
341110577Ssimokawa		}
342110577Ssimokawa	}
343111040Ssimokawa	splx(s);
344110577Ssimokawa}
345110577Ssimokawa
346110577Ssimokawastatic void
347110577Ssimokawafirewire_watchdog(void *arg)
348110577Ssimokawa{
349110577Ssimokawa	struct firewire_comm *fc;
350110577Ssimokawa
351110577Ssimokawa	fc = (struct firewire_comm *)arg;
352110577Ssimokawa	firewire_xfer_timeout(fc);
353110577Ssimokawa	fc->timeout(fc);
354110577Ssimokawa	callout_reset(&fc->timeout_callout, hz,
355110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
356110577Ssimokawa}
357110577Ssimokawa
358103285Sikob/*
359103285Sikob * The attach routine.
360103285Sikob */
361103285Sikobstatic int
362103285Sikobfirewire_attach( device_t dev )
363103285Sikob{
364103285Sikob	int i, unitmask, mn;
365103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
366103285Sikob	device_t pa = device_get_parent(dev);
367103285Sikob	struct firewire_comm *fc;
368103285Sikob	dev_t d;
369103285Sikob
370103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
371103285Sikob	sc->fc = fc;
372116978Ssimokawa	fc->status = FWBUSNOTREADY;
373103285Sikob
374103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
375103285Sikob
376103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
377103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
378103285Sikob		mn = unitmask | i;
379103285Sikob		/* XXX device name should be improved */
380103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
381108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
382103285Sikob			"fw%x", mn);
383103285Sikob#if __FreeBSD_version >= 500000
384103285Sikob		if (i == 0)
385103285Sikob			sc->dev = d;
386103285Sikob		else
387103285Sikob			dev_depends(sc->dev, d);
388103285Sikob#else
389103285Sikob		sc->dev[i] = d;
390103285Sikob#endif
391103285Sikob	}
392103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
393108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
394103285Sikob			"fwmem%d", device_get_unit(dev));
395103285Sikob#if __FreeBSD_version >= 500000
396103285Sikob	dev_depends(sc->dev, d);
397103285Sikob#else
398103285Sikob	sc->dev[i] = d;
399103285Sikob#endif
400110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
401110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
402110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
403110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
404108853Ssimokawa
405110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
406110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
407110193Ssimokawa
408103285Sikob	/* Locate our children */
409103285Sikob	bus_generic_probe(dev);
410103285Sikob
411103285Sikob	/* launch attachement of the added children */
412103285Sikob	bus_generic_attach(dev);
413103285Sikob
414103285Sikob	/* bus_reset */
415103285Sikob	fc->ibr(fc);
416103285Sikob
417103285Sikob	return 0;
418103285Sikob}
419103285Sikob
420103285Sikob/*
421103285Sikob * Attach it as child.
422103285Sikob */
423103285Sikobstatic device_t
424103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
425103285Sikob{
426103285Sikob        device_t child;
427103285Sikob	struct firewire_softc *sc;
428103285Sikob
429103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
430103285Sikob	child = device_add_child(dev, name, unit);
431103285Sikob	if (child) {
432103285Sikob		device_set_ivars(child, sc->fc);
433103285Sikob		device_probe_and_attach(child);
434103285Sikob	}
435103285Sikob
436103285Sikob	return child;
437103285Sikob}
438106790Ssimokawa
439116978Ssimokawastatic int
440116978Ssimokawafirewire_resume(device_t dev)
441116978Ssimokawa{
442116978Ssimokawa	struct firewire_softc *sc;
443116978Ssimokawa
444116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
445116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
446116978Ssimokawa
447116978Ssimokawa	bus_generic_resume(dev);
448116978Ssimokawa
449116978Ssimokawa	return(0);
450116978Ssimokawa}
451116978Ssimokawa
452103285Sikob/*
453103285Sikob * Dettach it.
454103285Sikob */
455103285Sikobstatic int
456103285Sikobfirewire_detach( device_t dev )
457103285Sikob{
458103285Sikob	struct firewire_softc *sc;
459111074Ssimokawa	struct csrdir *csrd, *next;
460111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
461103285Sikob
462103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
463106790Ssimokawa
464111078Ssimokawa	bus_generic_detach(dev);
465111078Ssimokawa
466111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
467111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
468111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
469111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
470111078Ssimokawa
471103285Sikob#if __FreeBSD_version >= 500000
472103285Sikob	destroy_dev(sc->dev);
473103285Sikob#else
474103285Sikob	{
475103285Sikob		int j;
476103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
477103285Sikob			destroy_dev(sc->dev[j]);
478103285Sikob	}
479103285Sikob#endif
480103285Sikob	/* XXX xfree_free and untimeout on all xfers */
481111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
482111078Ssimokawa							fwdev = fwdev_next) {
483111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
484111078Ssimokawa		free(fwdev, M_FW);
485111078Ssimokawa	}
486111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
487111074Ssimokawa		next = SLIST_NEXT(csrd, link);
488111074Ssimokawa		free(csrd, M_FW);
489111074Ssimokawa	}
490110195Ssimokawa	free(sc->fc->topology_map, M_FW);
491110195Ssimokawa	free(sc->fc->speed_map, M_FW);
492116376Ssimokawa	free(sc->fc->crom_src_buf, M_FW);
493103285Sikob	return(0);
494103285Sikob}
495103285Sikob#if 0
496103285Sikobstatic int
497103285Sikobfirewire_shutdown( device_t dev )
498103285Sikob{
499103285Sikob	return 0;
500103285Sikob}
501103285Sikob#endif
502106790Ssimokawa
503110577Ssimokawa
504110577Ssimokawastatic void
505110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
506110577Ssimokawa{
507110577Ssimokawa	struct fw_xfer *xfer;
508110577Ssimokawa
509110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
510110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
511111942Ssimokawa		xferq->queued --;
512110577Ssimokawa		xfer->resp = EAGAIN;
513113584Ssimokawa		fw_xfer_done(xfer);
514110577Ssimokawa	}
515110577Ssimokawa}
516110577Ssimokawa
517110798Ssimokawavoid
518110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
519110798Ssimokawa{
520110798Ssimokawa	int i;
521110798Ssimokawa
522110798Ssimokawa	fw_xferq_drain(fc->atq);
523110798Ssimokawa	fw_xferq_drain(fc->ats);
524110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
525110798Ssimokawa		fw_xferq_drain(fc->it[i]);
526110798Ssimokawa}
527110798Ssimokawa
528116376Ssimokawastatic void
529116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
530103285Sikob{
531103285Sikob	int i;
532103285Sikob
533103285Sikob	CSRARC(fc, STATE_CLEAR)
534103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
535103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
536103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
537103285Sikob
538103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
539103285Sikob	fc->irm = -1;
540103285Sikob
541103285Sikob	fc->max_node = -1;
542103285Sikob
543103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
544103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
545103285Sikob	}
546103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
547103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
548103285Sikob	CSRARC(fc, RESET_START) = 0;
549103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
550103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
551103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
552103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
553103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
554103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
555103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
556103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
557103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
558103285Sikob
559103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
560103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
561103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
562103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
563103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
564103285Sikob
565103285Sikob/* DV depend CSRs see blue book */
566103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
567103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
568103285Sikob
569103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
570103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
571116376Ssimokawa}
572113584Ssimokawa
573116376Ssimokawastatic void
574116376Ssimokawafw_init_crom(struct firewire_comm *fc)
575116376Ssimokawa{
576116376Ssimokawa	struct crom_src *src;
577116376Ssimokawa
578116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
579116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
580116376Ssimokawa	if (fc->crom_src_buf == NULL)
581116376Ssimokawa		return;
582116376Ssimokawa
583116376Ssimokawa	src = &fc->crom_src_buf->src;
584116376Ssimokawa	bzero(src, sizeof(struct crom_src));
585116376Ssimokawa
586116376Ssimokawa	/* BUS info sample */
587116376Ssimokawa	src->hdr.info_len = 4;
588116376Ssimokawa
589116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
590116376Ssimokawa
591116376Ssimokawa	src->businfo.irmc = 1;
592116376Ssimokawa	src->businfo.cmc = 1;
593116376Ssimokawa	src->businfo.isc = 1;
594116376Ssimokawa	src->businfo.bmc = 1;
595116376Ssimokawa	src->businfo.pmc = 0;
596116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
597116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
598116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
599116376Ssimokawa	src->businfo.generation = 0;
600116376Ssimokawa	src->businfo.link_spd = fc->speed;
601116376Ssimokawa
602116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
603116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
604116376Ssimokawa
605116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
606116376Ssimokawa
607116376Ssimokawa	fc->crom_src = src;
608116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
609116376Ssimokawa}
610116376Ssimokawa
611116376Ssimokawastatic void
612116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
613116376Ssimokawa{
614116376Ssimokawa	struct crom_src_buf *buf;
615116376Ssimokawa	struct crom_src *src;
616116376Ssimokawa	struct crom_chunk *root;
617116376Ssimokawa
618116376Ssimokawa	if (fc->crom_src_buf == NULL)
619116376Ssimokawa		fw_init_crom(fc);
620116376Ssimokawa
621116376Ssimokawa	buf =  fc->crom_src_buf;
622116376Ssimokawa	src = fc->crom_src;
623116376Ssimokawa	root = fc->crom_root;
624116376Ssimokawa
625116376Ssimokawa	src->businfo.generation ++;
626116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
627116376Ssimokawa
628116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
629116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
630116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
631116376Ssimokawa	/* private company_id */
632116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
633116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
634116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
635116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
636116376Ssimokawa}
637116376Ssimokawa
638116376Ssimokawa/*
639116376Ssimokawa * Called after bus reset.
640116376Ssimokawa */
641116376Ssimokawavoid
642116376Ssimokawafw_busreset(struct firewire_comm *fc)
643116376Ssimokawa{
644116376Ssimokawa	struct firewire_dev_comm *fdc;
645116376Ssimokawa	device_t *devlistp;
646116376Ssimokawa	int i, devcnt;
647116376Ssimokawa
648116376Ssimokawa	switch(fc->status){
649116376Ssimokawa	case FWBUSMGRELECT:
650116376Ssimokawa		callout_stop(&fc->bmr_callout);
651116376Ssimokawa		break;
652116376Ssimokawa	default:
653116376Ssimokawa		break;
654116376Ssimokawa	}
655116376Ssimokawa	fc->status = FWBUSRESET;
656116376Ssimokawa	fw_reset_csr(fc);
657116376Ssimokawa	fw_reset_crom(fc);
658116376Ssimokawa
659113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
660113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
661113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
662113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
663113584Ssimokawa				if (fdc->post_busreset != NULL)
664113584Ssimokawa					fdc->post_busreset(fdc);
665113584Ssimokawa			}
666113584Ssimokawa		free(devlistp, M_TEMP);
667113584Ssimokawa	}
668116376Ssimokawa
669116376Ssimokawa	crom_load(&fc->crom_src_buf->src, fc->config_rom, CROMSIZE);
670103285Sikob}
671106790Ssimokawa
672103285Sikob/* Call once after reboot */
673106790Ssimokawavoid fw_init(struct firewire_comm *fc)
674103285Sikob{
675103285Sikob	int i;
676106543Ssimokawa	struct csrdir *csrd;
677106543Ssimokawa#ifdef FW_VMACCESS
678103285Sikob	struct fw_xfer *xfer;
679103285Sikob	struct fw_bind *fwb;
680106543Ssimokawa#endif
681103285Sikob
682103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
683103285Sikob
684103285Sikob	fc->arq->queued = 0;
685103285Sikob	fc->ars->queued = 0;
686103285Sikob	fc->atq->queued = 0;
687103285Sikob	fc->ats->queued = 0;
688103285Sikob
689103285Sikob	fc->arq->buf = NULL;
690103285Sikob	fc->ars->buf = NULL;
691103285Sikob	fc->atq->buf = NULL;
692103285Sikob	fc->ats->buf = NULL;
693103285Sikob
694113584Ssimokawa	fc->arq->flag = 0;
695113584Ssimokawa	fc->ars->flag = 0;
696113584Ssimokawa	fc->atq->flag = 0;
697113584Ssimokawa	fc->ats->flag = 0;
698103285Sikob
699103285Sikob	STAILQ_INIT(&fc->atq->q);
700103285Sikob	STAILQ_INIT(&fc->ats->q);
701103285Sikob
702103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
703103285Sikob		fc->it[i]->queued = 0;
704103285Sikob		fc->ir[i]->queued = 0;
705103285Sikob
706103285Sikob		fc->it[i]->start = NULL;
707103285Sikob		fc->ir[i]->start = NULL;
708103285Sikob
709103285Sikob		fc->it[i]->buf = NULL;
710103285Sikob		fc->ir[i]->buf = NULL;
711103285Sikob
712103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
713103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
714103285Sikob
715103285Sikob		STAILQ_INIT(&fc->it[i]->q);
716103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
717103285Sikob
718103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
719103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
720103285Sikob	}
721103285Sikob
722103285Sikob	fc->arq->maxq = FWMAXQUEUE;
723103285Sikob	fc->ars->maxq = FWMAXQUEUE;
724103285Sikob	fc->atq->maxq = FWMAXQUEUE;
725103285Sikob	fc->ats->maxq = FWMAXQUEUE;
726103285Sikob
727103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
728103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
729103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
730103285Sikob	}
731103285Sikob/* Initialize csr registers */
732103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
733103285Sikob				sizeof(struct fw_topology_map),
734110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
735103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
736103285Sikob				sizeof(struct fw_speed_map),
737110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
738103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
739103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
740103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
741103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
742103285Sikob
743110193Ssimokawa	STAILQ_INIT(&fc->devices);
744103285Sikob	STAILQ_INIT(&fc->pending);
745103285Sikob
746103285Sikob/* Initialize csr ROM work space */
747103285Sikob	SLIST_INIT(&fc->ongocsr);
748103285Sikob	SLIST_INIT(&fc->csrfree);
749103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
750110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
751103285Sikob		if(csrd == NULL) break;
752103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
753103285Sikob	}
754103285Sikob
755103285Sikob/* Initialize Async handlers */
756103285Sikob	STAILQ_INIT(&fc->binds);
757103285Sikob	for( i = 0 ; i < 0x40 ; i++){
758103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
759103285Sikob	}
760103285Sikob
761103285Sikob/* DV depend CSRs see blue book */
762103285Sikob#if 0
763103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
764103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
765103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
766103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
767103285Sikob	}
768103285Sikob
769103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
770103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
771103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
772103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
773103285Sikob	}
774103285Sikob#endif
775103285Sikob
776116376Ssimokawa	fc->crom_src_buf = NULL;
777103285Sikob
778106543Ssimokawa#ifdef FW_VMACCESS
779103285Sikob	xfer = fw_xfer_alloc();
780103285Sikob	if(xfer == NULL) return;
781103285Sikob
782110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
783103285Sikob	if(fwb == NULL){
784103285Sikob		fw_xfer_free(xfer);
785103285Sikob	}
786103285Sikob	xfer->act.hand = fw_vmaccess;
787103285Sikob	xfer->fc = fc;
788103285Sikob	xfer->sc = NULL;
789103285Sikob
790103285Sikob	fwb->start_hi = 0x2;
791103285Sikob	fwb->start_lo = 0;
792103285Sikob	fwb->addrlen = 0xffffffff;
793103285Sikob	fwb->xfer = xfer;
794103285Sikob	fw_bindadd(fc, fwb);
795106543Ssimokawa#endif
796103285Sikob}
797106790Ssimokawa
798103285Sikob/*
799103285Sikob * To lookup binded process from IEEE1394 address.
800103285Sikob */
801106813Ssimokawastruct fw_bind *
802106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
803103285Sikob{
804103285Sikob	struct fw_bind *tfw;
805103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
806103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
807113584Ssimokawa		if (tfw->act_type != FWACT_NULL &&
808103285Sikob			tfw->start_hi == dest_hi &&
809103285Sikob			tfw->start_lo <= dest_lo &&
810103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
811103285Sikob			return(tfw);
812103285Sikob		}
813103285Sikob	}
814103285Sikob	return(NULL);
815103285Sikob}
816106790Ssimokawa
817103285Sikob/*
818103285Sikob * To bind IEEE1394 address block to process.
819103285Sikob */
820106790Ssimokawaint
821106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
822103285Sikob{
823103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
824103285Sikob	int err = 0;
825103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
826103285Sikob	if(tfw == NULL){
827103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
828103285Sikob		goto out;
829103285Sikob	}
830103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
831103285Sikob		(tfw->start_hi == fwb->start_hi &&
832103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
833103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
834103285Sikob		goto out;
835103285Sikob	}
836103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
837103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
838103285Sikob		   (tfw->start_hi == fwb->start_hi &&
839103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
840103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
841103285Sikob			if(tfw2 == NULL)
842103285Sikob				break;
843103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
844103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
845103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
846103285Sikob				break;
847103285Sikob			}else{
848103285Sikob				err = EBUSY;
849103285Sikob				goto out;
850103285Sikob			}
851103285Sikob		}
852103285Sikob	}
853103285Sikob	if(tfw != NULL){
854103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
855103285Sikob	}else{
856103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
857103285Sikob	}
858103285Sikobout:
859113584Ssimokawa	if (!err && fwb->act_type == FWACT_CH)
860113584Ssimokawa		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
861103285Sikob	return err;
862103285Sikob}
863103285Sikob
864103285Sikob/*
865103285Sikob * To free IEEE1394 address block.
866103285Sikob */
867106790Ssimokawaint
868106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
869103285Sikob{
870103285Sikob	int s;
871113584Ssimokawa	struct fw_xfer *xfer, *next;
872103285Sikob
873103285Sikob	s = splfw();
874103285Sikob	/* shall we check the existance? */
875103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
876113584Ssimokawa	/* shall we do this? */
877113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
878113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
879113584Ssimokawa		fw_xfer_free(xfer);
880113584Ssimokawa	}
881113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
882113584Ssimokawa
883103285Sikob	splx(s);
884103285Sikob	return 0;
885103285Sikob}
886103285Sikob
887103285Sikob/*
888103285Sikob * To free transaction label.
889103285Sikob */
890106790Ssimokawastatic void
891106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
892103285Sikob{
893103285Sikob	struct tlabel *tl;
894103285Sikob	int s = splfw();
895103285Sikob
896103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
897103285Sikob		tl = STAILQ_NEXT(tl, link)){
898103285Sikob		if(tl->xfer == xfer){
899103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
900110195Ssimokawa			free(tl, M_FW);
901103285Sikob			splx(s);
902103285Sikob			return;
903103285Sikob		}
904103285Sikob	}
905103285Sikob	splx(s);
906103285Sikob	return;
907103285Sikob}
908106790Ssimokawa
909103285Sikob/*
910103285Sikob * To obtain XFER structure by transaction label.
911103285Sikob */
912106790Ssimokawastatic struct fw_xfer *
913106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
914103285Sikob{
915103285Sikob	struct fw_xfer *xfer;
916103285Sikob	struct tlabel *tl;
917103285Sikob	int s = splfw();
918103285Sikob
919103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
920103285Sikob		tl = STAILQ_NEXT(tl, link)){
921103285Sikob		if(tl->xfer->dst == node){
922103285Sikob			xfer = tl->xfer;
923103285Sikob			splx(s);
924110577Ssimokawa			if (firewire_debug > 2)
925110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
926103285Sikob			return(xfer);
927103285Sikob		}
928103285Sikob	}
929110577Ssimokawa	if (firewire_debug > 1)
930110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
931103285Sikob	splx(s);
932103285Sikob	return(NULL);
933103285Sikob}
934106790Ssimokawa
935103285Sikob/*
936103285Sikob * To allocate IEEE1394 XFER structure.
937103285Sikob */
938106790Ssimokawastruct fw_xfer *
939110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
940103285Sikob{
941103285Sikob	struct fw_xfer *xfer;
942106790Ssimokawa
943110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
944106790Ssimokawa	if (xfer == NULL)
945106790Ssimokawa		return xfer;
946106790Ssimokawa
947110577Ssimokawa	microtime(&xfer->tv);
948110269Ssimokawa	xfer->malloc = type;
949106790Ssimokawa
950103285Sikob	return xfer;
951103285Sikob}
952106790Ssimokawa
953113584Ssimokawastruct fw_xfer *
954113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
955113584Ssimokawa{
956113584Ssimokawa	struct fw_xfer *xfer;
957113584Ssimokawa
958113584Ssimokawa	xfer = fw_xfer_alloc(type);
959113584Ssimokawa	xfer->send.len = send_len;
960113584Ssimokawa	xfer->recv.len = recv_len;
961113584Ssimokawa	if (xfer == NULL)
962113584Ssimokawa		return(NULL);
963113584Ssimokawa	if (send_len) {
964113584Ssimokawa		xfer->send.buf = malloc(send_len, type, M_NOWAIT | M_ZERO);
965113584Ssimokawa		if (xfer->send.buf == NULL) {
966113584Ssimokawa			fw_xfer_free(xfer);
967113584Ssimokawa			return(NULL);
968113584Ssimokawa		}
969113584Ssimokawa	}
970113584Ssimokawa	if (recv_len) {
971113584Ssimokawa		xfer->recv.buf = malloc(recv_len, type, M_NOWAIT);
972113584Ssimokawa		if (xfer->recv.buf == NULL) {
973113584Ssimokawa			if (xfer->send.buf != NULL)
974113584Ssimokawa				free(xfer->send.buf, type);
975113584Ssimokawa			fw_xfer_free(xfer);
976113584Ssimokawa			return(NULL);
977113584Ssimokawa		}
978113584Ssimokawa	}
979113584Ssimokawa	return(xfer);
980113584Ssimokawa}
981113584Ssimokawa
982103285Sikob/*
983103285Sikob * IEEE1394 XFER post process.
984103285Sikob */
985103285Sikobvoid
986103285Sikobfw_xfer_done(struct fw_xfer *xfer)
987103285Sikob{
988103285Sikob	if (xfer->act.hand == NULL)
989103285Sikob		return;
990103285Sikob
991103285Sikob	if (xfer->fc->status != FWBUSRESET)
992103285Sikob		xfer->act.hand(xfer);
993103285Sikob	else {
994103285Sikob		printf("fw_xfer_done: pending\n");
995103285Sikob		if (xfer->fc != NULL)
996103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
997103285Sikob		else
998103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
999103285Sikob	}
1000103285Sikob}
1001103285Sikob
1002106790Ssimokawavoid
1003113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
1004103285Sikob{
1005103285Sikob	int s;
1006113584Ssimokawa
1007103285Sikob	if(xfer == NULL ) return;
1008103285Sikob	if(xfer->state == FWXF_INQ){
1009103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1010103285Sikob		s = splfw();
1011103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1012103285Sikob		xfer->q->queued --;
1013103285Sikob		splx(s);
1014103285Sikob	}
1015113584Ssimokawa	if (xfer->fc != NULL) {
1016114729Ssimokawa#if 1
1017113584Ssimokawa		if(xfer->state == FWXF_START)
1018114729Ssimokawa			/*
1019114729Ssimokawa			 * This could happen if:
1020114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1021114729Ssimokawa			 *  2. firewire_watch() is called.
1022114729Ssimokawa			 */
1023114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1024103285Sikob#endif
1025113584Ssimokawa		fw_tl_free(xfer->fc, xfer);
1026103285Sikob	}
1027113584Ssimokawa	xfer->state = FWXF_INIT;
1028113584Ssimokawa	xfer->resp = 0;
1029113584Ssimokawa	xfer->retry = 0;
1030113584Ssimokawa}
1031113584Ssimokawa/*
1032113584Ssimokawa * To free IEEE1394 XFER structure.
1033113584Ssimokawa */
1034113584Ssimokawavoid
1035113584Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1036113584Ssimokawa{
1037113584Ssimokawa	if(xfer == NULL ) return;
1038113584Ssimokawa	fw_xfer_unload(xfer);
1039103285Sikob	if(xfer->send.buf != NULL){
1040113584Ssimokawa		free(xfer->send.buf, xfer->malloc);
1041103285Sikob	}
1042103285Sikob	if(xfer->recv.buf != NULL){
1043113584Ssimokawa		free(xfer->recv.buf, xfer->malloc);
1044103285Sikob	}
1045110269Ssimokawa	free(xfer, xfer->malloc);
1046103285Sikob}
1047103285Sikob
1048103285Sikobstatic void
1049110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1050103285Sikob{
1051103285Sikob#if 0
1052110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1053103285Sikob				xfer->state, xfer->resp);
1054103285Sikob#endif
1055103285Sikob	fw_xfer_free(xfer);
1056103285Sikob}
1057103285Sikob
1058103285Sikob/*
1059103285Sikob * To configure PHY.
1060103285Sikob */
1061103285Sikobstatic void
1062103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1063103285Sikob{
1064103285Sikob	struct fw_xfer *xfer;
1065103285Sikob	struct fw_pkt *fp;
1066103285Sikob
1067103285Sikob	fc->status = FWBUSPHYCONF;
1068103285Sikob
1069113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 12, 0);
1070113584Ssimokawa	if (xfer == NULL)
1071113584Ssimokawa		return;
1072103285Sikob	xfer->fc = fc;
1073103285Sikob	xfer->retry_req = fw_asybusy;
1074110072Ssimokawa	xfer->act.hand = fw_asy_callback_free;
1075103285Sikob
1076103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1077103285Sikob	fp->mode.ld[1] = 0;
1078103285Sikob	if (root_node >= 0)
1079113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1080103285Sikob	if (gap_count >= 0)
1081113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1082103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1083103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1084103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1085103285Sikob
1086107653Ssimokawa	if (firewire_debug)
1087107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1088103285Sikob						root_node, gap_count);
1089103285Sikob	fw_asyreq(fc, -1, xfer);
1090103285Sikob}
1091103285Sikob
1092103285Sikob#if 0
1093103285Sikob/*
1094103285Sikob * Dump self ID.
1095103285Sikob */
1096103285Sikobstatic void
1097103285Sikobfw_print_sid(u_int32_t sid)
1098103285Sikob{
1099103285Sikob	union fw_self_id *s;
1100103285Sikob	s = (union fw_self_id *) &sid;
1101103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1102103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1103103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1104103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1105103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1106103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1107103285Sikob}
1108103285Sikob#endif
1109103285Sikob
1110103285Sikob/*
1111103285Sikob * To receive self ID.
1112103285Sikob */
1113113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len)
1114103285Sikob{
1115113584Ssimokawa	u_int32_t *p;
1116103285Sikob	union fw_self_id *self_id;
1117103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1118103285Sikob
1119103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1120103285Sikob	fc->status = FWBUSINIT;
1121103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1122103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1123103285Sikob	fc->status = FWBUSCYMELECT;
1124103285Sikob	fc->topology_map->crc_len = 2;
1125103285Sikob	fc->topology_map->generation ++;
1126103285Sikob	fc->topology_map->self_id_count = 0;
1127103285Sikob	fc->topology_map->node_count = 0;
1128103285Sikob	fc->speed_map->generation ++;
1129103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1130103285Sikob	self_id = &fc->topology_map->self_id[0];
1131103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1132103285Sikob		if (sid[1] != ~sid[0]) {
1133103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1134103285Sikob			sid += 2;
1135103285Sikob			continue;
1136103285Sikob		}
1137103285Sikob		*self_id = *((union fw_self_id *)sid);
1138103285Sikob		fc->topology_map->crc_len++;
1139103285Sikob		if(self_id->p0.sequel == 0){
1140103285Sikob			fc->topology_map->node_count ++;
1141103285Sikob			c_port = 0;
1142103285Sikob#if 0
1143103285Sikob			fw_print_sid(sid[0]);
1144103285Sikob#endif
1145103285Sikob			node = self_id->p0.phy_id;
1146103285Sikob			if(fc->max_node < node){
1147103285Sikob				fc->max_node = self_id->p0.phy_id;
1148103285Sikob			}
1149103285Sikob			/* XXX I'm not sure this is the right speed_map */
1150103285Sikob			fc->speed_map->speed[node][node]
1151103285Sikob					= self_id->p0.phy_speed;
1152103285Sikob			for (j = 0; j < node; j ++) {
1153103285Sikob				fc->speed_map->speed[j][node]
1154103285Sikob					= fc->speed_map->speed[node][j]
1155103285Sikob					= min(fc->speed_map->speed[j][j],
1156103285Sikob							self_id->p0.phy_speed);
1157103285Sikob			}
1158103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1159103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1160103285Sikob				fc->irm = self_id->p0.phy_id;
1161103285Sikob			}
1162103285Sikob			if(self_id->p0.port0 >= 0x2){
1163103285Sikob				c_port++;
1164103285Sikob			}
1165103285Sikob			if(self_id->p0.port1 >= 0x2){
1166103285Sikob				c_port++;
1167103285Sikob			}
1168103285Sikob			if(self_id->p0.port2 >= 0x2){
1169103285Sikob				c_port++;
1170103285Sikob			}
1171103285Sikob		}
1172103285Sikob		if(c_port > 2){
1173103285Sikob			i_branch += (c_port - 2);
1174103285Sikob		}
1175103285Sikob		sid += 2;
1176103285Sikob		self_id++;
1177103285Sikob		fc->topology_map->self_id_count ++;
1178103285Sikob	}
1179108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1180103285Sikob	/* CRC */
1181103285Sikob	fc->topology_map->crc = fw_crc16(
1182103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1183103285Sikob			fc->topology_map->crc_len * 4);
1184103285Sikob	fc->speed_map->crc = fw_crc16(
1185103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1186103285Sikob			fc->speed_map->crc_len * 4);
1187103285Sikob	/* byteswap and copy to CSR */
1188103285Sikob	p = (u_int32_t *)fc->topology_map;
1189103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1190103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1191103285Sikob	p = (u_int32_t *)fc->speed_map;
1192103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1193103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1194103285Sikob	/* don't byte-swap u_int8_t array */
1195103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1196103285Sikob
1197103285Sikob	fc->max_hop = fc->max_node - i_branch;
1198103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1199103285Sikob
1200103285Sikob	if(fc->irm == -1 ){
1201103285Sikob		printf(", Not found IRM capable node");
1202103285Sikob	}else{
1203103285Sikob		printf(", cable IRM = %d", fc->irm);
1204103285Sikob		if (fc->irm == fc->nodeid)
1205110016Ssimokawa			printf(" (me)");
1206103285Sikob	}
1207110016Ssimokawa	printf("\n");
1208103285Sikob
1209109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1210114909Ssimokawa		if (fc->irm == fc->nodeid) {
1211103285Sikob			fc->status = FWBUSMGRDONE;
1212103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1213114909Ssimokawa			fw_bmr(fc);
1214109736Ssimokawa		} else {
1215103285Sikob			fc->status = FWBUSMGRELECT;
1216110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1217110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1218103285Sikob		}
1219114909Ssimokawa	} else
1220103285Sikob		fc->status = FWBUSMGRDONE;
1221114909Ssimokawa
1222108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1223108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1224103285Sikob}
1225106790Ssimokawa
1226103285Sikob/*
1227103285Sikob * To probe devices on the IEEE1394 bus.
1228103285Sikob */
1229106790Ssimokawastatic void
1230106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1231103285Sikob{
1232103285Sikob	int s;
1233103285Sikob	struct fw_device *fwdev, *next;
1234103285Sikob
1235103285Sikob	s = splfw();
1236103285Sikob	fc->status = FWBUSEXPLORE;
1237103285Sikob	fc->retry_count = 0;
1238103285Sikob
1239103285Sikob/*
1240103285Sikob * Invalidate all devices, just after bus reset. Devices
1241103285Sikob * to be removed has not been seen longer time.
1242103285Sikob */
1243110193Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1244110193Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1245110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1246103285Sikob			fwdev->status = FWDEVINVAL;
1247103285Sikob			fwdev->rcnt = 0;
1248110193Ssimokawa		} else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1249103285Sikob			fwdev->rcnt ++;
1250110193Ssimokawa		} else {
1251110193Ssimokawa			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1252110195Ssimokawa			free(fwdev, M_FW);
1253103285Sikob		}
1254103285Sikob	}
1255103285Sikob	fc->ongonode = 0;
1256103285Sikob	fc->ongoaddr = CSRROMOFF;
1257103285Sikob	fc->ongodev = NULL;
1258103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1259103285Sikob	fw_bus_explore(fc);
1260103285Sikob	splx(s);
1261103285Sikob}
1262106790Ssimokawa
1263103285Sikob/*
1264103285Sikob * To collect device informations on the IEEE1394 bus.
1265103285Sikob */
1266106790Ssimokawastatic void
1267106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1268103285Sikob{
1269103285Sikob	int err = 0;
1270110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1271103285Sikob	u_int32_t addr;
1272103285Sikob	struct fw_xfer *xfer;
1273103285Sikob	struct fw_pkt *fp;
1274103285Sikob
1275103285Sikob	if(fc->status != FWBUSEXPLORE)
1276103285Sikob		return;
1277103285Sikob
1278103285Sikobloop:
1279103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1280103285Sikob
1281103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1282103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1283103285Sikob
1284103285Sikob	/* check link */
1285103285Sikob	/* XXX we need to check phy_id first */
1286103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1287110577Ssimokawa		if (firewire_debug)
1288110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1289103285Sikob		fc->ongonode++;
1290103285Sikob		goto loop;
1291103285Sikob	}
1292103285Sikob
1293103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1294103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1295103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1296103285Sikob		fc->ongoaddr = CSRROMOFF;
1297103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1298103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1299103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1300103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1301103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1302103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1303103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1304103285Sikob	}else if(fc->ongodev == NULL){
1305110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1306110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1307103285Sikob				break;
1308103285Sikob		if(fwdev != NULL){
1309103285Sikob			fwdev->dst = fc->ongonode;
1310103285Sikob			fwdev->status = FWDEVATTACHED;
1311103285Sikob			fc->ongonode++;
1312103285Sikob			fc->ongoaddr = CSRROMOFF;
1313103285Sikob			fc->ongodev = NULL;
1314103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1315103285Sikob			goto loop;
1316103285Sikob		}
1317115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1318115787Ssimokawa							M_NOWAIT | M_ZERO);
1319103285Sikob		if(fwdev == NULL)
1320103285Sikob			return;
1321106810Ssimokawa		fwdev->fc = fc;
1322103285Sikob		fwdev->rommax = 0;
1323103285Sikob		fwdev->dst = fc->ongonode;
1324103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1325103285Sikob		fwdev->status = FWDEVINIT;
1326103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1327103285Sikob
1328110179Ssimokawa		pfwdev = NULL;
1329110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1330110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1331110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1332110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1333110179Ssimokawa				break;
1334110179Ssimokawa			pfwdev = tfwdev;
1335103285Sikob		}
1336110179Ssimokawa		if (pfwdev == NULL)
1337110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1338110179Ssimokawa		else
1339110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1340103285Sikob
1341108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1342107653Ssimokawa			linkspeed[fwdev->speed],
1343107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1344103285Sikob
1345103285Sikob		fc->ongodev = fwdev;
1346103285Sikob		fc->ongoaddr = CSRROMOFF;
1347103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1348103285Sikob	}else{
1349103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1350103285Sikob	}
1351103285Sikob#if 0
1352103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1353103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1354103285Sikob		fw_bus_explore_callback);
1355103285Sikob	if(xfer == NULL) goto done;
1356103285Sikob#else
1357113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1358103285Sikob	if(xfer == NULL){
1359103285Sikob		goto done;
1360103285Sikob	}
1361103285Sikob	xfer->spd = 0;
1362103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1363113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1364103285Sikob	fp->mode.rreqq.tlrt = 0;
1365103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1366103285Sikob	fp->mode.rreqq.pri = 0;
1367103285Sikob	fp->mode.rreqq.src = 0;
1368103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1369113584Ssimokawa	fp->mode.rreqq.dst = xfer->dst;
1370113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1371103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1372103285Sikob
1373110577Ssimokawa	if (firewire_debug)
1374110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1375110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1376103285Sikob	err = fw_asyreq(fc, -1, xfer);
1377103285Sikob	if(err){
1378103285Sikob		fw_xfer_free( xfer);
1379103285Sikob		return;
1380103285Sikob	}
1381103285Sikob#endif
1382103285Sikob	return;
1383103285Sikobdone:
1384103285Sikob	/* fw_attach_devs */
1385103285Sikob	fc->status = FWBUSEXPDONE;
1386107653Ssimokawa	if (firewire_debug)
1387107653Ssimokawa		printf("bus_explore done\n");
1388103285Sikob	fw_attach_dev(fc);
1389103285Sikob	return;
1390103285Sikob
1391103285Sikob}
1392106790Ssimokawa
1393103285Sikob/* Portable Async. request read quad */
1394106790Ssimokawastruct fw_xfer *
1395106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1396105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1397105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1398103285Sikob{
1399103285Sikob	struct fw_xfer *xfer;
1400103285Sikob	struct fw_pkt *fp;
1401103285Sikob	int err;
1402103285Sikob
1403113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1404113584Ssimokawa	if (xfer == NULL)
1405103285Sikob		return NULL;
1406113584Ssimokawa
1407103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1408103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1409113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1410103285Sikob	if(tl & FWP_TL_VALID){
1411103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1412103285Sikob	}else{
1413103285Sikob		fp->mode.rreqq.tlrt = 0;
1414103285Sikob	}
1415103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1416103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1417103285Sikob	fp->mode.rreqq.pri = 0;
1418103285Sikob	fp->mode.rreqq.src = 0;
1419103285Sikob	xfer->dst = addr_hi >> 16;
1420113584Ssimokawa	fp->mode.rreqq.dst = xfer->dst;
1421113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1422103285Sikob	xfer->act.hand = hand;
1423103285Sikob
1424103285Sikob	err = fw_asyreq(fc, -1, xfer);
1425103285Sikob	if(err){
1426103285Sikob		fw_xfer_free( xfer);
1427103285Sikob		return NULL;
1428103285Sikob	}
1429103285Sikob	return xfer;
1430103285Sikob}
1431106790Ssimokawa
1432103285Sikob/*
1433103285Sikob * Callback for the IEEE1394 bus information collection.
1434103285Sikob */
1435106790Ssimokawastatic void
1436106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1437106790Ssimokawa{
1438103285Sikob	struct firewire_comm *fc;
1439103285Sikob	struct fw_pkt *sfp,*rfp;
1440103285Sikob	struct csrhdr *chdr;
1441103285Sikob	struct csrdir *csrd;
1442103285Sikob	struct csrreg *csrreg;
1443103285Sikob	u_int32_t offset;
1444103285Sikob
1445103285Sikob
1446110577Ssimokawa	if(xfer == NULL) {
1447110577Ssimokawa		printf("xfer == NULL\n");
1448110577Ssimokawa		return;
1449110577Ssimokawa	}
1450103285Sikob	fc = xfer->fc;
1451110577Ssimokawa
1452110577Ssimokawa	if (firewire_debug)
1453110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1454110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1455110577Ssimokawa
1456103285Sikob	if(xfer->resp != 0){
1457110577Ssimokawa		printf("node%d: resp=%d addr=0x%x\n",
1458110577Ssimokawa			fc->ongonode, xfer->resp, fc->ongoaddr);
1459114284Ssimokawa		goto errnode;
1460103285Sikob	}
1461103285Sikob
1462103285Sikob	if(xfer->send.buf == NULL){
1463110577Ssimokawa		printf("node%d: send.buf=NULL addr=0x%x\n",
1464103285Sikob			fc->ongonode, fc->ongoaddr);
1465114284Ssimokawa		goto errnode;
1466103285Sikob	}
1467103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1468103285Sikob
1469103285Sikob	if(xfer->recv.buf == NULL){
1470110577Ssimokawa		printf("node%d: recv.buf=NULL addr=0x%x\n",
1471103285Sikob			fc->ongonode, fc->ongoaddr);
1472114284Ssimokawa		goto errnode;
1473103285Sikob	}
1474103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1475103285Sikob#if 0
1476103285Sikob	{
1477103285Sikob		u_int32_t *qld;
1478103285Sikob		int i;
1479103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1480103285Sikob		printf("len:%d\n", xfer->recv.len);
1481103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1482113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1483103285Sikob			if((i % 16) == 15) printf("\n");
1484103285Sikob		}
1485103285Sikob		if((i % 16) != 15) printf("\n");
1486103285Sikob	}
1487103285Sikob#endif
1488103285Sikob	if(fc->ongodev == NULL){
1489113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1490103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1491103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1492110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */
1493103285Sikob			if(chdr->info_len == 1){
1494110577Ssimokawa				if (firewire_debug)
1495110577Ssimokawa					printf("node%d: minimal config\n",
1496110577Ssimokawa								fc->ongonode);
1497103285Sikob				goto nextnode;
1498103285Sikob			}else{
1499103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1500103285Sikob			}
1501113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1502103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1503103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1504113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1505103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1506110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1507110577Ssimokawa				if (firewire_debug)
1508110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1509110577Ssimokawa							fc->ongonode);
1510103285Sikob				goto nextnode;
1511110577Ssimokawa			}
1512103285Sikob			fc->ongoaddr = CSRROMOFF;
1513103285Sikob		}
1514103285Sikob	}else{
1515103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1516103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1517103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1518103285Sikob		}
1519103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1520103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1521103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1522103285Sikob			offset = CSRROMOFF;
1523103285Sikob		}else{
1524103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1525103285Sikob			offset = csrd->off;
1526103285Sikob		}
1527103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1528103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1529103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1530103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1531103285Sikob				if(csrd == NULL){
1532103285Sikob					goto nextnode;
1533103285Sikob				}else{
1534103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1535103285Sikob					fc->ongoaddr += csrreg->val * 4;
1536103285Sikob					csrd->off = fc->ongoaddr;
1537103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1538103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1539103285Sikob					goto nextaddr;
1540103285Sikob				}
1541103285Sikob			}
1542103285Sikob		}
1543103285Sikob		fc->ongoaddr += 4;
1544103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1545103285Sikob				(fc->ongodev->rommax < 0x414)){
1546103285Sikob			if(fc->ongodev->rommax <= 0x414){
1547103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1548103285Sikob				if(csrd == NULL) goto nextnode;
1549103285Sikob				csrd->off = fc->ongoaddr;
1550103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1551103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1552103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1553103285Sikob			}
1554103285Sikob			goto nextaddr;
1555103285Sikob		}
1556103285Sikob
1557103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1558103285Sikob			if(csrd == NULL){
1559103285Sikob				goto nextnode;
1560103285Sikob			};
1561103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1562103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1563103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1564103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1565103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1566103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1567103285Sikob				offset = CSRROMOFF;
1568103285Sikob			}else{
1569103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1570103285Sikob				offset = csrd->off;
1571103285Sikob			}
1572103285Sikob		}
1573103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1574103285Sikob			goto nextnode;
1575103285Sikob		}
1576103285Sikob	}
1577103285Sikobnextaddr:
1578103285Sikob	fw_xfer_free( xfer);
1579103285Sikob	fw_bus_explore(fc);
1580103285Sikob	return;
1581114284Ssimokawaerrnode:
1582114284Ssimokawa	fc->retry_count++;
1583114284Ssimokawa	if (fc->ongodev != NULL)
1584114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1585103285Sikobnextnode:
1586103285Sikob	fw_xfer_free( xfer);
1587103285Sikob	fc->ongonode++;
1588103285Sikob/* housekeeping work space */
1589103285Sikob	fc->ongoaddr = CSRROMOFF;
1590103285Sikob	fc->ongodev = NULL;
1591103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1592103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1593103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1594103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1595103285Sikob	}
1596103285Sikob	fw_bus_explore(fc);
1597103285Sikob	return;
1598103285Sikob}
1599103285Sikob
1600103285Sikob/*
1601103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1602103285Sikob */
1603106815Ssimokawastatic void
1604106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1605103285Sikob{
1606103285Sikob	struct fw_device *fwdev;
1607103285Sikob	struct fw_xfer *xfer;
1608103285Sikob	int i, err;
1609103285Sikob	device_t *devlistp;
1610103285Sikob	int devcnt;
1611103285Sikob	struct firewire_dev_comm *fdc;
1612103285Sikob
1613114069Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1614114069Ssimokawa		if (fwdev->status == FWDEVINIT)
1615114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1616103285Sikob
1617108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1618103285Sikob	if( err != 0 )
1619103285Sikob		return;
1620103285Sikob	for( i = 0 ; i < devcnt ; i++){
1621103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1622103285Sikob			fdc = device_get_softc(devlistp[i]);
1623103285Sikob			if (fdc->post_explore != NULL)
1624103285Sikob				fdc->post_explore(fdc);
1625103285Sikob		}
1626103285Sikob	}
1627103285Sikob	free(devlistp, M_TEMP);
1628103285Sikob
1629103285Sikob	/* call pending handlers */
1630103285Sikob	i = 0;
1631103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1632103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1633103285Sikob		i++;
1634103285Sikob		if (xfer->act.hand)
1635103285Sikob			xfer->act.hand(xfer);
1636103285Sikob	}
1637103285Sikob	if (i > 0)
1638103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1639103285Sikob	if (fc->retry_count > 0) {
1640111040Ssimokawa		printf("probe failed for %d node\n", fc->retry_count);
1641111040Ssimokawa#if 0
1642110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1643110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1644111040Ssimokawa#endif
1645103285Sikob	}
1646103285Sikob	return;
1647103285Sikob}
1648106815Ssimokawa
1649103285Sikob/*
1650103285Sikob * To allocate uniq transaction label.
1651103285Sikob */
1652106815Ssimokawastatic int
1653106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1654103285Sikob{
1655103285Sikob	u_int i;
1656103285Sikob	struct tlabel *tl, *tmptl;
1657103285Sikob	int s;
1658103285Sikob	static u_int32_t label = 0;
1659103285Sikob
1660103285Sikob	s = splfw();
1661103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1662103285Sikob		label = (label + 1) & 0x3f;
1663103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1664103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1665103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1666103285Sikob		}
1667103285Sikob		if(tmptl == NULL) {
1668110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1669103285Sikob			if (tl == NULL) {
1670103285Sikob				splx(s);
1671103285Sikob				return (-1);
1672103285Sikob			}
1673103285Sikob			tl->xfer = xfer;
1674103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1675103285Sikob			splx(s);
1676110577Ssimokawa			if (firewire_debug > 1)
1677110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1678110577Ssimokawa						xfer->dst, label);
1679103285Sikob			return(label);
1680103285Sikob		}
1681103285Sikob	}
1682103285Sikob	splx(s);
1683103285Sikob
1684103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1685103285Sikob	return(-1);
1686103285Sikob}
1687106815Ssimokawa
1688113584Ssimokawastatic void
1689113584Ssimokawafw_rcv_copy(struct fw_xfer *xfer, struct iovec *vec, int nvec)
1690113584Ssimokawa{
1691113584Ssimokawa	char *p;
1692113584Ssimokawa	int res, i, len;
1693113584Ssimokawa
1694113584Ssimokawa	p = xfer->recv.buf;
1695113584Ssimokawa	res = xfer->recv.len;
1696113584Ssimokawa	for (i = 0; i < nvec; i++, vec++) {
1697113584Ssimokawa		len = vec->iov_len;
1698113584Ssimokawa		if (res < len) {
1699113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1700113584Ssimokawa						xfer->recv.len, len - res);
1701113584Ssimokawa			len = res;
1702113584Ssimokawa		}
1703113584Ssimokawa		bcopy(vec->iov_base, p, len);
1704113584Ssimokawa		p += len;
1705113584Ssimokawa		res -= len;
1706113584Ssimokawa		if (res <= 0)
1707113584Ssimokawa			break;
1708113584Ssimokawa	}
1709113584Ssimokawa	xfer->recv.len -= res;
1710113584Ssimokawa}
1711113584Ssimokawa
1712103285Sikob/*
1713103285Sikob * Generic packet receving process.
1714103285Sikob */
1715106815Ssimokawavoid
1716113584Ssimokawafw_rcv(struct firewire_comm *fc, struct iovec *vec, int nvec, u_int sub, u_int spd)
1717103285Sikob{
1718103285Sikob	struct fw_pkt *fp, *resfp;
1719103285Sikob	struct fw_xfer *xfer;
1720103285Sikob	struct fw_bind *bind;
1721103285Sikob	struct firewire_softc *sc;
1722113584Ssimokawa	int tcode, s;
1723113584Ssimokawa	int i, len, oldstate;
1724103285Sikob#if 0
1725103285Sikob	{
1726103285Sikob		u_int32_t *qld;
1727103285Sikob		int i;
1728103285Sikob		qld = (u_int32_t *)buf;
1729103285Sikob		printf("spd %d len:%d\n", spd, len);
1730103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1731103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1732103285Sikob			if((i % 16) == 15) printf("\n");
1733103285Sikob		}
1734103285Sikob		if((i % 16) != 15) printf("\n");
1735103285Sikob	}
1736103285Sikob#endif
1737113584Ssimokawa	fp = (struct fw_pkt *)vec[0].iov_base;
1738113584Ssimokawa	tcode = fp->mode.common.tcode;
1739113584Ssimokawa#if 0 /* XXX this check is not valid for RRESQ and WREQQ */
1740113584Ssimokawa	if (vec[0].iov_len < fc->tcode[tcode].hdr_len) {
1741113584Ssimokawa#if __FreeBSD_version >= 500000
1742113584Ssimokawa		printf("fw_rcv: iov_len(%zu) is less than"
1743113584Ssimokawa#else
1744113584Ssimokawa		printf("fw_rcv: iov_len(%u) is less than"
1745113584Ssimokawa#endif
1746113584Ssimokawa			" hdr_len(%d:tcode=%d)\n", vec[0].iov_len,
1747113584Ssimokawa			fc->tcode[tcode].hdr_len, tcode);
1748113584Ssimokawa	}
1749113584Ssimokawa#endif
1750113584Ssimokawa	switch (tcode) {
1751103285Sikob	case FWTCODE_WRES:
1752103285Sikob	case FWTCODE_RRESQ:
1753103285Sikob	case FWTCODE_RRESB:
1754103285Sikob	case FWTCODE_LRES:
1755113584Ssimokawa		xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1756103285Sikob					fp->mode.hdr.tlrt >> 2);
1757103285Sikob		if(xfer == NULL) {
1758103285Sikob			printf("fw_rcv: unknown response "
1759110891Ssimokawa					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1760113584Ssimokawa					tcode,
1761113584Ssimokawa					fp->mode.hdr.src,
1762103285Sikob					fp->mode.hdr.tlrt >> 2,
1763103285Sikob					fp->mode.hdr.tlrt & 3,
1764103285Sikob					fp->mode.rresq.data);
1765103285Sikob#if 1
1766103285Sikob			printf("try ad-hoc work around!!\n");
1767113584Ssimokawa			xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1768103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1769103285Sikob			if (xfer == NULL) {
1770103285Sikob				printf("no use...\n");
1771103285Sikob				goto err;
1772103285Sikob			}
1773103285Sikob#else
1774103285Sikob			goto err;
1775103285Sikob#endif
1776103285Sikob		}
1777113584Ssimokawa		fw_rcv_copy(xfer, vec, nvec);
1778113584Ssimokawa		xfer->resp = 0;
1779113584Ssimokawa		/* make sure the packet is drained in AT queue */
1780113584Ssimokawa		oldstate = xfer->state;
1781113584Ssimokawa		xfer->state = FWXF_RCVD;
1782113584Ssimokawa		switch (oldstate) {
1783113584Ssimokawa		case FWXF_SENT:
1784103285Sikob			fw_xfer_done(xfer);
1785103285Sikob			break;
1786113584Ssimokawa		case FWXF_START:
1787113584Ssimokawa			if (firewire_debug)
1788113584Ssimokawa				printf("not sent yet\n");
1789113584Ssimokawa			break;
1790103285Sikob		default:
1791113584Ssimokawa			printf("unexpected state %d\n", xfer->state);
1792103285Sikob		}
1793113584Ssimokawa		return;
1794103285Sikob	case FWTCODE_WREQQ:
1795103285Sikob	case FWTCODE_WREQB:
1796103285Sikob	case FWTCODE_RREQQ:
1797103285Sikob	case FWTCODE_RREQB:
1798103285Sikob	case FWTCODE_LREQ:
1799113584Ssimokawa		bind = fw_bindlookup(fc, fp->mode.rreqq.dest_hi,
1800113584Ssimokawa			fp->mode.rreqq.dest_lo);
1801103285Sikob		if(bind == NULL){
1802113962Ssimokawa#if __FreeBSD_version >= 500000
1803113584Ssimokawa			printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%x\n",
1804113962Ssimokawa#else
1805113962Ssimokawa			printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%lx\n",
1806113962Ssimokawa#endif
1807113584Ssimokawa				fp->mode.wreqq.dest_hi,
1808113584Ssimokawa				fp->mode.wreqq.dest_lo,
1809113584Ssimokawa				tcode,
1810113584Ssimokawa				fp->mode.hdr.src,
1811113584Ssimokawa				ntohl(fp->mode.wreqq.data));
1812103285Sikob			if (fc->status == FWBUSRESET) {
1813110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1814103285Sikob				goto err;
1815103285Sikob			}
1816113584Ssimokawa			xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 0);
1817103285Sikob			if(xfer == NULL){
1818103285Sikob				return;
1819103285Sikob			}
1820103285Sikob			xfer->spd = spd;
1821103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1822113584Ssimokawa			switch (tcode) {
1823103285Sikob			case FWTCODE_WREQQ:
1824103285Sikob			case FWTCODE_WREQB:
1825103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1826103285Sikob				xfer->send.len = 12;
1827103285Sikob				break;
1828103285Sikob			case FWTCODE_RREQQ:
1829103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1830103285Sikob				xfer->send.len = 16;
1831103285Sikob				break;
1832103285Sikob			case FWTCODE_RREQB:
1833103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1834103285Sikob				xfer->send.len = 16;
1835103285Sikob				break;
1836103285Sikob			case FWTCODE_LREQ:
1837103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1838103285Sikob				xfer->send.len = 16;
1839103285Sikob				break;
1840103285Sikob			}
1841103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1842103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1843103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1844103285Sikob			resfp->mode.rresb.rtcode = 7;
1845103285Sikob			resfp->mode.rresb.extcode = 0;
1846103285Sikob			resfp->mode.rresb.len = 0;
1847103285Sikob/*
1848103285Sikob			xfer->act.hand = fw_asy_callback;
1849103285Sikob*/
1850103285Sikob			xfer->act.hand = fw_xfer_free;
1851103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1852103285Sikob				fw_xfer_free( xfer);
1853103285Sikob				return;
1854103285Sikob			}
1855103285Sikob			goto err;
1856103285Sikob		}
1857113584Ssimokawa		len = 0;
1858113584Ssimokawa		for (i = 0; i < nvec; i ++)
1859113584Ssimokawa			len += vec[i].iov_len;
1860113584Ssimokawa		switch(bind->act_type){
1861103285Sikob		case FWACT_XFER:
1862113584Ssimokawa			/* splfw()?? */
1863113584Ssimokawa			xfer = STAILQ_FIRST(&bind->xferlist);
1864113584Ssimokawa			if (xfer == NULL) {
1865113584Ssimokawa				printf("Discard a packet for this bind.\n");
1866113584Ssimokawa				goto err;
1867113584Ssimokawa			}
1868113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1869113584Ssimokawa			fw_rcv_copy(xfer, vec, nvec);
1870103285Sikob			xfer->spd = spd;
1871103285Sikob			if (fc->status != FWBUSRESET)
1872103285Sikob				xfer->act.hand(xfer);
1873103285Sikob			else
1874103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1875103285Sikob			return;
1876103285Sikob			break;
1877103285Sikob		case FWACT_CH:
1878113584Ssimokawa			if(fc->ir[bind->sub]->queued >=
1879113584Ssimokawa				fc->ir[bind->sub]->maxq){
1880108655Ssimokawa				device_printf(fc->bdev,
1881108655Ssimokawa					"Discard a packet %x %d\n",
1882113584Ssimokawa					bind->sub,
1883113584Ssimokawa					fc->ir[bind->sub]->queued);
1884103285Sikob				goto err;
1885103285Sikob			}
1886113584Ssimokawa			xfer = STAILQ_FIRST(&bind->xferlist);
1887113584Ssimokawa			if (xfer == NULL) {
1888113584Ssimokawa				printf("Discard packet for this bind\n");
1889113584Ssimokawa				goto err;
1890113584Ssimokawa			}
1891113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1892113584Ssimokawa			fw_rcv_copy(xfer, vec, nvec);
1893103285Sikob			xfer->spd = spd;
1894103285Sikob			s = splfw();
1895113584Ssimokawa			fc->ir[bind->sub]->queued++;
1896113584Ssimokawa			STAILQ_INSERT_TAIL(&fc->ir[bind->sub]->q, xfer, link);
1897103285Sikob			splx(s);
1898103285Sikob
1899113584Ssimokawa			wakeup((caddr_t)fc->ir[bind->sub]);
1900103285Sikob
1901103285Sikob			return;
1902103285Sikob			break;
1903103285Sikob		default:
1904103285Sikob			goto err;
1905103285Sikob			break;
1906103285Sikob		}
1907103285Sikob		break;
1908103285Sikob	case FWTCODE_STREAM:
1909103285Sikob	{
1910103285Sikob		struct fw_xferq *xferq;
1911103285Sikob
1912103285Sikob		xferq = fc->ir[sub];
1913103285Sikob#if 0
1914103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1915103285Sikob			sub, len, off, spd);
1916103285Sikob#endif
1917103285Sikob		if(xferq->queued >= xferq->maxq) {
1918103285Sikob			printf("receive queue is full\n");
1919103285Sikob			goto err;
1920103285Sikob		}
1921113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1922113584Ssimokawa			per packet mode */
1923113584Ssimokawa		xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1924113584Ssimokawa						vec[0].iov_len);
1925103285Sikob		if(xfer == NULL) goto err;
1926113584Ssimokawa		fw_rcv_copy(xfer, vec, nvec);
1927103285Sikob		xfer->spd = spd;
1928103285Sikob		s = splfw();
1929103285Sikob		xferq->queued++;
1930103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1931103285Sikob		splx(s);
1932103285Sikob		sc = device_get_softc(fc->bdev);
1933103285Sikob#if __FreeBSD_version >= 500000
1934103285Sikob		if (SEL_WAITING(&xferq->rsel))
1935103285Sikob#else
1936103285Sikob		if (&xferq->rsel.si_pid != 0)
1937103285Sikob#endif
1938103285Sikob			selwakeup(&xferq->rsel);
1939103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1940103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1941103285Sikob			wakeup((caddr_t)xferq);
1942103285Sikob		}
1943103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1944103285Sikob			xferq->hand(xferq);
1945103285Sikob		}
1946103285Sikob		return;
1947103285Sikob		break;
1948103285Sikob	}
1949103285Sikob	default:
1950113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1951103285Sikob		break;
1952103285Sikob	}
1953103285Sikoberr:
1954113584Ssimokawa	return;
1955103285Sikob}
1956106815Ssimokawa
1957103285Sikob/*
1958103285Sikob * Post process for Bus Manager election process.
1959103285Sikob */
1960103285Sikobstatic void
1961103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1962103285Sikob{
1963109422Ssimokawa	struct fw_pkt *rfp;
1964103285Sikob	struct firewire_comm *fc;
1965109422Ssimokawa	int bmr;
1966103285Sikob
1967109422Ssimokawa	if (xfer == NULL)
1968109422Ssimokawa		return;
1969103285Sikob	fc = xfer->fc;
1970109422Ssimokawa	if (xfer->resp != 0)
1971103285Sikob		goto error;
1972109422Ssimokawa	if (xfer->send.buf == NULL)
1973103285Sikob		goto error;
1974109422Ssimokawa	if (xfer->recv.buf == NULL)
1975103285Sikob		goto error;
1976103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1977109422Ssimokawa	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1978109422Ssimokawa		goto error;
1979109422Ssimokawa
1980109422Ssimokawa	bmr = ntohl(rfp->mode.lres.payload[0]);
1981109422Ssimokawa	if (bmr == 0x3f)
1982109422Ssimokawa		bmr = fc->nodeid;
1983109422Ssimokawa
1984109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1985114909Ssimokawa	fw_xfer_free(xfer);
1986114909Ssimokawa	fw_bmr(fc);
1987114909Ssimokawa	return;
1988114909Ssimokawa
1989103285Sikoberror:
1990114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
1991103285Sikob	fw_xfer_free(xfer);
1992103285Sikob}
1993106815Ssimokawa
1994113584Ssimokawa
1995103285Sikob/*
1996103285Sikob * To candidate Bus Manager election process.
1997103285Sikob */
1998110270Ssimokawastatic void
1999106815Ssimokawafw_try_bmr(void *arg)
2000103285Sikob{
2001103285Sikob	struct fw_xfer *xfer;
2002103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2003103285Sikob	struct fw_pkt *fp;
2004103285Sikob	int err = 0;
2005103285Sikob
2006113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 24, 20);
2007103285Sikob	if(xfer == NULL){
2008103285Sikob		return;
2009103285Sikob	}
2010103285Sikob	xfer->spd = 0;
2011103285Sikob	fc->status = FWBUSMGRELECT;
2012103285Sikob
2013103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2014113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2015103285Sikob	fp->mode.lreq.tlrt = 0;
2016103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2017103285Sikob	fp->mode.lreq.pri = 0;
2018103285Sikob	fp->mode.lreq.src = 0;
2019113584Ssimokawa	fp->mode.lreq.len = 8;
2020113584Ssimokawa	fp->mode.lreq.extcode = FW_LREQ_CMPSWAP;
2021103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
2022113584Ssimokawa	fp->mode.lreq.dst = xfer->dst;
2023113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2024109422Ssimokawa	fp->mode.lreq.payload[0] = htonl(0x3f);
2025109422Ssimokawa	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
2026103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2027103285Sikob
2028103285Sikob	err = fw_asyreq(fc, -1, xfer);
2029103285Sikob	if(err){
2030103285Sikob		fw_xfer_free( xfer);
2031103285Sikob		return;
2032103285Sikob	}
2033103285Sikob	return;
2034103285Sikob}
2035106543Ssimokawa
2036106543Ssimokawa#ifdef FW_VMACCESS
2037103285Sikob/*
2038103285Sikob * Software implementation for physical memory block access.
2039103285Sikob * XXX:Too slow, usef for debug purpose only.
2040103285Sikob */
2041106815Ssimokawastatic void
2042106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2043103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2044113584Ssimokawa	u_int32_t *ld = (u_int32_t *)xfer->recv.buf;
2045103285Sikob
2046113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2047113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2048103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2049103285Sikob	if(xfer->resp != 0){
2050103285Sikob		fw_xfer_free( xfer);
2051103285Sikob		return;
2052103285Sikob	}
2053103285Sikob	if(xfer->recv.buf == NULL){
2054103285Sikob		fw_xfer_free( xfer);
2055103285Sikob		return;
2056103285Sikob	}
2057103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2058103285Sikob	switch(rfp->mode.hdr.tcode){
2059103285Sikob		/* XXX need fix for 64bit arch */
2060103285Sikob		case FWTCODE_WREQB:
2061110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2062103285Sikob			xfer->send.len = 12;
2063103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2064103285Sikob			bcopy(rfp->mode.wreqb.payload,
2065103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2066103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2067103285Sikob			sfp->mode.wres.rtcode = 0;
2068103285Sikob			break;
2069103285Sikob		case FWTCODE_WREQQ:
2070110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2071103285Sikob			xfer->send.len = 12;
2072103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2073103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2074103285Sikob			sfp->mode.wres.rtcode = 0;
2075103285Sikob			break;
2076103285Sikob		case FWTCODE_RREQB:
2077110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2078103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2079103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2080103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2081103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2082103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2083103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2084103285Sikob			sfp->mode.rresb.rtcode = 0;
2085103285Sikob			sfp->mode.rresb.extcode = 0;
2086103285Sikob			break;
2087103285Sikob		case FWTCODE_RREQQ:
2088110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2089103285Sikob			xfer->send.len = 16;
2090103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2091103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2092103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2093103285Sikob			sfp->mode.rresb.rtcode = 0;
2094103285Sikob			break;
2095103285Sikob		default:
2096103285Sikob			fw_xfer_free( xfer);
2097103285Sikob			return;
2098103285Sikob	}
2099103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2100103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2101103285Sikob	xfer->act.hand = fw_xfer_free;
2102103285Sikob	xfer->retry_req = fw_asybusy;
2103103285Sikob
2104103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2105103285Sikob	sfp->mode.hdr.pri = 0;
2106103285Sikob
2107103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2108103285Sikob/**/
2109103285Sikob	return;
2110103285Sikob}
2111106543Ssimokawa#endif
2112106543Ssimokawa
2113103285Sikob/*
2114103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2115103285Sikob */
2116106815Ssimokawau_int16_t
2117106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2118103285Sikob	u_int32_t i, sum, crc = 0;
2119103285Sikob	int shift;
2120103285Sikob	len = (len + 3) & ~3;
2121103285Sikob	for(i = 0 ; i < len ; i+= 4){
2122103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2123103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2124103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2125103285Sikob		}
2126103285Sikob		crc &= 0xffff;
2127103285Sikob	}
2128103285Sikob	return((u_int16_t) crc);
2129103285Sikob}
2130106815Ssimokawa
2131110270Ssimokawastatic int
2132110072Ssimokawafw_bmr(struct firewire_comm *fc)
2133110072Ssimokawa{
2134110072Ssimokawa	struct fw_device fwdev;
2135113584Ssimokawa	union fw_self_id *self_id;
2136110072Ssimokawa	int cmstr;
2137110072Ssimokawa
2138113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2139113584Ssimokawa	self_id = &fc->topology_map->self_id[fc->max_node];
2140113584Ssimokawa	if (fc->max_node > 0) {
2141114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2142114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2143113584Ssimokawa			cmstr = fc->max_node;
2144114909Ssimokawa		else {
2145114909Ssimokawa			device_printf(fc->bdev,
2146114909Ssimokawa				"root node is not cycle master capable\n");
2147114909Ssimokawa			/* XXX shall we be the cycle master? */
2148113584Ssimokawa			cmstr = fc->nodeid;
2149114909Ssimokawa			/* XXX need bus reset */
2150114909Ssimokawa		}
2151113584Ssimokawa	} else
2152113584Ssimokawa		cmstr = -1;
2153114909Ssimokawa
2154114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2155114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2156114909Ssimokawa		/* We are not the bus manager */
2157114909Ssimokawa		printf("\n");
2158114909Ssimokawa		return(0);
2159114909Ssimokawa	}
2160114909Ssimokawa	printf("(me)\n");
2161114909Ssimokawa
2162114909Ssimokawa	/* Optimize gapcount */
2163113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2164113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2165110072Ssimokawa	/* If we are the cycle master, nothing to do */
2166113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2167110072Ssimokawa		return 0;
2168110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2169110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2170110072Ssimokawa	fwdev.fc = fc;
2171110072Ssimokawa	fwdev.dst = cmstr;
2172110072Ssimokawa	fwdev.speed = 0;
2173110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2174110072Ssimokawa	fwdev.status = FWDEVINIT;
2175110072Ssimokawa	/* Set cmstr bit on the cycle master */
2176110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2177114909Ssimokawa		0xffff, 0xf0000000 | STATE_SET, htonl(1 << 8),
2178110072Ssimokawa		fw_asy_callback_free);
2179110072Ssimokawa
2180110072Ssimokawa	return 0;
2181110072Ssimokawa}
2182110072Ssimokawa
2183103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2184103285SikobMODULE_VERSION(firewire, 1);
2185