firewire.c revision 167632
1139749Simp/*- 2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa 3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 4103285Sikob * All rights reserved. 5103285Sikob * 6103285Sikob * Redistribution and use in source and binary forms, with or without 7103285Sikob * modification, are permitted provided that the following conditions 8103285Sikob * are met: 9103285Sikob * 1. Redistributions of source code must retain the above copyright 10103285Sikob * notice, this list of conditions and the following disclaimer. 11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 12103285Sikob * notice, this list of conditions and the following disclaimer in the 13103285Sikob * documentation and/or other materials provided with the distribution. 14103285Sikob * 3. All advertising materials mentioning features or use of this software 15103285Sikob * must display the acknowledgement as bellow: 16103285Sikob * 17103285Sikob * This product includes software developed by K. Kobayashi and H. Shimokawa 18103285Sikob * 19103285Sikob * 4. The name of the author may not be used to endorse or promote products 20103285Sikob * derived from this software without specific prior written permission. 21103285Sikob * 22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25103285Sikob * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32103285Sikob * POSSIBILITY OF SUCH DAMAGE. 33103285Sikob * 34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 167632 2007-03-16 05:39:33Z simokawa $ 35103285Sikob * 36103285Sikob */ 37103285Sikob 38103285Sikob#include <sys/param.h> 39103285Sikob#include <sys/systm.h> 40103285Sikob#include <sys/types.h> 41103285Sikob 42103285Sikob#include <sys/kernel.h> 43129879Sphk#include <sys/module.h> 44103285Sikob#include <sys/malloc.h> 45103285Sikob#include <sys/conf.h> 46103285Sikob#include <sys/sysctl.h> 47103285Sikob 48127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 49117067Ssimokawa#include <machine/clock.h> /* for DELAY() */ 50117067Ssimokawa#endif 51103285Sikob 52103285Sikob#include <sys/bus.h> /* used by smbus and newbus */ 53113584Ssimokawa#include <machine/bus.h> 54103285Sikob 55127468Ssimokawa#ifdef __DragonFly__ 56127468Ssimokawa#include "firewire.h" 57127468Ssimokawa#include "firewirereg.h" 58127468Ssimokawa#include "fwmem.h" 59127468Ssimokawa#include "iec13213.h" 60127468Ssimokawa#include "iec68113.h" 61127468Ssimokawa#else 62103285Sikob#include <dev/firewire/firewire.h> 63103285Sikob#include <dev/firewire/firewirereg.h> 64110072Ssimokawa#include <dev/firewire/fwmem.h> 65103285Sikob#include <dev/firewire/iec13213.h> 66103285Sikob#include <dev/firewire/iec68113.h> 67127468Ssimokawa#endif 68103285Sikob 69116376Ssimokawastruct crom_src_buf { 70116376Ssimokawa struct crom_src src; 71116376Ssimokawa struct crom_chunk root; 72116376Ssimokawa struct crom_chunk vendor; 73116376Ssimokawa struct crom_chunk hw; 74116376Ssimokawa}; 75116376Ssimokawa 76120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3; 77103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 78108281Ssimokawa "FireWire driver debug flag"); 79109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 80109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 81109736Ssimokawa "Try to be a bus manager"); 82120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0, 83120850Ssimokawa "Number of count of bus resets for removing lost device information"); 84103285Sikob 85110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 86110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 87110195Ssimokawa 88103285Sikob#define FW_MAXASYRTY 4 89103285Sikob 90103285Sikobdevclass_t firewire_devclass; 91103285Sikob 92125238Ssimokawastatic void firewire_identify (driver_t *, device_t); 93125238Ssimokawastatic int firewire_probe (device_t); 94124169Ssimokawastatic int firewire_attach (device_t); 95124169Ssimokawastatic int firewire_detach (device_t); 96124169Ssimokawastatic int firewire_resume (device_t); 97103285Sikob#if 0 98124169Ssimokawastatic int firewire_shutdown (device_t); 99103285Sikob#endif 100124169Ssimokawastatic device_t firewire_add_child (device_t, int, const char *, int); 101124169Ssimokawastatic void fw_try_bmr (void *); 102124169Ssimokawastatic void fw_try_bmr_callback (struct fw_xfer *); 103124169Ssimokawastatic void fw_asystart (struct fw_xfer *); 104124169Ssimokawastatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *); 105124169Ssimokawastatic void fw_bus_probe (struct firewire_comm *); 106124169Ssimokawastatic void fw_bus_explore (struct firewire_comm *); 107124169Ssimokawastatic void fw_bus_explore_callback (struct fw_xfer *); 108124169Ssimokawastatic void fw_attach_dev (struct firewire_comm *); 109106543Ssimokawa#ifdef FW_VMACCESS 110124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *); 111106543Ssimokawa#endif 112129585Sdfrstruct fw_xfer *asyreqq (struct firewire_comm *, uint8_t, uint8_t, uint8_t, 113129585Sdfr uint32_t, uint32_t, void (*)(struct fw_xfer *)); 114124169Ssimokawastatic int fw_bmr (struct firewire_comm *); 115103285Sikob 116103285Sikobstatic device_method_t firewire_methods[] = { 117103285Sikob /* Device interface */ 118125238Ssimokawa DEVMETHOD(device_identify, firewire_identify), 119125238Ssimokawa DEVMETHOD(device_probe, firewire_probe), 120103285Sikob DEVMETHOD(device_attach, firewire_attach), 121103285Sikob DEVMETHOD(device_detach, firewire_detach), 122108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 123116978Ssimokawa DEVMETHOD(device_resume, firewire_resume), 124103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 125103285Sikob 126103285Sikob /* Bus interface */ 127103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 128103285Sikob DEVMETHOD(bus_print_child, bus_generic_print_child), 129103285Sikob 130103285Sikob { 0, 0 } 131103285Sikob}; 132124251Ssimokawachar *linkspeed[] = { 133124251Ssimokawa "S100", "S200", "S400", "S800", 134124251Ssimokawa "S1600", "S3200", "undef", "undef" 135124251Ssimokawa}; 136103285Sikob 137124251Ssimokawastatic char *tcode_str[] = { 138124251Ssimokawa "WREQQ", "WREQB", "WRES", "undef", 139124251Ssimokawa "RREQQ", "RREQB", "RRESQ", "RRESB", 140124251Ssimokawa "CYCS", "LREQ", "STREAM", "LRES", 141124251Ssimokawa "undef", "undef", "PHY", "undef" 142124251Ssimokawa}; 143124251Ssimokawa 144114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 145114909Ssimokawa#define MAX_GAPHOP 15 146114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 147114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 148106813Ssimokawa 149103285Sikobstatic driver_t firewire_driver = { 150103285Sikob "firewire", 151103285Sikob firewire_methods, 152103285Sikob sizeof(struct firewire_softc), 153103285Sikob}; 154103285Sikob 155103285Sikob/* 156110072Ssimokawa * Lookup fwdev by node id. 157103285Sikob */ 158106810Ssimokawastruct fw_device * 159110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 160103285Sikob{ 161103285Sikob struct fw_device *fwdev; 162110072Ssimokawa int s; 163110072Ssimokawa 164110072Ssimokawa s = splfw(); 165110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 166120660Ssimokawa if (fwdev->dst == dst && fwdev->status != FWDEVINVAL) 167103285Sikob break; 168110072Ssimokawa splx(s); 169110072Ssimokawa 170106810Ssimokawa return fwdev; 171103285Sikob} 172106813Ssimokawa 173103285Sikob/* 174110072Ssimokawa * Lookup fwdev by EUI64. 175110072Ssimokawa */ 176110072Ssimokawastruct fw_device * 177110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 178110072Ssimokawa{ 179110072Ssimokawa struct fw_device *fwdev; 180110072Ssimokawa int s; 181110072Ssimokawa 182110072Ssimokawa s = splfw(); 183110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 184110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 185110072Ssimokawa break; 186110072Ssimokawa splx(s); 187110072Ssimokawa 188110072Ssimokawa if(fwdev == NULL) return NULL; 189110072Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 190110072Ssimokawa return fwdev; 191110072Ssimokawa} 192110072Ssimokawa 193110072Ssimokawa/* 194103285Sikob * Async. request procedure for userland application. 195103285Sikob */ 196103285Sikobint 197103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 198103285Sikob{ 199103285Sikob int err = 0; 200103285Sikob struct fw_xferq *xferq; 201103285Sikob int tl = 0, len; 202103285Sikob struct fw_pkt *fp; 203103285Sikob int tcode; 204103285Sikob struct tcode_info *info; 205103285Sikob 206103285Sikob if(xfer == NULL) return EINVAL; 207167632Ssimokawa if(xfer->hand == NULL){ 208167632Ssimokawa printf("hand == NULL\n"); 209103285Sikob return EINVAL; 210103285Sikob } 211120660Ssimokawa fp = &xfer->send.hdr; 212103285Sikob 213103285Sikob tcode = fp->mode.common.tcode & 0xf; 214103285Sikob info = &fc->tcode[tcode]; 215103285Sikob if (info->flag == 0) { 216124251Ssimokawa printf("invalid tcode=%x\n", tcode); 217103285Sikob return EINVAL; 218103285Sikob } 219103285Sikob if (info->flag & FWTI_REQ) 220103285Sikob xferq = fc->atq; 221103285Sikob else 222103285Sikob xferq = fc->ats; 223103285Sikob len = info->hdr_len; 224120660Ssimokawa if (xfer->send.pay_len > MAXREC(fc->maxrec)) { 225120660Ssimokawa printf("send.pay_len > maxrec\n"); 226120660Ssimokawa return EINVAL; 227120660Ssimokawa } 228103285Sikob if (info->flag & FWTI_BLOCK_STR) 229120660Ssimokawa len = fp->mode.stream.len; 230103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 231120660Ssimokawa len = fp->mode.rresb.len; 232120660Ssimokawa else 233120660Ssimokawa len = 0; 234120660Ssimokawa if (len != xfer->send.pay_len){ 235124251Ssimokawa printf("len(%d) != send.pay_len(%d) %s(%x)\n", 236124251Ssimokawa len, xfer->send.pay_len, tcode_str[tcode], tcode); 237103285Sikob return EINVAL; 238103285Sikob } 239106790Ssimokawa 240103285Sikob if(xferq->start == NULL){ 241103285Sikob printf("xferq->start == NULL\n"); 242103285Sikob return EINVAL; 243103285Sikob } 244103285Sikob if(!(xferq->queued < xferq->maxq)){ 245108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 246108655Ssimokawa xferq->queued); 247103285Sikob return EINVAL; 248103285Sikob } 249103285Sikob 250103285Sikob if (info->flag & FWTI_TLABEL) { 251130460Sdfr if ((tl = fw_get_tlabel(fc, xfer)) == -1) 252130460Sdfr return EAGAIN; 253103285Sikob fp->mode.hdr.tlrt = tl << 2; 254103285Sikob } 255103285Sikob 256103285Sikob xfer->tl = tl; 257103285Sikob xfer->resp = 0; 258103285Sikob xfer->fc = fc; 259103285Sikob xfer->q = xferq; 260103285Sikob 261103285Sikob fw_asystart(xfer); 262103285Sikob return err; 263103285Sikob} 264103285Sikob/* 265103285Sikob * Wakeup blocked process. 266103285Sikob */ 267103285Sikobvoid 268103285Sikobfw_asy_callback(struct fw_xfer *xfer){ 269103285Sikob wakeup(xfer); 270103285Sikob return; 271103285Sikob} 272103285Sikob/* 273103285Sikob * Postpone to later retry. 274103285Sikob */ 275103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){ 276103285Sikob printf("fw_asybusy\n"); 277103285Sikob/* 278103285Sikob xfer->ch = timeout((timeout_t *)fw_asystart, (void *)xfer, 20000); 279103285Sikob*/ 280124145Ssimokawa#if 0 281103285Sikob DELAY(20000); 282124145Ssimokawa#endif 283103285Sikob fw_asystart(xfer); 284103285Sikob return; 285103285Sikob} 286103285Sikob 287103285Sikob/* 288103285Sikob * Async. request with given xfer structure. 289103285Sikob */ 290106790Ssimokawastatic void 291106790Ssimokawafw_asystart(struct fw_xfer *xfer) 292106790Ssimokawa{ 293103285Sikob struct firewire_comm *fc = xfer->fc; 294103285Sikob int s; 295103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */ 296103285Sikob if (fc->status < FWBUSEXPLORE) { 297103285Sikob xfer->resp = EAGAIN; 298103285Sikob xfer->state = FWXF_BUSY; 299167632Ssimokawa if (xfer->hand != NULL) 300167632Ssimokawa xfer->hand(xfer); 301103285Sikob return; 302103285Sikob } 303103285Sikob#endif 304127468Ssimokawa microtime(&xfer->tv); 305103285Sikob s = splfw(); 306103285Sikob xfer->state = FWXF_INQ; 307103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 308103285Sikob xfer->q->queued ++; 309103285Sikob splx(s); 310103285Sikob /* XXX just queue for mbuf */ 311103285Sikob if (xfer->mbuf == NULL) 312103285Sikob xfer->q->start(fc); 313103285Sikob return; 314103285Sikob} 315106790Ssimokawa 316125238Ssimokawastatic void 317125238Ssimokawafirewire_identify(driver_t *driver, device_t parent) 318125238Ssimokawa{ 319125238Ssimokawa BUS_ADD_CHILD(parent, 0, "firewire", -1); 320125238Ssimokawa} 321125238Ssimokawa 322103285Sikobstatic int 323125238Ssimokawafirewire_probe(device_t dev) 324103285Sikob{ 325108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 326125238Ssimokawa return (0); 327103285Sikob} 328106790Ssimokawa 329110577Ssimokawastatic void 330110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc) 331110577Ssimokawa{ 332110577Ssimokawa struct fw_xfer *xfer; 333110577Ssimokawa struct tlabel *tl; 334110577Ssimokawa struct timeval tv; 335110577Ssimokawa struct timeval split_timeout; 336111040Ssimokawa int i, s; 337110577Ssimokawa 338120660Ssimokawa split_timeout.tv_sec = 0; 339120660Ssimokawa split_timeout.tv_usec = 200 * 1000; /* 200 msec */ 340110577Ssimokawa 341110577Ssimokawa microtime(&tv); 342110577Ssimokawa timevalsub(&tv, &split_timeout); 343110577Ssimokawa 344111040Ssimokawa s = splfw(); 345110577Ssimokawa for (i = 0; i < 0x40; i ++) { 346110577Ssimokawa while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 347110577Ssimokawa xfer = tl->xfer; 348110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 349110577Ssimokawa /* the rests are newer than this */ 350110577Ssimokawa break; 351124145Ssimokawa if (xfer->state == FWXF_START) 352124145Ssimokawa /* not sent yet */ 353124145Ssimokawa break; 354110577Ssimokawa device_printf(fc->bdev, 355117716Ssimokawa "split transaction timeout dst=0x%x tl=0x%x state=%d\n", 356120660Ssimokawa xfer->send.hdr.mode.hdr.dst, i, xfer->state); 357110577Ssimokawa xfer->resp = ETIMEDOUT; 358110577Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], link); 359113584Ssimokawa fw_xfer_done(xfer); 360110577Ssimokawa } 361110577Ssimokawa } 362111040Ssimokawa splx(s); 363110577Ssimokawa} 364110577Ssimokawa 365121463Ssimokawa#define WATCHDOC_HZ 10 366110577Ssimokawastatic void 367110577Ssimokawafirewire_watchdog(void *arg) 368110577Ssimokawa{ 369110577Ssimokawa struct firewire_comm *fc; 370121463Ssimokawa static int watchdoc_clock = 0; 371110577Ssimokawa 372110577Ssimokawa fc = (struct firewire_comm *)arg; 373121463Ssimokawa 374121463Ssimokawa /* 375121463Ssimokawa * At boot stage, the device interrupt is disabled and 376121463Ssimokawa * We encounter a timeout easily. To avoid this, 377121463Ssimokawa * ignore clock interrupt for a while. 378121463Ssimokawa */ 379121463Ssimokawa if (watchdoc_clock > WATCHDOC_HZ * 15) { 380121463Ssimokawa firewire_xfer_timeout(fc); 381121463Ssimokawa fc->timeout(fc); 382121463Ssimokawa } else 383121463Ssimokawa watchdoc_clock ++; 384121463Ssimokawa 385121463Ssimokawa callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ, 386110577Ssimokawa (void *)firewire_watchdog, (void *)fc); 387110577Ssimokawa} 388110577Ssimokawa 389103285Sikob/* 390103285Sikob * The attach routine. 391103285Sikob */ 392103285Sikobstatic int 393118455Ssimokawafirewire_attach(device_t dev) 394103285Sikob{ 395118455Ssimokawa int unit; 396103285Sikob struct firewire_softc *sc = device_get_softc(dev); 397103285Sikob device_t pa = device_get_parent(dev); 398103285Sikob struct firewire_comm *fc; 399103285Sikob 400103285Sikob fc = (struct firewire_comm *)device_get_softc(pa); 401103285Sikob sc->fc = fc; 402116978Ssimokawa fc->status = FWBUSNOTREADY; 403103285Sikob 404118455Ssimokawa unit = device_get_unit(dev); 405118455Ssimokawa if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 406103285Sikob 407118455Ssimokawa fwdev_makedev(sc); 408118455Ssimokawa 409110193Ssimokawa CALLOUT_INIT(&sc->fc->timeout_callout); 410110193Ssimokawa CALLOUT_INIT(&sc->fc->bmr_callout); 411110193Ssimokawa CALLOUT_INIT(&sc->fc->busprobe_callout); 412108853Ssimokawa 413110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 414110577Ssimokawa (void *)firewire_watchdog, (void *)sc->fc); 415110193Ssimokawa 416103285Sikob /* Locate our children */ 417103285Sikob bus_generic_probe(dev); 418103285Sikob 419103285Sikob /* launch attachement of the added children */ 420103285Sikob bus_generic_attach(dev); 421103285Sikob 422103285Sikob /* bus_reset */ 423125238Ssimokawa fw_busreset(fc); 424103285Sikob fc->ibr(fc); 425103285Sikob 426103285Sikob return 0; 427103285Sikob} 428103285Sikob 429103285Sikob/* 430103285Sikob * Attach it as child. 431103285Sikob */ 432103285Sikobstatic device_t 433103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit) 434103285Sikob{ 435103285Sikob device_t child; 436103285Sikob struct firewire_softc *sc; 437103285Sikob 438103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 439103285Sikob child = device_add_child(dev, name, unit); 440103285Sikob if (child) { 441103285Sikob device_set_ivars(child, sc->fc); 442103285Sikob device_probe_and_attach(child); 443103285Sikob } 444103285Sikob 445103285Sikob return child; 446103285Sikob} 447106790Ssimokawa 448116978Ssimokawastatic int 449116978Ssimokawafirewire_resume(device_t dev) 450116978Ssimokawa{ 451116978Ssimokawa struct firewire_softc *sc; 452116978Ssimokawa 453116978Ssimokawa sc = (struct firewire_softc *)device_get_softc(dev); 454116978Ssimokawa sc->fc->status = FWBUSNOTREADY; 455116978Ssimokawa 456116978Ssimokawa bus_generic_resume(dev); 457116978Ssimokawa 458116978Ssimokawa return(0); 459116978Ssimokawa} 460116978Ssimokawa 461103285Sikob/* 462103285Sikob * Dettach it. 463103285Sikob */ 464103285Sikobstatic int 465118455Ssimokawafirewire_detach(device_t dev) 466103285Sikob{ 467103285Sikob struct firewire_softc *sc; 468111074Ssimokawa struct csrdir *csrd, *next; 469111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 470118455Ssimokawa int err; 471103285Sikob 472103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 473118455Ssimokawa if ((err = fwdev_destroydev(sc)) != 0) 474118455Ssimokawa return err; 475106790Ssimokawa 476118455Ssimokawa if ((err = bus_generic_detach(dev)) != 0) 477118455Ssimokawa return err; 478111078Ssimokawa 479111078Ssimokawa callout_stop(&sc->fc->timeout_callout); 480111078Ssimokawa callout_stop(&sc->fc->bmr_callout); 481111078Ssimokawa callout_stop(&sc->fc->busprobe_callout); 482111078Ssimokawa 483103285Sikob /* XXX xfree_free and untimeout on all xfers */ 484111078Ssimokawa for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL; 485111078Ssimokawa fwdev = fwdev_next) { 486111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 487111078Ssimokawa free(fwdev, M_FW); 488111078Ssimokawa } 489111074Ssimokawa for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) { 490111074Ssimokawa next = SLIST_NEXT(csrd, link); 491111074Ssimokawa free(csrd, M_FW); 492111074Ssimokawa } 493110195Ssimokawa free(sc->fc->topology_map, M_FW); 494110195Ssimokawa free(sc->fc->speed_map, M_FW); 495116376Ssimokawa free(sc->fc->crom_src_buf, M_FW); 496103285Sikob return(0); 497103285Sikob} 498103285Sikob#if 0 499103285Sikobstatic int 500103285Sikobfirewire_shutdown( device_t dev ) 501103285Sikob{ 502103285Sikob return 0; 503103285Sikob} 504103285Sikob#endif 505106790Ssimokawa 506110577Ssimokawa 507110577Ssimokawastatic void 508110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 509110577Ssimokawa{ 510110577Ssimokawa struct fw_xfer *xfer; 511110577Ssimokawa 512110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 513110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 514111942Ssimokawa xferq->queued --; 515110577Ssimokawa xfer->resp = EAGAIN; 516113584Ssimokawa fw_xfer_done(xfer); 517110577Ssimokawa } 518110577Ssimokawa} 519110577Ssimokawa 520110798Ssimokawavoid 521110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 522110798Ssimokawa{ 523110798Ssimokawa int i; 524110798Ssimokawa 525110798Ssimokawa fw_xferq_drain(fc->atq); 526110798Ssimokawa fw_xferq_drain(fc->ats); 527110798Ssimokawa for(i = 0; i < fc->nisodma; i++) 528110798Ssimokawa fw_xferq_drain(fc->it[i]); 529110798Ssimokawa} 530110798Ssimokawa 531116376Ssimokawastatic void 532116376Ssimokawafw_reset_csr(struct firewire_comm *fc) 533103285Sikob{ 534103285Sikob int i; 535103285Sikob 536103285Sikob CSRARC(fc, STATE_CLEAR) 537103285Sikob = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 538103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 539103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 540103285Sikob 541103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 542103285Sikob fc->irm = -1; 543103285Sikob 544103285Sikob fc->max_node = -1; 545103285Sikob 546103285Sikob for(i = 2; i < 0x100/4 - 2 ; i++){ 547103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 548103285Sikob } 549103285Sikob CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 550103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 551103285Sikob CSRARC(fc, RESET_START) = 0; 552103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 553103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 554103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 555103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 556103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 557103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 558103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 559103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 560103285Sikob CSRARC(fc, IP_CHANNELS) = (1 << 31); 561103285Sikob 562103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 563103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 564103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 565103285Sikob 1 << 28 | 0xff << 16 | 0x09 << 8; 566103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 567103285Sikob 568103285Sikob/* DV depend CSRs see blue book */ 569103285Sikob CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 570103285Sikob CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 571103285Sikob 572103285Sikob CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 ); 573103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 574116376Ssimokawa} 575113584Ssimokawa 576116376Ssimokawastatic void 577116376Ssimokawafw_init_crom(struct firewire_comm *fc) 578116376Ssimokawa{ 579116376Ssimokawa struct crom_src *src; 580116376Ssimokawa 581116376Ssimokawa fc->crom_src_buf = (struct crom_src_buf *) 582116376Ssimokawa malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO); 583116376Ssimokawa if (fc->crom_src_buf == NULL) 584116376Ssimokawa return; 585116376Ssimokawa 586116376Ssimokawa src = &fc->crom_src_buf->src; 587116376Ssimokawa bzero(src, sizeof(struct crom_src)); 588116376Ssimokawa 589116376Ssimokawa /* BUS info sample */ 590116376Ssimokawa src->hdr.info_len = 4; 591116376Ssimokawa 592116376Ssimokawa src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 593116376Ssimokawa 594116376Ssimokawa src->businfo.irmc = 1; 595116376Ssimokawa src->businfo.cmc = 1; 596116376Ssimokawa src->businfo.isc = 1; 597116376Ssimokawa src->businfo.bmc = 1; 598116376Ssimokawa src->businfo.pmc = 0; 599116376Ssimokawa src->businfo.cyc_clk_acc = 100; 600116376Ssimokawa src->businfo.max_rec = fc->maxrec; 601116376Ssimokawa src->businfo.max_rom = MAXROM_4; 602117350Ssimokawa src->businfo.generation = 1; 603116376Ssimokawa src->businfo.link_spd = fc->speed; 604116376Ssimokawa 605116376Ssimokawa src->businfo.eui64.hi = fc->eui.hi; 606116376Ssimokawa src->businfo.eui64.lo = fc->eui.lo; 607116376Ssimokawa 608116376Ssimokawa STAILQ_INIT(&src->chunk_list); 609116376Ssimokawa 610116376Ssimokawa fc->crom_src = src; 611116376Ssimokawa fc->crom_root = &fc->crom_src_buf->root; 612116376Ssimokawa} 613116376Ssimokawa 614116376Ssimokawastatic void 615116376Ssimokawafw_reset_crom(struct firewire_comm *fc) 616116376Ssimokawa{ 617116376Ssimokawa struct crom_src_buf *buf; 618116376Ssimokawa struct crom_src *src; 619116376Ssimokawa struct crom_chunk *root; 620116376Ssimokawa 621116376Ssimokawa if (fc->crom_src_buf == NULL) 622116376Ssimokawa fw_init_crom(fc); 623116376Ssimokawa 624116376Ssimokawa buf = fc->crom_src_buf; 625116376Ssimokawa src = fc->crom_src; 626116376Ssimokawa root = fc->crom_root; 627116376Ssimokawa 628116376Ssimokawa STAILQ_INIT(&src->chunk_list); 629116376Ssimokawa 630116376Ssimokawa bzero(root, sizeof(struct crom_chunk)); 631116376Ssimokawa crom_add_chunk(src, NULL, root, 0); 632116376Ssimokawa crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 633116376Ssimokawa /* private company_id */ 634116376Ssimokawa crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 635127468Ssimokawa#ifdef __DragonFly__ 636127468Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project"); 637127468Ssimokawa crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version); 638127468Ssimokawa#else 639116376Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project"); 640116376Ssimokawa crom_add_entry(root, CSRKEY_HW, __FreeBSD_version); 641127468Ssimokawa#endif 642116376Ssimokawa crom_add_simple_text(src, root, &buf->hw, hostname); 643116376Ssimokawa} 644116376Ssimokawa 645116376Ssimokawa/* 646116376Ssimokawa * Called after bus reset. 647116376Ssimokawa */ 648116376Ssimokawavoid 649116376Ssimokawafw_busreset(struct firewire_comm *fc) 650116376Ssimokawa{ 651116376Ssimokawa struct firewire_dev_comm *fdc; 652117350Ssimokawa struct crom_src *src; 653116376Ssimokawa device_t *devlistp; 654117350Ssimokawa void *newrom; 655116376Ssimokawa int i, devcnt; 656116376Ssimokawa 657116376Ssimokawa switch(fc->status){ 658116376Ssimokawa case FWBUSMGRELECT: 659116376Ssimokawa callout_stop(&fc->bmr_callout); 660116376Ssimokawa break; 661116376Ssimokawa default: 662116376Ssimokawa break; 663116376Ssimokawa } 664116376Ssimokawa fc->status = FWBUSRESET; 665116376Ssimokawa fw_reset_csr(fc); 666116376Ssimokawa fw_reset_crom(fc); 667116376Ssimokawa 668113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 669113584Ssimokawa for( i = 0 ; i < devcnt ; i++) 670113584Ssimokawa if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 671113584Ssimokawa fdc = device_get_softc(devlistp[i]); 672113584Ssimokawa if (fdc->post_busreset != NULL) 673113584Ssimokawa fdc->post_busreset(fdc); 674113584Ssimokawa } 675113584Ssimokawa free(devlistp, M_TEMP); 676113584Ssimokawa } 677116376Ssimokawa 678117350Ssimokawa newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO); 679117350Ssimokawa src = &fc->crom_src_buf->src; 680129585Sdfr crom_load(src, (uint32_t *)newrom, CROMSIZE); 681117350Ssimokawa if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) { 682117350Ssimokawa /* bump generation and reload */ 683117350Ssimokawa src->businfo.generation ++; 684117350Ssimokawa /* generation must be between 0x2 and 0xF */ 685117350Ssimokawa if (src->businfo.generation < 2) 686117350Ssimokawa src->businfo.generation ++; 687129585Sdfr crom_load(src, (uint32_t *)newrom, CROMSIZE); 688117350Ssimokawa bcopy(newrom, (void *)fc->config_rom, CROMSIZE); 689117350Ssimokawa } 690117350Ssimokawa free(newrom, M_FW); 691103285Sikob} 692106790Ssimokawa 693103285Sikob/* Call once after reboot */ 694106790Ssimokawavoid fw_init(struct firewire_comm *fc) 695103285Sikob{ 696103285Sikob int i; 697106543Ssimokawa struct csrdir *csrd; 698106543Ssimokawa#ifdef FW_VMACCESS 699103285Sikob struct fw_xfer *xfer; 700103285Sikob struct fw_bind *fwb; 701106543Ssimokawa#endif 702103285Sikob 703103285Sikob fc->arq->queued = 0; 704103285Sikob fc->ars->queued = 0; 705103285Sikob fc->atq->queued = 0; 706103285Sikob fc->ats->queued = 0; 707103285Sikob 708103285Sikob fc->arq->buf = NULL; 709103285Sikob fc->ars->buf = NULL; 710103285Sikob fc->atq->buf = NULL; 711103285Sikob fc->ats->buf = NULL; 712103285Sikob 713113584Ssimokawa fc->arq->flag = 0; 714113584Ssimokawa fc->ars->flag = 0; 715113584Ssimokawa fc->atq->flag = 0; 716113584Ssimokawa fc->ats->flag = 0; 717103285Sikob 718103285Sikob STAILQ_INIT(&fc->atq->q); 719103285Sikob STAILQ_INIT(&fc->ats->q); 720103285Sikob 721103285Sikob for( i = 0 ; i < fc->nisodma ; i ++ ){ 722103285Sikob fc->it[i]->queued = 0; 723103285Sikob fc->ir[i]->queued = 0; 724103285Sikob 725103285Sikob fc->it[i]->start = NULL; 726103285Sikob fc->ir[i]->start = NULL; 727103285Sikob 728103285Sikob fc->it[i]->buf = NULL; 729103285Sikob fc->ir[i]->buf = NULL; 730103285Sikob 731103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 732103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 733103285Sikob 734103285Sikob STAILQ_INIT(&fc->it[i]->q); 735103285Sikob STAILQ_INIT(&fc->ir[i]->q); 736103285Sikob 737103285Sikob STAILQ_INIT(&fc->it[i]->binds); 738103285Sikob STAILQ_INIT(&fc->ir[i]->binds); 739103285Sikob } 740103285Sikob 741103285Sikob fc->arq->maxq = FWMAXQUEUE; 742103285Sikob fc->ars->maxq = FWMAXQUEUE; 743103285Sikob fc->atq->maxq = FWMAXQUEUE; 744103285Sikob fc->ats->maxq = FWMAXQUEUE; 745103285Sikob 746103285Sikob for( i = 0 ; i < fc->nisodma ; i++){ 747103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 748103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 749103285Sikob } 750103285Sikob/* Initialize csr registers */ 751103285Sikob fc->topology_map = (struct fw_topology_map *)malloc( 752103285Sikob sizeof(struct fw_topology_map), 753110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 754103285Sikob fc->speed_map = (struct fw_speed_map *)malloc( 755103285Sikob sizeof(struct fw_speed_map), 756110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 757103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 758103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 759103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 760103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 761103285Sikob 762110193Ssimokawa STAILQ_INIT(&fc->devices); 763103285Sikob 764103285Sikob/* Initialize csr ROM work space */ 765103285Sikob SLIST_INIT(&fc->ongocsr); 766103285Sikob SLIST_INIT(&fc->csrfree); 767103285Sikob for( i = 0 ; i < FWMAXCSRDIR ; i++){ 768110195Ssimokawa csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT); 769103285Sikob if(csrd == NULL) break; 770103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 771103285Sikob } 772103285Sikob 773103285Sikob/* Initialize Async handlers */ 774103285Sikob STAILQ_INIT(&fc->binds); 775103285Sikob for( i = 0 ; i < 0x40 ; i++){ 776103285Sikob STAILQ_INIT(&fc->tlabels[i]); 777103285Sikob } 778103285Sikob 779103285Sikob/* DV depend CSRs see blue book */ 780103285Sikob#if 0 781103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 782103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 783103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 784103285Sikob CSRARC(fc, i + oPCR) = 0x8000007a; 785103285Sikob } 786103285Sikob 787103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 788103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 789103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 790103285Sikob CSRARC(fc, i + iPCR) = 0x0; 791103285Sikob } 792103285Sikob#endif 793103285Sikob 794116376Ssimokawa fc->crom_src_buf = NULL; 795103285Sikob 796106543Ssimokawa#ifdef FW_VMACCESS 797103285Sikob xfer = fw_xfer_alloc(); 798103285Sikob if(xfer == NULL) return; 799103285Sikob 800110195Ssimokawa fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 801103285Sikob if(fwb == NULL){ 802103285Sikob fw_xfer_free(xfer); 803139680Sjmg return; 804103285Sikob } 805167632Ssimokawa xfer->hand = fw_vmaccess; 806103285Sikob xfer->fc = fc; 807103285Sikob xfer->sc = NULL; 808103285Sikob 809103285Sikob fwb->start_hi = 0x2; 810103285Sikob fwb->start_lo = 0; 811103285Sikob fwb->addrlen = 0xffffffff; 812103285Sikob fwb->xfer = xfer; 813103285Sikob fw_bindadd(fc, fwb); 814106543Ssimokawa#endif 815103285Sikob} 816106790Ssimokawa 817120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\ 818120660Ssimokawa ((fwb)->end < (addr))?1:0) 819120660Ssimokawa 820103285Sikob/* 821129541Sdfr * To lookup bound process from IEEE1394 address. 822103285Sikob */ 823106813Ssimokawastruct fw_bind * 824129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo) 825103285Sikob{ 826120660Ssimokawa u_int64_t addr; 827103285Sikob struct fw_bind *tfw; 828120660Ssimokawa 829120660Ssimokawa addr = ((u_int64_t)dest_hi << 32) | dest_lo; 830120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 831120660Ssimokawa if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0) 832103285Sikob return(tfw); 833103285Sikob return(NULL); 834103285Sikob} 835106790Ssimokawa 836103285Sikob/* 837103285Sikob * To bind IEEE1394 address block to process. 838103285Sikob */ 839106790Ssimokawaint 840106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 841103285Sikob{ 842120660Ssimokawa struct fw_bind *tfw, *prev = NULL; 843120660Ssimokawa 844120660Ssimokawa if (fwb->start > fwb->end) { 845127468Ssimokawa printf("%s: invalid range\n", __func__); 846120660Ssimokawa return EINVAL; 847120660Ssimokawa } 848120660Ssimokawa 849120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) { 850120660Ssimokawa if (fwb->end < tfw->start) 851120660Ssimokawa break; 852120660Ssimokawa prev = tfw; 853120660Ssimokawa } 854120660Ssimokawa if (prev == NULL) { 855103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 856103285Sikob goto out; 857103285Sikob } 858120660Ssimokawa if (prev->end < fwb->start) { 859120660Ssimokawa STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 860103285Sikob goto out; 861103285Sikob } 862120660Ssimokawa 863127468Ssimokawa printf("%s: bind failed\n", __func__); 864120660Ssimokawa return (EBUSY); 865120660Ssimokawa 866103285Sikobout: 867120660Ssimokawa if (fwb->act_type == FWACT_CH) 868113584Ssimokawa STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist); 869120660Ssimokawa return (0); 870103285Sikob} 871103285Sikob 872103285Sikob/* 873103285Sikob * To free IEEE1394 address block. 874103285Sikob */ 875106790Ssimokawaint 876106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 877103285Sikob{ 878120660Ssimokawa#if 0 879120660Ssimokawa struct fw_xfer *xfer, *next; 880120660Ssimokawa#endif 881120660Ssimokawa struct fw_bind *tfw; 882103285Sikob int s; 883103285Sikob 884103285Sikob s = splfw(); 885120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 886120660Ssimokawa if (tfw == fwb) { 887120660Ssimokawa STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 888120660Ssimokawa goto found; 889120660Ssimokawa } 890120660Ssimokawa 891129541Sdfr printf("%s: no such binding\n", __func__); 892120660Ssimokawa splx(s); 893120660Ssimokawa return (1); 894120660Ssimokawafound: 895120660Ssimokawa#if 0 896113584Ssimokawa /* shall we do this? */ 897113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 898113584Ssimokawa next = STAILQ_NEXT(xfer, link); 899113584Ssimokawa fw_xfer_free(xfer); 900113584Ssimokawa } 901113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 902120660Ssimokawa#endif 903113584Ssimokawa 904103285Sikob splx(s); 905103285Sikob return 0; 906103285Sikob} 907103285Sikob 908103285Sikob/* 909103285Sikob * To free transaction label. 910103285Sikob */ 911106790Ssimokawastatic void 912106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 913103285Sikob{ 914103285Sikob struct tlabel *tl; 915103285Sikob int s = splfw(); 916103285Sikob 917103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL; 918103285Sikob tl = STAILQ_NEXT(tl, link)){ 919103285Sikob if(tl->xfer == xfer){ 920103285Sikob STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link); 921110195Ssimokawa free(tl, M_FW); 922103285Sikob splx(s); 923103285Sikob return; 924103285Sikob } 925103285Sikob } 926103285Sikob splx(s); 927103285Sikob return; 928103285Sikob} 929106790Ssimokawa 930103285Sikob/* 931103285Sikob * To obtain XFER structure by transaction label. 932103285Sikob */ 933106790Ssimokawastatic struct fw_xfer * 934106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel) 935103285Sikob{ 936103285Sikob struct fw_xfer *xfer; 937103285Sikob struct tlabel *tl; 938103285Sikob int s = splfw(); 939103285Sikob 940103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL; 941103285Sikob tl = STAILQ_NEXT(tl, link)){ 942120660Ssimokawa if(tl->xfer->send.hdr.mode.hdr.dst == node){ 943103285Sikob xfer = tl->xfer; 944103285Sikob splx(s); 945110577Ssimokawa if (firewire_debug > 2) 946110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 947103285Sikob return(xfer); 948103285Sikob } 949103285Sikob } 950110577Ssimokawa if (firewire_debug > 1) 951110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 952103285Sikob splx(s); 953103285Sikob return(NULL); 954103285Sikob} 955106790Ssimokawa 956103285Sikob/* 957103285Sikob * To allocate IEEE1394 XFER structure. 958103285Sikob */ 959106790Ssimokawastruct fw_xfer * 960110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 961103285Sikob{ 962103285Sikob struct fw_xfer *xfer; 963106790Ssimokawa 964110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 965106790Ssimokawa if (xfer == NULL) 966106790Ssimokawa return xfer; 967106790Ssimokawa 968110269Ssimokawa xfer->malloc = type; 969106790Ssimokawa 970103285Sikob return xfer; 971103285Sikob} 972106790Ssimokawa 973113584Ssimokawastruct fw_xfer * 974113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 975113584Ssimokawa{ 976113584Ssimokawa struct fw_xfer *xfer; 977113584Ssimokawa 978113584Ssimokawa xfer = fw_xfer_alloc(type); 979126102Scperciva if (xfer == NULL) 980126102Scperciva return(NULL); 981120660Ssimokawa xfer->send.pay_len = send_len; 982120660Ssimokawa xfer->recv.pay_len = recv_len; 983120660Ssimokawa if (send_len > 0) { 984120660Ssimokawa xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 985120660Ssimokawa if (xfer->send.payload == NULL) { 986113584Ssimokawa fw_xfer_free(xfer); 987113584Ssimokawa return(NULL); 988113584Ssimokawa } 989113584Ssimokawa } 990120660Ssimokawa if (recv_len > 0) { 991120660Ssimokawa xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 992120660Ssimokawa if (xfer->recv.payload == NULL) { 993120660Ssimokawa if (xfer->send.payload != NULL) 994120660Ssimokawa free(xfer->send.payload, type); 995113584Ssimokawa fw_xfer_free(xfer); 996113584Ssimokawa return(NULL); 997113584Ssimokawa } 998113584Ssimokawa } 999113584Ssimokawa return(xfer); 1000113584Ssimokawa} 1001113584Ssimokawa 1002103285Sikob/* 1003103285Sikob * IEEE1394 XFER post process. 1004103285Sikob */ 1005103285Sikobvoid 1006103285Sikobfw_xfer_done(struct fw_xfer *xfer) 1007103285Sikob{ 1008167632Ssimokawa if (xfer->hand == NULL) { 1009167632Ssimokawa printf("hand == NULL\n"); 1010103285Sikob return; 1011117716Ssimokawa } 1012103285Sikob 1013121505Ssimokawa if (xfer->fc == NULL) 1014121505Ssimokawa panic("fw_xfer_done: why xfer->fc is NULL?"); 1015121505Ssimokawa 1016167632Ssimokawa xfer->hand(xfer); 1017103285Sikob} 1018103285Sikob 1019106790Ssimokawavoid 1020113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer) 1021103285Sikob{ 1022103285Sikob int s; 1023113584Ssimokawa 1024103285Sikob if(xfer == NULL ) return; 1025103285Sikob if(xfer->state == FWXF_INQ){ 1026103285Sikob printf("fw_xfer_free FWXF_INQ\n"); 1027103285Sikob s = splfw(); 1028103285Sikob STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1029103285Sikob xfer->q->queued --; 1030103285Sikob splx(s); 1031103285Sikob } 1032113584Ssimokawa if (xfer->fc != NULL) { 1033114729Ssimokawa#if 1 1034113584Ssimokawa if(xfer->state == FWXF_START) 1035114729Ssimokawa /* 1036114729Ssimokawa * This could happen if: 1037114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 1038114729Ssimokawa * 2. firewire_watch() is called. 1039114729Ssimokawa */ 1040114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 1041103285Sikob#endif 1042113584Ssimokawa fw_tl_free(xfer->fc, xfer); 1043103285Sikob } 1044113584Ssimokawa xfer->state = FWXF_INIT; 1045113584Ssimokawa xfer->resp = 0; 1046113584Ssimokawa} 1047113584Ssimokawa/* 1048113584Ssimokawa * To free IEEE1394 XFER structure. 1049113584Ssimokawa */ 1050113584Ssimokawavoid 1051120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer) 1052113584Ssimokawa{ 1053120660Ssimokawa if (xfer == NULL) { 1054127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1055120660Ssimokawa return; 1056120660Ssimokawa } 1057113584Ssimokawa fw_xfer_unload(xfer); 1058120660Ssimokawa if(xfer->send.payload != NULL){ 1059120660Ssimokawa free(xfer->send.payload, xfer->malloc); 1060103285Sikob } 1061120660Ssimokawa if(xfer->recv.payload != NULL){ 1062120660Ssimokawa free(xfer->recv.payload, xfer->malloc); 1063103285Sikob } 1064110269Ssimokawa free(xfer, xfer->malloc); 1065103285Sikob} 1066103285Sikob 1067120660Ssimokawavoid 1068120660Ssimokawafw_xfer_free( struct fw_xfer* xfer) 1069120660Ssimokawa{ 1070120660Ssimokawa if (xfer == NULL) { 1071127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1072120660Ssimokawa return; 1073120660Ssimokawa } 1074120660Ssimokawa fw_xfer_unload(xfer); 1075120660Ssimokawa free(xfer, xfer->malloc); 1076120660Ssimokawa} 1077120660Ssimokawa 1078120660Ssimokawavoid 1079110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 1080103285Sikob{ 1081103285Sikob#if 0 1082110072Ssimokawa printf("asyreq done state=%d resp=%d\n", 1083103285Sikob xfer->state, xfer->resp); 1084103285Sikob#endif 1085103285Sikob fw_xfer_free(xfer); 1086103285Sikob} 1087103285Sikob 1088103285Sikob/* 1089103285Sikob * To configure PHY. 1090103285Sikob */ 1091103285Sikobstatic void 1092103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1093103285Sikob{ 1094103285Sikob struct fw_xfer *xfer; 1095103285Sikob struct fw_pkt *fp; 1096103285Sikob 1097103285Sikob fc->status = FWBUSPHYCONF; 1098103285Sikob 1099120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1100113584Ssimokawa if (xfer == NULL) 1101113584Ssimokawa return; 1102103285Sikob xfer->fc = fc; 1103167632Ssimokawa xfer->hand = fw_asy_callback_free; 1104103285Sikob 1105120660Ssimokawa fp = &xfer->send.hdr; 1106103285Sikob fp->mode.ld[1] = 0; 1107103285Sikob if (root_node >= 0) 1108113584Ssimokawa fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1109103285Sikob if (gap_count >= 0) 1110113584Ssimokawa fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1111103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 1112103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 1113103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 1114103285Sikob 1115107653Ssimokawa if (firewire_debug) 1116107653Ssimokawa printf("send phy_config root_node=%d gap_count=%d\n", 1117103285Sikob root_node, gap_count); 1118103285Sikob fw_asyreq(fc, -1, xfer); 1119103285Sikob} 1120103285Sikob 1121103285Sikob#if 0 1122103285Sikob/* 1123103285Sikob * Dump self ID. 1124103285Sikob */ 1125103285Sikobstatic void 1126129585Sdfrfw_print_sid(uint32_t sid) 1127103285Sikob{ 1128103285Sikob union fw_self_id *s; 1129103285Sikob s = (union fw_self_id *) &sid; 1130103285Sikob printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1131103285Sikob " p0:%d p1:%d p2:%d i:%d m:%d\n", 1132103285Sikob s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1133103285Sikob s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1134103285Sikob s->p0.power_class, s->p0.port0, s->p0.port1, 1135103285Sikob s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1136103285Sikob} 1137103285Sikob#endif 1138103285Sikob 1139103285Sikob/* 1140103285Sikob * To receive self ID. 1141103285Sikob */ 1142129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len) 1143103285Sikob{ 1144129585Sdfr uint32_t *p; 1145103285Sikob union fw_self_id *self_id; 1146103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1147103285Sikob 1148129585Sdfr fc->sid_cnt = len /(sizeof(uint32_t) * 2); 1149103285Sikob fc->status = FWBUSINIT; 1150103285Sikob fc->max_node = fc->nodeid & 0x3f; 1151129585Sdfr CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16; 1152103285Sikob fc->status = FWBUSCYMELECT; 1153103285Sikob fc->topology_map->crc_len = 2; 1154103285Sikob fc->topology_map->generation ++; 1155103285Sikob fc->topology_map->self_id_count = 0; 1156103285Sikob fc->topology_map->node_count = 0; 1157103285Sikob fc->speed_map->generation ++; 1158103285Sikob fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1159103285Sikob self_id = &fc->topology_map->self_id[0]; 1160103285Sikob for(i = 0; i < fc->sid_cnt; i ++){ 1161103285Sikob if (sid[1] != ~sid[0]) { 1162103285Sikob printf("fw_sidrcv: invalid self-id packet\n"); 1163103285Sikob sid += 2; 1164103285Sikob continue; 1165103285Sikob } 1166103285Sikob *self_id = *((union fw_self_id *)sid); 1167103285Sikob fc->topology_map->crc_len++; 1168103285Sikob if(self_id->p0.sequel == 0){ 1169103285Sikob fc->topology_map->node_count ++; 1170103285Sikob c_port = 0; 1171103285Sikob#if 0 1172103285Sikob fw_print_sid(sid[0]); 1173103285Sikob#endif 1174103285Sikob node = self_id->p0.phy_id; 1175103285Sikob if(fc->max_node < node){ 1176103285Sikob fc->max_node = self_id->p0.phy_id; 1177103285Sikob } 1178103285Sikob /* XXX I'm not sure this is the right speed_map */ 1179103285Sikob fc->speed_map->speed[node][node] 1180103285Sikob = self_id->p0.phy_speed; 1181103285Sikob for (j = 0; j < node; j ++) { 1182103285Sikob fc->speed_map->speed[j][node] 1183103285Sikob = fc->speed_map->speed[node][j] 1184103285Sikob = min(fc->speed_map->speed[j][j], 1185103285Sikob self_id->p0.phy_speed); 1186103285Sikob } 1187103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1188103285Sikob (self_id->p0.link_active && self_id->p0.contender)) { 1189103285Sikob fc->irm = self_id->p0.phy_id; 1190103285Sikob } 1191103285Sikob if(self_id->p0.port0 >= 0x2){ 1192103285Sikob c_port++; 1193103285Sikob } 1194103285Sikob if(self_id->p0.port1 >= 0x2){ 1195103285Sikob c_port++; 1196103285Sikob } 1197103285Sikob if(self_id->p0.port2 >= 0x2){ 1198103285Sikob c_port++; 1199103285Sikob } 1200103285Sikob } 1201103285Sikob if(c_port > 2){ 1202103285Sikob i_branch += (c_port - 2); 1203103285Sikob } 1204103285Sikob sid += 2; 1205103285Sikob self_id++; 1206103285Sikob fc->topology_map->self_id_count ++; 1207103285Sikob } 1208108655Ssimokawa device_printf(fc->bdev, "%d nodes", fc->max_node + 1); 1209103285Sikob /* CRC */ 1210103285Sikob fc->topology_map->crc = fw_crc16( 1211129585Sdfr (uint32_t *)&fc->topology_map->generation, 1212103285Sikob fc->topology_map->crc_len * 4); 1213103285Sikob fc->speed_map->crc = fw_crc16( 1214129585Sdfr (uint32_t *)&fc->speed_map->generation, 1215103285Sikob fc->speed_map->crc_len * 4); 1216103285Sikob /* byteswap and copy to CSR */ 1217129585Sdfr p = (uint32_t *)fc->topology_map; 1218103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1219103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1220129585Sdfr p = (uint32_t *)fc->speed_map; 1221103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1222103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1223129585Sdfr /* don't byte-swap uint8_t array */ 1224103285Sikob bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1225103285Sikob 1226103285Sikob fc->max_hop = fc->max_node - i_branch; 1227103285Sikob printf(", maxhop <= %d", fc->max_hop); 1228103285Sikob 1229103285Sikob if(fc->irm == -1 ){ 1230103285Sikob printf(", Not found IRM capable node"); 1231103285Sikob }else{ 1232103285Sikob printf(", cable IRM = %d", fc->irm); 1233103285Sikob if (fc->irm == fc->nodeid) 1234110016Ssimokawa printf(" (me)"); 1235103285Sikob } 1236110016Ssimokawa printf("\n"); 1237103285Sikob 1238109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1239114909Ssimokawa if (fc->irm == fc->nodeid) { 1240103285Sikob fc->status = FWBUSMGRDONE; 1241103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1242114909Ssimokawa fw_bmr(fc); 1243109736Ssimokawa } else { 1244103285Sikob fc->status = FWBUSMGRELECT; 1245110193Ssimokawa callout_reset(&fc->bmr_callout, hz/8, 1246110193Ssimokawa (void *)fw_try_bmr, (void *)fc); 1247103285Sikob } 1248114909Ssimokawa } else 1249103285Sikob fc->status = FWBUSMGRDONE; 1250114909Ssimokawa 1251108853Ssimokawa callout_reset(&fc->busprobe_callout, hz/4, 1252108853Ssimokawa (void *)fw_bus_probe, (void *)fc); 1253103285Sikob} 1254106790Ssimokawa 1255103285Sikob/* 1256103285Sikob * To probe devices on the IEEE1394 bus. 1257103285Sikob */ 1258106790Ssimokawastatic void 1259106790Ssimokawafw_bus_probe(struct firewire_comm *fc) 1260103285Sikob{ 1261103285Sikob int s; 1262120850Ssimokawa struct fw_device *fwdev; 1263103285Sikob 1264103285Sikob s = splfw(); 1265103285Sikob fc->status = FWBUSEXPLORE; 1266103285Sikob 1267120850Ssimokawa /* Invalidate all devices, just after bus reset. */ 1268120850Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1269110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1270103285Sikob fwdev->status = FWDEVINVAL; 1271103285Sikob fwdev->rcnt = 0; 1272103285Sikob } 1273120850Ssimokawa 1274103285Sikob fc->ongonode = 0; 1275103285Sikob fc->ongoaddr = CSRROMOFF; 1276103285Sikob fc->ongodev = NULL; 1277103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1278103285Sikob fw_bus_explore(fc); 1279103285Sikob splx(s); 1280103285Sikob} 1281106790Ssimokawa 1282103285Sikob/* 1283129274Sdfr * Find the self_id packet for a node, ignoring sequels. 1284129274Sdfr */ 1285129274Sdfrstatic union fw_self_id * 1286129274Sdfrfw_find_self_id(struct firewire_comm *fc, int node) 1287129274Sdfr{ 1288129274Sdfr uint32_t i; 1289129274Sdfr union fw_self_id *s; 1290129274Sdfr 1291129274Sdfr for (i = 0; i < fc->topology_map->self_id_count; i++) { 1292129274Sdfr s = &fc->topology_map->self_id[i]; 1293129274Sdfr if (s->p0.sequel) 1294129274Sdfr continue; 1295129274Sdfr if (s->p0.phy_id == node) 1296129274Sdfr return s; 1297129274Sdfr } 1298129274Sdfr return 0; 1299129274Sdfr} 1300129274Sdfr 1301129274Sdfr/* 1302103285Sikob * To collect device informations on the IEEE1394 bus. 1303103285Sikob */ 1304106790Ssimokawastatic void 1305106790Ssimokawafw_bus_explore(struct firewire_comm *fc ) 1306103285Sikob{ 1307103285Sikob int err = 0; 1308110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1309129585Sdfr uint32_t addr; 1310103285Sikob struct fw_xfer *xfer; 1311103285Sikob struct fw_pkt *fp; 1312152792Ssimokawa union fw_self_id *fwsid; 1313103285Sikob 1314103285Sikob if(fc->status != FWBUSEXPLORE) 1315103285Sikob return; 1316103285Sikob 1317103285Sikobloop: 1318103285Sikob if(fc->ongonode == fc->nodeid) fc->ongonode++; 1319103285Sikob 1320103285Sikob if(fc->ongonode > fc->max_node) goto done; 1321103285Sikob if(fc->ongonode >= 0x3f) goto done; 1322103285Sikob 1323103285Sikob /* check link */ 1324103285Sikob /* XXX we need to check phy_id first */ 1325152792Ssimokawa fwsid = fw_find_self_id(fc, fc->ongonode); 1326152792Ssimokawa if (!fwsid || !fwsid->p0.link_active) { 1327110577Ssimokawa if (firewire_debug) 1328110577Ssimokawa printf("node%d: link down\n", fc->ongonode); 1329103285Sikob fc->ongonode++; 1330103285Sikob goto loop; 1331103285Sikob } 1332103285Sikob 1333103285Sikob if(fc->ongoaddr <= CSRROMOFF && 1334103285Sikob fc->ongoeui.hi == 0xffffffff && 1335103285Sikob fc->ongoeui.lo == 0xffffffff ){ 1336103285Sikob fc->ongoaddr = CSRROMOFF; 1337103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1338103285Sikob }else if(fc->ongoeui.hi == 0xffffffff ){ 1339103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1340103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1341103285Sikob }else if(fc->ongoeui.lo == 0xffffffff ){ 1342103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1343103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1344103285Sikob }else if(fc->ongodev == NULL){ 1345110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1346110193Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui)) 1347103285Sikob break; 1348103285Sikob if(fwdev != NULL){ 1349103285Sikob fwdev->dst = fc->ongonode; 1350117350Ssimokawa fwdev->status = FWDEVINIT; 1351117350Ssimokawa fc->ongodev = fwdev; 1352103285Sikob fc->ongoaddr = CSRROMOFF; 1353117350Ssimokawa addr = 0xf0000000 | fc->ongoaddr; 1354117350Ssimokawa goto dorequest; 1355103285Sikob } 1356115787Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, 1357115787Ssimokawa M_NOWAIT | M_ZERO); 1358103285Sikob if(fwdev == NULL) 1359103285Sikob return; 1360106810Ssimokawa fwdev->fc = fc; 1361103285Sikob fwdev->rommax = 0; 1362103285Sikob fwdev->dst = fc->ongonode; 1363103285Sikob fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo; 1364103285Sikob fwdev->status = FWDEVINIT; 1365103285Sikob fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode]; 1366103285Sikob 1367110179Ssimokawa pfwdev = NULL; 1368110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1369110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1370110179Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1371110179Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1372110179Ssimokawa break; 1373110179Ssimokawa pfwdev = tfwdev; 1374103285Sikob } 1375110179Ssimokawa if (pfwdev == NULL) 1376110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1377110179Ssimokawa else 1378110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1379103285Sikob 1380108655Ssimokawa device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1381107653Ssimokawa linkspeed[fwdev->speed], 1382107653Ssimokawa fc->ongoeui.hi, fc->ongoeui.lo); 1383103285Sikob 1384103285Sikob fc->ongodev = fwdev; 1385103285Sikob fc->ongoaddr = CSRROMOFF; 1386103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1387103285Sikob }else{ 1388103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1389103285Sikob } 1390117350Ssimokawadorequest: 1391103285Sikob#if 0 1392103285Sikob xfer = asyreqq(fc, FWSPD_S100, 0, 0, 1393103285Sikob ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr, 1394103285Sikob fw_bus_explore_callback); 1395103285Sikob if(xfer == NULL) goto done; 1396103285Sikob#else 1397120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1398103285Sikob if(xfer == NULL){ 1399103285Sikob goto done; 1400103285Sikob } 1401120660Ssimokawa xfer->send.spd = 0; 1402120660Ssimokawa fp = &xfer->send.hdr; 1403113584Ssimokawa fp->mode.rreqq.dest_hi = 0xffff; 1404103285Sikob fp->mode.rreqq.tlrt = 0; 1405103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1406103285Sikob fp->mode.rreqq.pri = 0; 1407103285Sikob fp->mode.rreqq.src = 0; 1408120660Ssimokawa fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode; 1409113584Ssimokawa fp->mode.rreqq.dest_lo = addr; 1410167632Ssimokawa xfer->hand = fw_bus_explore_callback; 1411103285Sikob 1412110577Ssimokawa if (firewire_debug) 1413110577Ssimokawa printf("node%d: explore addr=0x%x\n", 1414110577Ssimokawa fc->ongonode, fc->ongoaddr); 1415103285Sikob err = fw_asyreq(fc, -1, xfer); 1416103285Sikob if(err){ 1417103285Sikob fw_xfer_free( xfer); 1418103285Sikob return; 1419103285Sikob } 1420103285Sikob#endif 1421103285Sikob return; 1422103285Sikobdone: 1423103285Sikob /* fw_attach_devs */ 1424103285Sikob fc->status = FWBUSEXPDONE; 1425107653Ssimokawa if (firewire_debug) 1426107653Ssimokawa printf("bus_explore done\n"); 1427103285Sikob fw_attach_dev(fc); 1428103285Sikob return; 1429103285Sikob 1430103285Sikob} 1431106790Ssimokawa 1432103285Sikob/* Portable Async. request read quad */ 1433106790Ssimokawastruct fw_xfer * 1434129585Sdfrasyreqq(struct firewire_comm *fc, uint8_t spd, uint8_t tl, uint8_t rt, 1435129585Sdfr uint32_t addr_hi, uint32_t addr_lo, 1436124169Ssimokawa void (*hand) (struct fw_xfer*)) 1437103285Sikob{ 1438103285Sikob struct fw_xfer *xfer; 1439103285Sikob struct fw_pkt *fp; 1440103285Sikob int err; 1441103285Sikob 1442120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1443113584Ssimokawa if (xfer == NULL) 1444103285Sikob return NULL; 1445113584Ssimokawa 1446120660Ssimokawa xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */ 1447120660Ssimokawa fp = &xfer->send.hdr; 1448113584Ssimokawa fp->mode.rreqq.dest_hi = addr_hi & 0xffff; 1449103285Sikob if(tl & FWP_TL_VALID){ 1450103285Sikob fp->mode.rreqq.tlrt = (tl & 0x3f) << 2; 1451103285Sikob }else{ 1452103285Sikob fp->mode.rreqq.tlrt = 0; 1453103285Sikob } 1454103285Sikob fp->mode.rreqq.tlrt |= rt & 0x3; 1455103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1456103285Sikob fp->mode.rreqq.pri = 0; 1457103285Sikob fp->mode.rreqq.src = 0; 1458120660Ssimokawa fp->mode.rreqq.dst = addr_hi >> 16; 1459113584Ssimokawa fp->mode.rreqq.dest_lo = addr_lo; 1460167632Ssimokawa xfer->hand = hand; 1461103285Sikob 1462103285Sikob err = fw_asyreq(fc, -1, xfer); 1463103285Sikob if(err){ 1464103285Sikob fw_xfer_free( xfer); 1465103285Sikob return NULL; 1466103285Sikob } 1467103285Sikob return xfer; 1468103285Sikob} 1469106790Ssimokawa 1470103285Sikob/* 1471103285Sikob * Callback for the IEEE1394 bus information collection. 1472103285Sikob */ 1473106790Ssimokawastatic void 1474106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer) 1475106790Ssimokawa{ 1476103285Sikob struct firewire_comm *fc; 1477103285Sikob struct fw_pkt *sfp,*rfp; 1478103285Sikob struct csrhdr *chdr; 1479103285Sikob struct csrdir *csrd; 1480103285Sikob struct csrreg *csrreg; 1481129585Sdfr uint32_t offset; 1482103285Sikob 1483103285Sikob 1484110577Ssimokawa if(xfer == NULL) { 1485110577Ssimokawa printf("xfer == NULL\n"); 1486110577Ssimokawa return; 1487110577Ssimokawa } 1488103285Sikob fc = xfer->fc; 1489110577Ssimokawa 1490110577Ssimokawa if (firewire_debug) 1491110577Ssimokawa printf("node%d: callback addr=0x%x\n", 1492110577Ssimokawa fc->ongonode, fc->ongoaddr); 1493110577Ssimokawa 1494103285Sikob if(xfer->resp != 0){ 1495127468Ssimokawa device_printf(fc->bdev, 1496167630Ssimokawa "bus_explore node=%d addr=0x%x resp=%d\n", 1497167630Ssimokawa fc->ongonode, fc->ongoaddr, xfer->resp); 1498114284Ssimokawa goto errnode; 1499103285Sikob } 1500103285Sikob 1501120660Ssimokawa sfp = &xfer->send.hdr; 1502120660Ssimokawa rfp = &xfer->recv.hdr; 1503103285Sikob#if 0 1504103285Sikob { 1505129585Sdfr uint32_t *qld; 1506103285Sikob int i; 1507129585Sdfr qld = (uint32_t *)xfer->recv.buf; 1508103285Sikob printf("len:%d\n", xfer->recv.len); 1509103285Sikob for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){ 1510113584Ssimokawa printf("0x%08x ", rfp->mode.ld[i/4]); 1511103285Sikob if((i % 16) == 15) printf("\n"); 1512103285Sikob } 1513103285Sikob if((i % 16) != 15) printf("\n"); 1514103285Sikob } 1515103285Sikob#endif 1516103285Sikob if(fc->ongodev == NULL){ 1517113584Ssimokawa if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){ 1518103285Sikob rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data); 1519103285Sikob chdr = (struct csrhdr *)(&rfp->mode.rresq.data); 1520129541Sdfr/* If CSR is minimal confinguration, more investigation is not needed. */ 1521103285Sikob if(chdr->info_len == 1){ 1522110577Ssimokawa if (firewire_debug) 1523110577Ssimokawa printf("node%d: minimal config\n", 1524110577Ssimokawa fc->ongonode); 1525103285Sikob goto nextnode; 1526103285Sikob }else{ 1527103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1528103285Sikob } 1529113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){ 1530103285Sikob fc->ongoeui.hi = ntohl(rfp->mode.rresq.data); 1531103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1532113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){ 1533103285Sikob fc->ongoeui.lo = ntohl(rfp->mode.rresq.data); 1534110577Ssimokawa if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) { 1535110577Ssimokawa if (firewire_debug) 1536110577Ssimokawa printf("node%d: eui64 is zero.\n", 1537110577Ssimokawa fc->ongonode); 1538103285Sikob goto nextnode; 1539110577Ssimokawa } 1540103285Sikob fc->ongoaddr = CSRROMOFF; 1541103285Sikob } 1542103285Sikob }else{ 1543117350Ssimokawa if (fc->ongoaddr == CSRROMOFF && 1544117350Ssimokawa fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) { 1545117350Ssimokawa fc->ongodev->status = FWDEVATTACHED; 1546117350Ssimokawa goto nextnode; 1547117350Ssimokawa } 1548103285Sikob fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data); 1549103285Sikob if(fc->ongoaddr > fc->ongodev->rommax){ 1550103285Sikob fc->ongodev->rommax = fc->ongoaddr; 1551103285Sikob } 1552103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1553103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1554103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1555103285Sikob offset = CSRROMOFF; 1556103285Sikob }else{ 1557103285Sikob chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]; 1558103285Sikob offset = csrd->off; 1559103285Sikob } 1560103285Sikob if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){ 1561103285Sikob csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4]; 1562103285Sikob if( csrreg->key == 0x81 || csrreg->key == 0xd1){ 1563103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1564103285Sikob if(csrd == NULL){ 1565103285Sikob goto nextnode; 1566103285Sikob }else{ 1567103285Sikob csrd->ongoaddr = fc->ongoaddr; 1568103285Sikob fc->ongoaddr += csrreg->val * 4; 1569103285Sikob csrd->off = fc->ongoaddr; 1570103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1571103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1572103285Sikob goto nextaddr; 1573103285Sikob } 1574103285Sikob } 1575103285Sikob } 1576103285Sikob fc->ongoaddr += 4; 1577103285Sikob if(((fc->ongoaddr - offset)/4 > chdr->crc_len) && 1578103285Sikob (fc->ongodev->rommax < 0x414)){ 1579103285Sikob if(fc->ongodev->rommax <= 0x414){ 1580103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1581103285Sikob if(csrd == NULL) goto nextnode; 1582103285Sikob csrd->off = fc->ongoaddr; 1583103285Sikob csrd->ongoaddr = fc->ongoaddr; 1584103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1585103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1586103285Sikob } 1587103285Sikob goto nextaddr; 1588103285Sikob } 1589103285Sikob 1590103285Sikob while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){ 1591103285Sikob if(csrd == NULL){ 1592103285Sikob goto nextnode; 1593103285Sikob }; 1594103285Sikob fc->ongoaddr = csrd->ongoaddr + 4; 1595103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1596103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1597103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1598103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1599103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1600103285Sikob offset = CSRROMOFF; 1601103285Sikob }else{ 1602103285Sikob chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]); 1603103285Sikob offset = csrd->off; 1604103285Sikob } 1605103285Sikob } 1606103285Sikob if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){ 1607103285Sikob goto nextnode; 1608103285Sikob } 1609103285Sikob } 1610103285Sikobnextaddr: 1611103285Sikob fw_xfer_free( xfer); 1612103285Sikob fw_bus_explore(fc); 1613103285Sikob return; 1614114284Ssimokawaerrnode: 1615127468Ssimokawa if (fc->ongodev != NULL) { 1616114284Ssimokawa fc->ongodev->status = FWDEVINVAL; 1617127468Ssimokawa /* Invalidate ROM */ 1618127468Ssimokawa fc->ongodev->csrrom[0] = 0; 1619127468Ssimokawa } 1620103285Sikobnextnode: 1621103285Sikob fw_xfer_free( xfer); 1622103285Sikob fc->ongonode++; 1623103285Sikob/* housekeeping work space */ 1624103285Sikob fc->ongoaddr = CSRROMOFF; 1625103285Sikob fc->ongodev = NULL; 1626103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1627103285Sikob while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){ 1628103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1629103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1630103285Sikob } 1631103285Sikob fw_bus_explore(fc); 1632103285Sikob return; 1633103285Sikob} 1634103285Sikob 1635103285Sikob/* 1636103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1637103285Sikob */ 1638106815Ssimokawastatic void 1639106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1640103285Sikob{ 1641120850Ssimokawa struct fw_device *fwdev, *next; 1642103285Sikob int i, err; 1643103285Sikob device_t *devlistp; 1644103285Sikob int devcnt; 1645103285Sikob struct firewire_dev_comm *fdc; 1646103285Sikob 1647120850Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1648120850Ssimokawa next = STAILQ_NEXT(fwdev, link); 1649120850Ssimokawa if (fwdev->status == FWDEVINIT) { 1650114069Ssimokawa fwdev->status = FWDEVATTACHED; 1651120850Ssimokawa } else if (fwdev->status == FWDEVINVAL) { 1652120850Ssimokawa fwdev->rcnt ++; 1653120850Ssimokawa if (fwdev->rcnt > hold_count) { 1654120850Ssimokawa /* 1655120850Ssimokawa * Remove devices which have not been seen 1656120850Ssimokawa * for a while. 1657120850Ssimokawa */ 1658120850Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, 1659120850Ssimokawa link); 1660120850Ssimokawa free(fwdev, M_FW); 1661120850Ssimokawa } 1662120850Ssimokawa } 1663120850Ssimokawa } 1664103285Sikob 1665108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1666103285Sikob if( err != 0 ) 1667103285Sikob return; 1668103285Sikob for( i = 0 ; i < devcnt ; i++){ 1669103285Sikob if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1670103285Sikob fdc = device_get_softc(devlistp[i]); 1671103285Sikob if (fdc->post_explore != NULL) 1672103285Sikob fdc->post_explore(fdc); 1673103285Sikob } 1674103285Sikob } 1675103285Sikob free(devlistp, M_TEMP); 1676103285Sikob 1677103285Sikob return; 1678103285Sikob} 1679106815Ssimokawa 1680103285Sikob/* 1681129541Sdfr * To allocate unique transaction label. 1682103285Sikob */ 1683106815Ssimokawastatic int 1684106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1685103285Sikob{ 1686103285Sikob u_int i; 1687103285Sikob struct tlabel *tl, *tmptl; 1688103285Sikob int s; 1689129585Sdfr static uint32_t label = 0; 1690103285Sikob 1691103285Sikob s = splfw(); 1692103285Sikob for( i = 0 ; i < 0x40 ; i ++){ 1693103285Sikob label = (label + 1) & 0x3f; 1694103285Sikob for(tmptl = STAILQ_FIRST(&fc->tlabels[label]); 1695103285Sikob tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){ 1696120660Ssimokawa if (tmptl->xfer->send.hdr.mode.hdr.dst == 1697120660Ssimokawa xfer->send.hdr.mode.hdr.dst) 1698120660Ssimokawa break; 1699103285Sikob } 1700103285Sikob if(tmptl == NULL) { 1701110195Ssimokawa tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT); 1702103285Sikob if (tl == NULL) { 1703103285Sikob splx(s); 1704103285Sikob return (-1); 1705103285Sikob } 1706103285Sikob tl->xfer = xfer; 1707103285Sikob STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link); 1708103285Sikob splx(s); 1709110577Ssimokawa if (firewire_debug > 1) 1710110577Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", 1711120660Ssimokawa xfer->send.hdr.mode.hdr.dst, label); 1712103285Sikob return(label); 1713103285Sikob } 1714103285Sikob } 1715103285Sikob splx(s); 1716103285Sikob 1717130460Sdfr if (firewire_debug > 1) 1718130460Sdfr printf("fw_get_tlabel: no free tlabel\n"); 1719103285Sikob return(-1); 1720103285Sikob} 1721106815Ssimokawa 1722113584Ssimokawastatic void 1723120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb) 1724113584Ssimokawa{ 1725120660Ssimokawa struct fw_pkt *pkt; 1726120660Ssimokawa u_char *p; 1727120660Ssimokawa struct tcode_info *tinfo; 1728120660Ssimokawa u_int res, i, len, plen; 1729113584Ssimokawa 1730120660Ssimokawa rb->xfer->recv.spd -= rb->spd; 1731120660Ssimokawa 1732120660Ssimokawa pkt = (struct fw_pkt *)rb->vec->iov_base; 1733120660Ssimokawa tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 1734120660Ssimokawa 1735120660Ssimokawa /* Copy header */ 1736120660Ssimokawa p = (u_char *)&rb->xfer->recv.hdr; 1737120660Ssimokawa bcopy(rb->vec->iov_base, p, tinfo->hdr_len); 1738132771Skan rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len; 1739120660Ssimokawa rb->vec->iov_len -= tinfo->hdr_len; 1740120660Ssimokawa 1741120660Ssimokawa /* Copy payload */ 1742120660Ssimokawa p = (u_char *)rb->xfer->recv.payload; 1743120660Ssimokawa res = rb->xfer->recv.pay_len; 1744120660Ssimokawa 1745120660Ssimokawa /* special handling for RRESQ */ 1746120660Ssimokawa if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 1747129585Sdfr p != NULL && res >= sizeof(uint32_t)) { 1748129585Sdfr *(uint32_t *)p = pkt->mode.rresq.data; 1749129585Sdfr rb->xfer->recv.pay_len = sizeof(uint32_t); 1750120660Ssimokawa return; 1751120660Ssimokawa } 1752120660Ssimokawa 1753120660Ssimokawa if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 1754120660Ssimokawa return; 1755120660Ssimokawa 1756120660Ssimokawa plen = pkt->mode.rresb.len; 1757120660Ssimokawa 1758120660Ssimokawa for (i = 0; i < rb->nvec; i++, rb->vec++) { 1759120660Ssimokawa len = MIN(rb->vec->iov_len, plen); 1760113584Ssimokawa if (res < len) { 1761113584Ssimokawa printf("rcv buffer(%d) is %d bytes short.\n", 1762120660Ssimokawa rb->xfer->recv.pay_len, len - res); 1763113584Ssimokawa len = res; 1764113584Ssimokawa } 1765120660Ssimokawa bcopy(rb->vec->iov_base, p, len); 1766113584Ssimokawa p += len; 1767113584Ssimokawa res -= len; 1768120660Ssimokawa plen -= len; 1769120660Ssimokawa if (res == 0 || plen == 0) 1770113584Ssimokawa break; 1771113584Ssimokawa } 1772120660Ssimokawa rb->xfer->recv.pay_len -= res; 1773120660Ssimokawa 1774113584Ssimokawa} 1775113584Ssimokawa 1776103285Sikob/* 1777129541Sdfr * Generic packet receiving process. 1778103285Sikob */ 1779106815Ssimokawavoid 1780120660Ssimokawafw_rcv(struct fw_rcv_buf *rb) 1781103285Sikob{ 1782103285Sikob struct fw_pkt *fp, *resfp; 1783103285Sikob struct fw_bind *bind; 1784113584Ssimokawa int tcode, s; 1785113584Ssimokawa int i, len, oldstate; 1786103285Sikob#if 0 1787103285Sikob { 1788129585Sdfr uint32_t *qld; 1789103285Sikob int i; 1790129585Sdfr qld = (uint32_t *)buf; 1791103285Sikob printf("spd %d len:%d\n", spd, len); 1792103285Sikob for( i = 0 ; i <= len && i < 32; i+= 4){ 1793103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1794103285Sikob if((i % 16) == 15) printf("\n"); 1795103285Sikob } 1796103285Sikob if((i % 16) != 15) printf("\n"); 1797103285Sikob } 1798103285Sikob#endif 1799120660Ssimokawa fp = (struct fw_pkt *)rb->vec[0].iov_base; 1800113584Ssimokawa tcode = fp->mode.common.tcode; 1801113584Ssimokawa switch (tcode) { 1802103285Sikob case FWTCODE_WRES: 1803103285Sikob case FWTCODE_RRESQ: 1804103285Sikob case FWTCODE_RRESB: 1805103285Sikob case FWTCODE_LRES: 1806120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1807103285Sikob fp->mode.hdr.tlrt >> 2); 1808120660Ssimokawa if(rb->xfer == NULL) { 1809103285Sikob printf("fw_rcv: unknown response " 1810124251Ssimokawa "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n", 1811124251Ssimokawa tcode_str[tcode], tcode, 1812124251Ssimokawa fp->mode.hdr.src, 1813124251Ssimokawa fp->mode.hdr.tlrt >> 2, 1814124251Ssimokawa fp->mode.hdr.tlrt & 3, 1815124251Ssimokawa fp->mode.rresq.data); 1816103285Sikob#if 1 1817103285Sikob printf("try ad-hoc work around!!\n"); 1818120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1819103285Sikob (fp->mode.hdr.tlrt >> 2)^3); 1820120660Ssimokawa if (rb->xfer == NULL) { 1821103285Sikob printf("no use...\n"); 1822103285Sikob goto err; 1823103285Sikob } 1824103285Sikob#else 1825103285Sikob goto err; 1826103285Sikob#endif 1827103285Sikob } 1828120660Ssimokawa fw_rcv_copy(rb); 1829120660Ssimokawa if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 1830120660Ssimokawa rb->xfer->resp = EIO; 1831120660Ssimokawa else 1832120660Ssimokawa rb->xfer->resp = 0; 1833113584Ssimokawa /* make sure the packet is drained in AT queue */ 1834120660Ssimokawa oldstate = rb->xfer->state; 1835120660Ssimokawa rb->xfer->state = FWXF_RCVD; 1836113584Ssimokawa switch (oldstate) { 1837113584Ssimokawa case FWXF_SENT: 1838120660Ssimokawa fw_xfer_done(rb->xfer); 1839103285Sikob break; 1840113584Ssimokawa case FWXF_START: 1841119289Ssimokawa#if 0 1842113584Ssimokawa if (firewire_debug) 1843120660Ssimokawa printf("not sent yet tl=%x\n", rb->xfer->tl); 1844119289Ssimokawa#endif 1845113584Ssimokawa break; 1846103285Sikob default: 1847120660Ssimokawa printf("unexpected state %d\n", rb->xfer->state); 1848103285Sikob } 1849113584Ssimokawa return; 1850103285Sikob case FWTCODE_WREQQ: 1851103285Sikob case FWTCODE_WREQB: 1852103285Sikob case FWTCODE_RREQQ: 1853103285Sikob case FWTCODE_RREQB: 1854103285Sikob case FWTCODE_LREQ: 1855120660Ssimokawa bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 1856113584Ssimokawa fp->mode.rreqq.dest_lo); 1857103285Sikob if(bind == NULL){ 1858124251Ssimokawa printf("Unknown service addr 0x%04x:0x%08x %s(%x)" 1859127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1860127468Ssimokawa " src=0x%x data=%lx\n", 1861127468Ssimokawa#else 1862124251Ssimokawa " src=0x%x data=%x\n", 1863113962Ssimokawa#endif 1864124251Ssimokawa fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo, 1865124251Ssimokawa tcode_str[tcode], tcode, 1866124251Ssimokawa fp->mode.hdr.src, ntohl(fp->mode.wreqq.data)); 1867120660Ssimokawa if (rb->fc->status == FWBUSRESET) { 1868110798Ssimokawa printf("fw_rcv: cannot respond(bus reset)!\n"); 1869103285Sikob goto err; 1870103285Sikob } 1871120660Ssimokawa rb->xfer = fw_xfer_alloc(M_FWXFER); 1872120660Ssimokawa if(rb->xfer == NULL){ 1873103285Sikob return; 1874103285Sikob } 1875120660Ssimokawa rb->xfer->send.spd = rb->spd; 1876120660Ssimokawa rb->xfer->send.pay_len = 0; 1877120660Ssimokawa resfp = &rb->xfer->send.hdr; 1878113584Ssimokawa switch (tcode) { 1879103285Sikob case FWTCODE_WREQQ: 1880103285Sikob case FWTCODE_WREQB: 1881103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 1882103285Sikob break; 1883103285Sikob case FWTCODE_RREQQ: 1884103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1885103285Sikob break; 1886103285Sikob case FWTCODE_RREQB: 1887103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 1888103285Sikob break; 1889103285Sikob case FWTCODE_LREQ: 1890103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 1891103285Sikob break; 1892103285Sikob } 1893103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 1894103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1895103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 1896120660Ssimokawa resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 1897103285Sikob resfp->mode.rresb.extcode = 0; 1898103285Sikob resfp->mode.rresb.len = 0; 1899103285Sikob/* 1900167632Ssimokawa rb->xfer->hand = fw_asy_callback; 1901103285Sikob*/ 1902167632Ssimokawa rb->xfer->hand = fw_xfer_free; 1903120660Ssimokawa if(fw_asyreq(rb->fc, -1, rb->xfer)){ 1904120660Ssimokawa fw_xfer_free(rb->xfer); 1905103285Sikob return; 1906103285Sikob } 1907103285Sikob goto err; 1908103285Sikob } 1909113584Ssimokawa len = 0; 1910120660Ssimokawa for (i = 0; i < rb->nvec; i ++) 1911120660Ssimokawa len += rb->vec[i].iov_len; 1912113584Ssimokawa switch(bind->act_type){ 1913103285Sikob case FWACT_XFER: 1914113584Ssimokawa /* splfw()?? */ 1915120660Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 1916120660Ssimokawa if (rb->xfer == NULL) { 1917113584Ssimokawa printf("Discard a packet for this bind.\n"); 1918113584Ssimokawa goto err; 1919113584Ssimokawa } 1920113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1921120660Ssimokawa fw_rcv_copy(rb); 1922167632Ssimokawa rb->xfer->hand(rb->xfer); 1923103285Sikob return; 1924103285Sikob break; 1925103285Sikob case FWACT_CH: 1926120660Ssimokawa if(rb->fc->ir[bind->sub]->queued >= 1927120660Ssimokawa rb->fc->ir[bind->sub]->maxq){ 1928120660Ssimokawa device_printf(rb->fc->bdev, 1929108655Ssimokawa "Discard a packet %x %d\n", 1930113584Ssimokawa bind->sub, 1931120660Ssimokawa rb->fc->ir[bind->sub]->queued); 1932103285Sikob goto err; 1933103285Sikob } 1934120660Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 1935120660Ssimokawa if (rb->xfer == NULL) { 1936113584Ssimokawa printf("Discard packet for this bind\n"); 1937113584Ssimokawa goto err; 1938113584Ssimokawa } 1939113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1940120660Ssimokawa fw_rcv_copy(rb); 1941103285Sikob s = splfw(); 1942120660Ssimokawa rb->fc->ir[bind->sub]->queued++; 1943120660Ssimokawa STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q, 1944120660Ssimokawa rb->xfer, link); 1945103285Sikob splx(s); 1946103285Sikob 1947120660Ssimokawa wakeup((caddr_t)rb->fc->ir[bind->sub]); 1948103285Sikob 1949103285Sikob return; 1950103285Sikob break; 1951103285Sikob default: 1952103285Sikob goto err; 1953103285Sikob break; 1954103285Sikob } 1955103285Sikob break; 1956120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */ 1957103285Sikob case FWTCODE_STREAM: 1958103285Sikob { 1959103285Sikob struct fw_xferq *xferq; 1960103285Sikob 1961120660Ssimokawa xferq = rb->fc->ir[sub]; 1962103285Sikob#if 0 1963103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 1964103285Sikob sub, len, off, spd); 1965103285Sikob#endif 1966103285Sikob if(xferq->queued >= xferq->maxq) { 1967103285Sikob printf("receive queue is full\n"); 1968103285Sikob goto err; 1969103285Sikob } 1970113584Ssimokawa /* XXX get xfer from xfer queue, we don't need copy for 1971113584Ssimokawa per packet mode */ 1972120660Ssimokawa rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 1973113584Ssimokawa vec[0].iov_len); 1974120660Ssimokawa if (rb->xfer == NULL) goto err; 1975120660Ssimokawa fw_rcv_copy(rb) 1976103285Sikob s = splfw(); 1977103285Sikob xferq->queued++; 1978120660Ssimokawa STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link); 1979103285Sikob splx(s); 1980120660Ssimokawa sc = device_get_softc(rb->fc->bdev); 1981127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1982127468Ssimokawa if (&xferq->rsel.si_pid != 0) 1983127468Ssimokawa#else 1984103285Sikob if (SEL_WAITING(&xferq->rsel)) 1985103285Sikob#endif 1986122352Stanimura selwakeuppri(&xferq->rsel, FWPRI); 1987103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 1988103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 1989103285Sikob wakeup((caddr_t)xferq); 1990103285Sikob } 1991103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 1992103285Sikob xferq->hand(xferq); 1993103285Sikob } 1994103285Sikob return; 1995103285Sikob break; 1996103285Sikob } 1997120660Ssimokawa#endif 1998103285Sikob default: 1999113584Ssimokawa printf("fw_rcv: unknow tcode %d\n", tcode); 2000103285Sikob break; 2001103285Sikob } 2002103285Sikoberr: 2003113584Ssimokawa return; 2004103285Sikob} 2005106815Ssimokawa 2006103285Sikob/* 2007103285Sikob * Post process for Bus Manager election process. 2008103285Sikob */ 2009103285Sikobstatic void 2010103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 2011103285Sikob{ 2012103285Sikob struct firewire_comm *fc; 2013109422Ssimokawa int bmr; 2014103285Sikob 2015109422Ssimokawa if (xfer == NULL) 2016109422Ssimokawa return; 2017103285Sikob fc = xfer->fc; 2018109422Ssimokawa if (xfer->resp != 0) 2019103285Sikob goto error; 2020120660Ssimokawa if (xfer->recv.payload == NULL) 2021103285Sikob goto error; 2022120660Ssimokawa if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 2023103285Sikob goto error; 2024109422Ssimokawa 2025120660Ssimokawa bmr = ntohl(xfer->recv.payload[0]); 2026109422Ssimokawa if (bmr == 0x3f) 2027109422Ssimokawa bmr = fc->nodeid; 2028109422Ssimokawa 2029109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 2030120660Ssimokawa fw_xfer_free_buf(xfer); 2031114909Ssimokawa fw_bmr(fc); 2032114909Ssimokawa return; 2033114909Ssimokawa 2034103285Sikoberror: 2035114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 2036120660Ssimokawa fw_xfer_free_buf(xfer); 2037103285Sikob} 2038106815Ssimokawa 2039113584Ssimokawa 2040103285Sikob/* 2041103285Sikob * To candidate Bus Manager election process. 2042103285Sikob */ 2043110270Ssimokawastatic void 2044106815Ssimokawafw_try_bmr(void *arg) 2045103285Sikob{ 2046103285Sikob struct fw_xfer *xfer; 2047103285Sikob struct firewire_comm *fc = (struct firewire_comm *)arg; 2048103285Sikob struct fw_pkt *fp; 2049103285Sikob int err = 0; 2050103285Sikob 2051120660Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4); 2052103285Sikob if(xfer == NULL){ 2053103285Sikob return; 2054103285Sikob } 2055120660Ssimokawa xfer->send.spd = 0; 2056103285Sikob fc->status = FWBUSMGRELECT; 2057103285Sikob 2058120660Ssimokawa fp = &xfer->send.hdr; 2059113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 2060103285Sikob fp->mode.lreq.tlrt = 0; 2061103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 2062103285Sikob fp->mode.lreq.pri = 0; 2063103285Sikob fp->mode.lreq.src = 0; 2064113584Ssimokawa fp->mode.lreq.len = 8; 2065120660Ssimokawa fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 2066120660Ssimokawa fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 2067113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2068120660Ssimokawa xfer->send.payload[0] = htonl(0x3f); 2069120660Ssimokawa xfer->send.payload[1] = htonl(fc->nodeid); 2070167632Ssimokawa xfer->hand = fw_try_bmr_callback; 2071103285Sikob 2072103285Sikob err = fw_asyreq(fc, -1, xfer); 2073103285Sikob if(err){ 2074120660Ssimokawa fw_xfer_free_buf(xfer); 2075103285Sikob return; 2076103285Sikob } 2077103285Sikob return; 2078103285Sikob} 2079106543Ssimokawa 2080106543Ssimokawa#ifdef FW_VMACCESS 2081103285Sikob/* 2082103285Sikob * Software implementation for physical memory block access. 2083103285Sikob * XXX:Too slow, usef for debug purpose only. 2084103285Sikob */ 2085106815Ssimokawastatic void 2086106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){ 2087103285Sikob struct fw_pkt *rfp, *sfp = NULL; 2088129585Sdfr uint32_t *ld = (uint32_t *)xfer->recv.buf; 2089103285Sikob 2090113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 2091113584Ssimokawa xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 2092103285Sikob printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2093103285Sikob if(xfer->resp != 0){ 2094103285Sikob fw_xfer_free( xfer); 2095103285Sikob return; 2096103285Sikob } 2097103285Sikob if(xfer->recv.buf == NULL){ 2098103285Sikob fw_xfer_free( xfer); 2099103285Sikob return; 2100103285Sikob } 2101103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 2102103285Sikob switch(rfp->mode.hdr.tcode){ 2103103285Sikob /* XXX need fix for 64bit arch */ 2104103285Sikob case FWTCODE_WREQB: 2105110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2106103285Sikob xfer->send.len = 12; 2107103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2108103285Sikob bcopy(rfp->mode.wreqb.payload, 2109103285Sikob (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 2110103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2111103285Sikob sfp->mode.wres.rtcode = 0; 2112103285Sikob break; 2113103285Sikob case FWTCODE_WREQQ: 2114110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2115103285Sikob xfer->send.len = 12; 2116103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2117129585Sdfr *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 2118103285Sikob sfp->mode.wres.rtcode = 0; 2119103285Sikob break; 2120103285Sikob case FWTCODE_RREQB: 2121110195Ssimokawa xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 2122103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2123103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2124103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2125129585Sdfr sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len)); 2126103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 2127103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 2128103285Sikob sfp->mode.rresb.rtcode = 0; 2129103285Sikob sfp->mode.rresb.extcode = 0; 2130103285Sikob break; 2131103285Sikob case FWTCODE_RREQQ: 2132110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2133103285Sikob xfer->send.len = 16; 2134103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2135129585Sdfr sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2136103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 2137103285Sikob sfp->mode.rresb.rtcode = 0; 2138103285Sikob break; 2139103285Sikob default: 2140103285Sikob fw_xfer_free( xfer); 2141103285Sikob return; 2142103285Sikob } 2143103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2144103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2145167632Ssimokawa xfer->hand = fw_xfer_free; 2146103285Sikob 2147103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2148103285Sikob sfp->mode.hdr.pri = 0; 2149103285Sikob 2150103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2151103285Sikob/**/ 2152103285Sikob return; 2153103285Sikob} 2154106543Ssimokawa#endif 2155106543Ssimokawa 2156103285Sikob/* 2157103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2158103285Sikob */ 2159129585Sdfruint16_t 2160129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){ 2161129585Sdfr uint32_t i, sum, crc = 0; 2162103285Sikob int shift; 2163103285Sikob len = (len + 3) & ~3; 2164103285Sikob for(i = 0 ; i < len ; i+= 4){ 2165103285Sikob for( shift = 28 ; shift >= 0 ; shift -= 4){ 2166103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2167103285Sikob crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2168103285Sikob } 2169103285Sikob crc &= 0xffff; 2170103285Sikob } 2171129585Sdfr return((uint16_t) crc); 2172103285Sikob} 2173106815Ssimokawa 2174110270Ssimokawastatic int 2175110072Ssimokawafw_bmr(struct firewire_comm *fc) 2176110072Ssimokawa{ 2177110072Ssimokawa struct fw_device fwdev; 2178113584Ssimokawa union fw_self_id *self_id; 2179110072Ssimokawa int cmstr; 2180129585Sdfr uint32_t quad; 2181110072Ssimokawa 2182113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2183129274Sdfr self_id = fw_find_self_id(fc, fc->max_node); 2184113584Ssimokawa if (fc->max_node > 0) { 2185114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2186114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2187113584Ssimokawa cmstr = fc->max_node; 2188114909Ssimokawa else { 2189114909Ssimokawa device_printf(fc->bdev, 2190114909Ssimokawa "root node is not cycle master capable\n"); 2191114909Ssimokawa /* XXX shall we be the cycle master? */ 2192113584Ssimokawa cmstr = fc->nodeid; 2193114909Ssimokawa /* XXX need bus reset */ 2194114909Ssimokawa } 2195113584Ssimokawa } else 2196113584Ssimokawa cmstr = -1; 2197114909Ssimokawa 2198114909Ssimokawa device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID)); 2199114909Ssimokawa if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2200114909Ssimokawa /* We are not the bus manager */ 2201114909Ssimokawa printf("\n"); 2202114909Ssimokawa return(0); 2203114909Ssimokawa } 2204114909Ssimokawa printf("(me)\n"); 2205114909Ssimokawa 2206114909Ssimokawa /* Optimize gapcount */ 2207113584Ssimokawa if(fc->max_hop <= MAX_GAPHOP ) 2208113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2209110072Ssimokawa /* If we are the cycle master, nothing to do */ 2210113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2211110072Ssimokawa return 0; 2212110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2213110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2214110072Ssimokawa fwdev.fc = fc; 2215110072Ssimokawa fwdev.dst = cmstr; 2216110072Ssimokawa fwdev.speed = 0; 2217110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2218110072Ssimokawa fwdev.status = FWDEVINIT; 2219110072Ssimokawa /* Set cmstr bit on the cycle master */ 2220120660Ssimokawa quad = htonl(1 << 8); 2221110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2222120660Ssimokawa 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free); 2223110072Ssimokawa 2224110072Ssimokawa return 0; 2225110072Ssimokawa} 2226110072Ssimokawa 2227118455Ssimokawastatic int 2228118455Ssimokawafw_modevent(module_t mode, int type, void *data) 2229118455Ssimokawa{ 2230118455Ssimokawa int err = 0; 2231127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2232118455Ssimokawa static eventhandler_tag fwdev_ehtag = NULL; 2233118455Ssimokawa#endif 2234118455Ssimokawa 2235118455Ssimokawa switch (type) { 2236118455Ssimokawa case MOD_LOAD: 2237127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2238118455Ssimokawa fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone, 2239118455Ssimokawa fwdev_clone, 0, 1000); 2240118455Ssimokawa#endif 2241118455Ssimokawa break; 2242118455Ssimokawa case MOD_UNLOAD: 2243127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2244118455Ssimokawa if (fwdev_ehtag != NULL) 2245118455Ssimokawa EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag); 2246118455Ssimokawa#endif 2247118455Ssimokawa break; 2248118455Ssimokawa case MOD_SHUTDOWN: 2249118455Ssimokawa break; 2250132199Sphk default: 2251132199Sphk return (EOPNOTSUPP); 2252118455Ssimokawa } 2253118455Ssimokawa return (err); 2254118455Ssimokawa} 2255118455Ssimokawa 2256118455Ssimokawa 2257127468Ssimokawa#ifdef __DragonFly__ 2258127468SsimokawaDECLARE_DUMMY_MODULE(firewire); 2259127468Ssimokawa#endif 2260118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0); 2261103285SikobMODULE_VERSION(firewire, 1); 2262