firewire.c revision 169130
1139749Simp/*-
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 169130 2007-04-30 13:41:40Z 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>
43129879Sphk#include <sys/module.h>
44103285Sikob#include <sys/malloc.h>
45103285Sikob#include <sys/conf.h>
46103285Sikob#include <sys/sysctl.h>
47103285Sikob
48127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
49117067Ssimokawa#include <machine/clock.h>	/* for DELAY() */
50117067Ssimokawa#endif
51103285Sikob
52103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
53113584Ssimokawa#include <machine/bus.h>
54103285Sikob
55127468Ssimokawa#ifdef __DragonFly__
56127468Ssimokawa#include "firewire.h"
57127468Ssimokawa#include "firewirereg.h"
58127468Ssimokawa#include "fwmem.h"
59127468Ssimokawa#include "iec13213.h"
60127468Ssimokawa#include "iec68113.h"
61127468Ssimokawa#else
62103285Sikob#include <dev/firewire/firewire.h>
63103285Sikob#include <dev/firewire/firewirereg.h>
64110072Ssimokawa#include <dev/firewire/fwmem.h>
65103285Sikob#include <dev/firewire/iec13213.h>
66103285Sikob#include <dev/firewire/iec68113.h>
67127468Ssimokawa#endif
68103285Sikob
69116376Ssimokawastruct crom_src_buf {
70116376Ssimokawa	struct crom_src	src;
71116376Ssimokawa	struct crom_chunk root;
72116376Ssimokawa	struct crom_chunk vendor;
73116376Ssimokawa	struct crom_chunk hw;
74116376Ssimokawa};
75116376Ssimokawa
76120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3;
77103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
78108281Ssimokawa	"FireWire driver debug flag");
79109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
80109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
81109736Ssimokawa	"Try to be a bus manager");
82120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
83120850Ssimokawa	"Number of count of bus resets for removing lost device information");
84103285Sikob
85110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
86110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
87110195Ssimokawa
88103285Sikob#define FW_MAXASYRTY 4
89103285Sikob
90103285Sikobdevclass_t firewire_devclass;
91103285Sikob
92125238Ssimokawastatic void firewire_identify	(driver_t *, device_t);
93125238Ssimokawastatic int firewire_probe	(device_t);
94124169Ssimokawastatic int firewire_attach      (device_t);
95124169Ssimokawastatic int firewire_detach      (device_t);
96124169Ssimokawastatic int firewire_resume      (device_t);
97103285Sikob#if 0
98124169Ssimokawastatic int firewire_shutdown    (device_t);
99103285Sikob#endif
100124169Ssimokawastatic device_t firewire_add_child   (device_t, int, const char *, int);
101124169Ssimokawastatic void fw_try_bmr (void *);
102124169Ssimokawastatic void fw_try_bmr_callback (struct fw_xfer *);
103124169Ssimokawastatic void fw_asystart (struct fw_xfer *);
104124169Ssimokawastatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
105124169Ssimokawastatic void fw_bus_probe (struct firewire_comm *);
106124169Ssimokawastatic void fw_bus_explore (struct firewire_comm *);
107124169Ssimokawastatic void fw_bus_explore_callback (struct fw_xfer *);
108124169Ssimokawastatic void fw_attach_dev (struct firewire_comm *);
109106543Ssimokawa#ifdef FW_VMACCESS
110124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *);
111106543Ssimokawa#endif
112129585Sdfrstruct fw_xfer *asyreqq (struct firewire_comm *, uint8_t, uint8_t, uint8_t,
113129585Sdfr	uint32_t, uint32_t, void (*)(struct fw_xfer *));
114124169Ssimokawastatic int fw_bmr (struct firewire_comm *);
115103285Sikob
116103285Sikobstatic device_method_t firewire_methods[] = {
117103285Sikob	/* Device interface */
118125238Ssimokawa	DEVMETHOD(device_identify,	firewire_identify),
119125238Ssimokawa	DEVMETHOD(device_probe,		firewire_probe),
120103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
121103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
122108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
123116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
124103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
125103285Sikob
126103285Sikob	/* Bus interface */
127103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
128103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
129103285Sikob
130103285Sikob	{ 0, 0 }
131103285Sikob};
132124251Ssimokawachar *linkspeed[] = {
133124251Ssimokawa	"S100", "S200", "S400", "S800",
134124251Ssimokawa	"S1600", "S3200", "undef", "undef"
135124251Ssimokawa};
136103285Sikob
137124251Ssimokawastatic char *tcode_str[] = {
138124251Ssimokawa	"WREQQ", "WREQB", "WRES",   "undef",
139124251Ssimokawa	"RREQQ", "RREQB", "RRESQ",  "RRESB",
140124251Ssimokawa	"CYCS",  "LREQ",  "STREAM", "LRES",
141124251Ssimokawa	"undef", "undef", "PHY",    "undef"
142124251Ssimokawa};
143124251Ssimokawa
144114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
145114909Ssimokawa#define MAX_GAPHOP 15
146114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
147114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
148106813Ssimokawa
149103285Sikobstatic driver_t firewire_driver = {
150103285Sikob	"firewire",
151103285Sikob	firewire_methods,
152103285Sikob	sizeof(struct firewire_softc),
153103285Sikob};
154103285Sikob
155103285Sikob/*
156110072Ssimokawa * Lookup fwdev by node id.
157103285Sikob */
158106810Ssimokawastruct fw_device *
159110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
160103285Sikob{
161103285Sikob	struct fw_device *fwdev;
162110072Ssimokawa	int s;
163110072Ssimokawa
164110072Ssimokawa	s = splfw();
165110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
166120660Ssimokawa		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
167103285Sikob			break;
168110072Ssimokawa	splx(s);
169110072Ssimokawa
170106810Ssimokawa	return fwdev;
171103285Sikob}
172106813Ssimokawa
173103285Sikob/*
174110072Ssimokawa * Lookup fwdev by EUI64.
175110072Ssimokawa */
176110072Ssimokawastruct fw_device *
177110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
178110072Ssimokawa{
179110072Ssimokawa	struct fw_device *fwdev;
180110072Ssimokawa	int s;
181110072Ssimokawa
182110072Ssimokawa	s = splfw();
183110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
184110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
185110072Ssimokawa			break;
186110072Ssimokawa	splx(s);
187110072Ssimokawa
188110072Ssimokawa	if(fwdev == NULL) return NULL;
189110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
190110072Ssimokawa	return fwdev;
191110072Ssimokawa}
192110072Ssimokawa
193110072Ssimokawa/*
194103285Sikob * Async. request procedure for userland application.
195103285Sikob */
196103285Sikobint
197103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
198103285Sikob{
199103285Sikob	int err = 0;
200103285Sikob	struct fw_xferq *xferq;
201169119Ssimokawa	int tl = -1, len;
202103285Sikob	struct fw_pkt *fp;
203103285Sikob	int tcode;
204103285Sikob	struct tcode_info *info;
205103285Sikob
206103285Sikob	if(xfer == NULL) return EINVAL;
207167632Ssimokawa	if(xfer->hand == NULL){
208167632Ssimokawa		printf("hand == NULL\n");
209103285Sikob		return EINVAL;
210103285Sikob	}
211120660Ssimokawa	fp = &xfer->send.hdr;
212103285Sikob
213103285Sikob	tcode = fp->mode.common.tcode & 0xf;
214103285Sikob	info = &fc->tcode[tcode];
215103285Sikob	if (info->flag == 0) {
216124251Ssimokawa		printf("invalid tcode=%x\n", tcode);
217103285Sikob		return EINVAL;
218103285Sikob	}
219103285Sikob	if (info->flag & FWTI_REQ)
220103285Sikob		xferq = fc->atq;
221103285Sikob	else
222103285Sikob		xferq = fc->ats;
223103285Sikob	len = info->hdr_len;
224120660Ssimokawa	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
225120660Ssimokawa		printf("send.pay_len > maxrec\n");
226120660Ssimokawa		return EINVAL;
227120660Ssimokawa	}
228103285Sikob	if (info->flag & FWTI_BLOCK_STR)
229120660Ssimokawa		len = fp->mode.stream.len;
230103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
231120660Ssimokawa		len = fp->mode.rresb.len;
232120660Ssimokawa	else
233120660Ssimokawa		len = 0;
234120660Ssimokawa	if (len != xfer->send.pay_len){
235124251Ssimokawa		printf("len(%d) != send.pay_len(%d) %s(%x)\n",
236124251Ssimokawa		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
237103285Sikob		return EINVAL;
238103285Sikob	}
239106790Ssimokawa
240103285Sikob	if(xferq->start == NULL){
241103285Sikob		printf("xferq->start == NULL\n");
242103285Sikob		return EINVAL;
243103285Sikob	}
244103285Sikob	if(!(xferq->queued < xferq->maxq)){
245108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
246108655Ssimokawa			xferq->queued);
247103285Sikob		return EINVAL;
248103285Sikob	}
249103285Sikob
250103285Sikob	if (info->flag & FWTI_TLABEL) {
251130460Sdfr		if ((tl = fw_get_tlabel(fc, xfer)) == -1)
252130460Sdfr			return EAGAIN;
253103285Sikob		fp->mode.hdr.tlrt = tl << 2;
254103285Sikob	}
255103285Sikob
256103285Sikob	xfer->tl = tl;
257103285Sikob	xfer->resp = 0;
258103285Sikob	xfer->fc = fc;
259103285Sikob	xfer->q = xferq;
260103285Sikob
261103285Sikob	fw_asystart(xfer);
262103285Sikob	return err;
263103285Sikob}
264103285Sikob/*
265103285Sikob * Wakeup blocked process.
266103285Sikob */
267103285Sikobvoid
268103285Sikobfw_asy_callback(struct fw_xfer *xfer){
269103285Sikob	wakeup(xfer);
270103285Sikob	return;
271103285Sikob}
272103285Sikob
273103285Sikob/*
274103285Sikob * Async. request with given xfer structure.
275103285Sikob */
276106790Ssimokawastatic void
277106790Ssimokawafw_asystart(struct fw_xfer *xfer)
278106790Ssimokawa{
279103285Sikob	struct firewire_comm *fc = xfer->fc;
280103285Sikob	int s;
281103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
282103285Sikob	if (fc->status < FWBUSEXPLORE) {
283103285Sikob		xfer->resp = EAGAIN;
284103285Sikob		xfer->state = FWXF_BUSY;
285167632Ssimokawa		if (xfer->hand != NULL)
286167632Ssimokawa			xfer->hand(xfer);
287103285Sikob		return;
288103285Sikob	}
289103285Sikob#endif
290127468Ssimokawa	microtime(&xfer->tv);
291103285Sikob	s = splfw();
292103285Sikob	xfer->state = FWXF_INQ;
293103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
294103285Sikob	xfer->q->queued ++;
295103285Sikob	splx(s);
296103285Sikob	/* XXX just queue for mbuf */
297103285Sikob	if (xfer->mbuf == NULL)
298103285Sikob		xfer->q->start(fc);
299103285Sikob	return;
300103285Sikob}
301106790Ssimokawa
302125238Ssimokawastatic void
303125238Ssimokawafirewire_identify(driver_t *driver, device_t parent)
304125238Ssimokawa{
305125238Ssimokawa	BUS_ADD_CHILD(parent, 0, "firewire", -1);
306125238Ssimokawa}
307125238Ssimokawa
308103285Sikobstatic int
309125238Ssimokawafirewire_probe(device_t dev)
310103285Sikob{
311108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
312125238Ssimokawa	return (0);
313103285Sikob}
314106790Ssimokawa
315110577Ssimokawastatic void
316110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
317110577Ssimokawa{
318110577Ssimokawa	struct fw_xfer *xfer;
319110577Ssimokawa	struct timeval tv;
320110577Ssimokawa	struct timeval split_timeout;
321111040Ssimokawa	int i, s;
322110577Ssimokawa
323120660Ssimokawa	split_timeout.tv_sec = 0;
324120660Ssimokawa	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
325110577Ssimokawa
326110577Ssimokawa	microtime(&tv);
327110577Ssimokawa	timevalsub(&tv, &split_timeout);
328110577Ssimokawa
329111040Ssimokawa	s = splfw();
330110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
331169119Ssimokawa		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
332110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
333110577Ssimokawa				/* the rests are newer than this */
334110577Ssimokawa				break;
335124145Ssimokawa			if (xfer->state == FWXF_START)
336124145Ssimokawa				/* not sent yet */
337124145Ssimokawa				break;
338110577Ssimokawa			device_printf(fc->bdev,
339117716Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
340120660Ssimokawa				xfer->send.hdr.mode.hdr.dst, i, xfer->state);
341110577Ssimokawa			xfer->resp = ETIMEDOUT;
342113584Ssimokawa			fw_xfer_done(xfer);
343110577Ssimokawa		}
344110577Ssimokawa	}
345111040Ssimokawa	splx(s);
346110577Ssimokawa}
347110577Ssimokawa
348121463Ssimokawa#define WATCHDOC_HZ 10
349110577Ssimokawastatic void
350110577Ssimokawafirewire_watchdog(void *arg)
351110577Ssimokawa{
352110577Ssimokawa	struct firewire_comm *fc;
353121463Ssimokawa	static int watchdoc_clock = 0;
354110577Ssimokawa
355110577Ssimokawa	fc = (struct firewire_comm *)arg;
356121463Ssimokawa
357121463Ssimokawa	/*
358121463Ssimokawa	 * At boot stage, the device interrupt is disabled and
359121463Ssimokawa	 * We encounter a timeout easily. To avoid this,
360121463Ssimokawa	 * ignore clock interrupt for a while.
361121463Ssimokawa	 */
362121463Ssimokawa	if (watchdoc_clock > WATCHDOC_HZ * 15) {
363121463Ssimokawa		firewire_xfer_timeout(fc);
364121463Ssimokawa		fc->timeout(fc);
365121463Ssimokawa	} else
366121463Ssimokawa		watchdoc_clock ++;
367121463Ssimokawa
368121463Ssimokawa	callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ,
369110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
370110577Ssimokawa}
371110577Ssimokawa
372103285Sikob/*
373103285Sikob * The attach routine.
374103285Sikob */
375103285Sikobstatic int
376118455Ssimokawafirewire_attach(device_t dev)
377103285Sikob{
378118455Ssimokawa	int unit;
379103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
380103285Sikob	device_t pa = device_get_parent(dev);
381103285Sikob	struct firewire_comm *fc;
382103285Sikob
383103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
384103285Sikob	sc->fc = fc;
385116978Ssimokawa	fc->status = FWBUSNOTREADY;
386103285Sikob
387118455Ssimokawa	unit = device_get_unit(dev);
388118455Ssimokawa	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
389103285Sikob
390118455Ssimokawa	fwdev_makedev(sc);
391118455Ssimokawa
392110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
393110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
394110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
395108853Ssimokawa
396110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
397110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
398110193Ssimokawa
399103285Sikob	/* Locate our children */
400103285Sikob	bus_generic_probe(dev);
401103285Sikob
402103285Sikob	/* launch attachement of the added children */
403103285Sikob	bus_generic_attach(dev);
404103285Sikob
405103285Sikob	/* bus_reset */
406169117Ssimokawa	fw_busreset(fc, FWBUSNOTREADY);
407103285Sikob	fc->ibr(fc);
408103285Sikob
409103285Sikob	return 0;
410103285Sikob}
411103285Sikob
412103285Sikob/*
413103285Sikob * Attach it as child.
414103285Sikob */
415103285Sikobstatic device_t
416103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
417103285Sikob{
418103285Sikob        device_t child;
419103285Sikob	struct firewire_softc *sc;
420103285Sikob
421103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
422103285Sikob	child = device_add_child(dev, name, unit);
423103285Sikob	if (child) {
424103285Sikob		device_set_ivars(child, sc->fc);
425103285Sikob		device_probe_and_attach(child);
426103285Sikob	}
427103285Sikob
428103285Sikob	return child;
429103285Sikob}
430106790Ssimokawa
431116978Ssimokawastatic int
432116978Ssimokawafirewire_resume(device_t dev)
433116978Ssimokawa{
434116978Ssimokawa	struct firewire_softc *sc;
435116978Ssimokawa
436116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
437116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
438116978Ssimokawa
439116978Ssimokawa	bus_generic_resume(dev);
440116978Ssimokawa
441116978Ssimokawa	return(0);
442116978Ssimokawa}
443116978Ssimokawa
444103285Sikob/*
445103285Sikob * Dettach it.
446103285Sikob */
447103285Sikobstatic int
448118455Ssimokawafirewire_detach(device_t dev)
449103285Sikob{
450103285Sikob	struct firewire_softc *sc;
451111074Ssimokawa	struct csrdir *csrd, *next;
452111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
453118455Ssimokawa	int err;
454103285Sikob
455103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
456118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
457118455Ssimokawa		return err;
458106790Ssimokawa
459118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
460118455Ssimokawa		return err;
461111078Ssimokawa
462111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
463111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
464111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
465111078Ssimokawa
466103285Sikob	/* XXX xfree_free and untimeout on all xfers */
467111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
468111078Ssimokawa							fwdev = fwdev_next) {
469111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
470111078Ssimokawa		free(fwdev, M_FW);
471111078Ssimokawa	}
472111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
473111074Ssimokawa		next = SLIST_NEXT(csrd, link);
474111074Ssimokawa		free(csrd, M_FW);
475111074Ssimokawa	}
476110195Ssimokawa	free(sc->fc->topology_map, M_FW);
477110195Ssimokawa	free(sc->fc->speed_map, M_FW);
478116376Ssimokawa	free(sc->fc->crom_src_buf, M_FW);
479103285Sikob	return(0);
480103285Sikob}
481103285Sikob#if 0
482103285Sikobstatic int
483103285Sikobfirewire_shutdown( device_t dev )
484103285Sikob{
485103285Sikob	return 0;
486103285Sikob}
487103285Sikob#endif
488106790Ssimokawa
489110577Ssimokawa
490110577Ssimokawastatic void
491110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
492110577Ssimokawa{
493110577Ssimokawa	struct fw_xfer *xfer;
494110577Ssimokawa
495110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
496110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
497111942Ssimokawa		xferq->queued --;
498110577Ssimokawa		xfer->resp = EAGAIN;
499113584Ssimokawa		fw_xfer_done(xfer);
500110577Ssimokawa	}
501110577Ssimokawa}
502110577Ssimokawa
503110798Ssimokawavoid
504110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
505110798Ssimokawa{
506110798Ssimokawa	int i;
507110798Ssimokawa
508110798Ssimokawa	fw_xferq_drain(fc->atq);
509110798Ssimokawa	fw_xferq_drain(fc->ats);
510110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
511110798Ssimokawa		fw_xferq_drain(fc->it[i]);
512110798Ssimokawa}
513110798Ssimokawa
514116376Ssimokawastatic void
515116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
516103285Sikob{
517103285Sikob	int i;
518103285Sikob
519103285Sikob	CSRARC(fc, STATE_CLEAR)
520103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
521103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
522103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
523103285Sikob
524103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
525103285Sikob	fc->irm = -1;
526103285Sikob
527103285Sikob	fc->max_node = -1;
528103285Sikob
529103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
530103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
531103285Sikob	}
532103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
533103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
534103285Sikob	CSRARC(fc, RESET_START) = 0;
535103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
536103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
537103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
538103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
539103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
540103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
541103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
542103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
543103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
544103285Sikob
545103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
546103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
547103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
548103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
549103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
550103285Sikob
551103285Sikob/* DV depend CSRs see blue book */
552103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
553103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
554103285Sikob
555103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
556103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
557116376Ssimokawa}
558113584Ssimokawa
559116376Ssimokawastatic void
560116376Ssimokawafw_init_crom(struct firewire_comm *fc)
561116376Ssimokawa{
562116376Ssimokawa	struct crom_src *src;
563116376Ssimokawa
564116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
565116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
566116376Ssimokawa	if (fc->crom_src_buf == NULL)
567116376Ssimokawa		return;
568116376Ssimokawa
569116376Ssimokawa	src = &fc->crom_src_buf->src;
570116376Ssimokawa	bzero(src, sizeof(struct crom_src));
571116376Ssimokawa
572116376Ssimokawa	/* BUS info sample */
573116376Ssimokawa	src->hdr.info_len = 4;
574116376Ssimokawa
575116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
576116376Ssimokawa
577116376Ssimokawa	src->businfo.irmc = 1;
578116376Ssimokawa	src->businfo.cmc = 1;
579116376Ssimokawa	src->businfo.isc = 1;
580116376Ssimokawa	src->businfo.bmc = 1;
581116376Ssimokawa	src->businfo.pmc = 0;
582116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
583116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
584116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
585117350Ssimokawa	src->businfo.generation = 1;
586116376Ssimokawa	src->businfo.link_spd = fc->speed;
587116376Ssimokawa
588116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
589116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
590116376Ssimokawa
591116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
592116376Ssimokawa
593116376Ssimokawa	fc->crom_src = src;
594116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
595116376Ssimokawa}
596116376Ssimokawa
597116376Ssimokawastatic void
598116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
599116376Ssimokawa{
600116376Ssimokawa	struct crom_src_buf *buf;
601116376Ssimokawa	struct crom_src *src;
602116376Ssimokawa	struct crom_chunk *root;
603116376Ssimokawa
604116376Ssimokawa	if (fc->crom_src_buf == NULL)
605116376Ssimokawa		fw_init_crom(fc);
606116376Ssimokawa
607116376Ssimokawa	buf =  fc->crom_src_buf;
608116376Ssimokawa	src = fc->crom_src;
609116376Ssimokawa	root = fc->crom_root;
610116376Ssimokawa
611116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
612116376Ssimokawa
613116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
614116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
615116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
616116376Ssimokawa	/* private company_id */
617116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
618127468Ssimokawa#ifdef __DragonFly__
619127468Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
620127468Ssimokawa	crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
621127468Ssimokawa#else
622116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
623116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
624127468Ssimokawa#endif
625116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
626116376Ssimokawa}
627116376Ssimokawa
628116376Ssimokawa/*
629116376Ssimokawa * Called after bus reset.
630116376Ssimokawa */
631116376Ssimokawavoid
632169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status)
633116376Ssimokawa{
634116376Ssimokawa	struct firewire_dev_comm *fdc;
635117350Ssimokawa	struct crom_src *src;
636116376Ssimokawa	device_t *devlistp;
637117350Ssimokawa	void *newrom;
638116376Ssimokawa	int i, devcnt;
639116376Ssimokawa
640116376Ssimokawa	switch(fc->status){
641116376Ssimokawa	case FWBUSMGRELECT:
642116376Ssimokawa		callout_stop(&fc->bmr_callout);
643116376Ssimokawa		break;
644116376Ssimokawa	default:
645116376Ssimokawa		break;
646116376Ssimokawa	}
647169117Ssimokawa	fc->status = new_status;
648116376Ssimokawa	fw_reset_csr(fc);
649116376Ssimokawa	fw_reset_crom(fc);
650116376Ssimokawa
651113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
652113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
653113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
654113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
655113584Ssimokawa				if (fdc->post_busreset != NULL)
656113584Ssimokawa					fdc->post_busreset(fdc);
657113584Ssimokawa			}
658113584Ssimokawa		free(devlistp, M_TEMP);
659113584Ssimokawa	}
660116376Ssimokawa
661117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
662117350Ssimokawa	src = &fc->crom_src_buf->src;
663129585Sdfr	crom_load(src, (uint32_t *)newrom, CROMSIZE);
664117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
665117350Ssimokawa		/* bump generation and reload */
666117350Ssimokawa		src->businfo.generation ++;
667117350Ssimokawa		/* generation must be between 0x2 and 0xF */
668117350Ssimokawa		if (src->businfo.generation < 2)
669117350Ssimokawa			src->businfo.generation ++;
670129585Sdfr		crom_load(src, (uint32_t *)newrom, CROMSIZE);
671117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
672117350Ssimokawa	}
673117350Ssimokawa	free(newrom, M_FW);
674103285Sikob}
675106790Ssimokawa
676103285Sikob/* Call once after reboot */
677106790Ssimokawavoid fw_init(struct firewire_comm *fc)
678103285Sikob{
679103285Sikob	int i;
680106543Ssimokawa	struct csrdir *csrd;
681106543Ssimokawa#ifdef FW_VMACCESS
682103285Sikob	struct fw_xfer *xfer;
683103285Sikob	struct fw_bind *fwb;
684106543Ssimokawa#endif
685103285Sikob
686103285Sikob	fc->arq->queued = 0;
687103285Sikob	fc->ars->queued = 0;
688103285Sikob	fc->atq->queued = 0;
689103285Sikob	fc->ats->queued = 0;
690103285Sikob
691103285Sikob	fc->arq->buf = NULL;
692103285Sikob	fc->ars->buf = NULL;
693103285Sikob	fc->atq->buf = NULL;
694103285Sikob	fc->ats->buf = NULL;
695103285Sikob
696113584Ssimokawa	fc->arq->flag = 0;
697113584Ssimokawa	fc->ars->flag = 0;
698113584Ssimokawa	fc->atq->flag = 0;
699113584Ssimokawa	fc->ats->flag = 0;
700103285Sikob
701103285Sikob	STAILQ_INIT(&fc->atq->q);
702103285Sikob	STAILQ_INIT(&fc->ats->q);
703103285Sikob
704103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
705103285Sikob		fc->it[i]->queued = 0;
706103285Sikob		fc->ir[i]->queued = 0;
707103285Sikob
708103285Sikob		fc->it[i]->start = NULL;
709103285Sikob		fc->ir[i]->start = NULL;
710103285Sikob
711103285Sikob		fc->it[i]->buf = NULL;
712103285Sikob		fc->ir[i]->buf = NULL;
713103285Sikob
714103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
715103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
716103285Sikob
717103285Sikob		STAILQ_INIT(&fc->it[i]->q);
718103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
719103285Sikob	}
720103285Sikob
721103285Sikob	fc->arq->maxq = FWMAXQUEUE;
722103285Sikob	fc->ars->maxq = FWMAXQUEUE;
723103285Sikob	fc->atq->maxq = FWMAXQUEUE;
724103285Sikob	fc->ats->maxq = FWMAXQUEUE;
725103285Sikob
726103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
727103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
728103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
729103285Sikob	}
730103285Sikob/* Initialize csr registers */
731103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
732103285Sikob				sizeof(struct fw_topology_map),
733110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
734103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
735103285Sikob				sizeof(struct fw_speed_map),
736110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
737103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
738103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
739103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
740103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
741103285Sikob
742110193Ssimokawa	STAILQ_INIT(&fc->devices);
743103285Sikob
744103285Sikob/* Initialize csr ROM work space */
745103285Sikob	SLIST_INIT(&fc->ongocsr);
746103285Sikob	SLIST_INIT(&fc->csrfree);
747103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
748110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
749103285Sikob		if(csrd == NULL) break;
750103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
751103285Sikob	}
752103285Sikob
753103285Sikob/* Initialize Async handlers */
754103285Sikob	STAILQ_INIT(&fc->binds);
755103285Sikob	for( i = 0 ; i < 0x40 ; i++){
756103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
757103285Sikob	}
758103285Sikob
759103285Sikob/* DV depend CSRs see blue book */
760103285Sikob#if 0
761103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
762103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
763103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
764103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
765103285Sikob	}
766103285Sikob
767103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
768103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
769103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
770103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
771103285Sikob	}
772103285Sikob#endif
773103285Sikob
774116376Ssimokawa	fc->crom_src_buf = NULL;
775103285Sikob
776106543Ssimokawa#ifdef FW_VMACCESS
777103285Sikob	xfer = fw_xfer_alloc();
778103285Sikob	if(xfer == NULL) return;
779103285Sikob
780110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
781103285Sikob	if(fwb == NULL){
782103285Sikob		fw_xfer_free(xfer);
783139680Sjmg		return;
784103285Sikob	}
785167632Ssimokawa	xfer->hand = fw_vmaccess;
786103285Sikob	xfer->fc = fc;
787103285Sikob	xfer->sc = NULL;
788103285Sikob
789103285Sikob	fwb->start_hi = 0x2;
790103285Sikob	fwb->start_lo = 0;
791103285Sikob	fwb->addrlen = 0xffffffff;
792103285Sikob	fwb->xfer = xfer;
793103285Sikob	fw_bindadd(fc, fwb);
794106543Ssimokawa#endif
795103285Sikob}
796106790Ssimokawa
797120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
798120660Ssimokawa    ((fwb)->end < (addr))?1:0)
799120660Ssimokawa
800103285Sikob/*
801129541Sdfr * To lookup bound process from IEEE1394 address.
802103285Sikob */
803106813Ssimokawastruct fw_bind *
804129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
805103285Sikob{
806120660Ssimokawa	u_int64_t addr;
807103285Sikob	struct fw_bind *tfw;
808120660Ssimokawa
809120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
810120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
811169130Ssimokawa		if (BIND_CMP(addr, tfw) == 0)
812103285Sikob			return(tfw);
813103285Sikob	return(NULL);
814103285Sikob}
815106790Ssimokawa
816103285Sikob/*
817103285Sikob * To bind IEEE1394 address block to process.
818103285Sikob */
819106790Ssimokawaint
820106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
821103285Sikob{
822120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
823120660Ssimokawa
824120660Ssimokawa	if (fwb->start > fwb->end) {
825127468Ssimokawa		printf("%s: invalid range\n", __func__);
826120660Ssimokawa		return EINVAL;
827120660Ssimokawa	}
828120660Ssimokawa
829120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
830120660Ssimokawa		if (fwb->end < tfw->start)
831120660Ssimokawa			break;
832120660Ssimokawa		prev = tfw;
833120660Ssimokawa	}
834120660Ssimokawa	if (prev == NULL) {
835103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
836169130Ssimokawa		return (0);
837103285Sikob	}
838120660Ssimokawa	if (prev->end < fwb->start) {
839120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
840169130Ssimokawa		return (0);
841103285Sikob	}
842120660Ssimokawa
843127468Ssimokawa	printf("%s: bind failed\n", __func__);
844120660Ssimokawa	return (EBUSY);
845103285Sikob}
846103285Sikob
847103285Sikob/*
848103285Sikob * To free IEEE1394 address block.
849103285Sikob */
850106790Ssimokawaint
851106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
852103285Sikob{
853120660Ssimokawa#if 0
854120660Ssimokawa	struct fw_xfer *xfer, *next;
855120660Ssimokawa#endif
856120660Ssimokawa	struct fw_bind *tfw;
857103285Sikob	int s;
858103285Sikob
859103285Sikob	s = splfw();
860120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
861120660Ssimokawa		if (tfw == fwb) {
862120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
863120660Ssimokawa			goto found;
864120660Ssimokawa		}
865120660Ssimokawa
866129541Sdfr	printf("%s: no such binding\n", __func__);
867120660Ssimokawa	splx(s);
868120660Ssimokawa	return (1);
869120660Ssimokawafound:
870120660Ssimokawa#if 0
871113584Ssimokawa	/* shall we do this? */
872113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
873113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
874113584Ssimokawa		fw_xfer_free(xfer);
875113584Ssimokawa	}
876113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
877120660Ssimokawa#endif
878113584Ssimokawa
879103285Sikob	splx(s);
880103285Sikob	return 0;
881103285Sikob}
882103285Sikob
883169130Ssimokawaint
884169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
885169130Ssimokawa    int slen, int rlen, int n,
886169130Ssimokawa    struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
887169130Ssimokawa{
888169130Ssimokawa	int i, s;
889169130Ssimokawa	struct fw_xfer *xfer;
890169130Ssimokawa
891169130Ssimokawa	for (i = 0; i < n; i++) {
892169130Ssimokawa		xfer = fw_xfer_alloc_buf(type, slen, rlen);
893169130Ssimokawa		if (xfer == NULL)
894169130Ssimokawa			return (n);
895169130Ssimokawa		xfer->fc = fc;
896169130Ssimokawa		xfer->sc = sc;
897169130Ssimokawa		xfer->hand = hand;
898169130Ssimokawa		s = splfw();
899169130Ssimokawa		STAILQ_INSERT_TAIL(q, xfer, link);
900169130Ssimokawa		splx(s);
901169130Ssimokawa	}
902169130Ssimokawa	return (n);
903169130Ssimokawa}
904169130Ssimokawa
905169130Ssimokawavoid
906169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q)
907169130Ssimokawa{
908169130Ssimokawa	struct fw_xfer *xfer, *next;
909169130Ssimokawa
910169130Ssimokawa	for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
911169130Ssimokawa                next = STAILQ_NEXT(xfer, link);
912169130Ssimokawa                fw_xfer_free_buf(xfer);
913169130Ssimokawa        }
914169130Ssimokawa        STAILQ_INIT(q);
915169130Ssimokawa}
916169130Ssimokawa
917103285Sikob/*
918103285Sikob * To free transaction label.
919103285Sikob */
920106790Ssimokawastatic void
921106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
922103285Sikob{
923169119Ssimokawa	struct fw_xfer *txfer;
924169119Ssimokawa	int s;
925103285Sikob
926169119Ssimokawa	if (xfer->tl < 0)
927169119Ssimokawa		return;
928169119Ssimokawa
929169119Ssimokawa	s = splfw();
930169119Ssimokawa#if 1	/* make sure the label is allocated */
931169119Ssimokawa	STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
932169119Ssimokawa		if(txfer == xfer)
933169119Ssimokawa			break;
934169119Ssimokawa	if (txfer == NULL) {
935169119Ssimokawa		printf("%s: the xfer is not in the tlabel(%d)\n",
936169119Ssimokawa		    __FUNCTION__, xfer->tl);
937169119Ssimokawa		splx(s);
938169119Ssimokawa		return;
939103285Sikob	}
940169119Ssimokawa#endif
941169119Ssimokawa
942169119Ssimokawa	STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
943103285Sikob	splx(s);
944103285Sikob	return;
945103285Sikob}
946106790Ssimokawa
947103285Sikob/*
948103285Sikob * To obtain XFER structure by transaction label.
949103285Sikob */
950106790Ssimokawastatic struct fw_xfer *
951106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
952103285Sikob{
953103285Sikob	struct fw_xfer *xfer;
954103285Sikob	int s = splfw();
955103285Sikob
956169119Ssimokawa	STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
957169119Ssimokawa		if(xfer->send.hdr.mode.hdr.dst == node) {
958103285Sikob			splx(s);
959110577Ssimokawa			if (firewire_debug > 2)
960110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
961103285Sikob			return(xfer);
962103285Sikob		}
963110577Ssimokawa	if (firewire_debug > 1)
964110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
965103285Sikob	splx(s);
966103285Sikob	return(NULL);
967103285Sikob}
968106790Ssimokawa
969103285Sikob/*
970103285Sikob * To allocate IEEE1394 XFER structure.
971103285Sikob */
972106790Ssimokawastruct fw_xfer *
973110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
974103285Sikob{
975103285Sikob	struct fw_xfer *xfer;
976106790Ssimokawa
977110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
978106790Ssimokawa	if (xfer == NULL)
979106790Ssimokawa		return xfer;
980106790Ssimokawa
981110269Ssimokawa	xfer->malloc = type;
982106790Ssimokawa
983103285Sikob	return xfer;
984103285Sikob}
985106790Ssimokawa
986113584Ssimokawastruct fw_xfer *
987113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
988113584Ssimokawa{
989113584Ssimokawa	struct fw_xfer *xfer;
990113584Ssimokawa
991113584Ssimokawa	xfer = fw_xfer_alloc(type);
992126102Scperciva	if (xfer == NULL)
993126102Scperciva		return(NULL);
994120660Ssimokawa	xfer->send.pay_len = send_len;
995120660Ssimokawa	xfer->recv.pay_len = recv_len;
996120660Ssimokawa	if (send_len > 0) {
997120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
998120660Ssimokawa		if (xfer->send.payload == NULL) {
999113584Ssimokawa			fw_xfer_free(xfer);
1000113584Ssimokawa			return(NULL);
1001113584Ssimokawa		}
1002113584Ssimokawa	}
1003120660Ssimokawa	if (recv_len > 0) {
1004120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1005120660Ssimokawa		if (xfer->recv.payload == NULL) {
1006120660Ssimokawa			if (xfer->send.payload != NULL)
1007120660Ssimokawa				free(xfer->send.payload, type);
1008113584Ssimokawa			fw_xfer_free(xfer);
1009113584Ssimokawa			return(NULL);
1010113584Ssimokawa		}
1011113584Ssimokawa	}
1012113584Ssimokawa	return(xfer);
1013113584Ssimokawa}
1014113584Ssimokawa
1015103285Sikob/*
1016103285Sikob * IEEE1394 XFER post process.
1017103285Sikob */
1018103285Sikobvoid
1019103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1020103285Sikob{
1021167632Ssimokawa	if (xfer->hand == NULL) {
1022167632Ssimokawa		printf("hand == NULL\n");
1023103285Sikob		return;
1024117716Ssimokawa	}
1025103285Sikob
1026121505Ssimokawa	if (xfer->fc == NULL)
1027121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
1028121505Ssimokawa
1029168051Ssimokawa	fw_tl_free(xfer->fc, xfer);
1030167632Ssimokawa	xfer->hand(xfer);
1031103285Sikob}
1032103285Sikob
1033106790Ssimokawavoid
1034113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
1035103285Sikob{
1036103285Sikob	int s;
1037113584Ssimokawa
1038103285Sikob	if(xfer == NULL ) return;
1039103285Sikob	if(xfer->state == FWXF_INQ){
1040103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1041103285Sikob		s = splfw();
1042103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1043103285Sikob		xfer->q->queued --;
1044103285Sikob		splx(s);
1045103285Sikob	}
1046113584Ssimokawa	if (xfer->fc != NULL) {
1047114729Ssimokawa#if 1
1048113584Ssimokawa		if(xfer->state == FWXF_START)
1049114729Ssimokawa			/*
1050114729Ssimokawa			 * This could happen if:
1051114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1052114729Ssimokawa			 *  2. firewire_watch() is called.
1053114729Ssimokawa			 */
1054114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1055103285Sikob#endif
1056103285Sikob	}
1057113584Ssimokawa	xfer->state = FWXF_INIT;
1058113584Ssimokawa	xfer->resp = 0;
1059113584Ssimokawa}
1060113584Ssimokawa/*
1061113584Ssimokawa * To free IEEE1394 XFER structure.
1062113584Ssimokawa */
1063113584Ssimokawavoid
1064120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer)
1065113584Ssimokawa{
1066120660Ssimokawa	if (xfer == NULL) {
1067127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1068120660Ssimokawa		return;
1069120660Ssimokawa	}
1070113584Ssimokawa	fw_xfer_unload(xfer);
1071120660Ssimokawa	if(xfer->send.payload != NULL){
1072120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1073103285Sikob	}
1074120660Ssimokawa	if(xfer->recv.payload != NULL){
1075120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1076103285Sikob	}
1077110269Ssimokawa	free(xfer, xfer->malloc);
1078103285Sikob}
1079103285Sikob
1080120660Ssimokawavoid
1081120660Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1082120660Ssimokawa{
1083120660Ssimokawa	if (xfer == NULL) {
1084127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1085120660Ssimokawa		return;
1086120660Ssimokawa	}
1087120660Ssimokawa	fw_xfer_unload(xfer);
1088120660Ssimokawa	free(xfer, xfer->malloc);
1089120660Ssimokawa}
1090120660Ssimokawa
1091120660Ssimokawavoid
1092110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1093103285Sikob{
1094103285Sikob#if 0
1095110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1096103285Sikob				xfer->state, xfer->resp);
1097103285Sikob#endif
1098103285Sikob	fw_xfer_free(xfer);
1099103285Sikob}
1100103285Sikob
1101103285Sikob/*
1102103285Sikob * To configure PHY.
1103103285Sikob */
1104103285Sikobstatic void
1105103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1106103285Sikob{
1107103285Sikob	struct fw_xfer *xfer;
1108103285Sikob	struct fw_pkt *fp;
1109103285Sikob
1110103285Sikob	fc->status = FWBUSPHYCONF;
1111103285Sikob
1112120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1113113584Ssimokawa	if (xfer == NULL)
1114113584Ssimokawa		return;
1115103285Sikob	xfer->fc = fc;
1116167632Ssimokawa	xfer->hand = fw_asy_callback_free;
1117103285Sikob
1118120660Ssimokawa	fp = &xfer->send.hdr;
1119103285Sikob	fp->mode.ld[1] = 0;
1120103285Sikob	if (root_node >= 0)
1121113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1122103285Sikob	if (gap_count >= 0)
1123113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1124103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1125103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1126103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1127103285Sikob
1128107653Ssimokawa	if (firewire_debug)
1129107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1130103285Sikob						root_node, gap_count);
1131103285Sikob	fw_asyreq(fc, -1, xfer);
1132103285Sikob}
1133103285Sikob
1134103285Sikob#if 0
1135103285Sikob/*
1136103285Sikob * Dump self ID.
1137103285Sikob */
1138103285Sikobstatic void
1139129585Sdfrfw_print_sid(uint32_t sid)
1140103285Sikob{
1141103285Sikob	union fw_self_id *s;
1142103285Sikob	s = (union fw_self_id *) &sid;
1143103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1144103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1145103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1146103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1147103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1148103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1149103285Sikob}
1150103285Sikob#endif
1151103285Sikob
1152103285Sikob/*
1153103285Sikob * To receive self ID.
1154103285Sikob */
1155129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1156103285Sikob{
1157129585Sdfr	uint32_t *p;
1158103285Sikob	union fw_self_id *self_id;
1159103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1160103285Sikob
1161129585Sdfr	fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1162103285Sikob	fc->status = FWBUSINIT;
1163103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1164129585Sdfr	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1165103285Sikob	fc->status = FWBUSCYMELECT;
1166103285Sikob	fc->topology_map->crc_len = 2;
1167103285Sikob	fc->topology_map->generation ++;
1168103285Sikob	fc->topology_map->self_id_count = 0;
1169103285Sikob	fc->topology_map->node_count = 0;
1170103285Sikob	fc->speed_map->generation ++;
1171103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1172103285Sikob	self_id = &fc->topology_map->self_id[0];
1173103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1174103285Sikob		if (sid[1] != ~sid[0]) {
1175103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1176103285Sikob			sid += 2;
1177103285Sikob			continue;
1178103285Sikob		}
1179103285Sikob		*self_id = *((union fw_self_id *)sid);
1180103285Sikob		fc->topology_map->crc_len++;
1181103285Sikob		if(self_id->p0.sequel == 0){
1182103285Sikob			fc->topology_map->node_count ++;
1183103285Sikob			c_port = 0;
1184103285Sikob#if 0
1185103285Sikob			fw_print_sid(sid[0]);
1186103285Sikob#endif
1187103285Sikob			node = self_id->p0.phy_id;
1188103285Sikob			if(fc->max_node < node){
1189103285Sikob				fc->max_node = self_id->p0.phy_id;
1190103285Sikob			}
1191103285Sikob			/* XXX I'm not sure this is the right speed_map */
1192103285Sikob			fc->speed_map->speed[node][node]
1193103285Sikob					= self_id->p0.phy_speed;
1194103285Sikob			for (j = 0; j < node; j ++) {
1195103285Sikob				fc->speed_map->speed[j][node]
1196103285Sikob					= fc->speed_map->speed[node][j]
1197103285Sikob					= min(fc->speed_map->speed[j][j],
1198103285Sikob							self_id->p0.phy_speed);
1199103285Sikob			}
1200103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1201103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1202103285Sikob				fc->irm = self_id->p0.phy_id;
1203103285Sikob			}
1204103285Sikob			if(self_id->p0.port0 >= 0x2){
1205103285Sikob				c_port++;
1206103285Sikob			}
1207103285Sikob			if(self_id->p0.port1 >= 0x2){
1208103285Sikob				c_port++;
1209103285Sikob			}
1210103285Sikob			if(self_id->p0.port2 >= 0x2){
1211103285Sikob				c_port++;
1212103285Sikob			}
1213103285Sikob		}
1214103285Sikob		if(c_port > 2){
1215103285Sikob			i_branch += (c_port - 2);
1216103285Sikob		}
1217103285Sikob		sid += 2;
1218103285Sikob		self_id++;
1219103285Sikob		fc->topology_map->self_id_count ++;
1220103285Sikob	}
1221108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1222103285Sikob	/* CRC */
1223103285Sikob	fc->topology_map->crc = fw_crc16(
1224129585Sdfr			(uint32_t *)&fc->topology_map->generation,
1225103285Sikob			fc->topology_map->crc_len * 4);
1226103285Sikob	fc->speed_map->crc = fw_crc16(
1227129585Sdfr			(uint32_t *)&fc->speed_map->generation,
1228103285Sikob			fc->speed_map->crc_len * 4);
1229103285Sikob	/* byteswap and copy to CSR */
1230129585Sdfr	p = (uint32_t *)fc->topology_map;
1231103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1232103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1233129585Sdfr	p = (uint32_t *)fc->speed_map;
1234103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1235103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1236129585Sdfr	/* don't byte-swap uint8_t array */
1237103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1238103285Sikob
1239103285Sikob	fc->max_hop = fc->max_node - i_branch;
1240103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1241103285Sikob
1242103285Sikob	if(fc->irm == -1 ){
1243103285Sikob		printf(", Not found IRM capable node");
1244103285Sikob	}else{
1245103285Sikob		printf(", cable IRM = %d", fc->irm);
1246103285Sikob		if (fc->irm == fc->nodeid)
1247110016Ssimokawa			printf(" (me)");
1248103285Sikob	}
1249110016Ssimokawa	printf("\n");
1250103285Sikob
1251109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1252114909Ssimokawa		if (fc->irm == fc->nodeid) {
1253103285Sikob			fc->status = FWBUSMGRDONE;
1254103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1255114909Ssimokawa			fw_bmr(fc);
1256109736Ssimokawa		} else {
1257103285Sikob			fc->status = FWBUSMGRELECT;
1258110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1259110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1260103285Sikob		}
1261114909Ssimokawa	} else
1262103285Sikob		fc->status = FWBUSMGRDONE;
1263114909Ssimokawa
1264108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1265108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1266103285Sikob}
1267106790Ssimokawa
1268103285Sikob/*
1269103285Sikob * To probe devices on the IEEE1394 bus.
1270103285Sikob */
1271106790Ssimokawastatic void
1272106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1273103285Sikob{
1274103285Sikob	int s;
1275120850Ssimokawa	struct fw_device *fwdev;
1276103285Sikob
1277103285Sikob	s = splfw();
1278103285Sikob	fc->status = FWBUSEXPLORE;
1279103285Sikob
1280120850Ssimokawa	/* Invalidate all devices, just after bus reset. */
1281120850Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1282110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1283103285Sikob			fwdev->status = FWDEVINVAL;
1284103285Sikob			fwdev->rcnt = 0;
1285103285Sikob		}
1286120850Ssimokawa
1287103285Sikob	fc->ongonode = 0;
1288103285Sikob	fc->ongoaddr = CSRROMOFF;
1289103285Sikob	fc->ongodev = NULL;
1290103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1291103285Sikob	fw_bus_explore(fc);
1292103285Sikob	splx(s);
1293103285Sikob}
1294106790Ssimokawa
1295103285Sikob/*
1296129274Sdfr * Find the self_id packet for a node, ignoring sequels.
1297129274Sdfr */
1298129274Sdfrstatic union fw_self_id *
1299129274Sdfrfw_find_self_id(struct firewire_comm *fc, int node)
1300129274Sdfr{
1301129274Sdfr	uint32_t i;
1302129274Sdfr	union fw_self_id *s;
1303129274Sdfr
1304129274Sdfr	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1305129274Sdfr		s = &fc->topology_map->self_id[i];
1306129274Sdfr		if (s->p0.sequel)
1307129274Sdfr			continue;
1308129274Sdfr		if (s->p0.phy_id == node)
1309129274Sdfr			return s;
1310129274Sdfr	}
1311129274Sdfr	return 0;
1312129274Sdfr}
1313129274Sdfr
1314129274Sdfr/*
1315103285Sikob * To collect device informations on the IEEE1394 bus.
1316103285Sikob */
1317106790Ssimokawastatic void
1318106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1319103285Sikob{
1320103285Sikob	int err = 0;
1321110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1322129585Sdfr	uint32_t addr;
1323103285Sikob	struct fw_xfer *xfer;
1324103285Sikob	struct fw_pkt *fp;
1325152792Ssimokawa	union fw_self_id *fwsid;
1326103285Sikob
1327103285Sikob	if(fc->status != FWBUSEXPLORE)
1328103285Sikob		return;
1329103285Sikob
1330103285Sikobloop:
1331103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1332103285Sikob
1333103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1334103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1335103285Sikob
1336103285Sikob	/* check link */
1337103285Sikob	/* XXX we need to check phy_id first */
1338152792Ssimokawa	fwsid = fw_find_self_id(fc, fc->ongonode);
1339152792Ssimokawa	if (!fwsid || !fwsid->p0.link_active) {
1340110577Ssimokawa		if (firewire_debug)
1341110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1342103285Sikob		fc->ongonode++;
1343103285Sikob		goto loop;
1344103285Sikob	}
1345103285Sikob
1346103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1347103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1348103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1349103285Sikob		fc->ongoaddr = CSRROMOFF;
1350103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1351103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1352103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1353103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1354103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1355103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1356103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1357103285Sikob	}else if(fc->ongodev == NULL){
1358110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1359110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1360103285Sikob				break;
1361103285Sikob		if(fwdev != NULL){
1362103285Sikob			fwdev->dst = fc->ongonode;
1363117350Ssimokawa			fwdev->status = FWDEVINIT;
1364117350Ssimokawa			fc->ongodev = fwdev;
1365103285Sikob			fc->ongoaddr = CSRROMOFF;
1366117350Ssimokawa			addr = 0xf0000000 | fc->ongoaddr;
1367117350Ssimokawa			goto dorequest;
1368103285Sikob		}
1369115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1370115787Ssimokawa							M_NOWAIT | M_ZERO);
1371103285Sikob		if(fwdev == NULL)
1372103285Sikob			return;
1373106810Ssimokawa		fwdev->fc = fc;
1374103285Sikob		fwdev->rommax = 0;
1375103285Sikob		fwdev->dst = fc->ongonode;
1376103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1377103285Sikob		fwdev->status = FWDEVINIT;
1378103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1379103285Sikob
1380110179Ssimokawa		pfwdev = NULL;
1381110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1382110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1383110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1384110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1385110179Ssimokawa				break;
1386110179Ssimokawa			pfwdev = tfwdev;
1387103285Sikob		}
1388110179Ssimokawa		if (pfwdev == NULL)
1389110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1390110179Ssimokawa		else
1391110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1392103285Sikob
1393108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1394107653Ssimokawa			linkspeed[fwdev->speed],
1395107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1396103285Sikob
1397103285Sikob		fc->ongodev = fwdev;
1398103285Sikob		fc->ongoaddr = CSRROMOFF;
1399103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1400103285Sikob	}else{
1401103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1402103285Sikob	}
1403117350Ssimokawadorequest:
1404103285Sikob#if 0
1405103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1406103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1407103285Sikob		fw_bus_explore_callback);
1408103285Sikob	if(xfer == NULL) goto done;
1409103285Sikob#else
1410120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1411103285Sikob	if(xfer == NULL){
1412103285Sikob		goto done;
1413103285Sikob	}
1414120660Ssimokawa	xfer->send.spd = 0;
1415120660Ssimokawa	fp = &xfer->send.hdr;
1416113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1417103285Sikob	fp->mode.rreqq.tlrt = 0;
1418103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1419103285Sikob	fp->mode.rreqq.pri = 0;
1420103285Sikob	fp->mode.rreqq.src = 0;
1421120660Ssimokawa	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1422113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1423167632Ssimokawa	xfer->hand = fw_bus_explore_callback;
1424103285Sikob
1425110577Ssimokawa	if (firewire_debug)
1426110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1427110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1428103285Sikob	err = fw_asyreq(fc, -1, xfer);
1429103285Sikob	if(err){
1430103285Sikob		fw_xfer_free( xfer);
1431103285Sikob		return;
1432103285Sikob	}
1433103285Sikob#endif
1434103285Sikob	return;
1435103285Sikobdone:
1436103285Sikob	/* fw_attach_devs */
1437103285Sikob	fc->status = FWBUSEXPDONE;
1438107653Ssimokawa	if (firewire_debug)
1439107653Ssimokawa		printf("bus_explore done\n");
1440103285Sikob	fw_attach_dev(fc);
1441103285Sikob	return;
1442103285Sikob
1443103285Sikob}
1444106790Ssimokawa
1445103285Sikob/* Portable Async. request read quad */
1446106790Ssimokawastruct fw_xfer *
1447129585Sdfrasyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt,
1448129585Sdfr	uint32_t addr_hi, uint32_t addr_lo,
1449124169Ssimokawa	void (*hand) (struct fw_xfer*))
1450103285Sikob{
1451103285Sikob	struct fw_xfer *xfer;
1452103285Sikob	struct fw_pkt *fp;
1453103285Sikob	int err;
1454103285Sikob
1455120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1456113584Ssimokawa	if (xfer == NULL)
1457103285Sikob		return NULL;
1458113584Ssimokawa
1459120660Ssimokawa	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1460120660Ssimokawa	fp = &xfer->send.hdr;
1461113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1462103285Sikob	if(tl & FWP_TL_VALID){
1463103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1464103285Sikob	}else{
1465103285Sikob		fp->mode.rreqq.tlrt = 0;
1466103285Sikob	}
1467103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1468103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1469103285Sikob	fp->mode.rreqq.pri = 0;
1470103285Sikob	fp->mode.rreqq.src = 0;
1471120660Ssimokawa	fp->mode.rreqq.dst = addr_hi >> 16;
1472113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1473167632Ssimokawa	xfer->hand = hand;
1474103285Sikob
1475103285Sikob	err = fw_asyreq(fc, -1, xfer);
1476103285Sikob	if(err){
1477103285Sikob		fw_xfer_free( xfer);
1478103285Sikob		return NULL;
1479103285Sikob	}
1480103285Sikob	return xfer;
1481103285Sikob}
1482106790Ssimokawa
1483103285Sikob/*
1484103285Sikob * Callback for the IEEE1394 bus information collection.
1485103285Sikob */
1486106790Ssimokawastatic void
1487106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1488106790Ssimokawa{
1489103285Sikob	struct firewire_comm *fc;
1490103285Sikob	struct fw_pkt *sfp,*rfp;
1491103285Sikob	struct csrhdr *chdr;
1492103285Sikob	struct csrdir *csrd;
1493103285Sikob	struct csrreg *csrreg;
1494129585Sdfr	uint32_t offset;
1495103285Sikob
1496103285Sikob
1497110577Ssimokawa	if(xfer == NULL) {
1498110577Ssimokawa		printf("xfer == NULL\n");
1499110577Ssimokawa		return;
1500110577Ssimokawa	}
1501103285Sikob	fc = xfer->fc;
1502110577Ssimokawa
1503110577Ssimokawa	if (firewire_debug)
1504110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1505110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1506110577Ssimokawa
1507103285Sikob	if(xfer->resp != 0){
1508127468Ssimokawa		device_printf(fc->bdev,
1509167630Ssimokawa		    "bus_explore node=%d addr=0x%x resp=%d\n",
1510167630Ssimokawa		    fc->ongonode, fc->ongoaddr, xfer->resp);
1511114284Ssimokawa		goto errnode;
1512103285Sikob	}
1513103285Sikob
1514120660Ssimokawa	sfp = &xfer->send.hdr;
1515120660Ssimokawa	rfp = &xfer->recv.hdr;
1516103285Sikob#if 0
1517103285Sikob	{
1518129585Sdfr		uint32_t *qld;
1519103285Sikob		int i;
1520129585Sdfr		qld = (uint32_t *)xfer->recv.buf;
1521103285Sikob		printf("len:%d\n", xfer->recv.len);
1522103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1523113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1524103285Sikob			if((i % 16) == 15) printf("\n");
1525103285Sikob		}
1526103285Sikob		if((i % 16) != 15) printf("\n");
1527103285Sikob	}
1528103285Sikob#endif
1529103285Sikob	if(fc->ongodev == NULL){
1530113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1531103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1532103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1533129541Sdfr/* If CSR is minimal confinguration, more investigation is not needed. */
1534103285Sikob			if(chdr->info_len == 1){
1535110577Ssimokawa				if (firewire_debug)
1536110577Ssimokawa					printf("node%d: minimal config\n",
1537110577Ssimokawa								fc->ongonode);
1538103285Sikob				goto nextnode;
1539103285Sikob			}else{
1540103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1541103285Sikob			}
1542113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1543103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1544103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1545113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1546103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1547110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1548110577Ssimokawa				if (firewire_debug)
1549110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1550110577Ssimokawa							fc->ongonode);
1551103285Sikob				goto nextnode;
1552110577Ssimokawa			}
1553103285Sikob			fc->ongoaddr = CSRROMOFF;
1554103285Sikob		}
1555103285Sikob	}else{
1556117350Ssimokawa		if (fc->ongoaddr == CSRROMOFF &&
1557117350Ssimokawa		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1558117350Ssimokawa			fc->ongodev->status = FWDEVATTACHED;
1559117350Ssimokawa			goto nextnode;
1560117350Ssimokawa		}
1561103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1562103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1563103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1564103285Sikob		}
1565103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1566103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1567103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1568103285Sikob			offset = CSRROMOFF;
1569103285Sikob		}else{
1570103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1571103285Sikob			offset = csrd->off;
1572103285Sikob		}
1573103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1574103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1575103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1576103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1577103285Sikob				if(csrd == NULL){
1578103285Sikob					goto nextnode;
1579103285Sikob				}else{
1580103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1581103285Sikob					fc->ongoaddr += csrreg->val * 4;
1582103285Sikob					csrd->off = fc->ongoaddr;
1583103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1584103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1585103285Sikob					goto nextaddr;
1586103285Sikob				}
1587103285Sikob			}
1588103285Sikob		}
1589103285Sikob		fc->ongoaddr += 4;
1590103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1591103285Sikob				(fc->ongodev->rommax < 0x414)){
1592103285Sikob			if(fc->ongodev->rommax <= 0x414){
1593103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1594103285Sikob				if(csrd == NULL) goto nextnode;
1595103285Sikob				csrd->off = fc->ongoaddr;
1596103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1597103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1598103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1599103285Sikob			}
1600103285Sikob			goto nextaddr;
1601103285Sikob		}
1602103285Sikob
1603103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1604103285Sikob			if(csrd == NULL){
1605103285Sikob				goto nextnode;
1606103285Sikob			};
1607103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1608103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1609103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1610103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1611103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1612103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1613103285Sikob				offset = CSRROMOFF;
1614103285Sikob			}else{
1615103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1616103285Sikob				offset = csrd->off;
1617103285Sikob			}
1618103285Sikob		}
1619103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1620103285Sikob			goto nextnode;
1621103285Sikob		}
1622103285Sikob	}
1623103285Sikobnextaddr:
1624103285Sikob	fw_xfer_free( xfer);
1625103285Sikob	fw_bus_explore(fc);
1626103285Sikob	return;
1627114284Ssimokawaerrnode:
1628127468Ssimokawa	if (fc->ongodev != NULL) {
1629114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1630127468Ssimokawa		/* Invalidate ROM */
1631127468Ssimokawa		fc->ongodev->csrrom[0] = 0;
1632127468Ssimokawa	}
1633103285Sikobnextnode:
1634103285Sikob	fw_xfer_free( xfer);
1635103285Sikob	fc->ongonode++;
1636103285Sikob/* housekeeping work space */
1637103285Sikob	fc->ongoaddr = CSRROMOFF;
1638103285Sikob	fc->ongodev = NULL;
1639103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1640103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1641103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1642103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1643103285Sikob	}
1644103285Sikob	fw_bus_explore(fc);
1645103285Sikob	return;
1646103285Sikob}
1647103285Sikob
1648103285Sikob/*
1649103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1650103285Sikob */
1651106815Ssimokawastatic void
1652106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1653103285Sikob{
1654120850Ssimokawa	struct fw_device *fwdev, *next;
1655103285Sikob	int i, err;
1656103285Sikob	device_t *devlistp;
1657103285Sikob	int devcnt;
1658103285Sikob	struct firewire_dev_comm *fdc;
1659103285Sikob
1660120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1661120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1662120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1663114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1664120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1665120850Ssimokawa			fwdev->rcnt ++;
1666120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1667120850Ssimokawa				/*
1668120850Ssimokawa				 * Remove devices which have not been seen
1669120850Ssimokawa				 * for a while.
1670120850Ssimokawa				 */
1671120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1672120850Ssimokawa				    link);
1673120850Ssimokawa				free(fwdev, M_FW);
1674120850Ssimokawa			}
1675120850Ssimokawa		}
1676120850Ssimokawa	}
1677103285Sikob
1678108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1679103285Sikob	if( err != 0 )
1680103285Sikob		return;
1681103285Sikob	for( i = 0 ; i < devcnt ; i++){
1682103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1683103285Sikob			fdc = device_get_softc(devlistp[i]);
1684103285Sikob			if (fdc->post_explore != NULL)
1685103285Sikob				fdc->post_explore(fdc);
1686103285Sikob		}
1687103285Sikob	}
1688103285Sikob	free(devlistp, M_TEMP);
1689103285Sikob
1690103285Sikob	return;
1691103285Sikob}
1692106815Ssimokawa
1693103285Sikob/*
1694129541Sdfr * To allocate unique transaction label.
1695103285Sikob */
1696106815Ssimokawastatic int
1697106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1698103285Sikob{
1699103285Sikob	u_int i;
1700169119Ssimokawa	struct fw_xfer *txfer;
1701103285Sikob	int s;
1702129585Sdfr	static uint32_t label = 0;
1703103285Sikob
1704103285Sikob	s = splfw();
1705103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1706103285Sikob		label = (label + 1) & 0x3f;
1707169119Ssimokawa		STAILQ_FOREACH(txfer, &fc->tlabels[label], tlabel)
1708169119Ssimokawa			if (txfer->send.hdr.mode.hdr.dst ==
1709120660Ssimokawa			    xfer->send.hdr.mode.hdr.dst)
1710120660Ssimokawa				break;
1711169119Ssimokawa		if(txfer == NULL) {
1712169119Ssimokawa			STAILQ_INSERT_TAIL(&fc->tlabels[label], xfer, tlabel);
1713103285Sikob			splx(s);
1714110577Ssimokawa			if (firewire_debug > 1)
1715110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1716120660Ssimokawa				    xfer->send.hdr.mode.hdr.dst, label);
1717103285Sikob			return(label);
1718103285Sikob		}
1719103285Sikob	}
1720103285Sikob	splx(s);
1721103285Sikob
1722130460Sdfr	if (firewire_debug > 1)
1723130460Sdfr		printf("fw_get_tlabel: no free tlabel\n");
1724103285Sikob	return(-1);
1725103285Sikob}
1726106815Ssimokawa
1727113584Ssimokawastatic void
1728120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1729113584Ssimokawa{
1730120660Ssimokawa	struct fw_pkt *pkt;
1731120660Ssimokawa	u_char *p;
1732120660Ssimokawa	struct tcode_info *tinfo;
1733120660Ssimokawa	u_int res, i, len, plen;
1734113584Ssimokawa
1735169127Ssimokawa	rb->xfer->recv.spd = rb->spd;
1736120660Ssimokawa
1737120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1738120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1739120660Ssimokawa
1740120660Ssimokawa	/* Copy header */
1741120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1742120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1743132771Skan	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1744120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1745120660Ssimokawa
1746120660Ssimokawa	/* Copy payload */
1747120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1748120660Ssimokawa	res = rb->xfer->recv.pay_len;
1749120660Ssimokawa
1750120660Ssimokawa	/* special handling for RRESQ */
1751120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1752129585Sdfr	    p != NULL && res >= sizeof(uint32_t)) {
1753129585Sdfr		*(uint32_t *)p = pkt->mode.rresq.data;
1754129585Sdfr		rb->xfer->recv.pay_len = sizeof(uint32_t);
1755120660Ssimokawa		return;
1756120660Ssimokawa	}
1757120660Ssimokawa
1758120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1759120660Ssimokawa		return;
1760120660Ssimokawa
1761120660Ssimokawa	plen = pkt->mode.rresb.len;
1762120660Ssimokawa
1763120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1764120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1765113584Ssimokawa		if (res < len) {
1766113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1767120660Ssimokawa			    rb->xfer->recv.pay_len, len - res);
1768113584Ssimokawa			len = res;
1769113584Ssimokawa		}
1770120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1771113584Ssimokawa		p += len;
1772113584Ssimokawa		res -= len;
1773120660Ssimokawa		plen -= len;
1774120660Ssimokawa		if (res == 0 || plen == 0)
1775113584Ssimokawa			break;
1776113584Ssimokawa	}
1777120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1778120660Ssimokawa
1779113584Ssimokawa}
1780113584Ssimokawa
1781103285Sikob/*
1782129541Sdfr * Generic packet receiving process.
1783103285Sikob */
1784106815Ssimokawavoid
1785120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1786103285Sikob{
1787103285Sikob	struct fw_pkt *fp, *resfp;
1788103285Sikob	struct fw_bind *bind;
1789169130Ssimokawa	int tcode;
1790113584Ssimokawa	int i, len, oldstate;
1791103285Sikob#if 0
1792103285Sikob	{
1793129585Sdfr		uint32_t *qld;
1794103285Sikob		int i;
1795129585Sdfr		qld = (uint32_t *)buf;
1796103285Sikob		printf("spd %d len:%d\n", spd, len);
1797103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1798103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1799103285Sikob			if((i % 16) == 15) printf("\n");
1800103285Sikob		}
1801103285Sikob		if((i % 16) != 15) printf("\n");
1802103285Sikob	}
1803103285Sikob#endif
1804120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1805113584Ssimokawa	tcode = fp->mode.common.tcode;
1806113584Ssimokawa	switch (tcode) {
1807103285Sikob	case FWTCODE_WRES:
1808103285Sikob	case FWTCODE_RRESQ:
1809103285Sikob	case FWTCODE_RRESB:
1810103285Sikob	case FWTCODE_LRES:
1811120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1812103285Sikob					fp->mode.hdr.tlrt >> 2);
1813120660Ssimokawa		if(rb->xfer == NULL) {
1814103285Sikob			printf("fw_rcv: unknown response "
1815124251Ssimokawa			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1816124251Ssimokawa			    tcode_str[tcode], tcode,
1817124251Ssimokawa			    fp->mode.hdr.src,
1818124251Ssimokawa			    fp->mode.hdr.tlrt >> 2,
1819124251Ssimokawa			    fp->mode.hdr.tlrt & 3,
1820124251Ssimokawa			    fp->mode.rresq.data);
1821103285Sikob#if 1
1822103285Sikob			printf("try ad-hoc work around!!\n");
1823120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1824103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1825120660Ssimokawa			if (rb->xfer == NULL) {
1826103285Sikob				printf("no use...\n");
1827103285Sikob				goto err;
1828103285Sikob			}
1829103285Sikob#else
1830103285Sikob			goto err;
1831103285Sikob#endif
1832103285Sikob		}
1833120660Ssimokawa		fw_rcv_copy(rb);
1834120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1835120660Ssimokawa			rb->xfer->resp = EIO;
1836120660Ssimokawa		else
1837120660Ssimokawa			rb->xfer->resp = 0;
1838113584Ssimokawa		/* make sure the packet is drained in AT queue */
1839120660Ssimokawa		oldstate = rb->xfer->state;
1840120660Ssimokawa		rb->xfer->state = FWXF_RCVD;
1841113584Ssimokawa		switch (oldstate) {
1842113584Ssimokawa		case FWXF_SENT:
1843120660Ssimokawa			fw_xfer_done(rb->xfer);
1844103285Sikob			break;
1845113584Ssimokawa		case FWXF_START:
1846119289Ssimokawa#if 0
1847113584Ssimokawa			if (firewire_debug)
1848120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1849119289Ssimokawa#endif
1850113584Ssimokawa			break;
1851103285Sikob		default:
1852120660Ssimokawa			printf("unexpected state %d\n", rb->xfer->state);
1853103285Sikob		}
1854113584Ssimokawa		return;
1855103285Sikob	case FWTCODE_WREQQ:
1856103285Sikob	case FWTCODE_WREQB:
1857103285Sikob	case FWTCODE_RREQQ:
1858103285Sikob	case FWTCODE_RREQB:
1859103285Sikob	case FWTCODE_LREQ:
1860120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1861113584Ssimokawa			fp->mode.rreqq.dest_lo);
1862103285Sikob		if(bind == NULL){
1863124251Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1864127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1865127468Ssimokawa			    " src=0x%x data=%lx\n",
1866127468Ssimokawa#else
1867124251Ssimokawa			    " src=0x%x data=%x\n",
1868113962Ssimokawa#endif
1869124251Ssimokawa			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1870124251Ssimokawa			    tcode_str[tcode], tcode,
1871124251Ssimokawa			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1872120660Ssimokawa			if (rb->fc->status == FWBUSRESET) {
1873110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1874103285Sikob				goto err;
1875103285Sikob			}
1876120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
1877120660Ssimokawa			if(rb->xfer == NULL){
1878103285Sikob				return;
1879103285Sikob			}
1880120660Ssimokawa			rb->xfer->send.spd = rb->spd;
1881120660Ssimokawa			rb->xfer->send.pay_len = 0;
1882120660Ssimokawa			resfp = &rb->xfer->send.hdr;
1883113584Ssimokawa			switch (tcode) {
1884103285Sikob			case FWTCODE_WREQQ:
1885103285Sikob			case FWTCODE_WREQB:
1886103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1887103285Sikob				break;
1888103285Sikob			case FWTCODE_RREQQ:
1889103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1890103285Sikob				break;
1891103285Sikob			case FWTCODE_RREQB:
1892103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1893103285Sikob				break;
1894103285Sikob			case FWTCODE_LREQ:
1895103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1896103285Sikob				break;
1897103285Sikob			}
1898103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1899103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1900103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1901120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1902103285Sikob			resfp->mode.rresb.extcode = 0;
1903103285Sikob			resfp->mode.rresb.len = 0;
1904103285Sikob/*
1905167632Ssimokawa			rb->xfer->hand = fw_asy_callback;
1906103285Sikob*/
1907167632Ssimokawa			rb->xfer->hand = fw_xfer_free;
1908120660Ssimokawa			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1909120660Ssimokawa				fw_xfer_free(rb->xfer);
1910103285Sikob				return;
1911103285Sikob			}
1912103285Sikob			goto err;
1913103285Sikob		}
1914113584Ssimokawa		len = 0;
1915120660Ssimokawa		for (i = 0; i < rb->nvec; i ++)
1916120660Ssimokawa			len += rb->vec[i].iov_len;
1917169130Ssimokawa		rb->xfer = STAILQ_FIRST(&bind->xferlist);
1918169130Ssimokawa		if (rb->xfer == NULL) {
1919169130Ssimokawa			printf("Discard a packet for this bind.\n");
1920103285Sikob			goto err;
1921103285Sikob		}
1922169130Ssimokawa		STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1923169130Ssimokawa		fw_rcv_copy(rb);
1924169130Ssimokawa		rb->xfer->hand(rb->xfer);
1925169130Ssimokawa		return;
1926120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
1927103285Sikob	case FWTCODE_STREAM:
1928103285Sikob	{
1929103285Sikob		struct fw_xferq *xferq;
1930103285Sikob
1931120660Ssimokawa		xferq = rb->fc->ir[sub];
1932103285Sikob#if 0
1933103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1934103285Sikob			sub, len, off, spd);
1935103285Sikob#endif
1936103285Sikob		if(xferq->queued >= xferq->maxq) {
1937103285Sikob			printf("receive queue is full\n");
1938103285Sikob			goto err;
1939103285Sikob		}
1940113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1941113584Ssimokawa			per packet mode */
1942120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1943113584Ssimokawa						vec[0].iov_len);
1944120660Ssimokawa		if (rb->xfer == NULL) goto err;
1945120660Ssimokawa		fw_rcv_copy(rb)
1946103285Sikob		s = splfw();
1947103285Sikob		xferq->queued++;
1948120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1949103285Sikob		splx(s);
1950120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
1951127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1952127468Ssimokawa		if (&xferq->rsel.si_pid != 0)
1953127468Ssimokawa#else
1954103285Sikob		if (SEL_WAITING(&xferq->rsel))
1955103285Sikob#endif
1956122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
1957103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1958103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1959103285Sikob			wakeup((caddr_t)xferq);
1960103285Sikob		}
1961103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1962103285Sikob			xferq->hand(xferq);
1963103285Sikob		}
1964103285Sikob		return;
1965103285Sikob		break;
1966103285Sikob	}
1967120660Ssimokawa#endif
1968103285Sikob	default:
1969113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1970103285Sikob		break;
1971103285Sikob	}
1972103285Sikoberr:
1973113584Ssimokawa	return;
1974103285Sikob}
1975106815Ssimokawa
1976103285Sikob/*
1977103285Sikob * Post process for Bus Manager election process.
1978103285Sikob */
1979103285Sikobstatic void
1980103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1981103285Sikob{
1982103285Sikob	struct firewire_comm *fc;
1983109422Ssimokawa	int bmr;
1984103285Sikob
1985109422Ssimokawa	if (xfer == NULL)
1986109422Ssimokawa		return;
1987103285Sikob	fc = xfer->fc;
1988109422Ssimokawa	if (xfer->resp != 0)
1989103285Sikob		goto error;
1990120660Ssimokawa	if (xfer->recv.payload == NULL)
1991103285Sikob		goto error;
1992120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
1993103285Sikob		goto error;
1994109422Ssimokawa
1995120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
1996109422Ssimokawa	if (bmr == 0x3f)
1997109422Ssimokawa		bmr = fc->nodeid;
1998109422Ssimokawa
1999109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2000120660Ssimokawa	fw_xfer_free_buf(xfer);
2001114909Ssimokawa	fw_bmr(fc);
2002114909Ssimokawa	return;
2003114909Ssimokawa
2004103285Sikoberror:
2005114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2006120660Ssimokawa	fw_xfer_free_buf(xfer);
2007103285Sikob}
2008106815Ssimokawa
2009113584Ssimokawa
2010103285Sikob/*
2011103285Sikob * To candidate Bus Manager election process.
2012103285Sikob */
2013110270Ssimokawastatic void
2014106815Ssimokawafw_try_bmr(void *arg)
2015103285Sikob{
2016103285Sikob	struct fw_xfer *xfer;
2017103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2018103285Sikob	struct fw_pkt *fp;
2019103285Sikob	int err = 0;
2020103285Sikob
2021120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2022103285Sikob	if(xfer == NULL){
2023103285Sikob		return;
2024103285Sikob	}
2025120660Ssimokawa	xfer->send.spd = 0;
2026103285Sikob	fc->status = FWBUSMGRELECT;
2027103285Sikob
2028120660Ssimokawa	fp = &xfer->send.hdr;
2029113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2030103285Sikob	fp->mode.lreq.tlrt = 0;
2031103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2032103285Sikob	fp->mode.lreq.pri = 0;
2033103285Sikob	fp->mode.lreq.src = 0;
2034113584Ssimokawa	fp->mode.lreq.len = 8;
2035120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2036120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2037113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2038120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2039120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2040167632Ssimokawa	xfer->hand = fw_try_bmr_callback;
2041103285Sikob
2042103285Sikob	err = fw_asyreq(fc, -1, xfer);
2043103285Sikob	if(err){
2044120660Ssimokawa		fw_xfer_free_buf(xfer);
2045103285Sikob		return;
2046103285Sikob	}
2047103285Sikob	return;
2048103285Sikob}
2049106543Ssimokawa
2050106543Ssimokawa#ifdef FW_VMACCESS
2051103285Sikob/*
2052103285Sikob * Software implementation for physical memory block access.
2053103285Sikob * XXX:Too slow, usef for debug purpose only.
2054103285Sikob */
2055106815Ssimokawastatic void
2056106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2057103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2058129585Sdfr	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2059103285Sikob
2060113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2061113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2062103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2063103285Sikob	if(xfer->resp != 0){
2064103285Sikob		fw_xfer_free( xfer);
2065103285Sikob		return;
2066103285Sikob	}
2067103285Sikob	if(xfer->recv.buf == NULL){
2068103285Sikob		fw_xfer_free( xfer);
2069103285Sikob		return;
2070103285Sikob	}
2071103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2072103285Sikob	switch(rfp->mode.hdr.tcode){
2073103285Sikob		/* XXX need fix for 64bit arch */
2074103285Sikob		case FWTCODE_WREQB:
2075110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2076103285Sikob			xfer->send.len = 12;
2077103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2078103285Sikob			bcopy(rfp->mode.wreqb.payload,
2079103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2080103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2081103285Sikob			sfp->mode.wres.rtcode = 0;
2082103285Sikob			break;
2083103285Sikob		case FWTCODE_WREQQ:
2084110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2085103285Sikob			xfer->send.len = 12;
2086103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2087129585Sdfr			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2088103285Sikob			sfp->mode.wres.rtcode = 0;
2089103285Sikob			break;
2090103285Sikob		case FWTCODE_RREQB:
2091110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2092103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2093103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2094103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2095129585Sdfr				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2096103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2097103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2098103285Sikob			sfp->mode.rresb.rtcode = 0;
2099103285Sikob			sfp->mode.rresb.extcode = 0;
2100103285Sikob			break;
2101103285Sikob		case FWTCODE_RREQQ:
2102110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2103103285Sikob			xfer->send.len = 16;
2104103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2105129585Sdfr			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2106103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2107103285Sikob			sfp->mode.rresb.rtcode = 0;
2108103285Sikob			break;
2109103285Sikob		default:
2110103285Sikob			fw_xfer_free( xfer);
2111103285Sikob			return;
2112103285Sikob	}
2113103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2114103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2115167632Ssimokawa	xfer->hand = fw_xfer_free;
2116103285Sikob
2117103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2118103285Sikob	sfp->mode.hdr.pri = 0;
2119103285Sikob
2120103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2121103285Sikob/**/
2122103285Sikob	return;
2123103285Sikob}
2124106543Ssimokawa#endif
2125106543Ssimokawa
2126103285Sikob/*
2127103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2128103285Sikob */
2129129585Sdfruint16_t
2130129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){
2131129585Sdfr	uint32_t i, sum, crc = 0;
2132103285Sikob	int shift;
2133103285Sikob	len = (len + 3) & ~3;
2134103285Sikob	for(i = 0 ; i < len ; i+= 4){
2135103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2136103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2137103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2138103285Sikob		}
2139103285Sikob		crc &= 0xffff;
2140103285Sikob	}
2141129585Sdfr	return((uint16_t) crc);
2142103285Sikob}
2143106815Ssimokawa
2144110270Ssimokawastatic int
2145110072Ssimokawafw_bmr(struct firewire_comm *fc)
2146110072Ssimokawa{
2147110072Ssimokawa	struct fw_device fwdev;
2148113584Ssimokawa	union fw_self_id *self_id;
2149110072Ssimokawa	int cmstr;
2150129585Sdfr	uint32_t quad;
2151110072Ssimokawa
2152113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2153129274Sdfr	self_id = fw_find_self_id(fc, fc->max_node);
2154113584Ssimokawa	if (fc->max_node > 0) {
2155114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2156114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2157113584Ssimokawa			cmstr = fc->max_node;
2158114909Ssimokawa		else {
2159114909Ssimokawa			device_printf(fc->bdev,
2160114909Ssimokawa				"root node is not cycle master capable\n");
2161114909Ssimokawa			/* XXX shall we be the cycle master? */
2162113584Ssimokawa			cmstr = fc->nodeid;
2163114909Ssimokawa			/* XXX need bus reset */
2164114909Ssimokawa		}
2165113584Ssimokawa	} else
2166113584Ssimokawa		cmstr = -1;
2167114909Ssimokawa
2168114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2169114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2170114909Ssimokawa		/* We are not the bus manager */
2171114909Ssimokawa		printf("\n");
2172114909Ssimokawa		return(0);
2173114909Ssimokawa	}
2174114909Ssimokawa	printf("(me)\n");
2175114909Ssimokawa
2176114909Ssimokawa	/* Optimize gapcount */
2177113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2178113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2179110072Ssimokawa	/* If we are the cycle master, nothing to do */
2180113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2181110072Ssimokawa		return 0;
2182110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2183110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2184110072Ssimokawa	fwdev.fc = fc;
2185110072Ssimokawa	fwdev.dst = cmstr;
2186110072Ssimokawa	fwdev.speed = 0;
2187110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2188110072Ssimokawa	fwdev.status = FWDEVINIT;
2189110072Ssimokawa	/* Set cmstr bit on the cycle master */
2190120660Ssimokawa	quad = htonl(1 << 8);
2191110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2192120660Ssimokawa		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2193110072Ssimokawa
2194110072Ssimokawa	return 0;
2195110072Ssimokawa}
2196110072Ssimokawa
2197118455Ssimokawastatic int
2198118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2199118455Ssimokawa{
2200118455Ssimokawa	int err = 0;
2201127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2202118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2203118455Ssimokawa#endif
2204118455Ssimokawa
2205118455Ssimokawa	switch (type) {
2206118455Ssimokawa	case MOD_LOAD:
2207127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2208118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2209118455Ssimokawa						fwdev_clone, 0, 1000);
2210118455Ssimokawa#endif
2211118455Ssimokawa		break;
2212118455Ssimokawa	case MOD_UNLOAD:
2213127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2214118455Ssimokawa		if (fwdev_ehtag != NULL)
2215118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2216118455Ssimokawa#endif
2217118455Ssimokawa		break;
2218118455Ssimokawa	case MOD_SHUTDOWN:
2219118455Ssimokawa		break;
2220132199Sphk	default:
2221132199Sphk		return (EOPNOTSUPP);
2222118455Ssimokawa	}
2223118455Ssimokawa	return (err);
2224118455Ssimokawa}
2225118455Ssimokawa
2226118455Ssimokawa
2227127468Ssimokawa#ifdef __DragonFly__
2228127468SsimokawaDECLARE_DUMMY_MODULE(firewire);
2229127468Ssimokawa#endif
2230118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2231103285SikobMODULE_VERSION(firewire, 1);
2232