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