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