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