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