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