firewire.c revision 107653
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 107653 2002-12-06 02:17:30Z 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;
61103285SikobSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "Firewire Subsystem");
62103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
63103285Sikob	"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),
98103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
99103285Sikob
100103285Sikob	/* Bus interface */
101103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
102103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
103103285Sikob
104103285Sikob	{ 0, 0 }
105103285Sikob};
106103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
107103285Sikobu_int maxrec[6]={512,1024,2048,4096,8192,0};
108103285Sikob
109103285Sikob#define MAX_GAPHOP  16
110103285Sikobu_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17,
111103285Sikob			20, 23, 25, 28, 31, 33, 36, 39, 42};
112106813Ssimokawa
113106813Ssimokawaextern struct cdevsw firewire_cdevsw;
114106813Ssimokawa
115103285Sikobstatic driver_t firewire_driver = {
116103285Sikob	"firewire",
117103285Sikob	firewire_methods,
118103285Sikob	sizeof(struct firewire_softc),
119103285Sikob};
120103285Sikob
121103285Sikob/*
122103285Sikob * transmitter buffer update.
123103285Sikob */
124103285Sikobint
125103285Sikobfw_tbuf_update(struct firewire_comm *fc, int sub, int flag){
126103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
127103285Sikob	struct fw_dvbuf *dvbuf = NULL;
128103285Sikob	struct fw_xferq *it;
129103285Sikob	int s, err = 0, i, j, chtag;
130103285Sikob	struct fw_pkt *fp;
131103285Sikob	u_int64_t tmpsync, dvsync;
132103285Sikob
133103285Sikob	it = fc->it[sub];
134103285Sikob
135103285Sikob	s = splfw();
136103285Sikob	if(it->stdma == NULL){
137103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
138103285Sikob	}else if(flag != 0){
139103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
140103285Sikob		if(bulkxfer == it->stdma){
141103285Sikob			STAILQ_REMOVE_HEAD(&it->stvalid, link);
142103285Sikob			it->stdma->flag = 0;
143103285Sikob			STAILQ_INSERT_TAIL(&it->stfree, it->stdma, link);
144103285Sikob			if(!(it->flag & FWXFERQ_DV))
145103285Sikob				wakeup(it);
146103285Sikob		}
147103285Sikob		bulkxfer = STAILQ_FIRST(&it->stvalid);
148103285Sikob	}else{
149103285Sikob		bulkxfer = it->stdma;
150103285Sikob	}
151103285Sikob	splx(s);
152103285Sikob	if(bulkxfer != NULL){
153103285Sikob		s = splfw();
154103285Sikob		bulkxfer2 = STAILQ_NEXT(bulkxfer, link);
155103285Sikob#if 0
156103285Sikob		if(it->flag & FWXFERQ_DV && bulkxfer2 == NULL){
157103285Sikob			bulkxfer2 = STAILQ_FIRST(&it->stfree);
158103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
159103285Sikob			splx(s);
160103285Sikob			bcopy(bulkxfer->buf, bulkxfer2->buf,
161103285Sikob					it->psize * it->btpacket);
162103285Sikob			s = splfw();
163103285Sikob			STAILQ_INSERT_TAIL(&it->stvalid, bulkxfer2, link);
164103285Sikob		}
165103285Sikob#endif
166103285Sikob		splx(s);
167103285Sikob	}
168103285Sikob	it->stdma = bulkxfer;
169103285Sikob	it->stdma2 = bulkxfer2;
170103285Sikob
171103285Sikob	if(it->flag & FWXFERQ_DV){
172103285Sikob		chtag = it->flag & 0xff;
173103285Sikobdvloop:
174103285Sikob		if(it->dvdma == NULL){
175103285Sikob			dvbuf = STAILQ_FIRST(&it->dvvalid);
176103285Sikob			if(dvbuf != NULL){
177103285Sikob				s = splfw();
178103285Sikob				STAILQ_REMOVE_HEAD(&it->dvvalid, link);
179103285Sikob				it->dvdma = dvbuf;
180103285Sikob				splx(s);
181103285Sikob				it->queued = 0;
182103285Sikob			}
183103285Sikob		}
184103285Sikob		if(it->dvdma == NULL)
185103285Sikob			return err;
186103285Sikob
187103285Sikob		it->stproc = STAILQ_FIRST(&it->stfree);
188103285Sikob		if(it->stproc != NULL){
189103285Sikob			s = splfw();
190103285Sikob			STAILQ_REMOVE_HEAD(&it->stfree, link);
191103285Sikob			splx(s);
192103285Sikob		}else{
193103285Sikob			return err;
194103285Sikob		}
195103285Sikob/*
196103285Sikob * Insert least significant 12 bits timestamp value by computation.
197103285Sikob * Highest significant 4 bits is insert at just before packet sending.
198103285Sikob */
199103285Sikob		fp = (struct fw_pkt *)(it->stproc->buf);
200103285Sikob/* XXX: Parameter relies on NTSC type DV video */
201103485Sikob		tmpsync = (u_int64_t)3072 * 8000 * 100 / 2997;
202103285Sikob		tmpsync *= it->dvsync;
203103285Sikob		dvsync = tmpsync;
204103285Sikob		dvsync %= 0xc00;
205103285Sikob		fp->mode.ld[2] = htonl(0x80000000 | (dvsync % 0xc00));
206103285Sikob		it->dvsync ++;
207103285Sikob		it->dvsync %= 2997;
208103285Sikob
209103285Sikob		for( i = 0, j = 0 ; i < it->dvpacket ; i++){
210103285Sikob			bcopy(it->dvdma->buf + it->queued * it->psize,
211103285Sikob				it->stproc->buf + j * it->psize, it->psize);
212103285Sikob			fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
213103285Sikob			fp->mode.stream.len = htons(488);
214103285Sikob			fp->mode.stream.chtag = chtag;
215103285Sikob			fp->mode.stream.tcode = FWTCODE_STREAM;
216103285Sikob			fp->mode.ld[1] = htonl((fc->nodeid << 24) | 0x00780000 | it->dvdbc);
217103285Sikob			it->dvdbc++;
218103285Sikob			it->dvdbc %= 256;
219103285Sikob			it->queued ++;
220103285Sikob			j++;
221103285Sikob/* XXX: Parameter relies on NTSC type DV video */
222103285Sikob#if 1
223103285Sikob#define DVDIFF 203
224103285Sikob#define DVFRAC 2997
225103285Sikob#else
226103285Sikob#define DVDIFF 127
227103285Sikob#define DVFRAC 1875
228103285Sikob#endif
229103285Sikob			it->dvdiff += DVDIFF;
230103285Sikob			if(it->dvdiff >= DVFRAC){
231103285Sikob				it->dvdiff %= DVFRAC;
232103285Sikob				fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize);
233103285Sikob
234103285Sikob				fp->mode.stream.len = htons(0x8);
235103285Sikob				fp->mode.stream.chtag = chtag;
236103285Sikob				fp->mode.stream.tcode = FWTCODE_STREAM;
237103285Sikob				fp->mode.ld[1] = htonl((fc->nodeid << 24) |
238103285Sikob					 0x00780000 | it->dvdbc);
239103285Sikob				j++;
240103285Sikob			}
241103285Sikob		}
242103285Sikob		it->stproc->npacket = j;
243103285Sikob		s = splfw();
244103285Sikob		STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link);
245103285Sikob		splx(s);
246103285Sikob		if(it->queued >= it->dvpacket){
247103285Sikob			s = splfw();
248103285Sikob			STAILQ_INSERT_TAIL(&it->dvfree, it->dvdma, link);
249103285Sikob			it->dvdma = NULL;
250103285Sikob			splx(s);
251103285Sikob			wakeup(it);
252103285Sikob			goto dvloop;
253103285Sikob		}
254103285Sikob	}
255103285Sikob	return err;
256103285Sikob}
257103285Sikob/*
258103285Sikob * receving buffer update.
259103285Sikob */
260103285Sikobint
261103285Sikobfw_rbuf_update(struct firewire_comm *fc, int sub, int flag){
262103285Sikob	struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL;
263103285Sikob	struct fw_xferq *ir;
264103285Sikob	int s, err = 0;
265103285Sikob
266103285Sikob	ir = fc->ir[sub];
267103285Sikob	s = splfw();
268103285Sikob	if(ir->stdma != NULL){
269103285Sikob		if(flag != 0){
270103285Sikob			STAILQ_INSERT_TAIL(&ir->stvalid, ir->stdma, link);
271103285Sikob		}else{
272103285Sikob			ir->stdma->flag = 0;
273103285Sikob			STAILQ_INSERT_TAIL(&ir->stfree, ir->stdma, link);
274103285Sikob		}
275103285Sikob	}
276103285Sikob	if(ir->stdma2 != NULL){
277103285Sikob		bulkxfer = ir->stdma2;
278103285Sikob		bulkxfer2 = STAILQ_FIRST(&ir->stfree);
279103285Sikob		if(bulkxfer2 != NULL){
280103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
281103285Sikob		}
282103285Sikob	}else{
283103285Sikob		bulkxfer = STAILQ_FIRST(&ir->stfree);
284103285Sikob		if(bulkxfer != NULL){
285103285Sikob			STAILQ_REMOVE_HEAD(&ir->stfree, link);
286103285Sikob			bulkxfer2 = STAILQ_FIRST(&ir->stfree);
287103285Sikob			if(bulkxfer2 != NULL){
288103285Sikob				STAILQ_REMOVE_HEAD(&ir->stfree, link);
289103285Sikob			}
290103285Sikob		}else{
291103285Sikob			bulkxfer = STAILQ_FIRST(&ir->stvalid);
292103285Sikob			STAILQ_REMOVE_HEAD(&ir->stvalid, link);
293103285Sikob		}
294103285Sikob	}
295103285Sikob	splx(s);
296103285Sikob	ir->stdma = bulkxfer;
297103285Sikob	ir->stdma2 = bulkxfer2;
298103285Sikob	return err;
299103285Sikob}
300103285Sikob
301103285Sikob/*
302103285Sikob * To lookup node id. from EUI64.
303103285Sikob */
304106810Ssimokawastruct fw_device *
305106790Ssimokawafw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui)
306103285Sikob{
307103285Sikob	struct fw_device *fwdev;
308103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
309103285Sikob		fwdev = TAILQ_NEXT(fwdev, link)){
310103285Sikob		if(fwdev->eui.hi == eui.hi && fwdev->eui.lo == eui.lo){
311103285Sikob			break;
312103285Sikob		}
313103285Sikob	}
314106810Ssimokawa	if(fwdev == NULL) return NULL;
315106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
316106810Ssimokawa	return fwdev;
317103285Sikob}
318106813Ssimokawa
319103285Sikob/*
320103285Sikob * Async. request procedure for userland application.
321103285Sikob */
322103285Sikobint
323103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
324103285Sikob{
325103285Sikob	int err = 0;
326103285Sikob	struct fw_xferq *xferq;
327103285Sikob	int tl = 0, len;
328103285Sikob	struct fw_pkt *fp;
329103285Sikob	int tcode;
330103285Sikob	struct tcode_info *info;
331103285Sikob
332103285Sikob	if(xfer == NULL) return EINVAL;
333103285Sikob	if(xfer->send.len > fc->maxrec){
334103285Sikob		printf("send.len > maxrec\n");
335103285Sikob		return EINVAL;
336103285Sikob	}
337103285Sikob	if(xfer->act.hand == NULL){
338103285Sikob		printf("act.hand == NULL\n");
339103285Sikob		return EINVAL;
340103285Sikob	}
341103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
342103285Sikob
343103285Sikob	tcode = fp->mode.common.tcode & 0xf;
344103285Sikob	info = &fc->tcode[tcode];
345103285Sikob	if (info->flag == 0) {
346103285Sikob		printf("invalid tcode=%d\n", tcode);
347103285Sikob		return EINVAL;
348103285Sikob	}
349103285Sikob	if (info->flag & FWTI_REQ)
350103285Sikob		xferq = fc->atq;
351103285Sikob	else
352103285Sikob		xferq = fc->ats;
353103285Sikob	len = info->hdr_len;
354103285Sikob	if (info->flag & FWTI_BLOCK_STR)
355103285Sikob		len += ntohs(fp->mode.stream.len);
356103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
357103285Sikob		len += ntohs(fp->mode.rresb.len);
358103285Sikob	if( len >  xfer->send.len ){
359103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
360103285Sikob				len, xfer->send.len, tcode);
361103285Sikob		return EINVAL;
362103285Sikob	}
363103285Sikob	xfer->send.len = len;
364106790Ssimokawa
365103285Sikob	if(xferq->start == NULL){
366103285Sikob		printf("xferq->start == NULL\n");
367103285Sikob		return EINVAL;
368103285Sikob	}
369103285Sikob	if(!(xferq->queued < xferq->maxq)){
370103285Sikob		printf("%s:Discard a packet (queued=%d)\n",
371103285Sikob			device_get_nameunit(fc->dev), xferq->queued);
372103285Sikob		return EINVAL;
373103285Sikob	}
374103285Sikob
375103285Sikob
376103285Sikob	if (info->flag & FWTI_TLABEL) {
377103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
378103285Sikob			return EIO;
379103285Sikob		fp->mode.hdr.tlrt = tl << 2;
380103285Sikob	}
381103285Sikob
382103285Sikob	xfer->tl = tl;
383103285Sikob	xfer->tcode = tcode;
384103285Sikob	xfer->resp = 0;
385103285Sikob	xfer->fc = fc;
386103285Sikob	xfer->q = xferq;
387103285Sikob	xfer->act_type = FWACT_XFER;
388103285Sikob	xfer->retry_req = fw_asybusy;
389103285Sikob
390103285Sikob	fw_asystart(xfer);
391103285Sikob	return err;
392103285Sikob}
393103285Sikob/*
394103285Sikob * Wakeup blocked process.
395103285Sikob */
396103285Sikobvoid
397103285Sikobfw_asy_callback(struct fw_xfer *xfer){
398103285Sikob	wakeup(xfer);
399103285Sikob	return;
400103285Sikob}
401103285Sikob/*
402103285Sikob * Postpone to later retry.
403103285Sikob */
404103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
405106815Ssimokawa#if 1
406103285Sikob	printf("fw_asybusy\n");
407103285Sikob#endif
408103285Sikob#if XFER_TIMEOUT
409103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
410103285Sikob#endif
411103285Sikob/*
412103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
413103285Sikob*/
414103285Sikob	DELAY(20000);
415103285Sikob	fw_asystart(xfer);
416103285Sikob	return;
417103285Sikob}
418103285Sikob#if XFER_TIMEOUT
419103285Sikob/*
420103285Sikob * Post timeout for async. request.
421103285Sikob */
422103285Sikobvoid
423103285Sikobfw_xfer_timeout(void *arg)
424103285Sikob{
425103285Sikob	int s;
426103285Sikob	struct fw_xfer *xfer;
427103285Sikob
428103285Sikob	xfer = (struct fw_xfer *)arg;
429103285Sikob	printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp);
430103285Sikob	/* XXX set error code */
431103285Sikob	s = splfw();
432103285Sikob	xfer->act.hand(xfer);
433103285Sikob	splx(s);
434103285Sikob}
435103285Sikob#endif
436103285Sikob/*
437103285Sikob * Async. request with given xfer structure.
438103285Sikob */
439106790Ssimokawastatic void
440106790Ssimokawafw_asystart(struct fw_xfer *xfer)
441106790Ssimokawa{
442103285Sikob	struct firewire_comm *fc = xfer->fc;
443103285Sikob	int s;
444103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
445103285Sikob		xfer->resp = EBUSY;
446103285Sikob		xfer->state = FWXF_BUSY;
447103285Sikob		xfer->act.hand(xfer);
448103285Sikob		return;
449103285Sikob	}
450103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
451103285Sikob	if (fc->status < FWBUSEXPLORE) {
452103285Sikob		xfer->resp = EAGAIN;
453103285Sikob		xfer->state = FWXF_BUSY;
454103285Sikob		if (xfer->act.hand != NULL)
455103285Sikob			xfer->act.hand(xfer);
456103285Sikob		return;
457103285Sikob	}
458103285Sikob#endif
459103285Sikob	s = splfw();
460103285Sikob	xfer->state = FWXF_INQ;
461103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
462103285Sikob	xfer->q->queued ++;
463103285Sikob	splx(s);
464103285Sikob	/* XXX just queue for mbuf */
465103285Sikob	if (xfer->mbuf == NULL)
466103285Sikob		xfer->q->start(fc);
467103285Sikob#if XFER_TIMEOUT
468103285Sikob	if (xfer->act.hand != NULL)
469103285Sikob		xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz);
470103285Sikob#endif
471103285Sikob	return;
472103285Sikob}
473106790Ssimokawa
474103285Sikobstatic int
475103285Sikobfirewire_match( device_t dev )
476103285Sikob{
477103285Sikob	device_set_desc(dev, "IEEE1394(Firewire) bus");
478103285Sikob	return -140;
479103285Sikob}
480106790Ssimokawa
481103285Sikob/*
482103285Sikob * The attach routine.
483103285Sikob */
484103285Sikobstatic int
485103285Sikobfirewire_attach( device_t dev )
486103285Sikob{
487103285Sikob	int i, unitmask, mn;
488103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
489103285Sikob	device_t pa = device_get_parent(dev);
490103285Sikob	struct firewire_comm *fc;
491103285Sikob	dev_t d;
492103285Sikob
493103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
494103285Sikob	sc->fc = fc;
495103285Sikob	sc->fc->dev = dev;
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),
504103285Sikob			UID_ROOT, GID_OPERATOR, 0770,
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),
516103285Sikob			UID_ROOT, GID_OPERATOR, 0770,
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
525103285Sikob	/* Locate our children */
526103285Sikob	bus_generic_probe(dev);
527103285Sikob
528103285Sikob	/* launch attachement of the added children */
529103285Sikob	bus_generic_attach(dev);
530103285Sikob
531103285Sikob	/* bus_reset */
532103285Sikob	fc->ibr(fc);
533103285Sikob
534103285Sikob	return 0;
535103285Sikob}
536103285Sikob
537103285Sikob/*
538103285Sikob * Attach it as child.
539103285Sikob */
540103285Sikobstatic device_t
541103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
542103285Sikob{
543103285Sikob        device_t child;
544103285Sikob	struct firewire_softc *sc;
545103285Sikob
546103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
547103285Sikob	child = device_add_child(dev, name, unit);
548103285Sikob	if (child) {
549103285Sikob		device_set_ivars(child, sc->fc);
550103285Sikob		device_probe_and_attach(child);
551103285Sikob	}
552103285Sikob
553103285Sikob	return child;
554103285Sikob}
555106790Ssimokawa
556103285Sikob/*
557103285Sikob * Dettach it.
558103285Sikob */
559103285Sikobstatic int
560103285Sikobfirewire_detach( device_t dev )
561103285Sikob{
562103285Sikob	struct firewire_softc *sc;
563103285Sikob
564103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
565106790Ssimokawa
566103285Sikob#if __FreeBSD_version >= 500000
567103285Sikob	destroy_dev(sc->dev);
568103285Sikob#else
569103285Sikob	{
570103285Sikob		int j;
571103285Sikob		for (j = 0 ; j < sc->fc->nisodma + 1; j++)
572103285Sikob			destroy_dev(sc->dev[j]);
573103285Sikob	}
574103285Sikob#endif
575103285Sikob	/* XXX xfree_free and untimeout on all xfers */
576103285Sikob	untimeout((timeout_t *)sc->fc->timeout, sc->fc, sc->fc->timeouthandle);
577103285Sikob	free(sc->fc->topology_map, M_DEVBUF);
578103285Sikob	free(sc->fc->speed_map, M_DEVBUF);
579103285Sikob	bus_generic_detach(dev);
580103285Sikob	return(0);
581103285Sikob}
582103285Sikob#if 0
583103285Sikobstatic int
584103285Sikobfirewire_shutdown( device_t dev )
585103285Sikob{
586103285Sikob	return 0;
587103285Sikob}
588103285Sikob#endif
589106790Ssimokawa
590103285Sikob/*
591106790Ssimokawa * Called after bus reset.
592103285Sikob */
593106790Ssimokawavoid
594106790Ssimokawafw_busreset(struct firewire_comm *fc)
595103285Sikob{
596103285Sikob	int i;
597103285Sikob	struct fw_xfer *xfer;
598103285Sikob
599103285Sikob	switch(fc->status){
600103285Sikob	case FWBUSMGRELECT:
601103285Sikob		untimeout((timeout_t *)fw_try_bmr, (void *)fc, fc->bmrhandle);
602103285Sikob		break;
603103285Sikob	default:
604103285Sikob		break;
605103285Sikob	}
606103285Sikob	fc->status = FWBUSRESET;
607103285Sikob/* XXX: discard all queued packet */
608103285Sikob	while((xfer = STAILQ_FIRST(&fc->atq->q)) != NULL){
609103285Sikob		STAILQ_REMOVE_HEAD(&fc->atq->q, link);
610103285Sikob		xfer->resp = EAGAIN;
611103285Sikob		switch(xfer->act_type){
612103285Sikob		case FWACT_XFER:
613103285Sikob			fw_xfer_done(xfer);
614103285Sikob			break;
615103285Sikob		default:
616103285Sikob			break;
617103285Sikob		}
618103285Sikob		fw_xfer_free( xfer);
619103285Sikob	}
620103285Sikob	while((xfer = STAILQ_FIRST(&fc->ats->q)) != NULL){
621103285Sikob		STAILQ_REMOVE_HEAD(&fc->ats->q, link);
622103285Sikob		xfer->resp = EAGAIN;
623103285Sikob		switch(xfer->act_type){
624103285Sikob		case FWACT_XFER:
625103285Sikob			fw_xfer_done(xfer);
626103285Sikob		default:
627103285Sikob			break;
628103285Sikob		}
629103285Sikob		fw_xfer_free( xfer);
630103285Sikob	}
631103285Sikob	for(i = 0; i < fc->nisodma; i++)
632103285Sikob		while((xfer = STAILQ_FIRST(&fc->it[i]->q)) != NULL){
633103285Sikob			STAILQ_REMOVE_HEAD(&fc->it[i]->q, link);
634103285Sikob			xfer->resp = 0;
635103285Sikob			switch(xfer->act_type){
636103285Sikob			case FWACT_XFER:
637103285Sikob				fw_xfer_done(xfer);
638103285Sikob				break;
639103285Sikob			default:
640103285Sikob				break;
641103285Sikob			}
642103285Sikob			fw_xfer_free( xfer);
643103285Sikob		}
644103285Sikob
645103285Sikob	CSRARC(fc, STATE_CLEAR)
646103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
647103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
648103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
649103285Sikob
650103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
651103285Sikob	fc->irm = -1;
652103285Sikob
653103285Sikob	fc->max_node = -1;
654103285Sikob
655103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
656103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
657103285Sikob	}
658103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
659103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
660103285Sikob	CSRARC(fc, RESET_START) = 0;
661103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
662103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
663103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
664103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
665103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
666103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
667103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
668103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
669103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
670103285Sikob
671103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
672103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
673103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
674103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
675103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
676103285Sikob
677103285Sikob/* DV depend CSRs see blue book */
678103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
679103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
680103285Sikob
681103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
682103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
683103285Sikob}
684106790Ssimokawa
685103285Sikob/* Call once after reboot */
686106790Ssimokawavoid fw_init(struct firewire_comm *fc)
687103285Sikob{
688103285Sikob	int i;
689106543Ssimokawa	struct csrdir *csrd;
690106543Ssimokawa#ifdef FW_VMACCESS
691103285Sikob	struct fw_xfer *xfer;
692103285Sikob	struct fw_bind *fwb;
693106543Ssimokawa#endif
694103285Sikob
695103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
696103285Sikob
697103285Sikob	fc->arq->queued = 0;
698103285Sikob	fc->ars->queued = 0;
699103285Sikob	fc->atq->queued = 0;
700103285Sikob	fc->ats->queued = 0;
701103285Sikob
702103285Sikob	fc->arq->psize = FWPMAX_S400;
703103285Sikob	fc->ars->psize = FWPMAX_S400;
704103285Sikob	fc->atq->psize = FWPMAX_S400;
705103285Sikob	fc->ats->psize = FWPMAX_S400;
706103285Sikob
707103285Sikob
708103285Sikob	fc->arq->buf = NULL;
709103285Sikob	fc->ars->buf = NULL;
710103285Sikob	fc->atq->buf = NULL;
711103285Sikob	fc->ats->buf = NULL;
712103285Sikob
713103285Sikob	fc->arq->flag = FWXFERQ_PACKET;
714103285Sikob	fc->ars->flag = FWXFERQ_PACKET;
715103285Sikob	fc->atq->flag = FWXFERQ_PACKET;
716103285Sikob	fc->ats->flag = FWXFERQ_PACKET;
717103285Sikob
718103285Sikob	STAILQ_INIT(&fc->atq->q);
719103285Sikob	STAILQ_INIT(&fc->ats->q);
720103285Sikob
721103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
722103285Sikob		fc->it[i]->queued = 0;
723103285Sikob		fc->ir[i]->queued = 0;
724103285Sikob
725103285Sikob		fc->it[i]->start = NULL;
726103285Sikob		fc->ir[i]->start = NULL;
727103285Sikob
728103285Sikob		fc->it[i]->buf = NULL;
729103285Sikob		fc->ir[i]->buf = NULL;
730103285Sikob
731103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
732103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
733103285Sikob
734103285Sikob		STAILQ_INIT(&fc->it[i]->q);
735103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
736103285Sikob
737103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
738103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
739103285Sikob	}
740103285Sikob
741103285Sikob	fc->arq->maxq = FWMAXQUEUE;
742103285Sikob	fc->ars->maxq = FWMAXQUEUE;
743103285Sikob	fc->atq->maxq = FWMAXQUEUE;
744103285Sikob	fc->ats->maxq = FWMAXQUEUE;
745103285Sikob
746103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
747103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
748103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
749103285Sikob	}
750103285Sikob/* Initialize csr registers */
751103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
752103285Sikob				sizeof(struct fw_topology_map),
753103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
754103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
755103285Sikob				sizeof(struct fw_speed_map),
756103285Sikob				M_DEVBUF, M_DONTWAIT | M_ZERO);
757103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
758103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
759103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
760103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
761103285Sikob
762103285Sikob	TAILQ_INIT(&fc->devices);
763103285Sikob	STAILQ_INIT(&fc->pending);
764103285Sikob
765103285Sikob/* Initialize csr ROM work space */
766103285Sikob	SLIST_INIT(&fc->ongocsr);
767103285Sikob	SLIST_INIT(&fc->csrfree);
768103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
769103285Sikob		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_DEVBUF,M_DONTWAIT);
770103285Sikob		if(csrd == NULL) break;
771103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
772103285Sikob	}
773103285Sikob
774103285Sikob/* Initialize Async handlers */
775103285Sikob	STAILQ_INIT(&fc->binds);
776103285Sikob	for( i = 0 ; i < 0x40 ; i++){
777103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
778103285Sikob	}
779103285Sikob
780103285Sikob/* DV depend CSRs see blue book */
781103285Sikob#if 0
782103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
783103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
784103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
785103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
786103285Sikob	}
787103285Sikob
788103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
789103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
790103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
791103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
792103285Sikob	}
793103285Sikob#endif
794103285Sikob
795103285Sikob
796106543Ssimokawa#ifdef FW_VMACCESS
797103285Sikob	xfer = fw_xfer_alloc();
798103285Sikob	if(xfer == NULL) return;
799103285Sikob
800103285Sikob	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_DEVBUF, M_DONTWAIT);
801103285Sikob	if(fwb == NULL){
802103285Sikob		fw_xfer_free(xfer);
803103285Sikob	}
804103285Sikob	xfer->act.hand = fw_vmaccess;
805103285Sikob	xfer->act_type = FWACT_XFER;
806103285Sikob	xfer->fc = fc;
807103285Sikob	xfer->sc = NULL;
808103285Sikob
809103285Sikob	fwb->start_hi = 0x2;
810103285Sikob	fwb->start_lo = 0;
811103285Sikob	fwb->addrlen = 0xffffffff;
812103285Sikob	fwb->xfer = xfer;
813103285Sikob	fw_bindadd(fc, fwb);
814106543Ssimokawa#endif
815103285Sikob}
816106790Ssimokawa
817103285Sikob/*
818103285Sikob * To lookup binded process from IEEE1394 address.
819103285Sikob */
820106813Ssimokawastruct fw_bind *
821106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
822103285Sikob{
823103285Sikob	struct fw_bind *tfw;
824103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
825103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
826103285Sikob		if(tfw->xfer->act_type != FWACT_NULL &&
827103285Sikob			tfw->start_hi == dest_hi &&
828103285Sikob			tfw->start_lo <= dest_lo &&
829103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
830103285Sikob			return(tfw);
831103285Sikob		}
832103285Sikob	}
833103285Sikob	return(NULL);
834103285Sikob}
835106790Ssimokawa
836103285Sikob/*
837103285Sikob * To bind IEEE1394 address block to process.
838103285Sikob */
839106790Ssimokawaint
840106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
841103285Sikob{
842103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
843103285Sikob	int err = 0;
844103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
845103285Sikob	if(tfw == NULL){
846103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
847103285Sikob		goto out;
848103285Sikob	}
849103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
850103285Sikob		(tfw->start_hi == fwb->start_hi &&
851103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
852103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
853103285Sikob		goto out;
854103285Sikob	}
855103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
856103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
857103285Sikob		   (tfw->start_hi == fwb->start_hi &&
858103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
859103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
860103285Sikob			if(tfw2 == NULL)
861103285Sikob				break;
862103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
863103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
864103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
865103285Sikob				break;
866103285Sikob			}else{
867103285Sikob				err = EBUSY;
868103285Sikob				goto out;
869103285Sikob			}
870103285Sikob		}
871103285Sikob	}
872103285Sikob	if(tfw != NULL){
873103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
874103285Sikob	}else{
875103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
876103285Sikob	}
877103285Sikobout:
878103285Sikob	if(!err && fwb->xfer->act_type == FWACT_CH){
879103285Sikob		STAILQ_INSERT_HEAD(&fc->ir[fwb->xfer->sub]->binds, fwb, chlist);
880103285Sikob	}
881103285Sikob	return err;
882103285Sikob}
883103285Sikob
884103285Sikob/*
885103285Sikob * To free IEEE1394 address block.
886103285Sikob */
887106790Ssimokawaint
888106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
889103285Sikob{
890103285Sikob	int s;
891103285Sikob
892103285Sikob	s = splfw();
893103285Sikob	/* shall we check the existance? */
894103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
895103285Sikob	splx(s);
896103285Sikob	if (fwb->xfer)
897103285Sikob		fw_xfer_free(fwb->xfer);
898103285Sikob
899103285Sikob	return 0;
900103285Sikob}
901103285Sikob
902103285Sikob/*
903103285Sikob * To free transaction label.
904103285Sikob */
905106790Ssimokawastatic void
906106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
907103285Sikob{
908103285Sikob	struct tlabel *tl;
909103285Sikob	int s = splfw();
910103285Sikob
911103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
912103285Sikob		tl = STAILQ_NEXT(tl, link)){
913103285Sikob		if(tl->xfer == xfer){
914103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
915103285Sikob			free(tl, M_DEVBUF);
916103285Sikob			splx(s);
917103285Sikob			return;
918103285Sikob		}
919103285Sikob	}
920103285Sikob	splx(s);
921103285Sikob	return;
922103285Sikob}
923106790Ssimokawa
924103285Sikob/*
925103285Sikob * To obtain XFER structure by transaction label.
926103285Sikob */
927106790Ssimokawastatic struct fw_xfer *
928106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
929103285Sikob{
930103285Sikob	struct fw_xfer *xfer;
931103285Sikob	struct tlabel *tl;
932103285Sikob	int s = splfw();
933103285Sikob
934103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
935103285Sikob		tl = STAILQ_NEXT(tl, link)){
936103285Sikob		if(tl->xfer->dst == node){
937103285Sikob			xfer = tl->xfer;
938103285Sikob			splx(s);
939103285Sikob			return(xfer);
940103285Sikob		}
941103285Sikob	}
942103285Sikob	splx(s);
943103285Sikob	return(NULL);
944103285Sikob}
945106790Ssimokawa
946103285Sikob/*
947103285Sikob * To allocate IEEE1394 XFER structure.
948103285Sikob */
949106790Ssimokawastruct fw_xfer *
950106790Ssimokawafw_xfer_alloc()
951103285Sikob{
952103285Sikob	struct fw_xfer *xfer;
953106790Ssimokawa
954103285Sikob	xfer = malloc(sizeof(struct fw_xfer), M_DEVBUF, M_DONTWAIT | M_ZERO);
955106790Ssimokawa	if (xfer == NULL)
956106790Ssimokawa		return xfer;
957106790Ssimokawa
958103285Sikob	xfer->time = time_second;
959103285Sikob	xfer->sub = -1;
960106790Ssimokawa
961103285Sikob	return xfer;
962103285Sikob}
963106790Ssimokawa
964103285Sikob/*
965103285Sikob * IEEE1394 XFER post process.
966103285Sikob */
967103285Sikobvoid
968103285Sikobfw_xfer_done(struct fw_xfer *xfer)
969103285Sikob{
970103285Sikob	if (xfer->act.hand == NULL)
971103285Sikob		return;
972103285Sikob
973103285Sikob#if XFER_TIMEOUT
974103285Sikob	untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch);
975103285Sikob#endif
976103285Sikob
977103285Sikob	if (xfer->fc->status != FWBUSRESET)
978103285Sikob		xfer->act.hand(xfer);
979103285Sikob	else {
980103285Sikob		printf("fw_xfer_done: pending\n");
981103285Sikob		if (xfer->fc != NULL)
982103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
983103285Sikob		else
984103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
985103285Sikob	}
986103285Sikob}
987103285Sikob
988103285Sikob/*
989103285Sikob * To free IEEE1394 XFER structure.
990103285Sikob */
991106790Ssimokawavoid
992106790Ssimokawafw_xfer_free( struct fw_xfer* xfer)
993103285Sikob{
994103285Sikob	int s;
995103285Sikob	if(xfer == NULL ) return;
996103285Sikob	if(xfer->state == FWXF_INQ){
997103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
998103285Sikob		s = splfw();
999103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1000103285Sikob		xfer->q->queued --;
1001103285Sikob		splx(s);
1002103285Sikob	}
1003103285Sikob	if(xfer->fc != NULL){
1004103285Sikob		if(xfer->state == FWXF_START){
1005103285Sikob#if 0 /* this could happen if we call fwohci_arcv() before fwohci_txd() */
1006103285Sikob			printf("fw_xfer_free FWXF_START\n");
1007103285Sikob#endif
1008103285Sikob			s = splfw();
1009103285Sikob			xfer->q->drain(xfer->fc, xfer);
1010103285Sikob			splx(s);
1011103285Sikob		}
1012103285Sikob	}
1013103285Sikob	if(xfer->send.buf != NULL){
1014103285Sikob		free(xfer->send.buf, M_DEVBUF);
1015103285Sikob	}
1016103285Sikob	if(xfer->recv.buf != NULL){
1017103285Sikob		free(xfer->recv.buf, M_DEVBUF);
1018103285Sikob	}
1019103285Sikob	if(xfer->fc != NULL){
1020103285Sikob		fw_tl_free(xfer->fc, xfer);
1021103285Sikob	}
1022103285Sikob	free(xfer, M_DEVBUF);
1023103285Sikob}
1024103285Sikob
1025103285Sikob/*
1026103285Sikob * Callback for PHY configuration.
1027103285Sikob */
1028103285Sikobstatic void
1029103285Sikobfw_phy_config_callback(struct fw_xfer *xfer)
1030103285Sikob{
1031103285Sikob#if 0
1032103285Sikob	printf("phy_config done state=%d resp=%d\n",
1033103285Sikob				xfer->state, xfer->resp);
1034103285Sikob#endif
1035103285Sikob	fw_xfer_free(xfer);
1036103285Sikob	/* XXX need bus reset ?? */
1037103285Sikob	/* sc->fc->ibr(xfer->fc);  LOOP */
1038103285Sikob}
1039103285Sikob
1040103285Sikob/*
1041103285Sikob * To configure PHY.
1042103285Sikob */
1043103285Sikobstatic void
1044103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1045103285Sikob{
1046103285Sikob	struct fw_xfer *xfer;
1047103285Sikob	struct fw_pkt *fp;
1048103285Sikob
1049103285Sikob	fc->status = FWBUSPHYCONF;
1050103285Sikob
1051103285Sikob	DELAY(100000);
1052103285Sikob	xfer = fw_xfer_alloc();
1053103285Sikob	xfer->send.len = 12;
1054103285Sikob	xfer->send.off = 0;
1055103285Sikob	xfer->fc = fc;
1056103285Sikob	xfer->retry_req = fw_asybusy;
1057103285Sikob	xfer->act.hand = fw_phy_config_callback;
1058103285Sikob
1059103285Sikob	xfer->send.buf = malloc(sizeof(u_int32_t),
1060103285Sikob					M_DEVBUF, M_DONTWAIT | M_ZERO);
1061103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1062103285Sikob	fp->mode.ld[1] = 0;
1063103285Sikob	if (root_node >= 0)
1064103285Sikob		fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23);
1065103285Sikob	if (gap_count >= 0)
1066103285Sikob		fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16);
1067103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1068103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1069103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1070103285Sikob
1071107653Ssimokawa	if (firewire_debug)
1072107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1073103285Sikob						root_node, gap_count);
1074103285Sikob	fw_asyreq(fc, -1, xfer);
1075103285Sikob}
1076103285Sikob
1077103285Sikob#if 0
1078103285Sikob/*
1079103285Sikob * Dump self ID.
1080103285Sikob */
1081103285Sikobstatic void
1082103285Sikobfw_print_sid(u_int32_t sid)
1083103285Sikob{
1084103285Sikob	union fw_self_id *s;
1085103285Sikob	s = (union fw_self_id *) &sid;
1086103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1087103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1088103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1089103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1090103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1091103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1092103285Sikob}
1093103285Sikob#endif
1094103285Sikob
1095103285Sikob/*
1096103285Sikob * To receive self ID.
1097103285Sikob */
1098103285Sikobvoid fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off)
1099103285Sikob{
1100103285Sikob	u_int32_t *p, *sid = (u_int32_t *)(buf + off);
1101103285Sikob	union fw_self_id *self_id;
1102103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1103103285Sikob
1104103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1105103285Sikob	fc->status = FWBUSINIT;
1106103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1107103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1108103285Sikob	fc->status = FWBUSCYMELECT;
1109103285Sikob	fc->topology_map->crc_len = 2;
1110103285Sikob	fc->topology_map->generation ++;
1111103285Sikob	fc->topology_map->self_id_count = 0;
1112103285Sikob	fc->topology_map->node_count = 0;
1113103285Sikob	fc->speed_map->generation ++;
1114103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1115103285Sikob	self_id = &fc->topology_map->self_id[0];
1116103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1117103285Sikob		if (sid[1] != ~sid[0]) {
1118103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1119103285Sikob			sid += 2;
1120103285Sikob			continue;
1121103285Sikob		}
1122103285Sikob		*self_id = *((union fw_self_id *)sid);
1123103285Sikob		fc->topology_map->crc_len++;
1124103285Sikob		if(self_id->p0.sequel == 0){
1125103285Sikob			fc->topology_map->node_count ++;
1126103285Sikob			c_port = 0;
1127103285Sikob#if 0
1128103285Sikob			fw_print_sid(sid[0]);
1129103285Sikob#endif
1130103285Sikob			node = self_id->p0.phy_id;
1131103285Sikob			if(fc->max_node < node){
1132103285Sikob				fc->max_node = self_id->p0.phy_id;
1133103285Sikob			}
1134103285Sikob			/* XXX I'm not sure this is the right speed_map */
1135103285Sikob			fc->speed_map->speed[node][node]
1136103285Sikob					= self_id->p0.phy_speed;
1137103285Sikob			for (j = 0; j < node; j ++) {
1138103285Sikob				fc->speed_map->speed[j][node]
1139103285Sikob					= fc->speed_map->speed[node][j]
1140103285Sikob					= min(fc->speed_map->speed[j][j],
1141103285Sikob							self_id->p0.phy_speed);
1142103285Sikob			}
1143103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1144103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1145103285Sikob				fc->irm = self_id->p0.phy_id;
1146103285Sikob			}
1147103285Sikob			if(self_id->p0.port0 >= 0x2){
1148103285Sikob				c_port++;
1149103285Sikob			}
1150103285Sikob			if(self_id->p0.port1 >= 0x2){
1151103285Sikob				c_port++;
1152103285Sikob			}
1153103285Sikob			if(self_id->p0.port2 >= 0x2){
1154103285Sikob				c_port++;
1155103285Sikob			}
1156103285Sikob		}
1157103285Sikob		if(c_port > 2){
1158103285Sikob			i_branch += (c_port - 2);
1159103285Sikob		}
1160103285Sikob		sid += 2;
1161103285Sikob		self_id++;
1162103285Sikob		fc->topology_map->self_id_count ++;
1163103285Sikob	}
1164103285Sikob	printf("%s: %d nodes", device_get_nameunit(fc->dev), fc->max_node + 1);
1165103285Sikob	/* CRC */
1166103285Sikob	fc->topology_map->crc = fw_crc16(
1167103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1168103285Sikob			fc->topology_map->crc_len * 4);
1169103285Sikob	fc->speed_map->crc = fw_crc16(
1170103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1171103285Sikob			fc->speed_map->crc_len * 4);
1172103285Sikob	/* byteswap and copy to CSR */
1173103285Sikob	p = (u_int32_t *)fc->topology_map;
1174103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1175103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1176103285Sikob	p = (u_int32_t *)fc->speed_map;
1177103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1178103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1179103285Sikob	/* don't byte-swap u_int8_t array */
1180103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1181103285Sikob
1182103285Sikob	fc->max_hop = fc->max_node - i_branch;
1183103285Sikob#if 1
1184103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1185103285Sikob#endif
1186103285Sikob
1187103285Sikob	if(fc->irm == -1 ){
1188103285Sikob		printf(", Not found IRM capable node");
1189103285Sikob	}else{
1190103285Sikob		printf(", cable IRM = %d", fc->irm);
1191103285Sikob		if (fc->irm == fc->nodeid)
1192103285Sikob			printf(" (me)\n");
1193103285Sikob		else
1194103285Sikob			printf("\n");
1195103285Sikob	}
1196103285Sikob
1197103285Sikob	if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){
1198103285Sikob		if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1199103285Sikob			fc->status = FWBUSMGRDONE;
1200103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1201103285Sikob		}else{
1202103285Sikob			fc->status = FWBUSMGRELECT;
1203103285Sikob			fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8);
1204103285Sikob		}
1205103285Sikob	}else{
1206103285Sikob		fc->status = FWBUSMGRDONE;
1207103285Sikob		printf("%s: BMR = %x\n", device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID));
1208103285Sikob	}
1209103285Sikob	free(buf, M_DEVBUF);
1210103285Sikob#if 1
1211103285Sikob	/* XXX optimize gap_count, if I am BMGR */
1212103285Sikob	if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){
1213103285Sikob		fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
1214103285Sikob	}
1215103285Sikob#endif
1216103285Sikob#if 1
1217103285Sikob	timeout((timeout_t *)fw_bus_probe, (void *)fc, hz/4);
1218103285Sikob#else
1219103285Sikob	fw_bus_probe(fc);
1220103285Sikob#endif
1221103285Sikob}
1222106790Ssimokawa
1223103285Sikob/*
1224103285Sikob * To probe devices on the IEEE1394 bus.
1225103285Sikob */
1226106790Ssimokawastatic void
1227106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1228103285Sikob{
1229103285Sikob	int s;
1230103285Sikob	struct fw_device *fwdev, *next;
1231103285Sikob
1232103285Sikob	s = splfw();
1233103285Sikob	fc->status = FWBUSEXPLORE;
1234103285Sikob	fc->retry_count = 0;
1235103285Sikob
1236103285Sikob/*
1237103285Sikob * Invalidate all devices, just after bus reset. Devices
1238103285Sikob * to be removed has not been seen longer time.
1239103285Sikob */
1240103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1241103285Sikob		next = TAILQ_NEXT(fwdev, link);
1242103285Sikob		if(fwdev->status != FWDEVINVAL){
1243103285Sikob			fwdev->status = FWDEVINVAL;
1244103285Sikob			fwdev->rcnt = 0;
1245103285Sikob		}else if(fwdev->rcnt < FW_MAXDEVRCNT){
1246103285Sikob			fwdev->rcnt ++;
1247103285Sikob		}else{
1248103285Sikob			TAILQ_REMOVE(&fc->devices, fwdev, link);
1249103285Sikob			free(fwdev, M_DEVBUF);
1250103285Sikob		}
1251103285Sikob	}
1252103285Sikob	fc->ongonode = 0;
1253103285Sikob	fc->ongoaddr = CSRROMOFF;
1254103285Sikob	fc->ongodev = NULL;
1255103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1256103285Sikob	fw_bus_explore(fc);
1257103285Sikob	splx(s);
1258103285Sikob}
1259106790Ssimokawa
1260103285Sikob/*
1261103285Sikob * To collect device informations on the IEEE1394 bus.
1262103285Sikob */
1263106790Ssimokawastatic void
1264106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1265103285Sikob{
1266103285Sikob	int err = 0;
1267103285Sikob	struct fw_device *fwdev, *tfwdev;
1268103285Sikob	u_int32_t addr;
1269103285Sikob	struct fw_xfer *xfer;
1270103285Sikob	struct fw_pkt *fp;
1271103285Sikob
1272103285Sikob	if(fc->status != FWBUSEXPLORE)
1273103285Sikob		return;
1274103285Sikob
1275103285Sikobloop:
1276103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1277103285Sikob
1278103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1279103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1280103285Sikob
1281103285Sikob	/* check link */
1282103285Sikob	/* XXX we need to check phy_id first */
1283103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1284103285Sikob		printf("fw_bus_explore: node %d link down\n", fc->ongonode);
1285103285Sikob		fc->ongonode++;
1286103285Sikob		goto loop;
1287103285Sikob	}
1288103285Sikob
1289103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1290103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1291103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1292103285Sikob		fc->ongoaddr = CSRROMOFF;
1293103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1294103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1295103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1296103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1297103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1298103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1299103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1300103285Sikob	}else if(fc->ongodev == NULL){
1301103285Sikob		for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1302103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1303103285Sikob			if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){
1304103285Sikob				break;
1305103285Sikob			}
1306103285Sikob		}
1307103285Sikob		if(fwdev != NULL){
1308103285Sikob			fwdev->dst = fc->ongonode;
1309103285Sikob			fwdev->status = FWDEVATTACHED;
1310103285Sikob			fc->ongonode++;
1311103285Sikob			fc->ongoaddr = CSRROMOFF;
1312103285Sikob			fc->ongodev = NULL;
1313103285Sikob			fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1314103285Sikob			goto loop;
1315103285Sikob		}
1316103285Sikob		fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT);
1317103285Sikob		if(fwdev == NULL)
1318103285Sikob			return;
1319106810Ssimokawa		fwdev->fc = fc;
1320103285Sikob		fwdev->rommax = 0;
1321103285Sikob		fwdev->dst = fc->ongonode;
1322103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1323103285Sikob		fwdev->status = FWDEVINIT;
1324103285Sikob#if 0
1325103285Sikob		fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4)
1326103285Sikob			>> ((3 - (fc->ongonode % 4)) * 8);
1327103285Sikob#else
1328103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1329103285Sikob#endif
1330103285Sikob
1331103285Sikob		tfwdev = TAILQ_FIRST(&fc->devices);
1332103285Sikob		while( tfwdev != NULL &&
1333103285Sikob			(tfwdev->eui.hi > fwdev->eui.hi) &&
1334103285Sikob			((tfwdev->eui.hi == fwdev->eui.hi) &&
1335103285Sikob				tfwdev->eui.lo > fwdev->eui.lo)){
1336103285Sikob			tfwdev = TAILQ_NEXT( tfwdev, link);
1337103285Sikob		}
1338103285Sikob		if(tfwdev == NULL){
1339103285Sikob			TAILQ_INSERT_TAIL(&fc->devices, fwdev, link);
1340103285Sikob		}else{
1341103285Sikob			TAILQ_INSERT_BEFORE(tfwdev, fwdev, link);
1342103285Sikob		}
1343103285Sikob
1344107653Ssimokawa		device_printf(fc->dev, "New %s device ID:%08x%08x\n",
1345107653Ssimokawa			linkspeed[fwdev->speed],
1346107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1347103285Sikob
1348103285Sikob		fc->ongodev = fwdev;
1349103285Sikob		fc->ongoaddr = CSRROMOFF;
1350103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1351103285Sikob	}else{
1352103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1353103285Sikob	}
1354103285Sikob#if 0
1355103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1356103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1357103285Sikob		fw_bus_explore_callback);
1358103285Sikob	if(xfer == NULL) goto done;
1359103285Sikob#else
1360103285Sikob	xfer = fw_xfer_alloc();
1361103285Sikob	if(xfer == NULL){
1362103285Sikob		goto done;
1363103285Sikob	}
1364103285Sikob	xfer->send.len = 16;
1365103285Sikob	xfer->spd = 0;
1366103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1367103285Sikob	if(xfer->send.buf == NULL){
1368103285Sikob		fw_xfer_free( xfer);
1369103285Sikob		return;
1370103285Sikob	}
1371103285Sikob
1372103285Sikob	xfer->send.off = 0;
1373103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1374103285Sikob	fp->mode.rreqq.dest_hi = htons(0xffff);
1375103285Sikob	fp->mode.rreqq.tlrt = 0;
1376103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1377103285Sikob	fp->mode.rreqq.pri = 0;
1378103285Sikob	fp->mode.rreqq.src = 0;
1379103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1380103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1381103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr);
1382103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1383103285Sikob
1384103285Sikob	err = fw_asyreq(fc, -1, xfer);
1385103285Sikob	if(err){
1386103285Sikob		fw_xfer_free( xfer);
1387103285Sikob		return;
1388103285Sikob	}
1389103285Sikob#endif
1390103285Sikob	return;
1391103285Sikobdone:
1392103285Sikob	/* fw_attach_devs */
1393103285Sikob	fc->status = FWBUSEXPDONE;
1394107653Ssimokawa	if (firewire_debug)
1395107653Ssimokawa		printf("bus_explore done\n");
1396103285Sikob	fw_attach_dev(fc);
1397103285Sikob	return;
1398103285Sikob
1399103285Sikob}
1400106790Ssimokawa
1401103285Sikob/* Portable Async. request read quad */
1402106790Ssimokawastruct fw_xfer *
1403106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1404105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1405105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1406103285Sikob{
1407103285Sikob	struct fw_xfer *xfer;
1408103285Sikob	struct fw_pkt *fp;
1409103285Sikob	int err;
1410103285Sikob
1411103285Sikob	xfer = fw_xfer_alloc();
1412103285Sikob	if(xfer == NULL){
1413103285Sikob		return NULL;
1414103285Sikob	}
1415103285Sikob	xfer->send.len = 16;
1416103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1417103285Sikob	xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1418103285Sikob	if(xfer->send.buf == NULL){
1419103285Sikob		fw_xfer_free( xfer);
1420103285Sikob		return NULL;
1421103285Sikob	}
1422103285Sikob
1423103285Sikob	xfer->send.off = 0;
1424103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1425103285Sikob	fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff);
1426103285Sikob	if(tl & FWP_TL_VALID){
1427103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1428103285Sikob	}else{
1429103285Sikob		fp->mode.rreqq.tlrt = 0;
1430103285Sikob	}
1431103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1432103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1433103285Sikob	fp->mode.rreqq.pri = 0;
1434103285Sikob	fp->mode.rreqq.src = 0;
1435103285Sikob	xfer->dst = addr_hi >> 16;
1436103285Sikob	fp->mode.rreqq.dst = htons(xfer->dst);
1437103285Sikob	fp->mode.rreqq.dest_lo = htonl(addr_lo);
1438103285Sikob	xfer->act.hand = hand;
1439103285Sikob
1440103285Sikob	err = fw_asyreq(fc, -1, xfer);
1441103285Sikob	if(err){
1442103285Sikob		fw_xfer_free( xfer);
1443103285Sikob		return NULL;
1444103285Sikob	}
1445103285Sikob	return xfer;
1446103285Sikob}
1447106790Ssimokawa
1448103285Sikob/*
1449103285Sikob * Callback for the IEEE1394 bus information collection.
1450103285Sikob */
1451106790Ssimokawastatic void
1452106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1453106790Ssimokawa{
1454103285Sikob	struct firewire_comm *fc;
1455103285Sikob	struct fw_pkt *sfp,*rfp;
1456103285Sikob	struct csrhdr *chdr;
1457103285Sikob	struct csrdir *csrd;
1458103285Sikob	struct csrreg *csrreg;
1459103285Sikob	u_int32_t offset;
1460103285Sikob
1461103285Sikob
1462103285Sikob	if(xfer == NULL) return;
1463103285Sikob	fc = xfer->fc;
1464103285Sikob	if(xfer->resp != 0){
1465103285Sikob		printf("resp != 0: node=%d addr=0x%x\n",
1466103285Sikob			fc->ongonode, fc->ongoaddr);
1467103285Sikob		fc->retry_count++;
1468103285Sikob		goto nextnode;
1469103285Sikob	}
1470103285Sikob
1471103285Sikob	if(xfer->send.buf == NULL){
1472103285Sikob		printf("send.buf == NULL: node=%d addr=0x%x\n",
1473103285Sikob			fc->ongonode, fc->ongoaddr);
1474103285Sikob		printf("send.buf == NULL\n");
1475103285Sikob		fc->retry_count++;
1476103285Sikob		goto nextnode;
1477103285Sikob	}
1478103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1479103285Sikob
1480103285Sikob	if(xfer->recv.buf == NULL){
1481103285Sikob		printf("recv.buf == NULL: node=%d addr=0x%x\n",
1482103285Sikob			fc->ongonode, fc->ongoaddr);
1483103285Sikob		fc->retry_count++;
1484103285Sikob		goto nextnode;
1485103285Sikob	}
1486103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1487103285Sikob#if 0
1488103285Sikob	{
1489103285Sikob		u_int32_t *qld;
1490103285Sikob		int i;
1491103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1492103285Sikob		printf("len:%d\n", xfer->recv.len);
1493103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1494103285Sikob			printf("0x%08x ", ntohl(rfp->mode.ld[i/4]));
1495103285Sikob			if((i % 16) == 15) printf("\n");
1496103285Sikob		}
1497103285Sikob		if((i % 16) != 15) printf("\n");
1498103285Sikob	}
1499103285Sikob#endif
1500103285Sikob	if(fc->ongodev == NULL){
1501103285Sikob		if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){
1502103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1503103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1504103285Sikob/* If CSR is minimul confinguration, more investgation is not needed. */
1505103285Sikob			if(chdr->info_len == 1){
1506103285Sikob				goto nextnode;
1507103285Sikob			}else{
1508103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1509103285Sikob			}
1510103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){
1511103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1512103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1513103285Sikob		}else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){
1514103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1515103285Sikob			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0)
1516103285Sikob				goto nextnode;
1517103285Sikob			fc->ongoaddr = CSRROMOFF;
1518103285Sikob		}
1519103285Sikob	}else{
1520103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1521103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1522103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1523103285Sikob		}
1524103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1525103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1526103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1527103285Sikob			offset = CSRROMOFF;
1528103285Sikob		}else{
1529103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1530103285Sikob			offset = csrd->off;
1531103285Sikob		}
1532103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1533103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1534103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1535103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1536103285Sikob				if(csrd == NULL){
1537103285Sikob					goto nextnode;
1538103285Sikob				}else{
1539103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1540103285Sikob					fc->ongoaddr += csrreg->val * 4;
1541103285Sikob					csrd->off = fc->ongoaddr;
1542103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1543103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1544103285Sikob					goto nextaddr;
1545103285Sikob				}
1546103285Sikob			}
1547103285Sikob		}
1548103285Sikob		fc->ongoaddr += 4;
1549103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1550103285Sikob				(fc->ongodev->rommax < 0x414)){
1551103285Sikob			if(fc->ongodev->rommax <= 0x414){
1552103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1553103285Sikob				if(csrd == NULL) goto nextnode;
1554103285Sikob				csrd->off = fc->ongoaddr;
1555103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1556103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1557103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1558103285Sikob			}
1559103285Sikob			goto nextaddr;
1560103285Sikob		}
1561103285Sikob
1562103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1563103285Sikob			if(csrd == NULL){
1564103285Sikob				goto nextnode;
1565103285Sikob			};
1566103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1567103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1568103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1569103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1570103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1571103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1572103285Sikob				offset = CSRROMOFF;
1573103285Sikob			}else{
1574103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1575103285Sikob				offset = csrd->off;
1576103285Sikob			}
1577103285Sikob		}
1578103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1579103285Sikob			goto nextnode;
1580103285Sikob		}
1581103285Sikob	}
1582103285Sikobnextaddr:
1583103285Sikob	fw_xfer_free( xfer);
1584103285Sikob	fw_bus_explore(fc);
1585103285Sikob	return;
1586103285Sikobnextnode:
1587103285Sikob	fw_xfer_free( xfer);
1588103285Sikob	fc->ongonode++;
1589103285Sikob/* housekeeping work space */
1590103285Sikob	fc->ongoaddr = CSRROMOFF;
1591103285Sikob	fc->ongodev = NULL;
1592103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1593103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1594103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1595103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1596103285Sikob	}
1597103285Sikob	fw_bus_explore(fc);
1598103285Sikob	return;
1599103285Sikob}
1600103285Sikob
1601103285Sikob/*
1602103285Sikob * To obtain CSR register values.
1603103285Sikob */
1604106815Ssimokawau_int32_t
1605106815Ssimokawagetcsrdata(struct fw_device *fwdev, u_int8_t key)
1606103285Sikob{
1607103285Sikob	int i;
1608103285Sikob	struct csrhdr *chdr;
1609103285Sikob	struct csrreg *creg;
1610103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
1611103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){
1612103285Sikob		creg = (struct csrreg *)&fwdev->csrrom[i/4];
1613103285Sikob		if(creg->key == key){
1614103285Sikob			return (u_int32_t)creg->val;
1615103285Sikob		}
1616103285Sikob	}
1617103285Sikob	return 0;
1618103285Sikob}
1619106815Ssimokawa
1620103285Sikob/*
1621103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1622103285Sikob */
1623106815Ssimokawastatic void
1624106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1625103285Sikob{
1626103285Sikob	struct fw_device *fwdev;
1627103285Sikob	struct fw_xfer *xfer;
1628103285Sikob	int i, err;
1629103285Sikob	device_t *devlistp;
1630103285Sikob	int devcnt;
1631103285Sikob	struct firewire_dev_comm *fdc;
1632103285Sikob
1633103285Sikob	for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL;
1634103285Sikob			fwdev = TAILQ_NEXT(fwdev, link)){
1635103285Sikob		if(fwdev->status == FWDEVINIT){
1636103285Sikob			fwdev->spec = getcsrdata(fwdev, CSRKEY_SPEC);
1637103285Sikob			if(fwdev->spec == 0)
1638103285Sikob				continue;
1639103285Sikob			fwdev->ver = getcsrdata(fwdev, CSRKEY_VER);
1640103285Sikob			if(fwdev->ver == 0)
1641103285Sikob				continue;
1642103285Sikob			fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf;
1643103285Sikob
1644107653Ssimokawa			device_printf(fc->dev, "Device ");
1645103285Sikob			switch(fwdev->spec){
1646103285Sikob			case CSRVAL_ANSIT10:
1647103285Sikob				switch(fwdev->ver){
1648103285Sikob				case CSRVAL_T10SBP2:
1649107653Ssimokawa					printf("SBP-II");
1650103285Sikob					break;
1651103285Sikob				default:
1652103285Sikob					break;
1653103285Sikob				}
1654103285Sikob				break;
1655103285Sikob			case CSRVAL_1394TA:
1656103285Sikob				switch(fwdev->ver){
1657103285Sikob				case CSR_PROTAVC:
1658107653Ssimokawa					printf("AV/C");
1659103285Sikob					break;
1660103285Sikob				case CSR_PROTCAL:
1661107653Ssimokawa					printf("CAL");
1662103285Sikob					break;
1663103285Sikob				case CSR_PROTEHS:
1664107653Ssimokawa					printf("EHS");
1665103285Sikob					break;
1666103285Sikob				case CSR_PROTHAVI:
1667107653Ssimokawa					printf("HAVi");
1668103285Sikob					break;
1669103285Sikob				case CSR_PROTCAM104:
1670107653Ssimokawa					printf("1394 Cam 1.04");
1671103285Sikob					break;
1672103285Sikob				case CSR_PROTCAM120:
1673107653Ssimokawa					printf("1394 Cam 1.20");
1674103285Sikob					break;
1675103285Sikob				case CSR_PROTCAM130:
1676107653Ssimokawa					printf("1394 Cam 1.30");
1677103285Sikob					break;
1678103285Sikob				case CSR_PROTDPP:
1679107653Ssimokawa					printf("1394 Direct print");
1680103285Sikob					break;
1681103285Sikob				case CSR_PROTIICP:
1682107653Ssimokawa					printf("Industrial & Instrument");
1683103285Sikob					break;
1684103285Sikob				default:
1685107653Ssimokawa					printf("unknown 1394TA");
1686103285Sikob					break;
1687103285Sikob				}
1688103285Sikob				break;
1689103285Sikob			default:
1690107653Ssimokawa				printf("unknown spec");
1691103285Sikob				break;
1692103285Sikob			}
1693103285Sikob			fwdev->status = FWDEVATTACHED;
1694103285Sikob			printf("\n");
1695103285Sikob		}
1696103285Sikob	}
1697103285Sikob	err = device_get_children(fc->dev, &devlistp, &devcnt);
1698103285Sikob	if( err != 0 )
1699103285Sikob		return;
1700103285Sikob	for( i = 0 ; i < devcnt ; i++){
1701103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1702103285Sikob			fdc = device_get_softc(devlistp[i]);
1703103285Sikob			if (fdc->post_explore != NULL)
1704103285Sikob				fdc->post_explore(fdc);
1705103285Sikob		}
1706103285Sikob	}
1707103285Sikob	free(devlistp, M_TEMP);
1708103285Sikob
1709103285Sikob	/* call pending handlers */
1710103285Sikob	i = 0;
1711103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1712103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1713103285Sikob		i++;
1714103285Sikob		if (xfer->act.hand)
1715103285Sikob			xfer->act.hand(xfer);
1716103285Sikob	}
1717103285Sikob	if (i > 0)
1718103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1719103285Sikob	if (fc->retry_count > 0) {
1720103285Sikob		printf("retry_count = %d\n", fc->retry_count);
1721103285Sikob		fc->retry_probe_handle = timeout((timeout_t *)fc->ibr,
1722103285Sikob							(void *)fc, hz*2);
1723103285Sikob	}
1724103285Sikob	return;
1725103285Sikob}
1726106815Ssimokawa
1727103285Sikob/*
1728103285Sikob * To allocate uniq transaction label.
1729103285Sikob */
1730106815Ssimokawastatic int
1731106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1732103285Sikob{
1733103285Sikob	u_int i;
1734103285Sikob	struct tlabel *tl, *tmptl;
1735103285Sikob	int s;
1736103285Sikob	static u_int32_t label = 0;
1737103285Sikob
1738103285Sikob	s = splfw();
1739103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1740103285Sikob		label = (label + 1) & 0x3f;
1741103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1742103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1743103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1744103285Sikob		}
1745103285Sikob		if(tmptl == NULL) {
1746103285Sikob			tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_DONTWAIT);
1747103285Sikob			if (tl == NULL) {
1748103285Sikob				splx(s);
1749103285Sikob				return (-1);
1750103285Sikob			}
1751103285Sikob			tl->xfer = xfer;
1752103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1753103285Sikob			splx(s);
1754103285Sikob			return(label);
1755103285Sikob		}
1756103285Sikob	}
1757103285Sikob	splx(s);
1758103285Sikob
1759103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1760103285Sikob	return(-1);
1761103285Sikob}
1762106815Ssimokawa
1763103285Sikob/*
1764103285Sikob * Generic packet receving process.
1765103285Sikob */
1766106815Ssimokawavoid
1767106815Ssimokawafw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd)
1768103285Sikob{
1769103285Sikob	struct fw_pkt *fp, *resfp;
1770103285Sikob	struct fw_xfer *xfer;
1771103285Sikob	struct fw_bind *bind;
1772103285Sikob	struct firewire_softc *sc;
1773103285Sikob	int s;
1774103285Sikob#if 0
1775103285Sikob	{
1776103285Sikob		u_int32_t *qld;
1777103285Sikob		int i;
1778103285Sikob		qld = (u_int32_t *)buf;
1779103285Sikob		printf("spd %d len:%d\n", spd, len);
1780103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1781103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1782103285Sikob			if((i % 16) == 15) printf("\n");
1783103285Sikob		}
1784103285Sikob		if((i % 16) != 15) printf("\n");
1785103285Sikob	}
1786103285Sikob#endif
1787103285Sikob	fp = (struct fw_pkt *)(buf + off);
1788103285Sikob	switch(fp->mode.common.tcode){
1789103285Sikob	case FWTCODE_WRES:
1790103285Sikob	case FWTCODE_RRESQ:
1791103285Sikob	case FWTCODE_RRESB:
1792103285Sikob	case FWTCODE_LRES:
1793103285Sikob		xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1794103285Sikob					fp->mode.hdr.tlrt >> 2);
1795103285Sikob		if(xfer == NULL) {
1796103285Sikob			printf("fw_rcv: unknown response "
1797103285Sikob					"tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n",
1798103285Sikob					fp->mode.common.tcode,
1799103285Sikob					ntohs(fp->mode.hdr.src),
1800103285Sikob					fp->mode.hdr.tlrt >> 2,
1801103285Sikob					fp->mode.hdr.tlrt & 3,
1802103285Sikob					fp->mode.rresq.data);
1803103285Sikob#if 1
1804103285Sikob			printf("try ad-hoc work around!!\n");
1805103285Sikob			xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src),
1806103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1807103285Sikob			if (xfer == NULL) {
1808103285Sikob				printf("no use...\n");
1809103285Sikob				goto err;
1810103285Sikob			}
1811103285Sikob#else
1812103285Sikob			goto err;
1813103285Sikob#endif
1814103285Sikob		}
1815103285Sikob		switch(xfer->act_type){
1816103285Sikob		case FWACT_XFER:
1817103285Sikob			if((xfer->sub >= 0) &&
1818103285Sikob				((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){
1819103285Sikob				xfer->resp = EINVAL;
1820103285Sikob				fw_xfer_done(xfer);
1821103285Sikob				goto err;
1822103285Sikob			}
1823103285Sikob			xfer->recv.len = len;
1824103285Sikob			xfer->recv.off = off;
1825103285Sikob			xfer->recv.buf = buf;
1826103285Sikob			xfer->resp = 0;
1827103285Sikob			fw_xfer_done(xfer);
1828103285Sikob			return;
1829103285Sikob			break;
1830103285Sikob		case FWACT_CH:
1831103285Sikob		default:
1832103285Sikob			goto err;
1833103285Sikob			break;
1834103285Sikob		}
1835103285Sikob		break;
1836103285Sikob	case FWTCODE_WREQQ:
1837103285Sikob	case FWTCODE_WREQB:
1838103285Sikob	case FWTCODE_RREQQ:
1839103285Sikob	case FWTCODE_RREQB:
1840103285Sikob	case FWTCODE_LREQ:
1841103285Sikob		bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi),
1842103285Sikob			ntohl(fp->mode.rreqq.dest_lo));
1843103285Sikob		if(bind == NULL){
1844103285Sikob			printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n",
1845103285Sikob				ntohs(fp->mode.rreqq.dest_hi),
1846103285Sikob				ntohl(fp->mode.rreqq.dest_lo),
1847103285Sikob				fp->mode.common.tcode);
1848103285Sikob			if (fc->status == FWBUSRESET) {
1849103285Sikob				printf("fw_rcv: cannot response(bus reset)!\n");
1850103285Sikob				goto err;
1851103285Sikob			}
1852103285Sikob			xfer = fw_xfer_alloc();
1853103285Sikob			if(xfer == NULL){
1854103285Sikob				return;
1855103285Sikob			}
1856103285Sikob			xfer->spd = spd;
1857103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT);
1858103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1859103285Sikob			switch(fp->mode.common.tcode){
1860103285Sikob			case FWTCODE_WREQQ:
1861103285Sikob			case FWTCODE_WREQB:
1862103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1863103285Sikob				xfer->send.len = 12;
1864103285Sikob				break;
1865103285Sikob			case FWTCODE_RREQQ:
1866103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1867103285Sikob				xfer->send.len = 16;
1868103285Sikob				break;
1869103285Sikob			case FWTCODE_RREQB:
1870103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1871103285Sikob				xfer->send.len = 16;
1872103285Sikob				break;
1873103285Sikob			case FWTCODE_LREQ:
1874103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1875103285Sikob				xfer->send.len = 16;
1876103285Sikob				break;
1877103285Sikob			}
1878103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1879103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1880103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1881103285Sikob			resfp->mode.rresb.rtcode = 7;
1882103285Sikob			resfp->mode.rresb.extcode = 0;
1883103285Sikob			resfp->mode.rresb.len = 0;
1884103285Sikob/*
1885103285Sikob			xfer->act.hand = fw_asy_callback;
1886103285Sikob*/
1887103285Sikob			xfer->act.hand = fw_xfer_free;
1888103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1889103285Sikob				fw_xfer_free( xfer);
1890103285Sikob				return;
1891103285Sikob			}
1892103285Sikob			goto err;
1893103285Sikob		}
1894103285Sikob		switch(bind->xfer->act_type){
1895103285Sikob		case FWACT_XFER:
1896103285Sikob			xfer = fw_xfer_alloc();
1897103285Sikob			if(xfer == NULL) goto err;
1898103285Sikob			xfer->fc = bind->xfer->fc;
1899103285Sikob			xfer->sc = bind->xfer->sc;
1900103285Sikob			xfer->recv.buf = buf;
1901103285Sikob			xfer->recv.len = len;
1902103285Sikob			xfer->recv.off = off;
1903103285Sikob			xfer->spd = spd;
1904103285Sikob			xfer->act.hand = bind->xfer->act.hand;
1905103285Sikob			if (fc->status != FWBUSRESET)
1906103285Sikob				xfer->act.hand(xfer);
1907103285Sikob			else
1908103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1909103285Sikob			return;
1910103285Sikob			break;
1911103285Sikob		case FWACT_CH:
1912103285Sikob			if(fc->ir[bind->xfer->sub]->queued >=
1913103285Sikob				fc->ir[bind->xfer->sub]->maxq){
1914103285Sikob				printf("%s:Discard a packet %x %d\n",
1915103285Sikob					device_get_nameunit(fc->dev),
1916103285Sikob					bind->xfer->sub,
1917103285Sikob					fc->ir[bind->xfer->sub]->queued);
1918103285Sikob				goto err;
1919103285Sikob			}
1920103285Sikob			xfer = fw_xfer_alloc();
1921103285Sikob			if(xfer == NULL) goto err;
1922103285Sikob			xfer->recv.buf = buf;
1923103285Sikob			xfer->recv.len = len;
1924103285Sikob			xfer->recv.off = off;
1925103285Sikob			xfer->spd = spd;
1926103285Sikob			s = splfw();
1927103285Sikob			fc->ir[bind->xfer->sub]->queued++;
1928103285Sikob			STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link);
1929103285Sikob			splx(s);
1930103285Sikob
1931103285Sikob			wakeup((caddr_t)fc->ir[bind->xfer->sub]);
1932103285Sikob
1933103285Sikob			return;
1934103285Sikob			break;
1935103285Sikob		default:
1936103285Sikob			goto err;
1937103285Sikob			break;
1938103285Sikob		}
1939103285Sikob		break;
1940103285Sikob	case FWTCODE_STREAM:
1941103285Sikob	{
1942103285Sikob		struct fw_xferq *xferq;
1943103285Sikob
1944103285Sikob		xferq = fc->ir[sub];
1945103285Sikob#if 0
1946103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1947103285Sikob			sub, len, off, spd);
1948103285Sikob#endif
1949103285Sikob		if(xferq->queued >= xferq->maxq) {
1950103285Sikob			printf("receive queue is full\n");
1951103285Sikob			goto err;
1952103285Sikob		}
1953103285Sikob		xfer = fw_xfer_alloc();
1954103285Sikob		if(xfer == NULL) goto err;
1955103285Sikob		xfer->recv.buf = buf;
1956103285Sikob		xfer->recv.len = len;
1957103285Sikob		xfer->recv.off = off;
1958103285Sikob		xfer->spd = spd;
1959103285Sikob		s = splfw();
1960103285Sikob		xferq->queued++;
1961103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1962103285Sikob		splx(s);
1963103285Sikob		sc = device_get_softc(fc->bdev);
1964103285Sikob#if __FreeBSD_version >= 500000
1965103285Sikob		if (SEL_WAITING(&xferq->rsel))
1966103285Sikob#else
1967103285Sikob		if (&xferq->rsel.si_pid != 0)
1968103285Sikob#endif
1969103285Sikob			selwakeup(&xferq->rsel);
1970103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1971103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1972103285Sikob			wakeup((caddr_t)xferq);
1973103285Sikob		}
1974103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1975103285Sikob			xferq->hand(xferq);
1976103285Sikob		}
1977103285Sikob		return;
1978103285Sikob		break;
1979103285Sikob	}
1980103285Sikob	default:
1981103285Sikob		printf("fw_rcv: unknow tcode\n");
1982103285Sikob		break;
1983103285Sikob	}
1984103285Sikoberr:
1985103285Sikob	free(buf, M_DEVBUF);
1986103285Sikob}
1987106815Ssimokawa
1988103285Sikob/*
1989103285Sikob * Post process for Bus Manager election process.
1990103285Sikob */
1991103285Sikobstatic void
1992103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1993103285Sikob{
1994103285Sikob	struct fw_pkt *sfp,*rfp;
1995103285Sikob	struct firewire_comm *fc;
1996103285Sikob
1997103285Sikob	if(xfer == NULL) return;
1998103285Sikob	fc = xfer->fc;
1999103285Sikob	if(xfer->resp != 0){
2000103285Sikob		goto error;
2001103285Sikob	}
2002103285Sikob
2003103285Sikob	if(xfer->send.buf == NULL){
2004103285Sikob		goto error;
2005103285Sikob	}
2006103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
2007103285Sikob
2008103285Sikob	if(xfer->recv.buf == NULL){
2009103285Sikob		goto error;
2010103285Sikob	}
2011103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2012103285Sikob	CSRARC(fc, BUS_MGR_ID)
2013103285Sikob		= fc->set_bmr(fc, ntohl(rfp->mode.lres.payload[0]) & 0x3f);
2014103285Sikob	printf("%s: new bus manager %d ",
2015103285Sikob		device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID));
2016103285Sikob	if((htonl(rfp->mode.lres.payload[0]) & 0x3f) == fc->nodeid){
2017103285Sikob		printf("(me)\n");
2018103285Sikob/* If I am bus manager, optimize gapcount */
2019103285Sikob		if(fc->max_hop <= MAX_GAPHOP ){
2020103285Sikob			fw_phy_config(fc, -1, gap_cnt[fc->max_hop]);
2021103285Sikob		}
2022103285Sikob	}else{
2023103285Sikob		printf("\n");
2024103285Sikob	}
2025103285Sikoberror:
2026103285Sikob	fw_xfer_free(xfer);
2027103285Sikob}
2028106815Ssimokawa
2029103285Sikob/*
2030103285Sikob * To candidate Bus Manager election process.
2031103285Sikob */
2032106815Ssimokawavoid
2033106815Ssimokawafw_try_bmr(void *arg)
2034103285Sikob{
2035103285Sikob	struct fw_xfer *xfer;
2036103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
2037103285Sikob	struct fw_pkt *fp;
2038103285Sikob	int err = 0;
2039103285Sikob
2040103285Sikob	xfer = fw_xfer_alloc();
2041103285Sikob	if(xfer == NULL){
2042103285Sikob		return;
2043103285Sikob	}
2044103285Sikob	xfer->send.len = 24;
2045103285Sikob	xfer->spd = 0;
2046103285Sikob	xfer->send.buf = malloc(24, M_DEVBUF, M_DONTWAIT);
2047103285Sikob	if(xfer->send.buf == NULL){
2048103285Sikob		fw_xfer_free( xfer);
2049103285Sikob		return;
2050103285Sikob	}
2051103285Sikob
2052103285Sikob	fc->status = FWBUSMGRELECT;
2053103285Sikob
2054103285Sikob	xfer->send.off = 0;
2055103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2056103285Sikob	fp->mode.lreq.dest_hi = htons(0xffff);
2057103285Sikob	fp->mode.lreq.tlrt = 0;
2058103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2059103285Sikob	fp->mode.lreq.pri = 0;
2060103285Sikob	fp->mode.lreq.src = 0;
2061103285Sikob	fp->mode.lreq.len = htons(8);
2062103285Sikob	fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP);
2063103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
2064103285Sikob	fp->mode.lreq.dst = htons(xfer->dst);
2065103285Sikob	fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID);
2066103285Sikob	fp->mode.lreq.payload[0] = 0x3f;
2067103285Sikob	fp->mode.lreq.payload[1] = fc->nodeid;
2068103285Sikob	xfer->act_type = FWACT_XFER;
2069103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2070103285Sikob
2071103285Sikob	err = fw_asyreq(fc, -1, xfer);
2072103285Sikob	if(err){
2073103285Sikob		fw_xfer_free( xfer);
2074103285Sikob		return;
2075103285Sikob	}
2076103285Sikob	return;
2077103285Sikob}
2078106543Ssimokawa
2079106543Ssimokawa#ifdef FW_VMACCESS
2080103285Sikob/*
2081103285Sikob * Software implementation for physical memory block access.
2082103285Sikob * XXX:Too slow, usef for debug purpose only.
2083103285Sikob */
2084106815Ssimokawastatic void
2085106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2086103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2087103285Sikob	u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off);
2088103285Sikob
2089103285Sikob	printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n",
2090103285Sikob			xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2091103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2092103285Sikob	if(xfer->resp != 0){
2093103285Sikob		fw_xfer_free( xfer);
2094103285Sikob		return;
2095103285Sikob	}
2096103285Sikob	if(xfer->recv.buf == NULL){
2097103285Sikob		fw_xfer_free( xfer);
2098103285Sikob		return;
2099103285Sikob	}
2100103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2101103285Sikob	switch(rfp->mode.hdr.tcode){
2102103285Sikob		/* XXX need fix for 64bit arch */
2103103285Sikob		case FWTCODE_WREQB:
2104103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
2105103285Sikob			xfer->send.len = 12;
2106103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2107103285Sikob			bcopy(rfp->mode.wreqb.payload,
2108103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2109103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2110103285Sikob			sfp->mode.wres.rtcode = 0;
2111103285Sikob			break;
2112103285Sikob		case FWTCODE_WREQQ:
2113103285Sikob			xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT);
2114103285Sikob			xfer->send.len = 12;
2115103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2116103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2117103285Sikob			sfp->mode.wres.rtcode = 0;
2118103285Sikob			break;
2119103285Sikob		case FWTCODE_RREQB:
2120103285Sikob			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT);
2121103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2122103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2123103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2124103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2125103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2126103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2127103285Sikob			sfp->mode.rresb.rtcode = 0;
2128103285Sikob			sfp->mode.rresb.extcode = 0;
2129103285Sikob			break;
2130103285Sikob		case FWTCODE_RREQQ:
2131103285Sikob			xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT);
2132103285Sikob			xfer->send.len = 16;
2133103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2134103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2135103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2136103285Sikob			sfp->mode.rresb.rtcode = 0;
2137103285Sikob			break;
2138103285Sikob		default:
2139103285Sikob			fw_xfer_free( xfer);
2140103285Sikob			return;
2141103285Sikob	}
2142103285Sikob	xfer->send.off = 0;
2143103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2144103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2145103285Sikob	xfer->act.hand = fw_xfer_free;
2146103285Sikob	xfer->retry_req = fw_asybusy;
2147103285Sikob
2148103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2149103285Sikob	sfp->mode.hdr.pri = 0;
2150103285Sikob
2151103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2152103285Sikob/**/
2153103285Sikob	return;
2154103285Sikob}
2155106543Ssimokawa#endif
2156106543Ssimokawa
2157103285Sikob/*
2158103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2159103285Sikob */
2160106815Ssimokawau_int16_t
2161106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2162103285Sikob	u_int32_t i, sum, crc = 0;
2163103285Sikob	int shift;
2164103285Sikob	len = (len + 3) & ~3;
2165103285Sikob	for(i = 0 ; i < len ; i+= 4){
2166103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2167103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2168103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2169103285Sikob		}
2170103285Sikob		crc &= 0xffff;
2171103285Sikob	}
2172103285Sikob	return((u_int16_t) crc);
2173103285Sikob}
2174106815Ssimokawa
2175103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2176103285SikobMODULE_VERSION(firewire, 1);
2177