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