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