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