firewire.c revision 108853
1103285Sikob/*
2103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3103285Sikob * All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the acknowledgement as bellow:
15103285Sikob *
16103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
17103285Sikob *
18103285Sikob * 4. The name of the author may not be used to endorse or promote products
19103285Sikob *    derived from this software without specific prior written permission.
20103285Sikob *
21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31103285Sikob * POSSIBILITY OF SUCH DAMAGE.
32103285Sikob *
33103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 108853 2003-01-07 04:26:45Z simokawa $
34103285Sikob *
35103285Sikob */
36103285Sikob
37103285Sikob#include <sys/param.h>
38103285Sikob#include <sys/systm.h>
39103285Sikob#include <sys/types.h>
40103285Sikob#include <sys/mbuf.h>
41103285Sikob#include <sys/socket.h>
42103285Sikob#include <sys/socketvar.h>
43103285Sikob
44103285Sikob#include <sys/kernel.h>
45103285Sikob#include <sys/malloc.h>
46103285Sikob#include <sys/conf.h>
47103285Sikob#include <sys/uio.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob
50103285Sikob#include <machine/cpufunc.h>    /* for rdtsc proto for clock.h below */
51103285Sikob#include <machine/clock.h>
52103285Sikob
53103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
54103285Sikob
55103285Sikob#include <dev/firewire/firewire.h>
56103285Sikob#include <dev/firewire/firewirereg.h>
57103285Sikob#include <dev/firewire/iec13213.h>
58103285Sikob#include <dev/firewire/iec68113.h>
59103285Sikob
60103285Sikobint firewire_debug=0;
61108281SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
62103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
63108281Ssimokawa	"FireWire driver debug flag");
64103285Sikob
65103285Sikob#define FW_MAXASYRTY 4
66103285Sikob#define FW_MAXDEVRCNT 4
67103285Sikob
68103285Sikob#define XFER_TIMEOUT 0
69103285Sikob
70103285Sikobdevclass_t firewire_devclass;
71103285Sikob
72103285Sikobstatic int firewire_match      __P((device_t));
73103285Sikobstatic int firewire_attach      __P((device_t));
74103285Sikobstatic int firewire_detach      __P((device_t));
75103285Sikob#if 0
76103285Sikobstatic int firewire_shutdown    __P((device_t));
77103285Sikob#endif
78103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
79103285Sikobstatic void fw_try_bmr __P((void *));
80103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
81103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
82103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
83103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
84103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
85103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
86103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
87106543Ssimokawa#ifdef FW_VMACCESS
88103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
89106543Ssimokawa#endif
90103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
91105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
92103285Sikob
93103285Sikobstatic device_method_t firewire_methods[] = {
94103285Sikob	/* Device interface */
95103285Sikob	DEVMETHOD(device_probe,		firewire_match),
96103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
97103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
98108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
99108642Ssimokawa	DEVMETHOD(device_resume,	bus_generic_resume),
100103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
101103285Sikob
102103285Sikob	/* Bus interface */
103103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
104103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
105103285Sikob
106103285Sikob	{ 0, 0 }
107103285Sikob};
108103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
109103285Sikob
110103285Sikob#define MAX_GAPHOP  16
111103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
112103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
113106813Ssimokawa
114106813Ssimokawaextern struct cdevsw firewire_cdevsw;
115106813Ssimokawa
116103285Sikobstatic driver_t firewire_driver = {
117103285Sikob	"firewire",
118103285Sikob	firewire_methods,
119103285Sikob	sizeof(struct firewire_softc),
120103285Sikob};
121103285Sikob
122103285Sikob/*
123103285Sikob * transmitter buffer update.
124103285Sikob */
125103285Sikobint
126103285Sikobfw_tbuf_update(struct firewire_comm *fc, int sub, int flag){
127103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
128103285Sikob	struct fw_dvbuf *dvbuf = NULL;
129103285Sikob	struct fw_xferq *it;
130103285Sikob	int s, err = 0, i, j, chtag;
131103285Sikob	struct fw_pkt *fp;
132103285Sikob	u_int64_t tmpsync, dvsync;
133103285Sikob
134103285Sikob	it = fc->it[sub];
135103285Sikob
136103285Sikob	s = splfw();
137103285Sikob	if(it->stdma == NULL){
138103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
139103285Sikob	}else if(flag != 0){
140103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
141103285Sikob		if(bulkxfer == it->stdma){
142103285Sikob			STAILQ_REMOVE_HEAD(&it->stvalid, link);
143103285Sikob			it->stdma->flag = 0;
144103285Sikob			STAILQ_INSERT_TAIL(&it->stfree, it->stdma, link);
145103285Sikob			if(!(it->flag & FWXFERQ_DV))
146103285Sikob				wakeup(it);
147103285Sikob		}
148103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
149103285Sikob	}else{
150103285Sikob		bulkxfer = it->stdma;
151103285Sikob	}
152103285Sikob	splx(s);
153103285Sikob	if(bulkxfer != NULL){
154103285Sikob		s = splfw();
155103285Sikob		bulkxfer2 = STAILQ_NEXT(bulkxfer, link);
156103285Sikob#if 0
157103285Sikob		if(it->flag & FWXFERQ_DV && bulkxfer2 == NULL){
158103285Sikob			bulkxfer2 = STAILQ_FIRST(&it->stfree);
159103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
160103285Sikob			splx(s);
161103285Sikob			bcopy(bulkxfer->buf, bulkxfer2->buf,
162103285Sikob					it->psize * it->btpacket);
163103285Sikob			s = splfw();
164103285Sikob			STAILQ_INSERT_TAIL(&it->stvalid, bulkxfer2, link);
165103285Sikob		}
166103285Sikob#endif
167103285Sikob		splx(s);
168103285Sikob	}
169103285Sikob	it->stdma = bulkxfer;
170103285Sikob	it->stdma2 = bulkxfer2;
171103285Sikob
172103285Sikob	if(it->flag & FWXFERQ_DV){
173103285Sikob		chtag = it->flag & 0xff;
174103285Sikobdvloop:
175103285Sikob		if(it->dvdma == NULL){
176103285Sikob			dvbuf = STAILQ_FIRST(&it->dvvalid);
177103285Sikob			if(dvbuf != NULL){
178103285Sikob				s = splfw();
179103285Sikob				STAILQ_REMOVE_HEAD(&it->dvvalid, link);
180103285Sikob				it->dvdma = dvbuf;
181103285Sikob				splx(s);
182103285Sikob				it->queued = 0;
183103285Sikob			}
184103285Sikob		}
185103285Sikob		if(it->dvdma == NULL)
186103285Sikob			return err;
187103285Sikob
188103285Sikob		it->stproc = STAILQ_FIRST(&it->stfree);
189103285Sikob		if(it->stproc != NULL){
190103285Sikob			s = splfw();
191103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
192103285Sikob			splx(s);
193103285Sikob		}else{
194103285Sikob			return err;
195103285Sikob		}
196103285Sikob/*
197103285Sikob * Insert least significant 12 bits timestamp value by computation.
198103285Sikob * Highest significant 4 bits is insert at just before packet sending.
199103285Sikob */
200103285Sikob		fp = (struct fw_pkt *)(it->stproc->buf);
201103285Sikob/* XXX: Parameter relies on NTSC type DV video */
202103485Sikob		tmpsync = (u_int64_t)3072 * 8000 * 100 / 2997;
203103285Sikob		tmpsync *= it->dvsync;
204103285Sikob		dvsync = tmpsync;
205103285Sikob		dvsync %= 0xc00;
206103285Sikob		fp->mode.ld[2] = htonl(0x80000000 | (dvsync % 0xc00));
207103285Sikob		it->dvsync ++;
208103285Sikob		it->dvsync %= 2997;
209103285Sikob
210103285Sikob		for( i = 0, j = 0 ; i < it->dvpacket ; i++){
211103285Sikob			bcopy(it->dvdma->buf + it->queued * it->psize,
212103285Sikob				it->stproc->buf + j * it->psize, it->psize);
213103285Sikob			fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
214103285Sikob			fp->mode.stream.len = htons(488);
215103285Sikob			fp->mode.stream.chtag = chtag;
216103285Sikob			fp->mode.stream.tcode = FWTCODE_STREAM;
217103285Sikob			fp->mode.ld[1] = htonl((fc->nodeid << 24) | 0x00780000 | it->dvdbc);
218103285Sikob			it->dvdbc++;
219103285Sikob			it->dvdbc %= 256;
220103285Sikob			it->queued ++;
221103285Sikob			j++;
222103285Sikob/* XXX: Parameter relies on NTSC type DV video */
223103285Sikob#if 1
224103285Sikob#define DVDIFF 203
225103285Sikob#define DVFRAC 2997
226103285Sikob#else
227103285Sikob#define DVDIFF 127
228103285Sikob#define DVFRAC 1875
229103285Sikob#endif
230103285Sikob			it->dvdiff += DVDIFF;
231103285Sikob			if(it->dvdiff >= DVFRAC){
232103285Sikob				it->dvdiff %= DVFRAC;
233103285Sikob				fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
234103285Sikob
235103285Sikob				fp->mode.stream.len = htons(0x8);
236103285Sikob				fp->mode.stream.chtag = chtag;
237103285Sikob				fp->mode.stream.tcode = FWTCODE_STREAM;
238103285Sikob				fp->mode.ld[1] = htonl((fc->nodeid << 24) |
239103285Sikob					 0x00780000 | it->dvdbc);
240103285Sikob				j++;
241103285Sikob			}
242103285Sikob		}
243103285Sikob		it->stproc->npacket = j;
244103285Sikob		s = splfw();
245103285Sikob		STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
246103285Sikob		splx(s);
247103285Sikob		if(it->queued >= it->dvpacket){
248103285Sikob			s = splfw();
249103285Sikob			STAILQ_INSERT_TAIL(&it->dvfree, it->dvdma, link);
250103285Sikob			it->dvdma = NULL;
251103285Sikob			splx(s);
252103285Sikob			wakeup(it);
253103285Sikob			goto dvloop;
254103285Sikob		}
255103285Sikob	}
256103285Sikob	return err;
257103285Sikob}
258103285Sikob/*
259103285Sikob * receving buffer update.
260103285Sikob */
261103285Sikobint
262103285Sikobfw_rbuf_update(struct firewire_comm *fc, int sub, int flag){
263103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
264103285Sikob	struct fw_xferq *ir;
265103285Sikob	int s, err = 0;
266103285Sikob
267103285Sikob	ir = fc->ir[sub];
268103285Sikob	s = splfw();
269103285Sikob	if(ir->stdma != NULL){
270103285Sikob		if(flag != 0){
271103285Sikob			STAILQ_INSERT_TAIL(&ir->stvalid, ir->stdma, link);
272103285Sikob		}else{
273103285Sikob			ir->stdma->flag = 0;
274103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree, ir->stdma, link);
275103285Sikob		}
276103285Sikob	}
277103285Sikob	if(ir->stdma2 != NULL){
278103285Sikob		bulkxfer = ir->stdma2;
279103285Sikob		bulkxfer2 = STAILQ_FIRST(&ir->stfree);
280103285Sikob		if(bulkxfer2 != NULL){
281103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
282103285Sikob		}
283103285Sikob	}else{
284103285Sikob		bulkxfer = STAILQ_FIRST(&ir->stfree);
285103285Sikob		if(bulkxfer != NULL){
286103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
287103285Sikob			bulkxfer2 = STAILQ_FIRST(&ir->stfree);
288103285Sikob			if(bulkxfer2 != NULL){
289103285Sikob				STAILQ_REMOVE_HEAD(&ir->stfree, link);
290103285Sikob			}
291103285Sikob		}else{
292103285Sikob			bulkxfer = STAILQ_FIRST(&ir->stvalid);
293103285Sikob			STAILQ_REMOVE_HEAD(&ir->stvalid, link);
294103285Sikob		}
295103285Sikob	}
296103285Sikob	splx(s);
297103285Sikob	ir->stdma = bulkxfer;
298103285Sikob	ir->stdma2 = bulkxfer2;
299103285Sikob	return err;
300103285Sikob}
301103285Sikob
302103285Sikob/*
303103285Sikob * To lookup node id. from EUI64.
304103285Sikob */
305106810Ssimokawastruct fw_device *
306106790Ssimokawafw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui)
307103285Sikob{
308103285Sikob	struct fw_device *fwdev;
309103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
310103285Sikob		fwdev = TAILQ_NEXT(fwdev, link)){
311103285Sikob		if(fwdev->eui.hi == eui.hi && fwdev->eui.lo == eui.lo){
312103285Sikob			break;
313103285Sikob		}
314103285Sikob	}
315106810Ssimokawa	if(fwdev == NULL) return NULL;
316106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
317106810Ssimokawa	return fwdev;
318103285Sikob}
319106813Ssimokawa
320103285Sikob/*
321103285Sikob * Async. request procedure for userland application.
322103285Sikob */
323103285Sikobint
324103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
325103285Sikob{
326103285Sikob	int err = 0;
327103285Sikob	struct fw_xferq *xferq;
328103285Sikob	int tl = 0, len;
329103285Sikob	struct fw_pkt *fp;
330103285Sikob	int tcode;
331103285Sikob	struct tcode_info *info;
332103285Sikob
333103285Sikob	if(xfer == NULL) return EINVAL;
334108701Ssimokawa	if(xfer->send.len > MAXREC(fc->maxrec)){
335103285Sikob		printf("send.len > maxrec\n");
336103285Sikob		return EINVAL;
337103285Sikob	}
338103285Sikob	if(xfer->act.hand == NULL){
339103285Sikob		printf("act.hand == NULL\n");
340103285Sikob		return EINVAL;
341103285Sikob	}
342103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
343103285Sikob
344103285Sikob	tcode = fp->mode.common.tcode & 0xf;
345103285Sikob	info = &fc->tcode[tcode];
346103285Sikob	if (info->flag == 0) {
347103285Sikob		printf("invalid tcode=%d\n", tcode);
348103285Sikob		return EINVAL;
349103285Sikob	}
350103285Sikob	if (info->flag & FWTI_REQ)
351103285Sikob		xferq = fc->atq;
352103285Sikob	else
353103285Sikob		xferq = fc->ats;
354103285Sikob	len = info->hdr_len;
355103285Sikob	if (info->flag & FWTI_BLOCK_STR)
356103285Sikob		len += ntohs(fp->mode.stream.len);
357103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
358103285Sikob		len += ntohs(fp->mode.rresb.len);
359103285Sikob	if( len >  xfer->send.len ){
360103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
361103285Sikob				len, xfer->send.len, tcode);
362103285Sikob		return EINVAL;
363103285Sikob	}
364103285Sikob	xfer->send.len = len;
365106790Ssimokawa
366103285Sikob	if(xferq->start == NULL){
367103285Sikob		printf("xferq->start == NULL\n");
368103285Sikob		return EINVAL;
369103285Sikob	}
370103285Sikob	if(!(xferq->queued < xferq->maxq)){
371108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
372108655Ssimokawa			xferq->queued);
373103285Sikob		return EINVAL;
374103285Sikob	}
375103285Sikob
376103285Sikob
377103285Sikob	if (info->flag & FWTI_TLABEL) {
378103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
379103285Sikob			return EIO;
380103285Sikob		fp->mode.hdr.tlrt = tl << 2;
381103285Sikob	}
382103285Sikob
383103285Sikob	xfer->tl = tl;
384103285Sikob	xfer->tcode = tcode;
385103285Sikob	xfer->resp = 0;
386103285Sikob	xfer->fc = fc;
387103285Sikob	xfer->q = xferq;
388103285Sikob	xfer->act_type = FWACT_XFER;
389103285Sikob	xfer->retry_req = fw_asybusy;
390103285Sikob
391103285Sikob	fw_asystart(xfer);
392103285Sikob	return err;
393103285Sikob}
394103285Sikob/*
395103285Sikob * Wakeup blocked process.
396103285Sikob */
397103285Sikobvoid
398103285Sikobfw_asy_callback(struct fw_xfer *xfer){
399103285Sikob	wakeup(xfer);
400103285Sikob	return;
401103285Sikob}
402103285Sikob/*
403103285Sikob * Postpone to later retry.
404103285Sikob */
405103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
406106815Ssimokawa#if 1
407103285Sikob	printf("fw_asybusy\n");
408103285Sikob#endif
409103285Sikob#if XFER_TIMEOUT
410103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
411103285Sikob#endif
412103285Sikob/*
413103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
414103285Sikob*/
415103285Sikob	DELAY(20000);
416103285Sikob	fw_asystart(xfer);
417103285Sikob	return;
418103285Sikob}
419103285Sikob#if XFER_TIMEOUT
420103285Sikob/*
421103285Sikob * Post timeout for async. request.
422103285Sikob */
423103285Sikobvoid
424103285Sikobfw_xfer_timeout(void *arg)
425103285Sikob{
426103285Sikob	int s;
427103285Sikob	struct fw_xfer *xfer;
428103285Sikob
429103285Sikob	xfer = (struct fw_xfer *)arg;
430103285Sikob	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
431103285Sikob	/* XXX set error code */
432103285Sikob	s = splfw();
433103285Sikob	xfer->act.hand(xfer);
434103285Sikob	splx(s);
435103285Sikob}
436103285Sikob#endif
437103285Sikob/*
438103285Sikob * Async. request with given xfer structure.
439103285Sikob */
440106790Ssimokawastatic void
441106790Ssimokawafw_asystart(struct fw_xfer *xfer)
442106790Ssimokawa{
443103285Sikob	struct firewire_comm *fc = xfer->fc;
444103285Sikob	int s;
445103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
446103285Sikob		xfer->resp = EBUSY;
447103285Sikob		xfer->state = FWXF_BUSY;
448103285Sikob		xfer->act.hand(xfer);
449103285Sikob		return;
450103285Sikob	}
451103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
452103285Sikob	if (fc->status < FWBUSEXPLORE) {
453103285Sikob		xfer->resp = EAGAIN;
454103285Sikob		xfer->state = FWXF_BUSY;
455103285Sikob		if (xfer->act.hand != NULL)
456103285Sikob			xfer->act.hand(xfer);
457103285Sikob		return;
458103285Sikob	}
459103285Sikob#endif
460103285Sikob	s = splfw();
461103285Sikob	xfer->state = FWXF_INQ;
462103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
463103285Sikob	xfer->q->queued ++;
464103285Sikob	splx(s);
465103285Sikob	/* XXX just queue for mbuf */
466103285Sikob	if (xfer->mbuf == NULL)
467103285Sikob		xfer->q->start(fc);
468103285Sikob#if XFER_TIMEOUT
469103285Sikob	if (xfer->act.hand != NULL)
470103285Sikob		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
471103285Sikob#endif
472103285Sikob	return;
473103285Sikob}
474106790Ssimokawa
475103285Sikobstatic int
476103285Sikobfirewire_match( device_t dev )
477103285Sikob{
478108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
479103285Sikob	return -140;
480103285Sikob}
481106790Ssimokawa
482103285Sikob/*
483103285Sikob * The attach routine.
484103285Sikob */
485103285Sikobstatic int
486103285Sikobfirewire_attach( device_t dev )
487103285Sikob{
488103285Sikob	int i, unitmask, mn;
489103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
490103285Sikob	device_t pa = device_get_parent(dev);
491103285Sikob	struct firewire_comm *fc;
492103285Sikob	dev_t d;
493103285Sikob
494103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
495103285Sikob	sc->fc = fc;
496103285Sikob
497103285Sikob	unitmask = UNIT2MIN(device_get_unit(dev));
498103285Sikob
499103285Sikob	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
500103285Sikob	for ( i = 0 ; i < fc->nisodma ; i++ ){
501103285Sikob		mn = unitmask | i;
502103285Sikob		/* XXX device name should be improved */
503103285Sikob		d = make_dev(&firewire_cdevsw, unit2minor(mn),
504108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
505103285Sikob			"fw%x", mn);
506103285Sikob#if __FreeBSD_version >= 500000
507103285Sikob		if (i == 0)
508103285Sikob			sc->dev = d;
509103285Sikob		else
510103285Sikob			dev_depends(sc->dev, d);
511103285Sikob#else
512103285Sikob		sc->dev[i] = d;
513103285Sikob#endif
514103285Sikob	}
515103285Sikob	d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
516108276Ssimokawa			UID_ROOT, GID_OPERATOR, 0660,
517103285Sikob			"fwmem%d", device_get_unit(dev));
518103285Sikob#if __FreeBSD_version >= 500000
519103285Sikob	dev_depends(sc->dev, d);
520103285Sikob#else
521103285Sikob	sc->dev[i] = d;
522103285Sikob#endif
523103285Sikob	sc->fc->timeouthandle = timeout((timeout_t *)sc->fc->timeout, (void *)sc->fc, hz * 10);
524103285Sikob
525108853Ssimokawa	callout_init(&sc->fc->busprobe_callout
526108853Ssimokawa#if __FreeBSD_version >= 500000
527108853Ssimokawa						, /* mpsafe? */ 0);
528108853Ssimokawa#else
529108853Ssimokawa						);
530108853Ssimokawa#endif
531108853Ssimokawa
532103285Sikob	/* Locate our children */
533103285Sikob	bus_generic_probe(dev);
534103285Sikob
535103285Sikob	/* launch attachement of the added children */
536103285Sikob	bus_generic_attach(dev);
537103285Sikob
538103285Sikob	/* bus_reset */
539103285Sikob	fc->ibr(fc);
540103285Sikob
541103285Sikob	return 0;
542103285Sikob}
543103285Sikob
544103285Sikob/*
545103285Sikob * Attach it as child.
546103285Sikob */
547103285Sikobstatic device_t
548103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
549103285Sikob{
550103285Sikob        device_t child;
551103285Sikob	struct firewire_softc *sc;
552103285Sikob
553103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
554103285Sikob	child = device_add_child(dev, name, unit);
555103285Sikob	if (child) {
556103285Sikob		device_set_ivars(child, sc->fc);
557103285Sikob		device_probe_and_attach(child);
558103285Sikob	}
559103285Sikob
560103285Sikob	return child;
561103285Sikob}
562106790Ssimokawa
563103285Sikob/*
564103285Sikob * Dettach it.
565103285Sikob */
566103285Sikobstatic int
567103285Sikobfirewire_detach( device_t dev )
568103285Sikob{
569103285Sikob	struct firewire_softc *sc;
570103285Sikob
571103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
572106790Ssimokawa
573103285Sikob#if __FreeBSD_version >= 500000
574103285Sikob	destroy_dev(sc->dev);
575103285Sikob#else
576103285Sikob	{
577103285Sikob		int j;
578103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
579103285Sikob			destroy_dev(sc->dev[j]);
580103285Sikob	}
581103285Sikob#endif
582103285Sikob	/* XXX xfree_free and untimeout on all xfers */
583103285Sikob	untimeout((timeout_t *)sc->fc->timeout, sc->fc, sc->fc->timeouthandle);
584103285Sikob	free(sc->fc->topology_map, M_DEVBUF);
585103285Sikob	free(sc->fc->speed_map, M_DEVBUF);
586103285Sikob	bus_generic_detach(dev);
587103285Sikob	return(0);
588103285Sikob}
589103285Sikob#if 0
590103285Sikobstatic int
591103285Sikobfirewire_shutdown( device_t dev )
592103285Sikob{
593103285Sikob	return 0;
594103285Sikob}
595103285Sikob#endif
596106790Ssimokawa
597103285Sikob/*
598106790Ssimokawa * Called after bus reset.
599103285Sikob */
600106790Ssimokawavoid
601106790Ssimokawafw_busreset(struct firewire_comm *fc)
602103285Sikob{
603103285Sikob	int i;
604103285Sikob	struct fw_xfer *xfer;
605103285Sikob
606103285Sikob	switch(fc->status){
607103285Sikob	case FWBUSMGRELECT:
608103285Sikob		untimeout((timeout_t *)fw_try_bmr, (void *)fc, fc->bmrhandle);
609103285Sikob		break;
610103285Sikob	default:
611103285Sikob		break;
612103285Sikob	}
613103285Sikob	fc->status = FWBUSRESET;
614103285Sikob/* XXX: discard all queued packet */
615103285Sikob	while((xfer = STAILQ_FIRST(&fc->atq->q)) != NULL){
616103285Sikob		STAILQ_REMOVE_HEAD(&fc->atq->q, link);
617103285Sikob		xfer->resp = EAGAIN;
618103285Sikob		switch(xfer->act_type){
619103285Sikob		case FWACT_XFER:
620103285Sikob			fw_xfer_done(xfer);
621103285Sikob			break;
622103285Sikob		default:
623103285Sikob			break;
624103285Sikob		}
625103285Sikob		fw_xfer_free( xfer);
626103285Sikob	}
627103285Sikob	while((xfer = STAILQ_FIRST(&fc->ats->q)) != NULL){
628103285Sikob		STAILQ_REMOVE_HEAD(&fc->ats->q, link);
629103285Sikob		xfer->resp = EAGAIN;
630103285Sikob		switch(xfer->act_type){
631103285Sikob		case FWACT_XFER:
632103285Sikob			fw_xfer_done(xfer);
633103285Sikob		default:
634103285Sikob			break;
635103285Sikob		}
636103285Sikob		fw_xfer_free( xfer);
637103285Sikob	}
638103285Sikob	for(i = 0; i < fc->nisodma; i++)
639103285Sikob		while((xfer = STAILQ_FIRST(&fc->it[i]->q)) != NULL){
640103285Sikob			STAILQ_REMOVE_HEAD(&fc->it[i]->q, link);
641103285Sikob			xfer->resp = 0;
642103285Sikob			switch(xfer->act_type){
643103285Sikob			case FWACT_XFER:
644103285Sikob				fw_xfer_done(xfer);
645103285Sikob				break;
646103285Sikob			default:
647103285Sikob				break;
648103285Sikob			}
649103285Sikob			fw_xfer_free( xfer);
650103285Sikob		}
651103285Sikob
652103285Sikob	CSRARC(fc, STATE_CLEAR)
653103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
654103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
655103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
656103285Sikob
657103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
658103285Sikob	fc->irm = -1;
659103285Sikob
660103285Sikob	fc->max_node = -1;
661103285Sikob
662103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
663103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
664103285Sikob	}
665103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
666103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
667103285Sikob	CSRARC(fc, RESET_START) = 0;
668103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
669103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
670103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
671103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
672103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
673103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
674103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
675103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
676103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
677103285Sikob
678103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
679103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
680103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
681103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
682103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
683103285Sikob
684103285Sikob/* DV depend CSRs see blue book */
685103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
686103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
687103285Sikob
688103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
689103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
690103285Sikob}
691106790Ssimokawa
692103285Sikob/* Call once after reboot */
693106790Ssimokawavoid fw_init(struct firewire_comm *fc)
694103285Sikob{
695103285Sikob	int i;
696106543Ssimokawa	struct csrdir *csrd;
697106543Ssimokawa#ifdef FW_VMACCESS
698103285Sikob	struct fw_xfer *xfer;
699103285Sikob	struct fw_bind *fwb;
700106543Ssimokawa#endif
701103285Sikob
702103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
703103285Sikob
704103285Sikob	fc->arq->queued = 0;
705103285Sikob	fc->ars->queued = 0;
706103285Sikob	fc->atq->queued = 0;
707103285Sikob	fc->ats->queued = 0;
708103285Sikob
709103285Sikob	fc->arq->psize = FWPMAX_S400;
710103285Sikob	fc->ars->psize = FWPMAX_S400;
711103285Sikob	fc->atq->psize = FWPMAX_S400;
712103285Sikob	fc->ats->psize = FWPMAX_S400;
713103285Sikob
714103285Sikob
715103285Sikob	fc->arq->buf = NULL;
716103285Sikob	fc->ars->buf = NULL;
717103285Sikob	fc->atq->buf = NULL;
718103285Sikob	fc->ats->buf = NULL;
719103285Sikob
720103285Sikob	fc->arq->flag = FWXFERQ_PACKET;
721103285Sikob	fc->ars->flag = FWXFERQ_PACKET;
722103285Sikob	fc->atq->flag = FWXFERQ_PACKET;
723103285Sikob	fc->ats->flag = FWXFERQ_PACKET;
724103285Sikob
725103285Sikob	STAILQ_INIT(&fc->atq->q);
726103285Sikob	STAILQ_INIT(&fc->ats->q);
727103285Sikob
728103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
729103285Sikob		fc->it[i]->queued = 0;
730103285Sikob		fc->ir[i]->queued = 0;
731103285Sikob
732103285Sikob		fc->it[i]->start = NULL;
733103285Sikob		fc->ir[i]->start = NULL;
734103285Sikob
735103285Sikob		fc->it[i]->buf = NULL;
736103285Sikob		fc->ir[i]->buf = NULL;
737103285Sikob
738103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
739103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
740103285Sikob
741103285Sikob		STAILQ_INIT(&fc->it[i]->q);
742103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
743103285Sikob
744103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
745103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
746103285Sikob	}
747103285Sikob
748103285Sikob	fc->arq->maxq = FWMAXQUEUE;
749103285Sikob	fc->ars->maxq = FWMAXQUEUE;
750103285Sikob	fc->atq->maxq = FWMAXQUEUE;
751103285Sikob	fc->ats->maxq = FWMAXQUEUE;
752103285Sikob
753103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
754103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
755103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
756103285Sikob	}
757103285Sikob/* Initialize csr registers */
758103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
759103285Sikob				sizeof(struct fw_topology_map),
760103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
761103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
762103285Sikob				sizeof(struct fw_speed_map),
763103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
764103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
765103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
766103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
767103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
768103285Sikob
769103285Sikob	TAILQ_INIT(&fc->devices);
770103285Sikob	STAILQ_INIT(&fc->pending);
771103285Sikob
772103285Sikob/* Initialize csr ROM work space */
773103285Sikob	SLIST_INIT(&fc->ongocsr);
774103285Sikob	SLIST_INIT(&fc->csrfree);
775103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
776103285Sikob		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_DEVBUF,M_DONTWAIT);
777103285Sikob		if(csrd == NULL) break;
778103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
779103285Sikob	}
780103285Sikob
781103285Sikob/* Initialize Async handlers */
782103285Sikob	STAILQ_INIT(&fc->binds);
783103285Sikob	for( i = 0 ; i < 0x40 ; i++){
784103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
785103285Sikob	}
786103285Sikob
787103285Sikob/* DV depend CSRs see blue book */
788103285Sikob#if 0
789103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
790103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
791103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
792103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
793103285Sikob	}
794103285Sikob
795103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
796103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
797103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
798103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
799103285Sikob	}
800103285Sikob#endif
801103285Sikob
802103285Sikob
803106543Ssimokawa#ifdef FW_VMACCESS
804103285Sikob	xfer = fw_xfer_alloc();
805103285Sikob	if(xfer == NULL) return;
806103285Sikob
807103285Sikob	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
808103285Sikob	if(fwb == NULL){
809103285Sikob		fw_xfer_free(xfer);
810103285Sikob	}
811103285Sikob	xfer->act.hand = fw_vmaccess;
812103285Sikob	xfer->act_type = FWACT_XFER;
813103285Sikob	xfer->fc = fc;
814103285Sikob	xfer->sc = NULL;
815103285Sikob
816103285Sikob	fwb->start_hi = 0x2;
817103285Sikob	fwb->start_lo = 0;
818103285Sikob	fwb->addrlen = 0xffffffff;
819103285Sikob	fwb->xfer = xfer;
820103285Sikob	fw_bindadd(fc, fwb);
821106543Ssimokawa#endif
822103285Sikob}
823106790Ssimokawa
824103285Sikob/*
825103285Sikob * To lookup binded process from IEEE1394 address.
826103285Sikob */
827106813Ssimokawastruct fw_bind *
828106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
829103285Sikob{
830103285Sikob	struct fw_bind *tfw;
831103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
832103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
833103285Sikob		if(tfw->xfer->act_type != FWACT_NULL &&
834103285Sikob			tfw->start_hi == dest_hi &&
835103285Sikob			tfw->start_lo <= dest_lo &&
836103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
837103285Sikob			return(tfw);
838103285Sikob		}
839103285Sikob	}
840103285Sikob	return(NULL);
841103285Sikob}
842106790Ssimokawa
843103285Sikob/*
844103285Sikob * To bind IEEE1394 address block to process.
845103285Sikob */
846106790Ssimokawaint
847106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
848103285Sikob{
849103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
850103285Sikob	int err = 0;
851103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
852103285Sikob	if(tfw == NULL){
853103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
854103285Sikob		goto out;
855103285Sikob	}
856103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
857103285Sikob		(tfw->start_hi == fwb->start_hi &&
858103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
859103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
860103285Sikob		goto out;
861103285Sikob	}
862103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
863103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
864103285Sikob		   (tfw->start_hi == fwb->start_hi &&
865103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
866103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
867103285Sikob			if(tfw2 == NULL)
868103285Sikob				break;
869103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
870103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
871103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
872103285Sikob				break;
873103285Sikob			}else{
874103285Sikob				err = EBUSY;
875103285Sikob				goto out;
876103285Sikob			}
877103285Sikob		}
878103285Sikob	}
879103285Sikob	if(tfw != NULL){
880103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
881103285Sikob	}else{
882103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
883103285Sikob	}
884103285Sikobout:
885103285Sikob	if(!err && fwb->xfer->act_type == FWACT_CH){
886103285Sikob		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
887103285Sikob	}
888103285Sikob	return err;
889103285Sikob}
890103285Sikob
891103285Sikob/*
892103285Sikob * To free IEEE1394 address block.
893103285Sikob */
894106790Ssimokawaint
895106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
896103285Sikob{
897103285Sikob	int s;
898103285Sikob
899103285Sikob	s = splfw();
900103285Sikob	/* shall we check the existance? */
901103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
902103285Sikob	splx(s);
903103285Sikob	if (fwb->xfer)
904103285Sikob		fw_xfer_free(fwb->xfer);
905103285Sikob
906103285Sikob	return 0;
907103285Sikob}
908103285Sikob
909103285Sikob/*
910103285Sikob * To free transaction label.
911103285Sikob */
912106790Ssimokawastatic void
913106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
914103285Sikob{
915103285Sikob	struct tlabel *tl;
916103285Sikob	int s = splfw();
917103285Sikob
918103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
919103285Sikob		tl = STAILQ_NEXT(tl, link)){
920103285Sikob		if(tl->xfer == xfer){
921103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
922103285Sikob			free(tl, M_DEVBUF);
923103285Sikob			splx(s);
924103285Sikob			return;
925103285Sikob		}
926103285Sikob	}
927103285Sikob	splx(s);
928103285Sikob	return;
929103285Sikob}
930106790Ssimokawa
931103285Sikob/*
932103285Sikob * To obtain XFER structure by transaction label.
933103285Sikob */
934106790Ssimokawastatic struct fw_xfer *
935106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
936103285Sikob{
937103285Sikob	struct fw_xfer *xfer;
938103285Sikob	struct tlabel *tl;
939103285Sikob	int s = splfw();
940103285Sikob
941103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
942103285Sikob		tl = STAILQ_NEXT(tl, link)){
943103285Sikob		if(tl->xfer->dst == node){
944103285Sikob			xfer = tl->xfer;
945103285Sikob			splx(s);
946103285Sikob			return(xfer);
947103285Sikob		}
948103285Sikob	}
949103285Sikob	splx(s);
950103285Sikob	return(NULL);
951103285Sikob}
952106790Ssimokawa
953103285Sikob/*
954103285Sikob * To allocate IEEE1394 XFER structure.
955103285Sikob */
956106790Ssimokawastruct fw_xfer *
957106790Ssimokawafw_xfer_alloc()
958103285Sikob{
959103285Sikob	struct fw_xfer *xfer;
960106790Ssimokawa
961103285Sikob	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_DONTWAIT | M_ZERO);
962106790Ssimokawa	if (xfer == NULL)
963106790Ssimokawa		return xfer;
964106790Ssimokawa
965103285Sikob	xfer->time = time_second;
966103285Sikob	xfer->sub = -1;
967106790Ssimokawa
968103285Sikob	return xfer;
969103285Sikob}
970106790Ssimokawa
971103285Sikob/*
972103285Sikob * IEEE1394 XFER post process.
973103285Sikob */
974103285Sikobvoid
975103285Sikobfw_xfer_done(struct fw_xfer *xfer)
976103285Sikob{
977103285Sikob	if (xfer->act.hand == NULL)
978103285Sikob		return;
979103285Sikob
980103285Sikob#if XFER_TIMEOUT
981103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
982103285Sikob#endif
983103285Sikob
984103285Sikob	if (xfer->fc->status != FWBUSRESET)
985103285Sikob		xfer->act.hand(xfer);
986103285Sikob	else {
987103285Sikob		printf("fw_xfer_done: pending\n");
988103285Sikob		if (xfer->fc != NULL)
989103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
990103285Sikob		else
991103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
992103285Sikob	}
993103285Sikob}
994103285Sikob
995103285Sikob/*
996103285Sikob * To free IEEE1394 XFER structure.
997103285Sikob */
998106790Ssimokawavoid
999106790Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1000103285Sikob{
1001103285Sikob	int s;
1002103285Sikob	if(xfer == NULL ) return;
1003103285Sikob	if(xfer->state == FWXF_INQ){
1004103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
1005103285Sikob		s = splfw();
1006103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1007103285Sikob		xfer->q->queued --;
1008103285Sikob		splx(s);
1009103285Sikob	}
1010103285Sikob	if(xfer->fc != NULL){
1011103285Sikob		if(xfer->state == FWXF_START){
1012103285Sikob#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
1013103285Sikob			printf("fw_xfer_free FWXF_START\n");
1014103285Sikob#endif
1015103285Sikob			s = splfw();
1016103285Sikob			xfer->q->drain(xfer->fc, xfer);
1017103285Sikob			splx(s);
1018103285Sikob		}
1019103285Sikob	}
1020103285Sikob	if(xfer->send.buf != NULL){
1021103285Sikob		free(xfer->send.buf, M_DEVBUF);
1022103285Sikob	}
1023103285Sikob	if(xfer->recv.buf != NULL){
1024103285Sikob		free(xfer->recv.buf, M_DEVBUF);
1025103285Sikob	}
1026103285Sikob	if(xfer->fc != NULL){
1027103285Sikob		fw_tl_free(xfer->fc, xfer);
1028103285Sikob	}
1029103285Sikob	free(xfer, M_DEVBUF);
1030103285Sikob}
1031103285Sikob
1032103285Sikob/*
1033103285Sikob * Callback for PHY configuration.
1034103285Sikob */
1035103285Sikobstatic void
1036103285Sikobfw_phy_config_callback(struct fw_xfer *xfer)
1037103285Sikob{
1038103285Sikob#if 0
1039103285Sikob	printf("phy_config done state=%d resp=%d\n",
1040103285Sikob				xfer->state, xfer->resp);
1041103285Sikob#endif
1042103285Sikob	fw_xfer_free(xfer);
1043103285Sikob	/* XXX need bus reset ?? */
1044103285Sikob	/* sc->fc->ibr(xfer->fc);  LOOP */
1045103285Sikob}
1046103285Sikob
1047103285Sikob/*
1048103285Sikob * To configure PHY.
1049103285Sikob */
1050103285Sikobstatic void
1051103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1052103285Sikob{
1053103285Sikob	struct fw_xfer *xfer;
1054103285Sikob	struct fw_pkt *fp;
1055103285Sikob
1056103285Sikob	fc->status = FWBUSPHYCONF;
1057103285Sikob
1058103285Sikob	DELAY(100000);
1059103285Sikob	xfer = fw_xfer_alloc();
1060103285Sikob	xfer->send.len = 12;
1061103285Sikob	xfer->send.off = 0;
1062103285Sikob	xfer->fc = fc;
1063103285Sikob	xfer->retry_req = fw_asybusy;
1064103285Sikob	xfer->act.hand = fw_phy_config_callback;
1065103285Sikob
1066103285Sikob	xfer->send.buf = malloc(sizeof(u_int32_t),
1067103285Sikob					M_DEVBUF, M_DONTWAIT | M_ZERO);
1068103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1069103285Sikob	fp->mode.ld[1] = 0;
1070103285Sikob	if (root_node >= 0)
1071103285Sikob		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
1072103285Sikob	if (gap_count >= 0)
1073103285Sikob		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
1074103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1075103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1076103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1077103285Sikob
1078107653Ssimokawa	if (firewire_debug)
1079107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1080103285Sikob						root_node, gap_count);
1081103285Sikob	fw_asyreq(fc, -1, xfer);
1082103285Sikob}
1083103285Sikob
1084103285Sikob#if 0
1085103285Sikob/*
1086103285Sikob * Dump self ID.
1087103285Sikob */
1088103285Sikobstatic void
1089103285Sikobfw_print_sid(u_int32_t sid)
1090103285Sikob{
1091103285Sikob	union fw_self_id *s;
1092103285Sikob	s = (union fw_self_id *) &sid;
1093103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1094103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1095103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1096103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1097103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1098103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1099103285Sikob}
1100103285Sikob#endif
1101103285Sikob
1102103285Sikob/*
1103103285Sikob * To receive self ID.
1104103285Sikob */
1105103285Sikobvoid fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1106103285Sikob{
1107103285Sikob	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1108103285Sikob	union fw_self_id *self_id;
1109103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1110103285Sikob
1111103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1112103285Sikob	fc->status = FWBUSINIT;
1113103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1114103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1115103285Sikob	fc->status = FWBUSCYMELECT;
1116103285Sikob	fc->topology_map->crc_len = 2;
1117103285Sikob	fc->topology_map->generation ++;
1118103285Sikob	fc->topology_map->self_id_count = 0;
1119103285Sikob	fc->topology_map->node_count = 0;
1120103285Sikob	fc->speed_map->generation ++;
1121103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1122103285Sikob	self_id = &fc->topology_map->self_id[0];
1123103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1124103285Sikob		if (sid[1] != ~sid[0]) {
1125103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1126103285Sikob			sid += 2;
1127103285Sikob			continue;
1128103285Sikob		}
1129103285Sikob		*self_id = *((union fw_self_id *)sid);
1130103285Sikob		fc->topology_map->crc_len++;
1131103285Sikob		if(self_id->p0.sequel == 0){
1132103285Sikob			fc->topology_map->node_count ++;
1133103285Sikob			c_port = 0;
1134103285Sikob#if 0
1135103285Sikob			fw_print_sid(sid[0]);
1136103285Sikob#endif
1137103285Sikob			node = self_id->p0.phy_id;
1138103285Sikob			if(fc->max_node < node){
1139103285Sikob				fc->max_node = self_id->p0.phy_id;
1140103285Sikob			}
1141103285Sikob			/* XXX I'm not sure this is the right speed_map */
1142103285Sikob			fc->speed_map->speed[node][node]
1143103285Sikob					= self_id->p0.phy_speed;
1144103285Sikob			for (j = 0; j < node; j ++) {
1145103285Sikob				fc->speed_map->speed[j][node]
1146103285Sikob					= fc->speed_map->speed[node][j]
1147103285Sikob					= min(fc->speed_map->speed[j][j],
1148103285Sikob							self_id->p0.phy_speed);
1149103285Sikob			}
1150103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1151103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1152103285Sikob				fc->irm = self_id->p0.phy_id;
1153103285Sikob			}
1154103285Sikob			if(self_id->p0.port0 >= 0x2){
1155103285Sikob				c_port++;
1156103285Sikob			}
1157103285Sikob			if(self_id->p0.port1 >= 0x2){
1158103285Sikob				c_port++;
1159103285Sikob			}
1160103285Sikob			if(self_id->p0.port2 >= 0x2){
1161103285Sikob				c_port++;
1162103285Sikob			}
1163103285Sikob		}
1164103285Sikob		if(c_port > 2){
1165103285Sikob			i_branch += (c_port - 2);
1166103285Sikob		}
1167103285Sikob		sid += 2;
1168103285Sikob		self_id++;
1169103285Sikob		fc->topology_map->self_id_count ++;
1170103285Sikob	}
1171108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1172103285Sikob	/* CRC */
1173103285Sikob	fc->topology_map->crc = fw_crc16(
1174103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1175103285Sikob			fc->topology_map->crc_len * 4);
1176103285Sikob	fc->speed_map->crc = fw_crc16(
1177103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1178103285Sikob			fc->speed_map->crc_len * 4);
1179103285Sikob	/* byteswap and copy to CSR */
1180103285Sikob	p = (u_int32_t *)fc->topology_map;
1181103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1182103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1183103285Sikob	p = (u_int32_t *)fc->speed_map;
1184103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1185103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1186103285Sikob	/* don't byte-swap u_int8_t array */
1187103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1188103285Sikob
1189103285Sikob	fc->max_hop = fc->max_node - i_branch;
1190103285Sikob#if 1
1191103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1192103285Sikob#endif
1193103285Sikob
1194103285Sikob	if(fc->irm == -1 ){
1195103285Sikob		printf(", Not found IRM capable node");
1196103285Sikob	}else{
1197103285Sikob		printf(", cable IRM = %d", fc->irm);
1198103285Sikob		if (fc->irm == fc->nodeid)
1199103285Sikob			printf(" (me)\n");
1200103285Sikob		else
1201103285Sikob			printf("\n");
1202103285Sikob	}
1203103285Sikob
1204103285Sikob	if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){
1205103285Sikob		if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1206103285Sikob			fc->status = FWBUSMGRDONE;
1207103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1208103285Sikob		}else{
1209103285Sikob			fc->status = FWBUSMGRELECT;
1210103285Sikob			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8);
1211103285Sikob		}
1212103285Sikob	}else{
1213103285Sikob		fc->status = FWBUSMGRDONE;
1214108655Ssimokawa		device_printf(fc->bdev, "BMR = %x\n",
1215108655Ssimokawa				CSRARC(fc, BUS_MGR_ID));
1216103285Sikob	}
1217103285Sikob	free(buf, M_DEVBUF);
1218103285Sikob#if 1
1219103285Sikob	/* XXX optimize gap_count, if I am BMGR */
1220103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1221103285Sikob		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
1222103285Sikob	}
1223103285Sikob#endif
1224103285Sikob#if 1
1225108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1226108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1227103285Sikob#else
1228103285Sikob	fw_bus_probe(fc);
1229103285Sikob#endif
1230103285Sikob}
1231106790Ssimokawa
1232103285Sikob/*
1233103285Sikob * To probe devices on the IEEE1394 bus.
1234103285Sikob */
1235106790Ssimokawastatic void
1236106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1237103285Sikob{
1238103285Sikob	int s;
1239103285Sikob	struct fw_device *fwdev, *next;
1240103285Sikob
1241103285Sikob	s = splfw();
1242103285Sikob	fc->status = FWBUSEXPLORE;
1243103285Sikob	fc->retry_count = 0;
1244103285Sikob
1245103285Sikob/*
1246103285Sikob * Invalidate all devices, just after bus reset. Devices
1247103285Sikob * to be removed has not been seen longer time.
1248103285Sikob */
1249103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1250103285Sikob		next = TAILQ_NEXT(fwdev, link);
1251103285Sikob		if(fwdev->status != FWDEVINVAL){
1252103285Sikob			fwdev->status = FWDEVINVAL;
1253103285Sikob			fwdev->rcnt = 0;
1254103285Sikob		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
1255103285Sikob			fwdev->rcnt ++;
1256103285Sikob		}else{
1257103285Sikob			TAILQ_REMOVE(&fc->devices, fwdev, link);
1258103285Sikob			free(fwdev, M_DEVBUF);
1259103285Sikob		}
1260103285Sikob	}
1261103285Sikob	fc->ongonode = 0;
1262103285Sikob	fc->ongoaddr = CSRROMOFF;
1263103285Sikob	fc->ongodev = NULL;
1264103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1265103285Sikob	fw_bus_explore(fc);
1266103285Sikob	splx(s);
1267103285Sikob}
1268106790Ssimokawa
1269103285Sikob/*
1270103285Sikob * To collect device informations on the IEEE1394 bus.
1271103285Sikob */
1272106790Ssimokawastatic void
1273106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1274103285Sikob{
1275103285Sikob	int err = 0;
1276103285Sikob	struct fw_device *fwdev, *tfwdev;
1277103285Sikob	u_int32_t addr;
1278103285Sikob	struct fw_xfer *xfer;
1279103285Sikob	struct fw_pkt *fp;
1280103285Sikob
1281103285Sikob	if(fc->status != FWBUSEXPLORE)
1282103285Sikob		return;
1283103285Sikob
1284103285Sikobloop:
1285103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1286103285Sikob
1287103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1288103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1289103285Sikob
1290103285Sikob	/* check link */
1291103285Sikob	/* XXX we need to check phy_id first */
1292103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1293103285Sikob		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
1294103285Sikob		fc->ongonode++;
1295103285Sikob		goto loop;
1296103285Sikob	}
1297103285Sikob
1298103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1299103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1300103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1301103285Sikob		fc->ongoaddr = CSRROMOFF;
1302103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1303103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1304103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1305103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1306103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1307103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1308103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1309103285Sikob	}else if(fc->ongodev == NULL){
1310103285Sikob		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1311103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1312103285Sikob			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
1313103285Sikob				break;
1314103285Sikob			}
1315103285Sikob		}
1316103285Sikob		if(fwdev != NULL){
1317103285Sikob			fwdev->dst = fc->ongonode;
1318103285Sikob			fwdev->status = FWDEVATTACHED;
1319103285Sikob			fc->ongonode++;
1320103285Sikob			fc->ongoaddr = CSRROMOFF;
1321103285Sikob			fc->ongodev = NULL;
1322103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1323103285Sikob			goto loop;
1324103285Sikob		}
1325103285Sikob		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT);
1326103285Sikob		if(fwdev == NULL)
1327103285Sikob			return;
1328106810Ssimokawa		fwdev->fc = fc;
1329103285Sikob		fwdev->rommax = 0;
1330103285Sikob		fwdev->dst = fc->ongonode;
1331103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1332103285Sikob		fwdev->status = FWDEVINIT;
1333103285Sikob#if 0
1334103285Sikob		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1335103285Sikob			>> ((3 - (fc->ongonode % 4)) * 8);
1336103285Sikob#else
1337103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1338103285Sikob#endif
1339103285Sikob
1340103285Sikob		tfwdev = TAILQ_FIRST(&fc->devices);
1341103285Sikob		while( tfwdev != NULL &&
1342103285Sikob			(tfwdev->eui.hi > fwdev->eui.hi) &&
1343103285Sikob			((tfwdev->eui.hi == fwdev->eui.hi) &&
1344103285Sikob				tfwdev->eui.lo > fwdev->eui.lo)){
1345103285Sikob			tfwdev = TAILQ_NEXT( tfwdev, link);
1346103285Sikob		}
1347103285Sikob		if(tfwdev == NULL){
1348103285Sikob			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
1349103285Sikob		}else{
1350103285Sikob			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
1351103285Sikob		}
1352103285Sikob
1353108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1354107653Ssimokawa			linkspeed[fwdev->speed],
1355107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1356103285Sikob
1357103285Sikob		fc->ongodev = fwdev;
1358103285Sikob		fc->ongoaddr = CSRROMOFF;
1359103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1360103285Sikob	}else{
1361103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1362103285Sikob	}
1363103285Sikob#if 0
1364103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1365103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1366103285Sikob		fw_bus_explore_callback);
1367103285Sikob	if(xfer == NULL) goto done;
1368103285Sikob#else
1369103285Sikob	xfer = fw_xfer_alloc();
1370103285Sikob	if(xfer == NULL){
1371103285Sikob		goto done;
1372103285Sikob	}
1373103285Sikob	xfer->send.len = 16;
1374103285Sikob	xfer->spd = 0;
1375103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1376103285Sikob	if(xfer->send.buf == NULL){
1377103285Sikob		fw_xfer_free( xfer);
1378103285Sikob		return;
1379103285Sikob	}
1380103285Sikob
1381103285Sikob	xfer->send.off = 0;
1382103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1383103285Sikob	fp->mode.rreqq.dest_hi = htons(0xffff);
1384103285Sikob	fp->mode.rreqq.tlrt = 0;
1385103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1386103285Sikob	fp->mode.rreqq.pri = 0;
1387103285Sikob	fp->mode.rreqq.src = 0;
1388103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1389103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1390103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr);
1391103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1392103285Sikob
1393103285Sikob	err = fw_asyreq(fc, -1, xfer);
1394103285Sikob	if(err){
1395103285Sikob		fw_xfer_free( xfer);
1396103285Sikob		return;
1397103285Sikob	}
1398103285Sikob#endif
1399103285Sikob	return;
1400103285Sikobdone:
1401103285Sikob	/* fw_attach_devs */
1402103285Sikob	fc->status = FWBUSEXPDONE;
1403107653Ssimokawa	if (firewire_debug)
1404107653Ssimokawa		printf("bus_explore done\n");
1405103285Sikob	fw_attach_dev(fc);
1406103285Sikob	return;
1407103285Sikob
1408103285Sikob}
1409106790Ssimokawa
1410103285Sikob/* Portable Async. request read quad */
1411106790Ssimokawastruct fw_xfer *
1412106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1413105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1414105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1415103285Sikob{
1416103285Sikob	struct fw_xfer *xfer;
1417103285Sikob	struct fw_pkt *fp;
1418103285Sikob	int err;
1419103285Sikob
1420103285Sikob	xfer = fw_xfer_alloc();
1421103285Sikob	if(xfer == NULL){
1422103285Sikob		return NULL;
1423103285Sikob	}
1424103285Sikob	xfer->send.len = 16;
1425103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1426103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1427103285Sikob	if(xfer->send.buf == NULL){
1428103285Sikob		fw_xfer_free( xfer);
1429103285Sikob		return NULL;
1430103285Sikob	}
1431103285Sikob
1432103285Sikob	xfer->send.off = 0;
1433103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1434103285Sikob	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1435103285Sikob	if(tl & FWP_TL_VALID){
1436103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1437103285Sikob	}else{
1438103285Sikob		fp->mode.rreqq.tlrt = 0;
1439103285Sikob	}
1440103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1441103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1442103285Sikob	fp->mode.rreqq.pri = 0;
1443103285Sikob	fp->mode.rreqq.src = 0;
1444103285Sikob	xfer->dst = addr_hi >> 16;
1445103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1446103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1447103285Sikob	xfer->act.hand = hand;
1448103285Sikob
1449103285Sikob	err = fw_asyreq(fc, -1, xfer);
1450103285Sikob	if(err){
1451103285Sikob		fw_xfer_free( xfer);
1452103285Sikob		return NULL;
1453103285Sikob	}
1454103285Sikob	return xfer;
1455103285Sikob}
1456106790Ssimokawa
1457103285Sikob/*
1458103285Sikob * Callback for the IEEE1394 bus information collection.
1459103285Sikob */
1460106790Ssimokawastatic void
1461106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1462106790Ssimokawa{
1463103285Sikob	struct firewire_comm *fc;
1464103285Sikob	struct fw_pkt *sfp,*rfp;
1465103285Sikob	struct csrhdr *chdr;
1466103285Sikob	struct csrdir *csrd;
1467103285Sikob	struct csrreg *csrreg;
1468103285Sikob	u_int32_t offset;
1469103285Sikob
1470103285Sikob
1471103285Sikob	if(xfer == NULL) return;
1472103285Sikob	fc = xfer->fc;
1473103285Sikob	if(xfer->resp != 0){
1474103285Sikob		printf("resp != 0: node=%d addr=0x%x\n",
1475103285Sikob			fc->ongonode, fc->ongoaddr);
1476103285Sikob		fc->retry_count++;
1477103285Sikob		goto nextnode;
1478103285Sikob	}
1479103285Sikob
1480103285Sikob	if(xfer->send.buf == NULL){
1481103285Sikob		printf("send.buf == NULL: node=%d addr=0x%x\n",
1482103285Sikob			fc->ongonode, fc->ongoaddr);
1483103285Sikob		printf("send.buf == NULL\n");
1484103285Sikob		fc->retry_count++;
1485103285Sikob		goto nextnode;
1486103285Sikob	}
1487103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1488103285Sikob
1489103285Sikob	if(xfer->recv.buf == NULL){
1490103285Sikob		printf("recv.buf == NULL: node=%d addr=0x%x\n",
1491103285Sikob			fc->ongonode, fc->ongoaddr);
1492103285Sikob		fc->retry_count++;
1493103285Sikob		goto nextnode;
1494103285Sikob	}
1495103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1496103285Sikob#if 0
1497103285Sikob	{
1498103285Sikob		u_int32_t *qld;
1499103285Sikob		int i;
1500103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1501103285Sikob		printf("len:%d\n", xfer->recv.len);
1502103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1503103285Sikob			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1504103285Sikob			if((i % 16) == 15) printf("\n");
1505103285Sikob		}
1506103285Sikob		if((i % 16) != 15) printf("\n");
1507103285Sikob	}
1508103285Sikob#endif
1509103285Sikob	if(fc->ongodev == NULL){
1510103285Sikob		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1511103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1512103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1513103285Sikob/* If CSR is minimul confinguration, more investgation is not needed. */
1514103285Sikob			if(chdr->info_len == 1){
1515103285Sikob				goto nextnode;
1516103285Sikob			}else{
1517103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1518103285Sikob			}
1519103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1520103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1521103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1522103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1523103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1524103285Sikob			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
1525103285Sikob				goto nextnode;
1526103285Sikob			fc->ongoaddr = CSRROMOFF;
1527103285Sikob		}
1528103285Sikob	}else{
1529103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1530103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1531103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1532103285Sikob		}
1533103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1534103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1535103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1536103285Sikob			offset = CSRROMOFF;
1537103285Sikob		}else{
1538103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1539103285Sikob			offset = csrd->off;
1540103285Sikob		}
1541103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1542103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1543103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1544103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1545103285Sikob				if(csrd == NULL){
1546103285Sikob					goto nextnode;
1547103285Sikob				}else{
1548103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1549103285Sikob					fc->ongoaddr += csrreg->val * 4;
1550103285Sikob					csrd->off = fc->ongoaddr;
1551103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1552103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1553103285Sikob					goto nextaddr;
1554103285Sikob				}
1555103285Sikob			}
1556103285Sikob		}
1557103285Sikob		fc->ongoaddr += 4;
1558103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1559103285Sikob				(fc->ongodev->rommax < 0x414)){
1560103285Sikob			if(fc->ongodev->rommax <= 0x414){
1561103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1562103285Sikob				if(csrd == NULL) goto nextnode;
1563103285Sikob				csrd->off = fc->ongoaddr;
1564103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1565103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1566103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1567103285Sikob			}
1568103285Sikob			goto nextaddr;
1569103285Sikob		}
1570103285Sikob
1571103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1572103285Sikob			if(csrd == NULL){
1573103285Sikob				goto nextnode;
1574103285Sikob			};
1575103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1576103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1577103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1578103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1579103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1580103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1581103285Sikob				offset = CSRROMOFF;
1582103285Sikob			}else{
1583103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1584103285Sikob				offset = csrd->off;
1585103285Sikob			}
1586103285Sikob		}
1587103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1588103285Sikob			goto nextnode;
1589103285Sikob		}
1590103285Sikob	}
1591103285Sikobnextaddr:
1592103285Sikob	fw_xfer_free( xfer);
1593103285Sikob	fw_bus_explore(fc);
1594103285Sikob	return;
1595103285Sikobnextnode:
1596103285Sikob	fw_xfer_free( xfer);
1597103285Sikob	fc->ongonode++;
1598103285Sikob/* housekeeping work space */
1599103285Sikob	fc->ongoaddr = CSRROMOFF;
1600103285Sikob	fc->ongodev = NULL;
1601103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1602103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1603103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1604103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1605103285Sikob	}
1606103285Sikob	fw_bus_explore(fc);
1607103285Sikob	return;
1608103285Sikob}
1609103285Sikob
1610103285Sikob/*
1611103285Sikob * To obtain CSR register values.
1612103285Sikob */
1613106815Ssimokawau_int32_t
1614106815Ssimokawagetcsrdata(struct fw_device *fwdev, u_int8_t key)
1615103285Sikob{
1616103285Sikob	int i;
1617103285Sikob	struct csrhdr *chdr;
1618103285Sikob	struct csrreg *creg;
1619103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1620103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1621103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1622103285Sikob		if(creg->key == key){
1623103285Sikob			return (u_int32_t)creg->val;
1624103285Sikob		}
1625103285Sikob	}
1626103285Sikob	return 0;
1627103285Sikob}
1628106815Ssimokawa
1629103285Sikob/*
1630103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1631103285Sikob */
1632106815Ssimokawastatic void
1633106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1634103285Sikob{
1635103285Sikob	struct fw_device *fwdev;
1636103285Sikob	struct fw_xfer *xfer;
1637103285Sikob	int i, err;
1638103285Sikob	device_t *devlistp;
1639103285Sikob	int devcnt;
1640103285Sikob	struct firewire_dev_comm *fdc;
1641108701Ssimokawa	u_int32_t spec, ver;
1642103285Sikob
1643103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1644103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1645103285Sikob		if(fwdev->status == FWDEVINIT){
1646108701Ssimokawa			spec = getcsrdata(fwdev, CSRKEY_SPEC);
1647108701Ssimokawa			if(spec == 0)
1648103285Sikob				continue;
1649108701Ssimokawa			ver = getcsrdata(fwdev, CSRKEY_VER);
1650108701Ssimokawa			if(ver == 0)
1651103285Sikob				continue;
1652103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1653103285Sikob
1654108655Ssimokawa			device_printf(fc->bdev, "Device ");
1655108701Ssimokawa			switch(spec){
1656103285Sikob			case CSRVAL_ANSIT10:
1657108701Ssimokawa				switch(ver){
1658103285Sikob				case CSRVAL_T10SBP2:
1659107653Ssimokawa					printf("SBP-II");
1660103285Sikob					break;
1661103285Sikob				default:
1662103285Sikob					break;
1663103285Sikob				}
1664103285Sikob				break;
1665103285Sikob			case CSRVAL_1394TA:
1666108701Ssimokawa				switch(ver){
1667103285Sikob				case CSR_PROTAVC:
1668107653Ssimokawa					printf("AV/C");
1669103285Sikob					break;
1670103285Sikob				case CSR_PROTCAL:
1671107653Ssimokawa					printf("CAL");
1672103285Sikob					break;
1673103285Sikob				case CSR_PROTEHS:
1674107653Ssimokawa					printf("EHS");
1675103285Sikob					break;
1676103285Sikob				case CSR_PROTHAVI:
1677107653Ssimokawa					printf("HAVi");
1678103285Sikob					break;
1679103285Sikob				case CSR_PROTCAM104:
1680107653Ssimokawa					printf("1394 Cam 1.04");
1681103285Sikob					break;
1682103285Sikob				case CSR_PROTCAM120:
1683107653Ssimokawa					printf("1394 Cam 1.20");
1684103285Sikob					break;
1685103285Sikob				case CSR_PROTCAM130:
1686107653Ssimokawa					printf("1394 Cam 1.30");
1687103285Sikob					break;
1688103285Sikob				case CSR_PROTDPP:
1689107653Ssimokawa					printf("1394 Direct print");
1690103285Sikob					break;
1691103285Sikob				case CSR_PROTIICP:
1692107653Ssimokawa					printf("Industrial & Instrument");
1693103285Sikob					break;
1694103285Sikob				default:
1695107653Ssimokawa					printf("unknown 1394TA");
1696103285Sikob					break;
1697103285Sikob				}
1698103285Sikob				break;
1699103285Sikob			default:
1700107653Ssimokawa				printf("unknown spec");
1701103285Sikob				break;
1702103285Sikob			}
1703103285Sikob			fwdev->status = FWDEVATTACHED;
1704103285Sikob			printf("\n");
1705103285Sikob		}
1706103285Sikob	}
1707108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1708103285Sikob	if( err != 0 )
1709103285Sikob		return;
1710103285Sikob	for( i = 0 ; i < devcnt ; i++){
1711103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1712103285Sikob			fdc = device_get_softc(devlistp[i]);
1713103285Sikob			if (fdc->post_explore != NULL)
1714103285Sikob				fdc->post_explore(fdc);
1715103285Sikob		}
1716103285Sikob	}
1717103285Sikob	free(devlistp, M_TEMP);
1718103285Sikob
1719103285Sikob	/* call pending handlers */
1720103285Sikob	i = 0;
1721103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1722103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1723103285Sikob		i++;
1724103285Sikob		if (xfer->act.hand)
1725103285Sikob			xfer->act.hand(xfer);
1726103285Sikob	}
1727103285Sikob	if (i > 0)
1728103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1729103285Sikob	if (fc->retry_count > 0) {
1730103285Sikob		printf("retry_count = %d\n", fc->retry_count);
1731103285Sikob		fc->retry_probe_handle = timeout((timeout_t *)fc->ibr,
1732103285Sikob							(void *)fc, hz*2);
1733103285Sikob	}
1734103285Sikob	return;
1735103285Sikob}
1736106815Ssimokawa
1737103285Sikob/*
1738103285Sikob * To allocate uniq transaction label.
1739103285Sikob */
1740106815Ssimokawastatic int
1741106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1742103285Sikob{
1743103285Sikob	u_int i;
1744103285Sikob	struct tlabel *tl, *tmptl;
1745103285Sikob	int s;
1746103285Sikob	static u_int32_t label = 0;
1747103285Sikob
1748103285Sikob	s = splfw();
1749103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1750103285Sikob		label = (label + 1) & 0x3f;
1751103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1752103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1753103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1754103285Sikob		}
1755103285Sikob		if(tmptl == NULL) {
1756103285Sikob			tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_DONTWAIT);
1757103285Sikob			if (tl == NULL) {
1758103285Sikob				splx(s);
1759103285Sikob				return (-1);
1760103285Sikob			}
1761103285Sikob			tl->xfer = xfer;
1762103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1763103285Sikob			splx(s);
1764103285Sikob			return(label);
1765103285Sikob		}
1766103285Sikob	}
1767103285Sikob	splx(s);
1768103285Sikob
1769103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1770103285Sikob	return(-1);
1771103285Sikob}
1772106815Ssimokawa
1773103285Sikob/*
1774103285Sikob * Generic packet receving process.
1775103285Sikob */
1776106815Ssimokawavoid
1777106815Ssimokawafw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1778103285Sikob{
1779103285Sikob	struct fw_pkt *fp, *resfp;
1780103285Sikob	struct fw_xfer *xfer;
1781103285Sikob	struct fw_bind *bind;
1782103285Sikob	struct firewire_softc *sc;
1783103285Sikob	int s;
1784103285Sikob#if 0
1785103285Sikob	{
1786103285Sikob		u_int32_t *qld;
1787103285Sikob		int i;
1788103285Sikob		qld = (u_int32_t *)buf;
1789103285Sikob		printf("spd %d len:%d\n", spd, len);
1790103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1791103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1792103285Sikob			if((i % 16) == 15) printf("\n");
1793103285Sikob		}
1794103285Sikob		if((i % 16) != 15) printf("\n");
1795103285Sikob	}
1796103285Sikob#endif
1797103285Sikob	fp = (struct fw_pkt *)(buf + off);
1798103285Sikob	switch(fp->mode.common.tcode){
1799103285Sikob	case FWTCODE_WRES:
1800103285Sikob	case FWTCODE_RRESQ:
1801103285Sikob	case FWTCODE_RRESB:
1802103285Sikob	case FWTCODE_LRES:
1803103285Sikob		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1804103285Sikob					fp->mode.hdr.tlrt >> 2);
1805103285Sikob		if(xfer == NULL) {
1806103285Sikob			printf("fw_rcv: unknown response "
1807103285Sikob					"tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n",
1808103285Sikob					fp->mode.common.tcode,
1809103285Sikob					ntohs(fp->mode.hdr.src),
1810103285Sikob					fp->mode.hdr.tlrt >> 2,
1811103285Sikob					fp->mode.hdr.tlrt & 3,
1812103285Sikob					fp->mode.rresq.data);
1813103285Sikob#if 1
1814103285Sikob			printf("try ad-hoc work around!!\n");
1815103285Sikob			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1816103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1817103285Sikob			if (xfer == NULL) {
1818103285Sikob				printf("no use...\n");
1819103285Sikob				goto err;
1820103285Sikob			}
1821103285Sikob#else
1822103285Sikob			goto err;
1823103285Sikob#endif
1824103285Sikob		}
1825103285Sikob		switch(xfer->act_type){
1826103285Sikob		case FWACT_XFER:
1827103285Sikob			if((xfer->sub >= 0) &&
1828103285Sikob				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1829103285Sikob				xfer->resp = EINVAL;
1830103285Sikob				fw_xfer_done(xfer);
1831103285Sikob				goto err;
1832103285Sikob			}
1833103285Sikob			xfer->recv.len = len;
1834103285Sikob			xfer->recv.off = off;
1835103285Sikob			xfer->recv.buf = buf;
1836103285Sikob			xfer->resp = 0;
1837103285Sikob			fw_xfer_done(xfer);
1838103285Sikob			return;
1839103285Sikob			break;
1840103285Sikob		case FWACT_CH:
1841103285Sikob		default:
1842103285Sikob			goto err;
1843103285Sikob			break;
1844103285Sikob		}
1845103285Sikob		break;
1846103285Sikob	case FWTCODE_WREQQ:
1847103285Sikob	case FWTCODE_WREQB:
1848103285Sikob	case FWTCODE_RREQQ:
1849103285Sikob	case FWTCODE_RREQB:
1850103285Sikob	case FWTCODE_LREQ:
1851103285Sikob		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1852103285Sikob			ntohl(fp->mode.rreqq.dest_lo));
1853103285Sikob		if(bind == NULL){
1854108712Ssimokawa#if __FreeBSD_version >= 500000
1855103285Sikob			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
1856108712Ssimokawa#else
1857108712Ssimokawa			printf("Unknown service addr 0x%08x:0x%08lx tcode=%x\n",
1858108712Ssimokawa#endif
1859103285Sikob				ntohs(fp->mode.rreqq.dest_hi),
1860103285Sikob				ntohl(fp->mode.rreqq.dest_lo),
1861103285Sikob				fp->mode.common.tcode);
1862103285Sikob			if (fc->status == FWBUSRESET) {
1863103285Sikob				printf("fw_rcv: cannot response(bus reset)!\n");
1864103285Sikob				goto err;
1865103285Sikob			}
1866103285Sikob			xfer = fw_xfer_alloc();
1867103285Sikob			if(xfer == NULL){
1868103285Sikob				return;
1869103285Sikob			}
1870103285Sikob			xfer->spd = spd;
1871103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1872103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1873103285Sikob			switch(fp->mode.common.tcode){
1874103285Sikob			case FWTCODE_WREQQ:
1875103285Sikob			case FWTCODE_WREQB:
1876103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1877103285Sikob				xfer->send.len = 12;
1878103285Sikob				break;
1879103285Sikob			case FWTCODE_RREQQ:
1880103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1881103285Sikob				xfer->send.len = 16;
1882103285Sikob				break;
1883103285Sikob			case FWTCODE_RREQB:
1884103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1885103285Sikob				xfer->send.len = 16;
1886103285Sikob				break;
1887103285Sikob			case FWTCODE_LREQ:
1888103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1889103285Sikob				xfer->send.len = 16;
1890103285Sikob				break;
1891103285Sikob			}
1892103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1893103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1894103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1895103285Sikob			resfp->mode.rresb.rtcode = 7;
1896103285Sikob			resfp->mode.rresb.extcode = 0;
1897103285Sikob			resfp->mode.rresb.len = 0;
1898103285Sikob/*
1899103285Sikob			xfer->act.hand = fw_asy_callback;
1900103285Sikob*/
1901103285Sikob			xfer->act.hand = fw_xfer_free;
1902103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1903103285Sikob				fw_xfer_free( xfer);
1904103285Sikob				return;
1905103285Sikob			}
1906103285Sikob			goto err;
1907103285Sikob		}
1908103285Sikob		switch(bind->xfer->act_type){
1909103285Sikob		case FWACT_XFER:
1910103285Sikob			xfer = fw_xfer_alloc();
1911103285Sikob			if(xfer == NULL) goto err;
1912103285Sikob			xfer->fc = bind->xfer->fc;
1913103285Sikob			xfer->sc = bind->xfer->sc;
1914103285Sikob			xfer->recv.buf = buf;
1915103285Sikob			xfer->recv.len = len;
1916103285Sikob			xfer->recv.off = off;
1917103285Sikob			xfer->spd = spd;
1918103285Sikob			xfer->act.hand = bind->xfer->act.hand;
1919103285Sikob			if (fc->status != FWBUSRESET)
1920103285Sikob				xfer->act.hand(xfer);
1921103285Sikob			else
1922103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1923103285Sikob			return;
1924103285Sikob			break;
1925103285Sikob		case FWACT_CH:
1926103285Sikob			if(fc->ir[bind->xfer->sub]->queued >=
1927103285Sikob				fc->ir[bind->xfer->sub]->maxq){
1928108655Ssimokawa				device_printf(fc->bdev,
1929108655Ssimokawa					"Discard a packet %x %d\n",
1930103285Sikob					bind->xfer->sub,
1931103285Sikob					fc->ir[bind->xfer->sub]->queued);
1932103285Sikob				goto err;
1933103285Sikob			}
1934103285Sikob			xfer = fw_xfer_alloc();
1935103285Sikob			if(xfer == NULL) goto err;
1936103285Sikob			xfer->recv.buf = buf;
1937103285Sikob			xfer->recv.len = len;
1938103285Sikob			xfer->recv.off = off;
1939103285Sikob			xfer->spd = spd;
1940103285Sikob			s = splfw();
1941103285Sikob			fc->ir[bind->xfer->sub]->queued++;
1942103285Sikob			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1943103285Sikob			splx(s);
1944103285Sikob
1945103285Sikob			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1946103285Sikob
1947103285Sikob			return;
1948103285Sikob			break;
1949103285Sikob		default:
1950103285Sikob			goto err;
1951103285Sikob			break;
1952103285Sikob		}
1953103285Sikob		break;
1954103285Sikob	case FWTCODE_STREAM:
1955103285Sikob	{
1956103285Sikob		struct fw_xferq *xferq;
1957103285Sikob
1958103285Sikob		xferq = fc->ir[sub];
1959103285Sikob#if 0
1960103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1961103285Sikob			sub, len, off, spd);
1962103285Sikob#endif
1963103285Sikob		if(xferq->queued >= xferq->maxq) {
1964103285Sikob			printf("receive queue is full\n");
1965103285Sikob			goto err;
1966103285Sikob		}
1967103285Sikob		xfer = fw_xfer_alloc();
1968103285Sikob		if(xfer == NULL) goto err;
1969103285Sikob		xfer->recv.buf = buf;
1970103285Sikob		xfer->recv.len = len;
1971103285Sikob		xfer->recv.off = off;
1972103285Sikob		xfer->spd = spd;
1973103285Sikob		s = splfw();
1974103285Sikob		xferq->queued++;
1975103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1976103285Sikob		splx(s);
1977103285Sikob		sc = device_get_softc(fc->bdev);
1978103285Sikob#if __FreeBSD_version >= 500000
1979103285Sikob		if (SEL_WAITING(&xferq->rsel))
1980103285Sikob#else
1981103285Sikob		if (&xferq->rsel.si_pid != 0)
1982103285Sikob#endif
1983103285Sikob			selwakeup(&xferq->rsel);
1984103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1985103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1986103285Sikob			wakeup((caddr_t)xferq);
1987103285Sikob		}
1988103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1989103285Sikob			xferq->hand(xferq);
1990103285Sikob		}
1991103285Sikob		return;
1992103285Sikob		break;
1993103285Sikob	}
1994103285Sikob	default:
1995103285Sikob		printf("fw_rcv: unknow tcode\n");
1996103285Sikob		break;
1997103285Sikob	}
1998103285Sikoberr:
1999103285Sikob	free(buf, M_DEVBUF);
2000103285Sikob}
2001106815Ssimokawa
2002103285Sikob/*
2003103285Sikob * Post process for Bus Manager election process.
2004103285Sikob */
2005103285Sikobstatic void
2006103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
2007103285Sikob{
2008103285Sikob	struct fw_pkt *sfp,*rfp;
2009103285Sikob	struct firewire_comm *fc;
2010103285Sikob
2011103285Sikob	if(xfer == NULL) return;
2012103285Sikob	fc = xfer->fc;
2013103285Sikob	if(xfer->resp != 0){
2014103285Sikob		goto error;
2015103285Sikob	}
2016103285Sikob
2017103285Sikob	if(xfer->send.buf == NULL){
2018103285Sikob		goto error;
2019103285Sikob	}
2020103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
2021103285Sikob
2022103285Sikob	if(xfer->recv.buf == NULL){
2023103285Sikob		goto error;
2024103285Sikob	}
2025103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2026103285Sikob	CSRARC(fc, BUS_MGR_ID)
2027103285Sikob		= fc->set_bmr(fc, ntohl(rfp->mode.lres.payload[0]) & 0x3f);
2028108655Ssimokawa	device_printf(fc->bdev, "new bus manager %d ",
2029108655Ssimokawa		CSRARC(fc, BUS_MGR_ID));
2030103285Sikob	if((htonl(rfp->mode.lres.payload[0]) & 0x3f) == fc->nodeid){
2031103285Sikob		printf("(me)\n");
2032103285Sikob/* If I am bus manager, optimize gapcount */
2033103285Sikob		if(fc->max_hop <= MAX_GAPHOP ){
2034103285Sikob			fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
2035103285Sikob		}
2036103285Sikob	}else{
2037103285Sikob		printf("\n");
2038103285Sikob	}
2039103285Sikoberror:
2040103285Sikob	fw_xfer_free(xfer);
2041103285Sikob}
2042106815Ssimokawa
2043103285Sikob/*
2044103285Sikob * To candidate Bus Manager election process.
2045103285Sikob */
2046106815Ssimokawavoid
2047106815Ssimokawafw_try_bmr(void *arg)
2048103285Sikob{
2049103285Sikob	struct fw_xfer *xfer;
2050103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2051103285Sikob	struct fw_pkt *fp;
2052103285Sikob	int err = 0;
2053103285Sikob
2054103285Sikob	xfer = fw_xfer_alloc();
2055103285Sikob	if(xfer == NULL){
2056103285Sikob		return;
2057103285Sikob	}
2058103285Sikob	xfer->send.len = 24;
2059103285Sikob	xfer->spd = 0;
2060103285Sikob	xfer->send.buf = malloc(24, M_DEVBUF, M_DONTWAIT);
2061103285Sikob	if(xfer->send.buf == NULL){
2062103285Sikob		fw_xfer_free( xfer);
2063103285Sikob		return;
2064103285Sikob	}
2065103285Sikob
2066103285Sikob	fc->status = FWBUSMGRELECT;
2067103285Sikob
2068103285Sikob	xfer->send.off = 0;
2069103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2070103285Sikob	fp->mode.lreq.dest_hi = htons(0xffff);
2071103285Sikob	fp->mode.lreq.tlrt = 0;
2072103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2073103285Sikob	fp->mode.lreq.pri = 0;
2074103285Sikob	fp->mode.lreq.src = 0;
2075103285Sikob	fp->mode.lreq.len = htons(8);
2076103285Sikob	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
2077103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
2078103285Sikob	fp->mode.lreq.dst = htons(xfer->dst);
2079103285Sikob	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
2080103285Sikob	fp->mode.lreq.payload[0] = 0x3f;
2081103285Sikob	fp->mode.lreq.payload[1] = fc->nodeid;
2082103285Sikob	xfer->act_type = FWACT_XFER;
2083103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2084103285Sikob
2085103285Sikob	err = fw_asyreq(fc, -1, xfer);
2086103285Sikob	if(err){
2087103285Sikob		fw_xfer_free( xfer);
2088103285Sikob		return;
2089103285Sikob	}
2090103285Sikob	return;
2091103285Sikob}
2092106543Ssimokawa
2093106543Ssimokawa#ifdef FW_VMACCESS
2094103285Sikob/*
2095103285Sikob * Software implementation for physical memory block access.
2096103285Sikob * XXX:Too slow, usef for debug purpose only.
2097103285Sikob */
2098106815Ssimokawastatic void
2099106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2100103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2101103285Sikob	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
2102103285Sikob
2103103285Sikob	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
2104103285Sikob			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2105103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2106103285Sikob	if(xfer->resp != 0){
2107103285Sikob		fw_xfer_free( xfer);
2108103285Sikob		return;
2109103285Sikob	}
2110103285Sikob	if(xfer->recv.buf == NULL){
2111103285Sikob		fw_xfer_free( xfer);
2112103285Sikob		return;
2113103285Sikob	}
2114103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2115103285Sikob	switch(rfp->mode.hdr.tcode){
2116103285Sikob		/* XXX need fix for 64bit arch */
2117103285Sikob		case FWTCODE_WREQB:
2118103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
2119103285Sikob			xfer->send.len = 12;
2120103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2121103285Sikob			bcopy(rfp->mode.wreqb.payload,
2122103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2123103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2124103285Sikob			sfp->mode.wres.rtcode = 0;
2125103285Sikob			break;
2126103285Sikob		case FWTCODE_WREQQ:
2127103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
2128103285Sikob			xfer->send.len = 12;
2129103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2130103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2131103285Sikob			sfp->mode.wres.rtcode = 0;
2132103285Sikob			break;
2133103285Sikob		case FWTCODE_RREQB:
2134103285Sikob			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT);
2135103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2136103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2137103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2138103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2139103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2140103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2141103285Sikob			sfp->mode.rresb.rtcode = 0;
2142103285Sikob			sfp->mode.rresb.extcode = 0;
2143103285Sikob			break;
2144103285Sikob		case FWTCODE_RREQQ:
2145103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
2146103285Sikob			xfer->send.len = 16;
2147103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2148103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2149103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2150103285Sikob			sfp->mode.rresb.rtcode = 0;
2151103285Sikob			break;
2152103285Sikob		default:
2153103285Sikob			fw_xfer_free( xfer);
2154103285Sikob			return;
2155103285Sikob	}
2156103285Sikob	xfer->send.off = 0;
2157103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2158103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2159103285Sikob	xfer->act.hand = fw_xfer_free;
2160103285Sikob	xfer->retry_req = fw_asybusy;
2161103285Sikob
2162103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2163103285Sikob	sfp->mode.hdr.pri = 0;
2164103285Sikob
2165103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2166103285Sikob/**/
2167103285Sikob	return;
2168103285Sikob}
2169106543Ssimokawa#endif
2170106543Ssimokawa
2171103285Sikob/*
2172103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2173103285Sikob */
2174106815Ssimokawau_int16_t
2175106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2176103285Sikob	u_int32_t i, sum, crc = 0;
2177103285Sikob	int shift;
2178103285Sikob	len = (len + 3) & ~3;
2179103285Sikob	for(i = 0 ; i < len ; i+= 4){
2180103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2181103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2182103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2183103285Sikob		}
2184103285Sikob		crc &= 0xffff;
2185103285Sikob	}
2186103285Sikob	return((u_int16_t) crc);
2187103285Sikob}
2188106815Ssimokawa
2189103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2190103285SikobMODULE_VERSION(firewire, 1);
2191