firewire.c revision 114909
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 114909 2003-05-11 10:32:20Z 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 62109736Ssimokawaint firewire_debug=0, try_bmr=1; 63103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 64108281Ssimokawa "FireWire driver debug flag"); 65109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 66109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 67109736Ssimokawa "Try to be a bus manager"); 68103285Sikob 69110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 70110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 71110195Ssimokawa 72103285Sikob#define FW_MAXASYRTY 4 73103285Sikob#define FW_MAXDEVRCNT 4 74103285Sikob 75103285Sikobdevclass_t firewire_devclass; 76103285Sikob 77103285Sikobstatic int firewire_match __P((device_t)); 78103285Sikobstatic int firewire_attach __P((device_t)); 79103285Sikobstatic int firewire_detach __P((device_t)); 80103285Sikob#if 0 81103285Sikobstatic int firewire_shutdown __P((device_t)); 82103285Sikob#endif 83103285Sikobstatic device_t firewire_add_child __P((device_t, int, const char *, int)); 84103285Sikobstatic void fw_try_bmr __P((void *)); 85103285Sikobstatic void fw_try_bmr_callback __P((struct fw_xfer *)); 86103285Sikobstatic void fw_asystart __P((struct fw_xfer *)); 87103285Sikobstatic int fw_get_tlabel __P((struct firewire_comm *, struct fw_xfer *)); 88103285Sikobstatic void fw_bus_probe __P((struct firewire_comm *)); 89103285Sikobstatic void fw_bus_explore __P((struct firewire_comm *)); 90103285Sikobstatic void fw_bus_explore_callback __P((struct fw_xfer *)); 91103285Sikobstatic void fw_attach_dev __P((struct firewire_comm *)); 92106543Ssimokawa#ifdef FW_VMACCESS 93103285Sikobstatic void fw_vmaccess __P((struct fw_xfer *)); 94106543Ssimokawa#endif 95103285Sikobstruct fw_xfer *asyreqq __P((struct firewire_comm *, u_int8_t, u_int8_t, u_int8_t, 96105620Ssimokawa u_int32_t, u_int32_t, void (*)__P((struct fw_xfer *)))); 97110072Ssimokawastatic int fw_bmr __P((struct firewire_comm *)); 98103285Sikob 99103285Sikobstatic device_method_t firewire_methods[] = { 100103285Sikob /* Device interface */ 101103285Sikob DEVMETHOD(device_probe, firewire_match), 102103285Sikob DEVMETHOD(device_attach, firewire_attach), 103103285Sikob DEVMETHOD(device_detach, firewire_detach), 104108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 105108642Ssimokawa DEVMETHOD(device_resume, bus_generic_resume), 106103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 107103285Sikob 108103285Sikob /* Bus interface */ 109103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 110103285Sikob DEVMETHOD(bus_print_child, bus_generic_print_child), 111103285Sikob 112103285Sikob { 0, 0 } 113103285Sikob}; 114103285Sikobchar linkspeed[7][0x10]={"S100","S200","S400","S800","S1600","S3200","Unknown"}; 115103285Sikob 116114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 117114909Ssimokawa#define MAX_GAPHOP 15 118114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 119114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 120106813Ssimokawa 121106813Ssimokawaextern struct cdevsw firewire_cdevsw; 122106813Ssimokawa 123103285Sikobstatic driver_t firewire_driver = { 124103285Sikob "firewire", 125103285Sikob firewire_methods, 126103285Sikob sizeof(struct firewire_softc), 127103285Sikob}; 128103285Sikob 129103285Sikob/* 130110072Ssimokawa * Lookup fwdev by node id. 131103285Sikob */ 132106810Ssimokawastruct fw_device * 133110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 134103285Sikob{ 135103285Sikob struct fw_device *fwdev; 136110072Ssimokawa int s; 137110072Ssimokawa 138110072Ssimokawa s = splfw(); 139110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 140110072Ssimokawa if (fwdev->dst == dst) 141103285Sikob break; 142110072Ssimokawa splx(s); 143110072Ssimokawa 144106810Ssimokawa if(fwdev == NULL) return NULL; 145106810Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 146106810Ssimokawa return fwdev; 147103285Sikob} 148106813Ssimokawa 149103285Sikob/* 150110072Ssimokawa * Lookup fwdev by EUI64. 151110072Ssimokawa */ 152110072Ssimokawastruct fw_device * 153110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 154110072Ssimokawa{ 155110072Ssimokawa struct fw_device *fwdev; 156110072Ssimokawa int s; 157110072Ssimokawa 158110072Ssimokawa s = splfw(); 159110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 160110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 161110072Ssimokawa break; 162110072Ssimokawa splx(s); 163110072Ssimokawa 164110072Ssimokawa if(fwdev == NULL) return NULL; 165110072Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 166110072Ssimokawa return fwdev; 167110072Ssimokawa} 168110072Ssimokawa 169110072Ssimokawa/* 170103285Sikob * Async. request procedure for userland application. 171103285Sikob */ 172103285Sikobint 173103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 174103285Sikob{ 175103285Sikob int err = 0; 176103285Sikob struct fw_xferq *xferq; 177103285Sikob int tl = 0, len; 178103285Sikob struct fw_pkt *fp; 179103285Sikob int tcode; 180103285Sikob struct tcode_info *info; 181103285Sikob 182103285Sikob if(xfer == NULL) return EINVAL; 183108701Ssimokawa if(xfer->send.len > MAXREC(fc->maxrec)){ 184103285Sikob printf("send.len > maxrec\n"); 185103285Sikob return EINVAL; 186103285Sikob } 187103285Sikob if(xfer->act.hand == NULL){ 188103285Sikob printf("act.hand == NULL\n"); 189103285Sikob return EINVAL; 190103285Sikob } 191103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 192103285Sikob 193103285Sikob tcode = fp->mode.common.tcode & 0xf; 194103285Sikob info = &fc->tcode[tcode]; 195103285Sikob if (info->flag == 0) { 196103285Sikob printf("invalid tcode=%d\n", tcode); 197103285Sikob return EINVAL; 198103285Sikob } 199103285Sikob if (info->flag & FWTI_REQ) 200103285Sikob xferq = fc->atq; 201103285Sikob else 202103285Sikob xferq = fc->ats; 203103285Sikob len = info->hdr_len; 204103285Sikob if (info->flag & FWTI_BLOCK_STR) 205113584Ssimokawa len += fp->mode.stream.len; 206103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 207113584Ssimokawa len += fp->mode.rresb.len; 208103285Sikob if( len > xfer->send.len ){ 209103285Sikob printf("len(%d) > send.len(%d) (tcode=%d)\n", 210103285Sikob len, xfer->send.len, tcode); 211103285Sikob return EINVAL; 212103285Sikob } 213103285Sikob xfer->send.len = len; 214106790Ssimokawa 215103285Sikob if(xferq->start == NULL){ 216103285Sikob printf("xferq->start == NULL\n"); 217103285Sikob return EINVAL; 218103285Sikob } 219103285Sikob if(!(xferq->queued < xferq->maxq)){ 220108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 221108655Ssimokawa xferq->queued); 222103285Sikob return EINVAL; 223103285Sikob } 224103285Sikob 225103285Sikob 226103285Sikob if (info->flag & FWTI_TLABEL) { 227103285Sikob if((tl = fw_get_tlabel(fc, xfer)) == -1 ) 228103285Sikob return EIO; 229103285Sikob fp->mode.hdr.tlrt = tl << 2; 230103285Sikob } 231103285Sikob 232103285Sikob xfer->tl = tl; 233103285Sikob xfer->resp = 0; 234103285Sikob xfer->fc = fc; 235103285Sikob xfer->q = xferq; 236103285Sikob xfer->retry_req = fw_asybusy; 237103285Sikob 238103285Sikob fw_asystart(xfer); 239103285Sikob return err; 240103285Sikob} 241103285Sikob/* 242103285Sikob * Wakeup blocked process. 243103285Sikob */ 244103285Sikobvoid 245103285Sikobfw_asy_callback(struct fw_xfer *xfer){ 246103285Sikob wakeup(xfer); 247103285Sikob return; 248103285Sikob} 249103285Sikob/* 250103285Sikob * Postpone to later retry. 251103285Sikob */ 252103285Sikobvoid fw_asybusy(struct fw_xfer *xfer){ 253103285Sikob printf("fw_asybusy\n"); 254103285Sikob/* 255103285Sikob xfer->ch = timeout((timeout_t *)fw_asystart, (void *)xfer, 20000); 256103285Sikob*/ 257103285Sikob DELAY(20000); 258103285Sikob fw_asystart(xfer); 259103285Sikob return; 260103285Sikob} 261103285Sikob 262103285Sikob/* 263103285Sikob * Async. request with given xfer structure. 264103285Sikob */ 265106790Ssimokawastatic void 266106790Ssimokawafw_asystart(struct fw_xfer *xfer) 267106790Ssimokawa{ 268103285Sikob struct firewire_comm *fc = xfer->fc; 269103285Sikob int s; 270103285Sikob if(xfer->retry++ >= fc->max_asyretry){ 271113584Ssimokawa device_printf(fc->bdev, "max_asyretry exceeded\n"); 272103285Sikob xfer->resp = EBUSY; 273103285Sikob xfer->state = FWXF_BUSY; 274103285Sikob xfer->act.hand(xfer); 275103285Sikob return; 276103285Sikob } 277103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */ 278103285Sikob if (fc->status < FWBUSEXPLORE) { 279103285Sikob xfer->resp = EAGAIN; 280103285Sikob xfer->state = FWXF_BUSY; 281103285Sikob if (xfer->act.hand != NULL) 282103285Sikob xfer->act.hand(xfer); 283103285Sikob return; 284103285Sikob } 285103285Sikob#endif 286103285Sikob s = splfw(); 287103285Sikob xfer->state = FWXF_INQ; 288103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 289103285Sikob xfer->q->queued ++; 290103285Sikob splx(s); 291103285Sikob /* XXX just queue for mbuf */ 292103285Sikob if (xfer->mbuf == NULL) 293103285Sikob xfer->q->start(fc); 294103285Sikob return; 295103285Sikob} 296106790Ssimokawa 297103285Sikobstatic int 298103285Sikobfirewire_match( device_t dev ) 299103285Sikob{ 300108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 301103285Sikob return -140; 302103285Sikob} 303106790Ssimokawa 304110577Ssimokawastatic void 305110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc) 306110577Ssimokawa{ 307110577Ssimokawa struct fw_xfer *xfer; 308110577Ssimokawa struct tlabel *tl; 309110577Ssimokawa struct timeval tv; 310110577Ssimokawa struct timeval split_timeout; 311111040Ssimokawa int i, s; 312110577Ssimokawa 313110891Ssimokawa split_timeout.tv_sec = 6; 314110577Ssimokawa split_timeout.tv_usec = 0; 315110577Ssimokawa 316110577Ssimokawa microtime(&tv); 317110577Ssimokawa timevalsub(&tv, &split_timeout); 318110577Ssimokawa 319111040Ssimokawa s = splfw(); 320110577Ssimokawa for (i = 0; i < 0x40; i ++) { 321110577Ssimokawa while ((tl = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 322110577Ssimokawa xfer = tl->xfer; 323110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 324110577Ssimokawa /* the rests are newer than this */ 325110577Ssimokawa break; 326110577Ssimokawa device_printf(fc->bdev, 327110891Ssimokawa "split transaction timeout dst=0x%x tl=0x%x\n", 328110577Ssimokawa xfer->dst, i); 329110577Ssimokawa xfer->resp = ETIMEDOUT; 330110577Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], link); 331113584Ssimokawa fw_xfer_done(xfer); 332110577Ssimokawa } 333110577Ssimokawa } 334111040Ssimokawa splx(s); 335110577Ssimokawa} 336110577Ssimokawa 337110577Ssimokawastatic void 338110577Ssimokawafirewire_watchdog(void *arg) 339110577Ssimokawa{ 340110577Ssimokawa struct firewire_comm *fc; 341110577Ssimokawa 342110577Ssimokawa fc = (struct firewire_comm *)arg; 343110577Ssimokawa firewire_xfer_timeout(fc); 344110577Ssimokawa fc->timeout(fc); 345110577Ssimokawa callout_reset(&fc->timeout_callout, hz, 346110577Ssimokawa (void *)firewire_watchdog, (void *)fc); 347110577Ssimokawa} 348110577Ssimokawa 349103285Sikob/* 350103285Sikob * The attach routine. 351103285Sikob */ 352103285Sikobstatic int 353103285Sikobfirewire_attach( device_t dev ) 354103285Sikob{ 355103285Sikob int i, unitmask, mn; 356103285Sikob struct firewire_softc *sc = device_get_softc(dev); 357103285Sikob device_t pa = device_get_parent(dev); 358103285Sikob struct firewire_comm *fc; 359103285Sikob dev_t d; 360103285Sikob 361103285Sikob fc = (struct firewire_comm *)device_get_softc(pa); 362103285Sikob sc->fc = fc; 363111074Ssimokawa fc->status = -1; 364103285Sikob 365103285Sikob unitmask = UNIT2MIN(device_get_unit(dev)); 366103285Sikob 367103285Sikob if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 368103285Sikob for ( i = 0 ; i < fc->nisodma ; i++ ){ 369103285Sikob mn = unitmask | i; 370103285Sikob /* XXX device name should be improved */ 371103285Sikob d = make_dev(&firewire_cdevsw, unit2minor(mn), 372108276Ssimokawa UID_ROOT, GID_OPERATOR, 0660, 373103285Sikob "fw%x", mn); 374103285Sikob#if __FreeBSD_version >= 500000 375103285Sikob if (i == 0) 376103285Sikob sc->dev = d; 377103285Sikob else 378103285Sikob dev_depends(sc->dev, d); 379103285Sikob#else 380103285Sikob sc->dev[i] = d; 381103285Sikob#endif 382103285Sikob } 383103285Sikob d = make_dev(&firewire_cdevsw, unit2minor(unitmask | FWMEM_FLAG), 384108276Ssimokawa UID_ROOT, GID_OPERATOR, 0660, 385103285Sikob "fwmem%d", device_get_unit(dev)); 386103285Sikob#if __FreeBSD_version >= 500000 387103285Sikob dev_depends(sc->dev, d); 388103285Sikob#else 389103285Sikob sc->dev[i] = d; 390103285Sikob#endif 391110193Ssimokawa CALLOUT_INIT(&sc->fc->timeout_callout); 392110193Ssimokawa CALLOUT_INIT(&sc->fc->bmr_callout); 393110193Ssimokawa CALLOUT_INIT(&sc->fc->retry_probe_callout); 394110193Ssimokawa CALLOUT_INIT(&sc->fc->busprobe_callout); 395108853Ssimokawa 396110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 397110577Ssimokawa (void *)firewire_watchdog, (void *)sc->fc); 398110193Ssimokawa 399103285Sikob /* Locate our children */ 400103285Sikob bus_generic_probe(dev); 401103285Sikob 402103285Sikob /* launch attachement of the added children */ 403103285Sikob bus_generic_attach(dev); 404103285Sikob 405103285Sikob /* bus_reset */ 406103285Sikob fc->ibr(fc); 407103285Sikob 408103285Sikob return 0; 409103285Sikob} 410103285Sikob 411103285Sikob/* 412103285Sikob * Attach it as child. 413103285Sikob */ 414103285Sikobstatic device_t 415103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit) 416103285Sikob{ 417103285Sikob device_t child; 418103285Sikob struct firewire_softc *sc; 419103285Sikob 420103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 421103285Sikob child = device_add_child(dev, name, unit); 422103285Sikob if (child) { 423103285Sikob device_set_ivars(child, sc->fc); 424103285Sikob device_probe_and_attach(child); 425103285Sikob } 426103285Sikob 427103285Sikob return child; 428103285Sikob} 429106790Ssimokawa 430103285Sikob/* 431103285Sikob * Dettach it. 432103285Sikob */ 433103285Sikobstatic int 434103285Sikobfirewire_detach( device_t dev ) 435103285Sikob{ 436103285Sikob struct firewire_softc *sc; 437111074Ssimokawa struct csrdir *csrd, *next; 438111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 439103285Sikob 440103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 441106790Ssimokawa 442111078Ssimokawa bus_generic_detach(dev); 443111078Ssimokawa 444111078Ssimokawa callout_stop(&sc->fc->timeout_callout); 445111078Ssimokawa callout_stop(&sc->fc->bmr_callout); 446111078Ssimokawa callout_stop(&sc->fc->retry_probe_callout); 447111078Ssimokawa callout_stop(&sc->fc->busprobe_callout); 448111078Ssimokawa 449103285Sikob#if __FreeBSD_version >= 500000 450103285Sikob destroy_dev(sc->dev); 451103285Sikob#else 452103285Sikob { 453103285Sikob int j; 454103285Sikob for (j = 0 ; j < sc->fc->nisodma + 1; j++) 455103285Sikob destroy_dev(sc->dev[j]); 456103285Sikob } 457103285Sikob#endif 458103285Sikob /* XXX xfree_free and untimeout on all xfers */ 459111078Ssimokawa for (fwdev = STAILQ_FIRST(&sc->fc->devices); fwdev != NULL; 460111078Ssimokawa fwdev = fwdev_next) { 461111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 462111078Ssimokawa free(fwdev, M_FW); 463111078Ssimokawa } 464111074Ssimokawa for (csrd = SLIST_FIRST(&sc->fc->csrfree); csrd != NULL; csrd = next) { 465111074Ssimokawa next = SLIST_NEXT(csrd, link); 466111074Ssimokawa free(csrd, M_FW); 467111074Ssimokawa } 468110195Ssimokawa free(sc->fc->topology_map, M_FW); 469110195Ssimokawa free(sc->fc->speed_map, M_FW); 470103285Sikob return(0); 471103285Sikob} 472103285Sikob#if 0 473103285Sikobstatic int 474103285Sikobfirewire_shutdown( device_t dev ) 475103285Sikob{ 476103285Sikob return 0; 477103285Sikob} 478103285Sikob#endif 479106790Ssimokawa 480110577Ssimokawa 481110577Ssimokawastatic void 482110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 483110577Ssimokawa{ 484110577Ssimokawa struct fw_xfer *xfer; 485110577Ssimokawa 486110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 487110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 488111942Ssimokawa xferq->queued --; 489110577Ssimokawa xfer->resp = EAGAIN; 490113584Ssimokawa fw_xfer_done(xfer); 491110577Ssimokawa } 492110577Ssimokawa} 493110577Ssimokawa 494110798Ssimokawavoid 495110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 496110798Ssimokawa{ 497110798Ssimokawa int i; 498110798Ssimokawa 499110798Ssimokawa fw_xferq_drain(fc->atq); 500110798Ssimokawa fw_xferq_drain(fc->ats); 501110798Ssimokawa for(i = 0; i < fc->nisodma; i++) 502110798Ssimokawa fw_xferq_drain(fc->it[i]); 503110798Ssimokawa} 504110798Ssimokawa 505103285Sikob/* 506106790Ssimokawa * Called after bus reset. 507103285Sikob */ 508106790Ssimokawavoid 509106790Ssimokawafw_busreset(struct firewire_comm *fc) 510103285Sikob{ 511113584Ssimokawa struct firewire_dev_comm *fdc; 512113584Ssimokawa device_t *devlistp; 513113584Ssimokawa int devcnt; 514103285Sikob int i; 515103285Sikob 516103285Sikob switch(fc->status){ 517103285Sikob case FWBUSMGRELECT: 518110193Ssimokawa callout_stop(&fc->bmr_callout); 519103285Sikob break; 520103285Sikob default: 521103285Sikob break; 522103285Sikob } 523103285Sikob fc->status = FWBUSRESET; 524103285Sikob CSRARC(fc, STATE_CLEAR) 525103285Sikob = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 526103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 527103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 528103285Sikob 529103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 530103285Sikob fc->irm = -1; 531103285Sikob 532103285Sikob fc->max_node = -1; 533103285Sikob 534103285Sikob for(i = 2; i < 0x100/4 - 2 ; i++){ 535103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 536103285Sikob } 537103285Sikob CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 538103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 539103285Sikob CSRARC(fc, RESET_START) = 0; 540103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 541103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 542103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 543103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 544103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 545103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 546103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 547103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 548103285Sikob CSRARC(fc, IP_CHANNELS) = (1 << 31); 549103285Sikob 550103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 551103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 552103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 553103285Sikob 1 << 28 | 0xff << 16 | 0x09 << 8; 554103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 555103285Sikob 556103285Sikob/* DV depend CSRs see blue book */ 557103285Sikob CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 558103285Sikob CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 559103285Sikob 560103285Sikob CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 ); 561103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 562113584Ssimokawa 563113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 564113584Ssimokawa for( i = 0 ; i < devcnt ; i++) 565113584Ssimokawa if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 566113584Ssimokawa fdc = device_get_softc(devlistp[i]); 567113584Ssimokawa if (fdc->post_busreset != NULL) 568113584Ssimokawa fdc->post_busreset(fdc); 569113584Ssimokawa } 570113584Ssimokawa free(devlistp, M_TEMP); 571113584Ssimokawa } 572103285Sikob} 573106790Ssimokawa 574103285Sikob/* Call once after reboot */ 575106790Ssimokawavoid fw_init(struct firewire_comm *fc) 576103285Sikob{ 577103285Sikob int i; 578106543Ssimokawa struct csrdir *csrd; 579106543Ssimokawa#ifdef FW_VMACCESS 580103285Sikob struct fw_xfer *xfer; 581103285Sikob struct fw_bind *fwb; 582106543Ssimokawa#endif 583103285Sikob 584103285Sikob fc->max_asyretry = FW_MAXASYRTY; 585103285Sikob 586103285Sikob fc->arq->queued = 0; 587103285Sikob fc->ars->queued = 0; 588103285Sikob fc->atq->queued = 0; 589103285Sikob fc->ats->queued = 0; 590103285Sikob 591103285Sikob fc->arq->buf = NULL; 592103285Sikob fc->ars->buf = NULL; 593103285Sikob fc->atq->buf = NULL; 594103285Sikob fc->ats->buf = NULL; 595103285Sikob 596113584Ssimokawa fc->arq->flag = 0; 597113584Ssimokawa fc->ars->flag = 0; 598113584Ssimokawa fc->atq->flag = 0; 599113584Ssimokawa fc->ats->flag = 0; 600103285Sikob 601103285Sikob STAILQ_INIT(&fc->atq->q); 602103285Sikob STAILQ_INIT(&fc->ats->q); 603103285Sikob 604103285Sikob for( i = 0 ; i < fc->nisodma ; i ++ ){ 605103285Sikob fc->it[i]->queued = 0; 606103285Sikob fc->ir[i]->queued = 0; 607103285Sikob 608103285Sikob fc->it[i]->start = NULL; 609103285Sikob fc->ir[i]->start = NULL; 610103285Sikob 611103285Sikob fc->it[i]->buf = NULL; 612103285Sikob fc->ir[i]->buf = NULL; 613103285Sikob 614103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 615103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 616103285Sikob 617103285Sikob STAILQ_INIT(&fc->it[i]->q); 618103285Sikob STAILQ_INIT(&fc->ir[i]->q); 619103285Sikob 620103285Sikob STAILQ_INIT(&fc->it[i]->binds); 621103285Sikob STAILQ_INIT(&fc->ir[i]->binds); 622103285Sikob } 623103285Sikob 624103285Sikob fc->arq->maxq = FWMAXQUEUE; 625103285Sikob fc->ars->maxq = FWMAXQUEUE; 626103285Sikob fc->atq->maxq = FWMAXQUEUE; 627103285Sikob fc->ats->maxq = FWMAXQUEUE; 628103285Sikob 629103285Sikob for( i = 0 ; i < fc->nisodma ; i++){ 630103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 631103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 632103285Sikob } 633103285Sikob/* Initialize csr registers */ 634103285Sikob fc->topology_map = (struct fw_topology_map *)malloc( 635103285Sikob sizeof(struct fw_topology_map), 636110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 637103285Sikob fc->speed_map = (struct fw_speed_map *)malloc( 638103285Sikob sizeof(struct fw_speed_map), 639110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 640103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 641103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 642103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 643103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 644103285Sikob 645110193Ssimokawa STAILQ_INIT(&fc->devices); 646103285Sikob STAILQ_INIT(&fc->pending); 647103285Sikob 648103285Sikob/* Initialize csr ROM work space */ 649103285Sikob SLIST_INIT(&fc->ongocsr); 650103285Sikob SLIST_INIT(&fc->csrfree); 651103285Sikob for( i = 0 ; i < FWMAXCSRDIR ; i++){ 652110195Ssimokawa csrd = (struct csrdir *) malloc(sizeof(struct csrdir), M_FW,M_NOWAIT); 653103285Sikob if(csrd == NULL) break; 654103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 655103285Sikob } 656103285Sikob 657103285Sikob/* Initialize Async handlers */ 658103285Sikob STAILQ_INIT(&fc->binds); 659103285Sikob for( i = 0 ; i < 0x40 ; i++){ 660103285Sikob STAILQ_INIT(&fc->tlabels[i]); 661103285Sikob } 662103285Sikob 663103285Sikob/* DV depend CSRs see blue book */ 664103285Sikob#if 0 665103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 666103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 667103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 668103285Sikob CSRARC(fc, i + oPCR) = 0x8000007a; 669103285Sikob } 670103285Sikob 671103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 672103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 673103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 674103285Sikob CSRARC(fc, i + iPCR) = 0x0; 675103285Sikob } 676103285Sikob#endif 677103285Sikob 678103285Sikob 679106543Ssimokawa#ifdef FW_VMACCESS 680103285Sikob xfer = fw_xfer_alloc(); 681103285Sikob if(xfer == NULL) return; 682103285Sikob 683110195Ssimokawa fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 684103285Sikob if(fwb == NULL){ 685103285Sikob fw_xfer_free(xfer); 686103285Sikob } 687103285Sikob xfer->act.hand = fw_vmaccess; 688103285Sikob xfer->fc = fc; 689103285Sikob xfer->sc = NULL; 690103285Sikob 691103285Sikob fwb->start_hi = 0x2; 692103285Sikob fwb->start_lo = 0; 693103285Sikob fwb->addrlen = 0xffffffff; 694103285Sikob fwb->xfer = xfer; 695103285Sikob fw_bindadd(fc, fwb); 696106543Ssimokawa#endif 697103285Sikob} 698106790Ssimokawa 699103285Sikob/* 700103285Sikob * To lookup binded process from IEEE1394 address. 701103285Sikob */ 702106813Ssimokawastruct fw_bind * 703106790Ssimokawafw_bindlookup(struct firewire_comm *fc, u_int32_t dest_hi, u_int32_t dest_lo) 704103285Sikob{ 705103285Sikob struct fw_bind *tfw; 706103285Sikob for(tfw = STAILQ_FIRST(&fc->binds) ; tfw != NULL ; 707103285Sikob tfw = STAILQ_NEXT(tfw, fclist)){ 708113584Ssimokawa if (tfw->act_type != FWACT_NULL && 709103285Sikob tfw->start_hi == dest_hi && 710103285Sikob tfw->start_lo <= dest_lo && 711103285Sikob (tfw->start_lo + tfw->addrlen) > dest_lo){ 712103285Sikob return(tfw); 713103285Sikob } 714103285Sikob } 715103285Sikob return(NULL); 716103285Sikob} 717106790Ssimokawa 718103285Sikob/* 719103285Sikob * To bind IEEE1394 address block to process. 720103285Sikob */ 721106790Ssimokawaint 722106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 723103285Sikob{ 724103285Sikob struct fw_bind *tfw, *tfw2 = NULL; 725103285Sikob int err = 0; 726103285Sikob tfw = STAILQ_FIRST(&fc->binds); 727103285Sikob if(tfw == NULL){ 728103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 729103285Sikob goto out; 730103285Sikob } 731103285Sikob if((tfw->start_hi > fwb->start_hi) || 732103285Sikob (tfw->start_hi == fwb->start_hi && 733103285Sikob (tfw->start_lo > (fwb->start_lo + fwb->addrlen)))){ 734103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 735103285Sikob goto out; 736103285Sikob } 737103285Sikob for(; tfw != NULL; tfw = STAILQ_NEXT(tfw, fclist)){ 738103285Sikob if((tfw->start_hi < fwb->start_hi) || 739103285Sikob (tfw->start_hi == fwb->start_hi && 740103285Sikob (tfw->start_lo + tfw->addrlen) < fwb->start_lo)){ 741103285Sikob tfw2 = STAILQ_NEXT(tfw, fclist); 742103285Sikob if(tfw2 == NULL) 743103285Sikob break; 744103285Sikob if((tfw2->start_hi > fwb->start_hi) || 745103285Sikob (tfw2->start_hi == fwb->start_hi && 746103285Sikob tfw2->start_lo > (fwb->start_lo + fwb->addrlen))){ 747103285Sikob break; 748103285Sikob }else{ 749103285Sikob err = EBUSY; 750103285Sikob goto out; 751103285Sikob } 752103285Sikob } 753103285Sikob } 754103285Sikob if(tfw != NULL){ 755103285Sikob STAILQ_INSERT_AFTER(&fc->binds, tfw, fwb, fclist); 756103285Sikob }else{ 757103285Sikob STAILQ_INSERT_TAIL(&fc->binds, fwb, fclist); 758103285Sikob } 759103285Sikobout: 760113584Ssimokawa if (!err && fwb->act_type == FWACT_CH) 761113584Ssimokawa STAILQ_INSERT_HEAD(&fc->ir[fwb->sub]->binds, fwb, chlist); 762103285Sikob return err; 763103285Sikob} 764103285Sikob 765103285Sikob/* 766103285Sikob * To free IEEE1394 address block. 767103285Sikob */ 768106790Ssimokawaint 769106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 770103285Sikob{ 771103285Sikob int s; 772113584Ssimokawa struct fw_xfer *xfer, *next; 773103285Sikob 774103285Sikob s = splfw(); 775103285Sikob /* shall we check the existance? */ 776103285Sikob STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 777113584Ssimokawa /* shall we do this? */ 778113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 779113584Ssimokawa next = STAILQ_NEXT(xfer, link); 780113584Ssimokawa fw_xfer_free(xfer); 781113584Ssimokawa } 782113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 783113584Ssimokawa 784103285Sikob splx(s); 785103285Sikob return 0; 786103285Sikob} 787103285Sikob 788103285Sikob/* 789103285Sikob * To free transaction label. 790103285Sikob */ 791106790Ssimokawastatic void 792106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 793103285Sikob{ 794103285Sikob struct tlabel *tl; 795103285Sikob int s = splfw(); 796103285Sikob 797103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[xfer->tl]); tl != NULL; 798103285Sikob tl = STAILQ_NEXT(tl, link)){ 799103285Sikob if(tl->xfer == xfer){ 800103285Sikob STAILQ_REMOVE(&fc->tlabels[xfer->tl], tl, tlabel, link); 801110195Ssimokawa free(tl, M_FW); 802103285Sikob splx(s); 803103285Sikob return; 804103285Sikob } 805103285Sikob } 806103285Sikob splx(s); 807103285Sikob return; 808103285Sikob} 809106790Ssimokawa 810103285Sikob/* 811103285Sikob * To obtain XFER structure by transaction label. 812103285Sikob */ 813106790Ssimokawastatic struct fw_xfer * 814106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel) 815103285Sikob{ 816103285Sikob struct fw_xfer *xfer; 817103285Sikob struct tlabel *tl; 818103285Sikob int s = splfw(); 819103285Sikob 820103285Sikob for( tl = STAILQ_FIRST(&fc->tlabels[tlabel]); tl != NULL; 821103285Sikob tl = STAILQ_NEXT(tl, link)){ 822103285Sikob if(tl->xfer->dst == node){ 823103285Sikob xfer = tl->xfer; 824103285Sikob splx(s); 825110577Ssimokawa if (firewire_debug > 2) 826110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 827103285Sikob return(xfer); 828103285Sikob } 829103285Sikob } 830110577Ssimokawa if (firewire_debug > 1) 831110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 832103285Sikob splx(s); 833103285Sikob return(NULL); 834103285Sikob} 835106790Ssimokawa 836103285Sikob/* 837103285Sikob * To allocate IEEE1394 XFER structure. 838103285Sikob */ 839106790Ssimokawastruct fw_xfer * 840110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 841103285Sikob{ 842103285Sikob struct fw_xfer *xfer; 843106790Ssimokawa 844110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 845106790Ssimokawa if (xfer == NULL) 846106790Ssimokawa return xfer; 847106790Ssimokawa 848110577Ssimokawa microtime(&xfer->tv); 849110269Ssimokawa xfer->malloc = type; 850106790Ssimokawa 851103285Sikob return xfer; 852103285Sikob} 853106790Ssimokawa 854113584Ssimokawastruct fw_xfer * 855113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 856113584Ssimokawa{ 857113584Ssimokawa struct fw_xfer *xfer; 858113584Ssimokawa 859113584Ssimokawa xfer = fw_xfer_alloc(type); 860113584Ssimokawa xfer->send.len = send_len; 861113584Ssimokawa xfer->recv.len = recv_len; 862113584Ssimokawa if (xfer == NULL) 863113584Ssimokawa return(NULL); 864113584Ssimokawa if (send_len) { 865113584Ssimokawa xfer->send.buf = malloc(send_len, type, M_NOWAIT | M_ZERO); 866113584Ssimokawa if (xfer->send.buf == NULL) { 867113584Ssimokawa fw_xfer_free(xfer); 868113584Ssimokawa return(NULL); 869113584Ssimokawa } 870113584Ssimokawa } 871113584Ssimokawa if (recv_len) { 872113584Ssimokawa xfer->recv.buf = malloc(recv_len, type, M_NOWAIT); 873113584Ssimokawa if (xfer->recv.buf == NULL) { 874113584Ssimokawa if (xfer->send.buf != NULL) 875113584Ssimokawa free(xfer->send.buf, type); 876113584Ssimokawa fw_xfer_free(xfer); 877113584Ssimokawa return(NULL); 878113584Ssimokawa } 879113584Ssimokawa } 880113584Ssimokawa return(xfer); 881113584Ssimokawa} 882113584Ssimokawa 883103285Sikob/* 884103285Sikob * IEEE1394 XFER post process. 885103285Sikob */ 886103285Sikobvoid 887103285Sikobfw_xfer_done(struct fw_xfer *xfer) 888103285Sikob{ 889103285Sikob if (xfer->act.hand == NULL) 890103285Sikob return; 891103285Sikob 892103285Sikob if (xfer->fc->status != FWBUSRESET) 893103285Sikob xfer->act.hand(xfer); 894103285Sikob else { 895103285Sikob printf("fw_xfer_done: pending\n"); 896103285Sikob if (xfer->fc != NULL) 897103285Sikob STAILQ_INSERT_TAIL(&xfer->fc->pending, xfer, link); 898103285Sikob else 899103285Sikob panic("fw_xfer_done: why xfer->fc is NULL?"); 900103285Sikob } 901103285Sikob} 902103285Sikob 903106790Ssimokawavoid 904113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer) 905103285Sikob{ 906103285Sikob int s; 907113584Ssimokawa 908103285Sikob if(xfer == NULL ) return; 909103285Sikob if(xfer->state == FWXF_INQ){ 910103285Sikob printf("fw_xfer_free FWXF_INQ\n"); 911103285Sikob s = splfw(); 912103285Sikob STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 913103285Sikob xfer->q->queued --; 914103285Sikob splx(s); 915103285Sikob } 916113584Ssimokawa if (xfer->fc != NULL) { 917114729Ssimokawa#if 1 918113584Ssimokawa if(xfer->state == FWXF_START) 919114729Ssimokawa /* 920114729Ssimokawa * This could happen if: 921114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 922114729Ssimokawa * 2. firewire_watch() is called. 923114729Ssimokawa */ 924114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 925103285Sikob#endif 926113584Ssimokawa fw_tl_free(xfer->fc, xfer); 927103285Sikob } 928113584Ssimokawa xfer->state = FWXF_INIT; 929113584Ssimokawa xfer->resp = 0; 930113584Ssimokawa xfer->retry = 0; 931113584Ssimokawa} 932113584Ssimokawa/* 933113584Ssimokawa * To free IEEE1394 XFER structure. 934113584Ssimokawa */ 935113584Ssimokawavoid 936113584Ssimokawafw_xfer_free( struct fw_xfer* xfer) 937113584Ssimokawa{ 938113584Ssimokawa if(xfer == NULL ) return; 939113584Ssimokawa fw_xfer_unload(xfer); 940103285Sikob if(xfer->send.buf != NULL){ 941113584Ssimokawa free(xfer->send.buf, xfer->malloc); 942103285Sikob } 943103285Sikob if(xfer->recv.buf != NULL){ 944113584Ssimokawa free(xfer->recv.buf, xfer->malloc); 945103285Sikob } 946110269Ssimokawa free(xfer, xfer->malloc); 947103285Sikob} 948103285Sikob 949103285Sikobstatic void 950110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 951103285Sikob{ 952103285Sikob#if 0 953110072Ssimokawa printf("asyreq done state=%d resp=%d\n", 954103285Sikob xfer->state, xfer->resp); 955103285Sikob#endif 956103285Sikob fw_xfer_free(xfer); 957103285Sikob} 958103285Sikob 959103285Sikob/* 960103285Sikob * To configure PHY. 961103285Sikob */ 962103285Sikobstatic void 963103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 964103285Sikob{ 965103285Sikob struct fw_xfer *xfer; 966103285Sikob struct fw_pkt *fp; 967103285Sikob 968103285Sikob fc->status = FWBUSPHYCONF; 969103285Sikob 970113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 12, 0); 971113584Ssimokawa if (xfer == NULL) 972113584Ssimokawa return; 973103285Sikob xfer->fc = fc; 974103285Sikob xfer->retry_req = fw_asybusy; 975110072Ssimokawa xfer->act.hand = fw_asy_callback_free; 976103285Sikob 977103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 978103285Sikob fp->mode.ld[1] = 0; 979103285Sikob if (root_node >= 0) 980113584Ssimokawa fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 981103285Sikob if (gap_count >= 0) 982113584Ssimokawa fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 983103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 984103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 985103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 986103285Sikob 987107653Ssimokawa if (firewire_debug) 988107653Ssimokawa printf("send phy_config root_node=%d gap_count=%d\n", 989103285Sikob root_node, gap_count); 990103285Sikob fw_asyreq(fc, -1, xfer); 991103285Sikob} 992103285Sikob 993103285Sikob#if 0 994103285Sikob/* 995103285Sikob * Dump self ID. 996103285Sikob */ 997103285Sikobstatic void 998103285Sikobfw_print_sid(u_int32_t sid) 999103285Sikob{ 1000103285Sikob union fw_self_id *s; 1001103285Sikob s = (union fw_self_id *) &sid; 1002103285Sikob printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1003103285Sikob " p0:%d p1:%d p2:%d i:%d m:%d\n", 1004103285Sikob s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1005103285Sikob s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1006103285Sikob s->p0.power_class, s->p0.port0, s->p0.port1, 1007103285Sikob s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1008103285Sikob} 1009103285Sikob#endif 1010103285Sikob 1011103285Sikob/* 1012103285Sikob * To receive self ID. 1013103285Sikob */ 1014113584Ssimokawavoid fw_sidrcv(struct firewire_comm* fc, u_int32_t *sid, u_int len) 1015103285Sikob{ 1016113584Ssimokawa u_int32_t *p; 1017103285Sikob union fw_self_id *self_id; 1018103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1019103285Sikob 1020103285Sikob fc->sid_cnt = len /(sizeof(u_int32_t) * 2); 1021103285Sikob fc->status = FWBUSINIT; 1022103285Sikob fc->max_node = fc->nodeid & 0x3f; 1023103285Sikob CSRARC(fc, NODE_IDS) = ((u_int32_t)fc->nodeid) << 16; 1024103285Sikob fc->status = FWBUSCYMELECT; 1025103285Sikob fc->topology_map->crc_len = 2; 1026103285Sikob fc->topology_map->generation ++; 1027103285Sikob fc->topology_map->self_id_count = 0; 1028103285Sikob fc->topology_map->node_count = 0; 1029103285Sikob fc->speed_map->generation ++; 1030103285Sikob fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1031103285Sikob self_id = &fc->topology_map->self_id[0]; 1032103285Sikob for(i = 0; i < fc->sid_cnt; i ++){ 1033103285Sikob if (sid[1] != ~sid[0]) { 1034103285Sikob printf("fw_sidrcv: invalid self-id packet\n"); 1035103285Sikob sid += 2; 1036103285Sikob continue; 1037103285Sikob } 1038103285Sikob *self_id = *((union fw_self_id *)sid); 1039103285Sikob fc->topology_map->crc_len++; 1040103285Sikob if(self_id->p0.sequel == 0){ 1041103285Sikob fc->topology_map->node_count ++; 1042103285Sikob c_port = 0; 1043103285Sikob#if 0 1044103285Sikob fw_print_sid(sid[0]); 1045103285Sikob#endif 1046103285Sikob node = self_id->p0.phy_id; 1047103285Sikob if(fc->max_node < node){ 1048103285Sikob fc->max_node = self_id->p0.phy_id; 1049103285Sikob } 1050103285Sikob /* XXX I'm not sure this is the right speed_map */ 1051103285Sikob fc->speed_map->speed[node][node] 1052103285Sikob = self_id->p0.phy_speed; 1053103285Sikob for (j = 0; j < node; j ++) { 1054103285Sikob fc->speed_map->speed[j][node] 1055103285Sikob = fc->speed_map->speed[node][j] 1056103285Sikob = min(fc->speed_map->speed[j][j], 1057103285Sikob self_id->p0.phy_speed); 1058103285Sikob } 1059103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1060103285Sikob (self_id->p0.link_active && self_id->p0.contender)) { 1061103285Sikob fc->irm = self_id->p0.phy_id; 1062103285Sikob } 1063103285Sikob if(self_id->p0.port0 >= 0x2){ 1064103285Sikob c_port++; 1065103285Sikob } 1066103285Sikob if(self_id->p0.port1 >= 0x2){ 1067103285Sikob c_port++; 1068103285Sikob } 1069103285Sikob if(self_id->p0.port2 >= 0x2){ 1070103285Sikob c_port++; 1071103285Sikob } 1072103285Sikob } 1073103285Sikob if(c_port > 2){ 1074103285Sikob i_branch += (c_port - 2); 1075103285Sikob } 1076103285Sikob sid += 2; 1077103285Sikob self_id++; 1078103285Sikob fc->topology_map->self_id_count ++; 1079103285Sikob } 1080108655Ssimokawa device_printf(fc->bdev, "%d nodes", fc->max_node + 1); 1081103285Sikob /* CRC */ 1082103285Sikob fc->topology_map->crc = fw_crc16( 1083103285Sikob (u_int32_t *)&fc->topology_map->generation, 1084103285Sikob fc->topology_map->crc_len * 4); 1085103285Sikob fc->speed_map->crc = fw_crc16( 1086103285Sikob (u_int32_t *)&fc->speed_map->generation, 1087103285Sikob fc->speed_map->crc_len * 4); 1088103285Sikob /* byteswap and copy to CSR */ 1089103285Sikob p = (u_int32_t *)fc->topology_map; 1090103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1091103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1092103285Sikob p = (u_int32_t *)fc->speed_map; 1093103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1094103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1095103285Sikob /* don't byte-swap u_int8_t array */ 1096103285Sikob bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1097103285Sikob 1098103285Sikob fc->max_hop = fc->max_node - i_branch; 1099103285Sikob printf(", maxhop <= %d", fc->max_hop); 1100103285Sikob 1101103285Sikob if(fc->irm == -1 ){ 1102103285Sikob printf(", Not found IRM capable node"); 1103103285Sikob }else{ 1104103285Sikob printf(", cable IRM = %d", fc->irm); 1105103285Sikob if (fc->irm == fc->nodeid) 1106110016Ssimokawa printf(" (me)"); 1107103285Sikob } 1108110016Ssimokawa printf("\n"); 1109103285Sikob 1110109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1111114909Ssimokawa if (fc->irm == fc->nodeid) { 1112103285Sikob fc->status = FWBUSMGRDONE; 1113103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1114114909Ssimokawa fw_bmr(fc); 1115109736Ssimokawa } else { 1116103285Sikob fc->status = FWBUSMGRELECT; 1117110193Ssimokawa callout_reset(&fc->bmr_callout, hz/8, 1118110193Ssimokawa (void *)fw_try_bmr, (void *)fc); 1119103285Sikob } 1120114909Ssimokawa } else 1121103285Sikob fc->status = FWBUSMGRDONE; 1122114909Ssimokawa 1123108853Ssimokawa callout_reset(&fc->busprobe_callout, hz/4, 1124108853Ssimokawa (void *)fw_bus_probe, (void *)fc); 1125103285Sikob} 1126106790Ssimokawa 1127103285Sikob/* 1128103285Sikob * To probe devices on the IEEE1394 bus. 1129103285Sikob */ 1130106790Ssimokawastatic void 1131106790Ssimokawafw_bus_probe(struct firewire_comm *fc) 1132103285Sikob{ 1133103285Sikob int s; 1134103285Sikob struct fw_device *fwdev, *next; 1135103285Sikob 1136103285Sikob s = splfw(); 1137103285Sikob fc->status = FWBUSEXPLORE; 1138103285Sikob fc->retry_count = 0; 1139103285Sikob 1140103285Sikob/* 1141103285Sikob * Invalidate all devices, just after bus reset. Devices 1142103285Sikob * to be removed has not been seen longer time. 1143103285Sikob */ 1144110193Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1145110193Ssimokawa next = STAILQ_NEXT(fwdev, link); 1146110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1147103285Sikob fwdev->status = FWDEVINVAL; 1148103285Sikob fwdev->rcnt = 0; 1149110193Ssimokawa } else if(fwdev->rcnt < FW_MAXDEVRCNT) { 1150103285Sikob fwdev->rcnt ++; 1151110193Ssimokawa } else { 1152110193Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, link); 1153110195Ssimokawa free(fwdev, M_FW); 1154103285Sikob } 1155103285Sikob } 1156103285Sikob fc->ongonode = 0; 1157103285Sikob fc->ongoaddr = CSRROMOFF; 1158103285Sikob fc->ongodev = NULL; 1159103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1160103285Sikob fw_bus_explore(fc); 1161103285Sikob splx(s); 1162103285Sikob} 1163106790Ssimokawa 1164103285Sikob/* 1165103285Sikob * To collect device informations on the IEEE1394 bus. 1166103285Sikob */ 1167106790Ssimokawastatic void 1168106790Ssimokawafw_bus_explore(struct firewire_comm *fc ) 1169103285Sikob{ 1170103285Sikob int err = 0; 1171110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1172103285Sikob u_int32_t addr; 1173103285Sikob struct fw_xfer *xfer; 1174103285Sikob struct fw_pkt *fp; 1175103285Sikob 1176103285Sikob if(fc->status != FWBUSEXPLORE) 1177103285Sikob return; 1178103285Sikob 1179103285Sikobloop: 1180103285Sikob if(fc->ongonode == fc->nodeid) fc->ongonode++; 1181103285Sikob 1182103285Sikob if(fc->ongonode > fc->max_node) goto done; 1183103285Sikob if(fc->ongonode >= 0x3f) goto done; 1184103285Sikob 1185103285Sikob /* check link */ 1186103285Sikob /* XXX we need to check phy_id first */ 1187103285Sikob if (!fc->topology_map->self_id[fc->ongonode].p0.link_active) { 1188110577Ssimokawa if (firewire_debug) 1189110577Ssimokawa printf("node%d: link down\n", fc->ongonode); 1190103285Sikob fc->ongonode++; 1191103285Sikob goto loop; 1192103285Sikob } 1193103285Sikob 1194103285Sikob if(fc->ongoaddr <= CSRROMOFF && 1195103285Sikob fc->ongoeui.hi == 0xffffffff && 1196103285Sikob fc->ongoeui.lo == 0xffffffff ){ 1197103285Sikob fc->ongoaddr = CSRROMOFF; 1198103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1199103285Sikob }else if(fc->ongoeui.hi == 0xffffffff ){ 1200103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1201103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1202103285Sikob }else if(fc->ongoeui.lo == 0xffffffff ){ 1203103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1204103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1205103285Sikob }else if(fc->ongodev == NULL){ 1206110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1207110193Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, fc->ongoeui)) 1208103285Sikob break; 1209103285Sikob if(fwdev != NULL){ 1210103285Sikob fwdev->dst = fc->ongonode; 1211103285Sikob fwdev->status = FWDEVATTACHED; 1212103285Sikob fc->ongonode++; 1213103285Sikob fc->ongoaddr = CSRROMOFF; 1214103285Sikob fc->ongodev = NULL; 1215103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1216103285Sikob goto loop; 1217103285Sikob } 1218110195Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, M_NOWAIT); 1219103285Sikob if(fwdev == NULL) 1220103285Sikob return; 1221106810Ssimokawa fwdev->fc = fc; 1222103285Sikob fwdev->rommax = 0; 1223103285Sikob fwdev->dst = fc->ongonode; 1224103285Sikob fwdev->eui.hi = fc->ongoeui.hi; fwdev->eui.lo = fc->ongoeui.lo; 1225103285Sikob fwdev->status = FWDEVINIT; 1226103285Sikob fwdev->speed = fc->speed_map->speed[fc->nodeid][fc->ongonode]; 1227103285Sikob 1228110179Ssimokawa pfwdev = NULL; 1229110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1230110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1231110179Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1232110179Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1233110179Ssimokawa break; 1234110179Ssimokawa pfwdev = tfwdev; 1235103285Sikob } 1236110179Ssimokawa if (pfwdev == NULL) 1237110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1238110179Ssimokawa else 1239110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1240103285Sikob 1241108655Ssimokawa device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1242107653Ssimokawa linkspeed[fwdev->speed], 1243107653Ssimokawa fc->ongoeui.hi, fc->ongoeui.lo); 1244103285Sikob 1245103285Sikob fc->ongodev = fwdev; 1246103285Sikob fc->ongoaddr = CSRROMOFF; 1247103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1248103285Sikob }else{ 1249103285Sikob addr = 0xf0000000 | fc->ongoaddr; 1250103285Sikob } 1251103285Sikob#if 0 1252103285Sikob xfer = asyreqq(fc, FWSPD_S100, 0, 0, 1253103285Sikob ((FWLOCALBUS | fc->ongonode) << 16) | 0xffff , addr, 1254103285Sikob fw_bus_explore_callback); 1255103285Sikob if(xfer == NULL) goto done; 1256103285Sikob#else 1257113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16); 1258103285Sikob if(xfer == NULL){ 1259103285Sikob goto done; 1260103285Sikob } 1261103285Sikob xfer->spd = 0; 1262103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1263113584Ssimokawa fp->mode.rreqq.dest_hi = 0xffff; 1264103285Sikob fp->mode.rreqq.tlrt = 0; 1265103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1266103285Sikob fp->mode.rreqq.pri = 0; 1267103285Sikob fp->mode.rreqq.src = 0; 1268103285Sikob xfer->dst = FWLOCALBUS | fc->ongonode; 1269113584Ssimokawa fp->mode.rreqq.dst = xfer->dst; 1270113584Ssimokawa fp->mode.rreqq.dest_lo = addr; 1271103285Sikob xfer->act.hand = fw_bus_explore_callback; 1272103285Sikob 1273110577Ssimokawa if (firewire_debug) 1274110577Ssimokawa printf("node%d: explore addr=0x%x\n", 1275110577Ssimokawa fc->ongonode, fc->ongoaddr); 1276103285Sikob err = fw_asyreq(fc, -1, xfer); 1277103285Sikob if(err){ 1278103285Sikob fw_xfer_free( xfer); 1279103285Sikob return; 1280103285Sikob } 1281103285Sikob#endif 1282103285Sikob return; 1283103285Sikobdone: 1284103285Sikob /* fw_attach_devs */ 1285103285Sikob fc->status = FWBUSEXPDONE; 1286107653Ssimokawa if (firewire_debug) 1287107653Ssimokawa printf("bus_explore done\n"); 1288103285Sikob fw_attach_dev(fc); 1289103285Sikob return; 1290103285Sikob 1291103285Sikob} 1292106790Ssimokawa 1293103285Sikob/* Portable Async. request read quad */ 1294106790Ssimokawastruct fw_xfer * 1295106790Ssimokawaasyreqq(struct firewire_comm *fc, u_int8_t spd, u_int8_t tl, u_int8_t rt, 1296105620Ssimokawa u_int32_t addr_hi, u_int32_t addr_lo, 1297105620Ssimokawa void (*hand) __P((struct fw_xfer*))) 1298103285Sikob{ 1299103285Sikob struct fw_xfer *xfer; 1300103285Sikob struct fw_pkt *fp; 1301103285Sikob int err; 1302103285Sikob 1303113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 16); 1304113584Ssimokawa if (xfer == NULL) 1305103285Sikob return NULL; 1306113584Ssimokawa 1307103285Sikob xfer->spd = spd; /* XXX:min(spd, fc->spd) */ 1308103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1309113584Ssimokawa fp->mode.rreqq.dest_hi = addr_hi & 0xffff; 1310103285Sikob if(tl & FWP_TL_VALID){ 1311103285Sikob fp->mode.rreqq.tlrt = (tl & 0x3f) << 2; 1312103285Sikob }else{ 1313103285Sikob fp->mode.rreqq.tlrt = 0; 1314103285Sikob } 1315103285Sikob fp->mode.rreqq.tlrt |= rt & 0x3; 1316103285Sikob fp->mode.rreqq.tcode = FWTCODE_RREQQ; 1317103285Sikob fp->mode.rreqq.pri = 0; 1318103285Sikob fp->mode.rreqq.src = 0; 1319103285Sikob xfer->dst = addr_hi >> 16; 1320113584Ssimokawa fp->mode.rreqq.dst = xfer->dst; 1321113584Ssimokawa fp->mode.rreqq.dest_lo = addr_lo; 1322103285Sikob xfer->act.hand = hand; 1323103285Sikob 1324103285Sikob err = fw_asyreq(fc, -1, xfer); 1325103285Sikob if(err){ 1326103285Sikob fw_xfer_free( xfer); 1327103285Sikob return NULL; 1328103285Sikob } 1329103285Sikob return xfer; 1330103285Sikob} 1331106790Ssimokawa 1332103285Sikob/* 1333103285Sikob * Callback for the IEEE1394 bus information collection. 1334103285Sikob */ 1335106790Ssimokawastatic void 1336106790Ssimokawafw_bus_explore_callback(struct fw_xfer *xfer) 1337106790Ssimokawa{ 1338103285Sikob struct firewire_comm *fc; 1339103285Sikob struct fw_pkt *sfp,*rfp; 1340103285Sikob struct csrhdr *chdr; 1341103285Sikob struct csrdir *csrd; 1342103285Sikob struct csrreg *csrreg; 1343103285Sikob u_int32_t offset; 1344103285Sikob 1345103285Sikob 1346110577Ssimokawa if(xfer == NULL) { 1347110577Ssimokawa printf("xfer == NULL\n"); 1348110577Ssimokawa return; 1349110577Ssimokawa } 1350103285Sikob fc = xfer->fc; 1351110577Ssimokawa 1352110577Ssimokawa if (firewire_debug) 1353110577Ssimokawa printf("node%d: callback addr=0x%x\n", 1354110577Ssimokawa fc->ongonode, fc->ongoaddr); 1355110577Ssimokawa 1356103285Sikob if(xfer->resp != 0){ 1357110577Ssimokawa printf("node%d: resp=%d addr=0x%x\n", 1358110577Ssimokawa fc->ongonode, xfer->resp, fc->ongoaddr); 1359114284Ssimokawa goto errnode; 1360103285Sikob } 1361103285Sikob 1362103285Sikob if(xfer->send.buf == NULL){ 1363110577Ssimokawa printf("node%d: send.buf=NULL addr=0x%x\n", 1364103285Sikob fc->ongonode, fc->ongoaddr); 1365114284Ssimokawa goto errnode; 1366103285Sikob } 1367103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1368103285Sikob 1369103285Sikob if(xfer->recv.buf == NULL){ 1370110577Ssimokawa printf("node%d: recv.buf=NULL addr=0x%x\n", 1371103285Sikob fc->ongonode, fc->ongoaddr); 1372114284Ssimokawa goto errnode; 1373103285Sikob } 1374103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1375103285Sikob#if 0 1376103285Sikob { 1377103285Sikob u_int32_t *qld; 1378103285Sikob int i; 1379103285Sikob qld = (u_int32_t *)xfer->recv.buf; 1380103285Sikob printf("len:%d\n", xfer->recv.len); 1381103285Sikob for( i = 0 ; i <= xfer->recv.len && i < 32; i+= 4){ 1382113584Ssimokawa printf("0x%08x ", rfp->mode.ld[i/4]); 1383103285Sikob if((i % 16) == 15) printf("\n"); 1384103285Sikob } 1385103285Sikob if((i % 16) != 15) printf("\n"); 1386103285Sikob } 1387103285Sikob#endif 1388103285Sikob if(fc->ongodev == NULL){ 1389113584Ssimokawa if(sfp->mode.rreqq.dest_lo == (0xf0000000 | CSRROMOFF)){ 1390103285Sikob rfp->mode.rresq.data = ntohl(rfp->mode.rresq.data); 1391103285Sikob chdr = (struct csrhdr *)(&rfp->mode.rresq.data); 1392110577Ssimokawa/* If CSR is minimal confinguration, more investgation is not needed. */ 1393103285Sikob if(chdr->info_len == 1){ 1394110577Ssimokawa if (firewire_debug) 1395110577Ssimokawa printf("node%d: minimal config\n", 1396110577Ssimokawa fc->ongonode); 1397103285Sikob goto nextnode; 1398103285Sikob }else{ 1399103285Sikob fc->ongoaddr = CSRROMOFF + 0xc; 1400103285Sikob } 1401113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0xc))){ 1402103285Sikob fc->ongoeui.hi = ntohl(rfp->mode.rresq.data); 1403103285Sikob fc->ongoaddr = CSRROMOFF + 0x10; 1404113584Ssimokawa }else if(sfp->mode.rreqq.dest_lo == (0xf0000000 |(CSRROMOFF + 0x10))){ 1405103285Sikob fc->ongoeui.lo = ntohl(rfp->mode.rresq.data); 1406110577Ssimokawa if (fc->ongoeui.hi == 0 && fc->ongoeui.lo == 0) { 1407110577Ssimokawa if (firewire_debug) 1408110577Ssimokawa printf("node%d: eui64 is zero.\n", 1409110577Ssimokawa fc->ongonode); 1410103285Sikob goto nextnode; 1411110577Ssimokawa } 1412103285Sikob fc->ongoaddr = CSRROMOFF; 1413103285Sikob } 1414103285Sikob }else{ 1415103285Sikob fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4] = ntohl(rfp->mode.rresq.data); 1416103285Sikob if(fc->ongoaddr > fc->ongodev->rommax){ 1417103285Sikob fc->ongodev->rommax = fc->ongoaddr; 1418103285Sikob } 1419103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1420103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1421103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1422103285Sikob offset = CSRROMOFF; 1423103285Sikob }else{ 1424103285Sikob chdr = (struct csrhdr *)&fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]; 1425103285Sikob offset = csrd->off; 1426103285Sikob } 1427103285Sikob if(fc->ongoaddr > (CSRROMOFF + 0x14) && fc->ongoaddr != offset){ 1428103285Sikob csrreg = (struct csrreg *)&fc->ongodev->csrrom[(fc->ongoaddr - CSRROMOFF)/4]; 1429103285Sikob if( csrreg->key == 0x81 || csrreg->key == 0xd1){ 1430103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1431103285Sikob if(csrd == NULL){ 1432103285Sikob goto nextnode; 1433103285Sikob }else{ 1434103285Sikob csrd->ongoaddr = fc->ongoaddr; 1435103285Sikob fc->ongoaddr += csrreg->val * 4; 1436103285Sikob csrd->off = fc->ongoaddr; 1437103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1438103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1439103285Sikob goto nextaddr; 1440103285Sikob } 1441103285Sikob } 1442103285Sikob } 1443103285Sikob fc->ongoaddr += 4; 1444103285Sikob if(((fc->ongoaddr - offset)/4 > chdr->crc_len) && 1445103285Sikob (fc->ongodev->rommax < 0x414)){ 1446103285Sikob if(fc->ongodev->rommax <= 0x414){ 1447103285Sikob csrd = SLIST_FIRST(&fc->csrfree); 1448103285Sikob if(csrd == NULL) goto nextnode; 1449103285Sikob csrd->off = fc->ongoaddr; 1450103285Sikob csrd->ongoaddr = fc->ongoaddr; 1451103285Sikob SLIST_REMOVE_HEAD(&fc->csrfree, link); 1452103285Sikob SLIST_INSERT_HEAD(&fc->ongocsr, csrd, link); 1453103285Sikob } 1454103285Sikob goto nextaddr; 1455103285Sikob } 1456103285Sikob 1457103285Sikob while(((fc->ongoaddr - offset)/4 > chdr->crc_len)){ 1458103285Sikob if(csrd == NULL){ 1459103285Sikob goto nextnode; 1460103285Sikob }; 1461103285Sikob fc->ongoaddr = csrd->ongoaddr + 4; 1462103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1463103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1464103285Sikob csrd = SLIST_FIRST(&fc->ongocsr); 1465103285Sikob if((csrd = SLIST_FIRST(&fc->ongocsr)) == NULL){ 1466103285Sikob chdr = (struct csrhdr *)(fc->ongodev->csrrom); 1467103285Sikob offset = CSRROMOFF; 1468103285Sikob }else{ 1469103285Sikob chdr = (struct csrhdr *)&(fc->ongodev->csrrom[(csrd->off - CSRROMOFF)/4]); 1470103285Sikob offset = csrd->off; 1471103285Sikob } 1472103285Sikob } 1473103285Sikob if((fc->ongoaddr - CSRROMOFF) > CSRROMSIZE){ 1474103285Sikob goto nextnode; 1475103285Sikob } 1476103285Sikob } 1477103285Sikobnextaddr: 1478103285Sikob fw_xfer_free( xfer); 1479103285Sikob fw_bus_explore(fc); 1480103285Sikob return; 1481114284Ssimokawaerrnode: 1482114284Ssimokawa fc->retry_count++; 1483114284Ssimokawa if (fc->ongodev != NULL) 1484114284Ssimokawa fc->ongodev->status = FWDEVINVAL; 1485103285Sikobnextnode: 1486103285Sikob fw_xfer_free( xfer); 1487103285Sikob fc->ongonode++; 1488103285Sikob/* housekeeping work space */ 1489103285Sikob fc->ongoaddr = CSRROMOFF; 1490103285Sikob fc->ongodev = NULL; 1491103285Sikob fc->ongoeui.hi = 0xffffffff; fc->ongoeui.lo = 0xffffffff; 1492103285Sikob while((csrd = SLIST_FIRST(&fc->ongocsr)) != NULL){ 1493103285Sikob SLIST_REMOVE_HEAD(&fc->ongocsr, link); 1494103285Sikob SLIST_INSERT_HEAD(&fc->csrfree, csrd, link); 1495103285Sikob } 1496103285Sikob fw_bus_explore(fc); 1497103285Sikob return; 1498103285Sikob} 1499103285Sikob 1500103285Sikob/* 1501103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1502103285Sikob */ 1503106815Ssimokawastatic void 1504106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1505103285Sikob{ 1506103285Sikob struct fw_device *fwdev; 1507103285Sikob struct fw_xfer *xfer; 1508103285Sikob int i, err; 1509103285Sikob device_t *devlistp; 1510103285Sikob int devcnt; 1511103285Sikob struct firewire_dev_comm *fdc; 1512103285Sikob 1513114069Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1514114069Ssimokawa if (fwdev->status == FWDEVINIT) 1515114069Ssimokawa fwdev->status = FWDEVATTACHED; 1516103285Sikob 1517108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1518103285Sikob if( err != 0 ) 1519103285Sikob return; 1520103285Sikob for( i = 0 ; i < devcnt ; i++){ 1521103285Sikob if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1522103285Sikob fdc = device_get_softc(devlistp[i]); 1523103285Sikob if (fdc->post_explore != NULL) 1524103285Sikob fdc->post_explore(fdc); 1525103285Sikob } 1526103285Sikob } 1527103285Sikob free(devlistp, M_TEMP); 1528103285Sikob 1529103285Sikob /* call pending handlers */ 1530103285Sikob i = 0; 1531103285Sikob while ((xfer = STAILQ_FIRST(&fc->pending))) { 1532103285Sikob STAILQ_REMOVE_HEAD(&fc->pending, link); 1533103285Sikob i++; 1534103285Sikob if (xfer->act.hand) 1535103285Sikob xfer->act.hand(xfer); 1536103285Sikob } 1537103285Sikob if (i > 0) 1538103285Sikob printf("fw_attach_dev: %d pending handlers called\n", i); 1539103285Sikob if (fc->retry_count > 0) { 1540111040Ssimokawa printf("probe failed for %d node\n", fc->retry_count); 1541111040Ssimokawa#if 0 1542110193Ssimokawa callout_reset(&fc->retry_probe_callout, hz*2, 1543110193Ssimokawa (void *)fc->ibr, (void *)fc); 1544111040Ssimokawa#endif 1545103285Sikob } 1546103285Sikob return; 1547103285Sikob} 1548106815Ssimokawa 1549103285Sikob/* 1550103285Sikob * To allocate uniq transaction label. 1551103285Sikob */ 1552106815Ssimokawastatic int 1553106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1554103285Sikob{ 1555103285Sikob u_int i; 1556103285Sikob struct tlabel *tl, *tmptl; 1557103285Sikob int s; 1558103285Sikob static u_int32_t label = 0; 1559103285Sikob 1560103285Sikob s = splfw(); 1561103285Sikob for( i = 0 ; i < 0x40 ; i ++){ 1562103285Sikob label = (label + 1) & 0x3f; 1563103285Sikob for(tmptl = STAILQ_FIRST(&fc->tlabels[label]); 1564103285Sikob tmptl != NULL; tmptl = STAILQ_NEXT(tmptl, link)){ 1565103285Sikob if(tmptl->xfer->dst == xfer->dst) break; 1566103285Sikob } 1567103285Sikob if(tmptl == NULL) { 1568110195Ssimokawa tl = malloc(sizeof(struct tlabel),M_FW,M_NOWAIT); 1569103285Sikob if (tl == NULL) { 1570103285Sikob splx(s); 1571103285Sikob return (-1); 1572103285Sikob } 1573103285Sikob tl->xfer = xfer; 1574103285Sikob STAILQ_INSERT_TAIL(&fc->tlabels[label], tl, link); 1575103285Sikob splx(s); 1576110577Ssimokawa if (firewire_debug > 1) 1577110577Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", 1578110577Ssimokawa xfer->dst, label); 1579103285Sikob return(label); 1580103285Sikob } 1581103285Sikob } 1582103285Sikob splx(s); 1583103285Sikob 1584103285Sikob printf("fw_get_tlabel: no free tlabel\n"); 1585103285Sikob return(-1); 1586103285Sikob} 1587106815Ssimokawa 1588113584Ssimokawastatic void 1589113584Ssimokawafw_rcv_copy(struct fw_xfer *xfer, struct iovec *vec, int nvec) 1590113584Ssimokawa{ 1591113584Ssimokawa char *p; 1592113584Ssimokawa int res, i, len; 1593113584Ssimokawa 1594113584Ssimokawa p = xfer->recv.buf; 1595113584Ssimokawa res = xfer->recv.len; 1596113584Ssimokawa for (i = 0; i < nvec; i++, vec++) { 1597113584Ssimokawa len = vec->iov_len; 1598113584Ssimokawa if (res < len) { 1599113584Ssimokawa printf("rcv buffer(%d) is %d bytes short.\n", 1600113584Ssimokawa xfer->recv.len, len - res); 1601113584Ssimokawa len = res; 1602113584Ssimokawa } 1603113584Ssimokawa bcopy(vec->iov_base, p, len); 1604113584Ssimokawa p += len; 1605113584Ssimokawa res -= len; 1606113584Ssimokawa if (res <= 0) 1607113584Ssimokawa break; 1608113584Ssimokawa } 1609113584Ssimokawa xfer->recv.len -= res; 1610113584Ssimokawa} 1611113584Ssimokawa 1612103285Sikob/* 1613103285Sikob * Generic packet receving process. 1614103285Sikob */ 1615106815Ssimokawavoid 1616113584Ssimokawafw_rcv(struct firewire_comm *fc, struct iovec *vec, int nvec, u_int sub, u_int spd) 1617103285Sikob{ 1618103285Sikob struct fw_pkt *fp, *resfp; 1619103285Sikob struct fw_xfer *xfer; 1620103285Sikob struct fw_bind *bind; 1621103285Sikob struct firewire_softc *sc; 1622113584Ssimokawa int tcode, s; 1623113584Ssimokawa int i, len, oldstate; 1624103285Sikob#if 0 1625103285Sikob { 1626103285Sikob u_int32_t *qld; 1627103285Sikob int i; 1628103285Sikob qld = (u_int32_t *)buf; 1629103285Sikob printf("spd %d len:%d\n", spd, len); 1630103285Sikob for( i = 0 ; i <= len && i < 32; i+= 4){ 1631103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1632103285Sikob if((i % 16) == 15) printf("\n"); 1633103285Sikob } 1634103285Sikob if((i % 16) != 15) printf("\n"); 1635103285Sikob } 1636103285Sikob#endif 1637113584Ssimokawa fp = (struct fw_pkt *)vec[0].iov_base; 1638113584Ssimokawa tcode = fp->mode.common.tcode; 1639113584Ssimokawa#if 0 /* XXX this check is not valid for RRESQ and WREQQ */ 1640113584Ssimokawa if (vec[0].iov_len < fc->tcode[tcode].hdr_len) { 1641113584Ssimokawa#if __FreeBSD_version >= 500000 1642113584Ssimokawa printf("fw_rcv: iov_len(%zu) is less than" 1643113584Ssimokawa#else 1644113584Ssimokawa printf("fw_rcv: iov_len(%u) is less than" 1645113584Ssimokawa#endif 1646113584Ssimokawa " hdr_len(%d:tcode=%d)\n", vec[0].iov_len, 1647113584Ssimokawa fc->tcode[tcode].hdr_len, tcode); 1648113584Ssimokawa } 1649113584Ssimokawa#endif 1650113584Ssimokawa switch (tcode) { 1651103285Sikob case FWTCODE_WRES: 1652103285Sikob case FWTCODE_RRESQ: 1653103285Sikob case FWTCODE_RRESB: 1654103285Sikob case FWTCODE_LRES: 1655113584Ssimokawa xfer = fw_tl2xfer(fc, fp->mode.hdr.src, 1656103285Sikob fp->mode.hdr.tlrt >> 2); 1657103285Sikob if(xfer == NULL) { 1658103285Sikob printf("fw_rcv: unknown response " 1659110891Ssimokawa "tcode=%d src=0x%x tl=0x%x rt=%d data=0x%x\n", 1660113584Ssimokawa tcode, 1661113584Ssimokawa fp->mode.hdr.src, 1662103285Sikob fp->mode.hdr.tlrt >> 2, 1663103285Sikob fp->mode.hdr.tlrt & 3, 1664103285Sikob fp->mode.rresq.data); 1665103285Sikob#if 1 1666103285Sikob printf("try ad-hoc work around!!\n"); 1667113584Ssimokawa xfer = fw_tl2xfer(fc, fp->mode.hdr.src, 1668103285Sikob (fp->mode.hdr.tlrt >> 2)^3); 1669103285Sikob if (xfer == NULL) { 1670103285Sikob printf("no use...\n"); 1671103285Sikob goto err; 1672103285Sikob } 1673103285Sikob#else 1674103285Sikob goto err; 1675103285Sikob#endif 1676103285Sikob } 1677113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1678113584Ssimokawa xfer->resp = 0; 1679113584Ssimokawa /* make sure the packet is drained in AT queue */ 1680113584Ssimokawa oldstate = xfer->state; 1681113584Ssimokawa xfer->state = FWXF_RCVD; 1682113584Ssimokawa switch (oldstate) { 1683113584Ssimokawa case FWXF_SENT: 1684103285Sikob fw_xfer_done(xfer); 1685103285Sikob break; 1686113584Ssimokawa case FWXF_START: 1687113584Ssimokawa if (firewire_debug) 1688113584Ssimokawa printf("not sent yet\n"); 1689113584Ssimokawa break; 1690103285Sikob default: 1691113584Ssimokawa printf("unexpected state %d\n", xfer->state); 1692103285Sikob } 1693113584Ssimokawa return; 1694103285Sikob case FWTCODE_WREQQ: 1695103285Sikob case FWTCODE_WREQB: 1696103285Sikob case FWTCODE_RREQQ: 1697103285Sikob case FWTCODE_RREQB: 1698103285Sikob case FWTCODE_LREQ: 1699113584Ssimokawa bind = fw_bindlookup(fc, fp->mode.rreqq.dest_hi, 1700113584Ssimokawa fp->mode.rreqq.dest_lo); 1701103285Sikob if(bind == NULL){ 1702113962Ssimokawa#if __FreeBSD_version >= 500000 1703113584Ssimokawa printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%x\n", 1704113962Ssimokawa#else 1705113962Ssimokawa printf("Unknown service addr 0x%08x:0x%08x tcode=%x src=0x%x data=%lx\n", 1706113962Ssimokawa#endif 1707113584Ssimokawa fp->mode.wreqq.dest_hi, 1708113584Ssimokawa fp->mode.wreqq.dest_lo, 1709113584Ssimokawa tcode, 1710113584Ssimokawa fp->mode.hdr.src, 1711113584Ssimokawa ntohl(fp->mode.wreqq.data)); 1712103285Sikob if (fc->status == FWBUSRESET) { 1713110798Ssimokawa printf("fw_rcv: cannot respond(bus reset)!\n"); 1714103285Sikob goto err; 1715103285Sikob } 1716113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 16, 0); 1717103285Sikob if(xfer == NULL){ 1718103285Sikob return; 1719103285Sikob } 1720103285Sikob xfer->spd = spd; 1721103285Sikob resfp = (struct fw_pkt *)xfer->send.buf; 1722113584Ssimokawa switch (tcode) { 1723103285Sikob case FWTCODE_WREQQ: 1724103285Sikob case FWTCODE_WREQB: 1725103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 1726103285Sikob xfer->send.len = 12; 1727103285Sikob break; 1728103285Sikob case FWTCODE_RREQQ: 1729103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1730103285Sikob xfer->send.len = 16; 1731103285Sikob break; 1732103285Sikob case FWTCODE_RREQB: 1733103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 1734103285Sikob xfer->send.len = 16; 1735103285Sikob break; 1736103285Sikob case FWTCODE_LREQ: 1737103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 1738103285Sikob xfer->send.len = 16; 1739103285Sikob break; 1740103285Sikob } 1741103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 1742103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1743103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 1744103285Sikob resfp->mode.rresb.rtcode = 7; 1745103285Sikob resfp->mode.rresb.extcode = 0; 1746103285Sikob resfp->mode.rresb.len = 0; 1747103285Sikob/* 1748103285Sikob xfer->act.hand = fw_asy_callback; 1749103285Sikob*/ 1750103285Sikob xfer->act.hand = fw_xfer_free; 1751103285Sikob if(fw_asyreq(fc, -1, xfer)){ 1752103285Sikob fw_xfer_free( xfer); 1753103285Sikob return; 1754103285Sikob } 1755103285Sikob goto err; 1756103285Sikob } 1757113584Ssimokawa len = 0; 1758113584Ssimokawa for (i = 0; i < nvec; i ++) 1759113584Ssimokawa len += vec[i].iov_len; 1760113584Ssimokawa switch(bind->act_type){ 1761103285Sikob case FWACT_XFER: 1762113584Ssimokawa /* splfw()?? */ 1763113584Ssimokawa xfer = STAILQ_FIRST(&bind->xferlist); 1764113584Ssimokawa if (xfer == NULL) { 1765113584Ssimokawa printf("Discard a packet for this bind.\n"); 1766113584Ssimokawa goto err; 1767113584Ssimokawa } 1768113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1769113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1770103285Sikob xfer->spd = spd; 1771103285Sikob if (fc->status != FWBUSRESET) 1772103285Sikob xfer->act.hand(xfer); 1773103285Sikob else 1774103285Sikob STAILQ_INSERT_TAIL(&fc->pending, xfer, link); 1775103285Sikob return; 1776103285Sikob break; 1777103285Sikob case FWACT_CH: 1778113584Ssimokawa if(fc->ir[bind->sub]->queued >= 1779113584Ssimokawa fc->ir[bind->sub]->maxq){ 1780108655Ssimokawa device_printf(fc->bdev, 1781108655Ssimokawa "Discard a packet %x %d\n", 1782113584Ssimokawa bind->sub, 1783113584Ssimokawa fc->ir[bind->sub]->queued); 1784103285Sikob goto err; 1785103285Sikob } 1786113584Ssimokawa xfer = STAILQ_FIRST(&bind->xferlist); 1787113584Ssimokawa if (xfer == NULL) { 1788113584Ssimokawa printf("Discard packet for this bind\n"); 1789113584Ssimokawa goto err; 1790113584Ssimokawa } 1791113584Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1792113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1793103285Sikob xfer->spd = spd; 1794103285Sikob s = splfw(); 1795113584Ssimokawa fc->ir[bind->sub]->queued++; 1796113584Ssimokawa STAILQ_INSERT_TAIL(&fc->ir[bind->sub]->q, xfer, link); 1797103285Sikob splx(s); 1798103285Sikob 1799113584Ssimokawa wakeup((caddr_t)fc->ir[bind->sub]); 1800103285Sikob 1801103285Sikob return; 1802103285Sikob break; 1803103285Sikob default: 1804103285Sikob goto err; 1805103285Sikob break; 1806103285Sikob } 1807103285Sikob break; 1808103285Sikob case FWTCODE_STREAM: 1809103285Sikob { 1810103285Sikob struct fw_xferq *xferq; 1811103285Sikob 1812103285Sikob xferq = fc->ir[sub]; 1813103285Sikob#if 0 1814103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 1815103285Sikob sub, len, off, spd); 1816103285Sikob#endif 1817103285Sikob if(xferq->queued >= xferq->maxq) { 1818103285Sikob printf("receive queue is full\n"); 1819103285Sikob goto err; 1820103285Sikob } 1821113584Ssimokawa /* XXX get xfer from xfer queue, we don't need copy for 1822113584Ssimokawa per packet mode */ 1823113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 1824113584Ssimokawa vec[0].iov_len); 1825103285Sikob if(xfer == NULL) goto err; 1826113584Ssimokawa fw_rcv_copy(xfer, vec, nvec); 1827103285Sikob xfer->spd = spd; 1828103285Sikob s = splfw(); 1829103285Sikob xferq->queued++; 1830103285Sikob STAILQ_INSERT_TAIL(&xferq->q, xfer, link); 1831103285Sikob splx(s); 1832103285Sikob sc = device_get_softc(fc->bdev); 1833103285Sikob#if __FreeBSD_version >= 500000 1834103285Sikob if (SEL_WAITING(&xferq->rsel)) 1835103285Sikob#else 1836103285Sikob if (&xferq->rsel.si_pid != 0) 1837103285Sikob#endif 1838103285Sikob selwakeup(&xferq->rsel); 1839103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 1840103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 1841103285Sikob wakeup((caddr_t)xferq); 1842103285Sikob } 1843103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 1844103285Sikob xferq->hand(xferq); 1845103285Sikob } 1846103285Sikob return; 1847103285Sikob break; 1848103285Sikob } 1849103285Sikob default: 1850113584Ssimokawa printf("fw_rcv: unknow tcode %d\n", tcode); 1851103285Sikob break; 1852103285Sikob } 1853103285Sikoberr: 1854113584Ssimokawa return; 1855103285Sikob} 1856106815Ssimokawa 1857103285Sikob/* 1858103285Sikob * Post process for Bus Manager election process. 1859103285Sikob */ 1860103285Sikobstatic void 1861103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 1862103285Sikob{ 1863109422Ssimokawa struct fw_pkt *rfp; 1864103285Sikob struct firewire_comm *fc; 1865109422Ssimokawa int bmr; 1866103285Sikob 1867109422Ssimokawa if (xfer == NULL) 1868109422Ssimokawa return; 1869103285Sikob fc = xfer->fc; 1870109422Ssimokawa if (xfer->resp != 0) 1871103285Sikob goto error; 1872109422Ssimokawa if (xfer->send.buf == NULL) 1873103285Sikob goto error; 1874109422Ssimokawa if (xfer->recv.buf == NULL) 1875103285Sikob goto error; 1876103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1877109422Ssimokawa if (rfp->mode.lres.rtcode != FWRCODE_COMPLETE) 1878109422Ssimokawa goto error; 1879109422Ssimokawa 1880109422Ssimokawa bmr = ntohl(rfp->mode.lres.payload[0]); 1881109422Ssimokawa if (bmr == 0x3f) 1882109422Ssimokawa bmr = fc->nodeid; 1883109422Ssimokawa 1884109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 1885114909Ssimokawa fw_xfer_free(xfer); 1886114909Ssimokawa fw_bmr(fc); 1887114909Ssimokawa return; 1888114909Ssimokawa 1889103285Sikoberror: 1890114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 1891103285Sikob fw_xfer_free(xfer); 1892103285Sikob} 1893106815Ssimokawa 1894113584Ssimokawa 1895103285Sikob/* 1896103285Sikob * To candidate Bus Manager election process. 1897103285Sikob */ 1898110270Ssimokawastatic void 1899106815Ssimokawafw_try_bmr(void *arg) 1900103285Sikob{ 1901103285Sikob struct fw_xfer *xfer; 1902103285Sikob struct firewire_comm *fc = (struct firewire_comm *)arg; 1903103285Sikob struct fw_pkt *fp; 1904103285Sikob int err = 0; 1905103285Sikob 1906113584Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 24, 20); 1907103285Sikob if(xfer == NULL){ 1908103285Sikob return; 1909103285Sikob } 1910103285Sikob xfer->spd = 0; 1911103285Sikob fc->status = FWBUSMGRELECT; 1912103285Sikob 1913103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1914113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 1915103285Sikob fp->mode.lreq.tlrt = 0; 1916103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 1917103285Sikob fp->mode.lreq.pri = 0; 1918103285Sikob fp->mode.lreq.src = 0; 1919113584Ssimokawa fp->mode.lreq.len = 8; 1920113584Ssimokawa fp->mode.lreq.extcode = FW_LREQ_CMPSWAP; 1921103285Sikob xfer->dst = FWLOCALBUS | fc->irm; 1922113584Ssimokawa fp->mode.lreq.dst = xfer->dst; 1923113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 1924109422Ssimokawa fp->mode.lreq.payload[0] = htonl(0x3f); 1925109422Ssimokawa fp->mode.lreq.payload[1] = htonl(fc->nodeid); 1926103285Sikob xfer->act.hand = fw_try_bmr_callback; 1927103285Sikob 1928103285Sikob err = fw_asyreq(fc, -1, xfer); 1929103285Sikob if(err){ 1930103285Sikob fw_xfer_free( xfer); 1931103285Sikob return; 1932103285Sikob } 1933103285Sikob return; 1934103285Sikob} 1935106543Ssimokawa 1936106543Ssimokawa#ifdef FW_VMACCESS 1937103285Sikob/* 1938103285Sikob * Software implementation for physical memory block access. 1939103285Sikob * XXX:Too slow, usef for debug purpose only. 1940103285Sikob */ 1941106815Ssimokawastatic void 1942106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){ 1943103285Sikob struct fw_pkt *rfp, *sfp = NULL; 1944113584Ssimokawa u_int32_t *ld = (u_int32_t *)xfer->recv.buf; 1945103285Sikob 1946113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 1947113584Ssimokawa xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1948103285Sikob printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1949103285Sikob if(xfer->resp != 0){ 1950103285Sikob fw_xfer_free( xfer); 1951103285Sikob return; 1952103285Sikob } 1953103285Sikob if(xfer->recv.buf == NULL){ 1954103285Sikob fw_xfer_free( xfer); 1955103285Sikob return; 1956103285Sikob } 1957103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1958103285Sikob switch(rfp->mode.hdr.tcode){ 1959103285Sikob /* XXX need fix for 64bit arch */ 1960103285Sikob case FWTCODE_WREQB: 1961110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 1962103285Sikob xfer->send.len = 12; 1963103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1964103285Sikob bcopy(rfp->mode.wreqb.payload, 1965103285Sikob (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 1966103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1967103285Sikob sfp->mode.wres.rtcode = 0; 1968103285Sikob break; 1969103285Sikob case FWTCODE_WREQQ: 1970110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 1971103285Sikob xfer->send.len = 12; 1972103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1973103285Sikob *((u_int32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 1974103285Sikob sfp->mode.wres.rtcode = 0; 1975103285Sikob break; 1976103285Sikob case FWTCODE_RREQB: 1977110195Ssimokawa xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 1978103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 1979103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1980103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 1981103285Sikob sfp->mode.rresb.payload, (u_int16_t)ntohs(rfp->mode.rreqb.len)); 1982103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 1983103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 1984103285Sikob sfp->mode.rresb.rtcode = 0; 1985103285Sikob sfp->mode.rresb.extcode = 0; 1986103285Sikob break; 1987103285Sikob case FWTCODE_RREQQ: 1988110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 1989103285Sikob xfer->send.len = 16; 1990103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1991103285Sikob sfp->mode.rresq.data = *(u_int32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 1992103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 1993103285Sikob sfp->mode.rresb.rtcode = 0; 1994103285Sikob break; 1995103285Sikob default: 1996103285Sikob fw_xfer_free( xfer); 1997103285Sikob return; 1998103285Sikob } 1999103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2000103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2001103285Sikob xfer->act.hand = fw_xfer_free; 2002103285Sikob xfer->retry_req = fw_asybusy; 2003103285Sikob 2004103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2005103285Sikob sfp->mode.hdr.pri = 0; 2006103285Sikob 2007103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2008103285Sikob/**/ 2009103285Sikob return; 2010103285Sikob} 2011106543Ssimokawa#endif 2012106543Ssimokawa 2013103285Sikob/* 2014103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2015103285Sikob */ 2016106815Ssimokawau_int16_t 2017106815Ssimokawafw_crc16(u_int32_t *ptr, u_int32_t len){ 2018103285Sikob u_int32_t i, sum, crc = 0; 2019103285Sikob int shift; 2020103285Sikob len = (len + 3) & ~3; 2021103285Sikob for(i = 0 ; i < len ; i+= 4){ 2022103285Sikob for( shift = 28 ; shift >= 0 ; shift -= 4){ 2023103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2024103285Sikob crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2025103285Sikob } 2026103285Sikob crc &= 0xffff; 2027103285Sikob } 2028103285Sikob return((u_int16_t) crc); 2029103285Sikob} 2030106815Ssimokawa 2031110270Ssimokawastatic int 2032110072Ssimokawafw_bmr(struct firewire_comm *fc) 2033110072Ssimokawa{ 2034110072Ssimokawa struct fw_device fwdev; 2035113584Ssimokawa union fw_self_id *self_id; 2036110072Ssimokawa int cmstr; 2037110072Ssimokawa 2038113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2039113584Ssimokawa self_id = &fc->topology_map->self_id[fc->max_node]; 2040113584Ssimokawa if (fc->max_node > 0) { 2041114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2042114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2043113584Ssimokawa cmstr = fc->max_node; 2044114909Ssimokawa else { 2045114909Ssimokawa device_printf(fc->bdev, 2046114909Ssimokawa "root node is not cycle master capable\n"); 2047114909Ssimokawa /* XXX shall we be the cycle master? */ 2048113584Ssimokawa cmstr = fc->nodeid; 2049114909Ssimokawa /* XXX need bus reset */ 2050114909Ssimokawa } 2051113584Ssimokawa } else 2052113584Ssimokawa cmstr = -1; 2053114909Ssimokawa 2054114909Ssimokawa device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID)); 2055114909Ssimokawa if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2056114909Ssimokawa /* We are not the bus manager */ 2057114909Ssimokawa printf("\n"); 2058114909Ssimokawa return(0); 2059114909Ssimokawa } 2060114909Ssimokawa printf("(me)\n"); 2061114909Ssimokawa 2062114909Ssimokawa /* Optimize gapcount */ 2063113584Ssimokawa if(fc->max_hop <= MAX_GAPHOP ) 2064113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2065110072Ssimokawa /* If we are the cycle master, nothing to do */ 2066113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2067110072Ssimokawa return 0; 2068110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2069110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2070110072Ssimokawa fwdev.fc = fc; 2071110072Ssimokawa fwdev.dst = cmstr; 2072110072Ssimokawa fwdev.speed = 0; 2073110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2074110072Ssimokawa fwdev.status = FWDEVINIT; 2075110072Ssimokawa /* Set cmstr bit on the cycle master */ 2076110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2077114909Ssimokawa 0xffff, 0xf0000000 | STATE_SET, htonl(1 << 8), 2078110072Ssimokawa fw_asy_callback_free); 2079110072Ssimokawa 2080110072Ssimokawa return 0; 2081110072Ssimokawa} 2082110072Ssimokawa 2083103285SikobDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,0,0); 2084103285SikobMODULE_VERSION(firewire, 1); 2085