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