firewire.c revision 116978
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 116978 2003-06-28 11:11:36Z simokawa $ 35103285Sikob * 36103285Sikob */ 37103285Sikob 38103285Sikob#include <sys/param.h> 39103285Sikob#include <sys/systm.h> 40103285Sikob#include <sys/types.h> 41103285Sikob#include <sys/mbuf.h> 42103285Sikob#include <sys/socket.h> 43103285Sikob#include <sys/socketvar.h> 44103285Sikob 45103285Sikob#include <sys/kernel.h> 46103285Sikob#include <sys/malloc.h> 47103285Sikob#include <sys/conf.h> 48103285Sikob#include <sys/sysctl.h> 49103285Sikob 50103285Sikob#include <machine/cpufunc.h> /* for rdtsc proto for clock.h below */ 51103285Sikob#include <machine/clock.h> 52103285Sikob 53103285Sikob#include <sys/bus.h> /* used by smbus and newbus */ 54113584Ssimokawa#include <machine/bus.h> 55103285Sikob 56103285Sikob#include <dev/firewire/firewire.h> 57103285Sikob#include <dev/firewire/firewirereg.h> 58110072Ssimokawa#include <dev/firewire/fwmem.h> 59103285Sikob#include <dev/firewire/iec13213.h> 60103285Sikob#include <dev/firewire/iec68113.h> 61103285Sikob 62116376Ssimokawastruct crom_src_buf { 63116376Ssimokawa struct crom_src src; 64116376Ssimokawa struct crom_chunk root; 65116376Ssimokawa struct crom_chunk vendor; 66116376Ssimokawa struct crom_chunk hw; 67116376Ssimokawa}; 68116376Ssimokawa 69109736Ssimokawaint firewire_debug=0, try_bmr=1; 70103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 71108281Ssimokawa "FireWire driver debug flag"); 72109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 73109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 74109736Ssimokawa "Try to be a bus manager"); 75103285Sikob 76110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 77110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 78110195Ssimokawa 79103285Sikob#define FW_MAXASYRTY 4 80103285Sikob#define FW_MAXDEVRCNT 4 81103285Sikob 82103285Sikobdevclass_t firewire_devclass; 83103285Sikob 84103285Sikobstatic int firewire_match __P((device_t)); 85103285Sikobstatic int firewire_attach __P((device_t)); 86103285Sikobstatic int firewire_detach __P((device_t)); 87116978Ssimokawastatic int firewire_resume __P((device_t)); 88103285Sikob#if 0 89103285Sikobstatic int firewire_shutdown __P((device_t)); 90103285Sikob#endif 91103285Sikobstatic device_t firewire_add_child __P((device_t, int, const char *, int)); 92103285Sikobstatic void fw_try_bmr __P((void *)); 93103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *)); 94103285Sikobstatic void fw_asystart __P((struct fw_xfer *)); 95103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *)); 96103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *)); 97103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *)); 98103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *)); 99103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *)); 100106543Ssimokawa#ifdef FW_VMACCESS 101103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *)); 102106543Ssimokawa#endif 103103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t, 104105620Ssimokawa u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *)))); 105110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *)); 106103285Sikob 107103285Sikobstatic device_method_t firewire_methods[] = { 108103285Sikob /* Device interface */ 109103285Sikob DEVMETHOD(device_probe, firewire_match), 110103285Sikob DEVMETHOD(device_attach, firewire_attach), 111103285Sikob DEVMETHOD(device_detach, firewire_detach), 112108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 113116978Ssimokawa DEVMETHOD(device_resume, firewire_resume), 114103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 115103285Sikob 116103285Sikob /* Bus interface */ 117103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 118103285Sikob DEVMETHOD(bus_print_child, bus_generic_print_child), 119103285Sikob 120103285Sikob { 0, 0 } 121103285Sikob}; 122103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"}; 123103285Sikob 124114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 125114909Ssimokawa#define MAX_GAPHOP 15 126114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 127114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 128106813Ssimokawa 129106813Ssimokawaextern struct cdevsw firewire_cdevsw; 130106813Ssimokawa 131103285Sikobstatic driver_t firewire_driver = { 132103285Sikob "firewire", 133103285Sikob firewire_methods, 134103285Sikob sizeof(struct firewire_softc), 135103285Sikob}; 136103285Sikob 137103285Sikob/* 138110072Ssimokawa * Lookup fwdev by node id. 139103285Sikob */ 140106810Ssimokawastruct fw_device * 141110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 142103285Sikob{ 143103285Sikob struct fw_device *fwdev; 144110072Ssimokawa int s; 145110072Ssimokawa 146110072Ssimokawa s = splfw(); 147110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 148110072Ssimokawa if (fwdev->dst == dst) 149103285Sikob break; 150110072Ssimokawa splx(s); 151110072Ssimokawa 152106810Ssimokawa if(fwdev == NULL) return NULL; 153106810Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 154106810Ssimokawa return fwdev; 155103285Sikob} 156106813Ssimokawa 157103285Sikob/* 158110072Ssimokawa * Lookup fwdev by EUI64. 159110072Ssimokawa */ 160110072Ssimokawastruct fw_device * 161110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 162110072Ssimokawa{ 163110072Ssimokawa struct fw_device *fwdev; 164110072Ssimokawa int s; 165110072Ssimokawa 166110072Ssimokawa s = splfw(); 167110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 168110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 169110072Ssimokawa break; 170110072Ssimokawa splx(s); 171110072Ssimokawa 172110072Ssimokawa if(fwdev == NULL) return NULL; 173110072Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 174110072Ssimokawa return fwdev; 175110072Ssimokawa} 176110072Ssimokawa 177110072Ssimokawa/* 178103285Sikob * Async. request procedure for userland application. 179103285Sikob */ 180103285Sikobint 181103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 182103285Sikob{ 183103285Sikob int err = 0; 184103285Sikob struct fw_xferq *xferq; 185103285Sikob int tl = 0, len; 186103285Sikob struct fw_pkt *fp; 187103285Sikob int tcode; 188103285Sikob struct tcode_info *info; 189103285Sikob 190103285Sikob if(xfer == NULL) return EINVAL; 191108701Ssimokawa if(xfer->send.len > MAXREC(fc->maxrec)){ 192103285Sikob printf("send.len > maxrec\n"); 193103285Sikob return EINVAL; 194103285Sikob } 195103285Sikob if(xfer->act.hand == NULL){ 196103285Sikob printf("act.hand == NULL\n"); 197103285Sikob return EINVAL; 198103285Sikob } 199103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 200103285Sikob 201103285Sikob tcode = fp->mode.common.tcode & 0xf; 202103285Sikob info = &fc->tcode[tcode]; 203103285Sikob if (info->flag == 0) { 204103285Sikob printf("invalid tcode=%d\n", tcode); 205103285Sikob return EINVAL; 206103285Sikob } 207103285Sikob if (info->flag & FWTI_REQ) 208103285Sikob xferq = fc->atq; 209103285Sikob else 210103285Sikob xferq = fc->ats; 211103285Sikob len = info->hdr_len; 212103285Sikob if (info->flag & FWTI_BLOCK_STR) 213113584Ssimokawa len += fp->mode.stream.len; 214103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 215113584Ssimokawa len += fp->mode.rresb.len; 216103285Sikob if( len > xfer->send.len ){ 217103285Sikob printf("len(%d) > send.len(%d) (tcode=%d)\n", 218103285Sikob len, xfer->send.len, tcode); 219103285Sikob return EINVAL; 220103285Sikob } 221103285Sikob xfer->send.len = len; 222106790Ssimokawa 223103285Sikob if(xferq->start == NULL){ 224103285Sikob printf("xferq->start == NULL\n"); 225103285Sikob return EINVAL; 226103285Sikob } 227103285Sikob if(!(xferq->queued < xferq->maxq)){ 228108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 229108655Ssimokawa xferq->queued); 230103285Sikob return EINVAL; 231103285Sikob } 232103285Sikob 233103285Sikob 234103285Sikob if (info->flag & FWTI_TLABEL) { 235103285Sikob if((tl = fw_get_tlabel(fc, xfer)) == -1 ) 236103285Sikob return EIO; 237103285Sikob fp->mode.hdr.tlrt = tl << 2; 238103285Sikob } 239103285Sikob 240103285Sikob xfer->tl = tl; 241103285Sikob xfer->resp = 0; 242103285Sikob xfer->fc = fc; 243103285Sikob xfer->q = xferq; 244103285Sikob xfer->retry_req = fw_asybusy; 245103285Sikob 246103285Sikob fw_asystart(xfer); 247103285Sikob return err; 248103285Sikob} 249103285Sikob/* 250103285Sikob * Wakeup blocked process. 251103285Sikob */ 252103285Sikobvoid 253103285Sikobfw_asy_callback(struct fw_xfer *xfer){ 254103285Sikob wakeup(xfer); 255103285Sikob return; 256103285Sikob} 257103285Sikob/* 258103285Sikob * Postpone to later retry. 259103285Sikob */ 260103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){ 261103285Sikob printf("fw_asybusy\n"); 262103285Sikob/* 263103285Sikob xfer->ch = timeout((timeout_t *)fw_asystart, (void *)xfer, 20000); 264103285Sikob*/ 265103285Sikob DELAY(20000); 266103285Sikob fw_asystart(xfer); 267103285Sikob return; 268103285Sikob} 269103285Sikob 270103285Sikob/* 271103285Sikob * Async. request with given xfer structure. 272103285Sikob */ 273106790Ssimokawastatic void 274106790Ssimokawafw_asystart(struct fw_xfer *xfer) 275106790Ssimokawa{ 276103285Sikob struct firewire_comm *fc = xfer->fc; 277103285Sikob int s; 278103285Sikob if(xfer->retry++ >= fc->max_asyretry){ 279113584Ssimokawa device_printf(fc->bdev, "max_asyretry exceeded\n"); 280103285Sikob xfer->resp = EBUSY; 281103285Sikob xfer->state = FWXF_BUSY; 282103285Sikob xfer->act.hand(xfer); 283103285Sikob return; 284103285Sikob } 285103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */ 286103285Sikob if (fc->status < FWBUSEXPLORE) { 287103285Sikob xfer->resp = EAGAIN; 288103285Sikob xfer->state = FWXF_BUSY; 289103285Sikob if (xfer->act.hand != NULL) 290103285Sikob xfer->act.hand(xfer); 291103285Sikob return; 292103285Sikob } 293103285Sikob#endif 294103285Sikob s = splfw(); 295103285Sikob xfer->state = FWXF_INQ; 296103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 297103285Sikob xfer->q->queued ++; 298103285Sikob splx(s); 299103285Sikob /* XXX just queue for mbuf */ 300103285Sikob if (xfer->mbuf == NULL) 301103285Sikob xfer->q->start(fc); 302103285Sikob return; 303103285Sikob} 304106790Ssimokawa 305103285Sikobstatic int 306103285Sikobfirewire_match( device_t dev ) 307103285Sikob{ 308108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 309103285Sikob return -140; 310103285Sikob} 311106790Ssimokawa 312110577Ssimokawastatic void 313110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc) 314110577Ssimokawa{ 315110577Ssimokawa struct fw_xfer *xfer; 316110577Ssimokawa struct tlabel *tl; 317110577Ssimokawa struct timeval tv; 318110577Ssimokawa struct timeval split_timeout; 319111040Ssimokawa int i, s; 320110577Ssimokawa 321110891Ssimokawa split_timeout.tv_sec = 6; 322110577Ssimokawa split_timeout.tv_usec = 0; 323110577Ssimokawa 324110577Ssimokawa microtime(&tv); 325110577Ssimokawa timevalsub(&tv, &split_timeout); 326110577Ssimokawa 327111040Ssimokawa s = splfw(); 328110577Ssimokawa for (i = 0; i < 0x40; i ++) { 329110577Ssimokawa while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 330110577Ssimokawa xfer = tl->xfer; 331110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 332110577Ssimokawa /* the rests are newer than this */ 333110577Ssimokawa break; 334110577Ssimokawa device_printf(fc->bdev, 335110891Ssimokawa "split transaction timeout dst=0x%x tl=0x%x\n", 336110577Ssimokawa xfer->dst, i); 337110577Ssimokawa xfer->resp = ETIMEDOUT; 338110577Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], link); 339113584Ssimokawa fw_xfer_done(xfer); 340110577Ssimokawa } 341110577Ssimokawa } 342111040Ssimokawa splx(s); 343110577Ssimokawa} 344110577Ssimokawa 345110577Ssimokawastatic void 346110577Ssimokawafirewire_watchdog(void *arg) 347110577Ssimokawa{ 348110577Ssimokawa struct firewire_comm *fc; 349110577Ssimokawa 350110577Ssimokawa fc = (struct firewire_comm *)arg; 351110577Ssimokawa firewire_xfer_timeout(fc); 352110577Ssimokawa fc->timeout(fc); 353110577Ssimokawa callout_reset(&fc->timeout_callout, hz, 354110577Ssimokawa (void *)firewire_watchdog, (void *)fc); 355110577Ssimokawa} 356110577Ssimokawa 357103285Sikob/* 358103285Sikob * The attach routine. 359103285Sikob */ 360103285Sikobstatic int 361103285Sikobfirewire_attach( device_t dev ) 362103285Sikob{ 363103285Sikob int i, unitmask, mn; 364103285Sikob struct firewire_softc *sc = device_get_softc(dev); 365103285Sikob device_t pa = device_get_parent(dev); 366103285Sikob struct firewire_comm *fc; 367103285Sikob dev_t d; 368103285Sikob 369103285Sikob fc = (struct firewire_comm *)device_get_softc(pa); 370103285Sikob sc->fc = fc; 371116978Ssimokawa fc->status = FWBUSNOTREADY; 372103285Sikob 373103285Sikob unitmask = UNIT2MIN(device_get_unit(dev)); 374103285Sikob 375103285Sikob if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 376103285Sikob for ( i = 0 ; i < fc->nisodma ; i++ ){ 377103285Sikob mn = unitmask | i; 378103285Sikob /* XXX device name should be improved */ 379103285Sikob d = make_dev(&firewire_cdevsw, unit2minor(mn), 380108276Ssimokawa UID_ROOT, GID_OPERATOR, 0660, 381103285Sikob "fw%x", mn); 382103285Sikob#if __FreeBSD_version >= 500000 383103285Sikob if (i == 0) 384103285Sikob sc->dev = d; 385103285Sikob else 386103285Sikob dev_depends(sc->dev, d); 387103285Sikob#else 388103285Sikob sc->dev[i] = d; 389103285Sikob#endif 390103285Sikob } 391103285Sikob d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG), 392108276Ssimokawa UID_ROOT, GID_OPERATOR, 0660, 393103285Sikob "fwmem%d", device_get_unit(dev)); 394103285Sikob#if __FreeBSD_version >= 500000 395103285Sikob dev_depends(sc->dev, d); 396103285Sikob#else 397103285Sikob sc->dev[i] = d; 398103285Sikob#endif 399110193Ssimokawa CALLOUT_INIT(&sc->fc->timeout_callout); 400110193Ssimokawa CALLOUT_INIT(&sc->fc->bmr_callout); 401110193Ssimokawa CALLOUT_INIT(&sc->fc->retry_probe_callout); 402110193Ssimokawa CALLOUT_INIT(&sc->fc->busprobe_callout); 403108853Ssimokawa 404110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 405110577Ssimokawa (void *)firewire_watchdog, (void *)sc->fc); 406110193Ssimokawa 407103285Sikob /* Locate our children */ 408103285Sikob bus_generic_probe(dev); 409103285Sikob 410103285Sikob /* launch attachement of the added children */ 411103285Sikob bus_generic_attach(dev); 412103285Sikob 413103285Sikob /* bus_reset */ 414103285Sikob fc->ibr(fc); 415103285Sikob 416103285Sikob return 0; 417103285Sikob} 418103285Sikob 419103285Sikob/* 420103285Sikob * Attach it as child. 421103285Sikob */ 422103285Sikobstatic device_t 423103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit) 424103285Sikob{ 425103285Sikob device_t child; 426103285Sikob struct firewire_softc *sc; 427103285Sikob 428103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 429103285Sikob child = device_add_child(dev, name, unit); 430103285Sikob if (child) { 431103285Sikob device_set_ivars(child, sc->fc); 432103285Sikob device_probe_and_attach(child); 433103285Sikob } 434103285Sikob 435103285Sikob return child; 436103285Sikob} 437106790Ssimokawa 438116978Ssimokawastatic int 439116978Ssimokawafirewire_resume(device_t dev) 440116978Ssimokawa{ 441116978Ssimokawa struct firewire_softc *sc; 442116978Ssimokawa 443116978Ssimokawa sc = (struct firewire_softc *)device_get_softc(dev); 444116978Ssimokawa sc->fc->status = FWBUSNOTREADY; 445116978Ssimokawa 446116978Ssimokawa bus_generic_resume(dev); 447116978Ssimokawa 448116978Ssimokawa return(0); 449116978Ssimokawa} 450116978Ssimokawa 451103285Sikob/* 452103285Sikob * Dettach it. 453103285Sikob */ 454103285Sikobstatic int 455103285Sikobfirewire_detach( device_t dev ) 456103285Sikob{ 457103285Sikob struct firewire_softc *sc; 458111074Ssimokawa struct csrdir *csrd, *next; 459111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 460103285Sikob 461103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 462106790Ssimokawa 463111078Ssimokawa bus_generic_detach(dev); 464111078Ssimokawa 465111078Ssimokawa callout_stop(&sc->fc->timeout_callout); 466111078Ssimokawa callout_stop(&sc->fc->bmr_callout); 467111078Ssimokawa callout_stop(&sc->fc->retry_probe_callout); 468111078Ssimokawa callout_stop(&sc->fc->busprobe_callout); 469111078Ssimokawa 470103285Sikob#if __FreeBSD_version >= 500000 471103285Sikob destroy_dev(sc->dev); 472103285Sikob#else 473103285Sikob { 474103285Sikob int j; 475103285Sikob for (j = 0 ; j < sc->fc->nisodma + 1; j++) 476103285Sikob destroy_dev(sc->dev[j]); 477103285Sikob } 478103285Sikob#endif 479103285Sikob /* XXX xfree_free and untimeout on all xfers */ 480111078Ssimokawa for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL; 481111078Ssimokawa fwdev = fwdev_next) { 482111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 483111078Ssimokawa free(fwdev, M_FW); 484111078Ssimokawa } 485111074Ssimokawa for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) { 486111074Ssimokawa next = SLIST_NEXT(csrd, link); 487111074Ssimokawa free(csrd, M_FW); 488111074Ssimokawa } 489110195Ssimokawa free(sc->fc->topology_map, M_FW); 490110195Ssimokawa free(sc->fc->speed_map, M_FW); 491116376Ssimokawa free(sc->fc->crom_src_buf, M_FW); 492103285Sikob return(0); 493103285Sikob} 494103285Sikob#if 0 495103285Sikobstatic int 496103285Sikobfirewire_shutdown( device_t dev ) 497103285Sikob{ 498103285Sikob return 0; 499103285Sikob} 500103285Sikob#endif 501106790Ssimokawa 502110577Ssimokawa 503110577Ssimokawastatic void 504110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 505110577Ssimokawa{ 506110577Ssimokawa struct fw_xfer *xfer; 507110577Ssimokawa 508110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 509110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 510111942Ssimokawa xferq->queued --; 511110577Ssimokawa xfer->resp = EAGAIN; 512113584Ssimokawa fw_xfer_done(xfer); 513110577Ssimokawa } 514110577Ssimokawa} 515110577Ssimokawa 516110798Ssimokawavoid 517110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 518110798Ssimokawa{ 519110798Ssimokawa int i; 520110798Ssimokawa 521110798Ssimokawa fw_xferq_drain(fc->atq); 522110798Ssimokawa fw_xferq_drain(fc->ats); 523110798Ssimokawa for(i = 0; i < fc->nisodma; i++) 524110798Ssimokawa fw_xferq_drain(fc->it[i]); 525110798Ssimokawa} 526110798Ssimokawa 527116376Ssimokawastatic void 528116376Ssimokawafw_reset_csr(struct firewire_comm *fc) 529103285Sikob{ 530103285Sikob int i; 531103285Sikob 532103285Sikob CSRARC(fc, STATE_CLEAR) 533103285Sikob = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 534103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 535103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 536103285Sikob 537103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 538103285Sikob fc->irm = -1; 539103285Sikob 540103285Sikob fc->max_node = -1; 541103285Sikob 542103285Sikob for(i = 2; i < 0x100/4 - 2 ; i++){ 543103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 544103285Sikob } 545103285Sikob CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 546103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 547103285Sikob CSRARC(fc, RESET_START) = 0; 548103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 549103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 550103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 551103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 552103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 553103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 554103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 555103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 556103285Sikob CSRARC(fc, IP_CHANNELS) = (1 << 31); 557103285Sikob 558103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 559103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 560103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 561103285Sikob 1 << 28 | 0xff << 16 | 0x09 << 8; 562103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 563103285Sikob 564103285Sikob/* DV depend CSRs see blue book */ 565103285Sikob CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 566103285Sikob CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 567103285Sikob 568103285Sikob CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 ); 569103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 570116376Ssimokawa} 571113584Ssimokawa 572116376Ssimokawastatic void 573116376Ssimokawafw_init_crom(struct firewire_comm *fc) 574116376Ssimokawa{ 575116376Ssimokawa struct crom_src *src; 576116376Ssimokawa 577116376Ssimokawa fc->crom_src_buf = (struct crom_src_buf *) 578116376Ssimokawa malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO); 579116376Ssimokawa if (fc->crom_src_buf == NULL) 580116376Ssimokawa return; 581116376Ssimokawa 582116376Ssimokawa src = &fc->crom_src_buf->src; 583116376Ssimokawa bzero(src, sizeof(struct crom_src)); 584116376Ssimokawa 585116376Ssimokawa /* BUS info sample */ 586116376Ssimokawa src->hdr.info_len = 4; 587116376Ssimokawa 588116376Ssimokawa src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 589116376Ssimokawa 590116376Ssimokawa src->businfo.irmc = 1; 591116376Ssimokawa src->businfo.cmc = 1; 592116376Ssimokawa src->businfo.isc = 1; 593116376Ssimokawa src->businfo.bmc = 1; 594116376Ssimokawa src->businfo.pmc = 0; 595116376Ssimokawa src->businfo.cyc_clk_acc = 100; 596116376Ssimokawa src->businfo.max_rec = fc->maxrec; 597116376Ssimokawa src->businfo.max_rom = MAXROM_4; 598116376Ssimokawa src->businfo.generation = 0; 599116376Ssimokawa src->businfo.link_spd = fc->speed; 600116376Ssimokawa 601116376Ssimokawa src->businfo.eui64.hi = fc->eui.hi; 602116376Ssimokawa src->businfo.eui64.lo = fc->eui.lo; 603116376Ssimokawa 604116376Ssimokawa STAILQ_INIT(&src->chunk_list); 605116376Ssimokawa 606116376Ssimokawa fc->crom_src = src; 607116376Ssimokawa fc->crom_root = &fc->crom_src_buf->root; 608116376Ssimokawa} 609116376Ssimokawa 610116376Ssimokawastatic void 611116376Ssimokawafw_reset_crom(struct firewire_comm *fc) 612116376Ssimokawa{ 613116376Ssimokawa struct crom_src_buf *buf; 614116376Ssimokawa struct crom_src *src; 615116376Ssimokawa struct crom_chunk *root; 616116376Ssimokawa 617116376Ssimokawa if (fc->crom_src_buf == NULL) 618116376Ssimokawa fw_init_crom(fc); 619116376Ssimokawa 620116376Ssimokawa buf = fc->crom_src_buf; 621116376Ssimokawa src = fc->crom_src; 622116376Ssimokawa root = fc->crom_root; 623116376Ssimokawa 624116376Ssimokawa src->businfo.generation ++; 625116376Ssimokawa STAILQ_INIT(&src->chunk_list); 626116376Ssimokawa 627116376Ssimokawa bzero(root, sizeof(struct crom_chunk)); 628116376Ssimokawa crom_add_chunk(src, NULL, root, 0); 629116376Ssimokawa crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 630116376Ssimokawa /* private company_id */ 631116376Ssimokawa crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 632116376Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project"); 633116376Ssimokawa crom_add_entry(root, CSRKEY_HW, __FreeBSD_version); 634116376Ssimokawa crom_add_simple_text(src, root, &buf->hw, hostname); 635116376Ssimokawa} 636116376Ssimokawa 637116376Ssimokawa/* 638116376Ssimokawa * Called after bus reset. 639116376Ssimokawa */ 640116376Ssimokawavoid 641116376Ssimokawafw_busreset(struct firewire_comm *fc) 642116376Ssimokawa{ 643116376Ssimokawa struct firewire_dev_comm *fdc; 644116376Ssimokawa device_t *devlistp; 645116376Ssimokawa int i, devcnt; 646116376Ssimokawa 647116376Ssimokawa switch(fc->status){ 648116376Ssimokawa case FWBUSMGRELECT: 649116376Ssimokawa callout_stop(&fc->bmr_callout); 650116376Ssimokawa break; 651116376Ssimokawa default: 652116376Ssimokawa break; 653116376Ssimokawa } 654116376Ssimokawa fc->status = FWBUSRESET; 655116376Ssimokawa fw_reset_csr(fc); 656116376Ssimokawa fw_reset_crom(fc); 657116376Ssimokawa 658113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 659113584Ssimokawa for( i = 0 ; i < devcnt ; i++) 660113584Ssimokawa if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 661113584Ssimokawa fdc = device_get_softc(devlistp[i]); 662113584Ssimokawa if (fdc->post_busreset != NULL) 663113584Ssimokawa fdc->post_busreset(fdc); 664113584Ssimokawa } 665113584Ssimokawa free(devlistp, M_TEMP); 666113584Ssimokawa } 667116376Ssimokawa 668116376Ssimokawa crom_load(&fc->crom_src_buf->src, fc->config_rom, CROMSIZE); 669103285Sikob} 670106790Ssimokawa 671103285Sikob/* Call once after reboot */ 672106790Ssimokawavoid fw_init(struct firewire_comm *fc) 673103285Sikob{ 674103285Sikob int i; 675106543Ssimokawa struct csrdir *csrd; 676106543Ssimokawa#ifdef FW_VMACCESS 677103285Sikob struct fw_xfer *xfer; 678103285Sikob struct fw_bind *fwb; 679106543Ssimokawa#endif 680103285Sikob 681103285Sikob fc->max_asyretry = FW_MAXASYRTY; 682103285Sikob 683103285Sikob fc->arq->queued = 0; 684103285Sikob fc->ars->queued = 0; 685103285Sikob fc->atq->queued = 0; 686103285Sikob fc->ats->queued = 0; 687103285Sikob 688103285Sikob fc->arq->buf = NULL; 689103285Sikob fc->ars->buf = NULL; 690103285Sikob fc->atq->buf = NULL; 691103285Sikob fc->ats->buf = NULL; 692103285Sikob 693113584Ssimokawa fc->arq->flag = 0; 694113584Ssimokawa fc->ars->flag = 0; 695113584Ssimokawa fc->atq->flag = 0; 696113584Ssimokawa fc->ats->flag = 0; 697103285Sikob 698103285Sikob STAILQ_INIT(&fc->atq->q); 699103285Sikob STAILQ_INIT(&fc->ats->q); 700103285Sikob 701103285Sikob for( i = 0 ; i < fc->nisodma ; i ++ ){ 702103285Sikob fc->it[i]->queued = 0; 703103285Sikob fc->ir[i]->queued = 0; 704103285Sikob 705103285Sikob fc->it[i]->start = NULL; 706103285Sikob fc->ir[i]->start = NULL; 707103285Sikob 708103285Sikob fc->it[i]->buf = NULL; 709103285Sikob fc->ir[i]->buf = NULL; 710103285Sikob 711103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 712103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 713103285Sikob 714103285Sikob STAILQ_INIT(&fc->it[i]->q); 715103285Sikob STAILQ_INIT(&fc->ir[i]->q); 716103285Sikob 717103285Sikob STAILQ_INIT(&fc->it[i]->binds); 718103285Sikob STAILQ_INIT(&fc->ir[i]->binds); 719103285Sikob } 720103285Sikob 721103285Sikob fc->arq->maxq = FWMAXQUEUE; 722103285Sikob fc->ars->maxq = FWMAXQUEUE; 723103285Sikob fc->atq->maxq = FWMAXQUEUE; 724103285Sikob fc->ats->maxq = FWMAXQUEUE; 725103285Sikob 726103285Sikob for( i = 0 ; i < fc->nisodma ; i++){ 727103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 728103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 729103285Sikob } 730103285Sikob/* Initialize csr registers */ 731103285Sikob fc->topology_map = (struct fw_topology_map *)malloc( 732103285Sikob sizeof(struct fw_topology_map), 733110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 734103285Sikob fc->speed_map = (struct fw_speed_map *)malloc( 735103285Sikob sizeof(struct fw_speed_map), 736110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 737103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 738103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 739103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 740103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 741103285Sikob 742110193Ssimokawa STAILQ_INIT(&fc->devices); 743103285Sikob STAILQ_INIT(&fc->pending); 744103285Sikob 745103285Sikob/* Initialize csr ROM work space */ 746103285Sikob SLIST_INIT(&fc->ongocsr); 747103285Sikob SLIST_INIT(&fc->csrfree); 748103285Sikob for( i = 0 ; i < FWMAXCSRDIR ; i++){ 749110195Ssimokawa csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT); 750103285Sikob if(csrd == NULL) break; 751103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 752103285Sikob } 753103285Sikob 754103285Sikob/* Initialize Async handlers */ 755103285Sikob STAILQ_INIT(&fc->binds); 756103285Sikob for( i = 0 ; i < 0x40 ; i++){ 757103285Sikob STAILQ_INIT(&fc->tlabels[i]); 758103285Sikob } 759103285Sikob 760103285Sikob/* DV depend CSRs see blue book */ 761103285Sikob#if 0 762103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 763103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 764103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 765103285Sikob CSRARC(fc, i + oPCR) = 0x8000007a; 766103285Sikob } 767103285Sikob 768103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 769103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 770103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 771103285Sikob CSRARC(fc, i + iPCR) = 0x0; 772103285Sikob } 773103285Sikob#endif 774103285Sikob 775116376Ssimokawa fc->crom_src_buf = NULL; 776103285Sikob 777106543Ssimokawa#ifdef FW_VMACCESS 778103285Sikob xfer = fw_xfer_alloc(); 779103285Sikob if(xfer == NULL) return; 780103285Sikob 781110195Ssimokawa fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 782103285Sikob if(fwb == NULL){ 783103285Sikob fw_xfer_free(xfer); 784103285Sikob } 785103285Sikob xfer->act.hand = fw_vmaccess; 786103285Sikob xfer->fc = fc; 787103285Sikob xfer->sc = NULL; 788103285Sikob 789103285Sikob fwb->start_hi = 0x2; 790103285Sikob fwb->start_lo = 0; 791103285Sikob fwb->addrlen = 0xffffffff; 792103285Sikob fwb->xfer = xfer; 793103285Sikob fw_bindadd(fc, fwb); 794106543Ssimokawa#endif 795103285Sikob} 796106790Ssimokawa 797103285Sikob/* 798103285Sikob * To lookup binded process from IEEE1394 address. 799103285Sikob */ 800106813Ssimokawastruct fw_bind * 801106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo) 802103285Sikob{ 803103285Sikob struct fw_bind *tfw; 804103285Sikob for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ; 805103285Sikob tfw = STAILQ_NEXT(tfw, fclist)){ 806113584Ssimokawa if (tfw->act_type != FWACT_NULL && 807103285Sikob tfw->start_hi == dest_hi && 808103285Sikob tfw->start_lo <= dest_lo && 809103285Sikob (tfw->start_lo + tfw->addrlen) > dest_lo){ 810103285Sikob return(tfw); 811103285Sikob } 812103285Sikob } 813103285Sikob return(NULL); 814103285Sikob} 815106790Ssimokawa 816103285Sikob/* 817103285Sikob * To bind IEEE1394 address block to process. 818103285Sikob */ 819106790Ssimokawaint 820106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 821103285Sikob{ 822103285Sikob struct fw_bind *tfw, *tfw2 = NULL; 823103285Sikob int err = 0; 824103285Sikob tfw = STAILQ_FIRST(&fc->binds); 825103285Sikob if(tfw == NULL){ 826103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 827103285Sikob goto out; 828103285Sikob } 829103285Sikob if((tfw->start_hi > fwb->start_hi) || 830103285Sikob (tfw->start_hi == fwb->start_hi && 831103285Sikob (tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){ 832103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 833103285Sikob goto out; 834103285Sikob } 835103285Sikob for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){ 836103285Sikob if((tfw->start_hi < fwb->start_hi) || 837103285Sikob (tfw->start_hi == fwb->start_hi && 838103285Sikob (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){ 839103285Sikob tfw2 = STAILQ_NEXT(tfw, fclist); 840103285Sikob if(tfw2 == NULL) 841103285Sikob break; 842103285Sikob if((tfw2->start_hi > fwb->start_hi) || 843103285Sikob (tfw2->start_hi == fwb->start_hi && 844103285Sikob tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){ 845103285Sikob break; 846103285Sikob }else{ 847103285Sikob err = EBUSY; 848103285Sikob goto out; 849103285Sikob } 850103285Sikob } 851103285Sikob } 852103285Sikob if(tfw != NULL){ 853103285Sikob STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist); 854103285Sikob }else{ 855103285Sikob STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist); 856103285Sikob } 857103285Sikobout: 858113584Ssimokawa if (!err && fwb->act_type == FWACT_CH) 859113584Ssimokawa STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist); 860103285Sikob return err; 861103285Sikob} 862103285Sikob 863103285Sikob/* 864103285Sikob * To free IEEE1394 address block. 865103285Sikob */ 866106790Ssimokawaint 867106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 868103285Sikob{ 869103285Sikob int s; 870113584Ssimokawa struct fw_xfer *xfer, *next; 871103285Sikob 872103285Sikob s = splfw(); 873103285Sikob /* shall we check the existance? */ 874103285Sikob STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 875113584Ssimokawa /* shall we do this? */ 876113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 877113584Ssimokawa next = STAILQ_NEXT(xfer, link); 878113584Ssimokawa fw_xfer_free(xfer); 879113584Ssimokawa } 880113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 881113584Ssimokawa 882103285Sikob splx(s); 883103285Sikob return 0; 884103285Sikob} 885103285Sikob 886103285Sikob/* 887103285Sikob * To free transaction label. 888103285Sikob */ 889106790Ssimokawastatic void 890106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 891103285Sikob{ 892103285Sikob struct tlabel *tl; 893103285Sikob int s = splfw(); 894103285Sikob 895103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL; 896103285Sikob tl = STAILQ_NEXT(tl, link)){ 897103285Sikob if(tl->xfer == xfer){ 898103285Sikob STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link); 899110195Ssimokawa free(tl, M_FW); 900103285Sikob splx(s); 901103285Sikob return; 902103285Sikob } 903103285Sikob } 904103285Sikob splx(s); 905103285Sikob return; 906103285Sikob} 907106790Ssimokawa 908103285Sikob/* 909103285Sikob * To obtain XFER structure by transaction label. 910103285Sikob */ 911106790Ssimokawastatic struct fw_xfer * 912106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel) 913103285Sikob{ 914103285Sikob struct fw_xfer *xfer; 915103285Sikob struct tlabel *tl; 916103285Sikob int s = splfw(); 917103285Sikob 918103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL; 919103285Sikob tl = STAILQ_NEXT(tl, link)){ 920103285Sikob if(tl->xfer->dst == node){ 921103285Sikob xfer = tl->xfer; 922103285Sikob splx(s); 923110577Ssimokawa if (firewire_debug > 2) 924110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 925103285Sikob return(xfer); 926103285Sikob } 927103285Sikob } 928110577Ssimokawa if (firewire_debug > 1) 929110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 930103285Sikob splx(s); 931103285Sikob return(NULL); 932103285Sikob} 933106790Ssimokawa 934103285Sikob/* 935103285Sikob * To allocate IEEE1394 XFER structure. 936103285Sikob */ 937106790Ssimokawastruct fw_xfer * 938110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 939103285Sikob{ 940103285Sikob struct fw_xfer *xfer; 941106790Ssimokawa 942110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 943106790Ssimokawa if (xfer == NULL) 944106790Ssimokawa return xfer; 945106790Ssimokawa 946110577Ssimokawa microtime(&xfer->tv); 947110269Ssimokawa xfer->malloc = type; 948106790Ssimokawa 949103285Sikob return xfer; 950103285Sikob} 951106790Ssimokawa 952113584Ssimokawastruct fw_xfer * 953113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 954113584Ssimokawa{ 955113584Ssimokawa struct fw_xfer *xfer; 956113584Ssimokawa 957113584Ssimokawa xfer = fw_xfer_alloc(type); 958113584Ssimokawa xfer->send.len = send_len; 959113584Ssimokawa xfer->recv.len = recv_len; 960113584Ssimokawa if (xfer == NULL) 961113584Ssimokawa return(NULL); 962113584Ssimokawa if (send_len) { 963113584Ssimokawa xfer->send.buf = malloc(send_len, type, M_NOWAIT | M_ZERO); 964113584Ssimokawa if (xfer->send.buf == NULL) { 965113584Ssimokawa fw_xfer_free(xfer); 966113584Ssimokawa return(NULL); 967113584Ssimokawa } 968113584Ssimokawa } 969113584Ssimokawa if (recv_len) { 970113584Ssimokawa xfer->recv.buf = malloc(recv_len, type, M_NOWAIT); 971113584Ssimokawa if (xfer->recv.buf == NULL) { 972113584Ssimokawa if (xfer->send.buf != NULL) 973113584Ssimokawa free(xfer->send.buf, type); 974113584Ssimokawa fw_xfer_free(xfer); 975113584Ssimokawa return(NULL); 976113584Ssimokawa } 977113584Ssimokawa } 978113584Ssimokawa return(xfer); 979113584Ssimokawa} 980113584Ssimokawa 981103285Sikob/* 982103285Sikob * IEEE1394 XFER post process. 983103285Sikob */ 984103285Sikobvoid 985103285Sikobfw_xfer_done(struct fw_xfer *xfer) 986103285Sikob{ 987103285Sikob if (xfer->act.hand == NULL) 988103285Sikob return; 989103285Sikob 990103285Sikob if (xfer->fc->status != FWBUSRESET) 991103285Sikob xfer->act.hand(xfer); 992103285Sikob else { 993103285Sikob printf("fw_xfer_done: pending\n"); 994103285Sikob if (xfer->fc != NULL) 995103285Sikob STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link); 996103285Sikob else 997103285Sikob panic("fw_xfer_done: why xfer->fc is NULL?"); 998103285Sikob } 999103285Sikob} 1000103285Sikob 1001106790Ssimokawavoid 1002113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer) 1003103285Sikob{ 1004103285Sikob int s; 1005113584Ssimokawa 1006103285Sikob if(xfer == NULL ) return; 1007103285Sikob if(xfer->state == FWXF_INQ){ 1008103285Sikob printf("fw_xfer_free FWXF_INQ\n"); 1009103285Sikob s = splfw(); 1010103285Sikob STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1011103285Sikob xfer->q->queued --; 1012103285Sikob splx(s); 1013103285Sikob } 1014113584Ssimokawa if (xfer->fc != NULL) { 1015114729Ssimokawa#if 1 1016113584Ssimokawa if(xfer->state == FWXF_START) 1017114729Ssimokawa /* 1018114729Ssimokawa * This could happen if: 1019114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 1020114729Ssimokawa * 2. firewire_watch() is called. 1021114729Ssimokawa */ 1022114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 1023103285Sikob#endif 1024113584Ssimokawa fw_tl_free(xfer->fc, xfer); 1025103285Sikob } 1026113584Ssimokawa xfer->state = FWXF_INIT; 1027113584Ssimokawa xfer->resp = 0; 1028113584Ssimokawa xfer->retry = 0; 1029113584Ssimokawa} 1030113584Ssimokawa/* 1031113584Ssimokawa * To free IEEE1394 XFER structure. 1032113584Ssimokawa */ 1033113584Ssimokawavoid 1034113584Ssimokawafw_xfer_free( struct fw_xfer* xfer) 1035113584Ssimokawa{ 1036113584Ssimokawa if(xfer == NULL ) return; 1037113584Ssimokawa fw_xfer_unload(xfer); 1038103285Sikob if(xfer->send.buf != NULL){ 1039113584Ssimokawa free(xfer->send.buf, xfer->malloc); 1040103285Sikob } 1041103285Sikob if(xfer->recv.buf != NULL){ 1042113584Ssimokawa free(xfer->recv.buf, xfer->malloc); 1043103285Sikob } 1044110269Ssimokawa free(xfer, xfer->malloc); 1045103285Sikob} 1046103285Sikob 1047103285Sikobstatic void 1048110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 1049103285Sikob{ 1050103285Sikob#if 0 1051110072Ssimokawa printf("asyreq done state=%d resp=%d\n", 1052103285Sikob xfer->state, xfer->resp); 1053103285Sikob#endif 1054103285Sikob fw_xfer_free(xfer); 1055103285Sikob} 1056103285Sikob 1057103285Sikob/* 1058103285Sikob * To configure PHY. 1059103285Sikob */ 1060103285Sikobstatic void 1061103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1062103285Sikob{ 1063103285Sikob struct fw_xfer *xfer; 1064103285Sikob struct fw_pkt *fp; 1065103285Sikob 1066103285Sikob fc->status = FWBUSPHYCONF; 1067103285Sikob 1068113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 12, 0); 1069113584Ssimokawa if (xfer == NULL) 1070113584Ssimokawa return; 1071103285Sikob xfer->fc = fc; 1072103285Sikob xfer->retry_req = fw_asybusy; 1073110072Ssimokawa xfer->act.hand = fw_asy_callback_free; 1074103285Sikob 1075103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1076103285Sikob fp->mode.ld[1] = 0; 1077103285Sikob if (root_node >= 0) 1078113584Ssimokawa fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1079103285Sikob if (gap_count >= 0) 1080113584Ssimokawa fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1081103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 1082103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 1083103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 1084103285Sikob 1085107653Ssimokawa if (firewire_debug) 1086107653Ssimokawa printf("send phy_config root_node=%d gap_count=%d\n", 1087103285Sikob root_node, gap_count); 1088103285Sikob fw_asyreq(fc, -1, xfer); 1089103285Sikob} 1090103285Sikob 1091103285Sikob#if 0 1092103285Sikob/* 1093103285Sikob * Dump self ID. 1094103285Sikob */ 1095103285Sikobstatic void 1096103285Sikobfw_print_sid(u_int32_t sid) 1097103285Sikob{ 1098103285Sikob union fw_self_id *s; 1099103285Sikob s = (union fw_self_id *) &sid; 1100103285Sikob printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1101103285Sikob " p0:%d p1:%d p2:%d i:%d m:%d\n", 1102103285Sikob s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1103103285Sikob s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1104103285Sikob s->p0.power_class, s->p0.port0, s->p0.port1, 1105103285Sikob s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1106103285Sikob} 1107103285Sikob#endif 1108103285Sikob 1109103285Sikob/* 1110103285Sikob * To receive self ID. 1111103285Sikob */ 1112113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len) 1113103285Sikob{ 1114113584Ssimokawa u_int32_t *p; 1115103285Sikob union fw_self_id *self_id; 1116103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1117103285Sikob 1118103285Sikob fc->sid_cnt = len /(sizeof(u_int32_t) * 2); 1119103285Sikob fc->status = FWBUSINIT; 1120103285Sikob fc->max_node = fc->nodeid & 0x3f; 1121103285Sikob CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16; 1122103285Sikob fc->status = FWBUSCYMELECT; 1123103285Sikob fc->topology_map->crc_len = 2; 1124103285Sikob fc->topology_map->generation ++; 1125103285Sikob fc->topology_map->self_id_count = 0; 1126103285Sikob fc->topology_map->node_count = 0; 1127103285Sikob fc->speed_map->generation ++; 1128103285Sikob fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1129103285Sikob self_id = &fc->topology_map->self_id[0]; 1130103285Sikob for(i = 0; i < fc->sid_cnt; i ++){ 1131103285Sikob if (sid[1] != ~sid[0]) { 1132103285Sikob printf("fw_sidrcv: invalid self-id packet\n"); 1133103285Sikob sid += 2; 1134103285Sikob continue; 1135103285Sikob } 1136103285Sikob *self_id = *((union fw_self_id *)sid); 1137103285Sikob fc->topology_map->crc_len++; 1138103285Sikob if(self_id->p0.sequel == 0){ 1139103285Sikob fc->topology_map->node_count ++; 1140103285Sikob c_port = 0; 1141103285Sikob#if 0 1142103285Sikob fw_print_sid(sid[0]); 1143103285Sikob#endif 1144103285Sikob node = self_id->p0.phy_id; 1145103285Sikob if(fc->max_node < node){ 1146103285Sikob fc->max_node = self_id->p0.phy_id; 1147103285Sikob } 1148103285Sikob /* XXX I'm not sure this is the right speed_map */ 1149103285Sikob fc->speed_map->speed[node][node] 1150103285Sikob = self_id->p0.phy_speed; 1151103285Sikob for (j = 0; j < node; j ++) { 1152103285Sikob fc->speed_map->speed[j][node] 1153103285Sikob = fc->speed_map->speed[node][j] 1154103285Sikob = min(fc->speed_map->speed[j][j], 1155103285Sikob self_id->p0.phy_speed); 1156103285Sikob } 1157103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1158103285Sikob (self_id->p0.link_active && self_id->p0.contender)) { 1159103285Sikob fc->irm = self_id->p0.phy_id; 1160103285Sikob } 1161103285Sikob if(self_id->p0.port0 >= 0x2){ 1162103285Sikob c_port++; 1163103285Sikob } 1164103285Sikob if(self_id->p0.port1 >= 0x2){ 1165103285Sikob c_port++; 1166103285Sikob } 1167103285Sikob if(self_id->p0.port2 >= 0x2){ 1168103285Sikob c_port++; 1169103285Sikob } 1170103285Sikob } 1171103285Sikob if(c_port > 2){ 1172103285Sikob i_branch += (c_port - 2); 1173103285Sikob } 1174103285Sikob sid += 2; 1175103285Sikob self_id++; 1176103285Sikob fc->topology_map->self_id_count ++; 1177103285Sikob } 1178108655Ssimokawa device_printf(fc->bdev, "%d nodes", fc->max_node + 1); 1179103285Sikob /* CRC */ 1180103285Sikob fc->topology_map->crc = fw_crc16( 1181103285Sikob (u_int32_t *)&fc->topology_map->generation, 1182103285Sikob fc->topology_map->crc_len * 4); 1183103285Sikob fc->speed_map->crc = fw_crc16( 1184103285Sikob (u_int32_t *)&fc->speed_map->generation, 1185103285Sikob fc->speed_map->crc_len * 4); 1186103285Sikob /* byteswap and copy to CSR */ 1187103285Sikob p = (u_int32_t *)fc->topology_map; 1188103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1189103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1190103285Sikob p = (u_int32_t *)fc->speed_map; 1191103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1192103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1193103285Sikob /* don't byte-swap u_int8_t array */ 1194103285Sikob bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1195103285Sikob 1196103285Sikob fc->max_hop = fc->max_node - i_branch; 1197103285Sikob printf(", maxhop <= %d", fc->max_hop); 1198103285Sikob 1199103285Sikob if(fc->irm == -1 ){ 1200103285Sikob printf(", Not found IRM capable node"); 1201103285Sikob }else{ 1202103285Sikob printf(", cable IRM = %d", fc->irm); 1203103285Sikob if (fc->irm == fc->nodeid) 1204110016Ssimokawa printf(" (me)"); 1205103285Sikob } 1206110016Ssimokawa printf("\n"); 1207103285Sikob 1208109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1209114909Ssimokawa if (fc->irm == fc->nodeid) { 1210103285Sikob fc->status = FWBUSMGRDONE; 1211103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1212114909Ssimokawa fw_bmr(fc); 1213109736Ssimokawa } else { 1214103285Sikob fc->status = FWBUSMGRELECT; 1215110193Ssimokawa callout_reset(&fc->bmr_callout, hz/8, 1216110193Ssimokawa (void *)fw_try_bmr, (void *)fc); 1217103285Sikob } 1218114909Ssimokawa } else 1219103285Sikob fc->status = FWBUSMGRDONE; 1220114909Ssimokawa 1221108853Ssimokawa callout_reset(&fc->busprobe_callout, hz/4, 1222108853Ssimokawa (void *)fw_bus_probe, (void *)fc); 1223103285Sikob} 1224106790Ssimokawa 1225103285Sikob/* 1226103285Sikob * To probe devices on the IEEE1394 bus. 1227103285Sikob */ 1228106790Ssimokawastatic void 1229106790Ssimokawafw_bus_probe(struct firewire_comm *fc) 1230103285Sikob{ 1231103285Sikob int s; 1232103285Sikob struct fw_device *fwdev, *next; 1233103285Sikob 1234103285Sikob s = splfw(); 1235103285Sikob fc->status = FWBUSEXPLORE; 1236103285Sikob fc->retry_count = 0; 1237103285Sikob 1238103285Sikob/* 1239103285Sikob * Invalidate all devices, just after bus reset. Devices 1240103285Sikob * to be removed has not been seen longer time. 1241103285Sikob */ 1242110193Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1243110193Ssimokawa next = STAILQ_NEXT(fwdev, link); 1244110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1245103285Sikob fwdev->status = FWDEVINVAL; 1246103285Sikob fwdev->rcnt = 0; 1247110193Ssimokawa } else if(fwdev->rcnt < FW_MAXDEVRCNT) { 1248103285Sikob fwdev->rcnt ++; 1249110193Ssimokawa } else { 1250110193Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link); 1251110195Ssimokawa free(fwdev, M_FW); 1252103285Sikob } 1253103285Sikob } 1254103285Sikob fc->ongonode = 0; 1255103285Sikob fc->ongoaddr = CSRROMOFF; 1256103285Sikob fc->ongodev = NULL; 1257103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1258103285Sikob fw_bus_explore(fc); 1259103285Sikob splx(s); 1260103285Sikob} 1261106790Ssimokawa 1262103285Sikob/* 1263103285Sikob * To collect device informations on the IEEE1394 bus. 1264103285Sikob */ 1265106790Ssimokawastatic void 1266106790Ssimokawafw_bus_explore(struct firewire_comm *fc ) 1267103285Sikob{ 1268103285Sikob int err = 0; 1269110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1270103285Sikob u_int32_t addr; 1271103285Sikob struct fw_xfer *xfer; 1272103285Sikob struct fw_pkt *fp; 1273103285Sikob 1274103285Sikob if(fc->status != FWBUSEXPLORE) 1275103285Sikob return; 1276103285Sikob 1277103285Sikobloop: 1278103285Sikob if(fc->ongonode == fc->nodeid) fc->ongonode++; 1279103285Sikob 1280103285Sikob if(fc->ongonode > fc->max_node) goto done; 1281103285Sikob if(fc->ongonode >= 0x3f) goto done; 1282103285Sikob 1283103285Sikob /* check link */ 1284103285Sikob /* XXX we need to check phy_id first */ 1285103285Sikob if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) { 1286110577Ssimokawa if (firewire_debug) 1287110577Ssimokawa printf("node%d: link down\n", fc->ongonode); 1288103285Sikob fc->ongonode++; 1289103285Sikob goto loop; 1290103285Sikob } 1291103285Sikob 1292103285Sikob if(fc->ongoaddr <= CSRROMOFF && 1293103285Sikob fc->ongoeui.hi == 0xffffffff && 1294103285Sikob fc->ongoeui.lo == 0xffffffff ){ 1295103285Sikob fc->ongoaddr = CSRROMOFF; 1296103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1297103285Sikob }else if(fc->ongoeui.hi == 0xffffffff ){ 1298103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1299103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1300103285Sikob }else if(fc->ongoeui.lo == 0xffffffff ){ 1301103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1302103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1303103285Sikob }else if(fc->ongodev == NULL){ 1304110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1305110193Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui)) 1306103285Sikob break; 1307103285Sikob if(fwdev != NULL){ 1308103285Sikob fwdev->dst = fc->ongonode; 1309103285Sikob fwdev->status = FWDEVATTACHED; 1310103285Sikob fc->ongonode++; 1311103285Sikob fc->ongoaddr = CSRROMOFF; 1312103285Sikob fc->ongodev = NULL; 1313103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1314103285Sikob goto loop; 1315103285Sikob } 1316115787Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, 1317115787Ssimokawa M_NOWAIT | M_ZERO); 1318103285Sikob if(fwdev == NULL) 1319103285Sikob return; 1320106810Ssimokawa fwdev->fc = fc; 1321103285Sikob fwdev->rommax = 0; 1322103285Sikob fwdev->dst = fc->ongonode; 1323103285Sikob fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo; 1324103285Sikob fwdev->status = FWDEVINIT; 1325103285Sikob fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode]; 1326103285Sikob 1327110179Ssimokawa pfwdev = NULL; 1328110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1329110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1330110179Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1331110179Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1332110179Ssimokawa break; 1333110179Ssimokawa pfwdev = tfwdev; 1334103285Sikob } 1335110179Ssimokawa if (pfwdev == NULL) 1336110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1337110179Ssimokawa else 1338110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1339103285Sikob 1340108655Ssimokawa device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1341107653Ssimokawa linkspeed[fwdev->speed], 1342107653Ssimokawa fc->ongoeui.hi, fc->ongoeui.lo); 1343103285Sikob 1344103285Sikob fc->ongodev = fwdev; 1345103285Sikob fc->ongoaddr = CSRROMOFF; 1346103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1347103285Sikob }else{ 1348103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1349103285Sikob } 1350103285Sikob#if 0 1351103285Sikob xfer = asyreqq(fc, FWSPD_S100, 0, 0, 1352103285Sikob ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr, 1353103285Sikob fw_bus_explore_callback); 1354103285Sikob if(xfer == NULL) goto done; 1355103285Sikob#else 1356113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16); 1357103285Sikob if(xfer == NULL){ 1358103285Sikob goto done; 1359103285Sikob } 1360103285Sikob xfer->spd = 0; 1361103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1362113584Ssimokawa fp->mode.rreqq.dest_hi = 0xffff; 1363103285Sikob fp->mode.rreqq.tlrt = 0; 1364103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1365103285Sikob fp->mode.rreqq.pri = 0; 1366103285Sikob fp->mode.rreqq.src = 0; 1367103285Sikob xfer->dst = FWLOCALBUS | fc->ongonode; 1368113584Ssimokawa fp->mode.rreqq.dst = xfer->dst; 1369113584Ssimokawa fp->mode.rreqq.dest_lo = addr; 1370103285Sikob xfer->act.hand = fw_bus_explore_callback; 1371103285Sikob 1372110577Ssimokawa if (firewire_debug) 1373110577Ssimokawa printf("node%d: explore addr=0x%x\n", 1374110577Ssimokawa fc->ongonode, fc->ongoaddr); 1375103285Sikob err = fw_asyreq(fc, -1, xfer); 1376103285Sikob if(err){ 1377103285Sikob fw_xfer_free( xfer); 1378103285Sikob return; 1379103285Sikob } 1380103285Sikob#endif 1381103285Sikob return; 1382103285Sikobdone: 1383103285Sikob /* fw_attach_devs */ 1384103285Sikob fc->status = FWBUSEXPDONE; 1385107653Ssimokawa if (firewire_debug) 1386107653Ssimokawa printf("bus_explore done\n"); 1387103285Sikob fw_attach_dev(fc); 1388103285Sikob return; 1389103285Sikob 1390103285Sikob} 1391106790Ssimokawa 1392103285Sikob/* Portable Async. request read quad */ 1393106790Ssimokawastruct fw_xfer * 1394106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt, 1395105620Ssimokawa u_int32_t addr_hi, u_int32_t addr_lo, 1396105620Ssimokawa void (*hand) __P((struct fw_xfer*))) 1397103285Sikob{ 1398103285Sikob struct fw_xfer *xfer; 1399103285Sikob struct fw_pkt *fp; 1400103285Sikob int err; 1401103285Sikob 1402113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16); 1403113584Ssimokawa if (xfer == NULL) 1404103285Sikob return NULL; 1405113584Ssimokawa 1406103285Sikob xfer->spd = spd; /* XXX:min(spd, fc->spd) */ 1407103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1408113584Ssimokawa fp->mode.rreqq.dest_hi = addr_hi & 0xffff; 1409103285Sikob if(tl & FWP_TL_VALID){ 1410103285Sikob fp->mode.rreqq.tlrt = (tl & 0x3f) << 2; 1411103285Sikob }else{ 1412103285Sikob fp->mode.rreqq.tlrt = 0; 1413103285Sikob } 1414103285Sikob fp->mode.rreqq.tlrt |= rt & 0x3; 1415103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1416103285Sikob fp->mode.rreqq.pri = 0; 1417103285Sikob fp->mode.rreqq.src = 0; 1418103285Sikob xfer->dst = addr_hi >> 16; 1419113584Ssimokawa fp->mode.rreqq.dst = xfer->dst; 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 1461103285Sikob if(xfer->send.buf == NULL){ 1462110577Ssimokawa printf("node%d: send.buf=NULL addr=0x%x\n", 1463103285Sikob fc->ongonode, fc->ongoaddr); 1464114284Ssimokawa goto errnode; 1465103285Sikob } 1466103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1467103285Sikob 1468103285Sikob if(xfer->recv.buf == NULL){ 1469110577Ssimokawa printf("node%d: recv.buf=NULL addr=0x%x\n", 1470103285Sikob fc->ongonode, fc->ongoaddr); 1471114284Ssimokawa goto errnode; 1472103285Sikob } 1473103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1474103285Sikob#if 0 1475103285Sikob { 1476103285Sikob u_int32_t *qld; 1477103285Sikob int i; 1478103285Sikob qld = (u_int32_t *)xfer->recv.buf; 1479103285Sikob printf("len:%d\n", xfer->recv.len); 1480103285Sikob for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){ 1481113584Ssimokawa printf("0x%08x ", rfp->mode.ld[i/4]); 1482103285Sikob if((i % 16) == 15) printf("\n"); 1483103285Sikob } 1484103285Sikob if((i % 16) != 15) printf("\n"); 1485103285Sikob } 1486103285Sikob#endif 1487103285Sikob if(fc->ongodev == NULL){ 1488113584Ssimokawa if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){ 1489103285Sikob rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data); 1490103285Sikob chdr = (struct csrhdr *)(&rfp->mode.rresq.data); 1491110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */ 1492103285Sikob if(chdr->info_len == 1){ 1493110577Ssimokawa if (firewire_debug) 1494110577Ssimokawa printf("node%d: minimal config\n", 1495110577Ssimokawa fc->ongonode); 1496103285Sikob goto nextnode; 1497103285Sikob }else{ 1498103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1499103285Sikob } 1500113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){ 1501103285Sikob fc->ongoeui.hi = ntohl(rfp->mode.rresq.data); 1502103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1503113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){ 1504103285Sikob fc->ongoeui.lo = ntohl(rfp->mode.rresq.data); 1505110577Ssimokawa if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) { 1506110577Ssimokawa if (firewire_debug) 1507110577Ssimokawa printf("node%d: eui64 is zero.\n", 1508110577Ssimokawa fc->ongonode); 1509103285Sikob goto nextnode; 1510110577Ssimokawa } 1511103285Sikob fc->ongoaddr = CSRROMOFF; 1512103285Sikob } 1513103285Sikob }else{ 1514103285Sikob fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data); 1515103285Sikob if(fc->ongoaddr > fc->ongodev->rommax){ 1516103285Sikob fc->ongodev->rommax = fc->ongoaddr; 1517103285Sikob } 1518103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1519103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1520103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1521103285Sikob offset = CSRROMOFF; 1522103285Sikob }else{ 1523103285Sikob chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]; 1524103285Sikob offset = csrd->off; 1525103285Sikob } 1526103285Sikob if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){ 1527103285Sikob csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4]; 1528103285Sikob if( csrreg->key == 0x81 || csrreg->key == 0xd1){ 1529103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1530103285Sikob if(csrd == NULL){ 1531103285Sikob goto nextnode; 1532103285Sikob }else{ 1533103285Sikob csrd->ongoaddr = fc->ongoaddr; 1534103285Sikob fc->ongoaddr += csrreg->val * 4; 1535103285Sikob csrd->off = fc->ongoaddr; 1536103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1537103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1538103285Sikob goto nextaddr; 1539103285Sikob } 1540103285Sikob } 1541103285Sikob } 1542103285Sikob fc->ongoaddr += 4; 1543103285Sikob if(((fc->ongoaddr - offset)/4 > chdr->crc_len) && 1544103285Sikob (fc->ongodev->rommax < 0x414)){ 1545103285Sikob if(fc->ongodev->rommax <= 0x414){ 1546103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1547103285Sikob if(csrd == NULL) goto nextnode; 1548103285Sikob csrd->off = fc->ongoaddr; 1549103285Sikob csrd->ongoaddr = fc->ongoaddr; 1550103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1551103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1552103285Sikob } 1553103285Sikob goto nextaddr; 1554103285Sikob } 1555103285Sikob 1556103285Sikob while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){ 1557103285Sikob if(csrd == NULL){ 1558103285Sikob goto nextnode; 1559103285Sikob }; 1560103285Sikob fc->ongoaddr = csrd->ongoaddr + 4; 1561103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1562103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1563103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1564103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1565103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1566103285Sikob offset = CSRROMOFF; 1567103285Sikob }else{ 1568103285Sikob chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]); 1569103285Sikob offset = csrd->off; 1570103285Sikob } 1571103285Sikob } 1572103285Sikob if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){ 1573103285Sikob goto nextnode; 1574103285Sikob } 1575103285Sikob } 1576103285Sikobnextaddr: 1577103285Sikob fw_xfer_free( xfer); 1578103285Sikob fw_bus_explore(fc); 1579103285Sikob return; 1580114284Ssimokawaerrnode: 1581114284Ssimokawa fc->retry_count++; 1582114284Ssimokawa if (fc->ongodev != NULL) 1583114284Ssimokawa fc->ongodev->status = FWDEVINVAL; 1584103285Sikobnextnode: 1585103285Sikob fw_xfer_free( xfer); 1586103285Sikob fc->ongonode++; 1587103285Sikob/* housekeeping work space */ 1588103285Sikob fc->ongoaddr = CSRROMOFF; 1589103285Sikob fc->ongodev = NULL; 1590103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1591103285Sikob while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){ 1592103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1593103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1594103285Sikob } 1595103285Sikob fw_bus_explore(fc); 1596103285Sikob return; 1597103285Sikob} 1598103285Sikob 1599103285Sikob/* 1600103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1601103285Sikob */ 1602106815Ssimokawastatic void 1603106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1604103285Sikob{ 1605103285Sikob struct fw_device *fwdev; 1606103285Sikob struct fw_xfer *xfer; 1607103285Sikob int i, err; 1608103285Sikob device_t *devlistp; 1609103285Sikob int devcnt; 1610103285Sikob struct firewire_dev_comm *fdc; 1611103285Sikob 1612114069Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1613114069Ssimokawa if (fwdev->status == FWDEVINIT) 1614114069Ssimokawa fwdev->status = FWDEVATTACHED; 1615103285Sikob 1616108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1617103285Sikob if( err != 0 ) 1618103285Sikob return; 1619103285Sikob for( i = 0 ; i < devcnt ; i++){ 1620103285Sikob if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1621103285Sikob fdc = device_get_softc(devlistp[i]); 1622103285Sikob if (fdc->post_explore != NULL) 1623103285Sikob fdc->post_explore(fdc); 1624103285Sikob } 1625103285Sikob } 1626103285Sikob free(devlistp, M_TEMP); 1627103285Sikob 1628103285Sikob /* call pending handlers */ 1629103285Sikob i = 0; 1630103285Sikob while ((xfer = STAILQ_FIRST(&fc->pending))) { 1631103285Sikob STAILQ_REMOVE_HEAD(&fc->pending, link); 1632103285Sikob i++; 1633103285Sikob if (xfer->act.hand) 1634103285Sikob xfer->act.hand(xfer); 1635103285Sikob } 1636103285Sikob if (i > 0) 1637103285Sikob printf("fw_attach_dev: %d pending handlers called\n", i); 1638103285Sikob if (fc->retry_count > 0) { 1639111040Ssimokawa printf("probe failed for %d node\n", fc->retry_count); 1640111040Ssimokawa#if 0 1641110193Ssimokawa callout_reset(&fc->retry_probe_callout, hz*2, 1642110193Ssimokawa (void *)fc->ibr, (void *)fc); 1643111040Ssimokawa#endif 1644103285Sikob } 1645103285Sikob return; 1646103285Sikob} 1647106815Ssimokawa 1648103285Sikob/* 1649103285Sikob * To allocate uniq transaction label. 1650103285Sikob */ 1651106815Ssimokawastatic int 1652106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1653103285Sikob{ 1654103285Sikob u_int i; 1655103285Sikob struct tlabel *tl, *tmptl; 1656103285Sikob int s; 1657103285Sikob static u_int32_t label = 0; 1658103285Sikob 1659103285Sikob s = splfw(); 1660103285Sikob for( i = 0 ; i < 0x40 ; i ++){ 1661103285Sikob label = (label + 1) & 0x3f; 1662103285Sikob for(tmptl = STAILQ_FIRST(&fc->tlabels[label]); 1663103285Sikob tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){ 1664103285Sikob if(tmptl->xfer->dst == xfer->dst) break; 1665103285Sikob } 1666103285Sikob if(tmptl == NULL) { 1667110195Ssimokawa tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT); 1668103285Sikob if (tl == NULL) { 1669103285Sikob splx(s); 1670103285Sikob return (-1); 1671103285Sikob } 1672103285Sikob tl->xfer = xfer; 1673103285Sikob STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link); 1674103285Sikob splx(s); 1675110577Ssimokawa if (firewire_debug > 1) 1676110577Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", 1677110577Ssimokawa xfer->dst, label); 1678103285Sikob return(label); 1679103285Sikob } 1680103285Sikob } 1681103285Sikob splx(s); 1682103285Sikob 1683103285Sikob printf("fw_get_tlabel: no free tlabel\n"); 1684103285Sikob return(-1); 1685103285Sikob} 1686106815Ssimokawa 1687113584Ssimokawastatic void 1688113584Ssimokawafw_rcv_copy(struct fw_xfer *xfer, struct iovec *vec, int nvec) 1689113584Ssimokawa{ 1690113584Ssimokawa char *p; 1691113584Ssimokawa int res, i, len; 1692113584Ssimokawa 1693113584Ssimokawa p = xfer->recv.buf; 1694113584Ssimokawa res = xfer->recv.len; 1695113584Ssimokawa for (i = 0; i < nvec; i++, vec++) { 1696113584Ssimokawa len = vec->iov_len; 1697113584Ssimokawa if (res < len) { 1698113584Ssimokawa printf("rcv buffer(%d) is %d bytes short.\n", 1699113584Ssimokawa xfer->recv.len, len - res); 1700113584Ssimokawa len = res; 1701113584Ssimokawa } 1702113584Ssimokawa bcopy(vec->iov_base, p, len); 1703113584Ssimokawa p += len; 1704113584Ssimokawa res -= len; 1705113584Ssimokawa if (res <= 0) 1706113584Ssimokawa break; 1707113584Ssimokawa } 1708113584Ssimokawa xfer->recv.len -= res; 1709113584Ssimokawa} 1710113584Ssimokawa 1711103285Sikob/* 1712103285Sikob * Generic packet receving process. 1713103285Sikob */ 1714106815Ssimokawavoid 1715113584Ssimokawafw_rcv(struct firewire_comm *fc, struct iovec *vec, int nvec, u_int sub, u_int spd) 1716103285Sikob{ 1717103285Sikob struct fw_pkt *fp, *resfp; 1718103285Sikob struct fw_xfer *xfer; 1719103285Sikob struct fw_bind *bind; 1720103285Sikob struct firewire_softc *sc; 1721113584Ssimokawa int tcode, s; 1722113584Ssimokawa int i, len, oldstate; 1723103285Sikob#if 0 1724103285Sikob { 1725103285Sikob u_int32_t *qld; 1726103285Sikob int i; 1727103285Sikob qld = (u_int32_t *)buf; 1728103285Sikob printf("spd %d len:%d\n", spd, len); 1729103285Sikob for( i = 0 ; i <= len && i < 32; i+= 4){ 1730103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1731103285Sikob if((i % 16) == 15) printf("\n"); 1732103285Sikob } 1733103285Sikob if((i % 16) != 15) printf("\n"); 1734103285Sikob } 1735103285Sikob#endif 1736113584Ssimokawa fp = (struct fw_pkt *)vec[0].iov_base; 1737113584Ssimokawa tcode = fp->mode.common.tcode; 1738113584Ssimokawa#if 0 /* XXX this check is not valid for RRESQ and WREQQ */ 1739113584Ssimokawa if (vec[0].iov_len < fc->tcode[tcode].hdr_len) { 1740113584Ssimokawa#if __FreeBSD_version >= 500000 1741113584Ssimokawa printf("fw_rcv: iov_len(%zu) is less than" 1742113584Ssimokawa#else 1743113584Ssimokawa printf("fw_rcv: iov_len(%u) is less than" 1744113584Ssimokawa#endif 1745113584Ssimokawa " hdr_len(%d:tcode=%d)\n", vec[0].iov_len, 1746113584Ssimokawa fc->tcode[tcode].hdr_len, tcode); 1747113584Ssimokawa } 1748113584Ssimokawa#endif 1749113584Ssimokawa switch (tcode) { 1750103285Sikob case FWTCODE_WRES: 1751103285Sikob case FWTCODE_RRESQ: 1752103285Sikob case FWTCODE_RRESB: 1753103285Sikob case FWTCODE_LRES: 1754113584Ssimokawa xfer = fw_tl2xfer(fc, fp->mode.hdr.src, 1755103285Sikob fp->mode.hdr.tlrt >> 2); 1756103285Sikob if(xfer == NULL) { 1757103285Sikob printf("fw_rcv: unknown response " 1758110891Ssimokawa "tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n", 1759113584Ssimokawa tcode, 1760113584Ssimokawa fp->mode.hdr.src, 1761103285Sikob fp->mode.hdr.tlrt >> 2, 1762103285Sikob fp->mode.hdr.tlrt & 3, 1763103285Sikob fp->mode.rresq.data); 1764103285Sikob#if 1 1765103285Sikob printf("try ad-hoc work around!!\n"); 1766113584Ssimokawa xfer = fw_tl2xfer(fc, fp->mode.hdr.src, 1767103285Sikob (fp->mode.hdr.tlrt >> 2)^3); 1768103285Sikob if (xfer == NULL) { 1769103285Sikob printf("no use...\n"); 1770103285Sikob goto err; 1771103285Sikob } 1772103285Sikob#else 1773103285Sikob goto err; 1774103285Sikob#endif 1775103285Sikob } 1776113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1777113584Ssimokawa xfer->resp = 0; 1778113584Ssimokawa /* make sure the packet is drained in AT queue */ 1779113584Ssimokawa oldstate = xfer->state; 1780113584Ssimokawa xfer->state = FWXF_RCVD; 1781113584Ssimokawa switch (oldstate) { 1782113584Ssimokawa case FWXF_SENT: 1783103285Sikob fw_xfer_done(xfer); 1784103285Sikob break; 1785113584Ssimokawa case FWXF_START: 1786113584Ssimokawa if (firewire_debug) 1787113584Ssimokawa printf("not sent yet\n"); 1788113584Ssimokawa break; 1789103285Sikob default: 1790113584Ssimokawa printf("unexpected state %d\n", xfer->state); 1791103285Sikob } 1792113584Ssimokawa return; 1793103285Sikob case FWTCODE_WREQQ: 1794103285Sikob case FWTCODE_WREQB: 1795103285Sikob case FWTCODE_RREQQ: 1796103285Sikob case FWTCODE_RREQB: 1797103285Sikob case FWTCODE_LREQ: 1798113584Ssimokawa bind = fw_bindlookup(fc, fp->mode.rreqq.dest_hi, 1799113584Ssimokawa fp->mode.rreqq.dest_lo); 1800103285Sikob if(bind == NULL){ 1801113962Ssimokawa#if __FreeBSD_version >= 500000 1802113584Ssimokawa printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%x\n", 1803113962Ssimokawa#else 1804113962Ssimokawa printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%lx\n", 1805113962Ssimokawa#endif 1806113584Ssimokawa fp->mode.wreqq.dest_hi, 1807113584Ssimokawa fp->mode.wreqq.dest_lo, 1808113584Ssimokawa tcode, 1809113584Ssimokawa fp->mode.hdr.src, 1810113584Ssimokawa ntohl(fp->mode.wreqq.data)); 1811103285Sikob if (fc->status == FWBUSRESET) { 1812110798Ssimokawa printf("fw_rcv: cannot respond(bus reset)!\n"); 1813103285Sikob goto err; 1814103285Sikob } 1815113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 0); 1816103285Sikob if(xfer == NULL){ 1817103285Sikob return; 1818103285Sikob } 1819103285Sikob xfer->spd = spd; 1820103285Sikob resfp = (struct fw_pkt *)xfer->send.buf; 1821113584Ssimokawa switch (tcode) { 1822103285Sikob case FWTCODE_WREQQ: 1823103285Sikob case FWTCODE_WREQB: 1824103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 1825103285Sikob xfer->send.len = 12; 1826103285Sikob break; 1827103285Sikob case FWTCODE_RREQQ: 1828103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1829103285Sikob xfer->send.len = 16; 1830103285Sikob break; 1831103285Sikob case FWTCODE_RREQB: 1832103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 1833103285Sikob xfer->send.len = 16; 1834103285Sikob break; 1835103285Sikob case FWTCODE_LREQ: 1836103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 1837103285Sikob xfer->send.len = 16; 1838103285Sikob break; 1839103285Sikob } 1840103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 1841103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1842103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 1843103285Sikob resfp->mode.rresb.rtcode = 7; 1844103285Sikob resfp->mode.rresb.extcode = 0; 1845103285Sikob resfp->mode.rresb.len = 0; 1846103285Sikob/* 1847103285Sikob xfer->act.hand = fw_asy_callback; 1848103285Sikob*/ 1849103285Sikob xfer->act.hand = fw_xfer_free; 1850103285Sikob if(fw_asyreq(fc, -1, xfer)){ 1851103285Sikob fw_xfer_free( xfer); 1852103285Sikob return; 1853103285Sikob } 1854103285Sikob goto err; 1855103285Sikob } 1856113584Ssimokawa len = 0; 1857113584Ssimokawa for (i = 0; i < nvec; i ++) 1858113584Ssimokawa len += vec[i].iov_len; 1859113584Ssimokawa switch(bind->act_type){ 1860103285Sikob case FWACT_XFER: 1861113584Ssimokawa /* splfw()?? */ 1862113584Ssimokawa xfer = STAILQ_FIRST(&bind->xferlist); 1863113584Ssimokawa if (xfer == NULL) { 1864113584Ssimokawa printf("Discard a packet for this bind.\n"); 1865113584Ssimokawa goto err; 1866113584Ssimokawa } 1867113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1868113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1869103285Sikob xfer->spd = spd; 1870103285Sikob if (fc->status != FWBUSRESET) 1871103285Sikob xfer->act.hand(xfer); 1872103285Sikob else 1873103285Sikob STAILQ_INSERT_TAIL(&fc->pending, xfer, link); 1874103285Sikob return; 1875103285Sikob break; 1876103285Sikob case FWACT_CH: 1877113584Ssimokawa if(fc->ir[bind->sub]->queued >= 1878113584Ssimokawa fc->ir[bind->sub]->maxq){ 1879108655Ssimokawa device_printf(fc->bdev, 1880108655Ssimokawa "Discard a packet %x %d\n", 1881113584Ssimokawa bind->sub, 1882113584Ssimokawa fc->ir[bind->sub]->queued); 1883103285Sikob goto err; 1884103285Sikob } 1885113584Ssimokawa xfer = STAILQ_FIRST(&bind->xferlist); 1886113584Ssimokawa if (xfer == NULL) { 1887113584Ssimokawa printf("Discard packet for this bind\n"); 1888113584Ssimokawa goto err; 1889113584Ssimokawa } 1890113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1891113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1892103285Sikob xfer->spd = spd; 1893103285Sikob s = splfw(); 1894113584Ssimokawa fc->ir[bind->sub]->queued++; 1895113584Ssimokawa STAILQ_INSERT_TAIL(&fc->ir[bind->sub]->q, xfer, link); 1896103285Sikob splx(s); 1897103285Sikob 1898113584Ssimokawa wakeup((caddr_t)fc->ir[bind->sub]); 1899103285Sikob 1900103285Sikob return; 1901103285Sikob break; 1902103285Sikob default: 1903103285Sikob goto err; 1904103285Sikob break; 1905103285Sikob } 1906103285Sikob break; 1907103285Sikob case FWTCODE_STREAM: 1908103285Sikob { 1909103285Sikob struct fw_xferq *xferq; 1910103285Sikob 1911103285Sikob xferq = fc->ir[sub]; 1912103285Sikob#if 0 1913103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 1914103285Sikob sub, len, off, spd); 1915103285Sikob#endif 1916103285Sikob if(xferq->queued >= xferq->maxq) { 1917103285Sikob printf("receive queue is full\n"); 1918103285Sikob goto err; 1919103285Sikob } 1920113584Ssimokawa /* XXX get xfer from xfer queue, we don't need copy for 1921113584Ssimokawa per packet mode */ 1922113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 1923113584Ssimokawa vec[0].iov_len); 1924103285Sikob if(xfer == NULL) goto err; 1925113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1926103285Sikob xfer->spd = spd; 1927103285Sikob s = splfw(); 1928103285Sikob xferq->queued++; 1929103285Sikob STAILQ_INSERT_TAIL(&xferq->q, xfer, link); 1930103285Sikob splx(s); 1931103285Sikob sc = device_get_softc(fc->bdev); 1932103285Sikob#if __FreeBSD_version >= 500000 1933103285Sikob if (SEL_WAITING(&xferq->rsel)) 1934103285Sikob#else 1935103285Sikob if (&xferq->rsel.si_pid != 0) 1936103285Sikob#endif 1937103285Sikob selwakeup(&xferq->rsel); 1938103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 1939103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 1940103285Sikob wakeup((caddr_t)xferq); 1941103285Sikob } 1942103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 1943103285Sikob xferq->hand(xferq); 1944103285Sikob } 1945103285Sikob return; 1946103285Sikob break; 1947103285Sikob } 1948103285Sikob default: 1949113584Ssimokawa printf("fw_rcv: unknow tcode %d\n", tcode); 1950103285Sikob break; 1951103285Sikob } 1952103285Sikoberr: 1953113584Ssimokawa return; 1954103285Sikob} 1955106815Ssimokawa 1956103285Sikob/* 1957103285Sikob * Post process for Bus Manager election process. 1958103285Sikob */ 1959103285Sikobstatic void 1960103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 1961103285Sikob{ 1962109422Ssimokawa struct fw_pkt *rfp; 1963103285Sikob struct firewire_comm *fc; 1964109422Ssimokawa int bmr; 1965103285Sikob 1966109422Ssimokawa if (xfer == NULL) 1967109422Ssimokawa return; 1968103285Sikob fc = xfer->fc; 1969109422Ssimokawa if (xfer->resp != 0) 1970103285Sikob goto error; 1971109422Ssimokawa if (xfer->send.buf == NULL) 1972103285Sikob goto error; 1973109422Ssimokawa if (xfer->recv.buf == NULL) 1974103285Sikob goto error; 1975103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1976109422Ssimokawa if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE) 1977109422Ssimokawa goto error; 1978109422Ssimokawa 1979109422Ssimokawa bmr = ntohl(rfp->mode.lres.payload[0]); 1980109422Ssimokawa if (bmr == 0x3f) 1981109422Ssimokawa bmr = fc->nodeid; 1982109422Ssimokawa 1983109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 1984114909Ssimokawa fw_xfer_free(xfer); 1985114909Ssimokawa fw_bmr(fc); 1986114909Ssimokawa return; 1987114909Ssimokawa 1988103285Sikoberror: 1989114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 1990103285Sikob fw_xfer_free(xfer); 1991103285Sikob} 1992106815Ssimokawa 1993113584Ssimokawa 1994103285Sikob/* 1995103285Sikob * To candidate Bus Manager election process. 1996103285Sikob */ 1997110270Ssimokawastatic void 1998106815Ssimokawafw_try_bmr(void *arg) 1999103285Sikob{ 2000103285Sikob struct fw_xfer *xfer; 2001103285Sikob struct firewire_comm *fc = (struct firewire_comm *)arg; 2002103285Sikob struct fw_pkt *fp; 2003103285Sikob int err = 0; 2004103285Sikob 2005113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 24, 20); 2006103285Sikob if(xfer == NULL){ 2007103285Sikob return; 2008103285Sikob } 2009103285Sikob xfer->spd = 0; 2010103285Sikob fc->status = FWBUSMGRELECT; 2011103285Sikob 2012103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 2013113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 2014103285Sikob fp->mode.lreq.tlrt = 0; 2015103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 2016103285Sikob fp->mode.lreq.pri = 0; 2017103285Sikob fp->mode.lreq.src = 0; 2018113584Ssimokawa fp->mode.lreq.len = 8; 2019113584Ssimokawa fp->mode.lreq.extcode = FW_LREQ_CMPSWAP; 2020103285Sikob xfer->dst = FWLOCALBUS | fc->irm; 2021113584Ssimokawa fp->mode.lreq.dst = xfer->dst; 2022113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2023109422Ssimokawa fp->mode.lreq.payload[0] = htonl(0x3f); 2024109422Ssimokawa fp->mode.lreq.payload[1] = htonl(fc->nodeid); 2025103285Sikob xfer->act.hand = fw_try_bmr_callback; 2026103285Sikob 2027103285Sikob err = fw_asyreq(fc, -1, xfer); 2028103285Sikob if(err){ 2029103285Sikob fw_xfer_free( xfer); 2030103285Sikob return; 2031103285Sikob } 2032103285Sikob return; 2033103285Sikob} 2034106543Ssimokawa 2035106543Ssimokawa#ifdef FW_VMACCESS 2036103285Sikob/* 2037103285Sikob * Software implementation for physical memory block access. 2038103285Sikob * XXX:Too slow, usef for debug purpose only. 2039103285Sikob */ 2040106815Ssimokawastatic void 2041106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){ 2042103285Sikob struct fw_pkt *rfp, *sfp = NULL; 2043113584Ssimokawa u_int32_t *ld = (u_int32_t *)xfer->recv.buf; 2044103285Sikob 2045113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 2046113584Ssimokawa xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 2047103285Sikob printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2048103285Sikob if(xfer->resp != 0){ 2049103285Sikob fw_xfer_free( xfer); 2050103285Sikob return; 2051103285Sikob } 2052103285Sikob if(xfer->recv.buf == NULL){ 2053103285Sikob fw_xfer_free( xfer); 2054103285Sikob return; 2055103285Sikob } 2056103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 2057103285Sikob switch(rfp->mode.hdr.tcode){ 2058103285Sikob /* XXX need fix for 64bit arch */ 2059103285Sikob case FWTCODE_WREQB: 2060110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2061103285Sikob xfer->send.len = 12; 2062103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2063103285Sikob bcopy(rfp->mode.wreqb.payload, 2064103285Sikob (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 2065103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2066103285Sikob sfp->mode.wres.rtcode = 0; 2067103285Sikob break; 2068103285Sikob case FWTCODE_WREQQ: 2069110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2070103285Sikob xfer->send.len = 12; 2071103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2072103285Sikob *((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 2073103285Sikob sfp->mode.wres.rtcode = 0; 2074103285Sikob break; 2075103285Sikob case FWTCODE_RREQB: 2076110195Ssimokawa xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 2077103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2078103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2079103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2080103285Sikob sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len)); 2081103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 2082103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 2083103285Sikob sfp->mode.rresb.rtcode = 0; 2084103285Sikob sfp->mode.rresb.extcode = 0; 2085103285Sikob break; 2086103285Sikob case FWTCODE_RREQQ: 2087110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2088103285Sikob xfer->send.len = 16; 2089103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2090103285Sikob sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2091103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 2092103285Sikob sfp->mode.rresb.rtcode = 0; 2093103285Sikob break; 2094103285Sikob default: 2095103285Sikob fw_xfer_free( xfer); 2096103285Sikob return; 2097103285Sikob } 2098103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2099103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2100103285Sikob xfer->act.hand = fw_xfer_free; 2101103285Sikob xfer->retry_req = fw_asybusy; 2102103285Sikob 2103103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2104103285Sikob sfp->mode.hdr.pri = 0; 2105103285Sikob 2106103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2107103285Sikob/**/ 2108103285Sikob return; 2109103285Sikob} 2110106543Ssimokawa#endif 2111106543Ssimokawa 2112103285Sikob/* 2113103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2114103285Sikob */ 2115106815Ssimokawau_int16_t 2116106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){ 2117103285Sikob u_int32_t i, sum, crc = 0; 2118103285Sikob int shift; 2119103285Sikob len = (len + 3) & ~3; 2120103285Sikob for(i = 0 ; i < len ; i+= 4){ 2121103285Sikob for( shift = 28 ; shift >= 0 ; shift -= 4){ 2122103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2123103285Sikob crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2124103285Sikob } 2125103285Sikob crc &= 0xffff; 2126103285Sikob } 2127103285Sikob return((u_int16_t) crc); 2128103285Sikob} 2129106815Ssimokawa 2130110270Ssimokawastatic int 2131110072Ssimokawafw_bmr(struct firewire_comm *fc) 2132110072Ssimokawa{ 2133110072Ssimokawa struct fw_device fwdev; 2134113584Ssimokawa union fw_self_id *self_id; 2135110072Ssimokawa int cmstr; 2136110072Ssimokawa 2137113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2138113584Ssimokawa self_id = &fc->topology_map->self_id[fc->max_node]; 2139113584Ssimokawa if (fc->max_node > 0) { 2140114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2141114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2142113584Ssimokawa cmstr = fc->max_node; 2143114909Ssimokawa else { 2144114909Ssimokawa device_printf(fc->bdev, 2145114909Ssimokawa "root node is not cycle master capable\n"); 2146114909Ssimokawa /* XXX shall we be the cycle master? */ 2147113584Ssimokawa cmstr = fc->nodeid; 2148114909Ssimokawa /* XXX need bus reset */ 2149114909Ssimokawa } 2150113584Ssimokawa } else 2151113584Ssimokawa cmstr = -1; 2152114909Ssimokawa 2153114909Ssimokawa device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID)); 2154114909Ssimokawa if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2155114909Ssimokawa /* We are not the bus manager */ 2156114909Ssimokawa printf("\n"); 2157114909Ssimokawa return(0); 2158114909Ssimokawa } 2159114909Ssimokawa printf("(me)\n"); 2160114909Ssimokawa 2161114909Ssimokawa /* Optimize gapcount */ 2162113584Ssimokawa if(fc->max_hop <= MAX_GAPHOP ) 2163113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2164110072Ssimokawa /* If we are the cycle master, nothing to do */ 2165113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2166110072Ssimokawa return 0; 2167110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2168110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2169110072Ssimokawa fwdev.fc = fc; 2170110072Ssimokawa fwdev.dst = cmstr; 2171110072Ssimokawa fwdev.speed = 0; 2172110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2173110072Ssimokawa fwdev.status = FWDEVINIT; 2174110072Ssimokawa /* Set cmstr bit on the cycle master */ 2175110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2176114909Ssimokawa 0xffff, 0xf0000000 | STATE_SET, htonl(1 << 8), 2177110072Ssimokawa fw_asy_callback_free); 2178110072Ssimokawa 2179110072Ssimokawa return 0; 2180110072Ssimokawa} 2181110072Ssimokawa 2182103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0); 2183103285SikobMODULE_VERSION(firewire, 1); 2184