firewire.c revision 121463
1103285Sikob/* 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 121463 2003-10-24 07:42:21Z 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> 43103285Sikob#include <sys/malloc.h> 44103285Sikob#include <sys/conf.h> 45103285Sikob#include <sys/sysctl.h> 46103285Sikob 47117067Ssimokawa#if __FreeBSD_version < 500000 48117067Ssimokawa#include <machine/clock.h> /* for DELAY() */ 49117067Ssimokawa#endif 50103285Sikob 51103285Sikob#include <sys/bus.h> /* used by smbus and newbus */ 52113584Ssimokawa#include <machine/bus.h> 53103285Sikob 54103285Sikob#include <dev/firewire/firewire.h> 55103285Sikob#include <dev/firewire/firewirereg.h> 56110072Ssimokawa#include <dev/firewire/fwmem.h> 57103285Sikob#include <dev/firewire/iec13213.h> 58103285Sikob#include <dev/firewire/iec68113.h> 59103285Sikob 60116376Ssimokawastruct crom_src_buf { 61116376Ssimokawa struct crom_src src; 62116376Ssimokawa struct crom_chunk root; 63116376Ssimokawa struct crom_chunk vendor; 64116376Ssimokawa struct crom_chunk hw; 65116376Ssimokawa}; 66116376Ssimokawa 67120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3; 68103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 69108281Ssimokawa "FireWire driver debug flag"); 70109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 71109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 72109736Ssimokawa "Try to be a bus manager"); 73120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0, 74120850Ssimokawa "Number of count of bus resets for removing lost device information"); 75103285Sikob 76110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 77110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 78110195Ssimokawa 79103285Sikob#define FW_MAXASYRTY 4 80103285Sikob 81103285Sikobdevclass_t firewire_devclass; 82103285Sikob 83103285Sikobstatic int firewire_match __P((device_t)); 84103285Sikobstatic int firewire_attach __P((device_t)); 85103285Sikobstatic int firewire_detach __P((device_t)); 86116978Ssimokawastatic int firewire_resume __P((device_t)); 87103285Sikob#if 0 88103285Sikobstatic int firewire_shutdown __P((device_t)); 89103285Sikob#endif 90103285Sikobstatic device_t firewire_add_child __P((device_t, int, const char *, int)); 91103285Sikobstatic void fw_try_bmr __P((void *)); 92103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *)); 93103285Sikobstatic void fw_asystart __P((struct fw_xfer *)); 94103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *)); 95103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *)); 96103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *)); 97103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *)); 98103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *)); 99106543Ssimokawa#ifdef FW_VMACCESS 100103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *)); 101106543Ssimokawa#endif 102103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t, 103105620Ssimokawa u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *)))); 104110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *)); 105103285Sikob 106103285Sikobstatic device_method_t firewire_methods[] = { 107103285Sikob /* Device interface */ 108103285Sikob DEVMETHOD(device_probe, firewire_match), 109103285Sikob DEVMETHOD(device_attach, firewire_attach), 110103285Sikob DEVMETHOD(device_detach, firewire_detach), 111108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 112116978Ssimokawa DEVMETHOD(device_resume, firewire_resume), 113103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 114103285Sikob 115103285Sikob /* Bus interface */ 116103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 117103285Sikob DEVMETHOD(bus_print_child, bus_generic_print_child), 118103285Sikob 119103285Sikob { 0, 0 } 120103285Sikob}; 121103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"}; 122103285Sikob 123114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 124114909Ssimokawa#define MAX_GAPHOP 15 125114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 126114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 127106813Ssimokawa 128103285Sikobstatic driver_t firewire_driver = { 129103285Sikob "firewire", 130103285Sikob firewire_methods, 131103285Sikob sizeof(struct firewire_softc), 132103285Sikob}; 133103285Sikob 134103285Sikob/* 135110072Ssimokawa * Lookup fwdev by node id. 136103285Sikob */ 137106810Ssimokawastruct fw_device * 138110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 139103285Sikob{ 140103285Sikob struct fw_device *fwdev; 141110072Ssimokawa int s; 142110072Ssimokawa 143110072Ssimokawa s = splfw(); 144110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 145120660Ssimokawa if (fwdev->dst == dst && fwdev->status != FWDEVINVAL) 146103285Sikob break; 147110072Ssimokawa splx(s); 148110072Ssimokawa 149106810Ssimokawa return fwdev; 150103285Sikob} 151106813Ssimokawa 152103285Sikob/* 153110072Ssimokawa * Lookup fwdev by EUI64. 154110072Ssimokawa */ 155110072Ssimokawastruct fw_device * 156110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 157110072Ssimokawa{ 158110072Ssimokawa struct fw_device *fwdev; 159110072Ssimokawa int s; 160110072Ssimokawa 161110072Ssimokawa s = splfw(); 162110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 163110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 164110072Ssimokawa break; 165110072Ssimokawa splx(s); 166110072Ssimokawa 167110072Ssimokawa if(fwdev == NULL) return NULL; 168110072Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 169110072Ssimokawa return fwdev; 170110072Ssimokawa} 171110072Ssimokawa 172110072Ssimokawa/* 173103285Sikob * Async. request procedure for userland application. 174103285Sikob */ 175103285Sikobint 176103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 177103285Sikob{ 178103285Sikob int err = 0; 179103285Sikob struct fw_xferq *xferq; 180103285Sikob int tl = 0, len; 181103285Sikob struct fw_pkt *fp; 182103285Sikob int tcode; 183103285Sikob struct tcode_info *info; 184103285Sikob 185103285Sikob if(xfer == NULL) return EINVAL; 186103285Sikob if(xfer->act.hand == NULL){ 187103285Sikob printf("act.hand == NULL\n"); 188103285Sikob return EINVAL; 189103285Sikob } 190120660Ssimokawa fp = &xfer->send.hdr; 191103285Sikob 192103285Sikob tcode = fp->mode.common.tcode & 0xf; 193103285Sikob info = &fc->tcode[tcode]; 194103285Sikob if (info->flag == 0) { 195103285Sikob printf("invalid tcode=%d\n", tcode); 196103285Sikob return EINVAL; 197103285Sikob } 198103285Sikob if (info->flag & FWTI_REQ) 199103285Sikob xferq = fc->atq; 200103285Sikob else 201103285Sikob xferq = fc->ats; 202103285Sikob len = info->hdr_len; 203120660Ssimokawa if (xfer->send.pay_len > MAXREC(fc->maxrec)) { 204120660Ssimokawa printf("send.pay_len > maxrec\n"); 205120660Ssimokawa return EINVAL; 206120660Ssimokawa } 207103285Sikob if (info->flag & FWTI_BLOCK_STR) 208120660Ssimokawa len = fp->mode.stream.len; 209103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 210120660Ssimokawa len = fp->mode.rresb.len; 211120660Ssimokawa else 212120660Ssimokawa len = 0; 213120660Ssimokawa if (len != xfer->send.pay_len){ 214120660Ssimokawa printf("len(%d) != send.pay_len(%d) (tcode=%d)\n", 215120660Ssimokawa len, xfer->send.pay_len, tcode); 216103285Sikob return EINVAL; 217103285Sikob } 218106790Ssimokawa 219103285Sikob if(xferq->start == NULL){ 220103285Sikob printf("xferq->start == NULL\n"); 221103285Sikob return EINVAL; 222103285Sikob } 223103285Sikob if(!(xferq->queued < xferq->maxq)){ 224108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 225108655Ssimokawa xferq->queued); 226103285Sikob return EINVAL; 227103285Sikob } 228103285Sikob 229120660Ssimokawa microtime(&xfer->tv); 230103285Sikob if (info->flag & FWTI_TLABEL) { 231103285Sikob if((tl = fw_get_tlabel(fc, xfer)) == -1 ) 232103285Sikob return EIO; 233103285Sikob fp->mode.hdr.tlrt = tl << 2; 234103285Sikob } 235103285Sikob 236103285Sikob xfer->tl = tl; 237103285Sikob xfer->resp = 0; 238103285Sikob xfer->fc = fc; 239103285Sikob xfer->q = xferq; 240103285Sikob xfer->retry_req = fw_asybusy; 241103285Sikob 242103285Sikob fw_asystart(xfer); 243103285Sikob return err; 244103285Sikob} 245103285Sikob/* 246103285Sikob * Wakeup blocked process. 247103285Sikob */ 248103285Sikobvoid 249103285Sikobfw_asy_callback(struct fw_xfer *xfer){ 250103285Sikob wakeup(xfer); 251103285Sikob return; 252103285Sikob} 253103285Sikob/* 254103285Sikob * Postpone to later retry. 255103285Sikob */ 256103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){ 257103285Sikob printf("fw_asybusy\n"); 258103285Sikob/* 259103285Sikob xfer->ch = timeout((timeout_t *)fw_asystart, (void *)xfer, 20000); 260103285Sikob*/ 261103285Sikob DELAY(20000); 262103285Sikob fw_asystart(xfer); 263103285Sikob return; 264103285Sikob} 265103285Sikob 266103285Sikob/* 267103285Sikob * Async. request with given xfer structure. 268103285Sikob */ 269106790Ssimokawastatic void 270106790Ssimokawafw_asystart(struct fw_xfer *xfer) 271106790Ssimokawa{ 272103285Sikob struct firewire_comm *fc = xfer->fc; 273103285Sikob int s; 274103285Sikob if(xfer->retry++ >= fc->max_asyretry){ 275113584Ssimokawa device_printf(fc->bdev, "max_asyretry exceeded\n"); 276103285Sikob xfer->resp = EBUSY; 277103285Sikob xfer->state = FWXF_BUSY; 278103285Sikob xfer->act.hand(xfer); 279103285Sikob return; 280103285Sikob } 281103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */ 282103285Sikob if (fc->status < FWBUSEXPLORE) { 283103285Sikob xfer->resp = EAGAIN; 284103285Sikob xfer->state = FWXF_BUSY; 285103285Sikob if (xfer->act.hand != NULL) 286103285Sikob xfer->act.hand(xfer); 287103285Sikob return; 288103285Sikob } 289103285Sikob#endif 290103285Sikob s = splfw(); 291103285Sikob xfer->state = FWXF_INQ; 292103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 293103285Sikob xfer->q->queued ++; 294103285Sikob splx(s); 295103285Sikob /* XXX just queue for mbuf */ 296103285Sikob if (xfer->mbuf == NULL) 297103285Sikob xfer->q->start(fc); 298103285Sikob return; 299103285Sikob} 300106790Ssimokawa 301103285Sikobstatic int 302103285Sikobfirewire_match( device_t dev ) 303103285Sikob{ 304108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 305103285Sikob return -140; 306103285Sikob} 307106790Ssimokawa 308110577Ssimokawastatic void 309110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc) 310110577Ssimokawa{ 311110577Ssimokawa struct fw_xfer *xfer; 312110577Ssimokawa struct tlabel *tl; 313110577Ssimokawa struct timeval tv; 314110577Ssimokawa struct timeval split_timeout; 315111040Ssimokawa int i, s; 316110577Ssimokawa 317120660Ssimokawa split_timeout.tv_sec = 0; 318120660Ssimokawa split_timeout.tv_usec = 200 * 1000; /* 200 msec */ 319110577Ssimokawa 320110577Ssimokawa microtime(&tv); 321110577Ssimokawa timevalsub(&tv, &split_timeout); 322110577Ssimokawa 323111040Ssimokawa s = splfw(); 324110577Ssimokawa for (i = 0; i < 0x40; i ++) { 325110577Ssimokawa while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 326110577Ssimokawa xfer = tl->xfer; 327110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 328110577Ssimokawa /* the rests are newer than this */ 329110577Ssimokawa break; 330110577Ssimokawa device_printf(fc->bdev, 331117716Ssimokawa "split transaction timeout dst=0x%x tl=0x%x state=%d\n", 332120660Ssimokawa xfer->send.hdr.mode.hdr.dst, i, xfer->state); 333110577Ssimokawa xfer->resp = ETIMEDOUT; 334110577Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], link); 335113584Ssimokawa fw_xfer_done(xfer); 336110577Ssimokawa } 337110577Ssimokawa } 338111040Ssimokawa splx(s); 339110577Ssimokawa} 340110577Ssimokawa 341121463Ssimokawa#define WATCHDOC_HZ 10 342110577Ssimokawastatic void 343110577Ssimokawafirewire_watchdog(void *arg) 344110577Ssimokawa{ 345110577Ssimokawa struct firewire_comm *fc; 346121463Ssimokawa static int watchdoc_clock = 0; 347110577Ssimokawa 348110577Ssimokawa fc = (struct firewire_comm *)arg; 349121463Ssimokawa 350121463Ssimokawa /* 351121463Ssimokawa * At boot stage, the device interrupt is disabled and 352121463Ssimokawa * We encounter a timeout easily. To avoid this, 353121463Ssimokawa * ignore clock interrupt for a while. 354121463Ssimokawa */ 355121463Ssimokawa if (watchdoc_clock > WATCHDOC_HZ * 15) { 356121463Ssimokawa firewire_xfer_timeout(fc); 357121463Ssimokawa fc->timeout(fc); 358121463Ssimokawa } else 359121463Ssimokawa watchdoc_clock ++; 360121463Ssimokawa 361121463Ssimokawa callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ, 362110577Ssimokawa (void *)firewire_watchdog, (void *)fc); 363110577Ssimokawa} 364110577Ssimokawa 365103285Sikob/* 366103285Sikob * The attach routine. 367103285Sikob */ 368103285Sikobstatic int 369118455Ssimokawafirewire_attach(device_t dev) 370103285Sikob{ 371118455Ssimokawa int unit; 372103285Sikob struct firewire_softc *sc = device_get_softc(dev); 373103285Sikob device_t pa = device_get_parent(dev); 374103285Sikob struct firewire_comm *fc; 375103285Sikob 376103285Sikob fc = (struct firewire_comm *)device_get_softc(pa); 377103285Sikob sc->fc = fc; 378116978Ssimokawa fc->status = FWBUSNOTREADY; 379103285Sikob 380118455Ssimokawa unit = device_get_unit(dev); 381118455Ssimokawa if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 382103285Sikob 383118455Ssimokawa fwdev_makedev(sc); 384118455Ssimokawa 385110193Ssimokawa CALLOUT_INIT(&sc->fc->timeout_callout); 386110193Ssimokawa CALLOUT_INIT(&sc->fc->bmr_callout); 387110193Ssimokawa CALLOUT_INIT(&sc->fc->retry_probe_callout); 388110193Ssimokawa CALLOUT_INIT(&sc->fc->busprobe_callout); 389108853Ssimokawa 390110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 391110577Ssimokawa (void *)firewire_watchdog, (void *)sc->fc); 392110193Ssimokawa 393103285Sikob /* Locate our children */ 394103285Sikob bus_generic_probe(dev); 395103285Sikob 396103285Sikob /* launch attachement of the added children */ 397103285Sikob bus_generic_attach(dev); 398103285Sikob 399103285Sikob /* bus_reset */ 400103285Sikob fc->ibr(fc); 401103285Sikob 402103285Sikob return 0; 403103285Sikob} 404103285Sikob 405103285Sikob/* 406103285Sikob * Attach it as child. 407103285Sikob */ 408103285Sikobstatic device_t 409103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit) 410103285Sikob{ 411103285Sikob device_t child; 412103285Sikob struct firewire_softc *sc; 413103285Sikob 414103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 415103285Sikob child = device_add_child(dev, name, unit); 416103285Sikob if (child) { 417103285Sikob device_set_ivars(child, sc->fc); 418103285Sikob device_probe_and_attach(child); 419103285Sikob } 420103285Sikob 421103285Sikob return child; 422103285Sikob} 423106790Ssimokawa 424116978Ssimokawastatic int 425116978Ssimokawafirewire_resume(device_t dev) 426116978Ssimokawa{ 427116978Ssimokawa struct firewire_softc *sc; 428116978Ssimokawa 429116978Ssimokawa sc = (struct firewire_softc *)device_get_softc(dev); 430116978Ssimokawa sc->fc->status = FWBUSNOTREADY; 431116978Ssimokawa 432116978Ssimokawa bus_generic_resume(dev); 433116978Ssimokawa 434116978Ssimokawa return(0); 435116978Ssimokawa} 436116978Ssimokawa 437103285Sikob/* 438103285Sikob * Dettach it. 439103285Sikob */ 440103285Sikobstatic int 441118455Ssimokawafirewire_detach(device_t dev) 442103285Sikob{ 443103285Sikob struct firewire_softc *sc; 444111074Ssimokawa struct csrdir *csrd, *next; 445111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 446118455Ssimokawa int err; 447103285Sikob 448103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 449118455Ssimokawa if ((err = fwdev_destroydev(sc)) != 0) 450118455Ssimokawa return err; 451106790Ssimokawa 452118455Ssimokawa if ((err = bus_generic_detach(dev)) != 0) 453118455Ssimokawa return err; 454111078Ssimokawa 455111078Ssimokawa callout_stop(&sc->fc->timeout_callout); 456111078Ssimokawa callout_stop(&sc->fc->bmr_callout); 457111078Ssimokawa callout_stop(&sc->fc->retry_probe_callout); 458111078Ssimokawa callout_stop(&sc->fc->busprobe_callout); 459111078Ssimokawa 460103285Sikob /* XXX xfree_free and untimeout on all xfers */ 461111078Ssimokawa for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL; 462111078Ssimokawa fwdev = fwdev_next) { 463111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 464111078Ssimokawa free(fwdev, M_FW); 465111078Ssimokawa } 466111074Ssimokawa for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) { 467111074Ssimokawa next = SLIST_NEXT(csrd, link); 468111074Ssimokawa free(csrd, M_FW); 469111074Ssimokawa } 470110195Ssimokawa free(sc->fc->topology_map, M_FW); 471110195Ssimokawa free(sc->fc->speed_map, M_FW); 472116376Ssimokawa free(sc->fc->crom_src_buf, M_FW); 473103285Sikob return(0); 474103285Sikob} 475103285Sikob#if 0 476103285Sikobstatic int 477103285Sikobfirewire_shutdown( device_t dev ) 478103285Sikob{ 479103285Sikob return 0; 480103285Sikob} 481103285Sikob#endif 482106790Ssimokawa 483110577Ssimokawa 484110577Ssimokawastatic void 485110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 486110577Ssimokawa{ 487110577Ssimokawa struct fw_xfer *xfer; 488110577Ssimokawa 489110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 490110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 491111942Ssimokawa xferq->queued --; 492110577Ssimokawa xfer->resp = EAGAIN; 493113584Ssimokawa fw_xfer_done(xfer); 494110577Ssimokawa } 495110577Ssimokawa} 496110577Ssimokawa 497110798Ssimokawavoid 498110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 499110798Ssimokawa{ 500110798Ssimokawa int i; 501110798Ssimokawa 502110798Ssimokawa fw_xferq_drain(fc->atq); 503110798Ssimokawa fw_xferq_drain(fc->ats); 504110798Ssimokawa for(i = 0; i < fc->nisodma; i++) 505110798Ssimokawa fw_xferq_drain(fc->it[i]); 506110798Ssimokawa} 507110798Ssimokawa 508116376Ssimokawastatic void 509116376Ssimokawafw_reset_csr(struct firewire_comm *fc) 510103285Sikob{ 511103285Sikob int i; 512103285Sikob 513103285Sikob CSRARC(fc, STATE_CLEAR) 514103285Sikob = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 515103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 516103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 517103285Sikob 518103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 519103285Sikob fc->irm = -1; 520103285Sikob 521103285Sikob fc->max_node = -1; 522103285Sikob 523103285Sikob for(i = 2; i < 0x100/4 - 2 ; i++){ 524103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 525103285Sikob } 526103285Sikob CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 527103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 528103285Sikob CSRARC(fc, RESET_START) = 0; 529103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 530103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 531103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 532103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 533103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 534103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 535103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 536103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 537103285Sikob CSRARC(fc, IP_CHANNELS) = (1 << 31); 538103285Sikob 539103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 540103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 541103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 542103285Sikob 1 << 28 | 0xff << 16 | 0x09 << 8; 543103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 544103285Sikob 545103285Sikob/* DV depend CSRs see blue book */ 546103285Sikob CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 547103285Sikob CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 548103285Sikob 549103285Sikob CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 ); 550103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 551116376Ssimokawa} 552113584Ssimokawa 553116376Ssimokawastatic void 554116376Ssimokawafw_init_crom(struct firewire_comm *fc) 555116376Ssimokawa{ 556116376Ssimokawa struct crom_src *src; 557116376Ssimokawa 558116376Ssimokawa fc->crom_src_buf = (struct crom_src_buf *) 559116376Ssimokawa malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO); 560116376Ssimokawa if (fc->crom_src_buf == NULL) 561116376Ssimokawa return; 562116376Ssimokawa 563116376Ssimokawa src = &fc->crom_src_buf->src; 564116376Ssimokawa bzero(src, sizeof(struct crom_src)); 565116376Ssimokawa 566116376Ssimokawa /* BUS info sample */ 567116376Ssimokawa src->hdr.info_len = 4; 568116376Ssimokawa 569116376Ssimokawa src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 570116376Ssimokawa 571116376Ssimokawa src->businfo.irmc = 1; 572116376Ssimokawa src->businfo.cmc = 1; 573116376Ssimokawa src->businfo.isc = 1; 574116376Ssimokawa src->businfo.bmc = 1; 575116376Ssimokawa src->businfo.pmc = 0; 576116376Ssimokawa src->businfo.cyc_clk_acc = 100; 577116376Ssimokawa src->businfo.max_rec = fc->maxrec; 578116376Ssimokawa src->businfo.max_rom = MAXROM_4; 579117350Ssimokawa src->businfo.generation = 1; 580116376Ssimokawa src->businfo.link_spd = fc->speed; 581116376Ssimokawa 582116376Ssimokawa src->businfo.eui64.hi = fc->eui.hi; 583116376Ssimokawa src->businfo.eui64.lo = fc->eui.lo; 584116376Ssimokawa 585116376Ssimokawa STAILQ_INIT(&src->chunk_list); 586116376Ssimokawa 587116376Ssimokawa fc->crom_src = src; 588116376Ssimokawa fc->crom_root = &fc->crom_src_buf->root; 589116376Ssimokawa} 590116376Ssimokawa 591116376Ssimokawastatic void 592116376Ssimokawafw_reset_crom(struct firewire_comm *fc) 593116376Ssimokawa{ 594116376Ssimokawa struct crom_src_buf *buf; 595116376Ssimokawa struct crom_src *src; 596116376Ssimokawa struct crom_chunk *root; 597116376Ssimokawa 598116376Ssimokawa if (fc->crom_src_buf == NULL) 599116376Ssimokawa fw_init_crom(fc); 600116376Ssimokawa 601116376Ssimokawa buf = fc->crom_src_buf; 602116376Ssimokawa src = fc->crom_src; 603116376Ssimokawa root = fc->crom_root; 604116376Ssimokawa 605116376Ssimokawa STAILQ_INIT(&src->chunk_list); 606116376Ssimokawa 607116376Ssimokawa bzero(root, sizeof(struct crom_chunk)); 608116376Ssimokawa crom_add_chunk(src, NULL, root, 0); 609116376Ssimokawa crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 610116376Ssimokawa /* private company_id */ 611116376Ssimokawa crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 612116376Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project"); 613116376Ssimokawa crom_add_entry(root, CSRKEY_HW, __FreeBSD_version); 614116376Ssimokawa crom_add_simple_text(src, root, &buf->hw, hostname); 615116376Ssimokawa} 616116376Ssimokawa 617116376Ssimokawa/* 618116376Ssimokawa * Called after bus reset. 619116376Ssimokawa */ 620116376Ssimokawavoid 621116376Ssimokawafw_busreset(struct firewire_comm *fc) 622116376Ssimokawa{ 623116376Ssimokawa struct firewire_dev_comm *fdc; 624117350Ssimokawa struct crom_src *src; 625116376Ssimokawa device_t *devlistp; 626117350Ssimokawa void *newrom; 627116376Ssimokawa int i, devcnt; 628116376Ssimokawa 629116376Ssimokawa switch(fc->status){ 630116376Ssimokawa case FWBUSMGRELECT: 631116376Ssimokawa callout_stop(&fc->bmr_callout); 632116376Ssimokawa break; 633116376Ssimokawa default: 634116376Ssimokawa break; 635116376Ssimokawa } 636116376Ssimokawa fc->status = FWBUSRESET; 637116376Ssimokawa fw_reset_csr(fc); 638116376Ssimokawa fw_reset_crom(fc); 639116376Ssimokawa 640113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 641113584Ssimokawa for( i = 0 ; i < devcnt ; i++) 642113584Ssimokawa if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 643113584Ssimokawa fdc = device_get_softc(devlistp[i]); 644113584Ssimokawa if (fdc->post_busreset != NULL) 645113584Ssimokawa fdc->post_busreset(fdc); 646113584Ssimokawa } 647113584Ssimokawa free(devlistp, M_TEMP); 648113584Ssimokawa } 649116376Ssimokawa 650117350Ssimokawa newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO); 651117350Ssimokawa src = &fc->crom_src_buf->src; 652117350Ssimokawa crom_load(src, (u_int32_t *)newrom, CROMSIZE); 653117350Ssimokawa if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) { 654117350Ssimokawa /* bump generation and reload */ 655117350Ssimokawa src->businfo.generation ++; 656117350Ssimokawa /* generation must be between 0x2 and 0xF */ 657117350Ssimokawa if (src->businfo.generation < 2) 658117350Ssimokawa src->businfo.generation ++; 659117350Ssimokawa crom_load(src, (u_int32_t *)newrom, CROMSIZE); 660117350Ssimokawa bcopy(newrom, (void *)fc->config_rom, CROMSIZE); 661117350Ssimokawa } 662117350Ssimokawa free(newrom, M_FW); 663103285Sikob} 664106790Ssimokawa 665103285Sikob/* Call once after reboot */ 666106790Ssimokawavoid fw_init(struct firewire_comm *fc) 667103285Sikob{ 668103285Sikob int i; 669106543Ssimokawa struct csrdir *csrd; 670106543Ssimokawa#ifdef FW_VMACCESS 671103285Sikob struct fw_xfer *xfer; 672103285Sikob struct fw_bind *fwb; 673106543Ssimokawa#endif 674103285Sikob 675103285Sikob fc->max_asyretry = FW_MAXASYRTY; 676103285Sikob 677103285Sikob fc->arq->queued = 0; 678103285Sikob fc->ars->queued = 0; 679103285Sikob fc->atq->queued = 0; 680103285Sikob fc->ats->queued = 0; 681103285Sikob 682103285Sikob fc->arq->buf = NULL; 683103285Sikob fc->ars->buf = NULL; 684103285Sikob fc->atq->buf = NULL; 685103285Sikob fc->ats->buf = NULL; 686103285Sikob 687113584Ssimokawa fc->arq->flag = 0; 688113584Ssimokawa fc->ars->flag = 0; 689113584Ssimokawa fc->atq->flag = 0; 690113584Ssimokawa fc->ats->flag = 0; 691103285Sikob 692103285Sikob STAILQ_INIT(&fc->atq->q); 693103285Sikob STAILQ_INIT(&fc->ats->q); 694103285Sikob 695103285Sikob for( i = 0 ; i < fc->nisodma ; i ++ ){ 696103285Sikob fc->it[i]->queued = 0; 697103285Sikob fc->ir[i]->queued = 0; 698103285Sikob 699103285Sikob fc->it[i]->start = NULL; 700103285Sikob fc->ir[i]->start = NULL; 701103285Sikob 702103285Sikob fc->it[i]->buf = NULL; 703103285Sikob fc->ir[i]->buf = NULL; 704103285Sikob 705103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 706103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 707103285Sikob 708103285Sikob STAILQ_INIT(&fc->it[i]->q); 709103285Sikob STAILQ_INIT(&fc->ir[i]->q); 710103285Sikob 711103285Sikob STAILQ_INIT(&fc->it[i]->binds); 712103285Sikob STAILQ_INIT(&fc->ir[i]->binds); 713103285Sikob } 714103285Sikob 715103285Sikob fc->arq->maxq = FWMAXQUEUE; 716103285Sikob fc->ars->maxq = FWMAXQUEUE; 717103285Sikob fc->atq->maxq = FWMAXQUEUE; 718103285Sikob fc->ats->maxq = FWMAXQUEUE; 719103285Sikob 720103285Sikob for( i = 0 ; i < fc->nisodma ; i++){ 721103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 722103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 723103285Sikob } 724103285Sikob/* Initialize csr registers */ 725103285Sikob fc->topology_map = (struct fw_topology_map *)malloc( 726103285Sikob sizeof(struct fw_topology_map), 727110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 728103285Sikob fc->speed_map = (struct fw_speed_map *)malloc( 729103285Sikob sizeof(struct fw_speed_map), 730110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 731103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 732103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 733103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 734103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 735103285Sikob 736110193Ssimokawa STAILQ_INIT(&fc->devices); 737103285Sikob STAILQ_INIT(&fc->pending); 738103285Sikob 739103285Sikob/* Initialize csr ROM work space */ 740103285Sikob SLIST_INIT(&fc->ongocsr); 741103285Sikob SLIST_INIT(&fc->csrfree); 742103285Sikob for( i = 0 ; i < FWMAXCSRDIR ; i++){ 743110195Ssimokawa csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT); 744103285Sikob if(csrd == NULL) break; 745103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 746103285Sikob } 747103285Sikob 748103285Sikob/* Initialize Async handlers */ 749103285Sikob STAILQ_INIT(&fc->binds); 750103285Sikob for( i = 0 ; i < 0x40 ; i++){ 751103285Sikob STAILQ_INIT(&fc->tlabels[i]); 752103285Sikob } 753103285Sikob 754103285Sikob/* DV depend CSRs see blue book */ 755103285Sikob#if 0 756103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 757103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 758103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 759103285Sikob CSRARC(fc, i + oPCR) = 0x8000007a; 760103285Sikob } 761103285Sikob 762103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 763103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 764103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 765103285Sikob CSRARC(fc, i + iPCR) = 0x0; 766103285Sikob } 767103285Sikob#endif 768103285Sikob 769116376Ssimokawa fc->crom_src_buf = NULL; 770103285Sikob 771106543Ssimokawa#ifdef FW_VMACCESS 772103285Sikob xfer = fw_xfer_alloc(); 773103285Sikob if(xfer == NULL) return; 774103285Sikob 775110195Ssimokawa fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 776103285Sikob if(fwb == NULL){ 777103285Sikob fw_xfer_free(xfer); 778103285Sikob } 779103285Sikob xfer->act.hand = fw_vmaccess; 780103285Sikob xfer->fc = fc; 781103285Sikob xfer->sc = NULL; 782103285Sikob 783103285Sikob fwb->start_hi = 0x2; 784103285Sikob fwb->start_lo = 0; 785103285Sikob fwb->addrlen = 0xffffffff; 786103285Sikob fwb->xfer = xfer; 787103285Sikob fw_bindadd(fc, fwb); 788106543Ssimokawa#endif 789103285Sikob} 790106790Ssimokawa 791120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\ 792120660Ssimokawa ((fwb)->end < (addr))?1:0) 793120660Ssimokawa 794103285Sikob/* 795103285Sikob * To lookup binded process from IEEE1394 address. 796103285Sikob */ 797106813Ssimokawastruct fw_bind * 798120660Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int16_t dest_hi, u_int32_t dest_lo) 799103285Sikob{ 800120660Ssimokawa u_int64_t addr; 801103285Sikob struct fw_bind *tfw; 802120660Ssimokawa 803120660Ssimokawa addr = ((u_int64_t)dest_hi << 32) | dest_lo; 804120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 805120660Ssimokawa if (tfw->act_type != FWACT_NULL && BIND_CMP(addr, tfw) == 0) 806103285Sikob return(tfw); 807103285Sikob return(NULL); 808103285Sikob} 809106790Ssimokawa 810103285Sikob/* 811103285Sikob * To bind IEEE1394 address block to process. 812103285Sikob */ 813106790Ssimokawaint 814106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 815103285Sikob{ 816120660Ssimokawa struct fw_bind *tfw, *prev = NULL; 817120660Ssimokawa 818120660Ssimokawa if (fwb->start > fwb->end) { 819120660Ssimokawa printf("%s: invalid range\n", __FUNCTION__); 820120660Ssimokawa return EINVAL; 821120660Ssimokawa } 822120660Ssimokawa 823120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) { 824120660Ssimokawa if (fwb->end < tfw->start) 825120660Ssimokawa break; 826120660Ssimokawa prev = tfw; 827120660Ssimokawa } 828120660Ssimokawa if (prev == NULL) { 829103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 830103285Sikob goto out; 831103285Sikob } 832120660Ssimokawa if (prev->end < fwb->start) { 833120660Ssimokawa STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 834103285Sikob goto out; 835103285Sikob } 836120660Ssimokawa 837120660Ssimokawa printf("%s: bind failed\n", __FUNCTION__); 838120660Ssimokawa return (EBUSY); 839120660Ssimokawa 840103285Sikobout: 841120660Ssimokawa if (fwb->act_type == FWACT_CH) 842113584Ssimokawa STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist); 843120660Ssimokawa return (0); 844103285Sikob} 845103285Sikob 846103285Sikob/* 847103285Sikob * To free IEEE1394 address block. 848103285Sikob */ 849106790Ssimokawaint 850106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 851103285Sikob{ 852120660Ssimokawa#if 0 853120660Ssimokawa struct fw_xfer *xfer, *next; 854120660Ssimokawa#endif 855120660Ssimokawa struct fw_bind *tfw; 856103285Sikob int s; 857103285Sikob 858103285Sikob s = splfw(); 859120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 860120660Ssimokawa if (tfw == fwb) { 861120660Ssimokawa STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 862120660Ssimokawa goto found; 863120660Ssimokawa } 864120660Ssimokawa 865120660Ssimokawa printf("%s: no such bind\n", __FUNCTION__); 866120660Ssimokawa splx(s); 867120660Ssimokawa return (1); 868120660Ssimokawafound: 869120660Ssimokawa#if 0 870113584Ssimokawa /* shall we do this? */ 871113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 872113584Ssimokawa next = STAILQ_NEXT(xfer, link); 873113584Ssimokawa fw_xfer_free(xfer); 874113584Ssimokawa } 875113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 876120660Ssimokawa#endif 877113584Ssimokawa 878103285Sikob splx(s); 879103285Sikob return 0; 880103285Sikob} 881103285Sikob 882103285Sikob/* 883103285Sikob * To free transaction label. 884103285Sikob */ 885106790Ssimokawastatic void 886106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 887103285Sikob{ 888103285Sikob struct tlabel *tl; 889103285Sikob int s = splfw(); 890103285Sikob 891103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL; 892103285Sikob tl = STAILQ_NEXT(tl, link)){ 893103285Sikob if(tl->xfer == xfer){ 894103285Sikob STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link); 895110195Ssimokawa free(tl, M_FW); 896103285Sikob splx(s); 897103285Sikob return; 898103285Sikob } 899103285Sikob } 900103285Sikob splx(s); 901103285Sikob return; 902103285Sikob} 903106790Ssimokawa 904103285Sikob/* 905103285Sikob * To obtain XFER structure by transaction label. 906103285Sikob */ 907106790Ssimokawastatic struct fw_xfer * 908106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel) 909103285Sikob{ 910103285Sikob struct fw_xfer *xfer; 911103285Sikob struct tlabel *tl; 912103285Sikob int s = splfw(); 913103285Sikob 914103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL; 915103285Sikob tl = STAILQ_NEXT(tl, link)){ 916120660Ssimokawa if(tl->xfer->send.hdr.mode.hdr.dst == node){ 917103285Sikob xfer = tl->xfer; 918103285Sikob splx(s); 919110577Ssimokawa if (firewire_debug > 2) 920110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 921103285Sikob return(xfer); 922103285Sikob } 923103285Sikob } 924110577Ssimokawa if (firewire_debug > 1) 925110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 926103285Sikob splx(s); 927103285Sikob return(NULL); 928103285Sikob} 929106790Ssimokawa 930103285Sikob/* 931103285Sikob * To allocate IEEE1394 XFER structure. 932103285Sikob */ 933106790Ssimokawastruct fw_xfer * 934110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 935103285Sikob{ 936103285Sikob struct fw_xfer *xfer; 937106790Ssimokawa 938110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 939106790Ssimokawa if (xfer == NULL) 940106790Ssimokawa return xfer; 941106790Ssimokawa 942110269Ssimokawa xfer->malloc = type; 943106790Ssimokawa 944103285Sikob return xfer; 945103285Sikob} 946106790Ssimokawa 947113584Ssimokawastruct fw_xfer * 948113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 949113584Ssimokawa{ 950113584Ssimokawa struct fw_xfer *xfer; 951113584Ssimokawa 952113584Ssimokawa xfer = fw_xfer_alloc(type); 953120660Ssimokawa xfer->send.pay_len = send_len; 954120660Ssimokawa xfer->recv.pay_len = recv_len; 955113584Ssimokawa if (xfer == NULL) 956113584Ssimokawa return(NULL); 957120660Ssimokawa if (send_len > 0) { 958120660Ssimokawa xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 959120660Ssimokawa if (xfer->send.payload == NULL) { 960113584Ssimokawa fw_xfer_free(xfer); 961113584Ssimokawa return(NULL); 962113584Ssimokawa } 963113584Ssimokawa } 964120660Ssimokawa if (recv_len > 0) { 965120660Ssimokawa xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 966120660Ssimokawa if (xfer->recv.payload == NULL) { 967120660Ssimokawa if (xfer->send.payload != NULL) 968120660Ssimokawa free(xfer->send.payload, type); 969113584Ssimokawa fw_xfer_free(xfer); 970113584Ssimokawa return(NULL); 971113584Ssimokawa } 972113584Ssimokawa } 973113584Ssimokawa return(xfer); 974113584Ssimokawa} 975113584Ssimokawa 976103285Sikob/* 977103285Sikob * IEEE1394 XFER post process. 978103285Sikob */ 979103285Sikobvoid 980103285Sikobfw_xfer_done(struct fw_xfer *xfer) 981103285Sikob{ 982117716Ssimokawa if (xfer->act.hand == NULL) { 983117716Ssimokawa printf("act.hand == NULL\n"); 984103285Sikob return; 985117716Ssimokawa } 986103285Sikob 987103285Sikob if (xfer->fc->status != FWBUSRESET) 988103285Sikob xfer->act.hand(xfer); 989103285Sikob else { 990103285Sikob printf("fw_xfer_done: pending\n"); 991103285Sikob if (xfer->fc != NULL) 992103285Sikob STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link); 993103285Sikob else 994103285Sikob panic("fw_xfer_done: why xfer->fc is NULL?"); 995103285Sikob } 996103285Sikob} 997103285Sikob 998106790Ssimokawavoid 999113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer) 1000103285Sikob{ 1001103285Sikob int s; 1002113584Ssimokawa 1003103285Sikob if(xfer == NULL ) return; 1004103285Sikob if(xfer->state == FWXF_INQ){ 1005103285Sikob printf("fw_xfer_free FWXF_INQ\n"); 1006103285Sikob s = splfw(); 1007103285Sikob STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1008103285Sikob xfer->q->queued --; 1009103285Sikob splx(s); 1010103285Sikob } 1011113584Ssimokawa if (xfer->fc != NULL) { 1012114729Ssimokawa#if 1 1013113584Ssimokawa if(xfer->state == FWXF_START) 1014114729Ssimokawa /* 1015114729Ssimokawa * This could happen if: 1016114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 1017114729Ssimokawa * 2. firewire_watch() is called. 1018114729Ssimokawa */ 1019114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 1020103285Sikob#endif 1021113584Ssimokawa fw_tl_free(xfer->fc, xfer); 1022103285Sikob } 1023113584Ssimokawa xfer->state = FWXF_INIT; 1024113584Ssimokawa xfer->resp = 0; 1025113584Ssimokawa xfer->retry = 0; 1026113584Ssimokawa} 1027113584Ssimokawa/* 1028113584Ssimokawa * To free IEEE1394 XFER structure. 1029113584Ssimokawa */ 1030113584Ssimokawavoid 1031120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer) 1032113584Ssimokawa{ 1033120660Ssimokawa if (xfer == NULL) { 1034120660Ssimokawa printf("%s: xfer == NULL\n", __FUNCTION__); 1035120660Ssimokawa return; 1036120660Ssimokawa } 1037113584Ssimokawa fw_xfer_unload(xfer); 1038120660Ssimokawa if(xfer->send.payload != NULL){ 1039120660Ssimokawa free(xfer->send.payload, xfer->malloc); 1040103285Sikob } 1041120660Ssimokawa if(xfer->recv.payload != NULL){ 1042120660Ssimokawa free(xfer->recv.payload, xfer->malloc); 1043103285Sikob } 1044110269Ssimokawa free(xfer, xfer->malloc); 1045103285Sikob} 1046103285Sikob 1047120660Ssimokawavoid 1048120660Ssimokawafw_xfer_free( struct fw_xfer* xfer) 1049120660Ssimokawa{ 1050120660Ssimokawa if (xfer == NULL) { 1051120660Ssimokawa printf("%s: xfer == NULL\n", __FUNCTION__); 1052120660Ssimokawa return; 1053120660Ssimokawa } 1054120660Ssimokawa fw_xfer_unload(xfer); 1055120660Ssimokawa free(xfer, xfer->malloc); 1056120660Ssimokawa} 1057120660Ssimokawa 1058120660Ssimokawavoid 1059110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 1060103285Sikob{ 1061103285Sikob#if 0 1062110072Ssimokawa printf("asyreq done state=%d resp=%d\n", 1063103285Sikob xfer->state, xfer->resp); 1064103285Sikob#endif 1065103285Sikob fw_xfer_free(xfer); 1066103285Sikob} 1067103285Sikob 1068103285Sikob/* 1069103285Sikob * To configure PHY. 1070103285Sikob */ 1071103285Sikobstatic void 1072103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1073103285Sikob{ 1074103285Sikob struct fw_xfer *xfer; 1075103285Sikob struct fw_pkt *fp; 1076103285Sikob 1077103285Sikob fc->status = FWBUSPHYCONF; 1078103285Sikob 1079120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1080113584Ssimokawa if (xfer == NULL) 1081113584Ssimokawa return; 1082103285Sikob xfer->fc = fc; 1083103285Sikob xfer->retry_req = fw_asybusy; 1084110072Ssimokawa xfer->act.hand = fw_asy_callback_free; 1085103285Sikob 1086120660Ssimokawa fp = &xfer->send.hdr; 1087103285Sikob fp->mode.ld[1] = 0; 1088103285Sikob if (root_node >= 0) 1089113584Ssimokawa fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1090103285Sikob if (gap_count >= 0) 1091113584Ssimokawa fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1092103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 1093103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 1094103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 1095103285Sikob 1096107653Ssimokawa if (firewire_debug) 1097107653Ssimokawa printf("send phy_config root_node=%d gap_count=%d\n", 1098103285Sikob root_node, gap_count); 1099103285Sikob fw_asyreq(fc, -1, xfer); 1100103285Sikob} 1101103285Sikob 1102103285Sikob#if 0 1103103285Sikob/* 1104103285Sikob * Dump self ID. 1105103285Sikob */ 1106103285Sikobstatic void 1107103285Sikobfw_print_sid(u_int32_t sid) 1108103285Sikob{ 1109103285Sikob union fw_self_id *s; 1110103285Sikob s = (union fw_self_id *) &sid; 1111103285Sikob printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1112103285Sikob " p0:%d p1:%d p2:%d i:%d m:%d\n", 1113103285Sikob s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1114103285Sikob s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1115103285Sikob s->p0.power_class, s->p0.port0, s->p0.port1, 1116103285Sikob s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1117103285Sikob} 1118103285Sikob#endif 1119103285Sikob 1120103285Sikob/* 1121103285Sikob * To receive self ID. 1122103285Sikob */ 1123113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len) 1124103285Sikob{ 1125113584Ssimokawa u_int32_t *p; 1126103285Sikob union fw_self_id *self_id; 1127103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1128103285Sikob 1129103285Sikob fc->sid_cnt = len /(sizeof(u_int32_t) * 2); 1130103285Sikob fc->status = FWBUSINIT; 1131103285Sikob fc->max_node = fc->nodeid & 0x3f; 1132103285Sikob CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16; 1133103285Sikob fc->status = FWBUSCYMELECT; 1134103285Sikob fc->topology_map->crc_len = 2; 1135103285Sikob fc->topology_map->generation ++; 1136103285Sikob fc->topology_map->self_id_count = 0; 1137103285Sikob fc->topology_map->node_count = 0; 1138103285Sikob fc->speed_map->generation ++; 1139103285Sikob fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1140103285Sikob self_id = &fc->topology_map->self_id[0]; 1141103285Sikob for(i = 0; i < fc->sid_cnt; i ++){ 1142103285Sikob if (sid[1] != ~sid[0]) { 1143103285Sikob printf("fw_sidrcv: invalid self-id packet\n"); 1144103285Sikob sid += 2; 1145103285Sikob continue; 1146103285Sikob } 1147103285Sikob *self_id = *((union fw_self_id *)sid); 1148103285Sikob fc->topology_map->crc_len++; 1149103285Sikob if(self_id->p0.sequel == 0){ 1150103285Sikob fc->topology_map->node_count ++; 1151103285Sikob c_port = 0; 1152103285Sikob#if 0 1153103285Sikob fw_print_sid(sid[0]); 1154103285Sikob#endif 1155103285Sikob node = self_id->p0.phy_id; 1156103285Sikob if(fc->max_node < node){ 1157103285Sikob fc->max_node = self_id->p0.phy_id; 1158103285Sikob } 1159103285Sikob /* XXX I'm not sure this is the right speed_map */ 1160103285Sikob fc->speed_map->speed[node][node] 1161103285Sikob = self_id->p0.phy_speed; 1162103285Sikob for (j = 0; j < node; j ++) { 1163103285Sikob fc->speed_map->speed[j][node] 1164103285Sikob = fc->speed_map->speed[node][j] 1165103285Sikob = min(fc->speed_map->speed[j][j], 1166103285Sikob self_id->p0.phy_speed); 1167103285Sikob } 1168103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1169103285Sikob (self_id->p0.link_active && self_id->p0.contender)) { 1170103285Sikob fc->irm = self_id->p0.phy_id; 1171103285Sikob } 1172103285Sikob if(self_id->p0.port0 >= 0x2){ 1173103285Sikob c_port++; 1174103285Sikob } 1175103285Sikob if(self_id->p0.port1 >= 0x2){ 1176103285Sikob c_port++; 1177103285Sikob } 1178103285Sikob if(self_id->p0.port2 >= 0x2){ 1179103285Sikob c_port++; 1180103285Sikob } 1181103285Sikob } 1182103285Sikob if(c_port > 2){ 1183103285Sikob i_branch += (c_port - 2); 1184103285Sikob } 1185103285Sikob sid += 2; 1186103285Sikob self_id++; 1187103285Sikob fc->topology_map->self_id_count ++; 1188103285Sikob } 1189108655Ssimokawa device_printf(fc->bdev, "%d nodes", fc->max_node + 1); 1190103285Sikob /* CRC */ 1191103285Sikob fc->topology_map->crc = fw_crc16( 1192103285Sikob (u_int32_t *)&fc->topology_map->generation, 1193103285Sikob fc->topology_map->crc_len * 4); 1194103285Sikob fc->speed_map->crc = fw_crc16( 1195103285Sikob (u_int32_t *)&fc->speed_map->generation, 1196103285Sikob fc->speed_map->crc_len * 4); 1197103285Sikob /* byteswap and copy to CSR */ 1198103285Sikob p = (u_int32_t *)fc->topology_map; 1199103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1200103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1201103285Sikob p = (u_int32_t *)fc->speed_map; 1202103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1203103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1204103285Sikob /* don't byte-swap u_int8_t array */ 1205103285Sikob bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1206103285Sikob 1207103285Sikob fc->max_hop = fc->max_node - i_branch; 1208103285Sikob printf(", maxhop <= %d", fc->max_hop); 1209103285Sikob 1210103285Sikob if(fc->irm == -1 ){ 1211103285Sikob printf(", Not found IRM capable node"); 1212103285Sikob }else{ 1213103285Sikob printf(", cable IRM = %d", fc->irm); 1214103285Sikob if (fc->irm == fc->nodeid) 1215110016Ssimokawa printf(" (me)"); 1216103285Sikob } 1217110016Ssimokawa printf("\n"); 1218103285Sikob 1219109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1220114909Ssimokawa if (fc->irm == fc->nodeid) { 1221103285Sikob fc->status = FWBUSMGRDONE; 1222103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1223114909Ssimokawa fw_bmr(fc); 1224109736Ssimokawa } else { 1225103285Sikob fc->status = FWBUSMGRELECT; 1226110193Ssimokawa callout_reset(&fc->bmr_callout, hz/8, 1227110193Ssimokawa (void *)fw_try_bmr, (void *)fc); 1228103285Sikob } 1229114909Ssimokawa } else 1230103285Sikob fc->status = FWBUSMGRDONE; 1231114909Ssimokawa 1232108853Ssimokawa callout_reset(&fc->busprobe_callout, hz/4, 1233108853Ssimokawa (void *)fw_bus_probe, (void *)fc); 1234103285Sikob} 1235106790Ssimokawa 1236103285Sikob/* 1237103285Sikob * To probe devices on the IEEE1394 bus. 1238103285Sikob */ 1239106790Ssimokawastatic void 1240106790Ssimokawafw_bus_probe(struct firewire_comm *fc) 1241103285Sikob{ 1242103285Sikob int s; 1243120850Ssimokawa struct fw_device *fwdev; 1244103285Sikob 1245103285Sikob s = splfw(); 1246103285Sikob fc->status = FWBUSEXPLORE; 1247103285Sikob fc->retry_count = 0; 1248103285Sikob 1249120850Ssimokawa /* Invalidate all devices, just after bus reset. */ 1250120850Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1251110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1252103285Sikob fwdev->status = FWDEVINVAL; 1253103285Sikob fwdev->rcnt = 0; 1254103285Sikob } 1255120850Ssimokawa 1256103285Sikob fc->ongonode = 0; 1257103285Sikob fc->ongoaddr = CSRROMOFF; 1258103285Sikob fc->ongodev = NULL; 1259103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1260103285Sikob fw_bus_explore(fc); 1261103285Sikob splx(s); 1262103285Sikob} 1263106790Ssimokawa 1264103285Sikob/* 1265103285Sikob * To collect device informations on the IEEE1394 bus. 1266103285Sikob */ 1267106790Ssimokawastatic void 1268106790Ssimokawafw_bus_explore(struct firewire_comm *fc ) 1269103285Sikob{ 1270103285Sikob int err = 0; 1271110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1272103285Sikob u_int32_t addr; 1273103285Sikob struct fw_xfer *xfer; 1274103285Sikob struct fw_pkt *fp; 1275103285Sikob 1276103285Sikob if(fc->status != FWBUSEXPLORE) 1277103285Sikob return; 1278103285Sikob 1279103285Sikobloop: 1280103285Sikob if(fc->ongonode == fc->nodeid) fc->ongonode++; 1281103285Sikob 1282103285Sikob if(fc->ongonode > fc->max_node) goto done; 1283103285Sikob if(fc->ongonode >= 0x3f) goto done; 1284103285Sikob 1285103285Sikob /* check link */ 1286103285Sikob /* XXX we need to check phy_id first */ 1287103285Sikob if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) { 1288110577Ssimokawa if (firewire_debug) 1289110577Ssimokawa printf("node%d: link down\n", fc->ongonode); 1290103285Sikob fc->ongonode++; 1291103285Sikob goto loop; 1292103285Sikob } 1293103285Sikob 1294103285Sikob if(fc->ongoaddr <= CSRROMOFF && 1295103285Sikob fc->ongoeui.hi == 0xffffffff && 1296103285Sikob fc->ongoeui.lo == 0xffffffff ){ 1297103285Sikob fc->ongoaddr = CSRROMOFF; 1298103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1299103285Sikob }else if(fc->ongoeui.hi == 0xffffffff ){ 1300103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1301103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1302103285Sikob }else if(fc->ongoeui.lo == 0xffffffff ){ 1303103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1304103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1305103285Sikob }else if(fc->ongodev == NULL){ 1306110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1307110193Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui)) 1308103285Sikob break; 1309103285Sikob if(fwdev != NULL){ 1310103285Sikob fwdev->dst = fc->ongonode; 1311117350Ssimokawa fwdev->status = FWDEVINIT; 1312117350Ssimokawa fc->ongodev = fwdev; 1313103285Sikob fc->ongoaddr = CSRROMOFF; 1314117350Ssimokawa addr = 0xf0000000 | fc->ongoaddr; 1315117350Ssimokawa goto dorequest; 1316103285Sikob } 1317115787Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, 1318115787Ssimokawa M_NOWAIT | M_ZERO); 1319103285Sikob if(fwdev == NULL) 1320103285Sikob return; 1321106810Ssimokawa fwdev->fc = fc; 1322103285Sikob fwdev->rommax = 0; 1323103285Sikob fwdev->dst = fc->ongonode; 1324103285Sikob fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo; 1325103285Sikob fwdev->status = FWDEVINIT; 1326103285Sikob fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode]; 1327103285Sikob 1328110179Ssimokawa pfwdev = NULL; 1329110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1330110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1331110179Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1332110179Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1333110179Ssimokawa break; 1334110179Ssimokawa pfwdev = tfwdev; 1335103285Sikob } 1336110179Ssimokawa if (pfwdev == NULL) 1337110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1338110179Ssimokawa else 1339110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1340103285Sikob 1341108655Ssimokawa device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1342107653Ssimokawa linkspeed[fwdev->speed], 1343107653Ssimokawa fc->ongoeui.hi, fc->ongoeui.lo); 1344103285Sikob 1345103285Sikob fc->ongodev = fwdev; 1346103285Sikob fc->ongoaddr = CSRROMOFF; 1347103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1348103285Sikob }else{ 1349103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1350103285Sikob } 1351117350Ssimokawadorequest: 1352103285Sikob#if 0 1353103285Sikob xfer = asyreqq(fc, FWSPD_S100, 0, 0, 1354103285Sikob ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr, 1355103285Sikob fw_bus_explore_callback); 1356103285Sikob if(xfer == NULL) goto done; 1357103285Sikob#else 1358120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1359103285Sikob if(xfer == NULL){ 1360103285Sikob goto done; 1361103285Sikob } 1362120660Ssimokawa xfer->send.spd = 0; 1363120660Ssimokawa fp = &xfer->send.hdr; 1364113584Ssimokawa fp->mode.rreqq.dest_hi = 0xffff; 1365103285Sikob fp->mode.rreqq.tlrt = 0; 1366103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1367103285Sikob fp->mode.rreqq.pri = 0; 1368103285Sikob fp->mode.rreqq.src = 0; 1369120660Ssimokawa fp->mode.rreqq.dst = FWLOCALBUS | fc->ongonode; 1370113584Ssimokawa fp->mode.rreqq.dest_lo = addr; 1371103285Sikob xfer->act.hand = fw_bus_explore_callback; 1372103285Sikob 1373110577Ssimokawa if (firewire_debug) 1374110577Ssimokawa printf("node%d: explore addr=0x%x\n", 1375110577Ssimokawa fc->ongonode, fc->ongoaddr); 1376103285Sikob err = fw_asyreq(fc, -1, xfer); 1377103285Sikob if(err){ 1378103285Sikob fw_xfer_free( xfer); 1379103285Sikob return; 1380103285Sikob } 1381103285Sikob#endif 1382103285Sikob return; 1383103285Sikobdone: 1384103285Sikob /* fw_attach_devs */ 1385103285Sikob fc->status = FWBUSEXPDONE; 1386107653Ssimokawa if (firewire_debug) 1387107653Ssimokawa printf("bus_explore done\n"); 1388103285Sikob fw_attach_dev(fc); 1389103285Sikob return; 1390103285Sikob 1391103285Sikob} 1392106790Ssimokawa 1393103285Sikob/* Portable Async. request read quad */ 1394106790Ssimokawastruct fw_xfer * 1395106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt, 1396105620Ssimokawa u_int32_t addr_hi, u_int32_t addr_lo, 1397105620Ssimokawa void (*hand) __P((struct fw_xfer*))) 1398103285Sikob{ 1399103285Sikob struct fw_xfer *xfer; 1400103285Sikob struct fw_pkt *fp; 1401103285Sikob int err; 1402103285Sikob 1403120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1404113584Ssimokawa if (xfer == NULL) 1405103285Sikob return NULL; 1406113584Ssimokawa 1407120660Ssimokawa xfer->send.spd = spd; /* XXX:min(spd, fc->spd) */ 1408120660Ssimokawa fp = &xfer->send.hdr; 1409113584Ssimokawa fp->mode.rreqq.dest_hi = addr_hi & 0xffff; 1410103285Sikob if(tl & FWP_TL_VALID){ 1411103285Sikob fp->mode.rreqq.tlrt = (tl & 0x3f) << 2; 1412103285Sikob }else{ 1413103285Sikob fp->mode.rreqq.tlrt = 0; 1414103285Sikob } 1415103285Sikob fp->mode.rreqq.tlrt |= rt & 0x3; 1416103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1417103285Sikob fp->mode.rreqq.pri = 0; 1418103285Sikob fp->mode.rreqq.src = 0; 1419120660Ssimokawa fp->mode.rreqq.dst = addr_hi >> 16; 1420113584Ssimokawa fp->mode.rreqq.dest_lo = addr_lo; 1421103285Sikob xfer->act.hand = hand; 1422103285Sikob 1423103285Sikob err = fw_asyreq(fc, -1, xfer); 1424103285Sikob if(err){ 1425103285Sikob fw_xfer_free( xfer); 1426103285Sikob return NULL; 1427103285Sikob } 1428103285Sikob return xfer; 1429103285Sikob} 1430106790Ssimokawa 1431103285Sikob/* 1432103285Sikob * Callback for the IEEE1394 bus information collection. 1433103285Sikob */ 1434106790Ssimokawastatic void 1435106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer) 1436106790Ssimokawa{ 1437103285Sikob struct firewire_comm *fc; 1438103285Sikob struct fw_pkt *sfp,*rfp; 1439103285Sikob struct csrhdr *chdr; 1440103285Sikob struct csrdir *csrd; 1441103285Sikob struct csrreg *csrreg; 1442103285Sikob u_int32_t offset; 1443103285Sikob 1444103285Sikob 1445110577Ssimokawa if(xfer == NULL) { 1446110577Ssimokawa printf("xfer == NULL\n"); 1447110577Ssimokawa return; 1448110577Ssimokawa } 1449103285Sikob fc = xfer->fc; 1450110577Ssimokawa 1451110577Ssimokawa if (firewire_debug) 1452110577Ssimokawa printf("node%d: callback addr=0x%x\n", 1453110577Ssimokawa fc->ongonode, fc->ongoaddr); 1454110577Ssimokawa 1455103285Sikob if(xfer->resp != 0){ 1456110577Ssimokawa printf("node%d: resp=%d addr=0x%x\n", 1457110577Ssimokawa fc->ongonode, xfer->resp, fc->ongoaddr); 1458114284Ssimokawa goto errnode; 1459103285Sikob } 1460103285Sikob 1461120660Ssimokawa sfp = &xfer->send.hdr; 1462120660Ssimokawa rfp = &xfer->recv.hdr; 1463103285Sikob#if 0 1464103285Sikob { 1465103285Sikob u_int32_t *qld; 1466103285Sikob int i; 1467103285Sikob qld = (u_int32_t *)xfer->recv.buf; 1468103285Sikob printf("len:%d\n", xfer->recv.len); 1469103285Sikob for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){ 1470113584Ssimokawa printf("0x%08x ", rfp->mode.ld[i/4]); 1471103285Sikob if((i % 16) == 15) printf("\n"); 1472103285Sikob } 1473103285Sikob if((i % 16) != 15) printf("\n"); 1474103285Sikob } 1475103285Sikob#endif 1476103285Sikob if(fc->ongodev == NULL){ 1477113584Ssimokawa if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){ 1478103285Sikob rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data); 1479103285Sikob chdr = (struct csrhdr *)(&rfp->mode.rresq.data); 1480110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */ 1481103285Sikob if(chdr->info_len == 1){ 1482110577Ssimokawa if (firewire_debug) 1483110577Ssimokawa printf("node%d: minimal config\n", 1484110577Ssimokawa fc->ongonode); 1485103285Sikob goto nextnode; 1486103285Sikob }else{ 1487103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1488103285Sikob } 1489113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){ 1490103285Sikob fc->ongoeui.hi = ntohl(rfp->mode.rresq.data); 1491103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1492113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){ 1493103285Sikob fc->ongoeui.lo = ntohl(rfp->mode.rresq.data); 1494110577Ssimokawa if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) { 1495110577Ssimokawa if (firewire_debug) 1496110577Ssimokawa printf("node%d: eui64 is zero.\n", 1497110577Ssimokawa fc->ongonode); 1498103285Sikob goto nextnode; 1499110577Ssimokawa } 1500103285Sikob fc->ongoaddr = CSRROMOFF; 1501103285Sikob } 1502103285Sikob }else{ 1503117350Ssimokawa if (fc->ongoaddr == CSRROMOFF && 1504117350Ssimokawa fc->ongodev->csrrom[0] == ntohl(rfp->mode.rresq.data)) { 1505117350Ssimokawa fc->ongodev->status = FWDEVATTACHED; 1506117350Ssimokawa goto nextnode; 1507117350Ssimokawa } 1508103285Sikob fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data); 1509103285Sikob if(fc->ongoaddr > fc->ongodev->rommax){ 1510103285Sikob fc->ongodev->rommax = fc->ongoaddr; 1511103285Sikob } 1512103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1513103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1514103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1515103285Sikob offset = CSRROMOFF; 1516103285Sikob }else{ 1517103285Sikob chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]; 1518103285Sikob offset = csrd->off; 1519103285Sikob } 1520103285Sikob if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){ 1521103285Sikob csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4]; 1522103285Sikob if( csrreg->key == 0x81 || csrreg->key == 0xd1){ 1523103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1524103285Sikob if(csrd == NULL){ 1525103285Sikob goto nextnode; 1526103285Sikob }else{ 1527103285Sikob csrd->ongoaddr = fc->ongoaddr; 1528103285Sikob fc->ongoaddr += csrreg->val * 4; 1529103285Sikob csrd->off = fc->ongoaddr; 1530103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1531103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1532103285Sikob goto nextaddr; 1533103285Sikob } 1534103285Sikob } 1535103285Sikob } 1536103285Sikob fc->ongoaddr += 4; 1537103285Sikob if(((fc->ongoaddr - offset)/4 > chdr->crc_len) && 1538103285Sikob (fc->ongodev->rommax < 0x414)){ 1539103285Sikob if(fc->ongodev->rommax <= 0x414){ 1540103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1541103285Sikob if(csrd == NULL) goto nextnode; 1542103285Sikob csrd->off = fc->ongoaddr; 1543103285Sikob csrd->ongoaddr = fc->ongoaddr; 1544103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1545103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1546103285Sikob } 1547103285Sikob goto nextaddr; 1548103285Sikob } 1549103285Sikob 1550103285Sikob while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){ 1551103285Sikob if(csrd == NULL){ 1552103285Sikob goto nextnode; 1553103285Sikob }; 1554103285Sikob fc->ongoaddr = csrd->ongoaddr + 4; 1555103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1556103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1557103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1558103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1559103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1560103285Sikob offset = CSRROMOFF; 1561103285Sikob }else{ 1562103285Sikob chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]); 1563103285Sikob offset = csrd->off; 1564103285Sikob } 1565103285Sikob } 1566103285Sikob if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){ 1567103285Sikob goto nextnode; 1568103285Sikob } 1569103285Sikob } 1570103285Sikobnextaddr: 1571103285Sikob fw_xfer_free( xfer); 1572103285Sikob fw_bus_explore(fc); 1573103285Sikob return; 1574114284Ssimokawaerrnode: 1575114284Ssimokawa fc->retry_count++; 1576114284Ssimokawa if (fc->ongodev != NULL) 1577114284Ssimokawa fc->ongodev->status = FWDEVINVAL; 1578103285Sikobnextnode: 1579103285Sikob fw_xfer_free( xfer); 1580103285Sikob fc->ongonode++; 1581103285Sikob/* housekeeping work space */ 1582103285Sikob fc->ongoaddr = CSRROMOFF; 1583103285Sikob fc->ongodev = NULL; 1584103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1585103285Sikob while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){ 1586103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1587103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1588103285Sikob } 1589103285Sikob fw_bus_explore(fc); 1590103285Sikob return; 1591103285Sikob} 1592103285Sikob 1593103285Sikob/* 1594103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1595103285Sikob */ 1596106815Ssimokawastatic void 1597106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1598103285Sikob{ 1599120850Ssimokawa struct fw_device *fwdev, *next; 1600103285Sikob struct fw_xfer *xfer; 1601103285Sikob int i, err; 1602103285Sikob device_t *devlistp; 1603103285Sikob int devcnt; 1604103285Sikob struct firewire_dev_comm *fdc; 1605103285Sikob 1606120850Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1607120850Ssimokawa next = STAILQ_NEXT(fwdev, link); 1608120850Ssimokawa if (fwdev->status == FWDEVINIT) { 1609114069Ssimokawa fwdev->status = FWDEVATTACHED; 1610120850Ssimokawa } else if (fwdev->status == FWDEVINVAL) { 1611120850Ssimokawa fwdev->rcnt ++; 1612120850Ssimokawa if (fwdev->rcnt > hold_count) { 1613120850Ssimokawa /* 1614120850Ssimokawa * Remove devices which have not been seen 1615120850Ssimokawa * for a while. 1616120850Ssimokawa */ 1617120850Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, 1618120850Ssimokawa link); 1619120850Ssimokawa free(fwdev, M_FW); 1620120850Ssimokawa } 1621120850Ssimokawa } 1622120850Ssimokawa } 1623103285Sikob 1624108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1625103285Sikob if( err != 0 ) 1626103285Sikob return; 1627103285Sikob for( i = 0 ; i < devcnt ; i++){ 1628103285Sikob if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1629103285Sikob fdc = device_get_softc(devlistp[i]); 1630103285Sikob if (fdc->post_explore != NULL) 1631103285Sikob fdc->post_explore(fdc); 1632103285Sikob } 1633103285Sikob } 1634103285Sikob free(devlistp, M_TEMP); 1635103285Sikob 1636103285Sikob /* call pending handlers */ 1637103285Sikob i = 0; 1638103285Sikob while ((xfer = STAILQ_FIRST(&fc->pending))) { 1639103285Sikob STAILQ_REMOVE_HEAD(&fc->pending, link); 1640103285Sikob i++; 1641103285Sikob if (xfer->act.hand) 1642103285Sikob xfer->act.hand(xfer); 1643103285Sikob } 1644103285Sikob if (i > 0) 1645103285Sikob printf("fw_attach_dev: %d pending handlers called\n", i); 1646103285Sikob if (fc->retry_count > 0) { 1647111040Ssimokawa printf("probe failed for %d node\n", fc->retry_count); 1648111040Ssimokawa#if 0 1649110193Ssimokawa callout_reset(&fc->retry_probe_callout, hz*2, 1650110193Ssimokawa (void *)fc->ibr, (void *)fc); 1651111040Ssimokawa#endif 1652103285Sikob } 1653103285Sikob return; 1654103285Sikob} 1655106815Ssimokawa 1656103285Sikob/* 1657103285Sikob * To allocate uniq transaction label. 1658103285Sikob */ 1659106815Ssimokawastatic int 1660106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1661103285Sikob{ 1662103285Sikob u_int i; 1663103285Sikob struct tlabel *tl, *tmptl; 1664103285Sikob int s; 1665103285Sikob static u_int32_t label = 0; 1666103285Sikob 1667103285Sikob s = splfw(); 1668103285Sikob for( i = 0 ; i < 0x40 ; i ++){ 1669103285Sikob label = (label + 1) & 0x3f; 1670103285Sikob for(tmptl = STAILQ_FIRST(&fc->tlabels[label]); 1671103285Sikob tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){ 1672120660Ssimokawa if (tmptl->xfer->send.hdr.mode.hdr.dst == 1673120660Ssimokawa xfer->send.hdr.mode.hdr.dst) 1674120660Ssimokawa break; 1675103285Sikob } 1676103285Sikob if(tmptl == NULL) { 1677110195Ssimokawa tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT); 1678103285Sikob if (tl == NULL) { 1679103285Sikob splx(s); 1680103285Sikob return (-1); 1681103285Sikob } 1682103285Sikob tl->xfer = xfer; 1683103285Sikob STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link); 1684103285Sikob splx(s); 1685110577Ssimokawa if (firewire_debug > 1) 1686110577Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", 1687120660Ssimokawa xfer->send.hdr.mode.hdr.dst, label); 1688103285Sikob return(label); 1689103285Sikob } 1690103285Sikob } 1691103285Sikob splx(s); 1692103285Sikob 1693103285Sikob printf("fw_get_tlabel: no free tlabel\n"); 1694103285Sikob return(-1); 1695103285Sikob} 1696106815Ssimokawa 1697113584Ssimokawastatic void 1698120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb) 1699113584Ssimokawa{ 1700120660Ssimokawa struct fw_pkt *pkt; 1701120660Ssimokawa u_char *p; 1702120660Ssimokawa struct tcode_info *tinfo; 1703120660Ssimokawa u_int res, i, len, plen; 1704113584Ssimokawa 1705120660Ssimokawa rb->xfer->recv.spd -= rb->spd; 1706120660Ssimokawa 1707120660Ssimokawa pkt = (struct fw_pkt *)rb->vec->iov_base; 1708120660Ssimokawa tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 1709120660Ssimokawa 1710120660Ssimokawa /* Copy header */ 1711120660Ssimokawa p = (u_char *)&rb->xfer->recv.hdr; 1712120660Ssimokawa bcopy(rb->vec->iov_base, p, tinfo->hdr_len); 1713120660Ssimokawa (u_char *)rb->vec->iov_base += tinfo->hdr_len; 1714120660Ssimokawa rb->vec->iov_len -= tinfo->hdr_len; 1715120660Ssimokawa 1716120660Ssimokawa /* Copy payload */ 1717120660Ssimokawa p = (u_char *)rb->xfer->recv.payload; 1718120660Ssimokawa res = rb->xfer->recv.pay_len; 1719120660Ssimokawa 1720120660Ssimokawa /* special handling for RRESQ */ 1721120660Ssimokawa if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 1722120660Ssimokawa p != NULL && res >= sizeof(u_int32_t)) { 1723120660Ssimokawa *(u_int32_t *)p = pkt->mode.rresq.data; 1724120660Ssimokawa rb->xfer->recv.pay_len = sizeof(u_int32_t); 1725120660Ssimokawa return; 1726120660Ssimokawa } 1727120660Ssimokawa 1728120660Ssimokawa if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 1729120660Ssimokawa return; 1730120660Ssimokawa 1731120660Ssimokawa plen = pkt->mode.rresb.len; 1732120660Ssimokawa 1733120660Ssimokawa for (i = 0; i < rb->nvec; i++, rb->vec++) { 1734120660Ssimokawa len = MIN(rb->vec->iov_len, plen); 1735113584Ssimokawa if (res < len) { 1736113584Ssimokawa printf("rcv buffer(%d) is %d bytes short.\n", 1737120660Ssimokawa rb->xfer->recv.pay_len, len - res); 1738113584Ssimokawa len = res; 1739113584Ssimokawa } 1740120660Ssimokawa bcopy(rb->vec->iov_base, p, len); 1741113584Ssimokawa p += len; 1742113584Ssimokawa res -= len; 1743120660Ssimokawa plen -= len; 1744120660Ssimokawa if (res == 0 || plen == 0) 1745113584Ssimokawa break; 1746113584Ssimokawa } 1747120660Ssimokawa rb->xfer->recv.pay_len -= res; 1748120660Ssimokawa 1749113584Ssimokawa} 1750113584Ssimokawa 1751103285Sikob/* 1752103285Sikob * Generic packet receving process. 1753103285Sikob */ 1754106815Ssimokawavoid 1755120660Ssimokawafw_rcv(struct fw_rcv_buf *rb) 1756103285Sikob{ 1757103285Sikob struct fw_pkt *fp, *resfp; 1758103285Sikob struct fw_bind *bind; 1759113584Ssimokawa int tcode, s; 1760113584Ssimokawa int i, len, oldstate; 1761103285Sikob#if 0 1762103285Sikob { 1763103285Sikob u_int32_t *qld; 1764103285Sikob int i; 1765103285Sikob qld = (u_int32_t *)buf; 1766103285Sikob printf("spd %d len:%d\n", spd, len); 1767103285Sikob for( i = 0 ; i <= len && i < 32; i+= 4){ 1768103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1769103285Sikob if((i % 16) == 15) printf("\n"); 1770103285Sikob } 1771103285Sikob if((i % 16) != 15) printf("\n"); 1772103285Sikob } 1773103285Sikob#endif 1774120660Ssimokawa fp = (struct fw_pkt *)rb->vec[0].iov_base; 1775113584Ssimokawa tcode = fp->mode.common.tcode; 1776113584Ssimokawa switch (tcode) { 1777103285Sikob case FWTCODE_WRES: 1778103285Sikob case FWTCODE_RRESQ: 1779103285Sikob case FWTCODE_RRESB: 1780103285Sikob case FWTCODE_LRES: 1781120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1782103285Sikob fp->mode.hdr.tlrt >> 2); 1783120660Ssimokawa if(rb->xfer == NULL) { 1784103285Sikob printf("fw_rcv: unknown response " 1785110891Ssimokawa "tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n", 1786113584Ssimokawa tcode, 1787113584Ssimokawa fp->mode.hdr.src, 1788103285Sikob fp->mode.hdr.tlrt >> 2, 1789103285Sikob fp->mode.hdr.tlrt & 3, 1790103285Sikob fp->mode.rresq.data); 1791103285Sikob#if 1 1792103285Sikob printf("try ad-hoc work around!!\n"); 1793120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1794103285Sikob (fp->mode.hdr.tlrt >> 2)^3); 1795120660Ssimokawa if (rb->xfer == NULL) { 1796103285Sikob printf("no use...\n"); 1797103285Sikob goto err; 1798103285Sikob } 1799103285Sikob#else 1800103285Sikob goto err; 1801103285Sikob#endif 1802103285Sikob } 1803120660Ssimokawa fw_rcv_copy(rb); 1804120660Ssimokawa if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 1805120660Ssimokawa rb->xfer->resp = EIO; 1806120660Ssimokawa else 1807120660Ssimokawa rb->xfer->resp = 0; 1808113584Ssimokawa /* make sure the packet is drained in AT queue */ 1809120660Ssimokawa oldstate = rb->xfer->state; 1810120660Ssimokawa rb->xfer->state = FWXF_RCVD; 1811113584Ssimokawa switch (oldstate) { 1812113584Ssimokawa case FWXF_SENT: 1813120660Ssimokawa fw_xfer_done(rb->xfer); 1814103285Sikob break; 1815113584Ssimokawa case FWXF_START: 1816119289Ssimokawa#if 0 1817113584Ssimokawa if (firewire_debug) 1818120660Ssimokawa printf("not sent yet tl=%x\n", rb->xfer->tl); 1819119289Ssimokawa#endif 1820113584Ssimokawa break; 1821103285Sikob default: 1822120660Ssimokawa printf("unexpected state %d\n", rb->xfer->state); 1823103285Sikob } 1824113584Ssimokawa return; 1825103285Sikob case FWTCODE_WREQQ: 1826103285Sikob case FWTCODE_WREQB: 1827103285Sikob case FWTCODE_RREQQ: 1828103285Sikob case FWTCODE_RREQB: 1829103285Sikob case FWTCODE_LREQ: 1830120660Ssimokawa bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 1831113584Ssimokawa fp->mode.rreqq.dest_lo); 1832103285Sikob if(bind == NULL){ 1833113962Ssimokawa#if __FreeBSD_version >= 500000 1834120660Ssimokawa printf("Unknown service addr 0x%04x:0x%08x tcode=%x src=0x%x data=%x\n", 1835113962Ssimokawa#else 1836120660Ssimokawa printf("Unknown service addr 0x%04x:0x%08x tcode=%x src=0x%x data=%lx\n", 1837113962Ssimokawa#endif 1838113584Ssimokawa fp->mode.wreqq.dest_hi, 1839113584Ssimokawa fp->mode.wreqq.dest_lo, 1840113584Ssimokawa tcode, 1841113584Ssimokawa fp->mode.hdr.src, 1842113584Ssimokawa ntohl(fp->mode.wreqq.data)); 1843120660Ssimokawa if (rb->fc->status == FWBUSRESET) { 1844110798Ssimokawa printf("fw_rcv: cannot respond(bus reset)!\n"); 1845103285Sikob goto err; 1846103285Sikob } 1847120660Ssimokawa rb->xfer = fw_xfer_alloc(M_FWXFER); 1848120660Ssimokawa if(rb->xfer == NULL){ 1849103285Sikob return; 1850103285Sikob } 1851120660Ssimokawa rb->xfer->send.spd = rb->spd; 1852120660Ssimokawa rb->xfer->send.pay_len = 0; 1853120660Ssimokawa resfp = &rb->xfer->send.hdr; 1854113584Ssimokawa switch (tcode) { 1855103285Sikob case FWTCODE_WREQQ: 1856103285Sikob case FWTCODE_WREQB: 1857103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 1858103285Sikob break; 1859103285Sikob case FWTCODE_RREQQ: 1860103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1861103285Sikob break; 1862103285Sikob case FWTCODE_RREQB: 1863103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 1864103285Sikob break; 1865103285Sikob case FWTCODE_LREQ: 1866103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 1867103285Sikob break; 1868103285Sikob } 1869103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 1870103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1871103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 1872120660Ssimokawa resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 1873103285Sikob resfp->mode.rresb.extcode = 0; 1874103285Sikob resfp->mode.rresb.len = 0; 1875103285Sikob/* 1876120660Ssimokawa rb->xfer->act.hand = fw_asy_callback; 1877103285Sikob*/ 1878120660Ssimokawa rb->xfer->act.hand = fw_xfer_free; 1879120660Ssimokawa if(fw_asyreq(rb->fc, -1, rb->xfer)){ 1880120660Ssimokawa fw_xfer_free(rb->xfer); 1881103285Sikob return; 1882103285Sikob } 1883103285Sikob goto err; 1884103285Sikob } 1885113584Ssimokawa len = 0; 1886120660Ssimokawa for (i = 0; i < rb->nvec; i ++) 1887120660Ssimokawa len += rb->vec[i].iov_len; 1888113584Ssimokawa switch(bind->act_type){ 1889103285Sikob case FWACT_XFER: 1890113584Ssimokawa /* splfw()?? */ 1891120660Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 1892120660Ssimokawa if (rb->xfer == NULL) { 1893113584Ssimokawa printf("Discard a packet for this bind.\n"); 1894113584Ssimokawa goto err; 1895113584Ssimokawa } 1896113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1897120660Ssimokawa fw_rcv_copy(rb); 1898120660Ssimokawa if (rb->fc->status != FWBUSRESET) 1899120660Ssimokawa rb->xfer->act.hand(rb->xfer); 1900103285Sikob else 1901120660Ssimokawa STAILQ_INSERT_TAIL(&rb->fc->pending, 1902120660Ssimokawa rb->xfer, link); 1903103285Sikob return; 1904103285Sikob break; 1905103285Sikob case FWACT_CH: 1906120660Ssimokawa if(rb->fc->ir[bind->sub]->queued >= 1907120660Ssimokawa rb->fc->ir[bind->sub]->maxq){ 1908120660Ssimokawa device_printf(rb->fc->bdev, 1909108655Ssimokawa "Discard a packet %x %d\n", 1910113584Ssimokawa bind->sub, 1911120660Ssimokawa rb->fc->ir[bind->sub]->queued); 1912103285Sikob goto err; 1913103285Sikob } 1914120660Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 1915120660Ssimokawa if (rb->xfer == NULL) { 1916113584Ssimokawa printf("Discard packet for this bind\n"); 1917113584Ssimokawa goto err; 1918113584Ssimokawa } 1919113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1920120660Ssimokawa fw_rcv_copy(rb); 1921103285Sikob s = splfw(); 1922120660Ssimokawa rb->fc->ir[bind->sub]->queued++; 1923120660Ssimokawa STAILQ_INSERT_TAIL(&rb->fc->ir[bind->sub]->q, 1924120660Ssimokawa rb->xfer, link); 1925103285Sikob splx(s); 1926103285Sikob 1927120660Ssimokawa wakeup((caddr_t)rb->fc->ir[bind->sub]); 1928103285Sikob 1929103285Sikob return; 1930103285Sikob break; 1931103285Sikob default: 1932103285Sikob goto err; 1933103285Sikob break; 1934103285Sikob } 1935103285Sikob break; 1936120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */ 1937103285Sikob case FWTCODE_STREAM: 1938103285Sikob { 1939103285Sikob struct fw_xferq *xferq; 1940103285Sikob 1941120660Ssimokawa xferq = rb->fc->ir[sub]; 1942103285Sikob#if 0 1943103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 1944103285Sikob sub, len, off, spd); 1945103285Sikob#endif 1946103285Sikob if(xferq->queued >= xferq->maxq) { 1947103285Sikob printf("receive queue is full\n"); 1948103285Sikob goto err; 1949103285Sikob } 1950113584Ssimokawa /* XXX get xfer from xfer queue, we don't need copy for 1951113584Ssimokawa per packet mode */ 1952120660Ssimokawa rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 1953113584Ssimokawa vec[0].iov_len); 1954120660Ssimokawa if (rb->xfer == NULL) goto err; 1955120660Ssimokawa fw_rcv_copy(rb) 1956103285Sikob s = splfw(); 1957103285Sikob xferq->queued++; 1958120660Ssimokawa STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link); 1959103285Sikob splx(s); 1960120660Ssimokawa sc = device_get_softc(rb->fc->bdev); 1961103285Sikob#if __FreeBSD_version >= 500000 1962103285Sikob if (SEL_WAITING(&xferq->rsel)) 1963103285Sikob#else 1964103285Sikob if (&xferq->rsel.si_pid != 0) 1965103285Sikob#endif 1966103285Sikob selwakeup(&xferq->rsel); 1967103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 1968103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 1969103285Sikob wakeup((caddr_t)xferq); 1970103285Sikob } 1971103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 1972103285Sikob xferq->hand(xferq); 1973103285Sikob } 1974103285Sikob return; 1975103285Sikob break; 1976103285Sikob } 1977120660Ssimokawa#endif 1978103285Sikob default: 1979113584Ssimokawa printf("fw_rcv: unknow tcode %d\n", tcode); 1980103285Sikob break; 1981103285Sikob } 1982103285Sikoberr: 1983113584Ssimokawa return; 1984103285Sikob} 1985106815Ssimokawa 1986103285Sikob/* 1987103285Sikob * Post process for Bus Manager election process. 1988103285Sikob */ 1989103285Sikobstatic void 1990103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 1991103285Sikob{ 1992103285Sikob struct firewire_comm *fc; 1993109422Ssimokawa int bmr; 1994103285Sikob 1995109422Ssimokawa if (xfer == NULL) 1996109422Ssimokawa return; 1997103285Sikob fc = xfer->fc; 1998109422Ssimokawa if (xfer->resp != 0) 1999103285Sikob goto error; 2000120660Ssimokawa if (xfer->recv.payload == NULL) 2001103285Sikob goto error; 2002120660Ssimokawa if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 2003103285Sikob goto error; 2004109422Ssimokawa 2005120660Ssimokawa bmr = ntohl(xfer->recv.payload[0]); 2006109422Ssimokawa if (bmr == 0x3f) 2007109422Ssimokawa bmr = fc->nodeid; 2008109422Ssimokawa 2009109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 2010120660Ssimokawa fw_xfer_free_buf(xfer); 2011114909Ssimokawa fw_bmr(fc); 2012114909Ssimokawa return; 2013114909Ssimokawa 2014103285Sikoberror: 2015114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 2016120660Ssimokawa fw_xfer_free_buf(xfer); 2017103285Sikob} 2018106815Ssimokawa 2019113584Ssimokawa 2020103285Sikob/* 2021103285Sikob * To candidate Bus Manager election process. 2022103285Sikob */ 2023110270Ssimokawastatic void 2024106815Ssimokawafw_try_bmr(void *arg) 2025103285Sikob{ 2026103285Sikob struct fw_xfer *xfer; 2027103285Sikob struct firewire_comm *fc = (struct firewire_comm *)arg; 2028103285Sikob struct fw_pkt *fp; 2029103285Sikob int err = 0; 2030103285Sikob 2031120660Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4); 2032103285Sikob if(xfer == NULL){ 2033103285Sikob return; 2034103285Sikob } 2035120660Ssimokawa xfer->send.spd = 0; 2036103285Sikob fc->status = FWBUSMGRELECT; 2037103285Sikob 2038120660Ssimokawa fp = &xfer->send.hdr; 2039113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 2040103285Sikob fp->mode.lreq.tlrt = 0; 2041103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 2042103285Sikob fp->mode.lreq.pri = 0; 2043103285Sikob fp->mode.lreq.src = 0; 2044113584Ssimokawa fp->mode.lreq.len = 8; 2045120660Ssimokawa fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 2046120660Ssimokawa fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 2047113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2048120660Ssimokawa xfer->send.payload[0] = htonl(0x3f); 2049120660Ssimokawa xfer->send.payload[1] = htonl(fc->nodeid); 2050103285Sikob xfer->act.hand = fw_try_bmr_callback; 2051103285Sikob 2052103285Sikob err = fw_asyreq(fc, -1, xfer); 2053103285Sikob if(err){ 2054120660Ssimokawa fw_xfer_free_buf(xfer); 2055103285Sikob return; 2056103285Sikob } 2057103285Sikob return; 2058103285Sikob} 2059106543Ssimokawa 2060106543Ssimokawa#ifdef FW_VMACCESS 2061103285Sikob/* 2062103285Sikob * Software implementation for physical memory block access. 2063103285Sikob * XXX:Too slow, usef for debug purpose only. 2064103285Sikob */ 2065106815Ssimokawastatic void 2066106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){ 2067103285Sikob struct fw_pkt *rfp, *sfp = NULL; 2068113584Ssimokawa u_int32_t *ld = (u_int32_t *)xfer->recv.buf; 2069103285Sikob 2070113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 2071113584Ssimokawa xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 2072103285Sikob printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2073103285Sikob if(xfer->resp != 0){ 2074103285Sikob fw_xfer_free( xfer); 2075103285Sikob return; 2076103285Sikob } 2077103285Sikob if(xfer->recv.buf == NULL){ 2078103285Sikob fw_xfer_free( xfer); 2079103285Sikob return; 2080103285Sikob } 2081103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 2082103285Sikob switch(rfp->mode.hdr.tcode){ 2083103285Sikob /* XXX need fix for 64bit arch */ 2084103285Sikob case FWTCODE_WREQB: 2085110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2086103285Sikob xfer->send.len = 12; 2087103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2088103285Sikob bcopy(rfp->mode.wreqb.payload, 2089103285Sikob (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 2090103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2091103285Sikob sfp->mode.wres.rtcode = 0; 2092103285Sikob break; 2093103285Sikob case FWTCODE_WREQQ: 2094110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2095103285Sikob xfer->send.len = 12; 2096103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2097103285Sikob *((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 2098103285Sikob sfp->mode.wres.rtcode = 0; 2099103285Sikob break; 2100103285Sikob case FWTCODE_RREQB: 2101110195Ssimokawa xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 2102103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2103103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2104103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2105103285Sikob sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len)); 2106103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 2107103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 2108103285Sikob sfp->mode.rresb.rtcode = 0; 2109103285Sikob sfp->mode.rresb.extcode = 0; 2110103285Sikob break; 2111103285Sikob case FWTCODE_RREQQ: 2112110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2113103285Sikob xfer->send.len = 16; 2114103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2115103285Sikob sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2116103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 2117103285Sikob sfp->mode.rresb.rtcode = 0; 2118103285Sikob break; 2119103285Sikob default: 2120103285Sikob fw_xfer_free( xfer); 2121103285Sikob return; 2122103285Sikob } 2123103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2124103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2125103285Sikob xfer->act.hand = fw_xfer_free; 2126103285Sikob xfer->retry_req = fw_asybusy; 2127103285Sikob 2128103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2129103285Sikob sfp->mode.hdr.pri = 0; 2130103285Sikob 2131103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2132103285Sikob/**/ 2133103285Sikob return; 2134103285Sikob} 2135106543Ssimokawa#endif 2136106543Ssimokawa 2137103285Sikob/* 2138103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2139103285Sikob */ 2140106815Ssimokawau_int16_t 2141106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){ 2142103285Sikob u_int32_t i, sum, crc = 0; 2143103285Sikob int shift; 2144103285Sikob len = (len + 3) & ~3; 2145103285Sikob for(i = 0 ; i < len ; i+= 4){ 2146103285Sikob for( shift = 28 ; shift >= 0 ; shift -= 4){ 2147103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2148103285Sikob crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2149103285Sikob } 2150103285Sikob crc &= 0xffff; 2151103285Sikob } 2152103285Sikob return((u_int16_t) crc); 2153103285Sikob} 2154106815Ssimokawa 2155110270Ssimokawastatic int 2156110072Ssimokawafw_bmr(struct firewire_comm *fc) 2157110072Ssimokawa{ 2158110072Ssimokawa struct fw_device fwdev; 2159113584Ssimokawa union fw_self_id *self_id; 2160110072Ssimokawa int cmstr; 2161120660Ssimokawa u_int32_t quad; 2162110072Ssimokawa 2163113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2164113584Ssimokawa self_id = &fc->topology_map->self_id[fc->max_node]; 2165113584Ssimokawa if (fc->max_node > 0) { 2166114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2167114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2168113584Ssimokawa cmstr = fc->max_node; 2169114909Ssimokawa else { 2170114909Ssimokawa device_printf(fc->bdev, 2171114909Ssimokawa "root node is not cycle master capable\n"); 2172114909Ssimokawa /* XXX shall we be the cycle master? */ 2173113584Ssimokawa cmstr = fc->nodeid; 2174114909Ssimokawa /* XXX need bus reset */ 2175114909Ssimokawa } 2176113584Ssimokawa } else 2177113584Ssimokawa cmstr = -1; 2178114909Ssimokawa 2179114909Ssimokawa device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID)); 2180114909Ssimokawa if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2181114909Ssimokawa /* We are not the bus manager */ 2182114909Ssimokawa printf("\n"); 2183114909Ssimokawa return(0); 2184114909Ssimokawa } 2185114909Ssimokawa printf("(me)\n"); 2186114909Ssimokawa 2187114909Ssimokawa /* Optimize gapcount */ 2188113584Ssimokawa if(fc->max_hop <= MAX_GAPHOP ) 2189113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2190110072Ssimokawa /* If we are the cycle master, nothing to do */ 2191113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2192110072Ssimokawa return 0; 2193110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2194110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2195110072Ssimokawa fwdev.fc = fc; 2196110072Ssimokawa fwdev.dst = cmstr; 2197110072Ssimokawa fwdev.speed = 0; 2198110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2199110072Ssimokawa fwdev.status = FWDEVINIT; 2200110072Ssimokawa /* Set cmstr bit on the cycle master */ 2201120660Ssimokawa quad = htonl(1 << 8); 2202110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2203120660Ssimokawa 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free); 2204110072Ssimokawa 2205110072Ssimokawa return 0; 2206110072Ssimokawa} 2207110072Ssimokawa 2208118455Ssimokawastatic int 2209118455Ssimokawafw_modevent(module_t mode, int type, void *data) 2210118455Ssimokawa{ 2211118455Ssimokawa int err = 0; 2212118455Ssimokawa#if __FreeBSD_version >= 500000 2213118455Ssimokawa static eventhandler_tag fwdev_ehtag = NULL; 2214118455Ssimokawa#endif 2215118455Ssimokawa 2216118455Ssimokawa switch (type) { 2217118455Ssimokawa case MOD_LOAD: 2218118455Ssimokawa#if __FreeBSD_version >= 500000 2219118455Ssimokawa fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone, 2220118455Ssimokawa fwdev_clone, 0, 1000); 2221118455Ssimokawa#endif 2222118455Ssimokawa break; 2223118455Ssimokawa case MOD_UNLOAD: 2224118455Ssimokawa#if __FreeBSD_version >= 500000 2225118455Ssimokawa if (fwdev_ehtag != NULL) 2226118455Ssimokawa EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag); 2227118455Ssimokawa#endif 2228118455Ssimokawa break; 2229118455Ssimokawa case MOD_SHUTDOWN: 2230118455Ssimokawa break; 2231118455Ssimokawa } 2232118455Ssimokawa return (err); 2233118455Ssimokawa} 2234118455Ssimokawa 2235118455Ssimokawa 2236118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0); 2237103285SikobMODULE_VERSION(firewire, 1); 2238