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.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/kthread.h>
49 #if defined(__DragonFly__) || __FreeBSD_version < 500000
50 #include <machine/clock.h> /* for DELAY() */
53 #include <sys/bus.h> /* used by smbus and newbus */
54 #include <machine/bus.h>
58 #include "firewirereg.h"
63 #include <dev/firewire/firewire.h>
64 #include <dev/firewire/firewirereg.h>
65 #include <dev/firewire/fwmem.h>
66 #include <dev/firewire/iec13213.h>
67 #include <dev/firewire/iec68113.h>
72 struct crom_chunk root;
73 struct crom_chunk vendor;
77 int firewire_debug=0, try_bmr=1, hold_count=3;
78 SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
79 "FireWire driver debug flag");
80 SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
81 SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
82 "Try to be a bus manager");
83 SYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
84 "Number of count of bus resets for removing lost device information");
86 MALLOC_DEFINE(M_FW, "firewire", "FireWire");
87 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
89 #define FW_MAXASYRTY 4
91 devclass_t firewire_devclass;
93 static void firewire_identify (driver_t *, device_t);
94 static int firewire_probe (device_t);
95 static int firewire_attach (device_t);
96 static int firewire_detach (device_t);
97 static int firewire_resume (device_t);
99 static int firewire_shutdown (device_t);
101 static device_t firewire_add_child (device_t, int, const char *, int);
102 static void fw_try_bmr (void *);
103 static void fw_try_bmr_callback (struct fw_xfer *);
104 static void fw_asystart (struct fw_xfer *);
105 static int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
106 static void fw_bus_probe (struct firewire_comm *);
107 static void fw_attach_dev (struct firewire_comm *);
108 static void fw_bus_probe_thread(void *);
110 static void fw_vmaccess (struct fw_xfer *);
112 static int fw_bmr (struct firewire_comm *);
114 static device_method_t firewire_methods[] = {
115 /* Device interface */
116 DEVMETHOD(device_identify, firewire_identify),
117 DEVMETHOD(device_probe, firewire_probe),
118 DEVMETHOD(device_attach, firewire_attach),
119 DEVMETHOD(device_detach, firewire_detach),
120 DEVMETHOD(device_suspend, bus_generic_suspend),
121 DEVMETHOD(device_resume, firewire_resume),
122 DEVMETHOD(device_shutdown, bus_generic_shutdown),
125 DEVMETHOD(bus_add_child, firewire_add_child),
126 DEVMETHOD(bus_print_child, bus_generic_print_child),
130 char *linkspeed[] = {
131 "S100", "S200", "S400", "S800",
132 "S1600", "S3200", "undef", "undef"
135 static char *tcode_str[] = {
136 "WREQQ", "WREQB", "WRES", "undef",
137 "RREQQ", "RREQB", "RRESQ", "RRESB",
138 "CYCS", "LREQ", "STREAM", "LRES",
139 "undef", "undef", "PHY", "undef"
142 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
143 #define MAX_GAPHOP 15
144 u_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18,
145 21, 24, 26, 29, 32, 35, 37, 40};
147 static driver_t firewire_driver = {
150 sizeof(struct firewire_softc),
154 * Lookup fwdev by node id.
157 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
159 struct fw_device *fwdev;
163 STAILQ_FOREACH(fwdev, &fc->devices, link)
164 if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
172 * Lookup fwdev by EUI64.
175 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
177 struct fw_device *fwdev;
181 STAILQ_FOREACH(fwdev, &fc->devices, link)
182 if (FW_EUI64_EQUAL(fwdev->eui, *eui))
186 if(fwdev == NULL) return NULL;
187 if(fwdev->status == FWDEVINVAL) return NULL;
192 * Async. request procedure for userland application.
195 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
198 struct fw_xferq *xferq;
202 struct tcode_info *info;
204 if(xfer == NULL) return EINVAL;
205 if(xfer->hand == NULL){
206 printf("hand == NULL\n");
209 fp = &xfer->send.hdr;
211 tcode = fp->mode.common.tcode & 0xf;
212 info = &fc->tcode[tcode];
213 if (info->flag == 0) {
214 printf("invalid tcode=%x\n", tcode);
217 if (info->flag & FWTI_REQ)
222 if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
223 printf("send.pay_len > maxrec\n");
226 if (info->flag & FWTI_BLOCK_STR)
227 len = fp->mode.stream.len;
228 else if (info->flag & FWTI_BLOCK_ASY)
229 len = fp->mode.rresb.len;
232 if (len != xfer->send.pay_len){
233 printf("len(%d) != send.pay_len(%d) %s(%x)\n",
234 len, xfer->send.pay_len, tcode_str[tcode], tcode);
238 if(xferq->start == NULL){
239 printf("xferq->start == NULL\n");
242 if(!(xferq->queued < xferq->maxq)){
243 device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
248 if (info->flag & FWTI_TLABEL) {
249 if ((tl = fw_get_tlabel(fc, xfer)) == -1)
251 fp->mode.hdr.tlrt = tl << 2;
263 * Wakeup blocked process.
266 fw_asy_callback(struct fw_xfer *xfer){
272 * Async. request with given xfer structure.
275 fw_asystart(struct fw_xfer *xfer)
277 struct firewire_comm *fc = xfer->fc;
279 #if 0 /* XXX allow bus explore packets only after bus rest */
280 if (fc->status < FWBUSEXPLORE) {
282 xfer->state = FWXF_BUSY;
283 if (xfer->hand != NULL)
288 microtime(&xfer->tv);
290 xfer->state = FWXF_INQ;
291 STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
294 /* XXX just queue for mbuf */
295 if (xfer->mbuf == NULL)
301 firewire_identify(driver_t *driver, device_t parent)
303 BUS_ADD_CHILD(parent, 0, "firewire", -1);
307 firewire_probe(device_t dev)
309 device_set_desc(dev, "IEEE1394(FireWire) bus");
314 firewire_xfer_timeout(struct firewire_comm *fc)
316 struct fw_xfer *xfer;
318 struct timeval split_timeout;
321 split_timeout.tv_sec = 0;
322 split_timeout.tv_usec = 200 * 1000; /* 200 msec */
325 timevalsub(&tv, &split_timeout);
328 for (i = 0; i < 0x40; i ++) {
329 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
330 if (timevalcmp(&xfer->tv, &tv, >))
331 /* the rests are newer than this */
333 if (xfer->state == FWXF_START)
336 device_printf(fc->bdev,
337 "split transaction timeout dst=0x%x tl=0x%x state=%d\n",
338 xfer->send.hdr.mode.hdr.dst, i, xfer->state);
339 xfer->resp = ETIMEDOUT;
346 #define WATCHDOC_HZ 10
348 firewire_watchdog(void *arg)
350 struct firewire_comm *fc;
351 static int watchdoc_clock = 0;
353 fc = (struct firewire_comm *)arg;
356 * At boot stage, the device interrupt is disabled and
357 * We encounter a timeout easily. To avoid this,
358 * ignore clock interrupt for a while.
360 if (watchdoc_clock > WATCHDOC_HZ * 15) {
361 firewire_xfer_timeout(fc);
366 callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ,
367 (void *)firewire_watchdog, (void *)fc);
371 * The attach routine.
374 firewire_attach(device_t dev)
377 struct firewire_softc *sc = device_get_softc(dev);
378 device_t pa = device_get_parent(dev);
379 struct firewire_comm *fc;
382 fc = (struct firewire_comm *)device_get_softc(pa);
384 fc->status = FWBUSNOTREADY;
386 unit = device_get_unit(dev);
387 if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
391 CALLOUT_INIT(&sc->fc->timeout_callout);
392 CALLOUT_INIT(&sc->fc->bmr_callout);
393 CALLOUT_INIT(&sc->fc->busprobe_callout);
395 callout_reset(&sc->fc->timeout_callout, hz,
396 (void *)firewire_watchdog, (void *)sc->fc);
399 kthread_create(fw_bus_probe_thread, (void *)fc, &p,
400 0, 0, "fw%d_probe", unit);
402 /* Locate our children */
403 bus_generic_probe(dev);
405 /* launch attachement of the added children */
406 bus_generic_attach(dev);
409 fw_busreset(fc, FWBUSNOTREADY);
416 * Attach it as child.
419 firewire_add_child(device_t dev, int order, const char *name, int unit)
422 struct firewire_softc *sc;
424 sc = (struct firewire_softc *)device_get_softc(dev);
425 child = device_add_child(dev, name, unit);
427 device_set_ivars(child, sc->fc);
428 device_probe_and_attach(child);
435 firewire_resume(device_t dev)
437 struct firewire_softc *sc;
439 sc = (struct firewire_softc *)device_get_softc(dev);
440 sc->fc->status = FWBUSNOTREADY;
442 bus_generic_resume(dev);
451 firewire_detach(device_t dev)
453 struct firewire_softc *sc;
454 struct firewire_comm *fc;
455 struct fw_device *fwdev, *fwdev_next;
458 sc = (struct firewire_softc *)device_get_softc(dev);
460 fc->status = FWBUSDETACH;
462 if ((err = fwdev_destroydev(sc)) != 0)
465 if ((err = bus_generic_detach(dev)) != 0)
468 callout_stop(&fc->timeout_callout);
469 callout_stop(&fc->bmr_callout);
470 callout_stop(&fc->busprobe_callout);
472 /* XXX xfree_free and untimeout on all xfers */
473 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
474 fwdev = fwdev_next) {
475 fwdev_next = STAILQ_NEXT(fwdev, link);
478 free(fc->topology_map, M_FW);
479 free(fc->speed_map, M_FW);
480 free(fc->crom_src_buf, M_FW);
483 if (tsleep(fc, PWAIT, "fwthr", hz * 60))
484 printf("firewire task thread didn't die\n");
490 firewire_shutdown( device_t dev )
498 fw_xferq_drain(struct fw_xferq *xferq)
500 struct fw_xfer *xfer;
502 while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
503 STAILQ_REMOVE_HEAD(&xferq->q, link);
506 xfer->state = FWXF_SENTERR;
512 fw_drain_txq(struct firewire_comm *fc)
516 fw_xferq_drain(fc->atq);
517 fw_xferq_drain(fc->ats);
518 for(i = 0; i < fc->nisodma; i++)
519 fw_xferq_drain(fc->it[i]);
523 fw_reset_csr(struct firewire_comm *fc)
527 CSRARC(fc, STATE_CLEAR)
528 = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
529 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
530 CSRARC(fc, NODE_IDS) = 0x3f;
532 CSRARC(fc, TOPO_MAP + 8) = 0;
537 for(i = 2; i < 0x100/4 - 2 ; i++){
538 CSRARC(fc, SPED_MAP + i * 4) = 0;
540 CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
541 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
542 CSRARC(fc, RESET_START) = 0;
543 CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
544 CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
545 CSRARC(fc, CYCLE_TIME) = 0x0;
546 CSRARC(fc, BUS_TIME) = 0x0;
547 CSRARC(fc, BUS_MGR_ID) = 0x3f;
548 CSRARC(fc, BANDWIDTH_AV) = 4915;
549 CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
550 CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
551 CSRARC(fc, IP_CHANNELS) = (1 << 31);
553 CSRARC(fc, CONF_ROM) = 0x04 << 24;
554 CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
555 CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
556 1 << 28 | 0xff << 16 | 0x09 << 8;
557 CSRARC(fc, CONF_ROM + 0xc) = 0;
559 /* DV depend CSRs see blue book */
560 CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
561 CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
563 CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
564 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
568 fw_init_crom(struct firewire_comm *fc)
570 struct crom_src *src;
572 fc->crom_src_buf = (struct crom_src_buf *)
573 malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
574 if (fc->crom_src_buf == NULL)
577 src = &fc->crom_src_buf->src;
578 bzero(src, sizeof(struct crom_src));
580 /* BUS info sample */
581 src->hdr.info_len = 4;
583 src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
585 src->businfo.irmc = 1;
586 src->businfo.cmc = 1;
587 src->businfo.isc = 1;
588 src->businfo.bmc = 1;
589 src->businfo.pmc = 0;
590 src->businfo.cyc_clk_acc = 100;
591 src->businfo.max_rec = fc->maxrec;
592 src->businfo.max_rom = MAXROM_4;
593 src->businfo.generation = 1;
594 src->businfo.link_spd = fc->speed;
596 src->businfo.eui64.hi = fc->eui.hi;
597 src->businfo.eui64.lo = fc->eui.lo;
599 STAILQ_INIT(&src->chunk_list);
602 fc->crom_root = &fc->crom_src_buf->root;
606 fw_reset_crom(struct firewire_comm *fc)
608 struct crom_src_buf *buf;
609 struct crom_src *src;
610 struct crom_chunk *root;
612 if (fc->crom_src_buf == NULL)
615 buf = fc->crom_src_buf;
617 root = fc->crom_root;
619 STAILQ_INIT(&src->chunk_list);
621 bzero(root, sizeof(struct crom_chunk));
622 crom_add_chunk(src, NULL, root, 0);
623 crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
624 /* private company_id */
625 crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
627 crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
628 crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
630 crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
631 crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
633 crom_add_simple_text(src, root, &buf->hw, hostname);
637 * Called after bus reset.
640 fw_busreset(struct firewire_comm *fc, uint32_t new_status)
642 struct firewire_dev_comm *fdc;
643 struct crom_src *src;
650 callout_stop(&fc->bmr_callout);
655 fc->status = new_status;
659 if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
660 for( i = 0 ; i < devcnt ; i++)
661 if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
662 fdc = device_get_softc(devlistp[i]);
663 if (fdc->post_busreset != NULL)
664 fdc->post_busreset(fdc);
666 free(devlistp, M_TEMP);
669 newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
670 src = &fc->crom_src_buf->src;
671 crom_load(src, (uint32_t *)newrom, CROMSIZE);
672 if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
673 /* bump generation and reload */
674 src->businfo.generation ++;
675 /* generation must be between 0x2 and 0xF */
676 if (src->businfo.generation < 2)
677 src->businfo.generation ++;
678 crom_load(src, (uint32_t *)newrom, CROMSIZE);
679 bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
684 /* Call once after reboot */
685 void fw_init(struct firewire_comm *fc)
689 struct fw_xfer *xfer;
708 STAILQ_INIT(&fc->atq->q);
709 STAILQ_INIT(&fc->ats->q);
711 for( i = 0 ; i < fc->nisodma ; i ++ ){
712 fc->it[i]->queued = 0;
713 fc->ir[i]->queued = 0;
715 fc->it[i]->start = NULL;
716 fc->ir[i]->start = NULL;
718 fc->it[i]->buf = NULL;
719 fc->ir[i]->buf = NULL;
721 fc->it[i]->flag = FWXFERQ_STREAM;
722 fc->ir[i]->flag = FWXFERQ_STREAM;
724 STAILQ_INIT(&fc->it[i]->q);
725 STAILQ_INIT(&fc->ir[i]->q);
728 fc->arq->maxq = FWMAXQUEUE;
729 fc->ars->maxq = FWMAXQUEUE;
730 fc->atq->maxq = FWMAXQUEUE;
731 fc->ats->maxq = FWMAXQUEUE;
733 for( i = 0 ; i < fc->nisodma ; i++){
734 fc->ir[i]->maxq = FWMAXQUEUE;
735 fc->it[i]->maxq = FWMAXQUEUE;
737 /* Initialize csr registers */
738 fc->topology_map = (struct fw_topology_map *)malloc(
739 sizeof(struct fw_topology_map),
740 M_FW, M_NOWAIT | M_ZERO);
741 fc->speed_map = (struct fw_speed_map *)malloc(
742 sizeof(struct fw_speed_map),
743 M_FW, M_NOWAIT | M_ZERO);
744 CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
745 CSRARC(fc, TOPO_MAP + 4) = 1;
746 CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
747 CSRARC(fc, SPED_MAP + 4) = 1;
749 STAILQ_INIT(&fc->devices);
751 /* Initialize Async handlers */
752 STAILQ_INIT(&fc->binds);
753 for( i = 0 ; i < 0x40 ; i++){
754 STAILQ_INIT(&fc->tlabels[i]);
757 /* DV depend CSRs see blue book */
759 CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
760 CSRARC(fc, oPCR) = 0x8000007a;
761 for(i = 4 ; i < 0x7c/4 ; i+=4){
762 CSRARC(fc, i + oPCR) = 0x8000007a;
765 CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
766 CSRARC(fc, iPCR) = 0x803f0000;
767 for(i = 4 ; i < 0x7c/4 ; i+=4){
768 CSRARC(fc, i + iPCR) = 0x0;
772 fc->crom_src_buf = NULL;
775 xfer = fw_xfer_alloc();
776 if(xfer == NULL) return;
778 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
783 xfer->hand = fw_vmaccess;
789 fwb->addrlen = 0xffffffff;
795 #define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
796 ((fwb)->end < (addr))?1:0)
799 * To lookup bound process from IEEE1394 address.
802 fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
807 addr = ((u_int64_t)dest_hi << 32) | dest_lo;
808 STAILQ_FOREACH(tfw, &fc->binds, fclist)
809 if (BIND_CMP(addr, tfw) == 0)
815 * To bind IEEE1394 address block to process.
818 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
820 struct fw_bind *tfw, *prev = NULL;
822 if (fwb->start > fwb->end) {
823 printf("%s: invalid range\n", __func__);
827 STAILQ_FOREACH(tfw, &fc->binds, fclist) {
828 if (fwb->end < tfw->start)
833 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
836 if (prev->end < fwb->start) {
837 STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
841 printf("%s: bind failed\n", __func__);
846 * To free IEEE1394 address block.
849 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
852 struct fw_xfer *xfer, *next;
858 STAILQ_FOREACH(tfw, &fc->binds, fclist)
860 STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
864 printf("%s: no such binding\n", __func__);
869 /* shall we do this? */
870 for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
871 next = STAILQ_NEXT(xfer, link);
874 STAILQ_INIT(&fwb->xferlist);
882 fw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
883 int slen, int rlen, int n,
884 struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
887 struct fw_xfer *xfer;
889 for (i = 0; i < n; i++) {
890 xfer = fw_xfer_alloc_buf(type, slen, rlen);
897 STAILQ_INSERT_TAIL(q, xfer, link);
904 fw_xferlist_remove(struct fw_xferlist *q)
906 struct fw_xfer *xfer, *next;
908 for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
909 next = STAILQ_NEXT(xfer, link);
910 fw_xfer_free_buf(xfer);
916 * To free transaction label.
919 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
921 struct fw_xfer *txfer;
928 #if 1 /* make sure the label is allocated */
929 STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
933 printf("%s: the xfer is not in the tlabel(%d)\n",
934 __FUNCTION__, xfer->tl);
940 STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
946 * To obtain XFER structure by transaction label.
948 static struct fw_xfer *
949 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel)
951 struct fw_xfer *xfer;
954 STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
955 if(xfer->send.hdr.mode.hdr.dst == node) {
957 if (firewire_debug > 2)
958 printf("fw_tl2xfer: found tl=%d\n", tlabel);
961 if (firewire_debug > 1)
962 printf("fw_tl2xfer: not found tl=%d\n", tlabel);
968 * To allocate IEEE1394 XFER structure.
971 fw_xfer_alloc(struct malloc_type *type)
973 struct fw_xfer *xfer;
975 xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
985 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
987 struct fw_xfer *xfer;
989 xfer = fw_xfer_alloc(type);
992 xfer->send.pay_len = send_len;
993 xfer->recv.pay_len = recv_len;
995 xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
996 if (xfer->send.payload == NULL) {
1002 xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1003 if (xfer->recv.payload == NULL) {
1004 if (xfer->send.payload != NULL)
1005 free(xfer->send.payload, type);
1014 * IEEE1394 XFER post process.
1017 fw_xfer_done(struct fw_xfer *xfer)
1019 if (xfer->hand == NULL) {
1020 printf("hand == NULL\n");
1024 if (xfer->fc == NULL)
1025 panic("fw_xfer_done: why xfer->fc is NULL?");
1027 fw_tl_free(xfer->fc, xfer);
1032 fw_xfer_unload(struct fw_xfer* xfer)
1036 if(xfer == NULL ) return;
1037 if(xfer->state == FWXF_INQ){
1038 printf("fw_xfer_free FWXF_INQ\n");
1040 STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1044 if (xfer->fc != NULL) {
1046 if(xfer->state == FWXF_START)
1048 * This could happen if:
1049 * 1. We call fwohci_arcv() before fwohci_txd().
1050 * 2. firewire_watch() is called.
1052 printf("fw_xfer_free FWXF_START\n");
1055 xfer->state = FWXF_INIT;
1059 * To free IEEE1394 XFER structure.
1062 fw_xfer_free_buf( struct fw_xfer* xfer)
1065 printf("%s: xfer == NULL\n", __func__);
1068 fw_xfer_unload(xfer);
1069 if(xfer->send.payload != NULL){
1070 free(xfer->send.payload, xfer->malloc);
1072 if(xfer->recv.payload != NULL){
1073 free(xfer->recv.payload, xfer->malloc);
1075 free(xfer, xfer->malloc);
1079 fw_xfer_free( struct fw_xfer* xfer)
1082 printf("%s: xfer == NULL\n", __func__);
1085 fw_xfer_unload(xfer);
1086 free(xfer, xfer->malloc);
1090 fw_asy_callback_free(struct fw_xfer *xfer)
1093 printf("asyreq done state=%d resp=%d\n",
1094 xfer->state, xfer->resp);
1103 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1105 struct fw_xfer *xfer;
1108 fc->status = FWBUSPHYCONF;
1110 xfer = fw_xfer_alloc(M_FWXFER);
1114 xfer->hand = fw_asy_callback_free;
1116 fp = &xfer->send.hdr;
1119 fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1121 fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1122 fp->mode.ld[2] = ~fp->mode.ld[1];
1123 /* XXX Dangerous, how to pass PHY packet to device driver */
1124 fp->mode.common.tcode |= FWTCODE_PHY;
1127 printf("send phy_config root_node=%d gap_count=%d\n",
1128 root_node, gap_count);
1129 fw_asyreq(fc, -1, xfer);
1137 fw_print_sid(uint32_t sid)
1139 union fw_self_id *s;
1140 s = (union fw_self_id *) &sid;
1141 printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1142 " p0:%d p1:%d p2:%d i:%d m:%d\n",
1143 s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1144 s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1145 s->p0.power_class, s->p0.port0, s->p0.port1,
1146 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1151 * To receive self ID.
1153 void fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1156 union fw_self_id *self_id;
1157 u_int i, j, node, c_port = 0, i_branch = 0;
1159 fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1160 fc->status = FWBUSINIT;
1161 fc->max_node = fc->nodeid & 0x3f;
1162 CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1163 fc->status = FWBUSCYMELECT;
1164 fc->topology_map->crc_len = 2;
1165 fc->topology_map->generation ++;
1166 fc->topology_map->self_id_count = 0;
1167 fc->topology_map->node_count = 0;
1168 fc->speed_map->generation ++;
1169 fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1170 self_id = &fc->topology_map->self_id[0];
1171 for(i = 0; i < fc->sid_cnt; i ++){
1172 if (sid[1] != ~sid[0]) {
1173 printf("fw_sidrcv: invalid self-id packet\n");
1177 *self_id = *((union fw_self_id *)sid);
1178 fc->topology_map->crc_len++;
1179 if(self_id->p0.sequel == 0){
1180 fc->topology_map->node_count ++;
1183 fw_print_sid(sid[0]);
1185 node = self_id->p0.phy_id;
1186 if(fc->max_node < node){
1187 fc->max_node = self_id->p0.phy_id;
1189 /* XXX I'm not sure this is the right speed_map */
1190 fc->speed_map->speed[node][node]
1191 = self_id->p0.phy_speed;
1192 for (j = 0; j < node; j ++) {
1193 fc->speed_map->speed[j][node]
1194 = fc->speed_map->speed[node][j]
1195 = min(fc->speed_map->speed[j][j],
1196 self_id->p0.phy_speed);
1198 if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1199 (self_id->p0.link_active && self_id->p0.contender)) {
1200 fc->irm = self_id->p0.phy_id;
1202 if(self_id->p0.port0 >= 0x2){
1205 if(self_id->p0.port1 >= 0x2){
1208 if(self_id->p0.port2 >= 0x2){
1213 i_branch += (c_port - 2);
1217 fc->topology_map->self_id_count ++;
1219 device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1221 fc->topology_map->crc = fw_crc16(
1222 (uint32_t *)&fc->topology_map->generation,
1223 fc->topology_map->crc_len * 4);
1224 fc->speed_map->crc = fw_crc16(
1225 (uint32_t *)&fc->speed_map->generation,
1226 fc->speed_map->crc_len * 4);
1227 /* byteswap and copy to CSR */
1228 p = (uint32_t *)fc->topology_map;
1229 for (i = 0; i <= fc->topology_map->crc_len; i++)
1230 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1231 p = (uint32_t *)fc->speed_map;
1232 CSRARC(fc, SPED_MAP) = htonl(*p++);
1233 CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1234 /* don't byte-swap uint8_t array */
1235 bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1237 fc->max_hop = fc->max_node - i_branch;
1238 printf(", maxhop <= %d", fc->max_hop);
1241 printf(", Not found IRM capable node");
1243 printf(", cable IRM = %d", fc->irm);
1244 if (fc->irm == fc->nodeid)
1249 if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1250 if (fc->irm == fc->nodeid) {
1251 fc->status = FWBUSMGRDONE;
1252 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1255 fc->status = FWBUSMGRELECT;
1256 callout_reset(&fc->bmr_callout, hz/8,
1257 (void *)fw_try_bmr, (void *)fc);
1260 fc->status = FWBUSMGRDONE;
1262 callout_reset(&fc->busprobe_callout, hz/4,
1263 (void *)fw_bus_probe, (void *)fc);
1267 * To probe devices on the IEEE1394 bus.
1270 fw_bus_probe(struct firewire_comm *fc)
1273 struct fw_device *fwdev;
1276 fc->status = FWBUSEXPLORE;
1278 /* Invalidate all devices, just after bus reset. */
1279 STAILQ_FOREACH(fwdev, &fc->devices, link)
1280 if (fwdev->status != FWDEVINVAL) {
1281 fwdev->status = FWDEVINVAL;
1290 fw_explore_read_quads(struct fw_device *fwdev, int offset,
1291 uint32_t *quad, int n)
1293 struct fw_xfer *xfer;
1297 for (i = 0; i < n; i ++, offset += sizeof(uint32_t)) {
1298 xfer = fwmem_read_quad(fwdev, NULL, -1,
1299 0xffff, 0xf0000000 | offset, (void *)&tmp,
1303 tsleep((void *)xfer, PWAIT|PCATCH, "rquad", 0);
1305 if (xfer->resp == 0)
1306 quad[i] = ntohl(tmp);
1318 fw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
1321 struct csrdirectory *dir;
1324 dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1325 err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1326 (uint32_t *)dir, 1);
1330 offset += sizeof(uint32_t);
1331 reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1332 err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1333 (uint32_t *)reg, dir->crc_len);
1339 off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
1340 if (fwdev->rommax < off)
1341 fwdev->rommax = off;
1346 for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) {
1347 if (reg[i].key == CROM_UDIR)
1349 else if (reg[i].key == CROM_TEXTLEAF)
1354 off = offset + reg[i].val * sizeof(uint32_t);
1355 if (off > CROMSIZE) {
1356 printf("%s: invalid offset %d\n", __FUNCTION__, off);
1359 err = fw_explore_csrblock(fwdev, off, recur);
1367 fw_explore_node(struct fw_device *dfwdev)
1369 struct firewire_comm *fc;
1370 struct fw_device *fwdev, *pfwdev, *tfwdev;
1373 struct bus_info *binfo;
1377 csr = dfwdev->csrrom;
1381 err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1);
1384 hdr = (struct csrhdr *)&csr[0];
1385 if (hdr->info_len != 4) {
1387 printf("node%d: wrong bus info len(%d)\n",
1388 node, hdr->info_len);
1393 err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
1396 binfo = (struct bus_info *)&csr[1];
1397 if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
1399 printf("node%d: invalid bus name 0x%08x\n",
1400 node, binfo->bus_name);
1403 spd = fc->speed_map->speed[fc->nodeid][node];
1404 STAILQ_FOREACH(fwdev, &fc->devices, link)
1405 if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
1407 if (fwdev == NULL) {
1409 fwdev = malloc(sizeof(struct fw_device), M_FW,
1411 if (fwdev == NULL) {
1413 printf("node%d: no memory\n", node);
1417 fwdev->eui = binfo->eui64;
1418 /* inesrt into sorted fwdev list */
1420 STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1421 if (tfwdev->eui.hi > fwdev->eui.hi ||
1422 (tfwdev->eui.hi == fwdev->eui.hi &&
1423 tfwdev->eui.lo > fwdev->eui.lo))
1428 STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1430 STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1432 device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1434 fwdev->eui.hi, fwdev->eui.lo);
1437 fwdev->status = FWDEVINIT;
1441 if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) {
1443 printf("node%d: crom unchanged\n", node);
1447 bzero(&fwdev->csrrom[0], CROMSIZE);
1449 /* copy first quad and bus info block */
1450 bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5);
1451 fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
1453 err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
1456 fwdev->status = FWDEVINVAL;
1457 fwdev->csrrom[0] = 0;
1464 * Find the self_id packet for a node, ignoring sequels.
1466 static union fw_self_id *
1467 fw_find_self_id(struct firewire_comm *fc, int node)
1470 union fw_self_id *s;
1472 for (i = 0; i < fc->topology_map->self_id_count; i++) {
1473 s = &fc->topology_map->self_id[i];
1476 if (s->p0.phy_id == node)
1483 fw_explore(struct firewire_comm *fc)
1485 int node, err, s, i, todo, todo2, trys;
1487 struct fw_device dfwdev;
1488 union fw_self_id *fwsid;
1491 /* setup dummy fwdev */
1494 dfwdev.maxrec = 8; /* 512 */
1495 dfwdev.status = FWDEVINIT;
1497 for (node = 0; node <= fc->max_node; node ++) {
1498 /* We don't probe myself and linkdown nodes */
1499 if (node == fc->nodeid)
1501 fwsid = fw_find_self_id(fc, node);
1502 if (!fwsid || !fwsid->p0.link_active) {
1504 printf("node%d: link down\n", node);
1507 nodes[todo++] = node;
1511 for (trys = 0; todo > 0 && trys < 3; trys ++) {
1513 for (i = 0; i < todo; i ++) {
1514 dfwdev.dst = nodes[i];
1515 err = fw_explore_node(&dfwdev);
1517 nodes[todo2++] = nodes[i];
1519 printf("%s: node %d, err = %d\n",
1520 __FUNCTION__, node, err);
1529 fw_bus_probe_thread(void *arg)
1531 struct firewire_comm *fc;
1533 fc = (struct firewire_comm *)arg;
1537 if (fc->status == FWBUSEXPLORE) {
1539 fc->status = FWBUSEXPDONE;
1541 printf("bus_explore done\n");
1543 } else if (fc->status == FWBUSDETACH)
1545 tsleep((void *)fc, PWAIT|PCATCH, "-", 0);
1553 * To attach sub-devices layer onto IEEE1394 bus.
1556 fw_attach_dev(struct firewire_comm *fc)
1558 struct fw_device *fwdev, *next;
1562 struct firewire_dev_comm *fdc;
1564 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1565 next = STAILQ_NEXT(fwdev, link);
1566 if (fwdev->status == FWDEVINIT) {
1567 fwdev->status = FWDEVATTACHED;
1568 } else if (fwdev->status == FWDEVINVAL) {
1570 if (fwdev->rcnt > hold_count) {
1572 * Remove devices which have not been seen
1575 STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1582 err = device_get_children(fc->bdev, &devlistp, &devcnt);
1585 for( i = 0 ; i < devcnt ; i++){
1586 if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
1587 fdc = device_get_softc(devlistp[i]);
1588 if (fdc->post_explore != NULL)
1589 fdc->post_explore(fdc);
1592 free(devlistp, M_TEMP);
1598 * To allocate unique transaction label.
1601 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1604 struct fw_xfer *txfer;
1606 static uint32_t label = 0;
1609 for( i = 0 ; i < 0x40 ; i ++){
1610 label = (label + 1) & 0x3f;
1611 STAILQ_FOREACH(txfer, &fc->tlabels[label], tlabel)
1612 if (txfer->send.hdr.mode.hdr.dst ==
1613 xfer->send.hdr.mode.hdr.dst)
1616 STAILQ_INSERT_TAIL(&fc->tlabels[label], xfer, tlabel);
1618 if (firewire_debug > 1)
1619 printf("fw_get_tlabel: dst=%d tl=%d\n",
1620 xfer->send.hdr.mode.hdr.dst, label);
1626 if (firewire_debug > 1)
1627 printf("fw_get_tlabel: no free tlabel\n");
1632 fw_rcv_copy(struct fw_rcv_buf *rb)
1636 struct tcode_info *tinfo;
1637 u_int res, i, len, plen;
1639 rb->xfer->recv.spd = rb->spd;
1641 pkt = (struct fw_pkt *)rb->vec->iov_base;
1642 tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1645 p = (u_char *)&rb->xfer->recv.hdr;
1646 bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1647 rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1648 rb->vec->iov_len -= tinfo->hdr_len;
1651 p = (u_char *)rb->xfer->recv.payload;
1652 res = rb->xfer->recv.pay_len;
1654 /* special handling for RRESQ */
1655 if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1656 p != NULL && res >= sizeof(uint32_t)) {
1657 *(uint32_t *)p = pkt->mode.rresq.data;
1658 rb->xfer->recv.pay_len = sizeof(uint32_t);
1662 if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1665 plen = pkt->mode.rresb.len;
1667 for (i = 0; i < rb->nvec; i++, rb->vec++) {
1668 len = MIN(rb->vec->iov_len, plen);
1670 printf("rcv buffer(%d) is %d bytes short.\n",
1671 rb->xfer->recv.pay_len, len - res);
1674 bcopy(rb->vec->iov_base, p, len);
1678 if (res == 0 || plen == 0)
1681 rb->xfer->recv.pay_len -= res;
1686 * Generic packet receiving process.
1689 fw_rcv(struct fw_rcv_buf *rb)
1691 struct fw_pkt *fp, *resfp;
1692 struct fw_bind *bind;
1694 int i, len, oldstate;
1699 qld = (uint32_t *)buf;
1700 printf("spd %d len:%d\n", spd, len);
1701 for( i = 0 ; i <= len && i < 32; i+= 4){
1702 printf("0x%08x ", ntohl(qld[i/4]));
1703 if((i % 16) == 15) printf("\n");
1705 if((i % 16) != 15) printf("\n");
1708 fp = (struct fw_pkt *)rb->vec[0].iov_base;
1709 tcode = fp->mode.common.tcode;
1715 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1716 fp->mode.hdr.tlrt >> 2);
1717 if(rb->xfer == NULL) {
1718 printf("fw_rcv: unknown response "
1719 "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1720 tcode_str[tcode], tcode,
1722 fp->mode.hdr.tlrt >> 2,
1723 fp->mode.hdr.tlrt & 3,
1724 fp->mode.rresq.data);
1726 printf("try ad-hoc work around!!\n");
1727 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1728 (fp->mode.hdr.tlrt >> 2)^3);
1729 if (rb->xfer == NULL) {
1730 printf("no use...\n");
1738 if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1739 rb->xfer->resp = EIO;
1742 /* make sure the packet is drained in AT queue */
1743 oldstate = rb->xfer->state;
1744 rb->xfer->state = FWXF_RCVD;
1747 fw_xfer_done(rb->xfer);
1752 printf("not sent yet tl=%x\n", rb->xfer->tl);
1756 printf("unexpected state %d\n", rb->xfer->state);
1764 bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1765 fp->mode.rreqq.dest_lo);
1767 printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1768 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1769 " src=0x%x data=%lx\n",
1771 " src=0x%x data=%x\n",
1773 fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1774 tcode_str[tcode], tcode,
1775 fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1776 if (rb->fc->status == FWBUSRESET) {
1777 printf("fw_rcv: cannot respond(bus reset)!\n");
1780 rb->xfer = fw_xfer_alloc(M_FWXFER);
1781 if(rb->xfer == NULL){
1784 rb->xfer->send.spd = rb->spd;
1785 rb->xfer->send.pay_len = 0;
1786 resfp = &rb->xfer->send.hdr;
1790 resfp->mode.hdr.tcode = FWTCODE_WRES;
1793 resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1796 resfp->mode.hdr.tcode = FWTCODE_RRESB;
1799 resfp->mode.hdr.tcode = FWTCODE_LRES;
1802 resfp->mode.hdr.dst = fp->mode.hdr.src;
1803 resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1804 resfp->mode.hdr.pri = fp->mode.hdr.pri;
1805 resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1806 resfp->mode.rresb.extcode = 0;
1807 resfp->mode.rresb.len = 0;
1809 rb->xfer->hand = fw_asy_callback;
1811 rb->xfer->hand = fw_xfer_free;
1812 if(fw_asyreq(rb->fc, -1, rb->xfer)){
1813 fw_xfer_free(rb->xfer);
1819 for (i = 0; i < rb->nvec; i ++)
1820 len += rb->vec[i].iov_len;
1821 rb->xfer = STAILQ_FIRST(&bind->xferlist);
1822 if (rb->xfer == NULL) {
1824 printf("Discard a packet for this bind.\n");
1828 STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1830 rb->xfer->hand(rb->xfer);
1832 #if 0 /* shouldn't happen ?? or for GASP */
1833 case FWTCODE_STREAM:
1835 struct fw_xferq *xferq;
1837 xferq = rb->fc->ir[sub];
1839 printf("stream rcv dma %d len %d off %d spd %d\n",
1840 sub, len, off, spd);
1842 if(xferq->queued >= xferq->maxq) {
1843 printf("receive queue is full\n");
1846 /* XXX get xfer from xfer queue, we don't need copy for
1848 rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1850 if (rb->xfer == NULL)
1855 STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1857 sc = device_get_softc(rb->fc->bdev);
1858 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1859 if (&xferq->rsel.si_pid != 0)
1861 if (SEL_WAITING(&xferq->rsel))
1863 selwakeuppri(&xferq->rsel, FWPRI);
1864 if (xferq->flag & FWXFERQ_WAKEUP) {
1865 xferq->flag &= ~FWXFERQ_WAKEUP;
1866 wakeup((caddr_t)xferq);
1868 if (xferq->flag & FWXFERQ_HANDLER) {
1876 printf("fw_rcv: unknow tcode %d\n", tcode);
1882 * Post process for Bus Manager election process.
1885 fw_try_bmr_callback(struct fw_xfer *xfer)
1887 struct firewire_comm *fc;
1893 if (xfer->resp != 0)
1895 if (xfer->recv.payload == NULL)
1897 if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
1900 bmr = ntohl(xfer->recv.payload[0]);
1904 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
1905 fw_xfer_free_buf(xfer);
1910 device_printf(fc->bdev, "bus manager election failed\n");
1911 fw_xfer_free_buf(xfer);
1916 * To candidate Bus Manager election process.
1919 fw_try_bmr(void *arg)
1921 struct fw_xfer *xfer;
1922 struct firewire_comm *fc = (struct firewire_comm *)arg;
1926 xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
1931 fc->status = FWBUSMGRELECT;
1933 fp = &xfer->send.hdr;
1934 fp->mode.lreq.dest_hi = 0xffff;
1935 fp->mode.lreq.tlrt = 0;
1936 fp->mode.lreq.tcode = FWTCODE_LREQ;
1937 fp->mode.lreq.pri = 0;
1938 fp->mode.lreq.src = 0;
1939 fp->mode.lreq.len = 8;
1940 fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
1941 fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
1942 fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
1943 xfer->send.payload[0] = htonl(0x3f);
1944 xfer->send.payload[1] = htonl(fc->nodeid);
1945 xfer->hand = fw_try_bmr_callback;
1947 err = fw_asyreq(fc, -1, xfer);
1949 fw_xfer_free_buf(xfer);
1957 * Software implementation for physical memory block access.
1958 * XXX:Too slow, usef for debug purpose only.
1961 fw_vmaccess(struct fw_xfer *xfer){
1962 struct fw_pkt *rfp, *sfp = NULL;
1963 uint32_t *ld = (uint32_t *)xfer->recv.buf;
1965 printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
1966 xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1967 printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1968 if(xfer->resp != 0){
1969 fw_xfer_free( xfer);
1972 if(xfer->recv.buf == NULL){
1973 fw_xfer_free( xfer);
1976 rfp = (struct fw_pkt *)xfer->recv.buf;
1977 switch(rfp->mode.hdr.tcode){
1978 /* XXX need fix for 64bit arch */
1980 xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
1981 xfer->send.len = 12;
1982 sfp = (struct fw_pkt *)xfer->send.buf;
1983 bcopy(rfp->mode.wreqb.payload,
1984 (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
1985 sfp->mode.wres.tcode = FWTCODE_WRES;
1986 sfp->mode.wres.rtcode = 0;
1989 xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
1990 xfer->send.len = 12;
1991 sfp->mode.wres.tcode = FWTCODE_WRES;
1992 *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
1993 sfp->mode.wres.rtcode = 0;
1996 xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
1997 xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
1998 sfp = (struct fw_pkt *)xfer->send.buf;
1999 bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2000 sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2001 sfp->mode.rresb.tcode = FWTCODE_RRESB;
2002 sfp->mode.rresb.len = rfp->mode.rreqb.len;
2003 sfp->mode.rresb.rtcode = 0;
2004 sfp->mode.rresb.extcode = 0;
2007 xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2008 xfer->send.len = 16;
2009 sfp = (struct fw_pkt *)xfer->send.buf;
2010 sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2011 sfp->mode.wres.tcode = FWTCODE_RRESQ;
2012 sfp->mode.rresb.rtcode = 0;
2015 fw_xfer_free( xfer);
2018 sfp->mode.hdr.dst = rfp->mode.hdr.src;
2019 xfer->dst = ntohs(rfp->mode.hdr.src);
2020 xfer->hand = fw_xfer_free;
2022 sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2023 sfp->mode.hdr.pri = 0;
2025 fw_asyreq(xfer->fc, -1, xfer);
2032 * CRC16 check-sum for IEEE1394 register blocks.
2035 fw_crc16(uint32_t *ptr, uint32_t len){
2036 uint32_t i, sum, crc = 0;
2038 len = (len + 3) & ~3;
2039 for(i = 0 ; i < len ; i+= 4){
2040 for( shift = 28 ; shift >= 0 ; shift -= 4){
2041 sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2042 crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2046 return((uint16_t) crc);
2050 fw_bmr(struct firewire_comm *fc)
2052 struct fw_device fwdev;
2053 union fw_self_id *self_id;
2057 /* Check to see if the current root node is cycle master capable */
2058 self_id = fw_find_self_id(fc, fc->max_node);
2059 if (fc->max_node > 0) {
2060 /* XXX check cmc bit of businfo block rather than contender */
2061 if (self_id->p0.link_active && self_id->p0.contender)
2062 cmstr = fc->max_node;
2064 device_printf(fc->bdev,
2065 "root node is not cycle master capable\n");
2066 /* XXX shall we be the cycle master? */
2068 /* XXX need bus reset */
2073 device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2074 if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2075 /* We are not the bus manager */
2081 /* Optimize gapcount */
2082 if(fc->max_hop <= MAX_GAPHOP )
2083 fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2084 /* If we are the cycle master, nothing to do */
2085 if (cmstr == fc->nodeid || cmstr == -1)
2087 /* Bus probe has not finished, make dummy fwdev for cmstr */
2088 bzero(&fwdev, sizeof(fwdev));
2092 fwdev.maxrec = 8; /* 512 */
2093 fwdev.status = FWDEVINIT;
2094 /* Set cmstr bit on the cycle master */
2095 quad = htonl(1 << 8);
2096 fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2097 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2103 fw_modevent(module_t mode, int type, void *data)
2106 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2107 static eventhandler_tag fwdev_ehtag = NULL;
2112 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2113 fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2114 fwdev_clone, 0, 1000);
2118 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2119 if (fwdev_ehtag != NULL)
2120 EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2126 return (EOPNOTSUPP);
2132 #ifdef __DragonFly__
2133 DECLARE_DUMMY_MODULE(firewire);
2135 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2136 MODULE_VERSION(firewire, 1);