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