firewire.c revision 169829
1139749Simp/*- 2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa 3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 4103285Sikob * All rights reserved. 5103285Sikob * 6103285Sikob * Redistribution and use in source and binary forms, with or without 7103285Sikob * modification, are permitted provided that the following conditions 8103285Sikob * are met: 9103285Sikob * 1. Redistributions of source code must retain the above copyright 10103285Sikob * notice, this list of conditions and the following disclaimer. 11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 12103285Sikob * notice, this list of conditions and the following disclaimer in the 13103285Sikob * documentation and/or other materials provided with the distribution. 14103285Sikob * 3. All advertising materials mentioning features or use of this software 15103285Sikob * must display the acknowledgement as bellow: 16103285Sikob * 17103285Sikob * This product includes software developed by K. Kobayashi and H. Shimokawa 18103285Sikob * 19103285Sikob * 4. The name of the author may not be used to endorse or promote products 20103285Sikob * derived from this software without specific prior written permission. 21103285Sikob * 22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25103285Sikob * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32103285Sikob * POSSIBILITY OF SUCH DAMAGE. 33103285Sikob * 34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewire.c 169829 2007-05-21 12:17:54Z simokawa $ 35103285Sikob * 36103285Sikob */ 37103285Sikob 38103285Sikob#include <sys/param.h> 39103285Sikob#include <sys/systm.h> 40103285Sikob#include <sys/types.h> 41103285Sikob 42103285Sikob#include <sys/kernel.h> 43129879Sphk#include <sys/module.h> 44103285Sikob#include <sys/malloc.h> 45103285Sikob#include <sys/conf.h> 46103285Sikob#include <sys/sysctl.h> 47169806Ssimokawa#include <sys/kthread.h> 48103285Sikob 49127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 50117067Ssimokawa#include <machine/clock.h> /* for DELAY() */ 51117067Ssimokawa#endif 52103285Sikob 53103285Sikob#include <sys/bus.h> /* used by smbus and newbus */ 54113584Ssimokawa#include <machine/bus.h> 55103285Sikob 56127468Ssimokawa#ifdef __DragonFly__ 57127468Ssimokawa#include "firewire.h" 58127468Ssimokawa#include "firewirereg.h" 59127468Ssimokawa#include "fwmem.h" 60127468Ssimokawa#include "iec13213.h" 61127468Ssimokawa#include "iec68113.h" 62127468Ssimokawa#else 63103285Sikob#include <dev/firewire/firewire.h> 64103285Sikob#include <dev/firewire/firewirereg.h> 65110072Ssimokawa#include <dev/firewire/fwmem.h> 66103285Sikob#include <dev/firewire/iec13213.h> 67103285Sikob#include <dev/firewire/iec68113.h> 68127468Ssimokawa#endif 69103285Sikob 70116376Ssimokawastruct crom_src_buf { 71116376Ssimokawa struct crom_src src; 72116376Ssimokawa struct crom_chunk root; 73116376Ssimokawa struct crom_chunk vendor; 74116376Ssimokawa struct crom_chunk hw; 75116376Ssimokawa}; 76116376Ssimokawa 77120850Ssimokawaint firewire_debug=0, try_bmr=1, hold_count=3; 78103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 79108281Ssimokawa "FireWire driver debug flag"); 80109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 81109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 82109736Ssimokawa "Try to be a bus manager"); 83120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0, 84120850Ssimokawa "Number of count of bus resets for removing lost device information"); 85103285Sikob 86110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 87110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 88110195Ssimokawa 89103285Sikob#define FW_MAXASYRTY 4 90103285Sikob 91103285Sikobdevclass_t firewire_devclass; 92103285Sikob 93125238Ssimokawastatic void firewire_identify (driver_t *, device_t); 94125238Ssimokawastatic int firewire_probe (device_t); 95124169Ssimokawastatic int firewire_attach (device_t); 96124169Ssimokawastatic int firewire_detach (device_t); 97124169Ssimokawastatic int firewire_resume (device_t); 98103285Sikob#if 0 99124169Ssimokawastatic int firewire_shutdown (device_t); 100103285Sikob#endif 101124169Ssimokawastatic device_t firewire_add_child (device_t, int, const char *, int); 102124169Ssimokawastatic void fw_try_bmr (void *); 103124169Ssimokawastatic void fw_try_bmr_callback (struct fw_xfer *); 104124169Ssimokawastatic void fw_asystart (struct fw_xfer *); 105124169Ssimokawastatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *); 106124169Ssimokawastatic void fw_bus_probe (struct firewire_comm *); 107124169Ssimokawastatic void fw_attach_dev (struct firewire_comm *); 108169806Ssimokawastatic void fw_bus_probe_thread(void *); 109106543Ssimokawa#ifdef FW_VMACCESS 110124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *); 111106543Ssimokawa#endif 112124169Ssimokawastatic int fw_bmr (struct firewire_comm *); 113103285Sikob 114103285Sikobstatic device_method_t firewire_methods[] = { 115103285Sikob /* Device interface */ 116125238Ssimokawa DEVMETHOD(device_identify, firewire_identify), 117125238Ssimokawa DEVMETHOD(device_probe, firewire_probe), 118103285Sikob DEVMETHOD(device_attach, firewire_attach), 119103285Sikob DEVMETHOD(device_detach, firewire_detach), 120108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 121116978Ssimokawa DEVMETHOD(device_resume, firewire_resume), 122103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 123103285Sikob 124103285Sikob /* Bus interface */ 125103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 126103285Sikob DEVMETHOD(bus_print_child, bus_generic_print_child), 127103285Sikob 128103285Sikob { 0, 0 } 129103285Sikob}; 130124251Ssimokawachar *linkspeed[] = { 131124251Ssimokawa "S100", "S200", "S400", "S800", 132124251Ssimokawa "S1600", "S3200", "undef", "undef" 133124251Ssimokawa}; 134103285Sikob 135124251Ssimokawastatic char *tcode_str[] = { 136124251Ssimokawa "WREQQ", "WREQB", "WRES", "undef", 137124251Ssimokawa "RREQQ", "RREQB", "RRESQ", "RRESB", 138124251Ssimokawa "CYCS", "LREQ", "STREAM", "LRES", 139124251Ssimokawa "undef", "undef", "PHY", "undef" 140124251Ssimokawa}; 141124251Ssimokawa 142114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 143114909Ssimokawa#define MAX_GAPHOP 15 144114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 145114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 146106813Ssimokawa 147103285Sikobstatic driver_t firewire_driver = { 148103285Sikob "firewire", 149103285Sikob firewire_methods, 150103285Sikob sizeof(struct firewire_softc), 151103285Sikob}; 152103285Sikob 153103285Sikob/* 154110072Ssimokawa * Lookup fwdev by node id. 155103285Sikob */ 156106810Ssimokawastruct fw_device * 157110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 158103285Sikob{ 159103285Sikob struct fw_device *fwdev; 160110072Ssimokawa int s; 161110072Ssimokawa 162110072Ssimokawa s = splfw(); 163110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 164120660Ssimokawa if (fwdev->dst == dst && fwdev->status != FWDEVINVAL) 165103285Sikob break; 166110072Ssimokawa splx(s); 167110072Ssimokawa 168106810Ssimokawa return fwdev; 169103285Sikob} 170106813Ssimokawa 171103285Sikob/* 172110072Ssimokawa * Lookup fwdev by EUI64. 173110072Ssimokawa */ 174110072Ssimokawastruct fw_device * 175110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 176110072Ssimokawa{ 177110072Ssimokawa struct fw_device *fwdev; 178110072Ssimokawa int s; 179110072Ssimokawa 180110072Ssimokawa s = splfw(); 181110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 182110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 183110072Ssimokawa break; 184110072Ssimokawa splx(s); 185110072Ssimokawa 186110072Ssimokawa if(fwdev == NULL) return NULL; 187110072Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 188110072Ssimokawa return fwdev; 189110072Ssimokawa} 190110072Ssimokawa 191110072Ssimokawa/* 192103285Sikob * Async. request procedure for userland application. 193103285Sikob */ 194103285Sikobint 195103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 196103285Sikob{ 197103285Sikob int err = 0; 198103285Sikob struct fw_xferq *xferq; 199169119Ssimokawa int tl = -1, len; 200103285Sikob struct fw_pkt *fp; 201103285Sikob int tcode; 202103285Sikob struct tcode_info *info; 203103285Sikob 204103285Sikob if(xfer == NULL) return EINVAL; 205167632Ssimokawa if(xfer->hand == NULL){ 206167632Ssimokawa printf("hand == NULL\n"); 207103285Sikob return EINVAL; 208103285Sikob } 209120660Ssimokawa fp = &xfer->send.hdr; 210103285Sikob 211103285Sikob tcode = fp->mode.common.tcode & 0xf; 212103285Sikob info = &fc->tcode[tcode]; 213103285Sikob if (info->flag == 0) { 214124251Ssimokawa printf("invalid tcode=%x\n", tcode); 215103285Sikob return EINVAL; 216103285Sikob } 217103285Sikob if (info->flag & FWTI_REQ) 218103285Sikob xferq = fc->atq; 219103285Sikob else 220103285Sikob xferq = fc->ats; 221103285Sikob len = info->hdr_len; 222120660Ssimokawa if (xfer->send.pay_len > MAXREC(fc->maxrec)) { 223120660Ssimokawa printf("send.pay_len > maxrec\n"); 224120660Ssimokawa return EINVAL; 225120660Ssimokawa } 226103285Sikob if (info->flag & FWTI_BLOCK_STR) 227120660Ssimokawa len = fp->mode.stream.len; 228103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 229120660Ssimokawa len = fp->mode.rresb.len; 230120660Ssimokawa else 231120660Ssimokawa len = 0; 232120660Ssimokawa if (len != xfer->send.pay_len){ 233124251Ssimokawa printf("len(%d) != send.pay_len(%d) %s(%x)\n", 234124251Ssimokawa len, xfer->send.pay_len, tcode_str[tcode], tcode); 235103285Sikob return EINVAL; 236103285Sikob } 237106790Ssimokawa 238103285Sikob if(xferq->start == NULL){ 239103285Sikob printf("xferq->start == NULL\n"); 240103285Sikob return EINVAL; 241103285Sikob } 242103285Sikob if(!(xferq->queued < xferq->maxq)){ 243108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 244108655Ssimokawa xferq->queued); 245103285Sikob return EINVAL; 246103285Sikob } 247103285Sikob 248103285Sikob if (info->flag & FWTI_TLABEL) { 249130460Sdfr if ((tl = fw_get_tlabel(fc, xfer)) == -1) 250130460Sdfr return EAGAIN; 251103285Sikob fp->mode.hdr.tlrt = tl << 2; 252103285Sikob } 253103285Sikob 254103285Sikob xfer->tl = tl; 255103285Sikob xfer->resp = 0; 256103285Sikob xfer->fc = fc; 257103285Sikob xfer->q = xferq; 258103285Sikob 259103285Sikob fw_asystart(xfer); 260103285Sikob return err; 261103285Sikob} 262103285Sikob/* 263103285Sikob * Wakeup blocked process. 264103285Sikob */ 265103285Sikobvoid 266103285Sikobfw_asy_callback(struct fw_xfer *xfer){ 267103285Sikob wakeup(xfer); 268103285Sikob return; 269103285Sikob} 270103285Sikob 271103285Sikob/* 272103285Sikob * Async. request with given xfer structure. 273103285Sikob */ 274106790Ssimokawastatic void 275106790Ssimokawafw_asystart(struct fw_xfer *xfer) 276106790Ssimokawa{ 277103285Sikob struct firewire_comm *fc = xfer->fc; 278103285Sikob int s; 279103285Sikob#if 0 /* XXX allow bus explore packets only after bus rest */ 280103285Sikob if (fc->status < FWBUSEXPLORE) { 281103285Sikob xfer->resp = EAGAIN; 282103285Sikob xfer->state = FWXF_BUSY; 283167632Ssimokawa if (xfer->hand != NULL) 284167632Ssimokawa xfer->hand(xfer); 285103285Sikob return; 286103285Sikob } 287103285Sikob#endif 288127468Ssimokawa microtime(&xfer->tv); 289103285Sikob s = splfw(); 290103285Sikob xfer->state = FWXF_INQ; 291103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 292103285Sikob xfer->q->queued ++; 293103285Sikob splx(s); 294103285Sikob /* XXX just queue for mbuf */ 295103285Sikob if (xfer->mbuf == NULL) 296103285Sikob xfer->q->start(fc); 297103285Sikob return; 298103285Sikob} 299106790Ssimokawa 300125238Ssimokawastatic void 301125238Ssimokawafirewire_identify(driver_t *driver, device_t parent) 302125238Ssimokawa{ 303125238Ssimokawa BUS_ADD_CHILD(parent, 0, "firewire", -1); 304125238Ssimokawa} 305125238Ssimokawa 306103285Sikobstatic int 307125238Ssimokawafirewire_probe(device_t dev) 308103285Sikob{ 309108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 310125238Ssimokawa return (0); 311103285Sikob} 312106790Ssimokawa 313110577Ssimokawastatic void 314110577Ssimokawafirewire_xfer_timeout(struct firewire_comm *fc) 315110577Ssimokawa{ 316110577Ssimokawa struct fw_xfer *xfer; 317110577Ssimokawa struct timeval tv; 318110577Ssimokawa struct timeval split_timeout; 319111040Ssimokawa int i, s; 320110577Ssimokawa 321120660Ssimokawa split_timeout.tv_sec = 0; 322120660Ssimokawa split_timeout.tv_usec = 200 * 1000; /* 200 msec */ 323110577Ssimokawa 324110577Ssimokawa microtime(&tv); 325110577Ssimokawa timevalsub(&tv, &split_timeout); 326110577Ssimokawa 327111040Ssimokawa s = splfw(); 328110577Ssimokawa for (i = 0; i < 0x40; i ++) { 329169119Ssimokawa while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 330110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 331110577Ssimokawa /* the rests are newer than this */ 332110577Ssimokawa break; 333124145Ssimokawa if (xfer->state == FWXF_START) 334124145Ssimokawa /* not sent yet */ 335124145Ssimokawa break; 336110577Ssimokawa device_printf(fc->bdev, 337117716Ssimokawa "split transaction timeout dst=0x%x tl=0x%x state=%d\n", 338120660Ssimokawa xfer->send.hdr.mode.hdr.dst, i, xfer->state); 339110577Ssimokawa xfer->resp = ETIMEDOUT; 340113584Ssimokawa fw_xfer_done(xfer); 341110577Ssimokawa } 342110577Ssimokawa } 343111040Ssimokawa splx(s); 344110577Ssimokawa} 345110577Ssimokawa 346121463Ssimokawa#define WATCHDOC_HZ 10 347110577Ssimokawastatic void 348110577Ssimokawafirewire_watchdog(void *arg) 349110577Ssimokawa{ 350110577Ssimokawa struct firewire_comm *fc; 351121463Ssimokawa static int watchdoc_clock = 0; 352110577Ssimokawa 353110577Ssimokawa fc = (struct firewire_comm *)arg; 354121463Ssimokawa 355121463Ssimokawa /* 356121463Ssimokawa * At boot stage, the device interrupt is disabled and 357121463Ssimokawa * We encounter a timeout easily. To avoid this, 358121463Ssimokawa * ignore clock interrupt for a while. 359121463Ssimokawa */ 360121463Ssimokawa if (watchdoc_clock > WATCHDOC_HZ * 15) { 361121463Ssimokawa firewire_xfer_timeout(fc); 362121463Ssimokawa fc->timeout(fc); 363121463Ssimokawa } else 364121463Ssimokawa watchdoc_clock ++; 365121463Ssimokawa 366121463Ssimokawa callout_reset(&fc->timeout_callout, hz / WATCHDOC_HZ, 367110577Ssimokawa (void *)firewire_watchdog, (void *)fc); 368110577Ssimokawa} 369110577Ssimokawa 370103285Sikob/* 371103285Sikob * The attach routine. 372103285Sikob */ 373103285Sikobstatic int 374118455Ssimokawafirewire_attach(device_t dev) 375103285Sikob{ 376118455Ssimokawa int unit; 377103285Sikob struct firewire_softc *sc = device_get_softc(dev); 378103285Sikob device_t pa = device_get_parent(dev); 379103285Sikob struct firewire_comm *fc; 380169806Ssimokawa struct proc *p; 381103285Sikob 382103285Sikob fc = (struct firewire_comm *)device_get_softc(pa); 383103285Sikob sc->fc = fc; 384116978Ssimokawa fc->status = FWBUSNOTREADY; 385103285Sikob 386118455Ssimokawa unit = device_get_unit(dev); 387118455Ssimokawa if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 388103285Sikob 389118455Ssimokawa fwdev_makedev(sc); 390118455Ssimokawa 391110193Ssimokawa CALLOUT_INIT(&sc->fc->timeout_callout); 392110193Ssimokawa CALLOUT_INIT(&sc->fc->bmr_callout); 393110193Ssimokawa CALLOUT_INIT(&sc->fc->busprobe_callout); 394108853Ssimokawa 395110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 396110577Ssimokawa (void *)firewire_watchdog, (void *)sc->fc); 397110193Ssimokawa 398169806Ssimokawa /* create thread */ 399169806Ssimokawa kthread_create(fw_bus_probe_thread, (void *)fc, &p, 400169806Ssimokawa 0, 0, "fw%d_probe", unit); 401169806Ssimokawa 402103285Sikob /* Locate our children */ 403103285Sikob bus_generic_probe(dev); 404103285Sikob 405103285Sikob /* launch attachement of the added children */ 406103285Sikob bus_generic_attach(dev); 407103285Sikob 408103285Sikob /* bus_reset */ 409169117Ssimokawa fw_busreset(fc, FWBUSNOTREADY); 410103285Sikob fc->ibr(fc); 411103285Sikob 412103285Sikob return 0; 413103285Sikob} 414103285Sikob 415103285Sikob/* 416103285Sikob * Attach it as child. 417103285Sikob */ 418103285Sikobstatic device_t 419103285Sikobfirewire_add_child(device_t dev, int order, const char *name, int unit) 420103285Sikob{ 421103285Sikob device_t child; 422103285Sikob struct firewire_softc *sc; 423103285Sikob 424103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 425103285Sikob child = device_add_child(dev, name, unit); 426103285Sikob if (child) { 427103285Sikob device_set_ivars(child, sc->fc); 428103285Sikob device_probe_and_attach(child); 429103285Sikob } 430103285Sikob 431103285Sikob return child; 432103285Sikob} 433106790Ssimokawa 434116978Ssimokawastatic int 435116978Ssimokawafirewire_resume(device_t dev) 436116978Ssimokawa{ 437116978Ssimokawa struct firewire_softc *sc; 438116978Ssimokawa 439116978Ssimokawa sc = (struct firewire_softc *)device_get_softc(dev); 440116978Ssimokawa sc->fc->status = FWBUSNOTREADY; 441116978Ssimokawa 442116978Ssimokawa bus_generic_resume(dev); 443116978Ssimokawa 444116978Ssimokawa return(0); 445116978Ssimokawa} 446116978Ssimokawa 447103285Sikob/* 448103285Sikob * Dettach it. 449103285Sikob */ 450103285Sikobstatic int 451118455Ssimokawafirewire_detach(device_t dev) 452103285Sikob{ 453103285Sikob struct firewire_softc *sc; 454169806Ssimokawa struct firewire_comm *fc; 455111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 456118455Ssimokawa int err; 457103285Sikob 458103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 459169806Ssimokawa fc = sc->fc; 460169806Ssimokawa fc->status = FWBUSDETACH; 461169806Ssimokawa 462118455Ssimokawa if ((err = fwdev_destroydev(sc)) != 0) 463118455Ssimokawa return err; 464106790Ssimokawa 465118455Ssimokawa if ((err = bus_generic_detach(dev)) != 0) 466118455Ssimokawa return err; 467111078Ssimokawa 468169806Ssimokawa callout_stop(&fc->timeout_callout); 469169806Ssimokawa callout_stop(&fc->bmr_callout); 470169806Ssimokawa callout_stop(&fc->busprobe_callout); 471111078Ssimokawa 472103285Sikob /* XXX xfree_free and untimeout on all xfers */ 473169806Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; 474111078Ssimokawa fwdev = fwdev_next) { 475111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 476111078Ssimokawa free(fwdev, M_FW); 477111078Ssimokawa } 478169806Ssimokawa free(fc->topology_map, M_FW); 479169806Ssimokawa free(fc->speed_map, M_FW); 480169806Ssimokawa free(fc->crom_src_buf, M_FW); 481169806Ssimokawa 482169806Ssimokawa wakeup(fc); 483169806Ssimokawa if (tsleep(fc, PWAIT, "fwthr", hz * 60)) 484169806Ssimokawa printf("firewire task thread didn't die\n"); 485169806Ssimokawa 486103285Sikob return(0); 487103285Sikob} 488103285Sikob#if 0 489103285Sikobstatic int 490103285Sikobfirewire_shutdown( device_t dev ) 491103285Sikob{ 492103285Sikob return 0; 493103285Sikob} 494103285Sikob#endif 495106790Ssimokawa 496110577Ssimokawa 497110577Ssimokawastatic void 498110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 499110577Ssimokawa{ 500110577Ssimokawa struct fw_xfer *xfer; 501110577Ssimokawa 502110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 503110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 504111942Ssimokawa xferq->queued --; 505110577Ssimokawa xfer->resp = EAGAIN; 506169131Ssimokawa xfer->state = FWXF_SENTERR; 507113584Ssimokawa fw_xfer_done(xfer); 508110577Ssimokawa } 509110577Ssimokawa} 510110577Ssimokawa 511110798Ssimokawavoid 512110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 513110798Ssimokawa{ 514110798Ssimokawa int i; 515110798Ssimokawa 516110798Ssimokawa fw_xferq_drain(fc->atq); 517110798Ssimokawa fw_xferq_drain(fc->ats); 518110798Ssimokawa for(i = 0; i < fc->nisodma; i++) 519110798Ssimokawa fw_xferq_drain(fc->it[i]); 520110798Ssimokawa} 521110798Ssimokawa 522116376Ssimokawastatic void 523116376Ssimokawafw_reset_csr(struct firewire_comm *fc) 524103285Sikob{ 525103285Sikob int i; 526103285Sikob 527103285Sikob CSRARC(fc, STATE_CLEAR) 528103285Sikob = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 529103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 530103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 531103285Sikob 532103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 533103285Sikob fc->irm = -1; 534103285Sikob 535103285Sikob fc->max_node = -1; 536103285Sikob 537103285Sikob for(i = 2; i < 0x100/4 - 2 ; i++){ 538103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 539103285Sikob } 540103285Sikob CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 541103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 542103285Sikob CSRARC(fc, RESET_START) = 0; 543103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 544103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 545103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 546103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 547103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 548103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 549103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 550103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 551103285Sikob CSRARC(fc, IP_CHANNELS) = (1 << 31); 552103285Sikob 553103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 554103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 555103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 556103285Sikob 1 << 28 | 0xff << 16 | 0x09 << 8; 557103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 558103285Sikob 559103285Sikob/* DV depend CSRs see blue book */ 560103285Sikob CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 561103285Sikob CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 562103285Sikob 563103285Sikob CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 ); 564103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 565116376Ssimokawa} 566113584Ssimokawa 567116376Ssimokawastatic void 568116376Ssimokawafw_init_crom(struct firewire_comm *fc) 569116376Ssimokawa{ 570116376Ssimokawa struct crom_src *src; 571116376Ssimokawa 572116376Ssimokawa fc->crom_src_buf = (struct crom_src_buf *) 573116376Ssimokawa malloc(sizeof(struct crom_src_buf), M_FW, M_WAITOK | M_ZERO); 574116376Ssimokawa if (fc->crom_src_buf == NULL) 575116376Ssimokawa return; 576116376Ssimokawa 577116376Ssimokawa src = &fc->crom_src_buf->src; 578116376Ssimokawa bzero(src, sizeof(struct crom_src)); 579116376Ssimokawa 580116376Ssimokawa /* BUS info sample */ 581116376Ssimokawa src->hdr.info_len = 4; 582116376Ssimokawa 583116376Ssimokawa src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 584116376Ssimokawa 585116376Ssimokawa src->businfo.irmc = 1; 586116376Ssimokawa src->businfo.cmc = 1; 587116376Ssimokawa src->businfo.isc = 1; 588116376Ssimokawa src->businfo.bmc = 1; 589116376Ssimokawa src->businfo.pmc = 0; 590116376Ssimokawa src->businfo.cyc_clk_acc = 100; 591116376Ssimokawa src->businfo.max_rec = fc->maxrec; 592116376Ssimokawa src->businfo.max_rom = MAXROM_4; 593117350Ssimokawa src->businfo.generation = 1; 594116376Ssimokawa src->businfo.link_spd = fc->speed; 595116376Ssimokawa 596116376Ssimokawa src->businfo.eui64.hi = fc->eui.hi; 597116376Ssimokawa src->businfo.eui64.lo = fc->eui.lo; 598116376Ssimokawa 599116376Ssimokawa STAILQ_INIT(&src->chunk_list); 600116376Ssimokawa 601116376Ssimokawa fc->crom_src = src; 602116376Ssimokawa fc->crom_root = &fc->crom_src_buf->root; 603116376Ssimokawa} 604116376Ssimokawa 605116376Ssimokawastatic void 606116376Ssimokawafw_reset_crom(struct firewire_comm *fc) 607116376Ssimokawa{ 608116376Ssimokawa struct crom_src_buf *buf; 609116376Ssimokawa struct crom_src *src; 610116376Ssimokawa struct crom_chunk *root; 611116376Ssimokawa 612116376Ssimokawa if (fc->crom_src_buf == NULL) 613116376Ssimokawa fw_init_crom(fc); 614116376Ssimokawa 615116376Ssimokawa buf = fc->crom_src_buf; 616116376Ssimokawa src = fc->crom_src; 617116376Ssimokawa root = fc->crom_root; 618116376Ssimokawa 619116376Ssimokawa STAILQ_INIT(&src->chunk_list); 620116376Ssimokawa 621116376Ssimokawa bzero(root, sizeof(struct crom_chunk)); 622116376Ssimokawa crom_add_chunk(src, NULL, root, 0); 623116376Ssimokawa crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 624116376Ssimokawa /* private company_id */ 625116376Ssimokawa crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 626127468Ssimokawa#ifdef __DragonFly__ 627127468Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project"); 628127468Ssimokawa crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version); 629127468Ssimokawa#else 630116376Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project"); 631116376Ssimokawa crom_add_entry(root, CSRKEY_HW, __FreeBSD_version); 632127468Ssimokawa#endif 633116376Ssimokawa crom_add_simple_text(src, root, &buf->hw, hostname); 634116376Ssimokawa} 635116376Ssimokawa 636116376Ssimokawa/* 637116376Ssimokawa * Called after bus reset. 638116376Ssimokawa */ 639116376Ssimokawavoid 640169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status) 641116376Ssimokawa{ 642116376Ssimokawa struct firewire_dev_comm *fdc; 643117350Ssimokawa struct crom_src *src; 644116376Ssimokawa device_t *devlistp; 645117350Ssimokawa void *newrom; 646116376Ssimokawa int i, devcnt; 647116376Ssimokawa 648116376Ssimokawa switch(fc->status){ 649116376Ssimokawa case FWBUSMGRELECT: 650116376Ssimokawa callout_stop(&fc->bmr_callout); 651116376Ssimokawa break; 652116376Ssimokawa default: 653116376Ssimokawa break; 654116376Ssimokawa } 655169117Ssimokawa fc->status = new_status; 656116376Ssimokawa fw_reset_csr(fc); 657116376Ssimokawa fw_reset_crom(fc); 658116376Ssimokawa 659113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 660113584Ssimokawa for( i = 0 ; i < devcnt ; i++) 661113584Ssimokawa if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 662113584Ssimokawa fdc = device_get_softc(devlistp[i]); 663113584Ssimokawa if (fdc->post_busreset != NULL) 664113584Ssimokawa fdc->post_busreset(fdc); 665113584Ssimokawa } 666113584Ssimokawa free(devlistp, M_TEMP); 667113584Ssimokawa } 668116376Ssimokawa 669117350Ssimokawa newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO); 670117350Ssimokawa src = &fc->crom_src_buf->src; 671129585Sdfr crom_load(src, (uint32_t *)newrom, CROMSIZE); 672117350Ssimokawa if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) { 673117350Ssimokawa /* bump generation and reload */ 674117350Ssimokawa src->businfo.generation ++; 675117350Ssimokawa /* generation must be between 0x2 and 0xF */ 676117350Ssimokawa if (src->businfo.generation < 2) 677117350Ssimokawa src->businfo.generation ++; 678129585Sdfr crom_load(src, (uint32_t *)newrom, CROMSIZE); 679117350Ssimokawa bcopy(newrom, (void *)fc->config_rom, CROMSIZE); 680117350Ssimokawa } 681117350Ssimokawa free(newrom, M_FW); 682103285Sikob} 683106790Ssimokawa 684103285Sikob/* Call once after reboot */ 685106790Ssimokawavoid fw_init(struct firewire_comm *fc) 686103285Sikob{ 687103285Sikob int i; 688106543Ssimokawa#ifdef FW_VMACCESS 689103285Sikob struct fw_xfer *xfer; 690103285Sikob struct fw_bind *fwb; 691106543Ssimokawa#endif 692103285Sikob 693103285Sikob fc->arq->queued = 0; 694103285Sikob fc->ars->queued = 0; 695103285Sikob fc->atq->queued = 0; 696103285Sikob fc->ats->queued = 0; 697103285Sikob 698103285Sikob fc->arq->buf = NULL; 699103285Sikob fc->ars->buf = NULL; 700103285Sikob fc->atq->buf = NULL; 701103285Sikob fc->ats->buf = NULL; 702103285Sikob 703113584Ssimokawa fc->arq->flag = 0; 704113584Ssimokawa fc->ars->flag = 0; 705113584Ssimokawa fc->atq->flag = 0; 706113584Ssimokawa fc->ats->flag = 0; 707103285Sikob 708103285Sikob STAILQ_INIT(&fc->atq->q); 709103285Sikob STAILQ_INIT(&fc->ats->q); 710103285Sikob 711103285Sikob for( i = 0 ; i < fc->nisodma ; i ++ ){ 712103285Sikob fc->it[i]->queued = 0; 713103285Sikob fc->ir[i]->queued = 0; 714103285Sikob 715103285Sikob fc->it[i]->start = NULL; 716103285Sikob fc->ir[i]->start = NULL; 717103285Sikob 718103285Sikob fc->it[i]->buf = NULL; 719103285Sikob fc->ir[i]->buf = NULL; 720103285Sikob 721103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 722103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 723103285Sikob 724103285Sikob STAILQ_INIT(&fc->it[i]->q); 725103285Sikob STAILQ_INIT(&fc->ir[i]->q); 726103285Sikob } 727103285Sikob 728103285Sikob fc->arq->maxq = FWMAXQUEUE; 729103285Sikob fc->ars->maxq = FWMAXQUEUE; 730103285Sikob fc->atq->maxq = FWMAXQUEUE; 731103285Sikob fc->ats->maxq = FWMAXQUEUE; 732103285Sikob 733103285Sikob for( i = 0 ; i < fc->nisodma ; i++){ 734103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 735103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 736103285Sikob } 737103285Sikob/* Initialize csr registers */ 738103285Sikob fc->topology_map = (struct fw_topology_map *)malloc( 739103285Sikob sizeof(struct fw_topology_map), 740110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 741103285Sikob fc->speed_map = (struct fw_speed_map *)malloc( 742103285Sikob sizeof(struct fw_speed_map), 743110195Ssimokawa M_FW, M_NOWAIT | M_ZERO); 744103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 745103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 746103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 747103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 748103285Sikob 749110193Ssimokawa STAILQ_INIT(&fc->devices); 750103285Sikob 751103285Sikob/* Initialize Async handlers */ 752103285Sikob STAILQ_INIT(&fc->binds); 753103285Sikob for( i = 0 ; i < 0x40 ; i++){ 754103285Sikob STAILQ_INIT(&fc->tlabels[i]); 755103285Sikob } 756103285Sikob 757103285Sikob/* DV depend CSRs see blue book */ 758103285Sikob#if 0 759103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 760103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 761103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 762103285Sikob CSRARC(fc, i + oPCR) = 0x8000007a; 763103285Sikob } 764103285Sikob 765103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 766103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 767103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 768103285Sikob CSRARC(fc, i + iPCR) = 0x0; 769103285Sikob } 770103285Sikob#endif 771103285Sikob 772116376Ssimokawa fc->crom_src_buf = NULL; 773103285Sikob 774106543Ssimokawa#ifdef FW_VMACCESS 775103285Sikob xfer = fw_xfer_alloc(); 776103285Sikob if(xfer == NULL) return; 777103285Sikob 778110195Ssimokawa fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 779103285Sikob if(fwb == NULL){ 780103285Sikob fw_xfer_free(xfer); 781139680Sjmg return; 782103285Sikob } 783167632Ssimokawa xfer->hand = fw_vmaccess; 784103285Sikob xfer->fc = fc; 785103285Sikob xfer->sc = NULL; 786103285Sikob 787103285Sikob fwb->start_hi = 0x2; 788103285Sikob fwb->start_lo = 0; 789103285Sikob fwb->addrlen = 0xffffffff; 790103285Sikob fwb->xfer = xfer; 791103285Sikob fw_bindadd(fc, fwb); 792106543Ssimokawa#endif 793103285Sikob} 794106790Ssimokawa 795120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\ 796120660Ssimokawa ((fwb)->end < (addr))?1:0) 797120660Ssimokawa 798103285Sikob/* 799129541Sdfr * To lookup bound process from IEEE1394 address. 800103285Sikob */ 801106813Ssimokawastruct fw_bind * 802129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo) 803103285Sikob{ 804120660Ssimokawa u_int64_t addr; 805103285Sikob struct fw_bind *tfw; 806120660Ssimokawa 807120660Ssimokawa addr = ((u_int64_t)dest_hi << 32) | dest_lo; 808120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 809169130Ssimokawa if (BIND_CMP(addr, tfw) == 0) 810103285Sikob return(tfw); 811103285Sikob return(NULL); 812103285Sikob} 813106790Ssimokawa 814103285Sikob/* 815103285Sikob * To bind IEEE1394 address block to process. 816103285Sikob */ 817106790Ssimokawaint 818106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 819103285Sikob{ 820120660Ssimokawa struct fw_bind *tfw, *prev = NULL; 821120660Ssimokawa 822120660Ssimokawa if (fwb->start > fwb->end) { 823127468Ssimokawa printf("%s: invalid range\n", __func__); 824120660Ssimokawa return EINVAL; 825120660Ssimokawa } 826120660Ssimokawa 827120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) { 828120660Ssimokawa if (fwb->end < tfw->start) 829120660Ssimokawa break; 830120660Ssimokawa prev = tfw; 831120660Ssimokawa } 832120660Ssimokawa if (prev == NULL) { 833103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 834169130Ssimokawa return (0); 835103285Sikob } 836120660Ssimokawa if (prev->end < fwb->start) { 837120660Ssimokawa STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 838169130Ssimokawa return (0); 839103285Sikob } 840120660Ssimokawa 841127468Ssimokawa printf("%s: bind failed\n", __func__); 842120660Ssimokawa return (EBUSY); 843103285Sikob} 844103285Sikob 845103285Sikob/* 846103285Sikob * To free IEEE1394 address block. 847103285Sikob */ 848106790Ssimokawaint 849106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 850103285Sikob{ 851120660Ssimokawa#if 0 852120660Ssimokawa struct fw_xfer *xfer, *next; 853120660Ssimokawa#endif 854120660Ssimokawa struct fw_bind *tfw; 855103285Sikob int s; 856103285Sikob 857103285Sikob s = splfw(); 858120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 859120660Ssimokawa if (tfw == fwb) { 860120660Ssimokawa STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 861120660Ssimokawa goto found; 862120660Ssimokawa } 863120660Ssimokawa 864129541Sdfr printf("%s: no such binding\n", __func__); 865120660Ssimokawa splx(s); 866120660Ssimokawa return (1); 867120660Ssimokawafound: 868120660Ssimokawa#if 0 869113584Ssimokawa /* shall we do this? */ 870113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 871113584Ssimokawa next = STAILQ_NEXT(xfer, link); 872113584Ssimokawa fw_xfer_free(xfer); 873113584Ssimokawa } 874113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 875120660Ssimokawa#endif 876113584Ssimokawa 877103285Sikob splx(s); 878103285Sikob return 0; 879103285Sikob} 880103285Sikob 881169130Ssimokawaint 882169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type, 883169130Ssimokawa int slen, int rlen, int n, 884169130Ssimokawa struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *)) 885169130Ssimokawa{ 886169130Ssimokawa int i, s; 887169130Ssimokawa struct fw_xfer *xfer; 888169130Ssimokawa 889169130Ssimokawa for (i = 0; i < n; i++) { 890169130Ssimokawa xfer = fw_xfer_alloc_buf(type, slen, rlen); 891169130Ssimokawa if (xfer == NULL) 892169130Ssimokawa return (n); 893169130Ssimokawa xfer->fc = fc; 894169130Ssimokawa xfer->sc = sc; 895169130Ssimokawa xfer->hand = hand; 896169130Ssimokawa s = splfw(); 897169130Ssimokawa STAILQ_INSERT_TAIL(q, xfer, link); 898169130Ssimokawa splx(s); 899169130Ssimokawa } 900169130Ssimokawa return (n); 901169130Ssimokawa} 902169130Ssimokawa 903169130Ssimokawavoid 904169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q) 905169130Ssimokawa{ 906169130Ssimokawa struct fw_xfer *xfer, *next; 907169130Ssimokawa 908169130Ssimokawa for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) { 909169130Ssimokawa next = STAILQ_NEXT(xfer, link); 910169130Ssimokawa fw_xfer_free_buf(xfer); 911169130Ssimokawa } 912169130Ssimokawa STAILQ_INIT(q); 913169130Ssimokawa} 914169130Ssimokawa 915103285Sikob/* 916103285Sikob * To free transaction label. 917103285Sikob */ 918106790Ssimokawastatic void 919106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 920103285Sikob{ 921169119Ssimokawa struct fw_xfer *txfer; 922169119Ssimokawa int s; 923103285Sikob 924169119Ssimokawa if (xfer->tl < 0) 925169119Ssimokawa return; 926169119Ssimokawa 927169119Ssimokawa s = splfw(); 928169119Ssimokawa#if 1 /* make sure the label is allocated */ 929169119Ssimokawa STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel) 930169119Ssimokawa if(txfer == xfer) 931169119Ssimokawa break; 932169119Ssimokawa if (txfer == NULL) { 933169119Ssimokawa printf("%s: the xfer is not in the tlabel(%d)\n", 934169119Ssimokawa __FUNCTION__, xfer->tl); 935169119Ssimokawa splx(s); 936169119Ssimokawa return; 937103285Sikob } 938169119Ssimokawa#endif 939169119Ssimokawa 940169119Ssimokawa STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel); 941103285Sikob splx(s); 942103285Sikob return; 943103285Sikob} 944106790Ssimokawa 945103285Sikob/* 946103285Sikob * To obtain XFER structure by transaction label. 947103285Sikob */ 948106790Ssimokawastatic struct fw_xfer * 949106790Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel) 950103285Sikob{ 951103285Sikob struct fw_xfer *xfer; 952103285Sikob int s = splfw(); 953103285Sikob 954169119Ssimokawa STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel) 955169119Ssimokawa if(xfer->send.hdr.mode.hdr.dst == node) { 956103285Sikob splx(s); 957110577Ssimokawa if (firewire_debug > 2) 958110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 959103285Sikob return(xfer); 960103285Sikob } 961110577Ssimokawa if (firewire_debug > 1) 962110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 963103285Sikob splx(s); 964103285Sikob return(NULL); 965103285Sikob} 966106790Ssimokawa 967103285Sikob/* 968103285Sikob * To allocate IEEE1394 XFER structure. 969103285Sikob */ 970106790Ssimokawastruct fw_xfer * 971110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 972103285Sikob{ 973103285Sikob struct fw_xfer *xfer; 974106790Ssimokawa 975110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 976106790Ssimokawa if (xfer == NULL) 977106790Ssimokawa return xfer; 978106790Ssimokawa 979110269Ssimokawa xfer->malloc = type; 980106790Ssimokawa 981103285Sikob return xfer; 982103285Sikob} 983106790Ssimokawa 984113584Ssimokawastruct fw_xfer * 985113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 986113584Ssimokawa{ 987113584Ssimokawa struct fw_xfer *xfer; 988113584Ssimokawa 989113584Ssimokawa xfer = fw_xfer_alloc(type); 990126102Scperciva if (xfer == NULL) 991126102Scperciva return(NULL); 992120660Ssimokawa xfer->send.pay_len = send_len; 993120660Ssimokawa xfer->recv.pay_len = recv_len; 994120660Ssimokawa if (send_len > 0) { 995120660Ssimokawa xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 996120660Ssimokawa if (xfer->send.payload == NULL) { 997113584Ssimokawa fw_xfer_free(xfer); 998113584Ssimokawa return(NULL); 999113584Ssimokawa } 1000113584Ssimokawa } 1001120660Ssimokawa if (recv_len > 0) { 1002120660Ssimokawa xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 1003120660Ssimokawa if (xfer->recv.payload == NULL) { 1004120660Ssimokawa if (xfer->send.payload != NULL) 1005120660Ssimokawa free(xfer->send.payload, type); 1006113584Ssimokawa fw_xfer_free(xfer); 1007113584Ssimokawa return(NULL); 1008113584Ssimokawa } 1009113584Ssimokawa } 1010113584Ssimokawa return(xfer); 1011113584Ssimokawa} 1012113584Ssimokawa 1013103285Sikob/* 1014103285Sikob * IEEE1394 XFER post process. 1015103285Sikob */ 1016103285Sikobvoid 1017103285Sikobfw_xfer_done(struct fw_xfer *xfer) 1018103285Sikob{ 1019167632Ssimokawa if (xfer->hand == NULL) { 1020167632Ssimokawa printf("hand == NULL\n"); 1021103285Sikob return; 1022117716Ssimokawa } 1023103285Sikob 1024121505Ssimokawa if (xfer->fc == NULL) 1025121505Ssimokawa panic("fw_xfer_done: why xfer->fc is NULL?"); 1026121505Ssimokawa 1027168051Ssimokawa fw_tl_free(xfer->fc, xfer); 1028167632Ssimokawa xfer->hand(xfer); 1029103285Sikob} 1030103285Sikob 1031106790Ssimokawavoid 1032113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer) 1033103285Sikob{ 1034103285Sikob int s; 1035113584Ssimokawa 1036103285Sikob if(xfer == NULL ) return; 1037103285Sikob if(xfer->state == FWXF_INQ){ 1038103285Sikob printf("fw_xfer_free FWXF_INQ\n"); 1039103285Sikob s = splfw(); 1040103285Sikob STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1041103285Sikob xfer->q->queued --; 1042103285Sikob splx(s); 1043103285Sikob } 1044113584Ssimokawa if (xfer->fc != NULL) { 1045114729Ssimokawa#if 1 1046113584Ssimokawa if(xfer->state == FWXF_START) 1047114729Ssimokawa /* 1048114729Ssimokawa * This could happen if: 1049114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 1050114729Ssimokawa * 2. firewire_watch() is called. 1051114729Ssimokawa */ 1052114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 1053103285Sikob#endif 1054103285Sikob } 1055113584Ssimokawa xfer->state = FWXF_INIT; 1056113584Ssimokawa xfer->resp = 0; 1057113584Ssimokawa} 1058113584Ssimokawa/* 1059113584Ssimokawa * To free IEEE1394 XFER structure. 1060113584Ssimokawa */ 1061113584Ssimokawavoid 1062120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer) 1063113584Ssimokawa{ 1064120660Ssimokawa if (xfer == NULL) { 1065127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1066120660Ssimokawa return; 1067120660Ssimokawa } 1068113584Ssimokawa fw_xfer_unload(xfer); 1069120660Ssimokawa if(xfer->send.payload != NULL){ 1070120660Ssimokawa free(xfer->send.payload, xfer->malloc); 1071103285Sikob } 1072120660Ssimokawa if(xfer->recv.payload != NULL){ 1073120660Ssimokawa free(xfer->recv.payload, xfer->malloc); 1074103285Sikob } 1075110269Ssimokawa free(xfer, xfer->malloc); 1076103285Sikob} 1077103285Sikob 1078120660Ssimokawavoid 1079120660Ssimokawafw_xfer_free( struct fw_xfer* xfer) 1080120660Ssimokawa{ 1081120660Ssimokawa if (xfer == NULL) { 1082127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1083120660Ssimokawa return; 1084120660Ssimokawa } 1085120660Ssimokawa fw_xfer_unload(xfer); 1086120660Ssimokawa free(xfer, xfer->malloc); 1087120660Ssimokawa} 1088120660Ssimokawa 1089120660Ssimokawavoid 1090110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 1091103285Sikob{ 1092103285Sikob#if 0 1093110072Ssimokawa printf("asyreq done state=%d resp=%d\n", 1094103285Sikob xfer->state, xfer->resp); 1095103285Sikob#endif 1096103285Sikob fw_xfer_free(xfer); 1097103285Sikob} 1098103285Sikob 1099103285Sikob/* 1100103285Sikob * To configure PHY. 1101103285Sikob */ 1102103285Sikobstatic void 1103103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1104103285Sikob{ 1105103285Sikob struct fw_xfer *xfer; 1106103285Sikob struct fw_pkt *fp; 1107103285Sikob 1108103285Sikob fc->status = FWBUSPHYCONF; 1109103285Sikob 1110120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1111113584Ssimokawa if (xfer == NULL) 1112113584Ssimokawa return; 1113103285Sikob xfer->fc = fc; 1114167632Ssimokawa xfer->hand = fw_asy_callback_free; 1115103285Sikob 1116120660Ssimokawa fp = &xfer->send.hdr; 1117103285Sikob fp->mode.ld[1] = 0; 1118103285Sikob if (root_node >= 0) 1119113584Ssimokawa fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1120103285Sikob if (gap_count >= 0) 1121113584Ssimokawa fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1122103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 1123103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 1124103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 1125103285Sikob 1126107653Ssimokawa if (firewire_debug) 1127107653Ssimokawa printf("send phy_config root_node=%d gap_count=%d\n", 1128103285Sikob root_node, gap_count); 1129103285Sikob fw_asyreq(fc, -1, xfer); 1130103285Sikob} 1131103285Sikob 1132103285Sikob#if 0 1133103285Sikob/* 1134103285Sikob * Dump self ID. 1135103285Sikob */ 1136103285Sikobstatic void 1137129585Sdfrfw_print_sid(uint32_t sid) 1138103285Sikob{ 1139103285Sikob union fw_self_id *s; 1140103285Sikob s = (union fw_self_id *) &sid; 1141103285Sikob printf("node:%d link:%d gap:%d spd:%d del:%d con:%d pwr:%d" 1142103285Sikob " p0:%d p1:%d p2:%d i:%d m:%d\n", 1143103285Sikob s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1144103285Sikob s->p0.phy_speed, s->p0.phy_delay, s->p0.contender, 1145103285Sikob s->p0.power_class, s->p0.port0, s->p0.port1, 1146103285Sikob s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1147103285Sikob} 1148103285Sikob#endif 1149103285Sikob 1150103285Sikob/* 1151103285Sikob * To receive self ID. 1152103285Sikob */ 1153129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len) 1154103285Sikob{ 1155129585Sdfr uint32_t *p; 1156103285Sikob union fw_self_id *self_id; 1157103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1158103285Sikob 1159129585Sdfr fc->sid_cnt = len /(sizeof(uint32_t) * 2); 1160103285Sikob fc->status = FWBUSINIT; 1161103285Sikob fc->max_node = fc->nodeid & 0x3f; 1162129585Sdfr CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16; 1163103285Sikob fc->status = FWBUSCYMELECT; 1164103285Sikob fc->topology_map->crc_len = 2; 1165103285Sikob fc->topology_map->generation ++; 1166103285Sikob fc->topology_map->self_id_count = 0; 1167103285Sikob fc->topology_map->node_count = 0; 1168103285Sikob fc->speed_map->generation ++; 1169103285Sikob fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1170103285Sikob self_id = &fc->topology_map->self_id[0]; 1171103285Sikob for(i = 0; i < fc->sid_cnt; i ++){ 1172103285Sikob if (sid[1] != ~sid[0]) { 1173103285Sikob printf("fw_sidrcv: invalid self-id packet\n"); 1174103285Sikob sid += 2; 1175103285Sikob continue; 1176103285Sikob } 1177103285Sikob *self_id = *((union fw_self_id *)sid); 1178103285Sikob fc->topology_map->crc_len++; 1179103285Sikob if(self_id->p0.sequel == 0){ 1180103285Sikob fc->topology_map->node_count ++; 1181103285Sikob c_port = 0; 1182103285Sikob#if 0 1183103285Sikob fw_print_sid(sid[0]); 1184103285Sikob#endif 1185103285Sikob node = self_id->p0.phy_id; 1186103285Sikob if(fc->max_node < node){ 1187103285Sikob fc->max_node = self_id->p0.phy_id; 1188103285Sikob } 1189103285Sikob /* XXX I'm not sure this is the right speed_map */ 1190103285Sikob fc->speed_map->speed[node][node] 1191103285Sikob = self_id->p0.phy_speed; 1192103285Sikob for (j = 0; j < node; j ++) { 1193103285Sikob fc->speed_map->speed[j][node] 1194103285Sikob = fc->speed_map->speed[node][j] 1195103285Sikob = min(fc->speed_map->speed[j][j], 1196103285Sikob self_id->p0.phy_speed); 1197103285Sikob } 1198103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1199103285Sikob (self_id->p0.link_active && self_id->p0.contender)) { 1200103285Sikob fc->irm = self_id->p0.phy_id; 1201103285Sikob } 1202103285Sikob if(self_id->p0.port0 >= 0x2){ 1203103285Sikob c_port++; 1204103285Sikob } 1205103285Sikob if(self_id->p0.port1 >= 0x2){ 1206103285Sikob c_port++; 1207103285Sikob } 1208103285Sikob if(self_id->p0.port2 >= 0x2){ 1209103285Sikob c_port++; 1210103285Sikob } 1211103285Sikob } 1212103285Sikob if(c_port > 2){ 1213103285Sikob i_branch += (c_port - 2); 1214103285Sikob } 1215103285Sikob sid += 2; 1216103285Sikob self_id++; 1217103285Sikob fc->topology_map->self_id_count ++; 1218103285Sikob } 1219108655Ssimokawa device_printf(fc->bdev, "%d nodes", fc->max_node + 1); 1220103285Sikob /* CRC */ 1221103285Sikob fc->topology_map->crc = fw_crc16( 1222129585Sdfr (uint32_t *)&fc->topology_map->generation, 1223103285Sikob fc->topology_map->crc_len * 4); 1224103285Sikob fc->speed_map->crc = fw_crc16( 1225129585Sdfr (uint32_t *)&fc->speed_map->generation, 1226103285Sikob fc->speed_map->crc_len * 4); 1227103285Sikob /* byteswap and copy to CSR */ 1228129585Sdfr p = (uint32_t *)fc->topology_map; 1229103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1230103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1231129585Sdfr p = (uint32_t *)fc->speed_map; 1232103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1233103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1234129585Sdfr /* don't byte-swap uint8_t array */ 1235103285Sikob bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1236103285Sikob 1237103285Sikob fc->max_hop = fc->max_node - i_branch; 1238103285Sikob printf(", maxhop <= %d", fc->max_hop); 1239103285Sikob 1240103285Sikob if(fc->irm == -1 ){ 1241103285Sikob printf(", Not found IRM capable node"); 1242103285Sikob }else{ 1243103285Sikob printf(", cable IRM = %d", fc->irm); 1244103285Sikob if (fc->irm == fc->nodeid) 1245110016Ssimokawa printf(" (me)"); 1246103285Sikob } 1247110016Ssimokawa printf("\n"); 1248103285Sikob 1249109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1250114909Ssimokawa if (fc->irm == fc->nodeid) { 1251103285Sikob fc->status = FWBUSMGRDONE; 1252103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1253114909Ssimokawa fw_bmr(fc); 1254109736Ssimokawa } else { 1255103285Sikob fc->status = FWBUSMGRELECT; 1256110193Ssimokawa callout_reset(&fc->bmr_callout, hz/8, 1257110193Ssimokawa (void *)fw_try_bmr, (void *)fc); 1258103285Sikob } 1259114909Ssimokawa } else 1260103285Sikob fc->status = FWBUSMGRDONE; 1261114909Ssimokawa 1262108853Ssimokawa callout_reset(&fc->busprobe_callout, hz/4, 1263108853Ssimokawa (void *)fw_bus_probe, (void *)fc); 1264103285Sikob} 1265106790Ssimokawa 1266103285Sikob/* 1267103285Sikob * To probe devices on the IEEE1394 bus. 1268103285Sikob */ 1269106790Ssimokawastatic void 1270106790Ssimokawafw_bus_probe(struct firewire_comm *fc) 1271103285Sikob{ 1272103285Sikob int s; 1273120850Ssimokawa struct fw_device *fwdev; 1274103285Sikob 1275103285Sikob s = splfw(); 1276103285Sikob fc->status = FWBUSEXPLORE; 1277103285Sikob 1278120850Ssimokawa /* Invalidate all devices, just after bus reset. */ 1279120850Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1280110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1281103285Sikob fwdev->status = FWDEVINVAL; 1282103285Sikob fwdev->rcnt = 0; 1283103285Sikob } 1284169806Ssimokawa splx(s); 1285120850Ssimokawa 1286169806Ssimokawa wakeup((void *)fc); 1287103285Sikob} 1288106790Ssimokawa 1289169806Ssimokawastatic int 1290169806Ssimokawafw_explore_read_quads(struct fw_device *fwdev, int offset, 1291169806Ssimokawa uint32_t *quad, int n) 1292129274Sdfr{ 1293169806Ssimokawa struct fw_xfer *xfer; 1294169806Ssimokawa uint32_t tmp; 1295169806Ssimokawa int i, error; 1296129274Sdfr 1297169806Ssimokawa for (i = 0; i < n; i ++, offset += sizeof(uint32_t)) { 1298169806Ssimokawa xfer = fwmem_read_quad(fwdev, NULL, -1, 1299169806Ssimokawa 0xffff, 0xf0000000 | offset, (void *)&tmp, 1300169806Ssimokawa fw_asy_callback); 1301169806Ssimokawa if (xfer == NULL) 1302169806Ssimokawa return (-1); 1303169806Ssimokawa tsleep((void *)xfer, PWAIT|PCATCH, "rquad", 0); 1304169806Ssimokawa 1305169806Ssimokawa if (xfer->resp == 0) 1306169806Ssimokawa quad[i] = ntohl(tmp); 1307169806Ssimokawa 1308169806Ssimokawa error = xfer->resp; 1309169806Ssimokawa fw_xfer_free(xfer); 1310169806Ssimokawa if (error) 1311169806Ssimokawa return (error); 1312169806Ssimokawa } 1313169806Ssimokawa return (0); 1314169806Ssimokawa} 1315169806Ssimokawa 1316169806Ssimokawa 1317169806Ssimokawastatic int 1318169806Ssimokawafw_explore_csrblock(struct fw_device *fwdev, int offset, int recur) 1319169806Ssimokawa{ 1320169806Ssimokawa int err, i, off; 1321169806Ssimokawa struct csrdirectory *dir; 1322169806Ssimokawa struct csrreg *reg; 1323169806Ssimokawa 1324169806Ssimokawa dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)]; 1325169806Ssimokawa err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, 1326169806Ssimokawa (uint32_t *)dir, 1); 1327169806Ssimokawa if (err) 1328169806Ssimokawa return (-1); 1329169806Ssimokawa 1330169806Ssimokawa offset += sizeof(uint32_t); 1331169806Ssimokawa reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)]; 1332169806Ssimokawa err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, 1333169806Ssimokawa (uint32_t *)reg, dir->crc_len); 1334169806Ssimokawa if (err) 1335169806Ssimokawa return (-1); 1336169806Ssimokawa 1337169806Ssimokawa /* XXX check CRC */ 1338169806Ssimokawa 1339169806Ssimokawa off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1); 1340169806Ssimokawa if (fwdev->rommax < off) 1341169806Ssimokawa fwdev->rommax = off; 1342169806Ssimokawa 1343169806Ssimokawa if (recur == 0) 1344169806Ssimokawa return (0); 1345169806Ssimokawa 1346169806Ssimokawa for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) { 1347169806Ssimokawa if (reg[i].key == CROM_UDIR) 1348169806Ssimokawa recur = 1; 1349169806Ssimokawa else if (reg[i].key == CROM_TEXTLEAF) 1350169806Ssimokawa recur = 0; 1351169806Ssimokawa else 1352129274Sdfr continue; 1353169806Ssimokawa 1354169806Ssimokawa off = offset + reg[i].val * sizeof(uint32_t); 1355169806Ssimokawa if (off > CROMSIZE) { 1356169806Ssimokawa printf("%s: invalid offset %d\n", __FUNCTION__, off); 1357169806Ssimokawa return(-1); 1358169806Ssimokawa } 1359169806Ssimokawa err = fw_explore_csrblock(fwdev, off, recur); 1360169806Ssimokawa if (err) 1361169806Ssimokawa return (-1); 1362129274Sdfr } 1363169806Ssimokawa return (0); 1364129274Sdfr} 1365129274Sdfr 1366169806Ssimokawastatic int 1367169806Ssimokawafw_explore_node(struct fw_device *dfwdev) 1368103285Sikob{ 1369169806Ssimokawa struct firewire_comm *fc; 1370110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1371169806Ssimokawa uint32_t *csr; 1372169806Ssimokawa struct csrhdr *hdr; 1373169806Ssimokawa struct bus_info *binfo; 1374169806Ssimokawa int err, node, spd; 1375103285Sikob 1376169806Ssimokawa fc = dfwdev->fc; 1377169806Ssimokawa csr = dfwdev->csrrom; 1378169806Ssimokawa node = dfwdev->dst; 1379103285Sikob 1380169806Ssimokawa /* First quad */ 1381169806Ssimokawa err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1); 1382169806Ssimokawa if (err) 1383169806Ssimokawa return (-1); 1384169806Ssimokawa hdr = (struct csrhdr *)&csr[0]; 1385169806Ssimokawa if (hdr->info_len != 4) { 1386169806Ssimokawa if (firewire_debug) 1387169806Ssimokawa printf("node%d: wrong bus info len(%d)\n", 1388169806Ssimokawa node, hdr->info_len); 1389169806Ssimokawa return (-1); 1390169806Ssimokawa } 1391103285Sikob 1392169806Ssimokawa /* bus info */ 1393169806Ssimokawa err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4); 1394169806Ssimokawa if (err) 1395169806Ssimokawa return (-1); 1396169806Ssimokawa binfo = (struct bus_info *)&csr[1]; 1397169806Ssimokawa if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) { 1398110577Ssimokawa if (firewire_debug) 1399169806Ssimokawa printf("node%d: invalid bus name 0x%08x\n", 1400169806Ssimokawa node, binfo->bus_name); 1401169806Ssimokawa return (-1); 1402103285Sikob } 1403169806Ssimokawa spd = fc->speed_map->speed[fc->nodeid][node]; 1404169806Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1405169806Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64)) 1406169806Ssimokawa break; 1407169806Ssimokawa if (fwdev == NULL) { 1408169806Ssimokawa /* new device */ 1409169806Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, 1410169806Ssimokawa M_NOWAIT | M_ZERO); 1411169806Ssimokawa if (fwdev == NULL) { 1412169806Ssimokawa if (firewire_debug) 1413169806Ssimokawa printf("node%d: no memory\n", node); 1414169806Ssimokawa return (-1); 1415103285Sikob } 1416106810Ssimokawa fwdev->fc = fc; 1417169806Ssimokawa fwdev->eui = binfo->eui64; 1418169806Ssimokawa /* inesrt into sorted fwdev list */ 1419110179Ssimokawa pfwdev = NULL; 1420110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1421110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1422169806Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1423169806Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1424110179Ssimokawa break; 1425110179Ssimokawa pfwdev = tfwdev; 1426103285Sikob } 1427110179Ssimokawa if (pfwdev == NULL) 1428110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1429110179Ssimokawa else 1430110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1431103285Sikob 1432108655Ssimokawa device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1433169806Ssimokawa linkspeed[spd], 1434169806Ssimokawa fwdev->eui.hi, fwdev->eui.lo); 1435103285Sikob } 1436169806Ssimokawa fwdev->dst = node; 1437169806Ssimokawa fwdev->status = FWDEVINIT; 1438169806Ssimokawa fwdev->speed = spd; 1439103285Sikob 1440169806Ssimokawa /* unchanged ? */ 1441169806Ssimokawa if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) { 1442169806Ssimokawa if (firewire_debug) 1443169806Ssimokawa printf("node%d: crom unchanged\n", node); 1444169806Ssimokawa return (0); 1445103285Sikob } 1446103285Sikob 1447169806Ssimokawa bzero(&fwdev->csrrom[0], CROMSIZE); 1448106790Ssimokawa 1449169806Ssimokawa /* copy first quad and bus info block */ 1450169806Ssimokawa bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5); 1451169806Ssimokawa fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4; 1452103285Sikob 1453169806Ssimokawa err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */ 1454113584Ssimokawa 1455169806Ssimokawa if (err) { 1456169806Ssimokawa fwdev->status = FWDEVINVAL; 1457169806Ssimokawa fwdev->csrrom[0] = 0; 1458103285Sikob } 1459169806Ssimokawa return (err); 1460103285Sikob 1461103285Sikob} 1462106790Ssimokawa 1463103285Sikob/* 1464169806Ssimokawa * Find the self_id packet for a node, ignoring sequels. 1465103285Sikob */ 1466169806Ssimokawastatic union fw_self_id * 1467169806Ssimokawafw_find_self_id(struct firewire_comm *fc, int node) 1468106790Ssimokawa{ 1469169806Ssimokawa uint32_t i; 1470169806Ssimokawa union fw_self_id *s; 1471103285Sikob 1472169806Ssimokawa for (i = 0; i < fc->topology_map->self_id_count; i++) { 1473169806Ssimokawa s = &fc->topology_map->self_id[i]; 1474169806Ssimokawa if (s->p0.sequel) 1475169806Ssimokawa continue; 1476169806Ssimokawa if (s->p0.phy_id == node) 1477169806Ssimokawa return s; 1478110577Ssimokawa } 1479169806Ssimokawa return 0; 1480169806Ssimokawa} 1481110577Ssimokawa 1482169806Ssimokawastatic void 1483169806Ssimokawafw_explore(struct firewire_comm *fc) 1484169806Ssimokawa{ 1485169806Ssimokawa int node, err, s, i, todo, todo2, trys; 1486169806Ssimokawa char nodes[63]; 1487169806Ssimokawa struct fw_device dfwdev; 1488169829Ssimokawa union fw_self_id *fwsid; 1489110577Ssimokawa 1490169806Ssimokawa todo = 0; 1491169806Ssimokawa /* setup dummy fwdev */ 1492169806Ssimokawa dfwdev.fc = fc; 1493169806Ssimokawa dfwdev.speed = 0; 1494169806Ssimokawa dfwdev.maxrec = 8; /* 512 */ 1495169806Ssimokawa dfwdev.status = FWDEVINIT; 1496103285Sikob 1497169806Ssimokawa for (node = 0; node <= fc->max_node; node ++) { 1498169806Ssimokawa /* We don't probe myself and linkdown nodes */ 1499169806Ssimokawa if (node == fc->nodeid) 1500169806Ssimokawa continue; 1501169829Ssimokawa fwsid = fw_find_self_id(fc, node); 1502169829Ssimokawa if (!fwsid || !fwsid->p0.link_active) { 1503169806Ssimokawa if (firewire_debug) 1504169806Ssimokawa printf("node%d: link down\n", node); 1505169806Ssimokawa continue; 1506103285Sikob } 1507169806Ssimokawa nodes[todo++] = node; 1508103285Sikob } 1509103285Sikob 1510169806Ssimokawa s = splfw(); 1511169806Ssimokawa for (trys = 0; todo > 0 && trys < 3; trys ++) { 1512169806Ssimokawa todo2 = 0; 1513169806Ssimokawa for (i = 0; i < todo; i ++) { 1514169806Ssimokawa dfwdev.dst = nodes[i]; 1515169806Ssimokawa err = fw_explore_node(&dfwdev); 1516169806Ssimokawa if (err) 1517169806Ssimokawa nodes[todo2++] = nodes[i]; 1518169806Ssimokawa if (firewire_debug) 1519169806Ssimokawa printf("%s: node %d, err = %d\n", 1520169806Ssimokawa __FUNCTION__, node, err); 1521103285Sikob } 1522169806Ssimokawa todo = todo2; 1523103285Sikob } 1524169806Ssimokawa splx(s); 1525169806Ssimokawa} 1526169806Ssimokawa 1527169806Ssimokawa 1528169806Ssimokawastatic void 1529169806Ssimokawafw_bus_probe_thread(void *arg) 1530169806Ssimokawa{ 1531169806Ssimokawa struct firewire_comm *fc; 1532169806Ssimokawa 1533169806Ssimokawa fc = (struct firewire_comm *)arg; 1534169806Ssimokawa 1535169806Ssimokawa mtx_lock(&Giant); 1536169806Ssimokawa while (1) { 1537169806Ssimokawa if (fc->status == FWBUSEXPLORE) { 1538169806Ssimokawa fw_explore(fc); 1539169806Ssimokawa fc->status = FWBUSEXPDONE; 1540169806Ssimokawa if (firewire_debug) 1541169806Ssimokawa printf("bus_explore done\n"); 1542169806Ssimokawa fw_attach_dev(fc); 1543169806Ssimokawa } else if (fc->status == FWBUSDETACH) 1544169806Ssimokawa break; 1545169806Ssimokawa tsleep((void *)fc, PWAIT|PCATCH, "-", 0); 1546127468Ssimokawa } 1547169806Ssimokawa mtx_unlock(&Giant); 1548169806Ssimokawa wakeup(fc); 1549169806Ssimokawa kthread_exit(0); 1550103285Sikob} 1551103285Sikob 1552103285Sikob/* 1553103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1554103285Sikob */ 1555106815Ssimokawastatic void 1556106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1557103285Sikob{ 1558120850Ssimokawa struct fw_device *fwdev, *next; 1559103285Sikob int i, err; 1560103285Sikob device_t *devlistp; 1561103285Sikob int devcnt; 1562103285Sikob struct firewire_dev_comm *fdc; 1563103285Sikob 1564120850Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1565120850Ssimokawa next = STAILQ_NEXT(fwdev, link); 1566120850Ssimokawa if (fwdev->status == FWDEVINIT) { 1567114069Ssimokawa fwdev->status = FWDEVATTACHED; 1568120850Ssimokawa } else if (fwdev->status == FWDEVINVAL) { 1569120850Ssimokawa fwdev->rcnt ++; 1570120850Ssimokawa if (fwdev->rcnt > hold_count) { 1571120850Ssimokawa /* 1572120850Ssimokawa * Remove devices which have not been seen 1573120850Ssimokawa * for a while. 1574120850Ssimokawa */ 1575120850Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, 1576120850Ssimokawa link); 1577120850Ssimokawa free(fwdev, M_FW); 1578120850Ssimokawa } 1579120850Ssimokawa } 1580120850Ssimokawa } 1581103285Sikob 1582108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1583103285Sikob if( err != 0 ) 1584103285Sikob return; 1585103285Sikob for( i = 0 ; i < devcnt ; i++){ 1586103285Sikob if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1587103285Sikob fdc = device_get_softc(devlistp[i]); 1588103285Sikob if (fdc->post_explore != NULL) 1589103285Sikob fdc->post_explore(fdc); 1590103285Sikob } 1591103285Sikob } 1592103285Sikob free(devlistp, M_TEMP); 1593103285Sikob 1594103285Sikob return; 1595103285Sikob} 1596106815Ssimokawa 1597103285Sikob/* 1598129541Sdfr * To allocate unique transaction label. 1599103285Sikob */ 1600106815Ssimokawastatic int 1601106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1602103285Sikob{ 1603103285Sikob u_int i; 1604169119Ssimokawa struct fw_xfer *txfer; 1605103285Sikob int s; 1606129585Sdfr static uint32_t label = 0; 1607103285Sikob 1608103285Sikob s = splfw(); 1609103285Sikob for( i = 0 ; i < 0x40 ; i ++){ 1610103285Sikob label = (label + 1) & 0x3f; 1611169119Ssimokawa STAILQ_FOREACH(txfer, &fc->tlabels[label], tlabel) 1612169119Ssimokawa if (txfer->send.hdr.mode.hdr.dst == 1613120660Ssimokawa xfer->send.hdr.mode.hdr.dst) 1614120660Ssimokawa break; 1615169119Ssimokawa if(txfer == NULL) { 1616169119Ssimokawa STAILQ_INSERT_TAIL(&fc->tlabels[label], xfer, tlabel); 1617103285Sikob splx(s); 1618110577Ssimokawa if (firewire_debug > 1) 1619110577Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", 1620120660Ssimokawa xfer->send.hdr.mode.hdr.dst, label); 1621103285Sikob return(label); 1622103285Sikob } 1623103285Sikob } 1624103285Sikob splx(s); 1625103285Sikob 1626130460Sdfr if (firewire_debug > 1) 1627130460Sdfr printf("fw_get_tlabel: no free tlabel\n"); 1628103285Sikob return(-1); 1629103285Sikob} 1630106815Ssimokawa 1631113584Ssimokawastatic void 1632120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb) 1633113584Ssimokawa{ 1634120660Ssimokawa struct fw_pkt *pkt; 1635120660Ssimokawa u_char *p; 1636120660Ssimokawa struct tcode_info *tinfo; 1637120660Ssimokawa u_int res, i, len, plen; 1638113584Ssimokawa 1639169127Ssimokawa rb->xfer->recv.spd = rb->spd; 1640120660Ssimokawa 1641120660Ssimokawa pkt = (struct fw_pkt *)rb->vec->iov_base; 1642120660Ssimokawa tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 1643120660Ssimokawa 1644120660Ssimokawa /* Copy header */ 1645120660Ssimokawa p = (u_char *)&rb->xfer->recv.hdr; 1646120660Ssimokawa bcopy(rb->vec->iov_base, p, tinfo->hdr_len); 1647132771Skan rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len; 1648120660Ssimokawa rb->vec->iov_len -= tinfo->hdr_len; 1649120660Ssimokawa 1650120660Ssimokawa /* Copy payload */ 1651120660Ssimokawa p = (u_char *)rb->xfer->recv.payload; 1652120660Ssimokawa res = rb->xfer->recv.pay_len; 1653120660Ssimokawa 1654120660Ssimokawa /* special handling for RRESQ */ 1655120660Ssimokawa if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 1656129585Sdfr p != NULL && res >= sizeof(uint32_t)) { 1657129585Sdfr *(uint32_t *)p = pkt->mode.rresq.data; 1658129585Sdfr rb->xfer->recv.pay_len = sizeof(uint32_t); 1659120660Ssimokawa return; 1660120660Ssimokawa } 1661120660Ssimokawa 1662120660Ssimokawa if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 1663120660Ssimokawa return; 1664120660Ssimokawa 1665120660Ssimokawa plen = pkt->mode.rresb.len; 1666120660Ssimokawa 1667120660Ssimokawa for (i = 0; i < rb->nvec; i++, rb->vec++) { 1668120660Ssimokawa len = MIN(rb->vec->iov_len, plen); 1669113584Ssimokawa if (res < len) { 1670113584Ssimokawa printf("rcv buffer(%d) is %d bytes short.\n", 1671120660Ssimokawa rb->xfer->recv.pay_len, len - res); 1672113584Ssimokawa len = res; 1673113584Ssimokawa } 1674120660Ssimokawa bcopy(rb->vec->iov_base, p, len); 1675113584Ssimokawa p += len; 1676113584Ssimokawa res -= len; 1677120660Ssimokawa plen -= len; 1678120660Ssimokawa if (res == 0 || plen == 0) 1679113584Ssimokawa break; 1680113584Ssimokawa } 1681120660Ssimokawa rb->xfer->recv.pay_len -= res; 1682120660Ssimokawa 1683113584Ssimokawa} 1684113584Ssimokawa 1685103285Sikob/* 1686129541Sdfr * Generic packet receiving process. 1687103285Sikob */ 1688106815Ssimokawavoid 1689120660Ssimokawafw_rcv(struct fw_rcv_buf *rb) 1690103285Sikob{ 1691103285Sikob struct fw_pkt *fp, *resfp; 1692103285Sikob struct fw_bind *bind; 1693169130Ssimokawa int tcode; 1694113584Ssimokawa int i, len, oldstate; 1695103285Sikob#if 0 1696103285Sikob { 1697129585Sdfr uint32_t *qld; 1698103285Sikob int i; 1699129585Sdfr qld = (uint32_t *)buf; 1700103285Sikob printf("spd %d len:%d\n", spd, len); 1701103285Sikob for( i = 0 ; i <= len && i < 32; i+= 4){ 1702103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1703103285Sikob if((i % 16) == 15) printf("\n"); 1704103285Sikob } 1705103285Sikob if((i % 16) != 15) printf("\n"); 1706103285Sikob } 1707103285Sikob#endif 1708120660Ssimokawa fp = (struct fw_pkt *)rb->vec[0].iov_base; 1709113584Ssimokawa tcode = fp->mode.common.tcode; 1710113584Ssimokawa switch (tcode) { 1711103285Sikob case FWTCODE_WRES: 1712103285Sikob case FWTCODE_RRESQ: 1713103285Sikob case FWTCODE_RRESB: 1714103285Sikob case FWTCODE_LRES: 1715120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1716103285Sikob fp->mode.hdr.tlrt >> 2); 1717120660Ssimokawa if(rb->xfer == NULL) { 1718103285Sikob printf("fw_rcv: unknown response " 1719124251Ssimokawa "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n", 1720124251Ssimokawa tcode_str[tcode], tcode, 1721124251Ssimokawa fp->mode.hdr.src, 1722124251Ssimokawa fp->mode.hdr.tlrt >> 2, 1723124251Ssimokawa fp->mode.hdr.tlrt & 3, 1724124251Ssimokawa fp->mode.rresq.data); 1725103285Sikob#if 1 1726103285Sikob printf("try ad-hoc work around!!\n"); 1727120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1728103285Sikob (fp->mode.hdr.tlrt >> 2)^3); 1729120660Ssimokawa if (rb->xfer == NULL) { 1730103285Sikob printf("no use...\n"); 1731169131Ssimokawa return; 1732103285Sikob } 1733103285Sikob#else 1734169131Ssimokawa return; 1735103285Sikob#endif 1736103285Sikob } 1737120660Ssimokawa fw_rcv_copy(rb); 1738120660Ssimokawa if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 1739120660Ssimokawa rb->xfer->resp = EIO; 1740120660Ssimokawa else 1741120660Ssimokawa rb->xfer->resp = 0; 1742113584Ssimokawa /* make sure the packet is drained in AT queue */ 1743120660Ssimokawa oldstate = rb->xfer->state; 1744120660Ssimokawa rb->xfer->state = FWXF_RCVD; 1745113584Ssimokawa switch (oldstate) { 1746113584Ssimokawa case FWXF_SENT: 1747120660Ssimokawa fw_xfer_done(rb->xfer); 1748103285Sikob break; 1749113584Ssimokawa case FWXF_START: 1750119289Ssimokawa#if 0 1751113584Ssimokawa if (firewire_debug) 1752120660Ssimokawa printf("not sent yet tl=%x\n", rb->xfer->tl); 1753119289Ssimokawa#endif 1754113584Ssimokawa break; 1755103285Sikob default: 1756120660Ssimokawa printf("unexpected state %d\n", rb->xfer->state); 1757103285Sikob } 1758113584Ssimokawa return; 1759103285Sikob case FWTCODE_WREQQ: 1760103285Sikob case FWTCODE_WREQB: 1761103285Sikob case FWTCODE_RREQQ: 1762103285Sikob case FWTCODE_RREQB: 1763103285Sikob case FWTCODE_LREQ: 1764120660Ssimokawa bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 1765113584Ssimokawa fp->mode.rreqq.dest_lo); 1766103285Sikob if(bind == NULL){ 1767124251Ssimokawa printf("Unknown service addr 0x%04x:0x%08x %s(%x)" 1768127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1769127468Ssimokawa " src=0x%x data=%lx\n", 1770127468Ssimokawa#else 1771124251Ssimokawa " src=0x%x data=%x\n", 1772113962Ssimokawa#endif 1773124251Ssimokawa fp->mode.wreqq.dest_hi, fp->mode.wreqq.dest_lo, 1774124251Ssimokawa tcode_str[tcode], tcode, 1775124251Ssimokawa fp->mode.hdr.src, ntohl(fp->mode.wreqq.data)); 1776120660Ssimokawa if (rb->fc->status == FWBUSRESET) { 1777110798Ssimokawa printf("fw_rcv: cannot respond(bus reset)!\n"); 1778169131Ssimokawa return; 1779103285Sikob } 1780120660Ssimokawa rb->xfer = fw_xfer_alloc(M_FWXFER); 1781120660Ssimokawa if(rb->xfer == NULL){ 1782103285Sikob return; 1783103285Sikob } 1784120660Ssimokawa rb->xfer->send.spd = rb->spd; 1785120660Ssimokawa rb->xfer->send.pay_len = 0; 1786120660Ssimokawa resfp = &rb->xfer->send.hdr; 1787113584Ssimokawa switch (tcode) { 1788103285Sikob case FWTCODE_WREQQ: 1789103285Sikob case FWTCODE_WREQB: 1790103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 1791103285Sikob break; 1792103285Sikob case FWTCODE_RREQQ: 1793103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 1794103285Sikob break; 1795103285Sikob case FWTCODE_RREQB: 1796103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 1797103285Sikob break; 1798103285Sikob case FWTCODE_LREQ: 1799103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 1800103285Sikob break; 1801103285Sikob } 1802103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 1803103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 1804103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 1805120660Ssimokawa resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 1806103285Sikob resfp->mode.rresb.extcode = 0; 1807103285Sikob resfp->mode.rresb.len = 0; 1808103285Sikob/* 1809167632Ssimokawa rb->xfer->hand = fw_asy_callback; 1810103285Sikob*/ 1811167632Ssimokawa rb->xfer->hand = fw_xfer_free; 1812120660Ssimokawa if(fw_asyreq(rb->fc, -1, rb->xfer)){ 1813120660Ssimokawa fw_xfer_free(rb->xfer); 1814103285Sikob return; 1815103285Sikob } 1816169131Ssimokawa return; 1817103285Sikob } 1818113584Ssimokawa len = 0; 1819120660Ssimokawa for (i = 0; i < rb->nvec; i ++) 1820120660Ssimokawa len += rb->vec[i].iov_len; 1821169130Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 1822169130Ssimokawa if (rb->xfer == NULL) { 1823169131Ssimokawa#if 1 1824169130Ssimokawa printf("Discard a packet for this bind.\n"); 1825169131Ssimokawa#endif 1826169131Ssimokawa return; 1827103285Sikob } 1828169130Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 1829169130Ssimokawa fw_rcv_copy(rb); 1830169130Ssimokawa rb->xfer->hand(rb->xfer); 1831169130Ssimokawa return; 1832120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */ 1833103285Sikob case FWTCODE_STREAM: 1834103285Sikob { 1835103285Sikob struct fw_xferq *xferq; 1836103285Sikob 1837120660Ssimokawa xferq = rb->fc->ir[sub]; 1838103285Sikob#if 0 1839103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 1840103285Sikob sub, len, off, spd); 1841103285Sikob#endif 1842103285Sikob if(xferq->queued >= xferq->maxq) { 1843103285Sikob printf("receive queue is full\n"); 1844169131Ssimokawa return; 1845103285Sikob } 1846113584Ssimokawa /* XXX get xfer from xfer queue, we don't need copy for 1847113584Ssimokawa per packet mode */ 1848120660Ssimokawa rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 1849113584Ssimokawa vec[0].iov_len); 1850169131Ssimokawa if (rb->xfer == NULL) 1851169131Ssimokawa return; 1852120660Ssimokawa fw_rcv_copy(rb) 1853103285Sikob s = splfw(); 1854103285Sikob xferq->queued++; 1855120660Ssimokawa STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link); 1856103285Sikob splx(s); 1857120660Ssimokawa sc = device_get_softc(rb->fc->bdev); 1858127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1859127468Ssimokawa if (&xferq->rsel.si_pid != 0) 1860127468Ssimokawa#else 1861103285Sikob if (SEL_WAITING(&xferq->rsel)) 1862103285Sikob#endif 1863122352Stanimura selwakeuppri(&xferq->rsel, FWPRI); 1864103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 1865103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 1866103285Sikob wakeup((caddr_t)xferq); 1867103285Sikob } 1868103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 1869103285Sikob xferq->hand(xferq); 1870103285Sikob } 1871103285Sikob return; 1872103285Sikob break; 1873103285Sikob } 1874120660Ssimokawa#endif 1875103285Sikob default: 1876113584Ssimokawa printf("fw_rcv: unknow tcode %d\n", tcode); 1877103285Sikob break; 1878103285Sikob } 1879103285Sikob} 1880106815Ssimokawa 1881103285Sikob/* 1882103285Sikob * Post process for Bus Manager election process. 1883103285Sikob */ 1884103285Sikobstatic void 1885103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 1886103285Sikob{ 1887103285Sikob struct firewire_comm *fc; 1888109422Ssimokawa int bmr; 1889103285Sikob 1890109422Ssimokawa if (xfer == NULL) 1891109422Ssimokawa return; 1892103285Sikob fc = xfer->fc; 1893109422Ssimokawa if (xfer->resp != 0) 1894103285Sikob goto error; 1895120660Ssimokawa if (xfer->recv.payload == NULL) 1896103285Sikob goto error; 1897120660Ssimokawa if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 1898103285Sikob goto error; 1899109422Ssimokawa 1900120660Ssimokawa bmr = ntohl(xfer->recv.payload[0]); 1901109422Ssimokawa if (bmr == 0x3f) 1902109422Ssimokawa bmr = fc->nodeid; 1903109422Ssimokawa 1904109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 1905120660Ssimokawa fw_xfer_free_buf(xfer); 1906114909Ssimokawa fw_bmr(fc); 1907114909Ssimokawa return; 1908114909Ssimokawa 1909103285Sikoberror: 1910114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 1911120660Ssimokawa fw_xfer_free_buf(xfer); 1912103285Sikob} 1913106815Ssimokawa 1914113584Ssimokawa 1915103285Sikob/* 1916103285Sikob * To candidate Bus Manager election process. 1917103285Sikob */ 1918110270Ssimokawastatic void 1919106815Ssimokawafw_try_bmr(void *arg) 1920103285Sikob{ 1921103285Sikob struct fw_xfer *xfer; 1922103285Sikob struct firewire_comm *fc = (struct firewire_comm *)arg; 1923103285Sikob struct fw_pkt *fp; 1924103285Sikob int err = 0; 1925103285Sikob 1926120660Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4); 1927103285Sikob if(xfer == NULL){ 1928103285Sikob return; 1929103285Sikob } 1930120660Ssimokawa xfer->send.spd = 0; 1931103285Sikob fc->status = FWBUSMGRELECT; 1932103285Sikob 1933120660Ssimokawa fp = &xfer->send.hdr; 1934113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 1935103285Sikob fp->mode.lreq.tlrt = 0; 1936103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 1937103285Sikob fp->mode.lreq.pri = 0; 1938103285Sikob fp->mode.lreq.src = 0; 1939113584Ssimokawa fp->mode.lreq.len = 8; 1940120660Ssimokawa fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 1941120660Ssimokawa fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 1942113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 1943120660Ssimokawa xfer->send.payload[0] = htonl(0x3f); 1944120660Ssimokawa xfer->send.payload[1] = htonl(fc->nodeid); 1945167632Ssimokawa xfer->hand = fw_try_bmr_callback; 1946103285Sikob 1947103285Sikob err = fw_asyreq(fc, -1, xfer); 1948103285Sikob if(err){ 1949120660Ssimokawa fw_xfer_free_buf(xfer); 1950103285Sikob return; 1951103285Sikob } 1952103285Sikob return; 1953103285Sikob} 1954106543Ssimokawa 1955106543Ssimokawa#ifdef FW_VMACCESS 1956103285Sikob/* 1957103285Sikob * Software implementation for physical memory block access. 1958103285Sikob * XXX:Too slow, usef for debug purpose only. 1959103285Sikob */ 1960106815Ssimokawastatic void 1961106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){ 1962103285Sikob struct fw_pkt *rfp, *sfp = NULL; 1963129585Sdfr uint32_t *ld = (uint32_t *)xfer->recv.buf; 1964103285Sikob 1965113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 1966113584Ssimokawa xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1967103285Sikob printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1968103285Sikob if(xfer->resp != 0){ 1969103285Sikob fw_xfer_free( xfer); 1970103285Sikob return; 1971103285Sikob } 1972103285Sikob if(xfer->recv.buf == NULL){ 1973103285Sikob fw_xfer_free( xfer); 1974103285Sikob return; 1975103285Sikob } 1976103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1977103285Sikob switch(rfp->mode.hdr.tcode){ 1978103285Sikob /* XXX need fix for 64bit arch */ 1979103285Sikob case FWTCODE_WREQB: 1980110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 1981103285Sikob xfer->send.len = 12; 1982103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1983103285Sikob bcopy(rfp->mode.wreqb.payload, 1984103285Sikob (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 1985103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1986103285Sikob sfp->mode.wres.rtcode = 0; 1987103285Sikob break; 1988103285Sikob case FWTCODE_WREQQ: 1989110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 1990103285Sikob xfer->send.len = 12; 1991103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1992129585Sdfr *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 1993103285Sikob sfp->mode.wres.rtcode = 0; 1994103285Sikob break; 1995103285Sikob case FWTCODE_RREQB: 1996110195Ssimokawa xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 1997103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 1998103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1999103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2000129585Sdfr sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len)); 2001103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 2002103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 2003103285Sikob sfp->mode.rresb.rtcode = 0; 2004103285Sikob sfp->mode.rresb.extcode = 0; 2005103285Sikob break; 2006103285Sikob case FWTCODE_RREQQ: 2007110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2008103285Sikob xfer->send.len = 16; 2009103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2010129585Sdfr sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2011103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 2012103285Sikob sfp->mode.rresb.rtcode = 0; 2013103285Sikob break; 2014103285Sikob default: 2015103285Sikob fw_xfer_free( xfer); 2016103285Sikob return; 2017103285Sikob } 2018103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2019103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2020167632Ssimokawa xfer->hand = fw_xfer_free; 2021103285Sikob 2022103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2023103285Sikob sfp->mode.hdr.pri = 0; 2024103285Sikob 2025103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2026103285Sikob/**/ 2027103285Sikob return; 2028103285Sikob} 2029106543Ssimokawa#endif 2030106543Ssimokawa 2031103285Sikob/* 2032103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2033103285Sikob */ 2034129585Sdfruint16_t 2035129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){ 2036129585Sdfr uint32_t i, sum, crc = 0; 2037103285Sikob int shift; 2038103285Sikob len = (len + 3) & ~3; 2039103285Sikob for(i = 0 ; i < len ; i+= 4){ 2040103285Sikob for( shift = 28 ; shift >= 0 ; shift -= 4){ 2041103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2042103285Sikob crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2043103285Sikob } 2044103285Sikob crc &= 0xffff; 2045103285Sikob } 2046129585Sdfr return((uint16_t) crc); 2047103285Sikob} 2048106815Ssimokawa 2049110270Ssimokawastatic int 2050110072Ssimokawafw_bmr(struct firewire_comm *fc) 2051110072Ssimokawa{ 2052110072Ssimokawa struct fw_device fwdev; 2053113584Ssimokawa union fw_self_id *self_id; 2054110072Ssimokawa int cmstr; 2055129585Sdfr uint32_t quad; 2056110072Ssimokawa 2057113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2058129274Sdfr self_id = fw_find_self_id(fc, fc->max_node); 2059113584Ssimokawa if (fc->max_node > 0) { 2060114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2061114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2062113584Ssimokawa cmstr = fc->max_node; 2063114909Ssimokawa else { 2064114909Ssimokawa device_printf(fc->bdev, 2065114909Ssimokawa "root node is not cycle master capable\n"); 2066114909Ssimokawa /* XXX shall we be the cycle master? */ 2067113584Ssimokawa cmstr = fc->nodeid; 2068114909Ssimokawa /* XXX need bus reset */ 2069114909Ssimokawa } 2070113584Ssimokawa } else 2071113584Ssimokawa cmstr = -1; 2072114909Ssimokawa 2073114909Ssimokawa device_printf(fc->bdev, "bus manager %d ", CSRARC(fc, BUS_MGR_ID)); 2074114909Ssimokawa if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2075114909Ssimokawa /* We are not the bus manager */ 2076114909Ssimokawa printf("\n"); 2077114909Ssimokawa return(0); 2078114909Ssimokawa } 2079114909Ssimokawa printf("(me)\n"); 2080114909Ssimokawa 2081114909Ssimokawa /* Optimize gapcount */ 2082113584Ssimokawa if(fc->max_hop <= MAX_GAPHOP ) 2083113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2084110072Ssimokawa /* If we are the cycle master, nothing to do */ 2085113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2086110072Ssimokawa return 0; 2087110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2088110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2089110072Ssimokawa fwdev.fc = fc; 2090110072Ssimokawa fwdev.dst = cmstr; 2091110072Ssimokawa fwdev.speed = 0; 2092110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2093110072Ssimokawa fwdev.status = FWDEVINIT; 2094110072Ssimokawa /* Set cmstr bit on the cycle master */ 2095120660Ssimokawa quad = htonl(1 << 8); 2096110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2097120660Ssimokawa 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free); 2098110072Ssimokawa 2099110072Ssimokawa return 0; 2100110072Ssimokawa} 2101110072Ssimokawa 2102118455Ssimokawastatic int 2103118455Ssimokawafw_modevent(module_t mode, int type, void *data) 2104118455Ssimokawa{ 2105118455Ssimokawa int err = 0; 2106127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2107118455Ssimokawa static eventhandler_tag fwdev_ehtag = NULL; 2108118455Ssimokawa#endif 2109118455Ssimokawa 2110118455Ssimokawa switch (type) { 2111118455Ssimokawa case MOD_LOAD: 2112127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2113118455Ssimokawa fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone, 2114118455Ssimokawa fwdev_clone, 0, 1000); 2115118455Ssimokawa#endif 2116118455Ssimokawa break; 2117118455Ssimokawa case MOD_UNLOAD: 2118127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2119118455Ssimokawa if (fwdev_ehtag != NULL) 2120118455Ssimokawa EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag); 2121118455Ssimokawa#endif 2122118455Ssimokawa break; 2123118455Ssimokawa case MOD_SHUTDOWN: 2124118455Ssimokawa break; 2125132199Sphk default: 2126132199Sphk return (EOPNOTSUPP); 2127118455Ssimokawa } 2128118455Ssimokawa return (err); 2129118455Ssimokawa} 2130118455Ssimokawa 2131118455Ssimokawa 2132127468Ssimokawa#ifdef __DragonFly__ 2133127468SsimokawaDECLARE_DUMMY_MODULE(firewire); 2134127468Ssimokawa#endif 2135118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0); 2136103285SikobMODULE_VERSION(firewire, 1); 2137