firewire.c revision 129585
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 129585 2004-05-22 16:14:17Z 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>
43103285Sikob#include <sys/malloc.h>
44103285Sikob#include <sys/conf.h>
45103285Sikob#include <sys/sysctl.h>
46103285Sikob
47127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
48117067Ssimokawa#include <machine/clock.h>	/* for DELAY() */
49117067Ssimokawa#endif
50103285Sikob
51103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
52113584Ssimokawa#include <machine/bus.h>
53103285Sikob
54127468Ssimokawa#ifdef __DragonFly__
55127468Ssimokawa#include "firewire.h"
56127468Ssimokawa#include "firewirereg.h"
57127468Ssimokawa#include "fwmem.h"
58127468Ssimokawa#include "iec13213.h"
59127468Ssimokawa#include "iec68113.h"
60127468Ssimokawa#else
61103285Sikob#include <dev/firewire/firewire.h>
62103285Sikob#include <dev/firewire/firewirereg.h>
63110072Ssimokawa#include <dev/firewire/fwmem.h>
64103285Sikob#include <dev/firewire/iec13213.h>
65103285Sikob#include <dev/firewire/iec68113.h>
66127468Ssimokawa#endif
67103285Sikob
68116376Ssimokawastruct crom_src_buf {
69116376Ssimokawa	struct crom_src	src;
70116376Ssimokawa	struct crom_chunk root;
71116376Ssimokawa	struct crom_chunk vendor;
72116376Ssimokawa	struct crom_chunk hw;
73116376Ssimokawa};
74116376Ssimokawa
75120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3;
76103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
77108281Ssimokawa	"FireWire driver debug flag");
78109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
79109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
80109736Ssimokawa	"Try to be a bus manager");
81120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
82120850Ssimokawa	"Number of count of bus resets for removing lost device information");
83103285Sikob
84110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
85110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
86110195Ssimokawa
87103285Sikob#define FW_MAXASYRTY 4
88103285Sikob
89103285Sikobdevclass_t firewire_devclass;
90103285Sikob
91125238Ssimokawastatic void firewire_identify	(driver_t *, device_t);
92125238Ssimokawastatic int firewire_probe	(device_t);
93124169Ssimokawastatic int firewire_attach      (device_t);
94124169Ssimokawastatic int firewire_detach      (device_t);
95124169Ssimokawastatic int firewire_resume      (device_t);
96103285Sikob#if 0
97124169Ssimokawastatic int firewire_shutdown    (device_t);
98103285Sikob#endif
99124169Ssimokawastatic device_t firewire_add_child   (device_t, int, const char *, int);
100124169Ssimokawastatic void fw_try_bmr (void *);
101124169Ssimokawastatic void fw_try_bmr_callback (struct fw_xfer *);
102124169Ssimokawastatic void fw_asystart (struct fw_xfer *);
103124169Ssimokawastatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
104124169Ssimokawastatic void fw_bus_probe (struct firewire_comm *);
105124169Ssimokawastatic void fw_bus_explore (struct firewire_comm *);
106124169Ssimokawastatic void fw_bus_explore_callback (struct fw_xfer *);
107124169Ssimokawastatic void fw_attach_dev (struct firewire_comm *);
108106543Ssimokawa#ifdef FW_VMACCESS
109124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *);
110106543Ssimokawa#endif
111129585Sdfrstruct fw_xfer *asyreqq (struct firewire_comm *, uint8_t, uint8_t, uint8_t,
112129585Sdfr	uint32_t, uint32_t, void (*)(struct fw_xfer *));
113124169Ssimokawastatic int fw_bmr (struct firewire_comm *);
114103285Sikob
115103285Sikobstatic device_method_t firewire_methods[] = {
116103285Sikob	/* Device interface */
117125238Ssimokawa	DEVMETHOD(device_identify,	firewire_identify),
118125238Ssimokawa	DEVMETHOD(device_probe,		firewire_probe),
119103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
120103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
121108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
122116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
123103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
124103285Sikob
125103285Sikob	/* Bus interface */
126103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
127103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
128103285Sikob
129103285Sikob	{ 0, 0 }
130103285Sikob};
131124251Ssimokawachar *linkspeed[] = {
132124251Ssimokawa	"S100", "S200", "S400", "S800",
133124251Ssimokawa	"S1600", "S3200", "undef", "undef"
134124251Ssimokawa};
135103285Sikob
136124251Ssimokawastatic char *tcode_str[] = {
137124251Ssimokawa	"WREQQ", "WREQB", "WRES",   "undef",
138124251Ssimokawa	"RREQQ", "RREQB", "RRESQ",  "RRESB",
139124251Ssimokawa	"CYCS",  "LREQ",  "STREAM", "LRES",
140124251Ssimokawa	"undef", "undef", "PHY",    "undef"
141124251Ssimokawa};
142124251Ssimokawa
143114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
144114909Ssimokawa#define MAX_GAPHOP 15
145114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
146114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
147106813Ssimokawa
148103285Sikobstatic driver_t firewire_driver = {
149103285Sikob	"firewire",
150103285Sikob	firewire_methods,
151103285Sikob	sizeof(struct firewire_softc),
152103285Sikob};
153103285Sikob
154103285Sikob/*
155110072Ssimokawa * Lookup fwdev by node id.
156103285Sikob */
157106810Ssimokawastruct fw_device *
158110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
159103285Sikob{
160103285Sikob	struct fw_device *fwdev;
161110072Ssimokawa	int s;
162110072Ssimokawa
163110072Ssimokawa	s = splfw();
164110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
165120660Ssimokawa		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
166103285Sikob			break;
167110072Ssimokawa	splx(s);
168110072Ssimokawa
169106810Ssimokawa	return fwdev;
170103285Sikob}
171106813Ssimokawa
172103285Sikob/*
173110072Ssimokawa * Lookup fwdev by EUI64.
174110072Ssimokawa */
175110072Ssimokawastruct fw_device *
176110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
177110072Ssimokawa{
178110072Ssimokawa	struct fw_device *fwdev;
179110072Ssimokawa	int s;
180110072Ssimokawa
181110072Ssimokawa	s = splfw();
182110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
183110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
184110072Ssimokawa			break;
185110072Ssimokawa	splx(s);
186110072Ssimokawa
187110072Ssimokawa	if(fwdev == NULL) return NULL;
188110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
189110072Ssimokawa	return fwdev;
190110072Ssimokawa}
191110072Ssimokawa
192110072Ssimokawa/*
193103285Sikob * Async. request procedure for userland application.
194103285Sikob */
195103285Sikobint
196103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
197103285Sikob{
198103285Sikob	int err = 0;
199103285Sikob	struct fw_xferq *xferq;
200103285Sikob	int tl = 0, len;
201103285Sikob	struct fw_pkt *fp;
202103285Sikob	int tcode;
203103285Sikob	struct tcode_info *info;
204103285Sikob
205103285Sikob	if(xfer == NULL) return EINVAL;
206103285Sikob	if(xfer->act.hand == NULL){
207103285Sikob		printf("act.hand == NULL\n");
208103285Sikob		return EINVAL;
209103285Sikob	}
210120660Ssimokawa	fp = &xfer->send.hdr;
211103285Sikob
212103285Sikob	tcode = fp->mode.common.tcode & 0xf;
213103285Sikob	info = &fc->tcode[tcode];
214103285Sikob	if (info->flag == 0) {
215124251Ssimokawa		printf("invalid tcode=%x\n", tcode);
216103285Sikob		return EINVAL;
217103285Sikob	}
218103285Sikob	if (info->flag & FWTI_REQ)
219103285Sikob		xferq = fc->atq;
220103285Sikob	else
221103285Sikob		xferq = fc->ats;
222103285Sikob	len = info->hdr_len;
223120660Ssimokawa	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
224120660Ssimokawa		printf("send.pay_len > maxrec\n");
225120660Ssimokawa		return EINVAL;
226120660Ssimokawa	}
227103285Sikob	if (info->flag & FWTI_BLOCK_STR)
228120660Ssimokawa		len = fp->mode.stream.len;
229103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
230120660Ssimokawa		len = fp->mode.rresb.len;
231120660Ssimokawa	else
232120660Ssimokawa		len = 0;
233120660Ssimokawa	if (len != xfer->send.pay_len){
234124251Ssimokawa		printf("len(%d) != send.pay_len(%d) %s(%x)\n",
235124251Ssimokawa		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
236103285Sikob		return EINVAL;
237103285Sikob	}
238106790Ssimokawa
239103285Sikob	if(xferq->start == NULL){
240103285Sikob		printf("xferq->start == NULL\n");
241103285Sikob		return EINVAL;
242103285Sikob	}
243103285Sikob	if(!(xferq->queued < xferq->maxq)){
244108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
245108655Ssimokawa			xferq->queued);
246103285Sikob		return EINVAL;
247103285Sikob	}
248103285Sikob
249103285Sikob	if (info->flag & FWTI_TLABEL) {
250103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
251103285Sikob			return EIO;
252103285Sikob		fp->mode.hdr.tlrt = tl << 2;
253103285Sikob	}
254103285Sikob
255103285Sikob	xfer->tl = tl;
256103285Sikob	xfer->resp = 0;
257103285Sikob	xfer->fc = fc;
258103285Sikob	xfer->q = xferq;
259103285Sikob	xfer->retry_req = fw_asybusy;
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 * Postpone to later retry.
274103285Sikob */
275103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
276103285Sikob	printf("fw_asybusy\n");
277103285Sikob/*
278103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
279103285Sikob*/
280124145Ssimokawa#if 0
281103285Sikob	DELAY(20000);
282124145Ssimokawa#endif
283103285Sikob	fw_asystart(xfer);
284103285Sikob	return;
285103285Sikob}
286103285Sikob
287103285Sikob/*
288103285Sikob * Async. request with given xfer structure.
289103285Sikob */
290106790Ssimokawastatic void
291106790Ssimokawafw_asystart(struct fw_xfer *xfer)
292106790Ssimokawa{
293103285Sikob	struct firewire_comm *fc = xfer->fc;
294103285Sikob	int s;
295103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
296113584Ssimokawa		device_printf(fc->bdev, "max_asyretry exceeded\n");
297103285Sikob		xfer->resp = EBUSY;
298103285Sikob		xfer->state = FWXF_BUSY;
299103285Sikob		xfer->act.hand(xfer);
300103285Sikob		return;
301103285Sikob	}
302103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
303103285Sikob	if (fc->status < FWBUSEXPLORE) {
304103285Sikob		xfer->resp = EAGAIN;
305103285Sikob		xfer->state = FWXF_BUSY;
306103285Sikob		if (xfer->act.hand != NULL)
307103285Sikob			xfer->act.hand(xfer);
308103285Sikob		return;
309103285Sikob	}
310103285Sikob#endif
311127468Ssimokawa	microtime(&xfer->tv);
312103285Sikob	s = splfw();
313103285Sikob	xfer->state = FWXF_INQ;
314103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
315103285Sikob	xfer->q->queued ++;
316103285Sikob	splx(s);
317103285Sikob	/* XXX just queue for mbuf */
318103285Sikob	if (xfer->mbuf == NULL)
319103285Sikob		xfer->q->start(fc);
320103285Sikob	return;
321103285Sikob}
322106790Ssimokawa
323125238Ssimokawastatic void
324125238Ssimokawafirewire_identify(driver_t *driver, device_t parent)
325125238Ssimokawa{
326125238Ssimokawa	BUS_ADD_CHILD(parent, 0, "firewire", -1);
327125238Ssimokawa}
328125238Ssimokawa
329103285Sikobstatic int
330125238Ssimokawafirewire_probe(device_t dev)
331103285Sikob{
332108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
333125238Ssimokawa	return (0);
334103285Sikob}
335106790Ssimokawa
336110577Ssimokawastatic void
337110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
338110577Ssimokawa{
339110577Ssimokawa	struct fw_xfer *xfer;
340110577Ssimokawa	struct tlabel *tl;
341110577Ssimokawa	struct timeval tv;
342110577Ssimokawa	struct timeval split_timeout;
343111040Ssimokawa	int i, s;
344110577Ssimokawa
345120660Ssimokawa	split_timeout.tv_sec = 0;
346120660Ssimokawa	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
347110577Ssimokawa
348110577Ssimokawa	microtime(&tv);
349110577Ssimokawa	timevalsub(&tv, &split_timeout);
350110577Ssimokawa
351111040Ssimokawa	s = splfw();
352110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
353110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
354110577Ssimokawa			xfer = tl->xfer;
355110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
356110577Ssimokawa				/* the rests are newer than this */
357110577Ssimokawa				break;
358124145Ssimokawa			if (xfer->state == FWXF_START)
359124145Ssimokawa				/* not sent yet */
360124145Ssimokawa				break;
361110577Ssimokawa			device_printf(fc->bdev,
362117716Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
363120660Ssimokawa				xfer->send.hdr.mode.hdr.dst, i, xfer->state);
364110577Ssimokawa			xfer->resp = ETIMEDOUT;
365110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
366113584Ssimokawa			fw_xfer_done(xfer);
367110577Ssimokawa		}
368110577Ssimokawa	}
369111040Ssimokawa	splx(s);
370110577Ssimokawa}
371110577Ssimokawa
372121463Ssimokawa#define WATCHDOC_HZ 10
373110577Ssimokawastatic void
374110577Ssimokawafirewire_watchdog(void *arg)
375110577Ssimokawa{
376110577Ssimokawa	struct firewire_comm *fc;
377121463Ssimokawa	static int watchdoc_clock = 0;
378110577Ssimokawa
379110577Ssimokawa	fc = (struct firewire_comm *)arg;
380121463Ssimokawa
381121463Ssimokawa	/*
382121463Ssimokawa	 * At boot stage, the device interrupt is disabled and
383121463Ssimokawa	 * We encounter a timeout easily. To avoid this,
384121463Ssimokawa	 * ignore clock interrupt for a while.
385121463Ssimokawa	 */
386121463Ssimokawa	if (watchdoc_clock > WATCHDOC_HZ * 15) {
387121463Ssimokawa		firewire_xfer_timeout(fc);
388121463Ssimokawa		fc->timeout(fc);
389121463Ssimokawa	} else
390121463Ssimokawa		watchdoc_clock ++;
391121463Ssimokawa
392121463Ssimokawa	callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ,
393110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
394110577Ssimokawa}
395110577Ssimokawa
396103285Sikob/*
397103285Sikob * The attach routine.
398103285Sikob */
399103285Sikobstatic int
400118455Ssimokawafirewire_attach(device_t dev)
401103285Sikob{
402118455Ssimokawa	int unit;
403103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
404103285Sikob	device_t pa = device_get_parent(dev);
405103285Sikob	struct firewire_comm *fc;
406103285Sikob
407103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
408103285Sikob	sc->fc = fc;
409116978Ssimokawa	fc->status = FWBUSNOTREADY;
410103285Sikob
411118455Ssimokawa	unit = device_get_unit(dev);
412118455Ssimokawa	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
413103285Sikob
414118455Ssimokawa	fwdev_makedev(sc);
415118455Ssimokawa
416110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
417110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
418110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
419110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
420108853Ssimokawa
421110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
422110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
423110193Ssimokawa
424103285Sikob	/* Locate our children */
425103285Sikob	bus_generic_probe(dev);
426103285Sikob
427103285Sikob	/* launch attachement of the added children */
428103285Sikob	bus_generic_attach(dev);
429103285Sikob
430103285Sikob	/* bus_reset */
431125238Ssimokawa	fw_busreset(fc);
432103285Sikob	fc->ibr(fc);
433103285Sikob
434103285Sikob	return 0;
435103285Sikob}
436103285Sikob
437103285Sikob/*
438103285Sikob * Attach it as child.
439103285Sikob */
440103285Sikobstatic device_t
441103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
442103285Sikob{
443103285Sikob        device_t child;
444103285Sikob	struct firewire_softc *sc;
445103285Sikob
446103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
447103285Sikob	child = device_add_child(dev, name, unit);
448103285Sikob	if (child) {
449103285Sikob		device_set_ivars(child, sc->fc);
450103285Sikob		device_probe_and_attach(child);
451103285Sikob	}
452103285Sikob
453103285Sikob	return child;
454103285Sikob}
455106790Ssimokawa
456116978Ssimokawastatic int
457116978Ssimokawafirewire_resume(device_t dev)
458116978Ssimokawa{
459116978Ssimokawa	struct firewire_softc *sc;
460116978Ssimokawa
461116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
462116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
463116978Ssimokawa
464116978Ssimokawa	bus_generic_resume(dev);
465116978Ssimokawa
466116978Ssimokawa	return(0);
467116978Ssimokawa}
468116978Ssimokawa
469103285Sikob/*
470103285Sikob * Dettach it.
471103285Sikob */
472103285Sikobstatic int
473118455Ssimokawafirewire_detach(device_t dev)
474103285Sikob{
475103285Sikob	struct firewire_softc *sc;
476111074Ssimokawa	struct csrdir *csrd, *next;
477111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
478118455Ssimokawa	int err;
479103285Sikob
480103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
481118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
482118455Ssimokawa		return err;
483106790Ssimokawa
484118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
485118455Ssimokawa		return err;
486111078Ssimokawa
487111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
488111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
489111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
490111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
491111078Ssimokawa
492103285Sikob	/* XXX xfree_free and untimeout on all xfers */
493111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
494111078Ssimokawa							fwdev = fwdev_next) {
495111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
496111078Ssimokawa		free(fwdev, M_FW);
497111078Ssimokawa	}
498111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
499111074Ssimokawa		next = SLIST_NEXT(csrd, link);
500111074Ssimokawa		free(csrd, M_FW);
501111074Ssimokawa	}
502110195Ssimokawa	free(sc->fc->topology_map, M_FW);
503110195Ssimokawa	free(sc->fc->speed_map, M_FW);
504116376Ssimokawa	free(sc->fc->crom_src_buf, M_FW);
505103285Sikob	return(0);
506103285Sikob}
507103285Sikob#if 0
508103285Sikobstatic int
509103285Sikobfirewire_shutdown( device_t dev )
510103285Sikob{
511103285Sikob	return 0;
512103285Sikob}
513103285Sikob#endif
514106790Ssimokawa
515110577Ssimokawa
516110577Ssimokawastatic void
517110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
518110577Ssimokawa{
519110577Ssimokawa	struct fw_xfer *xfer;
520110577Ssimokawa
521110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
522110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
523111942Ssimokawa		xferq->queued --;
524110577Ssimokawa		xfer->resp = EAGAIN;
525113584Ssimokawa		fw_xfer_done(xfer);
526110577Ssimokawa	}
527110577Ssimokawa}
528110577Ssimokawa
529110798Ssimokawavoid
530110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
531110798Ssimokawa{
532110798Ssimokawa	int i;
533110798Ssimokawa
534110798Ssimokawa	fw_xferq_drain(fc->atq);
535110798Ssimokawa	fw_xferq_drain(fc->ats);
536110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
537110798Ssimokawa		fw_xferq_drain(fc->it[i]);
538110798Ssimokawa}
539110798Ssimokawa
540116376Ssimokawastatic void
541116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
542103285Sikob{
543103285Sikob	int i;
544103285Sikob
545103285Sikob	CSRARC(fc, STATE_CLEAR)
546103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
547103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
548103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
549103285Sikob
550103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
551103285Sikob	fc->irm = -1;
552103285Sikob
553103285Sikob	fc->max_node = -1;
554103285Sikob
555103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
556103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
557103285Sikob	}
558103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
559103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
560103285Sikob	CSRARC(fc, RESET_START) = 0;
561103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
562103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
563103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
564103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
565103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
566103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
567103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
568103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
569103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
570103285Sikob
571103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
572103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
573103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
574103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
575103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
576103285Sikob
577103285Sikob/* DV depend CSRs see blue book */
578103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
579103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
580103285Sikob
581103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
582103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
583116376Ssimokawa}
584113584Ssimokawa
585116376Ssimokawastatic void
586116376Ssimokawafw_init_crom(struct firewire_comm *fc)
587116376Ssimokawa{
588116376Ssimokawa	struct crom_src *src;
589116376Ssimokawa
590116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
591116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
592116376Ssimokawa	if (fc->crom_src_buf == NULL)
593116376Ssimokawa		return;
594116376Ssimokawa
595116376Ssimokawa	src = &fc->crom_src_buf->src;
596116376Ssimokawa	bzero(src, sizeof(struct crom_src));
597116376Ssimokawa
598116376Ssimokawa	/* BUS info sample */
599116376Ssimokawa	src->hdr.info_len = 4;
600116376Ssimokawa
601116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
602116376Ssimokawa
603116376Ssimokawa	src->businfo.irmc = 1;
604116376Ssimokawa	src->businfo.cmc = 1;
605116376Ssimokawa	src->businfo.isc = 1;
606116376Ssimokawa	src->businfo.bmc = 1;
607116376Ssimokawa	src->businfo.pmc = 0;
608116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
609116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
610116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
611117350Ssimokawa	src->businfo.generation = 1;
612116376Ssimokawa	src->businfo.link_spd = fc->speed;
613116376Ssimokawa
614116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
615116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
616116376Ssimokawa
617116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
618116376Ssimokawa
619116376Ssimokawa	fc->crom_src = src;
620116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
621116376Ssimokawa}
622116376Ssimokawa
623116376Ssimokawastatic void
624116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
625116376Ssimokawa{
626116376Ssimokawa	struct crom_src_buf *buf;
627116376Ssimokawa	struct crom_src *src;
628116376Ssimokawa	struct crom_chunk *root;
629116376Ssimokawa
630116376Ssimokawa	if (fc->crom_src_buf == NULL)
631116376Ssimokawa		fw_init_crom(fc);
632116376Ssimokawa
633116376Ssimokawa	buf =  fc->crom_src_buf;
634116376Ssimokawa	src = fc->crom_src;
635116376Ssimokawa	root = fc->crom_root;
636116376Ssimokawa
637116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
638116376Ssimokawa
639116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
640116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
641116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
642116376Ssimokawa	/* private company_id */
643116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
644127468Ssimokawa#ifdef __DragonFly__
645127468Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
646127468Ssimokawa	crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
647127468Ssimokawa#else
648116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
649116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
650127468Ssimokawa#endif
651116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
652116376Ssimokawa}
653116376Ssimokawa
654116376Ssimokawa/*
655116376Ssimokawa * Called after bus reset.
656116376Ssimokawa */
657116376Ssimokawavoid
658116376Ssimokawafw_busreset(struct firewire_comm *fc)
659116376Ssimokawa{
660116376Ssimokawa	struct firewire_dev_comm *fdc;
661117350Ssimokawa	struct crom_src *src;
662116376Ssimokawa	device_t *devlistp;
663117350Ssimokawa	void *newrom;
664116376Ssimokawa	int i, devcnt;
665116376Ssimokawa
666116376Ssimokawa	switch(fc->status){
667116376Ssimokawa	case FWBUSMGRELECT:
668116376Ssimokawa		callout_stop(&fc->bmr_callout);
669116376Ssimokawa		break;
670116376Ssimokawa	default:
671116376Ssimokawa		break;
672116376Ssimokawa	}
673116376Ssimokawa	fc->status = FWBUSRESET;
674116376Ssimokawa	fw_reset_csr(fc);
675116376Ssimokawa	fw_reset_crom(fc);
676116376Ssimokawa
677113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
678113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
679113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
680113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
681113584Ssimokawa				if (fdc->post_busreset != NULL)
682113584Ssimokawa					fdc->post_busreset(fdc);
683113584Ssimokawa			}
684113584Ssimokawa		free(devlistp, M_TEMP);
685113584Ssimokawa	}
686116376Ssimokawa
687117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
688117350Ssimokawa	src = &fc->crom_src_buf->src;
689129585Sdfr	crom_load(src, (uint32_t *)newrom, CROMSIZE);
690117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
691117350Ssimokawa		/* bump generation and reload */
692117350Ssimokawa		src->businfo.generation ++;
693117350Ssimokawa		/* generation must be between 0x2 and 0xF */
694117350Ssimokawa		if (src->businfo.generation < 2)
695117350Ssimokawa			src->businfo.generation ++;
696129585Sdfr		crom_load(src, (uint32_t *)newrom, CROMSIZE);
697117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
698117350Ssimokawa	}
699117350Ssimokawa	free(newrom, M_FW);
700103285Sikob}
701106790Ssimokawa
702103285Sikob/* Call once after reboot */
703106790Ssimokawavoid fw_init(struct firewire_comm *fc)
704103285Sikob{
705103285Sikob	int i;
706106543Ssimokawa	struct csrdir *csrd;
707106543Ssimokawa#ifdef FW_VMACCESS
708103285Sikob	struct fw_xfer *xfer;
709103285Sikob	struct fw_bind *fwb;
710106543Ssimokawa#endif
711103285Sikob
712103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
713103285Sikob
714103285Sikob	fc->arq->queued = 0;
715103285Sikob	fc->ars->queued = 0;
716103285Sikob	fc->atq->queued = 0;
717103285Sikob	fc->ats->queued = 0;
718103285Sikob
719103285Sikob	fc->arq->buf = NULL;
720103285Sikob	fc->ars->buf = NULL;
721103285Sikob	fc->atq->buf = NULL;
722103285Sikob	fc->ats->buf = NULL;
723103285Sikob
724113584Ssimokawa	fc->arq->flag = 0;
725113584Ssimokawa	fc->ars->flag = 0;
726113584Ssimokawa	fc->atq->flag = 0;
727113584Ssimokawa	fc->ats->flag = 0;
728103285Sikob
729103285Sikob	STAILQ_INIT(&fc->atq->q);
730103285Sikob	STAILQ_INIT(&fc->ats->q);
731103285Sikob
732103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
733103285Sikob		fc->it[i]->queued = 0;
734103285Sikob		fc->ir[i]->queued = 0;
735103285Sikob
736103285Sikob		fc->it[i]->start = NULL;
737103285Sikob		fc->ir[i]->start = NULL;
738103285Sikob
739103285Sikob		fc->it[i]->buf = NULL;
740103285Sikob		fc->ir[i]->buf = NULL;
741103285Sikob
742103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
743103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
744103285Sikob
745103285Sikob		STAILQ_INIT(&fc->it[i]->q);
746103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
747103285Sikob
748103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
749103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
750103285Sikob	}
751103285Sikob
752103285Sikob	fc->arq->maxq = FWMAXQUEUE;
753103285Sikob	fc->ars->maxq = FWMAXQUEUE;
754103285Sikob	fc->atq->maxq = FWMAXQUEUE;
755103285Sikob	fc->ats->maxq = FWMAXQUEUE;
756103285Sikob
757103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
758103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
759103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
760103285Sikob	}
761103285Sikob/* Initialize csr registers */
762103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
763103285Sikob				sizeof(struct fw_topology_map),
764110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
765103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
766103285Sikob				sizeof(struct fw_speed_map),
767110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
768103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
769103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
770103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
771103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
772103285Sikob
773110193Ssimokawa	STAILQ_INIT(&fc->devices);
774103285Sikob
775103285Sikob/* Initialize csr ROM work space */
776103285Sikob	SLIST_INIT(&fc->ongocsr);
777103285Sikob	SLIST_INIT(&fc->csrfree);
778103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
779110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
780103285Sikob		if(csrd == NULL) break;
781103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
782103285Sikob	}
783103285Sikob
784103285Sikob/* Initialize Async handlers */
785103285Sikob	STAILQ_INIT(&fc->binds);
786103285Sikob	for( i = 0 ; i < 0x40 ; i++){
787103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
788103285Sikob	}
789103285Sikob
790103285Sikob/* DV depend CSRs see blue book */
791103285Sikob#if 0
792103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
793103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
794103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
795103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
796103285Sikob	}
797103285Sikob
798103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
799103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
800103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
801103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
802103285Sikob	}
803103285Sikob#endif
804103285Sikob
805116376Ssimokawa	fc->crom_src_buf = NULL;
806103285Sikob
807106543Ssimokawa#ifdef FW_VMACCESS
808103285Sikob	xfer = fw_xfer_alloc();
809103285Sikob	if(xfer == NULL) return;
810103285Sikob
811110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
812103285Sikob	if(fwb == NULL){
813103285Sikob		fw_xfer_free(xfer);
814103285Sikob	}
815103285Sikob	xfer->act.hand = fw_vmaccess;
816103285Sikob	xfer->fc = fc;
817103285Sikob	xfer->sc = NULL;
818103285Sikob
819103285Sikob	fwb->start_hi = 0x2;
820103285Sikob	fwb->start_lo = 0;
821103285Sikob	fwb->addrlen = 0xffffffff;
822103285Sikob	fwb->xfer = xfer;
823103285Sikob	fw_bindadd(fc, fwb);
824106543Ssimokawa#endif
825103285Sikob}
826106790Ssimokawa
827120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
828120660Ssimokawa    ((fwb)->end < (addr))?1:0)
829120660Ssimokawa
830103285Sikob/*
831129541Sdfr * To lookup bound process from IEEE1394 address.
832103285Sikob */
833106813Ssimokawastruct fw_bind *
834129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
835103285Sikob{
836120660Ssimokawa	u_int64_t addr;
837103285Sikob	struct fw_bind *tfw;
838120660Ssimokawa
839120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
840120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
841120660Ssimokawa		if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0)
842103285Sikob			return(tfw);
843103285Sikob	return(NULL);
844103285Sikob}
845106790Ssimokawa
846103285Sikob/*
847103285Sikob * To bind IEEE1394 address block to process.
848103285Sikob */
849106790Ssimokawaint
850106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
851103285Sikob{
852120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
853120660Ssimokawa
854120660Ssimokawa	if (fwb->start > fwb->end) {
855127468Ssimokawa		printf("%s: invalid range\n", __func__);
856120660Ssimokawa		return EINVAL;
857120660Ssimokawa	}
858120660Ssimokawa
859120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
860120660Ssimokawa		if (fwb->end < tfw->start)
861120660Ssimokawa			break;
862120660Ssimokawa		prev = tfw;
863120660Ssimokawa	}
864120660Ssimokawa	if (prev == NULL) {
865103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
866103285Sikob		goto out;
867103285Sikob	}
868120660Ssimokawa	if (prev->end < fwb->start) {
869120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
870103285Sikob		goto out;
871103285Sikob	}
872120660Ssimokawa
873127468Ssimokawa	printf("%s: bind failed\n", __func__);
874120660Ssimokawa	return (EBUSY);
875120660Ssimokawa
876103285Sikobout:
877120660Ssimokawa	if (fwb->act_type == FWACT_CH)
878113584Ssimokawa		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
879120660Ssimokawa	return (0);
880103285Sikob}
881103285Sikob
882103285Sikob/*
883103285Sikob * To free IEEE1394 address block.
884103285Sikob */
885106790Ssimokawaint
886106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
887103285Sikob{
888120660Ssimokawa#if 0
889120660Ssimokawa	struct fw_xfer *xfer, *next;
890120660Ssimokawa#endif
891120660Ssimokawa	struct fw_bind *tfw;
892103285Sikob	int s;
893103285Sikob
894103285Sikob	s = splfw();
895120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
896120660Ssimokawa		if (tfw == fwb) {
897120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
898120660Ssimokawa			goto found;
899120660Ssimokawa		}
900120660Ssimokawa
901129541Sdfr	printf("%s: no such binding\n", __func__);
902120660Ssimokawa	splx(s);
903120660Ssimokawa	return (1);
904120660Ssimokawafound:
905120660Ssimokawa#if 0
906113584Ssimokawa	/* shall we do this? */
907113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
908113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
909113584Ssimokawa		fw_xfer_free(xfer);
910113584Ssimokawa	}
911113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
912120660Ssimokawa#endif
913113584Ssimokawa
914103285Sikob	splx(s);
915103285Sikob	return 0;
916103285Sikob}
917103285Sikob
918103285Sikob/*
919103285Sikob * To free transaction label.
920103285Sikob */
921106790Ssimokawastatic void
922106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
923103285Sikob{
924103285Sikob	struct tlabel *tl;
925103285Sikob	int s = splfw();
926103285Sikob
927103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
928103285Sikob		tl = STAILQ_NEXT(tl, link)){
929103285Sikob		if(tl->xfer == xfer){
930103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
931110195Ssimokawa			free(tl, M_FW);
932103285Sikob			splx(s);
933103285Sikob			return;
934103285Sikob		}
935103285Sikob	}
936103285Sikob	splx(s);
937103285Sikob	return;
938103285Sikob}
939106790Ssimokawa
940103285Sikob/*
941103285Sikob * To obtain XFER structure by transaction label.
942103285Sikob */
943106790Ssimokawastatic struct fw_xfer *
944106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
945103285Sikob{
946103285Sikob	struct fw_xfer *xfer;
947103285Sikob	struct tlabel *tl;
948103285Sikob	int s = splfw();
949103285Sikob
950103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
951103285Sikob		tl = STAILQ_NEXT(tl, link)){
952120660Ssimokawa		if(tl->xfer->send.hdr.mode.hdr.dst == node){
953103285Sikob			xfer = tl->xfer;
954103285Sikob			splx(s);
955110577Ssimokawa			if (firewire_debug > 2)
956110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
957103285Sikob			return(xfer);
958103285Sikob		}
959103285Sikob	}
960110577Ssimokawa	if (firewire_debug > 1)
961110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
962103285Sikob	splx(s);
963103285Sikob	return(NULL);
964103285Sikob}
965106790Ssimokawa
966103285Sikob/*
967103285Sikob * To allocate IEEE1394 XFER structure.
968103285Sikob */
969106790Ssimokawastruct fw_xfer *
970110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
971103285Sikob{
972103285Sikob	struct fw_xfer *xfer;
973106790Ssimokawa
974110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
975106790Ssimokawa	if (xfer == NULL)
976106790Ssimokawa		return xfer;
977106790Ssimokawa
978110269Ssimokawa	xfer->malloc = type;
979106790Ssimokawa
980103285Sikob	return xfer;
981103285Sikob}
982106790Ssimokawa
983113584Ssimokawastruct fw_xfer *
984113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
985113584Ssimokawa{
986113584Ssimokawa	struct fw_xfer *xfer;
987113584Ssimokawa
988113584Ssimokawa	xfer = fw_xfer_alloc(type);
989126102Scperciva	if (xfer == NULL)
990126102Scperciva		return(NULL);
991120660Ssimokawa	xfer->send.pay_len = send_len;
992120660Ssimokawa	xfer->recv.pay_len = recv_len;
993120660Ssimokawa	if (send_len > 0) {
994120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
995120660Ssimokawa		if (xfer->send.payload == NULL) {
996113584Ssimokawa			fw_xfer_free(xfer);
997113584Ssimokawa			return(NULL);
998113584Ssimokawa		}
999113584Ssimokawa	}
1000120660Ssimokawa	if (recv_len > 0) {
1001120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1002120660Ssimokawa		if (xfer->recv.payload == NULL) {
1003120660Ssimokawa			if (xfer->send.payload != NULL)
1004120660Ssimokawa				free(xfer->send.payload, type);
1005113584Ssimokawa			fw_xfer_free(xfer);
1006113584Ssimokawa			return(NULL);
1007113584Ssimokawa		}
1008113584Ssimokawa	}
1009113584Ssimokawa	return(xfer);
1010113584Ssimokawa}
1011113584Ssimokawa
1012103285Sikob/*
1013103285Sikob * IEEE1394 XFER post process.
1014103285Sikob */
1015103285Sikobvoid
1016103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1017103285Sikob{
1018117716Ssimokawa	if (xfer->act.hand == NULL) {
1019117716Ssimokawa		printf("act.hand == NULL\n");
1020103285Sikob		return;
1021117716Ssimokawa	}
1022103285Sikob
1023121505Ssimokawa	if (xfer->fc == NULL)
1024121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
1025121505Ssimokawa
1026124145Ssimokawa	xfer->act.hand(xfer);
1027103285Sikob}
1028103285Sikob
1029106790Ssimokawavoid
1030113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
1031103285Sikob{
1032103285Sikob	int s;
1033113584Ssimokawa
1034103285Sikob	if(xfer == NULL ) return;
1035103285Sikob	if(xfer->state == FWXF_INQ){
1036103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1037103285Sikob		s = splfw();
1038103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1039103285Sikob		xfer->q->queued --;
1040103285Sikob		splx(s);
1041103285Sikob	}
1042113584Ssimokawa	if (xfer->fc != NULL) {
1043114729Ssimokawa#if 1
1044113584Ssimokawa		if(xfer->state == FWXF_START)
1045114729Ssimokawa			/*
1046114729Ssimokawa			 * This could happen if:
1047114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1048114729Ssimokawa			 *  2. firewire_watch() is called.
1049114729Ssimokawa			 */
1050114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1051103285Sikob#endif
1052113584Ssimokawa		fw_tl_free(xfer->fc, xfer);
1053103285Sikob	}
1054113584Ssimokawa	xfer->state = FWXF_INIT;
1055113584Ssimokawa	xfer->resp = 0;
1056113584Ssimokawa	xfer->retry = 0;
1057113584Ssimokawa}
1058113584Ssimokawa/*
1059113584Ssimokawa * To free IEEE1394 XFER structure.
1060113584Ssimokawa */
1061113584Ssimokawavoid
1062120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer)
1063113584Ssimokawa{
1064120660Ssimokawa	if (xfer == NULL) {
1065127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1066120660Ssimokawa		return;
1067120660Ssimokawa	}
1068113584Ssimokawa	fw_xfer_unload(xfer);
1069120660Ssimokawa	if(xfer->send.payload != NULL){
1070120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1071103285Sikob	}
1072120660Ssimokawa	if(xfer->recv.payload != NULL){
1073120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1074103285Sikob	}
1075110269Ssimokawa	free(xfer, xfer->malloc);
1076103285Sikob}
1077103285Sikob
1078120660Ssimokawavoid
1079120660Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1080120660Ssimokawa{
1081120660Ssimokawa	if (xfer == NULL) {
1082127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1083120660Ssimokawa		return;
1084120660Ssimokawa	}
1085120660Ssimokawa	fw_xfer_unload(xfer);
1086120660Ssimokawa	free(xfer, xfer->malloc);
1087120660Ssimokawa}
1088120660Ssimokawa
1089120660Ssimokawavoid
1090110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1091103285Sikob{
1092103285Sikob#if 0
1093110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1094103285Sikob				xfer->state, xfer->resp);
1095103285Sikob#endif
1096103285Sikob	fw_xfer_free(xfer);
1097103285Sikob}
1098103285Sikob
1099103285Sikob/*
1100103285Sikob * To configure PHY.
1101103285Sikob */
1102103285Sikobstatic void
1103103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1104103285Sikob{
1105103285Sikob	struct fw_xfer *xfer;
1106103285Sikob	struct fw_pkt *fp;
1107103285Sikob
1108103285Sikob	fc->status = FWBUSPHYCONF;
1109103285Sikob
1110120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1111113584Ssimokawa	if (xfer == NULL)
1112113584Ssimokawa		return;
1113103285Sikob	xfer->fc = fc;
1114103285Sikob	xfer->retry_req = fw_asybusy;
1115110072Ssimokawa	xfer->act.hand = fw_asy_callback_free;
1116103285Sikob
1117120660Ssimokawa	fp = &xfer->send.hdr;
1118103285Sikob	fp->mode.ld[1] = 0;
1119103285Sikob	if (root_node >= 0)
1120113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1121103285Sikob	if (gap_count >= 0)
1122113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1123103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1124103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1125103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1126103285Sikob
1127107653Ssimokawa	if (firewire_debug)
1128107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1129103285Sikob						root_node, gap_count);
1130103285Sikob	fw_asyreq(fc, -1, xfer);
1131103285Sikob}
1132103285Sikob
1133103285Sikob#if 0
1134103285Sikob/*
1135103285Sikob * Dump self ID.
1136103285Sikob */
1137103285Sikobstatic void
1138129585Sdfrfw_print_sid(uint32_t sid)
1139103285Sikob{
1140103285Sikob	union fw_self_id *s;
1141103285Sikob	s = (union fw_self_id *) &sid;
1142103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1143103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1144103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1145103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1146103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1147103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1148103285Sikob}
1149103285Sikob#endif
1150103285Sikob
1151103285Sikob/*
1152103285Sikob * To receive self ID.
1153103285Sikob */
1154129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1155103285Sikob{
1156129585Sdfr	uint32_t *p;
1157103285Sikob	union fw_self_id *self_id;
1158103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1159103285Sikob
1160129585Sdfr	fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1161103285Sikob	fc->status = FWBUSINIT;
1162103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1163129585Sdfr	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1164103285Sikob	fc->status = FWBUSCYMELECT;
1165103285Sikob	fc->topology_map->crc_len = 2;
1166103285Sikob	fc->topology_map->generation ++;
1167103285Sikob	fc->topology_map->self_id_count = 0;
1168103285Sikob	fc->topology_map->node_count = 0;
1169103285Sikob	fc->speed_map->generation ++;
1170103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1171103285Sikob	self_id = &fc->topology_map->self_id[0];
1172103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1173103285Sikob		if (sid[1] != ~sid[0]) {
1174103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1175103285Sikob			sid += 2;
1176103285Sikob			continue;
1177103285Sikob		}
1178103285Sikob		*self_id = *((union fw_self_id *)sid);
1179103285Sikob		fc->topology_map->crc_len++;
1180103285Sikob		if(self_id->p0.sequel == 0){
1181103285Sikob			fc->topology_map->node_count ++;
1182103285Sikob			c_port = 0;
1183103285Sikob#if 0
1184103285Sikob			fw_print_sid(sid[0]);
1185103285Sikob#endif
1186103285Sikob			node = self_id->p0.phy_id;
1187103285Sikob			if(fc->max_node < node){
1188103285Sikob				fc->max_node = self_id->p0.phy_id;
1189103285Sikob			}
1190103285Sikob			/* XXX I'm not sure this is the right speed_map */
1191103285Sikob			fc->speed_map->speed[node][node]
1192103285Sikob					= self_id->p0.phy_speed;
1193103285Sikob			for (j = 0; j < node; j ++) {
1194103285Sikob				fc->speed_map->speed[j][node]
1195103285Sikob					= fc->speed_map->speed[node][j]
1196103285Sikob					= min(fc->speed_map->speed[j][j],
1197103285Sikob							self_id->p0.phy_speed);
1198103285Sikob			}
1199103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1200103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1201103285Sikob				fc->irm = self_id->p0.phy_id;
1202103285Sikob			}
1203103285Sikob			if(self_id->p0.port0 >= 0x2){
1204103285Sikob				c_port++;
1205103285Sikob			}
1206103285Sikob			if(self_id->p0.port1 >= 0x2){
1207103285Sikob				c_port++;
1208103285Sikob			}
1209103285Sikob			if(self_id->p0.port2 >= 0x2){
1210103285Sikob				c_port++;
1211103285Sikob			}
1212103285Sikob		}
1213103285Sikob		if(c_port > 2){
1214103285Sikob			i_branch += (c_port - 2);
1215103285Sikob		}
1216103285Sikob		sid += 2;
1217103285Sikob		self_id++;
1218103285Sikob		fc->topology_map->self_id_count ++;
1219103285Sikob	}
1220108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1221103285Sikob	/* CRC */
1222103285Sikob	fc->topology_map->crc = fw_crc16(
1223129585Sdfr			(uint32_t *)&fc->topology_map->generation,
1224103285Sikob			fc->topology_map->crc_len * 4);
1225103285Sikob	fc->speed_map->crc = fw_crc16(
1226129585Sdfr			(uint32_t *)&fc->speed_map->generation,
1227103285Sikob			fc->speed_map->crc_len * 4);
1228103285Sikob	/* byteswap and copy to CSR */
1229129585Sdfr	p = (uint32_t *)fc->topology_map;
1230103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1231103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1232129585Sdfr	p = (uint32_t *)fc->speed_map;
1233103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1234103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1235129585Sdfr	/* don't byte-swap uint8_t array */
1236103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1237103285Sikob
1238103285Sikob	fc->max_hop = fc->max_node - i_branch;
1239103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1240103285Sikob
1241103285Sikob	if(fc->irm == -1 ){
1242103285Sikob		printf(", Not found IRM capable node");
1243103285Sikob	}else{
1244103285Sikob		printf(", cable IRM = %d", fc->irm);
1245103285Sikob		if (fc->irm == fc->nodeid)
1246110016Ssimokawa			printf(" (me)");
1247103285Sikob	}
1248110016Ssimokawa	printf("\n");
1249103285Sikob
1250109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1251114909Ssimokawa		if (fc->irm == fc->nodeid) {
1252103285Sikob			fc->status = FWBUSMGRDONE;
1253103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1254114909Ssimokawa			fw_bmr(fc);
1255109736Ssimokawa		} else {
1256103285Sikob			fc->status = FWBUSMGRELECT;
1257110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1258110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1259103285Sikob		}
1260114909Ssimokawa	} else
1261103285Sikob		fc->status = FWBUSMGRDONE;
1262114909Ssimokawa
1263108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1264108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1265103285Sikob}
1266106790Ssimokawa
1267103285Sikob/*
1268103285Sikob * To probe devices on the IEEE1394 bus.
1269103285Sikob */
1270106790Ssimokawastatic void
1271106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1272103285Sikob{
1273103285Sikob	int s;
1274120850Ssimokawa	struct fw_device *fwdev;
1275103285Sikob
1276103285Sikob	s = splfw();
1277103285Sikob	fc->status = FWBUSEXPLORE;
1278103285Sikob	fc->retry_count = 0;
1279103285Sikob
1280120850Ssimokawa	/* Invalidate all devices, just after bus reset. */
1281120850Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1282110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1283103285Sikob			fwdev->status = FWDEVINVAL;
1284103285Sikob			fwdev->rcnt = 0;
1285103285Sikob		}
1286120850Ssimokawa
1287103285Sikob	fc->ongonode = 0;
1288103285Sikob	fc->ongoaddr = CSRROMOFF;
1289103285Sikob	fc->ongodev = NULL;
1290103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1291103285Sikob	fw_bus_explore(fc);
1292103285Sikob	splx(s);
1293103285Sikob}
1294106790Ssimokawa
1295103285Sikob/*
1296129274Sdfr * Find the self_id packet for a node, ignoring sequels.
1297129274Sdfr */
1298129274Sdfrstatic union fw_self_id *
1299129274Sdfrfw_find_self_id(struct firewire_comm *fc, int node)
1300129274Sdfr{
1301129274Sdfr	uint32_t i;
1302129274Sdfr	union fw_self_id *s;
1303129274Sdfr
1304129274Sdfr	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1305129274Sdfr		s = &fc->topology_map->self_id[i];
1306129274Sdfr		if (s->p0.sequel)
1307129274Sdfr			continue;
1308129274Sdfr		if (s->p0.phy_id == node)
1309129274Sdfr			return s;
1310129274Sdfr	}
1311129274Sdfr	return 0;
1312129274Sdfr}
1313129274Sdfr
1314129274Sdfr/*
1315103285Sikob * To collect device informations on the IEEE1394 bus.
1316103285Sikob */
1317106790Ssimokawastatic void
1318106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1319103285Sikob{
1320103285Sikob	int err = 0;
1321110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1322129585Sdfr	uint32_t addr;
1323103285Sikob	struct fw_xfer *xfer;
1324103285Sikob	struct fw_pkt *fp;
1325103285Sikob
1326103285Sikob	if(fc->status != FWBUSEXPLORE)
1327103285Sikob		return;
1328103285Sikob
1329103285Sikobloop:
1330103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1331103285Sikob
1332103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1333103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1334103285Sikob
1335103285Sikob	/* check link */
1336103285Sikob	/* XXX we need to check phy_id first */
1337129274Sdfr	if (!fw_find_self_id(fc, fc->ongonode)->p0.link_active) {
1338110577Ssimokawa		if (firewire_debug)
1339110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1340103285Sikob		fc->ongonode++;
1341103285Sikob		goto loop;
1342103285Sikob	}
1343103285Sikob
1344103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1345103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1346103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1347103285Sikob		fc->ongoaddr = CSRROMOFF;
1348103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1349103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1350103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1351103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1352103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1353103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1354103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1355103285Sikob	}else if(fc->ongodev == NULL){
1356110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1357110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1358103285Sikob				break;
1359103285Sikob		if(fwdev != NULL){
1360103285Sikob			fwdev->dst = fc->ongonode;
1361117350Ssimokawa			fwdev->status = FWDEVINIT;
1362117350Ssimokawa			fc->ongodev = fwdev;
1363103285Sikob			fc->ongoaddr = CSRROMOFF;
1364117350Ssimokawa			addr = 0xf0000000 | fc->ongoaddr;
1365117350Ssimokawa			goto dorequest;
1366103285Sikob		}
1367115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1368115787Ssimokawa							M_NOWAIT | M_ZERO);
1369103285Sikob		if(fwdev == NULL)
1370103285Sikob			return;
1371106810Ssimokawa		fwdev->fc = fc;
1372103285Sikob		fwdev->rommax = 0;
1373103285Sikob		fwdev->dst = fc->ongonode;
1374103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1375103285Sikob		fwdev->status = FWDEVINIT;
1376103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1377103285Sikob
1378110179Ssimokawa		pfwdev = NULL;
1379110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1380110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1381110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1382110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1383110179Ssimokawa				break;
1384110179Ssimokawa			pfwdev = tfwdev;
1385103285Sikob		}
1386110179Ssimokawa		if (pfwdev == NULL)
1387110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1388110179Ssimokawa		else
1389110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1390103285Sikob
1391108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1392107653Ssimokawa			linkspeed[fwdev->speed],
1393107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1394103285Sikob
1395103285Sikob		fc->ongodev = fwdev;
1396103285Sikob		fc->ongoaddr = CSRROMOFF;
1397103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1398103285Sikob	}else{
1399103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1400103285Sikob	}
1401117350Ssimokawadorequest:
1402103285Sikob#if 0
1403103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1404103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1405103285Sikob		fw_bus_explore_callback);
1406103285Sikob	if(xfer == NULL) goto done;
1407103285Sikob#else
1408120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1409103285Sikob	if(xfer == NULL){
1410103285Sikob		goto done;
1411103285Sikob	}
1412120660Ssimokawa	xfer->send.spd = 0;
1413120660Ssimokawa	fp = &xfer->send.hdr;
1414113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1415103285Sikob	fp->mode.rreqq.tlrt = 0;
1416103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1417103285Sikob	fp->mode.rreqq.pri = 0;
1418103285Sikob	fp->mode.rreqq.src = 0;
1419120660Ssimokawa	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1420113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1421103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1422103285Sikob
1423110577Ssimokawa	if (firewire_debug)
1424110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1425110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1426103285Sikob	err = fw_asyreq(fc, -1, xfer);
1427103285Sikob	if(err){
1428103285Sikob		fw_xfer_free( xfer);
1429103285Sikob		return;
1430103285Sikob	}
1431103285Sikob#endif
1432103285Sikob	return;
1433103285Sikobdone:
1434103285Sikob	/* fw_attach_devs */
1435103285Sikob	fc->status = FWBUSEXPDONE;
1436107653Ssimokawa	if (firewire_debug)
1437107653Ssimokawa		printf("bus_explore done\n");
1438103285Sikob	fw_attach_dev(fc);
1439103285Sikob	return;
1440103285Sikob
1441103285Sikob}
1442106790Ssimokawa
1443103285Sikob/* Portable Async. request read quad */
1444106790Ssimokawastruct fw_xfer *
1445129585Sdfrasyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt,
1446129585Sdfr	uint32_t addr_hi, uint32_t addr_lo,
1447124169Ssimokawa	void (*hand) (struct fw_xfer*))
1448103285Sikob{
1449103285Sikob	struct fw_xfer *xfer;
1450103285Sikob	struct fw_pkt *fp;
1451103285Sikob	int err;
1452103285Sikob
1453120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1454113584Ssimokawa	if (xfer == NULL)
1455103285Sikob		return NULL;
1456113584Ssimokawa
1457120660Ssimokawa	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1458120660Ssimokawa	fp = &xfer->send.hdr;
1459113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1460103285Sikob	if(tl & FWP_TL_VALID){
1461103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1462103285Sikob	}else{
1463103285Sikob		fp->mode.rreqq.tlrt = 0;
1464103285Sikob	}
1465103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1466103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1467103285Sikob	fp->mode.rreqq.pri = 0;
1468103285Sikob	fp->mode.rreqq.src = 0;
1469120660Ssimokawa	fp->mode.rreqq.dst = addr_hi >> 16;
1470113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1471103285Sikob	xfer->act.hand = hand;
1472103285Sikob
1473103285Sikob	err = fw_asyreq(fc, -1, xfer);
1474103285Sikob	if(err){
1475103285Sikob		fw_xfer_free( xfer);
1476103285Sikob		return NULL;
1477103285Sikob	}
1478103285Sikob	return xfer;
1479103285Sikob}
1480106790Ssimokawa
1481103285Sikob/*
1482103285Sikob * Callback for the IEEE1394 bus information collection.
1483103285Sikob */
1484106790Ssimokawastatic void
1485106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1486106790Ssimokawa{
1487103285Sikob	struct firewire_comm *fc;
1488103285Sikob	struct fw_pkt *sfp,*rfp;
1489103285Sikob	struct csrhdr *chdr;
1490103285Sikob	struct csrdir *csrd;
1491103285Sikob	struct csrreg *csrreg;
1492129585Sdfr	uint32_t offset;
1493103285Sikob
1494103285Sikob
1495110577Ssimokawa	if(xfer == NULL) {
1496110577Ssimokawa		printf("xfer == NULL\n");
1497110577Ssimokawa		return;
1498110577Ssimokawa	}
1499103285Sikob	fc = xfer->fc;
1500110577Ssimokawa
1501110577Ssimokawa	if (firewire_debug)
1502110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1503110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1504110577Ssimokawa
1505103285Sikob	if(xfer->resp != 0){
1506127468Ssimokawa		device_printf(fc->bdev,
1507127468Ssimokawa		    "bus_explore node=%d addr=0x%x resp=%d retry=%d\n",
1508127468Ssimokawa		    fc->ongonode, fc->ongoaddr, xfer->resp, xfer->retry);
1509127468Ssimokawa		if (xfer->retry < fc->max_asyretry) {
1510127468Ssimokawa			fw_asystart(xfer);
1511127468Ssimokawa			return;
1512127468Ssimokawa		}
1513114284Ssimokawa		goto errnode;
1514103285Sikob	}
1515103285Sikob
1516120660Ssimokawa	sfp = &xfer->send.hdr;
1517120660Ssimokawa	rfp = &xfer->recv.hdr;
1518103285Sikob#if 0
1519103285Sikob	{
1520129585Sdfr		uint32_t *qld;
1521103285Sikob		int i;
1522129585Sdfr		qld = (uint32_t *)xfer->recv.buf;
1523103285Sikob		printf("len:%d\n", xfer->recv.len);
1524103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1525113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1526103285Sikob			if((i % 16) == 15) printf("\n");
1527103285Sikob		}
1528103285Sikob		if((i % 16) != 15) printf("\n");
1529103285Sikob	}
1530103285Sikob#endif
1531103285Sikob	if(fc->ongodev == NULL){
1532113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1533103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1534103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1535129541Sdfr/* If CSR is minimal confinguration, more investigation is not needed. */
1536103285Sikob			if(chdr->info_len == 1){
1537110577Ssimokawa				if (firewire_debug)
1538110577Ssimokawa					printf("node%d: minimal config\n",
1539110577Ssimokawa								fc->ongonode);
1540103285Sikob				goto nextnode;
1541103285Sikob			}else{
1542103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1543103285Sikob			}
1544113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1545103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1546103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1547113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1548103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1549110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1550110577Ssimokawa				if (firewire_debug)
1551110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1552110577Ssimokawa							fc->ongonode);
1553103285Sikob				goto nextnode;
1554110577Ssimokawa			}
1555103285Sikob			fc->ongoaddr = CSRROMOFF;
1556103285Sikob		}
1557103285Sikob	}else{
1558117350Ssimokawa		if (fc->ongoaddr == CSRROMOFF &&
1559117350Ssimokawa		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1560117350Ssimokawa			fc->ongodev->status = FWDEVATTACHED;
1561117350Ssimokawa			goto nextnode;
1562117350Ssimokawa		}
1563103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1564103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1565103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1566103285Sikob		}
1567103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1568103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1569103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1570103285Sikob			offset = CSRROMOFF;
1571103285Sikob		}else{
1572103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1573103285Sikob			offset = csrd->off;
1574103285Sikob		}
1575103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1576103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1577103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1578103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1579103285Sikob				if(csrd == NULL){
1580103285Sikob					goto nextnode;
1581103285Sikob				}else{
1582103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1583103285Sikob					fc->ongoaddr += csrreg->val * 4;
1584103285Sikob					csrd->off = fc->ongoaddr;
1585103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1586103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1587103285Sikob					goto nextaddr;
1588103285Sikob				}
1589103285Sikob			}
1590103285Sikob		}
1591103285Sikob		fc->ongoaddr += 4;
1592103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1593103285Sikob				(fc->ongodev->rommax < 0x414)){
1594103285Sikob			if(fc->ongodev->rommax <= 0x414){
1595103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1596103285Sikob				if(csrd == NULL) goto nextnode;
1597103285Sikob				csrd->off = fc->ongoaddr;
1598103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1599103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1600103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1601103285Sikob			}
1602103285Sikob			goto nextaddr;
1603103285Sikob		}
1604103285Sikob
1605103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1606103285Sikob			if(csrd == NULL){
1607103285Sikob				goto nextnode;
1608103285Sikob			};
1609103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1610103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1611103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1612103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1613103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1614103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1615103285Sikob				offset = CSRROMOFF;
1616103285Sikob			}else{
1617103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1618103285Sikob				offset = csrd->off;
1619103285Sikob			}
1620103285Sikob		}
1621103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1622103285Sikob			goto nextnode;
1623103285Sikob		}
1624103285Sikob	}
1625103285Sikobnextaddr:
1626103285Sikob	fw_xfer_free( xfer);
1627103285Sikob	fw_bus_explore(fc);
1628103285Sikob	return;
1629114284Ssimokawaerrnode:
1630114284Ssimokawa	fc->retry_count++;
1631127468Ssimokawa	if (fc->ongodev != NULL) {
1632114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1633127468Ssimokawa		/* Invalidate ROM */
1634127468Ssimokawa		fc->ongodev->csrrom[0] = 0;
1635127468Ssimokawa	}
1636103285Sikobnextnode:
1637103285Sikob	fw_xfer_free( xfer);
1638103285Sikob	fc->ongonode++;
1639103285Sikob/* housekeeping work space */
1640103285Sikob	fc->ongoaddr = CSRROMOFF;
1641103285Sikob	fc->ongodev = NULL;
1642103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1643103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1644103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1645103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1646103285Sikob	}
1647103285Sikob	fw_bus_explore(fc);
1648103285Sikob	return;
1649103285Sikob}
1650103285Sikob
1651103285Sikob/*
1652103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1653103285Sikob */
1654106815Ssimokawastatic void
1655106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1656103285Sikob{
1657120850Ssimokawa	struct fw_device *fwdev, *next;
1658103285Sikob	int i, err;
1659103285Sikob	device_t *devlistp;
1660103285Sikob	int devcnt;
1661103285Sikob	struct firewire_dev_comm *fdc;
1662103285Sikob
1663120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1664120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1665120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1666114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1667120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1668120850Ssimokawa			fwdev->rcnt ++;
1669120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1670120850Ssimokawa				/*
1671120850Ssimokawa				 * Remove devices which have not been seen
1672120850Ssimokawa				 * for a while.
1673120850Ssimokawa				 */
1674120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1675120850Ssimokawa				    link);
1676120850Ssimokawa				free(fwdev, M_FW);
1677120850Ssimokawa			}
1678120850Ssimokawa		}
1679120850Ssimokawa	}
1680103285Sikob
1681108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1682103285Sikob	if( err != 0 )
1683103285Sikob		return;
1684103285Sikob	for( i = 0 ; i < devcnt ; i++){
1685103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1686103285Sikob			fdc = device_get_softc(devlistp[i]);
1687103285Sikob			if (fdc->post_explore != NULL)
1688103285Sikob				fdc->post_explore(fdc);
1689103285Sikob		}
1690103285Sikob	}
1691103285Sikob	free(devlistp, M_TEMP);
1692103285Sikob
1693103285Sikob	if (fc->retry_count > 0) {
1694127468Ssimokawa		device_printf(fc->bdev, "bus_explore failed for %d nodes\n",
1695127468Ssimokawa		    fc->retry_count);
1696111040Ssimokawa#if 0
1697110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1698110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1699111040Ssimokawa#endif
1700103285Sikob	}
1701103285Sikob	return;
1702103285Sikob}
1703106815Ssimokawa
1704103285Sikob/*
1705129541Sdfr * To allocate unique transaction label.
1706103285Sikob */
1707106815Ssimokawastatic int
1708106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1709103285Sikob{
1710103285Sikob	u_int i;
1711103285Sikob	struct tlabel *tl, *tmptl;
1712103285Sikob	int s;
1713129585Sdfr	static uint32_t label = 0;
1714103285Sikob
1715103285Sikob	s = splfw();
1716103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1717103285Sikob		label = (label + 1) & 0x3f;
1718103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1719103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1720120660Ssimokawa			if (tmptl->xfer->send.hdr.mode.hdr.dst ==
1721120660Ssimokawa			    xfer->send.hdr.mode.hdr.dst)
1722120660Ssimokawa				break;
1723103285Sikob		}
1724103285Sikob		if(tmptl == NULL) {
1725110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1726103285Sikob			if (tl == NULL) {
1727103285Sikob				splx(s);
1728103285Sikob				return (-1);
1729103285Sikob			}
1730103285Sikob			tl->xfer = xfer;
1731103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1732103285Sikob			splx(s);
1733110577Ssimokawa			if (firewire_debug > 1)
1734110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1735120660Ssimokawa				    xfer->send.hdr.mode.hdr.dst, label);
1736103285Sikob			return(label);
1737103285Sikob		}
1738103285Sikob	}
1739103285Sikob	splx(s);
1740103285Sikob
1741103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1742103285Sikob	return(-1);
1743103285Sikob}
1744106815Ssimokawa
1745113584Ssimokawastatic void
1746120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1747113584Ssimokawa{
1748120660Ssimokawa	struct fw_pkt *pkt;
1749120660Ssimokawa	u_char *p;
1750120660Ssimokawa	struct tcode_info *tinfo;
1751120660Ssimokawa	u_int res, i, len, plen;
1752113584Ssimokawa
1753120660Ssimokawa	rb->xfer->recv.spd -= rb->spd;
1754120660Ssimokawa
1755120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1756120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1757120660Ssimokawa
1758120660Ssimokawa	/* Copy header */
1759120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1760120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1761120660Ssimokawa	(u_char *)rb->vec->iov_base += tinfo->hdr_len;
1762120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1763120660Ssimokawa
1764120660Ssimokawa	/* Copy payload */
1765120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1766120660Ssimokawa	res = rb->xfer->recv.pay_len;
1767120660Ssimokawa
1768120660Ssimokawa	/* special handling for RRESQ */
1769120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1770129585Sdfr	    p != NULL && res >= sizeof(uint32_t)) {
1771129585Sdfr		*(uint32_t *)p = pkt->mode.rresq.data;
1772129585Sdfr		rb->xfer->recv.pay_len = sizeof(uint32_t);
1773120660Ssimokawa		return;
1774120660Ssimokawa	}
1775120660Ssimokawa
1776120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1777120660Ssimokawa		return;
1778120660Ssimokawa
1779120660Ssimokawa	plen = pkt->mode.rresb.len;
1780120660Ssimokawa
1781120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1782120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1783113584Ssimokawa		if (res < len) {
1784113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1785120660Ssimokawa			    rb->xfer->recv.pay_len, len - res);
1786113584Ssimokawa			len = res;
1787113584Ssimokawa		}
1788120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1789113584Ssimokawa		p += len;
1790113584Ssimokawa		res -= len;
1791120660Ssimokawa		plen -= len;
1792120660Ssimokawa		if (res == 0 || plen == 0)
1793113584Ssimokawa			break;
1794113584Ssimokawa	}
1795120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1796120660Ssimokawa
1797113584Ssimokawa}
1798113584Ssimokawa
1799103285Sikob/*
1800129541Sdfr * Generic packet receiving process.
1801103285Sikob */
1802106815Ssimokawavoid
1803120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1804103285Sikob{
1805103285Sikob	struct fw_pkt *fp, *resfp;
1806103285Sikob	struct fw_bind *bind;
1807113584Ssimokawa	int tcode, s;
1808113584Ssimokawa	int i, len, oldstate;
1809103285Sikob#if 0
1810103285Sikob	{
1811129585Sdfr		uint32_t *qld;
1812103285Sikob		int i;
1813129585Sdfr		qld = (uint32_t *)buf;
1814103285Sikob		printf("spd %d len:%d\n", spd, len);
1815103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1816103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1817103285Sikob			if((i % 16) == 15) printf("\n");
1818103285Sikob		}
1819103285Sikob		if((i % 16) != 15) printf("\n");
1820103285Sikob	}
1821103285Sikob#endif
1822120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1823113584Ssimokawa	tcode = fp->mode.common.tcode;
1824113584Ssimokawa	switch (tcode) {
1825103285Sikob	case FWTCODE_WRES:
1826103285Sikob	case FWTCODE_RRESQ:
1827103285Sikob	case FWTCODE_RRESB:
1828103285Sikob	case FWTCODE_LRES:
1829120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1830103285Sikob					fp->mode.hdr.tlrt >> 2);
1831120660Ssimokawa		if(rb->xfer == NULL) {
1832103285Sikob			printf("fw_rcv: unknown response "
1833124251Ssimokawa			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1834124251Ssimokawa			    tcode_str[tcode], tcode,
1835124251Ssimokawa			    fp->mode.hdr.src,
1836124251Ssimokawa			    fp->mode.hdr.tlrt >> 2,
1837124251Ssimokawa			    fp->mode.hdr.tlrt & 3,
1838124251Ssimokawa			    fp->mode.rresq.data);
1839103285Sikob#if 1
1840103285Sikob			printf("try ad-hoc work around!!\n");
1841120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1842103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1843120660Ssimokawa			if (rb->xfer == NULL) {
1844103285Sikob				printf("no use...\n");
1845103285Sikob				goto err;
1846103285Sikob			}
1847103285Sikob#else
1848103285Sikob			goto err;
1849103285Sikob#endif
1850103285Sikob		}
1851120660Ssimokawa		fw_rcv_copy(rb);
1852120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1853120660Ssimokawa			rb->xfer->resp = EIO;
1854120660Ssimokawa		else
1855120660Ssimokawa			rb->xfer->resp = 0;
1856113584Ssimokawa		/* make sure the packet is drained in AT queue */
1857120660Ssimokawa		oldstate = rb->xfer->state;
1858120660Ssimokawa		rb->xfer->state = FWXF_RCVD;
1859113584Ssimokawa		switch (oldstate) {
1860113584Ssimokawa		case FWXF_SENT:
1861120660Ssimokawa			fw_xfer_done(rb->xfer);
1862103285Sikob			break;
1863113584Ssimokawa		case FWXF_START:
1864119289Ssimokawa#if 0
1865113584Ssimokawa			if (firewire_debug)
1866120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1867119289Ssimokawa#endif
1868113584Ssimokawa			break;
1869103285Sikob		default:
1870120660Ssimokawa			printf("unexpected state %d\n", rb->xfer->state);
1871103285Sikob		}
1872113584Ssimokawa		return;
1873103285Sikob	case FWTCODE_WREQQ:
1874103285Sikob	case FWTCODE_WREQB:
1875103285Sikob	case FWTCODE_RREQQ:
1876103285Sikob	case FWTCODE_RREQB:
1877103285Sikob	case FWTCODE_LREQ:
1878120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1879113584Ssimokawa			fp->mode.rreqq.dest_lo);
1880103285Sikob		if(bind == NULL){
1881124251Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1882127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1883127468Ssimokawa			    " src=0x%x data=%lx\n",
1884127468Ssimokawa#else
1885124251Ssimokawa			    " src=0x%x data=%x\n",
1886113962Ssimokawa#endif
1887124251Ssimokawa			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1888124251Ssimokawa			    tcode_str[tcode], tcode,
1889124251Ssimokawa			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1890120660Ssimokawa			if (rb->fc->status == FWBUSRESET) {
1891110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1892103285Sikob				goto err;
1893103285Sikob			}
1894120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
1895120660Ssimokawa			if(rb->xfer == NULL){
1896103285Sikob				return;
1897103285Sikob			}
1898120660Ssimokawa			rb->xfer->send.spd = rb->spd;
1899120660Ssimokawa			rb->xfer->send.pay_len = 0;
1900120660Ssimokawa			resfp = &rb->xfer->send.hdr;
1901113584Ssimokawa			switch (tcode) {
1902103285Sikob			case FWTCODE_WREQQ:
1903103285Sikob			case FWTCODE_WREQB:
1904103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1905103285Sikob				break;
1906103285Sikob			case FWTCODE_RREQQ:
1907103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1908103285Sikob				break;
1909103285Sikob			case FWTCODE_RREQB:
1910103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1911103285Sikob				break;
1912103285Sikob			case FWTCODE_LREQ:
1913103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1914103285Sikob				break;
1915103285Sikob			}
1916103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1917103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1918103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1919120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1920103285Sikob			resfp->mode.rresb.extcode = 0;
1921103285Sikob			resfp->mode.rresb.len = 0;
1922103285Sikob/*
1923120660Ssimokawa			rb->xfer->act.hand = fw_asy_callback;
1924103285Sikob*/
1925120660Ssimokawa			rb->xfer->act.hand = fw_xfer_free;
1926120660Ssimokawa			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1927120660Ssimokawa				fw_xfer_free(rb->xfer);
1928103285Sikob				return;
1929103285Sikob			}
1930103285Sikob			goto err;
1931103285Sikob		}
1932113584Ssimokawa		len = 0;
1933120660Ssimokawa		for (i = 0; i < rb->nvec; i ++)
1934120660Ssimokawa			len += rb->vec[i].iov_len;
1935113584Ssimokawa		switch(bind->act_type){
1936103285Sikob		case FWACT_XFER:
1937113584Ssimokawa			/* splfw()?? */
1938120660Ssimokawa			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1939120660Ssimokawa			if (rb->xfer == NULL) {
1940113584Ssimokawa				printf("Discard a packet for this bind.\n");
1941113584Ssimokawa				goto err;
1942113584Ssimokawa			}
1943113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1944120660Ssimokawa			fw_rcv_copy(rb);
1945124145Ssimokawa			rb->xfer->act.hand(rb->xfer);
1946103285Sikob			return;
1947103285Sikob			break;
1948103285Sikob		case FWACT_CH:
1949120660Ssimokawa			if(rb->fc->ir[bind->sub]->queued >=
1950120660Ssimokawa				rb->fc->ir[bind->sub]->maxq){
1951120660Ssimokawa				device_printf(rb->fc->bdev,
1952108655Ssimokawa					"Discard a packet %x %d\n",
1953113584Ssimokawa					bind->sub,
1954120660Ssimokawa					rb->fc->ir[bind->sub]->queued);
1955103285Sikob				goto err;
1956103285Sikob			}
1957120660Ssimokawa			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1958120660Ssimokawa			if (rb->xfer == NULL) {
1959113584Ssimokawa				printf("Discard packet for this bind\n");
1960113584Ssimokawa				goto err;
1961113584Ssimokawa			}
1962113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1963120660Ssimokawa			fw_rcv_copy(rb);
1964103285Sikob			s = splfw();
1965120660Ssimokawa			rb->fc->ir[bind->sub]->queued++;
1966120660Ssimokawa			STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q,
1967120660Ssimokawa			    rb->xfer, link);
1968103285Sikob			splx(s);
1969103285Sikob
1970120660Ssimokawa			wakeup((caddr_t)rb->fc->ir[bind->sub]);
1971103285Sikob
1972103285Sikob			return;
1973103285Sikob			break;
1974103285Sikob		default:
1975103285Sikob			goto err;
1976103285Sikob			break;
1977103285Sikob		}
1978103285Sikob		break;
1979120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
1980103285Sikob	case FWTCODE_STREAM:
1981103285Sikob	{
1982103285Sikob		struct fw_xferq *xferq;
1983103285Sikob
1984120660Ssimokawa		xferq = rb->fc->ir[sub];
1985103285Sikob#if 0
1986103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1987103285Sikob			sub, len, off, spd);
1988103285Sikob#endif
1989103285Sikob		if(xferq->queued >= xferq->maxq) {
1990103285Sikob			printf("receive queue is full\n");
1991103285Sikob			goto err;
1992103285Sikob		}
1993113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1994113584Ssimokawa			per packet mode */
1995120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1996113584Ssimokawa						vec[0].iov_len);
1997120660Ssimokawa		if (rb->xfer == NULL) goto err;
1998120660Ssimokawa		fw_rcv_copy(rb)
1999103285Sikob		s = splfw();
2000103285Sikob		xferq->queued++;
2001120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
2002103285Sikob		splx(s);
2003120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
2004127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2005127468Ssimokawa		if (&xferq->rsel.si_pid != 0)
2006127468Ssimokawa#else
2007103285Sikob		if (SEL_WAITING(&xferq->rsel))
2008103285Sikob#endif
2009122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
2010103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
2011103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
2012103285Sikob			wakeup((caddr_t)xferq);
2013103285Sikob		}
2014103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
2015103285Sikob			xferq->hand(xferq);
2016103285Sikob		}
2017103285Sikob		return;
2018103285Sikob		break;
2019103285Sikob	}
2020120660Ssimokawa#endif
2021103285Sikob	default:
2022113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
2023103285Sikob		break;
2024103285Sikob	}
2025103285Sikoberr:
2026113584Ssimokawa	return;
2027103285Sikob}
2028106815Ssimokawa
2029103285Sikob/*
2030103285Sikob * Post process for Bus Manager election process.
2031103285Sikob */
2032103285Sikobstatic void
2033103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
2034103285Sikob{
2035103285Sikob	struct firewire_comm *fc;
2036109422Ssimokawa	int bmr;
2037103285Sikob
2038109422Ssimokawa	if (xfer == NULL)
2039109422Ssimokawa		return;
2040103285Sikob	fc = xfer->fc;
2041109422Ssimokawa	if (xfer->resp != 0)
2042103285Sikob		goto error;
2043120660Ssimokawa	if (xfer->recv.payload == NULL)
2044103285Sikob		goto error;
2045120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2046103285Sikob		goto error;
2047109422Ssimokawa
2048120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
2049109422Ssimokawa	if (bmr == 0x3f)
2050109422Ssimokawa		bmr = fc->nodeid;
2051109422Ssimokawa
2052109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2053120660Ssimokawa	fw_xfer_free_buf(xfer);
2054114909Ssimokawa	fw_bmr(fc);
2055114909Ssimokawa	return;
2056114909Ssimokawa
2057103285Sikoberror:
2058114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2059120660Ssimokawa	fw_xfer_free_buf(xfer);
2060103285Sikob}
2061106815Ssimokawa
2062113584Ssimokawa
2063103285Sikob/*
2064103285Sikob * To candidate Bus Manager election process.
2065103285Sikob */
2066110270Ssimokawastatic void
2067106815Ssimokawafw_try_bmr(void *arg)
2068103285Sikob{
2069103285Sikob	struct fw_xfer *xfer;
2070103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2071103285Sikob	struct fw_pkt *fp;
2072103285Sikob	int err = 0;
2073103285Sikob
2074120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2075103285Sikob	if(xfer == NULL){
2076103285Sikob		return;
2077103285Sikob	}
2078120660Ssimokawa	xfer->send.spd = 0;
2079103285Sikob	fc->status = FWBUSMGRELECT;
2080103285Sikob
2081120660Ssimokawa	fp = &xfer->send.hdr;
2082113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2083103285Sikob	fp->mode.lreq.tlrt = 0;
2084103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2085103285Sikob	fp->mode.lreq.pri = 0;
2086103285Sikob	fp->mode.lreq.src = 0;
2087113584Ssimokawa	fp->mode.lreq.len = 8;
2088120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2089120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2090113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2091120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2092120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2093103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2094103285Sikob
2095103285Sikob	err = fw_asyreq(fc, -1, xfer);
2096103285Sikob	if(err){
2097120660Ssimokawa		fw_xfer_free_buf(xfer);
2098103285Sikob		return;
2099103285Sikob	}
2100103285Sikob	return;
2101103285Sikob}
2102106543Ssimokawa
2103106543Ssimokawa#ifdef FW_VMACCESS
2104103285Sikob/*
2105103285Sikob * Software implementation for physical memory block access.
2106103285Sikob * XXX:Too slow, usef for debug purpose only.
2107103285Sikob */
2108106815Ssimokawastatic void
2109106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2110103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2111129585Sdfr	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2112103285Sikob
2113113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2114113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2115103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2116103285Sikob	if(xfer->resp != 0){
2117103285Sikob		fw_xfer_free( xfer);
2118103285Sikob		return;
2119103285Sikob	}
2120103285Sikob	if(xfer->recv.buf == NULL){
2121103285Sikob		fw_xfer_free( xfer);
2122103285Sikob		return;
2123103285Sikob	}
2124103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2125103285Sikob	switch(rfp->mode.hdr.tcode){
2126103285Sikob		/* XXX need fix for 64bit arch */
2127103285Sikob		case FWTCODE_WREQB:
2128110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2129103285Sikob			xfer->send.len = 12;
2130103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2131103285Sikob			bcopy(rfp->mode.wreqb.payload,
2132103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2133103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2134103285Sikob			sfp->mode.wres.rtcode = 0;
2135103285Sikob			break;
2136103285Sikob		case FWTCODE_WREQQ:
2137110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2138103285Sikob			xfer->send.len = 12;
2139103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2140129585Sdfr			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2141103285Sikob			sfp->mode.wres.rtcode = 0;
2142103285Sikob			break;
2143103285Sikob		case FWTCODE_RREQB:
2144110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2145103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2146103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2147103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2148129585Sdfr				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2149103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2150103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2151103285Sikob			sfp->mode.rresb.rtcode = 0;
2152103285Sikob			sfp->mode.rresb.extcode = 0;
2153103285Sikob			break;
2154103285Sikob		case FWTCODE_RREQQ:
2155110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2156103285Sikob			xfer->send.len = 16;
2157103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2158129585Sdfr			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2159103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2160103285Sikob			sfp->mode.rresb.rtcode = 0;
2161103285Sikob			break;
2162103285Sikob		default:
2163103285Sikob			fw_xfer_free( xfer);
2164103285Sikob			return;
2165103285Sikob	}
2166103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2167103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2168103285Sikob	xfer->act.hand = fw_xfer_free;
2169103285Sikob	xfer->retry_req = fw_asybusy;
2170103285Sikob
2171103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2172103285Sikob	sfp->mode.hdr.pri = 0;
2173103285Sikob
2174103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2175103285Sikob/**/
2176103285Sikob	return;
2177103285Sikob}
2178106543Ssimokawa#endif
2179106543Ssimokawa
2180103285Sikob/*
2181103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2182103285Sikob */
2183129585Sdfruint16_t
2184129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){
2185129585Sdfr	uint32_t i, sum, crc = 0;
2186103285Sikob	int shift;
2187103285Sikob	len = (len + 3) & ~3;
2188103285Sikob	for(i = 0 ; i < len ; i+= 4){
2189103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2190103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2191103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2192103285Sikob		}
2193103285Sikob		crc &= 0xffff;
2194103285Sikob	}
2195129585Sdfr	return((uint16_t) crc);
2196103285Sikob}
2197106815Ssimokawa
2198110270Ssimokawastatic int
2199110072Ssimokawafw_bmr(struct firewire_comm *fc)
2200110072Ssimokawa{
2201110072Ssimokawa	struct fw_device fwdev;
2202113584Ssimokawa	union fw_self_id *self_id;
2203110072Ssimokawa	int cmstr;
2204129585Sdfr	uint32_t quad;
2205110072Ssimokawa
2206113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2207129274Sdfr	self_id = fw_find_self_id(fc, fc->max_node);
2208113584Ssimokawa	if (fc->max_node > 0) {
2209114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2210114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2211113584Ssimokawa			cmstr = fc->max_node;
2212114909Ssimokawa		else {
2213114909Ssimokawa			device_printf(fc->bdev,
2214114909Ssimokawa				"root node is not cycle master capable\n");
2215114909Ssimokawa			/* XXX shall we be the cycle master? */
2216113584Ssimokawa			cmstr = fc->nodeid;
2217114909Ssimokawa			/* XXX need bus reset */
2218114909Ssimokawa		}
2219113584Ssimokawa	} else
2220113584Ssimokawa		cmstr = -1;
2221114909Ssimokawa
2222114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2223114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2224114909Ssimokawa		/* We are not the bus manager */
2225114909Ssimokawa		printf("\n");
2226114909Ssimokawa		return(0);
2227114909Ssimokawa	}
2228114909Ssimokawa	printf("(me)\n");
2229114909Ssimokawa
2230114909Ssimokawa	/* Optimize gapcount */
2231113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2232113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2233110072Ssimokawa	/* If we are the cycle master, nothing to do */
2234113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2235110072Ssimokawa		return 0;
2236110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2237110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2238110072Ssimokawa	fwdev.fc = fc;
2239110072Ssimokawa	fwdev.dst = cmstr;
2240110072Ssimokawa	fwdev.speed = 0;
2241110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2242110072Ssimokawa	fwdev.status = FWDEVINIT;
2243110072Ssimokawa	/* Set cmstr bit on the cycle master */
2244120660Ssimokawa	quad = htonl(1 << 8);
2245110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2246120660Ssimokawa		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2247110072Ssimokawa
2248110072Ssimokawa	return 0;
2249110072Ssimokawa}
2250110072Ssimokawa
2251118455Ssimokawastatic int
2252118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2253118455Ssimokawa{
2254118455Ssimokawa	int err = 0;
2255127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2256118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2257118455Ssimokawa#endif
2258118455Ssimokawa
2259118455Ssimokawa	switch (type) {
2260118455Ssimokawa	case MOD_LOAD:
2261127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2262118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2263118455Ssimokawa						fwdev_clone, 0, 1000);
2264118455Ssimokawa#endif
2265118455Ssimokawa		break;
2266118455Ssimokawa	case MOD_UNLOAD:
2267127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2268118455Ssimokawa		if (fwdev_ehtag != NULL)
2269118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2270118455Ssimokawa#endif
2271118455Ssimokawa		break;
2272118455Ssimokawa	case MOD_SHUTDOWN:
2273118455Ssimokawa		break;
2274118455Ssimokawa	}
2275118455Ssimokawa	return (err);
2276118455Ssimokawa}
2277118455Ssimokawa
2278118455Ssimokawa
2279127468Ssimokawa#ifdef __DragonFly__
2280127468SsimokawaDECLARE_DUMMY_MODULE(firewire);
2281127468Ssimokawa#endif
2282118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2283103285SikobMODULE_VERSION(firewire, 1);
2284