firewire.c revision 171457
1139749Simp/*-
2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 171457 2007-07-15 13:00:29Z simokawa $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/types.h>
41103285Sikob
42103285Sikob#include <sys/kernel.h>
43129879Sphk#include <sys/module.h>
44103285Sikob#include <sys/malloc.h>
45103285Sikob#include <sys/conf.h>
46103285Sikob#include <sys/sysctl.h>
47169806Ssimokawa#include <sys/kthread.h>
48103285Sikob
49170374Ssimokawa#include <sys/kdb.h>
50170374Ssimokawa
51127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
52117067Ssimokawa#include <machine/clock.h>	/* for DELAY() */
53117067Ssimokawa#endif
54103285Sikob
55103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
56113584Ssimokawa#include <machine/bus.h>
57103285Sikob
58127468Ssimokawa#ifdef __DragonFly__
59127468Ssimokawa#include "firewire.h"
60127468Ssimokawa#include "firewirereg.h"
61127468Ssimokawa#include "fwmem.h"
62127468Ssimokawa#include "iec13213.h"
63127468Ssimokawa#include "iec68113.h"
64127468Ssimokawa#else
65103285Sikob#include <dev/firewire/firewire.h>
66103285Sikob#include <dev/firewire/firewirereg.h>
67110072Ssimokawa#include <dev/firewire/fwmem.h>
68103285Sikob#include <dev/firewire/iec13213.h>
69103285Sikob#include <dev/firewire/iec68113.h>
70127468Ssimokawa#endif
71103285Sikob
72116376Ssimokawastruct crom_src_buf {
73116376Ssimokawa	struct crom_src	src;
74116376Ssimokawa	struct crom_chunk root;
75116376Ssimokawa	struct crom_chunk vendor;
76116376Ssimokawa	struct crom_chunk hw;
77116376Ssimokawa};
78116376Ssimokawa
79120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3;
80103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
81108281Ssimokawa	"FireWire driver debug flag");
82109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
83109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
84109736Ssimokawa	"Try to be a bus manager");
85120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
86120850Ssimokawa	"Number of count of bus resets for removing lost device information");
87103285Sikob
88110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
89110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
90110195Ssimokawa
91103285Sikob#define FW_MAXASYRTY 4
92103285Sikob
93103285Sikobdevclass_t firewire_devclass;
94103285Sikob
95125238Ssimokawastatic void firewire_identify	(driver_t *, device_t);
96125238Ssimokawastatic int firewire_probe	(device_t);
97124169Ssimokawastatic int firewire_attach      (device_t);
98124169Ssimokawastatic int firewire_detach      (device_t);
99124169Ssimokawastatic int firewire_resume      (device_t);
100170374Ssimokawastatic void firewire_xfer_timeout(void *, int);
101103285Sikob#if 0
102124169Ssimokawastatic int firewire_shutdown    (device_t);
103103285Sikob#endif
104124169Ssimokawastatic device_t firewire_add_child   (device_t, int, const char *, int);
105124169Ssimokawastatic void fw_try_bmr (void *);
106124169Ssimokawastatic void fw_try_bmr_callback (struct fw_xfer *);
107124169Ssimokawastatic void fw_asystart (struct fw_xfer *);
108124169Ssimokawastatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
109124169Ssimokawastatic void fw_bus_probe (struct firewire_comm *);
110124169Ssimokawastatic void fw_attach_dev (struct firewire_comm *);
111169806Ssimokawastatic void fw_bus_probe_thread(void *);
112106543Ssimokawa#ifdef FW_VMACCESS
113124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *);
114106543Ssimokawa#endif
115124169Ssimokawastatic int fw_bmr (struct firewire_comm *);
116170374Ssimokawastatic void fw_dump_hdr(struct fw_pkt *, char *);
117103285Sikob
118103285Sikobstatic device_method_t firewire_methods[] = {
119103285Sikob	/* Device interface */
120125238Ssimokawa	DEVMETHOD(device_identify,	firewire_identify),
121125238Ssimokawa	DEVMETHOD(device_probe,		firewire_probe),
122103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
123103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
124108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
125116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
126103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
127103285Sikob
128103285Sikob	/* Bus interface */
129103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
130103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
131103285Sikob
132103285Sikob	{ 0, 0 }
133103285Sikob};
134124251Ssimokawachar *linkspeed[] = {
135124251Ssimokawa	"S100", "S200", "S400", "S800",
136124251Ssimokawa	"S1600", "S3200", "undef", "undef"
137124251Ssimokawa};
138103285Sikob
139124251Ssimokawastatic char *tcode_str[] = {
140124251Ssimokawa	"WREQQ", "WREQB", "WRES",   "undef",
141124251Ssimokawa	"RREQQ", "RREQB", "RRESQ",  "RRESB",
142124251Ssimokawa	"CYCS",  "LREQ",  "STREAM", "LRES",
143124251Ssimokawa	"undef", "undef", "PHY",    "undef"
144124251Ssimokawa};
145124251Ssimokawa
146114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
147114909Ssimokawa#define MAX_GAPHOP 15
148114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
149114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
150106813Ssimokawa
151103285Sikobstatic driver_t firewire_driver = {
152103285Sikob	"firewire",
153103285Sikob	firewire_methods,
154103285Sikob	sizeof(struct firewire_softc),
155103285Sikob};
156103285Sikob
157103285Sikob/*
158110072Ssimokawa * Lookup fwdev by node id.
159103285Sikob */
160106810Ssimokawastruct fw_device *
161110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
162103285Sikob{
163103285Sikob	struct fw_device *fwdev;
164110072Ssimokawa	int s;
165110072Ssimokawa
166110072Ssimokawa	s = splfw();
167110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
168120660Ssimokawa		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
169103285Sikob			break;
170110072Ssimokawa	splx(s);
171110072Ssimokawa
172106810Ssimokawa	return fwdev;
173103285Sikob}
174106813Ssimokawa
175103285Sikob/*
176110072Ssimokawa * Lookup fwdev by EUI64.
177110072Ssimokawa */
178110072Ssimokawastruct fw_device *
179110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
180110072Ssimokawa{
181110072Ssimokawa	struct fw_device *fwdev;
182110072Ssimokawa	int s;
183110072Ssimokawa
184110072Ssimokawa	s = splfw();
185170374Ssimokawa	FW_GLOCK(fc);
186110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
187110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
188110072Ssimokawa			break;
189170374Ssimokawa	FW_GUNLOCK(fc);
190110072Ssimokawa	splx(s);
191110072Ssimokawa
192110072Ssimokawa	if(fwdev == NULL) return NULL;
193110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
194110072Ssimokawa	return fwdev;
195110072Ssimokawa}
196110072Ssimokawa
197110072Ssimokawa/*
198103285Sikob * Async. request procedure for userland application.
199103285Sikob */
200103285Sikobint
201103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
202103285Sikob{
203103285Sikob	int err = 0;
204103285Sikob	struct fw_xferq *xferq;
205170374Ssimokawa	int len;
206103285Sikob	struct fw_pkt *fp;
207103285Sikob	int tcode;
208103285Sikob	struct tcode_info *info;
209103285Sikob
210103285Sikob	if(xfer == NULL) return EINVAL;
211167632Ssimokawa	if(xfer->hand == NULL){
212167632Ssimokawa		printf("hand == NULL\n");
213103285Sikob		return EINVAL;
214103285Sikob	}
215120660Ssimokawa	fp = &xfer->send.hdr;
216103285Sikob
217103285Sikob	tcode = fp->mode.common.tcode & 0xf;
218103285Sikob	info = &fc->tcode[tcode];
219103285Sikob	if (info->flag == 0) {
220124251Ssimokawa		printf("invalid tcode=%x\n", tcode);
221103285Sikob		return EINVAL;
222103285Sikob	}
223170425Ssimokawa
224170425Ssimokawa	/* XXX allow bus explore packets only after bus rest */
225170425Ssimokawa	if ((fc->status < FWBUSEXPLORE) &&
226170425Ssimokawa	    ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) ||
227170425Ssimokawa	    (fp->mode.rreqq.dest_lo  < 0xf0000000) ||
228170425Ssimokawa	    (fp->mode.rreqq.dest_lo >= 0xf0001000))) {
229170425Ssimokawa		xfer->resp = EAGAIN;
230170425Ssimokawa		xfer->flag = FWXF_BUSY;
231170425Ssimokawa		return (EAGAIN);
232170425Ssimokawa	}
233170425Ssimokawa
234103285Sikob	if (info->flag & FWTI_REQ)
235103285Sikob		xferq = fc->atq;
236103285Sikob	else
237103285Sikob		xferq = fc->ats;
238103285Sikob	len = info->hdr_len;
239120660Ssimokawa	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
240120660Ssimokawa		printf("send.pay_len > maxrec\n");
241120660Ssimokawa		return EINVAL;
242120660Ssimokawa	}
243103285Sikob	if (info->flag & FWTI_BLOCK_STR)
244120660Ssimokawa		len = fp->mode.stream.len;
245103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
246120660Ssimokawa		len = fp->mode.rresb.len;
247120660Ssimokawa	else
248120660Ssimokawa		len = 0;
249120660Ssimokawa	if (len != xfer->send.pay_len){
250124251Ssimokawa		printf("len(%d) != send.pay_len(%d) %s(%x)\n",
251124251Ssimokawa		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
252103285Sikob		return EINVAL;
253103285Sikob	}
254106790Ssimokawa
255103285Sikob	if(xferq->start == NULL){
256103285Sikob		printf("xferq->start == NULL\n");
257103285Sikob		return EINVAL;
258103285Sikob	}
259103285Sikob	if(!(xferq->queued < xferq->maxq)){
260108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
261108655Ssimokawa			xferq->queued);
262170374Ssimokawa		return EAGAIN;
263103285Sikob	}
264103285Sikob
265170374Ssimokawa	xfer->tl = -1;
266103285Sikob	if (info->flag & FWTI_TLABEL) {
267170374Ssimokawa		if (fw_get_tlabel(fc, xfer) < 0)
268130460Sdfr			return EAGAIN;
269103285Sikob	}
270103285Sikob
271103285Sikob	xfer->resp = 0;
272103285Sikob	xfer->fc = fc;
273103285Sikob	xfer->q = xferq;
274103285Sikob
275103285Sikob	fw_asystart(xfer);
276103285Sikob	return err;
277103285Sikob}
278103285Sikob/*
279103285Sikob * Wakeup blocked process.
280103285Sikob */
281103285Sikobvoid
282170374Ssimokawafw_xferwake(struct fw_xfer *xfer)
283170374Ssimokawa{
284170374Ssimokawa	struct mtx *lock = &xfer->fc->wait_lock;
285170374Ssimokawa
286170374Ssimokawa	mtx_lock(lock);
287170374Ssimokawa	xfer->flag |= FWXF_WAKE;
288170374Ssimokawa	mtx_unlock(lock);
289170374Ssimokawa
290103285Sikob	wakeup(xfer);
291103285Sikob	return;
292103285Sikob}
293103285Sikob
294170374Ssimokawaint
295170374Ssimokawafw_xferwait(struct fw_xfer *xfer)
296170374Ssimokawa{
297170374Ssimokawa	struct mtx *lock = &xfer->fc->wait_lock;
298170374Ssimokawa	int err = 0;
299170374Ssimokawa
300170374Ssimokawa	mtx_lock(lock);
301170374Ssimokawa	if ((xfer->flag & FWXF_WAKE) == 0)
302170374Ssimokawa		err = msleep((void *)xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0);
303170374Ssimokawa	mtx_unlock(lock);
304170374Ssimokawa
305170374Ssimokawa	return (err);
306170374Ssimokawa}
307170374Ssimokawa
308103285Sikob/*
309103285Sikob * Async. request with given xfer structure.
310103285Sikob */
311106790Ssimokawastatic void
312106790Ssimokawafw_asystart(struct fw_xfer *xfer)
313106790Ssimokawa{
314103285Sikob	struct firewire_comm *fc = xfer->fc;
315103285Sikob	int s;
316170374Ssimokawa	s = splfw();
317170374Ssimokawa	/* Protect from interrupt/timeout */
318170374Ssimokawa	FW_GLOCK(fc);
319170374Ssimokawa	xfer->flag = FWXF_INQ;
320103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
321170374Ssimokawa#if 0
322103285Sikob	xfer->q->queued ++;
323170374Ssimokawa#endif
324170374Ssimokawa	FW_GUNLOCK(fc);
325103285Sikob	splx(s);
326103285Sikob	/* XXX just queue for mbuf */
327103285Sikob	if (xfer->mbuf == NULL)
328103285Sikob		xfer->q->start(fc);
329103285Sikob	return;
330103285Sikob}
331106790Ssimokawa
332125238Ssimokawastatic void
333125238Ssimokawafirewire_identify(driver_t *driver, device_t parent)
334125238Ssimokawa{
335125238Ssimokawa	BUS_ADD_CHILD(parent, 0, "firewire", -1);
336125238Ssimokawa}
337125238Ssimokawa
338103285Sikobstatic int
339125238Ssimokawafirewire_probe(device_t dev)
340103285Sikob{
341108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
342125238Ssimokawa	return (0);
343103285Sikob}
344106790Ssimokawa
345110577Ssimokawastatic void
346170374Ssimokawafirewire_xfer_timeout(void *arg, int pending)
347110577Ssimokawa{
348170374Ssimokawa	struct firewire_comm *fc = (struct firewire_comm *)arg;
349170374Ssimokawa	struct fw_xfer *xfer, *txfer;
350110577Ssimokawa	struct timeval tv;
351110577Ssimokawa	struct timeval split_timeout;
352170374Ssimokawa	STAILQ_HEAD(, fw_xfer) xfer_timeout;
353111040Ssimokawa	int i, s;
354110577Ssimokawa
355120660Ssimokawa	split_timeout.tv_sec = 0;
356120660Ssimokawa	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
357110577Ssimokawa
358110577Ssimokawa	microtime(&tv);
359110577Ssimokawa	timevalsub(&tv, &split_timeout);
360170374Ssimokawa	STAILQ_INIT(&xfer_timeout);
361110577Ssimokawa
362111040Ssimokawa	s = splfw();
363170374Ssimokawa	FW_GLOCK(fc);
364110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
365169119Ssimokawa		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
366170427Ssimokawa			if ((xfer->flag & FWXF_SENT) == 0)
367170427Ssimokawa				/* not sent yet */
368170427Ssimokawa				break;
369110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
370110577Ssimokawa				/* the rests are newer than this */
371110577Ssimokawa				break;
372110577Ssimokawa			device_printf(fc->bdev,
373170374Ssimokawa				"split transaction timeout: "
374170374Ssimokawa				"tl=0x%x flag=0x%02x\n", i, xfer->flag);
375170374Ssimokawa			fw_dump_hdr(&xfer->send.hdr, "send");
376110577Ssimokawa			xfer->resp = ETIMEDOUT;
377170374Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
378170374Ssimokawa			STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel);
379110577Ssimokawa		}
380110577Ssimokawa	}
381170374Ssimokawa	FW_GUNLOCK(fc);
382111040Ssimokawa	splx(s);
383170374Ssimokawa	fc->timeout(fc);
384170374Ssimokawa
385170374Ssimokawa	STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer)
386170374Ssimokawa		xfer->hand(xfer);
387110577Ssimokawa}
388110577Ssimokawa
389170374Ssimokawa#define WATCHDOG_HZ 10
390110577Ssimokawastatic void
391110577Ssimokawafirewire_watchdog(void *arg)
392110577Ssimokawa{
393110577Ssimokawa	struct firewire_comm *fc;
394170374Ssimokawa	static int watchdog_clock = 0;
395110577Ssimokawa
396110577Ssimokawa	fc = (struct firewire_comm *)arg;
397121463Ssimokawa
398121463Ssimokawa	/*
399121463Ssimokawa	 * At boot stage, the device interrupt is disabled and
400121463Ssimokawa	 * We encounter a timeout easily. To avoid this,
401121463Ssimokawa	 * ignore clock interrupt for a while.
402121463Ssimokawa	 */
403170374Ssimokawa	if (watchdog_clock > WATCHDOG_HZ * 15)
404170374Ssimokawa		taskqueue_enqueue(fc->taskqueue, &fc->task_timeout);
405170374Ssimokawa	else
406170374Ssimokawa		watchdog_clock ++;
407121463Ssimokawa
408170374Ssimokawa	callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ,
409110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
410110577Ssimokawa}
411110577Ssimokawa
412103285Sikob/*
413103285Sikob * The attach routine.
414103285Sikob */
415103285Sikobstatic int
416118455Ssimokawafirewire_attach(device_t dev)
417103285Sikob{
418118455Ssimokawa	int unit;
419103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
420103285Sikob	device_t pa = device_get_parent(dev);
421103285Sikob	struct firewire_comm *fc;
422103285Sikob
423103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
424103285Sikob	sc->fc = fc;
425116978Ssimokawa	fc->status = FWBUSNOTREADY;
426103285Sikob
427118455Ssimokawa	unit = device_get_unit(dev);
428118455Ssimokawa	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
429103285Sikob
430118455Ssimokawa	fwdev_makedev(sc);
431118455Ssimokawa
432170374Ssimokawa	mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF);
433170374Ssimokawa	CALLOUT_INIT(&fc->timeout_callout);
434170374Ssimokawa	CALLOUT_INIT(&fc->bmr_callout);
435170374Ssimokawa	CALLOUT_INIT(&fc->busprobe_callout);
436170374Ssimokawa	TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, (void *)fc);
437108853Ssimokawa
438110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
439110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
440110193Ssimokawa
441169806Ssimokawa	/* create thread */
442170374Ssimokawa	kthread_create(fw_bus_probe_thread, (void *)fc, &fc->probe_thread,
443169806Ssimokawa		0, 0, "fw%d_probe", unit);
444169806Ssimokawa
445103285Sikob	/* Locate our children */
446103285Sikob	bus_generic_probe(dev);
447103285Sikob
448103285Sikob	/* launch attachement of the added children */
449103285Sikob	bus_generic_attach(dev);
450103285Sikob
451103285Sikob	/* bus_reset */
452169117Ssimokawa	fw_busreset(fc, FWBUSNOTREADY);
453103285Sikob	fc->ibr(fc);
454103285Sikob
455103285Sikob	return 0;
456103285Sikob}
457103285Sikob
458103285Sikob/*
459103285Sikob * Attach it as child.
460103285Sikob */
461103285Sikobstatic device_t
462103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
463103285Sikob{
464103285Sikob        device_t child;
465103285Sikob	struct firewire_softc *sc;
466103285Sikob
467103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
468103285Sikob	child = device_add_child(dev, name, unit);
469103285Sikob	if (child) {
470103285Sikob		device_set_ivars(child, sc->fc);
471103285Sikob		device_probe_and_attach(child);
472103285Sikob	}
473103285Sikob
474103285Sikob	return child;
475103285Sikob}
476106790Ssimokawa
477116978Ssimokawastatic int
478116978Ssimokawafirewire_resume(device_t dev)
479116978Ssimokawa{
480116978Ssimokawa	struct firewire_softc *sc;
481116978Ssimokawa
482116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
483116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
484116978Ssimokawa
485116978Ssimokawa	bus_generic_resume(dev);
486116978Ssimokawa
487116978Ssimokawa	return(0);
488116978Ssimokawa}
489116978Ssimokawa
490103285Sikob/*
491103285Sikob * Dettach it.
492103285Sikob */
493103285Sikobstatic int
494118455Ssimokawafirewire_detach(device_t dev)
495103285Sikob{
496103285Sikob	struct firewire_softc *sc;
497169806Ssimokawa	struct firewire_comm *fc;
498111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
499118455Ssimokawa	int err;
500103285Sikob
501103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
502169806Ssimokawa	fc = sc->fc;
503170374Ssimokawa	mtx_lock(&fc->wait_lock);
504169806Ssimokawa	fc->status = FWBUSDETACH;
505170374Ssimokawa	wakeup(fc);
506170374Ssimokawa	if (msleep(fc->probe_thread, &fc->wait_lock, PWAIT, "fwthr", hz * 60))
507170374Ssimokawa		printf("firewire probe thread didn't die\n");
508170374Ssimokawa	mtx_unlock(&fc->wait_lock);
509169806Ssimokawa
510118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
511118455Ssimokawa		return err;
512106790Ssimokawa
513118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
514118455Ssimokawa		return err;
515111078Ssimokawa
516169806Ssimokawa	callout_stop(&fc->timeout_callout);
517169806Ssimokawa	callout_stop(&fc->bmr_callout);
518169806Ssimokawa	callout_stop(&fc->busprobe_callout);
519111078Ssimokawa
520103285Sikob	/* XXX xfree_free and untimeout on all xfers */
521169806Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
522111078Ssimokawa							fwdev = fwdev_next) {
523111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
524111078Ssimokawa		free(fwdev, M_FW);
525111078Ssimokawa	}
526169806Ssimokawa	free(fc->topology_map, M_FW);
527169806Ssimokawa	free(fc->speed_map, M_FW);
528169806Ssimokawa	free(fc->crom_src_buf, M_FW);
529169806Ssimokawa
530170374Ssimokawa	mtx_destroy(&fc->wait_lock);
531103285Sikob	return(0);
532103285Sikob}
533103285Sikob#if 0
534103285Sikobstatic int
535103285Sikobfirewire_shutdown( device_t dev )
536103285Sikob{
537103285Sikob	return 0;
538103285Sikob}
539103285Sikob#endif
540106790Ssimokawa
541110577Ssimokawa
542110577Ssimokawastatic void
543110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
544110577Ssimokawa{
545110577Ssimokawa	struct fw_xfer *xfer;
546110577Ssimokawa
547110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
548110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
549170374Ssimokawa#if 0
550111942Ssimokawa		xferq->queued --;
551170374Ssimokawa#endif
552110577Ssimokawa		xfer->resp = EAGAIN;
553170374Ssimokawa		xfer->flag = FWXF_SENTERR;
554113584Ssimokawa		fw_xfer_done(xfer);
555110577Ssimokawa	}
556110577Ssimokawa}
557110577Ssimokawa
558110798Ssimokawavoid
559110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
560110798Ssimokawa{
561170374Ssimokawa	struct fw_xfer *xfer, *txfer;
562170374Ssimokawa	STAILQ_HEAD(, fw_xfer) xfer_drain;
563110798Ssimokawa	int i;
564110798Ssimokawa
565170374Ssimokawa	STAILQ_INIT(&xfer_drain);
566170374Ssimokawa
567170374Ssimokawa	FW_GLOCK(fc);
568110798Ssimokawa	fw_xferq_drain(fc->atq);
569110798Ssimokawa	fw_xferq_drain(fc->ats);
570110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
571110798Ssimokawa		fw_xferq_drain(fc->it[i]);
572170374Ssimokawa
573170374Ssimokawa	for (i = 0; i < 0x40; i ++)
574170374Ssimokawa		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
575170374Ssimokawa			if (firewire_debug)
576170374Ssimokawa				printf("tl=%d flag=%d\n", i, xfer->flag);
577170374Ssimokawa			xfer->resp = EAGAIN;
578170374Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
579170374Ssimokawa			STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel);
580170374Ssimokawa		}
581170374Ssimokawa	FW_GUNLOCK(fc);
582170374Ssimokawa
583170374Ssimokawa	STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer)
584170374Ssimokawa		xfer->hand(xfer);
585110798Ssimokawa}
586110798Ssimokawa
587116376Ssimokawastatic void
588116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
589103285Sikob{
590103285Sikob	int i;
591103285Sikob
592103285Sikob	CSRARC(fc, STATE_CLEAR)
593103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
594103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
595103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
596103285Sikob
597103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
598103285Sikob	fc->irm = -1;
599103285Sikob
600103285Sikob	fc->max_node = -1;
601103285Sikob
602103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
603103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
604103285Sikob	}
605103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
606103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
607103285Sikob	CSRARC(fc, RESET_START) = 0;
608103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
609103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
610103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
611103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
612103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
613103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
614103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
615103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
616103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
617103285Sikob
618103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
619103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
620103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
621103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
622103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
623103285Sikob
624103285Sikob/* DV depend CSRs see blue book */
625103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
626103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
627103285Sikob
628103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
629103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
630116376Ssimokawa}
631113584Ssimokawa
632116376Ssimokawastatic void
633116376Ssimokawafw_init_crom(struct firewire_comm *fc)
634116376Ssimokawa{
635116376Ssimokawa	struct crom_src *src;
636116376Ssimokawa
637116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
638116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
639116376Ssimokawa	if (fc->crom_src_buf == NULL)
640116376Ssimokawa		return;
641116376Ssimokawa
642116376Ssimokawa	src = &fc->crom_src_buf->src;
643116376Ssimokawa	bzero(src, sizeof(struct crom_src));
644116376Ssimokawa
645116376Ssimokawa	/* BUS info sample */
646116376Ssimokawa	src->hdr.info_len = 4;
647116376Ssimokawa
648116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
649116376Ssimokawa
650116376Ssimokawa	src->businfo.irmc = 1;
651116376Ssimokawa	src->businfo.cmc = 1;
652116376Ssimokawa	src->businfo.isc = 1;
653116376Ssimokawa	src->businfo.bmc = 1;
654116376Ssimokawa	src->businfo.pmc = 0;
655116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
656116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
657116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
658117350Ssimokawa	src->businfo.generation = 1;
659116376Ssimokawa	src->businfo.link_spd = fc->speed;
660116376Ssimokawa
661116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
662116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
663116376Ssimokawa
664116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
665116376Ssimokawa
666116376Ssimokawa	fc->crom_src = src;
667116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
668116376Ssimokawa}
669116376Ssimokawa
670116376Ssimokawastatic void
671116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
672116376Ssimokawa{
673116376Ssimokawa	struct crom_src_buf *buf;
674116376Ssimokawa	struct crom_src *src;
675116376Ssimokawa	struct crom_chunk *root;
676116376Ssimokawa
677116376Ssimokawa	if (fc->crom_src_buf == NULL)
678116376Ssimokawa		fw_init_crom(fc);
679116376Ssimokawa
680116376Ssimokawa	buf =  fc->crom_src_buf;
681116376Ssimokawa	src = fc->crom_src;
682116376Ssimokawa	root = fc->crom_root;
683116376Ssimokawa
684116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
685116376Ssimokawa
686116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
687116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
688116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
689116376Ssimokawa	/* private company_id */
690116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
691127468Ssimokawa#ifdef __DragonFly__
692127468Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
693127468Ssimokawa	crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
694127468Ssimokawa#else
695116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
696116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
697127468Ssimokawa#endif
698116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
699116376Ssimokawa}
700116376Ssimokawa
701116376Ssimokawa/*
702116376Ssimokawa * Called after bus reset.
703116376Ssimokawa */
704116376Ssimokawavoid
705169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status)
706116376Ssimokawa{
707116376Ssimokawa	struct firewire_dev_comm *fdc;
708117350Ssimokawa	struct crom_src *src;
709116376Ssimokawa	device_t *devlistp;
710117350Ssimokawa	void *newrom;
711116376Ssimokawa	int i, devcnt;
712116376Ssimokawa
713116376Ssimokawa	switch(fc->status){
714116376Ssimokawa	case FWBUSMGRELECT:
715116376Ssimokawa		callout_stop(&fc->bmr_callout);
716116376Ssimokawa		break;
717116376Ssimokawa	default:
718116376Ssimokawa		break;
719116376Ssimokawa	}
720169117Ssimokawa	fc->status = new_status;
721116376Ssimokawa	fw_reset_csr(fc);
722116376Ssimokawa	fw_reset_crom(fc);
723116376Ssimokawa
724113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
725113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
726113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
727113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
728113584Ssimokawa				if (fdc->post_busreset != NULL)
729113584Ssimokawa					fdc->post_busreset(fdc);
730113584Ssimokawa			}
731113584Ssimokawa		free(devlistp, M_TEMP);
732113584Ssimokawa	}
733116376Ssimokawa
734117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
735117350Ssimokawa	src = &fc->crom_src_buf->src;
736129585Sdfr	crom_load(src, (uint32_t *)newrom, CROMSIZE);
737117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
738117350Ssimokawa		/* bump generation and reload */
739117350Ssimokawa		src->businfo.generation ++;
740117350Ssimokawa		/* generation must be between 0x2 and 0xF */
741117350Ssimokawa		if (src->businfo.generation < 2)
742117350Ssimokawa			src->businfo.generation ++;
743129585Sdfr		crom_load(src, (uint32_t *)newrom, CROMSIZE);
744117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
745117350Ssimokawa	}
746117350Ssimokawa	free(newrom, M_FW);
747103285Sikob}
748106790Ssimokawa
749103285Sikob/* Call once after reboot */
750106790Ssimokawavoid fw_init(struct firewire_comm *fc)
751103285Sikob{
752103285Sikob	int i;
753106543Ssimokawa#ifdef FW_VMACCESS
754103285Sikob	struct fw_xfer *xfer;
755103285Sikob	struct fw_bind *fwb;
756106543Ssimokawa#endif
757103285Sikob
758103285Sikob	fc->arq->queued = 0;
759103285Sikob	fc->ars->queued = 0;
760103285Sikob	fc->atq->queued = 0;
761103285Sikob	fc->ats->queued = 0;
762103285Sikob
763103285Sikob	fc->arq->buf = NULL;
764103285Sikob	fc->ars->buf = NULL;
765103285Sikob	fc->atq->buf = NULL;
766103285Sikob	fc->ats->buf = NULL;
767103285Sikob
768113584Ssimokawa	fc->arq->flag = 0;
769113584Ssimokawa	fc->ars->flag = 0;
770113584Ssimokawa	fc->atq->flag = 0;
771113584Ssimokawa	fc->ats->flag = 0;
772103285Sikob
773103285Sikob	STAILQ_INIT(&fc->atq->q);
774103285Sikob	STAILQ_INIT(&fc->ats->q);
775103285Sikob
776103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
777103285Sikob		fc->it[i]->queued = 0;
778103285Sikob		fc->ir[i]->queued = 0;
779103285Sikob
780103285Sikob		fc->it[i]->start = NULL;
781103285Sikob		fc->ir[i]->start = NULL;
782103285Sikob
783103285Sikob		fc->it[i]->buf = NULL;
784103285Sikob		fc->ir[i]->buf = NULL;
785103285Sikob
786103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
787103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
788103285Sikob
789103285Sikob		STAILQ_INIT(&fc->it[i]->q);
790103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
791103285Sikob	}
792103285Sikob
793103285Sikob	fc->arq->maxq = FWMAXQUEUE;
794103285Sikob	fc->ars->maxq = FWMAXQUEUE;
795103285Sikob	fc->atq->maxq = FWMAXQUEUE;
796103285Sikob	fc->ats->maxq = FWMAXQUEUE;
797103285Sikob
798103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
799103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
800103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
801103285Sikob	}
802103285Sikob/* Initialize csr registers */
803103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
804103285Sikob				sizeof(struct fw_topology_map),
805110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
806103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
807103285Sikob				sizeof(struct fw_speed_map),
808110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
809103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
810103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
811103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
812103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
813103285Sikob
814110193Ssimokawa	STAILQ_INIT(&fc->devices);
815103285Sikob
816103285Sikob/* Initialize Async handlers */
817103285Sikob	STAILQ_INIT(&fc->binds);
818103285Sikob	for( i = 0 ; i < 0x40 ; i++){
819103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
820103285Sikob	}
821103285Sikob
822103285Sikob/* DV depend CSRs see blue book */
823103285Sikob#if 0
824103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
825103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
826103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
827103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
828103285Sikob	}
829103285Sikob
830103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
831103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
832103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
833103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
834103285Sikob	}
835103285Sikob#endif
836103285Sikob
837116376Ssimokawa	fc->crom_src_buf = NULL;
838103285Sikob
839106543Ssimokawa#ifdef FW_VMACCESS
840103285Sikob	xfer = fw_xfer_alloc();
841103285Sikob	if(xfer == NULL) return;
842103285Sikob
843110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
844103285Sikob	if(fwb == NULL){
845103285Sikob		fw_xfer_free(xfer);
846139680Sjmg		return;
847103285Sikob	}
848167632Ssimokawa	xfer->hand = fw_vmaccess;
849103285Sikob	xfer->fc = fc;
850103285Sikob	xfer->sc = NULL;
851103285Sikob
852103285Sikob	fwb->start_hi = 0x2;
853103285Sikob	fwb->start_lo = 0;
854103285Sikob	fwb->addrlen = 0xffffffff;
855103285Sikob	fwb->xfer = xfer;
856103285Sikob	fw_bindadd(fc, fwb);
857106543Ssimokawa#endif
858103285Sikob}
859106790Ssimokawa
860120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
861120660Ssimokawa    ((fwb)->end < (addr))?1:0)
862120660Ssimokawa
863103285Sikob/*
864129541Sdfr * To lookup bound process from IEEE1394 address.
865103285Sikob */
866106813Ssimokawastruct fw_bind *
867129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
868103285Sikob{
869120660Ssimokawa	u_int64_t addr;
870170374Ssimokawa	struct fw_bind *tfw, *r = NULL;
871120660Ssimokawa
872120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
873170374Ssimokawa	FW_GLOCK(fc);
874120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
875170374Ssimokawa		if (BIND_CMP(addr, tfw) == 0) {
876170374Ssimokawa			r = tfw;
877170374Ssimokawa			break;
878170374Ssimokawa		}
879170374Ssimokawa	FW_GUNLOCK(fc);
880170374Ssimokawa	return(r);
881103285Sikob}
882106790Ssimokawa
883103285Sikob/*
884103285Sikob * To bind IEEE1394 address block to process.
885103285Sikob */
886106790Ssimokawaint
887106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
888103285Sikob{
889120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
890170374Ssimokawa	int r = 0;
891120660Ssimokawa
892120660Ssimokawa	if (fwb->start > fwb->end) {
893127468Ssimokawa		printf("%s: invalid range\n", __func__);
894120660Ssimokawa		return EINVAL;
895120660Ssimokawa	}
896120660Ssimokawa
897170374Ssimokawa	FW_GLOCK(fc);
898120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
899120660Ssimokawa		if (fwb->end < tfw->start)
900120660Ssimokawa			break;
901120660Ssimokawa		prev = tfw;
902120660Ssimokawa	}
903170374Ssimokawa	if (prev == NULL)
904103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
905170374Ssimokawa	else if (prev->end < fwb->start)
906120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
907170374Ssimokawa	else {
908170374Ssimokawa		printf("%s: bind failed\n", __func__);
909170374Ssimokawa		r = EBUSY;
910103285Sikob	}
911170374Ssimokawa	FW_GUNLOCK(fc);
912170374Ssimokawa	return (r);
913103285Sikob}
914103285Sikob
915103285Sikob/*
916103285Sikob * To free IEEE1394 address block.
917103285Sikob */
918106790Ssimokawaint
919106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
920103285Sikob{
921120660Ssimokawa#if 0
922120660Ssimokawa	struct fw_xfer *xfer, *next;
923120660Ssimokawa#endif
924120660Ssimokawa	struct fw_bind *tfw;
925103285Sikob	int s;
926103285Sikob
927103285Sikob	s = splfw();
928170374Ssimokawa	FW_GLOCK(fc);
929120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
930120660Ssimokawa		if (tfw == fwb) {
931120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
932120660Ssimokawa			goto found;
933120660Ssimokawa		}
934120660Ssimokawa
935129541Sdfr	printf("%s: no such binding\n", __func__);
936170374Ssimokawa	FW_GUNLOCK(fc);
937120660Ssimokawa	splx(s);
938120660Ssimokawa	return (1);
939120660Ssimokawafound:
940120660Ssimokawa#if 0
941113584Ssimokawa	/* shall we do this? */
942113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
943113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
944113584Ssimokawa		fw_xfer_free(xfer);
945113584Ssimokawa	}
946113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
947120660Ssimokawa#endif
948170374Ssimokawa	FW_GUNLOCK(fc);
949113584Ssimokawa
950103285Sikob	splx(s);
951103285Sikob	return 0;
952103285Sikob}
953103285Sikob
954169130Ssimokawaint
955169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
956169130Ssimokawa    int slen, int rlen, int n,
957169130Ssimokawa    struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
958169130Ssimokawa{
959169130Ssimokawa	int i, s;
960169130Ssimokawa	struct fw_xfer *xfer;
961169130Ssimokawa
962169130Ssimokawa	for (i = 0; i < n; i++) {
963169130Ssimokawa		xfer = fw_xfer_alloc_buf(type, slen, rlen);
964169130Ssimokawa		if (xfer == NULL)
965169130Ssimokawa			return (n);
966169130Ssimokawa		xfer->fc = fc;
967169130Ssimokawa		xfer->sc = sc;
968169130Ssimokawa		xfer->hand = hand;
969169130Ssimokawa		s = splfw();
970169130Ssimokawa		STAILQ_INSERT_TAIL(q, xfer, link);
971169130Ssimokawa		splx(s);
972169130Ssimokawa	}
973169130Ssimokawa	return (n);
974169130Ssimokawa}
975169130Ssimokawa
976169130Ssimokawavoid
977169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q)
978169130Ssimokawa{
979169130Ssimokawa	struct fw_xfer *xfer, *next;
980169130Ssimokawa
981169130Ssimokawa	for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
982169130Ssimokawa                next = STAILQ_NEXT(xfer, link);
983169130Ssimokawa                fw_xfer_free_buf(xfer);
984169130Ssimokawa        }
985169130Ssimokawa        STAILQ_INIT(q);
986169130Ssimokawa}
987170374Ssimokawa/*
988170374Ssimokawa * dump packet header
989170374Ssimokawa */
990170374Ssimokawastatic void
991170374Ssimokawafw_dump_hdr(struct fw_pkt *fp, char *prefix)
992170374Ssimokawa{
993170374Ssimokawa	printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
994170374Ssimokawa	    "src=0x%03x\n", prefix,
995170374Ssimokawa	    fp->mode.hdr.dst & 0x3f,
996170374Ssimokawa	    fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3,
997170374Ssimokawa	    fp->mode.hdr.tcode, fp->mode.hdr.pri,
998170374Ssimokawa	    fp->mode.hdr.src);
999170374Ssimokawa}
1000169130Ssimokawa
1001103285Sikob/*
1002103285Sikob * To free transaction label.
1003103285Sikob */
1004106790Ssimokawastatic void
1005106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
1006103285Sikob{
1007169119Ssimokawa	struct fw_xfer *txfer;
1008169119Ssimokawa	int s;
1009103285Sikob
1010169119Ssimokawa	if (xfer->tl < 0)
1011169119Ssimokawa		return;
1012169119Ssimokawa
1013169119Ssimokawa	s = splfw();
1014170374Ssimokawa	FW_GLOCK(fc);
1015169119Ssimokawa#if 1	/* make sure the label is allocated */
1016169119Ssimokawa	STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
1017169119Ssimokawa		if(txfer == xfer)
1018169119Ssimokawa			break;
1019169119Ssimokawa	if (txfer == NULL) {
1020170374Ssimokawa		printf("%s: the xfer is not in the queue "
1021170374Ssimokawa		    "(tlabel=%d, flag=0x%x)\n",
1022170374Ssimokawa		    __FUNCTION__, xfer->tl, xfer->flag);
1023170374Ssimokawa		fw_dump_hdr(&xfer->send.hdr, "send");
1024170374Ssimokawa		fw_dump_hdr(&xfer->recv.hdr, "recv");
1025170374Ssimokawa		kdb_backtrace();
1026170374Ssimokawa		FW_GUNLOCK(fc);
1027169119Ssimokawa		splx(s);
1028169119Ssimokawa		return;
1029103285Sikob	}
1030169119Ssimokawa#endif
1031169119Ssimokawa
1032169119Ssimokawa	STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
1033170374Ssimokawa	FW_GUNLOCK(fc);
1034103285Sikob	splx(s);
1035103285Sikob	return;
1036103285Sikob}
1037106790Ssimokawa
1038103285Sikob/*
1039103285Sikob * To obtain XFER structure by transaction label.
1040103285Sikob */
1041106790Ssimokawastatic struct fw_xfer *
1042170374Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode)
1043103285Sikob{
1044103285Sikob	struct fw_xfer *xfer;
1045103285Sikob	int s = splfw();
1046170374Ssimokawa	int req;
1047103285Sikob
1048170374Ssimokawa	FW_GLOCK(fc);
1049169119Ssimokawa	STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
1050169119Ssimokawa		if(xfer->send.hdr.mode.hdr.dst == node) {
1051170374Ssimokawa			FW_GUNLOCK(fc);
1052103285Sikob			splx(s);
1053170374Ssimokawa			KASSERT(xfer->tl == tlabel,
1054170374Ssimokawa				("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel));
1055170374Ssimokawa			/* extra sanity check */
1056170374Ssimokawa			req = xfer->send.hdr.mode.hdr.tcode;
1057170374Ssimokawa			if (xfer->fc->tcode[req].valid_res != tcode) {
1058170374Ssimokawa				printf("%s: invalid response tcode "
1059170374Ssimokawa				    "(0x%x for 0x%x)\n", __FUNCTION__,
1060170374Ssimokawa				    tcode, req);
1061170374Ssimokawa				return(NULL);
1062170374Ssimokawa			}
1063170374Ssimokawa
1064110577Ssimokawa			if (firewire_debug > 2)
1065110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
1066103285Sikob			return(xfer);
1067103285Sikob		}
1068170374Ssimokawa	FW_GUNLOCK(fc);
1069110577Ssimokawa	if (firewire_debug > 1)
1070110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
1071103285Sikob	splx(s);
1072103285Sikob	return(NULL);
1073103285Sikob}
1074106790Ssimokawa
1075103285Sikob/*
1076103285Sikob * To allocate IEEE1394 XFER structure.
1077103285Sikob */
1078106790Ssimokawastruct fw_xfer *
1079110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
1080103285Sikob{
1081103285Sikob	struct fw_xfer *xfer;
1082106790Ssimokawa
1083110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
1084106790Ssimokawa	if (xfer == NULL)
1085106790Ssimokawa		return xfer;
1086106790Ssimokawa
1087110269Ssimokawa	xfer->malloc = type;
1088106790Ssimokawa
1089103285Sikob	return xfer;
1090103285Sikob}
1091106790Ssimokawa
1092113584Ssimokawastruct fw_xfer *
1093113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
1094113584Ssimokawa{
1095113584Ssimokawa	struct fw_xfer *xfer;
1096113584Ssimokawa
1097113584Ssimokawa	xfer = fw_xfer_alloc(type);
1098126102Scperciva	if (xfer == NULL)
1099126102Scperciva		return(NULL);
1100120660Ssimokawa	xfer->send.pay_len = send_len;
1101120660Ssimokawa	xfer->recv.pay_len = recv_len;
1102120660Ssimokawa	if (send_len > 0) {
1103120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
1104120660Ssimokawa		if (xfer->send.payload == NULL) {
1105113584Ssimokawa			fw_xfer_free(xfer);
1106113584Ssimokawa			return(NULL);
1107113584Ssimokawa		}
1108113584Ssimokawa	}
1109120660Ssimokawa	if (recv_len > 0) {
1110120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1111120660Ssimokawa		if (xfer->recv.payload == NULL) {
1112120660Ssimokawa			if (xfer->send.payload != NULL)
1113120660Ssimokawa				free(xfer->send.payload, type);
1114113584Ssimokawa			fw_xfer_free(xfer);
1115113584Ssimokawa			return(NULL);
1116113584Ssimokawa		}
1117113584Ssimokawa	}
1118113584Ssimokawa	return(xfer);
1119113584Ssimokawa}
1120113584Ssimokawa
1121103285Sikob/*
1122103285Sikob * IEEE1394 XFER post process.
1123103285Sikob */
1124103285Sikobvoid
1125103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1126103285Sikob{
1127167632Ssimokawa	if (xfer->hand == NULL) {
1128167632Ssimokawa		printf("hand == NULL\n");
1129103285Sikob		return;
1130117716Ssimokawa	}
1131103285Sikob
1132121505Ssimokawa	if (xfer->fc == NULL)
1133121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
1134121505Ssimokawa
1135168051Ssimokawa	fw_tl_free(xfer->fc, xfer);
1136167632Ssimokawa	xfer->hand(xfer);
1137103285Sikob}
1138103285Sikob
1139106790Ssimokawavoid
1140113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
1141103285Sikob{
1142103285Sikob	int s;
1143113584Ssimokawa
1144103285Sikob	if(xfer == NULL ) return;
1145170374Ssimokawa	if(xfer->flag & FWXF_INQ){
1146103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1147103285Sikob		s = splfw();
1148170374Ssimokawa		FW_GLOCK(xfer->fc);
1149103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1150170374Ssimokawa#if 0
1151103285Sikob		xfer->q->queued --;
1152170374Ssimokawa#endif
1153170374Ssimokawa		FW_GUNLOCK(xfer->fc);
1154103285Sikob		splx(s);
1155103285Sikob	}
1156113584Ssimokawa	if (xfer->fc != NULL) {
1157114729Ssimokawa#if 1
1158170374Ssimokawa		if(xfer->flag & FWXF_START)
1159114729Ssimokawa			/*
1160114729Ssimokawa			 * This could happen if:
1161114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1162114729Ssimokawa			 *  2. firewire_watch() is called.
1163114729Ssimokawa			 */
1164114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1165103285Sikob#endif
1166103285Sikob	}
1167170374Ssimokawa	xfer->flag = FWXF_INIT;
1168113584Ssimokawa	xfer->resp = 0;
1169113584Ssimokawa}
1170113584Ssimokawa/*
1171113584Ssimokawa * To free IEEE1394 XFER structure.
1172113584Ssimokawa */
1173113584Ssimokawavoid
1174120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer)
1175113584Ssimokawa{
1176120660Ssimokawa	if (xfer == NULL) {
1177127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1178120660Ssimokawa		return;
1179120660Ssimokawa	}
1180113584Ssimokawa	fw_xfer_unload(xfer);
1181120660Ssimokawa	if(xfer->send.payload != NULL){
1182120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1183103285Sikob	}
1184120660Ssimokawa	if(xfer->recv.payload != NULL){
1185120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1186103285Sikob	}
1187110269Ssimokawa	free(xfer, xfer->malloc);
1188103285Sikob}
1189103285Sikob
1190120660Ssimokawavoid
1191120660Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1192120660Ssimokawa{
1193120660Ssimokawa	if (xfer == NULL) {
1194127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1195120660Ssimokawa		return;
1196120660Ssimokawa	}
1197120660Ssimokawa	fw_xfer_unload(xfer);
1198120660Ssimokawa	free(xfer, xfer->malloc);
1199120660Ssimokawa}
1200120660Ssimokawa
1201120660Ssimokawavoid
1202110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1203103285Sikob{
1204103285Sikob#if 0
1205170374Ssimokawa	printf("asyreq done flag=0x%02x resp=%d\n",
1206170374Ssimokawa				xfer->flag, xfer->resp);
1207103285Sikob#endif
1208103285Sikob	fw_xfer_free(xfer);
1209103285Sikob}
1210103285Sikob
1211103285Sikob/*
1212103285Sikob * To configure PHY.
1213103285Sikob */
1214103285Sikobstatic void
1215103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1216103285Sikob{
1217103285Sikob	struct fw_xfer *xfer;
1218103285Sikob	struct fw_pkt *fp;
1219103285Sikob
1220103285Sikob	fc->status = FWBUSPHYCONF;
1221103285Sikob
1222120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1223113584Ssimokawa	if (xfer == NULL)
1224113584Ssimokawa		return;
1225103285Sikob	xfer->fc = fc;
1226167632Ssimokawa	xfer->hand = fw_asy_callback_free;
1227103285Sikob
1228120660Ssimokawa	fp = &xfer->send.hdr;
1229103285Sikob	fp->mode.ld[1] = 0;
1230103285Sikob	if (root_node >= 0)
1231113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1232103285Sikob	if (gap_count >= 0)
1233113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1234103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1235103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1236103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1237103285Sikob
1238107653Ssimokawa	if (firewire_debug)
1239107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1240103285Sikob						root_node, gap_count);
1241103285Sikob	fw_asyreq(fc, -1, xfer);
1242103285Sikob}
1243103285Sikob
1244103285Sikob#if 0
1245103285Sikob/*
1246103285Sikob * Dump self ID.
1247103285Sikob */
1248103285Sikobstatic void
1249129585Sdfrfw_print_sid(uint32_t sid)
1250103285Sikob{
1251103285Sikob	union fw_self_id *s;
1252103285Sikob	s = (union fw_self_id *) &sid;
1253103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1254103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1255103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1256103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1257103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1258103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1259103285Sikob}
1260103285Sikob#endif
1261103285Sikob
1262103285Sikob/*
1263103285Sikob * To receive self ID.
1264103285Sikob */
1265129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1266103285Sikob{
1267129585Sdfr	uint32_t *p;
1268103285Sikob	union fw_self_id *self_id;
1269103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1270103285Sikob
1271129585Sdfr	fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1272103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1273129585Sdfr	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1274103285Sikob	fc->status = FWBUSCYMELECT;
1275103285Sikob	fc->topology_map->crc_len = 2;
1276103285Sikob	fc->topology_map->generation ++;
1277103285Sikob	fc->topology_map->self_id_count = 0;
1278103285Sikob	fc->topology_map->node_count = 0;
1279103285Sikob	fc->speed_map->generation ++;
1280103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1281103285Sikob	self_id = &fc->topology_map->self_id[0];
1282103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1283103285Sikob		if (sid[1] != ~sid[0]) {
1284103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1285103285Sikob			sid += 2;
1286103285Sikob			continue;
1287103285Sikob		}
1288103285Sikob		*self_id = *((union fw_self_id *)sid);
1289103285Sikob		fc->topology_map->crc_len++;
1290103285Sikob		if(self_id->p0.sequel == 0){
1291103285Sikob			fc->topology_map->node_count ++;
1292103285Sikob			c_port = 0;
1293103285Sikob#if 0
1294103285Sikob			fw_print_sid(sid[0]);
1295103285Sikob#endif
1296103285Sikob			node = self_id->p0.phy_id;
1297103285Sikob			if(fc->max_node < node){
1298103285Sikob				fc->max_node = self_id->p0.phy_id;
1299103285Sikob			}
1300103285Sikob			/* XXX I'm not sure this is the right speed_map */
1301103285Sikob			fc->speed_map->speed[node][node]
1302103285Sikob					= self_id->p0.phy_speed;
1303103285Sikob			for (j = 0; j < node; j ++) {
1304103285Sikob				fc->speed_map->speed[j][node]
1305103285Sikob					= fc->speed_map->speed[node][j]
1306103285Sikob					= min(fc->speed_map->speed[j][j],
1307103285Sikob							self_id->p0.phy_speed);
1308103285Sikob			}
1309103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1310103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1311103285Sikob				fc->irm = self_id->p0.phy_id;
1312103285Sikob			}
1313103285Sikob			if(self_id->p0.port0 >= 0x2){
1314103285Sikob				c_port++;
1315103285Sikob			}
1316103285Sikob			if(self_id->p0.port1 >= 0x2){
1317103285Sikob				c_port++;
1318103285Sikob			}
1319103285Sikob			if(self_id->p0.port2 >= 0x2){
1320103285Sikob				c_port++;
1321103285Sikob			}
1322103285Sikob		}
1323103285Sikob		if(c_port > 2){
1324103285Sikob			i_branch += (c_port - 2);
1325103285Sikob		}
1326103285Sikob		sid += 2;
1327103285Sikob		self_id++;
1328103285Sikob		fc->topology_map->self_id_count ++;
1329103285Sikob	}
1330108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1331103285Sikob	/* CRC */
1332103285Sikob	fc->topology_map->crc = fw_crc16(
1333129585Sdfr			(uint32_t *)&fc->topology_map->generation,
1334103285Sikob			fc->topology_map->crc_len * 4);
1335103285Sikob	fc->speed_map->crc = fw_crc16(
1336129585Sdfr			(uint32_t *)&fc->speed_map->generation,
1337103285Sikob			fc->speed_map->crc_len * 4);
1338103285Sikob	/* byteswap and copy to CSR */
1339129585Sdfr	p = (uint32_t *)fc->topology_map;
1340103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1341103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1342129585Sdfr	p = (uint32_t *)fc->speed_map;
1343103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1344103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1345129585Sdfr	/* don't byte-swap uint8_t array */
1346103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1347103285Sikob
1348103285Sikob	fc->max_hop = fc->max_node - i_branch;
1349103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1350103285Sikob
1351103285Sikob	if(fc->irm == -1 ){
1352103285Sikob		printf(", Not found IRM capable node");
1353103285Sikob	}else{
1354103285Sikob		printf(", cable IRM = %d", fc->irm);
1355103285Sikob		if (fc->irm == fc->nodeid)
1356110016Ssimokawa			printf(" (me)");
1357103285Sikob	}
1358110016Ssimokawa	printf("\n");
1359103285Sikob
1360109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1361114909Ssimokawa		if (fc->irm == fc->nodeid) {
1362103285Sikob			fc->status = FWBUSMGRDONE;
1363103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1364114909Ssimokawa			fw_bmr(fc);
1365109736Ssimokawa		} else {
1366103285Sikob			fc->status = FWBUSMGRELECT;
1367110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1368110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1369103285Sikob		}
1370114909Ssimokawa	} else
1371103285Sikob		fc->status = FWBUSMGRDONE;
1372114909Ssimokawa
1373108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1374108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1375103285Sikob}
1376106790Ssimokawa
1377103285Sikob/*
1378103285Sikob * To probe devices on the IEEE1394 bus.
1379103285Sikob */
1380106790Ssimokawastatic void
1381106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1382103285Sikob{
1383103285Sikob	int s;
1384120850Ssimokawa	struct fw_device *fwdev;
1385103285Sikob
1386103285Sikob	s = splfw();
1387103285Sikob	fc->status = FWBUSEXPLORE;
1388103285Sikob
1389120850Ssimokawa	/* Invalidate all devices, just after bus reset. */
1390120850Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1391110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1392103285Sikob			fwdev->status = FWDEVINVAL;
1393103285Sikob			fwdev->rcnt = 0;
1394103285Sikob		}
1395169806Ssimokawa	splx(s);
1396120850Ssimokawa
1397169806Ssimokawa	wakeup((void *)fc);
1398103285Sikob}
1399106790Ssimokawa
1400169806Ssimokawastatic int
1401169806Ssimokawafw_explore_read_quads(struct fw_device *fwdev, int offset,
1402169806Ssimokawa    uint32_t *quad, int n)
1403129274Sdfr{
1404169806Ssimokawa	struct fw_xfer *xfer;
1405169806Ssimokawa	uint32_t tmp;
1406169806Ssimokawa	int i, error;
1407129274Sdfr
1408169806Ssimokawa	for (i = 0; i < n; i ++, offset += sizeof(uint32_t)) {
1409169806Ssimokawa		xfer = fwmem_read_quad(fwdev, NULL, -1,
1410169806Ssimokawa		    0xffff, 0xf0000000 | offset, (void *)&tmp,
1411170374Ssimokawa		    fw_xferwake);
1412169806Ssimokawa		if (xfer == NULL)
1413169806Ssimokawa			return (-1);
1414170374Ssimokawa		fw_xferwait(xfer);
1415169806Ssimokawa
1416169806Ssimokawa		if (xfer->resp == 0)
1417169806Ssimokawa			quad[i] = ntohl(tmp);
1418169806Ssimokawa
1419169806Ssimokawa		error = xfer->resp;
1420169806Ssimokawa		fw_xfer_free(xfer);
1421169806Ssimokawa		if (error)
1422169806Ssimokawa			return (error);
1423169806Ssimokawa	}
1424169806Ssimokawa	return (0);
1425169806Ssimokawa}
1426169806Ssimokawa
1427169806Ssimokawa
1428169806Ssimokawastatic int
1429169806Ssimokawafw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
1430169806Ssimokawa{
1431169806Ssimokawa	int err, i, off;
1432169806Ssimokawa	struct csrdirectory *dir;
1433169806Ssimokawa	struct csrreg *reg;
1434169806Ssimokawa
1435169806Ssimokawa	dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1436169806Ssimokawa	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1437169806Ssimokawa	    (uint32_t *)dir, 1);
1438169806Ssimokawa	if (err)
1439169806Ssimokawa		return (-1);
1440169806Ssimokawa
1441169806Ssimokawa	offset += sizeof(uint32_t);
1442169806Ssimokawa	reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1443169806Ssimokawa	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1444169806Ssimokawa	    (uint32_t *)reg, dir->crc_len);
1445169806Ssimokawa	if (err)
1446169806Ssimokawa		return (-1);
1447169806Ssimokawa
1448169806Ssimokawa	/* XXX check CRC */
1449169806Ssimokawa
1450169806Ssimokawa	off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
1451169806Ssimokawa	if (fwdev->rommax < off)
1452169806Ssimokawa		fwdev->rommax = off;
1453169806Ssimokawa
1454169806Ssimokawa	if (recur == 0)
1455169806Ssimokawa		return (0);
1456169806Ssimokawa
1457169806Ssimokawa	for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) {
1458171302Ssimokawa		if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D)
1459169806Ssimokawa			recur = 1;
1460171302Ssimokawa		else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L)
1461169806Ssimokawa			recur = 0;
1462169806Ssimokawa		else
1463129274Sdfr			continue;
1464169806Ssimokawa
1465169806Ssimokawa		off = offset + reg[i].val * sizeof(uint32_t);
1466169806Ssimokawa		if (off > CROMSIZE) {
1467169806Ssimokawa			printf("%s: invalid offset %d\n", __FUNCTION__, off);
1468169806Ssimokawa			return(-1);
1469169806Ssimokawa		}
1470169806Ssimokawa		err = fw_explore_csrblock(fwdev, off, recur);
1471169806Ssimokawa		if (err)
1472169806Ssimokawa			return (-1);
1473129274Sdfr	}
1474169806Ssimokawa	return (0);
1475129274Sdfr}
1476129274Sdfr
1477169806Ssimokawastatic int
1478169806Ssimokawafw_explore_node(struct fw_device *dfwdev)
1479103285Sikob{
1480169806Ssimokawa	struct firewire_comm *fc;
1481110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1482169806Ssimokawa	uint32_t *csr;
1483169806Ssimokawa	struct csrhdr *hdr;
1484169806Ssimokawa	struct bus_info *binfo;
1485169806Ssimokawa	int err, node, spd;
1486103285Sikob
1487169806Ssimokawa	fc = dfwdev->fc;
1488169806Ssimokawa	csr = dfwdev->csrrom;
1489169806Ssimokawa	node = dfwdev->dst;
1490103285Sikob
1491169806Ssimokawa	/* First quad */
1492169806Ssimokawa	err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1);
1493169806Ssimokawa	if (err)
1494169806Ssimokawa		return (-1);
1495169806Ssimokawa	hdr = (struct csrhdr *)&csr[0];
1496169806Ssimokawa	if (hdr->info_len != 4) {
1497169806Ssimokawa		if (firewire_debug)
1498169806Ssimokawa			printf("node%d: wrong bus info len(%d)\n",
1499169806Ssimokawa			    node, hdr->info_len);
1500169806Ssimokawa		return (-1);
1501169806Ssimokawa	}
1502103285Sikob
1503169806Ssimokawa	/* bus info */
1504169806Ssimokawa	err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
1505169806Ssimokawa	if (err)
1506169806Ssimokawa		return (-1);
1507169806Ssimokawa	binfo = (struct bus_info *)&csr[1];
1508169806Ssimokawa	if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
1509110577Ssimokawa		if (firewire_debug)
1510169806Ssimokawa			printf("node%d: invalid bus name 0x%08x\n",
1511169806Ssimokawa			    node, binfo->bus_name);
1512169806Ssimokawa		return (-1);
1513103285Sikob	}
1514169806Ssimokawa	spd = fc->speed_map->speed[fc->nodeid][node];
1515169806Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1516169806Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
1517169806Ssimokawa			break;
1518169806Ssimokawa	if (fwdev == NULL) {
1519169806Ssimokawa		/* new device */
1520169806Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1521169806Ssimokawa						M_NOWAIT | M_ZERO);
1522169806Ssimokawa		if (fwdev == NULL) {
1523169806Ssimokawa			if (firewire_debug)
1524169806Ssimokawa				printf("node%d: no memory\n", node);
1525169806Ssimokawa			return (-1);
1526103285Sikob		}
1527106810Ssimokawa		fwdev->fc = fc;
1528169806Ssimokawa		fwdev->eui = binfo->eui64;
1529169806Ssimokawa		/* inesrt into sorted fwdev list */
1530110179Ssimokawa		pfwdev = NULL;
1531110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1532110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1533169806Ssimokawa				(tfwdev->eui.hi == fwdev->eui.hi &&
1534169806Ssimokawa				tfwdev->eui.lo > fwdev->eui.lo))
1535110179Ssimokawa				break;
1536110179Ssimokawa			pfwdev = tfwdev;
1537103285Sikob		}
1538110179Ssimokawa		if (pfwdev == NULL)
1539110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1540110179Ssimokawa		else
1541110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1542103285Sikob
1543108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1544169806Ssimokawa		    linkspeed[spd],
1545169806Ssimokawa		    fwdev->eui.hi, fwdev->eui.lo);
1546103285Sikob	}
1547169806Ssimokawa	fwdev->dst = node;
1548169806Ssimokawa	fwdev->status = FWDEVINIT;
1549169806Ssimokawa	fwdev->speed = spd;
1550103285Sikob
1551169806Ssimokawa	/* unchanged ? */
1552169806Ssimokawa	if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) {
1553169806Ssimokawa		if (firewire_debug)
1554169806Ssimokawa			printf("node%d: crom unchanged\n", node);
1555169806Ssimokawa		return (0);
1556103285Sikob	}
1557103285Sikob
1558169806Ssimokawa	bzero(&fwdev->csrrom[0], CROMSIZE);
1559106790Ssimokawa
1560169806Ssimokawa	/* copy first quad and bus info block */
1561169806Ssimokawa	bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5);
1562169806Ssimokawa	fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
1563103285Sikob
1564169806Ssimokawa	err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
1565113584Ssimokawa
1566169806Ssimokawa	if (err) {
1567169806Ssimokawa		fwdev->status = FWDEVINVAL;
1568169806Ssimokawa		fwdev->csrrom[0] = 0;
1569103285Sikob	}
1570169806Ssimokawa	return (err);
1571103285Sikob
1572103285Sikob}
1573106790Ssimokawa
1574103285Sikob/*
1575169806Ssimokawa * Find the self_id packet for a node, ignoring sequels.
1576103285Sikob */
1577169806Ssimokawastatic union fw_self_id *
1578169806Ssimokawafw_find_self_id(struct firewire_comm *fc, int node)
1579106790Ssimokawa{
1580169806Ssimokawa	uint32_t i;
1581169806Ssimokawa	union fw_self_id *s;
1582103285Sikob
1583169806Ssimokawa	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1584169806Ssimokawa		s = &fc->topology_map->self_id[i];
1585169806Ssimokawa		if (s->p0.sequel)
1586169806Ssimokawa			continue;
1587169806Ssimokawa		if (s->p0.phy_id == node)
1588169806Ssimokawa			return s;
1589110577Ssimokawa	}
1590169806Ssimokawa	return 0;
1591169806Ssimokawa}
1592110577Ssimokawa
1593169806Ssimokawastatic void
1594169806Ssimokawafw_explore(struct firewire_comm *fc)
1595169806Ssimokawa{
1596169806Ssimokawa	int node, err, s, i, todo, todo2, trys;
1597169806Ssimokawa	char nodes[63];
1598169806Ssimokawa	struct fw_device dfwdev;
1599169829Ssimokawa	union fw_self_id *fwsid;
1600110577Ssimokawa
1601169806Ssimokawa	todo = 0;
1602169806Ssimokawa	/* setup dummy fwdev */
1603169806Ssimokawa	dfwdev.fc = fc;
1604169806Ssimokawa	dfwdev.speed = 0;
1605169806Ssimokawa	dfwdev.maxrec = 8; /* 512 */
1606169806Ssimokawa	dfwdev.status = FWDEVINIT;
1607103285Sikob
1608169806Ssimokawa	for (node = 0; node <= fc->max_node; node ++) {
1609169806Ssimokawa		/* We don't probe myself and linkdown nodes */
1610169806Ssimokawa		if (node == fc->nodeid)
1611169806Ssimokawa			continue;
1612169829Ssimokawa		fwsid = fw_find_self_id(fc, node);
1613169829Ssimokawa		if (!fwsid || !fwsid->p0.link_active) {
1614169806Ssimokawa			if (firewire_debug)
1615169806Ssimokawa				printf("node%d: link down\n", node);
1616169806Ssimokawa			continue;
1617103285Sikob		}
1618169806Ssimokawa		nodes[todo++] = node;
1619103285Sikob	}
1620103285Sikob
1621169806Ssimokawa	s = splfw();
1622169806Ssimokawa	for (trys = 0; todo > 0 && trys < 3; trys ++) {
1623169806Ssimokawa		todo2 = 0;
1624169806Ssimokawa		for (i = 0; i < todo; i ++) {
1625169806Ssimokawa			dfwdev.dst = nodes[i];
1626169806Ssimokawa			err = fw_explore_node(&dfwdev);
1627169806Ssimokawa			if (err)
1628169806Ssimokawa				nodes[todo2++] = nodes[i];
1629169806Ssimokawa			if (firewire_debug)
1630169806Ssimokawa				printf("%s: node %d, err = %d\n",
1631169806Ssimokawa					__FUNCTION__, node, err);
1632103285Sikob		}
1633169806Ssimokawa		todo = todo2;
1634103285Sikob	}
1635169806Ssimokawa	splx(s);
1636169806Ssimokawa}
1637169806Ssimokawa
1638169806Ssimokawa
1639169806Ssimokawastatic void
1640169806Ssimokawafw_bus_probe_thread(void *arg)
1641169806Ssimokawa{
1642169806Ssimokawa	struct firewire_comm *fc;
1643169806Ssimokawa
1644169806Ssimokawa	fc = (struct firewire_comm *)arg;
1645169806Ssimokawa
1646170374Ssimokawa	mtx_lock(&fc->wait_lock);
1647170374Ssimokawa	while (fc->status != FWBUSDETACH) {
1648169806Ssimokawa		if (fc->status == FWBUSEXPLORE) {
1649170374Ssimokawa			mtx_unlock(&fc->wait_lock);
1650169806Ssimokawa			fw_explore(fc);
1651169806Ssimokawa			fc->status = FWBUSEXPDONE;
1652169806Ssimokawa			if (firewire_debug)
1653169806Ssimokawa				printf("bus_explore done\n");
1654169806Ssimokawa			fw_attach_dev(fc);
1655170374Ssimokawa			mtx_lock(&fc->wait_lock);
1656170374Ssimokawa		}
1657170374Ssimokawa		msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0);
1658127468Ssimokawa	}
1659170374Ssimokawa	mtx_unlock(&fc->wait_lock);
1660169806Ssimokawa	kthread_exit(0);
1661103285Sikob}
1662103285Sikob
1663103285Sikob/*
1664103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1665103285Sikob */
1666106815Ssimokawastatic void
1667106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1668103285Sikob{
1669120850Ssimokawa	struct fw_device *fwdev, *next;
1670103285Sikob	int i, err;
1671103285Sikob	device_t *devlistp;
1672103285Sikob	int devcnt;
1673103285Sikob	struct firewire_dev_comm *fdc;
1674103285Sikob
1675120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1676120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1677120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1678114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1679120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1680120850Ssimokawa			fwdev->rcnt ++;
1681120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1682120850Ssimokawa				/*
1683120850Ssimokawa				 * Remove devices which have not been seen
1684120850Ssimokawa				 * for a while.
1685120850Ssimokawa				 */
1686120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1687120850Ssimokawa				    link);
1688120850Ssimokawa				free(fwdev, M_FW);
1689120850Ssimokawa			}
1690120850Ssimokawa		}
1691120850Ssimokawa	}
1692103285Sikob
1693108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1694103285Sikob	if( err != 0 )
1695103285Sikob		return;
1696103285Sikob	for( i = 0 ; i < devcnt ; i++){
1697103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1698103285Sikob			fdc = device_get_softc(devlistp[i]);
1699103285Sikob			if (fdc->post_explore != NULL)
1700103285Sikob				fdc->post_explore(fdc);
1701103285Sikob		}
1702103285Sikob	}
1703103285Sikob	free(devlistp, M_TEMP);
1704103285Sikob
1705103285Sikob	return;
1706103285Sikob}
1707106815Ssimokawa
1708103285Sikob/*
1709129541Sdfr * To allocate unique transaction label.
1710103285Sikob */
1711106815Ssimokawastatic int
1712106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1713103285Sikob{
1714171457Ssimokawa	u_int dst, new_tlabel;
1715169119Ssimokawa	struct fw_xfer *txfer;
1716103285Sikob	int s;
1717103285Sikob
1718171457Ssimokawa	dst = xfer->send.hdr.mode.hdr.dst & 0x3f;
1719103285Sikob	s = splfw();
1720170374Ssimokawa	FW_GLOCK(fc);
1721171457Ssimokawa	new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f;
1722171457Ssimokawa	STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel)
1723171457Ssimokawa		if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst)
1724120660Ssimokawa				break;
1725171457Ssimokawa	if(txfer == NULL) {
1726171457Ssimokawa		fc->last_tlabel[dst] = new_tlabel;
1727171457Ssimokawa		STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel);
1728171457Ssimokawa		FW_GUNLOCK(fc);
1729171457Ssimokawa		splx(s);
1730171457Ssimokawa		xfer->tl = new_tlabel;
1731171457Ssimokawa		xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2;
1732171457Ssimokawa		if (firewire_debug > 1)
1733171457Ssimokawa			printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel);
1734171457Ssimokawa		return (new_tlabel);
1735103285Sikob	}
1736170374Ssimokawa	FW_GUNLOCK(fc);
1737103285Sikob	splx(s);
1738103285Sikob
1739130460Sdfr	if (firewire_debug > 1)
1740130460Sdfr		printf("fw_get_tlabel: no free tlabel\n");
1741171457Ssimokawa	return (-1);
1742103285Sikob}
1743106815Ssimokawa
1744113584Ssimokawastatic void
1745120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1746113584Ssimokawa{
1747120660Ssimokawa	struct fw_pkt *pkt;
1748120660Ssimokawa	u_char *p;
1749120660Ssimokawa	struct tcode_info *tinfo;
1750120660Ssimokawa	u_int res, i, len, plen;
1751113584Ssimokawa
1752169127Ssimokawa	rb->xfer->recv.spd = rb->spd;
1753120660Ssimokawa
1754120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1755120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1756120660Ssimokawa
1757120660Ssimokawa	/* Copy header */
1758120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1759120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1760132771Skan	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1761120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1762120660Ssimokawa
1763120660Ssimokawa	/* Copy payload */
1764120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1765120660Ssimokawa	res = rb->xfer->recv.pay_len;
1766120660Ssimokawa
1767120660Ssimokawa	/* special handling for RRESQ */
1768120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1769129585Sdfr	    p != NULL && res >= sizeof(uint32_t)) {
1770129585Sdfr		*(uint32_t *)p = pkt->mode.rresq.data;
1771129585Sdfr		rb->xfer->recv.pay_len = sizeof(uint32_t);
1772120660Ssimokawa		return;
1773120660Ssimokawa	}
1774120660Ssimokawa
1775120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1776120660Ssimokawa		return;
1777120660Ssimokawa
1778120660Ssimokawa	plen = pkt->mode.rresb.len;
1779120660Ssimokawa
1780120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1781120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1782113584Ssimokawa		if (res < len) {
1783113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1784120660Ssimokawa			    rb->xfer->recv.pay_len, len - res);
1785113584Ssimokawa			len = res;
1786113584Ssimokawa		}
1787120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1788113584Ssimokawa		p += len;
1789113584Ssimokawa		res -= len;
1790120660Ssimokawa		plen -= len;
1791120660Ssimokawa		if (res == 0 || plen == 0)
1792113584Ssimokawa			break;
1793113584Ssimokawa	}
1794120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1795120660Ssimokawa
1796113584Ssimokawa}
1797113584Ssimokawa
1798103285Sikob/*
1799129541Sdfr * Generic packet receiving process.
1800103285Sikob */
1801106815Ssimokawavoid
1802120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1803103285Sikob{
1804103285Sikob	struct fw_pkt *fp, *resfp;
1805103285Sikob	struct fw_bind *bind;
1806169130Ssimokawa	int tcode;
1807113584Ssimokawa	int i, len, oldstate;
1808103285Sikob#if 0
1809103285Sikob	{
1810129585Sdfr		uint32_t *qld;
1811103285Sikob		int i;
1812129585Sdfr		qld = (uint32_t *)buf;
1813103285Sikob		printf("spd %d len:%d\n", spd, len);
1814103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1815103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1816103285Sikob			if((i % 16) == 15) printf("\n");
1817103285Sikob		}
1818103285Sikob		if((i % 16) != 15) printf("\n");
1819103285Sikob	}
1820103285Sikob#endif
1821120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1822113584Ssimokawa	tcode = fp->mode.common.tcode;
1823113584Ssimokawa	switch (tcode) {
1824103285Sikob	case FWTCODE_WRES:
1825103285Sikob	case FWTCODE_RRESQ:
1826103285Sikob	case FWTCODE_RRESB:
1827103285Sikob	case FWTCODE_LRES:
1828120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1829170374Ssimokawa				fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode);
1830120660Ssimokawa		if(rb->xfer == NULL) {
1831103285Sikob			printf("fw_rcv: unknown response "
1832124251Ssimokawa			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1833124251Ssimokawa			    tcode_str[tcode], tcode,
1834124251Ssimokawa			    fp->mode.hdr.src,
1835124251Ssimokawa			    fp->mode.hdr.tlrt >> 2,
1836124251Ssimokawa			    fp->mode.hdr.tlrt & 3,
1837124251Ssimokawa			    fp->mode.rresq.data);
1838170374Ssimokawa#if 0
1839103285Sikob			printf("try ad-hoc work around!!\n");
1840120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1841103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1842120660Ssimokawa			if (rb->xfer == NULL) {
1843103285Sikob				printf("no use...\n");
1844169131Ssimokawa				return;
1845103285Sikob			}
1846103285Sikob#else
1847169131Ssimokawa			return;
1848103285Sikob#endif
1849103285Sikob		}
1850120660Ssimokawa		fw_rcv_copy(rb);
1851120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1852120660Ssimokawa			rb->xfer->resp = EIO;
1853120660Ssimokawa		else
1854120660Ssimokawa			rb->xfer->resp = 0;
1855113584Ssimokawa		/* make sure the packet is drained in AT queue */
1856170374Ssimokawa		oldstate = rb->xfer->flag;
1857170374Ssimokawa		rb->xfer->flag = FWXF_RCVD;
1858113584Ssimokawa		switch (oldstate) {
1859113584Ssimokawa		case FWXF_SENT:
1860120660Ssimokawa			fw_xfer_done(rb->xfer);
1861103285Sikob			break;
1862113584Ssimokawa		case FWXF_START:
1863119289Ssimokawa#if 0
1864113584Ssimokawa			if (firewire_debug)
1865120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1866119289Ssimokawa#endif
1867113584Ssimokawa			break;
1868103285Sikob		default:
1869170374Ssimokawa			printf("unexpected flag 0x%02x\n", rb->xfer->flag);
1870103285Sikob		}
1871113584Ssimokawa		return;
1872103285Sikob	case FWTCODE_WREQQ:
1873103285Sikob	case FWTCODE_WREQB:
1874103285Sikob	case FWTCODE_RREQQ:
1875103285Sikob	case FWTCODE_RREQB:
1876103285Sikob	case FWTCODE_LREQ:
1877120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1878113584Ssimokawa			fp->mode.rreqq.dest_lo);
1879103285Sikob		if(bind == NULL){
1880124251Ssimokawa			printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1881127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1882127468Ssimokawa			    " src=0x%x data=%lx\n",
1883127468Ssimokawa#else
1884124251Ssimokawa			    " src=0x%x data=%x\n",
1885113962Ssimokawa#endif
1886124251Ssimokawa			    fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1887124251Ssimokawa			    tcode_str[tcode], tcode,
1888124251Ssimokawa			    fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1889170425Ssimokawa			if (rb->fc->status == FWBUSINIT) {
1890110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1891169131Ssimokawa				return;
1892103285Sikob			}
1893120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
1894120660Ssimokawa			if(rb->xfer == NULL){
1895103285Sikob				return;
1896103285Sikob			}
1897120660Ssimokawa			rb->xfer->send.spd = rb->spd;
1898120660Ssimokawa			rb->xfer->send.pay_len = 0;
1899120660Ssimokawa			resfp = &rb->xfer->send.hdr;
1900113584Ssimokawa			switch (tcode) {
1901103285Sikob			case FWTCODE_WREQQ:
1902103285Sikob			case FWTCODE_WREQB:
1903103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1904103285Sikob				break;
1905103285Sikob			case FWTCODE_RREQQ:
1906103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1907103285Sikob				break;
1908103285Sikob			case FWTCODE_RREQB:
1909103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1910103285Sikob				break;
1911103285Sikob			case FWTCODE_LREQ:
1912103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1913103285Sikob				break;
1914103285Sikob			}
1915103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1916103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1917103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1918120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1919103285Sikob			resfp->mode.rresb.extcode = 0;
1920103285Sikob			resfp->mode.rresb.len = 0;
1921103285Sikob/*
1922170374Ssimokawa			rb->xfer->hand = fw_xferwake;
1923103285Sikob*/
1924167632Ssimokawa			rb->xfer->hand = fw_xfer_free;
1925120660Ssimokawa			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1926120660Ssimokawa				fw_xfer_free(rb->xfer);
1927103285Sikob				return;
1928103285Sikob			}
1929169131Ssimokawa			return;
1930103285Sikob		}
1931113584Ssimokawa		len = 0;
1932120660Ssimokawa		for (i = 0; i < rb->nvec; i ++)
1933120660Ssimokawa			len += rb->vec[i].iov_len;
1934169130Ssimokawa		rb->xfer = STAILQ_FIRST(&bind->xferlist);
1935169130Ssimokawa		if (rb->xfer == NULL) {
1936169131Ssimokawa#if 1
1937169130Ssimokawa			printf("Discard a packet for this bind.\n");
1938169131Ssimokawa#endif
1939169131Ssimokawa			return;
1940103285Sikob		}
1941169130Ssimokawa		STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1942169130Ssimokawa		fw_rcv_copy(rb);
1943169130Ssimokawa		rb->xfer->hand(rb->xfer);
1944169130Ssimokawa		return;
1945120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
1946103285Sikob	case FWTCODE_STREAM:
1947103285Sikob	{
1948103285Sikob		struct fw_xferq *xferq;
1949103285Sikob
1950120660Ssimokawa		xferq = rb->fc->ir[sub];
1951103285Sikob#if 0
1952103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1953103285Sikob			sub, len, off, spd);
1954103285Sikob#endif
1955103285Sikob		if(xferq->queued >= xferq->maxq) {
1956103285Sikob			printf("receive queue is full\n");
1957169131Ssimokawa			return;
1958103285Sikob		}
1959113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1960113584Ssimokawa			per packet mode */
1961120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1962113584Ssimokawa						vec[0].iov_len);
1963169131Ssimokawa		if (rb->xfer == NULL)
1964169131Ssimokawa			return;
1965120660Ssimokawa		fw_rcv_copy(rb)
1966103285Sikob		s = splfw();
1967103285Sikob		xferq->queued++;
1968120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1969103285Sikob		splx(s);
1970120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
1971127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1972127468Ssimokawa		if (&xferq->rsel.si_pid != 0)
1973127468Ssimokawa#else
1974103285Sikob		if (SEL_WAITING(&xferq->rsel))
1975103285Sikob#endif
1976122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
1977103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1978103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1979103285Sikob			wakeup((caddr_t)xferq);
1980103285Sikob		}
1981103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1982103285Sikob			xferq->hand(xferq);
1983103285Sikob		}
1984103285Sikob		return;
1985103285Sikob		break;
1986103285Sikob	}
1987120660Ssimokawa#endif
1988103285Sikob	default:
1989113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1990103285Sikob		break;
1991103285Sikob	}
1992103285Sikob}
1993106815Ssimokawa
1994103285Sikob/*
1995103285Sikob * Post process for Bus Manager election process.
1996103285Sikob */
1997103285Sikobstatic void
1998103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1999103285Sikob{
2000103285Sikob	struct firewire_comm *fc;
2001109422Ssimokawa	int bmr;
2002103285Sikob
2003109422Ssimokawa	if (xfer == NULL)
2004109422Ssimokawa		return;
2005103285Sikob	fc = xfer->fc;
2006109422Ssimokawa	if (xfer->resp != 0)
2007103285Sikob		goto error;
2008120660Ssimokawa	if (xfer->recv.payload == NULL)
2009103285Sikob		goto error;
2010120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2011103285Sikob		goto error;
2012109422Ssimokawa
2013120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
2014109422Ssimokawa	if (bmr == 0x3f)
2015109422Ssimokawa		bmr = fc->nodeid;
2016109422Ssimokawa
2017109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2018120660Ssimokawa	fw_xfer_free_buf(xfer);
2019114909Ssimokawa	fw_bmr(fc);
2020114909Ssimokawa	return;
2021114909Ssimokawa
2022103285Sikoberror:
2023114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2024120660Ssimokawa	fw_xfer_free_buf(xfer);
2025103285Sikob}
2026106815Ssimokawa
2027113584Ssimokawa
2028103285Sikob/*
2029103285Sikob * To candidate Bus Manager election process.
2030103285Sikob */
2031110270Ssimokawastatic void
2032106815Ssimokawafw_try_bmr(void *arg)
2033103285Sikob{
2034103285Sikob	struct fw_xfer *xfer;
2035103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2036103285Sikob	struct fw_pkt *fp;
2037103285Sikob	int err = 0;
2038103285Sikob
2039120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2040103285Sikob	if(xfer == NULL){
2041103285Sikob		return;
2042103285Sikob	}
2043120660Ssimokawa	xfer->send.spd = 0;
2044103285Sikob	fc->status = FWBUSMGRELECT;
2045103285Sikob
2046120660Ssimokawa	fp = &xfer->send.hdr;
2047113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2048103285Sikob	fp->mode.lreq.tlrt = 0;
2049103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2050103285Sikob	fp->mode.lreq.pri = 0;
2051103285Sikob	fp->mode.lreq.src = 0;
2052113584Ssimokawa	fp->mode.lreq.len = 8;
2053120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2054120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2055113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2056120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2057120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2058167632Ssimokawa	xfer->hand = fw_try_bmr_callback;
2059103285Sikob
2060103285Sikob	err = fw_asyreq(fc, -1, xfer);
2061103285Sikob	if(err){
2062120660Ssimokawa		fw_xfer_free_buf(xfer);
2063103285Sikob		return;
2064103285Sikob	}
2065103285Sikob	return;
2066103285Sikob}
2067106543Ssimokawa
2068106543Ssimokawa#ifdef FW_VMACCESS
2069103285Sikob/*
2070103285Sikob * Software implementation for physical memory block access.
2071103285Sikob * XXX:Too slow, usef for debug purpose only.
2072103285Sikob */
2073106815Ssimokawastatic void
2074106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2075103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2076129585Sdfr	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2077103285Sikob
2078113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2079113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2080103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2081103285Sikob	if(xfer->resp != 0){
2082103285Sikob		fw_xfer_free( xfer);
2083103285Sikob		return;
2084103285Sikob	}
2085103285Sikob	if(xfer->recv.buf == NULL){
2086103285Sikob		fw_xfer_free( xfer);
2087103285Sikob		return;
2088103285Sikob	}
2089103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2090103285Sikob	switch(rfp->mode.hdr.tcode){
2091103285Sikob		/* XXX need fix for 64bit arch */
2092103285Sikob		case FWTCODE_WREQB:
2093110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2094103285Sikob			xfer->send.len = 12;
2095103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2096103285Sikob			bcopy(rfp->mode.wreqb.payload,
2097103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2098103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2099103285Sikob			sfp->mode.wres.rtcode = 0;
2100103285Sikob			break;
2101103285Sikob		case FWTCODE_WREQQ:
2102110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2103103285Sikob			xfer->send.len = 12;
2104103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2105129585Sdfr			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2106103285Sikob			sfp->mode.wres.rtcode = 0;
2107103285Sikob			break;
2108103285Sikob		case FWTCODE_RREQB:
2109110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2110103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2111103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2112103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2113129585Sdfr				sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2114103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2115103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2116103285Sikob			sfp->mode.rresb.rtcode = 0;
2117103285Sikob			sfp->mode.rresb.extcode = 0;
2118103285Sikob			break;
2119103285Sikob		case FWTCODE_RREQQ:
2120110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2121103285Sikob			xfer->send.len = 16;
2122103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2123129585Sdfr			sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2124103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2125103285Sikob			sfp->mode.rresb.rtcode = 0;
2126103285Sikob			break;
2127103285Sikob		default:
2128103285Sikob			fw_xfer_free( xfer);
2129103285Sikob			return;
2130103285Sikob	}
2131103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2132103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2133167632Ssimokawa	xfer->hand = fw_xfer_free;
2134103285Sikob
2135103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2136103285Sikob	sfp->mode.hdr.pri = 0;
2137103285Sikob
2138103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2139103285Sikob/**/
2140103285Sikob	return;
2141103285Sikob}
2142106543Ssimokawa#endif
2143106543Ssimokawa
2144103285Sikob/*
2145103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2146103285Sikob */
2147129585Sdfruint16_t
2148129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){
2149129585Sdfr	uint32_t i, sum, crc = 0;
2150103285Sikob	int shift;
2151103285Sikob	len = (len + 3) & ~3;
2152103285Sikob	for(i = 0 ; i < len ; i+= 4){
2153103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2154103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2155103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2156103285Sikob		}
2157103285Sikob		crc &= 0xffff;
2158103285Sikob	}
2159129585Sdfr	return((uint16_t) crc);
2160103285Sikob}
2161106815Ssimokawa
2162110270Ssimokawastatic int
2163110072Ssimokawafw_bmr(struct firewire_comm *fc)
2164110072Ssimokawa{
2165110072Ssimokawa	struct fw_device fwdev;
2166113584Ssimokawa	union fw_self_id *self_id;
2167110072Ssimokawa	int cmstr;
2168129585Sdfr	uint32_t quad;
2169110072Ssimokawa
2170113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2171129274Sdfr	self_id = fw_find_self_id(fc, fc->max_node);
2172113584Ssimokawa	if (fc->max_node > 0) {
2173114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2174114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2175113584Ssimokawa			cmstr = fc->max_node;
2176114909Ssimokawa		else {
2177114909Ssimokawa			device_printf(fc->bdev,
2178114909Ssimokawa				"root node is not cycle master capable\n");
2179114909Ssimokawa			/* XXX shall we be the cycle master? */
2180113584Ssimokawa			cmstr = fc->nodeid;
2181114909Ssimokawa			/* XXX need bus reset */
2182114909Ssimokawa		}
2183113584Ssimokawa	} else
2184113584Ssimokawa		cmstr = -1;
2185114909Ssimokawa
2186114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2187114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2188114909Ssimokawa		/* We are not the bus manager */
2189114909Ssimokawa		printf("\n");
2190114909Ssimokawa		return(0);
2191114909Ssimokawa	}
2192114909Ssimokawa	printf("(me)\n");
2193114909Ssimokawa
2194114909Ssimokawa	/* Optimize gapcount */
2195113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2196113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2197110072Ssimokawa	/* If we are the cycle master, nothing to do */
2198113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2199110072Ssimokawa		return 0;
2200110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2201110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2202110072Ssimokawa	fwdev.fc = fc;
2203110072Ssimokawa	fwdev.dst = cmstr;
2204110072Ssimokawa	fwdev.speed = 0;
2205110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2206110072Ssimokawa	fwdev.status = FWDEVINIT;
2207110072Ssimokawa	/* Set cmstr bit on the cycle master */
2208120660Ssimokawa	quad = htonl(1 << 8);
2209110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2210120660Ssimokawa		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2211110072Ssimokawa
2212110072Ssimokawa	return 0;
2213110072Ssimokawa}
2214110072Ssimokawa
2215170374Ssimokawaint
2216170374Ssimokawafw_open_isodma(struct firewire_comm *fc, int tx)
2217170374Ssimokawa{
2218170374Ssimokawa	struct fw_xferq **xferqa;
2219170374Ssimokawa	struct fw_xferq *xferq;
2220170374Ssimokawa	int i;
2221170374Ssimokawa
2222170374Ssimokawa	if (tx)
2223170374Ssimokawa		xferqa = &fc->it[0];
2224170374Ssimokawa	else
2225170374Ssimokawa		xferqa = &fc->ir[0];
2226170374Ssimokawa
2227170374Ssimokawa	FW_GLOCK(fc);
2228170374Ssimokawa	for (i = 0; i < fc->nisodma; i ++) {
2229170374Ssimokawa		xferq = xferqa[i];
2230170374Ssimokawa		if ((xferq->flag & FWXFERQ_OPEN) == 0) {
2231170374Ssimokawa			xferq->flag |= FWXFERQ_OPEN;
2232170374Ssimokawa			break;
2233170374Ssimokawa		}
2234170374Ssimokawa	}
2235170374Ssimokawa	if (i == fc->nisodma) {
2236170374Ssimokawa		printf("no free dma channel (tx=%d)\n", tx);
2237170374Ssimokawa		i = -1;
2238170374Ssimokawa	}
2239170374Ssimokawa	FW_GUNLOCK(fc);
2240170374Ssimokawa	return (i);
2241170374Ssimokawa}
2242170374Ssimokawa
2243118455Ssimokawastatic int
2244118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2245118455Ssimokawa{
2246118455Ssimokawa	int err = 0;
2247127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2248118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2249118455Ssimokawa#endif
2250118455Ssimokawa
2251118455Ssimokawa	switch (type) {
2252118455Ssimokawa	case MOD_LOAD:
2253127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2254118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2255118455Ssimokawa						fwdev_clone, 0, 1000);
2256118455Ssimokawa#endif
2257118455Ssimokawa		break;
2258118455Ssimokawa	case MOD_UNLOAD:
2259127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2260118455Ssimokawa		if (fwdev_ehtag != NULL)
2261118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2262118455Ssimokawa#endif
2263118455Ssimokawa		break;
2264118455Ssimokawa	case MOD_SHUTDOWN:
2265118455Ssimokawa		break;
2266132199Sphk	default:
2267132199Sphk		return (EOPNOTSUPP);
2268118455Ssimokawa	}
2269118455Ssimokawa	return (err);
2270118455Ssimokawa}
2271118455Ssimokawa
2272118455Ssimokawa
2273127468Ssimokawa#ifdef __DragonFly__
2274127468SsimokawaDECLARE_DUMMY_MODULE(firewire);
2275127468Ssimokawa#endif
2276118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2277103285SikobMODULE_VERSION(firewire, 1);
2278