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