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