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