1 /* $NetBSD: firewire.c,v 1.43 2013/10/12 16:49:00 christos Exp $ */ 2 /*- 3 * Copyright (c) 2003 Hidetoshi Shimokawa 4 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the acknowledgement as bellow: 17 * 18 * This product includes software developed by K. Kobayashi and H. Shimokawa 19 * 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 * $FreeBSD: src/sys/dev/firewire/firewire.c,v 1.110 2009/04/07 02:33:46 sbruno Exp $ 36 * 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: firewire.c,v 1.43 2013/10/12 16:49:00 christos Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/bus.h> 44 #include <sys/callout.h> 45 #include <sys/condvar.h> 46 #include <sys/conf.h> 47 #include <sys/device.h> 48 #include <sys/errno.h> 49 #include <sys/kernel.h> 50 #include <sys/kthread.h> 51 #include <sys/malloc.h> 52 #include <sys/queue.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 56 #include <dev/ieee1394/firewire.h> 57 #include <dev/ieee1394/firewirereg.h> 58 #include <dev/ieee1394/fwmem.h> 59 #include <dev/ieee1394/iec13213.h> 60 #include <dev/ieee1394/iec68113.h> 61 62 #include "locators.h" 63 64 struct crom_src_buf { 65 struct crom_src src; 66 struct crom_chunk root; 67 struct crom_chunk vendor; 68 struct crom_chunk hw; 69 }; 70 71 int firewire_debug = 0, try_bmr = 1, hold_count = 0; 72 /* 73 * Setup sysctl(3) MIB, hw.ieee1394if.* 74 * 75 * TBD condition CTLFLAG_PERMANENT on being a module or not 76 */ 77 SYSCTL_SETUP(sysctl_ieee1394if, "sysctl ieee1394if(4) subtree setup") 78 { 79 int rc, ieee1394if_node_num; 80 const struct sysctlnode *node; 81 82 if ((rc = sysctl_createv(clog, 0, NULL, NULL, 83 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL, 84 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) { 85 goto err; 86 } 87 88 if ((rc = sysctl_createv(clog, 0, NULL, &node, 89 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee1394if", 90 SYSCTL_DESCR("ieee1394if controls"), 91 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) { 92 goto err; 93 } 94 ieee1394if_node_num = node->sysctl_num; 95 96 /* ieee1394if try bus manager flag */ 97 if ((rc = sysctl_createv(clog, 0, NULL, &node, 98 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 99 "try_bmr", SYSCTL_DESCR("Try to be a bus manager"), 100 NULL, 0, &try_bmr, 101 0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) { 102 goto err; 103 } 104 105 /* ieee1394if hold count */ 106 if ((rc = sysctl_createv(clog, 0, NULL, &node, 107 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 108 "hold_count", SYSCTL_DESCR("Number of count of " 109 "bus resets for removing lost device information"), 110 NULL, 0, &hold_count, 111 0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) { 112 goto err; 113 } 114 115 /* ieee1394if driver debug flag */ 116 if ((rc = sysctl_createv(clog, 0, NULL, &node, 117 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 118 "ieee1394_debug", SYSCTL_DESCR("ieee1394if driver debug flag"), 119 NULL, 0, &firewire_debug, 120 0, CTL_HW, ieee1394if_node_num, CTL_CREATE, CTL_EOL)) != 0) { 121 goto err; 122 } 123 124 return; 125 126 err: 127 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 128 } 129 130 MALLOC_DEFINE(M_FW, "ieee1394", "IEEE1394"); 131 132 #define FW_MAXASYRTY 4 133 134 #define FW_GENERATION_CHANGEABLE 2 135 136 static int firewirematch (device_t, cfdata_t, void *); 137 static void firewireattach (device_t, device_t, void *); 138 static int firewiredetach (device_t, int); 139 static int firewire_print (void *, const char *); 140 141 int firewire_resume (struct firewire_comm *); 142 143 static void fw_asystart(struct fw_xfer *); 144 static void firewire_xfer_timeout(struct firewire_comm *); 145 static void firewire_watchdog(void *); 146 static void fw_xferq_drain(struct fw_xferq *); 147 static void fw_reset_csr(struct firewire_comm *); 148 static void fw_init_crom(struct firewire_comm *); 149 static void fw_reset_crom(struct firewire_comm *); 150 static void fw_dump_hdr(struct fw_pkt *, const char *); 151 static void fw_tl_free(struct firewire_comm *, struct fw_xfer *); 152 static struct fw_xfer *fw_tl2xfer(struct firewire_comm *, int, int, int); 153 static void fw_phy_config(struct firewire_comm *, int, int); 154 static void fw_print_sid(uint32_t); 155 static void fw_bus_probe(struct firewire_comm *); 156 static int fw_explore_read_quads(struct fw_device *, int, uint32_t *, int); 157 static int fw_explore_csrblock(struct fw_device *, int, int); 158 static int fw_explore_node(struct fw_device *); 159 static union fw_self_id *fw_find_self_id(struct firewire_comm *, int); 160 static void fw_explore(struct firewire_comm *); 161 static void fw_bus_probe_thread(void *); 162 static void fw_attach_dev(struct firewire_comm *); 163 static int fw_get_tlabel(struct firewire_comm *, struct fw_xfer *); 164 static void fw_rcv_copy(struct fw_rcv_buf *); 165 static void fw_try_bmr_callback(struct fw_xfer *); 166 static void fw_try_bmr(void *); 167 static int fw_bmr(struct firewire_comm *); 168 169 170 CFATTACH_DECL_NEW(ieee1394if, sizeof(struct firewire_softc), 171 firewirematch, firewireattach, firewiredetach, NULL); 172 173 174 const char *fw_linkspeed[] = { 175 "S100", "S200", "S400", "S800", 176 "S1600", "S3200", "undef", "undef" 177 }; 178 179 static const char *tcode_str[] = { 180 "WREQQ", "WREQB", "WRES", "undef", 181 "RREQQ", "RREQB", "RRESQ", "RRESB", 182 "CYCS", "LREQ", "STREAM", "LRES", 183 "undef", "undef", "PHY", "undef" 184 }; 185 186 /* IEEE-1394a Table C-2 Gap count as a function of hops*/ 187 #define MAX_GAPHOP 15 188 u_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 189 21, 24, 26, 29, 32, 35, 37, 40}; 190 191 192 static int 193 firewirematch(device_t parent, cfdata_t cf, void *aux) 194 { 195 196 return 1; /* always match */ 197 } 198 199 static void 200 firewireattach(device_t parent, device_t self, void *aux) 201 { 202 struct firewire_softc *sc = device_private(self); 203 struct firewire_comm *fc = device_private(parent); 204 struct fw_attach_args faa; 205 struct firewire_dev_list *devlist; 206 207 aprint_naive("\n"); 208 aprint_normal(": IEEE1394 bus\n"); 209 210 fc->bdev = sc->dev = self; 211 sc->fc = fc; 212 SLIST_INIT(&sc->devlist); 213 214 fc->status = FWBUSNOTREADY; 215 216 if (fc->nisodma > FWMAXNDMA) 217 fc->nisodma = FWMAXNDMA; 218 219 fc->crom_src_buf = 220 (struct crom_src_buf *)malloc(sizeof(struct crom_src_buf), 221 M_FW, M_NOWAIT | M_ZERO); 222 if (fc->crom_src_buf == NULL) { 223 aprint_error_dev(fc->bdev, "Malloc Failure crom src buff\n"); 224 return; 225 } 226 fc->topology_map = 227 (struct fw_topology_map *)malloc(sizeof(struct fw_topology_map), 228 M_FW, M_NOWAIT | M_ZERO); 229 if (fc->topology_map == NULL) { 230 aprint_error_dev(fc->dev, "Malloc Failure topology map\n"); 231 free(fc->crom_src_buf, M_FW); 232 return; 233 } 234 fc->speed_map = 235 (struct fw_speed_map *)malloc(sizeof(struct fw_speed_map), 236 M_FW, M_NOWAIT | M_ZERO); 237 if (fc->speed_map == NULL) { 238 aprint_error_dev(fc->dev, "Malloc Failure speed map\n"); 239 free(fc->crom_src_buf, M_FW); 240 free(fc->topology_map, M_FW); 241 return; 242 } 243 244 mutex_init(&fc->tlabel_lock, MUTEX_DEFAULT, IPL_VM); 245 mutex_init(&fc->fc_mtx, MUTEX_DEFAULT, IPL_VM); 246 mutex_init(&fc->wait_lock, MUTEX_DEFAULT, IPL_VM); 247 cv_init(&fc->fc_cv, "ieee1394"); 248 249 callout_init(&fc->timeout_callout, CALLOUT_MPSAFE); 250 callout_setfunc(&fc->timeout_callout, firewire_watchdog, fc); 251 callout_init(&fc->bmr_callout, CALLOUT_MPSAFE); 252 callout_setfunc(&fc->bmr_callout, fw_try_bmr, fc); 253 callout_init(&fc->busprobe_callout, CALLOUT_MPSAFE); 254 callout_setfunc(&fc->busprobe_callout, (void *)fw_bus_probe, fc); 255 256 callout_schedule(&fc->timeout_callout, hz); 257 258 /* Tell config we will have started a thread to scan the bus. */ 259 config_pending_incr(self); 260 261 /* create thread */ 262 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, fw_bus_probe_thread, 263 fc, &fc->probe_thread, "fw%dprobe", device_unit(fc->bdev))) { 264 aprint_error_dev(self, "kthread_create failed\n"); 265 config_pending_decr(self); 266 } 267 268 devlist = malloc(sizeof(struct firewire_dev_list), M_DEVBUF, M_NOWAIT); 269 if (devlist == NULL) { 270 aprint_error_dev(self, "device list allocation failed\n"); 271 return; 272 } 273 274 faa.name = "fwip"; 275 faa.fc = fc; 276 faa.fwdev = NULL; 277 devlist->dev = config_found(sc->dev, &faa, firewire_print); 278 if (devlist->dev == NULL) 279 free(devlist, M_DEVBUF); 280 else 281 SLIST_INSERT_HEAD(&sc->devlist, devlist, link); 282 283 /* bus_reset */ 284 fw_busreset(fc, FWBUSNOTREADY); 285 fc->ibr(fc); 286 287 if (!pmf_device_register(self, NULL, NULL)) 288 aprint_error_dev(self, "couldn't establish power handler\n"); 289 290 return; 291 } 292 293 static int 294 firewiredetach(device_t self, int flags) 295 { 296 struct firewire_softc *sc = device_private(self); 297 struct firewire_comm *fc; 298 struct fw_device *fwdev, *fwdev_next; 299 struct firewire_dev_list *devlist; 300 int err; 301 302 fc = sc->fc; 303 mutex_enter(&fc->wait_lock); 304 fc->status = FWBUSDETACH; 305 cv_signal(&fc->fc_cv); 306 while (fc->status != FWBUSDETACHOK) { 307 err = cv_timedwait_sig(&fc->fc_cv, &fc->wait_lock, hz * 60); 308 if (err == EWOULDBLOCK) { 309 aprint_error_dev(self, 310 "firewire probe thread didn't die\n"); 311 break; 312 } 313 } 314 mutex_exit(&fc->wait_lock); 315 316 317 while ((devlist = SLIST_FIRST(&sc->devlist)) != NULL) { 318 if ((err = config_detach(devlist->dev, flags)) != 0) 319 return err; 320 SLIST_REMOVE(&sc->devlist, devlist, firewire_dev_list, link); 321 free(devlist, M_DEVBUF); 322 } 323 324 callout_stop(&fc->timeout_callout); 325 callout_stop(&fc->bmr_callout); 326 callout_stop(&fc->busprobe_callout); 327 328 /* XXX xfer_free and untimeout on all xfers */ 329 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; 330 fwdev = fwdev_next) { 331 fwdev_next = STAILQ_NEXT(fwdev, link); 332 free(fwdev, M_FW); 333 } 334 free(fc->topology_map, M_FW); 335 free(fc->speed_map, M_FW); 336 free(fc->crom_src_buf, M_FW); 337 338 cv_destroy(&fc->fc_cv); 339 mutex_destroy(&fc->wait_lock); 340 mutex_destroy(&fc->fc_mtx); 341 mutex_destroy(&fc->tlabel_lock); 342 return 0; 343 } 344 345 static int 346 firewire_print(void *aux, const char *pnp) 347 { 348 struct fw_attach_args *fwa = (struct fw_attach_args *)aux; 349 350 if (pnp) 351 aprint_normal("%s at %s", fwa->name, pnp); 352 353 return UNCONF; 354 } 355 356 int 357 firewire_resume(struct firewire_comm *fc) 358 { 359 360 fc->status = FWBUSNOTREADY; 361 return 0; 362 } 363 364 365 /* 366 * Lookup fwdev by node id. 367 */ 368 struct fw_device * 369 fw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 370 { 371 struct fw_device *fwdev; 372 373 mutex_enter(&fc->fc_mtx); 374 STAILQ_FOREACH(fwdev, &fc->devices, link) 375 if (fwdev->dst == dst && fwdev->status != FWDEVINVAL) 376 break; 377 mutex_exit(&fc->fc_mtx); 378 379 return fwdev; 380 } 381 382 /* 383 * Lookup fwdev by EUI64. 384 */ 385 struct fw_device * 386 fw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 387 { 388 struct fw_device *fwdev; 389 390 mutex_enter(&fc->fc_mtx); 391 STAILQ_FOREACH(fwdev, &fc->devices, link) 392 if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 393 break; 394 mutex_exit(&fc->fc_mtx); 395 396 if (fwdev == NULL) 397 return NULL; 398 if (fwdev->status == FWDEVINVAL) 399 return NULL; 400 return fwdev; 401 } 402 403 /* 404 * Async. request procedure for userland application. 405 */ 406 int 407 fw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 408 { 409 struct fw_xferq *xferq; 410 int len; 411 struct fw_pkt *fp; 412 int tcode; 413 const struct tcode_info *info; 414 415 if (xfer == NULL) 416 return EINVAL; 417 if (xfer->hand == NULL) { 418 aprint_error_dev(fc->bdev, "hand == NULL\n"); 419 return EINVAL; 420 } 421 fp = &xfer->send.hdr; 422 423 tcode = fp->mode.common.tcode & 0xf; 424 info = &fc->tcode[tcode]; 425 if (info->flag == 0) { 426 aprint_error_dev(fc->bdev, "invalid tcode=%x\n", tcode); 427 return EINVAL; 428 } 429 430 /* XXX allow bus explore packets only after bus rest */ 431 if ((fc->status < FWBUSEXPLORE) && 432 ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) || 433 (fp->mode.rreqq.dest_lo < 0xf0000000) || 434 (fp->mode.rreqq.dest_lo >= 0xf0001000))) { 435 xfer->resp = EAGAIN; 436 xfer->flag = FWXF_BUSY; 437 return EAGAIN; 438 } 439 440 if (info->flag & FWTI_REQ) 441 xferq = fc->atq; 442 else 443 xferq = fc->ats; 444 len = info->hdr_len; 445 if (xfer->send.pay_len > MAXREC(fc->maxrec)) { 446 aprint_error_dev(fc->bdev, "send.pay_len > maxrec\n"); 447 return EINVAL; 448 } 449 if (info->flag & FWTI_BLOCK_STR) 450 len = fp->mode.stream.len; 451 else if (info->flag & FWTI_BLOCK_ASY) 452 len = fp->mode.rresb.len; 453 else 454 len = 0; 455 if (len != xfer->send.pay_len) { 456 aprint_error_dev(fc->bdev, 457 "len(%d) != send.pay_len(%d) %s(%x)\n", 458 len, xfer->send.pay_len, tcode_str[tcode], tcode); 459 return EINVAL; 460 } 461 462 if (xferq->start == NULL) { 463 aprint_error_dev(fc->bdev, "xferq->start == NULL\n"); 464 return EINVAL; 465 } 466 if (!(xferq->queued < xferq->maxq)) { 467 aprint_error_dev(fc->bdev, "Discard a packet (queued=%d)\n", 468 xferq->queued); 469 return EAGAIN; 470 } 471 472 xfer->tl = -1; 473 if (info->flag & FWTI_TLABEL) 474 if (fw_get_tlabel(fc, xfer) < 0) 475 return EAGAIN; 476 477 xfer->resp = 0; 478 xfer->fc = fc; 479 xfer->q = xferq; 480 481 fw_asystart(xfer); 482 return 0; 483 } 484 485 /* 486 * Wakeup blocked process. 487 */ 488 void 489 fw_xferwake(struct fw_xfer *xfer) 490 { 491 492 mutex_enter(&xfer->fc->wait_lock); 493 xfer->flag |= FWXF_WAKE; 494 cv_signal(&xfer->cv); 495 mutex_exit(&xfer->fc->wait_lock); 496 497 return; 498 } 499 500 int 501 fw_xferwait(struct fw_xfer *xfer) 502 { 503 struct firewire_comm *fc = xfer->fc; 504 int err = 0; 505 506 mutex_enter(&fc->wait_lock); 507 while (!(xfer->flag & FWXF_WAKE)) 508 err = cv_wait_sig(&xfer->cv, &fc->wait_lock); 509 mutex_exit(&fc->wait_lock); 510 511 return err; 512 } 513 514 void 515 fw_drain_txq(struct firewire_comm *fc) 516 { 517 struct fw_xfer *xfer; 518 STAILQ_HEAD(, fw_xfer) xfer_drain; 519 int i; 520 521 STAILQ_INIT(&xfer_drain); 522 523 mutex_enter(&fc->atq->q_mtx); 524 fw_xferq_drain(fc->atq); 525 mutex_exit(&fc->atq->q_mtx); 526 mutex_enter(&fc->ats->q_mtx); 527 fw_xferq_drain(fc->ats); 528 mutex_exit(&fc->ats->q_mtx); 529 for (i = 0; i < fc->nisodma; i++) 530 fw_xferq_drain(fc->it[i]); 531 532 mutex_enter(&fc->tlabel_lock); 533 for (i = 0; i < 0x40; i++) 534 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 535 if (firewire_debug) 536 printf("tl=%d flag=%d\n", i, xfer->flag); 537 xfer->resp = EAGAIN; 538 STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel); 539 STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel); 540 } 541 mutex_exit(&fc->tlabel_lock); 542 543 STAILQ_FOREACH(xfer, &xfer_drain, tlabel) 544 xfer->hand(xfer); 545 } 546 547 /* 548 * Called after bus reset. 549 */ 550 void 551 fw_busreset(struct firewire_comm *fc, uint32_t new_status) 552 { 553 struct firewire_softc *sc = device_private(fc->bdev); 554 struct firewire_dev_list *devlist; 555 struct firewire_dev_comm *fdc; 556 struct crom_src *src; 557 uint32_t *newrom; 558 559 if (fc->status == FWBUSMGRELECT) 560 callout_stop(&fc->bmr_callout); 561 562 fc->status = new_status; 563 fw_reset_csr(fc); 564 565 if (fc->status == FWBUSNOTREADY) 566 fw_init_crom(fc); 567 568 fw_reset_crom(fc); 569 570 /* How many safe this access? */ 571 SLIST_FOREACH(devlist, &sc->devlist, link) { 572 fdc = device_private(devlist->dev); 573 if (fdc->post_busreset != NULL) 574 fdc->post_busreset(fdc); 575 } 576 577 /* 578 * If the old config rom needs to be overwritten, 579 * bump the businfo.generation indicator to 580 * indicate that we need to be reprobed 581 * See 1394a-2000 8.3.2.5.4 for more details. 582 * generation starts at 2 and rolls over at 0xF 583 * back to 2. 584 * 585 * A generation of 0 indicates a device 586 * that is not 1394a-2000 compliant. 587 * A generation of 1 indicates a device that 588 * does not change it's Bus Info Block or 589 * Configuration ROM. 590 */ 591 #define FW_MAX_GENERATION 0xF 592 newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO); 593 src = &fc->crom_src_buf->src; 594 crom_load(src, newrom, CROMSIZE); 595 if (memcmp(newrom, fc->config_rom, CROMSIZE) != 0) { 596 if (src->businfo.generation++ > FW_MAX_GENERATION) 597 src->businfo.generation = FW_GENERATION_CHANGEABLE; 598 memcpy((void *)fc->config_rom, newrom, CROMSIZE); 599 } 600 free(newrom, M_FW); 601 } 602 603 /* Call once after reboot */ 604 void 605 fw_init(struct firewire_comm *fc) 606 { 607 int i; 608 609 fc->arq->queued = 0; 610 fc->ars->queued = 0; 611 fc->atq->queued = 0; 612 fc->ats->queued = 0; 613 614 fc->arq->buf = NULL; 615 fc->ars->buf = NULL; 616 fc->atq->buf = NULL; 617 fc->ats->buf = NULL; 618 619 fc->arq->flag = 0; 620 fc->ars->flag = 0; 621 fc->atq->flag = 0; 622 fc->ats->flag = 0; 623 624 STAILQ_INIT(&fc->atq->q); 625 STAILQ_INIT(&fc->ats->q); 626 mutex_init(&fc->arq->q_mtx, MUTEX_DEFAULT, IPL_VM); 627 mutex_init(&fc->ars->q_mtx, MUTEX_DEFAULT, IPL_VM); 628 mutex_init(&fc->atq->q_mtx, MUTEX_DEFAULT, IPL_VM); 629 mutex_init(&fc->ats->q_mtx, MUTEX_DEFAULT, IPL_VM); 630 631 for (i = 0; i < fc->nisodma; i++) { 632 fc->it[i]->queued = 0; 633 fc->ir[i]->queued = 0; 634 635 fc->it[i]->start = NULL; 636 fc->ir[i]->start = NULL; 637 638 fc->it[i]->buf = NULL; 639 fc->ir[i]->buf = NULL; 640 641 fc->it[i]->flag = FWXFERQ_STREAM; 642 fc->ir[i]->flag = FWXFERQ_STREAM; 643 644 STAILQ_INIT(&fc->it[i]->q); 645 STAILQ_INIT(&fc->ir[i]->q); 646 } 647 648 fc->arq->maxq = FWMAXQUEUE; 649 fc->ars->maxq = FWMAXQUEUE; 650 fc->atq->maxq = FWMAXQUEUE; 651 fc->ats->maxq = FWMAXQUEUE; 652 653 for (i = 0; i < fc->nisodma; i++) { 654 fc->ir[i]->maxq = FWMAXQUEUE; 655 fc->it[i]->maxq = FWMAXQUEUE; 656 } 657 658 CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 659 CSRARC(fc, TOPO_MAP + 4) = 1; 660 CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 661 CSRARC(fc, SPED_MAP + 4) = 1; 662 663 STAILQ_INIT(&fc->devices); 664 665 /* Initialize Async handlers */ 666 STAILQ_INIT(&fc->binds); 667 for (i = 0; i < 0x40; i++) 668 STAILQ_INIT(&fc->tlabels[i]); 669 670 /* DV depend CSRs see blue book */ 671 #if 0 672 CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 673 CSRARC(fc, oPCR) = 0x8000007a; 674 for (i = 4; i < 0x7c/4; i+=4) 675 CSRARC(fc, i + oPCR) = 0x8000007a; 676 677 CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 678 CSRARC(fc, iPCR) = 0x803f0000; 679 for (i = 4; i < 0x7c/4; i+=4) 680 CSRARC(fc, i + iPCR) = 0x0; 681 #endif 682 683 fc->crom_src_buf = NULL; 684 } 685 686 void 687 fw_destroy(struct firewire_comm *fc) 688 { 689 mutex_destroy(&fc->arq->q_mtx); 690 mutex_destroy(&fc->ars->q_mtx); 691 mutex_destroy(&fc->atq->q_mtx); 692 mutex_destroy(&fc->ats->q_mtx); 693 } 694 695 #define BIND_CMP(addr, fwb) \ 696 (((addr) < (fwb)->start) ? -1 : ((fwb)->end < (addr)) ? 1 : 0) 697 698 /* 699 * To lookup bound process from IEEE1394 address. 700 */ 701 struct fw_bind * 702 fw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo) 703 { 704 u_int64_t addr; 705 struct fw_bind *tfw, *r = NULL; 706 707 addr = ((u_int64_t)dest_hi << 32) | dest_lo; 708 mutex_enter(&fc->fc_mtx); 709 STAILQ_FOREACH(tfw, &fc->binds, fclist) 710 if (BIND_CMP(addr, tfw) == 0) { 711 r = tfw; 712 break; 713 } 714 mutex_exit(&fc->fc_mtx); 715 return r; 716 } 717 718 /* 719 * To bind IEEE1394 address block to process. 720 */ 721 int 722 fw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 723 { 724 struct fw_bind *tfw, *prev = NULL; 725 int r = 0; 726 727 if (fwb->start > fwb->end) { 728 aprint_error_dev(fc->bdev, "invalid range\n"); 729 return EINVAL; 730 } 731 732 mutex_enter(&fc->fc_mtx); 733 STAILQ_FOREACH(tfw, &fc->binds, fclist) { 734 if (fwb->end < tfw->start) 735 break; 736 prev = tfw; 737 } 738 if (prev == NULL) 739 STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 740 else if (prev->end < fwb->start) 741 STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 742 else { 743 aprint_error_dev(fc->bdev, "bind failed\n"); 744 r = EBUSY; 745 } 746 mutex_exit(&fc->fc_mtx); 747 return r; 748 } 749 750 /* 751 * To free IEEE1394 address block. 752 */ 753 int 754 fw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 755 { 756 #if 0 757 struct fw_xfer *xfer, *next; 758 #endif 759 struct fw_bind *tfw; 760 761 mutex_enter(&fc->fc_mtx); 762 STAILQ_FOREACH(tfw, &fc->binds, fclist) 763 if (tfw == fwb) { 764 STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 765 mutex_exit(&fc->fc_mtx); 766 goto found; 767 } 768 769 mutex_exit(&fc->fc_mtx); 770 aprint_error_dev(fc->bdev, "no such binding\n"); 771 return 1; 772 found: 773 #if 0 774 /* shall we do this? */ 775 for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 776 next = STAILQ_NEXT(xfer, link); 777 fw_xfer_free(xfer); 778 } 779 STAILQ_INIT(&fwb->xferlist); 780 #endif 781 782 return 0; 783 } 784 785 int 786 fw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type, int slen, 787 int rlen, int n, struct firewire_comm *fc, void *sc, 788 void (*hand)(struct fw_xfer *)) 789 { 790 struct fw_xfer *xfer; 791 int i; 792 793 for (i = 0; i < n; i++) { 794 xfer = fw_xfer_alloc_buf(type, slen, rlen); 795 if (xfer == NULL) 796 return n; 797 xfer->fc = fc; 798 xfer->sc = sc; 799 xfer->hand = hand; 800 STAILQ_INSERT_TAIL(q, xfer, link); 801 } 802 return n; 803 } 804 805 void 806 fw_xferlist_remove(struct fw_xferlist *q) 807 { 808 struct fw_xfer *xfer, *next; 809 810 for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) { 811 next = STAILQ_NEXT(xfer, link); 812 fw_xfer_free_buf(xfer); 813 } 814 STAILQ_INIT(q); 815 } 816 817 /* 818 * To allocate IEEE1394 XFER structure. 819 */ 820 struct fw_xfer * 821 fw_xfer_alloc(struct malloc_type *type) 822 { 823 struct fw_xfer *xfer; 824 825 xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 826 if (xfer == NULL) 827 return xfer; 828 829 xfer->malloc = type; 830 cv_init(&xfer->cv, "fwxfer"); 831 832 return xfer; 833 } 834 835 struct fw_xfer * 836 fw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 837 { 838 struct fw_xfer *xfer; 839 840 xfer = fw_xfer_alloc(type); 841 if (xfer == NULL) 842 return NULL; 843 xfer->send.pay_len = send_len; 844 xfer->recv.pay_len = recv_len; 845 if (send_len > 0) { 846 xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 847 if (xfer->send.payload == NULL) { 848 fw_xfer_free(xfer); 849 return NULL; 850 } 851 } 852 if (recv_len > 0) { 853 xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 854 if (xfer->recv.payload == NULL) { 855 if (xfer->send.payload != NULL) 856 free(xfer->send.payload, type); 857 fw_xfer_free(xfer); 858 return NULL; 859 } 860 } 861 return xfer; 862 } 863 864 /* 865 * IEEE1394 XFER post process. 866 */ 867 void 868 fw_xfer_done(struct fw_xfer *xfer) 869 { 870 871 if (xfer->hand == NULL) { 872 aprint_error_dev(xfer->fc->bdev, "hand == NULL\n"); 873 return; 874 } 875 876 if (xfer->fc == NULL) 877 panic("fw_xfer_done: why xfer->fc is NULL?"); 878 879 fw_tl_free(xfer->fc, xfer); 880 xfer->hand(xfer); 881 } 882 883 void 884 fw_xfer_unload(struct fw_xfer* xfer) 885 { 886 887 if (xfer == NULL) 888 return; 889 if (xfer->flag & FWXF_INQ) { 890 aprint_error_dev(xfer->fc->bdev, "fw_xfer_free FWXF_INQ\n"); 891 mutex_enter(&xfer->q->q_mtx); 892 STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 893 #if 0 894 xfer->q->queued--; 895 #endif 896 mutex_exit(&xfer->q->q_mtx); 897 } 898 if (xfer->fc != NULL) { 899 #if 1 900 if (xfer->flag == FWXF_START) 901 /* 902 * This could happen if: 903 * 1. We call fwohci_arcv() before fwohci_txd(). 904 * 2. firewire_watch() is called. 905 */ 906 aprint_error_dev(xfer->fc->bdev, 907 "fw_xfer_free FWXF_START\n"); 908 #endif 909 } 910 xfer->flag = FWXF_INIT; 911 xfer->resp = 0; 912 } 913 914 /* 915 * To free IEEE1394 XFER structure. 916 */ 917 void 918 fw_xfer_free(struct fw_xfer* xfer) 919 { 920 921 if (xfer == NULL) { 922 aprint_error("fw_xfer_free: xfer == NULL\n"); 923 return; 924 } 925 fw_xfer_unload(xfer); 926 cv_destroy(&xfer->cv); 927 free(xfer, xfer->malloc); 928 } 929 930 void 931 fw_xfer_free_buf(struct fw_xfer* xfer) 932 { 933 934 if (xfer == NULL) { 935 aprint_error("fw_xfer_free_buf: xfer == NULL\n"); 936 return; 937 } 938 fw_xfer_unload(xfer); 939 if (xfer->send.payload != NULL) { 940 free(xfer->send.payload, xfer->malloc); 941 } 942 if (xfer->recv.payload != NULL) { 943 free(xfer->recv.payload, xfer->malloc); 944 } 945 cv_destroy(&xfer->cv); 946 free(xfer, xfer->malloc); 947 } 948 949 void 950 fw_asy_callback_free(struct fw_xfer *xfer) 951 { 952 953 #if 0 954 printf("asyreq done flag=%d resp=%d\n", xfer->flag, xfer->resp); 955 #endif 956 fw_xfer_free(xfer); 957 } 958 959 /* 960 * To receive self ID. 961 */ 962 void 963 fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len) 964 { 965 uint32_t *p; 966 union fw_self_id *self_id; 967 u_int i, j, node, c_port = 0, i_branch = 0; 968 969 fc->sid_cnt = len / (sizeof(uint32_t) * 2); 970 fc->max_node = fc->nodeid & 0x3f; 971 CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16; 972 fc->status = FWBUSCYMELECT; 973 fc->topology_map->crc_len = 2; 974 fc->topology_map->generation++; 975 fc->topology_map->self_id_count = 0; 976 fc->topology_map->node_count = 0; 977 fc->speed_map->generation++; 978 fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 979 self_id = fc->topology_map->self_id; 980 for (i = 0; i < fc->sid_cnt; i++) { 981 if (sid[1] != ~sid[0]) { 982 aprint_error_dev(fc->bdev, 983 "ERROR invalid self-id packet\n"); 984 sid += 2; 985 continue; 986 } 987 *self_id = *((union fw_self_id *)sid); 988 fc->topology_map->crc_len++; 989 if (self_id->p0.sequel == 0) { 990 fc->topology_map->node_count++; 991 c_port = 0; 992 if (firewire_debug) 993 fw_print_sid(sid[0]); 994 node = self_id->p0.phy_id; 995 if (fc->max_node < node) 996 fc->max_node = self_id->p0.phy_id; 997 /* XXX I'm not sure this is the right speed_map */ 998 fc->speed_map->speed[node][node] = 999 self_id->p0.phy_speed; 1000 for (j = 0; j < node; j++) 1001 fc->speed_map->speed[j][node] = 1002 fc->speed_map->speed[node][j] = 1003 min(fc->speed_map->speed[j][j], 1004 self_id->p0.phy_speed); 1005 if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1006 (self_id->p0.link_active && self_id->p0.contender)) 1007 fc->irm = self_id->p0.phy_id; 1008 if (self_id->p0.port0 >= 0x2) 1009 c_port++; 1010 if (self_id->p0.port1 >= 0x2) 1011 c_port++; 1012 if (self_id->p0.port2 >= 0x2) 1013 c_port++; 1014 } 1015 if (c_port > 2) 1016 i_branch += (c_port - 2); 1017 sid += 2; 1018 self_id++; 1019 fc->topology_map->self_id_count++; 1020 } 1021 /* CRC */ 1022 fc->topology_map->crc = 1023 fw_crc16((uint32_t *)&fc->topology_map->generation, 1024 fc->topology_map->crc_len * 4); 1025 fc->speed_map->crc = fw_crc16((uint32_t *)&fc->speed_map->generation, 1026 fc->speed_map->crc_len * 4); 1027 /* byteswap and copy to CSR */ 1028 p = (uint32_t *)fc->topology_map; 1029 for (i = 0; i <= fc->topology_map->crc_len; i++) 1030 CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1031 p = (uint32_t *)fc->speed_map; 1032 CSRARC(fc, SPED_MAP) = htonl(*p++); 1033 CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1034 /* don't byte-swap uint8_t array */ 1035 memcpy(&CSRARC(fc, SPED_MAP + 8), p, (fc->speed_map->crc_len - 1) * 4); 1036 1037 fc->max_hop = fc->max_node - i_branch; 1038 aprint_normal_dev(fc->bdev, "%d nodes, maxhop <= %d %s irm(%d)%s\n", 1039 fc->max_node + 1, fc->max_hop, 1040 (fc->irm == -1) ? "Not IRM capable" : "cable IRM", 1041 fc->irm, 1042 (fc->irm == fc->nodeid) ? " (me)" : ""); 1043 1044 if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1045 if (fc->irm == fc->nodeid) { 1046 fc->status = FWBUSMGRDONE; 1047 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1048 fw_bmr(fc); 1049 } else { 1050 fc->status = FWBUSMGRELECT; 1051 callout_schedule(&fc->bmr_callout, hz/8); 1052 } 1053 } else 1054 fc->status = FWBUSMGRDONE; 1055 1056 callout_schedule(&fc->busprobe_callout, hz/4); 1057 } 1058 1059 /* 1060 * Generic packet receiving process. 1061 */ 1062 void 1063 fw_rcv(struct fw_rcv_buf *rb) 1064 { 1065 struct fw_pkt *fp, *resfp; 1066 struct fw_bind *bind; 1067 int tcode; 1068 int i, len, oldstate; 1069 #if 0 1070 { 1071 uint32_t *qld; 1072 int i; 1073 qld = (uint32_t *)buf; 1074 printf("spd %d len:%d\n", spd, len); 1075 for (i = 0; i <= len && i < 32; i+= 4) { 1076 printf("0x%08x ", ntohl(qld[i/4])); 1077 if ((i % 16) == 15) printf("\n"); 1078 } 1079 if ((i % 16) != 15) printf("\n"); 1080 } 1081 #endif 1082 fp = (struct fw_pkt *)rb->vec[0].iov_base; 1083 tcode = fp->mode.common.tcode; 1084 switch (tcode) { 1085 case FWTCODE_WRES: 1086 case FWTCODE_RRESQ: 1087 case FWTCODE_RRESB: 1088 case FWTCODE_LRES: 1089 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1090 fp->mode.hdr.tlrt >> 2, tcode); 1091 if (rb->xfer == NULL) { 1092 aprint_error_dev(rb->fc->bdev, "unknown response" 1093 " %s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n", 1094 tcode_str[tcode], tcode, 1095 fp->mode.hdr.src, 1096 fp->mode.hdr.tlrt >> 2, 1097 fp->mode.hdr.tlrt & 3, 1098 fp->mode.rresq.data); 1099 #if 0 1100 printf("try ad-hoc work around!!\n"); 1101 rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1102 (fp->mode.hdr.tlrt >> 2) ^ 3); 1103 if (rb->xfer == NULL) { 1104 printf("no use...\n"); 1105 return; 1106 } 1107 #else 1108 return; 1109 #endif 1110 } 1111 fw_rcv_copy(rb); 1112 if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 1113 rb->xfer->resp = EIO; 1114 else 1115 rb->xfer->resp = 0; 1116 /* make sure the packet is drained in AT queue */ 1117 oldstate = rb->xfer->flag; 1118 rb->xfer->flag = FWXF_RCVD; 1119 switch (oldstate) { 1120 case FWXF_SENT: 1121 fw_xfer_done(rb->xfer); 1122 break; 1123 case FWXF_START: 1124 #if 0 1125 if (firewire_debug) 1126 printf("not sent yet tl=%x\n", rb->xfer->tl); 1127 #endif 1128 break; 1129 default: 1130 aprint_error_dev(rb->fc->bdev, 1131 "unexpected flag 0x%02x\n", rb->xfer->flag); 1132 } 1133 return; 1134 case FWTCODE_WREQQ: 1135 case FWTCODE_WREQB: 1136 case FWTCODE_RREQQ: 1137 case FWTCODE_RREQB: 1138 case FWTCODE_LREQ: 1139 bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 1140 fp->mode.rreqq.dest_lo); 1141 if (bind == NULL) { 1142 #if 1 1143 aprint_error_dev(rb->fc->bdev, "Unknown service addr" 1144 " 0x%04x:0x%08x %s(%x) src=0x%x data=%x\n", 1145 fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo, 1146 tcode_str[tcode], tcode, 1147 fp->mode.hdr.src, ntohl(fp->mode.wreqq.data)); 1148 #endif 1149 if (rb->fc->status == FWBUSINIT) { 1150 aprint_error_dev(rb->fc->bdev, 1151 "cannot respond(bus reset)!\n"); 1152 return; 1153 } 1154 rb->xfer = fw_xfer_alloc(M_FW); 1155 if (rb->xfer == NULL) 1156 return; 1157 rb->xfer->send.spd = rb->spd; 1158 rb->xfer->send.pay_len = 0; 1159 resfp = &rb->xfer->send.hdr; 1160 switch (tcode) { 1161 case FWTCODE_WREQQ: 1162 case FWTCODE_WREQB: 1163 resfp->mode.hdr.tcode = FWTCODE_WRES; 1164 break; 1165 case FWTCODE_RREQQ: 1166 resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1167 break; 1168 case FWTCODE_RREQB: 1169 resfp->mode.hdr.tcode = FWTCODE_RRESB; 1170 break; 1171 case FWTCODE_LREQ: 1172 resfp->mode.hdr.tcode = FWTCODE_LRES; 1173 break; 1174 } 1175 resfp->mode.hdr.dst = fp->mode.hdr.src; 1176 resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1177 resfp->mode.hdr.pri = fp->mode.hdr.pri; 1178 resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 1179 resfp->mode.rresb.extcode = 0; 1180 resfp->mode.rresb.len = 0; 1181 /* 1182 rb->xfer->hand = fw_xferwake; 1183 */ 1184 rb->xfer->hand = fw_xfer_free; 1185 if (fw_asyreq(rb->fc, -1, rb->xfer)) { 1186 fw_xfer_free(rb->xfer); 1187 return; 1188 } 1189 return; 1190 } 1191 len = 0; 1192 for (i = 0; i < rb->nvec; i++) 1193 len += rb->vec[i].iov_len; 1194 mutex_enter(&bind->fwb_mtx); 1195 rb->xfer = STAILQ_FIRST(&bind->xferlist); 1196 if (rb->xfer == NULL) { 1197 mutex_exit(&bind->fwb_mtx); 1198 #if 1 1199 aprint_error_dev(rb->fc->bdev, 1200 "Discard a packet for this bind.\n"); 1201 #endif 1202 return; 1203 } 1204 STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1205 mutex_exit(&bind->fwb_mtx); 1206 fw_rcv_copy(rb); 1207 rb->xfer->hand(rb->xfer); 1208 return; 1209 1210 default: 1211 aprint_error_dev(rb->fc->bdev, "unknow tcode %d\n", tcode); 1212 break; 1213 } 1214 } 1215 1216 /* 1217 * CRC16 check-sum for IEEE1394 register blocks. 1218 */ 1219 uint16_t 1220 fw_crc16(uint32_t *ptr, uint32_t len) 1221 { 1222 uint32_t i, sum, crc = 0; 1223 int shift; 1224 1225 len = (len + 3) & ~3; 1226 for (i = 0; i < len; i+= 4) { 1227 for (shift = 28; shift >= 0; shift -= 4) { 1228 sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 1229 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum; 1230 } 1231 crc &= 0xffff; 1232 } 1233 return (uint16_t)crc; 1234 } 1235 1236 int 1237 fw_open_isodma(struct firewire_comm *fc, int tx) 1238 { 1239 struct fw_xferq **xferqa; 1240 struct fw_xferq *xferq; 1241 int i; 1242 1243 if (tx) 1244 xferqa = fc->it; 1245 else 1246 xferqa = fc->ir; 1247 1248 mutex_enter(&fc->fc_mtx); 1249 for (i = 0; i < fc->nisodma; i++) { 1250 xferq = xferqa[i]; 1251 if (!(xferq->flag & FWXFERQ_OPEN)) { 1252 xferq->flag |= FWXFERQ_OPEN; 1253 break; 1254 } 1255 } 1256 if (i == fc->nisodma) { 1257 aprint_error_dev(fc->bdev, "no free dma channel (tx=%d)\n", tx); 1258 i = -1; 1259 } 1260 mutex_exit(&fc->fc_mtx); 1261 return i; 1262 } 1263 1264 /* 1265 * Async. request with given xfer structure. 1266 */ 1267 static void 1268 fw_asystart(struct fw_xfer *xfer) 1269 { 1270 struct firewire_comm *fc = xfer->fc; 1271 1272 /* Protect from interrupt/timeout */ 1273 mutex_enter(&xfer->q->q_mtx); 1274 xfer->flag = FWXF_INQ; 1275 STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 1276 #if 0 1277 xfer->q->queued++; 1278 #endif 1279 mutex_exit(&xfer->q->q_mtx); 1280 /* XXX just queue for mbuf */ 1281 if (xfer->mbuf == NULL) 1282 xfer->q->start(fc); 1283 return; 1284 } 1285 1286 static void 1287 firewire_xfer_timeout(struct firewire_comm *fc) 1288 { 1289 struct fw_xfer *xfer; 1290 struct timeval tv; 1291 struct timeval split_timeout; 1292 STAILQ_HEAD(, fw_xfer) xfer_timeout; 1293 int i; 1294 1295 split_timeout.tv_sec = 0; 1296 split_timeout.tv_usec = 200 * 1000; /* 200 msec */ 1297 1298 microtime(&tv); 1299 timersub(&tv, &split_timeout, &tv); 1300 STAILQ_INIT(&xfer_timeout); 1301 1302 mutex_enter(&fc->tlabel_lock); 1303 for (i = 0; i < 0x40; i++) { 1304 while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 1305 if ((xfer->flag & FWXF_SENT) == 0) 1306 /* not sent yet */ 1307 break; 1308 if (timercmp(&xfer->tv, &tv, >)) 1309 /* the rests are newer than this */ 1310 break; 1311 aprint_error_dev(fc->bdev, 1312 "split transaction timeout: tl=0x%x flag=0x%02x\n", 1313 i, xfer->flag); 1314 fw_dump_hdr(&xfer->send.hdr, "send"); 1315 xfer->resp = ETIMEDOUT; 1316 STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel); 1317 STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel); 1318 } 1319 } 1320 mutex_exit(&fc->tlabel_lock); 1321 fc->timeout(fc); 1322 1323 STAILQ_FOREACH(xfer, &xfer_timeout, tlabel) 1324 xfer->hand(xfer); 1325 } 1326 1327 #define WATCHDOG_HZ 10 1328 static void 1329 firewire_watchdog(void *arg) 1330 { 1331 struct firewire_comm *fc; 1332 static int watchdog_clock = 0; 1333 1334 fc = (struct firewire_comm *)arg; 1335 1336 /* 1337 * At boot stage, the device interrupt is disabled and 1338 * We encounter a timeout easily. To avoid this, 1339 * ignore clock interrupt for a while. 1340 */ 1341 if (watchdog_clock > WATCHDOG_HZ * 15) 1342 firewire_xfer_timeout(fc); 1343 else 1344 watchdog_clock++; 1345 1346 callout_schedule(&fc->timeout_callout, hz / WATCHDOG_HZ); 1347 } 1348 1349 static void 1350 fw_xferq_drain(struct fw_xferq *xferq) 1351 { 1352 struct fw_xfer *xfer; 1353 1354 while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 1355 STAILQ_REMOVE_HEAD(&xferq->q, link); 1356 #if 0 1357 xferq->queued--; 1358 #endif 1359 xfer->resp = EAGAIN; 1360 xfer->flag = FWXF_SENTERR; 1361 fw_xfer_done(xfer); 1362 } 1363 } 1364 1365 static void 1366 fw_reset_csr(struct firewire_comm *fc) 1367 { 1368 int i; 1369 1370 CSRARC(fc, STATE_CLEAR) = 1371 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14; 1372 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 1373 CSRARC(fc, NODE_IDS) = 0x3f; 1374 1375 CSRARC(fc, TOPO_MAP + 8) = 0; 1376 fc->irm = -1; 1377 1378 fc->max_node = -1; 1379 1380 for (i = 2; i < 0x100/4 - 2; i++) 1381 CSRARC(fc, SPED_MAP + i * 4) = 0; 1382 CSRARC(fc, STATE_CLEAR) = 1383 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14; 1384 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 1385 CSRARC(fc, RESET_START) = 0; 1386 CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 1387 CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 1388 CSRARC(fc, CYCLE_TIME) = 0x0; 1389 CSRARC(fc, BUS_TIME) = 0x0; 1390 CSRARC(fc, BUS_MGR_ID) = 0x3f; 1391 CSRARC(fc, BANDWIDTH_AV) = 4915; 1392 CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 1393 CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 1394 CSRARC(fc, IP_CHANNELS) = (1 << 31); 1395 1396 CSRARC(fc, CONF_ROM) = 0x04 << 24; 1397 CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 1398 CSRARC(fc, CONF_ROM + 8) = 1399 1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 | 0xff << 16 | 0x09 << 8; 1400 CSRARC(fc, CONF_ROM + 0xc) = 0; 1401 1402 /* DV depend CSRs see blue book */ 1403 CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 1404 CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 1405 1406 CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14); 1407 CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 1408 } 1409 1410 static void 1411 fw_init_crom(struct firewire_comm *fc) 1412 { 1413 struct crom_src *src; 1414 1415 src = &fc->crom_src_buf->src; 1416 memset(src, 0, sizeof(struct crom_src)); 1417 1418 /* BUS info sample */ 1419 src->hdr.info_len = 4; 1420 1421 src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 1422 1423 src->businfo.irmc = 1; 1424 src->businfo.cmc = 1; 1425 src->businfo.isc = 1; 1426 src->businfo.bmc = 1; 1427 src->businfo.pmc = 0; 1428 src->businfo.cyc_clk_acc = 100; 1429 src->businfo.max_rec = fc->maxrec; 1430 src->businfo.max_rom = MAXROM_4; 1431 src->businfo.generation = FW_GENERATION_CHANGEABLE; 1432 src->businfo.link_spd = fc->speed; 1433 1434 src->businfo.eui64.hi = fc->eui.hi; 1435 src->businfo.eui64.lo = fc->eui.lo; 1436 1437 STAILQ_INIT(&src->chunk_list); 1438 1439 fc->crom_src = src; 1440 fc->crom_root = &fc->crom_src_buf->root; 1441 } 1442 1443 static void 1444 fw_reset_crom(struct firewire_comm *fc) 1445 { 1446 struct crom_src_buf *buf; 1447 struct crom_src *src; 1448 struct crom_chunk *root; 1449 1450 buf = fc->crom_src_buf; 1451 src = fc->crom_src; 1452 root = fc->crom_root; 1453 1454 STAILQ_INIT(&src->chunk_list); 1455 1456 memset(root, 0, sizeof(struct crom_chunk)); 1457 crom_add_chunk(src, NULL, root, 0); 1458 crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 1459 /* private company_id */ 1460 crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 1461 crom_add_simple_text(src, root, &buf->vendor, PROJECT_STR); 1462 crom_add_entry(root, CSRKEY_HW, __NetBSD_Version__); 1463 crom_add_simple_text(src, root, &buf->hw, hostname); 1464 } 1465 1466 /* 1467 * dump packet header 1468 */ 1469 static void 1470 fw_dump_hdr(struct fw_pkt *fp, const char *prefix) 1471 { 1472 1473 printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x " 1474 "src=0x%03x\n", prefix, 1475 fp->mode.hdr.dst & 0x3f, 1476 fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3, 1477 fp->mode.hdr.tcode, fp->mode.hdr.pri, 1478 fp->mode.hdr.src); 1479 } 1480 1481 /* 1482 * To free transaction label. 1483 */ 1484 static void 1485 fw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 1486 { 1487 struct fw_xfer *txfer; 1488 1489 if (xfer->tl < 0) 1490 return; 1491 1492 mutex_enter(&fc->tlabel_lock); 1493 #if 1 /* make sure the label is allocated */ 1494 STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel) 1495 if (txfer == xfer) 1496 break; 1497 if (txfer == NULL) { 1498 mutex_exit(&fc->tlabel_lock); 1499 aprint_error_dev(fc->bdev, 1500 "the xfer is not in the queue (tlabel=%d, flag=0x%x)\n", 1501 xfer->tl, xfer->flag); 1502 fw_dump_hdr(&xfer->send.hdr, "send"); 1503 fw_dump_hdr(&xfer->recv.hdr, "recv"); 1504 KASSERT(FALSE); 1505 return; 1506 } 1507 #endif 1508 1509 STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel); 1510 mutex_exit(&fc->tlabel_lock); 1511 return; 1512 } 1513 1514 /* 1515 * To obtain XFER structure by transaction label. 1516 */ 1517 static struct fw_xfer * 1518 fw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode) 1519 { 1520 struct fw_xfer *xfer; 1521 int req; 1522 1523 mutex_enter(&fc->tlabel_lock); 1524 STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel) 1525 if (xfer->send.hdr.mode.hdr.dst == node) { 1526 mutex_exit(&fc->tlabel_lock); 1527 KASSERT(xfer->tl == tlabel); 1528 /* extra sanity check */ 1529 req = xfer->send.hdr.mode.hdr.tcode; 1530 if (xfer->fc->tcode[req].valid_res != tcode) { 1531 aprint_error_dev(fc->bdev, 1532 "invalid response tcode (0x%x for 0x%x)\n", 1533 tcode, req); 1534 return NULL; 1535 } 1536 1537 if (firewire_debug > 2) 1538 printf("fw_tl2xfer: found tl=%d\n", tlabel); 1539 return xfer; 1540 } 1541 mutex_exit(&fc->tlabel_lock); 1542 if (firewire_debug > 1) 1543 printf("fw_tl2xfer: not found tl=%d\n", tlabel); 1544 return NULL; 1545 } 1546 1547 /* 1548 * To configure PHY. 1549 */ 1550 static void 1551 fw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1552 { 1553 struct fw_xfer *xfer; 1554 struct fw_pkt *fp; 1555 1556 fc->status = FWBUSPHYCONF; 1557 1558 xfer = fw_xfer_alloc(M_FW); 1559 if (xfer == NULL) 1560 return; 1561 xfer->fc = fc; 1562 xfer->hand = fw_asy_callback_free; 1563 1564 fp = &xfer->send.hdr; 1565 fp->mode.ld[1] = 0; 1566 if (root_node >= 0) 1567 fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1568 if (gap_count >= 0) 1569 fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1570 fp->mode.ld[2] = ~fp->mode.ld[1]; 1571 /* XXX Dangerous, how to pass PHY packet to device driver */ 1572 fp->mode.common.tcode |= FWTCODE_PHY; 1573 1574 if (firewire_debug) 1575 printf("root_node=%d gap_count=%d\n", root_node, gap_count); 1576 fw_asyreq(fc, -1, xfer); 1577 } 1578 1579 /* 1580 * Dump self ID. 1581 */ 1582 static void 1583 fw_print_sid(uint32_t sid) 1584 { 1585 union fw_self_id *s; 1586 1587 s = (union fw_self_id *) &sid; 1588 if (s->p0.sequel) { 1589 if (s->p1.sequence_num == FW_SELF_ID_PAGE0) 1590 printf("node:%d p3:%d p4:%d p5:%d p6:%d p7:%d" 1591 "p8:%d p9:%d p10:%d\n", 1592 s->p1.phy_id, s->p1.port3, s->p1.port4, 1593 s->p1.port5, s->p1.port6, s->p1.port7, 1594 s->p1.port8, s->p1.port9, s->p1.port10); 1595 else if (s->p2.sequence_num == FW_SELF_ID_PAGE1) 1596 printf("node:%d p11:%d p12:%d p13:%d p14:%d p15:%d\n", 1597 s->p2.phy_id, s->p2.port11, s->p2.port12, 1598 s->p2.port13, s->p2.port14, s->p2.port15); 1599 else 1600 printf("node:%d Unknown Self ID Page number %d\n", 1601 s->p1.phy_id, s->p1.sequence_num); 1602 } else 1603 printf("node:%d link:%d gap:%d spd:%d con:%d pwr:%d" 1604 " p0:%d p1:%d p2:%d i:%d m:%d\n", 1605 s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1606 s->p0.phy_speed, s->p0.contender, 1607 s->p0.power_class, s->p0.port0, s->p0.port1, 1608 s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1609 } 1610 1611 /* 1612 * To probe devices on the IEEE1394 bus. 1613 */ 1614 static void 1615 fw_bus_probe(struct firewire_comm *fc) 1616 { 1617 struct fw_device *fwdev; 1618 1619 mutex_enter(&fc->wait_lock); 1620 fc->status = FWBUSEXPLORE; 1621 1622 /* Invalidate all devices, just after bus reset. */ 1623 if (firewire_debug) 1624 printf("iterate and invalidate all nodes\n"); 1625 mutex_enter(&fc->fc_mtx); 1626 STAILQ_FOREACH(fwdev, &fc->devices, link) 1627 if (fwdev->status != FWDEVINVAL) { 1628 fwdev->status = FWDEVINVAL; 1629 fwdev->rcnt = 0; 1630 if (firewire_debug) 1631 printf("Invalidate Dev ID: %08x%08x\n", 1632 fwdev->eui.hi, fwdev->eui.lo); 1633 } else 1634 if (firewire_debug) 1635 printf("Dev ID: %08x%08x already invalid\n", 1636 fwdev->eui.hi, fwdev->eui.lo); 1637 mutex_exit(&fc->fc_mtx); 1638 1639 cv_signal(&fc->fc_cv); 1640 mutex_exit(&fc->wait_lock); 1641 } 1642 1643 static int 1644 fw_explore_read_quads(struct fw_device *fwdev, int offset, uint32_t *quad, 1645 int length) 1646 { 1647 struct fw_xfer *xfer; 1648 uint32_t tmp; 1649 int i, error; 1650 1651 for (i = 0; i < length; i++, offset += sizeof(uint32_t)) { 1652 xfer = fwmem_read_quad(fwdev, NULL, -1, 0xffff, 1653 0xf0000000 | offset, (void *)&tmp, fw_xferwake); 1654 if (xfer == NULL) 1655 return -1; 1656 fw_xferwait(xfer); 1657 1658 if (xfer->resp == 0) 1659 quad[i] = ntohl(tmp); 1660 1661 error = xfer->resp; 1662 fw_xfer_free(xfer); 1663 if (error) 1664 return error; 1665 } 1666 return 0; 1667 } 1668 1669 1670 static int 1671 fw_explore_csrblock(struct fw_device *fwdev, int offset, int recur) 1672 { 1673 int err, i, off; 1674 struct csrdirectory *dir; 1675 struct csrreg *reg; 1676 1677 1678 dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)]; 1679 err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, (uint32_t *)dir, 1680 1); 1681 if (err) 1682 return -1; 1683 1684 offset += sizeof(uint32_t); 1685 reg = (struct csrreg *)&fwdev->csrrom[offset / sizeof(uint32_t)]; 1686 err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, (uint32_t *)reg, 1687 dir->crc_len); 1688 if (err) 1689 return -1; 1690 1691 /* XXX check CRC */ 1692 1693 off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1); 1694 if (fwdev->rommax < off) 1695 fwdev->rommax = off; 1696 1697 if (recur == 0) 1698 return 0; 1699 1700 for (i = 0; i < dir->crc_len; i++, offset += sizeof(uint32_t)) { 1701 if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D) 1702 recur = 1; 1703 else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L) 1704 recur = 0; 1705 else 1706 continue; 1707 1708 off = offset + reg[i].val * sizeof(uint32_t); 1709 if (off > CROMSIZE) { 1710 aprint_error_dev(fwdev->fc->bdev, "invalid offset %d\n", 1711 off); 1712 return -1; 1713 } 1714 err = fw_explore_csrblock(fwdev, off, recur); 1715 if (err) 1716 return -1; 1717 } 1718 return 0; 1719 } 1720 1721 static int 1722 fw_explore_node(struct fw_device *dfwdev) 1723 { 1724 struct firewire_comm *fc; 1725 struct fw_device *fwdev, *pfwdev, *tfwdev; 1726 struct csrhdr *hdr; 1727 struct bus_info *binfo; 1728 uint32_t *csr, speed_test = 0; 1729 int err, node; 1730 1731 fc = dfwdev->fc; 1732 csr = dfwdev->csrrom; 1733 node = dfwdev->dst; 1734 1735 /* First quad */ 1736 err = fw_explore_read_quads(dfwdev, CSRROMOFF, csr, 1); 1737 if (err) { 1738 aprint_error_dev(fc->bdev, 1739 "node%d: explore_read_quads failure\n", node); 1740 dfwdev->status = FWDEVINVAL; 1741 return -1; 1742 } 1743 hdr = (struct csrhdr *)csr; 1744 if (hdr->info_len != 4) { 1745 if (firewire_debug) 1746 printf("node%d: wrong bus info len(%d)\n", 1747 node, hdr->info_len); 1748 dfwdev->status = FWDEVINVAL; 1749 return -1; 1750 } 1751 1752 /* bus info */ 1753 err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4); 1754 if (err) { 1755 aprint_error_dev(fc->bdev, "node%d: error reading 0x04\n", 1756 node); 1757 dfwdev->status = FWDEVINVAL; 1758 return -1; 1759 } 1760 binfo = (struct bus_info *)&csr[1]; 1761 if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) { 1762 aprint_error_dev(fc->bdev, "node%d: invalid bus name 0x%08x\n", 1763 node, binfo->bus_name); 1764 dfwdev->status = FWDEVINVAL; 1765 return -1; 1766 } 1767 if (firewire_debug) 1768 printf("node(%d) BUS INFO BLOCK:\n" 1769 "irmc(%d) cmc(%d) isc(%d) bmc(%d) pmc(%d) " 1770 "cyc_clk_acc(%d) max_rec(%d) max_rom(%d) " 1771 "generation(%d) link_spd(%d)\n", 1772 node, binfo->irmc, binfo->cmc, binfo->isc, 1773 binfo->bmc, binfo->pmc, binfo->cyc_clk_acc, 1774 binfo->max_rec, binfo->max_rom, 1775 binfo->generation, binfo->link_spd); 1776 1777 mutex_enter(&fc->fc_mtx); 1778 STAILQ_FOREACH(fwdev, &fc->devices, link) 1779 if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64)) 1780 break; 1781 mutex_exit(&fc->fc_mtx); 1782 if (fwdev == NULL) { 1783 /* new device */ 1784 fwdev = 1785 malloc(sizeof(struct fw_device), M_FW, M_NOWAIT | M_ZERO); 1786 if (fwdev == NULL) { 1787 if (firewire_debug) 1788 printf("node%d: no memory\n", node); 1789 return -1; 1790 } 1791 fwdev->fc = fc; 1792 fwdev->eui = binfo->eui64; 1793 fwdev->dst = dfwdev->dst; 1794 fwdev->maxrec = dfwdev->maxrec; 1795 fwdev->status = FWDEVNEW; 1796 /* 1797 * Pre-1394a-2000 didn't have link_spd in 1798 * the Bus Info block, so try and use the 1799 * speed map value. 1800 * 1394a-2000 compliant devices only use 1801 * the Bus Info Block link spd value, so 1802 * ignore the speed map alltogether. SWB 1803 */ 1804 if (binfo->link_spd == FWSPD_S100 /* 0 */) { 1805 aprint_normal_dev(fc->bdev, 1806 "Pre 1394a-2000 detected\n"); 1807 fwdev->speed = fc->speed_map->speed[fc->nodeid][node]; 1808 } else 1809 fwdev->speed = binfo->link_spd; 1810 /* 1811 * Test this speed with a read to the CSRROM. 1812 * If it fails, slow down the speed and retry. 1813 */ 1814 while (fwdev->speed > FWSPD_S100 /* 0 */) { 1815 err = fw_explore_read_quads(fwdev, CSRROMOFF, 1816 &speed_test, 1); 1817 if (err) { 1818 aprint_error_dev(fc->bdev, "fwdev->speed(%s)" 1819 " decremented due to negotiation\n", 1820 fw_linkspeed[fwdev->speed]); 1821 fwdev->speed--; 1822 } else 1823 break; 1824 } 1825 /* 1826 * If the fwdev is not found in the 1827 * fc->devices TAILQ, then we will add it. 1828 */ 1829 pfwdev = NULL; 1830 mutex_enter(&fc->fc_mtx); 1831 STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1832 if (tfwdev->eui.hi > fwdev->eui.hi || 1833 (tfwdev->eui.hi == fwdev->eui.hi && 1834 tfwdev->eui.lo > fwdev->eui.lo)) 1835 break; 1836 pfwdev = tfwdev; 1837 } 1838 if (pfwdev == NULL) 1839 STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1840 else 1841 STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1842 mutex_exit(&fc->fc_mtx); 1843 1844 aprint_normal_dev(fc->bdev, "New %s device ID:%08x%08x\n", 1845 fw_linkspeed[fwdev->speed], fwdev->eui.hi, fwdev->eui.lo); 1846 } else { 1847 fwdev->dst = node; 1848 fwdev->status = FWDEVINIT; 1849 /* unchanged ? */ 1850 if (memcmp(csr, fwdev->csrrom, sizeof(uint32_t) * 5) == 0) { 1851 if (firewire_debug) 1852 printf("node%d: crom unchanged\n", node); 1853 return 0; 1854 } 1855 } 1856 1857 memset(fwdev->csrrom, 0, CROMSIZE); 1858 1859 /* copy first quad and bus info block */ 1860 memcpy(fwdev->csrrom, csr, sizeof(uint32_t) * 5); 1861 fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4; 1862 1863 err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */ 1864 1865 if (err) { 1866 if (firewire_debug) 1867 printf("explore csrblock failed err(%d)\n", err); 1868 fwdev->status = FWDEVINVAL; 1869 fwdev->csrrom[0] = 0; 1870 } 1871 return err; 1872 } 1873 1874 /* 1875 * Find the self_id packet for a node, ignoring sequels. 1876 */ 1877 static union fw_self_id * 1878 fw_find_self_id(struct firewire_comm *fc, int node) 1879 { 1880 uint32_t i; 1881 union fw_self_id *s; 1882 1883 for (i = 0; i < fc->topology_map->self_id_count; i++) { 1884 s = &fc->topology_map->self_id[i]; 1885 if (s->p0.sequel) 1886 continue; 1887 if (s->p0.phy_id == node) 1888 return s; 1889 } 1890 return 0; 1891 } 1892 1893 static void 1894 fw_explore(struct firewire_comm *fc) 1895 { 1896 struct fw_device *dfwdev; 1897 union fw_self_id *fwsid; 1898 int node, err, i, todo, todo2, trys; 1899 char nodes[63]; 1900 1901 todo = 0; 1902 dfwdev = malloc(sizeof(*dfwdev), M_TEMP, M_NOWAIT); 1903 if (dfwdev == NULL) 1904 return; 1905 /* setup dummy fwdev */ 1906 dfwdev->fc = fc; 1907 dfwdev->speed = 0; 1908 dfwdev->maxrec = 8; /* 512 */ 1909 dfwdev->status = FWDEVINIT; 1910 1911 for (node = 0; node <= fc->max_node; node++) { 1912 /* We don't probe myself and linkdown nodes */ 1913 if (node == fc->nodeid) { 1914 if (firewire_debug) 1915 printf("found myself node(%d) fc->nodeid(%d)" 1916 " fc->max_node(%d)\n", 1917 node, fc->nodeid, fc->max_node); 1918 continue; 1919 } else if (firewire_debug) 1920 printf("node(%d) fc->max_node(%d) found\n", 1921 node, fc->max_node); 1922 fwsid = fw_find_self_id(fc, node); 1923 if (!fwsid || !fwsid->p0.link_active) { 1924 if (firewire_debug) 1925 printf("node%d: link down\n", node); 1926 continue; 1927 } 1928 nodes[todo++] = node; 1929 } 1930 1931 for (trys = 0; todo > 0 && trys < 3; trys++) { 1932 todo2 = 0; 1933 for (i = 0; i < todo; i++) { 1934 dfwdev->dst = nodes[i]; 1935 err = fw_explore_node(dfwdev); 1936 if (err) 1937 nodes[todo2++] = nodes[i]; 1938 if (firewire_debug) 1939 printf("node %d, err = %d\n", nodes[i], err); 1940 } 1941 todo = todo2; 1942 } 1943 free(dfwdev, M_TEMP); 1944 } 1945 1946 static void 1947 fw_bus_probe_thread(void *arg) 1948 { 1949 struct firewire_comm *fc = (struct firewire_comm *)arg; 1950 1951 /* 1952 * Tell config we've scanned the bus. 1953 * 1954 * XXX This is not right -- we haven't actually scanned it. We 1955 * probably ought to call this after the first bus exploration. 1956 * 1957 * bool once = false; 1958 * ... 1959 * fw_attach_dev(fc); 1960 * if (!once) { 1961 * config_pending_decr(); 1962 * once = true; 1963 * } 1964 */ 1965 config_pending_decr(fc->bdev); 1966 1967 mutex_enter(&fc->wait_lock); 1968 while (fc->status != FWBUSDETACH) { 1969 if (fc->status == FWBUSEXPLORE) { 1970 mutex_exit(&fc->wait_lock); 1971 fw_explore(fc); 1972 fc->status = FWBUSEXPDONE; 1973 if (firewire_debug) 1974 printf("bus_explore done\n"); 1975 fw_attach_dev(fc); 1976 mutex_enter(&fc->wait_lock); 1977 } 1978 cv_wait_sig(&fc->fc_cv, &fc->wait_lock); 1979 } 1980 fc->status = FWBUSDETACHOK; 1981 cv_signal(&fc->fc_cv); 1982 mutex_exit(&fc->wait_lock); 1983 kthread_exit(0); 1984 1985 /* NOTREACHED */ 1986 } 1987 1988 static const char * 1989 fw_get_devclass(struct fw_device *fwdev) 1990 { 1991 struct crom_context cc; 1992 struct csrreg *reg; 1993 1994 crom_init_context(&cc, fwdev->csrrom); 1995 reg = crom_search_key(&cc, CSRKEY_VER); 1996 if (reg == NULL) 1997 return "null"; 1998 1999 switch (reg->val) { 2000 case CSR_PROTAVC: 2001 return "av/c"; 2002 case CSR_PROTCAL: 2003 return "cal"; 2004 case CSR_PROTEHS: 2005 return "ehs"; 2006 case CSR_PROTHAVI: 2007 return "havi"; 2008 case CSR_PROTCAM104: 2009 return "cam104"; 2010 case CSR_PROTCAM120: 2011 return "cam120"; 2012 case CSR_PROTCAM130: 2013 return "cam130"; 2014 case CSR_PROTDPP: 2015 return "printer"; 2016 case CSR_PROTIICP: 2017 return "iicp"; 2018 case CSRVAL_T10SBP2: 2019 return "sbp"; 2020 default: 2021 if (firewire_debug) 2022 printf("%s: reg->val 0x%x\n", 2023 __func__, reg->val); 2024 return "sbp"; 2025 } 2026 } 2027 2028 /* 2029 * To attach sub-devices layer onto IEEE1394 bus. 2030 */ 2031 static void 2032 fw_attach_dev(struct firewire_comm *fc) 2033 { 2034 struct firewire_softc *sc = device_private(fc->bdev); 2035 struct firewire_dev_list *devlist, *elm; 2036 struct fw_device *fwdev, *next; 2037 struct firewire_dev_comm *fdc; 2038 struct fw_attach_args fwa; 2039 int locs[IEEE1394IFCF_NLOCS]; 2040 2041 fwa.name = "null"; 2042 fwa.fc = fc; 2043 2044 mutex_enter(&fc->fc_mtx); 2045 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 2046 next = STAILQ_NEXT(fwdev, link); 2047 mutex_exit(&fc->fc_mtx); 2048 switch (fwdev->status) { 2049 case FWDEVNEW: 2050 devlist = malloc(sizeof(struct firewire_dev_list), 2051 M_DEVBUF, M_NOWAIT); 2052 if (devlist == NULL) { 2053 aprint_error_dev(fc->bdev, 2054 "memory allocation failed\n"); 2055 break; 2056 } 2057 2058 locs[IEEE1394IFCF_EUIHI] = fwdev->eui.hi; 2059 locs[IEEE1394IFCF_EUILO] = fwdev->eui.lo; 2060 2061 fwa.name = fw_get_devclass(fwdev); 2062 fwa.fwdev = fwdev; 2063 fwdev->dev = config_found_sm_loc(sc->dev, "ieee1394if", 2064 locs, &fwa, firewire_print, config_stdsubmatch); 2065 if (fwdev->dev == NULL) { 2066 free(devlist, M_DEVBUF); 2067 break; 2068 } 2069 2070 devlist->fwdev = fwdev; 2071 devlist->dev = fwdev->dev; 2072 2073 mutex_enter(&fc->fc_mtx); 2074 if (SLIST_EMPTY(&sc->devlist)) 2075 SLIST_INSERT_HEAD(&sc->devlist, devlist, link); 2076 else { 2077 for (elm = SLIST_FIRST(&sc->devlist); 2078 SLIST_NEXT(elm, link) != NULL; 2079 elm = SLIST_NEXT(elm, link)); 2080 SLIST_INSERT_AFTER(elm, devlist, link); 2081 } 2082 mutex_exit(&fc->fc_mtx); 2083 2084 /* FALLTHROUGH */ 2085 2086 case FWDEVINIT: 2087 case FWDEVATTACHED: 2088 fwdev->status = FWDEVATTACHED; 2089 break; 2090 2091 case FWDEVINVAL: 2092 fwdev->rcnt++; 2093 if (firewire_debug) 2094 printf("fwdev->rcnt(%d), hold_count(%d)\n", 2095 fwdev->rcnt, hold_count); 2096 break; 2097 2098 default: 2099 /* XXX */ 2100 break; 2101 } 2102 mutex_enter(&fc->fc_mtx); 2103 } 2104 mutex_exit(&fc->fc_mtx); 2105 2106 SLIST_FOREACH(devlist, &sc->devlist, link) { 2107 fdc = device_private(devlist->dev); 2108 if (fdc->post_explore != NULL) 2109 fdc->post_explore(fdc); 2110 } 2111 2112 for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 2113 next = STAILQ_NEXT(fwdev, link); 2114 if (fwdev->rcnt > 0 && fwdev->rcnt > hold_count) { 2115 /* 2116 * Remove devices which have not been seen 2117 * for a while. 2118 */ 2119 SLIST_FOREACH(devlist, &sc->devlist, link) 2120 if (devlist->fwdev == fwdev) 2121 break; 2122 2123 if (devlist == NULL) 2124 continue; 2125 2126 if (devlist->fwdev != fwdev) 2127 panic("already detached"); 2128 2129 SLIST_REMOVE(&sc->devlist, devlist, firewire_dev_list, 2130 link); 2131 free(devlist, M_DEVBUF); 2132 2133 if (config_detach(fwdev->dev, DETACH_FORCE) != 0) 2134 return; 2135 2136 STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link); 2137 free(fwdev, M_FW); 2138 } 2139 } 2140 2141 return; 2142 } 2143 2144 /* 2145 * To allocate unique transaction label. 2146 */ 2147 static int 2148 fw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 2149 { 2150 u_int dst, new_tlabel; 2151 struct fw_xfer *txfer; 2152 2153 dst = xfer->send.hdr.mode.hdr.dst & 0x3f; 2154 mutex_enter(&fc->tlabel_lock); 2155 new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f; 2156 STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel) 2157 if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst) 2158 break; 2159 if (txfer == NULL) { 2160 fc->last_tlabel[dst] = new_tlabel; 2161 STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel); 2162 mutex_exit(&fc->tlabel_lock); 2163 xfer->tl = new_tlabel; 2164 xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2; 2165 if (firewire_debug > 1) 2166 printf("fw_get_tlabel: dst=%d tl=%d\n", 2167 dst, new_tlabel); 2168 return new_tlabel; 2169 } 2170 mutex_exit(&fc->tlabel_lock); 2171 2172 if (firewire_debug > 1) 2173 printf("fw_get_tlabel: no free tlabel\n"); 2174 return -1; 2175 } 2176 2177 static void 2178 fw_rcv_copy(struct fw_rcv_buf *rb) 2179 { 2180 struct fw_pkt *pkt; 2181 u_char *p; 2182 const struct tcode_info *tinfo; 2183 u_int res, i, len, plen; 2184 2185 rb->xfer->recv.spd = rb->spd; 2186 2187 pkt = (struct fw_pkt *)rb->vec->iov_base; 2188 tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 2189 2190 /* Copy header */ 2191 p = (u_char *)&rb->xfer->recv.hdr; 2192 memcpy(p, rb->vec->iov_base, tinfo->hdr_len); 2193 rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len; 2194 rb->vec->iov_len -= tinfo->hdr_len; 2195 2196 /* Copy payload */ 2197 p = (u_char *)rb->xfer->recv.payload; 2198 res = rb->xfer->recv.pay_len; 2199 2200 /* special handling for RRESQ */ 2201 if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 2202 p != NULL && res >= sizeof(uint32_t)) { 2203 *(uint32_t *)p = pkt->mode.rresq.data; 2204 rb->xfer->recv.pay_len = sizeof(uint32_t); 2205 return; 2206 } 2207 2208 if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 2209 return; 2210 2211 plen = pkt->mode.rresb.len; 2212 2213 for (i = 0; i < rb->nvec; i++, rb->vec++) { 2214 len = MIN(rb->vec->iov_len, plen); 2215 if (res < len) { 2216 aprint_error_dev(rb->fc->bdev, 2217 "rcv buffer(%d) is %d bytes short.\n", 2218 rb->xfer->recv.pay_len, len - res); 2219 len = res; 2220 } 2221 if (p) { 2222 memcpy(p, rb->vec->iov_base, len); 2223 p += len; 2224 } 2225 res -= len; 2226 plen -= len; 2227 if (res == 0 || plen == 0) 2228 break; 2229 } 2230 rb->xfer->recv.pay_len -= res; 2231 2232 } 2233 2234 /* 2235 * Post process for Bus Manager election process. 2236 */ 2237 static void 2238 fw_try_bmr_callback(struct fw_xfer *xfer) 2239 { 2240 struct firewire_comm *fc; 2241 int bmr; 2242 2243 if (xfer == NULL) 2244 return; 2245 fc = xfer->fc; 2246 if (xfer->resp != 0) 2247 goto error; 2248 if (xfer->recv.payload == NULL) 2249 goto error; 2250 if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 2251 goto error; 2252 2253 bmr = ntohl(xfer->recv.payload[0]); 2254 if (bmr == 0x3f) 2255 bmr = fc->nodeid; 2256 2257 CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 2258 fw_xfer_free_buf(xfer); 2259 fw_bmr(fc); 2260 return; 2261 2262 error: 2263 aprint_error_dev(fc->bdev, "bus manager election failed\n"); 2264 fw_xfer_free_buf(xfer); 2265 } 2266 2267 2268 /* 2269 * To candidate Bus Manager election process. 2270 */ 2271 static void 2272 fw_try_bmr(void *arg) 2273 { 2274 struct fw_xfer *xfer; 2275 struct firewire_comm *fc = (struct firewire_comm *)arg; 2276 struct fw_pkt *fp; 2277 int err = 0; 2278 2279 xfer = fw_xfer_alloc_buf(M_FW, 8, 4); 2280 if (xfer == NULL) 2281 return; 2282 xfer->send.spd = 0; 2283 fc->status = FWBUSMGRELECT; 2284 2285 fp = &xfer->send.hdr; 2286 fp->mode.lreq.dest_hi = 0xffff; 2287 fp->mode.lreq.tlrt = 0; 2288 fp->mode.lreq.tcode = FWTCODE_LREQ; 2289 fp->mode.lreq.pri = 0; 2290 fp->mode.lreq.src = 0; 2291 fp->mode.lreq.len = 8; 2292 fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 2293 fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 2294 fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2295 xfer->send.payload[0] = htonl(0x3f); 2296 xfer->send.payload[1] = htonl(fc->nodeid); 2297 xfer->hand = fw_try_bmr_callback; 2298 2299 err = fw_asyreq(fc, -1, xfer); 2300 if (err) { 2301 fw_xfer_free_buf(xfer); 2302 return; 2303 } 2304 return; 2305 } 2306 2307 /* 2308 * Find the root node, if it is not 2309 * Cycle Master Capable, then we should 2310 * override this and become the Cycle 2311 * Master 2312 */ 2313 static int 2314 fw_bmr(struct firewire_comm *fc) 2315 { 2316 struct fw_device fwdev; 2317 union fw_self_id *self_id; 2318 int cmstr; 2319 uint32_t quad; 2320 2321 /* Check to see if the current root node is cycle master capable */ 2322 self_id = fw_find_self_id(fc, fc->max_node); 2323 if (fc->max_node > 0) { 2324 /* XXX check cmc bit of businfo block rather than contender */ 2325 if (self_id->p0.link_active && self_id->p0.contender) 2326 cmstr = fc->max_node; 2327 else { 2328 aprint_normal_dev(fc->bdev, 2329 "root node is not cycle master capable\n"); 2330 /* XXX shall we be the cycle master? */ 2331 cmstr = fc->nodeid; 2332 /* XXX need bus reset */ 2333 } 2334 } else 2335 cmstr = -1; 2336 2337 aprint_normal_dev(fc->bdev, "bus manager %d%s\n", 2338 CSRARC(fc, BUS_MGR_ID), 2339 (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) ? " (me)" : ""); 2340 if (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) 2341 /* We are not the bus manager */ 2342 return 0; 2343 2344 /* Optimize gapcount */ 2345 if (fc->max_hop <= MAX_GAPHOP) 2346 fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2347 /* If we are the cycle master, nothing to do */ 2348 if (cmstr == fc->nodeid || cmstr == -1) 2349 return 0; 2350 /* Bus probe has not finished, make dummy fwdev for cmstr */ 2351 memset(&fwdev, 0, sizeof(fwdev)); 2352 fwdev.fc = fc; 2353 fwdev.dst = cmstr; 2354 fwdev.speed = 0; 2355 fwdev.maxrec = 8; /* 512 */ 2356 fwdev.status = FWDEVINIT; 2357 /* Set cmstr bit on the cycle master */ 2358 quad = htonl(1 << 8); 2359 fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 0xffff, 0xf0000000 | STATE_SET, 2360 &quad, fw_asy_callback_free); 2361 2362 return 0; 2363 } 2364