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