firewire.c revision 119289
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 119289 2003-08-22 07:30:41Z simokawa $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/types.h>
41103285Sikob
42103285Sikob#include <sys/kernel.h>
43103285Sikob#include <sys/malloc.h>
44103285Sikob#include <sys/conf.h>
45103285Sikob#include <sys/sysctl.h>
46103285Sikob
47117067Ssimokawa#if __FreeBSD_version < 500000
48117067Ssimokawa#include <machine/clock.h>	/* for DELAY() */
49117067Ssimokawa#endif
50103285Sikob
51103285Sikob#include <sys/bus.h>		/* used by smbus and newbus */
52113584Ssimokawa#include <machine/bus.h>
53103285Sikob
54103285Sikob#include <dev/firewire/firewire.h>
55103285Sikob#include <dev/firewire/firewirereg.h>
56110072Ssimokawa#include <dev/firewire/fwmem.h>
57103285Sikob#include <dev/firewire/iec13213.h>
58103285Sikob#include <dev/firewire/iec68113.h>
59103285Sikob
60116376Ssimokawastruct crom_src_buf {
61116376Ssimokawa	struct crom_src	src;
62116376Ssimokawa	struct crom_chunk root;
63116376Ssimokawa	struct crom_chunk vendor;
64116376Ssimokawa	struct crom_chunk hw;
65116376Ssimokawa};
66116376Ssimokawa
67109736Ssimokawaint firewire_debug=0, try_bmr=1;
68103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
69108281Ssimokawa	"FireWire driver debug flag");
70109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
71109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
72109736Ssimokawa	"Try to be a bus manager");
73103285Sikob
74110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire");
75110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
76110195Ssimokawa
77103285Sikob#define FW_MAXASYRTY 4
78103285Sikob#define FW_MAXDEVRCNT 4
79103285Sikob
80103285Sikobdevclass_t firewire_devclass;
81103285Sikob
82103285Sikobstatic int firewire_match      __P((device_t));
83103285Sikobstatic int firewire_attach      __P((device_t));
84103285Sikobstatic int firewire_detach      __P((device_t));
85116978Ssimokawastatic int firewire_resume      __P((device_t));
86103285Sikob#if 0
87103285Sikobstatic int firewire_shutdown    __P((device_t));
88103285Sikob#endif
89103285Sikobstatic device_t firewire_add_child   __P((device_t, int, const char *, int));
90103285Sikobstatic void fw_try_bmr __P((void *));
91103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *));
92103285Sikobstatic void fw_asystart __P((struct fw_xfer *));
93103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
94103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *));
95103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *));
96103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *));
97103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *));
98106543Ssimokawa#ifdef FW_VMACCESS
99103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *));
100106543Ssimokawa#endif
101103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
102105620Ssimokawa	u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
103110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *));
104103285Sikob
105103285Sikobstatic device_method_t firewire_methods[] = {
106103285Sikob	/* Device interface */
107103285Sikob	DEVMETHOD(device_probe,		firewire_match),
108103285Sikob	DEVMETHOD(device_attach,	firewire_attach),
109103285Sikob	DEVMETHOD(device_detach,	firewire_detach),
110108642Ssimokawa	DEVMETHOD(device_suspend,	bus_generic_suspend),
111116978Ssimokawa	DEVMETHOD(device_resume,	firewire_resume),
112103285Sikob	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
113103285Sikob
114103285Sikob	/* Bus interface */
115103285Sikob	DEVMETHOD(bus_add_child,	firewire_add_child),
116103285Sikob	DEVMETHOD(bus_print_child,	bus_generic_print_child),
117103285Sikob
118103285Sikob	{ 0, 0 }
119103285Sikob};
120103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
121103285Sikob
122114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/
123114909Ssimokawa#define MAX_GAPHOP 15
124114909Ssimokawau_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
125114909Ssimokawa		   21, 24, 26, 29, 32, 35, 37, 40};
126106813Ssimokawa
127103285Sikobstatic driver_t firewire_driver = {
128103285Sikob	"firewire",
129103285Sikob	firewire_methods,
130103285Sikob	sizeof(struct firewire_softc),
131103285Sikob};
132103285Sikob
133103285Sikob/*
134110072Ssimokawa * Lookup fwdev by node id.
135103285Sikob */
136106810Ssimokawastruct fw_device *
137110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
138103285Sikob{
139103285Sikob	struct fw_device *fwdev;
140110072Ssimokawa	int s;
141110072Ssimokawa
142110072Ssimokawa	s = splfw();
143110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
144110072Ssimokawa		if (fwdev->dst == dst)
145103285Sikob			break;
146110072Ssimokawa	splx(s);
147110072Ssimokawa
148106810Ssimokawa	if(fwdev == NULL) return NULL;
149106810Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
150106810Ssimokawa	return fwdev;
151103285Sikob}
152106813Ssimokawa
153103285Sikob/*
154110072Ssimokawa * Lookup fwdev by EUI64.
155110072Ssimokawa */
156110072Ssimokawastruct fw_device *
157110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
158110072Ssimokawa{
159110072Ssimokawa	struct fw_device *fwdev;
160110072Ssimokawa	int s;
161110072Ssimokawa
162110072Ssimokawa	s = splfw();
163110193Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
164110582Ssimokawa		if (FW_EUI64_EQUAL(fwdev->eui, *eui))
165110072Ssimokawa			break;
166110072Ssimokawa	splx(s);
167110072Ssimokawa
168110072Ssimokawa	if(fwdev == NULL) return NULL;
169110072Ssimokawa	if(fwdev->status == FWDEVINVAL) return NULL;
170110072Ssimokawa	return fwdev;
171110072Ssimokawa}
172110072Ssimokawa
173110072Ssimokawa/*
174103285Sikob * Async. request procedure for userland application.
175103285Sikob */
176103285Sikobint
177103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
178103285Sikob{
179103285Sikob	int err = 0;
180103285Sikob	struct fw_xferq *xferq;
181103285Sikob	int tl = 0, len;
182103285Sikob	struct fw_pkt *fp;
183103285Sikob	int tcode;
184103285Sikob	struct tcode_info *info;
185103285Sikob
186103285Sikob	if(xfer == NULL) return EINVAL;
187108701Ssimokawa	if(xfer->send.len > MAXREC(fc->maxrec)){
188103285Sikob		printf("send.len > maxrec\n");
189103285Sikob		return EINVAL;
190103285Sikob	}
191103285Sikob	if(xfer->act.hand == NULL){
192103285Sikob		printf("act.hand == NULL\n");
193103285Sikob		return EINVAL;
194103285Sikob	}
195103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
196103285Sikob
197103285Sikob	tcode = fp->mode.common.tcode & 0xf;
198103285Sikob	info = &fc->tcode[tcode];
199103285Sikob	if (info->flag == 0) {
200103285Sikob		printf("invalid tcode=%d\n", tcode);
201103285Sikob		return EINVAL;
202103285Sikob	}
203103285Sikob	if (info->flag & FWTI_REQ)
204103285Sikob		xferq = fc->atq;
205103285Sikob	else
206103285Sikob		xferq = fc->ats;
207103285Sikob	len = info->hdr_len;
208103285Sikob	if (info->flag & FWTI_BLOCK_STR)
209113584Ssimokawa		len += fp->mode.stream.len;
210103285Sikob	else if (info->flag & FWTI_BLOCK_ASY)
211113584Ssimokawa		len += fp->mode.rresb.len;
212103285Sikob	if( len >  xfer->send.len ){
213103285Sikob		printf("len(%d) > send.len(%d) (tcode=%d)\n",
214103285Sikob				len, xfer->send.len, tcode);
215103285Sikob		return EINVAL;
216103285Sikob	}
217103285Sikob	xfer->send.len = len;
218106790Ssimokawa
219103285Sikob	if(xferq->start == NULL){
220103285Sikob		printf("xferq->start == NULL\n");
221103285Sikob		return EINVAL;
222103285Sikob	}
223103285Sikob	if(!(xferq->queued < xferq->maxq)){
224108655Ssimokawa		device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
225108655Ssimokawa			xferq->queued);
226103285Sikob		return EINVAL;
227103285Sikob	}
228103285Sikob
229103285Sikob
230103285Sikob	if (info->flag & FWTI_TLABEL) {
231103285Sikob		if((tl = fw_get_tlabel(fc, xfer)) == -1 )
232103285Sikob			return EIO;
233103285Sikob		fp->mode.hdr.tlrt = tl << 2;
234103285Sikob	}
235103285Sikob
236103285Sikob	xfer->tl = tl;
237103285Sikob	xfer->resp = 0;
238103285Sikob	xfer->fc = fc;
239103285Sikob	xfer->q = xferq;
240103285Sikob	xfer->retry_req = fw_asybusy;
241103285Sikob
242103285Sikob	fw_asystart(xfer);
243103285Sikob	return err;
244103285Sikob}
245103285Sikob/*
246103285Sikob * Wakeup blocked process.
247103285Sikob */
248103285Sikobvoid
249103285Sikobfw_asy_callback(struct fw_xfer *xfer){
250103285Sikob	wakeup(xfer);
251103285Sikob	return;
252103285Sikob}
253103285Sikob/*
254103285Sikob * Postpone to later retry.
255103285Sikob */
256103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){
257103285Sikob	printf("fw_asybusy\n");
258103285Sikob/*
259103285Sikob	xfer->ch =  timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
260103285Sikob*/
261103285Sikob	DELAY(20000);
262103285Sikob	fw_asystart(xfer);
263103285Sikob	return;
264103285Sikob}
265103285Sikob
266103285Sikob/*
267103285Sikob * Async. request with given xfer structure.
268103285Sikob */
269106790Ssimokawastatic void
270106790Ssimokawafw_asystart(struct fw_xfer *xfer)
271106790Ssimokawa{
272103285Sikob	struct firewire_comm *fc = xfer->fc;
273103285Sikob	int s;
274103285Sikob	if(xfer->retry++ >= fc->max_asyretry){
275113584Ssimokawa		device_printf(fc->bdev, "max_asyretry exceeded\n");
276103285Sikob		xfer->resp = EBUSY;
277103285Sikob		xfer->state = FWXF_BUSY;
278103285Sikob		xfer->act.hand(xfer);
279103285Sikob		return;
280103285Sikob	}
281103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */
282103285Sikob	if (fc->status < FWBUSEXPLORE) {
283103285Sikob		xfer->resp = EAGAIN;
284103285Sikob		xfer->state = FWXF_BUSY;
285103285Sikob		if (xfer->act.hand != NULL)
286103285Sikob			xfer->act.hand(xfer);
287103285Sikob		return;
288103285Sikob	}
289103285Sikob#endif
290103285Sikob	s = splfw();
291103285Sikob	xfer->state = FWXF_INQ;
292103285Sikob	STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
293103285Sikob	xfer->q->queued ++;
294103285Sikob	splx(s);
295103285Sikob	/* XXX just queue for mbuf */
296103285Sikob	if (xfer->mbuf == NULL)
297103285Sikob		xfer->q->start(fc);
298103285Sikob	return;
299103285Sikob}
300106790Ssimokawa
301103285Sikobstatic int
302103285Sikobfirewire_match( device_t dev )
303103285Sikob{
304108281Ssimokawa	device_set_desc(dev, "IEEE1394(FireWire) bus");
305103285Sikob	return -140;
306103285Sikob}
307106790Ssimokawa
308110577Ssimokawastatic void
309110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc)
310110577Ssimokawa{
311110577Ssimokawa	struct fw_xfer *xfer;
312110577Ssimokawa	struct tlabel *tl;
313110577Ssimokawa	struct timeval tv;
314110577Ssimokawa	struct timeval split_timeout;
315111040Ssimokawa	int i, s;
316110577Ssimokawa
317110891Ssimokawa	split_timeout.tv_sec = 6;
318110577Ssimokawa	split_timeout.tv_usec = 0;
319110577Ssimokawa
320110577Ssimokawa	microtime(&tv);
321110577Ssimokawa	timevalsub(&tv, &split_timeout);
322110577Ssimokawa
323111040Ssimokawa	s = splfw();
324110577Ssimokawa	for (i = 0; i < 0x40; i ++) {
325110577Ssimokawa		while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
326110577Ssimokawa			xfer = tl->xfer;
327110577Ssimokawa			if (timevalcmp(&xfer->tv, &tv, >))
328110577Ssimokawa				/* the rests are newer than this */
329110577Ssimokawa				break;
330110577Ssimokawa			device_printf(fc->bdev,
331117716Ssimokawa				"split transaction timeout dst=0x%x tl=0x%x state=%d\n",
332117716Ssimokawa				xfer->dst, i, xfer->state);
333110577Ssimokawa			xfer->resp = ETIMEDOUT;
334110577Ssimokawa			STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
335113584Ssimokawa			fw_xfer_done(xfer);
336110577Ssimokawa		}
337110577Ssimokawa	}
338111040Ssimokawa	splx(s);
339110577Ssimokawa}
340110577Ssimokawa
341110577Ssimokawastatic void
342110577Ssimokawafirewire_watchdog(void *arg)
343110577Ssimokawa{
344110577Ssimokawa	struct firewire_comm *fc;
345110577Ssimokawa
346110577Ssimokawa	fc = (struct firewire_comm *)arg;
347110577Ssimokawa	firewire_xfer_timeout(fc);
348110577Ssimokawa	fc->timeout(fc);
349110577Ssimokawa	callout_reset(&fc->timeout_callout, hz,
350110577Ssimokawa			(void *)firewire_watchdog, (void *)fc);
351110577Ssimokawa}
352110577Ssimokawa
353103285Sikob/*
354103285Sikob * The attach routine.
355103285Sikob */
356103285Sikobstatic int
357118455Ssimokawafirewire_attach(device_t dev)
358103285Sikob{
359118455Ssimokawa	int unit;
360103285Sikob	struct firewire_softc *sc = device_get_softc(dev);
361103285Sikob	device_t pa = device_get_parent(dev);
362103285Sikob	struct firewire_comm *fc;
363103285Sikob
364103285Sikob	fc = (struct firewire_comm *)device_get_softc(pa);
365103285Sikob	sc->fc = fc;
366116978Ssimokawa	fc->status = FWBUSNOTREADY;
367103285Sikob
368118455Ssimokawa	unit = device_get_unit(dev);
369118455Ssimokawa	if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
370103285Sikob
371118455Ssimokawa	fwdev_makedev(sc);
372118455Ssimokawa
373110193Ssimokawa	CALLOUT_INIT(&sc->fc->timeout_callout);
374110193Ssimokawa	CALLOUT_INIT(&sc->fc->bmr_callout);
375110193Ssimokawa	CALLOUT_INIT(&sc->fc->retry_probe_callout);
376110193Ssimokawa	CALLOUT_INIT(&sc->fc->busprobe_callout);
377108853Ssimokawa
378110577Ssimokawa	callout_reset(&sc->fc->timeout_callout, hz,
379110577Ssimokawa			(void *)firewire_watchdog, (void *)sc->fc);
380110193Ssimokawa
381103285Sikob	/* Locate our children */
382103285Sikob	bus_generic_probe(dev);
383103285Sikob
384103285Sikob	/* launch attachement of the added children */
385103285Sikob	bus_generic_attach(dev);
386103285Sikob
387103285Sikob	/* bus_reset */
388103285Sikob	fc->ibr(fc);
389103285Sikob
390103285Sikob	return 0;
391103285Sikob}
392103285Sikob
393103285Sikob/*
394103285Sikob * Attach it as child.
395103285Sikob */
396103285Sikobstatic device_t
397103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit)
398103285Sikob{
399103285Sikob        device_t child;
400103285Sikob	struct firewire_softc *sc;
401103285Sikob
402103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
403103285Sikob	child = device_add_child(dev, name, unit);
404103285Sikob	if (child) {
405103285Sikob		device_set_ivars(child, sc->fc);
406103285Sikob		device_probe_and_attach(child);
407103285Sikob	}
408103285Sikob
409103285Sikob	return child;
410103285Sikob}
411106790Ssimokawa
412116978Ssimokawastatic int
413116978Ssimokawafirewire_resume(device_t dev)
414116978Ssimokawa{
415116978Ssimokawa	struct firewire_softc *sc;
416116978Ssimokawa
417116978Ssimokawa	sc = (struct firewire_softc *)device_get_softc(dev);
418116978Ssimokawa	sc->fc->status = FWBUSNOTREADY;
419116978Ssimokawa
420116978Ssimokawa	bus_generic_resume(dev);
421116978Ssimokawa
422116978Ssimokawa	return(0);
423116978Ssimokawa}
424116978Ssimokawa
425103285Sikob/*
426103285Sikob * Dettach it.
427103285Sikob */
428103285Sikobstatic int
429118455Ssimokawafirewire_detach(device_t dev)
430103285Sikob{
431103285Sikob	struct firewire_softc *sc;
432111074Ssimokawa	struct csrdir *csrd, *next;
433111078Ssimokawa	struct fw_device *fwdev, *fwdev_next;
434118455Ssimokawa	int err;
435103285Sikob
436103285Sikob	sc = (struct firewire_softc *)device_get_softc(dev);
437118455Ssimokawa	if ((err = fwdev_destroydev(sc)) != 0)
438118455Ssimokawa		return err;
439106790Ssimokawa
440118455Ssimokawa	if ((err = bus_generic_detach(dev)) != 0)
441118455Ssimokawa		return err;
442111078Ssimokawa
443111078Ssimokawa	callout_stop(&sc->fc->timeout_callout);
444111078Ssimokawa	callout_stop(&sc->fc->bmr_callout);
445111078Ssimokawa	callout_stop(&sc->fc->retry_probe_callout);
446111078Ssimokawa	callout_stop(&sc->fc->busprobe_callout);
447111078Ssimokawa
448103285Sikob	/* XXX xfree_free and untimeout on all xfers */
449111078Ssimokawa	for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
450111078Ssimokawa							fwdev = fwdev_next) {
451111078Ssimokawa		fwdev_next = STAILQ_NEXT(fwdev, link);
452111078Ssimokawa		free(fwdev, M_FW);
453111078Ssimokawa	}
454111074Ssimokawa	for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
455111074Ssimokawa		next = SLIST_NEXT(csrd, link);
456111074Ssimokawa		free(csrd, M_FW);
457111074Ssimokawa	}
458110195Ssimokawa	free(sc->fc->topology_map, M_FW);
459110195Ssimokawa	free(sc->fc->speed_map, M_FW);
460116376Ssimokawa	free(sc->fc->crom_src_buf, M_FW);
461103285Sikob	return(0);
462103285Sikob}
463103285Sikob#if 0
464103285Sikobstatic int
465103285Sikobfirewire_shutdown( device_t dev )
466103285Sikob{
467103285Sikob	return 0;
468103285Sikob}
469103285Sikob#endif
470106790Ssimokawa
471110577Ssimokawa
472110577Ssimokawastatic void
473110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq)
474110577Ssimokawa{
475110577Ssimokawa	struct fw_xfer *xfer;
476110577Ssimokawa
477110577Ssimokawa	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
478110577Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->q, link);
479111942Ssimokawa		xferq->queued --;
480110577Ssimokawa		xfer->resp = EAGAIN;
481113584Ssimokawa		fw_xfer_done(xfer);
482110577Ssimokawa	}
483110577Ssimokawa}
484110577Ssimokawa
485110798Ssimokawavoid
486110798Ssimokawafw_drain_txq(struct firewire_comm *fc)
487110798Ssimokawa{
488110798Ssimokawa	int i;
489110798Ssimokawa
490110798Ssimokawa	fw_xferq_drain(fc->atq);
491110798Ssimokawa	fw_xferq_drain(fc->ats);
492110798Ssimokawa	for(i = 0; i < fc->nisodma; i++)
493110798Ssimokawa		fw_xferq_drain(fc->it[i]);
494110798Ssimokawa}
495110798Ssimokawa
496116376Ssimokawastatic void
497116376Ssimokawafw_reset_csr(struct firewire_comm *fc)
498103285Sikob{
499103285Sikob	int i;
500103285Sikob
501103285Sikob	CSRARC(fc, STATE_CLEAR)
502103285Sikob			= 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
503103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
504103285Sikob	CSRARC(fc, NODE_IDS) = 0x3f;
505103285Sikob
506103285Sikob	CSRARC(fc, TOPO_MAP + 8) = 0;
507103285Sikob	fc->irm = -1;
508103285Sikob
509103285Sikob	fc->max_node = -1;
510103285Sikob
511103285Sikob	for(i = 2; i < 0x100/4 - 2 ; i++){
512103285Sikob		CSRARC(fc, SPED_MAP + i * 4) = 0;
513103285Sikob	}
514103285Sikob	CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
515103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
516103285Sikob	CSRARC(fc, RESET_START) = 0;
517103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
518103285Sikob	CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
519103285Sikob	CSRARC(fc, CYCLE_TIME) = 0x0;
520103285Sikob	CSRARC(fc, BUS_TIME) = 0x0;
521103285Sikob	CSRARC(fc, BUS_MGR_ID) = 0x3f;
522103285Sikob	CSRARC(fc, BANDWIDTH_AV) = 4915;
523103285Sikob	CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
524103285Sikob	CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
525103285Sikob	CSRARC(fc, IP_CHANNELS) = (1 << 31);
526103285Sikob
527103285Sikob	CSRARC(fc, CONF_ROM) = 0x04 << 24;
528103285Sikob	CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
529103285Sikob	CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
530103285Sikob				1 << 28 | 0xff << 16 | 0x09 << 8;
531103285Sikob	CSRARC(fc, CONF_ROM + 0xc) = 0;
532103285Sikob
533103285Sikob/* DV depend CSRs see blue book */
534103285Sikob	CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
535103285Sikob	CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
536103285Sikob
537103285Sikob	CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
538103285Sikob	CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
539116376Ssimokawa}
540113584Ssimokawa
541116376Ssimokawastatic void
542116376Ssimokawafw_init_crom(struct firewire_comm *fc)
543116376Ssimokawa{
544116376Ssimokawa	struct crom_src *src;
545116376Ssimokawa
546116376Ssimokawa	fc->crom_src_buf = (struct crom_src_buf *)
547116376Ssimokawa		malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
548116376Ssimokawa	if (fc->crom_src_buf == NULL)
549116376Ssimokawa		return;
550116376Ssimokawa
551116376Ssimokawa	src = &fc->crom_src_buf->src;
552116376Ssimokawa	bzero(src, sizeof(struct crom_src));
553116376Ssimokawa
554116376Ssimokawa	/* BUS info sample */
555116376Ssimokawa	src->hdr.info_len = 4;
556116376Ssimokawa
557116376Ssimokawa	src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
558116376Ssimokawa
559116376Ssimokawa	src->businfo.irmc = 1;
560116376Ssimokawa	src->businfo.cmc = 1;
561116376Ssimokawa	src->businfo.isc = 1;
562116376Ssimokawa	src->businfo.bmc = 1;
563116376Ssimokawa	src->businfo.pmc = 0;
564116376Ssimokawa	src->businfo.cyc_clk_acc = 100;
565116376Ssimokawa	src->businfo.max_rec = fc->maxrec;
566116376Ssimokawa	src->businfo.max_rom = MAXROM_4;
567117350Ssimokawa	src->businfo.generation = 1;
568116376Ssimokawa	src->businfo.link_spd = fc->speed;
569116376Ssimokawa
570116376Ssimokawa	src->businfo.eui64.hi = fc->eui.hi;
571116376Ssimokawa	src->businfo.eui64.lo = fc->eui.lo;
572116376Ssimokawa
573116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
574116376Ssimokawa
575116376Ssimokawa	fc->crom_src = src;
576116376Ssimokawa	fc->crom_root = &fc->crom_src_buf->root;
577116376Ssimokawa}
578116376Ssimokawa
579116376Ssimokawastatic void
580116376Ssimokawafw_reset_crom(struct firewire_comm *fc)
581116376Ssimokawa{
582116376Ssimokawa	struct crom_src_buf *buf;
583116376Ssimokawa	struct crom_src *src;
584116376Ssimokawa	struct crom_chunk *root;
585116376Ssimokawa
586116376Ssimokawa	if (fc->crom_src_buf == NULL)
587116376Ssimokawa		fw_init_crom(fc);
588116376Ssimokawa
589116376Ssimokawa	buf =  fc->crom_src_buf;
590116376Ssimokawa	src = fc->crom_src;
591116376Ssimokawa	root = fc->crom_root;
592116376Ssimokawa
593116376Ssimokawa	STAILQ_INIT(&src->chunk_list);
594116376Ssimokawa
595116376Ssimokawa	bzero(root, sizeof(struct crom_chunk));
596116376Ssimokawa	crom_add_chunk(src, NULL, root, 0);
597116376Ssimokawa	crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
598116376Ssimokawa	/* private company_id */
599116376Ssimokawa	crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
600116376Ssimokawa	crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
601116376Ssimokawa	crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
602116376Ssimokawa	crom_add_simple_text(src, root, &buf->hw, hostname);
603116376Ssimokawa}
604116376Ssimokawa
605116376Ssimokawa/*
606116376Ssimokawa * Called after bus reset.
607116376Ssimokawa */
608116376Ssimokawavoid
609116376Ssimokawafw_busreset(struct firewire_comm *fc)
610116376Ssimokawa{
611116376Ssimokawa	struct firewire_dev_comm *fdc;
612117350Ssimokawa	struct crom_src *src;
613116376Ssimokawa	device_t *devlistp;
614117350Ssimokawa	void *newrom;
615116376Ssimokawa	int i, devcnt;
616116376Ssimokawa
617116376Ssimokawa	switch(fc->status){
618116376Ssimokawa	case FWBUSMGRELECT:
619116376Ssimokawa		callout_stop(&fc->bmr_callout);
620116376Ssimokawa		break;
621116376Ssimokawa	default:
622116376Ssimokawa		break;
623116376Ssimokawa	}
624116376Ssimokawa	fc->status = FWBUSRESET;
625116376Ssimokawa	fw_reset_csr(fc);
626116376Ssimokawa	fw_reset_crom(fc);
627116376Ssimokawa
628113584Ssimokawa	if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
629113584Ssimokawa		for( i = 0 ; i < devcnt ; i++)
630113584Ssimokawa			if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
631113584Ssimokawa				fdc = device_get_softc(devlistp[i]);
632113584Ssimokawa				if (fdc->post_busreset != NULL)
633113584Ssimokawa					fdc->post_busreset(fdc);
634113584Ssimokawa			}
635113584Ssimokawa		free(devlistp, M_TEMP);
636113584Ssimokawa	}
637116376Ssimokawa
638117350Ssimokawa	newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
639117350Ssimokawa	src = &fc->crom_src_buf->src;
640117350Ssimokawa	crom_load(src, (u_int32_t *)newrom, CROMSIZE);
641117350Ssimokawa	if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
642117350Ssimokawa		/* bump generation and reload */
643117350Ssimokawa		src->businfo.generation ++;
644117350Ssimokawa		/* generation must be between 0x2 and 0xF */
645117350Ssimokawa		if (src->businfo.generation < 2)
646117350Ssimokawa			src->businfo.generation ++;
647117350Ssimokawa		crom_load(src, (u_int32_t *)newrom, CROMSIZE);
648117350Ssimokawa		bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
649117350Ssimokawa	}
650117350Ssimokawa	free(newrom, M_FW);
651103285Sikob}
652106790Ssimokawa
653103285Sikob/* Call once after reboot */
654106790Ssimokawavoid fw_init(struct firewire_comm *fc)
655103285Sikob{
656103285Sikob	int i;
657106543Ssimokawa	struct csrdir *csrd;
658106543Ssimokawa#ifdef FW_VMACCESS
659103285Sikob	struct fw_xfer *xfer;
660103285Sikob	struct fw_bind *fwb;
661106543Ssimokawa#endif
662103285Sikob
663103285Sikob	fc->max_asyretry = FW_MAXASYRTY;
664103285Sikob
665103285Sikob	fc->arq->queued = 0;
666103285Sikob	fc->ars->queued = 0;
667103285Sikob	fc->atq->queued = 0;
668103285Sikob	fc->ats->queued = 0;
669103285Sikob
670103285Sikob	fc->arq->buf = NULL;
671103285Sikob	fc->ars->buf = NULL;
672103285Sikob	fc->atq->buf = NULL;
673103285Sikob	fc->ats->buf = NULL;
674103285Sikob
675113584Ssimokawa	fc->arq->flag = 0;
676113584Ssimokawa	fc->ars->flag = 0;
677113584Ssimokawa	fc->atq->flag = 0;
678113584Ssimokawa	fc->ats->flag = 0;
679103285Sikob
680103285Sikob	STAILQ_INIT(&fc->atq->q);
681103285Sikob	STAILQ_INIT(&fc->ats->q);
682103285Sikob
683103285Sikob	for( i = 0 ; i < fc->nisodma ; i ++ ){
684103285Sikob		fc->it[i]->queued = 0;
685103285Sikob		fc->ir[i]->queued = 0;
686103285Sikob
687103285Sikob		fc->it[i]->start = NULL;
688103285Sikob		fc->ir[i]->start = NULL;
689103285Sikob
690103285Sikob		fc->it[i]->buf = NULL;
691103285Sikob		fc->ir[i]->buf = NULL;
692103285Sikob
693103285Sikob		fc->it[i]->flag = FWXFERQ_STREAM;
694103285Sikob		fc->ir[i]->flag = FWXFERQ_STREAM;
695103285Sikob
696103285Sikob		STAILQ_INIT(&fc->it[i]->q);
697103285Sikob		STAILQ_INIT(&fc->ir[i]->q);
698103285Sikob
699103285Sikob		STAILQ_INIT(&fc->it[i]->binds);
700103285Sikob		STAILQ_INIT(&fc->ir[i]->binds);
701103285Sikob	}
702103285Sikob
703103285Sikob	fc->arq->maxq = FWMAXQUEUE;
704103285Sikob	fc->ars->maxq = FWMAXQUEUE;
705103285Sikob	fc->atq->maxq = FWMAXQUEUE;
706103285Sikob	fc->ats->maxq = FWMAXQUEUE;
707103285Sikob
708103285Sikob	for( i = 0 ; i < fc->nisodma ; i++){
709103285Sikob		fc->ir[i]->maxq = FWMAXQUEUE;
710103285Sikob		fc->it[i]->maxq = FWMAXQUEUE;
711103285Sikob	}
712103285Sikob/* Initialize csr registers */
713103285Sikob	fc->topology_map = (struct fw_topology_map *)malloc(
714103285Sikob				sizeof(struct fw_topology_map),
715110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
716103285Sikob	fc->speed_map = (struct fw_speed_map *)malloc(
717103285Sikob				sizeof(struct fw_speed_map),
718110195Ssimokawa				M_FW, M_NOWAIT | M_ZERO);
719103285Sikob	CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
720103285Sikob	CSRARC(fc, TOPO_MAP + 4) = 1;
721103285Sikob	CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
722103285Sikob	CSRARC(fc, SPED_MAP + 4) = 1;
723103285Sikob
724110193Ssimokawa	STAILQ_INIT(&fc->devices);
725103285Sikob	STAILQ_INIT(&fc->pending);
726103285Sikob
727103285Sikob/* Initialize csr ROM work space */
728103285Sikob	SLIST_INIT(&fc->ongocsr);
729103285Sikob	SLIST_INIT(&fc->csrfree);
730103285Sikob	for( i = 0 ; i < FWMAXCSRDIR ; i++){
731110195Ssimokawa		csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
732103285Sikob		if(csrd == NULL) break;
733103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
734103285Sikob	}
735103285Sikob
736103285Sikob/* Initialize Async handlers */
737103285Sikob	STAILQ_INIT(&fc->binds);
738103285Sikob	for( i = 0 ; i < 0x40 ; i++){
739103285Sikob		STAILQ_INIT(&fc->tlabels[i]);
740103285Sikob	}
741103285Sikob
742103285Sikob/* DV depend CSRs see blue book */
743103285Sikob#if 0
744103285Sikob	CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
745103285Sikob	CSRARC(fc, oPCR) = 0x8000007a;
746103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
747103285Sikob		CSRARC(fc, i + oPCR) = 0x8000007a;
748103285Sikob	}
749103285Sikob
750103285Sikob	CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
751103285Sikob	CSRARC(fc, iPCR) = 0x803f0000;
752103285Sikob	for(i = 4 ; i < 0x7c/4 ; i+=4){
753103285Sikob		CSRARC(fc, i + iPCR) = 0x0;
754103285Sikob	}
755103285Sikob#endif
756103285Sikob
757116376Ssimokawa	fc->crom_src_buf = NULL;
758103285Sikob
759106543Ssimokawa#ifdef FW_VMACCESS
760103285Sikob	xfer = fw_xfer_alloc();
761103285Sikob	if(xfer == NULL) return;
762103285Sikob
763110195Ssimokawa	fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
764103285Sikob	if(fwb == NULL){
765103285Sikob		fw_xfer_free(xfer);
766103285Sikob	}
767103285Sikob	xfer->act.hand = fw_vmaccess;
768103285Sikob	xfer->fc = fc;
769103285Sikob	xfer->sc = NULL;
770103285Sikob
771103285Sikob	fwb->start_hi = 0x2;
772103285Sikob	fwb->start_lo = 0;
773103285Sikob	fwb->addrlen = 0xffffffff;
774103285Sikob	fwb->xfer = xfer;
775103285Sikob	fw_bindadd(fc, fwb);
776106543Ssimokawa#endif
777103285Sikob}
778106790Ssimokawa
779103285Sikob/*
780103285Sikob * To lookup binded process from IEEE1394 address.
781103285Sikob */
782106813Ssimokawastruct fw_bind *
783106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
784103285Sikob{
785103285Sikob	struct fw_bind *tfw;
786103285Sikob	for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
787103285Sikob		tfw = STAILQ_NEXT(tfw, fclist)){
788113584Ssimokawa		if (tfw->act_type != FWACT_NULL &&
789103285Sikob			tfw->start_hi == dest_hi &&
790103285Sikob			tfw->start_lo <= dest_lo &&
791103285Sikob			(tfw->start_lo + tfw->addrlen) > dest_lo){
792103285Sikob			return(tfw);
793103285Sikob		}
794103285Sikob	}
795103285Sikob	return(NULL);
796103285Sikob}
797106790Ssimokawa
798103285Sikob/*
799103285Sikob * To bind IEEE1394 address block to process.
800103285Sikob */
801106790Ssimokawaint
802106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
803103285Sikob{
804103285Sikob	struct fw_bind *tfw, *tfw2 = NULL;
805103285Sikob	int err = 0;
806103285Sikob	tfw = STAILQ_FIRST(&fc->binds);
807103285Sikob	if(tfw == NULL){
808103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
809103285Sikob		goto out;
810103285Sikob	}
811103285Sikob	if((tfw->start_hi > fwb->start_hi) ||
812103285Sikob		(tfw->start_hi == fwb->start_hi &&
813103285Sikob		(tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
814103285Sikob		STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
815103285Sikob		goto out;
816103285Sikob	}
817103285Sikob	for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
818103285Sikob		if((tfw->start_hi < fwb->start_hi) ||
819103285Sikob		   (tfw->start_hi == fwb->start_hi &&
820103285Sikob		    (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
821103285Sikob		   tfw2 = STAILQ_NEXT(tfw, fclist);
822103285Sikob			if(tfw2 == NULL)
823103285Sikob				break;
824103285Sikob			if((tfw2->start_hi > fwb->start_hi) ||
825103285Sikob			   (tfw2->start_hi == fwb->start_hi &&
826103285Sikob			    tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
827103285Sikob				break;
828103285Sikob			}else{
829103285Sikob				err = EBUSY;
830103285Sikob				goto out;
831103285Sikob			}
832103285Sikob		}
833103285Sikob	}
834103285Sikob	if(tfw != NULL){
835103285Sikob		STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
836103285Sikob	}else{
837103285Sikob		STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
838103285Sikob	}
839103285Sikobout:
840113584Ssimokawa	if (!err && fwb->act_type == FWACT_CH)
841113584Ssimokawa		STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
842103285Sikob	return err;
843103285Sikob}
844103285Sikob
845103285Sikob/*
846103285Sikob * To free IEEE1394 address block.
847103285Sikob */
848106790Ssimokawaint
849106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
850103285Sikob{
851103285Sikob	int s;
852113584Ssimokawa	struct fw_xfer *xfer, *next;
853103285Sikob
854103285Sikob	s = splfw();
855103285Sikob	/* shall we check the existance? */
856103285Sikob	STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
857113584Ssimokawa	/* shall we do this? */
858113584Ssimokawa	for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
859113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
860113584Ssimokawa		fw_xfer_free(xfer);
861113584Ssimokawa	}
862113584Ssimokawa	STAILQ_INIT(&fwb->xferlist);
863113584Ssimokawa
864103285Sikob	splx(s);
865103285Sikob	return 0;
866103285Sikob}
867103285Sikob
868103285Sikob/*
869103285Sikob * To free transaction label.
870103285Sikob */
871106790Ssimokawastatic void
872106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
873103285Sikob{
874103285Sikob	struct tlabel *tl;
875103285Sikob	int s = splfw();
876103285Sikob
877103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
878103285Sikob		tl = STAILQ_NEXT(tl, link)){
879103285Sikob		if(tl->xfer == xfer){
880103285Sikob			STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
881110195Ssimokawa			free(tl, M_FW);
882103285Sikob			splx(s);
883103285Sikob			return;
884103285Sikob		}
885103285Sikob	}
886103285Sikob	splx(s);
887103285Sikob	return;
888103285Sikob}
889106790Ssimokawa
890103285Sikob/*
891103285Sikob * To obtain XFER structure by transaction label.
892103285Sikob */
893106790Ssimokawastatic struct fw_xfer *
894106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
895103285Sikob{
896103285Sikob	struct fw_xfer *xfer;
897103285Sikob	struct tlabel *tl;
898103285Sikob	int s = splfw();
899103285Sikob
900103285Sikob	for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
901103285Sikob		tl = STAILQ_NEXT(tl, link)){
902103285Sikob		if(tl->xfer->dst == node){
903103285Sikob			xfer = tl->xfer;
904103285Sikob			splx(s);
905110577Ssimokawa			if (firewire_debug > 2)
906110577Ssimokawa				printf("fw_tl2xfer: found tl=%d\n", tlabel);
907103285Sikob			return(xfer);
908103285Sikob		}
909103285Sikob	}
910110577Ssimokawa	if (firewire_debug > 1)
911110577Ssimokawa		printf("fw_tl2xfer: not found tl=%d\n", tlabel);
912103285Sikob	splx(s);
913103285Sikob	return(NULL);
914103285Sikob}
915106790Ssimokawa
916103285Sikob/*
917103285Sikob * To allocate IEEE1394 XFER structure.
918103285Sikob */
919106790Ssimokawastruct fw_xfer *
920110269Ssimokawafw_xfer_alloc(struct malloc_type *type)
921103285Sikob{
922103285Sikob	struct fw_xfer *xfer;
923106790Ssimokawa
924110269Ssimokawa	xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
925106790Ssimokawa	if (xfer == NULL)
926106790Ssimokawa		return xfer;
927106790Ssimokawa
928110577Ssimokawa	microtime(&xfer->tv);
929110269Ssimokawa	xfer->malloc = type;
930106790Ssimokawa
931103285Sikob	return xfer;
932103285Sikob}
933106790Ssimokawa
934113584Ssimokawastruct fw_xfer *
935113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
936113584Ssimokawa{
937113584Ssimokawa	struct fw_xfer *xfer;
938113584Ssimokawa
939113584Ssimokawa	xfer = fw_xfer_alloc(type);
940113584Ssimokawa	xfer->send.len = send_len;
941113584Ssimokawa	xfer->recv.len = recv_len;
942113584Ssimokawa	if (xfer == NULL)
943113584Ssimokawa		return(NULL);
944113584Ssimokawa	if (send_len) {
945113584Ssimokawa		xfer->send.buf = malloc(send_len, type, M_NOWAIT | M_ZERO);
946113584Ssimokawa		if (xfer->send.buf == NULL) {
947113584Ssimokawa			fw_xfer_free(xfer);
948113584Ssimokawa			return(NULL);
949113584Ssimokawa		}
950113584Ssimokawa	}
951113584Ssimokawa	if (recv_len) {
952113584Ssimokawa		xfer->recv.buf = malloc(recv_len, type, M_NOWAIT);
953113584Ssimokawa		if (xfer->recv.buf == NULL) {
954113584Ssimokawa			if (xfer->send.buf != NULL)
955113584Ssimokawa				free(xfer->send.buf, type);
956113584Ssimokawa			fw_xfer_free(xfer);
957113584Ssimokawa			return(NULL);
958113584Ssimokawa		}
959113584Ssimokawa	}
960113584Ssimokawa	return(xfer);
961113584Ssimokawa}
962113584Ssimokawa
963103285Sikob/*
964103285Sikob * IEEE1394 XFER post process.
965103285Sikob */
966103285Sikobvoid
967103285Sikobfw_xfer_done(struct fw_xfer *xfer)
968103285Sikob{
969117716Ssimokawa	if (xfer->act.hand == NULL) {
970117716Ssimokawa		printf("act.hand == NULL\n");
971103285Sikob		return;
972117716Ssimokawa	}
973103285Sikob
974103285Sikob	if (xfer->fc->status != FWBUSRESET)
975103285Sikob		xfer->act.hand(xfer);
976103285Sikob	else {
977103285Sikob		printf("fw_xfer_done: pending\n");
978103285Sikob		if (xfer->fc != NULL)
979103285Sikob			STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
980103285Sikob		else
981103285Sikob			panic("fw_xfer_done: why xfer->fc is NULL?");
982103285Sikob	}
983103285Sikob}
984103285Sikob
985106790Ssimokawavoid
986113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer)
987103285Sikob{
988103285Sikob	int s;
989113584Ssimokawa
990103285Sikob	if(xfer == NULL ) return;
991103285Sikob	if(xfer->state == FWXF_INQ){
992103285Sikob		printf("fw_xfer_free FWXF_INQ\n");
993103285Sikob		s = splfw();
994103285Sikob		STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
995103285Sikob		xfer->q->queued --;
996103285Sikob		splx(s);
997103285Sikob	}
998113584Ssimokawa	if (xfer->fc != NULL) {
999114729Ssimokawa#if 1
1000113584Ssimokawa		if(xfer->state == FWXF_START)
1001114729Ssimokawa			/*
1002114729Ssimokawa			 * This could happen if:
1003114729Ssimokawa			 *  1. We call fwohci_arcv() before fwohci_txd().
1004114729Ssimokawa			 *  2. firewire_watch() is called.
1005114729Ssimokawa			 */
1006114729Ssimokawa			printf("fw_xfer_free FWXF_START\n");
1007103285Sikob#endif
1008113584Ssimokawa		fw_tl_free(xfer->fc, xfer);
1009103285Sikob	}
1010113584Ssimokawa	xfer->state = FWXF_INIT;
1011113584Ssimokawa	xfer->resp = 0;
1012113584Ssimokawa	xfer->retry = 0;
1013113584Ssimokawa}
1014113584Ssimokawa/*
1015113584Ssimokawa * To free IEEE1394 XFER structure.
1016113584Ssimokawa */
1017113584Ssimokawavoid
1018113584Ssimokawafw_xfer_free( struct fw_xfer* xfer)
1019113584Ssimokawa{
1020113584Ssimokawa	if(xfer == NULL ) return;
1021113584Ssimokawa	fw_xfer_unload(xfer);
1022103285Sikob	if(xfer->send.buf != NULL){
1023113584Ssimokawa		free(xfer->send.buf, xfer->malloc);
1024103285Sikob	}
1025103285Sikob	if(xfer->recv.buf != NULL){
1026113584Ssimokawa		free(xfer->recv.buf, xfer->malloc);
1027103285Sikob	}
1028110269Ssimokawa	free(xfer, xfer->malloc);
1029103285Sikob}
1030103285Sikob
1031103285Sikobstatic void
1032110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer)
1033103285Sikob{
1034103285Sikob#if 0
1035110072Ssimokawa	printf("asyreq done state=%d resp=%d\n",
1036103285Sikob				xfer->state, xfer->resp);
1037103285Sikob#endif
1038103285Sikob	fw_xfer_free(xfer);
1039103285Sikob}
1040103285Sikob
1041103285Sikob/*
1042103285Sikob * To configure PHY.
1043103285Sikob */
1044103285Sikobstatic void
1045103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1046103285Sikob{
1047103285Sikob	struct fw_xfer *xfer;
1048103285Sikob	struct fw_pkt *fp;
1049103285Sikob
1050103285Sikob	fc->status = FWBUSPHYCONF;
1051103285Sikob
1052113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 12, 0);
1053113584Ssimokawa	if (xfer == NULL)
1054113584Ssimokawa		return;
1055103285Sikob	xfer->fc = fc;
1056103285Sikob	xfer->retry_req = fw_asybusy;
1057110072Ssimokawa	xfer->act.hand = fw_asy_callback_free;
1058103285Sikob
1059103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1060103285Sikob	fp->mode.ld[1] = 0;
1061103285Sikob	if (root_node >= 0)
1062113584Ssimokawa		fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1063103285Sikob	if (gap_count >= 0)
1064113584Ssimokawa		fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1065103285Sikob	fp->mode.ld[2] = ~fp->mode.ld[1];
1066103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */
1067103285Sikob	fp->mode.common.tcode |= FWTCODE_PHY;
1068103285Sikob
1069107653Ssimokawa	if (firewire_debug)
1070107653Ssimokawa		printf("send phy_config root_node=%d gap_count=%d\n",
1071103285Sikob						root_node, gap_count);
1072103285Sikob	fw_asyreq(fc, -1, xfer);
1073103285Sikob}
1074103285Sikob
1075103285Sikob#if 0
1076103285Sikob/*
1077103285Sikob * Dump self ID.
1078103285Sikob */
1079103285Sikobstatic void
1080103285Sikobfw_print_sid(u_int32_t sid)
1081103285Sikob{
1082103285Sikob	union fw_self_id *s;
1083103285Sikob	s = (union fw_self_id *) &sid;
1084103285Sikob	printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1085103285Sikob		" p0:%d p1:%d p2:%d i:%d m:%d\n",
1086103285Sikob		s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1087103285Sikob		s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1088103285Sikob		s->p0.power_class, s->p0.port0, s->p0.port1,
1089103285Sikob		s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1090103285Sikob}
1091103285Sikob#endif
1092103285Sikob
1093103285Sikob/*
1094103285Sikob * To receive self ID.
1095103285Sikob */
1096113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len)
1097103285Sikob{
1098113584Ssimokawa	u_int32_t *p;
1099103285Sikob	union fw_self_id *self_id;
1100103285Sikob	u_int i, j, node, c_port = 0, i_branch = 0;
1101103285Sikob
1102103285Sikob	fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1103103285Sikob	fc->status = FWBUSINIT;
1104103285Sikob	fc->max_node = fc->nodeid & 0x3f;
1105103285Sikob	CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1106103285Sikob	fc->status = FWBUSCYMELECT;
1107103285Sikob	fc->topology_map->crc_len = 2;
1108103285Sikob	fc->topology_map->generation ++;
1109103285Sikob	fc->topology_map->self_id_count = 0;
1110103285Sikob	fc->topology_map->node_count = 0;
1111103285Sikob	fc->speed_map->generation ++;
1112103285Sikob	fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1113103285Sikob	self_id = &fc->topology_map->self_id[0];
1114103285Sikob	for(i = 0; i < fc->sid_cnt; i ++){
1115103285Sikob		if (sid[1] != ~sid[0]) {
1116103285Sikob			printf("fw_sidrcv: invalid self-id packet\n");
1117103285Sikob			sid += 2;
1118103285Sikob			continue;
1119103285Sikob		}
1120103285Sikob		*self_id = *((union fw_self_id *)sid);
1121103285Sikob		fc->topology_map->crc_len++;
1122103285Sikob		if(self_id->p0.sequel == 0){
1123103285Sikob			fc->topology_map->node_count ++;
1124103285Sikob			c_port = 0;
1125103285Sikob#if 0
1126103285Sikob			fw_print_sid(sid[0]);
1127103285Sikob#endif
1128103285Sikob			node = self_id->p0.phy_id;
1129103285Sikob			if(fc->max_node < node){
1130103285Sikob				fc->max_node = self_id->p0.phy_id;
1131103285Sikob			}
1132103285Sikob			/* XXX I'm not sure this is the right speed_map */
1133103285Sikob			fc->speed_map->speed[node][node]
1134103285Sikob					= self_id->p0.phy_speed;
1135103285Sikob			for (j = 0; j < node; j ++) {
1136103285Sikob				fc->speed_map->speed[j][node]
1137103285Sikob					= fc->speed_map->speed[node][j]
1138103285Sikob					= min(fc->speed_map->speed[j][j],
1139103285Sikob							self_id->p0.phy_speed);
1140103285Sikob			}
1141103285Sikob			if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1142103285Sikob			  (self_id->p0.link_active && self_id->p0.contender)) {
1143103285Sikob				fc->irm = self_id->p0.phy_id;
1144103285Sikob			}
1145103285Sikob			if(self_id->p0.port0 >= 0x2){
1146103285Sikob				c_port++;
1147103285Sikob			}
1148103285Sikob			if(self_id->p0.port1 >= 0x2){
1149103285Sikob				c_port++;
1150103285Sikob			}
1151103285Sikob			if(self_id->p0.port2 >= 0x2){
1152103285Sikob				c_port++;
1153103285Sikob			}
1154103285Sikob		}
1155103285Sikob		if(c_port > 2){
1156103285Sikob			i_branch += (c_port - 2);
1157103285Sikob		}
1158103285Sikob		sid += 2;
1159103285Sikob		self_id++;
1160103285Sikob		fc->topology_map->self_id_count ++;
1161103285Sikob	}
1162108655Ssimokawa	device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1163103285Sikob	/* CRC */
1164103285Sikob	fc->topology_map->crc = fw_crc16(
1165103285Sikob			(u_int32_t *)&fc->topology_map->generation,
1166103285Sikob			fc->topology_map->crc_len * 4);
1167103285Sikob	fc->speed_map->crc = fw_crc16(
1168103285Sikob			(u_int32_t *)&fc->speed_map->generation,
1169103285Sikob			fc->speed_map->crc_len * 4);
1170103285Sikob	/* byteswap and copy to CSR */
1171103285Sikob	p = (u_int32_t *)fc->topology_map;
1172103285Sikob	for (i = 0; i <= fc->topology_map->crc_len; i++)
1173103285Sikob		CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1174103285Sikob	p = (u_int32_t *)fc->speed_map;
1175103285Sikob	CSRARC(fc, SPED_MAP) = htonl(*p++);
1176103285Sikob	CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1177103285Sikob	/* don't byte-swap u_int8_t array */
1178103285Sikob	bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1179103285Sikob
1180103285Sikob	fc->max_hop = fc->max_node - i_branch;
1181103285Sikob	printf(", maxhop <= %d", fc->max_hop);
1182103285Sikob
1183103285Sikob	if(fc->irm == -1 ){
1184103285Sikob		printf(", Not found IRM capable node");
1185103285Sikob	}else{
1186103285Sikob		printf(", cable IRM = %d", fc->irm);
1187103285Sikob		if (fc->irm == fc->nodeid)
1188110016Ssimokawa			printf(" (me)");
1189103285Sikob	}
1190110016Ssimokawa	printf("\n");
1191103285Sikob
1192109736Ssimokawa	if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1193114909Ssimokawa		if (fc->irm == fc->nodeid) {
1194103285Sikob			fc->status = FWBUSMGRDONE;
1195103285Sikob			CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1196114909Ssimokawa			fw_bmr(fc);
1197109736Ssimokawa		} else {
1198103285Sikob			fc->status = FWBUSMGRELECT;
1199110193Ssimokawa			callout_reset(&fc->bmr_callout, hz/8,
1200110193Ssimokawa				(void *)fw_try_bmr, (void *)fc);
1201103285Sikob		}
1202114909Ssimokawa	} else
1203103285Sikob		fc->status = FWBUSMGRDONE;
1204114909Ssimokawa
1205108853Ssimokawa	callout_reset(&fc->busprobe_callout, hz/4,
1206108853Ssimokawa			(void *)fw_bus_probe, (void *)fc);
1207103285Sikob}
1208106790Ssimokawa
1209103285Sikob/*
1210103285Sikob * To probe devices on the IEEE1394 bus.
1211103285Sikob */
1212106790Ssimokawastatic void
1213106790Ssimokawafw_bus_probe(struct firewire_comm *fc)
1214103285Sikob{
1215103285Sikob	int s;
1216103285Sikob	struct fw_device *fwdev, *next;
1217103285Sikob
1218103285Sikob	s = splfw();
1219103285Sikob	fc->status = FWBUSEXPLORE;
1220103285Sikob	fc->retry_count = 0;
1221103285Sikob
1222103285Sikob/*
1223103285Sikob * Invalidate all devices, just after bus reset. Devices
1224103285Sikob * to be removed has not been seen longer time.
1225103285Sikob */
1226110193Ssimokawa	for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1227110193Ssimokawa		next = STAILQ_NEXT(fwdev, link);
1228110193Ssimokawa		if (fwdev->status != FWDEVINVAL) {
1229103285Sikob			fwdev->status = FWDEVINVAL;
1230103285Sikob			fwdev->rcnt = 0;
1231110193Ssimokawa		} else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1232103285Sikob			fwdev->rcnt ++;
1233110193Ssimokawa		} else {
1234110193Ssimokawa			STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1235110195Ssimokawa			free(fwdev, M_FW);
1236103285Sikob		}
1237103285Sikob	}
1238103285Sikob	fc->ongonode = 0;
1239103285Sikob	fc->ongoaddr = CSRROMOFF;
1240103285Sikob	fc->ongodev = NULL;
1241103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1242103285Sikob	fw_bus_explore(fc);
1243103285Sikob	splx(s);
1244103285Sikob}
1245106790Ssimokawa
1246103285Sikob/*
1247103285Sikob * To collect device informations on the IEEE1394 bus.
1248103285Sikob */
1249106790Ssimokawastatic void
1250106790Ssimokawafw_bus_explore(struct firewire_comm *fc )
1251103285Sikob{
1252103285Sikob	int err = 0;
1253110179Ssimokawa	struct fw_device *fwdev, *pfwdev, *tfwdev;
1254103285Sikob	u_int32_t addr;
1255103285Sikob	struct fw_xfer *xfer;
1256103285Sikob	struct fw_pkt *fp;
1257103285Sikob
1258103285Sikob	if(fc->status != FWBUSEXPLORE)
1259103285Sikob		return;
1260103285Sikob
1261103285Sikobloop:
1262103285Sikob	if(fc->ongonode == fc->nodeid) fc->ongonode++;
1263103285Sikob
1264103285Sikob	if(fc->ongonode > fc->max_node) goto done;
1265103285Sikob	if(fc->ongonode >= 0x3f) goto done;
1266103285Sikob
1267103285Sikob	/* check link */
1268103285Sikob	/* XXX we need to check phy_id first */
1269103285Sikob	if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1270110577Ssimokawa		if (firewire_debug)
1271110577Ssimokawa			printf("node%d: link down\n", fc->ongonode);
1272103285Sikob		fc->ongonode++;
1273103285Sikob		goto loop;
1274103285Sikob	}
1275103285Sikob
1276103285Sikob	if(fc->ongoaddr <= CSRROMOFF &&
1277103285Sikob		fc->ongoeui.hi == 0xffffffff &&
1278103285Sikob		fc->ongoeui.lo == 0xffffffff ){
1279103285Sikob		fc->ongoaddr = CSRROMOFF;
1280103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1281103285Sikob	}else if(fc->ongoeui.hi == 0xffffffff ){
1282103285Sikob		fc->ongoaddr = CSRROMOFF + 0xc;
1283103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1284103285Sikob	}else if(fc->ongoeui.lo == 0xffffffff ){
1285103285Sikob		fc->ongoaddr = CSRROMOFF + 0x10;
1286103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1287103285Sikob	}else if(fc->ongodev == NULL){
1288110193Ssimokawa		STAILQ_FOREACH(fwdev, &fc->devices, link)
1289110193Ssimokawa			if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1290103285Sikob				break;
1291103285Sikob		if(fwdev != NULL){
1292103285Sikob			fwdev->dst = fc->ongonode;
1293117350Ssimokawa			fwdev->status = FWDEVINIT;
1294117350Ssimokawa			fc->ongodev = fwdev;
1295103285Sikob			fc->ongoaddr = CSRROMOFF;
1296117350Ssimokawa			addr = 0xf0000000 | fc->ongoaddr;
1297117350Ssimokawa			goto dorequest;
1298103285Sikob		}
1299115787Ssimokawa		fwdev = malloc(sizeof(struct fw_device), M_FW,
1300115787Ssimokawa							M_NOWAIT | M_ZERO);
1301103285Sikob		if(fwdev == NULL)
1302103285Sikob			return;
1303106810Ssimokawa		fwdev->fc = fc;
1304103285Sikob		fwdev->rommax = 0;
1305103285Sikob		fwdev->dst = fc->ongonode;
1306103285Sikob		fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1307103285Sikob		fwdev->status = FWDEVINIT;
1308103285Sikob		fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1309103285Sikob
1310110179Ssimokawa		pfwdev = NULL;
1311110193Ssimokawa		STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1312110179Ssimokawa			if (tfwdev->eui.hi > fwdev->eui.hi ||
1313110179Ssimokawa					(tfwdev->eui.hi == fwdev->eui.hi &&
1314110179Ssimokawa					tfwdev->eui.lo > fwdev->eui.lo))
1315110179Ssimokawa				break;
1316110179Ssimokawa			pfwdev = tfwdev;
1317103285Sikob		}
1318110179Ssimokawa		if (pfwdev == NULL)
1319110193Ssimokawa			STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1320110179Ssimokawa		else
1321110193Ssimokawa			STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1322103285Sikob
1323108655Ssimokawa		device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1324107653Ssimokawa			linkspeed[fwdev->speed],
1325107653Ssimokawa			fc->ongoeui.hi, fc->ongoeui.lo);
1326103285Sikob
1327103285Sikob		fc->ongodev = fwdev;
1328103285Sikob		fc->ongoaddr = CSRROMOFF;
1329103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1330103285Sikob	}else{
1331103285Sikob		addr = 0xf0000000 | fc->ongoaddr;
1332103285Sikob	}
1333117350Ssimokawadorequest:
1334103285Sikob#if 0
1335103285Sikob	xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1336103285Sikob		((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1337103285Sikob		fw_bus_explore_callback);
1338103285Sikob	if(xfer == NULL) goto done;
1339103285Sikob#else
1340113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1341103285Sikob	if(xfer == NULL){
1342103285Sikob		goto done;
1343103285Sikob	}
1344103285Sikob	xfer->spd = 0;
1345103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1346113584Ssimokawa	fp->mode.rreqq.dest_hi = 0xffff;
1347103285Sikob	fp->mode.rreqq.tlrt = 0;
1348103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1349103285Sikob	fp->mode.rreqq.pri = 0;
1350103285Sikob	fp->mode.rreqq.src = 0;
1351103285Sikob	xfer->dst = FWLOCALBUS | fc->ongonode;
1352113584Ssimokawa	fp->mode.rreqq.dst = xfer->dst;
1353113584Ssimokawa	fp->mode.rreqq.dest_lo = addr;
1354103285Sikob	xfer->act.hand = fw_bus_explore_callback;
1355103285Sikob
1356110577Ssimokawa	if (firewire_debug)
1357110577Ssimokawa		printf("node%d: explore addr=0x%x\n",
1358110577Ssimokawa				fc->ongonode, fc->ongoaddr);
1359103285Sikob	err = fw_asyreq(fc, -1, xfer);
1360103285Sikob	if(err){
1361103285Sikob		fw_xfer_free( xfer);
1362103285Sikob		return;
1363103285Sikob	}
1364103285Sikob#endif
1365103285Sikob	return;
1366103285Sikobdone:
1367103285Sikob	/* fw_attach_devs */
1368103285Sikob	fc->status = FWBUSEXPDONE;
1369107653Ssimokawa	if (firewire_debug)
1370107653Ssimokawa		printf("bus_explore done\n");
1371103285Sikob	fw_attach_dev(fc);
1372103285Sikob	return;
1373103285Sikob
1374103285Sikob}
1375106790Ssimokawa
1376103285Sikob/* Portable Async. request read quad */
1377106790Ssimokawastruct fw_xfer *
1378106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1379105620Ssimokawa	u_int32_t addr_hi, u_int32_t addr_lo,
1380105620Ssimokawa	void (*hand) __P((struct fw_xfer*)))
1381103285Sikob{
1382103285Sikob	struct fw_xfer *xfer;
1383103285Sikob	struct fw_pkt *fp;
1384103285Sikob	int err;
1385103285Sikob
1386113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1387113584Ssimokawa	if (xfer == NULL)
1388103285Sikob		return NULL;
1389113584Ssimokawa
1390103285Sikob	xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1391103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1392113584Ssimokawa	fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1393103285Sikob	if(tl & FWP_TL_VALID){
1394103285Sikob		fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1395103285Sikob	}else{
1396103285Sikob		fp->mode.rreqq.tlrt = 0;
1397103285Sikob	}
1398103285Sikob	fp->mode.rreqq.tlrt |= rt & 0x3;
1399103285Sikob	fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1400103285Sikob	fp->mode.rreqq.pri = 0;
1401103285Sikob	fp->mode.rreqq.src = 0;
1402103285Sikob	xfer->dst = addr_hi >> 16;
1403113584Ssimokawa	fp->mode.rreqq.dst = xfer->dst;
1404113584Ssimokawa	fp->mode.rreqq.dest_lo = addr_lo;
1405103285Sikob	xfer->act.hand = hand;
1406103285Sikob
1407103285Sikob	err = fw_asyreq(fc, -1, xfer);
1408103285Sikob	if(err){
1409103285Sikob		fw_xfer_free( xfer);
1410103285Sikob		return NULL;
1411103285Sikob	}
1412103285Sikob	return xfer;
1413103285Sikob}
1414106790Ssimokawa
1415103285Sikob/*
1416103285Sikob * Callback for the IEEE1394 bus information collection.
1417103285Sikob */
1418106790Ssimokawastatic void
1419106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer)
1420106790Ssimokawa{
1421103285Sikob	struct firewire_comm *fc;
1422103285Sikob	struct fw_pkt *sfp,*rfp;
1423103285Sikob	struct csrhdr *chdr;
1424103285Sikob	struct csrdir *csrd;
1425103285Sikob	struct csrreg *csrreg;
1426103285Sikob	u_int32_t offset;
1427103285Sikob
1428103285Sikob
1429110577Ssimokawa	if(xfer == NULL) {
1430110577Ssimokawa		printf("xfer == NULL\n");
1431110577Ssimokawa		return;
1432110577Ssimokawa	}
1433103285Sikob	fc = xfer->fc;
1434110577Ssimokawa
1435110577Ssimokawa	if (firewire_debug)
1436110577Ssimokawa		printf("node%d: callback addr=0x%x\n",
1437110577Ssimokawa			fc->ongonode, fc->ongoaddr);
1438110577Ssimokawa
1439103285Sikob	if(xfer->resp != 0){
1440110577Ssimokawa		printf("node%d: resp=%d addr=0x%x\n",
1441110577Ssimokawa			fc->ongonode, xfer->resp, fc->ongoaddr);
1442114284Ssimokawa		goto errnode;
1443103285Sikob	}
1444103285Sikob
1445103285Sikob	if(xfer->send.buf == NULL){
1446110577Ssimokawa		printf("node%d: send.buf=NULL addr=0x%x\n",
1447103285Sikob			fc->ongonode, fc->ongoaddr);
1448114284Ssimokawa		goto errnode;
1449103285Sikob	}
1450103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1451103285Sikob
1452103285Sikob	if(xfer->recv.buf == NULL){
1453110577Ssimokawa		printf("node%d: recv.buf=NULL addr=0x%x\n",
1454103285Sikob			fc->ongonode, fc->ongoaddr);
1455114284Ssimokawa		goto errnode;
1456103285Sikob	}
1457103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1458103285Sikob#if 0
1459103285Sikob	{
1460103285Sikob		u_int32_t *qld;
1461103285Sikob		int i;
1462103285Sikob		qld = (u_int32_t *)xfer->recv.buf;
1463103285Sikob		printf("len:%d\n", xfer->recv.len);
1464103285Sikob		for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1465113584Ssimokawa			printf("0x%08x ", rfp->mode.ld[i/4]);
1466103285Sikob			if((i % 16) == 15) printf("\n");
1467103285Sikob		}
1468103285Sikob		if((i % 16) != 15) printf("\n");
1469103285Sikob	}
1470103285Sikob#endif
1471103285Sikob	if(fc->ongodev == NULL){
1472113584Ssimokawa		if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1473103285Sikob			rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1474103285Sikob			chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1475110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */
1476103285Sikob			if(chdr->info_len == 1){
1477110577Ssimokawa				if (firewire_debug)
1478110577Ssimokawa					printf("node%d: minimal config\n",
1479110577Ssimokawa								fc->ongonode);
1480103285Sikob				goto nextnode;
1481103285Sikob			}else{
1482103285Sikob				fc->ongoaddr = CSRROMOFF + 0xc;
1483103285Sikob			}
1484113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1485103285Sikob			fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1486103285Sikob			fc->ongoaddr = CSRROMOFF + 0x10;
1487113584Ssimokawa		}else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1488103285Sikob			fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1489110577Ssimokawa			if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1490110577Ssimokawa				if (firewire_debug)
1491110577Ssimokawa					printf("node%d: eui64 is zero.\n",
1492110577Ssimokawa							fc->ongonode);
1493103285Sikob				goto nextnode;
1494110577Ssimokawa			}
1495103285Sikob			fc->ongoaddr = CSRROMOFF;
1496103285Sikob		}
1497103285Sikob	}else{
1498117350Ssimokawa		if (fc->ongoaddr == CSRROMOFF &&
1499117350Ssimokawa		    fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) {
1500117350Ssimokawa			fc->ongodev->status = FWDEVATTACHED;
1501117350Ssimokawa			goto nextnode;
1502117350Ssimokawa		}
1503103285Sikob		fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1504103285Sikob		if(fc->ongoaddr > fc->ongodev->rommax){
1505103285Sikob			fc->ongodev->rommax = fc->ongoaddr;
1506103285Sikob		}
1507103285Sikob		csrd = SLIST_FIRST(&fc->ongocsr);
1508103285Sikob		if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1509103285Sikob			chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1510103285Sikob			offset = CSRROMOFF;
1511103285Sikob		}else{
1512103285Sikob			chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1513103285Sikob			offset = csrd->off;
1514103285Sikob		}
1515103285Sikob		if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1516103285Sikob			csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1517103285Sikob			if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1518103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1519103285Sikob				if(csrd == NULL){
1520103285Sikob					goto nextnode;
1521103285Sikob				}else{
1522103285Sikob					csrd->ongoaddr = fc->ongoaddr;
1523103285Sikob					fc->ongoaddr += csrreg->val * 4;
1524103285Sikob					csrd->off = fc->ongoaddr;
1525103285Sikob					SLIST_REMOVE_HEAD(&fc->csrfree, link);
1526103285Sikob					SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1527103285Sikob					goto nextaddr;
1528103285Sikob				}
1529103285Sikob			}
1530103285Sikob		}
1531103285Sikob		fc->ongoaddr += 4;
1532103285Sikob		if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1533103285Sikob				(fc->ongodev->rommax < 0x414)){
1534103285Sikob			if(fc->ongodev->rommax <= 0x414){
1535103285Sikob				csrd = SLIST_FIRST(&fc->csrfree);
1536103285Sikob				if(csrd == NULL) goto nextnode;
1537103285Sikob				csrd->off = fc->ongoaddr;
1538103285Sikob				csrd->ongoaddr = fc->ongoaddr;
1539103285Sikob				SLIST_REMOVE_HEAD(&fc->csrfree, link);
1540103285Sikob				SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1541103285Sikob			}
1542103285Sikob			goto nextaddr;
1543103285Sikob		}
1544103285Sikob
1545103285Sikob		while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1546103285Sikob			if(csrd == NULL){
1547103285Sikob				goto nextnode;
1548103285Sikob			};
1549103285Sikob			fc->ongoaddr = csrd->ongoaddr + 4;
1550103285Sikob			SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1551103285Sikob			SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1552103285Sikob			csrd = SLIST_FIRST(&fc->ongocsr);
1553103285Sikob			if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1554103285Sikob				chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1555103285Sikob				offset = CSRROMOFF;
1556103285Sikob			}else{
1557103285Sikob				chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1558103285Sikob				offset = csrd->off;
1559103285Sikob			}
1560103285Sikob		}
1561103285Sikob		if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1562103285Sikob			goto nextnode;
1563103285Sikob		}
1564103285Sikob	}
1565103285Sikobnextaddr:
1566103285Sikob	fw_xfer_free( xfer);
1567103285Sikob	fw_bus_explore(fc);
1568103285Sikob	return;
1569114284Ssimokawaerrnode:
1570114284Ssimokawa	fc->retry_count++;
1571114284Ssimokawa	if (fc->ongodev != NULL)
1572114284Ssimokawa		fc->ongodev->status = FWDEVINVAL;
1573103285Sikobnextnode:
1574103285Sikob	fw_xfer_free( xfer);
1575103285Sikob	fc->ongonode++;
1576103285Sikob/* housekeeping work space */
1577103285Sikob	fc->ongoaddr = CSRROMOFF;
1578103285Sikob	fc->ongodev = NULL;
1579103285Sikob	fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1580103285Sikob	while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1581103285Sikob		SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1582103285Sikob		SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1583103285Sikob	}
1584103285Sikob	fw_bus_explore(fc);
1585103285Sikob	return;
1586103285Sikob}
1587103285Sikob
1588103285Sikob/*
1589103285Sikob * To attach sub-devices layer onto IEEE1394 bus.
1590103285Sikob */
1591106815Ssimokawastatic void
1592106815Ssimokawafw_attach_dev(struct firewire_comm *fc)
1593103285Sikob{
1594103285Sikob	struct fw_device *fwdev;
1595103285Sikob	struct fw_xfer *xfer;
1596103285Sikob	int i, err;
1597103285Sikob	device_t *devlistp;
1598103285Sikob	int devcnt;
1599103285Sikob	struct firewire_dev_comm *fdc;
1600103285Sikob
1601114069Ssimokawa	STAILQ_FOREACH(fwdev, &fc->devices, link)
1602114069Ssimokawa		if (fwdev->status == FWDEVINIT)
1603114069Ssimokawa			fwdev->status = FWDEVATTACHED;
1604103285Sikob
1605108773Ssimokawa	err = device_get_children(fc->bdev, &devlistp, &devcnt);
1606103285Sikob	if( err != 0 )
1607103285Sikob		return;
1608103285Sikob	for( i = 0 ; i < devcnt ; i++){
1609103285Sikob		if (device_get_state(devlistp[i]) >= DS_ATTACHED)  {
1610103285Sikob			fdc = device_get_softc(devlistp[i]);
1611103285Sikob			if (fdc->post_explore != NULL)
1612103285Sikob				fdc->post_explore(fdc);
1613103285Sikob		}
1614103285Sikob	}
1615103285Sikob	free(devlistp, M_TEMP);
1616103285Sikob
1617103285Sikob	/* call pending handlers */
1618103285Sikob	i = 0;
1619103285Sikob	while ((xfer = STAILQ_FIRST(&fc->pending))) {
1620103285Sikob		STAILQ_REMOVE_HEAD(&fc->pending, link);
1621103285Sikob		i++;
1622103285Sikob		if (xfer->act.hand)
1623103285Sikob			xfer->act.hand(xfer);
1624103285Sikob	}
1625103285Sikob	if (i > 0)
1626103285Sikob		printf("fw_attach_dev: %d pending handlers called\n", i);
1627103285Sikob	if (fc->retry_count > 0) {
1628111040Ssimokawa		printf("probe failed for %d node\n", fc->retry_count);
1629111040Ssimokawa#if 0
1630110193Ssimokawa		callout_reset(&fc->retry_probe_callout, hz*2,
1631110193Ssimokawa					(void *)fc->ibr, (void *)fc);
1632111040Ssimokawa#endif
1633103285Sikob	}
1634103285Sikob	return;
1635103285Sikob}
1636106815Ssimokawa
1637103285Sikob/*
1638103285Sikob * To allocate uniq transaction label.
1639103285Sikob */
1640106815Ssimokawastatic int
1641106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1642103285Sikob{
1643103285Sikob	u_int i;
1644103285Sikob	struct tlabel *tl, *tmptl;
1645103285Sikob	int s;
1646103285Sikob	static u_int32_t label = 0;
1647103285Sikob
1648103285Sikob	s = splfw();
1649103285Sikob	for( i = 0 ; i < 0x40 ; i ++){
1650103285Sikob		label = (label + 1) & 0x3f;
1651103285Sikob		for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1652103285Sikob			tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1653103285Sikob			if(tmptl->xfer->dst == xfer->dst) break;
1654103285Sikob		}
1655103285Sikob		if(tmptl == NULL) {
1656110195Ssimokawa			tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1657103285Sikob			if (tl == NULL) {
1658103285Sikob				splx(s);
1659103285Sikob				return (-1);
1660103285Sikob			}
1661103285Sikob			tl->xfer = xfer;
1662103285Sikob			STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1663103285Sikob			splx(s);
1664110577Ssimokawa			if (firewire_debug > 1)
1665110577Ssimokawa				printf("fw_get_tlabel: dst=%d tl=%d\n",
1666110577Ssimokawa						xfer->dst, label);
1667103285Sikob			return(label);
1668103285Sikob		}
1669103285Sikob	}
1670103285Sikob	splx(s);
1671103285Sikob
1672103285Sikob	printf("fw_get_tlabel: no free tlabel\n");
1673103285Sikob	return(-1);
1674103285Sikob}
1675106815Ssimokawa
1676113584Ssimokawastatic void
1677113584Ssimokawafw_rcv_copy(struct fw_xfer *xfer, struct iovec *vec, int nvec)
1678113584Ssimokawa{
1679113584Ssimokawa	char *p;
1680113584Ssimokawa	int res, i, len;
1681113584Ssimokawa
1682113584Ssimokawa	p = xfer->recv.buf;
1683113584Ssimokawa	res = xfer->recv.len;
1684113584Ssimokawa	for (i = 0; i < nvec; i++, vec++) {
1685113584Ssimokawa		len = vec->iov_len;
1686113584Ssimokawa		if (res < len) {
1687113584Ssimokawa			printf("rcv buffer(%d) is %d bytes short.\n",
1688113584Ssimokawa						xfer->recv.len, len - res);
1689113584Ssimokawa			len = res;
1690113584Ssimokawa		}
1691113584Ssimokawa		bcopy(vec->iov_base, p, len);
1692113584Ssimokawa		p += len;
1693113584Ssimokawa		res -= len;
1694113584Ssimokawa		if (res <= 0)
1695113584Ssimokawa			break;
1696113584Ssimokawa	}
1697113584Ssimokawa	xfer->recv.len -= res;
1698113584Ssimokawa}
1699113584Ssimokawa
1700103285Sikob/*
1701103285Sikob * Generic packet receving process.
1702103285Sikob */
1703106815Ssimokawavoid
1704113584Ssimokawafw_rcv(struct firewire_comm *fc, struct iovec *vec, int nvec, u_int sub, u_int spd)
1705103285Sikob{
1706103285Sikob	struct fw_pkt *fp, *resfp;
1707103285Sikob	struct fw_xfer *xfer;
1708103285Sikob	struct fw_bind *bind;
1709103285Sikob	struct firewire_softc *sc;
1710113584Ssimokawa	int tcode, s;
1711113584Ssimokawa	int i, len, oldstate;
1712103285Sikob#if 0
1713103285Sikob	{
1714103285Sikob		u_int32_t *qld;
1715103285Sikob		int i;
1716103285Sikob		qld = (u_int32_t *)buf;
1717103285Sikob		printf("spd %d len:%d\n", spd, len);
1718103285Sikob		for( i = 0 ; i <= len && i < 32; i+= 4){
1719103285Sikob			printf("0x%08x ", ntohl(qld[i/4]));
1720103285Sikob			if((i % 16) == 15) printf("\n");
1721103285Sikob		}
1722103285Sikob		if((i % 16) != 15) printf("\n");
1723103285Sikob	}
1724103285Sikob#endif
1725113584Ssimokawa	fp = (struct fw_pkt *)vec[0].iov_base;
1726113584Ssimokawa	tcode = fp->mode.common.tcode;
1727113584Ssimokawa#if 0 /* XXX this check is not valid for RRESQ and WREQQ */
1728113584Ssimokawa	if (vec[0].iov_len < fc->tcode[tcode].hdr_len) {
1729113584Ssimokawa#if __FreeBSD_version >= 500000
1730113584Ssimokawa		printf("fw_rcv: iov_len(%zu) is less than"
1731113584Ssimokawa#else
1732113584Ssimokawa		printf("fw_rcv: iov_len(%u) is less than"
1733113584Ssimokawa#endif
1734113584Ssimokawa			" hdr_len(%d:tcode=%d)\n", vec[0].iov_len,
1735113584Ssimokawa			fc->tcode[tcode].hdr_len, tcode);
1736113584Ssimokawa	}
1737113584Ssimokawa#endif
1738113584Ssimokawa	switch (tcode) {
1739103285Sikob	case FWTCODE_WRES:
1740103285Sikob	case FWTCODE_RRESQ:
1741103285Sikob	case FWTCODE_RRESB:
1742103285Sikob	case FWTCODE_LRES:
1743113584Ssimokawa		xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1744103285Sikob					fp->mode.hdr.tlrt >> 2);
1745103285Sikob		if(xfer == NULL) {
1746103285Sikob			printf("fw_rcv: unknown response "
1747110891Ssimokawa					"tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1748113584Ssimokawa					tcode,
1749113584Ssimokawa					fp->mode.hdr.src,
1750103285Sikob					fp->mode.hdr.tlrt >> 2,
1751103285Sikob					fp->mode.hdr.tlrt & 3,
1752103285Sikob					fp->mode.rresq.data);
1753103285Sikob#if 1
1754103285Sikob			printf("try ad-hoc work around!!\n");
1755113584Ssimokawa			xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1756103285Sikob					(fp->mode.hdr.tlrt >> 2)^3);
1757103285Sikob			if (xfer == NULL) {
1758103285Sikob				printf("no use...\n");
1759103285Sikob				goto err;
1760103285Sikob			}
1761103285Sikob#else
1762103285Sikob			goto err;
1763103285Sikob#endif
1764103285Sikob		}
1765113584Ssimokawa		fw_rcv_copy(xfer, vec, nvec);
1766113584Ssimokawa		xfer->resp = 0;
1767113584Ssimokawa		/* make sure the packet is drained in AT queue */
1768113584Ssimokawa		oldstate = xfer->state;
1769113584Ssimokawa		xfer->state = FWXF_RCVD;
1770113584Ssimokawa		switch (oldstate) {
1771113584Ssimokawa		case FWXF_SENT:
1772103285Sikob			fw_xfer_done(xfer);
1773103285Sikob			break;
1774113584Ssimokawa		case FWXF_START:
1775119289Ssimokawa#if 0
1776113584Ssimokawa			if (firewire_debug)
1777117716Ssimokawa				printf("not sent yet tl=%x\n", xfer->tl);
1778119289Ssimokawa#endif
1779113584Ssimokawa			break;
1780103285Sikob		default:
1781113584Ssimokawa			printf("unexpected state %d\n", xfer->state);
1782103285Sikob		}
1783113584Ssimokawa		return;
1784103285Sikob	case FWTCODE_WREQQ:
1785103285Sikob	case FWTCODE_WREQB:
1786103285Sikob	case FWTCODE_RREQQ:
1787103285Sikob	case FWTCODE_RREQB:
1788103285Sikob	case FWTCODE_LREQ:
1789113584Ssimokawa		bind = fw_bindlookup(fc, fp->mode.rreqq.dest_hi,
1790113584Ssimokawa			fp->mode.rreqq.dest_lo);
1791103285Sikob		if(bind == NULL){
1792113962Ssimokawa#if __FreeBSD_version >= 500000
1793113584Ssimokawa			printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%x\n",
1794113962Ssimokawa#else
1795113962Ssimokawa			printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%lx\n",
1796113962Ssimokawa#endif
1797113584Ssimokawa				fp->mode.wreqq.dest_hi,
1798113584Ssimokawa				fp->mode.wreqq.dest_lo,
1799113584Ssimokawa				tcode,
1800113584Ssimokawa				fp->mode.hdr.src,
1801113584Ssimokawa				ntohl(fp->mode.wreqq.data));
1802103285Sikob			if (fc->status == FWBUSRESET) {
1803110798Ssimokawa				printf("fw_rcv: cannot respond(bus reset)!\n");
1804103285Sikob				goto err;
1805103285Sikob			}
1806113584Ssimokawa			xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 0);
1807103285Sikob			if(xfer == NULL){
1808103285Sikob				return;
1809103285Sikob			}
1810103285Sikob			xfer->spd = spd;
1811103285Sikob			resfp = (struct fw_pkt *)xfer->send.buf;
1812113584Ssimokawa			switch (tcode) {
1813103285Sikob			case FWTCODE_WREQQ:
1814103285Sikob			case FWTCODE_WREQB:
1815103285Sikob				resfp->mode.hdr.tcode = FWTCODE_WRES;
1816103285Sikob				xfer->send.len = 12;
1817103285Sikob				break;
1818103285Sikob			case FWTCODE_RREQQ:
1819103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1820103285Sikob				xfer->send.len = 16;
1821103285Sikob				break;
1822103285Sikob			case FWTCODE_RREQB:
1823103285Sikob				resfp->mode.hdr.tcode = FWTCODE_RRESB;
1824103285Sikob				xfer->send.len = 16;
1825103285Sikob				break;
1826103285Sikob			case FWTCODE_LREQ:
1827103285Sikob				resfp->mode.hdr.tcode = FWTCODE_LRES;
1828103285Sikob				xfer->send.len = 16;
1829103285Sikob				break;
1830103285Sikob			}
1831103285Sikob			resfp->mode.hdr.dst = fp->mode.hdr.src;
1832103285Sikob			resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1833103285Sikob			resfp->mode.hdr.pri = fp->mode.hdr.pri;
1834103285Sikob			resfp->mode.rresb.rtcode = 7;
1835103285Sikob			resfp->mode.rresb.extcode = 0;
1836103285Sikob			resfp->mode.rresb.len = 0;
1837103285Sikob/*
1838103285Sikob			xfer->act.hand = fw_asy_callback;
1839103285Sikob*/
1840103285Sikob			xfer->act.hand = fw_xfer_free;
1841103285Sikob			if(fw_asyreq(fc, -1, xfer)){
1842103285Sikob				fw_xfer_free( xfer);
1843103285Sikob				return;
1844103285Sikob			}
1845103285Sikob			goto err;
1846103285Sikob		}
1847113584Ssimokawa		len = 0;
1848113584Ssimokawa		for (i = 0; i < nvec; i ++)
1849113584Ssimokawa			len += vec[i].iov_len;
1850113584Ssimokawa		switch(bind->act_type){
1851103285Sikob		case FWACT_XFER:
1852113584Ssimokawa			/* splfw()?? */
1853113584Ssimokawa			xfer = STAILQ_FIRST(&bind->xferlist);
1854113584Ssimokawa			if (xfer == NULL) {
1855113584Ssimokawa				printf("Discard a packet for this bind.\n");
1856113584Ssimokawa				goto err;
1857113584Ssimokawa			}
1858113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1859113584Ssimokawa			fw_rcv_copy(xfer, vec, nvec);
1860103285Sikob			xfer->spd = spd;
1861103285Sikob			if (fc->status != FWBUSRESET)
1862103285Sikob				xfer->act.hand(xfer);
1863103285Sikob			else
1864103285Sikob				STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1865103285Sikob			return;
1866103285Sikob			break;
1867103285Sikob		case FWACT_CH:
1868113584Ssimokawa			if(fc->ir[bind->sub]->queued >=
1869113584Ssimokawa				fc->ir[bind->sub]->maxq){
1870108655Ssimokawa				device_printf(fc->bdev,
1871108655Ssimokawa					"Discard a packet %x %d\n",
1872113584Ssimokawa					bind->sub,
1873113584Ssimokawa					fc->ir[bind->sub]->queued);
1874103285Sikob				goto err;
1875103285Sikob			}
1876113584Ssimokawa			xfer = STAILQ_FIRST(&bind->xferlist);
1877113584Ssimokawa			if (xfer == NULL) {
1878113584Ssimokawa				printf("Discard packet for this bind\n");
1879113584Ssimokawa				goto err;
1880113584Ssimokawa			}
1881113584Ssimokawa			STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1882113584Ssimokawa			fw_rcv_copy(xfer, vec, nvec);
1883103285Sikob			xfer->spd = spd;
1884103285Sikob			s = splfw();
1885113584Ssimokawa			fc->ir[bind->sub]->queued++;
1886113584Ssimokawa			STAILQ_INSERT_TAIL(&fc->ir[bind->sub]->q, xfer, link);
1887103285Sikob			splx(s);
1888103285Sikob
1889113584Ssimokawa			wakeup((caddr_t)fc->ir[bind->sub]);
1890103285Sikob
1891103285Sikob			return;
1892103285Sikob			break;
1893103285Sikob		default:
1894103285Sikob			goto err;
1895103285Sikob			break;
1896103285Sikob		}
1897103285Sikob		break;
1898103285Sikob	case FWTCODE_STREAM:
1899103285Sikob	{
1900103285Sikob		struct fw_xferq *xferq;
1901103285Sikob
1902103285Sikob		xferq = fc->ir[sub];
1903103285Sikob#if 0
1904103285Sikob		printf("stream rcv dma %d len %d off %d spd %d\n",
1905103285Sikob			sub, len, off, spd);
1906103285Sikob#endif
1907103285Sikob		if(xferq->queued >= xferq->maxq) {
1908103285Sikob			printf("receive queue is full\n");
1909103285Sikob			goto err;
1910103285Sikob		}
1911113584Ssimokawa		/* XXX get xfer from xfer queue, we don't need copy for
1912113584Ssimokawa			per packet mode */
1913113584Ssimokawa		xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1914113584Ssimokawa						vec[0].iov_len);
1915103285Sikob		if(xfer == NULL) goto err;
1916113584Ssimokawa		fw_rcv_copy(xfer, vec, nvec);
1917103285Sikob		xfer->spd = spd;
1918103285Sikob		s = splfw();
1919103285Sikob		xferq->queued++;
1920103285Sikob		STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1921103285Sikob		splx(s);
1922103285Sikob		sc = device_get_softc(fc->bdev);
1923103285Sikob#if __FreeBSD_version >= 500000
1924103285Sikob		if (SEL_WAITING(&xferq->rsel))
1925103285Sikob#else
1926103285Sikob		if (&xferq->rsel.si_pid != 0)
1927103285Sikob#endif
1928103285Sikob			selwakeup(&xferq->rsel);
1929103285Sikob		if (xferq->flag & FWXFERQ_WAKEUP) {
1930103285Sikob			xferq->flag &= ~FWXFERQ_WAKEUP;
1931103285Sikob			wakeup((caddr_t)xferq);
1932103285Sikob		}
1933103285Sikob		if (xferq->flag & FWXFERQ_HANDLER) {
1934103285Sikob			xferq->hand(xferq);
1935103285Sikob		}
1936103285Sikob		return;
1937103285Sikob		break;
1938103285Sikob	}
1939103285Sikob	default:
1940113584Ssimokawa		printf("fw_rcv: unknow tcode %d\n", tcode);
1941103285Sikob		break;
1942103285Sikob	}
1943103285Sikoberr:
1944113584Ssimokawa	return;
1945103285Sikob}
1946106815Ssimokawa
1947103285Sikob/*
1948103285Sikob * Post process for Bus Manager election process.
1949103285Sikob */
1950103285Sikobstatic void
1951103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer)
1952103285Sikob{
1953109422Ssimokawa	struct fw_pkt *rfp;
1954103285Sikob	struct firewire_comm *fc;
1955109422Ssimokawa	int bmr;
1956103285Sikob
1957109422Ssimokawa	if (xfer == NULL)
1958109422Ssimokawa		return;
1959103285Sikob	fc = xfer->fc;
1960109422Ssimokawa	if (xfer->resp != 0)
1961103285Sikob		goto error;
1962109422Ssimokawa	if (xfer->send.buf == NULL)
1963103285Sikob		goto error;
1964109422Ssimokawa	if (xfer->recv.buf == NULL)
1965103285Sikob		goto error;
1966103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1967109422Ssimokawa	if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1968109422Ssimokawa		goto error;
1969109422Ssimokawa
1970109422Ssimokawa	bmr = ntohl(rfp->mode.lres.payload[0]);
1971109422Ssimokawa	if (bmr == 0x3f)
1972109422Ssimokawa		bmr = fc->nodeid;
1973109422Ssimokawa
1974109422Ssimokawa	CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1975114909Ssimokawa	fw_xfer_free(xfer);
1976114909Ssimokawa	fw_bmr(fc);
1977114909Ssimokawa	return;
1978114909Ssimokawa
1979103285Sikoberror:
1980114909Ssimokawa	device_printf(fc->bdev, "bus manager election failed\n");
1981103285Sikob	fw_xfer_free(xfer);
1982103285Sikob}
1983106815Ssimokawa
1984113584Ssimokawa
1985103285Sikob/*
1986103285Sikob * To candidate Bus Manager election process.
1987103285Sikob */
1988110270Ssimokawastatic void
1989106815Ssimokawafw_try_bmr(void *arg)
1990103285Sikob{
1991103285Sikob	struct fw_xfer *xfer;
1992103285Sikob	struct firewire_comm *fc = (struct firewire_comm *)arg;
1993103285Sikob	struct fw_pkt *fp;
1994103285Sikob	int err = 0;
1995103285Sikob
1996113584Ssimokawa	xfer = fw_xfer_alloc_buf(M_FWXFER, 24, 20);
1997103285Sikob	if(xfer == NULL){
1998103285Sikob		return;
1999103285Sikob	}
2000103285Sikob	xfer->spd = 0;
2001103285Sikob	fc->status = FWBUSMGRELECT;
2002103285Sikob
2003103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
2004113584Ssimokawa	fp->mode.lreq.dest_hi = 0xffff;
2005103285Sikob	fp->mode.lreq.tlrt = 0;
2006103285Sikob	fp->mode.lreq.tcode = FWTCODE_LREQ;
2007103285Sikob	fp->mode.lreq.pri = 0;
2008103285Sikob	fp->mode.lreq.src = 0;
2009113584Ssimokawa	fp->mode.lreq.len = 8;
2010113584Ssimokawa	fp->mode.lreq.extcode = FW_LREQ_CMPSWAP;
2011103285Sikob	xfer->dst = FWLOCALBUS | fc->irm;
2012113584Ssimokawa	fp->mode.lreq.dst = xfer->dst;
2013113584Ssimokawa	fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2014109422Ssimokawa	fp->mode.lreq.payload[0] = htonl(0x3f);
2015109422Ssimokawa	fp->mode.lreq.payload[1] = htonl(fc->nodeid);
2016103285Sikob	xfer->act.hand = fw_try_bmr_callback;
2017103285Sikob
2018103285Sikob	err = fw_asyreq(fc, -1, xfer);
2019103285Sikob	if(err){
2020103285Sikob		fw_xfer_free( xfer);
2021103285Sikob		return;
2022103285Sikob	}
2023103285Sikob	return;
2024103285Sikob}
2025106543Ssimokawa
2026106543Ssimokawa#ifdef FW_VMACCESS
2027103285Sikob/*
2028103285Sikob * Software implementation for physical memory block access.
2029103285Sikob * XXX:Too slow, usef for debug purpose only.
2030103285Sikob */
2031106815Ssimokawastatic void
2032106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){
2033103285Sikob	struct fw_pkt *rfp, *sfp = NULL;
2034113584Ssimokawa	u_int32_t *ld = (u_int32_t *)xfer->recv.buf;
2035103285Sikob
2036113584Ssimokawa	printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2037113584Ssimokawa			xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2038103285Sikob	printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2039103285Sikob	if(xfer->resp != 0){
2040103285Sikob		fw_xfer_free( xfer);
2041103285Sikob		return;
2042103285Sikob	}
2043103285Sikob	if(xfer->recv.buf == NULL){
2044103285Sikob		fw_xfer_free( xfer);
2045103285Sikob		return;
2046103285Sikob	}
2047103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
2048103285Sikob	switch(rfp->mode.hdr.tcode){
2049103285Sikob		/* XXX need fix for 64bit arch */
2050103285Sikob		case FWTCODE_WREQB:
2051110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2052103285Sikob			xfer->send.len = 12;
2053103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2054103285Sikob			bcopy(rfp->mode.wreqb.payload,
2055103285Sikob				(caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2056103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2057103285Sikob			sfp->mode.wres.rtcode = 0;
2058103285Sikob			break;
2059103285Sikob		case FWTCODE_WREQQ:
2060110195Ssimokawa			xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2061103285Sikob			xfer->send.len = 12;
2062103285Sikob			sfp->mode.wres.tcode = FWTCODE_WRES;
2063103285Sikob			*((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2064103285Sikob			sfp->mode.wres.rtcode = 0;
2065103285Sikob			break;
2066103285Sikob		case FWTCODE_RREQB:
2067110195Ssimokawa			xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2068103285Sikob			xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2069103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2070103285Sikob			bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2071103285Sikob				sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
2072103285Sikob			sfp->mode.rresb.tcode = FWTCODE_RRESB;
2073103285Sikob			sfp->mode.rresb.len = rfp->mode.rreqb.len;
2074103285Sikob			sfp->mode.rresb.rtcode = 0;
2075103285Sikob			sfp->mode.rresb.extcode = 0;
2076103285Sikob			break;
2077103285Sikob		case FWTCODE_RREQQ:
2078110195Ssimokawa			xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2079103285Sikob			xfer->send.len = 16;
2080103285Sikob			sfp = (struct fw_pkt *)xfer->send.buf;
2081103285Sikob			sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2082103285Sikob			sfp->mode.wres.tcode = FWTCODE_RRESQ;
2083103285Sikob			sfp->mode.rresb.rtcode = 0;
2084103285Sikob			break;
2085103285Sikob		default:
2086103285Sikob			fw_xfer_free( xfer);
2087103285Sikob			return;
2088103285Sikob	}
2089103285Sikob	sfp->mode.hdr.dst = rfp->mode.hdr.src;
2090103285Sikob	xfer->dst = ntohs(rfp->mode.hdr.src);
2091103285Sikob	xfer->act.hand = fw_xfer_free;
2092103285Sikob	xfer->retry_req = fw_asybusy;
2093103285Sikob
2094103285Sikob	sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2095103285Sikob	sfp->mode.hdr.pri = 0;
2096103285Sikob
2097103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
2098103285Sikob/**/
2099103285Sikob	return;
2100103285Sikob}
2101106543Ssimokawa#endif
2102106543Ssimokawa
2103103285Sikob/*
2104103285Sikob * CRC16 check-sum for IEEE1394 register blocks.
2105103285Sikob */
2106106815Ssimokawau_int16_t
2107106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){
2108103285Sikob	u_int32_t i, sum, crc = 0;
2109103285Sikob	int shift;
2110103285Sikob	len = (len + 3) & ~3;
2111103285Sikob	for(i = 0 ; i < len ; i+= 4){
2112103285Sikob		for( shift = 28 ; shift >= 0 ; shift -= 4){
2113103285Sikob			sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2114103285Sikob			crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2115103285Sikob		}
2116103285Sikob		crc &= 0xffff;
2117103285Sikob	}
2118103285Sikob	return((u_int16_t) crc);
2119103285Sikob}
2120106815Ssimokawa
2121110270Ssimokawastatic int
2122110072Ssimokawafw_bmr(struct firewire_comm *fc)
2123110072Ssimokawa{
2124110072Ssimokawa	struct fw_device fwdev;
2125113584Ssimokawa	union fw_self_id *self_id;
2126110072Ssimokawa	int cmstr;
2127110072Ssimokawa
2128113584Ssimokawa	/* Check to see if the current root node is cycle master capable */
2129113584Ssimokawa	self_id = &fc->topology_map->self_id[fc->max_node];
2130113584Ssimokawa	if (fc->max_node > 0) {
2131114909Ssimokawa		/* XXX check cmc bit of businfo block rather than contender */
2132114909Ssimokawa		if (self_id->p0.link_active && self_id->p0.contender)
2133113584Ssimokawa			cmstr = fc->max_node;
2134114909Ssimokawa		else {
2135114909Ssimokawa			device_printf(fc->bdev,
2136114909Ssimokawa				"root node is not cycle master capable\n");
2137114909Ssimokawa			/* XXX shall we be the cycle master? */
2138113584Ssimokawa			cmstr = fc->nodeid;
2139114909Ssimokawa			/* XXX need bus reset */
2140114909Ssimokawa		}
2141113584Ssimokawa	} else
2142113584Ssimokawa		cmstr = -1;
2143114909Ssimokawa
2144114909Ssimokawa	device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2145114909Ssimokawa	if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2146114909Ssimokawa		/* We are not the bus manager */
2147114909Ssimokawa		printf("\n");
2148114909Ssimokawa		return(0);
2149114909Ssimokawa	}
2150114909Ssimokawa	printf("(me)\n");
2151114909Ssimokawa
2152114909Ssimokawa	/* Optimize gapcount */
2153113584Ssimokawa	if(fc->max_hop <= MAX_GAPHOP )
2154113584Ssimokawa		fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2155110072Ssimokawa	/* If we are the cycle master, nothing to do */
2156113584Ssimokawa	if (cmstr == fc->nodeid || cmstr == -1)
2157110072Ssimokawa		return 0;
2158110072Ssimokawa	/* Bus probe has not finished, make dummy fwdev for cmstr */
2159110072Ssimokawa	bzero(&fwdev, sizeof(fwdev));
2160110072Ssimokawa	fwdev.fc = fc;
2161110072Ssimokawa	fwdev.dst = cmstr;
2162110072Ssimokawa	fwdev.speed = 0;
2163110072Ssimokawa	fwdev.maxrec = 8; /* 512 */
2164110072Ssimokawa	fwdev.status = FWDEVINIT;
2165110072Ssimokawa	/* Set cmstr bit on the cycle master */
2166110072Ssimokawa	fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2167114909Ssimokawa		0xffff, 0xf0000000 | STATE_SET, htonl(1 << 8),
2168110072Ssimokawa		fw_asy_callback_free);
2169110072Ssimokawa
2170110072Ssimokawa	return 0;
2171110072Ssimokawa}
2172110072Ssimokawa
2173118455Ssimokawastatic int
2174118455Ssimokawafw_modevent(module_t mode, int type, void *data)
2175118455Ssimokawa{
2176118455Ssimokawa	int err = 0;
2177118455Ssimokawa#if __FreeBSD_version >= 500000
2178118455Ssimokawa	static eventhandler_tag fwdev_ehtag = NULL;
2179118455Ssimokawa#endif
2180118455Ssimokawa
2181118455Ssimokawa	switch (type) {
2182118455Ssimokawa	case MOD_LOAD:
2183118455Ssimokawa#if __FreeBSD_version >= 500000
2184118455Ssimokawa		fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2185118455Ssimokawa						fwdev_clone, 0, 1000);
2186118455Ssimokawa#endif
2187118455Ssimokawa		break;
2188118455Ssimokawa	case MOD_UNLOAD:
2189118455Ssimokawa#if __FreeBSD_version >= 500000
2190118455Ssimokawa		if (fwdev_ehtag != NULL)
2191118455Ssimokawa			EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2192118455Ssimokawa#endif
2193118455Ssimokawa		break;
2194118455Ssimokawa	case MOD_SHUTDOWN:
2195118455Ssimokawa		break;
2196118455Ssimokawa	}
2197118455Ssimokawa	return (err);
2198118455Ssimokawa}
2199118455Ssimokawa
2200118455Ssimokawa
2201118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2202103285SikobMODULE_VERSION(firewire, 1);
2203