firewire.c revision 124145
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 124145 2004-01-05 14:21:18Z simokawa $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/types.h>
41103285Sikob
42103285Sikob#include <sys/kernel.h>
43103285Sikob#include <sys/malloc.h>
44103285Sikob#include <sys/conf.h>
45103285Sikob#include <sys/sysctl.h>
46103285Sikob
47117067Ssimokawa#if __FreeBSD_version < 500000
48117067Ssimokawa#include <machine/clock.h>	/* for DELAY() */
49117067Ssimokawa#endif
50103285Sikob
51103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
52113584Ssimokawa#include <machine/bus.h>
53103285Sikob
54103285Sikob#include <dev/firewire/firewire.h>
55103285Sikob#include <dev/firewire/firewirereg.h>
56110072Ssimokawa#include <dev/firewire/fwmem.h>
57103285Sikob#include <dev/firewire/iec13213.h>
58103285Sikob#include <dev/firewire/iec68113.h>
59103285Sikob
60116376Ssimokawastruct crom_src_buf {
61116376Ssimokawa	struct crom_src	src;
62116376Ssimokawa	struct crom_chunk root;
63116376Ssimokawa	struct crom_chunk vendor;
64116376Ssimokawa	struct crom_chunk hw;
65116376Ssimokawa};
66116376Ssimokawa
67120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3;
68103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
69108281Ssimokawa	"FireWire driver debug flag");
70109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
71109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
72109736Ssimokawa	"Try to be a bus manager");
73120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
74120850Ssimokawa	"Number of count of bus resets for removing lost device information");
75103285Sikob
76110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
77110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
78110195Ssimokawa
79103285Sikob#define FW_MAXASYRTY 4
80103285Sikob
81103285Sikobdevclass_t firewire_devclass;
82103285Sikob
83103285Sikobstatic int firewire_match      __P((device_t));
84103285Sikobstatic int firewire_attach      __P((device_t));
85103285Sikobstatic int firewire_detach      __P((device_t));
86116978Ssimokawastatic int firewire_resume      __P((device_t));
87103285Sikob#if 0
88103285Sikobstatic int firewire_shutdown    __P((device_t));
89103285Sikob#endif
90103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
91103285Sikobstatic void fw_try_bmr __P((void *));
92103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
93103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
94103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
95103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
96103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
97103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
98103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
99106543Ssimokawa#ifdef FW_VMACCESS
100103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
101106543Ssimokawa#endif
102103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
103105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
104110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *));
105103285Sikob
106103285Sikobstatic device_method_t firewire_methods[] = {
107103285Sikob	/* Device interface */
108103285Sikob	DEVMETHOD(device_probe,		firewire_match),
109103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
110103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
111108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
112116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
113103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
114103285Sikob
115103285Sikob	/* Bus interface */
116103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
117103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
118103285Sikob
119103285Sikob	{ 0, 0 }
120103285Sikob};
121103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
122103285Sikob
123114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
124114909Ssimokawa#define MAX_GAPHOP 15
125114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
126114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
127106813Ssimokawa
128103285Sikobstatic driver_t firewire_driver = {
129103285Sikob	"firewire",
130103285Sikob	firewire_methods,
131103285Sikob	sizeof(struct firewire_softc),
132103285Sikob};
133103285Sikob
134103285Sikob/*
135110072Ssimokawa * Lookup fwdev by node id.
136103285Sikob */
137106810Ssimokawastruct fw_device *
138110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
139103285Sikob{
140103285Sikob	struct fw_device *fwdev;
141110072Ssimokawa	int s;
142110072Ssimokawa
143110072Ssimokawa	s = splfw();
144110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
145120660Ssimokawa		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
146103285Sikob			break;
147110072Ssimokawa	splx(s);
148110072Ssimokawa
149106810Ssimokawa	return fwdev;
150103285Sikob}
151106813Ssimokawa
152103285Sikob/*
153110072Ssimokawa * Lookup fwdev by EUI64.
154110072Ssimokawa */
155110072Ssimokawastruct fw_device *
156110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
157110072Ssimokawa{
158110072Ssimokawa	struct fw_device *fwdev;
159110072Ssimokawa	int s;
160110072Ssimokawa
161110072Ssimokawa	s = splfw();
162110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
163110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
164110072Ssimokawa			break;
165110072Ssimokawa	splx(s);
166110072Ssimokawa
167110072Ssimokawa	if(fwdev == NULL) return NULL;
168110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
169110072Ssimokawa	return fwdev;
170110072Ssimokawa}
171110072Ssimokawa
172110072Ssimokawa/*
173103285Sikob * Async. request procedure for userland application.
174103285Sikob */
175103285Sikobint
176103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
177103285Sikob{
178103285Sikob	int err = 0;
179103285Sikob	struct fw_xferq *xferq;
180103285Sikob	int tl = 0, len;
181103285Sikob	struct fw_pkt *fp;
182103285Sikob	int tcode;
183103285Sikob	struct tcode_info *info;
184103285Sikob
185103285Sikob	if(xfer == NULL) return EINVAL;
186103285Sikob	if(xfer->act.hand == NULL){
187103285Sikob		printf("act.hand == NULL\n");
188103285Sikob		return EINVAL;
189103285Sikob	}
190120660Ssimokawa	fp = &xfer->send.hdr;
191103285Sikob
192103285Sikob	tcode = fp->mode.common.tcode & 0xf;
193103285Sikob	info = &fc->tcode[tcode];
194103285Sikob	if (info->flag == 0) {
195103285Sikob		printf("invalid tcode=%d\n", tcode);
196103285Sikob		return EINVAL;
197103285Sikob	}
198103285Sikob	if (info->flag & FWTI_REQ)
199103285Sikob		xferq = fc->atq;
200103285Sikob	else
201103285Sikob		xferq = fc->ats;
202103285Sikob	len = info->hdr_len;
203120660Ssimokawa	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
204120660Ssimokawa		printf("send.pay_len > maxrec\n");
205120660Ssimokawa		return EINVAL;
206120660Ssimokawa	}
207103285Sikob	if (info->flag & FWTI_BLOCK_STR)
208120660Ssimokawa		len = fp->mode.stream.len;
209103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
210120660Ssimokawa		len = fp->mode.rresb.len;
211120660Ssimokawa	else
212120660Ssimokawa		len = 0;
213120660Ssimokawa	if (len != xfer->send.pay_len){
214120660Ssimokawa		printf("len(%d) != send.pay_len(%d) (tcode=%d)\n",
215120660Ssimokawa				len, xfer->send.pay_len, tcode);
216103285Sikob		return EINVAL;
217103285Sikob	}
218106790Ssimokawa
219103285Sikob	if(xferq->start == NULL){
220103285Sikob		printf("xferq->start == NULL\n");
221103285Sikob		return EINVAL;
222103285Sikob	}
223103285Sikob	if(!(xferq->queued < xferq->maxq)){
224108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
225108655Ssimokawa			xferq->queued);
226103285Sikob		return EINVAL;
227103285Sikob	}
228103285Sikob
229120660Ssimokawa	microtime(&xfer->tv);
230103285Sikob	if (info->flag & FWTI_TLABEL) {
231103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
232103285Sikob			return EIO;
233103285Sikob		fp->mode.hdr.tlrt = tl << 2;
234103285Sikob	}
235103285Sikob
236103285Sikob	xfer->tl = tl;
237103285Sikob	xfer->resp = 0;
238103285Sikob	xfer->fc = fc;
239103285Sikob	xfer->q = xferq;
240103285Sikob	xfer->retry_req = fw_asybusy;
241103285Sikob
242103285Sikob	fw_asystart(xfer);
243103285Sikob	return err;
244103285Sikob}
245103285Sikob/*
246103285Sikob * Wakeup blocked process.
247103285Sikob */
248103285Sikobvoid
249103285Sikobfw_asy_callback(struct fw_xfer *xfer){
250103285Sikob	wakeup(xfer);
251103285Sikob	return;
252103285Sikob}
253103285Sikob/*
254103285Sikob * Postpone to later retry.
255103285Sikob */
256103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
257103285Sikob	printf("fw_asybusy\n");
258103285Sikob/*
259103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
260103285Sikob*/
261124145Ssimokawa#if 0
262103285Sikob	DELAY(20000);
263124145Ssimokawa#endif
264103285Sikob	fw_asystart(xfer);
265103285Sikob	return;
266103285Sikob}
267103285Sikob
268103285Sikob/*
269103285Sikob * Async. request with given xfer structure.
270103285Sikob */
271106790Ssimokawastatic void
272106790Ssimokawafw_asystart(struct fw_xfer *xfer)
273106790Ssimokawa{
274103285Sikob	struct firewire_comm *fc = xfer->fc;
275103285Sikob	int s;
276103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
277113584Ssimokawa		device_printf(fc->bdev, "max_asyretry exceeded\n");
278103285Sikob		xfer->resp = EBUSY;
279103285Sikob		xfer->state = FWXF_BUSY;
280103285Sikob		xfer->act.hand(xfer);
281103285Sikob		return;
282103285Sikob	}
283103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
284103285Sikob	if (fc->status < FWBUSEXPLORE) {
285103285Sikob		xfer->resp = EAGAIN;
286103285Sikob		xfer->state = FWXF_BUSY;
287103285Sikob		if (xfer->act.hand != NULL)
288103285Sikob			xfer->act.hand(xfer);
289103285Sikob		return;
290103285Sikob	}
291103285Sikob#endif
292103285Sikob	s = splfw();
293103285Sikob	xfer->state = FWXF_INQ;
294103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
295103285Sikob	xfer->q->queued ++;
296103285Sikob	splx(s);
297103285Sikob	/* XXX just queue for mbuf */
298103285Sikob	if (xfer->mbuf == NULL)
299103285Sikob		xfer->q->start(fc);
300103285Sikob	return;
301103285Sikob}
302106790Ssimokawa
303103285Sikobstatic int
304103285Sikobfirewire_match( device_t dev )
305103285Sikob{
306108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
307103285Sikob	return -140;
308103285Sikob}
309106790Ssimokawa
310110577Ssimokawastatic void
311110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
312110577Ssimokawa{
313110577Ssimokawa	struct fw_xfer *xfer;
314110577Ssimokawa	struct tlabel *tl;
315110577Ssimokawa	struct timeval tv;
316110577Ssimokawa	struct timeval split_timeout;
317111040Ssimokawa	int i, s;
318110577Ssimokawa
319120660Ssimokawa	split_timeout.tv_sec = 0;
320120660Ssimokawa	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
321110577Ssimokawa
322110577Ssimokawa	microtime(&tv);
323110577Ssimokawa	timevalsub(&tv, &split_timeout);
324110577Ssimokawa
325111040Ssimokawa	s = splfw();
326110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
327110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
328110577Ssimokawa			xfer = tl->xfer;
329110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
330110577Ssimokawa				/* the rests are newer than this */
331110577Ssimokawa				break;
332124145Ssimokawa			if (xfer->state == FWXF_START)
333124145Ssimokawa				/* not sent yet */
334124145Ssimokawa				break;
335110577Ssimokawa			device_printf(fc->bdev,
336117716Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
337120660Ssimokawa				xfer->send.hdr.mode.hdr.dst, i, xfer->state);
338110577Ssimokawa			xfer->resp = ETIMEDOUT;
339110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
340113584Ssimokawa			fw_xfer_done(xfer);
341110577Ssimokawa		}
342110577Ssimokawa	}
343111040Ssimokawa	splx(s);
344110577Ssimokawa}
345110577Ssimokawa
346121463Ssimokawa#define WATCHDOC_HZ 10
347110577Ssimokawastatic void
348110577Ssimokawafirewire_watchdog(void *arg)
349110577Ssimokawa{
350110577Ssimokawa	struct firewire_comm *fc;
351121463Ssimokawa	static int watchdoc_clock = 0;
352110577Ssimokawa
353110577Ssimokawa	fc = (struct firewire_comm *)arg;
354121463Ssimokawa
355121463Ssimokawa	/*
356121463Ssimokawa	 * At boot stage, the device interrupt is disabled and
357121463Ssimokawa	 * We encounter a timeout easily. To avoid this,
358121463Ssimokawa	 * ignore clock interrupt for a while.
359121463Ssimokawa	 */
360121463Ssimokawa	if (watchdoc_clock > WATCHDOC_HZ * 15) {
361121463Ssimokawa		firewire_xfer_timeout(fc);
362121463Ssimokawa		fc->timeout(fc);
363121463Ssimokawa	} else
364121463Ssimokawa		watchdoc_clock ++;
365121463Ssimokawa
366121463Ssimokawa	callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ,
367110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
368110577Ssimokawa}
369110577Ssimokawa
370103285Sikob/*
371103285Sikob * The attach routine.
372103285Sikob */
373103285Sikobstatic int
374118455Ssimokawafirewire_attach(device_t dev)
375103285Sikob{
376118455Ssimokawa	int unit;
377103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
378103285Sikob	device_t pa = device_get_parent(dev);
379103285Sikob	struct firewire_comm *fc;
380103285Sikob
381103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
382103285Sikob	sc->fc = fc;
383116978Ssimokawa	fc->status = FWBUSNOTREADY;
384103285Sikob
385118455Ssimokawa	unit = device_get_unit(dev);
386118455Ssimokawa	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
387103285Sikob
388118455Ssimokawa	fwdev_makedev(sc);
389118455Ssimokawa
390110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
391110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
392110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
393110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
394108853Ssimokawa
395110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
396110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
397110193Ssimokawa
398103285Sikob	/* Locate our children */
399103285Sikob	bus_generic_probe(dev);
400103285Sikob
401103285Sikob	/* launch attachement of the added children */
402103285Sikob	bus_generic_attach(dev);
403103285Sikob
404103285Sikob	/* bus_reset */
405103285Sikob	fc->ibr(fc);
406103285Sikob
407103285Sikob	return 0;
408103285Sikob}
409103285Sikob
410103285Sikob/*
411103285Sikob * Attach it as child.
412103285Sikob */
413103285Sikobstatic device_t
414103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
415103285Sikob{
416103285Sikob        device_t child;
417103285Sikob	struct firewire_softc *sc;
418103285Sikob
419103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
420103285Sikob	child = device_add_child(dev, name, unit);
421103285Sikob	if (child) {
422103285Sikob		device_set_ivars(child, sc->fc);
423103285Sikob		device_probe_and_attach(child);
424103285Sikob	}
425103285Sikob
426103285Sikob	return child;
427103285Sikob}
428106790Ssimokawa
429116978Ssimokawastatic int
430116978Ssimokawafirewire_resume(device_t dev)
431116978Ssimokawa{
432116978Ssimokawa	struct firewire_softc *sc;
433116978Ssimokawa
434116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
435116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
436116978Ssimokawa
437116978Ssimokawa	bus_generic_resume(dev);
438116978Ssimokawa
439116978Ssimokawa	return(0);
440116978Ssimokawa}
441116978Ssimokawa
442103285Sikob/*
443103285Sikob * Dettach it.
444103285Sikob */
445103285Sikobstatic int
446118455Ssimokawafirewire_detach(device_t dev)
447103285Sikob{
448103285Sikob	struct firewire_softc *sc;
449111074Ssimokawa	struct csrdir *csrd, *next;
450111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
451118455Ssimokawa	int err;
452103285Sikob
453103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
454118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
455118455Ssimokawa		return err;
456106790Ssimokawa
457118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
458118455Ssimokawa		return err;
459111078Ssimokawa
460111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
461111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
462111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
463111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
464111078Ssimokawa
465103285Sikob	/* XXX xfree_free and untimeout on all xfers */
466111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
467111078Ssimokawa							fwdev = fwdev_next) {
468111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
469111078Ssimokawa		free(fwdev, M_FW);
470111078Ssimokawa	}
471111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
472111074Ssimokawa		next = SLIST_NEXT(csrd, link);
473111074Ssimokawa		free(csrd, M_FW);
474111074Ssimokawa	}
475110195Ssimokawa	free(sc->fc->topology_map, M_FW);
476110195Ssimokawa	free(sc->fc->speed_map, M_FW);
477116376Ssimokawa	free(sc->fc->crom_src_buf, M_FW);
478103285Sikob	return(0);
479103285Sikob}
480103285Sikob#if 0
481103285Sikobstatic int
482103285Sikobfirewire_shutdown( device_t dev )
483103285Sikob{
484103285Sikob	return 0;
485103285Sikob}
486103285Sikob#endif
487106790Ssimokawa
488110577Ssimokawa
489110577Ssimokawastatic void
490110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
491110577Ssimokawa{
492110577Ssimokawa	struct fw_xfer *xfer;
493110577Ssimokawa
494110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
495110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
496111942Ssimokawa		xferq->queued --;
497110577Ssimokawa		xfer->resp = EAGAIN;
498113584Ssimokawa		fw_xfer_done(xfer);
499110577Ssimokawa	}
500110577Ssimokawa}
501110577Ssimokawa
502110798Ssimokawavoid
503110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
504110798Ssimokawa{
505110798Ssimokawa	int i;
506110798Ssimokawa
507110798Ssimokawa	fw_xferq_drain(fc->atq);
508110798Ssimokawa	fw_xferq_drain(fc->ats);
509110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
510110798Ssimokawa		fw_xferq_drain(fc->it[i]);
511110798Ssimokawa}
512110798Ssimokawa
513116376Ssimokawastatic void
514116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
515103285Sikob{
516103285Sikob	int i;
517103285Sikob
518103285Sikob	CSRARC(fc, STATE_CLEAR)
519103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
520103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
521103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
522103285Sikob
523103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
524103285Sikob	fc->irm = -1;
525103285Sikob
526103285Sikob	fc->max_node = -1;
527103285Sikob
528103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
529103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
530103285Sikob	}
531103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
532103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
533103285Sikob	CSRARC(fc, RESET_START) = 0;
534103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
535103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
536103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
537103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
538103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
539103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
540103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
541103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
542103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
543103285Sikob
544103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
545103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
546103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
547103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
548103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
549103285Sikob
550103285Sikob/* DV depend CSRs see blue book */
551103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
552103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
553103285Sikob
554103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
555103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
556116376Ssimokawa}
557113584Ssimokawa
558116376Ssimokawastatic void
559116376Ssimokawafw_init_crom(struct firewire_comm *fc)
560116376Ssimokawa{
561116376Ssimokawa	struct crom_src *src;
562116376Ssimokawa
563116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
564116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
565116376Ssimokawa	if (fc->crom_src_buf == NULL)
566116376Ssimokawa		return;
567116376Ssimokawa
568116376Ssimokawa	src = &fc->crom_src_buf->src;
569116376Ssimokawa	bzero(src, sizeof(struct crom_src));
570116376Ssimokawa
571116376Ssimokawa	/* BUS info sample */
572116376Ssimokawa	src->hdr.info_len = 4;
573116376Ssimokawa
574116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
575116376Ssimokawa
576116376Ssimokawa	src->businfo.irmc = 1;
577116376Ssimokawa	src->businfo.cmc = 1;
578116376Ssimokawa	src->businfo.isc = 1;
579116376Ssimokawa	src->businfo.bmc = 1;
580116376Ssimokawa	src->businfo.pmc = 0;
581116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
582116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
583116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
584117350Ssimokawa	src->businfo.generation = 1;
585116376Ssimokawa	src->businfo.link_spd = fc->speed;
586116376Ssimokawa
587116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
588116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
589116376Ssimokawa
590116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
591116376Ssimokawa
592116376Ssimokawa	fc->crom_src = src;
593116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
594116376Ssimokawa}
595116376Ssimokawa
596116376Ssimokawastatic void
597116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
598116376Ssimokawa{
599116376Ssimokawa	struct crom_src_buf *buf;
600116376Ssimokawa	struct crom_src *src;
601116376Ssimokawa	struct crom_chunk *root;
602116376Ssimokawa
603116376Ssimokawa	if (fc->crom_src_buf == NULL)
604116376Ssimokawa		fw_init_crom(fc);
605116376Ssimokawa
606116376Ssimokawa	buf =  fc->crom_src_buf;
607116376Ssimokawa	src = fc->crom_src;
608116376Ssimokawa	root = fc->crom_root;
609116376Ssimokawa
610116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
611116376Ssimokawa
612116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
613116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
614116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
615116376Ssimokawa	/* private company_id */
616116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
617116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
618116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
619116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
620116376Ssimokawa}
621116376Ssimokawa
622116376Ssimokawa/*
623116376Ssimokawa * Called after bus reset.
624116376Ssimokawa */
625116376Ssimokawavoid
626116376Ssimokawafw_busreset(struct firewire_comm *fc)
627116376Ssimokawa{
628116376Ssimokawa	struct firewire_dev_comm *fdc;
629117350Ssimokawa	struct crom_src *src;
630116376Ssimokawa	device_t *devlistp;
631117350Ssimokawa	void *newrom;
632116376Ssimokawa	int i, devcnt;
633116376Ssimokawa
634116376Ssimokawa	switch(fc->status){
635116376Ssimokawa	case FWBUSMGRELECT:
636116376Ssimokawa		callout_stop(&fc->bmr_callout);
637116376Ssimokawa		break;
638116376Ssimokawa	default:
639116376Ssimokawa		break;
640116376Ssimokawa	}
641116376Ssimokawa	fc->status = FWBUSRESET;
642116376Ssimokawa	fw_reset_csr(fc);
643116376Ssimokawa	fw_reset_crom(fc);
644116376Ssimokawa
645113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
646113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
647113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
648113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
649113584Ssimokawa				if (fdc->post_busreset != NULL)
650113584Ssimokawa					fdc->post_busreset(fdc);
651113584Ssimokawa			}
652113584Ssimokawa		free(devlistp, M_TEMP);
653113584Ssimokawa	}
654116376Ssimokawa
655117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
656117350Ssimokawa	src = &fc->crom_src_buf->src;
657117350Ssimokawa	crom_load(src, (u_int32_t *)newrom, CROMSIZE);
658117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
659117350Ssimokawa		/* bump generation and reload */
660117350Ssimokawa		src->businfo.generation ++;
661117350Ssimokawa		/* generation must be between 0x2 and 0xF */
662117350Ssimokawa		if (src->businfo.generation < 2)
663117350Ssimokawa			src->businfo.generation ++;
664117350Ssimokawa		crom_load(src, (u_int32_t *)newrom, CROMSIZE);
665117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
666117350Ssimokawa	}
667117350Ssimokawa	free(newrom, M_FW);
668103285Sikob}
669106790Ssimokawa
670103285Sikob/* Call once after reboot */
671106790Ssimokawavoid fw_init(struct firewire_comm *fc)
672103285Sikob{
673103285Sikob	int i;
674106543Ssimokawa	struct csrdir *csrd;
675106543Ssimokawa#ifdef FW_VMACCESS
676103285Sikob	struct fw_xfer *xfer;
677103285Sikob	struct fw_bind *fwb;
678106543Ssimokawa#endif
679103285Sikob
680103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
681103285Sikob
682103285Sikob	fc->arq->queued = 0;
683103285Sikob	fc->ars->queued = 0;
684103285Sikob	fc->atq->queued = 0;
685103285Sikob	fc->ats->queued = 0;
686103285Sikob
687103285Sikob	fc->arq->buf = NULL;
688103285Sikob	fc->ars->buf = NULL;
689103285Sikob	fc->atq->buf = NULL;
690103285Sikob	fc->ats->buf = NULL;
691103285Sikob
692113584Ssimokawa	fc->arq->flag = 0;
693113584Ssimokawa	fc->ars->flag = 0;
694113584Ssimokawa	fc->atq->flag = 0;
695113584Ssimokawa	fc->ats->flag = 0;
696103285Sikob
697103285Sikob	STAILQ_INIT(&fc->atq->q);
698103285Sikob	STAILQ_INIT(&fc->ats->q);
699103285Sikob
700103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
701103285Sikob		fc->it[i]->queued = 0;
702103285Sikob		fc->ir[i]->queued = 0;
703103285Sikob
704103285Sikob		fc->it[i]->start = NULL;
705103285Sikob		fc->ir[i]->start = NULL;
706103285Sikob
707103285Sikob		fc->it[i]->buf = NULL;
708103285Sikob		fc->ir[i]->buf = NULL;
709103285Sikob
710103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
711103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
712103285Sikob
713103285Sikob		STAILQ_INIT(&fc->it[i]->q);
714103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
715103285Sikob
716103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
717103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
718103285Sikob	}
719103285Sikob
720103285Sikob	fc->arq->maxq = FWMAXQUEUE;
721103285Sikob	fc->ars->maxq = FWMAXQUEUE;
722103285Sikob	fc->atq->maxq = FWMAXQUEUE;
723103285Sikob	fc->ats->maxq = FWMAXQUEUE;
724103285Sikob
725103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
726103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
727103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
728103285Sikob	}
729103285Sikob/* Initialize csr registers */
730103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
731103285Sikob				sizeof(struct fw_topology_map),
732110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
733103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
734103285Sikob				sizeof(struct fw_speed_map),
735110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
736103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
737103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
738103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
739103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
740103285Sikob
741110193Ssimokawa	STAILQ_INIT(&fc->devices);
742103285Sikob
743103285Sikob/* Initialize csr ROM work space */
744103285Sikob	SLIST_INIT(&fc->ongocsr);
745103285Sikob	SLIST_INIT(&fc->csrfree);
746103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
747110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
748103285Sikob		if(csrd == NULL) break;
749103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
750103285Sikob	}
751103285Sikob
752103285Sikob/* Initialize Async handlers */
753103285Sikob	STAILQ_INIT(&fc->binds);
754103285Sikob	for( i = 0 ; i < 0x40 ; i++){
755103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
756103285Sikob	}
757103285Sikob
758103285Sikob/* DV depend CSRs see blue book */
759103285Sikob#if 0
760103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
761103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
762103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
763103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
764103285Sikob	}
765103285Sikob
766103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
767103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
768103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
769103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
770103285Sikob	}
771103285Sikob#endif
772103285Sikob
773116376Ssimokawa	fc->crom_src_buf = NULL;
774103285Sikob
775106543Ssimokawa#ifdef FW_VMACCESS
776103285Sikob	xfer = fw_xfer_alloc();
777103285Sikob	if(xfer == NULL) return;
778103285Sikob
779110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
780103285Sikob	if(fwb == NULL){
781103285Sikob		fw_xfer_free(xfer);
782103285Sikob	}
783103285Sikob	xfer->act.hand = fw_vmaccess;
784103285Sikob	xfer->fc = fc;
785103285Sikob	xfer->sc = NULL;
786103285Sikob
787103285Sikob	fwb->start_hi = 0x2;
788103285Sikob	fwb->start_lo = 0;
789103285Sikob	fwb->addrlen = 0xffffffff;
790103285Sikob	fwb->xfer = xfer;
791103285Sikob	fw_bindadd(fc, fwb);
792106543Ssimokawa#endif
793103285Sikob}
794106790Ssimokawa
795120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
796120660Ssimokawa    ((fwb)->end < (addr))?1:0)
797120660Ssimokawa
798103285Sikob/*
799103285Sikob * To lookup binded process from IEEE1394 address.
800103285Sikob */
801106813Ssimokawastruct fw_bind *
802120660Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int16_t dest_hi, u_int32_t dest_lo)
803103285Sikob{
804120660Ssimokawa	u_int64_t addr;
805103285Sikob	struct fw_bind *tfw;
806120660Ssimokawa
807120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
808120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
809120660Ssimokawa		if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0)
810103285Sikob			return(tfw);
811103285Sikob	return(NULL);
812103285Sikob}
813106790Ssimokawa
814103285Sikob/*
815103285Sikob * To bind IEEE1394 address block to process.
816103285Sikob */
817106790Ssimokawaint
818106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
819103285Sikob{
820120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
821120660Ssimokawa
822120660Ssimokawa	if (fwb->start > fwb->end) {
823120660Ssimokawa		printf("%s: invalid range\n", __FUNCTION__);
824120660Ssimokawa		return EINVAL;
825120660Ssimokawa	}
826120660Ssimokawa
827120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
828120660Ssimokawa		if (fwb->end < tfw->start)
829120660Ssimokawa			break;
830120660Ssimokawa		prev = tfw;
831120660Ssimokawa	}
832120660Ssimokawa	if (prev == NULL) {
833103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
834103285Sikob		goto out;
835103285Sikob	}
836120660Ssimokawa	if (prev->end < fwb->start) {
837120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
838103285Sikob		goto out;
839103285Sikob	}
840120660Ssimokawa
841120660Ssimokawa	printf("%s: bind failed\n", __FUNCTION__);
842120660Ssimokawa	return (EBUSY);
843120660Ssimokawa
844103285Sikobout:
845120660Ssimokawa	if (fwb->act_type == FWACT_CH)
846113584Ssimokawa		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
847120660Ssimokawa	return (0);
848103285Sikob}
849103285Sikob
850103285Sikob/*
851103285Sikob * To free IEEE1394 address block.
852103285Sikob */
853106790Ssimokawaint
854106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
855103285Sikob{
856120660Ssimokawa#if 0
857120660Ssimokawa	struct fw_xfer *xfer, *next;
858120660Ssimokawa#endif
859120660Ssimokawa	struct fw_bind *tfw;
860103285Sikob	int s;
861103285Sikob
862103285Sikob	s = splfw();
863120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
864120660Ssimokawa		if (tfw == fwb) {
865120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
866120660Ssimokawa			goto found;
867120660Ssimokawa		}
868120660Ssimokawa
869120660Ssimokawa	printf("%s: no such bind\n", __FUNCTION__);
870120660Ssimokawa	splx(s);
871120660Ssimokawa	return (1);
872120660Ssimokawafound:
873120660Ssimokawa#if 0
874113584Ssimokawa	/* shall we do this? */
875113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
876113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
877113584Ssimokawa		fw_xfer_free(xfer);
878113584Ssimokawa	}
879113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
880120660Ssimokawa#endif
881113584Ssimokawa
882103285Sikob	splx(s);
883103285Sikob	return 0;
884103285Sikob}
885103285Sikob
886103285Sikob/*
887103285Sikob * To free transaction label.
888103285Sikob */
889106790Ssimokawastatic void
890106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
891103285Sikob{
892103285Sikob	struct tlabel *tl;
893103285Sikob	int s = splfw();
894103285Sikob
895103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
896103285Sikob		tl = STAILQ_NEXT(tl, link)){
897103285Sikob		if(tl->xfer == xfer){
898103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
899110195Ssimokawa			free(tl, M_FW);
900103285Sikob			splx(s);
901103285Sikob			return;
902103285Sikob		}
903103285Sikob	}
904103285Sikob	splx(s);
905103285Sikob	return;
906103285Sikob}
907106790Ssimokawa
908103285Sikob/*
909103285Sikob * To obtain XFER structure by transaction label.
910103285Sikob */
911106790Ssimokawastatic struct fw_xfer *
912106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
913103285Sikob{
914103285Sikob	struct fw_xfer *xfer;
915103285Sikob	struct tlabel *tl;
916103285Sikob	int s = splfw();
917103285Sikob
918103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
919103285Sikob		tl = STAILQ_NEXT(tl, link)){
920120660Ssimokawa		if(tl->xfer->send.hdr.mode.hdr.dst == node){
921103285Sikob			xfer = tl->xfer;
922103285Sikob			splx(s);
923110577Ssimokawa			if (firewire_debug > 2)
924110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
925103285Sikob			return(xfer);
926103285Sikob		}
927103285Sikob	}
928110577Ssimokawa	if (firewire_debug > 1)
929110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
930103285Sikob	splx(s);
931103285Sikob	return(NULL);
932103285Sikob}
933106790Ssimokawa
934103285Sikob/*
935103285Sikob * To allocate IEEE1394 XFER structure.
936103285Sikob */
937106790Ssimokawastruct fw_xfer *
938110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
939103285Sikob{
940103285Sikob	struct fw_xfer *xfer;
941106790Ssimokawa
942110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
943106790Ssimokawa	if (xfer == NULL)
944106790Ssimokawa		return xfer;
945106790Ssimokawa
946110269Ssimokawa	xfer->malloc = type;
947106790Ssimokawa
948103285Sikob	return xfer;
949103285Sikob}
950106790Ssimokawa
951113584Ssimokawastruct fw_xfer *
952113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
953113584Ssimokawa{
954113584Ssimokawa	struct fw_xfer *xfer;
955113584Ssimokawa
956113584Ssimokawa	xfer = fw_xfer_alloc(type);
957120660Ssimokawa	xfer->send.pay_len = send_len;
958120660Ssimokawa	xfer->recv.pay_len = recv_len;
959113584Ssimokawa	if (xfer == NULL)
960113584Ssimokawa		return(NULL);
961120660Ssimokawa	if (send_len > 0) {
962120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
963120660Ssimokawa		if (xfer->send.payload == NULL) {
964113584Ssimokawa			fw_xfer_free(xfer);
965113584Ssimokawa			return(NULL);
966113584Ssimokawa		}
967113584Ssimokawa	}
968120660Ssimokawa	if (recv_len > 0) {
969120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
970120660Ssimokawa		if (xfer->recv.payload == NULL) {
971120660Ssimokawa			if (xfer->send.payload != NULL)
972120660Ssimokawa				free(xfer->send.payload, type);
973113584Ssimokawa			fw_xfer_free(xfer);
974113584Ssimokawa			return(NULL);
975113584Ssimokawa		}
976113584Ssimokawa	}
977113584Ssimokawa	return(xfer);
978113584Ssimokawa}
979113584Ssimokawa
980103285Sikob/*
981103285Sikob * IEEE1394 XFER post process.
982103285Sikob */
983103285Sikobvoid
984103285Sikobfw_xfer_done(struct fw_xfer *xfer)
985103285Sikob{
986117716Ssimokawa	if (xfer->act.hand == NULL) {
987117716Ssimokawa		printf("act.hand == NULL\n");
988103285Sikob		return;
989117716Ssimokawa	}
990103285Sikob
991121505Ssimokawa	if (xfer->fc == NULL)
992121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
993121505Ssimokawa
994124145Ssimokawa	xfer->act.hand(xfer);
995103285Sikob}
996103285Sikob
997106790Ssimokawavoid
998113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
999103285Sikob{
1000103285Sikob	int s;
1001113584Ssimokawa
1002103285Sikob	if(xfer == NULL ) return;
1003103285Sikob	if(xfer->state == FWXF_INQ){
1004103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1005103285Sikob		s = splfw();
1006103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1007103285Sikob		xfer->q->queued --;
1008103285Sikob		splx(s);
1009103285Sikob	}
1010113584Ssimokawa	if (xfer->fc != NULL) {
1011114729Ssimokawa#if 1
1012113584Ssimokawa		if(xfer->state == FWXF_START)
1013114729Ssimokawa			/*
1014114729Ssimokawa			 * This could happen if:
1015114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1016114729Ssimokawa			 *  2. firewire_watch() is called.
1017114729Ssimokawa			 */
1018114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1019103285Sikob#endif
1020113584Ssimokawa		fw_tl_free(xfer->fc, xfer);
1021103285Sikob	}
1022113584Ssimokawa	xfer->state = FWXF_INIT;
1023113584Ssimokawa	xfer->resp = 0;
1024113584Ssimokawa	xfer->retry = 0;
1025113584Ssimokawa}
1026113584Ssimokawa/*
1027113584Ssimokawa * To free IEEE1394 XFER structure.
1028113584Ssimokawa */
1029113584Ssimokawavoid
1030120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer)
1031113584Ssimokawa{
1032120660Ssimokawa	if (xfer == NULL) {
1033120660Ssimokawa		printf("%s: xfer == NULL\n", __FUNCTION__);
1034120660Ssimokawa		return;
1035120660Ssimokawa	}
1036113584Ssimokawa	fw_xfer_unload(xfer);
1037120660Ssimokawa	if(xfer->send.payload != NULL){
1038120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1039103285Sikob	}
1040120660Ssimokawa	if(xfer->recv.payload != NULL){
1041120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1042103285Sikob	}
1043110269Ssimokawa	free(xfer, xfer->malloc);
1044103285Sikob}
1045103285Sikob
1046120660Ssimokawavoid
1047120660Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1048120660Ssimokawa{
1049120660Ssimokawa	if (xfer == NULL) {
1050120660Ssimokawa		printf("%s: xfer == NULL\n", __FUNCTION__);
1051120660Ssimokawa		return;
1052120660Ssimokawa	}
1053120660Ssimokawa	fw_xfer_unload(xfer);
1054120660Ssimokawa	free(xfer, xfer->malloc);
1055120660Ssimokawa}
1056120660Ssimokawa
1057120660Ssimokawavoid
1058110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1059103285Sikob{
1060103285Sikob#if 0
1061110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1062103285Sikob				xfer->state, xfer->resp);
1063103285Sikob#endif
1064103285Sikob	fw_xfer_free(xfer);
1065103285Sikob}
1066103285Sikob
1067103285Sikob/*
1068103285Sikob * To configure PHY.
1069103285Sikob */
1070103285Sikobstatic void
1071103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1072103285Sikob{
1073103285Sikob	struct fw_xfer *xfer;
1074103285Sikob	struct fw_pkt *fp;
1075103285Sikob
1076103285Sikob	fc->status = FWBUSPHYCONF;
1077103285Sikob
1078120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1079113584Ssimokawa	if (xfer == NULL)
1080113584Ssimokawa		return;
1081103285Sikob	xfer->fc = fc;
1082103285Sikob	xfer->retry_req = fw_asybusy;
1083110072Ssimokawa	xfer->act.hand = fw_asy_callback_free;
1084103285Sikob
1085120660Ssimokawa	fp = &xfer->send.hdr;
1086103285Sikob	fp->mode.ld[1] = 0;
1087103285Sikob	if (root_node >= 0)
1088113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1089103285Sikob	if (gap_count >= 0)
1090113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1091103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1092103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1093103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1094103285Sikob
1095107653Ssimokawa	if (firewire_debug)
1096107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1097103285Sikob						root_node, gap_count);
1098103285Sikob	fw_asyreq(fc, -1, xfer);
1099103285Sikob}
1100103285Sikob
1101103285Sikob#if 0
1102103285Sikob/*
1103103285Sikob * Dump self ID.
1104103285Sikob */
1105103285Sikobstatic void
1106103285Sikobfw_print_sid(u_int32_t sid)
1107103285Sikob{
1108103285Sikob	union fw_self_id *s;
1109103285Sikob	s = (union fw_self_id *) &sid;
1110103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1111103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1112103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1113103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1114103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1115103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1116103285Sikob}
1117103285Sikob#endif
1118103285Sikob
1119103285Sikob/*
1120103285Sikob * To receive self ID.
1121103285Sikob */
1122113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len)
1123103285Sikob{
1124113584Ssimokawa	u_int32_t *p;
1125103285Sikob	union fw_self_id *self_id;
1126103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1127103285Sikob
1128103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1129103285Sikob	fc->status = FWBUSINIT;
1130103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1131103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1132103285Sikob	fc->status = FWBUSCYMELECT;
1133103285Sikob	fc->topology_map->crc_len = 2;
1134103285Sikob	fc->topology_map->generation ++;
1135103285Sikob	fc->topology_map->self_id_count = 0;
1136103285Sikob	fc->topology_map->node_count = 0;
1137103285Sikob	fc->speed_map->generation ++;
1138103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1139103285Sikob	self_id = &fc->topology_map->self_id[0];
1140103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1141103285Sikob		if (sid[1] != ~sid[0]) {
1142103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1143103285Sikob			sid += 2;
1144103285Sikob			continue;
1145103285Sikob		}
1146103285Sikob		*self_id = *((union fw_self_id *)sid);
1147103285Sikob		fc->topology_map->crc_len++;
1148103285Sikob		if(self_id->p0.sequel == 0){
1149103285Sikob			fc->topology_map->node_count ++;
1150103285Sikob			c_port = 0;
1151103285Sikob#if 0
1152103285Sikob			fw_print_sid(sid[0]);
1153103285Sikob#endif
1154103285Sikob			node = self_id->p0.phy_id;
1155103285Sikob			if(fc->max_node < node){
1156103285Sikob				fc->max_node = self_id->p0.phy_id;
1157103285Sikob			}
1158103285Sikob			/* XXX I'm not sure this is the right speed_map */
1159103285Sikob			fc->speed_map->speed[node][node]
1160103285Sikob					= self_id->p0.phy_speed;
1161103285Sikob			for (j = 0; j < node; j ++) {
1162103285Sikob				fc->speed_map->speed[j][node]
1163103285Sikob					= fc->speed_map->speed[node][j]
1164103285Sikob					= min(fc->speed_map->speed[j][j],
1165103285Sikob							self_id->p0.phy_speed);
1166103285Sikob			}
1167103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1168103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1169103285Sikob				fc->irm = self_id->p0.phy_id;
1170103285Sikob			}
1171103285Sikob			if(self_id->p0.port0 >= 0x2){
1172103285Sikob				c_port++;
1173103285Sikob			}
1174103285Sikob			if(self_id->p0.port1 >= 0x2){
1175103285Sikob				c_port++;
1176103285Sikob			}
1177103285Sikob			if(self_id->p0.port2 >= 0x2){
1178103285Sikob				c_port++;
1179103285Sikob			}
1180103285Sikob		}
1181103285Sikob		if(c_port > 2){
1182103285Sikob			i_branch += (c_port - 2);
1183103285Sikob		}
1184103285Sikob		sid += 2;
1185103285Sikob		self_id++;
1186103285Sikob		fc->topology_map->self_id_count ++;
1187103285Sikob	}
1188108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1189103285Sikob	/* CRC */
1190103285Sikob	fc->topology_map->crc = fw_crc16(
1191103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1192103285Sikob			fc->topology_map->crc_len * 4);
1193103285Sikob	fc->speed_map->crc = fw_crc16(
1194103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1195103285Sikob			fc->speed_map->crc_len * 4);
1196103285Sikob	/* byteswap and copy to CSR */
1197103285Sikob	p = (u_int32_t *)fc->topology_map;
1198103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1199103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1200103285Sikob	p = (u_int32_t *)fc->speed_map;
1201103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1202103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1203103285Sikob	/* don't byte-swap u_int8_t array */
1204103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1205103285Sikob
1206103285Sikob	fc->max_hop = fc->max_node - i_branch;
1207103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1208103285Sikob
1209103285Sikob	if(fc->irm == -1 ){
1210103285Sikob		printf(", Not found IRM capable node");
1211103285Sikob	}else{
1212103285Sikob		printf(", cable IRM = %d", fc->irm);
1213103285Sikob		if (fc->irm == fc->nodeid)
1214110016Ssimokawa			printf(" (me)");
1215103285Sikob	}
1216110016Ssimokawa	printf("\n");
1217103285Sikob
1218109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1219114909Ssimokawa		if (fc->irm == fc->nodeid) {
1220103285Sikob			fc->status = FWBUSMGRDONE;
1221103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1222114909Ssimokawa			fw_bmr(fc);
1223109736Ssimokawa		} else {
1224103285Sikob			fc->status = FWBUSMGRELECT;
1225110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1226110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1227103285Sikob		}
1228114909Ssimokawa	} else
1229103285Sikob		fc->status = FWBUSMGRDONE;
1230114909Ssimokawa
1231108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1232108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1233103285Sikob}
1234106790Ssimokawa
1235103285Sikob/*
1236103285Sikob * To probe devices on the IEEE1394 bus.
1237103285Sikob */
1238106790Ssimokawastatic void
1239106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1240103285Sikob{
1241103285Sikob	int s;
1242120850Ssimokawa	struct fw_device *fwdev;
1243103285Sikob
1244103285Sikob	s = splfw();
1245103285Sikob	fc->status = FWBUSEXPLORE;
1246103285Sikob	fc->retry_count = 0;
1247103285Sikob
1248120850Ssimokawa	/* Invalidate all devices, just after bus reset. */
1249120850Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1250110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1251103285Sikob			fwdev->status = FWDEVINVAL;
1252103285Sikob			fwdev->rcnt = 0;
1253103285Sikob		}
1254120850Ssimokawa
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;
1310117350Ssimokawa			fwdev->status = FWDEVINIT;
1311117350Ssimokawa			fc->ongodev = fwdev;
1312103285Sikob			fc->ongoaddr = CSRROMOFF;
1313117350Ssimokawa			addr = 0xf0000000 | fc->ongoaddr;
1314117350Ssimokawa			goto dorequest;
1315103285Sikob		}
1316115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1317115787Ssimokawa							M_NOWAIT | M_ZERO);
1318103285Sikob		if(fwdev == NULL)
1319103285Sikob			return;
1320106810Ssimokawa		fwdev->fc = fc;
1321103285Sikob		fwdev->rommax = 0;
1322103285Sikob		fwdev->dst = fc->ongonode;
1323103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1324103285Sikob		fwdev->status = FWDEVINIT;
1325103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1326103285Sikob
1327110179Ssimokawa		pfwdev = NULL;
1328110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1329110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1330110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1331110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1332110179Ssimokawa				break;
1333110179Ssimokawa			pfwdev = tfwdev;
1334103285Sikob		}
1335110179Ssimokawa		if (pfwdev == NULL)
1336110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1337110179Ssimokawa		else
1338110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1339103285Sikob
1340108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1341107653Ssimokawa			linkspeed[fwdev->speed],
1342107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1343103285Sikob
1344103285Sikob		fc->ongodev = fwdev;
1345103285Sikob		fc->ongoaddr = CSRROMOFF;
1346103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1347103285Sikob	}else{
1348103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1349103285Sikob	}
1350117350Ssimokawadorequest:
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
1357120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1358103285Sikob	if(xfer == NULL){
1359103285Sikob		goto done;
1360103285Sikob	}
1361120660Ssimokawa	xfer->send.spd = 0;
1362120660Ssimokawa	fp = &xfer->send.hdr;
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;
1368120660Ssimokawa	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1369113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1370103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1371103285Sikob
1372110577Ssimokawa	if (firewire_debug)
1373110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1374110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1375103285Sikob	err = fw_asyreq(fc, -1, xfer);
1376103285Sikob	if(err){
1377103285Sikob		fw_xfer_free( xfer);
1378103285Sikob		return;
1379103285Sikob	}
1380103285Sikob#endif
1381103285Sikob	return;
1382103285Sikobdone:
1383103285Sikob	/* fw_attach_devs */
1384103285Sikob	fc->status = FWBUSEXPDONE;
1385107653Ssimokawa	if (firewire_debug)
1386107653Ssimokawa		printf("bus_explore done\n");
1387103285Sikob	fw_attach_dev(fc);
1388103285Sikob	return;
1389103285Sikob
1390103285Sikob}
1391106790Ssimokawa
1392103285Sikob/* Portable Async. request read quad */
1393106790Ssimokawastruct fw_xfer *
1394106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1395105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1396105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1397103285Sikob{
1398103285Sikob	struct fw_xfer *xfer;
1399103285Sikob	struct fw_pkt *fp;
1400103285Sikob	int err;
1401103285Sikob
1402120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1403113584Ssimokawa	if (xfer == NULL)
1404103285Sikob		return NULL;
1405113584Ssimokawa
1406120660Ssimokawa	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1407120660Ssimokawa	fp = &xfer->send.hdr;
1408113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1409103285Sikob	if(tl & FWP_TL_VALID){
1410103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1411103285Sikob	}else{
1412103285Sikob		fp->mode.rreqq.tlrt = 0;
1413103285Sikob	}
1414103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1415103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1416103285Sikob	fp->mode.rreqq.pri = 0;
1417103285Sikob	fp->mode.rreqq.src = 0;
1418120660Ssimokawa	fp->mode.rreqq.dst = addr_hi >> 16;
1419113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1420103285Sikob	xfer->act.hand = hand;
1421103285Sikob
1422103285Sikob	err = fw_asyreq(fc, -1, xfer);
1423103285Sikob	if(err){
1424103285Sikob		fw_xfer_free( xfer);
1425103285Sikob		return NULL;
1426103285Sikob	}
1427103285Sikob	return xfer;
1428103285Sikob}
1429106790Ssimokawa
1430103285Sikob/*
1431103285Sikob * Callback for the IEEE1394 bus information collection.
1432103285Sikob */
1433106790Ssimokawastatic void
1434106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1435106790Ssimokawa{
1436103285Sikob	struct firewire_comm *fc;
1437103285Sikob	struct fw_pkt *sfp,*rfp;
1438103285Sikob	struct csrhdr *chdr;
1439103285Sikob	struct csrdir *csrd;
1440103285Sikob	struct csrreg *csrreg;
1441103285Sikob	u_int32_t offset;
1442103285Sikob
1443103285Sikob
1444110577Ssimokawa	if(xfer == NULL) {
1445110577Ssimokawa		printf("xfer == NULL\n");
1446110577Ssimokawa		return;
1447110577Ssimokawa	}
1448103285Sikob	fc = xfer->fc;
1449110577Ssimokawa
1450110577Ssimokawa	if (firewire_debug)
1451110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1452110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1453110577Ssimokawa
1454103285Sikob	if(xfer->resp != 0){
1455110577Ssimokawa		printf("node%d: resp=%d addr=0x%x\n",
1456110577Ssimokawa			fc->ongonode, xfer->resp, fc->ongoaddr);
1457114284Ssimokawa		goto errnode;
1458103285Sikob	}
1459103285Sikob
1460120660Ssimokawa	sfp = &xfer->send.hdr;
1461120660Ssimokawa	rfp = &xfer->recv.hdr;
1462103285Sikob#if 0
1463103285Sikob	{
1464103285Sikob		u_int32_t *qld;
1465103285Sikob		int i;
1466103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1467103285Sikob		printf("len:%d\n", xfer->recv.len);
1468103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1469113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1470103285Sikob			if((i % 16) == 15) printf("\n");
1471103285Sikob		}
1472103285Sikob		if((i % 16) != 15) printf("\n");
1473103285Sikob	}
1474103285Sikob#endif
1475103285Sikob	if(fc->ongodev == NULL){
1476113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1477103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1478103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1479110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */
1480103285Sikob			if(chdr->info_len == 1){
1481110577Ssimokawa				if (firewire_debug)
1482110577Ssimokawa					printf("node%d: minimal config\n",
1483110577Ssimokawa								fc->ongonode);
1484103285Sikob				goto nextnode;
1485103285Sikob			}else{
1486103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1487103285Sikob			}
1488113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1489103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1490103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1491113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1492103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1493110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1494110577Ssimokawa				if (firewire_debug)
1495110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1496110577Ssimokawa							fc->ongonode);
1497103285Sikob				goto nextnode;
1498110577Ssimokawa			}
1499103285Sikob			fc->ongoaddr = CSRROMOFF;
1500103285Sikob		}
1501103285Sikob	}else{
1502117350Ssimokawa		if (fc->ongoaddr == CSRROMOFF &&
1503117350Ssimokawa		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1504117350Ssimokawa			fc->ongodev->status = FWDEVATTACHED;
1505117350Ssimokawa			goto nextnode;
1506117350Ssimokawa		}
1507103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1508103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1509103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1510103285Sikob		}
1511103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1512103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1513103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1514103285Sikob			offset = CSRROMOFF;
1515103285Sikob		}else{
1516103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1517103285Sikob			offset = csrd->off;
1518103285Sikob		}
1519103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1520103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1521103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1522103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1523103285Sikob				if(csrd == NULL){
1524103285Sikob					goto nextnode;
1525103285Sikob				}else{
1526103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1527103285Sikob					fc->ongoaddr += csrreg->val * 4;
1528103285Sikob					csrd->off = fc->ongoaddr;
1529103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1530103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1531103285Sikob					goto nextaddr;
1532103285Sikob				}
1533103285Sikob			}
1534103285Sikob		}
1535103285Sikob		fc->ongoaddr += 4;
1536103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1537103285Sikob				(fc->ongodev->rommax < 0x414)){
1538103285Sikob			if(fc->ongodev->rommax <= 0x414){
1539103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1540103285Sikob				if(csrd == NULL) goto nextnode;
1541103285Sikob				csrd->off = fc->ongoaddr;
1542103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1543103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1544103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1545103285Sikob			}
1546103285Sikob			goto nextaddr;
1547103285Sikob		}
1548103285Sikob
1549103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1550103285Sikob			if(csrd == NULL){
1551103285Sikob				goto nextnode;
1552103285Sikob			};
1553103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1554103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1555103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1556103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1557103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1558103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1559103285Sikob				offset = CSRROMOFF;
1560103285Sikob			}else{
1561103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1562103285Sikob				offset = csrd->off;
1563103285Sikob			}
1564103285Sikob		}
1565103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1566103285Sikob			goto nextnode;
1567103285Sikob		}
1568103285Sikob	}
1569103285Sikobnextaddr:
1570103285Sikob	fw_xfer_free( xfer);
1571103285Sikob	fw_bus_explore(fc);
1572103285Sikob	return;
1573114284Ssimokawaerrnode:
1574114284Ssimokawa	fc->retry_count++;
1575114284Ssimokawa	if (fc->ongodev != NULL)
1576114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1577103285Sikobnextnode:
1578103285Sikob	fw_xfer_free( xfer);
1579103285Sikob	fc->ongonode++;
1580103285Sikob/* housekeeping work space */
1581103285Sikob	fc->ongoaddr = CSRROMOFF;
1582103285Sikob	fc->ongodev = NULL;
1583103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1584103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1585103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1586103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1587103285Sikob	}
1588103285Sikob	fw_bus_explore(fc);
1589103285Sikob	return;
1590103285Sikob}
1591103285Sikob
1592103285Sikob/*
1593103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1594103285Sikob */
1595106815Ssimokawastatic void
1596106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1597103285Sikob{
1598120850Ssimokawa	struct fw_device *fwdev, *next;
1599103285Sikob	int i, err;
1600103285Sikob	device_t *devlistp;
1601103285Sikob	int devcnt;
1602103285Sikob	struct firewire_dev_comm *fdc;
1603103285Sikob
1604120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1605120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1606120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1607114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1608120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1609120850Ssimokawa			fwdev->rcnt ++;
1610120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1611120850Ssimokawa				/*
1612120850Ssimokawa				 * Remove devices which have not been seen
1613120850Ssimokawa				 * for a while.
1614120850Ssimokawa				 */
1615120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1616120850Ssimokawa				    link);
1617120850Ssimokawa				free(fwdev, M_FW);
1618120850Ssimokawa			}
1619120850Ssimokawa		}
1620120850Ssimokawa	}
1621103285Sikob
1622108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1623103285Sikob	if( err != 0 )
1624103285Sikob		return;
1625103285Sikob	for( i = 0 ; i < devcnt ; i++){
1626103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1627103285Sikob			fdc = device_get_softc(devlistp[i]);
1628103285Sikob			if (fdc->post_explore != NULL)
1629103285Sikob				fdc->post_explore(fdc);
1630103285Sikob		}
1631103285Sikob	}
1632103285Sikob	free(devlistp, M_TEMP);
1633103285Sikob
1634103285Sikob	if (fc->retry_count > 0) {
1635111040Ssimokawa		printf("probe failed for %d node\n", fc->retry_count);
1636111040Ssimokawa#if 0
1637110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1638110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1639111040Ssimokawa#endif
1640103285Sikob	}
1641103285Sikob	return;
1642103285Sikob}
1643106815Ssimokawa
1644103285Sikob/*
1645103285Sikob * To allocate uniq transaction label.
1646103285Sikob */
1647106815Ssimokawastatic int
1648106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1649103285Sikob{
1650103285Sikob	u_int i;
1651103285Sikob	struct tlabel *tl, *tmptl;
1652103285Sikob	int s;
1653103285Sikob	static u_int32_t label = 0;
1654103285Sikob
1655103285Sikob	s = splfw();
1656103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1657103285Sikob		label = (label + 1) & 0x3f;
1658103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1659103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1660120660Ssimokawa			if (tmptl->xfer->send.hdr.mode.hdr.dst ==
1661120660Ssimokawa			    xfer->send.hdr.mode.hdr.dst)
1662120660Ssimokawa				break;
1663103285Sikob		}
1664103285Sikob		if(tmptl == NULL) {
1665110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1666103285Sikob			if (tl == NULL) {
1667103285Sikob				splx(s);
1668103285Sikob				return (-1);
1669103285Sikob			}
1670103285Sikob			tl->xfer = xfer;
1671103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1672103285Sikob			splx(s);
1673110577Ssimokawa			if (firewire_debug > 1)
1674110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1675120660Ssimokawa				    xfer->send.hdr.mode.hdr.dst, label);
1676103285Sikob			return(label);
1677103285Sikob		}
1678103285Sikob	}
1679103285Sikob	splx(s);
1680103285Sikob
1681103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1682103285Sikob	return(-1);
1683103285Sikob}
1684106815Ssimokawa
1685113584Ssimokawastatic void
1686120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1687113584Ssimokawa{
1688120660Ssimokawa	struct fw_pkt *pkt;
1689120660Ssimokawa	u_char *p;
1690120660Ssimokawa	struct tcode_info *tinfo;
1691120660Ssimokawa	u_int res, i, len, plen;
1692113584Ssimokawa
1693120660Ssimokawa	rb->xfer->recv.spd -= rb->spd;
1694120660Ssimokawa
1695120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1696120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1697120660Ssimokawa
1698120660Ssimokawa	/* Copy header */
1699120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1700120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1701120660Ssimokawa	(u_char *)rb->vec->iov_base += tinfo->hdr_len;
1702120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1703120660Ssimokawa
1704120660Ssimokawa	/* Copy payload */
1705120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1706120660Ssimokawa	res = rb->xfer->recv.pay_len;
1707120660Ssimokawa
1708120660Ssimokawa	/* special handling for RRESQ */
1709120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1710120660Ssimokawa	    p != NULL && res >= sizeof(u_int32_t)) {
1711120660Ssimokawa		*(u_int32_t *)p = pkt->mode.rresq.data;
1712120660Ssimokawa		rb->xfer->recv.pay_len = sizeof(u_int32_t);
1713120660Ssimokawa		return;
1714120660Ssimokawa	}
1715120660Ssimokawa
1716120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1717120660Ssimokawa		return;
1718120660Ssimokawa
1719120660Ssimokawa	plen = pkt->mode.rresb.len;
1720120660Ssimokawa
1721120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1722120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1723113584Ssimokawa		if (res < len) {
1724113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1725120660Ssimokawa			    rb->xfer->recv.pay_len, len - res);
1726113584Ssimokawa			len = res;
1727113584Ssimokawa		}
1728120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1729113584Ssimokawa		p += len;
1730113584Ssimokawa		res -= len;
1731120660Ssimokawa		plen -= len;
1732120660Ssimokawa		if (res == 0 || plen == 0)
1733113584Ssimokawa			break;
1734113584Ssimokawa	}
1735120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1736120660Ssimokawa
1737113584Ssimokawa}
1738113584Ssimokawa
1739103285Sikob/*
1740103285Sikob * Generic packet receving process.
1741103285Sikob */
1742106815Ssimokawavoid
1743120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1744103285Sikob{
1745103285Sikob	struct fw_pkt *fp, *resfp;
1746103285Sikob	struct fw_bind *bind;
1747113584Ssimokawa	int tcode, s;
1748113584Ssimokawa	int i, len, oldstate;
1749103285Sikob#if 0
1750103285Sikob	{
1751103285Sikob		u_int32_t *qld;
1752103285Sikob		int i;
1753103285Sikob		qld = (u_int32_t *)buf;
1754103285Sikob		printf("spd %d len:%d\n", spd, len);
1755103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1756103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1757103285Sikob			if((i % 16) == 15) printf("\n");
1758103285Sikob		}
1759103285Sikob		if((i % 16) != 15) printf("\n");
1760103285Sikob	}
1761103285Sikob#endif
1762120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1763113584Ssimokawa	tcode = fp->mode.common.tcode;
1764113584Ssimokawa	switch (tcode) {
1765103285Sikob	case FWTCODE_WRES:
1766103285Sikob	case FWTCODE_RRESQ:
1767103285Sikob	case FWTCODE_RRESB:
1768103285Sikob	case FWTCODE_LRES:
1769120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1770103285Sikob					fp->mode.hdr.tlrt >> 2);
1771120660Ssimokawa		if(rb->xfer == NULL) {
1772103285Sikob			printf("fw_rcv: unknown response "
1773110891Ssimokawa					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1774113584Ssimokawa					tcode,
1775113584Ssimokawa					fp->mode.hdr.src,
1776103285Sikob					fp->mode.hdr.tlrt >> 2,
1777103285Sikob					fp->mode.hdr.tlrt & 3,
1778103285Sikob					fp->mode.rresq.data);
1779103285Sikob#if 1
1780103285Sikob			printf("try ad-hoc work around!!\n");
1781120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1782103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1783120660Ssimokawa			if (rb->xfer == NULL) {
1784103285Sikob				printf("no use...\n");
1785103285Sikob				goto err;
1786103285Sikob			}
1787103285Sikob#else
1788103285Sikob			goto err;
1789103285Sikob#endif
1790103285Sikob		}
1791120660Ssimokawa		fw_rcv_copy(rb);
1792120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1793120660Ssimokawa			rb->xfer->resp = EIO;
1794120660Ssimokawa		else
1795120660Ssimokawa			rb->xfer->resp = 0;
1796113584Ssimokawa		/* make sure the packet is drained in AT queue */
1797120660Ssimokawa		oldstate = rb->xfer->state;
1798120660Ssimokawa		rb->xfer->state = FWXF_RCVD;
1799113584Ssimokawa		switch (oldstate) {
1800113584Ssimokawa		case FWXF_SENT:
1801120660Ssimokawa			fw_xfer_done(rb->xfer);
1802103285Sikob			break;
1803113584Ssimokawa		case FWXF_START:
1804119289Ssimokawa#if 0
1805113584Ssimokawa			if (firewire_debug)
1806120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1807119289Ssimokawa#endif
1808113584Ssimokawa			break;
1809103285Sikob		default:
1810120660Ssimokawa			printf("unexpected state %d\n", rb->xfer->state);
1811103285Sikob		}
1812113584Ssimokawa		return;
1813103285Sikob	case FWTCODE_WREQQ:
1814103285Sikob	case FWTCODE_WREQB:
1815103285Sikob	case FWTCODE_RREQQ:
1816103285Sikob	case FWTCODE_RREQB:
1817103285Sikob	case FWTCODE_LREQ:
1818120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1819113584Ssimokawa			fp->mode.rreqq.dest_lo);
1820103285Sikob		if(bind == NULL){
1821113962Ssimokawa#if __FreeBSD_version >= 500000
1822120660Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x tcode=%x src=0x%x data=%x\n",
1823113962Ssimokawa#else
1824120660Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x tcode=%x src=0x%x data=%lx\n",
1825113962Ssimokawa#endif
1826113584Ssimokawa				fp->mode.wreqq.dest_hi,
1827113584Ssimokawa				fp->mode.wreqq.dest_lo,
1828113584Ssimokawa				tcode,
1829113584Ssimokawa				fp->mode.hdr.src,
1830113584Ssimokawa				ntohl(fp->mode.wreqq.data));
1831120660Ssimokawa			if (rb->fc->status == FWBUSRESET) {
1832110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1833103285Sikob				goto err;
1834103285Sikob			}
1835120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
1836120660Ssimokawa			if(rb->xfer == NULL){
1837103285Sikob				return;
1838103285Sikob			}
1839120660Ssimokawa			rb->xfer->send.spd = rb->spd;
1840120660Ssimokawa			rb->xfer->send.pay_len = 0;
1841120660Ssimokawa			resfp = &rb->xfer->send.hdr;
1842113584Ssimokawa			switch (tcode) {
1843103285Sikob			case FWTCODE_WREQQ:
1844103285Sikob			case FWTCODE_WREQB:
1845103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1846103285Sikob				break;
1847103285Sikob			case FWTCODE_RREQQ:
1848103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1849103285Sikob				break;
1850103285Sikob			case FWTCODE_RREQB:
1851103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1852103285Sikob				break;
1853103285Sikob			case FWTCODE_LREQ:
1854103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1855103285Sikob				break;
1856103285Sikob			}
1857103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1858103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1859103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1860120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1861103285Sikob			resfp->mode.rresb.extcode = 0;
1862103285Sikob			resfp->mode.rresb.len = 0;
1863103285Sikob/*
1864120660Ssimokawa			rb->xfer->act.hand = fw_asy_callback;
1865103285Sikob*/
1866120660Ssimokawa			rb->xfer->act.hand = fw_xfer_free;
1867120660Ssimokawa			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1868120660Ssimokawa				fw_xfer_free(rb->xfer);
1869103285Sikob				return;
1870103285Sikob			}
1871103285Sikob			goto err;
1872103285Sikob		}
1873113584Ssimokawa		len = 0;
1874120660Ssimokawa		for (i = 0; i < rb->nvec; i ++)
1875120660Ssimokawa			len += rb->vec[i].iov_len;
1876113584Ssimokawa		switch(bind->act_type){
1877103285Sikob		case FWACT_XFER:
1878113584Ssimokawa			/* splfw()?? */
1879120660Ssimokawa			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1880120660Ssimokawa			if (rb->xfer == NULL) {
1881113584Ssimokawa				printf("Discard a packet for this bind.\n");
1882113584Ssimokawa				goto err;
1883113584Ssimokawa			}
1884113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1885120660Ssimokawa			fw_rcv_copy(rb);
1886124145Ssimokawa			rb->xfer->act.hand(rb->xfer);
1887103285Sikob			return;
1888103285Sikob			break;
1889103285Sikob		case FWACT_CH:
1890120660Ssimokawa			if(rb->fc->ir[bind->sub]->queued >=
1891120660Ssimokawa				rb->fc->ir[bind->sub]->maxq){
1892120660Ssimokawa				device_printf(rb->fc->bdev,
1893108655Ssimokawa					"Discard a packet %x %d\n",
1894113584Ssimokawa					bind->sub,
1895120660Ssimokawa					rb->fc->ir[bind->sub]->queued);
1896103285Sikob				goto err;
1897103285Sikob			}
1898120660Ssimokawa			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1899120660Ssimokawa			if (rb->xfer == NULL) {
1900113584Ssimokawa				printf("Discard packet for this bind\n");
1901113584Ssimokawa				goto err;
1902113584Ssimokawa			}
1903113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1904120660Ssimokawa			fw_rcv_copy(rb);
1905103285Sikob			s = splfw();
1906120660Ssimokawa			rb->fc->ir[bind->sub]->queued++;
1907120660Ssimokawa			STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q,
1908120660Ssimokawa			    rb->xfer, link);
1909103285Sikob			splx(s);
1910103285Sikob
1911120660Ssimokawa			wakeup((caddr_t)rb->fc->ir[bind->sub]);
1912103285Sikob
1913103285Sikob			return;
1914103285Sikob			break;
1915103285Sikob		default:
1916103285Sikob			goto err;
1917103285Sikob			break;
1918103285Sikob		}
1919103285Sikob		break;
1920120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
1921103285Sikob	case FWTCODE_STREAM:
1922103285Sikob	{
1923103285Sikob		struct fw_xferq *xferq;
1924103285Sikob
1925120660Ssimokawa		xferq = rb->fc->ir[sub];
1926103285Sikob#if 0
1927103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1928103285Sikob			sub, len, off, spd);
1929103285Sikob#endif
1930103285Sikob		if(xferq->queued >= xferq->maxq) {
1931103285Sikob			printf("receive queue is full\n");
1932103285Sikob			goto err;
1933103285Sikob		}
1934113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1935113584Ssimokawa			per packet mode */
1936120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1937113584Ssimokawa						vec[0].iov_len);
1938120660Ssimokawa		if (rb->xfer == NULL) goto err;
1939120660Ssimokawa		fw_rcv_copy(rb)
1940103285Sikob		s = splfw();
1941103285Sikob		xferq->queued++;
1942120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1943103285Sikob		splx(s);
1944120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
1945103285Sikob#if __FreeBSD_version >= 500000
1946103285Sikob		if (SEL_WAITING(&xferq->rsel))
1947103285Sikob#else
1948103285Sikob		if (&xferq->rsel.si_pid != 0)
1949103285Sikob#endif
1950122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
1951103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1952103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1953103285Sikob			wakeup((caddr_t)xferq);
1954103285Sikob		}
1955103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1956103285Sikob			xferq->hand(xferq);
1957103285Sikob		}
1958103285Sikob		return;
1959103285Sikob		break;
1960103285Sikob	}
1961120660Ssimokawa#endif
1962103285Sikob	default:
1963113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1964103285Sikob		break;
1965103285Sikob	}
1966103285Sikoberr:
1967113584Ssimokawa	return;
1968103285Sikob}
1969106815Ssimokawa
1970103285Sikob/*
1971103285Sikob * Post process for Bus Manager election process.
1972103285Sikob */
1973103285Sikobstatic void
1974103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1975103285Sikob{
1976103285Sikob	struct firewire_comm *fc;
1977109422Ssimokawa	int bmr;
1978103285Sikob
1979109422Ssimokawa	if (xfer == NULL)
1980109422Ssimokawa		return;
1981103285Sikob	fc = xfer->fc;
1982109422Ssimokawa	if (xfer->resp != 0)
1983103285Sikob		goto error;
1984120660Ssimokawa	if (xfer->recv.payload == NULL)
1985103285Sikob		goto error;
1986120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
1987103285Sikob		goto error;
1988109422Ssimokawa
1989120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
1990109422Ssimokawa	if (bmr == 0x3f)
1991109422Ssimokawa		bmr = fc->nodeid;
1992109422Ssimokawa
1993109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1994120660Ssimokawa	fw_xfer_free_buf(xfer);
1995114909Ssimokawa	fw_bmr(fc);
1996114909Ssimokawa	return;
1997114909Ssimokawa
1998103285Sikoberror:
1999114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2000120660Ssimokawa	fw_xfer_free_buf(xfer);
2001103285Sikob}
2002106815Ssimokawa
2003113584Ssimokawa
2004103285Sikob/*
2005103285Sikob * To candidate Bus Manager election process.
2006103285Sikob */
2007110270Ssimokawastatic void
2008106815Ssimokawafw_try_bmr(void *arg)
2009103285Sikob{
2010103285Sikob	struct fw_xfer *xfer;
2011103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2012103285Sikob	struct fw_pkt *fp;
2013103285Sikob	int err = 0;
2014103285Sikob
2015120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2016103285Sikob	if(xfer == NULL){
2017103285Sikob		return;
2018103285Sikob	}
2019120660Ssimokawa	xfer->send.spd = 0;
2020103285Sikob	fc->status = FWBUSMGRELECT;
2021103285Sikob
2022120660Ssimokawa	fp = &xfer->send.hdr;
2023113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2024103285Sikob	fp->mode.lreq.tlrt = 0;
2025103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2026103285Sikob	fp->mode.lreq.pri = 0;
2027103285Sikob	fp->mode.lreq.src = 0;
2028113584Ssimokawa	fp->mode.lreq.len = 8;
2029120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2030120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2031113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2032120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2033120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2034103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2035103285Sikob
2036103285Sikob	err = fw_asyreq(fc, -1, xfer);
2037103285Sikob	if(err){
2038120660Ssimokawa		fw_xfer_free_buf(xfer);
2039103285Sikob		return;
2040103285Sikob	}
2041103285Sikob	return;
2042103285Sikob}
2043106543Ssimokawa
2044106543Ssimokawa#ifdef FW_VMACCESS
2045103285Sikob/*
2046103285Sikob * Software implementation for physical memory block access.
2047103285Sikob * XXX:Too slow, usef for debug purpose only.
2048103285Sikob */
2049106815Ssimokawastatic void
2050106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2051103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2052113584Ssimokawa	u_int32_t *ld = (u_int32_t *)xfer->recv.buf;
2053103285Sikob
2054113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2055113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2056103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2057103285Sikob	if(xfer->resp != 0){
2058103285Sikob		fw_xfer_free( xfer);
2059103285Sikob		return;
2060103285Sikob	}
2061103285Sikob	if(xfer->recv.buf == NULL){
2062103285Sikob		fw_xfer_free( xfer);
2063103285Sikob		return;
2064103285Sikob	}
2065103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2066103285Sikob	switch(rfp->mode.hdr.tcode){
2067103285Sikob		/* XXX need fix for 64bit arch */
2068103285Sikob		case FWTCODE_WREQB:
2069110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2070103285Sikob			xfer->send.len = 12;
2071103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2072103285Sikob			bcopy(rfp->mode.wreqb.payload,
2073103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2074103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2075103285Sikob			sfp->mode.wres.rtcode = 0;
2076103285Sikob			break;
2077103285Sikob		case FWTCODE_WREQQ:
2078110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2079103285Sikob			xfer->send.len = 12;
2080103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2081103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2082103285Sikob			sfp->mode.wres.rtcode = 0;
2083103285Sikob			break;
2084103285Sikob		case FWTCODE_RREQB:
2085110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2086103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2087103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2088103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2089103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2090103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2091103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2092103285Sikob			sfp->mode.rresb.rtcode = 0;
2093103285Sikob			sfp->mode.rresb.extcode = 0;
2094103285Sikob			break;
2095103285Sikob		case FWTCODE_RREQQ:
2096110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2097103285Sikob			xfer->send.len = 16;
2098103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2099103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2100103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2101103285Sikob			sfp->mode.rresb.rtcode = 0;
2102103285Sikob			break;
2103103285Sikob		default:
2104103285Sikob			fw_xfer_free( xfer);
2105103285Sikob			return;
2106103285Sikob	}
2107103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2108103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2109103285Sikob	xfer->act.hand = fw_xfer_free;
2110103285Sikob	xfer->retry_req = fw_asybusy;
2111103285Sikob
2112103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2113103285Sikob	sfp->mode.hdr.pri = 0;
2114103285Sikob
2115103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2116103285Sikob/**/
2117103285Sikob	return;
2118103285Sikob}
2119106543Ssimokawa#endif
2120106543Ssimokawa
2121103285Sikob/*
2122103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2123103285Sikob */
2124106815Ssimokawau_int16_t
2125106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2126103285Sikob	u_int32_t i, sum, crc = 0;
2127103285Sikob	int shift;
2128103285Sikob	len = (len + 3) & ~3;
2129103285Sikob	for(i = 0 ; i < len ; i+= 4){
2130103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2131103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2132103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2133103285Sikob		}
2134103285Sikob		crc &= 0xffff;
2135103285Sikob	}
2136103285Sikob	return((u_int16_t) crc);
2137103285Sikob}
2138106815Ssimokawa
2139110270Ssimokawastatic int
2140110072Ssimokawafw_bmr(struct firewire_comm *fc)
2141110072Ssimokawa{
2142110072Ssimokawa	struct fw_device fwdev;
2143113584Ssimokawa	union fw_self_id *self_id;
2144110072Ssimokawa	int cmstr;
2145120660Ssimokawa	u_int32_t quad;
2146110072Ssimokawa
2147113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2148113584Ssimokawa	self_id = &fc->topology_map->self_id[fc->max_node];
2149113584Ssimokawa	if (fc->max_node > 0) {
2150114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2151114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2152113584Ssimokawa			cmstr = fc->max_node;
2153114909Ssimokawa		else {
2154114909Ssimokawa			device_printf(fc->bdev,
2155114909Ssimokawa				"root node is not cycle master capable\n");
2156114909Ssimokawa			/* XXX shall we be the cycle master? */
2157113584Ssimokawa			cmstr = fc->nodeid;
2158114909Ssimokawa			/* XXX need bus reset */
2159114909Ssimokawa		}
2160113584Ssimokawa	} else
2161113584Ssimokawa		cmstr = -1;
2162114909Ssimokawa
2163114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2164114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2165114909Ssimokawa		/* We are not the bus manager */
2166114909Ssimokawa		printf("\n");
2167114909Ssimokawa		return(0);
2168114909Ssimokawa	}
2169114909Ssimokawa	printf("(me)\n");
2170114909Ssimokawa
2171114909Ssimokawa	/* Optimize gapcount */
2172113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2173113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2174110072Ssimokawa	/* If we are the cycle master, nothing to do */
2175113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2176110072Ssimokawa		return 0;
2177110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2178110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2179110072Ssimokawa	fwdev.fc = fc;
2180110072Ssimokawa	fwdev.dst = cmstr;
2181110072Ssimokawa	fwdev.speed = 0;
2182110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2183110072Ssimokawa	fwdev.status = FWDEVINIT;
2184110072Ssimokawa	/* Set cmstr bit on the cycle master */
2185120660Ssimokawa	quad = htonl(1 << 8);
2186110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2187120660Ssimokawa		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2188110072Ssimokawa
2189110072Ssimokawa	return 0;
2190110072Ssimokawa}
2191110072Ssimokawa
2192118455Ssimokawastatic int
2193118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2194118455Ssimokawa{
2195118455Ssimokawa	int err = 0;
2196118455Ssimokawa#if __FreeBSD_version >= 500000
2197118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2198118455Ssimokawa#endif
2199118455Ssimokawa
2200118455Ssimokawa	switch (type) {
2201118455Ssimokawa	case MOD_LOAD:
2202118455Ssimokawa#if __FreeBSD_version >= 500000
2203118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2204118455Ssimokawa						fwdev_clone, 0, 1000);
2205118455Ssimokawa#endif
2206118455Ssimokawa		break;
2207118455Ssimokawa	case MOD_UNLOAD:
2208118455Ssimokawa#if __FreeBSD_version >= 500000
2209118455Ssimokawa		if (fwdev_ehtag != NULL)
2210118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2211118455Ssimokawa#endif
2212118455Ssimokawa		break;
2213118455Ssimokawa	case MOD_SHUTDOWN:
2214118455Ssimokawa		break;
2215118455Ssimokawa	}
2216118455Ssimokawa	return (err);
2217118455Ssimokawa}
2218118455Ssimokawa
2219118455Ssimokawa
2220118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2221103285SikobMODULE_VERSION(firewire, 1);
2222