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