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