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