firewire.c revision 109379
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 109379 2003-01-16 13:09:33Z 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 DV_PAL
188#define DVSEC 3
189#define DVFRAC 75	/* PAL: 25 Hz (1875 = 25 * 3) */
190#define DVDIFF 5	/* 125 = (8000/300 - 25) * 3 */
191#else
192#define DVSEC 100
193#define DVFRAC 2997	/* NTSC: 29.97 Hz (2997 = 29.97 * 100) */
194#define DVDIFF 203	/* 203 = (8000/250 - 29.97) * 100 */
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 = PAGE_SIZE;
702	fc->ars->psize = PAGE_SIZE;
703	fc->atq->psize = 0;
704	fc->ats->psize = 0;
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#if 0
1051	DELAY(100000);
1052#endif
1053	xfer = fw_xfer_alloc();
1054	xfer->send.len = 12;
1055	xfer->send.off = 0;
1056	xfer->fc = fc;
1057	xfer->retry_req = fw_asybusy;
1058	xfer->act.hand = fw_phy_config_callback;
1059
1060	xfer->send.buf = malloc(sizeof(u_int32_t),
1061					M_DEVBUF, M_DONTWAIT | M_ZERO);
1062	fp = (struct fw_pkt *)xfer->send.buf;
1063	fp->mode.ld[1] = 0;
1064	if (root_node >= 0)
1065		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
1066	if (gap_count >= 0)
1067		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
1068	fp->mode.ld[2] = ~fp->mode.ld[1];
1069/* XXX Dangerous, how to pass PHY packet to device driver */
1070	fp->mode.common.tcode |= FWTCODE_PHY;
1071
1072	if (firewire_debug)
1073		printf("send phy_config root_node=%d gap_count=%d\n",
1074						root_node, gap_count);
1075	fw_asyreq(fc, -1, xfer);
1076}
1077
1078#if 0
1079/*
1080 * Dump self ID.
1081 */
1082static void
1083fw_print_sid(u_int32_t sid)
1084{
1085	union fw_self_id *s;
1086	s = (union fw_self_id *) &sid;
1087	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1088		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1089		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1090		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1091		s->p0.power_class, s->p0.port0, s->p0.port1,
1092		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1093}
1094#endif
1095
1096/*
1097 * To receive self ID.
1098 */
1099void fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1100{
1101	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1102	union fw_self_id *self_id;
1103	u_int i, j, node, c_port = 0, i_branch = 0;
1104
1105	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1106	fc->status = FWBUSINIT;
1107	fc->max_node = fc->nodeid & 0x3f;
1108	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1109	fc->status = FWBUSCYMELECT;
1110	fc->topology_map->crc_len = 2;
1111	fc->topology_map->generation ++;
1112	fc->topology_map->self_id_count = 0;
1113	fc->topology_map->node_count = 0;
1114	fc->speed_map->generation ++;
1115	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1116	self_id = &fc->topology_map->self_id[0];
1117	for(i = 0; i < fc->sid_cnt; i ++){
1118		if (sid[1] != ~sid[0]) {
1119			printf("fw_sidrcv: invalid self-id packet\n");
1120			sid += 2;
1121			continue;
1122		}
1123		*self_id = *((union fw_self_id *)sid);
1124		fc->topology_map->crc_len++;
1125		if(self_id->p0.sequel == 0){
1126			fc->topology_map->node_count ++;
1127			c_port = 0;
1128#if 0
1129			fw_print_sid(sid[0]);
1130#endif
1131			node = self_id->p0.phy_id;
1132			if(fc->max_node < node){
1133				fc->max_node = self_id->p0.phy_id;
1134			}
1135			/* XXX I'm not sure this is the right speed_map */
1136			fc->speed_map->speed[node][node]
1137					= self_id->p0.phy_speed;
1138			for (j = 0; j < node; j ++) {
1139				fc->speed_map->speed[j][node]
1140					= fc->speed_map->speed[node][j]
1141					= min(fc->speed_map->speed[j][j],
1142							self_id->p0.phy_speed);
1143			}
1144			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1145			  (self_id->p0.link_active && self_id->p0.contender)) {
1146				fc->irm = self_id->p0.phy_id;
1147			}
1148			if(self_id->p0.port0 >= 0x2){
1149				c_port++;
1150			}
1151			if(self_id->p0.port1 >= 0x2){
1152				c_port++;
1153			}
1154			if(self_id->p0.port2 >= 0x2){
1155				c_port++;
1156			}
1157		}
1158		if(c_port > 2){
1159			i_branch += (c_port - 2);
1160		}
1161		sid += 2;
1162		self_id++;
1163		fc->topology_map->self_id_count ++;
1164	}
1165	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1166	/* CRC */
1167	fc->topology_map->crc = fw_crc16(
1168			(u_int32_t *)&fc->topology_map->generation,
1169			fc->topology_map->crc_len * 4);
1170	fc->speed_map->crc = fw_crc16(
1171			(u_int32_t *)&fc->speed_map->generation,
1172			fc->speed_map->crc_len * 4);
1173	/* byteswap and copy to CSR */
1174	p = (u_int32_t *)fc->topology_map;
1175	for (i = 0; i <= fc->topology_map->crc_len; i++)
1176		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1177	p = (u_int32_t *)fc->speed_map;
1178	CSRARC(fc, SPED_MAP) = htonl(*p++);
1179	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1180	/* don't byte-swap u_int8_t array */
1181	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1182
1183	fc->max_hop = fc->max_node - i_branch;
1184#if 1
1185	printf(", maxhop <= %d", fc->max_hop);
1186#endif
1187
1188	if(fc->irm == -1 ){
1189		printf(", Not found IRM capable node");
1190	}else{
1191		printf(", cable IRM = %d", fc->irm);
1192		if (fc->irm == fc->nodeid)
1193			printf(" (me)\n");
1194		else
1195			printf("\n");
1196	}
1197
1198	if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){
1199		if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1200			fc->status = FWBUSMGRDONE;
1201			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1202		}else{
1203			fc->status = FWBUSMGRELECT;
1204			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8);
1205		}
1206	}else{
1207		fc->status = FWBUSMGRDONE;
1208		device_printf(fc->bdev, "BMR = %x\n",
1209				CSRARC(fc, BUS_MGR_ID));
1210	}
1211	free(buf, M_DEVBUF);
1212#if 1
1213	/* XXX optimize gap_count, if I am BMGR */
1214	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1215		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
1216	}
1217#endif
1218#if 1
1219	callout_reset(&fc->busprobe_callout, hz/4,
1220			(void *)fw_bus_probe, (void *)fc);
1221#else
1222	fw_bus_probe(fc);
1223#endif
1224}
1225
1226/*
1227 * To probe devices on the IEEE1394 bus.
1228 */
1229static void
1230fw_bus_probe(struct firewire_comm *fc)
1231{
1232	int s;
1233	struct fw_device *fwdev, *next;
1234
1235	s = splfw();
1236	fc->status = FWBUSEXPLORE;
1237	fc->retry_count = 0;
1238
1239/*
1240 * Invalidate all devices, just after bus reset. Devices
1241 * to be removed has not been seen longer time.
1242 */
1243	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1244		next = TAILQ_NEXT(fwdev, link);
1245		if(fwdev->status != FWDEVINVAL){
1246			fwdev->status = FWDEVINVAL;
1247			fwdev->rcnt = 0;
1248		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
1249			fwdev->rcnt ++;
1250		}else{
1251			TAILQ_REMOVE(&fc->devices, fwdev, link);
1252			free(fwdev, M_DEVBUF);
1253		}
1254	}
1255	fc->ongonode = 0;
1256	fc->ongoaddr = CSRROMOFF;
1257	fc->ongodev = NULL;
1258	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1259	fw_bus_explore(fc);
1260	splx(s);
1261}
1262
1263/*
1264 * To collect device informations on the IEEE1394 bus.
1265 */
1266static void
1267fw_bus_explore(struct firewire_comm *fc )
1268{
1269	int err = 0;
1270	struct fw_device *fwdev, *tfwdev;
1271	u_int32_t addr;
1272	struct fw_xfer *xfer;
1273	struct fw_pkt *fp;
1274
1275	if(fc->status != FWBUSEXPLORE)
1276		return;
1277
1278loop:
1279	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1280
1281	if(fc->ongonode > fc->max_node) goto done;
1282	if(fc->ongonode >= 0x3f) goto done;
1283
1284	/* check link */
1285	/* XXX we need to check phy_id first */
1286	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1287		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
1288		fc->ongonode++;
1289		goto loop;
1290	}
1291
1292	if(fc->ongoaddr <= CSRROMOFF &&
1293		fc->ongoeui.hi == 0xffffffff &&
1294		fc->ongoeui.lo == 0xffffffff ){
1295		fc->ongoaddr = CSRROMOFF;
1296		addr = 0xf0000000 | fc->ongoaddr;
1297	}else if(fc->ongoeui.hi == 0xffffffff ){
1298		fc->ongoaddr = CSRROMOFF + 0xc;
1299		addr = 0xf0000000 | fc->ongoaddr;
1300	}else if(fc->ongoeui.lo == 0xffffffff ){
1301		fc->ongoaddr = CSRROMOFF + 0x10;
1302		addr = 0xf0000000 | fc->ongoaddr;
1303	}else if(fc->ongodev == NULL){
1304		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1305			fwdev = TAILQ_NEXT(fwdev, link)){
1306			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
1307				break;
1308			}
1309		}
1310		if(fwdev != NULL){
1311			fwdev->dst = fc->ongonode;
1312			fwdev->status = FWDEVATTACHED;
1313			fc->ongonode++;
1314			fc->ongoaddr = CSRROMOFF;
1315			fc->ongodev = NULL;
1316			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1317			goto loop;
1318		}
1319		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT);
1320		if(fwdev == NULL)
1321			return;
1322		fwdev->fc = fc;
1323		fwdev->rommax = 0;
1324		fwdev->dst = fc->ongonode;
1325		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1326		fwdev->status = FWDEVINIT;
1327#if 0
1328		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1329			>> ((3 - (fc->ongonode % 4)) * 8);
1330#else
1331		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1332#endif
1333
1334		tfwdev = TAILQ_FIRST(&fc->devices);
1335		while( tfwdev != NULL &&
1336			(tfwdev->eui.hi > fwdev->eui.hi) &&
1337			((tfwdev->eui.hi == fwdev->eui.hi) &&
1338				tfwdev->eui.lo > fwdev->eui.lo)){
1339			tfwdev = TAILQ_NEXT( tfwdev, link);
1340		}
1341		if(tfwdev == NULL){
1342			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
1343		}else{
1344			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
1345		}
1346
1347		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1348			linkspeed[fwdev->speed],
1349			fc->ongoeui.hi, fc->ongoeui.lo);
1350
1351		fc->ongodev = fwdev;
1352		fc->ongoaddr = CSRROMOFF;
1353		addr = 0xf0000000 | fc->ongoaddr;
1354	}else{
1355		addr = 0xf0000000 | fc->ongoaddr;
1356	}
1357#if 0
1358	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1359		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1360		fw_bus_explore_callback);
1361	if(xfer == NULL) goto done;
1362#else
1363	xfer = fw_xfer_alloc();
1364	if(xfer == NULL){
1365		goto done;
1366	}
1367	xfer->send.len = 16;
1368	xfer->spd = 0;
1369	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1370	if(xfer->send.buf == NULL){
1371		fw_xfer_free( xfer);
1372		return;
1373	}
1374
1375	xfer->send.off = 0;
1376	fp = (struct fw_pkt *)xfer->send.buf;
1377	fp->mode.rreqq.dest_hi = htons(0xffff);
1378	fp->mode.rreqq.tlrt = 0;
1379	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1380	fp->mode.rreqq.pri = 0;
1381	fp->mode.rreqq.src = 0;
1382	xfer->dst = FWLOCALBUS | fc->ongonode;
1383	fp->mode.rreqq.dst = htons(xfer->dst);
1384	fp->mode.rreqq.dest_lo = htonl(addr);
1385	xfer->act.hand = fw_bus_explore_callback;
1386
1387	err = fw_asyreq(fc, -1, xfer);
1388	if(err){
1389		fw_xfer_free( xfer);
1390		return;
1391	}
1392#endif
1393	return;
1394done:
1395	/* fw_attach_devs */
1396	fc->status = FWBUSEXPDONE;
1397	if (firewire_debug)
1398		printf("bus_explore done\n");
1399	fw_attach_dev(fc);
1400	return;
1401
1402}
1403
1404/* Portable Async. request read quad */
1405struct fw_xfer *
1406asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1407	u_int32_t addr_hi, u_int32_t addr_lo,
1408	void (*hand) __P((struct fw_xfer*)))
1409{
1410	struct fw_xfer *xfer;
1411	struct fw_pkt *fp;
1412	int err;
1413
1414	xfer = fw_xfer_alloc();
1415	if(xfer == NULL){
1416		return NULL;
1417	}
1418	xfer->send.len = 16;
1419	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1420	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1421	if(xfer->send.buf == NULL){
1422		fw_xfer_free( xfer);
1423		return NULL;
1424	}
1425
1426	xfer->send.off = 0;
1427	fp = (struct fw_pkt *)xfer->send.buf;
1428	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1429	if(tl & FWP_TL_VALID){
1430		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1431	}else{
1432		fp->mode.rreqq.tlrt = 0;
1433	}
1434	fp->mode.rreqq.tlrt |= rt & 0x3;
1435	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1436	fp->mode.rreqq.pri = 0;
1437	fp->mode.rreqq.src = 0;
1438	xfer->dst = addr_hi >> 16;
1439	fp->mode.rreqq.dst = htons(xfer->dst);
1440	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1441	xfer->act.hand = hand;
1442
1443	err = fw_asyreq(fc, -1, xfer);
1444	if(err){
1445		fw_xfer_free( xfer);
1446		return NULL;
1447	}
1448	return xfer;
1449}
1450
1451/*
1452 * Callback for the IEEE1394 bus information collection.
1453 */
1454static void
1455fw_bus_explore_callback(struct fw_xfer *xfer)
1456{
1457	struct firewire_comm *fc;
1458	struct fw_pkt *sfp,*rfp;
1459	struct csrhdr *chdr;
1460	struct csrdir *csrd;
1461	struct csrreg *csrreg;
1462	u_int32_t offset;
1463
1464
1465	if(xfer == NULL) return;
1466	fc = xfer->fc;
1467	if(xfer->resp != 0){
1468		printf("resp != 0: node=%d addr=0x%x\n",
1469			fc->ongonode, fc->ongoaddr);
1470		fc->retry_count++;
1471		goto nextnode;
1472	}
1473
1474	if(xfer->send.buf == NULL){
1475		printf("send.buf == NULL: node=%d addr=0x%x\n",
1476			fc->ongonode, fc->ongoaddr);
1477		printf("send.buf == NULL\n");
1478		fc->retry_count++;
1479		goto nextnode;
1480	}
1481	sfp = (struct fw_pkt *)xfer->send.buf;
1482
1483	if(xfer->recv.buf == NULL){
1484		printf("recv.buf == NULL: node=%d addr=0x%x\n",
1485			fc->ongonode, fc->ongoaddr);
1486		fc->retry_count++;
1487		goto nextnode;
1488	}
1489	rfp = (struct fw_pkt *)xfer->recv.buf;
1490#if 0
1491	{
1492		u_int32_t *qld;
1493		int i;
1494		qld = (u_int32_t *)xfer->recv.buf;
1495		printf("len:%d\n", xfer->recv.len);
1496		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1497			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1498			if((i % 16) == 15) printf("\n");
1499		}
1500		if((i % 16) != 15) printf("\n");
1501	}
1502#endif
1503	if(fc->ongodev == NULL){
1504		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1505			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1506			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1507/* If CSR is minimul confinguration, more investgation is not needed. */
1508			if(chdr->info_len == 1){
1509				goto nextnode;
1510			}else{
1511				fc->ongoaddr = CSRROMOFF + 0xc;
1512			}
1513		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1514			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1515			fc->ongoaddr = CSRROMOFF + 0x10;
1516		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1517			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1518			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
1519				goto nextnode;
1520			fc->ongoaddr = CSRROMOFF;
1521		}
1522	}else{
1523		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1524		if(fc->ongoaddr > fc->ongodev->rommax){
1525			fc->ongodev->rommax = fc->ongoaddr;
1526		}
1527		csrd = SLIST_FIRST(&fc->ongocsr);
1528		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1529			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1530			offset = CSRROMOFF;
1531		}else{
1532			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1533			offset = csrd->off;
1534		}
1535		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1536			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1537			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1538				csrd = SLIST_FIRST(&fc->csrfree);
1539				if(csrd == NULL){
1540					goto nextnode;
1541				}else{
1542					csrd->ongoaddr = fc->ongoaddr;
1543					fc->ongoaddr += csrreg->val * 4;
1544					csrd->off = fc->ongoaddr;
1545					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1546					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1547					goto nextaddr;
1548				}
1549			}
1550		}
1551		fc->ongoaddr += 4;
1552		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1553				(fc->ongodev->rommax < 0x414)){
1554			if(fc->ongodev->rommax <= 0x414){
1555				csrd = SLIST_FIRST(&fc->csrfree);
1556				if(csrd == NULL) goto nextnode;
1557				csrd->off = fc->ongoaddr;
1558				csrd->ongoaddr = fc->ongoaddr;
1559				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1560				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1561			}
1562			goto nextaddr;
1563		}
1564
1565		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1566			if(csrd == NULL){
1567				goto nextnode;
1568			};
1569			fc->ongoaddr = csrd->ongoaddr + 4;
1570			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1571			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1572			csrd = SLIST_FIRST(&fc->ongocsr);
1573			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1574				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1575				offset = CSRROMOFF;
1576			}else{
1577				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1578				offset = csrd->off;
1579			}
1580		}
1581		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1582			goto nextnode;
1583		}
1584	}
1585nextaddr:
1586	fw_xfer_free( xfer);
1587	fw_bus_explore(fc);
1588	return;
1589nextnode:
1590	fw_xfer_free( xfer);
1591	fc->ongonode++;
1592/* housekeeping work space */
1593	fc->ongoaddr = CSRROMOFF;
1594	fc->ongodev = NULL;
1595	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1596	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1597		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1598		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1599	}
1600	fw_bus_explore(fc);
1601	return;
1602}
1603
1604/*
1605 * To obtain CSR register values.
1606 */
1607u_int32_t
1608getcsrdata(struct fw_device *fwdev, u_int8_t key)
1609{
1610	int i;
1611	struct csrhdr *chdr;
1612	struct csrreg *creg;
1613	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1614	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1615		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1616		if(creg->key == key){
1617			return (u_int32_t)creg->val;
1618		}
1619	}
1620	return 0;
1621}
1622
1623/*
1624 * To attach sub-devices layer onto IEEE1394 bus.
1625 */
1626static void
1627fw_attach_dev(struct firewire_comm *fc)
1628{
1629	struct fw_device *fwdev;
1630	struct fw_xfer *xfer;
1631	int i, err;
1632	device_t *devlistp;
1633	int devcnt;
1634	struct firewire_dev_comm *fdc;
1635	u_int32_t spec, ver;
1636
1637	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1638			fwdev = TAILQ_NEXT(fwdev, link)){
1639		if(fwdev->status == FWDEVINIT){
1640			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1641			if(spec == 0)
1642				continue;
1643			ver = getcsrdata(fwdev, CSRKEY_VER);
1644			if(ver == 0)
1645				continue;
1646			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1647
1648			device_printf(fc->bdev, "Device ");
1649			switch(spec){
1650			case CSRVAL_ANSIT10:
1651				switch(ver){
1652				case CSRVAL_T10SBP2:
1653					printf("SBP-II");
1654					break;
1655				default:
1656					break;
1657				}
1658				break;
1659			case CSRVAL_1394TA:
1660				switch(ver){
1661				case CSR_PROTAVC:
1662					printf("AV/C");
1663					break;
1664				case CSR_PROTCAL:
1665					printf("CAL");
1666					break;
1667				case CSR_PROTEHS:
1668					printf("EHS");
1669					break;
1670				case CSR_PROTHAVI:
1671					printf("HAVi");
1672					break;
1673				case CSR_PROTCAM104:
1674					printf("1394 Cam 1.04");
1675					break;
1676				case CSR_PROTCAM120:
1677					printf("1394 Cam 1.20");
1678					break;
1679				case CSR_PROTCAM130:
1680					printf("1394 Cam 1.30");
1681					break;
1682				case CSR_PROTDPP:
1683					printf("1394 Direct print");
1684					break;
1685				case CSR_PROTIICP:
1686					printf("Industrial & Instrument");
1687					break;
1688				default:
1689					printf("unknown 1394TA");
1690					break;
1691				}
1692				break;
1693			default:
1694				printf("unknown spec");
1695				break;
1696			}
1697			fwdev->status = FWDEVATTACHED;
1698			printf("\n");
1699		}
1700	}
1701	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1702	if( err != 0 )
1703		return;
1704	for( i = 0 ; i < devcnt ; i++){
1705		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1706			fdc = device_get_softc(devlistp[i]);
1707			if (fdc->post_explore != NULL)
1708				fdc->post_explore(fdc);
1709		}
1710	}
1711	free(devlistp, M_TEMP);
1712
1713	/* call pending handlers */
1714	i = 0;
1715	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1716		STAILQ_REMOVE_HEAD(&fc->pending, link);
1717		i++;
1718		if (xfer->act.hand)
1719			xfer->act.hand(xfer);
1720	}
1721	if (i > 0)
1722		printf("fw_attach_dev: %d pending handlers called\n", i);
1723	if (fc->retry_count > 0) {
1724		printf("retry_count = %d\n", fc->retry_count);
1725		fc->retry_probe_handle = timeout((timeout_t *)fc->ibr,
1726							(void *)fc, hz*2);
1727	}
1728	return;
1729}
1730
1731/*
1732 * To allocate uniq transaction label.
1733 */
1734static int
1735fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1736{
1737	u_int i;
1738	struct tlabel *tl, *tmptl;
1739	int s;
1740	static u_int32_t label = 0;
1741
1742	s = splfw();
1743	for( i = 0 ; i < 0x40 ; i ++){
1744		label = (label + 1) & 0x3f;
1745		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1746			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1747			if(tmptl->xfer->dst == xfer->dst) break;
1748		}
1749		if(tmptl == NULL) {
1750			tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_DONTWAIT);
1751			if (tl == NULL) {
1752				splx(s);
1753				return (-1);
1754			}
1755			tl->xfer = xfer;
1756			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1757			splx(s);
1758			return(label);
1759		}
1760	}
1761	splx(s);
1762
1763	printf("fw_get_tlabel: no free tlabel\n");
1764	return(-1);
1765}
1766
1767/*
1768 * Generic packet receving process.
1769 */
1770void
1771fw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1772{
1773	struct fw_pkt *fp, *resfp;
1774	struct fw_xfer *xfer;
1775	struct fw_bind *bind;
1776	struct firewire_softc *sc;
1777	int s;
1778#if 0
1779	{
1780		u_int32_t *qld;
1781		int i;
1782		qld = (u_int32_t *)buf;
1783		printf("spd %d len:%d\n", spd, len);
1784		for( i = 0 ; i <= len && i < 32; i+= 4){
1785			printf("0x%08x ", ntohl(qld[i/4]));
1786			if((i % 16) == 15) printf("\n");
1787		}
1788		if((i % 16) != 15) printf("\n");
1789	}
1790#endif
1791	fp = (struct fw_pkt *)(buf + off);
1792	switch(fp->mode.common.tcode){
1793	case FWTCODE_WRES:
1794	case FWTCODE_RRESQ:
1795	case FWTCODE_RRESB:
1796	case FWTCODE_LRES:
1797		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1798					fp->mode.hdr.tlrt >> 2);
1799		if(xfer == NULL) {
1800			printf("fw_rcv: unknown response "
1801					"tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n",
1802					fp->mode.common.tcode,
1803					ntohs(fp->mode.hdr.src),
1804					fp->mode.hdr.tlrt >> 2,
1805					fp->mode.hdr.tlrt & 3,
1806					fp->mode.rresq.data);
1807#if 1
1808			printf("try ad-hoc work around!!\n");
1809			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1810					(fp->mode.hdr.tlrt >> 2)^3);
1811			if (xfer == NULL) {
1812				printf("no use...\n");
1813				goto err;
1814			}
1815#else
1816			goto err;
1817#endif
1818		}
1819		switch(xfer->act_type){
1820		case FWACT_XFER:
1821			if((xfer->sub >= 0) &&
1822				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1823				xfer->resp = EINVAL;
1824				fw_xfer_done(xfer);
1825				goto err;
1826			}
1827			xfer->recv.len = len;
1828			xfer->recv.off = off;
1829			xfer->recv.buf = buf;
1830			xfer->resp = 0;
1831			fw_xfer_done(xfer);
1832			return;
1833			break;
1834		case FWACT_CH:
1835		default:
1836			goto err;
1837			break;
1838		}
1839		break;
1840	case FWTCODE_WREQQ:
1841	case FWTCODE_WREQB:
1842	case FWTCODE_RREQQ:
1843	case FWTCODE_RREQB:
1844	case FWTCODE_LREQ:
1845		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1846			ntohl(fp->mode.rreqq.dest_lo));
1847		if(bind == NULL){
1848#if __FreeBSD_version >= 500000
1849			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
1850#else
1851			printf("Unknown service addr 0x%08x:0x%08lx tcode=%x\n",
1852#endif
1853				ntohs(fp->mode.rreqq.dest_hi),
1854				ntohl(fp->mode.rreqq.dest_lo),
1855				fp->mode.common.tcode);
1856			if (fc->status == FWBUSRESET) {
1857				printf("fw_rcv: cannot response(bus reset)!\n");
1858				goto err;
1859			}
1860			xfer = fw_xfer_alloc();
1861			if(xfer == NULL){
1862				return;
1863			}
1864			xfer->spd = spd;
1865			xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1866			resfp = (struct fw_pkt *)xfer->send.buf;
1867			switch(fp->mode.common.tcode){
1868			case FWTCODE_WREQQ:
1869			case FWTCODE_WREQB:
1870				resfp->mode.hdr.tcode = FWTCODE_WRES;
1871				xfer->send.len = 12;
1872				break;
1873			case FWTCODE_RREQQ:
1874				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1875				xfer->send.len = 16;
1876				break;
1877			case FWTCODE_RREQB:
1878				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1879				xfer->send.len = 16;
1880				break;
1881			case FWTCODE_LREQ:
1882				resfp->mode.hdr.tcode = FWTCODE_LRES;
1883				xfer->send.len = 16;
1884				break;
1885			}
1886			resfp->mode.hdr.dst = fp->mode.hdr.src;
1887			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1888			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1889			resfp->mode.rresb.rtcode = 7;
1890			resfp->mode.rresb.extcode = 0;
1891			resfp->mode.rresb.len = 0;
1892/*
1893			xfer->act.hand = fw_asy_callback;
1894*/
1895			xfer->act.hand = fw_xfer_free;
1896			if(fw_asyreq(fc, -1, xfer)){
1897				fw_xfer_free( xfer);
1898				return;
1899			}
1900			goto err;
1901		}
1902		switch(bind->xfer->act_type){
1903		case FWACT_XFER:
1904			xfer = fw_xfer_alloc();
1905			if(xfer == NULL) goto err;
1906			xfer->fc = bind->xfer->fc;
1907			xfer->sc = bind->xfer->sc;
1908			xfer->recv.buf = buf;
1909			xfer->recv.len = len;
1910			xfer->recv.off = off;
1911			xfer->spd = spd;
1912			xfer->act.hand = bind->xfer->act.hand;
1913			if (fc->status != FWBUSRESET)
1914				xfer->act.hand(xfer);
1915			else
1916				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1917			return;
1918			break;
1919		case FWACT_CH:
1920			if(fc->ir[bind->xfer->sub]->queued >=
1921				fc->ir[bind->xfer->sub]->maxq){
1922				device_printf(fc->bdev,
1923					"Discard a packet %x %d\n",
1924					bind->xfer->sub,
1925					fc->ir[bind->xfer->sub]->queued);
1926				goto err;
1927			}
1928			xfer = fw_xfer_alloc();
1929			if(xfer == NULL) goto err;
1930			xfer->recv.buf = buf;
1931			xfer->recv.len = len;
1932			xfer->recv.off = off;
1933			xfer->spd = spd;
1934			s = splfw();
1935			fc->ir[bind->xfer->sub]->queued++;
1936			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1937			splx(s);
1938
1939			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1940
1941			return;
1942			break;
1943		default:
1944			goto err;
1945			break;
1946		}
1947		break;
1948	case FWTCODE_STREAM:
1949	{
1950		struct fw_xferq *xferq;
1951
1952		xferq = fc->ir[sub];
1953#if 0
1954		printf("stream rcv dma %d len %d off %d spd %d\n",
1955			sub, len, off, spd);
1956#endif
1957		if(xferq->queued >= xferq->maxq) {
1958			printf("receive queue is full\n");
1959			goto err;
1960		}
1961		xfer = fw_xfer_alloc();
1962		if(xfer == NULL) goto err;
1963		xfer->recv.buf = buf;
1964		xfer->recv.len = len;
1965		xfer->recv.off = off;
1966		xfer->spd = spd;
1967		s = splfw();
1968		xferq->queued++;
1969		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1970		splx(s);
1971		sc = device_get_softc(fc->bdev);
1972#if __FreeBSD_version >= 500000
1973		if (SEL_WAITING(&xferq->rsel))
1974#else
1975		if (&xferq->rsel.si_pid != 0)
1976#endif
1977			selwakeup(&xferq->rsel);
1978		if (xferq->flag & FWXFERQ_WAKEUP) {
1979			xferq->flag &= ~FWXFERQ_WAKEUP;
1980			wakeup((caddr_t)xferq);
1981		}
1982		if (xferq->flag & FWXFERQ_HANDLER) {
1983			xferq->hand(xferq);
1984		}
1985		return;
1986		break;
1987	}
1988	default:
1989		printf("fw_rcv: unknow tcode\n");
1990		break;
1991	}
1992err:
1993	free(buf, M_DEVBUF);
1994}
1995
1996/*
1997 * Post process for Bus Manager election process.
1998 */
1999static void
2000fw_try_bmr_callback(struct fw_xfer *xfer)
2001{
2002	struct fw_pkt *sfp,*rfp;
2003	struct firewire_comm *fc;
2004
2005	if(xfer == NULL) return;
2006	fc = xfer->fc;
2007	if(xfer->resp != 0){
2008		goto error;
2009	}
2010
2011	if(xfer->send.buf == NULL){
2012		goto error;
2013	}
2014	sfp = (struct fw_pkt *)xfer->send.buf;
2015
2016	if(xfer->recv.buf == NULL){
2017		goto error;
2018	}
2019	rfp = (struct fw_pkt *)xfer->recv.buf;
2020	CSRARC(fc, BUS_MGR_ID)
2021		= fc->set_bmr(fc, ntohl(rfp->mode.lres.payload[0]) & 0x3f);
2022	device_printf(fc->bdev, "new bus manager %d ",
2023		CSRARC(fc, BUS_MGR_ID));
2024	if((htonl(rfp->mode.lres.payload[0]) & 0x3f) == fc->nodeid){
2025		printf("(me)\n");
2026/* If I am bus manager, optimize gapcount */
2027		if(fc->max_hop <= MAX_GAPHOP ){
2028			fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
2029		}
2030	}else{
2031		printf("\n");
2032	}
2033error:
2034	fw_xfer_free(xfer);
2035}
2036
2037/*
2038 * To candidate Bus Manager election process.
2039 */
2040void
2041fw_try_bmr(void *arg)
2042{
2043	struct fw_xfer *xfer;
2044	struct firewire_comm *fc = (struct firewire_comm *)arg;
2045	struct fw_pkt *fp;
2046	int err = 0;
2047
2048	xfer = fw_xfer_alloc();
2049	if(xfer == NULL){
2050		return;
2051	}
2052	xfer->send.len = 24;
2053	xfer->spd = 0;
2054	xfer->send.buf = malloc(24, M_DEVBUF, M_DONTWAIT);
2055	if(xfer->send.buf == NULL){
2056		fw_xfer_free( xfer);
2057		return;
2058	}
2059
2060	fc->status = FWBUSMGRELECT;
2061
2062	xfer->send.off = 0;
2063	fp = (struct fw_pkt *)xfer->send.buf;
2064	fp->mode.lreq.dest_hi = htons(0xffff);
2065	fp->mode.lreq.tlrt = 0;
2066	fp->mode.lreq.tcode = FWTCODE_LREQ;
2067	fp->mode.lreq.pri = 0;
2068	fp->mode.lreq.src = 0;
2069	fp->mode.lreq.len = htons(8);
2070	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
2071	xfer->dst = FWLOCALBUS | fc->irm;
2072	fp->mode.lreq.dst = htons(xfer->dst);
2073	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
2074	fp->mode.lreq.payload[0] = 0x3f;
2075	fp->mode.lreq.payload[1] = fc->nodeid;
2076	xfer->act_type = FWACT_XFER;
2077	xfer->act.hand = fw_try_bmr_callback;
2078
2079	err = fw_asyreq(fc, -1, xfer);
2080	if(err){
2081		fw_xfer_free( xfer);
2082		return;
2083	}
2084	return;
2085}
2086
2087#ifdef FW_VMACCESS
2088/*
2089 * Software implementation for physical memory block access.
2090 * XXX:Too slow, usef for debug purpose only.
2091 */
2092static void
2093fw_vmaccess(struct fw_xfer *xfer){
2094	struct fw_pkt *rfp, *sfp = NULL;
2095	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
2096
2097	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
2098			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2099	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2100	if(xfer->resp != 0){
2101		fw_xfer_free( xfer);
2102		return;
2103	}
2104	if(xfer->recv.buf == NULL){
2105		fw_xfer_free( xfer);
2106		return;
2107	}
2108	rfp = (struct fw_pkt *)xfer->recv.buf;
2109	switch(rfp->mode.hdr.tcode){
2110		/* XXX need fix for 64bit arch */
2111		case FWTCODE_WREQB:
2112			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
2113			xfer->send.len = 12;
2114			sfp = (struct fw_pkt *)xfer->send.buf;
2115			bcopy(rfp->mode.wreqb.payload,
2116				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2117			sfp->mode.wres.tcode = FWTCODE_WRES;
2118			sfp->mode.wres.rtcode = 0;
2119			break;
2120		case FWTCODE_WREQQ:
2121			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
2122			xfer->send.len = 12;
2123			sfp->mode.wres.tcode = FWTCODE_WRES;
2124			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2125			sfp->mode.wres.rtcode = 0;
2126			break;
2127		case FWTCODE_RREQB:
2128			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT);
2129			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2130			sfp = (struct fw_pkt *)xfer->send.buf;
2131			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2132				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2133			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2134			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2135			sfp->mode.rresb.rtcode = 0;
2136			sfp->mode.rresb.extcode = 0;
2137			break;
2138		case FWTCODE_RREQQ:
2139			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
2140			xfer->send.len = 16;
2141			sfp = (struct fw_pkt *)xfer->send.buf;
2142			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2143			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2144			sfp->mode.rresb.rtcode = 0;
2145			break;
2146		default:
2147			fw_xfer_free( xfer);
2148			return;
2149	}
2150	xfer->send.off = 0;
2151	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2152	xfer->dst = ntohs(rfp->mode.hdr.src);
2153	xfer->act.hand = fw_xfer_free;
2154	xfer->retry_req = fw_asybusy;
2155
2156	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2157	sfp->mode.hdr.pri = 0;
2158
2159	fw_asyreq(xfer->fc, -1, xfer);
2160/**/
2161	return;
2162}
2163#endif
2164
2165/*
2166 * CRC16 check-sum for IEEE1394 register blocks.
2167 */
2168u_int16_t
2169fw_crc16(u_int32_t *ptr, u_int32_t len){
2170	u_int32_t i, sum, crc = 0;
2171	int shift;
2172	len = (len + 3) & ~3;
2173	for(i = 0 ; i < len ; i+= 4){
2174		for( shift = 28 ; shift >= 0 ; shift -= 4){
2175			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2176			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2177		}
2178		crc &= 0xffff;
2179	}
2180	return((u_int16_t) crc);
2181}
2182
2183DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2184MODULE_VERSION(firewire, 1);
2185