firewire.c revision 113584
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 113584 2003-04-17 03:38:03Z simokawa $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/types.h>
41103285Sikob#include <sys/mbuf.h>
42103285Sikob#include <sys/socket.h>
43103285Sikob#include <sys/socketvar.h>
44103285Sikob
45103285Sikob#include <sys/kernel.h>
46103285Sikob#include <sys/malloc.h>
47103285Sikob#include <sys/conf.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 */
54113584Ssimokawa#include <machine/bus.h>
55103285Sikob
56103285Sikob#include <dev/firewire/firewire.h>
57103285Sikob#include <dev/firewire/firewirereg.h>
58110072Ssimokawa#include <dev/firewire/fwmem.h>
59103285Sikob#include <dev/firewire/iec13213.h>
60103285Sikob#include <dev/firewire/iec68113.h>
61103285Sikob
62109736Ssimokawaint firewire_debug=0, try_bmr=1;
63103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
64108281Ssimokawa	"FireWire driver debug flag");
65109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
66109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
67109736Ssimokawa	"Try to be a bus manager");
68103285Sikob
69110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
70110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
71110195Ssimokawa
72103285Sikob#define FW_MAXASYRTY 4
73103285Sikob#define FW_MAXDEVRCNT 4
74103285Sikob
75103285Sikobdevclass_t firewire_devclass;
76103285Sikob
77103285Sikobstatic int firewire_match      __P((device_t));
78103285Sikobstatic int firewire_attach      __P((device_t));
79103285Sikobstatic int firewire_detach      __P((device_t));
80103285Sikob#if 0
81103285Sikobstatic int firewire_shutdown    __P((device_t));
82103285Sikob#endif
83103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
84103285Sikobstatic void fw_try_bmr __P((void *));
85103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
86103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
87103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
88103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
89103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
90103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
91103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
92106543Ssimokawa#ifdef FW_VMACCESS
93103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
94106543Ssimokawa#endif
95103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
96105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
97110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *));
98103285Sikob
99103285Sikobstatic device_method_t firewire_methods[] = {
100103285Sikob	/* Device interface */
101103285Sikob	DEVMETHOD(device_probe,		firewire_match),
102103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
103103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
104108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
105108642Ssimokawa	DEVMETHOD(device_resume,	bus_generic_resume),
106103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
107103285Sikob
108103285Sikob	/* Bus interface */
109103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
110103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
111103285Sikob
112103285Sikob	{ 0, 0 }
113103285Sikob};
114103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
115103285Sikob
116103285Sikob#define MAX_GAPHOP  16
117103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
118103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
119106813Ssimokawa
120106813Ssimokawaextern struct cdevsw firewire_cdevsw;
121106813Ssimokawa
122103285Sikobstatic driver_t firewire_driver = {
123103285Sikob	"firewire",
124103285Sikob	firewire_methods,
125103285Sikob	sizeof(struct firewire_softc),
126103285Sikob};
127103285Sikob
128103285Sikob/*
129110072Ssimokawa * Lookup fwdev by node id.
130103285Sikob */
131106810Ssimokawastruct fw_device *
132110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
133103285Sikob{
134103285Sikob	struct fw_device *fwdev;
135110072Ssimokawa	int s;
136110072Ssimokawa
137110072Ssimokawa	s = splfw();
138110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
139110072Ssimokawa		if (fwdev->dst == dst)
140103285Sikob			break;
141110072Ssimokawa	splx(s);
142110072Ssimokawa
143106810Ssimokawa	if(fwdev == NULL) return NULL;
144106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
145106810Ssimokawa	return fwdev;
146103285Sikob}
147106813Ssimokawa
148103285Sikob/*
149110072Ssimokawa * Lookup fwdev by EUI64.
150110072Ssimokawa */
151110072Ssimokawastruct fw_device *
152110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
153110072Ssimokawa{
154110072Ssimokawa	struct fw_device *fwdev;
155110072Ssimokawa	int s;
156110072Ssimokawa
157110072Ssimokawa	s = splfw();
158110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
159110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
160110072Ssimokawa			break;
161110072Ssimokawa	splx(s);
162110072Ssimokawa
163110072Ssimokawa	if(fwdev == NULL) return NULL;
164110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
165110072Ssimokawa	return fwdev;
166110072Ssimokawa}
167110072Ssimokawa
168110072Ssimokawa/*
169103285Sikob * Async. request procedure for userland application.
170103285Sikob */
171103285Sikobint
172103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
173103285Sikob{
174103285Sikob	int err = 0;
175103285Sikob	struct fw_xferq *xferq;
176103285Sikob	int tl = 0, len;
177103285Sikob	struct fw_pkt *fp;
178103285Sikob	int tcode;
179103285Sikob	struct tcode_info *info;
180103285Sikob
181103285Sikob	if(xfer == NULL) return EINVAL;
182108701Ssimokawa	if(xfer->send.len > MAXREC(fc->maxrec)){
183103285Sikob		printf("send.len > maxrec\n");
184103285Sikob		return EINVAL;
185103285Sikob	}
186103285Sikob	if(xfer->act.hand == NULL){
187103285Sikob		printf("act.hand == NULL\n");
188103285Sikob		return EINVAL;
189103285Sikob	}
190103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
191103285Sikob
192103285Sikob	tcode = fp->mode.common.tcode & 0xf;
193103285Sikob	info = &fc->tcode[tcode];
194103285Sikob	if (info->flag == 0) {
195103285Sikob		printf("invalid tcode=%d\n", tcode);
196103285Sikob		return EINVAL;
197103285Sikob	}
198103285Sikob	if (info->flag & FWTI_REQ)
199103285Sikob		xferq = fc->atq;
200103285Sikob	else
201103285Sikob		xferq = fc->ats;
202103285Sikob	len = info->hdr_len;
203103285Sikob	if (info->flag & FWTI_BLOCK_STR)
204113584Ssimokawa		len += fp->mode.stream.len;
205103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
206113584Ssimokawa		len += fp->mode.rresb.len;
207103285Sikob	if( len >  xfer->send.len ){
208103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
209103285Sikob				len, xfer->send.len, tcode);
210103285Sikob		return EINVAL;
211103285Sikob	}
212103285Sikob	xfer->send.len = len;
213106790Ssimokawa
214103285Sikob	if(xferq->start == NULL){
215103285Sikob		printf("xferq->start == NULL\n");
216103285Sikob		return EINVAL;
217103285Sikob	}
218103285Sikob	if(!(xferq->queued < xferq->maxq)){
219108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
220108655Ssimokawa			xferq->queued);
221103285Sikob		return EINVAL;
222103285Sikob	}
223103285Sikob
224103285Sikob
225103285Sikob	if (info->flag & FWTI_TLABEL) {
226103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
227103285Sikob			return EIO;
228103285Sikob		fp->mode.hdr.tlrt = tl << 2;
229103285Sikob	}
230103285Sikob
231103285Sikob	xfer->tl = tl;
232103285Sikob	xfer->resp = 0;
233103285Sikob	xfer->fc = fc;
234103285Sikob	xfer->q = xferq;
235103285Sikob	xfer->retry_req = fw_asybusy;
236103285Sikob
237103285Sikob	fw_asystart(xfer);
238103285Sikob	return err;
239103285Sikob}
240103285Sikob/*
241103285Sikob * Wakeup blocked process.
242103285Sikob */
243103285Sikobvoid
244103285Sikobfw_asy_callback(struct fw_xfer *xfer){
245103285Sikob	wakeup(xfer);
246103285Sikob	return;
247103285Sikob}
248103285Sikob/*
249103285Sikob * Postpone to later retry.
250103285Sikob */
251103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
252103285Sikob	printf("fw_asybusy\n");
253103285Sikob/*
254103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
255103285Sikob*/
256103285Sikob	DELAY(20000);
257103285Sikob	fw_asystart(xfer);
258103285Sikob	return;
259103285Sikob}
260103285Sikob
261103285Sikob/*
262103285Sikob * Async. request with given xfer structure.
263103285Sikob */
264106790Ssimokawastatic void
265106790Ssimokawafw_asystart(struct fw_xfer *xfer)
266106790Ssimokawa{
267103285Sikob	struct firewire_comm *fc = xfer->fc;
268103285Sikob	int s;
269103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
270113584Ssimokawa		device_printf(fc->bdev, "max_asyretry exceeded\n");
271103285Sikob		xfer->resp = EBUSY;
272103285Sikob		xfer->state = FWXF_BUSY;
273103285Sikob		xfer->act.hand(xfer);
274103285Sikob		return;
275103285Sikob	}
276103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
277103285Sikob	if (fc->status < FWBUSEXPLORE) {
278103285Sikob		xfer->resp = EAGAIN;
279103285Sikob		xfer->state = FWXF_BUSY;
280103285Sikob		if (xfer->act.hand != NULL)
281103285Sikob			xfer->act.hand(xfer);
282103285Sikob		return;
283103285Sikob	}
284103285Sikob#endif
285103285Sikob	s = splfw();
286103285Sikob	xfer->state = FWXF_INQ;
287103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
288103285Sikob	xfer->q->queued ++;
289103285Sikob	splx(s);
290103285Sikob	/* XXX just queue for mbuf */
291103285Sikob	if (xfer->mbuf == NULL)
292103285Sikob		xfer->q->start(fc);
293103285Sikob	return;
294103285Sikob}
295106790Ssimokawa
296103285Sikobstatic int
297103285Sikobfirewire_match( device_t dev )
298103285Sikob{
299108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
300103285Sikob	return -140;
301103285Sikob}
302106790Ssimokawa
303110577Ssimokawastatic void
304110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
305110577Ssimokawa{
306110577Ssimokawa	struct fw_xfer *xfer;
307110577Ssimokawa	struct tlabel *tl;
308110577Ssimokawa	struct timeval tv;
309110577Ssimokawa	struct timeval split_timeout;
310111040Ssimokawa	int i, s;
311110577Ssimokawa
312110891Ssimokawa	split_timeout.tv_sec = 6;
313110577Ssimokawa	split_timeout.tv_usec = 0;
314110577Ssimokawa
315110577Ssimokawa	microtime(&tv);
316110577Ssimokawa	timevalsub(&tv, &split_timeout);
317110577Ssimokawa
318111040Ssimokawa	s = splfw();
319110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
320110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
321110577Ssimokawa			xfer = tl->xfer;
322110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
323110577Ssimokawa				/* the rests are newer than this */
324110577Ssimokawa				break;
325110577Ssimokawa			device_printf(fc->bdev,
326110891Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x\n",
327110577Ssimokawa				xfer->dst, i);
328110577Ssimokawa			xfer->resp = ETIMEDOUT;
329110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
330113584Ssimokawa			fw_xfer_done(xfer);
331110577Ssimokawa		}
332110577Ssimokawa	}
333111040Ssimokawa	splx(s);
334110577Ssimokawa}
335110577Ssimokawa
336110577Ssimokawastatic void
337110577Ssimokawafirewire_watchdog(void *arg)
338110577Ssimokawa{
339110577Ssimokawa	struct firewire_comm *fc;
340110577Ssimokawa
341110577Ssimokawa	fc = (struct firewire_comm *)arg;
342110577Ssimokawa	firewire_xfer_timeout(fc);
343110577Ssimokawa	fc->timeout(fc);
344110577Ssimokawa	callout_reset(&fc->timeout_callout, hz,
345110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
346110577Ssimokawa}
347110577Ssimokawa
348103285Sikob/*
349103285Sikob * The attach routine.
350103285Sikob */
351103285Sikobstatic int
352103285Sikobfirewire_attach( device_t dev )
353103285Sikob{
354103285Sikob	int i, unitmask, mn;
355103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
356103285Sikob	device_t pa = device_get_parent(dev);
357103285Sikob	struct firewire_comm *fc;
358103285Sikob	dev_t d;
359103285Sikob
360103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
361103285Sikob	sc->fc = fc;
362111074Ssimokawa	fc->status = -1;
363103285Sikob
364103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
365103285Sikob
366103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
367103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
368103285Sikob		mn = unitmask | i;
369103285Sikob		/* XXX device name should be improved */
370103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
371108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
372103285Sikob			"fw%x", mn);
373103285Sikob#if __FreeBSD_version >= 500000
374103285Sikob		if (i == 0)
375103285Sikob			sc->dev = d;
376103285Sikob		else
377103285Sikob			dev_depends(sc->dev, d);
378103285Sikob#else
379103285Sikob		sc->dev[i] = d;
380103285Sikob#endif
381103285Sikob	}
382103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
383108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
384103285Sikob			"fwmem%d", device_get_unit(dev));
385103285Sikob#if __FreeBSD_version >= 500000
386103285Sikob	dev_depends(sc->dev, d);
387103285Sikob#else
388103285Sikob	sc->dev[i] = d;
389103285Sikob#endif
390110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
391110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
392110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
393110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
394108853Ssimokawa
395110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
396110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
397110193Ssimokawa
398103285Sikob	/* Locate our children */
399103285Sikob	bus_generic_probe(dev);
400103285Sikob
401103285Sikob	/* launch attachement of the added children */
402103285Sikob	bus_generic_attach(dev);
403103285Sikob
404103285Sikob	/* bus_reset */
405103285Sikob	fc->ibr(fc);
406103285Sikob
407103285Sikob	return 0;
408103285Sikob}
409103285Sikob
410103285Sikob/*
411103285Sikob * Attach it as child.
412103285Sikob */
413103285Sikobstatic device_t
414103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
415103285Sikob{
416103285Sikob        device_t child;
417103285Sikob	struct firewire_softc *sc;
418103285Sikob
419103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
420103285Sikob	child = device_add_child(dev, name, unit);
421103285Sikob	if (child) {
422103285Sikob		device_set_ivars(child, sc->fc);
423103285Sikob		device_probe_and_attach(child);
424103285Sikob	}
425103285Sikob
426103285Sikob	return child;
427103285Sikob}
428106790Ssimokawa
429103285Sikob/*
430103285Sikob * Dettach it.
431103285Sikob */
432103285Sikobstatic int
433103285Sikobfirewire_detach( device_t dev )
434103285Sikob{
435103285Sikob	struct firewire_softc *sc;
436111074Ssimokawa	struct csrdir *csrd, *next;
437111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
438103285Sikob
439103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
440106790Ssimokawa
441111078Ssimokawa	bus_generic_detach(dev);
442111078Ssimokawa
443111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
444111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
445111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
446111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
447111078Ssimokawa
448103285Sikob#if __FreeBSD_version >= 500000
449103285Sikob	destroy_dev(sc->dev);
450103285Sikob#else
451103285Sikob	{
452103285Sikob		int j;
453103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
454103285Sikob			destroy_dev(sc->dev[j]);
455103285Sikob	}
456103285Sikob#endif
457103285Sikob	/* XXX xfree_free and untimeout on all xfers */
458111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
459111078Ssimokawa							fwdev = fwdev_next) {
460111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
461111078Ssimokawa		free(fwdev, M_FW);
462111078Ssimokawa	}
463111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
464111074Ssimokawa		next = SLIST_NEXT(csrd, link);
465111074Ssimokawa		free(csrd, M_FW);
466111074Ssimokawa	}
467110195Ssimokawa	free(sc->fc->topology_map, M_FW);
468110195Ssimokawa	free(sc->fc->speed_map, M_FW);
469103285Sikob	return(0);
470103285Sikob}
471103285Sikob#if 0
472103285Sikobstatic int
473103285Sikobfirewire_shutdown( device_t dev )
474103285Sikob{
475103285Sikob	return 0;
476103285Sikob}
477103285Sikob#endif
478106790Ssimokawa
479110577Ssimokawa
480110577Ssimokawastatic void
481110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
482110577Ssimokawa{
483110577Ssimokawa	struct fw_xfer *xfer;
484110577Ssimokawa
485110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
486110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
487111942Ssimokawa		xferq->queued --;
488110577Ssimokawa		xfer->resp = EAGAIN;
489113584Ssimokawa		fw_xfer_done(xfer);
490110577Ssimokawa	}
491110577Ssimokawa}
492110577Ssimokawa
493110798Ssimokawavoid
494110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
495110798Ssimokawa{
496110798Ssimokawa	int i;
497110798Ssimokawa
498110798Ssimokawa	fw_xferq_drain(fc->atq);
499110798Ssimokawa	fw_xferq_drain(fc->ats);
500110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
501110798Ssimokawa		fw_xferq_drain(fc->it[i]);
502110798Ssimokawa}
503110798Ssimokawa
504103285Sikob/*
505106790Ssimokawa * Called after bus reset.
506103285Sikob */
507106790Ssimokawavoid
508106790Ssimokawafw_busreset(struct firewire_comm *fc)
509103285Sikob{
510113584Ssimokawa	struct firewire_dev_comm *fdc;
511113584Ssimokawa	device_t *devlistp;
512113584Ssimokawa	int devcnt;
513103285Sikob	int i;
514103285Sikob
515103285Sikob	switch(fc->status){
516103285Sikob	case FWBUSMGRELECT:
517110193Ssimokawa		callout_stop(&fc->bmr_callout);
518103285Sikob		break;
519103285Sikob	default:
520103285Sikob		break;
521103285Sikob	}
522103285Sikob	fc->status = FWBUSRESET;
523103285Sikob	CSRARC(fc, STATE_CLEAR)
524103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
525103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
526103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
527103285Sikob
528103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
529103285Sikob	fc->irm = -1;
530103285Sikob
531103285Sikob	fc->max_node = -1;
532103285Sikob
533103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
534103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
535103285Sikob	}
536103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
537103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
538103285Sikob	CSRARC(fc, RESET_START) = 0;
539103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
540103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
541103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
542103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
543103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
544103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
545103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
546103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
547103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
548103285Sikob
549103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
550103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
551103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
552103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
553103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
554103285Sikob
555103285Sikob/* DV depend CSRs see blue book */
556103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
557103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
558103285Sikob
559103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
560103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
561113584Ssimokawa
562113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
563113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
564113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
565113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
566113584Ssimokawa				if (fdc->post_busreset != NULL)
567113584Ssimokawa					fdc->post_busreset(fdc);
568113584Ssimokawa			}
569113584Ssimokawa		free(devlistp, M_TEMP);
570113584Ssimokawa	}
571103285Sikob}
572106790Ssimokawa
573103285Sikob/* Call once after reboot */
574106790Ssimokawavoid fw_init(struct firewire_comm *fc)
575103285Sikob{
576103285Sikob	int i;
577106543Ssimokawa	struct csrdir *csrd;
578106543Ssimokawa#ifdef FW_VMACCESS
579103285Sikob	struct fw_xfer *xfer;
580103285Sikob	struct fw_bind *fwb;
581106543Ssimokawa#endif
582103285Sikob
583103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
584103285Sikob
585103285Sikob	fc->arq->queued = 0;
586103285Sikob	fc->ars->queued = 0;
587103285Sikob	fc->atq->queued = 0;
588103285Sikob	fc->ats->queued = 0;
589103285Sikob
590103285Sikob	fc->arq->buf = NULL;
591103285Sikob	fc->ars->buf = NULL;
592103285Sikob	fc->atq->buf = NULL;
593103285Sikob	fc->ats->buf = NULL;
594103285Sikob
595113584Ssimokawa	fc->arq->flag = 0;
596113584Ssimokawa	fc->ars->flag = 0;
597113584Ssimokawa	fc->atq->flag = 0;
598113584Ssimokawa	fc->ats->flag = 0;
599103285Sikob
600103285Sikob	STAILQ_INIT(&fc->atq->q);
601103285Sikob	STAILQ_INIT(&fc->ats->q);
602103285Sikob
603103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
604103285Sikob		fc->it[i]->queued = 0;
605103285Sikob		fc->ir[i]->queued = 0;
606103285Sikob
607103285Sikob		fc->it[i]->start = NULL;
608103285Sikob		fc->ir[i]->start = NULL;
609103285Sikob
610103285Sikob		fc->it[i]->buf = NULL;
611103285Sikob		fc->ir[i]->buf = NULL;
612103285Sikob
613103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
614103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
615103285Sikob
616103285Sikob		STAILQ_INIT(&fc->it[i]->q);
617103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
618103285Sikob
619103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
620103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
621103285Sikob	}
622103285Sikob
623103285Sikob	fc->arq->maxq = FWMAXQUEUE;
624103285Sikob	fc->ars->maxq = FWMAXQUEUE;
625103285Sikob	fc->atq->maxq = FWMAXQUEUE;
626103285Sikob	fc->ats->maxq = FWMAXQUEUE;
627103285Sikob
628103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
629103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
630103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
631103285Sikob	}
632103285Sikob/* Initialize csr registers */
633103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
634103285Sikob				sizeof(struct fw_topology_map),
635110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
636103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
637103285Sikob				sizeof(struct fw_speed_map),
638110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
639103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
640103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
641103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
642103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
643103285Sikob
644110193Ssimokawa	STAILQ_INIT(&fc->devices);
645103285Sikob	STAILQ_INIT(&fc->pending);
646103285Sikob
647103285Sikob/* Initialize csr ROM work space */
648103285Sikob	SLIST_INIT(&fc->ongocsr);
649103285Sikob	SLIST_INIT(&fc->csrfree);
650103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
651110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
652103285Sikob		if(csrd == NULL) break;
653103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
654103285Sikob	}
655103285Sikob
656103285Sikob/* Initialize Async handlers */
657103285Sikob	STAILQ_INIT(&fc->binds);
658103285Sikob	for( i = 0 ; i < 0x40 ; i++){
659103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
660103285Sikob	}
661103285Sikob
662103285Sikob/* DV depend CSRs see blue book */
663103285Sikob#if 0
664103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
665103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
666103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
667103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
668103285Sikob	}
669103285Sikob
670103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
671103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
672103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
673103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
674103285Sikob	}
675103285Sikob#endif
676103285Sikob
677103285Sikob
678106543Ssimokawa#ifdef FW_VMACCESS
679103285Sikob	xfer = fw_xfer_alloc();
680103285Sikob	if(xfer == NULL) return;
681103285Sikob
682110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
683103285Sikob	if(fwb == NULL){
684103285Sikob		fw_xfer_free(xfer);
685103285Sikob	}
686103285Sikob	xfer->act.hand = fw_vmaccess;
687103285Sikob	xfer->fc = fc;
688103285Sikob	xfer->sc = NULL;
689103285Sikob
690103285Sikob	fwb->start_hi = 0x2;
691103285Sikob	fwb->start_lo = 0;
692103285Sikob	fwb->addrlen = 0xffffffff;
693103285Sikob	fwb->xfer = xfer;
694103285Sikob	fw_bindadd(fc, fwb);
695106543Ssimokawa#endif
696103285Sikob}
697106790Ssimokawa
698103285Sikob/*
699103285Sikob * To lookup binded process from IEEE1394 address.
700103285Sikob */
701106813Ssimokawastruct fw_bind *
702106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
703103285Sikob{
704103285Sikob	struct fw_bind *tfw;
705103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
706103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
707113584Ssimokawa		if (tfw->act_type != FWACT_NULL &&
708103285Sikob			tfw->start_hi == dest_hi &&
709103285Sikob			tfw->start_lo <= dest_lo &&
710103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
711103285Sikob			return(tfw);
712103285Sikob		}
713103285Sikob	}
714103285Sikob	return(NULL);
715103285Sikob}
716106790Ssimokawa
717103285Sikob/*
718103285Sikob * To bind IEEE1394 address block to process.
719103285Sikob */
720106790Ssimokawaint
721106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
722103285Sikob{
723103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
724103285Sikob	int err = 0;
725103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
726103285Sikob	if(tfw == NULL){
727103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
728103285Sikob		goto out;
729103285Sikob	}
730103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
731103285Sikob		(tfw->start_hi == fwb->start_hi &&
732103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
733103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
734103285Sikob		goto out;
735103285Sikob	}
736103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
737103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
738103285Sikob		   (tfw->start_hi == fwb->start_hi &&
739103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
740103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
741103285Sikob			if(tfw2 == NULL)
742103285Sikob				break;
743103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
744103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
745103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
746103285Sikob				break;
747103285Sikob			}else{
748103285Sikob				err = EBUSY;
749103285Sikob				goto out;
750103285Sikob			}
751103285Sikob		}
752103285Sikob	}
753103285Sikob	if(tfw != NULL){
754103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
755103285Sikob	}else{
756103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
757103285Sikob	}
758103285Sikobout:
759113584Ssimokawa	if (!err && fwb->act_type == FWACT_CH)
760113584Ssimokawa		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
761103285Sikob	return err;
762103285Sikob}
763103285Sikob
764103285Sikob/*
765103285Sikob * To free IEEE1394 address block.
766103285Sikob */
767106790Ssimokawaint
768106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
769103285Sikob{
770103285Sikob	int s;
771113584Ssimokawa	struct fw_xfer *xfer, *next;
772103285Sikob
773103285Sikob	s = splfw();
774103285Sikob	/* shall we check the existance? */
775103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
776113584Ssimokawa	/* shall we do this? */
777113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
778113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
779113584Ssimokawa		fw_xfer_free(xfer);
780113584Ssimokawa	}
781113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
782113584Ssimokawa
783103285Sikob	splx(s);
784103285Sikob	return 0;
785103285Sikob}
786103285Sikob
787103285Sikob/*
788103285Sikob * To free transaction label.
789103285Sikob */
790106790Ssimokawastatic void
791106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
792103285Sikob{
793103285Sikob	struct tlabel *tl;
794103285Sikob	int s = splfw();
795103285Sikob
796103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
797103285Sikob		tl = STAILQ_NEXT(tl, link)){
798103285Sikob		if(tl->xfer == xfer){
799103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
800110195Ssimokawa			free(tl, M_FW);
801103285Sikob			splx(s);
802103285Sikob			return;
803103285Sikob		}
804103285Sikob	}
805103285Sikob	splx(s);
806103285Sikob	return;
807103285Sikob}
808106790Ssimokawa
809103285Sikob/*
810103285Sikob * To obtain XFER structure by transaction label.
811103285Sikob */
812106790Ssimokawastatic struct fw_xfer *
813106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
814103285Sikob{
815103285Sikob	struct fw_xfer *xfer;
816103285Sikob	struct tlabel *tl;
817103285Sikob	int s = splfw();
818103285Sikob
819103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
820103285Sikob		tl = STAILQ_NEXT(tl, link)){
821103285Sikob		if(tl->xfer->dst == node){
822103285Sikob			xfer = tl->xfer;
823103285Sikob			splx(s);
824110577Ssimokawa			if (firewire_debug > 2)
825110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
826103285Sikob			return(xfer);
827103285Sikob		}
828103285Sikob	}
829110577Ssimokawa	if (firewire_debug > 1)
830110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
831103285Sikob	splx(s);
832103285Sikob	return(NULL);
833103285Sikob}
834106790Ssimokawa
835103285Sikob/*
836103285Sikob * To allocate IEEE1394 XFER structure.
837103285Sikob */
838106790Ssimokawastruct fw_xfer *
839110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
840103285Sikob{
841103285Sikob	struct fw_xfer *xfer;
842106790Ssimokawa
843110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
844106790Ssimokawa	if (xfer == NULL)
845106790Ssimokawa		return xfer;
846106790Ssimokawa
847110577Ssimokawa	microtime(&xfer->tv);
848110269Ssimokawa	xfer->malloc = type;
849106790Ssimokawa
850103285Sikob	return xfer;
851103285Sikob}
852106790Ssimokawa
853113584Ssimokawastruct fw_xfer *
854113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
855113584Ssimokawa{
856113584Ssimokawa	struct fw_xfer *xfer;
857113584Ssimokawa
858113584Ssimokawa	xfer = fw_xfer_alloc(type);
859113584Ssimokawa	xfer->send.len = send_len;
860113584Ssimokawa	xfer->recv.len = recv_len;
861113584Ssimokawa	if (xfer == NULL)
862113584Ssimokawa		return(NULL);
863113584Ssimokawa	if (send_len) {
864113584Ssimokawa		xfer->send.buf = malloc(send_len, type, M_NOWAIT | M_ZERO);
865113584Ssimokawa		if (xfer->send.buf == NULL) {
866113584Ssimokawa			fw_xfer_free(xfer);
867113584Ssimokawa			return(NULL);
868113584Ssimokawa		}
869113584Ssimokawa	}
870113584Ssimokawa	if (recv_len) {
871113584Ssimokawa		xfer->recv.buf = malloc(recv_len, type, M_NOWAIT);
872113584Ssimokawa		if (xfer->recv.buf == NULL) {
873113584Ssimokawa			if (xfer->send.buf != NULL)
874113584Ssimokawa				free(xfer->send.buf, type);
875113584Ssimokawa			fw_xfer_free(xfer);
876113584Ssimokawa			return(NULL);
877113584Ssimokawa		}
878113584Ssimokawa	}
879113584Ssimokawa	return(xfer);
880113584Ssimokawa}
881113584Ssimokawa
882103285Sikob/*
883103285Sikob * IEEE1394 XFER post process.
884103285Sikob */
885103285Sikobvoid
886103285Sikobfw_xfer_done(struct fw_xfer *xfer)
887103285Sikob{
888103285Sikob	if (xfer->act.hand == NULL)
889103285Sikob		return;
890103285Sikob
891103285Sikob	if (xfer->fc->status != FWBUSRESET)
892103285Sikob		xfer->act.hand(xfer);
893103285Sikob	else {
894103285Sikob		printf("fw_xfer_done: pending\n");
895103285Sikob		if (xfer->fc != NULL)
896103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
897103285Sikob		else
898103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
899103285Sikob	}
900103285Sikob}
901103285Sikob
902106790Ssimokawavoid
903113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
904103285Sikob{
905103285Sikob	int s;
906113584Ssimokawa
907103285Sikob	if(xfer == NULL ) return;
908103285Sikob	if(xfer->state == FWXF_INQ){
909103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
910103285Sikob		s = splfw();
911103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
912103285Sikob		xfer->q->queued --;
913103285Sikob		splx(s);
914103285Sikob	}
915113584Ssimokawa	if (xfer->fc != NULL) {
916113584Ssimokawa#if 1 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
917113584Ssimokawa		if(xfer->state == FWXF_START)
918113584Ssimokawa			panic("fw_xfer_free FWXF_START\n");
919103285Sikob#endif
920113584Ssimokawa		fw_tl_free(xfer->fc, xfer);
921103285Sikob	}
922113584Ssimokawa	xfer->state = FWXF_INIT;
923113584Ssimokawa	xfer->resp = 0;
924113584Ssimokawa	xfer->retry = 0;
925113584Ssimokawa}
926113584Ssimokawa/*
927113584Ssimokawa * To free IEEE1394 XFER structure.
928113584Ssimokawa */
929113584Ssimokawavoid
930113584Ssimokawafw_xfer_free( struct fw_xfer* xfer)
931113584Ssimokawa{
932113584Ssimokawa	if(xfer == NULL ) return;
933113584Ssimokawa	fw_xfer_unload(xfer);
934103285Sikob	if(xfer->send.buf != NULL){
935113584Ssimokawa		free(xfer->send.buf, xfer->malloc);
936103285Sikob	}
937103285Sikob	if(xfer->recv.buf != NULL){
938113584Ssimokawa		free(xfer->recv.buf, xfer->malloc);
939103285Sikob	}
940110269Ssimokawa	free(xfer, xfer->malloc);
941103285Sikob}
942103285Sikob
943103285Sikobstatic void
944110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
945103285Sikob{
946103285Sikob#if 0
947110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
948103285Sikob				xfer->state, xfer->resp);
949103285Sikob#endif
950103285Sikob	fw_xfer_free(xfer);
951103285Sikob}
952103285Sikob
953103285Sikob/*
954103285Sikob * To configure PHY.
955103285Sikob */
956103285Sikobstatic void
957103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
958103285Sikob{
959103285Sikob	struct fw_xfer *xfer;
960103285Sikob	struct fw_pkt *fp;
961103285Sikob
962103285Sikob	fc->status = FWBUSPHYCONF;
963103285Sikob
964113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 12, 0);
965113584Ssimokawa	if (xfer == NULL)
966113584Ssimokawa		return;
967103285Sikob	xfer->fc = fc;
968103285Sikob	xfer->retry_req = fw_asybusy;
969110072Ssimokawa	xfer->act.hand = fw_asy_callback_free;
970103285Sikob
971103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
972103285Sikob	fp->mode.ld[1] = 0;
973103285Sikob	if (root_node >= 0)
974113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
975103285Sikob	if (gap_count >= 0)
976113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
977103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
978103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
979103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
980103285Sikob
981107653Ssimokawa	if (firewire_debug)
982107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
983103285Sikob						root_node, gap_count);
984103285Sikob	fw_asyreq(fc, -1, xfer);
985103285Sikob}
986103285Sikob
987103285Sikob#if 0
988103285Sikob/*
989103285Sikob * Dump self ID.
990103285Sikob */
991103285Sikobstatic void
992103285Sikobfw_print_sid(u_int32_t sid)
993103285Sikob{
994103285Sikob	union fw_self_id *s;
995103285Sikob	s = (union fw_self_id *) &sid;
996103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
997103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
998103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
999103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1000103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1001103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1002103285Sikob}
1003103285Sikob#endif
1004103285Sikob
1005103285Sikob/*
1006103285Sikob * To receive self ID.
1007103285Sikob */
1008113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len)
1009103285Sikob{
1010113584Ssimokawa	u_int32_t *p;
1011103285Sikob	union fw_self_id *self_id;
1012103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1013103285Sikob
1014103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1015103285Sikob	fc->status = FWBUSINIT;
1016103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1017103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1018103285Sikob	fc->status = FWBUSCYMELECT;
1019103285Sikob	fc->topology_map->crc_len = 2;
1020103285Sikob	fc->topology_map->generation ++;
1021103285Sikob	fc->topology_map->self_id_count = 0;
1022103285Sikob	fc->topology_map->node_count = 0;
1023103285Sikob	fc->speed_map->generation ++;
1024103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1025103285Sikob	self_id = &fc->topology_map->self_id[0];
1026103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1027103285Sikob		if (sid[1] != ~sid[0]) {
1028103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1029103285Sikob			sid += 2;
1030103285Sikob			continue;
1031103285Sikob		}
1032103285Sikob		*self_id = *((union fw_self_id *)sid);
1033103285Sikob		fc->topology_map->crc_len++;
1034103285Sikob		if(self_id->p0.sequel == 0){
1035103285Sikob			fc->topology_map->node_count ++;
1036103285Sikob			c_port = 0;
1037103285Sikob#if 0
1038103285Sikob			fw_print_sid(sid[0]);
1039103285Sikob#endif
1040103285Sikob			node = self_id->p0.phy_id;
1041103285Sikob			if(fc->max_node < node){
1042103285Sikob				fc->max_node = self_id->p0.phy_id;
1043103285Sikob			}
1044103285Sikob			/* XXX I'm not sure this is the right speed_map */
1045103285Sikob			fc->speed_map->speed[node][node]
1046103285Sikob					= self_id->p0.phy_speed;
1047103285Sikob			for (j = 0; j < node; j ++) {
1048103285Sikob				fc->speed_map->speed[j][node]
1049103285Sikob					= fc->speed_map->speed[node][j]
1050103285Sikob					= min(fc->speed_map->speed[j][j],
1051103285Sikob							self_id->p0.phy_speed);
1052103285Sikob			}
1053103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1054103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1055103285Sikob				fc->irm = self_id->p0.phy_id;
1056103285Sikob			}
1057103285Sikob			if(self_id->p0.port0 >= 0x2){
1058103285Sikob				c_port++;
1059103285Sikob			}
1060103285Sikob			if(self_id->p0.port1 >= 0x2){
1061103285Sikob				c_port++;
1062103285Sikob			}
1063103285Sikob			if(self_id->p0.port2 >= 0x2){
1064103285Sikob				c_port++;
1065103285Sikob			}
1066103285Sikob		}
1067103285Sikob		if(c_port > 2){
1068103285Sikob			i_branch += (c_port - 2);
1069103285Sikob		}
1070103285Sikob		sid += 2;
1071103285Sikob		self_id++;
1072103285Sikob		fc->topology_map->self_id_count ++;
1073103285Sikob	}
1074108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1075103285Sikob	/* CRC */
1076103285Sikob	fc->topology_map->crc = fw_crc16(
1077103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1078103285Sikob			fc->topology_map->crc_len * 4);
1079103285Sikob	fc->speed_map->crc = fw_crc16(
1080103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1081103285Sikob			fc->speed_map->crc_len * 4);
1082103285Sikob	/* byteswap and copy to CSR */
1083103285Sikob	p = (u_int32_t *)fc->topology_map;
1084103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1085103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1086103285Sikob	p = (u_int32_t *)fc->speed_map;
1087103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1088103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1089103285Sikob	/* don't byte-swap u_int8_t array */
1090103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1091103285Sikob
1092103285Sikob	fc->max_hop = fc->max_node - i_branch;
1093103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1094103285Sikob
1095103285Sikob	if(fc->irm == -1 ){
1096103285Sikob		printf(", Not found IRM capable node");
1097103285Sikob	}else{
1098103285Sikob		printf(", cable IRM = %d", fc->irm);
1099103285Sikob		if (fc->irm == fc->nodeid)
1100110016Ssimokawa			printf(" (me)");
1101103285Sikob	}
1102110016Ssimokawa	printf("\n");
1103103285Sikob
1104109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1105109736Ssimokawa		if (fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)) {
1106103285Sikob			fc->status = FWBUSMGRDONE;
1107103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1108109736Ssimokawa		} else {
1109103285Sikob			fc->status = FWBUSMGRELECT;
1110110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1111110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1112103285Sikob		}
1113109736Ssimokawa	} else {
1114103285Sikob		fc->status = FWBUSMGRDONE;
1115109736Ssimokawa#if 0
1116108655Ssimokawa		device_printf(fc->bdev, "BMR = %x\n",
1117108655Ssimokawa				CSRARC(fc, BUS_MGR_ID));
1118109736Ssimokawa#endif
1119103285Sikob	}
1120103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1121110072Ssimokawa		/* I am BMGR */
1122110072Ssimokawa		fw_bmr(fc);
1123103285Sikob	}
1124108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1125108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1126103285Sikob}
1127106790Ssimokawa
1128103285Sikob/*
1129103285Sikob * To probe devices on the IEEE1394 bus.
1130103285Sikob */
1131106790Ssimokawastatic void
1132106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1133103285Sikob{
1134103285Sikob	int s;
1135103285Sikob	struct fw_device *fwdev, *next;
1136103285Sikob
1137103285Sikob	s = splfw();
1138103285Sikob	fc->status = FWBUSEXPLORE;
1139103285Sikob	fc->retry_count = 0;
1140103285Sikob
1141103285Sikob/*
1142103285Sikob * Invalidate all devices, just after bus reset. Devices
1143103285Sikob * to be removed has not been seen longer time.
1144103285Sikob */
1145110193Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1146110193Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1147110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1148103285Sikob			fwdev->status = FWDEVINVAL;
1149103285Sikob			fwdev->rcnt = 0;
1150110193Ssimokawa		} else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1151103285Sikob			fwdev->rcnt ++;
1152110193Ssimokawa		} else {
1153110193Ssimokawa			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1154110195Ssimokawa			free(fwdev, M_FW);
1155103285Sikob		}
1156103285Sikob	}
1157103285Sikob	fc->ongonode = 0;
1158103285Sikob	fc->ongoaddr = CSRROMOFF;
1159103285Sikob	fc->ongodev = NULL;
1160103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1161103285Sikob	fw_bus_explore(fc);
1162103285Sikob	splx(s);
1163103285Sikob}
1164106790Ssimokawa
1165103285Sikob/*
1166103285Sikob * To collect device informations on the IEEE1394 bus.
1167103285Sikob */
1168106790Ssimokawastatic void
1169106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1170103285Sikob{
1171103285Sikob	int err = 0;
1172110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1173103285Sikob	u_int32_t addr;
1174103285Sikob	struct fw_xfer *xfer;
1175103285Sikob	struct fw_pkt *fp;
1176103285Sikob
1177103285Sikob	if(fc->status != FWBUSEXPLORE)
1178103285Sikob		return;
1179103285Sikob
1180103285Sikobloop:
1181103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1182103285Sikob
1183103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1184103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1185103285Sikob
1186103285Sikob	/* check link */
1187103285Sikob	/* XXX we need to check phy_id first */
1188103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1189110577Ssimokawa		if (firewire_debug)
1190110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1191103285Sikob		fc->ongonode++;
1192103285Sikob		goto loop;
1193103285Sikob	}
1194103285Sikob
1195103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1196103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1197103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1198103285Sikob		fc->ongoaddr = CSRROMOFF;
1199103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1200103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1201103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1202103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1203103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1204103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1205103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1206103285Sikob	}else if(fc->ongodev == NULL){
1207110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1208110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1209103285Sikob				break;
1210103285Sikob		if(fwdev != NULL){
1211103285Sikob			fwdev->dst = fc->ongonode;
1212103285Sikob			fwdev->status = FWDEVATTACHED;
1213103285Sikob			fc->ongonode++;
1214103285Sikob			fc->ongoaddr = CSRROMOFF;
1215103285Sikob			fc->ongodev = NULL;
1216103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1217103285Sikob			goto loop;
1218103285Sikob		}
1219110195Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW, M_NOWAIT);
1220103285Sikob		if(fwdev == NULL)
1221103285Sikob			return;
1222106810Ssimokawa		fwdev->fc = fc;
1223103285Sikob		fwdev->rommax = 0;
1224103285Sikob		fwdev->dst = fc->ongonode;
1225103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1226103285Sikob		fwdev->status = FWDEVINIT;
1227103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1228103285Sikob
1229110179Ssimokawa		pfwdev = NULL;
1230110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1231110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1232110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1233110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1234110179Ssimokawa				break;
1235110179Ssimokawa			pfwdev = tfwdev;
1236103285Sikob		}
1237110179Ssimokawa		if (pfwdev == NULL)
1238110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1239110179Ssimokawa		else
1240110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1241103285Sikob
1242108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1243107653Ssimokawa			linkspeed[fwdev->speed],
1244107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1245103285Sikob
1246103285Sikob		fc->ongodev = fwdev;
1247103285Sikob		fc->ongoaddr = CSRROMOFF;
1248103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1249103285Sikob	}else{
1250103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1251103285Sikob	}
1252103285Sikob#if 0
1253103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1254103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1255103285Sikob		fw_bus_explore_callback);
1256103285Sikob	if(xfer == NULL) goto done;
1257103285Sikob#else
1258113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1259103285Sikob	if(xfer == NULL){
1260103285Sikob		goto done;
1261103285Sikob	}
1262103285Sikob	xfer->spd = 0;
1263103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1264113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1265103285Sikob	fp->mode.rreqq.tlrt = 0;
1266103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1267103285Sikob	fp->mode.rreqq.pri = 0;
1268103285Sikob	fp->mode.rreqq.src = 0;
1269103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1270113584Ssimokawa	fp->mode.rreqq.dst = xfer->dst;
1271113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1272103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1273103285Sikob
1274110577Ssimokawa	if (firewire_debug)
1275110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1276110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1277103285Sikob	err = fw_asyreq(fc, -1, xfer);
1278103285Sikob	if(err){
1279103285Sikob		fw_xfer_free( xfer);
1280103285Sikob		return;
1281103285Sikob	}
1282103285Sikob#endif
1283103285Sikob	return;
1284103285Sikobdone:
1285103285Sikob	/* fw_attach_devs */
1286103285Sikob	fc->status = FWBUSEXPDONE;
1287107653Ssimokawa	if (firewire_debug)
1288107653Ssimokawa		printf("bus_explore done\n");
1289103285Sikob	fw_attach_dev(fc);
1290103285Sikob	return;
1291103285Sikob
1292103285Sikob}
1293106790Ssimokawa
1294103285Sikob/* Portable Async. request read quad */
1295106790Ssimokawastruct fw_xfer *
1296106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1297105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1298105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1299103285Sikob{
1300103285Sikob	struct fw_xfer *xfer;
1301103285Sikob	struct fw_pkt *fp;
1302103285Sikob	int err;
1303103285Sikob
1304113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1305113584Ssimokawa	if (xfer == NULL)
1306103285Sikob		return NULL;
1307113584Ssimokawa
1308103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1309103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1310113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1311103285Sikob	if(tl & FWP_TL_VALID){
1312103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1313103285Sikob	}else{
1314103285Sikob		fp->mode.rreqq.tlrt = 0;
1315103285Sikob	}
1316103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1317103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1318103285Sikob	fp->mode.rreqq.pri = 0;
1319103285Sikob	fp->mode.rreqq.src = 0;
1320103285Sikob	xfer->dst = addr_hi >> 16;
1321113584Ssimokawa	fp->mode.rreqq.dst = xfer->dst;
1322113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1323103285Sikob	xfer->act.hand = hand;
1324103285Sikob
1325103285Sikob	err = fw_asyreq(fc, -1, xfer);
1326103285Sikob	if(err){
1327103285Sikob		fw_xfer_free( xfer);
1328103285Sikob		return NULL;
1329103285Sikob	}
1330103285Sikob	return xfer;
1331103285Sikob}
1332106790Ssimokawa
1333103285Sikob/*
1334103285Sikob * Callback for the IEEE1394 bus information collection.
1335103285Sikob */
1336106790Ssimokawastatic void
1337106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1338106790Ssimokawa{
1339103285Sikob	struct firewire_comm *fc;
1340103285Sikob	struct fw_pkt *sfp,*rfp;
1341103285Sikob	struct csrhdr *chdr;
1342103285Sikob	struct csrdir *csrd;
1343103285Sikob	struct csrreg *csrreg;
1344103285Sikob	u_int32_t offset;
1345103285Sikob
1346103285Sikob
1347110577Ssimokawa	if(xfer == NULL) {
1348110577Ssimokawa		printf("xfer == NULL\n");
1349110577Ssimokawa		return;
1350110577Ssimokawa	}
1351103285Sikob	fc = xfer->fc;
1352110577Ssimokawa
1353110577Ssimokawa	if (firewire_debug)
1354110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1355110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1356110577Ssimokawa
1357103285Sikob	if(xfer->resp != 0){
1358110577Ssimokawa		printf("node%d: resp=%d addr=0x%x\n",
1359110577Ssimokawa			fc->ongonode, xfer->resp, fc->ongoaddr);
1360103285Sikob		fc->retry_count++;
1361103285Sikob		goto nextnode;
1362103285Sikob	}
1363103285Sikob
1364103285Sikob	if(xfer->send.buf == NULL){
1365110577Ssimokawa		printf("node%d: send.buf=NULL addr=0x%x\n",
1366103285Sikob			fc->ongonode, fc->ongoaddr);
1367103285Sikob		fc->retry_count++;
1368103285Sikob		goto nextnode;
1369103285Sikob	}
1370103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1371103285Sikob
1372103285Sikob	if(xfer->recv.buf == NULL){
1373110577Ssimokawa		printf("node%d: recv.buf=NULL addr=0x%x\n",
1374103285Sikob			fc->ongonode, fc->ongoaddr);
1375103285Sikob		fc->retry_count++;
1376103285Sikob		goto nextnode;
1377103285Sikob	}
1378103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1379103285Sikob#if 0
1380103285Sikob	{
1381103285Sikob		u_int32_t *qld;
1382103285Sikob		int i;
1383103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1384103285Sikob		printf("len:%d\n", xfer->recv.len);
1385103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1386113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1387103285Sikob			if((i % 16) == 15) printf("\n");
1388103285Sikob		}
1389103285Sikob		if((i % 16) != 15) printf("\n");
1390103285Sikob	}
1391103285Sikob#endif
1392103285Sikob	if(fc->ongodev == NULL){
1393113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1394103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1395103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1396110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */
1397103285Sikob			if(chdr->info_len == 1){
1398110577Ssimokawa				if (firewire_debug)
1399110577Ssimokawa					printf("node%d: minimal config\n",
1400110577Ssimokawa								fc->ongonode);
1401103285Sikob				goto nextnode;
1402103285Sikob			}else{
1403103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1404103285Sikob			}
1405113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1406103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1407103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1408113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1409103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1410110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1411110577Ssimokawa				if (firewire_debug)
1412110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1413110577Ssimokawa							fc->ongonode);
1414103285Sikob				goto nextnode;
1415110577Ssimokawa			}
1416103285Sikob			fc->ongoaddr = CSRROMOFF;
1417103285Sikob		}
1418103285Sikob	}else{
1419103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1420103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1421103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1422103285Sikob		}
1423103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1424103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1425103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1426103285Sikob			offset = CSRROMOFF;
1427103285Sikob		}else{
1428103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1429103285Sikob			offset = csrd->off;
1430103285Sikob		}
1431103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1432103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1433103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1434103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1435103285Sikob				if(csrd == NULL){
1436103285Sikob					goto nextnode;
1437103285Sikob				}else{
1438103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1439103285Sikob					fc->ongoaddr += csrreg->val * 4;
1440103285Sikob					csrd->off = fc->ongoaddr;
1441103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1442103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1443103285Sikob					goto nextaddr;
1444103285Sikob				}
1445103285Sikob			}
1446103285Sikob		}
1447103285Sikob		fc->ongoaddr += 4;
1448103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1449103285Sikob				(fc->ongodev->rommax < 0x414)){
1450103285Sikob			if(fc->ongodev->rommax <= 0x414){
1451103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1452103285Sikob				if(csrd == NULL) goto nextnode;
1453103285Sikob				csrd->off = fc->ongoaddr;
1454103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1455103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1456103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1457103285Sikob			}
1458103285Sikob			goto nextaddr;
1459103285Sikob		}
1460103285Sikob
1461103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1462103285Sikob			if(csrd == NULL){
1463103285Sikob				goto nextnode;
1464103285Sikob			};
1465103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1466103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1467103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1468103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1469103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1470103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1471103285Sikob				offset = CSRROMOFF;
1472103285Sikob			}else{
1473103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1474103285Sikob				offset = csrd->off;
1475103285Sikob			}
1476103285Sikob		}
1477103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1478103285Sikob			goto nextnode;
1479103285Sikob		}
1480103285Sikob	}
1481103285Sikobnextaddr:
1482103285Sikob	fw_xfer_free( xfer);
1483103285Sikob	fw_bus_explore(fc);
1484103285Sikob	return;
1485103285Sikobnextnode:
1486103285Sikob	fw_xfer_free( xfer);
1487103285Sikob	fc->ongonode++;
1488103285Sikob/* housekeeping work space */
1489103285Sikob	fc->ongoaddr = CSRROMOFF;
1490103285Sikob	fc->ongodev = NULL;
1491103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1492103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1493103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1494103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1495103285Sikob	}
1496103285Sikob	fw_bus_explore(fc);
1497103285Sikob	return;
1498103285Sikob}
1499103285Sikob
1500103285Sikob/*
1501103285Sikob * To obtain CSR register values.
1502103285Sikob */
1503106815Ssimokawau_int32_t
1504106815Ssimokawagetcsrdata(struct fw_device *fwdev, u_int8_t key)
1505103285Sikob{
1506103285Sikob	int i;
1507103285Sikob	struct csrhdr *chdr;
1508103285Sikob	struct csrreg *creg;
1509103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1510103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1511103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1512103285Sikob		if(creg->key == key){
1513103285Sikob			return (u_int32_t)creg->val;
1514103285Sikob		}
1515103285Sikob	}
1516103285Sikob	return 0;
1517103285Sikob}
1518106815Ssimokawa
1519103285Sikob/*
1520103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1521103285Sikob */
1522106815Ssimokawastatic void
1523106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1524103285Sikob{
1525103285Sikob	struct fw_device *fwdev;
1526103285Sikob	struct fw_xfer *xfer;
1527103285Sikob	int i, err;
1528103285Sikob	device_t *devlistp;
1529103285Sikob	int devcnt;
1530103285Sikob	struct firewire_dev_comm *fdc;
1531108701Ssimokawa	u_int32_t spec, ver;
1532103285Sikob
1533110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link) {
1534103285Sikob		if(fwdev->status == FWDEVINIT){
1535108701Ssimokawa			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1536108701Ssimokawa			if(spec == 0)
1537103285Sikob				continue;
1538108701Ssimokawa			ver = getcsrdata(fwdev, CSRKEY_VER);
1539108701Ssimokawa			if(ver == 0)
1540103285Sikob				continue;
1541103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1542103285Sikob
1543108655Ssimokawa			device_printf(fc->bdev, "Device ");
1544108701Ssimokawa			switch(spec){
1545103285Sikob			case CSRVAL_ANSIT10:
1546108701Ssimokawa				switch(ver){
1547103285Sikob				case CSRVAL_T10SBP2:
1548107653Ssimokawa					printf("SBP-II");
1549103285Sikob					break;
1550103285Sikob				default:
1551103285Sikob					break;
1552103285Sikob				}
1553103285Sikob				break;
1554103285Sikob			case CSRVAL_1394TA:
1555108701Ssimokawa				switch(ver){
1556103285Sikob				case CSR_PROTAVC:
1557107653Ssimokawa					printf("AV/C");
1558103285Sikob					break;
1559103285Sikob				case CSR_PROTCAL:
1560107653Ssimokawa					printf("CAL");
1561103285Sikob					break;
1562103285Sikob				case CSR_PROTEHS:
1563107653Ssimokawa					printf("EHS");
1564103285Sikob					break;
1565103285Sikob				case CSR_PROTHAVI:
1566107653Ssimokawa					printf("HAVi");
1567103285Sikob					break;
1568103285Sikob				case CSR_PROTCAM104:
1569107653Ssimokawa					printf("1394 Cam 1.04");
1570103285Sikob					break;
1571103285Sikob				case CSR_PROTCAM120:
1572107653Ssimokawa					printf("1394 Cam 1.20");
1573103285Sikob					break;
1574103285Sikob				case CSR_PROTCAM130:
1575107653Ssimokawa					printf("1394 Cam 1.30");
1576103285Sikob					break;
1577103285Sikob				case CSR_PROTDPP:
1578107653Ssimokawa					printf("1394 Direct print");
1579103285Sikob					break;
1580103285Sikob				case CSR_PROTIICP:
1581107653Ssimokawa					printf("Industrial & Instrument");
1582103285Sikob					break;
1583103285Sikob				default:
1584107653Ssimokawa					printf("unknown 1394TA");
1585103285Sikob					break;
1586103285Sikob				}
1587103285Sikob				break;
1588103285Sikob			default:
1589107653Ssimokawa				printf("unknown spec");
1590103285Sikob				break;
1591103285Sikob			}
1592103285Sikob			fwdev->status = FWDEVATTACHED;
1593103285Sikob			printf("\n");
1594103285Sikob		}
1595103285Sikob	}
1596108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1597103285Sikob	if( err != 0 )
1598103285Sikob		return;
1599103285Sikob	for( i = 0 ; i < devcnt ; i++){
1600103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1601103285Sikob			fdc = device_get_softc(devlistp[i]);
1602103285Sikob			if (fdc->post_explore != NULL)
1603103285Sikob				fdc->post_explore(fdc);
1604103285Sikob		}
1605103285Sikob	}
1606103285Sikob	free(devlistp, M_TEMP);
1607103285Sikob
1608103285Sikob	/* call pending handlers */
1609103285Sikob	i = 0;
1610103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1611103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1612103285Sikob		i++;
1613103285Sikob		if (xfer->act.hand)
1614103285Sikob			xfer->act.hand(xfer);
1615103285Sikob	}
1616103285Sikob	if (i > 0)
1617103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1618103285Sikob	if (fc->retry_count > 0) {
1619111040Ssimokawa		printf("probe failed for %d node\n", fc->retry_count);
1620111040Ssimokawa#if 0
1621110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1622110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1623111040Ssimokawa#endif
1624103285Sikob	}
1625103285Sikob	return;
1626103285Sikob}
1627106815Ssimokawa
1628103285Sikob/*
1629103285Sikob * To allocate uniq transaction label.
1630103285Sikob */
1631106815Ssimokawastatic int
1632106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1633103285Sikob{
1634103285Sikob	u_int i;
1635103285Sikob	struct tlabel *tl, *tmptl;
1636103285Sikob	int s;
1637103285Sikob	static u_int32_t label = 0;
1638103285Sikob
1639103285Sikob	s = splfw();
1640103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1641103285Sikob		label = (label + 1) & 0x3f;
1642103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1643103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1644103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1645103285Sikob		}
1646103285Sikob		if(tmptl == NULL) {
1647110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1648103285Sikob			if (tl == NULL) {
1649103285Sikob				splx(s);
1650103285Sikob				return (-1);
1651103285Sikob			}
1652103285Sikob			tl->xfer = xfer;
1653103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1654103285Sikob			splx(s);
1655110577Ssimokawa			if (firewire_debug > 1)
1656110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1657110577Ssimokawa						xfer->dst, label);
1658103285Sikob			return(label);
1659103285Sikob		}
1660103285Sikob	}
1661103285Sikob	splx(s);
1662103285Sikob
1663103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1664103285Sikob	return(-1);
1665103285Sikob}
1666106815Ssimokawa
1667113584Ssimokawastatic void
1668113584Ssimokawafw_rcv_copy(struct fw_xfer *xfer, struct iovec *vec, int nvec)
1669113584Ssimokawa{
1670113584Ssimokawa	char *p;
1671113584Ssimokawa	int res, i, len;
1672113584Ssimokawa
1673113584Ssimokawa	p = xfer->recv.buf;
1674113584Ssimokawa	res = xfer->recv.len;
1675113584Ssimokawa	for (i = 0; i < nvec; i++, vec++) {
1676113584Ssimokawa		len = vec->iov_len;
1677113584Ssimokawa		if (res < len) {
1678113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1679113584Ssimokawa						xfer->recv.len, len - res);
1680113584Ssimokawa			len = res;
1681113584Ssimokawa		}
1682113584Ssimokawa		bcopy(vec->iov_base, p, len);
1683113584Ssimokawa		p += len;
1684113584Ssimokawa		res -= len;
1685113584Ssimokawa		if (res <= 0)
1686113584Ssimokawa			break;
1687113584Ssimokawa	}
1688113584Ssimokawa	xfer->recv.len -= res;
1689113584Ssimokawa}
1690113584Ssimokawa
1691103285Sikob/*
1692103285Sikob * Generic packet receving process.
1693103285Sikob */
1694106815Ssimokawavoid
1695113584Ssimokawafw_rcv(struct firewire_comm *fc, struct iovec *vec, int nvec, u_int sub, u_int spd)
1696103285Sikob{
1697103285Sikob	struct fw_pkt *fp, *resfp;
1698103285Sikob	struct fw_xfer *xfer;
1699103285Sikob	struct fw_bind *bind;
1700103285Sikob	struct firewire_softc *sc;
1701113584Ssimokawa	int tcode, s;
1702113584Ssimokawa	int i, len, oldstate;
1703103285Sikob#if 0
1704103285Sikob	{
1705103285Sikob		u_int32_t *qld;
1706103285Sikob		int i;
1707103285Sikob		qld = (u_int32_t *)buf;
1708103285Sikob		printf("spd %d len:%d\n", spd, len);
1709103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1710103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1711103285Sikob			if((i % 16) == 15) printf("\n");
1712103285Sikob		}
1713103285Sikob		if((i % 16) != 15) printf("\n");
1714103285Sikob	}
1715103285Sikob#endif
1716113584Ssimokawa	fp = (struct fw_pkt *)vec[0].iov_base;
1717113584Ssimokawa	tcode = fp->mode.common.tcode;
1718113584Ssimokawa#if 0 /* XXX this check is not valid for RRESQ and WREQQ */
1719113584Ssimokawa	if (vec[0].iov_len < fc->tcode[tcode].hdr_len) {
1720113584Ssimokawa#if __FreeBSD_version >= 500000
1721113584Ssimokawa		printf("fw_rcv: iov_len(%zu) is less than"
1722113584Ssimokawa#else
1723113584Ssimokawa		printf("fw_rcv: iov_len(%u) is less than"
1724113584Ssimokawa#endif
1725113584Ssimokawa			" hdr_len(%d:tcode=%d)\n", vec[0].iov_len,
1726113584Ssimokawa			fc->tcode[tcode].hdr_len, tcode);
1727113584Ssimokawa	}
1728113584Ssimokawa#endif
1729113584Ssimokawa	switch (tcode) {
1730103285Sikob	case FWTCODE_WRES:
1731103285Sikob	case FWTCODE_RRESQ:
1732103285Sikob	case FWTCODE_RRESB:
1733103285Sikob	case FWTCODE_LRES:
1734113584Ssimokawa		xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1735103285Sikob					fp->mode.hdr.tlrt >> 2);
1736103285Sikob		if(xfer == NULL) {
1737103285Sikob			printf("fw_rcv: unknown response "
1738110891Ssimokawa					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1739113584Ssimokawa					tcode,
1740113584Ssimokawa					fp->mode.hdr.src,
1741103285Sikob					fp->mode.hdr.tlrt >> 2,
1742103285Sikob					fp->mode.hdr.tlrt & 3,
1743103285Sikob					fp->mode.rresq.data);
1744103285Sikob#if 1
1745103285Sikob			printf("try ad-hoc work around!!\n");
1746113584Ssimokawa			xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1747103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1748103285Sikob			if (xfer == NULL) {
1749103285Sikob				printf("no use...\n");
1750103285Sikob				goto err;
1751103285Sikob			}
1752103285Sikob#else
1753103285Sikob			goto err;
1754103285Sikob#endif
1755103285Sikob		}
1756113584Ssimokawa		fw_rcv_copy(xfer, vec, nvec);
1757113584Ssimokawa		xfer->resp = 0;
1758113584Ssimokawa		/* make sure the packet is drained in AT queue */
1759113584Ssimokawa		oldstate = xfer->state;
1760113584Ssimokawa		xfer->state = FWXF_RCVD;
1761113584Ssimokawa		switch (oldstate) {
1762113584Ssimokawa		case FWXF_SENT:
1763103285Sikob			fw_xfer_done(xfer);
1764103285Sikob			break;
1765113584Ssimokawa		case FWXF_START:
1766113584Ssimokawa			if (firewire_debug)
1767113584Ssimokawa				printf("not sent yet\n");
1768113584Ssimokawa			break;
1769103285Sikob		default:
1770113584Ssimokawa			printf("unexpected state %d\n", xfer->state);
1771103285Sikob		}
1772113584Ssimokawa		return;
1773103285Sikob	case FWTCODE_WREQQ:
1774103285Sikob	case FWTCODE_WREQB:
1775103285Sikob	case FWTCODE_RREQQ:
1776103285Sikob	case FWTCODE_RREQB:
1777103285Sikob	case FWTCODE_LREQ:
1778113584Ssimokawa		bind = fw_bindlookup(fc, fp->mode.rreqq.dest_hi,
1779113584Ssimokawa			fp->mode.rreqq.dest_lo);
1780103285Sikob		if(bind == NULL){
1781113584Ssimokawa			printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%x\n",
1782113584Ssimokawa				fp->mode.wreqq.dest_hi,
1783113584Ssimokawa				fp->mode.wreqq.dest_lo,
1784113584Ssimokawa				tcode,
1785113584Ssimokawa				fp->mode.hdr.src,
1786113584Ssimokawa				ntohl(fp->mode.wreqq.data));
1787103285Sikob			if (fc->status == FWBUSRESET) {
1788110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1789103285Sikob				goto err;
1790103285Sikob			}
1791113584Ssimokawa			xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 0);
1792103285Sikob			if(xfer == NULL){
1793103285Sikob				return;
1794103285Sikob			}
1795103285Sikob			xfer->spd = spd;
1796103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1797113584Ssimokawa			switch (tcode) {
1798103285Sikob			case FWTCODE_WREQQ:
1799103285Sikob			case FWTCODE_WREQB:
1800103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1801103285Sikob				xfer->send.len = 12;
1802103285Sikob				break;
1803103285Sikob			case FWTCODE_RREQQ:
1804103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1805103285Sikob				xfer->send.len = 16;
1806103285Sikob				break;
1807103285Sikob			case FWTCODE_RREQB:
1808103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1809103285Sikob				xfer->send.len = 16;
1810103285Sikob				break;
1811103285Sikob			case FWTCODE_LREQ:
1812103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1813103285Sikob				xfer->send.len = 16;
1814103285Sikob				break;
1815103285Sikob			}
1816103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1817103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1818103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1819103285Sikob			resfp->mode.rresb.rtcode = 7;
1820103285Sikob			resfp->mode.rresb.extcode = 0;
1821103285Sikob			resfp->mode.rresb.len = 0;
1822103285Sikob/*
1823103285Sikob			xfer->act.hand = fw_asy_callback;
1824103285Sikob*/
1825103285Sikob			xfer->act.hand = fw_xfer_free;
1826103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1827103285Sikob				fw_xfer_free( xfer);
1828103285Sikob				return;
1829103285Sikob			}
1830103285Sikob			goto err;
1831103285Sikob		}
1832113584Ssimokawa		len = 0;
1833113584Ssimokawa		for (i = 0; i < nvec; i ++)
1834113584Ssimokawa			len += vec[i].iov_len;
1835113584Ssimokawa		switch(bind->act_type){
1836103285Sikob		case FWACT_XFER:
1837113584Ssimokawa			/* splfw()?? */
1838113584Ssimokawa			xfer = STAILQ_FIRST(&bind->xferlist);
1839113584Ssimokawa			if (xfer == NULL) {
1840113584Ssimokawa				printf("Discard a packet for this bind.\n");
1841113584Ssimokawa				goto err;
1842113584Ssimokawa			}
1843113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1844113584Ssimokawa			fw_rcv_copy(xfer, vec, nvec);
1845103285Sikob			xfer->spd = spd;
1846103285Sikob			if (fc->status != FWBUSRESET)
1847103285Sikob				xfer->act.hand(xfer);
1848103285Sikob			else
1849103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1850103285Sikob			return;
1851103285Sikob			break;
1852103285Sikob		case FWACT_CH:
1853113584Ssimokawa			if(fc->ir[bind->sub]->queued >=
1854113584Ssimokawa				fc->ir[bind->sub]->maxq){
1855108655Ssimokawa				device_printf(fc->bdev,
1856108655Ssimokawa					"Discard a packet %x %d\n",
1857113584Ssimokawa					bind->sub,
1858113584Ssimokawa					fc->ir[bind->sub]->queued);
1859103285Sikob				goto err;
1860103285Sikob			}
1861113584Ssimokawa			xfer = STAILQ_FIRST(&bind->xferlist);
1862113584Ssimokawa			if (xfer == NULL) {
1863113584Ssimokawa				printf("Discard packet for this bind\n");
1864113584Ssimokawa				goto err;
1865113584Ssimokawa			}
1866113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1867113584Ssimokawa			fw_rcv_copy(xfer, vec, nvec);
1868103285Sikob			xfer->spd = spd;
1869103285Sikob			s = splfw();
1870113584Ssimokawa			fc->ir[bind->sub]->queued++;
1871113584Ssimokawa			STAILQ_INSERT_TAIL(&fc->ir[bind->sub]->q, xfer, link);
1872103285Sikob			splx(s);
1873103285Sikob
1874113584Ssimokawa			wakeup((caddr_t)fc->ir[bind->sub]);
1875103285Sikob
1876103285Sikob			return;
1877103285Sikob			break;
1878103285Sikob		default:
1879103285Sikob			goto err;
1880103285Sikob			break;
1881103285Sikob		}
1882103285Sikob		break;
1883103285Sikob	case FWTCODE_STREAM:
1884103285Sikob	{
1885103285Sikob		struct fw_xferq *xferq;
1886103285Sikob
1887103285Sikob		xferq = fc->ir[sub];
1888103285Sikob#if 0
1889103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1890103285Sikob			sub, len, off, spd);
1891103285Sikob#endif
1892103285Sikob		if(xferq->queued >= xferq->maxq) {
1893103285Sikob			printf("receive queue is full\n");
1894103285Sikob			goto err;
1895103285Sikob		}
1896113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1897113584Ssimokawa			per packet mode */
1898113584Ssimokawa		xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1899113584Ssimokawa						vec[0].iov_len);
1900103285Sikob		if(xfer == NULL) goto err;
1901113584Ssimokawa		fw_rcv_copy(xfer, vec, nvec);
1902103285Sikob		xfer->spd = spd;
1903103285Sikob		s = splfw();
1904103285Sikob		xferq->queued++;
1905103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1906103285Sikob		splx(s);
1907103285Sikob		sc = device_get_softc(fc->bdev);
1908103285Sikob#if __FreeBSD_version >= 500000
1909103285Sikob		if (SEL_WAITING(&xferq->rsel))
1910103285Sikob#else
1911103285Sikob		if (&xferq->rsel.si_pid != 0)
1912103285Sikob#endif
1913103285Sikob			selwakeup(&xferq->rsel);
1914103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1915103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1916103285Sikob			wakeup((caddr_t)xferq);
1917103285Sikob		}
1918103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1919103285Sikob			xferq->hand(xferq);
1920103285Sikob		}
1921103285Sikob		return;
1922103285Sikob		break;
1923103285Sikob	}
1924103285Sikob	default:
1925113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1926103285Sikob		break;
1927103285Sikob	}
1928103285Sikoberr:
1929113584Ssimokawa	return;
1930103285Sikob}
1931106815Ssimokawa
1932103285Sikob/*
1933103285Sikob * Post process for Bus Manager election process.
1934103285Sikob */
1935103285Sikobstatic void
1936103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1937103285Sikob{
1938109422Ssimokawa	struct fw_pkt *rfp;
1939103285Sikob	struct firewire_comm *fc;
1940109422Ssimokawa	int bmr;
1941103285Sikob
1942109422Ssimokawa	if (xfer == NULL)
1943109422Ssimokawa		return;
1944103285Sikob	fc = xfer->fc;
1945109422Ssimokawa	if (xfer->resp != 0)
1946103285Sikob		goto error;
1947109422Ssimokawa	if (xfer->send.buf == NULL)
1948103285Sikob		goto error;
1949109422Ssimokawa	if (xfer->recv.buf == NULL)
1950103285Sikob		goto error;
1951103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1952109422Ssimokawa	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1953109422Ssimokawa		goto error;
1954109422Ssimokawa
1955109422Ssimokawa	bmr = ntohl(rfp->mode.lres.payload[0]);
1956109422Ssimokawa	if (bmr == 0x3f)
1957109422Ssimokawa		bmr = fc->nodeid;
1958109422Ssimokawa
1959109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1960108655Ssimokawa	device_printf(fc->bdev, "new bus manager %d ",
1961108655Ssimokawa		CSRARC(fc, BUS_MGR_ID));
1962109422Ssimokawa	if(bmr == fc->nodeid){
1963103285Sikob		printf("(me)\n");
1964110072Ssimokawa		fw_bmr(fc);
1965103285Sikob	}else{
1966103285Sikob		printf("\n");
1967103285Sikob	}
1968103285Sikoberror:
1969103285Sikob	fw_xfer_free(xfer);
1970103285Sikob}
1971106815Ssimokawa
1972113584Ssimokawa
1973103285Sikob/*
1974103285Sikob * To candidate Bus Manager election process.
1975103285Sikob */
1976110270Ssimokawastatic void
1977106815Ssimokawafw_try_bmr(void *arg)
1978103285Sikob{
1979103285Sikob	struct fw_xfer *xfer;
1980103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
1981103285Sikob	struct fw_pkt *fp;
1982103285Sikob	int err = 0;
1983103285Sikob
1984113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 24, 20);
1985103285Sikob	if(xfer == NULL){
1986103285Sikob		return;
1987103285Sikob	}
1988103285Sikob	xfer->spd = 0;
1989103285Sikob	fc->status = FWBUSMGRELECT;
1990103285Sikob
1991103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1992113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
1993103285Sikob	fp->mode.lreq.tlrt = 0;
1994103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
1995103285Sikob	fp->mode.lreq.pri = 0;
1996103285Sikob	fp->mode.lreq.src = 0;
1997113584Ssimokawa	fp->mode.lreq.len = 8;
1998113584Ssimokawa	fp->mode.lreq.extcode = FW_LREQ_CMPSWAP;
1999103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
2000113584Ssimokawa	fp->mode.lreq.dst = xfer->dst;
2001113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2002109422Ssimokawa	fp->mode.lreq.payload[0] = htonl(0x3f);
2003109422Ssimokawa	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
2004103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2005103285Sikob
2006103285Sikob	err = fw_asyreq(fc, -1, xfer);
2007103285Sikob	if(err){
2008103285Sikob		fw_xfer_free( xfer);
2009103285Sikob		return;
2010103285Sikob	}
2011103285Sikob	return;
2012103285Sikob}
2013106543Ssimokawa
2014106543Ssimokawa#ifdef FW_VMACCESS
2015103285Sikob/*
2016103285Sikob * Software implementation for physical memory block access.
2017103285Sikob * XXX:Too slow, usef for debug purpose only.
2018103285Sikob */
2019106815Ssimokawastatic void
2020106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2021103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2022113584Ssimokawa	u_int32_t *ld = (u_int32_t *)xfer->recv.buf;
2023103285Sikob
2024113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2025113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2026103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2027103285Sikob	if(xfer->resp != 0){
2028103285Sikob		fw_xfer_free( xfer);
2029103285Sikob		return;
2030103285Sikob	}
2031103285Sikob	if(xfer->recv.buf == NULL){
2032103285Sikob		fw_xfer_free( xfer);
2033103285Sikob		return;
2034103285Sikob	}
2035103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2036103285Sikob	switch(rfp->mode.hdr.tcode){
2037103285Sikob		/* XXX need fix for 64bit arch */
2038103285Sikob		case FWTCODE_WREQB:
2039110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2040103285Sikob			xfer->send.len = 12;
2041103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2042103285Sikob			bcopy(rfp->mode.wreqb.payload,
2043103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2044103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2045103285Sikob			sfp->mode.wres.rtcode = 0;
2046103285Sikob			break;
2047103285Sikob		case FWTCODE_WREQQ:
2048110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2049103285Sikob			xfer->send.len = 12;
2050103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2051103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2052103285Sikob			sfp->mode.wres.rtcode = 0;
2053103285Sikob			break;
2054103285Sikob		case FWTCODE_RREQB:
2055110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2056103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2057103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2058103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2059103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2060103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2061103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2062103285Sikob			sfp->mode.rresb.rtcode = 0;
2063103285Sikob			sfp->mode.rresb.extcode = 0;
2064103285Sikob			break;
2065103285Sikob		case FWTCODE_RREQQ:
2066110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2067103285Sikob			xfer->send.len = 16;
2068103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2069103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2070103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2071103285Sikob			sfp->mode.rresb.rtcode = 0;
2072103285Sikob			break;
2073103285Sikob		default:
2074103285Sikob			fw_xfer_free( xfer);
2075103285Sikob			return;
2076103285Sikob	}
2077103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2078103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2079103285Sikob	xfer->act.hand = fw_xfer_free;
2080103285Sikob	xfer->retry_req = fw_asybusy;
2081103285Sikob
2082103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2083103285Sikob	sfp->mode.hdr.pri = 0;
2084103285Sikob
2085103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2086103285Sikob/**/
2087103285Sikob	return;
2088103285Sikob}
2089106543Ssimokawa#endif
2090106543Ssimokawa
2091103285Sikob/*
2092103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2093103285Sikob */
2094106815Ssimokawau_int16_t
2095106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2096103285Sikob	u_int32_t i, sum, crc = 0;
2097103285Sikob	int shift;
2098103285Sikob	len = (len + 3) & ~3;
2099103285Sikob	for(i = 0 ; i < len ; i+= 4){
2100103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2101103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2102103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2103103285Sikob		}
2104103285Sikob		crc &= 0xffff;
2105103285Sikob	}
2106103285Sikob	return((u_int16_t) crc);
2107103285Sikob}
2108106815Ssimokawa
2109110270Ssimokawastatic int
2110110072Ssimokawafw_bmr(struct firewire_comm *fc)
2111110072Ssimokawa{
2112110072Ssimokawa	struct fw_device fwdev;
2113113584Ssimokawa	union fw_self_id *self_id;
2114110072Ssimokawa	int cmstr;
2115110072Ssimokawa
2116113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2117113584Ssimokawa	self_id = &fc->topology_map->self_id[fc->max_node];
2118113584Ssimokawa	if (fc->max_node > 0) {
2119113584Ssimokawa		if (self_id->p0.contender)
2120113584Ssimokawa			cmstr = fc->max_node;
2121113584Ssimokawa		else
2122113584Ssimokawa			/* XXX shall we be cycle master? */
2123113584Ssimokawa			cmstr = fc->nodeid;
2124113584Ssimokawa			/* XXX bus reset? */
2125113584Ssimokawa	} else
2126113584Ssimokawa		cmstr = -1;
2127110072Ssimokawa	/* If I am the bus manager, optimize gapcount */
2128113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2129113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2130110072Ssimokawa	/* If we are the cycle master, nothing to do */
2131113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2132110072Ssimokawa		return 0;
2133110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2134110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2135110072Ssimokawa	fwdev.fc = fc;
2136110072Ssimokawa	fwdev.dst = cmstr;
2137110072Ssimokawa	fwdev.speed = 0;
2138110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2139110072Ssimokawa	fwdev.status = FWDEVINIT;
2140110072Ssimokawa	/* Set cmstr bit on the cycle master */
2141110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2142112524Ssimokawa		0xffff, 0xf0000000 | STATE_SET, htonl(1 << 16),
2143110072Ssimokawa		fw_asy_callback_free);
2144110072Ssimokawa
2145110072Ssimokawa	return 0;
2146110072Ssimokawa}
2147110072Ssimokawa
2148103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2149103285SikobMODULE_VERSION(firewire, 1);
2150