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