2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the acknowledgement as bellow:
17 * This product includes software developed by K. Kobayashi and H. Shimokawa
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * $FreeBSD: src/sys/dev/firewire/firewire.c,v 1.3.2.22 2003/05/12 04:16:30 simokawa Exp $
35 * $DragonFly: src/sys/bus/firewire/firewire.c,v 1.3 2003/08/07 21:16:45 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
51 #include <machine/cpufunc.h> /* for rdtsc proto for clock.h below */
52 #include <machine/clock.h>
54 #include <sys/bus.h> /* used by smbus and newbus */
55 #include <machine/bus.h>
58 #include "firewirereg.h"
63 int firewire_debug=0, try_bmr=1;
64 SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
65 "FireWire driver debug flag");
66 SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
67 SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
68 "Try to be a bus manager");
70 MALLOC_DEFINE(M_FW, "firewire", "FireWire");
71 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
73 #define FW_MAXASYRTY 4
74 #define FW_MAXDEVRCNT 4
76 devclass_t firewire_devclass;
78 static int firewire_match __P((device_t));
79 static int firewire_attach __P((device_t));
80 static int firewire_detach __P((device_t));
82 static int firewire_shutdown __P((device_t));
84 static device_t firewire_add_child __P((device_t, int, const char *, int));
85 static void fw_try_bmr __P((void *));
86 static void fw_try_bmr_callback __P((struct fw_xfer *));
87 static void fw_asystart __P((struct fw_xfer *));
88 static int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *));
89 static void fw_bus_probe __P((struct firewire_comm *));
90 static void fw_bus_explore __P((struct firewire_comm *));
91 static void fw_bus_explore_callback __P((struct fw_xfer *));
92 static void fw_attach_dev __P((struct firewire_comm *));
94 static void fw_vmaccess __P((struct fw_xfer *));
96 struct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t,
97 u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *))));
98 static int fw_bmr __P((struct firewire_comm *));
100 static device_method_t firewire_methods[] = {
101 /* Device interface */
102 DEVMETHOD(device_probe, firewire_match),
103 DEVMETHOD(device_attach, firewire_attach),
104 DEVMETHOD(device_detach, firewire_detach),
105 DEVMETHOD(device_suspend, bus_generic_suspend),
106 DEVMETHOD(device_resume, bus_generic_resume),
107 DEVMETHOD(device_shutdown, bus_generic_shutdown),
110 DEVMETHOD(bus_add_child, firewire_add_child),
111 DEVMETHOD(bus_print_child, bus_generic_print_child),
115 char linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"};
117 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
118 #define MAX_GAPHOP 15
119 u_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18,
120 21, 24, 26, 29, 32, 35, 37, 40};
122 extern struct cdevsw firewire_cdevsw;
124 static driver_t firewire_driver = {
127 sizeof(struct firewire_softc),
131 * Lookup fwdev by node id.
134 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
136 struct fw_device *fwdev;
140 STAILQ_FOREACH(fwdev, &fc->devices, link)
141 if (fwdev->dst == dst)
145 if(fwdev == NULL) return NULL;
146 if(fwdev->status == FWDEVINVAL) return NULL;
151 * Lookup fwdev by EUI64.
154 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
156 struct fw_device *fwdev;
160 STAILQ_FOREACH(fwdev, &fc->devices, link)
161 if (FW_EUI64_EQUAL(fwdev->eui, *eui))
165 if(fwdev == NULL) return NULL;
166 if(fwdev->status == FWDEVINVAL) return NULL;
171 * Async. request procedure for userland application.
174 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
177 struct fw_xferq *xferq;
181 struct tcode_info *info;
183 if(xfer == NULL) return EINVAL;
184 if(xfer->send.len > MAXREC(fc->maxrec)){
185 printf("send.len > maxrec\n");
188 if(xfer->act.hand == NULL){
189 printf("act.hand == NULL\n");
192 fp = (struct fw_pkt *)xfer->send.buf;
194 tcode = fp->mode.common.tcode & 0xf;
195 info = &fc->tcode[tcode];
196 if (info->flag == 0) {
197 printf("invalid tcode=%d\n", tcode);
200 if (info->flag & FWTI_REQ)
205 if (info->flag & FWTI_BLOCK_STR)
206 len += fp->mode.stream.len;
207 else if (info->flag & FWTI_BLOCK_ASY)
208 len += fp->mode.rresb.len;
209 if( len > xfer->send.len ){
210 printf("len(%d) > send.len(%d) (tcode=%d)\n",
211 len, xfer->send.len, tcode);
214 xfer->send.len = len;
216 if(xferq->start == NULL){
217 printf("xferq->start == NULL\n");
220 if(!(xferq->queued < xferq->maxq)){
221 device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
227 if (info->flag & FWTI_TLABEL) {
228 if((tl = fw_get_tlabel(fc, xfer)) == -1 )
230 fp->mode.hdr.tlrt = tl << 2;
237 xfer->retry_req = fw_asybusy;
243 * Wakeup blocked process.
246 fw_asy_callback(struct fw_xfer *xfer){
251 * Postpone to later retry.
253 void fw_asybusy(struct fw_xfer *xfer){
254 printf("fw_asybusy\n");
256 xfer->ch = timeout((timeout_t *)fw_asystart, (void *)xfer, 20000);
264 * Async. request with given xfer structure.
267 fw_asystart(struct fw_xfer *xfer)
269 struct firewire_comm *fc = xfer->fc;
271 if(xfer->retry++ >= fc->max_asyretry){
272 device_printf(fc->bdev, "max_asyretry exceeded\n");
274 xfer->state = FWXF_BUSY;
275 xfer->act.hand(xfer);
278 #if 0 /* XXX allow bus explore packets only after bus rest */
279 if (fc->status < FWBUSEXPLORE) {
281 xfer->state = FWXF_BUSY;
282 if (xfer->act.hand != NULL)
283 xfer->act.hand(xfer);
288 xfer->state = FWXF_INQ;
289 STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
292 /* XXX just queue for mbuf */
293 if (xfer->mbuf == NULL)
299 firewire_match( device_t dev )
301 device_set_desc(dev, "IEEE1394(FireWire) bus");
306 firewire_xfer_timeout(struct firewire_comm *fc)
308 struct fw_xfer *xfer;
311 struct timeval split_timeout;
314 split_timeout.tv_sec = 6;
315 split_timeout.tv_usec = 0;
318 timevalsub(&tv, &split_timeout);
321 for (i = 0; i < 0x40; i ++) {
322 while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
324 if (timevalcmp(&xfer->tv, &tv, >))
325 /* the rests are newer than this */
327 device_printf(fc->bdev,
328 "split transaction timeout dst=0x%x tl=0x%x\n",
330 xfer->resp = ETIMEDOUT;
331 STAILQ_REMOVE_HEAD(&fc->tlabels[i], link);
339 firewire_watchdog(void *arg)
341 struct firewire_comm *fc;
343 fc = (struct firewire_comm *)arg;
344 firewire_xfer_timeout(fc);
346 callout_reset(&fc->timeout_callout, hz,
347 (void *)firewire_watchdog, (void *)fc);
351 * The attach routine.
354 firewire_attach( device_t dev )
357 struct firewire_softc *sc = device_get_softc(dev);
358 device_t pa = device_get_parent(dev);
359 struct firewire_comm *fc;
362 fc = (struct firewire_comm *)device_get_softc(pa);
366 unitmask = UNIT2MIN(device_get_unit(dev));
368 if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
369 for ( i = 0 ; i < fc->nisodma ; i++ ){
371 /* XXX device name should be improved */
372 d = make_dev(&firewire_cdevsw, unit2minor(mn),
373 UID_ROOT, GID_OPERATOR, 0660,
375 #if __FreeBSD_version >= 500000
379 dev_depends(sc->dev, d);
384 d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG),
385 UID_ROOT, GID_OPERATOR, 0660,
386 "fwmem%d", device_get_unit(dev));
387 #if __FreeBSD_version >= 500000
388 dev_depends(sc->dev, d);
392 CALLOUT_INIT(&sc->fc->timeout_callout);
393 CALLOUT_INIT(&sc->fc->bmr_callout);
394 CALLOUT_INIT(&sc->fc->retry_probe_callout);
395 CALLOUT_INIT(&sc->fc->busprobe_callout);
397 callout_reset(&sc->fc->timeout_callout, hz,
398 (void *)firewire_watchdog, (void *)sc->fc);
400 /* Locate our children */
401 bus_generic_probe(dev);
403 /* launch attachement of the added children */
404 bus_generic_attach(dev);
413 * Attach it as child.
416 firewire_add_child(device_t dev, int order, const char *name, int unit)
419 struct firewire_softc *sc;
421 sc = (struct firewire_softc *)device_get_softc(dev);
422 child = device_add_child(dev, name, unit);
424 device_set_ivars(child, sc->fc);
425 device_probe_and_attach(child);
435 firewire_detach( device_t dev )
437 struct firewire_softc *sc;
438 struct csrdir *csrd, *next;
439 struct fw_device *fwdev, *fwdev_next;
441 sc = (struct firewire_softc *)device_get_softc(dev);
443 bus_generic_detach(dev);
445 callout_stop(&sc->fc->timeout_callout);
446 callout_stop(&sc->fc->bmr_callout);
447 callout_stop(&sc->fc->retry_probe_callout);
448 callout_stop(&sc->fc->busprobe_callout);
450 #if __FreeBSD_version >= 500000
451 destroy_dev(sc->dev);
455 for (j = 0 ; j < sc->fc->nisodma + 1; j++)
456 destroy_dev(sc->dev[j]);
459 /* XXX xfree_free and untimeout on all xfers */
460 for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL;
461 fwdev = fwdev_next) {
462 fwdev_next = STAILQ_NEXT(fwdev, link);
465 for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) {
466 next = SLIST_NEXT(csrd, link);
469 free(sc->fc->topology_map, M_FW);
470 free(sc->fc->speed_map, M_FW);
475 firewire_shutdown( device_t dev )
483 fw_xferq_drain(struct fw_xferq *xferq)
485 struct fw_xfer *xfer;
487 while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
488 STAILQ_REMOVE_HEAD(&xferq->q, link);
496 fw_drain_txq(struct firewire_comm *fc)
500 fw_xferq_drain(fc->atq);
501 fw_xferq_drain(fc->ats);
502 for(i = 0; i < fc->nisodma; i++)
503 fw_xferq_drain(fc->it[i]);
507 * Called after bus reset.
510 fw_busreset(struct firewire_comm *fc)
512 struct firewire_dev_comm *fdc;
519 callout_stop(&fc->bmr_callout);
524 fc->status = FWBUSRESET;
525 CSRARC(fc, STATE_CLEAR)
526 = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
527 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
528 CSRARC(fc, NODE_IDS) = 0x3f;
530 CSRARC(fc, TOPO_MAP + 8) = 0;
535 for(i = 2; i < 0x100/4 - 2 ; i++){
536 CSRARC(fc, SPED_MAP + i * 4) = 0;
538 CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
539 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
540 CSRARC(fc, RESET_START) = 0;
541 CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
542 CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
543 CSRARC(fc, CYCLE_TIME) = 0x0;
544 CSRARC(fc, BUS_TIME) = 0x0;
545 CSRARC(fc, BUS_MGR_ID) = 0x3f;
546 CSRARC(fc, BANDWIDTH_AV) = 4915;
547 CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
548 CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
549 CSRARC(fc, IP_CHANNELS) = (1 << 31);
551 CSRARC(fc, CONF_ROM) = 0x04 << 24;
552 CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
553 CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
554 1 << 28 | 0xff << 16 | 0x09 << 8;
555 CSRARC(fc, CONF_ROM + 0xc) = 0;
557 /* DV depend CSRs see blue book */
558 CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
559 CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
561 CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
562 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
564 if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
565 for( i = 0 ; i < devcnt ; i++)
566 if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
567 fdc = device_get_softc(devlistp[i]);
568 if (fdc->post_busreset != NULL)
569 fdc->post_busreset(fdc);
571 free(devlistp, M_TEMP);
575 /* Call once after reboot */
576 void fw_init(struct firewire_comm *fc)
581 struct fw_xfer *xfer;
585 fc->max_asyretry = FW_MAXASYRTY;
602 STAILQ_INIT(&fc->atq->q);
603 STAILQ_INIT(&fc->ats->q);
605 for( i = 0 ; i < fc->nisodma ; i ++ ){
606 fc->it[i]->queued = 0;
607 fc->ir[i]->queued = 0;
609 fc->it[i]->start = NULL;
610 fc->ir[i]->start = NULL;
612 fc->it[i]->buf = NULL;
613 fc->ir[i]->buf = NULL;
615 fc->it[i]->flag = FWXFERQ_STREAM;
616 fc->ir[i]->flag = FWXFERQ_STREAM;
618 STAILQ_INIT(&fc->it[i]->q);
619 STAILQ_INIT(&fc->ir[i]->q);
621 STAILQ_INIT(&fc->it[i]->binds);
622 STAILQ_INIT(&fc->ir[i]->binds);
625 fc->arq->maxq = FWMAXQUEUE;
626 fc->ars->maxq = FWMAXQUEUE;
627 fc->atq->maxq = FWMAXQUEUE;
628 fc->ats->maxq = FWMAXQUEUE;
630 for( i = 0 ; i < fc->nisodma ; i++){
631 fc->ir[i]->maxq = FWMAXQUEUE;
632 fc->it[i]->maxq = FWMAXQUEUE;
634 /* Initialize csr registers */
635 fc->topology_map = (struct fw_topology_map *)malloc(
636 sizeof(struct fw_topology_map),
637 M_FW, M_NOWAIT | M_ZERO);
638 fc->speed_map = (struct fw_speed_map *)malloc(
639 sizeof(struct fw_speed_map),
640 M_FW, M_NOWAIT | M_ZERO);
641 CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
642 CSRARC(fc, TOPO_MAP + 4) = 1;
643 CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
644 CSRARC(fc, SPED_MAP + 4) = 1;
646 STAILQ_INIT(&fc->devices);
647 STAILQ_INIT(&fc->pending);
649 /* Initialize csr ROM work space */
650 SLIST_INIT(&fc->ongocsr);
651 SLIST_INIT(&fc->csrfree);
652 for( i = 0 ; i < FWMAXCSRDIR ; i++){
653 csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT);
654 if(csrd == NULL) break;
655 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
658 /* Initialize Async handlers */
659 STAILQ_INIT(&fc->binds);
660 for( i = 0 ; i < 0x40 ; i++){
661 STAILQ_INIT(&fc->tlabels[i]);
664 /* DV depend CSRs see blue book */
666 CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
667 CSRARC(fc, oPCR) = 0x8000007a;
668 for(i = 4 ; i < 0x7c/4 ; i+=4){
669 CSRARC(fc, i + oPCR) = 0x8000007a;
672 CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
673 CSRARC(fc, iPCR) = 0x803f0000;
674 for(i = 4 ; i < 0x7c/4 ; i+=4){
675 CSRARC(fc, i + iPCR) = 0x0;
681 xfer = fw_xfer_alloc();
682 if(xfer == NULL) return;
684 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
688 xfer->act.hand = fw_vmaccess;
694 fwb->addrlen = 0xffffffff;
701 * To lookup binded process from IEEE1394 address.
704 fw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo)
707 for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ;
708 tfw = STAILQ_NEXT(tfw, fclist)){
709 if (tfw->act_type != FWACT_NULL &&
710 tfw->start_hi == dest_hi &&
711 tfw->start_lo <= dest_lo &&
712 (tfw->start_lo + tfw->addrlen) > dest_lo){
720 * To bind IEEE1394 address block to process.
723 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
725 struct fw_bind *tfw, *tfw2 = NULL;
727 tfw = STAILQ_FIRST(&fc->binds);
729 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
732 if((tfw->start_hi > fwb->start_hi) ||
733 (tfw->start_hi == fwb->start_hi &&
734 (tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){
735 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
738 for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){
739 if((tfw->start_hi < fwb->start_hi) ||
740 (tfw->start_hi == fwb->start_hi &&
741 (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){
742 tfw2 = STAILQ_NEXT(tfw, fclist);
745 if((tfw2->start_hi > fwb->start_hi) ||
746 (tfw2->start_hi == fwb->start_hi &&
747 tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){
756 STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist);
758 STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist);
761 if (!err && fwb->act_type == FWACT_CH)
762 STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist);
767 * To free IEEE1394 address block.
770 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
773 struct fw_xfer *xfer, *next;
776 /* shall we check the existance? */
777 STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
778 /* shall we do this? */
779 for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
780 next = STAILQ_NEXT(xfer, link);
783 STAILQ_INIT(&fwb->xferlist);
790 * To free transaction label.
793 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
798 for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL;
799 tl = STAILQ_NEXT(tl, link)){
800 if(tl->xfer == xfer){
801 STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link);
812 * To obtain XFER structure by transaction label.
814 static struct fw_xfer *
815 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
817 struct fw_xfer *xfer;
821 for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL;
822 tl = STAILQ_NEXT(tl, link)){
823 if(tl->xfer->dst == node){
826 if (firewire_debug > 2)
827 printf("fw_tl2xfer: found tl=%d\n", tlabel);
831 if (firewire_debug > 1)
832 printf("fw_tl2xfer: not found tl=%d\n", tlabel);
838 * To allocate IEEE1394 XFER structure.
841 fw_xfer_alloc(struct malloc_type *type)
843 struct fw_xfer *xfer;
845 xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
849 microtime(&xfer->tv);
856 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
858 struct fw_xfer *xfer;
860 xfer = fw_xfer_alloc(type);
861 xfer->send.len = send_len;
862 xfer->recv.len = recv_len;
866 xfer->send.buf = malloc(send_len, type, M_NOWAIT | M_ZERO);
867 if (xfer->send.buf == NULL) {
873 xfer->recv.buf = malloc(recv_len, type, M_NOWAIT);
874 if (xfer->recv.buf == NULL) {
875 if (xfer->send.buf != NULL)
876 free(xfer->send.buf, type);
885 * IEEE1394 XFER post process.
888 fw_xfer_done(struct fw_xfer *xfer)
890 if (xfer->act.hand == NULL)
893 if (xfer->fc->status != FWBUSRESET)
894 xfer->act.hand(xfer);
896 printf("fw_xfer_done: pending\n");
897 if (xfer->fc != NULL)
898 STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link);
900 panic("fw_xfer_done: why xfer->fc is NULL?");
905 fw_xfer_unload(struct fw_xfer* xfer)
909 if(xfer == NULL ) return;
910 if(xfer->state == FWXF_INQ){
911 printf("fw_xfer_free FWXF_INQ\n");
913 STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
917 if (xfer->fc != NULL) {
919 if(xfer->state == FWXF_START)
921 * This could happen if:
922 * 1. We call fwohci_arcv() before fwohci_txd().
923 * 2. firewire_watch() is called.
925 printf("fw_xfer_free FWXF_START\n");
927 fw_tl_free(xfer->fc, xfer);
929 xfer->state = FWXF_INIT;
934 * To free IEEE1394 XFER structure.
937 fw_xfer_free( struct fw_xfer* xfer)
939 if(xfer == NULL ) return;
940 fw_xfer_unload(xfer);
941 if(xfer->send.buf != NULL){
942 free(xfer->send.buf, xfer->malloc);
944 if(xfer->recv.buf != NULL){
945 free(xfer->recv.buf, xfer->malloc);
947 free(xfer, xfer->malloc);
951 fw_asy_callback_free(struct fw_xfer *xfer)
954 printf("asyreq done state=%d resp=%d\n",
955 xfer->state, xfer->resp);
964 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
966 struct fw_xfer *xfer;
969 fc->status = FWBUSPHYCONF;
971 xfer = fw_xfer_alloc_buf(M_FWXFER, 12, 0);
975 xfer->retry_req = fw_asybusy;
976 xfer->act.hand = fw_asy_callback_free;
978 fp = (struct fw_pkt *)xfer->send.buf;
981 fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
983 fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
984 fp->mode.ld[2] = ~fp->mode.ld[1];
985 /* XXX Dangerous, how to pass PHY packet to device driver */
986 fp->mode.common.tcode |= FWTCODE_PHY;
989 printf("send phy_config root_node=%d gap_count=%d\n",
990 root_node, gap_count);
991 fw_asyreq(fc, -1, xfer);
999 fw_print_sid(u_int32_t sid)
1001 union fw_self_id *s;
1002 s = (union fw_self_id *) &sid;
1003 printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1004 " p0:%d p1:%d p2:%d i:%d m:%d\n",
1005 s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1006 s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1007 s->p0.power_class, s->p0.port0, s->p0.port1,
1008 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1013 * To receive self ID.
1015 void fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len)
1018 union fw_self_id *self_id;
1019 u_int i, j, node, c_port = 0, i_branch = 0;
1021 fc->sid_cnt = len /(sizeof(u_int32_t) * 2);
1022 fc->status = FWBUSINIT;
1023 fc->max_node = fc->nodeid & 0x3f;
1024 CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16;
1025 fc->status = FWBUSCYMELECT;
1026 fc->topology_map->crc_len = 2;
1027 fc->topology_map->generation ++;
1028 fc->topology_map->self_id_count = 0;
1029 fc->topology_map->node_count = 0;
1030 fc->speed_map->generation ++;
1031 fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1032 self_id = &fc->topology_map->self_id[0];
1033 for(i = 0; i < fc->sid_cnt; i ++){
1034 if (sid[1] != ~sid[0]) {
1035 printf("fw_sidrcv: invalid self-id packet\n");
1039 *self_id = *((union fw_self_id *)sid);
1040 fc->topology_map->crc_len++;
1041 if(self_id->p0.sequel == 0){
1042 fc->topology_map->node_count ++;
1045 fw_print_sid(sid[0]);
1047 node = self_id->p0.phy_id;
1048 if(fc->max_node < node){
1049 fc->max_node = self_id->p0.phy_id;
1051 /* XXX I'm not sure this is the right speed_map */
1052 fc->speed_map->speed[node][node]
1053 = self_id->p0.phy_speed;
1054 for (j = 0; j < node; j ++) {
1055 fc->speed_map->speed[j][node]
1056 = fc->speed_map->speed[node][j]
1057 = min(fc->speed_map->speed[j][j],
1058 self_id->p0.phy_speed);
1060 if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1061 (self_id->p0.link_active && self_id->p0.contender)) {
1062 fc->irm = self_id->p0.phy_id;
1064 if(self_id->p0.port0 >= 0x2){
1067 if(self_id->p0.port1 >= 0x2){
1070 if(self_id->p0.port2 >= 0x2){
1075 i_branch += (c_port - 2);
1079 fc->topology_map->self_id_count ++;
1081 device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1083 fc->topology_map->crc = fw_crc16(
1084 (u_int32_t *)&fc->topology_map->generation,
1085 fc->topology_map->crc_len * 4);
1086 fc->speed_map->crc = fw_crc16(
1087 (u_int32_t *)&fc->speed_map->generation,
1088 fc->speed_map->crc_len * 4);
1089 /* byteswap and copy to CSR */
1090 p = (u_int32_t *)fc->topology_map;
1091 for (i = 0; i <= fc->topology_map->crc_len; i++)
1092 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1093 p = (u_int32_t *)fc->speed_map;
1094 CSRARC(fc, SPED_MAP) = htonl(*p++);
1095 CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1096 /* don't byte-swap u_int8_t array */
1097 bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1099 fc->max_hop = fc->max_node - i_branch;
1100 printf(", maxhop <= %d", fc->max_hop);
1103 printf(", Not found IRM capable node");
1105 printf(", cable IRM = %d", fc->irm);
1106 if (fc->irm == fc->nodeid)
1111 if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1112 if (fc->irm == fc->nodeid) {
1113 fc->status = FWBUSMGRDONE;
1114 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1117 fc->status = FWBUSMGRELECT;
1118 callout_reset(&fc->bmr_callout, hz/8,
1119 (void *)fw_try_bmr, (void *)fc);
1122 fc->status = FWBUSMGRDONE;
1124 callout_reset(&fc->busprobe_callout, hz/4,
1125 (void *)fw_bus_probe, (void *)fc);
1129 * To probe devices on the IEEE1394 bus.
1132 fw_bus_probe(struct firewire_comm *fc)
1135 struct fw_device *fwdev, *next;
1138 fc->status = FWBUSEXPLORE;
1139 fc->retry_count = 0;
1142 * Invalidate all devices, just after bus reset. Devices
1143 * to be removed has not been seen longer time.
1145 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1146 next = STAILQ_NEXT(fwdev, link);
1147 if (fwdev->status != FWDEVINVAL) {
1148 fwdev->status = FWDEVINVAL;
1150 } else if(fwdev->rcnt < FW_MAXDEVRCNT) {
1153 STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
1158 fc->ongoaddr = CSRROMOFF;
1160 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1166 * To collect device informations on the IEEE1394 bus.
1169 fw_bus_explore(struct firewire_comm *fc )
1172 struct fw_device *fwdev, *pfwdev, *tfwdev;
1174 struct fw_xfer *xfer;
1177 if(fc->status != FWBUSEXPLORE)
1181 if(fc->ongonode == fc->nodeid) fc->ongonode++;
1183 if(fc->ongonode > fc->max_node) goto done;
1184 if(fc->ongonode >= 0x3f) goto done;
1187 /* XXX we need to check phy_id first */
1188 if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) {
1190 printf("node%d: link down\n", fc->ongonode);
1195 if(fc->ongoaddr <= CSRROMOFF &&
1196 fc->ongoeui.hi == 0xffffffff &&
1197 fc->ongoeui.lo == 0xffffffff ){
1198 fc->ongoaddr = CSRROMOFF;
1199 addr = 0xf0000000 | fc->ongoaddr;
1200 }else if(fc->ongoeui.hi == 0xffffffff ){
1201 fc->ongoaddr = CSRROMOFF + 0xc;
1202 addr = 0xf0000000 | fc->ongoaddr;
1203 }else if(fc->ongoeui.lo == 0xffffffff ){
1204 fc->ongoaddr = CSRROMOFF + 0x10;
1205 addr = 0xf0000000 | fc->ongoaddr;
1206 }else if(fc->ongodev == NULL){
1207 STAILQ_FOREACH(fwdev, &fc->devices, link)
1208 if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui))
1211 fwdev->dst = fc->ongonode;
1212 fwdev->status = FWDEVATTACHED;
1214 fc->ongoaddr = CSRROMOFF;
1216 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1219 fwdev = malloc(sizeof(struct fw_device), M_FW, M_NOWAIT);
1224 fwdev->dst = fc->ongonode;
1225 fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo;
1226 fwdev->status = FWDEVINIT;
1227 fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode];
1230 STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1231 if (tfwdev->eui.hi > fwdev->eui.hi ||
1232 (tfwdev->eui.hi == fwdev->eui.hi &&
1233 tfwdev->eui.lo > fwdev->eui.lo))
1238 STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1240 STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1242 device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1243 linkspeed[fwdev->speed],
1244 fc->ongoeui.hi, fc->ongoeui.lo);
1246 fc->ongodev = fwdev;
1247 fc->ongoaddr = CSRROMOFF;
1248 addr = 0xf0000000 | fc->ongoaddr;
1250 addr = 0xf0000000 | fc->ongoaddr;
1253 xfer = asyreqq(fc, FWSPD_S100, 0, 0,
1254 ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr,
1255 fw_bus_explore_callback);
1256 if(xfer == NULL) goto done;
1258 xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1263 fp = (struct fw_pkt *)xfer->send.buf;
1264 fp->mode.rreqq.dest_hi = 0xffff;
1265 fp->mode.rreqq.tlrt = 0;
1266 fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1267 fp->mode.rreqq.pri = 0;
1268 fp->mode.rreqq.src = 0;
1269 xfer->dst = FWLOCALBUS | fc->ongonode;
1270 fp->mode.rreqq.dst = xfer->dst;
1271 fp->mode.rreqq.dest_lo = addr;
1272 xfer->act.hand = fw_bus_explore_callback;
1275 printf("node%d: explore addr=0x%x\n",
1276 fc->ongonode, fc->ongoaddr);
1277 err = fw_asyreq(fc, -1, xfer);
1279 fw_xfer_free( xfer);
1285 /* fw_attach_devs */
1286 fc->status = FWBUSEXPDONE;
1288 printf("bus_explore done\n");
1294 /* Portable Async. request read quad */
1296 asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt,
1297 u_int32_t addr_hi, u_int32_t addr_lo,
1298 void (*hand) __P((struct fw_xfer*)))
1300 struct fw_xfer *xfer;
1304 xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16);
1308 xfer->spd = spd; /* XXX:min(spd, fc->spd) */
1309 fp = (struct fw_pkt *)xfer->send.buf;
1310 fp->mode.rreqq.dest_hi = addr_hi & 0xffff;
1311 if(tl & FWP_TL_VALID){
1312 fp->mode.rreqq.tlrt = (tl & 0x3f) << 2;
1314 fp->mode.rreqq.tlrt = 0;
1316 fp->mode.rreqq.tlrt |= rt & 0x3;
1317 fp->mode.rreqq.tcode = FWTCODE_RREQQ;
1318 fp->mode.rreqq.pri = 0;
1319 fp->mode.rreqq.src = 0;
1320 xfer->dst = addr_hi >> 16;
1321 fp->mode.rreqq.dst = xfer->dst;
1322 fp->mode.rreqq.dest_lo = addr_lo;
1323 xfer->act.hand = hand;
1325 err = fw_asyreq(fc, -1, xfer);
1327 fw_xfer_free( xfer);
1334 * Callback for the IEEE1394 bus information collection.
1337 fw_bus_explore_callback(struct fw_xfer *xfer)
1339 struct firewire_comm *fc;
1340 struct fw_pkt *sfp,*rfp;
1341 struct csrhdr *chdr;
1342 struct csrdir *csrd;
1343 struct csrreg *csrreg;
1348 printf("xfer == NULL\n");
1354 printf("node%d: callback addr=0x%x\n",
1355 fc->ongonode, fc->ongoaddr);
1357 if(xfer->resp != 0){
1358 printf("node%d: resp=%d addr=0x%x\n",
1359 fc->ongonode, xfer->resp, fc->ongoaddr);
1363 if(xfer->send.buf == NULL){
1364 printf("node%d: send.buf=NULL addr=0x%x\n",
1365 fc->ongonode, fc->ongoaddr);
1368 sfp = (struct fw_pkt *)xfer->send.buf;
1370 if(xfer->recv.buf == NULL){
1371 printf("node%d: recv.buf=NULL addr=0x%x\n",
1372 fc->ongonode, fc->ongoaddr);
1375 rfp = (struct fw_pkt *)xfer->recv.buf;
1380 qld = (u_int32_t *)xfer->recv.buf;
1381 printf("len:%d\n", xfer->recv.len);
1382 for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){
1383 printf("0x%08x ", rfp->mode.ld[i/4]);
1384 if((i % 16) == 15) printf("\n");
1386 if((i % 16) != 15) printf("\n");
1389 if(fc->ongodev == NULL){
1390 if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){
1391 rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data);
1392 chdr = (struct csrhdr *)(&rfp->mode.rresq.data);
1393 /* If CSR is minimal confinguration, more investgation is not needed. */
1394 if(chdr->info_len == 1){
1396 printf("node%d: minimal config\n",
1400 fc->ongoaddr = CSRROMOFF + 0xc;
1402 }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){
1403 fc->ongoeui.hi = ntohl(rfp->mode.rresq.data);
1404 fc->ongoaddr = CSRROMOFF + 0x10;
1405 }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){
1406 fc->ongoeui.lo = ntohl(rfp->mode.rresq.data);
1407 if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) {
1409 printf("node%d: eui64 is zero.\n",
1413 fc->ongoaddr = CSRROMOFF;
1416 fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data);
1417 if(fc->ongoaddr > fc->ongodev->rommax){
1418 fc->ongodev->rommax = fc->ongoaddr;
1420 csrd = SLIST_FIRST(&fc->ongocsr);
1421 if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1422 chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1425 chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4];
1428 if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){
1429 csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4];
1430 if( csrreg->key == 0x81 || csrreg->key == 0xd1){
1431 csrd = SLIST_FIRST(&fc->csrfree);
1435 csrd->ongoaddr = fc->ongoaddr;
1436 fc->ongoaddr += csrreg->val * 4;
1437 csrd->off = fc->ongoaddr;
1438 SLIST_REMOVE_HEAD(&fc->csrfree, link);
1439 SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1445 if(((fc->ongoaddr - offset)/4 > chdr->crc_len) &&
1446 (fc->ongodev->rommax < 0x414)){
1447 if(fc->ongodev->rommax <= 0x414){
1448 csrd = SLIST_FIRST(&fc->csrfree);
1449 if(csrd == NULL) goto nextnode;
1450 csrd->off = fc->ongoaddr;
1451 csrd->ongoaddr = fc->ongoaddr;
1452 SLIST_REMOVE_HEAD(&fc->csrfree, link);
1453 SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link);
1458 while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){
1462 fc->ongoaddr = csrd->ongoaddr + 4;
1463 SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1464 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1465 csrd = SLIST_FIRST(&fc->ongocsr);
1466 if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){
1467 chdr = (struct csrhdr *)(fc->ongodev->csrrom);
1470 chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]);
1474 if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){
1479 fw_xfer_free( xfer);
1484 if (fc->ongodev != NULL)
1485 fc->ongodev->status = FWDEVINVAL;
1487 fw_xfer_free( xfer);
1489 /* housekeeping work space */
1490 fc->ongoaddr = CSRROMOFF;
1492 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff;
1493 while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){
1494 SLIST_REMOVE_HEAD(&fc->ongocsr, link);
1495 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link);
1502 * To attach sub-devices layer onto IEEE1394 bus.
1505 fw_attach_dev(struct firewire_comm *fc)
1507 struct fw_device *fwdev;
1508 struct fw_xfer *xfer;
1512 struct firewire_dev_comm *fdc;
1514 STAILQ_FOREACH(fwdev, &fc->devices, link)
1515 if (fwdev->status == FWDEVINIT)
1516 fwdev->status = FWDEVATTACHED;
1518 err = device_get_children(fc->bdev, &devlistp, &devcnt);
1521 for( i = 0 ; i < devcnt ; i++){
1522 if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
1523 fdc = device_get_softc(devlistp[i]);
1524 if (fdc->post_explore != NULL)
1525 fdc->post_explore(fdc);
1528 free(devlistp, M_TEMP);
1530 /* call pending handlers */
1532 while ((xfer = STAILQ_FIRST(&fc->pending))) {
1533 STAILQ_REMOVE_HEAD(&fc->pending, link);
1536 xfer->act.hand(xfer);
1539 printf("fw_attach_dev: %d pending handlers called\n", i);
1540 if (fc->retry_count > 0) {
1541 printf("probe failed for %d node\n", fc->retry_count);
1543 callout_reset(&fc->retry_probe_callout, hz*2,
1544 (void *)fc->ibr, (void *)fc);
1551 * To allocate uniq transaction label.
1554 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1557 struct tlabel *tl, *tmptl;
1559 static u_int32_t label = 0;
1562 for( i = 0 ; i < 0x40 ; i ++){
1563 label = (label + 1) & 0x3f;
1564 for(tmptl = STAILQ_FIRST(&fc->tlabels[label]);
1565 tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){
1566 if(tmptl->xfer->dst == xfer->dst) break;
1569 tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT);
1575 STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link);
1577 if (firewire_debug > 1)
1578 printf("fw_get_tlabel: dst=%d tl=%d\n",
1585 printf("fw_get_tlabel: no free tlabel\n");
1590 fw_rcv_copy(struct fw_xfer *xfer, struct iovec *vec, int nvec)
1596 res = xfer->recv.len;
1597 for (i = 0; i < nvec; i++, vec++) {
1600 printf("rcv buffer(%d) is %d bytes short.\n",
1601 xfer->recv.len, len - res);
1604 bcopy(vec->iov_base, p, len);
1610 xfer->recv.len -= res;
1614 * Generic packet receving process.
1617 fw_rcv(struct firewire_comm *fc, struct iovec *vec, int nvec, u_int sub, u_int spd)
1619 struct fw_pkt *fp, *resfp;
1620 struct fw_xfer *xfer;
1621 struct fw_bind *bind;
1622 struct firewire_softc *sc;
1624 int i, len, oldstate;
1629 qld = (u_int32_t *)buf;
1630 printf("spd %d len:%d\n", spd, len);
1631 for( i = 0 ; i <= len && i < 32; i+= 4){
1632 printf("0x%08x ", ntohl(qld[i/4]));
1633 if((i % 16) == 15) printf("\n");
1635 if((i % 16) != 15) printf("\n");
1638 fp = (struct fw_pkt *)vec[0].iov_base;
1639 tcode = fp->mode.common.tcode;
1640 #if 0 /* XXX this check is not valid for RRESQ and WREQQ */
1641 if (vec[0].iov_len < fc->tcode[tcode].hdr_len) {
1642 #if __FreeBSD_version >= 500000
1643 printf("fw_rcv: iov_len(%zu) is less than"
1645 printf("fw_rcv: iov_len(%u) is less than"
1647 " hdr_len(%d:tcode=%d)\n", vec[0].iov_len,
1648 fc->tcode[tcode].hdr_len, tcode);
1656 xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1657 fp->mode.hdr.tlrt >> 2);
1659 printf("fw_rcv: unknown response "
1660 "tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n",
1663 fp->mode.hdr.tlrt >> 2,
1664 fp->mode.hdr.tlrt & 3,
1665 fp->mode.rresq.data);
1667 printf("try ad-hoc work around!!\n");
1668 xfer = fw_tl2xfer(fc, fp->mode.hdr.src,
1669 (fp->mode.hdr.tlrt >> 2)^3);
1671 printf("no use...\n");
1678 fw_rcv_copy(xfer, vec, nvec);
1680 /* make sure the packet is drained in AT queue */
1681 oldstate = xfer->state;
1682 xfer->state = FWXF_RCVD;
1689 printf("not sent yet\n");
1692 printf("unexpected state %d\n", xfer->state);
1700 bind = fw_bindlookup(fc, fp->mode.rreqq.dest_hi,
1701 fp->mode.rreqq.dest_lo);
1703 #if __FreeBSD_version >= 500000
1704 printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%x\n",
1706 printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%lx\n",
1708 fp->mode.wreqq.dest_hi,
1709 fp->mode.wreqq.dest_lo,
1712 ntohl(fp->mode.wreqq.data));
1713 if (fc->status == FWBUSRESET) {
1714 printf("fw_rcv: cannot respond(bus reset)!\n");
1717 xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 0);
1722 resfp = (struct fw_pkt *)xfer->send.buf;
1726 resfp->mode.hdr.tcode = FWTCODE_WRES;
1727 xfer->send.len = 12;
1730 resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1731 xfer->send.len = 16;
1734 resfp->mode.hdr.tcode = FWTCODE_RRESB;
1735 xfer->send.len = 16;
1738 resfp->mode.hdr.tcode = FWTCODE_LRES;
1739 xfer->send.len = 16;
1742 resfp->mode.hdr.dst = fp->mode.hdr.src;
1743 resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1744 resfp->mode.hdr.pri = fp->mode.hdr.pri;
1745 resfp->mode.rresb.rtcode = 7;
1746 resfp->mode.rresb.extcode = 0;
1747 resfp->mode.rresb.len = 0;
1749 xfer->act.hand = fw_asy_callback;
1751 xfer->act.hand = fw_xfer_free;
1752 if(fw_asyreq(fc, -1, xfer)){
1753 fw_xfer_free( xfer);
1759 for (i = 0; i < nvec; i ++)
1760 len += vec[i].iov_len;
1761 switch(bind->act_type){
1764 xfer = STAILQ_FIRST(&bind->xferlist);
1766 printf("Discard a packet for this bind.\n");
1769 STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1770 fw_rcv_copy(xfer, vec, nvec);
1772 if (fc->status != FWBUSRESET)
1773 xfer->act.hand(xfer);
1775 STAILQ_INSERT_TAIL(&fc->pending, xfer, link);
1779 if(fc->ir[bind->sub]->queued >=
1780 fc->ir[bind->sub]->maxq){
1781 device_printf(fc->bdev,
1782 "Discard a packet %x %d\n",
1784 fc->ir[bind->sub]->queued);
1787 xfer = STAILQ_FIRST(&bind->xferlist);
1789 printf("Discard packet for this bind\n");
1792 STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1793 fw_rcv_copy(xfer, vec, nvec);
1796 fc->ir[bind->sub]->queued++;
1797 STAILQ_INSERT_TAIL(&fc->ir[bind->sub]->q, xfer, link);
1800 wakeup((caddr_t)fc->ir[bind->sub]);
1809 case FWTCODE_STREAM:
1811 struct fw_xferq *xferq;
1813 xferq = fc->ir[sub];
1815 printf("stream rcv dma %d len %d off %d spd %d\n",
1816 sub, len, off, spd);
1818 if(xferq->queued >= xferq->maxq) {
1819 printf("receive queue is full\n");
1822 /* XXX get xfer from xfer queue, we don't need copy for
1824 xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1826 if(xfer == NULL) goto err;
1827 fw_rcv_copy(xfer, vec, nvec);
1831 STAILQ_INSERT_TAIL(&xferq->q, xfer, link);
1833 sc = device_get_softc(fc->bdev);
1834 #if __FreeBSD_version >= 500000
1835 if (SEL_WAITING(&xferq->rsel))
1837 if (&xferq->rsel.si_pid != 0)
1839 selwakeup(&xferq->rsel);
1840 if (xferq->flag & FWXFERQ_WAKEUP) {
1841 xferq->flag &= ~FWXFERQ_WAKEUP;
1842 wakeup((caddr_t)xferq);
1844 if (xferq->flag & FWXFERQ_HANDLER) {
1851 printf("fw_rcv: unknow tcode %d\n", tcode);
1859 * Post process for Bus Manager election process.
1862 fw_try_bmr_callback(struct fw_xfer *xfer)
1865 struct firewire_comm *fc;
1871 if (xfer->resp != 0)
1873 if (xfer->send.buf == NULL)
1875 if (xfer->recv.buf == NULL)
1877 rfp = (struct fw_pkt *)xfer->recv.buf;
1878 if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE)
1881 bmr = ntohl(rfp->mode.lres.payload[0]);
1885 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1891 device_printf(fc->bdev, "bus manager election failed\n");
1897 * To candidate Bus Manager election process.
1900 fw_try_bmr(void *arg)
1902 struct fw_xfer *xfer;
1903 struct firewire_comm *fc = (struct firewire_comm *)arg;
1907 xfer = fw_xfer_alloc_buf(M_FWXFER, 24, 20);
1912 fc->status = FWBUSMGRELECT;
1914 fp = (struct fw_pkt *)xfer->send.buf;
1915 fp->mode.lreq.dest_hi = 0xffff;
1916 fp->mode.lreq.tlrt = 0;
1917 fp->mode.lreq.tcode = FWTCODE_LREQ;
1918 fp->mode.lreq.pri = 0;
1919 fp->mode.lreq.src = 0;
1920 fp->mode.lreq.len = 8;
1921 fp->mode.lreq.extcode = FW_LREQ_CMPSWAP;
1922 xfer->dst = FWLOCALBUS | fc->irm;
1923 fp->mode.lreq.dst = xfer->dst;
1924 fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
1925 fp->mode.lreq.payload[0] = htonl(0x3f);
1926 fp->mode.lreq.payload[1] = htonl(fc->nodeid);
1927 xfer->act.hand = fw_try_bmr_callback;
1929 err = fw_asyreq(fc, -1, xfer);
1931 fw_xfer_free( xfer);
1939 * Software implementation for physical memory block access.
1940 * XXX:Too slow, usef for debug purpose only.
1943 fw_vmaccess(struct fw_xfer *xfer){
1944 struct fw_pkt *rfp, *sfp = NULL;
1945 u_int32_t *ld = (u_int32_t *)xfer->recv.buf;
1947 printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
1948 xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1949 printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1950 if(xfer->resp != 0){
1951 fw_xfer_free( xfer);
1954 if(xfer->recv.buf == NULL){
1955 fw_xfer_free( xfer);
1958 rfp = (struct fw_pkt *)xfer->recv.buf;
1959 switch(rfp->mode.hdr.tcode){
1960 /* XXX need fix for 64bit arch */
1962 xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
1963 xfer->send.len = 12;
1964 sfp = (struct fw_pkt *)xfer->send.buf;
1965 bcopy(rfp->mode.wreqb.payload,
1966 (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
1967 sfp->mode.wres.tcode = FWTCODE_WRES;
1968 sfp->mode.wres.rtcode = 0;
1971 xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
1972 xfer->send.len = 12;
1973 sfp->mode.wres.tcode = FWTCODE_WRES;
1974 *((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
1975 sfp->mode.wres.rtcode = 0;
1978 xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
1979 xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
1980 sfp = (struct fw_pkt *)xfer->send.buf;
1981 bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
1982 sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len));
1983 sfp->mode.rresb.tcode = FWTCODE_RRESB;
1984 sfp->mode.rresb.len = rfp->mode.rreqb.len;
1985 sfp->mode.rresb.rtcode = 0;
1986 sfp->mode.rresb.extcode = 0;
1989 xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
1990 xfer->send.len = 16;
1991 sfp = (struct fw_pkt *)xfer->send.buf;
1992 sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
1993 sfp->mode.wres.tcode = FWTCODE_RRESQ;
1994 sfp->mode.rresb.rtcode = 0;
1997 fw_xfer_free( xfer);
2000 sfp->mode.hdr.dst = rfp->mode.hdr.src;
2001 xfer->dst = ntohs(rfp->mode.hdr.src);
2002 xfer->act.hand = fw_xfer_free;
2003 xfer->retry_req = fw_asybusy;
2005 sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2006 sfp->mode.hdr.pri = 0;
2008 fw_asyreq(xfer->fc, -1, xfer);
2015 * CRC16 check-sum for IEEE1394 register blocks.
2018 fw_crc16(u_int32_t *ptr, u_int32_t len){
2019 u_int32_t i, sum, crc = 0;
2021 len = (len + 3) & ~3;
2022 for(i = 0 ; i < len ; i+= 4){
2023 for( shift = 28 ; shift >= 0 ; shift -= 4){
2024 sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2025 crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2029 return((u_int16_t) crc);
2033 fw_bmr(struct firewire_comm *fc)
2035 struct fw_device fwdev;
2036 union fw_self_id *self_id;
2039 /* Check to see if the current root node is cycle master capable */
2040 self_id = &fc->topology_map->self_id[fc->max_node];
2041 if (fc->max_node > 0) {
2042 /* XXX check cmc bit of businfo block rather than contender */
2043 if (self_id->p0.link_active && self_id->p0.contender)
2044 cmstr = fc->max_node;
2046 device_printf(fc->bdev,
2047 "root node is not cycle master capable\n");
2048 /* XXX shall we be the cycle master? */
2050 /* XXX need bus reset */
2055 device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2056 if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2057 /* We are not the bus manager */
2063 /* Optimize gapcount */
2064 if(fc->max_hop <= MAX_GAPHOP )
2065 fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2066 /* If we are the cycle master, nothing to do */
2067 if (cmstr == fc->nodeid || cmstr == -1)
2069 /* Bus probe has not finished, make dummy fwdev for cmstr */
2070 bzero(&fwdev, sizeof(fwdev));
2074 fwdev.maxrec = 8; /* 512 */
2075 fwdev.status = FWDEVINIT;
2076 /* Set cmstr bit on the cycle master */
2077 fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2078 0xffff, 0xf0000000 | STATE_SET, htonl(1 << 8),
2079 fw_asy_callback_free);
2084 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0);
2085 MODULE_VERSION(firewire, 1);