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