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