firewire.c revision 169127
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 169127 2007-04-30 12:55:03Z 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 STAILQ_INIT(&fc->it[i]->binds); 721 STAILQ_INIT(&fc->ir[i]->binds); 722 } 723 724 fc->arq->maxq = FWMAXQUEUE; 725 fc->ars->maxq = FWMAXQUEUE; 726 fc->atq->maxq = FWMAXQUEUE; 727 fc->ats->maxq = FWMAXQUEUE; 728 729 for( i = 0 ; i < fc->nisodma ; i++){ 730 fc->ir[i]->maxq = FWMAXQUEUE; 731 fc->it[i]->maxq = FWMAXQUEUE; 732 } 733/* Initialize csr registers */ 734 fc->topology_map = (struct fw_topology_map *)malloc( 735 sizeof(struct fw_topology_map), 736 M_FW, M_NOWAIT | M_ZERO); 737 fc->speed_map = (struct fw_speed_map *)malloc( 738 sizeof(struct fw_speed_map), 739 M_FW, M_NOWAIT | M_ZERO); 740 CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 741 CSRARC(fc, TOPO_MAP + 4) = 1; 742 CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 743 CSRARC(fc, SPED_MAP + 4) = 1; 744 745 STAILQ_INIT(&fc->devices); 746 747/* Initialize csr ROM work space */ 748 SLIST_INIT(&fc->ongocsr); 749 SLIST_INIT(&fc->csrfree); 750 for( i = 0 ; i < FWMAXCSRDIR ; i++){ 751 csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT); 752 if(csrd == NULL) break; 753 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 754 } 755 756/* Initialize Async handlers */ 757 STAILQ_INIT(&fc->binds); 758 for( i = 0 ; i < 0x40 ; i++){ 759 STAILQ_INIT(&fc->tlabels[i]); 760 } 761 762/* DV depend CSRs see blue book */ 763#if 0 764 CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 765 CSRARC(fc, oPCR) = 0x8000007a; 766 for(i = 4 ; i < 0x7c/4 ; i+=4){ 767 CSRARC(fc, i + oPCR) = 0x8000007a; 768 } 769 770 CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 771 CSRARC(fc, iPCR) = 0x803f0000; 772 for(i = 4 ; i < 0x7c/4 ; i+=4){ 773 CSRARC(fc, i + iPCR) = 0x0; 774 } 775#endif 776 777 fc->crom_src_buf = NULL; 778 779#ifdef FW_VMACCESS 780 xfer = fw_xfer_alloc(); 781 if(xfer == NULL) return; 782 783 fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 784 if(fwb == NULL){ 785 fw_xfer_free(xfer); 786 return; 787 } 788 xfer->hand = fw_vmaccess; 789 xfer->fc = fc; 790 xfer->sc = NULL; 791 792 fwb->start_hi = 0x2; 793 fwb->start_lo = 0; 794 fwb->addrlen = 0xffffffff; 795 fwb->xfer = xfer; 796 fw_bindadd(fc, fwb); 797#endif 798} 799 800#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\ 801 ((fwb)->end < (addr))?1:0) 802 803/* 804 * To lookup bound process from IEEE1394 address. 805 */ 806struct fw_bind * 807fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo) 808{ 809 u_int64_t addr; 810 struct fw_bind *tfw; 811 812 addr = ((u_int64_t)dest_hi << 32) | dest_lo; 813 STAILQ_FOREACH(tfw, &fc->binds, fclist) 814 if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0) 815 return(tfw); 816 return(NULL); 817} 818 819/* 820 * To bind IEEE1394 address block to process. 821 */ 822int 823fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 824{ 825 struct fw_bind *tfw, *prev = NULL; 826 827 if (fwb->start > fwb->end) { 828 printf("%s: invalid range\n", __func__); 829 return EINVAL; 830 } 831 832 STAILQ_FOREACH(tfw, &fc->binds, fclist) { 833 if (fwb->end < tfw->start) 834 break; 835 prev = tfw; 836 } 837 if (prev == NULL) { 838 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 839 goto out; 840 } 841 if (prev->end < fwb->start) { 842 STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 843 goto out; 844 } 845 846 printf("%s: bind failed\n", __func__); 847 return (EBUSY); 848 849out: 850 if (fwb->act_type == FWACT_CH) 851 STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist); 852 return (0); 853} 854 855/* 856 * To free IEEE1394 address block. 857 */ 858int 859fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 860{ 861#if 0 862 struct fw_xfer *xfer, *next; 863#endif 864 struct fw_bind *tfw; 865 int s; 866 867 s = splfw(); 868 STAILQ_FOREACH(tfw, &fc->binds, fclist) 869 if (tfw == fwb) { 870 STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 871 goto found; 872 } 873 874 printf("%s: no such binding\n", __func__); 875 splx(s); 876 return (1); 877found: 878#if 0 879 /* shall we do this? */ 880 for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 881 next = STAILQ_NEXT(xfer, link); 882 fw_xfer_free(xfer); 883 } 884 STAILQ_INIT(&fwb->xferlist); 885#endif 886 887 splx(s); 888 return 0; 889} 890 891/* 892 * To free transaction label. 893 */ 894static void 895fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 896{ 897 struct fw_xfer *txfer; 898 int s; 899 900 if (xfer->tl < 0) 901 return; 902 903 s = splfw(); 904#if 1 /* make sure the label is allocated */ 905 STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel) 906 if(txfer == xfer) 907 break; 908 if (txfer == NULL) { 909 printf("%s: the xfer is not in the tlabel(%d)\n", 910 __FUNCTION__, xfer->tl); 911 splx(s); 912 return; 913 } 914#endif 915 916 STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel); 917 splx(s); 918 return; 919} 920 921/* 922 * To obtain XFER structure by transaction label. 923 */ 924static struct fw_xfer * 925fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel) 926{ 927 struct fw_xfer *xfer; 928 int s = splfw(); 929 930 STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel) 931 if(xfer->send.hdr.mode.hdr.dst == node) { 932 splx(s); 933 if (firewire_debug > 2) 934 printf("fw_tl2xfer: found tl=%d\n", tlabel); 935 return(xfer); 936 } 937 if (firewire_debug > 1) 938 printf("fw_tl2xfer: not found tl=%d\n", tlabel); 939 splx(s); 940 return(NULL); 941} 942 943/* 944 * To allocate IEEE1394 XFER structure. 945 */ 946struct fw_xfer * 947fw_xfer_alloc(struct malloc_type *type) 948{ 949 struct fw_xfer *xfer; 950 951 xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 952 if (xfer == NULL) 953 return xfer; 954 955 xfer->malloc = type; 956 957 return xfer; 958} 959 960struct fw_xfer * 961fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 962{ 963 struct fw_xfer *xfer; 964 965 xfer = fw_xfer_alloc(type); 966 if (xfer == NULL) 967 return(NULL); 968 xfer->send.pay_len = send_len; 969 xfer->recv.pay_len = recv_len; 970 if (send_len > 0) { 971 xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 972 if (xfer->send.payload == NULL) { 973 fw_xfer_free(xfer); 974 return(NULL); 975 } 976 } 977 if (recv_len > 0) { 978 xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 979 if (xfer->recv.payload == NULL) { 980 if (xfer->send.payload != NULL) 981 free(xfer->send.payload, type); 982 fw_xfer_free(xfer); 983 return(NULL); 984 } 985 } 986 return(xfer); 987} 988 989/* 990 * IEEE1394 XFER post process. 991 */ 992void 993fw_xfer_done(struct fw_xfer *xfer) 994{ 995 if (xfer->hand == NULL) { 996 printf("hand == NULL\n"); 997 return; 998 } 999 1000 if (xfer->fc == NULL) 1001 panic("fw_xfer_done: why xfer->fc is NULL?"); 1002 1003 fw_tl_free(xfer->fc, xfer); 1004 xfer->hand(xfer); 1005} 1006 1007void 1008fw_xfer_unload(struct fw_xfer* xfer) 1009{ 1010 int s; 1011 1012 if(xfer == NULL ) return; 1013 if(xfer->state == FWXF_INQ){ 1014 printf("fw_xfer_free FWXF_INQ\n"); 1015 s = splfw(); 1016 STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1017 xfer->q->queued --; 1018 splx(s); 1019 } 1020 if (xfer->fc != NULL) { 1021#if 1 1022 if(xfer->state == FWXF_START) 1023 /* 1024 * This could happen if: 1025 * 1. We call fwohci_arcv() before fwohci_txd(). 1026 * 2. firewire_watch() is called. 1027 */ 1028 printf("fw_xfer_free FWXF_START\n"); 1029#endif 1030 } 1031 xfer->state = FWXF_INIT; 1032 xfer->resp = 0; 1033} 1034/* 1035 * To free IEEE1394 XFER structure. 1036 */ 1037void 1038fw_xfer_free_buf( struct fw_xfer* xfer) 1039{ 1040 if (xfer == NULL) { 1041 printf("%s: xfer == NULL\n", __func__); 1042 return; 1043 } 1044 fw_xfer_unload(xfer); 1045 if(xfer->send.payload != NULL){ 1046 free(xfer->send.payload, xfer->malloc); 1047 } 1048 if(xfer->recv.payload != NULL){ 1049 free(xfer->recv.payload, xfer->malloc); 1050 } 1051 free(xfer, xfer->malloc); 1052} 1053 1054void 1055fw_xfer_free( struct fw_xfer* xfer) 1056{ 1057 if (xfer == NULL) { 1058 printf("%s: xfer == NULL\n", __func__); 1059 return; 1060 } 1061 fw_xfer_unload(xfer); 1062 free(xfer, xfer->malloc); 1063} 1064 1065void 1066fw_asy_callback_free(struct fw_xfer *xfer) 1067{ 1068#if 0 1069 printf("asyreq done state=%d resp=%d\n", 1070 xfer->state, xfer->resp); 1071#endif 1072 fw_xfer_free(xfer); 1073} 1074 1075/* 1076 * To configure PHY. 1077 */ 1078static void 1079fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1080{ 1081 struct fw_xfer *xfer; 1082 struct fw_pkt *fp; 1083 1084 fc->status = FWBUSPHYCONF; 1085 1086 xfer = fw_xfer_alloc(M_FWXFER); 1087 if (xfer == NULL) 1088 return; 1089 xfer->fc = fc; 1090 xfer->hand = fw_asy_callback_free; 1091 1092 fp = &xfer->send.hdr; 1093 fp->mode.ld[1] = 0; 1094 if (root_node >= 0) 1095 fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1096 if (gap_count >= 0) 1097 fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1098 fp->mode.ld[2] = ~fp->mode.ld[1]; 1099/* XXX Dangerous, how to pass PHY packet to device driver */ 1100 fp->mode.common.tcode |= FWTCODE_PHY; 1101 1102 if (firewire_debug) 1103 printf("send phy_config root_node=%d gap_count=%d\n", 1104 root_node, gap_count); 1105 fw_asyreq(fc, -1, xfer); 1106} 1107 1108#if 0 1109/* 1110 * Dump self ID. 1111 */ 1112static void 1113fw_print_sid(uint32_t sid) 1114{ 1115 union fw_self_id *s; 1116 s = (union fw_self_id *) &sid; 1117 printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1118 " p0:%d p1:%d p2:%d i:%d m:%d\n", 1119 s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1120 s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1121 s->p0.power_class, s->p0.port0, s->p0.port1, 1122 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1123} 1124#endif 1125 1126/* 1127 * To receive self ID. 1128 */ 1129void fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len) 1130{ 1131 uint32_t *p; 1132 union fw_self_id *self_id; 1133 u_int i, j, node, c_port = 0, i_branch = 0; 1134 1135 fc->sid_cnt = len /(sizeof(uint32_t) * 2); 1136 fc->status = FWBUSINIT; 1137 fc->max_node = fc->nodeid & 0x3f; 1138 CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16; 1139 fc->status = FWBUSCYMELECT; 1140 fc->topology_map->crc_len = 2; 1141 fc->topology_map->generation ++; 1142 fc->topology_map->self_id_count = 0; 1143 fc->topology_map->node_count = 0; 1144 fc->speed_map->generation ++; 1145 fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1146 self_id = &fc->topology_map->self_id[0]; 1147 for(i = 0; i < fc->sid_cnt; i ++){ 1148 if (sid[1] != ~sid[0]) { 1149 printf("fw_sidrcv: invalid self-id packet\n"); 1150 sid += 2; 1151 continue; 1152 } 1153 *self_id = *((union fw_self_id *)sid); 1154 fc->topology_map->crc_len++; 1155 if(self_id->p0.sequel == 0){ 1156 fc->topology_map->node_count ++; 1157 c_port = 0; 1158#if 0 1159 fw_print_sid(sid[0]); 1160#endif 1161 node = self_id->p0.phy_id; 1162 if(fc->max_node < node){ 1163 fc->max_node = self_id->p0.phy_id; 1164 } 1165 /* XXX I'm not sure this is the right speed_map */ 1166 fc->speed_map->speed[node][node] 1167 = self_id->p0.phy_speed; 1168 for (j = 0; j < node; j ++) { 1169 fc->speed_map->speed[j][node] 1170 = fc->speed_map->speed[node][j] 1171 = min(fc->speed_map->speed[j][j], 1172 self_id->p0.phy_speed); 1173 } 1174 if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1175 (self_id->p0.link_active && self_id->p0.contender)) { 1176 fc->irm = self_id->p0.phy_id; 1177 } 1178 if(self_id->p0.port0 >= 0x2){ 1179 c_port++; 1180 } 1181 if(self_id->p0.port1 >= 0x2){ 1182 c_port++; 1183 } 1184 if(self_id->p0.port2 >= 0x2){ 1185 c_port++; 1186 } 1187 } 1188 if(c_port > 2){ 1189 i_branch += (c_port - 2); 1190 } 1191 sid += 2; 1192 self_id++; 1193 fc->topology_map->self_id_count ++; 1194 } 1195 device_printf(fc->bdev, "%d nodes", fc->max_node + 1); 1196 /* CRC */ 1197 fc->topology_map->crc = fw_crc16( 1198 (uint32_t *)&fc->topology_map->generation, 1199 fc->topology_map->crc_len * 4); 1200 fc->speed_map->crc = fw_crc16( 1201 (uint32_t *)&fc->speed_map->generation, 1202 fc->speed_map->crc_len * 4); 1203 /* byteswap and copy to CSR */ 1204 p = (uint32_t *)fc->topology_map; 1205 for (i = 0; i <= fc->topology_map->crc_len; i++) 1206 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1207 p = (uint32_t *)fc->speed_map; 1208 CSRARC(fc, SPED_MAP) = htonl(*p++); 1209 CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1210 /* don't byte-swap uint8_t array */ 1211 bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1212 1213 fc->max_hop = fc->max_node - i_branch; 1214 printf(", maxhop <= %d", fc->max_hop); 1215 1216 if(fc->irm == -1 ){ 1217 printf(", Not found IRM capable node"); 1218 }else{ 1219 printf(", cable IRM = %d", fc->irm); 1220 if (fc->irm == fc->nodeid) 1221 printf(" (me)"); 1222 } 1223 printf("\n"); 1224 1225 if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1226 if (fc->irm == fc->nodeid) { 1227 fc->status = FWBUSMGRDONE; 1228 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1229 fw_bmr(fc); 1230 } else { 1231 fc->status = FWBUSMGRELECT; 1232 callout_reset(&fc->bmr_callout, hz/8, 1233 (void *)fw_try_bmr, (void *)fc); 1234 } 1235 } else 1236 fc->status = FWBUSMGRDONE; 1237 1238 callout_reset(&fc->busprobe_callout, hz/4, 1239 (void *)fw_bus_probe, (void *)fc); 1240} 1241 1242/* 1243 * To probe devices on the IEEE1394 bus. 1244 */ 1245static void 1246fw_bus_probe(struct firewire_comm *fc) 1247{ 1248 int s; 1249 struct fw_device *fwdev; 1250 1251 s = splfw(); 1252 fc->status = FWBUSEXPLORE; 1253 1254 /* Invalidate all devices, just after bus reset. */ 1255 STAILQ_FOREACH(fwdev, &fc->devices, link) 1256 if (fwdev->status != FWDEVINVAL) { 1257 fwdev->status = FWDEVINVAL; 1258 fwdev->rcnt = 0; 1259 } 1260 1261 fc->ongonode = 0; 1262 fc->ongoaddr = CSRROMOFF; 1263 fc->ongodev = NULL; 1264 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1265 fw_bus_explore(fc); 1266 splx(s); 1267} 1268 1269/* 1270 * Find the self_id packet for a node, ignoring sequels. 1271 */ 1272static union fw_self_id * 1273fw_find_self_id(struct firewire_comm *fc, int node) 1274{ 1275 uint32_t i; 1276 union fw_self_id *s; 1277 1278 for (i = 0; i < fc->topology_map->self_id_count; i++) { 1279 s = &fc->topology_map->self_id[i]; 1280 if (s->p0.sequel) 1281 continue; 1282 if (s->p0.phy_id == node) 1283 return s; 1284 } 1285 return 0; 1286} 1287 1288/* 1289 * To collect device informations on the IEEE1394 bus. 1290 */ 1291static void 1292fw_bus_explore(struct firewire_comm *fc ) 1293{ 1294 int err = 0; 1295 struct fw_device *fwdev, *pfwdev, *tfwdev; 1296 uint32_t addr; 1297 struct fw_xfer *xfer; 1298 struct fw_pkt *fp; 1299 union fw_self_id *fwsid; 1300 1301 if(fc->status != FWBUSEXPLORE) 1302 return; 1303 1304loop: 1305 if(fc->ongonode == fc->nodeid) fc->ongonode++; 1306 1307 if(fc->ongonode > fc->max_node) goto done; 1308 if(fc->ongonode >= 0x3f) goto done; 1309 1310 /* check link */ 1311 /* XXX we need to check phy_id first */ 1312 fwsid = fw_find_self_id(fc, fc->ongonode); 1313 if (!fwsid || !fwsid->p0.link_active) { 1314 if (firewire_debug) 1315 printf("node%d: link down\n", fc->ongonode); 1316 fc->ongonode++; 1317 goto loop; 1318 } 1319 1320 if(fc->ongoaddr <= CSRROMOFF && 1321 fc->ongoeui.hi == 0xffffffff && 1322 fc->ongoeui.lo == 0xffffffff ){ 1323 fc->ongoaddr = CSRROMOFF; 1324 addr = 0xf0000000 | fc->ongoaddr; 1325 }else if(fc->ongoeui.hi == 0xffffffff ){ 1326 fc->ongoaddr = CSRROMOFF + 0xc; 1327 addr = 0xf0000000 | fc->ongoaddr; 1328 }else if(fc->ongoeui.lo == 0xffffffff ){ 1329 fc->ongoaddr = CSRROMOFF + 0x10; 1330 addr = 0xf0000000 | fc->ongoaddr; 1331 }else if(fc->ongodev == NULL){ 1332 STAILQ_FOREACH(fwdev, &fc->devices, link) 1333 if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui)) 1334 break; 1335 if(fwdev != NULL){ 1336 fwdev->dst = fc->ongonode; 1337 fwdev->status = FWDEVINIT; 1338 fc->ongodev = fwdev; 1339 fc->ongoaddr = CSRROMOFF; 1340 addr = 0xf0000000 | fc->ongoaddr; 1341 goto dorequest; 1342 } 1343 fwdev = malloc(sizeof(struct fw_device), M_FW, 1344 M_NOWAIT | M_ZERO); 1345 if(fwdev == NULL) 1346 return; 1347 fwdev->fc = fc; 1348 fwdev->rommax = 0; 1349 fwdev->dst = fc->ongonode; 1350 fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo; 1351 fwdev->status = FWDEVINIT; 1352 fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode]; 1353 1354 pfwdev = NULL; 1355 STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1356 if (tfwdev->eui.hi > fwdev->eui.hi || 1357 (tfwdev->eui.hi == fwdev->eui.hi && 1358 tfwdev->eui.lo > fwdev->eui.lo)) 1359 break; 1360 pfwdev = tfwdev; 1361 } 1362 if (pfwdev == NULL) 1363 STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1364 else 1365 STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1366 1367 device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1368 linkspeed[fwdev->speed], 1369 fc->ongoeui.hi, fc->ongoeui.lo); 1370 1371 fc->ongodev = fwdev; 1372 fc->ongoaddr = CSRROMOFF; 1373 addr = 0xf0000000 | fc->ongoaddr; 1374 }else{ 1375 addr = 0xf0000000 | fc->ongoaddr; 1376 } 1377dorequest: 1378#if 0 1379 xfer = asyreqq(fc, FWSPD_S100, 0, 0, 1380 ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr, 1381 fw_bus_explore_callback); 1382 if(xfer == NULL) goto done; 1383#else 1384 xfer = fw_xfer_alloc(M_FWXFER); 1385 if(xfer == NULL){ 1386 goto done; 1387 } 1388 xfer->send.spd = 0; 1389 fp = &xfer->send.hdr; 1390 fp->mode.rreqq.dest_hi = 0xffff; 1391 fp->mode.rreqq.tlrt = 0; 1392 fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1393 fp->mode.rreqq.pri = 0; 1394 fp->mode.rreqq.src = 0; 1395 fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode; 1396 fp->mode.rreqq.dest_lo = addr; 1397 xfer->hand = fw_bus_explore_callback; 1398 1399 if (firewire_debug) 1400 printf("node%d: explore addr=0x%x\n", 1401 fc->ongonode, fc->ongoaddr); 1402 err = fw_asyreq(fc, -1, xfer); 1403 if(err){ 1404 fw_xfer_free( xfer); 1405 return; 1406 } 1407#endif 1408 return; 1409done: 1410 /* fw_attach_devs */ 1411 fc->status = FWBUSEXPDONE; 1412 if (firewire_debug) 1413 printf("bus_explore done\n"); 1414 fw_attach_dev(fc); 1415 return; 1416 1417} 1418 1419/* Portable Async. request read quad */ 1420struct fw_xfer * 1421asyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt, 1422 uint32_t addr_hi, uint32_t addr_lo, 1423 void (*hand) (struct fw_xfer*)) 1424{ 1425 struct fw_xfer *xfer; 1426 struct fw_pkt *fp; 1427 int err; 1428 1429 xfer = fw_xfer_alloc(M_FWXFER); 1430 if (xfer == NULL) 1431 return NULL; 1432 1433 xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */ 1434 fp = &xfer->send.hdr; 1435 fp->mode.rreqq.dest_hi = addr_hi & 0xffff; 1436 if(tl & FWP_TL_VALID){ 1437 fp->mode.rreqq.tlrt = (tl & 0x3f) << 2; 1438 }else{ 1439 fp->mode.rreqq.tlrt = 0; 1440 } 1441 fp->mode.rreqq.tlrt |= rt & 0x3; 1442 fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1443 fp->mode.rreqq.pri = 0; 1444 fp->mode.rreqq.src = 0; 1445 fp->mode.rreqq.dst = addr_hi >> 16; 1446 fp->mode.rreqq.dest_lo = addr_lo; 1447 xfer->hand = hand; 1448 1449 err = fw_asyreq(fc, -1, xfer); 1450 if(err){ 1451 fw_xfer_free( xfer); 1452 return NULL; 1453 } 1454 return xfer; 1455} 1456 1457/* 1458 * Callback for the IEEE1394 bus information collection. 1459 */ 1460static void 1461fw_bus_explore_callback(struct fw_xfer *xfer) 1462{ 1463 struct firewire_comm *fc; 1464 struct fw_pkt *sfp,*rfp; 1465 struct csrhdr *chdr; 1466 struct csrdir *csrd; 1467 struct csrreg *csrreg; 1468 uint32_t offset; 1469 1470 1471 if(xfer == NULL) { 1472 printf("xfer == NULL\n"); 1473 return; 1474 } 1475 fc = xfer->fc; 1476 1477 if (firewire_debug) 1478 printf("node%d: callback addr=0x%x\n", 1479 fc->ongonode, fc->ongoaddr); 1480 1481 if(xfer->resp != 0){ 1482 device_printf(fc->bdev, 1483 "bus_explore node=%d addr=0x%x resp=%d\n", 1484 fc->ongonode, fc->ongoaddr, xfer->resp); 1485 goto errnode; 1486 } 1487 1488 sfp = &xfer->send.hdr; 1489 rfp = &xfer->recv.hdr; 1490#if 0 1491 { 1492 uint32_t *qld; 1493 int i; 1494 qld = (uint32_t *)xfer->recv.buf; 1495 printf("len:%d\n", xfer->recv.len); 1496 for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){ 1497 printf("0x%08x ", rfp->mode.ld[i/4]); 1498 if((i % 16) == 15) printf("\n"); 1499 } 1500 if((i % 16) != 15) printf("\n"); 1501 } 1502#endif 1503 if(fc->ongodev == NULL){ 1504 if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){ 1505 rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data); 1506 chdr = (struct csrhdr *)(&rfp->mode.rresq.data); 1507/* If CSR is minimal confinguration, more investigation is not needed. */ 1508 if(chdr->info_len == 1){ 1509 if (firewire_debug) 1510 printf("node%d: minimal config\n", 1511 fc->ongonode); 1512 goto nextnode; 1513 }else{ 1514 fc->ongoaddr = CSRROMOFF + 0xc; 1515 } 1516 }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){ 1517 fc->ongoeui.hi = ntohl(rfp->mode.rresq.data); 1518 fc->ongoaddr = CSRROMOFF + 0x10; 1519 }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){ 1520 fc->ongoeui.lo = ntohl(rfp->mode.rresq.data); 1521 if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) { 1522 if (firewire_debug) 1523 printf("node%d: eui64 is zero.\n", 1524 fc->ongonode); 1525 goto nextnode; 1526 } 1527 fc->ongoaddr = CSRROMOFF; 1528 } 1529 }else{ 1530 if (fc->ongoaddr == CSRROMOFF && 1531 fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) { 1532 fc->ongodev->status = FWDEVATTACHED; 1533 goto nextnode; 1534 } 1535 fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data); 1536 if(fc->ongoaddr > fc->ongodev->rommax){ 1537 fc->ongodev->rommax = fc->ongoaddr; 1538 } 1539 csrd = SLIST_FIRST(&fc->ongocsr); 1540 if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1541 chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1542 offset = CSRROMOFF; 1543 }else{ 1544 chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]; 1545 offset = csrd->off; 1546 } 1547 if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){ 1548 csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4]; 1549 if( csrreg->key == 0x81 || csrreg->key == 0xd1){ 1550 csrd = SLIST_FIRST(&fc->csrfree); 1551 if(csrd == NULL){ 1552 goto nextnode; 1553 }else{ 1554 csrd->ongoaddr = fc->ongoaddr; 1555 fc->ongoaddr += csrreg->val * 4; 1556 csrd->off = fc->ongoaddr; 1557 SLIST_REMOVE_HEAD(&fc->csrfree, link); 1558 SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1559 goto nextaddr; 1560 } 1561 } 1562 } 1563 fc->ongoaddr += 4; 1564 if(((fc->ongoaddr - offset)/4 > chdr->crc_len) && 1565 (fc->ongodev->rommax < 0x414)){ 1566 if(fc->ongodev->rommax <= 0x414){ 1567 csrd = SLIST_FIRST(&fc->csrfree); 1568 if(csrd == NULL) goto nextnode; 1569 csrd->off = fc->ongoaddr; 1570 csrd->ongoaddr = fc->ongoaddr; 1571 SLIST_REMOVE_HEAD(&fc->csrfree, link); 1572 SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1573 } 1574 goto nextaddr; 1575 } 1576 1577 while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){ 1578 if(csrd == NULL){ 1579 goto nextnode; 1580 }; 1581 fc->ongoaddr = csrd->ongoaddr + 4; 1582 SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1583 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1584 csrd = SLIST_FIRST(&fc->ongocsr); 1585 if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1586 chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1587 offset = CSRROMOFF; 1588 }else{ 1589 chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]); 1590 offset = csrd->off; 1591 } 1592 } 1593 if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){ 1594 goto nextnode; 1595 } 1596 } 1597nextaddr: 1598 fw_xfer_free( xfer); 1599 fw_bus_explore(fc); 1600 return; 1601errnode: 1602 if (fc->ongodev != NULL) { 1603 fc->ongodev->status = FWDEVINVAL; 1604 /* Invalidate ROM */ 1605 fc->ongodev->csrrom[0] = 0; 1606 } 1607nextnode: 1608 fw_xfer_free( xfer); 1609 fc->ongonode++; 1610/* housekeeping work space */ 1611 fc->ongoaddr = CSRROMOFF; 1612 fc->ongodev = NULL; 1613 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1614 while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){ 1615 SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1616 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1617 } 1618 fw_bus_explore(fc); 1619 return; 1620} 1621 1622/* 1623 * To attach sub-devices layer onto IEEE1394 bus. 1624 */ 1625static void 1626fw_attach_dev(struct firewire_comm *fc) 1627{ 1628 struct fw_device *fwdev, *next; 1629 int i, err; 1630 device_t *devlistp; 1631 int devcnt; 1632 struct firewire_dev_comm *fdc; 1633 1634 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1635 next = STAILQ_NEXT(fwdev, link); 1636 if (fwdev->status == FWDEVINIT) { 1637 fwdev->status = FWDEVATTACHED; 1638 } else if (fwdev->status == FWDEVINVAL) { 1639 fwdev->rcnt ++; 1640 if (fwdev->rcnt > hold_count) { 1641 /* 1642 * Remove devices which have not been seen 1643 * for a while. 1644 */ 1645 STAILQ_REMOVE(&fc->devices, fwdev, fw_device, 1646 link); 1647 free(fwdev, M_FW); 1648 } 1649 } 1650 } 1651 1652 err = device_get_children(fc->bdev, &devlistp, &devcnt); 1653 if( err != 0 ) 1654 return; 1655 for( i = 0 ; i < devcnt ; i++){ 1656 if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1657 fdc = device_get_softc(devlistp[i]); 1658 if (fdc->post_explore != NULL) 1659 fdc->post_explore(fdc); 1660 } 1661 } 1662 free(devlistp, M_TEMP); 1663 1664 return; 1665} 1666 1667/* 1668 * To allocate unique transaction label. 1669 */ 1670static int 1671fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1672{ 1673 u_int i; 1674 struct fw_xfer *txfer; 1675 int s; 1676 static uint32_t label = 0; 1677 1678 s = splfw(); 1679 for( i = 0 ; i < 0x40 ; i ++){ 1680 label = (label + 1) & 0x3f; 1681 STAILQ_FOREACH(txfer, &fc->tlabels[label], tlabel) 1682 if (txfer->send.hdr.mode.hdr.dst == 1683 xfer->send.hdr.mode.hdr.dst) 1684 break; 1685 if(txfer == NULL) { 1686 STAILQ_INSERT_TAIL(&fc->tlabels[label], xfer, tlabel); 1687 splx(s); 1688 if (firewire_debug > 1) 1689 printf("fw_get_tlabel: dst=%d tl=%d\n", 1690 xfer->send.hdr.mode.hdr.dst, label); 1691 return(label); 1692 } 1693 } 1694 splx(s); 1695 1696 if (firewire_debug > 1) 1697 printf("fw_get_tlabel: no free tlabel\n"); 1698 return(-1); 1699} 1700 1701static void 1702fw_rcv_copy(struct fw_rcv_buf *rb) 1703{ 1704 struct fw_pkt *pkt; 1705 u_char *p; 1706 struct tcode_info *tinfo; 1707 u_int res, i, len, plen; 1708 1709 rb->xfer->recv.spd = rb->spd; 1710 1711 pkt = (struct fw_pkt *)rb->vec->iov_base; 1712 tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 1713 1714 /* Copy header */ 1715 p = (u_char *)&rb->xfer->recv.hdr; 1716 bcopy(rb->vec->iov_base, p, tinfo->hdr_len); 1717 rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len; 1718 rb->vec->iov_len -= tinfo->hdr_len; 1719 1720 /* Copy payload */ 1721 p = (u_char *)rb->xfer->recv.payload; 1722 res = rb->xfer->recv.pay_len; 1723 1724 /* special handling for RRESQ */ 1725 if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 1726 p != NULL && res >= sizeof(uint32_t)) { 1727 *(uint32_t *)p = pkt->mode.rresq.data; 1728 rb->xfer->recv.pay_len = sizeof(uint32_t); 1729 return; 1730 } 1731 1732 if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 1733 return; 1734 1735 plen = pkt->mode.rresb.len; 1736 1737 for (i = 0; i < rb->nvec; i++, rb->vec++) { 1738 len = MIN(rb->vec->iov_len, plen); 1739 if (res < len) { 1740 printf("rcv buffer(%d) is %d bytes short.\n", 1741 rb->xfer->recv.pay_len, len - res); 1742 len = res; 1743 } 1744 bcopy(rb->vec->iov_base, p, len); 1745 p += len; 1746 res -= len; 1747 plen -= len; 1748 if (res == 0 || plen == 0) 1749 break; 1750 } 1751 rb->xfer->recv.pay_len -= res; 1752 1753} 1754 1755/* 1756 * Generic packet receiving process. 1757 */ 1758void 1759fw_rcv(struct fw_rcv_buf *rb) 1760{ 1761 struct fw_pkt *fp, *resfp; 1762 struct fw_bind *bind; 1763 int tcode, s; 1764 int i, len, oldstate; 1765#if 0 1766 { 1767 uint32_t *qld; 1768 int i; 1769 qld = (uint32_t *)buf; 1770 printf("spd %d len:%d\n", spd, len); 1771 for( i = 0 ; i <= len && i < 32; i+= 4){ 1772 printf("0x%08x ", ntohl(qld[i/4])); 1773 if((i % 16) == 15) printf("\n"); 1774 } 1775 if((i % 16) != 15) printf("\n"); 1776 } 1777#endif 1778 fp = (struct fw_pkt *)rb->vec[0].iov_base; 1779 tcode = fp->mode.common.tcode; 1780 switch (tcode) { 1781 case FWTCODE_WRES: 1782 case FWTCODE_RRESQ: 1783 case FWTCODE_RRESB: 1784 case FWTCODE_LRES: 1785 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1786 fp->mode.hdr.tlrt >> 2); 1787 if(rb->xfer == NULL) { 1788 printf("fw_rcv: unknown response " 1789 "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n", 1790 tcode_str[tcode], tcode, 1791 fp->mode.hdr.src, 1792 fp->mode.hdr.tlrt >> 2, 1793 fp->mode.hdr.tlrt & 3, 1794 fp->mode.rresq.data); 1795#if 1 1796 printf("try ad-hoc work around!!\n"); 1797 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1798 (fp->mode.hdr.tlrt >> 2)^3); 1799 if (rb->xfer == NULL) { 1800 printf("no use...\n"); 1801 goto err; 1802 } 1803#else 1804 goto err; 1805#endif 1806 } 1807 fw_rcv_copy(rb); 1808 if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 1809 rb->xfer->resp = EIO; 1810 else 1811 rb->xfer->resp = 0; 1812 /* make sure the packet is drained in AT queue */ 1813 oldstate = rb->xfer->state; 1814 rb->xfer->state = FWXF_RCVD; 1815 switch (oldstate) { 1816 case FWXF_SENT: 1817 fw_xfer_done(rb->xfer); 1818 break; 1819 case FWXF_START: 1820#if 0 1821 if (firewire_debug) 1822 printf("not sent yet tl=%x\n", rb->xfer->tl); 1823#endif 1824 break; 1825 default: 1826 printf("unexpected state %d\n", rb->xfer->state); 1827 } 1828 return; 1829 case FWTCODE_WREQQ: 1830 case FWTCODE_WREQB: 1831 case FWTCODE_RREQQ: 1832 case FWTCODE_RREQB: 1833 case FWTCODE_LREQ: 1834 bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 1835 fp->mode.rreqq.dest_lo); 1836 if(bind == NULL){ 1837 printf("Unknown service addr 0x%04x:0x%08x %s(%x)" 1838#if defined(__DragonFly__) || __FreeBSD_version < 500000 1839 " src=0x%x data=%lx\n", 1840#else 1841 " src=0x%x data=%x\n", 1842#endif 1843 fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo, 1844 tcode_str[tcode], tcode, 1845 fp->mode.hdr.src, ntohl(fp->mode.wreqq.data)); 1846 if (rb->fc->status == FWBUSRESET) { 1847 printf("fw_rcv: cannot respond(bus reset)!\n"); 1848 goto err; 1849 } 1850 rb->xfer = fw_xfer_alloc(M_FWXFER); 1851 if(rb->xfer == NULL){ 1852 return; 1853 } 1854 rb->xfer->send.spd = rb->spd; 1855 rb->xfer->send.pay_len = 0; 1856 resfp = &rb->xfer->send.hdr; 1857 switch (tcode) { 1858 case FWTCODE_WREQQ: 1859 case FWTCODE_WREQB: 1860 resfp->mode.hdr.tcode = FWTCODE_WRES; 1861 break; 1862 case FWTCODE_RREQQ: 1863 resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1864 break; 1865 case FWTCODE_RREQB: 1866 resfp->mode.hdr.tcode = FWTCODE_RRESB; 1867 break; 1868 case FWTCODE_LREQ: 1869 resfp->mode.hdr.tcode = FWTCODE_LRES; 1870 break; 1871 } 1872 resfp->mode.hdr.dst = fp->mode.hdr.src; 1873 resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1874 resfp->mode.hdr.pri = fp->mode.hdr.pri; 1875 resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 1876 resfp->mode.rresb.extcode = 0; 1877 resfp->mode.rresb.len = 0; 1878/* 1879 rb->xfer->hand = fw_asy_callback; 1880*/ 1881 rb->xfer->hand = fw_xfer_free; 1882 if(fw_asyreq(rb->fc, -1, rb->xfer)){ 1883 fw_xfer_free(rb->xfer); 1884 return; 1885 } 1886 goto err; 1887 } 1888 len = 0; 1889 for (i = 0; i < rb->nvec; i ++) 1890 len += rb->vec[i].iov_len; 1891 switch(bind->act_type){ 1892 case FWACT_XFER: 1893 /* splfw()?? */ 1894 rb->xfer = STAILQ_FIRST(&bind->xferlist); 1895 if (rb->xfer == NULL) { 1896 printf("Discard a packet for this bind.\n"); 1897 goto err; 1898 } 1899 STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1900 fw_rcv_copy(rb); 1901 rb->xfer->hand(rb->xfer); 1902 return; 1903 break; 1904 case FWACT_CH: 1905 if(rb->fc->ir[bind->sub]->queued >= 1906 rb->fc->ir[bind->sub]->maxq){ 1907 device_printf(rb->fc->bdev, 1908 "Discard a packet %x %d\n", 1909 bind->sub, 1910 rb->fc->ir[bind->sub]->queued); 1911 goto err; 1912 } 1913 rb->xfer = STAILQ_FIRST(&bind->xferlist); 1914 if (rb->xfer == NULL) { 1915 printf("Discard packet for this bind\n"); 1916 goto err; 1917 } 1918 STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1919 fw_rcv_copy(rb); 1920 s = splfw(); 1921 rb->fc->ir[bind->sub]->queued++; 1922 STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q, 1923 rb->xfer, link); 1924 splx(s); 1925 1926 wakeup((caddr_t)rb->fc->ir[bind->sub]); 1927 1928 return; 1929 break; 1930 default: 1931 goto err; 1932 break; 1933 } 1934 break; 1935#if 0 /* shouldn't happen ?? or for GASP */ 1936 case FWTCODE_STREAM: 1937 { 1938 struct fw_xferq *xferq; 1939 1940 xferq = rb->fc->ir[sub]; 1941#if 0 1942 printf("stream rcv dma %d len %d off %d spd %d\n", 1943 sub, len, off, spd); 1944#endif 1945 if(xferq->queued >= xferq->maxq) { 1946 printf("receive queue is full\n"); 1947 goto err; 1948 } 1949 /* XXX get xfer from xfer queue, we don't need copy for 1950 per packet mode */ 1951 rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 1952 vec[0].iov_len); 1953 if (rb->xfer == NULL) goto err; 1954 fw_rcv_copy(rb) 1955 s = splfw(); 1956 xferq->queued++; 1957 STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link); 1958 splx(s); 1959 sc = device_get_softc(rb->fc->bdev); 1960#if defined(__DragonFly__) || __FreeBSD_version < 500000 1961 if (&xferq->rsel.si_pid != 0) 1962#else 1963 if (SEL_WAITING(&xferq->rsel)) 1964#endif 1965 selwakeuppri(&xferq->rsel, FWPRI); 1966 if (xferq->flag & FWXFERQ_WAKEUP) { 1967 xferq->flag &= ~FWXFERQ_WAKEUP; 1968 wakeup((caddr_t)xferq); 1969 } 1970 if (xferq->flag & FWXFERQ_HANDLER) { 1971 xferq->hand(xferq); 1972 } 1973 return; 1974 break; 1975 } 1976#endif 1977 default: 1978 printf("fw_rcv: unknow tcode %d\n", tcode); 1979 break; 1980 } 1981err: 1982 return; 1983} 1984 1985/* 1986 * Post process for Bus Manager election process. 1987 */ 1988static void 1989fw_try_bmr_callback(struct fw_xfer *xfer) 1990{ 1991 struct firewire_comm *fc; 1992 int bmr; 1993 1994 if (xfer == NULL) 1995 return; 1996 fc = xfer->fc; 1997 if (xfer->resp != 0) 1998 goto error; 1999 if (xfer->recv.payload == NULL) 2000 goto error; 2001 if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 2002 goto error; 2003 2004 bmr = ntohl(xfer->recv.payload[0]); 2005 if (bmr == 0x3f) 2006 bmr = fc->nodeid; 2007 2008 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 2009 fw_xfer_free_buf(xfer); 2010 fw_bmr(fc); 2011 return; 2012 2013error: 2014 device_printf(fc->bdev, "bus manager election failed\n"); 2015 fw_xfer_free_buf(xfer); 2016} 2017 2018 2019/* 2020 * To candidate Bus Manager election process. 2021 */ 2022static void 2023fw_try_bmr(void *arg) 2024{ 2025 struct fw_xfer *xfer; 2026 struct firewire_comm *fc = (struct firewire_comm *)arg; 2027 struct fw_pkt *fp; 2028 int err = 0; 2029 2030 xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4); 2031 if(xfer == NULL){ 2032 return; 2033 } 2034 xfer->send.spd = 0; 2035 fc->status = FWBUSMGRELECT; 2036 2037 fp = &xfer->send.hdr; 2038 fp->mode.lreq.dest_hi = 0xffff; 2039 fp->mode.lreq.tlrt = 0; 2040 fp->mode.lreq.tcode = FWTCODE_LREQ; 2041 fp->mode.lreq.pri = 0; 2042 fp->mode.lreq.src = 0; 2043 fp->mode.lreq.len = 8; 2044 fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 2045 fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 2046 fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2047 xfer->send.payload[0] = htonl(0x3f); 2048 xfer->send.payload[1] = htonl(fc->nodeid); 2049 xfer->hand = fw_try_bmr_callback; 2050 2051 err = fw_asyreq(fc, -1, xfer); 2052 if(err){ 2053 fw_xfer_free_buf(xfer); 2054 return; 2055 } 2056 return; 2057} 2058 2059#ifdef FW_VMACCESS 2060/* 2061 * Software implementation for physical memory block access. 2062 * XXX:Too slow, usef for debug purpose only. 2063 */ 2064static void 2065fw_vmaccess(struct fw_xfer *xfer){ 2066 struct fw_pkt *rfp, *sfp = NULL; 2067 uint32_t *ld = (uint32_t *)xfer->recv.buf; 2068 2069 printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 2070 xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 2071 printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2072 if(xfer->resp != 0){ 2073 fw_xfer_free( xfer); 2074 return; 2075 } 2076 if(xfer->recv.buf == NULL){ 2077 fw_xfer_free( xfer); 2078 return; 2079 } 2080 rfp = (struct fw_pkt *)xfer->recv.buf; 2081 switch(rfp->mode.hdr.tcode){ 2082 /* XXX need fix for 64bit arch */ 2083 case FWTCODE_WREQB: 2084 xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2085 xfer->send.len = 12; 2086 sfp = (struct fw_pkt *)xfer->send.buf; 2087 bcopy(rfp->mode.wreqb.payload, 2088 (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 2089 sfp->mode.wres.tcode = FWTCODE_WRES; 2090 sfp->mode.wres.rtcode = 0; 2091 break; 2092 case FWTCODE_WREQQ: 2093 xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2094 xfer->send.len = 12; 2095 sfp->mode.wres.tcode = FWTCODE_WRES; 2096 *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 2097 sfp->mode.wres.rtcode = 0; 2098 break; 2099 case FWTCODE_RREQB: 2100 xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 2101 xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2102 sfp = (struct fw_pkt *)xfer->send.buf; 2103 bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2104 sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len)); 2105 sfp->mode.rresb.tcode = FWTCODE_RRESB; 2106 sfp->mode.rresb.len = rfp->mode.rreqb.len; 2107 sfp->mode.rresb.rtcode = 0; 2108 sfp->mode.rresb.extcode = 0; 2109 break; 2110 case FWTCODE_RREQQ: 2111 xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2112 xfer->send.len = 16; 2113 sfp = (struct fw_pkt *)xfer->send.buf; 2114 sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2115 sfp->mode.wres.tcode = FWTCODE_RRESQ; 2116 sfp->mode.rresb.rtcode = 0; 2117 break; 2118 default: 2119 fw_xfer_free( xfer); 2120 return; 2121 } 2122 sfp->mode.hdr.dst = rfp->mode.hdr.src; 2123 xfer->dst = ntohs(rfp->mode.hdr.src); 2124 xfer->hand = fw_xfer_free; 2125 2126 sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2127 sfp->mode.hdr.pri = 0; 2128 2129 fw_asyreq(xfer->fc, -1, xfer); 2130/**/ 2131 return; 2132} 2133#endif 2134 2135/* 2136 * CRC16 check-sum for IEEE1394 register blocks. 2137 */ 2138uint16_t 2139fw_crc16(uint32_t *ptr, uint32_t len){ 2140 uint32_t i, sum, crc = 0; 2141 int shift; 2142 len = (len + 3) & ~3; 2143 for(i = 0 ; i < len ; i+= 4){ 2144 for( shift = 28 ; shift >= 0 ; shift -= 4){ 2145 sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2146 crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2147 } 2148 crc &= 0xffff; 2149 } 2150 return((uint16_t) crc); 2151} 2152 2153static int 2154fw_bmr(struct firewire_comm *fc) 2155{ 2156 struct fw_device fwdev; 2157 union fw_self_id *self_id; 2158 int cmstr; 2159 uint32_t quad; 2160 2161 /* Check to see if the current root node is cycle master capable */ 2162 self_id = fw_find_self_id(fc, fc->max_node); 2163 if (fc->max_node > 0) { 2164 /* XXX check cmc bit of businfo block rather than contender */ 2165 if (self_id->p0.link_active && self_id->p0.contender) 2166 cmstr = fc->max_node; 2167 else { 2168 device_printf(fc->bdev, 2169 "root node is not cycle master capable\n"); 2170 /* XXX shall we be the cycle master? */ 2171 cmstr = fc->nodeid; 2172 /* XXX need bus reset */ 2173 } 2174 } else 2175 cmstr = -1; 2176 2177 device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID)); 2178 if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2179 /* We are not the bus manager */ 2180 printf("\n"); 2181 return(0); 2182 } 2183 printf("(me)\n"); 2184 2185 /* Optimize gapcount */ 2186 if(fc->max_hop <= MAX_GAPHOP ) 2187 fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2188 /* If we are the cycle master, nothing to do */ 2189 if (cmstr == fc->nodeid || cmstr == -1) 2190 return 0; 2191 /* Bus probe has not finished, make dummy fwdev for cmstr */ 2192 bzero(&fwdev, sizeof(fwdev)); 2193 fwdev.fc = fc; 2194 fwdev.dst = cmstr; 2195 fwdev.speed = 0; 2196 fwdev.maxrec = 8; /* 512 */ 2197 fwdev.status = FWDEVINIT; 2198 /* Set cmstr bit on the cycle master */ 2199 quad = htonl(1 << 8); 2200 fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2201 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free); 2202 2203 return 0; 2204} 2205 2206static int 2207fw_modevent(module_t mode, int type, void *data) 2208{ 2209 int err = 0; 2210#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2211 static eventhandler_tag fwdev_ehtag = NULL; 2212#endif 2213 2214 switch (type) { 2215 case MOD_LOAD: 2216#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2217 fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone, 2218 fwdev_clone, 0, 1000); 2219#endif 2220 break; 2221 case MOD_UNLOAD: 2222#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2223 if (fwdev_ehtag != NULL) 2224 EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag); 2225#endif 2226 break; 2227 case MOD_SHUTDOWN: 2228 break; 2229 default: 2230 return (EOPNOTSUPP); 2231 } 2232 return (err); 2233} 2234 2235 2236#ifdef __DragonFly__ 2237DECLARE_DUMMY_MODULE(firewire); 2238#endif 2239DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0); 2240MODULE_VERSION(firewire, 1); 2241