firewire.c revision 120850
1/*
2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the acknowledgement as bellow:
16 *
17 *    This product includes software developed by K. Kobayashi and H. Shimokawa
18 *
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/dev/firewire/firewire.c 120850 2003-10-06 07:17:43Z simokawa $
35 *
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/types.h>
41
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/conf.h>
45#include <sys/sysctl.h>
46
47#if __FreeBSD_version < 500000
48#include <machine/clock.h>	/* for DELAY() */
49#endif
50
51#include <sys/bus.h>		/* used by smbus and newbus */
52#include <machine/bus.h>
53
54#include <dev/firewire/firewire.h>
55#include <dev/firewire/firewirereg.h>
56#include <dev/firewire/fwmem.h>
57#include <dev/firewire/iec13213.h>
58#include <dev/firewire/iec68113.h>
59
60struct crom_src_buf {
61	struct crom_src	src;
62	struct crom_chunk root;
63	struct crom_chunk vendor;
64	struct crom_chunk hw;
65};
66
67int firewire_debug=0, try_bmr=1, hold_count=3;
68SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
69	"FireWire driver debug flag");
70SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
71SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
72	"Try to be a bus manager");
73SYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
74	"Number of count of bus resets for removing lost device information");
75
76MALLOC_DEFINE(M_FW, "firewire", "FireWire");
77MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
78
79#define FW_MAXASYRTY 4
80
81devclass_t firewire_devclass;
82
83static int firewire_match      __P((device_t));
84static int firewire_attach      __P((device_t));
85static int firewire_detach      __P((device_t));
86static int firewire_resume      __P((device_t));
87#if 0
88static int firewire_shutdown    __P((device_t));
89#endif
90static device_t firewire_add_child   __P((device_t, int, const char *, int));
91static void fw_try_bmr __P((void *));
92static void fw_try_bmr_callback __P((struct fw_xfer *));
93static void fw_asystart __P((struct fw_xfer *));
94static int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
95static void fw_bus_probe __P((struct firewire_comm *));
96static void fw_bus_explore __P((struct firewire_comm *));
97static void fw_bus_explore_callback __P((struct fw_xfer *));
98static void fw_attach_dev __P((struct firewire_comm *));
99#ifdef FW_VMACCESS
100static void fw_vmaccess __P((struct fw_xfer *));
101#endif
102struct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
103	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
104static int fw_bmr __P((struct firewire_comm *));
105
106static device_method_t firewire_methods[] = {
107	/* Device interface */
108	DEVMETHOD(device_probe,		firewire_match),
109	DEVMETHOD(device_attach,	firewire_attach),
110	DEVMETHOD(device_detach,	firewire_detach),
111	DEVMETHOD(device_suspend,	bus_generic_suspend),
112	DEVMETHOD(device_resume,	firewire_resume),
113	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
114
115	/* Bus interface */
116	DEVMETHOD(bus_add_child,	firewire_add_child),
117	DEVMETHOD(bus_print_child,	bus_generic_print_child),
118
119	{ 0, 0 }
120};
121char linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
122
123/* IEEE-1394a Table C-2 Gap count as a function of hops*/
124#define MAX_GAPHOP 15
125u_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
126		   21, 24, 26, 29, 32, 35, 37, 40};
127
128static driver_t firewire_driver = {
129	"firewire",
130	firewire_methods,
131	sizeof(struct firewire_softc),
132};
133
134/*
135 * Lookup fwdev by node id.
136 */
137struct fw_device *
138fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
139{
140	struct fw_device *fwdev;
141	int s;
142
143	s = splfw();
144	STAILQ_FOREACH(fwdev, &fc->devices, link)
145		if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
146			break;
147	splx(s);
148
149	return fwdev;
150}
151
152/*
153 * Lookup fwdev by EUI64.
154 */
155struct fw_device *
156fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
157{
158	struct fw_device *fwdev;
159	int s;
160
161	s = splfw();
162	STAILQ_FOREACH(fwdev, &fc->devices, link)
163		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
164			break;
165	splx(s);
166
167	if(fwdev == NULL) return NULL;
168	if(fwdev->status == FWDEVINVAL) return NULL;
169	return fwdev;
170}
171
172/*
173 * Async. request procedure for userland application.
174 */
175int
176fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
177{
178	int err = 0;
179	struct fw_xferq *xferq;
180	int tl = 0, len;
181	struct fw_pkt *fp;
182	int tcode;
183	struct tcode_info *info;
184
185	if(xfer == NULL) return EINVAL;
186	if(xfer->act.hand == NULL){
187		printf("act.hand == NULL\n");
188		return EINVAL;
189	}
190	fp = &xfer->send.hdr;
191
192	tcode = fp->mode.common.tcode & 0xf;
193	info = &fc->tcode[tcode];
194	if (info->flag == 0) {
195		printf("invalid tcode=%d\n", tcode);
196		return EINVAL;
197	}
198	if (info->flag & FWTI_REQ)
199		xferq = fc->atq;
200	else
201		xferq = fc->ats;
202	len = info->hdr_len;
203	if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
204		printf("send.pay_len > maxrec\n");
205		return EINVAL;
206	}
207	if (info->flag & FWTI_BLOCK_STR)
208		len = fp->mode.stream.len;
209	else if (info->flag & FWTI_BLOCK_ASY)
210		len = fp->mode.rresb.len;
211	else
212		len = 0;
213	if (len != xfer->send.pay_len){
214		printf("len(%d) != send.pay_len(%d) (tcode=%d)\n",
215				len, xfer->send.pay_len, tcode);
216		return EINVAL;
217	}
218
219	if(xferq->start == NULL){
220		printf("xferq->start == NULL\n");
221		return EINVAL;
222	}
223	if(!(xferq->queued < xferq->maxq)){
224		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
225			xferq->queued);
226		return EINVAL;
227	}
228
229	microtime(&xfer->tv);
230	if (info->flag & FWTI_TLABEL) {
231		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
232			return EIO;
233		fp->mode.hdr.tlrt = tl << 2;
234	}
235
236	xfer->tl = tl;
237	xfer->resp = 0;
238	xfer->fc = fc;
239	xfer->q = xferq;
240	xfer->retry_req = fw_asybusy;
241
242	fw_asystart(xfer);
243	return err;
244}
245/*
246 * Wakeup blocked process.
247 */
248void
249fw_asy_callback(struct fw_xfer *xfer){
250	wakeup(xfer);
251	return;
252}
253/*
254 * Postpone to later retry.
255 */
256void fw_asybusy(struct fw_xfer *xfer){
257	printf("fw_asybusy\n");
258/*
259	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
260*/
261	DELAY(20000);
262	fw_asystart(xfer);
263	return;
264}
265
266/*
267 * Async. request with given xfer structure.
268 */
269static void
270fw_asystart(struct fw_xfer *xfer)
271{
272	struct firewire_comm *fc = xfer->fc;
273	int s;
274	if(xfer->retry++ >= fc->max_asyretry){
275		device_printf(fc->bdev, "max_asyretry exceeded\n");
276		xfer->resp = EBUSY;
277		xfer->state = FWXF_BUSY;
278		xfer->act.hand(xfer);
279		return;
280	}
281#if 0 /* XXX allow bus explore packets only after bus rest */
282	if (fc->status < FWBUSEXPLORE) {
283		xfer->resp = EAGAIN;
284		xfer->state = FWXF_BUSY;
285		if (xfer->act.hand != NULL)
286			xfer->act.hand(xfer);
287		return;
288	}
289#endif
290	s = splfw();
291	xfer->state = FWXF_INQ;
292	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
293	xfer->q->queued ++;
294	splx(s);
295	/* XXX just queue for mbuf */
296	if (xfer->mbuf == NULL)
297		xfer->q->start(fc);
298	return;
299}
300
301static int
302firewire_match( device_t dev )
303{
304	device_set_desc(dev, "IEEE1394(FireWire) bus");
305	return -140;
306}
307
308static void
309firewire_xfer_timeout(struct firewire_comm *fc)
310{
311	struct fw_xfer *xfer;
312	struct tlabel *tl;
313	struct timeval tv;
314	struct timeval split_timeout;
315	int i, s;
316
317	split_timeout.tv_sec = 0;
318	split_timeout.tv_usec = 200 * 1000;	 /* 200 msec */
319
320	microtime(&tv);
321	timevalsub(&tv, &split_timeout);
322
323	s = splfw();
324	for (i = 0; i < 0x40; i ++) {
325		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
326			xfer = tl->xfer;
327			if (timevalcmp(&xfer->tv, &tv, >))
328				/* the rests are newer than this */
329				break;
330			device_printf(fc->bdev,
331				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
332				xfer->send.hdr.mode.hdr.dst, i, xfer->state);
333			xfer->resp = ETIMEDOUT;
334			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
335			fw_xfer_done(xfer);
336		}
337	}
338	splx(s);
339}
340
341static void
342firewire_watchdog(void *arg)
343{
344	struct firewire_comm *fc;
345
346	fc = (struct firewire_comm *)arg;
347	firewire_xfer_timeout(fc);
348	fc->timeout(fc);
349	callout_reset(&fc->timeout_callout, hz / 10,
350			(void *)firewire_watchdog, (void *)fc);
351}
352
353/*
354 * The attach routine.
355 */
356static int
357firewire_attach(device_t dev)
358{
359	int unit;
360	struct firewire_softc *sc = device_get_softc(dev);
361	device_t pa = device_get_parent(dev);
362	struct firewire_comm *fc;
363
364	fc = (struct firewire_comm *)device_get_softc(pa);
365	sc->fc = fc;
366	fc->status = FWBUSNOTREADY;
367
368	unit = device_get_unit(dev);
369	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
370
371	fwdev_makedev(sc);
372
373	CALLOUT_INIT(&sc->fc->timeout_callout);
374	CALLOUT_INIT(&sc->fc->bmr_callout);
375	CALLOUT_INIT(&sc->fc->retry_probe_callout);
376	CALLOUT_INIT(&sc->fc->busprobe_callout);
377
378	callout_reset(&sc->fc->timeout_callout, hz,
379			(void *)firewire_watchdog, (void *)sc->fc);
380
381	/* Locate our children */
382	bus_generic_probe(dev);
383
384	/* launch attachement of the added children */
385	bus_generic_attach(dev);
386
387	/* bus_reset */
388	fc->ibr(fc);
389
390	return 0;
391}
392
393/*
394 * Attach it as child.
395 */
396static device_t
397firewire_add_child(device_t dev, int order, const char *name, int unit)
398{
399        device_t child;
400	struct firewire_softc *sc;
401
402	sc = (struct firewire_softc *)device_get_softc(dev);
403	child = device_add_child(dev, name, unit);
404	if (child) {
405		device_set_ivars(child, sc->fc);
406		device_probe_and_attach(child);
407	}
408
409	return child;
410}
411
412static int
413firewire_resume(device_t dev)
414{
415	struct firewire_softc *sc;
416
417	sc = (struct firewire_softc *)device_get_softc(dev);
418	sc->fc->status = FWBUSNOTREADY;
419
420	bus_generic_resume(dev);
421
422	return(0);
423}
424
425/*
426 * Dettach it.
427 */
428static int
429firewire_detach(device_t dev)
430{
431	struct firewire_softc *sc;
432	struct csrdir *csrd, *next;
433	struct fw_device *fwdev, *fwdev_next;
434	int err;
435
436	sc = (struct firewire_softc *)device_get_softc(dev);
437	if ((err = fwdev_destroydev(sc)) != 0)
438		return err;
439
440	if ((err = bus_generic_detach(dev)) != 0)
441		return err;
442
443	callout_stop(&sc->fc->timeout_callout);
444	callout_stop(&sc->fc->bmr_callout);
445	callout_stop(&sc->fc->retry_probe_callout);
446	callout_stop(&sc->fc->busprobe_callout);
447
448	/* XXX xfree_free and untimeout on all xfers */
449	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
450							fwdev = fwdev_next) {
451		fwdev_next = STAILQ_NEXT(fwdev, link);
452		free(fwdev, M_FW);
453	}
454	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
455		next = SLIST_NEXT(csrd, link);
456		free(csrd, M_FW);
457	}
458	free(sc->fc->topology_map, M_FW);
459	free(sc->fc->speed_map, M_FW);
460	free(sc->fc->crom_src_buf, M_FW);
461	return(0);
462}
463#if 0
464static int
465firewire_shutdown( device_t dev )
466{
467	return 0;
468}
469#endif
470
471
472static void
473fw_xferq_drain(struct fw_xferq *xferq)
474{
475	struct fw_xfer *xfer;
476
477	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
478		STAILQ_REMOVE_HEAD(&xferq->q, link);
479		xferq->queued --;
480		xfer->resp = EAGAIN;
481		fw_xfer_done(xfer);
482	}
483}
484
485void
486fw_drain_txq(struct firewire_comm *fc)
487{
488	int i;
489
490	fw_xferq_drain(fc->atq);
491	fw_xferq_drain(fc->ats);
492	for(i = 0; i < fc->nisodma; i++)
493		fw_xferq_drain(fc->it[i]);
494}
495
496static void
497fw_reset_csr(struct firewire_comm *fc)
498{
499	int i;
500
501	CSRARC(fc, STATE_CLEAR)
502			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
503	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
504	CSRARC(fc, NODE_IDS) = 0x3f;
505
506	CSRARC(fc, TOPO_MAP + 8) = 0;
507	fc->irm = -1;
508
509	fc->max_node = -1;
510
511	for(i = 2; i < 0x100/4 - 2 ; i++){
512		CSRARC(fc, SPED_MAP + i * 4) = 0;
513	}
514	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
515	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
516	CSRARC(fc, RESET_START) = 0;
517	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
518	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
519	CSRARC(fc, CYCLE_TIME) = 0x0;
520	CSRARC(fc, BUS_TIME) = 0x0;
521	CSRARC(fc, BUS_MGR_ID) = 0x3f;
522	CSRARC(fc, BANDWIDTH_AV) = 4915;
523	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
524	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
525	CSRARC(fc, IP_CHANNELS) = (1 << 31);
526
527	CSRARC(fc, CONF_ROM) = 0x04 << 24;
528	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
529	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
530				1 << 28 | 0xff << 16 | 0x09 << 8;
531	CSRARC(fc, CONF_ROM + 0xc) = 0;
532
533/* DV depend CSRs see blue book */
534	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
535	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
536
537	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
538	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
539}
540
541static void
542fw_init_crom(struct firewire_comm *fc)
543{
544	struct crom_src *src;
545
546	fc->crom_src_buf = (struct crom_src_buf *)
547		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
548	if (fc->crom_src_buf == NULL)
549		return;
550
551	src = &fc->crom_src_buf->src;
552	bzero(src, sizeof(struct crom_src));
553
554	/* BUS info sample */
555	src->hdr.info_len = 4;
556
557	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
558
559	src->businfo.irmc = 1;
560	src->businfo.cmc = 1;
561	src->businfo.isc = 1;
562	src->businfo.bmc = 1;
563	src->businfo.pmc = 0;
564	src->businfo.cyc_clk_acc = 100;
565	src->businfo.max_rec = fc->maxrec;
566	src->businfo.max_rom = MAXROM_4;
567	src->businfo.generation = 1;
568	src->businfo.link_spd = fc->speed;
569
570	src->businfo.eui64.hi = fc->eui.hi;
571	src->businfo.eui64.lo = fc->eui.lo;
572
573	STAILQ_INIT(&src->chunk_list);
574
575	fc->crom_src = src;
576	fc->crom_root = &fc->crom_src_buf->root;
577}
578
579static void
580fw_reset_crom(struct firewire_comm *fc)
581{
582	struct crom_src_buf *buf;
583	struct crom_src *src;
584	struct crom_chunk *root;
585
586	if (fc->crom_src_buf == NULL)
587		fw_init_crom(fc);
588
589	buf =  fc->crom_src_buf;
590	src = fc->crom_src;
591	root = fc->crom_root;
592
593	STAILQ_INIT(&src->chunk_list);
594
595	bzero(root, sizeof(struct crom_chunk));
596	crom_add_chunk(src, NULL, root, 0);
597	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
598	/* private company_id */
599	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
600	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
601	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
602	crom_add_simple_text(src, root, &buf->hw, hostname);
603}
604
605/*
606 * Called after bus reset.
607 */
608void
609fw_busreset(struct firewire_comm *fc)
610{
611	struct firewire_dev_comm *fdc;
612	struct crom_src *src;
613	device_t *devlistp;
614	void *newrom;
615	int i, devcnt;
616
617	switch(fc->status){
618	case FWBUSMGRELECT:
619		callout_stop(&fc->bmr_callout);
620		break;
621	default:
622		break;
623	}
624	fc->status = FWBUSRESET;
625	fw_reset_csr(fc);
626	fw_reset_crom(fc);
627
628	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
629		for( i = 0 ; i < devcnt ; i++)
630			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
631				fdc = device_get_softc(devlistp[i]);
632				if (fdc->post_busreset != NULL)
633					fdc->post_busreset(fdc);
634			}
635		free(devlistp, M_TEMP);
636	}
637
638	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
639	src = &fc->crom_src_buf->src;
640	crom_load(src, (u_int32_t *)newrom, CROMSIZE);
641	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
642		/* bump generation and reload */
643		src->businfo.generation ++;
644		/* generation must be between 0x2 and 0xF */
645		if (src->businfo.generation < 2)
646			src->businfo.generation ++;
647		crom_load(src, (u_int32_t *)newrom, CROMSIZE);
648		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
649	}
650	free(newrom, M_FW);
651}
652
653/* Call once after reboot */
654void fw_init(struct firewire_comm *fc)
655{
656	int i;
657	struct csrdir *csrd;
658#ifdef FW_VMACCESS
659	struct fw_xfer *xfer;
660	struct fw_bind *fwb;
661#endif
662
663	fc->max_asyretry = FW_MAXASYRTY;
664
665	fc->arq->queued = 0;
666	fc->ars->queued = 0;
667	fc->atq->queued = 0;
668	fc->ats->queued = 0;
669
670	fc->arq->buf = NULL;
671	fc->ars->buf = NULL;
672	fc->atq->buf = NULL;
673	fc->ats->buf = NULL;
674
675	fc->arq->flag = 0;
676	fc->ars->flag = 0;
677	fc->atq->flag = 0;
678	fc->ats->flag = 0;
679
680	STAILQ_INIT(&fc->atq->q);
681	STAILQ_INIT(&fc->ats->q);
682
683	for( i = 0 ; i < fc->nisodma ; i ++ ){
684		fc->it[i]->queued = 0;
685		fc->ir[i]->queued = 0;
686
687		fc->it[i]->start = NULL;
688		fc->ir[i]->start = NULL;
689
690		fc->it[i]->buf = NULL;
691		fc->ir[i]->buf = NULL;
692
693		fc->it[i]->flag = FWXFERQ_STREAM;
694		fc->ir[i]->flag = FWXFERQ_STREAM;
695
696		STAILQ_INIT(&fc->it[i]->q);
697		STAILQ_INIT(&fc->ir[i]->q);
698
699		STAILQ_INIT(&fc->it[i]->binds);
700		STAILQ_INIT(&fc->ir[i]->binds);
701	}
702
703	fc->arq->maxq = FWMAXQUEUE;
704	fc->ars->maxq = FWMAXQUEUE;
705	fc->atq->maxq = FWMAXQUEUE;
706	fc->ats->maxq = FWMAXQUEUE;
707
708	for( i = 0 ; i < fc->nisodma ; i++){
709		fc->ir[i]->maxq = FWMAXQUEUE;
710		fc->it[i]->maxq = FWMAXQUEUE;
711	}
712/* Initialize csr registers */
713	fc->topology_map = (struct fw_topology_map *)malloc(
714				sizeof(struct fw_topology_map),
715				M_FW, M_NOWAIT | M_ZERO);
716	fc->speed_map = (struct fw_speed_map *)malloc(
717				sizeof(struct fw_speed_map),
718				M_FW, M_NOWAIT | M_ZERO);
719	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
720	CSRARC(fc, TOPO_MAP + 4) = 1;
721	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
722	CSRARC(fc, SPED_MAP + 4) = 1;
723
724	STAILQ_INIT(&fc->devices);
725	STAILQ_INIT(&fc->pending);
726
727/* Initialize csr ROM work space */
728	SLIST_INIT(&fc->ongocsr);
729	SLIST_INIT(&fc->csrfree);
730	for( i = 0 ; i < FWMAXCSRDIR ; i++){
731		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
732		if(csrd == NULL) break;
733		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
734	}
735
736/* Initialize Async handlers */
737	STAILQ_INIT(&fc->binds);
738	for( i = 0 ; i < 0x40 ; i++){
739		STAILQ_INIT(&fc->tlabels[i]);
740	}
741
742/* DV depend CSRs see blue book */
743#if 0
744	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
745	CSRARC(fc, oPCR) = 0x8000007a;
746	for(i = 4 ; i < 0x7c/4 ; i+=4){
747		CSRARC(fc, i + oPCR) = 0x8000007a;
748	}
749
750	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
751	CSRARC(fc, iPCR) = 0x803f0000;
752	for(i = 4 ; i < 0x7c/4 ; i+=4){
753		CSRARC(fc, i + iPCR) = 0x0;
754	}
755#endif
756
757	fc->crom_src_buf = NULL;
758
759#ifdef FW_VMACCESS
760	xfer = fw_xfer_alloc();
761	if(xfer == NULL) return;
762
763	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
764	if(fwb == NULL){
765		fw_xfer_free(xfer);
766	}
767	xfer->act.hand = fw_vmaccess;
768	xfer->fc = fc;
769	xfer->sc = NULL;
770
771	fwb->start_hi = 0x2;
772	fwb->start_lo = 0;
773	fwb->addrlen = 0xffffffff;
774	fwb->xfer = xfer;
775	fw_bindadd(fc, fwb);
776#endif
777}
778
779#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
780    ((fwb)->end < (addr))?1:0)
781
782/*
783 * To lookup binded process from IEEE1394 address.
784 */
785struct fw_bind *
786fw_bindlookup(struct firewire_comm *fc, u_int16_t dest_hi, u_int32_t dest_lo)
787{
788	u_int64_t addr;
789	struct fw_bind *tfw;
790
791	addr = ((u_int64_t)dest_hi << 32) | dest_lo;
792	STAILQ_FOREACH(tfw, &fc->binds, fclist)
793		if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0)
794			return(tfw);
795	return(NULL);
796}
797
798/*
799 * To bind IEEE1394 address block to process.
800 */
801int
802fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
803{
804	struct fw_bind *tfw, *prev = NULL;
805
806	if (fwb->start > fwb->end) {
807		printf("%s: invalid range\n", __FUNCTION__);
808		return EINVAL;
809	}
810
811	STAILQ_FOREACH(tfw, &fc->binds, fclist) {
812		if (fwb->end < tfw->start)
813			break;
814		prev = tfw;
815	}
816	if (prev == NULL) {
817		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
818		goto out;
819	}
820	if (prev->end < fwb->start) {
821		STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
822		goto out;
823	}
824
825	printf("%s: bind failed\n", __FUNCTION__);
826	return (EBUSY);
827
828out:
829	if (fwb->act_type == FWACT_CH)
830		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
831	return (0);
832}
833
834/*
835 * To free IEEE1394 address block.
836 */
837int
838fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
839{
840#if 0
841	struct fw_xfer *xfer, *next;
842#endif
843	struct fw_bind *tfw;
844	int s;
845
846	s = splfw();
847	STAILQ_FOREACH(tfw, &fc->binds, fclist)
848		if (tfw == fwb) {
849			STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
850			goto found;
851		}
852
853	printf("%s: no such bind\n", __FUNCTION__);
854	splx(s);
855	return (1);
856found:
857#if 0
858	/* shall we do this? */
859	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
860		next = STAILQ_NEXT(xfer, link);
861		fw_xfer_free(xfer);
862	}
863	STAILQ_INIT(&fwb->xferlist);
864#endif
865
866	splx(s);
867	return 0;
868}
869
870/*
871 * To free transaction label.
872 */
873static void
874fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
875{
876	struct tlabel *tl;
877	int s = splfw();
878
879	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
880		tl = STAILQ_NEXT(tl, link)){
881		if(tl->xfer == xfer){
882			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
883			free(tl, M_FW);
884			splx(s);
885			return;
886		}
887	}
888	splx(s);
889	return;
890}
891
892/*
893 * To obtain XFER structure by transaction label.
894 */
895static struct fw_xfer *
896fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
897{
898	struct fw_xfer *xfer;
899	struct tlabel *tl;
900	int s = splfw();
901
902	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
903		tl = STAILQ_NEXT(tl, link)){
904		if(tl->xfer->send.hdr.mode.hdr.dst == node){
905			xfer = tl->xfer;
906			splx(s);
907			if (firewire_debug > 2)
908				printf("fw_tl2xfer: found tl=%d\n", tlabel);
909			return(xfer);
910		}
911	}
912	if (firewire_debug > 1)
913		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
914	splx(s);
915	return(NULL);
916}
917
918/*
919 * To allocate IEEE1394 XFER structure.
920 */
921struct fw_xfer *
922fw_xfer_alloc(struct malloc_type *type)
923{
924	struct fw_xfer *xfer;
925
926	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
927	if (xfer == NULL)
928		return xfer;
929
930	xfer->malloc = type;
931
932	return xfer;
933}
934
935struct fw_xfer *
936fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
937{
938	struct fw_xfer *xfer;
939
940	xfer = fw_xfer_alloc(type);
941	xfer->send.pay_len = send_len;
942	xfer->recv.pay_len = recv_len;
943	if (xfer == NULL)
944		return(NULL);
945	if (send_len > 0) {
946		xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
947		if (xfer->send.payload == NULL) {
948			fw_xfer_free(xfer);
949			return(NULL);
950		}
951	}
952	if (recv_len > 0) {
953		xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
954		if (xfer->recv.payload == NULL) {
955			if (xfer->send.payload != NULL)
956				free(xfer->send.payload, type);
957			fw_xfer_free(xfer);
958			return(NULL);
959		}
960	}
961	return(xfer);
962}
963
964/*
965 * IEEE1394 XFER post process.
966 */
967void
968fw_xfer_done(struct fw_xfer *xfer)
969{
970	if (xfer->act.hand == NULL) {
971		printf("act.hand == NULL\n");
972		return;
973	}
974
975	if (xfer->fc->status != FWBUSRESET)
976		xfer->act.hand(xfer);
977	else {
978		printf("fw_xfer_done: pending\n");
979		if (xfer->fc != NULL)
980			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
981		else
982			panic("fw_xfer_done: why xfer->fc is NULL?");
983	}
984}
985
986void
987fw_xfer_unload(struct fw_xfer* xfer)
988{
989	int s;
990
991	if(xfer == NULL ) return;
992	if(xfer->state == FWXF_INQ){
993		printf("fw_xfer_free FWXF_INQ\n");
994		s = splfw();
995		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
996		xfer->q->queued --;
997		splx(s);
998	}
999	if (xfer->fc != NULL) {
1000#if 1
1001		if(xfer->state == FWXF_START)
1002			/*
1003			 * This could happen if:
1004			 *  1. We call fwohci_arcv() before fwohci_txd().
1005			 *  2. firewire_watch() is called.
1006			 */
1007			printf("fw_xfer_free FWXF_START\n");
1008#endif
1009		fw_tl_free(xfer->fc, xfer);
1010	}
1011	xfer->state = FWXF_INIT;
1012	xfer->resp = 0;
1013	xfer->retry = 0;
1014}
1015/*
1016 * To free IEEE1394 XFER structure.
1017 */
1018void
1019fw_xfer_free_buf( struct fw_xfer* xfer)
1020{
1021	if (xfer == NULL) {
1022		printf("%s: xfer == NULL\n", __FUNCTION__);
1023		return;
1024	}
1025	fw_xfer_unload(xfer);
1026	if(xfer->send.payload != NULL){
1027		free(xfer->send.payload, xfer->malloc);
1028	}
1029	if(xfer->recv.payload != NULL){
1030		free(xfer->recv.payload, xfer->malloc);
1031	}
1032	free(xfer, xfer->malloc);
1033}
1034
1035void
1036fw_xfer_free( struct fw_xfer* xfer)
1037{
1038	if (xfer == NULL) {
1039		printf("%s: xfer == NULL\n", __FUNCTION__);
1040		return;
1041	}
1042	fw_xfer_unload(xfer);
1043	free(xfer, xfer->malloc);
1044}
1045
1046void
1047fw_asy_callback_free(struct fw_xfer *xfer)
1048{
1049#if 0
1050	printf("asyreq done state=%d resp=%d\n",
1051				xfer->state, xfer->resp);
1052#endif
1053	fw_xfer_free(xfer);
1054}
1055
1056/*
1057 * To configure PHY.
1058 */
1059static void
1060fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1061{
1062	struct fw_xfer *xfer;
1063	struct fw_pkt *fp;
1064
1065	fc->status = FWBUSPHYCONF;
1066
1067	xfer = fw_xfer_alloc(M_FWXFER);
1068	if (xfer == NULL)
1069		return;
1070	xfer->fc = fc;
1071	xfer->retry_req = fw_asybusy;
1072	xfer->act.hand = fw_asy_callback_free;
1073
1074	fp = &xfer->send.hdr;
1075	fp->mode.ld[1] = 0;
1076	if (root_node >= 0)
1077		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1078	if (gap_count >= 0)
1079		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1080	fp->mode.ld[2] = ~fp->mode.ld[1];
1081/* XXX Dangerous, how to pass PHY packet to device driver */
1082	fp->mode.common.tcode |= FWTCODE_PHY;
1083
1084	if (firewire_debug)
1085		printf("send phy_config root_node=%d gap_count=%d\n",
1086						root_node, gap_count);
1087	fw_asyreq(fc, -1, xfer);
1088}
1089
1090#if 0
1091/*
1092 * Dump self ID.
1093 */
1094static void
1095fw_print_sid(u_int32_t sid)
1096{
1097	union fw_self_id *s;
1098	s = (union fw_self_id *) &sid;
1099	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1100		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1101		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1102		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1103		s->p0.power_class, s->p0.port0, s->p0.port1,
1104		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1105}
1106#endif
1107
1108/*
1109 * To receive self ID.
1110 */
1111void fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len)
1112{
1113	u_int32_t *p;
1114	union fw_self_id *self_id;
1115	u_int i, j, node, c_port = 0, i_branch = 0;
1116
1117	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1118	fc->status = FWBUSINIT;
1119	fc->max_node = fc->nodeid & 0x3f;
1120	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1121	fc->status = FWBUSCYMELECT;
1122	fc->topology_map->crc_len = 2;
1123	fc->topology_map->generation ++;
1124	fc->topology_map->self_id_count = 0;
1125	fc->topology_map->node_count = 0;
1126	fc->speed_map->generation ++;
1127	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1128	self_id = &fc->topology_map->self_id[0];
1129	for(i = 0; i < fc->sid_cnt; i ++){
1130		if (sid[1] != ~sid[0]) {
1131			printf("fw_sidrcv: invalid self-id packet\n");
1132			sid += 2;
1133			continue;
1134		}
1135		*self_id = *((union fw_self_id *)sid);
1136		fc->topology_map->crc_len++;
1137		if(self_id->p0.sequel == 0){
1138			fc->topology_map->node_count ++;
1139			c_port = 0;
1140#if 0
1141			fw_print_sid(sid[0]);
1142#endif
1143			node = self_id->p0.phy_id;
1144			if(fc->max_node < node){
1145				fc->max_node = self_id->p0.phy_id;
1146			}
1147			/* XXX I'm not sure this is the right speed_map */
1148			fc->speed_map->speed[node][node]
1149					= self_id->p0.phy_speed;
1150			for (j = 0; j < node; j ++) {
1151				fc->speed_map->speed[j][node]
1152					= fc->speed_map->speed[node][j]
1153					= min(fc->speed_map->speed[j][j],
1154							self_id->p0.phy_speed);
1155			}
1156			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1157			  (self_id->p0.link_active && self_id->p0.contender)) {
1158				fc->irm = self_id->p0.phy_id;
1159			}
1160			if(self_id->p0.port0 >= 0x2){
1161				c_port++;
1162			}
1163			if(self_id->p0.port1 >= 0x2){
1164				c_port++;
1165			}
1166			if(self_id->p0.port2 >= 0x2){
1167				c_port++;
1168			}
1169		}
1170		if(c_port > 2){
1171			i_branch += (c_port - 2);
1172		}
1173		sid += 2;
1174		self_id++;
1175		fc->topology_map->self_id_count ++;
1176	}
1177	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1178	/* CRC */
1179	fc->topology_map->crc = fw_crc16(
1180			(u_int32_t *)&fc->topology_map->generation,
1181			fc->topology_map->crc_len * 4);
1182	fc->speed_map->crc = fw_crc16(
1183			(u_int32_t *)&fc->speed_map->generation,
1184			fc->speed_map->crc_len * 4);
1185	/* byteswap and copy to CSR */
1186	p = (u_int32_t *)fc->topology_map;
1187	for (i = 0; i <= fc->topology_map->crc_len; i++)
1188		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1189	p = (u_int32_t *)fc->speed_map;
1190	CSRARC(fc, SPED_MAP) = htonl(*p++);
1191	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1192	/* don't byte-swap u_int8_t array */
1193	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1194
1195	fc->max_hop = fc->max_node - i_branch;
1196	printf(", maxhop <= %d", fc->max_hop);
1197
1198	if(fc->irm == -1 ){
1199		printf(", Not found IRM capable node");
1200	}else{
1201		printf(", cable IRM = %d", fc->irm);
1202		if (fc->irm == fc->nodeid)
1203			printf(" (me)");
1204	}
1205	printf("\n");
1206
1207	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1208		if (fc->irm == fc->nodeid) {
1209			fc->status = FWBUSMGRDONE;
1210			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1211			fw_bmr(fc);
1212		} else {
1213			fc->status = FWBUSMGRELECT;
1214			callout_reset(&fc->bmr_callout, hz/8,
1215				(void *)fw_try_bmr, (void *)fc);
1216		}
1217	} else
1218		fc->status = FWBUSMGRDONE;
1219
1220	callout_reset(&fc->busprobe_callout, hz/4,
1221			(void *)fw_bus_probe, (void *)fc);
1222}
1223
1224/*
1225 * To probe devices on the IEEE1394 bus.
1226 */
1227static void
1228fw_bus_probe(struct firewire_comm *fc)
1229{
1230	int s;
1231	struct fw_device *fwdev;
1232
1233	s = splfw();
1234	fc->status = FWBUSEXPLORE;
1235	fc->retry_count = 0;
1236
1237	/* Invalidate all devices, just after bus reset. */
1238	STAILQ_FOREACH(fwdev, &fc->devices, link)
1239		if (fwdev->status != FWDEVINVAL) {
1240			fwdev->status = FWDEVINVAL;
1241			fwdev->rcnt = 0;
1242		}
1243
1244	fc->ongonode = 0;
1245	fc->ongoaddr = CSRROMOFF;
1246	fc->ongodev = NULL;
1247	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1248	fw_bus_explore(fc);
1249	splx(s);
1250}
1251
1252/*
1253 * To collect device informations on the IEEE1394 bus.
1254 */
1255static void
1256fw_bus_explore(struct firewire_comm *fc )
1257{
1258	int err = 0;
1259	struct fw_device *fwdev, *pfwdev, *tfwdev;
1260	u_int32_t addr;
1261	struct fw_xfer *xfer;
1262	struct fw_pkt *fp;
1263
1264	if(fc->status != FWBUSEXPLORE)
1265		return;
1266
1267loop:
1268	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1269
1270	if(fc->ongonode > fc->max_node) goto done;
1271	if(fc->ongonode >= 0x3f) goto done;
1272
1273	/* check link */
1274	/* XXX we need to check phy_id first */
1275	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1276		if (firewire_debug)
1277			printf("node%d: link down\n", fc->ongonode);
1278		fc->ongonode++;
1279		goto loop;
1280	}
1281
1282	if(fc->ongoaddr <= CSRROMOFF &&
1283		fc->ongoeui.hi == 0xffffffff &&
1284		fc->ongoeui.lo == 0xffffffff ){
1285		fc->ongoaddr = CSRROMOFF;
1286		addr = 0xf0000000 | fc->ongoaddr;
1287	}else if(fc->ongoeui.hi == 0xffffffff ){
1288		fc->ongoaddr = CSRROMOFF + 0xc;
1289		addr = 0xf0000000 | fc->ongoaddr;
1290	}else if(fc->ongoeui.lo == 0xffffffff ){
1291		fc->ongoaddr = CSRROMOFF + 0x10;
1292		addr = 0xf0000000 | fc->ongoaddr;
1293	}else if(fc->ongodev == NULL){
1294		STAILQ_FOREACH(fwdev, &fc->devices, link)
1295			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1296				break;
1297		if(fwdev != NULL){
1298			fwdev->dst = fc->ongonode;
1299			fwdev->status = FWDEVINIT;
1300			fc->ongodev = fwdev;
1301			fc->ongoaddr = CSRROMOFF;
1302			addr = 0xf0000000 | fc->ongoaddr;
1303			goto dorequest;
1304		}
1305		fwdev = malloc(sizeof(struct fw_device), M_FW,
1306							M_NOWAIT | M_ZERO);
1307		if(fwdev == NULL)
1308			return;
1309		fwdev->fc = fc;
1310		fwdev->rommax = 0;
1311		fwdev->dst = fc->ongonode;
1312		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1313		fwdev->status = FWDEVINIT;
1314		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1315
1316		pfwdev = NULL;
1317		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1318			if (tfwdev->eui.hi > fwdev->eui.hi ||
1319					(tfwdev->eui.hi == fwdev->eui.hi &&
1320					tfwdev->eui.lo > fwdev->eui.lo))
1321				break;
1322			pfwdev = tfwdev;
1323		}
1324		if (pfwdev == NULL)
1325			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1326		else
1327			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1328
1329		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1330			linkspeed[fwdev->speed],
1331			fc->ongoeui.hi, fc->ongoeui.lo);
1332
1333		fc->ongodev = fwdev;
1334		fc->ongoaddr = CSRROMOFF;
1335		addr = 0xf0000000 | fc->ongoaddr;
1336	}else{
1337		addr = 0xf0000000 | fc->ongoaddr;
1338	}
1339dorequest:
1340#if 0
1341	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1342		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1343		fw_bus_explore_callback);
1344	if(xfer == NULL) goto done;
1345#else
1346	xfer = fw_xfer_alloc(M_FWXFER);
1347	if(xfer == NULL){
1348		goto done;
1349	}
1350	xfer->send.spd = 0;
1351	fp = &xfer->send.hdr;
1352	fp->mode.rreqq.dest_hi = 0xffff;
1353	fp->mode.rreqq.tlrt = 0;
1354	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1355	fp->mode.rreqq.pri = 0;
1356	fp->mode.rreqq.src = 0;
1357	fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode;
1358	fp->mode.rreqq.dest_lo = addr;
1359	xfer->act.hand = fw_bus_explore_callback;
1360
1361	if (firewire_debug)
1362		printf("node%d: explore addr=0x%x\n",
1363				fc->ongonode, fc->ongoaddr);
1364	err = fw_asyreq(fc, -1, xfer);
1365	if(err){
1366		fw_xfer_free( xfer);
1367		return;
1368	}
1369#endif
1370	return;
1371done:
1372	/* fw_attach_devs */
1373	fc->status = FWBUSEXPDONE;
1374	if (firewire_debug)
1375		printf("bus_explore done\n");
1376	fw_attach_dev(fc);
1377	return;
1378
1379}
1380
1381/* Portable Async. request read quad */
1382struct fw_xfer *
1383asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1384	u_int32_t addr_hi, u_int32_t addr_lo,
1385	void (*hand) __P((struct fw_xfer*)))
1386{
1387	struct fw_xfer *xfer;
1388	struct fw_pkt *fp;
1389	int err;
1390
1391	xfer = fw_xfer_alloc(M_FWXFER);
1392	if (xfer == NULL)
1393		return NULL;
1394
1395	xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */
1396	fp = &xfer->send.hdr;
1397	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1398	if(tl & FWP_TL_VALID){
1399		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1400	}else{
1401		fp->mode.rreqq.tlrt = 0;
1402	}
1403	fp->mode.rreqq.tlrt |= rt & 0x3;
1404	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1405	fp->mode.rreqq.pri = 0;
1406	fp->mode.rreqq.src = 0;
1407	fp->mode.rreqq.dst = addr_hi >> 16;
1408	fp->mode.rreqq.dest_lo = addr_lo;
1409	xfer->act.hand = hand;
1410
1411	err = fw_asyreq(fc, -1, xfer);
1412	if(err){
1413		fw_xfer_free( xfer);
1414		return NULL;
1415	}
1416	return xfer;
1417}
1418
1419/*
1420 * Callback for the IEEE1394 bus information collection.
1421 */
1422static void
1423fw_bus_explore_callback(struct fw_xfer *xfer)
1424{
1425	struct firewire_comm *fc;
1426	struct fw_pkt *sfp,*rfp;
1427	struct csrhdr *chdr;
1428	struct csrdir *csrd;
1429	struct csrreg *csrreg;
1430	u_int32_t offset;
1431
1432
1433	if(xfer == NULL) {
1434		printf("xfer == NULL\n");
1435		return;
1436	}
1437	fc = xfer->fc;
1438
1439	if (firewire_debug)
1440		printf("node%d: callback addr=0x%x\n",
1441			fc->ongonode, fc->ongoaddr);
1442
1443	if(xfer->resp != 0){
1444		printf("node%d: resp=%d addr=0x%x\n",
1445			fc->ongonode, xfer->resp, fc->ongoaddr);
1446		goto errnode;
1447	}
1448
1449	sfp = &xfer->send.hdr;
1450	rfp = &xfer->recv.hdr;
1451#if 0
1452	{
1453		u_int32_t *qld;
1454		int i;
1455		qld = (u_int32_t *)xfer->recv.buf;
1456		printf("len:%d\n", xfer->recv.len);
1457		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1458			printf("0x%08x ", rfp->mode.ld[i/4]);
1459			if((i % 16) == 15) printf("\n");
1460		}
1461		if((i % 16) != 15) printf("\n");
1462	}
1463#endif
1464	if(fc->ongodev == NULL){
1465		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1466			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1467			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1468/* If CSR is minimal confinguration, more investgation is not needed. */
1469			if(chdr->info_len == 1){
1470				if (firewire_debug)
1471					printf("node%d: minimal config\n",
1472								fc->ongonode);
1473				goto nextnode;
1474			}else{
1475				fc->ongoaddr = CSRROMOFF + 0xc;
1476			}
1477		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1478			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1479			fc->ongoaddr = CSRROMOFF + 0x10;
1480		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1481			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1482			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1483				if (firewire_debug)
1484					printf("node%d: eui64 is zero.\n",
1485							fc->ongonode);
1486				goto nextnode;
1487			}
1488			fc->ongoaddr = CSRROMOFF;
1489		}
1490	}else{
1491		if (fc->ongoaddr == CSRROMOFF &&
1492		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1493			fc->ongodev->status = FWDEVATTACHED;
1494			goto nextnode;
1495		}
1496		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1497		if(fc->ongoaddr > fc->ongodev->rommax){
1498			fc->ongodev->rommax = fc->ongoaddr;
1499		}
1500		csrd = SLIST_FIRST(&fc->ongocsr);
1501		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1502			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1503			offset = CSRROMOFF;
1504		}else{
1505			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1506			offset = csrd->off;
1507		}
1508		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1509			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1510			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1511				csrd = SLIST_FIRST(&fc->csrfree);
1512				if(csrd == NULL){
1513					goto nextnode;
1514				}else{
1515					csrd->ongoaddr = fc->ongoaddr;
1516					fc->ongoaddr += csrreg->val * 4;
1517					csrd->off = fc->ongoaddr;
1518					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1519					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1520					goto nextaddr;
1521				}
1522			}
1523		}
1524		fc->ongoaddr += 4;
1525		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1526				(fc->ongodev->rommax < 0x414)){
1527			if(fc->ongodev->rommax <= 0x414){
1528				csrd = SLIST_FIRST(&fc->csrfree);
1529				if(csrd == NULL) goto nextnode;
1530				csrd->off = fc->ongoaddr;
1531				csrd->ongoaddr = fc->ongoaddr;
1532				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1533				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1534			}
1535			goto nextaddr;
1536		}
1537
1538		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1539			if(csrd == NULL){
1540				goto nextnode;
1541			};
1542			fc->ongoaddr = csrd->ongoaddr + 4;
1543			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1544			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1545			csrd = SLIST_FIRST(&fc->ongocsr);
1546			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1547				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1548				offset = CSRROMOFF;
1549			}else{
1550				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1551				offset = csrd->off;
1552			}
1553		}
1554		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1555			goto nextnode;
1556		}
1557	}
1558nextaddr:
1559	fw_xfer_free( xfer);
1560	fw_bus_explore(fc);
1561	return;
1562errnode:
1563	fc->retry_count++;
1564	if (fc->ongodev != NULL)
1565		fc->ongodev->status = FWDEVINVAL;
1566nextnode:
1567	fw_xfer_free( xfer);
1568	fc->ongonode++;
1569/* housekeeping work space */
1570	fc->ongoaddr = CSRROMOFF;
1571	fc->ongodev = NULL;
1572	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1573	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1574		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1575		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1576	}
1577	fw_bus_explore(fc);
1578	return;
1579}
1580
1581/*
1582 * To attach sub-devices layer onto IEEE1394 bus.
1583 */
1584static void
1585fw_attach_dev(struct firewire_comm *fc)
1586{
1587	struct fw_device *fwdev, *next;
1588	struct fw_xfer *xfer;
1589	int i, err;
1590	device_t *devlistp;
1591	int devcnt;
1592	struct firewire_dev_comm *fdc;
1593
1594	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1595		next = STAILQ_NEXT(fwdev, link);
1596		if (fwdev->status == FWDEVINIT) {
1597			fwdev->status = FWDEVATTACHED;
1598		} else if (fwdev->status == FWDEVINVAL) {
1599			fwdev->rcnt ++;
1600			if (fwdev->rcnt > hold_count) {
1601				/*
1602				 * Remove devices which have not been seen
1603				 * for a while.
1604				 */
1605				STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1606				    link);
1607				free(fwdev, M_FW);
1608			}
1609		}
1610	}
1611
1612	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1613	if( err != 0 )
1614		return;
1615	for( i = 0 ; i < devcnt ; i++){
1616		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1617			fdc = device_get_softc(devlistp[i]);
1618			if (fdc->post_explore != NULL)
1619				fdc->post_explore(fdc);
1620		}
1621	}
1622	free(devlistp, M_TEMP);
1623
1624	/* call pending handlers */
1625	i = 0;
1626	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1627		STAILQ_REMOVE_HEAD(&fc->pending, link);
1628		i++;
1629		if (xfer->act.hand)
1630			xfer->act.hand(xfer);
1631	}
1632	if (i > 0)
1633		printf("fw_attach_dev: %d pending handlers called\n", i);
1634	if (fc->retry_count > 0) {
1635		printf("probe failed for %d node\n", fc->retry_count);
1636#if 0
1637		callout_reset(&fc->retry_probe_callout, hz*2,
1638					(void *)fc->ibr, (void *)fc);
1639#endif
1640	}
1641	return;
1642}
1643
1644/*
1645 * To allocate uniq transaction label.
1646 */
1647static int
1648fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1649{
1650	u_int i;
1651	struct tlabel *tl, *tmptl;
1652	int s;
1653	static u_int32_t label = 0;
1654
1655	s = splfw();
1656	for( i = 0 ; i < 0x40 ; i ++){
1657		label = (label + 1) & 0x3f;
1658		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1659			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1660			if (tmptl->xfer->send.hdr.mode.hdr.dst ==
1661			    xfer->send.hdr.mode.hdr.dst)
1662				break;
1663		}
1664		if(tmptl == NULL) {
1665			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1666			if (tl == NULL) {
1667				splx(s);
1668				return (-1);
1669			}
1670			tl->xfer = xfer;
1671			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1672			splx(s);
1673			if (firewire_debug > 1)
1674				printf("fw_get_tlabel: dst=%d tl=%d\n",
1675				    xfer->send.hdr.mode.hdr.dst, label);
1676			return(label);
1677		}
1678	}
1679	splx(s);
1680
1681	printf("fw_get_tlabel: no free tlabel\n");
1682	return(-1);
1683}
1684
1685static void
1686fw_rcv_copy(struct fw_rcv_buf *rb)
1687{
1688	struct fw_pkt *pkt;
1689	u_char *p;
1690	struct tcode_info *tinfo;
1691	u_int res, i, len, plen;
1692
1693	rb->xfer->recv.spd -= rb->spd;
1694
1695	pkt = (struct fw_pkt *)rb->vec->iov_base;
1696	tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1697
1698	/* Copy header */
1699	p = (u_char *)&rb->xfer->recv.hdr;
1700	bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1701	(u_char *)rb->vec->iov_base += tinfo->hdr_len;
1702	rb->vec->iov_len -= tinfo->hdr_len;
1703
1704	/* Copy payload */
1705	p = (u_char *)rb->xfer->recv.payload;
1706	res = rb->xfer->recv.pay_len;
1707
1708	/* special handling for RRESQ */
1709	if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1710	    p != NULL && res >= sizeof(u_int32_t)) {
1711		*(u_int32_t *)p = pkt->mode.rresq.data;
1712		rb->xfer->recv.pay_len = sizeof(u_int32_t);
1713		return;
1714	}
1715
1716	if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1717		return;
1718
1719	plen = pkt->mode.rresb.len;
1720
1721	for (i = 0; i < rb->nvec; i++, rb->vec++) {
1722		len = MIN(rb->vec->iov_len, plen);
1723		if (res < len) {
1724			printf("rcv buffer(%d) is %d bytes short.\n",
1725			    rb->xfer->recv.pay_len, len - res);
1726			len = res;
1727		}
1728		bcopy(rb->vec->iov_base, p, len);
1729		p += len;
1730		res -= len;
1731		plen -= len;
1732		if (res == 0 || plen == 0)
1733			break;
1734	}
1735	rb->xfer->recv.pay_len -= res;
1736
1737}
1738
1739/*
1740 * Generic packet receving process.
1741 */
1742void
1743fw_rcv(struct fw_rcv_buf *rb)
1744{
1745	struct fw_pkt *fp, *resfp;
1746	struct fw_bind *bind;
1747	int tcode, s;
1748	int i, len, oldstate;
1749#if 0
1750	{
1751		u_int32_t *qld;
1752		int i;
1753		qld = (u_int32_t *)buf;
1754		printf("spd %d len:%d\n", spd, len);
1755		for( i = 0 ; i <= len && i < 32; i+= 4){
1756			printf("0x%08x ", ntohl(qld[i/4]));
1757			if((i % 16) == 15) printf("\n");
1758		}
1759		if((i % 16) != 15) printf("\n");
1760	}
1761#endif
1762	fp = (struct fw_pkt *)rb->vec[0].iov_base;
1763	tcode = fp->mode.common.tcode;
1764	switch (tcode) {
1765	case FWTCODE_WRES:
1766	case FWTCODE_RRESQ:
1767	case FWTCODE_RRESB:
1768	case FWTCODE_LRES:
1769		rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1770					fp->mode.hdr.tlrt >> 2);
1771		if(rb->xfer == NULL) {
1772			printf("fw_rcv: unknown response "
1773					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1774					tcode,
1775					fp->mode.hdr.src,
1776					fp->mode.hdr.tlrt >> 2,
1777					fp->mode.hdr.tlrt & 3,
1778					fp->mode.rresq.data);
1779#if 1
1780			printf("try ad-hoc work around!!\n");
1781			rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1782					(fp->mode.hdr.tlrt >> 2)^3);
1783			if (rb->xfer == NULL) {
1784				printf("no use...\n");
1785				goto err;
1786			}
1787#else
1788			goto err;
1789#endif
1790		}
1791		fw_rcv_copy(rb);
1792		if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1793			rb->xfer->resp = EIO;
1794		else
1795			rb->xfer->resp = 0;
1796		/* make sure the packet is drained in AT queue */
1797		oldstate = rb->xfer->state;
1798		rb->xfer->state = FWXF_RCVD;
1799		switch (oldstate) {
1800		case FWXF_SENT:
1801			fw_xfer_done(rb->xfer);
1802			break;
1803		case FWXF_START:
1804#if 0
1805			if (firewire_debug)
1806				printf("not sent yet tl=%x\n", rb->xfer->tl);
1807#endif
1808			break;
1809		default:
1810			printf("unexpected state %d\n", rb->xfer->state);
1811		}
1812		return;
1813	case FWTCODE_WREQQ:
1814	case FWTCODE_WREQB:
1815	case FWTCODE_RREQQ:
1816	case FWTCODE_RREQB:
1817	case FWTCODE_LREQ:
1818		bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1819			fp->mode.rreqq.dest_lo);
1820		if(bind == NULL){
1821#if __FreeBSD_version >= 500000
1822			printf("Unknown service addr 0x%04x:0x%08x tcode=%x src=0x%x data=%x\n",
1823#else
1824			printf("Unknown service addr 0x%04x:0x%08x tcode=%x src=0x%x data=%lx\n",
1825#endif
1826				fp->mode.wreqq.dest_hi,
1827				fp->mode.wreqq.dest_lo,
1828				tcode,
1829				fp->mode.hdr.src,
1830				ntohl(fp->mode.wreqq.data));
1831			if (rb->fc->status == FWBUSRESET) {
1832				printf("fw_rcv: cannot respond(bus reset)!\n");
1833				goto err;
1834			}
1835			rb->xfer = fw_xfer_alloc(M_FWXFER);
1836			if(rb->xfer == NULL){
1837				return;
1838			}
1839			rb->xfer->send.spd = rb->spd;
1840			rb->xfer->send.pay_len = 0;
1841			resfp = &rb->xfer->send.hdr;
1842			switch (tcode) {
1843			case FWTCODE_WREQQ:
1844			case FWTCODE_WREQB:
1845				resfp->mode.hdr.tcode = FWTCODE_WRES;
1846				break;
1847			case FWTCODE_RREQQ:
1848				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1849				break;
1850			case FWTCODE_RREQB:
1851				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1852				break;
1853			case FWTCODE_LREQ:
1854				resfp->mode.hdr.tcode = FWTCODE_LRES;
1855				break;
1856			}
1857			resfp->mode.hdr.dst = fp->mode.hdr.src;
1858			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1859			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1860			resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1861			resfp->mode.rresb.extcode = 0;
1862			resfp->mode.rresb.len = 0;
1863/*
1864			rb->xfer->act.hand = fw_asy_callback;
1865*/
1866			rb->xfer->act.hand = fw_xfer_free;
1867			if(fw_asyreq(rb->fc, -1, rb->xfer)){
1868				fw_xfer_free(rb->xfer);
1869				return;
1870			}
1871			goto err;
1872		}
1873		len = 0;
1874		for (i = 0; i < rb->nvec; i ++)
1875			len += rb->vec[i].iov_len;
1876		switch(bind->act_type){
1877		case FWACT_XFER:
1878			/* splfw()?? */
1879			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1880			if (rb->xfer == NULL) {
1881				printf("Discard a packet for this bind.\n");
1882				goto err;
1883			}
1884			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1885			fw_rcv_copy(rb);
1886			if (rb->fc->status != FWBUSRESET)
1887				rb->xfer->act.hand(rb->xfer);
1888			else
1889				STAILQ_INSERT_TAIL(&rb->fc->pending,
1890				    rb->xfer, link);
1891			return;
1892			break;
1893		case FWACT_CH:
1894			if(rb->fc->ir[bind->sub]->queued >=
1895				rb->fc->ir[bind->sub]->maxq){
1896				device_printf(rb->fc->bdev,
1897					"Discard a packet %x %d\n",
1898					bind->sub,
1899					rb->fc->ir[bind->sub]->queued);
1900				goto err;
1901			}
1902			rb->xfer = STAILQ_FIRST(&bind->xferlist);
1903			if (rb->xfer == NULL) {
1904				printf("Discard packet for this bind\n");
1905				goto err;
1906			}
1907			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1908			fw_rcv_copy(rb);
1909			s = splfw();
1910			rb->fc->ir[bind->sub]->queued++;
1911			STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q,
1912			    rb->xfer, link);
1913			splx(s);
1914
1915			wakeup((caddr_t)rb->fc->ir[bind->sub]);
1916
1917			return;
1918			break;
1919		default:
1920			goto err;
1921			break;
1922		}
1923		break;
1924#if 0 /* shouldn't happen ?? or for GASP */
1925	case FWTCODE_STREAM:
1926	{
1927		struct fw_xferq *xferq;
1928
1929		xferq = rb->fc->ir[sub];
1930#if 0
1931		printf("stream rcv dma %d len %d off %d spd %d\n",
1932			sub, len, off, spd);
1933#endif
1934		if(xferq->queued >= xferq->maxq) {
1935			printf("receive queue is full\n");
1936			goto err;
1937		}
1938		/* XXX get xfer from xfer queue, we don't need copy for
1939			per packet mode */
1940		rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1941						vec[0].iov_len);
1942		if (rb->xfer == NULL) goto err;
1943		fw_rcv_copy(rb)
1944		s = splfw();
1945		xferq->queued++;
1946		STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1947		splx(s);
1948		sc = device_get_softc(rb->fc->bdev);
1949#if __FreeBSD_version >= 500000
1950		if (SEL_WAITING(&xferq->rsel))
1951#else
1952		if (&xferq->rsel.si_pid != 0)
1953#endif
1954			selwakeup(&xferq->rsel);
1955		if (xferq->flag & FWXFERQ_WAKEUP) {
1956			xferq->flag &= ~FWXFERQ_WAKEUP;
1957			wakeup((caddr_t)xferq);
1958		}
1959		if (xferq->flag & FWXFERQ_HANDLER) {
1960			xferq->hand(xferq);
1961		}
1962		return;
1963		break;
1964	}
1965#endif
1966	default:
1967		printf("fw_rcv: unknow tcode %d\n", tcode);
1968		break;
1969	}
1970err:
1971	return;
1972}
1973
1974/*
1975 * Post process for Bus Manager election process.
1976 */
1977static void
1978fw_try_bmr_callback(struct fw_xfer *xfer)
1979{
1980	struct firewire_comm *fc;
1981	int bmr;
1982
1983	if (xfer == NULL)
1984		return;
1985	fc = xfer->fc;
1986	if (xfer->resp != 0)
1987		goto error;
1988	if (xfer->recv.payload == NULL)
1989		goto error;
1990	if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
1991		goto error;
1992
1993	bmr = ntohl(xfer->recv.payload[0]);
1994	if (bmr == 0x3f)
1995		bmr = fc->nodeid;
1996
1997	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1998	fw_xfer_free_buf(xfer);
1999	fw_bmr(fc);
2000	return;
2001
2002error:
2003	device_printf(fc->bdev, "bus manager election failed\n");
2004	fw_xfer_free_buf(xfer);
2005}
2006
2007
2008/*
2009 * To candidate Bus Manager election process.
2010 */
2011static void
2012fw_try_bmr(void *arg)
2013{
2014	struct fw_xfer *xfer;
2015	struct firewire_comm *fc = (struct firewire_comm *)arg;
2016	struct fw_pkt *fp;
2017	int err = 0;
2018
2019	xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2020	if(xfer == NULL){
2021		return;
2022	}
2023	xfer->send.spd = 0;
2024	fc->status = FWBUSMGRELECT;
2025
2026	fp = &xfer->send.hdr;
2027	fp->mode.lreq.dest_hi = 0xffff;
2028	fp->mode.lreq.tlrt = 0;
2029	fp->mode.lreq.tcode = FWTCODE_LREQ;
2030	fp->mode.lreq.pri = 0;
2031	fp->mode.lreq.src = 0;
2032	fp->mode.lreq.len = 8;
2033	fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2034	fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2035	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2036	xfer->send.payload[0] = htonl(0x3f);
2037	xfer->send.payload[1] = htonl(fc->nodeid);
2038	xfer->act.hand = fw_try_bmr_callback;
2039
2040	err = fw_asyreq(fc, -1, xfer);
2041	if(err){
2042		fw_xfer_free_buf(xfer);
2043		return;
2044	}
2045	return;
2046}
2047
2048#ifdef FW_VMACCESS
2049/*
2050 * Software implementation for physical memory block access.
2051 * XXX:Too slow, usef for debug purpose only.
2052 */
2053static void
2054fw_vmaccess(struct fw_xfer *xfer){
2055	struct fw_pkt *rfp, *sfp = NULL;
2056	u_int32_t *ld = (u_int32_t *)xfer->recv.buf;
2057
2058	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2059			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2060	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2061	if(xfer->resp != 0){
2062		fw_xfer_free( xfer);
2063		return;
2064	}
2065	if(xfer->recv.buf == NULL){
2066		fw_xfer_free( xfer);
2067		return;
2068	}
2069	rfp = (struct fw_pkt *)xfer->recv.buf;
2070	switch(rfp->mode.hdr.tcode){
2071		/* XXX need fix for 64bit arch */
2072		case FWTCODE_WREQB:
2073			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2074			xfer->send.len = 12;
2075			sfp = (struct fw_pkt *)xfer->send.buf;
2076			bcopy(rfp->mode.wreqb.payload,
2077				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2078			sfp->mode.wres.tcode = FWTCODE_WRES;
2079			sfp->mode.wres.rtcode = 0;
2080			break;
2081		case FWTCODE_WREQQ:
2082			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2083			xfer->send.len = 12;
2084			sfp->mode.wres.tcode = FWTCODE_WRES;
2085			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2086			sfp->mode.wres.rtcode = 0;
2087			break;
2088		case FWTCODE_RREQB:
2089			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2090			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2091			sfp = (struct fw_pkt *)xfer->send.buf;
2092			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2093				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2094			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2095			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2096			sfp->mode.rresb.rtcode = 0;
2097			sfp->mode.rresb.extcode = 0;
2098			break;
2099		case FWTCODE_RREQQ:
2100			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2101			xfer->send.len = 16;
2102			sfp = (struct fw_pkt *)xfer->send.buf;
2103			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2104			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2105			sfp->mode.rresb.rtcode = 0;
2106			break;
2107		default:
2108			fw_xfer_free( xfer);
2109			return;
2110	}
2111	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2112	xfer->dst = ntohs(rfp->mode.hdr.src);
2113	xfer->act.hand = fw_xfer_free;
2114	xfer->retry_req = fw_asybusy;
2115
2116	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2117	sfp->mode.hdr.pri = 0;
2118
2119	fw_asyreq(xfer->fc, -1, xfer);
2120/**/
2121	return;
2122}
2123#endif
2124
2125/*
2126 * CRC16 check-sum for IEEE1394 register blocks.
2127 */
2128u_int16_t
2129fw_crc16(u_int32_t *ptr, u_int32_t len){
2130	u_int32_t i, sum, crc = 0;
2131	int shift;
2132	len = (len + 3) & ~3;
2133	for(i = 0 ; i < len ; i+= 4){
2134		for( shift = 28 ; shift >= 0 ; shift -= 4){
2135			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2136			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2137		}
2138		crc &= 0xffff;
2139	}
2140	return((u_int16_t) crc);
2141}
2142
2143static int
2144fw_bmr(struct firewire_comm *fc)
2145{
2146	struct fw_device fwdev;
2147	union fw_self_id *self_id;
2148	int cmstr;
2149	u_int32_t quad;
2150
2151	/* Check to see if the current root node is cycle master capable */
2152	self_id = &fc->topology_map->self_id[fc->max_node];
2153	if (fc->max_node > 0) {
2154		/* XXX check cmc bit of businfo block rather than contender */
2155		if (self_id->p0.link_active && self_id->p0.contender)
2156			cmstr = fc->max_node;
2157		else {
2158			device_printf(fc->bdev,
2159				"root node is not cycle master capable\n");
2160			/* XXX shall we be the cycle master? */
2161			cmstr = fc->nodeid;
2162			/* XXX need bus reset */
2163		}
2164	} else
2165		cmstr = -1;
2166
2167	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2168	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2169		/* We are not the bus manager */
2170		printf("\n");
2171		return(0);
2172	}
2173	printf("(me)\n");
2174
2175	/* Optimize gapcount */
2176	if(fc->max_hop <= MAX_GAPHOP )
2177		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2178	/* If we are the cycle master, nothing to do */
2179	if (cmstr == fc->nodeid || cmstr == -1)
2180		return 0;
2181	/* Bus probe has not finished, make dummy fwdev for cmstr */
2182	bzero(&fwdev, sizeof(fwdev));
2183	fwdev.fc = fc;
2184	fwdev.dst = cmstr;
2185	fwdev.speed = 0;
2186	fwdev.maxrec = 8; /* 512 */
2187	fwdev.status = FWDEVINIT;
2188	/* Set cmstr bit on the cycle master */
2189	quad = htonl(1 << 8);
2190	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2191		0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2192
2193	return 0;
2194}
2195
2196static int
2197fw_modevent(module_t mode, int type, void *data)
2198{
2199	int err = 0;
2200#if __FreeBSD_version >= 500000
2201	static eventhandler_tag fwdev_ehtag = NULL;
2202#endif
2203
2204	switch (type) {
2205	case MOD_LOAD:
2206#if __FreeBSD_version >= 500000
2207		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2208						fwdev_clone, 0, 1000);
2209#endif
2210		break;
2211	case MOD_UNLOAD:
2212#if __FreeBSD_version >= 500000
2213		if (fwdev_ehtag != NULL)
2214			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2215#endif
2216		break;
2217	case MOD_SHUTDOWN:
2218		break;
2219	}
2220	return (err);
2221}
2222
2223
2224DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2225MODULE_VERSION(firewire, 1);
2226