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