firewire.c revision 111078
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 111078 2003-02-18 10:56:00Z 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>
57110072Ssimokawa#include <dev/firewire/fwmem.h>
58103285Sikob#include <dev/firewire/iec13213.h>
59103285Sikob#include <dev/firewire/iec68113.h>
60103285Sikob
61109736Ssimokawaint firewire_debug=0, try_bmr=1;
62103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
63108281Ssimokawa	"FireWire driver debug flag");
64109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
65109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
66109736Ssimokawa	"Try to be a bus manager");
67103285Sikob
68110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
69110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
70110195Ssimokawa
71103285Sikob#define FW_MAXASYRTY 4
72103285Sikob#define FW_MAXDEVRCNT 4
73103285Sikob
74103285Sikob#define XFER_TIMEOUT 0
75103285Sikob
76103285Sikobdevclass_t firewire_devclass;
77103285Sikob
78103285Sikobstatic int firewire_match      __P((device_t));
79103285Sikobstatic int firewire_attach      __P((device_t));
80103285Sikobstatic int firewire_detach      __P((device_t));
81103285Sikob#if 0
82103285Sikobstatic int firewire_shutdown    __P((device_t));
83103285Sikob#endif
84103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
85103285Sikobstatic void fw_try_bmr __P((void *));
86103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
87103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
88103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
89103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
90103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
91103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
92103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
93106543Ssimokawa#ifdef FW_VMACCESS
94103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
95106543Ssimokawa#endif
96103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
97105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
98110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *));
99103285Sikob
100103285Sikobstatic device_method_t firewire_methods[] = {
101103285Sikob	/* Device interface */
102103285Sikob	DEVMETHOD(device_probe,		firewire_match),
103103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
104103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
105108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
106108642Ssimokawa	DEVMETHOD(device_resume,	bus_generic_resume),
107103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
108103285Sikob
109103285Sikob	/* Bus interface */
110103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
111103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
112103285Sikob
113103285Sikob	{ 0, 0 }
114103285Sikob};
115103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
116103285Sikob
117103285Sikob#define MAX_GAPHOP  16
118103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
119103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
120106813Ssimokawa
121106813Ssimokawaextern struct cdevsw firewire_cdevsw;
122106813Ssimokawa
123103285Sikobstatic driver_t firewire_driver = {
124103285Sikob	"firewire",
125103285Sikob	firewire_methods,
126103285Sikob	sizeof(struct firewire_softc),
127103285Sikob};
128103285Sikob
129103285Sikob/*
130110072Ssimokawa * Lookup fwdev by node id.
131103285Sikob */
132106810Ssimokawastruct fw_device *
133110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
134103285Sikob{
135103285Sikob	struct fw_device *fwdev;
136110072Ssimokawa	int s;
137110072Ssimokawa
138110072Ssimokawa	s = splfw();
139110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
140110072Ssimokawa		if (fwdev->dst == dst)
141103285Sikob			break;
142110072Ssimokawa	splx(s);
143110072Ssimokawa
144106810Ssimokawa	if(fwdev == NULL) return NULL;
145106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
146106810Ssimokawa	return fwdev;
147103285Sikob}
148106813Ssimokawa
149103285Sikob/*
150110072Ssimokawa * Lookup fwdev by EUI64.
151110072Ssimokawa */
152110072Ssimokawastruct fw_device *
153110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
154110072Ssimokawa{
155110072Ssimokawa	struct fw_device *fwdev;
156110072Ssimokawa	int s;
157110072Ssimokawa
158110072Ssimokawa	s = splfw();
159110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
160110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
161110072Ssimokawa			break;
162110072Ssimokawa	splx(s);
163110072Ssimokawa
164110072Ssimokawa	if(fwdev == NULL) return NULL;
165110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
166110072Ssimokawa	return fwdev;
167110072Ssimokawa}
168110072Ssimokawa
169110072Ssimokawa/*
170103285Sikob * Async. request procedure for userland application.
171103285Sikob */
172103285Sikobint
173103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
174103285Sikob{
175103285Sikob	int err = 0;
176103285Sikob	struct fw_xferq *xferq;
177103285Sikob	int tl = 0, len;
178103285Sikob	struct fw_pkt *fp;
179103285Sikob	int tcode;
180103285Sikob	struct tcode_info *info;
181103285Sikob
182103285Sikob	if(xfer == NULL) return EINVAL;
183108701Ssimokawa	if(xfer->send.len > MAXREC(fc->maxrec)){
184103285Sikob		printf("send.len > maxrec\n");
185103285Sikob		return EINVAL;
186103285Sikob	}
187103285Sikob	if(xfer->act.hand == NULL){
188103285Sikob		printf("act.hand == NULL\n");
189103285Sikob		return EINVAL;
190103285Sikob	}
191103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
192103285Sikob
193103285Sikob	tcode = fp->mode.common.tcode & 0xf;
194103285Sikob	info = &fc->tcode[tcode];
195103285Sikob	if (info->flag == 0) {
196103285Sikob		printf("invalid tcode=%d\n", tcode);
197103285Sikob		return EINVAL;
198103285Sikob	}
199103285Sikob	if (info->flag & FWTI_REQ)
200103285Sikob		xferq = fc->atq;
201103285Sikob	else
202103285Sikob		xferq = fc->ats;
203103285Sikob	len = info->hdr_len;
204103285Sikob	if (info->flag & FWTI_BLOCK_STR)
205103285Sikob		len += ntohs(fp->mode.stream.len);
206103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
207103285Sikob		len += ntohs(fp->mode.rresb.len);
208103285Sikob	if( len >  xfer->send.len ){
209103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
210103285Sikob				len, xfer->send.len, tcode);
211103285Sikob		return EINVAL;
212103285Sikob	}
213103285Sikob	xfer->send.len = len;
214106790Ssimokawa
215103285Sikob	if(xferq->start == NULL){
216103285Sikob		printf("xferq->start == NULL\n");
217103285Sikob		return EINVAL;
218103285Sikob	}
219103285Sikob	if(!(xferq->queued < xferq->maxq)){
220108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
221108655Ssimokawa			xferq->queued);
222103285Sikob		return EINVAL;
223103285Sikob	}
224103285Sikob
225103285Sikob
226103285Sikob	if (info->flag & FWTI_TLABEL) {
227103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
228103285Sikob			return EIO;
229103285Sikob		fp->mode.hdr.tlrt = tl << 2;
230103285Sikob	}
231103285Sikob
232103285Sikob	xfer->tl = tl;
233103285Sikob	xfer->tcode = tcode;
234103285Sikob	xfer->resp = 0;
235103285Sikob	xfer->fc = fc;
236103285Sikob	xfer->q = xferq;
237103285Sikob	xfer->act_type = FWACT_XFER;
238103285Sikob	xfer->retry_req = fw_asybusy;
239103285Sikob
240103285Sikob	fw_asystart(xfer);
241103285Sikob	return err;
242103285Sikob}
243103285Sikob/*
244103285Sikob * Wakeup blocked process.
245103285Sikob */
246103285Sikobvoid
247103285Sikobfw_asy_callback(struct fw_xfer *xfer){
248103285Sikob	wakeup(xfer);
249103285Sikob	return;
250103285Sikob}
251103285Sikob/*
252103285Sikob * Postpone to later retry.
253103285Sikob */
254103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
255106815Ssimokawa#if 1
256103285Sikob	printf("fw_asybusy\n");
257103285Sikob#endif
258103285Sikob#if XFER_TIMEOUT
259103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
260103285Sikob#endif
261103285Sikob/*
262103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
263103285Sikob*/
264103285Sikob	DELAY(20000);
265103285Sikob	fw_asystart(xfer);
266103285Sikob	return;
267103285Sikob}
268103285Sikob#if XFER_TIMEOUT
269103285Sikob/*
270103285Sikob * Post timeout for async. request.
271103285Sikob */
272103285Sikobvoid
273103285Sikobfw_xfer_timeout(void *arg)
274103285Sikob{
275103285Sikob	int s;
276103285Sikob	struct fw_xfer *xfer;
277103285Sikob
278103285Sikob	xfer = (struct fw_xfer *)arg;
279103285Sikob	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
280103285Sikob	/* XXX set error code */
281103285Sikob	s = splfw();
282103285Sikob	xfer->act.hand(xfer);
283103285Sikob	splx(s);
284103285Sikob}
285103285Sikob#endif
286103285Sikob/*
287103285Sikob * Async. request with given xfer structure.
288103285Sikob */
289106790Ssimokawastatic void
290106790Ssimokawafw_asystart(struct fw_xfer *xfer)
291106790Ssimokawa{
292103285Sikob	struct firewire_comm *fc = xfer->fc;
293103285Sikob	int s;
294103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
295103285Sikob		xfer->resp = EBUSY;
296103285Sikob		xfer->state = FWXF_BUSY;
297103285Sikob		xfer->act.hand(xfer);
298103285Sikob		return;
299103285Sikob	}
300103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
301103285Sikob	if (fc->status < FWBUSEXPLORE) {
302103285Sikob		xfer->resp = EAGAIN;
303103285Sikob		xfer->state = FWXF_BUSY;
304103285Sikob		if (xfer->act.hand != NULL)
305103285Sikob			xfer->act.hand(xfer);
306103285Sikob		return;
307103285Sikob	}
308103285Sikob#endif
309103285Sikob	s = splfw();
310103285Sikob	xfer->state = FWXF_INQ;
311103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
312103285Sikob	xfer->q->queued ++;
313103285Sikob	splx(s);
314103285Sikob	/* XXX just queue for mbuf */
315103285Sikob	if (xfer->mbuf == NULL)
316103285Sikob		xfer->q->start(fc);
317103285Sikob#if XFER_TIMEOUT
318103285Sikob	if (xfer->act.hand != NULL)
319103285Sikob		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
320103285Sikob#endif
321103285Sikob	return;
322103285Sikob}
323106790Ssimokawa
324103285Sikobstatic int
325103285Sikobfirewire_match( device_t dev )
326103285Sikob{
327108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
328103285Sikob	return -140;
329103285Sikob}
330106790Ssimokawa
331110577Ssimokawastatic void
332110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
333110577Ssimokawa{
334110577Ssimokawa	struct fw_xfer *xfer;
335110577Ssimokawa	struct tlabel *tl;
336110577Ssimokawa	struct timeval tv;
337110577Ssimokawa	struct timeval split_timeout;
338111040Ssimokawa	int i, s;
339110577Ssimokawa
340110891Ssimokawa	split_timeout.tv_sec = 6;
341110577Ssimokawa	split_timeout.tv_usec = 0;
342110577Ssimokawa
343110577Ssimokawa	microtime(&tv);
344110577Ssimokawa	timevalsub(&tv, &split_timeout);
345110577Ssimokawa
346111040Ssimokawa	s = splfw();
347110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
348110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
349110577Ssimokawa			xfer = tl->xfer;
350110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
351110577Ssimokawa				/* the rests are newer than this */
352110577Ssimokawa				break;
353110577Ssimokawa			device_printf(fc->bdev,
354110891Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x\n",
355110577Ssimokawa				xfer->dst, i);
356110577Ssimokawa			xfer->resp = ETIMEDOUT;
357110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
358110577Ssimokawa			switch (xfer->act_type) {
359110577Ssimokawa			case FWACT_XFER:
360110577Ssimokawa				fw_xfer_done(xfer);
361110577Ssimokawa				break;
362110577Ssimokawa			default:
363110577Ssimokawa				/* ??? */
364110577Ssimokawa				fw_xfer_free(xfer);
365110577Ssimokawa				break;
366110577Ssimokawa			}
367110577Ssimokawa		}
368110577Ssimokawa	}
369111040Ssimokawa	splx(s);
370110577Ssimokawa}
371110577Ssimokawa
372110577Ssimokawastatic void
373110577Ssimokawafirewire_watchdog(void *arg)
374110577Ssimokawa{
375110577Ssimokawa	struct firewire_comm *fc;
376110577Ssimokawa
377110577Ssimokawa	fc = (struct firewire_comm *)arg;
378110577Ssimokawa	firewire_xfer_timeout(fc);
379110577Ssimokawa	fc->timeout(fc);
380110577Ssimokawa	callout_reset(&fc->timeout_callout, hz,
381110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
382110577Ssimokawa}
383110577Ssimokawa
384103285Sikob/*
385103285Sikob * The attach routine.
386103285Sikob */
387103285Sikobstatic int
388103285Sikobfirewire_attach( device_t dev )
389103285Sikob{
390103285Sikob	int i, unitmask, mn;
391103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
392103285Sikob	device_t pa = device_get_parent(dev);
393103285Sikob	struct firewire_comm *fc;
394103285Sikob	dev_t d;
395103285Sikob
396103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
397103285Sikob	sc->fc = fc;
398111074Ssimokawa	fc->status = -1;
399103285Sikob
400103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
401103285Sikob
402103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
403103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
404103285Sikob		mn = unitmask | i;
405103285Sikob		/* XXX device name should be improved */
406103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
407108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
408103285Sikob			"fw%x", mn);
409103285Sikob#if __FreeBSD_version >= 500000
410103285Sikob		if (i == 0)
411103285Sikob			sc->dev = d;
412103285Sikob		else
413103285Sikob			dev_depends(sc->dev, d);
414103285Sikob#else
415103285Sikob		sc->dev[i] = d;
416103285Sikob#endif
417103285Sikob	}
418103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
419108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
420103285Sikob			"fwmem%d", device_get_unit(dev));
421103285Sikob#if __FreeBSD_version >= 500000
422103285Sikob	dev_depends(sc->dev, d);
423103285Sikob#else
424103285Sikob	sc->dev[i] = d;
425103285Sikob#endif
426108853Ssimokawa#if __FreeBSD_version >= 500000
427110193Ssimokawa#define CALLOUT_INIT(x) callout_init(x, 0 /* mpsafe */)
428108853Ssimokawa#else
429110193Ssimokawa#define CALLOUT_INIT(x) callout_init(x)
430108853Ssimokawa#endif
431110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
432110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
433110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
434110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
435108853Ssimokawa
436110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
437110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
438110193Ssimokawa
439103285Sikob	/* Locate our children */
440103285Sikob	bus_generic_probe(dev);
441103285Sikob
442103285Sikob	/* launch attachement of the added children */
443103285Sikob	bus_generic_attach(dev);
444103285Sikob
445110577Ssimokawa#if 1
446103285Sikob	/* bus_reset */
447103285Sikob	fc->ibr(fc);
448110577Ssimokawa#endif
449103285Sikob
450103285Sikob	return 0;
451103285Sikob}
452103285Sikob
453103285Sikob/*
454103285Sikob * Attach it as child.
455103285Sikob */
456103285Sikobstatic device_t
457103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
458103285Sikob{
459103285Sikob        device_t child;
460103285Sikob	struct firewire_softc *sc;
461103285Sikob
462103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
463103285Sikob	child = device_add_child(dev, name, unit);
464103285Sikob	if (child) {
465103285Sikob		device_set_ivars(child, sc->fc);
466103285Sikob		device_probe_and_attach(child);
467103285Sikob	}
468103285Sikob
469103285Sikob	return child;
470103285Sikob}
471106790Ssimokawa
472103285Sikob/*
473103285Sikob * Dettach it.
474103285Sikob */
475103285Sikobstatic int
476103285Sikobfirewire_detach( device_t dev )
477103285Sikob{
478103285Sikob	struct firewire_softc *sc;
479111074Ssimokawa	struct csrdir *csrd, *next;
480111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
481103285Sikob
482103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
483106790Ssimokawa
484111078Ssimokawa	bus_generic_detach(dev);
485111078Ssimokawa
486111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
487111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
488111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
489111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
490111078Ssimokawa
491103285Sikob#if __FreeBSD_version >= 500000
492103285Sikob	destroy_dev(sc->dev);
493103285Sikob#else
494103285Sikob	{
495103285Sikob		int j;
496103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
497103285Sikob			destroy_dev(sc->dev[j]);
498103285Sikob	}
499103285Sikob#endif
500103285Sikob	/* XXX xfree_free and untimeout on all xfers */
501111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
502111078Ssimokawa							fwdev = fwdev_next) {
503111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
504111078Ssimokawa		free(fwdev, M_FW);
505111078Ssimokawa	}
506111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
507111074Ssimokawa		next = SLIST_NEXT(csrd, link);
508111074Ssimokawa		free(csrd, M_FW);
509111074Ssimokawa	}
510110195Ssimokawa	free(sc->fc->topology_map, M_FW);
511110195Ssimokawa	free(sc->fc->speed_map, M_FW);
512103285Sikob	return(0);
513103285Sikob}
514103285Sikob#if 0
515103285Sikobstatic int
516103285Sikobfirewire_shutdown( device_t dev )
517103285Sikob{
518103285Sikob	return 0;
519103285Sikob}
520103285Sikob#endif
521106790Ssimokawa
522110577Ssimokawa
523110577Ssimokawastatic void
524110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
525110577Ssimokawa{
526110577Ssimokawa	struct fw_xfer *xfer;
527110577Ssimokawa
528110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
529110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
530110577Ssimokawa		xfer->resp = EAGAIN;
531110577Ssimokawa		switch (xfer->act_type) {
532110577Ssimokawa		case FWACT_XFER:
533110577Ssimokawa			fw_xfer_done(xfer);
534110577Ssimokawa			break;
535110577Ssimokawa		default:
536110577Ssimokawa			/* ??? */
537110577Ssimokawa			fw_xfer_free(xfer);
538110577Ssimokawa			break;
539110577Ssimokawa		}
540110577Ssimokawa	}
541110577Ssimokawa}
542110577Ssimokawa
543110798Ssimokawavoid
544110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
545110798Ssimokawa{
546110798Ssimokawa	int i;
547110798Ssimokawa
548110798Ssimokawa	fw_xferq_drain(fc->atq);
549110798Ssimokawa	fw_xferq_drain(fc->ats);
550110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
551110798Ssimokawa		fw_xferq_drain(fc->it[i]);
552110798Ssimokawa}
553110798Ssimokawa
554103285Sikob/*
555106790Ssimokawa * Called after bus reset.
556103285Sikob */
557106790Ssimokawavoid
558106790Ssimokawafw_busreset(struct firewire_comm *fc)
559103285Sikob{
560103285Sikob	int i;
561103285Sikob
562103285Sikob	switch(fc->status){
563103285Sikob	case FWBUSMGRELECT:
564110193Ssimokawa		callout_stop(&fc->bmr_callout);
565103285Sikob		break;
566103285Sikob	default:
567103285Sikob		break;
568103285Sikob	}
569103285Sikob	fc->status = FWBUSRESET;
570103285Sikob	CSRARC(fc, STATE_CLEAR)
571103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
572103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
573103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
574103285Sikob
575103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
576103285Sikob	fc->irm = -1;
577103285Sikob
578103285Sikob	fc->max_node = -1;
579103285Sikob
580103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
581103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
582103285Sikob	}
583103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
584103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
585103285Sikob	CSRARC(fc, RESET_START) = 0;
586103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
587103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
588103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
589103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
590103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
591103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
592103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
593103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
594103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
595103285Sikob
596103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
597103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
598103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
599103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
600103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
601103285Sikob
602103285Sikob/* DV depend CSRs see blue book */
603103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
604103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
605103285Sikob
606103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
607103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
608103285Sikob}
609106790Ssimokawa
610103285Sikob/* Call once after reboot */
611106790Ssimokawavoid fw_init(struct firewire_comm *fc)
612103285Sikob{
613103285Sikob	int i;
614106543Ssimokawa	struct csrdir *csrd;
615106543Ssimokawa#ifdef FW_VMACCESS
616103285Sikob	struct fw_xfer *xfer;
617103285Sikob	struct fw_bind *fwb;
618106543Ssimokawa#endif
619103285Sikob
620103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
621103285Sikob
622103285Sikob	fc->arq->queued = 0;
623103285Sikob	fc->ars->queued = 0;
624103285Sikob	fc->atq->queued = 0;
625103285Sikob	fc->ats->queued = 0;
626103285Sikob
627109379Ssimokawa	fc->arq->psize = PAGE_SIZE;
628109379Ssimokawa	fc->ars->psize = PAGE_SIZE;
629109379Ssimokawa	fc->atq->psize = 0;
630109379Ssimokawa	fc->ats->psize = 0;
631103285Sikob
632103285Sikob
633103285Sikob	fc->arq->buf = NULL;
634103285Sikob	fc->ars->buf = NULL;
635103285Sikob	fc->atq->buf = NULL;
636103285Sikob	fc->ats->buf = NULL;
637103285Sikob
638103285Sikob	fc->arq->flag = FWXFERQ_PACKET;
639103285Sikob	fc->ars->flag = FWXFERQ_PACKET;
640103285Sikob	fc->atq->flag = FWXFERQ_PACKET;
641103285Sikob	fc->ats->flag = FWXFERQ_PACKET;
642103285Sikob
643103285Sikob	STAILQ_INIT(&fc->atq->q);
644103285Sikob	STAILQ_INIT(&fc->ats->q);
645103285Sikob
646103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
647103285Sikob		fc->it[i]->queued = 0;
648103285Sikob		fc->ir[i]->queued = 0;
649103285Sikob
650103285Sikob		fc->it[i]->start = NULL;
651103285Sikob		fc->ir[i]->start = NULL;
652103285Sikob
653103285Sikob		fc->it[i]->buf = NULL;
654103285Sikob		fc->ir[i]->buf = NULL;
655103285Sikob
656103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
657103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
658103285Sikob
659103285Sikob		STAILQ_INIT(&fc->it[i]->q);
660103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
661103285Sikob
662103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
663103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
664103285Sikob	}
665103285Sikob
666103285Sikob	fc->arq->maxq = FWMAXQUEUE;
667103285Sikob	fc->ars->maxq = FWMAXQUEUE;
668103285Sikob	fc->atq->maxq = FWMAXQUEUE;
669103285Sikob	fc->ats->maxq = FWMAXQUEUE;
670103285Sikob
671103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
672103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
673103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
674103285Sikob	}
675103285Sikob/* Initialize csr registers */
676103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
677103285Sikob				sizeof(struct fw_topology_map),
678110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
679103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
680103285Sikob				sizeof(struct fw_speed_map),
681110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
682103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
683103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
684103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
685103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
686103285Sikob
687110193Ssimokawa	STAILQ_INIT(&fc->devices);
688103285Sikob	STAILQ_INIT(&fc->pending);
689103285Sikob
690103285Sikob/* Initialize csr ROM work space */
691103285Sikob	SLIST_INIT(&fc->ongocsr);
692103285Sikob	SLIST_INIT(&fc->csrfree);
693103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
694110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
695103285Sikob		if(csrd == NULL) break;
696103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
697103285Sikob	}
698103285Sikob
699103285Sikob/* Initialize Async handlers */
700103285Sikob	STAILQ_INIT(&fc->binds);
701103285Sikob	for( i = 0 ; i < 0x40 ; i++){
702103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
703103285Sikob	}
704103285Sikob
705103285Sikob/* DV depend CSRs see blue book */
706103285Sikob#if 0
707103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
708103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
709103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
710103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
711103285Sikob	}
712103285Sikob
713103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
714103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
715103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
716103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
717103285Sikob	}
718103285Sikob#endif
719103285Sikob
720103285Sikob
721106543Ssimokawa#ifdef FW_VMACCESS
722103285Sikob	xfer = fw_xfer_alloc();
723103285Sikob	if(xfer == NULL) return;
724103285Sikob
725110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
726103285Sikob	if(fwb == NULL){
727103285Sikob		fw_xfer_free(xfer);
728103285Sikob	}
729103285Sikob	xfer->act.hand = fw_vmaccess;
730103285Sikob	xfer->act_type = FWACT_XFER;
731103285Sikob	xfer->fc = fc;
732103285Sikob	xfer->sc = NULL;
733103285Sikob
734103285Sikob	fwb->start_hi = 0x2;
735103285Sikob	fwb->start_lo = 0;
736103285Sikob	fwb->addrlen = 0xffffffff;
737103285Sikob	fwb->xfer = xfer;
738103285Sikob	fw_bindadd(fc, fwb);
739106543Ssimokawa#endif
740103285Sikob}
741106790Ssimokawa
742103285Sikob/*
743103285Sikob * To lookup binded process from IEEE1394 address.
744103285Sikob */
745106813Ssimokawastruct fw_bind *
746106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
747103285Sikob{
748103285Sikob	struct fw_bind *tfw;
749103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
750103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
751103285Sikob		if(tfw->xfer->act_type != FWACT_NULL &&
752103285Sikob			tfw->start_hi == dest_hi &&
753103285Sikob			tfw->start_lo <= dest_lo &&
754103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
755103285Sikob			return(tfw);
756103285Sikob		}
757103285Sikob	}
758103285Sikob	return(NULL);
759103285Sikob}
760106790Ssimokawa
761103285Sikob/*
762103285Sikob * To bind IEEE1394 address block to process.
763103285Sikob */
764106790Ssimokawaint
765106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
766103285Sikob{
767103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
768103285Sikob	int err = 0;
769103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
770103285Sikob	if(tfw == NULL){
771103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
772103285Sikob		goto out;
773103285Sikob	}
774103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
775103285Sikob		(tfw->start_hi == fwb->start_hi &&
776103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
777103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
778103285Sikob		goto out;
779103285Sikob	}
780103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
781103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
782103285Sikob		   (tfw->start_hi == fwb->start_hi &&
783103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
784103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
785103285Sikob			if(tfw2 == NULL)
786103285Sikob				break;
787103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
788103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
789103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
790103285Sikob				break;
791103285Sikob			}else{
792103285Sikob				err = EBUSY;
793103285Sikob				goto out;
794103285Sikob			}
795103285Sikob		}
796103285Sikob	}
797103285Sikob	if(tfw != NULL){
798103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
799103285Sikob	}else{
800103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
801103285Sikob	}
802103285Sikobout:
803103285Sikob	if(!err && fwb->xfer->act_type == FWACT_CH){
804103285Sikob		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
805103285Sikob	}
806103285Sikob	return err;
807103285Sikob}
808103285Sikob
809103285Sikob/*
810103285Sikob * To free IEEE1394 address block.
811103285Sikob */
812106790Ssimokawaint
813106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
814103285Sikob{
815103285Sikob	int s;
816103285Sikob
817103285Sikob	s = splfw();
818103285Sikob	/* shall we check the existance? */
819103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
820103285Sikob	splx(s);
821103285Sikob	if (fwb->xfer)
822103285Sikob		fw_xfer_free(fwb->xfer);
823103285Sikob
824103285Sikob	return 0;
825103285Sikob}
826103285Sikob
827103285Sikob/*
828103285Sikob * To free transaction label.
829103285Sikob */
830106790Ssimokawastatic void
831106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
832103285Sikob{
833103285Sikob	struct tlabel *tl;
834103285Sikob	int s = splfw();
835103285Sikob
836103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
837103285Sikob		tl = STAILQ_NEXT(tl, link)){
838103285Sikob		if(tl->xfer == xfer){
839103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
840110195Ssimokawa			free(tl, M_FW);
841103285Sikob			splx(s);
842103285Sikob			return;
843103285Sikob		}
844103285Sikob	}
845103285Sikob	splx(s);
846103285Sikob	return;
847103285Sikob}
848106790Ssimokawa
849103285Sikob/*
850103285Sikob * To obtain XFER structure by transaction label.
851103285Sikob */
852106790Ssimokawastatic struct fw_xfer *
853106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
854103285Sikob{
855103285Sikob	struct fw_xfer *xfer;
856103285Sikob	struct tlabel *tl;
857103285Sikob	int s = splfw();
858103285Sikob
859103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
860103285Sikob		tl = STAILQ_NEXT(tl, link)){
861103285Sikob		if(tl->xfer->dst == node){
862103285Sikob			xfer = tl->xfer;
863103285Sikob			splx(s);
864110577Ssimokawa			if (firewire_debug > 2)
865110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
866103285Sikob			return(xfer);
867103285Sikob		}
868103285Sikob	}
869110577Ssimokawa	if (firewire_debug > 1)
870110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
871103285Sikob	splx(s);
872103285Sikob	return(NULL);
873103285Sikob}
874106790Ssimokawa
875103285Sikob/*
876103285Sikob * To allocate IEEE1394 XFER structure.
877103285Sikob */
878106790Ssimokawastruct fw_xfer *
879110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
880103285Sikob{
881103285Sikob	struct fw_xfer *xfer;
882106790Ssimokawa
883110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
884106790Ssimokawa	if (xfer == NULL)
885106790Ssimokawa		return xfer;
886106790Ssimokawa
887110577Ssimokawa	microtime(&xfer->tv);
888103285Sikob	xfer->sub = -1;
889110269Ssimokawa	xfer->malloc = type;
890106790Ssimokawa
891103285Sikob	return xfer;
892103285Sikob}
893106790Ssimokawa
894103285Sikob/*
895103285Sikob * IEEE1394 XFER post process.
896103285Sikob */
897103285Sikobvoid
898103285Sikobfw_xfer_done(struct fw_xfer *xfer)
899103285Sikob{
900103285Sikob	if (xfer->act.hand == NULL)
901103285Sikob		return;
902103285Sikob
903103285Sikob#if XFER_TIMEOUT
904103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
905103285Sikob#endif
906103285Sikob
907103285Sikob	if (xfer->fc->status != FWBUSRESET)
908103285Sikob		xfer->act.hand(xfer);
909103285Sikob	else {
910103285Sikob		printf("fw_xfer_done: pending\n");
911103285Sikob		if (xfer->fc != NULL)
912103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
913103285Sikob		else
914103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
915103285Sikob	}
916103285Sikob}
917103285Sikob
918103285Sikob/*
919103285Sikob * To free IEEE1394 XFER structure.
920103285Sikob */
921106790Ssimokawavoid
922106790Ssimokawafw_xfer_free( struct fw_xfer* xfer)
923103285Sikob{
924103285Sikob	int s;
925103285Sikob	if(xfer == NULL ) return;
926103285Sikob	if(xfer->state == FWXF_INQ){
927103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
928103285Sikob		s = splfw();
929103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
930103285Sikob		xfer->q->queued --;
931103285Sikob		splx(s);
932103285Sikob	}
933103285Sikob	if(xfer->fc != NULL){
934103285Sikob		if(xfer->state == FWXF_START){
935103285Sikob#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
936103285Sikob			printf("fw_xfer_free FWXF_START\n");
937103285Sikob#endif
938103285Sikob			s = splfw();
939103285Sikob			xfer->q->drain(xfer->fc, xfer);
940103285Sikob			splx(s);
941103285Sikob		}
942103285Sikob	}
943103285Sikob	if(xfer->send.buf != NULL){
944110195Ssimokawa		free(xfer->send.buf, M_FW);
945103285Sikob	}
946103285Sikob	if(xfer->recv.buf != NULL){
947110195Ssimokawa		free(xfer->recv.buf, M_FW);
948103285Sikob	}
949103285Sikob	if(xfer->fc != NULL){
950103285Sikob		fw_tl_free(xfer->fc, xfer);
951103285Sikob	}
952110269Ssimokawa	free(xfer, xfer->malloc);
953103285Sikob}
954103285Sikob
955103285Sikobstatic void
956110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
957103285Sikob{
958103285Sikob#if 0
959110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
960103285Sikob				xfer->state, xfer->resp);
961103285Sikob#endif
962103285Sikob	fw_xfer_free(xfer);
963103285Sikob}
964103285Sikob
965103285Sikob/*
966103285Sikob * To configure PHY.
967103285Sikob */
968103285Sikobstatic void
969103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
970103285Sikob{
971103285Sikob	struct fw_xfer *xfer;
972103285Sikob	struct fw_pkt *fp;
973103285Sikob
974103285Sikob	fc->status = FWBUSPHYCONF;
975103285Sikob
976109280Ssimokawa#if 0
977103285Sikob	DELAY(100000);
978109280Ssimokawa#endif
979110269Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
980103285Sikob	xfer->send.len = 12;
981103285Sikob	xfer->send.off = 0;
982103285Sikob	xfer->fc = fc;
983103285Sikob	xfer->retry_req = fw_asybusy;
984110072Ssimokawa	xfer->act.hand = fw_asy_callback_free;
985103285Sikob
986103285Sikob	xfer->send.buf = malloc(sizeof(u_int32_t),
987110195Ssimokawa					M_FW, M_NOWAIT | M_ZERO);
988103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
989103285Sikob	fp->mode.ld[1] = 0;
990103285Sikob	if (root_node >= 0)
991103285Sikob		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
992103285Sikob	if (gap_count >= 0)
993103285Sikob		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
994103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
995103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
996103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
997103285Sikob
998107653Ssimokawa	if (firewire_debug)
999107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1000103285Sikob						root_node, gap_count);
1001103285Sikob	fw_asyreq(fc, -1, xfer);
1002103285Sikob}
1003103285Sikob
1004103285Sikob#if 0
1005103285Sikob/*
1006103285Sikob * Dump self ID.
1007103285Sikob */
1008103285Sikobstatic void
1009103285Sikobfw_print_sid(u_int32_t sid)
1010103285Sikob{
1011103285Sikob	union fw_self_id *s;
1012103285Sikob	s = (union fw_self_id *) &sid;
1013103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1014103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1015103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1016103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1017103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1018103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1019103285Sikob}
1020103285Sikob#endif
1021103285Sikob
1022103285Sikob/*
1023103285Sikob * To receive self ID.
1024103285Sikob */
1025103285Sikobvoid fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1026103285Sikob{
1027103285Sikob	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1028103285Sikob	union fw_self_id *self_id;
1029103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1030103285Sikob
1031103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1032103285Sikob	fc->status = FWBUSINIT;
1033103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1034103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1035103285Sikob	fc->status = FWBUSCYMELECT;
1036103285Sikob	fc->topology_map->crc_len = 2;
1037103285Sikob	fc->topology_map->generation ++;
1038103285Sikob	fc->topology_map->self_id_count = 0;
1039103285Sikob	fc->topology_map->node_count = 0;
1040103285Sikob	fc->speed_map->generation ++;
1041103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1042103285Sikob	self_id = &fc->topology_map->self_id[0];
1043103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1044103285Sikob		if (sid[1] != ~sid[0]) {
1045103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1046103285Sikob			sid += 2;
1047103285Sikob			continue;
1048103285Sikob		}
1049103285Sikob		*self_id = *((union fw_self_id *)sid);
1050103285Sikob		fc->topology_map->crc_len++;
1051103285Sikob		if(self_id->p0.sequel == 0){
1052103285Sikob			fc->topology_map->node_count ++;
1053103285Sikob			c_port = 0;
1054103285Sikob#if 0
1055103285Sikob			fw_print_sid(sid[0]);
1056103285Sikob#endif
1057103285Sikob			node = self_id->p0.phy_id;
1058103285Sikob			if(fc->max_node < node){
1059103285Sikob				fc->max_node = self_id->p0.phy_id;
1060103285Sikob			}
1061103285Sikob			/* XXX I'm not sure this is the right speed_map */
1062103285Sikob			fc->speed_map->speed[node][node]
1063103285Sikob					= self_id->p0.phy_speed;
1064103285Sikob			for (j = 0; j < node; j ++) {
1065103285Sikob				fc->speed_map->speed[j][node]
1066103285Sikob					= fc->speed_map->speed[node][j]
1067103285Sikob					= min(fc->speed_map->speed[j][j],
1068103285Sikob							self_id->p0.phy_speed);
1069103285Sikob			}
1070103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1071103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1072103285Sikob				fc->irm = self_id->p0.phy_id;
1073103285Sikob			}
1074103285Sikob			if(self_id->p0.port0 >= 0x2){
1075103285Sikob				c_port++;
1076103285Sikob			}
1077103285Sikob			if(self_id->p0.port1 >= 0x2){
1078103285Sikob				c_port++;
1079103285Sikob			}
1080103285Sikob			if(self_id->p0.port2 >= 0x2){
1081103285Sikob				c_port++;
1082103285Sikob			}
1083103285Sikob		}
1084103285Sikob		if(c_port > 2){
1085103285Sikob			i_branch += (c_port - 2);
1086103285Sikob		}
1087103285Sikob		sid += 2;
1088103285Sikob		self_id++;
1089103285Sikob		fc->topology_map->self_id_count ++;
1090103285Sikob	}
1091108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1092103285Sikob	/* CRC */
1093103285Sikob	fc->topology_map->crc = fw_crc16(
1094103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1095103285Sikob			fc->topology_map->crc_len * 4);
1096103285Sikob	fc->speed_map->crc = fw_crc16(
1097103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1098103285Sikob			fc->speed_map->crc_len * 4);
1099103285Sikob	/* byteswap and copy to CSR */
1100103285Sikob	p = (u_int32_t *)fc->topology_map;
1101103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1102103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1103103285Sikob	p = (u_int32_t *)fc->speed_map;
1104103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1105103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1106103285Sikob	/* don't byte-swap u_int8_t array */
1107103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1108103285Sikob
1109103285Sikob	fc->max_hop = fc->max_node - i_branch;
1110103285Sikob#if 1
1111103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1112103285Sikob#endif
1113103285Sikob
1114103285Sikob	if(fc->irm == -1 ){
1115103285Sikob		printf(", Not found IRM capable node");
1116103285Sikob	}else{
1117103285Sikob		printf(", cable IRM = %d", fc->irm);
1118103285Sikob		if (fc->irm == fc->nodeid)
1119110016Ssimokawa			printf(" (me)");
1120103285Sikob	}
1121110016Ssimokawa	printf("\n");
1122103285Sikob
1123109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1124109736Ssimokawa		if (fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)) {
1125103285Sikob			fc->status = FWBUSMGRDONE;
1126103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1127109736Ssimokawa		} else {
1128103285Sikob			fc->status = FWBUSMGRELECT;
1129110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1130110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1131103285Sikob		}
1132109736Ssimokawa	} else {
1133103285Sikob		fc->status = FWBUSMGRDONE;
1134109736Ssimokawa#if 0
1135108655Ssimokawa		device_printf(fc->bdev, "BMR = %x\n",
1136108655Ssimokawa				CSRARC(fc, BUS_MGR_ID));
1137109736Ssimokawa#endif
1138103285Sikob	}
1139110195Ssimokawa	free(buf, M_FW);
1140103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1141110072Ssimokawa		/* I am BMGR */
1142110072Ssimokawa		fw_bmr(fc);
1143103285Sikob	}
1144108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1145108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1146103285Sikob}
1147106790Ssimokawa
1148103285Sikob/*
1149103285Sikob * To probe devices on the IEEE1394 bus.
1150103285Sikob */
1151106790Ssimokawastatic void
1152106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1153103285Sikob{
1154103285Sikob	int s;
1155103285Sikob	struct fw_device *fwdev, *next;
1156103285Sikob
1157103285Sikob	s = splfw();
1158103285Sikob	fc->status = FWBUSEXPLORE;
1159103285Sikob	fc->retry_count = 0;
1160103285Sikob
1161103285Sikob/*
1162103285Sikob * Invalidate all devices, just after bus reset. Devices
1163103285Sikob * to be removed has not been seen longer time.
1164103285Sikob */
1165110193Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1166110193Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1167110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1168103285Sikob			fwdev->status = FWDEVINVAL;
1169103285Sikob			fwdev->rcnt = 0;
1170110193Ssimokawa		} else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1171103285Sikob			fwdev->rcnt ++;
1172110193Ssimokawa		} else {
1173110193Ssimokawa			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1174110195Ssimokawa			free(fwdev, M_FW);
1175103285Sikob		}
1176103285Sikob	}
1177103285Sikob	fc->ongonode = 0;
1178103285Sikob	fc->ongoaddr = CSRROMOFF;
1179103285Sikob	fc->ongodev = NULL;
1180103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1181103285Sikob	fw_bus_explore(fc);
1182103285Sikob	splx(s);
1183103285Sikob}
1184106790Ssimokawa
1185103285Sikob/*
1186103285Sikob * To collect device informations on the IEEE1394 bus.
1187103285Sikob */
1188106790Ssimokawastatic void
1189106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1190103285Sikob{
1191103285Sikob	int err = 0;
1192110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1193103285Sikob	u_int32_t addr;
1194103285Sikob	struct fw_xfer *xfer;
1195103285Sikob	struct fw_pkt *fp;
1196103285Sikob
1197103285Sikob	if(fc->status != FWBUSEXPLORE)
1198103285Sikob		return;
1199103285Sikob
1200103285Sikobloop:
1201103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1202103285Sikob
1203103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1204103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1205103285Sikob
1206103285Sikob	/* check link */
1207103285Sikob	/* XXX we need to check phy_id first */
1208103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1209110577Ssimokawa		if (firewire_debug)
1210110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1211103285Sikob		fc->ongonode++;
1212103285Sikob		goto loop;
1213103285Sikob	}
1214103285Sikob
1215103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1216103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1217103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1218103285Sikob		fc->ongoaddr = CSRROMOFF;
1219103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1220103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1221103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1222103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1223103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1224103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1225103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1226103285Sikob	}else if(fc->ongodev == NULL){
1227110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1228110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1229103285Sikob				break;
1230103285Sikob		if(fwdev != NULL){
1231103285Sikob			fwdev->dst = fc->ongonode;
1232103285Sikob			fwdev->status = FWDEVATTACHED;
1233103285Sikob			fc->ongonode++;
1234103285Sikob			fc->ongoaddr = CSRROMOFF;
1235103285Sikob			fc->ongodev = NULL;
1236103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1237103285Sikob			goto loop;
1238103285Sikob		}
1239110195Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW, M_NOWAIT);
1240103285Sikob		if(fwdev == NULL)
1241103285Sikob			return;
1242106810Ssimokawa		fwdev->fc = fc;
1243103285Sikob		fwdev->rommax = 0;
1244103285Sikob		fwdev->dst = fc->ongonode;
1245103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1246103285Sikob		fwdev->status = FWDEVINIT;
1247103285Sikob#if 0
1248103285Sikob		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1249103285Sikob			>> ((3 - (fc->ongonode % 4)) * 8);
1250103285Sikob#else
1251103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1252103285Sikob#endif
1253103285Sikob
1254110179Ssimokawa		pfwdev = NULL;
1255110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1256110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1257110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1258110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1259110179Ssimokawa				break;
1260110179Ssimokawa			pfwdev = tfwdev;
1261103285Sikob		}
1262110179Ssimokawa		if (pfwdev == NULL)
1263110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1264110179Ssimokawa		else
1265110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1266103285Sikob
1267108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1268107653Ssimokawa			linkspeed[fwdev->speed],
1269107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1270103285Sikob
1271103285Sikob		fc->ongodev = fwdev;
1272103285Sikob		fc->ongoaddr = CSRROMOFF;
1273103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1274103285Sikob	}else{
1275103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1276103285Sikob	}
1277103285Sikob#if 0
1278103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1279103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1280103285Sikob		fw_bus_explore_callback);
1281103285Sikob	if(xfer == NULL) goto done;
1282103285Sikob#else
1283110269Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1284103285Sikob	if(xfer == NULL){
1285103285Sikob		goto done;
1286103285Sikob	}
1287103285Sikob	xfer->send.len = 16;
1288103285Sikob	xfer->spd = 0;
1289110195Ssimokawa	xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1290103285Sikob	if(xfer->send.buf == NULL){
1291103285Sikob		fw_xfer_free( xfer);
1292103285Sikob		return;
1293103285Sikob	}
1294103285Sikob
1295103285Sikob	xfer->send.off = 0;
1296103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1297103285Sikob	fp->mode.rreqq.dest_hi = htons(0xffff);
1298103285Sikob	fp->mode.rreqq.tlrt = 0;
1299103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1300103285Sikob	fp->mode.rreqq.pri = 0;
1301103285Sikob	fp->mode.rreqq.src = 0;
1302103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1303103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1304103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr);
1305103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1306103285Sikob
1307110577Ssimokawa	if (firewire_debug)
1308110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1309110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1310103285Sikob	err = fw_asyreq(fc, -1, xfer);
1311103285Sikob	if(err){
1312103285Sikob		fw_xfer_free( xfer);
1313103285Sikob		return;
1314103285Sikob	}
1315103285Sikob#endif
1316103285Sikob	return;
1317103285Sikobdone:
1318103285Sikob	/* fw_attach_devs */
1319103285Sikob	fc->status = FWBUSEXPDONE;
1320107653Ssimokawa	if (firewire_debug)
1321107653Ssimokawa		printf("bus_explore done\n");
1322103285Sikob	fw_attach_dev(fc);
1323103285Sikob	return;
1324103285Sikob
1325103285Sikob}
1326106790Ssimokawa
1327103285Sikob/* Portable Async. request read quad */
1328106790Ssimokawastruct fw_xfer *
1329106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1330105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1331105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1332103285Sikob{
1333103285Sikob	struct fw_xfer *xfer;
1334103285Sikob	struct fw_pkt *fp;
1335103285Sikob	int err;
1336103285Sikob
1337110269Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1338103285Sikob	if(xfer == NULL){
1339103285Sikob		return NULL;
1340103285Sikob	}
1341103285Sikob	xfer->send.len = 16;
1342103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1343110195Ssimokawa	xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1344103285Sikob	if(xfer->send.buf == NULL){
1345103285Sikob		fw_xfer_free( xfer);
1346103285Sikob		return NULL;
1347103285Sikob	}
1348103285Sikob
1349103285Sikob	xfer->send.off = 0;
1350103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1351103285Sikob	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1352103285Sikob	if(tl & FWP_TL_VALID){
1353103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1354103285Sikob	}else{
1355103285Sikob		fp->mode.rreqq.tlrt = 0;
1356103285Sikob	}
1357103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1358103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1359103285Sikob	fp->mode.rreqq.pri = 0;
1360103285Sikob	fp->mode.rreqq.src = 0;
1361103285Sikob	xfer->dst = addr_hi >> 16;
1362103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1363103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1364103285Sikob	xfer->act.hand = hand;
1365103285Sikob
1366103285Sikob	err = fw_asyreq(fc, -1, xfer);
1367103285Sikob	if(err){
1368103285Sikob		fw_xfer_free( xfer);
1369103285Sikob		return NULL;
1370103285Sikob	}
1371103285Sikob	return xfer;
1372103285Sikob}
1373106790Ssimokawa
1374103285Sikob/*
1375103285Sikob * Callback for the IEEE1394 bus information collection.
1376103285Sikob */
1377106790Ssimokawastatic void
1378106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1379106790Ssimokawa{
1380103285Sikob	struct firewire_comm *fc;
1381103285Sikob	struct fw_pkt *sfp,*rfp;
1382103285Sikob	struct csrhdr *chdr;
1383103285Sikob	struct csrdir *csrd;
1384103285Sikob	struct csrreg *csrreg;
1385103285Sikob	u_int32_t offset;
1386103285Sikob
1387103285Sikob
1388110577Ssimokawa	if(xfer == NULL) {
1389110577Ssimokawa		printf("xfer == NULL\n");
1390110577Ssimokawa		return;
1391110577Ssimokawa	}
1392103285Sikob	fc = xfer->fc;
1393110577Ssimokawa
1394110577Ssimokawa	if (firewire_debug)
1395110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1396110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1397110577Ssimokawa
1398103285Sikob	if(xfer->resp != 0){
1399110577Ssimokawa		printf("node%d: resp=%d addr=0x%x\n",
1400110577Ssimokawa			fc->ongonode, xfer->resp, fc->ongoaddr);
1401103285Sikob		fc->retry_count++;
1402103285Sikob		goto nextnode;
1403103285Sikob	}
1404103285Sikob
1405103285Sikob	if(xfer->send.buf == NULL){
1406110577Ssimokawa		printf("node%d: send.buf=NULL addr=0x%x\n",
1407103285Sikob			fc->ongonode, fc->ongoaddr);
1408103285Sikob		fc->retry_count++;
1409103285Sikob		goto nextnode;
1410103285Sikob	}
1411103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1412103285Sikob
1413103285Sikob	if(xfer->recv.buf == NULL){
1414110577Ssimokawa		printf("node%d: recv.buf=NULL addr=0x%x\n",
1415103285Sikob			fc->ongonode, fc->ongoaddr);
1416103285Sikob		fc->retry_count++;
1417103285Sikob		goto nextnode;
1418103285Sikob	}
1419103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1420103285Sikob#if 0
1421103285Sikob	{
1422103285Sikob		u_int32_t *qld;
1423103285Sikob		int i;
1424103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1425103285Sikob		printf("len:%d\n", xfer->recv.len);
1426103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1427103285Sikob			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1428103285Sikob			if((i % 16) == 15) printf("\n");
1429103285Sikob		}
1430103285Sikob		if((i % 16) != 15) printf("\n");
1431103285Sikob	}
1432103285Sikob#endif
1433103285Sikob	if(fc->ongodev == NULL){
1434103285Sikob		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1435103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1436103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1437110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */
1438103285Sikob			if(chdr->info_len == 1){
1439110577Ssimokawa				if (firewire_debug)
1440110577Ssimokawa					printf("node%d: minimal config\n",
1441110577Ssimokawa								fc->ongonode);
1442103285Sikob				goto nextnode;
1443103285Sikob			}else{
1444103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1445103285Sikob			}
1446103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1447103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1448103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1449103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1450103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1451110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1452110577Ssimokawa				if (firewire_debug)
1453110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1454110577Ssimokawa							fc->ongonode);
1455103285Sikob				goto nextnode;
1456110577Ssimokawa			}
1457103285Sikob			fc->ongoaddr = CSRROMOFF;
1458103285Sikob		}
1459103285Sikob	}else{
1460103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1461103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1462103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1463103285Sikob		}
1464103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1465103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1466103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1467103285Sikob			offset = CSRROMOFF;
1468103285Sikob		}else{
1469103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1470103285Sikob			offset = csrd->off;
1471103285Sikob		}
1472103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1473103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1474103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1475103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1476103285Sikob				if(csrd == NULL){
1477103285Sikob					goto nextnode;
1478103285Sikob				}else{
1479103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1480103285Sikob					fc->ongoaddr += csrreg->val * 4;
1481103285Sikob					csrd->off = fc->ongoaddr;
1482103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1483103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1484103285Sikob					goto nextaddr;
1485103285Sikob				}
1486103285Sikob			}
1487103285Sikob		}
1488103285Sikob		fc->ongoaddr += 4;
1489103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1490103285Sikob				(fc->ongodev->rommax < 0x414)){
1491103285Sikob			if(fc->ongodev->rommax <= 0x414){
1492103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1493103285Sikob				if(csrd == NULL) goto nextnode;
1494103285Sikob				csrd->off = fc->ongoaddr;
1495103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1496103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1497103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1498103285Sikob			}
1499103285Sikob			goto nextaddr;
1500103285Sikob		}
1501103285Sikob
1502103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1503103285Sikob			if(csrd == NULL){
1504103285Sikob				goto nextnode;
1505103285Sikob			};
1506103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1507103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1508103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1509103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1510103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1511103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1512103285Sikob				offset = CSRROMOFF;
1513103285Sikob			}else{
1514103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1515103285Sikob				offset = csrd->off;
1516103285Sikob			}
1517103285Sikob		}
1518103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1519103285Sikob			goto nextnode;
1520103285Sikob		}
1521103285Sikob	}
1522103285Sikobnextaddr:
1523103285Sikob	fw_xfer_free( xfer);
1524103285Sikob	fw_bus_explore(fc);
1525103285Sikob	return;
1526103285Sikobnextnode:
1527103285Sikob	fw_xfer_free( xfer);
1528103285Sikob	fc->ongonode++;
1529103285Sikob/* housekeeping work space */
1530103285Sikob	fc->ongoaddr = CSRROMOFF;
1531103285Sikob	fc->ongodev = NULL;
1532103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1533103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1534103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1535103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1536103285Sikob	}
1537103285Sikob	fw_bus_explore(fc);
1538103285Sikob	return;
1539103285Sikob}
1540103285Sikob
1541103285Sikob/*
1542103285Sikob * To obtain CSR register values.
1543103285Sikob */
1544106815Ssimokawau_int32_t
1545106815Ssimokawagetcsrdata(struct fw_device *fwdev, u_int8_t key)
1546103285Sikob{
1547103285Sikob	int i;
1548103285Sikob	struct csrhdr *chdr;
1549103285Sikob	struct csrreg *creg;
1550103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1551103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1552103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1553103285Sikob		if(creg->key == key){
1554103285Sikob			return (u_int32_t)creg->val;
1555103285Sikob		}
1556103285Sikob	}
1557103285Sikob	return 0;
1558103285Sikob}
1559106815Ssimokawa
1560103285Sikob/*
1561103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1562103285Sikob */
1563106815Ssimokawastatic void
1564106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1565103285Sikob{
1566103285Sikob	struct fw_device *fwdev;
1567103285Sikob	struct fw_xfer *xfer;
1568103285Sikob	int i, err;
1569103285Sikob	device_t *devlistp;
1570103285Sikob	int devcnt;
1571103285Sikob	struct firewire_dev_comm *fdc;
1572108701Ssimokawa	u_int32_t spec, ver;
1573103285Sikob
1574110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link) {
1575103285Sikob		if(fwdev->status == FWDEVINIT){
1576108701Ssimokawa			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1577108701Ssimokawa			if(spec == 0)
1578103285Sikob				continue;
1579108701Ssimokawa			ver = getcsrdata(fwdev, CSRKEY_VER);
1580108701Ssimokawa			if(ver == 0)
1581103285Sikob				continue;
1582103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1583103285Sikob
1584108655Ssimokawa			device_printf(fc->bdev, "Device ");
1585108701Ssimokawa			switch(spec){
1586103285Sikob			case CSRVAL_ANSIT10:
1587108701Ssimokawa				switch(ver){
1588103285Sikob				case CSRVAL_T10SBP2:
1589107653Ssimokawa					printf("SBP-II");
1590103285Sikob					break;
1591103285Sikob				default:
1592103285Sikob					break;
1593103285Sikob				}
1594103285Sikob				break;
1595103285Sikob			case CSRVAL_1394TA:
1596108701Ssimokawa				switch(ver){
1597103285Sikob				case CSR_PROTAVC:
1598107653Ssimokawa					printf("AV/C");
1599103285Sikob					break;
1600103285Sikob				case CSR_PROTCAL:
1601107653Ssimokawa					printf("CAL");
1602103285Sikob					break;
1603103285Sikob				case CSR_PROTEHS:
1604107653Ssimokawa					printf("EHS");
1605103285Sikob					break;
1606103285Sikob				case CSR_PROTHAVI:
1607107653Ssimokawa					printf("HAVi");
1608103285Sikob					break;
1609103285Sikob				case CSR_PROTCAM104:
1610107653Ssimokawa					printf("1394 Cam 1.04");
1611103285Sikob					break;
1612103285Sikob				case CSR_PROTCAM120:
1613107653Ssimokawa					printf("1394 Cam 1.20");
1614103285Sikob					break;
1615103285Sikob				case CSR_PROTCAM130:
1616107653Ssimokawa					printf("1394 Cam 1.30");
1617103285Sikob					break;
1618103285Sikob				case CSR_PROTDPP:
1619107653Ssimokawa					printf("1394 Direct print");
1620103285Sikob					break;
1621103285Sikob				case CSR_PROTIICP:
1622107653Ssimokawa					printf("Industrial & Instrument");
1623103285Sikob					break;
1624103285Sikob				default:
1625107653Ssimokawa					printf("unknown 1394TA");
1626103285Sikob					break;
1627103285Sikob				}
1628103285Sikob				break;
1629103285Sikob			default:
1630107653Ssimokawa				printf("unknown spec");
1631103285Sikob				break;
1632103285Sikob			}
1633103285Sikob			fwdev->status = FWDEVATTACHED;
1634103285Sikob			printf("\n");
1635103285Sikob		}
1636103285Sikob	}
1637108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1638103285Sikob	if( err != 0 )
1639103285Sikob		return;
1640103285Sikob	for( i = 0 ; i < devcnt ; i++){
1641103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1642103285Sikob			fdc = device_get_softc(devlistp[i]);
1643103285Sikob			if (fdc->post_explore != NULL)
1644103285Sikob				fdc->post_explore(fdc);
1645103285Sikob		}
1646103285Sikob	}
1647103285Sikob	free(devlistp, M_TEMP);
1648103285Sikob
1649103285Sikob	/* call pending handlers */
1650103285Sikob	i = 0;
1651103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1652103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1653103285Sikob		i++;
1654103285Sikob		if (xfer->act.hand)
1655103285Sikob			xfer->act.hand(xfer);
1656103285Sikob	}
1657103285Sikob	if (i > 0)
1658103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1659103285Sikob	if (fc->retry_count > 0) {
1660111040Ssimokawa		printf("probe failed for %d node\n", fc->retry_count);
1661111040Ssimokawa#if 0
1662110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1663110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1664111040Ssimokawa#endif
1665103285Sikob	}
1666103285Sikob	return;
1667103285Sikob}
1668106815Ssimokawa
1669103285Sikob/*
1670103285Sikob * To allocate uniq transaction label.
1671103285Sikob */
1672106815Ssimokawastatic int
1673106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1674103285Sikob{
1675103285Sikob	u_int i;
1676103285Sikob	struct tlabel *tl, *tmptl;
1677103285Sikob	int s;
1678103285Sikob	static u_int32_t label = 0;
1679103285Sikob
1680103285Sikob	s = splfw();
1681103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1682103285Sikob		label = (label + 1) & 0x3f;
1683103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1684103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1685103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1686103285Sikob		}
1687103285Sikob		if(tmptl == NULL) {
1688110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1689103285Sikob			if (tl == NULL) {
1690103285Sikob				splx(s);
1691103285Sikob				return (-1);
1692103285Sikob			}
1693103285Sikob			tl->xfer = xfer;
1694103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1695103285Sikob			splx(s);
1696110577Ssimokawa			if (firewire_debug > 1)
1697110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1698110577Ssimokawa						xfer->dst, label);
1699103285Sikob			return(label);
1700103285Sikob		}
1701103285Sikob	}
1702103285Sikob	splx(s);
1703103285Sikob
1704103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1705103285Sikob	return(-1);
1706103285Sikob}
1707106815Ssimokawa
1708103285Sikob/*
1709103285Sikob * Generic packet receving process.
1710103285Sikob */
1711106815Ssimokawavoid
1712106815Ssimokawafw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1713103285Sikob{
1714103285Sikob	struct fw_pkt *fp, *resfp;
1715103285Sikob	struct fw_xfer *xfer;
1716103285Sikob	struct fw_bind *bind;
1717103285Sikob	struct firewire_softc *sc;
1718103285Sikob	int s;
1719103285Sikob#if 0
1720103285Sikob	{
1721103285Sikob		u_int32_t *qld;
1722103285Sikob		int i;
1723103285Sikob		qld = (u_int32_t *)buf;
1724103285Sikob		printf("spd %d len:%d\n", spd, len);
1725103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1726103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1727103285Sikob			if((i % 16) == 15) printf("\n");
1728103285Sikob		}
1729103285Sikob		if((i % 16) != 15) printf("\n");
1730103285Sikob	}
1731103285Sikob#endif
1732103285Sikob	fp = (struct fw_pkt *)(buf + off);
1733103285Sikob	switch(fp->mode.common.tcode){
1734103285Sikob	case FWTCODE_WRES:
1735103285Sikob	case FWTCODE_RRESQ:
1736103285Sikob	case FWTCODE_RRESB:
1737103285Sikob	case FWTCODE_LRES:
1738103285Sikob		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1739103285Sikob					fp->mode.hdr.tlrt >> 2);
1740103285Sikob		if(xfer == NULL) {
1741103285Sikob			printf("fw_rcv: unknown response "
1742110891Ssimokawa					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1743103285Sikob					fp->mode.common.tcode,
1744103285Sikob					ntohs(fp->mode.hdr.src),
1745103285Sikob					fp->mode.hdr.tlrt >> 2,
1746103285Sikob					fp->mode.hdr.tlrt & 3,
1747103285Sikob					fp->mode.rresq.data);
1748103285Sikob#if 1
1749103285Sikob			printf("try ad-hoc work around!!\n");
1750103285Sikob			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1751103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1752103285Sikob			if (xfer == NULL) {
1753103285Sikob				printf("no use...\n");
1754103285Sikob				goto err;
1755103285Sikob			}
1756103285Sikob#else
1757103285Sikob			goto err;
1758103285Sikob#endif
1759103285Sikob		}
1760103285Sikob		switch(xfer->act_type){
1761103285Sikob		case FWACT_XFER:
1762103285Sikob			if((xfer->sub >= 0) &&
1763103285Sikob				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1764103285Sikob				xfer->resp = EINVAL;
1765103285Sikob				fw_xfer_done(xfer);
1766103285Sikob				goto err;
1767103285Sikob			}
1768103285Sikob			xfer->recv.len = len;
1769103285Sikob			xfer->recv.off = off;
1770103285Sikob			xfer->recv.buf = buf;
1771103285Sikob			xfer->resp = 0;
1772103285Sikob			fw_xfer_done(xfer);
1773103285Sikob			return;
1774103285Sikob			break;
1775103285Sikob		case FWACT_CH:
1776103285Sikob		default:
1777103285Sikob			goto err;
1778103285Sikob			break;
1779103285Sikob		}
1780103285Sikob		break;
1781103285Sikob	case FWTCODE_WREQQ:
1782103285Sikob	case FWTCODE_WREQB:
1783103285Sikob	case FWTCODE_RREQQ:
1784103285Sikob	case FWTCODE_RREQB:
1785103285Sikob	case FWTCODE_LREQ:
1786103285Sikob		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1787103285Sikob			ntohl(fp->mode.rreqq.dest_lo));
1788103285Sikob		if(bind == NULL){
1789108712Ssimokawa#if __FreeBSD_version >= 500000
1790103285Sikob			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
1791108712Ssimokawa#else
1792108712Ssimokawa			printf("Unknown service addr 0x%08x:0x%08lx tcode=%x\n",
1793108712Ssimokawa#endif
1794103285Sikob				ntohs(fp->mode.rreqq.dest_hi),
1795103285Sikob				ntohl(fp->mode.rreqq.dest_lo),
1796103285Sikob				fp->mode.common.tcode);
1797103285Sikob			if (fc->status == FWBUSRESET) {
1798110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1799103285Sikob				goto err;
1800103285Sikob			}
1801110269Ssimokawa			xfer = fw_xfer_alloc(M_FWXFER);
1802103285Sikob			if(xfer == NULL){
1803103285Sikob				return;
1804103285Sikob			}
1805103285Sikob			xfer->spd = spd;
1806110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1807103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1808103285Sikob			switch(fp->mode.common.tcode){
1809103285Sikob			case FWTCODE_WREQQ:
1810103285Sikob			case FWTCODE_WREQB:
1811103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1812103285Sikob				xfer->send.len = 12;
1813103285Sikob				break;
1814103285Sikob			case FWTCODE_RREQQ:
1815103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1816103285Sikob				xfer->send.len = 16;
1817103285Sikob				break;
1818103285Sikob			case FWTCODE_RREQB:
1819103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1820103285Sikob				xfer->send.len = 16;
1821103285Sikob				break;
1822103285Sikob			case FWTCODE_LREQ:
1823103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1824103285Sikob				xfer->send.len = 16;
1825103285Sikob				break;
1826103285Sikob			}
1827103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1828103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1829103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1830103285Sikob			resfp->mode.rresb.rtcode = 7;
1831103285Sikob			resfp->mode.rresb.extcode = 0;
1832103285Sikob			resfp->mode.rresb.len = 0;
1833103285Sikob/*
1834103285Sikob			xfer->act.hand = fw_asy_callback;
1835103285Sikob*/
1836103285Sikob			xfer->act.hand = fw_xfer_free;
1837103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1838103285Sikob				fw_xfer_free( xfer);
1839103285Sikob				return;
1840103285Sikob			}
1841103285Sikob			goto err;
1842103285Sikob		}
1843103285Sikob		switch(bind->xfer->act_type){
1844103285Sikob		case FWACT_XFER:
1845110269Ssimokawa			xfer = fw_xfer_alloc(M_FWXFER);
1846103285Sikob			if(xfer == NULL) goto err;
1847103285Sikob			xfer->fc = bind->xfer->fc;
1848103285Sikob			xfer->sc = bind->xfer->sc;
1849103285Sikob			xfer->recv.buf = buf;
1850103285Sikob			xfer->recv.len = len;
1851103285Sikob			xfer->recv.off = off;
1852103285Sikob			xfer->spd = spd;
1853103285Sikob			xfer->act.hand = bind->xfer->act.hand;
1854103285Sikob			if (fc->status != FWBUSRESET)
1855103285Sikob				xfer->act.hand(xfer);
1856103285Sikob			else
1857103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1858103285Sikob			return;
1859103285Sikob			break;
1860103285Sikob		case FWACT_CH:
1861103285Sikob			if(fc->ir[bind->xfer->sub]->queued >=
1862103285Sikob				fc->ir[bind->xfer->sub]->maxq){
1863108655Ssimokawa				device_printf(fc->bdev,
1864108655Ssimokawa					"Discard a packet %x %d\n",
1865103285Sikob					bind->xfer->sub,
1866103285Sikob					fc->ir[bind->xfer->sub]->queued);
1867103285Sikob				goto err;
1868103285Sikob			}
1869110269Ssimokawa			xfer = fw_xfer_alloc(M_FWXFER);
1870103285Sikob			if(xfer == NULL) goto err;
1871103285Sikob			xfer->recv.buf = buf;
1872103285Sikob			xfer->recv.len = len;
1873103285Sikob			xfer->recv.off = off;
1874103285Sikob			xfer->spd = spd;
1875103285Sikob			s = splfw();
1876103285Sikob			fc->ir[bind->xfer->sub]->queued++;
1877103285Sikob			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1878103285Sikob			splx(s);
1879103285Sikob
1880103285Sikob			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1881103285Sikob
1882103285Sikob			return;
1883103285Sikob			break;
1884103285Sikob		default:
1885103285Sikob			goto err;
1886103285Sikob			break;
1887103285Sikob		}
1888103285Sikob		break;
1889103285Sikob	case FWTCODE_STREAM:
1890103285Sikob	{
1891103285Sikob		struct fw_xferq *xferq;
1892103285Sikob
1893103285Sikob		xferq = fc->ir[sub];
1894103285Sikob#if 0
1895103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1896103285Sikob			sub, len, off, spd);
1897103285Sikob#endif
1898103285Sikob		if(xferq->queued >= xferq->maxq) {
1899103285Sikob			printf("receive queue is full\n");
1900103285Sikob			goto err;
1901103285Sikob		}
1902110269Ssimokawa		xfer = fw_xfer_alloc(M_FWXFER);
1903103285Sikob		if(xfer == NULL) goto err;
1904103285Sikob		xfer->recv.buf = buf;
1905103285Sikob		xfer->recv.len = len;
1906103285Sikob		xfer->recv.off = off;
1907103285Sikob		xfer->spd = spd;
1908103285Sikob		s = splfw();
1909103285Sikob		xferq->queued++;
1910103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1911103285Sikob		splx(s);
1912103285Sikob		sc = device_get_softc(fc->bdev);
1913103285Sikob#if __FreeBSD_version >= 500000
1914103285Sikob		if (SEL_WAITING(&xferq->rsel))
1915103285Sikob#else
1916103285Sikob		if (&xferq->rsel.si_pid != 0)
1917103285Sikob#endif
1918103285Sikob			selwakeup(&xferq->rsel);
1919103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1920103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1921103285Sikob			wakeup((caddr_t)xferq);
1922103285Sikob		}
1923103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1924103285Sikob			xferq->hand(xferq);
1925103285Sikob		}
1926103285Sikob		return;
1927103285Sikob		break;
1928103285Sikob	}
1929103285Sikob	default:
1930103285Sikob		printf("fw_rcv: unknow tcode\n");
1931103285Sikob		break;
1932103285Sikob	}
1933103285Sikoberr:
1934110195Ssimokawa	free(buf, M_FW);
1935103285Sikob}
1936106815Ssimokawa
1937103285Sikob/*
1938103285Sikob * Post process for Bus Manager election process.
1939103285Sikob */
1940103285Sikobstatic void
1941103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1942103285Sikob{
1943109422Ssimokawa	struct fw_pkt *rfp;
1944103285Sikob	struct firewire_comm *fc;
1945109422Ssimokawa	int bmr;
1946103285Sikob
1947109422Ssimokawa	if (xfer == NULL)
1948109422Ssimokawa		return;
1949103285Sikob	fc = xfer->fc;
1950109422Ssimokawa	if (xfer->resp != 0)
1951103285Sikob		goto error;
1952109422Ssimokawa	if (xfer->send.buf == NULL)
1953103285Sikob		goto error;
1954109422Ssimokawa	if (xfer->recv.buf == NULL)
1955103285Sikob		goto error;
1956103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1957109422Ssimokawa	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1958109422Ssimokawa		goto error;
1959109422Ssimokawa
1960109422Ssimokawa	bmr = ntohl(rfp->mode.lres.payload[0]);
1961109422Ssimokawa	if (bmr == 0x3f)
1962109422Ssimokawa		bmr = fc->nodeid;
1963109422Ssimokawa
1964109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1965108655Ssimokawa	device_printf(fc->bdev, "new bus manager %d ",
1966108655Ssimokawa		CSRARC(fc, BUS_MGR_ID));
1967109422Ssimokawa	if(bmr == fc->nodeid){
1968103285Sikob		printf("(me)\n");
1969110072Ssimokawa		fw_bmr(fc);
1970103285Sikob	}else{
1971103285Sikob		printf("\n");
1972103285Sikob	}
1973103285Sikoberror:
1974103285Sikob	fw_xfer_free(xfer);
1975103285Sikob}
1976106815Ssimokawa
1977103285Sikob/*
1978103285Sikob * To candidate Bus Manager election process.
1979103285Sikob */
1980110270Ssimokawastatic void
1981106815Ssimokawafw_try_bmr(void *arg)
1982103285Sikob{
1983103285Sikob	struct fw_xfer *xfer;
1984103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
1985103285Sikob	struct fw_pkt *fp;
1986103285Sikob	int err = 0;
1987103285Sikob
1988110269Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1989103285Sikob	if(xfer == NULL){
1990103285Sikob		return;
1991103285Sikob	}
1992103285Sikob	xfer->send.len = 24;
1993103285Sikob	xfer->spd = 0;
1994110195Ssimokawa	xfer->send.buf = malloc(24, M_FW, M_NOWAIT);
1995103285Sikob	if(xfer->send.buf == NULL){
1996103285Sikob		fw_xfer_free( xfer);
1997103285Sikob		return;
1998103285Sikob	}
1999103285Sikob
2000103285Sikob	fc->status = FWBUSMGRELECT;
2001103285Sikob
2002103285Sikob	xfer->send.off = 0;
2003103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2004103285Sikob	fp->mode.lreq.dest_hi = htons(0xffff);
2005103285Sikob	fp->mode.lreq.tlrt = 0;
2006103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2007103285Sikob	fp->mode.lreq.pri = 0;
2008103285Sikob	fp->mode.lreq.src = 0;
2009103285Sikob	fp->mode.lreq.len = htons(8);
2010103285Sikob	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
2011103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
2012103285Sikob	fp->mode.lreq.dst = htons(xfer->dst);
2013103285Sikob	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
2014109422Ssimokawa	fp->mode.lreq.payload[0] = htonl(0x3f);
2015109422Ssimokawa	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
2016103285Sikob	xfer->act_type = FWACT_XFER;
2017103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2018103285Sikob
2019103285Sikob	err = fw_asyreq(fc, -1, xfer);
2020103285Sikob	if(err){
2021103285Sikob		fw_xfer_free( xfer);
2022103285Sikob		return;
2023103285Sikob	}
2024103285Sikob	return;
2025103285Sikob}
2026106543Ssimokawa
2027106543Ssimokawa#ifdef FW_VMACCESS
2028103285Sikob/*
2029103285Sikob * Software implementation for physical memory block access.
2030103285Sikob * XXX:Too slow, usef for debug purpose only.
2031103285Sikob */
2032106815Ssimokawastatic void
2033106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2034103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2035103285Sikob	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
2036103285Sikob
2037103285Sikob	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
2038103285Sikob			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2039103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2040103285Sikob	if(xfer->resp != 0){
2041103285Sikob		fw_xfer_free( xfer);
2042103285Sikob		return;
2043103285Sikob	}
2044103285Sikob	if(xfer->recv.buf == NULL){
2045103285Sikob		fw_xfer_free( xfer);
2046103285Sikob		return;
2047103285Sikob	}
2048103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2049103285Sikob	switch(rfp->mode.hdr.tcode){
2050103285Sikob		/* XXX need fix for 64bit arch */
2051103285Sikob		case FWTCODE_WREQB:
2052110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2053103285Sikob			xfer->send.len = 12;
2054103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2055103285Sikob			bcopy(rfp->mode.wreqb.payload,
2056103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2057103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2058103285Sikob			sfp->mode.wres.rtcode = 0;
2059103285Sikob			break;
2060103285Sikob		case FWTCODE_WREQQ:
2061110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2062103285Sikob			xfer->send.len = 12;
2063103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2064103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2065103285Sikob			sfp->mode.wres.rtcode = 0;
2066103285Sikob			break;
2067103285Sikob		case FWTCODE_RREQB:
2068110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2069103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2070103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2071103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2072103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2073103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2074103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2075103285Sikob			sfp->mode.rresb.rtcode = 0;
2076103285Sikob			sfp->mode.rresb.extcode = 0;
2077103285Sikob			break;
2078103285Sikob		case FWTCODE_RREQQ:
2079110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2080103285Sikob			xfer->send.len = 16;
2081103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2082103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2083103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2084103285Sikob			sfp->mode.rresb.rtcode = 0;
2085103285Sikob			break;
2086103285Sikob		default:
2087103285Sikob			fw_xfer_free( xfer);
2088103285Sikob			return;
2089103285Sikob	}
2090103285Sikob	xfer->send.off = 0;
2091103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2092103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2093103285Sikob	xfer->act.hand = fw_xfer_free;
2094103285Sikob	xfer->retry_req = fw_asybusy;
2095103285Sikob
2096103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2097103285Sikob	sfp->mode.hdr.pri = 0;
2098103285Sikob
2099103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2100103285Sikob/**/
2101103285Sikob	return;
2102103285Sikob}
2103106543Ssimokawa#endif
2104106543Ssimokawa
2105103285Sikob/*
2106103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2107103285Sikob */
2108106815Ssimokawau_int16_t
2109106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2110103285Sikob	u_int32_t i, sum, crc = 0;
2111103285Sikob	int shift;
2112103285Sikob	len = (len + 3) & ~3;
2113103285Sikob	for(i = 0 ; i < len ; i+= 4){
2114103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2115103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2116103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2117103285Sikob		}
2118103285Sikob		crc &= 0xffff;
2119103285Sikob	}
2120103285Sikob	return((u_int16_t) crc);
2121103285Sikob}
2122106815Ssimokawa
2123110270Ssimokawastatic int
2124110072Ssimokawafw_bmr(struct firewire_comm *fc)
2125110072Ssimokawa{
2126110072Ssimokawa	struct fw_device fwdev;
2127110072Ssimokawa	int cmstr;
2128110072Ssimokawa
2129110072Ssimokawa	/* XXX Assume that the current root node is cycle master capable */
2130110072Ssimokawa	cmstr = fc->max_node;
2131110072Ssimokawa	/* If I am the bus manager, optimize gapcount */
2132110072Ssimokawa	if(fc->max_hop <= MAX_GAPHOP ){
2133110072Ssimokawa		fw_phy_config(fc, (fc->max_node > 0)?cmstr:-1,
2134110072Ssimokawa						gap_cnt[fc->max_hop]);
2135110072Ssimokawa	}
2136110072Ssimokawa	/* If we are the cycle master, nothing to do */
2137110072Ssimokawa	if (cmstr == fc->nodeid)
2138110072Ssimokawa		return 0;
2139110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2140110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2141110072Ssimokawa	fwdev.fc = fc;
2142110072Ssimokawa	fwdev.dst = cmstr;
2143110072Ssimokawa	fwdev.speed = 0;
2144110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2145110072Ssimokawa	fwdev.status = FWDEVINIT;
2146110072Ssimokawa	/* Set cmstr bit on the cycle master */
2147110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2148110072Ssimokawa		0xffff, 0xf0000000 | STATE_SET, 1 << 16,
2149110072Ssimokawa		fw_asy_callback_free);
2150110072Ssimokawa
2151110072Ssimokawa	return 0;
2152110072Ssimokawa}
2153110072Ssimokawa
2154103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2155103285SikobMODULE_VERSION(firewire, 1);
2156