firewire.c revision 277509
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
35227843Smarius#include <sys/cdefs.h>
36227843Smarius__FBSDID("$FreeBSD: head/sys/dev/firewire/firewire.c 277509 2015-01-21 20:05:10Z will $");
37227843Smarius
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/types.h>
41103285Sikob
42193066Sjamie#include <sys/jail.h>
43103285Sikob#include <sys/kernel.h>
44129879Sphk#include <sys/module.h>
45103285Sikob#include <sys/malloc.h>
46103285Sikob#include <sys/conf.h>
47103285Sikob#include <sys/sysctl.h>
48169806Ssimokawa#include <sys/kthread.h>
49103285Sikob
50170374Ssimokawa#include <sys/kdb.h>
51103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
52113584Ssimokawa#include <machine/bus.h>
53103285Sikob
54103285Sikob#include <dev/firewire/firewire.h>
55103285Sikob#include <dev/firewire/firewirereg.h>
56110072Ssimokawa#include <dev/firewire/fwmem.h>
57103285Sikob#include <dev/firewire/iec13213.h>
58103285Sikob#include <dev/firewire/iec68113.h>
59103285Sikob
60116376Ssimokawastruct crom_src_buf {
61116376Ssimokawa	struct crom_src	src;
62116376Ssimokawa	struct crom_chunk root;
63116376Ssimokawa	struct crom_chunk vendor;
64116376Ssimokawa	struct crom_chunk hw;
65116376Ssimokawa};
66116376Ssimokawa
67272214Skanint firewire_debug = 0, try_bmr = 1, hold_count = 0;
68103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
69108281Ssimokawa	"FireWire driver debug flag");
70109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
71109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
72109736Ssimokawa	"Try to be a bus manager");
73120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
74120850Ssimokawa	"Number of count of bus resets for removing lost device information");
75103285Sikob
76110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
77110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
78110195Ssimokawa
79103285Sikob#define FW_MAXASYRTY 4
80103285Sikob
81103285Sikobdevclass_t firewire_devclass;
82103285Sikob
83272214Skanstatic void firewire_identify(driver_t *, device_t);
84272214Skanstatic int firewire_probe(device_t);
85272214Skanstatic int firewire_attach(device_t);
86272214Skanstatic int firewire_detach(device_t);
87272214Skanstatic int firewire_resume(device_t);
88170374Ssimokawastatic void firewire_xfer_timeout(void *, int);
89212413Savgstatic device_t firewire_add_child(device_t, u_int, const char *, int);
90272214Skanstatic void fw_try_bmr(void *);
91272214Skanstatic void fw_try_bmr_callback(struct fw_xfer *);
92272214Skanstatic void fw_asystart(struct fw_xfer *);
93272214Skanstatic int fw_get_tlabel(struct firewire_comm *, struct fw_xfer *);
94272214Skanstatic void fw_bus_probe(void *);
95272214Skanstatic void fw_attach_dev(struct firewire_comm *);
96169806Ssimokawastatic void fw_bus_probe_thread(void *);
97106543Ssimokawa#ifdef FW_VMACCESS
98124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *);
99106543Ssimokawa#endif
100124169Ssimokawastatic int fw_bmr (struct firewire_comm *);
101170374Ssimokawastatic void fw_dump_hdr(struct fw_pkt *, char *);
102103285Sikob
103103285Sikobstatic device_method_t firewire_methods[] = {
104103285Sikob	/* Device interface */
105125238Ssimokawa	DEVMETHOD(device_identify,	firewire_identify),
106125238Ssimokawa	DEVMETHOD(device_probe,		firewire_probe),
107103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
108103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
109108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
110116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
111103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
112103285Sikob
113103285Sikob	/* Bus interface */
114103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
115103285Sikob
116227843Smarius	DEVMETHOD_END
117103285Sikob};
118272214Skan
119124251Ssimokawachar *linkspeed[] = {
120124251Ssimokawa	"S100", "S200", "S400", "S800",
121124251Ssimokawa	"S1600", "S3200", "undef", "undef"
122124251Ssimokawa};
123103285Sikob
124124251Ssimokawastatic char *tcode_str[] = {
125124251Ssimokawa	"WREQQ", "WREQB", "WRES",   "undef",
126124251Ssimokawa	"RREQQ", "RREQB", "RRESQ",  "RRESB",
127124251Ssimokawa	"CYCS",  "LREQ",  "STREAM", "LRES",
128124251Ssimokawa	"undef", "undef", "PHY",    "undef"
129124251Ssimokawa};
130124251Ssimokawa
131114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
132114909Ssimokawa#define MAX_GAPHOP 15
133114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
134114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
135106813Ssimokawa
136103285Sikobstatic driver_t firewire_driver = {
137103285Sikob	"firewire",
138103285Sikob	firewire_methods,
139103285Sikob	sizeof(struct firewire_softc),
140103285Sikob};
141103285Sikob
142103285Sikob/*
143110072Ssimokawa * Lookup fwdev by node id.
144103285Sikob */
145106810Ssimokawastruct fw_device *
146110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
147103285Sikob{
148103285Sikob	struct fw_device *fwdev;
149110072Ssimokawa
150277509Swill	FW_GLOCK(fc);
151110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
152120660Ssimokawa		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
153103285Sikob			break;
154277509Swill	FW_GUNLOCK(fc);
155110072Ssimokawa
156106810Ssimokawa	return fwdev;
157103285Sikob}
158106813Ssimokawa
159103285Sikob/*
160110072Ssimokawa * Lookup fwdev by EUI64.
161110072Ssimokawa */
162110072Ssimokawastruct fw_device *
163110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
164110072Ssimokawa{
165110072Ssimokawa	struct fw_device *fwdev;
166110072Ssimokawa
167170374Ssimokawa	FW_GLOCK(fc);
168110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
169110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
170110072Ssimokawa			break;
171170374Ssimokawa	FW_GUNLOCK(fc);
172110072Ssimokawa
173272214Skan	if (fwdev == NULL)
174272214Skan		return NULL;
175272214Skan	if (fwdev->status == FWDEVINVAL)
176272214Skan		return NULL;
177110072Ssimokawa	return fwdev;
178110072Ssimokawa}
179110072Ssimokawa
180110072Ssimokawa/*
181103285Sikob * Async. request procedure for userland application.
182103285Sikob */
183103285Sikobint
184103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
185103285Sikob{
186103285Sikob	int err = 0;
187103285Sikob	struct fw_xferq *xferq;
188170374Ssimokawa	int len;
189103285Sikob	struct fw_pkt *fp;
190103285Sikob	int tcode;
191103285Sikob	struct tcode_info *info;
192103285Sikob
193272214Skan	if (xfer == NULL)
194272214Skan		return EINVAL;
195272214Skan	if (xfer->hand == NULL) {
196167632Ssimokawa		printf("hand == NULL\n");
197103285Sikob		return EINVAL;
198103285Sikob	}
199120660Ssimokawa	fp = &xfer->send.hdr;
200103285Sikob
201103285Sikob	tcode = fp->mode.common.tcode & 0xf;
202103285Sikob	info = &fc->tcode[tcode];
203103285Sikob	if (info->flag == 0) {
204124251Ssimokawa		printf("invalid tcode=%x\n", tcode);
205103285Sikob		return EINVAL;
206103285Sikob	}
207170425Ssimokawa
208170425Ssimokawa	/* XXX allow bus explore packets only after bus rest */
209170425Ssimokawa	if ((fc->status < FWBUSEXPLORE) &&
210170425Ssimokawa	    ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) ||
211170425Ssimokawa	    (fp->mode.rreqq.dest_lo  < 0xf0000000) ||
212170425Ssimokawa	    (fp->mode.rreqq.dest_lo >= 0xf0001000))) {
213170425Ssimokawa		xfer->resp = EAGAIN;
214170425Ssimokawa		xfer->flag = FWXF_BUSY;
215170425Ssimokawa		return (EAGAIN);
216170425Ssimokawa	}
217170425Ssimokawa
218103285Sikob	if (info->flag & FWTI_REQ)
219103285Sikob		xferq = fc->atq;
220103285Sikob	else
221103285Sikob		xferq = fc->ats;
222103285Sikob	len = info->hdr_len;
223120660Ssimokawa	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
224120660Ssimokawa		printf("send.pay_len > maxrec\n");
225120660Ssimokawa		return EINVAL;
226120660Ssimokawa	}
227103285Sikob	if (info->flag & FWTI_BLOCK_STR)
228120660Ssimokawa		len = fp->mode.stream.len;
229103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
230120660Ssimokawa		len = fp->mode.rresb.len;
231120660Ssimokawa	else
232120660Ssimokawa		len = 0;
233272214Skan	if (len != xfer->send.pay_len) {
234124251Ssimokawa		printf("len(%d) != send.pay_len(%d) %s(%x)\n",
235124251Ssimokawa		    len, xfer->send.pay_len, tcode_str[tcode], tcode);
236272214Skan		return EINVAL;
237103285Sikob	}
238106790Ssimokawa
239272214Skan	if (xferq->start == NULL) {
240103285Sikob		printf("xferq->start == NULL\n");
241103285Sikob		return EINVAL;
242103285Sikob	}
243272214Skan	if (!(xferq->queued < xferq->maxq)) {
244108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
245108655Ssimokawa			xferq->queued);
246170374Ssimokawa		return EAGAIN;
247103285Sikob	}
248103285Sikob
249170374Ssimokawa	xfer->tl = -1;
250103285Sikob	if (info->flag & FWTI_TLABEL) {
251170374Ssimokawa		if (fw_get_tlabel(fc, xfer) < 0)
252130460Sdfr			return EAGAIN;
253103285Sikob	}
254103285Sikob
255103285Sikob	xfer->resp = 0;
256103285Sikob	xfer->fc = fc;
257103285Sikob	xfer->q = xferq;
258103285Sikob
259103285Sikob	fw_asystart(xfer);
260103285Sikob	return err;
261103285Sikob}
262272214Skan
263103285Sikob/*
264103285Sikob * Wakeup blocked process.
265103285Sikob */
266103285Sikobvoid
267170374Ssimokawafw_xferwake(struct fw_xfer *xfer)
268170374Ssimokawa{
269170374Ssimokawa	struct mtx *lock = &xfer->fc->wait_lock;
270170374Ssimokawa
271170374Ssimokawa	mtx_lock(lock);
272170374Ssimokawa	xfer->flag |= FWXF_WAKE;
273170374Ssimokawa	mtx_unlock(lock);
274170374Ssimokawa
275103285Sikob	wakeup(xfer);
276103285Sikob	return;
277103285Sikob}
278103285Sikob
279170374Ssimokawaint
280170374Ssimokawafw_xferwait(struct fw_xfer *xfer)
281170374Ssimokawa{
282170374Ssimokawa	struct mtx *lock = &xfer->fc->wait_lock;
283170374Ssimokawa	int err = 0;
284170374Ssimokawa
285170374Ssimokawa	mtx_lock(lock);
286272214Skan	while ((xfer->flag & FWXF_WAKE) == 0)
287272214Skan		err = msleep(xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0);
288170374Ssimokawa	mtx_unlock(lock);
289170374Ssimokawa
290170374Ssimokawa	return (err);
291170374Ssimokawa}
292170374Ssimokawa
293103285Sikob/*
294103285Sikob * Async. request with given xfer structure.
295103285Sikob */
296106790Ssimokawastatic void
297106790Ssimokawafw_asystart(struct fw_xfer *xfer)
298106790Ssimokawa{
299103285Sikob	struct firewire_comm *fc = xfer->fc;
300272214Skan
301170374Ssimokawa	/* Protect from interrupt/timeout */
302170374Ssimokawa	FW_GLOCK(fc);
303170374Ssimokawa	xfer->flag = FWXF_INQ;
304103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
305170374Ssimokawa#if 0
306272214Skan	xfer->q->queued++;
307170374Ssimokawa#endif
308170374Ssimokawa	FW_GUNLOCK(fc);
309103285Sikob	/* XXX just queue for mbuf */
310103285Sikob	if (xfer->mbuf == NULL)
311103285Sikob		xfer->q->start(fc);
312103285Sikob	return;
313103285Sikob}
314106790Ssimokawa
315125238Ssimokawastatic void
316125238Ssimokawafirewire_identify(driver_t *driver, device_t parent)
317125238Ssimokawa{
318125238Ssimokawa	BUS_ADD_CHILD(parent, 0, "firewire", -1);
319125238Ssimokawa}
320125238Ssimokawa
321103285Sikobstatic int
322125238Ssimokawafirewire_probe(device_t dev)
323103285Sikob{
324108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
325125238Ssimokawa	return (0);
326103285Sikob}
327106790Ssimokawa
328277509Swill/* Just use a per-packet callout? */
329110577Ssimokawastatic void
330170374Ssimokawafirewire_xfer_timeout(void *arg, int pending)
331110577Ssimokawa{
332170374Ssimokawa	struct firewire_comm *fc = (struct firewire_comm *)arg;
333170374Ssimokawa	struct fw_xfer *xfer, *txfer;
334110577Ssimokawa	struct timeval tv;
335110577Ssimokawa	struct timeval split_timeout;
336170374Ssimokawa	STAILQ_HEAD(, fw_xfer) xfer_timeout;
337277509Swill	int i;
338110577Ssimokawa
339120660Ssimokawa	split_timeout.tv_sec = 0;
340120660Ssimokawa	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
341110577Ssimokawa
342110577Ssimokawa	microtime(&tv);
343110577Ssimokawa	timevalsub(&tv, &split_timeout);
344170374Ssimokawa	STAILQ_INIT(&xfer_timeout);
345110577Ssimokawa
346171513Ssimokawa	mtx_lock(&fc->tlabel_lock);
347277509Swill	for (i = 0; i < nitems(fc->tlabels); i++) {
348169119Ssimokawa		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
349170427Ssimokawa			if ((xfer->flag & FWXF_SENT) == 0)
350170427Ssimokawa				/* not sent yet */
351170427Ssimokawa				break;
352110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
353110577Ssimokawa				/* the rests are newer than this */
354110577Ssimokawa				break;
355110577Ssimokawa			device_printf(fc->bdev,
356272214Skan			    "split transaction timeout: tl=0x%x flag=0x%02x\n",
357272214Skan			    i, xfer->flag);
358170374Ssimokawa			fw_dump_hdr(&xfer->send.hdr, "send");
359110577Ssimokawa			xfer->resp = ETIMEDOUT;
360249291Swill			xfer->tl = -1;
361170374Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
362170374Ssimokawa			STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel);
363110577Ssimokawa		}
364110577Ssimokawa	}
365171513Ssimokawa	mtx_unlock(&fc->tlabel_lock);
366170374Ssimokawa	fc->timeout(fc);
367170374Ssimokawa
368170374Ssimokawa	STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer)
369272214Skan		xfer->hand(xfer);
370110577Ssimokawa}
371110577Ssimokawa
372170374Ssimokawa#define WATCHDOG_HZ 10
373110577Ssimokawastatic void
374110577Ssimokawafirewire_watchdog(void *arg)
375110577Ssimokawa{
376110577Ssimokawa	struct firewire_comm *fc;
377170374Ssimokawa	static int watchdog_clock = 0;
378110577Ssimokawa
379272214Skan	fc = arg;
380121463Ssimokawa
381121463Ssimokawa	/*
382121463Ssimokawa	 * At boot stage, the device interrupt is disabled and
383121463Ssimokawa	 * We encounter a timeout easily. To avoid this,
384121463Ssimokawa	 * ignore clock interrupt for a while.
385121463Ssimokawa	 */
386170374Ssimokawa	if (watchdog_clock > WATCHDOG_HZ * 15)
387170374Ssimokawa		taskqueue_enqueue(fc->taskqueue, &fc->task_timeout);
388170374Ssimokawa	else
389272214Skan		watchdog_clock++;
390121463Ssimokawa
391170374Ssimokawa	callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ,
392272214Skan	    firewire_watchdog, fc);
393110577Ssimokawa}
394110577Ssimokawa
395103285Sikob/*
396103285Sikob * The attach routine.
397103285Sikob */
398103285Sikobstatic int
399118455Ssimokawafirewire_attach(device_t dev)
400103285Sikob{
401118455Ssimokawa	int unit;
402103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
403103285Sikob	device_t pa = device_get_parent(dev);
404103285Sikob	struct firewire_comm *fc;
405103285Sikob
406272214Skan	fc = device_get_softc(pa);
407103285Sikob	sc->fc = fc;
408116978Ssimokawa	fc->status = FWBUSNOTREADY;
409103285Sikob
410118455Ssimokawa	unit = device_get_unit(dev);
411272214Skan	if (fc->nisodma > FWMAXNDMA)
412272214Skan		fc->nisodma = FWMAXNDMA;
413103285Sikob
414118455Ssimokawa	fwdev_makedev(sc);
415118455Ssimokawa
416272214Skan	fc->crom_src_buf = malloc(sizeof(struct crom_src_buf),
417272214Skan	    M_FW, M_NOWAIT | M_ZERO);
418187993Ssbruno	if (fc->crom_src_buf == NULL) {
419272214Skan		device_printf(fc->dev,
420272214Skan		    "%s: unable to allocate crom src buffer\n", __func__);
421187993Ssbruno		return ENOMEM;
422187993Ssbruno	}
423272214Skan	fc->topology_map = malloc(sizeof(struct fw_topology_map),
424272214Skan	    M_FW, M_NOWAIT | M_ZERO);
425187993Ssbruno	if (fc->topology_map == NULL) {
426272214Skan		device_printf(fc->dev, "%s: unable to allocate topology map\n",
427272214Skan		    __func__);
428187993Ssbruno		free(fc->crom_src_buf, M_FW);
429187993Ssbruno		return ENOMEM;
430187993Ssbruno	}
431272214Skan	fc->speed_map = malloc(sizeof(struct fw_speed_map),
432272214Skan	    M_FW, M_NOWAIT | M_ZERO);
433187993Ssbruno	if (fc->speed_map == NULL) {
434272214Skan		device_printf(fc->dev, "%s: unable to allocate speed map\n",
435272214Skan		    __func__);
436187993Ssbruno		free(fc->crom_src_buf, M_FW);
437187993Ssbruno		free(fc->topology_map, M_FW);
438187993Ssbruno		return ENOMEM;
439187993Ssbruno	}
440187993Ssbruno
441170374Ssimokawa	mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF);
442171513Ssimokawa	mtx_init(&fc->tlabel_lock, "fwtlabel", NULL, MTX_DEF);
443170374Ssimokawa	CALLOUT_INIT(&fc->timeout_callout);
444170374Ssimokawa	CALLOUT_INIT(&fc->bmr_callout);
445170374Ssimokawa	CALLOUT_INIT(&fc->busprobe_callout);
446272214Skan	TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, fc);
447108853Ssimokawa
448110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
449272214Skan	    firewire_watchdog, sc->fc);
450110193Ssimokawa
451169806Ssimokawa	/* create thread */
452272214Skan	kproc_create(fw_bus_probe_thread, fc, &fc->probe_thread,
453272214Skan	    0, 0, "fw%d_probe", unit);
454169806Ssimokawa
455103285Sikob	/* Locate our children */
456103285Sikob	bus_generic_probe(dev);
457103285Sikob
458103285Sikob	/* launch attachement of the added children */
459103285Sikob	bus_generic_attach(dev);
460103285Sikob
461103285Sikob	/* bus_reset */
462187993Ssbruno	FW_GLOCK(fc);
463169117Ssimokawa	fw_busreset(fc, FWBUSNOTREADY);
464187993Ssbruno	FW_GUNLOCK(fc);
465103285Sikob	fc->ibr(fc);
466103285Sikob
467103285Sikob	return 0;
468103285Sikob}
469103285Sikob
470103285Sikob/*
471103285Sikob * Attach it as child.
472103285Sikob */
473103285Sikobstatic device_t
474212413Savgfirewire_add_child(device_t dev, u_int order, const char *name, int unit)
475103285Sikob{
476272214Skan	device_t child;
477103285Sikob	struct firewire_softc *sc;
478103285Sikob
479272214Skan	sc = device_get_softc(dev);
480103285Sikob	child = device_add_child(dev, name, unit);
481103285Sikob	if (child) {
482103285Sikob		device_set_ivars(child, sc->fc);
483103285Sikob		device_probe_and_attach(child);
484103285Sikob	}
485103285Sikob
486103285Sikob	return child;
487103285Sikob}
488106790Ssimokawa
489116978Ssimokawastatic int
490116978Ssimokawafirewire_resume(device_t dev)
491116978Ssimokawa{
492116978Ssimokawa	struct firewire_softc *sc;
493116978Ssimokawa
494272214Skan	sc = device_get_softc(dev);
495116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
496272214Skan
497116978Ssimokawa	bus_generic_resume(dev);
498116978Ssimokawa
499272214Skan	return (0);
500116978Ssimokawa}
501116978Ssimokawa
502103285Sikob/*
503272214Skan * Detach it.
504103285Sikob */
505103285Sikobstatic int
506118455Ssimokawafirewire_detach(device_t dev)
507103285Sikob{
508103285Sikob	struct firewire_softc *sc;
509169806Ssimokawa	struct firewire_comm *fc;
510111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
511118455Ssimokawa	int err;
512103285Sikob
513272214Skan	sc = device_get_softc(dev);
514169806Ssimokawa	fc = sc->fc;
515170374Ssimokawa	mtx_lock(&fc->wait_lock);
516169806Ssimokawa	fc->status = FWBUSDETACH;
517170374Ssimokawa	wakeup(fc);
518170374Ssimokawa	if (msleep(fc->probe_thread, &fc->wait_lock, PWAIT, "fwthr", hz * 60))
519170374Ssimokawa		printf("firewire probe thread didn't die\n");
520170374Ssimokawa	mtx_unlock(&fc->wait_lock);
521169806Ssimokawa
522272214Skan	if (fc->arq != 0 && fc->arq->maxq > 0)
523178915Ssimokawa		fw_drain_txq(fc);
524178915Ssimokawa
525118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
526118455Ssimokawa		return err;
527106790Ssimokawa
528118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
529118455Ssimokawa		return err;
530111078Ssimokawa
531169806Ssimokawa	callout_stop(&fc->timeout_callout);
532169806Ssimokawa	callout_stop(&fc->bmr_callout);
533169806Ssimokawa	callout_stop(&fc->busprobe_callout);
534111078Ssimokawa
535178915Ssimokawa	/* XXX xfer_free and untimeout on all xfers */
536169806Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
537272214Skan	     fwdev = fwdev_next) {
538111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
539111078Ssimokawa		free(fwdev, M_FW);
540111078Ssimokawa	}
541169806Ssimokawa	free(fc->topology_map, M_FW);
542169806Ssimokawa	free(fc->speed_map, M_FW);
543169806Ssimokawa	free(fc->crom_src_buf, M_FW);
544169806Ssimokawa
545171513Ssimokawa	mtx_destroy(&fc->tlabel_lock);
546170374Ssimokawa	mtx_destroy(&fc->wait_lock);
547272214Skan	return (0);
548103285Sikob}
549106790Ssimokawa
550110577Ssimokawastatic void
551110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
552110577Ssimokawa{
553110577Ssimokawa	struct fw_xfer *xfer;
554110577Ssimokawa
555110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
556110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
557170374Ssimokawa#if 0
558272214Skan		xferq->queued--;
559170374Ssimokawa#endif
560110577Ssimokawa		xfer->resp = EAGAIN;
561170374Ssimokawa		xfer->flag = FWXF_SENTERR;
562113584Ssimokawa		fw_xfer_done(xfer);
563110577Ssimokawa	}
564110577Ssimokawa}
565110577Ssimokawa
566110798Ssimokawavoid
567110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
568110798Ssimokawa{
569170374Ssimokawa	struct fw_xfer *xfer, *txfer;
570170374Ssimokawa	STAILQ_HEAD(, fw_xfer) xfer_drain;
571110798Ssimokawa	int i;
572110798Ssimokawa
573170374Ssimokawa	STAILQ_INIT(&xfer_drain);
574170374Ssimokawa
575170374Ssimokawa	FW_GLOCK(fc);
576110798Ssimokawa	fw_xferq_drain(fc->atq);
577110798Ssimokawa	fw_xferq_drain(fc->ats);
578272214Skan	for (i = 0; i < fc->nisodma; i++)
579110798Ssimokawa		fw_xferq_drain(fc->it[i]);
580171513Ssimokawa	FW_GUNLOCK(fc);
581170374Ssimokawa
582171513Ssimokawa	mtx_lock(&fc->tlabel_lock);
583272214Skan	for (i = 0; i < 0x40; i++)
584170374Ssimokawa		while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
585170374Ssimokawa			if (firewire_debug)
586170374Ssimokawa				printf("tl=%d flag=%d\n", i, xfer->flag);
587249291Swill			xfer->tl = -1;
588170374Ssimokawa			xfer->resp = EAGAIN;
589170374Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
590170374Ssimokawa			STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel);
591170374Ssimokawa		}
592171513Ssimokawa	mtx_unlock(&fc->tlabel_lock);
593170374Ssimokawa
594170374Ssimokawa	STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer)
595272214Skan		xfer->hand(xfer);
596110798Ssimokawa}
597110798Ssimokawa
598116376Ssimokawastatic void
599116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
600103285Sikob{
601103285Sikob	int i;
602103285Sikob
603103285Sikob	CSRARC(fc, STATE_CLEAR)
604272214Skan			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14;
605103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
606103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
607103285Sikob
608103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
609103285Sikob	fc->irm = -1;
610103285Sikob
611103285Sikob	fc->max_node = -1;
612103285Sikob
613272214Skan	for (i = 2; i < 0x100 / 4 - 2; i++) {
614103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
615103285Sikob	}
616272214Skan	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14;
617103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
618103285Sikob	CSRARC(fc, RESET_START) = 0;
619103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
620103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
621103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
622103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
623103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
624103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
625103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
626103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
627258780Seadler	CSRARC(fc, IP_CHANNELS) = (1U << 31);
628103285Sikob
629103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
630103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
631103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
632272214Skan	    1 << 28 | 0xff << 16 | 0x09 << 8;
633103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
634103285Sikob
635272214Skan	/* DV depend CSRs see blue book */
636272214Skan	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
637272214Skan	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
638103285Sikob
639272214Skan	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14);
640103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
641116376Ssimokawa}
642113584Ssimokawa
643116376Ssimokawastatic void
644116376Ssimokawafw_init_crom(struct firewire_comm *fc)
645116376Ssimokawa{
646116376Ssimokawa	struct crom_src *src;
647116376Ssimokawa
648116376Ssimokawa	src = &fc->crom_src_buf->src;
649116376Ssimokawa	bzero(src, sizeof(struct crom_src));
650116376Ssimokawa
651116376Ssimokawa	/* BUS info sample */
652116376Ssimokawa	src->hdr.info_len = 4;
653116376Ssimokawa
654116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
655116376Ssimokawa
656116376Ssimokawa	src->businfo.irmc = 1;
657116376Ssimokawa	src->businfo.cmc = 1;
658116376Ssimokawa	src->businfo.isc = 1;
659116376Ssimokawa	src->businfo.bmc = 1;
660116376Ssimokawa	src->businfo.pmc = 0;
661116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
662116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
663116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
664189928Ssbruno#define FW_GENERATION_CHANGEABLE 2
665189928Ssbruno	src->businfo.generation = FW_GENERATION_CHANGEABLE;
666116376Ssimokawa	src->businfo.link_spd = fc->speed;
667116376Ssimokawa
668116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
669116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
670116376Ssimokawa
671116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
672116376Ssimokawa
673116376Ssimokawa	fc->crom_src = src;
674116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
675116376Ssimokawa}
676116376Ssimokawa
677116376Ssimokawastatic void
678116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
679116376Ssimokawa{
680116376Ssimokawa	struct crom_src_buf *buf;
681116376Ssimokawa	struct crom_src *src;
682116376Ssimokawa	struct crom_chunk *root;
683116376Ssimokawa
684116376Ssimokawa	buf =  fc->crom_src_buf;
685116376Ssimokawa	src = fc->crom_src;
686116376Ssimokawa	root = fc->crom_root;
687116376Ssimokawa
688116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
689116376Ssimokawa
690116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
691116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
692116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
693116376Ssimokawa	/* private company_id */
694116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
695116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
696116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
697193066Sjamie	mtx_lock(&prison0.pr_mtx);
698194118Sjamie	crom_add_simple_text(src, root, &buf->hw, prison0.pr_hostname);
699193066Sjamie	mtx_unlock(&prison0.pr_mtx);
700116376Ssimokawa}
701116376Ssimokawa
702116376Ssimokawa/*
703116376Ssimokawa * Called after bus reset.
704116376Ssimokawa */
705116376Ssimokawavoid
706169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status)
707116376Ssimokawa{
708116376Ssimokawa	struct firewire_dev_comm *fdc;
709117350Ssimokawa	struct crom_src *src;
710116376Ssimokawa	device_t *devlistp;
711189928Ssbruno	uint32_t *newrom;
712116376Ssimokawa	int i, devcnt;
713116376Ssimokawa
714187993Ssbruno	FW_GLOCK_ASSERT(fc);
715187993Ssbruno	if (fc->status == FWBUSMGRELECT)
716116376Ssimokawa		callout_stop(&fc->bmr_callout);
717187993Ssbruno
718169117Ssimokawa	fc->status = new_status;
719116376Ssimokawa	fw_reset_csr(fc);
720187993Ssbruno
721187993Ssbruno	if (fc->status == FWBUSNOTREADY)
722187993Ssbruno		fw_init_crom(fc);
723187993Ssbruno
724116376Ssimokawa	fw_reset_crom(fc);
725116376Ssimokawa
726113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
727272214Skan		for (i = 0; i < devcnt; i++)
728272214Skan			if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
729113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
730113584Ssimokawa				if (fdc->post_busreset != NULL)
731113584Ssimokawa					fdc->post_busreset(fdc);
732113584Ssimokawa			}
733113584Ssimokawa		free(devlistp, M_TEMP);
734113584Ssimokawa	}
735116376Ssimokawa
736117350Ssimokawa	src = &fc->crom_src_buf->src;
737272214Skan	/*
738272214Skan	 * If the old config rom needs to be overwritten,
739272214Skan	 * bump the businfo.generation indicator to
740272214Skan	 * indicate that we need to be reprobed
741272214Skan	 * See 1394a-2000 8.3.2.5.4 for more details.
742272214Skan	 * generation starts at 2 and rolls over at 0xF
743272214Skan	 * back to 2.
744272214Skan	 *
745272214Skan	 * A generation of 0 indicates a device
746272214Skan	 * that is not 1394a-2000 compliant.
747272214Skan	 * A generation of 1 indicates a device that
748272214Skan	 * does not change it's Bus Info Block or
749272214Skan	 * Configuration ROM.
750272214Skan	 */
751189928Ssbruno#define FW_MAX_GENERATION 0xF
752189928Ssbruno	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
753189928Ssbruno	src = &fc->crom_src_buf->src;
754189928Ssbruno	crom_load(src, newrom, CROMSIZE);
755189928Ssbruno	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
756277508Swill		/* Bump generation and reload. */
757277508Swill		src->businfo.generation++;
758277508Swill
759277508Swill		/* Handle generation count wraps. */
760277508Swill		if (src->businfo.generation < FW_GENERATION_CHANGEABLE)
761189928Ssbruno			src->businfo.generation = FW_GENERATION_CHANGEABLE;
762277508Swill
763277508Swill		/* Recalculate CRC to account for generation change. */
764277508Swill		crom_load(src, newrom, CROMSIZE);
765272214Skan		bcopy(newrom, fc->config_rom, CROMSIZE);
766189928Ssbruno	}
767189928Ssbruno	free(newrom, M_FW);
768103285Sikob}
769106790Ssimokawa
770103285Sikob/* Call once after reboot */
771106790Ssimokawavoid fw_init(struct firewire_comm *fc)
772103285Sikob{
773103285Sikob	int i;
774106543Ssimokawa#ifdef FW_VMACCESS
775103285Sikob	struct fw_xfer *xfer;
776103285Sikob	struct fw_bind *fwb;
777106543Ssimokawa#endif
778103285Sikob
779103285Sikob	fc->arq->queued = 0;
780103285Sikob	fc->ars->queued = 0;
781103285Sikob	fc->atq->queued = 0;
782103285Sikob	fc->ats->queued = 0;
783103285Sikob
784103285Sikob	fc->arq->buf = NULL;
785103285Sikob	fc->ars->buf = NULL;
786103285Sikob	fc->atq->buf = NULL;
787103285Sikob	fc->ats->buf = NULL;
788103285Sikob
789113584Ssimokawa	fc->arq->flag = 0;
790113584Ssimokawa	fc->ars->flag = 0;
791113584Ssimokawa	fc->atq->flag = 0;
792113584Ssimokawa	fc->ats->flag = 0;
793103285Sikob
794103285Sikob	STAILQ_INIT(&fc->atq->q);
795103285Sikob	STAILQ_INIT(&fc->ats->q);
796103285Sikob
797272214Skan	for (i = 0; i < fc->nisodma; i++) {
798103285Sikob		fc->it[i]->queued = 0;
799103285Sikob		fc->ir[i]->queued = 0;
800103285Sikob
801103285Sikob		fc->it[i]->start = NULL;
802103285Sikob		fc->ir[i]->start = NULL;
803103285Sikob
804103285Sikob		fc->it[i]->buf = NULL;
805103285Sikob		fc->ir[i]->buf = NULL;
806103285Sikob
807103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
808103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
809103285Sikob
810103285Sikob		STAILQ_INIT(&fc->it[i]->q);
811103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
812103285Sikob	}
813103285Sikob
814103285Sikob	fc->arq->maxq = FWMAXQUEUE;
815103285Sikob	fc->ars->maxq = FWMAXQUEUE;
816103285Sikob	fc->atq->maxq = FWMAXQUEUE;
817103285Sikob	fc->ats->maxq = FWMAXQUEUE;
818103285Sikob
819272214Skan	for (i = 0; i < fc->nisodma; i++) {
820103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
821103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
822103285Sikob	}
823187993Ssbruno
824103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
825103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
826103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
827103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
828103285Sikob
829110193Ssimokawa	STAILQ_INIT(&fc->devices);
830103285Sikob
831272214Skan	/* Initialize Async handlers */
832103285Sikob	STAILQ_INIT(&fc->binds);
833272214Skan	for (i = 0; i < 0x40; i++) {
834103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
835103285Sikob	}
836103285Sikob
837103285Sikob/* DV depend CSRs see blue book */
838103285Sikob#if 0
839103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
840103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
841272214Skan	for (i = 4; i < 0x7c/4; i += 4) {
842272214Skan		CSRARC(fc, i + oPCR) = 0x8000007a;
843103285Sikob	}
844272214Skan
845103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
846103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
847272214Skan	for (i = 4; i < 0x7c/4; i += 4) {
848272214Skan		CSRARC(fc, i + iPCR) = 0x0;
849103285Sikob	}
850103285Sikob#endif
851103285Sikob
852116376Ssimokawa	fc->crom_src_buf = NULL;
853103285Sikob
854106543Ssimokawa#ifdef FW_VMACCESS
855103285Sikob	xfer = fw_xfer_alloc();
856272214Skan	if (xfer == NULL)
857272214Skan		return;
858103285Sikob
859272214Skan	fwb = malloc(sizeof(struct fw_bind), M_FW, M_NOWAIT);
860272214Skan	if (fwb == NULL) {
861103285Sikob		fw_xfer_free(xfer);
862139680Sjmg		return;
863103285Sikob	}
864167632Ssimokawa	xfer->hand = fw_vmaccess;
865103285Sikob	xfer->fc = fc;
866103285Sikob	xfer->sc = NULL;
867103285Sikob
868103285Sikob	fwb->start_hi = 0x2;
869103285Sikob	fwb->start_lo = 0;
870103285Sikob	fwb->addrlen = 0xffffffff;
871103285Sikob	fwb->xfer = xfer;
872103285Sikob	fw_bindadd(fc, fwb);
873106543Ssimokawa#endif
874103285Sikob}
875106790Ssimokawa
876272214Skan#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)? -1 : \
877272214Skan    ((fwb)->end < (addr)) ? 1 : 0)
878120660Ssimokawa
879103285Sikob/*
880129541Sdfr * To lookup bound process from IEEE1394 address.
881103285Sikob */
882106813Ssimokawastruct fw_bind *
883129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
884103285Sikob{
885120660Ssimokawa	u_int64_t addr;
886170374Ssimokawa	struct fw_bind *tfw, *r = NULL;
887120660Ssimokawa
888120660Ssimokawa	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
889170374Ssimokawa	FW_GLOCK(fc);
890120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
891170374Ssimokawa		if (BIND_CMP(addr, tfw) == 0) {
892170374Ssimokawa			r = tfw;
893170374Ssimokawa			break;
894170374Ssimokawa		}
895170374Ssimokawa	FW_GUNLOCK(fc);
896272214Skan	return (r);
897103285Sikob}
898106790Ssimokawa
899103285Sikob/*
900103285Sikob * To bind IEEE1394 address block to process.
901103285Sikob */
902106790Ssimokawaint
903106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
904103285Sikob{
905120660Ssimokawa	struct fw_bind *tfw, *prev = NULL;
906170374Ssimokawa	int r = 0;
907120660Ssimokawa
908120660Ssimokawa	if (fwb->start > fwb->end) {
909127468Ssimokawa		printf("%s: invalid range\n", __func__);
910120660Ssimokawa		return EINVAL;
911120660Ssimokawa	}
912120660Ssimokawa
913170374Ssimokawa	FW_GLOCK(fc);
914120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
915120660Ssimokawa		if (fwb->end < tfw->start)
916120660Ssimokawa			break;
917120660Ssimokawa		prev = tfw;
918120660Ssimokawa	}
919170374Ssimokawa	if (prev == NULL)
920103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
921170374Ssimokawa	else if (prev->end < fwb->start)
922120660Ssimokawa		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
923170374Ssimokawa	else {
924170374Ssimokawa		printf("%s: bind failed\n", __func__);
925170374Ssimokawa		r = EBUSY;
926103285Sikob	}
927170374Ssimokawa	FW_GUNLOCK(fc);
928170374Ssimokawa	return (r);
929103285Sikob}
930103285Sikob
931103285Sikob/*
932103285Sikob * To free IEEE1394 address block.
933103285Sikob */
934106790Ssimokawaint
935106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
936103285Sikob{
937120660Ssimokawa#if 0
938120660Ssimokawa	struct fw_xfer *xfer, *next;
939120660Ssimokawa#endif
940120660Ssimokawa	struct fw_bind *tfw;
941103285Sikob	int s;
942103285Sikob
943103285Sikob	s = splfw();
944170374Ssimokawa	FW_GLOCK(fc);
945120660Ssimokawa	STAILQ_FOREACH(tfw, &fc->binds, fclist)
946120660Ssimokawa		if (tfw == fwb) {
947120660Ssimokawa			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
948120660Ssimokawa			goto found;
949120660Ssimokawa		}
950120660Ssimokawa
951129541Sdfr	printf("%s: no such binding\n", __func__);
952170374Ssimokawa	FW_GUNLOCK(fc);
953120660Ssimokawa	splx(s);
954120660Ssimokawa	return (1);
955120660Ssimokawafound:
956120660Ssimokawa#if 0
957113584Ssimokawa	/* shall we do this? */
958113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
959113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
960113584Ssimokawa		fw_xfer_free(xfer);
961113584Ssimokawa	}
962113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
963120660Ssimokawa#endif
964170374Ssimokawa	FW_GUNLOCK(fc);
965113584Ssimokawa
966103285Sikob	splx(s);
967103285Sikob	return 0;
968103285Sikob}
969103285Sikob
970169130Ssimokawaint
971169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
972169130Ssimokawa    int slen, int rlen, int n,
973169130Ssimokawa    struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
974169130Ssimokawa{
975169130Ssimokawa	int i, s;
976169130Ssimokawa	struct fw_xfer *xfer;
977169130Ssimokawa
978169130Ssimokawa	for (i = 0; i < n; i++) {
979169130Ssimokawa		xfer = fw_xfer_alloc_buf(type, slen, rlen);
980169130Ssimokawa		if (xfer == NULL)
981272214Skan			return (i);
982169130Ssimokawa		xfer->fc = fc;
983169130Ssimokawa		xfer->sc = sc;
984169130Ssimokawa		xfer->hand = hand;
985169130Ssimokawa		s = splfw();
986169130Ssimokawa		STAILQ_INSERT_TAIL(q, xfer, link);
987169130Ssimokawa		splx(s);
988169130Ssimokawa	}
989169130Ssimokawa	return (n);
990169130Ssimokawa}
991169130Ssimokawa
992169130Ssimokawavoid
993169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q)
994169130Ssimokawa{
995169130Ssimokawa	struct fw_xfer *xfer, *next;
996169130Ssimokawa
997169130Ssimokawa	for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
998272214Skan		next = STAILQ_NEXT(xfer, link);
999272214Skan		fw_xfer_free_buf(xfer);
1000272214Skan	}
1001272214Skan	STAILQ_INIT(q);
1002169130Ssimokawa}
1003170374Ssimokawa/*
1004170374Ssimokawa * dump packet header
1005170374Ssimokawa */
1006170374Ssimokawastatic void
1007170374Ssimokawafw_dump_hdr(struct fw_pkt *fp, char *prefix)
1008170374Ssimokawa{
1009170374Ssimokawa	printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
1010170374Ssimokawa	    "src=0x%03x\n", prefix,
1011170374Ssimokawa	    fp->mode.hdr.dst & 0x3f,
1012170374Ssimokawa	    fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3,
1013170374Ssimokawa	    fp->mode.hdr.tcode, fp->mode.hdr.pri,
1014170374Ssimokawa	    fp->mode.hdr.src);
1015170374Ssimokawa}
1016169130Ssimokawa
1017103285Sikob/*
1018103285Sikob * To free transaction label.
1019103285Sikob */
1020106790Ssimokawastatic void
1021106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
1022103285Sikob{
1023169119Ssimokawa	struct fw_xfer *txfer;
1024103285Sikob
1025171513Ssimokawa	mtx_lock(&fc->tlabel_lock);
1026249291Swill	if (xfer->tl < 0) {
1027249291Swill		mtx_unlock(&fc->tlabel_lock);
1028249291Swill		return;
1029249291Swill	}
1030271795Swill	/* make sure the label is allocated */
1031169119Ssimokawa	STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
1032272214Skan		if (txfer == xfer)
1033169119Ssimokawa			break;
1034169119Ssimokawa	if (txfer == NULL) {
1035170374Ssimokawa		printf("%s: the xfer is not in the queue "
1036170374Ssimokawa		    "(tlabel=%d, flag=0x%x)\n",
1037170374Ssimokawa		    __FUNCTION__, xfer->tl, xfer->flag);
1038170374Ssimokawa		fw_dump_hdr(&xfer->send.hdr, "send");
1039170374Ssimokawa		fw_dump_hdr(&xfer->recv.hdr, "recv");
1040170374Ssimokawa		kdb_backtrace();
1041171513Ssimokawa		mtx_unlock(&fc->tlabel_lock);
1042169119Ssimokawa		return;
1043103285Sikob	}
1044169119Ssimokawa
1045169119Ssimokawa	STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
1046249291Swill	xfer->tl = -1;
1047171513Ssimokawa	mtx_unlock(&fc->tlabel_lock);
1048103285Sikob	return;
1049103285Sikob}
1050106790Ssimokawa
1051103285Sikob/*
1052103285Sikob * To obtain XFER structure by transaction label.
1053103285Sikob */
1054106790Ssimokawastatic struct fw_xfer *
1055170374Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode)
1056103285Sikob{
1057103285Sikob	struct fw_xfer *xfer;
1058103285Sikob	int s = splfw();
1059170374Ssimokawa	int req;
1060103285Sikob
1061171513Ssimokawa	mtx_lock(&fc->tlabel_lock);
1062169119Ssimokawa	STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
1063272214Skan		if (xfer->send.hdr.mode.hdr.dst == node) {
1064171513Ssimokawa			mtx_unlock(&fc->tlabel_lock);
1065103285Sikob			splx(s);
1066170374Ssimokawa			KASSERT(xfer->tl == tlabel,
1067170374Ssimokawa				("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel));
1068170374Ssimokawa			/* extra sanity check */
1069170374Ssimokawa			req = xfer->send.hdr.mode.hdr.tcode;
1070170374Ssimokawa			if (xfer->fc->tcode[req].valid_res != tcode) {
1071170374Ssimokawa				printf("%s: invalid response tcode "
1072170374Ssimokawa				    "(0x%x for 0x%x)\n", __FUNCTION__,
1073170374Ssimokawa				    tcode, req);
1074272214Skan				return (NULL);
1075170374Ssimokawa			}
1076272214Skan
1077110577Ssimokawa			if (firewire_debug > 2)
1078110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
1079272214Skan			return (xfer);
1080103285Sikob		}
1081171513Ssimokawa	mtx_unlock(&fc->tlabel_lock);
1082110577Ssimokawa	if (firewire_debug > 1)
1083110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
1084103285Sikob	splx(s);
1085272214Skan	return (NULL);
1086103285Sikob}
1087106790Ssimokawa
1088103285Sikob/*
1089103285Sikob * To allocate IEEE1394 XFER structure.
1090103285Sikob */
1091106790Ssimokawastruct fw_xfer *
1092110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
1093103285Sikob{
1094103285Sikob	struct fw_xfer *xfer;
1095106790Ssimokawa
1096110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
1097106790Ssimokawa	if (xfer == NULL)
1098106790Ssimokawa		return xfer;
1099106790Ssimokawa
1100110269Ssimokawa	xfer->malloc = type;
1101106790Ssimokawa
1102103285Sikob	return xfer;
1103103285Sikob}
1104106790Ssimokawa
1105113584Ssimokawastruct fw_xfer *
1106113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
1107113584Ssimokawa{
1108113584Ssimokawa	struct fw_xfer *xfer;
1109113584Ssimokawa
1110113584Ssimokawa	xfer = fw_xfer_alloc(type);
1111126102Scperciva	if (xfer == NULL)
1112272214Skan		return (NULL);
1113120660Ssimokawa	xfer->send.pay_len = send_len;
1114120660Ssimokawa	xfer->recv.pay_len = recv_len;
1115120660Ssimokawa	if (send_len > 0) {
1116120660Ssimokawa		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
1117120660Ssimokawa		if (xfer->send.payload == NULL) {
1118113584Ssimokawa			fw_xfer_free(xfer);
1119272214Skan			return (NULL);
1120113584Ssimokawa		}
1121113584Ssimokawa	}
1122120660Ssimokawa	if (recv_len > 0) {
1123120660Ssimokawa		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1124120660Ssimokawa		if (xfer->recv.payload == NULL) {
1125120660Ssimokawa			if (xfer->send.payload != NULL)
1126120660Ssimokawa				free(xfer->send.payload, type);
1127113584Ssimokawa			fw_xfer_free(xfer);
1128272214Skan			return (NULL);
1129113584Ssimokawa		}
1130113584Ssimokawa	}
1131272214Skan	return (xfer);
1132113584Ssimokawa}
1133113584Ssimokawa
1134103285Sikob/*
1135103285Sikob * IEEE1394 XFER post process.
1136103285Sikob */
1137103285Sikobvoid
1138103285Sikobfw_xfer_done(struct fw_xfer *xfer)
1139103285Sikob{
1140167632Ssimokawa	if (xfer->hand == NULL) {
1141167632Ssimokawa		printf("hand == NULL\n");
1142103285Sikob		return;
1143117716Ssimokawa	}
1144103285Sikob
1145121505Ssimokawa	if (xfer->fc == NULL)
1146121505Ssimokawa		panic("fw_xfer_done: why xfer->fc is NULL?");
1147121505Ssimokawa
1148168051Ssimokawa	fw_tl_free(xfer->fc, xfer);
1149167632Ssimokawa	xfer->hand(xfer);
1150103285Sikob}
1151103285Sikob
1152106790Ssimokawavoid
1153272214Skanfw_xfer_unload(struct fw_xfer *xfer)
1154103285Sikob{
1155113584Ssimokawa
1156272214Skan	if (xfer == NULL)
1157272214Skan		return;
1158277508Swill
1159113584Ssimokawa	if (xfer->fc != NULL) {
1160277508Swill		FW_GLOCK(xfer->fc);
1161277508Swill		if (xfer->flag & FWXF_INQ) {
1162277508Swill			STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1163277508Swill			xfer->flag &= ~FWXF_INQ;
1164277508Swill	#if 0
1165277508Swill			xfer->q->queued--;
1166277508Swill	#endif
1167277508Swill		}
1168277508Swill		FW_GUNLOCK(xfer->fc);
1169277508Swill
1170249291Swill		/*
1171249291Swill		 * Ensure that any tlabel owner can't access this
1172249291Swill		 * xfer after it's freed.
1173249291Swill		 */
1174249291Swill		fw_tl_free(xfer->fc, xfer);
1175114729Ssimokawa#if 1
1176272214Skan		if (xfer->flag & FWXF_START)
1177114729Ssimokawa			/*
1178114729Ssimokawa			 * This could happen if:
1179114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1180114729Ssimokawa			 *  2. firewire_watch() is called.
1181114729Ssimokawa			 */
1182114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1183103285Sikob#endif
1184103285Sikob	}
1185170374Ssimokawa	xfer->flag = FWXF_INIT;
1186113584Ssimokawa	xfer->resp = 0;
1187113584Ssimokawa}
1188272214Skan
1189113584Ssimokawa/*
1190272214Skan * To free IEEE1394 XFER structure.
1191113584Ssimokawa */
1192113584Ssimokawavoid
1193272214Skanfw_xfer_free_buf(struct fw_xfer *xfer)
1194113584Ssimokawa{
1195120660Ssimokawa	if (xfer == NULL) {
1196127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1197120660Ssimokawa		return;
1198120660Ssimokawa	}
1199113584Ssimokawa	fw_xfer_unload(xfer);
1200272214Skan	if (xfer->send.payload != NULL)
1201120660Ssimokawa		free(xfer->send.payload, xfer->malloc);
1202272214Skan	if (xfer->recv.payload != NULL)
1203120660Ssimokawa		free(xfer->recv.payload, xfer->malloc);
1204110269Ssimokawa	free(xfer, xfer->malloc);
1205103285Sikob}
1206103285Sikob
1207120660Ssimokawavoid
1208272214Skanfw_xfer_free(struct fw_xfer *xfer)
1209120660Ssimokawa{
1210120660Ssimokawa	if (xfer == NULL) {
1211127468Ssimokawa		printf("%s: xfer == NULL\n", __func__);
1212120660Ssimokawa		return;
1213120660Ssimokawa	}
1214120660Ssimokawa	fw_xfer_unload(xfer);
1215120660Ssimokawa	free(xfer, xfer->malloc);
1216120660Ssimokawa}
1217120660Ssimokawa
1218120660Ssimokawavoid
1219110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1220103285Sikob{
1221103285Sikob#if 0
1222170374Ssimokawa	printf("asyreq done flag=0x%02x resp=%d\n",
1223170374Ssimokawa				xfer->flag, xfer->resp);
1224103285Sikob#endif
1225103285Sikob	fw_xfer_free(xfer);
1226103285Sikob}
1227103285Sikob
1228103285Sikob/*
1229272214Skan * To configure PHY.
1230103285Sikob */
1231103285Sikobstatic void
1232103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1233103285Sikob{
1234103285Sikob	struct fw_xfer *xfer;
1235103285Sikob	struct fw_pkt *fp;
1236103285Sikob
1237103285Sikob	fc->status = FWBUSPHYCONF;
1238103285Sikob
1239120660Ssimokawa	xfer = fw_xfer_alloc(M_FWXFER);
1240113584Ssimokawa	if (xfer == NULL)
1241113584Ssimokawa		return;
1242103285Sikob	xfer->fc = fc;
1243167632Ssimokawa	xfer->hand = fw_asy_callback_free;
1244103285Sikob
1245120660Ssimokawa	fp = &xfer->send.hdr;
1246103285Sikob	fp->mode.ld[1] = 0;
1247103285Sikob	if (root_node >= 0)
1248272214Skan		fp->mode.ld[1] |= (1 << 23) | (root_node & 0x3f) << 24;
1249103285Sikob	if (gap_count >= 0)
1250272214Skan		fp->mode.ld[1] |= (1 << 22) | (gap_count & 0x3f) << 16;
1251103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1252103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1253103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1254103285Sikob
1255107653Ssimokawa	if (firewire_debug)
1256188722Ssbruno		device_printf(fc->bdev, "%s: root_node=%d gap_count=%d\n",
1257188722Ssbruno					__func__, root_node, gap_count);
1258103285Sikob	fw_asyreq(fc, -1, xfer);
1259103285Sikob}
1260103285Sikob
1261103285Sikob/*
1262272214Skan * Dump self ID.
1263103285Sikob */
1264103285Sikobstatic void
1265129585Sdfrfw_print_sid(uint32_t sid)
1266103285Sikob{
1267103285Sikob	union fw_self_id *s;
1268103285Sikob	s = (union fw_self_id *) &sid;
1269272214Skan	if (s->p0.sequel) {
1270272214Skan		if (s->p1.sequence_num == FW_SELF_ID_PAGE0) {
1271188726Ssbruno			printf("node:%d p3:%d p4:%d p5:%d p6:%d p7:%d"
1272272214Skan			    "p8:%d p9:%d p10:%d\n",
1273272214Skan			    s->p1.phy_id, s->p1.port3, s->p1.port4,
1274272214Skan			    s->p1.port5, s->p1.port6, s->p1.port7,
1275272214Skan			    s->p1.port8, s->p1.port9, s->p1.port10);
1276272214Skan		} else if (s->p2.sequence_num == FW_SELF_ID_PAGE1) {
1277188726Ssbruno			printf("node:%d p11:%d p12:%d p13:%d p14:%d p15:%d\n",
1278272214Skan			    s->p2.phy_id, s->p2.port11, s->p2.port12,
1279272214Skan			    s->p2.port13, s->p2.port14, s->p2.port15);
1280188726Ssbruno		} else {
1281188726Ssbruno			printf("node:%d Unknown Self ID Page number %d\n",
1282272214Skan			    s->p1.phy_id, s->p1.sequence_num);
1283188726Ssbruno		}
1284188726Ssbruno	} else {
1285188726Ssbruno		printf("node:%d link:%d gap:%d spd:%d con:%d pwr:%d"
1286272214Skan		    " p0:%d p1:%d p2:%d i:%d m:%d\n",
1287272214Skan		    s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1288272214Skan		    s->p0.phy_speed, s->p0.contender,
1289272214Skan		    s->p0.power_class, s->p0.port0, s->p0.port1,
1290272214Skan		    s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1291188726Ssbruno	}
1292103285Sikob}
1293103285Sikob
1294103285Sikob/*
1295272214Skan * To receive self ID.
1296103285Sikob */
1297272214Skanvoid fw_sidrcv(struct firewire_comm *fc, uint32_t *sid, u_int len)
1298103285Sikob{
1299129585Sdfr	uint32_t *p;
1300103285Sikob	union fw_self_id *self_id;
1301103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1302103285Sikob
1303272214Skan	fc->sid_cnt = len / (sizeof(uint32_t) * 2);
1304103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1305129585Sdfr	CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1306103285Sikob	fc->status = FWBUSCYMELECT;
1307103285Sikob	fc->topology_map->crc_len = 2;
1308272214Skan	fc->topology_map->generation++;
1309103285Sikob	fc->topology_map->self_id_count = 0;
1310272214Skan	fc->topology_map->node_count= 0;
1311272214Skan	fc->speed_map->generation++;
1312272214Skan	fc->speed_map->crc_len = 1 + (64 * 64 + 3) / 4;
1313103285Sikob	self_id = &fc->topology_map->self_id[0];
1314272214Skan	for (i = 0; i < fc->sid_cnt; i++) {
1315103285Sikob		if (sid[1] != ~sid[0]) {
1316272214Skan			device_printf(fc->bdev,
1317272214Skan			    "%s: ERROR invalid self-id packet\n", __func__);
1318103285Sikob			sid += 2;
1319103285Sikob			continue;
1320103285Sikob		}
1321103285Sikob		*self_id = *((union fw_self_id *)sid);
1322103285Sikob		fc->topology_map->crc_len++;
1323272214Skan		if (self_id->p0.sequel == 0) {
1324272214Skan			fc->topology_map->node_count++;
1325103285Sikob			c_port = 0;
1326188722Ssbruno			if (firewire_debug)
1327188722Ssbruno				fw_print_sid(sid[0]);
1328103285Sikob			node = self_id->p0.phy_id;
1329272214Skan			if (fc->max_node < node)
1330103285Sikob				fc->max_node = self_id->p0.phy_id;
1331103285Sikob			/* XXX I'm not sure this is the right speed_map */
1332272214Skan			fc->speed_map->speed[node][node] =
1333272214Skan			    self_id->p0.phy_speed;
1334272214Skan			for (j = 0; j < node; j++) {
1335272214Skan				fc->speed_map->speed[j][node] =
1336272214Skan				    fc->speed_map->speed[node][j] =
1337272214Skan				    min(fc->speed_map->speed[j][j],
1338272214Skan					self_id->p0.phy_speed);
1339103285Sikob			}
1340103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1341272214Skan			  (self_id->p0.link_active && self_id->p0.contender))
1342103285Sikob				fc->irm = self_id->p0.phy_id;
1343272214Skan			if (self_id->p0.port0 >= 0x2)
1344103285Sikob				c_port++;
1345272214Skan			if (self_id->p0.port1 >= 0x2)
1346103285Sikob				c_port++;
1347272214Skan			if (self_id->p0.port2 >= 0x2)
1348103285Sikob				c_port++;
1349103285Sikob		}
1350272214Skan		if (c_port > 2)
1351103285Sikob			i_branch += (c_port - 2);
1352103285Sikob		sid += 2;
1353103285Sikob		self_id++;
1354272214Skan		fc->topology_map->self_id_count++;
1355103285Sikob	}
1356103285Sikob	/* CRC */
1357103285Sikob	fc->topology_map->crc = fw_crc16(
1358272214Skan	    (uint32_t *)&fc->topology_map->generation,
1359272214Skan	    fc->topology_map->crc_len * 4);
1360103285Sikob	fc->speed_map->crc = fw_crc16(
1361272214Skan	    (uint32_t *)&fc->speed_map->generation,
1362272214Skan	    fc->speed_map->crc_len * 4);
1363103285Sikob	/* byteswap and copy to CSR */
1364129585Sdfr	p = (uint32_t *)fc->topology_map;
1365103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1366103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1367129585Sdfr	p = (uint32_t *)fc->speed_map;
1368103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1369103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1370129585Sdfr	/* don't byte-swap uint8_t array */
1371272214Skan	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1) * 4);
1372103285Sikob
1373103285Sikob	fc->max_hop = fc->max_node - i_branch;
1374188722Ssbruno	device_printf(fc->bdev, "%d nodes, maxhop <= %d %s irm(%d) %s\n",
1375272214Skan	    fc->max_node + 1, fc->max_hop,
1376272214Skan	    (fc->irm == -1) ? "Not IRM capable" : "cable IRM",
1377272214Skan	    fc->irm, (fc->irm == fc->nodeid) ? " (me) " : "");
1378103285Sikob
1379109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1380114909Ssimokawa		if (fc->irm == fc->nodeid) {
1381103285Sikob			fc->status = FWBUSMGRDONE;
1382103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1383114909Ssimokawa			fw_bmr(fc);
1384109736Ssimokawa		} else {
1385103285Sikob			fc->status = FWBUSMGRELECT;
1386272214Skan			callout_reset(&fc->bmr_callout, hz / 8,
1387272214Skan			    fw_try_bmr, fc);
1388103285Sikob		}
1389114909Ssimokawa	} else
1390103285Sikob		fc->status = FWBUSMGRDONE;
1391114909Ssimokawa
1392272214Skan	callout_reset(&fc->busprobe_callout, hz / 4, fw_bus_probe, fc);
1393103285Sikob}
1394106790Ssimokawa
1395103285Sikob/*
1396272214Skan * To probe devices on the IEEE1394 bus.
1397103285Sikob */
1398106790Ssimokawastatic void
1399272214Skanfw_bus_probe(void *arg)
1400103285Sikob{
1401272214Skan	struct firewire_comm *fc;
1402272214Skan	struct fw_device *fwdev;
1403103285Sikob	int s;
1404103285Sikob
1405103285Sikob	s = splfw();
1406272214Skan	fc = arg;
1407103285Sikob	fc->status = FWBUSEXPLORE;
1408103285Sikob
1409120850Ssimokawa	/* Invalidate all devices, just after bus reset. */
1410188722Ssbruno	if (firewire_debug)
1411188722Ssbruno		device_printf(fc->bdev, "%s:"
1412188722Ssbruno			"iterate and invalidate all nodes\n",
1413188722Ssbruno			__func__);
1414120850Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1415110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1416103285Sikob			fwdev->status = FWDEVINVAL;
1417103285Sikob			fwdev->rcnt = 0;
1418188722Ssbruno			if (firewire_debug)
1419188722Ssbruno				device_printf(fc->bdev, "%s:"
1420188722Ssbruno					"Invalidate Dev ID: %08x%08x\n",
1421188722Ssbruno					__func__, fwdev->eui.hi, fwdev->eui.lo);
1422188722Ssbruno		} else {
1423188722Ssbruno			if (firewire_debug)
1424188722Ssbruno				device_printf(fc->bdev, "%s:"
1425188722Ssbruno					"Dev ID: %08x%08x already invalid\n",
1426188722Ssbruno					__func__, fwdev->eui.hi, fwdev->eui.lo);
1427103285Sikob		}
1428169806Ssimokawa	splx(s);
1429120850Ssimokawa
1430272214Skan	wakeup(fc);
1431103285Sikob}
1432106790Ssimokawa
1433169806Ssimokawastatic int
1434169806Ssimokawafw_explore_read_quads(struct fw_device *fwdev, int offset,
1435188722Ssbruno    uint32_t *quad, int length)
1436129274Sdfr{
1437169806Ssimokawa	struct fw_xfer *xfer;
1438169806Ssimokawa	uint32_t tmp;
1439169806Ssimokawa	int i, error;
1440129274Sdfr
1441272214Skan	for (i = 0; i < length; i++, offset += sizeof(uint32_t)) {
1442272214Skan		xfer = fwmem_read_quad(fwdev, NULL, -1, 0xffff,
1443272214Skan		    0xf0000000 | offset, &tmp, fw_xferwake);
1444169806Ssimokawa		if (xfer == NULL)
1445169806Ssimokawa			return (-1);
1446170374Ssimokawa		fw_xferwait(xfer);
1447169806Ssimokawa
1448169806Ssimokawa		if (xfer->resp == 0)
1449169806Ssimokawa			quad[i] = ntohl(tmp);
1450169806Ssimokawa
1451169806Ssimokawa		error = xfer->resp;
1452169806Ssimokawa		fw_xfer_free(xfer);
1453169806Ssimokawa		if (error)
1454169806Ssimokawa			return (error);
1455169806Ssimokawa	}
1456169806Ssimokawa	return (0);
1457169806Ssimokawa}
1458169806Ssimokawa
1459169806Ssimokawa
1460169806Ssimokawastatic int
1461169806Ssimokawafw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
1462169806Ssimokawa{
1463169806Ssimokawa	int err, i, off;
1464169806Ssimokawa	struct csrdirectory *dir;
1465169806Ssimokawa	struct csrreg *reg;
1466169806Ssimokawa
1467272214Skan	dir = (struct csrdirectory *)&fwdev->csrrom[offset / sizeof(uint32_t)];
1468169806Ssimokawa	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1469169806Ssimokawa	    (uint32_t *)dir, 1);
1470169806Ssimokawa	if (err)
1471169806Ssimokawa		return (-1);
1472169806Ssimokawa
1473169806Ssimokawa	offset += sizeof(uint32_t);
1474272214Skan	reg = (struct csrreg *)&fwdev->csrrom[offset / sizeof(uint32_t)];
1475169806Ssimokawa	err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1476169806Ssimokawa	    (uint32_t *)reg, dir->crc_len);
1477169806Ssimokawa	if (err)
1478169806Ssimokawa		return (-1);
1479169806Ssimokawa
1480169806Ssimokawa	/* XXX check CRC */
1481169806Ssimokawa
1482169806Ssimokawa	off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
1483169806Ssimokawa	if (fwdev->rommax < off)
1484169806Ssimokawa		fwdev->rommax = off;
1485169806Ssimokawa
1486169806Ssimokawa	if (recur == 0)
1487169806Ssimokawa		return (0);
1488169806Ssimokawa
1489272214Skan	for (i = 0; i < dir->crc_len; i++, offset += sizeof(uint32_t)) {
1490171302Ssimokawa		if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D)
1491169806Ssimokawa			recur = 1;
1492171302Ssimokawa		else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L)
1493169806Ssimokawa			recur = 0;
1494169806Ssimokawa		else
1495129274Sdfr			continue;
1496169806Ssimokawa
1497169806Ssimokawa		off = offset + reg[i].val * sizeof(uint32_t);
1498169806Ssimokawa		if (off > CROMSIZE) {
1499169806Ssimokawa			printf("%s: invalid offset %d\n", __FUNCTION__, off);
1500272214Skan			return (-1);
1501169806Ssimokawa		}
1502169806Ssimokawa		err = fw_explore_csrblock(fwdev, off, recur);
1503169806Ssimokawa		if (err)
1504169806Ssimokawa			return (-1);
1505129274Sdfr	}
1506169806Ssimokawa	return (0);
1507129274Sdfr}
1508129274Sdfr
1509169806Ssimokawastatic int
1510169806Ssimokawafw_explore_node(struct fw_device *dfwdev)
1511103285Sikob{
1512169806Ssimokawa	struct firewire_comm *fc;
1513110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1514169806Ssimokawa	uint32_t *csr;
1515169806Ssimokawa	struct csrhdr *hdr;
1516169806Ssimokawa	struct bus_info *binfo;
1517188704Ssbruno	int err, node;
1518188704Ssbruno	uint32_t speed_test = 0;
1519103285Sikob
1520169806Ssimokawa	fc = dfwdev->fc;
1521169806Ssimokawa	csr = dfwdev->csrrom;
1522169806Ssimokawa	node = dfwdev->dst;
1523103285Sikob
1524169806Ssimokawa	/* First quad */
1525169806Ssimokawa	err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1);
1526188704Ssbruno	if (err) {
1527188704Ssbruno		dfwdev->status = FWDEVINVAL;
1528169806Ssimokawa		return (-1);
1529188704Ssbruno	}
1530169806Ssimokawa	hdr = (struct csrhdr *)&csr[0];
1531169806Ssimokawa	if (hdr->info_len != 4) {
1532169806Ssimokawa		if (firewire_debug)
1533272214Skan			device_printf(fc->bdev,
1534272214Skan			    "%s: node%d: wrong bus info len(%d)\n",
1535188722Ssbruno			    __func__, node, hdr->info_len);
1536188722Ssbruno		dfwdev->status = FWDEVINVAL;
1537169806Ssimokawa		return (-1);
1538169806Ssimokawa	}
1539103285Sikob
1540169806Ssimokawa	/* bus info */
1541169806Ssimokawa	err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
1542188722Ssbruno	if (err) {
1543188722Ssbruno		dfwdev->status = FWDEVINVAL;
1544169806Ssimokawa		return (-1);
1545188722Ssbruno	}
1546169806Ssimokawa	binfo = (struct bus_info *)&csr[1];
1547169806Ssimokawa	if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
1548188722Ssbruno		dfwdev->status = FWDEVINVAL;
1549169806Ssimokawa		return (-1);
1550103285Sikob	}
1551188704Ssbruno
1552188704Ssbruno	if (firewire_debug)
1553188704Ssbruno		device_printf(fc->bdev, "%s: node(%d) BUS INFO BLOCK:\n"
1554272214Skan		    "irmc(%d) cmc(%d) isc(%d) bmc(%d) pmc(%d) "
1555272214Skan		    "cyc_clk_acc(%d) max_rec(%d) max_rom(%d) "
1556272214Skan		    "generation(%d) link_spd(%d)\n",
1557272214Skan		    __func__, node,
1558272214Skan		    binfo->irmc, binfo->cmc, binfo->isc,
1559272214Skan		    binfo->bmc, binfo->pmc, binfo->cyc_clk_acc,
1560272214Skan		    binfo->max_rec, binfo->max_rom,
1561272214Skan		    binfo->generation, binfo->link_spd);
1562188704Ssbruno
1563169806Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1564169806Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
1565169806Ssimokawa			break;
1566169806Ssimokawa	if (fwdev == NULL) {
1567169806Ssimokawa		/* new device */
1568169806Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1569272214Skan		    M_NOWAIT | M_ZERO);
1570169806Ssimokawa		if (fwdev == NULL) {
1571188722Ssbruno			device_printf(fc->bdev, "%s: node%d: no memory\n",
1572188722Ssbruno					__func__, node);
1573169806Ssimokawa			return (-1);
1574103285Sikob		}
1575106810Ssimokawa		fwdev->fc = fc;
1576169806Ssimokawa		fwdev->eui = binfo->eui64;
1577189928Ssbruno		fwdev->dst = dfwdev->dst;
1578189928Ssbruno		fwdev->maxrec = dfwdev->maxrec;
1579189928Ssbruno		fwdev->status = dfwdev->status;
1580189928Ssbruno
1581188704Ssbruno		/*
1582188704Ssbruno		 * Pre-1394a-2000 didn't have link_spd in
1583272214Skan		 * the Bus Info block, so try and use the
1584188704Ssbruno		 * speed map value.
1585188704Ssbruno		 * 1394a-2000 compliant devices only use
1586188704Ssbruno		 * the Bus Info Block link spd value, so
1587188704Ssbruno		 * ignore the speed map alltogether. SWB
1588188704Ssbruno		 */
1589272214Skan		if (binfo->link_spd == FWSPD_S100 /* 0 */) {
1590189928Ssbruno			device_printf(fc->bdev, "%s: "
1591272214Skan			    "Pre 1394a-2000 detected\n", __func__);
1592188704Ssbruno			fwdev->speed = fc->speed_map->speed[fc->nodeid][node];
1593188704Ssbruno		} else
1594188704Ssbruno			fwdev->speed = binfo->link_spd;
1595188704Ssbruno		/*
1596188704Ssbruno		 * Test this speed with a read to the CSRROM.
1597188704Ssbruno		 * If it fails, slow down the speed and retry.
1598188704Ssbruno		 */
1599189928Ssbruno		while (fwdev->speed > FWSPD_S100 /* 0 */) {
1600188704Ssbruno			err = fw_explore_read_quads(fwdev, CSRROMOFF,
1601272214Skan			    &speed_test, 1);
1602189928Ssbruno			if (err) {
1603272214Skan				device_printf(fc->bdev,
1604272214Skan				    "%s: fwdev->speed(%s) decremented due to negotiation\n",
1605272214Skan				    __func__, linkspeed[fwdev->speed]);
1606188704Ssbruno				fwdev->speed--;
1607189928Ssbruno			} else
1608188704Ssbruno				break;
1609272214Skan
1610188704Ssbruno		}
1611190792Ssbruno
1612190792Ssbruno		/*
1613272214Skan		 * If the fwdev is not found in the
1614190792Ssbruno		 * fc->devices TAILQ, then we will add it.
1615190792Ssbruno		 */
1616110179Ssimokawa		pfwdev = NULL;
1617110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1618110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1619169806Ssimokawa				(tfwdev->eui.hi == fwdev->eui.hi &&
1620169806Ssimokawa				tfwdev->eui.lo > fwdev->eui.lo))
1621110179Ssimokawa				break;
1622110179Ssimokawa			pfwdev = tfwdev;
1623103285Sikob		}
1624110179Ssimokawa		if (pfwdev == NULL)
1625110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1626110179Ssimokawa		else
1627110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1628189928Ssbruno	} else {
1629189928Ssbruno		fwdev->dst = node;
1630189928Ssbruno		fwdev->status = FWDEVINIT;
1631189928Ssbruno		/* unchanged ? */
1632189928Ssbruno		if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) {
1633189928Ssbruno			if (firewire_debug)
1634272214Skan				device_printf(fc->dev,
1635272214Skan				    "node%d: crom unchanged\n", node);
1636189928Ssbruno			return (0);
1637189928Ssbruno		}
1638103285Sikob	}
1639103285Sikob
1640169806Ssimokawa	bzero(&fwdev->csrrom[0], CROMSIZE);
1641106790Ssimokawa
1642169806Ssimokawa	/* copy first quad and bus info block */
1643169806Ssimokawa	bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5);
1644169806Ssimokawa	fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
1645103285Sikob
1646169806Ssimokawa	err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
1647113584Ssimokawa
1648169806Ssimokawa	if (err) {
1649189928Ssbruno		if (firewire_debug)
1650189928Ssbruno			device_printf(fc->dev, "%s: explore csrblock failed err(%d)\n",
1651189928Ssbruno					__func__, err);
1652169806Ssimokawa		fwdev->status = FWDEVINVAL;
1653169806Ssimokawa		fwdev->csrrom[0] = 0;
1654103285Sikob	}
1655169806Ssimokawa	return (err);
1656103285Sikob
1657103285Sikob}
1658106790Ssimokawa
1659103285Sikob/*
1660169806Ssimokawa * Find the self_id packet for a node, ignoring sequels.
1661103285Sikob */
1662169806Ssimokawastatic union fw_self_id *
1663169806Ssimokawafw_find_self_id(struct firewire_comm *fc, int node)
1664106790Ssimokawa{
1665169806Ssimokawa	uint32_t i;
1666169806Ssimokawa	union fw_self_id *s;
1667103285Sikob
1668169806Ssimokawa	for (i = 0; i < fc->topology_map->self_id_count; i++) {
1669169806Ssimokawa		s = &fc->topology_map->self_id[i];
1670169806Ssimokawa		if (s->p0.sequel)
1671169806Ssimokawa			continue;
1672169806Ssimokawa		if (s->p0.phy_id == node)
1673169806Ssimokawa			return s;
1674110577Ssimokawa	}
1675169806Ssimokawa	return 0;
1676169806Ssimokawa}
1677110577Ssimokawa
1678169806Ssimokawastatic void
1679169806Ssimokawafw_explore(struct firewire_comm *fc)
1680169806Ssimokawa{
1681169806Ssimokawa	int node, err, s, i, todo, todo2, trys;
1682169806Ssimokawa	char nodes[63];
1683169806Ssimokawa	struct fw_device dfwdev;
1684169829Ssimokawa	union fw_self_id *fwsid;
1685110577Ssimokawa
1686169806Ssimokawa	todo = 0;
1687169806Ssimokawa	/* setup dummy fwdev */
1688169806Ssimokawa	dfwdev.fc = fc;
1689169806Ssimokawa	dfwdev.speed = 0;
1690169806Ssimokawa	dfwdev.maxrec = 8; /* 512 */
1691169806Ssimokawa	dfwdev.status = FWDEVINIT;
1692103285Sikob
1693272214Skan	for (node = 0; node <= fc->max_node; node++) {
1694169806Ssimokawa		/* We don't probe myself and linkdown nodes */
1695188722Ssbruno		if (node == fc->nodeid) {
1696188722Ssbruno			if (firewire_debug)
1697188722Ssbruno				device_printf(fc->bdev, "%s:"
1698272214Skan				    "found myself node(%d) fc->nodeid(%d) fc->max_node(%d)\n",
1699272214Skan				    __func__, node, fc->nodeid, fc->max_node);
1700169806Ssimokawa			continue;
1701188722Ssbruno		} else if (firewire_debug) {
1702188722Ssbruno			device_printf(fc->bdev, "%s:"
1703272214Skan			    "node(%d) fc->max_node(%d) found\n",
1704272214Skan			    __func__, node, fc->max_node);
1705188722Ssbruno		}
1706169829Ssimokawa		fwsid = fw_find_self_id(fc, node);
1707169829Ssimokawa		if (!fwsid || !fwsid->p0.link_active) {
1708169806Ssimokawa			if (firewire_debug)
1709272214Skan				device_printf(fc->bdev,
1710272214Skan				    "%s: node%d: link down\n",
1711272214Skan				    __func__, node);
1712169806Ssimokawa			continue;
1713103285Sikob		}
1714169806Ssimokawa		nodes[todo++] = node;
1715103285Sikob	}
1716103285Sikob
1717169806Ssimokawa	s = splfw();
1718272214Skan	for (trys = 0; todo > 0 && trys < 3; trys++) {
1719169806Ssimokawa		todo2 = 0;
1720272214Skan		for (i = 0; i < todo; i++) {
1721169806Ssimokawa			dfwdev.dst = nodes[i];
1722169806Ssimokawa			err = fw_explore_node(&dfwdev);
1723169806Ssimokawa			if (err)
1724169806Ssimokawa				nodes[todo2++] = nodes[i];
1725169806Ssimokawa			if (firewire_debug)
1726272214Skan				device_printf(fc->bdev,
1727272214Skan				    "%s: node %d, err = %d\n",
1728272214Skan				    __func__, node, err);
1729103285Sikob		}
1730169806Ssimokawa		todo = todo2;
1731103285Sikob	}
1732169806Ssimokawa	splx(s);
1733169806Ssimokawa}
1734169806Ssimokawa
1735169806Ssimokawastatic void
1736169806Ssimokawafw_bus_probe_thread(void *arg)
1737169806Ssimokawa{
1738169806Ssimokawa	struct firewire_comm *fc;
1739169806Ssimokawa
1740272214Skan	fc = arg;
1741169806Ssimokawa
1742170374Ssimokawa	mtx_lock(&fc->wait_lock);
1743170374Ssimokawa	while (fc->status != FWBUSDETACH) {
1744169806Ssimokawa		if (fc->status == FWBUSEXPLORE) {
1745170374Ssimokawa			mtx_unlock(&fc->wait_lock);
1746169806Ssimokawa			fw_explore(fc);
1747169806Ssimokawa			fc->status = FWBUSEXPDONE;
1748169806Ssimokawa			if (firewire_debug)
1749169806Ssimokawa				printf("bus_explore done\n");
1750169806Ssimokawa			fw_attach_dev(fc);
1751170374Ssimokawa			mtx_lock(&fc->wait_lock);
1752170374Ssimokawa		}
1753170374Ssimokawa		msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0);
1754127468Ssimokawa	}
1755170374Ssimokawa	mtx_unlock(&fc->wait_lock);
1756172836Sjulian	kproc_exit(0);
1757103285Sikob}
1758103285Sikob
1759103285Sikob/*
1760103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1761103285Sikob */
1762106815Ssimokawastatic void
1763106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1764103285Sikob{
1765120850Ssimokawa	struct fw_device *fwdev, *next;
1766103285Sikob	int i, err;
1767103285Sikob	device_t *devlistp;
1768103285Sikob	int devcnt;
1769103285Sikob	struct firewire_dev_comm *fdc;
1770103285Sikob
1771120850Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1772120850Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1773120850Ssimokawa		if (fwdev->status == FWDEVINIT) {
1774114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1775120850Ssimokawa		} else if (fwdev->status == FWDEVINVAL) {
1776272214Skan			fwdev->rcnt++;
1777188722Ssbruno			if (firewire_debug)
1778188722Ssbruno				device_printf(fc->bdev, "%s:"
1779272214Skan				    "fwdev->rcnt(%d), hold_count(%d)\n",
1780272214Skan				    __func__, fwdev->rcnt, hold_count);
1781120850Ssimokawa			if (fwdev->rcnt > hold_count) {
1782120850Ssimokawa				/*
1783120850Ssimokawa				 * Remove devices which have not been seen
1784120850Ssimokawa				 * for a while.
1785120850Ssimokawa				 */
1786120850Ssimokawa				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1787120850Ssimokawa				    link);
1788120850Ssimokawa				free(fwdev, M_FW);
1789120850Ssimokawa			}
1790120850Ssimokawa		}
1791120850Ssimokawa	}
1792103285Sikob
1793108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1794272214Skan	if (err == 0) {
1795272214Skan		for (i = 0; i < devcnt; i++) {
1796272214Skan			if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
1797188722Ssbruno				fdc = device_get_softc(devlistp[i]);
1798188722Ssbruno				if (fdc->post_explore != NULL)
1799188722Ssbruno					fdc->post_explore(fdc);
1800188722Ssbruno			}
1801103285Sikob		}
1802188722Ssbruno		free(devlistp, M_TEMP);
1803103285Sikob	}
1804103285Sikob
1805103285Sikob	return;
1806103285Sikob}
1807106815Ssimokawa
1808103285Sikob/*
1809129541Sdfr * To allocate unique transaction label.
1810103285Sikob */
1811106815Ssimokawastatic int
1812106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1813103285Sikob{
1814171457Ssimokawa	u_int dst, new_tlabel;
1815169119Ssimokawa	struct fw_xfer *txfer;
1816103285Sikob	int s;
1817103285Sikob
1818171457Ssimokawa	dst = xfer->send.hdr.mode.hdr.dst & 0x3f;
1819103285Sikob	s = splfw();
1820171513Ssimokawa	mtx_lock(&fc->tlabel_lock);
1821171457Ssimokawa	new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f;
1822171457Ssimokawa	STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel)
1823171457Ssimokawa		if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst)
1824272214Skan			break;
1825272214Skan	if (txfer == NULL) {
1826171457Ssimokawa		fc->last_tlabel[dst] = new_tlabel;
1827171457Ssimokawa		STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel);
1828171513Ssimokawa		mtx_unlock(&fc->tlabel_lock);
1829171457Ssimokawa		splx(s);
1830171457Ssimokawa		xfer->tl = new_tlabel;
1831171457Ssimokawa		xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2;
1832171457Ssimokawa		if (firewire_debug > 1)
1833171457Ssimokawa			printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel);
1834171457Ssimokawa		return (new_tlabel);
1835103285Sikob	}
1836171513Ssimokawa	mtx_unlock(&fc->tlabel_lock);
1837103285Sikob	splx(s);
1838103285Sikob
1839130460Sdfr	if (firewire_debug > 1)
1840130460Sdfr		printf("fw_get_tlabel: no free tlabel\n");
1841171457Ssimokawa	return (-1);
1842103285Sikob}
1843106815Ssimokawa
1844113584Ssimokawastatic void
1845120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb)
1846113584Ssimokawa{
1847120660Ssimokawa	struct fw_pkt *pkt;
1848120660Ssimokawa	u_char *p;
1849120660Ssimokawa	struct tcode_info *tinfo;
1850120660Ssimokawa	u_int res, i, len, plen;
1851113584Ssimokawa
1852169127Ssimokawa	rb->xfer->recv.spd = rb->spd;
1853120660Ssimokawa
1854120660Ssimokawa	pkt = (struct fw_pkt *)rb->vec->iov_base;
1855120660Ssimokawa	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1856120660Ssimokawa
1857272214Skan	/* Copy header */
1858120660Ssimokawa	p = (u_char *)&rb->xfer->recv.hdr;
1859120660Ssimokawa	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1860132771Skan	rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1861120660Ssimokawa	rb->vec->iov_len -= tinfo->hdr_len;
1862120660Ssimokawa
1863120660Ssimokawa	/* Copy payload */
1864120660Ssimokawa	p = (u_char *)rb->xfer->recv.payload;
1865120660Ssimokawa	res = rb->xfer->recv.pay_len;
1866120660Ssimokawa
1867120660Ssimokawa	/* special handling for RRESQ */
1868120660Ssimokawa	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1869129585Sdfr	    p != NULL && res >= sizeof(uint32_t)) {
1870129585Sdfr		*(uint32_t *)p = pkt->mode.rresq.data;
1871129585Sdfr		rb->xfer->recv.pay_len = sizeof(uint32_t);
1872120660Ssimokawa		return;
1873120660Ssimokawa	}
1874120660Ssimokawa
1875120660Ssimokawa	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1876120660Ssimokawa		return;
1877120660Ssimokawa
1878120660Ssimokawa	plen = pkt->mode.rresb.len;
1879120660Ssimokawa
1880120660Ssimokawa	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1881120660Ssimokawa		len = MIN(rb->vec->iov_len, plen);
1882113584Ssimokawa		if (res < len) {
1883188722Ssbruno			device_printf(rb->fc->bdev, "%s:"
1884188722Ssbruno				" rcv buffer(%d) is %d bytes short.\n",
1885188722Ssbruno				__func__, rb->xfer->recv.pay_len, len - res);
1886113584Ssimokawa			len = res;
1887113584Ssimokawa		}
1888120660Ssimokawa		bcopy(rb->vec->iov_base, p, len);
1889113584Ssimokawa		p += len;
1890113584Ssimokawa		res -= len;
1891120660Ssimokawa		plen -= len;
1892120660Ssimokawa		if (res == 0 || plen == 0)
1893113584Ssimokawa			break;
1894113584Ssimokawa	}
1895120660Ssimokawa	rb->xfer->recv.pay_len -= res;
1896113584Ssimokawa}
1897113584Ssimokawa
1898103285Sikob/*
1899129541Sdfr * Generic packet receiving process.
1900103285Sikob */
1901106815Ssimokawavoid
1902120660Ssimokawafw_rcv(struct fw_rcv_buf *rb)
1903103285Sikob{
1904103285Sikob	struct fw_pkt *fp, *resfp;
1905103285Sikob	struct fw_bind *bind;
1906169130Ssimokawa	int tcode;
1907113584Ssimokawa	int i, len, oldstate;
1908103285Sikob#if 0
1909103285Sikob	{
1910129585Sdfr		uint32_t *qld;
1911103285Sikob		int i;
1912129585Sdfr		qld = (uint32_t *)buf;
1913103285Sikob		printf("spd %d len:%d\n", spd, len);
1914272214Skan		for (i = 0; i <= len && i < 32; i+= 4) {
1915103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1916272214Skan			if ((i % 16) == 15) printf("\n");
1917103285Sikob		}
1918272214Skan		if ((i % 16) != 15) printf("\n");
1919103285Sikob	}
1920103285Sikob#endif
1921120660Ssimokawa	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1922113584Ssimokawa	tcode = fp->mode.common.tcode;
1923113584Ssimokawa	switch (tcode) {
1924103285Sikob	case FWTCODE_WRES:
1925103285Sikob	case FWTCODE_RRESQ:
1926103285Sikob	case FWTCODE_RRESB:
1927103285Sikob	case FWTCODE_LRES:
1928120660Ssimokawa		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1929170374Ssimokawa				fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode);
1930272214Skan		if (rb->xfer == NULL) {
1931272214Skan			device_printf(rb->fc->bdev, "%s: unknown response "
1932272214Skan			    "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1933272214Skan			    __func__,
1934272214Skan			    tcode_str[tcode], tcode,
1935272214Skan			    fp->mode.hdr.src,
1936272214Skan			    fp->mode.hdr.tlrt >> 2,
1937272214Skan			    fp->mode.hdr.tlrt & 3,
1938272214Skan			    fp->mode.rresq.data);
1939170374Ssimokawa#if 0
1940103285Sikob			printf("try ad-hoc work around!!\n");
1941120660Ssimokawa			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1942272214Skan			    (fp->mode.hdr.tlrt >> 2)^3);
1943120660Ssimokawa			if (rb->xfer == NULL) {
1944103285Sikob				printf("no use...\n");
1945169131Ssimokawa				return;
1946103285Sikob			}
1947103285Sikob#else
1948169131Ssimokawa			return;
1949103285Sikob#endif
1950103285Sikob		}
1951120660Ssimokawa		fw_rcv_copy(rb);
1952120660Ssimokawa		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1953120660Ssimokawa			rb->xfer->resp = EIO;
1954120660Ssimokawa		else
1955120660Ssimokawa			rb->xfer->resp = 0;
1956113584Ssimokawa		/* make sure the packet is drained in AT queue */
1957170374Ssimokawa		oldstate = rb->xfer->flag;
1958170374Ssimokawa		rb->xfer->flag = FWXF_RCVD;
1959113584Ssimokawa		switch (oldstate) {
1960113584Ssimokawa		case FWXF_SENT:
1961120660Ssimokawa			fw_xfer_done(rb->xfer);
1962103285Sikob			break;
1963113584Ssimokawa		case FWXF_START:
1964119289Ssimokawa#if 0
1965113584Ssimokawa			if (firewire_debug)
1966120660Ssimokawa				printf("not sent yet tl=%x\n", rb->xfer->tl);
1967119289Ssimokawa#endif
1968113584Ssimokawa			break;
1969103285Sikob		default:
1970188722Ssbruno			device_printf(rb->fc->bdev, "%s: "
1971272214Skan			    "unexpected flag 0x%02x\n", __func__,
1972272214Skan			    rb->xfer->flag);
1973103285Sikob		}
1974113584Ssimokawa		return;
1975103285Sikob	case FWTCODE_WREQQ:
1976103285Sikob	case FWTCODE_WREQB:
1977103285Sikob	case FWTCODE_RREQQ:
1978103285Sikob	case FWTCODE_RREQB:
1979103285Sikob	case FWTCODE_LREQ:
1980120660Ssimokawa		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1981272214Skan		    fp->mode.rreqq.dest_lo);
1982272214Skan		if (bind == NULL) {
1983188722Ssbruno			device_printf(rb->fc->bdev, "%s: "
1984272214Skan			    "Unknown service addr 0x%04x:0x%08x %s(%x)"
1985272214Skan			    " src=0x%x data=%x\n",
1986272214Skan			    __func__,
1987272214Skan			    fp->mode.wreqq.dest_hi,
1988272214Skan			    fp->mode.wreqq.dest_lo,
1989272214Skan			    tcode_str[tcode], tcode,
1990272214Skan			    fp->mode.hdr.src,
1991272214Skan			    ntohl(fp->mode.wreqq.data));
1992188722Ssbruno
1993170425Ssimokawa			if (rb->fc->status == FWBUSINIT) {
1994272214Skan				device_printf(rb->fc->bdev,
1995272214Skan				    "%s: cannot respond(bus reset)!\n",
1996272214Skan				    __func__);
1997169131Ssimokawa				return;
1998103285Sikob			}
1999120660Ssimokawa			rb->xfer = fw_xfer_alloc(M_FWXFER);
2000272214Skan			if (rb->xfer == NULL) {
2001103285Sikob				return;
2002103285Sikob			}
2003120660Ssimokawa			rb->xfer->send.spd = rb->spd;
2004120660Ssimokawa			rb->xfer->send.pay_len = 0;
2005120660Ssimokawa			resfp = &rb->xfer->send.hdr;
2006113584Ssimokawa			switch (tcode) {
2007103285Sikob			case FWTCODE_WREQQ:
2008103285Sikob			case FWTCODE_WREQB:
2009103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
2010103285Sikob				break;
2011103285Sikob			case FWTCODE_RREQQ:
2012103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
2013103285Sikob				break;
2014103285Sikob			case FWTCODE_RREQB:
2015103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
2016103285Sikob				break;
2017103285Sikob			case FWTCODE_LREQ:
2018103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
2019103285Sikob				break;
2020103285Sikob			}
2021103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
2022103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
2023103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
2024120660Ssimokawa			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
2025103285Sikob			resfp->mode.rresb.extcode = 0;
2026103285Sikob			resfp->mode.rresb.len = 0;
2027103285Sikob/*
2028170374Ssimokawa			rb->xfer->hand = fw_xferwake;
2029103285Sikob*/
2030167632Ssimokawa			rb->xfer->hand = fw_xfer_free;
2031272214Skan			if (fw_asyreq(rb->fc, -1, rb->xfer))
2032120660Ssimokawa				fw_xfer_free(rb->xfer);
2033103285Sikob		}
2034113584Ssimokawa		len = 0;
2035272214Skan		for (i = 0; i < rb->nvec; i++)
2036120660Ssimokawa			len += rb->vec[i].iov_len;
2037169130Ssimokawa		rb->xfer = STAILQ_FIRST(&bind->xferlist);
2038169130Ssimokawa		if (rb->xfer == NULL) {
2039188722Ssbruno			device_printf(rb->fc->bdev, "%s: "
2040272214Skan			    "Discard a packet for this bind.\n", __func__);
2041169131Ssimokawa			return;
2042103285Sikob		}
2043169130Ssimokawa		STAILQ_REMOVE_HEAD(&bind->xferlist, link);
2044169130Ssimokawa		fw_rcv_copy(rb);
2045169130Ssimokawa		rb->xfer->hand(rb->xfer);
2046169130Ssimokawa		return;
2047120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */
2048103285Sikob	case FWTCODE_STREAM:
2049103285Sikob	{
2050103285Sikob		struct fw_xferq *xferq;
2051103285Sikob
2052120660Ssimokawa		xferq = rb->fc->ir[sub];
2053103285Sikob#if 0
2054103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
2055103285Sikob			sub, len, off, spd);
2056103285Sikob#endif
2057272214Skan		if (xferq->queued >= xferq->maxq) {
2058103285Sikob			printf("receive queue is full\n");
2059169131Ssimokawa			return;
2060103285Sikob		}
2061272214Skan		/* XXX get xfer from xfer queue, we don't need copy for
2062113584Ssimokawa			per packet mode */
2063120660Ssimokawa		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
2064113584Ssimokawa						vec[0].iov_len);
2065169131Ssimokawa		if (rb->xfer == NULL)
2066169131Ssimokawa			return;
2067120660Ssimokawa		fw_rcv_copy(rb)
2068103285Sikob		s = splfw();
2069103285Sikob		xferq->queued++;
2070120660Ssimokawa		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
2071103285Sikob		splx(s);
2072120660Ssimokawa		sc = device_get_softc(rb->fc->bdev);
2073103285Sikob		if (SEL_WAITING(&xferq->rsel))
2074122352Stanimura			selwakeuppri(&xferq->rsel, FWPRI);
2075103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
2076103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
2077103285Sikob			wakeup((caddr_t)xferq);
2078103285Sikob		}
2079103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
2080103285Sikob			xferq->hand(xferq);
2081103285Sikob		}
2082103285Sikob		return;
2083103285Sikob		break;
2084103285Sikob	}
2085120660Ssimokawa#endif
2086103285Sikob	default:
2087188722Ssbruno		device_printf(rb->fc->bdev,"%s: unknown tcode %d\n",
2088272214Skan		    __func__, tcode);
2089103285Sikob		break;
2090103285Sikob	}
2091103285Sikob}
2092106815Ssimokawa
2093103285Sikob/*
2094103285Sikob * Post process for Bus Manager election process.
2095103285Sikob */
2096103285Sikobstatic void
2097103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
2098103285Sikob{
2099103285Sikob	struct firewire_comm *fc;
2100109422Ssimokawa	int bmr;
2101103285Sikob
2102109422Ssimokawa	if (xfer == NULL)
2103109422Ssimokawa		return;
2104103285Sikob	fc = xfer->fc;
2105109422Ssimokawa	if (xfer->resp != 0)
2106103285Sikob		goto error;
2107120660Ssimokawa	if (xfer->recv.payload == NULL)
2108103285Sikob		goto error;
2109120660Ssimokawa	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2110103285Sikob		goto error;
2111109422Ssimokawa
2112120660Ssimokawa	bmr = ntohl(xfer->recv.payload[0]);
2113109422Ssimokawa	if (bmr == 0x3f)
2114109422Ssimokawa		bmr = fc->nodeid;
2115109422Ssimokawa
2116109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2117120660Ssimokawa	fw_xfer_free_buf(xfer);
2118114909Ssimokawa	fw_bmr(fc);
2119114909Ssimokawa	return;
2120114909Ssimokawa
2121103285Sikoberror:
2122114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
2123120660Ssimokawa	fw_xfer_free_buf(xfer);
2124103285Sikob}
2125106815Ssimokawa
2126113584Ssimokawa
2127103285Sikob/*
2128103285Sikob * To candidate Bus Manager election process.
2129103285Sikob */
2130110270Ssimokawastatic void
2131106815Ssimokawafw_try_bmr(void *arg)
2132103285Sikob{
2133103285Sikob	struct fw_xfer *xfer;
2134272214Skan	struct firewire_comm *fc = arg;
2135103285Sikob	struct fw_pkt *fp;
2136103285Sikob	int err = 0;
2137103285Sikob
2138120660Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2139272214Skan	if (xfer == NULL)
2140103285Sikob		return;
2141120660Ssimokawa	xfer->send.spd = 0;
2142103285Sikob	fc->status = FWBUSMGRELECT;
2143103285Sikob
2144120660Ssimokawa	fp = &xfer->send.hdr;
2145113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2146103285Sikob	fp->mode.lreq.tlrt = 0;
2147103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2148103285Sikob	fp->mode.lreq.pri = 0;
2149103285Sikob	fp->mode.lreq.src = 0;
2150113584Ssimokawa	fp->mode.lreq.len = 8;
2151120660Ssimokawa	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2152120660Ssimokawa	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2153113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2154120660Ssimokawa	xfer->send.payload[0] = htonl(0x3f);
2155120660Ssimokawa	xfer->send.payload[1] = htonl(fc->nodeid);
2156167632Ssimokawa	xfer->hand = fw_try_bmr_callback;
2157103285Sikob
2158103285Sikob	err = fw_asyreq(fc, -1, xfer);
2159272214Skan	if (err) {
2160120660Ssimokawa		fw_xfer_free_buf(xfer);
2161103285Sikob		return;
2162103285Sikob	}
2163103285Sikob	return;
2164103285Sikob}
2165106543Ssimokawa
2166106543Ssimokawa#ifdef FW_VMACCESS
2167103285Sikob/*
2168103285Sikob * Software implementation for physical memory block access.
2169272214Skan * XXX:Too slow, useful for debug purpose only.
2170103285Sikob */
2171106815Ssimokawastatic void
2172272214Skanfw_vmaccess(struct fw_xfer *xfer)
2173272214Skan{
2174103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2175129585Sdfr	uint32_t *ld = (uint32_t *)xfer->recv.buf;
2176103285Sikob
2177113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2178272214Skan	    xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]),
2179272214Skan	    ntohl(ld[3]));
2180272214Skan	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]),
2181272214Skan	    ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2182272214Skan	if (xfer->resp != 0) {
2183272214Skan		fw_xfer_free(xfer);
2184103285Sikob		return;
2185103285Sikob	}
2186272214Skan	if (xfer->recv.buf == NULL) {
2187272214Skan		fw_xfer_free(xfer);
2188103285Sikob		return;
2189103285Sikob	}
2190103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2191272214Skan	switch (rfp->mode.hdr.tcode) {
2192103285Sikob		/* XXX need fix for 64bit arch */
2193103285Sikob		case FWTCODE_WREQB:
2194110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2195103285Sikob			xfer->send.len = 12;
2196103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2197103285Sikob			bcopy(rfp->mode.wreqb.payload,
2198272214Skan			    (caddr_t)ntohl(rfp->mode.wreqb.dest_lo),s
2199272214Skan			    ntohs(rfp->mode.wreqb.len));
2200103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2201103285Sikob			sfp->mode.wres.rtcode = 0;
2202103285Sikob			break;
2203103285Sikob		case FWTCODE_WREQQ:
2204110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2205103285Sikob			xfer->send.len = 12;
2206103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2207272214Skan			*((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) =
2208272214Skan			    rfp->mode.wreqq.data;
2209103285Sikob			sfp->mode.wres.rtcode = 0;
2210103285Sikob			break;
2211103285Sikob		case FWTCODE_RREQB:
2212272214Skan			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len,
2213272214Skan			    M_FW, M_NOWAIT);
2214103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2215103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2216103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2217272214Skan			    sfp->mode.rresb.payload,
2218272214Skan			    ntohs(rfp->mode.rreqb.len));
2219103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2220103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2221103285Sikob			sfp->mode.rresb.rtcode = 0;
2222103285Sikob			sfp->mode.rresb.extcode = 0;
2223103285Sikob			break;
2224103285Sikob		case FWTCODE_RREQQ:
2225110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2226103285Sikob			xfer->send.len = 16;
2227103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2228272214Skan			sfp->mode.rresq.data =
2229272214Skan			    *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2230103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2231103285Sikob			sfp->mode.rresb.rtcode = 0;
2232103285Sikob			break;
2233103285Sikob		default:
2234272214Skan			fw_xfer_free(xfer);
2235103285Sikob			return;
2236103285Sikob	}
2237103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2238103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2239167632Ssimokawa	xfer->hand = fw_xfer_free;
2240103285Sikob
2241103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2242103285Sikob	sfp->mode.hdr.pri = 0;
2243103285Sikob
2244103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2245103285Sikob/**/
2246103285Sikob	return;
2247103285Sikob}
2248272214Skan#endif
2249106543Ssimokawa
2250103285Sikob/*
2251103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2252103285Sikob */
2253129585Sdfruint16_t
2254272214Skanfw_crc16(uint32_t *ptr, uint32_t len)
2255272214Skan{
2256129585Sdfr	uint32_t i, sum, crc = 0;
2257103285Sikob	int shift;
2258103285Sikob	len = (len + 3) & ~3;
2259272214Skan	for (i = 0; i < len; i += 4) {
2260272214Skan		for (shift = 28; shift >= 0; shift -= 4) {
2261103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2262272214Skan			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
2263103285Sikob		}
2264103285Sikob		crc &= 0xffff;
2265103285Sikob	}
2266272214Skan	return ((uint16_t) crc);
2267103285Sikob}
2268106815Ssimokawa
2269188722Ssbruno/*
2270188722Ssbruno * Find the root node, if it is not
2271188722Ssbruno * Cycle Master Capable, then we should
2272188722Ssbruno * override this and become the Cycle
2273188722Ssbruno * Master
2274188722Ssbruno */
2275110270Ssimokawastatic int
2276110072Ssimokawafw_bmr(struct firewire_comm *fc)
2277110072Ssimokawa{
2278110072Ssimokawa	struct fw_device fwdev;
2279113584Ssimokawa	union fw_self_id *self_id;
2280110072Ssimokawa	int cmstr;
2281129585Sdfr	uint32_t quad;
2282110072Ssimokawa
2283113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2284129274Sdfr	self_id = fw_find_self_id(fc, fc->max_node);
2285113584Ssimokawa	if (fc->max_node > 0) {
2286114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2287114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2288113584Ssimokawa			cmstr = fc->max_node;
2289114909Ssimokawa		else {
2290114909Ssimokawa			device_printf(fc->bdev,
2291272214Skan			    "root node is not cycle master capable\n");
2292114909Ssimokawa			/* XXX shall we be the cycle master? */
2293113584Ssimokawa			cmstr = fc->nodeid;
2294114909Ssimokawa			/* XXX need bus reset */
2295114909Ssimokawa		}
2296113584Ssimokawa	} else
2297113584Ssimokawa		cmstr = -1;
2298114909Ssimokawa
2299188722Ssbruno	device_printf(fc->bdev, "bus manager %d %s\n",
2300188722Ssbruno		CSRARC(fc, BUS_MGR_ID),
2301188722Ssbruno		(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) ? "(me)" : "");
2302272214Skan	if (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2303114909Ssimokawa		/* We are not the bus manager */
2304272214Skan		return (0);
2305114909Ssimokawa	}
2306114909Ssimokawa
2307114909Ssimokawa	/* Optimize gapcount */
2308272214Skan	if (fc->max_hop <= MAX_GAPHOP)
2309113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2310110072Ssimokawa	/* If we are the cycle master, nothing to do */
2311113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2312110072Ssimokawa		return 0;
2313110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2314110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2315110072Ssimokawa	fwdev.fc = fc;
2316110072Ssimokawa	fwdev.dst = cmstr;
2317110072Ssimokawa	fwdev.speed = 0;
2318110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2319110072Ssimokawa	fwdev.status = FWDEVINIT;
2320110072Ssimokawa	/* Set cmstr bit on the cycle master */
2321120660Ssimokawa	quad = htonl(1 << 8);
2322110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2323272214Skan	    0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2324110072Ssimokawa
2325110072Ssimokawa	return 0;
2326110072Ssimokawa}
2327110072Ssimokawa
2328170374Ssimokawaint
2329170374Ssimokawafw_open_isodma(struct firewire_comm *fc, int tx)
2330170374Ssimokawa{
2331170374Ssimokawa	struct fw_xferq **xferqa;
2332170374Ssimokawa	struct fw_xferq *xferq;
2333170374Ssimokawa	int i;
2334170374Ssimokawa
2335170374Ssimokawa	if (tx)
2336170374Ssimokawa		xferqa = &fc->it[0];
2337170374Ssimokawa	else
2338170374Ssimokawa		xferqa = &fc->ir[0];
2339170374Ssimokawa
2340170374Ssimokawa	FW_GLOCK(fc);
2341272214Skan	for (i = 0; i < fc->nisodma; i++) {
2342170374Ssimokawa		xferq = xferqa[i];
2343170374Ssimokawa		if ((xferq->flag & FWXFERQ_OPEN) == 0) {
2344170374Ssimokawa			xferq->flag |= FWXFERQ_OPEN;
2345170374Ssimokawa			break;
2346170374Ssimokawa		}
2347170374Ssimokawa	}
2348170374Ssimokawa	if (i == fc->nisodma) {
2349170374Ssimokawa		printf("no free dma channel (tx=%d)\n", tx);
2350170374Ssimokawa		i = -1;
2351170374Ssimokawa	}
2352170374Ssimokawa	FW_GUNLOCK(fc);
2353170374Ssimokawa	return (i);
2354170374Ssimokawa}
2355170374Ssimokawa
2356118455Ssimokawastatic int
2357118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2358118455Ssimokawa{
2359118455Ssimokawa	int err = 0;
2360118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2361118455Ssimokawa
2362118455Ssimokawa	switch (type) {
2363118455Ssimokawa	case MOD_LOAD:
2364118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2365272214Skan		    fwdev_clone, 0, 1000);
2366118455Ssimokawa		break;
2367118455Ssimokawa	case MOD_UNLOAD:
2368118455Ssimokawa		if (fwdev_ehtag != NULL)
2369118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2370118455Ssimokawa		break;
2371118455Ssimokawa	case MOD_SHUTDOWN:
2372118455Ssimokawa		break;
2373132199Sphk	default:
2374132199Sphk		return (EOPNOTSUPP);
2375118455Ssimokawa	}
2376118455Ssimokawa	return (err);
2377118455Ssimokawa}
2378118455Ssimokawa
2379118455Ssimokawa
2380272214SkanDRIVER_MODULE(firewire, fwohci, firewire_driver, firewire_devclass,
2381272214Skan    fw_modevent,0);
2382103285SikobMODULE_VERSION(firewire, 1);
2383