firewire.c revision 152792
1217309Snwhitehorn/*-
2255852Sdteske * Copyright (c) 2003 Hidetoshi Shimokawa
3217309Snwhitehorn * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4217309Snwhitehorn * All rights reserved.
5217309Snwhitehorn *
6251843Sbapt * Redistribution and use in source and binary forms, with or without
7217309Snwhitehorn * modification, are permitted provided that the following conditions
8217309Snwhitehorn * are met:
9217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
10217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
11217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
12217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
13217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
14217309Snwhitehorn * 3. All advertising materials mentioning features or use of this software
15217309Snwhitehorn *    must display the acknowledgement as bellow:
16217309Snwhitehorn *
17217309Snwhitehorn *    This product includes software developed by K. Kobayashi and H. Shimokawa
18217309Snwhitehorn *
19217309Snwhitehorn * 4. The name of the author may not be used to endorse or promote products
20217309Snwhitehorn *    derived from this software without specific prior written permission.
21217309Snwhitehorn *
22217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23217309Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24217309Snwhitehorn * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25217309Snwhitehorn * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26217309Snwhitehorn * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27217309Snwhitehorn * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28217309Snwhitehorn * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29217309Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30217309Snwhitehorn * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31217309Snwhitehorn * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32217309Snwhitehorn * POSSIBILITY OF SUCH DAMAGE.
33217309Snwhitehorn *
34217309Snwhitehorn * $FreeBSD: head/sys/dev/firewire/firewire.c 152792 2005-11-25 14:29:24Z simokawa $
35217309Snwhitehorn *
36217309Snwhitehorn */
37217309Snwhitehorn
38217309Snwhitehorn#include <sys/param.h>
39217309Snwhitehorn#include <sys/systm.h>
40217309Snwhitehorn#include <sys/types.h>
41217309Snwhitehorn
42217309Snwhitehorn#include <sys/kernel.h>
43217309Snwhitehorn#include <sys/module.h>
44217309Snwhitehorn#include <sys/malloc.h>
45217309Snwhitehorn#include <sys/conf.h>
46217309Snwhitehorn#include <sys/sysctl.h>
47217309Snwhitehorn
48217309Snwhitehorn#if defined(__DragonFly__) || __FreeBSD_version < 500000
49217309Snwhitehorn#include <machine/clock.h>	/* for DELAY() */
50217309Snwhitehorn#endif
51217309Snwhitehorn
52217309Snwhitehorn#include <sys/bus.h>		/* used by smbus and newbus */
53217309Snwhitehorn#include <machine/bus.h>
54217309Snwhitehorn
55217309Snwhitehorn#ifdef __DragonFly__
56217309Snwhitehorn#include "firewire.h"
57217309Snwhitehorn#include "firewirereg.h"
58217309Snwhitehorn#include "fwmem.h"
59251843Sbapt#include "iec13213.h"
60217309Snwhitehorn#include "iec68113.h"
61217309Snwhitehorn#else
62217309Snwhitehorn#include <dev/firewire/firewire.h>
63217309Snwhitehorn#include <dev/firewire/firewirereg.h>
64217309Snwhitehorn#include <dev/firewire/fwmem.h>
65217309Snwhitehorn#include <dev/firewire/iec13213.h>
66217309Snwhitehorn#include <dev/firewire/iec68113.h>
67217309Snwhitehorn#endif
68217309Snwhitehorn
69217309Snwhitehornstruct crom_src_buf {
70224014Snwhitehorn	struct crom_src	src;
71217309Snwhitehorn	struct crom_chunk root;
72224014Snwhitehorn	struct crom_chunk vendor;
73217309Snwhitehorn	struct crom_chunk hw;
74255852Sdteske};
75217309Snwhitehorn
76217309Snwhitehornint firewire_debug=0, try_bmr=1, hold_count=3;
77217309SnwhitehornSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
78217309Snwhitehorn	"FireWire driver debug flag");
79217309SnwhitehornSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
80217309SnwhitehornSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
81217309Snwhitehorn	"Try to be a bus manager");
82217309SnwhitehornSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
83217309Snwhitehorn	"Number of count of bus resets for removing lost device information");
84217309Snwhitehorn
85217309SnwhitehornMALLOC_DEFINE(M_FW, "firewire", "FireWire");
86251843SbaptMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
87217309Snwhitehorn
88217309Snwhitehorn#define FW_MAXASYRTY 4
89217309Snwhitehorn
90217309Snwhitehorndevclass_t firewire_devclass;
91217309Snwhitehorn
92217309Snwhitehornstatic void firewire_identify	(driver_t *, device_t);
93217309Snwhitehornstatic int firewire_probe	(device_t);
94217309Snwhitehornstatic int firewire_attach      (device_t);
95217309Snwhitehornstatic int firewire_detach      (device_t);
96217309Snwhitehornstatic int firewire_resume      (device_t);
97217309Snwhitehorn#if 0
98220749Snwhitehornstatic int firewire_shutdown    (device_t);
99224014Snwhitehorn#endif
100217309Snwhitehornstatic device_t firewire_add_child   (device_t, int, const char *, int);
101217309Snwhitehornstatic void fw_try_bmr (void *);
102217309Snwhitehornstatic void fw_try_bmr_callback (struct fw_xfer *);
103217309Snwhitehornstatic void fw_asystart (struct fw_xfer *);
104217309Snwhitehornstatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
105217309Snwhitehornstatic void fw_bus_probe (struct firewire_comm *);
106217309Snwhitehornstatic void fw_bus_explore (struct firewire_comm *);
107217309Snwhitehornstatic void fw_bus_explore_callback (struct fw_xfer *);
108217309Snwhitehornstatic void fw_attach_dev (struct firewire_comm *);
109220749Snwhitehorn#ifdef FW_VMACCESS
110217309Snwhitehornstatic void fw_vmaccess (struct fw_xfer *);
111217309Snwhitehorn#endif
112217309Snwhitehornstruct fw_xfer *asyreqq (struct firewire_comm *, uint8_t, uint8_t, uint8_t,
113220749Snwhitehorn	uint32_t, uint32_t, void (*)(struct fw_xfer *));
114217309Snwhitehornstatic int fw_bmr (struct firewire_comm *);
115217309Snwhitehorn
116217309Snwhitehornstatic device_method_t firewire_methods[] = {
117217309Snwhitehorn	/* Device interface */
118217309Snwhitehorn	DEVMETHOD(device_identify,	firewire_identify),
119217309Snwhitehorn	DEVMETHOD(device_probe,		firewire_probe),
120217309Snwhitehorn	DEVMETHOD(device_attach,	firewire_attach),
121217309Snwhitehorn	DEVMETHOD(device_detach,	firewire_detach),
122217309Snwhitehorn	DEVMETHOD(device_suspend,	bus_generic_suspend),
123217309Snwhitehorn	DEVMETHOD(device_resume,	firewire_resume),
124217309Snwhitehorn	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
125217309Snwhitehorn
126217309Snwhitehorn	/* Bus interface */
127217309Snwhitehorn	DEVMETHOD(bus_add_child,	firewire_add_child),
128217309Snwhitehorn	DEVMETHOD(bus_print_child,	bus_generic_print_child),
129217309Snwhitehorn
130217309Snwhitehorn	{ 0, 0 }
131217309Snwhitehorn};
132217309Snwhitehornchar *linkspeed[] = {
133217309Snwhitehorn	"S100", "S200", "S400", "S800",
134217309Snwhitehorn	"S1600", "S3200", "undef", "undef"
135217309Snwhitehorn};
136217309Snwhitehorn
137217309Snwhitehornstatic char *tcode_str[] = {
138217309Snwhitehorn	"WREQQ", "WREQB", "WRES",   "undef",
139224014Snwhitehorn	"RREQQ", "RREQB", "RRESQ",  "RRESB",
140217309Snwhitehorn	"CYCS",  "LREQ",  "STREAM", "LRES",
141217309Snwhitehorn	"undef", "undef", "PHY",    "undef"
142217309Snwhitehorn};
143217309Snwhitehorn
144251843Sbapt/* IEEE-1394a Table C-2 Gap count as a function of hops*/
145251843Sbapt#define MAX_GAPHOP 15
146251843Sbaptu_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
147251843Sbapt		   21, 24, 26, 29, 32, 35, 37, 40};
148251843Sbapt
149251843Sbaptstatic driver_t firewire_driver = {
150251843Sbapt	"firewire",
151251843Sbapt	firewire_methods,
152251843Sbapt	sizeof(struct firewire_softc),
153251843Sbapt};
154251843Sbapt
155251843Sbapt/*
156251843Sbapt * Lookup fwdev by node id.
157251843Sbapt */
158251843Sbaptstruct fw_device *
159251843Sbaptfw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
160251843Sbapt{
161251843Sbapt	struct fw_device *fwdev;
162251843Sbapt	int s;
163251843Sbapt
164217309Snwhitehorn	s = splfw();
165217309Snwhitehorn	STAILQ_FOREACH(fwdev, &fc->devices, link)
166217309Snwhitehorn		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
167217309Snwhitehorn			break;
168217309Snwhitehorn	splx(s);
169217309Snwhitehorn
170217309Snwhitehorn	return fwdev;
171217309Snwhitehorn}
172217309Snwhitehorn
173217309Snwhitehorn/*
174217309Snwhitehorn * Lookup fwdev by EUI64.
175217309Snwhitehorn */
176217309Snwhitehornstruct fw_device *
177217309Snwhitehornfw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
178217309Snwhitehorn{
179217309Snwhitehorn	struct fw_device *fwdev;
180217309Snwhitehorn	int s;
181217309Snwhitehorn
182217309Snwhitehorn	s = splfw();
183217309Snwhitehorn	STAILQ_FOREACH(fwdev, &fc->devices, link)
184217309Snwhitehorn		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
185217309Snwhitehorn			break;
186217309Snwhitehorn	splx(s);
187217309Snwhitehorn
188217309Snwhitehorn	if(fwdev == NULL) return NULL;
189217309Snwhitehorn	if(fwdev->status == FWDEVINVAL) return NULL;
190217309Snwhitehorn	return fwdev;
191217309Snwhitehorn}
192217309Snwhitehorn
193217309Snwhitehorn/*
194217309Snwhitehorn * Async. request procedure for userland application.
195217309Snwhitehorn */
196217309Snwhitehornint
197217309Snwhitehornfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
198217309Snwhitehorn{
199217309Snwhitehorn	int err = 0;
200217309Snwhitehorn	struct fw_xferq *xferq;
201217309Snwhitehorn	int tl = 0, len;
202217309Snwhitehorn	struct fw_pkt *fp;
203217309Snwhitehorn	int tcode;
204217309Snwhitehorn	struct tcode_info *info;
205217309Snwhitehorn
206217309Snwhitehorn	if(xfer == NULL) return EINVAL;
207217309Snwhitehorn	if(xfer->act.hand == NULL){
208251843Sbapt		printf("act.hand == NULL\n");
209251843Sbapt		return EINVAL;
210217309Snwhitehorn	}
211217309Snwhitehorn	fp = &xfer->send.hdr;
212217309Snwhitehorn
213217309Snwhitehorn	tcode = fp->mode.common.tcode & 0xf;
214217309Snwhitehorn	info = &fc->tcode[tcode];
215217309Snwhitehorn	if (info->flag == 0) {
216217309Snwhitehorn		printf("invalid tcode=%x\n", tcode);
217217309Snwhitehorn		return EINVAL;
218217309Snwhitehorn	}
219251843Sbapt	if (info->flag & FWTI_REQ)
220217309Snwhitehorn		xferq = fc->atq;
221217309Snwhitehorn	else
222217309Snwhitehorn		xferq = fc->ats;
223217309Snwhitehorn	len = info->hdr_len;
224217309Snwhitehorn	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
225217309Snwhitehorn		printf("send.pay_len > maxrec\n");
226217309Snwhitehorn		return EINVAL;
227217309Snwhitehorn	}
228217309Snwhitehorn	if (info->flag & FWTI_BLOCK_STR)
229217309Snwhitehorn		len = fp->mode.stream.len;
230217309Snwhitehorn	else if (info->flag & FWTI_BLOCK_ASY)
231217309Snwhitehorn		len = fp->mode.rresb.len;
232217309Snwhitehorn	else
233255852Sdteske		len = 0;
234224014Snwhitehorn	if (len != xfer->send.pay_len){
235224014Snwhitehorn		printf("len(%d) != send.pay_len(%d) %s(%x)\n",
236217309Snwhitehorn		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
237217309Snwhitehorn		return EINVAL;
238217309Snwhitehorn	}
239217309Snwhitehorn
240217309Snwhitehorn	if(xferq->start == NULL){
241217309Snwhitehorn		printf("xferq->start == NULL\n");
242217309Snwhitehorn		return EINVAL;
243217309Snwhitehorn	}
244217309Snwhitehorn	if(!(xferq->queued < xferq->maxq)){
245217309Snwhitehorn		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
246217309Snwhitehorn			xferq->queued);
247251843Sbapt		return EINVAL;
248217309Snwhitehorn	}
249217309Snwhitehorn
250217309Snwhitehorn	if (info->flag & FWTI_TLABEL) {
251217309Snwhitehorn		if ((tl = fw_get_tlabel(fc, xfer)) == -1)
252217309Snwhitehorn			return EAGAIN;
253217309Snwhitehorn		fp->mode.hdr.tlrt = tl << 2;
254217309Snwhitehorn	}
255217309Snwhitehorn
256251843Sbapt	xfer->tl = tl;
257217309Snwhitehorn	xfer->resp = 0;
258217309Snwhitehorn	xfer->fc = fc;
259217309Snwhitehorn	xfer->q = xferq;
260220749Snwhitehorn	xfer->retry_req = fw_asybusy;
261224014Snwhitehorn
262217309Snwhitehorn	fw_asystart(xfer);
263217309Snwhitehorn	return err;
264217309Snwhitehorn}
265217309Snwhitehorn/*
266217309Snwhitehorn * Wakeup blocked process.
267217309Snwhitehorn */
268217309Snwhitehornvoid
269217309Snwhitehornfw_asy_callback(struct fw_xfer *xfer){
270217309Snwhitehorn	wakeup(xfer);
271217309Snwhitehorn	return;
272220749Snwhitehorn}
273217309Snwhitehorn/*
274217309Snwhitehorn * Postpone to later retry.
275217309Snwhitehorn */
276220749Snwhitehornvoid fw_asybusy(struct fw_xfer *xfer){
277220749Snwhitehorn	printf("fw_asybusy\n");
278217309Snwhitehorn/*
279217309Snwhitehorn	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
280217309Snwhitehorn*/
281217309Snwhitehorn#if 0
282217309Snwhitehorn	DELAY(20000);
283217309Snwhitehorn#endif
284217309Snwhitehorn	fw_asystart(xfer);
285217309Snwhitehorn	return;
286217309Snwhitehorn}
287217309Snwhitehorn
288217309Snwhitehorn/*
289217309Snwhitehorn * Async. request with given xfer structure.
290217309Snwhitehorn */
291217309Snwhitehornstatic void
292217309Snwhitehornfw_asystart(struct fw_xfer *xfer)
293217309Snwhitehorn{
294217309Snwhitehorn	struct firewire_comm *fc = xfer->fc;
295217309Snwhitehorn	int s;
296217309Snwhitehorn	if(xfer->retry++ >= fc->max_asyretry){
297217309Snwhitehorn		device_printf(fc->bdev, "max_asyretry exceeded\n");
298217309Snwhitehorn		xfer->resp = EBUSY;
299217309Snwhitehorn		xfer->state = FWXF_BUSY;
300217309Snwhitehorn		xfer->act.hand(xfer);
301217309Snwhitehorn		return;
302224014Snwhitehorn	}
303217309Snwhitehorn#if 0 /* XXX allow bus explore packets only after bus rest */
304217309Snwhitehorn	if (fc->status < FWBUSEXPLORE) {
305217309Snwhitehorn		xfer->resp = EAGAIN;
306217309Snwhitehorn		xfer->state = FWXF_BUSY;
307251843Sbapt		if (xfer->act.hand != NULL)
308251843Sbapt			xfer->act.hand(xfer);
309251843Sbapt		return;
310251843Sbapt	}
311251843Sbapt#endif
312251843Sbapt	microtime(&xfer->tv);
313251843Sbapt	s = splfw();
314251843Sbapt	xfer->state = FWXF_INQ;
315251843Sbapt	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
316251843Sbapt	xfer->q->queued ++;
317251843Sbapt	splx(s);
318251843Sbapt	/* XXX just queue for mbuf */
319251843Sbapt	if (xfer->mbuf == NULL)
320251843Sbapt		xfer->q->start(fc);
321251843Sbapt	return;
322251843Sbapt}
323251843Sbapt
324251843Sbaptstatic void
325251843Sbaptfirewire_identify(driver_t *driver, device_t parent)
326251843Sbapt{
327251843Sbapt	BUS_ADD_CHILD(parent, 0, "firewire", -1);
328251843Sbapt}
329251843Sbapt
330251843Sbaptstatic int
331251843Sbaptfirewire_probe(device_t dev)
332251843Sbapt{
333251843Sbapt	device_set_desc(dev, "IEEE1394(FireWire) bus");
334251843Sbapt	return (0);
335217309Snwhitehorn}
336217309Snwhitehorn
337217309Snwhitehornstatic void
338217309Snwhitehornfirewire_xfer_timeout(struct firewire_comm *fc)
339217309Snwhitehorn{
340217309Snwhitehorn	struct fw_xfer *xfer;
341217309Snwhitehorn	struct tlabel *tl;
342217309Snwhitehorn	struct timeval tv;
343217309Snwhitehorn	struct timeval split_timeout;
344217309Snwhitehorn	int i, s;
345217309Snwhitehorn
346217309Snwhitehorn	split_timeout.tv_sec = 0;
347217309Snwhitehorn	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
348217309Snwhitehorn
349217309Snwhitehorn	microtime(&tv);
350217309Snwhitehorn	timevalsub(&tv, &split_timeout);
351217309Snwhitehorn
352217309Snwhitehorn	s = splfw();
353217309Snwhitehorn	for (i = 0; i < 0x40; i ++) {
354217309Snwhitehorn		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
355217309Snwhitehorn			xfer = tl->xfer;
356217309Snwhitehorn			if (timevalcmp(&xfer->tv, &tv, >))
357217309Snwhitehorn				/* the rests are newer than this */
358217309Snwhitehorn				break;
359217309Snwhitehorn			if (xfer->state == FWXF_START)
360217309Snwhitehorn				/* not sent yet */
361217309Snwhitehorn				break;
362217309Snwhitehorn			device_printf(fc->bdev,
363217309Snwhitehorn				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
364217309Snwhitehorn				xfer->send.hdr.mode.hdr.dst, i, xfer->state);
365217309Snwhitehorn			xfer->resp = ETIMEDOUT;
366217309Snwhitehorn			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
367217309Snwhitehorn			fw_xfer_done(xfer);
368217309Snwhitehorn		}
369217309Snwhitehorn	}
370217309Snwhitehorn	splx(s);
371220749Snwhitehorn}
372217309Snwhitehorn
373217309Snwhitehorn#define WATCHDOC_HZ 10
374217309Snwhitehornstatic void
375217309Snwhitehornfirewire_watchdog(void *arg)
376217309Snwhitehorn{
377220749Snwhitehorn	struct firewire_comm *fc;
378217309Snwhitehorn	static int watchdoc_clock = 0;
379217309Snwhitehorn
380217309Snwhitehorn	fc = (struct firewire_comm *)arg;
381217309Snwhitehorn
382217309Snwhitehorn	/*
383217309Snwhitehorn	 * At boot stage, the device interrupt is disabled and
384217309Snwhitehorn	 * We encounter a timeout easily. To avoid this,
385217309Snwhitehorn	 * ignore clock interrupt for a while.
386217309Snwhitehorn	 */
387217309Snwhitehorn	if (watchdoc_clock > WATCHDOC_HZ * 15) {
388217309Snwhitehorn		firewire_xfer_timeout(fc);
389217309Snwhitehorn		fc->timeout(fc);
390217309Snwhitehorn	} else
391217309Snwhitehorn		watchdoc_clock ++;
392217309Snwhitehorn
393217309Snwhitehorn	callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ,
394217309Snwhitehorn			(void *)firewire_watchdog, (void *)fc);
395217309Snwhitehorn}
396217309Snwhitehorn
397217309Snwhitehorn/*
398217309Snwhitehorn * The attach routine.
399217309Snwhitehorn */
400217309Snwhitehornstatic int
401217309Snwhitehornfirewire_attach(device_t dev)
402217309Snwhitehorn{
403217309Snwhitehorn	int unit;
404217309Snwhitehorn	struct firewire_softc *sc = device_get_softc(dev);
405220749Snwhitehorn	device_t pa = device_get_parent(dev);
406217309Snwhitehorn	struct firewire_comm *fc;
407217309Snwhitehorn
408217309Snwhitehorn	fc = (struct firewire_comm *)device_get_softc(pa);
409217309Snwhitehorn	sc->fc = fc;
410217309Snwhitehorn	fc->status = FWBUSNOTREADY;
411217309Snwhitehorn
412217309Snwhitehorn	unit = device_get_unit(dev);
413217309Snwhitehorn	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
414217309Snwhitehorn
415220749Snwhitehorn	fwdev_makedev(sc);
416220749Snwhitehorn
417217309Snwhitehorn	CALLOUT_INIT(&sc->fc->timeout_callout);
418217309Snwhitehorn	CALLOUT_INIT(&sc->fc->bmr_callout);
419217309Snwhitehorn	CALLOUT_INIT(&sc->fc->retry_probe_callout);
420217309Snwhitehorn	CALLOUT_INIT(&sc->fc->busprobe_callout);
421217309Snwhitehorn
422217309Snwhitehorn	callout_reset(&sc->fc->timeout_callout, hz,
423217309Snwhitehorn			(void *)firewire_watchdog, (void *)sc->fc);
424217309Snwhitehorn
425217309Snwhitehorn	/* Locate our children */
426217309Snwhitehorn	bus_generic_probe(dev);
427217309Snwhitehorn
428217309Snwhitehorn	/* launch attachement of the added children */
429217309Snwhitehorn	bus_generic_attach(dev);
430217309Snwhitehorn
431217309Snwhitehorn	/* bus_reset */
432217309Snwhitehorn	fw_busreset(fc);
433217309Snwhitehorn	fc->ibr(fc);
434217309Snwhitehorn
435217309Snwhitehorn	return 0;
436217309Snwhitehorn}
437217309Snwhitehorn
438217309Snwhitehorn/*
439217309Snwhitehorn * Attach it as child.
440217309Snwhitehorn */
441217309Snwhitehornstatic device_t
442217309Snwhitehornfirewire_add_child(device_t dev, int order, const char *name, int unit)
443217309Snwhitehorn{
444217309Snwhitehorn        device_t child;
445217309Snwhitehorn	struct firewire_softc *sc;
446217309Snwhitehorn
447217309Snwhitehorn	sc = (struct firewire_softc *)device_get_softc(dev);
448217309Snwhitehorn	child = device_add_child(dev, name, unit);
449217309Snwhitehorn	if (child) {
450217309Snwhitehorn		device_set_ivars(child, sc->fc);
451217309Snwhitehorn		device_probe_and_attach(child);
452217309Snwhitehorn	}
453217309Snwhitehorn
454217309Snwhitehorn	return child;
455217309Snwhitehorn}
456217309Snwhitehorn
457220749Snwhitehornstatic int
458217309Snwhitehornfirewire_resume(device_t dev)
459217309Snwhitehorn{
460217309Snwhitehorn	struct firewire_softc *sc;
461217309Snwhitehorn
462217309Snwhitehorn	sc = (struct firewire_softc *)device_get_softc(dev);
463217309Snwhitehorn	sc->fc->status = FWBUSNOTREADY;
464217309Snwhitehorn
465217309Snwhitehorn	bus_generic_resume(dev);
466217309Snwhitehorn
467217309Snwhitehorn	return(0);
468217309Snwhitehorn}
469217309Snwhitehorn
470217309Snwhitehorn/*
471217309Snwhitehorn * Dettach it.
472217309Snwhitehorn */
473251843Sbaptstatic int
474251843Sbaptfirewire_detach(device_t dev)
475251843Sbapt{
476251843Sbapt	struct firewire_softc *sc;
477251843Sbapt	struct csrdir *csrd, *next;
478251843Sbapt	struct fw_device *fwdev, *fwdev_next;
479217309Snwhitehorn	int err;
480217309Snwhitehorn
481217309Snwhitehorn	sc = (struct firewire_softc *)device_get_softc(dev);
482217309Snwhitehorn	if ((err = fwdev_destroydev(sc)) != 0)
483217309Snwhitehorn		return err;
484217309Snwhitehorn
485217309Snwhitehorn	if ((err = bus_generic_detach(dev)) != 0)
486217309Snwhitehorn		return err;
487217309Snwhitehorn
488217309Snwhitehorn	callout_stop(&sc->fc->timeout_callout);
489217309Snwhitehorn	callout_stop(&sc->fc->bmr_callout);
490217309Snwhitehorn	callout_stop(&sc->fc->retry_probe_callout);
491217309Snwhitehorn	callout_stop(&sc->fc->busprobe_callout);
492217309Snwhitehorn
493217309Snwhitehorn	/* XXX xfree_free and untimeout on all xfers */
494217309Snwhitehorn	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
495217309Snwhitehorn							fwdev = fwdev_next) {
496220749Snwhitehorn		fwdev_next = STAILQ_NEXT(fwdev, link);
497251843Sbapt		free(fwdev, M_FW);
498251843Sbapt	}
499251843Sbapt	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
500251843Sbapt		next = SLIST_NEXT(csrd, link);
501251843Sbapt		free(csrd, M_FW);
502217309Snwhitehorn	}
503217309Snwhitehorn	free(sc->fc->topology_map, M_FW);
504217309Snwhitehorn	free(sc->fc->speed_map, M_FW);
505217309Snwhitehorn	free(sc->fc->crom_src_buf, M_FW);
506217309Snwhitehorn	return(0);
507217309Snwhitehorn}
508217309Snwhitehorn#if 0
509217309Snwhitehornstatic int
510217309Snwhitehornfirewire_shutdown( device_t dev )
511251843Sbapt{
512217309Snwhitehorn	return 0;
513217309Snwhitehorn}
514217309Snwhitehorn#endif
515217309Snwhitehorn
516217309Snwhitehorn
517217309Snwhitehornstatic void
518251843Sbaptfw_xferq_drain(struct fw_xferq *xferq)
519251843Sbapt{
520217309Snwhitehorn	struct fw_xfer *xfer;
521217309Snwhitehorn
522217309Snwhitehorn	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
523251843Sbapt		STAILQ_REMOVE_HEAD(&xferq->q, link);
524217309Snwhitehorn		xferq->queued --;
525217309Snwhitehorn		xfer->resp = EAGAIN;
526217309Snwhitehorn		fw_xfer_done(xfer);
527217309Snwhitehorn	}
528217309Snwhitehorn}
529217309Snwhitehorn
530217309Snwhitehornvoid
531217309Snwhitehornfw_drain_txq(struct firewire_comm *fc)
532217309Snwhitehorn{
533217309Snwhitehorn	int i;
534217309Snwhitehorn
535217309Snwhitehorn	fw_xferq_drain(fc->atq);
536217309Snwhitehorn	fw_xferq_drain(fc->ats);
537217309Snwhitehorn	for(i = 0; i < fc->nisodma; i++)
538217309Snwhitehorn		fw_xferq_drain(fc->it[i]);
539217309Snwhitehorn}
540217309Snwhitehorn
541217309Snwhitehornstatic void
542217309Snwhitehornfw_reset_csr(struct firewire_comm *fc)
543217309Snwhitehorn{
544217309Snwhitehorn	int i;
545217309Snwhitehorn
546217309Snwhitehorn	CSRARC(fc, STATE_CLEAR)
547217309Snwhitehorn			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
548217309Snwhitehorn	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
549217309Snwhitehorn	CSRARC(fc, NODE_IDS) = 0x3f;
550217309Snwhitehorn
551217309Snwhitehorn	CSRARC(fc, TOPO_MAP + 8) = 0;
552217309Snwhitehorn	fc->irm = -1;
553217309Snwhitehorn
554217309Snwhitehorn	fc->max_node = -1;
555217309Snwhitehorn
556217309Snwhitehorn	for(i = 2; i < 0x100/4 - 2 ; i++){
557217309Snwhitehorn		CSRARC(fc, SPED_MAP + i * 4) = 0;
558217309Snwhitehorn	}
559217309Snwhitehorn	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
560217309Snwhitehorn	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
561217309Snwhitehorn	CSRARC(fc, RESET_START) = 0;
562217309Snwhitehorn	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
563217309Snwhitehorn	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
564217309Snwhitehorn	CSRARC(fc, CYCLE_TIME) = 0x0;
565217309Snwhitehorn	CSRARC(fc, BUS_TIME) = 0x0;
566217309Snwhitehorn	CSRARC(fc, BUS_MGR_ID) = 0x3f;
567217309Snwhitehorn	CSRARC(fc, BANDWIDTH_AV) = 4915;
568217309Snwhitehorn	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
569217309Snwhitehorn	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
570217309Snwhitehorn	CSRARC(fc, IP_CHANNELS) = (1 << 31);
571217309Snwhitehorn
572217309Snwhitehorn	CSRARC(fc, CONF_ROM) = 0x04 << 24;
573217309Snwhitehorn	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
574217309Snwhitehorn	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
575217309Snwhitehorn				1 << 28 | 0xff << 16 | 0x09 << 8;
576217309Snwhitehorn	CSRARC(fc, CONF_ROM + 0xc) = 0;
577251843Sbapt
578217309Snwhitehorn/* DV depend CSRs see blue book */
579251843Sbapt	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
580251843Sbapt	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
581251843Sbapt
582251843Sbapt	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
583251843Sbapt	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
584251843Sbapt}
585251843Sbapt
586251843Sbaptstatic void
587251843Sbaptfw_init_crom(struct firewire_comm *fc)
588217309Snwhitehorn{
589217309Snwhitehorn	struct crom_src *src;
590217309Snwhitehorn
591217309Snwhitehorn	fc->crom_src_buf = (struct crom_src_buf *)
592217309Snwhitehorn		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
593217309Snwhitehorn	if (fc->crom_src_buf == NULL)
594217309Snwhitehorn		return;
595217309Snwhitehorn
596217309Snwhitehorn	src = &fc->crom_src_buf->src;
597217309Snwhitehorn	bzero(src, sizeof(struct crom_src));
598217309Snwhitehorn
599217309Snwhitehorn	/* BUS info sample */
600217309Snwhitehorn	src->hdr.info_len = 4;
601217309Snwhitehorn
602217309Snwhitehorn	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
603217309Snwhitehorn
604217309Snwhitehorn	src->businfo.irmc = 1;
605217309Snwhitehorn	src->businfo.cmc = 1;
606217309Snwhitehorn	src->businfo.isc = 1;
607217309Snwhitehorn	src->businfo.bmc = 1;
608217309Snwhitehorn	src->businfo.pmc = 0;
609217309Snwhitehorn	src->businfo.cyc_clk_acc = 100;
610217309Snwhitehorn	src->businfo.max_rec = fc->maxrec;
611217309Snwhitehorn	src->businfo.max_rom = MAXROM_4;
612217309Snwhitehorn	src->businfo.generation = 1;
613217309Snwhitehorn	src->businfo.link_spd = fc->speed;
614217309Snwhitehorn
615217309Snwhitehorn	src->businfo.eui64.hi = fc->eui.hi;
616217309Snwhitehorn	src->businfo.eui64.lo = fc->eui.lo;
617217309Snwhitehorn
618217309Snwhitehorn	STAILQ_INIT(&src->chunk_list);
619217309Snwhitehorn
620217309Snwhitehorn	fc->crom_src = src;
621217309Snwhitehorn	fc->crom_root = &fc->crom_src_buf->root;
622217309Snwhitehorn}
623217309Snwhitehorn
624217309Snwhitehornstatic void
625217309Snwhitehornfw_reset_crom(struct firewire_comm *fc)
626217309Snwhitehorn{
627217309Snwhitehorn	struct crom_src_buf *buf;
628217309Snwhitehorn	struct crom_src *src;
629217309Snwhitehorn	struct crom_chunk *root;
630217309Snwhitehorn
631217309Snwhitehorn	if (fc->crom_src_buf == NULL)
632217309Snwhitehorn		fw_init_crom(fc);
633217309Snwhitehorn
634220749Snwhitehorn	buf =  fc->crom_src_buf;
635220749Snwhitehorn	src = fc->crom_src;
636217309Snwhitehorn	root = fc->crom_root;
637217309Snwhitehorn
638217309Snwhitehorn	STAILQ_INIT(&src->chunk_list);
639217309Snwhitehorn
640217309Snwhitehorn	bzero(root, sizeof(struct crom_chunk));
641217309Snwhitehorn	crom_add_chunk(src, NULL, root, 0);
642217309Snwhitehorn	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
643217309Snwhitehorn	/* private company_id */
644217309Snwhitehorn	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
645217309Snwhitehorn#ifdef __DragonFly__
646217309Snwhitehorn	crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
647217309Snwhitehorn	crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
648217309Snwhitehorn#else
649217309Snwhitehorn	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
650217309Snwhitehorn	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
651217309Snwhitehorn#endif
652217309Snwhitehorn	crom_add_simple_text(src, root, &buf->hw, hostname);
653217309Snwhitehorn}
654217309Snwhitehorn
655217309Snwhitehorn/*
656217309Snwhitehorn * Called after bus reset.
657217309Snwhitehorn */
658217309Snwhitehornvoid
659217309Snwhitehornfw_busreset(struct firewire_comm *fc)
660217309Snwhitehorn{
661217309Snwhitehorn	struct firewire_dev_comm *fdc;
662217309Snwhitehorn	struct crom_src *src;
663217309Snwhitehorn	device_t *devlistp;
664217309Snwhitehorn	void *newrom;
665217309Snwhitehorn	int i, devcnt;
666217309Snwhitehorn
667217309Snwhitehorn	switch(fc->status){
668217309Snwhitehorn	case FWBUSMGRELECT:
669217309Snwhitehorn		callout_stop(&fc->bmr_callout);
670217309Snwhitehorn		break;
671217309Snwhitehorn	default:
672217309Snwhitehorn		break;
673217309Snwhitehorn	}
674217309Snwhitehorn	fc->status = FWBUSRESET;
675217309Snwhitehorn	fw_reset_csr(fc);
676217309Snwhitehorn	fw_reset_crom(fc);
677217309Snwhitehorn
678217309Snwhitehorn	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
679217309Snwhitehorn		for( i = 0 ; i < devcnt ; i++)
680217309Snwhitehorn			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
681217309Snwhitehorn				fdc = device_get_softc(devlistp[i]);
682217309Snwhitehorn				if (fdc->post_busreset != NULL)
683217309Snwhitehorn					fdc->post_busreset(fdc);
684217309Snwhitehorn			}
685217309Snwhitehorn		free(devlistp, M_TEMP);
686217309Snwhitehorn	}
687217309Snwhitehorn
688217309Snwhitehorn	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
689217309Snwhitehorn	src = &fc->crom_src_buf->src;
690217309Snwhitehorn	crom_load(src, (uint32_t *)newrom, CROMSIZE);
691217309Snwhitehorn	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
692217309Snwhitehorn		/* bump generation and reload */
693217309Snwhitehorn		src->businfo.generation ++;
694217309Snwhitehorn		/* generation must be between 0x2 and 0xF */
695217309Snwhitehorn		if (src->businfo.generation < 2)
696217309Snwhitehorn			src->businfo.generation ++;
697217309Snwhitehorn		crom_load(src, (uint32_t *)newrom, CROMSIZE);
698217309Snwhitehorn		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
699217309Snwhitehorn	}
700217309Snwhitehorn	free(newrom, M_FW);
701217309Snwhitehorn}
702217309Snwhitehorn
703217309Snwhitehorn/* Call once after reboot */
704217309Snwhitehornvoid fw_init(struct firewire_comm *fc)
705217309Snwhitehorn{
706217309Snwhitehorn	int i;
707217309Snwhitehorn	struct csrdir *csrd;
708217309Snwhitehorn#ifdef FW_VMACCESS
709220749Snwhitehorn	struct fw_xfer *xfer;
710220749Snwhitehorn	struct fw_bind *fwb;
711217309Snwhitehorn#endif
712217309Snwhitehorn
713217309Snwhitehorn	fc->max_asyretry = FW_MAXASYRTY;
714217309Snwhitehorn
715217309Snwhitehorn	fc->arq->queued = 0;
716217309Snwhitehorn	fc->ars->queued = 0;
717220749Snwhitehorn	fc->atq->queued = 0;
718220749Snwhitehorn	fc->ats->queued = 0;
719220749Snwhitehorn
720220749Snwhitehorn	fc->arq->buf = NULL;
721217309Snwhitehorn	fc->ars->buf = NULL;
722217309Snwhitehorn	fc->atq->buf = NULL;
723217309Snwhitehorn	fc->ats->buf = NULL;
724217309Snwhitehorn
725220749Snwhitehorn	fc->arq->flag = 0;
726220749Snwhitehorn	fc->ars->flag = 0;
727220749Snwhitehorn	fc->atq->flag = 0;
728220749Snwhitehorn	fc->ats->flag = 0;
729220749Snwhitehorn
730220749Snwhitehorn	STAILQ_INIT(&fc->atq->q);
731220749Snwhitehorn	STAILQ_INIT(&fc->ats->q);
732220749Snwhitehorn
733220749Snwhitehorn	for( i = 0 ; i < fc->nisodma ; i ++ ){
734220749Snwhitehorn		fc->it[i]->queued = 0;
735220749Snwhitehorn		fc->ir[i]->queued = 0;
736217309Snwhitehorn
737217309Snwhitehorn		fc->it[i]->start = NULL;
738217309Snwhitehorn		fc->ir[i]->start = NULL;
739217309Snwhitehorn
740217309Snwhitehorn		fc->it[i]->buf = NULL;
741217309Snwhitehorn		fc->ir[i]->buf = NULL;
742217309Snwhitehorn
743217309Snwhitehorn		fc->it[i]->flag = FWXFERQ_STREAM;
744217309Snwhitehorn		fc->ir[i]->flag = FWXFERQ_STREAM;
745217309Snwhitehorn
746217309Snwhitehorn		STAILQ_INIT(&fc->it[i]->q);
747217309Snwhitehorn		STAILQ_INIT(&fc->ir[i]->q);
748217309Snwhitehorn
749217309Snwhitehorn		STAILQ_INIT(&fc->it[i]->binds);
750217309Snwhitehorn		STAILQ_INIT(&fc->ir[i]->binds);
751217309Snwhitehorn	}
752217309Snwhitehorn
753217309Snwhitehorn	fc->arq->maxq = FWMAXQUEUE;
754217309Snwhitehorn	fc->ars->maxq = FWMAXQUEUE;
755217309Snwhitehorn	fc->atq->maxq = FWMAXQUEUE;
756217309Snwhitehorn	fc->ats->maxq = FWMAXQUEUE;
757217309Snwhitehorn
758217309Snwhitehorn	for( i = 0 ; i < fc->nisodma ; i++){
759217309Snwhitehorn		fc->ir[i]->maxq = FWMAXQUEUE;
760217309Snwhitehorn		fc->it[i]->maxq = FWMAXQUEUE;
761217309Snwhitehorn	}
762217309Snwhitehorn/* Initialize csr registers */
763217309Snwhitehorn	fc->topology_map = (struct fw_topology_map *)malloc(
764217309Snwhitehorn				sizeof(struct fw_topology_map),
765217309Snwhitehorn				M_FW, M_NOWAIT | M_ZERO);
766217309Snwhitehorn	fc->speed_map = (struct fw_speed_map *)malloc(
767217309Snwhitehorn				sizeof(struct fw_speed_map),
768217309Snwhitehorn				M_FW, M_NOWAIT | M_ZERO);
769217309Snwhitehorn	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
770217309Snwhitehorn	CSRARC(fc, TOPO_MAP + 4) = 1;
771217309Snwhitehorn	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
772217309Snwhitehorn	CSRARC(fc, SPED_MAP + 4) = 1;
773217309Snwhitehorn
774217309Snwhitehorn	STAILQ_INIT(&fc->devices);
775217309Snwhitehorn
776217309Snwhitehorn/* Initialize csr ROM work space */
777217309Snwhitehorn	SLIST_INIT(&fc->ongocsr);
778217309Snwhitehorn	SLIST_INIT(&fc->csrfree);
779217309Snwhitehorn	for( i = 0 ; i < FWMAXCSRDIR ; i++){
780217309Snwhitehorn		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
781217309Snwhitehorn		if(csrd == NULL) break;
782217309Snwhitehorn		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
783217309Snwhitehorn	}
784217309Snwhitehorn
785217309Snwhitehorn/* Initialize Async handlers */
786217309Snwhitehorn	STAILQ_INIT(&fc->binds);
787217309Snwhitehorn	for( i = 0 ; i < 0x40 ; i++){
788217309Snwhitehorn		STAILQ_INIT(&fc->tlabels[i]);
789217309Snwhitehorn	}
790217309Snwhitehorn
791217309Snwhitehorn/* DV depend CSRs see blue book */
792217309Snwhitehorn#if 0
793217309Snwhitehorn	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
794217309Snwhitehorn	CSRARC(fc, oPCR) = 0x8000007a;
795217309Snwhitehorn	for(i = 4 ; i < 0x7c/4 ; i+=4){
796217309Snwhitehorn		CSRARC(fc, i + oPCR) = 0x8000007a;
797217309Snwhitehorn	}
798217309Snwhitehorn
799217309Snwhitehorn	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
800217309Snwhitehorn	CSRARC(fc, iPCR) = 0x803f0000;
801217309Snwhitehorn	for(i = 4 ; i < 0x7c/4 ; i+=4){
802217309Snwhitehorn		CSRARC(fc, i + iPCR) = 0x0;
803217309Snwhitehorn	}
804217309Snwhitehorn#endif
805217309Snwhitehorn
806217309Snwhitehorn	fc->crom_src_buf = NULL;
807217309Snwhitehorn
808217309Snwhitehorn#ifdef FW_VMACCESS
809217309Snwhitehorn	xfer = fw_xfer_alloc();
810217309Snwhitehorn	if(xfer == NULL) return;
811217309Snwhitehorn
812217309Snwhitehorn	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
813217309Snwhitehorn	if(fwb == NULL){
814217309Snwhitehorn		fw_xfer_free(xfer);
815217309Snwhitehorn		return;
816217309Snwhitehorn	}
817217309Snwhitehorn	xfer->act.hand = fw_vmaccess;
818217309Snwhitehorn	xfer->fc = fc;
819217309Snwhitehorn	xfer->sc = NULL;
820217309Snwhitehorn
821217309Snwhitehorn	fwb->start_hi = 0x2;
822217309Snwhitehorn	fwb->start_lo = 0;
823217309Snwhitehorn	fwb->addrlen = 0xffffffff;
824217309Snwhitehorn	fwb->xfer = xfer;
825217309Snwhitehorn	fw_bindadd(fc, fwb);
826217309Snwhitehorn#endif
827217309Snwhitehorn}
828217309Snwhitehorn
829217309Snwhitehorn#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
830217309Snwhitehorn    ((fwb)->end < (addr))?1:0)
831217309Snwhitehorn
832217309Snwhitehorn/*
833217309Snwhitehorn * To lookup bound process from IEEE1394 address.
834217309Snwhitehorn */
835217309Snwhitehornstruct fw_bind *
836217309Snwhitehornfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
837217309Snwhitehorn{
838217309Snwhitehorn	u_int64_t addr;
839217309Snwhitehorn	struct fw_bind *tfw;
840217309Snwhitehorn
841217309Snwhitehorn	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
842217309Snwhitehorn	STAILQ_FOREACH(tfw, &fc->binds, fclist)
843217309Snwhitehorn		if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0)
844217309Snwhitehorn			return(tfw);
845217309Snwhitehorn	return(NULL);
846217309Snwhitehorn}
847251843Sbapt
848251843Sbapt/*
849251843Sbapt * To bind IEEE1394 address block to process.
850251843Sbapt */
851251843Sbaptint
852251843Sbaptfw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
853251843Sbapt{
854251843Sbapt	struct fw_bind *tfw, *prev = NULL;
855251843Sbapt
856251843Sbapt	if (fwb->start > fwb->end) {
857251843Sbapt		printf("%s: invalid range\n", __func__);
858251843Sbapt		return EINVAL;
859251843Sbapt	}
860251843Sbapt
861251843Sbapt	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
862251843Sbapt		if (fwb->end < tfw->start)
863251843Sbapt			break;
864251843Sbapt		prev = tfw;
865251843Sbapt	}
866251843Sbapt	if (prev == NULL) {
867251843Sbapt		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
868251843Sbapt		goto out;
869251843Sbapt	}
870251843Sbapt	if (prev->end < fwb->start) {
871251843Sbapt		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
872251843Sbapt		goto out;
873251843Sbapt	}
874251843Sbapt
875251843Sbapt	printf("%s: bind failed\n", __func__);
876251843Sbapt	return (EBUSY);
877251843Sbapt
878251843Sbaptout:
879251843Sbapt	if (fwb->act_type == FWACT_CH)
880251843Sbapt		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
881251843Sbapt	return (0);
882251843Sbapt}
883251843Sbapt
884251843Sbapt/*
885251843Sbapt * To free IEEE1394 address block.
886251843Sbapt */
887251843Sbaptint
888251843Sbaptfw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
889251843Sbapt{
890251843Sbapt#if 0
891251843Sbapt	struct fw_xfer *xfer, *next;
892251843Sbapt#endif
893251843Sbapt	struct fw_bind *tfw;
894251843Sbapt	int s;
895251843Sbapt
896251843Sbapt	s = splfw();
897251843Sbapt	STAILQ_FOREACH(tfw, &fc->binds, fclist)
898251843Sbapt		if (tfw == fwb) {
899251843Sbapt			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
900251843Sbapt			goto found;
901251843Sbapt		}
902251843Sbapt
903251843Sbapt	printf("%s: no such binding\n", __func__);
904251843Sbapt	splx(s);
905251843Sbapt	return (1);
906251843Sbaptfound:
907251843Sbapt#if 0
908251843Sbapt	/* shall we do this? */
909251843Sbapt	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
910251843Sbapt		next = STAILQ_NEXT(xfer, link);
911251843Sbapt		fw_xfer_free(xfer);
912251843Sbapt	}
913251843Sbapt	STAILQ_INIT(&fwb->xferlist);
914251843Sbapt#endif
915251843Sbapt
916217309Snwhitehorn	splx(s);
917217309Snwhitehorn	return 0;
918217309Snwhitehorn}
919217309Snwhitehorn
920217309Snwhitehorn/*
921217309Snwhitehorn * To free transaction label.
922217309Snwhitehorn */
923217309Snwhitehornstatic void
924217309Snwhitehornfw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
925217309Snwhitehorn{
926217309Snwhitehorn	struct tlabel *tl;
927217309Snwhitehorn	int s = splfw();
928217309Snwhitehorn
929217309Snwhitehorn	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
930217309Snwhitehorn		tl = STAILQ_NEXT(tl, link)){
931217309Snwhitehorn		if(tl->xfer == xfer){
932217309Snwhitehorn			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
933217309Snwhitehorn			free(tl, M_FW);
934217309Snwhitehorn			splx(s);
935217309Snwhitehorn			return;
936217309Snwhitehorn		}
937217309Snwhitehorn	}
938217309Snwhitehorn	splx(s);
939217309Snwhitehorn	return;
940217309Snwhitehorn}
941217309Snwhitehorn
942217309Snwhitehorn/*
943217309Snwhitehorn * To obtain XFER structure by transaction label.
944217309Snwhitehorn */
945217309Snwhitehornstatic struct fw_xfer *
946217309Snwhitehornfw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
947217309Snwhitehorn{
948217309Snwhitehorn	struct fw_xfer *xfer;
949217309Snwhitehorn	struct tlabel *tl;
950217309Snwhitehorn	int s = splfw();
951217309Snwhitehorn
952217309Snwhitehorn	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
953217309Snwhitehorn		tl = STAILQ_NEXT(tl, link)){
954217309Snwhitehorn		if(tl->xfer->send.hdr.mode.hdr.dst == node){
955217309Snwhitehorn			xfer = tl->xfer;
956217309Snwhitehorn			splx(s);
957217309Snwhitehorn			if (firewire_debug > 2)
958217309Snwhitehorn				printf("fw_tl2xfer: found tl=%d\n", tlabel);
959217309Snwhitehorn			return(xfer);
960217309Snwhitehorn		}
961217309Snwhitehorn	}
962217309Snwhitehorn	if (firewire_debug > 1)
963217309Snwhitehorn		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
964217309Snwhitehorn	splx(s);
965217309Snwhitehorn	return(NULL);
966217309Snwhitehorn}
967217309Snwhitehorn
968217309Snwhitehorn/*
969217309Snwhitehorn * To allocate IEEE1394 XFER structure.
970217309Snwhitehorn */
971217309Snwhitehornstruct fw_xfer *
972217309Snwhitehornfw_xfer_alloc(struct malloc_type *type)
973217309Snwhitehorn{
974217309Snwhitehorn	struct fw_xfer *xfer;
975217309Snwhitehorn
976217309Snwhitehorn	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
977217309Snwhitehorn	if (xfer == NULL)
978217309Snwhitehorn		return xfer;
979217309Snwhitehorn
980217309Snwhitehorn	xfer->malloc = type;
981217309Snwhitehorn
982217309Snwhitehorn	return xfer;
983217309Snwhitehorn}
984217309Snwhitehorn
985217309Snwhitehornstruct fw_xfer *
986217309Snwhitehornfw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
987217309Snwhitehorn{
988217309Snwhitehorn	struct fw_xfer *xfer;
989217309Snwhitehorn
990217309Snwhitehorn	xfer = fw_xfer_alloc(type);
991217309Snwhitehorn	if (xfer == NULL)
992217309Snwhitehorn		return(NULL);
993217309Snwhitehorn	xfer->send.pay_len = send_len;
994217309Snwhitehorn	xfer->recv.pay_len = recv_len;
995217309Snwhitehorn	if (send_len > 0) {
996217309Snwhitehorn		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
997217309Snwhitehorn		if (xfer->send.payload == NULL) {
998217309Snwhitehorn			fw_xfer_free(xfer);
999217309Snwhitehorn			return(NULL);
1000217309Snwhitehorn		}
1001217309Snwhitehorn	}
1002217309Snwhitehorn	if (recv_len > 0) {
1003224014Snwhitehorn		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1004220749Snwhitehorn		if (xfer->recv.payload == NULL) {
1005220749Snwhitehorn			if (xfer->send.payload != NULL)
1006220749Snwhitehorn				free(xfer->send.payload, type);
1007220749Snwhitehorn			fw_xfer_free(xfer);
1008220749Snwhitehorn			return(NULL);
1009220749Snwhitehorn		}
1010220749Snwhitehorn	}
1011220749Snwhitehorn	return(xfer);
1012220749Snwhitehorn}
1013220749Snwhitehorn
1014220749Snwhitehorn/*
1015220749Snwhitehorn * IEEE1394 XFER post process.
1016220749Snwhitehorn */
1017220749Snwhitehornvoid
1018220749Snwhitehornfw_xfer_done(struct fw_xfer *xfer)
1019220749Snwhitehorn{
1020220749Snwhitehorn	if (xfer->act.hand == NULL) {
1021220749Snwhitehorn		printf("act.hand == NULL\n");
1022220749Snwhitehorn		return;
1023224014Snwhitehorn	}
1024220749Snwhitehorn
1025217309Snwhitehorn	if (xfer->fc == NULL)
1026217309Snwhitehorn		panic("fw_xfer_done: why xfer->fc is NULL?");
1027220749Snwhitehorn
1028220749Snwhitehorn	xfer->act.hand(xfer);
1029220749Snwhitehorn}
1030220749Snwhitehorn
1031220749Snwhitehornvoid
1032220749Snwhitehornfw_xfer_unload(struct fw_xfer* xfer)
1033220749Snwhitehorn{
1034220749Snwhitehorn	int s;
1035220749Snwhitehorn
1036220749Snwhitehorn	if(xfer == NULL ) return;
1037220749Snwhitehorn	if(xfer->state == FWXF_INQ){
1038220749Snwhitehorn		printf("fw_xfer_free FWXF_INQ\n");
1039220749Snwhitehorn		s = splfw();
1040220749Snwhitehorn		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1041220749Snwhitehorn		xfer->q->queued --;
1042220749Snwhitehorn		splx(s);
1043220749Snwhitehorn	}
1044220749Snwhitehorn	if (xfer->fc != NULL) {
1045220749Snwhitehorn#if 1
1046220749Snwhitehorn		if(xfer->state == FWXF_START)
1047220749Snwhitehorn			/*
1048220749Snwhitehorn			 * This could happen if:
1049220749Snwhitehorn			 *  1. We call fwohci_arcv() before fwohci_txd().
1050220749Snwhitehorn			 *  2. firewire_watch() is called.
1051220749Snwhitehorn			 */
1052220749Snwhitehorn			printf("fw_xfer_free FWXF_START\n");
1053220749Snwhitehorn#endif
1054217309Snwhitehorn		fw_tl_free(xfer->fc, xfer);
1055217309Snwhitehorn	}
1056217309Snwhitehorn	xfer->state = FWXF_INIT;
1057217309Snwhitehorn	xfer->resp = 0;
1058217309Snwhitehorn	xfer->retry = 0;
1059217309Snwhitehorn}
1060217309Snwhitehorn/*
1061217309Snwhitehorn * To free IEEE1394 XFER structure.
1062217309Snwhitehorn */
1063217309Snwhitehornvoid
1064217309Snwhitehornfw_xfer_free_buf( struct fw_xfer* xfer)
1065217309Snwhitehorn{
1066217309Snwhitehorn	if (xfer == NULL) {
1067217309Snwhitehorn		printf("%s: xfer == NULL\n", __func__);
1068217309Snwhitehorn		return;
1069217309Snwhitehorn	}
1070217309Snwhitehorn	fw_xfer_unload(xfer);
1071217309Snwhitehorn	if(xfer->send.payload != NULL){
1072217309Snwhitehorn		free(xfer->send.payload, xfer->malloc);
1073217309Snwhitehorn	}
1074217309Snwhitehorn	if(xfer->recv.payload != NULL){
1075217309Snwhitehorn		free(xfer->recv.payload, xfer->malloc);
1076217309Snwhitehorn	}
1077217309Snwhitehorn	free(xfer, xfer->malloc);
1078217309Snwhitehorn}
1079217309Snwhitehorn
1080217309Snwhitehornvoid
1081217309Snwhitehornfw_xfer_free( struct fw_xfer* xfer)
1082217309Snwhitehorn{
1083217309Snwhitehorn	if (xfer == NULL) {
1084217309Snwhitehorn		printf("%s: xfer == NULL\n", __func__);
1085217309Snwhitehorn		return;
1086217309Snwhitehorn	}
1087217309Snwhitehorn	fw_xfer_unload(xfer);
1088217309Snwhitehorn	free(xfer, xfer->malloc);
1089217309Snwhitehorn}
1090217309Snwhitehorn
1091217309Snwhitehornvoid
1092217309Snwhitehornfw_asy_callback_free(struct fw_xfer *xfer)
1093217309Snwhitehorn{
1094217309Snwhitehorn#if 0
1095217309Snwhitehorn	printf("asyreq done state=%d resp=%d\n",
1096217309Snwhitehorn				xfer->state, xfer->resp);
1097217309Snwhitehorn#endif
1098217309Snwhitehorn	fw_xfer_free(xfer);
1099217309Snwhitehorn}
1100217309Snwhitehorn
1101217309Snwhitehorn/*
1102217309Snwhitehorn * To configure PHY.
1103217309Snwhitehorn */
1104217309Snwhitehornstatic void
1105217309Snwhitehornfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1106217309Snwhitehorn{
1107217309Snwhitehorn	struct fw_xfer *xfer;
1108217309Snwhitehorn	struct fw_pkt *fp;
1109217309Snwhitehorn
1110217309Snwhitehorn	fc->status = FWBUSPHYCONF;
1111220749Snwhitehorn
1112220749Snwhitehorn	xfer = fw_xfer_alloc(M_FWXFER);
1113217309Snwhitehorn	if (xfer == NULL)
1114217309Snwhitehorn		return;
1115217309Snwhitehorn	xfer->fc = fc;
1116217309Snwhitehorn	xfer->retry_req = fw_asybusy;
1117217309Snwhitehorn	xfer->act.hand = fw_asy_callback_free;
1118217309Snwhitehorn
1119217309Snwhitehorn	fp = &xfer->send.hdr;
1120217309Snwhitehorn	fp->mode.ld[1] = 0;
1121217309Snwhitehorn	if (root_node >= 0)
1122217309Snwhitehorn		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1123217309Snwhitehorn	if (gap_count >= 0)
1124217309Snwhitehorn		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1125217309Snwhitehorn	fp->mode.ld[2] = ~fp->mode.ld[1];
1126217309Snwhitehorn/* XXX Dangerous, how to pass PHY packet to device driver */
1127217309Snwhitehorn	fp->mode.common.tcode |= FWTCODE_PHY;
1128217309Snwhitehorn
1129217309Snwhitehorn	if (firewire_debug)
1130251843Sbapt		printf("send phy_config root_node=%d gap_count=%d\n",
1131217309Snwhitehorn						root_node, gap_count);
1132217309Snwhitehorn	fw_asyreq(fc, -1, xfer);
1133217309Snwhitehorn}
1134217309Snwhitehorn
1135251843Sbapt#if 0
1136217309Snwhitehorn/*
1137251843Sbapt * Dump self ID.
1138217309Snwhitehorn */
1139217309Snwhitehornstatic void
1140217309Snwhitehornfw_print_sid(uint32_t sid)
1141217309Snwhitehorn{
1142217309Snwhitehorn	union fw_self_id *s;
1143217309Snwhitehorn	s = (union fw_self_id *) &sid;
1144217309Snwhitehorn	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1145217309Snwhitehorn		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1146217309Snwhitehorn		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1147217309Snwhitehorn		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1148217309Snwhitehorn		s->p0.power_class, s->p0.port0, s->p0.port1,
1149217309Snwhitehorn		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1150217309Snwhitehorn}
1151217309Snwhitehorn#endif
1152217309Snwhitehorn
1153217309Snwhitehorn/*
1154217309Snwhitehorn * To receive self ID.
1155217309Snwhitehorn */
1156217309Snwhitehornvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1157217309Snwhitehorn{
1158217309Snwhitehorn	uint32_t *p;
1159217309Snwhitehorn	union fw_self_id *self_id;
1160217309Snwhitehorn	u_int i, j, node, c_port = 0, i_branch = 0;
1161217309Snwhitehorn
1162217309Snwhitehorn	fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1163217309Snwhitehorn	fc->status = FWBUSINIT;
1164217309Snwhitehorn	fc->max_node = fc->nodeid & 0x3f;
1165220749Snwhitehorn	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1166217309Snwhitehorn	fc->status = FWBUSCYMELECT;
1167217309Snwhitehorn	fc->topology_map->crc_len = 2;
1168217309Snwhitehorn	fc->topology_map->generation ++;
1169217309Snwhitehorn	fc->topology_map->self_id_count = 0;
1170217309Snwhitehorn	fc->topology_map->node_count = 0;
1171217309Snwhitehorn	fc->speed_map->generation ++;
1172217309Snwhitehorn	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1173217309Snwhitehorn	self_id = &fc->topology_map->self_id[0];
1174217309Snwhitehorn	for(i = 0; i < fc->sid_cnt; i ++){
1175217309Snwhitehorn		if (sid[1] != ~sid[0]) {
1176217309Snwhitehorn			printf("fw_sidrcv: invalid self-id packet\n");
1177217309Snwhitehorn			sid += 2;
1178217309Snwhitehorn			continue;
1179251843Sbapt		}
1180251843Sbapt		*self_id = *((union fw_self_id *)sid);
1181251843Sbapt		fc->topology_map->crc_len++;
1182251843Sbapt		if(self_id->p0.sequel == 0){
1183251843Sbapt			fc->topology_map->node_count ++;
1184251843Sbapt			c_port = 0;
1185251843Sbapt#if 0
1186251843Sbapt			fw_print_sid(sid[0]);
1187251843Sbapt#endif
1188251843Sbapt			node = self_id->p0.phy_id;
1189251843Sbapt			if(fc->max_node < node){
1190251843Sbapt				fc->max_node = self_id->p0.phy_id;
1191251843Sbapt			}
1192251843Sbapt			/* XXX I'm not sure this is the right speed_map */
1193251843Sbapt			fc->speed_map->speed[node][node]
1194251843Sbapt					= self_id->p0.phy_speed;
1195251843Sbapt			for (j = 0; j < node; j ++) {
1196251843Sbapt				fc->speed_map->speed[j][node]
1197251843Sbapt					= fc->speed_map->speed[node][j]
1198251843Sbapt					= min(fc->speed_map->speed[j][j],
1199251843Sbapt							self_id->p0.phy_speed);
1200251843Sbapt			}
1201251843Sbapt			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1202251843Sbapt			  (self_id->p0.link_active && self_id->p0.contender)) {
1203251843Sbapt				fc->irm = self_id->p0.phy_id;
1204251843Sbapt			}
1205251843Sbapt			if(self_id->p0.port0 >= 0x2){
1206251843Sbapt				c_port++;
1207251843Sbapt			}
1208251843Sbapt			if(self_id->p0.port1 >= 0x2){
1209251843Sbapt				c_port++;
1210251843Sbapt			}
1211251843Sbapt			if(self_id->p0.port2 >= 0x2){
1212251843Sbapt				c_port++;
1213251843Sbapt			}
1214251843Sbapt		}
1215251843Sbapt		if(c_port > 2){
1216217309Snwhitehorn			i_branch += (c_port - 2);
1217217309Snwhitehorn		}
1218217309Snwhitehorn		sid += 2;
1219217309Snwhitehorn		self_id++;
1220217309Snwhitehorn		fc->topology_map->self_id_count ++;
1221217309Snwhitehorn	}
1222217309Snwhitehorn	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1223217309Snwhitehorn	/* CRC */
1224217309Snwhitehorn	fc->topology_map->crc = fw_crc16(
1225217309Snwhitehorn			(uint32_t *)&fc->topology_map->generation,
1226217309Snwhitehorn			fc->topology_map->crc_len * 4);
1227217309Snwhitehorn	fc->speed_map->crc = fw_crc16(
1228217309Snwhitehorn			(uint32_t *)&fc->speed_map->generation,
1229217309Snwhitehorn			fc->speed_map->crc_len * 4);
1230217309Snwhitehorn	/* byteswap and copy to CSR */
1231217309Snwhitehorn	p = (uint32_t *)fc->topology_map;
1232217309Snwhitehorn	for (i = 0; i <= fc->topology_map->crc_len; i++)
1233217309Snwhitehorn		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1234217309Snwhitehorn	p = (uint32_t *)fc->speed_map;
1235217309Snwhitehorn	CSRARC(fc, SPED_MAP) = htonl(*p++);
1236217309Snwhitehorn	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1237217309Snwhitehorn	/* don't byte-swap uint8_t array */
1238217309Snwhitehorn	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1239217309Snwhitehorn
1240217309Snwhitehorn	fc->max_hop = fc->max_node - i_branch;
1241217309Snwhitehorn	printf(", maxhop <= %d", fc->max_hop);
1242217309Snwhitehorn
1243217309Snwhitehorn	if(fc->irm == -1 ){
1244217309Snwhitehorn		printf(", Not found IRM capable node");
1245217309Snwhitehorn	}else{
1246217309Snwhitehorn		printf(", cable IRM = %d", fc->irm);
1247217309Snwhitehorn		if (fc->irm == fc->nodeid)
1248217309Snwhitehorn			printf(" (me)");
1249217309Snwhitehorn	}
1250217309Snwhitehorn	printf("\n");
1251217309Snwhitehorn
1252217309Snwhitehorn	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1253217309Snwhitehorn		if (fc->irm == fc->nodeid) {
1254217309Snwhitehorn			fc->status = FWBUSMGRDONE;
1255217309Snwhitehorn			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1256217309Snwhitehorn			fw_bmr(fc);
1257217309Snwhitehorn		} else {
1258217309Snwhitehorn			fc->status = FWBUSMGRELECT;
1259217309Snwhitehorn			callout_reset(&fc->bmr_callout, hz/8,
1260217309Snwhitehorn				(void *)fw_try_bmr, (void *)fc);
1261217309Snwhitehorn		}
1262224014Snwhitehorn	} else
1263224014Snwhitehorn		fc->status = FWBUSMGRDONE;
1264224014Snwhitehorn
1265224014Snwhitehorn	callout_reset(&fc->busprobe_callout, hz/4,
1266224014Snwhitehorn			(void *)fw_bus_probe, (void *)fc);
1267224014Snwhitehorn}
1268224014Snwhitehorn
1269224014Snwhitehorn/*
1270224014Snwhitehorn * To probe devices on the IEEE1394 bus.
1271217309Snwhitehorn */
1272217309Snwhitehornstatic void
1273217309Snwhitehornfw_bus_probe(struct firewire_comm *fc)
1274217309Snwhitehorn{
1275217309Snwhitehorn	int s;
1276217309Snwhitehorn	struct fw_device *fwdev;
1277217309Snwhitehorn
1278217309Snwhitehorn	s = splfw();
1279251843Sbapt	fc->status = FWBUSEXPLORE;
1280217309Snwhitehorn	fc->retry_count = 0;
1281217309Snwhitehorn
1282217309Snwhitehorn	/* Invalidate all devices, just after bus reset. */
1283217309Snwhitehorn	STAILQ_FOREACH(fwdev, &fc->devices, link)
1284217309Snwhitehorn		if (fwdev->status != FWDEVINVAL) {
1285217309Snwhitehorn			fwdev->status = FWDEVINVAL;
1286217309Snwhitehorn			fwdev->rcnt = 0;
1287217309Snwhitehorn		}
1288217309Snwhitehorn
1289217309Snwhitehorn	fc->ongonode = 0;
1290217309Snwhitehorn	fc->ongoaddr = CSRROMOFF;
1291217309Snwhitehorn	fc->ongodev = NULL;
1292217309Snwhitehorn	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1293217309Snwhitehorn	fw_bus_explore(fc);
1294217309Snwhitehorn	splx(s);
1295217309Snwhitehorn}
1296217309Snwhitehorn
1297217309Snwhitehorn/*
1298217309Snwhitehorn * Find the self_id packet for a node, ignoring sequels.
1299217309Snwhitehorn */
1300220749Snwhitehornstatic union fw_self_id *
1301220749Snwhitehornfw_find_self_id(struct firewire_comm *fc, int node)
1302217309Snwhitehorn{
1303217309Snwhitehorn	uint32_t i;
1304224014Snwhitehorn	union fw_self_id *s;
1305224014Snwhitehorn
1306224014Snwhitehorn	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1307217309Snwhitehorn		s = &fc->topology_map->self_id[i];
1308217309Snwhitehorn		if (s->p0.sequel)
1309217309Snwhitehorn			continue;
1310217309Snwhitehorn		if (s->p0.phy_id == node)
1311217309Snwhitehorn			return s;
1312217309Snwhitehorn	}
1313217309Snwhitehorn	return 0;
1314217309Snwhitehorn}
1315217309Snwhitehorn
1316217309Snwhitehorn/*
1317217309Snwhitehorn * To collect device informations on the IEEE1394 bus.
1318217309Snwhitehorn */
1319220749Snwhitehornstatic void
1320217309Snwhitehornfw_bus_explore(struct firewire_comm *fc )
1321217309Snwhitehorn{
1322217309Snwhitehorn	int err = 0;
1323217309Snwhitehorn	struct fw_device *fwdev, *pfwdev, *tfwdev;
1324217309Snwhitehorn	uint32_t addr;
1325217309Snwhitehorn	struct fw_xfer *xfer;
1326217309Snwhitehorn	struct fw_pkt *fp;
1327217309Snwhitehorn	union fw_self_id *fwsid;
1328217309Snwhitehorn
1329217309Snwhitehorn	if(fc->status != FWBUSEXPLORE)
1330217309Snwhitehorn		return;
1331217309Snwhitehorn
1332217309Snwhitehornloop:
1333217309Snwhitehorn	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1334217309Snwhitehorn
1335217309Snwhitehorn	if(fc->ongonode > fc->max_node) goto done;
1336217309Snwhitehorn	if(fc->ongonode >= 0x3f) goto done;
1337217309Snwhitehorn
1338217309Snwhitehorn	/* check link */
1339217309Snwhitehorn	/* XXX we need to check phy_id first */
1340217309Snwhitehorn	fwsid = fw_find_self_id(fc, fc->ongonode);
1341217309Snwhitehorn	if (!fwsid || !fwsid->p0.link_active) {
1342217309Snwhitehorn		if (firewire_debug)
1343251843Sbapt			printf("node%d: link down\n", fc->ongonode);
1344217309Snwhitehorn		fc->ongonode++;
1345251843Sbapt		goto loop;
1346251843Sbapt	}
1347251843Sbapt
1348251843Sbapt	if(fc->ongoaddr <= CSRROMOFF &&
1349251843Sbapt		fc->ongoeui.hi == 0xffffffff &&
1350251843Sbapt		fc->ongoeui.lo == 0xffffffff ){
1351251843Sbapt		fc->ongoaddr = CSRROMOFF;
1352251843Sbapt		addr = 0xf0000000 | fc->ongoaddr;
1353251843Sbapt	}else if(fc->ongoeui.hi == 0xffffffff ){
1354251843Sbapt		fc->ongoaddr = CSRROMOFF + 0xc;
1355251843Sbapt		addr = 0xf0000000 | fc->ongoaddr;
1356251843Sbapt	}else if(fc->ongoeui.lo == 0xffffffff ){
1357251843Sbapt		fc->ongoaddr = CSRROMOFF + 0x10;
1358251843Sbapt		addr = 0xf0000000 | fc->ongoaddr;
1359251843Sbapt	}else if(fc->ongodev == NULL){
1360251843Sbapt		STAILQ_FOREACH(fwdev, &fc->devices, link)
1361251843Sbapt			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1362251843Sbapt				break;
1363251843Sbapt		if(fwdev != NULL){
1364251843Sbapt			fwdev->dst = fc->ongonode;
1365251843Sbapt			fwdev->status = FWDEVINIT;
1366251843Sbapt			fc->ongodev = fwdev;
1367251843Sbapt			fc->ongoaddr = CSRROMOFF;
1368251843Sbapt			addr = 0xf0000000 | fc->ongoaddr;
1369251843Sbapt			goto dorequest;
1370217309Snwhitehorn		}
1371217309Snwhitehorn		fwdev = malloc(sizeof(struct fw_device), M_FW,
1372217309Snwhitehorn							M_NOWAIT | M_ZERO);
1373217309Snwhitehorn		if(fwdev == NULL)
1374217309Snwhitehorn			return;
1375217309Snwhitehorn		fwdev->fc = fc;
1376217309Snwhitehorn		fwdev->rommax = 0;
1377217309Snwhitehorn		fwdev->dst = fc->ongonode;
1378217309Snwhitehorn		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1379251843Sbapt		fwdev->status = FWDEVINIT;
1380251843Sbapt		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1381217309Snwhitehorn
1382251843Sbapt		pfwdev = NULL;
1383217309Snwhitehorn		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1384217309Snwhitehorn			if (tfwdev->eui.hi > fwdev->eui.hi ||
1385217309Snwhitehorn					(tfwdev->eui.hi == fwdev->eui.hi &&
1386217309Snwhitehorn					tfwdev->eui.lo > fwdev->eui.lo))
1387217309Snwhitehorn				break;
1388217309Snwhitehorn			pfwdev = tfwdev;
1389217309Snwhitehorn		}
1390217309Snwhitehorn		if (pfwdev == NULL)
1391217309Snwhitehorn			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1392217309Snwhitehorn		else
1393217309Snwhitehorn			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1394217309Snwhitehorn
1395217309Snwhitehorn		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1396217309Snwhitehorn			linkspeed[fwdev->speed],
1397217309Snwhitehorn			fc->ongoeui.hi, fc->ongoeui.lo);
1398217309Snwhitehorn
1399217309Snwhitehorn		fc->ongodev = fwdev;
1400217309Snwhitehorn		fc->ongoaddr = CSRROMOFF;
1401217309Snwhitehorn		addr = 0xf0000000 | fc->ongoaddr;
1402224014Snwhitehorn	}else{
1403224014Snwhitehorn		addr = 0xf0000000 | fc->ongoaddr;
1404224014Snwhitehorn	}
1405217309Snwhitehorndorequest:
1406217309Snwhitehorn#if 0
1407217309Snwhitehorn	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1408217309Snwhitehorn		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1409217309Snwhitehorn		fw_bus_explore_callback);
1410217309Snwhitehorn	if(xfer == NULL) goto done;
1411217309Snwhitehorn#else
1412217309Snwhitehorn	xfer = fw_xfer_alloc(M_FWXFER);
1413217309Snwhitehorn	if(xfer == NULL){
1414217309Snwhitehorn		goto done;
1415217309Snwhitehorn	}
1416217309Snwhitehorn	xfer->send.spd = 0;
1417217309Snwhitehorn	fp = &xfer->send.hdr;
1418217309Snwhitehorn	fp->mode.rreqq.dest_hi = 0xffff;
1419217309Snwhitehorn	fp->mode.rreqq.tlrt = 0;
1420217309Snwhitehorn	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1421217309Snwhitehorn	fp->mode.rreqq.pri = 0;
1422217309Snwhitehorn	fp->mode.rreqq.src = 0;
1423217309Snwhitehorn	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1424217309Snwhitehorn	fp->mode.rreqq.dest_lo = addr;
1425217309Snwhitehorn	xfer->act.hand = fw_bus_explore_callback;
1426217309Snwhitehorn
1427217309Snwhitehorn	if (firewire_debug)
1428217309Snwhitehorn		printf("node%d: explore addr=0x%x\n",
1429217309Snwhitehorn				fc->ongonode, fc->ongoaddr);
1430217309Snwhitehorn	err = fw_asyreq(fc, -1, xfer);
1431217309Snwhitehorn	if(err){
1432217309Snwhitehorn		fw_xfer_free( xfer);
1433217309Snwhitehorn		return;
1434217309Snwhitehorn	}
1435217309Snwhitehorn#endif
1436217309Snwhitehorn	return;
1437217309Snwhitehorndone:
1438217309Snwhitehorn	/* fw_attach_devs */
1439217309Snwhitehorn	fc->status = FWBUSEXPDONE;
1440251843Sbapt	if (firewire_debug)
1441217309Snwhitehorn		printf("bus_explore done\n");
1442251843Sbapt	fw_attach_dev(fc);
1443251843Sbapt	return;
1444251843Sbapt
1445251843Sbapt}
1446217309Snwhitehorn
1447217309Snwhitehorn/* Portable Async. request read quad */
1448217309Snwhitehornstruct fw_xfer *
1449217309Snwhitehornasyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt,
1450217309Snwhitehorn	uint32_t addr_hi, uint32_t addr_lo,
1451217309Snwhitehorn	void (*hand) (struct fw_xfer*))
1452217309Snwhitehorn{
1453217309Snwhitehorn	struct fw_xfer *xfer;
1454217309Snwhitehorn	struct fw_pkt *fp;
1455224014Snwhitehorn	int err;
1456224014Snwhitehorn
1457224014Snwhitehorn	xfer = fw_xfer_alloc(M_FWXFER);
1458224014Snwhitehorn	if (xfer == NULL)
1459224014Snwhitehorn		return NULL;
1460224014Snwhitehorn
1461217309Snwhitehorn	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1462217309Snwhitehorn	fp = &xfer->send.hdr;
1463217309Snwhitehorn	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1464217309Snwhitehorn	if(tl & FWP_TL_VALID){
1465217309Snwhitehorn		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1466217309Snwhitehorn	}else{
1467255852Sdteske		fp->mode.rreqq.tlrt = 0;
1468255852Sdteske	}
1469255852Sdteske	fp->mode.rreqq.tlrt |= rt & 0x3;
1470217309Snwhitehorn	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1471217309Snwhitehorn	fp->mode.rreqq.pri = 0;
1472217309Snwhitehorn	fp->mode.rreqq.src = 0;
1473217309Snwhitehorn	fp->mode.rreqq.dst = addr_hi >> 16;
1474217309Snwhitehorn	fp->mode.rreqq.dest_lo = addr_lo;
1475217309Snwhitehorn	xfer->act.hand = hand;
1476217309Snwhitehorn
1477217309Snwhitehorn	err = fw_asyreq(fc, -1, xfer);
1478217309Snwhitehorn	if(err){
1479251843Sbapt		fw_xfer_free( xfer);
1480251843Sbapt		return NULL;
1481251843Sbapt	}
1482217309Snwhitehorn	return xfer;
1483217309Snwhitehorn}
1484217309Snwhitehorn
1485217309Snwhitehorn/*
1486217309Snwhitehorn * Callback for the IEEE1394 bus information collection.
1487217309Snwhitehorn */
1488217309Snwhitehornstatic void
1489217309Snwhitehornfw_bus_explore_callback(struct fw_xfer *xfer)
1490217309Snwhitehorn{
1491217309Snwhitehorn	struct firewire_comm *fc;
1492217309Snwhitehorn	struct fw_pkt *sfp,*rfp;
1493217309Snwhitehorn	struct csrhdr *chdr;
1494217309Snwhitehorn	struct csrdir *csrd;
1495217309Snwhitehorn	struct csrreg *csrreg;
1496217309Snwhitehorn	uint32_t offset;
1497217309Snwhitehorn
1498217309Snwhitehorn
1499217309Snwhitehorn	if(xfer == NULL) {
1500217309Snwhitehorn		printf("xfer == NULL\n");
1501217309Snwhitehorn		return;
1502217309Snwhitehorn	}
1503217309Snwhitehorn	fc = xfer->fc;
1504217309Snwhitehorn
1505224014Snwhitehorn	if (firewire_debug)
1506217309Snwhitehorn		printf("node%d: callback addr=0x%x\n",
1507217309Snwhitehorn			fc->ongonode, fc->ongoaddr);
1508217309Snwhitehorn
1509217309Snwhitehorn	if(xfer->resp != 0){
1510217309Snwhitehorn		device_printf(fc->bdev,
1511217309Snwhitehorn		    "bus_explore node=%d addr=0x%x resp=%d retry=%d\n",
1512217309Snwhitehorn		    fc->ongonode, fc->ongoaddr, xfer->resp, xfer->retry);
1513217309Snwhitehorn		if (xfer->retry < fc->max_asyretry) {
1514217309Snwhitehorn			fw_asystart(xfer);
1515217309Snwhitehorn			return;
1516217309Snwhitehorn		}
1517217309Snwhitehorn		goto errnode;
1518217309Snwhitehorn	}
1519217309Snwhitehorn
1520217309Snwhitehorn	sfp = &xfer->send.hdr;
1521217309Snwhitehorn	rfp = &xfer->recv.hdr;
1522217309Snwhitehorn#if 0
1523217309Snwhitehorn	{
1524217309Snwhitehorn		uint32_t *qld;
1525217309Snwhitehorn		int i;
1526217309Snwhitehorn		qld = (uint32_t *)xfer->recv.buf;
1527217309Snwhitehorn		printf("len:%d\n", xfer->recv.len);
1528217309Snwhitehorn		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1529217309Snwhitehorn			printf("0x%08x ", rfp->mode.ld[i/4]);
1530217309Snwhitehorn			if((i % 16) == 15) printf("\n");
1531217309Snwhitehorn		}
1532217309Snwhitehorn		if((i % 16) != 15) printf("\n");
1533217309Snwhitehorn	}
1534217309Snwhitehorn#endif
1535251843Sbapt	if(fc->ongodev == NULL){
1536217309Snwhitehorn		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1537217309Snwhitehorn			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1538217309Snwhitehorn			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1539217309Snwhitehorn/* If CSR is minimal confinguration, more investigation is not needed. */
1540217309Snwhitehorn			if(chdr->info_len == 1){
1541217309Snwhitehorn				if (firewire_debug)
1542217309Snwhitehorn					printf("node%d: minimal config\n",
1543217309Snwhitehorn								fc->ongonode);
1544217309Snwhitehorn				goto nextnode;
1545217309Snwhitehorn			}else{
1546217309Snwhitehorn				fc->ongoaddr = CSRROMOFF + 0xc;
1547217309Snwhitehorn			}
1548217309Snwhitehorn		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1549217309Snwhitehorn			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1550217309Snwhitehorn			fc->ongoaddr = CSRROMOFF + 0x10;
1551217309Snwhitehorn		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1552217309Snwhitehorn			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1553217309Snwhitehorn			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1554217309Snwhitehorn				if (firewire_debug)
1555217309Snwhitehorn					printf("node%d: eui64 is zero.\n",
1556217309Snwhitehorn							fc->ongonode);
1557217309Snwhitehorn				goto nextnode;
1558217309Snwhitehorn			}
1559217309Snwhitehorn			fc->ongoaddr = CSRROMOFF;
1560217309Snwhitehorn		}
1561217309Snwhitehorn	}else{
1562217309Snwhitehorn		if (fc->ongoaddr == CSRROMOFF &&
1563217309Snwhitehorn		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1564217309Snwhitehorn			fc->ongodev->status = FWDEVATTACHED;
1565217309Snwhitehorn			goto nextnode;
1566217309Snwhitehorn		}
1567217309Snwhitehorn		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1568217309Snwhitehorn		if(fc->ongoaddr > fc->ongodev->rommax){
1569217309Snwhitehorn			fc->ongodev->rommax = fc->ongoaddr;
1570217309Snwhitehorn		}
1571217309Snwhitehorn		csrd = SLIST_FIRST(&fc->ongocsr);
1572217309Snwhitehorn		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1573217309Snwhitehorn			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1574217309Snwhitehorn			offset = CSRROMOFF;
1575217309Snwhitehorn		}else{
1576217309Snwhitehorn			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1577217309Snwhitehorn			offset = csrd->off;
1578217309Snwhitehorn		}
1579217309Snwhitehorn		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1580217309Snwhitehorn			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1581217309Snwhitehorn			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1582217309Snwhitehorn				csrd = SLIST_FIRST(&fc->csrfree);
1583217309Snwhitehorn				if(csrd == NULL){
1584217309Snwhitehorn					goto nextnode;
1585217309Snwhitehorn				}else{
1586220749Snwhitehorn					csrd->ongoaddr = fc->ongoaddr;
1587220749Snwhitehorn					fc->ongoaddr += csrreg->val * 4;
1588224014Snwhitehorn					csrd->off = fc->ongoaddr;
1589220749Snwhitehorn					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1590251843Sbapt					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1591251843Sbapt					goto nextaddr;
1592251843Sbapt				}
1593251843Sbapt			}
1594251843Sbapt		}
1595251843Sbapt		fc->ongoaddr += 4;
1596217309Snwhitehorn		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1597217309Snwhitehorn				(fc->ongodev->rommax < 0x414)){
1598217309Snwhitehorn			if(fc->ongodev->rommax <= 0x414){
1599251843Sbapt				csrd = SLIST_FIRST(&fc->csrfree);
1600217309Snwhitehorn				if(csrd == NULL) goto nextnode;
1601217309Snwhitehorn				csrd->off = fc->ongoaddr;
1602217309Snwhitehorn				csrd->ongoaddr = fc->ongoaddr;
1603217309Snwhitehorn				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1604217309Snwhitehorn				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1605217309Snwhitehorn			}
1606217309Snwhitehorn			goto nextaddr;
1607217309Snwhitehorn		}
1608217309Snwhitehorn
1609217309Snwhitehorn		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1610217309Snwhitehorn			if(csrd == NULL){
1611217309Snwhitehorn				goto nextnode;
1612217309Snwhitehorn			};
1613217309Snwhitehorn			fc->ongoaddr = csrd->ongoaddr + 4;
1614217309Snwhitehorn			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1615217309Snwhitehorn			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1616217309Snwhitehorn			csrd = SLIST_FIRST(&fc->ongocsr);
1617217309Snwhitehorn			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1618217309Snwhitehorn				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1619217309Snwhitehorn				offset = CSRROMOFF;
1620217309Snwhitehorn			}else{
1621217309Snwhitehorn				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1622217309Snwhitehorn				offset = csrd->off;
1623217309Snwhitehorn			}
1624251843Sbapt		}
1625217309Snwhitehorn		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1626217309Snwhitehorn			goto nextnode;
1627251843Sbapt		}
1628251843Sbapt	}
1629251843Sbaptnextaddr:
1630251843Sbapt	fw_xfer_free( xfer);
1631251843Sbapt	fw_bus_explore(fc);
1632251843Sbapt	return;
1633251843Sbapterrnode:
1634251843Sbapt	fc->retry_count++;
1635217309Snwhitehorn	if (fc->ongodev != NULL) {
1636217309Snwhitehorn		fc->ongodev->status = FWDEVINVAL;
1637217309Snwhitehorn		/* Invalidate ROM */
1638217309Snwhitehorn		fc->ongodev->csrrom[0] = 0;
1639217309Snwhitehorn	}
1640217309Snwhitehornnextnode:
1641217309Snwhitehorn	fw_xfer_free( xfer);
1642217309Snwhitehorn	fc->ongonode++;
1643217309Snwhitehorn/* housekeeping work space */
1644217309Snwhitehorn	fc->ongoaddr = CSRROMOFF;
1645217309Snwhitehorn	fc->ongodev = NULL;
1646217309Snwhitehorn	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1647217309Snwhitehorn	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1648217309Snwhitehorn		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1649217309Snwhitehorn		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1650217309Snwhitehorn	}
1651217309Snwhitehorn	fw_bus_explore(fc);
1652217309Snwhitehorn	return;
1653251843Sbapt}
1654251843Sbapt
1655217309Snwhitehorn/*
1656217309Snwhitehorn * To attach sub-devices layer onto IEEE1394 bus.
1657217309Snwhitehorn */
1658217309Snwhitehornstatic void
1659217309Snwhitehornfw_attach_dev(struct firewire_comm *fc)
1660217309Snwhitehorn{
1661251843Sbapt	struct fw_device *fwdev, *next;
1662251843Sbapt	int i, err;
1663217309Snwhitehorn	device_t *devlistp;
1664217309Snwhitehorn	int devcnt;
1665217309Snwhitehorn	struct firewire_dev_comm *fdc;
1666217309Snwhitehorn
1667217309Snwhitehorn	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1668217309Snwhitehorn		next = STAILQ_NEXT(fwdev, link);
1669217309Snwhitehorn		if (fwdev->status == FWDEVINIT) {
1670217309Snwhitehorn			fwdev->status = FWDEVATTACHED;
1671217309Snwhitehorn		} else if (fwdev->status == FWDEVINVAL) {
1672220749Snwhitehorn			fwdev->rcnt ++;
1673220749Snwhitehorn			if (fwdev->rcnt > hold_count) {
1674217309Snwhitehorn				/*
1675251843Sbapt				 * Remove devices which have not been seen
1676217309Snwhitehorn				 * for a while.
1677251843Sbapt				 */
1678251843Sbapt				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1679251843Sbapt				    link);
1680251843Sbapt				free(fwdev, M_FW);
1681251843Sbapt			}
1682217309Snwhitehorn		}
1683217309Snwhitehorn	}
1684217309Snwhitehorn
1685217309Snwhitehorn	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1686217309Snwhitehorn	if( err != 0 )
1687251843Sbapt		return;
1688251843Sbapt	for( i = 0 ; i < devcnt ; i++){
1689217309Snwhitehorn		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1690217309Snwhitehorn			fdc = device_get_softc(devlistp[i]);
1691217309Snwhitehorn			if (fdc->post_explore != NULL)
1692217309Snwhitehorn				fdc->post_explore(fdc);
1693217309Snwhitehorn		}
1694217309Snwhitehorn	}
1695217309Snwhitehorn	free(devlistp, M_TEMP);
1696217309Snwhitehorn
1697217309Snwhitehorn	if (fc->retry_count > 0) {
1698217309Snwhitehorn		device_printf(fc->bdev, "bus_explore failed for %d nodes\n",
1699217309Snwhitehorn		    fc->retry_count);
1700217309Snwhitehorn#if 0
1701217309Snwhitehorn		callout_reset(&fc->retry_probe_callout, hz*2,
1702217309Snwhitehorn					(void *)fc->ibr, (void *)fc);
1703217309Snwhitehorn#endif
1704217309Snwhitehorn	}
1705217309Snwhitehorn	return;
1706217309Snwhitehorn}
1707217309Snwhitehorn
1708217309Snwhitehorn/*
1709217309Snwhitehorn * To allocate unique transaction label.
1710217309Snwhitehorn */
1711217309Snwhitehornstatic int
1712217309Snwhitehornfw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1713217309Snwhitehorn{
1714220749Snwhitehorn	u_int i;
1715220749Snwhitehorn	struct tlabel *tl, *tmptl;
1716217309Snwhitehorn	int s;
1717217309Snwhitehorn	static uint32_t label = 0;
1718217309Snwhitehorn
1719217309Snwhitehorn	s = splfw();
1720217309Snwhitehorn	for( i = 0 ; i < 0x40 ; i ++){
1721217309Snwhitehorn		label = (label + 1) & 0x3f;
1722217309Snwhitehorn		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1723217309Snwhitehorn			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1724217309Snwhitehorn			if (tmptl->xfer->send.hdr.mode.hdr.dst ==
1725217309Snwhitehorn			    xfer->send.hdr.mode.hdr.dst)
1726217309Snwhitehorn				break;
1727217309Snwhitehorn		}
1728217309Snwhitehorn		if(tmptl == NULL) {
1729217309Snwhitehorn			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1730224014Snwhitehorn			if (tl == NULL) {
1731224014Snwhitehorn				splx(s);
1732224014Snwhitehorn				return (-1);
1733217309Snwhitehorn			}
1734217309Snwhitehorn			tl->xfer = xfer;
1735217309Snwhitehorn			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1736217309Snwhitehorn			splx(s);
1737217309Snwhitehorn			if (firewire_debug > 1)
1738217309Snwhitehorn				printf("fw_get_tlabel: dst=%d tl=%d\n",
1739217309Snwhitehorn				    xfer->send.hdr.mode.hdr.dst, label);
1740217309Snwhitehorn			return(label);
1741217309Snwhitehorn		}
1742217309Snwhitehorn	}
1743217309Snwhitehorn	splx(s);
1744217309Snwhitehorn
1745217309Snwhitehorn	if (firewire_debug > 1)
1746217309Snwhitehorn		printf("fw_get_tlabel: no free tlabel\n");
1747217309Snwhitehorn	return(-1);
1748217309Snwhitehorn}
1749217309Snwhitehorn
1750217309Snwhitehornstatic void
1751217309Snwhitehornfw_rcv_copy(struct fw_rcv_buf *rb)
1752217309Snwhitehorn{
1753217309Snwhitehorn	struct fw_pkt *pkt;
1754217309Snwhitehorn	u_char *p;
1755217309Snwhitehorn	struct tcode_info *tinfo;
1756224014Snwhitehorn	u_int res, i, len, plen;
1757224014Snwhitehorn
1758224014Snwhitehorn	rb->xfer->recv.spd -= rb->spd;
1759224014Snwhitehorn
1760224014Snwhitehorn	pkt = (struct fw_pkt *)rb->vec->iov_base;
1761224014Snwhitehorn	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1762224014Snwhitehorn
1763224014Snwhitehorn	/* Copy header */
1764251843Sbapt	p = (u_char *)&rb->xfer->recv.hdr;
1765251843Sbapt	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1766251843Sbapt	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1767251843Sbapt	rb->vec->iov_len -= tinfo->hdr_len;
1768251843Sbapt
1769251843Sbapt	/* Copy payload */
1770251843Sbapt	p = (u_char *)rb->xfer->recv.payload;
1771251843Sbapt	res = rb->xfer->recv.pay_len;
1772251843Sbapt
1773251843Sbapt	/* special handling for RRESQ */
1774251843Sbapt	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1775251843Sbapt	    p != NULL && res >= sizeof(uint32_t)) {
1776251843Sbapt		*(uint32_t *)p = pkt->mode.rresq.data;
1777251843Sbapt		rb->xfer->recv.pay_len = sizeof(uint32_t);
1778251843Sbapt		return;
1779217309Snwhitehorn	}
1780217309Snwhitehorn
1781217309Snwhitehorn	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1782217309Snwhitehorn		return;
1783251843Sbapt
1784251843Sbapt	plen = pkt->mode.rresb.len;
1785251843Sbapt
1786217309Snwhitehorn	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1787217309Snwhitehorn		len = MIN(rb->vec->iov_len, plen);
1788217309Snwhitehorn		if (res < len) {
1789217309Snwhitehorn			printf("rcv buffer(%d) is %d bytes short.\n",
1790217309Snwhitehorn			    rb->xfer->recv.pay_len, len - res);
1791217309Snwhitehorn			len = res;
1792217309Snwhitehorn		}
1793217309Snwhitehorn		bcopy(rb->vec->iov_base, p, len);
1794217309Snwhitehorn		p += len;
1795217309Snwhitehorn		res -= len;
1796217309Snwhitehorn		plen -= len;
1797224014Snwhitehorn		if (res == 0 || plen == 0)
1798224014Snwhitehorn			break;
1799224014Snwhitehorn	}
1800224014Snwhitehorn	rb->xfer->recv.pay_len -= res;
1801224014Snwhitehorn
1802217309Snwhitehorn}
1803217309Snwhitehorn
1804217309Snwhitehorn/*
1805217309Snwhitehorn * Generic packet receiving process.
1806217309Snwhitehorn */
1807217309Snwhitehornvoid
1808217309Snwhitehornfw_rcv(struct fw_rcv_buf *rb)
1809217309Snwhitehorn{
1810224014Snwhitehorn	struct fw_pkt *fp, *resfp;
1811217309Snwhitehorn	struct fw_bind *bind;
1812217309Snwhitehorn	int tcode, s;
1813217309Snwhitehorn	int i, len, oldstate;
1814217309Snwhitehorn#if 0
1815217309Snwhitehorn	{
1816217309Snwhitehorn		uint32_t *qld;
1817217309Snwhitehorn		int i;
1818217309Snwhitehorn		qld = (uint32_t *)buf;
1819217309Snwhitehorn		printf("spd %d len:%d\n", spd, len);
1820217309Snwhitehorn		for( i = 0 ; i <= len && i < 32; i+= 4){
1821217309Snwhitehorn			printf("0x%08x ", ntohl(qld[i/4]));
1822217309Snwhitehorn			if((i % 16) == 15) printf("\n");
1823217309Snwhitehorn		}
1824217309Snwhitehorn		if((i % 16) != 15) printf("\n");
1825217309Snwhitehorn	}
1826217309Snwhitehorn#endif
1827217309Snwhitehorn	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1828217309Snwhitehorn	tcode = fp->mode.common.tcode;
1829217309Snwhitehorn	switch (tcode) {
1830217309Snwhitehorn	case FWTCODE_WRES:
1831217309Snwhitehorn	case FWTCODE_RRESQ:
1832217309Snwhitehorn	case FWTCODE_RRESB:
1833217309Snwhitehorn	case FWTCODE_LRES:
1834217309Snwhitehorn		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1835217309Snwhitehorn					fp->mode.hdr.tlrt >> 2);
1836217309Snwhitehorn		if(rb->xfer == NULL) {
1837217309Snwhitehorn			printf("fw_rcv: unknown response "
1838217309Snwhitehorn			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1839217309Snwhitehorn			    tcode_str[tcode], tcode,
1840217309Snwhitehorn			    fp->mode.hdr.src,
1841217309Snwhitehorn			    fp->mode.hdr.tlrt >> 2,
1842217309Snwhitehorn			    fp->mode.hdr.tlrt & 3,
1843217309Snwhitehorn			    fp->mode.rresq.data);
1844217309Snwhitehorn#if 1
1845217309Snwhitehorn			printf("try ad-hoc work around!!\n");
1846217309Snwhitehorn			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1847217309Snwhitehorn					(fp->mode.hdr.tlrt >> 2)^3);
1848217309Snwhitehorn			if (rb->xfer == NULL) {
1849217309Snwhitehorn				printf("no use...\n");
1850217309Snwhitehorn				goto err;
1851217309Snwhitehorn			}
1852217309Snwhitehorn#else
1853217309Snwhitehorn			goto err;
1854217309Snwhitehorn#endif
1855217309Snwhitehorn		}
1856217309Snwhitehorn		fw_rcv_copy(rb);
1857217309Snwhitehorn		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1858217309Snwhitehorn			rb->xfer->resp = EIO;
1859217309Snwhitehorn		else
1860217309Snwhitehorn			rb->xfer->resp = 0;
1861217309Snwhitehorn		/* make sure the packet is drained in AT queue */
1862217309Snwhitehorn		oldstate = rb->xfer->state;
1863217309Snwhitehorn		rb->xfer->state = FWXF_RCVD;
1864217309Snwhitehorn		switch (oldstate) {
1865217309Snwhitehorn		case FWXF_SENT:
1866217309Snwhitehorn			fw_xfer_done(rb->xfer);
1867217309Snwhitehorn			break;
1868217309Snwhitehorn		case FWXF_START:
1869217309Snwhitehorn#if 0
1870217309Snwhitehorn			if (firewire_debug)
1871217309Snwhitehorn				printf("not sent yet tl=%x\n", rb->xfer->tl);
1872217309Snwhitehorn#endif
1873217309Snwhitehorn			break;
1874217309Snwhitehorn		default:
1875217309Snwhitehorn			printf("unexpected state %d\n", rb->xfer->state);
1876217309Snwhitehorn		}
1877217309Snwhitehorn		return;
1878217309Snwhitehorn	case FWTCODE_WREQQ:
1879217309Snwhitehorn	case FWTCODE_WREQB:
1880217309Snwhitehorn	case FWTCODE_RREQQ:
1881217309Snwhitehorn	case FWTCODE_RREQB:
1882217309Snwhitehorn	case FWTCODE_LREQ:
1883217309Snwhitehorn		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1884217309Snwhitehorn			fp->mode.rreqq.dest_lo);
1885217309Snwhitehorn		if(bind == NULL){
1886217309Snwhitehorn			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1887217309Snwhitehorn#if defined(__DragonFly__) || __FreeBSD_version < 500000
1888217309Snwhitehorn			    " src=0x%x data=%lx\n",
1889217309Snwhitehorn#else
1890217309Snwhitehorn			    " src=0x%x data=%x\n",
1891217309Snwhitehorn#endif
1892217309Snwhitehorn			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1893217309Snwhitehorn			    tcode_str[tcode], tcode,
1894217309Snwhitehorn			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1895217309Snwhitehorn			if (rb->fc->status == FWBUSRESET) {
1896217309Snwhitehorn				printf("fw_rcv: cannot respond(bus reset)!\n");
1897217309Snwhitehorn				goto err;
1898217309Snwhitehorn			}
1899217309Snwhitehorn			rb->xfer = fw_xfer_alloc(M_FWXFER);
1900217309Snwhitehorn			if(rb->xfer == NULL){
1901217309Snwhitehorn				return;
1902217309Snwhitehorn			}
1903217309Snwhitehorn			rb->xfer->send.spd = rb->spd;
1904217309Snwhitehorn			rb->xfer->send.pay_len = 0;
1905217309Snwhitehorn			resfp = &rb->xfer->send.hdr;
1906217309Snwhitehorn			switch (tcode) {
1907217309Snwhitehorn			case FWTCODE_WREQQ:
1908224014Snwhitehorn			case FWTCODE_WREQB:
1909224014Snwhitehorn				resfp->mode.hdr.tcode = FWTCODE_WRES;
1910217309Snwhitehorn				break;
1911217309Snwhitehorn			case FWTCODE_RREQQ:
1912217309Snwhitehorn				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1913251843Sbapt				break;
1914251843Sbapt			case FWTCODE_RREQB:
1915251843Sbapt				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1916251843Sbapt				break;
1917251843Sbapt			case FWTCODE_LREQ:
1918251843Sbapt				resfp->mode.hdr.tcode = FWTCODE_LRES;
1919251843Sbapt				break;
1920217309Snwhitehorn			}
1921217309Snwhitehorn			resfp->mode.hdr.dst = fp->mode.hdr.src;
1922217309Snwhitehorn			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1923217309Snwhitehorn			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1924217309Snwhitehorn			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1925217309Snwhitehorn			resfp->mode.rresb.extcode = 0;
1926217309Snwhitehorn			resfp->mode.rresb.len = 0;
1927217309Snwhitehorn/*
1928251843Sbapt			rb->xfer->act.hand = fw_asy_callback;
1929217309Snwhitehorn*/
1930217309Snwhitehorn			rb->xfer->act.hand = fw_xfer_free;
1931220749Snwhitehorn			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1932217309Snwhitehorn				fw_xfer_free(rb->xfer);
1933220749Snwhitehorn				return;
1934220749Snwhitehorn			}
1935217309Snwhitehorn			goto err;
1936217309Snwhitehorn		}
1937217309Snwhitehorn		len = 0;
1938217309Snwhitehorn		for (i = 0; i < rb->nvec; i ++)
1939217309Snwhitehorn			len += rb->vec[i].iov_len;
1940217309Snwhitehorn		switch(bind->act_type){
1941217309Snwhitehorn		case FWACT_XFER:
1942217309Snwhitehorn			/* splfw()?? */
1943217309Snwhitehorn			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1944217309Snwhitehorn			if (rb->xfer == NULL) {
1945217309Snwhitehorn				printf("Discard a packet for this bind.\n");
1946217309Snwhitehorn				goto err;
1947217309Snwhitehorn			}
1948217309Snwhitehorn			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1949217309Snwhitehorn			fw_rcv_copy(rb);
1950217309Snwhitehorn			rb->xfer->act.hand(rb->xfer);
1951217309Snwhitehorn			return;
1952217309Snwhitehorn			break;
1953217309Snwhitehorn		case FWACT_CH:
1954217309Snwhitehorn			if(rb->fc->ir[bind->sub]->queued >=
1955217309Snwhitehorn				rb->fc->ir[bind->sub]->maxq){
1956217309Snwhitehorn				device_printf(rb->fc->bdev,
1957217309Snwhitehorn					"Discard a packet %x %d\n",
1958217309Snwhitehorn					bind->sub,
1959217309Snwhitehorn					rb->fc->ir[bind->sub]->queued);
1960217309Snwhitehorn				goto err;
1961217309Snwhitehorn			}
1962217309Snwhitehorn			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1963217309Snwhitehorn			if (rb->xfer == NULL) {
1964217309Snwhitehorn				printf("Discard packet for this bind\n");
1965217309Snwhitehorn				goto err;
1966217309Snwhitehorn			}
1967217309Snwhitehorn			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1968217309Snwhitehorn			fw_rcv_copy(rb);
1969217309Snwhitehorn			s = splfw();
1970217309Snwhitehorn			rb->fc->ir[bind->sub]->queued++;
1971217309Snwhitehorn			STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q,
1972217309Snwhitehorn			    rb->xfer, link);
1973217309Snwhitehorn			splx(s);
1974217309Snwhitehorn
1975217309Snwhitehorn			wakeup((caddr_t)rb->fc->ir[bind->sub]);
1976217309Snwhitehorn
1977			return;
1978			break;
1979		default:
1980			goto err;
1981			break;
1982		}
1983		break;
1984#if 0 /* shouldn't happen ?? or for GASP */
1985	case FWTCODE_STREAM:
1986	{
1987		struct fw_xferq *xferq;
1988
1989		xferq = rb->fc->ir[sub];
1990#if 0
1991		printf("stream rcv dma %d len %d off %d spd %d\n",
1992			sub, len, off, spd);
1993#endif
1994		if(xferq->queued >= xferq->maxq) {
1995			printf("receive queue is full\n");
1996			goto err;
1997		}
1998		/* XXX get xfer from xfer queue, we don't need copy for
1999			per packet mode */
2000		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
2001						vec[0].iov_len);
2002		if (rb->xfer == NULL) goto err;
2003		fw_rcv_copy(rb)
2004		s = splfw();
2005		xferq->queued++;
2006		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
2007		splx(s);
2008		sc = device_get_softc(rb->fc->bdev);
2009#if defined(__DragonFly__) || __FreeBSD_version < 500000
2010		if (&xferq->rsel.si_pid != 0)
2011#else
2012		if (SEL_WAITING(&xferq->rsel))
2013#endif
2014			selwakeuppri(&xferq->rsel, FWPRI);
2015		if (xferq->flag & FWXFERQ_WAKEUP) {
2016			xferq->flag &= ~FWXFERQ_WAKEUP;
2017			wakeup((caddr_t)xferq);
2018		}
2019		if (xferq->flag & FWXFERQ_HANDLER) {
2020			xferq->hand(xferq);
2021		}
2022		return;
2023		break;
2024	}
2025#endif
2026	default:
2027		printf("fw_rcv: unknow tcode %d\n", tcode);
2028		break;
2029	}
2030err:
2031	return;
2032}
2033
2034/*
2035 * Post process for Bus Manager election process.
2036 */
2037static void
2038fw_try_bmr_callback(struct fw_xfer *xfer)
2039{
2040	struct firewire_comm *fc;
2041	int bmr;
2042
2043	if (xfer == NULL)
2044		return;
2045	fc = xfer->fc;
2046	if (xfer->resp != 0)
2047		goto error;
2048	if (xfer->recv.payload == NULL)
2049		goto error;
2050	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2051		goto error;
2052
2053	bmr = ntohl(xfer->recv.payload[0]);
2054	if (bmr == 0x3f)
2055		bmr = fc->nodeid;
2056
2057	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2058	fw_xfer_free_buf(xfer);
2059	fw_bmr(fc);
2060	return;
2061
2062error:
2063	device_printf(fc->bdev, "bus manager election failed\n");
2064	fw_xfer_free_buf(xfer);
2065}
2066
2067
2068/*
2069 * To candidate Bus Manager election process.
2070 */
2071static void
2072fw_try_bmr(void *arg)
2073{
2074	struct fw_xfer *xfer;
2075	struct firewire_comm *fc = (struct firewire_comm *)arg;
2076	struct fw_pkt *fp;
2077	int err = 0;
2078
2079	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2080	if(xfer == NULL){
2081		return;
2082	}
2083	xfer->send.spd = 0;
2084	fc->status = FWBUSMGRELECT;
2085
2086	fp = &xfer->send.hdr;
2087	fp->mode.lreq.dest_hi = 0xffff;
2088	fp->mode.lreq.tlrt = 0;
2089	fp->mode.lreq.tcode = FWTCODE_LREQ;
2090	fp->mode.lreq.pri = 0;
2091	fp->mode.lreq.src = 0;
2092	fp->mode.lreq.len = 8;
2093	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2094	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2095	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2096	xfer->send.payload[0] = htonl(0x3f);
2097	xfer->send.payload[1] = htonl(fc->nodeid);
2098	xfer->act.hand = fw_try_bmr_callback;
2099
2100	err = fw_asyreq(fc, -1, xfer);
2101	if(err){
2102		fw_xfer_free_buf(xfer);
2103		return;
2104	}
2105	return;
2106}
2107
2108#ifdef FW_VMACCESS
2109/*
2110 * Software implementation for physical memory block access.
2111 * XXX:Too slow, usef for debug purpose only.
2112 */
2113static void
2114fw_vmaccess(struct fw_xfer *xfer){
2115	struct fw_pkt *rfp, *sfp = NULL;
2116	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2117
2118	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2119			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2120	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2121	if(xfer->resp != 0){
2122		fw_xfer_free( xfer);
2123		return;
2124	}
2125	if(xfer->recv.buf == NULL){
2126		fw_xfer_free( xfer);
2127		return;
2128	}
2129	rfp = (struct fw_pkt *)xfer->recv.buf;
2130	switch(rfp->mode.hdr.tcode){
2131		/* XXX need fix for 64bit arch */
2132		case FWTCODE_WREQB:
2133			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2134			xfer->send.len = 12;
2135			sfp = (struct fw_pkt *)xfer->send.buf;
2136			bcopy(rfp->mode.wreqb.payload,
2137				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2138			sfp->mode.wres.tcode = FWTCODE_WRES;
2139			sfp->mode.wres.rtcode = 0;
2140			break;
2141		case FWTCODE_WREQQ:
2142			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2143			xfer->send.len = 12;
2144			sfp->mode.wres.tcode = FWTCODE_WRES;
2145			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2146			sfp->mode.wres.rtcode = 0;
2147			break;
2148		case FWTCODE_RREQB:
2149			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2150			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2151			sfp = (struct fw_pkt *)xfer->send.buf;
2152			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2153				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2154			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2155			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2156			sfp->mode.rresb.rtcode = 0;
2157			sfp->mode.rresb.extcode = 0;
2158			break;
2159		case FWTCODE_RREQQ:
2160			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2161			xfer->send.len = 16;
2162			sfp = (struct fw_pkt *)xfer->send.buf;
2163			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2164			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2165			sfp->mode.rresb.rtcode = 0;
2166			break;
2167		default:
2168			fw_xfer_free( xfer);
2169			return;
2170	}
2171	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2172	xfer->dst = ntohs(rfp->mode.hdr.src);
2173	xfer->act.hand = fw_xfer_free;
2174	xfer->retry_req = fw_asybusy;
2175
2176	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2177	sfp->mode.hdr.pri = 0;
2178
2179	fw_asyreq(xfer->fc, -1, xfer);
2180/**/
2181	return;
2182}
2183#endif
2184
2185/*
2186 * CRC16 check-sum for IEEE1394 register blocks.
2187 */
2188uint16_t
2189fw_crc16(uint32_t *ptr, uint32_t len){
2190	uint32_t i, sum, crc = 0;
2191	int shift;
2192	len = (len + 3) & ~3;
2193	for(i = 0 ; i < len ; i+= 4){
2194		for( shift = 28 ; shift >= 0 ; shift -= 4){
2195			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2196			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2197		}
2198		crc &= 0xffff;
2199	}
2200	return((uint16_t) crc);
2201}
2202
2203static int
2204fw_bmr(struct firewire_comm *fc)
2205{
2206	struct fw_device fwdev;
2207	union fw_self_id *self_id;
2208	int cmstr;
2209	uint32_t quad;
2210
2211	/* Check to see if the current root node is cycle master capable */
2212	self_id = fw_find_self_id(fc, fc->max_node);
2213	if (fc->max_node > 0) {
2214		/* XXX check cmc bit of businfo block rather than contender */
2215		if (self_id->p0.link_active && self_id->p0.contender)
2216			cmstr = fc->max_node;
2217		else {
2218			device_printf(fc->bdev,
2219				"root node is not cycle master capable\n");
2220			/* XXX shall we be the cycle master? */
2221			cmstr = fc->nodeid;
2222			/* XXX need bus reset */
2223		}
2224	} else
2225		cmstr = -1;
2226
2227	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2228	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2229		/* We are not the bus manager */
2230		printf("\n");
2231		return(0);
2232	}
2233	printf("(me)\n");
2234
2235	/* Optimize gapcount */
2236	if(fc->max_hop <= MAX_GAPHOP )
2237		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2238	/* If we are the cycle master, nothing to do */
2239	if (cmstr == fc->nodeid || cmstr == -1)
2240		return 0;
2241	/* Bus probe has not finished, make dummy fwdev for cmstr */
2242	bzero(&fwdev, sizeof(fwdev));
2243	fwdev.fc = fc;
2244	fwdev.dst = cmstr;
2245	fwdev.speed = 0;
2246	fwdev.maxrec = 8; /* 512 */
2247	fwdev.status = FWDEVINIT;
2248	/* Set cmstr bit on the cycle master */
2249	quad = htonl(1 << 8);
2250	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2251		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2252
2253	return 0;
2254}
2255
2256static int
2257fw_modevent(module_t mode, int type, void *data)
2258{
2259	int err = 0;
2260#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2261	static eventhandler_tag fwdev_ehtag = NULL;
2262#endif
2263
2264	switch (type) {
2265	case MOD_LOAD:
2266#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2267		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2268						fwdev_clone, 0, 1000);
2269#endif
2270		break;
2271	case MOD_UNLOAD:
2272#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2273		if (fwdev_ehtag != NULL)
2274			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2275#endif
2276		break;
2277	case MOD_SHUTDOWN:
2278		break;
2279	default:
2280		return (EOPNOTSUPP);
2281	}
2282	return (err);
2283}
2284
2285
2286#ifdef __DragonFly__
2287DECLARE_DUMMY_MODULE(firewire);
2288#endif
2289DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2290MODULE_VERSION(firewire, 1);
2291