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