xref: /freebsd/sys/dev/firewire/firewire.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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 	CALLOUT_INIT(&sc->fc->timeout_callout);
427 	CALLOUT_INIT(&sc->fc->bmr_callout);
428 	CALLOUT_INIT(&sc->fc->retry_probe_callout);
429 	CALLOUT_INIT(&sc->fc->busprobe_callout);
430 
431 	callout_reset(&sc->fc->timeout_callout, hz,
432 			(void *)firewire_watchdog, (void *)sc->fc);
433 
434 	/* Locate our children */
435 	bus_generic_probe(dev);
436 
437 	/* launch attachement of the added children */
438 	bus_generic_attach(dev);
439 
440 #if 1
441 	/* bus_reset */
442 	fc->ibr(fc);
443 #endif
444 
445 	return 0;
446 }
447 
448 /*
449  * Attach it as child.
450  */
451 static device_t
452 firewire_add_child(device_t dev, int order, const char *name, int unit)
453 {
454         device_t child;
455 	struct firewire_softc *sc;
456 
457 	sc = (struct firewire_softc *)device_get_softc(dev);
458 	child = device_add_child(dev, name, unit);
459 	if (child) {
460 		device_set_ivars(child, sc->fc);
461 		device_probe_and_attach(child);
462 	}
463 
464 	return child;
465 }
466 
467 /*
468  * Dettach it.
469  */
470 static int
471 firewire_detach( device_t dev )
472 {
473 	struct firewire_softc *sc;
474 	struct csrdir *csrd, *next;
475 	struct fw_device *fwdev, *fwdev_next;
476 
477 	sc = (struct firewire_softc *)device_get_softc(dev);
478 
479 	bus_generic_detach(dev);
480 
481 	callout_stop(&sc->fc->timeout_callout);
482 	callout_stop(&sc->fc->bmr_callout);
483 	callout_stop(&sc->fc->retry_probe_callout);
484 	callout_stop(&sc->fc->busprobe_callout);
485 
486 #if __FreeBSD_version >= 500000
487 	destroy_dev(sc->dev);
488 #else
489 	{
490 		int j;
491 		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
492 			destroy_dev(sc->dev[j]);
493 	}
494 #endif
495 	/* XXX xfree_free and untimeout on all xfers */
496 	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
497 							fwdev = fwdev_next) {
498 		fwdev_next = STAILQ_NEXT(fwdev, link);
499 		free(fwdev, M_FW);
500 	}
501 	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
502 		next = SLIST_NEXT(csrd, link);
503 		free(csrd, M_FW);
504 	}
505 	free(sc->fc->topology_map, M_FW);
506 	free(sc->fc->speed_map, M_FW);
507 	return(0);
508 }
509 #if 0
510 static int
511 firewire_shutdown( device_t dev )
512 {
513 	return 0;
514 }
515 #endif
516 
517 
518 static void
519 fw_xferq_drain(struct fw_xferq *xferq)
520 {
521 	struct fw_xfer *xfer;
522 
523 	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
524 		STAILQ_REMOVE_HEAD(&xferq->q, link);
525 		xferq->queued --;
526 		xfer->resp = EAGAIN;
527 		switch (xfer->act_type) {
528 		case FWACT_XFER:
529 			fw_xfer_done(xfer);
530 			break;
531 		default:
532 			/* ??? */
533 			fw_xfer_free(xfer);
534 			break;
535 		}
536 	}
537 }
538 
539 void
540 fw_drain_txq(struct firewire_comm *fc)
541 {
542 	int i;
543 
544 	fw_xferq_drain(fc->atq);
545 	fw_xferq_drain(fc->ats);
546 	for(i = 0; i < fc->nisodma; i++)
547 		fw_xferq_drain(fc->it[i]);
548 }
549 
550 /*
551  * Called after bus reset.
552  */
553 void
554 fw_busreset(struct firewire_comm *fc)
555 {
556 	int i;
557 
558 	switch(fc->status){
559 	case FWBUSMGRELECT:
560 		callout_stop(&fc->bmr_callout);
561 		break;
562 	default:
563 		break;
564 	}
565 	fc->status = FWBUSRESET;
566 	CSRARC(fc, STATE_CLEAR)
567 			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
568 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
569 	CSRARC(fc, NODE_IDS) = 0x3f;
570 
571 	CSRARC(fc, TOPO_MAP + 8) = 0;
572 	fc->irm = -1;
573 
574 	fc->max_node = -1;
575 
576 	for(i = 2; i < 0x100/4 - 2 ; i++){
577 		CSRARC(fc, SPED_MAP + i * 4) = 0;
578 	}
579 	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
580 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
581 	CSRARC(fc, RESET_START) = 0;
582 	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
583 	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
584 	CSRARC(fc, CYCLE_TIME) = 0x0;
585 	CSRARC(fc, BUS_TIME) = 0x0;
586 	CSRARC(fc, BUS_MGR_ID) = 0x3f;
587 	CSRARC(fc, BANDWIDTH_AV) = 4915;
588 	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
589 	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
590 	CSRARC(fc, IP_CHANNELS) = (1 << 31);
591 
592 	CSRARC(fc, CONF_ROM) = 0x04 << 24;
593 	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
594 	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
595 				1 << 28 | 0xff << 16 | 0x09 << 8;
596 	CSRARC(fc, CONF_ROM + 0xc) = 0;
597 
598 /* DV depend CSRs see blue book */
599 	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
600 	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
601 
602 	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
603 	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
604 }
605 
606 /* Call once after reboot */
607 void fw_init(struct firewire_comm *fc)
608 {
609 	int i;
610 	struct csrdir *csrd;
611 #ifdef FW_VMACCESS
612 	struct fw_xfer *xfer;
613 	struct fw_bind *fwb;
614 #endif
615 
616 	fc->max_asyretry = FW_MAXASYRTY;
617 
618 	fc->arq->queued = 0;
619 	fc->ars->queued = 0;
620 	fc->atq->queued = 0;
621 	fc->ats->queued = 0;
622 
623 	fc->arq->psize = PAGE_SIZE;
624 	fc->ars->psize = PAGE_SIZE;
625 	fc->atq->psize = 0;
626 	fc->ats->psize = 0;
627 
628 
629 	fc->arq->buf = NULL;
630 	fc->ars->buf = NULL;
631 	fc->atq->buf = NULL;
632 	fc->ats->buf = NULL;
633 
634 	fc->arq->flag = FWXFERQ_PACKET;
635 	fc->ars->flag = FWXFERQ_PACKET;
636 	fc->atq->flag = FWXFERQ_PACKET;
637 	fc->ats->flag = FWXFERQ_PACKET;
638 
639 	STAILQ_INIT(&fc->atq->q);
640 	STAILQ_INIT(&fc->ats->q);
641 
642 	for( i = 0 ; i < fc->nisodma ; i ++ ){
643 		fc->it[i]->queued = 0;
644 		fc->ir[i]->queued = 0;
645 
646 		fc->it[i]->start = NULL;
647 		fc->ir[i]->start = NULL;
648 
649 		fc->it[i]->buf = NULL;
650 		fc->ir[i]->buf = NULL;
651 
652 		fc->it[i]->flag = FWXFERQ_STREAM;
653 		fc->ir[i]->flag = FWXFERQ_STREAM;
654 
655 		STAILQ_INIT(&fc->it[i]->q);
656 		STAILQ_INIT(&fc->ir[i]->q);
657 
658 		STAILQ_INIT(&fc->it[i]->binds);
659 		STAILQ_INIT(&fc->ir[i]->binds);
660 	}
661 
662 	fc->arq->maxq = FWMAXQUEUE;
663 	fc->ars->maxq = FWMAXQUEUE;
664 	fc->atq->maxq = FWMAXQUEUE;
665 	fc->ats->maxq = FWMAXQUEUE;
666 
667 	for( i = 0 ; i < fc->nisodma ; i++){
668 		fc->ir[i]->maxq = FWMAXQUEUE;
669 		fc->it[i]->maxq = FWMAXQUEUE;
670 	}
671 /* Initialize csr registers */
672 	fc->topology_map = (struct fw_topology_map *)malloc(
673 				sizeof(struct fw_topology_map),
674 				M_FW, M_NOWAIT | M_ZERO);
675 	fc->speed_map = (struct fw_speed_map *)malloc(
676 				sizeof(struct fw_speed_map),
677 				M_FW, M_NOWAIT | M_ZERO);
678 	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
679 	CSRARC(fc, TOPO_MAP + 4) = 1;
680 	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
681 	CSRARC(fc, SPED_MAP + 4) = 1;
682 
683 	STAILQ_INIT(&fc->devices);
684 	STAILQ_INIT(&fc->pending);
685 
686 /* Initialize csr ROM work space */
687 	SLIST_INIT(&fc->ongocsr);
688 	SLIST_INIT(&fc->csrfree);
689 	for( i = 0 ; i < FWMAXCSRDIR ; i++){
690 		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
691 		if(csrd == NULL) break;
692 		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
693 	}
694 
695 /* Initialize Async handlers */
696 	STAILQ_INIT(&fc->binds);
697 	for( i = 0 ; i < 0x40 ; i++){
698 		STAILQ_INIT(&fc->tlabels[i]);
699 	}
700 
701 /* DV depend CSRs see blue book */
702 #if 0
703 	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
704 	CSRARC(fc, oPCR) = 0x8000007a;
705 	for(i = 4 ; i < 0x7c/4 ; i+=4){
706 		CSRARC(fc, i + oPCR) = 0x8000007a;
707 	}
708 
709 	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
710 	CSRARC(fc, iPCR) = 0x803f0000;
711 	for(i = 4 ; i < 0x7c/4 ; i+=4){
712 		CSRARC(fc, i + iPCR) = 0x0;
713 	}
714 #endif
715 
716 
717 #ifdef FW_VMACCESS
718 	xfer = fw_xfer_alloc();
719 	if(xfer == NULL) return;
720 
721 	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
722 	if(fwb == NULL){
723 		fw_xfer_free(xfer);
724 	}
725 	xfer->act.hand = fw_vmaccess;
726 	xfer->act_type = FWACT_XFER;
727 	xfer->fc = fc;
728 	xfer->sc = NULL;
729 
730 	fwb->start_hi = 0x2;
731 	fwb->start_lo = 0;
732 	fwb->addrlen = 0xffffffff;
733 	fwb->xfer = xfer;
734 	fw_bindadd(fc, fwb);
735 #endif
736 }
737 
738 /*
739  * To lookup binded process from IEEE1394 address.
740  */
741 struct fw_bind *
742 fw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
743 {
744 	struct fw_bind *tfw;
745 	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
746 		tfw = STAILQ_NEXT(tfw, fclist)){
747 		if(tfw->xfer->act_type != FWACT_NULL &&
748 			tfw->start_hi == dest_hi &&
749 			tfw->start_lo <= dest_lo &&
750 			(tfw->start_lo + tfw->addrlen) > dest_lo){
751 			return(tfw);
752 		}
753 	}
754 	return(NULL);
755 }
756 
757 /*
758  * To bind IEEE1394 address block to process.
759  */
760 int
761 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
762 {
763 	struct fw_bind *tfw, *tfw2 = NULL;
764 	int err = 0;
765 	tfw = STAILQ_FIRST(&fc->binds);
766 	if(tfw == NULL){
767 		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
768 		goto out;
769 	}
770 	if((tfw->start_hi > fwb->start_hi) ||
771 		(tfw->start_hi == fwb->start_hi &&
772 		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
773 		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
774 		goto out;
775 	}
776 	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
777 		if((tfw->start_hi < fwb->start_hi) ||
778 		   (tfw->start_hi == fwb->start_hi &&
779 		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
780 		   tfw2 = STAILQ_NEXT(tfw, fclist);
781 			if(tfw2 == NULL)
782 				break;
783 			if((tfw2->start_hi > fwb->start_hi) ||
784 			   (tfw2->start_hi == fwb->start_hi &&
785 			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
786 				break;
787 			}else{
788 				err = EBUSY;
789 				goto out;
790 			}
791 		}
792 	}
793 	if(tfw != NULL){
794 		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
795 	}else{
796 		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
797 	}
798 out:
799 	if(!err && fwb->xfer->act_type == FWACT_CH){
800 		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
801 	}
802 	return err;
803 }
804 
805 /*
806  * To free IEEE1394 address block.
807  */
808 int
809 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
810 {
811 	int s;
812 
813 	s = splfw();
814 	/* shall we check the existance? */
815 	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
816 	splx(s);
817 	if (fwb->xfer)
818 		fw_xfer_free(fwb->xfer);
819 
820 	return 0;
821 }
822 
823 /*
824  * To free transaction label.
825  */
826 static void
827 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
828 {
829 	struct tlabel *tl;
830 	int s = splfw();
831 
832 	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
833 		tl = STAILQ_NEXT(tl, link)){
834 		if(tl->xfer == xfer){
835 			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
836 			free(tl, M_FW);
837 			splx(s);
838 			return;
839 		}
840 	}
841 	splx(s);
842 	return;
843 }
844 
845 /*
846  * To obtain XFER structure by transaction label.
847  */
848 static struct fw_xfer *
849 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
850 {
851 	struct fw_xfer *xfer;
852 	struct tlabel *tl;
853 	int s = splfw();
854 
855 	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
856 		tl = STAILQ_NEXT(tl, link)){
857 		if(tl->xfer->dst == node){
858 			xfer = tl->xfer;
859 			splx(s);
860 			if (firewire_debug > 2)
861 				printf("fw_tl2xfer: found tl=%d\n", tlabel);
862 			return(xfer);
863 		}
864 	}
865 	if (firewire_debug > 1)
866 		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
867 	splx(s);
868 	return(NULL);
869 }
870 
871 /*
872  * To allocate IEEE1394 XFER structure.
873  */
874 struct fw_xfer *
875 fw_xfer_alloc(struct malloc_type *type)
876 {
877 	struct fw_xfer *xfer;
878 
879 	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
880 	if (xfer == NULL)
881 		return xfer;
882 
883 	microtime(&xfer->tv);
884 	xfer->sub = -1;
885 	xfer->malloc = type;
886 
887 	return xfer;
888 }
889 
890 /*
891  * IEEE1394 XFER post process.
892  */
893 void
894 fw_xfer_done(struct fw_xfer *xfer)
895 {
896 	if (xfer->act.hand == NULL)
897 		return;
898 
899 #if XFER_TIMEOUT
900 	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
901 #endif
902 
903 	if (xfer->fc->status != FWBUSRESET)
904 		xfer->act.hand(xfer);
905 	else {
906 		printf("fw_xfer_done: pending\n");
907 		if (xfer->fc != NULL)
908 			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
909 		else
910 			panic("fw_xfer_done: why xfer->fc is NULL?");
911 	}
912 }
913 
914 /*
915  * To free IEEE1394 XFER structure.
916  */
917 void
918 fw_xfer_free( struct fw_xfer* xfer)
919 {
920 	int s;
921 	if(xfer == NULL ) return;
922 	if(xfer->state == FWXF_INQ){
923 		printf("fw_xfer_free FWXF_INQ\n");
924 		s = splfw();
925 		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
926 		xfer->q->queued --;
927 		splx(s);
928 	}
929 	if(xfer->fc != NULL){
930 		if(xfer->state == FWXF_START){
931 #if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
932 			printf("fw_xfer_free FWXF_START\n");
933 #endif
934 			s = splfw();
935 			xfer->q->drain(xfer->fc, xfer);
936 			splx(s);
937 		}
938 	}
939 	if(xfer->send.buf != NULL){
940 		free(xfer->send.buf, M_FW);
941 	}
942 	if(xfer->recv.buf != NULL){
943 		free(xfer->recv.buf, M_FW);
944 	}
945 	if(xfer->fc != NULL){
946 		fw_tl_free(xfer->fc, xfer);
947 	}
948 	free(xfer, xfer->malloc);
949 }
950 
951 static void
952 fw_asy_callback_free(struct fw_xfer *xfer)
953 {
954 #if 0
955 	printf("asyreq done state=%d resp=%d\n",
956 				xfer->state, xfer->resp);
957 #endif
958 	fw_xfer_free(xfer);
959 }
960 
961 /*
962  * To configure PHY.
963  */
964 static void
965 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
966 {
967 	struct fw_xfer *xfer;
968 	struct fw_pkt *fp;
969 
970 	fc->status = FWBUSPHYCONF;
971 
972 #if 0
973 	DELAY(100000);
974 #endif
975 	xfer = fw_xfer_alloc(M_FWXFER);
976 	xfer->send.len = 12;
977 	xfer->send.off = 0;
978 	xfer->fc = fc;
979 	xfer->retry_req = fw_asybusy;
980 	xfer->act.hand = fw_asy_callback_free;
981 
982 	xfer->send.buf = malloc(sizeof(u_int32_t),
983 					M_FW, M_NOWAIT | M_ZERO);
984 	fp = (struct fw_pkt *)xfer->send.buf;
985 	fp->mode.ld[1] = 0;
986 	if (root_node >= 0)
987 		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
988 	if (gap_count >= 0)
989 		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
990 	fp->mode.ld[2] = ~fp->mode.ld[1];
991 /* XXX Dangerous, how to pass PHY packet to device driver */
992 	fp->mode.common.tcode |= FWTCODE_PHY;
993 
994 	if (firewire_debug)
995 		printf("send phy_config root_node=%d gap_count=%d\n",
996 						root_node, gap_count);
997 	fw_asyreq(fc, -1, xfer);
998 }
999 
1000 #if 0
1001 /*
1002  * Dump self ID.
1003  */
1004 static void
1005 fw_print_sid(u_int32_t sid)
1006 {
1007 	union fw_self_id *s;
1008 	s = (union fw_self_id *) &sid;
1009 	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1010 		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1011 		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1012 		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1013 		s->p0.power_class, s->p0.port0, s->p0.port1,
1014 		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1015 }
1016 #endif
1017 
1018 /*
1019  * To receive self ID.
1020  */
1021 void fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1022 {
1023 	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1024 	union fw_self_id *self_id;
1025 	u_int i, j, node, c_port = 0, i_branch = 0;
1026 
1027 	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1028 	fc->status = FWBUSINIT;
1029 	fc->max_node = fc->nodeid & 0x3f;
1030 	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1031 	fc->status = FWBUSCYMELECT;
1032 	fc->topology_map->crc_len = 2;
1033 	fc->topology_map->generation ++;
1034 	fc->topology_map->self_id_count = 0;
1035 	fc->topology_map->node_count = 0;
1036 	fc->speed_map->generation ++;
1037 	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1038 	self_id = &fc->topology_map->self_id[0];
1039 	for(i = 0; i < fc->sid_cnt; i ++){
1040 		if (sid[1] != ~sid[0]) {
1041 			printf("fw_sidrcv: invalid self-id packet\n");
1042 			sid += 2;
1043 			continue;
1044 		}
1045 		*self_id = *((union fw_self_id *)sid);
1046 		fc->topology_map->crc_len++;
1047 		if(self_id->p0.sequel == 0){
1048 			fc->topology_map->node_count ++;
1049 			c_port = 0;
1050 #if 0
1051 			fw_print_sid(sid[0]);
1052 #endif
1053 			node = self_id->p0.phy_id;
1054 			if(fc->max_node < node){
1055 				fc->max_node = self_id->p0.phy_id;
1056 			}
1057 			/* XXX I'm not sure this is the right speed_map */
1058 			fc->speed_map->speed[node][node]
1059 					= self_id->p0.phy_speed;
1060 			for (j = 0; j < node; j ++) {
1061 				fc->speed_map->speed[j][node]
1062 					= fc->speed_map->speed[node][j]
1063 					= min(fc->speed_map->speed[j][j],
1064 							self_id->p0.phy_speed);
1065 			}
1066 			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1067 			  (self_id->p0.link_active && self_id->p0.contender)) {
1068 				fc->irm = self_id->p0.phy_id;
1069 			}
1070 			if(self_id->p0.port0 >= 0x2){
1071 				c_port++;
1072 			}
1073 			if(self_id->p0.port1 >= 0x2){
1074 				c_port++;
1075 			}
1076 			if(self_id->p0.port2 >= 0x2){
1077 				c_port++;
1078 			}
1079 		}
1080 		if(c_port > 2){
1081 			i_branch += (c_port - 2);
1082 		}
1083 		sid += 2;
1084 		self_id++;
1085 		fc->topology_map->self_id_count ++;
1086 	}
1087 	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1088 	/* CRC */
1089 	fc->topology_map->crc = fw_crc16(
1090 			(u_int32_t *)&fc->topology_map->generation,
1091 			fc->topology_map->crc_len * 4);
1092 	fc->speed_map->crc = fw_crc16(
1093 			(u_int32_t *)&fc->speed_map->generation,
1094 			fc->speed_map->crc_len * 4);
1095 	/* byteswap and copy to CSR */
1096 	p = (u_int32_t *)fc->topology_map;
1097 	for (i = 0; i <= fc->topology_map->crc_len; i++)
1098 		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1099 	p = (u_int32_t *)fc->speed_map;
1100 	CSRARC(fc, SPED_MAP) = htonl(*p++);
1101 	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1102 	/* don't byte-swap u_int8_t array */
1103 	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1104 
1105 	fc->max_hop = fc->max_node - i_branch;
1106 #if 1
1107 	printf(", maxhop <= %d", fc->max_hop);
1108 #endif
1109 
1110 	if(fc->irm == -1 ){
1111 		printf(", Not found IRM capable node");
1112 	}else{
1113 		printf(", cable IRM = %d", fc->irm);
1114 		if (fc->irm == fc->nodeid)
1115 			printf(" (me)");
1116 	}
1117 	printf("\n");
1118 
1119 	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1120 		if (fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)) {
1121 			fc->status = FWBUSMGRDONE;
1122 			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1123 		} else {
1124 			fc->status = FWBUSMGRELECT;
1125 			callout_reset(&fc->bmr_callout, hz/8,
1126 				(void *)fw_try_bmr, (void *)fc);
1127 		}
1128 	} else {
1129 		fc->status = FWBUSMGRDONE;
1130 #if 0
1131 		device_printf(fc->bdev, "BMR = %x\n",
1132 				CSRARC(fc, BUS_MGR_ID));
1133 #endif
1134 	}
1135 	free(buf, M_FW);
1136 	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1137 		/* I am BMGR */
1138 		fw_bmr(fc);
1139 	}
1140 	callout_reset(&fc->busprobe_callout, hz/4,
1141 			(void *)fw_bus_probe, (void *)fc);
1142 }
1143 
1144 /*
1145  * To probe devices on the IEEE1394 bus.
1146  */
1147 static void
1148 fw_bus_probe(struct firewire_comm *fc)
1149 {
1150 	int s;
1151 	struct fw_device *fwdev, *next;
1152 
1153 	s = splfw();
1154 	fc->status = FWBUSEXPLORE;
1155 	fc->retry_count = 0;
1156 
1157 /*
1158  * Invalidate all devices, just after bus reset. Devices
1159  * to be removed has not been seen longer time.
1160  */
1161 	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1162 		next = STAILQ_NEXT(fwdev, link);
1163 		if (fwdev->status != FWDEVINVAL) {
1164 			fwdev->status = FWDEVINVAL;
1165 			fwdev->rcnt = 0;
1166 		} else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1167 			fwdev->rcnt ++;
1168 		} else {
1169 			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1170 			free(fwdev, M_FW);
1171 		}
1172 	}
1173 	fc->ongonode = 0;
1174 	fc->ongoaddr = CSRROMOFF;
1175 	fc->ongodev = NULL;
1176 	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1177 	fw_bus_explore(fc);
1178 	splx(s);
1179 }
1180 
1181 /*
1182  * To collect device informations on the IEEE1394 bus.
1183  */
1184 static void
1185 fw_bus_explore(struct firewire_comm *fc )
1186 {
1187 	int err = 0;
1188 	struct fw_device *fwdev, *pfwdev, *tfwdev;
1189 	u_int32_t addr;
1190 	struct fw_xfer *xfer;
1191 	struct fw_pkt *fp;
1192 
1193 	if(fc->status != FWBUSEXPLORE)
1194 		return;
1195 
1196 loop:
1197 	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1198 
1199 	if(fc->ongonode > fc->max_node) goto done;
1200 	if(fc->ongonode >= 0x3f) goto done;
1201 
1202 	/* check link */
1203 	/* XXX we need to check phy_id first */
1204 	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1205 		if (firewire_debug)
1206 			printf("node%d: link down\n", fc->ongonode);
1207 		fc->ongonode++;
1208 		goto loop;
1209 	}
1210 
1211 	if(fc->ongoaddr <= CSRROMOFF &&
1212 		fc->ongoeui.hi == 0xffffffff &&
1213 		fc->ongoeui.lo == 0xffffffff ){
1214 		fc->ongoaddr = CSRROMOFF;
1215 		addr = 0xf0000000 | fc->ongoaddr;
1216 	}else if(fc->ongoeui.hi == 0xffffffff ){
1217 		fc->ongoaddr = CSRROMOFF + 0xc;
1218 		addr = 0xf0000000 | fc->ongoaddr;
1219 	}else if(fc->ongoeui.lo == 0xffffffff ){
1220 		fc->ongoaddr = CSRROMOFF + 0x10;
1221 		addr = 0xf0000000 | fc->ongoaddr;
1222 	}else if(fc->ongodev == NULL){
1223 		STAILQ_FOREACH(fwdev, &fc->devices, link)
1224 			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1225 				break;
1226 		if(fwdev != NULL){
1227 			fwdev->dst = fc->ongonode;
1228 			fwdev->status = FWDEVATTACHED;
1229 			fc->ongonode++;
1230 			fc->ongoaddr = CSRROMOFF;
1231 			fc->ongodev = NULL;
1232 			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1233 			goto loop;
1234 		}
1235 		fwdev = malloc(sizeof(struct fw_device), M_FW, M_NOWAIT);
1236 		if(fwdev == NULL)
1237 			return;
1238 		fwdev->fc = fc;
1239 		fwdev->rommax = 0;
1240 		fwdev->dst = fc->ongonode;
1241 		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1242 		fwdev->status = FWDEVINIT;
1243 #if 0
1244 		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1245 			>> ((3 - (fc->ongonode % 4)) * 8);
1246 #else
1247 		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1248 #endif
1249 
1250 		pfwdev = NULL;
1251 		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1252 			if (tfwdev->eui.hi > fwdev->eui.hi ||
1253 					(tfwdev->eui.hi == fwdev->eui.hi &&
1254 					tfwdev->eui.lo > fwdev->eui.lo))
1255 				break;
1256 			pfwdev = tfwdev;
1257 		}
1258 		if (pfwdev == NULL)
1259 			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1260 		else
1261 			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1262 
1263 		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1264 			linkspeed[fwdev->speed],
1265 			fc->ongoeui.hi, fc->ongoeui.lo);
1266 
1267 		fc->ongodev = fwdev;
1268 		fc->ongoaddr = CSRROMOFF;
1269 		addr = 0xf0000000 | fc->ongoaddr;
1270 	}else{
1271 		addr = 0xf0000000 | fc->ongoaddr;
1272 	}
1273 #if 0
1274 	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1275 		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1276 		fw_bus_explore_callback);
1277 	if(xfer == NULL) goto done;
1278 #else
1279 	xfer = fw_xfer_alloc(M_FWXFER);
1280 	if(xfer == NULL){
1281 		goto done;
1282 	}
1283 	xfer->send.len = 16;
1284 	xfer->spd = 0;
1285 	xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1286 	if(xfer->send.buf == NULL){
1287 		fw_xfer_free( xfer);
1288 		return;
1289 	}
1290 
1291 	xfer->send.off = 0;
1292 	fp = (struct fw_pkt *)xfer->send.buf;
1293 	fp->mode.rreqq.dest_hi = htons(0xffff);
1294 	fp->mode.rreqq.tlrt = 0;
1295 	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1296 	fp->mode.rreqq.pri = 0;
1297 	fp->mode.rreqq.src = 0;
1298 	xfer->dst = FWLOCALBUS | fc->ongonode;
1299 	fp->mode.rreqq.dst = htons(xfer->dst);
1300 	fp->mode.rreqq.dest_lo = htonl(addr);
1301 	xfer->act.hand = fw_bus_explore_callback;
1302 
1303 	if (firewire_debug)
1304 		printf("node%d: explore addr=0x%x\n",
1305 				fc->ongonode, fc->ongoaddr);
1306 	err = fw_asyreq(fc, -1, xfer);
1307 	if(err){
1308 		fw_xfer_free( xfer);
1309 		return;
1310 	}
1311 #endif
1312 	return;
1313 done:
1314 	/* fw_attach_devs */
1315 	fc->status = FWBUSEXPDONE;
1316 	if (firewire_debug)
1317 		printf("bus_explore done\n");
1318 	fw_attach_dev(fc);
1319 	return;
1320 
1321 }
1322 
1323 /* Portable Async. request read quad */
1324 struct fw_xfer *
1325 asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1326 	u_int32_t addr_hi, u_int32_t addr_lo,
1327 	void (*hand) __P((struct fw_xfer*)))
1328 {
1329 	struct fw_xfer *xfer;
1330 	struct fw_pkt *fp;
1331 	int err;
1332 
1333 	xfer = fw_xfer_alloc(M_FWXFER);
1334 	if(xfer == NULL){
1335 		return NULL;
1336 	}
1337 	xfer->send.len = 16;
1338 	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1339 	xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1340 	if(xfer->send.buf == NULL){
1341 		fw_xfer_free( xfer);
1342 		return NULL;
1343 	}
1344 
1345 	xfer->send.off = 0;
1346 	fp = (struct fw_pkt *)xfer->send.buf;
1347 	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1348 	if(tl & FWP_TL_VALID){
1349 		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1350 	}else{
1351 		fp->mode.rreqq.tlrt = 0;
1352 	}
1353 	fp->mode.rreqq.tlrt |= rt & 0x3;
1354 	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1355 	fp->mode.rreqq.pri = 0;
1356 	fp->mode.rreqq.src = 0;
1357 	xfer->dst = addr_hi >> 16;
1358 	fp->mode.rreqq.dst = htons(xfer->dst);
1359 	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1360 	xfer->act.hand = hand;
1361 
1362 	err = fw_asyreq(fc, -1, xfer);
1363 	if(err){
1364 		fw_xfer_free( xfer);
1365 		return NULL;
1366 	}
1367 	return xfer;
1368 }
1369 
1370 /*
1371  * Callback for the IEEE1394 bus information collection.
1372  */
1373 static void
1374 fw_bus_explore_callback(struct fw_xfer *xfer)
1375 {
1376 	struct firewire_comm *fc;
1377 	struct fw_pkt *sfp,*rfp;
1378 	struct csrhdr *chdr;
1379 	struct csrdir *csrd;
1380 	struct csrreg *csrreg;
1381 	u_int32_t offset;
1382 
1383 
1384 	if(xfer == NULL) {
1385 		printf("xfer == NULL\n");
1386 		return;
1387 	}
1388 	fc = xfer->fc;
1389 
1390 	if (firewire_debug)
1391 		printf("node%d: callback addr=0x%x\n",
1392 			fc->ongonode, fc->ongoaddr);
1393 
1394 	if(xfer->resp != 0){
1395 		printf("node%d: resp=%d addr=0x%x\n",
1396 			fc->ongonode, xfer->resp, fc->ongoaddr);
1397 		fc->retry_count++;
1398 		goto nextnode;
1399 	}
1400 
1401 	if(xfer->send.buf == NULL){
1402 		printf("node%d: send.buf=NULL addr=0x%x\n",
1403 			fc->ongonode, fc->ongoaddr);
1404 		fc->retry_count++;
1405 		goto nextnode;
1406 	}
1407 	sfp = (struct fw_pkt *)xfer->send.buf;
1408 
1409 	if(xfer->recv.buf == NULL){
1410 		printf("node%d: recv.buf=NULL addr=0x%x\n",
1411 			fc->ongonode, fc->ongoaddr);
1412 		fc->retry_count++;
1413 		goto nextnode;
1414 	}
1415 	rfp = (struct fw_pkt *)xfer->recv.buf;
1416 #if 0
1417 	{
1418 		u_int32_t *qld;
1419 		int i;
1420 		qld = (u_int32_t *)xfer->recv.buf;
1421 		printf("len:%d\n", xfer->recv.len);
1422 		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1423 			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1424 			if((i % 16) == 15) printf("\n");
1425 		}
1426 		if((i % 16) != 15) printf("\n");
1427 	}
1428 #endif
1429 	if(fc->ongodev == NULL){
1430 		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1431 			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1432 			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1433 /* If CSR is minimal confinguration, more investgation is not needed. */
1434 			if(chdr->info_len == 1){
1435 				if (firewire_debug)
1436 					printf("node%d: minimal config\n",
1437 								fc->ongonode);
1438 				goto nextnode;
1439 			}else{
1440 				fc->ongoaddr = CSRROMOFF + 0xc;
1441 			}
1442 		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1443 			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1444 			fc->ongoaddr = CSRROMOFF + 0x10;
1445 		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1446 			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1447 			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1448 				if (firewire_debug)
1449 					printf("node%d: eui64 is zero.\n",
1450 							fc->ongonode);
1451 				goto nextnode;
1452 			}
1453 			fc->ongoaddr = CSRROMOFF;
1454 		}
1455 	}else{
1456 		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1457 		if(fc->ongoaddr > fc->ongodev->rommax){
1458 			fc->ongodev->rommax = fc->ongoaddr;
1459 		}
1460 		csrd = SLIST_FIRST(&fc->ongocsr);
1461 		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1462 			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1463 			offset = CSRROMOFF;
1464 		}else{
1465 			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1466 			offset = csrd->off;
1467 		}
1468 		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1469 			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1470 			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1471 				csrd = SLIST_FIRST(&fc->csrfree);
1472 				if(csrd == NULL){
1473 					goto nextnode;
1474 				}else{
1475 					csrd->ongoaddr = fc->ongoaddr;
1476 					fc->ongoaddr += csrreg->val * 4;
1477 					csrd->off = fc->ongoaddr;
1478 					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1479 					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1480 					goto nextaddr;
1481 				}
1482 			}
1483 		}
1484 		fc->ongoaddr += 4;
1485 		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1486 				(fc->ongodev->rommax < 0x414)){
1487 			if(fc->ongodev->rommax <= 0x414){
1488 				csrd = SLIST_FIRST(&fc->csrfree);
1489 				if(csrd == NULL) goto nextnode;
1490 				csrd->off = fc->ongoaddr;
1491 				csrd->ongoaddr = fc->ongoaddr;
1492 				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1493 				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1494 			}
1495 			goto nextaddr;
1496 		}
1497 
1498 		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1499 			if(csrd == NULL){
1500 				goto nextnode;
1501 			};
1502 			fc->ongoaddr = csrd->ongoaddr + 4;
1503 			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1504 			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1505 			csrd = SLIST_FIRST(&fc->ongocsr);
1506 			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1507 				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1508 				offset = CSRROMOFF;
1509 			}else{
1510 				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1511 				offset = csrd->off;
1512 			}
1513 		}
1514 		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1515 			goto nextnode;
1516 		}
1517 	}
1518 nextaddr:
1519 	fw_xfer_free( xfer);
1520 	fw_bus_explore(fc);
1521 	return;
1522 nextnode:
1523 	fw_xfer_free( xfer);
1524 	fc->ongonode++;
1525 /* housekeeping work space */
1526 	fc->ongoaddr = CSRROMOFF;
1527 	fc->ongodev = NULL;
1528 	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1529 	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1530 		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1531 		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1532 	}
1533 	fw_bus_explore(fc);
1534 	return;
1535 }
1536 
1537 /*
1538  * To obtain CSR register values.
1539  */
1540 u_int32_t
1541 getcsrdata(struct fw_device *fwdev, u_int8_t key)
1542 {
1543 	int i;
1544 	struct csrhdr *chdr;
1545 	struct csrreg *creg;
1546 	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1547 	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1548 		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1549 		if(creg->key == key){
1550 			return (u_int32_t)creg->val;
1551 		}
1552 	}
1553 	return 0;
1554 }
1555 
1556 /*
1557  * To attach sub-devices layer onto IEEE1394 bus.
1558  */
1559 static void
1560 fw_attach_dev(struct firewire_comm *fc)
1561 {
1562 	struct fw_device *fwdev;
1563 	struct fw_xfer *xfer;
1564 	int i, err;
1565 	device_t *devlistp;
1566 	int devcnt;
1567 	struct firewire_dev_comm *fdc;
1568 	u_int32_t spec, ver;
1569 
1570 	STAILQ_FOREACH(fwdev, &fc->devices, link) {
1571 		if(fwdev->status == FWDEVINIT){
1572 			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1573 			if(spec == 0)
1574 				continue;
1575 			ver = getcsrdata(fwdev, CSRKEY_VER);
1576 			if(ver == 0)
1577 				continue;
1578 			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1579 
1580 			device_printf(fc->bdev, "Device ");
1581 			switch(spec){
1582 			case CSRVAL_ANSIT10:
1583 				switch(ver){
1584 				case CSRVAL_T10SBP2:
1585 					printf("SBP-II");
1586 					break;
1587 				default:
1588 					break;
1589 				}
1590 				break;
1591 			case CSRVAL_1394TA:
1592 				switch(ver){
1593 				case CSR_PROTAVC:
1594 					printf("AV/C");
1595 					break;
1596 				case CSR_PROTCAL:
1597 					printf("CAL");
1598 					break;
1599 				case CSR_PROTEHS:
1600 					printf("EHS");
1601 					break;
1602 				case CSR_PROTHAVI:
1603 					printf("HAVi");
1604 					break;
1605 				case CSR_PROTCAM104:
1606 					printf("1394 Cam 1.04");
1607 					break;
1608 				case CSR_PROTCAM120:
1609 					printf("1394 Cam 1.20");
1610 					break;
1611 				case CSR_PROTCAM130:
1612 					printf("1394 Cam 1.30");
1613 					break;
1614 				case CSR_PROTDPP:
1615 					printf("1394 Direct print");
1616 					break;
1617 				case CSR_PROTIICP:
1618 					printf("Industrial & Instrument");
1619 					break;
1620 				default:
1621 					printf("unknown 1394TA");
1622 					break;
1623 				}
1624 				break;
1625 			default:
1626 				printf("unknown spec");
1627 				break;
1628 			}
1629 			fwdev->status = FWDEVATTACHED;
1630 			printf("\n");
1631 		}
1632 	}
1633 	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1634 	if( err != 0 )
1635 		return;
1636 	for( i = 0 ; i < devcnt ; i++){
1637 		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1638 			fdc = device_get_softc(devlistp[i]);
1639 			if (fdc->post_explore != NULL)
1640 				fdc->post_explore(fdc);
1641 		}
1642 	}
1643 	free(devlistp, M_TEMP);
1644 
1645 	/* call pending handlers */
1646 	i = 0;
1647 	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1648 		STAILQ_REMOVE_HEAD(&fc->pending, link);
1649 		i++;
1650 		if (xfer->act.hand)
1651 			xfer->act.hand(xfer);
1652 	}
1653 	if (i > 0)
1654 		printf("fw_attach_dev: %d pending handlers called\n", i);
1655 	if (fc->retry_count > 0) {
1656 		printf("probe failed for %d node\n", fc->retry_count);
1657 #if 0
1658 		callout_reset(&fc->retry_probe_callout, hz*2,
1659 					(void *)fc->ibr, (void *)fc);
1660 #endif
1661 	}
1662 	return;
1663 }
1664 
1665 /*
1666  * To allocate uniq transaction label.
1667  */
1668 static int
1669 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1670 {
1671 	u_int i;
1672 	struct tlabel *tl, *tmptl;
1673 	int s;
1674 	static u_int32_t label = 0;
1675 
1676 	s = splfw();
1677 	for( i = 0 ; i < 0x40 ; i ++){
1678 		label = (label + 1) & 0x3f;
1679 		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1680 			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1681 			if(tmptl->xfer->dst == xfer->dst) break;
1682 		}
1683 		if(tmptl == NULL) {
1684 			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1685 			if (tl == NULL) {
1686 				splx(s);
1687 				return (-1);
1688 			}
1689 			tl->xfer = xfer;
1690 			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1691 			splx(s);
1692 			if (firewire_debug > 1)
1693 				printf("fw_get_tlabel: dst=%d tl=%d\n",
1694 						xfer->dst, label);
1695 			return(label);
1696 		}
1697 	}
1698 	splx(s);
1699 
1700 	printf("fw_get_tlabel: no free tlabel\n");
1701 	return(-1);
1702 }
1703 
1704 /*
1705  * Generic packet receving process.
1706  */
1707 void
1708 fw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1709 {
1710 	struct fw_pkt *fp, *resfp;
1711 	struct fw_xfer *xfer;
1712 	struct fw_bind *bind;
1713 	struct firewire_softc *sc;
1714 	int s;
1715 #if 0
1716 	{
1717 		u_int32_t *qld;
1718 		int i;
1719 		qld = (u_int32_t *)buf;
1720 		printf("spd %d len:%d\n", spd, len);
1721 		for( i = 0 ; i <= len && i < 32; i+= 4){
1722 			printf("0x%08x ", ntohl(qld[i/4]));
1723 			if((i % 16) == 15) printf("\n");
1724 		}
1725 		if((i % 16) != 15) printf("\n");
1726 	}
1727 #endif
1728 	fp = (struct fw_pkt *)(buf + off);
1729 	switch(fp->mode.common.tcode){
1730 	case FWTCODE_WRES:
1731 	case FWTCODE_RRESQ:
1732 	case FWTCODE_RRESB:
1733 	case FWTCODE_LRES:
1734 		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1735 					fp->mode.hdr.tlrt >> 2);
1736 		if(xfer == NULL) {
1737 			printf("fw_rcv: unknown response "
1738 					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1739 					fp->mode.common.tcode,
1740 					ntohs(fp->mode.hdr.src),
1741 					fp->mode.hdr.tlrt >> 2,
1742 					fp->mode.hdr.tlrt & 3,
1743 					fp->mode.rresq.data);
1744 #if 1
1745 			printf("try ad-hoc work around!!\n");
1746 			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1747 					(fp->mode.hdr.tlrt >> 2)^3);
1748 			if (xfer == NULL) {
1749 				printf("no use...\n");
1750 				goto err;
1751 			}
1752 #else
1753 			goto err;
1754 #endif
1755 		}
1756 		switch(xfer->act_type){
1757 		case FWACT_XFER:
1758 			if((xfer->sub >= 0) &&
1759 				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1760 				xfer->resp = EINVAL;
1761 				fw_xfer_done(xfer);
1762 				goto err;
1763 			}
1764 			xfer->recv.len = len;
1765 			xfer->recv.off = off;
1766 			xfer->recv.buf = buf;
1767 			xfer->resp = 0;
1768 			fw_xfer_done(xfer);
1769 			return;
1770 			break;
1771 		case FWACT_CH:
1772 		default:
1773 			goto err;
1774 			break;
1775 		}
1776 		break;
1777 	case FWTCODE_WREQQ:
1778 	case FWTCODE_WREQB:
1779 	case FWTCODE_RREQQ:
1780 	case FWTCODE_RREQB:
1781 	case FWTCODE_LREQ:
1782 		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1783 			ntohl(fp->mode.rreqq.dest_lo));
1784 		if(bind == NULL){
1785 #if __FreeBSD_version >= 500000
1786 			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n src=0x%x",
1787 #else
1788 			printf("Unknown service addr 0x%08x:0x%08lx tcode=%x src=0x%x\n",
1789 #endif
1790 				ntohs(fp->mode.rreqq.dest_hi),
1791 				ntohl(fp->mode.rreqq.dest_lo),
1792 				fp->mode.common.tcode,
1793 				fp->mode.hdr.src);
1794 			if (fc->status == FWBUSRESET) {
1795 				printf("fw_rcv: cannot respond(bus reset)!\n");
1796 				goto err;
1797 			}
1798 			xfer = fw_xfer_alloc(M_FWXFER);
1799 			if(xfer == NULL){
1800 				return;
1801 			}
1802 			xfer->spd = spd;
1803 			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1804 			resfp = (struct fw_pkt *)xfer->send.buf;
1805 			switch(fp->mode.common.tcode){
1806 			case FWTCODE_WREQQ:
1807 			case FWTCODE_WREQB:
1808 				resfp->mode.hdr.tcode = FWTCODE_WRES;
1809 				xfer->send.len = 12;
1810 				break;
1811 			case FWTCODE_RREQQ:
1812 				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1813 				xfer->send.len = 16;
1814 				break;
1815 			case FWTCODE_RREQB:
1816 				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1817 				xfer->send.len = 16;
1818 				break;
1819 			case FWTCODE_LREQ:
1820 				resfp->mode.hdr.tcode = FWTCODE_LRES;
1821 				xfer->send.len = 16;
1822 				break;
1823 			}
1824 			resfp->mode.hdr.dst = fp->mode.hdr.src;
1825 			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1826 			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1827 			resfp->mode.rresb.rtcode = 7;
1828 			resfp->mode.rresb.extcode = 0;
1829 			resfp->mode.rresb.len = 0;
1830 /*
1831 			xfer->act.hand = fw_asy_callback;
1832 */
1833 			xfer->act.hand = fw_xfer_free;
1834 			if(fw_asyreq(fc, -1, xfer)){
1835 				fw_xfer_free( xfer);
1836 				return;
1837 			}
1838 			goto err;
1839 		}
1840 		switch(bind->xfer->act_type){
1841 		case FWACT_XFER:
1842 			xfer = fw_xfer_alloc(M_FWXFER);
1843 			if(xfer == NULL) goto err;
1844 			xfer->fc = bind->xfer->fc;
1845 			xfer->sc = bind->xfer->sc;
1846 			xfer->recv.buf = buf;
1847 			xfer->recv.len = len;
1848 			xfer->recv.off = off;
1849 			xfer->spd = spd;
1850 			xfer->act.hand = bind->xfer->act.hand;
1851 			if (fc->status != FWBUSRESET)
1852 				xfer->act.hand(xfer);
1853 			else
1854 				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1855 			return;
1856 			break;
1857 		case FWACT_CH:
1858 			if(fc->ir[bind->xfer->sub]->queued >=
1859 				fc->ir[bind->xfer->sub]->maxq){
1860 				device_printf(fc->bdev,
1861 					"Discard a packet %x %d\n",
1862 					bind->xfer->sub,
1863 					fc->ir[bind->xfer->sub]->queued);
1864 				goto err;
1865 			}
1866 			xfer = fw_xfer_alloc(M_FWXFER);
1867 			if(xfer == NULL) goto err;
1868 			xfer->recv.buf = buf;
1869 			xfer->recv.len = len;
1870 			xfer->recv.off = off;
1871 			xfer->spd = spd;
1872 			s = splfw();
1873 			fc->ir[bind->xfer->sub]->queued++;
1874 			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1875 			splx(s);
1876 
1877 			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1878 
1879 			return;
1880 			break;
1881 		default:
1882 			goto err;
1883 			break;
1884 		}
1885 		break;
1886 	case FWTCODE_STREAM:
1887 	{
1888 		struct fw_xferq *xferq;
1889 
1890 		xferq = fc->ir[sub];
1891 #if 0
1892 		printf("stream rcv dma %d len %d off %d spd %d\n",
1893 			sub, len, off, spd);
1894 #endif
1895 		if(xferq->queued >= xferq->maxq) {
1896 			printf("receive queue is full\n");
1897 			goto err;
1898 		}
1899 		xfer = fw_xfer_alloc(M_FWXFER);
1900 		if(xfer == NULL) goto err;
1901 		xfer->recv.buf = buf;
1902 		xfer->recv.len = len;
1903 		xfer->recv.off = off;
1904 		xfer->spd = spd;
1905 		s = splfw();
1906 		xferq->queued++;
1907 		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1908 		splx(s);
1909 		sc = device_get_softc(fc->bdev);
1910 #if __FreeBSD_version >= 500000
1911 		if (SEL_WAITING(&xferq->rsel))
1912 #else
1913 		if (&xferq->rsel.si_pid != 0)
1914 #endif
1915 			selwakeup(&xferq->rsel);
1916 		if (xferq->flag & FWXFERQ_WAKEUP) {
1917 			xferq->flag &= ~FWXFERQ_WAKEUP;
1918 			wakeup((caddr_t)xferq);
1919 		}
1920 		if (xferq->flag & FWXFERQ_HANDLER) {
1921 			xferq->hand(xferq);
1922 		}
1923 		return;
1924 		break;
1925 	}
1926 	default:
1927 		printf("fw_rcv: unknow tcode\n");
1928 		break;
1929 	}
1930 err:
1931 	free(buf, M_FW);
1932 }
1933 
1934 /*
1935  * Post process for Bus Manager election process.
1936  */
1937 static void
1938 fw_try_bmr_callback(struct fw_xfer *xfer)
1939 {
1940 	struct fw_pkt *rfp;
1941 	struct firewire_comm *fc;
1942 	int bmr;
1943 
1944 	if (xfer == NULL)
1945 		return;
1946 	fc = xfer->fc;
1947 	if (xfer->resp != 0)
1948 		goto error;
1949 	if (xfer->send.buf == NULL)
1950 		goto error;
1951 	if (xfer->recv.buf == NULL)
1952 		goto error;
1953 	rfp = (struct fw_pkt *)xfer->recv.buf;
1954 	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1955 		goto error;
1956 
1957 	bmr = ntohl(rfp->mode.lres.payload[0]);
1958 	if (bmr == 0x3f)
1959 		bmr = fc->nodeid;
1960 
1961 	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1962 	device_printf(fc->bdev, "new bus manager %d ",
1963 		CSRARC(fc, BUS_MGR_ID));
1964 	if(bmr == fc->nodeid){
1965 		printf("(me)\n");
1966 		fw_bmr(fc);
1967 	}else{
1968 		printf("\n");
1969 	}
1970 error:
1971 	fw_xfer_free(xfer);
1972 }
1973 
1974 /*
1975  * To candidate Bus Manager election process.
1976  */
1977 static void
1978 fw_try_bmr(void *arg)
1979 {
1980 	struct fw_xfer *xfer;
1981 	struct firewire_comm *fc = (struct firewire_comm *)arg;
1982 	struct fw_pkt *fp;
1983 	int err = 0;
1984 
1985 	xfer = fw_xfer_alloc(M_FWXFER);
1986 	if(xfer == NULL){
1987 		return;
1988 	}
1989 	xfer->send.len = 24;
1990 	xfer->spd = 0;
1991 	xfer->send.buf = malloc(24, M_FW, M_NOWAIT);
1992 	if(xfer->send.buf == NULL){
1993 		fw_xfer_free( xfer);
1994 		return;
1995 	}
1996 
1997 	fc->status = FWBUSMGRELECT;
1998 
1999 	xfer->send.off = 0;
2000 	fp = (struct fw_pkt *)xfer->send.buf;
2001 	fp->mode.lreq.dest_hi = htons(0xffff);
2002 	fp->mode.lreq.tlrt = 0;
2003 	fp->mode.lreq.tcode = FWTCODE_LREQ;
2004 	fp->mode.lreq.pri = 0;
2005 	fp->mode.lreq.src = 0;
2006 	fp->mode.lreq.len = htons(8);
2007 	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
2008 	xfer->dst = FWLOCALBUS | fc->irm;
2009 	fp->mode.lreq.dst = htons(xfer->dst);
2010 	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
2011 	fp->mode.lreq.payload[0] = htonl(0x3f);
2012 	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
2013 	xfer->act_type = FWACT_XFER;
2014 	xfer->act.hand = fw_try_bmr_callback;
2015 
2016 	err = fw_asyreq(fc, -1, xfer);
2017 	if(err){
2018 		fw_xfer_free( xfer);
2019 		return;
2020 	}
2021 	return;
2022 }
2023 
2024 #ifdef FW_VMACCESS
2025 /*
2026  * Software implementation for physical memory block access.
2027  * XXX:Too slow, usef for debug purpose only.
2028  */
2029 static void
2030 fw_vmaccess(struct fw_xfer *xfer){
2031 	struct fw_pkt *rfp, *sfp = NULL;
2032 	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
2033 
2034 	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
2035 			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2036 	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2037 	if(xfer->resp != 0){
2038 		fw_xfer_free( xfer);
2039 		return;
2040 	}
2041 	if(xfer->recv.buf == NULL){
2042 		fw_xfer_free( xfer);
2043 		return;
2044 	}
2045 	rfp = (struct fw_pkt *)xfer->recv.buf;
2046 	switch(rfp->mode.hdr.tcode){
2047 		/* XXX need fix for 64bit arch */
2048 		case FWTCODE_WREQB:
2049 			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2050 			xfer->send.len = 12;
2051 			sfp = (struct fw_pkt *)xfer->send.buf;
2052 			bcopy(rfp->mode.wreqb.payload,
2053 				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2054 			sfp->mode.wres.tcode = FWTCODE_WRES;
2055 			sfp->mode.wres.rtcode = 0;
2056 			break;
2057 		case FWTCODE_WREQQ:
2058 			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2059 			xfer->send.len = 12;
2060 			sfp->mode.wres.tcode = FWTCODE_WRES;
2061 			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2062 			sfp->mode.wres.rtcode = 0;
2063 			break;
2064 		case FWTCODE_RREQB:
2065 			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2066 			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2067 			sfp = (struct fw_pkt *)xfer->send.buf;
2068 			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2069 				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2070 			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2071 			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2072 			sfp->mode.rresb.rtcode = 0;
2073 			sfp->mode.rresb.extcode = 0;
2074 			break;
2075 		case FWTCODE_RREQQ:
2076 			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2077 			xfer->send.len = 16;
2078 			sfp = (struct fw_pkt *)xfer->send.buf;
2079 			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2080 			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2081 			sfp->mode.rresb.rtcode = 0;
2082 			break;
2083 		default:
2084 			fw_xfer_free( xfer);
2085 			return;
2086 	}
2087 	xfer->send.off = 0;
2088 	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2089 	xfer->dst = ntohs(rfp->mode.hdr.src);
2090 	xfer->act.hand = fw_xfer_free;
2091 	xfer->retry_req = fw_asybusy;
2092 
2093 	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2094 	sfp->mode.hdr.pri = 0;
2095 
2096 	fw_asyreq(xfer->fc, -1, xfer);
2097 /**/
2098 	return;
2099 }
2100 #endif
2101 
2102 /*
2103  * CRC16 check-sum for IEEE1394 register blocks.
2104  */
2105 u_int16_t
2106 fw_crc16(u_int32_t *ptr, u_int32_t len){
2107 	u_int32_t i, sum, crc = 0;
2108 	int shift;
2109 	len = (len + 3) & ~3;
2110 	for(i = 0 ; i < len ; i+= 4){
2111 		for( shift = 28 ; shift >= 0 ; shift -= 4){
2112 			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2113 			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2114 		}
2115 		crc &= 0xffff;
2116 	}
2117 	return((u_int16_t) crc);
2118 }
2119 
2120 static int
2121 fw_bmr(struct firewire_comm *fc)
2122 {
2123 	struct fw_device fwdev;
2124 	int cmstr;
2125 
2126 	/* XXX Assume that the current root node is cycle master capable */
2127 	cmstr = fc->max_node;
2128 	/* If I am the bus manager, optimize gapcount */
2129 	if(fc->max_hop <= MAX_GAPHOP ){
2130 		fw_phy_config(fc, (fc->max_node > 0)?cmstr:-1,
2131 						gap_cnt[fc->max_hop]);
2132 	}
2133 	/* If we are the cycle master, nothing to do */
2134 	if (cmstr == fc->nodeid)
2135 		return 0;
2136 	/* Bus probe has not finished, make dummy fwdev for cmstr */
2137 	bzero(&fwdev, sizeof(fwdev));
2138 	fwdev.fc = fc;
2139 	fwdev.dst = cmstr;
2140 	fwdev.speed = 0;
2141 	fwdev.maxrec = 8; /* 512 */
2142 	fwdev.status = FWDEVINIT;
2143 	/* Set cmstr bit on the cycle master */
2144 	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2145 		0xffff, 0xf0000000 | STATE_SET, htonl(1 << 16),
2146 		fw_asy_callback_free);
2147 
2148 	return 0;
2149 }
2150 
2151 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2152 MODULE_VERSION(firewire, 1);
2153