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