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