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