1 /* $NetBSD: firewire.c,v 1.24 2009/03/18 16:00:18 cegger Exp $ */
3 * Copyright (c) 2003 Hidetoshi Shimokawa
4 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the acknowledgement as bellow:
18 * This product includes software developed by K. Kobayashi and H. Shimokawa
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 * $FreeBSD: src/sys/dev/firewire/firewire.c,v 1.101 2007/10/20 23:23:14 julian Exp $
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: firewire.c,v 1.24 2009/03/18 16:00:18 cegger Exp $");
42 #if defined(__FreeBSD__)
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
51 #include <sys/sysctl.h>
52 #include <sys/kthread.h>
56 #if defined(__DragonFly__) || __FreeBSD_version < 500000
57 #include <machine/clock.h> /* for DELAY() */
60 #include <sys/bus.h> /* used by smbus and newbus */
66 #include "firewirereg.h"
71 #include <dev/firewire/fw_port.h>
72 #include <dev/firewire/firewire.h>
73 #include <dev/firewire/firewirereg.h>
74 #include <dev/firewire/fwmem.h>
75 #include <dev/firewire/iec13213.h>
76 #include <dev/firewire/iec68113.h>
78 #elif defined(__NetBSD__)
79 #include <sys/param.h>
80 #include <sys/device.h>
81 #include <sys/errno.h>
83 #include <sys/kernel.h>
84 #include <sys/kthread.h>
85 #include <sys/malloc.h>
86 #include <sys/queue.h>
87 #include <sys/sysctl.h>
88 #include <sys/systm.h>
92 #include <dev/ieee1394/fw_port.h>
93 #include <dev/ieee1394/firewire.h>
94 #include <dev/ieee1394/firewirereg.h>
95 #include <dev/ieee1394/fwmem.h>
96 #include <dev/ieee1394/iec13213.h>
97 #include <dev/ieee1394/iec68113.h>
102 struct crom_src_buf
{
104 struct crom_chunk root
;
105 struct crom_chunk vendor
;
106 struct crom_chunk hw
;
109 int firewire_debug
=0, try_bmr
=1, hold_count
=3;
110 #if defined(__FreeBSD__)
111 SYSCTL_INT(_debug
, OID_AUTO
, firewire_debug
, CTLFLAG_RW
, &firewire_debug
, 0,
112 "FireWire driver debug flag");
113 SYSCTL_NODE(_hw
, OID_AUTO
, firewire
, CTLFLAG_RD
, 0, "FireWire Subsystem");
114 SYSCTL_INT(_hw_firewire
, OID_AUTO
, try_bmr
, CTLFLAG_RW
, &try_bmr
, 0,
115 "Try to be a bus manager");
116 SYSCTL_INT(_hw_firewire
, OID_AUTO
, hold_count
, CTLFLAG_RW
, &hold_count
, 0,
117 "Number of count of bus resets for removing lost device information");
119 MALLOC_DEFINE(M_FW
, "firewire", "FireWire");
120 MALLOC_DEFINE(M_FWXFER
, "fw_xfer", "XFER/FireWire");
121 #elif defined(__NetBSD__)
123 * Setup sysctl(3) MIB, hw.ieee1394if.*
125 * TBD condition CTLFLAG_PERMANENT on being a module or not
127 SYSCTL_SETUP(sysctl_ieee1394if
, "sysctl ieee1394if(4) subtree setup")
129 int rc
, ieee1394if_node_num
;
130 const struct sysctlnode
*node
;
132 if ((rc
= sysctl_createv(clog
, 0, NULL
, NULL
,
133 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "hw", NULL
,
134 NULL
, 0, NULL
, 0, CTL_HW
, CTL_EOL
)) != 0) {
138 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
139 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "ieee1394if",
140 SYSCTL_DESCR("ieee1394if controls"),
141 NULL
, 0, NULL
, 0, CTL_HW
, CTL_CREATE
, CTL_EOL
)) != 0) {
144 ieee1394if_node_num
= node
->sysctl_num
;
146 /* ieee1394if try bus manager flag */
147 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
148 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
, CTLTYPE_INT
,
149 "try_bmr", SYSCTL_DESCR("Try to be a bus manager"),
151 0, CTL_HW
, ieee1394if_node_num
, CTL_CREATE
, CTL_EOL
)) != 0) {
155 /* ieee1394if hold count */
156 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
157 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
, CTLTYPE_INT
,
158 "hold_count", SYSCTL_DESCR("Number of count of "
159 "bus resets for removing lost device information"),
160 NULL
, 0, &hold_count
,
161 0, CTL_HW
, ieee1394if_node_num
, CTL_CREATE
, CTL_EOL
)) != 0) {
165 /* ieee1394if driver debug flag */
166 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
167 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
, CTLTYPE_INT
,
168 "ieee1394_debug", SYSCTL_DESCR("ieee1394if driver debug flag"),
169 NULL
, 0, &firewire_debug
,
170 0, CTL_HW
, ieee1394if_node_num
, CTL_CREATE
, CTL_EOL
)) != 0) {
177 printf("%s: sysctl_createv failed (rc = %d)\n", __func__
, rc
);
180 MALLOC_DEFINE(M_FW
, "ieee1394", "IEEE1394");
181 MALLOC_DEFINE(M_FWXFER
, "fw_xfer", "XFER/IEEE1394");
184 #define FW_MAXASYRTY 4
186 #if defined(__FreeBSD__)
187 devclass_t firewire_devclass
;
189 static void firewire_identify (driver_t
*, device_t
);
190 static int firewire_probe (device_t
);
191 static int firewire_attach (device_t
);
192 static int firewire_detach (device_t
);
193 static int firewire_resume (device_t
);
195 static int firewire_shutdown (device_t
);
197 static device_t
firewire_add_child (device_t
, int, const char *, int);
198 #elif defined(__NetBSD__)
199 int firewirematch (device_t
, cfdata_t
, void *);
200 void firewireattach (device_t
, device_t
, void *);
201 int firewiredetach (device_t
, int);
202 int firewire_print (void *, const char *);
203 int firewire_resume (struct firewire_comm
*);
205 static void firewire_xfer_timeout(void *, int);
206 static void fw_try_bmr (void *);
207 static void fw_try_bmr_callback (struct fw_xfer
*);
208 static void fw_asystart (struct fw_xfer
*);
209 static int fw_get_tlabel (struct firewire_comm
*, struct fw_xfer
*);
210 static void fw_bus_probe (struct firewire_comm
*);
211 static void fw_attach_dev (struct firewire_comm
*);
212 static void fw_bus_probe_thread(void *);
214 static void fw_vmaccess (struct fw_xfer
*);
216 static int fw_bmr (struct firewire_comm
*);
217 static void fw_dump_hdr(struct fw_pkt
*, const char *);
219 #if defined(__FreeBSD__)
220 static device_method_t firewire_methods
[] = {
221 /* Device interface */
222 DEVMETHOD(device_identify
, firewire_identify
),
223 DEVMETHOD(device_probe
, firewire_probe
),
224 DEVMETHOD(device_attach
, firewire_attach
),
225 DEVMETHOD(device_detach
, firewire_detach
),
226 DEVMETHOD(device_suspend
, bus_generic_suspend
),
227 DEVMETHOD(device_resume
, firewire_resume
),
228 DEVMETHOD(device_shutdown
, bus_generic_shutdown
),
231 DEVMETHOD(bus_add_child
, firewire_add_child
),
232 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
236 #elif defined(__NetBSD__)
237 CFATTACH_DECL_NEW(ieee1394if
, sizeof(struct firewire_softc
),
238 firewirematch
, firewireattach
, firewiredetach
, NULL
);
240 const char *fw_linkspeed
[] = {
241 "S100", "S200", "S400", "S800",
242 "S1600", "S3200", "undef", "undef"
245 static const char *tcode_str
[] = {
246 "WREQQ", "WREQB", "WRES", "undef",
247 "RREQQ", "RREQB", "RRESQ", "RRESB",
248 "CYCS", "LREQ", "STREAM", "LRES",
249 "undef", "undef", "PHY", "undef"
252 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
253 #define MAX_GAPHOP 15
254 u_int gap_cnt
[] = { 5, 5, 7, 8, 10, 13, 16, 18,
255 21, 24, 26, 29, 32, 35, 37, 40};
257 #if defined(__FreeBSD__)
258 static driver_t firewire_driver
= {
261 sizeof(struct firewire_softc
),
266 * Lookup fwdev by node id.
269 fw_noderesolve_nodeid(struct firewire_comm
*fc
, int dst
)
271 struct fw_device
*fwdev
;
276 STAILQ_FOREACH(fwdev
, &fc
->devices
, link
)
277 if (fwdev
->dst
== dst
&& fwdev
->status
!= FWDEVINVAL
)
286 * Lookup fwdev by EUI64.
289 fw_noderesolve_eui64(struct firewire_comm
*fc
, struct fw_eui64
*eui
)
291 struct fw_device
*fwdev
;
295 STAILQ_FOREACH(fwdev
, &fc
->devices
, link
)
296 if (FW_EUI64_EQUAL(fwdev
->eui
, *eui
))
300 if(fwdev
== NULL
) return NULL
;
301 if(fwdev
->status
== FWDEVINVAL
) return NULL
;
306 * Async. request procedure for userland application.
309 fw_asyreq(struct firewire_comm
*fc
, int sub
, struct fw_xfer
*xfer
)
312 struct fw_xferq
*xferq
;
316 const struct tcode_info
*info
;
318 if(xfer
== NULL
) return EINVAL
;
319 if(xfer
->hand
== NULL
){
320 printf("hand == NULL\n");
323 fp
= &xfer
->send
.hdr
;
325 tcode
= fp
->mode
.common
.tcode
& 0xf;
326 info
= &fc
->tcode
[tcode
];
327 if (info
->flag
== 0) {
328 printf("invalid tcode=%x\n", tcode
);
332 /* XXX allow bus explore packets only after bus rest */
333 if ((fc
->status
< FWBUSEXPLORE
) &&
334 ((tcode
!= FWTCODE_RREQQ
) || (fp
->mode
.rreqq
.dest_hi
!= 0xffff) ||
335 (fp
->mode
.rreqq
.dest_lo
< 0xf0000000) ||
336 (fp
->mode
.rreqq
.dest_lo
>= 0xf0001000))) {
338 xfer
->flag
= FWXF_BUSY
;
342 if (info
->flag
& FWTI_REQ
)
347 if (xfer
->send
.pay_len
> MAXREC(fc
->maxrec
)) {
348 printf("send.pay_len > maxrec\n");
351 if (info
->flag
& FWTI_BLOCK_STR
)
352 len
= fp
->mode
.stream
.len
;
353 else if (info
->flag
& FWTI_BLOCK_ASY
)
354 len
= fp
->mode
.rresb
.len
;
357 if (len
!= xfer
->send
.pay_len
){
358 printf("len(%d) != send.pay_len(%d) %s(%x)\n",
359 len
, xfer
->send
.pay_len
, tcode_str
[tcode
], tcode
);
363 if(xferq
->start
== NULL
){
364 printf("xferq->start == NULL\n");
367 if(!(xferq
->queued
< xferq
->maxq
)){
368 fw_printf(fc
->bdev
, "Discard a packet (queued=%d)\n",
374 if (info
->flag
& FWTI_TLABEL
) {
375 if (fw_get_tlabel(fc
, xfer
) < 0)
387 * Wakeup blocked process.
390 fw_xferwake(struct fw_xfer
*xfer
)
392 fw_mtx_t lock
= &xfer
->fc
->wait_lock
;
395 xfer
->flag
|= FWXF_WAKE
;
403 fw_xferwait(struct fw_xfer
*xfer
)
405 fw_mtx_t lock
= &xfer
->fc
->wait_lock
;
409 if ((xfer
->flag
& FWXF_WAKE
) == 0)
410 err
= fw_msleep((void *)xfer
, lock
, PWAIT
|PCATCH
, "fw_xferwait", 0);
417 * Async. request with given xfer structure.
420 fw_asystart(struct fw_xfer
*xfer
)
422 struct firewire_comm
*fc
= xfer
->fc
;
425 /* Protect from interrupt/timeout */
427 xfer
->flag
= FWXF_INQ
;
428 STAILQ_INSERT_TAIL(&xfer
->q
->q
, xfer
, link
);
434 /* XXX just queue for mbuf */
435 if (xfer
->mbuf
== NULL
)
440 #if defined(__FreeBSD__)
442 firewire_identify(driver_t
*driver
, device_t parent
)
444 BUS_ADD_CHILD(parent
, 0, "firewire", -1);
448 firewire_probe(device_t dev
)
450 device_set_desc(dev
, "IEEE1394(FireWire) bus");
453 #elif defined(__NetBSD__)
455 firewirematch(device_t parent
, cfdata_t cf
, void *aux
)
457 struct fwbus_attach_args
*faa
= (struct fwbus_attach_args
*)aux
;
459 if (strcmp(faa
->name
, "ieee1394if") == 0)
466 firewire_xfer_timeout(void *arg
, int pending
)
468 struct firewire_comm
*fc
= (struct firewire_comm
*)arg
;
469 struct fw_xfer
*xfer
, *txfer
;
471 struct timeval split_timeout
;
472 STAILQ_HEAD(, fw_xfer
) xfer_timeout
;
475 split_timeout
.tv_sec
= 0;
476 split_timeout
.tv_usec
= 200 * 1000; /* 200 msec */
479 fw_timevalsub(&tv
, &split_timeout
);
480 STAILQ_INIT(&xfer_timeout
);
483 fw_mtx_lock(&fc
->tlabel_lock
);
484 for (i
= 0; i
< 0x40; i
++) {
485 while ((xfer
= STAILQ_FIRST(&fc
->tlabels
[i
])) != NULL
) {
486 if ((xfer
->flag
& FWXF_SENT
) == 0)
489 if (fw_timevalcmp(&xfer
->tv
, &tv
, >))
490 /* the rests are newer than this */
493 "split transaction timeout: "
494 "tl=0x%x flag=0x%02x\n", i
, xfer
->flag
);
495 fw_dump_hdr(&xfer
->send
.hdr
, "send");
496 xfer
->resp
= ETIMEDOUT
;
497 STAILQ_REMOVE_HEAD(&fc
->tlabels
[i
], tlabel
);
498 STAILQ_INSERT_TAIL(&xfer_timeout
, xfer
, tlabel
);
501 fw_mtx_unlock(&fc
->tlabel_lock
);
505 STAILQ_FOREACH_SAFE(xfer
, &xfer_timeout
, tlabel
, txfer
)
509 #define WATCHDOG_HZ 10
511 firewire_watchdog(void *arg
)
513 struct firewire_comm
*fc
;
514 static int watchdog_clock
= 0;
516 fc
= (struct firewire_comm
*)arg
;
519 * At boot stage, the device interrupt is disabled and
520 * We encounter a timeout easily. To avoid this,
521 * ignore clock interrupt for a while.
523 if (watchdog_clock
> WATCHDOG_HZ
* 15)
524 fw_taskqueue_enqueue(fc
->taskqueue
, &fc
->task_timeout
);
528 fw_callout_reset(&fc
->timeout_callout
, hz
/ WATCHDOG_HZ
,
529 (void *)firewire_watchdog
, (void *)fc
);
533 * The attach routine.
537 FW_ATTACH_START(firewire
, sc
, fwa
);
538 FIREWIRE_ATTACH_START
;
541 fc
->status
= FWBUSNOTREADY
;
543 if( fc
->nisodma
> FWMAXNDMA
) fc
->nisodma
= FWMAXNDMA
;
547 fw_mtx_init(&fc
->wait_lock
, "fwwait", NULL
, MTX_DEF
);
548 fw_mtx_init(&fc
->tlabel_lock
, "fwtlabel", NULL
, MTX_DEF
);
549 fw_callout_init(&fc
->timeout_callout
);
550 fw_callout_init(&fc
->bmr_callout
);
551 fw_callout_init(&fc
->busprobe_callout
);
552 FW_TASK_INIT(&fc
->task_timeout
, 0, firewire_xfer_timeout
, (void *)fc
);
554 fw_callout_reset(&sc
->fc
->timeout_callout
, hz
,
555 (void *)firewire_watchdog
, (void *)sc
->fc
);
558 fw_kthread_create(fw_bus_probe_thread
, (void *)fc
, &fc
->probe_thread
,
559 "fw%d_probe", fw_get_unit(fc
->bdev
));
560 fw_config_pending_incr();
562 FIREWIRE_GENERIC_ATTACH
;
565 fw_busreset(fc
, FWBUSNOTREADY
);
569 if (!pmf_device_register(self
, NULL
, NULL
))
570 aprint_error_dev(self
, "couldn't establish power handler\n");
576 #if defined(__FreeBSD__)
578 * Attach it as child.
581 firewire_add_child(device_t dev
, int order
, const char *name
, int unit
)
584 struct firewire_softc
*sc
;
585 struct fw_attach_args fwa
;
587 sc
= (struct firewire_softc
*)device_get_softc(dev
);
588 child
= device_add_child(dev
, name
, unit
);
593 device_set_ivars(child
, &fwa
);
594 device_probe_and_attach(child
);
601 firewire_resume(device_t dev
)
603 struct firewire_softc
*sc
;
605 sc
= (struct firewire_softc
*)device_get_softc(dev
);
606 sc
->fc
->status
= FWBUSNOTREADY
;
608 bus_generic_resume(dev
);
619 FW_DETACH_START(firewire
, sc
);
620 struct firewire_comm
*fc
;
621 struct fw_device
*fwdev
, *fwdev_next
;
624 fw_mtx_lock(&fc
->wait_lock
);
625 fc
->status
= FWBUSDETACH
;
627 if (fw_msleep(fc
->probe_thread
,
628 &fc
->wait_lock
, PWAIT
, "fwthr", hz
* 60))
629 printf("firewire probe thread didn't die\n");
630 fw_mtx_unlock(&fc
->wait_lock
);
632 FWDEV_DESTROYDEV(sc
);
633 FIREWIRE_GENERIC_DETACH
;
635 fw_callout_stop(&fc
->timeout_callout
);
636 fw_callout_stop(&fc
->bmr_callout
);
637 fw_callout_stop(&fc
->busprobe_callout
);
639 /* XXX xfree_free and untimeout on all xfers */
640 for (fwdev
= STAILQ_FIRST(&fc
->devices
); fwdev
!= NULL
;
641 fwdev
= fwdev_next
) {
642 fwdev_next
= STAILQ_NEXT(fwdev
, link
);
645 free(fc
->topology_map
, M_FW
);
646 free(fc
->speed_map
, M_FW
);
647 free(fc
->crom_src_buf
, M_FW
);
649 fw_mtx_destroy(&fc
->wait_lock
);
650 fw_mtx_destroy(&fc
->tlabel_lock
);
653 #if defined(__FreeBSD__)
656 firewire_shutdown( device_t dev
)
661 #elif defined(__NetBSD__)
663 firewire_print(void *aux
, const char *pnp
)
665 struct fw_attach_args
*fwa
= (struct fw_attach_args
*)aux
;
668 aprint_normal("%s at %s", fwa
->name
, pnp
);
674 firewire_resume(struct firewire_comm
*fc
)
677 fc
->status
= FWBUSNOTREADY
;
683 fw_xferq_drain(struct fw_xferq
*xferq
)
685 struct fw_xfer
*xfer
;
687 while ((xfer
= STAILQ_FIRST(&xferq
->q
)) != NULL
) {
688 STAILQ_REMOVE_HEAD(&xferq
->q
, link
);
693 xfer
->flag
= FWXF_SENTERR
;
699 fw_drain_txq(struct firewire_comm
*fc
)
701 struct fw_xfer
*xfer
, *txfer
;
702 STAILQ_HEAD(, fw_xfer
) xfer_drain
;
705 STAILQ_INIT(&xfer_drain
);
708 fw_xferq_drain(fc
->atq
);
709 fw_xferq_drain(fc
->ats
);
710 for(i
= 0; i
< fc
->nisodma
; i
++)
711 fw_xferq_drain(fc
->it
[i
]);
714 fw_mtx_lock(&fc
->tlabel_lock
);
715 for (i
= 0; i
< 0x40; i
++)
716 while ((xfer
= STAILQ_FIRST(&fc
->tlabels
[i
])) != NULL
) {
718 printf("tl=%d flag=%d\n", i
, xfer
->flag
);
720 STAILQ_REMOVE_HEAD(&fc
->tlabels
[i
], tlabel
);
721 STAILQ_INSERT_TAIL(&xfer_drain
, xfer
, tlabel
);
723 fw_mtx_unlock(&fc
->tlabel_lock
);
725 STAILQ_FOREACH_SAFE(xfer
, &xfer_drain
, tlabel
, txfer
)
730 fw_reset_csr(struct firewire_comm
*fc
)
734 CSRARC(fc
, STATE_CLEAR
)
735 = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
736 CSRARC(fc
, STATE_SET
) = CSRARC(fc
, STATE_CLEAR
);
737 CSRARC(fc
, NODE_IDS
) = 0x3f;
739 CSRARC(fc
, TOPO_MAP
+ 8) = 0;
744 for(i
= 2; i
< 0x100/4 - 2 ; i
++){
745 CSRARC(fc
, SPED_MAP
+ i
* 4) = 0;
747 CSRARC(fc
, STATE_CLEAR
) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
748 CSRARC(fc
, STATE_SET
) = CSRARC(fc
, STATE_CLEAR
);
749 CSRARC(fc
, RESET_START
) = 0;
750 CSRARC(fc
, SPLIT_TIMEOUT_HI
) = 0;
751 CSRARC(fc
, SPLIT_TIMEOUT_LO
) = 800 << 19;
752 CSRARC(fc
, CYCLE_TIME
) = 0x0;
753 CSRARC(fc
, BUS_TIME
) = 0x0;
754 CSRARC(fc
, BUS_MGR_ID
) = 0x3f;
755 CSRARC(fc
, BANDWIDTH_AV
) = 4915;
756 CSRARC(fc
, CHANNELS_AV_HI
) = 0xffffffff;
757 CSRARC(fc
, CHANNELS_AV_LO
) = 0xffffffff;
758 CSRARC(fc
, IP_CHANNELS
) = (1 << 31);
760 CSRARC(fc
, CONF_ROM
) = 0x04 << 24;
761 CSRARC(fc
, CONF_ROM
+ 4) = 0x31333934; /* means strings 1394 */
762 CSRARC(fc
, CONF_ROM
+ 8) = 1 << 31 | 1 << 30 | 1 << 29 |
763 1 << 28 | 0xff << 16 | 0x09 << 8;
764 CSRARC(fc
, CONF_ROM
+ 0xc) = 0;
766 /* DV depend CSRs see blue book */
767 CSRARC(fc
, oPCR
) &= ~DV_BROADCAST_ON
;
768 CSRARC(fc
, iPCR
) &= ~DV_BROADCAST_ON
;
770 CSRARC(fc
, STATE_CLEAR
) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
771 CSRARC(fc
, STATE_SET
) = CSRARC(fc
, STATE_CLEAR
);
775 fw_init_crom(struct firewire_comm
*fc
)
777 struct crom_src
*src
;
779 fc
->crom_src_buf
= (struct crom_src_buf
*)
780 malloc(sizeof(struct crom_src_buf
), M_FW
, M_WAITOK
| M_ZERO
);
781 if (fc
->crom_src_buf
== NULL
)
784 src
= &fc
->crom_src_buf
->src
;
785 memset(src
, 0, sizeof(struct crom_src
));
787 /* BUS info sample */
788 src
->hdr
.info_len
= 4;
790 src
->businfo
.bus_name
= CSR_BUS_NAME_IEEE1394
;
792 src
->businfo
.irmc
= 1;
793 src
->businfo
.cmc
= 1;
794 src
->businfo
.isc
= 1;
795 src
->businfo
.bmc
= 1;
796 src
->businfo
.pmc
= 0;
797 src
->businfo
.cyc_clk_acc
= 100;
798 src
->businfo
.max_rec
= fc
->maxrec
;
799 src
->businfo
.max_rom
= MAXROM_4
;
800 src
->businfo
.generation
= 1;
801 src
->businfo
.link_spd
= fc
->speed
;
803 src
->businfo
.eui64
.hi
= fc
->eui
.hi
;
804 src
->businfo
.eui64
.lo
= fc
->eui
.lo
;
806 STAILQ_INIT(&src
->chunk_list
);
809 fc
->crom_root
= &fc
->crom_src_buf
->root
;
813 fw_reset_crom(struct firewire_comm
*fc
)
815 struct crom_src_buf
*buf
;
816 struct crom_src
*src
;
817 struct crom_chunk
*root
;
819 if (fc
->crom_src_buf
== NULL
)
822 buf
= fc
->crom_src_buf
;
824 root
= fc
->crom_root
;
826 STAILQ_INIT(&src
->chunk_list
);
828 memset(root
, 0, sizeof(struct crom_chunk
));
829 crom_add_chunk(src
, NULL
, root
, 0);
830 crom_add_entry(root
, CSRKEY_NCAP
, 0x0083c0); /* XXX */
831 /* private company_id */
832 crom_add_entry(root
, CSRKEY_VENDOR
, CSRVAL_VENDOR_PRIVATE
);
833 crom_add_simple_text(src
, root
, &buf
->vendor
, PROJECT_STR
);
834 crom_add_entry(root
, CSRKEY_HW
, OS_VER
);
835 crom_add_simple_text(src
, root
, &buf
->hw
, hostname
);
839 * Called after bus reset.
842 fw_busreset(struct firewire_comm
*fc
, uint32_t new_status
)
844 struct firewire_dev_comm
*fdc
;
845 struct crom_src
*src
;
850 fw_callout_stop(&fc
->bmr_callout
);
855 fc
->status
= new_status
;
859 FIREWIRE_CHILDREN_FOREACH_FUNC(post_busreset
, fdc
);
861 newrom
= malloc(CROMSIZE
, M_FW
, M_NOWAIT
| M_ZERO
);
862 src
= &fc
->crom_src_buf
->src
;
863 crom_load(src
, (uint32_t *)newrom
, CROMSIZE
);
864 if (memcmp(newrom
, fc
->config_rom
, CROMSIZE
) != 0) {
865 /* bump generation and reload */
866 src
->businfo
.generation
++;
867 /* generation must be between 0x2 and 0xF */
868 if (src
->businfo
.generation
< 2)
869 src
->businfo
.generation
++;
870 crom_load(src
, (uint32_t *)newrom
, CROMSIZE
);
871 bcopy(newrom
, (void *)fc
->config_rom
, CROMSIZE
);
876 /* Call once after reboot */
877 void fw_init(struct firewire_comm
*fc
)
881 struct fw_xfer
*xfer
;
900 STAILQ_INIT(&fc
->atq
->q
);
901 STAILQ_INIT(&fc
->ats
->q
);
903 for( i
= 0 ; i
< fc
->nisodma
; i
++ ){
904 fc
->it
[i
]->queued
= 0;
905 fc
->ir
[i
]->queued
= 0;
907 fc
->it
[i
]->start
= NULL
;
908 fc
->ir
[i
]->start
= NULL
;
910 fc
->it
[i
]->buf
= NULL
;
911 fc
->ir
[i
]->buf
= NULL
;
913 fc
->it
[i
]->flag
= FWXFERQ_STREAM
;
914 fc
->ir
[i
]->flag
= FWXFERQ_STREAM
;
916 STAILQ_INIT(&fc
->it
[i
]->q
);
917 STAILQ_INIT(&fc
->ir
[i
]->q
);
920 fc
->arq
->maxq
= FWMAXQUEUE
;
921 fc
->ars
->maxq
= FWMAXQUEUE
;
922 fc
->atq
->maxq
= FWMAXQUEUE
;
923 fc
->ats
->maxq
= FWMAXQUEUE
;
925 for( i
= 0 ; i
< fc
->nisodma
; i
++){
926 fc
->ir
[i
]->maxq
= FWMAXQUEUE
;
927 fc
->it
[i
]->maxq
= FWMAXQUEUE
;
929 /* Initialize csr registers */
930 fc
->topology_map
= (struct fw_topology_map
*)malloc(
931 sizeof(struct fw_topology_map
),
932 M_FW
, M_NOWAIT
| M_ZERO
);
933 fc
->speed_map
= (struct fw_speed_map
*)malloc(
934 sizeof(struct fw_speed_map
),
935 M_FW
, M_NOWAIT
| M_ZERO
);
936 CSRARC(fc
, TOPO_MAP
) = 0x3f1 << 16;
937 CSRARC(fc
, TOPO_MAP
+ 4) = 1;
938 CSRARC(fc
, SPED_MAP
) = 0x3f1 << 16;
939 CSRARC(fc
, SPED_MAP
+ 4) = 1;
941 STAILQ_INIT(&fc
->devices
);
943 /* Initialize Async handlers */
944 STAILQ_INIT(&fc
->binds
);
945 for( i
= 0 ; i
< 0x40 ; i
++){
946 STAILQ_INIT(&fc
->tlabels
[i
]);
949 /* DV depend CSRs see blue book */
951 CSRARC(fc
, oMPR
) = 0x3fff0001; /* # output channel = 1 */
952 CSRARC(fc
, oPCR
) = 0x8000007a;
953 for(i
= 4 ; i
< 0x7c/4 ; i
+=4){
954 CSRARC(fc
, i
+ oPCR
) = 0x8000007a;
957 CSRARC(fc
, iMPR
) = 0x00ff0001; /* # input channel = 1 */
958 CSRARC(fc
, iPCR
) = 0x803f0000;
959 for(i
= 4 ; i
< 0x7c/4 ; i
+=4){
960 CSRARC(fc
, i
+ iPCR
) = 0x0;
964 fc
->crom_src_buf
= NULL
;
967 xfer
= fw_xfer_alloc();
968 if(xfer
== NULL
) return;
970 fwb
= (struct fw_bind
*)malloc(sizeof (struct fw_bind
), M_FW
, M_NOWAIT
);
975 xfer
->hand
= fw_vmaccess
;
981 fwb
->addrlen
= 0xffffffff;
987 #define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
988 ((fwb)->end < (addr))?1:0)
991 * To lookup bound process from IEEE1394 address.
994 fw_bindlookup(struct firewire_comm
*fc
, uint16_t dest_hi
, uint32_t dest_lo
)
997 struct fw_bind
*tfw
, *r
= NULL
;
999 addr
= ((u_int64_t
)dest_hi
<< 32) | dest_lo
;
1001 STAILQ_FOREACH(tfw
, &fc
->binds
, fclist
)
1002 if (BIND_CMP(addr
, tfw
) == 0) {
1011 * To bind IEEE1394 address block to process.
1014 fw_bindadd(struct firewire_comm
*fc
, struct fw_bind
*fwb
)
1016 struct fw_bind
*tfw
, *prev
= NULL
;
1019 if (fwb
->start
> fwb
->end
) {
1020 printf("%s: invalid range\n", __func__
);
1025 STAILQ_FOREACH(tfw
, &fc
->binds
, fclist
) {
1026 if (fwb
->end
< tfw
->start
)
1031 STAILQ_INSERT_HEAD(&fc
->binds
, fwb
, fclist
);
1032 else if (prev
->end
< fwb
->start
)
1033 STAILQ_INSERT_AFTER(&fc
->binds
, prev
, fwb
, fclist
);
1035 printf("%s: bind failed\n", __func__
);
1043 * To free IEEE1394 address block.
1046 fw_bindremove(struct firewire_comm
*fc
, struct fw_bind
*fwb
)
1049 struct fw_xfer
*xfer
, *next
;
1051 struct fw_bind
*tfw
;
1056 STAILQ_FOREACH(tfw
, &fc
->binds
, fclist
)
1058 STAILQ_REMOVE(&fc
->binds
, fwb
, fw_bind
, fclist
);
1062 printf("%s: no such binding\n", __func__
);
1068 /* shall we do this? */
1069 for (xfer
= STAILQ_FIRST(&fwb
->xferlist
); xfer
!= NULL
; xfer
= next
) {
1070 next
= STAILQ_NEXT(xfer
, link
);
1073 STAILQ_INIT(&fwb
->xferlist
);
1082 fw_xferlist_add(struct fw_xferlist
*q
, struct malloc_type
*type
,
1083 int slen
, int rlen
, int n
,
1084 struct firewire_comm
*fc
, void *sc
, void (*hand
)(struct fw_xfer
*))
1087 struct fw_xfer
*xfer
;
1089 for (i
= 0; i
< n
; i
++) {
1090 xfer
= fw_xfer_alloc_buf(type
, slen
, rlen
);
1097 STAILQ_INSERT_TAIL(q
, xfer
, link
);
1104 fw_xferlist_remove(struct fw_xferlist
*q
)
1106 struct fw_xfer
*xfer
, *next
;
1108 for (xfer
= STAILQ_FIRST(q
); xfer
!= NULL
; xfer
= next
) {
1109 next
= STAILQ_NEXT(xfer
, link
);
1110 fw_xfer_free_buf(xfer
);
1116 * dump packet header
1119 fw_dump_hdr(struct fw_pkt
*fp
, const char *prefix
)
1121 printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
1122 "src=0x%03x\n", prefix
,
1123 fp
->mode
.hdr
.dst
& 0x3f,
1124 fp
->mode
.hdr
.tlrt
>> 2, fp
->mode
.hdr
.tlrt
& 3,
1125 fp
->mode
.hdr
.tcode
, fp
->mode
.hdr
.pri
,
1130 * To free transaction label.
1133 fw_tl_free(struct firewire_comm
*fc
, struct fw_xfer
*xfer
)
1135 struct fw_xfer
*txfer
;
1142 fw_mtx_lock(&fc
->tlabel_lock
);
1143 #if 1 /* make sure the label is allocated */
1144 STAILQ_FOREACH(txfer
, &fc
->tlabels
[xfer
->tl
], tlabel
)
1147 if (txfer
== NULL
) {
1148 printf("%s: the xfer is not in the queue "
1149 "(tlabel=%d, flag=0x%x)\n",
1150 __func__
, xfer
->tl
, xfer
->flag
);
1151 fw_dump_hdr(&xfer
->send
.hdr
, "send");
1152 fw_dump_hdr(&xfer
->recv
.hdr
, "recv");
1154 fw_mtx_unlock(&fc
->tlabel_lock
);
1160 STAILQ_REMOVE(&fc
->tlabels
[xfer
->tl
], xfer
, fw_xfer
, tlabel
);
1161 fw_mtx_unlock(&fc
->tlabel_lock
);
1167 * To obtain XFER structure by transaction label.
1169 static struct fw_xfer
*
1170 fw_tl2xfer(struct firewire_comm
*fc
, int node
, int tlabel
, int tcode
)
1172 struct fw_xfer
*xfer
;
1176 fw_mtx_lock(&fc
->tlabel_lock
);
1177 STAILQ_FOREACH(xfer
, &fc
->tlabels
[tlabel
], tlabel
)
1178 if(xfer
->send
.hdr
.mode
.hdr
.dst
== node
) {
1179 fw_mtx_unlock(&fc
->tlabel_lock
);
1181 FW_KASSERT(xfer
->tl
== tlabel
,
1182 ("xfer->tl 0x%x != 0x%x", xfer
->tl
, tlabel
));
1183 /* extra sanity check */
1184 req
= xfer
->send
.hdr
.mode
.hdr
.tcode
;
1185 if (xfer
->fc
->tcode
[req
].valid_res
!= tcode
) {
1186 printf("%s: invalid response tcode "
1187 "(0x%x for 0x%x)\n", __func__
,
1192 if (firewire_debug
> 2)
1193 printf("fw_tl2xfer: found tl=%d\n", tlabel
);
1196 fw_mtx_unlock(&fc
->tlabel_lock
);
1197 if (firewire_debug
> 1)
1198 printf("fw_tl2xfer: not found tl=%d\n", tlabel
);
1204 * To allocate IEEE1394 XFER structure.
1207 fw_xfer_alloc(struct malloc_type
*type
)
1209 struct fw_xfer
*xfer
;
1211 xfer
= malloc(sizeof(struct fw_xfer
), type
, M_NOWAIT
| M_ZERO
);
1215 xfer
->malloc
= type
;
1221 fw_xfer_alloc_buf(struct malloc_type
*type
, int send_len
, int recv_len
)
1223 struct fw_xfer
*xfer
;
1225 xfer
= fw_xfer_alloc(type
);
1228 xfer
->send
.pay_len
= send_len
;
1229 xfer
->recv
.pay_len
= recv_len
;
1231 xfer
->send
.payload
= malloc(send_len
, type
, M_NOWAIT
| M_ZERO
);
1232 if (xfer
->send
.payload
== NULL
) {
1238 xfer
->recv
.payload
= malloc(recv_len
, type
, M_NOWAIT
);
1239 if (xfer
->recv
.payload
== NULL
) {
1240 if (xfer
->send
.payload
!= NULL
)
1241 free(xfer
->send
.payload
, type
);
1250 * IEEE1394 XFER post process.
1253 fw_xfer_done(struct fw_xfer
*xfer
)
1255 if (xfer
->hand
== NULL
) {
1256 printf("hand == NULL\n");
1260 if (xfer
->fc
== NULL
)
1261 panic("fw_xfer_done: why xfer->fc is NULL?");
1263 fw_tl_free(xfer
->fc
, xfer
);
1268 fw_xfer_unload(struct fw_xfer
* xfer
)
1272 if(xfer
== NULL
) return;
1273 if(xfer
->flag
& FWXF_INQ
){
1274 printf("fw_xfer_free FWXF_INQ\n");
1277 STAILQ_REMOVE(&xfer
->q
->q
, xfer
, fw_xfer
, link
);
1281 FW_GUNLOCK(xfer
->fc
);
1284 if (xfer
->fc
!= NULL
) {
1286 if(xfer
->flag
== FWXF_START
)
1288 * This could happen if:
1289 * 1. We call fwohci_arcv() before fwohci_txd().
1290 * 2. firewire_watch() is called.
1292 printf("fw_xfer_free FWXF_START\n");
1295 xfer
->flag
= FWXF_INIT
;
1299 * To free IEEE1394 XFER structure.
1302 fw_xfer_free_buf( struct fw_xfer
* xfer
)
1305 printf("%s: xfer == NULL\n", __func__
);
1308 fw_xfer_unload(xfer
);
1309 if(xfer
->send
.payload
!= NULL
){
1310 free(xfer
->send
.payload
, xfer
->malloc
);
1312 if(xfer
->recv
.payload
!= NULL
){
1313 free(xfer
->recv
.payload
, xfer
->malloc
);
1315 free(xfer
, xfer
->malloc
);
1319 fw_xfer_free( struct fw_xfer
* xfer
)
1322 printf("%s: xfer == NULL\n", __func__
);
1325 fw_xfer_unload(xfer
);
1326 free(xfer
, xfer
->malloc
);
1330 fw_asy_callback_free(struct fw_xfer
*xfer
)
1333 printf("asyreq done flag=%d resp=%d\n",
1334 xfer
->flag
, xfer
->resp
);
1343 fw_phy_config(struct firewire_comm
*fc
, int root_node
, int gap_count
)
1345 struct fw_xfer
*xfer
;
1348 fc
->status
= FWBUSPHYCONF
;
1350 xfer
= fw_xfer_alloc(M_FWXFER
);
1354 xfer
->hand
= fw_asy_callback_free
;
1356 fp
= &xfer
->send
.hdr
;
1359 fp
->mode
.ld
[1] |= (root_node
& 0x3f) << 24 | 1 << 23;
1361 fp
->mode
.ld
[1] |= 1 << 22 | (gap_count
& 0x3f) << 16;
1362 fp
->mode
.ld
[2] = ~fp
->mode
.ld
[1];
1363 /* XXX Dangerous, how to pass PHY packet to device driver */
1364 fp
->mode
.common
.tcode
|= FWTCODE_PHY
;
1367 printf("send phy_config root_node=%d gap_count=%d\n",
1368 root_node
, gap_count
);
1369 fw_asyreq(fc
, -1, xfer
);
1377 fw_print_sid(uint32_t sid
)
1379 union fw_self_id
*s
;
1380 s
= (union fw_self_id
*) &sid
;
1381 printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1382 " p0:%d p1:%d p2:%d i:%d m:%d\n",
1383 s
->p0
.phy_id
, s
->p0
.link_active
, s
->p0
.gap_count
,
1384 s
->p0
.phy_speed
, s
->p0
.phy_delay
, s
->p0
.contender
,
1385 s
->p0
.power_class
, s
->p0
.port0
, s
->p0
.port1
,
1386 s
->p0
.port2
, s
->p0
.initiated_reset
, s
->p0
.more_packets
);
1391 * To receive self ID.
1393 void fw_sidrcv(struct firewire_comm
* fc
, uint32_t *sid
, u_int len
)
1396 union fw_self_id
*self_id
;
1397 u_int i
, j
, node
, c_port
= 0, i_branch
= 0;
1399 fc
->sid_cnt
= len
/(sizeof(uint32_t) * 2);
1400 fc
->max_node
= fc
->nodeid
& 0x3f;
1401 CSRARC(fc
, NODE_IDS
) = ((uint32_t)fc
->nodeid
) << 16;
1402 fc
->status
= FWBUSCYMELECT
;
1403 fc
->topology_map
->crc_len
= 2;
1404 fc
->topology_map
->generation
++;
1405 fc
->topology_map
->self_id_count
= 0;
1406 fc
->topology_map
->node_count
= 0;
1407 fc
->speed_map
->generation
++;
1408 fc
->speed_map
->crc_len
= 1 + (64*64 + 3) / 4;
1409 self_id
= &fc
->topology_map
->self_id
[0];
1410 for(i
= 0; i
< fc
->sid_cnt
; i
++){
1411 if (sid
[1] != ~sid
[0]) {
1412 printf("fw_sidrcv: invalid self-id packet\n");
1416 *self_id
= *((union fw_self_id
*)sid
);
1417 fc
->topology_map
->crc_len
++;
1418 if(self_id
->p0
.sequel
== 0){
1419 fc
->topology_map
->node_count
++;
1422 fw_print_sid(sid
[0]);
1424 node
= self_id
->p0
.phy_id
;
1425 if(fc
->max_node
< node
){
1426 fc
->max_node
= self_id
->p0
.phy_id
;
1428 /* XXX I'm not sure this is the right speed_map */
1429 fc
->speed_map
->speed
[node
][node
]
1430 = self_id
->p0
.phy_speed
;
1431 for (j
= 0; j
< node
; j
++) {
1432 fc
->speed_map
->speed
[j
][node
]
1433 = fc
->speed_map
->speed
[node
][j
]
1434 = min(fc
->speed_map
->speed
[j
][j
],
1435 self_id
->p0
.phy_speed
);
1437 if ((fc
->irm
== -1 || self_id
->p0
.phy_id
> fc
->irm
) &&
1438 (self_id
->p0
.link_active
&& self_id
->p0
.contender
)) {
1439 fc
->irm
= self_id
->p0
.phy_id
;
1441 if(self_id
->p0
.port0
>= 0x2){
1444 if(self_id
->p0
.port1
>= 0x2){
1447 if(self_id
->p0
.port2
>= 0x2){
1452 i_branch
+= (c_port
- 2);
1456 fc
->topology_map
->self_id_count
++;
1458 fw_printf(fc
->bdev
, "%d nodes", fc
->max_node
+ 1);
1460 fc
->topology_map
->crc
= fw_crc16(
1461 (uint32_t *)&fc
->topology_map
->generation
,
1462 fc
->topology_map
->crc_len
* 4);
1463 fc
->speed_map
->crc
= fw_crc16(
1464 (uint32_t *)&fc
->speed_map
->generation
,
1465 fc
->speed_map
->crc_len
* 4);
1466 /* byteswap and copy to CSR */
1467 p
= (uint32_t *)fc
->topology_map
;
1468 for (i
= 0; i
<= fc
->topology_map
->crc_len
; i
++)
1469 CSRARC(fc
, TOPO_MAP
+ i
* 4) = htonl(*p
++);
1470 p
= (uint32_t *)fc
->speed_map
;
1471 CSRARC(fc
, SPED_MAP
) = htonl(*p
++);
1472 CSRARC(fc
, SPED_MAP
+ 4) = htonl(*p
++);
1473 /* don't byte-swap uint8_t array */
1474 bcopy(p
, &CSRARC(fc
, SPED_MAP
+ 8), (fc
->speed_map
->crc_len
- 1)*4);
1476 fc
->max_hop
= fc
->max_node
- i_branch
;
1477 aprint_normal(", maxhop <= %d", fc
->max_hop
);
1480 aprint_normal(", Not found IRM capable node");
1482 aprint_normal(", cable IRM = %d", fc
->irm
);
1483 if (fc
->irm
== fc
->nodeid
)
1484 aprint_normal(" (me)");
1486 aprint_normal("\n");
1488 if (try_bmr
&& (fc
->irm
!= -1) && (CSRARC(fc
, BUS_MGR_ID
) == 0x3f)) {
1489 if (fc
->irm
== fc
->nodeid
) {
1490 fc
->status
= FWBUSMGRDONE
;
1491 CSRARC(fc
, BUS_MGR_ID
) = fc
->set_bmr(fc
, fc
->irm
);
1494 fc
->status
= FWBUSMGRELECT
;
1495 fw_callout_reset(&fc
->bmr_callout
, hz
/8,
1496 (void *)fw_try_bmr
, (void *)fc
);
1499 fc
->status
= FWBUSMGRDONE
;
1501 fw_callout_reset(&fc
->busprobe_callout
, hz
/4,
1502 (void *)fw_bus_probe
, (void *)fc
);
1506 * To probe devices on the IEEE1394 bus.
1509 fw_bus_probe(struct firewire_comm
*fc
)
1512 struct fw_device
*fwdev
;
1515 fc
->status
= FWBUSEXPLORE
;
1517 /* Invalidate all devices, just after bus reset. */
1518 STAILQ_FOREACH(fwdev
, &fc
->devices
, link
)
1519 if (fwdev
->status
!= FWDEVINVAL
) {
1520 fwdev
->status
= FWDEVINVAL
;
1529 fw_explore_read_quads(struct fw_device
*fwdev
, int offset
,
1530 uint32_t *quad
, int n
)
1532 struct fw_xfer
*xfer
;
1537 for (i
= 0; i
< n
; i
++, offset
+= sizeof(uint32_t)) {
1538 xfer
= fwmem_read_quad(fwdev
, NULL
, -1,
1539 0xffff, 0xf0000000 | offset
, (void *)&tmp
,
1545 if (xfer
->resp
== 0)
1546 quad
[i
] = ntohl(tmp
);
1558 fw_explore_csrblock(struct fw_device
*fwdev
, int offset
, int recur
)
1561 struct csrdirectory
*dir
;
1565 dir
= (struct csrdirectory
*)&fwdev
->csrrom
[offset
/sizeof(uint32_t)];
1566 err
= fw_explore_read_quads(fwdev
, CSRROMOFF
+ offset
,
1567 (uint32_t *)dir
, 1);
1571 offset
+= sizeof(uint32_t);
1572 reg
= (struct csrreg
*)&fwdev
->csrrom
[offset
/sizeof(uint32_t)];
1573 err
= fw_explore_read_quads(fwdev
, CSRROMOFF
+ offset
,
1574 (uint32_t *)reg
, dir
->crc_len
);
1580 off
= CSRROMOFF
+ offset
+ sizeof(uint32_t) * (dir
->crc_len
- 1);
1581 if (fwdev
->rommax
< off
)
1582 fwdev
->rommax
= off
;
1587 for (i
= 0; i
< dir
->crc_len
; i
++, offset
+= sizeof(uint32_t)) {
1588 if ((reg
[i
].key
& CSRTYPE_MASK
) == CSRTYPE_D
)
1590 else if ((reg
[i
].key
& CSRTYPE_MASK
) == CSRTYPE_L
)
1595 off
= offset
+ reg
[i
].val
* sizeof(uint32_t);
1596 if (off
> CROMSIZE
) {
1597 printf("%s: invalid offset %d\n", __func__
, off
);
1600 err
= fw_explore_csrblock(fwdev
, off
, recur
);
1608 fw_explore_node(struct fw_device
*dfwdev
)
1610 struct firewire_comm
*fc
;
1611 struct fw_device
*fwdev
, *pfwdev
, *tfwdev
;
1614 struct bus_info
*binfo
;
1618 csr
= dfwdev
->csrrom
;
1622 err
= fw_explore_read_quads(dfwdev
, CSRROMOFF
, &csr
[0], 1);
1625 hdr
= (struct csrhdr
*)&csr
[0];
1626 if (hdr
->info_len
!= 4) {
1628 printf("node%d: wrong bus info len(%d)\n",
1629 node
, hdr
->info_len
);
1634 err
= fw_explore_read_quads(dfwdev
, CSRROMOFF
+ 0x04, &csr
[1], 4);
1637 binfo
= (struct bus_info
*)&csr
[1];
1638 if (binfo
->bus_name
!= CSR_BUS_NAME_IEEE1394
) {
1640 printf("node%d: invalid bus name 0x%08x\n",
1641 node
, binfo
->bus_name
);
1644 spd
= fc
->speed_map
->speed
[fc
->nodeid
][node
];
1645 STAILQ_FOREACH(fwdev
, &fc
->devices
, link
)
1646 if (FW_EUI64_EQUAL(fwdev
->eui
, binfo
->eui64
))
1648 if (fwdev
== NULL
) {
1650 fwdev
= malloc(sizeof(struct fw_device
), M_FW
,
1652 if (fwdev
== NULL
) {
1654 printf("node%d: no memory\n", node
);
1658 fwdev
->eui
= binfo
->eui64
;
1659 fwdev
->status
= FWDEVNEW
;
1660 /* insert into sorted fwdev list */
1662 STAILQ_FOREACH(tfwdev
, &fc
->devices
, link
) {
1663 if (tfwdev
->eui
.hi
> fwdev
->eui
.hi
||
1664 (tfwdev
->eui
.hi
== fwdev
->eui
.hi
&&
1665 tfwdev
->eui
.lo
> fwdev
->eui
.lo
))
1670 STAILQ_INSERT_HEAD(&fc
->devices
, fwdev
, link
);
1672 STAILQ_INSERT_AFTER(&fc
->devices
, pfwdev
, fwdev
, link
);
1674 fw_printf(fc
->bdev
, "New %s device ID:%08x%08x\n",
1676 fwdev
->eui
.hi
, fwdev
->eui
.lo
);
1679 fwdev
->status
= FWDEVINIT
;
1684 if (memcmp(&csr
[0], &fwdev
->csrrom
[0], sizeof(uint32_t) * 5) == 0) {
1686 printf("node%d: crom unchanged\n", node
);
1690 memset(&fwdev
->csrrom
[0], 0, CROMSIZE
);
1692 /* copy first quad and bus info block */
1693 bcopy(&csr
[0], &fwdev
->csrrom
[0], sizeof(uint32_t) * 5);
1694 fwdev
->rommax
= CSRROMOFF
+ sizeof(uint32_t) * 4;
1696 err
= fw_explore_csrblock(fwdev
, 0x14, 1); /* root directory */
1699 fwdev
->status
= FWDEVINVAL
;
1700 fwdev
->csrrom
[0] = 0;
1707 * Find the self_id packet for a node, ignoring sequels.
1709 static union fw_self_id
*
1710 fw_find_self_id(struct firewire_comm
*fc
, int node
)
1713 union fw_self_id
*s
;
1715 for (i
= 0; i
< fc
->topology_map
->self_id_count
; i
++) {
1716 s
= &fc
->topology_map
->self_id
[i
];
1719 if (s
->p0
.phy_id
== node
)
1726 fw_explore(struct firewire_comm
*fc
)
1728 int node
, err
, s
, i
, todo
, todo2
, trys
;
1730 struct fw_device
*dfwdev
;
1731 union fw_self_id
*fwsid
;
1734 dfwdev
= malloc(sizeof(*dfwdev
), M_TEMP
, M_NOWAIT
);
1737 /* setup dummy fwdev */
1740 dfwdev
->maxrec
= 8; /* 512 */
1741 dfwdev
->status
= FWDEVINIT
;
1743 for (node
= 0; node
<= fc
->max_node
; node
++) {
1744 /* We don't probe myself and linkdown nodes */
1745 if (node
== fc
->nodeid
)
1747 fwsid
= fw_find_self_id(fc
, node
);
1748 if (!fwsid
|| !fwsid
->p0
.link_active
) {
1750 printf("node%d: link down\n", node
);
1753 nodes
[todo
++] = node
;
1757 for (trys
= 0; todo
> 0 && trys
< 3; trys
++) {
1759 for (i
= 0; i
< todo
; i
++) {
1760 dfwdev
->dst
= nodes
[i
];
1761 err
= fw_explore_node(dfwdev
);
1763 nodes
[todo2
++] = nodes
[i
];
1765 printf("%s: node %d, err = %d\n",
1766 __func__
, node
, err
);
1771 free(dfwdev
, M_TEMP
);
1775 fw_bus_probe_thread(void *arg
)
1777 struct firewire_comm
*fc
;
1779 fc
= (struct firewire_comm
*)arg
;
1781 fw_config_pending_decr();
1783 fw_mtx_lock(&fc
->wait_lock
);
1784 while (fc
->status
!= FWBUSDETACH
) {
1785 if (fc
->status
== FWBUSEXPLORE
) {
1786 fw_mtx_unlock(&fc
->wait_lock
);
1788 fc
->status
= FWBUSEXPDONE
;
1790 printf("bus_explore done\n");
1792 fw_mtx_lock(&fc
->wait_lock
);
1794 fw_msleep((void *)fc
, &fc
->wait_lock
, PWAIT
|PCATCH
, "-", 0);
1796 fw_mtx_unlock(&fc
->wait_lock
);
1802 * To attach sub-devices layer onto IEEE1394 bus.
1805 fw_attach_dev(struct firewire_comm
*fc
)
1807 struct fw_device
*fwdev
, *next
;
1808 struct firewire_dev_comm
*fdc
;
1809 struct fw_attach_args fwa
;
1814 for (fwdev
= STAILQ_FIRST(&fc
->devices
); fwdev
!= NULL
; fwdev
= next
) {
1815 next
= STAILQ_NEXT(fwdev
, link
);
1816 switch (fwdev
->status
) {
1818 FIREWIRE_SBP_ATTACH
;
1822 fwdev
->status
= FWDEVATTACHED
;
1835 FIREWIRE_CHILDREN_FOREACH_FUNC(post_explore
, fdc
);
1837 for (fwdev
= STAILQ_FIRST(&fc
->devices
); fwdev
!= NULL
; fwdev
= next
) {
1838 next
= STAILQ_NEXT(fwdev
, link
);
1839 if (fwdev
->rcnt
> 0 && fwdev
->rcnt
> hold_count
) {
1841 * Remove devices which have not been seen
1844 FIREWIRE_SBP_DETACH
;
1845 STAILQ_REMOVE(&fc
->devices
, fwdev
, fw_device
, link
);
1854 * To allocate unique transaction label.
1857 fw_get_tlabel(struct firewire_comm
*fc
, struct fw_xfer
*xfer
)
1859 u_int dst
, new_tlabel
;
1860 struct fw_xfer
*txfer
;
1863 dst
= xfer
->send
.hdr
.mode
.hdr
.dst
& 0x3f;
1865 fw_mtx_lock(&fc
->tlabel_lock
);
1866 new_tlabel
= (fc
->last_tlabel
[dst
] + 1) & 0x3f;
1867 STAILQ_FOREACH(txfer
, &fc
->tlabels
[new_tlabel
], tlabel
)
1868 if ((txfer
->send
.hdr
.mode
.hdr
.dst
& 0x3f) == dst
)
1871 fc
->last_tlabel
[dst
] = new_tlabel
;
1872 STAILQ_INSERT_TAIL(&fc
->tlabels
[new_tlabel
], xfer
, tlabel
);
1873 fw_mtx_unlock(&fc
->tlabel_lock
);
1875 xfer
->tl
= new_tlabel
;
1876 xfer
->send
.hdr
.mode
.hdr
.tlrt
= new_tlabel
<< 2;
1877 if (firewire_debug
> 1)
1878 printf("fw_get_tlabel: dst=%d tl=%d\n", dst
, new_tlabel
);
1879 return (new_tlabel
);
1881 fw_mtx_unlock(&fc
->tlabel_lock
);
1884 if (firewire_debug
> 1)
1885 printf("fw_get_tlabel: no free tlabel\n");
1890 fw_rcv_copy(struct fw_rcv_buf
*rb
)
1894 const struct tcode_info
*tinfo
;
1895 u_int res
, i
, len
, plen
;
1897 rb
->xfer
->recv
.spd
= rb
->spd
;
1899 pkt
= (struct fw_pkt
*)rb
->vec
->iov_base
;
1900 tinfo
= &rb
->fc
->tcode
[pkt
->mode
.hdr
.tcode
];
1903 p
= (u_char
*)&rb
->xfer
->recv
.hdr
;
1904 bcopy(rb
->vec
->iov_base
, p
, tinfo
->hdr_len
);
1905 rb
->vec
->iov_base
= (u_char
*)rb
->vec
->iov_base
+ tinfo
->hdr_len
;
1906 rb
->vec
->iov_len
-= tinfo
->hdr_len
;
1909 p
= (u_char
*)rb
->xfer
->recv
.payload
;
1910 res
= rb
->xfer
->recv
.pay_len
;
1912 /* special handling for RRESQ */
1913 if (pkt
->mode
.hdr
.tcode
== FWTCODE_RRESQ
&&
1914 p
!= NULL
&& res
>= sizeof(uint32_t)) {
1915 *(uint32_t *)p
= pkt
->mode
.rresq
.data
;
1916 rb
->xfer
->recv
.pay_len
= sizeof(uint32_t);
1920 if ((tinfo
->flag
& FWTI_BLOCK_ASY
) == 0)
1923 plen
= pkt
->mode
.rresb
.len
;
1925 for (i
= 0; i
< rb
->nvec
; i
++, rb
->vec
++) {
1926 len
= MIN(rb
->vec
->iov_len
, plen
);
1928 printf("rcv buffer(%d) is %d bytes short.\n",
1929 rb
->xfer
->recv
.pay_len
, len
- res
);
1933 bcopy(rb
->vec
->iov_base
, p
, len
);
1938 if (res
== 0 || plen
== 0)
1941 rb
->xfer
->recv
.pay_len
-= res
;
1946 * Generic packet receiving process.
1949 fw_rcv(struct fw_rcv_buf
*rb
)
1951 struct fw_pkt
*fp
, *resfp
;
1952 struct fw_bind
*bind
;
1954 int i
, len
, oldstate
;
1959 qld
= (uint32_t *)buf
;
1960 printf("spd %d len:%d\n", spd
, len
);
1961 for( i
= 0 ; i
<= len
&& i
< 32; i
+= 4){
1962 printf("0x%08x ", ntohl(qld
[i
/4]));
1963 if((i
% 16) == 15) printf("\n");
1965 if((i
% 16) != 15) printf("\n");
1968 fp
= (struct fw_pkt
*)rb
->vec
[0].iov_base
;
1969 tcode
= fp
->mode
.common
.tcode
;
1975 rb
->xfer
= fw_tl2xfer(rb
->fc
, fp
->mode
.hdr
.src
,
1976 fp
->mode
.hdr
.tlrt
>> 2, fp
->mode
.hdr
.tcode
);
1977 if(rb
->xfer
== NULL
) {
1978 printf("fw_rcv: unknown response "
1979 "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1980 tcode_str
[tcode
], tcode
,
1982 fp
->mode
.hdr
.tlrt
>> 2,
1983 fp
->mode
.hdr
.tlrt
& 3,
1984 fp
->mode
.rresq
.data
);
1986 printf("try ad-hoc work around!!\n");
1987 rb
->xfer
= fw_tl2xfer(rb
->fc
, fp
->mode
.hdr
.src
,
1988 (fp
->mode
.hdr
.tlrt
>> 2)^3);
1989 if (rb
->xfer
== NULL
) {
1990 printf("no use...\n");
1998 if (rb
->xfer
->recv
.hdr
.mode
.wres
.rtcode
!= RESP_CMP
)
1999 rb
->xfer
->resp
= EIO
;
2002 /* make sure the packet is drained in AT queue */
2003 oldstate
= rb
->xfer
->flag
;
2004 rb
->xfer
->flag
= FWXF_RCVD
;
2007 fw_xfer_done(rb
->xfer
);
2012 printf("not sent yet tl=%x\n", rb
->xfer
->tl
);
2016 printf("unexpected flag 0x%02x\n", rb
->xfer
->flag
);
2024 bind
= fw_bindlookup(rb
->fc
, fp
->mode
.rreqq
.dest_hi
,
2025 fp
->mode
.rreqq
.dest_lo
);
2028 printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
2029 #if defined(__DragonFly__) || \
2030 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
2031 " src=0x%x data=%lx\n",
2033 " src=0x%x data=%x\n",
2035 fp
->mode
.wreqq
.dest_hi
, fp
->mode
.wreqq
.dest_lo
,
2036 tcode_str
[tcode
], tcode
,
2037 fp
->mode
.hdr
.src
, ntohl(fp
->mode
.wreqq
.data
));
2039 if (rb
->fc
->status
== FWBUSINIT
) {
2040 printf("fw_rcv: cannot respond(bus reset)!\n");
2043 rb
->xfer
= fw_xfer_alloc(M_FWXFER
);
2044 if(rb
->xfer
== NULL
){
2047 rb
->xfer
->send
.spd
= rb
->spd
;
2048 rb
->xfer
->send
.pay_len
= 0;
2049 resfp
= &rb
->xfer
->send
.hdr
;
2053 resfp
->mode
.hdr
.tcode
= FWTCODE_WRES
;
2056 resfp
->mode
.hdr
.tcode
= FWTCODE_RRESQ
;
2059 resfp
->mode
.hdr
.tcode
= FWTCODE_RRESB
;
2062 resfp
->mode
.hdr
.tcode
= FWTCODE_LRES
;
2065 resfp
->mode
.hdr
.dst
= fp
->mode
.hdr
.src
;
2066 resfp
->mode
.hdr
.tlrt
= fp
->mode
.hdr
.tlrt
;
2067 resfp
->mode
.hdr
.pri
= fp
->mode
.hdr
.pri
;
2068 resfp
->mode
.rresb
.rtcode
= RESP_ADDRESS_ERROR
;
2069 resfp
->mode
.rresb
.extcode
= 0;
2070 resfp
->mode
.rresb
.len
= 0;
2072 rb->xfer->hand = fw_xferwake;
2074 rb
->xfer
->hand
= fw_xfer_free
;
2075 if(fw_asyreq(rb
->fc
, -1, rb
->xfer
)){
2076 fw_xfer_free(rb
->xfer
);
2082 for (i
= 0; i
< rb
->nvec
; i
++)
2083 len
+= rb
->vec
[i
].iov_len
;
2084 rb
->xfer
= STAILQ_FIRST(&bind
->xferlist
);
2085 if (rb
->xfer
== NULL
) {
2087 printf("Discard a packet for this bind.\n");
2091 STAILQ_REMOVE_HEAD(&bind
->xferlist
, link
);
2093 rb
->xfer
->hand(rb
->xfer
);
2095 #if 0 /* shouldn't happen ?? or for GASP */
2096 case FWTCODE_STREAM
:
2098 struct fw_xferq
*xferq
;
2100 xferq
= rb
->fc
->ir
[sub
];
2102 printf("stream rcv dma %d len %d off %d spd %d\n",
2103 sub
, len
, off
, spd
);
2105 if(xferq
->queued
>= xferq
->maxq
) {
2106 printf("receive queue is full\n");
2109 /* XXX get xfer from xfer queue, we don't need copy for
2111 rb
->xfer
= fw_xfer_alloc_buf(M_FWXFER
, 0, /* XXX */
2113 if (rb
->xfer
== NULL
)
2118 STAILQ_INSERT_TAIL(&xferq
->q
, rb
->xfer
, link
);
2120 sc
= device_get_softc(rb
->fc
->bdev
);
2121 #if defined(__DragonFly__) || \
2122 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
2123 if (&xferq
->rsel
.si_pid
!= 0)
2125 if (SEL_WAITING(&xferq
->rsel
))
2127 selwakeuppri(&xferq
->rsel
, FWPRI
);
2128 if (xferq
->flag
& FWXFERQ_WAKEUP
) {
2129 xferq
->flag
&= ~FWXFERQ_WAKEUP
;
2130 wakeup((void *)xferq
);
2132 if (xferq
->flag
& FWXFERQ_HANDLER
) {
2140 printf("fw_rcv: unknow tcode %d\n", tcode
);
2146 * Post process for Bus Manager election process.
2149 fw_try_bmr_callback(struct fw_xfer
*xfer
)
2151 struct firewire_comm
*fc
;
2157 if (xfer
->resp
!= 0)
2159 if (xfer
->recv
.payload
== NULL
)
2161 if (xfer
->recv
.hdr
.mode
.lres
.rtcode
!= FWRCODE_COMPLETE
)
2164 bmr
= ntohl(xfer
->recv
.payload
[0]);
2168 CSRARC(fc
, BUS_MGR_ID
) = fc
->set_bmr(fc
, bmr
& 0x3f);
2169 fw_xfer_free_buf(xfer
);
2174 fw_printf(fc
->bdev
, "bus manager election failed\n");
2175 fw_xfer_free_buf(xfer
);
2180 * To candidate Bus Manager election process.
2183 fw_try_bmr(void *arg
)
2185 struct fw_xfer
*xfer
;
2186 struct firewire_comm
*fc
= (struct firewire_comm
*)arg
;
2190 xfer
= fw_xfer_alloc_buf(M_FWXFER
, 8, 4);
2195 fc
->status
= FWBUSMGRELECT
;
2197 fp
= &xfer
->send
.hdr
;
2198 fp
->mode
.lreq
.dest_hi
= 0xffff;
2199 fp
->mode
.lreq
.tlrt
= 0;
2200 fp
->mode
.lreq
.tcode
= FWTCODE_LREQ
;
2201 fp
->mode
.lreq
.pri
= 0;
2202 fp
->mode
.lreq
.src
= 0;
2203 fp
->mode
.lreq
.len
= 8;
2204 fp
->mode
.lreq
.extcode
= EXTCODE_CMP_SWAP
;
2205 fp
->mode
.lreq
.dst
= FWLOCALBUS
| fc
->irm
;
2206 fp
->mode
.lreq
.dest_lo
= 0xf0000000 | BUS_MGR_ID
;
2207 xfer
->send
.payload
[0] = htonl(0x3f);
2208 xfer
->send
.payload
[1] = htonl(fc
->nodeid
);
2209 xfer
->hand
= fw_try_bmr_callback
;
2211 err
= fw_asyreq(fc
, -1, xfer
);
2213 fw_xfer_free_buf(xfer
);
2221 * Software implementation for physical memory block access.
2222 * XXX:Too slow, usef for debug purpose only.
2225 fw_vmaccess(struct fw_xfer
*xfer
){
2226 struct fw_pkt
*rfp
, *sfp
= NULL
;
2227 uint32_t *ld
= (uint32_t *)xfer
->recv
.buf
;
2229 printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2230 xfer
->spd
, xfer
->recv
.len
, ntohl(ld
[0]), ntohl(ld
[1]), ntohl(ld
[2]), ntohl(ld
[3]));
2231 printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld
[4]), ntohl(ld
[5]), ntohl(ld
[6]), ntohl(ld
[7]));
2232 if(xfer
->resp
!= 0){
2233 fw_xfer_free( xfer
);
2236 if(xfer
->recv
.buf
== NULL
){
2237 fw_xfer_free( xfer
);
2240 rfp
= (struct fw_pkt
*)xfer
->recv
.buf
;
2241 switch(rfp
->mode
.hdr
.tcode
){
2242 /* XXX need fix for 64bit arch */
2244 xfer
->send
.buf
= malloc(12, M_FW
, M_NOWAIT
);
2245 xfer
->send
.len
= 12;
2246 sfp
= (struct fw_pkt
*)xfer
->send
.buf
;
2247 bcopy(rfp
->mode
.wreqb
.payload
,
2248 (void *)ntohl(rfp
->mode
.wreqb
.dest_lo
), ntohs(rfp
->mode
.wreqb
.len
));
2249 sfp
->mode
.wres
.tcode
= FWTCODE_WRES
;
2250 sfp
->mode
.wres
.rtcode
= 0;
2253 xfer
->send
.buf
= malloc(12, M_FW
, M_NOWAIT
);
2254 xfer
->send
.len
= 12;
2255 sfp
->mode
.wres
.tcode
= FWTCODE_WRES
;
2256 *((uint32_t *)(ntohl(rfp
->mode
.wreqb
.dest_lo
))) = rfp
->mode
.wreqq
.data
;
2257 sfp
->mode
.wres
.rtcode
= 0;
2260 xfer
->send
.buf
= malloc(16 + rfp
->mode
.rreqb
.len
, M_FW
, M_NOWAIT
);
2261 xfer
->send
.len
= 16 + ntohs(rfp
->mode
.rreqb
.len
);
2262 sfp
= (struct fw_pkt
*)xfer
->send
.buf
;
2263 bcopy((void *)ntohl(rfp
->mode
.rreqb
.dest_lo
),
2264 sfp
->mode
.rresb
.payload
, (uint16_t)ntohs(rfp
->mode
.rreqb
.len
));
2265 sfp
->mode
.rresb
.tcode
= FWTCODE_RRESB
;
2266 sfp
->mode
.rresb
.len
= rfp
->mode
.rreqb
.len
;
2267 sfp
->mode
.rresb
.rtcode
= 0;
2268 sfp
->mode
.rresb
.extcode
= 0;
2271 xfer
->send
.buf
= malloc(16, M_FW
, M_NOWAIT
);
2272 xfer
->send
.len
= 16;
2273 sfp
= (struct fw_pkt
*)xfer
->send
.buf
;
2274 sfp
->mode
.rresq
.data
= *(uint32_t *)(ntohl(rfp
->mode
.rreqq
.dest_lo
));
2275 sfp
->mode
.wres
.tcode
= FWTCODE_RRESQ
;
2276 sfp
->mode
.rresb
.rtcode
= 0;
2279 fw_xfer_free( xfer
);
2282 sfp
->mode
.hdr
.dst
= rfp
->mode
.hdr
.src
;
2283 xfer
->dst
= ntohs(rfp
->mode
.hdr
.src
);
2284 xfer
->hand
= fw_xfer_free
;
2286 sfp
->mode
.hdr
.tlrt
= rfp
->mode
.hdr
.tlrt
;
2287 sfp
->mode
.hdr
.pri
= 0;
2289 fw_asyreq(xfer
->fc
, -1, xfer
);
2296 * CRC16 check-sum for IEEE1394 register blocks.
2299 fw_crc16(uint32_t *ptr
, uint32_t len
){
2300 uint32_t i
, sum
, crc
= 0;
2302 len
= (len
+ 3) & ~3;
2303 for(i
= 0 ; i
< len
; i
+= 4){
2304 for( shift
= 28 ; shift
>= 0 ; shift
-= 4){
2305 sum
= ((crc
>> 12) ^ (ptr
[i
/4] >> shift
)) & 0xf;
2306 crc
= (crc
<< 4) ^ ( sum
<< 12 ) ^ ( sum
<< 5) ^ sum
;
2310 return((uint16_t) crc
);
2314 fw_bmr(struct firewire_comm
*fc
)
2316 struct fw_device fwdev
;
2317 union fw_self_id
*self_id
;
2321 /* Check to see if the current root node is cycle master capable */
2322 self_id
= fw_find_self_id(fc
, fc
->max_node
);
2323 if (fc
->max_node
> 0) {
2324 /* XXX check cmc bit of businfo block rather than contender */
2325 if (self_id
->p0
.link_active
&& self_id
->p0
.contender
)
2326 cmstr
= fc
->max_node
;
2329 "root node is not cycle master capable\n");
2330 /* XXX shall we be the cycle master? */
2332 /* XXX need bus reset */
2337 fw_printf(fc
->bdev
, "bus manager %d ", CSRARC(fc
, BUS_MGR_ID
));
2338 if(CSRARC(fc
, BUS_MGR_ID
) != fc
->nodeid
) {
2339 /* We are not the bus manager */
2340 aprint_normal("\n");
2343 aprint_normal("(me)\n");
2345 /* Optimize gapcount */
2346 if(fc
->max_hop
<= MAX_GAPHOP
)
2347 fw_phy_config(fc
, cmstr
, gap_cnt
[fc
->max_hop
]);
2348 /* If we are the cycle master, nothing to do */
2349 if (cmstr
== fc
->nodeid
|| cmstr
== -1)
2351 /* Bus probe has not finished, make dummy fwdev for cmstr */
2352 memset(&fwdev
, 0, sizeof(fwdev
));
2356 fwdev
.maxrec
= 8; /* 512 */
2357 fwdev
.status
= FWDEVINIT
;
2358 /* Set cmstr bit on the cycle master */
2359 quad
= htonl(1 << 8);
2360 fwmem_write_quad(&fwdev
, NULL
, 0/*spd*/,
2361 0xffff, 0xf0000000 | STATE_SET
, &quad
, fw_asy_callback_free
);
2367 fw_open_isodma(struct firewire_comm
*fc
, int tx
)
2369 struct fw_xferq
**xferqa
;
2370 struct fw_xferq
*xferq
;
2374 xferqa
= &fc
->it
[0];
2376 xferqa
= &fc
->ir
[0];
2379 for (i
= 0; i
< fc
->nisodma
; i
++) {
2381 if ((xferq
->flag
& FWXFERQ_OPEN
) == 0) {
2382 xferq
->flag
|= FWXFERQ_OPEN
;
2386 if (i
== fc
->nisodma
) {
2387 printf("no free dma channel (tx=%d)\n", tx
);
2394 #if defined(__FreeBSD__)
2396 fw_modevent(module_t mode
, int type
, void *data
)
2399 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2400 static eventhandler_tag fwdev_ehtag
= NULL
;
2405 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2406 fwdev_ehtag
= EVENTHANDLER_REGISTER(dev_clone
,
2407 fwdev_clone
, 0, 1000);
2411 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2412 if (fwdev_ehtag
!= NULL
)
2413 EVENTHANDLER_DEREGISTER(dev_clone
, fwdev_ehtag
);
2419 return (EOPNOTSUPP
);
2425 #ifdef __DragonFly__
2426 DECLARE_DUMMY_MODULE(firewire
);
2428 DRIVER_MODULE(firewire
,fwohci
,firewire_driver
,firewire_devclass
,fw_modevent
,0);
2429 MODULE_VERSION(firewire
, 1);