firewire.c revision 130460
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 130460 2004-06-14 09:34:20Z dfr $
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;
201103285Sikob	int tl = 0, len;
202103285Sikob	struct fw_pkt *fp;
203103285Sikob	int tcode;
204103285Sikob	struct tcode_info *info;
205103285Sikob
206103285Sikob	if(xfer == NULL) return EINVAL;
207103285Sikob	if(xfer->act.hand == NULL){
208103285Sikob		printf("act.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	xfer->retry_req = fw_asybusy;
261103285Sikob
262103285Sikob	fw_asystart(xfer);
263103285Sikob	return err;
264103285Sikob}
265103285Sikob/*
266103285Sikob * Wakeup blocked process.
267103285Sikob */
268103285Sikobvoid
269103285Sikobfw_asy_callback(struct fw_xfer *xfer){
270103285Sikob	wakeup(xfer);
271103285Sikob	return;
272103285Sikob}
273103285Sikob/*
274103285Sikob * Postpone to later retry.
275103285Sikob */
276103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
277103285Sikob	printf("fw_asybusy\n");
278103285Sikob/*
279103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
280103285Sikob*/
281124145Ssimokawa#if 0
282103285Sikob	DELAY(20000);
283124145Ssimokawa#endif
284103285Sikob	fw_asystart(xfer);
285103285Sikob	return;
286103285Sikob}
287103285Sikob
288103285Sikob/*
289103285Sikob * Async. request with given xfer structure.
290103285Sikob */
291106790Ssimokawastatic void
292106790Ssimokawafw_asystart(struct fw_xfer *xfer)
293106790Ssimokawa{
294103285Sikob	struct firewire_comm *fc = xfer->fc;
295103285Sikob	int s;
296103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
297113584Ssimokawa		device_printf(fc->bdev, "max_asyretry exceeded\n");
298103285Sikob		xfer->resp = EBUSY;
299103285Sikob		xfer->state = FWXF_BUSY;
300103285Sikob		xfer->act.hand(xfer);
301103285Sikob		return;
302103285Sikob	}
303103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
304103285Sikob	if (fc->status < FWBUSEXPLORE) {
305103285Sikob		xfer->resp = EAGAIN;
306103285Sikob		xfer->state = FWXF_BUSY;
307103285Sikob		if (xfer->act.hand != NULL)
308103285Sikob			xfer->act.hand(xfer);
309103285Sikob		return;
310103285Sikob	}
311103285Sikob#endif
312127468Ssimokawa	microtime(&xfer->tv);
313103285Sikob	s = splfw();
314103285Sikob	xfer->state = FWXF_INQ;
315103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
316103285Sikob	xfer->q->queued ++;
317103285Sikob	splx(s);
318103285Sikob	/* XXX just queue for mbuf */
319103285Sikob	if (xfer->mbuf == NULL)
320103285Sikob		xfer->q->start(fc);
321103285Sikob	return;
322103285Sikob}
323106790Ssimokawa
324125238Ssimokawastatic void
325125238Ssimokawafirewire_identify(driver_t *driver, device_t parent)
326125238Ssimokawa{
327125238Ssimokawa	BUS_ADD_CHILD(parent, 0, "firewire", -1);
328125238Ssimokawa}
329125238Ssimokawa
330103285Sikobstatic int
331125238Ssimokawafirewire_probe(device_t dev)
332103285Sikob{
333108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
334125238Ssimokawa	return (0);
335103285Sikob}
336106790Ssimokawa
337110577Ssimokawastatic void
338110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
339110577Ssimokawa{
340110577Ssimokawa	struct fw_xfer *xfer;
341110577Ssimokawa	struct tlabel *tl;
342110577Ssimokawa	struct timeval tv;
343110577Ssimokawa	struct timeval split_timeout;
344111040Ssimokawa	int i, s;
345110577Ssimokawa
346120660Ssimokawa	split_timeout.tv_sec = 0;
347120660Ssimokawa	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
348110577Ssimokawa
349110577Ssimokawa	microtime(&tv);
350110577Ssimokawa	timevalsub(&tv, &split_timeout);
351110577Ssimokawa
352111040Ssimokawa	s = splfw();
353110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
354110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
355110577Ssimokawa			xfer = tl->xfer;
356110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
357110577Ssimokawa				/* the rests are newer than this */
358110577Ssimokawa				break;
359124145Ssimokawa			if (xfer->state == FWXF_START)
360124145Ssimokawa				/* not sent yet */
361124145Ssimokawa				break;
362110577Ssimokawa			device_printf(fc->bdev,
363117716Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
364120660Ssimokawa				xfer->send.hdr.mode.hdr.dst, i, xfer->state);
365110577Ssimokawa			xfer->resp = ETIMEDOUT;
366110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
367113584Ssimokawa			fw_xfer_done(xfer);
368110577Ssimokawa		}
369110577Ssimokawa	}
370111040Ssimokawa	splx(s);
371110577Ssimokawa}
372110577Ssimokawa
373121463Ssimokawa#define WATCHDOC_HZ 10
374110577Ssimokawastatic void
375110577Ssimokawafirewire_watchdog(void *arg)
376110577Ssimokawa{
377110577Ssimokawa	struct firewire_comm *fc;
378121463Ssimokawa	static int watchdoc_clock = 0;
379110577Ssimokawa
380110577Ssimokawa	fc = (struct firewire_comm *)arg;
381121463Ssimokawa
382121463Ssimokawa	/*
383121463Ssimokawa	 * At boot stage, the device interrupt is disabled and
384121463Ssimokawa	 * We encounter a timeout easily. To avoid this,
385121463Ssimokawa	 * ignore clock interrupt for a while.
386121463Ssimokawa	 */
387121463Ssimokawa	if (watchdoc_clock > WATCHDOC_HZ * 15) {
388121463Ssimokawa		firewire_xfer_timeout(fc);
389121463Ssimokawa		fc->timeout(fc);
390121463Ssimokawa	} else
391121463Ssimokawa		watchdoc_clock ++;
392121463Ssimokawa
393121463Ssimokawa	callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ,
394110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
395110577Ssimokawa}
396110577Ssimokawa
397103285Sikob/*
398103285Sikob * The attach routine.
399103285Sikob */
400103285Sikobstatic int
401118455Ssimokawafirewire_attach(device_t dev)
402103285Sikob{
403118455Ssimokawa	int unit;
404103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
405103285Sikob	device_t pa = device_get_parent(dev);
406103285Sikob	struct firewire_comm *fc;
407103285Sikob
408103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
409103285Sikob	sc->fc = fc;
410116978Ssimokawa	fc->status = FWBUSNOTREADY;
411103285Sikob
412118455Ssimokawa	unit = device_get_unit(dev);
413118455Ssimokawa	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
414103285Sikob
415118455Ssimokawa	fwdev_makedev(sc);
416118455Ssimokawa
417110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
418110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
419110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
420110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
421108853Ssimokawa
422110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
423110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
424110193Ssimokawa
425103285Sikob	/* Locate our children */
426103285Sikob	bus_generic_probe(dev);
427103285Sikob
428103285Sikob	/* launch attachement of the added children */
429103285Sikob	bus_generic_attach(dev);
430103285Sikob
431103285Sikob	/* bus_reset */
432125238Ssimokawa	fw_busreset(fc);
433103285Sikob	fc->ibr(fc);
434103285Sikob
435103285Sikob	return 0;
436103285Sikob}
437103285Sikob
438103285Sikob/*
439103285Sikob * Attach it as child.
440103285Sikob */
441103285Sikobstatic device_t
442103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
443103285Sikob{
444103285Sikob        device_t child;
445103285Sikob	struct firewire_softc *sc;
446103285Sikob
447103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
448103285Sikob	child = device_add_child(dev, name, unit);
449103285Sikob	if (child) {
450103285Sikob		device_set_ivars(child, sc->fc);
451103285Sikob		device_probe_and_attach(child);
452103285Sikob	}
453103285Sikob
454103285Sikob	return child;
455103285Sikob}
456106790Ssimokawa
457116978Ssimokawastatic int
458116978Ssimokawafirewire_resume(device_t dev)
459116978Ssimokawa{
460116978Ssimokawa	struct firewire_softc *sc;
461116978Ssimokawa
462116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
463116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
464116978Ssimokawa
465116978Ssimokawa	bus_generic_resume(dev);
466116978Ssimokawa
467116978Ssimokawa	return(0);
468116978Ssimokawa}
469116978Ssimokawa
470103285Sikob/*
471103285Sikob * Dettach it.
472103285Sikob */
473103285Sikobstatic int
474118455Ssimokawafirewire_detach(device_t dev)
475103285Sikob{
476103285Sikob	struct firewire_softc *sc;
477111074Ssimokawa	struct csrdir *csrd, *next;
478111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
479118455Ssimokawa	int err;
480103285Sikob
481103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
482118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
483118455Ssimokawa		return err;
484106790Ssimokawa
485118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
486118455Ssimokawa		return err;
487111078Ssimokawa
488111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
489111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
490111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
491111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
492111078Ssimokawa
493103285Sikob	/* XXX xfree_free and untimeout on all xfers */
494111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
495111078Ssimokawa							fwdev = fwdev_next) {
496111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
497111078Ssimokawa		free(fwdev, M_FW);
498111078Ssimokawa	}
499111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
500111074Ssimokawa		next = SLIST_NEXT(csrd, link);
501111074Ssimokawa		free(csrd, M_FW);
502111074Ssimokawa	}
503110195Ssimokawa	free(sc->fc->topology_map, M_FW);
504110195Ssimokawa	free(sc->fc->speed_map, M_FW);
505116376Ssimokawa	free(sc->fc->crom_src_buf, M_FW);
506103285Sikob	return(0);
507103285Sikob}
508103285Sikob#if 0
509103285Sikobstatic int
510103285Sikobfirewire_shutdown( device_t dev )
511103285Sikob{
512103285Sikob	return 0;
513103285Sikob}
514103285Sikob#endif
515106790Ssimokawa
516110577Ssimokawa
517110577Ssimokawastatic void
518110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
519110577Ssimokawa{
520110577Ssimokawa	struct fw_xfer *xfer;
521110577Ssimokawa
522110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
523110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
524111942Ssimokawa		xferq->queued --;
525110577Ssimokawa		xfer->resp = EAGAIN;
526113584Ssimokawa		fw_xfer_done(xfer);
527110577Ssimokawa	}
528110577Ssimokawa}
529110577Ssimokawa
530110798Ssimokawavoid
531110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
532110798Ssimokawa{
533110798Ssimokawa	int i;
534110798Ssimokawa
535110798Ssimokawa	fw_xferq_drain(fc->atq);
536110798Ssimokawa	fw_xferq_drain(fc->ats);
537110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
538110798Ssimokawa		fw_xferq_drain(fc->it[i]);
539110798Ssimokawa}
540110798Ssimokawa
541116376Ssimokawastatic void
542116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
543103285Sikob{
544103285Sikob	int i;
545103285Sikob
546103285Sikob	CSRARC(fc, STATE_CLEAR)
547103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
548103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
549103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
550103285Sikob
551103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
552103285Sikob	fc->irm = -1;
553103285Sikob
554103285Sikob	fc->max_node = -1;
555103285Sikob
556103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
557103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
558103285Sikob	}
559103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
560103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
561103285Sikob	CSRARC(fc, RESET_START) = 0;
562103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
563103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
564103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
565103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
566103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
567103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
568103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
569103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
570103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
571103285Sikob
572103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
573103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
574103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
575103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
576103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
577103285Sikob
578103285Sikob/* DV depend CSRs see blue book */
579103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
580103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
581103285Sikob
582103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
583103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
584116376Ssimokawa}
585113584Ssimokawa
586116376Ssimokawastatic void
587116376Ssimokawafw_init_crom(struct firewire_comm *fc)
588116376Ssimokawa{
589116376Ssimokawa	struct crom_src *src;
590116376Ssimokawa
591116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
592116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
593116376Ssimokawa	if (fc->crom_src_buf == NULL)
594116376Ssimokawa		return;
595116376Ssimokawa
596116376Ssimokawa	src = &fc->crom_src_buf->src;
597116376Ssimokawa	bzero(src, sizeof(struct crom_src));
598116376Ssimokawa
599116376Ssimokawa	/* BUS info sample */
600116376Ssimokawa	src->hdr.info_len = 4;
601116376Ssimokawa
602116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
603116376Ssimokawa
604116376Ssimokawa	src->businfo.irmc = 1;
605116376Ssimokawa	src->businfo.cmc = 1;
606116376Ssimokawa	src->businfo.isc = 1;
607116376Ssimokawa	src->businfo.bmc = 1;
608116376Ssimokawa	src->businfo.pmc = 0;
609116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
610116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
611116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
612117350Ssimokawa	src->businfo.generation = 1;
613116376Ssimokawa	src->businfo.link_spd = fc->speed;
614116376Ssimokawa
615116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
616116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
617116376Ssimokawa
618116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
619116376Ssimokawa
620116376Ssimokawa	fc->crom_src = src;
621116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
622116376Ssimokawa}
623116376Ssimokawa
624116376Ssimokawastatic void
625116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
626116376Ssimokawa{
627116376Ssimokawa	struct crom_src_buf *buf;
628116376Ssimokawa	struct crom_src *src;
629116376Ssimokawa	struct crom_chunk *root;
630116376Ssimokawa
631116376Ssimokawa	if (fc->crom_src_buf == NULL)
632116376Ssimokawa		fw_init_crom(fc);
633116376Ssimokawa
634116376Ssimokawa	buf =  fc->crom_src_buf;
635116376Ssimokawa	src = fc->crom_src;
636116376Ssimokawa	root = fc->crom_root;
637116376Ssimokawa
638116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
639116376Ssimokawa
640116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
641116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
642116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
643116376Ssimokawa	/* private company_id */
644116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
645127468Ssimokawa#ifdef __DragonFly__
646127468Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
647127468Ssimokawa	crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
648127468Ssimokawa#else
649116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
650116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
651127468Ssimokawa#endif
652116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
653116376Ssimokawa}
654116376Ssimokawa
655116376Ssimokawa/*
656116376Ssimokawa * Called after bus reset.
657116376Ssimokawa */
658116376Ssimokawavoid
659116376Ssimokawafw_busreset(struct firewire_comm *fc)
660116376Ssimokawa{
661116376Ssimokawa	struct firewire_dev_comm *fdc;
662117350Ssimokawa	struct crom_src *src;
663116376Ssimokawa	device_t *devlistp;
664117350Ssimokawa	void *newrom;
665116376Ssimokawa	int i, devcnt;
666116376Ssimokawa
667116376Ssimokawa	switch(fc->status){
668116376Ssimokawa	case FWBUSMGRELECT:
669116376Ssimokawa		callout_stop(&fc->bmr_callout);
670116376Ssimokawa		break;
671116376Ssimokawa	default:
672116376Ssimokawa		break;
673116376Ssimokawa	}
674116376Ssimokawa	fc->status = FWBUSRESET;
675116376Ssimokawa	fw_reset_csr(fc);
676116376Ssimokawa	fw_reset_crom(fc);
677116376Ssimokawa
678113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
679113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
680113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
681113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
682113584Ssimokawa				if (fdc->post_busreset != NULL)
683113584Ssimokawa					fdc->post_busreset(fdc);
684113584Ssimokawa			}
685113584Ssimokawa		free(devlistp, M_TEMP);
686113584Ssimokawa	}
687116376Ssimokawa
688117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
689117350Ssimokawa	src = &fc->crom_src_buf->src;
690129585Sdfr	crom_load(src, (uint32_t *)newrom, CROMSIZE);
691117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
692117350Ssimokawa		/* bump generation and reload */
693117350Ssimokawa		src->businfo.generation ++;
694117350Ssimokawa		/* generation must be between 0x2 and 0xF */
695117350Ssimokawa		if (src->businfo.generation < 2)
696117350Ssimokawa			src->businfo.generation ++;
697129585Sdfr		crom_load(src, (uint32_t *)newrom, CROMSIZE);
698117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
699117350Ssimokawa	}
700117350Ssimokawa	free(newrom, M_FW);
701103285Sikob}
702106790Ssimokawa
703103285Sikob/* Call once after reboot */
704106790Ssimokawavoid fw_init(struct firewire_comm *fc)
705103285Sikob{
706103285Sikob	int i;
707106543Ssimokawa	struct csrdir *csrd;
708106543Ssimokawa#ifdef FW_VMACCESS
709103285Sikob	struct fw_xfer *xfer;
710103285Sikob	struct fw_bind *fwb;
711106543Ssimokawa#endif
712103285Sikob
713103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
714103285Sikob
715103285Sikob	fc->arq->queued = 0;
716103285Sikob	fc->ars->queued = 0;
717103285Sikob	fc->atq->queued = 0;
718103285Sikob	fc->ats->queued = 0;
719103285Sikob
720103285Sikob	fc->arq->buf = NULL;
721103285Sikob	fc->ars->buf = NULL;
722103285Sikob	fc->atq->buf = NULL;
723103285Sikob	fc->ats->buf = NULL;
724103285Sikob
725113584Ssimokawa	fc->arq->flag = 0;
726113584Ssimokawa	fc->ars->flag = 0;
727113584Ssimokawa	fc->atq->flag = 0;
728113584Ssimokawa	fc->ats->flag = 0;
729103285Sikob
730103285Sikob	STAILQ_INIT(&fc->atq->q);
731103285Sikob	STAILQ_INIT(&fc->ats->q);
732103285Sikob
733103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
734103285Sikob		fc->it[i]->queued = 0;
735103285Sikob		fc->ir[i]->queued = 0;
736103285Sikob
737103285Sikob		fc->it[i]->start = NULL;
738103285Sikob		fc->ir[i]->start = NULL;
739103285Sikob
740103285Sikob		fc->it[i]->buf = NULL;
741103285Sikob		fc->ir[i]->buf = NULL;
742103285Sikob
743103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
744103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
745103285Sikob
746103285Sikob		STAILQ_INIT(&fc->it[i]->q);
747103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
748103285Sikob
749103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
750103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
751103285Sikob	}
752103285Sikob
753103285Sikob	fc->arq->maxq = FWMAXQUEUE;
754103285Sikob	fc->ars->maxq = FWMAXQUEUE;
755103285Sikob	fc->atq->maxq = FWMAXQUEUE;
756103285Sikob	fc->ats->maxq = FWMAXQUEUE;
757103285Sikob
758103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
759103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
760103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
761103285Sikob	}
762103285Sikob/* Initialize csr registers */
763103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
764103285Sikob				sizeof(struct fw_topology_map),
765110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
766103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
767103285Sikob				sizeof(struct fw_speed_map),
768110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
769103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
770103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
771103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
772103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
773103285Sikob
774110193Ssimokawa	STAILQ_INIT(&fc->devices);
775103285Sikob
776103285Sikob/* Initialize csr ROM work space */
777103285Sikob	SLIST_INIT(&fc->ongocsr);
778103285Sikob	SLIST_INIT(&fc->csrfree);
779103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
780110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
781103285Sikob		if(csrd == NULL) break;
782103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
783103285Sikob	}
784103285Sikob
785103285Sikob/* Initialize Async handlers */
786103285Sikob	STAILQ_INIT(&fc->binds);
787103285Sikob	for( i = 0 ; i < 0x40 ; i++){
788103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
789103285Sikob	}
790103285Sikob
791103285Sikob/* DV depend CSRs see blue book */
792103285Sikob#if 0
793103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
794103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
795103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
796103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
797103285Sikob	}
798103285Sikob
799103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
800103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
801103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
802103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
803103285Sikob	}
804103285Sikob#endif
805103285Sikob
806116376Ssimokawa	fc->crom_src_buf = NULL;
807103285Sikob
808106543Ssimokawa#ifdef FW_VMACCESS
809103285Sikob	xfer = fw_xfer_alloc();
810103285Sikob	if(xfer == NULL) return;
811103285Sikob
812110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
813103285Sikob	if(fwb == NULL){
814103285Sikob		fw_xfer_free(xfer);
815103285Sikob	}
816103285Sikob	xfer->act.hand = fw_vmaccess;
817103285Sikob	xfer->fc = fc;
818103285Sikob	xfer->sc = NULL;
819103285Sikob
820103285Sikob	fwb->start_hi = 0x2;
821103285Sikob	fwb->start_lo = 0;
822103285Sikob	fwb->addrlen = 0xffffffff;
823103285Sikob	fwb->xfer = xfer;
824103285Sikob	fw_bindadd(fc, fwb);
825106543Ssimokawa#endif
826103285Sikob}
827106790Ssimokawa
828120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
829120660Ssimokawa    ((fwb)->end < (addr))?1:0)
830120660Ssimokawa
831103285Sikob/*
832129541Sdfr * To lookup bound process from IEEE1394 address.
833103285Sikob */
834106813Ssimokawastruct fw_bind *
835129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
836103285Sikob{
837120660Ssimokawa	u_int64_t addr;
838103285Sikob	struct fw_bind *tfw;
839120660Ssimokawa
840120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
841120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
842120660Ssimokawa		if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0)
843103285Sikob			return(tfw);
844103285Sikob	return(NULL);
845103285Sikob}
846106790Ssimokawa
847103285Sikob/*
848103285Sikob * To bind IEEE1394 address block to process.
849103285Sikob */
850106790Ssimokawaint
851106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
852103285Sikob{
853120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
854120660Ssimokawa
855120660Ssimokawa	if (fwb->start > fwb->end) {
856127468Ssimokawa		printf("%s: invalid range\n", __func__);
857120660Ssimokawa		return EINVAL;
858120660Ssimokawa	}
859120660Ssimokawa
860120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
861120660Ssimokawa		if (fwb->end < tfw->start)
862120660Ssimokawa			break;
863120660Ssimokawa		prev = tfw;
864120660Ssimokawa	}
865120660Ssimokawa	if (prev == NULL) {
866103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
867103285Sikob		goto out;
868103285Sikob	}
869120660Ssimokawa	if (prev->end < fwb->start) {
870120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
871103285Sikob		goto out;
872103285Sikob	}
873120660Ssimokawa
874127468Ssimokawa	printf("%s: bind failed\n", __func__);
875120660Ssimokawa	return (EBUSY);
876120660Ssimokawa
877103285Sikobout:
878120660Ssimokawa	if (fwb->act_type == FWACT_CH)
879113584Ssimokawa		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
880120660Ssimokawa	return (0);
881103285Sikob}
882103285Sikob
883103285Sikob/*
884103285Sikob * To free IEEE1394 address block.
885103285Sikob */
886106790Ssimokawaint
887106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
888103285Sikob{
889120660Ssimokawa#if 0
890120660Ssimokawa	struct fw_xfer *xfer, *next;
891120660Ssimokawa#endif
892120660Ssimokawa	struct fw_bind *tfw;
893103285Sikob	int s;
894103285Sikob
895103285Sikob	s = splfw();
896120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
897120660Ssimokawa		if (tfw == fwb) {
898120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
899120660Ssimokawa			goto found;
900120660Ssimokawa		}
901120660Ssimokawa
902129541Sdfr	printf("%s: no such binding\n", __func__);
903120660Ssimokawa	splx(s);
904120660Ssimokawa	return (1);
905120660Ssimokawafound:
906120660Ssimokawa#if 0
907113584Ssimokawa	/* shall we do this? */
908113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
909113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
910113584Ssimokawa		fw_xfer_free(xfer);
911113584Ssimokawa	}
912113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
913120660Ssimokawa#endif
914113584Ssimokawa
915103285Sikob	splx(s);
916103285Sikob	return 0;
917103285Sikob}
918103285Sikob
919103285Sikob/*
920103285Sikob * To free transaction label.
921103285Sikob */
922106790Ssimokawastatic void
923106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
924103285Sikob{
925103285Sikob	struct tlabel *tl;
926103285Sikob	int s = splfw();
927103285Sikob
928103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
929103285Sikob		tl = STAILQ_NEXT(tl, link)){
930103285Sikob		if(tl->xfer == xfer){
931103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
932110195Ssimokawa			free(tl, M_FW);
933103285Sikob			splx(s);
934103285Sikob			return;
935103285Sikob		}
936103285Sikob	}
937103285Sikob	splx(s);
938103285Sikob	return;
939103285Sikob}
940106790Ssimokawa
941103285Sikob/*
942103285Sikob * To obtain XFER structure by transaction label.
943103285Sikob */
944106790Ssimokawastatic struct fw_xfer *
945106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
946103285Sikob{
947103285Sikob	struct fw_xfer *xfer;
948103285Sikob	struct tlabel *tl;
949103285Sikob	int s = splfw();
950103285Sikob
951103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
952103285Sikob		tl = STAILQ_NEXT(tl, link)){
953120660Ssimokawa		if(tl->xfer->send.hdr.mode.hdr.dst == node){
954103285Sikob			xfer = tl->xfer;
955103285Sikob			splx(s);
956110577Ssimokawa			if (firewire_debug > 2)
957110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
958103285Sikob			return(xfer);
959103285Sikob		}
960103285Sikob	}
961110577Ssimokawa	if (firewire_debug > 1)
962110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
963103285Sikob	splx(s);
964103285Sikob	return(NULL);
965103285Sikob}
966106790Ssimokawa
967103285Sikob/*
968103285Sikob * To allocate IEEE1394 XFER structure.
969103285Sikob */
970106790Ssimokawastruct fw_xfer *
971110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
972103285Sikob{
973103285Sikob	struct fw_xfer *xfer;
974106790Ssimokawa
975110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
976106790Ssimokawa	if (xfer == NULL)
977106790Ssimokawa		return xfer;
978106790Ssimokawa
979110269Ssimokawa	xfer->malloc = type;
980106790Ssimokawa
981103285Sikob	return xfer;
982103285Sikob}
983106790Ssimokawa
984113584Ssimokawastruct fw_xfer *
985113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
986113584Ssimokawa{
987113584Ssimokawa	struct fw_xfer *xfer;
988113584Ssimokawa
989113584Ssimokawa	xfer = fw_xfer_alloc(type);
990126102Scperciva	if (xfer == NULL)
991126102Scperciva		return(NULL);
992120660Ssimokawa	xfer->send.pay_len = send_len;
993120660Ssimokawa	xfer->recv.pay_len = recv_len;
994120660Ssimokawa	if (send_len > 0) {
995120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
996120660Ssimokawa		if (xfer->send.payload == NULL) {
997113584Ssimokawa			fw_xfer_free(xfer);
998113584Ssimokawa			return(NULL);
999113584Ssimokawa		}
1000113584Ssimokawa	}
1001120660Ssimokawa	if (recv_len > 0) {
1002120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1003120660Ssimokawa		if (xfer->recv.payload == NULL) {
1004120660Ssimokawa			if (xfer->send.payload != NULL)
1005120660Ssimokawa				free(xfer->send.payload, type);
1006113584Ssimokawa			fw_xfer_free(xfer);
1007113584Ssimokawa			return(NULL);
1008113584Ssimokawa		}
1009113584Ssimokawa	}
1010113584Ssimokawa	return(xfer);
1011113584Ssimokawa}
1012113584Ssimokawa
1013103285Sikob/*
1014103285Sikob * IEEE1394 XFER post process.
1015103285Sikob */
1016103285Sikobvoid
1017103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1018103285Sikob{
1019117716Ssimokawa	if (xfer->act.hand == NULL) {
1020117716Ssimokawa		printf("act.hand == NULL\n");
1021103285Sikob		return;
1022117716Ssimokawa	}
1023103285Sikob
1024121505Ssimokawa	if (xfer->fc == NULL)
1025121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
1026121505Ssimokawa
1027124145Ssimokawa	xfer->act.hand(xfer);
1028103285Sikob}
1029103285Sikob
1030106790Ssimokawavoid
1031113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
1032103285Sikob{
1033103285Sikob	int s;
1034113584Ssimokawa
1035103285Sikob	if(xfer == NULL ) return;
1036103285Sikob	if(xfer->state == FWXF_INQ){
1037103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1038103285Sikob		s = splfw();
1039103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1040103285Sikob		xfer->q->queued --;
1041103285Sikob		splx(s);
1042103285Sikob	}
1043113584Ssimokawa	if (xfer->fc != NULL) {
1044114729Ssimokawa#if 1
1045113584Ssimokawa		if(xfer->state == FWXF_START)
1046114729Ssimokawa			/*
1047114729Ssimokawa			 * This could happen if:
1048114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1049114729Ssimokawa			 *  2. firewire_watch() is called.
1050114729Ssimokawa			 */
1051114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1052103285Sikob#endif
1053113584Ssimokawa		fw_tl_free(xfer->fc, xfer);
1054103285Sikob	}
1055113584Ssimokawa	xfer->state = FWXF_INIT;
1056113584Ssimokawa	xfer->resp = 0;
1057113584Ssimokawa	xfer->retry = 0;
1058113584Ssimokawa}
1059113584Ssimokawa/*
1060113584Ssimokawa * To free IEEE1394 XFER structure.
1061113584Ssimokawa */
1062113584Ssimokawavoid
1063120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer)
1064113584Ssimokawa{
1065120660Ssimokawa	if (xfer == NULL) {
1066127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1067120660Ssimokawa		return;
1068120660Ssimokawa	}
1069113584Ssimokawa	fw_xfer_unload(xfer);
1070120660Ssimokawa	if(xfer->send.payload != NULL){
1071120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1072103285Sikob	}
1073120660Ssimokawa	if(xfer->recv.payload != NULL){
1074120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1075103285Sikob	}
1076110269Ssimokawa	free(xfer, xfer->malloc);
1077103285Sikob}
1078103285Sikob
1079120660Ssimokawavoid
1080120660Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1081120660Ssimokawa{
1082120660Ssimokawa	if (xfer == NULL) {
1083127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1084120660Ssimokawa		return;
1085120660Ssimokawa	}
1086120660Ssimokawa	fw_xfer_unload(xfer);
1087120660Ssimokawa	free(xfer, xfer->malloc);
1088120660Ssimokawa}
1089120660Ssimokawa
1090120660Ssimokawavoid
1091110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1092103285Sikob{
1093103285Sikob#if 0
1094110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1095103285Sikob				xfer->state, xfer->resp);
1096103285Sikob#endif
1097103285Sikob	fw_xfer_free(xfer);
1098103285Sikob}
1099103285Sikob
1100103285Sikob/*
1101103285Sikob * To configure PHY.
1102103285Sikob */
1103103285Sikobstatic void
1104103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1105103285Sikob{
1106103285Sikob	struct fw_xfer *xfer;
1107103285Sikob	struct fw_pkt *fp;
1108103285Sikob
1109103285Sikob	fc->status = FWBUSPHYCONF;
1110103285Sikob
1111120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1112113584Ssimokawa	if (xfer == NULL)
1113113584Ssimokawa		return;
1114103285Sikob	xfer->fc = fc;
1115103285Sikob	xfer->retry_req = fw_asybusy;
1116110072Ssimokawa	xfer->act.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	fc->retry_count = 0;
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;
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 */
1338129274Sdfr	if (!fw_find_self_id(fc, fc->ongonode)->p0.link_active) {
1339110577Ssimokawa		if (firewire_debug)
1340110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1341103285Sikob		fc->ongonode++;
1342103285Sikob		goto loop;
1343103285Sikob	}
1344103285Sikob
1345103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1346103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1347103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1348103285Sikob		fc->ongoaddr = CSRROMOFF;
1349103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1350103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1351103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1352103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1353103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1354103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1355103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1356103285Sikob	}else if(fc->ongodev == NULL){
1357110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1358110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1359103285Sikob				break;
1360103285Sikob		if(fwdev != NULL){
1361103285Sikob			fwdev->dst = fc->ongonode;
1362117350Ssimokawa			fwdev->status = FWDEVINIT;
1363117350Ssimokawa			fc->ongodev = fwdev;
1364103285Sikob			fc->ongoaddr = CSRROMOFF;
1365117350Ssimokawa			addr = 0xf0000000 | fc->ongoaddr;
1366117350Ssimokawa			goto dorequest;
1367103285Sikob		}
1368115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1369115787Ssimokawa							M_NOWAIT | M_ZERO);
1370103285Sikob		if(fwdev == NULL)
1371103285Sikob			return;
1372106810Ssimokawa		fwdev->fc = fc;
1373103285Sikob		fwdev->rommax = 0;
1374103285Sikob		fwdev->dst = fc->ongonode;
1375103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1376103285Sikob		fwdev->status = FWDEVINIT;
1377103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1378103285Sikob
1379110179Ssimokawa		pfwdev = NULL;
1380110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1381110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1382110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1383110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1384110179Ssimokawa				break;
1385110179Ssimokawa			pfwdev = tfwdev;
1386103285Sikob		}
1387110179Ssimokawa		if (pfwdev == NULL)
1388110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1389110179Ssimokawa		else
1390110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1391103285Sikob
1392108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1393107653Ssimokawa			linkspeed[fwdev->speed],
1394107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1395103285Sikob
1396103285Sikob		fc->ongodev = fwdev;
1397103285Sikob		fc->ongoaddr = CSRROMOFF;
1398103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1399103285Sikob	}else{
1400103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1401103285Sikob	}
1402117350Ssimokawadorequest:
1403103285Sikob#if 0
1404103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1405103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1406103285Sikob		fw_bus_explore_callback);
1407103285Sikob	if(xfer == NULL) goto done;
1408103285Sikob#else
1409120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1410103285Sikob	if(xfer == NULL){
1411103285Sikob		goto done;
1412103285Sikob	}
1413120660Ssimokawa	xfer->send.spd = 0;
1414120660Ssimokawa	fp = &xfer->send.hdr;
1415113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1416103285Sikob	fp->mode.rreqq.tlrt = 0;
1417103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1418103285Sikob	fp->mode.rreqq.pri = 0;
1419103285Sikob	fp->mode.rreqq.src = 0;
1420120660Ssimokawa	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1421113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1422103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1423103285Sikob
1424110577Ssimokawa	if (firewire_debug)
1425110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1426110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1427103285Sikob	err = fw_asyreq(fc, -1, xfer);
1428103285Sikob	if(err){
1429103285Sikob		fw_xfer_free( xfer);
1430103285Sikob		return;
1431103285Sikob	}
1432103285Sikob#endif
1433103285Sikob	return;
1434103285Sikobdone:
1435103285Sikob	/* fw_attach_devs */
1436103285Sikob	fc->status = FWBUSEXPDONE;
1437107653Ssimokawa	if (firewire_debug)
1438107653Ssimokawa		printf("bus_explore done\n");
1439103285Sikob	fw_attach_dev(fc);
1440103285Sikob	return;
1441103285Sikob
1442103285Sikob}
1443106790Ssimokawa
1444103285Sikob/* Portable Async. request read quad */
1445106790Ssimokawastruct fw_xfer *
1446129585Sdfrasyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt,
1447129585Sdfr	uint32_t addr_hi, uint32_t addr_lo,
1448124169Ssimokawa	void (*hand) (struct fw_xfer*))
1449103285Sikob{
1450103285Sikob	struct fw_xfer *xfer;
1451103285Sikob	struct fw_pkt *fp;
1452103285Sikob	int err;
1453103285Sikob
1454120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1455113584Ssimokawa	if (xfer == NULL)
1456103285Sikob		return NULL;
1457113584Ssimokawa
1458120660Ssimokawa	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1459120660Ssimokawa	fp = &xfer->send.hdr;
1460113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1461103285Sikob	if(tl & FWP_TL_VALID){
1462103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1463103285Sikob	}else{
1464103285Sikob		fp->mode.rreqq.tlrt = 0;
1465103285Sikob	}
1466103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1467103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1468103285Sikob	fp->mode.rreqq.pri = 0;
1469103285Sikob	fp->mode.rreqq.src = 0;
1470120660Ssimokawa	fp->mode.rreqq.dst = addr_hi >> 16;
1471113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1472103285Sikob	xfer->act.hand = hand;
1473103285Sikob
1474103285Sikob	err = fw_asyreq(fc, -1, xfer);
1475103285Sikob	if(err){
1476103285Sikob		fw_xfer_free( xfer);
1477103285Sikob		return NULL;
1478103285Sikob	}
1479103285Sikob	return xfer;
1480103285Sikob}
1481106790Ssimokawa
1482103285Sikob/*
1483103285Sikob * Callback for the IEEE1394 bus information collection.
1484103285Sikob */
1485106790Ssimokawastatic void
1486106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1487106790Ssimokawa{
1488103285Sikob	struct firewire_comm *fc;
1489103285Sikob	struct fw_pkt *sfp,*rfp;
1490103285Sikob	struct csrhdr *chdr;
1491103285Sikob	struct csrdir *csrd;
1492103285Sikob	struct csrreg *csrreg;
1493129585Sdfr	uint32_t offset;
1494103285Sikob
1495103285Sikob
1496110577Ssimokawa	if(xfer == NULL) {
1497110577Ssimokawa		printf("xfer == NULL\n");
1498110577Ssimokawa		return;
1499110577Ssimokawa	}
1500103285Sikob	fc = xfer->fc;
1501110577Ssimokawa
1502110577Ssimokawa	if (firewire_debug)
1503110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1504110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1505110577Ssimokawa
1506103285Sikob	if(xfer->resp != 0){
1507127468Ssimokawa		device_printf(fc->bdev,
1508127468Ssimokawa		    "bus_explore node=%d addr=0x%x resp=%d retry=%d\n",
1509127468Ssimokawa		    fc->ongonode, fc->ongoaddr, xfer->resp, xfer->retry);
1510127468Ssimokawa		if (xfer->retry < fc->max_asyretry) {
1511127468Ssimokawa			fw_asystart(xfer);
1512127468Ssimokawa			return;
1513127468Ssimokawa		}
1514114284Ssimokawa		goto errnode;
1515103285Sikob	}
1516103285Sikob
1517120660Ssimokawa	sfp = &xfer->send.hdr;
1518120660Ssimokawa	rfp = &xfer->recv.hdr;
1519103285Sikob#if 0
1520103285Sikob	{
1521129585Sdfr		uint32_t *qld;
1522103285Sikob		int i;
1523129585Sdfr		qld = (uint32_t *)xfer->recv.buf;
1524103285Sikob		printf("len:%d\n", xfer->recv.len);
1525103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1526113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1527103285Sikob			if((i % 16) == 15) printf("\n");
1528103285Sikob		}
1529103285Sikob		if((i % 16) != 15) printf("\n");
1530103285Sikob	}
1531103285Sikob#endif
1532103285Sikob	if(fc->ongodev == NULL){
1533113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1534103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1535103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1536129541Sdfr/* If CSR is minimal confinguration, more investigation is not needed. */
1537103285Sikob			if(chdr->info_len == 1){
1538110577Ssimokawa				if (firewire_debug)
1539110577Ssimokawa					printf("node%d: minimal config\n",
1540110577Ssimokawa								fc->ongonode);
1541103285Sikob				goto nextnode;
1542103285Sikob			}else{
1543103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1544103285Sikob			}
1545113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1546103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1547103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1548113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1549103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1550110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1551110577Ssimokawa				if (firewire_debug)
1552110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1553110577Ssimokawa							fc->ongonode);
1554103285Sikob				goto nextnode;
1555110577Ssimokawa			}
1556103285Sikob			fc->ongoaddr = CSRROMOFF;
1557103285Sikob		}
1558103285Sikob	}else{
1559117350Ssimokawa		if (fc->ongoaddr == CSRROMOFF &&
1560117350Ssimokawa		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1561117350Ssimokawa			fc->ongodev->status = FWDEVATTACHED;
1562117350Ssimokawa			goto nextnode;
1563117350Ssimokawa		}
1564103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1565103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1566103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1567103285Sikob		}
1568103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1569103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1570103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1571103285Sikob			offset = CSRROMOFF;
1572103285Sikob		}else{
1573103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1574103285Sikob			offset = csrd->off;
1575103285Sikob		}
1576103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1577103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1578103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1579103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1580103285Sikob				if(csrd == NULL){
1581103285Sikob					goto nextnode;
1582103285Sikob				}else{
1583103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1584103285Sikob					fc->ongoaddr += csrreg->val * 4;
1585103285Sikob					csrd->off = fc->ongoaddr;
1586103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1587103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1588103285Sikob					goto nextaddr;
1589103285Sikob				}
1590103285Sikob			}
1591103285Sikob		}
1592103285Sikob		fc->ongoaddr += 4;
1593103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1594103285Sikob				(fc->ongodev->rommax < 0x414)){
1595103285Sikob			if(fc->ongodev->rommax <= 0x414){
1596103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1597103285Sikob				if(csrd == NULL) goto nextnode;
1598103285Sikob				csrd->off = fc->ongoaddr;
1599103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1600103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1601103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1602103285Sikob			}
1603103285Sikob			goto nextaddr;
1604103285Sikob		}
1605103285Sikob
1606103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1607103285Sikob			if(csrd == NULL){
1608103285Sikob				goto nextnode;
1609103285Sikob			};
1610103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1611103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1612103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1613103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1614103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1615103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1616103285Sikob				offset = CSRROMOFF;
1617103285Sikob			}else{
1618103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1619103285Sikob				offset = csrd->off;
1620103285Sikob			}
1621103285Sikob		}
1622103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1623103285Sikob			goto nextnode;
1624103285Sikob		}
1625103285Sikob	}
1626103285Sikobnextaddr:
1627103285Sikob	fw_xfer_free( xfer);
1628103285Sikob	fw_bus_explore(fc);
1629103285Sikob	return;
1630114284Ssimokawaerrnode:
1631114284Ssimokawa	fc->retry_count++;
1632127468Ssimokawa	if (fc->ongodev != NULL) {
1633114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1634127468Ssimokawa		/* Invalidate ROM */
1635127468Ssimokawa		fc->ongodev->csrrom[0] = 0;
1636127468Ssimokawa	}
1637103285Sikobnextnode:
1638103285Sikob	fw_xfer_free( xfer);
1639103285Sikob	fc->ongonode++;
1640103285Sikob/* housekeeping work space */
1641103285Sikob	fc->ongoaddr = CSRROMOFF;
1642103285Sikob	fc->ongodev = NULL;
1643103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1644103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1645103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1646103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1647103285Sikob	}
1648103285Sikob	fw_bus_explore(fc);
1649103285Sikob	return;
1650103285Sikob}
1651103285Sikob
1652103285Sikob/*
1653103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1654103285Sikob */
1655106815Ssimokawastatic void
1656106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1657103285Sikob{
1658120850Ssimokawa	struct fw_device *fwdev, *next;
1659103285Sikob	int i, err;
1660103285Sikob	device_t *devlistp;
1661103285Sikob	int devcnt;
1662103285Sikob	struct firewire_dev_comm *fdc;
1663103285Sikob
1664120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1665120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1666120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1667114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1668120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1669120850Ssimokawa			fwdev->rcnt ++;
1670120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1671120850Ssimokawa				/*
1672120850Ssimokawa				 * Remove devices which have not been seen
1673120850Ssimokawa				 * for a while.
1674120850Ssimokawa				 */
1675120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1676120850Ssimokawa				    link);
1677120850Ssimokawa				free(fwdev, M_FW);
1678120850Ssimokawa			}
1679120850Ssimokawa		}
1680120850Ssimokawa	}
1681103285Sikob
1682108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1683103285Sikob	if( err != 0 )
1684103285Sikob		return;
1685103285Sikob	for( i = 0 ; i < devcnt ; i++){
1686103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1687103285Sikob			fdc = device_get_softc(devlistp[i]);
1688103285Sikob			if (fdc->post_explore != NULL)
1689103285Sikob				fdc->post_explore(fdc);
1690103285Sikob		}
1691103285Sikob	}
1692103285Sikob	free(devlistp, M_TEMP);
1693103285Sikob
1694103285Sikob	if (fc->retry_count > 0) {
1695127468Ssimokawa		device_printf(fc->bdev, "bus_explore failed for %d nodes\n",
1696127468Ssimokawa		    fc->retry_count);
1697111040Ssimokawa#if 0
1698110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1699110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1700111040Ssimokawa#endif
1701103285Sikob	}
1702103285Sikob	return;
1703103285Sikob}
1704106815Ssimokawa
1705103285Sikob/*
1706129541Sdfr * To allocate unique transaction label.
1707103285Sikob */
1708106815Ssimokawastatic int
1709106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1710103285Sikob{
1711103285Sikob	u_int i;
1712103285Sikob	struct tlabel *tl, *tmptl;
1713103285Sikob	int s;
1714129585Sdfr	static uint32_t label = 0;
1715103285Sikob
1716103285Sikob	s = splfw();
1717103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1718103285Sikob		label = (label + 1) & 0x3f;
1719103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1720103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1721120660Ssimokawa			if (tmptl->xfer->send.hdr.mode.hdr.dst ==
1722120660Ssimokawa			    xfer->send.hdr.mode.hdr.dst)
1723120660Ssimokawa				break;
1724103285Sikob		}
1725103285Sikob		if(tmptl == NULL) {
1726110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1727103285Sikob			if (tl == NULL) {
1728103285Sikob				splx(s);
1729103285Sikob				return (-1);
1730103285Sikob			}
1731103285Sikob			tl->xfer = xfer;
1732103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1733103285Sikob			splx(s);
1734110577Ssimokawa			if (firewire_debug > 1)
1735110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1736120660Ssimokawa				    xfer->send.hdr.mode.hdr.dst, label);
1737103285Sikob			return(label);
1738103285Sikob		}
1739103285Sikob	}
1740103285Sikob	splx(s);
1741103285Sikob
1742130460Sdfr	if (firewire_debug > 1)
1743130460Sdfr		printf("fw_get_tlabel: no free tlabel\n");
1744103285Sikob	return(-1);
1745103285Sikob}
1746106815Ssimokawa
1747113584Ssimokawastatic void
1748120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1749113584Ssimokawa{
1750120660Ssimokawa	struct fw_pkt *pkt;
1751120660Ssimokawa	u_char *p;
1752120660Ssimokawa	struct tcode_info *tinfo;
1753120660Ssimokawa	u_int res, i, len, plen;
1754113584Ssimokawa
1755120660Ssimokawa	rb->xfer->recv.spd -= rb->spd;
1756120660Ssimokawa
1757120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1758120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1759120660Ssimokawa
1760120660Ssimokawa	/* Copy header */
1761120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1762120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1763120660Ssimokawa	(u_char *)rb->vec->iov_base += tinfo->hdr_len;
1764120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1765120660Ssimokawa
1766120660Ssimokawa	/* Copy payload */
1767120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1768120660Ssimokawa	res = rb->xfer->recv.pay_len;
1769120660Ssimokawa
1770120660Ssimokawa	/* special handling for RRESQ */
1771120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1772129585Sdfr	    p != NULL && res >= sizeof(uint32_t)) {
1773129585Sdfr		*(uint32_t *)p = pkt->mode.rresq.data;
1774129585Sdfr		rb->xfer->recv.pay_len = sizeof(uint32_t);
1775120660Ssimokawa		return;
1776120660Ssimokawa	}
1777120660Ssimokawa
1778120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1779120660Ssimokawa		return;
1780120660Ssimokawa
1781120660Ssimokawa	plen = pkt->mode.rresb.len;
1782120660Ssimokawa
1783120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1784120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1785113584Ssimokawa		if (res < len) {
1786113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1787120660Ssimokawa			    rb->xfer->recv.pay_len, len - res);
1788113584Ssimokawa			len = res;
1789113584Ssimokawa		}
1790120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1791113584Ssimokawa		p += len;
1792113584Ssimokawa		res -= len;
1793120660Ssimokawa		plen -= len;
1794120660Ssimokawa		if (res == 0 || plen == 0)
1795113584Ssimokawa			break;
1796113584Ssimokawa	}
1797120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1798120660Ssimokawa
1799113584Ssimokawa}
1800113584Ssimokawa
1801103285Sikob/*
1802129541Sdfr * Generic packet receiving process.
1803103285Sikob */
1804106815Ssimokawavoid
1805120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1806103285Sikob{
1807103285Sikob	struct fw_pkt *fp, *resfp;
1808103285Sikob	struct fw_bind *bind;
1809113584Ssimokawa	int tcode, s;
1810113584Ssimokawa	int i, len, oldstate;
1811103285Sikob#if 0
1812103285Sikob	{
1813129585Sdfr		uint32_t *qld;
1814103285Sikob		int i;
1815129585Sdfr		qld = (uint32_t *)buf;
1816103285Sikob		printf("spd %d len:%d\n", spd, len);
1817103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1818103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1819103285Sikob			if((i % 16) == 15) printf("\n");
1820103285Sikob		}
1821103285Sikob		if((i % 16) != 15) printf("\n");
1822103285Sikob	}
1823103285Sikob#endif
1824120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1825113584Ssimokawa	tcode = fp->mode.common.tcode;
1826113584Ssimokawa	switch (tcode) {
1827103285Sikob	case FWTCODE_WRES:
1828103285Sikob	case FWTCODE_RRESQ:
1829103285Sikob	case FWTCODE_RRESB:
1830103285Sikob	case FWTCODE_LRES:
1831120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1832103285Sikob					fp->mode.hdr.tlrt >> 2);
1833120660Ssimokawa		if(rb->xfer == NULL) {
1834103285Sikob			printf("fw_rcv: unknown response "
1835124251Ssimokawa			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1836124251Ssimokawa			    tcode_str[tcode], tcode,
1837124251Ssimokawa			    fp->mode.hdr.src,
1838124251Ssimokawa			    fp->mode.hdr.tlrt >> 2,
1839124251Ssimokawa			    fp->mode.hdr.tlrt & 3,
1840124251Ssimokawa			    fp->mode.rresq.data);
1841103285Sikob#if 1
1842103285Sikob			printf("try ad-hoc work around!!\n");
1843120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1844103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1845120660Ssimokawa			if (rb->xfer == NULL) {
1846103285Sikob				printf("no use...\n");
1847103285Sikob				goto err;
1848103285Sikob			}
1849103285Sikob#else
1850103285Sikob			goto err;
1851103285Sikob#endif
1852103285Sikob		}
1853120660Ssimokawa		fw_rcv_copy(rb);
1854120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1855120660Ssimokawa			rb->xfer->resp = EIO;
1856120660Ssimokawa		else
1857120660Ssimokawa			rb->xfer->resp = 0;
1858113584Ssimokawa		/* make sure the packet is drained in AT queue */
1859120660Ssimokawa		oldstate = rb->xfer->state;
1860120660Ssimokawa		rb->xfer->state = FWXF_RCVD;
1861113584Ssimokawa		switch (oldstate) {
1862113584Ssimokawa		case FWXF_SENT:
1863120660Ssimokawa			fw_xfer_done(rb->xfer);
1864103285Sikob			break;
1865113584Ssimokawa		case FWXF_START:
1866119289Ssimokawa#if 0
1867113584Ssimokawa			if (firewire_debug)
1868120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1869119289Ssimokawa#endif
1870113584Ssimokawa			break;
1871103285Sikob		default:
1872120660Ssimokawa			printf("unexpected state %d\n", rb->xfer->state);
1873103285Sikob		}
1874113584Ssimokawa		return;
1875103285Sikob	case FWTCODE_WREQQ:
1876103285Sikob	case FWTCODE_WREQB:
1877103285Sikob	case FWTCODE_RREQQ:
1878103285Sikob	case FWTCODE_RREQB:
1879103285Sikob	case FWTCODE_LREQ:
1880120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1881113584Ssimokawa			fp->mode.rreqq.dest_lo);
1882103285Sikob		if(bind == NULL){
1883124251Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1884127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1885127468Ssimokawa			    " src=0x%x data=%lx\n",
1886127468Ssimokawa#else
1887124251Ssimokawa			    " src=0x%x data=%x\n",
1888113962Ssimokawa#endif
1889124251Ssimokawa			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1890124251Ssimokawa			    tcode_str[tcode], tcode,
1891124251Ssimokawa			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1892120660Ssimokawa			if (rb->fc->status == FWBUSRESET) {
1893110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1894103285Sikob				goto err;
1895103285Sikob			}
1896120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
1897120660Ssimokawa			if(rb->xfer == NULL){
1898103285Sikob				return;
1899103285Sikob			}
1900120660Ssimokawa			rb->xfer->send.spd = rb->spd;
1901120660Ssimokawa			rb->xfer->send.pay_len = 0;
1902120660Ssimokawa			resfp = &rb->xfer->send.hdr;
1903113584Ssimokawa			switch (tcode) {
1904103285Sikob			case FWTCODE_WREQQ:
1905103285Sikob			case FWTCODE_WREQB:
1906103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1907103285Sikob				break;
1908103285Sikob			case FWTCODE_RREQQ:
1909103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1910103285Sikob				break;
1911103285Sikob			case FWTCODE_RREQB:
1912103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1913103285Sikob				break;
1914103285Sikob			case FWTCODE_LREQ:
1915103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1916103285Sikob				break;
1917103285Sikob			}
1918103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1919103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1920103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1921120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1922103285Sikob			resfp->mode.rresb.extcode = 0;
1923103285Sikob			resfp->mode.rresb.len = 0;
1924103285Sikob/*
1925120660Ssimokawa			rb->xfer->act.hand = fw_asy_callback;
1926103285Sikob*/
1927120660Ssimokawa			rb->xfer->act.hand = fw_xfer_free;
1928120660Ssimokawa			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1929120660Ssimokawa				fw_xfer_free(rb->xfer);
1930103285Sikob				return;
1931103285Sikob			}
1932103285Sikob			goto err;
1933103285Sikob		}
1934113584Ssimokawa		len = 0;
1935120660Ssimokawa		for (i = 0; i < rb->nvec; i ++)
1936120660Ssimokawa			len += rb->vec[i].iov_len;
1937113584Ssimokawa		switch(bind->act_type){
1938103285Sikob		case FWACT_XFER:
1939113584Ssimokawa			/* splfw()?? */
1940120660Ssimokawa			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1941120660Ssimokawa			if (rb->xfer == NULL) {
1942113584Ssimokawa				printf("Discard a packet for this bind.\n");
1943113584Ssimokawa				goto err;
1944113584Ssimokawa			}
1945113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1946120660Ssimokawa			fw_rcv_copy(rb);
1947124145Ssimokawa			rb->xfer->act.hand(rb->xfer);
1948103285Sikob			return;
1949103285Sikob			break;
1950103285Sikob		case FWACT_CH:
1951120660Ssimokawa			if(rb->fc->ir[bind->sub]->queued >=
1952120660Ssimokawa				rb->fc->ir[bind->sub]->maxq){
1953120660Ssimokawa				device_printf(rb->fc->bdev,
1954108655Ssimokawa					"Discard a packet %x %d\n",
1955113584Ssimokawa					bind->sub,
1956120660Ssimokawa					rb->fc->ir[bind->sub]->queued);
1957103285Sikob				goto err;
1958103285Sikob			}
1959120660Ssimokawa			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1960120660Ssimokawa			if (rb->xfer == NULL) {
1961113584Ssimokawa				printf("Discard packet for this bind\n");
1962113584Ssimokawa				goto err;
1963113584Ssimokawa			}
1964113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1965120660Ssimokawa			fw_rcv_copy(rb);
1966103285Sikob			s = splfw();
1967120660Ssimokawa			rb->fc->ir[bind->sub]->queued++;
1968120660Ssimokawa			STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q,
1969120660Ssimokawa			    rb->xfer, link);
1970103285Sikob			splx(s);
1971103285Sikob
1972120660Ssimokawa			wakeup((caddr_t)rb->fc->ir[bind->sub]);
1973103285Sikob
1974103285Sikob			return;
1975103285Sikob			break;
1976103285Sikob		default:
1977103285Sikob			goto err;
1978103285Sikob			break;
1979103285Sikob		}
1980103285Sikob		break;
1981120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
1982103285Sikob	case FWTCODE_STREAM:
1983103285Sikob	{
1984103285Sikob		struct fw_xferq *xferq;
1985103285Sikob
1986120660Ssimokawa		xferq = rb->fc->ir[sub];
1987103285Sikob#if 0
1988103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1989103285Sikob			sub, len, off, spd);
1990103285Sikob#endif
1991103285Sikob		if(xferq->queued >= xferq->maxq) {
1992103285Sikob			printf("receive queue is full\n");
1993103285Sikob			goto err;
1994103285Sikob		}
1995113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1996113584Ssimokawa			per packet mode */
1997120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1998113584Ssimokawa						vec[0].iov_len);
1999120660Ssimokawa		if (rb->xfer == NULL) goto err;
2000120660Ssimokawa		fw_rcv_copy(rb)
2001103285Sikob		s = splfw();
2002103285Sikob		xferq->queued++;
2003120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
2004103285Sikob		splx(s);
2005120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
2006127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2007127468Ssimokawa		if (&xferq->rsel.si_pid != 0)
2008127468Ssimokawa#else
2009103285Sikob		if (SEL_WAITING(&xferq->rsel))
2010103285Sikob#endif
2011122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
2012103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
2013103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
2014103285Sikob			wakeup((caddr_t)xferq);
2015103285Sikob		}
2016103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
2017103285Sikob			xferq->hand(xferq);
2018103285Sikob		}
2019103285Sikob		return;
2020103285Sikob		break;
2021103285Sikob	}
2022120660Ssimokawa#endif
2023103285Sikob	default:
2024113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
2025103285Sikob		break;
2026103285Sikob	}
2027103285Sikoberr:
2028113584Ssimokawa	return;
2029103285Sikob}
2030106815Ssimokawa
2031103285Sikob/*
2032103285Sikob * Post process for Bus Manager election process.
2033103285Sikob */
2034103285Sikobstatic void
2035103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
2036103285Sikob{
2037103285Sikob	struct firewire_comm *fc;
2038109422Ssimokawa	int bmr;
2039103285Sikob
2040109422Ssimokawa	if (xfer == NULL)
2041109422Ssimokawa		return;
2042103285Sikob	fc = xfer->fc;
2043109422Ssimokawa	if (xfer->resp != 0)
2044103285Sikob		goto error;
2045120660Ssimokawa	if (xfer->recv.payload == NULL)
2046103285Sikob		goto error;
2047120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2048103285Sikob		goto error;
2049109422Ssimokawa
2050120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
2051109422Ssimokawa	if (bmr == 0x3f)
2052109422Ssimokawa		bmr = fc->nodeid;
2053109422Ssimokawa
2054109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2055120660Ssimokawa	fw_xfer_free_buf(xfer);
2056114909Ssimokawa	fw_bmr(fc);
2057114909Ssimokawa	return;
2058114909Ssimokawa
2059103285Sikoberror:
2060114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2061120660Ssimokawa	fw_xfer_free_buf(xfer);
2062103285Sikob}
2063106815Ssimokawa
2064113584Ssimokawa
2065103285Sikob/*
2066103285Sikob * To candidate Bus Manager election process.
2067103285Sikob */
2068110270Ssimokawastatic void
2069106815Ssimokawafw_try_bmr(void *arg)
2070103285Sikob{
2071103285Sikob	struct fw_xfer *xfer;
2072103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2073103285Sikob	struct fw_pkt *fp;
2074103285Sikob	int err = 0;
2075103285Sikob
2076120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2077103285Sikob	if(xfer == NULL){
2078103285Sikob		return;
2079103285Sikob	}
2080120660Ssimokawa	xfer->send.spd = 0;
2081103285Sikob	fc->status = FWBUSMGRELECT;
2082103285Sikob
2083120660Ssimokawa	fp = &xfer->send.hdr;
2084113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2085103285Sikob	fp->mode.lreq.tlrt = 0;
2086103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2087103285Sikob	fp->mode.lreq.pri = 0;
2088103285Sikob	fp->mode.lreq.src = 0;
2089113584Ssimokawa	fp->mode.lreq.len = 8;
2090120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2091120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2092113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2093120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2094120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2095103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2096103285Sikob
2097103285Sikob	err = fw_asyreq(fc, -1, xfer);
2098103285Sikob	if(err){
2099120660Ssimokawa		fw_xfer_free_buf(xfer);
2100103285Sikob		return;
2101103285Sikob	}
2102103285Sikob	return;
2103103285Sikob}
2104106543Ssimokawa
2105106543Ssimokawa#ifdef FW_VMACCESS
2106103285Sikob/*
2107103285Sikob * Software implementation for physical memory block access.
2108103285Sikob * XXX:Too slow, usef for debug purpose only.
2109103285Sikob */
2110106815Ssimokawastatic void
2111106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2112103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2113129585Sdfr	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2114103285Sikob
2115113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2116113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2117103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2118103285Sikob	if(xfer->resp != 0){
2119103285Sikob		fw_xfer_free( xfer);
2120103285Sikob		return;
2121103285Sikob	}
2122103285Sikob	if(xfer->recv.buf == NULL){
2123103285Sikob		fw_xfer_free( xfer);
2124103285Sikob		return;
2125103285Sikob	}
2126103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2127103285Sikob	switch(rfp->mode.hdr.tcode){
2128103285Sikob		/* XXX need fix for 64bit arch */
2129103285Sikob		case FWTCODE_WREQB:
2130110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2131103285Sikob			xfer->send.len = 12;
2132103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2133103285Sikob			bcopy(rfp->mode.wreqb.payload,
2134103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2135103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2136103285Sikob			sfp->mode.wres.rtcode = 0;
2137103285Sikob			break;
2138103285Sikob		case FWTCODE_WREQQ:
2139110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2140103285Sikob			xfer->send.len = 12;
2141103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2142129585Sdfr			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2143103285Sikob			sfp->mode.wres.rtcode = 0;
2144103285Sikob			break;
2145103285Sikob		case FWTCODE_RREQB:
2146110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2147103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2148103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2149103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2150129585Sdfr				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2151103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2152103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2153103285Sikob			sfp->mode.rresb.rtcode = 0;
2154103285Sikob			sfp->mode.rresb.extcode = 0;
2155103285Sikob			break;
2156103285Sikob		case FWTCODE_RREQQ:
2157110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2158103285Sikob			xfer->send.len = 16;
2159103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2160129585Sdfr			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2161103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2162103285Sikob			sfp->mode.rresb.rtcode = 0;
2163103285Sikob			break;
2164103285Sikob		default:
2165103285Sikob			fw_xfer_free( xfer);
2166103285Sikob			return;
2167103285Sikob	}
2168103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2169103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2170103285Sikob	xfer->act.hand = fw_xfer_free;
2171103285Sikob	xfer->retry_req = fw_asybusy;
2172103285Sikob
2173103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2174103285Sikob	sfp->mode.hdr.pri = 0;
2175103285Sikob
2176103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2177103285Sikob/**/
2178103285Sikob	return;
2179103285Sikob}
2180106543Ssimokawa#endif
2181106543Ssimokawa
2182103285Sikob/*
2183103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2184103285Sikob */
2185129585Sdfruint16_t
2186129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){
2187129585Sdfr	uint32_t i, sum, crc = 0;
2188103285Sikob	int shift;
2189103285Sikob	len = (len + 3) & ~3;
2190103285Sikob	for(i = 0 ; i < len ; i+= 4){
2191103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2192103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2193103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2194103285Sikob		}
2195103285Sikob		crc &= 0xffff;
2196103285Sikob	}
2197129585Sdfr	return((uint16_t) crc);
2198103285Sikob}
2199106815Ssimokawa
2200110270Ssimokawastatic int
2201110072Ssimokawafw_bmr(struct firewire_comm *fc)
2202110072Ssimokawa{
2203110072Ssimokawa	struct fw_device fwdev;
2204113584Ssimokawa	union fw_self_id *self_id;
2205110072Ssimokawa	int cmstr;
2206129585Sdfr	uint32_t quad;
2207110072Ssimokawa
2208113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2209129274Sdfr	self_id = fw_find_self_id(fc, fc->max_node);
2210113584Ssimokawa	if (fc->max_node > 0) {
2211114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2212114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2213113584Ssimokawa			cmstr = fc->max_node;
2214114909Ssimokawa		else {
2215114909Ssimokawa			device_printf(fc->bdev,
2216114909Ssimokawa				"root node is not cycle master capable\n");
2217114909Ssimokawa			/* XXX shall we be the cycle master? */
2218113584Ssimokawa			cmstr = fc->nodeid;
2219114909Ssimokawa			/* XXX need bus reset */
2220114909Ssimokawa		}
2221113584Ssimokawa	} else
2222113584Ssimokawa		cmstr = -1;
2223114909Ssimokawa
2224114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2225114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2226114909Ssimokawa		/* We are not the bus manager */
2227114909Ssimokawa		printf("\n");
2228114909Ssimokawa		return(0);
2229114909Ssimokawa	}
2230114909Ssimokawa	printf("(me)\n");
2231114909Ssimokawa
2232114909Ssimokawa	/* Optimize gapcount */
2233113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2234113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2235110072Ssimokawa	/* If we are the cycle master, nothing to do */
2236113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2237110072Ssimokawa		return 0;
2238110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2239110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2240110072Ssimokawa	fwdev.fc = fc;
2241110072Ssimokawa	fwdev.dst = cmstr;
2242110072Ssimokawa	fwdev.speed = 0;
2243110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2244110072Ssimokawa	fwdev.status = FWDEVINIT;
2245110072Ssimokawa	/* Set cmstr bit on the cycle master */
2246120660Ssimokawa	quad = htonl(1 << 8);
2247110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2248120660Ssimokawa		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2249110072Ssimokawa
2250110072Ssimokawa	return 0;
2251110072Ssimokawa}
2252110072Ssimokawa
2253118455Ssimokawastatic int
2254118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2255118455Ssimokawa{
2256118455Ssimokawa	int err = 0;
2257127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2258118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2259118455Ssimokawa#endif
2260118455Ssimokawa
2261118455Ssimokawa	switch (type) {
2262118455Ssimokawa	case MOD_LOAD:
2263127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2264118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2265118455Ssimokawa						fwdev_clone, 0, 1000);
2266118455Ssimokawa#endif
2267118455Ssimokawa		break;
2268118455Ssimokawa	case MOD_UNLOAD:
2269127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2270118455Ssimokawa		if (fwdev_ehtag != NULL)
2271118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2272118455Ssimokawa#endif
2273118455Ssimokawa		break;
2274118455Ssimokawa	case MOD_SHUTDOWN:
2275118455Ssimokawa		break;
2276118455Ssimokawa	}
2277118455Ssimokawa	return (err);
2278118455Ssimokawa}
2279118455Ssimokawa
2280118455Ssimokawa
2281127468Ssimokawa#ifdef __DragonFly__
2282127468SsimokawaDECLARE_DUMMY_MODULE(firewire);
2283127468Ssimokawa#endif
2284118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2285103285SikobMODULE_VERSION(firewire, 1);
2286