xref: /freebsd/sys/dev/firewire/firewire.c (revision a163d034fadcfb4a25ca34a2ba5f491c47b6ff69)
1 /*
2  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the acknowledgement as bellow:
15  *
16  *    This product includes software developed by K. Kobayashi and H. Shimokawa
17  *
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  *
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/conf.h>
47 #include <sys/uio.h>
48 #include <sys/sysctl.h>
49 
50 #include <machine/cpufunc.h>    /* for rdtsc proto for clock.h below */
51 #include <machine/clock.h>
52 
53 #include <sys/bus.h>		/* used by smbus and newbus */
54 
55 #include <dev/firewire/firewire.h>
56 #include <dev/firewire/firewirereg.h>
57 #include <dev/firewire/fwmem.h>
58 #include <dev/firewire/iec13213.h>
59 #include <dev/firewire/iec68113.h>
60 
61 int firewire_debug=0, try_bmr=1;
62 SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
63 	"FireWire driver debug flag");
64 SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
65 SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
66 	"Try to be a bus manager");
67 
68 MALLOC_DEFINE(M_FW, "firewire", "FireWire");
69 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
70 
71 #define FW_MAXASYRTY 4
72 #define FW_MAXDEVRCNT 4
73 
74 #define XFER_TIMEOUT 0
75 
76 devclass_t firewire_devclass;
77 
78 static int firewire_match      __P((device_t));
79 static int firewire_attach      __P((device_t));
80 static int firewire_detach      __P((device_t));
81 #if 0
82 static int firewire_shutdown    __P((device_t));
83 #endif
84 static device_t firewire_add_child   __P((device_t, int, const char *, int));
85 static void fw_try_bmr __P((void *));
86 static void fw_try_bmr_callback __P((struct fw_xfer *));
87 static void fw_asystart __P((struct fw_xfer *));
88 static int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
89 static void fw_bus_probe __P((struct firewire_comm *));
90 static void fw_bus_explore __P((struct firewire_comm *));
91 static void fw_bus_explore_callback __P((struct fw_xfer *));
92 static void fw_attach_dev __P((struct firewire_comm *));
93 #ifdef FW_VMACCESS
94 static void fw_vmaccess __P((struct fw_xfer *));
95 #endif
96 struct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
97 	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
98 static int fw_bmr __P((struct firewire_comm *));
99 
100 static device_method_t firewire_methods[] = {
101 	/* Device interface */
102 	DEVMETHOD(device_probe,		firewire_match),
103 	DEVMETHOD(device_attach,	firewire_attach),
104 	DEVMETHOD(device_detach,	firewire_detach),
105 	DEVMETHOD(device_suspend,	bus_generic_suspend),
106 	DEVMETHOD(device_resume,	bus_generic_resume),
107 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
108 
109 	/* Bus interface */
110 	DEVMETHOD(bus_add_child,	firewire_add_child),
111 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
112 
113 	{ 0, 0 }
114 };
115 char linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
116 
117 #define MAX_GAPHOP  16
118 u_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
119 			20, 23, 25, 28, 31, 33, 36, 39, 42};
120 
121 extern struct cdevsw firewire_cdevsw;
122 
123 static driver_t firewire_driver = {
124 	"firewire",
125 	firewire_methods,
126 	sizeof(struct firewire_softc),
127 };
128 
129 /*
130  * Lookup fwdev by node id.
131  */
132 struct fw_device *
133 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
134 {
135 	struct fw_device *fwdev;
136 	int s;
137 
138 	s = splfw();
139 	STAILQ_FOREACH(fwdev, &fc->devices, link)
140 		if (fwdev->dst == dst)
141 			break;
142 	splx(s);
143 
144 	if(fwdev == NULL) return NULL;
145 	if(fwdev->status == FWDEVINVAL) return NULL;
146 	return fwdev;
147 }
148 
149 /*
150  * Lookup fwdev by EUI64.
151  */
152 struct fw_device *
153 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
154 {
155 	struct fw_device *fwdev;
156 	int s;
157 
158 	s = splfw();
159 	STAILQ_FOREACH(fwdev, &fc->devices, link)
160 		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
161 			break;
162 	splx(s);
163 
164 	if(fwdev == NULL) return NULL;
165 	if(fwdev->status == FWDEVINVAL) return NULL;
166 	return fwdev;
167 }
168 
169 /*
170  * Async. request procedure for userland application.
171  */
172 int
173 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
174 {
175 	int err = 0;
176 	struct fw_xferq *xferq;
177 	int tl = 0, len;
178 	struct fw_pkt *fp;
179 	int tcode;
180 	struct tcode_info *info;
181 
182 	if(xfer == NULL) return EINVAL;
183 	if(xfer->send.len > MAXREC(fc->maxrec)){
184 		printf("send.len > maxrec\n");
185 		return EINVAL;
186 	}
187 	if(xfer->act.hand == NULL){
188 		printf("act.hand == NULL\n");
189 		return EINVAL;
190 	}
191 	fp = (struct fw_pkt *)xfer->send.buf;
192 
193 	tcode = fp->mode.common.tcode & 0xf;
194 	info = &fc->tcode[tcode];
195 	if (info->flag == 0) {
196 		printf("invalid tcode=%d\n", tcode);
197 		return EINVAL;
198 	}
199 	if (info->flag & FWTI_REQ)
200 		xferq = fc->atq;
201 	else
202 		xferq = fc->ats;
203 	len = info->hdr_len;
204 	if (info->flag & FWTI_BLOCK_STR)
205 		len += ntohs(fp->mode.stream.len);
206 	else if (info->flag & FWTI_BLOCK_ASY)
207 		len += ntohs(fp->mode.rresb.len);
208 	if( len >  xfer->send.len ){
209 		printf("len(%d) > send.len(%d) (tcode=%d)\n",
210 				len, xfer->send.len, tcode);
211 		return EINVAL;
212 	}
213 	xfer->send.len = len;
214 
215 	if(xferq->start == NULL){
216 		printf("xferq->start == NULL\n");
217 		return EINVAL;
218 	}
219 	if(!(xferq->queued < xferq->maxq)){
220 		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
221 			xferq->queued);
222 		return EINVAL;
223 	}
224 
225 
226 	if (info->flag & FWTI_TLABEL) {
227 		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
228 			return EIO;
229 		fp->mode.hdr.tlrt = tl << 2;
230 	}
231 
232 	xfer->tl = tl;
233 	xfer->tcode = tcode;
234 	xfer->resp = 0;
235 	xfer->fc = fc;
236 	xfer->q = xferq;
237 	xfer->act_type = FWACT_XFER;
238 	xfer->retry_req = fw_asybusy;
239 
240 	fw_asystart(xfer);
241 	return err;
242 }
243 /*
244  * Wakeup blocked process.
245  */
246 void
247 fw_asy_callback(struct fw_xfer *xfer){
248 	wakeup(xfer);
249 	return;
250 }
251 /*
252  * Postpone to later retry.
253  */
254 void fw_asybusy(struct fw_xfer *xfer){
255 #if 1
256 	printf("fw_asybusy\n");
257 #endif
258 #if XFER_TIMEOUT
259 	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
260 #endif
261 /*
262 	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
263 */
264 	DELAY(20000);
265 	fw_asystart(xfer);
266 	return;
267 }
268 #if XFER_TIMEOUT
269 /*
270  * Post timeout for async. request.
271  */
272 void
273 fw_xfer_timeout(void *arg)
274 {
275 	int s;
276 	struct fw_xfer *xfer;
277 
278 	xfer = (struct fw_xfer *)arg;
279 	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
280 	/* XXX set error code */
281 	s = splfw();
282 	xfer->act.hand(xfer);
283 	splx(s);
284 }
285 #endif
286 /*
287  * Async. request with given xfer structure.
288  */
289 static void
290 fw_asystart(struct fw_xfer *xfer)
291 {
292 	struct firewire_comm *fc = xfer->fc;
293 	int s;
294 	if(xfer->retry++ >= fc->max_asyretry){
295 		xfer->resp = EBUSY;
296 		xfer->state = FWXF_BUSY;
297 		xfer->act.hand(xfer);
298 		return;
299 	}
300 #if 0 /* XXX allow bus explore packets only after bus rest */
301 	if (fc->status < FWBUSEXPLORE) {
302 		xfer->resp = EAGAIN;
303 		xfer->state = FWXF_BUSY;
304 		if (xfer->act.hand != NULL)
305 			xfer->act.hand(xfer);
306 		return;
307 	}
308 #endif
309 	s = splfw();
310 	xfer->state = FWXF_INQ;
311 	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
312 	xfer->q->queued ++;
313 	splx(s);
314 	/* XXX just queue for mbuf */
315 	if (xfer->mbuf == NULL)
316 		xfer->q->start(fc);
317 #if XFER_TIMEOUT
318 	if (xfer->act.hand != NULL)
319 		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
320 #endif
321 	return;
322 }
323 
324 static int
325 firewire_match( device_t dev )
326 {
327 	device_set_desc(dev, "IEEE1394(FireWire) bus");
328 	return -140;
329 }
330 
331 static void
332 firewire_xfer_timeout(struct firewire_comm *fc)
333 {
334 	struct fw_xfer *xfer;
335 	struct tlabel *tl;
336 	struct timeval tv;
337 	struct timeval split_timeout;
338 	int i, s;
339 
340 	split_timeout.tv_sec = 6;
341 	split_timeout.tv_usec = 0;
342 
343 	microtime(&tv);
344 	timevalsub(&tv, &split_timeout);
345 
346 	s = splfw();
347 	for (i = 0; i < 0x40; i ++) {
348 		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
349 			xfer = tl->xfer;
350 			if (timevalcmp(&xfer->tv, &tv, >))
351 				/* the rests are newer than this */
352 				break;
353 			device_printf(fc->bdev,
354 				"split transaction timeout dst=0x%x tl=0x%x\n",
355 				xfer->dst, i);
356 			xfer->resp = ETIMEDOUT;
357 			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
358 			switch (xfer->act_type) {
359 			case FWACT_XFER:
360 				fw_xfer_done(xfer);
361 				break;
362 			default:
363 				/* ??? */
364 				fw_xfer_free(xfer);
365 				break;
366 			}
367 		}
368 	}
369 	splx(s);
370 }
371 
372 static void
373 firewire_watchdog(void *arg)
374 {
375 	struct firewire_comm *fc;
376 
377 	fc = (struct firewire_comm *)arg;
378 	firewire_xfer_timeout(fc);
379 	fc->timeout(fc);
380 	callout_reset(&fc->timeout_callout, hz,
381 			(void *)firewire_watchdog, (void *)fc);
382 }
383 
384 /*
385  * The attach routine.
386  */
387 static int
388 firewire_attach( device_t dev )
389 {
390 	int i, unitmask, mn;
391 	struct firewire_softc *sc = device_get_softc(dev);
392 	device_t pa = device_get_parent(dev);
393 	struct firewire_comm *fc;
394 	dev_t d;
395 
396 	fc = (struct firewire_comm *)device_get_softc(pa);
397 	sc->fc = fc;
398 	fc->status = -1;
399 
400 	unitmask = UNIT2MIN(device_get_unit(dev));
401 
402 	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
403 	for ( i = 0 ; i < fc->nisodma ; i++ ){
404 		mn = unitmask | i;
405 		/* XXX device name should be improved */
406 		d = make_dev(&firewire_cdevsw, unit2minor(mn),
407 			UID_ROOT, GID_OPERATOR, 0660,
408 			"fw%x", mn);
409 #if __FreeBSD_version >= 500000
410 		if (i == 0)
411 			sc->dev = d;
412 		else
413 			dev_depends(sc->dev, d);
414 #else
415 		sc->dev[i] = d;
416 #endif
417 	}
418 	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
419 			UID_ROOT, GID_OPERATOR, 0660,
420 			"fwmem%d", device_get_unit(dev));
421 #if __FreeBSD_version >= 500000
422 	dev_depends(sc->dev, d);
423 #else
424 	sc->dev[i] = d;
425 #endif
426 #if __FreeBSD_version >= 500000
427 #define CALLOUT_INIT(x) callout_init(x, 0 /* mpsafe */)
428 #else
429 #define CALLOUT_INIT(x) callout_init(x)
430 #endif
431 	CALLOUT_INIT(&sc->fc->timeout_callout);
432 	CALLOUT_INIT(&sc->fc->bmr_callout);
433 	CALLOUT_INIT(&sc->fc->retry_probe_callout);
434 	CALLOUT_INIT(&sc->fc->busprobe_callout);
435 
436 	callout_reset(&sc->fc->timeout_callout, hz,
437 			(void *)firewire_watchdog, (void *)sc->fc);
438 
439 	/* Locate our children */
440 	bus_generic_probe(dev);
441 
442 	/* launch attachement of the added children */
443 	bus_generic_attach(dev);
444 
445 #if 1
446 	/* bus_reset */
447 	fc->ibr(fc);
448 #endif
449 
450 	return 0;
451 }
452 
453 /*
454  * Attach it as child.
455  */
456 static device_t
457 firewire_add_child(device_t dev, int order, const char *name, int unit)
458 {
459         device_t child;
460 	struct firewire_softc *sc;
461 
462 	sc = (struct firewire_softc *)device_get_softc(dev);
463 	child = device_add_child(dev, name, unit);
464 	if (child) {
465 		device_set_ivars(child, sc->fc);
466 		device_probe_and_attach(child);
467 	}
468 
469 	return child;
470 }
471 
472 /*
473  * Dettach it.
474  */
475 static int
476 firewire_detach( device_t dev )
477 {
478 	struct firewire_softc *sc;
479 	struct csrdir *csrd, *next;
480 	struct fw_device *fwdev, *fwdev_next;
481 
482 	sc = (struct firewire_softc *)device_get_softc(dev);
483 
484 	bus_generic_detach(dev);
485 
486 	callout_stop(&sc->fc->timeout_callout);
487 	callout_stop(&sc->fc->bmr_callout);
488 	callout_stop(&sc->fc->retry_probe_callout);
489 	callout_stop(&sc->fc->busprobe_callout);
490 
491 #if __FreeBSD_version >= 500000
492 	destroy_dev(sc->dev);
493 #else
494 	{
495 		int j;
496 		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
497 			destroy_dev(sc->dev[j]);
498 	}
499 #endif
500 	/* XXX xfree_free and untimeout on all xfers */
501 	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
502 							fwdev = fwdev_next) {
503 		fwdev_next = STAILQ_NEXT(fwdev, link);
504 		free(fwdev, M_FW);
505 	}
506 	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
507 		next = SLIST_NEXT(csrd, link);
508 		free(csrd, M_FW);
509 	}
510 	free(sc->fc->topology_map, M_FW);
511 	free(sc->fc->speed_map, M_FW);
512 	return(0);
513 }
514 #if 0
515 static int
516 firewire_shutdown( device_t dev )
517 {
518 	return 0;
519 }
520 #endif
521 
522 
523 static void
524 fw_xferq_drain(struct fw_xferq *xferq)
525 {
526 	struct fw_xfer *xfer;
527 
528 	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
529 		STAILQ_REMOVE_HEAD(&xferq->q, link);
530 		xfer->resp = EAGAIN;
531 		switch (xfer->act_type) {
532 		case FWACT_XFER:
533 			fw_xfer_done(xfer);
534 			break;
535 		default:
536 			/* ??? */
537 			fw_xfer_free(xfer);
538 			break;
539 		}
540 	}
541 }
542 
543 void
544 fw_drain_txq(struct firewire_comm *fc)
545 {
546 	int i;
547 
548 	fw_xferq_drain(fc->atq);
549 	fw_xferq_drain(fc->ats);
550 	for(i = 0; i < fc->nisodma; i++)
551 		fw_xferq_drain(fc->it[i]);
552 }
553 
554 /*
555  * Called after bus reset.
556  */
557 void
558 fw_busreset(struct firewire_comm *fc)
559 {
560 	int i;
561 
562 	switch(fc->status){
563 	case FWBUSMGRELECT:
564 		callout_stop(&fc->bmr_callout);
565 		break;
566 	default:
567 		break;
568 	}
569 	fc->status = FWBUSRESET;
570 	CSRARC(fc, STATE_CLEAR)
571 			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
572 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
573 	CSRARC(fc, NODE_IDS) = 0x3f;
574 
575 	CSRARC(fc, TOPO_MAP + 8) = 0;
576 	fc->irm = -1;
577 
578 	fc->max_node = -1;
579 
580 	for(i = 2; i < 0x100/4 - 2 ; i++){
581 		CSRARC(fc, SPED_MAP + i * 4) = 0;
582 	}
583 	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
584 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
585 	CSRARC(fc, RESET_START) = 0;
586 	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
587 	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
588 	CSRARC(fc, CYCLE_TIME) = 0x0;
589 	CSRARC(fc, BUS_TIME) = 0x0;
590 	CSRARC(fc, BUS_MGR_ID) = 0x3f;
591 	CSRARC(fc, BANDWIDTH_AV) = 4915;
592 	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
593 	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
594 	CSRARC(fc, IP_CHANNELS) = (1 << 31);
595 
596 	CSRARC(fc, CONF_ROM) = 0x04 << 24;
597 	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
598 	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
599 				1 << 28 | 0xff << 16 | 0x09 << 8;
600 	CSRARC(fc, CONF_ROM + 0xc) = 0;
601 
602 /* DV depend CSRs see blue book */
603 	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
604 	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
605 
606 	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
607 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
608 }
609 
610 /* Call once after reboot */
611 void fw_init(struct firewire_comm *fc)
612 {
613 	int i;
614 	struct csrdir *csrd;
615 #ifdef FW_VMACCESS
616 	struct fw_xfer *xfer;
617 	struct fw_bind *fwb;
618 #endif
619 
620 	fc->max_asyretry = FW_MAXASYRTY;
621 
622 	fc->arq->queued = 0;
623 	fc->ars->queued = 0;
624 	fc->atq->queued = 0;
625 	fc->ats->queued = 0;
626 
627 	fc->arq->psize = PAGE_SIZE;
628 	fc->ars->psize = PAGE_SIZE;
629 	fc->atq->psize = 0;
630 	fc->ats->psize = 0;
631 
632 
633 	fc->arq->buf = NULL;
634 	fc->ars->buf = NULL;
635 	fc->atq->buf = NULL;
636 	fc->ats->buf = NULL;
637 
638 	fc->arq->flag = FWXFERQ_PACKET;
639 	fc->ars->flag = FWXFERQ_PACKET;
640 	fc->atq->flag = FWXFERQ_PACKET;
641 	fc->ats->flag = FWXFERQ_PACKET;
642 
643 	STAILQ_INIT(&fc->atq->q);
644 	STAILQ_INIT(&fc->ats->q);
645 
646 	for( i = 0 ; i < fc->nisodma ; i ++ ){
647 		fc->it[i]->queued = 0;
648 		fc->ir[i]->queued = 0;
649 
650 		fc->it[i]->start = NULL;
651 		fc->ir[i]->start = NULL;
652 
653 		fc->it[i]->buf = NULL;
654 		fc->ir[i]->buf = NULL;
655 
656 		fc->it[i]->flag = FWXFERQ_STREAM;
657 		fc->ir[i]->flag = FWXFERQ_STREAM;
658 
659 		STAILQ_INIT(&fc->it[i]->q);
660 		STAILQ_INIT(&fc->ir[i]->q);
661 
662 		STAILQ_INIT(&fc->it[i]->binds);
663 		STAILQ_INIT(&fc->ir[i]->binds);
664 	}
665 
666 	fc->arq->maxq = FWMAXQUEUE;
667 	fc->ars->maxq = FWMAXQUEUE;
668 	fc->atq->maxq = FWMAXQUEUE;
669 	fc->ats->maxq = FWMAXQUEUE;
670 
671 	for( i = 0 ; i < fc->nisodma ; i++){
672 		fc->ir[i]->maxq = FWMAXQUEUE;
673 		fc->it[i]->maxq = FWMAXQUEUE;
674 	}
675 /* Initialize csr registers */
676 	fc->topology_map = (struct fw_topology_map *)malloc(
677 				sizeof(struct fw_topology_map),
678 				M_FW, M_NOWAIT | M_ZERO);
679 	fc->speed_map = (struct fw_speed_map *)malloc(
680 				sizeof(struct fw_speed_map),
681 				M_FW, M_NOWAIT | M_ZERO);
682 	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
683 	CSRARC(fc, TOPO_MAP + 4) = 1;
684 	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
685 	CSRARC(fc, SPED_MAP + 4) = 1;
686 
687 	STAILQ_INIT(&fc->devices);
688 	STAILQ_INIT(&fc->pending);
689 
690 /* Initialize csr ROM work space */
691 	SLIST_INIT(&fc->ongocsr);
692 	SLIST_INIT(&fc->csrfree);
693 	for( i = 0 ; i < FWMAXCSRDIR ; i++){
694 		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
695 		if(csrd == NULL) break;
696 		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
697 	}
698 
699 /* Initialize Async handlers */
700 	STAILQ_INIT(&fc->binds);
701 	for( i = 0 ; i < 0x40 ; i++){
702 		STAILQ_INIT(&fc->tlabels[i]);
703 	}
704 
705 /* DV depend CSRs see blue book */
706 #if 0
707 	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
708 	CSRARC(fc, oPCR) = 0x8000007a;
709 	for(i = 4 ; i < 0x7c/4 ; i+=4){
710 		CSRARC(fc, i + oPCR) = 0x8000007a;
711 	}
712 
713 	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
714 	CSRARC(fc, iPCR) = 0x803f0000;
715 	for(i = 4 ; i < 0x7c/4 ; i+=4){
716 		CSRARC(fc, i + iPCR) = 0x0;
717 	}
718 #endif
719 
720 
721 #ifdef FW_VMACCESS
722 	xfer = fw_xfer_alloc();
723 	if(xfer == NULL) return;
724 
725 	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
726 	if(fwb == NULL){
727 		fw_xfer_free(xfer);
728 	}
729 	xfer->act.hand = fw_vmaccess;
730 	xfer->act_type = FWACT_XFER;
731 	xfer->fc = fc;
732 	xfer->sc = NULL;
733 
734 	fwb->start_hi = 0x2;
735 	fwb->start_lo = 0;
736 	fwb->addrlen = 0xffffffff;
737 	fwb->xfer = xfer;
738 	fw_bindadd(fc, fwb);
739 #endif
740 }
741 
742 /*
743  * To lookup binded process from IEEE1394 address.
744  */
745 struct fw_bind *
746 fw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
747 {
748 	struct fw_bind *tfw;
749 	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
750 		tfw = STAILQ_NEXT(tfw, fclist)){
751 		if(tfw->xfer->act_type != FWACT_NULL &&
752 			tfw->start_hi == dest_hi &&
753 			tfw->start_lo <= dest_lo &&
754 			(tfw->start_lo + tfw->addrlen) > dest_lo){
755 			return(tfw);
756 		}
757 	}
758 	return(NULL);
759 }
760 
761 /*
762  * To bind IEEE1394 address block to process.
763  */
764 int
765 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
766 {
767 	struct fw_bind *tfw, *tfw2 = NULL;
768 	int err = 0;
769 	tfw = STAILQ_FIRST(&fc->binds);
770 	if(tfw == NULL){
771 		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
772 		goto out;
773 	}
774 	if((tfw->start_hi > fwb->start_hi) ||
775 		(tfw->start_hi == fwb->start_hi &&
776 		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
777 		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
778 		goto out;
779 	}
780 	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
781 		if((tfw->start_hi < fwb->start_hi) ||
782 		   (tfw->start_hi == fwb->start_hi &&
783 		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
784 		   tfw2 = STAILQ_NEXT(tfw, fclist);
785 			if(tfw2 == NULL)
786 				break;
787 			if((tfw2->start_hi > fwb->start_hi) ||
788 			   (tfw2->start_hi == fwb->start_hi &&
789 			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
790 				break;
791 			}else{
792 				err = EBUSY;
793 				goto out;
794 			}
795 		}
796 	}
797 	if(tfw != NULL){
798 		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
799 	}else{
800 		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
801 	}
802 out:
803 	if(!err && fwb->xfer->act_type == FWACT_CH){
804 		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
805 	}
806 	return err;
807 }
808 
809 /*
810  * To free IEEE1394 address block.
811  */
812 int
813 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
814 {
815 	int s;
816 
817 	s = splfw();
818 	/* shall we check the existance? */
819 	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
820 	splx(s);
821 	if (fwb->xfer)
822 		fw_xfer_free(fwb->xfer);
823 
824 	return 0;
825 }
826 
827 /*
828  * To free transaction label.
829  */
830 static void
831 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
832 {
833 	struct tlabel *tl;
834 	int s = splfw();
835 
836 	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
837 		tl = STAILQ_NEXT(tl, link)){
838 		if(tl->xfer == xfer){
839 			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
840 			free(tl, M_FW);
841 			splx(s);
842 			return;
843 		}
844 	}
845 	splx(s);
846 	return;
847 }
848 
849 /*
850  * To obtain XFER structure by transaction label.
851  */
852 static struct fw_xfer *
853 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
854 {
855 	struct fw_xfer *xfer;
856 	struct tlabel *tl;
857 	int s = splfw();
858 
859 	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
860 		tl = STAILQ_NEXT(tl, link)){
861 		if(tl->xfer->dst == node){
862 			xfer = tl->xfer;
863 			splx(s);
864 			if (firewire_debug > 2)
865 				printf("fw_tl2xfer: found tl=%d\n", tlabel);
866 			return(xfer);
867 		}
868 	}
869 	if (firewire_debug > 1)
870 		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
871 	splx(s);
872 	return(NULL);
873 }
874 
875 /*
876  * To allocate IEEE1394 XFER structure.
877  */
878 struct fw_xfer *
879 fw_xfer_alloc(struct malloc_type *type)
880 {
881 	struct fw_xfer *xfer;
882 
883 	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
884 	if (xfer == NULL)
885 		return xfer;
886 
887 	microtime(&xfer->tv);
888 	xfer->sub = -1;
889 	xfer->malloc = type;
890 
891 	return xfer;
892 }
893 
894 /*
895  * IEEE1394 XFER post process.
896  */
897 void
898 fw_xfer_done(struct fw_xfer *xfer)
899 {
900 	if (xfer->act.hand == NULL)
901 		return;
902 
903 #if XFER_TIMEOUT
904 	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
905 #endif
906 
907 	if (xfer->fc->status != FWBUSRESET)
908 		xfer->act.hand(xfer);
909 	else {
910 		printf("fw_xfer_done: pending\n");
911 		if (xfer->fc != NULL)
912 			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
913 		else
914 			panic("fw_xfer_done: why xfer->fc is NULL?");
915 	}
916 }
917 
918 /*
919  * To free IEEE1394 XFER structure.
920  */
921 void
922 fw_xfer_free( struct fw_xfer* xfer)
923 {
924 	int s;
925 	if(xfer == NULL ) return;
926 	if(xfer->state == FWXF_INQ){
927 		printf("fw_xfer_free FWXF_INQ\n");
928 		s = splfw();
929 		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
930 		xfer->q->queued --;
931 		splx(s);
932 	}
933 	if(xfer->fc != NULL){
934 		if(xfer->state == FWXF_START){
935 #if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
936 			printf("fw_xfer_free FWXF_START\n");
937 #endif
938 			s = splfw();
939 			xfer->q->drain(xfer->fc, xfer);
940 			splx(s);
941 		}
942 	}
943 	if(xfer->send.buf != NULL){
944 		free(xfer->send.buf, M_FW);
945 	}
946 	if(xfer->recv.buf != NULL){
947 		free(xfer->recv.buf, M_FW);
948 	}
949 	if(xfer->fc != NULL){
950 		fw_tl_free(xfer->fc, xfer);
951 	}
952 	free(xfer, xfer->malloc);
953 }
954 
955 static void
956 fw_asy_callback_free(struct fw_xfer *xfer)
957 {
958 #if 0
959 	printf("asyreq done state=%d resp=%d\n",
960 				xfer->state, xfer->resp);
961 #endif
962 	fw_xfer_free(xfer);
963 }
964 
965 /*
966  * To configure PHY.
967  */
968 static void
969 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
970 {
971 	struct fw_xfer *xfer;
972 	struct fw_pkt *fp;
973 
974 	fc->status = FWBUSPHYCONF;
975 
976 #if 0
977 	DELAY(100000);
978 #endif
979 	xfer = fw_xfer_alloc(M_FWXFER);
980 	xfer->send.len = 12;
981 	xfer->send.off = 0;
982 	xfer->fc = fc;
983 	xfer->retry_req = fw_asybusy;
984 	xfer->act.hand = fw_asy_callback_free;
985 
986 	xfer->send.buf = malloc(sizeof(u_int32_t),
987 					M_FW, M_NOWAIT | M_ZERO);
988 	fp = (struct fw_pkt *)xfer->send.buf;
989 	fp->mode.ld[1] = 0;
990 	if (root_node >= 0)
991 		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
992 	if (gap_count >= 0)
993 		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
994 	fp->mode.ld[2] = ~fp->mode.ld[1];
995 /* XXX Dangerous, how to pass PHY packet to device driver */
996 	fp->mode.common.tcode |= FWTCODE_PHY;
997 
998 	if (firewire_debug)
999 		printf("send phy_config root_node=%d gap_count=%d\n",
1000 						root_node, gap_count);
1001 	fw_asyreq(fc, -1, xfer);
1002 }
1003 
1004 #if 0
1005 /*
1006  * Dump self ID.
1007  */
1008 static void
1009 fw_print_sid(u_int32_t sid)
1010 {
1011 	union fw_self_id *s;
1012 	s = (union fw_self_id *) &sid;
1013 	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1014 		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1015 		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1016 		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1017 		s->p0.power_class, s->p0.port0, s->p0.port1,
1018 		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1019 }
1020 #endif
1021 
1022 /*
1023  * To receive self ID.
1024  */
1025 void fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1026 {
1027 	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1028 	union fw_self_id *self_id;
1029 	u_int i, j, node, c_port = 0, i_branch = 0;
1030 
1031 	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1032 	fc->status = FWBUSINIT;
1033 	fc->max_node = fc->nodeid & 0x3f;
1034 	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1035 	fc->status = FWBUSCYMELECT;
1036 	fc->topology_map->crc_len = 2;
1037 	fc->topology_map->generation ++;
1038 	fc->topology_map->self_id_count = 0;
1039 	fc->topology_map->node_count = 0;
1040 	fc->speed_map->generation ++;
1041 	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1042 	self_id = &fc->topology_map->self_id[0];
1043 	for(i = 0; i < fc->sid_cnt; i ++){
1044 		if (sid[1] != ~sid[0]) {
1045 			printf("fw_sidrcv: invalid self-id packet\n");
1046 			sid += 2;
1047 			continue;
1048 		}
1049 		*self_id = *((union fw_self_id *)sid);
1050 		fc->topology_map->crc_len++;
1051 		if(self_id->p0.sequel == 0){
1052 			fc->topology_map->node_count ++;
1053 			c_port = 0;
1054 #if 0
1055 			fw_print_sid(sid[0]);
1056 #endif
1057 			node = self_id->p0.phy_id;
1058 			if(fc->max_node < node){
1059 				fc->max_node = self_id->p0.phy_id;
1060 			}
1061 			/* XXX I'm not sure this is the right speed_map */
1062 			fc->speed_map->speed[node][node]
1063 					= self_id->p0.phy_speed;
1064 			for (j = 0; j < node; j ++) {
1065 				fc->speed_map->speed[j][node]
1066 					= fc->speed_map->speed[node][j]
1067 					= min(fc->speed_map->speed[j][j],
1068 							self_id->p0.phy_speed);
1069 			}
1070 			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1071 			  (self_id->p0.link_active && self_id->p0.contender)) {
1072 				fc->irm = self_id->p0.phy_id;
1073 			}
1074 			if(self_id->p0.port0 >= 0x2){
1075 				c_port++;
1076 			}
1077 			if(self_id->p0.port1 >= 0x2){
1078 				c_port++;
1079 			}
1080 			if(self_id->p0.port2 >= 0x2){
1081 				c_port++;
1082 			}
1083 		}
1084 		if(c_port > 2){
1085 			i_branch += (c_port - 2);
1086 		}
1087 		sid += 2;
1088 		self_id++;
1089 		fc->topology_map->self_id_count ++;
1090 	}
1091 	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1092 	/* CRC */
1093 	fc->topology_map->crc = fw_crc16(
1094 			(u_int32_t *)&fc->topology_map->generation,
1095 			fc->topology_map->crc_len * 4);
1096 	fc->speed_map->crc = fw_crc16(
1097 			(u_int32_t *)&fc->speed_map->generation,
1098 			fc->speed_map->crc_len * 4);
1099 	/* byteswap and copy to CSR */
1100 	p = (u_int32_t *)fc->topology_map;
1101 	for (i = 0; i <= fc->topology_map->crc_len; i++)
1102 		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1103 	p = (u_int32_t *)fc->speed_map;
1104 	CSRARC(fc, SPED_MAP) = htonl(*p++);
1105 	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1106 	/* don't byte-swap u_int8_t array */
1107 	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1108 
1109 	fc->max_hop = fc->max_node - i_branch;
1110 #if 1
1111 	printf(", maxhop <= %d", fc->max_hop);
1112 #endif
1113 
1114 	if(fc->irm == -1 ){
1115 		printf(", Not found IRM capable node");
1116 	}else{
1117 		printf(", cable IRM = %d", fc->irm);
1118 		if (fc->irm == fc->nodeid)
1119 			printf(" (me)");
1120 	}
1121 	printf("\n");
1122 
1123 	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1124 		if (fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)) {
1125 			fc->status = FWBUSMGRDONE;
1126 			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1127 		} else {
1128 			fc->status = FWBUSMGRELECT;
1129 			callout_reset(&fc->bmr_callout, hz/8,
1130 				(void *)fw_try_bmr, (void *)fc);
1131 		}
1132 	} else {
1133 		fc->status = FWBUSMGRDONE;
1134 #if 0
1135 		device_printf(fc->bdev, "BMR = %x\n",
1136 				CSRARC(fc, BUS_MGR_ID));
1137 #endif
1138 	}
1139 	free(buf, M_FW);
1140 	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1141 		/* I am BMGR */
1142 		fw_bmr(fc);
1143 	}
1144 	callout_reset(&fc->busprobe_callout, hz/4,
1145 			(void *)fw_bus_probe, (void *)fc);
1146 }
1147 
1148 /*
1149  * To probe devices on the IEEE1394 bus.
1150  */
1151 static void
1152 fw_bus_probe(struct firewire_comm *fc)
1153 {
1154 	int s;
1155 	struct fw_device *fwdev, *next;
1156 
1157 	s = splfw();
1158 	fc->status = FWBUSEXPLORE;
1159 	fc->retry_count = 0;
1160 
1161 /*
1162  * Invalidate all devices, just after bus reset. Devices
1163  * to be removed has not been seen longer time.
1164  */
1165 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1166 		next = STAILQ_NEXT(fwdev, link);
1167 		if (fwdev->status != FWDEVINVAL) {
1168 			fwdev->status = FWDEVINVAL;
1169 			fwdev->rcnt = 0;
1170 		} else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1171 			fwdev->rcnt ++;
1172 		} else {
1173 			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1174 			free(fwdev, M_FW);
1175 		}
1176 	}
1177 	fc->ongonode = 0;
1178 	fc->ongoaddr = CSRROMOFF;
1179 	fc->ongodev = NULL;
1180 	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1181 	fw_bus_explore(fc);
1182 	splx(s);
1183 }
1184 
1185 /*
1186  * To collect device informations on the IEEE1394 bus.
1187  */
1188 static void
1189 fw_bus_explore(struct firewire_comm *fc )
1190 {
1191 	int err = 0;
1192 	struct fw_device *fwdev, *pfwdev, *tfwdev;
1193 	u_int32_t addr;
1194 	struct fw_xfer *xfer;
1195 	struct fw_pkt *fp;
1196 
1197 	if(fc->status != FWBUSEXPLORE)
1198 		return;
1199 
1200 loop:
1201 	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1202 
1203 	if(fc->ongonode > fc->max_node) goto done;
1204 	if(fc->ongonode >= 0x3f) goto done;
1205 
1206 	/* check link */
1207 	/* XXX we need to check phy_id first */
1208 	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1209 		if (firewire_debug)
1210 			printf("node%d: link down\n", fc->ongonode);
1211 		fc->ongonode++;
1212 		goto loop;
1213 	}
1214 
1215 	if(fc->ongoaddr <= CSRROMOFF &&
1216 		fc->ongoeui.hi == 0xffffffff &&
1217 		fc->ongoeui.lo == 0xffffffff ){
1218 		fc->ongoaddr = CSRROMOFF;
1219 		addr = 0xf0000000 | fc->ongoaddr;
1220 	}else if(fc->ongoeui.hi == 0xffffffff ){
1221 		fc->ongoaddr = CSRROMOFF + 0xc;
1222 		addr = 0xf0000000 | fc->ongoaddr;
1223 	}else if(fc->ongoeui.lo == 0xffffffff ){
1224 		fc->ongoaddr = CSRROMOFF + 0x10;
1225 		addr = 0xf0000000 | fc->ongoaddr;
1226 	}else if(fc->ongodev == NULL){
1227 		STAILQ_FOREACH(fwdev, &fc->devices, link)
1228 			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1229 				break;
1230 		if(fwdev != NULL){
1231 			fwdev->dst = fc->ongonode;
1232 			fwdev->status = FWDEVATTACHED;
1233 			fc->ongonode++;
1234 			fc->ongoaddr = CSRROMOFF;
1235 			fc->ongodev = NULL;
1236 			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1237 			goto loop;
1238 		}
1239 		fwdev = malloc(sizeof(struct fw_device), M_FW, M_NOWAIT);
1240 		if(fwdev == NULL)
1241 			return;
1242 		fwdev->fc = fc;
1243 		fwdev->rommax = 0;
1244 		fwdev->dst = fc->ongonode;
1245 		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1246 		fwdev->status = FWDEVINIT;
1247 #if 0
1248 		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1249 			>> ((3 - (fc->ongonode % 4)) * 8);
1250 #else
1251 		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1252 #endif
1253 
1254 		pfwdev = NULL;
1255 		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1256 			if (tfwdev->eui.hi > fwdev->eui.hi ||
1257 					(tfwdev->eui.hi == fwdev->eui.hi &&
1258 					tfwdev->eui.lo > fwdev->eui.lo))
1259 				break;
1260 			pfwdev = tfwdev;
1261 		}
1262 		if (pfwdev == NULL)
1263 			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1264 		else
1265 			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1266 
1267 		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1268 			linkspeed[fwdev->speed],
1269 			fc->ongoeui.hi, fc->ongoeui.lo);
1270 
1271 		fc->ongodev = fwdev;
1272 		fc->ongoaddr = CSRROMOFF;
1273 		addr = 0xf0000000 | fc->ongoaddr;
1274 	}else{
1275 		addr = 0xf0000000 | fc->ongoaddr;
1276 	}
1277 #if 0
1278 	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1279 		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1280 		fw_bus_explore_callback);
1281 	if(xfer == NULL) goto done;
1282 #else
1283 	xfer = fw_xfer_alloc(M_FWXFER);
1284 	if(xfer == NULL){
1285 		goto done;
1286 	}
1287 	xfer->send.len = 16;
1288 	xfer->spd = 0;
1289 	xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1290 	if(xfer->send.buf == NULL){
1291 		fw_xfer_free( xfer);
1292 		return;
1293 	}
1294 
1295 	xfer->send.off = 0;
1296 	fp = (struct fw_pkt *)xfer->send.buf;
1297 	fp->mode.rreqq.dest_hi = htons(0xffff);
1298 	fp->mode.rreqq.tlrt = 0;
1299 	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1300 	fp->mode.rreqq.pri = 0;
1301 	fp->mode.rreqq.src = 0;
1302 	xfer->dst = FWLOCALBUS | fc->ongonode;
1303 	fp->mode.rreqq.dst = htons(xfer->dst);
1304 	fp->mode.rreqq.dest_lo = htonl(addr);
1305 	xfer->act.hand = fw_bus_explore_callback;
1306 
1307 	if (firewire_debug)
1308 		printf("node%d: explore addr=0x%x\n",
1309 				fc->ongonode, fc->ongoaddr);
1310 	err = fw_asyreq(fc, -1, xfer);
1311 	if(err){
1312 		fw_xfer_free( xfer);
1313 		return;
1314 	}
1315 #endif
1316 	return;
1317 done:
1318 	/* fw_attach_devs */
1319 	fc->status = FWBUSEXPDONE;
1320 	if (firewire_debug)
1321 		printf("bus_explore done\n");
1322 	fw_attach_dev(fc);
1323 	return;
1324 
1325 }
1326 
1327 /* Portable Async. request read quad */
1328 struct fw_xfer *
1329 asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1330 	u_int32_t addr_hi, u_int32_t addr_lo,
1331 	void (*hand) __P((struct fw_xfer*)))
1332 {
1333 	struct fw_xfer *xfer;
1334 	struct fw_pkt *fp;
1335 	int err;
1336 
1337 	xfer = fw_xfer_alloc(M_FWXFER);
1338 	if(xfer == NULL){
1339 		return NULL;
1340 	}
1341 	xfer->send.len = 16;
1342 	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1343 	xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1344 	if(xfer->send.buf == NULL){
1345 		fw_xfer_free( xfer);
1346 		return NULL;
1347 	}
1348 
1349 	xfer->send.off = 0;
1350 	fp = (struct fw_pkt *)xfer->send.buf;
1351 	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1352 	if(tl & FWP_TL_VALID){
1353 		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1354 	}else{
1355 		fp->mode.rreqq.tlrt = 0;
1356 	}
1357 	fp->mode.rreqq.tlrt |= rt & 0x3;
1358 	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1359 	fp->mode.rreqq.pri = 0;
1360 	fp->mode.rreqq.src = 0;
1361 	xfer->dst = addr_hi >> 16;
1362 	fp->mode.rreqq.dst = htons(xfer->dst);
1363 	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1364 	xfer->act.hand = hand;
1365 
1366 	err = fw_asyreq(fc, -1, xfer);
1367 	if(err){
1368 		fw_xfer_free( xfer);
1369 		return NULL;
1370 	}
1371 	return xfer;
1372 }
1373 
1374 /*
1375  * Callback for the IEEE1394 bus information collection.
1376  */
1377 static void
1378 fw_bus_explore_callback(struct fw_xfer *xfer)
1379 {
1380 	struct firewire_comm *fc;
1381 	struct fw_pkt *sfp,*rfp;
1382 	struct csrhdr *chdr;
1383 	struct csrdir *csrd;
1384 	struct csrreg *csrreg;
1385 	u_int32_t offset;
1386 
1387 
1388 	if(xfer == NULL) {
1389 		printf("xfer == NULL\n");
1390 		return;
1391 	}
1392 	fc = xfer->fc;
1393 
1394 	if (firewire_debug)
1395 		printf("node%d: callback addr=0x%x\n",
1396 			fc->ongonode, fc->ongoaddr);
1397 
1398 	if(xfer->resp != 0){
1399 		printf("node%d: resp=%d addr=0x%x\n",
1400 			fc->ongonode, xfer->resp, fc->ongoaddr);
1401 		fc->retry_count++;
1402 		goto nextnode;
1403 	}
1404 
1405 	if(xfer->send.buf == NULL){
1406 		printf("node%d: send.buf=NULL addr=0x%x\n",
1407 			fc->ongonode, fc->ongoaddr);
1408 		fc->retry_count++;
1409 		goto nextnode;
1410 	}
1411 	sfp = (struct fw_pkt *)xfer->send.buf;
1412 
1413 	if(xfer->recv.buf == NULL){
1414 		printf("node%d: recv.buf=NULL addr=0x%x\n",
1415 			fc->ongonode, fc->ongoaddr);
1416 		fc->retry_count++;
1417 		goto nextnode;
1418 	}
1419 	rfp = (struct fw_pkt *)xfer->recv.buf;
1420 #if 0
1421 	{
1422 		u_int32_t *qld;
1423 		int i;
1424 		qld = (u_int32_t *)xfer->recv.buf;
1425 		printf("len:%d\n", xfer->recv.len);
1426 		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1427 			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1428 			if((i % 16) == 15) printf("\n");
1429 		}
1430 		if((i % 16) != 15) printf("\n");
1431 	}
1432 #endif
1433 	if(fc->ongodev == NULL){
1434 		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1435 			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1436 			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1437 /* If CSR is minimal confinguration, more investgation is not needed. */
1438 			if(chdr->info_len == 1){
1439 				if (firewire_debug)
1440 					printf("node%d: minimal config\n",
1441 								fc->ongonode);
1442 				goto nextnode;
1443 			}else{
1444 				fc->ongoaddr = CSRROMOFF + 0xc;
1445 			}
1446 		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1447 			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1448 			fc->ongoaddr = CSRROMOFF + 0x10;
1449 		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1450 			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1451 			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1452 				if (firewire_debug)
1453 					printf("node%d: eui64 is zero.\n",
1454 							fc->ongonode);
1455 				goto nextnode;
1456 			}
1457 			fc->ongoaddr = CSRROMOFF;
1458 		}
1459 	}else{
1460 		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1461 		if(fc->ongoaddr > fc->ongodev->rommax){
1462 			fc->ongodev->rommax = fc->ongoaddr;
1463 		}
1464 		csrd = SLIST_FIRST(&fc->ongocsr);
1465 		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1466 			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1467 			offset = CSRROMOFF;
1468 		}else{
1469 			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1470 			offset = csrd->off;
1471 		}
1472 		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1473 			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1474 			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1475 				csrd = SLIST_FIRST(&fc->csrfree);
1476 				if(csrd == NULL){
1477 					goto nextnode;
1478 				}else{
1479 					csrd->ongoaddr = fc->ongoaddr;
1480 					fc->ongoaddr += csrreg->val * 4;
1481 					csrd->off = fc->ongoaddr;
1482 					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1483 					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1484 					goto nextaddr;
1485 				}
1486 			}
1487 		}
1488 		fc->ongoaddr += 4;
1489 		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1490 				(fc->ongodev->rommax < 0x414)){
1491 			if(fc->ongodev->rommax <= 0x414){
1492 				csrd = SLIST_FIRST(&fc->csrfree);
1493 				if(csrd == NULL) goto nextnode;
1494 				csrd->off = fc->ongoaddr;
1495 				csrd->ongoaddr = fc->ongoaddr;
1496 				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1497 				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1498 			}
1499 			goto nextaddr;
1500 		}
1501 
1502 		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1503 			if(csrd == NULL){
1504 				goto nextnode;
1505 			};
1506 			fc->ongoaddr = csrd->ongoaddr + 4;
1507 			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1508 			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1509 			csrd = SLIST_FIRST(&fc->ongocsr);
1510 			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1511 				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1512 				offset = CSRROMOFF;
1513 			}else{
1514 				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1515 				offset = csrd->off;
1516 			}
1517 		}
1518 		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1519 			goto nextnode;
1520 		}
1521 	}
1522 nextaddr:
1523 	fw_xfer_free( xfer);
1524 	fw_bus_explore(fc);
1525 	return;
1526 nextnode:
1527 	fw_xfer_free( xfer);
1528 	fc->ongonode++;
1529 /* housekeeping work space */
1530 	fc->ongoaddr = CSRROMOFF;
1531 	fc->ongodev = NULL;
1532 	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1533 	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1534 		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1535 		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1536 	}
1537 	fw_bus_explore(fc);
1538 	return;
1539 }
1540 
1541 /*
1542  * To obtain CSR register values.
1543  */
1544 u_int32_t
1545 getcsrdata(struct fw_device *fwdev, u_int8_t key)
1546 {
1547 	int i;
1548 	struct csrhdr *chdr;
1549 	struct csrreg *creg;
1550 	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1551 	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1552 		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1553 		if(creg->key == key){
1554 			return (u_int32_t)creg->val;
1555 		}
1556 	}
1557 	return 0;
1558 }
1559 
1560 /*
1561  * To attach sub-devices layer onto IEEE1394 bus.
1562  */
1563 static void
1564 fw_attach_dev(struct firewire_comm *fc)
1565 {
1566 	struct fw_device *fwdev;
1567 	struct fw_xfer *xfer;
1568 	int i, err;
1569 	device_t *devlistp;
1570 	int devcnt;
1571 	struct firewire_dev_comm *fdc;
1572 	u_int32_t spec, ver;
1573 
1574 	STAILQ_FOREACH(fwdev, &fc->devices, link) {
1575 		if(fwdev->status == FWDEVINIT){
1576 			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1577 			if(spec == 0)
1578 				continue;
1579 			ver = getcsrdata(fwdev, CSRKEY_VER);
1580 			if(ver == 0)
1581 				continue;
1582 			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1583 
1584 			device_printf(fc->bdev, "Device ");
1585 			switch(spec){
1586 			case CSRVAL_ANSIT10:
1587 				switch(ver){
1588 				case CSRVAL_T10SBP2:
1589 					printf("SBP-II");
1590 					break;
1591 				default:
1592 					break;
1593 				}
1594 				break;
1595 			case CSRVAL_1394TA:
1596 				switch(ver){
1597 				case CSR_PROTAVC:
1598 					printf("AV/C");
1599 					break;
1600 				case CSR_PROTCAL:
1601 					printf("CAL");
1602 					break;
1603 				case CSR_PROTEHS:
1604 					printf("EHS");
1605 					break;
1606 				case CSR_PROTHAVI:
1607 					printf("HAVi");
1608 					break;
1609 				case CSR_PROTCAM104:
1610 					printf("1394 Cam 1.04");
1611 					break;
1612 				case CSR_PROTCAM120:
1613 					printf("1394 Cam 1.20");
1614 					break;
1615 				case CSR_PROTCAM130:
1616 					printf("1394 Cam 1.30");
1617 					break;
1618 				case CSR_PROTDPP:
1619 					printf("1394 Direct print");
1620 					break;
1621 				case CSR_PROTIICP:
1622 					printf("Industrial & Instrument");
1623 					break;
1624 				default:
1625 					printf("unknown 1394TA");
1626 					break;
1627 				}
1628 				break;
1629 			default:
1630 				printf("unknown spec");
1631 				break;
1632 			}
1633 			fwdev->status = FWDEVATTACHED;
1634 			printf("\n");
1635 		}
1636 	}
1637 	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1638 	if( err != 0 )
1639 		return;
1640 	for( i = 0 ; i < devcnt ; i++){
1641 		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1642 			fdc = device_get_softc(devlistp[i]);
1643 			if (fdc->post_explore != NULL)
1644 				fdc->post_explore(fdc);
1645 		}
1646 	}
1647 	free(devlistp, M_TEMP);
1648 
1649 	/* call pending handlers */
1650 	i = 0;
1651 	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1652 		STAILQ_REMOVE_HEAD(&fc->pending, link);
1653 		i++;
1654 		if (xfer->act.hand)
1655 			xfer->act.hand(xfer);
1656 	}
1657 	if (i > 0)
1658 		printf("fw_attach_dev: %d pending handlers called\n", i);
1659 	if (fc->retry_count > 0) {
1660 		printf("probe failed for %d node\n", fc->retry_count);
1661 #if 0
1662 		callout_reset(&fc->retry_probe_callout, hz*2,
1663 					(void *)fc->ibr, (void *)fc);
1664 #endif
1665 	}
1666 	return;
1667 }
1668 
1669 /*
1670  * To allocate uniq transaction label.
1671  */
1672 static int
1673 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1674 {
1675 	u_int i;
1676 	struct tlabel *tl, *tmptl;
1677 	int s;
1678 	static u_int32_t label = 0;
1679 
1680 	s = splfw();
1681 	for( i = 0 ; i < 0x40 ; i ++){
1682 		label = (label + 1) & 0x3f;
1683 		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1684 			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1685 			if(tmptl->xfer->dst == xfer->dst) break;
1686 		}
1687 		if(tmptl == NULL) {
1688 			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1689 			if (tl == NULL) {
1690 				splx(s);
1691 				return (-1);
1692 			}
1693 			tl->xfer = xfer;
1694 			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1695 			splx(s);
1696 			if (firewire_debug > 1)
1697 				printf("fw_get_tlabel: dst=%d tl=%d\n",
1698 						xfer->dst, label);
1699 			return(label);
1700 		}
1701 	}
1702 	splx(s);
1703 
1704 	printf("fw_get_tlabel: no free tlabel\n");
1705 	return(-1);
1706 }
1707 
1708 /*
1709  * Generic packet receving process.
1710  */
1711 void
1712 fw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1713 {
1714 	struct fw_pkt *fp, *resfp;
1715 	struct fw_xfer *xfer;
1716 	struct fw_bind *bind;
1717 	struct firewire_softc *sc;
1718 	int s;
1719 #if 0
1720 	{
1721 		u_int32_t *qld;
1722 		int i;
1723 		qld = (u_int32_t *)buf;
1724 		printf("spd %d len:%d\n", spd, len);
1725 		for( i = 0 ; i <= len && i < 32; i+= 4){
1726 			printf("0x%08x ", ntohl(qld[i/4]));
1727 			if((i % 16) == 15) printf("\n");
1728 		}
1729 		if((i % 16) != 15) printf("\n");
1730 	}
1731 #endif
1732 	fp = (struct fw_pkt *)(buf + off);
1733 	switch(fp->mode.common.tcode){
1734 	case FWTCODE_WRES:
1735 	case FWTCODE_RRESQ:
1736 	case FWTCODE_RRESB:
1737 	case FWTCODE_LRES:
1738 		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1739 					fp->mode.hdr.tlrt >> 2);
1740 		if(xfer == NULL) {
1741 			printf("fw_rcv: unknown response "
1742 					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1743 					fp->mode.common.tcode,
1744 					ntohs(fp->mode.hdr.src),
1745 					fp->mode.hdr.tlrt >> 2,
1746 					fp->mode.hdr.tlrt & 3,
1747 					fp->mode.rresq.data);
1748 #if 1
1749 			printf("try ad-hoc work around!!\n");
1750 			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1751 					(fp->mode.hdr.tlrt >> 2)^3);
1752 			if (xfer == NULL) {
1753 				printf("no use...\n");
1754 				goto err;
1755 			}
1756 #else
1757 			goto err;
1758 #endif
1759 		}
1760 		switch(xfer->act_type){
1761 		case FWACT_XFER:
1762 			if((xfer->sub >= 0) &&
1763 				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1764 				xfer->resp = EINVAL;
1765 				fw_xfer_done(xfer);
1766 				goto err;
1767 			}
1768 			xfer->recv.len = len;
1769 			xfer->recv.off = off;
1770 			xfer->recv.buf = buf;
1771 			xfer->resp = 0;
1772 			fw_xfer_done(xfer);
1773 			return;
1774 			break;
1775 		case FWACT_CH:
1776 		default:
1777 			goto err;
1778 			break;
1779 		}
1780 		break;
1781 	case FWTCODE_WREQQ:
1782 	case FWTCODE_WREQB:
1783 	case FWTCODE_RREQQ:
1784 	case FWTCODE_RREQB:
1785 	case FWTCODE_LREQ:
1786 		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1787 			ntohl(fp->mode.rreqq.dest_lo));
1788 		if(bind == NULL){
1789 #if __FreeBSD_version >= 500000
1790 			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
1791 #else
1792 			printf("Unknown service addr 0x%08x:0x%08lx tcode=%x\n",
1793 #endif
1794 				ntohs(fp->mode.rreqq.dest_hi),
1795 				ntohl(fp->mode.rreqq.dest_lo),
1796 				fp->mode.common.tcode);
1797 			if (fc->status == FWBUSRESET) {
1798 				printf("fw_rcv: cannot respond(bus reset)!\n");
1799 				goto err;
1800 			}
1801 			xfer = fw_xfer_alloc(M_FWXFER);
1802 			if(xfer == NULL){
1803 				return;
1804 			}
1805 			xfer->spd = spd;
1806 			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1807 			resfp = (struct fw_pkt *)xfer->send.buf;
1808 			switch(fp->mode.common.tcode){
1809 			case FWTCODE_WREQQ:
1810 			case FWTCODE_WREQB:
1811 				resfp->mode.hdr.tcode = FWTCODE_WRES;
1812 				xfer->send.len = 12;
1813 				break;
1814 			case FWTCODE_RREQQ:
1815 				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1816 				xfer->send.len = 16;
1817 				break;
1818 			case FWTCODE_RREQB:
1819 				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1820 				xfer->send.len = 16;
1821 				break;
1822 			case FWTCODE_LREQ:
1823 				resfp->mode.hdr.tcode = FWTCODE_LRES;
1824 				xfer->send.len = 16;
1825 				break;
1826 			}
1827 			resfp->mode.hdr.dst = fp->mode.hdr.src;
1828 			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1829 			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1830 			resfp->mode.rresb.rtcode = 7;
1831 			resfp->mode.rresb.extcode = 0;
1832 			resfp->mode.rresb.len = 0;
1833 /*
1834 			xfer->act.hand = fw_asy_callback;
1835 */
1836 			xfer->act.hand = fw_xfer_free;
1837 			if(fw_asyreq(fc, -1, xfer)){
1838 				fw_xfer_free( xfer);
1839 				return;
1840 			}
1841 			goto err;
1842 		}
1843 		switch(bind->xfer->act_type){
1844 		case FWACT_XFER:
1845 			xfer = fw_xfer_alloc(M_FWXFER);
1846 			if(xfer == NULL) goto err;
1847 			xfer->fc = bind->xfer->fc;
1848 			xfer->sc = bind->xfer->sc;
1849 			xfer->recv.buf = buf;
1850 			xfer->recv.len = len;
1851 			xfer->recv.off = off;
1852 			xfer->spd = spd;
1853 			xfer->act.hand = bind->xfer->act.hand;
1854 			if (fc->status != FWBUSRESET)
1855 				xfer->act.hand(xfer);
1856 			else
1857 				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1858 			return;
1859 			break;
1860 		case FWACT_CH:
1861 			if(fc->ir[bind->xfer->sub]->queued >=
1862 				fc->ir[bind->xfer->sub]->maxq){
1863 				device_printf(fc->bdev,
1864 					"Discard a packet %x %d\n",
1865 					bind->xfer->sub,
1866 					fc->ir[bind->xfer->sub]->queued);
1867 				goto err;
1868 			}
1869 			xfer = fw_xfer_alloc(M_FWXFER);
1870 			if(xfer == NULL) goto err;
1871 			xfer->recv.buf = buf;
1872 			xfer->recv.len = len;
1873 			xfer->recv.off = off;
1874 			xfer->spd = spd;
1875 			s = splfw();
1876 			fc->ir[bind->xfer->sub]->queued++;
1877 			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1878 			splx(s);
1879 
1880 			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1881 
1882 			return;
1883 			break;
1884 		default:
1885 			goto err;
1886 			break;
1887 		}
1888 		break;
1889 	case FWTCODE_STREAM:
1890 	{
1891 		struct fw_xferq *xferq;
1892 
1893 		xferq = fc->ir[sub];
1894 #if 0
1895 		printf("stream rcv dma %d len %d off %d spd %d\n",
1896 			sub, len, off, spd);
1897 #endif
1898 		if(xferq->queued >= xferq->maxq) {
1899 			printf("receive queue is full\n");
1900 			goto err;
1901 		}
1902 		xfer = fw_xfer_alloc(M_FWXFER);
1903 		if(xfer == NULL) goto err;
1904 		xfer->recv.buf = buf;
1905 		xfer->recv.len = len;
1906 		xfer->recv.off = off;
1907 		xfer->spd = spd;
1908 		s = splfw();
1909 		xferq->queued++;
1910 		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1911 		splx(s);
1912 		sc = device_get_softc(fc->bdev);
1913 #if __FreeBSD_version >= 500000
1914 		if (SEL_WAITING(&xferq->rsel))
1915 #else
1916 		if (&xferq->rsel.si_pid != 0)
1917 #endif
1918 			selwakeup(&xferq->rsel);
1919 		if (xferq->flag & FWXFERQ_WAKEUP) {
1920 			xferq->flag &= ~FWXFERQ_WAKEUP;
1921 			wakeup((caddr_t)xferq);
1922 		}
1923 		if (xferq->flag & FWXFERQ_HANDLER) {
1924 			xferq->hand(xferq);
1925 		}
1926 		return;
1927 		break;
1928 	}
1929 	default:
1930 		printf("fw_rcv: unknow tcode\n");
1931 		break;
1932 	}
1933 err:
1934 	free(buf, M_FW);
1935 }
1936 
1937 /*
1938  * Post process for Bus Manager election process.
1939  */
1940 static void
1941 fw_try_bmr_callback(struct fw_xfer *xfer)
1942 {
1943 	struct fw_pkt *rfp;
1944 	struct firewire_comm *fc;
1945 	int bmr;
1946 
1947 	if (xfer == NULL)
1948 		return;
1949 	fc = xfer->fc;
1950 	if (xfer->resp != 0)
1951 		goto error;
1952 	if (xfer->send.buf == NULL)
1953 		goto error;
1954 	if (xfer->recv.buf == NULL)
1955 		goto error;
1956 	rfp = (struct fw_pkt *)xfer->recv.buf;
1957 	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1958 		goto error;
1959 
1960 	bmr = ntohl(rfp->mode.lres.payload[0]);
1961 	if (bmr == 0x3f)
1962 		bmr = fc->nodeid;
1963 
1964 	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1965 	device_printf(fc->bdev, "new bus manager %d ",
1966 		CSRARC(fc, BUS_MGR_ID));
1967 	if(bmr == fc->nodeid){
1968 		printf("(me)\n");
1969 		fw_bmr(fc);
1970 	}else{
1971 		printf("\n");
1972 	}
1973 error:
1974 	fw_xfer_free(xfer);
1975 }
1976 
1977 /*
1978  * To candidate Bus Manager election process.
1979  */
1980 static void
1981 fw_try_bmr(void *arg)
1982 {
1983 	struct fw_xfer *xfer;
1984 	struct firewire_comm *fc = (struct firewire_comm *)arg;
1985 	struct fw_pkt *fp;
1986 	int err = 0;
1987 
1988 	xfer = fw_xfer_alloc(M_FWXFER);
1989 	if(xfer == NULL){
1990 		return;
1991 	}
1992 	xfer->send.len = 24;
1993 	xfer->spd = 0;
1994 	xfer->send.buf = malloc(24, M_FW, M_NOWAIT);
1995 	if(xfer->send.buf == NULL){
1996 		fw_xfer_free( xfer);
1997 		return;
1998 	}
1999 
2000 	fc->status = FWBUSMGRELECT;
2001 
2002 	xfer->send.off = 0;
2003 	fp = (struct fw_pkt *)xfer->send.buf;
2004 	fp->mode.lreq.dest_hi = htons(0xffff);
2005 	fp->mode.lreq.tlrt = 0;
2006 	fp->mode.lreq.tcode = FWTCODE_LREQ;
2007 	fp->mode.lreq.pri = 0;
2008 	fp->mode.lreq.src = 0;
2009 	fp->mode.lreq.len = htons(8);
2010 	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
2011 	xfer->dst = FWLOCALBUS | fc->irm;
2012 	fp->mode.lreq.dst = htons(xfer->dst);
2013 	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
2014 	fp->mode.lreq.payload[0] = htonl(0x3f);
2015 	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
2016 	xfer->act_type = FWACT_XFER;
2017 	xfer->act.hand = fw_try_bmr_callback;
2018 
2019 	err = fw_asyreq(fc, -1, xfer);
2020 	if(err){
2021 		fw_xfer_free( xfer);
2022 		return;
2023 	}
2024 	return;
2025 }
2026 
2027 #ifdef FW_VMACCESS
2028 /*
2029  * Software implementation for physical memory block access.
2030  * XXX:Too slow, usef for debug purpose only.
2031  */
2032 static void
2033 fw_vmaccess(struct fw_xfer *xfer){
2034 	struct fw_pkt *rfp, *sfp = NULL;
2035 	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
2036 
2037 	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
2038 			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2039 	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2040 	if(xfer->resp != 0){
2041 		fw_xfer_free( xfer);
2042 		return;
2043 	}
2044 	if(xfer->recv.buf == NULL){
2045 		fw_xfer_free( xfer);
2046 		return;
2047 	}
2048 	rfp = (struct fw_pkt *)xfer->recv.buf;
2049 	switch(rfp->mode.hdr.tcode){
2050 		/* XXX need fix for 64bit arch */
2051 		case FWTCODE_WREQB:
2052 			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2053 			xfer->send.len = 12;
2054 			sfp = (struct fw_pkt *)xfer->send.buf;
2055 			bcopy(rfp->mode.wreqb.payload,
2056 				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2057 			sfp->mode.wres.tcode = FWTCODE_WRES;
2058 			sfp->mode.wres.rtcode = 0;
2059 			break;
2060 		case FWTCODE_WREQQ:
2061 			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2062 			xfer->send.len = 12;
2063 			sfp->mode.wres.tcode = FWTCODE_WRES;
2064 			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2065 			sfp->mode.wres.rtcode = 0;
2066 			break;
2067 		case FWTCODE_RREQB:
2068 			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2069 			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2070 			sfp = (struct fw_pkt *)xfer->send.buf;
2071 			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2072 				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2073 			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2074 			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2075 			sfp->mode.rresb.rtcode = 0;
2076 			sfp->mode.rresb.extcode = 0;
2077 			break;
2078 		case FWTCODE_RREQQ:
2079 			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2080 			xfer->send.len = 16;
2081 			sfp = (struct fw_pkt *)xfer->send.buf;
2082 			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2083 			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2084 			sfp->mode.rresb.rtcode = 0;
2085 			break;
2086 		default:
2087 			fw_xfer_free( xfer);
2088 			return;
2089 	}
2090 	xfer->send.off = 0;
2091 	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2092 	xfer->dst = ntohs(rfp->mode.hdr.src);
2093 	xfer->act.hand = fw_xfer_free;
2094 	xfer->retry_req = fw_asybusy;
2095 
2096 	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2097 	sfp->mode.hdr.pri = 0;
2098 
2099 	fw_asyreq(xfer->fc, -1, xfer);
2100 /**/
2101 	return;
2102 }
2103 #endif
2104 
2105 /*
2106  * CRC16 check-sum for IEEE1394 register blocks.
2107  */
2108 u_int16_t
2109 fw_crc16(u_int32_t *ptr, u_int32_t len){
2110 	u_int32_t i, sum, crc = 0;
2111 	int shift;
2112 	len = (len + 3) & ~3;
2113 	for(i = 0 ; i < len ; i+= 4){
2114 		for( shift = 28 ; shift >= 0 ; shift -= 4){
2115 			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2116 			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2117 		}
2118 		crc &= 0xffff;
2119 	}
2120 	return((u_int16_t) crc);
2121 }
2122 
2123 static int
2124 fw_bmr(struct firewire_comm *fc)
2125 {
2126 	struct fw_device fwdev;
2127 	int cmstr;
2128 
2129 	/* XXX Assume that the current root node is cycle master capable */
2130 	cmstr = fc->max_node;
2131 	/* If I am the bus manager, optimize gapcount */
2132 	if(fc->max_hop <= MAX_GAPHOP ){
2133 		fw_phy_config(fc, (fc->max_node > 0)?cmstr:-1,
2134 						gap_cnt[fc->max_hop]);
2135 	}
2136 	/* If we are the cycle master, nothing to do */
2137 	if (cmstr == fc->nodeid)
2138 		return 0;
2139 	/* Bus probe has not finished, make dummy fwdev for cmstr */
2140 	bzero(&fwdev, sizeof(fwdev));
2141 	fwdev.fc = fc;
2142 	fwdev.dst = cmstr;
2143 	fwdev.speed = 0;
2144 	fwdev.maxrec = 8; /* 512 */
2145 	fwdev.status = FWDEVINIT;
2146 	/* Set cmstr bit on the cycle master */
2147 	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2148 		0xffff, 0xf0000000 | STATE_SET, 1 << 16,
2149 		fw_asy_callback_free);
2150 
2151 	return 0;
2152 }
2153 
2154 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2155 MODULE_VERSION(firewire, 1);
2156