firewire.c revision 108701
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 108701 2003-01-05 06:21:30Z simokawa $
34 *
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/types.h>
40#include <sys/mbuf.h>
41#include <sys/socket.h>
42#include <sys/socketvar.h>
43
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#include <sys/conf.h>
47#include <sys/uio.h>
48#include <sys/sysctl.h>
49
50#include <machine/cpufunc.h>    /* for rdtsc proto for clock.h below */
51#include <machine/clock.h>
52
53#include <sys/bus.h>		/* used by smbus and newbus */
54
55#include <dev/firewire/firewire.h>
56#include <dev/firewire/firewirereg.h>
57#include <dev/firewire/iec13213.h>
58#include <dev/firewire/iec68113.h>
59
60int firewire_debug=0;
61SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
62SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
63	"FireWire driver debug flag");
64
65#define FW_MAXASYRTY 4
66#define FW_MAXDEVRCNT 4
67
68#define XFER_TIMEOUT 0
69
70devclass_t firewire_devclass;
71
72static int firewire_match      __P((device_t));
73static int firewire_attach      __P((device_t));
74static int firewire_detach      __P((device_t));
75#if 0
76static int firewire_shutdown    __P((device_t));
77#endif
78static device_t firewire_add_child   __P((device_t, int, const char *, int));
79static void fw_try_bmr __P((void *));
80static void fw_try_bmr_callback __P((struct fw_xfer *));
81static void fw_asystart __P((struct fw_xfer *));
82static int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
83static void fw_bus_probe __P((struct firewire_comm *));
84static void fw_bus_explore __P((struct firewire_comm *));
85static void fw_bus_explore_callback __P((struct fw_xfer *));
86static void fw_attach_dev __P((struct firewire_comm *));
87#ifdef FW_VMACCESS
88static void fw_vmaccess __P((struct fw_xfer *));
89#endif
90struct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
91	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
92
93static device_method_t firewire_methods[] = {
94	/* Device interface */
95	DEVMETHOD(device_probe,		firewire_match),
96	DEVMETHOD(device_attach,	firewire_attach),
97	DEVMETHOD(device_detach,	firewire_detach),
98	DEVMETHOD(device_suspend,	bus_generic_suspend),
99	DEVMETHOD(device_resume,	bus_generic_resume),
100	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
101
102	/* Bus interface */
103	DEVMETHOD(bus_add_child,	firewire_add_child),
104	DEVMETHOD(bus_print_child,	bus_generic_print_child),
105
106	{ 0, 0 }
107};
108char linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
109
110#define MAX_GAPHOP  16
111u_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
112			20, 23, 25, 28, 31, 33, 36, 39, 42};
113
114extern struct cdevsw firewire_cdevsw;
115
116static driver_t firewire_driver = {
117	"firewire",
118	firewire_methods,
119	sizeof(struct firewire_softc),
120};
121
122/*
123 * transmitter buffer update.
124 */
125int
126fw_tbuf_update(struct firewire_comm *fc, int sub, int flag){
127	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
128	struct fw_dvbuf *dvbuf = NULL;
129	struct fw_xferq *it;
130	int s, err = 0, i, j, chtag;
131	struct fw_pkt *fp;
132	u_int64_t tmpsync, dvsync;
133
134	it = fc->it[sub];
135
136	s = splfw();
137	if(it->stdma == NULL){
138		bulkxfer = STAILQ_FIRST(&it->stvalid);
139	}else if(flag != 0){
140		bulkxfer = STAILQ_FIRST(&it->stvalid);
141		if(bulkxfer == it->stdma){
142			STAILQ_REMOVE_HEAD(&it->stvalid, link);
143			it->stdma->flag = 0;
144			STAILQ_INSERT_TAIL(&it->stfree, it->stdma, link);
145			if(!(it->flag & FWXFERQ_DV))
146				wakeup(it);
147		}
148		bulkxfer = STAILQ_FIRST(&it->stvalid);
149	}else{
150		bulkxfer = it->stdma;
151	}
152	splx(s);
153	if(bulkxfer != NULL){
154		s = splfw();
155		bulkxfer2 = STAILQ_NEXT(bulkxfer, link);
156#if 0
157		if(it->flag & FWXFERQ_DV && bulkxfer2 == NULL){
158			bulkxfer2 = STAILQ_FIRST(&it->stfree);
159			STAILQ_REMOVE_HEAD(&it->stfree, link);
160			splx(s);
161			bcopy(bulkxfer->buf, bulkxfer2->buf,
162					it->psize * it->btpacket);
163			s = splfw();
164			STAILQ_INSERT_TAIL(&it->stvalid, bulkxfer2, link);
165		}
166#endif
167		splx(s);
168	}
169	it->stdma = bulkxfer;
170	it->stdma2 = bulkxfer2;
171
172	if(it->flag & FWXFERQ_DV){
173		chtag = it->flag & 0xff;
174dvloop:
175		if(it->dvdma == NULL){
176			dvbuf = STAILQ_FIRST(&it->dvvalid);
177			if(dvbuf != NULL){
178				s = splfw();
179				STAILQ_REMOVE_HEAD(&it->dvvalid, link);
180				it->dvdma = dvbuf;
181				splx(s);
182				it->queued = 0;
183			}
184		}
185		if(it->dvdma == NULL)
186			return err;
187
188		it->stproc = STAILQ_FIRST(&it->stfree);
189		if(it->stproc != NULL){
190			s = splfw();
191			STAILQ_REMOVE_HEAD(&it->stfree, link);
192			splx(s);
193		}else{
194			return err;
195		}
196/*
197 * Insert least significant 12 bits timestamp value by computation.
198 * Highest significant 4 bits is insert at just before packet sending.
199 */
200		fp = (struct fw_pkt *)(it->stproc->buf);
201/* XXX: Parameter relies on NTSC type DV video */
202		tmpsync = (u_int64_t)3072 * 8000 * 100 / 2997;
203		tmpsync *= it->dvsync;
204		dvsync = tmpsync;
205		dvsync %= 0xc00;
206		fp->mode.ld[2] = htonl(0x80000000 | (dvsync % 0xc00));
207		it->dvsync ++;
208		it->dvsync %= 2997;
209
210		for( i = 0, j = 0 ; i < it->dvpacket ; i++){
211			bcopy(it->dvdma->buf + it->queued * it->psize,
212				it->stproc->buf + j * it->psize, it->psize);
213			fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
214			fp->mode.stream.len = htons(488);
215			fp->mode.stream.chtag = chtag;
216			fp->mode.stream.tcode = FWTCODE_STREAM;
217			fp->mode.ld[1] = htonl((fc->nodeid << 24) | 0x00780000 | it->dvdbc);
218			it->dvdbc++;
219			it->dvdbc %= 256;
220			it->queued ++;
221			j++;
222/* XXX: Parameter relies on NTSC type DV video */
223#if 1
224#define DVDIFF 203
225#define DVFRAC 2997
226#else
227#define DVDIFF 127
228#define DVFRAC 1875
229#endif
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		s = splfw();
245		STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
246		splx(s);
247		if(it->queued >= it->dvpacket){
248			s = splfw();
249			STAILQ_INSERT_TAIL(&it->dvfree, it->dvdma, link);
250			it->dvdma = NULL;
251			splx(s);
252			wakeup(it);
253			goto dvloop;
254		}
255	}
256	return err;
257}
258/*
259 * receving buffer update.
260 */
261int
262fw_rbuf_update(struct firewire_comm *fc, int sub, int flag){
263	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
264	struct fw_xferq *ir;
265	int s, err = 0;
266
267	ir = fc->ir[sub];
268	s = splfw();
269	if(ir->stdma != NULL){
270		if(flag != 0){
271			STAILQ_INSERT_TAIL(&ir->stvalid, ir->stdma, link);
272		}else{
273			ir->stdma->flag = 0;
274			STAILQ_INSERT_TAIL(&ir->stfree, ir->stdma, link);
275		}
276	}
277	if(ir->stdma2 != NULL){
278		bulkxfer = ir->stdma2;
279		bulkxfer2 = STAILQ_FIRST(&ir->stfree);
280		if(bulkxfer2 != NULL){
281			STAILQ_REMOVE_HEAD(&ir->stfree, link);
282		}
283	}else{
284		bulkxfer = STAILQ_FIRST(&ir->stfree);
285		if(bulkxfer != NULL){
286			STAILQ_REMOVE_HEAD(&ir->stfree, link);
287			bulkxfer2 = STAILQ_FIRST(&ir->stfree);
288			if(bulkxfer2 != NULL){
289				STAILQ_REMOVE_HEAD(&ir->stfree, link);
290			}
291		}else{
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	/* Locate our children */
526	bus_generic_probe(dev);
527
528	/* launch attachement of the added children */
529	bus_generic_attach(dev);
530
531	/* bus_reset */
532	fc->ibr(fc);
533
534	return 0;
535}
536
537/*
538 * Attach it as child.
539 */
540static device_t
541firewire_add_child(device_t dev, int order, const char *name, int unit)
542{
543        device_t child;
544	struct firewire_softc *sc;
545
546	sc = (struct firewire_softc *)device_get_softc(dev);
547	child = device_add_child(dev, name, unit);
548	if (child) {
549		device_set_ivars(child, sc->fc);
550		device_probe_and_attach(child);
551	}
552
553	return child;
554}
555
556/*
557 * Dettach it.
558 */
559static int
560firewire_detach( device_t dev )
561{
562	struct firewire_softc *sc;
563
564	sc = (struct firewire_softc *)device_get_softc(dev);
565
566#if __FreeBSD_version >= 500000
567	destroy_dev(sc->dev);
568#else
569	{
570		int j;
571		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
572			destroy_dev(sc->dev[j]);
573	}
574#endif
575	/* XXX xfree_free and untimeout on all xfers */
576	untimeout((timeout_t *)sc->fc->timeout, sc->fc, sc->fc->timeouthandle);
577	free(sc->fc->topology_map, M_DEVBUF);
578	free(sc->fc->speed_map, M_DEVBUF);
579	bus_generic_detach(dev);
580	return(0);
581}
582#if 0
583static int
584firewire_shutdown( device_t dev )
585{
586	return 0;
587}
588#endif
589
590/*
591 * Called after bus reset.
592 */
593void
594fw_busreset(struct firewire_comm *fc)
595{
596	int i;
597	struct fw_xfer *xfer;
598
599	switch(fc->status){
600	case FWBUSMGRELECT:
601		untimeout((timeout_t *)fw_try_bmr, (void *)fc, fc->bmrhandle);
602		break;
603	default:
604		break;
605	}
606	fc->status = FWBUSRESET;
607/* XXX: discard all queued packet */
608	while((xfer = STAILQ_FIRST(&fc->atq->q)) != NULL){
609		STAILQ_REMOVE_HEAD(&fc->atq->q, link);
610		xfer->resp = EAGAIN;
611		switch(xfer->act_type){
612		case FWACT_XFER:
613			fw_xfer_done(xfer);
614			break;
615		default:
616			break;
617		}
618		fw_xfer_free( xfer);
619	}
620	while((xfer = STAILQ_FIRST(&fc->ats->q)) != NULL){
621		STAILQ_REMOVE_HEAD(&fc->ats->q, link);
622		xfer->resp = EAGAIN;
623		switch(xfer->act_type){
624		case FWACT_XFER:
625			fw_xfer_done(xfer);
626		default:
627			break;
628		}
629		fw_xfer_free( xfer);
630	}
631	for(i = 0; i < fc->nisodma; i++)
632		while((xfer = STAILQ_FIRST(&fc->it[i]->q)) != NULL){
633			STAILQ_REMOVE_HEAD(&fc->it[i]->q, link);
634			xfer->resp = 0;
635			switch(xfer->act_type){
636			case FWACT_XFER:
637				fw_xfer_done(xfer);
638				break;
639			default:
640				break;
641			}
642			fw_xfer_free( xfer);
643		}
644
645	CSRARC(fc, STATE_CLEAR)
646			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
647	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
648	CSRARC(fc, NODE_IDS) = 0x3f;
649
650	CSRARC(fc, TOPO_MAP + 8) = 0;
651	fc->irm = -1;
652
653	fc->max_node = -1;
654
655	for(i = 2; i < 0x100/4 - 2 ; i++){
656		CSRARC(fc, SPED_MAP + i * 4) = 0;
657	}
658	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
659	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
660	CSRARC(fc, RESET_START) = 0;
661	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
662	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
663	CSRARC(fc, CYCLE_TIME) = 0x0;
664	CSRARC(fc, BUS_TIME) = 0x0;
665	CSRARC(fc, BUS_MGR_ID) = 0x3f;
666	CSRARC(fc, BANDWIDTH_AV) = 4915;
667	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
668	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
669	CSRARC(fc, IP_CHANNELS) = (1 << 31);
670
671	CSRARC(fc, CONF_ROM) = 0x04 << 24;
672	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
673	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
674				1 << 28 | 0xff << 16 | 0x09 << 8;
675	CSRARC(fc, CONF_ROM + 0xc) = 0;
676
677/* DV depend CSRs see blue book */
678	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
679	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
680
681	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
682	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
683}
684
685/* Call once after reboot */
686void fw_init(struct firewire_comm *fc)
687{
688	int i;
689	struct csrdir *csrd;
690#ifdef FW_VMACCESS
691	struct fw_xfer *xfer;
692	struct fw_bind *fwb;
693#endif
694
695	fc->max_asyretry = FW_MAXASYRTY;
696
697	fc->arq->queued = 0;
698	fc->ars->queued = 0;
699	fc->atq->queued = 0;
700	fc->ats->queued = 0;
701
702	fc->arq->psize = FWPMAX_S400;
703	fc->ars->psize = FWPMAX_S400;
704	fc->atq->psize = FWPMAX_S400;
705	fc->ats->psize = FWPMAX_S400;
706
707
708	fc->arq->buf = NULL;
709	fc->ars->buf = NULL;
710	fc->atq->buf = NULL;
711	fc->ats->buf = NULL;
712
713	fc->arq->flag = FWXFERQ_PACKET;
714	fc->ars->flag = FWXFERQ_PACKET;
715	fc->atq->flag = FWXFERQ_PACKET;
716	fc->ats->flag = FWXFERQ_PACKET;
717
718	STAILQ_INIT(&fc->atq->q);
719	STAILQ_INIT(&fc->ats->q);
720
721	for( i = 0 ; i < fc->nisodma ; i ++ ){
722		fc->it[i]->queued = 0;
723		fc->ir[i]->queued = 0;
724
725		fc->it[i]->start = NULL;
726		fc->ir[i]->start = NULL;
727
728		fc->it[i]->buf = NULL;
729		fc->ir[i]->buf = NULL;
730
731		fc->it[i]->flag = FWXFERQ_STREAM;
732		fc->ir[i]->flag = FWXFERQ_STREAM;
733
734		STAILQ_INIT(&fc->it[i]->q);
735		STAILQ_INIT(&fc->ir[i]->q);
736
737		STAILQ_INIT(&fc->it[i]->binds);
738		STAILQ_INIT(&fc->ir[i]->binds);
739	}
740
741	fc->arq->maxq = FWMAXQUEUE;
742	fc->ars->maxq = FWMAXQUEUE;
743	fc->atq->maxq = FWMAXQUEUE;
744	fc->ats->maxq = FWMAXQUEUE;
745
746	for( i = 0 ; i < fc->nisodma ; i++){
747		fc->ir[i]->maxq = FWMAXQUEUE;
748		fc->it[i]->maxq = FWMAXQUEUE;
749	}
750/* Initialize csr registers */
751	fc->topology_map = (struct fw_topology_map *)malloc(
752				sizeof(struct fw_topology_map),
753				M_DEVBUF, M_DONTWAIT | M_ZERO);
754	fc->speed_map = (struct fw_speed_map *)malloc(
755				sizeof(struct fw_speed_map),
756				M_DEVBUF, M_DONTWAIT | M_ZERO);
757	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
758	CSRARC(fc, TOPO_MAP + 4) = 1;
759	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
760	CSRARC(fc, SPED_MAP + 4) = 1;
761
762	TAILQ_INIT(&fc->devices);
763	STAILQ_INIT(&fc->pending);
764
765/* Initialize csr ROM work space */
766	SLIST_INIT(&fc->ongocsr);
767	SLIST_INIT(&fc->csrfree);
768	for( i = 0 ; i < FWMAXCSRDIR ; i++){
769		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_DEVBUF,M_DONTWAIT);
770		if(csrd == NULL) break;
771		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
772	}
773
774/* Initialize Async handlers */
775	STAILQ_INIT(&fc->binds);
776	for( i = 0 ; i < 0x40 ; i++){
777		STAILQ_INIT(&fc->tlabels[i]);
778	}
779
780/* DV depend CSRs see blue book */
781#if 0
782	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
783	CSRARC(fc, oPCR) = 0x8000007a;
784	for(i = 4 ; i < 0x7c/4 ; i+=4){
785		CSRARC(fc, i + oPCR) = 0x8000007a;
786	}
787
788	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
789	CSRARC(fc, iPCR) = 0x803f0000;
790	for(i = 4 ; i < 0x7c/4 ; i+=4){
791		CSRARC(fc, i + iPCR) = 0x0;
792	}
793#endif
794
795
796#ifdef FW_VMACCESS
797	xfer = fw_xfer_alloc();
798	if(xfer == NULL) return;
799
800	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
801	if(fwb == NULL){
802		fw_xfer_free(xfer);
803	}
804	xfer->act.hand = fw_vmaccess;
805	xfer->act_type = FWACT_XFER;
806	xfer->fc = fc;
807	xfer->sc = NULL;
808
809	fwb->start_hi = 0x2;
810	fwb->start_lo = 0;
811	fwb->addrlen = 0xffffffff;
812	fwb->xfer = xfer;
813	fw_bindadd(fc, fwb);
814#endif
815}
816
817/*
818 * To lookup binded process from IEEE1394 address.
819 */
820struct fw_bind *
821fw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
822{
823	struct fw_bind *tfw;
824	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
825		tfw = STAILQ_NEXT(tfw, fclist)){
826		if(tfw->xfer->act_type != FWACT_NULL &&
827			tfw->start_hi == dest_hi &&
828			tfw->start_lo <= dest_lo &&
829			(tfw->start_lo + tfw->addrlen) > dest_lo){
830			return(tfw);
831		}
832	}
833	return(NULL);
834}
835
836/*
837 * To bind IEEE1394 address block to process.
838 */
839int
840fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
841{
842	struct fw_bind *tfw, *tfw2 = NULL;
843	int err = 0;
844	tfw = STAILQ_FIRST(&fc->binds);
845	if(tfw == NULL){
846		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
847		goto out;
848	}
849	if((tfw->start_hi > fwb->start_hi) ||
850		(tfw->start_hi == fwb->start_hi &&
851		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
852		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
853		goto out;
854	}
855	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
856		if((tfw->start_hi < fwb->start_hi) ||
857		   (tfw->start_hi == fwb->start_hi &&
858		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
859		   tfw2 = STAILQ_NEXT(tfw, fclist);
860			if(tfw2 == NULL)
861				break;
862			if((tfw2->start_hi > fwb->start_hi) ||
863			   (tfw2->start_hi == fwb->start_hi &&
864			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
865				break;
866			}else{
867				err = EBUSY;
868				goto out;
869			}
870		}
871	}
872	if(tfw != NULL){
873		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
874	}else{
875		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
876	}
877out:
878	if(!err && fwb->xfer->act_type == FWACT_CH){
879		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
880	}
881	return err;
882}
883
884/*
885 * To free IEEE1394 address block.
886 */
887int
888fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
889{
890	int s;
891
892	s = splfw();
893	/* shall we check the existance? */
894	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
895	splx(s);
896	if (fwb->xfer)
897		fw_xfer_free(fwb->xfer);
898
899	return 0;
900}
901
902/*
903 * To free transaction label.
904 */
905static void
906fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
907{
908	struct tlabel *tl;
909	int s = splfw();
910
911	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
912		tl = STAILQ_NEXT(tl, link)){
913		if(tl->xfer == xfer){
914			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
915			free(tl, M_DEVBUF);
916			splx(s);
917			return;
918		}
919	}
920	splx(s);
921	return;
922}
923
924/*
925 * To obtain XFER structure by transaction label.
926 */
927static struct fw_xfer *
928fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
929{
930	struct fw_xfer *xfer;
931	struct tlabel *tl;
932	int s = splfw();
933
934	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
935		tl = STAILQ_NEXT(tl, link)){
936		if(tl->xfer->dst == node){
937			xfer = tl->xfer;
938			splx(s);
939			return(xfer);
940		}
941	}
942	splx(s);
943	return(NULL);
944}
945
946/*
947 * To allocate IEEE1394 XFER structure.
948 */
949struct fw_xfer *
950fw_xfer_alloc()
951{
952	struct fw_xfer *xfer;
953
954	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_DONTWAIT | M_ZERO);
955	if (xfer == NULL)
956		return xfer;
957
958	xfer->time = time_second;
959	xfer->sub = -1;
960
961	return xfer;
962}
963
964/*
965 * IEEE1394 XFER post process.
966 */
967void
968fw_xfer_done(struct fw_xfer *xfer)
969{
970	if (xfer->act.hand == NULL)
971		return;
972
973#if XFER_TIMEOUT
974	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
975#endif
976
977	if (xfer->fc->status != FWBUSRESET)
978		xfer->act.hand(xfer);
979	else {
980		printf("fw_xfer_done: pending\n");
981		if (xfer->fc != NULL)
982			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
983		else
984			panic("fw_xfer_done: why xfer->fc is NULL?");
985	}
986}
987
988/*
989 * To free IEEE1394 XFER structure.
990 */
991void
992fw_xfer_free( struct fw_xfer* xfer)
993{
994	int s;
995	if(xfer == NULL ) return;
996	if(xfer->state == FWXF_INQ){
997		printf("fw_xfer_free FWXF_INQ\n");
998		s = splfw();
999		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1000		xfer->q->queued --;
1001		splx(s);
1002	}
1003	if(xfer->fc != NULL){
1004		if(xfer->state == FWXF_START){
1005#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
1006			printf("fw_xfer_free FWXF_START\n");
1007#endif
1008			s = splfw();
1009			xfer->q->drain(xfer->fc, xfer);
1010			splx(s);
1011		}
1012	}
1013	if(xfer->send.buf != NULL){
1014		free(xfer->send.buf, M_DEVBUF);
1015	}
1016	if(xfer->recv.buf != NULL){
1017		free(xfer->recv.buf, M_DEVBUF);
1018	}
1019	if(xfer->fc != NULL){
1020		fw_tl_free(xfer->fc, xfer);
1021	}
1022	free(xfer, M_DEVBUF);
1023}
1024
1025/*
1026 * Callback for PHY configuration.
1027 */
1028static void
1029fw_phy_config_callback(struct fw_xfer *xfer)
1030{
1031#if 0
1032	printf("phy_config done state=%d resp=%d\n",
1033				xfer->state, xfer->resp);
1034#endif
1035	fw_xfer_free(xfer);
1036	/* XXX need bus reset ?? */
1037	/* sc->fc->ibr(xfer->fc);  LOOP */
1038}
1039
1040/*
1041 * To configure PHY.
1042 */
1043static void
1044fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1045{
1046	struct fw_xfer *xfer;
1047	struct fw_pkt *fp;
1048
1049	fc->status = FWBUSPHYCONF;
1050
1051	DELAY(100000);
1052	xfer = fw_xfer_alloc();
1053	xfer->send.len = 12;
1054	xfer->send.off = 0;
1055	xfer->fc = fc;
1056	xfer->retry_req = fw_asybusy;
1057	xfer->act.hand = fw_phy_config_callback;
1058
1059	xfer->send.buf = malloc(sizeof(u_int32_t),
1060					M_DEVBUF, M_DONTWAIT | M_ZERO);
1061	fp = (struct fw_pkt *)xfer->send.buf;
1062	fp->mode.ld[1] = 0;
1063	if (root_node >= 0)
1064		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
1065	if (gap_count >= 0)
1066		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
1067	fp->mode.ld[2] = ~fp->mode.ld[1];
1068/* XXX Dangerous, how to pass PHY packet to device driver */
1069	fp->mode.common.tcode |= FWTCODE_PHY;
1070
1071	if (firewire_debug)
1072		printf("send phy_config root_node=%d gap_count=%d\n",
1073						root_node, gap_count);
1074	fw_asyreq(fc, -1, xfer);
1075}
1076
1077#if 0
1078/*
1079 * Dump self ID.
1080 */
1081static void
1082fw_print_sid(u_int32_t sid)
1083{
1084	union fw_self_id *s;
1085	s = (union fw_self_id *) &sid;
1086	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1087		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1088		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1089		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1090		s->p0.power_class, s->p0.port0, s->p0.port1,
1091		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1092}
1093#endif
1094
1095/*
1096 * To receive self ID.
1097 */
1098void fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1099{
1100	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1101	union fw_self_id *self_id;
1102	u_int i, j, node, c_port = 0, i_branch = 0;
1103
1104	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1105	fc->status = FWBUSINIT;
1106	fc->max_node = fc->nodeid & 0x3f;
1107	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1108	fc->status = FWBUSCYMELECT;
1109	fc->topology_map->crc_len = 2;
1110	fc->topology_map->generation ++;
1111	fc->topology_map->self_id_count = 0;
1112	fc->topology_map->node_count = 0;
1113	fc->speed_map->generation ++;
1114	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1115	self_id = &fc->topology_map->self_id[0];
1116	for(i = 0; i < fc->sid_cnt; i ++){
1117		if (sid[1] != ~sid[0]) {
1118			printf("fw_sidrcv: invalid self-id packet\n");
1119			sid += 2;
1120			continue;
1121		}
1122		*self_id = *((union fw_self_id *)sid);
1123		fc->topology_map->crc_len++;
1124		if(self_id->p0.sequel == 0){
1125			fc->topology_map->node_count ++;
1126			c_port = 0;
1127#if 0
1128			fw_print_sid(sid[0]);
1129#endif
1130			node = self_id->p0.phy_id;
1131			if(fc->max_node < node){
1132				fc->max_node = self_id->p0.phy_id;
1133			}
1134			/* XXX I'm not sure this is the right speed_map */
1135			fc->speed_map->speed[node][node]
1136					= self_id->p0.phy_speed;
1137			for (j = 0; j < node; j ++) {
1138				fc->speed_map->speed[j][node]
1139					= fc->speed_map->speed[node][j]
1140					= min(fc->speed_map->speed[j][j],
1141							self_id->p0.phy_speed);
1142			}
1143			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1144			  (self_id->p0.link_active && self_id->p0.contender)) {
1145				fc->irm = self_id->p0.phy_id;
1146			}
1147			if(self_id->p0.port0 >= 0x2){
1148				c_port++;
1149			}
1150			if(self_id->p0.port1 >= 0x2){
1151				c_port++;
1152			}
1153			if(self_id->p0.port2 >= 0x2){
1154				c_port++;
1155			}
1156		}
1157		if(c_port > 2){
1158			i_branch += (c_port - 2);
1159		}
1160		sid += 2;
1161		self_id++;
1162		fc->topology_map->self_id_count ++;
1163	}
1164	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1165	/* CRC */
1166	fc->topology_map->crc = fw_crc16(
1167			(u_int32_t *)&fc->topology_map->generation,
1168			fc->topology_map->crc_len * 4);
1169	fc->speed_map->crc = fw_crc16(
1170			(u_int32_t *)&fc->speed_map->generation,
1171			fc->speed_map->crc_len * 4);
1172	/* byteswap and copy to CSR */
1173	p = (u_int32_t *)fc->topology_map;
1174	for (i = 0; i <= fc->topology_map->crc_len; i++)
1175		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1176	p = (u_int32_t *)fc->speed_map;
1177	CSRARC(fc, SPED_MAP) = htonl(*p++);
1178	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1179	/* don't byte-swap u_int8_t array */
1180	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1181
1182	fc->max_hop = fc->max_node - i_branch;
1183#if 1
1184	printf(", maxhop <= %d", fc->max_hop);
1185#endif
1186
1187	if(fc->irm == -1 ){
1188		printf(", Not found IRM capable node");
1189	}else{
1190		printf(", cable IRM = %d", fc->irm);
1191		if (fc->irm == fc->nodeid)
1192			printf(" (me)\n");
1193		else
1194			printf("\n");
1195	}
1196
1197	if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){
1198		if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1199			fc->status = FWBUSMGRDONE;
1200			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1201		}else{
1202			fc->status = FWBUSMGRELECT;
1203			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8);
1204		}
1205	}else{
1206		fc->status = FWBUSMGRDONE;
1207		device_printf(fc->bdev, "BMR = %x\n",
1208				CSRARC(fc, BUS_MGR_ID));
1209	}
1210	free(buf, M_DEVBUF);
1211#if 1
1212	/* XXX optimize gap_count, if I am BMGR */
1213	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1214		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
1215	}
1216#endif
1217#if 1
1218	timeout((timeout_t *)fw_bus_probe, (void *)fc, hz/4);
1219#else
1220	fw_bus_probe(fc);
1221#endif
1222}
1223
1224/*
1225 * To probe devices on the IEEE1394 bus.
1226 */
1227static void
1228fw_bus_probe(struct firewire_comm *fc)
1229{
1230	int s;
1231	struct fw_device *fwdev, *next;
1232
1233	s = splfw();
1234	fc->status = FWBUSEXPLORE;
1235	fc->retry_count = 0;
1236
1237/*
1238 * Invalidate all devices, just after bus reset. Devices
1239 * to be removed has not been seen longer time.
1240 */
1241	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1242		next = TAILQ_NEXT(fwdev, link);
1243		if(fwdev->status != FWDEVINVAL){
1244			fwdev->status = FWDEVINVAL;
1245			fwdev->rcnt = 0;
1246		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
1247			fwdev->rcnt ++;
1248		}else{
1249			TAILQ_REMOVE(&fc->devices, fwdev, link);
1250			free(fwdev, M_DEVBUF);
1251		}
1252	}
1253	fc->ongonode = 0;
1254	fc->ongoaddr = CSRROMOFF;
1255	fc->ongodev = NULL;
1256	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1257	fw_bus_explore(fc);
1258	splx(s);
1259}
1260
1261/*
1262 * To collect device informations on the IEEE1394 bus.
1263 */
1264static void
1265fw_bus_explore(struct firewire_comm *fc )
1266{
1267	int err = 0;
1268	struct fw_device *fwdev, *tfwdev;
1269	u_int32_t addr;
1270	struct fw_xfer *xfer;
1271	struct fw_pkt *fp;
1272
1273	if(fc->status != FWBUSEXPLORE)
1274		return;
1275
1276loop:
1277	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1278
1279	if(fc->ongonode > fc->max_node) goto done;
1280	if(fc->ongonode >= 0x3f) goto done;
1281
1282	/* check link */
1283	/* XXX we need to check phy_id first */
1284	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1285		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
1286		fc->ongonode++;
1287		goto loop;
1288	}
1289
1290	if(fc->ongoaddr <= CSRROMOFF &&
1291		fc->ongoeui.hi == 0xffffffff &&
1292		fc->ongoeui.lo == 0xffffffff ){
1293		fc->ongoaddr = CSRROMOFF;
1294		addr = 0xf0000000 | fc->ongoaddr;
1295	}else if(fc->ongoeui.hi == 0xffffffff ){
1296		fc->ongoaddr = CSRROMOFF + 0xc;
1297		addr = 0xf0000000 | fc->ongoaddr;
1298	}else if(fc->ongoeui.lo == 0xffffffff ){
1299		fc->ongoaddr = CSRROMOFF + 0x10;
1300		addr = 0xf0000000 | fc->ongoaddr;
1301	}else if(fc->ongodev == NULL){
1302		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1303			fwdev = TAILQ_NEXT(fwdev, link)){
1304			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
1305				break;
1306			}
1307		}
1308		if(fwdev != NULL){
1309			fwdev->dst = fc->ongonode;
1310			fwdev->status = FWDEVATTACHED;
1311			fc->ongonode++;
1312			fc->ongoaddr = CSRROMOFF;
1313			fc->ongodev = NULL;
1314			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1315			goto loop;
1316		}
1317		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT);
1318		if(fwdev == NULL)
1319			return;
1320		fwdev->fc = fc;
1321		fwdev->rommax = 0;
1322		fwdev->dst = fc->ongonode;
1323		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1324		fwdev->status = FWDEVINIT;
1325#if 0
1326		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1327			>> ((3 - (fc->ongonode % 4)) * 8);
1328#else
1329		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1330#endif
1331
1332		tfwdev = TAILQ_FIRST(&fc->devices);
1333		while( tfwdev != NULL &&
1334			(tfwdev->eui.hi > fwdev->eui.hi) &&
1335			((tfwdev->eui.hi == fwdev->eui.hi) &&
1336				tfwdev->eui.lo > fwdev->eui.lo)){
1337			tfwdev = TAILQ_NEXT( tfwdev, link);
1338		}
1339		if(tfwdev == NULL){
1340			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
1341		}else{
1342			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
1343		}
1344
1345		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1346			linkspeed[fwdev->speed],
1347			fc->ongoeui.hi, fc->ongoeui.lo);
1348
1349		fc->ongodev = fwdev;
1350		fc->ongoaddr = CSRROMOFF;
1351		addr = 0xf0000000 | fc->ongoaddr;
1352	}else{
1353		addr = 0xf0000000 | fc->ongoaddr;
1354	}
1355#if 0
1356	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1357		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1358		fw_bus_explore_callback);
1359	if(xfer == NULL) goto done;
1360#else
1361	xfer = fw_xfer_alloc();
1362	if(xfer == NULL){
1363		goto done;
1364	}
1365	xfer->send.len = 16;
1366	xfer->spd = 0;
1367	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1368	if(xfer->send.buf == NULL){
1369		fw_xfer_free( xfer);
1370		return;
1371	}
1372
1373	xfer->send.off = 0;
1374	fp = (struct fw_pkt *)xfer->send.buf;
1375	fp->mode.rreqq.dest_hi = htons(0xffff);
1376	fp->mode.rreqq.tlrt = 0;
1377	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1378	fp->mode.rreqq.pri = 0;
1379	fp->mode.rreqq.src = 0;
1380	xfer->dst = FWLOCALBUS | fc->ongonode;
1381	fp->mode.rreqq.dst = htons(xfer->dst);
1382	fp->mode.rreqq.dest_lo = htonl(addr);
1383	xfer->act.hand = fw_bus_explore_callback;
1384
1385	err = fw_asyreq(fc, -1, xfer);
1386	if(err){
1387		fw_xfer_free( xfer);
1388		return;
1389	}
1390#endif
1391	return;
1392done:
1393	/* fw_attach_devs */
1394	fc->status = FWBUSEXPDONE;
1395	if (firewire_debug)
1396		printf("bus_explore done\n");
1397	fw_attach_dev(fc);
1398	return;
1399
1400}
1401
1402/* Portable Async. request read quad */
1403struct fw_xfer *
1404asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1405	u_int32_t addr_hi, u_int32_t addr_lo,
1406	void (*hand) __P((struct fw_xfer*)))
1407{
1408	struct fw_xfer *xfer;
1409	struct fw_pkt *fp;
1410	int err;
1411
1412	xfer = fw_xfer_alloc();
1413	if(xfer == NULL){
1414		return NULL;
1415	}
1416	xfer->send.len = 16;
1417	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1418	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1419	if(xfer->send.buf == NULL){
1420		fw_xfer_free( xfer);
1421		return NULL;
1422	}
1423
1424	xfer->send.off = 0;
1425	fp = (struct fw_pkt *)xfer->send.buf;
1426	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1427	if(tl & FWP_TL_VALID){
1428		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1429	}else{
1430		fp->mode.rreqq.tlrt = 0;
1431	}
1432	fp->mode.rreqq.tlrt |= rt & 0x3;
1433	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1434	fp->mode.rreqq.pri = 0;
1435	fp->mode.rreqq.src = 0;
1436	xfer->dst = addr_hi >> 16;
1437	fp->mode.rreqq.dst = htons(xfer->dst);
1438	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1439	xfer->act.hand = hand;
1440
1441	err = fw_asyreq(fc, -1, xfer);
1442	if(err){
1443		fw_xfer_free( xfer);
1444		return NULL;
1445	}
1446	return xfer;
1447}
1448
1449/*
1450 * Callback for the IEEE1394 bus information collection.
1451 */
1452static void
1453fw_bus_explore_callback(struct fw_xfer *xfer)
1454{
1455	struct firewire_comm *fc;
1456	struct fw_pkt *sfp,*rfp;
1457	struct csrhdr *chdr;
1458	struct csrdir *csrd;
1459	struct csrreg *csrreg;
1460	u_int32_t offset;
1461
1462
1463	if(xfer == NULL) return;
1464	fc = xfer->fc;
1465	if(xfer->resp != 0){
1466		printf("resp != 0: node=%d addr=0x%x\n",
1467			fc->ongonode, fc->ongoaddr);
1468		fc->retry_count++;
1469		goto nextnode;
1470	}
1471
1472	if(xfer->send.buf == NULL){
1473		printf("send.buf == NULL: node=%d addr=0x%x\n",
1474			fc->ongonode, fc->ongoaddr);
1475		printf("send.buf == NULL\n");
1476		fc->retry_count++;
1477		goto nextnode;
1478	}
1479	sfp = (struct fw_pkt *)xfer->send.buf;
1480
1481	if(xfer->recv.buf == NULL){
1482		printf("recv.buf == NULL: node=%d addr=0x%x\n",
1483			fc->ongonode, fc->ongoaddr);
1484		fc->retry_count++;
1485		goto nextnode;
1486	}
1487	rfp = (struct fw_pkt *)xfer->recv.buf;
1488#if 0
1489	{
1490		u_int32_t *qld;
1491		int i;
1492		qld = (u_int32_t *)xfer->recv.buf;
1493		printf("len:%d\n", xfer->recv.len);
1494		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1495			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1496			if((i % 16) == 15) printf("\n");
1497		}
1498		if((i % 16) != 15) printf("\n");
1499	}
1500#endif
1501	if(fc->ongodev == NULL){
1502		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1503			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1504			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1505/* If CSR is minimul confinguration, more investgation is not needed. */
1506			if(chdr->info_len == 1){
1507				goto nextnode;
1508			}else{
1509				fc->ongoaddr = CSRROMOFF + 0xc;
1510			}
1511		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1512			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1513			fc->ongoaddr = CSRROMOFF + 0x10;
1514		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1515			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1516			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
1517				goto nextnode;
1518			fc->ongoaddr = CSRROMOFF;
1519		}
1520	}else{
1521		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1522		if(fc->ongoaddr > fc->ongodev->rommax){
1523			fc->ongodev->rommax = fc->ongoaddr;
1524		}
1525		csrd = SLIST_FIRST(&fc->ongocsr);
1526		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1527			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1528			offset = CSRROMOFF;
1529		}else{
1530			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1531			offset = csrd->off;
1532		}
1533		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1534			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1535			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1536				csrd = SLIST_FIRST(&fc->csrfree);
1537				if(csrd == NULL){
1538					goto nextnode;
1539				}else{
1540					csrd->ongoaddr = fc->ongoaddr;
1541					fc->ongoaddr += csrreg->val * 4;
1542					csrd->off = fc->ongoaddr;
1543					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1544					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1545					goto nextaddr;
1546				}
1547			}
1548		}
1549		fc->ongoaddr += 4;
1550		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1551				(fc->ongodev->rommax < 0x414)){
1552			if(fc->ongodev->rommax <= 0x414){
1553				csrd = SLIST_FIRST(&fc->csrfree);
1554				if(csrd == NULL) goto nextnode;
1555				csrd->off = fc->ongoaddr;
1556				csrd->ongoaddr = fc->ongoaddr;
1557				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1558				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1559			}
1560			goto nextaddr;
1561		}
1562
1563		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1564			if(csrd == NULL){
1565				goto nextnode;
1566			};
1567			fc->ongoaddr = csrd->ongoaddr + 4;
1568			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1569			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1570			csrd = SLIST_FIRST(&fc->ongocsr);
1571			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1572				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1573				offset = CSRROMOFF;
1574			}else{
1575				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1576				offset = csrd->off;
1577			}
1578		}
1579		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1580			goto nextnode;
1581		}
1582	}
1583nextaddr:
1584	fw_xfer_free( xfer);
1585	fw_bus_explore(fc);
1586	return;
1587nextnode:
1588	fw_xfer_free( xfer);
1589	fc->ongonode++;
1590/* housekeeping work space */
1591	fc->ongoaddr = CSRROMOFF;
1592	fc->ongodev = NULL;
1593	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1594	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1595		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1596		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1597	}
1598	fw_bus_explore(fc);
1599	return;
1600}
1601
1602/*
1603 * To obtain CSR register values.
1604 */
1605u_int32_t
1606getcsrdata(struct fw_device *fwdev, u_int8_t key)
1607{
1608	int i;
1609	struct csrhdr *chdr;
1610	struct csrreg *creg;
1611	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1612	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1613		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1614		if(creg->key == key){
1615			return (u_int32_t)creg->val;
1616		}
1617	}
1618	return 0;
1619}
1620
1621/*
1622 * To attach sub-devices layer onto IEEE1394 bus.
1623 */
1624static void
1625fw_attach_dev(struct firewire_comm *fc)
1626{
1627	struct fw_device *fwdev;
1628	struct fw_xfer *xfer;
1629	int i, err;
1630	device_t *devlistp;
1631	int devcnt;
1632	struct firewire_dev_comm *fdc;
1633	u_int32_t spec, ver;
1634
1635	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1636			fwdev = TAILQ_NEXT(fwdev, link)){
1637		if(fwdev->status == FWDEVINIT){
1638			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1639			if(spec == 0)
1640				continue;
1641			ver = getcsrdata(fwdev, CSRKEY_VER);
1642			if(ver == 0)
1643				continue;
1644			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1645
1646			device_printf(fc->bdev, "Device ");
1647			switch(spec){
1648			case CSRVAL_ANSIT10:
1649				switch(ver){
1650				case CSRVAL_T10SBP2:
1651					printf("SBP-II");
1652					break;
1653				default:
1654					break;
1655				}
1656				break;
1657			case CSRVAL_1394TA:
1658				switch(ver){
1659				case CSR_PROTAVC:
1660					printf("AV/C");
1661					break;
1662				case CSR_PROTCAL:
1663					printf("CAL");
1664					break;
1665				case CSR_PROTEHS:
1666					printf("EHS");
1667					break;
1668				case CSR_PROTHAVI:
1669					printf("HAVi");
1670					break;
1671				case CSR_PROTCAM104:
1672					printf("1394 Cam 1.04");
1673					break;
1674				case CSR_PROTCAM120:
1675					printf("1394 Cam 1.20");
1676					break;
1677				case CSR_PROTCAM130:
1678					printf("1394 Cam 1.30");
1679					break;
1680				case CSR_PROTDPP:
1681					printf("1394 Direct print");
1682					break;
1683				case CSR_PROTIICP:
1684					printf("Industrial & Instrument");
1685					break;
1686				default:
1687					printf("unknown 1394TA");
1688					break;
1689				}
1690				break;
1691			default:
1692				printf("unknown spec");
1693				break;
1694			}
1695			fwdev->status = FWDEVATTACHED;
1696			printf("\n");
1697		}
1698	}
1699	err = device_get_children(fc->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