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