firewire.c revision 169131
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 169131 2007-04-30 13:51:13Z 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;
499169131Ssimokawa		xfer->state = FWXF_SENTERR;
500113584Ssimokawa		fw_xfer_done(xfer);
501110577Ssimokawa	}
502110577Ssimokawa}
503110577Ssimokawa
504110798Ssimokawavoid
505110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
506110798Ssimokawa{
507110798Ssimokawa	int i;
508110798Ssimokawa
509110798Ssimokawa	fw_xferq_drain(fc->atq);
510110798Ssimokawa	fw_xferq_drain(fc->ats);
511110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
512110798Ssimokawa		fw_xferq_drain(fc->it[i]);
513110798Ssimokawa}
514110798Ssimokawa
515116376Ssimokawastatic void
516116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
517103285Sikob{
518103285Sikob	int i;
519103285Sikob
520103285Sikob	CSRARC(fc, STATE_CLEAR)
521103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
522103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
523103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
524103285Sikob
525103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
526103285Sikob	fc->irm = -1;
527103285Sikob
528103285Sikob	fc->max_node = -1;
529103285Sikob
530103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
531103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
532103285Sikob	}
533103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
534103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
535103285Sikob	CSRARC(fc, RESET_START) = 0;
536103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
537103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
538103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
539103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
540103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
541103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
542103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
543103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
544103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
545103285Sikob
546103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
547103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
548103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
549103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
550103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
551103285Sikob
552103285Sikob/* DV depend CSRs see blue book */
553103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
554103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
555103285Sikob
556103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
557103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
558116376Ssimokawa}
559113584Ssimokawa
560116376Ssimokawastatic void
561116376Ssimokawafw_init_crom(struct firewire_comm *fc)
562116376Ssimokawa{
563116376Ssimokawa	struct crom_src *src;
564116376Ssimokawa
565116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
566116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
567116376Ssimokawa	if (fc->crom_src_buf == NULL)
568116376Ssimokawa		return;
569116376Ssimokawa
570116376Ssimokawa	src = &fc->crom_src_buf->src;
571116376Ssimokawa	bzero(src, sizeof(struct crom_src));
572116376Ssimokawa
573116376Ssimokawa	/* BUS info sample */
574116376Ssimokawa	src->hdr.info_len = 4;
575116376Ssimokawa
576116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
577116376Ssimokawa
578116376Ssimokawa	src->businfo.irmc = 1;
579116376Ssimokawa	src->businfo.cmc = 1;
580116376Ssimokawa	src->businfo.isc = 1;
581116376Ssimokawa	src->businfo.bmc = 1;
582116376Ssimokawa	src->businfo.pmc = 0;
583116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
584116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
585116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
586117350Ssimokawa	src->businfo.generation = 1;
587116376Ssimokawa	src->businfo.link_spd = fc->speed;
588116376Ssimokawa
589116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
590116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
591116376Ssimokawa
592116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
593116376Ssimokawa
594116376Ssimokawa	fc->crom_src = src;
595116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
596116376Ssimokawa}
597116376Ssimokawa
598116376Ssimokawastatic void
599116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
600116376Ssimokawa{
601116376Ssimokawa	struct crom_src_buf *buf;
602116376Ssimokawa	struct crom_src *src;
603116376Ssimokawa	struct crom_chunk *root;
604116376Ssimokawa
605116376Ssimokawa	if (fc->crom_src_buf == NULL)
606116376Ssimokawa		fw_init_crom(fc);
607116376Ssimokawa
608116376Ssimokawa	buf =  fc->crom_src_buf;
609116376Ssimokawa	src = fc->crom_src;
610116376Ssimokawa	root = fc->crom_root;
611116376Ssimokawa
612116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
613116376Ssimokawa
614116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
615116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
616116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
617116376Ssimokawa	/* private company_id */
618116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
619127468Ssimokawa#ifdef __DragonFly__
620127468Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
621127468Ssimokawa	crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
622127468Ssimokawa#else
623116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
624116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
625127468Ssimokawa#endif
626116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
627116376Ssimokawa}
628116376Ssimokawa
629116376Ssimokawa/*
630116376Ssimokawa * Called after bus reset.
631116376Ssimokawa */
632116376Ssimokawavoid
633169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status)
634116376Ssimokawa{
635116376Ssimokawa	struct firewire_dev_comm *fdc;
636117350Ssimokawa	struct crom_src *src;
637116376Ssimokawa	device_t *devlistp;
638117350Ssimokawa	void *newrom;
639116376Ssimokawa	int i, devcnt;
640116376Ssimokawa
641116376Ssimokawa	switch(fc->status){
642116376Ssimokawa	case FWBUSMGRELECT:
643116376Ssimokawa		callout_stop(&fc->bmr_callout);
644116376Ssimokawa		break;
645116376Ssimokawa	default:
646116376Ssimokawa		break;
647116376Ssimokawa	}
648169117Ssimokawa	fc->status = new_status;
649116376Ssimokawa	fw_reset_csr(fc);
650116376Ssimokawa	fw_reset_crom(fc);
651116376Ssimokawa
652113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
653113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
654113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
655113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
656113584Ssimokawa				if (fdc->post_busreset != NULL)
657113584Ssimokawa					fdc->post_busreset(fdc);
658113584Ssimokawa			}
659113584Ssimokawa		free(devlistp, M_TEMP);
660113584Ssimokawa	}
661116376Ssimokawa
662117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
663117350Ssimokawa	src = &fc->crom_src_buf->src;
664129585Sdfr	crom_load(src, (uint32_t *)newrom, CROMSIZE);
665117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
666117350Ssimokawa		/* bump generation and reload */
667117350Ssimokawa		src->businfo.generation ++;
668117350Ssimokawa		/* generation must be between 0x2 and 0xF */
669117350Ssimokawa		if (src->businfo.generation < 2)
670117350Ssimokawa			src->businfo.generation ++;
671129585Sdfr		crom_load(src, (uint32_t *)newrom, CROMSIZE);
672117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
673117350Ssimokawa	}
674117350Ssimokawa	free(newrom, M_FW);
675103285Sikob}
676106790Ssimokawa
677103285Sikob/* Call once after reboot */
678106790Ssimokawavoid fw_init(struct firewire_comm *fc)
679103285Sikob{
680103285Sikob	int i;
681106543Ssimokawa	struct csrdir *csrd;
682106543Ssimokawa#ifdef FW_VMACCESS
683103285Sikob	struct fw_xfer *xfer;
684103285Sikob	struct fw_bind *fwb;
685106543Ssimokawa#endif
686103285Sikob
687103285Sikob	fc->arq->queued = 0;
688103285Sikob	fc->ars->queued = 0;
689103285Sikob	fc->atq->queued = 0;
690103285Sikob	fc->ats->queued = 0;
691103285Sikob
692103285Sikob	fc->arq->buf = NULL;
693103285Sikob	fc->ars->buf = NULL;
694103285Sikob	fc->atq->buf = NULL;
695103285Sikob	fc->ats->buf = NULL;
696103285Sikob
697113584Ssimokawa	fc->arq->flag = 0;
698113584Ssimokawa	fc->ars->flag = 0;
699113584Ssimokawa	fc->atq->flag = 0;
700113584Ssimokawa	fc->ats->flag = 0;
701103285Sikob
702103285Sikob	STAILQ_INIT(&fc->atq->q);
703103285Sikob	STAILQ_INIT(&fc->ats->q);
704103285Sikob
705103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
706103285Sikob		fc->it[i]->queued = 0;
707103285Sikob		fc->ir[i]->queued = 0;
708103285Sikob
709103285Sikob		fc->it[i]->start = NULL;
710103285Sikob		fc->ir[i]->start = NULL;
711103285Sikob
712103285Sikob		fc->it[i]->buf = NULL;
713103285Sikob		fc->ir[i]->buf = NULL;
714103285Sikob
715103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
716103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
717103285Sikob
718103285Sikob		STAILQ_INIT(&fc->it[i]->q);
719103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
720103285Sikob	}
721103285Sikob
722103285Sikob	fc->arq->maxq = FWMAXQUEUE;
723103285Sikob	fc->ars->maxq = FWMAXQUEUE;
724103285Sikob	fc->atq->maxq = FWMAXQUEUE;
725103285Sikob	fc->ats->maxq = FWMAXQUEUE;
726103285Sikob
727103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
728103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
729103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
730103285Sikob	}
731103285Sikob/* Initialize csr registers */
732103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
733103285Sikob				sizeof(struct fw_topology_map),
734110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
735103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
736103285Sikob				sizeof(struct fw_speed_map),
737110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
738103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
739103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
740103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
741103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
742103285Sikob
743110193Ssimokawa	STAILQ_INIT(&fc->devices);
744103285Sikob
745103285Sikob/* Initialize csr ROM work space */
746103285Sikob	SLIST_INIT(&fc->ongocsr);
747103285Sikob	SLIST_INIT(&fc->csrfree);
748103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
749110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
750103285Sikob		if(csrd == NULL) break;
751103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
752103285Sikob	}
753103285Sikob
754103285Sikob/* Initialize Async handlers */
755103285Sikob	STAILQ_INIT(&fc->binds);
756103285Sikob	for( i = 0 ; i < 0x40 ; i++){
757103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
758103285Sikob	}
759103285Sikob
760103285Sikob/* DV depend CSRs see blue book */
761103285Sikob#if 0
762103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
763103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
764103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
765103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
766103285Sikob	}
767103285Sikob
768103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
769103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
770103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
771103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
772103285Sikob	}
773103285Sikob#endif
774103285Sikob
775116376Ssimokawa	fc->crom_src_buf = NULL;
776103285Sikob
777106543Ssimokawa#ifdef FW_VMACCESS
778103285Sikob	xfer = fw_xfer_alloc();
779103285Sikob	if(xfer == NULL) return;
780103285Sikob
781110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
782103285Sikob	if(fwb == NULL){
783103285Sikob		fw_xfer_free(xfer);
784139680Sjmg		return;
785103285Sikob	}
786167632Ssimokawa	xfer->hand = fw_vmaccess;
787103285Sikob	xfer->fc = fc;
788103285Sikob	xfer->sc = NULL;
789103285Sikob
790103285Sikob	fwb->start_hi = 0x2;
791103285Sikob	fwb->start_lo = 0;
792103285Sikob	fwb->addrlen = 0xffffffff;
793103285Sikob	fwb->xfer = xfer;
794103285Sikob	fw_bindadd(fc, fwb);
795106543Ssimokawa#endif
796103285Sikob}
797106790Ssimokawa
798120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
799120660Ssimokawa    ((fwb)->end < (addr))?1:0)
800120660Ssimokawa
801103285Sikob/*
802129541Sdfr * To lookup bound process from IEEE1394 address.
803103285Sikob */
804106813Ssimokawastruct fw_bind *
805129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
806103285Sikob{
807120660Ssimokawa	u_int64_t addr;
808103285Sikob	struct fw_bind *tfw;
809120660Ssimokawa
810120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
811120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
812169130Ssimokawa		if (BIND_CMP(addr, tfw) == 0)
813103285Sikob			return(tfw);
814103285Sikob	return(NULL);
815103285Sikob}
816106790Ssimokawa
817103285Sikob/*
818103285Sikob * To bind IEEE1394 address block to process.
819103285Sikob */
820106790Ssimokawaint
821106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
822103285Sikob{
823120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
824120660Ssimokawa
825120660Ssimokawa	if (fwb->start > fwb->end) {
826127468Ssimokawa		printf("%s: invalid range\n", __func__);
827120660Ssimokawa		return EINVAL;
828120660Ssimokawa	}
829120660Ssimokawa
830120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
831120660Ssimokawa		if (fwb->end < tfw->start)
832120660Ssimokawa			break;
833120660Ssimokawa		prev = tfw;
834120660Ssimokawa	}
835120660Ssimokawa	if (prev == NULL) {
836103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
837169130Ssimokawa		return (0);
838103285Sikob	}
839120660Ssimokawa	if (prev->end < fwb->start) {
840120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
841169130Ssimokawa		return (0);
842103285Sikob	}
843120660Ssimokawa
844127468Ssimokawa	printf("%s: bind failed\n", __func__);
845120660Ssimokawa	return (EBUSY);
846103285Sikob}
847103285Sikob
848103285Sikob/*
849103285Sikob * To free IEEE1394 address block.
850103285Sikob */
851106790Ssimokawaint
852106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
853103285Sikob{
854120660Ssimokawa#if 0
855120660Ssimokawa	struct fw_xfer *xfer, *next;
856120660Ssimokawa#endif
857120660Ssimokawa	struct fw_bind *tfw;
858103285Sikob	int s;
859103285Sikob
860103285Sikob	s = splfw();
861120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
862120660Ssimokawa		if (tfw == fwb) {
863120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
864120660Ssimokawa			goto found;
865120660Ssimokawa		}
866120660Ssimokawa
867129541Sdfr	printf("%s: no such binding\n", __func__);
868120660Ssimokawa	splx(s);
869120660Ssimokawa	return (1);
870120660Ssimokawafound:
871120660Ssimokawa#if 0
872113584Ssimokawa	/* shall we do this? */
873113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
874113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
875113584Ssimokawa		fw_xfer_free(xfer);
876113584Ssimokawa	}
877113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
878120660Ssimokawa#endif
879113584Ssimokawa
880103285Sikob	splx(s);
881103285Sikob	return 0;
882103285Sikob}
883103285Sikob
884169130Ssimokawaint
885169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
886169130Ssimokawa    int slen, int rlen, int n,
887169130Ssimokawa    struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
888169130Ssimokawa{
889169130Ssimokawa	int i, s;
890169130Ssimokawa	struct fw_xfer *xfer;
891169130Ssimokawa
892169130Ssimokawa	for (i = 0; i < n; i++) {
893169130Ssimokawa		xfer = fw_xfer_alloc_buf(type, slen, rlen);
894169130Ssimokawa		if (xfer == NULL)
895169130Ssimokawa			return (n);
896169130Ssimokawa		xfer->fc = fc;
897169130Ssimokawa		xfer->sc = sc;
898169130Ssimokawa		xfer->hand = hand;
899169130Ssimokawa		s = splfw();
900169130Ssimokawa		STAILQ_INSERT_TAIL(q, xfer, link);
901169130Ssimokawa		splx(s);
902169130Ssimokawa	}
903169130Ssimokawa	return (n);
904169130Ssimokawa}
905169130Ssimokawa
906169130Ssimokawavoid
907169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q)
908169130Ssimokawa{
909169130Ssimokawa	struct fw_xfer *xfer, *next;
910169130Ssimokawa
911169130Ssimokawa	for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
912169130Ssimokawa                next = STAILQ_NEXT(xfer, link);
913169130Ssimokawa                fw_xfer_free_buf(xfer);
914169130Ssimokawa        }
915169130Ssimokawa        STAILQ_INIT(q);
916169130Ssimokawa}
917169130Ssimokawa
918103285Sikob/*
919103285Sikob * To free transaction label.
920103285Sikob */
921106790Ssimokawastatic void
922106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
923103285Sikob{
924169119Ssimokawa	struct fw_xfer *txfer;
925169119Ssimokawa	int s;
926103285Sikob
927169119Ssimokawa	if (xfer->tl < 0)
928169119Ssimokawa		return;
929169119Ssimokawa
930169119Ssimokawa	s = splfw();
931169119Ssimokawa#if 1	/* make sure the label is allocated */
932169119Ssimokawa	STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
933169119Ssimokawa		if(txfer == xfer)
934169119Ssimokawa			break;
935169119Ssimokawa	if (txfer == NULL) {
936169119Ssimokawa		printf("%s: the xfer is not in the tlabel(%d)\n",
937169119Ssimokawa		    __FUNCTION__, xfer->tl);
938169119Ssimokawa		splx(s);
939169119Ssimokawa		return;
940103285Sikob	}
941169119Ssimokawa#endif
942169119Ssimokawa
943169119Ssimokawa	STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
944103285Sikob	splx(s);
945103285Sikob	return;
946103285Sikob}
947106790Ssimokawa
948103285Sikob/*
949103285Sikob * To obtain XFER structure by transaction label.
950103285Sikob */
951106790Ssimokawastatic struct fw_xfer *
952106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
953103285Sikob{
954103285Sikob	struct fw_xfer *xfer;
955103285Sikob	int s = splfw();
956103285Sikob
957169119Ssimokawa	STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
958169119Ssimokawa		if(xfer->send.hdr.mode.hdr.dst == node) {
959103285Sikob			splx(s);
960110577Ssimokawa			if (firewire_debug > 2)
961110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
962103285Sikob			return(xfer);
963103285Sikob		}
964110577Ssimokawa	if (firewire_debug > 1)
965110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
966103285Sikob	splx(s);
967103285Sikob	return(NULL);
968103285Sikob}
969106790Ssimokawa
970103285Sikob/*
971103285Sikob * To allocate IEEE1394 XFER structure.
972103285Sikob */
973106790Ssimokawastruct fw_xfer *
974110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
975103285Sikob{
976103285Sikob	struct fw_xfer *xfer;
977106790Ssimokawa
978110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
979106790Ssimokawa	if (xfer == NULL)
980106790Ssimokawa		return xfer;
981106790Ssimokawa
982110269Ssimokawa	xfer->malloc = type;
983106790Ssimokawa
984103285Sikob	return xfer;
985103285Sikob}
986106790Ssimokawa
987113584Ssimokawastruct fw_xfer *
988113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
989113584Ssimokawa{
990113584Ssimokawa	struct fw_xfer *xfer;
991113584Ssimokawa
992113584Ssimokawa	xfer = fw_xfer_alloc(type);
993126102Scperciva	if (xfer == NULL)
994126102Scperciva		return(NULL);
995120660Ssimokawa	xfer->send.pay_len = send_len;
996120660Ssimokawa	xfer->recv.pay_len = recv_len;
997120660Ssimokawa	if (send_len > 0) {
998120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
999120660Ssimokawa		if (xfer->send.payload == NULL) {
1000113584Ssimokawa			fw_xfer_free(xfer);
1001113584Ssimokawa			return(NULL);
1002113584Ssimokawa		}
1003113584Ssimokawa	}
1004120660Ssimokawa	if (recv_len > 0) {
1005120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1006120660Ssimokawa		if (xfer->recv.payload == NULL) {
1007120660Ssimokawa			if (xfer->send.payload != NULL)
1008120660Ssimokawa				free(xfer->send.payload, type);
1009113584Ssimokawa			fw_xfer_free(xfer);
1010113584Ssimokawa			return(NULL);
1011113584Ssimokawa		}
1012113584Ssimokawa	}
1013113584Ssimokawa	return(xfer);
1014113584Ssimokawa}
1015113584Ssimokawa
1016103285Sikob/*
1017103285Sikob * IEEE1394 XFER post process.
1018103285Sikob */
1019103285Sikobvoid
1020103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1021103285Sikob{
1022167632Ssimokawa	if (xfer->hand == NULL) {
1023167632Ssimokawa		printf("hand == NULL\n");
1024103285Sikob		return;
1025117716Ssimokawa	}
1026103285Sikob
1027121505Ssimokawa	if (xfer->fc == NULL)
1028121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
1029121505Ssimokawa
1030168051Ssimokawa	fw_tl_free(xfer->fc, xfer);
1031167632Ssimokawa	xfer->hand(xfer);
1032103285Sikob}
1033103285Sikob
1034106790Ssimokawavoid
1035113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
1036103285Sikob{
1037103285Sikob	int s;
1038113584Ssimokawa
1039103285Sikob	if(xfer == NULL ) return;
1040103285Sikob	if(xfer->state == FWXF_INQ){
1041103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1042103285Sikob		s = splfw();
1043103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1044103285Sikob		xfer->q->queued --;
1045103285Sikob		splx(s);
1046103285Sikob	}
1047113584Ssimokawa	if (xfer->fc != NULL) {
1048114729Ssimokawa#if 1
1049113584Ssimokawa		if(xfer->state == FWXF_START)
1050114729Ssimokawa			/*
1051114729Ssimokawa			 * This could happen if:
1052114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1053114729Ssimokawa			 *  2. firewire_watch() is called.
1054114729Ssimokawa			 */
1055114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1056103285Sikob#endif
1057103285Sikob	}
1058113584Ssimokawa	xfer->state = FWXF_INIT;
1059113584Ssimokawa	xfer->resp = 0;
1060113584Ssimokawa}
1061113584Ssimokawa/*
1062113584Ssimokawa * To free IEEE1394 XFER structure.
1063113584Ssimokawa */
1064113584Ssimokawavoid
1065120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer)
1066113584Ssimokawa{
1067120660Ssimokawa	if (xfer == NULL) {
1068127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1069120660Ssimokawa		return;
1070120660Ssimokawa	}
1071113584Ssimokawa	fw_xfer_unload(xfer);
1072120660Ssimokawa	if(xfer->send.payload != NULL){
1073120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1074103285Sikob	}
1075120660Ssimokawa	if(xfer->recv.payload != NULL){
1076120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1077103285Sikob	}
1078110269Ssimokawa	free(xfer, xfer->malloc);
1079103285Sikob}
1080103285Sikob
1081120660Ssimokawavoid
1082120660Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1083120660Ssimokawa{
1084120660Ssimokawa	if (xfer == NULL) {
1085127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1086120660Ssimokawa		return;
1087120660Ssimokawa	}
1088120660Ssimokawa	fw_xfer_unload(xfer);
1089120660Ssimokawa	free(xfer, xfer->malloc);
1090120660Ssimokawa}
1091120660Ssimokawa
1092120660Ssimokawavoid
1093110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1094103285Sikob{
1095103285Sikob#if 0
1096110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1097103285Sikob				xfer->state, xfer->resp);
1098103285Sikob#endif
1099103285Sikob	fw_xfer_free(xfer);
1100103285Sikob}
1101103285Sikob
1102103285Sikob/*
1103103285Sikob * To configure PHY.
1104103285Sikob */
1105103285Sikobstatic void
1106103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1107103285Sikob{
1108103285Sikob	struct fw_xfer *xfer;
1109103285Sikob	struct fw_pkt *fp;
1110103285Sikob
1111103285Sikob	fc->status = FWBUSPHYCONF;
1112103285Sikob
1113120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1114113584Ssimokawa	if (xfer == NULL)
1115113584Ssimokawa		return;
1116103285Sikob	xfer->fc = fc;
1117167632Ssimokawa	xfer->hand = fw_asy_callback_free;
1118103285Sikob
1119120660Ssimokawa	fp = &xfer->send.hdr;
1120103285Sikob	fp->mode.ld[1] = 0;
1121103285Sikob	if (root_node >= 0)
1122113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1123103285Sikob	if (gap_count >= 0)
1124113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1125103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1126103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1127103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1128103285Sikob
1129107653Ssimokawa	if (firewire_debug)
1130107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1131103285Sikob						root_node, gap_count);
1132103285Sikob	fw_asyreq(fc, -1, xfer);
1133103285Sikob}
1134103285Sikob
1135103285Sikob#if 0
1136103285Sikob/*
1137103285Sikob * Dump self ID.
1138103285Sikob */
1139103285Sikobstatic void
1140129585Sdfrfw_print_sid(uint32_t sid)
1141103285Sikob{
1142103285Sikob	union fw_self_id *s;
1143103285Sikob	s = (union fw_self_id *) &sid;
1144103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1145103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1146103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1147103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1148103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1149103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1150103285Sikob}
1151103285Sikob#endif
1152103285Sikob
1153103285Sikob/*
1154103285Sikob * To receive self ID.
1155103285Sikob */
1156129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1157103285Sikob{
1158129585Sdfr	uint32_t *p;
1159103285Sikob	union fw_self_id *self_id;
1160103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1161103285Sikob
1162129585Sdfr	fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1163103285Sikob	fc->status = FWBUSINIT;
1164103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1165129585Sdfr	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1166103285Sikob	fc->status = FWBUSCYMELECT;
1167103285Sikob	fc->topology_map->crc_len = 2;
1168103285Sikob	fc->topology_map->generation ++;
1169103285Sikob	fc->topology_map->self_id_count = 0;
1170103285Sikob	fc->topology_map->node_count = 0;
1171103285Sikob	fc->speed_map->generation ++;
1172103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1173103285Sikob	self_id = &fc->topology_map->self_id[0];
1174103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1175103285Sikob		if (sid[1] != ~sid[0]) {
1176103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1177103285Sikob			sid += 2;
1178103285Sikob			continue;
1179103285Sikob		}
1180103285Sikob		*self_id = *((union fw_self_id *)sid);
1181103285Sikob		fc->topology_map->crc_len++;
1182103285Sikob		if(self_id->p0.sequel == 0){
1183103285Sikob			fc->topology_map->node_count ++;
1184103285Sikob			c_port = 0;
1185103285Sikob#if 0
1186103285Sikob			fw_print_sid(sid[0]);
1187103285Sikob#endif
1188103285Sikob			node = self_id->p0.phy_id;
1189103285Sikob			if(fc->max_node < node){
1190103285Sikob				fc->max_node = self_id->p0.phy_id;
1191103285Sikob			}
1192103285Sikob			/* XXX I'm not sure this is the right speed_map */
1193103285Sikob			fc->speed_map->speed[node][node]
1194103285Sikob					= self_id->p0.phy_speed;
1195103285Sikob			for (j = 0; j < node; j ++) {
1196103285Sikob				fc->speed_map->speed[j][node]
1197103285Sikob					= fc->speed_map->speed[node][j]
1198103285Sikob					= min(fc->speed_map->speed[j][j],
1199103285Sikob							self_id->p0.phy_speed);
1200103285Sikob			}
1201103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1202103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1203103285Sikob				fc->irm = self_id->p0.phy_id;
1204103285Sikob			}
1205103285Sikob			if(self_id->p0.port0 >= 0x2){
1206103285Sikob				c_port++;
1207103285Sikob			}
1208103285Sikob			if(self_id->p0.port1 >= 0x2){
1209103285Sikob				c_port++;
1210103285Sikob			}
1211103285Sikob			if(self_id->p0.port2 >= 0x2){
1212103285Sikob				c_port++;
1213103285Sikob			}
1214103285Sikob		}
1215103285Sikob		if(c_port > 2){
1216103285Sikob			i_branch += (c_port - 2);
1217103285Sikob		}
1218103285Sikob		sid += 2;
1219103285Sikob		self_id++;
1220103285Sikob		fc->topology_map->self_id_count ++;
1221103285Sikob	}
1222108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1223103285Sikob	/* CRC */
1224103285Sikob	fc->topology_map->crc = fw_crc16(
1225129585Sdfr			(uint32_t *)&fc->topology_map->generation,
1226103285Sikob			fc->topology_map->crc_len * 4);
1227103285Sikob	fc->speed_map->crc = fw_crc16(
1228129585Sdfr			(uint32_t *)&fc->speed_map->generation,
1229103285Sikob			fc->speed_map->crc_len * 4);
1230103285Sikob	/* byteswap and copy to CSR */
1231129585Sdfr	p = (uint32_t *)fc->topology_map;
1232103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1233103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1234129585Sdfr	p = (uint32_t *)fc->speed_map;
1235103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1236103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1237129585Sdfr	/* don't byte-swap uint8_t array */
1238103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1239103285Sikob
1240103285Sikob	fc->max_hop = fc->max_node - i_branch;
1241103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1242103285Sikob
1243103285Sikob	if(fc->irm == -1 ){
1244103285Sikob		printf(", Not found IRM capable node");
1245103285Sikob	}else{
1246103285Sikob		printf(", cable IRM = %d", fc->irm);
1247103285Sikob		if (fc->irm == fc->nodeid)
1248110016Ssimokawa			printf(" (me)");
1249103285Sikob	}
1250110016Ssimokawa	printf("\n");
1251103285Sikob
1252109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1253114909Ssimokawa		if (fc->irm == fc->nodeid) {
1254103285Sikob			fc->status = FWBUSMGRDONE;
1255103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1256114909Ssimokawa			fw_bmr(fc);
1257109736Ssimokawa		} else {
1258103285Sikob			fc->status = FWBUSMGRELECT;
1259110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1260110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1261103285Sikob		}
1262114909Ssimokawa	} else
1263103285Sikob		fc->status = FWBUSMGRDONE;
1264114909Ssimokawa
1265108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1266108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1267103285Sikob}
1268106790Ssimokawa
1269103285Sikob/*
1270103285Sikob * To probe devices on the IEEE1394 bus.
1271103285Sikob */
1272106790Ssimokawastatic void
1273106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1274103285Sikob{
1275103285Sikob	int s;
1276120850Ssimokawa	struct fw_device *fwdev;
1277103285Sikob
1278103285Sikob	s = splfw();
1279103285Sikob	fc->status = FWBUSEXPLORE;
1280103285Sikob
1281120850Ssimokawa	/* Invalidate all devices, just after bus reset. */
1282120850Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1283110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1284103285Sikob			fwdev->status = FWDEVINVAL;
1285103285Sikob			fwdev->rcnt = 0;
1286103285Sikob		}
1287120850Ssimokawa
1288103285Sikob	fc->ongonode = 0;
1289103285Sikob	fc->ongoaddr = CSRROMOFF;
1290103285Sikob	fc->ongodev = NULL;
1291103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1292103285Sikob	fw_bus_explore(fc);
1293103285Sikob	splx(s);
1294103285Sikob}
1295106790Ssimokawa
1296103285Sikob/*
1297129274Sdfr * Find the self_id packet for a node, ignoring sequels.
1298129274Sdfr */
1299129274Sdfrstatic union fw_self_id *
1300129274Sdfrfw_find_self_id(struct firewire_comm *fc, int node)
1301129274Sdfr{
1302129274Sdfr	uint32_t i;
1303129274Sdfr	union fw_self_id *s;
1304129274Sdfr
1305129274Sdfr	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1306129274Sdfr		s = &fc->topology_map->self_id[i];
1307129274Sdfr		if (s->p0.sequel)
1308129274Sdfr			continue;
1309129274Sdfr		if (s->p0.phy_id == node)
1310129274Sdfr			return s;
1311129274Sdfr	}
1312129274Sdfr	return 0;
1313129274Sdfr}
1314129274Sdfr
1315129274Sdfr/*
1316103285Sikob * To collect device informations on the IEEE1394 bus.
1317103285Sikob */
1318106790Ssimokawastatic void
1319106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1320103285Sikob{
1321103285Sikob	int err = 0;
1322110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1323129585Sdfr	uint32_t addr;
1324103285Sikob	struct fw_xfer *xfer;
1325103285Sikob	struct fw_pkt *fp;
1326152792Ssimokawa	union fw_self_id *fwsid;
1327103285Sikob
1328103285Sikob	if(fc->status != FWBUSEXPLORE)
1329103285Sikob		return;
1330103285Sikob
1331103285Sikobloop:
1332103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1333103285Sikob
1334103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1335103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1336103285Sikob
1337103285Sikob	/* check link */
1338103285Sikob	/* XXX we need to check phy_id first */
1339152792Ssimokawa	fwsid = fw_find_self_id(fc, fc->ongonode);
1340152792Ssimokawa	if (!fwsid || !fwsid->p0.link_active) {
1341110577Ssimokawa		if (firewire_debug)
1342110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1343103285Sikob		fc->ongonode++;
1344103285Sikob		goto loop;
1345103285Sikob	}
1346103285Sikob
1347103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1348103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1349103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1350103285Sikob		fc->ongoaddr = CSRROMOFF;
1351103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1352103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1353103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1354103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1355103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1356103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1357103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1358103285Sikob	}else if(fc->ongodev == NULL){
1359110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1360110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1361103285Sikob				break;
1362103285Sikob		if(fwdev != NULL){
1363103285Sikob			fwdev->dst = fc->ongonode;
1364117350Ssimokawa			fwdev->status = FWDEVINIT;
1365117350Ssimokawa			fc->ongodev = fwdev;
1366103285Sikob			fc->ongoaddr = CSRROMOFF;
1367117350Ssimokawa			addr = 0xf0000000 | fc->ongoaddr;
1368117350Ssimokawa			goto dorequest;
1369103285Sikob		}
1370115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1371115787Ssimokawa							M_NOWAIT | M_ZERO);
1372103285Sikob		if(fwdev == NULL)
1373103285Sikob			return;
1374106810Ssimokawa		fwdev->fc = fc;
1375103285Sikob		fwdev->rommax = 0;
1376103285Sikob		fwdev->dst = fc->ongonode;
1377103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1378103285Sikob		fwdev->status = FWDEVINIT;
1379103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1380103285Sikob
1381110179Ssimokawa		pfwdev = NULL;
1382110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1383110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1384110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1385110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1386110179Ssimokawa				break;
1387110179Ssimokawa			pfwdev = tfwdev;
1388103285Sikob		}
1389110179Ssimokawa		if (pfwdev == NULL)
1390110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1391110179Ssimokawa		else
1392110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1393103285Sikob
1394108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1395107653Ssimokawa			linkspeed[fwdev->speed],
1396107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1397103285Sikob
1398103285Sikob		fc->ongodev = fwdev;
1399103285Sikob		fc->ongoaddr = CSRROMOFF;
1400103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1401103285Sikob	}else{
1402103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1403103285Sikob	}
1404117350Ssimokawadorequest:
1405103285Sikob#if 0
1406103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1407103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1408103285Sikob		fw_bus_explore_callback);
1409103285Sikob	if(xfer == NULL) goto done;
1410103285Sikob#else
1411120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1412103285Sikob	if(xfer == NULL){
1413103285Sikob		goto done;
1414103285Sikob	}
1415120660Ssimokawa	xfer->send.spd = 0;
1416120660Ssimokawa	fp = &xfer->send.hdr;
1417113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1418103285Sikob	fp->mode.rreqq.tlrt = 0;
1419103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1420103285Sikob	fp->mode.rreqq.pri = 0;
1421103285Sikob	fp->mode.rreqq.src = 0;
1422120660Ssimokawa	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1423113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1424167632Ssimokawa	xfer->hand = fw_bus_explore_callback;
1425103285Sikob
1426110577Ssimokawa	if (firewire_debug)
1427110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1428110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1429103285Sikob	err = fw_asyreq(fc, -1, xfer);
1430103285Sikob	if(err){
1431103285Sikob		fw_xfer_free( xfer);
1432103285Sikob		return;
1433103285Sikob	}
1434103285Sikob#endif
1435103285Sikob	return;
1436103285Sikobdone:
1437103285Sikob	/* fw_attach_devs */
1438103285Sikob	fc->status = FWBUSEXPDONE;
1439107653Ssimokawa	if (firewire_debug)
1440107653Ssimokawa		printf("bus_explore done\n");
1441103285Sikob	fw_attach_dev(fc);
1442103285Sikob	return;
1443103285Sikob
1444103285Sikob}
1445106790Ssimokawa
1446103285Sikob/* Portable Async. request read quad */
1447106790Ssimokawastruct fw_xfer *
1448129585Sdfrasyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt,
1449129585Sdfr	uint32_t addr_hi, uint32_t addr_lo,
1450124169Ssimokawa	void (*hand) (struct fw_xfer*))
1451103285Sikob{
1452103285Sikob	struct fw_xfer *xfer;
1453103285Sikob	struct fw_pkt *fp;
1454103285Sikob	int err;
1455103285Sikob
1456120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1457113584Ssimokawa	if (xfer == NULL)
1458103285Sikob		return NULL;
1459113584Ssimokawa
1460120660Ssimokawa	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1461120660Ssimokawa	fp = &xfer->send.hdr;
1462113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1463103285Sikob	if(tl & FWP_TL_VALID){
1464103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1465103285Sikob	}else{
1466103285Sikob		fp->mode.rreqq.tlrt = 0;
1467103285Sikob	}
1468103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1469103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1470103285Sikob	fp->mode.rreqq.pri = 0;
1471103285Sikob	fp->mode.rreqq.src = 0;
1472120660Ssimokawa	fp->mode.rreqq.dst = addr_hi >> 16;
1473113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1474167632Ssimokawa	xfer->hand = hand;
1475103285Sikob
1476103285Sikob	err = fw_asyreq(fc, -1, xfer);
1477103285Sikob	if(err){
1478103285Sikob		fw_xfer_free( xfer);
1479103285Sikob		return NULL;
1480103285Sikob	}
1481103285Sikob	return xfer;
1482103285Sikob}
1483106790Ssimokawa
1484103285Sikob/*
1485103285Sikob * Callback for the IEEE1394 bus information collection.
1486103285Sikob */
1487106790Ssimokawastatic void
1488106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1489106790Ssimokawa{
1490103285Sikob	struct firewire_comm *fc;
1491103285Sikob	struct fw_pkt *sfp,*rfp;
1492103285Sikob	struct csrhdr *chdr;
1493103285Sikob	struct csrdir *csrd;
1494103285Sikob	struct csrreg *csrreg;
1495129585Sdfr	uint32_t offset;
1496103285Sikob
1497103285Sikob
1498110577Ssimokawa	if(xfer == NULL) {
1499110577Ssimokawa		printf("xfer == NULL\n");
1500110577Ssimokawa		return;
1501110577Ssimokawa	}
1502103285Sikob	fc = xfer->fc;
1503110577Ssimokawa
1504110577Ssimokawa	if (firewire_debug)
1505110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1506110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1507110577Ssimokawa
1508103285Sikob	if(xfer->resp != 0){
1509127468Ssimokawa		device_printf(fc->bdev,
1510167630Ssimokawa		    "bus_explore node=%d addr=0x%x resp=%d\n",
1511167630Ssimokawa		    fc->ongonode, fc->ongoaddr, xfer->resp);
1512114284Ssimokawa		goto errnode;
1513103285Sikob	}
1514103285Sikob
1515120660Ssimokawa	sfp = &xfer->send.hdr;
1516120660Ssimokawa	rfp = &xfer->recv.hdr;
1517103285Sikob#if 0
1518103285Sikob	{
1519129585Sdfr		uint32_t *qld;
1520103285Sikob		int i;
1521129585Sdfr		qld = (uint32_t *)xfer->recv.buf;
1522103285Sikob		printf("len:%d\n", xfer->recv.len);
1523103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1524113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1525103285Sikob			if((i % 16) == 15) printf("\n");
1526103285Sikob		}
1527103285Sikob		if((i % 16) != 15) printf("\n");
1528103285Sikob	}
1529103285Sikob#endif
1530103285Sikob	if(fc->ongodev == NULL){
1531113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1532103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1533103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1534129541Sdfr/* If CSR is minimal confinguration, more investigation is not needed. */
1535103285Sikob			if(chdr->info_len == 1){
1536110577Ssimokawa				if (firewire_debug)
1537110577Ssimokawa					printf("node%d: minimal config\n",
1538110577Ssimokawa								fc->ongonode);
1539103285Sikob				goto nextnode;
1540103285Sikob			}else{
1541103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1542103285Sikob			}
1543113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1544103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1545103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1546113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1547103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1548110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1549110577Ssimokawa				if (firewire_debug)
1550110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1551110577Ssimokawa							fc->ongonode);
1552103285Sikob				goto nextnode;
1553110577Ssimokawa			}
1554103285Sikob			fc->ongoaddr = CSRROMOFF;
1555103285Sikob		}
1556103285Sikob	}else{
1557117350Ssimokawa		if (fc->ongoaddr == CSRROMOFF &&
1558117350Ssimokawa		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1559117350Ssimokawa			fc->ongodev->status = FWDEVATTACHED;
1560117350Ssimokawa			goto nextnode;
1561117350Ssimokawa		}
1562103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1563103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1564103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1565103285Sikob		}
1566103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1567103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1568103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1569103285Sikob			offset = CSRROMOFF;
1570103285Sikob		}else{
1571103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1572103285Sikob			offset = csrd->off;
1573103285Sikob		}
1574103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1575103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1576103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1577103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1578103285Sikob				if(csrd == NULL){
1579103285Sikob					goto nextnode;
1580103285Sikob				}else{
1581103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1582103285Sikob					fc->ongoaddr += csrreg->val * 4;
1583103285Sikob					csrd->off = fc->ongoaddr;
1584103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1585103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1586103285Sikob					goto nextaddr;
1587103285Sikob				}
1588103285Sikob			}
1589103285Sikob		}
1590103285Sikob		fc->ongoaddr += 4;
1591103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1592103285Sikob				(fc->ongodev->rommax < 0x414)){
1593103285Sikob			if(fc->ongodev->rommax <= 0x414){
1594103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1595103285Sikob				if(csrd == NULL) goto nextnode;
1596103285Sikob				csrd->off = fc->ongoaddr;
1597103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1598103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1599103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1600103285Sikob			}
1601103285Sikob			goto nextaddr;
1602103285Sikob		}
1603103285Sikob
1604103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1605103285Sikob			if(csrd == NULL){
1606103285Sikob				goto nextnode;
1607103285Sikob			};
1608103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1609103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1610103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1611103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1612103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1613103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1614103285Sikob				offset = CSRROMOFF;
1615103285Sikob			}else{
1616103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1617103285Sikob				offset = csrd->off;
1618103285Sikob			}
1619103285Sikob		}
1620103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1621103285Sikob			goto nextnode;
1622103285Sikob		}
1623103285Sikob	}
1624103285Sikobnextaddr:
1625103285Sikob	fw_xfer_free( xfer);
1626103285Sikob	fw_bus_explore(fc);
1627103285Sikob	return;
1628114284Ssimokawaerrnode:
1629127468Ssimokawa	if (fc->ongodev != NULL) {
1630114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1631127468Ssimokawa		/* Invalidate ROM */
1632127468Ssimokawa		fc->ongodev->csrrom[0] = 0;
1633127468Ssimokawa	}
1634103285Sikobnextnode:
1635103285Sikob	fw_xfer_free( xfer);
1636103285Sikob	fc->ongonode++;
1637103285Sikob/* housekeeping work space */
1638103285Sikob	fc->ongoaddr = CSRROMOFF;
1639103285Sikob	fc->ongodev = NULL;
1640103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1641103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1642103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1643103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1644103285Sikob	}
1645103285Sikob	fw_bus_explore(fc);
1646103285Sikob	return;
1647103285Sikob}
1648103285Sikob
1649103285Sikob/*
1650103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1651103285Sikob */
1652106815Ssimokawastatic void
1653106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1654103285Sikob{
1655120850Ssimokawa	struct fw_device *fwdev, *next;
1656103285Sikob	int i, err;
1657103285Sikob	device_t *devlistp;
1658103285Sikob	int devcnt;
1659103285Sikob	struct firewire_dev_comm *fdc;
1660103285Sikob
1661120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1662120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1663120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1664114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1665120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1666120850Ssimokawa			fwdev->rcnt ++;
1667120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1668120850Ssimokawa				/*
1669120850Ssimokawa				 * Remove devices which have not been seen
1670120850Ssimokawa				 * for a while.
1671120850Ssimokawa				 */
1672120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1673120850Ssimokawa				    link);
1674120850Ssimokawa				free(fwdev, M_FW);
1675120850Ssimokawa			}
1676120850Ssimokawa		}
1677120850Ssimokawa	}
1678103285Sikob
1679108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1680103285Sikob	if( err != 0 )
1681103285Sikob		return;
1682103285Sikob	for( i = 0 ; i < devcnt ; i++){
1683103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1684103285Sikob			fdc = device_get_softc(devlistp[i]);
1685103285Sikob			if (fdc->post_explore != NULL)
1686103285Sikob				fdc->post_explore(fdc);
1687103285Sikob		}
1688103285Sikob	}
1689103285Sikob	free(devlistp, M_TEMP);
1690103285Sikob
1691103285Sikob	return;
1692103285Sikob}
1693106815Ssimokawa
1694103285Sikob/*
1695129541Sdfr * To allocate unique transaction label.
1696103285Sikob */
1697106815Ssimokawastatic int
1698106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1699103285Sikob{
1700103285Sikob	u_int i;
1701169119Ssimokawa	struct fw_xfer *txfer;
1702103285Sikob	int s;
1703129585Sdfr	static uint32_t label = 0;
1704103285Sikob
1705103285Sikob	s = splfw();
1706103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1707103285Sikob		label = (label + 1) & 0x3f;
1708169119Ssimokawa		STAILQ_FOREACH(txfer, &fc->tlabels[label], tlabel)
1709169119Ssimokawa			if (txfer->send.hdr.mode.hdr.dst ==
1710120660Ssimokawa			    xfer->send.hdr.mode.hdr.dst)
1711120660Ssimokawa				break;
1712169119Ssimokawa		if(txfer == NULL) {
1713169119Ssimokawa			STAILQ_INSERT_TAIL(&fc->tlabels[label], xfer, tlabel);
1714103285Sikob			splx(s);
1715110577Ssimokawa			if (firewire_debug > 1)
1716110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1717120660Ssimokawa				    xfer->send.hdr.mode.hdr.dst, label);
1718103285Sikob			return(label);
1719103285Sikob		}
1720103285Sikob	}
1721103285Sikob	splx(s);
1722103285Sikob
1723130460Sdfr	if (firewire_debug > 1)
1724130460Sdfr		printf("fw_get_tlabel: no free tlabel\n");
1725103285Sikob	return(-1);
1726103285Sikob}
1727106815Ssimokawa
1728113584Ssimokawastatic void
1729120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1730113584Ssimokawa{
1731120660Ssimokawa	struct fw_pkt *pkt;
1732120660Ssimokawa	u_char *p;
1733120660Ssimokawa	struct tcode_info *tinfo;
1734120660Ssimokawa	u_int res, i, len, plen;
1735113584Ssimokawa
1736169127Ssimokawa	rb->xfer->recv.spd = rb->spd;
1737120660Ssimokawa
1738120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1739120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1740120660Ssimokawa
1741120660Ssimokawa	/* Copy header */
1742120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1743120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1744132771Skan	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1745120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1746120660Ssimokawa
1747120660Ssimokawa	/* Copy payload */
1748120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1749120660Ssimokawa	res = rb->xfer->recv.pay_len;
1750120660Ssimokawa
1751120660Ssimokawa	/* special handling for RRESQ */
1752120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1753129585Sdfr	    p != NULL && res >= sizeof(uint32_t)) {
1754129585Sdfr		*(uint32_t *)p = pkt->mode.rresq.data;
1755129585Sdfr		rb->xfer->recv.pay_len = sizeof(uint32_t);
1756120660Ssimokawa		return;
1757120660Ssimokawa	}
1758120660Ssimokawa
1759120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1760120660Ssimokawa		return;
1761120660Ssimokawa
1762120660Ssimokawa	plen = pkt->mode.rresb.len;
1763120660Ssimokawa
1764120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1765120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1766113584Ssimokawa		if (res < len) {
1767113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1768120660Ssimokawa			    rb->xfer->recv.pay_len, len - res);
1769113584Ssimokawa			len = res;
1770113584Ssimokawa		}
1771120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1772113584Ssimokawa		p += len;
1773113584Ssimokawa		res -= len;
1774120660Ssimokawa		plen -= len;
1775120660Ssimokawa		if (res == 0 || plen == 0)
1776113584Ssimokawa			break;
1777113584Ssimokawa	}
1778120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1779120660Ssimokawa
1780113584Ssimokawa}
1781113584Ssimokawa
1782103285Sikob/*
1783129541Sdfr * Generic packet receiving process.
1784103285Sikob */
1785106815Ssimokawavoid
1786120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1787103285Sikob{
1788103285Sikob	struct fw_pkt *fp, *resfp;
1789103285Sikob	struct fw_bind *bind;
1790169130Ssimokawa	int tcode;
1791113584Ssimokawa	int i, len, oldstate;
1792103285Sikob#if 0
1793103285Sikob	{
1794129585Sdfr		uint32_t *qld;
1795103285Sikob		int i;
1796129585Sdfr		qld = (uint32_t *)buf;
1797103285Sikob		printf("spd %d len:%d\n", spd, len);
1798103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1799103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1800103285Sikob			if((i % 16) == 15) printf("\n");
1801103285Sikob		}
1802103285Sikob		if((i % 16) != 15) printf("\n");
1803103285Sikob	}
1804103285Sikob#endif
1805120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1806113584Ssimokawa	tcode = fp->mode.common.tcode;
1807113584Ssimokawa	switch (tcode) {
1808103285Sikob	case FWTCODE_WRES:
1809103285Sikob	case FWTCODE_RRESQ:
1810103285Sikob	case FWTCODE_RRESB:
1811103285Sikob	case FWTCODE_LRES:
1812120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1813103285Sikob					fp->mode.hdr.tlrt >> 2);
1814120660Ssimokawa		if(rb->xfer == NULL) {
1815103285Sikob			printf("fw_rcv: unknown response "
1816124251Ssimokawa			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1817124251Ssimokawa			    tcode_str[tcode], tcode,
1818124251Ssimokawa			    fp->mode.hdr.src,
1819124251Ssimokawa			    fp->mode.hdr.tlrt >> 2,
1820124251Ssimokawa			    fp->mode.hdr.tlrt & 3,
1821124251Ssimokawa			    fp->mode.rresq.data);
1822103285Sikob#if 1
1823103285Sikob			printf("try ad-hoc work around!!\n");
1824120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1825103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1826120660Ssimokawa			if (rb->xfer == NULL) {
1827103285Sikob				printf("no use...\n");
1828169131Ssimokawa				return;
1829103285Sikob			}
1830103285Sikob#else
1831169131Ssimokawa			return;
1832103285Sikob#endif
1833103285Sikob		}
1834120660Ssimokawa		fw_rcv_copy(rb);
1835120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1836120660Ssimokawa			rb->xfer->resp = EIO;
1837120660Ssimokawa		else
1838120660Ssimokawa			rb->xfer->resp = 0;
1839113584Ssimokawa		/* make sure the packet is drained in AT queue */
1840120660Ssimokawa		oldstate = rb->xfer->state;
1841120660Ssimokawa		rb->xfer->state = FWXF_RCVD;
1842113584Ssimokawa		switch (oldstate) {
1843113584Ssimokawa		case FWXF_SENT:
1844120660Ssimokawa			fw_xfer_done(rb->xfer);
1845103285Sikob			break;
1846113584Ssimokawa		case FWXF_START:
1847119289Ssimokawa#if 0
1848113584Ssimokawa			if (firewire_debug)
1849120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1850119289Ssimokawa#endif
1851113584Ssimokawa			break;
1852103285Sikob		default:
1853120660Ssimokawa			printf("unexpected state %d\n", rb->xfer->state);
1854103285Sikob		}
1855113584Ssimokawa		return;
1856103285Sikob	case FWTCODE_WREQQ:
1857103285Sikob	case FWTCODE_WREQB:
1858103285Sikob	case FWTCODE_RREQQ:
1859103285Sikob	case FWTCODE_RREQB:
1860103285Sikob	case FWTCODE_LREQ:
1861120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1862113584Ssimokawa			fp->mode.rreqq.dest_lo);
1863103285Sikob		if(bind == NULL){
1864124251Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1865127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1866127468Ssimokawa			    " src=0x%x data=%lx\n",
1867127468Ssimokawa#else
1868124251Ssimokawa			    " src=0x%x data=%x\n",
1869113962Ssimokawa#endif
1870124251Ssimokawa			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1871124251Ssimokawa			    tcode_str[tcode], tcode,
1872124251Ssimokawa			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1873120660Ssimokawa			if (rb->fc->status == FWBUSRESET) {
1874110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1875169131Ssimokawa				return;
1876103285Sikob			}
1877120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
1878120660Ssimokawa			if(rb->xfer == NULL){
1879103285Sikob				return;
1880103285Sikob			}
1881120660Ssimokawa			rb->xfer->send.spd = rb->spd;
1882120660Ssimokawa			rb->xfer->send.pay_len = 0;
1883120660Ssimokawa			resfp = &rb->xfer->send.hdr;
1884113584Ssimokawa			switch (tcode) {
1885103285Sikob			case FWTCODE_WREQQ:
1886103285Sikob			case FWTCODE_WREQB:
1887103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1888103285Sikob				break;
1889103285Sikob			case FWTCODE_RREQQ:
1890103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1891103285Sikob				break;
1892103285Sikob			case FWTCODE_RREQB:
1893103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1894103285Sikob				break;
1895103285Sikob			case FWTCODE_LREQ:
1896103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1897103285Sikob				break;
1898103285Sikob			}
1899103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1900103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1901103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1902120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1903103285Sikob			resfp->mode.rresb.extcode = 0;
1904103285Sikob			resfp->mode.rresb.len = 0;
1905103285Sikob/*
1906167632Ssimokawa			rb->xfer->hand = fw_asy_callback;
1907103285Sikob*/
1908167632Ssimokawa			rb->xfer->hand = fw_xfer_free;
1909120660Ssimokawa			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1910120660Ssimokawa				fw_xfer_free(rb->xfer);
1911103285Sikob				return;
1912103285Sikob			}
1913169131Ssimokawa			return;
1914103285Sikob		}
1915113584Ssimokawa		len = 0;
1916120660Ssimokawa		for (i = 0; i < rb->nvec; i ++)
1917120660Ssimokawa			len += rb->vec[i].iov_len;
1918169130Ssimokawa		rb->xfer = STAILQ_FIRST(&bind->xferlist);
1919169130Ssimokawa		if (rb->xfer == NULL) {
1920169131Ssimokawa#if 1
1921169130Ssimokawa			printf("Discard a packet for this bind.\n");
1922169131Ssimokawa#endif
1923169131Ssimokawa			return;
1924103285Sikob		}
1925169130Ssimokawa		STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1926169130Ssimokawa		fw_rcv_copy(rb);
1927169130Ssimokawa		rb->xfer->hand(rb->xfer);
1928169130Ssimokawa		return;
1929120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
1930103285Sikob	case FWTCODE_STREAM:
1931103285Sikob	{
1932103285Sikob		struct fw_xferq *xferq;
1933103285Sikob
1934120660Ssimokawa		xferq = rb->fc->ir[sub];
1935103285Sikob#if 0
1936103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1937103285Sikob			sub, len, off, spd);
1938103285Sikob#endif
1939103285Sikob		if(xferq->queued >= xferq->maxq) {
1940103285Sikob			printf("receive queue is full\n");
1941169131Ssimokawa			return;
1942103285Sikob		}
1943113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1944113584Ssimokawa			per packet mode */
1945120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1946113584Ssimokawa						vec[0].iov_len);
1947169131Ssimokawa		if (rb->xfer == NULL)
1948169131Ssimokawa			return;
1949120660Ssimokawa		fw_rcv_copy(rb)
1950103285Sikob		s = splfw();
1951103285Sikob		xferq->queued++;
1952120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1953103285Sikob		splx(s);
1954120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
1955127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1956127468Ssimokawa		if (&xferq->rsel.si_pid != 0)
1957127468Ssimokawa#else
1958103285Sikob		if (SEL_WAITING(&xferq->rsel))
1959103285Sikob#endif
1960122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
1961103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1962103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1963103285Sikob			wakeup((caddr_t)xferq);
1964103285Sikob		}
1965103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1966103285Sikob			xferq->hand(xferq);
1967103285Sikob		}
1968103285Sikob		return;
1969103285Sikob		break;
1970103285Sikob	}
1971120660Ssimokawa#endif
1972103285Sikob	default:
1973113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1974103285Sikob		break;
1975103285Sikob	}
1976103285Sikob}
1977106815Ssimokawa
1978103285Sikob/*
1979103285Sikob * Post process for Bus Manager election process.
1980103285Sikob */
1981103285Sikobstatic void
1982103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1983103285Sikob{
1984103285Sikob	struct firewire_comm *fc;
1985109422Ssimokawa	int bmr;
1986103285Sikob
1987109422Ssimokawa	if (xfer == NULL)
1988109422Ssimokawa		return;
1989103285Sikob	fc = xfer->fc;
1990109422Ssimokawa	if (xfer->resp != 0)
1991103285Sikob		goto error;
1992120660Ssimokawa	if (xfer->recv.payload == NULL)
1993103285Sikob		goto error;
1994120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
1995103285Sikob		goto error;
1996109422Ssimokawa
1997120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
1998109422Ssimokawa	if (bmr == 0x3f)
1999109422Ssimokawa		bmr = fc->nodeid;
2000109422Ssimokawa
2001109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2002120660Ssimokawa	fw_xfer_free_buf(xfer);
2003114909Ssimokawa	fw_bmr(fc);
2004114909Ssimokawa	return;
2005114909Ssimokawa
2006103285Sikoberror:
2007114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2008120660Ssimokawa	fw_xfer_free_buf(xfer);
2009103285Sikob}
2010106815Ssimokawa
2011113584Ssimokawa
2012103285Sikob/*
2013103285Sikob * To candidate Bus Manager election process.
2014103285Sikob */
2015110270Ssimokawastatic void
2016106815Ssimokawafw_try_bmr(void *arg)
2017103285Sikob{
2018103285Sikob	struct fw_xfer *xfer;
2019103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2020103285Sikob	struct fw_pkt *fp;
2021103285Sikob	int err = 0;
2022103285Sikob
2023120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2024103285Sikob	if(xfer == NULL){
2025103285Sikob		return;
2026103285Sikob	}
2027120660Ssimokawa	xfer->send.spd = 0;
2028103285Sikob	fc->status = FWBUSMGRELECT;
2029103285Sikob
2030120660Ssimokawa	fp = &xfer->send.hdr;
2031113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2032103285Sikob	fp->mode.lreq.tlrt = 0;
2033103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2034103285Sikob	fp->mode.lreq.pri = 0;
2035103285Sikob	fp->mode.lreq.src = 0;
2036113584Ssimokawa	fp->mode.lreq.len = 8;
2037120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2038120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2039113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2040120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2041120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2042167632Ssimokawa	xfer->hand = fw_try_bmr_callback;
2043103285Sikob
2044103285Sikob	err = fw_asyreq(fc, -1, xfer);
2045103285Sikob	if(err){
2046120660Ssimokawa		fw_xfer_free_buf(xfer);
2047103285Sikob		return;
2048103285Sikob	}
2049103285Sikob	return;
2050103285Sikob}
2051106543Ssimokawa
2052106543Ssimokawa#ifdef FW_VMACCESS
2053103285Sikob/*
2054103285Sikob * Software implementation for physical memory block access.
2055103285Sikob * XXX:Too slow, usef for debug purpose only.
2056103285Sikob */
2057106815Ssimokawastatic void
2058106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2059103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2060129585Sdfr	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2061103285Sikob
2062113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2063113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2064103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2065103285Sikob	if(xfer->resp != 0){
2066103285Sikob		fw_xfer_free( xfer);
2067103285Sikob		return;
2068103285Sikob	}
2069103285Sikob	if(xfer->recv.buf == NULL){
2070103285Sikob		fw_xfer_free( xfer);
2071103285Sikob		return;
2072103285Sikob	}
2073103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2074103285Sikob	switch(rfp->mode.hdr.tcode){
2075103285Sikob		/* XXX need fix for 64bit arch */
2076103285Sikob		case FWTCODE_WREQB:
2077110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2078103285Sikob			xfer->send.len = 12;
2079103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2080103285Sikob			bcopy(rfp->mode.wreqb.payload,
2081103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2082103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2083103285Sikob			sfp->mode.wres.rtcode = 0;
2084103285Sikob			break;
2085103285Sikob		case FWTCODE_WREQQ:
2086110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2087103285Sikob			xfer->send.len = 12;
2088103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2089129585Sdfr			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2090103285Sikob			sfp->mode.wres.rtcode = 0;
2091103285Sikob			break;
2092103285Sikob		case FWTCODE_RREQB:
2093110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2094103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2095103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2096103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2097129585Sdfr				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2098103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2099103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2100103285Sikob			sfp->mode.rresb.rtcode = 0;
2101103285Sikob			sfp->mode.rresb.extcode = 0;
2102103285Sikob			break;
2103103285Sikob		case FWTCODE_RREQQ:
2104110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2105103285Sikob			xfer->send.len = 16;
2106103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2107129585Sdfr			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2108103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2109103285Sikob			sfp->mode.rresb.rtcode = 0;
2110103285Sikob			break;
2111103285Sikob		default:
2112103285Sikob			fw_xfer_free( xfer);
2113103285Sikob			return;
2114103285Sikob	}
2115103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2116103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2117167632Ssimokawa	xfer->hand = fw_xfer_free;
2118103285Sikob
2119103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2120103285Sikob	sfp->mode.hdr.pri = 0;
2121103285Sikob
2122103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2123103285Sikob/**/
2124103285Sikob	return;
2125103285Sikob}
2126106543Ssimokawa#endif
2127106543Ssimokawa
2128103285Sikob/*
2129103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2130103285Sikob */
2131129585Sdfruint16_t
2132129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){
2133129585Sdfr	uint32_t i, sum, crc = 0;
2134103285Sikob	int shift;
2135103285Sikob	len = (len + 3) & ~3;
2136103285Sikob	for(i = 0 ; i < len ; i+= 4){
2137103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2138103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2139103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2140103285Sikob		}
2141103285Sikob		crc &= 0xffff;
2142103285Sikob	}
2143129585Sdfr	return((uint16_t) crc);
2144103285Sikob}
2145106815Ssimokawa
2146110270Ssimokawastatic int
2147110072Ssimokawafw_bmr(struct firewire_comm *fc)
2148110072Ssimokawa{
2149110072Ssimokawa	struct fw_device fwdev;
2150113584Ssimokawa	union fw_self_id *self_id;
2151110072Ssimokawa	int cmstr;
2152129585Sdfr	uint32_t quad;
2153110072Ssimokawa
2154113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2155129274Sdfr	self_id = fw_find_self_id(fc, fc->max_node);
2156113584Ssimokawa	if (fc->max_node > 0) {
2157114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2158114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2159113584Ssimokawa			cmstr = fc->max_node;
2160114909Ssimokawa		else {
2161114909Ssimokawa			device_printf(fc->bdev,
2162114909Ssimokawa				"root node is not cycle master capable\n");
2163114909Ssimokawa			/* XXX shall we be the cycle master? */
2164113584Ssimokawa			cmstr = fc->nodeid;
2165114909Ssimokawa			/* XXX need bus reset */
2166114909Ssimokawa		}
2167113584Ssimokawa	} else
2168113584Ssimokawa		cmstr = -1;
2169114909Ssimokawa
2170114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2171114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2172114909Ssimokawa		/* We are not the bus manager */
2173114909Ssimokawa		printf("\n");
2174114909Ssimokawa		return(0);
2175114909Ssimokawa	}
2176114909Ssimokawa	printf("(me)\n");
2177114909Ssimokawa
2178114909Ssimokawa	/* Optimize gapcount */
2179113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2180113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2181110072Ssimokawa	/* If we are the cycle master, nothing to do */
2182113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2183110072Ssimokawa		return 0;
2184110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2185110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2186110072Ssimokawa	fwdev.fc = fc;
2187110072Ssimokawa	fwdev.dst = cmstr;
2188110072Ssimokawa	fwdev.speed = 0;
2189110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2190110072Ssimokawa	fwdev.status = FWDEVINIT;
2191110072Ssimokawa	/* Set cmstr bit on the cycle master */
2192120660Ssimokawa	quad = htonl(1 << 8);
2193110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2194120660Ssimokawa		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2195110072Ssimokawa
2196110072Ssimokawa	return 0;
2197110072Ssimokawa}
2198110072Ssimokawa
2199118455Ssimokawastatic int
2200118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2201118455Ssimokawa{
2202118455Ssimokawa	int err = 0;
2203127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2204118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2205118455Ssimokawa#endif
2206118455Ssimokawa
2207118455Ssimokawa	switch (type) {
2208118455Ssimokawa	case MOD_LOAD:
2209127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2210118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2211118455Ssimokawa						fwdev_clone, 0, 1000);
2212118455Ssimokawa#endif
2213118455Ssimokawa		break;
2214118455Ssimokawa	case MOD_UNLOAD:
2215127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2216118455Ssimokawa		if (fwdev_ehtag != NULL)
2217118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2218118455Ssimokawa#endif
2219118455Ssimokawa		break;
2220118455Ssimokawa	case MOD_SHUTDOWN:
2221118455Ssimokawa		break;
2222132199Sphk	default:
2223132199Sphk		return (EOPNOTSUPP);
2224118455Ssimokawa	}
2225118455Ssimokawa	return (err);
2226118455Ssimokawa}
2227118455Ssimokawa
2228118455Ssimokawa
2229127468Ssimokawa#ifdef __DragonFly__
2230127468SsimokawaDECLARE_DUMMY_MODULE(firewire);
2231127468Ssimokawa#endif
2232118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2233103285SikobMODULE_VERSION(firewire, 1);
2234