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