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