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