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