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 35227843Smarius#include <sys/cdefs.h> 36227843Smarius__FBSDID("$FreeBSD: stable/11/sys/dev/firewire/firewire.c 310077 2016-12-14 16:43:04Z avg $"); 37227843Smarius 38103285Sikob#include <sys/param.h> 39103285Sikob#include <sys/systm.h> 40103285Sikob#include <sys/types.h> 41103285Sikob 42193066Sjamie#include <sys/jail.h> 43103285Sikob#include <sys/kernel.h> 44129879Sphk#include <sys/module.h> 45103285Sikob#include <sys/malloc.h> 46103285Sikob#include <sys/conf.h> 47103285Sikob#include <sys/sysctl.h> 48169806Ssimokawa#include <sys/kthread.h> 49103285Sikob 50170374Ssimokawa#include <sys/kdb.h> 51103285Sikob#include <sys/bus.h> /* used by smbus and newbus */ 52113584Ssimokawa#include <machine/bus.h> 53103285Sikob 54103285Sikob#include <dev/firewire/firewire.h> 55103285Sikob#include <dev/firewire/firewirereg.h> 56110072Ssimokawa#include <dev/firewire/fwmem.h> 57103285Sikob#include <dev/firewire/iec13213.h> 58103285Sikob#include <dev/firewire/iec68113.h> 59103285Sikob 60116376Ssimokawastruct crom_src_buf { 61116376Ssimokawa struct crom_src src; 62116376Ssimokawa struct crom_chunk root; 63116376Ssimokawa struct crom_chunk vendor; 64116376Ssimokawa struct crom_chunk hw; 65116376Ssimokawa}; 66116376Ssimokawa 67272214Skanint firewire_debug = 0, try_bmr = 1, hold_count = 0; 68103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 69108281Ssimokawa "FireWire driver debug flag"); 70109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 71109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 72109736Ssimokawa "Try to be a bus manager"); 73120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0, 74120850Ssimokawa "Number of count of bus resets for removing lost device information"); 75103285Sikob 76110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 77110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 78110195Ssimokawa 79103285Sikob#define FW_MAXASYRTY 4 80103285Sikob 81103285Sikobdevclass_t firewire_devclass; 82103285Sikob 83272214Skanstatic void firewire_identify(driver_t *, device_t); 84272214Skanstatic int firewire_probe(device_t); 85272214Skanstatic int firewire_attach(device_t); 86272214Skanstatic int firewire_detach(device_t); 87272214Skanstatic int firewire_resume(device_t); 88170374Ssimokawastatic void firewire_xfer_timeout(void *, int); 89212413Savgstatic device_t firewire_add_child(device_t, u_int, const char *, int); 90272214Skanstatic void fw_try_bmr(void *); 91272214Skanstatic void fw_try_bmr_callback(struct fw_xfer *); 92272214Skanstatic void fw_asystart(struct fw_xfer *); 93272214Skanstatic int fw_get_tlabel(struct firewire_comm *, struct fw_xfer *); 94272214Skanstatic void fw_bus_probe(void *); 95272214Skanstatic void fw_attach_dev(struct firewire_comm *); 96169806Ssimokawastatic void fw_bus_probe_thread(void *); 97106543Ssimokawa#ifdef FW_VMACCESS 98124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *); 99106543Ssimokawa#endif 100124169Ssimokawastatic int fw_bmr (struct firewire_comm *); 101170374Ssimokawastatic void fw_dump_hdr(struct fw_pkt *, char *); 102103285Sikob 103103285Sikobstatic device_method_t firewire_methods[] = { 104103285Sikob /* Device interface */ 105125238Ssimokawa DEVMETHOD(device_identify, firewire_identify), 106125238Ssimokawa DEVMETHOD(device_probe, firewire_probe), 107103285Sikob DEVMETHOD(device_attach, firewire_attach), 108103285Sikob DEVMETHOD(device_detach, firewire_detach), 109108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 110116978Ssimokawa DEVMETHOD(device_resume, firewire_resume), 111103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 112103285Sikob 113103285Sikob /* Bus interface */ 114103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 115103285Sikob 116227843Smarius DEVMETHOD_END 117103285Sikob}; 118272214Skan 119124251Ssimokawachar *linkspeed[] = { 120124251Ssimokawa "S100", "S200", "S400", "S800", 121124251Ssimokawa "S1600", "S3200", "undef", "undef" 122124251Ssimokawa}; 123103285Sikob 124124251Ssimokawastatic char *tcode_str[] = { 125124251Ssimokawa "WREQQ", "WREQB", "WRES", "undef", 126124251Ssimokawa "RREQQ", "RREQB", "RRESQ", "RRESB", 127124251Ssimokawa "CYCS", "LREQ", "STREAM", "LRES", 128124251Ssimokawa "undef", "undef", "PHY", "undef" 129124251Ssimokawa}; 130124251Ssimokawa 131114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 132114909Ssimokawa#define MAX_GAPHOP 15 133114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 134114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 135106813Ssimokawa 136103285Sikobstatic driver_t firewire_driver = { 137103285Sikob "firewire", 138103285Sikob firewire_methods, 139103285Sikob sizeof(struct firewire_softc), 140103285Sikob}; 141103285Sikob 142103285Sikob/* 143110072Ssimokawa * Lookup fwdev by node id. 144103285Sikob */ 145106810Ssimokawastruct fw_device * 146110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 147103285Sikob{ 148103285Sikob struct fw_device *fwdev; 149110072Ssimokawa 150277509Swill FW_GLOCK(fc); 151110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 152120660Ssimokawa if (fwdev->dst == dst && fwdev->status != FWDEVINVAL) 153103285Sikob break; 154277509Swill FW_GUNLOCK(fc); 155110072Ssimokawa 156106810Ssimokawa return fwdev; 157103285Sikob} 158106813Ssimokawa 159103285Sikob/* 160110072Ssimokawa * Lookup fwdev by EUI64. 161110072Ssimokawa */ 162110072Ssimokawastruct fw_device * 163110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 164110072Ssimokawa{ 165110072Ssimokawa struct fw_device *fwdev; 166110072Ssimokawa 167170374Ssimokawa FW_GLOCK(fc); 168110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 169110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 170110072Ssimokawa break; 171170374Ssimokawa FW_GUNLOCK(fc); 172110072Ssimokawa 173272214Skan if (fwdev == NULL) 174272214Skan return NULL; 175272214Skan if (fwdev->status == FWDEVINVAL) 176272214Skan return NULL; 177110072Ssimokawa return fwdev; 178110072Ssimokawa} 179110072Ssimokawa 180110072Ssimokawa/* 181103285Sikob * Async. request procedure for userland application. 182103285Sikob */ 183103285Sikobint 184103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 185103285Sikob{ 186103285Sikob int err = 0; 187103285Sikob struct fw_xferq *xferq; 188170374Ssimokawa int len; 189103285Sikob struct fw_pkt *fp; 190103285Sikob int tcode; 191103285Sikob struct tcode_info *info; 192103285Sikob 193272214Skan if (xfer == NULL) 194272214Skan return EINVAL; 195272214Skan if (xfer->hand == NULL) { 196167632Ssimokawa printf("hand == NULL\n"); 197103285Sikob return EINVAL; 198103285Sikob } 199120660Ssimokawa fp = &xfer->send.hdr; 200103285Sikob 201103285Sikob tcode = fp->mode.common.tcode & 0xf; 202103285Sikob info = &fc->tcode[tcode]; 203103285Sikob if (info->flag == 0) { 204124251Ssimokawa printf("invalid tcode=%x\n", tcode); 205103285Sikob return EINVAL; 206103285Sikob } 207170425Ssimokawa 208170425Ssimokawa /* XXX allow bus explore packets only after bus rest */ 209170425Ssimokawa if ((fc->status < FWBUSEXPLORE) && 210170425Ssimokawa ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) || 211170425Ssimokawa (fp->mode.rreqq.dest_lo < 0xf0000000) || 212170425Ssimokawa (fp->mode.rreqq.dest_lo >= 0xf0001000))) { 213170425Ssimokawa xfer->resp = EAGAIN; 214170425Ssimokawa xfer->flag = FWXF_BUSY; 215170425Ssimokawa return (EAGAIN); 216170425Ssimokawa } 217170425Ssimokawa 218103285Sikob if (info->flag & FWTI_REQ) 219103285Sikob xferq = fc->atq; 220103285Sikob else 221103285Sikob xferq = fc->ats; 222103285Sikob len = info->hdr_len; 223120660Ssimokawa if (xfer->send.pay_len > MAXREC(fc->maxrec)) { 224120660Ssimokawa printf("send.pay_len > maxrec\n"); 225120660Ssimokawa return EINVAL; 226120660Ssimokawa } 227103285Sikob if (info->flag & FWTI_BLOCK_STR) 228120660Ssimokawa len = fp->mode.stream.len; 229103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 230120660Ssimokawa len = fp->mode.rresb.len; 231120660Ssimokawa else 232120660Ssimokawa len = 0; 233272214Skan if (len != xfer->send.pay_len) { 234124251Ssimokawa printf("len(%d) != send.pay_len(%d) %s(%x)\n", 235124251Ssimokawa len, xfer->send.pay_len, tcode_str[tcode], tcode); 236272214Skan return EINVAL; 237103285Sikob } 238106790Ssimokawa 239272214Skan if (xferq->start == NULL) { 240103285Sikob printf("xferq->start == NULL\n"); 241103285Sikob return EINVAL; 242103285Sikob } 243272214Skan if (!(xferq->queued < xferq->maxq)) { 244108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 245108655Ssimokawa xferq->queued); 246170374Ssimokawa return EAGAIN; 247103285Sikob } 248103285Sikob 249170374Ssimokawa xfer->tl = -1; 250103285Sikob if (info->flag & FWTI_TLABEL) { 251170374Ssimokawa if (fw_get_tlabel(fc, xfer) < 0) 252130460Sdfr return EAGAIN; 253103285Sikob } 254103285Sikob 255103285Sikob xfer->resp = 0; 256103285Sikob xfer->fc = fc; 257103285Sikob xfer->q = xferq; 258103285Sikob 259103285Sikob fw_asystart(xfer); 260103285Sikob return err; 261103285Sikob} 262272214Skan 263103285Sikob/* 264103285Sikob * Wakeup blocked process. 265103285Sikob */ 266103285Sikobvoid 267170374Ssimokawafw_xferwake(struct fw_xfer *xfer) 268170374Ssimokawa{ 269170374Ssimokawa struct mtx *lock = &xfer->fc->wait_lock; 270170374Ssimokawa 271170374Ssimokawa mtx_lock(lock); 272170374Ssimokawa xfer->flag |= FWXF_WAKE; 273170374Ssimokawa mtx_unlock(lock); 274170374Ssimokawa 275103285Sikob wakeup(xfer); 276103285Sikob return; 277103285Sikob} 278103285Sikob 279170374Ssimokawaint 280170374Ssimokawafw_xferwait(struct fw_xfer *xfer) 281170374Ssimokawa{ 282170374Ssimokawa struct mtx *lock = &xfer->fc->wait_lock; 283170374Ssimokawa int err = 0; 284170374Ssimokawa 285170374Ssimokawa mtx_lock(lock); 286272214Skan while ((xfer->flag & FWXF_WAKE) == 0) 287272214Skan err = msleep(xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0); 288170374Ssimokawa mtx_unlock(lock); 289170374Ssimokawa 290170374Ssimokawa return (err); 291170374Ssimokawa} 292170374Ssimokawa 293103285Sikob/* 294103285Sikob * Async. request with given xfer structure. 295103285Sikob */ 296106790Ssimokawastatic void 297106790Ssimokawafw_asystart(struct fw_xfer *xfer) 298106790Ssimokawa{ 299103285Sikob struct firewire_comm *fc = xfer->fc; 300272214Skan 301170374Ssimokawa /* Protect from interrupt/timeout */ 302170374Ssimokawa FW_GLOCK(fc); 303170374Ssimokawa xfer->flag = FWXF_INQ; 304103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 305170374Ssimokawa#if 0 306272214Skan xfer->q->queued++; 307170374Ssimokawa#endif 308170374Ssimokawa FW_GUNLOCK(fc); 309103285Sikob /* XXX just queue for mbuf */ 310103285Sikob if (xfer->mbuf == NULL) 311103285Sikob xfer->q->start(fc); 312103285Sikob return; 313103285Sikob} 314106790Ssimokawa 315125238Ssimokawastatic void 316125238Ssimokawafirewire_identify(driver_t *driver, device_t parent) 317125238Ssimokawa{ 318125238Ssimokawa BUS_ADD_CHILD(parent, 0, "firewire", -1); 319125238Ssimokawa} 320125238Ssimokawa 321103285Sikobstatic int 322125238Ssimokawafirewire_probe(device_t dev) 323103285Sikob{ 324108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 325125238Ssimokawa return (0); 326103285Sikob} 327106790Ssimokawa 328277509Swill/* Just use a per-packet callout? */ 329110577Ssimokawastatic void 330170374Ssimokawafirewire_xfer_timeout(void *arg, int pending) 331110577Ssimokawa{ 332170374Ssimokawa struct firewire_comm *fc = (struct firewire_comm *)arg; 333170374Ssimokawa struct fw_xfer *xfer, *txfer; 334110577Ssimokawa struct timeval tv; 335110577Ssimokawa struct timeval split_timeout; 336170374Ssimokawa STAILQ_HEAD(, fw_xfer) xfer_timeout; 337277509Swill int i; 338110577Ssimokawa 339120660Ssimokawa split_timeout.tv_sec = 0; 340120660Ssimokawa split_timeout.tv_usec = 200 * 1000; /* 200 msec */ 341110577Ssimokawa 342110577Ssimokawa microtime(&tv); 343110577Ssimokawa timevalsub(&tv, &split_timeout); 344170374Ssimokawa STAILQ_INIT(&xfer_timeout); 345110577Ssimokawa 346171513Ssimokawa mtx_lock(&fc->tlabel_lock); 347277509Swill for (i = 0; i < nitems(fc->tlabels); i++) { 348169119Ssimokawa while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 349170427Ssimokawa if ((xfer->flag & FWXF_SENT) == 0) 350170427Ssimokawa /* not sent yet */ 351170427Ssimokawa break; 352110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 353110577Ssimokawa /* the rests are newer than this */ 354110577Ssimokawa break; 355110577Ssimokawa device_printf(fc->bdev, 356272214Skan "split transaction timeout: tl=0x%x flag=0x%02x\n", 357272214Skan i, xfer->flag); 358170374Ssimokawa fw_dump_hdr(&xfer->send.hdr, "send"); 359110577Ssimokawa xfer->resp = ETIMEDOUT; 360249291Swill xfer->tl = -1; 361170374Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel); 362170374Ssimokawa STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel); 363110577Ssimokawa } 364110577Ssimokawa } 365171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 366170374Ssimokawa fc->timeout(fc); 367170374Ssimokawa 368170374Ssimokawa STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer) 369272214Skan xfer->hand(xfer); 370110577Ssimokawa} 371110577Ssimokawa 372170374Ssimokawa#define WATCHDOG_HZ 10 373110577Ssimokawastatic void 374110577Ssimokawafirewire_watchdog(void *arg) 375110577Ssimokawa{ 376110577Ssimokawa struct firewire_comm *fc; 377170374Ssimokawa static int watchdog_clock = 0; 378110577Ssimokawa 379272214Skan fc = arg; 380121463Ssimokawa 381121463Ssimokawa /* 382121463Ssimokawa * At boot stage, the device interrupt is disabled and 383121463Ssimokawa * We encounter a timeout easily. To avoid this, 384121463Ssimokawa * ignore clock interrupt for a while. 385121463Ssimokawa */ 386170374Ssimokawa if (watchdog_clock > WATCHDOG_HZ * 15) 387170374Ssimokawa taskqueue_enqueue(fc->taskqueue, &fc->task_timeout); 388170374Ssimokawa else 389272214Skan watchdog_clock++; 390121463Ssimokawa 391170374Ssimokawa callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ, 392272214Skan firewire_watchdog, fc); 393110577Ssimokawa} 394110577Ssimokawa 395103285Sikob/* 396103285Sikob * The attach routine. 397103285Sikob */ 398103285Sikobstatic int 399118455Ssimokawafirewire_attach(device_t dev) 400103285Sikob{ 401118455Ssimokawa int unit; 402103285Sikob struct firewire_softc *sc = device_get_softc(dev); 403103285Sikob device_t pa = device_get_parent(dev); 404103285Sikob struct firewire_comm *fc; 405103285Sikob 406272214Skan fc = device_get_softc(pa); 407103285Sikob sc->fc = fc; 408116978Ssimokawa fc->status = FWBUSNOTREADY; 409103285Sikob 410118455Ssimokawa unit = device_get_unit(dev); 411272214Skan if (fc->nisodma > FWMAXNDMA) 412272214Skan fc->nisodma = FWMAXNDMA; 413103285Sikob 414118455Ssimokawa fwdev_makedev(sc); 415118455Ssimokawa 416272214Skan fc->crom_src_buf = malloc(sizeof(struct crom_src_buf), 417272214Skan M_FW, M_NOWAIT | M_ZERO); 418187993Ssbruno if (fc->crom_src_buf == NULL) { 419272214Skan device_printf(fc->dev, 420272214Skan "%s: unable to allocate crom src buffer\n", __func__); 421187993Ssbruno return ENOMEM; 422187993Ssbruno } 423272214Skan fc->topology_map = malloc(sizeof(struct fw_topology_map), 424272214Skan M_FW, M_NOWAIT | M_ZERO); 425187993Ssbruno if (fc->topology_map == NULL) { 426272214Skan device_printf(fc->dev, "%s: unable to allocate topology map\n", 427272214Skan __func__); 428187993Ssbruno free(fc->crom_src_buf, M_FW); 429187993Ssbruno return ENOMEM; 430187993Ssbruno } 431272214Skan fc->speed_map = malloc(sizeof(struct fw_speed_map), 432272214Skan M_FW, M_NOWAIT | M_ZERO); 433187993Ssbruno if (fc->speed_map == NULL) { 434272214Skan device_printf(fc->dev, "%s: unable to allocate speed map\n", 435272214Skan __func__); 436187993Ssbruno free(fc->crom_src_buf, M_FW); 437187993Ssbruno free(fc->topology_map, M_FW); 438187993Ssbruno return ENOMEM; 439187993Ssbruno } 440187993Ssbruno 441170374Ssimokawa mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF); 442171513Ssimokawa mtx_init(&fc->tlabel_lock, "fwtlabel", NULL, MTX_DEF); 443170374Ssimokawa CALLOUT_INIT(&fc->timeout_callout); 444170374Ssimokawa CALLOUT_INIT(&fc->bmr_callout); 445170374Ssimokawa CALLOUT_INIT(&fc->busprobe_callout); 446272214Skan TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, fc); 447108853Ssimokawa 448110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 449272214Skan firewire_watchdog, sc->fc); 450110193Ssimokawa 451169806Ssimokawa /* create thread */ 452272214Skan kproc_create(fw_bus_probe_thread, fc, &fc->probe_thread, 453272214Skan 0, 0, "fw%d_probe", unit); 454169806Ssimokawa 455103285Sikob /* Locate our children */ 456103285Sikob bus_generic_probe(dev); 457103285Sikob 458103285Sikob /* launch attachement of the added children */ 459103285Sikob bus_generic_attach(dev); 460103285Sikob 461103285Sikob /* bus_reset */ 462187993Ssbruno FW_GLOCK(fc); 463169117Ssimokawa fw_busreset(fc, FWBUSNOTREADY); 464187993Ssbruno FW_GUNLOCK(fc); 465103285Sikob fc->ibr(fc); 466103285Sikob 467103285Sikob return 0; 468103285Sikob} 469103285Sikob 470103285Sikob/* 471103285Sikob * Attach it as child. 472103285Sikob */ 473103285Sikobstatic device_t 474212413Savgfirewire_add_child(device_t dev, u_int order, const char *name, int unit) 475103285Sikob{ 476272214Skan device_t child; 477103285Sikob struct firewire_softc *sc; 478103285Sikob 479272214Skan sc = device_get_softc(dev); 480103285Sikob child = device_add_child(dev, name, unit); 481103285Sikob if (child) { 482103285Sikob device_set_ivars(child, sc->fc); 483103285Sikob device_probe_and_attach(child); 484103285Sikob } 485103285Sikob 486103285Sikob return child; 487103285Sikob} 488106790Ssimokawa 489116978Ssimokawastatic int 490116978Ssimokawafirewire_resume(device_t dev) 491116978Ssimokawa{ 492116978Ssimokawa struct firewire_softc *sc; 493116978Ssimokawa 494272214Skan sc = device_get_softc(dev); 495116978Ssimokawa sc->fc->status = FWBUSNOTREADY; 496272214Skan 497116978Ssimokawa bus_generic_resume(dev); 498116978Ssimokawa 499272214Skan return (0); 500116978Ssimokawa} 501116978Ssimokawa 502103285Sikob/* 503272214Skan * Detach it. 504103285Sikob */ 505103285Sikobstatic int 506118455Ssimokawafirewire_detach(device_t dev) 507103285Sikob{ 508103285Sikob struct firewire_softc *sc; 509169806Ssimokawa struct firewire_comm *fc; 510111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 511118455Ssimokawa int err; 512103285Sikob 513272214Skan sc = device_get_softc(dev); 514169806Ssimokawa fc = sc->fc; 515170374Ssimokawa mtx_lock(&fc->wait_lock); 516169806Ssimokawa fc->status = FWBUSDETACH; 517170374Ssimokawa wakeup(fc); 518170374Ssimokawa if (msleep(fc->probe_thread, &fc->wait_lock, PWAIT, "fwthr", hz * 60)) 519170374Ssimokawa printf("firewire probe thread didn't die\n"); 520170374Ssimokawa mtx_unlock(&fc->wait_lock); 521169806Ssimokawa 522272214Skan if (fc->arq != 0 && fc->arq->maxq > 0) 523178915Ssimokawa fw_drain_txq(fc); 524178915Ssimokawa 525118455Ssimokawa if ((err = fwdev_destroydev(sc)) != 0) 526118455Ssimokawa return err; 527106790Ssimokawa 528118455Ssimokawa if ((err = bus_generic_detach(dev)) != 0) 529118455Ssimokawa return err; 530111078Ssimokawa 531169806Ssimokawa callout_stop(&fc->timeout_callout); 532169806Ssimokawa callout_stop(&fc->bmr_callout); 533169806Ssimokawa callout_stop(&fc->busprobe_callout); 534111078Ssimokawa 535178915Ssimokawa /* XXX xfer_free and untimeout on all xfers */ 536169806Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; 537272214Skan fwdev = fwdev_next) { 538111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 539111078Ssimokawa free(fwdev, M_FW); 540111078Ssimokawa } 541169806Ssimokawa free(fc->topology_map, M_FW); 542169806Ssimokawa free(fc->speed_map, M_FW); 543169806Ssimokawa free(fc->crom_src_buf, M_FW); 544169806Ssimokawa 545171513Ssimokawa mtx_destroy(&fc->tlabel_lock); 546170374Ssimokawa mtx_destroy(&fc->wait_lock); 547272214Skan return (0); 548103285Sikob} 549106790Ssimokawa 550110577Ssimokawastatic void 551110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 552110577Ssimokawa{ 553110577Ssimokawa struct fw_xfer *xfer; 554110577Ssimokawa 555110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 556110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 557170374Ssimokawa#if 0 558272214Skan xferq->queued--; 559170374Ssimokawa#endif 560110577Ssimokawa xfer->resp = EAGAIN; 561170374Ssimokawa xfer->flag = FWXF_SENTERR; 562113584Ssimokawa fw_xfer_done(xfer); 563110577Ssimokawa } 564110577Ssimokawa} 565110577Ssimokawa 566110798Ssimokawavoid 567110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 568110798Ssimokawa{ 569170374Ssimokawa struct fw_xfer *xfer, *txfer; 570170374Ssimokawa STAILQ_HEAD(, fw_xfer) xfer_drain; 571110798Ssimokawa int i; 572110798Ssimokawa 573170374Ssimokawa STAILQ_INIT(&xfer_drain); 574170374Ssimokawa 575170374Ssimokawa FW_GLOCK(fc); 576110798Ssimokawa fw_xferq_drain(fc->atq); 577110798Ssimokawa fw_xferq_drain(fc->ats); 578272214Skan for (i = 0; i < fc->nisodma; i++) 579110798Ssimokawa fw_xferq_drain(fc->it[i]); 580171513Ssimokawa FW_GUNLOCK(fc); 581170374Ssimokawa 582171513Ssimokawa mtx_lock(&fc->tlabel_lock); 583272214Skan for (i = 0; i < 0x40; i++) 584170374Ssimokawa while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 585170374Ssimokawa if (firewire_debug) 586170374Ssimokawa printf("tl=%d flag=%d\n", i, xfer->flag); 587249291Swill xfer->tl = -1; 588170374Ssimokawa xfer->resp = EAGAIN; 589170374Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel); 590170374Ssimokawa STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel); 591170374Ssimokawa } 592171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 593170374Ssimokawa 594170374Ssimokawa STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer) 595272214Skan xfer->hand(xfer); 596110798Ssimokawa} 597110798Ssimokawa 598116376Ssimokawastatic void 599116376Ssimokawafw_reset_csr(struct firewire_comm *fc) 600103285Sikob{ 601103285Sikob int i; 602103285Sikob 603103285Sikob CSRARC(fc, STATE_CLEAR) 604272214Skan = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14; 605103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 606103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 607103285Sikob 608103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 609103285Sikob fc->irm = -1; 610103285Sikob 611103285Sikob fc->max_node = -1; 612103285Sikob 613272214Skan for (i = 2; i < 0x100 / 4 - 2; i++) { 614103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 615103285Sikob } 616272214Skan CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14; 617103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 618103285Sikob CSRARC(fc, RESET_START) = 0; 619103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 620103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 621103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 622103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 623103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 624103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 625103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 626103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 627258780Seadler CSRARC(fc, IP_CHANNELS) = (1U << 31); 628103285Sikob 629103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 630103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 631103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 632272214Skan 1 << 28 | 0xff << 16 | 0x09 << 8; 633103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 634103285Sikob 635272214Skan /* DV depend CSRs see blue book */ 636272214Skan CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 637272214Skan CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 638103285Sikob 639272214Skan CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14); 640103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 641116376Ssimokawa} 642113584Ssimokawa 643116376Ssimokawastatic void 644116376Ssimokawafw_init_crom(struct firewire_comm *fc) 645116376Ssimokawa{ 646116376Ssimokawa struct crom_src *src; 647116376Ssimokawa 648116376Ssimokawa src = &fc->crom_src_buf->src; 649116376Ssimokawa bzero(src, sizeof(struct crom_src)); 650116376Ssimokawa 651116376Ssimokawa /* BUS info sample */ 652116376Ssimokawa src->hdr.info_len = 4; 653116376Ssimokawa 654116376Ssimokawa src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 655116376Ssimokawa 656116376Ssimokawa src->businfo.irmc = 1; 657116376Ssimokawa src->businfo.cmc = 1; 658116376Ssimokawa src->businfo.isc = 1; 659116376Ssimokawa src->businfo.bmc = 1; 660116376Ssimokawa src->businfo.pmc = 0; 661116376Ssimokawa src->businfo.cyc_clk_acc = 100; 662116376Ssimokawa src->businfo.max_rec = fc->maxrec; 663116376Ssimokawa src->businfo.max_rom = MAXROM_4; 664189928Ssbruno#define FW_GENERATION_CHANGEABLE 2 665189928Ssbruno src->businfo.generation = FW_GENERATION_CHANGEABLE; 666116376Ssimokawa src->businfo.link_spd = fc->speed; 667116376Ssimokawa 668116376Ssimokawa src->businfo.eui64.hi = fc->eui.hi; 669116376Ssimokawa src->businfo.eui64.lo = fc->eui.lo; 670116376Ssimokawa 671116376Ssimokawa STAILQ_INIT(&src->chunk_list); 672116376Ssimokawa 673116376Ssimokawa fc->crom_src = src; 674116376Ssimokawa fc->crom_root = &fc->crom_src_buf->root; 675116376Ssimokawa} 676116376Ssimokawa 677116376Ssimokawastatic void 678116376Ssimokawafw_reset_crom(struct firewire_comm *fc) 679116376Ssimokawa{ 680116376Ssimokawa struct crom_src_buf *buf; 681116376Ssimokawa struct crom_src *src; 682116376Ssimokawa struct crom_chunk *root; 683116376Ssimokawa 684116376Ssimokawa buf = fc->crom_src_buf; 685116376Ssimokawa src = fc->crom_src; 686116376Ssimokawa root = fc->crom_root; 687116376Ssimokawa 688116376Ssimokawa STAILQ_INIT(&src->chunk_list); 689116376Ssimokawa 690116376Ssimokawa bzero(root, sizeof(struct crom_chunk)); 691116376Ssimokawa crom_add_chunk(src, NULL, root, 0); 692116376Ssimokawa crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 693116376Ssimokawa /* private company_id */ 694116376Ssimokawa crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 695116376Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project"); 696116376Ssimokawa crom_add_entry(root, CSRKEY_HW, __FreeBSD_version); 697193066Sjamie mtx_lock(&prison0.pr_mtx); 698194118Sjamie crom_add_simple_text(src, root, &buf->hw, prison0.pr_hostname); 699193066Sjamie mtx_unlock(&prison0.pr_mtx); 700116376Ssimokawa} 701116376Ssimokawa 702116376Ssimokawa/* 703116376Ssimokawa * Called after bus reset. 704116376Ssimokawa */ 705116376Ssimokawavoid 706169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status) 707116376Ssimokawa{ 708116376Ssimokawa struct firewire_dev_comm *fdc; 709117350Ssimokawa struct crom_src *src; 710116376Ssimokawa device_t *devlistp; 711189928Ssbruno uint32_t *newrom; 712116376Ssimokawa int i, devcnt; 713116376Ssimokawa 714187993Ssbruno FW_GLOCK_ASSERT(fc); 715187993Ssbruno if (fc->status == FWBUSMGRELECT) 716116376Ssimokawa callout_stop(&fc->bmr_callout); 717187993Ssbruno 718169117Ssimokawa fc->status = new_status; 719116376Ssimokawa fw_reset_csr(fc); 720187993Ssbruno 721187993Ssbruno if (fc->status == FWBUSNOTREADY) 722187993Ssbruno fw_init_crom(fc); 723187993Ssbruno 724116376Ssimokawa fw_reset_crom(fc); 725116376Ssimokawa 726113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 727272214Skan for (i = 0; i < devcnt; i++) 728272214Skan if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 729113584Ssimokawa fdc = device_get_softc(devlistp[i]); 730113584Ssimokawa if (fdc->post_busreset != NULL) 731113584Ssimokawa fdc->post_busreset(fdc); 732113584Ssimokawa } 733113584Ssimokawa free(devlistp, M_TEMP); 734113584Ssimokawa } 735116376Ssimokawa 736117350Ssimokawa src = &fc->crom_src_buf->src; 737272214Skan /* 738272214Skan * If the old config rom needs to be overwritten, 739272214Skan * bump the businfo.generation indicator to 740272214Skan * indicate that we need to be reprobed 741272214Skan * See 1394a-2000 8.3.2.5.4 for more details. 742272214Skan * generation starts at 2 and rolls over at 0xF 743272214Skan * back to 2. 744272214Skan * 745272214Skan * A generation of 0 indicates a device 746272214Skan * that is not 1394a-2000 compliant. 747272214Skan * A generation of 1 indicates a device that 748272214Skan * does not change it's Bus Info Block or 749272214Skan * Configuration ROM. 750272214Skan */ 751189928Ssbruno#define FW_MAX_GENERATION 0xF 752189928Ssbruno newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO); 753189928Ssbruno src = &fc->crom_src_buf->src; 754189928Ssbruno crom_load(src, newrom, CROMSIZE); 755189928Ssbruno if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) { 756277508Swill /* Bump generation and reload. */ 757277508Swill src->businfo.generation++; 758277508Swill 759277508Swill /* Handle generation count wraps. */ 760277508Swill if (src->businfo.generation < FW_GENERATION_CHANGEABLE) 761189928Ssbruno src->businfo.generation = FW_GENERATION_CHANGEABLE; 762277508Swill 763277508Swill /* Recalculate CRC to account for generation change. */ 764277508Swill crom_load(src, newrom, CROMSIZE); 765272214Skan bcopy(newrom, fc->config_rom, CROMSIZE); 766189928Ssbruno } 767189928Ssbruno free(newrom, M_FW); 768103285Sikob} 769106790Ssimokawa 770103285Sikob/* Call once after reboot */ 771106790Ssimokawavoid fw_init(struct firewire_comm *fc) 772103285Sikob{ 773103285Sikob int i; 774106543Ssimokawa#ifdef FW_VMACCESS 775103285Sikob struct fw_xfer *xfer; 776103285Sikob struct fw_bind *fwb; 777106543Ssimokawa#endif 778103285Sikob 779103285Sikob fc->arq->queued = 0; 780103285Sikob fc->ars->queued = 0; 781103285Sikob fc->atq->queued = 0; 782103285Sikob fc->ats->queued = 0; 783103285Sikob 784103285Sikob fc->arq->buf = NULL; 785103285Sikob fc->ars->buf = NULL; 786103285Sikob fc->atq->buf = NULL; 787103285Sikob fc->ats->buf = NULL; 788103285Sikob 789113584Ssimokawa fc->arq->flag = 0; 790113584Ssimokawa fc->ars->flag = 0; 791113584Ssimokawa fc->atq->flag = 0; 792113584Ssimokawa fc->ats->flag = 0; 793103285Sikob 794103285Sikob STAILQ_INIT(&fc->atq->q); 795103285Sikob STAILQ_INIT(&fc->ats->q); 796103285Sikob 797272214Skan for (i = 0; i < fc->nisodma; i++) { 798103285Sikob fc->it[i]->queued = 0; 799103285Sikob fc->ir[i]->queued = 0; 800103285Sikob 801103285Sikob fc->it[i]->start = NULL; 802103285Sikob fc->ir[i]->start = NULL; 803103285Sikob 804103285Sikob fc->it[i]->buf = NULL; 805103285Sikob fc->ir[i]->buf = NULL; 806103285Sikob 807103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 808103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 809103285Sikob 810103285Sikob STAILQ_INIT(&fc->it[i]->q); 811103285Sikob STAILQ_INIT(&fc->ir[i]->q); 812103285Sikob } 813103285Sikob 814103285Sikob fc->arq->maxq = FWMAXQUEUE; 815103285Sikob fc->ars->maxq = FWMAXQUEUE; 816103285Sikob fc->atq->maxq = FWMAXQUEUE; 817103285Sikob fc->ats->maxq = FWMAXQUEUE; 818103285Sikob 819272214Skan for (i = 0; i < fc->nisodma; i++) { 820103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 821103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 822103285Sikob } 823187993Ssbruno 824103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 825103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 826103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 827103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 828103285Sikob 829110193Ssimokawa STAILQ_INIT(&fc->devices); 830103285Sikob 831272214Skan /* Initialize Async handlers */ 832103285Sikob STAILQ_INIT(&fc->binds); 833272214Skan for (i = 0; i < 0x40; i++) { 834103285Sikob STAILQ_INIT(&fc->tlabels[i]); 835103285Sikob } 836103285Sikob 837103285Sikob/* DV depend CSRs see blue book */ 838103285Sikob#if 0 839103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 840103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 841272214Skan for (i = 4; i < 0x7c/4; i += 4) { 842272214Skan CSRARC(fc, i + oPCR) = 0x8000007a; 843103285Sikob } 844272214Skan 845103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 846103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 847272214Skan for (i = 4; i < 0x7c/4; i += 4) { 848272214Skan CSRARC(fc, i + iPCR) = 0x0; 849103285Sikob } 850103285Sikob#endif 851103285Sikob 852116376Ssimokawa fc->crom_src_buf = NULL; 853103285Sikob 854106543Ssimokawa#ifdef FW_VMACCESS 855103285Sikob xfer = fw_xfer_alloc(); 856272214Skan if (xfer == NULL) 857272214Skan return; 858103285Sikob 859272214Skan fwb = malloc(sizeof(struct fw_bind), M_FW, M_NOWAIT); 860272214Skan if (fwb == NULL) { 861103285Sikob fw_xfer_free(xfer); 862139680Sjmg return; 863103285Sikob } 864167632Ssimokawa xfer->hand = fw_vmaccess; 865103285Sikob xfer->fc = fc; 866103285Sikob xfer->sc = NULL; 867103285Sikob 868103285Sikob fwb->start_hi = 0x2; 869103285Sikob fwb->start_lo = 0; 870103285Sikob fwb->addrlen = 0xffffffff; 871103285Sikob fwb->xfer = xfer; 872103285Sikob fw_bindadd(fc, fwb); 873106543Ssimokawa#endif 874103285Sikob} 875106790Ssimokawa 876272214Skan#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)? -1 : \ 877272214Skan ((fwb)->end < (addr)) ? 1 : 0) 878120660Ssimokawa 879103285Sikob/* 880129541Sdfr * To lookup bound process from IEEE1394 address. 881103285Sikob */ 882106813Ssimokawastruct fw_bind * 883129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo) 884103285Sikob{ 885120660Ssimokawa u_int64_t addr; 886170374Ssimokawa struct fw_bind *tfw, *r = NULL; 887120660Ssimokawa 888120660Ssimokawa addr = ((u_int64_t)dest_hi << 32) | dest_lo; 889170374Ssimokawa FW_GLOCK(fc); 890120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 891170374Ssimokawa if (BIND_CMP(addr, tfw) == 0) { 892170374Ssimokawa r = tfw; 893170374Ssimokawa break; 894170374Ssimokawa } 895170374Ssimokawa FW_GUNLOCK(fc); 896272214Skan return (r); 897103285Sikob} 898106790Ssimokawa 899103285Sikob/* 900103285Sikob * To bind IEEE1394 address block to process. 901103285Sikob */ 902106790Ssimokawaint 903106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 904103285Sikob{ 905120660Ssimokawa struct fw_bind *tfw, *prev = NULL; 906170374Ssimokawa int r = 0; 907120660Ssimokawa 908120660Ssimokawa if (fwb->start > fwb->end) { 909127468Ssimokawa printf("%s: invalid range\n", __func__); 910120660Ssimokawa return EINVAL; 911120660Ssimokawa } 912120660Ssimokawa 913170374Ssimokawa FW_GLOCK(fc); 914120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) { 915120660Ssimokawa if (fwb->end < tfw->start) 916120660Ssimokawa break; 917120660Ssimokawa prev = tfw; 918120660Ssimokawa } 919170374Ssimokawa if (prev == NULL) 920103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 921170374Ssimokawa else if (prev->end < fwb->start) 922120660Ssimokawa STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 923170374Ssimokawa else { 924170374Ssimokawa printf("%s: bind failed\n", __func__); 925170374Ssimokawa r = EBUSY; 926103285Sikob } 927170374Ssimokawa FW_GUNLOCK(fc); 928170374Ssimokawa return (r); 929103285Sikob} 930103285Sikob 931103285Sikob/* 932103285Sikob * To free IEEE1394 address block. 933103285Sikob */ 934106790Ssimokawaint 935106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 936103285Sikob{ 937120660Ssimokawa#if 0 938120660Ssimokawa struct fw_xfer *xfer, *next; 939120660Ssimokawa#endif 940120660Ssimokawa struct fw_bind *tfw; 941103285Sikob int s; 942103285Sikob 943103285Sikob s = splfw(); 944170374Ssimokawa FW_GLOCK(fc); 945120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 946120660Ssimokawa if (tfw == fwb) { 947120660Ssimokawa STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 948120660Ssimokawa goto found; 949120660Ssimokawa } 950120660Ssimokawa 951129541Sdfr printf("%s: no such binding\n", __func__); 952170374Ssimokawa FW_GUNLOCK(fc); 953120660Ssimokawa splx(s); 954120660Ssimokawa return (1); 955120660Ssimokawafound: 956120660Ssimokawa#if 0 957113584Ssimokawa /* shall we do this? */ 958113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 959113584Ssimokawa next = STAILQ_NEXT(xfer, link); 960113584Ssimokawa fw_xfer_free(xfer); 961113584Ssimokawa } 962113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 963120660Ssimokawa#endif 964170374Ssimokawa FW_GUNLOCK(fc); 965113584Ssimokawa 966103285Sikob splx(s); 967103285Sikob return 0; 968103285Sikob} 969103285Sikob 970169130Ssimokawaint 971169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type, 972169130Ssimokawa int slen, int rlen, int n, 973169130Ssimokawa struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *)) 974169130Ssimokawa{ 975169130Ssimokawa int i, s; 976169130Ssimokawa struct fw_xfer *xfer; 977169130Ssimokawa 978169130Ssimokawa for (i = 0; i < n; i++) { 979169130Ssimokawa xfer = fw_xfer_alloc_buf(type, slen, rlen); 980169130Ssimokawa if (xfer == NULL) 981272214Skan return (i); 982169130Ssimokawa xfer->fc = fc; 983169130Ssimokawa xfer->sc = sc; 984169130Ssimokawa xfer->hand = hand; 985169130Ssimokawa s = splfw(); 986169130Ssimokawa STAILQ_INSERT_TAIL(q, xfer, link); 987169130Ssimokawa splx(s); 988169130Ssimokawa } 989169130Ssimokawa return (n); 990169130Ssimokawa} 991169130Ssimokawa 992169130Ssimokawavoid 993169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q) 994169130Ssimokawa{ 995169130Ssimokawa struct fw_xfer *xfer, *next; 996169130Ssimokawa 997169130Ssimokawa for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) { 998272214Skan next = STAILQ_NEXT(xfer, link); 999272214Skan fw_xfer_free_buf(xfer); 1000272214Skan } 1001272214Skan STAILQ_INIT(q); 1002169130Ssimokawa} 1003170374Ssimokawa/* 1004170374Ssimokawa * dump packet header 1005170374Ssimokawa */ 1006170374Ssimokawastatic void 1007170374Ssimokawafw_dump_hdr(struct fw_pkt *fp, char *prefix) 1008170374Ssimokawa{ 1009170374Ssimokawa printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x " 1010170374Ssimokawa "src=0x%03x\n", prefix, 1011170374Ssimokawa fp->mode.hdr.dst & 0x3f, 1012170374Ssimokawa fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3, 1013170374Ssimokawa fp->mode.hdr.tcode, fp->mode.hdr.pri, 1014170374Ssimokawa fp->mode.hdr.src); 1015170374Ssimokawa} 1016169130Ssimokawa 1017103285Sikob/* 1018103285Sikob * To free transaction label. 1019103285Sikob */ 1020106790Ssimokawastatic void 1021106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 1022103285Sikob{ 1023169119Ssimokawa struct fw_xfer *txfer; 1024103285Sikob 1025171513Ssimokawa mtx_lock(&fc->tlabel_lock); 1026249291Swill if (xfer->tl < 0) { 1027249291Swill mtx_unlock(&fc->tlabel_lock); 1028249291Swill return; 1029249291Swill } 1030271795Swill /* make sure the label is allocated */ 1031169119Ssimokawa STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel) 1032272214Skan if (txfer == xfer) 1033169119Ssimokawa break; 1034169119Ssimokawa if (txfer == NULL) { 1035170374Ssimokawa printf("%s: the xfer is not in the queue " 1036170374Ssimokawa "(tlabel=%d, flag=0x%x)\n", 1037170374Ssimokawa __FUNCTION__, xfer->tl, xfer->flag); 1038170374Ssimokawa fw_dump_hdr(&xfer->send.hdr, "send"); 1039170374Ssimokawa fw_dump_hdr(&xfer->recv.hdr, "recv"); 1040170374Ssimokawa kdb_backtrace(); 1041171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1042169119Ssimokawa return; 1043103285Sikob } 1044169119Ssimokawa 1045169119Ssimokawa STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel); 1046249291Swill xfer->tl = -1; 1047171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1048103285Sikob return; 1049103285Sikob} 1050106790Ssimokawa 1051103285Sikob/* 1052103285Sikob * To obtain XFER structure by transaction label. 1053103285Sikob */ 1054106790Ssimokawastatic struct fw_xfer * 1055170374Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode) 1056103285Sikob{ 1057103285Sikob struct fw_xfer *xfer; 1058103285Sikob int s = splfw(); 1059170374Ssimokawa int req; 1060103285Sikob 1061171513Ssimokawa mtx_lock(&fc->tlabel_lock); 1062169119Ssimokawa STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel) 1063272214Skan if (xfer->send.hdr.mode.hdr.dst == node) { 1064171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1065103285Sikob splx(s); 1066170374Ssimokawa KASSERT(xfer->tl == tlabel, 1067170374Ssimokawa ("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel)); 1068170374Ssimokawa /* extra sanity check */ 1069170374Ssimokawa req = xfer->send.hdr.mode.hdr.tcode; 1070170374Ssimokawa if (xfer->fc->tcode[req].valid_res != tcode) { 1071170374Ssimokawa printf("%s: invalid response tcode " 1072170374Ssimokawa "(0x%x for 0x%x)\n", __FUNCTION__, 1073170374Ssimokawa tcode, req); 1074272214Skan return (NULL); 1075170374Ssimokawa } 1076272214Skan 1077110577Ssimokawa if (firewire_debug > 2) 1078110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 1079272214Skan return (xfer); 1080103285Sikob } 1081171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1082110577Ssimokawa if (firewire_debug > 1) 1083110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 1084103285Sikob splx(s); 1085272214Skan return (NULL); 1086103285Sikob} 1087106790Ssimokawa 1088103285Sikob/* 1089103285Sikob * To allocate IEEE1394 XFER structure. 1090103285Sikob */ 1091106790Ssimokawastruct fw_xfer * 1092110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 1093103285Sikob{ 1094103285Sikob struct fw_xfer *xfer; 1095106790Ssimokawa 1096110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 1097106790Ssimokawa if (xfer == NULL) 1098106790Ssimokawa return xfer; 1099106790Ssimokawa 1100110269Ssimokawa xfer->malloc = type; 1101310077Savg xfer->tl = -1; 1102106790Ssimokawa 1103103285Sikob return xfer; 1104103285Sikob} 1105106790Ssimokawa 1106113584Ssimokawastruct fw_xfer * 1107113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 1108113584Ssimokawa{ 1109113584Ssimokawa struct fw_xfer *xfer; 1110113584Ssimokawa 1111113584Ssimokawa xfer = fw_xfer_alloc(type); 1112126102Scperciva if (xfer == NULL) 1113272214Skan return (NULL); 1114120660Ssimokawa xfer->send.pay_len = send_len; 1115120660Ssimokawa xfer->recv.pay_len = recv_len; 1116120660Ssimokawa if (send_len > 0) { 1117120660Ssimokawa xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 1118120660Ssimokawa if (xfer->send.payload == NULL) { 1119113584Ssimokawa fw_xfer_free(xfer); 1120272214Skan return (NULL); 1121113584Ssimokawa } 1122113584Ssimokawa } 1123120660Ssimokawa if (recv_len > 0) { 1124120660Ssimokawa xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 1125120660Ssimokawa if (xfer->recv.payload == NULL) { 1126120660Ssimokawa if (xfer->send.payload != NULL) 1127120660Ssimokawa free(xfer->send.payload, type); 1128113584Ssimokawa fw_xfer_free(xfer); 1129272214Skan return (NULL); 1130113584Ssimokawa } 1131113584Ssimokawa } 1132272214Skan return (xfer); 1133113584Ssimokawa} 1134113584Ssimokawa 1135103285Sikob/* 1136103285Sikob * IEEE1394 XFER post process. 1137103285Sikob */ 1138103285Sikobvoid 1139103285Sikobfw_xfer_done(struct fw_xfer *xfer) 1140103285Sikob{ 1141167632Ssimokawa if (xfer->hand == NULL) { 1142167632Ssimokawa printf("hand == NULL\n"); 1143103285Sikob return; 1144117716Ssimokawa } 1145103285Sikob 1146121505Ssimokawa if (xfer->fc == NULL) 1147121505Ssimokawa panic("fw_xfer_done: why xfer->fc is NULL?"); 1148121505Ssimokawa 1149168051Ssimokawa fw_tl_free(xfer->fc, xfer); 1150167632Ssimokawa xfer->hand(xfer); 1151103285Sikob} 1152103285Sikob 1153106790Ssimokawavoid 1154272214Skanfw_xfer_unload(struct fw_xfer *xfer) 1155103285Sikob{ 1156113584Ssimokawa 1157272214Skan if (xfer == NULL) 1158272214Skan return; 1159277508Swill 1160113584Ssimokawa if (xfer->fc != NULL) { 1161277508Swill FW_GLOCK(xfer->fc); 1162277508Swill if (xfer->flag & FWXF_INQ) { 1163277508Swill STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1164277508Swill xfer->flag &= ~FWXF_INQ; 1165277508Swill #if 0 1166277508Swill xfer->q->queued--; 1167277508Swill #endif 1168277508Swill } 1169277508Swill FW_GUNLOCK(xfer->fc); 1170277508Swill 1171249291Swill /* 1172249291Swill * Ensure that any tlabel owner can't access this 1173249291Swill * xfer after it's freed. 1174249291Swill */ 1175249291Swill fw_tl_free(xfer->fc, xfer); 1176114729Ssimokawa#if 1 1177272214Skan if (xfer->flag & FWXF_START) 1178114729Ssimokawa /* 1179114729Ssimokawa * This could happen if: 1180114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 1181114729Ssimokawa * 2. firewire_watch() is called. 1182114729Ssimokawa */ 1183114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 1184103285Sikob#endif 1185103285Sikob } 1186170374Ssimokawa xfer->flag = FWXF_INIT; 1187113584Ssimokawa xfer->resp = 0; 1188113584Ssimokawa} 1189272214Skan 1190113584Ssimokawa/* 1191272214Skan * To free IEEE1394 XFER structure. 1192113584Ssimokawa */ 1193113584Ssimokawavoid 1194272214Skanfw_xfer_free_buf(struct fw_xfer *xfer) 1195113584Ssimokawa{ 1196120660Ssimokawa if (xfer == NULL) { 1197127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1198120660Ssimokawa return; 1199120660Ssimokawa } 1200113584Ssimokawa fw_xfer_unload(xfer); 1201272214Skan if (xfer->send.payload != NULL) 1202120660Ssimokawa free(xfer->send.payload, xfer->malloc); 1203272214Skan if (xfer->recv.payload != NULL) 1204120660Ssimokawa free(xfer->recv.payload, xfer->malloc); 1205110269Ssimokawa free(xfer, xfer->malloc); 1206103285Sikob} 1207103285Sikob 1208120660Ssimokawavoid 1209272214Skanfw_xfer_free(struct fw_xfer *xfer) 1210120660Ssimokawa{ 1211120660Ssimokawa if (xfer == NULL) { 1212127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1213120660Ssimokawa return; 1214120660Ssimokawa } 1215120660Ssimokawa fw_xfer_unload(xfer); 1216120660Ssimokawa free(xfer, xfer->malloc); 1217120660Ssimokawa} 1218120660Ssimokawa 1219120660Ssimokawavoid 1220110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 1221103285Sikob{ 1222103285Sikob#if 0 1223170374Ssimokawa printf("asyreq done flag=0x%02x resp=%d\n", 1224170374Ssimokawa xfer->flag, xfer->resp); 1225103285Sikob#endif 1226103285Sikob fw_xfer_free(xfer); 1227103285Sikob} 1228103285Sikob 1229103285Sikob/* 1230272214Skan * To configure PHY. 1231103285Sikob */ 1232103285Sikobstatic void 1233103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1234103285Sikob{ 1235103285Sikob struct fw_xfer *xfer; 1236103285Sikob struct fw_pkt *fp; 1237103285Sikob 1238103285Sikob fc->status = FWBUSPHYCONF; 1239103285Sikob 1240120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1241113584Ssimokawa if (xfer == NULL) 1242113584Ssimokawa return; 1243103285Sikob xfer->fc = fc; 1244167632Ssimokawa xfer->hand = fw_asy_callback_free; 1245103285Sikob 1246120660Ssimokawa fp = &xfer->send.hdr; 1247103285Sikob fp->mode.ld[1] = 0; 1248103285Sikob if (root_node >= 0) 1249272214Skan fp->mode.ld[1] |= (1 << 23) | (root_node & 0x3f) << 24; 1250103285Sikob if (gap_count >= 0) 1251272214Skan fp->mode.ld[1] |= (1 << 22) | (gap_count & 0x3f) << 16; 1252103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 1253103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 1254103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 1255103285Sikob 1256107653Ssimokawa if (firewire_debug) 1257188722Ssbruno device_printf(fc->bdev, "%s: root_node=%d gap_count=%d\n", 1258188722Ssbruno __func__, root_node, gap_count); 1259103285Sikob fw_asyreq(fc, -1, xfer); 1260103285Sikob} 1261103285Sikob 1262103285Sikob/* 1263272214Skan * Dump self ID. 1264103285Sikob */ 1265103285Sikobstatic void 1266129585Sdfrfw_print_sid(uint32_t sid) 1267103285Sikob{ 1268103285Sikob union fw_self_id *s; 1269103285Sikob s = (union fw_self_id *) &sid; 1270272214Skan if (s->p0.sequel) { 1271272214Skan if (s->p1.sequence_num == FW_SELF_ID_PAGE0) { 1272188726Ssbruno printf("node:%d p3:%d p4:%d p5:%d p6:%d p7:%d" 1273272214Skan "p8:%d p9:%d p10:%d\n", 1274272214Skan s->p1.phy_id, s->p1.port3, s->p1.port4, 1275272214Skan s->p1.port5, s->p1.port6, s->p1.port7, 1276272214Skan s->p1.port8, s->p1.port9, s->p1.port10); 1277272214Skan } else if (s->p2.sequence_num == FW_SELF_ID_PAGE1) { 1278188726Ssbruno printf("node:%d p11:%d p12:%d p13:%d p14:%d p15:%d\n", 1279272214Skan s->p2.phy_id, s->p2.port11, s->p2.port12, 1280272214Skan s->p2.port13, s->p2.port14, s->p2.port15); 1281188726Ssbruno } else { 1282188726Ssbruno printf("node:%d Unknown Self ID Page number %d\n", 1283272214Skan s->p1.phy_id, s->p1.sequence_num); 1284188726Ssbruno } 1285188726Ssbruno } else { 1286188726Ssbruno printf("node:%d link:%d gap:%d spd:%d con:%d pwr:%d" 1287272214Skan " p0:%d p1:%d p2:%d i:%d m:%d\n", 1288272214Skan s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1289272214Skan s->p0.phy_speed, s->p0.contender, 1290272214Skan s->p0.power_class, s->p0.port0, s->p0.port1, 1291272214Skan s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1292188726Ssbruno } 1293103285Sikob} 1294103285Sikob 1295103285Sikob/* 1296272214Skan * To receive self ID. 1297103285Sikob */ 1298272214Skanvoid fw_sidrcv(struct firewire_comm *fc, uint32_t *sid, u_int len) 1299103285Sikob{ 1300129585Sdfr uint32_t *p; 1301103285Sikob union fw_self_id *self_id; 1302103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1303103285Sikob 1304272214Skan fc->sid_cnt = len / (sizeof(uint32_t) * 2); 1305103285Sikob fc->max_node = fc->nodeid & 0x3f; 1306129585Sdfr CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16; 1307103285Sikob fc->status = FWBUSCYMELECT; 1308103285Sikob fc->topology_map->crc_len = 2; 1309272214Skan fc->topology_map->generation++; 1310103285Sikob fc->topology_map->self_id_count = 0; 1311272214Skan fc->topology_map->node_count= 0; 1312272214Skan fc->speed_map->generation++; 1313272214Skan fc->speed_map->crc_len = 1 + (64 * 64 + 3) / 4; 1314103285Sikob self_id = &fc->topology_map->self_id[0]; 1315272214Skan for (i = 0; i < fc->sid_cnt; i++) { 1316103285Sikob if (sid[1] != ~sid[0]) { 1317272214Skan device_printf(fc->bdev, 1318272214Skan "%s: ERROR invalid self-id packet\n", __func__); 1319103285Sikob sid += 2; 1320103285Sikob continue; 1321103285Sikob } 1322103285Sikob *self_id = *((union fw_self_id *)sid); 1323103285Sikob fc->topology_map->crc_len++; 1324272214Skan if (self_id->p0.sequel == 0) { 1325272214Skan fc->topology_map->node_count++; 1326103285Sikob c_port = 0; 1327188722Ssbruno if (firewire_debug) 1328188722Ssbruno fw_print_sid(sid[0]); 1329103285Sikob node = self_id->p0.phy_id; 1330272214Skan if (fc->max_node < node) 1331103285Sikob fc->max_node = self_id->p0.phy_id; 1332103285Sikob /* XXX I'm not sure this is the right speed_map */ 1333272214Skan fc->speed_map->speed[node][node] = 1334272214Skan self_id->p0.phy_speed; 1335272214Skan for (j = 0; j < node; j++) { 1336272214Skan fc->speed_map->speed[j][node] = 1337272214Skan fc->speed_map->speed[node][j] = 1338272214Skan min(fc->speed_map->speed[j][j], 1339272214Skan self_id->p0.phy_speed); 1340103285Sikob } 1341103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1342272214Skan (self_id->p0.link_active && self_id->p0.contender)) 1343103285Sikob fc->irm = self_id->p0.phy_id; 1344272214Skan if (self_id->p0.port0 >= 0x2) 1345103285Sikob c_port++; 1346272214Skan if (self_id->p0.port1 >= 0x2) 1347103285Sikob c_port++; 1348272214Skan if (self_id->p0.port2 >= 0x2) 1349103285Sikob c_port++; 1350103285Sikob } 1351272214Skan if (c_port > 2) 1352103285Sikob i_branch += (c_port - 2); 1353103285Sikob sid += 2; 1354103285Sikob self_id++; 1355272214Skan fc->topology_map->self_id_count++; 1356103285Sikob } 1357103285Sikob /* CRC */ 1358103285Sikob fc->topology_map->crc = fw_crc16( 1359272214Skan (uint32_t *)&fc->topology_map->generation, 1360272214Skan fc->topology_map->crc_len * 4); 1361103285Sikob fc->speed_map->crc = fw_crc16( 1362272214Skan (uint32_t *)&fc->speed_map->generation, 1363272214Skan fc->speed_map->crc_len * 4); 1364103285Sikob /* byteswap and copy to CSR */ 1365129585Sdfr p = (uint32_t *)fc->topology_map; 1366103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1367103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1368129585Sdfr p = (uint32_t *)fc->speed_map; 1369103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1370103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1371129585Sdfr /* don't byte-swap uint8_t array */ 1372272214Skan bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1) * 4); 1373103285Sikob 1374103285Sikob fc->max_hop = fc->max_node - i_branch; 1375188722Ssbruno device_printf(fc->bdev, "%d nodes, maxhop <= %d %s irm(%d) %s\n", 1376272214Skan fc->max_node + 1, fc->max_hop, 1377272214Skan (fc->irm == -1) ? "Not IRM capable" : "cable IRM", 1378272214Skan fc->irm, (fc->irm == fc->nodeid) ? " (me) " : ""); 1379103285Sikob 1380109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1381114909Ssimokawa if (fc->irm == fc->nodeid) { 1382103285Sikob fc->status = FWBUSMGRDONE; 1383103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1384114909Ssimokawa fw_bmr(fc); 1385109736Ssimokawa } else { 1386103285Sikob fc->status = FWBUSMGRELECT; 1387272214Skan callout_reset(&fc->bmr_callout, hz / 8, 1388272214Skan fw_try_bmr, fc); 1389103285Sikob } 1390114909Ssimokawa } else 1391103285Sikob fc->status = FWBUSMGRDONE; 1392114909Ssimokawa 1393272214Skan callout_reset(&fc->busprobe_callout, hz / 4, fw_bus_probe, fc); 1394103285Sikob} 1395106790Ssimokawa 1396103285Sikob/* 1397272214Skan * To probe devices on the IEEE1394 bus. 1398103285Sikob */ 1399106790Ssimokawastatic void 1400272214Skanfw_bus_probe(void *arg) 1401103285Sikob{ 1402272214Skan struct firewire_comm *fc; 1403272214Skan struct fw_device *fwdev; 1404103285Sikob int s; 1405103285Sikob 1406103285Sikob s = splfw(); 1407272214Skan fc = arg; 1408103285Sikob fc->status = FWBUSEXPLORE; 1409103285Sikob 1410120850Ssimokawa /* Invalidate all devices, just after bus reset. */ 1411188722Ssbruno if (firewire_debug) 1412188722Ssbruno device_printf(fc->bdev, "%s:" 1413188722Ssbruno "iterate and invalidate all nodes\n", 1414188722Ssbruno __func__); 1415120850Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1416110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1417103285Sikob fwdev->status = FWDEVINVAL; 1418103285Sikob fwdev->rcnt = 0; 1419188722Ssbruno if (firewire_debug) 1420188722Ssbruno device_printf(fc->bdev, "%s:" 1421188722Ssbruno "Invalidate Dev ID: %08x%08x\n", 1422188722Ssbruno __func__, fwdev->eui.hi, fwdev->eui.lo); 1423188722Ssbruno } else { 1424188722Ssbruno if (firewire_debug) 1425188722Ssbruno device_printf(fc->bdev, "%s:" 1426188722Ssbruno "Dev ID: %08x%08x already invalid\n", 1427188722Ssbruno __func__, fwdev->eui.hi, fwdev->eui.lo); 1428103285Sikob } 1429169806Ssimokawa splx(s); 1430120850Ssimokawa 1431272214Skan wakeup(fc); 1432103285Sikob} 1433106790Ssimokawa 1434169806Ssimokawastatic int 1435169806Ssimokawafw_explore_read_quads(struct fw_device *fwdev, int offset, 1436188722Ssbruno uint32_t *quad, int length) 1437129274Sdfr{ 1438169806Ssimokawa struct fw_xfer *xfer; 1439169806Ssimokawa uint32_t tmp; 1440169806Ssimokawa int i, error; 1441129274Sdfr 1442272214Skan for (i = 0; i < length; i++, offset += sizeof(uint32_t)) { 1443272214Skan xfer = fwmem_read_quad(fwdev, NULL, -1, 0xffff, 1444272214Skan 0xf0000000 | offset, &tmp, fw_xferwake); 1445169806Ssimokawa if (xfer == NULL) 1446169806Ssimokawa return (-1); 1447170374Ssimokawa fw_xferwait(xfer); 1448169806Ssimokawa 1449169806Ssimokawa if (xfer->resp == 0) 1450169806Ssimokawa quad[i] = ntohl(tmp); 1451169806Ssimokawa 1452169806Ssimokawa error = xfer->resp; 1453169806Ssimokawa fw_xfer_free(xfer); 1454169806Ssimokawa if (error) 1455169806Ssimokawa return (error); 1456169806Ssimokawa } 1457169806Ssimokawa return (0); 1458169806Ssimokawa} 1459169806Ssimokawa 1460169806Ssimokawa 1461169806Ssimokawastatic int 1462169806Ssimokawafw_explore_csrblock(struct fw_device *fwdev, int offset, int recur) 1463169806Ssimokawa{ 1464169806Ssimokawa int err, i, off; 1465169806Ssimokawa struct csrdirectory *dir; 1466169806Ssimokawa struct csrreg *reg; 1467169806Ssimokawa 1468272214Skan dir = (struct csrdirectory *)&fwdev->csrrom[offset / sizeof(uint32_t)]; 1469169806Ssimokawa err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, 1470169806Ssimokawa (uint32_t *)dir, 1); 1471169806Ssimokawa if (err) 1472169806Ssimokawa return (-1); 1473169806Ssimokawa 1474169806Ssimokawa offset += sizeof(uint32_t); 1475272214Skan reg = (struct csrreg *)&fwdev->csrrom[offset / sizeof(uint32_t)]; 1476169806Ssimokawa err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, 1477169806Ssimokawa (uint32_t *)reg, dir->crc_len); 1478169806Ssimokawa if (err) 1479169806Ssimokawa return (-1); 1480169806Ssimokawa 1481169806Ssimokawa /* XXX check CRC */ 1482169806Ssimokawa 1483169806Ssimokawa off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1); 1484169806Ssimokawa if (fwdev->rommax < off) 1485169806Ssimokawa fwdev->rommax = off; 1486169806Ssimokawa 1487169806Ssimokawa if (recur == 0) 1488169806Ssimokawa return (0); 1489169806Ssimokawa 1490272214Skan for (i = 0; i < dir->crc_len; i++, offset += sizeof(uint32_t)) { 1491171302Ssimokawa if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D) 1492169806Ssimokawa recur = 1; 1493171302Ssimokawa else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L) 1494169806Ssimokawa recur = 0; 1495169806Ssimokawa else 1496129274Sdfr continue; 1497169806Ssimokawa 1498169806Ssimokawa off = offset + reg[i].val * sizeof(uint32_t); 1499169806Ssimokawa if (off > CROMSIZE) { 1500169806Ssimokawa printf("%s: invalid offset %d\n", __FUNCTION__, off); 1501272214Skan return (-1); 1502169806Ssimokawa } 1503169806Ssimokawa err = fw_explore_csrblock(fwdev, off, recur); 1504169806Ssimokawa if (err) 1505169806Ssimokawa return (-1); 1506129274Sdfr } 1507169806Ssimokawa return (0); 1508129274Sdfr} 1509129274Sdfr 1510169806Ssimokawastatic int 1511169806Ssimokawafw_explore_node(struct fw_device *dfwdev) 1512103285Sikob{ 1513169806Ssimokawa struct firewire_comm *fc; 1514110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1515169806Ssimokawa uint32_t *csr; 1516169806Ssimokawa struct csrhdr *hdr; 1517169806Ssimokawa struct bus_info *binfo; 1518188704Ssbruno int err, node; 1519188704Ssbruno uint32_t speed_test = 0; 1520103285Sikob 1521169806Ssimokawa fc = dfwdev->fc; 1522169806Ssimokawa csr = dfwdev->csrrom; 1523169806Ssimokawa node = dfwdev->dst; 1524103285Sikob 1525169806Ssimokawa /* First quad */ 1526169806Ssimokawa err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1); 1527188704Ssbruno if (err) { 1528188704Ssbruno dfwdev->status = FWDEVINVAL; 1529169806Ssimokawa return (-1); 1530188704Ssbruno } 1531169806Ssimokawa hdr = (struct csrhdr *)&csr[0]; 1532169806Ssimokawa if (hdr->info_len != 4) { 1533169806Ssimokawa if (firewire_debug) 1534272214Skan device_printf(fc->bdev, 1535272214Skan "%s: node%d: wrong bus info len(%d)\n", 1536188722Ssbruno __func__, node, hdr->info_len); 1537188722Ssbruno dfwdev->status = FWDEVINVAL; 1538169806Ssimokawa return (-1); 1539169806Ssimokawa } 1540103285Sikob 1541169806Ssimokawa /* bus info */ 1542169806Ssimokawa err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4); 1543188722Ssbruno if (err) { 1544188722Ssbruno dfwdev->status = FWDEVINVAL; 1545169806Ssimokawa return (-1); 1546188722Ssbruno } 1547169806Ssimokawa binfo = (struct bus_info *)&csr[1]; 1548169806Ssimokawa if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) { 1549188722Ssbruno dfwdev->status = FWDEVINVAL; 1550169806Ssimokawa return (-1); 1551103285Sikob } 1552188704Ssbruno 1553188704Ssbruno if (firewire_debug) 1554188704Ssbruno device_printf(fc->bdev, "%s: node(%d) BUS INFO BLOCK:\n" 1555272214Skan "irmc(%d) cmc(%d) isc(%d) bmc(%d) pmc(%d) " 1556272214Skan "cyc_clk_acc(%d) max_rec(%d) max_rom(%d) " 1557272214Skan "generation(%d) link_spd(%d)\n", 1558272214Skan __func__, node, 1559272214Skan binfo->irmc, binfo->cmc, binfo->isc, 1560272214Skan binfo->bmc, binfo->pmc, binfo->cyc_clk_acc, 1561272214Skan binfo->max_rec, binfo->max_rom, 1562272214Skan binfo->generation, binfo->link_spd); 1563188704Ssbruno 1564169806Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1565169806Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64)) 1566169806Ssimokawa break; 1567169806Ssimokawa if (fwdev == NULL) { 1568169806Ssimokawa /* new device */ 1569169806Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, 1570272214Skan M_NOWAIT | M_ZERO); 1571169806Ssimokawa if (fwdev == NULL) { 1572188722Ssbruno device_printf(fc->bdev, "%s: node%d: no memory\n", 1573188722Ssbruno __func__, node); 1574169806Ssimokawa return (-1); 1575103285Sikob } 1576106810Ssimokawa fwdev->fc = fc; 1577169806Ssimokawa fwdev->eui = binfo->eui64; 1578189928Ssbruno fwdev->dst = dfwdev->dst; 1579189928Ssbruno fwdev->maxrec = dfwdev->maxrec; 1580189928Ssbruno fwdev->status = dfwdev->status; 1581189928Ssbruno 1582188704Ssbruno /* 1583188704Ssbruno * Pre-1394a-2000 didn't have link_spd in 1584272214Skan * the Bus Info block, so try and use the 1585188704Ssbruno * speed map value. 1586188704Ssbruno * 1394a-2000 compliant devices only use 1587188704Ssbruno * the Bus Info Block link spd value, so 1588298955Spfg * ignore the speed map altogether. SWB 1589188704Ssbruno */ 1590272214Skan if (binfo->link_spd == FWSPD_S100 /* 0 */) { 1591189928Ssbruno device_printf(fc->bdev, "%s: " 1592272214Skan "Pre 1394a-2000 detected\n", __func__); 1593188704Ssbruno fwdev->speed = fc->speed_map->speed[fc->nodeid][node]; 1594188704Ssbruno } else 1595188704Ssbruno fwdev->speed = binfo->link_spd; 1596188704Ssbruno /* 1597188704Ssbruno * Test this speed with a read to the CSRROM. 1598188704Ssbruno * If it fails, slow down the speed and retry. 1599188704Ssbruno */ 1600189928Ssbruno while (fwdev->speed > FWSPD_S100 /* 0 */) { 1601188704Ssbruno err = fw_explore_read_quads(fwdev, CSRROMOFF, 1602272214Skan &speed_test, 1); 1603189928Ssbruno if (err) { 1604272214Skan device_printf(fc->bdev, 1605272214Skan "%s: fwdev->speed(%s) decremented due to negotiation\n", 1606272214Skan __func__, linkspeed[fwdev->speed]); 1607188704Ssbruno fwdev->speed--; 1608189928Ssbruno } else 1609188704Ssbruno break; 1610272214Skan 1611188704Ssbruno } 1612190792Ssbruno 1613190792Ssbruno /* 1614272214Skan * If the fwdev is not found in the 1615190792Ssbruno * fc->devices TAILQ, then we will add it. 1616190792Ssbruno */ 1617110179Ssimokawa pfwdev = NULL; 1618110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1619110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1620169806Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1621169806Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1622110179Ssimokawa break; 1623110179Ssimokawa pfwdev = tfwdev; 1624103285Sikob } 1625110179Ssimokawa if (pfwdev == NULL) 1626110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1627110179Ssimokawa else 1628110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1629189928Ssbruno } else { 1630189928Ssbruno fwdev->dst = node; 1631189928Ssbruno fwdev->status = FWDEVINIT; 1632189928Ssbruno /* unchanged ? */ 1633189928Ssbruno if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) { 1634189928Ssbruno if (firewire_debug) 1635272214Skan device_printf(fc->dev, 1636272214Skan "node%d: crom unchanged\n", node); 1637189928Ssbruno return (0); 1638189928Ssbruno } 1639103285Sikob } 1640103285Sikob 1641169806Ssimokawa bzero(&fwdev->csrrom[0], CROMSIZE); 1642106790Ssimokawa 1643169806Ssimokawa /* copy first quad and bus info block */ 1644169806Ssimokawa bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5); 1645169806Ssimokawa fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4; 1646103285Sikob 1647169806Ssimokawa err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */ 1648113584Ssimokawa 1649169806Ssimokawa if (err) { 1650189928Ssbruno if (firewire_debug) 1651189928Ssbruno device_printf(fc->dev, "%s: explore csrblock failed err(%d)\n", 1652189928Ssbruno __func__, err); 1653169806Ssimokawa fwdev->status = FWDEVINVAL; 1654169806Ssimokawa fwdev->csrrom[0] = 0; 1655103285Sikob } 1656169806Ssimokawa return (err); 1657103285Sikob 1658103285Sikob} 1659106790Ssimokawa 1660103285Sikob/* 1661169806Ssimokawa * Find the self_id packet for a node, ignoring sequels. 1662103285Sikob */ 1663169806Ssimokawastatic union fw_self_id * 1664169806Ssimokawafw_find_self_id(struct firewire_comm *fc, int node) 1665106790Ssimokawa{ 1666169806Ssimokawa uint32_t i; 1667169806Ssimokawa union fw_self_id *s; 1668103285Sikob 1669169806Ssimokawa for (i = 0; i < fc->topology_map->self_id_count; i++) { 1670169806Ssimokawa s = &fc->topology_map->self_id[i]; 1671169806Ssimokawa if (s->p0.sequel) 1672169806Ssimokawa continue; 1673169806Ssimokawa if (s->p0.phy_id == node) 1674169806Ssimokawa return s; 1675110577Ssimokawa } 1676169806Ssimokawa return 0; 1677169806Ssimokawa} 1678110577Ssimokawa 1679169806Ssimokawastatic void 1680169806Ssimokawafw_explore(struct firewire_comm *fc) 1681169806Ssimokawa{ 1682169806Ssimokawa int node, err, s, i, todo, todo2, trys; 1683169806Ssimokawa char nodes[63]; 1684169806Ssimokawa struct fw_device dfwdev; 1685169829Ssimokawa union fw_self_id *fwsid; 1686110577Ssimokawa 1687169806Ssimokawa todo = 0; 1688169806Ssimokawa /* setup dummy fwdev */ 1689169806Ssimokawa dfwdev.fc = fc; 1690169806Ssimokawa dfwdev.speed = 0; 1691169806Ssimokawa dfwdev.maxrec = 8; /* 512 */ 1692169806Ssimokawa dfwdev.status = FWDEVINIT; 1693103285Sikob 1694272214Skan for (node = 0; node <= fc->max_node; node++) { 1695169806Ssimokawa /* We don't probe myself and linkdown nodes */ 1696188722Ssbruno if (node == fc->nodeid) { 1697188722Ssbruno if (firewire_debug) 1698188722Ssbruno device_printf(fc->bdev, "%s:" 1699272214Skan "found myself node(%d) fc->nodeid(%d) fc->max_node(%d)\n", 1700272214Skan __func__, node, fc->nodeid, fc->max_node); 1701169806Ssimokawa continue; 1702188722Ssbruno } else if (firewire_debug) { 1703188722Ssbruno device_printf(fc->bdev, "%s:" 1704272214Skan "node(%d) fc->max_node(%d) found\n", 1705272214Skan __func__, node, fc->max_node); 1706188722Ssbruno } 1707169829Ssimokawa fwsid = fw_find_self_id(fc, node); 1708169829Ssimokawa if (!fwsid || !fwsid->p0.link_active) { 1709169806Ssimokawa if (firewire_debug) 1710272214Skan device_printf(fc->bdev, 1711272214Skan "%s: node%d: link down\n", 1712272214Skan __func__, node); 1713169806Ssimokawa continue; 1714103285Sikob } 1715169806Ssimokawa nodes[todo++] = node; 1716103285Sikob } 1717103285Sikob 1718169806Ssimokawa s = splfw(); 1719272214Skan for (trys = 0; todo > 0 && trys < 3; trys++) { 1720169806Ssimokawa todo2 = 0; 1721272214Skan for (i = 0; i < todo; i++) { 1722169806Ssimokawa dfwdev.dst = nodes[i]; 1723169806Ssimokawa err = fw_explore_node(&dfwdev); 1724169806Ssimokawa if (err) 1725169806Ssimokawa nodes[todo2++] = nodes[i]; 1726169806Ssimokawa if (firewire_debug) 1727272214Skan device_printf(fc->bdev, 1728272214Skan "%s: node %d, err = %d\n", 1729272214Skan __func__, node, err); 1730103285Sikob } 1731169806Ssimokawa todo = todo2; 1732103285Sikob } 1733169806Ssimokawa splx(s); 1734169806Ssimokawa} 1735169806Ssimokawa 1736169806Ssimokawastatic void 1737169806Ssimokawafw_bus_probe_thread(void *arg) 1738169806Ssimokawa{ 1739169806Ssimokawa struct firewire_comm *fc; 1740169806Ssimokawa 1741272214Skan fc = arg; 1742169806Ssimokawa 1743170374Ssimokawa mtx_lock(&fc->wait_lock); 1744170374Ssimokawa while (fc->status != FWBUSDETACH) { 1745169806Ssimokawa if (fc->status == FWBUSEXPLORE) { 1746170374Ssimokawa mtx_unlock(&fc->wait_lock); 1747169806Ssimokawa fw_explore(fc); 1748169806Ssimokawa fc->status = FWBUSEXPDONE; 1749169806Ssimokawa if (firewire_debug) 1750169806Ssimokawa printf("bus_explore done\n"); 1751169806Ssimokawa fw_attach_dev(fc); 1752170374Ssimokawa mtx_lock(&fc->wait_lock); 1753170374Ssimokawa } 1754170374Ssimokawa msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0); 1755127468Ssimokawa } 1756170374Ssimokawa mtx_unlock(&fc->wait_lock); 1757172836Sjulian kproc_exit(0); 1758103285Sikob} 1759103285Sikob 1760103285Sikob/* 1761103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1762103285Sikob */ 1763106815Ssimokawastatic void 1764106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1765103285Sikob{ 1766120850Ssimokawa struct fw_device *fwdev, *next; 1767103285Sikob int i, err; 1768103285Sikob device_t *devlistp; 1769103285Sikob int devcnt; 1770103285Sikob struct firewire_dev_comm *fdc; 1771103285Sikob 1772120850Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1773120850Ssimokawa next = STAILQ_NEXT(fwdev, link); 1774120850Ssimokawa if (fwdev->status == FWDEVINIT) { 1775114069Ssimokawa fwdev->status = FWDEVATTACHED; 1776120850Ssimokawa } else if (fwdev->status == FWDEVINVAL) { 1777272214Skan fwdev->rcnt++; 1778188722Ssbruno if (firewire_debug) 1779188722Ssbruno device_printf(fc->bdev, "%s:" 1780272214Skan "fwdev->rcnt(%d), hold_count(%d)\n", 1781272214Skan __func__, fwdev->rcnt, hold_count); 1782120850Ssimokawa if (fwdev->rcnt > hold_count) { 1783120850Ssimokawa /* 1784120850Ssimokawa * Remove devices which have not been seen 1785120850Ssimokawa * for a while. 1786120850Ssimokawa */ 1787120850Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, 1788120850Ssimokawa link); 1789120850Ssimokawa free(fwdev, M_FW); 1790120850Ssimokawa } 1791120850Ssimokawa } 1792120850Ssimokawa } 1793103285Sikob 1794108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1795272214Skan if (err == 0) { 1796272214Skan for (i = 0; i < devcnt; i++) { 1797272214Skan if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1798188722Ssbruno fdc = device_get_softc(devlistp[i]); 1799188722Ssbruno if (fdc->post_explore != NULL) 1800188722Ssbruno fdc->post_explore(fdc); 1801188722Ssbruno } 1802103285Sikob } 1803188722Ssbruno free(devlistp, M_TEMP); 1804103285Sikob } 1805103285Sikob 1806103285Sikob return; 1807103285Sikob} 1808106815Ssimokawa 1809103285Sikob/* 1810129541Sdfr * To allocate unique transaction label. 1811103285Sikob */ 1812106815Ssimokawastatic int 1813106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1814103285Sikob{ 1815171457Ssimokawa u_int dst, new_tlabel; 1816169119Ssimokawa struct fw_xfer *txfer; 1817103285Sikob int s; 1818103285Sikob 1819171457Ssimokawa dst = xfer->send.hdr.mode.hdr.dst & 0x3f; 1820103285Sikob s = splfw(); 1821171513Ssimokawa mtx_lock(&fc->tlabel_lock); 1822171457Ssimokawa new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f; 1823171457Ssimokawa STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel) 1824171457Ssimokawa if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst) 1825272214Skan break; 1826272214Skan if (txfer == NULL) { 1827171457Ssimokawa fc->last_tlabel[dst] = new_tlabel; 1828171457Ssimokawa STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel); 1829171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1830171457Ssimokawa splx(s); 1831171457Ssimokawa xfer->tl = new_tlabel; 1832171457Ssimokawa xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2; 1833171457Ssimokawa if (firewire_debug > 1) 1834171457Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel); 1835171457Ssimokawa return (new_tlabel); 1836103285Sikob } 1837171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1838103285Sikob splx(s); 1839103285Sikob 1840130460Sdfr if (firewire_debug > 1) 1841130460Sdfr printf("fw_get_tlabel: no free tlabel\n"); 1842171457Ssimokawa return (-1); 1843103285Sikob} 1844106815Ssimokawa 1845113584Ssimokawastatic void 1846120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb) 1847113584Ssimokawa{ 1848120660Ssimokawa struct fw_pkt *pkt; 1849120660Ssimokawa u_char *p; 1850120660Ssimokawa struct tcode_info *tinfo; 1851120660Ssimokawa u_int res, i, len, plen; 1852113584Ssimokawa 1853169127Ssimokawa rb->xfer->recv.spd = rb->spd; 1854120660Ssimokawa 1855120660Ssimokawa pkt = (struct fw_pkt *)rb->vec->iov_base; 1856120660Ssimokawa tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 1857120660Ssimokawa 1858272214Skan /* Copy header */ 1859120660Ssimokawa p = (u_char *)&rb->xfer->recv.hdr; 1860120660Ssimokawa bcopy(rb->vec->iov_base, p, tinfo->hdr_len); 1861132771Skan rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len; 1862120660Ssimokawa rb->vec->iov_len -= tinfo->hdr_len; 1863120660Ssimokawa 1864120660Ssimokawa /* Copy payload */ 1865120660Ssimokawa p = (u_char *)rb->xfer->recv.payload; 1866120660Ssimokawa res = rb->xfer->recv.pay_len; 1867120660Ssimokawa 1868120660Ssimokawa /* special handling for RRESQ */ 1869120660Ssimokawa if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 1870129585Sdfr p != NULL && res >= sizeof(uint32_t)) { 1871129585Sdfr *(uint32_t *)p = pkt->mode.rresq.data; 1872129585Sdfr rb->xfer->recv.pay_len = sizeof(uint32_t); 1873120660Ssimokawa return; 1874120660Ssimokawa } 1875120660Ssimokawa 1876120660Ssimokawa if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 1877120660Ssimokawa return; 1878120660Ssimokawa 1879120660Ssimokawa plen = pkt->mode.rresb.len; 1880120660Ssimokawa 1881120660Ssimokawa for (i = 0; i < rb->nvec; i++, rb->vec++) { 1882120660Ssimokawa len = MIN(rb->vec->iov_len, plen); 1883113584Ssimokawa if (res < len) { 1884188722Ssbruno device_printf(rb->fc->bdev, "%s:" 1885188722Ssbruno " rcv buffer(%d) is %d bytes short.\n", 1886188722Ssbruno __func__, rb->xfer->recv.pay_len, len - res); 1887113584Ssimokawa len = res; 1888113584Ssimokawa } 1889120660Ssimokawa bcopy(rb->vec->iov_base, p, len); 1890113584Ssimokawa p += len; 1891113584Ssimokawa res -= len; 1892120660Ssimokawa plen -= len; 1893120660Ssimokawa if (res == 0 || plen == 0) 1894113584Ssimokawa break; 1895113584Ssimokawa } 1896120660Ssimokawa rb->xfer->recv.pay_len -= res; 1897113584Ssimokawa} 1898113584Ssimokawa 1899103285Sikob/* 1900129541Sdfr * Generic packet receiving process. 1901103285Sikob */ 1902106815Ssimokawavoid 1903120660Ssimokawafw_rcv(struct fw_rcv_buf *rb) 1904103285Sikob{ 1905103285Sikob struct fw_pkt *fp, *resfp; 1906103285Sikob struct fw_bind *bind; 1907169130Ssimokawa int tcode; 1908113584Ssimokawa int i, len, oldstate; 1909103285Sikob#if 0 1910103285Sikob { 1911129585Sdfr uint32_t *qld; 1912103285Sikob int i; 1913129585Sdfr qld = (uint32_t *)buf; 1914103285Sikob printf("spd %d len:%d\n", spd, len); 1915272214Skan for (i = 0; i <= len && i < 32; i+= 4) { 1916103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1917272214Skan if ((i % 16) == 15) printf("\n"); 1918103285Sikob } 1919272214Skan if ((i % 16) != 15) printf("\n"); 1920103285Sikob } 1921103285Sikob#endif 1922120660Ssimokawa fp = (struct fw_pkt *)rb->vec[0].iov_base; 1923113584Ssimokawa tcode = fp->mode.common.tcode; 1924113584Ssimokawa switch (tcode) { 1925103285Sikob case FWTCODE_WRES: 1926103285Sikob case FWTCODE_RRESQ: 1927103285Sikob case FWTCODE_RRESB: 1928103285Sikob case FWTCODE_LRES: 1929120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1930170374Ssimokawa fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode); 1931272214Skan if (rb->xfer == NULL) { 1932272214Skan device_printf(rb->fc->bdev, "%s: unknown response " 1933272214Skan "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n", 1934272214Skan __func__, 1935272214Skan tcode_str[tcode], tcode, 1936272214Skan fp->mode.hdr.src, 1937272214Skan fp->mode.hdr.tlrt >> 2, 1938272214Skan fp->mode.hdr.tlrt & 3, 1939272214Skan fp->mode.rresq.data); 1940170374Ssimokawa#if 0 1941103285Sikob printf("try ad-hoc work around!!\n"); 1942120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1943272214Skan (fp->mode.hdr.tlrt >> 2)^3); 1944120660Ssimokawa if (rb->xfer == NULL) { 1945103285Sikob printf("no use...\n"); 1946169131Ssimokawa return; 1947103285Sikob } 1948103285Sikob#else 1949169131Ssimokawa return; 1950103285Sikob#endif 1951103285Sikob } 1952120660Ssimokawa fw_rcv_copy(rb); 1953120660Ssimokawa if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 1954120660Ssimokawa rb->xfer->resp = EIO; 1955120660Ssimokawa else 1956120660Ssimokawa rb->xfer->resp = 0; 1957113584Ssimokawa /* make sure the packet is drained in AT queue */ 1958170374Ssimokawa oldstate = rb->xfer->flag; 1959170374Ssimokawa rb->xfer->flag = FWXF_RCVD; 1960113584Ssimokawa switch (oldstate) { 1961113584Ssimokawa case FWXF_SENT: 1962120660Ssimokawa fw_xfer_done(rb->xfer); 1963103285Sikob break; 1964113584Ssimokawa case FWXF_START: 1965119289Ssimokawa#if 0 1966113584Ssimokawa if (firewire_debug) 1967120660Ssimokawa printf("not sent yet tl=%x\n", rb->xfer->tl); 1968119289Ssimokawa#endif 1969113584Ssimokawa break; 1970103285Sikob default: 1971188722Ssbruno device_printf(rb->fc->bdev, "%s: " 1972272214Skan "unexpected flag 0x%02x\n", __func__, 1973272214Skan rb->xfer->flag); 1974103285Sikob } 1975113584Ssimokawa return; 1976103285Sikob case FWTCODE_WREQQ: 1977103285Sikob case FWTCODE_WREQB: 1978103285Sikob case FWTCODE_RREQQ: 1979103285Sikob case FWTCODE_RREQB: 1980103285Sikob case FWTCODE_LREQ: 1981120660Ssimokawa bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 1982272214Skan fp->mode.rreqq.dest_lo); 1983272214Skan if (bind == NULL) { 1984188722Ssbruno device_printf(rb->fc->bdev, "%s: " 1985272214Skan "Unknown service addr 0x%04x:0x%08x %s(%x)" 1986272214Skan " src=0x%x data=%x\n", 1987272214Skan __func__, 1988272214Skan fp->mode.wreqq.dest_hi, 1989272214Skan fp->mode.wreqq.dest_lo, 1990272214Skan tcode_str[tcode], tcode, 1991272214Skan fp->mode.hdr.src, 1992272214Skan ntohl(fp->mode.wreqq.data)); 1993188722Ssbruno 1994170425Ssimokawa if (rb->fc->status == FWBUSINIT) { 1995272214Skan device_printf(rb->fc->bdev, 1996272214Skan "%s: cannot respond(bus reset)!\n", 1997272214Skan __func__); 1998169131Ssimokawa return; 1999103285Sikob } 2000120660Ssimokawa rb->xfer = fw_xfer_alloc(M_FWXFER); 2001272214Skan if (rb->xfer == NULL) { 2002103285Sikob return; 2003103285Sikob } 2004120660Ssimokawa rb->xfer->send.spd = rb->spd; 2005120660Ssimokawa rb->xfer->send.pay_len = 0; 2006120660Ssimokawa resfp = &rb->xfer->send.hdr; 2007113584Ssimokawa switch (tcode) { 2008103285Sikob case FWTCODE_WREQQ: 2009103285Sikob case FWTCODE_WREQB: 2010103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 2011103285Sikob break; 2012103285Sikob case FWTCODE_RREQQ: 2013103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 2014103285Sikob break; 2015103285Sikob case FWTCODE_RREQB: 2016103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 2017103285Sikob break; 2018103285Sikob case FWTCODE_LREQ: 2019103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 2020103285Sikob break; 2021103285Sikob } 2022103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 2023103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 2024103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 2025120660Ssimokawa resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 2026103285Sikob resfp->mode.rresb.extcode = 0; 2027103285Sikob resfp->mode.rresb.len = 0; 2028103285Sikob/* 2029170374Ssimokawa rb->xfer->hand = fw_xferwake; 2030103285Sikob*/ 2031167632Ssimokawa rb->xfer->hand = fw_xfer_free; 2032272214Skan if (fw_asyreq(rb->fc, -1, rb->xfer)) 2033120660Ssimokawa fw_xfer_free(rb->xfer); 2034277510Swill return; 2035103285Sikob } 2036113584Ssimokawa len = 0; 2037272214Skan for (i = 0; i < rb->nvec; i++) 2038120660Ssimokawa len += rb->vec[i].iov_len; 2039169130Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 2040169130Ssimokawa if (rb->xfer == NULL) { 2041188722Ssbruno device_printf(rb->fc->bdev, "%s: " 2042272214Skan "Discard a packet for this bind.\n", __func__); 2043169131Ssimokawa return; 2044103285Sikob } 2045169130Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 2046169130Ssimokawa fw_rcv_copy(rb); 2047169130Ssimokawa rb->xfer->hand(rb->xfer); 2048169130Ssimokawa return; 2049120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */ 2050103285Sikob case FWTCODE_STREAM: 2051103285Sikob { 2052103285Sikob struct fw_xferq *xferq; 2053103285Sikob 2054120660Ssimokawa xferq = rb->fc->ir[sub]; 2055103285Sikob#if 0 2056103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 2057103285Sikob sub, len, off, spd); 2058103285Sikob#endif 2059272214Skan if (xferq->queued >= xferq->maxq) { 2060103285Sikob printf("receive queue is full\n"); 2061169131Ssimokawa return; 2062103285Sikob } 2063272214Skan /* XXX get xfer from xfer queue, we don't need copy for 2064113584Ssimokawa per packet mode */ 2065120660Ssimokawa rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 2066113584Ssimokawa vec[0].iov_len); 2067169131Ssimokawa if (rb->xfer == NULL) 2068169131Ssimokawa return; 2069120660Ssimokawa fw_rcv_copy(rb) 2070103285Sikob s = splfw(); 2071103285Sikob xferq->queued++; 2072120660Ssimokawa STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link); 2073103285Sikob splx(s); 2074120660Ssimokawa sc = device_get_softc(rb->fc->bdev); 2075103285Sikob if (SEL_WAITING(&xferq->rsel)) 2076122352Stanimura selwakeuppri(&xferq->rsel, FWPRI); 2077103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 2078103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 2079103285Sikob wakeup((caddr_t)xferq); 2080103285Sikob } 2081103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 2082103285Sikob xferq->hand(xferq); 2083103285Sikob } 2084103285Sikob return; 2085103285Sikob break; 2086103285Sikob } 2087120660Ssimokawa#endif 2088103285Sikob default: 2089188722Ssbruno device_printf(rb->fc->bdev,"%s: unknown tcode %d\n", 2090272214Skan __func__, tcode); 2091103285Sikob break; 2092103285Sikob } 2093103285Sikob} 2094106815Ssimokawa 2095103285Sikob/* 2096103285Sikob * Post process for Bus Manager election process. 2097103285Sikob */ 2098103285Sikobstatic void 2099103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 2100103285Sikob{ 2101103285Sikob struct firewire_comm *fc; 2102109422Ssimokawa int bmr; 2103103285Sikob 2104109422Ssimokawa if (xfer == NULL) 2105109422Ssimokawa return; 2106103285Sikob fc = xfer->fc; 2107109422Ssimokawa if (xfer->resp != 0) 2108103285Sikob goto error; 2109120660Ssimokawa if (xfer->recv.payload == NULL) 2110103285Sikob goto error; 2111120660Ssimokawa if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 2112103285Sikob goto error; 2113109422Ssimokawa 2114120660Ssimokawa bmr = ntohl(xfer->recv.payload[0]); 2115109422Ssimokawa if (bmr == 0x3f) 2116109422Ssimokawa bmr = fc->nodeid; 2117109422Ssimokawa 2118109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 2119120660Ssimokawa fw_xfer_free_buf(xfer); 2120114909Ssimokawa fw_bmr(fc); 2121114909Ssimokawa return; 2122114909Ssimokawa 2123103285Sikoberror: 2124114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 2125120660Ssimokawa fw_xfer_free_buf(xfer); 2126103285Sikob} 2127106815Ssimokawa 2128113584Ssimokawa 2129103285Sikob/* 2130103285Sikob * To candidate Bus Manager election process. 2131103285Sikob */ 2132110270Ssimokawastatic void 2133106815Ssimokawafw_try_bmr(void *arg) 2134103285Sikob{ 2135103285Sikob struct fw_xfer *xfer; 2136272214Skan struct firewire_comm *fc = arg; 2137103285Sikob struct fw_pkt *fp; 2138103285Sikob int err = 0; 2139103285Sikob 2140120660Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4); 2141272214Skan if (xfer == NULL) 2142103285Sikob return; 2143120660Ssimokawa xfer->send.spd = 0; 2144103285Sikob fc->status = FWBUSMGRELECT; 2145103285Sikob 2146120660Ssimokawa fp = &xfer->send.hdr; 2147113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 2148103285Sikob fp->mode.lreq.tlrt = 0; 2149103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 2150103285Sikob fp->mode.lreq.pri = 0; 2151103285Sikob fp->mode.lreq.src = 0; 2152113584Ssimokawa fp->mode.lreq.len = 8; 2153120660Ssimokawa fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 2154120660Ssimokawa fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 2155113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2156120660Ssimokawa xfer->send.payload[0] = htonl(0x3f); 2157120660Ssimokawa xfer->send.payload[1] = htonl(fc->nodeid); 2158167632Ssimokawa xfer->hand = fw_try_bmr_callback; 2159103285Sikob 2160103285Sikob err = fw_asyreq(fc, -1, xfer); 2161272214Skan if (err) { 2162120660Ssimokawa fw_xfer_free_buf(xfer); 2163103285Sikob return; 2164103285Sikob } 2165103285Sikob return; 2166103285Sikob} 2167106543Ssimokawa 2168106543Ssimokawa#ifdef FW_VMACCESS 2169103285Sikob/* 2170103285Sikob * Software implementation for physical memory block access. 2171272214Skan * XXX:Too slow, useful for debug purpose only. 2172103285Sikob */ 2173106815Ssimokawastatic void 2174272214Skanfw_vmaccess(struct fw_xfer *xfer) 2175272214Skan{ 2176103285Sikob struct fw_pkt *rfp, *sfp = NULL; 2177129585Sdfr uint32_t *ld = (uint32_t *)xfer->recv.buf; 2178103285Sikob 2179113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 2180272214Skan xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), 2181272214Skan ntohl(ld[3])); 2182272214Skan printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), 2183272214Skan ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2184272214Skan if (xfer->resp != 0) { 2185272214Skan fw_xfer_free(xfer); 2186103285Sikob return; 2187103285Sikob } 2188272214Skan if (xfer->recv.buf == NULL) { 2189272214Skan fw_xfer_free(xfer); 2190103285Sikob return; 2191103285Sikob } 2192103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 2193272214Skan switch (rfp->mode.hdr.tcode) { 2194103285Sikob /* XXX need fix for 64bit arch */ 2195103285Sikob case FWTCODE_WREQB: 2196110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2197103285Sikob xfer->send.len = 12; 2198103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2199103285Sikob bcopy(rfp->mode.wreqb.payload, 2200272214Skan (caddr_t)ntohl(rfp->mode.wreqb.dest_lo),s 2201272214Skan ntohs(rfp->mode.wreqb.len)); 2202103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2203103285Sikob sfp->mode.wres.rtcode = 0; 2204103285Sikob break; 2205103285Sikob case FWTCODE_WREQQ: 2206110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2207103285Sikob xfer->send.len = 12; 2208103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2209272214Skan *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = 2210272214Skan rfp->mode.wreqq.data; 2211103285Sikob sfp->mode.wres.rtcode = 0; 2212103285Sikob break; 2213103285Sikob case FWTCODE_RREQB: 2214272214Skan xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, 2215272214Skan M_FW, M_NOWAIT); 2216103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2217103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2218103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2219272214Skan sfp->mode.rresb.payload, 2220272214Skan ntohs(rfp->mode.rreqb.len)); 2221103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 2222103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 2223103285Sikob sfp->mode.rresb.rtcode = 0; 2224103285Sikob sfp->mode.rresb.extcode = 0; 2225103285Sikob break; 2226103285Sikob case FWTCODE_RREQQ: 2227110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2228103285Sikob xfer->send.len = 16; 2229103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2230272214Skan sfp->mode.rresq.data = 2231272214Skan *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2232103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 2233103285Sikob sfp->mode.rresb.rtcode = 0; 2234103285Sikob break; 2235103285Sikob default: 2236272214Skan fw_xfer_free(xfer); 2237103285Sikob return; 2238103285Sikob } 2239103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2240103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2241167632Ssimokawa xfer->hand = fw_xfer_free; 2242103285Sikob 2243103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2244103285Sikob sfp->mode.hdr.pri = 0; 2245103285Sikob 2246103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2247103285Sikob/**/ 2248103285Sikob return; 2249103285Sikob} 2250272214Skan#endif 2251106543Ssimokawa 2252103285Sikob/* 2253103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2254103285Sikob */ 2255129585Sdfruint16_t 2256272214Skanfw_crc16(uint32_t *ptr, uint32_t len) 2257272214Skan{ 2258129585Sdfr uint32_t i, sum, crc = 0; 2259103285Sikob int shift; 2260103285Sikob len = (len + 3) & ~3; 2261272214Skan for (i = 0; i < len; i += 4) { 2262272214Skan for (shift = 28; shift >= 0; shift -= 4) { 2263103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2264272214Skan crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum; 2265103285Sikob } 2266103285Sikob crc &= 0xffff; 2267103285Sikob } 2268272214Skan return ((uint16_t) crc); 2269103285Sikob} 2270106815Ssimokawa 2271188722Ssbruno/* 2272188722Ssbruno * Find the root node, if it is not 2273188722Ssbruno * Cycle Master Capable, then we should 2274188722Ssbruno * override this and become the Cycle 2275188722Ssbruno * Master 2276188722Ssbruno */ 2277110270Ssimokawastatic int 2278110072Ssimokawafw_bmr(struct firewire_comm *fc) 2279110072Ssimokawa{ 2280110072Ssimokawa struct fw_device fwdev; 2281113584Ssimokawa union fw_self_id *self_id; 2282110072Ssimokawa int cmstr; 2283129585Sdfr uint32_t quad; 2284110072Ssimokawa 2285113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2286129274Sdfr self_id = fw_find_self_id(fc, fc->max_node); 2287113584Ssimokawa if (fc->max_node > 0) { 2288114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2289114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2290113584Ssimokawa cmstr = fc->max_node; 2291114909Ssimokawa else { 2292114909Ssimokawa device_printf(fc->bdev, 2293272214Skan "root node is not cycle master capable\n"); 2294114909Ssimokawa /* XXX shall we be the cycle master? */ 2295113584Ssimokawa cmstr = fc->nodeid; 2296114909Ssimokawa /* XXX need bus reset */ 2297114909Ssimokawa } 2298113584Ssimokawa } else 2299113584Ssimokawa cmstr = -1; 2300114909Ssimokawa 2301188722Ssbruno device_printf(fc->bdev, "bus manager %d %s\n", 2302188722Ssbruno CSRARC(fc, BUS_MGR_ID), 2303188722Ssbruno (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) ? "(me)" : ""); 2304272214Skan if (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2305114909Ssimokawa /* We are not the bus manager */ 2306272214Skan return (0); 2307114909Ssimokawa } 2308114909Ssimokawa 2309114909Ssimokawa /* Optimize gapcount */ 2310272214Skan if (fc->max_hop <= MAX_GAPHOP) 2311113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2312110072Ssimokawa /* If we are the cycle master, nothing to do */ 2313113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2314110072Ssimokawa return 0; 2315110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2316110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2317110072Ssimokawa fwdev.fc = fc; 2318110072Ssimokawa fwdev.dst = cmstr; 2319110072Ssimokawa fwdev.speed = 0; 2320110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2321110072Ssimokawa fwdev.status = FWDEVINIT; 2322110072Ssimokawa /* Set cmstr bit on the cycle master */ 2323120660Ssimokawa quad = htonl(1 << 8); 2324110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2325272214Skan 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free); 2326110072Ssimokawa 2327110072Ssimokawa return 0; 2328110072Ssimokawa} 2329110072Ssimokawa 2330170374Ssimokawaint 2331170374Ssimokawafw_open_isodma(struct firewire_comm *fc, int tx) 2332170374Ssimokawa{ 2333170374Ssimokawa struct fw_xferq **xferqa; 2334170374Ssimokawa struct fw_xferq *xferq; 2335170374Ssimokawa int i; 2336170374Ssimokawa 2337170374Ssimokawa if (tx) 2338170374Ssimokawa xferqa = &fc->it[0]; 2339170374Ssimokawa else 2340170374Ssimokawa xferqa = &fc->ir[0]; 2341170374Ssimokawa 2342170374Ssimokawa FW_GLOCK(fc); 2343272214Skan for (i = 0; i < fc->nisodma; i++) { 2344170374Ssimokawa xferq = xferqa[i]; 2345170374Ssimokawa if ((xferq->flag & FWXFERQ_OPEN) == 0) { 2346170374Ssimokawa xferq->flag |= FWXFERQ_OPEN; 2347170374Ssimokawa break; 2348170374Ssimokawa } 2349170374Ssimokawa } 2350170374Ssimokawa if (i == fc->nisodma) { 2351170374Ssimokawa printf("no free dma channel (tx=%d)\n", tx); 2352170374Ssimokawa i = -1; 2353170374Ssimokawa } 2354170374Ssimokawa FW_GUNLOCK(fc); 2355170374Ssimokawa return (i); 2356170374Ssimokawa} 2357170374Ssimokawa 2358118455Ssimokawastatic int 2359118455Ssimokawafw_modevent(module_t mode, int type, void *data) 2360118455Ssimokawa{ 2361118455Ssimokawa int err = 0; 2362118455Ssimokawa static eventhandler_tag fwdev_ehtag = NULL; 2363118455Ssimokawa 2364118455Ssimokawa switch (type) { 2365118455Ssimokawa case MOD_LOAD: 2366118455Ssimokawa fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone, 2367272214Skan fwdev_clone, 0, 1000); 2368118455Ssimokawa break; 2369118455Ssimokawa case MOD_UNLOAD: 2370118455Ssimokawa if (fwdev_ehtag != NULL) 2371118455Ssimokawa EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag); 2372118455Ssimokawa break; 2373118455Ssimokawa case MOD_SHUTDOWN: 2374118455Ssimokawa break; 2375132199Sphk default: 2376132199Sphk return (EOPNOTSUPP); 2377118455Ssimokawa } 2378118455Ssimokawa return (err); 2379118455Ssimokawa} 2380118455Ssimokawa 2381118455Ssimokawa 2382272214SkanDRIVER_MODULE(firewire, fwohci, firewire_driver, firewire_devclass, 2383272214Skan fw_modevent,0); 2384103285SikobMODULE_VERSION(firewire, 1); 2385