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$"); 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> 51170374Ssimokawa 52127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 53117067Ssimokawa#include <machine/clock.h> /* for DELAY() */ 54117067Ssimokawa#endif 55103285Sikob 56103285Sikob#include <sys/bus.h> /* used by smbus and newbus */ 57113584Ssimokawa#include <machine/bus.h> 58103285Sikob 59127468Ssimokawa#ifdef __DragonFly__ 60127468Ssimokawa#include "firewire.h" 61127468Ssimokawa#include "firewirereg.h" 62127468Ssimokawa#include "fwmem.h" 63127468Ssimokawa#include "iec13213.h" 64127468Ssimokawa#include "iec68113.h" 65127468Ssimokawa#else 66103285Sikob#include <dev/firewire/firewire.h> 67103285Sikob#include <dev/firewire/firewirereg.h> 68110072Ssimokawa#include <dev/firewire/fwmem.h> 69103285Sikob#include <dev/firewire/iec13213.h> 70103285Sikob#include <dev/firewire/iec68113.h> 71127468Ssimokawa#endif 72103285Sikob 73116376Ssimokawastruct crom_src_buf { 74116376Ssimokawa struct crom_src src; 75116376Ssimokawa struct crom_chunk root; 76116376Ssimokawa struct crom_chunk vendor; 77116376Ssimokawa struct crom_chunk hw; 78116376Ssimokawa}; 79116376Ssimokawa 80188704Ssbrunoint firewire_debug=0, try_bmr=1, hold_count=0; 81103285SikobSYSCTL_INT(_debug, OID_AUTO, firewire_debug, CTLFLAG_RW, &firewire_debug, 0, 82108281Ssimokawa "FireWire driver debug flag"); 83109736SsimokawaSYSCTL_NODE(_hw, OID_AUTO, firewire, CTLFLAG_RD, 0, "FireWire Subsystem"); 84109736SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, try_bmr, CTLFLAG_RW, &try_bmr, 0, 85109736Ssimokawa "Try to be a bus manager"); 86120850SsimokawaSYSCTL_INT(_hw_firewire, OID_AUTO, hold_count, CTLFLAG_RW, &hold_count, 0, 87120850Ssimokawa "Number of count of bus resets for removing lost device information"); 88103285Sikob 89110195SsimokawaMALLOC_DEFINE(M_FW, "firewire", "FireWire"); 90110269SsimokawaMALLOC_DEFINE(M_FWXFER, "fw_xfer", "XFER/FireWire"); 91110195Ssimokawa 92103285Sikob#define FW_MAXASYRTY 4 93103285Sikob 94103285Sikobdevclass_t firewire_devclass; 95103285Sikob 96125238Ssimokawastatic void firewire_identify (driver_t *, device_t); 97125238Ssimokawastatic int firewire_probe (device_t); 98124169Ssimokawastatic int firewire_attach (device_t); 99124169Ssimokawastatic int firewire_detach (device_t); 100124169Ssimokawastatic int firewire_resume (device_t); 101170374Ssimokawastatic void firewire_xfer_timeout(void *, int); 102103285Sikob#if 0 103124169Ssimokawastatic int firewire_shutdown (device_t); 104103285Sikob#endif 105212413Savgstatic device_t firewire_add_child(device_t, u_int, const char *, int); 106124169Ssimokawastatic void fw_try_bmr (void *); 107124169Ssimokawastatic void fw_try_bmr_callback (struct fw_xfer *); 108124169Ssimokawastatic void fw_asystart (struct fw_xfer *); 109124169Ssimokawastatic int fw_get_tlabel (struct firewire_comm *, struct fw_xfer *); 110124169Ssimokawastatic void fw_bus_probe (struct firewire_comm *); 111124169Ssimokawastatic void fw_attach_dev (struct firewire_comm *); 112169806Ssimokawastatic void fw_bus_probe_thread(void *); 113106543Ssimokawa#ifdef FW_VMACCESS 114124169Ssimokawastatic void fw_vmaccess (struct fw_xfer *); 115106543Ssimokawa#endif 116124169Ssimokawastatic int fw_bmr (struct firewire_comm *); 117170374Ssimokawastatic void fw_dump_hdr(struct fw_pkt *, char *); 118103285Sikob 119103285Sikobstatic device_method_t firewire_methods[] = { 120103285Sikob /* Device interface */ 121125238Ssimokawa DEVMETHOD(device_identify, firewire_identify), 122125238Ssimokawa DEVMETHOD(device_probe, firewire_probe), 123103285Sikob DEVMETHOD(device_attach, firewire_attach), 124103285Sikob DEVMETHOD(device_detach, firewire_detach), 125108642Ssimokawa DEVMETHOD(device_suspend, bus_generic_suspend), 126116978Ssimokawa DEVMETHOD(device_resume, firewire_resume), 127103285Sikob DEVMETHOD(device_shutdown, bus_generic_shutdown), 128103285Sikob 129103285Sikob /* Bus interface */ 130103285Sikob DEVMETHOD(bus_add_child, firewire_add_child), 131103285Sikob 132227843Smarius DEVMETHOD_END 133103285Sikob}; 134124251Ssimokawachar *linkspeed[] = { 135124251Ssimokawa "S100", "S200", "S400", "S800", 136124251Ssimokawa "S1600", "S3200", "undef", "undef" 137124251Ssimokawa}; 138103285Sikob 139124251Ssimokawastatic char *tcode_str[] = { 140124251Ssimokawa "WREQQ", "WREQB", "WRES", "undef", 141124251Ssimokawa "RREQQ", "RREQB", "RRESQ", "RRESB", 142124251Ssimokawa "CYCS", "LREQ", "STREAM", "LRES", 143124251Ssimokawa "undef", "undef", "PHY", "undef" 144124251Ssimokawa}; 145124251Ssimokawa 146114909Ssimokawa/* IEEE-1394a Table C-2 Gap count as a function of hops*/ 147114909Ssimokawa#define MAX_GAPHOP 15 148114909Ssimokawau_int gap_cnt[] = { 5, 5, 7, 8, 10, 13, 16, 18, 149114909Ssimokawa 21, 24, 26, 29, 32, 35, 37, 40}; 150106813Ssimokawa 151103285Sikobstatic driver_t firewire_driver = { 152103285Sikob "firewire", 153103285Sikob firewire_methods, 154103285Sikob sizeof(struct firewire_softc), 155103285Sikob}; 156103285Sikob 157103285Sikob/* 158110072Ssimokawa * Lookup fwdev by node id. 159103285Sikob */ 160106810Ssimokawastruct fw_device * 161110072Ssimokawafw_noderesolve_nodeid(struct firewire_comm *fc, int dst) 162103285Sikob{ 163103285Sikob struct fw_device *fwdev; 164110072Ssimokawa int s; 165110072Ssimokawa 166110072Ssimokawa s = splfw(); 167110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 168120660Ssimokawa if (fwdev->dst == dst && fwdev->status != FWDEVINVAL) 169103285Sikob break; 170110072Ssimokawa splx(s); 171110072Ssimokawa 172106810Ssimokawa return fwdev; 173103285Sikob} 174106813Ssimokawa 175103285Sikob/* 176110072Ssimokawa * Lookup fwdev by EUI64. 177110072Ssimokawa */ 178110072Ssimokawastruct fw_device * 179110582Ssimokawafw_noderesolve_eui64(struct firewire_comm *fc, struct fw_eui64 *eui) 180110072Ssimokawa{ 181110072Ssimokawa struct fw_device *fwdev; 182110072Ssimokawa int s; 183110072Ssimokawa 184110072Ssimokawa s = splfw(); 185170374Ssimokawa FW_GLOCK(fc); 186110193Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 187110582Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, *eui)) 188110072Ssimokawa break; 189170374Ssimokawa FW_GUNLOCK(fc); 190110072Ssimokawa splx(s); 191110072Ssimokawa 192110072Ssimokawa if(fwdev == NULL) return NULL; 193110072Ssimokawa if(fwdev->status == FWDEVINVAL) return NULL; 194110072Ssimokawa return fwdev; 195110072Ssimokawa} 196110072Ssimokawa 197110072Ssimokawa/* 198103285Sikob * Async. request procedure for userland application. 199103285Sikob */ 200103285Sikobint 201103285Sikobfw_asyreq(struct firewire_comm *fc, int sub, struct fw_xfer *xfer) 202103285Sikob{ 203103285Sikob int err = 0; 204103285Sikob struct fw_xferq *xferq; 205170374Ssimokawa int len; 206103285Sikob struct fw_pkt *fp; 207103285Sikob int tcode; 208103285Sikob struct tcode_info *info; 209103285Sikob 210103285Sikob if(xfer == NULL) return EINVAL; 211167632Ssimokawa if(xfer->hand == NULL){ 212167632Ssimokawa printf("hand == NULL\n"); 213103285Sikob return EINVAL; 214103285Sikob } 215120660Ssimokawa fp = &xfer->send.hdr; 216103285Sikob 217103285Sikob tcode = fp->mode.common.tcode & 0xf; 218103285Sikob info = &fc->tcode[tcode]; 219103285Sikob if (info->flag == 0) { 220124251Ssimokawa printf("invalid tcode=%x\n", tcode); 221103285Sikob return EINVAL; 222103285Sikob } 223170425Ssimokawa 224170425Ssimokawa /* XXX allow bus explore packets only after bus rest */ 225170425Ssimokawa if ((fc->status < FWBUSEXPLORE) && 226170425Ssimokawa ((tcode != FWTCODE_RREQQ) || (fp->mode.rreqq.dest_hi != 0xffff) || 227170425Ssimokawa (fp->mode.rreqq.dest_lo < 0xf0000000) || 228170425Ssimokawa (fp->mode.rreqq.dest_lo >= 0xf0001000))) { 229170425Ssimokawa xfer->resp = EAGAIN; 230170425Ssimokawa xfer->flag = FWXF_BUSY; 231170425Ssimokawa return (EAGAIN); 232170425Ssimokawa } 233170425Ssimokawa 234103285Sikob if (info->flag & FWTI_REQ) 235103285Sikob xferq = fc->atq; 236103285Sikob else 237103285Sikob xferq = fc->ats; 238103285Sikob len = info->hdr_len; 239120660Ssimokawa if (xfer->send.pay_len > MAXREC(fc->maxrec)) { 240120660Ssimokawa printf("send.pay_len > maxrec\n"); 241120660Ssimokawa return EINVAL; 242120660Ssimokawa } 243103285Sikob if (info->flag & FWTI_BLOCK_STR) 244120660Ssimokawa len = fp->mode.stream.len; 245103285Sikob else if (info->flag & FWTI_BLOCK_ASY) 246120660Ssimokawa len = fp->mode.rresb.len; 247120660Ssimokawa else 248120660Ssimokawa len = 0; 249120660Ssimokawa if (len != xfer->send.pay_len){ 250124251Ssimokawa printf("len(%d) != send.pay_len(%d) %s(%x)\n", 251124251Ssimokawa len, xfer->send.pay_len, tcode_str[tcode], tcode); 252103285Sikob return EINVAL; 253103285Sikob } 254106790Ssimokawa 255103285Sikob if(xferq->start == NULL){ 256103285Sikob printf("xferq->start == NULL\n"); 257103285Sikob return EINVAL; 258103285Sikob } 259103285Sikob if(!(xferq->queued < xferq->maxq)){ 260108655Ssimokawa device_printf(fc->bdev, "Discard a packet (queued=%d)\n", 261108655Ssimokawa xferq->queued); 262170374Ssimokawa return EAGAIN; 263103285Sikob } 264103285Sikob 265170374Ssimokawa xfer->tl = -1; 266103285Sikob if (info->flag & FWTI_TLABEL) { 267170374Ssimokawa if (fw_get_tlabel(fc, xfer) < 0) 268130460Sdfr return EAGAIN; 269103285Sikob } 270103285Sikob 271103285Sikob xfer->resp = 0; 272103285Sikob xfer->fc = fc; 273103285Sikob xfer->q = xferq; 274103285Sikob 275103285Sikob fw_asystart(xfer); 276103285Sikob return err; 277103285Sikob} 278103285Sikob/* 279103285Sikob * Wakeup blocked process. 280103285Sikob */ 281103285Sikobvoid 282170374Ssimokawafw_xferwake(struct fw_xfer *xfer) 283170374Ssimokawa{ 284170374Ssimokawa struct mtx *lock = &xfer->fc->wait_lock; 285170374Ssimokawa 286170374Ssimokawa mtx_lock(lock); 287170374Ssimokawa xfer->flag |= FWXF_WAKE; 288170374Ssimokawa mtx_unlock(lock); 289170374Ssimokawa 290103285Sikob wakeup(xfer); 291103285Sikob return; 292103285Sikob} 293103285Sikob 294170374Ssimokawaint 295170374Ssimokawafw_xferwait(struct fw_xfer *xfer) 296170374Ssimokawa{ 297170374Ssimokawa struct mtx *lock = &xfer->fc->wait_lock; 298170374Ssimokawa int err = 0; 299170374Ssimokawa 300170374Ssimokawa mtx_lock(lock); 301170374Ssimokawa if ((xfer->flag & FWXF_WAKE) == 0) 302170374Ssimokawa err = msleep((void *)xfer, lock, PWAIT|PCATCH, "fw_xferwait", 0); 303170374Ssimokawa mtx_unlock(lock); 304170374Ssimokawa 305170374Ssimokawa return (err); 306170374Ssimokawa} 307170374Ssimokawa 308103285Sikob/* 309103285Sikob * Async. request with given xfer structure. 310103285Sikob */ 311106790Ssimokawastatic void 312106790Ssimokawafw_asystart(struct fw_xfer *xfer) 313106790Ssimokawa{ 314103285Sikob struct firewire_comm *fc = xfer->fc; 315103285Sikob int s; 316170374Ssimokawa s = splfw(); 317170374Ssimokawa /* Protect from interrupt/timeout */ 318170374Ssimokawa FW_GLOCK(fc); 319170374Ssimokawa xfer->flag = FWXF_INQ; 320103285Sikob STAILQ_INSERT_TAIL(&xfer->q->q, xfer, link); 321170374Ssimokawa#if 0 322103285Sikob xfer->q->queued ++; 323170374Ssimokawa#endif 324170374Ssimokawa FW_GUNLOCK(fc); 325103285Sikob splx(s); 326103285Sikob /* XXX just queue for mbuf */ 327103285Sikob if (xfer->mbuf == NULL) 328103285Sikob xfer->q->start(fc); 329103285Sikob return; 330103285Sikob} 331106790Ssimokawa 332125238Ssimokawastatic void 333125238Ssimokawafirewire_identify(driver_t *driver, device_t parent) 334125238Ssimokawa{ 335125238Ssimokawa BUS_ADD_CHILD(parent, 0, "firewire", -1); 336125238Ssimokawa} 337125238Ssimokawa 338103285Sikobstatic int 339125238Ssimokawafirewire_probe(device_t dev) 340103285Sikob{ 341108281Ssimokawa device_set_desc(dev, "IEEE1394(FireWire) bus"); 342125238Ssimokawa return (0); 343103285Sikob} 344106790Ssimokawa 345110577Ssimokawastatic void 346170374Ssimokawafirewire_xfer_timeout(void *arg, int pending) 347110577Ssimokawa{ 348170374Ssimokawa struct firewire_comm *fc = (struct firewire_comm *)arg; 349170374Ssimokawa struct fw_xfer *xfer, *txfer; 350110577Ssimokawa struct timeval tv; 351110577Ssimokawa struct timeval split_timeout; 352170374Ssimokawa STAILQ_HEAD(, fw_xfer) xfer_timeout; 353111040Ssimokawa int i, s; 354110577Ssimokawa 355120660Ssimokawa split_timeout.tv_sec = 0; 356120660Ssimokawa split_timeout.tv_usec = 200 * 1000; /* 200 msec */ 357110577Ssimokawa 358110577Ssimokawa microtime(&tv); 359110577Ssimokawa timevalsub(&tv, &split_timeout); 360170374Ssimokawa STAILQ_INIT(&xfer_timeout); 361110577Ssimokawa 362111040Ssimokawa s = splfw(); 363171513Ssimokawa mtx_lock(&fc->tlabel_lock); 364110577Ssimokawa for (i = 0; i < 0x40; i ++) { 365169119Ssimokawa while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 366170427Ssimokawa if ((xfer->flag & FWXF_SENT) == 0) 367170427Ssimokawa /* not sent yet */ 368170427Ssimokawa break; 369110577Ssimokawa if (timevalcmp(&xfer->tv, &tv, >)) 370110577Ssimokawa /* the rests are newer than this */ 371110577Ssimokawa break; 372110577Ssimokawa device_printf(fc->bdev, 373170374Ssimokawa "split transaction timeout: " 374170374Ssimokawa "tl=0x%x flag=0x%02x\n", i, xfer->flag); 375170374Ssimokawa fw_dump_hdr(&xfer->send.hdr, "send"); 376110577Ssimokawa xfer->resp = ETIMEDOUT; 377249291Swill xfer->tl = -1; 378170374Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel); 379170374Ssimokawa STAILQ_INSERT_TAIL(&xfer_timeout, xfer, tlabel); 380110577Ssimokawa } 381110577Ssimokawa } 382171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 383111040Ssimokawa splx(s); 384170374Ssimokawa fc->timeout(fc); 385170374Ssimokawa 386170374Ssimokawa STAILQ_FOREACH_SAFE(xfer, &xfer_timeout, tlabel, txfer) 387170374Ssimokawa xfer->hand(xfer); 388110577Ssimokawa} 389110577Ssimokawa 390170374Ssimokawa#define WATCHDOG_HZ 10 391110577Ssimokawastatic void 392110577Ssimokawafirewire_watchdog(void *arg) 393110577Ssimokawa{ 394110577Ssimokawa struct firewire_comm *fc; 395170374Ssimokawa static int watchdog_clock = 0; 396110577Ssimokawa 397110577Ssimokawa fc = (struct firewire_comm *)arg; 398121463Ssimokawa 399121463Ssimokawa /* 400121463Ssimokawa * At boot stage, the device interrupt is disabled and 401121463Ssimokawa * We encounter a timeout easily. To avoid this, 402121463Ssimokawa * ignore clock interrupt for a while. 403121463Ssimokawa */ 404170374Ssimokawa if (watchdog_clock > WATCHDOG_HZ * 15) 405170374Ssimokawa taskqueue_enqueue(fc->taskqueue, &fc->task_timeout); 406170374Ssimokawa else 407170374Ssimokawa watchdog_clock ++; 408121463Ssimokawa 409170374Ssimokawa callout_reset(&fc->timeout_callout, hz / WATCHDOG_HZ, 410110577Ssimokawa (void *)firewire_watchdog, (void *)fc); 411110577Ssimokawa} 412110577Ssimokawa 413103285Sikob/* 414103285Sikob * The attach routine. 415103285Sikob */ 416103285Sikobstatic int 417118455Ssimokawafirewire_attach(device_t dev) 418103285Sikob{ 419118455Ssimokawa int unit; 420103285Sikob struct firewire_softc *sc = device_get_softc(dev); 421103285Sikob device_t pa = device_get_parent(dev); 422103285Sikob struct firewire_comm *fc; 423103285Sikob 424103285Sikob fc = (struct firewire_comm *)device_get_softc(pa); 425103285Sikob sc->fc = fc; 426116978Ssimokawa fc->status = FWBUSNOTREADY; 427103285Sikob 428118455Ssimokawa unit = device_get_unit(dev); 429118455Ssimokawa if( fc->nisodma > FWMAXNDMA) fc->nisodma = FWMAXNDMA; 430103285Sikob 431118455Ssimokawa fwdev_makedev(sc); 432118455Ssimokawa 433187993Ssbruno fc->crom_src_buf = (struct crom_src_buf *)malloc( 434187993Ssbruno sizeof(struct crom_src_buf), 435187993Ssbruno M_FW, M_NOWAIT | M_ZERO); 436187993Ssbruno if (fc->crom_src_buf == NULL) { 437187993Ssbruno device_printf(fc->dev, "%s: Malloc Failure crom src buff\n", __func__); 438187993Ssbruno return ENOMEM; 439187993Ssbruno } 440187993Ssbruno fc->topology_map = (struct fw_topology_map *)malloc( 441187993Ssbruno sizeof(struct fw_topology_map), 442187993Ssbruno M_FW, M_NOWAIT | M_ZERO); 443187993Ssbruno if (fc->topology_map == NULL) { 444187993Ssbruno device_printf(fc->dev, "%s: Malloc Failure topology map\n", __func__); 445187993Ssbruno free(fc->crom_src_buf, M_FW); 446187993Ssbruno return ENOMEM; 447187993Ssbruno } 448187993Ssbruno fc->speed_map = (struct fw_speed_map *)malloc( 449187993Ssbruno sizeof(struct fw_speed_map), 450187993Ssbruno M_FW, M_NOWAIT | M_ZERO); 451187993Ssbruno if (fc->speed_map == NULL) { 452187993Ssbruno device_printf(fc->dev, "%s: Malloc Failure speed map\n", __func__); 453187993Ssbruno free(fc->crom_src_buf, M_FW); 454187993Ssbruno free(fc->topology_map, M_FW); 455187993Ssbruno return ENOMEM; 456187993Ssbruno } 457187993Ssbruno 458170374Ssimokawa mtx_init(&fc->wait_lock, "fwwait", NULL, MTX_DEF); 459171513Ssimokawa mtx_init(&fc->tlabel_lock, "fwtlabel", NULL, MTX_DEF); 460170374Ssimokawa CALLOUT_INIT(&fc->timeout_callout); 461170374Ssimokawa CALLOUT_INIT(&fc->bmr_callout); 462170374Ssimokawa CALLOUT_INIT(&fc->busprobe_callout); 463170374Ssimokawa TASK_INIT(&fc->task_timeout, 0, firewire_xfer_timeout, (void *)fc); 464108853Ssimokawa 465110577Ssimokawa callout_reset(&sc->fc->timeout_callout, hz, 466110577Ssimokawa (void *)firewire_watchdog, (void *)sc->fc); 467110193Ssimokawa 468169806Ssimokawa /* create thread */ 469172836Sjulian kproc_create(fw_bus_probe_thread, (void *)fc, &fc->probe_thread, 470169806Ssimokawa 0, 0, "fw%d_probe", unit); 471169806Ssimokawa 472103285Sikob /* Locate our children */ 473103285Sikob bus_generic_probe(dev); 474103285Sikob 475103285Sikob /* launch attachement of the added children */ 476103285Sikob bus_generic_attach(dev); 477103285Sikob 478103285Sikob /* bus_reset */ 479187993Ssbruno FW_GLOCK(fc); 480169117Ssimokawa fw_busreset(fc, FWBUSNOTREADY); 481187993Ssbruno FW_GUNLOCK(fc); 482103285Sikob fc->ibr(fc); 483103285Sikob 484103285Sikob return 0; 485103285Sikob} 486103285Sikob 487103285Sikob/* 488103285Sikob * Attach it as child. 489103285Sikob */ 490103285Sikobstatic device_t 491212413Savgfirewire_add_child(device_t dev, u_int order, const char *name, int unit) 492103285Sikob{ 493103285Sikob device_t child; 494103285Sikob struct firewire_softc *sc; 495103285Sikob 496103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 497103285Sikob child = device_add_child(dev, name, unit); 498103285Sikob if (child) { 499103285Sikob device_set_ivars(child, sc->fc); 500103285Sikob device_probe_and_attach(child); 501103285Sikob } 502103285Sikob 503103285Sikob return child; 504103285Sikob} 505106790Ssimokawa 506116978Ssimokawastatic int 507116978Ssimokawafirewire_resume(device_t dev) 508116978Ssimokawa{ 509116978Ssimokawa struct firewire_softc *sc; 510116978Ssimokawa 511116978Ssimokawa sc = (struct firewire_softc *)device_get_softc(dev); 512116978Ssimokawa sc->fc->status = FWBUSNOTREADY; 513116978Ssimokawa 514116978Ssimokawa bus_generic_resume(dev); 515116978Ssimokawa 516116978Ssimokawa return(0); 517116978Ssimokawa} 518116978Ssimokawa 519103285Sikob/* 520103285Sikob * Dettach it. 521103285Sikob */ 522103285Sikobstatic int 523118455Ssimokawafirewire_detach(device_t dev) 524103285Sikob{ 525103285Sikob struct firewire_softc *sc; 526169806Ssimokawa struct firewire_comm *fc; 527111078Ssimokawa struct fw_device *fwdev, *fwdev_next; 528118455Ssimokawa int err; 529103285Sikob 530103285Sikob sc = (struct firewire_softc *)device_get_softc(dev); 531169806Ssimokawa fc = sc->fc; 532170374Ssimokawa mtx_lock(&fc->wait_lock); 533169806Ssimokawa fc->status = FWBUSDETACH; 534170374Ssimokawa wakeup(fc); 535170374Ssimokawa if (msleep(fc->probe_thread, &fc->wait_lock, PWAIT, "fwthr", hz * 60)) 536170374Ssimokawa printf("firewire probe thread didn't die\n"); 537170374Ssimokawa mtx_unlock(&fc->wait_lock); 538169806Ssimokawa 539178915Ssimokawa if (fc->arq !=0 && fc->arq->maxq > 0) 540178915Ssimokawa fw_drain_txq(fc); 541178915Ssimokawa 542118455Ssimokawa if ((err = fwdev_destroydev(sc)) != 0) 543118455Ssimokawa return err; 544106790Ssimokawa 545118455Ssimokawa if ((err = bus_generic_detach(dev)) != 0) 546118455Ssimokawa return err; 547111078Ssimokawa 548169806Ssimokawa callout_stop(&fc->timeout_callout); 549169806Ssimokawa callout_stop(&fc->bmr_callout); 550169806Ssimokawa callout_stop(&fc->busprobe_callout); 551111078Ssimokawa 552178915Ssimokawa /* XXX xfer_free and untimeout on all xfers */ 553169806Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; 554111078Ssimokawa fwdev = fwdev_next) { 555111078Ssimokawa fwdev_next = STAILQ_NEXT(fwdev, link); 556111078Ssimokawa free(fwdev, M_FW); 557111078Ssimokawa } 558169806Ssimokawa free(fc->topology_map, M_FW); 559169806Ssimokawa free(fc->speed_map, M_FW); 560169806Ssimokawa free(fc->crom_src_buf, M_FW); 561169806Ssimokawa 562171513Ssimokawa mtx_destroy(&fc->tlabel_lock); 563170374Ssimokawa mtx_destroy(&fc->wait_lock); 564103285Sikob return(0); 565103285Sikob} 566103285Sikob#if 0 567103285Sikobstatic int 568103285Sikobfirewire_shutdown( device_t dev ) 569103285Sikob{ 570103285Sikob return 0; 571103285Sikob} 572103285Sikob#endif 573106790Ssimokawa 574110577Ssimokawa 575110577Ssimokawastatic void 576110798Ssimokawafw_xferq_drain(struct fw_xferq *xferq) 577110577Ssimokawa{ 578110577Ssimokawa struct fw_xfer *xfer; 579110577Ssimokawa 580110577Ssimokawa while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) { 581110577Ssimokawa STAILQ_REMOVE_HEAD(&xferq->q, link); 582170374Ssimokawa#if 0 583111942Ssimokawa xferq->queued --; 584170374Ssimokawa#endif 585110577Ssimokawa xfer->resp = EAGAIN; 586170374Ssimokawa xfer->flag = FWXF_SENTERR; 587113584Ssimokawa fw_xfer_done(xfer); 588110577Ssimokawa } 589110577Ssimokawa} 590110577Ssimokawa 591110798Ssimokawavoid 592110798Ssimokawafw_drain_txq(struct firewire_comm *fc) 593110798Ssimokawa{ 594170374Ssimokawa struct fw_xfer *xfer, *txfer; 595170374Ssimokawa STAILQ_HEAD(, fw_xfer) xfer_drain; 596110798Ssimokawa int i; 597110798Ssimokawa 598170374Ssimokawa STAILQ_INIT(&xfer_drain); 599170374Ssimokawa 600170374Ssimokawa FW_GLOCK(fc); 601110798Ssimokawa fw_xferq_drain(fc->atq); 602110798Ssimokawa fw_xferq_drain(fc->ats); 603110798Ssimokawa for(i = 0; i < fc->nisodma; i++) 604110798Ssimokawa fw_xferq_drain(fc->it[i]); 605171513Ssimokawa FW_GUNLOCK(fc); 606170374Ssimokawa 607171513Ssimokawa mtx_lock(&fc->tlabel_lock); 608170374Ssimokawa for (i = 0; i < 0x40; i ++) 609170374Ssimokawa while ((xfer = STAILQ_FIRST(&fc->tlabels[i])) != NULL) { 610170374Ssimokawa if (firewire_debug) 611170374Ssimokawa printf("tl=%d flag=%d\n", i, xfer->flag); 612249291Swill xfer->tl = -1; 613170374Ssimokawa xfer->resp = EAGAIN; 614170374Ssimokawa STAILQ_REMOVE_HEAD(&fc->tlabels[i], tlabel); 615170374Ssimokawa STAILQ_INSERT_TAIL(&xfer_drain, xfer, tlabel); 616170374Ssimokawa } 617171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 618170374Ssimokawa 619170374Ssimokawa STAILQ_FOREACH_SAFE(xfer, &xfer_drain, tlabel, txfer) 620170374Ssimokawa xfer->hand(xfer); 621110798Ssimokawa} 622110798Ssimokawa 623116376Ssimokawastatic void 624116376Ssimokawafw_reset_csr(struct firewire_comm *fc) 625103285Sikob{ 626103285Sikob int i; 627103285Sikob 628103285Sikob CSRARC(fc, STATE_CLEAR) 629103285Sikob = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 630103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 631103285Sikob CSRARC(fc, NODE_IDS) = 0x3f; 632103285Sikob 633103285Sikob CSRARC(fc, TOPO_MAP + 8) = 0; 634103285Sikob fc->irm = -1; 635103285Sikob 636103285Sikob fc->max_node = -1; 637103285Sikob 638103285Sikob for(i = 2; i < 0x100/4 - 2 ; i++){ 639103285Sikob CSRARC(fc, SPED_MAP + i * 4) = 0; 640103285Sikob } 641103285Sikob CSRARC(fc, STATE_CLEAR) = 1 << 23 | 0 << 17 | 1 << 16 | 1 << 15 | 1 << 14 ; 642103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 643103285Sikob CSRARC(fc, RESET_START) = 0; 644103285Sikob CSRARC(fc, SPLIT_TIMEOUT_HI) = 0; 645103285Sikob CSRARC(fc, SPLIT_TIMEOUT_LO) = 800 << 19; 646103285Sikob CSRARC(fc, CYCLE_TIME) = 0x0; 647103285Sikob CSRARC(fc, BUS_TIME) = 0x0; 648103285Sikob CSRARC(fc, BUS_MGR_ID) = 0x3f; 649103285Sikob CSRARC(fc, BANDWIDTH_AV) = 4915; 650103285Sikob CSRARC(fc, CHANNELS_AV_HI) = 0xffffffff; 651103285Sikob CSRARC(fc, CHANNELS_AV_LO) = 0xffffffff; 652261455Seadler CSRARC(fc, IP_CHANNELS) = (1U << 31); 653103285Sikob 654103285Sikob CSRARC(fc, CONF_ROM) = 0x04 << 24; 655103285Sikob CSRARC(fc, CONF_ROM + 4) = 0x31333934; /* means strings 1394 */ 656103285Sikob CSRARC(fc, CONF_ROM + 8) = 1 << 31 | 1 << 30 | 1 << 29 | 657103285Sikob 1 << 28 | 0xff << 16 | 0x09 << 8; 658103285Sikob CSRARC(fc, CONF_ROM + 0xc) = 0; 659103285Sikob 660103285Sikob/* DV depend CSRs see blue book */ 661103285Sikob CSRARC(fc, oPCR) &= ~DV_BROADCAST_ON; 662103285Sikob CSRARC(fc, iPCR) &= ~DV_BROADCAST_ON; 663103285Sikob 664103285Sikob CSRARC(fc, STATE_CLEAR) &= ~(1 << 23 | 1 << 15 | 1 << 14 ); 665103285Sikob CSRARC(fc, STATE_SET) = CSRARC(fc, STATE_CLEAR); 666116376Ssimokawa} 667113584Ssimokawa 668116376Ssimokawastatic void 669116376Ssimokawafw_init_crom(struct firewire_comm *fc) 670116376Ssimokawa{ 671116376Ssimokawa struct crom_src *src; 672116376Ssimokawa 673116376Ssimokawa src = &fc->crom_src_buf->src; 674116376Ssimokawa bzero(src, sizeof(struct crom_src)); 675116376Ssimokawa 676116376Ssimokawa /* BUS info sample */ 677116376Ssimokawa src->hdr.info_len = 4; 678116376Ssimokawa 679116376Ssimokawa src->businfo.bus_name = CSR_BUS_NAME_IEEE1394; 680116376Ssimokawa 681116376Ssimokawa src->businfo.irmc = 1; 682116376Ssimokawa src->businfo.cmc = 1; 683116376Ssimokawa src->businfo.isc = 1; 684116376Ssimokawa src->businfo.bmc = 1; 685116376Ssimokawa src->businfo.pmc = 0; 686116376Ssimokawa src->businfo.cyc_clk_acc = 100; 687116376Ssimokawa src->businfo.max_rec = fc->maxrec; 688116376Ssimokawa src->businfo.max_rom = MAXROM_4; 689189928Ssbruno#define FW_GENERATION_CHANGEABLE 2 690189928Ssbruno src->businfo.generation = FW_GENERATION_CHANGEABLE; 691116376Ssimokawa src->businfo.link_spd = fc->speed; 692116376Ssimokawa 693116376Ssimokawa src->businfo.eui64.hi = fc->eui.hi; 694116376Ssimokawa src->businfo.eui64.lo = fc->eui.lo; 695116376Ssimokawa 696116376Ssimokawa STAILQ_INIT(&src->chunk_list); 697116376Ssimokawa 698116376Ssimokawa fc->crom_src = src; 699116376Ssimokawa fc->crom_root = &fc->crom_src_buf->root; 700116376Ssimokawa} 701116376Ssimokawa 702116376Ssimokawastatic void 703116376Ssimokawafw_reset_crom(struct firewire_comm *fc) 704116376Ssimokawa{ 705116376Ssimokawa struct crom_src_buf *buf; 706116376Ssimokawa struct crom_src *src; 707116376Ssimokawa struct crom_chunk *root; 708116376Ssimokawa 709116376Ssimokawa buf = fc->crom_src_buf; 710116376Ssimokawa src = fc->crom_src; 711116376Ssimokawa root = fc->crom_root; 712116376Ssimokawa 713116376Ssimokawa STAILQ_INIT(&src->chunk_list); 714116376Ssimokawa 715116376Ssimokawa bzero(root, sizeof(struct crom_chunk)); 716116376Ssimokawa crom_add_chunk(src, NULL, root, 0); 717116376Ssimokawa crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */ 718116376Ssimokawa /* private company_id */ 719116376Ssimokawa crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE); 720127468Ssimokawa#ifdef __DragonFly__ 721127468Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project"); 722127468Ssimokawa crom_add_entry(root, CSRKEY_HW, __DragonFly_cc_version); 723127468Ssimokawa#else 724116376Ssimokawa crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project"); 725116376Ssimokawa crom_add_entry(root, CSRKEY_HW, __FreeBSD_version); 726127468Ssimokawa#endif 727193066Sjamie mtx_lock(&prison0.pr_mtx); 728194118Sjamie crom_add_simple_text(src, root, &buf->hw, prison0.pr_hostname); 729193066Sjamie mtx_unlock(&prison0.pr_mtx); 730116376Ssimokawa} 731116376Ssimokawa 732116376Ssimokawa/* 733116376Ssimokawa * Called after bus reset. 734116376Ssimokawa */ 735116376Ssimokawavoid 736169117Ssimokawafw_busreset(struct firewire_comm *fc, uint32_t new_status) 737116376Ssimokawa{ 738116376Ssimokawa struct firewire_dev_comm *fdc; 739117350Ssimokawa struct crom_src *src; 740116376Ssimokawa device_t *devlistp; 741189928Ssbruno uint32_t *newrom; 742116376Ssimokawa int i, devcnt; 743116376Ssimokawa 744187993Ssbruno FW_GLOCK_ASSERT(fc); 745187993Ssbruno if (fc->status == FWBUSMGRELECT) 746116376Ssimokawa callout_stop(&fc->bmr_callout); 747187993Ssbruno 748169117Ssimokawa fc->status = new_status; 749116376Ssimokawa fw_reset_csr(fc); 750187993Ssbruno 751187993Ssbruno if (fc->status == FWBUSNOTREADY) 752187993Ssbruno fw_init_crom(fc); 753187993Ssbruno 754116376Ssimokawa fw_reset_crom(fc); 755116376Ssimokawa 756113584Ssimokawa if (device_get_children(fc->bdev, &devlistp, &devcnt) == 0) { 757113584Ssimokawa for( i = 0 ; i < devcnt ; i++) 758113584Ssimokawa if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 759113584Ssimokawa fdc = device_get_softc(devlistp[i]); 760113584Ssimokawa if (fdc->post_busreset != NULL) 761113584Ssimokawa fdc->post_busreset(fdc); 762113584Ssimokawa } 763113584Ssimokawa free(devlistp, M_TEMP); 764113584Ssimokawa } 765116376Ssimokawa 766117350Ssimokawa src = &fc->crom_src_buf->src; 767189928Ssbruno /* 768189928Ssbruno * If the old config rom needs to be overwritten, 769189928Ssbruno * bump the businfo.generation indicator to 770189928Ssbruno * indicate that we need to be reprobed 771189928Ssbruno * See 1394a-2000 8.3.2.5.4 for more details. 772189928Ssbruno * generation starts at 2 and rolls over at 0xF 773189928Ssbruno * back to 2. 774189928Ssbruno * 775189928Ssbruno * A generation of 0 indicates a device 776189928Ssbruno * that is not 1394a-2000 compliant. 777189928Ssbruno * A generation of 1 indicates a device that 778189928Ssbruno * does not change it's Bus Info Block or 779189928Ssbruno * Configuration ROM. 780189928Ssbruno */ 781189928Ssbruno#define FW_MAX_GENERATION 0xF 782189928Ssbruno newrom = malloc(CROMSIZE, M_FW, M_NOWAIT | M_ZERO); 783189928Ssbruno src = &fc->crom_src_buf->src; 784189928Ssbruno crom_load(src, newrom, CROMSIZE); 785189928Ssbruno if (bcmp(newrom, fc->config_rom, CROMSIZE) != 0) { 786189928Ssbruno if ( src->businfo.generation++ > FW_MAX_GENERATION ) 787189928Ssbruno src->businfo.generation = FW_GENERATION_CHANGEABLE; 788189928Ssbruno bcopy(newrom, (void *)fc->config_rom, CROMSIZE); 789189928Ssbruno } 790189928Ssbruno free(newrom, M_FW); 791189928Ssbruno 792103285Sikob} 793106790Ssimokawa 794103285Sikob/* Call once after reboot */ 795106790Ssimokawavoid fw_init(struct firewire_comm *fc) 796103285Sikob{ 797103285Sikob int i; 798106543Ssimokawa#ifdef FW_VMACCESS 799103285Sikob struct fw_xfer *xfer; 800103285Sikob struct fw_bind *fwb; 801106543Ssimokawa#endif 802103285Sikob 803103285Sikob fc->arq->queued = 0; 804103285Sikob fc->ars->queued = 0; 805103285Sikob fc->atq->queued = 0; 806103285Sikob fc->ats->queued = 0; 807103285Sikob 808103285Sikob fc->arq->buf = NULL; 809103285Sikob fc->ars->buf = NULL; 810103285Sikob fc->atq->buf = NULL; 811103285Sikob fc->ats->buf = NULL; 812103285Sikob 813113584Ssimokawa fc->arq->flag = 0; 814113584Ssimokawa fc->ars->flag = 0; 815113584Ssimokawa fc->atq->flag = 0; 816113584Ssimokawa fc->ats->flag = 0; 817103285Sikob 818103285Sikob STAILQ_INIT(&fc->atq->q); 819103285Sikob STAILQ_INIT(&fc->ats->q); 820103285Sikob 821103285Sikob for( i = 0 ; i < fc->nisodma ; i ++ ){ 822103285Sikob fc->it[i]->queued = 0; 823103285Sikob fc->ir[i]->queued = 0; 824103285Sikob 825103285Sikob fc->it[i]->start = NULL; 826103285Sikob fc->ir[i]->start = NULL; 827103285Sikob 828103285Sikob fc->it[i]->buf = NULL; 829103285Sikob fc->ir[i]->buf = NULL; 830103285Sikob 831103285Sikob fc->it[i]->flag = FWXFERQ_STREAM; 832103285Sikob fc->ir[i]->flag = FWXFERQ_STREAM; 833103285Sikob 834103285Sikob STAILQ_INIT(&fc->it[i]->q); 835103285Sikob STAILQ_INIT(&fc->ir[i]->q); 836103285Sikob } 837103285Sikob 838103285Sikob fc->arq->maxq = FWMAXQUEUE; 839103285Sikob fc->ars->maxq = FWMAXQUEUE; 840103285Sikob fc->atq->maxq = FWMAXQUEUE; 841103285Sikob fc->ats->maxq = FWMAXQUEUE; 842103285Sikob 843103285Sikob for( i = 0 ; i < fc->nisodma ; i++){ 844103285Sikob fc->ir[i]->maxq = FWMAXQUEUE; 845103285Sikob fc->it[i]->maxq = FWMAXQUEUE; 846103285Sikob } 847187993Ssbruno 848103285Sikob CSRARC(fc, TOPO_MAP) = 0x3f1 << 16; 849103285Sikob CSRARC(fc, TOPO_MAP + 4) = 1; 850103285Sikob CSRARC(fc, SPED_MAP) = 0x3f1 << 16; 851103285Sikob CSRARC(fc, SPED_MAP + 4) = 1; 852103285Sikob 853110193Ssimokawa STAILQ_INIT(&fc->devices); 854103285Sikob 855103285Sikob/* Initialize Async handlers */ 856103285Sikob STAILQ_INIT(&fc->binds); 857103285Sikob for( i = 0 ; i < 0x40 ; i++){ 858103285Sikob STAILQ_INIT(&fc->tlabels[i]); 859103285Sikob } 860103285Sikob 861103285Sikob/* DV depend CSRs see blue book */ 862103285Sikob#if 0 863103285Sikob CSRARC(fc, oMPR) = 0x3fff0001; /* # output channel = 1 */ 864103285Sikob CSRARC(fc, oPCR) = 0x8000007a; 865103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 866103285Sikob CSRARC(fc, i + oPCR) = 0x8000007a; 867103285Sikob } 868103285Sikob 869103285Sikob CSRARC(fc, iMPR) = 0x00ff0001; /* # input channel = 1 */ 870103285Sikob CSRARC(fc, iPCR) = 0x803f0000; 871103285Sikob for(i = 4 ; i < 0x7c/4 ; i+=4){ 872103285Sikob CSRARC(fc, i + iPCR) = 0x0; 873103285Sikob } 874103285Sikob#endif 875103285Sikob 876116376Ssimokawa fc->crom_src_buf = NULL; 877103285Sikob 878106543Ssimokawa#ifdef FW_VMACCESS 879103285Sikob xfer = fw_xfer_alloc(); 880103285Sikob if(xfer == NULL) return; 881103285Sikob 882110195Ssimokawa fwb = (struct fw_bind *)malloc(sizeof (struct fw_bind), M_FW, M_NOWAIT); 883103285Sikob if(fwb == NULL){ 884103285Sikob fw_xfer_free(xfer); 885139680Sjmg return; 886103285Sikob } 887167632Ssimokawa xfer->hand = fw_vmaccess; 888103285Sikob xfer->fc = fc; 889103285Sikob xfer->sc = NULL; 890103285Sikob 891103285Sikob fwb->start_hi = 0x2; 892103285Sikob fwb->start_lo = 0; 893103285Sikob fwb->addrlen = 0xffffffff; 894103285Sikob fwb->xfer = xfer; 895103285Sikob fw_bindadd(fc, fwb); 896106543Ssimokawa#endif 897103285Sikob} 898106790Ssimokawa 899120660Ssimokawa#define BIND_CMP(addr, fwb) (((addr) < (fwb)->start)?-1:\ 900120660Ssimokawa ((fwb)->end < (addr))?1:0) 901120660Ssimokawa 902103285Sikob/* 903129541Sdfr * To lookup bound process from IEEE1394 address. 904103285Sikob */ 905106813Ssimokawastruct fw_bind * 906129585Sdfrfw_bindlookup(struct firewire_comm *fc, uint16_t dest_hi, uint32_t dest_lo) 907103285Sikob{ 908120660Ssimokawa u_int64_t addr; 909170374Ssimokawa struct fw_bind *tfw, *r = NULL; 910120660Ssimokawa 911120660Ssimokawa addr = ((u_int64_t)dest_hi << 32) | dest_lo; 912170374Ssimokawa FW_GLOCK(fc); 913120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 914170374Ssimokawa if (BIND_CMP(addr, tfw) == 0) { 915170374Ssimokawa r = tfw; 916170374Ssimokawa break; 917170374Ssimokawa } 918170374Ssimokawa FW_GUNLOCK(fc); 919170374Ssimokawa return(r); 920103285Sikob} 921106790Ssimokawa 922103285Sikob/* 923103285Sikob * To bind IEEE1394 address block to process. 924103285Sikob */ 925106790Ssimokawaint 926106790Ssimokawafw_bindadd(struct firewire_comm *fc, struct fw_bind *fwb) 927103285Sikob{ 928120660Ssimokawa struct fw_bind *tfw, *prev = NULL; 929170374Ssimokawa int r = 0; 930120660Ssimokawa 931120660Ssimokawa if (fwb->start > fwb->end) { 932127468Ssimokawa printf("%s: invalid range\n", __func__); 933120660Ssimokawa return EINVAL; 934120660Ssimokawa } 935120660Ssimokawa 936170374Ssimokawa FW_GLOCK(fc); 937120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) { 938120660Ssimokawa if (fwb->end < tfw->start) 939120660Ssimokawa break; 940120660Ssimokawa prev = tfw; 941120660Ssimokawa } 942170374Ssimokawa if (prev == NULL) 943103285Sikob STAILQ_INSERT_HEAD(&fc->binds, fwb, fclist); 944170374Ssimokawa else if (prev->end < fwb->start) 945120660Ssimokawa STAILQ_INSERT_AFTER(&fc->binds, prev, fwb, fclist); 946170374Ssimokawa else { 947170374Ssimokawa printf("%s: bind failed\n", __func__); 948170374Ssimokawa r = EBUSY; 949103285Sikob } 950170374Ssimokawa FW_GUNLOCK(fc); 951170374Ssimokawa return (r); 952103285Sikob} 953103285Sikob 954103285Sikob/* 955103285Sikob * To free IEEE1394 address block. 956103285Sikob */ 957106790Ssimokawaint 958106790Ssimokawafw_bindremove(struct firewire_comm *fc, struct fw_bind *fwb) 959103285Sikob{ 960120660Ssimokawa#if 0 961120660Ssimokawa struct fw_xfer *xfer, *next; 962120660Ssimokawa#endif 963120660Ssimokawa struct fw_bind *tfw; 964103285Sikob int s; 965103285Sikob 966103285Sikob s = splfw(); 967170374Ssimokawa FW_GLOCK(fc); 968120660Ssimokawa STAILQ_FOREACH(tfw, &fc->binds, fclist) 969120660Ssimokawa if (tfw == fwb) { 970120660Ssimokawa STAILQ_REMOVE(&fc->binds, fwb, fw_bind, fclist); 971120660Ssimokawa goto found; 972120660Ssimokawa } 973120660Ssimokawa 974129541Sdfr printf("%s: no such binding\n", __func__); 975170374Ssimokawa FW_GUNLOCK(fc); 976120660Ssimokawa splx(s); 977120660Ssimokawa return (1); 978120660Ssimokawafound: 979120660Ssimokawa#if 0 980113584Ssimokawa /* shall we do this? */ 981113584Ssimokawa for (xfer = STAILQ_FIRST(&fwb->xferlist); xfer != NULL; xfer = next) { 982113584Ssimokawa next = STAILQ_NEXT(xfer, link); 983113584Ssimokawa fw_xfer_free(xfer); 984113584Ssimokawa } 985113584Ssimokawa STAILQ_INIT(&fwb->xferlist); 986120660Ssimokawa#endif 987170374Ssimokawa FW_GUNLOCK(fc); 988113584Ssimokawa 989103285Sikob splx(s); 990103285Sikob return 0; 991103285Sikob} 992103285Sikob 993169130Ssimokawaint 994169130Ssimokawafw_xferlist_add(struct fw_xferlist *q, struct malloc_type *type, 995169130Ssimokawa int slen, int rlen, int n, 996169130Ssimokawa struct firewire_comm *fc, void *sc, void (*hand)(struct fw_xfer *)) 997169130Ssimokawa{ 998169130Ssimokawa int i, s; 999169130Ssimokawa struct fw_xfer *xfer; 1000169130Ssimokawa 1001169130Ssimokawa for (i = 0; i < n; i++) { 1002169130Ssimokawa xfer = fw_xfer_alloc_buf(type, slen, rlen); 1003169130Ssimokawa if (xfer == NULL) 1004169130Ssimokawa return (n); 1005169130Ssimokawa xfer->fc = fc; 1006169130Ssimokawa xfer->sc = sc; 1007169130Ssimokawa xfer->hand = hand; 1008169130Ssimokawa s = splfw(); 1009169130Ssimokawa STAILQ_INSERT_TAIL(q, xfer, link); 1010169130Ssimokawa splx(s); 1011169130Ssimokawa } 1012169130Ssimokawa return (n); 1013169130Ssimokawa} 1014169130Ssimokawa 1015169130Ssimokawavoid 1016169130Ssimokawafw_xferlist_remove(struct fw_xferlist *q) 1017169130Ssimokawa{ 1018169130Ssimokawa struct fw_xfer *xfer, *next; 1019169130Ssimokawa 1020169130Ssimokawa for (xfer = STAILQ_FIRST(q); xfer != NULL; xfer = next) { 1021169130Ssimokawa next = STAILQ_NEXT(xfer, link); 1022169130Ssimokawa fw_xfer_free_buf(xfer); 1023169130Ssimokawa } 1024169130Ssimokawa STAILQ_INIT(q); 1025169130Ssimokawa} 1026170374Ssimokawa/* 1027170374Ssimokawa * dump packet header 1028170374Ssimokawa */ 1029170374Ssimokawastatic void 1030170374Ssimokawafw_dump_hdr(struct fw_pkt *fp, char *prefix) 1031170374Ssimokawa{ 1032170374Ssimokawa printf("%s: dst=0x%02x tl=0x%02x rt=%d tcode=0x%x pri=0x%x " 1033170374Ssimokawa "src=0x%03x\n", prefix, 1034170374Ssimokawa fp->mode.hdr.dst & 0x3f, 1035170374Ssimokawa fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tlrt & 3, 1036170374Ssimokawa fp->mode.hdr.tcode, fp->mode.hdr.pri, 1037170374Ssimokawa fp->mode.hdr.src); 1038170374Ssimokawa} 1039169130Ssimokawa 1040103285Sikob/* 1041103285Sikob * To free transaction label. 1042103285Sikob */ 1043106790Ssimokawastatic void 1044106790Ssimokawafw_tl_free(struct firewire_comm *fc, struct fw_xfer *xfer) 1045103285Sikob{ 1046169119Ssimokawa struct fw_xfer *txfer; 1047169119Ssimokawa int s; 1048103285Sikob 1049169119Ssimokawa s = splfw(); 1050171513Ssimokawa mtx_lock(&fc->tlabel_lock); 1051249291Swill if (xfer->tl < 0) { 1052249291Swill mtx_unlock(&fc->tlabel_lock); 1053249291Swill return; 1054249291Swill } 1055169119Ssimokawa#if 1 /* make sure the label is allocated */ 1056169119Ssimokawa STAILQ_FOREACH(txfer, &fc->tlabels[xfer->tl], tlabel) 1057169119Ssimokawa if(txfer == xfer) 1058169119Ssimokawa break; 1059169119Ssimokawa if (txfer == NULL) { 1060170374Ssimokawa printf("%s: the xfer is not in the queue " 1061170374Ssimokawa "(tlabel=%d, flag=0x%x)\n", 1062170374Ssimokawa __FUNCTION__, xfer->tl, xfer->flag); 1063170374Ssimokawa fw_dump_hdr(&xfer->send.hdr, "send"); 1064170374Ssimokawa fw_dump_hdr(&xfer->recv.hdr, "recv"); 1065170374Ssimokawa kdb_backtrace(); 1066171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1067169119Ssimokawa splx(s); 1068169119Ssimokawa return; 1069103285Sikob } 1070169119Ssimokawa#endif 1071169119Ssimokawa 1072169119Ssimokawa STAILQ_REMOVE(&fc->tlabels[xfer->tl], xfer, fw_xfer, tlabel); 1073249291Swill xfer->tl = -1; 1074171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1075103285Sikob splx(s); 1076103285Sikob return; 1077103285Sikob} 1078106790Ssimokawa 1079103285Sikob/* 1080103285Sikob * To obtain XFER structure by transaction label. 1081103285Sikob */ 1082106790Ssimokawastatic struct fw_xfer * 1083170374Ssimokawafw_tl2xfer(struct firewire_comm *fc, int node, int tlabel, int tcode) 1084103285Sikob{ 1085103285Sikob struct fw_xfer *xfer; 1086103285Sikob int s = splfw(); 1087170374Ssimokawa int req; 1088103285Sikob 1089171513Ssimokawa mtx_lock(&fc->tlabel_lock); 1090169119Ssimokawa STAILQ_FOREACH(xfer, &fc->tlabels[tlabel], tlabel) 1091169119Ssimokawa if(xfer->send.hdr.mode.hdr.dst == node) { 1092171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1093103285Sikob splx(s); 1094170374Ssimokawa KASSERT(xfer->tl == tlabel, 1095170374Ssimokawa ("xfer->tl 0x%x != 0x%x", xfer->tl, tlabel)); 1096170374Ssimokawa /* extra sanity check */ 1097170374Ssimokawa req = xfer->send.hdr.mode.hdr.tcode; 1098170374Ssimokawa if (xfer->fc->tcode[req].valid_res != tcode) { 1099170374Ssimokawa printf("%s: invalid response tcode " 1100170374Ssimokawa "(0x%x for 0x%x)\n", __FUNCTION__, 1101170374Ssimokawa tcode, req); 1102170374Ssimokawa return(NULL); 1103170374Ssimokawa } 1104170374Ssimokawa 1105110577Ssimokawa if (firewire_debug > 2) 1106110577Ssimokawa printf("fw_tl2xfer: found tl=%d\n", tlabel); 1107103285Sikob return(xfer); 1108103285Sikob } 1109171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1110110577Ssimokawa if (firewire_debug > 1) 1111110577Ssimokawa printf("fw_tl2xfer: not found tl=%d\n", tlabel); 1112103285Sikob splx(s); 1113103285Sikob return(NULL); 1114103285Sikob} 1115106790Ssimokawa 1116103285Sikob/* 1117103285Sikob * To allocate IEEE1394 XFER structure. 1118103285Sikob */ 1119106790Ssimokawastruct fw_xfer * 1120110269Ssimokawafw_xfer_alloc(struct malloc_type *type) 1121103285Sikob{ 1122103285Sikob struct fw_xfer *xfer; 1123106790Ssimokawa 1124110269Ssimokawa xfer = malloc(sizeof(struct fw_xfer), type, M_NOWAIT | M_ZERO); 1125106790Ssimokawa if (xfer == NULL) 1126106790Ssimokawa return xfer; 1127106790Ssimokawa 1128110269Ssimokawa xfer->malloc = type; 1129106790Ssimokawa 1130103285Sikob return xfer; 1131103285Sikob} 1132106790Ssimokawa 1133113584Ssimokawastruct fw_xfer * 1134113584Ssimokawafw_xfer_alloc_buf(struct malloc_type *type, int send_len, int recv_len) 1135113584Ssimokawa{ 1136113584Ssimokawa struct fw_xfer *xfer; 1137113584Ssimokawa 1138113584Ssimokawa xfer = fw_xfer_alloc(type); 1139126102Scperciva if (xfer == NULL) 1140126102Scperciva return(NULL); 1141120660Ssimokawa xfer->send.pay_len = send_len; 1142120660Ssimokawa xfer->recv.pay_len = recv_len; 1143120660Ssimokawa if (send_len > 0) { 1144120660Ssimokawa xfer->send.payload = malloc(send_len, type, M_NOWAIT | M_ZERO); 1145120660Ssimokawa if (xfer->send.payload == NULL) { 1146113584Ssimokawa fw_xfer_free(xfer); 1147113584Ssimokawa return(NULL); 1148113584Ssimokawa } 1149113584Ssimokawa } 1150120660Ssimokawa if (recv_len > 0) { 1151120660Ssimokawa xfer->recv.payload = malloc(recv_len, type, M_NOWAIT); 1152120660Ssimokawa if (xfer->recv.payload == NULL) { 1153120660Ssimokawa if (xfer->send.payload != NULL) 1154120660Ssimokawa free(xfer->send.payload, type); 1155113584Ssimokawa fw_xfer_free(xfer); 1156113584Ssimokawa return(NULL); 1157113584Ssimokawa } 1158113584Ssimokawa } 1159113584Ssimokawa return(xfer); 1160113584Ssimokawa} 1161113584Ssimokawa 1162103285Sikob/* 1163103285Sikob * IEEE1394 XFER post process. 1164103285Sikob */ 1165103285Sikobvoid 1166103285Sikobfw_xfer_done(struct fw_xfer *xfer) 1167103285Sikob{ 1168167632Ssimokawa if (xfer->hand == NULL) { 1169167632Ssimokawa printf("hand == NULL\n"); 1170103285Sikob return; 1171117716Ssimokawa } 1172103285Sikob 1173121505Ssimokawa if (xfer->fc == NULL) 1174121505Ssimokawa panic("fw_xfer_done: why xfer->fc is NULL?"); 1175121505Ssimokawa 1176168051Ssimokawa fw_tl_free(xfer->fc, xfer); 1177167632Ssimokawa xfer->hand(xfer); 1178103285Sikob} 1179103285Sikob 1180106790Ssimokawavoid 1181113584Ssimokawafw_xfer_unload(struct fw_xfer* xfer) 1182103285Sikob{ 1183103285Sikob int s; 1184113584Ssimokawa 1185103285Sikob if(xfer == NULL ) return; 1186170374Ssimokawa if(xfer->flag & FWXF_INQ){ 1187103285Sikob printf("fw_xfer_free FWXF_INQ\n"); 1188103285Sikob s = splfw(); 1189170374Ssimokawa FW_GLOCK(xfer->fc); 1190103285Sikob STAILQ_REMOVE(&xfer->q->q, xfer, fw_xfer, link); 1191170374Ssimokawa#if 0 1192103285Sikob xfer->q->queued --; 1193170374Ssimokawa#endif 1194170374Ssimokawa FW_GUNLOCK(xfer->fc); 1195103285Sikob splx(s); 1196103285Sikob } 1197113584Ssimokawa if (xfer->fc != NULL) { 1198249291Swill /* 1199249291Swill * Ensure that any tlabel owner can't access this 1200249291Swill * xfer after it's freed. 1201249291Swill */ 1202249291Swill fw_tl_free(xfer->fc, xfer); 1203114729Ssimokawa#if 1 1204170374Ssimokawa if(xfer->flag & FWXF_START) 1205114729Ssimokawa /* 1206114729Ssimokawa * This could happen if: 1207114729Ssimokawa * 1. We call fwohci_arcv() before fwohci_txd(). 1208114729Ssimokawa * 2. firewire_watch() is called. 1209114729Ssimokawa */ 1210114729Ssimokawa printf("fw_xfer_free FWXF_START\n"); 1211103285Sikob#endif 1212103285Sikob } 1213170374Ssimokawa xfer->flag = FWXF_INIT; 1214113584Ssimokawa xfer->resp = 0; 1215113584Ssimokawa} 1216113584Ssimokawa/* 1217113584Ssimokawa * To free IEEE1394 XFER structure. 1218113584Ssimokawa */ 1219113584Ssimokawavoid 1220120660Ssimokawafw_xfer_free_buf( struct fw_xfer* xfer) 1221113584Ssimokawa{ 1222120660Ssimokawa if (xfer == NULL) { 1223127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1224120660Ssimokawa return; 1225120660Ssimokawa } 1226113584Ssimokawa fw_xfer_unload(xfer); 1227120660Ssimokawa if(xfer->send.payload != NULL){ 1228120660Ssimokawa free(xfer->send.payload, xfer->malloc); 1229103285Sikob } 1230120660Ssimokawa if(xfer->recv.payload != NULL){ 1231120660Ssimokawa free(xfer->recv.payload, xfer->malloc); 1232103285Sikob } 1233110269Ssimokawa free(xfer, xfer->malloc); 1234103285Sikob} 1235103285Sikob 1236120660Ssimokawavoid 1237120660Ssimokawafw_xfer_free( struct fw_xfer* xfer) 1238120660Ssimokawa{ 1239120660Ssimokawa if (xfer == NULL) { 1240127468Ssimokawa printf("%s: xfer == NULL\n", __func__); 1241120660Ssimokawa return; 1242120660Ssimokawa } 1243120660Ssimokawa fw_xfer_unload(xfer); 1244120660Ssimokawa free(xfer, xfer->malloc); 1245120660Ssimokawa} 1246120660Ssimokawa 1247120660Ssimokawavoid 1248110072Ssimokawafw_asy_callback_free(struct fw_xfer *xfer) 1249103285Sikob{ 1250103285Sikob#if 0 1251170374Ssimokawa printf("asyreq done flag=0x%02x resp=%d\n", 1252170374Ssimokawa xfer->flag, xfer->resp); 1253103285Sikob#endif 1254103285Sikob fw_xfer_free(xfer); 1255103285Sikob} 1256103285Sikob 1257103285Sikob/* 1258103285Sikob * To configure PHY. 1259103285Sikob */ 1260103285Sikobstatic void 1261103285Sikobfw_phy_config(struct firewire_comm *fc, int root_node, int gap_count) 1262103285Sikob{ 1263103285Sikob struct fw_xfer *xfer; 1264103285Sikob struct fw_pkt *fp; 1265103285Sikob 1266103285Sikob fc->status = FWBUSPHYCONF; 1267103285Sikob 1268120660Ssimokawa xfer = fw_xfer_alloc(M_FWXFER); 1269113584Ssimokawa if (xfer == NULL) 1270113584Ssimokawa return; 1271103285Sikob xfer->fc = fc; 1272167632Ssimokawa xfer->hand = fw_asy_callback_free; 1273103285Sikob 1274120660Ssimokawa fp = &xfer->send.hdr; 1275103285Sikob fp->mode.ld[1] = 0; 1276103285Sikob if (root_node >= 0) 1277113584Ssimokawa fp->mode.ld[1] |= (root_node & 0x3f) << 24 | 1 << 23; 1278103285Sikob if (gap_count >= 0) 1279113584Ssimokawa fp->mode.ld[1] |= 1 << 22 | (gap_count & 0x3f) << 16; 1280103285Sikob fp->mode.ld[2] = ~fp->mode.ld[1]; 1281103285Sikob/* XXX Dangerous, how to pass PHY packet to device driver */ 1282103285Sikob fp->mode.common.tcode |= FWTCODE_PHY; 1283103285Sikob 1284107653Ssimokawa if (firewire_debug) 1285188722Ssbruno device_printf(fc->bdev, "%s: root_node=%d gap_count=%d\n", 1286188722Ssbruno __func__, root_node, gap_count); 1287103285Sikob fw_asyreq(fc, -1, xfer); 1288103285Sikob} 1289103285Sikob 1290103285Sikob/* 1291103285Sikob * Dump self ID. 1292103285Sikob */ 1293103285Sikobstatic void 1294129585Sdfrfw_print_sid(uint32_t sid) 1295103285Sikob{ 1296103285Sikob union fw_self_id *s; 1297103285Sikob s = (union fw_self_id *) &sid; 1298188726Ssbruno if ( s->p0.sequel ) { 1299188726Ssbruno if ( s->p1.sequence_num == FW_SELF_ID_PAGE0 ) { 1300188726Ssbruno printf("node:%d p3:%d p4:%d p5:%d p6:%d p7:%d" 1301188726Ssbruno "p8:%d p9:%d p10:%d\n", 1302188726Ssbruno s->p1.phy_id, s->p1.port3, s->p1.port4, 1303188726Ssbruno s->p1.port5, s->p1.port6, s->p1.port7, 1304188726Ssbruno s->p1.port8, s->p1.port9, s->p1.port10); 1305188726Ssbruno } else if (s->p2.sequence_num == FW_SELF_ID_PAGE1 ){ 1306188726Ssbruno printf("node:%d p11:%d p12:%d p13:%d p14:%d p15:%d\n", 1307188726Ssbruno s->p2.phy_id, s->p2.port11, s->p2.port12, 1308188726Ssbruno s->p2.port13, s->p2.port14, s->p2.port15); 1309188726Ssbruno } else { 1310188726Ssbruno printf("node:%d Unknown Self ID Page number %d\n", 1311188726Ssbruno s->p1.phy_id, s->p1.sequence_num); 1312188726Ssbruno } 1313188726Ssbruno } else { 1314188726Ssbruno printf("node:%d link:%d gap:%d spd:%d con:%d pwr:%d" 1315188726Ssbruno " p0:%d p1:%d p2:%d i:%d m:%d\n", 1316188726Ssbruno s->p0.phy_id, s->p0.link_active, s->p0.gap_count, 1317188726Ssbruno s->p0.phy_speed, s->p0.contender, 1318188726Ssbruno s->p0.power_class, s->p0.port0, s->p0.port1, 1319188726Ssbruno s->p0.port2, s->p0.initiated_reset, s->p0.more_packets); 1320188726Ssbruno } 1321103285Sikob} 1322103285Sikob 1323103285Sikob/* 1324103285Sikob * To receive self ID. 1325103285Sikob */ 1326129585Sdfrvoid fw_sidrcv(struct firewire_comm* fc, uint32_t *sid, u_int len) 1327103285Sikob{ 1328129585Sdfr uint32_t *p; 1329103285Sikob union fw_self_id *self_id; 1330103285Sikob u_int i, j, node, c_port = 0, i_branch = 0; 1331103285Sikob 1332129585Sdfr fc->sid_cnt = len /(sizeof(uint32_t) * 2); 1333103285Sikob fc->max_node = fc->nodeid & 0x3f; 1334129585Sdfr CSRARC(fc, NODE_IDS) = ((uint32_t)fc->nodeid) << 16; 1335103285Sikob fc->status = FWBUSCYMELECT; 1336103285Sikob fc->topology_map->crc_len = 2; 1337103285Sikob fc->topology_map->generation ++; 1338103285Sikob fc->topology_map->self_id_count = 0; 1339103285Sikob fc->topology_map->node_count = 0; 1340103285Sikob fc->speed_map->generation ++; 1341103285Sikob fc->speed_map->crc_len = 1 + (64*64 + 3) / 4; 1342103285Sikob self_id = &fc->topology_map->self_id[0]; 1343103285Sikob for(i = 0; i < fc->sid_cnt; i ++){ 1344103285Sikob if (sid[1] != ~sid[0]) { 1345188722Ssbruno device_printf(fc->bdev, "%s: ERROR invalid self-id packet\n", 1346188722Ssbruno __func__); 1347103285Sikob sid += 2; 1348103285Sikob continue; 1349103285Sikob } 1350103285Sikob *self_id = *((union fw_self_id *)sid); 1351103285Sikob fc->topology_map->crc_len++; 1352103285Sikob if(self_id->p0.sequel == 0){ 1353103285Sikob fc->topology_map->node_count ++; 1354103285Sikob c_port = 0; 1355188722Ssbruno if (firewire_debug) 1356188722Ssbruno fw_print_sid(sid[0]); 1357103285Sikob node = self_id->p0.phy_id; 1358103285Sikob if(fc->max_node < node){ 1359103285Sikob fc->max_node = self_id->p0.phy_id; 1360103285Sikob } 1361103285Sikob /* XXX I'm not sure this is the right speed_map */ 1362103285Sikob fc->speed_map->speed[node][node] 1363103285Sikob = self_id->p0.phy_speed; 1364103285Sikob for (j = 0; j < node; j ++) { 1365103285Sikob fc->speed_map->speed[j][node] 1366103285Sikob = fc->speed_map->speed[node][j] 1367103285Sikob = min(fc->speed_map->speed[j][j], 1368103285Sikob self_id->p0.phy_speed); 1369103285Sikob } 1370103285Sikob if ((fc->irm == -1 || self_id->p0.phy_id > fc->irm) && 1371103285Sikob (self_id->p0.link_active && self_id->p0.contender)) { 1372103285Sikob fc->irm = self_id->p0.phy_id; 1373103285Sikob } 1374103285Sikob if(self_id->p0.port0 >= 0x2){ 1375103285Sikob c_port++; 1376103285Sikob } 1377103285Sikob if(self_id->p0.port1 >= 0x2){ 1378103285Sikob c_port++; 1379103285Sikob } 1380103285Sikob if(self_id->p0.port2 >= 0x2){ 1381103285Sikob c_port++; 1382103285Sikob } 1383103285Sikob } 1384103285Sikob if(c_port > 2){ 1385103285Sikob i_branch += (c_port - 2); 1386103285Sikob } 1387103285Sikob sid += 2; 1388103285Sikob self_id++; 1389103285Sikob fc->topology_map->self_id_count ++; 1390103285Sikob } 1391103285Sikob /* CRC */ 1392103285Sikob fc->topology_map->crc = fw_crc16( 1393129585Sdfr (uint32_t *)&fc->topology_map->generation, 1394103285Sikob fc->topology_map->crc_len * 4); 1395103285Sikob fc->speed_map->crc = fw_crc16( 1396129585Sdfr (uint32_t *)&fc->speed_map->generation, 1397103285Sikob fc->speed_map->crc_len * 4); 1398103285Sikob /* byteswap and copy to CSR */ 1399129585Sdfr p = (uint32_t *)fc->topology_map; 1400103285Sikob for (i = 0; i <= fc->topology_map->crc_len; i++) 1401103285Sikob CSRARC(fc, TOPO_MAP + i * 4) = htonl(*p++); 1402129585Sdfr p = (uint32_t *)fc->speed_map; 1403103285Sikob CSRARC(fc, SPED_MAP) = htonl(*p++); 1404103285Sikob CSRARC(fc, SPED_MAP + 4) = htonl(*p++); 1405129585Sdfr /* don't byte-swap uint8_t array */ 1406103285Sikob bcopy(p, &CSRARC(fc, SPED_MAP + 8), (fc->speed_map->crc_len - 1)*4); 1407103285Sikob 1408103285Sikob fc->max_hop = fc->max_node - i_branch; 1409188722Ssbruno device_printf(fc->bdev, "%d nodes, maxhop <= %d %s irm(%d) %s\n", 1410188722Ssbruno fc->max_node + 1, fc->max_hop, 1411188722Ssbruno (fc->irm == -1) ? "Not IRM capable" : "cable IRM", 1412188722Ssbruno fc->irm, 1413188722Ssbruno (fc->irm == fc->nodeid) ? " (me) " : ""); 1414103285Sikob 1415109736Ssimokawa if (try_bmr && (fc->irm != -1) && (CSRARC(fc, BUS_MGR_ID) == 0x3f)) { 1416114909Ssimokawa if (fc->irm == fc->nodeid) { 1417103285Sikob fc->status = FWBUSMGRDONE; 1418103285Sikob CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, fc->irm); 1419114909Ssimokawa fw_bmr(fc); 1420109736Ssimokawa } else { 1421103285Sikob fc->status = FWBUSMGRELECT; 1422110193Ssimokawa callout_reset(&fc->bmr_callout, hz/8, 1423110193Ssimokawa (void *)fw_try_bmr, (void *)fc); 1424103285Sikob } 1425114909Ssimokawa } else 1426103285Sikob fc->status = FWBUSMGRDONE; 1427114909Ssimokawa 1428108853Ssimokawa callout_reset(&fc->busprobe_callout, hz/4, 1429108853Ssimokawa (void *)fw_bus_probe, (void *)fc); 1430103285Sikob} 1431106790Ssimokawa 1432103285Sikob/* 1433103285Sikob * To probe devices on the IEEE1394 bus. 1434103285Sikob */ 1435106790Ssimokawastatic void 1436106790Ssimokawafw_bus_probe(struct firewire_comm *fc) 1437103285Sikob{ 1438103285Sikob int s; 1439120850Ssimokawa struct fw_device *fwdev; 1440103285Sikob 1441103285Sikob s = splfw(); 1442103285Sikob fc->status = FWBUSEXPLORE; 1443103285Sikob 1444120850Ssimokawa /* Invalidate all devices, just after bus reset. */ 1445188722Ssbruno if (firewire_debug) 1446188722Ssbruno device_printf(fc->bdev, "%s:" 1447188722Ssbruno "iterate and invalidate all nodes\n", 1448188722Ssbruno __func__); 1449120850Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1450110193Ssimokawa if (fwdev->status != FWDEVINVAL) { 1451103285Sikob fwdev->status = FWDEVINVAL; 1452103285Sikob fwdev->rcnt = 0; 1453188722Ssbruno if (firewire_debug) 1454188722Ssbruno device_printf(fc->bdev, "%s:" 1455188722Ssbruno "Invalidate Dev ID: %08x%08x\n", 1456188722Ssbruno __func__, fwdev->eui.hi, fwdev->eui.lo); 1457188722Ssbruno } else { 1458188722Ssbruno if (firewire_debug) 1459188722Ssbruno device_printf(fc->bdev, "%s:" 1460188722Ssbruno "Dev ID: %08x%08x already invalid\n", 1461188722Ssbruno __func__, fwdev->eui.hi, fwdev->eui.lo); 1462103285Sikob } 1463169806Ssimokawa splx(s); 1464120850Ssimokawa 1465169806Ssimokawa wakeup((void *)fc); 1466103285Sikob} 1467106790Ssimokawa 1468169806Ssimokawastatic int 1469169806Ssimokawafw_explore_read_quads(struct fw_device *fwdev, int offset, 1470188722Ssbruno uint32_t *quad, int length) 1471129274Sdfr{ 1472169806Ssimokawa struct fw_xfer *xfer; 1473169806Ssimokawa uint32_t tmp; 1474169806Ssimokawa int i, error; 1475129274Sdfr 1476188722Ssbruno for (i = 0; i < length; i ++, offset += sizeof(uint32_t)) { 1477169806Ssimokawa xfer = fwmem_read_quad(fwdev, NULL, -1, 1478169806Ssimokawa 0xffff, 0xf0000000 | offset, (void *)&tmp, 1479170374Ssimokawa fw_xferwake); 1480169806Ssimokawa if (xfer == NULL) 1481169806Ssimokawa return (-1); 1482170374Ssimokawa fw_xferwait(xfer); 1483169806Ssimokawa 1484169806Ssimokawa if (xfer->resp == 0) 1485169806Ssimokawa quad[i] = ntohl(tmp); 1486169806Ssimokawa 1487169806Ssimokawa error = xfer->resp; 1488169806Ssimokawa fw_xfer_free(xfer); 1489169806Ssimokawa if (error) 1490169806Ssimokawa return (error); 1491169806Ssimokawa } 1492169806Ssimokawa return (0); 1493169806Ssimokawa} 1494169806Ssimokawa 1495169806Ssimokawa 1496169806Ssimokawastatic int 1497169806Ssimokawafw_explore_csrblock(struct fw_device *fwdev, int offset, int recur) 1498169806Ssimokawa{ 1499169806Ssimokawa int err, i, off; 1500169806Ssimokawa struct csrdirectory *dir; 1501169806Ssimokawa struct csrreg *reg; 1502169806Ssimokawa 1503169806Ssimokawa dir = (struct csrdirectory *)&fwdev->csrrom[offset/sizeof(uint32_t)]; 1504169806Ssimokawa err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, 1505169806Ssimokawa (uint32_t *)dir, 1); 1506169806Ssimokawa if (err) 1507169806Ssimokawa return (-1); 1508169806Ssimokawa 1509169806Ssimokawa offset += sizeof(uint32_t); 1510169806Ssimokawa reg = (struct csrreg *)&fwdev->csrrom[offset/sizeof(uint32_t)]; 1511169806Ssimokawa err = fw_explore_read_quads(fwdev, CSRROMOFF + offset, 1512169806Ssimokawa (uint32_t *)reg, dir->crc_len); 1513169806Ssimokawa if (err) 1514169806Ssimokawa return (-1); 1515169806Ssimokawa 1516169806Ssimokawa /* XXX check CRC */ 1517169806Ssimokawa 1518169806Ssimokawa off = CSRROMOFF + offset + sizeof(uint32_t) * (dir->crc_len - 1); 1519169806Ssimokawa if (fwdev->rommax < off) 1520169806Ssimokawa fwdev->rommax = off; 1521169806Ssimokawa 1522169806Ssimokawa if (recur == 0) 1523169806Ssimokawa return (0); 1524169806Ssimokawa 1525169806Ssimokawa for (i = 0; i < dir->crc_len; i ++, offset += sizeof(uint32_t)) { 1526171302Ssimokawa if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_D) 1527169806Ssimokawa recur = 1; 1528171302Ssimokawa else if ((reg[i].key & CSRTYPE_MASK) == CSRTYPE_L) 1529169806Ssimokawa recur = 0; 1530169806Ssimokawa else 1531129274Sdfr continue; 1532169806Ssimokawa 1533169806Ssimokawa off = offset + reg[i].val * sizeof(uint32_t); 1534169806Ssimokawa if (off > CROMSIZE) { 1535169806Ssimokawa printf("%s: invalid offset %d\n", __FUNCTION__, off); 1536169806Ssimokawa return(-1); 1537169806Ssimokawa } 1538169806Ssimokawa err = fw_explore_csrblock(fwdev, off, recur); 1539169806Ssimokawa if (err) 1540169806Ssimokawa return (-1); 1541129274Sdfr } 1542169806Ssimokawa return (0); 1543129274Sdfr} 1544129274Sdfr 1545169806Ssimokawastatic int 1546169806Ssimokawafw_explore_node(struct fw_device *dfwdev) 1547103285Sikob{ 1548169806Ssimokawa struct firewire_comm *fc; 1549110179Ssimokawa struct fw_device *fwdev, *pfwdev, *tfwdev; 1550169806Ssimokawa uint32_t *csr; 1551169806Ssimokawa struct csrhdr *hdr; 1552169806Ssimokawa struct bus_info *binfo; 1553188704Ssbruno int err, node; 1554188704Ssbruno uint32_t speed_test = 0; 1555103285Sikob 1556169806Ssimokawa fc = dfwdev->fc; 1557169806Ssimokawa csr = dfwdev->csrrom; 1558169806Ssimokawa node = dfwdev->dst; 1559103285Sikob 1560169806Ssimokawa /* First quad */ 1561169806Ssimokawa err = fw_explore_read_quads(dfwdev, CSRROMOFF, &csr[0], 1); 1562188704Ssbruno if (err) { 1563188704Ssbruno device_printf(fc->bdev, "%s: node%d: explore_read_quads failure\n", 1564188704Ssbruno __func__, node); 1565188704Ssbruno dfwdev->status = FWDEVINVAL; 1566169806Ssimokawa return (-1); 1567188704Ssbruno } 1568169806Ssimokawa hdr = (struct csrhdr *)&csr[0]; 1569169806Ssimokawa if (hdr->info_len != 4) { 1570169806Ssimokawa if (firewire_debug) 1571188722Ssbruno device_printf(fc->bdev, "%s: node%d: wrong bus info len(%d)\n", 1572188722Ssbruno __func__, node, hdr->info_len); 1573188722Ssbruno dfwdev->status = FWDEVINVAL; 1574169806Ssimokawa return (-1); 1575169806Ssimokawa } 1576103285Sikob 1577169806Ssimokawa /* bus info */ 1578169806Ssimokawa err = fw_explore_read_quads(dfwdev, CSRROMOFF + 0x04, &csr[1], 4); 1579188722Ssbruno if (err) { 1580188722Ssbruno device_printf(fc->bdev, "%s: node%d: error reading 0x04\n", 1581188722Ssbruno __func__, node); 1582188722Ssbruno dfwdev->status = FWDEVINVAL; 1583169806Ssimokawa return (-1); 1584188722Ssbruno } 1585169806Ssimokawa binfo = (struct bus_info *)&csr[1]; 1586169806Ssimokawa if (binfo->bus_name != CSR_BUS_NAME_IEEE1394) { 1587188722Ssbruno device_printf(fc->bdev, "%s: node%d: invalid bus name 0x%08x\n", 1588188722Ssbruno __func__, node, binfo->bus_name); 1589188722Ssbruno dfwdev->status = FWDEVINVAL; 1590169806Ssimokawa return (-1); 1591103285Sikob } 1592188704Ssbruno 1593188704Ssbruno if (firewire_debug) 1594188704Ssbruno device_printf(fc->bdev, "%s: node(%d) BUS INFO BLOCK:\n" 1595188704Ssbruno "irmc(%d) cmc(%d) isc(%d) bmc(%d) pmc(%d) " 1596188704Ssbruno "cyc_clk_acc(%d) max_rec(%d) max_rom(%d) " 1597188704Ssbruno "generation(%d) link_spd(%d)\n", 1598188704Ssbruno __func__, node, 1599188704Ssbruno binfo->irmc, binfo->cmc, binfo->isc, 1600188704Ssbruno binfo->bmc, binfo->pmc, binfo->cyc_clk_acc, 1601188704Ssbruno binfo->max_rec, binfo->max_rom, 1602188704Ssbruno binfo->generation, binfo->link_spd); 1603188704Ssbruno 1604169806Ssimokawa STAILQ_FOREACH(fwdev, &fc->devices, link) 1605169806Ssimokawa if (FW_EUI64_EQUAL(fwdev->eui, binfo->eui64)) 1606169806Ssimokawa break; 1607169806Ssimokawa if (fwdev == NULL) { 1608169806Ssimokawa /* new device */ 1609169806Ssimokawa fwdev = malloc(sizeof(struct fw_device), M_FW, 1610169806Ssimokawa M_NOWAIT | M_ZERO); 1611169806Ssimokawa if (fwdev == NULL) { 1612188722Ssbruno device_printf(fc->bdev, "%s: node%d: no memory\n", 1613188722Ssbruno __func__, node); 1614169806Ssimokawa return (-1); 1615103285Sikob } 1616106810Ssimokawa fwdev->fc = fc; 1617169806Ssimokawa fwdev->eui = binfo->eui64; 1618189928Ssbruno fwdev->dst = dfwdev->dst; 1619189928Ssbruno fwdev->maxrec = dfwdev->maxrec; 1620189928Ssbruno fwdev->status = dfwdev->status; 1621189928Ssbruno 1622188704Ssbruno /* 1623188704Ssbruno * Pre-1394a-2000 didn't have link_spd in 1624188704Ssbruno * the Bus Info block, so try and use the 1625188704Ssbruno * speed map value. 1626188704Ssbruno * 1394a-2000 compliant devices only use 1627188704Ssbruno * the Bus Info Block link spd value, so 1628188704Ssbruno * ignore the speed map alltogether. SWB 1629188704Ssbruno */ 1630188704Ssbruno if ( binfo->link_spd == FWSPD_S100 /* 0 */) { 1631189928Ssbruno device_printf(fc->bdev, "%s: " 1632188704Ssbruno "Pre 1394a-2000 detected\n", 1633188704Ssbruno __func__); 1634188704Ssbruno fwdev->speed = fc->speed_map->speed[fc->nodeid][node]; 1635188704Ssbruno } else 1636188704Ssbruno fwdev->speed = binfo->link_spd; 1637188704Ssbruno /* 1638188704Ssbruno * Test this speed with a read to the CSRROM. 1639188704Ssbruno * If it fails, slow down the speed and retry. 1640188704Ssbruno */ 1641189928Ssbruno while (fwdev->speed > FWSPD_S100 /* 0 */) { 1642188704Ssbruno err = fw_explore_read_quads(fwdev, CSRROMOFF, 1643188704Ssbruno &speed_test, 1); 1644189928Ssbruno if (err) { 1645189928Ssbruno device_printf(fc->bdev, "%s: fwdev->speed(%s)" 1646189928Ssbruno " decremented due to negotiation\n", 1647189928Ssbruno __func__, 1648189928Ssbruno linkspeed[fwdev->speed]); 1649188704Ssbruno fwdev->speed--; 1650189928Ssbruno } else 1651188704Ssbruno break; 1652188704Ssbruno 1653188704Ssbruno } 1654190792Ssbruno 1655190792Ssbruno /* 1656190792Ssbruno * If the fwdev is not found in the 1657190792Ssbruno * fc->devices TAILQ, then we will add it. 1658190792Ssbruno */ 1659110179Ssimokawa pfwdev = NULL; 1660110193Ssimokawa STAILQ_FOREACH(tfwdev, &fc->devices, link) { 1661110179Ssimokawa if (tfwdev->eui.hi > fwdev->eui.hi || 1662169806Ssimokawa (tfwdev->eui.hi == fwdev->eui.hi && 1663169806Ssimokawa tfwdev->eui.lo > fwdev->eui.lo)) 1664110179Ssimokawa break; 1665110179Ssimokawa pfwdev = tfwdev; 1666103285Sikob } 1667110179Ssimokawa if (pfwdev == NULL) 1668110193Ssimokawa STAILQ_INSERT_HEAD(&fc->devices, fwdev, link); 1669110179Ssimokawa else 1670110193Ssimokawa STAILQ_INSERT_AFTER(&fc->devices, pfwdev, fwdev, link); 1671103285Sikob 1672108655Ssimokawa device_printf(fc->bdev, "New %s device ID:%08x%08x\n", 1673188704Ssbruno linkspeed[fwdev->speed], 1674169806Ssimokawa fwdev->eui.hi, fwdev->eui.lo); 1675189928Ssbruno } else { 1676189928Ssbruno fwdev->dst = node; 1677189928Ssbruno fwdev->status = FWDEVINIT; 1678189928Ssbruno /* unchanged ? */ 1679189928Ssbruno if (bcmp(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5) == 0) { 1680189928Ssbruno if (firewire_debug) 1681189928Ssbruno device_printf(fc->dev, "node%d: crom unchanged\n", node); 1682189928Ssbruno return (0); 1683189928Ssbruno } 1684103285Sikob } 1685103285Sikob 1686169806Ssimokawa bzero(&fwdev->csrrom[0], CROMSIZE); 1687106790Ssimokawa 1688169806Ssimokawa /* copy first quad and bus info block */ 1689169806Ssimokawa bcopy(&csr[0], &fwdev->csrrom[0], sizeof(uint32_t) * 5); 1690169806Ssimokawa fwdev->rommax = CSRROMOFF + sizeof(uint32_t) * 4; 1691103285Sikob 1692169806Ssimokawa err = fw_explore_csrblock(fwdev, 0x14, 1); /* root directory */ 1693113584Ssimokawa 1694169806Ssimokawa if (err) { 1695189928Ssbruno if (firewire_debug) 1696189928Ssbruno device_printf(fc->dev, "%s: explore csrblock failed err(%d)\n", 1697189928Ssbruno __func__, err); 1698169806Ssimokawa fwdev->status = FWDEVINVAL; 1699169806Ssimokawa fwdev->csrrom[0] = 0; 1700103285Sikob } 1701169806Ssimokawa return (err); 1702103285Sikob 1703103285Sikob} 1704106790Ssimokawa 1705103285Sikob/* 1706169806Ssimokawa * Find the self_id packet for a node, ignoring sequels. 1707103285Sikob */ 1708169806Ssimokawastatic union fw_self_id * 1709169806Ssimokawafw_find_self_id(struct firewire_comm *fc, int node) 1710106790Ssimokawa{ 1711169806Ssimokawa uint32_t i; 1712169806Ssimokawa union fw_self_id *s; 1713103285Sikob 1714169806Ssimokawa for (i = 0; i < fc->topology_map->self_id_count; i++) { 1715169806Ssimokawa s = &fc->topology_map->self_id[i]; 1716169806Ssimokawa if (s->p0.sequel) 1717169806Ssimokawa continue; 1718169806Ssimokawa if (s->p0.phy_id == node) 1719169806Ssimokawa return s; 1720110577Ssimokawa } 1721169806Ssimokawa return 0; 1722169806Ssimokawa} 1723110577Ssimokawa 1724169806Ssimokawastatic void 1725169806Ssimokawafw_explore(struct firewire_comm *fc) 1726169806Ssimokawa{ 1727169806Ssimokawa int node, err, s, i, todo, todo2, trys; 1728169806Ssimokawa char nodes[63]; 1729169806Ssimokawa struct fw_device dfwdev; 1730169829Ssimokawa union fw_self_id *fwsid; 1731110577Ssimokawa 1732169806Ssimokawa todo = 0; 1733169806Ssimokawa /* setup dummy fwdev */ 1734169806Ssimokawa dfwdev.fc = fc; 1735169806Ssimokawa dfwdev.speed = 0; 1736169806Ssimokawa dfwdev.maxrec = 8; /* 512 */ 1737169806Ssimokawa dfwdev.status = FWDEVINIT; 1738103285Sikob 1739169806Ssimokawa for (node = 0; node <= fc->max_node; node ++) { 1740169806Ssimokawa /* We don't probe myself and linkdown nodes */ 1741188722Ssbruno if (node == fc->nodeid) { 1742188722Ssbruno if (firewire_debug) 1743188722Ssbruno device_printf(fc->bdev, "%s:" 1744188722Ssbruno "found myself node(%d) fc->nodeid(%d) fc->max_node(%d)\n", 1745188722Ssbruno __func__, node, fc->nodeid, fc->max_node); 1746169806Ssimokawa continue; 1747188722Ssbruno } else if (firewire_debug) { 1748188722Ssbruno device_printf(fc->bdev, "%s:" 1749188722Ssbruno "node(%d) fc->max_node(%d) found\n", 1750188722Ssbruno __func__, node, fc->max_node); 1751188722Ssbruno } 1752169829Ssimokawa fwsid = fw_find_self_id(fc, node); 1753169829Ssimokawa if (!fwsid || !fwsid->p0.link_active) { 1754169806Ssimokawa if (firewire_debug) 1755188722Ssbruno device_printf(fc->bdev, "%s: node%d: link down\n", 1756188722Ssbruno __func__, node); 1757169806Ssimokawa continue; 1758103285Sikob } 1759169806Ssimokawa nodes[todo++] = node; 1760103285Sikob } 1761103285Sikob 1762169806Ssimokawa s = splfw(); 1763169806Ssimokawa for (trys = 0; todo > 0 && trys < 3; trys ++) { 1764169806Ssimokawa todo2 = 0; 1765169806Ssimokawa for (i = 0; i < todo; i ++) { 1766169806Ssimokawa dfwdev.dst = nodes[i]; 1767169806Ssimokawa err = fw_explore_node(&dfwdev); 1768169806Ssimokawa if (err) 1769169806Ssimokawa nodes[todo2++] = nodes[i]; 1770169806Ssimokawa if (firewire_debug) 1771188722Ssbruno device_printf(fc->bdev, "%s: node %d, err = %d\n", 1772188722Ssbruno __func__, node, err); 1773103285Sikob } 1774169806Ssimokawa todo = todo2; 1775103285Sikob } 1776169806Ssimokawa splx(s); 1777169806Ssimokawa} 1778169806Ssimokawa 1779169806Ssimokawa 1780169806Ssimokawastatic void 1781169806Ssimokawafw_bus_probe_thread(void *arg) 1782169806Ssimokawa{ 1783169806Ssimokawa struct firewire_comm *fc; 1784169806Ssimokawa 1785169806Ssimokawa fc = (struct firewire_comm *)arg; 1786169806Ssimokawa 1787170374Ssimokawa mtx_lock(&fc->wait_lock); 1788170374Ssimokawa while (fc->status != FWBUSDETACH) { 1789169806Ssimokawa if (fc->status == FWBUSEXPLORE) { 1790170374Ssimokawa mtx_unlock(&fc->wait_lock); 1791169806Ssimokawa fw_explore(fc); 1792169806Ssimokawa fc->status = FWBUSEXPDONE; 1793169806Ssimokawa if (firewire_debug) 1794169806Ssimokawa printf("bus_explore done\n"); 1795169806Ssimokawa fw_attach_dev(fc); 1796170374Ssimokawa mtx_lock(&fc->wait_lock); 1797170374Ssimokawa } 1798170374Ssimokawa msleep((void *)fc, &fc->wait_lock, PWAIT|PCATCH, "-", 0); 1799127468Ssimokawa } 1800170374Ssimokawa mtx_unlock(&fc->wait_lock); 1801172836Sjulian kproc_exit(0); 1802103285Sikob} 1803103285Sikob 1804103285Sikob/* 1805103285Sikob * To attach sub-devices layer onto IEEE1394 bus. 1806103285Sikob */ 1807106815Ssimokawastatic void 1808106815Ssimokawafw_attach_dev(struct firewire_comm *fc) 1809103285Sikob{ 1810120850Ssimokawa struct fw_device *fwdev, *next; 1811103285Sikob int i, err; 1812103285Sikob device_t *devlistp; 1813103285Sikob int devcnt; 1814103285Sikob struct firewire_dev_comm *fdc; 1815103285Sikob 1816120850Ssimokawa for (fwdev = STAILQ_FIRST(&fc->devices); fwdev != NULL; fwdev = next) { 1817120850Ssimokawa next = STAILQ_NEXT(fwdev, link); 1818120850Ssimokawa if (fwdev->status == FWDEVINIT) { 1819114069Ssimokawa fwdev->status = FWDEVATTACHED; 1820120850Ssimokawa } else if (fwdev->status == FWDEVINVAL) { 1821120850Ssimokawa fwdev->rcnt ++; 1822188722Ssbruno if (firewire_debug) 1823188722Ssbruno device_printf(fc->bdev, "%s:" 1824188722Ssbruno "fwdev->rcnt(%d), hold_count(%d)\n", 1825188722Ssbruno __func__, fwdev->rcnt, hold_count); 1826120850Ssimokawa if (fwdev->rcnt > hold_count) { 1827120850Ssimokawa /* 1828120850Ssimokawa * Remove devices which have not been seen 1829120850Ssimokawa * for a while. 1830120850Ssimokawa */ 1831188722Ssbruno device_printf(fc->bdev, "%s:" 1832188722Ssbruno "Removing missing device ID:%08x%08x\n", 1833188722Ssbruno __func__, fwdev->eui.hi, fwdev->eui.lo); 1834120850Ssimokawa STAILQ_REMOVE(&fc->devices, fwdev, fw_device, 1835120850Ssimokawa link); 1836120850Ssimokawa free(fwdev, M_FW); 1837120850Ssimokawa } 1838120850Ssimokawa } 1839120850Ssimokawa } 1840103285Sikob 1841108773Ssimokawa err = device_get_children(fc->bdev, &devlistp, &devcnt); 1842188722Ssbruno if( err == 0 ) { 1843188722Ssbruno for( i = 0 ; i < devcnt ; i++){ 1844188722Ssbruno if (device_get_state(devlistp[i]) >= DS_ATTACHED) { 1845188722Ssbruno fdc = device_get_softc(devlistp[i]); 1846188722Ssbruno if (fdc->post_explore != NULL) 1847188722Ssbruno fdc->post_explore(fdc); 1848188722Ssbruno } 1849103285Sikob } 1850188722Ssbruno free(devlistp, M_TEMP); 1851103285Sikob } 1852103285Sikob 1853103285Sikob return; 1854103285Sikob} 1855106815Ssimokawa 1856103285Sikob/* 1857129541Sdfr * To allocate unique transaction label. 1858103285Sikob */ 1859106815Ssimokawastatic int 1860106815Ssimokawafw_get_tlabel(struct firewire_comm *fc, struct fw_xfer *xfer) 1861103285Sikob{ 1862171457Ssimokawa u_int dst, new_tlabel; 1863169119Ssimokawa struct fw_xfer *txfer; 1864103285Sikob int s; 1865103285Sikob 1866171457Ssimokawa dst = xfer->send.hdr.mode.hdr.dst & 0x3f; 1867103285Sikob s = splfw(); 1868171513Ssimokawa mtx_lock(&fc->tlabel_lock); 1869171457Ssimokawa new_tlabel = (fc->last_tlabel[dst] + 1) & 0x3f; 1870171457Ssimokawa STAILQ_FOREACH(txfer, &fc->tlabels[new_tlabel], tlabel) 1871171457Ssimokawa if ((txfer->send.hdr.mode.hdr.dst & 0x3f) == dst) 1872120660Ssimokawa break; 1873171457Ssimokawa if(txfer == NULL) { 1874171457Ssimokawa fc->last_tlabel[dst] = new_tlabel; 1875171457Ssimokawa STAILQ_INSERT_TAIL(&fc->tlabels[new_tlabel], xfer, tlabel); 1876171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1877171457Ssimokawa splx(s); 1878171457Ssimokawa xfer->tl = new_tlabel; 1879171457Ssimokawa xfer->send.hdr.mode.hdr.tlrt = new_tlabel << 2; 1880171457Ssimokawa if (firewire_debug > 1) 1881171457Ssimokawa printf("fw_get_tlabel: dst=%d tl=%d\n", dst, new_tlabel); 1882171457Ssimokawa return (new_tlabel); 1883103285Sikob } 1884171513Ssimokawa mtx_unlock(&fc->tlabel_lock); 1885103285Sikob splx(s); 1886103285Sikob 1887130460Sdfr if (firewire_debug > 1) 1888130460Sdfr printf("fw_get_tlabel: no free tlabel\n"); 1889171457Ssimokawa return (-1); 1890103285Sikob} 1891106815Ssimokawa 1892113584Ssimokawastatic void 1893120660Ssimokawafw_rcv_copy(struct fw_rcv_buf *rb) 1894113584Ssimokawa{ 1895120660Ssimokawa struct fw_pkt *pkt; 1896120660Ssimokawa u_char *p; 1897120660Ssimokawa struct tcode_info *tinfo; 1898120660Ssimokawa u_int res, i, len, plen; 1899113584Ssimokawa 1900169127Ssimokawa rb->xfer->recv.spd = rb->spd; 1901120660Ssimokawa 1902120660Ssimokawa pkt = (struct fw_pkt *)rb->vec->iov_base; 1903120660Ssimokawa tinfo = &rb->fc->tcode[pkt->mode.hdr.tcode]; 1904120660Ssimokawa 1905120660Ssimokawa /* Copy header */ 1906120660Ssimokawa p = (u_char *)&rb->xfer->recv.hdr; 1907120660Ssimokawa bcopy(rb->vec->iov_base, p, tinfo->hdr_len); 1908132771Skan rb->vec->iov_base = (u_char *)rb->vec->iov_base + tinfo->hdr_len; 1909120660Ssimokawa rb->vec->iov_len -= tinfo->hdr_len; 1910120660Ssimokawa 1911120660Ssimokawa /* Copy payload */ 1912120660Ssimokawa p = (u_char *)rb->xfer->recv.payload; 1913120660Ssimokawa res = rb->xfer->recv.pay_len; 1914120660Ssimokawa 1915120660Ssimokawa /* special handling for RRESQ */ 1916120660Ssimokawa if (pkt->mode.hdr.tcode == FWTCODE_RRESQ && 1917129585Sdfr p != NULL && res >= sizeof(uint32_t)) { 1918129585Sdfr *(uint32_t *)p = pkt->mode.rresq.data; 1919129585Sdfr rb->xfer->recv.pay_len = sizeof(uint32_t); 1920120660Ssimokawa return; 1921120660Ssimokawa } 1922120660Ssimokawa 1923120660Ssimokawa if ((tinfo->flag & FWTI_BLOCK_ASY) == 0) 1924120660Ssimokawa return; 1925120660Ssimokawa 1926120660Ssimokawa plen = pkt->mode.rresb.len; 1927120660Ssimokawa 1928120660Ssimokawa for (i = 0; i < rb->nvec; i++, rb->vec++) { 1929120660Ssimokawa len = MIN(rb->vec->iov_len, plen); 1930113584Ssimokawa if (res < len) { 1931188722Ssbruno device_printf(rb->fc->bdev, "%s:" 1932188722Ssbruno " rcv buffer(%d) is %d bytes short.\n", 1933188722Ssbruno __func__, rb->xfer->recv.pay_len, len - res); 1934113584Ssimokawa len = res; 1935113584Ssimokawa } 1936120660Ssimokawa bcopy(rb->vec->iov_base, p, len); 1937113584Ssimokawa p += len; 1938113584Ssimokawa res -= len; 1939120660Ssimokawa plen -= len; 1940120660Ssimokawa if (res == 0 || plen == 0) 1941113584Ssimokawa break; 1942113584Ssimokawa } 1943120660Ssimokawa rb->xfer->recv.pay_len -= res; 1944120660Ssimokawa 1945113584Ssimokawa} 1946113584Ssimokawa 1947103285Sikob/* 1948129541Sdfr * Generic packet receiving process. 1949103285Sikob */ 1950106815Ssimokawavoid 1951120660Ssimokawafw_rcv(struct fw_rcv_buf *rb) 1952103285Sikob{ 1953103285Sikob struct fw_pkt *fp, *resfp; 1954103285Sikob struct fw_bind *bind; 1955169130Ssimokawa int tcode; 1956113584Ssimokawa int i, len, oldstate; 1957103285Sikob#if 0 1958103285Sikob { 1959129585Sdfr uint32_t *qld; 1960103285Sikob int i; 1961129585Sdfr qld = (uint32_t *)buf; 1962103285Sikob printf("spd %d len:%d\n", spd, len); 1963103285Sikob for( i = 0 ; i <= len && i < 32; i+= 4){ 1964103285Sikob printf("0x%08x ", ntohl(qld[i/4])); 1965103285Sikob if((i % 16) == 15) printf("\n"); 1966103285Sikob } 1967103285Sikob if((i % 16) != 15) printf("\n"); 1968103285Sikob } 1969103285Sikob#endif 1970120660Ssimokawa fp = (struct fw_pkt *)rb->vec[0].iov_base; 1971113584Ssimokawa tcode = fp->mode.common.tcode; 1972113584Ssimokawa switch (tcode) { 1973103285Sikob case FWTCODE_WRES: 1974103285Sikob case FWTCODE_RRESQ: 1975103285Sikob case FWTCODE_RRESB: 1976103285Sikob case FWTCODE_LRES: 1977120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1978170374Ssimokawa fp->mode.hdr.tlrt >> 2, fp->mode.hdr.tcode); 1979120660Ssimokawa if(rb->xfer == NULL) { 1980188722Ssbruno device_printf(rb->fc->bdev, "%s: " 1981188722Ssbruno "unknown response " 1982188722Ssbruno "%s(%x) src=0x%x tl=0x%x rt=%d data=0x%x\n", 1983188722Ssbruno __func__, 1984188722Ssbruno tcode_str[tcode], tcode, 1985188722Ssbruno fp->mode.hdr.src, 1986188722Ssbruno fp->mode.hdr.tlrt >> 2, 1987188722Ssbruno fp->mode.hdr.tlrt & 3, 1988188722Ssbruno fp->mode.rresq.data); 1989170374Ssimokawa#if 0 1990103285Sikob printf("try ad-hoc work around!!\n"); 1991120660Ssimokawa rb->xfer = fw_tl2xfer(rb->fc, fp->mode.hdr.src, 1992103285Sikob (fp->mode.hdr.tlrt >> 2)^3); 1993120660Ssimokawa if (rb->xfer == NULL) { 1994103285Sikob printf("no use...\n"); 1995169131Ssimokawa return; 1996103285Sikob } 1997103285Sikob#else 1998169131Ssimokawa return; 1999103285Sikob#endif 2000103285Sikob } 2001120660Ssimokawa fw_rcv_copy(rb); 2002120660Ssimokawa if (rb->xfer->recv.hdr.mode.wres.rtcode != RESP_CMP) 2003120660Ssimokawa rb->xfer->resp = EIO; 2004120660Ssimokawa else 2005120660Ssimokawa rb->xfer->resp = 0; 2006113584Ssimokawa /* make sure the packet is drained in AT queue */ 2007170374Ssimokawa oldstate = rb->xfer->flag; 2008170374Ssimokawa rb->xfer->flag = FWXF_RCVD; 2009113584Ssimokawa switch (oldstate) { 2010113584Ssimokawa case FWXF_SENT: 2011120660Ssimokawa fw_xfer_done(rb->xfer); 2012103285Sikob break; 2013113584Ssimokawa case FWXF_START: 2014119289Ssimokawa#if 0 2015113584Ssimokawa if (firewire_debug) 2016120660Ssimokawa printf("not sent yet tl=%x\n", rb->xfer->tl); 2017119289Ssimokawa#endif 2018113584Ssimokawa break; 2019103285Sikob default: 2020188722Ssbruno device_printf(rb->fc->bdev, "%s: " 2021188722Ssbruno "unexpected flag 0x%02x\n", __func__, rb->xfer->flag); 2022103285Sikob } 2023113584Ssimokawa return; 2024103285Sikob case FWTCODE_WREQQ: 2025103285Sikob case FWTCODE_WREQB: 2026103285Sikob case FWTCODE_RREQQ: 2027103285Sikob case FWTCODE_RREQB: 2028103285Sikob case FWTCODE_LREQ: 2029120660Ssimokawa bind = fw_bindlookup(rb->fc, fp->mode.rreqq.dest_hi, 2030113584Ssimokawa fp->mode.rreqq.dest_lo); 2031103285Sikob if(bind == NULL){ 2032188722Ssbruno device_printf(rb->fc->bdev, "%s: " 2033188722Ssbruno "Unknown service addr 0x%04x:0x%08x %s(%x)" 2034127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2035188722Ssbruno " src=0x%x data=%lx\n", 2036127468Ssimokawa#else 2037188722Ssbruno " src=0x%x data=%x\n", 2038113962Ssimokawa#endif 2039188722Ssbruno __func__, 2040188722Ssbruno fp->mode.wreqq.dest_hi, 2041188722Ssbruno fp->mode.wreqq.dest_lo, 2042188722Ssbruno tcode_str[tcode], tcode, 2043188722Ssbruno fp->mode.hdr.src, 2044188722Ssbruno ntohl(fp->mode.wreqq.data)); 2045188722Ssbruno 2046170425Ssimokawa if (rb->fc->status == FWBUSINIT) { 2047188722Ssbruno device_printf(rb->fc->bdev, "%s: cannot respond(bus reset)!\n", 2048188722Ssbruno __func__); 2049169131Ssimokawa return; 2050103285Sikob } 2051120660Ssimokawa rb->xfer = fw_xfer_alloc(M_FWXFER); 2052120660Ssimokawa if(rb->xfer == NULL){ 2053103285Sikob return; 2054103285Sikob } 2055120660Ssimokawa rb->xfer->send.spd = rb->spd; 2056120660Ssimokawa rb->xfer->send.pay_len = 0; 2057120660Ssimokawa resfp = &rb->xfer->send.hdr; 2058113584Ssimokawa switch (tcode) { 2059103285Sikob case FWTCODE_WREQQ: 2060103285Sikob case FWTCODE_WREQB: 2061103285Sikob resfp->mode.hdr.tcode = FWTCODE_WRES; 2062103285Sikob break; 2063103285Sikob case FWTCODE_RREQQ: 2064103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESQ; 2065103285Sikob break; 2066103285Sikob case FWTCODE_RREQB: 2067103285Sikob resfp->mode.hdr.tcode = FWTCODE_RRESB; 2068103285Sikob break; 2069103285Sikob case FWTCODE_LREQ: 2070103285Sikob resfp->mode.hdr.tcode = FWTCODE_LRES; 2071103285Sikob break; 2072103285Sikob } 2073103285Sikob resfp->mode.hdr.dst = fp->mode.hdr.src; 2074103285Sikob resfp->mode.hdr.tlrt = fp->mode.hdr.tlrt; 2075103285Sikob resfp->mode.hdr.pri = fp->mode.hdr.pri; 2076120660Ssimokawa resfp->mode.rresb.rtcode = RESP_ADDRESS_ERROR; 2077103285Sikob resfp->mode.rresb.extcode = 0; 2078103285Sikob resfp->mode.rresb.len = 0; 2079103285Sikob/* 2080170374Ssimokawa rb->xfer->hand = fw_xferwake; 2081103285Sikob*/ 2082167632Ssimokawa rb->xfer->hand = fw_xfer_free; 2083120660Ssimokawa if(fw_asyreq(rb->fc, -1, rb->xfer)){ 2084120660Ssimokawa fw_xfer_free(rb->xfer); 2085103285Sikob return; 2086103285Sikob } 2087169131Ssimokawa return; 2088103285Sikob } 2089113584Ssimokawa len = 0; 2090120660Ssimokawa for (i = 0; i < rb->nvec; i ++) 2091120660Ssimokawa len += rb->vec[i].iov_len; 2092169130Ssimokawa rb->xfer = STAILQ_FIRST(&bind->xferlist); 2093169130Ssimokawa if (rb->xfer == NULL) { 2094188722Ssbruno device_printf(rb->fc->bdev, "%s: " 2095188722Ssbruno "Discard a packet for this bind.\n", 2096188722Ssbruno __func__); 2097169131Ssimokawa return; 2098103285Sikob } 2099169130Ssimokawa STAILQ_REMOVE_HEAD(&bind->xferlist, link); 2100169130Ssimokawa fw_rcv_copy(rb); 2101169130Ssimokawa rb->xfer->hand(rb->xfer); 2102169130Ssimokawa return; 2103120660Ssimokawa#if 0 /* shouldn't happen ?? or for GASP */ 2104103285Sikob case FWTCODE_STREAM: 2105103285Sikob { 2106103285Sikob struct fw_xferq *xferq; 2107103285Sikob 2108120660Ssimokawa xferq = rb->fc->ir[sub]; 2109103285Sikob#if 0 2110103285Sikob printf("stream rcv dma %d len %d off %d spd %d\n", 2111103285Sikob sub, len, off, spd); 2112103285Sikob#endif 2113103285Sikob if(xferq->queued >= xferq->maxq) { 2114103285Sikob printf("receive queue is full\n"); 2115169131Ssimokawa return; 2116103285Sikob } 2117113584Ssimokawa /* XXX get xfer from xfer queue, we don't need copy for 2118113584Ssimokawa per packet mode */ 2119120660Ssimokawa rb->xfer = fw_xfer_alloc_buf(M_FWXFER, 0, /* XXX */ 2120113584Ssimokawa vec[0].iov_len); 2121169131Ssimokawa if (rb->xfer == NULL) 2122169131Ssimokawa return; 2123120660Ssimokawa fw_rcv_copy(rb) 2124103285Sikob s = splfw(); 2125103285Sikob xferq->queued++; 2126120660Ssimokawa STAILQ_INSERT_TAIL(&xferq->q, rb->xfer, link); 2127103285Sikob splx(s); 2128120660Ssimokawa sc = device_get_softc(rb->fc->bdev); 2129127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2130127468Ssimokawa if (&xferq->rsel.si_pid != 0) 2131127468Ssimokawa#else 2132103285Sikob if (SEL_WAITING(&xferq->rsel)) 2133103285Sikob#endif 2134122352Stanimura selwakeuppri(&xferq->rsel, FWPRI); 2135103285Sikob if (xferq->flag & FWXFERQ_WAKEUP) { 2136103285Sikob xferq->flag &= ~FWXFERQ_WAKEUP; 2137103285Sikob wakeup((caddr_t)xferq); 2138103285Sikob } 2139103285Sikob if (xferq->flag & FWXFERQ_HANDLER) { 2140103285Sikob xferq->hand(xferq); 2141103285Sikob } 2142103285Sikob return; 2143103285Sikob break; 2144103285Sikob } 2145120660Ssimokawa#endif 2146103285Sikob default: 2147188722Ssbruno device_printf(rb->fc->bdev,"%s: unknown tcode %d\n", 2148188722Ssbruno __func__, tcode); 2149103285Sikob break; 2150103285Sikob } 2151103285Sikob} 2152106815Ssimokawa 2153103285Sikob/* 2154103285Sikob * Post process for Bus Manager election process. 2155103285Sikob */ 2156103285Sikobstatic void 2157103285Sikobfw_try_bmr_callback(struct fw_xfer *xfer) 2158103285Sikob{ 2159103285Sikob struct firewire_comm *fc; 2160109422Ssimokawa int bmr; 2161103285Sikob 2162109422Ssimokawa if (xfer == NULL) 2163109422Ssimokawa return; 2164103285Sikob fc = xfer->fc; 2165109422Ssimokawa if (xfer->resp != 0) 2166103285Sikob goto error; 2167120660Ssimokawa if (xfer->recv.payload == NULL) 2168103285Sikob goto error; 2169120660Ssimokawa if (xfer->recv.hdr.mode.lres.rtcode != FWRCODE_COMPLETE) 2170103285Sikob goto error; 2171109422Ssimokawa 2172120660Ssimokawa bmr = ntohl(xfer->recv.payload[0]); 2173109422Ssimokawa if (bmr == 0x3f) 2174109422Ssimokawa bmr = fc->nodeid; 2175109422Ssimokawa 2176109422Ssimokawa CSRARC(fc, BUS_MGR_ID) = fc->set_bmr(fc, bmr & 0x3f); 2177120660Ssimokawa fw_xfer_free_buf(xfer); 2178114909Ssimokawa fw_bmr(fc); 2179114909Ssimokawa return; 2180114909Ssimokawa 2181103285Sikoberror: 2182114909Ssimokawa device_printf(fc->bdev, "bus manager election failed\n"); 2183120660Ssimokawa fw_xfer_free_buf(xfer); 2184103285Sikob} 2185106815Ssimokawa 2186113584Ssimokawa 2187103285Sikob/* 2188103285Sikob * To candidate Bus Manager election process. 2189103285Sikob */ 2190110270Ssimokawastatic void 2191106815Ssimokawafw_try_bmr(void *arg) 2192103285Sikob{ 2193103285Sikob struct fw_xfer *xfer; 2194103285Sikob struct firewire_comm *fc = (struct firewire_comm *)arg; 2195103285Sikob struct fw_pkt *fp; 2196103285Sikob int err = 0; 2197103285Sikob 2198120660Ssimokawa xfer = fw_xfer_alloc_buf(M_FWXFER, 8, 4); 2199103285Sikob if(xfer == NULL){ 2200103285Sikob return; 2201103285Sikob } 2202120660Ssimokawa xfer->send.spd = 0; 2203103285Sikob fc->status = FWBUSMGRELECT; 2204103285Sikob 2205120660Ssimokawa fp = &xfer->send.hdr; 2206113584Ssimokawa fp->mode.lreq.dest_hi = 0xffff; 2207103285Sikob fp->mode.lreq.tlrt = 0; 2208103285Sikob fp->mode.lreq.tcode = FWTCODE_LREQ; 2209103285Sikob fp->mode.lreq.pri = 0; 2210103285Sikob fp->mode.lreq.src = 0; 2211113584Ssimokawa fp->mode.lreq.len = 8; 2212120660Ssimokawa fp->mode.lreq.extcode = EXTCODE_CMP_SWAP; 2213120660Ssimokawa fp->mode.lreq.dst = FWLOCALBUS | fc->irm; 2214113584Ssimokawa fp->mode.lreq.dest_lo = 0xf0000000 | BUS_MGR_ID; 2215120660Ssimokawa xfer->send.payload[0] = htonl(0x3f); 2216120660Ssimokawa xfer->send.payload[1] = htonl(fc->nodeid); 2217167632Ssimokawa xfer->hand = fw_try_bmr_callback; 2218103285Sikob 2219103285Sikob err = fw_asyreq(fc, -1, xfer); 2220103285Sikob if(err){ 2221120660Ssimokawa fw_xfer_free_buf(xfer); 2222103285Sikob return; 2223103285Sikob } 2224103285Sikob return; 2225103285Sikob} 2226106543Ssimokawa 2227106543Ssimokawa#ifdef FW_VMACCESS 2228103285Sikob/* 2229103285Sikob * Software implementation for physical memory block access. 2230103285Sikob * XXX:Too slow, usef for debug purpose only. 2231103285Sikob */ 2232106815Ssimokawastatic void 2233106815Ssimokawafw_vmaccess(struct fw_xfer *xfer){ 2234103285Sikob struct fw_pkt *rfp, *sfp = NULL; 2235129585Sdfr uint32_t *ld = (uint32_t *)xfer->recv.buf; 2236103285Sikob 2237113584Ssimokawa printf("vmaccess spd:%2x len:%03x data:%08x %08x %08x %08x\n", 2238113584Ssimokawa xfer->spd, xfer->recv.len, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 2239103285Sikob printf("vmaccess data:%08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 2240103285Sikob if(xfer->resp != 0){ 2241103285Sikob fw_xfer_free( xfer); 2242103285Sikob return; 2243103285Sikob } 2244103285Sikob if(xfer->recv.buf == NULL){ 2245103285Sikob fw_xfer_free( xfer); 2246103285Sikob return; 2247103285Sikob } 2248103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 2249103285Sikob switch(rfp->mode.hdr.tcode){ 2250103285Sikob /* XXX need fix for 64bit arch */ 2251103285Sikob case FWTCODE_WREQB: 2252110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2253103285Sikob xfer->send.len = 12; 2254103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2255103285Sikob bcopy(rfp->mode.wreqb.payload, 2256103285Sikob (caddr_t)ntohl(rfp->mode.wreqb.dest_lo), ntohs(rfp->mode.wreqb.len)); 2257103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2258103285Sikob sfp->mode.wres.rtcode = 0; 2259103285Sikob break; 2260103285Sikob case FWTCODE_WREQQ: 2261110195Ssimokawa xfer->send.buf = malloc(12, M_FW, M_NOWAIT); 2262103285Sikob xfer->send.len = 12; 2263103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 2264129585Sdfr *((uint32_t *)(ntohl(rfp->mode.wreqb.dest_lo))) = rfp->mode.wreqq.data; 2265103285Sikob sfp->mode.wres.rtcode = 0; 2266103285Sikob break; 2267103285Sikob case FWTCODE_RREQB: 2268110195Ssimokawa xfer->send.buf = malloc(16 + rfp->mode.rreqb.len, M_FW, M_NOWAIT); 2269103285Sikob xfer->send.len = 16 + ntohs(rfp->mode.rreqb.len); 2270103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2271103285Sikob bcopy((caddr_t)ntohl(rfp->mode.rreqb.dest_lo), 2272129585Sdfr sfp->mode.rresb.payload, (uint16_t)ntohs(rfp->mode.rreqb.len)); 2273103285Sikob sfp->mode.rresb.tcode = FWTCODE_RRESB; 2274103285Sikob sfp->mode.rresb.len = rfp->mode.rreqb.len; 2275103285Sikob sfp->mode.rresb.rtcode = 0; 2276103285Sikob sfp->mode.rresb.extcode = 0; 2277103285Sikob break; 2278103285Sikob case FWTCODE_RREQQ: 2279110195Ssimokawa xfer->send.buf = malloc(16, M_FW, M_NOWAIT); 2280103285Sikob xfer->send.len = 16; 2281103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 2282129585Sdfr sfp->mode.rresq.data = *(uint32_t *)(ntohl(rfp->mode.rreqq.dest_lo)); 2283103285Sikob sfp->mode.wres.tcode = FWTCODE_RRESQ; 2284103285Sikob sfp->mode.rresb.rtcode = 0; 2285103285Sikob break; 2286103285Sikob default: 2287103285Sikob fw_xfer_free( xfer); 2288103285Sikob return; 2289103285Sikob } 2290103285Sikob sfp->mode.hdr.dst = rfp->mode.hdr.src; 2291103285Sikob xfer->dst = ntohs(rfp->mode.hdr.src); 2292167632Ssimokawa xfer->hand = fw_xfer_free; 2293103285Sikob 2294103285Sikob sfp->mode.hdr.tlrt = rfp->mode.hdr.tlrt; 2295103285Sikob sfp->mode.hdr.pri = 0; 2296103285Sikob 2297103285Sikob fw_asyreq(xfer->fc, -1, xfer); 2298103285Sikob/**/ 2299103285Sikob return; 2300103285Sikob} 2301106543Ssimokawa#endif 2302106543Ssimokawa 2303103285Sikob/* 2304103285Sikob * CRC16 check-sum for IEEE1394 register blocks. 2305103285Sikob */ 2306129585Sdfruint16_t 2307129585Sdfrfw_crc16(uint32_t *ptr, uint32_t len){ 2308129585Sdfr uint32_t i, sum, crc = 0; 2309103285Sikob int shift; 2310103285Sikob len = (len + 3) & ~3; 2311103285Sikob for(i = 0 ; i < len ; i+= 4){ 2312103285Sikob for( shift = 28 ; shift >= 0 ; shift -= 4){ 2313103285Sikob sum = ((crc >> 12) ^ (ptr[i/4] >> shift)) & 0xf; 2314103285Sikob crc = (crc << 4) ^ ( sum << 12 ) ^ ( sum << 5) ^ sum; 2315103285Sikob } 2316103285Sikob crc &= 0xffff; 2317103285Sikob } 2318129585Sdfr return((uint16_t) crc); 2319103285Sikob} 2320106815Ssimokawa 2321188722Ssbruno/* 2322188722Ssbruno * Find the root node, if it is not 2323188722Ssbruno * Cycle Master Capable, then we should 2324188722Ssbruno * override this and become the Cycle 2325188722Ssbruno * Master 2326188722Ssbruno */ 2327110270Ssimokawastatic int 2328110072Ssimokawafw_bmr(struct firewire_comm *fc) 2329110072Ssimokawa{ 2330110072Ssimokawa struct fw_device fwdev; 2331113584Ssimokawa union fw_self_id *self_id; 2332110072Ssimokawa int cmstr; 2333129585Sdfr uint32_t quad; 2334110072Ssimokawa 2335113584Ssimokawa /* Check to see if the current root node is cycle master capable */ 2336129274Sdfr self_id = fw_find_self_id(fc, fc->max_node); 2337113584Ssimokawa if (fc->max_node > 0) { 2338114909Ssimokawa /* XXX check cmc bit of businfo block rather than contender */ 2339114909Ssimokawa if (self_id->p0.link_active && self_id->p0.contender) 2340113584Ssimokawa cmstr = fc->max_node; 2341114909Ssimokawa else { 2342114909Ssimokawa device_printf(fc->bdev, 2343114909Ssimokawa "root node is not cycle master capable\n"); 2344114909Ssimokawa /* XXX shall we be the cycle master? */ 2345113584Ssimokawa cmstr = fc->nodeid; 2346114909Ssimokawa /* XXX need bus reset */ 2347114909Ssimokawa } 2348113584Ssimokawa } else 2349113584Ssimokawa cmstr = -1; 2350114909Ssimokawa 2351188722Ssbruno device_printf(fc->bdev, "bus manager %d %s\n", 2352188722Ssbruno CSRARC(fc, BUS_MGR_ID), 2353188722Ssbruno (CSRARC(fc, BUS_MGR_ID) != fc->nodeid) ? "(me)" : ""); 2354114909Ssimokawa if(CSRARC(fc, BUS_MGR_ID) != fc->nodeid) { 2355114909Ssimokawa /* We are not the bus manager */ 2356114909Ssimokawa return(0); 2357114909Ssimokawa } 2358114909Ssimokawa 2359114909Ssimokawa /* Optimize gapcount */ 2360113584Ssimokawa if(fc->max_hop <= MAX_GAPHOP ) 2361113584Ssimokawa fw_phy_config(fc, cmstr, gap_cnt[fc->max_hop]); 2362110072Ssimokawa /* If we are the cycle master, nothing to do */ 2363113584Ssimokawa if (cmstr == fc->nodeid || cmstr == -1) 2364110072Ssimokawa return 0; 2365110072Ssimokawa /* Bus probe has not finished, make dummy fwdev for cmstr */ 2366110072Ssimokawa bzero(&fwdev, sizeof(fwdev)); 2367110072Ssimokawa fwdev.fc = fc; 2368110072Ssimokawa fwdev.dst = cmstr; 2369110072Ssimokawa fwdev.speed = 0; 2370110072Ssimokawa fwdev.maxrec = 8; /* 512 */ 2371110072Ssimokawa fwdev.status = FWDEVINIT; 2372110072Ssimokawa /* Set cmstr bit on the cycle master */ 2373120660Ssimokawa quad = htonl(1 << 8); 2374110072Ssimokawa fwmem_write_quad(&fwdev, NULL, 0/*spd*/, 2375120660Ssimokawa 0xffff, 0xf0000000 | STATE_SET, &quad, fw_asy_callback_free); 2376110072Ssimokawa 2377110072Ssimokawa return 0; 2378110072Ssimokawa} 2379110072Ssimokawa 2380170374Ssimokawaint 2381170374Ssimokawafw_open_isodma(struct firewire_comm *fc, int tx) 2382170374Ssimokawa{ 2383170374Ssimokawa struct fw_xferq **xferqa; 2384170374Ssimokawa struct fw_xferq *xferq; 2385170374Ssimokawa int i; 2386170374Ssimokawa 2387170374Ssimokawa if (tx) 2388170374Ssimokawa xferqa = &fc->it[0]; 2389170374Ssimokawa else 2390170374Ssimokawa xferqa = &fc->ir[0]; 2391170374Ssimokawa 2392170374Ssimokawa FW_GLOCK(fc); 2393170374Ssimokawa for (i = 0; i < fc->nisodma; i ++) { 2394170374Ssimokawa xferq = xferqa[i]; 2395170374Ssimokawa if ((xferq->flag & FWXFERQ_OPEN) == 0) { 2396170374Ssimokawa xferq->flag |= FWXFERQ_OPEN; 2397170374Ssimokawa break; 2398170374Ssimokawa } 2399170374Ssimokawa } 2400170374Ssimokawa if (i == fc->nisodma) { 2401170374Ssimokawa printf("no free dma channel (tx=%d)\n", tx); 2402170374Ssimokawa i = -1; 2403170374Ssimokawa } 2404170374Ssimokawa FW_GUNLOCK(fc); 2405170374Ssimokawa return (i); 2406170374Ssimokawa} 2407170374Ssimokawa 2408118455Ssimokawastatic int 2409118455Ssimokawafw_modevent(module_t mode, int type, void *data) 2410118455Ssimokawa{ 2411118455Ssimokawa int err = 0; 2412127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2413118455Ssimokawa static eventhandler_tag fwdev_ehtag = NULL; 2414118455Ssimokawa#endif 2415118455Ssimokawa 2416118455Ssimokawa switch (type) { 2417118455Ssimokawa case MOD_LOAD: 2418127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2419118455Ssimokawa fwdev_ehtag = EVENTHANDLER_REGISTER(dev_clone, 2420118455Ssimokawa fwdev_clone, 0, 1000); 2421118455Ssimokawa#endif 2422118455Ssimokawa break; 2423118455Ssimokawa case MOD_UNLOAD: 2424127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2425118455Ssimokawa if (fwdev_ehtag != NULL) 2426118455Ssimokawa EVENTHANDLER_DEREGISTER(dev_clone, fwdev_ehtag); 2427118455Ssimokawa#endif 2428118455Ssimokawa break; 2429118455Ssimokawa case MOD_SHUTDOWN: 2430118455Ssimokawa break; 2431132199Sphk default: 2432132199Sphk return (EOPNOTSUPP); 2433118455Ssimokawa } 2434118455Ssimokawa return (err); 2435118455Ssimokawa} 2436118455Ssimokawa 2437118455Ssimokawa 2438127468Ssimokawa#ifdef __DragonFly__ 2439127468SsimokawaDECLARE_DUMMY_MODULE(firewire); 2440127468Ssimokawa#endif 2441118455SsimokawaDRIVER_MODULE(firewire,fwohci,firewire_driver,firewire_devclass,fw_modevent,0); 2442103285SikobMODULE_VERSION(firewire, 1); 2443