firewire.c revision 110016
1103285Sikob/*
2103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3103285Sikob * All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the acknowledgement as bellow:
15103285Sikob *
16103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
17103285Sikob *
18103285Sikob * 4. The name of the author may not be used to endorse or promote products
19103285Sikob *    derived from this software without specific prior written permission.
20103285Sikob *
21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31103285Sikob * POSSIBILITY OF SUCH DAMAGE.
32103285Sikob *
33103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 110016 2003-01-29 02:13:31Z simokawa $
34103285Sikob *
35103285Sikob */
36103285Sikob
37103285Sikob#include <sys/param.h>
38103285Sikob#include <sys/systm.h>
39103285Sikob#include <sys/types.h>
40103285Sikob#include <sys/mbuf.h>
41103285Sikob#include <sys/socket.h>
42103285Sikob#include <sys/socketvar.h>
43103285Sikob
44103285Sikob#include <sys/kernel.h>
45103285Sikob#include <sys/malloc.h>
46103285Sikob#include <sys/conf.h>
47103285Sikob#include <sys/uio.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob
50103285Sikob#include <machine/cpufunc.h>    /* for rdtsc proto for clock.h below */
51103285Sikob#include <machine/clock.h>
52103285Sikob
53103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
54103285Sikob
55103285Sikob#include <dev/firewire/firewire.h>
56103285Sikob#include <dev/firewire/firewirereg.h>
57103285Sikob#include <dev/firewire/iec13213.h>
58103285Sikob#include <dev/firewire/iec68113.h>
59103285Sikob
60109736Ssimokawaint firewire_debug=0, try_bmr=1;
61103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
62108281Ssimokawa	"FireWire driver debug flag");
63109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
64109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
65109736Ssimokawa	"Try to be a bus manager");
66103285Sikob
67103285Sikob#define FW_MAXASYRTY 4
68103285Sikob#define FW_MAXDEVRCNT 4
69103285Sikob
70103285Sikob#define XFER_TIMEOUT 0
71103285Sikob
72103285Sikobdevclass_t firewire_devclass;
73103285Sikob
74103285Sikobstatic int firewire_match      __P((device_t));
75103285Sikobstatic int firewire_attach      __P((device_t));
76103285Sikobstatic int firewire_detach      __P((device_t));
77103285Sikob#if 0
78103285Sikobstatic int firewire_shutdown    __P((device_t));
79103285Sikob#endif
80103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
81103285Sikobstatic void fw_try_bmr __P((void *));
82103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
83103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
84103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
85103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
86103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
87103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
88103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
89106543Ssimokawa#ifdef FW_VMACCESS
90103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
91106543Ssimokawa#endif
92103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
93105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
94103285Sikob
95103285Sikobstatic device_method_t firewire_methods[] = {
96103285Sikob	/* Device interface */
97103285Sikob	DEVMETHOD(device_probe,		firewire_match),
98103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
99103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
100108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
101108642Ssimokawa	DEVMETHOD(device_resume,	bus_generic_resume),
102103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
103103285Sikob
104103285Sikob	/* Bus interface */
105103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
106103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
107103285Sikob
108103285Sikob	{ 0, 0 }
109103285Sikob};
110103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
111103285Sikob
112103285Sikob#define MAX_GAPHOP  16
113103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
114103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
115106813Ssimokawa
116106813Ssimokawaextern struct cdevsw firewire_cdevsw;
117106813Ssimokawa
118103285Sikobstatic driver_t firewire_driver = {
119103285Sikob	"firewire",
120103285Sikob	firewire_methods,
121103285Sikob	sizeof(struct firewire_softc),
122103285Sikob};
123103285Sikob
124103285Sikob/*
125103285Sikob * To lookup node id. from EUI64.
126103285Sikob */
127106810Ssimokawastruct fw_device *
128106790Ssimokawafw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui)
129103285Sikob{
130103285Sikob	struct fw_device *fwdev;
131103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
132103285Sikob		fwdev = TAILQ_NEXT(fwdev, link)){
133103285Sikob		if(fwdev->eui.hi == eui.hi && fwdev->eui.lo == eui.lo){
134103285Sikob			break;
135103285Sikob		}
136103285Sikob	}
137106810Ssimokawa	if(fwdev == NULL) return NULL;
138106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
139106810Ssimokawa	return fwdev;
140103285Sikob}
141106813Ssimokawa
142103285Sikob/*
143103285Sikob * Async. request procedure for userland application.
144103285Sikob */
145103285Sikobint
146103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
147103285Sikob{
148103285Sikob	int err = 0;
149103285Sikob	struct fw_xferq *xferq;
150103285Sikob	int tl = 0, len;
151103285Sikob	struct fw_pkt *fp;
152103285Sikob	int tcode;
153103285Sikob	struct tcode_info *info;
154103285Sikob
155103285Sikob	if(xfer == NULL) return EINVAL;
156108701Ssimokawa	if(xfer->send.len > MAXREC(fc->maxrec)){
157103285Sikob		printf("send.len > maxrec\n");
158103285Sikob		return EINVAL;
159103285Sikob	}
160103285Sikob	if(xfer->act.hand == NULL){
161103285Sikob		printf("act.hand == NULL\n");
162103285Sikob		return EINVAL;
163103285Sikob	}
164103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
165103285Sikob
166103285Sikob	tcode = fp->mode.common.tcode & 0xf;
167103285Sikob	info = &fc->tcode[tcode];
168103285Sikob	if (info->flag == 0) {
169103285Sikob		printf("invalid tcode=%d\n", tcode);
170103285Sikob		return EINVAL;
171103285Sikob	}
172103285Sikob	if (info->flag & FWTI_REQ)
173103285Sikob		xferq = fc->atq;
174103285Sikob	else
175103285Sikob		xferq = fc->ats;
176103285Sikob	len = info->hdr_len;
177103285Sikob	if (info->flag & FWTI_BLOCK_STR)
178103285Sikob		len += ntohs(fp->mode.stream.len);
179103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
180103285Sikob		len += ntohs(fp->mode.rresb.len);
181103285Sikob	if( len >  xfer->send.len ){
182103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
183103285Sikob				len, xfer->send.len, tcode);
184103285Sikob		return EINVAL;
185103285Sikob	}
186103285Sikob	xfer->send.len = len;
187106790Ssimokawa
188103285Sikob	if(xferq->start == NULL){
189103285Sikob		printf("xferq->start == NULL\n");
190103285Sikob		return EINVAL;
191103285Sikob	}
192103285Sikob	if(!(xferq->queued < xferq->maxq)){
193108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
194108655Ssimokawa			xferq->queued);
195103285Sikob		return EINVAL;
196103285Sikob	}
197103285Sikob
198103285Sikob
199103285Sikob	if (info->flag & FWTI_TLABEL) {
200103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
201103285Sikob			return EIO;
202103285Sikob		fp->mode.hdr.tlrt = tl << 2;
203103285Sikob	}
204103285Sikob
205103285Sikob	xfer->tl = tl;
206103285Sikob	xfer->tcode = tcode;
207103285Sikob	xfer->resp = 0;
208103285Sikob	xfer->fc = fc;
209103285Sikob	xfer->q = xferq;
210103285Sikob	xfer->act_type = FWACT_XFER;
211103285Sikob	xfer->retry_req = fw_asybusy;
212103285Sikob
213103285Sikob	fw_asystart(xfer);
214103285Sikob	return err;
215103285Sikob}
216103285Sikob/*
217103285Sikob * Wakeup blocked process.
218103285Sikob */
219103285Sikobvoid
220103285Sikobfw_asy_callback(struct fw_xfer *xfer){
221103285Sikob	wakeup(xfer);
222103285Sikob	return;
223103285Sikob}
224103285Sikob/*
225103285Sikob * Postpone to later retry.
226103285Sikob */
227103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
228106815Ssimokawa#if 1
229103285Sikob	printf("fw_asybusy\n");
230103285Sikob#endif
231103285Sikob#if XFER_TIMEOUT
232103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
233103285Sikob#endif
234103285Sikob/*
235103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
236103285Sikob*/
237103285Sikob	DELAY(20000);
238103285Sikob	fw_asystart(xfer);
239103285Sikob	return;
240103285Sikob}
241103285Sikob#if XFER_TIMEOUT
242103285Sikob/*
243103285Sikob * Post timeout for async. request.
244103285Sikob */
245103285Sikobvoid
246103285Sikobfw_xfer_timeout(void *arg)
247103285Sikob{
248103285Sikob	int s;
249103285Sikob	struct fw_xfer *xfer;
250103285Sikob
251103285Sikob	xfer = (struct fw_xfer *)arg;
252103285Sikob	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
253103285Sikob	/* XXX set error code */
254103285Sikob	s = splfw();
255103285Sikob	xfer->act.hand(xfer);
256103285Sikob	splx(s);
257103285Sikob}
258103285Sikob#endif
259103285Sikob/*
260103285Sikob * Async. request with given xfer structure.
261103285Sikob */
262106790Ssimokawastatic void
263106790Ssimokawafw_asystart(struct fw_xfer *xfer)
264106790Ssimokawa{
265103285Sikob	struct firewire_comm *fc = xfer->fc;
266103285Sikob	int s;
267103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
268103285Sikob		xfer->resp = EBUSY;
269103285Sikob		xfer->state = FWXF_BUSY;
270103285Sikob		xfer->act.hand(xfer);
271103285Sikob		return;
272103285Sikob	}
273103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
274103285Sikob	if (fc->status < FWBUSEXPLORE) {
275103285Sikob		xfer->resp = EAGAIN;
276103285Sikob		xfer->state = FWXF_BUSY;
277103285Sikob		if (xfer->act.hand != NULL)
278103285Sikob			xfer->act.hand(xfer);
279103285Sikob		return;
280103285Sikob	}
281103285Sikob#endif
282103285Sikob	s = splfw();
283103285Sikob	xfer->state = FWXF_INQ;
284103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
285103285Sikob	xfer->q->queued ++;
286103285Sikob	splx(s);
287103285Sikob	/* XXX just queue for mbuf */
288103285Sikob	if (xfer->mbuf == NULL)
289103285Sikob		xfer->q->start(fc);
290103285Sikob#if XFER_TIMEOUT
291103285Sikob	if (xfer->act.hand != NULL)
292103285Sikob		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
293103285Sikob#endif
294103285Sikob	return;
295103285Sikob}
296106790Ssimokawa
297103285Sikobstatic int
298103285Sikobfirewire_match( device_t dev )
299103285Sikob{
300108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
301103285Sikob	return -140;
302103285Sikob}
303106790Ssimokawa
304103285Sikob/*
305103285Sikob * The attach routine.
306103285Sikob */
307103285Sikobstatic int
308103285Sikobfirewire_attach( device_t dev )
309103285Sikob{
310103285Sikob	int i, unitmask, mn;
311103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
312103285Sikob	device_t pa = device_get_parent(dev);
313103285Sikob	struct firewire_comm *fc;
314103285Sikob	dev_t d;
315103285Sikob
316103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
317103285Sikob	sc->fc = fc;
318103285Sikob
319103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
320103285Sikob
321103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
322103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
323103285Sikob		mn = unitmask | i;
324103285Sikob		/* XXX device name should be improved */
325103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
326108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
327103285Sikob			"fw%x", mn);
328103285Sikob#if __FreeBSD_version >= 500000
329103285Sikob		if (i == 0)
330103285Sikob			sc->dev = d;
331103285Sikob		else
332103285Sikob			dev_depends(sc->dev, d);
333103285Sikob#else
334103285Sikob		sc->dev[i] = d;
335103285Sikob#endif
336103285Sikob	}
337103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
338108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
339103285Sikob			"fwmem%d", device_get_unit(dev));
340103285Sikob#if __FreeBSD_version >= 500000
341103285Sikob	dev_depends(sc->dev, d);
342103285Sikob#else
343103285Sikob	sc->dev[i] = d;
344103285Sikob#endif
345103285Sikob	sc->fc->timeouthandle = timeout((timeout_t *)sc->fc->timeout, (void *)sc->fc, hz * 10);
346103285Sikob
347108853Ssimokawa	callout_init(&sc->fc->busprobe_callout
348108853Ssimokawa#if __FreeBSD_version >= 500000
349108853Ssimokawa						, /* mpsafe? */ 0);
350108853Ssimokawa#else
351108853Ssimokawa						);
352108853Ssimokawa#endif
353108853Ssimokawa
354103285Sikob	/* Locate our children */
355103285Sikob	bus_generic_probe(dev);
356103285Sikob
357103285Sikob	/* launch attachement of the added children */
358103285Sikob	bus_generic_attach(dev);
359103285Sikob
360103285Sikob	/* bus_reset */
361103285Sikob	fc->ibr(fc);
362103285Sikob
363103285Sikob	return 0;
364103285Sikob}
365103285Sikob
366103285Sikob/*
367103285Sikob * Attach it as child.
368103285Sikob */
369103285Sikobstatic device_t
370103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
371103285Sikob{
372103285Sikob        device_t child;
373103285Sikob	struct firewire_softc *sc;
374103285Sikob
375103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
376103285Sikob	child = device_add_child(dev, name, unit);
377103285Sikob	if (child) {
378103285Sikob		device_set_ivars(child, sc->fc);
379103285Sikob		device_probe_and_attach(child);
380103285Sikob	}
381103285Sikob
382103285Sikob	return child;
383103285Sikob}
384106790Ssimokawa
385103285Sikob/*
386103285Sikob * Dettach it.
387103285Sikob */
388103285Sikobstatic int
389103285Sikobfirewire_detach( device_t dev )
390103285Sikob{
391103285Sikob	struct firewire_softc *sc;
392103285Sikob
393103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
394106790Ssimokawa
395103285Sikob#if __FreeBSD_version >= 500000
396103285Sikob	destroy_dev(sc->dev);
397103285Sikob#else
398103285Sikob	{
399103285Sikob		int j;
400103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
401103285Sikob			destroy_dev(sc->dev[j]);
402103285Sikob	}
403103285Sikob#endif
404103285Sikob	/* XXX xfree_free and untimeout on all xfers */
405103285Sikob	untimeout((timeout_t *)sc->fc->timeout, sc->fc, sc->fc->timeouthandle);
406103285Sikob	free(sc->fc->topology_map, M_DEVBUF);
407103285Sikob	free(sc->fc->speed_map, M_DEVBUF);
408103285Sikob	bus_generic_detach(dev);
409103285Sikob	return(0);
410103285Sikob}
411103285Sikob#if 0
412103285Sikobstatic int
413103285Sikobfirewire_shutdown( device_t dev )
414103285Sikob{
415103285Sikob	return 0;
416103285Sikob}
417103285Sikob#endif
418106790Ssimokawa
419103285Sikob/*
420106790Ssimokawa * Called after bus reset.
421103285Sikob */
422106790Ssimokawavoid
423106790Ssimokawafw_busreset(struct firewire_comm *fc)
424103285Sikob{
425103285Sikob	int i;
426103285Sikob	struct fw_xfer *xfer;
427103285Sikob
428103285Sikob	switch(fc->status){
429103285Sikob	case FWBUSMGRELECT:
430103285Sikob		untimeout((timeout_t *)fw_try_bmr, (void *)fc, fc->bmrhandle);
431103285Sikob		break;
432103285Sikob	default:
433103285Sikob		break;
434103285Sikob	}
435103285Sikob	fc->status = FWBUSRESET;
436103285Sikob/* XXX: discard all queued packet */
437103285Sikob	while((xfer = STAILQ_FIRST(&fc->atq->q)) != NULL){
438103285Sikob		STAILQ_REMOVE_HEAD(&fc->atq->q, link);
439103285Sikob		xfer->resp = EAGAIN;
440103285Sikob		switch(xfer->act_type){
441103285Sikob		case FWACT_XFER:
442103285Sikob			fw_xfer_done(xfer);
443103285Sikob			break;
444103285Sikob		default:
445103285Sikob			break;
446103285Sikob		}
447103285Sikob		fw_xfer_free( xfer);
448103285Sikob	}
449103285Sikob	while((xfer = STAILQ_FIRST(&fc->ats->q)) != NULL){
450103285Sikob		STAILQ_REMOVE_HEAD(&fc->ats->q, link);
451103285Sikob		xfer->resp = EAGAIN;
452103285Sikob		switch(xfer->act_type){
453103285Sikob		case FWACT_XFER:
454103285Sikob			fw_xfer_done(xfer);
455103285Sikob		default:
456103285Sikob			break;
457103285Sikob		}
458103285Sikob		fw_xfer_free( xfer);
459103285Sikob	}
460103285Sikob	for(i = 0; i < fc->nisodma; i++)
461103285Sikob		while((xfer = STAILQ_FIRST(&fc->it[i]->q)) != NULL){
462103285Sikob			STAILQ_REMOVE_HEAD(&fc->it[i]->q, link);
463103285Sikob			xfer->resp = 0;
464103285Sikob			switch(xfer->act_type){
465103285Sikob			case FWACT_XFER:
466103285Sikob				fw_xfer_done(xfer);
467103285Sikob				break;
468103285Sikob			default:
469103285Sikob				break;
470103285Sikob			}
471103285Sikob			fw_xfer_free( xfer);
472103285Sikob		}
473103285Sikob
474103285Sikob	CSRARC(fc, STATE_CLEAR)
475103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
476103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
477103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
478103285Sikob
479103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
480103285Sikob	fc->irm = -1;
481103285Sikob
482103285Sikob	fc->max_node = -1;
483103285Sikob
484103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
485103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
486103285Sikob	}
487103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
488103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
489103285Sikob	CSRARC(fc, RESET_START) = 0;
490103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
491103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
492103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
493103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
494103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
495103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
496103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
497103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
498103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
499103285Sikob
500103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
501103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
502103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
503103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
504103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
505103285Sikob
506103285Sikob/* DV depend CSRs see blue book */
507103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
508103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
509103285Sikob
510103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
511103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
512103285Sikob}
513106790Ssimokawa
514103285Sikob/* Call once after reboot */
515106790Ssimokawavoid fw_init(struct firewire_comm *fc)
516103285Sikob{
517103285Sikob	int i;
518106543Ssimokawa	struct csrdir *csrd;
519106543Ssimokawa#ifdef FW_VMACCESS
520103285Sikob	struct fw_xfer *xfer;
521103285Sikob	struct fw_bind *fwb;
522106543Ssimokawa#endif
523103285Sikob
524103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
525103285Sikob
526103285Sikob	fc->arq->queued = 0;
527103285Sikob	fc->ars->queued = 0;
528103285Sikob	fc->atq->queued = 0;
529103285Sikob	fc->ats->queued = 0;
530103285Sikob
531109379Ssimokawa	fc->arq->psize = PAGE_SIZE;
532109379Ssimokawa	fc->ars->psize = PAGE_SIZE;
533109379Ssimokawa	fc->atq->psize = 0;
534109379Ssimokawa	fc->ats->psize = 0;
535103285Sikob
536103285Sikob
537103285Sikob	fc->arq->buf = NULL;
538103285Sikob	fc->ars->buf = NULL;
539103285Sikob	fc->atq->buf = NULL;
540103285Sikob	fc->ats->buf = NULL;
541103285Sikob
542103285Sikob	fc->arq->flag = FWXFERQ_PACKET;
543103285Sikob	fc->ars->flag = FWXFERQ_PACKET;
544103285Sikob	fc->atq->flag = FWXFERQ_PACKET;
545103285Sikob	fc->ats->flag = FWXFERQ_PACKET;
546103285Sikob
547103285Sikob	STAILQ_INIT(&fc->atq->q);
548103285Sikob	STAILQ_INIT(&fc->ats->q);
549103285Sikob
550103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
551103285Sikob		fc->it[i]->queued = 0;
552103285Sikob		fc->ir[i]->queued = 0;
553103285Sikob
554103285Sikob		fc->it[i]->start = NULL;
555103285Sikob		fc->ir[i]->start = NULL;
556103285Sikob
557103285Sikob		fc->it[i]->buf = NULL;
558103285Sikob		fc->ir[i]->buf = NULL;
559103285Sikob
560103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
561103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
562103285Sikob
563103285Sikob		STAILQ_INIT(&fc->it[i]->q);
564103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
565103285Sikob
566103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
567103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
568103285Sikob	}
569103285Sikob
570103285Sikob	fc->arq->maxq = FWMAXQUEUE;
571103285Sikob	fc->ars->maxq = FWMAXQUEUE;
572103285Sikob	fc->atq->maxq = FWMAXQUEUE;
573103285Sikob	fc->ats->maxq = FWMAXQUEUE;
574103285Sikob
575103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
576103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
577103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
578103285Sikob	}
579103285Sikob/* Initialize csr registers */
580103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
581103285Sikob				sizeof(struct fw_topology_map),
582109424Ssimokawa				M_DEVBUF, M_NOWAIT | M_ZERO);
583103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
584103285Sikob				sizeof(struct fw_speed_map),
585109424Ssimokawa				M_DEVBUF, M_NOWAIT | M_ZERO);
586103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
587103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
588103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
589103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
590103285Sikob
591103285Sikob	TAILQ_INIT(&fc->devices);
592103285Sikob	STAILQ_INIT(&fc->pending);
593103285Sikob
594103285Sikob/* Initialize csr ROM work space */
595103285Sikob	SLIST_INIT(&fc->ongocsr);
596103285Sikob	SLIST_INIT(&fc->csrfree);
597103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
598109424Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_DEVBUF,M_NOWAIT);
599103285Sikob		if(csrd == NULL) break;
600103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
601103285Sikob	}
602103285Sikob
603103285Sikob/* Initialize Async handlers */
604103285Sikob	STAILQ_INIT(&fc->binds);
605103285Sikob	for( i = 0 ; i < 0x40 ; i++){
606103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
607103285Sikob	}
608103285Sikob
609103285Sikob/* DV depend CSRs see blue book */
610103285Sikob#if 0
611103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
612103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
613103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
614103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
615103285Sikob	}
616103285Sikob
617103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
618103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
619103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
620103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
621103285Sikob	}
622103285Sikob#endif
623103285Sikob
624103285Sikob
625106543Ssimokawa#ifdef FW_VMACCESS
626103285Sikob	xfer = fw_xfer_alloc();
627103285Sikob	if(xfer == NULL) return;
628103285Sikob
629109424Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_NOWAIT);
630103285Sikob	if(fwb == NULL){
631103285Sikob		fw_xfer_free(xfer);
632103285Sikob	}
633103285Sikob	xfer->act.hand = fw_vmaccess;
634103285Sikob	xfer->act_type = FWACT_XFER;
635103285Sikob	xfer->fc = fc;
636103285Sikob	xfer->sc = NULL;
637103285Sikob
638103285Sikob	fwb->start_hi = 0x2;
639103285Sikob	fwb->start_lo = 0;
640103285Sikob	fwb->addrlen = 0xffffffff;
641103285Sikob	fwb->xfer = xfer;
642103285Sikob	fw_bindadd(fc, fwb);
643106543Ssimokawa#endif
644103285Sikob}
645106790Ssimokawa
646103285Sikob/*
647103285Sikob * To lookup binded process from IEEE1394 address.
648103285Sikob */
649106813Ssimokawastruct fw_bind *
650106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
651103285Sikob{
652103285Sikob	struct fw_bind *tfw;
653103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
654103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
655103285Sikob		if(tfw->xfer->act_type != FWACT_NULL &&
656103285Sikob			tfw->start_hi == dest_hi &&
657103285Sikob			tfw->start_lo <= dest_lo &&
658103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
659103285Sikob			return(tfw);
660103285Sikob		}
661103285Sikob	}
662103285Sikob	return(NULL);
663103285Sikob}
664106790Ssimokawa
665103285Sikob/*
666103285Sikob * To bind IEEE1394 address block to process.
667103285Sikob */
668106790Ssimokawaint
669106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
670103285Sikob{
671103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
672103285Sikob	int err = 0;
673103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
674103285Sikob	if(tfw == NULL){
675103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
676103285Sikob		goto out;
677103285Sikob	}
678103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
679103285Sikob		(tfw->start_hi == fwb->start_hi &&
680103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
681103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
682103285Sikob		goto out;
683103285Sikob	}
684103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
685103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
686103285Sikob		   (tfw->start_hi == fwb->start_hi &&
687103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
688103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
689103285Sikob			if(tfw2 == NULL)
690103285Sikob				break;
691103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
692103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
693103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
694103285Sikob				break;
695103285Sikob			}else{
696103285Sikob				err = EBUSY;
697103285Sikob				goto out;
698103285Sikob			}
699103285Sikob		}
700103285Sikob	}
701103285Sikob	if(tfw != NULL){
702103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
703103285Sikob	}else{
704103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
705103285Sikob	}
706103285Sikobout:
707103285Sikob	if(!err && fwb->xfer->act_type == FWACT_CH){
708103285Sikob		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
709103285Sikob	}
710103285Sikob	return err;
711103285Sikob}
712103285Sikob
713103285Sikob/*
714103285Sikob * To free IEEE1394 address block.
715103285Sikob */
716106790Ssimokawaint
717106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
718103285Sikob{
719103285Sikob	int s;
720103285Sikob
721103285Sikob	s = splfw();
722103285Sikob	/* shall we check the existance? */
723103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
724103285Sikob	splx(s);
725103285Sikob	if (fwb->xfer)
726103285Sikob		fw_xfer_free(fwb->xfer);
727103285Sikob
728103285Sikob	return 0;
729103285Sikob}
730103285Sikob
731103285Sikob/*
732103285Sikob * To free transaction label.
733103285Sikob */
734106790Ssimokawastatic void
735106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
736103285Sikob{
737103285Sikob	struct tlabel *tl;
738103285Sikob	int s = splfw();
739103285Sikob
740103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
741103285Sikob		tl = STAILQ_NEXT(tl, link)){
742103285Sikob		if(tl->xfer == xfer){
743103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
744103285Sikob			free(tl, M_DEVBUF);
745103285Sikob			splx(s);
746103285Sikob			return;
747103285Sikob		}
748103285Sikob	}
749103285Sikob	splx(s);
750103285Sikob	return;
751103285Sikob}
752106790Ssimokawa
753103285Sikob/*
754103285Sikob * To obtain XFER structure by transaction label.
755103285Sikob */
756106790Ssimokawastatic struct fw_xfer *
757106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
758103285Sikob{
759103285Sikob	struct fw_xfer *xfer;
760103285Sikob	struct tlabel *tl;
761103285Sikob	int s = splfw();
762103285Sikob
763103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
764103285Sikob		tl = STAILQ_NEXT(tl, link)){
765103285Sikob		if(tl->xfer->dst == node){
766103285Sikob			xfer = tl->xfer;
767103285Sikob			splx(s);
768103285Sikob			return(xfer);
769103285Sikob		}
770103285Sikob	}
771103285Sikob	splx(s);
772103285Sikob	return(NULL);
773103285Sikob}
774106790Ssimokawa
775103285Sikob/*
776103285Sikob * To allocate IEEE1394 XFER structure.
777103285Sikob */
778106790Ssimokawastruct fw_xfer *
779106790Ssimokawafw_xfer_alloc()
780103285Sikob{
781103285Sikob	struct fw_xfer *xfer;
782106790Ssimokawa
783109424Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_NOWAIT | M_ZERO);
784106790Ssimokawa	if (xfer == NULL)
785106790Ssimokawa		return xfer;
786106790Ssimokawa
787103285Sikob	xfer->time = time_second;
788103285Sikob	xfer->sub = -1;
789106790Ssimokawa
790103285Sikob	return xfer;
791103285Sikob}
792106790Ssimokawa
793103285Sikob/*
794103285Sikob * IEEE1394 XFER post process.
795103285Sikob */
796103285Sikobvoid
797103285Sikobfw_xfer_done(struct fw_xfer *xfer)
798103285Sikob{
799103285Sikob	if (xfer->act.hand == NULL)
800103285Sikob		return;
801103285Sikob
802103285Sikob#if XFER_TIMEOUT
803103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
804103285Sikob#endif
805103285Sikob
806103285Sikob	if (xfer->fc->status != FWBUSRESET)
807103285Sikob		xfer->act.hand(xfer);
808103285Sikob	else {
809103285Sikob		printf("fw_xfer_done: pending\n");
810103285Sikob		if (xfer->fc != NULL)
811103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
812103285Sikob		else
813103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
814103285Sikob	}
815103285Sikob}
816103285Sikob
817103285Sikob/*
818103285Sikob * To free IEEE1394 XFER structure.
819103285Sikob */
820106790Ssimokawavoid
821106790Ssimokawafw_xfer_free( struct fw_xfer* xfer)
822103285Sikob{
823103285Sikob	int s;
824103285Sikob	if(xfer == NULL ) return;
825103285Sikob	if(xfer->state == FWXF_INQ){
826103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
827103285Sikob		s = splfw();
828103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
829103285Sikob		xfer->q->queued --;
830103285Sikob		splx(s);
831103285Sikob	}
832103285Sikob	if(xfer->fc != NULL){
833103285Sikob		if(xfer->state == FWXF_START){
834103285Sikob#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
835103285Sikob			printf("fw_xfer_free FWXF_START\n");
836103285Sikob#endif
837103285Sikob			s = splfw();
838103285Sikob			xfer->q->drain(xfer->fc, xfer);
839103285Sikob			splx(s);
840103285Sikob		}
841103285Sikob	}
842103285Sikob	if(xfer->send.buf != NULL){
843103285Sikob		free(xfer->send.buf, M_DEVBUF);
844103285Sikob	}
845103285Sikob	if(xfer->recv.buf != NULL){
846103285Sikob		free(xfer->recv.buf, M_DEVBUF);
847103285Sikob	}
848103285Sikob	if(xfer->fc != NULL){
849103285Sikob		fw_tl_free(xfer->fc, xfer);
850103285Sikob	}
851103285Sikob	free(xfer, M_DEVBUF);
852103285Sikob}
853103285Sikob
854103285Sikob/*
855103285Sikob * Callback for PHY configuration.
856103285Sikob */
857103285Sikobstatic void
858103285Sikobfw_phy_config_callback(struct fw_xfer *xfer)
859103285Sikob{
860103285Sikob#if 0
861103285Sikob	printf("phy_config done state=%d resp=%d\n",
862103285Sikob				xfer->state, xfer->resp);
863103285Sikob#endif
864103285Sikob	fw_xfer_free(xfer);
865103285Sikob	/* XXX need bus reset ?? */
866103285Sikob	/* sc->fc->ibr(xfer->fc);  LOOP */
867103285Sikob}
868103285Sikob
869103285Sikob/*
870103285Sikob * To configure PHY.
871103285Sikob */
872103285Sikobstatic void
873103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
874103285Sikob{
875103285Sikob	struct fw_xfer *xfer;
876103285Sikob	struct fw_pkt *fp;
877103285Sikob
878103285Sikob	fc->status = FWBUSPHYCONF;
879103285Sikob
880109280Ssimokawa#if 0
881103285Sikob	DELAY(100000);
882109280Ssimokawa#endif
883103285Sikob	xfer = fw_xfer_alloc();
884103285Sikob	xfer->send.len = 12;
885103285Sikob	xfer->send.off = 0;
886103285Sikob	xfer->fc = fc;
887103285Sikob	xfer->retry_req = fw_asybusy;
888103285Sikob	xfer->act.hand = fw_phy_config_callback;
889103285Sikob
890103285Sikob	xfer->send.buf = malloc(sizeof(u_int32_t),
891109424Ssimokawa					M_DEVBUF, M_NOWAIT | M_ZERO);
892103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
893103285Sikob	fp->mode.ld[1] = 0;
894103285Sikob	if (root_node >= 0)
895103285Sikob		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
896103285Sikob	if (gap_count >= 0)
897103285Sikob		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
898103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
899103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
900103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
901103285Sikob
902107653Ssimokawa	if (firewire_debug)
903107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
904103285Sikob						root_node, gap_count);
905103285Sikob	fw_asyreq(fc, -1, xfer);
906103285Sikob}
907103285Sikob
908103285Sikob#if 0
909103285Sikob/*
910103285Sikob * Dump self ID.
911103285Sikob */
912103285Sikobstatic void
913103285Sikobfw_print_sid(u_int32_t sid)
914103285Sikob{
915103285Sikob	union fw_self_id *s;
916103285Sikob	s = (union fw_self_id *) &sid;
917103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
918103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
919103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
920103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
921103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
922103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
923103285Sikob}
924103285Sikob#endif
925103285Sikob
926103285Sikob/*
927103285Sikob * To receive self ID.
928103285Sikob */
929103285Sikobvoid fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
930103285Sikob{
931103285Sikob	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
932103285Sikob	union fw_self_id *self_id;
933103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
934103285Sikob
935103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
936103285Sikob	fc->status = FWBUSINIT;
937103285Sikob	fc->max_node = fc->nodeid & 0x3f;
938103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
939103285Sikob	fc->status = FWBUSCYMELECT;
940103285Sikob	fc->topology_map->crc_len = 2;
941103285Sikob	fc->topology_map->generation ++;
942103285Sikob	fc->topology_map->self_id_count = 0;
943103285Sikob	fc->topology_map->node_count = 0;
944103285Sikob	fc->speed_map->generation ++;
945103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
946103285Sikob	self_id = &fc->topology_map->self_id[0];
947103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
948103285Sikob		if (sid[1] != ~sid[0]) {
949103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
950103285Sikob			sid += 2;
951103285Sikob			continue;
952103285Sikob		}
953103285Sikob		*self_id = *((union fw_self_id *)sid);
954103285Sikob		fc->topology_map->crc_len++;
955103285Sikob		if(self_id->p0.sequel == 0){
956103285Sikob			fc->topology_map->node_count ++;
957103285Sikob			c_port = 0;
958103285Sikob#if 0
959103285Sikob			fw_print_sid(sid[0]);
960103285Sikob#endif
961103285Sikob			node = self_id->p0.phy_id;
962103285Sikob			if(fc->max_node < node){
963103285Sikob				fc->max_node = self_id->p0.phy_id;
964103285Sikob			}
965103285Sikob			/* XXX I'm not sure this is the right speed_map */
966103285Sikob			fc->speed_map->speed[node][node]
967103285Sikob					= self_id->p0.phy_speed;
968103285Sikob			for (j = 0; j < node; j ++) {
969103285Sikob				fc->speed_map->speed[j][node]
970103285Sikob					= fc->speed_map->speed[node][j]
971103285Sikob					= min(fc->speed_map->speed[j][j],
972103285Sikob							self_id->p0.phy_speed);
973103285Sikob			}
974103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
975103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
976103285Sikob				fc->irm = self_id->p0.phy_id;
977103285Sikob			}
978103285Sikob			if(self_id->p0.port0 >= 0x2){
979103285Sikob				c_port++;
980103285Sikob			}
981103285Sikob			if(self_id->p0.port1 >= 0x2){
982103285Sikob				c_port++;
983103285Sikob			}
984103285Sikob			if(self_id->p0.port2 >= 0x2){
985103285Sikob				c_port++;
986103285Sikob			}
987103285Sikob		}
988103285Sikob		if(c_port > 2){
989103285Sikob			i_branch += (c_port - 2);
990103285Sikob		}
991103285Sikob		sid += 2;
992103285Sikob		self_id++;
993103285Sikob		fc->topology_map->self_id_count ++;
994103285Sikob	}
995108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
996103285Sikob	/* CRC */
997103285Sikob	fc->topology_map->crc = fw_crc16(
998103285Sikob			(u_int32_t *)&fc->topology_map->generation,
999103285Sikob			fc->topology_map->crc_len * 4);
1000103285Sikob	fc->speed_map->crc = fw_crc16(
1001103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1002103285Sikob			fc->speed_map->crc_len * 4);
1003103285Sikob	/* byteswap and copy to CSR */
1004103285Sikob	p = (u_int32_t *)fc->topology_map;
1005103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1006103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1007103285Sikob	p = (u_int32_t *)fc->speed_map;
1008103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1009103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1010103285Sikob	/* don't byte-swap u_int8_t array */
1011103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1012103285Sikob
1013103285Sikob	fc->max_hop = fc->max_node - i_branch;
1014103285Sikob#if 1
1015103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1016103285Sikob#endif
1017103285Sikob
1018103285Sikob	if(fc->irm == -1 ){
1019103285Sikob		printf(", Not found IRM capable node");
1020103285Sikob	}else{
1021103285Sikob		printf(", cable IRM = %d", fc->irm);
1022103285Sikob		if (fc->irm == fc->nodeid)
1023110016Ssimokawa			printf(" (me)");
1024103285Sikob	}
1025110016Ssimokawa	printf("\n");
1026103285Sikob
1027109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1028109736Ssimokawa		if (fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)) {
1029103285Sikob			fc->status = FWBUSMGRDONE;
1030103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1031109736Ssimokawa		} else {
1032103285Sikob			fc->status = FWBUSMGRELECT;
1033109736Ssimokawa			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,
1034109736Ssimokawa							(void *)fc, hz / 8);
1035103285Sikob		}
1036109736Ssimokawa	} else {
1037103285Sikob		fc->status = FWBUSMGRDONE;
1038109736Ssimokawa#if 0
1039108655Ssimokawa		device_printf(fc->bdev, "BMR = %x\n",
1040108655Ssimokawa				CSRARC(fc, BUS_MGR_ID));
1041109736Ssimokawa#endif
1042103285Sikob	}
1043103285Sikob	free(buf, M_DEVBUF);
1044109736Ssimokawa	/* Optimize gap_count, if I am BMGR */
1045103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1046103285Sikob		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
1047103285Sikob	}
1048108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1049108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1050103285Sikob}
1051106790Ssimokawa
1052103285Sikob/*
1053103285Sikob * To probe devices on the IEEE1394 bus.
1054103285Sikob */
1055106790Ssimokawastatic void
1056106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1057103285Sikob{
1058103285Sikob	int s;
1059103285Sikob	struct fw_device *fwdev, *next;
1060103285Sikob
1061103285Sikob	s = splfw();
1062103285Sikob	fc->status = FWBUSEXPLORE;
1063103285Sikob	fc->retry_count = 0;
1064103285Sikob
1065103285Sikob/*
1066103285Sikob * Invalidate all devices, just after bus reset. Devices
1067103285Sikob * to be removed has not been seen longer time.
1068103285Sikob */
1069103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1070103285Sikob		next = TAILQ_NEXT(fwdev, link);
1071103285Sikob		if(fwdev->status != FWDEVINVAL){
1072103285Sikob			fwdev->status = FWDEVINVAL;
1073103285Sikob			fwdev->rcnt = 0;
1074103285Sikob		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
1075103285Sikob			fwdev->rcnt ++;
1076103285Sikob		}else{
1077103285Sikob			TAILQ_REMOVE(&fc->devices, fwdev, link);
1078103285Sikob			free(fwdev, M_DEVBUF);
1079103285Sikob		}
1080103285Sikob	}
1081103285Sikob	fc->ongonode = 0;
1082103285Sikob	fc->ongoaddr = CSRROMOFF;
1083103285Sikob	fc->ongodev = NULL;
1084103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1085103285Sikob	fw_bus_explore(fc);
1086103285Sikob	splx(s);
1087103285Sikob}
1088106790Ssimokawa
1089103285Sikob/*
1090103285Sikob * To collect device informations on the IEEE1394 bus.
1091103285Sikob */
1092106790Ssimokawastatic void
1093106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1094103285Sikob{
1095103285Sikob	int err = 0;
1096103285Sikob	struct fw_device *fwdev, *tfwdev;
1097103285Sikob	u_int32_t addr;
1098103285Sikob	struct fw_xfer *xfer;
1099103285Sikob	struct fw_pkt *fp;
1100103285Sikob
1101103285Sikob	if(fc->status != FWBUSEXPLORE)
1102103285Sikob		return;
1103103285Sikob
1104103285Sikobloop:
1105103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1106103285Sikob
1107103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1108103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1109103285Sikob
1110103285Sikob	/* check link */
1111103285Sikob	/* XXX we need to check phy_id first */
1112103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1113103285Sikob		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
1114103285Sikob		fc->ongonode++;
1115103285Sikob		goto loop;
1116103285Sikob	}
1117103285Sikob
1118103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1119103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1120103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1121103285Sikob		fc->ongoaddr = CSRROMOFF;
1122103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1123103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1124103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1125103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1126103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1127103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1128103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1129103285Sikob	}else if(fc->ongodev == NULL){
1130103285Sikob		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1131103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1132103285Sikob			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
1133103285Sikob				break;
1134103285Sikob			}
1135103285Sikob		}
1136103285Sikob		if(fwdev != NULL){
1137103285Sikob			fwdev->dst = fc->ongonode;
1138103285Sikob			fwdev->status = FWDEVATTACHED;
1139103285Sikob			fc->ongonode++;
1140103285Sikob			fc->ongoaddr = CSRROMOFF;
1141103285Sikob			fc->ongodev = NULL;
1142103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1143103285Sikob			goto loop;
1144103285Sikob		}
1145109424Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_NOWAIT);
1146103285Sikob		if(fwdev == NULL)
1147103285Sikob			return;
1148106810Ssimokawa		fwdev->fc = fc;
1149103285Sikob		fwdev->rommax = 0;
1150103285Sikob		fwdev->dst = fc->ongonode;
1151103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1152103285Sikob		fwdev->status = FWDEVINIT;
1153103285Sikob#if 0
1154103285Sikob		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1155103285Sikob			>> ((3 - (fc->ongonode % 4)) * 8);
1156103285Sikob#else
1157103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1158103285Sikob#endif
1159103285Sikob
1160103285Sikob		tfwdev = TAILQ_FIRST(&fc->devices);
1161103285Sikob		while( tfwdev != NULL &&
1162103285Sikob			(tfwdev->eui.hi > fwdev->eui.hi) &&
1163103285Sikob			((tfwdev->eui.hi == fwdev->eui.hi) &&
1164103285Sikob				tfwdev->eui.lo > fwdev->eui.lo)){
1165103285Sikob			tfwdev = TAILQ_NEXT( tfwdev, link);
1166103285Sikob		}
1167103285Sikob		if(tfwdev == NULL){
1168103285Sikob			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
1169103285Sikob		}else{
1170103285Sikob			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
1171103285Sikob		}
1172103285Sikob
1173108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1174107653Ssimokawa			linkspeed[fwdev->speed],
1175107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1176103285Sikob
1177103285Sikob		fc->ongodev = fwdev;
1178103285Sikob		fc->ongoaddr = CSRROMOFF;
1179103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1180103285Sikob	}else{
1181103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1182103285Sikob	}
1183103285Sikob#if 0
1184103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1185103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1186103285Sikob		fw_bus_explore_callback);
1187103285Sikob	if(xfer == NULL) goto done;
1188103285Sikob#else
1189103285Sikob	xfer = fw_xfer_alloc();
1190103285Sikob	if(xfer == NULL){
1191103285Sikob		goto done;
1192103285Sikob	}
1193103285Sikob	xfer->send.len = 16;
1194103285Sikob	xfer->spd = 0;
1195109424Ssimokawa	xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
1196103285Sikob	if(xfer->send.buf == NULL){
1197103285Sikob		fw_xfer_free( xfer);
1198103285Sikob		return;
1199103285Sikob	}
1200103285Sikob
1201103285Sikob	xfer->send.off = 0;
1202103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1203103285Sikob	fp->mode.rreqq.dest_hi = htons(0xffff);
1204103285Sikob	fp->mode.rreqq.tlrt = 0;
1205103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1206103285Sikob	fp->mode.rreqq.pri = 0;
1207103285Sikob	fp->mode.rreqq.src = 0;
1208103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1209103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1210103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr);
1211103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1212103285Sikob
1213103285Sikob	err = fw_asyreq(fc, -1, xfer);
1214103285Sikob	if(err){
1215103285Sikob		fw_xfer_free( xfer);
1216103285Sikob		return;
1217103285Sikob	}
1218103285Sikob#endif
1219103285Sikob	return;
1220103285Sikobdone:
1221103285Sikob	/* fw_attach_devs */
1222103285Sikob	fc->status = FWBUSEXPDONE;
1223107653Ssimokawa	if (firewire_debug)
1224107653Ssimokawa		printf("bus_explore done\n");
1225103285Sikob	fw_attach_dev(fc);
1226103285Sikob	return;
1227103285Sikob
1228103285Sikob}
1229106790Ssimokawa
1230103285Sikob/* Portable Async. request read quad */
1231106790Ssimokawastruct fw_xfer *
1232106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1233105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1234105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1235103285Sikob{
1236103285Sikob	struct fw_xfer *xfer;
1237103285Sikob	struct fw_pkt *fp;
1238103285Sikob	int err;
1239103285Sikob
1240103285Sikob	xfer = fw_xfer_alloc();
1241103285Sikob	if(xfer == NULL){
1242103285Sikob		return NULL;
1243103285Sikob	}
1244103285Sikob	xfer->send.len = 16;
1245103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1246109424Ssimokawa	xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
1247103285Sikob	if(xfer->send.buf == NULL){
1248103285Sikob		fw_xfer_free( xfer);
1249103285Sikob		return NULL;
1250103285Sikob	}
1251103285Sikob
1252103285Sikob	xfer->send.off = 0;
1253103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1254103285Sikob	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1255103285Sikob	if(tl & FWP_TL_VALID){
1256103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1257103285Sikob	}else{
1258103285Sikob		fp->mode.rreqq.tlrt = 0;
1259103285Sikob	}
1260103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1261103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1262103285Sikob	fp->mode.rreqq.pri = 0;
1263103285Sikob	fp->mode.rreqq.src = 0;
1264103285Sikob	xfer->dst = addr_hi >> 16;
1265103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1266103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1267103285Sikob	xfer->act.hand = hand;
1268103285Sikob
1269103285Sikob	err = fw_asyreq(fc, -1, xfer);
1270103285Sikob	if(err){
1271103285Sikob		fw_xfer_free( xfer);
1272103285Sikob		return NULL;
1273103285Sikob	}
1274103285Sikob	return xfer;
1275103285Sikob}
1276106790Ssimokawa
1277103285Sikob/*
1278103285Sikob * Callback for the IEEE1394 bus information collection.
1279103285Sikob */
1280106790Ssimokawastatic void
1281106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1282106790Ssimokawa{
1283103285Sikob	struct firewire_comm *fc;
1284103285Sikob	struct fw_pkt *sfp,*rfp;
1285103285Sikob	struct csrhdr *chdr;
1286103285Sikob	struct csrdir *csrd;
1287103285Sikob	struct csrreg *csrreg;
1288103285Sikob	u_int32_t offset;
1289103285Sikob
1290103285Sikob
1291103285Sikob	if(xfer == NULL) return;
1292103285Sikob	fc = xfer->fc;
1293103285Sikob	if(xfer->resp != 0){
1294103285Sikob		printf("resp != 0: node=%d addr=0x%x\n",
1295103285Sikob			fc->ongonode, fc->ongoaddr);
1296103285Sikob		fc->retry_count++;
1297103285Sikob		goto nextnode;
1298103285Sikob	}
1299103285Sikob
1300103285Sikob	if(xfer->send.buf == NULL){
1301103285Sikob		printf("send.buf == NULL: node=%d addr=0x%x\n",
1302103285Sikob			fc->ongonode, fc->ongoaddr);
1303103285Sikob		printf("send.buf == NULL\n");
1304103285Sikob		fc->retry_count++;
1305103285Sikob		goto nextnode;
1306103285Sikob	}
1307103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1308103285Sikob
1309103285Sikob	if(xfer->recv.buf == NULL){
1310103285Sikob		printf("recv.buf == NULL: node=%d addr=0x%x\n",
1311103285Sikob			fc->ongonode, fc->ongoaddr);
1312103285Sikob		fc->retry_count++;
1313103285Sikob		goto nextnode;
1314103285Sikob	}
1315103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1316103285Sikob#if 0
1317103285Sikob	{
1318103285Sikob		u_int32_t *qld;
1319103285Sikob		int i;
1320103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1321103285Sikob		printf("len:%d\n", xfer->recv.len);
1322103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1323103285Sikob			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1324103285Sikob			if((i % 16) == 15) printf("\n");
1325103285Sikob		}
1326103285Sikob		if((i % 16) != 15) printf("\n");
1327103285Sikob	}
1328103285Sikob#endif
1329103285Sikob	if(fc->ongodev == NULL){
1330103285Sikob		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1331103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1332103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1333103285Sikob/* If CSR is minimul confinguration, more investgation is not needed. */
1334103285Sikob			if(chdr->info_len == 1){
1335103285Sikob				goto nextnode;
1336103285Sikob			}else{
1337103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1338103285Sikob			}
1339103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1340103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1341103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1342103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1343103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1344103285Sikob			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
1345103285Sikob				goto nextnode;
1346103285Sikob			fc->ongoaddr = CSRROMOFF;
1347103285Sikob		}
1348103285Sikob	}else{
1349103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1350103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1351103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1352103285Sikob		}
1353103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1354103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1355103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1356103285Sikob			offset = CSRROMOFF;
1357103285Sikob		}else{
1358103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1359103285Sikob			offset = csrd->off;
1360103285Sikob		}
1361103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1362103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1363103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1364103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1365103285Sikob				if(csrd == NULL){
1366103285Sikob					goto nextnode;
1367103285Sikob				}else{
1368103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1369103285Sikob					fc->ongoaddr += csrreg->val * 4;
1370103285Sikob					csrd->off = fc->ongoaddr;
1371103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1372103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1373103285Sikob					goto nextaddr;
1374103285Sikob				}
1375103285Sikob			}
1376103285Sikob		}
1377103285Sikob		fc->ongoaddr += 4;
1378103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1379103285Sikob				(fc->ongodev->rommax < 0x414)){
1380103285Sikob			if(fc->ongodev->rommax <= 0x414){
1381103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1382103285Sikob				if(csrd == NULL) goto nextnode;
1383103285Sikob				csrd->off = fc->ongoaddr;
1384103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1385103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1386103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1387103285Sikob			}
1388103285Sikob			goto nextaddr;
1389103285Sikob		}
1390103285Sikob
1391103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1392103285Sikob			if(csrd == NULL){
1393103285Sikob				goto nextnode;
1394103285Sikob			};
1395103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1396103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1397103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1398103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1399103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1400103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1401103285Sikob				offset = CSRROMOFF;
1402103285Sikob			}else{
1403103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1404103285Sikob				offset = csrd->off;
1405103285Sikob			}
1406103285Sikob		}
1407103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1408103285Sikob			goto nextnode;
1409103285Sikob		}
1410103285Sikob	}
1411103285Sikobnextaddr:
1412103285Sikob	fw_xfer_free( xfer);
1413103285Sikob	fw_bus_explore(fc);
1414103285Sikob	return;
1415103285Sikobnextnode:
1416103285Sikob	fw_xfer_free( xfer);
1417103285Sikob	fc->ongonode++;
1418103285Sikob/* housekeeping work space */
1419103285Sikob	fc->ongoaddr = CSRROMOFF;
1420103285Sikob	fc->ongodev = NULL;
1421103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1422103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1423103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1424103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1425103285Sikob	}
1426103285Sikob	fw_bus_explore(fc);
1427103285Sikob	return;
1428103285Sikob}
1429103285Sikob
1430103285Sikob/*
1431103285Sikob * To obtain CSR register values.
1432103285Sikob */
1433106815Ssimokawau_int32_t
1434106815Ssimokawagetcsrdata(struct fw_device *fwdev, u_int8_t key)
1435103285Sikob{
1436103285Sikob	int i;
1437103285Sikob	struct csrhdr *chdr;
1438103285Sikob	struct csrreg *creg;
1439103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1440103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1441103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1442103285Sikob		if(creg->key == key){
1443103285Sikob			return (u_int32_t)creg->val;
1444103285Sikob		}
1445103285Sikob	}
1446103285Sikob	return 0;
1447103285Sikob}
1448106815Ssimokawa
1449103285Sikob/*
1450103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1451103285Sikob */
1452106815Ssimokawastatic void
1453106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1454103285Sikob{
1455103285Sikob	struct fw_device *fwdev;
1456103285Sikob	struct fw_xfer *xfer;
1457103285Sikob	int i, err;
1458103285Sikob	device_t *devlistp;
1459103285Sikob	int devcnt;
1460103285Sikob	struct firewire_dev_comm *fdc;
1461108701Ssimokawa	u_int32_t spec, ver;
1462103285Sikob
1463103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1464103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1465103285Sikob		if(fwdev->status == FWDEVINIT){
1466108701Ssimokawa			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1467108701Ssimokawa			if(spec == 0)
1468103285Sikob				continue;
1469108701Ssimokawa			ver = getcsrdata(fwdev, CSRKEY_VER);
1470108701Ssimokawa			if(ver == 0)
1471103285Sikob				continue;
1472103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1473103285Sikob
1474108655Ssimokawa			device_printf(fc->bdev, "Device ");
1475108701Ssimokawa			switch(spec){
1476103285Sikob			case CSRVAL_ANSIT10:
1477108701Ssimokawa				switch(ver){
1478103285Sikob				case CSRVAL_T10SBP2:
1479107653Ssimokawa					printf("SBP-II");
1480103285Sikob					break;
1481103285Sikob				default:
1482103285Sikob					break;
1483103285Sikob				}
1484103285Sikob				break;
1485103285Sikob			case CSRVAL_1394TA:
1486108701Ssimokawa				switch(ver){
1487103285Sikob				case CSR_PROTAVC:
1488107653Ssimokawa					printf("AV/C");
1489103285Sikob					break;
1490103285Sikob				case CSR_PROTCAL:
1491107653Ssimokawa					printf("CAL");
1492103285Sikob					break;
1493103285Sikob				case CSR_PROTEHS:
1494107653Ssimokawa					printf("EHS");
1495103285Sikob					break;
1496103285Sikob				case CSR_PROTHAVI:
1497107653Ssimokawa					printf("HAVi");
1498103285Sikob					break;
1499103285Sikob				case CSR_PROTCAM104:
1500107653Ssimokawa					printf("1394 Cam 1.04");
1501103285Sikob					break;
1502103285Sikob				case CSR_PROTCAM120:
1503107653Ssimokawa					printf("1394 Cam 1.20");
1504103285Sikob					break;
1505103285Sikob				case CSR_PROTCAM130:
1506107653Ssimokawa					printf("1394 Cam 1.30");
1507103285Sikob					break;
1508103285Sikob				case CSR_PROTDPP:
1509107653Ssimokawa					printf("1394 Direct print");
1510103285Sikob					break;
1511103285Sikob				case CSR_PROTIICP:
1512107653Ssimokawa					printf("Industrial & Instrument");
1513103285Sikob					break;
1514103285Sikob				default:
1515107653Ssimokawa					printf("unknown 1394TA");
1516103285Sikob					break;
1517103285Sikob				}
1518103285Sikob				break;
1519103285Sikob			default:
1520107653Ssimokawa				printf("unknown spec");
1521103285Sikob				break;
1522103285Sikob			}
1523103285Sikob			fwdev->status = FWDEVATTACHED;
1524103285Sikob			printf("\n");
1525103285Sikob		}
1526103285Sikob	}
1527108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1528103285Sikob	if( err != 0 )
1529103285Sikob		return;
1530103285Sikob	for( i = 0 ; i < devcnt ; i++){
1531103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1532103285Sikob			fdc = device_get_softc(devlistp[i]);
1533103285Sikob			if (fdc->post_explore != NULL)
1534103285Sikob				fdc->post_explore(fdc);
1535103285Sikob		}
1536103285Sikob	}
1537103285Sikob	free(devlistp, M_TEMP);
1538103285Sikob
1539103285Sikob	/* call pending handlers */
1540103285Sikob	i = 0;
1541103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1542103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1543103285Sikob		i++;
1544103285Sikob		if (xfer->act.hand)
1545103285Sikob			xfer->act.hand(xfer);
1546103285Sikob	}
1547103285Sikob	if (i > 0)
1548103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1549103285Sikob	if (fc->retry_count > 0) {
1550103285Sikob		printf("retry_count = %d\n", fc->retry_count);
1551103285Sikob		fc->retry_probe_handle = timeout((timeout_t *)fc->ibr,
1552103285Sikob							(void *)fc, hz*2);
1553103285Sikob	}
1554103285Sikob	return;
1555103285Sikob}
1556106815Ssimokawa
1557103285Sikob/*
1558103285Sikob * To allocate uniq transaction label.
1559103285Sikob */
1560106815Ssimokawastatic int
1561106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1562103285Sikob{
1563103285Sikob	u_int i;
1564103285Sikob	struct tlabel *tl, *tmptl;
1565103285Sikob	int s;
1566103285Sikob	static u_int32_t label = 0;
1567103285Sikob
1568103285Sikob	s = splfw();
1569103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1570103285Sikob		label = (label + 1) & 0x3f;
1571103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1572103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1573103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1574103285Sikob		}
1575103285Sikob		if(tmptl == NULL) {
1576109424Ssimokawa			tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_NOWAIT);
1577103285Sikob			if (tl == NULL) {
1578103285Sikob				splx(s);
1579103285Sikob				return (-1);
1580103285Sikob			}
1581103285Sikob			tl->xfer = xfer;
1582103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1583103285Sikob			splx(s);
1584103285Sikob			return(label);
1585103285Sikob		}
1586103285Sikob	}
1587103285Sikob	splx(s);
1588103285Sikob
1589103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1590103285Sikob	return(-1);
1591103285Sikob}
1592106815Ssimokawa
1593103285Sikob/*
1594103285Sikob * Generic packet receving process.
1595103285Sikob */
1596106815Ssimokawavoid
1597106815Ssimokawafw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1598103285Sikob{
1599103285Sikob	struct fw_pkt *fp, *resfp;
1600103285Sikob	struct fw_xfer *xfer;
1601103285Sikob	struct fw_bind *bind;
1602103285Sikob	struct firewire_softc *sc;
1603103285Sikob	int s;
1604103285Sikob#if 0
1605103285Sikob	{
1606103285Sikob		u_int32_t *qld;
1607103285Sikob		int i;
1608103285Sikob		qld = (u_int32_t *)buf;
1609103285Sikob		printf("spd %d len:%d\n", spd, len);
1610103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1611103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1612103285Sikob			if((i % 16) == 15) printf("\n");
1613103285Sikob		}
1614103285Sikob		if((i % 16) != 15) printf("\n");
1615103285Sikob	}
1616103285Sikob#endif
1617103285Sikob	fp = (struct fw_pkt *)(buf + off);
1618103285Sikob	switch(fp->mode.common.tcode){
1619103285Sikob	case FWTCODE_WRES:
1620103285Sikob	case FWTCODE_RRESQ:
1621103285Sikob	case FWTCODE_RRESB:
1622103285Sikob	case FWTCODE_LRES:
1623103285Sikob		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1624103285Sikob					fp->mode.hdr.tlrt >> 2);
1625103285Sikob		if(xfer == NULL) {
1626103285Sikob			printf("fw_rcv: unknown response "
1627103285Sikob					"tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n",
1628103285Sikob					fp->mode.common.tcode,
1629103285Sikob					ntohs(fp->mode.hdr.src),
1630103285Sikob					fp->mode.hdr.tlrt >> 2,
1631103285Sikob					fp->mode.hdr.tlrt & 3,
1632103285Sikob					fp->mode.rresq.data);
1633103285Sikob#if 1
1634103285Sikob			printf("try ad-hoc work around!!\n");
1635103285Sikob			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1636103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1637103285Sikob			if (xfer == NULL) {
1638103285Sikob				printf("no use...\n");
1639103285Sikob				goto err;
1640103285Sikob			}
1641103285Sikob#else
1642103285Sikob			goto err;
1643103285Sikob#endif
1644103285Sikob		}
1645103285Sikob		switch(xfer->act_type){
1646103285Sikob		case FWACT_XFER:
1647103285Sikob			if((xfer->sub >= 0) &&
1648103285Sikob				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1649103285Sikob				xfer->resp = EINVAL;
1650103285Sikob				fw_xfer_done(xfer);
1651103285Sikob				goto err;
1652103285Sikob			}
1653103285Sikob			xfer->recv.len = len;
1654103285Sikob			xfer->recv.off = off;
1655103285Sikob			xfer->recv.buf = buf;
1656103285Sikob			xfer->resp = 0;
1657103285Sikob			fw_xfer_done(xfer);
1658103285Sikob			return;
1659103285Sikob			break;
1660103285Sikob		case FWACT_CH:
1661103285Sikob		default:
1662103285Sikob			goto err;
1663103285Sikob			break;
1664103285Sikob		}
1665103285Sikob		break;
1666103285Sikob	case FWTCODE_WREQQ:
1667103285Sikob	case FWTCODE_WREQB:
1668103285Sikob	case FWTCODE_RREQQ:
1669103285Sikob	case FWTCODE_RREQB:
1670103285Sikob	case FWTCODE_LREQ:
1671103285Sikob		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1672103285Sikob			ntohl(fp->mode.rreqq.dest_lo));
1673103285Sikob		if(bind == NULL){
1674108712Ssimokawa#if __FreeBSD_version >= 500000
1675103285Sikob			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
1676108712Ssimokawa#else
1677108712Ssimokawa			printf("Unknown service addr 0x%08x:0x%08lx tcode=%x\n",
1678108712Ssimokawa#endif
1679103285Sikob				ntohs(fp->mode.rreqq.dest_hi),
1680103285Sikob				ntohl(fp->mode.rreqq.dest_lo),
1681103285Sikob				fp->mode.common.tcode);
1682103285Sikob			if (fc->status == FWBUSRESET) {
1683103285Sikob				printf("fw_rcv: cannot response(bus reset)!\n");
1684103285Sikob				goto err;
1685103285Sikob			}
1686103285Sikob			xfer = fw_xfer_alloc();
1687103285Sikob			if(xfer == NULL){
1688103285Sikob				return;
1689103285Sikob			}
1690103285Sikob			xfer->spd = spd;
1691109424Ssimokawa			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
1692103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1693103285Sikob			switch(fp->mode.common.tcode){
1694103285Sikob			case FWTCODE_WREQQ:
1695103285Sikob			case FWTCODE_WREQB:
1696103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1697103285Sikob				xfer->send.len = 12;
1698103285Sikob				break;
1699103285Sikob			case FWTCODE_RREQQ:
1700103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1701103285Sikob				xfer->send.len = 16;
1702103285Sikob				break;
1703103285Sikob			case FWTCODE_RREQB:
1704103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1705103285Sikob				xfer->send.len = 16;
1706103285Sikob				break;
1707103285Sikob			case FWTCODE_LREQ:
1708103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1709103285Sikob				xfer->send.len = 16;
1710103285Sikob				break;
1711103285Sikob			}
1712103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1713103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1714103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1715103285Sikob			resfp->mode.rresb.rtcode = 7;
1716103285Sikob			resfp->mode.rresb.extcode = 0;
1717103285Sikob			resfp->mode.rresb.len = 0;
1718103285Sikob/*
1719103285Sikob			xfer->act.hand = fw_asy_callback;
1720103285Sikob*/
1721103285Sikob			xfer->act.hand = fw_xfer_free;
1722103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1723103285Sikob				fw_xfer_free( xfer);
1724103285Sikob				return;
1725103285Sikob			}
1726103285Sikob			goto err;
1727103285Sikob		}
1728103285Sikob		switch(bind->xfer->act_type){
1729103285Sikob		case FWACT_XFER:
1730103285Sikob			xfer = fw_xfer_alloc();
1731103285Sikob			if(xfer == NULL) goto err;
1732103285Sikob			xfer->fc = bind->xfer->fc;
1733103285Sikob			xfer->sc = bind->xfer->sc;
1734103285Sikob			xfer->recv.buf = buf;
1735103285Sikob			xfer->recv.len = len;
1736103285Sikob			xfer->recv.off = off;
1737103285Sikob			xfer->spd = spd;
1738103285Sikob			xfer->act.hand = bind->xfer->act.hand;
1739103285Sikob			if (fc->status != FWBUSRESET)
1740103285Sikob				xfer->act.hand(xfer);
1741103285Sikob			else
1742103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1743103285Sikob			return;
1744103285Sikob			break;
1745103285Sikob		case FWACT_CH:
1746103285Sikob			if(fc->ir[bind->xfer->sub]->queued >=
1747103285Sikob				fc->ir[bind->xfer->sub]->maxq){
1748108655Ssimokawa				device_printf(fc->bdev,
1749108655Ssimokawa					"Discard a packet %x %d\n",
1750103285Sikob					bind->xfer->sub,
1751103285Sikob					fc->ir[bind->xfer->sub]->queued);
1752103285Sikob				goto err;
1753103285Sikob			}
1754103285Sikob			xfer = fw_xfer_alloc();
1755103285Sikob			if(xfer == NULL) goto err;
1756103285Sikob			xfer->recv.buf = buf;
1757103285Sikob			xfer->recv.len = len;
1758103285Sikob			xfer->recv.off = off;
1759103285Sikob			xfer->spd = spd;
1760103285Sikob			s = splfw();
1761103285Sikob			fc->ir[bind->xfer->sub]->queued++;
1762103285Sikob			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1763103285Sikob			splx(s);
1764103285Sikob
1765103285Sikob			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1766103285Sikob
1767103285Sikob			return;
1768103285Sikob			break;
1769103285Sikob		default:
1770103285Sikob			goto err;
1771103285Sikob			break;
1772103285Sikob		}
1773103285Sikob		break;
1774103285Sikob	case FWTCODE_STREAM:
1775103285Sikob	{
1776103285Sikob		struct fw_xferq *xferq;
1777103285Sikob
1778103285Sikob		xferq = fc->ir[sub];
1779103285Sikob#if 0
1780103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1781103285Sikob			sub, len, off, spd);
1782103285Sikob#endif
1783103285Sikob		if(xferq->queued >= xferq->maxq) {
1784103285Sikob			printf("receive queue is full\n");
1785103285Sikob			goto err;
1786103285Sikob		}
1787103285Sikob		xfer = fw_xfer_alloc();
1788103285Sikob		if(xfer == NULL) goto err;
1789103285Sikob		xfer->recv.buf = buf;
1790103285Sikob		xfer->recv.len = len;
1791103285Sikob		xfer->recv.off = off;
1792103285Sikob		xfer->spd = spd;
1793103285Sikob		s = splfw();
1794103285Sikob		xferq->queued++;
1795103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1796103285Sikob		splx(s);
1797103285Sikob		sc = device_get_softc(fc->bdev);
1798103285Sikob#if __FreeBSD_version >= 500000
1799103285Sikob		if (SEL_WAITING(&xferq->rsel))
1800103285Sikob#else
1801103285Sikob		if (&xferq->rsel.si_pid != 0)
1802103285Sikob#endif
1803103285Sikob			selwakeup(&xferq->rsel);
1804103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1805103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1806103285Sikob			wakeup((caddr_t)xferq);
1807103285Sikob		}
1808103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1809103285Sikob			xferq->hand(xferq);
1810103285Sikob		}
1811103285Sikob		return;
1812103285Sikob		break;
1813103285Sikob	}
1814103285Sikob	default:
1815103285Sikob		printf("fw_rcv: unknow tcode\n");
1816103285Sikob		break;
1817103285Sikob	}
1818103285Sikoberr:
1819103285Sikob	free(buf, M_DEVBUF);
1820103285Sikob}
1821106815Ssimokawa
1822103285Sikob/*
1823103285Sikob * Post process for Bus Manager election process.
1824103285Sikob */
1825103285Sikobstatic void
1826103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1827103285Sikob{
1828109422Ssimokawa	struct fw_pkt *rfp;
1829103285Sikob	struct firewire_comm *fc;
1830109422Ssimokawa	int bmr;
1831103285Sikob
1832109422Ssimokawa	if (xfer == NULL)
1833109422Ssimokawa		return;
1834103285Sikob	fc = xfer->fc;
1835109422Ssimokawa	if (xfer->resp != 0)
1836103285Sikob		goto error;
1837109422Ssimokawa	if (xfer->send.buf == NULL)
1838103285Sikob		goto error;
1839109422Ssimokawa	if (xfer->recv.buf == NULL)
1840103285Sikob		goto error;
1841103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1842109422Ssimokawa	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1843109422Ssimokawa		goto error;
1844109422Ssimokawa
1845109422Ssimokawa	bmr = ntohl(rfp->mode.lres.payload[0]);
1846109422Ssimokawa	if (bmr == 0x3f)
1847109422Ssimokawa		bmr = fc->nodeid;
1848109422Ssimokawa
1849109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1850108655Ssimokawa	device_printf(fc->bdev, "new bus manager %d ",
1851108655Ssimokawa		CSRARC(fc, BUS_MGR_ID));
1852109422Ssimokawa	if(bmr == fc->nodeid){
1853103285Sikob		printf("(me)\n");
1854103285Sikob/* If I am bus manager, optimize gapcount */
1855103285Sikob		if(fc->max_hop <= MAX_GAPHOP ){
1856103285Sikob			fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
1857103285Sikob		}
1858103285Sikob	}else{
1859103285Sikob		printf("\n");
1860103285Sikob	}
1861103285Sikoberror:
1862103285Sikob	fw_xfer_free(xfer);
1863103285Sikob}
1864106815Ssimokawa
1865103285Sikob/*
1866103285Sikob * To candidate Bus Manager election process.
1867103285Sikob */
1868106815Ssimokawavoid
1869106815Ssimokawafw_try_bmr(void *arg)
1870103285Sikob{
1871103285Sikob	struct fw_xfer *xfer;
1872103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
1873103285Sikob	struct fw_pkt *fp;
1874103285Sikob	int err = 0;
1875103285Sikob
1876103285Sikob	xfer = fw_xfer_alloc();
1877103285Sikob	if(xfer == NULL){
1878103285Sikob		return;
1879103285Sikob	}
1880103285Sikob	xfer->send.len = 24;
1881103285Sikob	xfer->spd = 0;
1882109424Ssimokawa	xfer->send.buf = malloc(24, M_DEVBUF, M_NOWAIT);
1883103285Sikob	if(xfer->send.buf == NULL){
1884103285Sikob		fw_xfer_free( xfer);
1885103285Sikob		return;
1886103285Sikob	}
1887103285Sikob
1888103285Sikob	fc->status = FWBUSMGRELECT;
1889103285Sikob
1890103285Sikob	xfer->send.off = 0;
1891103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1892103285Sikob	fp->mode.lreq.dest_hi = htons(0xffff);
1893103285Sikob	fp->mode.lreq.tlrt = 0;
1894103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
1895103285Sikob	fp->mode.lreq.pri = 0;
1896103285Sikob	fp->mode.lreq.src = 0;
1897103285Sikob	fp->mode.lreq.len = htons(8);
1898103285Sikob	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
1899103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
1900103285Sikob	fp->mode.lreq.dst = htons(xfer->dst);
1901103285Sikob	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
1902109422Ssimokawa	fp->mode.lreq.payload[0] = htonl(0x3f);
1903109422Ssimokawa	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
1904103285Sikob	xfer->act_type = FWACT_XFER;
1905103285Sikob	xfer->act.hand = fw_try_bmr_callback;
1906103285Sikob
1907103285Sikob	err = fw_asyreq(fc, -1, xfer);
1908103285Sikob	if(err){
1909103285Sikob		fw_xfer_free( xfer);
1910103285Sikob		return;
1911103285Sikob	}
1912103285Sikob	return;
1913103285Sikob}
1914106543Ssimokawa
1915106543Ssimokawa#ifdef FW_VMACCESS
1916103285Sikob/*
1917103285Sikob * Software implementation for physical memory block access.
1918103285Sikob * XXX:Too slow, usef for debug purpose only.
1919103285Sikob */
1920106815Ssimokawastatic void
1921106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
1922103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
1923103285Sikob	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
1924103285Sikob
1925103285Sikob	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
1926103285Sikob			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1927103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1928103285Sikob	if(xfer->resp != 0){
1929103285Sikob		fw_xfer_free( xfer);
1930103285Sikob		return;
1931103285Sikob	}
1932103285Sikob	if(xfer->recv.buf == NULL){
1933103285Sikob		fw_xfer_free( xfer);
1934103285Sikob		return;
1935103285Sikob	}
1936103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1937103285Sikob	switch(rfp->mode.hdr.tcode){
1938103285Sikob		/* XXX need fix for 64bit arch */
1939103285Sikob		case FWTCODE_WREQB:
1940103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
1941103285Sikob			xfer->send.len = 12;
1942103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
1943103285Sikob			bcopy(rfp->mode.wreqb.payload,
1944103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
1945103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
1946103285Sikob			sfp->mode.wres.rtcode = 0;
1947103285Sikob			break;
1948103285Sikob		case FWTCODE_WREQQ:
1949103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
1950103285Sikob			xfer->send.len = 12;
1951103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
1952103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
1953103285Sikob			sfp->mode.wres.rtcode = 0;
1954103285Sikob			break;
1955103285Sikob		case FWTCODE_RREQB:
1956103285Sikob			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT);
1957103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
1958103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
1959103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
1960103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
1961103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
1962103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
1963103285Sikob			sfp->mode.rresb.rtcode = 0;
1964103285Sikob			sfp->mode.rresb.extcode = 0;
1965103285Sikob			break;
1966103285Sikob		case FWTCODE_RREQQ:
1967103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
1968103285Sikob			xfer->send.len = 16;
1969103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
1970103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
1971103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
1972103285Sikob			sfp->mode.rresb.rtcode = 0;
1973103285Sikob			break;
1974103285Sikob		default:
1975103285Sikob			fw_xfer_free( xfer);
1976103285Sikob			return;
1977103285Sikob	}
1978103285Sikob	xfer->send.off = 0;
1979103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
1980103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
1981103285Sikob	xfer->act.hand = fw_xfer_free;
1982103285Sikob	xfer->retry_req = fw_asybusy;
1983103285Sikob
1984103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
1985103285Sikob	sfp->mode.hdr.pri = 0;
1986103285Sikob
1987103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1988103285Sikob/**/
1989103285Sikob	return;
1990103285Sikob}
1991106543Ssimokawa#endif
1992106543Ssimokawa
1993103285Sikob/*
1994103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
1995103285Sikob */
1996106815Ssimokawau_int16_t
1997106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
1998103285Sikob	u_int32_t i, sum, crc = 0;
1999103285Sikob	int shift;
2000103285Sikob	len = (len + 3) & ~3;
2001103285Sikob	for(i = 0 ; i < len ; i+= 4){
2002103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2003103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2004103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2005103285Sikob		}
2006103285Sikob		crc &= 0xffff;
2007103285Sikob	}
2008103285Sikob	return((u_int16_t) crc);
2009103285Sikob}
2010106815Ssimokawa
2011103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2012103285SikobMODULE_VERSION(firewire, 1);
2013