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