Home | History | Annotate | Line # | Download | only in ieee1394
firewire.c revision 1.16
      1 /*	$NetBSD: firewire.c,v 1.16 2007/11/05 19:08:56 kiyohara Exp $	*/
      2 /*-
      3  * Copyright (c) 2003 Hidetoshi Shimokawa
      4  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the acknowledgement as bellow:
     17  *
     18  *    This product includes software developed by K. Kobayashi and H. Shimokawa
     19  *
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  * $FreeBSD: src/sys/dev/firewire/firewire.c,v 1.101 2007/10/20 23:23:14 julian Exp $
     36  *
     37  */
     38 
     39 #if defined(__FreeBSD__)
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/types.h>
     43 
     44 #include <sys/kernel.h>
     45 #include <sys/module.h>
     46 #include <sys/malloc.h>
     47 #include <sys/conf.h>
     48 #include <sys/sysctl.h>
     49 #include <sys/kthread.h>
     50 
     51 #include <sys/kdb.h>
     52 
     53 #if defined(__DragonFly__) || __FreeBSD_version < 500000
     54 #include <machine/clock.h>	/* for DELAY() */
     55 #endif
     56 
     57 #include <sys/bus.h>		/* used by smbus and newbus */
     58 #include <sys/bus.h>
     59 
     60 #ifdef __DragonFly__
     61 #include "fw_port.h"
     62 #include "firewire.h"
     63 #include "firewirereg.h"
     64 #include "fwmem.h"
     65 #include "iec13213.h"
     66 #include "iec68113.h"
     67 #else
     68 #include <dev/firewire/fw_port.h>
     69 #include <dev/firewire/firewire.h>
     70 #include <dev/firewire/firewirereg.h>
     71 #include <dev/firewire/fwmem.h>
     72 #include <dev/firewire/iec13213.h>
     73 #include <dev/firewire/iec68113.h>
     74 #endif
     75 #elif defined(__NetBSD__)
     76 #include <sys/param.h>
     77 #include <sys/device.h>
     78 #include <sys/errno.h>
     79 #include <sys/conf.h>
     80 #include <sys/kernel.h>
     81 #include <sys/kthread.h>
     82 #include <sys/malloc.h>
     83 #include <sys/queue.h>
     84 #include <sys/sysctl.h>
     85 #include <sys/systm.h>
     86 
     87 #include <sys/bus.h>
     88 
     89 #include <dev/ieee1394/fw_port.h>
     90 #include <dev/ieee1394/firewire.h>
     91 #include <dev/ieee1394/firewirereg.h>
     92 #include <dev/ieee1394/fwmem.h>
     93 #include <dev/ieee1394/iec13213.h>
     94 #include <dev/ieee1394/iec68113.h>
     95 
     96 #include "locators.h"
     97 #endif
     98 
     99 struct crom_src_buf {
    100 	struct crom_src	src;
    101 	struct crom_chunk root;
    102 	struct crom_chunk vendor;
    103 	struct crom_chunk hw;
    104 };
    105 
    106 int firewire_debug=0, try_bmr=1, hold_count=3;
    107 #if defined(__FreeBSD__)
    108 SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
    109 	"FireWire driver debug flag");
    110 SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
    111 SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
    112 	"Try to be a bus manager");
    113 SYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
    114 	"Number of count of bus resets for removing lost device information");
    115 
    116 MALLOC_DEFINE(M_FW, "firewire", "FireWire");
    117 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
    118 #elif defined(__NetBSD__)
    119 /*
    120  * Setup sysctl(3) MIB, hw.ieee1394if.*
    121  *
    122  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
    123  */
    124 SYSCTL_SETUP(sysctl_ieee1394if, "sysctl ieee1394if(4) subtree setup")
    125 {
    126 	int rc, ieee1394if_node_num;
    127 	const struct sysctlnode *node;
    128 
    129 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
    130 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    131 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
    132 		goto err;
    133 	}
    134 
    135 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    136 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee1394if",
    137 	    SYSCTL_DESCR("ieee1394if controls"),
    138 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
    139 		goto err;
    140 	}
    141 	ieee1394if_node_num = node->sysctl_num;
    142 
    143 	/* ieee1394if try bus manager flag */
    144 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    145 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
    146 	    "try_bmr", SYSCTL_DESCR("Try to be a bus manager"),
    147 	    NULL, 0, &try_bmr,
    148 	    0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
    149 		goto err;
    150 	}
    151 
    152 	/* ieee1394if hold count */
    153 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    154 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
    155 	    "hold_count", SYSCTL_DESCR("Number of count of "
    156 	    "bus resets for removing lost device information"),
    157 	    NULL, 0, &hold_count,
    158 	    0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
    159 		goto err;
    160 	}
    161 
    162 	/* ieee1394if driver debug flag */
    163 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    164 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
    165 	    "ieee1394_debug", SYSCTL_DESCR("ieee1394if driver debug flag"),
    166 	    NULL, 0, &firewire_debug,
    167 	    0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
    168 		goto err;
    169 	}
    170 
    171 	return;
    172 
    173 err:
    174 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    175 }
    176 
    177 MALLOC_DEFINE(M_FW, "ieee1394", "IEEE1394");
    178 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/IEEE1394");
    179 #endif
    180 
    181 #define FW_MAXASYRTY 4
    182 
    183 #if defined(__FreeBSD__)
    184 devclass_t firewire_devclass;
    185 
    186 static void firewire_identify	(driver_t *, device_t);
    187 static int firewire_probe	(device_t);
    188 static int firewire_attach      (device_t);
    189 static int firewire_detach      (device_t);
    190 static int firewire_resume      (device_t);
    191 #if 0
    192 static int firewire_shutdown    (device_t);
    193 #endif
    194 static device_t firewire_add_child   (device_t, int, const char *, int);
    195 #elif defined(__NetBSD__)
    196 int firewirematch (struct device *, struct cfdata *, void *);
    197 void firewireattach (struct device *, struct device *, void *);
    198 int firewiredetach (struct device *, int);
    199 int firewire_print (void *, const char *);
    200 int firewire_resume(struct firewire_comm *);
    201 #endif
    202 static void firewire_xfer_timeout(void *, int);
    203 static void fw_try_bmr (void *);
    204 static void fw_try_bmr_callback (struct fw_xfer *);
    205 static void fw_asystart (struct fw_xfer *);
    206 static int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
    207 static void fw_bus_probe (struct firewire_comm *);
    208 static void fw_attach_dev (struct firewire_comm *);
    209 static void fw_bus_probe_thread(void *);
    210 #ifdef FW_VMACCESS
    211 static void fw_vmaccess (struct fw_xfer *);
    212 #endif
    213 static int fw_bmr (struct firewire_comm *);
    214 static void fw_dump_hdr(struct fw_pkt *, const char *);
    215 
    216 #if defined(__FreeBSD__)
    217 static device_method_t firewire_methods[] = {
    218 	/* Device interface */
    219 	DEVMETHOD(device_identify,	firewire_identify),
    220 	DEVMETHOD(device_probe,		firewire_probe),
    221 	DEVMETHOD(device_attach,	firewire_attach),
    222 	DEVMETHOD(device_detach,	firewire_detach),
    223 	DEVMETHOD(device_suspend,	bus_generic_suspend),
    224 	DEVMETHOD(device_resume,	firewire_resume),
    225 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
    226 
    227 	/* Bus interface */
    228 	DEVMETHOD(bus_add_child,	firewire_add_child),
    229 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
    230 
    231 	{ 0, 0 }
    232 };
    233 #elif defined(__NetBSD__)
    234 CFATTACH_DECL(ieee1394if, sizeof (struct firewire_softc),
    235     firewirematch, firewireattach, firewiredetach, NULL);
    236 #endif
    237 const char *fw_linkspeed[] = {
    238 	"S100", "S200", "S400", "S800",
    239 	"S1600", "S3200", "undef", "undef"
    240 };
    241 
    242 static const char *tcode_str[] = {
    243 	"WREQQ", "WREQB", "WRES",   "undef",
    244 	"RREQQ", "RREQB", "RRESQ",  "RRESB",
    245 	"CYCS",  "LREQ",  "STREAM", "LRES",
    246 	"undef", "undef", "PHY",    "undef"
    247 };
    248 
    249 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
    250 #define MAX_GAPHOP 15
    251 u_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
    252 		   21, 24, 26, 29, 32, 35, 37, 40};
    253 
    254 #if defined(__FreeBSD__)
    255 static driver_t firewire_driver = {
    256 	"firewire",
    257 	firewire_methods,
    258 	sizeof(struct firewire_softc),
    259 };
    260 #endif
    261 
    262 /*
    263  * Lookup fwdev by node id.
    264  */
    265 struct fw_device *
    266 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
    267 {
    268 	struct fw_device *fwdev;
    269 	int s;
    270 
    271 	s = splfw();
    272 	FW_GLOCK(fc);
    273 	STAILQ_FOREACH(fwdev, &fc->devices, link)
    274 		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
    275 			break;
    276 	FW_GUNLOCK(fc);
    277 	splx(s);
    278 
    279 	return fwdev;
    280 }
    281 
    282 /*
    283  * Lookup fwdev by EUI64.
    284  */
    285 struct fw_device *
    286 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
    287 {
    288 	struct fw_device *fwdev;
    289 	int s;
    290 
    291 	s = splfw();
    292 	STAILQ_FOREACH(fwdev, &fc->devices, link)
    293 		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
    294 			break;
    295 	splx(s);
    296 
    297 	if(fwdev == NULL) return NULL;
    298 	if(fwdev->status == FWDEVINVAL) return NULL;
    299 	return fwdev;
    300 }
    301 
    302 /*
    303  * Async. request procedure for userland application.
    304  */
    305 int
    306 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
    307 {
    308 	int err = 0;
    309 	struct fw_xferq *xferq;
    310 	int len;
    311 	struct fw_pkt *fp;
    312 	int tcode;
    313 	const struct tcode_info *info;
    314 
    315 	if(xfer == NULL) return EINVAL;
    316 	if(xfer->hand == NULL){
    317 		printf("hand == NULL\n");
    318 		return EINVAL;
    319 	}
    320 	fp = &xfer->send.hdr;
    321 
    322 	tcode = fp->mode.common.tcode & 0xf;
    323 	info = &fc->tcode[tcode];
    324 	if (info->flag == 0) {
    325 		printf("invalid tcode=%x\n", tcode);
    326 		return EINVAL;
    327 	}
    328 
    329 	/* XXX allow bus explore packets only after bus rest */
    330 	if ((fc->status < FWBUSEXPLORE) &&
    331 	    ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) ||
    332 	    (fp->mode.rreqq.dest_lo  < 0xf0000000) ||
    333 	    (fp->mode.rreqq.dest_lo >= 0xf0001000))) {
    334 		xfer->resp = EAGAIN;
    335 		xfer->flag = FWXF_BUSY;
    336 		return (EAGAIN);
    337 	}
    338 
    339 	if (info->flag & FWTI_REQ)
    340 		xferq = fc->atq;
    341 	else
    342 		xferq = fc->ats;
    343 	len = info->hdr_len;
    344 	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
    345 		printf("send.pay_len > maxrec\n");
    346 		return EINVAL;
    347 	}
    348 	if (info->flag & FWTI_BLOCK_STR)
    349 		len = fp->mode.stream.len;
    350 	else if (info->flag & FWTI_BLOCK_ASY)
    351 		len = fp->mode.rresb.len;
    352 	else
    353 		len = 0;
    354 	if (len != xfer->send.pay_len){
    355 		printf("len(%d) != send.pay_len(%d) %s(%x)\n",
    356 		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
    357 		return EINVAL;
    358 	}
    359 
    360 	if(xferq->start == NULL){
    361 		printf("xferq->start == NULL\n");
    362 		return EINVAL;
    363 	}
    364 	if(!(xferq->queued < xferq->maxq)){
    365 		fw_printf(fc->bdev, "Discard a packet (queued=%d)\n",
    366 			xferq->queued);
    367 		return EAGAIN;
    368 	}
    369 
    370 	xfer->tl = -1;
    371 	if (info->flag & FWTI_TLABEL) {
    372 		if (fw_get_tlabel(fc, xfer) < 0)
    373 			return EAGAIN;
    374 	}
    375 
    376 	xfer->resp = 0;
    377 	xfer->fc = fc;
    378 	xfer->q = xferq;
    379 
    380 	fw_asystart(xfer);
    381 	return err;
    382 }
    383 /*
    384  * Wakeup blocked process.
    385  */
    386 void
    387 fw_xferwake(struct fw_xfer *xfer)
    388 {
    389 	fw_mtx_t lock = &xfer->fc->wait_lock;
    390 
    391 	fw_mtx_lock(lock);
    392 	xfer->flag |= FWXF_WAKE;
    393 	fw_mtx_unlock(lock);
    394 
    395 	wakeup(xfer);
    396 	return;
    397 }
    398 
    399 int
    400 fw_xferwait(struct fw_xfer *xfer)
    401 {
    402 	fw_mtx_t lock = &xfer->fc->wait_lock;
    403 	int err = 0;
    404 
    405 	fw_mtx_lock(lock);
    406 	if ((xfer->flag & FWXF_WAKE) == 0)
    407 		err = fw_msleep((void *)xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0);
    408 	fw_mtx_unlock(lock);
    409 
    410 	return (err);
    411 }
    412 
    413 /*
    414  * Async. request with given xfer structure.
    415  */
    416 static void
    417 fw_asystart(struct fw_xfer *xfer)
    418 {
    419 	struct firewire_comm *fc = xfer->fc;
    420 	int s;
    421 	s = splfw();
    422 	/* Protect from interrupt/timeout */
    423 	FW_GLOCK(fc);
    424 	xfer->flag = FWXF_INQ;
    425 	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
    426 #if 0
    427 	xfer->q->queued ++;
    428 #endif
    429 	FW_GUNLOCK(fc);
    430 	splx(s);
    431 	/* XXX just queue for mbuf */
    432 	if (xfer->mbuf == NULL)
    433 		xfer->q->start(fc);
    434 	return;
    435 }
    436 
    437 #if defined(__FreeBSD__)
    438 static void
    439 firewire_identify(driver_t *driver, device_t parent)
    440 {
    441 	BUS_ADD_CHILD(parent, 0, "firewire", -1);
    442 }
    443 
    444 static int
    445 firewire_probe(device_t dev)
    446 {
    447 	device_set_desc(dev, "IEEE1394(FireWire) bus");
    448 	return (0);
    449 }
    450 #elif defined(__NetBSD__)
    451 int
    452 firewirematch(struct device *parent, struct cfdata *cf,
    453     void *aux)
    454 {
    455 	struct fwbus_attach_args *faa = (struct fwbus_attach_args *)aux;
    456 
    457 	if (strcmp(faa->name, "ieee1394if") == 0)
    458 		return (1);
    459 	return (0);
    460 }
    461 #endif
    462 
    463 static void
    464 firewire_xfer_timeout(void *arg, int pending)
    465 {
    466 	struct firewire_comm *fc = (struct firewire_comm *)arg;
    467 	struct fw_xfer *xfer, *txfer;
    468 	struct timeval tv;
    469 	struct timeval split_timeout;
    470 	STAILQ_HEAD(, fw_xfer) xfer_timeout;
    471 	int i, s;
    472 
    473 	split_timeout.tv_sec = 0;
    474 	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
    475 
    476 	microtime(&tv);
    477 	fw_timevalsub(&tv, &split_timeout);
    478 	STAILQ_INIT(&xfer_timeout);
    479 
    480 	s = splfw();
    481 	fw_mtx_lock(&fc->tlabel_lock);
    482 	for (i = 0; i < 0x40; i ++) {
    483 		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
    484 			if ((xfer->flag & FWXF_SENT) == 0)
    485 				/* not sent yet */
    486 				break;
    487 			if (fw_timevalcmp(&xfer->tv, &tv, >))
    488 				/* the rests are newer than this */
    489 				break;
    490 			fw_printf(fc->bdev,
    491 				"split transaction timeout: "
    492 				"tl=0x%x flag=0x%02x\n", i, xfer->flag);
    493 			fw_dump_hdr(&xfer->send.hdr, "send");
    494 			xfer->resp = ETIMEDOUT;
    495 			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
    496 			STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel);
    497 		}
    498 	}
    499 	fw_mtx_unlock(&fc->tlabel_lock);
    500 	splx(s);
    501 	fc->timeout(fc);
    502 
    503 	STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer)
    504 	    xfer->hand(xfer);
    505 }
    506 
    507 #define WATCHDOG_HZ 10
    508 static void
    509 firewire_watchdog(void *arg)
    510 {
    511 	struct firewire_comm *fc;
    512 	static int watchdog_clock = 0;
    513 
    514 	fc = (struct firewire_comm *)arg;
    515 
    516 	/*
    517 	 * At boot stage, the device interrupt is disabled and
    518 	 * We encounter a timeout easily. To avoid this,
    519 	 * ignore clock interrupt for a while.
    520 	 */
    521 	if (watchdog_clock > WATCHDOG_HZ * 15)
    522 		fw_taskqueue_enqueue(fc->taskqueue, &fc->task_timeout);
    523 	else
    524 		watchdog_clock ++;
    525 
    526 	fw_callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ,
    527 			(void *)firewire_watchdog, (void *)fc);
    528 }
    529 
    530 /*
    531  * The attach routine.
    532  */
    533 FW_ATTACH(firewire)
    534 {
    535 	FW_ATTACH_START(firewire, sc, fwa);
    536 	FIREWIRE_ATTACH_START;
    537 
    538 	sc->fc = fc;
    539 	fc->status = FWBUSNOTREADY;
    540 
    541 	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
    542 
    543 	FWDEV_MAKEDEV(sc);
    544 
    545 	fw_mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF);
    546 	fw_mtx_init(&fc->tlabel_lock, "fwtlabel", NULL, MTX_DEF);
    547 	fw_callout_init(&fc->timeout_callout);
    548 	fw_callout_init(&fc->bmr_callout);
    549 	fw_callout_init(&fc->busprobe_callout);
    550 	FW_TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, (void *)fc);
    551 
    552 	fw_callout_reset(&sc->fc->timeout_callout, hz,
    553 			(void *)firewire_watchdog, (void *)sc->fc);
    554 
    555 	/* create thread */
    556 	fw_kthread_create(fw_bus_probe_thread, (void *)fc, &fc->probe_thread,
    557 	    "fw%d_probe", fw_get_unit(fc->bdev));
    558 	fw_config_pending_incr();
    559 
    560 	FIREWIRE_GENERIC_ATTACH;
    561 
    562 	/* bus_reset */
    563 	fw_busreset(fc, FWBUSNOTREADY);
    564 	fc->ibr(fc);
    565 
    566 	FW_ATTACH_RETURN(0);
    567 }
    568 
    569 #if defined(__FreeBSD__)
    570 /*
    571  * Attach it as child.
    572  */
    573 static device_t
    574 firewire_add_child(device_t dev, int order, const char *name, int unit)
    575 {
    576         device_t child;
    577 	struct firewire_softc *sc;
    578 	struct fw_attach_args fwa;
    579 
    580 	sc = (struct firewire_softc *)device_get_softc(dev);
    581 	child = device_add_child(dev, name, unit);
    582 	if (child) {
    583 		fwa.name = name;
    584 		fwa.fc = sc->fc;
    585 		fwa.fwdev = NULL;
    586 		device_set_ivars(child, &fwa);
    587 		device_probe_and_attach(child);
    588 	}
    589 
    590 	return child;
    591 }
    592 
    593 static int
    594 firewire_resume(device_t dev)
    595 {
    596 	struct firewire_softc *sc;
    597 
    598 	sc = (struct firewire_softc *)device_get_softc(dev);
    599 	sc->fc->status = FWBUSNOTREADY;
    600 
    601 	bus_generic_resume(dev);
    602 
    603 	return(0);
    604 }
    605 #endif
    606 
    607 /*
    608  * Dettach it.
    609  */
    610 FW_DETACH(firewire)
    611 {
    612 	FW_DETACH_START(firewire, sc);
    613 	struct firewire_comm *fc;
    614 	struct fw_device *fwdev, *fwdev_next;
    615 
    616 	fc = sc->fc;
    617 	fw_mtx_lock(&fc->wait_lock);
    618 	fc->status = FWBUSDETACH;
    619 	wakeup(fc);
    620 	if (fw_msleep(fc->probe_thread,
    621 	    &fc->wait_lock, PWAIT, "fwthr", hz * 60))
    622 		printf("firewire probe thread didn't die\n");
    623 	fw_mtx_unlock(&fc->wait_lock);
    624 
    625 	FWDEV_DESTROYDEV(sc);
    626 	FIREWIRE_GENERIC_DETACH;
    627 
    628 	fw_callout_stop(&fc->timeout_callout);
    629 	fw_callout_stop(&fc->bmr_callout);
    630 	fw_callout_stop(&fc->busprobe_callout);
    631 
    632 	/* XXX xfree_free and untimeout on all xfers */
    633 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
    634 							fwdev = fwdev_next) {
    635 		fwdev_next = STAILQ_NEXT(fwdev, link);
    636 		free(fwdev, M_FW);
    637 	}
    638 	free(fc->topology_map, M_FW);
    639 	free(fc->speed_map, M_FW);
    640 	free(fc->crom_src_buf, M_FW);
    641 
    642 	fw_mtx_destroy(&fc->wait_lock);
    643 	fw_mtx_destroy(&fc->tlabel_lock);
    644 	return(0);
    645 }
    646 #if defined(__FreeBSD__)
    647 #if 0
    648 static int
    649 firewire_shutdown( device_t dev )
    650 {
    651 	return 0;
    652 }
    653 #endif
    654 #elif defined(__NetBSD__)
    655 int
    656 firewire_print(void *aux, const char *pnp)
    657 {
    658 	struct fw_attach_args *fwa = (struct fw_attach_args *)aux;
    659 
    660 	if (pnp)
    661 		aprint_normal("%s at %s", fwa->name, pnp);
    662 
    663 	return UNCONF;
    664 }
    665 
    666 int
    667 firewire_resume(struct firewire_comm *fc)
    668 {
    669 
    670 	fc->status = FWBUSNOTREADY;
    671 	return 0;
    672 }
    673 #endif
    674 
    675 static void
    676 fw_xferq_drain(struct fw_xferq *xferq)
    677 {
    678 	struct fw_xfer *xfer;
    679 
    680 	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
    681 		STAILQ_REMOVE_HEAD(&xferq->q, link);
    682 #if 0
    683 		xferq->queued --;
    684 #endif
    685 		xfer->resp = EAGAIN;
    686 		xfer->flag = FWXF_SENTERR;
    687 		fw_xfer_done(xfer);
    688 	}
    689 }
    690 
    691 void
    692 fw_drain_txq(struct firewire_comm *fc)
    693 {
    694 	struct fw_xfer *xfer, *txfer;
    695 	STAILQ_HEAD(, fw_xfer) xfer_drain;
    696 	int i;
    697 
    698 	STAILQ_INIT(&xfer_drain);
    699 
    700 	FW_GLOCK(fc);
    701 	fw_xferq_drain(fc->atq);
    702 	fw_xferq_drain(fc->ats);
    703 	for(i = 0; i < fc->nisodma; i++)
    704 		fw_xferq_drain(fc->it[i]);
    705 	FW_GUNLOCK(fc);
    706 
    707 	fw_mtx_lock(&fc->tlabel_lock);
    708 	for (i = 0; i < 0x40; i ++)
    709 		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
    710 			if (firewire_debug)
    711 				printf("tl=%d flag=%d\n", i, xfer->flag);
    712 			xfer->resp = EAGAIN;
    713 			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
    714 			STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel);
    715 		}
    716 	fw_mtx_unlock(&fc->tlabel_lock);
    717 
    718 	STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer)
    719 		xfer->hand(xfer);
    720 }
    721 
    722 static void
    723 fw_reset_csr(struct firewire_comm *fc)
    724 {
    725 	int i;
    726 
    727 	CSRARC(fc, STATE_CLEAR)
    728 			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
    729 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
    730 	CSRARC(fc, NODE_IDS) = 0x3f;
    731 
    732 	CSRARC(fc, TOPO_MAP + 8) = 0;
    733 	fc->irm = -1;
    734 
    735 	fc->max_node = -1;
    736 
    737 	for(i = 2; i < 0x100/4 - 2 ; i++){
    738 		CSRARC(fc, SPED_MAP + i * 4) = 0;
    739 	}
    740 	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
    741 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
    742 	CSRARC(fc, RESET_START) = 0;
    743 	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
    744 	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
    745 	CSRARC(fc, CYCLE_TIME) = 0x0;
    746 	CSRARC(fc, BUS_TIME) = 0x0;
    747 	CSRARC(fc, BUS_MGR_ID) = 0x3f;
    748 	CSRARC(fc, BANDWIDTH_AV) = 4915;
    749 	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
    750 	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
    751 	CSRARC(fc, IP_CHANNELS) = (1 << 31);
    752 
    753 	CSRARC(fc, CONF_ROM) = 0x04 << 24;
    754 	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
    755 	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
    756 				1 << 28 | 0xff << 16 | 0x09 << 8;
    757 	CSRARC(fc, CONF_ROM + 0xc) = 0;
    758 
    759 /* DV depend CSRs see blue book */
    760 	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
    761 	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
    762 
    763 	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
    764 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
    765 }
    766 
    767 static void
    768 fw_init_crom(struct firewire_comm *fc)
    769 {
    770 	struct crom_src *src;
    771 
    772 	fc->crom_src_buf = (struct crom_src_buf *)
    773 		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
    774 	if (fc->crom_src_buf == NULL)
    775 		return;
    776 
    777 	src = &fc->crom_src_buf->src;
    778 	bzero(src, sizeof(struct crom_src));
    779 
    780 	/* BUS info sample */
    781 	src->hdr.info_len = 4;
    782 
    783 	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
    784 
    785 	src->businfo.irmc = 1;
    786 	src->businfo.cmc = 1;
    787 	src->businfo.isc = 1;
    788 	src->businfo.bmc = 1;
    789 	src->businfo.pmc = 0;
    790 	src->businfo.cyc_clk_acc = 100;
    791 	src->businfo.max_rec = fc->maxrec;
    792 	src->businfo.max_rom = MAXROM_4;
    793 	src->businfo.generation = 1;
    794 	src->businfo.link_spd = fc->speed;
    795 
    796 	src->businfo.eui64.hi = fc->eui.hi;
    797 	src->businfo.eui64.lo = fc->eui.lo;
    798 
    799 	STAILQ_INIT(&src->chunk_list);
    800 
    801 	fc->crom_src = src;
    802 	fc->crom_root = &fc->crom_src_buf->root;
    803 }
    804 
    805 static void
    806 fw_reset_crom(struct firewire_comm *fc)
    807 {
    808 	struct crom_src_buf *buf;
    809 	struct crom_src *src;
    810 	struct crom_chunk *root;
    811 
    812 	if (fc->crom_src_buf == NULL)
    813 		fw_init_crom(fc);
    814 
    815 	buf =  fc->crom_src_buf;
    816 	src = fc->crom_src;
    817 	root = fc->crom_root;
    818 
    819 	STAILQ_INIT(&src->chunk_list);
    820 
    821 	bzero(root, sizeof(struct crom_chunk));
    822 	crom_add_chunk(src, NULL, root, 0);
    823 	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
    824 	/* private company_id */
    825 	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
    826 	crom_add_simple_text(src, root, &buf->vendor, PROJECT_STR);
    827 	crom_add_entry(root, CSRKEY_HW, OS_VER);
    828 	crom_add_simple_text(src, root, &buf->hw, hostname);
    829 }
    830 
    831 /*
    832  * Called after bus reset.
    833  */
    834 void
    835 fw_busreset(struct firewire_comm *fc, uint32_t new_status)
    836 {
    837 	struct firewire_dev_comm *fdc;
    838 	struct crom_src *src;
    839 	void *newrom;
    840 
    841 	switch(fc->status){
    842 	case FWBUSMGRELECT:
    843 		fw_callout_stop(&fc->bmr_callout);
    844 		break;
    845 	default:
    846 		break;
    847 	}
    848 	fc->status = new_status;
    849 	fw_reset_csr(fc);
    850 	fw_reset_crom(fc);
    851 
    852 	FIREWIRE_CHILDREN_FOREACH_FUNC(post_busreset, fdc);
    853 
    854 	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
    855 	src = &fc->crom_src_buf->src;
    856 	crom_load(src, (uint32_t *)newrom, CROMSIZE);
    857 	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
    858 		/* bump generation and reload */
    859 		src->businfo.generation ++;
    860 		/* generation must be between 0x2 and 0xF */
    861 		if (src->businfo.generation < 2)
    862 			src->businfo.generation ++;
    863 		crom_load(src, (uint32_t *)newrom, CROMSIZE);
    864 		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
    865 	}
    866 	free(newrom, M_FW);
    867 }
    868 
    869 /* Call once after reboot */
    870 void fw_init(struct firewire_comm *fc)
    871 {
    872 	int i;
    873 #ifdef FW_VMACCESS
    874 	struct fw_xfer *xfer;
    875 	struct fw_bind *fwb;
    876 #endif
    877 
    878 	fc->arq->queued = 0;
    879 	fc->ars->queued = 0;
    880 	fc->atq->queued = 0;
    881 	fc->ats->queued = 0;
    882 
    883 	fc->arq->buf = NULL;
    884 	fc->ars->buf = NULL;
    885 	fc->atq->buf = NULL;
    886 	fc->ats->buf = NULL;
    887 
    888 	fc->arq->flag = 0;
    889 	fc->ars->flag = 0;
    890 	fc->atq->flag = 0;
    891 	fc->ats->flag = 0;
    892 
    893 	STAILQ_INIT(&fc->atq->q);
    894 	STAILQ_INIT(&fc->ats->q);
    895 
    896 	for( i = 0 ; i < fc->nisodma ; i ++ ){
    897 		fc->it[i]->queued = 0;
    898 		fc->ir[i]->queued = 0;
    899 
    900 		fc->it[i]->start = NULL;
    901 		fc->ir[i]->start = NULL;
    902 
    903 		fc->it[i]->buf = NULL;
    904 		fc->ir[i]->buf = NULL;
    905 
    906 		fc->it[i]->flag = FWXFERQ_STREAM;
    907 		fc->ir[i]->flag = FWXFERQ_STREAM;
    908 
    909 		STAILQ_INIT(&fc->it[i]->q);
    910 		STAILQ_INIT(&fc->ir[i]->q);
    911 	}
    912 
    913 	fc->arq->maxq = FWMAXQUEUE;
    914 	fc->ars->maxq = FWMAXQUEUE;
    915 	fc->atq->maxq = FWMAXQUEUE;
    916 	fc->ats->maxq = FWMAXQUEUE;
    917 
    918 	for( i = 0 ; i < fc->nisodma ; i++){
    919 		fc->ir[i]->maxq = FWMAXQUEUE;
    920 		fc->it[i]->maxq = FWMAXQUEUE;
    921 	}
    922 /* Initialize csr registers */
    923 	fc->topology_map = (struct fw_topology_map *)malloc(
    924 				sizeof(struct fw_topology_map),
    925 				M_FW, M_NOWAIT | M_ZERO);
    926 	fc->speed_map = (struct fw_speed_map *)malloc(
    927 				sizeof(struct fw_speed_map),
    928 				M_FW, M_NOWAIT | M_ZERO);
    929 	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
    930 	CSRARC(fc, TOPO_MAP + 4) = 1;
    931 	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
    932 	CSRARC(fc, SPED_MAP + 4) = 1;
    933 
    934 	STAILQ_INIT(&fc->devices);
    935 
    936 /* Initialize Async handlers */
    937 	STAILQ_INIT(&fc->binds);
    938 	for( i = 0 ; i < 0x40 ; i++){
    939 		STAILQ_INIT(&fc->tlabels[i]);
    940 	}
    941 
    942 /* DV depend CSRs see blue book */
    943 #if 0
    944 	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
    945 	CSRARC(fc, oPCR) = 0x8000007a;
    946 	for(i = 4 ; i < 0x7c/4 ; i+=4){
    947 		CSRARC(fc, i + oPCR) = 0x8000007a;
    948 	}
    949 
    950 	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
    951 	CSRARC(fc, iPCR) = 0x803f0000;
    952 	for(i = 4 ; i < 0x7c/4 ; i+=4){
    953 		CSRARC(fc, i + iPCR) = 0x0;
    954 	}
    955 #endif
    956 
    957 	fc->crom_src_buf = NULL;
    958 
    959 #ifdef FW_VMACCESS
    960 	xfer = fw_xfer_alloc();
    961 	if(xfer == NULL) return;
    962 
    963 	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
    964 	if(fwb == NULL){
    965 		fw_xfer_free(xfer);
    966 		return;
    967 	}
    968 	xfer->hand = fw_vmaccess;
    969 	xfer->fc = fc;
    970 	xfer->sc = NULL;
    971 
    972 	fwb->start_hi = 0x2;
    973 	fwb->start_lo = 0;
    974 	fwb->addrlen = 0xffffffff;
    975 	fwb->xfer = xfer;
    976 	fw_bindadd(fc, fwb);
    977 #endif
    978 }
    979 
    980 #define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
    981     ((fwb)->end < (addr))?1:0)
    982 
    983 /*
    984  * To lookup bound process from IEEE1394 address.
    985  */
    986 struct fw_bind *
    987 fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
    988 {
    989 	u_int64_t addr;
    990 	struct fw_bind *tfw, *r = NULL;
    991 
    992 	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
    993 	FW_GLOCK(fc);
    994 	STAILQ_FOREACH(tfw, &fc->binds, fclist)
    995 		if (BIND_CMP(addr, tfw) == 0) {
    996 			r = tfw;
    997 			break;
    998 		}
    999 	FW_GUNLOCK(fc);
   1000 	return(r);
   1001 }
   1002 
   1003 /*
   1004  * To bind IEEE1394 address block to process.
   1005  */
   1006 int
   1007 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
   1008 {
   1009 	struct fw_bind *tfw, *prev = NULL;
   1010 	int r = 0;
   1011 
   1012 	if (fwb->start > fwb->end) {
   1013 		printf("%s: invalid range\n", __func__);
   1014 		return EINVAL;
   1015 	}
   1016 
   1017 	FW_GLOCK(fc);
   1018 	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
   1019 		if (fwb->end < tfw->start)
   1020 			break;
   1021 		prev = tfw;
   1022 	}
   1023 	if (prev == NULL)
   1024 		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
   1025 	else if (prev->end < fwb->start)
   1026 		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
   1027 	else {
   1028 		printf("%s: bind failed\n", __func__);
   1029 		r = EBUSY;
   1030 	}
   1031 	FW_GUNLOCK(fc);
   1032 	return (r);
   1033 }
   1034 
   1035 /*
   1036  * To free IEEE1394 address block.
   1037  */
   1038 int
   1039 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
   1040 {
   1041 #if 0
   1042 	struct fw_xfer *xfer, *next;
   1043 #endif
   1044 	struct fw_bind *tfw;
   1045 	int s;
   1046 
   1047 	s = splfw();
   1048 	FW_GLOCK(fc);
   1049 	STAILQ_FOREACH(tfw, &fc->binds, fclist)
   1050 		if (tfw == fwb) {
   1051 			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
   1052 			goto found;
   1053 		}
   1054 
   1055 	printf("%s: no such binding\n", __func__);
   1056 	FW_GUNLOCK(fc);
   1057 	splx(s);
   1058 	return (1);
   1059 found:
   1060 #if 0
   1061 	/* shall we do this? */
   1062 	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
   1063 		next = STAILQ_NEXT(xfer, link);
   1064 		fw_xfer_free(xfer);
   1065 	}
   1066 	STAILQ_INIT(&fwb->xferlist);
   1067 #endif
   1068 	FW_GUNLOCK(fc);
   1069 
   1070 	splx(s);
   1071 	return 0;
   1072 }
   1073 
   1074 int
   1075 fw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
   1076     int slen, int rlen, int n,
   1077     struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
   1078 {
   1079 	int i, s;
   1080 	struct fw_xfer *xfer;
   1081 
   1082 	for (i = 0; i < n; i++) {
   1083 		xfer = fw_xfer_alloc_buf(type, slen, rlen);
   1084 		if (xfer == NULL)
   1085 			return (n);
   1086 		xfer->fc = fc;
   1087 		xfer->sc = sc;
   1088 		xfer->hand = hand;
   1089 		s = splfw();
   1090 		STAILQ_INSERT_TAIL(q, xfer, link);
   1091 		splx(s);
   1092 	}
   1093 	return (n);
   1094 }
   1095 
   1096 void
   1097 fw_xferlist_remove(struct fw_xferlist *q)
   1098 {
   1099 	struct fw_xfer *xfer, *next;
   1100 
   1101 	for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
   1102 		next = STAILQ_NEXT(xfer, link);
   1103 		fw_xfer_free_buf(xfer);
   1104 	}
   1105 	STAILQ_INIT(q);
   1106 }
   1107 
   1108 /*
   1109  * dump packet header
   1110  */
   1111 static void
   1112 fw_dump_hdr(struct fw_pkt *fp, const char *prefix)
   1113 {
   1114 	printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
   1115 	    "src=0x%03x\n", prefix,
   1116 	     fp->mode.hdr.dst & 0x3f,
   1117 	     fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3,
   1118 	     fp->mode.hdr.tcode, fp->mode.hdr.pri,
   1119 	     fp->mode.hdr.src);
   1120 }
   1121 
   1122 /*
   1123  * To free transaction label.
   1124  */
   1125 static void
   1126 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
   1127 {
   1128 	struct fw_xfer *txfer;
   1129 	int s;
   1130 
   1131 	if (xfer->tl < 0)
   1132 		return;
   1133 
   1134 	s = splfw();
   1135 	fw_mtx_lock(&fc->tlabel_lock);
   1136 #if 1 /* make sure the label is allocated */
   1137 	STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
   1138 		if(txfer == xfer)
   1139 			break;
   1140 	if (txfer == NULL) {
   1141 		printf("%s: the xfer is not in the queue "
   1142 		    "(tlabel=%d, flag=0x%x)\n",
   1143 		    __FUNCTION__, xfer->tl, xfer->flag);
   1144 		fw_dump_hdr(&xfer->send.hdr, "send");
   1145 		fw_dump_hdr(&xfer->recv.hdr, "recv");
   1146 		kdb_backtrace();
   1147 		fw_mtx_unlock(&fc->tlabel_lock);
   1148 		splx(s);
   1149 		return;
   1150 	}
   1151 #endif
   1152 
   1153 	STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
   1154 	fw_mtx_unlock(&fc->tlabel_lock);
   1155 	splx(s);
   1156 	return;
   1157 }
   1158 
   1159 /*
   1160  * To obtain XFER structure by transaction label.
   1161  */
   1162 static struct fw_xfer *
   1163 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode)
   1164 {
   1165 	struct fw_xfer *xfer;
   1166 	int s = splfw();
   1167 	int req;
   1168 
   1169 	fw_mtx_lock(&fc->tlabel_lock);
   1170 	STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
   1171 		if(xfer->send.hdr.mode.hdr.dst == node) {
   1172 			fw_mtx_unlock(&fc->tlabel_lock);
   1173 			splx(s);
   1174 			FW_KASSERT(xfer->tl == tlabel,
   1175 			    ("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel));
   1176 			/* extra sanity check */
   1177 			req = xfer->send.hdr.mode.hdr.tcode;
   1178 			if (xfer->fc->tcode[req].valid_res != tcode) {
   1179 				printf("%s: invalid response tcode "
   1180 				    "(0x%x for 0x%x)\n", __FUNCTION__,
   1181 				    tcode, req);
   1182 				return(NULL);
   1183 			}
   1184 
   1185 			if (firewire_debug > 2)
   1186 				printf("fw_tl2xfer: found tl=%d\n", tlabel);
   1187 			return(xfer);
   1188 		}
   1189 	fw_mtx_unlock(&fc->tlabel_lock);
   1190 	if (firewire_debug > 1)
   1191 		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
   1192 	splx(s);
   1193 	return(NULL);
   1194 }
   1195 
   1196 /*
   1197  * To allocate IEEE1394 XFER structure.
   1198  */
   1199 struct fw_xfer *
   1200 fw_xfer_alloc(struct malloc_type *type)
   1201 {
   1202 	struct fw_xfer *xfer;
   1203 
   1204 	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
   1205 	if (xfer == NULL)
   1206 		return xfer;
   1207 
   1208 	xfer->malloc = type;
   1209 
   1210 	return xfer;
   1211 }
   1212 
   1213 struct fw_xfer *
   1214 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
   1215 {
   1216 	struct fw_xfer *xfer;
   1217 
   1218 	xfer = fw_xfer_alloc(type);
   1219 	if (xfer == NULL)
   1220 		return(NULL);
   1221 	xfer->send.pay_len = send_len;
   1222 	xfer->recv.pay_len = recv_len;
   1223 	if (send_len > 0) {
   1224 		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
   1225 		if (xfer->send.payload == NULL) {
   1226 			fw_xfer_free(xfer);
   1227 			return(NULL);
   1228 		}
   1229 	}
   1230 	if (recv_len > 0) {
   1231 		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
   1232 		if (xfer->recv.payload == NULL) {
   1233 			if (xfer->send.payload != NULL)
   1234 				free(xfer->send.payload, type);
   1235 			fw_xfer_free(xfer);
   1236 			return(NULL);
   1237 		}
   1238 	}
   1239 	return(xfer);
   1240 }
   1241 
   1242 /*
   1243  * IEEE1394 XFER post process.
   1244  */
   1245 void
   1246 fw_xfer_done(struct fw_xfer *xfer)
   1247 {
   1248 	if (xfer->hand == NULL) {
   1249 		printf("hand == NULL\n");
   1250 		return;
   1251 	}
   1252 
   1253 	if (xfer->fc == NULL)
   1254 		panic("fw_xfer_done: why xfer->fc is NULL?");
   1255 
   1256 	fw_tl_free(xfer->fc, xfer);
   1257 	xfer->hand(xfer);
   1258 }
   1259 
   1260 void
   1261 fw_xfer_unload(struct fw_xfer* xfer)
   1262 {
   1263 	int s;
   1264 
   1265 	if(xfer == NULL ) return;
   1266 	if(xfer->flag & FWXF_INQ){
   1267 		printf("fw_xfer_free FWXF_INQ\n");
   1268 		s = splfw();
   1269 		FW_GLOCK(xfer->fc);
   1270 		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
   1271 #if 0
   1272 		xfer->q->queued --;
   1273 #endif
   1274 		FW_GUNLOCK(xfer->fc);
   1275 		splx(s);
   1276 	}
   1277 	if (xfer->fc != NULL) {
   1278 #if 1
   1279 		if(xfer->flag == FWXF_START)
   1280 			/*
   1281 			 * This could happen if:
   1282 			 *  1. We call fwohci_arcv() before fwohci_txd().
   1283 			 *  2. firewire_watch() is called.
   1284 			 */
   1285 			printf("fw_xfer_free FWXF_START\n");
   1286 #endif
   1287 	}
   1288 	xfer->flag = FWXF_INIT;
   1289 	xfer->resp = 0;
   1290 }
   1291 /*
   1292  * To free IEEE1394 XFER structure.
   1293  */
   1294 void
   1295 fw_xfer_free_buf( struct fw_xfer* xfer)
   1296 {
   1297 	if (xfer == NULL) {
   1298 		printf("%s: xfer == NULL\n", __func__);
   1299 		return;
   1300 	}
   1301 	fw_xfer_unload(xfer);
   1302 	if(xfer->send.payload != NULL){
   1303 		free(xfer->send.payload, xfer->malloc);
   1304 	}
   1305 	if(xfer->recv.payload != NULL){
   1306 		free(xfer->recv.payload, xfer->malloc);
   1307 	}
   1308 	free(xfer, xfer->malloc);
   1309 }
   1310 
   1311 void
   1312 fw_xfer_free( struct fw_xfer* xfer)
   1313 {
   1314 	if (xfer == NULL) {
   1315 		printf("%s: xfer == NULL\n", __func__);
   1316 		return;
   1317 	}
   1318 	fw_xfer_unload(xfer);
   1319 	free(xfer, xfer->malloc);
   1320 }
   1321 
   1322 void
   1323 fw_asy_callback_free(struct fw_xfer *xfer)
   1324 {
   1325 #if 0
   1326 	printf("asyreq done flag=%d resp=%d\n",
   1327 				xfer->flag, xfer->resp);
   1328 #endif
   1329 	fw_xfer_free(xfer);
   1330 }
   1331 
   1332 /*
   1333  * To configure PHY.
   1334  */
   1335 static void
   1336 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
   1337 {
   1338 	struct fw_xfer *xfer;
   1339 	struct fw_pkt *fp;
   1340 
   1341 	fc->status = FWBUSPHYCONF;
   1342 
   1343 	xfer = fw_xfer_alloc(M_FWXFER);
   1344 	if (xfer == NULL)
   1345 		return;
   1346 	xfer->fc = fc;
   1347 	xfer->hand = fw_asy_callback_free;
   1348 
   1349 	fp = &xfer->send.hdr;
   1350 	fp->mode.ld[1] = 0;
   1351 	if (root_node >= 0)
   1352 		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
   1353 	if (gap_count >= 0)
   1354 		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
   1355 	fp->mode.ld[2] = ~fp->mode.ld[1];
   1356 /* XXX Dangerous, how to pass PHY packet to device driver */
   1357 	fp->mode.common.tcode |= FWTCODE_PHY;
   1358 
   1359 	if (firewire_debug)
   1360 		printf("send phy_config root_node=%d gap_count=%d\n",
   1361 						root_node, gap_count);
   1362 	fw_asyreq(fc, -1, xfer);
   1363 }
   1364 
   1365 #if 0
   1366 /*
   1367  * Dump self ID.
   1368  */
   1369 static void
   1370 fw_print_sid(uint32_t sid)
   1371 {
   1372 	union fw_self_id *s;
   1373 	s = (union fw_self_id *) &sid;
   1374 	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
   1375 		" p0:%d p1:%d p2:%d i:%d m:%d\n",
   1376 		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
   1377 		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
   1378 		s->p0.power_class, s->p0.port0, s->p0.port1,
   1379 		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
   1380 }
   1381 #endif
   1382 
   1383 /*
   1384  * To receive self ID.
   1385  */
   1386 void fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
   1387 {
   1388 	uint32_t *p;
   1389 	union fw_self_id *self_id;
   1390 	u_int i, j, node, c_port = 0, i_branch = 0;
   1391 
   1392 	fc->sid_cnt = len /(sizeof(uint32_t) * 2);
   1393 	fc->max_node = fc->nodeid & 0x3f;
   1394 	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
   1395 	fc->status = FWBUSCYMELECT;
   1396 	fc->topology_map->crc_len = 2;
   1397 	fc->topology_map->generation ++;
   1398 	fc->topology_map->self_id_count = 0;
   1399 	fc->topology_map->node_count = 0;
   1400 	fc->speed_map->generation ++;
   1401 	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
   1402 	self_id = &fc->topology_map->self_id[0];
   1403 	for(i = 0; i < fc->sid_cnt; i ++){
   1404 		if (sid[1] != ~sid[0]) {
   1405 			printf("fw_sidrcv: invalid self-id packet\n");
   1406 			sid += 2;
   1407 			continue;
   1408 		}
   1409 		*self_id = *((union fw_self_id *)sid);
   1410 		fc->topology_map->crc_len++;
   1411 		if(self_id->p0.sequel == 0){
   1412 			fc->topology_map->node_count ++;
   1413 			c_port = 0;
   1414 #if 0
   1415 			fw_print_sid(sid[0]);
   1416 #endif
   1417 			node = self_id->p0.phy_id;
   1418 			if(fc->max_node < node){
   1419 				fc->max_node = self_id->p0.phy_id;
   1420 			}
   1421 			/* XXX I'm not sure this is the right speed_map */
   1422 			fc->speed_map->speed[node][node]
   1423 					= self_id->p0.phy_speed;
   1424 			for (j = 0; j < node; j ++) {
   1425 				fc->speed_map->speed[j][node]
   1426 					= fc->speed_map->speed[node][j]
   1427 					= min(fc->speed_map->speed[j][j],
   1428 							self_id->p0.phy_speed);
   1429 			}
   1430 			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
   1431 			  (self_id->p0.link_active && self_id->p0.contender)) {
   1432 				fc->irm = self_id->p0.phy_id;
   1433 			}
   1434 			if(self_id->p0.port0 >= 0x2){
   1435 				c_port++;
   1436 			}
   1437 			if(self_id->p0.port1 >= 0x2){
   1438 				c_port++;
   1439 			}
   1440 			if(self_id->p0.port2 >= 0x2){
   1441 				c_port++;
   1442 			}
   1443 		}
   1444 		if(c_port > 2){
   1445 			i_branch += (c_port - 2);
   1446 		}
   1447 		sid += 2;
   1448 		self_id++;
   1449 		fc->topology_map->self_id_count ++;
   1450 	}
   1451 	fw_printf(fc->bdev, "%d nodes", fc->max_node + 1);
   1452 	/* CRC */
   1453 	fc->topology_map->crc = fw_crc16(
   1454 			(uint32_t *)&fc->topology_map->generation,
   1455 			fc->topology_map->crc_len * 4);
   1456 	fc->speed_map->crc = fw_crc16(
   1457 			(uint32_t *)&fc->speed_map->generation,
   1458 			fc->speed_map->crc_len * 4);
   1459 	/* byteswap and copy to CSR */
   1460 	p = (uint32_t *)fc->topology_map;
   1461 	for (i = 0; i <= fc->topology_map->crc_len; i++)
   1462 		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
   1463 	p = (uint32_t *)fc->speed_map;
   1464 	CSRARC(fc, SPED_MAP) = htonl(*p++);
   1465 	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
   1466 	/* don't byte-swap uint8_t array */
   1467 	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
   1468 
   1469 	fc->max_hop = fc->max_node - i_branch;
   1470 	printf(", maxhop <= %d", fc->max_hop);
   1471 
   1472 	if(fc->irm == -1 ){
   1473 		printf(", Not found IRM capable node");
   1474 	}else{
   1475 		printf(", cable IRM = %d", fc->irm);
   1476 		if (fc->irm == fc->nodeid)
   1477 			printf(" (me)");
   1478 	}
   1479 	printf("\n");
   1480 
   1481 	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
   1482 		if (fc->irm == fc->nodeid) {
   1483 			fc->status = FWBUSMGRDONE;
   1484 			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
   1485 			fw_bmr(fc);
   1486 		} else {
   1487 			fc->status = FWBUSMGRELECT;
   1488 			fw_callout_reset(&fc->bmr_callout, hz/8,
   1489 				(void *)fw_try_bmr, (void *)fc);
   1490 		}
   1491 	} else
   1492 		fc->status = FWBUSMGRDONE;
   1493 
   1494 	fw_callout_reset(&fc->busprobe_callout, hz/4,
   1495 			(void *)fw_bus_probe, (void *)fc);
   1496 }
   1497 
   1498 /*
   1499  * To probe devices on the IEEE1394 bus.
   1500  */
   1501 static void
   1502 fw_bus_probe(struct firewire_comm *fc)
   1503 {
   1504 	int s;
   1505 	struct fw_device *fwdev;
   1506 
   1507 	s = splfw();
   1508 	fc->status = FWBUSEXPLORE;
   1509 
   1510 	/* Invalidate all devices, just after bus reset. */
   1511 	STAILQ_FOREACH(fwdev, &fc->devices, link)
   1512 		if (fwdev->status != FWDEVINVAL) {
   1513 			fwdev->status = FWDEVINVAL;
   1514 			fwdev->rcnt = 0;
   1515 		}
   1516 	splx(s);
   1517 
   1518 	wakeup((void *)fc);
   1519 }
   1520 
   1521 static int
   1522 fw_explore_read_quads(struct fw_device *fwdev, int offset,
   1523     uint32_t *quad, int n)
   1524 {
   1525 	struct fw_xfer *xfer;
   1526 	uint32_t tmp;
   1527 	int i, error;
   1528 
   1529 
   1530 	for (i = 0; i < n; i ++, offset += sizeof(uint32_t)) {
   1531 		xfer = fwmem_read_quad(fwdev, NULL, -1,
   1532 		    0xffff, 0xf0000000 | offset, (void *)&tmp,
   1533 		    fw_xferwake);
   1534 		if (xfer == NULL)
   1535 			return (-1);
   1536 		fw_xferwait(xfer);
   1537 
   1538 		if (xfer->resp == 0)
   1539 			quad[i] = ntohl(tmp);
   1540 
   1541 		error = xfer->resp;
   1542 		fw_xfer_free(xfer);
   1543 		if (error)
   1544 			return (error);
   1545 	}
   1546 	return (0);
   1547 }
   1548 
   1549 
   1550 static int
   1551 fw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
   1552 {
   1553 	int err, i, off;
   1554 	struct csrdirectory *dir;
   1555 	struct csrreg *reg;
   1556 
   1557 
   1558 	dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)];
   1559 	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
   1560 	    (uint32_t *)dir, 1);
   1561 	if (err)
   1562 		return (-1);
   1563 
   1564 	offset += sizeof(uint32_t);
   1565 	reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)];
   1566 	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
   1567 	    (uint32_t *)reg, dir->crc_len);
   1568 	if (err)
   1569 		return (-1);
   1570 
   1571 	/* XXX check CRC */
   1572 
   1573 	off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
   1574 	if (fwdev->rommax < off)
   1575 		fwdev->rommax = off;
   1576 
   1577 	if (recur == 0)
   1578 		return (0);
   1579 
   1580 	for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) {
   1581 		if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D)
   1582 			recur = 1;
   1583 		else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L)
   1584 			recur = 0;
   1585 		else
   1586 			continue;
   1587 
   1588 		off = offset + reg[i].val * sizeof(uint32_t);
   1589 		if (off > CROMSIZE) {
   1590 			printf("%s: invalid offset %d\n", __FUNCTION__, off);
   1591 			return(-1);
   1592 		}
   1593 		err = fw_explore_csrblock(fwdev, off, recur);
   1594 		if (err)
   1595 			return (-1);
   1596 	}
   1597 	return (0);
   1598 }
   1599 
   1600 static int
   1601 fw_explore_node(struct fw_device *dfwdev)
   1602 {
   1603 	struct firewire_comm *fc;
   1604 	struct fw_device *fwdev, *pfwdev, *tfwdev;
   1605 	uint32_t *csr;
   1606 	struct csrhdr *hdr;
   1607 	struct bus_info *binfo;
   1608 	int err, node, spd;
   1609 
   1610 	fc = dfwdev->fc;
   1611 	csr = dfwdev->csrrom;
   1612 	node = dfwdev->dst;
   1613 
   1614 	/* First quad */
   1615 	err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1);
   1616 	if (err)
   1617 		return (-1);
   1618 	hdr = (struct csrhdr *)&csr[0];
   1619 	if (hdr->info_len != 4) {
   1620 		if (firewire_debug)
   1621 			printf("node%d: wrong bus info len(%d)\n",
   1622 			    node, hdr->info_len);
   1623 		return (-1);
   1624 	}
   1625 
   1626 	/* bus info */
   1627 	err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
   1628 	if (err)
   1629 		return (-1);
   1630 	binfo = (struct bus_info *)&csr[1];
   1631 	if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
   1632 		if (firewire_debug)
   1633 			printf("node%d: invalid bus name 0x%08x\n",
   1634 			    node, binfo->bus_name);
   1635 		return (-1);
   1636 	}
   1637 	spd = fc->speed_map->speed[fc->nodeid][node];
   1638 	STAILQ_FOREACH(fwdev, &fc->devices, link)
   1639 		if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
   1640 			break;
   1641 	if (fwdev == NULL) {
   1642 		/* new device */
   1643 		fwdev = malloc(sizeof(struct fw_device), M_FW,
   1644 						M_NOWAIT | M_ZERO);
   1645 		if (fwdev == NULL) {
   1646 			if (firewire_debug)
   1647 				printf("node%d: no memory\n", node);
   1648 			return (-1);
   1649 		}
   1650 		fwdev->fc = fc;
   1651 		fwdev->eui = binfo->eui64;
   1652 		fwdev->status = FWDEVNEW;
   1653 		/* insert into sorted fwdev list */
   1654 		pfwdev = NULL;
   1655 		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
   1656 			if (tfwdev->eui.hi > fwdev->eui.hi ||
   1657 				(tfwdev->eui.hi == fwdev->eui.hi &&
   1658 				tfwdev->eui.lo > fwdev->eui.lo))
   1659 				break;
   1660 			pfwdev = tfwdev;
   1661 		}
   1662 		if (pfwdev == NULL)
   1663 			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
   1664 		else
   1665 			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
   1666 
   1667 		fw_printf(fc->bdev, "New %s device ID:%08x%08x\n",
   1668 		    fw_linkspeed[spd],
   1669 		    fwdev->eui.hi, fwdev->eui.lo);
   1670 
   1671 	} else
   1672 		fwdev->status = FWDEVINIT;
   1673 	fwdev->dst = node;
   1674 	fwdev->speed = spd;
   1675 
   1676 	/* unchanged ? */
   1677 	if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) {
   1678 		if (firewire_debug)
   1679 			printf("node%d: crom unchanged\n", node);
   1680 		return (0);
   1681 	}
   1682 
   1683 	bzero(&fwdev->csrrom[0], CROMSIZE);
   1684 
   1685 	/* copy first quad and bus info block */
   1686 	bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5);
   1687 	fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
   1688 
   1689 	err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
   1690 
   1691 	if (err) {
   1692 		fwdev->status = FWDEVINVAL;
   1693 		fwdev->csrrom[0] = 0;
   1694 	}
   1695 	return (err);
   1696 
   1697 }
   1698 
   1699 /*
   1700  * Find the self_id packet for a node, ignoring sequels.
   1701  */
   1702 static union fw_self_id *
   1703 fw_find_self_id(struct firewire_comm *fc, int node)
   1704 {
   1705 	uint32_t i;
   1706 	union fw_self_id *s;
   1707 
   1708 	for (i = 0; i < fc->topology_map->self_id_count; i++) {
   1709 		s = &fc->topology_map->self_id[i];
   1710 		if (s->p0.sequel)
   1711 			continue;
   1712 		if (s->p0.phy_id == node)
   1713 			return s;
   1714 	}
   1715 	return 0;
   1716 }
   1717 
   1718 static void
   1719 fw_explore(struct firewire_comm *fc)
   1720 {
   1721 	int node, err, s, i, todo, todo2, trys;
   1722 	char nodes[63];
   1723 	struct fw_device *dfwdev;
   1724 	union fw_self_id *fwsid;
   1725 
   1726 	todo = 0;
   1727 	dfwdev = malloc(sizeof(*dfwdev), M_TEMP, M_NOWAIT);
   1728 	if (dfwdev == NULL)
   1729 		return;
   1730 	/* setup dummy fwdev */
   1731 	dfwdev->fc = fc;
   1732 	dfwdev->speed = 0;
   1733 	dfwdev->maxrec = 8; /* 512 */
   1734 	dfwdev->status = FWDEVINIT;
   1735 
   1736 	for (node = 0; node <= fc->max_node; node ++) {
   1737 		/* We don't probe myself and linkdown nodes */
   1738 		if (node == fc->nodeid)
   1739 			continue;
   1740 		fwsid = fw_find_self_id(fc, node);
   1741 		if (!fwsid || !fwsid->p0.link_active) {
   1742 			if (firewire_debug)
   1743 				printf("node%d: link down\n", node);
   1744 			continue;
   1745 		}
   1746 		nodes[todo++] = node;
   1747 	}
   1748 
   1749 	s = splfw();
   1750 	for (trys = 0; todo > 0 && trys < 3; trys ++) {
   1751 		todo2 = 0;
   1752 		for (i = 0; i < todo; i ++) {
   1753 			dfwdev->dst = nodes[i];
   1754 			err = fw_explore_node(dfwdev);
   1755 			if (err)
   1756 				nodes[todo2++] = nodes[i];
   1757 			if (firewire_debug)
   1758 				printf("%s: node %d, err = %d\n",
   1759 					__FUNCTION__, node, err);
   1760 		}
   1761 		todo = todo2;
   1762 	}
   1763 	splx(s);
   1764 	free(dfwdev, M_TEMP);
   1765 }
   1766 
   1767 static void
   1768 fw_bus_probe_thread(void *arg)
   1769 {
   1770 	struct firewire_comm *fc;
   1771 
   1772 	fc = (struct firewire_comm *)arg;
   1773 
   1774 	fw_config_pending_decr();
   1775 
   1776 	fw_mtx_lock(&fc->wait_lock);
   1777 	while (fc->status != FWBUSDETACH) {
   1778 		if (fc->status == FWBUSEXPLORE) {
   1779 			fw_mtx_unlock(&fc->wait_lock);
   1780 			fw_explore(fc);
   1781 			fc->status = FWBUSEXPDONE;
   1782 			if (firewire_debug)
   1783 				printf("bus_explore done\n");
   1784 			fw_attach_dev(fc);
   1785 			fw_mtx_lock(&fc->wait_lock);
   1786 		}
   1787 		fw_msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0);
   1788 	}
   1789 	fw_mtx_unlock(&fc->wait_lock);
   1790 	fw_kthread_exit(0);
   1791 }
   1792 
   1793 
   1794 /*
   1795  * To attach sub-devices layer onto IEEE1394 bus.
   1796  */
   1797 static void
   1798 fw_attach_dev(struct firewire_comm *fc)
   1799 {
   1800 	struct fw_device *fwdev, *next;
   1801 	struct firewire_dev_comm *fdc;
   1802 	struct fw_attach_args fwa;
   1803 
   1804 	fwa.name = "sbp";
   1805 	fwa.fc = fc;
   1806 
   1807 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
   1808 		next = STAILQ_NEXT(fwdev, link);
   1809 		switch (fwdev->status) {
   1810 		case FWDEVNEW:
   1811 			FIREWIRE_SBP_ATTACH;
   1812 
   1813 		case FWDEVINIT:
   1814 		case FWDEVATTACHED:
   1815 			fwdev->status = FWDEVATTACHED;
   1816 			break;
   1817 
   1818 		case FWDEVINVAL:
   1819 			fwdev->rcnt ++;
   1820 			break;
   1821 
   1822 		default:
   1823 			/* XXX */
   1824 			break;
   1825 		}
   1826 	}
   1827 
   1828 	FIREWIRE_CHILDREN_FOREACH_FUNC(post_explore, fdc);
   1829 
   1830 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
   1831 		next = STAILQ_NEXT(fwdev, link);
   1832 		if (fwdev->rcnt > 0 && fwdev->rcnt > hold_count) {
   1833 			/*
   1834 			 * Remove devices which have not been seen
   1835 			 * for a while.
   1836 			 */
   1837 			FIREWIRE_SBP_DETACH;
   1838 			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
   1839 			free(fwdev, M_FW);
   1840 		}
   1841 	}
   1842 
   1843 	return;
   1844 }
   1845 
   1846 /*
   1847  * To allocate unique transaction label.
   1848  */
   1849 static int
   1850 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
   1851 {
   1852 	u_int dst, new_tlabel;
   1853 	struct fw_xfer *txfer;
   1854 	int s;
   1855 
   1856 	dst = xfer->send.hdr.mode.hdr.dst & 0x3f;
   1857 	s = splfw();
   1858 	fw_mtx_lock(&fc->tlabel_lock);
   1859 	new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f;
   1860 	STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel)
   1861 		if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst)
   1862 			break;
   1863 	if(txfer == NULL) {
   1864 		fc->last_tlabel[dst] = new_tlabel;
   1865 		STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel);
   1866 		fw_mtx_unlock(&fc->tlabel_lock);
   1867 		splx(s);
   1868 		xfer->tl = new_tlabel;
   1869 		xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2;
   1870 		if (firewire_debug > 1)
   1871 			printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel);
   1872 		return (new_tlabel);
   1873 	}
   1874 	fw_mtx_unlock(&fc->tlabel_lock);
   1875 	splx(s);
   1876 
   1877 	if (firewire_debug > 1)
   1878 		printf("fw_get_tlabel: no free tlabel\n");
   1879 	return (-1);
   1880 }
   1881 
   1882 static void
   1883 fw_rcv_copy(struct fw_rcv_buf *rb)
   1884 {
   1885 	struct fw_pkt *pkt;
   1886 	u_char *p;
   1887 	const struct tcode_info *tinfo;
   1888 	u_int res, i, len, plen;
   1889 
   1890 	rb->xfer->recv.spd = rb->spd;
   1891 
   1892 	pkt = (struct fw_pkt *)rb->vec->iov_base;
   1893 	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
   1894 
   1895 	/* Copy header */
   1896 	p = (u_char *)&rb->xfer->recv.hdr;
   1897 	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
   1898 	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
   1899 	rb->vec->iov_len -= tinfo->hdr_len;
   1900 
   1901 	/* Copy payload */
   1902 	p = (u_char *)rb->xfer->recv.payload;
   1903 	res = rb->xfer->recv.pay_len;
   1904 
   1905 	/* special handling for RRESQ */
   1906 	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
   1907 	    p != NULL && res >= sizeof(uint32_t)) {
   1908 		*(uint32_t *)p = pkt->mode.rresq.data;
   1909 		rb->xfer->recv.pay_len = sizeof(uint32_t);
   1910 		return;
   1911 	}
   1912 
   1913 	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
   1914 		return;
   1915 
   1916 	plen = pkt->mode.rresb.len;
   1917 
   1918 	for (i = 0; i < rb->nvec; i++, rb->vec++) {
   1919 		len = MIN(rb->vec->iov_len, plen);
   1920 		if (res < len) {
   1921 			printf("rcv buffer(%d) is %d bytes short.\n",
   1922 			    rb->xfer->recv.pay_len, len - res);
   1923 			len = res;
   1924 		}
   1925 		if (p) {
   1926 			bcopy(rb->vec->iov_base, p, len);
   1927 			p += len;
   1928 		}
   1929 		res -= len;
   1930 		plen -= len;
   1931 		if (res == 0 || plen == 0)
   1932 			break;
   1933 	}
   1934 	rb->xfer->recv.pay_len -= res;
   1935 
   1936 }
   1937 
   1938 /*
   1939  * Generic packet receiving process.
   1940  */
   1941 void
   1942 fw_rcv(struct fw_rcv_buf *rb)
   1943 {
   1944 	struct fw_pkt *fp, *resfp;
   1945 	struct fw_bind *bind;
   1946 	int tcode;
   1947 	int i, len, oldstate;
   1948 #if 0
   1949 	{
   1950 		uint32_t *qld;
   1951 		int i;
   1952 		qld = (uint32_t *)buf;
   1953 		printf("spd %d len:%d\n", spd, len);
   1954 		for( i = 0 ; i <= len && i < 32; i+= 4){
   1955 			printf("0x%08x ", ntohl(qld[i/4]));
   1956 			if((i % 16) == 15) printf("\n");
   1957 		}
   1958 		if((i % 16) != 15) printf("\n");
   1959 	}
   1960 #endif
   1961 	fp = (struct fw_pkt *)rb->vec[0].iov_base;
   1962 	tcode = fp->mode.common.tcode;
   1963 	switch (tcode) {
   1964 	case FWTCODE_WRES:
   1965 	case FWTCODE_RRESQ:
   1966 	case FWTCODE_RRESB:
   1967 	case FWTCODE_LRES:
   1968 		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
   1969 		    fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode);
   1970 		if(rb->xfer == NULL) {
   1971 			printf("fw_rcv: unknown response "
   1972 			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
   1973 			    tcode_str[tcode], tcode,
   1974 			    fp->mode.hdr.src,
   1975 			    fp->mode.hdr.tlrt >> 2,
   1976 			    fp->mode.hdr.tlrt & 3,
   1977 			    fp->mode.rresq.data);
   1978 #if 0
   1979 			printf("try ad-hoc work around!!\n");
   1980 			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
   1981 					(fp->mode.hdr.tlrt >> 2)^3);
   1982 			if (rb->xfer == NULL) {
   1983 				printf("no use...\n");
   1984 				return;
   1985 			}
   1986 #else
   1987 			return;
   1988 #endif
   1989 		}
   1990 		fw_rcv_copy(rb);
   1991 		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
   1992 			rb->xfer->resp = EIO;
   1993 		else
   1994 			rb->xfer->resp = 0;
   1995 		/* make sure the packet is drained in AT queue */
   1996 		oldstate = rb->xfer->flag;
   1997 		rb->xfer->flag = FWXF_RCVD;
   1998 		switch (oldstate) {
   1999 		case FWXF_SENT:
   2000 			fw_xfer_done(rb->xfer);
   2001 			break;
   2002 		case FWXF_START:
   2003 #if 0
   2004 			if (firewire_debug)
   2005 				printf("not sent yet tl=%x\n", rb->xfer->tl);
   2006 #endif
   2007 			break;
   2008 		default:
   2009 			printf("unexpected flag 0x%02x\n", rb->xfer->flag);
   2010 		}
   2011 		return;
   2012 	case FWTCODE_WREQQ:
   2013 	case FWTCODE_WREQB:
   2014 	case FWTCODE_RREQQ:
   2015 	case FWTCODE_RREQB:
   2016 	case FWTCODE_LREQ:
   2017 		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
   2018 			fp->mode.rreqq.dest_lo);
   2019 		if(bind == NULL){
   2020 #if 1
   2021 			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
   2022 #if defined(__DragonFly__) || \
   2023     (defined(__FreeBSD__) && __FreeBSD_version < 500000)
   2024 			    " src=0x%x data=%lx\n",
   2025 #else
   2026 			    " src=0x%x data=%x\n",
   2027 #endif
   2028 			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
   2029 			    tcode_str[tcode], tcode,
   2030 			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
   2031 #endif
   2032 			if (rb->fc->status == FWBUSINIT) {
   2033 				printf("fw_rcv: cannot respond(bus reset)!\n");
   2034 				return;
   2035 			}
   2036 			rb->xfer = fw_xfer_alloc(M_FWXFER);
   2037 			if(rb->xfer == NULL){
   2038 				return;
   2039 			}
   2040 			rb->xfer->send.spd = rb->spd;
   2041 			rb->xfer->send.pay_len = 0;
   2042 			resfp = &rb->xfer->send.hdr;
   2043 			switch (tcode) {
   2044 			case FWTCODE_WREQQ:
   2045 			case FWTCODE_WREQB:
   2046 				resfp->mode.hdr.tcode = FWTCODE_WRES;
   2047 				break;
   2048 			case FWTCODE_RREQQ:
   2049 				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
   2050 				break;
   2051 			case FWTCODE_RREQB:
   2052 				resfp->mode.hdr.tcode = FWTCODE_RRESB;
   2053 				break;
   2054 			case FWTCODE_LREQ:
   2055 				resfp->mode.hdr.tcode = FWTCODE_LRES;
   2056 				break;
   2057 			}
   2058 			resfp->mode.hdr.dst = fp->mode.hdr.src;
   2059 			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
   2060 			resfp->mode.hdr.pri = fp->mode.hdr.pri;
   2061 			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
   2062 			resfp->mode.rresb.extcode = 0;
   2063 			resfp->mode.rresb.len = 0;
   2064 /*
   2065 			rb->xfer->hand = fw_xferwake;
   2066 */
   2067 			rb->xfer->hand = fw_xfer_free;
   2068 			if(fw_asyreq(rb->fc, -1, rb->xfer)){
   2069 				fw_xfer_free(rb->xfer);
   2070 				return;
   2071 			}
   2072 			return;
   2073 		}
   2074 		len = 0;
   2075 		for (i = 0; i < rb->nvec; i ++)
   2076 			len += rb->vec[i].iov_len;
   2077 		rb->xfer = STAILQ_FIRST(&bind->xferlist);
   2078 		if (rb->xfer == NULL) {
   2079 #if 1
   2080 			printf("Discard a packet for this bind.\n");
   2081 #endif
   2082 			return;
   2083 		}
   2084 		STAILQ_REMOVE_HEAD(&bind->xferlist, link);
   2085 		fw_rcv_copy(rb);
   2086 		rb->xfer->hand(rb->xfer);
   2087 		return;
   2088 #if 0 /* shouldn't happen ?? or for GASP */
   2089 	case FWTCODE_STREAM:
   2090 	{
   2091 		struct fw_xferq *xferq;
   2092 
   2093 		xferq = rb->fc->ir[sub];
   2094 #if 0
   2095 		printf("stream rcv dma %d len %d off %d spd %d\n",
   2096 			sub, len, off, spd);
   2097 #endif
   2098 		if(xferq->queued >= xferq->maxq) {
   2099 			printf("receive queue is full\n");
   2100 			return;
   2101 		}
   2102 		/* XXX get xfer from xfer queue, we don't need copy for
   2103 			per packet mode */
   2104 		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
   2105 						vec[0].iov_len);
   2106 		if (rb->xfer == NULL)
   2107 			return;
   2108 		fw_rcv_copy(rb)
   2109 		s = splfw();
   2110 		xferq->queued++;
   2111 		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
   2112 		splx(s);
   2113 		sc = device_get_softc(rb->fc->bdev);
   2114 #if defined(__DragonFly__) || \
   2115     (defined(__FreeBSD__) && __FreeBSD_version < 500000)
   2116 		if (&xferq->rsel.si_pid != 0)
   2117 #else
   2118 		if (SEL_WAITING(&xferq->rsel))
   2119 #endif
   2120 			selwakeuppri(&xferq->rsel, FWPRI);
   2121 		if (xferq->flag & FWXFERQ_WAKEUP) {
   2122 			xferq->flag &= ~FWXFERQ_WAKEUP;
   2123 			wakeup((void *)xferq);
   2124 		}
   2125 		if (xferq->flag & FWXFERQ_HANDLER) {
   2126 			xferq->hand(xferq);
   2127 		}
   2128 		return;
   2129 		break;
   2130 	}
   2131 #endif
   2132 	default:
   2133 		printf("fw_rcv: unknow tcode %d\n", tcode);
   2134 		break;
   2135 	}
   2136 }
   2137 
   2138 /*
   2139  * Post process for Bus Manager election process.
   2140  */
   2141 static void
   2142 fw_try_bmr_callback(struct fw_xfer *xfer)
   2143 {
   2144 	struct firewire_comm *fc;
   2145 	int bmr;
   2146 
   2147 	if (xfer == NULL)
   2148 		return;
   2149 	fc = xfer->fc;
   2150 	if (xfer->resp != 0)
   2151 		goto error;
   2152 	if (xfer->recv.payload == NULL)
   2153 		goto error;
   2154 	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
   2155 		goto error;
   2156 
   2157 	bmr = ntohl(xfer->recv.payload[0]);
   2158 	if (bmr == 0x3f)
   2159 		bmr = fc->nodeid;
   2160 
   2161 	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
   2162 	fw_xfer_free_buf(xfer);
   2163 	fw_bmr(fc);
   2164 	return;
   2165 
   2166 error:
   2167 	fw_printf(fc->bdev, "bus manager election failed\n");
   2168 	fw_xfer_free_buf(xfer);
   2169 }
   2170 
   2171 
   2172 /*
   2173  * To candidate Bus Manager election process.
   2174  */
   2175 static void
   2176 fw_try_bmr(void *arg)
   2177 {
   2178 	struct fw_xfer *xfer;
   2179 	struct firewire_comm *fc = (struct firewire_comm *)arg;
   2180 	struct fw_pkt *fp;
   2181 	int err = 0;
   2182 
   2183 	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
   2184 	if(xfer == NULL){
   2185 		return;
   2186 	}
   2187 	xfer->send.spd = 0;
   2188 	fc->status = FWBUSMGRELECT;
   2189 
   2190 	fp = &xfer->send.hdr;
   2191 	fp->mode.lreq.dest_hi = 0xffff;
   2192 	fp->mode.lreq.tlrt = 0;
   2193 	fp->mode.lreq.tcode = FWTCODE_LREQ;
   2194 	fp->mode.lreq.pri = 0;
   2195 	fp->mode.lreq.src = 0;
   2196 	fp->mode.lreq.len = 8;
   2197 	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
   2198 	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
   2199 	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
   2200 	xfer->send.payload[0] = htonl(0x3f);
   2201 	xfer->send.payload[1] = htonl(fc->nodeid);
   2202 	xfer->hand = fw_try_bmr_callback;
   2203 
   2204 	err = fw_asyreq(fc, -1, xfer);
   2205 	if(err){
   2206 		fw_xfer_free_buf(xfer);
   2207 		return;
   2208 	}
   2209 	return;
   2210 }
   2211 
   2212 #ifdef FW_VMACCESS
   2213 /*
   2214  * Software implementation for physical memory block access.
   2215  * XXX:Too slow, usef for debug purpose only.
   2216  */
   2217 static void
   2218 fw_vmaccess(struct fw_xfer *xfer){
   2219 	struct fw_pkt *rfp, *sfp = NULL;
   2220 	uint32_t *ld = (uint32_t *)xfer->recv.buf;
   2221 
   2222 	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
   2223 			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
   2224 	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
   2225 	if(xfer->resp != 0){
   2226 		fw_xfer_free( xfer);
   2227 		return;
   2228 	}
   2229 	if(xfer->recv.buf == NULL){
   2230 		fw_xfer_free( xfer);
   2231 		return;
   2232 	}
   2233 	rfp = (struct fw_pkt *)xfer->recv.buf;
   2234 	switch(rfp->mode.hdr.tcode){
   2235 		/* XXX need fix for 64bit arch */
   2236 		case FWTCODE_WREQB:
   2237 			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
   2238 			xfer->send.len = 12;
   2239 			sfp = (struct fw_pkt *)xfer->send.buf;
   2240 			bcopy(rfp->mode.wreqb.payload,
   2241 				(void *)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
   2242 			sfp->mode.wres.tcode = FWTCODE_WRES;
   2243 			sfp->mode.wres.rtcode = 0;
   2244 			break;
   2245 		case FWTCODE_WREQQ:
   2246 			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
   2247 			xfer->send.len = 12;
   2248 			sfp->mode.wres.tcode = FWTCODE_WRES;
   2249 			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
   2250 			sfp->mode.wres.rtcode = 0;
   2251 			break;
   2252 		case FWTCODE_RREQB:
   2253 			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
   2254 			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
   2255 			sfp = (struct fw_pkt *)xfer->send.buf;
   2256 			bcopy((void *)ntohl(rfp->mode.rreqb.dest_lo),
   2257 				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
   2258 			sfp->mode.rresb.tcode = FWTCODE_RRESB;
   2259 			sfp->mode.rresb.len = rfp->mode.rreqb.len;
   2260 			sfp->mode.rresb.rtcode = 0;
   2261 			sfp->mode.rresb.extcode = 0;
   2262 			break;
   2263 		case FWTCODE_RREQQ:
   2264 			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
   2265 			xfer->send.len = 16;
   2266 			sfp = (struct fw_pkt *)xfer->send.buf;
   2267 			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
   2268 			sfp->mode.wres.tcode = FWTCODE_RRESQ;
   2269 			sfp->mode.rresb.rtcode = 0;
   2270 			break;
   2271 		default:
   2272 			fw_xfer_free( xfer);
   2273 			return;
   2274 	}
   2275 	sfp->mode.hdr.dst = rfp->mode.hdr.src;
   2276 	xfer->dst = ntohs(rfp->mode.hdr.src);
   2277 	xfer->hand = fw_xfer_free;
   2278 
   2279 	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
   2280 	sfp->mode.hdr.pri = 0;
   2281 
   2282 	fw_asyreq(xfer->fc, -1, xfer);
   2283 /**/
   2284 	return;
   2285 }
   2286 #endif
   2287 
   2288 /*
   2289  * CRC16 check-sum for IEEE1394 register blocks.
   2290  */
   2291 uint16_t
   2292 fw_crc16(uint32_t *ptr, uint32_t len){
   2293 	uint32_t i, sum, crc = 0;
   2294 	int shift;
   2295 	len = (len + 3) & ~3;
   2296 	for(i = 0 ; i < len ; i+= 4){
   2297 		for( shift = 28 ; shift >= 0 ; shift -= 4){
   2298 			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
   2299 			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
   2300 		}
   2301 		crc &= 0xffff;
   2302 	}
   2303 	return((uint16_t) crc);
   2304 }
   2305 
   2306 static int
   2307 fw_bmr(struct firewire_comm *fc)
   2308 {
   2309 	struct fw_device fwdev;
   2310 	union fw_self_id *self_id;
   2311 	int cmstr;
   2312 	uint32_t quad;
   2313 
   2314 	/* Check to see if the current root node is cycle master capable */
   2315 	self_id = fw_find_self_id(fc, fc->max_node);
   2316 	if (fc->max_node > 0) {
   2317 		/* XXX check cmc bit of businfo block rather than contender */
   2318 		if (self_id->p0.link_active && self_id->p0.contender)
   2319 			cmstr = fc->max_node;
   2320 		else {
   2321 			fw_printf(fc->bdev,
   2322 				"root node is not cycle master capable\n");
   2323 			/* XXX shall we be the cycle master? */
   2324 			cmstr = fc->nodeid;
   2325 			/* XXX need bus reset */
   2326 		}
   2327 	} else
   2328 		cmstr = -1;
   2329 
   2330 	fw_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
   2331 	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
   2332 		/* We are not the bus manager */
   2333 		printf("\n");
   2334 		return(0);
   2335 	}
   2336 	printf("(me)\n");
   2337 
   2338 	/* Optimize gapcount */
   2339 	if(fc->max_hop <= MAX_GAPHOP )
   2340 		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
   2341 	/* If we are the cycle master, nothing to do */
   2342 	if (cmstr == fc->nodeid || cmstr == -1)
   2343 		return 0;
   2344 	/* Bus probe has not finished, make dummy fwdev for cmstr */
   2345 	bzero(&fwdev, sizeof(fwdev));
   2346 	fwdev.fc = fc;
   2347 	fwdev.dst = cmstr;
   2348 	fwdev.speed = 0;
   2349 	fwdev.maxrec = 8; /* 512 */
   2350 	fwdev.status = FWDEVINIT;
   2351 	/* Set cmstr bit on the cycle master */
   2352 	quad = htonl(1 << 8);
   2353 	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
   2354 		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
   2355 
   2356 	return 0;
   2357 }
   2358 
   2359 int
   2360 fw_open_isodma(struct firewire_comm *fc, int tx)
   2361 {
   2362 	struct fw_xferq **xferqa;
   2363 	struct fw_xferq *xferq;
   2364 	int i;
   2365 
   2366 	if (tx)
   2367 		xferqa = &fc->it[0];
   2368 	else
   2369 		xferqa = &fc->ir[0];
   2370 
   2371 	FW_GLOCK(fc);
   2372 	for (i = 0; i < fc->nisodma; i ++) {
   2373 		xferq = xferqa[i];
   2374 		if ((xferq->flag & FWXFERQ_OPEN) == 0) {
   2375 			xferq->flag |= FWXFERQ_OPEN;
   2376 			break;
   2377 		}
   2378 	}
   2379 	if (i == fc->nisodma) {
   2380 		printf("no free dma channel (tx=%d)\n", tx);
   2381 		i = -1;
   2382 	}
   2383 	FW_GUNLOCK(fc);
   2384 	return (i);
   2385 }
   2386 
   2387 #if defined(__FreeBSD__)
   2388 static int
   2389 fw_modevent(module_t mode, int type, void *data)
   2390 {
   2391 	int err = 0;
   2392 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
   2393 	static eventhandler_tag fwdev_ehtag = NULL;
   2394 #endif
   2395 
   2396 	switch (type) {
   2397 	case MOD_LOAD:
   2398 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
   2399 		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
   2400 						fwdev_clone, 0, 1000);
   2401 #endif
   2402 		break;
   2403 	case MOD_UNLOAD:
   2404 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
   2405 		if (fwdev_ehtag != NULL)
   2406 			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
   2407 #endif
   2408 		break;
   2409 	case MOD_SHUTDOWN:
   2410 		break;
   2411 	default:
   2412 		return (EOPNOTSUPP);
   2413 	}
   2414 	return (err);
   2415 }
   2416 
   2417 
   2418 #ifdef __DragonFly__
   2419 DECLARE_DUMMY_MODULE(firewire);
   2420 #endif
   2421 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
   2422 MODULE_VERSION(firewire, 1);
   2423 #endif
   2424