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