Home | History | Annotate | Line # | Download | only in ieee1394
fwohci.c revision 1.7
      1 /*-
      2  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *        This product includes software developed by the NetBSD
     19  *        Foundation, Inc. and its contributors.
     20  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  *    contributors may be used to endorse or promote products derived
     22  *    from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * IEEE1394 Open Host Controller Interface
     39  *	based on OHCI Specification 1.1 (January 6, 2000)
     40  * The first version to support network interface part is wrtten by
     41  * Atsushi Onoe <onoe (at) netbsd.org>.
     42  */
     43 
     44 #include "opt_inet.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/types.h>
     49 #include <sys/socket.h>
     50 #include <sys/callout.h>
     51 #include <sys/device.h>
     52 #include <sys/kernel.h>
     53 #include <sys/malloc.h>
     54 #include <sys/mbuf.h>
     55 
     56 #if __NetBSD_Version__ >= 105010000
     57 #include <uvm/uvm_extern.h>
     58 #else
     59 #include <vm/vm.h>
     60 #endif
     61 
     62 #include <machine/bus.h>
     63 
     64 #include <dev/ieee1394/ieee1394reg.h>
     65 #include <dev/ieee1394/fwohcireg.h>
     66 
     67 #include <dev/ieee1394/ieee1394var.h>
     68 #include <dev/ieee1394/fwohcivar.h>
     69 
     70 static const char * const ieee1394_speeds[] = { IEEE1394_SPD_STRINGS };
     71 
     72 #if 0
     73 static int fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
     74 			       bus_dmamap_t *mapp, caddr_t *kvap, int flags);
     75 #endif
     76 static void fwohci_hw_init(struct fwohci_softc *);
     77 static void fwohci_power(int, void *);
     78 static void fwohci_shutdown(void *);
     79 
     80 static int  fwohci_desc_alloc(struct fwohci_softc *);
     81 
     82 static int  fwohci_ctx_alloc(struct fwohci_softc *, struct fwohci_ctx **,
     83 		int, int);
     84 static void fwohci_ctx_init(struct fwohci_softc *, struct fwohci_ctx *);
     85 
     86 static int  fwohci_buf_alloc(struct fwohci_softc *, struct fwohci_buf *);
     87 static void fwohci_buf_free(struct fwohci_softc *, struct fwohci_buf *);
     88 static void fwohci_buf_init(struct fwohci_softc *);
     89 static void fwohci_buf_start(struct fwohci_softc *);
     90 static void fwohci_buf_stop(struct fwohci_softc *);
     91 static void fwohci_buf_next(struct fwohci_softc *, struct fwohci_ctx *);
     92 static int  fwohci_buf_pktget(struct fwohci_softc *, struct fwohci_ctx *,
     93 		caddr_t *, int);
     94 static int  fwohci_buf_input(struct fwohci_softc *, struct fwohci_ctx *,
     95 		struct fwohci_pkt *);
     96 
     97 static u_int8_t fwohci_phy_read(struct fwohci_softc *, u_int8_t);
     98 static void fwohci_phy_write(struct fwohci_softc *, u_int8_t, u_int8_t);
     99 static void fwohci_phy_busreset(struct fwohci_softc *);
    100 static void fwohci_phy_input(struct fwohci_softc *, struct fwohci_pkt *);
    101 
    102 static int  fwohci_handler_set(struct fwohci_softc *, int, u_int32_t, u_int32_t,
    103 		int (*)(struct fwohci_softc *, void *, struct fwohci_pkt *),
    104 		void *);
    105 
    106 static void fwohci_arrq_input(struct fwohci_softc *, struct fwohci_ctx *);
    107 static void fwohci_arrs_input(struct fwohci_softc *, struct fwohci_ctx *);
    108 static void fwohci_ir_input(struct fwohci_softc *, struct fwohci_ctx *);
    109 
    110 static int  fwohci_at_output(struct fwohci_softc *, struct fwohci_ctx *,
    111 		struct fwohci_pkt *);
    112 static void fwohci_at_done(struct fwohci_softc *, struct fwohci_ctx *);
    113 static void fwohci_atrs_output(struct fwohci_softc *, int, struct fwohci_pkt *,
    114 		struct fwohci_pkt *);
    115 
    116 static void fwohci_configrom_init(struct fwohci_softc *);
    117 
    118 static void fwohci_selfid_init(struct fwohci_softc *);
    119 static int  fwohci_selfid_input(struct fwohci_softc *);
    120 
    121 static void fwohci_csr_init(struct fwohci_softc *);
    122 static int  fwohci_csr_input(struct fwohci_softc *, void *,
    123 		struct fwohci_pkt *);
    124 
    125 static void fwohci_uid_collect(struct fwohci_softc *);
    126 static int  fwohci_uid_input(struct fwohci_softc *, void *,
    127 		struct fwohci_pkt *);
    128 static int  fwohci_uid_lookup(struct fwohci_softc *, u_int8_t *);
    129 
    130 static int  fwohci_if_inreg(struct device *, u_int32_t, u_int32_t,
    131 		void (*)(struct device *, struct mbuf *));
    132 static int  fwohci_if_input(struct fwohci_softc *, void *, struct fwohci_pkt *);
    133 static int  fwohci_if_output(struct device *, struct mbuf *,
    134 		void (*)(struct device *, struct mbuf *));
    135 
    136 int
    137 fwohci_init(struct fwohci_softc *sc, const struct evcnt *ev)
    138 {
    139 	int i;
    140 	u_int32_t val;
    141 #if 0
    142 	int error;
    143 #endif
    144 
    145 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ev,
    146 	    sc->sc_sc1394.sc1394_dev.dv_xname, "intr");
    147 
    148 	/*
    149 	 * Wait for reset completion
    150 	 */
    151 	for (i = 0; i < OHCI_LOOP; i++) {
    152 		val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
    153 		if ((val & OHCI_HCControl_SoftReset) == 0)
    154 			break;
    155 	}
    156 
    157 	/* What dialect of OHCI is this device?
    158 	 */
    159 	val = OHCI_CSR_READ(sc, OHCI_REG_Version);
    160 	printf("%s: OHCI %u.%u", sc->sc_sc1394.sc1394_dev.dv_xname,
    161 	    OHCI_Version_GET_Version(val), OHCI_Version_GET_Revision(val));
    162 
    163 	/* Is the Global UID ROM present?
    164 	 */
    165 	if ((val & OHCI_Version_GUID_ROM) == 0) {
    166 		printf("\n%s: fatal: no global UID ROM\n", sc->sc_sc1394.sc1394_dev.dv_xname);
    167 		return -1;
    168 	} else {
    169 
    170 		/* Extract the Global UID
    171 		 */
    172 		val = OHCI_CSR_READ(sc, OHCI_REG_GUIDHi);
    173 		sc->sc_sc1394.sc1394_guid[0] = (val >> 24) & 0xff;
    174 		sc->sc_sc1394.sc1394_guid[1] = (val >> 16) & 0xff;
    175 		sc->sc_sc1394.sc1394_guid[2] = (val >>  8) & 0xff;
    176 		sc->sc_sc1394.sc1394_guid[3] = (val >>  0) & 0xff;
    177 
    178 		val = OHCI_CSR_READ(sc, OHCI_REG_GUIDLo);
    179 		sc->sc_sc1394.sc1394_guid[4] = (val >> 24) & 0xff;
    180 		sc->sc_sc1394.sc1394_guid[5] = (val >> 16) & 0xff;
    181 		sc->sc_sc1394.sc1394_guid[6] = (val >>  8) & 0xff;
    182 		sc->sc_sc1394.sc1394_guid[7] = (val >>  0) & 0xff;
    183 	}
    184 
    185 	printf(", %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    186 	    sc->sc_sc1394.sc1394_guid[0], sc->sc_sc1394.sc1394_guid[1],
    187 	    sc->sc_sc1394.sc1394_guid[2], sc->sc_sc1394.sc1394_guid[3],
    188 	    sc->sc_sc1394.sc1394_guid[4], sc->sc_sc1394.sc1394_guid[5],
    189 	    sc->sc_sc1394.sc1394_guid[6], sc->sc_sc1394.sc1394_guid[7]);
    190 
    191 	/* Get the maximum link speed and receive size
    192 	 */
    193 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    194 	sc->sc_sc1394.sc1394_link_speed =
    195 	    (val & OHCI_BusOptions_LinkSpd_MASK)
    196 		>> OHCI_BusOptions_LinkSpd_BITPOS;
    197 	if (sc->sc_sc1394.sc1394_link_speed < IEEE1394_SPD_MAX) {
    198 		printf(", %s", ieee1394_speeds[sc->sc_sc1394.sc1394_link_speed]);
    199 	} else {
    200 		printf(", unknown speed %u", sc->sc_sc1394.sc1394_link_speed);
    201 	}
    202 
    203 	/* MaxRec is encoded as log2(max_rec_octets)-1
    204 	 */
    205 	sc->sc_sc1394.sc1394_max_receive =
    206 	    1 << (((val & OHCI_BusOptions_MaxRec_MASK)
    207 		       >> OHCI_BusOptions_MaxRec_BITPOS) + 1);
    208 	printf(", %u max_rec", sc->sc_sc1394.sc1394_max_receive);
    209 
    210 	/*
    211 	 * Count how many isochronous ctx we have.
    212 	 */
    213 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
    214 	val = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntMaskClear);
    215 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskClear, ~0);
    216 	for (i = 0; val != 0; val >>= 1) {
    217 		if (val & 0x1)
    218 			i++;
    219 	}
    220 	sc->sc_isoctx = i;
    221 	printf(", %d iso_ctx", sc->sc_isoctx);
    222 
    223 	printf("\n");
    224 
    225 #if 0
    226 	error = fwohci_dnamem_alloc(sc, OHCI_CONFIG_SIZE, OHCI_CONFIG_ALIGNMENT,
    227 				    &sc->sc_configrom_map,
    228 				    (caddr_t *) &sc->sc_configrom,
    229 				    BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    230 	return error;
    231 #endif
    232 
    233 	/*
    234 	 * Enable Link Power
    235 	 */
    236 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
    237 
    238 	/*
    239 	 * Allocate descriptors
    240 	 */
    241 	if (fwohci_desc_alloc(sc))
    242 		return -1;
    243 
    244 	/*
    245 	 * Allocate DMA Context
    246 	 */
    247 	fwohci_ctx_alloc(sc, &sc->sc_ctx_arrq, OHCI_BUF_ARRQ_CNT,
    248 	    OHCI_CTX_ASYNC_RX_REQUEST);
    249 	fwohci_ctx_alloc(sc, &sc->sc_ctx_arrs, OHCI_BUF_ARRS_CNT,
    250 	    OHCI_CTX_ASYNC_RX_RESPONSE);
    251 	fwohci_ctx_alloc(sc, &sc->sc_ctx_atrq, OHCI_BUF_ATRQ_CNT,
    252 	    OHCI_CTX_ASYNC_TX_REQUEST);
    253 	fwohci_ctx_alloc(sc, &sc->sc_ctx_atrs, OHCI_BUF_ATRS_CNT,
    254 	    OHCI_CTX_ASYNC_TX_RESPONSE);
    255 	sc->sc_ctx_ir = malloc(sizeof(sc->sc_ctx_ir[0]) * sc->sc_isoctx,
    256 	    M_DEVBUF, M_WAITOK);
    257 	for (i = 0; i < sc->sc_isoctx; i++) {
    258 		fwohci_ctx_alloc(sc, &sc->sc_ctx_ir[i], OHCI_BUF_IR_CNT, i);
    259 		sc->sc_ctx_ir[i]->fc_ppbmode = 1;
    260 	}
    261 
    262 	/*
    263 	 * Allocate buffer for configuration ROM and SelfID buffer
    264 	 */
    265 	fwohci_buf_alloc(sc, &sc->sc_buf_cnfrom);
    266 	fwohci_buf_alloc(sc, &sc->sc_buf_selfid);
    267 
    268 	/*
    269 	 * establish hooks for shutdown and suspend/resume
    270 	 */
    271 	sc->sc_shutdownhook = shutdownhook_establish(fwohci_shutdown, sc);
    272 	sc->sc_powerhook = powerhook_establish(fwohci_power, sc);
    273 	callout_init(&sc->sc_selfid_callout);
    274 
    275 	/*
    276 	 * Initialize hardware registers.
    277 	 */
    278 	fwohci_hw_init(sc);
    279 
    280 	/*
    281 	 * Initiate Bus Reset
    282 	 */
    283 	config_defer(&sc->sc_sc1394.sc1394_dev,
    284 	    (void (*)(struct device *))fwohci_phy_busreset);
    285 
    286 	sc->sc_sc1394.sc1394_ifinreg = fwohci_if_inreg;
    287 	sc->sc_sc1394.sc1394_ifoutput = fwohci_if_output;
    288 	sc->sc_sc1394.sc1394_if = config_found(&sc->sc_sc1394.sc1394_dev,
    289 	    "fw", fwohci_print);
    290 
    291 	return 0;
    292 }
    293 
    294 int
    295 fwohci_intr(void *arg)
    296 {
    297 	struct fwohci_softc * const sc = arg;
    298 	int i;
    299 	int progress = 0;
    300 	u_int32_t intmask, iso;
    301 
    302 	for (;;) {
    303 		intmask = OHCI_CSR_READ(sc, OHCI_REG_IntEventClear);
    304 		if (intmask == 0)
    305 			return progress;
    306 		OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    307 		    intmask & ~OHCI_Int_BusReset);
    308 #ifdef FW_DEBUG
    309 		printf("%s: intmask=0x%08x:", sc->sc_sc1394.sc1394_dev.dv_xname, intmask);
    310 		if (intmask & OHCI_Int_CycleTooLong)
    311 			printf(" CycleTooLong");
    312 		if (intmask & OHCI_Int_UnrecoverableError)
    313 			printf(" UnrecoverableError");
    314 		if (intmask & OHCI_Int_CycleInconsistent)
    315 			printf(" CycleInconsistent");
    316 		if (intmask & OHCI_Int_BusReset)
    317 			printf(" BusReset");
    318 		if (intmask & OHCI_Int_SelfIDComplete)
    319 			printf(" SelfIDComplete");
    320 		if (intmask & OHCI_Int_LockRespErr)
    321 			printf(" LockRespErr");
    322 		if (intmask & OHCI_Int_PostedWriteErr)
    323 			printf(" PostedWriteErr");
    324 		if (intmask & OHCI_Int_ReqTxComplete)
    325 			printf(" ReqTxComplete(0x%04x)",
    326 			    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_REQUEST,
    327 			    OHCI_SUBREG_ContextControlClear));
    328 		if (intmask & OHCI_Int_RespTxComplete)
    329 			printf(" RespTxComplete(0x%04x)",
    330 			    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
    331 			    OHCI_SUBREG_ContextControlClear));
    332 		if (intmask & OHCI_Int_ARRS)
    333 			printf(" ARRS(0x%04x)",
    334 			    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
    335 			    OHCI_SUBREG_ContextControlClear));
    336 		if (intmask & OHCI_Int_ARRQ)
    337 			printf(" ARRQ(0x%04x)",
    338 			    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_REQUEST,
    339 			    OHCI_SUBREG_ContextControlClear));
    340 		if (intmask & OHCI_Int_IsochRx)
    341 			printf(" IsochRx(0x%08x)",
    342 			    OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntEventClear));
    343 		if (intmask & OHCI_Int_IsochTx)
    344 			printf(" IsochTx(0x%08x)",
    345 			    OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntEventClear));
    346 		if (intmask & OHCI_Int_RQPkt)
    347 			printf(" RQPkt(0x%04x)",
    348 			    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_REQUEST,
    349 			    OHCI_SUBREG_ContextControlClear));
    350 		if (intmask & OHCI_Int_RSPkt)
    351 			printf(" RSPkt(0x%04x)",
    352 			    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
    353 			    OHCI_SUBREG_ContextControlClear));
    354 		printf("\n");
    355 #endif /* FW_DEBUG */
    356 		if (intmask & OHCI_Int_BusReset) {
    357 			/*
    358 			 * According to OHCI spec 6.1.1 "busReset",
    359 			 * All asynchronous transmit must be stopped before
    360 			 * clearing BusReset.  Moreover, the BusReset
    361 			 * interrupt bit should not be cleared during the
    362 			 * SelfID phase.  Thus we turned off interrupt mask
    363 			 * bit of BusReset instead until SelfID completion
    364 			 * or SelfID timeout.
    365 			 */
    366 			OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear,
    367 			    OHCI_Int_BusReset);
    368 			fwohci_buf_stop(sc);
    369 			if (sc->sc_uidtbl != NULL) {
    370 				free(sc->sc_uidtbl, M_DEVBUF);
    371 				sc->sc_uidtbl = NULL;
    372 			}
    373 			callout_reset(&sc->sc_selfid_callout,
    374 			    OHCI_SELFID_TIMEOUT,
    375 			    (void (*)(void *))fwohci_phy_busreset, sc);
    376 			sc->sc_rootid = 0;
    377 			sc->sc_irmid = IEEE1394_BCAST_PHY_ID;
    378 		}
    379 
    380 		if (intmask & OHCI_Int_ReqTxComplete)
    381 			fwohci_at_done(sc, sc->sc_ctx_atrq);
    382 		if (intmask & OHCI_Int_RespTxComplete)
    383 			fwohci_at_done(sc, sc->sc_ctx_atrs);
    384 		if (intmask & OHCI_Int_RQPkt)
    385 			fwohci_arrq_input(sc, sc->sc_ctx_arrq);
    386 		if (intmask & OHCI_Int_RSPkt)
    387 			fwohci_arrs_input(sc, sc->sc_ctx_arrs);
    388 
    389 		if (intmask & OHCI_Int_IsochTx) {
    390 			iso = OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntEventClear);
    391 			OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntEventClear, iso);
    392 		}
    393 		if (intmask & OHCI_Int_IsochRx) {
    394 			iso = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntEventClear);
    395 			OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntEventClear, iso);
    396 			for (i = 0; i < sc->sc_isoctx; i++) {
    397 				if (iso & (1 << i))
    398 					fwohci_ir_input(sc, sc->sc_ctx_ir[i]);
    399 			}
    400 		}
    401 
    402 		if (intmask & OHCI_Int_SelfIDComplete) {
    403 			if (fwohci_selfid_input(sc) == 0) {
    404 				callout_stop(&sc->sc_selfid_callout);
    405 				OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    406 				    OHCI_Int_BusReset);
    407 				OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet,
    408 				    OHCI_Int_BusReset);
    409 				fwohci_buf_init(sc);
    410 				fwohci_buf_start(sc);
    411 				fwohci_uid_collect(sc);
    412 			}
    413 		}
    414 
    415 		if (!progress) {
    416 			sc->sc_intrcnt.ev_count++;
    417 			progress = 1;
    418 		}
    419 	}
    420 }
    421 
    422 #if 0
    423 static int
    424 fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
    425 		    bus_dmamap_t *mapp, caddr_t *kvap, int flags)
    426 {
    427 	bus_dma_segment_t segs[1];
    428 	int error, nsegs, steps;
    429 
    430 	steps = 0;
    431 	error = bus_dmamem_alloc(sc->sc_dmat, size, alignment, alignment,
    432 				 segs, 1, &nsegs, flags);
    433 	if (error)
    434 		goto cleanup;
    435 
    436 	steps = 1;
    437 	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, segs[0].ds_len,
    438 			       kvap, flags);
    439 	if (error)
    440 		goto cleanup;
    441 
    442 	if (error == 0)
    443 		error = bus_dmamap_create(sc->sc_dmat, size, 1, alignment,
    444 					  size, flags, mapp);
    445 	if (error)
    446 		goto cleanup;
    447 	if (error == 0)
    448 		error = bus_dmamap_load(sc->sc_dmat, *mapp, *kvap, size, NULL, flags);
    449 	if (error)
    450 		goto cleanup;
    451 
    452 cleanup:
    453 	switch (steps) {
    454 	case 1:
    455 		bus_dmamem_free(sc->sc_dmat, segs, nsegs);
    456 	}
    457 
    458 	return error;
    459 }
    460 #endif
    461 
    462 int
    463 fwohci_print(void *aux, const char *pnp)
    464 {
    465 	char *name = aux;
    466 
    467 	if (pnp)
    468 		printf("%s at %s", name, pnp);
    469 
    470 	return UNCONF;
    471 }
    472 
    473 static void
    474 fwohci_hw_init(struct fwohci_softc *sc)
    475 {
    476 	int i;
    477 	u_int32_t val;
    478 
    479 	/*
    480 	 * Software Reset.
    481 	 */
    482 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
    483 	for (i = 0; i < OHCI_LOOP; i++) {
    484 		val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
    485 		if ((val & OHCI_HCControl_SoftReset) == 0)
    486 			break;
    487 	}
    488 
    489 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
    490 
    491 	/*
    492 	 * First, initilize CSRs with undefined value to default settings.
    493 	 */
    494 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    495 	val |= OHCI_BusOptions_ISC | OHCI_BusOptions_CMC;
    496 #if 0
    497 	val |= OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC;
    498 #else
    499 	val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC);
    500 #endif
    501 	OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
    502 	for (i = 0; i < sc->sc_isoctx; i++) {
    503 		OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlClear,
    504 		    ~0);
    505 	}
    506 	OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear, ~0);
    507 
    508 	fwohci_configrom_init(sc);
    509 	fwohci_selfid_init(sc);
    510 	fwohci_buf_init(sc);
    511 	fwohci_csr_init(sc);
    512 
    513 	/*
    514 	 * Final CSR settings.
    515 	 */
    516 	OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
    517 	    OHCI_LinkControl_CycleTimerEnable |
    518 	    OHCI_LinkControl_RcvSelfID | OHCI_LinkControl_RcvPhyPkt);
    519 
    520 	OHCI_CSR_WRITE(sc, OHCI_REG_ATRetries, 0x00000888);	/*XXX*/
    521 
    522 	/* clear receive filter */
    523 	OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskHiClear, ~0);
    524 	OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskLoClear, ~0);
    525 	OHCI_CSR_WRITE(sc, OHCI_REG_AsynchronousRequestFilterHiSet, 0x80000000);
    526 
    527 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear,
    528 	    OHCI_HCControl_NoByteSwapData | OHCI_HCControl_APhyEnhanceEnable);
    529 
    530 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, ~0);
    531 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset |
    532 	    OHCI_Int_SelfIDComplete | OHCI_Int_IsochRx | OHCI_Int_IsochTx |
    533 	    OHCI_Int_RSPkt | OHCI_Int_RQPkt | OHCI_Int_ARRS | OHCI_Int_ARRQ |
    534 	    OHCI_Int_RespTxComplete | OHCI_Int_ReqTxComplete);
    535 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_CycleTooLong |
    536 	    OHCI_Int_UnrecoverableError | OHCI_Int_CycleInconsistent |
    537 	    OHCI_Int_LockRespErr | OHCI_Int_PostedWriteErr);
    538 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntMaskSet, ~0);
    539 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
    540 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_MasterEnable);
    541 
    542 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LinkEnable);
    543 
    544 	/*
    545 	 * Start the receivers
    546 	 */
    547 	fwohci_buf_start(sc);
    548 }
    549 
    550 static void
    551 fwohci_power(int why, void *arg)
    552 {
    553 	struct fwohci_softc *sc = arg;
    554 	int s;
    555 
    556 	s = splimp();
    557 	if (why == PWR_RESUME) {
    558 		fwohci_hw_init(sc);
    559 		fwohci_phy_busreset(sc);
    560 	} else {
    561 		fwohci_shutdown(sc);
    562 	}
    563 	splx(s);
    564 }
    565 
    566 static void
    567 fwohci_shutdown(void *arg)
    568 {
    569 	struct fwohci_softc *sc = arg;
    570 	u_int32_t val;
    571 
    572 	callout_stop(&sc->sc_selfid_callout);
    573 	/* disable all interrupt */
    574 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, OHCI_Int_MasterEnable);
    575 	fwohci_buf_stop(sc);
    576 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    577 	val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_ISC |
    578 		OHCI_BusOptions_CMC | OHCI_BusOptions_IRMC);
    579 	OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
    580 	fwohci_phy_busreset(sc);
    581 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear, OHCI_HCControl_LPS);
    582 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
    583 }
    584 
    585 /*
    586  * COMMON FUNCTIONS
    587  */
    588 
    589 /*
    590  * read the PHY Register.
    591  */
    592 static u_int8_t
    593 fwohci_phy_read(struct fwohci_softc *sc, u_int8_t reg)
    594 {
    595 	int i;
    596 	u_int32_t val;
    597 
    598 	OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl,
    599 	    OHCI_PhyControl_RdReg | (reg << OHCI_PhyControl_RegAddr_BITPOS));
    600 	for (i = 0; i < OHCI_LOOP; i++) {
    601 		if (OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
    602 		    OHCI_PhyControl_RdDone)
    603 			break;
    604 	}
    605 	val = OHCI_CSR_READ(sc, OHCI_REG_PhyControl);
    606 	return (val & OHCI_PhyControl_RdData) >> OHCI_PhyControl_RdData_BITPOS;
    607 }
    608 
    609 /*
    610  * write the PHY Register.
    611  */
    612 static void
    613 fwohci_phy_write(struct fwohci_softc *sc, u_int8_t reg, u_int8_t val)
    614 {
    615 	int i;
    616 
    617 	OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl, OHCI_PhyControl_WrReg |
    618 	    (reg << OHCI_PhyControl_RegAddr_BITPOS) |
    619 	    (val << OHCI_PhyControl_WrData_BITPOS));
    620 	for (i = 0; i < OHCI_LOOP; i++) {
    621 		if (!(OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
    622 		    OHCI_PhyControl_WrReg))
    623 			break;
    624 	}
    625 }
    626 
    627 /*
    628  * Initiate Bus Reset
    629  */
    630 static void
    631 fwohci_phy_busreset(struct fwohci_softc *sc)
    632 {
    633 	int s;
    634 	u_int8_t val;
    635 
    636 	s = splimp();
    637 	OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    638 	    OHCI_Int_BusReset | OHCI_Int_SelfIDComplete);
    639 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset);
    640 	callout_stop(&sc->sc_selfid_callout);
    641 	val = fwohci_phy_read(sc, 1);
    642 	val = (val & 0x80) |			/* preserve RHB (force root) */
    643 	    0x40 |				/* Initiate Bus Reset */
    644 	    0x3f;				/* default GAP count */
    645 	fwohci_phy_write(sc, 1, val);
    646 	splx(s);
    647 }
    648 
    649 /*
    650  * PHY Packet
    651  */
    652 static void
    653 fwohci_phy_input(struct fwohci_softc *sc, struct fwohci_pkt *pkt)
    654 {
    655 	u_int32_t val;
    656 	u_int8_t key, phyid;
    657 
    658 	val = pkt->fp_hdr[1];
    659 	if (val != ~pkt->fp_hdr[2]) {
    660 		if (val == 0 && ((*pkt->fp_trail & 0x001f0000) >> 16) ==
    661 		    OHCI_CTXCTL_EVENT_BUS_RESET) {
    662 #ifdef FW_DEBUG
    663 			printf("fwohci_phy_input: BusReset: 0x%08x\n",
    664 			    pkt->fp_hdr[2]);
    665 #endif
    666 		} else {
    667 			printf("%s: phy packet corrupted (0x%08x, 0x%08x)\n",
    668 			    sc->sc_sc1394.sc1394_dev.dv_xname, val,
    669 			    pkt->fp_hdr[2]);
    670 		}
    671 		return;
    672 	}
    673 	key = (val & 0xc0000000) >> 30;
    674 	phyid = (val & 0x3f000000) >> 24;
    675 	switch (key) {
    676 	case 0:
    677 #ifdef FW_DEBUG
    678 		printf("fwohci_phy_input: PHY Config from %d:", phyid);
    679 		if (val & 0x00800000)
    680 			printf(" ForceRoot");
    681 		if (val & 0x00400000)
    682 			printf(" Gap=%x", (val & 0x003f0000) >> 16);
    683 		printf("\n");
    684 #endif
    685 		break;
    686 	case 1:
    687 #ifdef FW_DEBUG
    688 		printf("fwohci_phy_input: Link-on from %d\n", phyid);
    689 #endif
    690 		break;
    691 	case 2:
    692 #ifdef FW_DEBUG
    693 		printf("fwohci_phy_input: SelfID from %d:", phyid);
    694 		if (val & 0x00800000) {
    695 			printf(" #%d", (val & 0x00700000) >> 20);
    696 		} else {
    697 			if (val & 0x00400000)
    698 				printf(" LinkActive");
    699 			printf(" Gap=%x", (val & 0x003f0000) >> 16);
    700 			printf(" Spd=S%d", 100 << ((val & 0x0000c000) >> 14));
    701 			if (val & 0x00000800)
    702 				printf(" Cont");
    703 			if (val & 0x00000002)
    704 				printf(" InitiateBusReset");
    705 		}
    706 		if (val & 0x00000001)
    707 			printf(" +");
    708 		printf("\n");
    709 #endif
    710 		break;
    711 	default:
    712 		printf("fwphci_phy_input: Unknown: 0x%08x\n", val);
    713 		break;
    714 	}
    715 }
    716 
    717 /*
    718  * Descriptor for context DMA.
    719  */
    720 static int
    721 fwohci_desc_alloc(struct fwohci_softc *sc)
    722 {
    723 	int error;
    724 
    725 	/*
    726 	 * allocate descriptor buffer
    727 	 */
    728 
    729 	sc->sc_descsize = sizeof(struct fwohci_desc) *
    730 	    (OHCI_BUF_ARRQ_CNT + OHCI_BUF_ARRS_CNT +
    731 	    OHCI_BUF_ATRQ_CNT + OHCI_BUF_ATRS_CNT +
    732 	    OHCI_BUF_IR_CNT * sc->sc_isoctx + 2);
    733 
    734 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_descsize,
    735 	    PAGE_SIZE, 0, &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
    736 		printf("%s: unable to allocate descriptor buffer, error = %d\n",
    737 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    738 		goto fail_0;
    739 	}
    740 
    741 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
    742 	    sc->sc_descsize, (caddr_t *)&sc->sc_desc,
    743 	    BUS_DMA_COHERENT|BUS_DMA_WAITOK)) != 0) {
    744 		printf("%s: unable to map descriptor buffer, error = %d\n",
    745 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    746 		goto fail_1;
    747 	}
    748 
    749 	if ((error = bus_dmamap_create(sc->sc_dmat, sc->sc_descsize,
    750 	    sc->sc_dnseg, sc->sc_descsize, 0, BUS_DMA_WAITOK, &sc->sc_ddmamap))
    751 	    != 0) {
    752 		printf("%s: unable to create descriptor buffer DMA map, "
    753 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
    754 		goto fail_2;
    755 	}
    756 
    757 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
    758 	    sc->sc_descsize, NULL, BUS_DMA_WAITOK)) != 0) {
    759 		printf("%s: unable to load descriptor buffer DMA map, "
    760 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
    761 		goto fail_3;
    762 	}
    763 
    764 	sc->sc_descfree = sc->sc_desc;
    765 
    766 	return 0;
    767 
    768   fail_3:
    769 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
    770   fail_2:
    771 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, sc->sc_descsize);
    772   fail_1:
    773 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
    774   fail_0:
    775 	return error;
    776 }
    777 
    778 /*
    779  * Asyncronous/Isochronous Transmit/Receive Context
    780  */
    781 static int
    782 fwohci_ctx_alloc(struct fwohci_softc *sc, struct fwohci_ctx **fcp,
    783     int bufcnt, int ctx)
    784 {
    785 	int i, error;
    786 	struct fwohci_ctx *fc;
    787 	struct fwohci_buf *fb;
    788 	struct fwohci_desc *fd;
    789 
    790 	fc = malloc(sizeof(*fc) + sizeof(*fb) * bufcnt, M_DEVBUF, M_WAITOK);
    791 	memset(fc, 0, sizeof(*fc) + sizeof(*fb) * bufcnt);
    792 	LIST_INIT(&fc->fc_handler);
    793 	TAILQ_INIT(&fc->fc_buf);
    794 	TAILQ_INIT(&fc->fc_busy);
    795 	fc->fc_ctx = ctx;
    796 	fc->fc_bufcnt = bufcnt;
    797 	fb = (struct fwohci_buf *)&fc[1];
    798 	for (i = 0; i < bufcnt; i++, fb++) {
    799 		if ((error = fwohci_buf_alloc(sc, fb)) != 0)
    800 			goto fail;
    801 #ifdef DIAGNOSTICS
    802 		if ((caddr_t)sc->sc_descfree >=
    803 		    (caddr_t)sc->sc_desc + sc->sc_descsize)
    804 			panic("fwohci_ctx_alloc: descriptor exhausted: %d\n",
    805 			    sc->sc_descfree - sc->sc_desc);
    806 #endif
    807 		fd = sc->sc_descfree++;
    808 		fb->fb_desc = fd;
    809 		fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
    810 		    ((caddr_t)fd - (caddr_t)sc->sc_desc);
    811 		fd->fd_flags = OHCI_DESC_INPUT | OHCI_DESC_STATUS |
    812 		    OHCI_DESC_INTR_ALWAYS | OHCI_DESC_BRANCH;
    813 		fd->fd_reqcount = fb->fb_dmamap->dm_segs[0].ds_len;
    814 		fd->fd_data = fb->fb_dmamap->dm_segs[0].ds_addr;
    815 		TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
    816 	}
    817 	*fcp = fc;
    818 	return 0;
    819 
    820   fail:
    821 	while (i-- > 0)
    822 		fwohci_buf_free(sc, --fb);
    823 	free(fc, M_DEVBUF);
    824 	return error;
    825 }
    826 
    827 static void
    828 fwohci_ctx_init(struct fwohci_softc *sc, struct fwohci_ctx *fc)
    829 {
    830 	struct fwohci_buf *fb, *nfb;
    831 	struct fwohci_desc *fd;
    832 
    833 	for (fb = TAILQ_FIRST(&fc->fc_buf); fb != NULL; fb = nfb) {
    834 		nfb = TAILQ_NEXT(fb, fb_list);
    835 		fb->fb_off = 0;
    836 		fd = fb->fb_desc;
    837 		fd->fd_branch = (nfb != NULL) ? (nfb->fb_daddr | 1) : 0;
    838 		fd->fd_rescount = fd->fd_reqcount;
    839 	}
    840 }
    841 
    842 /*
    843  * DMA data buffer
    844  */
    845 static int
    846 fwohci_buf_alloc(struct fwohci_softc *sc, struct fwohci_buf *fb)
    847 {
    848 	int error;
    849 
    850 	if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
    851 	    PAGE_SIZE, &fb->fb_seg, 1, &fb->fb_nseg, BUS_DMA_WAITOK)) != 0) {
    852 		printf("%s: unable to allocate buffer, error = %d\n",
    853 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    854 		goto fail_0;
    855 	}
    856 
    857 	if ((error = bus_dmamem_map(sc->sc_dmat, &fb->fb_seg,
    858 	    fb->fb_nseg, PAGE_SIZE, &fb->fb_buf, BUS_DMA_WAITOK)) != 0) {
    859 		printf("%s: unable to map buffer, error = %d\n",
    860 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    861 		goto fail_1;
    862 	}
    863 
    864 	if ((error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, fb->fb_nseg,
    865 	    PAGE_SIZE, 0, BUS_DMA_WAITOK, &fb->fb_dmamap)) != 0) {
    866 		printf("%s: unable to create buffer DMA map, "
    867 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
    868 		    error);
    869 		goto fail_2;
    870 	}
    871 
    872 	if ((error = bus_dmamap_load(sc->sc_dmat, fb->fb_dmamap,
    873 	    fb->fb_buf, PAGE_SIZE, NULL, BUS_DMA_WAITOK)) != 0) {
    874 		printf("%s: unable to load buffer DMA map, "
    875 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
    876 		    error);
    877 		goto fail_3;
    878 	}
    879 
    880 	return 0;
    881 
    882 	bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
    883   fail_3:
    884 	bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
    885   fail_2:
    886 	bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
    887   fail_1:
    888 	bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
    889   fail_0:
    890 	return error;
    891 }
    892 
    893 static void
    894 fwohci_buf_free(struct fwohci_softc *sc, struct fwohci_buf *fb)
    895 {
    896 
    897 	bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
    898 	bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
    899 	bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
    900 	bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
    901 }
    902 
    903 static void
    904 fwohci_buf_init(struct fwohci_softc *sc)
    905 {
    906 	int i;
    907 	struct fwohci_buf *fb;
    908 
    909 	/*
    910 	 * Initialize for Asynchronous Transmit Request.
    911 	 */
    912 	while ((fb = TAILQ_FIRST(&sc->sc_ctx_atrq->fc_busy)) != NULL) {
    913 		TAILQ_REMOVE(&sc->sc_ctx_atrq->fc_busy, fb, fb_list);
    914 		if (fb->fb_m != NULL) {
    915 			if (fb->fb_callback != NULL) {
    916 				(*fb->fb_callback)
    917 				    (sc->sc_sc1394.sc1394_if, fb->fb_m);
    918 				fb->fb_callback = NULL;
    919 			} else
    920 				m_freem(fb->fb_m);
    921 			fb->fb_m = NULL;
    922 		}
    923 		TAILQ_INSERT_TAIL(&sc->sc_ctx_atrq->fc_buf, fb, fb_list);
    924 	}
    925 	sc->sc_ctx_atrq->fc_branch = NULL;
    926 
    927 	/*
    928 	 * Initialize for Asynchronous Transmit Response.
    929 	 */
    930 	while ((fb = TAILQ_FIRST(&sc->sc_ctx_atrs->fc_busy)) != NULL) {
    931 		TAILQ_REMOVE(&sc->sc_ctx_atrs->fc_busy, fb, fb_list);
    932 		if (fb->fb_m != NULL) {
    933 			if (fb->fb_callback != NULL) {
    934 				(*fb->fb_callback)
    935 				    (sc->sc_sc1394.sc1394_if, fb->fb_m);
    936 				fb->fb_callback = NULL;
    937 			} else
    938 				m_freem(fb->fb_m);
    939 			fb->fb_m = NULL;
    940 		}
    941 		TAILQ_INSERT_TAIL(&sc->sc_ctx_atrs->fc_buf, fb, fb_list);
    942 	}
    943 	sc->sc_ctx_atrs->fc_branch = NULL;
    944 
    945 	/*
    946 	 * Initialize for Asynchronous Receive Request.
    947 	 */
    948 	fwohci_ctx_init(sc, sc->sc_ctx_arrq);
    949 	fb = TAILQ_FIRST(&sc->sc_ctx_arrq->fc_buf);
    950 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
    951 	    OHCI_SUBREG_CommandPtr, fb->fb_daddr | 1);
    952 
    953 	/*
    954 	 * Initialize for Asynchronous Receive Response.
    955 	 */
    956 	fwohci_ctx_init(sc, sc->sc_ctx_arrs);
    957 	fb = TAILQ_FIRST(&sc->sc_ctx_arrs->fc_buf);
    958 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
    959 	    OHCI_SUBREG_CommandPtr, fb->fb_daddr | 1);
    960 
    961 	/*
    962 	 * Initialize for Isochronous Receive.
    963 	 */
    964 	for (i = 0; i < sc->sc_isoctx; i++) {
    965 		fwohci_ctx_init(sc, sc->sc_ctx_ir[i]);
    966 		fb = TAILQ_FIRST(&sc->sc_ctx_ir[i]->fc_buf);
    967 		OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_CommandPtr,
    968 		    fb->fb_daddr | 1);
    969 		OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlClear,
    970 		    OHCI_CTXCTL_RX_BUFFER_FILL |
    971 		    OHCI_CTXCTL_RX_CYCLE_MATCH_ENABLE |
    972 		    OHCI_CTXCTL_RX_MULTI_CHAN_MODE |
    973 		    OHCI_CTXCTL_RX_DUAL_BUFFER_MODE);
    974 		OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlSet,
    975 		    OHCI_CTXCTL_RX_ISOCH_HEADER);
    976 	}
    977 }
    978 
    979 static void
    980 fwohci_buf_start(struct fwohci_softc *sc)
    981 {
    982 	int i;
    983 
    984 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
    985 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
    986 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
    987 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
    988 	for (i = 0; i < sc->sc_isoctx; i++) {
    989 		if (LIST_FIRST(&sc->sc_ctx_ir[i]->fc_handler) != NULL) {
    990 			OHCI_SYNC_RX_DMA_WRITE(sc, i,
    991 			    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
    992 		}
    993 	}
    994 }
    995 
    996 static void
    997 fwohci_buf_stop(struct fwohci_softc *sc)
    998 {
    999 	int i, j;
   1000 
   1001 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   1002 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1003 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   1004 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1005 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   1006 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1007 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   1008 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1009 	for (i = 0; i < sc->sc_isoctx; i++) {
   1010 		OHCI_SYNC_RX_DMA_WRITE(sc, i,
   1011 		    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1012 	}
   1013 
   1014 	/*
   1015 	 * Make sure the transmitter is stopped.
   1016 	 */
   1017 	for (j = 0; j < OHCI_LOOP; j++) {
   1018 		if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   1019 		    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
   1020 			continue;
   1021 		if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   1022 		    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
   1023 			continue;
   1024 		break;
   1025 	}
   1026 }
   1027 
   1028 static void
   1029 fwohci_buf_next(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1030 {
   1031 	struct fwohci_buf *fb, *tfb;
   1032 
   1033 	while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   1034 		if (fb->fb_off != fb->fb_desc->fd_reqcount ||
   1035 		    fb->fb_desc->fd_rescount != 0)
   1036 			break;
   1037 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1038 		fb->fb_desc->fd_rescount = fb->fb_desc->fd_reqcount;
   1039 		fb->fb_off = 0;
   1040 		fb->fb_desc->fd_branch = 0;
   1041 		tfb = TAILQ_LAST(&fc->fc_buf, fwohci_buf_s);
   1042 		tfb->fb_desc->fd_branch = fb->fb_daddr | 1;
   1043 		TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1044 	}
   1045 }
   1046 
   1047 static int
   1048 fwohci_buf_pktget(struct fwohci_softc *sc, struct fwohci_ctx *fc, caddr_t *pp,
   1049     int len)
   1050 {
   1051 	struct fwohci_buf *fb;
   1052 	struct fwohci_desc *fd;
   1053 	int bufend;
   1054 
   1055 	fb = TAILQ_FIRST(&fc->fc_buf);
   1056   again:
   1057 	fd = fb->fb_desc;
   1058 #ifdef FW_DEBUG
   1059 printf("fwohci_buf_pktget: desc %d, off %d, req %d, res %d\n", fd - sc->sc_desc, fb->fb_off, fd->fd_reqcount, fd->fd_rescount);
   1060 #endif
   1061 	bufend = fd->fd_reqcount - fd->fd_rescount;
   1062 	if (fb->fb_off >= bufend) {
   1063 		if (fc->fc_ppbmode && fb->fb_off > 0) {
   1064 			fb->fb_off = fd->fd_reqcount;
   1065 			fd->fd_rescount = 0;
   1066 		}
   1067 		if (fd->fd_rescount == 0) {
   1068 			if ((fb = TAILQ_NEXT(fb, fb_list)) != NULL)
   1069 				goto again;
   1070 		}
   1071 		return 0;
   1072 	}
   1073 	if (fb->fb_off + len > bufend)
   1074 		len = bufend - fb->fb_off;
   1075 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, fb->fb_off, len,
   1076 	    BUS_DMASYNC_POSTREAD);
   1077 	*pp = fb->fb_buf + fb->fb_off;
   1078 	fb->fb_off += roundup(len, 4);
   1079 	return len;
   1080 }
   1081 
   1082 static int
   1083 fwohci_buf_input(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   1084     struct fwohci_pkt *pkt)
   1085 {
   1086 	caddr_t p;
   1087 	int len, count, i;
   1088 
   1089 	/* get first quadlet */
   1090 	count = 4;
   1091 	if (fc->fc_ppbmode) {
   1092 		/*
   1093 		 * get trailer first, may be bogus data unless status update
   1094 		 * in descriptor is set.
   1095 		 */
   1096 		len = fwohci_buf_pktget(sc, fc, (caddr_t *)&pkt->fp_trail,
   1097 		    sizeof(pkt->fp_trail));
   1098 		if (len <= 0) {
   1099 #ifdef FW_DEBUG
   1100 			printf("fwohci_buf_input: no input (ppb) for %d\n",
   1101 			    fc->fc_ctx);
   1102 #endif
   1103 			return 0;
   1104 		}
   1105 	}
   1106 	len = fwohci_buf_pktget(sc, fc, &p, count);
   1107 	if (len <= 0) {
   1108 #ifdef FW_DEBUG
   1109 		printf("fwohci_buf_input: no input for %d\n", fc->fc_ctx);
   1110 #endif
   1111 		return 0;
   1112 	}
   1113 	pkt->fp_hdr[0] = *(u_int32_t *)p;
   1114 	pkt->fp_tcode = (pkt->fp_hdr[0] & 0x000000f0) >> 4;
   1115 	switch (pkt->fp_tcode) {
   1116 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1117 	case IEEE1394_TCODE_READ_RESP_QUAD:
   1118 		pkt->fp_hlen = 12;
   1119 		pkt->fp_dlen = 4;
   1120 		break;
   1121 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1122 	case IEEE1394_TCODE_READ_RESP_BLOCK:
   1123 	case IEEE1394_TCODE_LOCK_REQ:
   1124 	case IEEE1394_TCODE_LOCK_RESP:
   1125 		pkt->fp_hlen = 16;
   1126 		break;
   1127 	case IEEE1394_TCODE_STREAM_DATA:
   1128 		pkt->fp_hlen = 4;
   1129 		pkt->fp_dlen = pkt->fp_hdr[0] >> 16;
   1130 		break;
   1131 	default:
   1132 		pkt->fp_hlen = 12;
   1133 		pkt->fp_dlen = 0;
   1134 		break;
   1135 	}
   1136 
   1137 	/* get header */
   1138 	while (count < pkt->fp_hlen) {
   1139 		len = fwohci_buf_pktget(sc, fc, &p, pkt->fp_hlen - count);
   1140 		if (len == 0) {
   1141 			printf("fwohci_buf_input: malformed input 1: %d\n",
   1142 			    pkt->fp_hlen - count);
   1143 			return 0;
   1144 		}
   1145 		memcpy((caddr_t)pkt->fp_hdr + count, p, len);
   1146 		count += len;
   1147 	}
   1148 	if (pkt->fp_hlen == 16)
   1149 		pkt->fp_dlen = pkt->fp_hdr[3] >> 16;
   1150 #ifdef FW_DEBUG
   1151 	printf("fwohci_buf_input: tcode=0x%x, hlen=%d, dlen=%d\n",
   1152 	    pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen);
   1153 #endif
   1154 
   1155 	/* get data */
   1156 	count = 0;
   1157 	i = 0;
   1158 	while (count < pkt->fp_dlen) {
   1159 		len = fwohci_buf_pktget(sc, fc,
   1160 		    (caddr_t *)&pkt->fp_iov[i].iov_base,
   1161 		    pkt->fp_dlen - count);
   1162 		if (len == 0) {
   1163 			printf("fwohci_buf_input: malformed input 2: %d\n",
   1164 			    pkt->fp_hlen - count);
   1165 			return 0;
   1166 		}
   1167 		pkt->fp_iov[i++].iov_len = len;
   1168 		count += len;
   1169 	}
   1170 
   1171 	if (!fc->fc_ppbmode) {
   1172 		/* get trailer */
   1173 		len = fwohci_buf_pktget(sc, fc, (caddr_t *)&pkt->fp_trail,
   1174 		    sizeof(pkt->fp_trail));
   1175 		if (len <= 0) {
   1176 			printf("fwohci_buf_input: malformed input 3: %d\n",
   1177 			    pkt->fp_hlen - count);
   1178 			return 0;
   1179 		}
   1180 	}
   1181 	return 1;
   1182 }
   1183 
   1184 static int
   1185 fwohci_handler_set(struct fwohci_softc *sc,
   1186     int tcode, u_int32_t key1, u_int32_t key2,
   1187     int (*handler)(struct fwohci_softc *, void *, struct fwohci_pkt *),
   1188     void *arg)
   1189 {
   1190 	struct fwohci_ctx *fc;
   1191 	struct fwohci_handler *fh;
   1192 	int i;
   1193 
   1194 	if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1195 		for (i = 0; ; i++) {
   1196 			if (i == sc->sc_isoctx) {
   1197 				/* no more free ctx */
   1198 				return ENOMEM;
   1199 			}
   1200 			fc = sc->sc_ctx_ir[i];
   1201 			fh = LIST_FIRST(&fc->fc_handler);
   1202 			if (fh == NULL)
   1203 				break;
   1204 			if (fh->fh_tcode == tcode && fh->fh_key1 == key1 &&
   1205 			    fh->fh_key2 == key2)
   1206 				break;
   1207 		}
   1208 	} else {
   1209 		switch (tcode) {
   1210 		case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1211 		case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1212 		case IEEE1394_TCODE_READ_REQ_QUAD:
   1213 		case IEEE1394_TCODE_READ_REQ_BLOCK:
   1214 		case IEEE1394_TCODE_LOCK_REQ:
   1215 			fc = sc->sc_ctx_arrq;
   1216 			break;
   1217 		case IEEE1394_TCODE_WRITE_RESP:
   1218 		case IEEE1394_TCODE_READ_RESP_QUAD:
   1219 		case IEEE1394_TCODE_READ_RESP_BLOCK:
   1220 		case IEEE1394_TCODE_LOCK_RESP:
   1221 			fc = sc->sc_ctx_arrs;
   1222 			break;
   1223 		default:
   1224 			return EIO;
   1225 		}
   1226 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1227 		    fh = LIST_NEXT(fh, fh_list)) {
   1228 			if (fh->fh_tcode == tcode && fh->fh_key1 == key1 &&
   1229 			    fh->fh_key2 == key2)
   1230 				break;
   1231 		}
   1232 	}
   1233 	if (handler == NULL) {
   1234 		if (fh != NULL)
   1235 			LIST_REMOVE(fh, fh_list);
   1236 		return 0;
   1237 	}
   1238 	if (fh == NULL) {
   1239 		fh = malloc(sizeof(*fh), M_DEVBUF, M_NOWAIT);
   1240 		if (fh == NULL)
   1241 			return ENOMEM;
   1242 		LIST_INSERT_HEAD(&fc->fc_handler, fh, fh_list);
   1243 	}
   1244 	fh->fh_tcode = tcode;
   1245 	fh->fh_key1 = key1;
   1246 	fh->fh_key2 = key2;
   1247 	fh->fh_handler = handler;
   1248 	fh->fh_handarg = arg;
   1249 #ifdef FW_DEBUG
   1250 	printf("fwohci_handler_set: ctx %d, tcode %x, key1 0x%x, key2 0x%x\n", fc->fc_ctx, tcode, key1, key2);
   1251 #endif
   1252 
   1253 	if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1254 		struct fwohci_buf *fb;
   1255 		fwohci_ctx_init(sc, fc);
   1256 		fb = TAILQ_FIRST(&fc->fc_buf);
   1257 #ifdef FW_DEBUG
   1258 		printf("fwohci_handler_set: SYNC desc %d\n", fb->fb_desc - sc->sc_desc);
   1259 #endif
   1260 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_CommandPtr,
   1261 		    fb->fb_daddr | 1);
   1262 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   1263 		    OHCI_SUBREG_ContextControlClear,
   1264 		    OHCI_CTXCTL_RX_BUFFER_FILL |
   1265 		    OHCI_CTXCTL_RX_CYCLE_MATCH_ENABLE |
   1266 		    OHCI_CTXCTL_RX_MULTI_CHAN_MODE |
   1267 		    OHCI_CTXCTL_RX_DUAL_BUFFER_MODE);
   1268 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   1269 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RX_ISOCH_HEADER);
   1270 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_ContextMatch,
   1271 		    (OHCI_CTXMATCH_TAG0 << key2) | key1);
   1272 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   1273 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1274 	}
   1275 	return 0;
   1276 }
   1277 
   1278 /*
   1279  * Asyncronous Receive Requests input frontend.
   1280  */
   1281 static void
   1282 fwohci_arrq_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1283 {
   1284 	int rcode;
   1285 	u_int32_t key1, key2;
   1286 	struct fwohci_handler *fh;
   1287 	struct fwohci_pkt pkt, res;
   1288 
   1289 	while (fwohci_buf_input(sc, fc, &pkt)) {
   1290 		if (pkt.fp_tcode == OHCI_TCODE_PHY) {
   1291 			fwohci_phy_input(sc, &pkt);
   1292 			continue;
   1293 		}
   1294 		key1 = pkt.fp_hdr[1] & 0xffff;
   1295 		key2 = pkt.fp_hdr[2];
   1296 		memset(&res, 0, sizeof(res));
   1297 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1298 		    fh = LIST_NEXT(fh, fh_list)) {
   1299 			if (pkt.fp_tcode == fh->fh_tcode &&
   1300 			    key1 == fh->fh_key1 &&
   1301 			    key2 == fh->fh_key2) {
   1302 				rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
   1303 				    &pkt);
   1304 				break;
   1305 			}
   1306 		}
   1307 		if (fh == NULL) {
   1308 			rcode = IEEE1394_RCODE_ADDRESS_ERROR;
   1309 #ifdef FW_DEBUG
   1310 			printf("fwohci_arrq_input: no listener: tcode 0x%x, "
   1311 			    "addr=0x%04x %08x\n", pkt.fp_tcode,
   1312 			    key1, key2);
   1313 #endif
   1314 		}
   1315 		if (((*pkt.fp_trail & 0x001f0000) >> 16) !=
   1316 		    OHCI_CTXCTL_EVENT_ACK_PENDING)
   1317 			continue;
   1318 		if (rcode != -1)
   1319 			fwohci_atrs_output(sc, rcode, &pkt, &res);
   1320 	}
   1321 	fwohci_buf_next(sc, fc);
   1322 	OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1323 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   1324 }
   1325 
   1326 /*
   1327  * Asynchronous Receive Response input frontend.
   1328  */
   1329 static void
   1330 fwohci_arrs_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1331 {
   1332 	struct fwohci_pkt pkt;
   1333 	struct fwohci_handler *fh;
   1334 	u_int16_t srcid;
   1335 	int rcode, tlabel;
   1336 
   1337 	while (fwohci_buf_input(sc, fc, &pkt)) {
   1338 		srcid = pkt.fp_hdr[1] >> 16;
   1339 		rcode = (pkt.fp_hdr[1] & 0x0000f000) >> 12;
   1340 		tlabel = (pkt.fp_hdr[0] & 0x0000fc00) >> 10;
   1341 #ifdef FW_DEBUG
   1342 		printf("fwohci_arrs_input: tcode 0x%x, from 0x%04x, tlabel 0x%x, rcode 0x%x, hlen %d, dlen %d\n",
   1343 		    pkt.fp_tcode, srcid, tlabel, rcode, pkt.fp_hlen, pkt.fp_dlen);
   1344 #endif
   1345 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1346 		    fh = LIST_NEXT(fh, fh_list)) {
   1347 			if (pkt.fp_tcode == fh->fh_tcode &&
   1348 			    (srcid & OHCI_NodeId_NodeNumber) == fh->fh_key1 &&
   1349 			    tlabel == fh->fh_key2) {
   1350 				(*fh->fh_handler)(sc, fh->fh_handarg, &pkt);
   1351 				LIST_REMOVE(fh, fh_list);
   1352 				free(fh, M_DEVBUF);
   1353 				break;
   1354 			}
   1355 		}
   1356 #ifdef FW_DEBUG
   1357 		if (fh == NULL)
   1358 			printf("fwohci_arrs_input: no lister\n");
   1359 #endif
   1360 	}
   1361 	fwohci_buf_next(sc, fc);
   1362 	OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1363 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   1364 }
   1365 
   1366 /*
   1367  * Isochronous Receive input frontend.
   1368  */
   1369 static void
   1370 fwohci_ir_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1371 {
   1372 	int rcode, chan, tag;
   1373 	struct iovec *iov;
   1374 	struct fwohci_handler *fh;
   1375 	struct fwohci_pkt pkt;
   1376 
   1377 	while (fwohci_buf_input(sc, fc, &pkt)) {
   1378 		chan = (pkt.fp_hdr[0] & 0x00003f00) >> 8;
   1379 		tag  = (pkt.fp_hdr[0] & 0x0000c000) >> 14;
   1380 #ifdef FW_DEBUG
   1381 		printf("fwohci_ir_input: hdr 0x%08x, tcode %d, hlen %d, dlen %d\n", pkt.fp_hdr[0], pkt.fp_tcode, pkt.fp_hlen, pkt.fp_dlen);
   1382 #endif
   1383 		if (tag == IEEE1394_TAG_GASP) {
   1384 			/*
   1385 			 * The pkt with tag=3 is GASP format.
   1386 			 * Move GASP header to header part.
   1387 			 */
   1388 			if (pkt.fp_dlen < 8)
   1389 				continue;
   1390 			iov = pkt.fp_iov;
   1391 			/* assuming pkt per buffer mode */
   1392 			memcpy(pkt.fp_hdr + 1, iov->iov_base, 8);
   1393 			iov->iov_base = (caddr_t)iov->iov_base + 8;
   1394 			iov->iov_len -= 8;
   1395 			pkt.fp_hlen += 8;
   1396 			pkt.fp_dlen -= 8;
   1397 		}
   1398 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1399 		    fh = LIST_NEXT(fh, fh_list)) {
   1400 			if (pkt.fp_tcode == fh->fh_tcode &&
   1401 			    chan == fh->fh_key1 && tag == fh->fh_key2) {
   1402 				rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
   1403 				    &pkt);
   1404 				break;
   1405 			}
   1406 		}
   1407 #ifdef FW_DEBUG
   1408 		if (fh == NULL)
   1409 			printf("fwohci_ir_input: no handler\n");
   1410 		else
   1411 			printf("fwohci_ir_input: rcode %d\n", rcode);
   1412 #endif
   1413 	}
   1414 	fwohci_buf_next(sc, fc);
   1415 	OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_ContextControlSet,
   1416 	    OHCI_CTXCTL_WAKE);
   1417 }
   1418 
   1419 /*
   1420  * Asynchronous Transmit common routine.
   1421  */
   1422 static int
   1423 fwohci_at_output(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   1424     struct fwohci_pkt *pkt)
   1425 {
   1426 	struct fwohci_buf *fb, *nfb;
   1427 	struct fwohci_desc *fd;
   1428 	struct iovec *iov;
   1429 	int i, ndesc;
   1430 	u_int32_t val;
   1431 
   1432 #ifdef FW_DEBUG
   1433 	printf("fwohci_at_output: tcode 0x%x, hlen %d, dlen %d",
   1434 	    pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen);
   1435 	for (i = 0; i < pkt->fp_hlen/4; i++)
   1436 		printf("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]);
   1437 	printf("$");
   1438 	for (ndesc = 0, iov = pkt->fp_iov; ndesc < pkt->fp_iovcnt; ndesc++, iov++) {
   1439 		for (i = 0; i < iov->iov_len; i++)
   1440 			printf("%s%02x", (i%32)?((i%4)?"":" "):"\n\t",
   1441 			    ((u_int8_t *)iov->iov_base)[i]);
   1442 		printf("$");
   1443 	}
   1444 	printf("\n");
   1445 #endif
   1446 
   1447 	ndesc = 2 + pkt->fp_iovcnt;
   1448 	if (ndesc > 8)
   1449 		return ENOBUFS;
   1450 
   1451 	fb = TAILQ_FIRST(&fc->fc_buf);
   1452 	if (fb == NULL)
   1453 		return ENOBUFS;
   1454 	for (i = 1, fb = TAILQ_FIRST(&fc->fc_buf); i < ndesc; i++, fb = nfb) {
   1455 		nfb = TAILQ_NEXT(fb, fb_list);
   1456 		if (nfb == NULL)
   1457 			return ENOBUFS;
   1458 		if (nfb->fb_desc != fb->fb_desc + 1) {
   1459 			while ((fb = TAILQ_FIRST(&fc->fc_buf)) != nfb) {
   1460 				TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1461 				TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1462 			}
   1463 			break;
   1464 		}
   1465 	}
   1466 
   1467 	fb = TAILQ_FIRST(&fc->fc_buf);
   1468 	fd = fb->fb_desc;
   1469 	fd->fd_flags = OHCI_DESC_IMMED;
   1470 	fd->fd_reqcount = pkt->fp_hlen;
   1471 	fd->fd_data = 0;
   1472 	fd->fd_branch = 0;
   1473 	fd->fd_status = 0;
   1474 	if (fc->fc_ctx == OHCI_CTX_ASYNC_TX_RESPONSE) {
   1475 		i = 3;				/* XXX: 3 sec */
   1476 		val = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   1477 		fd->fd_timestamp = ((val >> 12) & 0x1fff) |
   1478 		    ((((val >> 25) + i) & 0x7) << 13);
   1479 	} else
   1480 		fd->fd_timestamp = 0;
   1481 	fb = TAILQ_NEXT(fb, fb_list);
   1482 	memcpy(fb->fb_desc, pkt->fp_hdr, pkt->fp_hlen);
   1483 	for (i = 0, iov = pkt->fp_iov; i < pkt->fp_iovcnt; i++, iov++) {
   1484 		fb = TAILQ_NEXT(fb, fb_list);
   1485 		/*
   1486 		 * XXX: should rewrite to map mbuf to io area instead
   1487 		 * of copy.
   1488 		 */
   1489 		memcpy(fb->fb_buf, iov->iov_base, iov->iov_len);
   1490 		fd = fb->fb_desc;
   1491 		fd->fd_flags = 0;
   1492 		fd->fd_reqcount = iov->iov_len;
   1493 		fd->fd_data = fb->fb_dmamap->dm_segs[0].ds_addr;
   1494 		fd->fd_branch = 0;
   1495 		fd->fd_status = 0;
   1496 		fd->fd_timestamp = 0;
   1497 		bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0, iov->iov_len,
   1498 		    BUS_DMASYNC_PREWRITE);
   1499 	}
   1500 	fd->fd_flags |= OHCI_DESC_LAST | OHCI_DESC_BRANCH;
   1501 	fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   1502 	/* hang mbuf on the last buffer */
   1503 	fb->fb_m = pkt->fp_m;
   1504 	fb->fb_callback = pkt->fp_callback;
   1505 
   1506 	fb = TAILQ_FIRST(&fc->fc_buf);
   1507 #ifdef FW_DEBUG
   1508 	printf("fwohci_at_output: desc %d", fb->fb_desc - sc->sc_desc);
   1509 	for (i = 0; i < ndesc * 4; i++)
   1510 		printf("%s%08x", i&7?" ":"\n\t", ((u_int32_t *)fb->fb_desc)[i]);
   1511 	printf("\n");
   1512 #endif
   1513 
   1514 	val = OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
   1515 	    OHCI_SUBREG_ContextControlClear);
   1516 
   1517 	if (val & OHCI_CTXCTL_RUN) {
   1518 		if (fc->fc_branch == NULL) {
   1519 			OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1520 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1521 			goto run;
   1522 		}
   1523 		*fc->fc_branch = fb->fb_daddr | ndesc;
   1524 		if ((val & OHCI_CTXCTL_ACTIVE) == 0)
   1525 			OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1526 			    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   1527 	} else {
   1528   run:
   1529 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1530 		    OHCI_SUBREG_CommandPtr, fb->fb_daddr | ndesc);
   1531 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1532 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1533 	}
   1534 	fc->fc_branch = &fd->fd_branch;
   1535 
   1536 	for (i = 0; i < ndesc; i++) {
   1537 		fb = TAILQ_FIRST(&fc->fc_buf);
   1538 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1539 		TAILQ_INSERT_TAIL(&fc->fc_busy, fb, fb_list);
   1540 	}
   1541 	return 0;
   1542 }
   1543 
   1544 static void
   1545 fwohci_at_done(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1546 {
   1547 	struct fwohci_buf *fb, *lfb;
   1548 
   1549 	while ((fb = TAILQ_FIRST(&fc->fc_busy)) != NULL) {
   1550 		for (lfb = fb; lfb != NULL; lfb = TAILQ_NEXT(lfb, fb_list)) {
   1551 #ifdef FW_DEBUG
   1552 			printf("fwohci_at_done: desc %d, %08x %08x %08x %08x\n",
   1553 			    lfb->fb_desc - sc->sc_desc,
   1554 			    ((u_int32_t *)lfb->fb_desc)[0],
   1555 			    ((u_int32_t *)lfb->fb_desc)[1],
   1556 			    ((u_int32_t *)lfb->fb_desc)[2],
   1557 			    ((u_int32_t *)lfb->fb_desc)[3]);
   1558 #endif
   1559 			if (lfb->fb_desc->fd_flags & OHCI_DESC_LAST)
   1560 				break;
   1561 		}
   1562 		if (lfb == NULL) {
   1563 			printf("fwohci_at_done: last not found\n");
   1564 			break;
   1565 		}
   1566 		if (!(lfb->fb_desc->fd_status & OHCI_CTXCTL_ACTIVE))
   1567 			break;
   1568 		if (lfb->fb_desc->fd_flags & OHCI_DESC_IMMED)
   1569 			lfb = TAILQ_NEXT(lfb, fb_list);
   1570 		do {
   1571 			fb = TAILQ_FIRST(&fc->fc_busy);
   1572 			TAILQ_REMOVE(&fc->fc_busy, fb, fb_list);
   1573 			if (fb->fb_m != NULL) {
   1574 				if (fb->fb_callback != NULL) {
   1575 					(*fb->fb_callback)
   1576 					    (sc->sc_sc1394.sc1394_if, fb->fb_m);
   1577 					fb->fb_callback = NULL;
   1578 				} else {
   1579 					m_freem(fb->fb_m);
   1580 				}
   1581 				fb->fb_m = NULL;
   1582 			}
   1583 			TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1584 		} while (fb != lfb);
   1585 	}
   1586 }
   1587 
   1588 /*
   1589  * Asynchronous Transmit Reponse -- in response of request packet.
   1590  */
   1591 static void
   1592 fwohci_atrs_output(struct fwohci_softc *sc, int rcode, struct fwohci_pkt *req,
   1593     struct fwohci_pkt *res)
   1594 {
   1595 	int i;
   1596 
   1597 	if (((*req->fp_trail & 0x001f0000) >> 16) !=
   1598 	    OHCI_CTXCTL_EVENT_ACK_PENDING)
   1599 		return;
   1600 
   1601 	res->fp_hdr[0] = (req->fp_hdr[0] & 0x0000fc00) | 0x00000100;
   1602 	res->fp_hdr[1] = (req->fp_hdr[1] & 0xffff0000) | (rcode << 12);
   1603 	switch (req->fp_tcode) {
   1604 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1605 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1606 		res->fp_tcode = IEEE1394_TCODE_WRITE_RESP;
   1607 		res->fp_hlen = 12;
   1608 		break;
   1609 	case IEEE1394_TCODE_READ_REQ_QUAD:
   1610 		res->fp_tcode = IEEE1394_TCODE_READ_RESP_QUAD;
   1611 		res->fp_hlen = 16;
   1612 		res->fp_dlen = 0;
   1613 		if (res->fp_iovcnt == 1 && res->fp_iov[0].iov_len == 4)
   1614 			res->fp_hdr[3] =
   1615 			    *(u_int32_t *)res->fp_iov[0].iov_base;
   1616 		res->fp_iovcnt = 0;
   1617 		break;
   1618 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   1619 	case IEEE1394_TCODE_LOCK_REQ:
   1620 		if (req->fp_tcode == IEEE1394_TCODE_LOCK_REQ)
   1621 			res->fp_tcode = IEEE1394_TCODE_LOCK_RESP;
   1622 		else
   1623 			res->fp_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
   1624 		res->fp_hlen = 16;
   1625 		res->fp_dlen = 0;
   1626 		for (i = 0; i < res->fp_iovcnt; i++)
   1627 			res->fp_dlen += res->fp_iov[i].iov_len;
   1628 		res->fp_hdr[3] = res->fp_dlen << 16;
   1629 		break;
   1630 	}
   1631 	res->fp_hdr[0] |= (res->fp_tcode << 4);
   1632 	fwohci_at_output(sc, sc->sc_ctx_atrs, res);
   1633 }
   1634 
   1635 /*
   1636  * APPLICATION LAYER SERVICES
   1637  */
   1638 
   1639 /*
   1640  * Initialization for Configuration ROM (no DMA context)
   1641  */
   1642 
   1643 #define	CFR_MAXUNIT		20
   1644 
   1645 struct configromctx {
   1646 	u_int32_t	*ptr;
   1647 	int		curunit;
   1648 	struct {
   1649 		u_int32_t	*start;
   1650 		int		length;
   1651 		u_int32_t	*refer;
   1652 		int		refunit;
   1653 	} unit[CFR_MAXUNIT];
   1654 };
   1655 
   1656 #define	CFR_PUT_DATA4(cfr, d1, d2, d3, d4)				\
   1657 	(*(cfr)->ptr++ = (((d1)<<24) | ((d2)<<16) | ((d3)<<8) | (d4)))
   1658 
   1659 #define	CFR_PUT_DATA1(cfr, d)	(*(cfr)->ptr++ = (d))
   1660 
   1661 #define	CFR_PUT_VALUE(cfr, key, d)	(*(cfr)->ptr++ = ((key)<<24) | (d))
   1662 
   1663 #define	CFR_PUT_CRC(cfr, n)						\
   1664 	(*(cfr)->unit[n].start = ((cfr)->unit[n].length << 16) |	\
   1665 	    fwohci_crc16((cfr)->unit[n].start + 1, (cfr)->unit[n].length))
   1666 
   1667 #define	CFR_START_UNIT(cfr, n)						\
   1668 do {									\
   1669 	if ((cfr)->unit[n].refer != NULL) {				\
   1670 		*(cfr)->unit[n].refer |=				\
   1671 		    (cfr)->ptr - (cfr)->unit[n].refer;			\
   1672 		CFR_PUT_CRC(cfr, (cfr)->unit[n].refunit);		\
   1673 	}								\
   1674 	(cfr)->curunit = (n);						\
   1675 	(cfr)->unit[n].start = (cfr)->ptr++;				\
   1676 } while (0 /* CONSTCOND */)
   1677 
   1678 #define	CFR_PUT_REFER(cfr, key, n)					\
   1679 do {									\
   1680 	(cfr)->unit[n].refer = (cfr)->ptr;				\
   1681 	(cfr)->unit[n].refunit = (cfr)->curunit;			\
   1682 	*(cfr)->ptr++ = (key) << 24;					\
   1683 } while (0 /* CONSTCOND */)
   1684 
   1685 #define	CFR_END_UNIT(cfr)						\
   1686 do {									\
   1687 	(cfr)->unit[(cfr)->curunit].length = (cfr)->ptr -		\
   1688 	    ((cfr)->unit[(cfr)->curunit].start + 1);			\
   1689 	CFR_PUT_CRC(cfr, (cfr)->curunit);				\
   1690 } while (0 /* CONSTCOND */)
   1691 
   1692 static u_int16_t
   1693 fwohci_crc16(u_int32_t *ptr, int len)
   1694 {
   1695 	int shift;
   1696 	u_int32_t crc, sum, data;
   1697 
   1698 	crc = 0;
   1699 	while (len-- > 0) {
   1700 		data = *ptr++;
   1701 		for (shift = 28; shift >= 0; shift -= 4) {
   1702 			sum = ((crc >> 12) ^ (data >> shift)) & 0x000f;
   1703 			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
   1704 		}
   1705 		crc &= 0xffff;
   1706 	}
   1707 	return crc;
   1708 }
   1709 
   1710 static void
   1711 fwohci_configrom_init(struct fwohci_softc *sc)
   1712 {
   1713 	int i;
   1714 	struct fwohci_buf *fb;
   1715 	u_int32_t *hdr;
   1716 	struct configromctx cfr;
   1717 
   1718 	fb = &sc->sc_buf_cnfrom;
   1719 	memset(&cfr, 0, sizeof(cfr));
   1720 	cfr.ptr = hdr = (u_int32_t *)fb->fb_buf;
   1721 
   1722 	/* headers */
   1723 	CFR_START_UNIT(&cfr, 0);
   1724 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusId));
   1725 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusOptions));
   1726 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDHi));
   1727 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDLo));
   1728 	CFR_END_UNIT(&cfr);
   1729 	/* copy info_length from crc_length */
   1730 	*hdr |= (*hdr & 0x00ff0000) << 8;
   1731 	OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMhdr, *hdr);
   1732 
   1733 	/* root directory */
   1734 	CFR_START_UNIT(&cfr, 1);
   1735 	CFR_PUT_VALUE(&cfr, 0x03, 0x00005e);	/* vendor id */
   1736 	CFR_PUT_REFER(&cfr, 0x81, 2);		/* textual descriptor offset */
   1737 	CFR_PUT_VALUE(&cfr, 0x0c, 0x0083c0);	/* node capability */
   1738 						/* spt,64,fix,lst,drq */
   1739 #ifdef INET
   1740 	CFR_PUT_REFER(&cfr, 0xd1, 3);		/* IPv4 unit directory */
   1741 #endif /* INET */
   1742 #ifdef INET6
   1743 	CFR_PUT_REFER(&cfr, 0xd1, 4);		/* IPv6 unit directory */
   1744 #endif /* INET6 */
   1745 	CFR_END_UNIT(&cfr);
   1746 
   1747 	CFR_START_UNIT(&cfr, 2);
   1748 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   1749 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   1750 	CFR_PUT_DATA4(&cfr, 'N', 'e', 't', 'B');
   1751 	CFR_PUT_DATA4(&cfr, 'S', 'D', 0x00, 0x00);
   1752 	CFR_END_UNIT(&cfr);
   1753 
   1754 #ifdef INET
   1755 	/* IPv4 unit directory */
   1756 	CFR_START_UNIT(&cfr, 3);
   1757 	CFR_PUT_VALUE(&cfr, 0x12, 0x00005e);	/* unit spec id */
   1758 	CFR_PUT_REFER(&cfr, 0x81, 6);		/* textual descriptor offset */
   1759 	CFR_PUT_VALUE(&cfr, 0x13, 0x000001);	/* unit sw version */
   1760 	CFR_PUT_REFER(&cfr, 0x81, 7);		/* textual descriptor offset */
   1761 	CFR_END_UNIT(&cfr);
   1762 
   1763 	CFR_START_UNIT(&cfr, 6);
   1764 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   1765 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   1766 	CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
   1767 	CFR_END_UNIT(&cfr);
   1768 
   1769 	CFR_START_UNIT(&cfr, 7);
   1770 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   1771 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   1772 	CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '4');
   1773 	CFR_END_UNIT(&cfr);
   1774 #endif /* INET */
   1775 
   1776 #ifdef INET6
   1777 	/* IPv6 unit directory */
   1778 	CFR_START_UNIT(&cfr, 4);
   1779 	CFR_PUT_VALUE(&cfr, 0x12, 0x00005e);	/* unit spec id */
   1780 	CFR_PUT_REFER(&cfr, 0x81, 8);		/* textual descriptor offset */
   1781 	CFR_PUT_VALUE(&cfr, 0x13, 0x000001);	/* unit sw version */
   1782 	CFR_PUT_REFER(&cfr, 0x81, 9);		/* textual descriptor offset */
   1783 	CFR_END_UNIT(&cfr);
   1784 
   1785 	CFR_START_UNIT(&cfr, 8);
   1786 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   1787 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   1788 	CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
   1789 	CFR_END_UNIT(&cfr);
   1790 
   1791 	CFR_START_UNIT(&cfr, 9);
   1792 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   1793 	CFR_PUT_DATA1(&cfr, 0);
   1794 	CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '6');
   1795 	CFR_END_UNIT(&cfr);
   1796 #endif /* INET6 */
   1797 
   1798 #ifdef FW_DEBUG
   1799 	printf("%s: Config ROM:", sc->sc_sc1394.sc1394_dev.dv_xname);
   1800 	for (i = 0; i < cfr.ptr - hdr; i++)
   1801 		printf("%s%08x", i&7?" ":"\n    ", hdr[i]);
   1802 	printf("\n");
   1803 #endif /* FW_DEBUG */
   1804 
   1805 	/*
   1806 	 * Make network byte order for DMA
   1807 	 */
   1808 	for (i = 0; i < cfr.ptr - hdr; i++)
   1809 		NTOHL(hdr[i]);
   1810 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
   1811 	    (caddr_t)cfr.ptr - fb->fb_buf, BUS_DMASYNC_PREWRITE);
   1812 
   1813 	OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMmap,
   1814 	    fb->fb_dmamap->dm_segs[0].ds_addr);
   1815 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_BIBImageValid);
   1816 }
   1817 
   1818 /*
   1819  * SelfID buffer (no DMA context)
   1820  */
   1821 static void
   1822 fwohci_selfid_init(struct fwohci_softc *sc)
   1823 {
   1824 	struct fwohci_buf *fb;
   1825 	u_int32_t val;
   1826 
   1827 	fb = &sc->sc_buf_selfid;
   1828 #ifdef DIAGNOSTICS
   1829 	if ((fb->fb_dmamap->dm_segs[0].ds_addr & 0x7ff) != 0)
   1830 		panic("fwohci_selfid_init: not aligned: %p (%ld) %p",
   1831 		    (caddr_t)fb->fb_dmamap->dm_segs[0].ds_addr,
   1832 		    fb->fb_dmamap->dm_segs[0].ds_len, fb->fb_buf);
   1833 #endif
   1834 	memset(fb->fb_buf, 0x55, fb->fb_dmamap->dm_segs[0].ds_len);
   1835 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
   1836 	    fb->fb_dmamap->dm_segs[0].ds_len, BUS_DMASYNC_PREREAD);
   1837 
   1838 	OHCI_CSR_WRITE(sc, OHCI_REG_SelfIDBuffer,
   1839 	    fb->fb_dmamap->dm_segs[0].ds_addr);
   1840 
   1841 	val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
   1842 }
   1843 
   1844 static int
   1845 fwohci_selfid_input(struct fwohci_softc *sc)
   1846 {
   1847 	int i;
   1848 	u_int32_t count, val, gen;
   1849 	u_int32_t *buf;
   1850 
   1851 	val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
   1852 	if (val & OHCI_SelfID_Error) {
   1853 		printf("%s: SelfID Error\n", sc->sc_sc1394.sc1394_dev.dv_xname);
   1854 		return -1;
   1855 	}
   1856 	count = (val & OHCI_SelfID_Size_MASK) >> OHCI_SelfID_Size_BITPOS;
   1857 	gen = (val & OHCI_SelfID_Gen_MASK) >> OHCI_SelfID_Gen_BITPOS;
   1858 
   1859 	bus_dmamap_sync(sc->sc_dmat, sc->sc_buf_selfid.fb_dmamap,
   1860 	    0, count << 2, BUS_DMASYNC_POSTREAD);
   1861 
   1862 	buf = (u_int32_t *)sc->sc_buf_selfid.fb_buf;
   1863 	if ((val & OHCI_SelfID_Gen_MASK) != (buf[0] & OHCI_SelfID_Gen_MASK)) {
   1864 		printf("%s: SelfID Gen mismatch (%d, %d)\n",
   1865 		    sc->sc_sc1394.sc1394_dev.dv_xname, gen,
   1866 		    (buf[0] & OHCI_SelfID_Gen_MASK) >> OHCI_SelfID_Gen_BITPOS);
   1867 		return -1;
   1868 	}
   1869 
   1870 #ifdef FW_DEBUG
   1871 	printf("%s: SelfID: 0x%08x", sc->sc_sc1394.sc1394_dev.dv_xname, val);
   1872 	for (i = 0; i < count; i++)
   1873 		printf("%s%08x", i&7?" ":"\n    ", buf[i]);
   1874 	printf("\n");
   1875 #endif /* FW_DEBUG */
   1876 
   1877 	val = OHCI_CSR_READ(sc, OHCI_REG_NodeId);
   1878 	if ((val & OHCI_NodeId_IDValid) == 0) {
   1879 		sc->sc_nodeid = IEEE1394_BCAST_PHY_ID;	/* invalid */
   1880 		printf("%s: nodeid is invalid\n",
   1881 		    sc->sc_sc1394.sc1394_dev.dv_xname);
   1882 		return -1;
   1883 	}
   1884 	sc->sc_nodeid = val & 0xffff;
   1885 
   1886 	for (i = 1; i < count; i += 2) {
   1887 		if (buf[i] != ~buf[i + 1]) {
   1888 			printf("%s: SelfID corrupted (%d, 0x%08x, 0x%08x)\n",
   1889 			    sc->sc_sc1394.sc1394_dev.dv_xname, i,
   1890 			    buf[i], buf[i + 1]);
   1891 			return -1;
   1892 		}
   1893 		if (buf[i] & 0x00000001)
   1894 			continue;	/* more pkt */
   1895 		if (buf[i] & 0x00800000)
   1896 			continue;	/* external id */
   1897 		sc->sc_rootid = (buf[i] & 0x3f000000) >> 24;
   1898 		if ((buf[i] & 0x00400800) == 0x00400800)
   1899 			sc->sc_irmid = sc->sc_rootid;
   1900 	}
   1901 #ifdef FW_DEBUG
   1902 	printf("%s: nodeid=0x%04x(%d), rootid=%d, irmid=%d\n",
   1903 	    sc->sc_sc1394.sc1394_dev.dv_xname,
   1904 	    sc->sc_nodeid, sc->sc_nodeid & OHCI_NodeId_NodeNumber,
   1905 	    sc->sc_rootid, sc->sc_irmid);
   1906 #endif
   1907 
   1908 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) > sc->sc_rootid)
   1909 		return -1;
   1910 
   1911 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) == sc->sc_rootid)
   1912 		OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
   1913 		    OHCI_LinkControl_CycleMaster);
   1914 	else
   1915 		OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear,
   1916 		    OHCI_LinkControl_CycleMaster);
   1917 	return 0;
   1918 }
   1919 
   1920 /*
   1921  * some CSRs are handled by driver.
   1922  */
   1923 static void
   1924 fwohci_csr_init(struct fwohci_softc *sc)
   1925 {
   1926 	int i;
   1927 	static u_int32_t csr[] = {
   1928 	    CSR_STATE_CLEAR, CSR_STATE_SET, CSR_SB_CYCLE_TIME,
   1929 	    CSR_SB_BUS_TIME, CSR_SB_BUSY_TIMEOUT, CSR_SB_BUS_MANAGER_ID,
   1930 	    CSR_SB_CHANNEL_AVAILABLE_HI, CSR_SB_CHANNEL_AVAILABLE_LO,
   1931 	    CSR_SB_BROADCAST_CHANNEL
   1932 	};
   1933 
   1934 	for (i = 0; i < sizeof(csr) / sizeof(csr[0]); i++) {
   1935 		fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_QUAD,
   1936 		    CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
   1937 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_REQ_QUAD,
   1938 		    CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
   1939 	}
   1940 	sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] = 31;	/*XXX*/
   1941 }
   1942 
   1943 static int
   1944 fwohci_csr_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   1945 {
   1946 	struct fwohci_pkt res;
   1947 	u_int32_t reg;
   1948 
   1949 	/*
   1950 	 * XXX need to do special functionality other than just r/w...
   1951 	 */
   1952 	reg = pkt->fp_hdr[2] - CSR_BASE_LO;
   1953 
   1954 	if ((reg & 0x03) != 0) {
   1955 		/* alignment error */
   1956 		return IEEE1394_RCODE_ADDRESS_ERROR;
   1957 	}
   1958 	if (pkt->fp_tcode == IEEE1394_TCODE_WRITE_REQ_QUAD) {
   1959 #ifdef FW_DEBUG
   1960 		printf("fwohci_csr_input: CSR[0x%04x]: 0x%08x -> 0x%08x\n",
   1961 		    reg, *(u_int32_t *)(&sc->sc_csr[reg]),
   1962 		    ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base));
   1963 #endif
   1964 		*(u_int32_t *)&sc->sc_csr[reg] =
   1965 		    ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base);
   1966 	} else {
   1967 #ifdef FW_DEBUG
   1968 		printf("fwohci_csr_input: CSR[0x%04x]: 0x%08x\n",
   1969 		    reg, *(u_int32_t *)(&sc->sc_csr[reg]));
   1970 #endif
   1971 		res.fp_hdr[3] = htonl(*(u_int32_t *)&sc->sc_csr[reg]);
   1972 		res.fp_iov[0].iov_base = &res.fp_hdr[3];
   1973 		res.fp_iov[0].iov_len = 4;
   1974 		res.fp_iovcnt = 1;
   1975 		fwohci_atrs_output(sc, IEEE1394_RCODE_COMPLETE, pkt, &res);
   1976 		return -1;
   1977 	}
   1978 	return IEEE1394_RCODE_COMPLETE;
   1979 }
   1980 
   1981 /*
   1982  * Mapping between nodeid and unique ID (EUI-64).
   1983  */
   1984 static void
   1985 fwohci_uid_collect(struct fwohci_softc *sc)
   1986 {
   1987 	int i;
   1988 	struct fwohci_uidtbl *fu;
   1989 	struct fwohci_pkt pkt;
   1990 
   1991 	if (sc->sc_uidtbl != NULL)
   1992 		free(sc->sc_uidtbl, M_DEVBUF);
   1993 	sc->sc_uidtbl = malloc(sizeof(*fu) * (sc->sc_rootid + 1),
   1994 	    M_DEVBUF, M_NOWAIT);
   1995 	if (sc->sc_uidtbl == NULL)
   1996 		return;
   1997 	memset(sc->sc_uidtbl, 0, sizeof(*fu) * (sc->sc_rootid + 1));
   1998 
   1999 	memset(&pkt, 0, sizeof(pkt));
   2000 	for (i = 0, fu = sc->sc_uidtbl; i <= sc->sc_rootid; i++, fu++) {
   2001 		if (i == (sc->sc_nodeid & OHCI_NodeId_NodeNumber)) {
   2002 			memcpy(fu->fu_hi.fu_uid, sc->sc_sc1394.sc1394_guid, 4);
   2003 			memcpy(fu->fu_lo.fu_uid, sc->sc_sc1394.sc1394_guid, 4);
   2004 			fu->fu_hi.fu_valid = fu->fu_lo.fu_valid = 1;
   2005 			continue;
   2006 		}
   2007 		fu->fu_hi.fu_valid = fu->fu_lo.fu_valid = 0;
   2008 		pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   2009 		pkt.fp_hlen = 12;
   2010 		pkt.fp_dlen = 0;
   2011 		pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   2012 		    (pkt.fp_tcode << 4);
   2013 		pkt.fp_hdr[1] = ((0xffc0 | i) << 16) | CSR_BASE_HI;
   2014 		pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 12;
   2015 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, i,
   2016 		    sc->sc_tlabel, fwohci_uid_input, &fu->fu_hi);
   2017 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   2018 		fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   2019 
   2020 		pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   2021 		    (pkt.fp_tcode << 4);
   2022 		pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 16;
   2023 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, i,
   2024 		    sc->sc_tlabel, fwohci_uid_input, &fu->fu_lo);
   2025 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   2026 		fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   2027 	}
   2028 }
   2029 
   2030 static int
   2031 fwohci_uid_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *res)
   2032 {
   2033 	struct fwohci_uident *fu = arg;
   2034 
   2035 	memcpy(fu->fu_uid, res->fp_iov[0].iov_base, 4);
   2036 	fu->fu_valid = 1;
   2037 #ifdef FW_DEBUG
   2038 	printf("fwohci_uid_input: %02x%02x%02x%02x\n",
   2039 	    fu->fu_uid[0], fu->fu_uid[1], fu->fu_uid[2], fu->fu_uid[3]);
   2040 #endif
   2041 	return 0;
   2042 }
   2043 
   2044 static int
   2045 fwohci_uid_lookup(struct fwohci_softc *sc, u_int8_t *uid)
   2046 {
   2047 	struct fwohci_uidtbl *fu;
   2048 	int n;
   2049 	static const u_int8_t bcast[] =
   2050 	    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
   2051 
   2052 	if (memcmp(uid, bcast, sizeof(bcast)) == 0)
   2053 		return IEEE1394_BCAST_PHY_ID;
   2054 	fu = sc->sc_uidtbl;
   2055 	if (fu == NULL) {
   2056 		fwohci_uid_collect(sc); /* try to get */
   2057 		return -1;
   2058 	}
   2059 	for (n = 0; n <= sc->sc_rootid; n++, fu++) {
   2060 		if (fu->fu_hi.fu_valid && fu->fu_lo.fu_valid &&
   2061 		    memcmp(fu->fu_hi.fu_uid, uid, 4) == 0 &&
   2062 		    memcmp(fu->fu_lo.fu_uid, uid + 4, 4) == 0)
   2063 			break;
   2064 	}
   2065 	if (n > sc->sc_rootid) {
   2066 		fwohci_uid_collect(sc); /* try to get */
   2067 		return -1;
   2068 	}
   2069 	return n;
   2070 }
   2071 
   2072 /*
   2073  * functions to support network interface
   2074  */
   2075 static int
   2076 fwohci_if_inreg(struct device *self, u_int32_t offhi, u_int32_t offlo,
   2077     void (*handler)(struct device *, struct mbuf *))
   2078 {
   2079 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
   2080 	int s;
   2081 
   2082 	s = splimp();
   2083 	fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_BLOCK, offhi, offlo,
   2084 	    fwohci_if_input, handler);
   2085 	fwohci_handler_set(sc, IEEE1394_TCODE_STREAM_DATA,
   2086 	    sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] & OHCI_NodeId_NodeNumber,
   2087 	    IEEE1394_TAG_GASP, fwohci_if_input, handler);
   2088 	splx(s);
   2089 	return 0;
   2090 }
   2091 
   2092 static int
   2093 fwohci_if_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   2094 {
   2095 	int n, len;
   2096 	struct mbuf *m;
   2097 	struct iovec *iov;
   2098 	void (*handler)(struct device *, struct mbuf *) = arg;
   2099 
   2100 #ifdef FW_DEBUG
   2101 	{ int i;
   2102 	printf("fwohci_if_input: tcode=0x%x, dlen=%d",
   2103 	    pkt->fp_tcode, pkt->fp_dlen);
   2104 	for (i = 0; i < pkt->fp_hlen/4; i++)
   2105 		printf("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]);
   2106 	printf("$");
   2107 	for (n = 0, len = pkt->fp_dlen; len > 0; len -= i, n++) {
   2108 		iov = &pkt->fp_iov[n];
   2109 		for (i = 0; i < iov->iov_len; i++)
   2110 			printf("%s%02x", (i%32)?((i%4)?"":" "):"\n\t",
   2111 			    ((u_int8_t *)iov->iov_base)[i]);
   2112 		printf("$");
   2113 	}
   2114 	printf("\n");
   2115 	}
   2116 #endif /* FW_DEBUG */
   2117 	len = pkt->fp_dlen;
   2118 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2119 	if (m == NULL)
   2120 		return IEEE1394_RCODE_COMPLETE;
   2121 	if (pkt->fp_tcode == IEEE1394_TCODE_STREAM_DATA)
   2122 		m->m_flags |= M_BCAST;
   2123 	m->m_pkthdr.rcvif = NULL;	/* set in child */
   2124 	m->m_pkthdr.len = len;
   2125 	m->m_len = 0;
   2126 	if (len > MHLEN) {
   2127 		MCLGET(m, M_DONTWAIT);
   2128 		if ((m->m_flags & M_EXT) == 0) {
   2129 			m_freem(m);
   2130 			return IEEE1394_RCODE_COMPLETE;
   2131 		}
   2132 	}
   2133 	/*
   2134 	 * We may use receive buffer by external mbuf instead of copy here.
   2135 	 * But asynchronous receive buffer must be operate in buffer fill
   2136 	 * mode, so that each receive buffer will shared by multiple mbufs.
   2137 	 * If upper layer doesn't free mbuf soon, e.g. application program
   2138 	 * is suspended, buffer must be reallocated.
   2139 	 * Isochronous buffer must be operate in packet buffer mode, and
   2140 	 * it is easy to map receive buffer to external mbuf.  But it is
   2141 	 * used for broadcast/multicast only, and is expected not so
   2142 	 * performance sensitive for now.
   2143 	 * XXX: The performance may be important for multicast case,
   2144 	 * so we should revisit here later.
   2145 	 *						-- onoe
   2146 	 */
   2147 	n = 0;
   2148 	iov = pkt->fp_iov;
   2149 	while (len > 0) {
   2150 		memcpy(mtod(m, caddr_t) + m->m_len, iov->iov_base,
   2151 		    iov->iov_len);
   2152 	        m->m_len += iov->iov_len;
   2153 	        len -= iov->iov_len;
   2154 		iov++;
   2155 	}
   2156 	(*handler)(sc->sc_sc1394.sc1394_if, m);
   2157 	return IEEE1394_RCODE_COMPLETE;
   2158 }
   2159 
   2160 static int
   2161 fwohci_if_output(struct device *self, struct mbuf *m0,
   2162     void (*callback)(struct device *, struct mbuf *))
   2163 {
   2164 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
   2165 	struct mbuf *m;
   2166 	struct fwohci_pkt pkt;
   2167 	struct iovec *iov;
   2168 	u_int8_t *p;
   2169 	int s, n, error;
   2170 
   2171 	memset(&pkt, 0, sizeof(pkt));
   2172 	s = splimp();
   2173 	if (m0->m_flags & (M_BCAST|M_MCAST)) {
   2174 		m_adj(m0, 8);
   2175 		/* construct GASP header */
   2176 		p = mtod(m0, u_int8_t *);
   2177 		p[0] = sc->sc_nodeid >> 8;
   2178 		p[1] = sc->sc_nodeid & 0xff;
   2179 		p[2] = 0x00; p[3] = 0x00; p[4] = 0x5e;
   2180 		p[5] = 0x00; p[6] = 0x00; p[7] = 0x01;
   2181 		pkt.fp_tcode = IEEE1394_TCODE_STREAM_DATA;
   2182 		pkt.fp_hlen = 8;
   2183 		pkt.fp_hdr[0] = (IEEE1394_TAG_GASP << 14) |
   2184 		    ((sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] &
   2185 		    OHCI_NodeId_NodeNumber) << 8);
   2186 		pkt.fp_hdr[1] = m0->m_pkthdr.len << 16;
   2187 	} else {
   2188 		p = mtod(m0, u_int8_t *);
   2189 		m_adj(m0, 16);
   2190 		n = fwohci_uid_lookup(sc, p);
   2191 		if (n < 0) {
   2192 			printf("fwohci_if_output: nodeid unknown: %08x%08x\n",
   2193 			    htonl(((u_int32_t *)p)[0]),
   2194 			    htonl(((u_int32_t *)p)[1]));
   2195 			error = EHOSTUNREACH;
   2196 			goto end;
   2197 		}
   2198 		if (n == (sc->sc_nodeid & OHCI_NodeId_NodeNumber)) {
   2199 			/* should not come here */
   2200 			error = EIO;
   2201 			goto end;
   2202 		}
   2203 		pkt.fp_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
   2204 		pkt.fp_hlen = 16;
   2205 		pkt.fp_hdr[0] = 0x00800100 | (sc->sc_tlabel << 10) |
   2206 		    (p[9] << 16);
   2207 		pkt.fp_hdr[1] =
   2208 		    (((sc->sc_nodeid & OHCI_NodeId_BusNumber) | n) << 16) |
   2209 		    (p[10] << 8) | p[11];
   2210 		pkt.fp_hdr[2] = (p[12]<<24) | (p[13]<<16) | (p[14]<<8) | p[15];
   2211 		pkt.fp_hdr[3] = m0->m_pkthdr.len << 16;
   2212 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   2213 	}
   2214 	pkt.fp_hdr[0] |= (pkt.fp_tcode << 4);
   2215 	pkt.fp_dlen = m0->m_pkthdr.len;
   2216 	for (m = m0; m != NULL; m = m->m_next) {
   2217 		iov = &pkt.fp_iov[pkt.fp_iovcnt++];
   2218 		iov->iov_base = mtod(m, caddr_t);
   2219 		iov->iov_len = m->m_len;
   2220 	}
   2221 	pkt.fp_m = m0;
   2222 	pkt.fp_callback = callback;
   2223 	error = fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   2224   end:
   2225 	splx(s);
   2226 	if (error) {
   2227 		if (callback)
   2228 			(*callback)(sc->sc_sc1394.sc1394_if, m0);
   2229 		else
   2230 			m_freem(m0);
   2231 	}
   2232 	return error;
   2233 }
   2234