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