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