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