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