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