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>
48 #include <sys/vimage.h>
52 #if defined(__DragonFly__) || __FreeBSD_version < 500000
53 #include <machine/clock.h> /* for DELAY() */
56 #include <sys/bus.h> /* used by smbus and newbus */
57 #include <machine/bus.h>
61 #include "firewirereg.h"
66 #include <dev/firewire/firewire.h>
67 #include <dev/firewire/firewirereg.h>
68 #include <dev/firewire/fwmem.h>
69 #include <dev/firewire/iec13213.h>
70 #include <dev/firewire/iec68113.h>
75 struct crom_chunk root;
76 struct crom_chunk vendor;
80 int firewire_debug=0, try_bmr=1, hold_count=3;
81 SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
82 "FireWire driver debug flag");
83 SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
84 SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
85 "Try to be a bus manager");
86 SYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
87 "Number of count of bus resets for removing lost device information");
89 MALLOC_DEFINE(M_FW, "firewire", "FireWire");
90 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
92 #define FW_MAXASYRTY 4
94 devclass_t firewire_devclass;
96 static void firewire_identify (driver_t *, device_t);
97 static int firewire_probe (device_t);
98 static int firewire_attach (device_t);
99 static int firewire_detach (device_t);
100 static int firewire_resume (device_t);
101 static void firewire_xfer_timeout(void *, int);
103 static int firewire_shutdown (device_t);
105 static device_t firewire_add_child (device_t, int, const char *, int);
106 static void fw_try_bmr (void *);
107 static void fw_try_bmr_callback (struct fw_xfer *);
108 static void fw_asystart (struct fw_xfer *);
109 static int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
110 static void fw_bus_probe (struct firewire_comm *);
111 static void fw_attach_dev (struct firewire_comm *);
112 static void fw_bus_probe_thread(void *);
114 static void fw_vmaccess (struct fw_xfer *);
116 static int fw_bmr (struct firewire_comm *);
117 static void fw_dump_hdr(struct fw_pkt *, char *);
119 static device_method_t firewire_methods[] = {
120 /* Device interface */
121 DEVMETHOD(device_identify, firewire_identify),
122 DEVMETHOD(device_probe, firewire_probe),
123 DEVMETHOD(device_attach, firewire_attach),
124 DEVMETHOD(device_detach, firewire_detach),
125 DEVMETHOD(device_suspend, bus_generic_suspend),
126 DEVMETHOD(device_resume, firewire_resume),
127 DEVMETHOD(device_shutdown, bus_generic_shutdown),
130 DEVMETHOD(bus_add_child, firewire_add_child),
131 DEVMETHOD(bus_print_child, bus_generic_print_child),
135 char *linkspeed[] = {
136 "S100", "S200", "S400", "S800",
137 "S1600", "S3200", "undef", "undef"
140 static char *tcode_str[] = {
141 "WREQQ", "WREQB", "WRES", "undef",
142 "RREQQ", "RREQB", "RRESQ", "RRESB",
143 "CYCS", "LREQ", "STREAM", "LRES",
144 "undef", "undef", "PHY", "undef"
147 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
148 #define MAX_GAPHOP 15
149 u_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18,
150 21, 24, 26, 29, 32, 35, 37, 40};
152 static driver_t firewire_driver = {
155 sizeof(struct firewire_softc),
159 * Lookup fwdev by node id.
162 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
164 struct fw_device *fwdev;
168 STAILQ_FOREACH(fwdev, &fc->devices, link)
169 if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
177 * Lookup fwdev by EUI64.
180 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
182 struct fw_device *fwdev;
187 STAILQ_FOREACH(fwdev, &fc->devices, link)
188 if (FW_EUI64_EQUAL(fwdev->eui, *eui))
193 if(fwdev == NULL) return NULL;
194 if(fwdev->status == FWDEVINVAL) return NULL;
199 * Async. request procedure for userland application.
202 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
205 struct fw_xferq *xferq;
209 struct tcode_info *info;
211 if(xfer == NULL) return EINVAL;
212 if(xfer->hand == NULL){
213 printf("hand == NULL\n");
216 fp = &xfer->send.hdr;
218 tcode = fp->mode.common.tcode & 0xf;
219 info = &fc->tcode[tcode];
220 if (info->flag == 0) {
221 printf("invalid tcode=%x\n", tcode);
225 /* XXX allow bus explore packets only after bus rest */
226 if ((fc->status < FWBUSEXPLORE) &&
227 ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) ||
228 (fp->mode.rreqq.dest_lo < 0xf0000000) ||
229 (fp->mode.rreqq.dest_lo >= 0xf0001000))) {
231 xfer->flag = FWXF_BUSY;
235 if (info->flag & FWTI_REQ)
240 if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
241 printf("send.pay_len > maxrec\n");
244 if (info->flag & FWTI_BLOCK_STR)
245 len = fp->mode.stream.len;
246 else if (info->flag & FWTI_BLOCK_ASY)
247 len = fp->mode.rresb.len;
250 if (len != xfer->send.pay_len){
251 printf("len(%d) != send.pay_len(%d) %s(%x)\n",
252 len, xfer->send.pay_len, tcode_str[tcode], tcode);
256 if(xferq->start == NULL){
257 printf("xferq->start == NULL\n");
260 if(!(xferq->queued < xferq->maxq)){
261 device_printf(fc->bdev, "Discard a packet (queued=%d)\n",
267 if (info->flag & FWTI_TLABEL) {
268 if (fw_get_tlabel(fc, xfer) < 0)
280 * Wakeup blocked process.
283 fw_xferwake(struct fw_xfer *xfer)
285 struct mtx *lock = &xfer->fc->wait_lock;
288 xfer->flag |= FWXF_WAKE;
296 fw_xferwait(struct fw_xfer *xfer)
298 struct mtx *lock = &xfer->fc->wait_lock;
302 if ((xfer->flag & FWXF_WAKE) == 0)
303 err = msleep((void *)xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0);
310 * Async. request with given xfer structure.
313 fw_asystart(struct fw_xfer *xfer)
315 struct firewire_comm *fc = xfer->fc;
318 /* Protect from interrupt/timeout */
320 xfer->flag = FWXF_INQ;
321 STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
327 /* XXX just queue for mbuf */
328 if (xfer->mbuf == NULL)
334 firewire_identify(driver_t *driver, device_t parent)
336 BUS_ADD_CHILD(parent, 0, "firewire", -1);
340 firewire_probe(device_t dev)
342 device_set_desc(dev, "IEEE1394(FireWire) bus");
347 firewire_xfer_timeout(void *arg, int pending)
349 struct firewire_comm *fc = (struct firewire_comm *)arg;
350 struct fw_xfer *xfer, *txfer;
352 struct timeval split_timeout;
353 STAILQ_HEAD(, fw_xfer) xfer_timeout;
356 split_timeout.tv_sec = 0;
357 split_timeout.tv_usec = 200 * 1000; /* 200 msec */
360 timevalsub(&tv, &split_timeout);
361 STAILQ_INIT(&xfer_timeout);
364 mtx_lock(&fc->tlabel_lock);
365 for (i = 0; i < 0x40; i ++) {
366 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
367 if ((xfer->flag & FWXF_SENT) == 0)
370 if (timevalcmp(&xfer->tv, &tv, >))
371 /* the rests are newer than this */
373 device_printf(fc->bdev,
374 "split transaction timeout: "
375 "tl=0x%x flag=0x%02x\n", i, xfer->flag);
376 fw_dump_hdr(&xfer->send.hdr, "send");
377 xfer->resp = ETIMEDOUT;
378 STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
379 STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel);
382 mtx_unlock(&fc->tlabel_lock);
386 STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer)
390 #define WATCHDOG_HZ 10
392 firewire_watchdog(void *arg)
394 struct firewire_comm *fc;
395 static int watchdog_clock = 0;
397 fc = (struct firewire_comm *)arg;
400 * At boot stage, the device interrupt is disabled and
401 * We encounter a timeout easily. To avoid this,
402 * ignore clock interrupt for a while.
404 if (watchdog_clock > WATCHDOG_HZ * 15)
405 taskqueue_enqueue(fc->taskqueue, &fc->task_timeout);
409 callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ,
410 (void *)firewire_watchdog, (void *)fc);
414 * The attach routine.
417 firewire_attach(device_t dev)
420 struct firewire_softc *sc = device_get_softc(dev);
421 device_t pa = device_get_parent(dev);
422 struct firewire_comm *fc;
424 fc = (struct firewire_comm *)device_get_softc(pa);
426 fc->status = FWBUSNOTREADY;
428 unit = device_get_unit(dev);
429 if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
433 mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF);
434 mtx_init(&fc->tlabel_lock, "fwtlabel", NULL, MTX_DEF);
435 CALLOUT_INIT(&fc->timeout_callout);
436 CALLOUT_INIT(&fc->bmr_callout);
437 CALLOUT_INIT(&fc->busprobe_callout);
438 TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, (void *)fc);
440 callout_reset(&sc->fc->timeout_callout, hz,
441 (void *)firewire_watchdog, (void *)sc->fc);
444 kproc_create(fw_bus_probe_thread, (void *)fc, &fc->probe_thread,
445 0, 0, "fw%d_probe", unit);
447 /* Locate our children */
448 bus_generic_probe(dev);
450 /* launch attachement of the added children */
451 bus_generic_attach(dev);
454 fw_busreset(fc, FWBUSNOTREADY);
461 * Attach it as child.
464 firewire_add_child(device_t dev, int order, const char *name, int unit)
467 struct firewire_softc *sc;
469 sc = (struct firewire_softc *)device_get_softc(dev);
470 child = device_add_child(dev, name, unit);
472 device_set_ivars(child, sc->fc);
473 device_probe_and_attach(child);
480 firewire_resume(device_t dev)
482 struct firewire_softc *sc;
484 sc = (struct firewire_softc *)device_get_softc(dev);
485 sc->fc->status = FWBUSNOTREADY;
487 bus_generic_resume(dev);
496 firewire_detach(device_t dev)
498 struct firewire_softc *sc;
499 struct firewire_comm *fc;
500 struct fw_device *fwdev, *fwdev_next;
503 sc = (struct firewire_softc *)device_get_softc(dev);
505 mtx_lock(&fc->wait_lock);
506 fc->status = FWBUSDETACH;
508 if (msleep(fc->probe_thread, &fc->wait_lock, PWAIT, "fwthr", hz * 60))
509 printf("firewire probe thread didn't die\n");
510 mtx_unlock(&fc->wait_lock);
512 if (fc->arq !=0 && fc->arq->maxq > 0)
515 if ((err = fwdev_destroydev(sc)) != 0)
518 if ((err = bus_generic_detach(dev)) != 0)
521 callout_stop(&fc->timeout_callout);
522 callout_stop(&fc->bmr_callout);
523 callout_stop(&fc->busprobe_callout);
525 /* XXX xfer_free and untimeout on all xfers */
526 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
527 fwdev = fwdev_next) {
528 fwdev_next = STAILQ_NEXT(fwdev, link);
531 free(fc->topology_map, M_FW);
532 free(fc->speed_map, M_FW);
533 free(fc->crom_src_buf, M_FW);
535 mtx_destroy(&fc->tlabel_lock);
536 mtx_destroy(&fc->wait_lock);
541 firewire_shutdown( device_t dev )
549 fw_xferq_drain(struct fw_xferq *xferq)
551 struct fw_xfer *xfer;
553 while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
554 STAILQ_REMOVE_HEAD(&xferq->q, link);
559 xfer->flag = FWXF_SENTERR;
565 fw_drain_txq(struct firewire_comm *fc)
567 struct fw_xfer *xfer, *txfer;
568 STAILQ_HEAD(, fw_xfer) xfer_drain;
571 STAILQ_INIT(&xfer_drain);
574 fw_xferq_drain(fc->atq);
575 fw_xferq_drain(fc->ats);
576 for(i = 0; i < fc->nisodma; i++)
577 fw_xferq_drain(fc->it[i]);
580 mtx_lock(&fc->tlabel_lock);
581 for (i = 0; i < 0x40; i ++)
582 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
584 printf("tl=%d flag=%d\n", i, xfer->flag);
586 STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
587 STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel);
589 mtx_unlock(&fc->tlabel_lock);
591 STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer)
596 fw_reset_csr(struct firewire_comm *fc)
600 CSRARC(fc, STATE_CLEAR)
601 = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
602 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
603 CSRARC(fc, NODE_IDS) = 0x3f;
605 CSRARC(fc, TOPO_MAP + 8) = 0;
610 for(i = 2; i < 0x100/4 - 2 ; i++){
611 CSRARC(fc, SPED_MAP + i * 4) = 0;
613 CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
614 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
615 CSRARC(fc, RESET_START) = 0;
616 CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
617 CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
618 CSRARC(fc, CYCLE_TIME) = 0x0;
619 CSRARC(fc, BUS_TIME) = 0x0;
620 CSRARC(fc, BUS_MGR_ID) = 0x3f;
621 CSRARC(fc, BANDWIDTH_AV) = 4915;
622 CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
623 CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
624 CSRARC(fc, IP_CHANNELS) = (1 << 31);
626 CSRARC(fc, CONF_ROM) = 0x04 << 24;
627 CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
628 CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
629 1 << 28 | 0xff << 16 | 0x09 << 8;
630 CSRARC(fc, CONF_ROM + 0xc) = 0;
632 /* DV depend CSRs see blue book */
633 CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON;
634 CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON;
636 CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
637 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
641 fw_init_crom(struct firewire_comm *fc)
643 struct crom_src *src;
645 fc->crom_src_buf = (struct crom_src_buf *)
646 malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
647 if (fc->crom_src_buf == NULL)
650 src = &fc->crom_src_buf->src;
651 bzero(src, sizeof(struct crom_src));
653 /* BUS info sample */
654 src->hdr.info_len = 4;
656 src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
658 src->businfo.irmc = 1;
659 src->businfo.cmc = 1;
660 src->businfo.isc = 1;
661 src->businfo.bmc = 1;
662 src->businfo.pmc = 0;
663 src->businfo.cyc_clk_acc = 100;
664 src->businfo.max_rec = fc->maxrec;
665 src->businfo.max_rom = MAXROM_4;
666 src->businfo.generation = 1;
667 src->businfo.link_spd = fc->speed;
669 src->businfo.eui64.hi = fc->eui.hi;
670 src->businfo.eui64.lo = fc->eui.lo;
672 STAILQ_INIT(&src->chunk_list);
675 fc->crom_root = &fc->crom_src_buf->root;
679 fw_reset_crom(struct firewire_comm *fc)
681 struct crom_src_buf *buf;
682 struct crom_src *src;
683 struct crom_chunk *root;
685 if (fc->crom_src_buf == NULL)
688 buf = fc->crom_src_buf;
690 root = fc->crom_root;
692 STAILQ_INIT(&src->chunk_list);
694 bzero(root, sizeof(struct crom_chunk));
695 crom_add_chunk(src, NULL, root, 0);
696 crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
697 /* private company_id */
698 crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
700 crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
701 crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version);
703 crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
704 crom_add_entry(root, CSRKEY_HW, __FreeBSD_version);
706 crom_add_simple_text(src, root, &buf->hw, V_hostname);
710 * Called after bus reset.
713 fw_busreset(struct firewire_comm *fc, uint32_t new_status)
715 struct firewire_dev_comm *fdc;
716 struct crom_src *src;
723 callout_stop(&fc->bmr_callout);
728 fc->status = new_status;
732 if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) {
733 for( i = 0 ; i < devcnt ; i++)
734 if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
735 fdc = device_get_softc(devlistp[i]);
736 if (fdc->post_busreset != NULL)
737 fdc->post_busreset(fdc);
739 free(devlistp, M_TEMP);
742 newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
743 src = &fc->crom_src_buf->src;
744 crom_load(src, (uint32_t *)newrom, CROMSIZE);
745 if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
746 /* bump generation and reload */
747 src->businfo.generation ++;
748 /* generation must be between 0x2 and 0xF */
749 if (src->businfo.generation < 2)
750 src->businfo.generation ++;
751 crom_load(src, (uint32_t *)newrom, CROMSIZE);
752 bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
757 /* Call once after reboot */
758 void fw_init(struct firewire_comm *fc)
762 struct fw_xfer *xfer;
781 STAILQ_INIT(&fc->atq->q);
782 STAILQ_INIT(&fc->ats->q);
784 for( i = 0 ; i < fc->nisodma ; i ++ ){
785 fc->it[i]->queued = 0;
786 fc->ir[i]->queued = 0;
788 fc->it[i]->start = NULL;
789 fc->ir[i]->start = NULL;
791 fc->it[i]->buf = NULL;
792 fc->ir[i]->buf = NULL;
794 fc->it[i]->flag = FWXFERQ_STREAM;
795 fc->ir[i]->flag = FWXFERQ_STREAM;
797 STAILQ_INIT(&fc->it[i]->q);
798 STAILQ_INIT(&fc->ir[i]->q);
801 fc->arq->maxq = FWMAXQUEUE;
802 fc->ars->maxq = FWMAXQUEUE;
803 fc->atq->maxq = FWMAXQUEUE;
804 fc->ats->maxq = FWMAXQUEUE;
806 for( i = 0 ; i < fc->nisodma ; i++){
807 fc->ir[i]->maxq = FWMAXQUEUE;
808 fc->it[i]->maxq = FWMAXQUEUE;
810 /* Initialize csr registers */
811 fc->topology_map = (struct fw_topology_map *)malloc(
812 sizeof(struct fw_topology_map),
813 M_FW, M_NOWAIT | M_ZERO);
814 fc->speed_map = (struct fw_speed_map *)malloc(
815 sizeof(struct fw_speed_map),
816 M_FW, M_NOWAIT | M_ZERO);
817 CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
818 CSRARC(fc, TOPO_MAP + 4) = 1;
819 CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
820 CSRARC(fc, SPED_MAP + 4) = 1;
822 STAILQ_INIT(&fc->devices);
824 /* Initialize Async handlers */
825 STAILQ_INIT(&fc->binds);
826 for( i = 0 ; i < 0x40 ; i++){
827 STAILQ_INIT(&fc->tlabels[i]);
830 /* DV depend CSRs see blue book */
832 CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
833 CSRARC(fc, oPCR) = 0x8000007a;
834 for(i = 4 ; i < 0x7c/4 ; i+=4){
835 CSRARC(fc, i + oPCR) = 0x8000007a;
838 CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
839 CSRARC(fc, iPCR) = 0x803f0000;
840 for(i = 4 ; i < 0x7c/4 ; i+=4){
841 CSRARC(fc, i + iPCR) = 0x0;
845 fc->crom_src_buf = NULL;
848 xfer = fw_xfer_alloc();
849 if(xfer == NULL) return;
851 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
856 xfer->hand = fw_vmaccess;
862 fwb->addrlen = 0xffffffff;
868 #define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
869 ((fwb)->end < (addr))?1:0)
872 * To lookup bound process from IEEE1394 address.
875 fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
878 struct fw_bind *tfw, *r = NULL;
880 addr = ((u_int64_t)dest_hi << 32) | dest_lo;
882 STAILQ_FOREACH(tfw, &fc->binds, fclist)
883 if (BIND_CMP(addr, tfw) == 0) {
892 * To bind IEEE1394 address block to process.
895 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
897 struct fw_bind *tfw, *prev = NULL;
900 if (fwb->start > fwb->end) {
901 printf("%s: invalid range\n", __func__);
906 STAILQ_FOREACH(tfw, &fc->binds, fclist) {
907 if (fwb->end < tfw->start)
912 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
913 else if (prev->end < fwb->start)
914 STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
916 printf("%s: bind failed\n", __func__);
924 * To free IEEE1394 address block.
927 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
930 struct fw_xfer *xfer, *next;
937 STAILQ_FOREACH(tfw, &fc->binds, fclist)
939 STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
943 printf("%s: no such binding\n", __func__);
949 /* shall we do this? */
950 for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
951 next = STAILQ_NEXT(xfer, link);
954 STAILQ_INIT(&fwb->xferlist);
963 fw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
964 int slen, int rlen, int n,
965 struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
968 struct fw_xfer *xfer;
970 for (i = 0; i < n; i++) {
971 xfer = fw_xfer_alloc_buf(type, slen, rlen);
978 STAILQ_INSERT_TAIL(q, xfer, link);
985 fw_xferlist_remove(struct fw_xferlist *q)
987 struct fw_xfer *xfer, *next;
989 for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
990 next = STAILQ_NEXT(xfer, link);
991 fw_xfer_free_buf(xfer);
999 fw_dump_hdr(struct fw_pkt *fp, char *prefix)
1001 printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
1002 "src=0x%03x\n", prefix,
1003 fp->mode.hdr.dst & 0x3f,
1004 fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3,
1005 fp->mode.hdr.tcode, fp->mode.hdr.pri,
1010 * To free transaction label.
1013 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
1015 struct fw_xfer *txfer;
1022 mtx_lock(&fc->tlabel_lock);
1023 #if 1 /* make sure the label is allocated */
1024 STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
1027 if (txfer == NULL) {
1028 printf("%s: the xfer is not in the queue "
1029 "(tlabel=%d, flag=0x%x)\n",
1030 __FUNCTION__, xfer->tl, xfer->flag);
1031 fw_dump_hdr(&xfer->send.hdr, "send");
1032 fw_dump_hdr(&xfer->recv.hdr, "recv");
1034 mtx_unlock(&fc->tlabel_lock);
1040 STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
1041 mtx_unlock(&fc->tlabel_lock);
1047 * To obtain XFER structure by transaction label.
1049 static struct fw_xfer *
1050 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode)
1052 struct fw_xfer *xfer;
1056 mtx_lock(&fc->tlabel_lock);
1057 STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
1058 if(xfer->send.hdr.mode.hdr.dst == node) {
1059 mtx_unlock(&fc->tlabel_lock);
1061 KASSERT(xfer->tl == tlabel,
1062 ("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel));
1063 /* extra sanity check */
1064 req = xfer->send.hdr.mode.hdr.tcode;
1065 if (xfer->fc->tcode[req].valid_res != tcode) {
1066 printf("%s: invalid response tcode "
1067 "(0x%x for 0x%x)\n", __FUNCTION__,
1072 if (firewire_debug > 2)
1073 printf("fw_tl2xfer: found tl=%d\n", tlabel);
1076 mtx_unlock(&fc->tlabel_lock);
1077 if (firewire_debug > 1)
1078 printf("fw_tl2xfer: not found tl=%d\n", tlabel);
1084 * To allocate IEEE1394 XFER structure.
1087 fw_xfer_alloc(struct malloc_type *type)
1089 struct fw_xfer *xfer;
1091 xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
1095 xfer->malloc = type;
1101 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
1103 struct fw_xfer *xfer;
1105 xfer = fw_xfer_alloc(type);
1108 xfer->send.pay_len = send_len;
1109 xfer->recv.pay_len = recv_len;
1111 xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
1112 if (xfer->send.payload == NULL) {
1118 xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
1119 if (xfer->recv.payload == NULL) {
1120 if (xfer->send.payload != NULL)
1121 free(xfer->send.payload, type);
1130 * IEEE1394 XFER post process.
1133 fw_xfer_done(struct fw_xfer *xfer)
1135 if (xfer->hand == NULL) {
1136 printf("hand == NULL\n");
1140 if (xfer->fc == NULL)
1141 panic("fw_xfer_done: why xfer->fc is NULL?");
1143 fw_tl_free(xfer->fc, xfer);
1148 fw_xfer_unload(struct fw_xfer* xfer)
1152 if(xfer == NULL ) return;
1153 if(xfer->flag & FWXF_INQ){
1154 printf("fw_xfer_free FWXF_INQ\n");
1157 STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
1161 FW_GUNLOCK(xfer->fc);
1164 if (xfer->fc != NULL) {
1166 if(xfer->flag & FWXF_START)
1168 * This could happen if:
1169 * 1. We call fwohci_arcv() before fwohci_txd().
1170 * 2. firewire_watch() is called.
1172 printf("fw_xfer_free FWXF_START\n");
1175 xfer->flag = FWXF_INIT;
1179 * To free IEEE1394 XFER structure.
1182 fw_xfer_free_buf( struct fw_xfer* xfer)
1185 printf("%s: xfer == NULL\n", __func__);
1188 fw_xfer_unload(xfer);
1189 if(xfer->send.payload != NULL){
1190 free(xfer->send.payload, xfer->malloc);
1192 if(xfer->recv.payload != NULL){
1193 free(xfer->recv.payload, xfer->malloc);
1195 free(xfer, xfer->malloc);
1199 fw_xfer_free( struct fw_xfer* xfer)
1202 printf("%s: xfer == NULL\n", __func__);
1205 fw_xfer_unload(xfer);
1206 free(xfer, xfer->malloc);
1210 fw_asy_callback_free(struct fw_xfer *xfer)
1213 printf("asyreq done flag=0x%02x resp=%d\n",
1214 xfer->flag, xfer->resp);
1223 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
1225 struct fw_xfer *xfer;
1228 fc->status = FWBUSPHYCONF;
1230 xfer = fw_xfer_alloc(M_FWXFER);
1234 xfer->hand = fw_asy_callback_free;
1236 fp = &xfer->send.hdr;
1239 fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
1241 fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
1242 fp->mode.ld[2] = ~fp->mode.ld[1];
1243 /* XXX Dangerous, how to pass PHY packet to device driver */
1244 fp->mode.common.tcode |= FWTCODE_PHY;
1247 printf("send phy_config root_node=%d gap_count=%d\n",
1248 root_node, gap_count);
1249 fw_asyreq(fc, -1, xfer);
1257 fw_print_sid(uint32_t sid)
1259 union fw_self_id *s;
1260 s = (union fw_self_id *) &sid;
1261 printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
1262 " p0:%d p1:%d p2:%d i:%d m:%d\n",
1263 s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
1264 s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
1265 s->p0.power_class, s->p0.port0, s->p0.port1,
1266 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
1271 * To receive self ID.
1273 void fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
1276 union fw_self_id *self_id;
1277 u_int i, j, node, c_port = 0, i_branch = 0;
1279 fc->sid_cnt = len /(sizeof(uint32_t) * 2);
1280 fc->max_node = fc->nodeid & 0x3f;
1281 CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
1282 fc->status = FWBUSCYMELECT;
1283 fc->topology_map->crc_len = 2;
1284 fc->topology_map->generation ++;
1285 fc->topology_map->self_id_count = 0;
1286 fc->topology_map->node_count = 0;
1287 fc->speed_map->generation ++;
1288 fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
1289 self_id = &fc->topology_map->self_id[0];
1290 for(i = 0; i < fc->sid_cnt; i ++){
1291 if (sid[1] != ~sid[0]) {
1292 printf("fw_sidrcv: invalid self-id packet\n");
1296 *self_id = *((union fw_self_id *)sid);
1297 fc->topology_map->crc_len++;
1298 if(self_id->p0.sequel == 0){
1299 fc->topology_map->node_count ++;
1302 fw_print_sid(sid[0]);
1304 node = self_id->p0.phy_id;
1305 if(fc->max_node < node){
1306 fc->max_node = self_id->p0.phy_id;
1308 /* XXX I'm not sure this is the right speed_map */
1309 fc->speed_map->speed[node][node]
1310 = self_id->p0.phy_speed;
1311 for (j = 0; j < node; j ++) {
1312 fc->speed_map->speed[j][node]
1313 = fc->speed_map->speed[node][j]
1314 = min(fc->speed_map->speed[j][j],
1315 self_id->p0.phy_speed);
1317 if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
1318 (self_id->p0.link_active && self_id->p0.contender)) {
1319 fc->irm = self_id->p0.phy_id;
1321 if(self_id->p0.port0 >= 0x2){
1324 if(self_id->p0.port1 >= 0x2){
1327 if(self_id->p0.port2 >= 0x2){
1332 i_branch += (c_port - 2);
1336 fc->topology_map->self_id_count ++;
1338 device_printf(fc->bdev, "%d nodes", fc->max_node + 1);
1340 fc->topology_map->crc = fw_crc16(
1341 (uint32_t *)&fc->topology_map->generation,
1342 fc->topology_map->crc_len * 4);
1343 fc->speed_map->crc = fw_crc16(
1344 (uint32_t *)&fc->speed_map->generation,
1345 fc->speed_map->crc_len * 4);
1346 /* byteswap and copy to CSR */
1347 p = (uint32_t *)fc->topology_map;
1348 for (i = 0; i <= fc->topology_map->crc_len; i++)
1349 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
1350 p = (uint32_t *)fc->speed_map;
1351 CSRARC(fc, SPED_MAP) = htonl(*p++);
1352 CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
1353 /* don't byte-swap uint8_t array */
1354 bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
1356 fc->max_hop = fc->max_node - i_branch;
1357 printf(", maxhop <= %d", fc->max_hop);
1360 printf(", Not found IRM capable node");
1362 printf(", cable IRM = %d", fc->irm);
1363 if (fc->irm == fc->nodeid)
1368 if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
1369 if (fc->irm == fc->nodeid) {
1370 fc->status = FWBUSMGRDONE;
1371 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
1374 fc->status = FWBUSMGRELECT;
1375 callout_reset(&fc->bmr_callout, hz/8,
1376 (void *)fw_try_bmr, (void *)fc);
1379 fc->status = FWBUSMGRDONE;
1381 callout_reset(&fc->busprobe_callout, hz/4,
1382 (void *)fw_bus_probe, (void *)fc);
1386 * To probe devices on the IEEE1394 bus.
1389 fw_bus_probe(struct firewire_comm *fc)
1392 struct fw_device *fwdev;
1395 fc->status = FWBUSEXPLORE;
1397 /* Invalidate all devices, just after bus reset. */
1398 STAILQ_FOREACH(fwdev, &fc->devices, link)
1399 if (fwdev->status != FWDEVINVAL) {
1400 fwdev->status = FWDEVINVAL;
1409 fw_explore_read_quads(struct fw_device *fwdev, int offset,
1410 uint32_t *quad, int n)
1412 struct fw_xfer *xfer;
1416 for (i = 0; i < n; i ++, offset += sizeof(uint32_t)) {
1417 xfer = fwmem_read_quad(fwdev, NULL, -1,
1418 0xffff, 0xf0000000 | offset, (void *)&tmp,
1424 if (xfer->resp == 0)
1425 quad[i] = ntohl(tmp);
1437 fw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
1440 struct csrdirectory *dir;
1443 dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1444 err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1445 (uint32_t *)dir, 1);
1449 offset += sizeof(uint32_t);
1450 reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)];
1451 err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
1452 (uint32_t *)reg, dir->crc_len);
1458 off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
1459 if (fwdev->rommax < off)
1460 fwdev->rommax = off;
1465 for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) {
1466 if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D)
1468 else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L)
1473 off = offset + reg[i].val * sizeof(uint32_t);
1474 if (off > CROMSIZE) {
1475 printf("%s: invalid offset %d\n", __FUNCTION__, off);
1478 err = fw_explore_csrblock(fwdev, off, recur);
1486 fw_explore_node(struct fw_device *dfwdev)
1488 struct firewire_comm *fc;
1489 struct fw_device *fwdev, *pfwdev, *tfwdev;
1492 struct bus_info *binfo;
1496 csr = dfwdev->csrrom;
1500 err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1);
1503 hdr = (struct csrhdr *)&csr[0];
1504 if (hdr->info_len != 4) {
1506 printf("node%d: wrong bus info len(%d)\n",
1507 node, hdr->info_len);
1512 err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
1515 binfo = (struct bus_info *)&csr[1];
1516 if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
1518 printf("node%d: invalid bus name 0x%08x\n",
1519 node, binfo->bus_name);
1522 spd = fc->speed_map->speed[fc->nodeid][node];
1523 STAILQ_FOREACH(fwdev, &fc->devices, link)
1524 if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
1526 if (fwdev == NULL) {
1528 fwdev = malloc(sizeof(struct fw_device), M_FW,
1530 if (fwdev == NULL) {
1532 printf("node%d: no memory\n", node);
1536 fwdev->eui = binfo->eui64;
1537 /* inesrt into sorted fwdev list */
1539 STAILQ_FOREACH(tfwdev, &fc->devices, link) {
1540 if (tfwdev->eui.hi > fwdev->eui.hi ||
1541 (tfwdev->eui.hi == fwdev->eui.hi &&
1542 tfwdev->eui.lo > fwdev->eui.lo))
1547 STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
1549 STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
1551 device_printf(fc->bdev, "New %s device ID:%08x%08x\n",
1553 fwdev->eui.hi, fwdev->eui.lo);
1556 fwdev->status = FWDEVINIT;
1560 if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) {
1562 printf("node%d: crom unchanged\n", node);
1566 bzero(&fwdev->csrrom[0], CROMSIZE);
1568 /* copy first quad and bus info block */
1569 bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5);
1570 fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
1572 err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
1575 fwdev->status = FWDEVINVAL;
1576 fwdev->csrrom[0] = 0;
1583 * Find the self_id packet for a node, ignoring sequels.
1585 static union fw_self_id *
1586 fw_find_self_id(struct firewire_comm *fc, int node)
1589 union fw_self_id *s;
1591 for (i = 0; i < fc->topology_map->self_id_count; i++) {
1592 s = &fc->topology_map->self_id[i];
1595 if (s->p0.phy_id == node)
1602 fw_explore(struct firewire_comm *fc)
1604 int node, err, s, i, todo, todo2, trys;
1606 struct fw_device dfwdev;
1607 union fw_self_id *fwsid;
1610 /* setup dummy fwdev */
1613 dfwdev.maxrec = 8; /* 512 */
1614 dfwdev.status = FWDEVINIT;
1616 for (node = 0; node <= fc->max_node; node ++) {
1617 /* We don't probe myself and linkdown nodes */
1618 if (node == fc->nodeid)
1620 fwsid = fw_find_self_id(fc, node);
1621 if (!fwsid || !fwsid->p0.link_active) {
1623 printf("node%d: link down\n", node);
1626 nodes[todo++] = node;
1630 for (trys = 0; todo > 0 && trys < 3; trys ++) {
1632 for (i = 0; i < todo; i ++) {
1633 dfwdev.dst = nodes[i];
1634 err = fw_explore_node(&dfwdev);
1636 nodes[todo2++] = nodes[i];
1638 printf("%s: node %d, err = %d\n",
1639 __FUNCTION__, node, err);
1648 fw_bus_probe_thread(void *arg)
1650 struct firewire_comm *fc;
1652 fc = (struct firewire_comm *)arg;
1654 mtx_lock(&fc->wait_lock);
1655 while (fc->status != FWBUSDETACH) {
1656 if (fc->status == FWBUSEXPLORE) {
1657 mtx_unlock(&fc->wait_lock);
1659 fc->status = FWBUSEXPDONE;
1661 printf("bus_explore done\n");
1663 mtx_lock(&fc->wait_lock);
1665 msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0);
1667 mtx_unlock(&fc->wait_lock);
1672 * To attach sub-devices layer onto IEEE1394 bus.
1675 fw_attach_dev(struct firewire_comm *fc)
1677 struct fw_device *fwdev, *next;
1681 struct firewire_dev_comm *fdc;
1683 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
1684 next = STAILQ_NEXT(fwdev, link);
1685 if (fwdev->status == FWDEVINIT) {
1686 fwdev->status = FWDEVATTACHED;
1687 } else if (fwdev->status == FWDEVINVAL) {
1689 if (fwdev->rcnt > hold_count) {
1691 * Remove devices which have not been seen
1694 STAILQ_REMOVE(&fc->devices, fwdev, fw_device,
1701 err = device_get_children(fc->bdev, &devlistp, &devcnt);
1704 for( i = 0 ; i < devcnt ; i++){
1705 if (device_get_state(devlistp[i]) >= DS_ATTACHED) {
1706 fdc = device_get_softc(devlistp[i]);
1707 if (fdc->post_explore != NULL)
1708 fdc->post_explore(fdc);
1711 free(devlistp, M_TEMP);
1717 * To allocate unique transaction label.
1720 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
1722 u_int dst, new_tlabel;
1723 struct fw_xfer *txfer;
1726 dst = xfer->send.hdr.mode.hdr.dst & 0x3f;
1728 mtx_lock(&fc->tlabel_lock);
1729 new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f;
1730 STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel)
1731 if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst)
1734 fc->last_tlabel[dst] = new_tlabel;
1735 STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel);
1736 mtx_unlock(&fc->tlabel_lock);
1738 xfer->tl = new_tlabel;
1739 xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2;
1740 if (firewire_debug > 1)
1741 printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel);
1742 return (new_tlabel);
1744 mtx_unlock(&fc->tlabel_lock);
1747 if (firewire_debug > 1)
1748 printf("fw_get_tlabel: no free tlabel\n");
1753 fw_rcv_copy(struct fw_rcv_buf *rb)
1757 struct tcode_info *tinfo;
1758 u_int res, i, len, plen;
1760 rb->xfer->recv.spd = rb->spd;
1762 pkt = (struct fw_pkt *)rb->vec->iov_base;
1763 tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
1766 p = (u_char *)&rb->xfer->recv.hdr;
1767 bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
1768 rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
1769 rb->vec->iov_len -= tinfo->hdr_len;
1772 p = (u_char *)rb->xfer->recv.payload;
1773 res = rb->xfer->recv.pay_len;
1775 /* special handling for RRESQ */
1776 if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
1777 p != NULL && res >= sizeof(uint32_t)) {
1778 *(uint32_t *)p = pkt->mode.rresq.data;
1779 rb->xfer->recv.pay_len = sizeof(uint32_t);
1783 if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
1786 plen = pkt->mode.rresb.len;
1788 for (i = 0; i < rb->nvec; i++, rb->vec++) {
1789 len = MIN(rb->vec->iov_len, plen);
1791 printf("rcv buffer(%d) is %d bytes short.\n",
1792 rb->xfer->recv.pay_len, len - res);
1795 bcopy(rb->vec->iov_base, p, len);
1799 if (res == 0 || plen == 0)
1802 rb->xfer->recv.pay_len -= res;
1807 * Generic packet receiving process.
1810 fw_rcv(struct fw_rcv_buf *rb)
1812 struct fw_pkt *fp, *resfp;
1813 struct fw_bind *bind;
1815 int i, len, oldstate;
1820 qld = (uint32_t *)buf;
1821 printf("spd %d len:%d\n", spd, len);
1822 for( i = 0 ; i <= len && i < 32; i+= 4){
1823 printf("0x%08x ", ntohl(qld[i/4]));
1824 if((i % 16) == 15) printf("\n");
1826 if((i % 16) != 15) printf("\n");
1829 fp = (struct fw_pkt *)rb->vec[0].iov_base;
1830 tcode = fp->mode.common.tcode;
1836 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1837 fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode);
1838 if(rb->xfer == NULL) {
1839 printf("fw_rcv: unknown response "
1840 "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
1841 tcode_str[tcode], tcode,
1843 fp->mode.hdr.tlrt >> 2,
1844 fp->mode.hdr.tlrt & 3,
1845 fp->mode.rresq.data);
1847 printf("try ad-hoc work around!!\n");
1848 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
1849 (fp->mode.hdr.tlrt >> 2)^3);
1850 if (rb->xfer == NULL) {
1851 printf("no use...\n");
1859 if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
1860 rb->xfer->resp = EIO;
1863 /* make sure the packet is drained in AT queue */
1864 oldstate = rb->xfer->flag;
1865 rb->xfer->flag = FWXF_RCVD;
1868 fw_xfer_done(rb->xfer);
1873 printf("not sent yet tl=%x\n", rb->xfer->tl);
1877 printf("unexpected flag 0x%02x\n", rb->xfer->flag);
1885 bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
1886 fp->mode.rreqq.dest_lo);
1888 printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
1889 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1890 " src=0x%x data=%lx\n",
1892 " src=0x%x data=%x\n",
1894 fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
1895 tcode_str[tcode], tcode,
1896 fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
1897 if (rb->fc->status == FWBUSINIT) {
1898 printf("fw_rcv: cannot respond(bus reset)!\n");
1901 rb->xfer = fw_xfer_alloc(M_FWXFER);
1902 if(rb->xfer == NULL){
1905 rb->xfer->send.spd = rb->spd;
1906 rb->xfer->send.pay_len = 0;
1907 resfp = &rb->xfer->send.hdr;
1911 resfp->mode.hdr.tcode = FWTCODE_WRES;
1914 resfp->mode.hdr.tcode = FWTCODE_RRESQ;
1917 resfp->mode.hdr.tcode = FWTCODE_RRESB;
1920 resfp->mode.hdr.tcode = FWTCODE_LRES;
1923 resfp->mode.hdr.dst = fp->mode.hdr.src;
1924 resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
1925 resfp->mode.hdr.pri = fp->mode.hdr.pri;
1926 resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
1927 resfp->mode.rresb.extcode = 0;
1928 resfp->mode.rresb.len = 0;
1930 rb->xfer->hand = fw_xferwake;
1932 rb->xfer->hand = fw_xfer_free;
1933 if(fw_asyreq(rb->fc, -1, rb->xfer)){
1934 fw_xfer_free(rb->xfer);
1940 for (i = 0; i < rb->nvec; i ++)
1941 len += rb->vec[i].iov_len;
1942 rb->xfer = STAILQ_FIRST(&bind->xferlist);
1943 if (rb->xfer == NULL) {
1945 printf("Discard a packet for this bind.\n");
1949 STAILQ_REMOVE_HEAD(&bind->xferlist, link);
1951 rb->xfer->hand(rb->xfer);
1953 #if 0 /* shouldn't happen ?? or for GASP */
1954 case FWTCODE_STREAM:
1956 struct fw_xferq *xferq;
1958 xferq = rb->fc->ir[sub];
1960 printf("stream rcv dma %d len %d off %d spd %d\n",
1961 sub, len, off, spd);
1963 if(xferq->queued >= xferq->maxq) {
1964 printf("receive queue is full\n");
1967 /* XXX get xfer from xfer queue, we don't need copy for
1969 rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
1971 if (rb->xfer == NULL)
1976 STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
1978 sc = device_get_softc(rb->fc->bdev);
1979 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1980 if (&xferq->rsel.si_pid != 0)
1982 if (SEL_WAITING(&xferq->rsel))
1984 selwakeuppri(&xferq->rsel, FWPRI);
1985 if (xferq->flag & FWXFERQ_WAKEUP) {
1986 xferq->flag &= ~FWXFERQ_WAKEUP;
1987 wakeup((caddr_t)xferq);
1989 if (xferq->flag & FWXFERQ_HANDLER) {
1997 printf("fw_rcv: unknow tcode %d\n", tcode);
2003 * Post process for Bus Manager election process.
2006 fw_try_bmr_callback(struct fw_xfer *xfer)
2008 struct firewire_comm *fc;
2014 if (xfer->resp != 0)
2016 if (xfer->recv.payload == NULL)
2018 if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
2021 bmr = ntohl(xfer->recv.payload[0]);
2025 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
2026 fw_xfer_free_buf(xfer);
2031 device_printf(fc->bdev, "bus manager election failed\n");
2032 fw_xfer_free_buf(xfer);
2037 * To candidate Bus Manager election process.
2040 fw_try_bmr(void *arg)
2042 struct fw_xfer *xfer;
2043 struct firewire_comm *fc = (struct firewire_comm *)arg;
2047 xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
2052 fc->status = FWBUSMGRELECT;
2054 fp = &xfer->send.hdr;
2055 fp->mode.lreq.dest_hi = 0xffff;
2056 fp->mode.lreq.tlrt = 0;
2057 fp->mode.lreq.tcode = FWTCODE_LREQ;
2058 fp->mode.lreq.pri = 0;
2059 fp->mode.lreq.src = 0;
2060 fp->mode.lreq.len = 8;
2061 fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
2062 fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
2063 fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
2064 xfer->send.payload[0] = htonl(0x3f);
2065 xfer->send.payload[1] = htonl(fc->nodeid);
2066 xfer->hand = fw_try_bmr_callback;
2068 err = fw_asyreq(fc, -1, xfer);
2070 fw_xfer_free_buf(xfer);
2078 * Software implementation for physical memory block access.
2079 * XXX:Too slow, usef for debug purpose only.
2082 fw_vmaccess(struct fw_xfer *xfer){
2083 struct fw_pkt *rfp, *sfp = NULL;
2084 uint32_t *ld = (uint32_t *)xfer->recv.buf;
2086 printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
2087 xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
2088 printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
2089 if(xfer->resp != 0){
2090 fw_xfer_free( xfer);
2093 if(xfer->recv.buf == NULL){
2094 fw_xfer_free( xfer);
2097 rfp = (struct fw_pkt *)xfer->recv.buf;
2098 switch(rfp->mode.hdr.tcode){
2099 /* XXX need fix for 64bit arch */
2101 xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2102 xfer->send.len = 12;
2103 sfp = (struct fw_pkt *)xfer->send.buf;
2104 bcopy(rfp->mode.wreqb.payload,
2105 (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
2106 sfp->mode.wres.tcode = FWTCODE_WRES;
2107 sfp->mode.wres.rtcode = 0;
2110 xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
2111 xfer->send.len = 12;
2112 sfp->mode.wres.tcode = FWTCODE_WRES;
2113 *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
2114 sfp->mode.wres.rtcode = 0;
2117 xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
2118 xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
2119 sfp = (struct fw_pkt *)xfer->send.buf;
2120 bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo),
2121 sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
2122 sfp->mode.rresb.tcode = FWTCODE_RRESB;
2123 sfp->mode.rresb.len = rfp->mode.rreqb.len;
2124 sfp->mode.rresb.rtcode = 0;
2125 sfp->mode.rresb.extcode = 0;
2128 xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
2129 xfer->send.len = 16;
2130 sfp = (struct fw_pkt *)xfer->send.buf;
2131 sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
2132 sfp->mode.wres.tcode = FWTCODE_RRESQ;
2133 sfp->mode.rresb.rtcode = 0;
2136 fw_xfer_free( xfer);
2139 sfp->mode.hdr.dst = rfp->mode.hdr.src;
2140 xfer->dst = ntohs(rfp->mode.hdr.src);
2141 xfer->hand = fw_xfer_free;
2143 sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
2144 sfp->mode.hdr.pri = 0;
2146 fw_asyreq(xfer->fc, -1, xfer);
2153 * CRC16 check-sum for IEEE1394 register blocks.
2156 fw_crc16(uint32_t *ptr, uint32_t len){
2157 uint32_t i, sum, crc = 0;
2159 len = (len + 3) & ~3;
2160 for(i = 0 ; i < len ; i+= 4){
2161 for( shift = 28 ; shift >= 0 ; shift -= 4){
2162 sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
2163 crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
2167 return((uint16_t) crc);
2171 fw_bmr(struct firewire_comm *fc)
2173 struct fw_device fwdev;
2174 union fw_self_id *self_id;
2178 /* Check to see if the current root node is cycle master capable */
2179 self_id = fw_find_self_id(fc, fc->max_node);
2180 if (fc->max_node > 0) {
2181 /* XXX check cmc bit of businfo block rather than contender */
2182 if (self_id->p0.link_active && self_id->p0.contender)
2183 cmstr = fc->max_node;
2185 device_printf(fc->bdev,
2186 "root node is not cycle master capable\n");
2187 /* XXX shall we be the cycle master? */
2189 /* XXX need bus reset */
2194 device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
2195 if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
2196 /* We are not the bus manager */
2202 /* Optimize gapcount */
2203 if(fc->max_hop <= MAX_GAPHOP )
2204 fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
2205 /* If we are the cycle master, nothing to do */
2206 if (cmstr == fc->nodeid || cmstr == -1)
2208 /* Bus probe has not finished, make dummy fwdev for cmstr */
2209 bzero(&fwdev, sizeof(fwdev));
2213 fwdev.maxrec = 8; /* 512 */
2214 fwdev.status = FWDEVINIT;
2215 /* Set cmstr bit on the cycle master */
2216 quad = htonl(1 << 8);
2217 fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
2218 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
2224 fw_open_isodma(struct firewire_comm *fc, int tx)
2226 struct fw_xferq **xferqa;
2227 struct fw_xferq *xferq;
2231 xferqa = &fc->it[0];
2233 xferqa = &fc->ir[0];
2236 for (i = 0; i < fc->nisodma; i ++) {
2238 if ((xferq->flag & FWXFERQ_OPEN) == 0) {
2239 xferq->flag |= FWXFERQ_OPEN;
2243 if (i == fc->nisodma) {
2244 printf("no free dma channel (tx=%d)\n", tx);
2252 fw_modevent(module_t mode, int type, void *data)
2255 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2256 static eventhandler_tag fwdev_ehtag = NULL;
2261 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2262 fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
2263 fwdev_clone, 0, 1000);
2267 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2268 if (fwdev_ehtag != NULL)
2269 EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
2275 return (EOPNOTSUPP);
2281 #ifdef __DragonFly__
2282 DECLARE_DUMMY_MODULE(firewire);
2284 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
2285 MODULE_VERSION(firewire, 1);