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