The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ieee1394/firewire.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: firewire.c,v 1.21 2008/05/10 13:56:54 jmcneill Exp $   */
    2 /*-
    3  * Copyright (c) 2003 Hidetoshi Shimokawa
    4  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the acknowledgement as bellow:
   17  *
   18  *    This product includes software developed by K. Kobayashi and H. Shimokawa
   19  *
   20  * 4. The name of the author may not be used to endorse or promote products
   21  *    derived from this software without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
   27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33  * POSSIBILITY OF SUCH DAMAGE.
   34  * 
   35  * $FreeBSD: src/sys/dev/firewire/firewire.c,v 1.101 2007/10/20 23:23:14 julian Exp $
   36  *
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __KERNEL_RCSID(0, "$NetBSD: firewire.c,v 1.21 2008/05/10 13:56:54 jmcneill Exp $");
   41 
   42 #if defined(__FreeBSD__)
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/types.h>
   46 
   47 #include <sys/kernel.h>
   48 #include <sys/module.h>
   49 #include <sys/malloc.h>
   50 #include <sys/conf.h>
   51 #include <sys/sysctl.h>
   52 #include <sys/kthread.h>
   53 
   54 #include <sys/kdb.h>
   55 
   56 #if defined(__DragonFly__) || __FreeBSD_version < 500000
   57 #include <machine/clock.h>      /* for DELAY() */
   58 #endif
   59 
   60 #include <sys/bus.h>            /* used by smbus and newbus */
   61 #include <sys/bus.h>
   62 
   63 #ifdef __DragonFly__
   64 #include "fw_port.h"
   65 #include "firewire.h"
   66 #include "firewirereg.h"
   67 #include "fwmem.h"
   68 #include "iec13213.h"
   69 #include "iec68113.h"
   70 #else
   71 #include <dev/firewire/fw_port.h>
   72 #include <dev/firewire/firewire.h>
   73 #include <dev/firewire/firewirereg.h>
   74 #include <dev/firewire/fwmem.h>
   75 #include <dev/firewire/iec13213.h>
   76 #include <dev/firewire/iec68113.h>
   77 #endif
   78 #elif defined(__NetBSD__)
   79 #include <sys/param.h>
   80 #include <sys/device.h>
   81 #include <sys/errno.h>
   82 #include <sys/conf.h>
   83 #include <sys/kernel.h>
   84 #include <sys/kthread.h>
   85 #include <sys/malloc.h>
   86 #include <sys/queue.h>
   87 #include <sys/sysctl.h>
   88 #include <sys/systm.h>
   89 
   90 #include <sys/bus.h>
   91 
   92 #include <dev/ieee1394/fw_port.h>
   93 #include <dev/ieee1394/firewire.h>
   94 #include <dev/ieee1394/firewirereg.h>
   95 #include <dev/ieee1394/fwmem.h>
   96 #include <dev/ieee1394/iec13213.h>
   97 #include <dev/ieee1394/iec68113.h>
   98 
   99 #include "locators.h"
  100 #endif
  101 
  102 struct crom_src_buf {
  103         struct crom_src src;
  104         struct crom_chunk root;
  105         struct crom_chunk vendor;
  106         struct crom_chunk hw;
  107 };
  108 
  109 int firewire_debug=0, try_bmr=1, hold_count=3;
  110 #if defined(__FreeBSD__)
  111 SYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0,
  112         "FireWire driver debug flag");
  113 SYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem");
  114 SYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0,
  115         "Try to be a bus manager");
  116 SYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0,
  117         "Number of count of bus resets for removing lost device information");
  118 
  119 MALLOC_DEFINE(M_FW, "firewire", "FireWire");
  120 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire");
  121 #elif defined(__NetBSD__)
  122 /*
  123  * Setup sysctl(3) MIB, hw.ieee1394if.*
  124  *
  125  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
  126  */
  127 SYSCTL_SETUP(sysctl_ieee1394if, "sysctl ieee1394if(4) subtree setup")
  128 {
  129         int rc, ieee1394if_node_num;
  130         const struct sysctlnode *node;
  131 
  132         if ((rc = sysctl_createv(clog, 0, NULL, NULL,
  133             CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
  134             NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
  135                 goto err;
  136         }
  137 
  138         if ((rc = sysctl_createv(clog, 0, NULL, &node,
  139             CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee1394if",
  140             SYSCTL_DESCR("ieee1394if controls"),
  141             NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
  142                 goto err;
  143         }
  144         ieee1394if_node_num = node->sysctl_num;
  145 
  146         /* ieee1394if try bus manager flag */
  147         if ((rc = sysctl_createv(clog, 0, NULL, &node,
  148             CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
  149             "try_bmr", SYSCTL_DESCR("Try to be a bus manager"),
  150             NULL, 0, &try_bmr,
  151             0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
  152                 goto err;
  153         }
  154 
  155         /* ieee1394if hold count */
  156         if ((rc = sysctl_createv(clog, 0, NULL, &node,
  157             CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
  158             "hold_count", SYSCTL_DESCR("Number of count of "
  159             "bus resets for removing lost device information"),
  160             NULL, 0, &hold_count,
  161             0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
  162                 goto err;
  163         }
  164 
  165         /* ieee1394if driver debug flag */
  166         if ((rc = sysctl_createv(clog, 0, NULL, &node,
  167             CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
  168             "ieee1394_debug", SYSCTL_DESCR("ieee1394if driver debug flag"),
  169             NULL, 0, &firewire_debug,
  170             0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) {
  171                 goto err;
  172         }
  173 
  174         return;
  175 
  176 err:
  177         printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
  178 }
  179 
  180 MALLOC_DEFINE(M_FW, "ieee1394", "IEEE1394");
  181 MALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/IEEE1394");
  182 #endif
  183 
  184 #define FW_MAXASYRTY 4
  185 
  186 #if defined(__FreeBSD__)
  187 devclass_t firewire_devclass;
  188 
  189 static void firewire_identify   (driver_t *, device_t);
  190 static int firewire_probe       (device_t);
  191 static int firewire_attach      (device_t);
  192 static int firewire_detach      (device_t);
  193 static int firewire_resume      (device_t);
  194 #if 0
  195 static int firewire_shutdown    (device_t);
  196 #endif
  197 static device_t firewire_add_child   (device_t, int, const char *, int);
  198 #elif defined(__NetBSD__)
  199 int firewirematch (device_t, struct cfdata *, void *);
  200 void firewireattach (device_t, device_t, void *);
  201 int firewiredetach (device_t, int);
  202 int firewire_print (void *, const char *);
  203 int firewire_resume (struct firewire_comm *);
  204 #endif
  205 static void firewire_xfer_timeout(void *, int);
  206 static void fw_try_bmr (void *);
  207 static void fw_try_bmr_callback (struct fw_xfer *);
  208 static void fw_asystart (struct fw_xfer *);
  209 static int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *);
  210 static void fw_bus_probe (struct firewire_comm *);
  211 static void fw_attach_dev (struct firewire_comm *);
  212 static void fw_bus_probe_thread(void *);
  213 #ifdef FW_VMACCESS
  214 static void fw_vmaccess (struct fw_xfer *);
  215 #endif
  216 static int fw_bmr (struct firewire_comm *);
  217 static void fw_dump_hdr(struct fw_pkt *, const char *);
  218 
  219 #if defined(__FreeBSD__)
  220 static device_method_t firewire_methods[] = {
  221         /* Device interface */
  222         DEVMETHOD(device_identify,      firewire_identify),
  223         DEVMETHOD(device_probe,         firewire_probe),
  224         DEVMETHOD(device_attach,        firewire_attach),
  225         DEVMETHOD(device_detach,        firewire_detach),
  226         DEVMETHOD(device_suspend,       bus_generic_suspend),
  227         DEVMETHOD(device_resume,        firewire_resume),
  228         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  229 
  230         /* Bus interface */
  231         DEVMETHOD(bus_add_child,        firewire_add_child),
  232         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  233 
  234         { 0, 0 }
  235 };
  236 #elif defined(__NetBSD__)
  237 CFATTACH_DECL_NEW(ieee1394if, sizeof(struct firewire_softc),
  238     firewirematch, firewireattach, firewiredetach, NULL);
  239 #endif
  240 const char *fw_linkspeed[] = {
  241         "S100", "S200", "S400", "S800",
  242         "S1600", "S3200", "undef", "undef"
  243 };
  244 
  245 static const char *tcode_str[] = {
  246         "WREQQ", "WREQB", "WRES",   "undef",
  247         "RREQQ", "RREQB", "RRESQ",  "RRESB",
  248         "CYCS",  "LREQ",  "STREAM", "LRES",
  249         "undef", "undef", "PHY",    "undef"
  250 };
  251 
  252 /* IEEE-1394a Table C-2 Gap count as a function of hops*/
  253 #define MAX_GAPHOP 15
  254 u_int gap_cnt[] = { 5,  5,  7,  8, 10, 13, 16, 18,
  255                    21, 24, 26, 29, 32, 35, 37, 40};
  256 
  257 #if defined(__FreeBSD__)
  258 static driver_t firewire_driver = {
  259         "firewire",
  260         firewire_methods,
  261         sizeof(struct firewire_softc),
  262 };
  263 #endif
  264 
  265 /*
  266  * Lookup fwdev by node id.
  267  */
  268 struct fw_device *
  269 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst)
  270 {
  271         struct fw_device *fwdev;
  272         int s;
  273 
  274         s = splfw();
  275         FW_GLOCK(fc);
  276         STAILQ_FOREACH(fwdev, &fc->devices, link)
  277                 if (fwdev->dst == dst && fwdev->status != FWDEVINVAL)
  278                         break;
  279         FW_GUNLOCK(fc);
  280         splx(s);
  281 
  282         return fwdev;
  283 }
  284 
  285 /*
  286  * Lookup fwdev by EUI64.
  287  */
  288 struct fw_device *
  289 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui)
  290 {
  291         struct fw_device *fwdev;
  292         int s;
  293 
  294         s = splfw();
  295         STAILQ_FOREACH(fwdev, &fc->devices, link)
  296                 if (FW_EUI64_EQUAL(fwdev->eui, *eui))
  297                         break;
  298         splx(s);
  299 
  300         if(fwdev == NULL) return NULL;
  301         if(fwdev->status == FWDEVINVAL) return NULL;
  302         return fwdev;
  303 }
  304 
  305 /*
  306  * Async. request procedure for userland application.
  307  */
  308 int
  309 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer)
  310 {
  311         int err = 0;
  312         struct fw_xferq *xferq;
  313         int len;
  314         struct fw_pkt *fp;
  315         int tcode;
  316         const struct tcode_info *info;
  317 
  318         if(xfer == NULL) return EINVAL;
  319         if(xfer->hand == NULL){
  320                 printf("hand == NULL\n");
  321                 return EINVAL;
  322         }
  323         fp = &xfer->send.hdr;
  324 
  325         tcode = fp->mode.common.tcode & 0xf;
  326         info = &fc->tcode[tcode];
  327         if (info->flag == 0) {
  328                 printf("invalid tcode=%x\n", tcode);
  329                 return EINVAL;
  330         }
  331 
  332         /* XXX allow bus explore packets only after bus rest */
  333         if ((fc->status < FWBUSEXPLORE) &&
  334             ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) ||
  335             (fp->mode.rreqq.dest_lo  < 0xf0000000) ||
  336             (fp->mode.rreqq.dest_lo >= 0xf0001000))) {
  337                 xfer->resp = EAGAIN;
  338                 xfer->flag = FWXF_BUSY;
  339                 return (EAGAIN);
  340         }
  341 
  342         if (info->flag & FWTI_REQ)
  343                 xferq = fc->atq;
  344         else
  345                 xferq = fc->ats;
  346         len = info->hdr_len;
  347         if (xfer->send.pay_len > MAXREC(fc->maxrec)) {
  348                 printf("send.pay_len > maxrec\n");
  349                 return EINVAL;
  350         }
  351         if (info->flag & FWTI_BLOCK_STR)
  352                 len = fp->mode.stream.len;
  353         else if (info->flag & FWTI_BLOCK_ASY)
  354                 len = fp->mode.rresb.len;
  355         else
  356                 len = 0;
  357         if (len != xfer->send.pay_len){
  358                 printf("len(%d) != send.pay_len(%d) %s(%x)\n",
  359                     len, xfer->send.pay_len, tcode_str[tcode], tcode);
  360                 return EINVAL; 
  361         }
  362 
  363         if(xferq->start == NULL){
  364                 printf("xferq->start == NULL\n");
  365                 return EINVAL;
  366         }
  367         if(!(xferq->queued < xferq->maxq)){
  368                 fw_printf(fc->bdev, "Discard a packet (queued=%d)\n",
  369                         xferq->queued);
  370                 return EAGAIN;
  371         }
  372 
  373         xfer->tl = -1;
  374         if (info->flag & FWTI_TLABEL) {
  375                 if (fw_get_tlabel(fc, xfer) < 0)
  376                         return EAGAIN;
  377         }
  378 
  379         xfer->resp = 0;
  380         xfer->fc = fc;
  381         xfer->q = xferq;
  382 
  383         fw_asystart(xfer);
  384         return err;
  385 }
  386 /*
  387  * Wakeup blocked process.
  388  */
  389 void
  390 fw_xferwake(struct fw_xfer *xfer)
  391 {
  392         fw_mtx_t lock = &xfer->fc->wait_lock;
  393 
  394         fw_mtx_lock(lock);
  395         xfer->flag |= FWXF_WAKE;
  396         fw_mtx_unlock(lock);
  397 
  398         wakeup(xfer);
  399         return;
  400 }
  401 
  402 int
  403 fw_xferwait(struct fw_xfer *xfer)
  404 {
  405         fw_mtx_t lock = &xfer->fc->wait_lock;
  406         int err = 0;
  407 
  408         fw_mtx_lock(lock);
  409         if ((xfer->flag & FWXF_WAKE) == 0)
  410                 err = fw_msleep((void *)xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0);
  411         fw_mtx_unlock(lock);
  412 
  413         return (err);
  414 }
  415 
  416 /*
  417  * Async. request with given xfer structure.
  418  */
  419 static void
  420 fw_asystart(struct fw_xfer *xfer)
  421 {
  422         struct firewire_comm *fc = xfer->fc;
  423         int s;
  424         s = splfw();
  425         /* Protect from interrupt/timeout */
  426         FW_GLOCK(fc);
  427         xfer->flag = FWXF_INQ;
  428         STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link);
  429 #if 0
  430         xfer->q->queued ++;
  431 #endif
  432         FW_GUNLOCK(fc);
  433         splx(s);
  434         /* XXX just queue for mbuf */
  435         if (xfer->mbuf == NULL)
  436                 xfer->q->start(fc);
  437         return;
  438 }
  439 
  440 #if defined(__FreeBSD__)
  441 static void
  442 firewire_identify(driver_t *driver, device_t parent)
  443 {
  444         BUS_ADD_CHILD(parent, 0, "firewire", -1);
  445 }
  446 
  447 static int
  448 firewire_probe(device_t dev)
  449 {
  450         device_set_desc(dev, "IEEE1394(FireWire) bus");
  451         return (0);
  452 }
  453 #elif defined(__NetBSD__)
  454 int
  455 firewirematch(device_t parent, struct cfdata *cf, void *aux)
  456 {
  457         struct fwbus_attach_args *faa = (struct fwbus_attach_args *)aux;
  458          
  459         if (strcmp(faa->name, "ieee1394if") == 0) 
  460                 return (1);
  461         return (0);
  462 }
  463 #endif
  464 
  465 static void
  466 firewire_xfer_timeout(void *arg, int pending)
  467 {
  468         struct firewire_comm *fc = (struct firewire_comm *)arg;
  469         struct fw_xfer *xfer, *txfer;
  470         struct timeval tv;
  471         struct timeval split_timeout;
  472         STAILQ_HEAD(, fw_xfer) xfer_timeout;
  473         int i, s;
  474 
  475         split_timeout.tv_sec = 0;
  476         split_timeout.tv_usec = 200 * 1000;      /* 200 msec */
  477 
  478         microtime(&tv);
  479         fw_timevalsub(&tv, &split_timeout);
  480         STAILQ_INIT(&xfer_timeout);
  481 
  482         s = splfw();
  483         fw_mtx_lock(&fc->tlabel_lock);
  484         for (i = 0; i < 0x40; i ++) {
  485                 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
  486                         if ((xfer->flag & FWXF_SENT) == 0)
  487                                 /* not sent yet */
  488                                 break;
  489                         if (fw_timevalcmp(&xfer->tv, &tv, >))
  490                                 /* the rests are newer than this */
  491                                 break;
  492                         fw_printf(fc->bdev,
  493                                 "split transaction timeout: "
  494                                 "tl=0x%x flag=0x%02x\n", i, xfer->flag);
  495                         fw_dump_hdr(&xfer->send.hdr, "send");
  496                         xfer->resp = ETIMEDOUT;
  497                         STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
  498                         STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel);
  499                 }
  500         }
  501         fw_mtx_unlock(&fc->tlabel_lock);
  502         splx(s);
  503         fc->timeout(fc);
  504 
  505         STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer)
  506             xfer->hand(xfer);
  507 }
  508 
  509 #define WATCHDOG_HZ 10
  510 static void
  511 firewire_watchdog(void *arg)
  512 {
  513         struct firewire_comm *fc;
  514         static int watchdog_clock = 0;
  515 
  516         fc = (struct firewire_comm *)arg;
  517 
  518         /*
  519          * At boot stage, the device interrupt is disabled and
  520          * We encounter a timeout easily. To avoid this,
  521          * ignore clock interrupt for a while.
  522          */
  523         if (watchdog_clock > WATCHDOG_HZ * 15)
  524                 fw_taskqueue_enqueue(fc->taskqueue, &fc->task_timeout);
  525         else
  526                 watchdog_clock ++;
  527 
  528         fw_callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ,
  529                         (void *)firewire_watchdog, (void *)fc);
  530 }
  531 
  532 /*
  533  * The attach routine.
  534  */
  535 FW_ATTACH(firewire)
  536 {
  537         FW_ATTACH_START(firewire, sc, fwa);
  538         FIREWIRE_ATTACH_START;
  539 
  540         sc->fc = fc;
  541         fc->status = FWBUSNOTREADY;
  542 
  543         if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA;
  544 
  545         FWDEV_MAKEDEV(sc);
  546 
  547         fw_mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF);
  548         fw_mtx_init(&fc->tlabel_lock, "fwtlabel", NULL, MTX_DEF);
  549         fw_callout_init(&fc->timeout_callout);
  550         fw_callout_init(&fc->bmr_callout);
  551         fw_callout_init(&fc->busprobe_callout);
  552         FW_TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, (void *)fc);
  553 
  554         fw_callout_reset(&sc->fc->timeout_callout, hz,
  555                         (void *)firewire_watchdog, (void *)sc->fc);
  556 
  557         /* create thread */
  558         fw_kthread_create(fw_bus_probe_thread, (void *)fc, &fc->probe_thread,
  559             "fw%d_probe", fw_get_unit(fc->bdev));
  560         fw_config_pending_incr();
  561 
  562         FIREWIRE_GENERIC_ATTACH;
  563 
  564         /* bus_reset */
  565         fw_busreset(fc, FWBUSNOTREADY);
  566         fc->ibr(fc);
  567 
  568 #ifdef __NetBSD__
  569         if (!pmf_device_register(self, NULL, NULL))
  570                 aprint_error_dev(self, "couldn't establish power handler\n");
  571 #endif
  572 
  573         FW_ATTACH_RETURN(0);
  574 }
  575 
  576 #if defined(__FreeBSD__)
  577 /*
  578  * Attach it as child.
  579  */
  580 static device_t
  581 firewire_add_child(device_t dev, int order, const char *name, int unit)
  582 {
  583         device_t child;
  584         struct firewire_softc *sc;
  585         struct fw_attach_args fwa;
  586 
  587         sc = (struct firewire_softc *)device_get_softc(dev);
  588         child = device_add_child(dev, name, unit);
  589         if (child) {
  590                 fwa.name = name;
  591                 fwa.fc = sc->fc;
  592                 fwa.fwdev = NULL;
  593                 device_set_ivars(child, &fwa);
  594                 device_probe_and_attach(child);
  595         }
  596 
  597         return child;
  598 }
  599 
  600 static int
  601 firewire_resume(device_t dev)
  602 {
  603         struct firewire_softc *sc;
  604 
  605         sc = (struct firewire_softc *)device_get_softc(dev);
  606         sc->fc->status = FWBUSNOTREADY;
  607         
  608         bus_generic_resume(dev);
  609 
  610         return(0);
  611 }
  612 #endif
  613 
  614 /*
  615  * Dettach it.
  616  */
  617 FW_DETACH(firewire)
  618 {
  619         FW_DETACH_START(firewire, sc);
  620         struct firewire_comm *fc;
  621         struct fw_device *fwdev, *fwdev_next;
  622 
  623         fc = sc->fc;
  624         fw_mtx_lock(&fc->wait_lock);
  625         fc->status = FWBUSDETACH;
  626         wakeup(fc);
  627         if (fw_msleep(fc->probe_thread,
  628             &fc->wait_lock, PWAIT, "fwthr", hz * 60))
  629                 printf("firewire probe thread didn't die\n");
  630         fw_mtx_unlock(&fc->wait_lock);
  631 
  632         FWDEV_DESTROYDEV(sc);
  633         FIREWIRE_GENERIC_DETACH;
  634 
  635         fw_callout_stop(&fc->timeout_callout);
  636         fw_callout_stop(&fc->bmr_callout);
  637         fw_callout_stop(&fc->busprobe_callout);
  638 
  639         /* XXX xfree_free and untimeout on all xfers */
  640         for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL;
  641                                                         fwdev = fwdev_next) {
  642                 fwdev_next = STAILQ_NEXT(fwdev, link);
  643                 free(fwdev, M_FW);
  644         }
  645         free(fc->topology_map, M_FW);
  646         free(fc->speed_map, M_FW);
  647         free(fc->crom_src_buf, M_FW);
  648 
  649         fw_mtx_destroy(&fc->wait_lock);
  650         fw_mtx_destroy(&fc->tlabel_lock);
  651         return(0);
  652 }
  653 #if defined(__FreeBSD__)
  654 #if 0
  655 static int
  656 firewire_shutdown( device_t dev )
  657 {
  658         return 0;
  659 }
  660 #endif
  661 #elif defined(__NetBSD__)
  662 int
  663 firewire_print(void *aux, const char *pnp)
  664 {
  665         struct fw_attach_args *fwa = (struct fw_attach_args *)aux;
  666 
  667         if (pnp)
  668                 aprint_normal("%s at %s", fwa->name, pnp);
  669 
  670         return UNCONF;
  671 }
  672 
  673 int
  674 firewire_resume(struct firewire_comm *fc)
  675 {
  676 
  677         fc->status = FWBUSNOTREADY;
  678         return 0;
  679 }
  680 #endif
  681 
  682 static void
  683 fw_xferq_drain(struct fw_xferq *xferq)
  684 {
  685         struct fw_xfer *xfer;
  686 
  687         while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
  688                 STAILQ_REMOVE_HEAD(&xferq->q, link);
  689 #if 0
  690                 xferq->queued --;
  691 #endif
  692                 xfer->resp = EAGAIN;
  693                 xfer->flag = FWXF_SENTERR;
  694                 fw_xfer_done(xfer);
  695         }
  696 }
  697 
  698 void
  699 fw_drain_txq(struct firewire_comm *fc)
  700 {
  701         struct fw_xfer *xfer, *txfer;
  702         STAILQ_HEAD(, fw_xfer) xfer_drain;
  703         int i;
  704 
  705         STAILQ_INIT(&xfer_drain);
  706 
  707         FW_GLOCK(fc);
  708         fw_xferq_drain(fc->atq);
  709         fw_xferq_drain(fc->ats);
  710         for(i = 0; i < fc->nisodma; i++)
  711                 fw_xferq_drain(fc->it[i]);
  712         FW_GUNLOCK(fc);
  713 
  714         fw_mtx_lock(&fc->tlabel_lock);
  715         for (i = 0; i < 0x40; i ++)
  716                 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) {
  717                         if (firewire_debug)
  718                                 printf("tl=%d flag=%d\n", i, xfer->flag);
  719                         xfer->resp = EAGAIN;
  720                         STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel);
  721                         STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel);
  722                 }
  723         fw_mtx_unlock(&fc->tlabel_lock);
  724 
  725         STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer)
  726                 xfer->hand(xfer);
  727 }
  728 
  729 static void
  730 fw_reset_csr(struct firewire_comm *fc)
  731 {
  732         int i;
  733 
  734         CSRARC(fc, STATE_CLEAR)
  735                         = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
  736         CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
  737         CSRARC(fc, NODE_IDS) = 0x3f;
  738 
  739         CSRARC(fc, TOPO_MAP + 8) = 0;
  740         fc->irm = -1;
  741 
  742         fc->max_node = -1;
  743 
  744         for(i = 2; i < 0x100/4 - 2 ; i++){
  745                 CSRARC(fc, SPED_MAP + i * 4) = 0;
  746         }
  747         CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ;
  748         CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
  749         CSRARC(fc, RESET_START) = 0;
  750         CSRARC(fc, SPLIT_TIMEOUT_HI) = 0;
  751         CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19;
  752         CSRARC(fc, CYCLE_TIME) = 0x0;
  753         CSRARC(fc, BUS_TIME) = 0x0;
  754         CSRARC(fc, BUS_MGR_ID) = 0x3f;
  755         CSRARC(fc, BANDWIDTH_AV) = 4915;
  756         CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff;
  757         CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff;
  758         CSRARC(fc, IP_CHANNELS) = (1 << 31);
  759 
  760         CSRARC(fc, CONF_ROM) = 0x04 << 24;
  761         CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */
  762         CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 |
  763                                 1 << 28 | 0xff << 16 | 0x09 << 8;
  764         CSRARC(fc, CONF_ROM + 0xc) = 0;
  765 
  766 /* DV depend CSRs see blue book */
  767         CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 
  768         CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 
  769 
  770         CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 );
  771         CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR);
  772 }
  773 
  774 static void
  775 fw_init_crom(struct firewire_comm *fc)
  776 {
  777         struct crom_src *src;
  778 
  779         fc->crom_src_buf = (struct crom_src_buf *)
  780                 malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO);
  781         if (fc->crom_src_buf == NULL)
  782                 return;
  783 
  784         src = &fc->crom_src_buf->src;
  785         bzero(src, sizeof(struct crom_src));
  786 
  787         /* BUS info sample */
  788         src->hdr.info_len = 4;
  789 
  790         src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
  791 
  792         src->businfo.irmc = 1;
  793         src->businfo.cmc = 1;
  794         src->businfo.isc = 1;
  795         src->businfo.bmc = 1;
  796         src->businfo.pmc = 0;
  797         src->businfo.cyc_clk_acc = 100;
  798         src->businfo.max_rec = fc->maxrec;
  799         src->businfo.max_rom = MAXROM_4;
  800         src->businfo.generation = 1;
  801         src->businfo.link_spd = fc->speed;
  802 
  803         src->businfo.eui64.hi = fc->eui.hi;
  804         src->businfo.eui64.lo = fc->eui.lo;
  805 
  806         STAILQ_INIT(&src->chunk_list);
  807 
  808         fc->crom_src = src;
  809         fc->crom_root = &fc->crom_src_buf->root;
  810 }
  811 
  812 static void
  813 fw_reset_crom(struct firewire_comm *fc)
  814 {
  815         struct crom_src_buf *buf;
  816         struct crom_src *src;
  817         struct crom_chunk *root;
  818 
  819         if (fc->crom_src_buf == NULL)
  820                 fw_init_crom(fc);
  821 
  822         buf =  fc->crom_src_buf;
  823         src = fc->crom_src;
  824         root = fc->crom_root;
  825 
  826         STAILQ_INIT(&src->chunk_list);
  827 
  828         bzero(root, sizeof(struct crom_chunk));
  829         crom_add_chunk(src, NULL, root, 0);
  830         crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
  831         /* private company_id */
  832         crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
  833         crom_add_simple_text(src, root, &buf->vendor, PROJECT_STR);
  834         crom_add_entry(root, CSRKEY_HW, OS_VER);
  835         crom_add_simple_text(src, root, &buf->hw, hostname);
  836 }
  837 
  838 /*
  839  * Called after bus reset.
  840  */
  841 void
  842 fw_busreset(struct firewire_comm *fc, uint32_t new_status)
  843 {
  844         struct firewire_dev_comm *fdc;
  845         struct crom_src *src;
  846         void *newrom;
  847 
  848         switch(fc->status){
  849         case FWBUSMGRELECT:
  850                 fw_callout_stop(&fc->bmr_callout);
  851                 break;
  852         default:
  853                 break;
  854         }
  855         fc->status = new_status;
  856         fw_reset_csr(fc);
  857         fw_reset_crom(fc);
  858 
  859         FIREWIRE_CHILDREN_FOREACH_FUNC(post_busreset, fdc);
  860 
  861         newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO);
  862         src = &fc->crom_src_buf->src;
  863         crom_load(src, (uint32_t *)newrom, CROMSIZE);
  864         if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) {
  865                 /* bump generation and reload */
  866                 src->businfo.generation ++;
  867                 /* generation must be between 0x2 and 0xF */
  868                 if (src->businfo.generation < 2)
  869                         src->businfo.generation ++;
  870                 crom_load(src, (uint32_t *)newrom, CROMSIZE);
  871                 bcopy(newrom, (void *)fc->config_rom, CROMSIZE);
  872         }
  873         free(newrom, M_FW);
  874 }
  875 
  876 /* Call once after reboot */
  877 void fw_init(struct firewire_comm *fc)
  878 {
  879         int i;
  880 #ifdef FW_VMACCESS
  881         struct fw_xfer *xfer;
  882         struct fw_bind *fwb;
  883 #endif
  884 
  885         fc->arq->queued = 0;
  886         fc->ars->queued = 0;
  887         fc->atq->queued = 0;
  888         fc->ats->queued = 0;
  889 
  890         fc->arq->buf = NULL;
  891         fc->ars->buf = NULL;
  892         fc->atq->buf = NULL;
  893         fc->ats->buf = NULL;
  894 
  895         fc->arq->flag = 0;
  896         fc->ars->flag = 0;
  897         fc->atq->flag = 0;
  898         fc->ats->flag = 0;
  899 
  900         STAILQ_INIT(&fc->atq->q);
  901         STAILQ_INIT(&fc->ats->q);
  902 
  903         for( i = 0 ; i < fc->nisodma ; i ++ ){
  904                 fc->it[i]->queued = 0;
  905                 fc->ir[i]->queued = 0;
  906 
  907                 fc->it[i]->start = NULL;
  908                 fc->ir[i]->start = NULL;
  909 
  910                 fc->it[i]->buf = NULL;
  911                 fc->ir[i]->buf = NULL;
  912 
  913                 fc->it[i]->flag = FWXFERQ_STREAM;
  914                 fc->ir[i]->flag = FWXFERQ_STREAM;
  915 
  916                 STAILQ_INIT(&fc->it[i]->q);
  917                 STAILQ_INIT(&fc->ir[i]->q);
  918         }
  919 
  920         fc->arq->maxq = FWMAXQUEUE;
  921         fc->ars->maxq = FWMAXQUEUE;
  922         fc->atq->maxq = FWMAXQUEUE;
  923         fc->ats->maxq = FWMAXQUEUE;
  924 
  925         for( i = 0 ; i < fc->nisodma ; i++){
  926                 fc->ir[i]->maxq = FWMAXQUEUE;
  927                 fc->it[i]->maxq = FWMAXQUEUE;
  928         }
  929 /* Initialize csr registers */
  930         fc->topology_map = (struct fw_topology_map *)malloc(
  931                                 sizeof(struct fw_topology_map),
  932                                 M_FW, M_NOWAIT | M_ZERO);
  933         fc->speed_map = (struct fw_speed_map *)malloc(
  934                                 sizeof(struct fw_speed_map),
  935                                 M_FW, M_NOWAIT | M_ZERO);
  936         CSRARC(fc, TOPO_MAP) = 0x3f1 << 16;
  937         CSRARC(fc, TOPO_MAP + 4) = 1;
  938         CSRARC(fc, SPED_MAP) = 0x3f1 << 16;
  939         CSRARC(fc, SPED_MAP + 4) = 1;
  940 
  941         STAILQ_INIT(&fc->devices);
  942 
  943 /* Initialize Async handlers */
  944         STAILQ_INIT(&fc->binds);
  945         for( i = 0 ; i < 0x40 ; i++){
  946                 STAILQ_INIT(&fc->tlabels[i]);
  947         }
  948 
  949 /* DV depend CSRs see blue book */
  950 #if 0
  951         CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */
  952         CSRARC(fc, oPCR) = 0x8000007a;
  953         for(i = 4 ; i < 0x7c/4 ; i+=4){
  954                 CSRARC(fc, i + oPCR) = 0x8000007a; 
  955         }
  956  
  957         CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */
  958         CSRARC(fc, iPCR) = 0x803f0000;
  959         for(i = 4 ; i < 0x7c/4 ; i+=4){
  960                 CSRARC(fc, i + iPCR) = 0x0; 
  961         }
  962 #endif
  963 
  964         fc->crom_src_buf = NULL;
  965 
  966 #ifdef FW_VMACCESS
  967         xfer = fw_xfer_alloc();
  968         if(xfer == NULL) return;
  969 
  970         fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT);
  971         if(fwb == NULL){
  972                 fw_xfer_free(xfer);
  973                 return;
  974         }
  975         xfer->hand = fw_vmaccess;
  976         xfer->fc = fc;
  977         xfer->sc = NULL;
  978 
  979         fwb->start_hi = 0x2;
  980         fwb->start_lo = 0;
  981         fwb->addrlen = 0xffffffff;
  982         fwb->xfer = xfer;
  983         fw_bindadd(fc, fwb);
  984 #endif
  985 }
  986 
  987 #define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\
  988     ((fwb)->end < (addr))?1:0)
  989 
  990 /*
  991  * To lookup bound process from IEEE1394 address.
  992  */
  993 struct fw_bind *
  994 fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo)
  995 {
  996         u_int64_t addr;
  997         struct fw_bind *tfw, *r = NULL;
  998 
  999         addr = ((u_int64_t)dest_hi << 32) | dest_lo;
 1000         FW_GLOCK(fc);
 1001         STAILQ_FOREACH(tfw, &fc->binds, fclist)
 1002                 if (BIND_CMP(addr, tfw) == 0) {
 1003                         r = tfw;
 1004                         break;
 1005                 }
 1006         FW_GUNLOCK(fc);
 1007         return(r);
 1008 }
 1009 
 1010 /*
 1011  * To bind IEEE1394 address block to process.
 1012  */
 1013 int
 1014 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb)
 1015 {
 1016         struct fw_bind *tfw, *prev = NULL;
 1017         int r = 0;
 1018 
 1019         if (fwb->start > fwb->end) {
 1020                 printf("%s: invalid range\n", __func__);
 1021                 return EINVAL;
 1022         }
 1023 
 1024         FW_GLOCK(fc);
 1025         STAILQ_FOREACH(tfw, &fc->binds, fclist) {
 1026                 if (fwb->end < tfw->start)
 1027                         break;
 1028                 prev = tfw;
 1029         }
 1030         if (prev == NULL)
 1031                 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist);
 1032         else if (prev->end < fwb->start)
 1033                 STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist);
 1034         else {
 1035                 printf("%s: bind failed\n", __func__);
 1036                 r = EBUSY;
 1037         }
 1038         FW_GUNLOCK(fc);
 1039         return (r);
 1040 }
 1041 
 1042 /*
 1043  * To free IEEE1394 address block.
 1044  */
 1045 int
 1046 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb)
 1047 {
 1048 #if 0
 1049         struct fw_xfer *xfer, *next;
 1050 #endif
 1051         struct fw_bind *tfw;
 1052         int s;
 1053 
 1054         s = splfw();
 1055         FW_GLOCK(fc);
 1056         STAILQ_FOREACH(tfw, &fc->binds, fclist)
 1057                 if (tfw == fwb) {
 1058                         STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist);
 1059                         goto found;
 1060                 }
 1061 
 1062         printf("%s: no such binding\n", __func__);
 1063         FW_GUNLOCK(fc);
 1064         splx(s);
 1065         return (1);
 1066 found:
 1067 #if 0
 1068         /* shall we do this? */
 1069         for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) {
 1070                 next = STAILQ_NEXT(xfer, link);
 1071                 fw_xfer_free(xfer);
 1072         }
 1073         STAILQ_INIT(&fwb->xferlist);
 1074 #endif
 1075         FW_GUNLOCK(fc);
 1076 
 1077         splx(s);
 1078         return 0;
 1079 }
 1080 
 1081 int
 1082 fw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type,
 1083     int slen, int rlen, int n,
 1084     struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *))
 1085 {
 1086         int i, s;
 1087         struct fw_xfer *xfer;
 1088 
 1089         for (i = 0; i < n; i++) {
 1090                 xfer = fw_xfer_alloc_buf(type, slen, rlen);
 1091                 if (xfer == NULL)
 1092                         return (n);
 1093                 xfer->fc = fc;
 1094                 xfer->sc = sc;
 1095                 xfer->hand = hand;
 1096                 s = splfw();
 1097                 STAILQ_INSERT_TAIL(q, xfer, link);
 1098                 splx(s);
 1099         }
 1100         return (n);
 1101 }
 1102 
 1103 void
 1104 fw_xferlist_remove(struct fw_xferlist *q)
 1105 {
 1106         struct fw_xfer *xfer, *next;
 1107 
 1108         for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) {
 1109                 next = STAILQ_NEXT(xfer, link);
 1110                 fw_xfer_free_buf(xfer);
 1111         }
 1112         STAILQ_INIT(q);
 1113 }
 1114 
 1115 /*
 1116  * dump packet header
 1117  */
 1118 static void
 1119 fw_dump_hdr(struct fw_pkt *fp, const char *prefix)
 1120 {
 1121         printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x "
 1122             "src=0x%03x\n", prefix,
 1123              fp->mode.hdr.dst & 0x3f,
 1124              fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3,
 1125              fp->mode.hdr.tcode, fp->mode.hdr.pri,
 1126              fp->mode.hdr.src);
 1127 }
 1128 
 1129 /*
 1130  * To free transaction label.
 1131  */
 1132 static void
 1133 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer)
 1134 {
 1135         struct fw_xfer *txfer;
 1136         int s;
 1137 
 1138         if (xfer->tl < 0)
 1139                 return;
 1140 
 1141         s = splfw();
 1142         fw_mtx_lock(&fc->tlabel_lock);
 1143 #if 1 /* make sure the label is allocated */
 1144         STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel)
 1145                 if(txfer == xfer)
 1146                         break;
 1147         if (txfer == NULL) {
 1148                 printf("%s: the xfer is not in the queue "
 1149                     "(tlabel=%d, flag=0x%x)\n",
 1150                     __func__, xfer->tl, xfer->flag);
 1151                 fw_dump_hdr(&xfer->send.hdr, "send");
 1152                 fw_dump_hdr(&xfer->recv.hdr, "recv");
 1153                 kdb_backtrace();
 1154                 fw_mtx_unlock(&fc->tlabel_lock);
 1155                 splx(s);
 1156                 return;
 1157         }
 1158 #endif
 1159 
 1160         STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel);
 1161         fw_mtx_unlock(&fc->tlabel_lock);
 1162         splx(s);
 1163         return;
 1164 }
 1165 
 1166 /*
 1167  * To obtain XFER structure by transaction label.
 1168  */
 1169 static struct fw_xfer *
 1170 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode)
 1171 {
 1172         struct fw_xfer *xfer;
 1173         int s = splfw();
 1174         int req;
 1175 
 1176         fw_mtx_lock(&fc->tlabel_lock);
 1177         STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel)
 1178                 if(xfer->send.hdr.mode.hdr.dst == node) {
 1179                         fw_mtx_unlock(&fc->tlabel_lock);
 1180                         splx(s);
 1181                         FW_KASSERT(xfer->tl == tlabel,
 1182                             ("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel));
 1183                         /* extra sanity check */
 1184                         req = xfer->send.hdr.mode.hdr.tcode;
 1185                         if (xfer->fc->tcode[req].valid_res != tcode) {
 1186                                 printf("%s: invalid response tcode "
 1187                                     "(0x%x for 0x%x)\n", __func__,
 1188                                     tcode, req);
 1189                                 return(NULL);
 1190                         }
 1191 
 1192                         if (firewire_debug > 2)
 1193                                 printf("fw_tl2xfer: found tl=%d\n", tlabel);
 1194                         return(xfer);
 1195                 }
 1196         fw_mtx_unlock(&fc->tlabel_lock);
 1197         if (firewire_debug > 1)
 1198                 printf("fw_tl2xfer: not found tl=%d\n", tlabel);
 1199         splx(s);
 1200         return(NULL);
 1201 }
 1202 
 1203 /*
 1204  * To allocate IEEE1394 XFER structure.
 1205  */
 1206 struct fw_xfer *
 1207 fw_xfer_alloc(struct malloc_type *type)
 1208 {
 1209         struct fw_xfer *xfer;
 1210 
 1211         xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO);
 1212         if (xfer == NULL)
 1213                 return xfer;
 1214 
 1215         xfer->malloc = type;
 1216 
 1217         return xfer;
 1218 }
 1219 
 1220 struct fw_xfer *
 1221 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len)
 1222 {
 1223         struct fw_xfer *xfer;
 1224 
 1225         xfer = fw_xfer_alloc(type);
 1226         if (xfer == NULL)
 1227                 return(NULL);
 1228         xfer->send.pay_len = send_len;
 1229         xfer->recv.pay_len = recv_len;
 1230         if (send_len > 0) {
 1231                 xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO);
 1232                 if (xfer->send.payload == NULL) {
 1233                         fw_xfer_free(xfer);
 1234                         return(NULL);
 1235                 }
 1236         }
 1237         if (recv_len > 0) {
 1238                 xfer->recv.payload = malloc(recv_len, type, M_NOWAIT);
 1239                 if (xfer->recv.payload == NULL) {
 1240                         if (xfer->send.payload != NULL)
 1241                                 free(xfer->send.payload, type);
 1242                         fw_xfer_free(xfer);
 1243                         return(NULL);
 1244                 }
 1245         }
 1246         return(xfer);
 1247 }
 1248 
 1249 /*
 1250  * IEEE1394 XFER post process.
 1251  */
 1252 void
 1253 fw_xfer_done(struct fw_xfer *xfer)
 1254 {
 1255         if (xfer->hand == NULL) {
 1256                 printf("hand == NULL\n");
 1257                 return;
 1258         }
 1259 
 1260         if (xfer->fc == NULL)
 1261                 panic("fw_xfer_done: why xfer->fc is NULL?");
 1262 
 1263         fw_tl_free(xfer->fc, xfer);
 1264         xfer->hand(xfer);
 1265 }
 1266 
 1267 void
 1268 fw_xfer_unload(struct fw_xfer* xfer)
 1269 {
 1270         int s;
 1271 
 1272         if(xfer == NULL ) return;
 1273         if(xfer->flag & FWXF_INQ){
 1274                 printf("fw_xfer_free FWXF_INQ\n");
 1275                 s = splfw();
 1276                 FW_GLOCK(xfer->fc);
 1277                 STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link);
 1278 #if 0
 1279                 xfer->q->queued --;
 1280 #endif
 1281                 FW_GUNLOCK(xfer->fc);
 1282                 splx(s);
 1283         }
 1284         if (xfer->fc != NULL) {
 1285 #if 1
 1286                 if(xfer->flag == FWXF_START)
 1287                         /*
 1288                          * This could happen if:
 1289                          *  1. We call fwohci_arcv() before fwohci_txd().
 1290                          *  2. firewire_watch() is called.
 1291                          */
 1292                         printf("fw_xfer_free FWXF_START\n");
 1293 #endif
 1294         }
 1295         xfer->flag = FWXF_INIT;
 1296         xfer->resp = 0;
 1297 }
 1298 /*
 1299  * To free IEEE1394 XFER structure. 
 1300  */
 1301 void
 1302 fw_xfer_free_buf( struct fw_xfer* xfer)
 1303 {
 1304         if (xfer == NULL) {
 1305                 printf("%s: xfer == NULL\n", __func__);
 1306                 return;
 1307         }
 1308         fw_xfer_unload(xfer);
 1309         if(xfer->send.payload != NULL){
 1310                 free(xfer->send.payload, xfer->malloc);
 1311         }
 1312         if(xfer->recv.payload != NULL){
 1313                 free(xfer->recv.payload, xfer->malloc);
 1314         }
 1315         free(xfer, xfer->malloc);
 1316 }
 1317 
 1318 void
 1319 fw_xfer_free( struct fw_xfer* xfer)
 1320 {
 1321         if (xfer == NULL) {
 1322                 printf("%s: xfer == NULL\n", __func__);
 1323                 return;
 1324         }
 1325         fw_xfer_unload(xfer);
 1326         free(xfer, xfer->malloc);
 1327 }
 1328 
 1329 void
 1330 fw_asy_callback_free(struct fw_xfer *xfer)
 1331 {
 1332 #if 0
 1333         printf("asyreq done flag=%d resp=%d\n",
 1334                                 xfer->flag, xfer->resp);
 1335 #endif
 1336         fw_xfer_free(xfer);
 1337 }
 1338 
 1339 /*
 1340  * To configure PHY. 
 1341  */
 1342 static void
 1343 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count)
 1344 {
 1345         struct fw_xfer *xfer;
 1346         struct fw_pkt *fp;
 1347 
 1348         fc->status = FWBUSPHYCONF;
 1349 
 1350         xfer = fw_xfer_alloc(M_FWXFER);
 1351         if (xfer == NULL)
 1352                 return;
 1353         xfer->fc = fc;
 1354         xfer->hand = fw_asy_callback_free;
 1355 
 1356         fp = &xfer->send.hdr;
 1357         fp->mode.ld[1] = 0;
 1358         if (root_node >= 0)
 1359                 fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23;
 1360         if (gap_count >= 0)
 1361                 fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16;
 1362         fp->mode.ld[2] = ~fp->mode.ld[1];
 1363 /* XXX Dangerous, how to pass PHY packet to device driver */
 1364         fp->mode.common.tcode |= FWTCODE_PHY;
 1365 
 1366         if (firewire_debug)
 1367                 printf("send phy_config root_node=%d gap_count=%d\n",
 1368                                                 root_node, gap_count);
 1369         fw_asyreq(fc, -1, xfer);
 1370 }
 1371 
 1372 #if 0
 1373 /*
 1374  * Dump self ID. 
 1375  */
 1376 static void
 1377 fw_print_sid(uint32_t sid)
 1378 {
 1379         union fw_self_id *s;
 1380         s = (union fw_self_id *) &sid;
 1381         printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d"
 1382                 " p0:%d p1:%d p2:%d i:%d m:%d\n",
 1383                 s->p0.phy_id, s->p0.link_active, s->p0.gap_count,
 1384                 s->p0.phy_speed, s->p0.phy_delay, s->p0.contender,
 1385                 s->p0.power_class, s->p0.port0, s->p0.port1,
 1386                 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets);
 1387 }
 1388 #endif
 1389 
 1390 /*
 1391  * To receive self ID. 
 1392  */
 1393 void fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len)
 1394 {
 1395         uint32_t *p;
 1396         union fw_self_id *self_id;
 1397         u_int i, j, node, c_port = 0, i_branch = 0;
 1398 
 1399         fc->sid_cnt = len /(sizeof(uint32_t) * 2);
 1400         fc->max_node = fc->nodeid & 0x3f;
 1401         CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16;
 1402         fc->status = FWBUSCYMELECT;
 1403         fc->topology_map->crc_len = 2;
 1404         fc->topology_map->generation ++;
 1405         fc->topology_map->self_id_count = 0;
 1406         fc->topology_map->node_count = 0;
 1407         fc->speed_map->generation ++;
 1408         fc->speed_map->crc_len = 1 + (64*64 + 3) / 4;
 1409         self_id = &fc->topology_map->self_id[0];
 1410         for(i = 0; i < fc->sid_cnt; i ++){
 1411                 if (sid[1] != ~sid[0]) {
 1412                         printf("fw_sidrcv: invalid self-id packet\n");
 1413                         sid += 2;
 1414                         continue;
 1415                 }
 1416                 *self_id = *((union fw_self_id *)sid);
 1417                 fc->topology_map->crc_len++;
 1418                 if(self_id->p0.sequel == 0){
 1419                         fc->topology_map->node_count ++;
 1420                         c_port = 0;
 1421 #if 0
 1422                         fw_print_sid(sid[0]);
 1423 #endif
 1424                         node = self_id->p0.phy_id;
 1425                         if(fc->max_node < node){
 1426                                 fc->max_node = self_id->p0.phy_id;
 1427                         }
 1428                         /* XXX I'm not sure this is the right speed_map */
 1429                         fc->speed_map->speed[node][node]
 1430                                         = self_id->p0.phy_speed;
 1431                         for (j = 0; j < node; j ++) {
 1432                                 fc->speed_map->speed[j][node]
 1433                                         = fc->speed_map->speed[node][j]
 1434                                         = min(fc->speed_map->speed[j][j],
 1435                                                         self_id->p0.phy_speed);
 1436                         }
 1437                         if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) &&
 1438                           (self_id->p0.link_active && self_id->p0.contender)) {
 1439                                 fc->irm = self_id->p0.phy_id;
 1440                         }
 1441                         if(self_id->p0.port0 >= 0x2){
 1442                                 c_port++;
 1443                         }
 1444                         if(self_id->p0.port1 >= 0x2){
 1445                                 c_port++;
 1446                         }
 1447                         if(self_id->p0.port2 >= 0x2){
 1448                                 c_port++;
 1449                         }
 1450                 }
 1451                 if(c_port > 2){
 1452                         i_branch += (c_port - 2);
 1453                 }
 1454                 sid += 2;
 1455                 self_id++;
 1456                 fc->topology_map->self_id_count ++;
 1457         }
 1458         fw_printf(fc->bdev, "%d nodes", fc->max_node + 1);
 1459         /* CRC */
 1460         fc->topology_map->crc = fw_crc16(
 1461                         (uint32_t *)&fc->topology_map->generation,
 1462                         fc->topology_map->crc_len * 4);
 1463         fc->speed_map->crc = fw_crc16(
 1464                         (uint32_t *)&fc->speed_map->generation,
 1465                         fc->speed_map->crc_len * 4);
 1466         /* byteswap and copy to CSR */
 1467         p = (uint32_t *)fc->topology_map;
 1468         for (i = 0; i <= fc->topology_map->crc_len; i++)
 1469                 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++);
 1470         p = (uint32_t *)fc->speed_map;
 1471         CSRARC(fc, SPED_MAP) = htonl(*p++);
 1472         CSRARC(fc, SPED_MAP + 4) = htonl(*p++);
 1473         /* don't byte-swap uint8_t array */
 1474         bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4);
 1475 
 1476         fc->max_hop = fc->max_node - i_branch;
 1477         aprint_normal(", maxhop <= %d", fc->max_hop);
 1478                 
 1479         if(fc->irm == -1 ){
 1480                 aprint_normal(", Not found IRM capable node");
 1481         }else{
 1482                 aprint_normal(", cable IRM = %d", fc->irm);
 1483                 if (fc->irm == fc->nodeid)
 1484                         aprint_normal(" (me)");
 1485         }
 1486         aprint_normal("\n");
 1487 
 1488         if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) {
 1489                 if (fc->irm == fc->nodeid) {
 1490                         fc->status = FWBUSMGRDONE;
 1491                         CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm);
 1492                         fw_bmr(fc);
 1493                 } else {
 1494                         fc->status = FWBUSMGRELECT;
 1495                         fw_callout_reset(&fc->bmr_callout, hz/8,
 1496                                 (void *)fw_try_bmr, (void *)fc);
 1497                 }
 1498         } else
 1499                 fc->status = FWBUSMGRDONE;
 1500 
 1501         fw_callout_reset(&fc->busprobe_callout, hz/4,
 1502                         (void *)fw_bus_probe, (void *)fc);
 1503 }
 1504 
 1505 /*
 1506  * To probe devices on the IEEE1394 bus. 
 1507  */
 1508 static void
 1509 fw_bus_probe(struct firewire_comm *fc)
 1510 {
 1511         int s;
 1512         struct fw_device *fwdev;
 1513 
 1514         s = splfw();
 1515         fc->status = FWBUSEXPLORE;
 1516 
 1517         /* Invalidate all devices, just after bus reset. */
 1518         STAILQ_FOREACH(fwdev, &fc->devices, link)
 1519                 if (fwdev->status != FWDEVINVAL) {
 1520                         fwdev->status = FWDEVINVAL;
 1521                         fwdev->rcnt = 0;
 1522                 }
 1523         splx(s);
 1524 
 1525         wakeup((void *)fc);
 1526 }
 1527 
 1528 static int
 1529 fw_explore_read_quads(struct fw_device *fwdev, int offset,
 1530     uint32_t *quad, int n)
 1531 {
 1532         struct fw_xfer *xfer;
 1533         uint32_t tmp;
 1534         int i, error;
 1535 
 1536 
 1537         for (i = 0; i < n; i ++, offset += sizeof(uint32_t)) {
 1538                 xfer = fwmem_read_quad(fwdev, NULL, -1,
 1539                     0xffff, 0xf0000000 | offset, (void *)&tmp,
 1540                     fw_xferwake);
 1541                 if (xfer == NULL)
 1542                         return (-1);
 1543                 fw_xferwait(xfer);
 1544 
 1545                 if (xfer->resp == 0)
 1546                         quad[i] = ntohl(tmp);
 1547 
 1548                 error = xfer->resp;
 1549                 fw_xfer_free(xfer);
 1550                 if (error)
 1551                         return (error);
 1552         }
 1553         return (0);
 1554 }
 1555 
 1556 
 1557 static int
 1558 fw_explore_csrblock(struct fw_device *fwdev, int offset, int recur)
 1559 {
 1560         int err, i, off;
 1561         struct csrdirectory *dir;
 1562         struct csrreg *reg;
 1563 
 1564 
 1565         dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)];
 1566         err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
 1567             (uint32_t *)dir, 1);
 1568         if (err)
 1569                 return (-1);
 1570 
 1571         offset += sizeof(uint32_t);
 1572         reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)];
 1573         err = fw_explore_read_quads(fwdev, CSRROMOFF + offset,
 1574             (uint32_t *)reg, dir->crc_len);
 1575         if (err)
 1576                 return (-1);
 1577 
 1578         /* XXX check CRC */
 1579 
 1580         off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1);
 1581         if (fwdev->rommax < off)
 1582                 fwdev->rommax = off;
 1583 
 1584         if (recur == 0)
 1585                 return (0);
 1586 
 1587         for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) {
 1588                 if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D)
 1589                         recur = 1;
 1590                 else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L)
 1591                         recur = 0;
 1592                 else
 1593                         continue;
 1594 
 1595                 off = offset + reg[i].val * sizeof(uint32_t);
 1596                 if (off > CROMSIZE) {
 1597                         printf("%s: invalid offset %d\n", __func__, off);
 1598                         return(-1);
 1599                 }
 1600                 err = fw_explore_csrblock(fwdev, off, recur);
 1601                 if (err)
 1602                         return (-1);
 1603         }
 1604         return (0);
 1605 }
 1606 
 1607 static int
 1608 fw_explore_node(struct fw_device *dfwdev)
 1609 {
 1610         struct firewire_comm *fc;
 1611         struct fw_device *fwdev, *pfwdev, *tfwdev;
 1612         uint32_t *csr;
 1613         struct csrhdr *hdr;
 1614         struct bus_info *binfo;
 1615         int err, node, spd;
 1616 
 1617         fc = dfwdev->fc;
 1618         csr = dfwdev->csrrom;
 1619         node = dfwdev->dst;
 1620 
 1621         /* First quad */
 1622         err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1);
 1623         if (err)
 1624                 return (-1);
 1625         hdr = (struct csrhdr *)&csr[0];
 1626         if (hdr->info_len != 4) {
 1627                 if (firewire_debug)
 1628                         printf("node%d: wrong bus info len(%d)\n",
 1629                             node, hdr->info_len);
 1630                 return (-1);
 1631         }
 1632 
 1633         /* bus info */
 1634         err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4);
 1635         if (err)
 1636                 return (-1);
 1637         binfo = (struct bus_info *)&csr[1];
 1638         if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) {
 1639                 if (firewire_debug)
 1640                         printf("node%d: invalid bus name 0x%08x\n",
 1641                             node, binfo->bus_name);
 1642                 return (-1);
 1643         }
 1644         spd = fc->speed_map->speed[fc->nodeid][node];
 1645         STAILQ_FOREACH(fwdev, &fc->devices, link)
 1646                 if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64))
 1647                         break;
 1648         if (fwdev == NULL) {
 1649                 /* new device */
 1650                 fwdev = malloc(sizeof(struct fw_device), M_FW,
 1651                                                 M_NOWAIT | M_ZERO);
 1652                 if (fwdev == NULL) {
 1653                         if (firewire_debug)
 1654                                 printf("node%d: no memory\n", node);
 1655                         return (-1);
 1656                 }
 1657                 fwdev->fc = fc;
 1658                 fwdev->eui = binfo->eui64;
 1659                 fwdev->status = FWDEVNEW;
 1660                 /* insert into sorted fwdev list */
 1661                 pfwdev = NULL;
 1662                 STAILQ_FOREACH(tfwdev, &fc->devices, link) {
 1663                         if (tfwdev->eui.hi > fwdev->eui.hi ||
 1664                                 (tfwdev->eui.hi == fwdev->eui.hi &&
 1665                                 tfwdev->eui.lo > fwdev->eui.lo))
 1666                                 break;
 1667                         pfwdev = tfwdev;
 1668                 }
 1669                 if (pfwdev == NULL)
 1670                         STAILQ_INSERT_HEAD(&fc->devices, fwdev, link);
 1671                 else
 1672                         STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link);
 1673 
 1674                 fw_printf(fc->bdev, "New %s device ID:%08x%08x\n",
 1675                     fw_linkspeed[spd],
 1676                     fwdev->eui.hi, fwdev->eui.lo);
 1677 
 1678         } else
 1679                 fwdev->status = FWDEVINIT;
 1680         fwdev->dst = node;
 1681         fwdev->speed = spd;
 1682 
 1683         /* unchanged ? */
 1684         if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) {
 1685                 if (firewire_debug)
 1686                         printf("node%d: crom unchanged\n", node);
 1687                 return (0);
 1688         }
 1689 
 1690         bzero(&fwdev->csrrom[0], CROMSIZE);
 1691 
 1692         /* copy first quad and bus info block */
 1693         bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5);
 1694         fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4;
 1695 
 1696         err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */
 1697 
 1698         if (err) {
 1699                 fwdev->status = FWDEVINVAL;
 1700                 fwdev->csrrom[0] = 0;
 1701         }
 1702         return (err);
 1703 
 1704 }
 1705 
 1706 /*
 1707  * Find the self_id packet for a node, ignoring sequels.
 1708  */
 1709 static union fw_self_id *
 1710 fw_find_self_id(struct firewire_comm *fc, int node)
 1711 {
 1712         uint32_t i;
 1713         union fw_self_id *s;
 1714         
 1715         for (i = 0; i < fc->topology_map->self_id_count; i++) {
 1716                 s = &fc->topology_map->self_id[i];
 1717                 if (s->p0.sequel)
 1718                         continue;
 1719                 if (s->p0.phy_id == node)
 1720                         return s;
 1721         }
 1722         return 0;
 1723 }
 1724 
 1725 static void
 1726 fw_explore(struct firewire_comm *fc)
 1727 {
 1728         int node, err, s, i, todo, todo2, trys;
 1729         char nodes[63];
 1730         struct fw_device *dfwdev;
 1731         union fw_self_id *fwsid;
 1732 
 1733         todo = 0;
 1734         dfwdev = malloc(sizeof(*dfwdev), M_TEMP, M_NOWAIT);
 1735         if (dfwdev == NULL)
 1736                 return;
 1737         /* setup dummy fwdev */
 1738         dfwdev->fc = fc;
 1739         dfwdev->speed = 0;
 1740         dfwdev->maxrec = 8; /* 512 */
 1741         dfwdev->status = FWDEVINIT;
 1742 
 1743         for (node = 0; node <= fc->max_node; node ++) {
 1744                 /* We don't probe myself and linkdown nodes */
 1745                 if (node == fc->nodeid)
 1746                         continue;
 1747                 fwsid = fw_find_self_id(fc, node);
 1748                 if (!fwsid || !fwsid->p0.link_active) {
 1749                         if (firewire_debug)
 1750                                 printf("node%d: link down\n", node);
 1751                         continue;
 1752                 }
 1753                 nodes[todo++] = node;
 1754         }
 1755 
 1756         s = splfw();
 1757         for (trys = 0; todo > 0 && trys < 3; trys ++) {
 1758                 todo2 = 0;
 1759                 for (i = 0; i < todo; i ++) {
 1760                         dfwdev->dst = nodes[i];
 1761                         err = fw_explore_node(dfwdev);
 1762                         if (err)
 1763                                 nodes[todo2++] = nodes[i];
 1764                         if (firewire_debug)
 1765                                 printf("%s: node %d, err = %d\n",
 1766                                         __func__, node, err);
 1767                 }
 1768                 todo = todo2;
 1769         }
 1770         splx(s);
 1771         free(dfwdev, M_TEMP);
 1772 }
 1773 
 1774 static void
 1775 fw_bus_probe_thread(void *arg)
 1776 {
 1777         struct firewire_comm *fc;
 1778 
 1779         fc = (struct firewire_comm *)arg;
 1780 
 1781         fw_config_pending_decr();
 1782 
 1783         fw_mtx_lock(&fc->wait_lock);
 1784         while (fc->status != FWBUSDETACH) {
 1785                 if (fc->status == FWBUSEXPLORE) {
 1786                         fw_mtx_unlock(&fc->wait_lock);
 1787                         fw_explore(fc);
 1788                         fc->status = FWBUSEXPDONE;
 1789                         if (firewire_debug)
 1790                                 printf("bus_explore done\n");
 1791                         fw_attach_dev(fc);
 1792                         fw_mtx_lock(&fc->wait_lock);
 1793                 }
 1794                 fw_msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0);
 1795         }
 1796         fw_mtx_unlock(&fc->wait_lock);
 1797         fw_kthread_exit(0);
 1798 }
 1799 
 1800 
 1801 /*
 1802  * To attach sub-devices layer onto IEEE1394 bus.
 1803  */
 1804 static void
 1805 fw_attach_dev(struct firewire_comm *fc)
 1806 {
 1807         struct fw_device *fwdev, *next;
 1808         struct firewire_dev_comm *fdc;
 1809         struct fw_attach_args fwa;
 1810 
 1811         fwa.name = "sbp";
 1812         fwa.fc = fc;
 1813 
 1814         for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
 1815                 next = STAILQ_NEXT(fwdev, link);
 1816                 switch (fwdev->status) {
 1817                 case FWDEVNEW:
 1818                         FIREWIRE_SBP_ATTACH;
 1819 
 1820                 case FWDEVINIT:
 1821                 case FWDEVATTACHED:
 1822                         fwdev->status = FWDEVATTACHED;
 1823                         break;
 1824 
 1825                 case FWDEVINVAL:
 1826                         fwdev->rcnt ++;
 1827                         break;
 1828 
 1829                 default:
 1830                         /* XXX */
 1831                         break;
 1832                 }
 1833         }
 1834 
 1835         FIREWIRE_CHILDREN_FOREACH_FUNC(post_explore, fdc);
 1836 
 1837         for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) {
 1838                 next = STAILQ_NEXT(fwdev, link);
 1839                 if (fwdev->rcnt > 0 && fwdev->rcnt > hold_count) {
 1840                         /*
 1841                          * Remove devices which have not been seen
 1842                          * for a while.
 1843                          */
 1844                         FIREWIRE_SBP_DETACH;
 1845                         STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link);
 1846                         free(fwdev, M_FW);
 1847                 }
 1848         }
 1849 
 1850         return;
 1851 }
 1852 
 1853 /*
 1854  * To allocate unique transaction label.
 1855  */
 1856 static int
 1857 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer)
 1858 {
 1859         u_int dst, new_tlabel;
 1860         struct fw_xfer *txfer;
 1861         int s;
 1862 
 1863         dst = xfer->send.hdr.mode.hdr.dst & 0x3f;
 1864         s = splfw();
 1865         fw_mtx_lock(&fc->tlabel_lock);
 1866         new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f;
 1867         STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel)
 1868                 if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst)
 1869                         break;
 1870         if(txfer == NULL) {
 1871                 fc->last_tlabel[dst] = new_tlabel;
 1872                 STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel);
 1873                 fw_mtx_unlock(&fc->tlabel_lock);
 1874                 splx(s);
 1875                 xfer->tl = new_tlabel;
 1876                 xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2;
 1877                 if (firewire_debug > 1)
 1878                         printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel);
 1879                 return (new_tlabel);
 1880         }
 1881         fw_mtx_unlock(&fc->tlabel_lock);
 1882         splx(s);
 1883 
 1884         if (firewire_debug > 1)
 1885                 printf("fw_get_tlabel: no free tlabel\n");
 1886         return (-1);
 1887 }
 1888 
 1889 static void
 1890 fw_rcv_copy(struct fw_rcv_buf *rb)
 1891 {
 1892         struct fw_pkt *pkt;
 1893         u_char *p;
 1894         const struct tcode_info *tinfo;
 1895         u_int res, i, len, plen;
 1896 
 1897         rb->xfer->recv.spd = rb->spd;
 1898 
 1899         pkt = (struct fw_pkt *)rb->vec->iov_base;
 1900         tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode];
 1901 
 1902         /* Copy header */ 
 1903         p = (u_char *)&rb->xfer->recv.hdr;
 1904         bcopy(rb->vec->iov_base, p, tinfo->hdr_len);
 1905         rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len;
 1906         rb->vec->iov_len -= tinfo->hdr_len;
 1907 
 1908         /* Copy payload */
 1909         p = (u_char *)rb->xfer->recv.payload;
 1910         res = rb->xfer->recv.pay_len;
 1911 
 1912         /* special handling for RRESQ */
 1913         if (pkt->mode.hdr.tcode == FWTCODE_RRESQ &&
 1914             p != NULL && res >= sizeof(uint32_t)) {
 1915                 *(uint32_t *)p = pkt->mode.rresq.data;
 1916                 rb->xfer->recv.pay_len = sizeof(uint32_t);
 1917                 return;
 1918         }
 1919 
 1920         if ((tinfo->flag & FWTI_BLOCK_ASY) == 0)
 1921                 return;
 1922 
 1923         plen = pkt->mode.rresb.len;
 1924 
 1925         for (i = 0; i < rb->nvec; i++, rb->vec++) {
 1926                 len = MIN(rb->vec->iov_len, plen);
 1927                 if (res < len) {
 1928                         printf("rcv buffer(%d) is %d bytes short.\n",
 1929                             rb->xfer->recv.pay_len, len - res);
 1930                         len = res;
 1931                 }
 1932                 if (p) {
 1933                         bcopy(rb->vec->iov_base, p, len);
 1934                         p += len;
 1935                 }
 1936                 res -= len;
 1937                 plen -= len;
 1938                 if (res == 0 || plen == 0)
 1939                         break;
 1940         }
 1941         rb->xfer->recv.pay_len -= res;
 1942 
 1943 }
 1944 
 1945 /*
 1946  * Generic packet receiving process.
 1947  */
 1948 void
 1949 fw_rcv(struct fw_rcv_buf *rb)
 1950 {
 1951         struct fw_pkt *fp, *resfp;
 1952         struct fw_bind *bind;
 1953         int tcode;
 1954         int i, len, oldstate;
 1955 #if 0
 1956         {
 1957                 uint32_t *qld;
 1958                 int i;
 1959                 qld = (uint32_t *)buf;
 1960                 printf("spd %d len:%d\n", spd, len);
 1961                 for( i = 0 ; i <= len && i < 32; i+= 4){
 1962                         printf("0x%08x ", ntohl(qld[i/4]));
 1963                         if((i % 16) == 15) printf("\n");
 1964                 }
 1965                 if((i % 16) != 15) printf("\n");
 1966         }
 1967 #endif
 1968         fp = (struct fw_pkt *)rb->vec[0].iov_base;
 1969         tcode = fp->mode.common.tcode;
 1970         switch (tcode) {
 1971         case FWTCODE_WRES:
 1972         case FWTCODE_RRESQ:
 1973         case FWTCODE_RRESB:
 1974         case FWTCODE_LRES:
 1975                 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
 1976                     fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode);
 1977                 if(rb->xfer == NULL) {
 1978                         printf("fw_rcv: unknown response "
 1979                             "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n",
 1980                             tcode_str[tcode], tcode,
 1981                             fp->mode.hdr.src,
 1982                             fp->mode.hdr.tlrt >> 2,
 1983                             fp->mode.hdr.tlrt & 3,
 1984                             fp->mode.rresq.data);
 1985 #if 0
 1986                         printf("try ad-hoc work around!!\n");
 1987                         rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src,
 1988                                         (fp->mode.hdr.tlrt >> 2)^3);
 1989                         if (rb->xfer == NULL) {
 1990                                 printf("no use...\n");
 1991                                 return;
 1992                         }
 1993 #else
 1994                         return;
 1995 #endif
 1996                 }
 1997                 fw_rcv_copy(rb);
 1998                 if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP)
 1999                         rb->xfer->resp = EIO;
 2000                 else
 2001                         rb->xfer->resp = 0;
 2002                 /* make sure the packet is drained in AT queue */
 2003                 oldstate = rb->xfer->flag;
 2004                 rb->xfer->flag = FWXF_RCVD;
 2005                 switch (oldstate) {
 2006                 case FWXF_SENT:
 2007                         fw_xfer_done(rb->xfer);
 2008                         break;
 2009                 case FWXF_START:
 2010 #if 0
 2011                         if (firewire_debug)
 2012                                 printf("not sent yet tl=%x\n", rb->xfer->tl);
 2013 #endif
 2014                         break;
 2015                 default:
 2016                         printf("unexpected flag 0x%02x\n", rb->xfer->flag);
 2017                 }
 2018                 return;
 2019         case FWTCODE_WREQQ:
 2020         case FWTCODE_WREQB:
 2021         case FWTCODE_RREQQ:
 2022         case FWTCODE_RREQB:
 2023         case FWTCODE_LREQ:
 2024                 bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi,
 2025                         fp->mode.rreqq.dest_lo);
 2026                 if(bind == NULL){
 2027 #if 1
 2028                         printf("Unknown service addr 0x%04x:0x%08x %s(%x)"
 2029 #if defined(__DragonFly__) || \
 2030     (defined(__FreeBSD__) && __FreeBSD_version < 500000)
 2031                             " src=0x%x data=%lx\n",
 2032 #else
 2033                             " src=0x%x data=%x\n",
 2034 #endif
 2035                             fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo,
 2036                             tcode_str[tcode], tcode,
 2037                             fp->mode.hdr.src, ntohl(fp->mode.wreqq.data));
 2038 #endif
 2039                         if (rb->fc->status == FWBUSINIT) {
 2040                                 printf("fw_rcv: cannot respond(bus reset)!\n");
 2041                                 return;
 2042                         }
 2043                         rb->xfer = fw_xfer_alloc(M_FWXFER);
 2044                         if(rb->xfer == NULL){
 2045                                 return;
 2046                         }
 2047                         rb->xfer->send.spd = rb->spd;
 2048                         rb->xfer->send.pay_len = 0;
 2049                         resfp = &rb->xfer->send.hdr;
 2050                         switch (tcode) {
 2051                         case FWTCODE_WREQQ:
 2052                         case FWTCODE_WREQB:
 2053                                 resfp->mode.hdr.tcode = FWTCODE_WRES;
 2054                                 break;
 2055                         case FWTCODE_RREQQ:
 2056                                 resfp->mode.hdr.tcode = FWTCODE_RRESQ;
 2057                                 break;
 2058                         case FWTCODE_RREQB:
 2059                                 resfp->mode.hdr.tcode = FWTCODE_RRESB;
 2060                                 break;
 2061                         case FWTCODE_LREQ:
 2062                                 resfp->mode.hdr.tcode = FWTCODE_LRES;
 2063                                 break;
 2064                         }
 2065                         resfp->mode.hdr.dst = fp->mode.hdr.src;
 2066                         resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt;
 2067                         resfp->mode.hdr.pri = fp->mode.hdr.pri;
 2068                         resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR;
 2069                         resfp->mode.rresb.extcode = 0;
 2070                         resfp->mode.rresb.len = 0;
 2071 /*
 2072                         rb->xfer->hand = fw_xferwake;
 2073 */
 2074                         rb->xfer->hand = fw_xfer_free;
 2075                         if(fw_asyreq(rb->fc, -1, rb->xfer)){
 2076                                 fw_xfer_free(rb->xfer);
 2077                                 return;
 2078                         }
 2079                         return;
 2080                 }
 2081                 len = 0;
 2082                 for (i = 0; i < rb->nvec; i ++)
 2083                         len += rb->vec[i].iov_len;
 2084                 rb->xfer = STAILQ_FIRST(&bind->xferlist);
 2085                 if (rb->xfer == NULL) {
 2086 #if 1
 2087                         printf("Discard a packet for this bind.\n");
 2088 #endif
 2089                         return;
 2090                 }
 2091                 STAILQ_REMOVE_HEAD(&bind->xferlist, link);
 2092                 fw_rcv_copy(rb);
 2093                 rb->xfer->hand(rb->xfer);
 2094                 return;
 2095 #if 0 /* shouldn't happen ?? or for GASP */
 2096         case FWTCODE_STREAM:
 2097         {
 2098                 struct fw_xferq *xferq;
 2099 
 2100                 xferq = rb->fc->ir[sub];
 2101 #if 0
 2102                 printf("stream rcv dma %d len %d off %d spd %d\n",
 2103                         sub, len, off, spd);
 2104 #endif
 2105                 if(xferq->queued >= xferq->maxq) {
 2106                         printf("receive queue is full\n");
 2107                         return;
 2108                 }
 2109                 /* XXX get xfer from xfer queue, we don't need copy for 
 2110                         per packet mode */
 2111                 rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */
 2112                                                 vec[0].iov_len);
 2113                 if (rb->xfer == NULL)
 2114                         return;
 2115                 fw_rcv_copy(rb)
 2116                 s = splfw();
 2117                 xferq->queued++;
 2118                 STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link);
 2119                 splx(s);
 2120                 sc = device_get_softc(rb->fc->bdev);
 2121 #if defined(__DragonFly__) || \
 2122     (defined(__FreeBSD__) && __FreeBSD_version < 500000)
 2123                 if (&xferq->rsel.si_pid != 0)
 2124 #else
 2125                 if (SEL_WAITING(&xferq->rsel))
 2126 #endif
 2127                         selwakeuppri(&xferq->rsel, FWPRI);
 2128                 if (xferq->flag & FWXFERQ_WAKEUP) {
 2129                         xferq->flag &= ~FWXFERQ_WAKEUP;
 2130                         wakeup((void *)xferq);
 2131                 }
 2132                 if (xferq->flag & FWXFERQ_HANDLER) {
 2133                         xferq->hand(xferq);
 2134                 }
 2135                 return;
 2136                 break;
 2137         }
 2138 #endif
 2139         default:
 2140                 printf("fw_rcv: unknow tcode %d\n", tcode);
 2141                 break;
 2142         }
 2143 }
 2144 
 2145 /*
 2146  * Post process for Bus Manager election process.
 2147  */
 2148 static void
 2149 fw_try_bmr_callback(struct fw_xfer *xfer)
 2150 {
 2151         struct firewire_comm *fc;
 2152         int bmr;
 2153 
 2154         if (xfer == NULL)
 2155                 return;
 2156         fc = xfer->fc;
 2157         if (xfer->resp != 0)
 2158                 goto error;
 2159         if (xfer->recv.payload == NULL)
 2160                 goto error;
 2161         if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE)
 2162                 goto error;
 2163 
 2164         bmr = ntohl(xfer->recv.payload[0]);
 2165         if (bmr == 0x3f)
 2166                 bmr = fc->nodeid;
 2167 
 2168         CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f);
 2169         fw_xfer_free_buf(xfer);
 2170         fw_bmr(fc);
 2171         return;
 2172 
 2173 error:
 2174         fw_printf(fc->bdev, "bus manager election failed\n");
 2175         fw_xfer_free_buf(xfer);
 2176 }
 2177 
 2178 
 2179 /*
 2180  * To candidate Bus Manager election process.
 2181  */
 2182 static void
 2183 fw_try_bmr(void *arg)
 2184 {
 2185         struct fw_xfer *xfer;
 2186         struct firewire_comm *fc = (struct firewire_comm *)arg;
 2187         struct fw_pkt *fp;
 2188         int err = 0;
 2189 
 2190         xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4);
 2191         if(xfer == NULL){
 2192                 return;
 2193         }
 2194         xfer->send.spd = 0;
 2195         fc->status = FWBUSMGRELECT;
 2196 
 2197         fp = &xfer->send.hdr;
 2198         fp->mode.lreq.dest_hi = 0xffff;
 2199         fp->mode.lreq.tlrt = 0;
 2200         fp->mode.lreq.tcode = FWTCODE_LREQ;
 2201         fp->mode.lreq.pri = 0;
 2202         fp->mode.lreq.src = 0;
 2203         fp->mode.lreq.len = 8;
 2204         fp->mode.lreq.extcode = EXTCODE_CMP_SWAP;
 2205         fp->mode.lreq.dst = FWLOCALBUS | fc->irm;
 2206         fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID;
 2207         xfer->send.payload[0] = htonl(0x3f);
 2208         xfer->send.payload[1] = htonl(fc->nodeid);
 2209         xfer->hand = fw_try_bmr_callback;
 2210 
 2211         err = fw_asyreq(fc, -1, xfer);
 2212         if(err){
 2213                 fw_xfer_free_buf(xfer);
 2214                 return;
 2215         }
 2216         return;
 2217 }
 2218 
 2219 #ifdef FW_VMACCESS
 2220 /*
 2221  * Software implementation for physical memory block access.
 2222  * XXX:Too slow, usef for debug purpose only.
 2223  */
 2224 static void
 2225 fw_vmaccess(struct fw_xfer *xfer){
 2226         struct fw_pkt *rfp, *sfp = NULL;
 2227         uint32_t *ld = (uint32_t *)xfer->recv.buf;
 2228 
 2229         printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n",
 2230                         xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
 2231         printf("vmaccess          data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
 2232         if(xfer->resp != 0){
 2233                 fw_xfer_free( xfer);
 2234                 return;
 2235         }
 2236         if(xfer->recv.buf == NULL){
 2237                 fw_xfer_free( xfer);
 2238                 return;
 2239         }
 2240         rfp = (struct fw_pkt *)xfer->recv.buf;
 2241         switch(rfp->mode.hdr.tcode){
 2242                 /* XXX need fix for 64bit arch */
 2243                 case FWTCODE_WREQB:
 2244                         xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
 2245                         xfer->send.len = 12;
 2246                         sfp = (struct fw_pkt *)xfer->send.buf;
 2247                         bcopy(rfp->mode.wreqb.payload,
 2248                                 (void *)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len));
 2249                         sfp->mode.wres.tcode = FWTCODE_WRES;
 2250                         sfp->mode.wres.rtcode = 0;
 2251                         break;
 2252                 case FWTCODE_WREQQ:
 2253                         xfer->send.buf = malloc(12, M_FW, M_NOWAIT);
 2254                         xfer->send.len = 12;
 2255                         sfp->mode.wres.tcode = FWTCODE_WRES;
 2256                         *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data;
 2257                         sfp->mode.wres.rtcode = 0;
 2258                         break;
 2259                 case FWTCODE_RREQB:
 2260                         xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT);
 2261                         xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len);
 2262                         sfp = (struct fw_pkt *)xfer->send.buf;
 2263                         bcopy((void *)ntohl(rfp->mode.rreqb.dest_lo),
 2264                                 sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len));
 2265                         sfp->mode.rresb.tcode = FWTCODE_RRESB;
 2266                         sfp->mode.rresb.len = rfp->mode.rreqb.len;
 2267                         sfp->mode.rresb.rtcode = 0;
 2268                         sfp->mode.rresb.extcode = 0;
 2269                         break;
 2270                 case FWTCODE_RREQQ:
 2271                         xfer->send.buf = malloc(16, M_FW, M_NOWAIT);
 2272                         xfer->send.len = 16;
 2273                         sfp = (struct fw_pkt *)xfer->send.buf;
 2274                         sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo));
 2275                         sfp->mode.wres.tcode = FWTCODE_RRESQ;
 2276                         sfp->mode.rresb.rtcode = 0;
 2277                         break;
 2278                 default:
 2279                         fw_xfer_free( xfer);
 2280                         return;
 2281         }
 2282         sfp->mode.hdr.dst = rfp->mode.hdr.src;
 2283         xfer->dst = ntohs(rfp->mode.hdr.src);
 2284         xfer->hand = fw_xfer_free;
 2285 
 2286         sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt;
 2287         sfp->mode.hdr.pri = 0;
 2288 
 2289         fw_asyreq(xfer->fc, -1, xfer);
 2290 /**/
 2291         return;
 2292 }
 2293 #endif 
 2294 
 2295 /*
 2296  * CRC16 check-sum for IEEE1394 register blocks.
 2297  */
 2298 uint16_t
 2299 fw_crc16(uint32_t *ptr, uint32_t len){
 2300         uint32_t i, sum, crc = 0;
 2301         int shift;
 2302         len = (len + 3) & ~3;
 2303         for(i = 0 ; i < len ; i+= 4){
 2304                 for( shift = 28 ; shift >= 0 ; shift -= 4){
 2305                         sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf;
 2306                         crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum;
 2307                 }
 2308                 crc &= 0xffff;
 2309         }
 2310         return((uint16_t) crc);
 2311 }
 2312 
 2313 static int
 2314 fw_bmr(struct firewire_comm *fc)
 2315 {
 2316         struct fw_device fwdev;
 2317         union fw_self_id *self_id;
 2318         int cmstr;
 2319         uint32_t quad;
 2320 
 2321         /* Check to see if the current root node is cycle master capable */
 2322         self_id = fw_find_self_id(fc, fc->max_node);
 2323         if (fc->max_node > 0) {
 2324                 /* XXX check cmc bit of businfo block rather than contender */
 2325                 if (self_id->p0.link_active && self_id->p0.contender)
 2326                         cmstr = fc->max_node;
 2327                 else {
 2328                         fw_printf(fc->bdev,
 2329                                 "root node is not cycle master capable\n");
 2330                         /* XXX shall we be the cycle master? */
 2331                         cmstr = fc->nodeid;
 2332                         /* XXX need bus reset */
 2333                 }
 2334         } else
 2335                 cmstr = -1;
 2336 
 2337         fw_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID));
 2338         if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) {
 2339                 /* We are not the bus manager */
 2340                 aprint_normal("\n");
 2341                 return(0);
 2342         }
 2343         aprint_normal("(me)\n");
 2344 
 2345         /* Optimize gapcount */
 2346         if(fc->max_hop <= MAX_GAPHOP )
 2347                 fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]);
 2348         /* If we are the cycle master, nothing to do */
 2349         if (cmstr == fc->nodeid || cmstr == -1)
 2350                 return 0;
 2351         /* Bus probe has not finished, make dummy fwdev for cmstr */
 2352         bzero(&fwdev, sizeof(fwdev));
 2353         fwdev.fc = fc;
 2354         fwdev.dst = cmstr;
 2355         fwdev.speed = 0;
 2356         fwdev.maxrec = 8; /* 512 */
 2357         fwdev.status = FWDEVINIT;
 2358         /* Set cmstr bit on the cycle master */
 2359         quad = htonl(1 << 8);
 2360         fwmem_write_quad(&fwdev, NULL, 0/*spd*/,
 2361                 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free);
 2362 
 2363         return 0;
 2364 }
 2365 
 2366 int
 2367 fw_open_isodma(struct firewire_comm *fc, int tx)
 2368 {
 2369         struct fw_xferq **xferqa;
 2370         struct fw_xferq *xferq;
 2371         int i;
 2372 
 2373         if (tx)
 2374                 xferqa = &fc->it[0];
 2375         else
 2376                 xferqa = &fc->ir[0];
 2377 
 2378         FW_GLOCK(fc);
 2379         for (i = 0; i < fc->nisodma; i ++) {
 2380                 xferq = xferqa[i];
 2381                 if ((xferq->flag & FWXFERQ_OPEN) == 0) {
 2382                         xferq->flag |= FWXFERQ_OPEN;
 2383                         break;
 2384                 }
 2385         }
 2386         if (i == fc->nisodma) {
 2387                 printf("no free dma channel (tx=%d)\n", tx);
 2388                 i = -1;
 2389         }
 2390         FW_GUNLOCK(fc);
 2391         return (i);
 2392 }
 2393 
 2394 #if defined(__FreeBSD__)
 2395 static int
 2396 fw_modevent(module_t mode, int type, void *data)
 2397 {
 2398         int err = 0;
 2399 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
 2400         static eventhandler_tag fwdev_ehtag = NULL;
 2401 #endif
 2402 
 2403         switch (type) {
 2404         case MOD_LOAD:
 2405 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
 2406                 fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone,
 2407                                                 fwdev_clone, 0, 1000);
 2408 #endif
 2409                 break;
 2410         case MOD_UNLOAD:
 2411 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
 2412                 if (fwdev_ehtag != NULL)
 2413                         EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag);
 2414 #endif
 2415                 break;
 2416         case MOD_SHUTDOWN:
 2417                 break;
 2418         default:
 2419                 return (EOPNOTSUPP);
 2420         }
 2421         return (err);
 2422 }
 2423 
 2424 
 2425 #ifdef __DragonFly__
 2426 DECLARE_DUMMY_MODULE(firewire);
 2427 #endif
 2428 DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0);
 2429 MODULE_VERSION(firewire, 1);
 2430 #endif

Cache object: 730ecb65fe87fd651806db6312258c66


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.