firewire.c revision 106815
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 106815 2002-11-12 17:16:20Z 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_shutdown, bus_generic_shutdown), 99 100 /* Bus interface */ 101 DEVMETHOD(bus_add_child, firewire_add_child), 102 DEVMETHOD(bus_print_child, bus_generic_print_child), 103 104 { 0, 0 } 105}; 106char linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"}; 107u_int maxrec[6]={512,1024,2048,4096,8192,0}; 108 109#define MAX_GAPHOP 16 110u_int gap_cnt[] = {1, 1, 4, 6, 9, 12, 14, 17, 111 20, 23, 25, 28, 31, 33, 36, 39, 42}; 112 113extern struct cdevsw firewire_cdevsw; 114 115static driver_t firewire_driver = { 116 "firewire", 117 firewire_methods, 118 sizeof(struct firewire_softc), 119}; 120 121/* 122 * transmitter buffer update. 123 */ 124int 125fw_tbuf_update(struct firewire_comm *fc, int sub, int flag){ 126 struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL; 127 struct fw_dvbuf *dvbuf = NULL; 128 struct fw_xferq *it; 129 int s, err = 0, i, j, chtag; 130 struct fw_pkt *fp; 131 u_int64_t tmpsync, dvsync; 132 133 it = fc->it[sub]; 134 135 s = splfw(); 136 if(it->stdma == NULL){ 137 bulkxfer = STAILQ_FIRST(&it->stvalid); 138 }else if(flag != 0){ 139 bulkxfer = STAILQ_FIRST(&it->stvalid); 140 if(bulkxfer == it->stdma){ 141 STAILQ_REMOVE_HEAD(&it->stvalid, link); 142 it->stdma->flag = 0; 143 STAILQ_INSERT_TAIL(&it->stfree, it->stdma, link); 144 if(!(it->flag & FWXFERQ_DV)) 145 wakeup(it); 146 } 147 bulkxfer = STAILQ_FIRST(&it->stvalid); 148 }else{ 149 bulkxfer = it->stdma; 150 } 151 splx(s); 152 if(bulkxfer != NULL){ 153 s = splfw(); 154 bulkxfer2 = STAILQ_NEXT(bulkxfer, link); 155#if 0 156 if(it->flag & FWXFERQ_DV && bulkxfer2 == NULL){ 157 bulkxfer2 = STAILQ_FIRST(&it->stfree); 158 STAILQ_REMOVE_HEAD(&it->stfree, link); 159 splx(s); 160 bcopy(bulkxfer->buf, bulkxfer2->buf, 161 it->psize * it->btpacket); 162 s = splfw(); 163 STAILQ_INSERT_TAIL(&it->stvalid, bulkxfer2, link); 164 } 165#endif 166 splx(s); 167 } 168 it->stdma = bulkxfer; 169 it->stdma2 = bulkxfer2; 170 171 if(it->flag & FWXFERQ_DV){ 172 chtag = it->flag & 0xff; 173dvloop: 174 if(it->dvdma == NULL){ 175 dvbuf = STAILQ_FIRST(&it->dvvalid); 176 if(dvbuf != NULL){ 177 s = splfw(); 178 STAILQ_REMOVE_HEAD(&it->dvvalid, link); 179 it->dvdma = dvbuf; 180 splx(s); 181 it->queued = 0; 182 } 183 } 184 if(it->dvdma == NULL) 185 return err; 186 187 it->stproc = STAILQ_FIRST(&it->stfree); 188 if(it->stproc != NULL){ 189 s = splfw(); 190 STAILQ_REMOVE_HEAD(&it->stfree, link); 191 splx(s); 192 }else{ 193 return err; 194 } 195/* 196 * Insert least significant 12 bits timestamp value by computation. 197 * Highest significant 4 bits is insert at just before packet sending. 198 */ 199 fp = (struct fw_pkt *)(it->stproc->buf); 200/* XXX: Parameter relies on NTSC type DV video */ 201 tmpsync = (u_int64_t)3072 * 8000 * 100 / 2997; 202 tmpsync *= it->dvsync; 203 dvsync = tmpsync; 204 dvsync %= 0xc00; 205 fp->mode.ld[2] = htonl(0x80000000 | (dvsync % 0xc00)); 206 it->dvsync ++; 207 it->dvsync %= 2997; 208 209 for( i = 0, j = 0 ; i < it->dvpacket ; i++){ 210 bcopy(it->dvdma->buf + it->queued * it->psize, 211 it->stproc->buf + j * it->psize, it->psize); 212 fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize); 213 fp->mode.stream.len = htons(488); 214 fp->mode.stream.chtag = chtag; 215 fp->mode.stream.tcode = FWTCODE_STREAM; 216 fp->mode.ld[1] = htonl((fc->nodeid << 24) | 0x00780000 | it->dvdbc); 217 it->dvdbc++; 218 it->dvdbc %= 256; 219 it->queued ++; 220 j++; 221/* XXX: Parameter relies on NTSC type DV video */ 222#if 1 223#define DVDIFF 203 224#define DVFRAC 2997 225#else 226#define DVDIFF 127 227#define DVFRAC 1875 228#endif 229 it->dvdiff += DVDIFF; 230 if(it->dvdiff >= DVFRAC){ 231 it->dvdiff %= DVFRAC; 232 fp = (struct fw_pkt *)(it->stproc->buf + j * it->psize); 233 234 fp->mode.stream.len = htons(0x8); 235 fp->mode.stream.chtag = chtag; 236 fp->mode.stream.tcode = FWTCODE_STREAM; 237 fp->mode.ld[1] = htonl((fc->nodeid << 24) | 238 0x00780000 | it->dvdbc); 239 j++; 240 } 241 } 242 it->stproc->npacket = j; 243 s = splfw(); 244 STAILQ_INSERT_TAIL(&it->stvalid, it->stproc, link); 245 splx(s); 246 if(it->queued >= it->dvpacket){ 247 s = splfw(); 248 STAILQ_INSERT_TAIL(&it->dvfree, it->dvdma, link); 249 it->dvdma = NULL; 250 splx(s); 251 wakeup(it); 252 goto dvloop; 253 } 254 } 255 return err; 256} 257/* 258 * receving buffer update. 259 */ 260int 261fw_rbuf_update(struct firewire_comm *fc, int sub, int flag){ 262 struct fw_bulkxfer *bulkxfer, *bulkxfer2 = NULL; 263 struct fw_xferq *ir; 264 int s, err = 0; 265 266 ir = fc->ir[sub]; 267 s = splfw(); 268 if(ir->stdma != NULL){ 269 if(flag != 0){ 270 STAILQ_INSERT_TAIL(&ir->stvalid, ir->stdma, link); 271 }else{ 272 ir->stdma->flag = 0; 273 STAILQ_INSERT_TAIL(&ir->stfree, ir->stdma, link); 274 } 275 } 276 if(ir->stdma2 != NULL){ 277 bulkxfer = ir->stdma2; 278 bulkxfer2 = STAILQ_FIRST(&ir->stfree); 279 if(bulkxfer2 != NULL){ 280 STAILQ_REMOVE_HEAD(&ir->stfree, link); 281 } 282 }else{ 283 bulkxfer = STAILQ_FIRST(&ir->stfree); 284 if(bulkxfer != NULL){ 285 STAILQ_REMOVE_HEAD(&ir->stfree, link); 286 bulkxfer2 = STAILQ_FIRST(&ir->stfree); 287 if(bulkxfer2 != NULL){ 288 STAILQ_REMOVE_HEAD(&ir->stfree, link); 289 } 290 }else{ 291 bulkxfer = STAILQ_FIRST(&ir->stvalid); 292 STAILQ_REMOVE_HEAD(&ir->stvalid, link); 293 } 294 } 295 splx(s); 296 ir->stdma = bulkxfer; 297 ir->stdma2 = bulkxfer2; 298 return err; 299} 300 301/* 302 * To lookup node id. from EUI64. 303 */ 304struct fw_device * 305fw_noderesolve(struct firewire_comm *fc, struct fw_eui64 eui) 306{ 307 struct fw_device *fwdev; 308 for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; 309 fwdev = TAILQ_NEXT(fwdev, link)){ 310 if(fwdev->eui.hi == eui.hi && fwdev->eui.lo == eui.lo){ 311 break; 312 } 313 } 314 if(fwdev == NULL) return NULL; 315 if(fwdev->status == FWDEVINVAL) return NULL; 316 return fwdev; 317} 318 319/* 320 * Async. request procedure for userland application. 321 */ 322int 323fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 324{ 325 int err = 0; 326 struct fw_xferq *xferq; 327 int tl = 0, len; 328 struct fw_pkt *fp; 329 int tcode; 330 struct tcode_info *info; 331 332 if(xfer == NULL) return EINVAL; 333 if(xfer->send.len > fc->maxrec){ 334 printf("send.len > maxrec\n"); 335 return EINVAL; 336 } 337 if(xfer->act.hand == NULL){ 338 printf("act.hand == NULL\n"); 339 return EINVAL; 340 } 341 fp = (struct fw_pkt *)xfer->send.buf; 342 343 tcode = fp->mode.common.tcode & 0xf; 344 info = &fc->tcode[tcode]; 345 if (info->flag == 0) { 346 printf("invalid tcode=%d\n", tcode); 347 return EINVAL; 348 } 349 if (info->flag & FWTI_REQ) 350 xferq = fc->atq; 351 else 352 xferq = fc->ats; 353 len = info->hdr_len; 354 if (info->flag & FWTI_BLOCK_STR) 355 len += ntohs(fp->mode.stream.len); 356 else if (info->flag & FWTI_BLOCK_ASY) 357 len += ntohs(fp->mode.rresb.len); 358 if( len > xfer->send.len ){ 359 printf("len(%d) > send.len(%d) (tcode=%d)\n", 360 len, xfer->send.len, tcode); 361 return EINVAL; 362 } 363 xfer->send.len = len; 364 365 if(xferq->start == NULL){ 366 printf("xferq->start == NULL\n"); 367 return EINVAL; 368 } 369 if(!(xferq->queued < xferq->maxq)){ 370 printf("%s:Discard a packet (queued=%d)\n", 371 device_get_nameunit(fc->dev), xferq->queued); 372 return EINVAL; 373 } 374 375 376 if (info->flag & FWTI_TLABEL) { 377 if((tl = fw_get_tlabel(fc, xfer)) == -1 ) 378 return EIO; 379 fp->mode.hdr.tlrt = tl << 2; 380 } 381 382 xfer->tl = tl; 383 xfer->tcode = tcode; 384 xfer->resp = 0; 385 xfer->fc = fc; 386 xfer->q = xferq; 387 xfer->act_type = FWACT_XFER; 388 xfer->retry_req = fw_asybusy; 389 390 fw_asystart(xfer); 391 return err; 392} 393/* 394 * Wakeup blocked process. 395 */ 396void 397fw_asy_callback(struct fw_xfer *xfer){ 398 wakeup(xfer); 399 return; 400} 401/* 402 * Postpone to later retry. 403 */ 404void fw_asybusy(struct fw_xfer *xfer){ 405#if 1 406 printf("fw_asybusy\n"); 407#endif 408#if XFER_TIMEOUT 409 untimeout(fw_xfer_timeout, (void *)xfer, xfer->ch); 410#endif 411/* 412 xfer->ch = timeout((timeout_t *)fw_asystart, (void *)xfer, 20000); 413*/ 414 DELAY(20000); 415 fw_asystart(xfer); 416 return; 417} 418#if XFER_TIMEOUT 419/* 420 * Post timeout for async. request. 421 */ 422void 423fw_xfer_timeout(void *arg) 424{ 425 int s; 426 struct fw_xfer *xfer; 427 428 xfer = (struct fw_xfer *)arg; 429 printf("fw_xfer_timeout status=%d resp=%d\n", xfer->state, xfer->resp); 430 /* XXX set error code */ 431 s = splfw(); 432 xfer->act.hand(xfer); 433 splx(s); 434} 435#endif 436/* 437 * Async. request with given xfer structure. 438 */ 439static void 440fw_asystart(struct fw_xfer *xfer) 441{ 442 struct firewire_comm *fc = xfer->fc; 443 int s; 444 if(xfer->retry++ >= fc->max_asyretry){ 445 xfer->resp = EBUSY; 446 xfer->state = FWXF_BUSY; 447 xfer->act.hand(xfer); 448 return; 449 } 450#if 0 /* XXX allow bus explore packets only after bus rest */ 451 if (fc->status < FWBUSEXPLORE) { 452 xfer->resp = EAGAIN; 453 xfer->state = FWXF_BUSY; 454 if (xfer->act.hand != NULL) 455 xfer->act.hand(xfer); 456 return; 457 } 458#endif 459 s = splfw(); 460 xfer->state = FWXF_INQ; 461 STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 462 xfer->q->queued ++; 463 splx(s); 464 /* XXX just queue for mbuf */ 465 if (xfer->mbuf == NULL) 466 xfer->q->start(fc); 467#if XFER_TIMEOUT 468 if (xfer->act.hand != NULL) 469 xfer->ch = timeout(fw_xfer_timeout, (void *)xfer, hz); 470#endif 471 return; 472} 473 474static int 475firewire_match( device_t dev ) 476{ 477 device_set_desc(dev, "IEEE1394(Firewire) bus"); 478 return -140; 479} 480 481/* 482 * The attach routine. 483 */ 484static int 485firewire_attach( device_t dev ) 486{ 487 int i, unitmask, mn; 488 struct firewire_softc *sc = device_get_softc(dev); 489 device_t pa = device_get_parent(dev); 490 struct firewire_comm *fc; 491 dev_t d; 492 493 fc = (struct firewire_comm *)device_get_softc(pa); 494 sc->fc = fc; 495 sc->fc->dev = dev; 496 497 unitmask = UNIT2MIN(device_get_unit(dev)); 498 499 if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 500 for ( i = 0 ; i < fc->nisodma ; i++ ){ 501 mn = unitmask | i; 502 /* XXX device name should be improved */ 503 d = make_dev(&firewire_cdevsw, unit2minor(mn), 504 UID_ROOT, GID_OPERATOR, 0770, 505 "fw%x", mn); 506#if __FreeBSD_version >= 500000 507 if (i == 0) 508 sc->dev = d; 509 else 510 dev_depends(sc->dev, d); 511#else 512 sc->dev[i] = d; 513#endif 514 } 515 d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG), 516 UID_ROOT, GID_OPERATOR, 0770, 517 "fwmem%d", device_get_unit(dev)); 518#if __FreeBSD_version >= 500000 519 dev_depends(sc->dev, d); 520#else 521 sc->dev[i] = d; 522#endif 523 printf("%s: firewire bus attach\n", device_get_nameunit(sc->fc->dev)); 524 sc->fc->timeouthandle = timeout((timeout_t *)sc->fc->timeout, (void *)sc->fc, hz * 10); 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 = FWPMAX_S400; 704 fc->ars->psize = FWPMAX_S400; 705 fc->atq->psize = FWPMAX_S400; 706 fc->ats->psize = FWPMAX_S400; 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_DONTWAIT | M_ZERO); 755 fc->speed_map = (struct fw_speed_map *)malloc( 756 sizeof(struct fw_speed_map), 757 M_DEVBUF, M_DONTWAIT | 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_DONTWAIT); 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_DONTWAIT); 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_DONTWAIT | 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 DELAY(100000); 1053 xfer = fw_xfer_alloc(); 1054 xfer->send.len = 12; 1055 xfer->send.off = 0; 1056 xfer->fc = fc; 1057 xfer->retry_req = fw_asybusy; 1058 xfer->act.hand = fw_phy_config_callback; 1059 1060 xfer->send.buf = malloc(sizeof(u_int32_t), 1061 M_DEVBUF, M_DONTWAIT | M_ZERO); 1062 fp = (struct fw_pkt *)xfer->send.buf; 1063 fp->mode.ld[1] = 0; 1064 if (root_node >= 0) 1065 fp->mode.ld[1] |= htonl((root_node & 0x3f) << 24 | 1 << 23); 1066 if (gap_count >= 0) 1067 fp->mode.ld[1] |= htonl(1 << 22 | (gap_count & 0x3f) << 16); 1068 fp->mode.ld[2] = ~fp->mode.ld[1]; 1069/* XXX Dangerous, how to pass PHY packet to device driver */ 1070 fp->mode.common.tcode |= FWTCODE_PHY; 1071 1072 printf("send phy_config root_node=%d gap_count=%d\n", 1073 root_node, gap_count); 1074 fw_asyreq(fc, -1, xfer); 1075} 1076 1077#if 0 1078/* 1079 * Dump self ID. 1080 */ 1081static void 1082fw_print_sid(u_int32_t sid) 1083{ 1084 union fw_self_id *s; 1085 s = (union fw_self_id *) &sid; 1086 printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1087 " p0:%d p1:%d p2:%d i:%d m:%d\n", 1088 s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1089 s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1090 s->p0.power_class, s->p0.port0, s->p0.port1, 1091 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1092} 1093#endif 1094 1095/* 1096 * To receive self ID. 1097 */ 1098void fw_sidrcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int off) 1099{ 1100 u_int32_t *p, *sid = (u_int32_t *)(buf + off); 1101 union fw_self_id *self_id; 1102 u_int i, j, node, c_port = 0, i_branch = 0; 1103 1104 fc->sid_cnt = len /(sizeof(u_int32_t) * 2); 1105 fc->status = FWBUSINIT; 1106 fc->max_node = fc->nodeid & 0x3f; 1107 CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16; 1108 fc->status = FWBUSCYMELECT; 1109 fc->topology_map->crc_len = 2; 1110 fc->topology_map->generation ++; 1111 fc->topology_map->self_id_count = 0; 1112 fc->topology_map->node_count = 0; 1113 fc->speed_map->generation ++; 1114 fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1115 self_id = &fc->topology_map->self_id[0]; 1116 for(i = 0; i < fc->sid_cnt; i ++){ 1117 if (sid[1] != ~sid[0]) { 1118 printf("fw_sidrcv: invalid self-id packet\n"); 1119 sid += 2; 1120 continue; 1121 } 1122 *self_id = *((union fw_self_id *)sid); 1123 fc->topology_map->crc_len++; 1124 if(self_id->p0.sequel == 0){ 1125 fc->topology_map->node_count ++; 1126 c_port = 0; 1127#if 0 1128 fw_print_sid(sid[0]); 1129#endif 1130 node = self_id->p0.phy_id; 1131 if(fc->max_node < node){ 1132 fc->max_node = self_id->p0.phy_id; 1133 } 1134 /* XXX I'm not sure this is the right speed_map */ 1135 fc->speed_map->speed[node][node] 1136 = self_id->p0.phy_speed; 1137 for (j = 0; j < node; j ++) { 1138 fc->speed_map->speed[j][node] 1139 = fc->speed_map->speed[node][j] 1140 = min(fc->speed_map->speed[j][j], 1141 self_id->p0.phy_speed); 1142 } 1143 if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1144 (self_id->p0.link_active && self_id->p0.contender)) { 1145 fc->irm = self_id->p0.phy_id; 1146 } 1147 if(self_id->p0.port0 >= 0x2){ 1148 c_port++; 1149 } 1150 if(self_id->p0.port1 >= 0x2){ 1151 c_port++; 1152 } 1153 if(self_id->p0.port2 >= 0x2){ 1154 c_port++; 1155 } 1156 } 1157 if(c_port > 2){ 1158 i_branch += (c_port - 2); 1159 } 1160 sid += 2; 1161 self_id++; 1162 fc->topology_map->self_id_count ++; 1163 } 1164 printf("%s: %d nodes", device_get_nameunit(fc->dev), fc->max_node + 1); 1165 /* CRC */ 1166 fc->topology_map->crc = fw_crc16( 1167 (u_int32_t *)&fc->topology_map->generation, 1168 fc->topology_map->crc_len * 4); 1169 fc->speed_map->crc = fw_crc16( 1170 (u_int32_t *)&fc->speed_map->generation, 1171 fc->speed_map->crc_len * 4); 1172 /* byteswap and copy to CSR */ 1173 p = (u_int32_t *)fc->topology_map; 1174 for (i = 0; i <= fc->topology_map->crc_len; i++) 1175 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1176 p = (u_int32_t *)fc->speed_map; 1177 CSRARC(fc, SPED_MAP) = htonl(*p++); 1178 CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1179 /* don't byte-swap u_int8_t array */ 1180 bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1181 1182 fc->max_hop = fc->max_node - i_branch; 1183#if 1 1184 printf(", maxhop <= %d", fc->max_hop); 1185#endif 1186 1187 if(fc->irm == -1 ){ 1188 printf(", Not found IRM capable node"); 1189 }else{ 1190 printf(", cable IRM = %d", fc->irm); 1191 if (fc->irm == fc->nodeid) 1192 printf(" (me)\n"); 1193 else 1194 printf("\n"); 1195 } 1196 1197 if((fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f) ){ 1198 if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){ 1199 fc->status = FWBUSMGRDONE; 1200 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1201 }else{ 1202 fc->status = FWBUSMGRELECT; 1203 fc->bmrhandle = timeout((timeout_t *)fw_try_bmr,(void *)fc, hz / 8); 1204 } 1205 }else{ 1206 fc->status = FWBUSMGRDONE; 1207 printf("%s: BMR = %x\n", device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID)); 1208 } 1209 free(buf, M_DEVBUF); 1210#if 1 1211 /* XXX optimize gap_count, if I am BMGR */ 1212 if(fc->irm == ((CSRARC(fc, NODE_IDS) >> 16 ) & 0x3f)){ 1213 fw_phy_config(fc, -1, gap_cnt[fc->max_hop]); 1214 } 1215#endif 1216#if 1 1217 timeout((timeout_t *)fw_bus_probe, (void *)fc, hz/4); 1218#else 1219 fw_bus_probe(fc); 1220#endif 1221} 1222 1223/* 1224 * To probe devices on the IEEE1394 bus. 1225 */ 1226static void 1227fw_bus_probe(struct firewire_comm *fc) 1228{ 1229 int s; 1230 struct fw_device *fwdev, *next; 1231 1232 s = splfw(); 1233 fc->status = FWBUSEXPLORE; 1234 fc->retry_count = 0; 1235 1236/* 1237 * Invalidate all devices, just after bus reset. Devices 1238 * to be removed has not been seen longer time. 1239 */ 1240 for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1241 next = TAILQ_NEXT(fwdev, link); 1242 if(fwdev->status != FWDEVINVAL){ 1243 fwdev->status = FWDEVINVAL; 1244 fwdev->rcnt = 0; 1245 }else if(fwdev->rcnt < FW_MAXDEVRCNT){ 1246 fwdev->rcnt ++; 1247 }else{ 1248 TAILQ_REMOVE(&fc->devices, fwdev, link); 1249 free(fwdev, M_DEVBUF); 1250 } 1251 } 1252 fc->ongonode = 0; 1253 fc->ongoaddr = CSRROMOFF; 1254 fc->ongodev = NULL; 1255 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1256 fw_bus_explore(fc); 1257 splx(s); 1258} 1259 1260/* 1261 * To collect device informations on the IEEE1394 bus. 1262 */ 1263static void 1264fw_bus_explore(struct firewire_comm *fc ) 1265{ 1266 int err = 0; 1267 struct fw_device *fwdev, *tfwdev; 1268 u_int32_t addr; 1269 struct fw_xfer *xfer; 1270 struct fw_pkt *fp; 1271 1272 if(fc->status != FWBUSEXPLORE) 1273 return; 1274 1275loop: 1276 if(fc->ongonode == fc->nodeid) fc->ongonode++; 1277 1278 if(fc->ongonode > fc->max_node) goto done; 1279 if(fc->ongonode >= 0x3f) goto done; 1280 1281 /* check link */ 1282 /* XXX we need to check phy_id first */ 1283 if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) { 1284 printf("fw_bus_explore: node %d link down\n", fc->ongonode); 1285 fc->ongonode++; 1286 goto loop; 1287 } 1288 1289 if(fc->ongoaddr <= CSRROMOFF && 1290 fc->ongoeui.hi == 0xffffffff && 1291 fc->ongoeui.lo == 0xffffffff ){ 1292 fc->ongoaddr = CSRROMOFF; 1293 addr = 0xf0000000 | fc->ongoaddr; 1294 }else if(fc->ongoeui.hi == 0xffffffff ){ 1295 fc->ongoaddr = CSRROMOFF + 0xc; 1296 addr = 0xf0000000 | fc->ongoaddr; 1297 }else if(fc->ongoeui.lo == 0xffffffff ){ 1298 fc->ongoaddr = CSRROMOFF + 0x10; 1299 addr = 0xf0000000 | fc->ongoaddr; 1300 }else if(fc->ongodev == NULL){ 1301 for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; 1302 fwdev = TAILQ_NEXT(fwdev, link)){ 1303 if(fwdev->eui.hi == fc->ongoeui.hi && fwdev->eui.lo == fc->ongoeui.lo){ 1304 break; 1305 } 1306 } 1307 if(fwdev != NULL){ 1308 fwdev->dst = fc->ongonode; 1309 fwdev->status = FWDEVATTACHED; 1310 fc->ongonode++; 1311 fc->ongoaddr = CSRROMOFF; 1312 fc->ongodev = NULL; 1313 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1314 goto loop; 1315 } 1316 fwdev = malloc(sizeof(struct fw_device), M_DEVBUF, M_DONTWAIT); 1317 if(fwdev == NULL) 1318 return; 1319 fwdev->fc = fc; 1320 fwdev->rommax = 0; 1321 fwdev->dst = fc->ongonode; 1322 fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo; 1323 fwdev->status = FWDEVINIT; 1324#if 0 1325 fwdev->speed = CSRARC(fc, SPED_MAP + 8 + fc->ongonode / 4) 1326 >> ((3 - (fc->ongonode % 4)) * 8); 1327#else 1328 fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode]; 1329#endif 1330 1331 tfwdev = TAILQ_FIRST(&fc->devices); 1332 while( tfwdev != NULL && 1333 (tfwdev->eui.hi > fwdev->eui.hi) && 1334 ((tfwdev->eui.hi == fwdev->eui.hi) && 1335 tfwdev->eui.lo > fwdev->eui.lo)){ 1336 tfwdev = TAILQ_NEXT( tfwdev, link); 1337 } 1338 if(tfwdev == NULL){ 1339 TAILQ_INSERT_TAIL(&fc->devices, fwdev, link); 1340 }else{ 1341 TAILQ_INSERT_BEFORE(tfwdev, fwdev, link); 1342 } 1343 1344 printf("%s:Discover new %s device ID:%08x%08x\n", device_get_nameunit(fc->dev), linkspeed[fwdev->speed], fc->ongoeui.hi, fc->ongoeui.lo); 1345 1346 fc->ongodev = fwdev; 1347 fc->ongoaddr = CSRROMOFF; 1348 addr = 0xf0000000 | fc->ongoaddr; 1349 }else{ 1350 addr = 0xf0000000 | fc->ongoaddr; 1351 } 1352#if 0 1353 xfer = asyreqq(fc, FWSPD_S100, 0, 0, 1354 ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr, 1355 fw_bus_explore_callback); 1356 if(xfer == NULL) goto done; 1357#else 1358 xfer = fw_xfer_alloc(); 1359 if(xfer == NULL){ 1360 goto done; 1361 } 1362 xfer->send.len = 16; 1363 xfer->spd = 0; 1364 xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT); 1365 if(xfer->send.buf == NULL){ 1366 fw_xfer_free( xfer); 1367 return; 1368 } 1369 1370 xfer->send.off = 0; 1371 fp = (struct fw_pkt *)xfer->send.buf; 1372 fp->mode.rreqq.dest_hi = htons(0xffff); 1373 fp->mode.rreqq.tlrt = 0; 1374 fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1375 fp->mode.rreqq.pri = 0; 1376 fp->mode.rreqq.src = 0; 1377 xfer->dst = FWLOCALBUS | fc->ongonode; 1378 fp->mode.rreqq.dst = htons(xfer->dst); 1379 fp->mode.rreqq.dest_lo = htonl(addr); 1380 xfer->act.hand = fw_bus_explore_callback; 1381 1382 err = fw_asyreq(fc, -1, xfer); 1383 if(err){ 1384 fw_xfer_free( xfer); 1385 return; 1386 } 1387#endif 1388 return; 1389done: 1390 /* fw_attach_devs */ 1391 fc->status = FWBUSEXPDONE; 1392 printf("bus_explore done\n"); 1393 fw_attach_dev(fc); 1394 return; 1395 1396} 1397 1398/* Portable Async. request read quad */ 1399struct fw_xfer * 1400asyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt, 1401 u_int32_t addr_hi, u_int32_t addr_lo, 1402 void (*hand) __P((struct fw_xfer*))) 1403{ 1404 struct fw_xfer *xfer; 1405 struct fw_pkt *fp; 1406 int err; 1407 1408 xfer = fw_xfer_alloc(); 1409 if(xfer == NULL){ 1410 return NULL; 1411 } 1412 xfer->send.len = 16; 1413 xfer->spd = spd; /* XXX:min(spd, fc->spd) */ 1414 xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT); 1415 if(xfer->send.buf == NULL){ 1416 fw_xfer_free( xfer); 1417 return NULL; 1418 } 1419 1420 xfer->send.off = 0; 1421 fp = (struct fw_pkt *)xfer->send.buf; 1422 fp->mode.rreqq.dest_hi = htons(addr_hi & 0xffff); 1423 if(tl & FWP_TL_VALID){ 1424 fp->mode.rreqq.tlrt = (tl & 0x3f) << 2; 1425 }else{ 1426 fp->mode.rreqq.tlrt = 0; 1427 } 1428 fp->mode.rreqq.tlrt |= rt & 0x3; 1429 fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1430 fp->mode.rreqq.pri = 0; 1431 fp->mode.rreqq.src = 0; 1432 xfer->dst = addr_hi >> 16; 1433 fp->mode.rreqq.dst = htons(xfer->dst); 1434 fp->mode.rreqq.dest_lo = htonl(addr_lo); 1435 xfer->act.hand = hand; 1436 1437 err = fw_asyreq(fc, -1, xfer); 1438 if(err){ 1439 fw_xfer_free( xfer); 1440 return NULL; 1441 } 1442 return xfer; 1443} 1444 1445/* 1446 * Callback for the IEEE1394 bus information collection. 1447 */ 1448static void 1449fw_bus_explore_callback(struct fw_xfer *xfer) 1450{ 1451 struct firewire_comm *fc; 1452 struct fw_pkt *sfp,*rfp; 1453 struct csrhdr *chdr; 1454 struct csrdir *csrd; 1455 struct csrreg *csrreg; 1456 u_int32_t offset; 1457 1458 1459 if(xfer == NULL) return; 1460 fc = xfer->fc; 1461 if(xfer->resp != 0){ 1462 printf("resp != 0: node=%d addr=0x%x\n", 1463 fc->ongonode, fc->ongoaddr); 1464 fc->retry_count++; 1465 goto nextnode; 1466 } 1467 1468 if(xfer->send.buf == NULL){ 1469 printf("send.buf == NULL: node=%d addr=0x%x\n", 1470 fc->ongonode, fc->ongoaddr); 1471 printf("send.buf == NULL\n"); 1472 fc->retry_count++; 1473 goto nextnode; 1474 } 1475 sfp = (struct fw_pkt *)xfer->send.buf; 1476 1477 if(xfer->recv.buf == NULL){ 1478 printf("recv.buf == NULL: node=%d addr=0x%x\n", 1479 fc->ongonode, fc->ongoaddr); 1480 fc->retry_count++; 1481 goto nextnode; 1482 } 1483 rfp = (struct fw_pkt *)xfer->recv.buf; 1484#if 0 1485 { 1486 u_int32_t *qld; 1487 int i; 1488 qld = (u_int32_t *)xfer->recv.buf; 1489 printf("len:%d\n", xfer->recv.len); 1490 for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){ 1491 printf("0x%08x ", ntohl(rfp->mode.ld[i/4])); 1492 if((i % 16) == 15) printf("\n"); 1493 } 1494 if((i % 16) != 15) printf("\n"); 1495 } 1496#endif 1497 if(fc->ongodev == NULL){ 1498 if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 | CSRROMOFF))){ 1499 rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data); 1500 chdr = (struct csrhdr *)(&rfp->mode.rresq.data); 1501/* If CSR is minimul confinguration, more investgation is not needed. */ 1502 if(chdr->info_len == 1){ 1503 goto nextnode; 1504 }else{ 1505 fc->ongoaddr = CSRROMOFF + 0xc; 1506 } 1507 }else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0xc)))){ 1508 fc->ongoeui.hi = ntohl(rfp->mode.rresq.data); 1509 fc->ongoaddr = CSRROMOFF + 0x10; 1510 }else if(sfp->mode.rreqq.dest_lo == htonl((0xf0000000 |(CSRROMOFF + 0x10)))){ 1511 fc->ongoeui.lo = ntohl(rfp->mode.rresq.data); 1512 if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) 1513 goto nextnode; 1514 fc->ongoaddr = CSRROMOFF; 1515 } 1516 }else{ 1517 fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data); 1518 if(fc->ongoaddr > fc->ongodev->rommax){ 1519 fc->ongodev->rommax = fc->ongoaddr; 1520 } 1521 csrd = SLIST_FIRST(&fc->ongocsr); 1522 if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1523 chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1524 offset = CSRROMOFF; 1525 }else{ 1526 chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]; 1527 offset = csrd->off; 1528 } 1529 if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){ 1530 csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4]; 1531 if( csrreg->key == 0x81 || csrreg->key == 0xd1){ 1532 csrd = SLIST_FIRST(&fc->csrfree); 1533 if(csrd == NULL){ 1534 goto nextnode; 1535 }else{ 1536 csrd->ongoaddr = fc->ongoaddr; 1537 fc->ongoaddr += csrreg->val * 4; 1538 csrd->off = fc->ongoaddr; 1539 SLIST_REMOVE_HEAD(&fc->csrfree, link); 1540 SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1541 goto nextaddr; 1542 } 1543 } 1544 } 1545 fc->ongoaddr += 4; 1546 if(((fc->ongoaddr - offset)/4 > chdr->crc_len) && 1547 (fc->ongodev->rommax < 0x414)){ 1548 if(fc->ongodev->rommax <= 0x414){ 1549 csrd = SLIST_FIRST(&fc->csrfree); 1550 if(csrd == NULL) goto nextnode; 1551 csrd->off = fc->ongoaddr; 1552 csrd->ongoaddr = fc->ongoaddr; 1553 SLIST_REMOVE_HEAD(&fc->csrfree, link); 1554 SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1555 } 1556 goto nextaddr; 1557 } 1558 1559 while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){ 1560 if(csrd == NULL){ 1561 goto nextnode; 1562 }; 1563 fc->ongoaddr = csrd->ongoaddr + 4; 1564 SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1565 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1566 csrd = SLIST_FIRST(&fc->ongocsr); 1567 if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1568 chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1569 offset = CSRROMOFF; 1570 }else{ 1571 chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]); 1572 offset = csrd->off; 1573 } 1574 } 1575 if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){ 1576 goto nextnode; 1577 } 1578 } 1579nextaddr: 1580 fw_xfer_free( xfer); 1581 fw_bus_explore(fc); 1582 return; 1583nextnode: 1584 fw_xfer_free( xfer); 1585 fc->ongonode++; 1586/* housekeeping work space */ 1587 fc->ongoaddr = CSRROMOFF; 1588 fc->ongodev = NULL; 1589 fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1590 while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){ 1591 SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1592 SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1593 } 1594 fw_bus_explore(fc); 1595 return; 1596} 1597 1598/* 1599 * To obtain CSR register values. 1600 */ 1601u_int32_t 1602getcsrdata(struct fw_device *fwdev, u_int8_t key) 1603{ 1604 int i; 1605 struct csrhdr *chdr; 1606 struct csrreg *creg; 1607 chdr = (struct csrhdr *)&fwdev->csrrom[0]; 1608 for( i = chdr->info_len + 4; i <= fwdev->rommax - CSRROMOFF; i+=4){ 1609 creg = (struct csrreg *)&fwdev->csrrom[i/4]; 1610 if(creg->key == key){ 1611 return (u_int32_t)creg->val; 1612 } 1613 } 1614 return 0; 1615} 1616 1617/* 1618 * To attach sub-devices layer onto IEEE1394 bus. 1619 */ 1620static void 1621fw_attach_dev(struct firewire_comm *fc) 1622{ 1623 struct fw_device *fwdev; 1624 struct fw_xfer *xfer; 1625 int i, err; 1626 device_t *devlistp; 1627 int devcnt; 1628 struct firewire_dev_comm *fdc; 1629 1630 for(fwdev = TAILQ_FIRST(&fc->devices); fwdev != NULL; 1631 fwdev = TAILQ_NEXT(fwdev, link)){ 1632 if(fwdev->status == FWDEVINIT){ 1633 fwdev->spec = getcsrdata(fwdev, CSRKEY_SPEC); 1634 if(fwdev->spec == 0) 1635 continue; 1636 fwdev->ver = getcsrdata(fwdev, CSRKEY_VER); 1637 if(fwdev->ver == 0) 1638 continue; 1639 fwdev->maxrec = (fwdev->csrrom[2] >> 12) & 0xf; 1640 1641 switch(fwdev->spec){ 1642 case CSRVAL_ANSIT10: 1643 switch(fwdev->ver){ 1644 case CSRVAL_T10SBP2: 1645 printf("Device SBP-II"); 1646 break; 1647 default: 1648 break; 1649 } 1650 break; 1651 case CSRVAL_1394TA: 1652 switch(fwdev->ver){ 1653 case CSR_PROTAVC: 1654 printf("Device AV/C"); 1655 break; 1656 case CSR_PROTCAL: 1657 printf("Device CAL"); 1658 break; 1659 case CSR_PROTEHS: 1660 printf("Device EHS"); 1661 break; 1662 case CSR_PROTHAVI: 1663 printf("Device HAVi"); 1664 break; 1665 case CSR_PROTCAM104: 1666 printf("Device 1394 Cam 1.04"); 1667 break; 1668 case CSR_PROTCAM120: 1669 printf("Device 1394 Cam 1.20"); 1670 break; 1671 case CSR_PROTCAM130: 1672 printf("Device 1394 Cam 1.30"); 1673 break; 1674 case CSR_PROTDPP: 1675 printf("Device 1394 Direct print"); 1676 break; 1677 case CSR_PROTIICP: 1678 printf("Device Industrial & Instrument"); 1679 break; 1680 default: 1681 printf("Device unkwon 1394TA"); 1682 break; 1683 } 1684 break; 1685 default: 1686 break; 1687 } 1688 fwdev->status = FWDEVATTACHED; 1689 printf("\n"); 1690 } 1691 } 1692 err = device_get_children(fc->dev, &devlistp, &devcnt); 1693 if( err != 0 ) 1694 return; 1695 for( i = 0 ; i < devcnt ; i++){ 1696 if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1697 fdc = device_get_softc(devlistp[i]); 1698 if (fdc->post_explore != NULL) 1699 fdc->post_explore(fdc); 1700 } 1701 } 1702 free(devlistp, M_TEMP); 1703 1704 /* call pending handlers */ 1705 i = 0; 1706 while ((xfer = STAILQ_FIRST(&fc->pending))) { 1707 STAILQ_REMOVE_HEAD(&fc->pending, link); 1708 i++; 1709 if (xfer->act.hand) 1710 xfer->act.hand(xfer); 1711 } 1712 if (i > 0) 1713 printf("fw_attach_dev: %d pending handlers called\n", i); 1714 if (fc->retry_count > 0) { 1715 printf("retry_count = %d\n", fc->retry_count); 1716 fc->retry_probe_handle = timeout((timeout_t *)fc->ibr, 1717 (void *)fc, hz*2); 1718 } 1719 return; 1720} 1721 1722/* 1723 * To allocate uniq transaction label. 1724 */ 1725static int 1726fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1727{ 1728 u_int i; 1729 struct tlabel *tl, *tmptl; 1730 int s; 1731 static u_int32_t label = 0; 1732 1733 s = splfw(); 1734 for( i = 0 ; i < 0x40 ; i ++){ 1735 label = (label + 1) & 0x3f; 1736 for(tmptl = STAILQ_FIRST(&fc->tlabels[label]); 1737 tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){ 1738 if(tmptl->xfer->dst == xfer->dst) break; 1739 } 1740 if(tmptl == NULL) { 1741 tl = malloc(sizeof(struct tlabel),M_DEVBUF,M_DONTWAIT); 1742 if (tl == NULL) { 1743 splx(s); 1744 return (-1); 1745 } 1746 tl->xfer = xfer; 1747 STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link); 1748 splx(s); 1749 return(label); 1750 } 1751 } 1752 splx(s); 1753 1754 printf("fw_get_tlabel: no free tlabel\n"); 1755 return(-1); 1756} 1757 1758/* 1759 * Generic packet receving process. 1760 */ 1761void 1762fw_rcv(struct firewire_comm* fc, caddr_t buf, u_int len, u_int sub, u_int off, u_int spd) 1763{ 1764 struct fw_pkt *fp, *resfp; 1765 struct fw_xfer *xfer; 1766 struct fw_bind *bind; 1767 struct firewire_softc *sc; 1768 int s; 1769#if 0 1770 { 1771 u_int32_t *qld; 1772 int i; 1773 qld = (u_int32_t *)buf; 1774 printf("spd %d len:%d\n", spd, len); 1775 for( i = 0 ; i <= len && i < 32; i+= 4){ 1776 printf("0x%08x ", ntohl(qld[i/4])); 1777 if((i % 16) == 15) printf("\n"); 1778 } 1779 if((i % 16) != 15) printf("\n"); 1780 } 1781#endif 1782 fp = (struct fw_pkt *)(buf + off); 1783 switch(fp->mode.common.tcode){ 1784 case FWTCODE_WRES: 1785 case FWTCODE_RRESQ: 1786 case FWTCODE_RRESB: 1787 case FWTCODE_LRES: 1788 xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src), 1789 fp->mode.hdr.tlrt >> 2); 1790 if(xfer == NULL) { 1791 printf("fw_rcv: unknown response " 1792 "tcode=%d src=0x%x tl=%x rt=%d data=0x%x\n", 1793 fp->mode.common.tcode, 1794 ntohs(fp->mode.hdr.src), 1795 fp->mode.hdr.tlrt >> 2, 1796 fp->mode.hdr.tlrt & 3, 1797 fp->mode.rresq.data); 1798#if 1 1799 printf("try ad-hoc work around!!\n"); 1800 xfer = fw_tl2xfer(fc, ntohs(fp->mode.hdr.src), 1801 (fp->mode.hdr.tlrt >> 2)^3); 1802 if (xfer == NULL) { 1803 printf("no use...\n"); 1804 goto err; 1805 } 1806#else 1807 goto err; 1808#endif 1809 } 1810 switch(xfer->act_type){ 1811 case FWACT_XFER: 1812 if((xfer->sub >= 0) && 1813 ((fc->ir[xfer->sub]->flag & FWXFERQ_MODEMASK ) == 0)){ 1814 xfer->resp = EINVAL; 1815 fw_xfer_done(xfer); 1816 goto err; 1817 } 1818 xfer->recv.len = len; 1819 xfer->recv.off = off; 1820 xfer->recv.buf = buf; 1821 xfer->resp = 0; 1822 fw_xfer_done(xfer); 1823 return; 1824 break; 1825 case FWACT_CH: 1826 default: 1827 goto err; 1828 break; 1829 } 1830 break; 1831 case FWTCODE_WREQQ: 1832 case FWTCODE_WREQB: 1833 case FWTCODE_RREQQ: 1834 case FWTCODE_RREQB: 1835 case FWTCODE_LREQ: 1836 bind = fw_bindlookup(fc, ntohs(fp->mode.rreqq.dest_hi), 1837 ntohl(fp->mode.rreqq.dest_lo)); 1838 if(bind == NULL){ 1839 printf("Unknown service addr 0x%08x:0x%08x tcode=%x\n", 1840 ntohs(fp->mode.rreqq.dest_hi), 1841 ntohl(fp->mode.rreqq.dest_lo), 1842 fp->mode.common.tcode); 1843 if (fc->status == FWBUSRESET) { 1844 printf("fw_rcv: cannot response(bus reset)!\n"); 1845 goto err; 1846 } 1847 xfer = fw_xfer_alloc(); 1848 if(xfer == NULL){ 1849 return; 1850 } 1851 xfer->spd = spd; 1852 xfer->send.buf = malloc(16, M_DEVBUF, M_DONTWAIT); 1853 resfp = (struct fw_pkt *)xfer->send.buf; 1854 switch(fp->mode.common.tcode){ 1855 case FWTCODE_WREQQ: 1856 case FWTCODE_WREQB: 1857 resfp->mode.hdr.tcode = FWTCODE_WRES; 1858 xfer->send.len = 12; 1859 break; 1860 case FWTCODE_RREQQ: 1861 resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1862 xfer->send.len = 16; 1863 break; 1864 case FWTCODE_RREQB: 1865 resfp->mode.hdr.tcode = FWTCODE_RRESB; 1866 xfer->send.len = 16; 1867 break; 1868 case FWTCODE_LREQ: 1869 resfp->mode.hdr.tcode = FWTCODE_LRES; 1870 xfer->send.len = 16; 1871 break; 1872 } 1873 resfp->mode.hdr.dst = fp->mode.hdr.src; 1874 resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1875 resfp->mode.hdr.pri = fp->mode.hdr.pri; 1876 resfp->mode.rresb.rtcode = 7; 1877 resfp->mode.rresb.extcode = 0; 1878 resfp->mode.rresb.len = 0; 1879/* 1880 xfer->act.hand = fw_asy_callback; 1881*/ 1882 xfer->act.hand = fw_xfer_free; 1883 if(fw_asyreq(fc, -1, xfer)){ 1884 fw_xfer_free( xfer); 1885 return; 1886 } 1887 goto err; 1888 } 1889 switch(bind->xfer->act_type){ 1890 case FWACT_XFER: 1891 xfer = fw_xfer_alloc(); 1892 if(xfer == NULL) goto err; 1893 xfer->fc = bind->xfer->fc; 1894 xfer->sc = bind->xfer->sc; 1895 xfer->recv.buf = buf; 1896 xfer->recv.len = len; 1897 xfer->recv.off = off; 1898 xfer->spd = spd; 1899 xfer->act.hand = bind->xfer->act.hand; 1900 if (fc->status != FWBUSRESET) 1901 xfer->act.hand(xfer); 1902 else 1903 STAILQ_INSERT_TAIL(&fc->pending, xfer, link); 1904 return; 1905 break; 1906 case FWACT_CH: 1907 if(fc->ir[bind->xfer->sub]->queued >= 1908 fc->ir[bind->xfer->sub]->maxq){ 1909 printf("%s:Discard a packet %x %d\n", 1910 device_get_nameunit(fc->dev), 1911 bind->xfer->sub, 1912 fc->ir[bind->xfer->sub]->queued); 1913 goto err; 1914 } 1915 xfer = fw_xfer_alloc(); 1916 if(xfer == NULL) goto err; 1917 xfer->recv.buf = buf; 1918 xfer->recv.len = len; 1919 xfer->recv.off = off; 1920 xfer->spd = spd; 1921 s = splfw(); 1922 fc->ir[bind->xfer->sub]->queued++; 1923 STAILQ_INSERT_TAIL(&fc->ir[bind->xfer->sub]->q, xfer, link); 1924 splx(s); 1925 1926 wakeup((caddr_t)fc->ir[bind->xfer->sub]); 1927 1928 return; 1929 break; 1930 default: 1931 goto err; 1932 break; 1933 } 1934 break; 1935 case FWTCODE_STREAM: 1936 { 1937 struct fw_xferq *xferq; 1938 1939 xferq = fc->ir[sub]; 1940#if 0 1941 printf("stream rcv dma %d len %d off %d spd %d\n", 1942 sub, len, off, spd); 1943#endif 1944 if(xferq->queued >= xferq->maxq) { 1945 printf("receive queue is full\n"); 1946 goto err; 1947 } 1948 xfer = fw_xfer_alloc(); 1949 if(xfer == NULL) goto err; 1950 xfer->recv.buf = buf; 1951 xfer->recv.len = len; 1952 xfer->recv.off = off; 1953 xfer->spd = spd; 1954 s = splfw(); 1955 xferq->queued++; 1956 STAILQ_INSERT_TAIL(&xferq->q, xfer, link); 1957 splx(s); 1958 sc = device_get_softc(fc->bdev); 1959#if __FreeBSD_version >= 500000 1960 if (SEL_WAITING(&xferq->rsel)) 1961#else 1962 if (&xferq->rsel.si_pid != 0) 1963#endif 1964 selwakeup(&xferq->rsel); 1965 if (xferq->flag & FWXFERQ_WAKEUP) { 1966 xferq->flag &= ~FWXFERQ_WAKEUP; 1967 wakeup((caddr_t)xferq); 1968 } 1969 if (xferq->flag & FWXFERQ_HANDLER) { 1970 xferq->hand(xferq); 1971 } 1972 return; 1973 break; 1974 } 1975 default: 1976 printf("fw_rcv: unknow tcode\n"); 1977 break; 1978 } 1979err: 1980 free(buf, M_DEVBUF); 1981} 1982 1983/* 1984 * Post process for Bus Manager election process. 1985 */ 1986static void 1987fw_try_bmr_callback(struct fw_xfer *xfer) 1988{ 1989 struct fw_pkt *sfp,*rfp; 1990 struct firewire_comm *fc; 1991 1992 if(xfer == NULL) return; 1993 fc = xfer->fc; 1994 if(xfer->resp != 0){ 1995 goto error; 1996 } 1997 1998 if(xfer->send.buf == NULL){ 1999 goto error; 2000 } 2001 sfp = (struct fw_pkt *)xfer->send.buf; 2002 2003 if(xfer->recv.buf == NULL){ 2004 goto error; 2005 } 2006 rfp = (struct fw_pkt *)xfer->recv.buf; 2007 CSRARC(fc, BUS_MGR_ID) 2008 = fc->set_bmr(fc, ntohl(rfp->mode.lres.payload[0]) & 0x3f); 2009 printf("%s: new bus manager %d ", 2010 device_get_nameunit(fc->dev), CSRARC(fc, BUS_MGR_ID)); 2011 if((htonl(rfp->mode.lres.payload[0]) & 0x3f) == fc->nodeid){ 2012 printf("(me)\n"); 2013/* If I am bus manager, optimize gapcount */ 2014 if(fc->max_hop <= MAX_GAPHOP ){ 2015 fw_phy_config(fc, -1, gap_cnt[fc->max_hop]); 2016 } 2017 }else{ 2018 printf("\n"); 2019 } 2020error: 2021 fw_xfer_free(xfer); 2022} 2023 2024/* 2025 * To candidate Bus Manager election process. 2026 */ 2027void 2028fw_try_bmr(void *arg) 2029{ 2030 struct fw_xfer *xfer; 2031 struct firewire_comm *fc = (struct firewire_comm *)arg; 2032 struct fw_pkt *fp; 2033 int err = 0; 2034 2035 xfer = fw_xfer_alloc(); 2036 if(xfer == NULL){ 2037 return; 2038 } 2039 xfer->send.len = 24; 2040 xfer->spd = 0; 2041 xfer->send.buf = malloc(24, M_DEVBUF, M_DONTWAIT); 2042 if(xfer->send.buf == NULL){ 2043 fw_xfer_free( xfer); 2044 return; 2045 } 2046 2047 fc->status = FWBUSMGRELECT; 2048 2049 xfer->send.off = 0; 2050 fp = (struct fw_pkt *)xfer->send.buf; 2051 fp->mode.lreq.dest_hi = htons(0xffff); 2052 fp->mode.lreq.tlrt = 0; 2053 fp->mode.lreq.tcode = FWTCODE_LREQ; 2054 fp->mode.lreq.pri = 0; 2055 fp->mode.lreq.src = 0; 2056 fp->mode.lreq.len = htons(8); 2057 fp->mode.lreq.extcode = htons(FW_LREQ_CMPSWAP); 2058 xfer->dst = FWLOCALBUS | fc->irm; 2059 fp->mode.lreq.dst = htons(xfer->dst); 2060 fp->mode.lreq.dest_lo = htonl(0xf0000000 | BUS_MGR_ID); 2061 fp->mode.lreq.payload[0] = 0x3f; 2062 fp->mode.lreq.payload[1] = fc->nodeid; 2063 xfer->act_type = FWACT_XFER; 2064 xfer->act.hand = fw_try_bmr_callback; 2065 2066 err = fw_asyreq(fc, -1, xfer); 2067 if(err){ 2068 fw_xfer_free( xfer); 2069 return; 2070 } 2071 return; 2072} 2073 2074#ifdef FW_VMACCESS 2075/* 2076 * Software implementation for physical memory block access. 2077 * XXX:Too slow, usef for debug purpose only. 2078 */ 2079static void 2080fw_vmaccess(struct fw_xfer *xfer){ 2081 struct fw_pkt *rfp, *sfp = NULL; 2082 u_int32_t *ld = (u_int32_t *)(xfer->recv.buf + xfer->recv.off); 2083 2084 printf("vmaccess spd:%2x len:%03x %d data:%08x %08x %08x %08x\n", 2085 xfer->spd, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 2086 printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2087 if(xfer->resp != 0){ 2088 fw_xfer_free( xfer); 2089 return; 2090 } 2091 if(xfer->recv.buf == NULL){ 2092 fw_xfer_free( xfer); 2093 return; 2094 } 2095 rfp = (struct fw_pkt *)xfer->recv.buf; 2096 switch(rfp->mode.hdr.tcode){ 2097 /* XXX need fix for 64bit arch */ 2098 case FWTCODE_WREQB: 2099 xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT); 2100 xfer->send.len = 12; 2101 sfp = (struct fw_pkt *)xfer->send.buf; 2102 bcopy(rfp->mode.wreqb.payload, 2103 (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 2104 sfp->mode.wres.tcode = FWTCODE_WRES; 2105 sfp->mode.wres.rtcode = 0; 2106 break; 2107 case FWTCODE_WREQQ: 2108 xfer->send.buf = malloc(12, M_DEVBUF, M_NOWAIT); 2109 xfer->send.len = 12; 2110 sfp->mode.wres.tcode = FWTCODE_WRES; 2111 *((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 2112 sfp->mode.wres.rtcode = 0; 2113 break; 2114 case FWTCODE_RREQB: 2115 xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_DEVBUF, M_NOWAIT); 2116 xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2117 sfp = (struct fw_pkt *)xfer->send.buf; 2118 bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2119 sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len)); 2120 sfp->mode.rresb.tcode = FWTCODE_RRESB; 2121 sfp->mode.rresb.len = rfp->mode.rreqb.len; 2122 sfp->mode.rresb.rtcode = 0; 2123 sfp->mode.rresb.extcode = 0; 2124 break; 2125 case FWTCODE_RREQQ: 2126 xfer->send.buf = malloc(16, M_DEVBUF, M_NOWAIT); 2127 xfer->send.len = 16; 2128 sfp = (struct fw_pkt *)xfer->send.buf; 2129 sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2130 sfp->mode.wres.tcode = FWTCODE_RRESQ; 2131 sfp->mode.rresb.rtcode = 0; 2132 break; 2133 default: 2134 fw_xfer_free( xfer); 2135 return; 2136 } 2137 xfer->send.off = 0; 2138 sfp->mode.hdr.dst = rfp->mode.hdr.src; 2139 xfer->dst = ntohs(rfp->mode.hdr.src); 2140 xfer->act.hand = fw_xfer_free; 2141 xfer->retry_req = fw_asybusy; 2142 2143 sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2144 sfp->mode.hdr.pri = 0; 2145 2146 fw_asyreq(xfer->fc, -1, xfer); 2147/**/ 2148 return; 2149} 2150#endif 2151 2152/* 2153 * CRC16 check-sum for IEEE1394 register blocks. 2154 */ 2155u_int16_t 2156fw_crc16(u_int32_t *ptr, u_int32_t len){ 2157 u_int32_t i, sum, crc = 0; 2158 int shift; 2159 len = (len + 3) & ~3; 2160 for(i = 0 ; i < len ; i+= 4){ 2161 for( shift = 28 ; shift >= 0 ; shift -= 4){ 2162 sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2163 crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2164 } 2165 crc &= 0xffff; 2166 } 2167 return((u_int16_t) crc); 2168} 2169 2170DRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0); 2171MODULE_VERSION(firewire, 1); 2172