Home | History | Annotate | Line # | Download | only in ieee1394
fwohci.c revision 1.62
      1 /*	$NetBSD: fwohci.c,v 1.62 2002/12/04 00:28:41 haya Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * IEEE1394 Open Host Controller Interface
     41  *	based on OHCI Specification 1.1 (January 6, 2000)
     42  * The first version to support network interface part is wrtten by
     43  * Atsushi Onoe <onoe (at) netbsd.org>.
     44  */
     45 
     46 /*
     47  * The first version to support isochronous acquisition part is wrtten
     48  * by HAYAKAWA Koichi <haya (at) netbsd.org>.
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 __KERNEL_RCSID(0, "$NetBSD: fwohci.c,v 1.62 2002/12/04 00:28:41 haya Exp $");
     53 
     54 #define FWOHCI_WAIT_DEBUG 1
     55 
     56 #define FWOHCI_IT_BUFNUM 4
     57 
     58 #include "opt_inet.h"
     59 #include "fwiso.h"
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/kthread.h>
     64 #include <sys/socket.h>
     65 #include <sys/callout.h>
     66 #include <sys/device.h>
     67 #include <sys/kernel.h>
     68 #include <sys/malloc.h>
     69 #include <sys/mbuf.h>
     70 #include <sys/poll.h>
     71 #include <sys/select.h>
     72 
     73 #if __NetBSD_Version__ >= 105010000
     74 #include <uvm/uvm_extern.h>
     75 #else
     76 #include <vm/vm.h>
     77 #endif
     78 
     79 #include <machine/bus.h>
     80 #include <machine/intr.h>
     81 
     82 #include <dev/ieee1394/ieee1394reg.h>
     83 #include <dev/ieee1394/fwohcireg.h>
     84 
     85 #include <dev/ieee1394/ieee1394var.h>
     86 #include <dev/ieee1394/fwohcivar.h>
     87 #include <dev/ieee1394/fwisovar.h>
     88 
     89 static const char * const ieee1394_speeds[] = { IEEE1394_SPD_STRINGS };
     90 
     91 #if 0
     92 static int fwohci_dnamem_alloc(struct fwohci_softc *sc, int size,
     93     int alignment, bus_dmamap_t *mapp, caddr_t *kvap, int flags);
     94 #endif
     95 static void fwohci_create_event_thread(void *);
     96 static void fwohci_thread_init(void *);
     97 
     98 static void fwohci_event_thread(struct fwohci_softc *);
     99 static void fwohci_hw_init(struct fwohci_softc *);
    100 static void fwohci_power(int, void *);
    101 static void fwohci_shutdown(void *);
    102 
    103 static int  fwohci_desc_alloc(struct fwohci_softc *);
    104 static struct fwohci_desc *fwohci_desc_get(struct fwohci_softc *, int);
    105 static void fwohci_desc_put(struct fwohci_softc *, struct fwohci_desc *, int);
    106 
    107 static int  fwohci_ctx_alloc(struct fwohci_softc *, struct fwohci_ctx **,
    108     int, int, int);
    109 static void fwohci_ctx_free(struct fwohci_softc *, struct fwohci_ctx *);
    110 static void fwohci_ctx_init(struct fwohci_softc *, struct fwohci_ctx *);
    111 
    112 static int fwohci_misc_dmabuf_alloc(bus_dma_tag_t, int, int,
    113     bus_dma_segment_t *, bus_dmamap_t *, void **, const char *);
    114 static void fwohci_misc_dmabuf_free(bus_dma_tag_t, int, int,
    115     bus_dma_segment_t *, bus_dmamap_t *, caddr_t);
    116 
    117 static struct fwohci_ir_ctx *fwohci_ir_ctx_construct(struct fwohci_softc *,
    118     int, int, int, int, int, int);
    119 static void fwohci_ir_ctx_destruct(struct fwohci_ir_ctx *);
    120 
    121 static int fwohci_ir_buf_setup(struct fwohci_ir_ctx *);
    122 static int fwohci_ir_init(struct fwohci_ir_ctx *);
    123 static int fwohci_ir_start(struct fwohci_ir_ctx *);
    124 static void fwohci_ir_intr(struct fwohci_softc *, struct fwohci_ir_ctx *);
    125 static int fwohci_ir_stop(struct fwohci_ir_ctx *);
    126 static int fwohci_ir_ctx_packetnum(struct fwohci_ir_ctx *);
    127 #ifdef USEDRAIN
    128 static int fwohci_ir_ctx_drain(struct fwohci_ir_ctx *);
    129 #endif /* USEDRAIN */
    130 
    131 static int fwohci_it_desc_alloc(struct fwohci_it_ctx *);
    132 static void fwohci_it_desc_free(struct fwohci_it_ctx *itc);
    133 struct fwohci_it_ctx *fwohci_it_ctx_construct(struct fwohci_softc *,
    134     int, int, int, int);
    135 void fwohci_it_ctx_destruct(struct fwohci_it_ctx *);
    136 int fwohci_it_ctx_writedata(ieee1394_it_tag_t, int,
    137     struct ieee1394_it_datalist *, int);
    138 static void fwohci_it_ctx_run(struct fwohci_it_ctx *);
    139 int fwohci_it_ctx_flush(ieee1394_it_tag_t);
    140 static void fwohci_it_intr(struct fwohci_softc *, struct fwohci_it_ctx *);
    141 
    142 int fwohci_itd_construct(struct fwohci_it_ctx *, struct fwohci_it_dmabuf *,
    143     int, struct fwohci_desc *, bus_addr_t, int, int, paddr_t);
    144 void fwohci_itd_destruct(struct fwohci_it_dmabuf *);
    145 static int fwohci_itd_dmabuf_alloc(struct fwohci_it_dmabuf *);
    146 static void fwohci_itd_dmabuf_free(struct fwohci_it_dmabuf *);
    147 int fwohci_itd_link(struct fwohci_it_dmabuf *, struct fwohci_it_dmabuf *);
    148 int fwohci_itd_unlink(struct fwohci_it_dmabuf *);
    149 int fwohci_itd_writedata(struct fwohci_it_dmabuf *, int,
    150     struct ieee1394_it_datalist *);
    151 int fwohci_itd_isfilled(struct fwohci_it_dmabuf *);
    152 
    153 static int  fwohci_buf_alloc(struct fwohci_softc *, struct fwohci_buf *);
    154 static void fwohci_buf_free(struct fwohci_softc *, struct fwohci_buf *);
    155 static void fwohci_buf_init_rx(struct fwohci_softc *);
    156 static void fwohci_buf_start_rx(struct fwohci_softc *);
    157 static void fwohci_buf_stop_tx(struct fwohci_softc *);
    158 static void fwohci_buf_stop_rx(struct fwohci_softc *);
    159 static void fwohci_buf_next(struct fwohci_softc *, struct fwohci_ctx *);
    160 static int  fwohci_buf_pktget(struct fwohci_softc *, struct fwohci_buf **,
    161     caddr_t *, int);
    162 static int  fwohci_buf_input(struct fwohci_softc *, struct fwohci_ctx *,
    163     struct fwohci_pkt *);
    164 static int  fwohci_buf_input_ppb(struct fwohci_softc *, struct fwohci_ctx *,
    165     struct fwohci_pkt *);
    166 
    167 static u_int8_t fwohci_phy_read(struct fwohci_softc *, u_int8_t);
    168 static void fwohci_phy_write(struct fwohci_softc *, u_int8_t, u_int8_t);
    169 static void fwohci_phy_busreset(struct fwohci_softc *);
    170 static void fwohci_phy_input(struct fwohci_softc *, struct fwohci_pkt *);
    171 
    172 static int  fwohci_handler_set(struct fwohci_softc *, int, u_int32_t, u_int32_t,
    173     int (*)(struct fwohci_softc *, void *, struct fwohci_pkt *), void *);
    174 
    175 ieee1394_ir_tag_t fwohci_ir_ctx_set(struct device *, int, int, int, int, int);
    176 int fwohci_ir_ctx_clear(struct device *, ieee1394_ir_tag_t);
    177 int fwohci_ir_read(struct device *, ieee1394_ir_tag_t, struct uio *,
    178     int, int);
    179 int fwohci_ir_wait(struct device *, ieee1394_ir_tag_t, void *, char *name);
    180 int fwohci_ir_select(struct device *, ieee1394_ir_tag_t, struct proc *);
    181 
    182 
    183 
    184 ieee1394_it_tag_t fwohci_it_set(struct ieee1394_softc *, int, int);
    185 static ieee1394_it_tag_t fwohci_it_ctx_set(struct fwohci_softc *, int, int, int);
    186 int fwohci_it_ctx_clear(ieee1394_it_tag_t *);
    187 
    188 static void fwohci_arrq_input(struct fwohci_softc *, struct fwohci_ctx *);
    189 static void fwohci_arrs_input(struct fwohci_softc *, struct fwohci_ctx *);
    190 static void fwohci_as_input(struct fwohci_softc *, struct fwohci_ctx *);
    191 
    192 static int  fwohci_at_output(struct fwohci_softc *, struct fwohci_ctx *,
    193     struct fwohci_pkt *);
    194 static void fwohci_at_done(struct fwohci_softc *, struct fwohci_ctx *, int);
    195 static void fwohci_atrs_output(struct fwohci_softc *, int, struct fwohci_pkt *,
    196     struct fwohci_pkt *);
    197 
    198 static int  fwohci_guidrom_init(struct fwohci_softc *);
    199 static void fwohci_configrom_init(struct fwohci_softc *);
    200 static int  fwohci_configrom_input(struct fwohci_softc *, void *,
    201     struct fwohci_pkt *);
    202 static void fwohci_selfid_init(struct fwohci_softc *);
    203 static int  fwohci_selfid_input(struct fwohci_softc *);
    204 
    205 static void fwohci_csr_init(struct fwohci_softc *);
    206 static int  fwohci_csr_input(struct fwohci_softc *, void *,
    207     struct fwohci_pkt *);
    208 
    209 static void fwohci_uid_collect(struct fwohci_softc *);
    210 static void fwohci_uid_req(struct fwohci_softc *, int);
    211 static int  fwohci_uid_input(struct fwohci_softc *, void *,
    212     struct fwohci_pkt *);
    213 static int  fwohci_uid_lookup(struct fwohci_softc *, const u_int8_t *);
    214 static void fwohci_check_nodes(struct fwohci_softc *);
    215 
    216 static int  fwohci_if_inreg(struct device *, u_int32_t, u_int32_t,
    217     void (*)(struct device *, struct mbuf *));
    218 static int  fwohci_if_input(struct fwohci_softc *, void *, struct fwohci_pkt *);
    219 static int  fwohci_if_input_iso(struct fwohci_softc *, void *, struct fwohci_pkt *);
    220 
    221 static int  fwohci_if_output(struct device *, struct mbuf *,
    222     void (*)(struct device *, struct mbuf *));
    223 static int fwohci_if_setiso(struct device *, u_int32_t, u_int32_t, u_int32_t,
    224     void (*)(struct device *, struct mbuf *));
    225 static int  fwohci_read(struct ieee1394_abuf *);
    226 static int  fwohci_write(struct ieee1394_abuf *);
    227 static int  fwohci_read_resp(struct fwohci_softc *, void *, struct fwohci_pkt *);
    228 static int  fwohci_write_ack(struct fwohci_softc *, void *, struct fwohci_pkt *);
    229 static int  fwohci_read_multi_resp(struct fwohci_softc *, void *,
    230     struct fwohci_pkt *);
    231 static int  fwohci_inreg(struct ieee1394_abuf *, int);
    232 static int  fwohci_unreg(struct ieee1394_abuf *, int);
    233 static int  fwohci_parse_input(struct fwohci_softc *, void *,
    234     struct fwohci_pkt *);
    235 static int  fwohci_submatch(struct device *, struct cfdata *, void *);
    236 
    237 /* XXX */
    238 u_int16_t fwohci_cycletimer(struct fwohci_softc *);
    239 u_int16_t fwohci_it_cycletimer(ieee1394_it_tag_t);
    240 
    241 #ifdef FW_DEBUG
    242 static void fwohci_show_intr(struct fwohci_softc *, u_int32_t);
    243 static void fwohci_show_phypkt(struct fwohci_softc *, u_int32_t);
    244 
    245 /* 1 is normal debug, 2 is verbose debug, 3 is complete (packet dumps). */
    246 
    247 #define DPRINTF(x)      if (fwdebug) printf x
    248 #define DPRINTFN(n,x)   if (fwdebug>(n)) printf x
    249 int     fwdebug = 1;
    250 #else
    251 #define DPRINTF(x)
    252 #define DPRINTFN(n,x)
    253 #endif
    254 
    255 #define OHCI_ITHEADER_SPD_MASK		0x00070000
    256 #define OHCI_ITHEADER_SPD_BITPOS	16
    257 #define OHCI_ITHEADER_TAG_MASK		0x0000c000
    258 #define OHCI_ITHEADER_TAG_BITPOS	14
    259 #define OHCI_ITHEADER_CHAN_MASK		0x00003f00
    260 #define OHCI_ITHEADER_CHAN_BITPOS	8
    261 #define OHCI_ITHEADER_TCODE_MASK	0x000000f0
    262 #define OHCI_ITHEADER_TCODE_BITPOS	4
    263 #define OHCI_ITHEADER_SY_MASK		0x0000000f
    264 #define OHCI_ITHEADER_SY_BITPOS		0
    265 
    266 #define OHCI_ITHEADER_VAL(fld, val) \
    267 	(OHCI_ITHEADER_##fld##_MASK & ((val) << OHCI_ITHEADER_##fld##_BITPOS))
    268 
    269 int
    270 fwohci_init(struct fwohci_softc *sc, const struct evcnt *ev)
    271 {
    272 	int i;
    273 	u_int32_t val;
    274 #if 0
    275 	int error;
    276 #endif
    277 
    278 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ev,
    279 	    sc->sc_sc1394.sc1394_dev.dv_xname, "intr");
    280 
    281 	evcnt_attach_dynamic(&sc->sc_isocnt, EVCNT_TYPE_MISC, ev,
    282 	    sc->sc_sc1394.sc1394_dev.dv_xname, "isorcvs");
    283 	evcnt_attach_dynamic(&sc->sc_ascnt, EVCNT_TYPE_MISC, ev,
    284 	    sc->sc_sc1394.sc1394_dev.dv_xname, "asrcvs");
    285 	evcnt_attach_dynamic(&sc->sc_itintrcnt, EVCNT_TYPE_INTR, ev,
    286 	    sc->sc_sc1394.sc1394_dev.dv_xname, "itintr");
    287 
    288 	/*
    289 	 * Wait for reset completion
    290 	 */
    291 	for (i = 0; i < OHCI_LOOP; i++) {
    292 		val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
    293 		if ((val & OHCI_HCControl_SoftReset) == 0)
    294 			break;
    295 		DELAY(10);
    296 	}
    297 
    298 	/* What dialect of OHCI is this device?
    299 	 */
    300 	val = OHCI_CSR_READ(sc, OHCI_REG_Version);
    301 	printf("%s: OHCI %u.%u", sc->sc_sc1394.sc1394_dev.dv_xname,
    302 	    OHCI_Version_GET_Version(val), OHCI_Version_GET_Revision(val));
    303 
    304 	LIST_INIT(&sc->sc_nodelist);
    305 
    306 	if (fwohci_guidrom_init(sc) != 0) {
    307 		printf("\n%s: fatal: no global UID ROM\n",
    308 		    sc->sc_sc1394.sc1394_dev.dv_xname);
    309 		return -1;
    310 	}
    311 
    312 	printf(", %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    313 	    sc->sc_sc1394.sc1394_guid[0], sc->sc_sc1394.sc1394_guid[1],
    314 	    sc->sc_sc1394.sc1394_guid[2], sc->sc_sc1394.sc1394_guid[3],
    315 	    sc->sc_sc1394.sc1394_guid[4], sc->sc_sc1394.sc1394_guid[5],
    316 	    sc->sc_sc1394.sc1394_guid[6], sc->sc_sc1394.sc1394_guid[7]);
    317 
    318 	/* Get the maximum link speed and receive size
    319 	 */
    320 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    321 	sc->sc_sc1394.sc1394_link_speed =
    322 	    OHCI_BITVAL(val, OHCI_BusOptions_LinkSpd);
    323 	if (sc->sc_sc1394.sc1394_link_speed < IEEE1394_SPD_MAX) {
    324 		printf(", %s",
    325 		    ieee1394_speeds[sc->sc_sc1394.sc1394_link_speed]);
    326 	} else {
    327 		printf(", unknown speed %u", sc->sc_sc1394.sc1394_link_speed);
    328 	}
    329 
    330 	/* MaxRec is encoded as log2(max_rec_octets)-1
    331 	 */
    332 	sc->sc_sc1394.sc1394_max_receive =
    333 	    1 << (OHCI_BITVAL(val, OHCI_BusOptions_MaxRec) + 1);
    334 	printf(", %u max_rec", sc->sc_sc1394.sc1394_max_receive);
    335 
    336 	/*
    337 	 * Count how many isochronous receive ctx we have.
    338 	 */
    339 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
    340 	val = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntMaskClear);
    341 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskClear, ~0);
    342 	for (i = 0; val != 0; val >>= 1) {
    343 		if (val & 0x1)
    344 			i++;
    345 	}
    346 	sc->sc_isoctx = i;
    347 	printf(", %d ir_ctx", sc->sc_isoctx);
    348 
    349 	/*
    350 	 * Count how many isochronous transmit ctx we have.
    351 	 */
    352 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntMaskSet, ~0);
    353 	val = OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntMaskClear);
    354 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntMaskClear, ~0);
    355 	for (i = 0; val != 0; val >>= 1) {
    356 		if (val & 0x1) {
    357 			i++;
    358 			OHCI_SYNC_TX_DMA_WRITE(sc, i,OHCI_SUBREG_CommandPtr,0);
    359 		}
    360 	}
    361 	sc->sc_itctx = i;
    362 
    363 	printf(", %d it_ctx", sc->sc_itctx);
    364 
    365 	printf("\n");
    366 
    367 #if 0
    368 	error = fwohci_dnamem_alloc(sc, OHCI_CONFIG_SIZE,
    369 	    OHCI_CONFIG_ALIGNMENT, &sc->sc_configrom_map,
    370 	    (caddr_t *) &sc->sc_configrom, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    371 	return error;
    372 #endif
    373 
    374 	sc->sc_dying = 0;
    375 	sc->sc_nodeid = 0xffff;		/* invalid */
    376 
    377 	sc->sc_sc1394.sc1394_callback.sc1394_read = fwohci_read;
    378 	sc->sc_sc1394.sc1394_callback.sc1394_write = fwohci_write;
    379 	sc->sc_sc1394.sc1394_callback.sc1394_inreg = fwohci_inreg;
    380 	sc->sc_sc1394.sc1394_callback.sc1394_unreg = fwohci_unreg;
    381 
    382 	kthread_create(fwohci_create_event_thread, sc);
    383 	return 0;
    384 }
    385 
    386 static int
    387 fwohci_if_setiso(struct device *self, u_int32_t channel, u_int32_t tag,
    388     u_int32_t direction, void (*handler)(struct device *, struct mbuf *))
    389 {
    390 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
    391 	int retval;
    392 	int s;
    393 
    394 	if (direction == 1) {
    395 		return EIO;
    396 	}
    397 
    398 	s = splnet();
    399 	retval = fwohci_handler_set(sc, IEEE1394_TCODE_STREAM_DATA,
    400 	    channel, 1 << tag, fwohci_if_input_iso, handler);
    401 	splx(s);
    402 
    403 	if (!retval) {
    404 		printf("%s: dummy iso handler set\n",
    405 		    sc->sc_sc1394.sc1394_dev.dv_xname);
    406 	} else {
    407 		printf("%s: dummy iso handler cannot set\n",
    408 		    sc->sc_sc1394.sc1394_dev.dv_xname);
    409 	}
    410 
    411 	return retval;
    412 }
    413 
    414 int
    415 fwohci_intr(void *arg)
    416 {
    417 	struct fwohci_softc * const sc = arg;
    418 	int progress = 0;
    419 	u_int32_t intmask, iso;
    420 
    421 	for (;;) {
    422 		intmask = OHCI_CSR_READ(sc, OHCI_REG_IntEventClear);
    423 
    424 		/*
    425 		 * On a bus reset, everything except bus reset gets
    426 		 * cleared.  That can't get cleared until the selfid
    427 		 * phase completes (which happens outside the
    428 		 * interrupt routines). So if just a bus reset is left
    429 		 * in the mask and it's already in the sc_intmask,
    430 		 * just return.
    431 		 */
    432 
    433 		if ((intmask == 0) ||
    434 		    (progress && (intmask == OHCI_Int_BusReset) &&
    435 			(sc->sc_intmask & OHCI_Int_BusReset))) {
    436 			if (progress)
    437 				wakeup(fwohci_event_thread);
    438 			return progress;
    439 		}
    440 		OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    441 		    intmask & ~OHCI_Int_BusReset);
    442 #ifdef FW_DEBUG
    443 		if (fwdebug > 1)
    444 			fwohci_show_intr(sc, intmask);
    445 #endif
    446 
    447 		if (intmask & OHCI_Int_BusReset) {
    448 			/*
    449 			 * According to OHCI spec 6.1.1 "busReset",
    450 			 * All asynchronous transmit must be stopped before
    451 			 * clearing BusReset.  Moreover, the BusReset
    452 			 * interrupt bit should not be cleared during the
    453 			 * SelfID phase.  Thus we turned off interrupt mask
    454 			 * bit of BusReset instead until SelfID completion
    455 			 * or SelfID timeout.
    456 			 */
    457 			intmask &= OHCI_Int_SelfIDComplete;
    458 			OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear,
    459 			    OHCI_Int_BusReset);
    460 			sc->sc_intmask = OHCI_Int_BusReset;
    461 		}
    462 		sc->sc_intmask |= intmask;
    463 
    464 		if (intmask & OHCI_Int_IsochTx) {
    465 			int i;
    466 
    467 			iso = OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntEventClear);
    468 			OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntEventClear, iso);
    469 
    470 			sc->sc_itintrcnt.ev_count++;
    471 			for (i = 0; i < sc->sc_itctx; ++i) {
    472 				if ((iso & (1<<i)) == 0 ||
    473 				    sc->sc_ctx_it[i] == NULL) {
    474 					continue;
    475 				}
    476 
    477 				fwohci_it_intr(sc, sc->sc_ctx_it[i]);
    478 			}
    479 		}
    480 		if (intmask & OHCI_Int_IsochRx) {
    481 			int i;
    482 
    483 			iso = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntEventClear);
    484 			OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntEventClear, iso);
    485 
    486 			for (i = 0; i < sc->sc_isoctx; i++) {
    487 				if ((iso & (1 << i))
    488 				    && sc->sc_ctx_ir[i] != NULL) {
    489 					iso &= ~(1 << i);
    490 					fwohci_ir_intr(sc, sc->sc_ctx_ir[i]);
    491 				}
    492 			}
    493 
    494 			if (iso == 0) {
    495 				sc->sc_intmask &= ~OHCI_Int_IsochRx;
    496 			}
    497 			sc->sc_iso |= iso;
    498 		}
    499 
    500 		if (!progress) {
    501 			sc->sc_intrcnt.ev_count++;
    502 			progress = 1;
    503 		}
    504 	}
    505 }
    506 
    507 static void
    508 fwohci_create_event_thread(void *arg)
    509 {
    510 	struct fwohci_softc  *sc = arg;
    511 
    512 	if (kthread_create1(fwohci_thread_init, sc, &sc->sc_event_thread, "%s",
    513 	    sc->sc_sc1394.sc1394_dev.dv_xname)) {
    514 		printf("%s: unable to create event thread\n",
    515 		    sc->sc_sc1394.sc1394_dev.dv_xname);
    516 		panic("fwohci_create_event_thread");
    517 	}
    518 }
    519 
    520 static void
    521 fwohci_thread_init(void *arg)
    522 {
    523 	struct fwohci_softc *sc = arg;
    524 	int i;
    525 
    526 	/*
    527 	 * Allocate descriptors
    528 	 */
    529 	if (fwohci_desc_alloc(sc)) {
    530 		printf("%s: not enabling interrupts\n",
    531 		    sc->sc_sc1394.sc1394_dev.dv_xname);
    532 		kthread_exit(1);
    533 	}
    534 
    535 	/*
    536 	 * Enable Link Power
    537 	 */
    538 
    539 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
    540 
    541 	/*
    542 	 * Allocate DMA Context
    543 	 */
    544 	fwohci_ctx_alloc(sc, &sc->sc_ctx_arrq, OHCI_BUF_ARRQ_CNT,
    545 	    OHCI_CTX_ASYNC_RX_REQUEST, FWOHCI_CTX_ASYNC);
    546 	fwohci_ctx_alloc(sc, &sc->sc_ctx_arrs, OHCI_BUF_ARRS_CNT,
    547 	    OHCI_CTX_ASYNC_RX_RESPONSE, FWOHCI_CTX_ASYNC);
    548 	fwohci_ctx_alloc(sc, &sc->sc_ctx_atrq, 0, OHCI_CTX_ASYNC_TX_REQUEST,
    549 	    FWOHCI_CTX_ASYNC);
    550 	fwohci_ctx_alloc(sc, &sc->sc_ctx_atrs, 0, OHCI_CTX_ASYNC_TX_RESPONSE,
    551 	    FWOHCI_CTX_ASYNC);
    552 	sc->sc_ctx_as = malloc(sizeof(sc->sc_ctx_as[0]) * sc->sc_isoctx,
    553 	    M_DEVBUF, M_WAITOK);
    554 	if (sc->sc_ctx_as == NULL) {
    555 		printf("no asynchronous stream\n");
    556 	} else {
    557 		for (i = 0; i < sc->sc_isoctx; i++)
    558 			sc->sc_ctx_as[i] = NULL;
    559 	}
    560 	sc->sc_ctx_ir = malloc(sizeof(sc->sc_ctx_ir[0]) * sc->sc_isoctx,
    561 	    M_DEVBUF, M_WAITOK|M_ZERO);
    562 	sc->sc_ctx_it = malloc(sizeof(sc->sc_ctx_it[0]) * sc->sc_itctx,
    563 	    M_DEVBUF, M_WAITOK|M_ZERO);
    564 
    565 	/*
    566 	 * Allocate buffer for configuration ROM and SelfID buffer
    567 	 */
    568 	fwohci_buf_alloc(sc, &sc->sc_buf_cnfrom);
    569 	fwohci_buf_alloc(sc, &sc->sc_buf_selfid);
    570 
    571 	callout_init(&sc->sc_selfid_callout);
    572 
    573 	sc->sc_sc1394.sc1394_ifinreg = fwohci_if_inreg;
    574 	sc->sc_sc1394.sc1394_ifoutput = fwohci_if_output;
    575 	sc->sc_sc1394.sc1394_ifsetiso = fwohci_if_setiso;
    576 
    577 	sc->sc_sc1394.sc1394_ir_open = fwohci_ir_ctx_set;
    578 	sc->sc_sc1394.sc1394_ir_close = fwohci_ir_ctx_clear;
    579 	sc->sc_sc1394.sc1394_ir_read = fwohci_ir_read;
    580 	sc->sc_sc1394.sc1394_ir_wait = fwohci_ir_wait;
    581 	sc->sc_sc1394.sc1394_ir_select = fwohci_ir_select;
    582 
    583 #if 0
    584 	sc->sc_sc1394.sc1394_it_open = fwohci_it_open;
    585 	sc->sc_sc1394.sc1394_it_write = fwohci_it_write;
    586 	sc->sc_sc1394.sc1394_it_close = fwohci_it_close;
    587 	/* XXX: need fwohci_it_flush? */
    588 #endif
    589 
    590 	/*
    591 	 * establish hooks for shutdown and suspend/resume
    592 	 */
    593 	sc->sc_shutdownhook = shutdownhook_establish(fwohci_shutdown, sc);
    594 	sc->sc_powerhook = powerhook_establish(fwohci_power, sc);
    595 
    596 	sc->sc_sc1394.sc1394_if = config_found(&sc->sc_sc1394.sc1394_dev, "fw",
    597 	    fwohci_print);
    598 
    599 #if NFWISO > 0
    600 	fwiso_register_if(&sc->sc_sc1394);
    601 #endif
    602 
    603 	/* Main loop. It's not coming back normally. */
    604 
    605 	fwohci_event_thread(sc);
    606 
    607 	kthread_exit(0);
    608 }
    609 
    610 static void
    611 fwohci_event_thread(struct fwohci_softc *sc)
    612 {
    613 	int i, s;
    614 	u_int32_t intmask, iso;
    615 
    616 	s = splbio();
    617 
    618 	/*
    619 	 * Initialize hardware registers.
    620 	 */
    621 
    622 	fwohci_hw_init(sc);
    623 
    624 	/* Initial Bus Reset */
    625 	fwohci_phy_busreset(sc);
    626 	splx(s);
    627 
    628 	while (!sc->sc_dying) {
    629 		s = splbio();
    630 		intmask = sc->sc_intmask;
    631 		if (intmask == 0) {
    632 			tsleep(fwohci_event_thread, PZERO, "fwohciev", 0);
    633 			splx(s);
    634 			continue;
    635 		}
    636 		sc->sc_intmask = 0;
    637 		splx(s);
    638 
    639 		if (intmask & OHCI_Int_BusReset) {
    640 			fwohci_buf_stop_tx(sc);
    641 			if (sc->sc_uidtbl != NULL) {
    642 				free(sc->sc_uidtbl, M_DEVBUF);
    643 				sc->sc_uidtbl = NULL;
    644 			}
    645 
    646 			callout_reset(&sc->sc_selfid_callout,
    647 			    OHCI_SELFID_TIMEOUT,
    648 			    (void (*)(void *))fwohci_phy_busreset, sc);
    649 			sc->sc_nodeid = 0xffff;	/* indicate invalid */
    650 			sc->sc_rootid = 0;
    651 			sc->sc_irmid = IEEE1394_BCAST_PHY_ID;
    652 		}
    653 		if (intmask & OHCI_Int_SelfIDComplete) {
    654 			s = splbio();
    655 			OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    656 			    OHCI_Int_BusReset);
    657 			OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet,
    658 			    OHCI_Int_BusReset);
    659 			splx(s);
    660 			callout_stop(&sc->sc_selfid_callout);
    661 			if (fwohci_selfid_input(sc) == 0) {
    662 				fwohci_buf_start_rx(sc);
    663 				fwohci_uid_collect(sc);
    664 			}
    665 		}
    666 		if (intmask & OHCI_Int_ReqTxComplete)
    667 			fwohci_at_done(sc, sc->sc_ctx_atrq, 0);
    668 		if (intmask & OHCI_Int_RespTxComplete)
    669 			fwohci_at_done(sc, sc->sc_ctx_atrs, 0);
    670 		if (intmask & OHCI_Int_RQPkt)
    671 			fwohci_arrq_input(sc, sc->sc_ctx_arrq);
    672 		if (intmask & OHCI_Int_RSPkt)
    673 			fwohci_arrs_input(sc, sc->sc_ctx_arrs);
    674 		if (intmask & OHCI_Int_IsochRx) {
    675 			if (sc->sc_ctx_as == NULL) {
    676 				continue;
    677 			}
    678 			s = splbio();
    679 			iso = sc->sc_iso;
    680 			sc->sc_iso = 0;
    681 			splx(s);
    682 			for (i = 0; i < sc->sc_isoctx; i++) {
    683 				if ((iso & (1 << i)) &&
    684 				    sc->sc_ctx_as[i] != NULL) {
    685 					fwohci_as_input(sc, sc->sc_ctx_as[i]);
    686 					sc->sc_ascnt.ev_count++;
    687 				}
    688 			}
    689 		}
    690 	}
    691 }
    692 
    693 #if 0
    694 static int
    695 fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
    696     bus_dmamap_t *mapp, caddr_t *kvap, int flags)
    697 {
    698 	bus_dma_segment_t segs[1];
    699 	int error, nsegs, steps;
    700 
    701 	steps = 0;
    702 	error = bus_dmamem_alloc(sc->sc_dmat, size, alignment, alignment,
    703 	    segs, 1, &nsegs, flags);
    704 	if (error)
    705 		goto cleanup;
    706 
    707 	steps = 1;
    708 	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, segs[0].ds_len,
    709 	    kvap, flags);
    710 	if (error)
    711 		goto cleanup;
    712 
    713 	if (error == 0)
    714 		error = bus_dmamap_create(sc->sc_dmat, size, 1, alignment,
    715 		    size, flags, mapp);
    716 	if (error)
    717 		goto cleanup;
    718 	if (error == 0)
    719 		error = bus_dmamap_load(sc->sc_dmat, *mapp, *kvap, size, NULL,
    720 		    flags);
    721 	if (error)
    722 		goto cleanup;
    723 
    724  cleanup:
    725 	switch (steps) {
    726 	case 1:
    727 		bus_dmamem_free(sc->sc_dmat, segs, nsegs);
    728 	}
    729 
    730 	return error;
    731 }
    732 #endif
    733 
    734 int
    735 fwohci_print(void *aux, const char *pnp)
    736 {
    737 	char *name = aux;
    738 
    739 	if (pnp)
    740 		printf("%s at %s", name, pnp);
    741 
    742 	return UNCONF;
    743 }
    744 
    745 static void
    746 fwohci_hw_init(struct fwohci_softc *sc)
    747 {
    748 	int i;
    749 	u_int32_t val;
    750 
    751 	/*
    752 	 * Software Reset.
    753 	 */
    754 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
    755 	for (i = 0; i < OHCI_LOOP; i++) {
    756 		val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
    757 		if ((val & OHCI_HCControl_SoftReset) == 0)
    758 			break;
    759 		DELAY(10);
    760 	}
    761 
    762 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
    763 
    764 	/*
    765 	 * First, initilize CSRs with undefined value to default settings.
    766 	 */
    767 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    768 	val |= OHCI_BusOptions_ISC | OHCI_BusOptions_CMC;
    769 #if 0
    770 	val |= OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC;
    771 #else
    772 	val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC);
    773 #endif
    774 	OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
    775 	for (i = 0; i < sc->sc_isoctx; i++) {
    776 		OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlClear,
    777 		    ~0);
    778 	}
    779 	for (i = 0; i < sc->sc_itctx; i++) {
    780 		OHCI_SYNC_TX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlClear,
    781 		    ~0);
    782 	}
    783 	OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear, ~0);
    784 
    785 	fwohci_configrom_init(sc);
    786 	fwohci_selfid_init(sc);
    787 	fwohci_buf_init_rx(sc);
    788 	fwohci_csr_init(sc);
    789 
    790 	/*
    791 	 * Final CSR settings.
    792 	 */
    793 	OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
    794 	    OHCI_LinkControl_CycleTimerEnable |
    795 	    OHCI_LinkControl_RcvSelfID | OHCI_LinkControl_RcvPhyPkt);
    796 
    797 	OHCI_CSR_WRITE(sc, OHCI_REG_ATRetries, 0x00000888);	/*XXX*/
    798 
    799 	/* clear receive filter */
    800 	OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskHiClear, ~0);
    801 	OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskLoClear, ~0);
    802 	OHCI_CSR_WRITE(sc, OHCI_REG_AsynchronousRequestFilterHiSet, 0x80000000);
    803 
    804 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear,
    805 	    OHCI_HCControl_NoByteSwapData | OHCI_HCControl_APhyEnhanceEnable);
    806 #if BYTE_ORDER == BIG_ENDIAN
    807 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet,
    808 	    OHCI_HCControl_NoByteSwapData);
    809 #endif
    810 
    811 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, ~0);
    812 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset |
    813 	    OHCI_Int_SelfIDComplete | OHCI_Int_IsochRx | OHCI_Int_IsochTx |
    814 	    OHCI_Int_RSPkt | OHCI_Int_RQPkt | OHCI_Int_ARRS | OHCI_Int_ARRQ |
    815 	    OHCI_Int_RespTxComplete | OHCI_Int_ReqTxComplete);
    816 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_CycleTooLong |
    817 	    OHCI_Int_UnrecoverableError | OHCI_Int_CycleInconsistent |
    818 	    OHCI_Int_LockRespErr | OHCI_Int_PostedWriteErr);
    819 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntMaskSet, ~0);
    820 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
    821 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_MasterEnable);
    822 
    823 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LinkEnable);
    824 
    825 	/*
    826 	 * Start the receivers
    827 	 */
    828 	fwohci_buf_start_rx(sc);
    829 }
    830 
    831 static void
    832 fwohci_power(int why, void *arg)
    833 {
    834 	struct fwohci_softc *sc = arg;
    835 	int s;
    836 
    837 	s = splbio();
    838 	switch (why) {
    839 	case PWR_SUSPEND:
    840 	case PWR_STANDBY:
    841 		fwohci_shutdown(sc);
    842 		break;
    843 	case PWR_RESUME:
    844 		fwohci_hw_init(sc);
    845 		fwohci_phy_busreset(sc);
    846 		break;
    847 	case PWR_SOFTSUSPEND:
    848 	case PWR_SOFTSTANDBY:
    849 	case PWR_SOFTRESUME:
    850 		break;
    851 	}
    852 	splx(s);
    853 }
    854 
    855 static void
    856 fwohci_shutdown(void *arg)
    857 {
    858 	struct fwohci_softc *sc = arg;
    859 	u_int32_t val;
    860 
    861 	callout_stop(&sc->sc_selfid_callout);
    862 	/* disable all interrupt */
    863 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, OHCI_Int_MasterEnable);
    864 	fwohci_buf_stop_tx(sc);
    865 	fwohci_buf_stop_rx(sc);
    866 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    867 	val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_ISC |
    868 		OHCI_BusOptions_CMC | OHCI_BusOptions_IRMC);
    869 	OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
    870 	fwohci_phy_busreset(sc);
    871 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear, OHCI_HCControl_LinkEnable);
    872 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear, OHCI_HCControl_LPS);
    873 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
    874 }
    875 
    876 /*
    877  * COMMON FUNCTIONS
    878  */
    879 
    880 /*
    881  * read the PHY Register.
    882  */
    883 static u_int8_t
    884 fwohci_phy_read(struct fwohci_softc *sc, u_int8_t reg)
    885 {
    886 	int i;
    887 	u_int32_t val;
    888 
    889 	OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl,
    890 	    OHCI_PhyControl_RdReg | (reg << OHCI_PhyControl_RegAddr_BITPOS));
    891 	for (i = 0; i < OHCI_LOOP; i++) {
    892 		if (OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
    893 		    OHCI_PhyControl_RdDone)
    894 			break;
    895 		DELAY(10);
    896 	}
    897 	val = OHCI_CSR_READ(sc, OHCI_REG_PhyControl);
    898 	return (val & OHCI_PhyControl_RdData) >> OHCI_PhyControl_RdData_BITPOS;
    899 }
    900 
    901 /*
    902  * write the PHY Register.
    903  */
    904 static void
    905 fwohci_phy_write(struct fwohci_softc *sc, u_int8_t reg, u_int8_t val)
    906 {
    907 	int i;
    908 
    909 	OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl, OHCI_PhyControl_WrReg |
    910 	    (reg << OHCI_PhyControl_RegAddr_BITPOS) |
    911 	    (val << OHCI_PhyControl_WrData_BITPOS));
    912 	for (i = 0; i < OHCI_LOOP; i++) {
    913 		if (!(OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
    914 		    OHCI_PhyControl_WrReg))
    915 			break;
    916 		DELAY(10);
    917 	}
    918 }
    919 
    920 /*
    921  * Initiate Bus Reset
    922  */
    923 static void
    924 fwohci_phy_busreset(struct fwohci_softc *sc)
    925 {
    926 	int s;
    927 	u_int8_t val;
    928 
    929 	s = splbio();
    930 	OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    931 	    OHCI_Int_BusReset | OHCI_Int_SelfIDComplete);
    932 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset);
    933 	callout_stop(&sc->sc_selfid_callout);
    934 	val = fwohci_phy_read(sc, 1);
    935 	val = (val & 0x80) |			/* preserve RHB (force root) */
    936 	    0x40 |				/* Initiate Bus Reset */
    937 	    0x3f;				/* default GAP count */
    938 	fwohci_phy_write(sc, 1, val);
    939 	splx(s);
    940 }
    941 
    942 /*
    943  * PHY Packet
    944  */
    945 static void
    946 fwohci_phy_input(struct fwohci_softc *sc, struct fwohci_pkt *pkt)
    947 {
    948 	u_int32_t val;
    949 
    950 	val = pkt->fp_hdr[1];
    951 	if (val != ~pkt->fp_hdr[2]) {
    952 		if (val == 0 && ((*pkt->fp_trail & 0x001f0000) >> 16) ==
    953 		    OHCI_CTXCTL_EVENT_BUS_RESET) {
    954 			DPRINTFN(1, ("fwohci_phy_input: BusReset: 0x%08x\n",
    955 			    pkt->fp_hdr[2]));
    956 		} else {
    957 			printf("%s: phy packet corrupted (0x%08x, 0x%08x)\n",
    958 			    sc->sc_sc1394.sc1394_dev.dv_xname, val,
    959 			    pkt->fp_hdr[2]);
    960 		}
    961 		return;
    962 	}
    963 #ifdef FW_DEBUG
    964 	if (fwdebug > 1)
    965 		fwohci_show_phypkt(sc, val);
    966 #endif
    967 }
    968 
    969 /*
    970  * Descriptor for context DMA.
    971  */
    972 static int
    973 fwohci_desc_alloc(struct fwohci_softc *sc)
    974 {
    975 	int error, mapsize, dsize;
    976 
    977 	/*
    978 	 * allocate descriptor buffer
    979 	 */
    980 
    981 	sc->sc_descsize = OHCI_BUF_ARRQ_CNT + OHCI_BUF_ARRS_CNT +
    982 	    OHCI_BUF_ATRQ_CNT + OHCI_BUF_ATRS_CNT +
    983 	    OHCI_BUF_IR_CNT * sc->sc_isoctx + 2;
    984 	dsize = sizeof(struct fwohci_desc) * sc->sc_descsize;
    985 	mapsize = howmany(sc->sc_descsize, NBBY);
    986 	sc->sc_descmap = malloc(mapsize, M_DEVBUF, M_WAITOK|M_ZERO);
    987 
    988 	if (sc->sc_descmap == NULL) {
    989 		printf("fwohci_desc_alloc: cannot get memory\n");
    990 		return -1;
    991 	}
    992 
    993 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dsize, PAGE_SIZE, 0,
    994 	    &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
    995 		printf("%s: unable to allocate descriptor buffer, error = %d\n",
    996 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    997 		goto fail_0;
    998 	}
    999 
   1000 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
   1001 	    dsize, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT | BUS_DMA_WAITOK))
   1002 	    != 0) {
   1003 		printf("%s: unable to map descriptor buffer, error = %d\n",
   1004 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1005 		goto fail_1;
   1006 	}
   1007 
   1008 	if ((error = bus_dmamap_create(sc->sc_dmat, dsize, sc->sc_dnseg,
   1009 	    dsize, 0, BUS_DMA_WAITOK, &sc->sc_ddmamap)) != 0) {
   1010 		printf("%s: unable to create descriptor buffer DMA map, "
   1011 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1012 		goto fail_2;
   1013 	}
   1014 
   1015 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
   1016 	    dsize, NULL, BUS_DMA_WAITOK)) != 0) {
   1017 		printf("%s: unable to load descriptor buffer DMA map, "
   1018 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1019 		goto fail_3;
   1020 	}
   1021 
   1022 	return 0;
   1023 
   1024   fail_3:
   1025 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1026   fail_2:
   1027 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, dsize);
   1028   fail_1:
   1029 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
   1030   fail_0:
   1031 	return error;
   1032 }
   1033 
   1034 static struct fwohci_desc *
   1035 fwohci_desc_get(struct fwohci_softc *sc, int ndesc)
   1036 {
   1037 	int i, n;
   1038 
   1039 	for (n = 0; n <= sc->sc_descsize - ndesc; n++) {
   1040 		for (i = 0; ; i++) {
   1041 			if (i == ndesc) {
   1042 				for (i = 0; i < ndesc; i++)
   1043 					setbit(sc->sc_descmap, n + i);
   1044 				return sc->sc_desc + n;
   1045 			}
   1046 			if (isset(sc->sc_descmap, n + i))
   1047 				break;
   1048 		}
   1049 	}
   1050 	return NULL;
   1051 }
   1052 
   1053 static void
   1054 fwohci_desc_put(struct fwohci_softc *sc, struct fwohci_desc *fd, int ndesc)
   1055 {
   1056 	int i, n;
   1057 
   1058 	n = fd - sc->sc_desc;
   1059 	for (i = 0; i < ndesc; i++, n++) {
   1060 #ifdef DIAGNOSTIC
   1061 		if (isclr(sc->sc_descmap, n))
   1062 			panic("fwohci_desc_put: duplicated free");
   1063 #endif
   1064 		clrbit(sc->sc_descmap, n);
   1065 	}
   1066 }
   1067 
   1068 /*
   1069  * Asyncronous/Isochronous Transmit/Receive Context
   1070  */
   1071 static int
   1072 fwohci_ctx_alloc(struct fwohci_softc *sc, struct fwohci_ctx **fcp,
   1073     int bufcnt, int ctx, int ctxtype)
   1074 {
   1075 	int i, error;
   1076 	struct fwohci_ctx *fc;
   1077 	struct fwohci_buf *fb;
   1078 	struct fwohci_desc *fd;
   1079 #if DOUBLEBUF
   1080 	int buf2cnt;
   1081 #endif
   1082 
   1083 	fc = malloc(sizeof(*fc), M_DEVBUF, M_WAITOK|M_ZERO);
   1084 	LIST_INIT(&fc->fc_handler);
   1085 	TAILQ_INIT(&fc->fc_buf);
   1086 	fc->fc_ctx = ctx;
   1087 	fc->fc_buffers = fb = malloc(sizeof(*fb) * bufcnt, M_DEVBUF, M_WAITOK|M_ZERO);
   1088 	fc->fc_bufcnt = bufcnt;
   1089 #if DOUBLEBUF
   1090 	TAILQ_INIT(&fc->fc_buf2); /* for isochronous */
   1091 	if (ctxtype == FWOHCI_CTX_ISO_MULTI) {
   1092 		buf2cnt = bufcnt/2;
   1093 		bufcnt -= buf2cnt;
   1094 		if (buf2cnt == 0) {
   1095 			panic("cannot allocate iso buffer");
   1096 		}
   1097 	}
   1098 #endif
   1099 	for (i = 0; i < bufcnt; i++, fb++) {
   1100 		if ((error = fwohci_buf_alloc(sc, fb)) != 0)
   1101 			goto fail;
   1102 		if ((fd = fwohci_desc_get(sc, 1)) == NULL) {
   1103 			error = ENOBUFS;
   1104 			goto fail;
   1105 		}
   1106 		fb->fb_desc = fd;
   1107 		fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
   1108 		    ((caddr_t)fd - (caddr_t)sc->sc_desc);
   1109 		fd->fd_flags = OHCI_DESC_INPUT | OHCI_DESC_STATUS |
   1110 		    OHCI_DESC_INTR_ALWAYS | OHCI_DESC_BRANCH;
   1111 		fd->fd_reqcount = fb->fb_dmamap->dm_segs[0].ds_len;
   1112 		fd->fd_data = fb->fb_dmamap->dm_segs[0].ds_addr;
   1113 		TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1114 	}
   1115 #if DOUBLEBUF
   1116 	if (ctxtype == FWOHCI_CTX_ISO_MULTI) {
   1117 		for (i = bufcnt; i < bufcnt + buf2cnt; i++, fb++) {
   1118 			if ((error = fwohci_buf_alloc(sc, fb)) != 0)
   1119 				goto fail;
   1120 			if ((fd = fwohci_desc_get(sc, 1)) == NULL) {
   1121 				error = ENOBUFS;
   1122 				goto fail;
   1123 			}
   1124 			fb->fb_desc = fd;
   1125 			fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
   1126 			    ((caddr_t)fd - (caddr_t)sc->sc_desc);
   1127 			bus_dmamap_sync(sc->sc_dmat, sc->sc_ddmamap,
   1128 			    (caddr_t)fd - (caddr_t)sc->sc_desc, sizeof(struct fwohci_desc),
   1129 			    BUS_DMASYNC_PREWRITE);
   1130 			fd->fd_flags = OHCI_DESC_INPUT | OHCI_DESC_STATUS |
   1131 			    OHCI_DESC_INTR_ALWAYS | OHCI_DESC_BRANCH;
   1132 			fd->fd_reqcount = fb->fb_dmamap->dm_segs[0].ds_len;
   1133 			fd->fd_data = fb->fb_dmamap->dm_segs[0].ds_addr;
   1134 			TAILQ_INSERT_TAIL(&fc->fc_buf2, fb, fb_list);
   1135 			bus_dmamap_sync(sc->sc_dmat, sc->sc_ddmamap,
   1136 			    (caddr_t)fd - (caddr_t)sc->sc_desc, sizeof(struct fwohci_desc),
   1137 			    BUS_DMASYNC_POSTWRITE);
   1138 		}
   1139 	}
   1140 #endif /* DOUBLEBUF */
   1141 	fc->fc_type = ctxtype;
   1142 	*fcp = fc;
   1143 	return 0;
   1144 
   1145   fail:
   1146 	while (i-- > 0) {
   1147 		fb--;
   1148 		if (fb->fb_desc)
   1149 			fwohci_desc_put(sc, fb->fb_desc, 1);
   1150 		fwohci_buf_free(sc, fb);
   1151 	}
   1152 	free(fc, M_DEVBUF);
   1153 	return error;
   1154 }
   1155 
   1156 static void
   1157 fwohci_ctx_free(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1158 {
   1159 	struct fwohci_buf *fb;
   1160 	struct fwohci_handler *fh;
   1161 
   1162 #if DOUBLEBUF
   1163 	if ((fc->fc_type == FWOHCI_CTX_ISO_MULTI) &&
   1164 	    (TAILQ_FIRST(&fc->fc_buf) > TAILQ_FIRST(&fc->fc_buf2))) {
   1165 		struct fwohci_buf_s fctmp;
   1166 
   1167 		fctmp = fc->fc_buf;
   1168 		fc->fc_buf = fc->fc_buf2;
   1169 		fc->fc_buf2 = fctmp;
   1170 	}
   1171 #endif
   1172 	while ((fh = LIST_FIRST(&fc->fc_handler)) != NULL)
   1173 		fwohci_handler_set(sc, fh->fh_tcode, fh->fh_key1, fh->fh_key2,
   1174 		    NULL, NULL);
   1175 	while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   1176 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1177 		if (fb->fb_desc)
   1178 			fwohci_desc_put(sc, fb->fb_desc, 1);
   1179 		fwohci_buf_free(sc, fb);
   1180 	}
   1181 #if DOUBLEBUF
   1182 	while ((fb = TAILQ_FIRST(&fc->fc_buf2)) != NULL) {
   1183 		TAILQ_REMOVE(&fc->fc_buf2, fb, fb_list);
   1184 		if (fb->fb_desc)
   1185 			fwohci_desc_put(sc, fb->fb_desc, 1);
   1186 		fwohci_buf_free(sc, fb);
   1187 	}
   1188 #endif /* DOUBLEBUF */
   1189 	free(fc->fc_buffers, M_DEVBUF);
   1190 	free(fc, M_DEVBUF);
   1191 }
   1192 
   1193 static void
   1194 fwohci_ctx_init(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1195 {
   1196 	struct fwohci_buf *fb, *nfb;
   1197 	struct fwohci_desc *fd;
   1198 	struct fwohci_handler *fh;
   1199 	int n;
   1200 
   1201 	for (fb = TAILQ_FIRST(&fc->fc_buf); fb != NULL; fb = nfb) {
   1202 		nfb = TAILQ_NEXT(fb, fb_list);
   1203 		fb->fb_off = 0;
   1204 		fd = fb->fb_desc;
   1205 		fd->fd_branch = (nfb != NULL) ? (nfb->fb_daddr | 1) : 0;
   1206 		fd->fd_rescount = fd->fd_reqcount;
   1207 	}
   1208 
   1209 #if DOUBLEBUF
   1210 	for (fb = TAILQ_FIRST(&fc->fc_buf2); fb != NULL; fb = nfb) {
   1211 		bus_dmamap_sync(sc->sc_dmat, sc->sc_ddmamap,
   1212 		    (caddr_t)fd - (caddr_t)sc->sc_desc, sizeof(struct fwohci_desc),
   1213 		    BUS_DMASYNC_PREWRITE);
   1214 		nfb = TAILQ_NEXT(fb, fb_list);
   1215 		fb->fb_off = 0;
   1216 		fd = fb->fb_desc;
   1217 		fd->fd_branch = (nfb != NULL) ? (nfb->fb_daddr | 1) : 0;
   1218 		fd->fd_rescount = fd->fd_reqcount;
   1219 		bus_dmamap_sync(sc->sc_dmat, sc->sc_ddmamap,
   1220 		    (caddr_t)fd - (caddr_t)sc->sc_desc, sizeof(struct fwohci_desc),
   1221 		    BUS_DMASYNC_POSTWRITE);
   1222 	}
   1223 #endif /* DOUBLEBUF */
   1224 
   1225 	n = fc->fc_ctx;
   1226 	fb = TAILQ_FIRST(&fc->fc_buf);
   1227 	if (fc->fc_type != FWOHCI_CTX_ASYNC) {
   1228 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_CommandPtr,
   1229 		    fb->fb_daddr | 1);
   1230 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlClear,
   1231 		    OHCI_CTXCTL_RX_BUFFER_FILL |
   1232 		    OHCI_CTXCTL_RX_CYCLE_MATCH_ENABLE |
   1233 		    OHCI_CTXCTL_RX_MULTI_CHAN_MODE |
   1234 		    OHCI_CTXCTL_RX_DUAL_BUFFER_MODE);
   1235 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlSet,
   1236 		    OHCI_CTXCTL_RX_ISOCH_HEADER);
   1237 		if (fc->fc_type == FWOHCI_CTX_ISO_MULTI) {
   1238 			OHCI_SYNC_RX_DMA_WRITE(sc, n,
   1239 			    OHCI_SUBREG_ContextControlSet,
   1240 			    OHCI_CTXCTL_RX_BUFFER_FILL);
   1241 		}
   1242 		fh = LIST_FIRST(&fc->fc_handler);
   1243 
   1244 		if (fh->fh_key1 == IEEE1394_ISO_CHANNEL_ANY) {
   1245 			OHCI_SYNC_RX_DMA_WRITE(sc, n,
   1246 			    OHCI_SUBREG_ContextControlSet,
   1247 			    OHCI_CTXCTL_RX_MULTI_CHAN_MODE);
   1248 
   1249 			/* Receive all the isochronous channels */
   1250 			OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskHiSet,
   1251 			    0xffffffff);
   1252 			OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskLoSet,
   1253 			    0xffffffff);
   1254 			DPRINTF(("%s: CTXCTL 0x%08x\n",
   1255 			    sc->sc_sc1394.sc1394_dev.dv_xname,
   1256 			    OHCI_SYNC_RX_DMA_READ(sc, n,
   1257 				OHCI_SUBREG_ContextControlSet)));
   1258 		}
   1259 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextMatch,
   1260 		    (fh->fh_key2 << OHCI_CTXMATCH_TAG_BITPOS) |
   1261 		    (fh->fh_key1 & IEEE1394_ISO_CHANNEL_MASK));
   1262 	} else {
   1263 		OHCI_ASYNC_DMA_WRITE(sc, n, OHCI_SUBREG_CommandPtr,
   1264 		    fb->fb_daddr | 1);
   1265 	}
   1266 }
   1267 
   1268 /*
   1269  * DMA data buffer
   1270  */
   1271 static int
   1272 fwohci_buf_alloc(struct fwohci_softc *sc, struct fwohci_buf *fb)
   1273 {
   1274 	int error;
   1275 
   1276 	if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
   1277 	    PAGE_SIZE, &fb->fb_seg, 1, &fb->fb_nseg, BUS_DMA_WAITOK)) != 0) {
   1278 		printf("%s: unable to allocate buffer, error = %d\n",
   1279 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1280 		goto fail_0;
   1281 	}
   1282 
   1283 	if ((error = bus_dmamem_map(sc->sc_dmat, &fb->fb_seg,
   1284 	    fb->fb_nseg, PAGE_SIZE, &fb->fb_buf, BUS_DMA_WAITOK)) != 0) {
   1285 		printf("%s: unable to map buffer, error = %d\n",
   1286 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1287 		goto fail_1;
   1288 	}
   1289 
   1290 	if ((error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, fb->fb_nseg,
   1291 	    PAGE_SIZE, 0, BUS_DMA_WAITOK, &fb->fb_dmamap)) != 0) {
   1292 		printf("%s: unable to create buffer DMA map, "
   1293 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
   1294 		    error);
   1295 		goto fail_2;
   1296 	}
   1297 
   1298 	if ((error = bus_dmamap_load(sc->sc_dmat, fb->fb_dmamap,
   1299 	    fb->fb_buf, PAGE_SIZE, NULL, BUS_DMA_WAITOK)) != 0) {
   1300 		printf("%s: unable to load buffer DMA map, "
   1301 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
   1302 		    error);
   1303 		goto fail_3;
   1304 	}
   1305 
   1306 	return 0;
   1307 
   1308 	bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
   1309   fail_3:
   1310 	bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   1311   fail_2:
   1312 	bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
   1313   fail_1:
   1314 	bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
   1315   fail_0:
   1316 	return error;
   1317 }
   1318 
   1319 static void
   1320 fwohci_buf_free(struct fwohci_softc *sc, struct fwohci_buf *fb)
   1321 {
   1322 
   1323 	bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
   1324 	bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   1325 	bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
   1326 	bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
   1327 }
   1328 
   1329 static void
   1330 fwohci_buf_init_rx(struct fwohci_softc *sc)
   1331 {
   1332 	int i;
   1333 
   1334 	/*
   1335 	 * Initialize for Asynchronous Receive Queue.
   1336 	 */
   1337 	fwohci_ctx_init(sc, sc->sc_ctx_arrq);
   1338 	fwohci_ctx_init(sc, sc->sc_ctx_arrs);
   1339 
   1340 	/*
   1341 	 * Initialize for Isochronous Receive Queue.
   1342 	 */
   1343 	if (sc->sc_ctx_as != NULL) {
   1344 		for (i = 0; i < sc->sc_isoctx; i++) {
   1345 			if (sc->sc_ctx_as[i] != NULL)
   1346 				fwohci_ctx_init(sc, sc->sc_ctx_as[i]);
   1347 		}
   1348 	}
   1349 }
   1350 
   1351 static void
   1352 fwohci_buf_start_rx(struct fwohci_softc *sc)
   1353 {
   1354 	int i;
   1355 
   1356 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   1357 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1358 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   1359 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1360 	if (sc->sc_ctx_as != NULL) {
   1361 		for (i = 0; i < sc->sc_isoctx; i++) {
   1362 			if (sc->sc_ctx_as[i] != NULL)
   1363 				OHCI_SYNC_RX_DMA_WRITE(sc, i,
   1364 				    OHCI_SUBREG_ContextControlSet,
   1365 				    OHCI_CTXCTL_RUN);
   1366 		}
   1367 	}
   1368 }
   1369 
   1370 static void
   1371 fwohci_buf_stop_tx(struct fwohci_softc *sc)
   1372 {
   1373 	int i;
   1374 
   1375 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   1376 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1377 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   1378 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1379 
   1380 	/*
   1381 	 * Make sure the transmitter is stopped.
   1382 	 */
   1383 	for (i = 0; i < OHCI_LOOP; i++) {
   1384 		DELAY(10);
   1385 		if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   1386 		    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
   1387 			continue;
   1388 		if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   1389 		    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
   1390 			continue;
   1391 		break;
   1392 	}
   1393 
   1394 	/*
   1395 	 * Initialize for Asynchronous Transmit Queue.
   1396 	 */
   1397 	fwohci_at_done(sc, sc->sc_ctx_atrq, 1);
   1398 	fwohci_at_done(sc, sc->sc_ctx_atrs, 1);
   1399 }
   1400 
   1401 static void
   1402 fwohci_buf_stop_rx(struct fwohci_softc *sc)
   1403 {
   1404 	int i;
   1405 
   1406 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   1407 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1408 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   1409 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1410 	for (i = 0; i < sc->sc_isoctx; i++) {
   1411 		OHCI_SYNC_RX_DMA_WRITE(sc, i,
   1412 		    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1413 	}
   1414 }
   1415 
   1416 static void
   1417 fwohci_buf_next(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1418 {
   1419 	struct fwohci_buf *fb, *tfb;
   1420 
   1421 #if DOUBLEBUF
   1422 	if (fc->fc_type != FWOHCI_CTX_ISO_MULTI) {
   1423 #endif
   1424 		while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   1425 			if (fc->fc_type) {
   1426 				if (fb->fb_off == 0)
   1427 					break;
   1428 			} else {
   1429 				if (fb->fb_off != fb->fb_desc->fd_reqcount ||
   1430 				    fb->fb_desc->fd_rescount != 0)
   1431 					break;
   1432 			}
   1433 			TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1434 			fb->fb_desc->fd_rescount = fb->fb_desc->fd_reqcount;
   1435 			fb->fb_off = 0;
   1436 			fb->fb_desc->fd_branch = 0;
   1437 			tfb = TAILQ_LAST(&fc->fc_buf, fwohci_buf_s);
   1438 			tfb->fb_desc->fd_branch = fb->fb_daddr | 1;
   1439 			TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1440 		}
   1441 #if DOUBLEBUF
   1442 	} else {
   1443 		struct fwohci_buf_s fctmp;
   1444 
   1445 		/* cleaning buffer */
   1446 		for (fb = TAILQ_FIRST(&fc->fc_buf); fb != NULL;
   1447 		     fb = TAILQ_NEXT(fb, fb_list)) {
   1448 			fb->fb_off = 0;
   1449 			fb->fb_desc->fd_rescount = fb->fb_desc->fd_reqcount;
   1450 		}
   1451 
   1452 		/* rotating buffer */
   1453 		fctmp = fc->fc_buf;
   1454 		fc->fc_buf = fc->fc_buf2;
   1455 		fc->fc_buf2 = fctmp;
   1456 	}
   1457 #endif
   1458 }
   1459 
   1460 static int
   1461 fwohci_buf_pktget(struct fwohci_softc *sc, struct fwohci_buf **fbp, caddr_t *pp,
   1462     int len)
   1463 {
   1464 	struct fwohci_buf *fb;
   1465 	struct fwohci_desc *fd;
   1466 	int bufend;
   1467 
   1468 	fb = *fbp;
   1469   again:
   1470 	fd = fb->fb_desc;
   1471 	DPRINTFN(1, ("fwohci_buf_pktget: desc %ld, off %d, req %d, res %d,"
   1472 	    " len %d, avail %d\n", (long)(fd - sc->sc_desc), fb->fb_off,
   1473 	    fd->fd_reqcount, fd->fd_rescount, len,
   1474 	    fd->fd_reqcount - fd->fd_rescount - fb->fb_off));
   1475 	bufend = fd->fd_reqcount - fd->fd_rescount;
   1476 	if (fb->fb_off >= bufend) {
   1477 		DPRINTFN(5, ("buf %x finish req %d res %d off %d ",
   1478 		    fb->fb_desc->fd_data, fd->fd_reqcount, fd->fd_rescount,
   1479 		    fb->fb_off));
   1480 		if (fd->fd_rescount == 0) {
   1481 			*fbp = fb = TAILQ_NEXT(fb, fb_list);
   1482 			if (fb != NULL)
   1483 				goto again;
   1484 		}
   1485 		return 0;
   1486 	}
   1487 	if (fb->fb_off + len > bufend)
   1488 		len = bufend - fb->fb_off;
   1489 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, fb->fb_off, len,
   1490 	    BUS_DMASYNC_POSTREAD);
   1491 	*pp = fb->fb_buf + fb->fb_off;
   1492 	fb->fb_off += roundup(len, 4);
   1493 	return len;
   1494 }
   1495 
   1496 static int
   1497 fwohci_buf_input(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   1498     struct fwohci_pkt *pkt)
   1499 {
   1500 	caddr_t p;
   1501 	struct fwohci_buf *fb;
   1502 	int len, count, i;
   1503 #ifdef FW_DEBUG
   1504 	int tlabel;
   1505 #endif
   1506 
   1507 	memset(pkt, 0, sizeof(*pkt));
   1508 	pkt->fp_uio.uio_iov = pkt->fp_iov;
   1509 	pkt->fp_uio.uio_rw = UIO_WRITE;
   1510 	pkt->fp_uio.uio_segflg = UIO_SYSSPACE;
   1511 
   1512 	/* get first quadlet */
   1513 	fb = TAILQ_FIRST(&fc->fc_buf);
   1514 	count = 4;
   1515 	len = fwohci_buf_pktget(sc, &fb, &p, count);
   1516 	if (len <= 0) {
   1517 		DPRINTFN(1, ("fwohci_buf_input: no input for %d\n",
   1518 		    fc->fc_ctx));
   1519 		return 0;
   1520 	}
   1521 	pkt->fp_hdr[0] = *(u_int32_t *)p;
   1522 	pkt->fp_tcode = (pkt->fp_hdr[0] & 0x000000f0) >> 4;
   1523 	switch (pkt->fp_tcode) {
   1524 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1525 	case IEEE1394_TCODE_READ_RESP_QUAD:
   1526 		pkt->fp_hlen = 12;
   1527 		pkt->fp_dlen = 4;
   1528 		break;
   1529 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   1530 		pkt->fp_hlen = 16;
   1531 		pkt->fp_dlen = 0;
   1532 		break;
   1533 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1534 	case IEEE1394_TCODE_READ_RESP_BLOCK:
   1535 	case IEEE1394_TCODE_LOCK_REQ:
   1536 	case IEEE1394_TCODE_LOCK_RESP:
   1537 		pkt->fp_hlen = 16;
   1538 		break;
   1539 	case IEEE1394_TCODE_STREAM_DATA:
   1540 #ifdef DIAGNOSTIC
   1541 		if (fc->fc_type == FWOHCI_CTX_ISO_MULTI)
   1542 #endif
   1543 		{
   1544 			pkt->fp_hlen = 4;
   1545 			pkt->fp_dlen = pkt->fp_hdr[0] >> 16;
   1546 			DPRINTFN(5, ("[%d]", pkt->fp_dlen));
   1547 			break;
   1548 		}
   1549 #ifdef DIAGNOSTIC
   1550 		else {
   1551 			printf("fwohci_buf_input: bad tcode: STREAM_DATA\n");
   1552 			return 0;
   1553 		}
   1554 #endif
   1555 	default:
   1556 		pkt->fp_hlen = 12;
   1557 		pkt->fp_dlen = 0;
   1558 		break;
   1559 	}
   1560 
   1561 	/* get header */
   1562 	while (count < pkt->fp_hlen) {
   1563 		len = fwohci_buf_pktget(sc, &fb, &p, pkt->fp_hlen - count);
   1564 		if (len == 0) {
   1565 			printf("fwohci_buf_input: malformed input 1: %d\n",
   1566 			    pkt->fp_hlen - count);
   1567 			return 0;
   1568 		}
   1569 		memcpy((caddr_t)pkt->fp_hdr + count, p, len);
   1570 		count += len;
   1571 	}
   1572 	if (pkt->fp_hlen == 16 &&
   1573 	    pkt->fp_tcode != IEEE1394_TCODE_READ_REQ_BLOCK)
   1574 		pkt->fp_dlen = pkt->fp_hdr[3] >> 16;
   1575 #ifdef FW_DEBUG
   1576 	tlabel = (pkt->fp_hdr[0] & 0x0000fc00) >> 10;
   1577 #endif
   1578 	DPRINTFN(1, ("fwohci_buf_input: tcode=0x%x, tlabel=0x%x, hlen=%d, "
   1579 	    "dlen=%d\n", pkt->fp_tcode, tlabel, pkt->fp_hlen, pkt->fp_dlen));
   1580 
   1581 	/* get data */
   1582 	count = 0;
   1583 	i = 0;
   1584 	while (count < pkt->fp_dlen) {
   1585 		len = fwohci_buf_pktget(sc, &fb,
   1586 		    (caddr_t *)&pkt->fp_iov[i].iov_base,
   1587 		    pkt->fp_dlen - count);
   1588 		if (len == 0) {
   1589 			printf("fwohci_buf_input: malformed input 2: %d\n",
   1590 			    pkt->fp_dlen - count);
   1591 			return 0;
   1592 		}
   1593 		pkt->fp_iov[i++].iov_len = len;
   1594 		count += len;
   1595 	}
   1596 	pkt->fp_uio.uio_iovcnt = i;
   1597 	pkt->fp_uio.uio_resid = count;
   1598 
   1599 	/* get trailer */
   1600 	len = fwohci_buf_pktget(sc, &fb, (caddr_t *)&pkt->fp_trail,
   1601 	    sizeof(*pkt->fp_trail));
   1602 	if (len <= 0) {
   1603 		printf("fwohci_buf_input: malformed input 3: %d\n",
   1604 		    pkt->fp_hlen - count);
   1605 		return 0;
   1606 	}
   1607 	return 1;
   1608 }
   1609 
   1610 static int
   1611 fwohci_buf_input_ppb(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   1612     struct fwohci_pkt *pkt)
   1613 {
   1614 	caddr_t p;
   1615 	int len;
   1616 	struct fwohci_buf *fb;
   1617 	struct fwohci_desc *fd;
   1618 
   1619 	if (fc->fc_type ==  FWOHCI_CTX_ISO_MULTI) {
   1620 		return fwohci_buf_input(sc, fc, pkt);
   1621 	}
   1622 
   1623 	memset(pkt, 0, sizeof(*pkt));
   1624 	pkt->fp_uio.uio_iov = pkt->fp_iov;
   1625 	pkt->fp_uio.uio_rw = UIO_WRITE;
   1626 	pkt->fp_uio.uio_segflg = UIO_SYSSPACE;
   1627 
   1628 	for (fb = TAILQ_FIRST(&fc->fc_buf); ; fb = TAILQ_NEXT(fb, fb_list)) {
   1629 		if (fb == NULL)
   1630 			return 0;
   1631 		if (fb->fb_off == 0)
   1632 			break;
   1633 	}
   1634 	fd = fb->fb_desc;
   1635 	len = fd->fd_reqcount - fd->fd_rescount;
   1636 	if (len == 0)
   1637 		return 0;
   1638 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, fb->fb_off, len,
   1639 	    BUS_DMASYNC_POSTREAD);
   1640 
   1641 	p = fb->fb_buf;
   1642 	fb->fb_off += roundup(len, 4);
   1643 	if (len < 8) {
   1644 		printf("fwohci_buf_input_ppb: malformed input 1: %d\n", len);
   1645 		return 0;
   1646 	}
   1647 
   1648 	/*
   1649 	 * get trailer first, may be bogus data unless status update
   1650 	 * in descriptor is set.
   1651 	 */
   1652 	pkt->fp_trail = (u_int32_t *)p;
   1653 	*pkt->fp_trail = (*pkt->fp_trail & 0xffff) | (fd->fd_status << 16);
   1654 	pkt->fp_hdr[0] = ((u_int32_t *)p)[1];
   1655 	pkt->fp_tcode = (pkt->fp_hdr[0] & 0x000000f0) >> 4;
   1656 #ifdef DIAGNOSTIC
   1657 	if (pkt->fp_tcode != IEEE1394_TCODE_STREAM_DATA) {
   1658 		printf("fwohci_buf_input_ppb: bad tcode: 0x%x\n",
   1659 		    pkt->fp_tcode);
   1660 		return 0;
   1661 	}
   1662 #endif
   1663 	pkt->fp_hlen = 4;
   1664 	pkt->fp_dlen = pkt->fp_hdr[0] >> 16;
   1665 	p += 8;
   1666 	len -= 8;
   1667 	if (pkt->fp_dlen != len) {
   1668 		printf("fwohci_buf_input_ppb: malformed input 2: %d != %d\n",
   1669 		    pkt->fp_dlen, len);
   1670 		return 0;
   1671 	}
   1672 	DPRINTFN(1, ("fwohci_buf_input_ppb: tcode=0x%x, hlen=%d, dlen=%d\n",
   1673 	    pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen));
   1674 	pkt->fp_iov[0].iov_base = p;
   1675 	pkt->fp_iov[0].iov_len = len;
   1676 	pkt->fp_uio.uio_iovcnt = 0;
   1677 	pkt->fp_uio.uio_resid = len;
   1678 	return 1;
   1679 }
   1680 
   1681 static int
   1682 fwohci_handler_set(struct fwohci_softc *sc,
   1683     int tcode, u_int32_t key1, u_int32_t key2,
   1684     int (*handler)(struct fwohci_softc *, void *, struct fwohci_pkt *),
   1685     void *arg)
   1686 {
   1687 	struct fwohci_ctx *fc;
   1688 	struct fwohci_handler *fh;
   1689 	int i, j;
   1690 
   1691 	if (tcode == IEEE1394_TCODE_STREAM_DATA &&
   1692 	    (((key1 & OHCI_ASYNC_STREAM) && sc->sc_ctx_as != NULL)
   1693 		    || (key1 & OHCI_ASYNC_STREAM) == 0)) {
   1694 		int isasync = key1 & OHCI_ASYNC_STREAM;
   1695 
   1696 		key1 = key1 & IEEE1394_ISO_CHANNEL_ANY ?
   1697 		    IEEE1394_ISO_CHANNEL_ANY : (key1 & IEEE1394_ISOCH_MASK);
   1698 		if (key1 & IEEE1394_ISO_CHANNEL_ANY) {
   1699 			printf("%s: key changed to %x\n",
   1700 			    sc->sc_sc1394.sc1394_dev.dv_xname, key1);
   1701 		}
   1702 		j = sc->sc_isoctx;
   1703 		fh = NULL;
   1704 
   1705 		for (i = 0; i < sc->sc_isoctx; i++) {
   1706 			if ((fc = sc->sc_ctx_as[i]) == NULL) {
   1707 				if (j == sc->sc_isoctx)
   1708 					j = i;
   1709 				continue;
   1710 			}
   1711 			fh = LIST_FIRST(&fc->fc_handler);
   1712 			if (fh->fh_tcode == tcode &&
   1713 			    fh->fh_key1 == key1 && fh->fh_key2 == key2)
   1714 				break;
   1715 			fh = NULL;
   1716 		}
   1717 		if (fh == NULL) {
   1718 			if (handler == NULL)
   1719 				return 0;
   1720 			if (j == sc->sc_isoctx) {
   1721 				DPRINTF(("fwohci_handler_set: no more free "
   1722 				    "context\n"));
   1723 				return ENOMEM;
   1724 			}
   1725 			if ((fc = sc->sc_ctx_as[j]) == NULL) {
   1726 				fwohci_ctx_alloc(sc, &fc, OHCI_BUF_IR_CNT, j,
   1727 				    isasync ? FWOHCI_CTX_ISO_SINGLE :
   1728 				    FWOHCI_CTX_ISO_MULTI);
   1729 				sc->sc_ctx_as[j] = fc;
   1730 			}
   1731 		}
   1732 #ifdef FW_DEBUG
   1733 		if (fh == NULL && handler != NULL) {
   1734 			printf("use ir context %d\n", j);
   1735 		} else if (fh != NULL && handler == NULL) {
   1736 			printf("remove ir context %d\n", i);
   1737 		}
   1738 #endif
   1739 	} else {
   1740 		switch (tcode) {
   1741 		case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1742 		case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1743 		case IEEE1394_TCODE_READ_REQ_QUAD:
   1744 		case IEEE1394_TCODE_READ_REQ_BLOCK:
   1745 		case IEEE1394_TCODE_LOCK_REQ:
   1746 			fc = sc->sc_ctx_arrq;
   1747 			break;
   1748 		case IEEE1394_TCODE_WRITE_RESP:
   1749 		case IEEE1394_TCODE_READ_RESP_QUAD:
   1750 		case IEEE1394_TCODE_READ_RESP_BLOCK:
   1751 		case IEEE1394_TCODE_LOCK_RESP:
   1752 			fc = sc->sc_ctx_arrs;
   1753 			break;
   1754 		default:
   1755 			return EIO;
   1756 		}
   1757 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1758 		    fh = LIST_NEXT(fh, fh_list)) {
   1759 			if (fh->fh_tcode == tcode &&
   1760 			    fh->fh_key1 == key1 && fh->fh_key2 == key2)
   1761 				break;
   1762 		}
   1763 	}
   1764 	if (handler == NULL) {
   1765 		if (fh != NULL) {
   1766 			LIST_REMOVE(fh, fh_list);
   1767 			free(fh, M_DEVBUF);
   1768 		}
   1769 		if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1770 			OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   1771 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1772 			sc->sc_ctx_as[fc->fc_ctx] = NULL;
   1773 			fwohci_ctx_free(sc, fc);
   1774 		}
   1775 		return 0;
   1776 	}
   1777 	if (fh == NULL) {
   1778 		fh = malloc(sizeof(*fh), M_DEVBUF, M_WAITOK);
   1779 		LIST_INSERT_HEAD(&fc->fc_handler, fh, fh_list);
   1780 	}
   1781 	fh->fh_tcode = tcode;
   1782 	fh->fh_key1 = key1;
   1783 	fh->fh_key2 = key2;
   1784 	fh->fh_handler = handler;
   1785 	fh->fh_handarg = arg;
   1786 	DPRINTFN(1, ("fwohci_handler_set: ctx %d, tcode %x, key 0x%x, 0x%x\n",
   1787 	    fc->fc_ctx, tcode, key1, key2));
   1788 
   1789 	if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1790 		fwohci_ctx_init(sc, fc);
   1791 		DPRINTFN(1, ("fwohci_handler_set: SYNC desc %ld\n",
   1792 		    (long)(TAILQ_FIRST(&fc->fc_buf)->fb_desc - sc->sc_desc)));
   1793 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   1794 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1795 	}
   1796 	return 0;
   1797 }
   1798 
   1799 
   1800 
   1801 
   1802 
   1803 /*
   1804  * static ieee1394_ir_tag_t
   1805  * fwohci_ir_ctx_set(struct device *dev, int channel, int tagbm,
   1806  *	int bufnum, int maxsize, int flags)
   1807  *
   1808  *	This function will return non-negative value if it succeeds.
   1809  *	This return value is pointer to the context of isochronous
   1810  *	transmission.  This function will return NULL value if it
   1811  *	fails.
   1812  */
   1813 ieee1394_ir_tag_t
   1814 fwohci_ir_ctx_set(struct device *dev, int channel, int tagbm,
   1815     int bufnum, int maxsize, int flags)
   1816 {
   1817 	int i, openctx;
   1818 	struct fwohci_ir_ctx *irc;
   1819 	struct fwohci_softc *sc = (struct fwohci_softc *)dev;
   1820 	const char *xname = sc->sc_sc1394.sc1394_dev.dv_xname;
   1821 
   1822 	printf("%s: ir_ctx_set channel %d tagbm 0x%x maxsize %d bufnum %d\n",
   1823 	    xname, channel, tagbm, maxsize, bufnum);
   1824 	/*
   1825 	 * This loop will find the smallest vacant context and check
   1826 	 * whether other channel uses the same channel.
   1827 	 */
   1828 	openctx = sc->sc_isoctx;
   1829 	for (i = 0; i < sc->sc_isoctx; ++i) {
   1830 		if (sc->sc_ctx_ir[i] == NULL) {
   1831 			/*
   1832 			 * Find a vacant contet.  If this has the
   1833 			 * smallest context number, register it.
   1834 			 */
   1835 			if (openctx == sc->sc_isoctx) {
   1836 				openctx = i;
   1837 			}
   1838 		} else {
   1839 			/*
   1840 			 * This context is used.  Check whether this
   1841 			 * context uses the same channel as ours.
   1842 			 */
   1843 			if (sc->sc_ctx_ir[i]->irc_channel == channel) {
   1844 				/* Using same channel. */
   1845 				printf("%s: channel %d occupied by ctx%d\n",
   1846 				    xname, channel, i);
   1847 				return NULL;
   1848 			}
   1849 		}
   1850 	}
   1851 
   1852 	/*
   1853 	 * If there is a vacant context, allocate isochronous transmit
   1854 	 * context for it.
   1855 	 */
   1856 	if (openctx != sc->sc_isoctx) {
   1857 		printf("%s using ctx %d for iso receive\n", xname, openctx);
   1858 		if ((irc = fwohci_ir_ctx_construct(sc, openctx, channel,
   1859 		    tagbm, bufnum, maxsize, flags)) == NULL) {
   1860 			return NULL;
   1861 		}
   1862 #ifndef IR_CTX_OPENTEST
   1863 		sc->sc_ctx_ir[openctx] = irc;
   1864 #else
   1865 		fwohci_ir_ctx_destruct(irc);
   1866 		irc = NULL;
   1867 #endif
   1868 	} else {
   1869 		printf("%s: cannot find any vacant contexts\n", xname);
   1870 		irc = NULL;
   1871 	}
   1872 
   1873 	return (ieee1394_ir_tag_t)irc;
   1874 }
   1875 
   1876 
   1877 /*
   1878  * int fwohci_ir_ctx_clear(struct device *dev, ieee1394_ir_tag_t *ir)
   1879  *
   1880  *	This function will return 0 if it succeed.  Otherwise return
   1881  *	negative value.
   1882  */
   1883 int
   1884 fwohci_ir_ctx_clear(struct device *dev, ieee1394_ir_tag_t ir)
   1885 {
   1886 	struct fwohci_ir_ctx *irc = (struct fwohci_ir_ctx *)ir;
   1887 	struct fwohci_softc *sc = irc->irc_sc;
   1888 	int i;
   1889 
   1890 	if (sc->sc_ctx_ir[irc->irc_num] != irc) {
   1891 		printf("fwohci_ir_ctx_clear: irc differs %p %p\n",
   1892 		    sc->sc_ctx_ir[irc->irc_num], irc);
   1893 		return -1;
   1894 	}
   1895 
   1896 	i = 0;
   1897 	while (irc->irc_status & IRC_STATUS_RUN) {
   1898 		tsleep((void *)irc, PWAIT|PCATCH, "IEEE1394 iso receive", 100);
   1899 		if (irc->irc_status & IRC_STATUS_RUN) {
   1900 			if (fwohci_ir_stop(irc) == 0) {
   1901 				irc->irc_status &= ~IRC_STATUS_RUN;
   1902 			}
   1903 
   1904 		}
   1905 		if (++i > 20) {
   1906 			u_int32_t reg
   1907 			    = OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   1908 				OHCI_SUBREG_ContextControlSet);
   1909 
   1910 			printf("fwochi_ir_ctx_clear: "
   1911 			    "Cannot stop iso receive engine\n");
   1912 			printf("%s:  intr IR_CommandPtr 0x%08x "
   1913 			    "ContextCtrl 0x%08x%s%s%s%s\n",
   1914 			    sc->sc_sc1394.sc1394_dev.dv_xname,
   1915 			    OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   1916 				OHCI_SUBREG_CommandPtr),
   1917 			    reg,
   1918 			    reg & OHCI_CTXCTL_RUN ? " run" : "",
   1919 			    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   1920 			    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   1921 			    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   1922 
   1923 			return EBUSY;
   1924 		}
   1925 	}
   1926 
   1927 	printf("fwohci_ir_ctx_clear: DMA engine is stopped. get %d frames max queuelen %d pos %d\n",
   1928 	    irc->irc_pktcount, irc->irc_maxqueuelen, irc->irc_maxqueuepos);
   1929 
   1930 	fwohci_ir_ctx_destruct(irc);
   1931 
   1932 	sc->sc_ctx_ir[irc->irc_num] = NULL;
   1933 
   1934 	return 0;
   1935 }
   1936 
   1937 
   1938 
   1939 
   1940 
   1941 
   1942 
   1943 
   1944 ieee1394_it_tag_t
   1945 fwohci_it_set(struct ieee1394_softc *isc, int channel, int tagbm)
   1946 {
   1947 	ieee1394_it_tag_t rv;
   1948 	int tag;
   1949 
   1950 	for (tag = 0; tagbm != 0 && (tagbm & 0x01) == 0; tagbm >>= 1, ++tag);
   1951 
   1952 	rv = fwohci_it_ctx_set((struct fwohci_softc *)isc, channel, tag, 488);
   1953 
   1954 	return rv;
   1955 }
   1956 
   1957 /*
   1958  * static ieee1394_it_tag_t
   1959  * fwohci_it_ctx_set(struct fwohci_softc *sc,
   1960  *    u_int32_t key1 (channel), u_int32_t key2 (tag), int maxsize)
   1961  *
   1962  *	This function will return non-negative value if it succeeds.
   1963  *	This return value is pointer to the context of isochronous
   1964  *	transmission.  This function will return NULL value if it
   1965  *	fails.
   1966  */
   1967 static ieee1394_it_tag_t
   1968 fwohci_it_ctx_set(struct fwohci_softc *sc, int channel, int tag, int maxsize)
   1969 {
   1970 	int i, openctx;
   1971 	struct fwohci_it_ctx *itc;
   1972 	const char *xname = sc->sc_sc1394.sc1394_dev.dv_xname;
   1973 #ifdef TEST_CHAIN
   1974 	extern int fwohci_test_chain(struct fwohci_it_ctx *);
   1975 #endif /* TEST_CHAIN */
   1976 #ifdef TEST_WRITE
   1977 	extern void fwohci_test_write(struct fwohci_it_ctx *itc);
   1978 #endif /* TEST_WRITE */
   1979 
   1980 	printf("%s: it_ctx_set channel %d tag %d maxsize %d\n",
   1981 	    xname, channel, tag, maxsize);
   1982 
   1983 	/*
   1984 	 * This loop will find the smallest vacant context and check
   1985 	 * whether other channel uses the same channel.
   1986 	 */
   1987 	openctx = sc->sc_itctx;
   1988 	for (i = 0; i < sc->sc_itctx; ++i) {
   1989 		if (sc->sc_ctx_it[i] == NULL) {
   1990 			/*
   1991 			 * Find a vacant contet.  If this has the
   1992 			 * smallest context number, register it.
   1993 			 */
   1994 			if (openctx == sc->sc_itctx) {
   1995 				openctx = i;
   1996 			}
   1997 		} else {
   1998 			/*
   1999 			 * This context is used.  Check whether this
   2000 			 * context uses the same channel as ours.
   2001 			 */
   2002 			if (sc->sc_ctx_it[i]->itc_channel == channel) {
   2003 				/* Using same channel. */
   2004 				printf("%s: channel %d occupied by ctx%d\n",
   2005 				    xname, channel, i);
   2006 				return NULL;
   2007 			}
   2008 		}
   2009 	}
   2010 
   2011 	/*
   2012 	 * If there is a vacant context, allocate isochronous transmit
   2013 	 * context for it.
   2014 	 */
   2015 	if (openctx != sc->sc_itctx) {
   2016 		printf("%s using ctx %d for iso trasmit\n", xname, openctx);
   2017 		if ((itc = fwohci_it_ctx_construct(sc, openctx, channel,
   2018 		    tag, maxsize)) == NULL) {
   2019 			return NULL;
   2020 		}
   2021 		sc->sc_ctx_it[openctx] = itc;
   2022 
   2023 #ifdef TEST_CHAIN
   2024 		fwohci_test_chain(itc);
   2025 #endif /* TEST_CHAIN */
   2026 #ifdef TEST_WRITE
   2027 		fwohci_test_write(itc);
   2028 		itc = NULL;
   2029 #endif /* TEST_WRITE */
   2030 
   2031 	} else {
   2032 		printf("%s: cannot find any vacant contexts\n", xname);
   2033 		itc = NULL;
   2034 	}
   2035 
   2036 	return (ieee1394_it_tag_t)itc;
   2037 }
   2038 
   2039 
   2040 /*
   2041  * int fwohci_it_ctx_clear(ieee1394_it_tag_t *it)
   2042  *
   2043  *	This function will return 0 if it succeed.  Otherwise return
   2044  *	negative value.
   2045  */
   2046 int
   2047 fwohci_it_ctx_clear(ieee1394_it_tag_t *it)
   2048 {
   2049 	struct fwohci_it_ctx *itc = (struct fwohci_it_ctx *)it;
   2050 	struct fwohci_softc *sc = itc->itc_sc;
   2051 	int i;
   2052 
   2053 	if (sc->sc_ctx_it[itc->itc_num] != itc) {
   2054 		printf("fwohci_it_ctx_clear: itc differs %p %p\n",
   2055 		    sc->sc_ctx_it[itc->itc_num], itc);
   2056 		return -1;
   2057 	}
   2058 
   2059 	fwohci_it_ctx_flush(it);
   2060 
   2061 	i = 0;
   2062 	while (itc->itc_flags & ITC_FLAGS_RUN) {
   2063 		tsleep((void *)itc, PWAIT|PCATCH, "IEEE1394 iso transmit", 100);
   2064 		if (itc->itc_flags & ITC_FLAGS_RUN) {
   2065 			u_int32_t reg;
   2066 
   2067 			reg = OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num,
   2068 			    OHCI_SUBREG_ContextControlSet);
   2069 
   2070 			if ((reg & OHCI_CTXCTL_WAKE) == 0) {
   2071 				itc->itc_flags &= ~ITC_FLAGS_RUN;
   2072 				printf("fwochi_it_ctx_clear: "
   2073 				    "DMA engine stopped without intr\n");
   2074 			}
   2075 			printf("%s: %d intr IT_CommandPtr 0x%08x "
   2076 			    "ContextCtrl 0x%08x%s%s%s%s\n",
   2077 			    sc->sc_sc1394.sc1394_dev.dv_xname, i,
   2078 			    OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num,
   2079 				OHCI_SUBREG_CommandPtr),
   2080 			    reg,
   2081 			    reg & OHCI_CTXCTL_RUN ? " run" : "",
   2082 			    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   2083 			    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   2084 			    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   2085 
   2086 
   2087 		}
   2088 		if (++i > 20) {
   2089 			u_int32_t reg
   2090 			    = OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num,
   2091 				OHCI_SUBREG_ContextControlSet);
   2092 
   2093 			printf("fwochi_it_ctx_clear: "
   2094 			    "Cannot stop iso transmit engine\n");
   2095 			printf("%s:  intr IT_CommandPtr 0x%08x "
   2096 			    "ContextCtrl 0x%08x%s%s%s%s\n",
   2097 			    sc->sc_sc1394.sc1394_dev.dv_xname,
   2098 			    OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num,
   2099 				OHCI_SUBREG_CommandPtr),
   2100 			    reg,
   2101 			    reg & OHCI_CTXCTL_RUN ? " run" : "",
   2102 			    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   2103 			    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   2104 			    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   2105 
   2106 			return EBUSY;
   2107 		}
   2108 	}
   2109 
   2110 	printf("fwohci_it_ctx_clear: DMA engine is stopped.\n");
   2111 
   2112 	fwohci_it_ctx_destruct(itc);
   2113 
   2114 	sc->sc_ctx_it[itc->itc_num] = NULL;
   2115 
   2116 
   2117 	return 0;
   2118 }
   2119 
   2120 
   2121 
   2122 
   2123 
   2124 
   2125 /*
   2126  * Asyncronous Receive Requests input frontend.
   2127  */
   2128 static void
   2129 fwohci_arrq_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   2130 {
   2131 	int rcode;
   2132 	u_int32_t key1, key2;
   2133 	struct fwohci_handler *fh;
   2134 	struct fwohci_pkt pkt, res;
   2135 
   2136 	/*
   2137 	 * Do not return if next packet is in the buffer, or the next
   2138 	 * packet cannot be received until the next receive interrupt.
   2139 	 */
   2140 	while (fwohci_buf_input(sc, fc, &pkt)) {
   2141 		if (pkt.fp_tcode == OHCI_TCODE_PHY) {
   2142 			fwohci_phy_input(sc, &pkt);
   2143 			continue;
   2144 		}
   2145 		key1 = pkt.fp_hdr[1] & 0xffff;
   2146 		key2 = pkt.fp_hdr[2];
   2147 		memset(&res, 0, sizeof(res));
   2148 		res.fp_uio.uio_rw = UIO_WRITE;
   2149 		res.fp_uio.uio_segflg = UIO_SYSSPACE;
   2150 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   2151 		    fh = LIST_NEXT(fh, fh_list)) {
   2152 			if (pkt.fp_tcode == fh->fh_tcode &&
   2153 			    key1 == fh->fh_key1 &&
   2154 			    key2 == fh->fh_key2) {
   2155 				rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
   2156 				    &pkt);
   2157 				break;
   2158 			}
   2159 		}
   2160 		if (fh == NULL) {
   2161 			rcode = IEEE1394_RCODE_ADDRESS_ERROR;
   2162 			DPRINTFN(1, ("fwohci_arrq_input: no listener: tcode "
   2163 			    "0x%x, addr=0x%04x %08x\n", pkt.fp_tcode, key1,
   2164 			    key2));
   2165 		}
   2166 		if (((*pkt.fp_trail & 0x001f0000) >> 16) !=
   2167 		    OHCI_CTXCTL_EVENT_ACK_PENDING)
   2168 			continue;
   2169 		if (rcode != -1)
   2170 			fwohci_atrs_output(sc, rcode, &pkt, &res);
   2171 	}
   2172 	fwohci_buf_next(sc, fc);
   2173 	OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2174 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   2175 }
   2176 
   2177 
   2178 /*
   2179  * Asynchronous Receive Response input frontend.
   2180  */
   2181 static void
   2182 fwohci_arrs_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   2183 {
   2184 	struct fwohci_pkt pkt;
   2185 	struct fwohci_handler *fh;
   2186 	u_int16_t srcid;
   2187 	int rcode, tlabel;
   2188 
   2189 	while (fwohci_buf_input(sc, fc, &pkt)) {
   2190 		srcid = pkt.fp_hdr[1] >> 16;
   2191 		rcode = (pkt.fp_hdr[1] & 0x0000f000) >> 12;
   2192 		tlabel = (pkt.fp_hdr[0] & 0x0000fc00) >> 10;
   2193 		DPRINTFN(1, ("fwohci_arrs_input: tcode 0x%x, from 0x%04x,"
   2194 		    " tlabel 0x%x, rcode 0x%x, hlen %d, dlen %d\n",
   2195 		    pkt.fp_tcode, srcid, tlabel, rcode, pkt.fp_hlen,
   2196 		    pkt.fp_dlen));
   2197 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   2198 		    fh = LIST_NEXT(fh, fh_list)) {
   2199 			if (pkt.fp_tcode == fh->fh_tcode &&
   2200 			    (srcid & OHCI_NodeId_NodeNumber) == fh->fh_key1 &&
   2201 			    tlabel == fh->fh_key2) {
   2202 				(*fh->fh_handler)(sc, fh->fh_handarg, &pkt);
   2203 				LIST_REMOVE(fh, fh_list);
   2204 				free(fh, M_DEVBUF);
   2205 				break;
   2206 			}
   2207 		}
   2208 		if (fh == NULL)
   2209 			DPRINTFN(1, ("fwohci_arrs_input: no listner\n"));
   2210 	}
   2211 	fwohci_buf_next(sc, fc);
   2212 	OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2213 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   2214 }
   2215 
   2216 /*
   2217  * Isochronous Receive input frontend.
   2218  */
   2219 static void
   2220 fwohci_as_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   2221 {
   2222 	int rcode, chan, tag;
   2223 	struct iovec *iov;
   2224 	struct fwohci_handler *fh;
   2225 	struct fwohci_pkt pkt;
   2226 
   2227 #if DOUBLEBUF
   2228 	if (fc->fc_type == FWOHCI_CTX_ISO_MULTI) {
   2229 		struct fwohci_buf *fb;
   2230 		int i;
   2231 		u_int32_t reg;
   2232 
   2233 		/* stop dma engine before read buffer */
   2234 		reg = OHCI_SYNC_RX_DMA_READ(sc, fc->fc_ctx,
   2235 		    OHCI_SUBREG_ContextControlClear);
   2236 		DPRINTFN(5, ("ir_input %08x =>", reg));
   2237 		if (reg & OHCI_CTXCTL_RUN) {
   2238 			OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   2239 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   2240 		}
   2241 		DPRINTFN(5, (" %08x\n", OHCI_SYNC_RX_DMA_READ(sc, fc->fc_ctx, OHCI_SUBREG_ContextControlClear)));
   2242 
   2243 		i = 0;
   2244 		while ((reg = OHCI_SYNC_RX_DMA_READ(sc, fc->fc_ctx, OHCI_SUBREG_ContextControlSet)) & OHCI_CTXCTL_ACTIVE) {
   2245 			delay(10);
   2246 			if (++i > 10000) {
   2247 				printf("cannot stop dma engine 0x%08x\n", reg);
   2248 				return;
   2249 			}
   2250 		}
   2251 
   2252 		/* rotate dma buffer */
   2253 		fb = TAILQ_FIRST(&fc->fc_buf2);
   2254 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_CommandPtr,
   2255 		    fb->fb_daddr | 1);
   2256 		/* start dma engine */
   2257 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   2258 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   2259 		OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntEventClear,
   2260 		    (1 << fc->fc_ctx));
   2261 	}
   2262 #endif
   2263 
   2264 	while (fwohci_buf_input_ppb(sc, fc, &pkt)) {
   2265 		chan = (pkt.fp_hdr[0] & 0x00003f00) >> 8;
   2266 		tag  = (pkt.fp_hdr[0] & 0x0000c000) >> 14;
   2267 		DPRINTFN(1, ("fwohci_as_input: hdr 0x%08x, tcode 0x%0x, hlen %d"
   2268 		    ", dlen %d\n", pkt.fp_hdr[0], pkt.fp_tcode, pkt.fp_hlen,
   2269 		    pkt.fp_dlen));
   2270 		if (tag == IEEE1394_TAG_GASP &&
   2271 		    fc->fc_type == FWOHCI_CTX_ISO_SINGLE) {
   2272 			/*
   2273 			 * The pkt with tag=3 is GASP format.
   2274 			 * Move GASP header to header part.
   2275 			 */
   2276 			if (pkt.fp_dlen < 8)
   2277 				continue;
   2278 			iov = pkt.fp_iov;
   2279 			/* assuming pkt per buffer mode */
   2280 			pkt.fp_hdr[1] = ntohl(((u_int32_t *)iov->iov_base)[0]);
   2281 			pkt.fp_hdr[2] = ntohl(((u_int32_t *)iov->iov_base)[1]);
   2282 			iov->iov_base = (caddr_t)iov->iov_base + 8;
   2283 			iov->iov_len -= 8;
   2284 			pkt.fp_hlen += 8;
   2285 			pkt.fp_dlen -= 8;
   2286 		}
   2287 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   2288 		    fh = LIST_NEXT(fh, fh_list)) {
   2289 			if (pkt.fp_tcode == fh->fh_tcode &&
   2290 			    (chan == fh->fh_key1 ||
   2291 				fh->fh_key1 == IEEE1394_ISO_CHANNEL_ANY) &&
   2292 			    ((1 << tag) & fh->fh_key2) != 0) {
   2293 				rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
   2294 				    &pkt);
   2295 				break;
   2296 			}
   2297 		}
   2298 #ifdef FW_DEBUG
   2299 		if (fh == NULL) {
   2300 			DPRINTFN(1, ("fwohci_as_input: no handler\n"));
   2301 		} else {
   2302 			DPRINTFN(1, ("fwohci_as_input: rcode %d\n", rcode));
   2303 		}
   2304 #endif
   2305 	}
   2306 	fwohci_buf_next(sc, fc);
   2307 
   2308 	if (fc->fc_type == FWOHCI_CTX_ISO_SINGLE) {
   2309 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   2310 		    OHCI_SUBREG_ContextControlSet,
   2311 		    OHCI_CTXCTL_WAKE);
   2312 	}
   2313 }
   2314 
   2315 /*
   2316  * Asynchronous Transmit common routine.
   2317  */
   2318 static int
   2319 fwohci_at_output(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   2320     struct fwohci_pkt *pkt)
   2321 {
   2322 	struct fwohci_buf *fb;
   2323 	struct fwohci_desc *fd;
   2324 	struct mbuf *m, *m0;
   2325 	int i, ndesc, error, off, len;
   2326 	u_int32_t val;
   2327 #ifdef FW_DEBUG
   2328 	struct iovec *iov;
   2329         int tlabel = (pkt->fp_hdr[0] & 0x0000fc00) >> 10;
   2330 #endif
   2331 
   2332 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) == IEEE1394_BCAST_PHY_ID)
   2333 		/* We can't send anything during selfid duration */
   2334 		return EAGAIN;
   2335 
   2336 #ifdef FW_DEBUG
   2337 	DPRINTFN(1, ("fwohci_at_output: tcode 0x%x, tlabel 0x%x hlen %d, "
   2338 	    "dlen %d", pkt->fp_tcode, tlabel, pkt->fp_hlen, pkt->fp_dlen));
   2339 	for (i = 0; i < pkt->fp_hlen/4; i++)
   2340 		DPRINTFN(2, ("%s%08x", i?" ":"\n    ", pkt->fp_hdr[i]));
   2341 	DPRINTFN(2, ("$"));
   2342 	for (ndesc = 0, iov = pkt->fp_iov;
   2343 	     ndesc < pkt->fp_uio.uio_iovcnt; ndesc++, iov++) {
   2344 		for (i = 0; i < iov->iov_len; i++)
   2345 			DPRINTFN(2, ("%s%02x", (i%32)?((i%4)?"":" "):"\n    ",
   2346 			    ((u_int8_t *)iov->iov_base)[i]));
   2347 		DPRINTFN(2, ("$"));
   2348 	}
   2349 	DPRINTFN(1, ("\n"));
   2350 #endif
   2351 
   2352 	if ((m = pkt->fp_m) != NULL) {
   2353 		for (ndesc = 2; m != NULL; m = m->m_next)
   2354 			ndesc++;
   2355 		if (ndesc > OHCI_DESC_MAX) {
   2356 			m0 = NULL;
   2357 			ndesc = 2;
   2358 			for (off = 0; off < pkt->fp_dlen; off += len) {
   2359 				if (m0 == NULL) {
   2360 					MGETHDR(m0, M_DONTWAIT, MT_DATA);
   2361 					if (m0 != NULL)
   2362 						M_COPY_PKTHDR(m0, pkt->fp_m);
   2363 					m = m0;
   2364 				} else {
   2365 					MGET(m->m_next, M_DONTWAIT, MT_DATA);
   2366 					m = m->m_next;
   2367 				}
   2368 				if (m != NULL)
   2369 					MCLGET(m, M_DONTWAIT);
   2370 				if (m == NULL || (m->m_flags & M_EXT) == 0) {
   2371 					m_freem(m0);
   2372 					return ENOMEM;
   2373 				}
   2374 				len = pkt->fp_dlen - off;
   2375 				if (len > m->m_ext.ext_size)
   2376 					len = m->m_ext.ext_size;
   2377 				m_copydata(pkt->fp_m, off, len,
   2378 				    mtod(m, caddr_t));
   2379 				m->m_len = len;
   2380 				ndesc++;
   2381 			}
   2382 			m_freem(pkt->fp_m);
   2383 			pkt->fp_m = m0;
   2384 		}
   2385 	} else
   2386 		ndesc = 2 + pkt->fp_uio.uio_iovcnt;
   2387 
   2388 	if (ndesc > OHCI_DESC_MAX)
   2389 		return ENOBUFS;
   2390 
   2391 	if (fc->fc_bufcnt > 50)			/*XXX*/
   2392 		return ENOBUFS;
   2393 	fb = malloc(sizeof(*fb), M_DEVBUF, M_WAITOK);
   2394 	fb->fb_nseg = ndesc;
   2395 	fb->fb_desc = fwohci_desc_get(sc, ndesc);
   2396 	if (fb->fb_desc == NULL) {
   2397 		free(fb, M_DEVBUF);
   2398 		return ENOBUFS;
   2399 	}
   2400 	fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
   2401 	    ((caddr_t)fb->fb_desc - (caddr_t)sc->sc_desc);
   2402 	fb->fb_m = pkt->fp_m;
   2403 	fb->fb_callback = pkt->fp_callback;
   2404 	fb->fb_statuscb = pkt->fp_statuscb;
   2405 	fb->fb_statusarg = pkt->fp_statusarg;
   2406 
   2407 	if (ndesc > 2) {
   2408 		if ((error = bus_dmamap_create(sc->sc_dmat, pkt->fp_dlen, ndesc,
   2409 		    PAGE_SIZE, 0, BUS_DMA_WAITOK, &fb->fb_dmamap)) != 0) {
   2410 			fwohci_desc_put(sc, fb->fb_desc, ndesc);
   2411 			free(fb, M_DEVBUF);
   2412 			return error;
   2413 		}
   2414 
   2415 		if (pkt->fp_m != NULL)
   2416 			error = bus_dmamap_load_mbuf(sc->sc_dmat, fb->fb_dmamap,
   2417 			    pkt->fp_m, BUS_DMA_WAITOK);
   2418 		else
   2419 			error = bus_dmamap_load_uio(sc->sc_dmat, fb->fb_dmamap,
   2420 			    &pkt->fp_uio, BUS_DMA_WAITOK);
   2421 		if (error != 0) {
   2422 			bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   2423 			fwohci_desc_put(sc, fb->fb_desc, ndesc);
   2424 			free(fb, M_DEVBUF);
   2425 			return error;
   2426 		}
   2427 		bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0, pkt->fp_dlen,
   2428 		    BUS_DMASYNC_PREWRITE);
   2429 	}
   2430 
   2431 	fd = fb->fb_desc;
   2432 	fd->fd_flags = OHCI_DESC_IMMED;
   2433 	fd->fd_reqcount = pkt->fp_hlen;
   2434 	fd->fd_data = 0;
   2435 	fd->fd_branch = 0;
   2436 	fd->fd_status = 0;
   2437 	if (fc->fc_ctx == OHCI_CTX_ASYNC_TX_RESPONSE) {
   2438 		i = 3;				/* XXX: 3 sec */
   2439 		val = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   2440 		fd->fd_timestamp = ((val >> 12) & 0x1fff) |
   2441 		    ((((val >> 25) + i) & 0x7) << 13);
   2442 	} else
   2443 		fd->fd_timestamp = 0;
   2444 	memcpy(fd + 1, pkt->fp_hdr, pkt->fp_hlen);
   2445 	for (i = 0; i < ndesc - 2; i++) {
   2446 		fd = fb->fb_desc + 2 + i;
   2447 		fd->fd_flags = 0;
   2448 		fd->fd_reqcount = fb->fb_dmamap->dm_segs[i].ds_len;
   2449 		fd->fd_data = fb->fb_dmamap->dm_segs[i].ds_addr;
   2450 		fd->fd_branch = 0;
   2451 		fd->fd_status = 0;
   2452 		fd->fd_timestamp = 0;
   2453 	}
   2454 	fd->fd_flags |= OHCI_DESC_LAST | OHCI_DESC_BRANCH;
   2455 	fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   2456 
   2457 #ifdef FW_DEBUG
   2458 	DPRINTFN(1, ("fwohci_at_output: desc %ld",
   2459 	    (long)(fb->fb_desc - sc->sc_desc)));
   2460 	for (i = 0; i < ndesc * 4; i++)
   2461 		DPRINTFN(2, ("%s%08x", i&7?" ":"\n    ",
   2462 		    ((u_int32_t *)fb->fb_desc)[i]));
   2463 	DPRINTFN(1, ("\n"));
   2464 #endif
   2465 
   2466 	val = OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
   2467 	    OHCI_SUBREG_ContextControlClear);
   2468 
   2469 	if (val & OHCI_CTXCTL_RUN) {
   2470 		if (fc->fc_branch == NULL) {
   2471 			OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2472 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   2473 			goto run;
   2474 		}
   2475 		*fc->fc_branch = fb->fb_daddr | ndesc;
   2476 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2477 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   2478 	} else {
   2479   run:
   2480 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2481 		    OHCI_SUBREG_CommandPtr, fb->fb_daddr | ndesc);
   2482 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2483 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   2484 	}
   2485 	fc->fc_branch = &fd->fd_branch;
   2486 
   2487 	fc->fc_bufcnt++;
   2488 	TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   2489 	pkt->fp_m = NULL;
   2490 	return 0;
   2491 }
   2492 
   2493 static void
   2494 fwohci_at_done(struct fwohci_softc *sc, struct fwohci_ctx *fc, int force)
   2495 {
   2496 	struct fwohci_buf *fb;
   2497 	struct fwohci_desc *fd;
   2498 	struct fwohci_pkt pkt;
   2499 	int i;
   2500 
   2501 	while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   2502 		fd = fb->fb_desc;
   2503 #ifdef FW_DEBUG
   2504 		DPRINTFN(1, ("fwohci_at_done: %sdesc %ld (%d)",
   2505 		    force ? "force " : "", (long)(fd - sc->sc_desc),
   2506 		    fb->fb_nseg));
   2507 		for (i = 0; i < fb->fb_nseg * 4; i++)
   2508 			DPRINTFN(2, ("%s%08x", i&7?" ":"\n    ",
   2509 			    ((u_int32_t *)fd)[i]));
   2510 		DPRINTFN(1, ("\n"));
   2511 #endif
   2512 		if (fb->fb_nseg > 2)
   2513 			fd += fb->fb_nseg - 1;
   2514 		if (!force && !(fd->fd_status & OHCI_CTXCTL_ACTIVE))
   2515 			break;
   2516 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   2517 		if (fc->fc_branch == &fd->fd_branch) {
   2518 			OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   2519 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   2520 			fc->fc_branch = NULL;
   2521 			for (i = 0; i < OHCI_LOOP; i++) {
   2522 				if (!(OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
   2523 				    OHCI_SUBREG_ContextControlClear) &
   2524 				    OHCI_CTXCTL_ACTIVE))
   2525 					break;
   2526 				DELAY(10);
   2527 			}
   2528 		}
   2529 
   2530 		if (fb->fb_statuscb) {
   2531 			memset(&pkt, 0, sizeof(pkt));
   2532 			pkt.fp_status = fd->fd_status;
   2533 			memcpy(pkt.fp_hdr, fd + 1, sizeof(pkt.fp_hdr[0]));
   2534 
   2535 			/* Indicate this is just returning the status bits. */
   2536 			pkt.fp_tcode = -1;
   2537 			(*fb->fb_statuscb)(sc, fb->fb_statusarg, &pkt);
   2538 			fb->fb_statuscb = NULL;
   2539 			fb->fb_statusarg = NULL;
   2540 		}
   2541 		fwohci_desc_put(sc, fb->fb_desc, fb->fb_nseg);
   2542 		if (fb->fb_nseg > 2)
   2543 			bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   2544 		fc->fc_bufcnt--;
   2545 		if (fb->fb_callback) {
   2546 			(*fb->fb_callback)(sc->sc_sc1394.sc1394_if, fb->fb_m);
   2547 			fb->fb_callback = NULL;
   2548 		} else if (fb->fb_m != NULL)
   2549 			m_freem(fb->fb_m);
   2550 		free(fb, M_DEVBUF);
   2551 	}
   2552 }
   2553 
   2554 /*
   2555  * Asynchronous Transmit Reponse -- in response of request packet.
   2556  */
   2557 static void
   2558 fwohci_atrs_output(struct fwohci_softc *sc, int rcode, struct fwohci_pkt *req,
   2559     struct fwohci_pkt *res)
   2560 {
   2561 
   2562 	if (((*req->fp_trail & 0x001f0000) >> 16) !=
   2563 	    OHCI_CTXCTL_EVENT_ACK_PENDING)
   2564 		return;
   2565 
   2566 	res->fp_hdr[0] = (req->fp_hdr[0] & 0x0000fc00) | 0x00000100;
   2567 	res->fp_hdr[1] = (req->fp_hdr[1] & 0xffff0000) | (rcode << 12);
   2568 	switch (req->fp_tcode) {
   2569 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   2570 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   2571 		res->fp_tcode = IEEE1394_TCODE_WRITE_RESP;
   2572 		res->fp_hlen = 12;
   2573 		break;
   2574 	case IEEE1394_TCODE_READ_REQ_QUAD:
   2575 		res->fp_tcode = IEEE1394_TCODE_READ_RESP_QUAD;
   2576 		res->fp_hlen = 16;
   2577 		res->fp_dlen = 0;
   2578 		if (res->fp_uio.uio_iovcnt == 1 && res->fp_iov[0].iov_len == 4)
   2579 			res->fp_hdr[3] =
   2580 			    *(u_int32_t *)res->fp_iov[0].iov_base;
   2581 		res->fp_uio.uio_iovcnt = 0;
   2582 		break;
   2583 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   2584 	case IEEE1394_TCODE_LOCK_REQ:
   2585 		if (req->fp_tcode == IEEE1394_TCODE_LOCK_REQ)
   2586 			res->fp_tcode = IEEE1394_TCODE_LOCK_RESP;
   2587 		else
   2588 			res->fp_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
   2589 		res->fp_hlen = 16;
   2590 		res->fp_dlen = res->fp_uio.uio_resid;
   2591 		res->fp_hdr[3] = res->fp_dlen << 16;
   2592 		break;
   2593 	}
   2594 	res->fp_hdr[0] |= (res->fp_tcode << 4);
   2595 	fwohci_at_output(sc, sc->sc_ctx_atrs, res);
   2596 }
   2597 
   2598 /*
   2599  * APPLICATION LAYER SERVICES
   2600  */
   2601 
   2602 /*
   2603  * Retrieve Global UID from GUID ROM
   2604  */
   2605 static int
   2606 fwohci_guidrom_init(struct fwohci_softc *sc)
   2607 {
   2608 	int i, n, off;
   2609 	u_int32_t val1, val2;
   2610 
   2611 	/* Extract the Global UID
   2612 	 */
   2613 	val1 = OHCI_CSR_READ(sc, OHCI_REG_GUIDHi);
   2614 	val2 = OHCI_CSR_READ(sc, OHCI_REG_GUIDLo);
   2615 
   2616 	if (val1 != 0 || val2 != 0) {
   2617 		sc->sc_sc1394.sc1394_guid[0] = (val1 >> 24) & 0xff;
   2618 		sc->sc_sc1394.sc1394_guid[1] = (val1 >> 16) & 0xff;
   2619 		sc->sc_sc1394.sc1394_guid[2] = (val1 >>  8) & 0xff;
   2620 		sc->sc_sc1394.sc1394_guid[3] = (val1 >>  0) & 0xff;
   2621 		sc->sc_sc1394.sc1394_guid[4] = (val2 >> 24) & 0xff;
   2622 		sc->sc_sc1394.sc1394_guid[5] = (val2 >> 16) & 0xff;
   2623 		sc->sc_sc1394.sc1394_guid[6] = (val2 >>  8) & 0xff;
   2624 		sc->sc_sc1394.sc1394_guid[7] = (val2 >>  0) & 0xff;
   2625 	} else {
   2626 		val1 = OHCI_CSR_READ(sc, OHCI_REG_Version);
   2627 		if ((val1 & OHCI_Version_GUID_ROM) == 0)
   2628 			return -1;
   2629 		OHCI_CSR_WRITE(sc, OHCI_REG_Guid_Rom, OHCI_Guid_AddrReset);
   2630 		for (i = 0; i < OHCI_LOOP; i++) {
   2631 			val1 = OHCI_CSR_READ(sc, OHCI_REG_Guid_Rom);
   2632 			if (!(val1 & OHCI_Guid_AddrReset))
   2633 				break;
   2634 			DELAY(10);
   2635 		}
   2636 		off = OHCI_BITVAL(val1, OHCI_Guid_MiniROM) + 4;
   2637 		val2 = 0;
   2638 		for (n = 0; n < off + sizeof(sc->sc_sc1394.sc1394_guid); n++) {
   2639 			OHCI_CSR_WRITE(sc, OHCI_REG_Guid_Rom,
   2640 			    OHCI_Guid_RdStart);
   2641 			for (i = 0; i < OHCI_LOOP; i++) {
   2642 				val1 = OHCI_CSR_READ(sc, OHCI_REG_Guid_Rom);
   2643 				if (!(val1 & OHCI_Guid_RdStart))
   2644 					break;
   2645 				DELAY(10);
   2646 			}
   2647 			if (n < off)
   2648 				continue;
   2649 			val1 = OHCI_BITVAL(val1, OHCI_Guid_RdData);
   2650 			sc->sc_sc1394.sc1394_guid[n - off] = val1;
   2651 			val2 |= val1;
   2652 		}
   2653 		if (val2 == 0)
   2654 			return -1;
   2655 	}
   2656 	return 0;
   2657 }
   2658 
   2659 /*
   2660  * Initialization for Configuration ROM (no DMA context)
   2661  */
   2662 
   2663 #define	CFR_MAXUNIT		20
   2664 
   2665 struct configromctx {
   2666 	u_int32_t	*ptr;
   2667 	int		curunit;
   2668 	struct {
   2669 		u_int32_t	*start;
   2670 		int		length;
   2671 		u_int32_t	*refer;
   2672 		int		refunit;
   2673 	} unit[CFR_MAXUNIT];
   2674 };
   2675 
   2676 #define	CFR_PUT_DATA4(cfr, d1, d2, d3, d4)				\
   2677 	(*(cfr)->ptr++ = (((d1)<<24) | ((d2)<<16) | ((d3)<<8) | (d4)))
   2678 
   2679 #define	CFR_PUT_DATA1(cfr, d)	(*(cfr)->ptr++ = (d))
   2680 
   2681 #define	CFR_PUT_VALUE(cfr, key, d)	(*(cfr)->ptr++ = ((key)<<24) | (d))
   2682 
   2683 #define	CFR_PUT_CRC(cfr, n)						\
   2684 	(*(cfr)->unit[n].start = ((cfr)->unit[n].length << 16) |	\
   2685 	    fwohci_crc16((cfr)->unit[n].start + 1, (cfr)->unit[n].length))
   2686 
   2687 #define	CFR_START_UNIT(cfr, n)						\
   2688 do {									\
   2689 	if ((cfr)->unit[n].refer != NULL) {				\
   2690 		*(cfr)->unit[n].refer |=				\
   2691 		    (cfr)->ptr - (cfr)->unit[n].refer;			\
   2692 		CFR_PUT_CRC(cfr, (cfr)->unit[n].refunit);		\
   2693 	}								\
   2694 	(cfr)->curunit = (n);						\
   2695 	(cfr)->unit[n].start = (cfr)->ptr++;				\
   2696 } while (0 /* CONSTCOND */)
   2697 
   2698 #define	CFR_PUT_REFER(cfr, key, n)					\
   2699 do {									\
   2700 	(cfr)->unit[n].refer = (cfr)->ptr;				\
   2701 	(cfr)->unit[n].refunit = (cfr)->curunit;			\
   2702 	*(cfr)->ptr++ = (key) << 24;					\
   2703 } while (0 /* CONSTCOND */)
   2704 
   2705 #define	CFR_END_UNIT(cfr)						\
   2706 do {									\
   2707 	(cfr)->unit[(cfr)->curunit].length = (cfr)->ptr -		\
   2708 	    ((cfr)->unit[(cfr)->curunit].start + 1);			\
   2709 	CFR_PUT_CRC(cfr, (cfr)->curunit);				\
   2710 } while (0 /* CONSTCOND */)
   2711 
   2712 static u_int16_t
   2713 fwohci_crc16(u_int32_t *ptr, int len)
   2714 {
   2715 	int shift;
   2716 	u_int32_t crc, sum, data;
   2717 
   2718 	crc = 0;
   2719 	while (len-- > 0) {
   2720 		data = *ptr++;
   2721 		for (shift = 28; shift >= 0; shift -= 4) {
   2722 			sum = ((crc >> 12) ^ (data >> shift)) & 0x000f;
   2723 			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
   2724 		}
   2725 		crc &= 0xffff;
   2726 	}
   2727 	return crc;
   2728 }
   2729 
   2730 static void
   2731 fwohci_configrom_init(struct fwohci_softc *sc)
   2732 {
   2733 	int i, val;
   2734 	struct fwohci_buf *fb;
   2735 	u_int32_t *hdr;
   2736 	struct configromctx cfr;
   2737 
   2738 	fb = &sc->sc_buf_cnfrom;
   2739 	memset(&cfr, 0, sizeof(cfr));
   2740 	cfr.ptr = hdr = (u_int32_t *)fb->fb_buf;
   2741 
   2742 	/* headers */
   2743 	CFR_START_UNIT(&cfr, 0);
   2744 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusId));
   2745 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusOptions));
   2746 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDHi));
   2747 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDLo));
   2748 	CFR_END_UNIT(&cfr);
   2749 	/* copy info_length from crc_length */
   2750 	*hdr |= (*hdr & 0x00ff0000) << 8;
   2751 	OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMhdr, *hdr);
   2752 
   2753 	/* root directory */
   2754 	CFR_START_UNIT(&cfr, 1);
   2755 	CFR_PUT_VALUE(&cfr, 0x03, 0x00005e);	/* vendor id */
   2756 	CFR_PUT_REFER(&cfr, 0x81, 2);		/* textual descriptor offset */
   2757 	CFR_PUT_VALUE(&cfr, 0x0c, 0x0083c0);	/* node capability */
   2758 						/* spt,64,fix,lst,drq */
   2759 #ifdef INET
   2760 	CFR_PUT_REFER(&cfr, 0xd1, 3);		/* IPv4 unit directory */
   2761 #endif /* INET */
   2762 #ifdef INET6
   2763 	CFR_PUT_REFER(&cfr, 0xd1, 4);		/* IPv6 unit directory */
   2764 #endif /* INET6 */
   2765 	CFR_END_UNIT(&cfr);
   2766 
   2767 	CFR_START_UNIT(&cfr, 2);
   2768 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2769 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2770 	CFR_PUT_DATA4(&cfr, 'N', 'e', 't', 'B');
   2771 	CFR_PUT_DATA4(&cfr, 'S', 'D', 0x00, 0x00);
   2772 	CFR_END_UNIT(&cfr);
   2773 
   2774 #ifdef INET
   2775 	/* IPv4 unit directory */
   2776 	CFR_START_UNIT(&cfr, 3);
   2777 	CFR_PUT_VALUE(&cfr, 0x12, 0x00005e);	/* unit spec id */
   2778 	CFR_PUT_REFER(&cfr, 0x81, 6);		/* textual descriptor offset */
   2779 	CFR_PUT_VALUE(&cfr, 0x13, 0x000001);	/* unit sw version */
   2780 	CFR_PUT_REFER(&cfr, 0x81, 7);		/* textual descriptor offset */
   2781 	CFR_PUT_REFER(&cfr, 0x95, 8);		/* Unit location */
   2782 	CFR_END_UNIT(&cfr);
   2783 
   2784 	CFR_START_UNIT(&cfr, 6);
   2785 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2786 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2787 	CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
   2788 	CFR_END_UNIT(&cfr);
   2789 
   2790 	CFR_START_UNIT(&cfr, 7);
   2791 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2792 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2793 	CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '4');
   2794 	CFR_END_UNIT(&cfr);
   2795 
   2796 	CFR_START_UNIT(&cfr, 8);		/* Spec's valid addr range. */
   2797 	CFR_PUT_DATA1(&cfr, FW_FIFO_HI);
   2798 	CFR_PUT_DATA1(&cfr, (FW_FIFO_LO | 0x1));
   2799 	CFR_PUT_DATA1(&cfr, FW_FIFO_HI);
   2800 	CFR_PUT_DATA1(&cfr, FW_FIFO_LO);
   2801 	CFR_END_UNIT(&cfr);
   2802 
   2803 #endif /* INET */
   2804 
   2805 #ifdef INET6
   2806 	/* IPv6 unit directory */
   2807 	CFR_START_UNIT(&cfr, 4);
   2808 	CFR_PUT_VALUE(&cfr, 0x12, 0x00005e);	/* unit spec id */
   2809 	CFR_PUT_REFER(&cfr, 0x81, 9);		/* textual descriptor offset */
   2810 	CFR_PUT_VALUE(&cfr, 0x13, 0x000002);	/* unit sw version */
   2811 						/* XXX: TBA by IANA */
   2812 	CFR_PUT_REFER(&cfr, 0x81, 10);		/* textual descriptor offset */
   2813 	CFR_PUT_REFER(&cfr, 0x95, 11);		/* Unit location */
   2814 	CFR_END_UNIT(&cfr);
   2815 
   2816 	CFR_START_UNIT(&cfr, 9);
   2817 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2818 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2819 	CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
   2820 	CFR_END_UNIT(&cfr);
   2821 
   2822 	CFR_START_UNIT(&cfr, 10);
   2823 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2824 	CFR_PUT_DATA1(&cfr, 0);
   2825 	CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '6');
   2826 	CFR_END_UNIT(&cfr);
   2827 
   2828 	CFR_START_UNIT(&cfr, 11);		/* Spec's valid addr range. */
   2829 	CFR_PUT_DATA1(&cfr, FW_FIFO_HI);
   2830 	CFR_PUT_DATA1(&cfr, (FW_FIFO_LO | 0x1));
   2831 	CFR_PUT_DATA1(&cfr, FW_FIFO_HI);
   2832 	CFR_PUT_DATA1(&cfr, FW_FIFO_LO);
   2833 	CFR_END_UNIT(&cfr);
   2834 
   2835 #endif /* INET6 */
   2836 
   2837 	fb->fb_off = cfr.ptr - hdr;
   2838 #ifdef FW_DEBUG
   2839 	DPRINTF(("%s: Config ROM:", sc->sc_sc1394.sc1394_dev.dv_xname));
   2840 	for (i = 0; i < fb->fb_off; i++)
   2841 		DPRINTF(("%s%08x", i&7?" ":"\n    ", hdr[i]));
   2842 	DPRINTF(("\n"));
   2843 #endif /* FW_DEBUG */
   2844 
   2845 	/*
   2846 	 * Make network byte order for DMA
   2847 	 */
   2848 	for (i = 0; i < fb->fb_off; i++)
   2849 		HTONL(hdr[i]);
   2850 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
   2851 	    (caddr_t)cfr.ptr - fb->fb_buf, BUS_DMASYNC_PREWRITE);
   2852 
   2853 	OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMmap,
   2854 	    fb->fb_dmamap->dm_segs[0].ds_addr);
   2855 
   2856 	/* This register is only valid on OHCI 1.1. */
   2857 	val = OHCI_CSR_READ(sc, OHCI_REG_Version);
   2858 	if ((OHCI_Version_GET_Version(val) == 1) &&
   2859 	    (OHCI_Version_GET_Revision(val) == 1))
   2860 		OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet,
   2861 		    OHCI_HCControl_BIBImageValid);
   2862 
   2863 	/* Just allow quad reads of the rom. */
   2864 	for (i = 0; i < fb->fb_off; i++)
   2865 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_REQ_QUAD,
   2866 		    CSR_BASE_HI, CSR_BASE_LO + CSR_CONFIG_ROM + (i * 4),
   2867 		    fwohci_configrom_input, NULL);
   2868 }
   2869 
   2870 static int
   2871 fwohci_configrom_input(struct fwohci_softc *sc, void *arg,
   2872     struct fwohci_pkt *pkt)
   2873 {
   2874 	struct fwohci_pkt res;
   2875 	u_int32_t loc, *rom;
   2876 
   2877 	/* This will be used as an array index so size accordingly. */
   2878 	loc = pkt->fp_hdr[2] - (CSR_BASE_LO + CSR_CONFIG_ROM);
   2879 	if ((loc & 0x03) != 0) {
   2880 		/* alignment error */
   2881 		return IEEE1394_RCODE_ADDRESS_ERROR;
   2882 	}
   2883 	else
   2884 		loc /= 4;
   2885 	rom = (u_int32_t *)sc->sc_buf_cnfrom.fb_buf;
   2886 
   2887 	DPRINTFN(1, ("fwohci_configrom_input: ConfigRom[0x%04x]: 0x%08x\n", loc,
   2888 	    ntohl(rom[loc])));
   2889 
   2890 	memset(&res, 0, sizeof(res));
   2891 	res.fp_hdr[3] = rom[loc];
   2892 	fwohci_atrs_output(sc, IEEE1394_RCODE_COMPLETE, pkt, &res);
   2893 	return -1;
   2894 }
   2895 
   2896 /*
   2897  * SelfID buffer (no DMA context)
   2898  */
   2899 static void
   2900 fwohci_selfid_init(struct fwohci_softc *sc)
   2901 {
   2902 	struct fwohci_buf *fb;
   2903 
   2904 	fb = &sc->sc_buf_selfid;
   2905 #ifdef DIAGNOSTIC
   2906 	if ((fb->fb_dmamap->dm_segs[0].ds_addr & 0x7ff) != 0)
   2907 		panic("fwohci_selfid_init: not aligned: %ld (%ld) %p",
   2908 		    (unsigned long)fb->fb_dmamap->dm_segs[0].ds_addr,
   2909 		    (unsigned long)fb->fb_dmamap->dm_segs[0].ds_len, fb->fb_buf);
   2910 #endif
   2911 	memset(fb->fb_buf, 0, fb->fb_dmamap->dm_segs[0].ds_len);
   2912 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
   2913 	    fb->fb_dmamap->dm_segs[0].ds_len, BUS_DMASYNC_PREREAD);
   2914 
   2915 	OHCI_CSR_WRITE(sc, OHCI_REG_SelfIDBuffer,
   2916 	    fb->fb_dmamap->dm_segs[0].ds_addr);
   2917 }
   2918 
   2919 static int
   2920 fwohci_selfid_input(struct fwohci_softc *sc)
   2921 {
   2922 	int i;
   2923 	u_int32_t count, val, gen;
   2924 	u_int32_t *buf;
   2925 
   2926 	buf = (u_int32_t *)sc->sc_buf_selfid.fb_buf;
   2927 	val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
   2928   again:
   2929 	if (val & OHCI_SelfID_Error) {
   2930 		printf("%s: SelfID Error\n", sc->sc_sc1394.sc1394_dev.dv_xname);
   2931 		return -1;
   2932 	}
   2933 	count = OHCI_BITVAL(val, OHCI_SelfID_Size);
   2934 
   2935 	bus_dmamap_sync(sc->sc_dmat, sc->sc_buf_selfid.fb_dmamap,
   2936 	    0, count << 2, BUS_DMASYNC_POSTREAD);
   2937 	gen = OHCI_BITVAL(buf[0], OHCI_SelfID_Gen);
   2938 
   2939 #ifdef FW_DEBUG
   2940 	DPRINTFN(1, ("%s: SelfID: 0x%08x", sc->sc_sc1394.sc1394_dev.dv_xname,
   2941 	    val));
   2942 	for (i = 0; i < count; i++)
   2943 		DPRINTFN(2, ("%s%08x", i&7?" ":"\n    ", buf[i]));
   2944 	DPRINTFN(1, ("\n"));
   2945 #endif /* FW_DEBUG */
   2946 
   2947 	for (i = 1; i < count; i += 2) {
   2948 		if (buf[i] != ~buf[i + 1])
   2949 			break;
   2950 		if (buf[i] & 0x00000001)
   2951 			continue;	/* more pkt */
   2952 		if (buf[i] & 0x00800000)
   2953 			continue;	/* external id */
   2954 		sc->sc_rootid = (buf[i] & 0x3f000000) >> 24;
   2955 		if ((buf[i] & 0x00400800) == 0x00400800)
   2956 			sc->sc_irmid = sc->sc_rootid;
   2957 	}
   2958 
   2959 	val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
   2960 	if (OHCI_BITVAL(val, OHCI_SelfID_Gen) != gen) {
   2961 		if (OHCI_BITVAL(val, OHCI_SelfID_Gen) !=
   2962 		    OHCI_BITVAL(buf[0], OHCI_SelfID_Gen))
   2963 			goto again;
   2964 		DPRINTF(("%s: SelfID Gen mismatch (%d, %d)\n",
   2965 		    sc->sc_sc1394.sc1394_dev.dv_xname, gen,
   2966 		    OHCI_BITVAL(val, OHCI_SelfID_Gen)));
   2967 		return -1;
   2968 	}
   2969 	if (i != count) {
   2970 		printf("%s: SelfID corrupted (%d, 0x%08x, 0x%08x)\n",
   2971 		    sc->sc_sc1394.sc1394_dev.dv_xname, i, buf[i], buf[i + 1]);
   2972 #if 1
   2973 		if (i == 1 && buf[i] == 0 && buf[i + 1] == 0) {
   2974 			/*
   2975 			 * XXX: CXD3222 sometimes fails to DMA
   2976 			 * selfid packet??
   2977 			 */
   2978 			sc->sc_rootid = (count - 1) / 2 - 1;
   2979 			sc->sc_irmid = sc->sc_rootid;
   2980 		} else
   2981 #endif
   2982 		return -1;
   2983 	}
   2984 
   2985 	val = OHCI_CSR_READ(sc, OHCI_REG_NodeId);
   2986 	if ((val & OHCI_NodeId_IDValid) == 0) {
   2987 		sc->sc_nodeid = 0xffff;		/* invalid */
   2988 		printf("%s: nodeid is invalid\n",
   2989 		    sc->sc_sc1394.sc1394_dev.dv_xname);
   2990 		return -1;
   2991 	}
   2992 	sc->sc_nodeid = val & 0xffff;
   2993 
   2994 	DPRINTF(("%s: nodeid=0x%04x(%d), rootid=%d, irmid=%d\n",
   2995 	    sc->sc_sc1394.sc1394_dev.dv_xname, sc->sc_nodeid,
   2996 	    sc->sc_nodeid & OHCI_NodeId_NodeNumber, sc->sc_rootid,
   2997 	    sc->sc_irmid));
   2998 
   2999 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) > sc->sc_rootid)
   3000 		return -1;
   3001 
   3002 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) == sc->sc_rootid)
   3003 		OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
   3004 		    OHCI_LinkControl_CycleMaster);
   3005 	else
   3006 		OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear,
   3007 		    OHCI_LinkControl_CycleMaster);
   3008 	return 0;
   3009 }
   3010 
   3011 /*
   3012  * some CSRs are handled by driver.
   3013  */
   3014 static void
   3015 fwohci_csr_init(struct fwohci_softc *sc)
   3016 {
   3017 	int i;
   3018 	static u_int32_t csr[] = {
   3019 	    CSR_STATE_CLEAR, CSR_STATE_SET, CSR_SB_CYCLE_TIME,
   3020 	    CSR_SB_BUS_TIME, CSR_SB_BUSY_TIMEOUT, CSR_SB_BUS_MANAGER_ID,
   3021 	    CSR_SB_CHANNEL_AVAILABLE_HI, CSR_SB_CHANNEL_AVAILABLE_LO,
   3022 	    CSR_SB_BROADCAST_CHANNEL
   3023 	};
   3024 
   3025 	for (i = 0; i < sizeof(csr) / sizeof(csr[0]); i++) {
   3026 		fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_QUAD,
   3027 		    CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
   3028 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_REQ_QUAD,
   3029 		    CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
   3030 	}
   3031 	sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] = 31;	/*XXX*/
   3032 }
   3033 
   3034 static int
   3035 fwohci_csr_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   3036 {
   3037 	struct fwohci_pkt res;
   3038 	u_int32_t reg;
   3039 
   3040 	/*
   3041 	 * XXX need to do special functionality other than just r/w...
   3042 	 */
   3043 	reg = pkt->fp_hdr[2] - CSR_BASE_LO;
   3044 
   3045 	if ((reg & 0x03) != 0) {
   3046 		/* alignment error */
   3047 		return IEEE1394_RCODE_ADDRESS_ERROR;
   3048 	}
   3049 	DPRINTFN(1, ("fwohci_csr_input: CSR[0x%04x]: 0x%08x", reg,
   3050 	    *(u_int32_t *)(&sc->sc_csr[reg])));
   3051 	if (pkt->fp_tcode == IEEE1394_TCODE_WRITE_REQ_QUAD) {
   3052 		DPRINTFN(1, (" -> 0x%08x\n",
   3053 		    ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base)));
   3054 		*(u_int32_t *)&sc->sc_csr[reg] =
   3055 		    ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base);
   3056 	} else {
   3057 		DPRINTFN(1, ("\n"));
   3058 		res.fp_hdr[3] = htonl(*(u_int32_t *)&sc->sc_csr[reg]);
   3059 		res.fp_iov[0].iov_base = &res.fp_hdr[3];
   3060 		res.fp_iov[0].iov_len = 4;
   3061 		res.fp_uio.uio_resid = 4;
   3062 		res.fp_uio.uio_iovcnt = 1;
   3063 		fwohci_atrs_output(sc, IEEE1394_RCODE_COMPLETE, pkt, &res);
   3064 		return -1;
   3065 	}
   3066 	return IEEE1394_RCODE_COMPLETE;
   3067 }
   3068 
   3069 /*
   3070  * Mapping between nodeid and unique ID (EUI-64).
   3071  *
   3072  * Track old mappings and simply update their devices with the new id's when
   3073  * they match an existing EUI. This allows proper renumeration of the bus.
   3074  */
   3075 static void
   3076 fwohci_uid_collect(struct fwohci_softc *sc)
   3077 {
   3078 	int i;
   3079 	struct fwohci_uidtbl *fu;
   3080 	struct ieee1394_softc *iea;
   3081 
   3082 	LIST_FOREACH(iea, &sc->sc_nodelist, sc1394_node)
   3083 		iea->sc1394_node_id = 0xffff;
   3084 
   3085 	if (sc->sc_uidtbl != NULL)
   3086 		free(sc->sc_uidtbl, M_DEVBUF);
   3087 	sc->sc_uidtbl = malloc(sizeof(*fu) * (sc->sc_rootid + 1), M_DEVBUF,
   3088 	    M_NOWAIT|M_ZERO);	/* XXX M_WAITOK requires locks */
   3089 	if (sc->sc_uidtbl == NULL)
   3090 		return;
   3091 
   3092 	for (i = 0, fu = sc->sc_uidtbl; i <= sc->sc_rootid; i++, fu++) {
   3093 		if (i == (sc->sc_nodeid & OHCI_NodeId_NodeNumber)) {
   3094 			memcpy(fu->fu_uid, sc->sc_sc1394.sc1394_guid, 8);
   3095 			fu->fu_valid = 3;
   3096 
   3097 			iea = (struct ieee1394_softc *)sc->sc_sc1394.sc1394_if;
   3098 			if (iea) {
   3099 				iea->sc1394_node_id = i;
   3100 				DPRINTF(("%s: Updating nodeid to %d\n",
   3101 				    iea->sc1394_dev.dv_xname,
   3102 				    iea->sc1394_node_id));
   3103 			}
   3104 		} else {
   3105 			fu->fu_valid = 0;
   3106 			fwohci_uid_req(sc, i);
   3107 		}
   3108 	}
   3109 	if (sc->sc_rootid == 0)
   3110 		fwohci_check_nodes(sc);
   3111 }
   3112 
   3113 static void
   3114 fwohci_uid_req(struct fwohci_softc *sc, int phyid)
   3115 {
   3116 	struct fwohci_pkt pkt;
   3117 
   3118 	memset(&pkt, 0, sizeof(pkt));
   3119 	pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   3120 	pkt.fp_hlen = 12;
   3121 	pkt.fp_dlen = 0;
   3122 	pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   3123 	    (pkt.fp_tcode << 4);
   3124 	pkt.fp_hdr[1] = ((0xffc0 | phyid) << 16) | CSR_BASE_HI;
   3125 	pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 12;
   3126 	fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, phyid,
   3127 	    sc->sc_tlabel, fwohci_uid_input, (void *)0);
   3128 	sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   3129 	fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   3130 
   3131 	pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   3132 	    (pkt.fp_tcode << 4);
   3133 	pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 16;
   3134 	fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, phyid,
   3135 	    sc->sc_tlabel, fwohci_uid_input, (void *)1);
   3136 	sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   3137 	fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   3138 }
   3139 
   3140 static int
   3141 fwohci_uid_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *res)
   3142 {
   3143 	struct fwohci_uidtbl *fu;
   3144 	struct ieee1394_softc *iea;
   3145 	struct ieee1394_attach_args fwa;
   3146 	int i, n, done, rcode, found;
   3147 
   3148 	found = 0;
   3149 
   3150 	n = (res->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
   3151 	rcode = (res->fp_hdr[1] & 0x0000f000) >> 12;
   3152 	if (rcode != IEEE1394_RCODE_COMPLETE ||
   3153 	    sc->sc_uidtbl == NULL ||
   3154 	    n > sc->sc_rootid)
   3155 		return 0;
   3156 	fu = &sc->sc_uidtbl[n];
   3157 	if (arg == 0) {
   3158 		memcpy(fu->fu_uid, res->fp_iov[0].iov_base, 4);
   3159 		fu->fu_valid |= 0x1;
   3160 	} else {
   3161 		memcpy(fu->fu_uid + 4, res->fp_iov[0].iov_base, 4);
   3162 		fu->fu_valid |= 0x2;
   3163 	}
   3164 #ifdef FW_DEBUG
   3165 	if (fu->fu_valid == 0x3)
   3166 		DPRINTFN(1, ("fwohci_uid_input: "
   3167 		    "Node %d, UID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", n,
   3168 		    fu->fu_uid[0], fu->fu_uid[1], fu->fu_uid[2], fu->fu_uid[3],
   3169 		    fu->fu_uid[4], fu->fu_uid[5], fu->fu_uid[6], fu->fu_uid[7]));
   3170 #endif
   3171 	if (fu->fu_valid == 0x3) {
   3172 		LIST_FOREACH(iea, &sc->sc_nodelist, sc1394_node)
   3173 			if (memcmp(iea->sc1394_guid, fu->fu_uid, 8) == 0) {
   3174 				found = 1;
   3175 				iea->sc1394_node_id = n;
   3176 				DPRINTF(("%s: Updating nodeid to %d\n",
   3177 				    iea->sc1394_dev.dv_xname,
   3178 				    iea->sc1394_node_id));
   3179 				if (iea->sc1394_callback.sc1394_reset)
   3180 					iea->sc1394_callback.sc1394_reset(iea,
   3181 					    iea->sc1394_callback.sc1394_resetarg);
   3182 				break;
   3183 			}
   3184 		if (!found) {
   3185 			strcpy(fwa.name, "fwnode");
   3186 			memcpy(fwa.uid, fu->fu_uid, 8);
   3187 			fwa.nodeid = n;
   3188 			iea = (struct ieee1394_softc *)
   3189 			    config_found_sm(&sc->sc_sc1394.sc1394_dev, &fwa,
   3190 			    fwohci_print, fwohci_submatch);
   3191 			if (iea != NULL)
   3192 				LIST_INSERT_HEAD(&sc->sc_nodelist, iea,
   3193 				    sc1394_node);
   3194 		}
   3195 	}
   3196 	done = 1;
   3197 
   3198 	for (i = 0; i < sc->sc_rootid + 1; i++) {
   3199 		fu = &sc->sc_uidtbl[i];
   3200 		if (fu->fu_valid != 0x3) {
   3201 			done = 0;
   3202 			break;
   3203 		}
   3204 	}
   3205 	if (done)
   3206 		fwohci_check_nodes(sc);
   3207 
   3208 	return 0;
   3209 }
   3210 
   3211 static void
   3212 fwohci_check_nodes(struct fwohci_softc *sc)
   3213 {
   3214 	struct device *detach = NULL;
   3215 	struct ieee1394_softc *iea;
   3216 
   3217 	LIST_FOREACH(iea, &sc->sc_nodelist, sc1394_node) {
   3218 
   3219 		/*
   3220 		 * Have to defer detachment until the next
   3221 		 * loop iteration since config_detach
   3222 		 * free's the softc and the loop iterator
   3223 		 * needs data from the softc to move
   3224 		 * forward.
   3225 		 */
   3226 
   3227 		if (detach) {
   3228 			config_detach(detach, 0);
   3229 			detach = NULL;
   3230 		}
   3231 		if (iea->sc1394_node_id == 0xffff) {
   3232 			detach = (struct device *)iea;
   3233 			LIST_REMOVE(iea, sc1394_node);
   3234 		}
   3235 	}
   3236 	if (detach)
   3237 		config_detach(detach, 0);
   3238 }
   3239 
   3240 static int
   3241 fwohci_uid_lookup(struct fwohci_softc *sc, const u_int8_t *uid)
   3242 {
   3243 	struct fwohci_uidtbl *fu;
   3244 	int n;
   3245 	static const u_int8_t bcast[] =
   3246 	    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
   3247 
   3248 	fu = sc->sc_uidtbl;
   3249 	if (fu == NULL) {
   3250 		if (memcmp(uid, bcast, sizeof(bcast)) == 0)
   3251 			return IEEE1394_BCAST_PHY_ID;
   3252 		fwohci_uid_collect(sc); /* try to get */
   3253 		return -1;
   3254 	}
   3255 	for (n = 0; n <= sc->sc_rootid; n++, fu++) {
   3256 		if (fu->fu_valid == 0x3 && memcmp(fu->fu_uid, uid, 8) == 0)
   3257 			return n;
   3258 	}
   3259 	if (memcmp(uid, bcast, sizeof(bcast)) == 0)
   3260 		return IEEE1394_BCAST_PHY_ID;
   3261 	for (n = 0, fu = sc->sc_uidtbl; n <= sc->sc_rootid; n++, fu++) {
   3262 		if (fu->fu_valid != 0x3) {
   3263 			/*
   3264 			 * XXX: need timer before retransmission
   3265 			 */
   3266 			fwohci_uid_req(sc, n);
   3267 		}
   3268 	}
   3269 	return -1;
   3270 }
   3271 
   3272 /*
   3273  * functions to support network interface
   3274  */
   3275 static int
   3276 fwohci_if_inreg(struct device *self, u_int32_t offhi, u_int32_t offlo,
   3277     void (*handler)(struct device *, struct mbuf *))
   3278 {
   3279 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
   3280 
   3281 	fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_BLOCK, offhi, offlo,
   3282 	    handler ? fwohci_if_input : NULL, handler);
   3283 	fwohci_handler_set(sc, IEEE1394_TCODE_STREAM_DATA,
   3284 	    (sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] & IEEE1394_ISOCH_MASK) |
   3285 	    OHCI_ASYNC_STREAM,
   3286 	    1 << IEEE1394_TAG_GASP,
   3287 	    handler ? fwohci_if_input : NULL, handler);
   3288 	return 0;
   3289 }
   3290 
   3291 static int
   3292 fwohci_if_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   3293 {
   3294 	int n, len;
   3295 	struct mbuf *m;
   3296 	struct iovec *iov;
   3297 	void (*handler)(struct device *, struct mbuf *) = arg;
   3298 
   3299 #ifdef FW_DEBUG
   3300 	int i;
   3301 	DPRINTFN(1, ("fwohci_if_input: tcode=0x%x, dlen=%d", pkt->fp_tcode,
   3302 	    pkt->fp_dlen));
   3303 	for (i = 0; i < pkt->fp_hlen/4; i++)
   3304 		DPRINTFN(2, ("%s%08x", i?" ":"\n    ", pkt->fp_hdr[i]));
   3305 	DPRINTFN(2, ("$"));
   3306 	for (n = 0, len = pkt->fp_dlen; len > 0; len -= i, n++){
   3307 		iov = &pkt->fp_iov[n];
   3308 		for (i = 0; i < iov->iov_len; i++)
   3309 			DPRINTFN(2, ("%s%02x", (i%32)?((i%4)?"":" "):"\n    ",
   3310 			    ((u_int8_t *)iov->iov_base)[i]));
   3311 		DPRINTFN(2, ("$"));
   3312 	}
   3313 	DPRINTFN(1, ("\n"));
   3314 #endif /* FW_DEBUG */
   3315 	len = pkt->fp_dlen;
   3316 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   3317 	if (m == NULL)
   3318 		return IEEE1394_RCODE_COMPLETE;
   3319 	m->m_len = 16;
   3320 	if (len + m->m_len > MHLEN) {
   3321 		MCLGET(m, M_DONTWAIT);
   3322 		if ((m->m_flags & M_EXT) == 0) {
   3323 			m_freem(m);
   3324 			return IEEE1394_RCODE_COMPLETE;
   3325 		}
   3326 	}
   3327 	n = (pkt->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
   3328 	if (sc->sc_uidtbl == NULL || n > sc->sc_rootid ||
   3329 	    sc->sc_uidtbl[n].fu_valid != 0x3) {
   3330 		printf("%s: packet from unknown node: phy id %d\n",
   3331 		    sc->sc_sc1394.sc1394_dev.dv_xname, n);
   3332 		m_freem(m);
   3333 		fwohci_uid_req(sc, n);
   3334 		return IEEE1394_RCODE_COMPLETE;
   3335 	}
   3336 	memcpy(mtod(m, caddr_t), sc->sc_uidtbl[n].fu_uid, 8);
   3337 	if (pkt->fp_tcode == IEEE1394_TCODE_STREAM_DATA) {
   3338 		m->m_flags |= M_BCAST;
   3339 		mtod(m, u_int32_t *)[2] = mtod(m, u_int32_t *)[3] = 0;
   3340 	} else {
   3341 		mtod(m, u_int32_t *)[2] = htonl(pkt->fp_hdr[1]);
   3342 		mtod(m, u_int32_t *)[3] = htonl(pkt->fp_hdr[2]);
   3343 	}
   3344 	mtod(m, u_int8_t *)[8] = n;	/*XXX: node id for debug */
   3345 	mtod(m, u_int8_t *)[9] =
   3346 	    (*pkt->fp_trail >> (16 + OHCI_CTXCTL_SPD_BITPOS)) &
   3347 	    ((1 << OHCI_CTXCTL_SPD_BITLEN) - 1);
   3348 
   3349 	m->m_pkthdr.rcvif = NULL;	/* set in child */
   3350 	m->m_pkthdr.len = len + m->m_len;
   3351 	/*
   3352 	 * We may use receive buffer by external mbuf instead of copy here.
   3353 	 * But asynchronous receive buffer must be operate in buffer fill
   3354 	 * mode, so that each receive buffer will shared by multiple mbufs.
   3355 	 * If upper layer doesn't free mbuf soon, e.g. application program
   3356 	 * is suspended, buffer must be reallocated.
   3357 	 * Isochronous buffer must be operate in packet buffer mode, and
   3358 	 * it is easy to map receive buffer to external mbuf.  But it is
   3359 	 * used for broadcast/multicast only, and is expected not so
   3360 	 * performance sensitive for now.
   3361 	 * XXX: The performance may be important for multicast case,
   3362 	 * so we should revisit here later.
   3363 	 *						-- onoe
   3364 	 */
   3365 	n = 0;
   3366 	iov = pkt->fp_uio.uio_iov;
   3367 	while (len > 0) {
   3368 		memcpy(mtod(m, caddr_t) + m->m_len, iov->iov_base,
   3369 		    iov->iov_len);
   3370 		m->m_len += iov->iov_len;
   3371 		len -= iov->iov_len;
   3372 		iov++;
   3373 	}
   3374 	(*handler)(sc->sc_sc1394.sc1394_if, m);
   3375 	return IEEE1394_RCODE_COMPLETE;
   3376 }
   3377 
   3378 static int
   3379 fwohci_if_input_iso(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   3380 {
   3381 	int n, len;
   3382 	int chan, tag;
   3383 	struct mbuf *m;
   3384 	struct iovec *iov;
   3385 	void (*handler)(struct device *, struct mbuf *) = arg;
   3386 #ifdef FW_DEBUG
   3387 	int i;
   3388 #endif
   3389 
   3390 	chan = (pkt->fp_hdr[0] & 0x00003f00) >> 8;
   3391 	tag  = (pkt->fp_hdr[0] & 0x0000c000) >> 14;
   3392 #ifdef FW_DEBUG
   3393 	DPRINTFN(1, ("fwohci_if_input_iso: "
   3394 	    "tcode=0x%x, chan=%d, tag=%x, dlen=%d",
   3395 	    pkt->fp_tcode, chan, tag, pkt->fp_dlen));
   3396 	for (i = 0; i < pkt->fp_hlen/4; i++)
   3397 		DPRINTFN(2, ("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]));
   3398 	DPRINTFN(2, ("$"));
   3399 	for (n = 0, len = pkt->fp_dlen; len > 0; len -= i, n++){
   3400 		iov = &pkt->fp_iov[n];
   3401 		for (i = 0; i < iov->iov_len; i++)
   3402 			DPRINTFN(2, ("%s%02x",
   3403 			    (i%32)?((i%4)?"":" "):"\n\t",
   3404 			    ((u_int8_t *)iov->iov_base)[i]));
   3405 		DPRINTFN(2, ("$"));
   3406 	}
   3407 	DPRINTFN(2, ("\n"));
   3408 #endif /* FW_DEBUG */
   3409 	len = pkt->fp_dlen;
   3410 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   3411 	if (m == NULL)
   3412 		return IEEE1394_RCODE_COMPLETE;
   3413 	m->m_len = 16;
   3414 	if (m->m_len + len > MHLEN) {
   3415 		MCLGET(m, M_DONTWAIT);
   3416 		if ((m->m_flags & M_EXT) == 0) {
   3417 			m_freem(m);
   3418 			return IEEE1394_RCODE_COMPLETE;
   3419 		}
   3420 	}
   3421 
   3422 	m->m_flags |= M_BCAST;
   3423 
   3424 	if (tag == IEEE1394_TAG_GASP) {
   3425 		n = (pkt->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
   3426 		if (sc->sc_uidtbl == NULL || n > sc->sc_rootid ||
   3427 		    sc->sc_uidtbl[n].fu_valid != 0x3) {
   3428 			printf("%s: packet from unknown node: phy id %d\n",
   3429 			    sc->sc_sc1394.sc1394_dev.dv_xname, n);
   3430 			m_freem(m);
   3431 			return IEEE1394_RCODE_COMPLETE;
   3432 		}
   3433 		memcpy(mtod(m, caddr_t), sc->sc_uidtbl[n].fu_uid, 8);
   3434 		mtod(m, u_int32_t *)[2] = htonl(pkt->fp_hdr[1]);
   3435 		mtod(m, u_int32_t *)[3] = htonl(pkt->fp_hdr[2]);
   3436 		mtod(m, u_int8_t *)[8] = n;	/*XXX: node id for debug */
   3437 		mtod(m, u_int8_t *)[9] =
   3438 		    (*pkt->fp_trail >> (16 + OHCI_CTXCTL_SPD_BITPOS)) &
   3439 		    ((1 << OHCI_CTXCTL_SPD_BITLEN) - 1);
   3440 	}
   3441 	mtod(m, u_int8_t *)[14] = chan;
   3442 	mtod(m, u_int8_t *)[15] = tag;
   3443 
   3444 
   3445 	m->m_pkthdr.rcvif = NULL;	/* set in child */
   3446 	m->m_pkthdr.len = len + m->m_len;
   3447 	/*
   3448 	 * We may use receive buffer by external mbuf instead of copy here.
   3449 	 * But asynchronous receive buffer must be operate in buffer fill
   3450 	 * mode, so that each receive buffer will shared by multiple mbufs.
   3451 	 * If upper layer doesn't free mbuf soon, e.g. application program
   3452 	 * is suspended, buffer must be reallocated.
   3453 	 * Isochronous buffer must be operate in packet buffer mode, and
   3454 	 * it is easy to map receive buffer to external mbuf.  But it is
   3455 	 * used for broadcast/multicast only, and is expected not so
   3456 	 * performance sensitive for now.
   3457 	 * XXX: The performance may be important for multicast case,
   3458 	 * so we should revisit here later.
   3459 	 *						-- onoe
   3460 	 */
   3461 	n = 0;
   3462 	iov = pkt->fp_uio.uio_iov;
   3463 	while (len > 0) {
   3464 		memcpy(mtod(m, caddr_t) + m->m_len, iov->iov_base,
   3465 		    iov->iov_len);
   3466 	        m->m_len += iov->iov_len;
   3467 	        len -= iov->iov_len;
   3468 		iov++;
   3469 	}
   3470 	(*handler)(sc->sc_sc1394.sc1394_if, m);
   3471 	return IEEE1394_RCODE_COMPLETE;
   3472 }
   3473 
   3474 
   3475 
   3476 static int
   3477 fwohci_if_output(struct device *self, struct mbuf *m0,
   3478     void (*callback)(struct device *, struct mbuf *))
   3479 {
   3480 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
   3481 	struct fwohci_pkt pkt;
   3482 	u_int8_t *p;
   3483 	int n, error, spd, hdrlen, maxrec;
   3484 #ifdef FW_DEBUG
   3485 	struct mbuf *m;
   3486 #endif
   3487 
   3488 	p = mtod(m0, u_int8_t *);
   3489 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
   3490 		spd = IEEE1394_SPD_S100;	/*XXX*/
   3491 		maxrec = 512;			/*XXX*/
   3492 		hdrlen = 8;
   3493 	} else {
   3494 		n = fwohci_uid_lookup(sc, p);
   3495 		if (n < 0) {
   3496 			printf("%s: nodeid unknown:"
   3497 			    " %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
   3498 			    sc->sc_sc1394.sc1394_dev.dv_xname,
   3499 			    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
   3500 			error = EHOSTUNREACH;
   3501 			goto end;
   3502 		}
   3503 		if (n == IEEE1394_BCAST_PHY_ID) {
   3504 			printf("%s: broadcast with !M_MCAST\n",
   3505 			    sc->sc_sc1394.sc1394_dev.dv_xname);
   3506 #ifdef FW_DEBUG
   3507 			DPRINTFN(2, ("packet:"));
   3508 			for (m = m0; m != NULL; m = m->m_next) {
   3509 				for (n = 0; n < m->m_len; n++)
   3510 					DPRINTFN(2, ("%s%02x", (n%32)?
   3511 					    ((n%4)?"":" "):"\n    ",
   3512 					    mtod(m, u_int8_t *)[n]));
   3513 				DPRINTFN(2, ("$"));
   3514 			}
   3515 			DPRINTFN(2, ("\n"));
   3516 #endif
   3517 			error = EHOSTUNREACH;
   3518 			goto end;
   3519 		}
   3520 		maxrec = 2 << p[8];
   3521 		spd = p[9];
   3522 		hdrlen = 0;
   3523 	}
   3524 	if (spd > sc->sc_sc1394.sc1394_link_speed) {
   3525 		DPRINTF(("fwohci_if_output: spd (%d) is faster than %d\n",
   3526 		    spd, sc->sc_sc1394.sc1394_link_speed));
   3527 		spd = sc->sc_sc1394.sc1394_link_speed;
   3528 	}
   3529 	if (maxrec > (512 << spd)) {
   3530 		DPRINTF(("fwohci_if_output: maxrec (%d) is larger for spd (%d)"
   3531 		    "\n", maxrec, spd));
   3532 		maxrec = 512 << spd;
   3533 	}
   3534 	while (maxrec > sc->sc_sc1394.sc1394_max_receive) {
   3535 		DPRINTF(("fwohci_if_output: maxrec (%d) is larger than"
   3536 		    " %d\n", maxrec, sc->sc_sc1394.sc1394_max_receive));
   3537 		maxrec >>= 1;
   3538 	}
   3539 	if (maxrec < 512) {
   3540 		DPRINTF(("fwohci_if_output: maxrec (%d) is smaller than "
   3541 		    "minimum\n", maxrec));
   3542 		maxrec = 512;
   3543 	}
   3544 
   3545 	m_adj(m0, 16 - hdrlen);
   3546 	if (m0->m_pkthdr.len > maxrec) {
   3547 		DPRINTF(("fwohci_if_output: packet too big: hdr %d, pktlen "
   3548 		    "%d, maxrec %d\n", hdrlen, m0->m_pkthdr.len, maxrec));
   3549 		error = E2BIG;	/*XXX*/
   3550 		goto end;
   3551 	}
   3552 
   3553 	memset(&pkt, 0, sizeof(pkt));
   3554 	pkt.fp_uio.uio_iov = pkt.fp_iov;
   3555 	pkt.fp_uio.uio_segflg = UIO_SYSSPACE;
   3556 	pkt.fp_uio.uio_rw = UIO_WRITE;
   3557 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
   3558 		/* construct GASP header */
   3559 		p = mtod(m0, u_int8_t *);
   3560 		p[0] = sc->sc_nodeid >> 8;
   3561 		p[1] = sc->sc_nodeid & 0xff;
   3562 		p[2] = 0x00; p[3] = 0x00; p[4] = 0x5e;
   3563 		p[5] = 0x00; p[6] = 0x00; p[7] = 0x01;
   3564 		pkt.fp_tcode = IEEE1394_TCODE_STREAM_DATA;
   3565 		pkt.fp_hlen = 8;
   3566 		pkt.fp_hdr[0] = (spd << 16) | (IEEE1394_TAG_GASP << 14) |
   3567 		    ((sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] &
   3568 		    OHCI_NodeId_NodeNumber) << 8);
   3569 		pkt.fp_hdr[1] = m0->m_pkthdr.len << 16;
   3570 	} else {
   3571 		pkt.fp_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
   3572 		pkt.fp_hlen = 16;
   3573 		pkt.fp_hdr[0] = 0x00800100 | (sc->sc_tlabel << 10) |
   3574 		    (spd << 16);
   3575 		pkt.fp_hdr[1] =
   3576 		    (((sc->sc_nodeid & OHCI_NodeId_BusNumber) | n) << 16) |
   3577 		    (p[10] << 8) | p[11];
   3578 		pkt.fp_hdr[2] = (p[12]<<24) | (p[13]<<16) | (p[14]<<8) | p[15];
   3579 		pkt.fp_hdr[3] = m0->m_pkthdr.len << 16;
   3580 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   3581 	}
   3582 	pkt.fp_hdr[0] |= (pkt.fp_tcode << 4);
   3583 	pkt.fp_dlen = m0->m_pkthdr.len;
   3584 	pkt.fp_m = m0;
   3585 	pkt.fp_callback = callback;
   3586 	error = fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   3587 	m0 = pkt.fp_m;
   3588   end:
   3589 	if (m0 != NULL) {
   3590 		if (callback)
   3591 			(*callback)(sc->sc_sc1394.sc1394_if, m0);
   3592 		else
   3593 			m_freem(m0);
   3594 	}
   3595 	return error;
   3596 }
   3597 
   3598 /*
   3599  * High level routines to provide abstraction to attaching layers to
   3600  * send/receive data.
   3601  */
   3602 
   3603 /*
   3604  * These break down into 4 routines as follows:
   3605  *
   3606  * int fwohci_read(struct ieee1394_abuf *)
   3607  *
   3608  * This routine will attempt to read a region from the requested node.
   3609  * A callback must be provided which will be called when either the completed
   3610  * read is done or an unrecoverable error occurs. This is mainly a convenience
   3611  * routine since it will encapsulate retrying a region as quadlet vs. block
   3612  * reads and recombining all the returned data. This could also be done with a
   3613  * series of write/inreg's for each packet sent.
   3614  *
   3615  * int fwohci_write(struct ieee1394_abuf *)
   3616  *
   3617  * The work horse main entry point for putting packets on the bus. This is the
   3618  * generalized interface for fwnode/etc code to put packets out onto the bus.
   3619  * It accepts all standard ieee1394 tcodes (XXX: only a few today) and
   3620  * optionally will callback via a func pointer to the calling code with the
   3621  * resulting ACK code from the packet. If the ACK code is to be ignored (i.e.
   3622  * no cb) then the write routine will take care of free'ing the abuf since the
   3623  * fwnode/etc code won't have any knowledge of when to do this. This allows for
   3624  * simple one-off packets to be sent from the upper-level code without worrying
   3625  * about a callback for cleanup.
   3626  *
   3627  * int fwohci_inreg(struct ieee1394_abuf *, int)
   3628  *
   3629  * This is very simple. It evals the abuf passed in and registers an internal
   3630  * handler as the callback for packets received for that operation.
   3631  * The integer argument specifies whether on a block read/write operation to
   3632  * allow sub-regions to be read/written (in block form) as well.
   3633  *
   3634  * XXX: This whole structure needs to be redone as a list of regions and
   3635  * operations allowed on those regions.
   3636  *
   3637  * int fwohci_unreg(struct ieee1394_abuf *, int)
   3638  *
   3639  * This simply unregisters the respective callback done via inreg for items
   3640  * which only need to register an area for a one-time operation (like a status
   3641  * buffer a remote node will write to when the current operation is done). The
   3642  * int argument specifies the same behavior as inreg, except in reverse (i.e.
   3643  * it unregisters).
   3644  */
   3645 
   3646 static int
   3647 fwohci_read(struct ieee1394_abuf *ab)
   3648 {
   3649 	struct fwohci_pkt pkt;
   3650 	struct ieee1394_softc *sc = ab->ab_req;
   3651 	struct fwohci_softc *psc =
   3652 	    (struct fwohci_softc *)sc->sc1394_dev.dv_parent;
   3653 	struct fwohci_cb *fcb;
   3654 	u_int32_t high, lo;
   3655 	int rv, tcode;
   3656 
   3657 	/* Have to have a callback when reading. */
   3658 	if (ab->ab_cb == NULL)
   3659 		return -1;
   3660 
   3661 	fcb = malloc(sizeof(struct fwohci_cb), M_DEVBUF, M_WAITOK);
   3662 	fcb->ab = ab;
   3663 	fcb->count = 0;
   3664 	fcb->abuf_valid = 1;
   3665 
   3666 	high = ((ab->ab_addr & 0x0000ffff00000000ULL) >> 32);
   3667 	lo = (ab->ab_addr & 0x00000000ffffffffULL);
   3668 
   3669 	memset(&pkt, 0, sizeof(pkt));
   3670 	pkt.fp_hdr[1] = ((0xffc0 | ab->ab_req->sc1394_node_id) << 16) | high;
   3671 	pkt.fp_hdr[2] = lo;
   3672 	pkt.fp_dlen = 0;
   3673 
   3674 	if (ab->ab_length == 4) {
   3675 		pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   3676 		tcode = IEEE1394_TCODE_READ_RESP_QUAD;
   3677 		pkt.fp_hlen = 12;
   3678 	} else {
   3679 		pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
   3680 		pkt.fp_hlen = 16;
   3681 		tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
   3682 		pkt.fp_hdr[3] = (ab->ab_length << 16);
   3683 	}
   3684 	pkt.fp_hdr[0] = 0x00000100 | (sc->sc1394_link_speed << 16) |
   3685 	    (psc->sc_tlabel << 10) | (pkt.fp_tcode << 4);
   3686 
   3687 	pkt.fp_statusarg = fcb;
   3688 	pkt.fp_statuscb = fwohci_read_resp;
   3689 
   3690 	rv = fwohci_handler_set(psc, tcode, ab->ab_req->sc1394_node_id,
   3691 	    psc->sc_tlabel, fwohci_read_resp, fcb);
   3692 	if (rv)
   3693 		return rv;
   3694 	rv = fwohci_at_output(psc, psc->sc_ctx_atrq, &pkt);
   3695 	if (rv)
   3696 		fwohci_handler_set(psc, tcode, ab->ab_req->sc1394_node_id,
   3697 		    psc->sc_tlabel, NULL, NULL);
   3698 	psc->sc_tlabel = (psc->sc_tlabel + 1) & 0x3f;
   3699 	fcb->count = 1;
   3700 	return rv;
   3701 }
   3702 
   3703 static int
   3704 fwohci_write(struct ieee1394_abuf *ab)
   3705 {
   3706 	struct fwohci_pkt pkt;
   3707 	struct ieee1394_softc *sc = ab->ab_req;
   3708 	struct fwohci_softc *psc =
   3709 	    (struct fwohci_softc *)sc->sc1394_dev.dv_parent;
   3710 	u_int32_t high, lo;
   3711 	int rv;
   3712 
   3713 	if (ab->ab_length > IEEE1394_MAX_REC(sc->sc1394_max_receive)) {
   3714 		DPRINTF(("Packet too large: %d\n", ab->ab_length));
   3715 		return E2BIG;
   3716 	}
   3717 
   3718 	if (ab->ab_data && ab->ab_uio)
   3719 		panic("Can't call with uio and data set");
   3720 	if ((ab->ab_data == NULL) && (ab->ab_uio == NULL))
   3721 		panic("One of either ab_data or ab_uio must be set");
   3722 
   3723 	memset(&pkt, 0, sizeof(pkt));
   3724 
   3725 	pkt.fp_tcode = ab->ab_tcode;
   3726 	if (ab->ab_data) {
   3727 		pkt.fp_uio.uio_iov = pkt.fp_iov;
   3728 		pkt.fp_uio.uio_segflg = UIO_SYSSPACE;
   3729 		pkt.fp_uio.uio_rw = UIO_WRITE;
   3730 	} else
   3731 		memcpy(&pkt.fp_uio, ab->ab_uio, sizeof(struct uio));
   3732 
   3733 	pkt.fp_statusarg = ab;
   3734 	pkt.fp_statuscb = fwohci_write_ack;
   3735 
   3736 	switch (ab->ab_tcode) {
   3737 	case IEEE1394_TCODE_WRITE_RESP:
   3738 		pkt.fp_hlen = 12;
   3739 	case IEEE1394_TCODE_READ_RESP_QUAD:
   3740 	case IEEE1394_TCODE_READ_RESP_BLOCK:
   3741 		if (!pkt.fp_hlen)
   3742 			pkt.fp_hlen = 16;
   3743 		high = ab->ab_retlen;
   3744 		ab->ab_retlen = 0;
   3745 		lo = 0;
   3746 		pkt.fp_hdr[0] = 0x00000100 | (sc->sc1394_link_speed << 16) |
   3747 		    (ab->ab_tlabel << 10) | (pkt.fp_tcode << 4);
   3748 		break;
   3749 	default:
   3750 		pkt.fp_hlen = 16;
   3751 		high = ((ab->ab_addr & 0x0000ffff00000000ULL) >> 32);
   3752 		lo = (ab->ab_addr & 0x00000000ffffffffULL);
   3753 		pkt.fp_hdr[0] = 0x00000100 | (sc->sc1394_link_speed << 16) |
   3754 		    (psc->sc_tlabel << 10) | (pkt.fp_tcode << 4);
   3755 		psc->sc_tlabel = (psc->sc_tlabel + 1) & 0x3f;
   3756 		break;
   3757 	}
   3758 
   3759 	pkt.fp_hdr[1] = ((0xffc0 | ab->ab_req->sc1394_node_id) << 16) | high;
   3760 	pkt.fp_hdr[2] = lo;
   3761 	if (pkt.fp_hlen == 16) {
   3762 		if (ab->ab_length == 4) {
   3763 			pkt.fp_hdr[3] = ab->ab_data[0];
   3764 			pkt.fp_dlen = 0;
   3765 		}  else {
   3766 			pkt.fp_hdr[3] = (ab->ab_length << 16);
   3767 			pkt.fp_dlen = ab->ab_length;
   3768 			if (ab->ab_data) {
   3769 				pkt.fp_uio.uio_iovcnt = 1;
   3770 				pkt.fp_uio.uio_resid = ab->ab_length;
   3771 				pkt.fp_iov[0].iov_base = ab->ab_data;
   3772 				pkt.fp_iov[0].iov_len = ab->ab_length;
   3773 			}
   3774 		}
   3775 	}
   3776 	switch (ab->ab_tcode) {
   3777 	case IEEE1394_TCODE_WRITE_RESP:
   3778 	case IEEE1394_TCODE_READ_RESP_QUAD:
   3779 	case IEEE1394_TCODE_READ_RESP_BLOCK:
   3780 		rv = fwohci_at_output(psc, psc->sc_ctx_atrs, &pkt);
   3781 		break;
   3782 	default:
   3783 		rv = fwohci_at_output(psc, psc->sc_ctx_atrq, &pkt);
   3784 		break;
   3785 	}
   3786 	return rv;
   3787 }
   3788 
   3789 static int
   3790 fwohci_read_resp(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   3791 {
   3792 	struct fwohci_cb *fcb = arg;
   3793 	struct ieee1394_abuf *ab = fcb->ab;
   3794 	struct fwohci_pkt newpkt;
   3795 	u_int32_t *cur, high, lo;
   3796 	int i, tcode, rcode, status, rv;
   3797 
   3798 	/*
   3799 	 * Both the ACK handling and normal response callbacks are handled here.
   3800 	 * The main reason for this is the various error conditions that can
   3801 	 * occur trying to block read some areas and the ways that gets reported
   3802 	 * back to calling station. This is a variety of ACK codes, responses,
   3803 	 * etc which makes it much more difficult to process if both aren't
   3804 	 * handled here.
   3805 	 */
   3806 
   3807 	/* Check for status packet. */
   3808 
   3809 	if (pkt->fp_tcode == -1) {
   3810 		status = pkt->fp_status & OHCI_DESC_STATUS_ACK_MASK;
   3811 		rcode = -1;
   3812 		tcode = (pkt->fp_hdr[0] >> 4) & 0xf;
   3813 		if ((status != OHCI_CTXCTL_EVENT_ACK_COMPLETE) &&
   3814 		    (status != OHCI_CTXCTL_EVENT_ACK_PENDING))
   3815 			DPRINTFN(2, ("Got status packet: 0x%02x\n",
   3816 			    (unsigned int)status));
   3817 		fcb->count--;
   3818 
   3819 		/*
   3820 		 * Got all the ack's back and the buffer is invalid (i.e. the
   3821 		 * callback has been called. Clean up.
   3822 		 */
   3823 
   3824 		if (fcb->abuf_valid == 0) {
   3825 			if (fcb->count == 0)
   3826 				free(fcb, M_DEVBUF);
   3827 			return IEEE1394_RCODE_COMPLETE;
   3828 		}
   3829 	} else {
   3830 		status = -1;
   3831 		tcode = pkt->fp_tcode;
   3832 		rcode = (pkt->fp_hdr[1] & 0x0000f000) >> 12;
   3833 	}
   3834 
   3835 	/*
   3836 	 * Some area's (like the config rom want to be read as quadlets only.
   3837 	 *
   3838 	 * The current ideas to try are:
   3839 	 *
   3840 	 * Got an ACK_TYPE_ERROR on a block read.
   3841 	 *
   3842 	 * Got either RCODE_TYPE or RCODE_ADDRESS errors in a block read
   3843 	 * response.
   3844 	 *
   3845 	 * In all cases construct a new packet for a quadlet read and let
   3846 	 * mutli_resp handle the iteration over the space.
   3847 	 */
   3848 
   3849 	if (((status == OHCI_CTXCTL_EVENT_ACK_TYPE_ERROR) &&
   3850 	     (tcode == IEEE1394_TCODE_READ_REQ_BLOCK)) ||
   3851 	    (((rcode == IEEE1394_RCODE_TYPE_ERROR) ||
   3852 	     (rcode == IEEE1394_RCODE_ADDRESS_ERROR)) &&
   3853 	      (tcode == IEEE1394_TCODE_READ_RESP_BLOCK))) {
   3854 
   3855 		/* Read the area in quadlet chunks (internally track this). */
   3856 
   3857 		memset(&newpkt, 0, sizeof(newpkt));
   3858 
   3859 		high = ((ab->ab_addr & 0x0000ffff00000000ULL) >> 32);
   3860 		lo = (ab->ab_addr & 0x00000000ffffffffULL);
   3861 
   3862 		newpkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   3863 		newpkt.fp_hlen = 12;
   3864 		newpkt.fp_dlen = 0;
   3865 		newpkt.fp_hdr[1] =
   3866 		    ((0xffc0 | ab->ab_req->sc1394_node_id) << 16) | high;
   3867 		newpkt.fp_hdr[2] = lo;
   3868 		newpkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   3869 		    (newpkt.fp_tcode << 4);
   3870 
   3871 		rv = fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD,
   3872 		    ab->ab_req->sc1394_node_id, sc->sc_tlabel,
   3873 		    fwohci_read_multi_resp, fcb);
   3874 		if (rv) {
   3875 			(*ab->ab_cb)(ab, -1);
   3876 			goto cleanup;
   3877 		}
   3878 		newpkt.fp_statusarg = fcb;
   3879 		newpkt.fp_statuscb = fwohci_read_resp;
   3880 		rv = fwohci_at_output(sc, sc->sc_ctx_atrq, &newpkt);
   3881 		if (rv) {
   3882 			fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD,
   3883 			    ab->ab_req->sc1394_node_id, sc->sc_tlabel, NULL,
   3884 			    NULL);
   3885 			(*ab->ab_cb)(ab, -1);
   3886 			goto cleanup;
   3887 		}
   3888 		fcb->count++;
   3889 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   3890 		return IEEE1394_RCODE_COMPLETE;
   3891 	} else if ((rcode != -1) || ((status != -1) &&
   3892 	    (status != OHCI_CTXCTL_EVENT_ACK_COMPLETE) &&
   3893 	    (status != OHCI_CTXCTL_EVENT_ACK_PENDING))) {
   3894 
   3895 		/*
   3896 		 * Recombine all the iov data into 1 chunk for higher
   3897 		 * level code.
   3898 		 */
   3899 
   3900 		if (rcode != -1) {
   3901 			cur = ab->ab_data;
   3902 			for (i = 0; i < pkt->fp_uio.uio_iovcnt; i++) {
   3903 				/*
   3904 				 * Make sure and don't exceed the buffer
   3905 				 * allocated for return.
   3906 				 */
   3907 				if ((ab->ab_retlen + pkt->fp_iov[i].iov_len) >
   3908 				    ab->ab_length) {
   3909 					memcpy(cur, pkt->fp_iov[i].iov_base,
   3910 					    (ab->ab_length - ab->ab_retlen));
   3911 					ab->ab_retlen = ab->ab_length;
   3912 					break;
   3913 				}
   3914 				memcpy(cur, pkt->fp_iov[i].iov_base,
   3915 				    pkt->fp_iov[i].iov_len);
   3916 				cur += pkt->fp_iov[i].iov_len;
   3917 				ab->ab_retlen += pkt->fp_iov[i].iov_len;
   3918 			}
   3919 		}
   3920 		if (status != -1)
   3921 			/* XXX: Need a complete tlabel interface. */
   3922 			for (i = 0; i < 64; i++)
   3923 				fwohci_handler_set(sc,
   3924 				    IEEE1394_TCODE_READ_RESP_QUAD,
   3925 				    ab->ab_req->sc1394_node_id, i, NULL, NULL);
   3926 		(*ab->ab_cb)(ab, rcode);
   3927 		goto cleanup;
   3928 	} else
   3929 		/* Good ack packet. */
   3930 		return IEEE1394_RCODE_COMPLETE;
   3931 
   3932 	/* Can't get here unless ab->ab_cb has been called. */
   3933 
   3934  cleanup:
   3935 	fcb->abuf_valid = 0;
   3936 	if (fcb->count == 0)
   3937 		free(fcb, M_DEVBUF);
   3938 	return IEEE1394_RCODE_COMPLETE;
   3939 }
   3940 
   3941 static int
   3942 fwohci_read_multi_resp(struct fwohci_softc *sc, void *arg,
   3943     struct fwohci_pkt *pkt)
   3944 {
   3945 	struct fwohci_cb *fcb = arg;
   3946 	struct ieee1394_abuf *ab = fcb->ab;
   3947 	struct fwohci_pkt newpkt;
   3948 	u_int32_t high, lo;
   3949 	int rcode, rv;
   3950 
   3951 	/*
   3952 	 * Bad return codes from the wire, just return what's already in the
   3953 	 * buf.
   3954 	 */
   3955 
   3956 	/* Make sure a response packet didn't arrive after a bad ACK. */
   3957 	if (fcb->abuf_valid == 0)
   3958 		return IEEE1394_RCODE_COMPLETE;
   3959 
   3960 	rcode = (pkt->fp_hdr[1] & 0x0000f000) >> 12;
   3961 
   3962 	if (rcode) {
   3963 		(*ab->ab_cb)(ab, rcode);
   3964 		goto cleanup;
   3965 	}
   3966 
   3967 	if ((ab->ab_retlen + pkt->fp_iov[0].iov_len) > ab->ab_length) {
   3968 		memcpy(((char *)ab->ab_data + ab->ab_retlen),
   3969 		    pkt->fp_iov[0].iov_base, (ab->ab_length - ab->ab_retlen));
   3970 		ab->ab_retlen = ab->ab_length;
   3971 	} else {
   3972 		memcpy(((char *)ab->ab_data + ab->ab_retlen),
   3973 		    pkt->fp_iov[0].iov_base, 4);
   3974 		ab->ab_retlen += 4;
   3975 	}
   3976 	/* Still more, loop and read 4 more bytes. */
   3977 	if (ab->ab_retlen < ab->ab_length) {
   3978 		memset(&newpkt, 0, sizeof(newpkt));
   3979 
   3980 		high = ((ab->ab_addr & 0x0000ffff00000000ULL) >> 32);
   3981 		lo = (ab->ab_addr & 0x00000000ffffffffULL) + ab->ab_retlen;
   3982 
   3983 		newpkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   3984 		newpkt.fp_hlen = 12;
   3985 		newpkt.fp_dlen = 0;
   3986 		newpkt.fp_hdr[1] =
   3987 		    ((0xffc0 | ab->ab_req->sc1394_node_id) << 16) | high;
   3988 		newpkt.fp_hdr[2] = lo;
   3989 		newpkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   3990 		    (newpkt.fp_tcode << 4);
   3991 
   3992 		newpkt.fp_statusarg = fcb;
   3993 		newpkt.fp_statuscb = fwohci_read_resp;
   3994 
   3995 		/*
   3996 		 * Bad return code.  Just give up and return what's
   3997 		 * come in now.
   3998 		 */
   3999 		rv = fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD,
   4000 		    ab->ab_req->sc1394_node_id, sc->sc_tlabel,
   4001 		    fwohci_read_multi_resp, fcb);
   4002 		if (rv)
   4003 			(*ab->ab_cb)(ab, -1);
   4004 		else {
   4005 			rv = fwohci_at_output(sc, sc->sc_ctx_atrq, &newpkt);
   4006 			if (rv) {
   4007 				fwohci_handler_set(sc,
   4008 				    IEEE1394_TCODE_READ_RESP_QUAD,
   4009 				    ab->ab_req->sc1394_node_id, sc->sc_tlabel,
   4010 				    NULL, NULL);
   4011 				(*ab->ab_cb)(ab, -1);
   4012 			} else {
   4013 				sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   4014 				fcb->count++;
   4015 				return IEEE1394_RCODE_COMPLETE;
   4016 			}
   4017 		}
   4018 	} else
   4019 		(*ab->ab_cb)(ab, IEEE1394_RCODE_COMPLETE);
   4020 
   4021  cleanup:
   4022 	/* Can't get here unless ab_cb has been called. */
   4023 	fcb->abuf_valid = 0;
   4024 	if (fcb->count == 0)
   4025 		free(fcb, M_DEVBUF);
   4026 	return IEEE1394_RCODE_COMPLETE;
   4027 }
   4028 
   4029 static int
   4030 fwohci_write_ack(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   4031 {
   4032 	struct ieee1394_abuf *ab = arg;
   4033 	u_int16_t status;
   4034 
   4035 
   4036 	status = pkt->fp_status & OHCI_DESC_STATUS_ACK_MASK;
   4037 	if ((status != OHCI_CTXCTL_EVENT_ACK_COMPLETE) &&
   4038 	    (status != OHCI_CTXCTL_EVENT_ACK_PENDING))
   4039 		DPRINTF(("Got status packet: 0x%02x\n",
   4040 		    (unsigned int)status));
   4041 
   4042 	/* No callback means this level should free the buffers. */
   4043 	if (ab->ab_cb)
   4044 		(*ab->ab_cb)(ab, status);
   4045 	else {
   4046 		if (ab->ab_data)
   4047 			free(ab->ab_data, M_1394DATA);
   4048 		free(ab, M_1394DATA);
   4049 	}
   4050 	return IEEE1394_RCODE_COMPLETE;
   4051 }
   4052 
   4053 static int
   4054 fwohci_inreg(struct ieee1394_abuf *ab, int allow)
   4055 {
   4056 	struct ieee1394_softc *sc = ab->ab_req;
   4057 	struct fwohci_softc *psc =
   4058 	    (struct fwohci_softc *)sc->sc1394_dev.dv_parent;
   4059 	u_int32_t high, lo;
   4060 	int i, j, rv;
   4061 
   4062 	high = ((ab->ab_addr & 0x0000ffff00000000ULL) >> 32);
   4063 	lo = (ab->ab_addr & 0x00000000ffffffffULL);
   4064 
   4065 	rv = 0;
   4066 	switch (ab->ab_tcode) {
   4067 	case IEEE1394_TCODE_READ_REQ_QUAD:
   4068 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   4069 		if (ab->ab_cb)
   4070 			rv = fwohci_handler_set(psc, ab->ab_tcode, high, lo,
   4071 			    fwohci_parse_input, ab);
   4072 		else
   4073 			fwohci_handler_set(psc, ab->ab_tcode, high, lo, NULL,
   4074 			    NULL);
   4075 		break;
   4076 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   4077 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   4078 		if (allow) {
   4079 			for (i = 0; i < (ab->ab_length / 4); i++) {
   4080 				if (ab->ab_cb) {
   4081 					rv = fwohci_handler_set(psc,
   4082 					    ab->ab_tcode, high, lo + (i * 4),
   4083 					    fwohci_parse_input, ab);
   4084 					if (rv)
   4085 						break;
   4086 				} else
   4087 					fwohci_handler_set(psc, ab->ab_tcode,
   4088 					    high, lo + (i * 4), NULL, NULL);
   4089 			}
   4090 			if (i != (ab->ab_length / 4)) {
   4091 				j = i + 1;
   4092 				for (i = 0; i < j; i++)
   4093 					fwohci_handler_set(psc, ab->ab_tcode,
   4094 					    high, lo + (i * 4), NULL, NULL);
   4095 			}
   4096 
   4097 			/*
   4098 			 * XXX: Need something to indicate writing a smaller
   4099 			 * amount is ok.
   4100 			 */
   4101 			if (ab->ab_cb)
   4102                                 ab->ab_subok = 1;
   4103 		} else {
   4104 			if (ab->ab_cb)
   4105 				rv = fwohci_handler_set(psc, ab->ab_tcode, high,
   4106 				    lo, fwohci_parse_input, ab);
   4107 			else
   4108 				fwohci_handler_set(psc, ab->ab_tcode, high, lo,
   4109 				    NULL, NULL);
   4110 		}
   4111 		break;
   4112 	default:
   4113 		DPRINTF(("Invalid registration tcode: %d\n", ab->ab_tcode));
   4114 		return -1;
   4115 		break;
   4116 	}
   4117 	return rv;
   4118 }
   4119 
   4120 static int
   4121 fwohci_unreg(struct ieee1394_abuf *ab, int allow)
   4122 {
   4123 	void *save;
   4124 	int rv;
   4125 
   4126 	save = ab->ab_cb;
   4127 	ab->ab_cb = NULL;
   4128 	rv = fwohci_inreg(ab, allow);
   4129 	ab->ab_cb = save;
   4130 	return rv;
   4131 }
   4132 
   4133 static int
   4134 fwohci_parse_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   4135 {
   4136 	struct ieee1394_abuf *ab = (struct ieee1394_abuf *)arg;
   4137 	u_int64_t addr;
   4138 	u_int8_t *cur;
   4139 	int i, count, ret;
   4140 
   4141 	ab->ab_tcode = (pkt->fp_hdr[0] >> 4) & 0xf;
   4142 	ab->ab_tlabel = (pkt->fp_hdr[0] >> 10) & 0x3f;
   4143 	addr = (((u_int64_t)(pkt->fp_hdr[1] & 0xffff) << 32) | pkt->fp_hdr[2]);
   4144 
   4145 	/* Make sure it's always 0 in case this gets reused multiple times. */
   4146 	ab->ab_retlen = 0;
   4147 
   4148 	switch (ab->ab_tcode) {
   4149 	case IEEE1394_TCODE_READ_REQ_QUAD:
   4150 		ab->ab_retlen = 4;
   4151 		/* Response's (if required) will come from callback code */
   4152 		ret = -1;
   4153 		break;
   4154 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   4155 		ab->ab_retlen = (pkt->fp_hdr[3] >> 16) & 0xffff;
   4156 		if (ab->ab_subok) {
   4157 			if ((addr + ab->ab_retlen) >
   4158 			    (ab->ab_addr + ab->ab_length))
   4159 				return IEEE1394_RCODE_ADDRESS_ERROR;
   4160 		} else
   4161 			if (ab->ab_retlen != ab->ab_length)
   4162 				return IEEE1394_RCODE_ADDRESS_ERROR;
   4163 		/* Response's (if required) will come from callback code */
   4164 		ret = -1;
   4165 		break;
   4166 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   4167 		ab->ab_retlen = 4;
   4168 		/* Fall through. */
   4169 
   4170 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   4171 		if (!ab->ab_retlen)
   4172 			ab->ab_retlen = (pkt->fp_hdr[3] >> 16) & 0xffff;
   4173 		if (ab->ab_subok) {
   4174 			if ((addr + ab->ab_retlen) >
   4175 			    (ab->ab_addr + ab->ab_length))
   4176 				return IEEE1394_RCODE_ADDRESS_ERROR;
   4177 		} else
   4178 			if (ab->ab_retlen != ab->ab_length)
   4179 				return IEEE1394_RCODE_ADDRESS_ERROR;
   4180 
   4181 		ab->ab_data = malloc(ab->ab_retlen, M_1394DATA, M_WAITOK);
   4182 		if (ab->ab_tcode == IEEE1394_TCODE_WRITE_REQ_QUAD)
   4183 			ab->ab_data[0] = pkt->fp_hdr[3];
   4184 		else {
   4185 			count = 0;
   4186 			cur = (u_int8_t *)ab->ab_data;
   4187 			for (i = 0; i < pkt->fp_uio.uio_iovcnt; i++) {
   4188 				memcpy(cur, pkt->fp_iov[i].iov_base,
   4189 				    pkt->fp_iov[i].iov_len);
   4190 				cur += pkt->fp_iov[i].iov_len;
   4191 				count += pkt->fp_iov[i].iov_len;
   4192 			}
   4193 			if (ab->ab_retlen != count)
   4194 				panic("Packet claims %d length "
   4195 				    "but only %d bytes returned\n",
   4196 				    ab->ab_retlen, count);
   4197 		}
   4198 		ret = IEEE1394_RCODE_COMPLETE;
   4199 		break;
   4200 	default:
   4201 		panic("Got a callback for a tcode that wasn't requested: %d",
   4202 		    ab->ab_tcode);
   4203 		break;
   4204 	}
   4205 	ab->ab_addr = addr;
   4206 	ab->ab_cb(ab, IEEE1394_RCODE_COMPLETE);
   4207 	return ret;
   4208 }
   4209 
   4210 static int
   4211 fwohci_submatch(struct device *parent, struct cfdata *cf, void *aux)
   4212 {
   4213 	struct ieee1394_attach_args *fwa = aux;
   4214 
   4215 	/* Both halves must be filled in for a match. */
   4216 	if ((cf->fwbuscf_idhi == FWBUS_UNK_IDHI &&
   4217 	    cf->fwbuscf_idlo == FWBUS_UNK_IDLO) ||
   4218 	    (cf->fwbuscf_idhi == ntohl(*((u_int32_t *)&fwa->uid[0])) &&
   4219 	    cf->fwbuscf_idlo == ntohl(*((u_int32_t *)&fwa->uid[4]))))
   4220 		return (config_match(parent, cf, aux));
   4221 	return 0;
   4222 }
   4223 
   4224 int
   4225 fwohci_detach(struct fwohci_softc *sc, int flags)
   4226 {
   4227 	int rv = 0;
   4228 
   4229 	if (sc->sc_sc1394.sc1394_if != NULL)
   4230 		rv = config_detach(sc->sc_sc1394.sc1394_if, flags);
   4231 	if (rv != 0)
   4232 		return (rv);
   4233 
   4234 	callout_stop(&sc->sc_selfid_callout);
   4235 
   4236 	if (sc->sc_powerhook != NULL)
   4237 		powerhook_disestablish(sc->sc_powerhook);
   4238 	if (sc->sc_shutdownhook != NULL)
   4239 		shutdownhook_disestablish(sc->sc_shutdownhook);
   4240 
   4241 	return (rv);
   4242 }
   4243 
   4244 int
   4245 fwohci_activate(struct device *self, enum devact act)
   4246 {
   4247 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
   4248 	int s, rv = 0;
   4249 
   4250 	s = splhigh();
   4251 	switch (act) {
   4252 	case DVACT_ACTIVATE:
   4253 		rv = EOPNOTSUPP;
   4254 		break;
   4255 
   4256 	case DVACT_DEACTIVATE:
   4257 		if (sc->sc_sc1394.sc1394_if != NULL)
   4258 	                rv = config_deactivate(sc->sc_sc1394.sc1394_if);
   4259 		break;
   4260 	}
   4261 	splx(s);
   4262 
   4263 	return (rv);
   4264 }
   4265 
   4266 #ifdef FW_DEBUG
   4267 static void
   4268 fwohci_show_intr(struct fwohci_softc *sc, u_int32_t intmask)
   4269 {
   4270 
   4271 	printf("%s: intmask=0x%08x:", sc->sc_sc1394.sc1394_dev.dv_xname,
   4272 	    intmask);
   4273 	if (intmask & OHCI_Int_CycleTooLong)
   4274 		printf(" CycleTooLong");
   4275 	if (intmask & OHCI_Int_UnrecoverableError)
   4276 		printf(" UnrecoverableError");
   4277 	if (intmask & OHCI_Int_CycleInconsistent)
   4278 		printf(" CycleInconsistent");
   4279 	if (intmask & OHCI_Int_BusReset)
   4280 		printf(" BusReset");
   4281 	if (intmask & OHCI_Int_SelfIDComplete)
   4282 		printf(" SelfIDComplete");
   4283 	if (intmask & OHCI_Int_LockRespErr)
   4284 		printf(" LockRespErr");
   4285 	if (intmask & OHCI_Int_PostedWriteErr)
   4286 		printf(" PostedWriteErr");
   4287 	if (intmask & OHCI_Int_ReqTxComplete)
   4288 		printf(" ReqTxComplete(0x%04x)",
   4289 		    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   4290 		    OHCI_SUBREG_ContextControlClear));
   4291 	if (intmask & OHCI_Int_RespTxComplete)
   4292 		printf(" RespTxComplete(0x%04x)",
   4293 		    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   4294 		    OHCI_SUBREG_ContextControlClear));
   4295 	if (intmask & OHCI_Int_ARRS)
   4296 		printf(" ARRS(0x%04x)",
   4297 		    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   4298 		    OHCI_SUBREG_ContextControlClear));
   4299 	if (intmask & OHCI_Int_ARRQ)
   4300 		printf(" ARRQ(0x%04x)",
   4301 		    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   4302 		    OHCI_SUBREG_ContextControlClear));
   4303 	if (intmask & OHCI_Int_IsochRx)
   4304 		printf(" IsochRx(0x%08x)",
   4305 		    OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntEventClear));
   4306 	if (intmask & OHCI_Int_IsochTx)
   4307 		printf(" IsochTx(0x%08x)",
   4308 		    OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntEventClear));
   4309 	if (intmask & OHCI_Int_RQPkt)
   4310 		printf(" RQPkt(0x%04x)",
   4311 		    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   4312 		    OHCI_SUBREG_ContextControlClear));
   4313 	if (intmask & OHCI_Int_RSPkt)
   4314 		printf(" RSPkt(0x%04x)",
   4315 		    OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   4316 		    OHCI_SUBREG_ContextControlClear));
   4317 	printf("\n");
   4318 }
   4319 
   4320 static void
   4321 fwohci_show_phypkt(struct fwohci_softc *sc, u_int32_t val)
   4322 {
   4323 	u_int8_t key, phyid;
   4324 
   4325 	key = (val & 0xc0000000) >> 30;
   4326 	phyid = (val & 0x3f000000) >> 24;
   4327 	printf("%s: PHY packet from %d: ",
   4328 	    sc->sc_sc1394.sc1394_dev.dv_xname, phyid);
   4329 	switch (key) {
   4330 	case 0:
   4331 		printf("PHY Config:");
   4332 		if (val & 0x00800000)
   4333 			printf(" ForceRoot");
   4334 		if (val & 0x00400000)
   4335 			printf(" Gap=%x", (val & 0x003f0000) >> 16);
   4336 		printf("\n");
   4337 		break;
   4338 	case 1:
   4339 		printf("Link-on\n");
   4340 		break;
   4341 	case 2:
   4342 		printf("SelfID:");
   4343 		if (val & 0x00800000) {
   4344 			printf(" #%d", (val & 0x00700000) >> 20);
   4345 		} else {
   4346 			if (val & 0x00400000)
   4347 				printf(" LinkActive");
   4348 			printf(" Gap=%x", (val & 0x003f0000) >> 16);
   4349 			printf(" Spd=S%d", 100 << ((val & 0x0000c000) >> 14));
   4350 			if (val & 0x00000800)
   4351 				printf(" Cont");
   4352 			if (val & 0x00000002)
   4353 				printf(" InitiateBusReset");
   4354 		}
   4355 		if (val & 0x00000001)
   4356 			printf(" +");
   4357 		printf("\n");
   4358 		break;
   4359 	default:
   4360 		printf("unknown: 0x%08x\n", val);
   4361 		break;
   4362 	}
   4363 }
   4364 #endif /* FW_DEBUG */
   4365 
   4366 #if 0
   4367 void fwohci_dumpreg(struct ieee1394_softc *, struct fwiso_regdump *);
   4368 
   4369 void
   4370 fwohci_dumpreg(struct ieee1394_softc *isc, struct fwiso_regdump *fr)
   4371 {
   4372 	struct fwohci_softc *sc = (struct fwohci_softc *)isc;
   4373 #if 0
   4374 	u_int32_t val;
   4375 
   4376 	printf("%s: dump reg\n", isc->sc1394_dev.dv_xname);
   4377 	printf("\tNodeID reg 0x%08x\n",
   4378 	    OHCI_CSR_READ(sc, OHCI_REG_NodeId));
   4379 	val = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   4380 	printf("\tIsoCounter 0x%08x, %d %d %d", val,
   4381 	    (val >> 25) & 0xfe, (val >> 12) & 0x1fff, val & 0xfff);
   4382 	val = OHCI_CSR_READ(sc, OHCI_REG_IntMaskSet);
   4383 	printf(" IntMask    0x%08x, %s\n", val,
   4384 	    val & OHCI_Int_IsochTx ? "isoTx" : "");
   4385 
   4386 	val = OHCI_SYNC_TX_DMA_READ(sc, 0, OHCI_SUBREG_ContextControlSet);
   4387 	printf("\tIT_CommandPtr 0x%08x ContextCtrl 0x%08x%s%s%s%s\n",
   4388 	    OHCI_SYNC_TX_DMA_READ(sc, 0, OHCI_SUBREG_CommandPtr),
   4389 	    val,
   4390 	    val & OHCI_CTXCTL_RUN ? " run" : "",
   4391 	    val & OHCI_CTXCTL_WAKE ? " wake" : "",
   4392 	    val & OHCI_CTXCTL_DEAD ? " dead" : "",
   4393 	    val & OHCI_CTXCTL_ACTIVE ? " active" : "");
   4394 #endif
   4395 
   4396 	fr->fr_nodeid = OHCI_CSR_READ(sc, OHCI_REG_NodeId);
   4397 	fr->fr_isocounter = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   4398 	fr->fr_intmask = OHCI_CSR_READ(sc, OHCI_REG_IntMaskSet);
   4399 	fr->fr_it0_commandptr = OHCI_SYNC_TX_DMA_READ(sc, 0, OHCI_SUBREG_CommandPtr);
   4400 	fr->fr_it0_contextctrl = OHCI_SYNC_TX_DMA_READ(sc, 0, OHCI_SUBREG_ContextControlSet);
   4401 
   4402 
   4403 }
   4404 #endif
   4405 
   4406 
   4407 u_int16_t
   4408 fwohci_cycletimer(struct fwohci_softc *sc)
   4409 {
   4410 	u_int32_t reg;
   4411 
   4412 	reg = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   4413 
   4414 	return (reg >> 12)&0xffff;
   4415 }
   4416 
   4417 
   4418 u_int16_t
   4419 fwohci_it_cycletimer(ieee1394_it_tag_t it)
   4420 {
   4421 	struct fwohci_it_ctx *itc = (struct fwohci_it_ctx *)it;
   4422 
   4423 	return fwohci_cycletimer(itc->itc_sc);
   4424 }
   4425 
   4426 
   4427 
   4428 
   4429 
   4430 /*
   4431  * return value: if positive value, number of DMA buffer segments.  If
   4432  * negative value, error happens.  Never zero.
   4433  */
   4434 static int
   4435 fwohci_misc_dmabuf_alloc(bus_dma_tag_t dmat, int dsize, int segno,
   4436     bus_dma_segment_t *segp, bus_dmamap_t *dmapp, void **mapp,
   4437     const char *xname)
   4438 {
   4439 	int nsegs;
   4440 	int error;
   4441 
   4442 	printf("fwohci_misc_desc_alloc: dsize %d segno %d\n", dsize, segno);
   4443 
   4444 	if ((error = bus_dmamem_alloc(dmat, dsize, PAGE_SIZE, 0,
   4445 	    segp, segno, &nsegs, 0)) != 0) {
   4446 		printf("%s: unable to allocate descriptor buffer, error = %d\n",
   4447 		    xname, error);
   4448 		goto fail_0;
   4449 	}
   4450 
   4451 	DPRINTF(("fwohci_misc_desc_alloc: %d segment[s]\n", nsegs));
   4452 
   4453 	if ((error = bus_dmamem_map(dmat, segp, nsegs, dsize, (caddr_t *)mapp,
   4454 	    BUS_DMA_COHERENT | BUS_DMA_WAITOK)) != 0) {
   4455 		printf("%s: unable to map descriptor buffer, error = %d\n",
   4456 		    xname, error);
   4457 		goto fail_1;
   4458 	}
   4459 
   4460 	DPRINTF(("fwohci_misc_desc_alloc: %s map ok\n", xname));
   4461 
   4462 #ifdef FWOHCI_DEBUG
   4463 	{
   4464 		int loop;
   4465 
   4466 		for (loop = 0; loop < nsegs; ++loop) {
   4467 			printf("\t%.2d: 0x%lx - 0x%lx\n", loop,
   4468 			    (long)segp[loop].ds_addr,
   4469 			    (long)segp[loop].ds_addr + segp[loop].ds_len - 1);
   4470 		}
   4471 	}
   4472 #endif /* FWOHCI_DEBUG */
   4473 
   4474 	if ((error = bus_dmamap_create(dmat, dsize, nsegs, dsize,
   4475 	    0, BUS_DMA_WAITOK, dmapp)) != 0) {
   4476 		printf("%s: unable to create descriptor buffer DMA map, "
   4477 		    "error = %d\n", xname, error);
   4478 		goto fail_2;
   4479 	}
   4480 
   4481 	DPRINTF(("fwohci_misc_dmabuf_alloc: bus_dmamem_create success\n"));
   4482 
   4483 	if ((error = bus_dmamap_load(dmat, *dmapp, *mapp, dsize, NULL,
   4484 	    BUS_DMA_WAITOK)) != 0) {
   4485 		printf("%s: unable to load descriptor buffer DMA map, "
   4486 		    "error = %d\n", xname, error);
   4487 		goto fail_3;
   4488 	}
   4489 
   4490 	DPRINTF(("fwohci_it_desc_alloc: bus_dmamem_load success\n"));
   4491 
   4492 	return nsegs;
   4493 
   4494   fail_3:
   4495 	bus_dmamap_destroy(dmat, *dmapp);
   4496   fail_2:
   4497 	bus_dmamem_unmap(dmat, *mapp, dsize);
   4498   fail_1:
   4499 	bus_dmamem_free(dmat, segp, nsegs);
   4500   fail_0:
   4501 	return error;
   4502 }
   4503 
   4504 
   4505 static void
   4506 fwohci_misc_dmabuf_free(bus_dma_tag_t dmat, int dsize, int nsegs,
   4507     bus_dma_segment_t *segp, bus_dmamap_t *dmapp, caddr_t map)
   4508 {
   4509 	bus_dmamap_destroy(dmat, *dmapp);
   4510 	bus_dmamem_unmap(dmat, map, dsize);
   4511 	bus_dmamem_free(dmat, segp, nsegs);
   4512 }
   4513 
   4514 
   4515 
   4516 
   4517 /*
   4518  * Isochronous receive service
   4519  */
   4520 
   4521 /*
   4522  * static struct fwohci_ir_ctx *
   4523  * fwohci_ir_ctx_construct(struct fwohci_softc *sc, int no, int ch, int tagbm,
   4524  *			   int bufnum, int maxsize, int flags)
   4525  */
   4526 static struct fwohci_ir_ctx *
   4527 fwohci_ir_ctx_construct(struct fwohci_softc *sc, int no, int ch, int tagbm,
   4528     int bufnum, int maxsize, int flags)
   4529 {
   4530 	struct fwohci_ir_ctx *irc;
   4531 	int i;
   4532 
   4533 	printf("fwohci_ir_construct(%s, %d, %d, %x, %d, %d\n",
   4534 	    sc->sc_sc1394.sc1394_dev.dv_xname, no, ch, tagbm, bufnum, maxsize);
   4535 
   4536 	if ((irc = malloc(sizeof(*irc), M_DEVBUF, M_WAITOK|M_ZERO)) == NULL) {
   4537 		return NULL;
   4538 	}
   4539 
   4540 	irc->irc_sc = sc;
   4541 
   4542 	irc->irc_num = no;
   4543 	irc->irc_status = 0;
   4544 
   4545 	irc->irc_channel = ch;
   4546 	irc->irc_tagbm = tagbm;
   4547 
   4548 	irc->irc_desc_num = bufnum;
   4549 
   4550 	irc->irc_flags = flags;
   4551 
   4552 	/* add header */
   4553 	maxsize += 8;
   4554 	/* rounding up */
   4555 	for (i = 32; i < maxsize; i <<= 1);
   4556 	printf("fwohci_ir_ctx_construct: maxsize %d => %d\n",
   4557 	    maxsize, i);
   4558 
   4559 	maxsize = i;
   4560 
   4561 	irc->irc_maxsize = maxsize;
   4562 	irc->irc_buf_totalsize = bufnum * maxsize;
   4563 
   4564 	if (fwohci_ir_buf_setup(irc)) {
   4565 		/* cannot alloc descriptor */
   4566 		return NULL;
   4567 	}
   4568 
   4569 	irc->irc_readtop = irc->irc_desc_map;
   4570 	irc->irc_writeend = irc->irc_desc_map + irc->irc_desc_num - 1;
   4571 	irc->irc_savedbranch = irc->irc_writeend->fd_branch;
   4572 	irc->irc_writeend->fd_branch = 0;
   4573 	/* sync */
   4574 
   4575 	if (fwohci_ir_stop(irc) || fwohci_ir_init(irc)) {
   4576 		return NULL;
   4577 	}
   4578 
   4579 	irc->irc_status |= IRC_STATUS_READY;
   4580 
   4581 	return irc;
   4582 }
   4583 
   4584 
   4585 
   4586 /*
   4587  * static void fwohci_ir_ctx_destruct(struct fwohci_ir_ctx *irc)
   4588  *
   4589  *	This function release all DMA buffers and itself.
   4590  */
   4591 static void
   4592 fwohci_ir_ctx_destruct(struct fwohci_ir_ctx *irc)
   4593 {
   4594 	fwohci_misc_dmabuf_free(irc->irc_sc->sc_dmat, irc->irc_buf_totalsize,
   4595 	    irc->irc_buf_nsegs, irc->irc_buf_segs,
   4596 	    &irc->irc_buf_dmamap, (caddr_t)irc->irc_buf);
   4597 	fwohci_misc_dmabuf_free(irc->irc_sc->sc_dmat,
   4598 	    irc->irc_desc_size,
   4599 	    irc->irc_desc_nsegs, &irc->irc_desc_seg,
   4600 	    &irc->irc_desc_dmamap, (caddr_t)irc->irc_desc_map);
   4601 
   4602 	free(irc, M_DEVBUF);
   4603 }
   4604 
   4605 
   4606 
   4607 
   4608 /*
   4609  * static int fwohci_ir_buf_setup(struct fwohci_ir_ctx *irc)
   4610  *
   4611  *	Allocates descriptors for context DMA dedicated for
   4612  *	isochronous receive.
   4613  *
   4614  *	This function returns 0 (zero) if it succeeds.  Otherwise,
   4615  *	return negative value.
   4616  */
   4617 static int
   4618 fwohci_ir_buf_setup(struct fwohci_ir_ctx *irc)
   4619 {
   4620 	int nsegs;
   4621 	struct fwohci_desc *fd;
   4622 	u_int32_t branch;
   4623 	int bufno = 0;		/* DMA segment */
   4624 	bus_size_t bufused = 0;	/* offset in a DMA segment */
   4625 
   4626 	irc->irc_desc_size = irc->irc_desc_num * sizeof(struct fwohci_desc);
   4627 
   4628 	nsegs = fwohci_misc_dmabuf_alloc(irc->irc_sc->sc_dmat,
   4629 	    irc->irc_desc_size, 1, &irc->irc_desc_seg, &irc->irc_desc_dmamap,
   4630 	    (void **)&irc->irc_desc_map,
   4631 	    irc->irc_sc->sc_sc1394.sc1394_dev.dv_xname);
   4632 
   4633 	if (nsegs < 0) {
   4634 		printf("fwohci_ir_buf_alloc: cannot get descriptor\n");
   4635 		return -1;
   4636 	}
   4637 	irc->irc_desc_nsegs = nsegs;
   4638 
   4639 	nsegs = fwohci_misc_dmabuf_alloc(irc->irc_sc->sc_dmat,
   4640 	    irc->irc_buf_totalsize, 16, irc->irc_buf_segs,
   4641 	    &irc->irc_buf_dmamap, (void **)&irc->irc_buf,
   4642 	    irc->irc_sc->sc_sc1394.sc1394_dev.dv_xname);
   4643 
   4644 	if (nsegs < 0) {
   4645 		printf("fwohci_ir_buf_alloc: cannot get DMA buffer\n");
   4646 		fwohci_misc_dmabuf_free(irc->irc_sc->sc_dmat,
   4647 		    irc->irc_desc_size,
   4648 		    irc->irc_desc_nsegs, &irc->irc_desc_seg,
   4649 		    &irc->irc_desc_dmamap, (caddr_t)irc->irc_desc_map);
   4650 		return -1;
   4651 	}
   4652 	irc->irc_buf_nsegs = nsegs;
   4653 
   4654 	branch = irc->irc_desc_dmamap->dm_segs[0].ds_addr
   4655 	    + sizeof(struct fwohci_desc);
   4656 	bufno = 0;
   4657 	bufused = 0;
   4658 
   4659 	for (fd = irc->irc_desc_map;
   4660 	     fd < irc->irc_desc_map + irc->irc_desc_num; ++fd) {
   4661 		fd->fd_flags = OHCI_DESC_INPUT | OHCI_DESC_LAST
   4662 		    | OHCI_DESC_STATUS | OHCI_DESC_BRANCH;
   4663 		if (irc->irc_flags & IEEE1394_IR_SHORTDELAY) {
   4664 			fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   4665 		}
   4666 #if 0
   4667 		if  ((fd - irc->irc_desc_map) % 64 == 0) {
   4668 			fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   4669 		}
   4670 #endif
   4671 		fd->fd_reqcount = irc->irc_maxsize;
   4672 		fd->fd_status = fd->fd_rescount = 0;
   4673 
   4674 		fd->fd_branch = branch | 0x01;
   4675 		branch += sizeof(struct fwohci_desc);
   4676 
   4677 		/* physical addr to data? */
   4678 		fd->fd_data =
   4679 		    (u_int32_t)((irc->irc_buf_segs[bufno].ds_addr + bufused));
   4680 		bufused += irc->irc_maxsize;
   4681 		if (bufused > irc->irc_buf_segs[bufno].ds_len) {
   4682 			bufused = 0;
   4683 			if (++bufno == irc->irc_buf_nsegs) {
   4684 				/* fail */
   4685 				printf("fwohci_ir_buf_setup fail\n");
   4686 
   4687 				fwohci_misc_dmabuf_free(irc->irc_sc->sc_dmat,
   4688 				    irc->irc_desc_size,
   4689 				    irc->irc_desc_nsegs, &irc->irc_desc_seg,
   4690 				    &irc->irc_desc_dmamap,
   4691 				    (caddr_t)irc->irc_desc_map);
   4692 				fwohci_misc_dmabuf_free(irc->irc_sc->sc_dmat,
   4693 				    irc->irc_buf_totalsize,
   4694 				    irc->irc_buf_nsegs, irc->irc_buf_segs,
   4695 				    &irc->irc_buf_dmamap,
   4696 				    (caddr_t)irc->irc_buf);
   4697 				return -1;
   4698 			}
   4699 		}
   4700 
   4701 #ifdef FWOHCI_DEBUG
   4702 		if (fd < irc->irc_desc_map + 4
   4703 		    || (fd > irc->irc_desc_map + irc->irc_desc_num - 4)) {
   4704 			printf("fwohci_ir_buf_setup: desc %d %p buf %08x"
   4705 			    " size %d branch %08x\n",
   4706 			    fd - irc->irc_desc_map, fd, fd->fd_data,
   4707 			    fd->fd_reqcount, fd->fd_branch);
   4708 		}
   4709 #endif /* FWOHCI_DEBUG */
   4710 	}
   4711 
   4712 	--fd;
   4713 	fd->fd_branch = irc->irc_desc_dmamap->dm_segs[0].ds_addr | 1;
   4714 	DPRINTF(("fwohci_ir_buf_setup: desc %d %p buf %08x size %d branch %08x\n",
   4715 	    fd - irc->irc_desc_map, fd, fd->fd_data, fd->fd_reqcount,
   4716 	    fd->fd_branch));
   4717 
   4718 	return 0;
   4719 }
   4720 
   4721 
   4722 
   4723 /*
   4724  * static void fwohci_ir_init(struct fwohci_ir_ctx *irc)
   4725  *
   4726  *	This function initialise DMA engine.
   4727  */
   4728 static int
   4729 fwohci_ir_init(struct fwohci_ir_ctx *irc)
   4730 {
   4731 	struct fwohci_softc *sc = irc->irc_sc;
   4732 	int n = irc->irc_num;
   4733 	u_int32_t ctxmatch;
   4734 
   4735 	ctxmatch = irc->irc_channel & IEEE1394_ISO_CHANNEL_MASK;
   4736 
   4737 	if (irc->irc_channel & IEEE1394_ISO_CHANNEL_ANY) {
   4738 		OHCI_SYNC_RX_DMA_WRITE(sc, n,
   4739 		    OHCI_SUBREG_ContextControlSet,
   4740 		    OHCI_CTXCTL_RX_MULTI_CHAN_MODE);
   4741 
   4742 		/* Receive all the isochronous channels */
   4743 		OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskHiSet, 0xffffffff);
   4744 		OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskLoSet, 0xffffffff);
   4745 		ctxmatch = 0;
   4746 	}
   4747 
   4748 	ctxmatch |= ((irc->irc_tagbm & 0x0f) << OHCI_CTXMATCH_TAG_BITPOS);
   4749 	OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextMatch, ctxmatch);
   4750 
   4751 	OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlClear,
   4752 	    OHCI_CTXCTL_RX_BUFFER_FILL | OHCI_CTXCTL_RX_CYCLE_MATCH_ENABLE);
   4753 	OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlSet,
   4754 	    OHCI_CTXCTL_RX_ISOCH_HEADER);
   4755 
   4756 	printf("fwohci_ir_init\n");
   4757 
   4758 	return 0;
   4759 }
   4760 
   4761 
   4762 /*
   4763  * static int fwohci_ir_start(struct fwohci_ir_ctx *irc)
   4764  *
   4765  *	This function starts DMA engine.  This function must call
   4766  *	after fwohci_ir_init() and active bit of context control
   4767  *	register negated.  This function will not check it.
   4768  */
   4769 static int
   4770 fwohci_ir_start(struct fwohci_ir_ctx *irc)
   4771 {
   4772 	struct fwohci_softc *sc = irc->irc_sc;
   4773 	int startidx = irc->irc_readtop - irc->irc_desc_map;
   4774 	u_int32_t startaddr;
   4775 
   4776 	startaddr = irc->irc_desc_dmamap->dm_segs[0].ds_addr
   4777 	    + sizeof(struct fwohci_desc)*startidx;
   4778 
   4779 	OHCI_SYNC_RX_DMA_WRITE(sc, irc->irc_num, OHCI_SUBREG_CommandPtr,
   4780 	    startaddr | 1);
   4781 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntEventClear,
   4782 		    (1 << irc->irc_num));
   4783 	OHCI_SYNC_RX_DMA_WRITE(sc, irc->irc_num,
   4784 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   4785 
   4786 	printf("fwohci_ir_start: CmdPtr %08x Ctx %08x startidx %d\n",
   4787 	    OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num, OHCI_SUBREG_CommandPtr),
   4788 	    OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num, OHCI_SUBREG_ContextControlSet),
   4789 	    startidx);
   4790 
   4791 	irc->irc_status &= ~IRC_STATUS_READY;
   4792 	irc->irc_status |= IRC_STATUS_RUN;
   4793 
   4794 	if ((irc->irc_flags & IEEE1394_IR_TRIGGER_CIP_SYNC) == 0) {
   4795 		irc->irc_status |= IRC_STATUS_RECEIVE;
   4796 	}
   4797 
   4798 	return 0;
   4799 }
   4800 
   4801 
   4802 
   4803 /*
   4804  * static int fwohci_ir_stop(struct fwohci_ir_ctx *irc)
   4805  *
   4806  *	This function stops DMA engine.
   4807  */
   4808 static int
   4809 fwohci_ir_stop(struct fwohci_ir_ctx *irc)
   4810 {
   4811 	struct fwohci_softc *sc = irc->irc_sc;
   4812 	int i;
   4813 
   4814 	printf("fwohci_ir_stop\n");
   4815 
   4816 	OHCI_SYNC_RX_DMA_WRITE(sc, irc->irc_num,
   4817 	    OHCI_SUBREG_ContextControlClear,
   4818 	    OHCI_CTXCTL_RUN | OHCI_CTXCTL_DEAD);
   4819 
   4820 	i = 0;
   4821 	while (OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   4822 	    OHCI_SUBREG_ContextControlSet) & OHCI_CTXCTL_ACTIVE) {
   4823 #if 0
   4824 		u_int32_t reg = OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   4825 		    OHCI_SUBREG_ContextControlClear);
   4826 
   4827 		printf("%s: %d intr IR_CommandPtr 0x%08x "
   4828 		    "ContextCtrl 0x%08x%s%s%s%s\n",
   4829 		    sc->sc_sc1394.sc1394_dev.dv_xname, i,
   4830 		    OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   4831 			OHCI_SUBREG_CommandPtr),
   4832 		    reg,
   4833 		    reg & OHCI_CTXCTL_RUN ? " run" : "",
   4834 		    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   4835 		    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   4836 		    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   4837 #endif
   4838 		if (i > 20) {
   4839 			printf("fwohci_ir_stop: %s does not stop\n",
   4840 			    sc->sc_sc1394.sc1394_dev.dv_xname);
   4841 			return 1;
   4842 		}
   4843 		DELAY(10);
   4844 	}
   4845 
   4846 	irc->irc_status &= ~IRC_STATUS_RUN;
   4847 
   4848 	return 0;
   4849 }
   4850 
   4851 
   4852 
   4853 
   4854 
   4855 
   4856 static void
   4857 fwohci_ir_intr(struct fwohci_softc *sc, struct fwohci_ir_ctx *irc)
   4858 {
   4859 	const char *xname = sc->sc_sc1394.sc1394_dev.dv_xname;
   4860 	u_int32_t cmd, ctx;
   4861 	int idx;
   4862 	struct fwohci_desc *fd;
   4863 
   4864 	sc->sc_isocnt.ev_count++;
   4865 
   4866 	if (!(irc->irc_status & IRC_STATUS_RUN)) {
   4867 		printf("fwohci_ir_intr: not running\n");
   4868 		return;
   4869 	}
   4870 
   4871 	bus_dmamap_sync(sc->sc_dmat, irc->irc_desc_dmamap,
   4872 	    0, irc->irc_desc_size, BUS_DMASYNC_PREREAD);
   4873 
   4874 	ctx = OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   4875 	    OHCI_SUBREG_ContextControlSet);
   4876 
   4877 	cmd = OHCI_SYNC_RX_DMA_READ(sc, irc->irc_num,
   4878 	    OHCI_SUBREG_CommandPtr);
   4879 
   4880 #define OHCI_CTXCTL_RUNNING (OHCI_CTXCTL_RUN|OHCI_CTXCTL_ACTIVE)
   4881 #define OHCI_CTXCTL_RUNNING_MASK (OHCI_CTXCTL_RUNNING|OHCI_CTXCTL_DEAD)
   4882 
   4883 	idx = (cmd & 0xfffffff8) - (u_int32_t)irc->irc_desc_dmamap->dm_segs[0].ds_addr;
   4884 	idx /= sizeof(struct fwohci_desc);
   4885 
   4886 	if ((ctx & OHCI_CTXCTL_RUNNING_MASK) == OHCI_CTXCTL_RUNNING) {
   4887 		if (irc->irc_waitchan != NULL) {
   4888 			DPRINTF(("fwohci_ir_intr: wakeup "
   4889 			    "ctx %d CmdPtr %08x Ctxctl %08x idx %d\n",
   4890 			    irc->irc_num, cmd, ctx, idx));
   4891 #ifdef FWOHCI_WAIT_DEBUG
   4892 			irc->irc_cycle[1] = fwohci_cycletimer(irc->irc_sc);
   4893 #endif
   4894 			wakeup((void *)irc->irc_waitchan);
   4895 		}
   4896 		selwakeup(&irc->irc_sel);
   4897 		return;
   4898 	}
   4899 
   4900 	fd = irc->irc_desc_map + idx;
   4901 
   4902 	printf("fwohci_ir_intr: %s error "
   4903 	    "ctx %d CmdPtr %08x Ctxctl %08x idx %d\n", xname,
   4904 	    irc->irc_num, cmd, ctx, idx);
   4905 	printf("\tfd flag %x branch %x stat %x rescnt %x total pkt %d\n",
   4906 	    fd->fd_flags, fd->fd_branch, fd->fd_status,fd->fd_rescount,
   4907 	    irc->irc_pktcount);
   4908 }
   4909 
   4910 
   4911 
   4912 
   4913 /*
   4914  * static int fwohci_ir_ctx_packetnum(struct fwohci_ir_ctx *irc)
   4915  *
   4916  *	This function obtains the lenth of descriptors with data.
   4917  */
   4918 static int
   4919 fwohci_ir_ctx_packetnum(struct fwohci_ir_ctx *irc)
   4920 {
   4921 	struct fwohci_desc *fd = irc->irc_readtop;
   4922 	int i = 0;
   4923 
   4924 	/* XXX SYNC */
   4925 	while (fd->fd_status != 0) {
   4926 		if (fd == irc->irc_readtop && i > 0) {
   4927 			printf("descriptor filled %d at %d\n", i,
   4928 			    irc->irc_pktcount);
   4929 #ifdef FWOHCI_WAIT_DEBUG
   4930 			irc->irc_cycle[2] = fwohci_cycletimer(irc->irc_sc);
   4931 			printf("cycletimer %d:%d %d:%d %d:%d\n",
   4932 			    irc->irc_cycle[0]>>13, irc->irc_cycle[0]&0x1fff,
   4933 			    irc->irc_cycle[1]>>13, irc->irc_cycle[1]&0x1fff,
   4934 			    irc->irc_cycle[2]>>13, irc->irc_cycle[2]&0x1fff);
   4935 #endif
   4936 
   4937 			break;
   4938 		}
   4939 
   4940 		++i;
   4941 		++fd;
   4942 		if (fd == irc->irc_desc_map + irc->irc_desc_num) {
   4943 			fd = irc->irc_desc_map;
   4944 		}
   4945 
   4946 	}
   4947 
   4948 	return i;
   4949 }
   4950 
   4951 
   4952 
   4953 
   4954 /*
   4955  * int fwohci_ir_read(struct device *dev, ieee1394_ir_tag_t tag,
   4956  *		      struct uio *uio, int headoffs, int flags)
   4957  *
   4958  *	This function reads data from fwohci's isochronous receive
   4959  *	buffer.
   4960  */
   4961 int
   4962 fwohci_ir_read(struct device *dev, ieee1394_ir_tag_t tag, struct uio *uio,
   4963     int headoffs, int flags)
   4964 {
   4965 	struct fwohci_ir_ctx *irc = (struct fwohci_ir_ctx *)tag;
   4966 	int packetnum;
   4967 	int copylen, hdrshim, fwisohdrsiz;
   4968 	struct fwohci_desc *fd, *fdprev;
   4969 	u_int8_t *data;
   4970 	int status = 0;
   4971 	u_int32_t tmpbranch;
   4972 	int pktcount_prev = irc->irc_pktcount;
   4973 #ifdef FW_DEBUG
   4974 	int totalread = 0;
   4975 #endif
   4976 
   4977 	if (irc->irc_status & IRC_STATUS_READY) {
   4978 		printf("fwohci_ir_read: starting iso read engine\n");
   4979 		fwohci_ir_start(irc);
   4980 	}
   4981 
   4982 	packetnum = fwohci_ir_ctx_packetnum(irc);
   4983 
   4984 	DPRINTF(("fwohci_ir_read resid %d DMA buf %d\n",
   4985 	    uio->uio_resid, packetnum));
   4986 
   4987 	if (packetnum == 0) {
   4988 		return EAGAIN;
   4989 	}
   4990 
   4991 #ifdef USEDRAIN
   4992 	if (packetnum > irc->irc_desc_num - irc->irc_desc_num/4) {
   4993 		packetnum -= fwohci_ir_ctx_drain(irc);
   4994 		if (irc->irc_pktcount != 0) {
   4995 			printf("fwohci_ir_read overrun %d\n",
   4996 			    irc->irc_pktcount);
   4997 		}
   4998 	}
   4999 #endif /* USEDRAIN */
   5000 
   5001 	fd = irc->irc_readtop;
   5002 
   5003 #if 0
   5004 	if ((irc->irc_status & IRC_STATUS_RECEIVE) == 0
   5005 	    && irc->irc_flags & IEEE1394_IR_TRIGGER_CIP_SYNC) {
   5006 		unsigned int s;
   5007 		int i = 0;
   5008 
   5009 		fdprev = fd;
   5010 		while (fd->fd_status != 0) {
   5011 			s = data[14] << 8;
   5012 			s |= data[15];
   5013 
   5014 			if (s != 0x0000ffffu) {
   5015 				DPRINTF(("find header %x at %d\n",
   5016 				    s, irc->irc_pktcount));
   5017 				irc->irc_status |= IRC_STATUS_RECEIVE;
   5018 				break;
   5019 			}
   5020 
   5021 			fd->fd_rescount = 0;
   5022 			fd->fd_status = 0;
   5023 
   5024 			fdprev = fd;
   5025 			if (++fd == irc->irc_desc_map + irc->irc_desc_num) {
   5026 				fd = irc->irc_desc_map;
   5027 				data = irc->irc_buf;
   5028 			}
   5029 			++i;
   5030 		}
   5031 
   5032 		/* XXX SYNC */
   5033 		if (i > 0) {
   5034 			tmpbranch = fdprev->fd_branch;
   5035 			fdprev->fd_branch = 0;
   5036 			irc->irc_writeend->fd_branch = irc->irc_savedbranch;
   5037 			irc->irc_writeend = fdprev;
   5038 			irc->irc_savedbranch = tmpbranch;
   5039 		}
   5040 		/* XXX SYNC */
   5041 
   5042 		if (fd->fd_status == 0) {
   5043 			return EAGAIN;
   5044 		}
   5045 	}
   5046 #endif
   5047 
   5048 	hdrshim = 8;
   5049 	fwisohdrsiz = 0;
   5050 	data = irc->irc_buf + (fd - irc->irc_desc_map) * irc->irc_maxsize;
   5051 	if (irc->irc_flags & IEEE1394_IR_NEEDHEADER) {
   5052 		fwisohdrsiz = sizeof(struct fwiso_header);
   5053 	}
   5054 
   5055 	while (fd->fd_status != 0 &&
   5056 	    (copylen = fd->fd_reqcount - fd->fd_rescount - hdrshim - headoffs)
   5057 	    + fwisohdrsiz < uio->uio_resid) {
   5058 
   5059 		DPRINTF(("pkt %04x:%04x uiomove %p, %d\n",
   5060 		    fd->fd_status, fd->fd_rescount,
   5061 		    (void *)(data + 8 + headoffs), copylen));
   5062 		if ((irc->irc_status & IRC_STATUS_RECEIVE) == 0) {
   5063 			DPRINTF(("[%d]", copylen));
   5064 			if (irc->irc_pktcount > 1000) {
   5065 				printf("no header found\n");
   5066 				status = EIO;
   5067 				break; /* XXX */
   5068 			}
   5069 		} else {
   5070 			DPRINTF(("<%d>", copylen));
   5071 		}
   5072 
   5073 		if ((irc->irc_status & IRC_STATUS_RECEIVE) == 0
   5074 		    && irc->irc_flags & IEEE1394_IR_TRIGGER_CIP_SYNC
   5075 		    && copylen > 0) {
   5076 			unsigned int s;
   5077 
   5078 			s = data[14] << 8;
   5079 			s |= data[15];
   5080 
   5081 			if (s != 0x0000ffffu) {
   5082 				DPRINTF(("find header %x at %d\n",
   5083 				    s, irc->irc_pktcount));
   5084 				irc->irc_status |= IRC_STATUS_RECEIVE;
   5085 			}
   5086 		}
   5087 
   5088 		if (irc->irc_status & IRC_STATUS_RECEIVE) {
   5089 			if (copylen > 0) {
   5090 				if (irc->irc_flags & IEEE1394_IR_NEEDHEADER) {
   5091 					struct fwiso_header fh;
   5092 
   5093 					fh.fh_timestamp = htonl((*(u_int32_t *)data) & 0xffff);
   5094 					fh.fh_speed = htonl((fd->fd_status >> 5)& 0x00000007);
   5095 					fh.fh_capture_size = htonl(copylen + 4);
   5096 					fh.fh_iso_header = htonl(*(u_int32_t *)(data + 4));
   5097 					status = uiomove((void *)&fh,
   5098 					    sizeof(fh), uio);
   5099 					if (status != 0) {
   5100 						/* An error happens */
   5101 						printf("uio error in hdr\n");
   5102 						break;
   5103 					}
   5104 				}
   5105 				status = uiomove((void *)(data + 8 + headoffs),
   5106 				    copylen, uio);
   5107 				if (status != 0) {
   5108 					/* An error happens */
   5109 					printf("uio error\n");
   5110 					break;
   5111 				}
   5112 #ifdef FW_DEBUG
   5113 				totalread += copylen;
   5114 #endif
   5115 			}
   5116 		}
   5117 
   5118 		fd->fd_rescount = 0;
   5119 		fd->fd_status = 0;
   5120 
   5121 #if 0
   5122 		/* advance writeend pointer and fill branch */
   5123 
   5124 		tmpbranch = fd->fd_branch;
   5125 		fd->fd_branch = 0;
   5126 		irc->irc_writeend->fd_branch = irc->irc_savedbranch;
   5127 		irc->irc_writeend = fd;
   5128 		irc->irc_savedbranch = tmpbranch;
   5129 #endif
   5130 		fdprev = fd;
   5131 
   5132 		data += irc->irc_maxsize;
   5133 		if (++fd == irc->irc_desc_map + irc->irc_desc_num) {
   5134 			fd = irc->irc_desc_map;
   5135 			data = irc->irc_buf;
   5136 		}
   5137 		++irc->irc_pktcount;
   5138 	}
   5139 
   5140 #if 1
   5141 	if (irc->irc_pktcount != pktcount_prev) {
   5142 		/* XXX SYNC */
   5143 		tmpbranch = fdprev->fd_branch;
   5144 		fdprev->fd_branch = 0;
   5145 		irc->irc_writeend->fd_branch = irc->irc_savedbranch;
   5146 		irc->irc_writeend = fdprev;
   5147 		irc->irc_savedbranch = tmpbranch;
   5148 		/* XXX SYNC */
   5149 	}
   5150 #endif
   5151 
   5152 	if (!(OHCI_SYNC_RX_DMA_READ(irc->irc_sc, irc->irc_num,
   5153 	    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)) {
   5154 		/* do wake */
   5155 		OHCI_SYNC_RX_DMA_WRITE(irc->irc_sc, irc->irc_num,
   5156 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   5157 	}
   5158 
   5159 	if (packetnum > irc->irc_maxqueuelen) {
   5160 		irc->irc_maxqueuelen = packetnum;
   5161 		irc->irc_maxqueuepos = irc->irc_pktcount;
   5162 	}
   5163 
   5164 	if (irc->irc_pktcount == pktcount_prev) {
   5165 #if 0
   5166 		printf("fwohci_ir_read: process 0 packet, total %d\n",
   5167 		    irc->irc_pktcount);
   5168 		if (++pktfail > 30) {
   5169 			return 0;
   5170 		}
   5171 #endif
   5172 		return EAGAIN;
   5173 	}
   5174 
   5175 	irc->irc_readtop = fd;
   5176 
   5177 	DPRINTF(("fwochi_ir_read: process %d packet, total %d\n",
   5178 	    totalread, irc->irc_pktcount));
   5179 
   5180 	return status;
   5181 }
   5182 
   5183 
   5184 
   5185 
   5186 /*
   5187  * int fwohci_ir_wait(struct device *dev, ieee1394_ir_tag_t tag,
   5188  *		      void *wchan, char *name)
   5189  *
   5190  *	This function waits till new data comes.
   5191  */
   5192 int
   5193 fwohci_ir_wait(struct device *dev, ieee1394_ir_tag_t tag, void *wchan, char *name)
   5194 {
   5195 	struct fwohci_ir_ctx *irc = (struct fwohci_ir_ctx *)tag;
   5196 	struct fwohci_desc *fd;
   5197 	int pktnum;
   5198 	int stat;
   5199 
   5200 	if ((pktnum = fwohci_ir_ctx_packetnum(irc)) > 4) {
   5201 		DPRINTF(("fwohci_ir_wait enough data %d\n", pktnum));
   5202 		return 0;
   5203 	}
   5204 
   5205 	fd = irc->irc_readtop + 32;
   5206 	if (fd >= irc->irc_desc_map + irc->irc_desc_num) {
   5207 		fd -= irc->irc_desc_num;
   5208 	}
   5209 
   5210 	irc->irc_waitchan = wchan;
   5211 	if ((irc->irc_flags & IEEE1394_IR_SHORTDELAY) == 0) {
   5212 		fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   5213 		DPRINTF(("fwohci_ir_wait stops %d set intr %d\n",
   5214 		    irc->irc_readtop - irc->irc_desc_map,
   5215 		    fd - irc->irc_desc_map));
   5216 		/* XXX SYNC */
   5217 	}
   5218 
   5219 #ifdef FWOHCI_WAIT_DEBUG
   5220 	irc->irc_cycle[0] = fwohci_cycletimer(irc->irc_sc);
   5221 #endif
   5222 
   5223 	irc->irc_status |= IRC_STATUS_SLEEPING;
   5224 	if ((stat = tsleep(wchan, PCATCH|PRIBIO, name, hz*10)) != 0) {
   5225 		irc->irc_waitchan = NULL;
   5226 		fd->fd_flags &= ~OHCI_DESC_INTR_ALWAYS;
   5227 		if (stat == EWOULDBLOCK) {
   5228 			printf("fwohci_ir_wait: timeout\n");
   5229 			return EIO;
   5230 		} else {
   5231 			return EINTR;
   5232 		}
   5233 	}
   5234 
   5235 	irc->irc_waitchan = NULL;
   5236 	if ((irc->irc_flags & IEEE1394_IR_SHORTDELAY) == 0) {
   5237 		fd->fd_flags &= ~OHCI_DESC_INTR_ALWAYS;
   5238 		/* XXX SYNC */
   5239 	}
   5240 
   5241 	DPRINTF(("fwohci_ir_wait: wakeup\n"));
   5242 
   5243 	return 0;
   5244 }
   5245 
   5246 
   5247 
   5248 
   5249 /*
   5250  * int fwohci_ir_select(struct device *dev, ieee1394_ir_tag_t tag,
   5251  *			   struct proc *p)
   5252  *
   5253  *	This function returns the number of packets in queue.
   5254  */
   5255 int
   5256 fwohci_ir_select(struct device *dev, ieee1394_ir_tag_t tag, struct proc *p)
   5257 {
   5258 	struct fwohci_ir_ctx *irc = (struct fwohci_ir_ctx *)tag;
   5259 	int pktnum;
   5260 
   5261 	if (irc->irc_status & IRC_STATUS_READY) {
   5262 		printf("fwohci_ir_select: starting iso read engine\n");
   5263 		fwohci_ir_start(irc);
   5264 	}
   5265 
   5266 	if ((pktnum = fwohci_ir_ctx_packetnum(irc)) == 0) {
   5267 		selrecord(p, &irc->irc_sel);
   5268 	}
   5269 
   5270 	return pktnum;
   5271 }
   5272 
   5273 
   5274 
   5275 #ifdef USEDRAIN
   5276 /*
   5277  * int fwohci_ir_ctx_drain(struct fwohci_ir_ctx *irc)
   5278  *
   5279  *	This function will drain all the packets in receive DMA
   5280  *	buffer.
   5281  */
   5282 static int
   5283 fwohci_ir_ctx_drain(struct fwohci_ir_ctx *irc)
   5284 {
   5285 	struct fwohci_desc *fd = irc->irc_readtop;
   5286 	u_int32_t reg;
   5287 	int count = 0;
   5288 
   5289 	reg = OHCI_SYNC_RX_DMA_READ(irc->irc_sc, irc->irc_num,
   5290 	    OHCI_SUBREG_ContextControlClear);
   5291 
   5292 	printf("fwohci_ir_ctx_drain ctx%s%s%s%s\n",
   5293 	    reg & OHCI_CTXCTL_RUN ? " run" : "",
   5294 	    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   5295 	    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   5296 	    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   5297 
   5298 	if ((reg & OHCI_CTXCTL_RUNNING_MASK) == OHCI_CTXCTL_RUN) {
   5299 		/* DMA engine is stopped */
   5300 		u_int32_t startadr;
   5301 
   5302 		for (fd = irc->irc_desc_map;
   5303 		     fd < irc->irc_desc_map + irc->irc_desc_num;
   5304 		     ++fd) {
   5305 			fd->fd_status = 0;
   5306 		}
   5307 
   5308 		/* Restore branch addr of the last descriptor */
   5309 		irc->irc_writeend->fd_branch = irc->irc_savedbranch;
   5310 
   5311 		irc->irc_readtop = irc->irc_desc_map;
   5312 		irc->irc_writeend = irc->irc_desc_map + irc->irc_desc_num - 1;
   5313 		irc->irc_savedbranch = irc->irc_writeend->fd_branch;
   5314 		irc->irc_writeend->fd_branch = 0;
   5315 
   5316 		count = irc->irc_desc_num;
   5317 
   5318 		OHCI_SYNC_RX_DMA_WRITE(irc->irc_sc, irc->irc_num,
   5319 		    OHCI_SUBREG_ContextControlClear,
   5320 		    OHCI_CTXCTL_RUN | OHCI_CTXCTL_DEAD);
   5321 
   5322 		startadr = (u_int32_t)irc->irc_desc_dmamap->dm_segs[0].ds_addr;
   5323 
   5324 		printf("fwohci_ir_ctx_drain: remove %d pkts\n", count);
   5325 
   5326 		OHCI_SYNC_RX_DMA_WRITE(irc->irc_sc, irc->irc_num,
   5327 		    OHCI_SUBREG_CommandPtr, startadr | 1);
   5328 
   5329 		OHCI_SYNC_RX_DMA_WRITE(irc->irc_sc, irc->irc_num,
   5330 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   5331 	} else {
   5332 		const int removecount = irc->irc_desc_num/2;
   5333 		u_int32_t tmpbranch;
   5334 
   5335 		for (count = 0; count < removecount; ++count) {
   5336 			if (fd->fd_status == 0) {
   5337 				break;
   5338 			}
   5339 
   5340 			fd->fd_status = 0;
   5341 
   5342 			tmpbranch = fd->fd_branch;
   5343 			fd->fd_branch = 0;
   5344 			irc->irc_writeend->fd_branch = irc->irc_savedbranch;
   5345 			irc->irc_writeend = fd;
   5346 			irc->irc_savedbranch = tmpbranch;
   5347 
   5348 			if (++fd == irc->irc_desc_map + irc->irc_desc_num) {
   5349 				fd = irc->irc_desc_map;
   5350 			}
   5351 			++count;
   5352 		}
   5353 
   5354 		printf("fwohci_ir_ctx_drain: remove %d pkts\n", count);
   5355 	}
   5356 
   5357 	return count;
   5358 }
   5359 #endif /* USEDRAIN */
   5360 
   5361 
   5362 
   5363 
   5364 
   5365 
   5366 
   5367 
   5368 
   5369 /*
   5370  * service routines for isochronous transmit
   5371  */
   5372 
   5373 
   5374 struct fwohci_it_ctx *
   5375 fwohci_it_ctx_construct(struct fwohci_softc *sc, int no, int ch, int tag, int maxsize)
   5376 {
   5377 	struct fwohci_it_ctx *itc;
   5378 	size_t dmastrsize;
   5379 	struct fwohci_it_dmabuf *dmastr;
   5380 	struct fwohci_desc *desc;
   5381 	bus_addr_t descphys;
   5382 	int nodesc;
   5383 	int i, j;
   5384 
   5385 	if ((itc = malloc(sizeof(*itc), M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) {
   5386 		return itc;
   5387 	}
   5388 
   5389 	itc->itc_num = no;
   5390 	itc->itc_flags = 0;
   5391 	itc->itc_sc = sc;
   5392 	itc->itc_bufnum = FWOHCI_IT_BUFNUM;
   5393 
   5394 	itc->itc_channel = ch;
   5395 	itc->itc_tag = tag;
   5396 	itc->itc_speed = OHCI_CTXCTL_SPD_100; /* XXX */
   5397 
   5398 	itc->itc_outpkt = 0;
   5399 
   5400 	itc->itc_maxsize = maxsize;
   5401 
   5402 	dmastrsize = sizeof(struct fwohci_it_dmabuf)*itc->itc_bufnum;
   5403 
   5404 	if ((dmastr = malloc(dmastrsize, M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) {
   5405 		goto error_1;
   5406 	}
   5407 	itc->itc_buf = dmastr;
   5408 
   5409 	/*
   5410 	 * Get memory for descriptors.  One buffer will have 256
   5411 	 * packet entry and 1 trailing descriptor for writing scratch.
   5412 	 * 4-byte space for scratch.
   5413 	 */
   5414 	itc->itc_descsize = (256*3 + 1)*itc->itc_bufnum;
   5415 
   5416 	if (fwohci_it_desc_alloc(itc)) {
   5417 		printf("%s: cannot get enough memory for descriptor\n",
   5418 		    sc->sc_sc1394.sc1394_dev.dv_xname);
   5419 		goto error_2;
   5420 	}
   5421 
   5422 	/* prepare DMA buffer */
   5423 	nodesc = itc->itc_descsize/itc->itc_bufnum;
   5424 	desc = (struct fwohci_desc *)itc->itc_descmap;
   5425 	descphys = itc->itc_dseg.ds_addr;
   5426 
   5427 	for (i = 0; i < itc->itc_bufnum; ++i) {
   5428 
   5429 		if (fwohci_itd_construct(itc, &dmastr[i], i, desc,
   5430 		    descphys, nodesc,
   5431 		    itc->itc_maxsize, itc->itc_scratch_paddr)) {
   5432 			goto error_3;
   5433 		}
   5434 		desc += nodesc;
   5435 		descphys += sizeof(struct fwohci_desc)*nodesc;
   5436 	}
   5437 
   5438 #if 1
   5439 	itc->itc_buf_start = itc->itc_buf;
   5440 	itc->itc_buf_end = itc->itc_buf;
   5441 	itc->itc_buf_linkend = itc->itc_buf;
   5442 #else
   5443 	itc->itc_bufidx_start = 0;
   5444 	itc->itc_bufidx_end = 0;
   5445 	itc->itc_bufidx_linkend = 0;
   5446 #endif
   5447 	itc->itc_buf_cnt = 0;
   5448 	itc->itc_waitchan = NULL;
   5449 	*itc->itc_scratch = 0xffffffff;
   5450 
   5451 	return itc;
   5452 
   5453  error_3:
   5454 	for (j = 0; j < i; ++j) {
   5455 		fwohci_itd_destruct(&dmastr[j]);
   5456 	}
   5457 	fwohci_it_desc_free(itc);
   5458  error_2:
   5459 	free(itc->itc_buf, M_DEVBUF);
   5460  error_1:
   5461 	free(itc, M_DEVBUF);
   5462 
   5463 	return NULL;
   5464 }
   5465 
   5466 
   5467 
   5468 void
   5469 fwohci_it_ctx_destruct(struct fwohci_it_ctx *itc)
   5470 {
   5471 	int i;
   5472 
   5473 	for (i = 0; i < itc->itc_bufnum; ++i) {
   5474 		fwohci_itd_destruct(&itc->itc_buf[i]);
   5475 	}
   5476 
   5477 	fwohci_it_desc_free(itc);
   5478 	free(itc, M_DEVBUF);
   5479 }
   5480 
   5481 
   5482 /*
   5483  * static int fwohci_it_desc_alloc(struct fwohci_it_ctx *itc)
   5484  *
   5485  *	Allocates descriptors for context DMA dedicated for
   5486  *	isochronous transmit.
   5487  *
   5488  *	This function returns 0 (zero) if it succeeds.  Otherwise,
   5489  *	return negative value.
   5490  */
   5491 static int
   5492 fwohci_it_desc_alloc(struct fwohci_it_ctx *itc)
   5493 {
   5494 	bus_dma_tag_t dmat = itc->itc_sc->sc_dmat;
   5495 	const char *xname = itc->itc_sc->sc_sc1394.sc1394_dev.dv_xname;
   5496 	int error, dsize;
   5497 
   5498 	/* add for scratch */
   5499 	itc->itc_descsize++;
   5500 
   5501 	/* rounding up to 256 */
   5502 	if ((itc->itc_descsize & 0x0ff) != 0) {
   5503 		itc->itc_descsize =
   5504 		    (itc->itc_descsize & ~0x0ff) + 0x100;
   5505 	}
   5506 	/* remove for scratch */
   5507 
   5508 	itc->itc_descsize--;
   5509 	printf("%s: fwohci_it_desc_alloc will allocate %d descs\n",
   5510 	    xname, itc->itc_descsize);
   5511 
   5512 	/*
   5513 	 * allocate descriptor buffer
   5514 	 */
   5515 	dsize = sizeof(struct fwohci_desc) * itc->itc_descsize;
   5516 
   5517 	printf("%s: fwohci_it_desc_alloc: descriptor %d, dsize %d\n",
   5518 	    xname, itc->itc_descsize, dsize);
   5519 
   5520 	if ((error = bus_dmamem_alloc(dmat, dsize, PAGE_SIZE, 0,
   5521 	    &itc->itc_dseg, 1, &itc->itc_dnsegs, 0)) != 0) {
   5522 		printf("%s: unable to allocate descriptor buffer, error = %d\n",
   5523 		    xname, error);
   5524 		goto fail_0;
   5525 	}
   5526 
   5527 	printf("fwohci_it_desc_alloc: %d segment[s]\n", itc->itc_dnsegs);
   5528 
   5529 	if ((error = bus_dmamem_map(dmat, &itc->itc_dseg,
   5530 	    itc->itc_dnsegs, dsize, (caddr_t *)&itc->itc_descmap,
   5531 	    BUS_DMA_COHERENT | BUS_DMA_WAITOK)) != 0) {
   5532 		printf("%s: unable to map descriptor buffer, error = %d\n",
   5533 		    xname, error);
   5534 		goto fail_1;
   5535 	}
   5536 
   5537 	printf("fwohci_it_desc_alloc: bus_dmamem_map success dseg %lx:%lx\n",
   5538 	    (long)itc->itc_dseg.ds_addr, (long)itc->itc_dseg.ds_len);
   5539 
   5540 	if ((error = bus_dmamap_create(dmat, dsize, itc->itc_dnsegs,
   5541 	    dsize, 0, BUS_DMA_WAITOK, &itc->itc_ddmamap)) != 0) {
   5542 		printf("%s: unable to create descriptor buffer DMA map, "
   5543 		    "error = %d\n", xname, error);
   5544 		goto fail_2;
   5545 	}
   5546 
   5547 	printf("fwohci_it_desc_alloc: bus_dmamem_create success\n");
   5548 
   5549 	{
   5550 		int loop;
   5551 
   5552 		for (loop = 0; loop < itc->itc_ddmamap->dm_nsegs; ++loop) {
   5553 			printf("\t%.2d: 0x%lx - 0x%lx\n", loop,
   5554 			    (long)itc->itc_ddmamap->dm_segs[loop].ds_addr,
   5555 			    (long)itc->itc_ddmamap->dm_segs[loop].ds_addr +
   5556 			    (long)itc->itc_ddmamap->dm_segs[loop].ds_len - 1);
   5557 		}
   5558 	}
   5559 
   5560 	if ((error = bus_dmamap_load(dmat, itc->itc_ddmamap,
   5561 	    itc->itc_descmap, dsize, NULL, BUS_DMA_WAITOK)) != 0) {
   5562 		printf("%s: unable to load descriptor buffer DMA map, "
   5563 		    "error = %d\n", xname, error);
   5564 		goto fail_3;
   5565 	}
   5566 
   5567 	printf("%s: fwohci_it_desc_alloc: get DMA memory phys:0x%08x vm:%p\n",
   5568 	    xname, (int)itc->itc_ddmamap->dm_segs[0].ds_addr, itc->itc_descmap);
   5569 
   5570 	itc->itc_scratch = (u_int32_t *)(itc->itc_descmap
   5571 	    + (sizeof(struct fwohci_desc))*itc->itc_descsize);
   5572 	itc->itc_scratch_paddr =
   5573 	    itc->itc_ddmamap->dm_segs[0].ds_addr
   5574 	    + (sizeof(struct fwohci_desc))*itc->itc_descsize;
   5575 
   5576 	printf("%s: scratch %p, 0x%x\n", xname, itc->itc_scratch,
   5577 	    (int)itc->itc_scratch_paddr);
   5578 
   5579 	/* itc->itc_scratch_paddr = vtophys(itc->itc_scratch); */
   5580 
   5581 	return 0;
   5582 
   5583   fail_3:
   5584 	bus_dmamap_destroy(dmat, itc->itc_ddmamap);
   5585   fail_2:
   5586 	bus_dmamem_unmap(dmat, (caddr_t)itc->itc_descmap, dsize);
   5587   fail_1:
   5588 	bus_dmamem_free(dmat, &itc->itc_dseg, itc->itc_dnsegs);
   5589   fail_0:
   5590 	itc->itc_dnsegs = 0;
   5591 	itc->itc_descmap = NULL;
   5592 	return error;
   5593 }
   5594 
   5595 
   5596 static void
   5597 fwohci_it_desc_free(struct fwohci_it_ctx *itc)
   5598 {
   5599 	bus_dma_tag_t dmat = itc->itc_sc->sc_dmat;
   5600 	int dsize = sizeof(struct fwohci_desc) * itc->itc_descsize + 4;
   5601 
   5602 	bus_dmamap_destroy(dmat, itc->itc_ddmamap);
   5603 	bus_dmamem_unmap(dmat, (caddr_t)itc->itc_descmap, dsize);
   5604 	bus_dmamem_free(dmat, &itc->itc_dseg, itc->itc_dnsegs);
   5605 
   5606 	itc->itc_dnsegs = 0;
   5607 	itc->itc_descmap = NULL;
   5608 }
   5609 
   5610 
   5611 
   5612 /*
   5613  * int fwohci_it_ctx_writedata(ieee1394_it_tag_t it, int ndata,
   5614  *		struct ieee1394_it_datalist *itdata, int flags)
   5615  *
   5616  *	This function will write packet data to DMA buffer in the
   5617  *	context.  This function will parse ieee1394_it_datalist
   5618  *	command and fill DMA buffer.  This function will return the
   5619  *	number of written packets, or error code if the return value
   5620  *	is negative.
   5621  *
   5622  *	When this funtion returns positive value but smaller than
   5623  *	ndata, it reaches at the ent of DMA buffer.
   5624  */
   5625 int
   5626 fwohci_it_ctx_writedata(ieee1394_it_tag_t it, int ndata,
   5627     struct ieee1394_it_datalist *itdata, int flags)
   5628 {
   5629 	struct fwohci_it_ctx *itc = (struct fwohci_it_ctx *)it;
   5630 	int rv;
   5631 	int writepkt = 0;
   5632 	struct fwohci_it_dmabuf *itd;
   5633 	int i = 0;
   5634 
   5635 	itd = itc->itc_buf_end;
   5636 
   5637 	while (ndata > 0) {
   5638 		int s;
   5639 
   5640 		if (fwohci_itd_isfull(itd) || fwohci_itd_islocked(itd)) {
   5641 			if (itc->itc_buf_cnt == itc->itc_bufnum) {
   5642 				/* no space to write */
   5643 				printf("sleeping: start linkend end %d %d %d "
   5644 				    "bufcnt %d\n",
   5645 				    itc->itc_buf_start->itd_num,
   5646 				    itc->itc_buf_linkend->itd_num,
   5647 				    itc->itc_buf_end->itd_num,
   5648 				    itc->itc_buf_cnt);
   5649 
   5650 				itc->itc_waitchan = itc;
   5651 				if (tsleep((void *)itc->itc_waitchan,
   5652 				    PCATCH, "fwohci it", 0) == EWOULDBLOCK) {
   5653 					itc->itc_waitchan = NULL;
   5654 					printf("fwohci0 signal\n");
   5655 					break;
   5656 				}
   5657 				printf("waking:   start linkend end %d %d %d\n",
   5658 				    itc->itc_buf_start->itd_num,
   5659 				    itc->itc_buf_linkend->itd_num,
   5660 				    itc->itc_buf_end->itd_num);
   5661 
   5662 				itc->itc_waitchan = itc;
   5663 				i = 0;
   5664 			} else {
   5665 				/*
   5666 				 * Use next buffer.  This DMA buffer is full
   5667 				 * or locked.
   5668 				 */
   5669 				INC_BUF(itc, itd);
   5670 			}
   5671 		}
   5672 
   5673 		if (++i > 10) {
   5674 			panic("why loop so much %d", itc->itc_buf_cnt);
   5675 			break;
   5676 		}
   5677 
   5678 		s = splbio();
   5679 
   5680 		if (fwohci_itd_hasdata(itd) == 0) {
   5681 			++itc->itc_buf_cnt;
   5682 			DPRINTF(("<buf cnt %d>\n", itc->itc_buf_cnt));
   5683 		}
   5684 
   5685 		rv = fwohci_itd_writedata(itd, ndata, itdata);
   5686 		DPRINTF(("fwohci_it_ctx_writedata: buf %d ndata %d rv %d\n",
   5687 		    itd->itd_num, ndata, rv));
   5688 
   5689 		if (itc->itc_buf_start == itc->itc_buf_linkend
   5690 		    && (itc->itc_flags & ITC_FLAGS_RUN) != 0) {
   5691 
   5692 #ifdef DEBUG_USERADD
   5693 			printf("fwohci_it_ctx_writedata: emergency!\n");
   5694 #endif
   5695 			if (itc->itc_buf_linkend != itc->itc_buf_end
   5696 			    && fwohci_itd_hasdata(itc->itc_buf_end)) {
   5697 				struct fwohci_it_dmabuf *itdn = itc->itc_buf_linkend;
   5698 
   5699 				INC_BUF(itc, itdn);
   5700 				printf("connecting %d after %d\n",
   5701 				    itdn->itd_num,
   5702 				    itc->itc_buf_linkend->itd_num);
   5703 				if (fwohci_itd_link(itc->itc_buf_linkend, itdn)) {
   5704 					printf("fwohci_it_ctx_writedata:"
   5705 					    " cannot link correctly\n");
   5706 					return -1;
   5707 				}
   5708 				itc->itc_buf_linkend = itdn;
   5709 			}
   5710 		}
   5711 
   5712 		splx(s);
   5713 
   5714 		if (rv < 0) {
   5715 			/* some errors happend */
   5716 			break;
   5717 		}
   5718 
   5719 		writepkt += rv;
   5720 		ndata -= rv;
   5721 		itdata += rv;
   5722 		itc->itc_buf_end = itd;
   5723 	}
   5724 
   5725 	/* Start DMA engine if stopped */
   5726 	if ((itc->itc_flags & ITC_FLAGS_RUN) == 0) {
   5727 		if (itc->itc_buf_cnt > itc->itc_bufnum - 1 || flags) {
   5728 			/* run */
   5729 			printf("fwohci_itc_ctl_writedata: DMA engine start\n");
   5730 			fwohci_it_ctx_run(itc);
   5731 		}
   5732 	}
   5733 
   5734 	return writepkt;
   5735 }
   5736 
   5737 
   5738 
   5739 static void
   5740 fwohci_it_ctx_run(struct fwohci_it_ctx *itc)
   5741 {
   5742 	struct fwohci_softc *sc = itc->itc_sc;
   5743 	int ctx = itc->itc_num;
   5744 	struct fwohci_it_dmabuf *itd
   5745 	    = (struct fwohci_it_dmabuf *)itc->itc_buf_start;
   5746 	u_int32_t reg;
   5747 	int i;
   5748 
   5749 	if (itc->itc_flags & ITC_FLAGS_RUN) {
   5750 		return;
   5751 	}
   5752 	itc->itc_flags |= ITC_FLAGS_RUN;
   5753 
   5754 	/*
   5755 	 * dirty, but I can't imagine better place to save branch addr
   5756 	 * of top DMA buffer and substitute 0 to it.
   5757 	 */
   5758 	itd->itd_savedbranch = itd->itd_lastdesc->fd_branch;
   5759 	itd->itd_lastdesc->fd_branch = 0;
   5760 
   5761 	if (itc->itc_buf_cnt > 1) {
   5762 		struct fwohci_it_dmabuf *itdn = itd;
   5763 
   5764 #if 0
   5765 		INC_BUF(itc, itdn);
   5766 
   5767 		if (fwohci_itd_link(itd, itdn)) {
   5768 			printf("fwohci_it_ctx_run: cannot link correctly\n");
   5769 			return;
   5770 		}
   5771 		itc->itc_buf_linkend = itdn;
   5772 #else
   5773 		for (;;) {
   5774 			INC_BUF(itc, itdn);
   5775 
   5776 			if (itdn == itc->itc_buf_end) {
   5777 				break;
   5778 			}
   5779 			if (fwohci_itd_link(itd, itdn)) {
   5780 				printf("fwohci_it_ctx_run: cannot link\n");
   5781 				return;
   5782 			}
   5783 			itd = itdn;
   5784 		}
   5785 		itc->itc_buf_linkend = itd;
   5786 #endif
   5787 	} else {
   5788 		itd->itd_lastdesc->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   5789 		itc->itc_buf_linkend = itc->itc_buf_end;
   5790 		itc->itc_buf_end->itd_flags |= ITD_FLAGS_LOCK;
   5791 
   5792 		/* sanity check */
   5793 		if (itc->itc_buf_end != itc->itc_buf_start) {
   5794 			printf("buf start & end differs %p %p\n",
   5795 			    itc->itc_buf_end, itc->itc_buf_start);
   5796 		}
   5797 #if 0
   5798 		{
   5799 			u_int32_t *fdp;
   5800 			u_int32_t adr;
   5801 			int i;
   5802 
   5803 			printf("fwohci_it_ctx_run: itc_buf_cnt 1, DMA buf %d\n",
   5804 			    itd->itd_num);
   5805 			printf(" last desc %p npacket %d, %d 0x%04x%04x",
   5806 			    itd->itd_lastdesc, itd->itd_npacket,
   5807 			    (itd->itd_lastdesc - itd->itd_desc)/3,
   5808 			    itd->itd_lastdesc->fd_flags,
   5809 			    itd->itd_lastdesc->fd_reqcount);
   5810 			fdp = (u_int32_t *)itd->itd_desc;
   5811 			adr = (u_int32_t)itd->itd_desc_phys; /* XXX */
   5812 
   5813 			for (i = 0; i < 7*4; ++i) {
   5814 				if (i % 4 == 0) {
   5815 					printf("\n%x:", adr + 4*i);
   5816 				}
   5817 				printf(" %08x", fdp[i]);
   5818 			}
   5819 
   5820 			if (itd->itd_npacket > 4) {
   5821 				printf("\n...");
   5822 				i = (itd->itd_npacket - 2)*12 + 4;
   5823 			} else {
   5824 				i = 2*12 + 4;
   5825 			}
   5826 			for (;i < itd->itd_npacket*12 + 4; ++i) {
   5827 				if (i % 4 == 0) {
   5828 					printf("\n%x:", adr + 4*i);
   5829 				}
   5830 				printf(" %08x", fdp[i]);
   5831 			}
   5832 			printf("\n");
   5833 		}
   5834 #endif
   5835 	}
   5836 	{
   5837 		struct fwohci_desc *fd;
   5838 
   5839 		printf("fwohci_it_ctx_run: link start linkend end %d %d %d\n",
   5840 		    itc->itc_buf_start->itd_num,
   5841 		    itc->itc_buf_linkend->itd_num,
   5842 		    itc->itc_buf_end->itd_num);
   5843 
   5844 		fd = itc->itc_buf_start->itd_desc;
   5845 		if ((fd->fd_flags & 0xff00) != OHCI_DESC_STORE_VALUE) {
   5846 			printf("fwohci_it_ctx_run: start buf not with STORE\n");
   5847 		}
   5848 		fd += 3;
   5849 		if ((fd->fd_flags & OHCI_DESC_INTR_ALWAYS) == 0) {
   5850 			printf("fwohci_it_ctx_run: start buf does not have intr\n");
   5851 		}
   5852 
   5853 		fd = itc->itc_buf_linkend->itd_desc;
   5854 		if ((fd->fd_flags & 0xff00) != OHCI_DESC_STORE_VALUE) {
   5855 			printf("fwohci_it_ctx_run: linkend buf not with STORE\n");
   5856 		}
   5857 		fd += 3;
   5858 		if ((fd->fd_flags & OHCI_DESC_INTR_ALWAYS) == 0) {
   5859 			printf("fwohci_it_ctx_run: linkend buf does not have intr\n");
   5860 		}
   5861 	}
   5862 
   5863 	*itc->itc_scratch = 0xffffffff;
   5864 
   5865 	OHCI_SYNC_TX_DMA_WRITE(sc, ctx, OHCI_SUBREG_ContextControlClear,
   5866 	    0xffff0000);
   5867 	reg = OHCI_SYNC_TX_DMA_READ(sc, ctx, OHCI_SUBREG_ContextControlSet);
   5868 
   5869 	printf("fwohci_it_ctx_run start for ctx %d\n", ctx);
   5870 	printf("%s: bfr IT_CommandPtr 0x%08x ContextCtrl 0x%08x%s%s%s%s\n",
   5871 	    sc->sc_sc1394.sc1394_dev.dv_xname,
   5872 	    OHCI_SYNC_TX_DMA_READ(sc, ctx, OHCI_SUBREG_CommandPtr),
   5873 	    reg,
   5874 	    reg & OHCI_CTXCTL_RUN ? " run" : "",
   5875 	    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   5876 	    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   5877 	    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   5878 
   5879 	OHCI_SYNC_TX_DMA_WRITE(sc, ctx, OHCI_SUBREG_ContextControlClear,
   5880 	    OHCI_CTXCTL_RUN);
   5881 
   5882 	reg = OHCI_SYNC_TX_DMA_READ(sc, ctx, OHCI_SUBREG_ContextControlSet);
   5883 	i = 0;
   5884 	while (reg & (OHCI_CTXCTL_ACTIVE | OHCI_CTXCTL_RUN)) {
   5885 		delay(100);
   5886 		if (++i > 1000) {
   5887 			printf("%s: cannot stop iso transmit engine\n",
   5888 			    sc->sc_sc1394.sc1394_dev.dv_xname);
   5889 			break;
   5890 		}
   5891 		reg = OHCI_SYNC_TX_DMA_READ(sc, ctx,
   5892 		    OHCI_SUBREG_ContextControlSet);
   5893 	}
   5894 
   5895 	printf("%s: itm IT_CommandPtr 0x%08x ContextCtrl 0x%08x%s%s%s%s\n",
   5896 	    sc->sc_sc1394.sc1394_dev.dv_xname,
   5897 	    OHCI_SYNC_TX_DMA_READ(sc, ctx, OHCI_SUBREG_CommandPtr),
   5898 	    reg,
   5899 	    reg & OHCI_CTXCTL_RUN ? " run" : "",
   5900 	    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   5901 	    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   5902 	    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   5903 
   5904 	printf("%s: writing CommandPtr to 0x%08x\n",
   5905 	    sc->sc_sc1394.sc1394_dev.dv_xname,
   5906 	    (int)itc->itc_buf_start->itd_desc_phys);
   5907 	OHCI_SYNC_TX_DMA_WRITE(sc, ctx, OHCI_SUBREG_CommandPtr,
   5908 	    fwohci_itd_list_head(itc->itc_buf_start) | 4);
   5909 
   5910 	OHCI_SYNC_TX_DMA_WRITE(sc, ctx, OHCI_SUBREG_ContextControlSet,
   5911 	    OHCI_CTXCTL_RUN | OHCI_CTXCTL_WAKE);
   5912 
   5913 	reg = OHCI_SYNC_TX_DMA_READ(sc, ctx, OHCI_SUBREG_ContextControlSet);
   5914 
   5915 	printf("%s: aft IT_CommandPtr 0x%08x ContextCtrl 0x%08x%s%s%s%s\n",
   5916 	    sc->sc_sc1394.sc1394_dev.dv_xname,
   5917 	    OHCI_SYNC_TX_DMA_READ(sc, ctx, OHCI_SUBREG_CommandPtr),
   5918 	    reg,
   5919 	    reg & OHCI_CTXCTL_RUN ? " run" : "",
   5920 	    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   5921 	    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   5922 	    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   5923 }
   5924 
   5925 
   5926 
   5927 int
   5928 fwohci_it_ctx_flush(ieee1394_it_tag_t it)
   5929 {
   5930 	struct fwohci_it_ctx *itc = (struct fwohci_it_ctx *)it;
   5931 	int rv = 0;
   5932 
   5933 	if ((itc->itc_flags & ITC_FLAGS_RUN) == 0
   5934 	    && itc->itc_buf_cnt > 0) {
   5935 		printf("fwohci_it_ctx_flush: %s flushing\n",
   5936 		    itc->itc_sc->sc_sc1394.sc1394_dev.dv_xname);
   5937 
   5938 		fwohci_it_ctx_run(itc);
   5939 		rv = 1;
   5940 	}
   5941 
   5942 	return rv;
   5943 }
   5944 
   5945 
   5946 /*
   5947  * static void fwohci_it_intr(struct fwohci_softc *sc,
   5948  *			      struct fwochi_it_ctx *itc)
   5949  *
   5950  *	This function is the interrupt handler for isochronous
   5951  *	transmit interrupt.  This function will 1) unlink used
   5952  *	(already transmitted) buffers, 2) link new filled buffers, if
   5953  *	necessary and 3) say some free dma buffers exist to
   5954  *	fwiso_write()
   5955  */
   5956 static void
   5957 fwohci_it_intr(struct fwohci_softc *sc, struct fwohci_it_ctx *itc)
   5958 {
   5959 	struct fwohci_it_dmabuf *itd, *newstartbuf;
   5960 	u_int16_t scratchval;
   5961 	u_int32_t reg;
   5962 
   5963 	reg = OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num,
   5964 	    OHCI_SUBREG_ContextControlSet);
   5965 
   5966 	/* print out debug info */
   5967 #ifdef FW_DEBUG
   5968 	printf("fwohci_it_intr: CTX %d\n", itc->itc_num);
   5969 
   5970 	printf("fwohci_it_intr: %s: IT_CommandPtr 0x%08x "
   5971 	    "ContextCtrl 0x%08x%s%s%s%s\n",
   5972 	    sc->sc_sc1394.sc1394_dev.dv_xname,
   5973 	    OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num, OHCI_SUBREG_CommandPtr),
   5974 	    reg,
   5975 	    reg & OHCI_CTXCTL_RUN ? " run" : "",
   5976 	    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   5977 	    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   5978 	    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   5979 	printf("fwohci_it_intr: %s: scratch %x start %d end %d valid %d\n",
   5980 	    sc->sc_sc1394.sc1394_dev.dv_xname, *itc->itc_scratch,
   5981 	    itc->itc_buf_start->itd_num, itc->itc_buf_end->itd_num,
   5982 	    itc->itc_buf_cnt);
   5983 	{
   5984 		u_int32_t reg
   5985 		    = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   5986 		printf("\t\tIsoCounter 0x%08x, %d %d %d\n", reg,
   5987 		    (reg >> 25) & 0xfe, (reg >> 12) & 0x1fff, reg & 0xfff);
   5988 	}
   5989 #endif /* FW_DEBUG */
   5990 	/* end print out debug info */
   5991 
   5992 	scratchval = (*itc->itc_scratch) & 0x0000ffff;
   5993 	*itc->itc_scratch = 0xffffffff;
   5994 
   5995 	if ((reg & OHCI_CTXCTL_ACTIVE) == 0 && scratchval != 0xffff) {
   5996 		/* DMA engine has been stopped */
   5997 		printf("DMA engine stopped\n");
   5998 		printf("fwohci_it_intr: %s: IT_CommandPtr 0x%08x "
   5999 		    "ContextCtrl 0x%08x%s%s%s%s\n",
   6000 		    sc->sc_sc1394.sc1394_dev.dv_xname,
   6001 		    OHCI_SYNC_TX_DMA_READ(sc, itc->itc_num, OHCI_SUBREG_CommandPtr),
   6002 		    reg,
   6003 		    reg & OHCI_CTXCTL_RUN ? " run" : "",
   6004 		    reg & OHCI_CTXCTL_WAKE ? " wake" : "",
   6005 		    reg & OHCI_CTXCTL_DEAD ? " dead" : "",
   6006 		    reg & OHCI_CTXCTL_ACTIVE ? " active" : "");
   6007 		printf("fwohci_it_intr: %s: scratch %x start %d end %d valid %d\n",
   6008 		    sc->sc_sc1394.sc1394_dev.dv_xname, *itc->itc_scratch,
   6009 		    itc->itc_buf_start->itd_num, itc->itc_buf_end->itd_num,
   6010 		    itc->itc_buf_cnt);
   6011 		{
   6012 			u_int32_t reg
   6013 			    = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   6014 			printf("\t\tIsoCounter 0x%08x, %d %d %d\n", reg,
   6015 			    (reg >> 25) & 0xfe, (reg >> 12) & 0x1fff, reg & 0xfff);
   6016 		}
   6017 		printf("\t\tbranch of lastdesc 0x%08x\n",
   6018 		    itc->itc_buf_start->itd_lastdesc->fd_branch);
   6019 
   6020 		scratchval = 0xffff;
   6021 		itc->itc_flags &= ~ITC_FLAGS_RUN;
   6022 	}
   6023 
   6024 	/* unlink old buffers */
   6025 	if (scratchval != 0xffff) {
   6026 		/* normal path */
   6027 		newstartbuf = &itc->itc_buf[scratchval];
   6028 	} else {
   6029 		/* DMA engine stopped */
   6030 		newstartbuf = itc->itc_buf_linkend;
   6031 		INC_BUF(itc, newstartbuf);
   6032 	}
   6033 
   6034 	itd = (struct fwohci_it_dmabuf *)itc->itc_buf_start;
   6035 	itc->itc_buf_start = newstartbuf;
   6036 	while (itd != newstartbuf) {
   6037 		itc->itc_outpkt += itd->itd_npacket;
   6038 		fwohci_itd_unlink(itd);
   6039 		INC_BUF(itc, itd);
   6040 		--itc->itc_buf_cnt;
   6041 		DPRINTF(("<buf cnt %d>\n", itc->itc_buf_cnt));
   6042 	}
   6043 
   6044 #ifdef DEBUG_USERADD
   6045 	if (scratchval != 0xffff) {
   6046 		printf("fwohci0: intr start %d dataend %d %d\n", scratchval,
   6047 		    itc->itc_buf_end->itd_num, itc->itc_outpkt);
   6048 	}
   6049 #endif
   6050 
   6051 	if (scratchval == 0xffff) {
   6052 		/* no data supplied */
   6053 		printf("fwohci_it_intr: no it data.  output total %d\n",
   6054 		    itc->itc_outpkt);
   6055 
   6056 		if (itc->itc_buf_cnt > 0) {
   6057 			printf("fwohci_it_intr: it DMA stops "
   6058 			    "w/ valid databuf %d buf %d data %d"
   6059 			    " intr reg 0x%08x\n",
   6060 			    itc->itc_buf_cnt,
   6061 			    itc->itc_buf_end->itd_num,
   6062 			    fwohci_itd_hasdata(itc->itc_buf_end),
   6063 			    OHCI_CSR_READ(sc, OHCI_REG_IntEventSet));
   6064 		} else {
   6065 			/* All the data gone */
   6066 			itc->itc_buf_start
   6067 			    = itc->itc_buf_end
   6068 			    = itc->itc_buf_linkend
   6069 			    = &itc->itc_buf[0];
   6070 			printf("fwohci_it_intr: all packets gone\n");
   6071 		}
   6072 
   6073 		itc->itc_flags &= ~ITC_FLAGS_RUN;
   6074 
   6075 		OHCI_SYNC_TX_DMA_WRITE(sc, itc->itc_num,
   6076 		    OHCI_SUBREG_ContextControlClear, 0xffffffff);
   6077 		OHCI_SYNC_TX_DMA_WRITE(sc, itc->itc_num,
   6078 		    OHCI_SUBREG_CommandPtr, 0);
   6079 		OHCI_SYNC_TX_DMA_WRITE(sc, itc->itc_num,
   6080 		    OHCI_SUBREG_ContextControlClear, 0x1f);
   6081 
   6082 		/* send message */
   6083 		if (itc->itc_waitchan != NULL) {
   6084 			wakeup((void *)itc->itc_waitchan);
   6085 		}
   6086 
   6087 		return;
   6088 	}
   6089 
   6090 #if 0
   6091 	/* unlink old buffers */
   6092 	newstartbuf = &itc->itc_buf[scratchval];
   6093 
   6094 	itd = (struct fwohci_it_dmabuf *)itc->itc_buf_start;
   6095 	itc->itc_buf_start = newstartbuf;
   6096 	while (itd != newstartbuf) {
   6097 		itc->itc_outpkt += itd->itd_npacket;
   6098 		fwohci_itd_unlink(itd);
   6099 		INC_BUF(itc, itd);
   6100 		--itc->itc_buf_cnt;
   6101 		DPRINTF(("<buf cnt %d>\n", itc->itc_buf_cnt));
   6102 	}
   6103 #endif
   6104 
   6105 	/* sanity check */
   6106 	{
   6107 		int startidx, endidx, linkendidx;
   6108 
   6109 		startidx = itc->itc_buf_start->itd_num;
   6110 		endidx = itc->itc_buf_end->itd_num;
   6111 		linkendidx = itc->itc_buf_linkend->itd_num;
   6112 
   6113 		if (startidx < endidx) {
   6114 			if (linkendidx < startidx
   6115 			    || endidx < linkendidx) {
   6116 				printf("funny, linkend is not between start "
   6117 				    "and end [%d, %d]: %d\n",
   6118 				    startidx, endidx, linkendidx);
   6119 			}
   6120 		} else if (startidx > endidx) {
   6121 			if (linkendidx < startidx
   6122 			    && endidx < linkendidx) {
   6123 				printf("funny, linkend is not between start "
   6124 				    "and end [%d, %d]: %d\n",
   6125 				    startidx, endidx, linkendidx);
   6126 			}
   6127 		} else {
   6128 			if (linkendidx != startidx) {
   6129 				printf("funny, linkend is not between start "
   6130 				    "and end [%d, %d]: %d\n",
   6131 				    startidx, endidx, linkendidx);
   6132 			}
   6133 
   6134 		}
   6135 	}
   6136 
   6137 	/* link if some valid DMA buffers exist */
   6138 	if (itc->itc_buf_cnt > 1
   6139 	    && itc->itc_buf_linkend != itc->itc_buf_end) {
   6140 		struct fwohci_it_dmabuf *itdprev;
   6141 		int i;
   6142 
   6143 		DPRINTF(("CTX %d: start linkend dataend bufs %d, %d, %d, %d\n",
   6144 		    itc->itc_num,
   6145 		    itc->itc_buf_start->itd_num,
   6146 		    itc->itc_buf_linkend->itd_num,
   6147 		    itc->itc_buf_end->itd_num,
   6148 		    itc->itc_buf_cnt));
   6149 
   6150 		itd = itdprev = itc->itc_buf_linkend;
   6151 		INC_BUF(itc, itd);
   6152 
   6153 #if 0
   6154 		if (fwohci_itd_isfilled(itd) || itc->itc_buf_cnt == 2) {
   6155 			while (itdprev != itc->itc_buf_end) {
   6156 
   6157 				if (fwohci_itd_link(itdprev, itd)) {
   6158 					break;
   6159 				}
   6160 
   6161 				itdprev = itd;
   6162 				INC_BUF(itc, itd);
   6163 			}
   6164 			itc->itc_buf_linkend = itdprev;
   6165 		}
   6166 #endif
   6167 		i = 0;
   6168 		while (itdprev != itc->itc_buf_end) {
   6169 			if (!fwohci_itd_isfilled(itd) && itc->itc_buf_cnt > 2) {
   6170 				break;
   6171 			}
   6172 
   6173 			if (fwohci_itd_link(itdprev, itd)) {
   6174 				break;
   6175 			}
   6176 
   6177 			itdprev = itd;
   6178 			INC_BUF(itc, itd);
   6179 
   6180 			itc->itc_buf_linkend = itdprev;
   6181 			++i;
   6182 		}
   6183 
   6184 		if (i > 0) {
   6185 			DPRINTF(("CTX %d: start linkend dataend bufs %d, %d, %d, %d\n",
   6186 			    itc->itc_num,
   6187 			    itc->itc_buf_start->itd_num,
   6188 			    itc->itc_buf_linkend->itd_num,
   6189 			    itc->itc_buf_end->itd_num,
   6190 			    itc->itc_buf_cnt));
   6191 		}
   6192 	} else {
   6193 		struct fwohci_it_dmabuf *le;
   6194 
   6195 		le = itc->itc_buf_linkend;
   6196 
   6197 		printf("CTX %d: start linkend dataend bufs %d, %d, %d, %d no buffer added\n",
   6198 			    itc->itc_num,
   6199 			    itc->itc_buf_start->itd_num,
   6200 			    itc->itc_buf_linkend->itd_num,
   6201 			    itc->itc_buf_end->itd_num,
   6202 			    itc->itc_buf_cnt);
   6203 		printf("\tlast descriptor %s %04x %08x\n",
   6204 		    le->itd_lastdesc->fd_flags & OHCI_DESC_INTR_ALWAYS ? "intr" : "",
   6205 		    le->itd_lastdesc->fd_flags,
   6206 		    le->itd_lastdesc->fd_branch);
   6207 	}
   6208 
   6209 	/* send message */
   6210 	if (itc->itc_waitchan != NULL) {
   6211 		/*  */
   6212 		wakeup((void *)itc->itc_waitchan);
   6213 	}
   6214 }
   6215 
   6216 
   6217 
   6218 /*
   6219  * int fwohci_itd_construct(struct fwohci_it_ctx *itc,
   6220  *			    struct fwohci_it_dmabuf *itd, int num,
   6221  *			    struct fwohci_desc *desc, bus_addr_t phys,
   6222  *			    int descsize, int maxsize, paddr_t scratch)
   6223  *
   6224  *
   6225  *
   6226  */
   6227 int
   6228 fwohci_itd_construct(struct fwohci_it_ctx *itc, struct fwohci_it_dmabuf *itd,
   6229     int num, struct fwohci_desc *desc, bus_addr_t phys, int descsize,
   6230     int maxsize, paddr_t scratch)
   6231 {
   6232 	const char *xname = itc->itc_sc->sc_sc1394.sc1394_dev.dv_xname;
   6233 	struct fwohci_desc *fd;
   6234 	struct fwohci_desc *descend;
   6235 	int npkt;
   6236 	int bufno = 0;		/* DMA segment */
   6237 	bus_size_t bufused = 0;	/* offset in a DMA segment */
   6238 	int roundsize;
   6239 	int tag = itc->itc_tag;
   6240 	int ch = itc->itc_channel;
   6241 
   6242 	itd->itd_ctx = itc;
   6243 	itd->itd_num = num;
   6244 
   6245 	if (descsize > 1024*3) {
   6246 		printf("%s: fwohci_itd_construct[%d] descsize %d too big\n",
   6247 		    xname, num, descsize);
   6248 		return -1;
   6249 	}
   6250 
   6251 	itd->itd_desc = desc;
   6252 	itd->itd_descsize = descsize;
   6253 	itd->itd_desc_phys = phys;
   6254 
   6255 	itd->itd_lastdesc = desc;
   6256 	itd->itd_npacket = 0;
   6257 
   6258 	printf("%s: fwohci_itd_construct[%d] desc %p descsize %d, maxsize %d\n",
   6259 	    xname, itd->itd_num, itd->itd_desc, itd->itd_descsize, maxsize);
   6260 
   6261 	if (descsize < 4) {
   6262 		/* too small descriptor array.  at least 4 */
   6263 		return -1;
   6264 	}
   6265 
   6266 	/* count up how many packet can handle */
   6267 	itd->itd_maxpacket = (descsize - 1)/3;
   6268 
   6269 	/* rounding up to power of 2. minimum 16 */
   6270 	roundsize = 16;
   6271 	for (roundsize = 16; roundsize < maxsize; roundsize <<= 1);
   6272 	itd->itd_maxsize = roundsize;
   6273 
   6274 	printf("\t\tdesc%d [%x, %x]\n", itd->itd_num,
   6275 	    (u_int32_t)phys,
   6276 	    (u_int32_t)phys
   6277 	    + (itd->itd_maxpacket*3 + 1)*sizeof(struct fwohci_desc));
   6278 	printf("%s: fwohci_itd_construct[%d] npkt %d maxsize round up to %d\n",
   6279 	    xname, itd->itd_num, itd->itd_maxpacket, itd->itd_maxsize);
   6280 
   6281 	/* obtain DMA buffer */
   6282 	if (fwohci_itd_dmabuf_alloc(itd)) {
   6283 		/* cannot allocate memory for DMA buffer */
   6284 		return -1;
   6285 	}
   6286 
   6287 	/*
   6288 	 * make descriptor chain
   6289 	 *
   6290 	 * First descriptor group has a STORE_VALUE, OUTPUT_IMMEDIATE
   6291 	 * and OUTPUT_LAST descriptors Second and after that, a
   6292 	 * descriptor group has an OUTPUT_IMMEDIATE and an OUTPUT_LAST
   6293 	 * descriptor.
   6294 	 */
   6295 	descend = desc + descsize;
   6296 
   6297 	/* set store value descriptor for 1st descriptor group */
   6298 	desc->fd_flags = OHCI_DESC_STORE_VALUE;
   6299 	desc->fd_reqcount = num; /* write number of DMA buffer class */
   6300 	desc->fd_data = scratch; /* at physical memory 'scratch' */
   6301 	desc->fd_branch = 0;
   6302 	desc->fd_status = desc->fd_rescount = 0;
   6303 
   6304 	itd->itd_store = desc;
   6305 	itd->itd_store_phys = phys;
   6306 
   6307 	++desc;
   6308 	phys += 16;
   6309 
   6310 	npkt = 0;
   6311 	/* make OUTPUT_DESC chain for packets */
   6312 	for (fd = desc; fd + 2 < descend; fd += 3, ++npkt) {
   6313 		struct fwohci_desc *fi = fd;
   6314 		struct fwohci_desc *fl = fd + 2;
   6315 		u_int32_t *fi_data = (u_int32_t *)(fd + 1);
   6316 
   6317 #if 0
   6318 		if (npkt > itd->itd_maxpacket - 3) {
   6319 			printf("%s: %3d fi fl %p %p\n", xname, npkt, fi,fl);
   6320 		}
   6321 #endif
   6322 
   6323 		fi->fd_reqcount = 8; /* data size for OHCI command */
   6324 		fi->fd_flags = OHCI_DESC_IMMED;
   6325 		fi->fd_data = 0;
   6326 		fi->fd_branch = 0; /* branch for error */
   6327 		fi->fd_status = fi->fd_rescount = 0;
   6328 
   6329 		/* channel and tag is unchanged */
   6330 		*fi_data = OHCI_ITHEADER_VAL(TAG, tag) |
   6331 		    OHCI_ITHEADER_VAL(CHAN, ch) |
   6332 		    OHCI_ITHEADER_VAL(TCODE, IEEE1394_TCODE_STREAM_DATA);
   6333 		*++fi_data = 0;
   6334 		*++fi_data = 0;
   6335 		*++fi_data = 0;
   6336 
   6337 		fl->fd_flags = OHCI_DESC_OUTPUT | OHCI_DESC_LAST |
   6338 		    OHCI_DESC_BRANCH;
   6339 		fl->fd_branch =
   6340 		    (phys + sizeof(struct fwohci_desc)*(npkt + 1)*3) | 0x03;
   6341 		fl->fd_status = fl->fd_rescount = 0;
   6342 
   6343 #ifdef FW_DEBUG
   6344 		if (npkt > itd->itd_maxpacket - 3) {
   6345 			DPRINTF(("%s: %3d fi fl fl branch %p %p 0x%x\n",
   6346 			    xname, npkt, fi, fl, (int)fl->fd_branch));
   6347 		}
   6348 #endif
   6349 
   6350 		/* physical addr to data? */
   6351 		fl->fd_data =
   6352 		    (u_int32_t)((itd->itd_seg[bufno].ds_addr + bufused));
   6353 		bufused += itd->itd_maxsize;
   6354 		if (bufused > itd->itd_seg[bufno].ds_len) {
   6355 			bufused = 0;
   6356 			if (++bufno == itd->itd_nsegs) {
   6357 				/* fail */
   6358 				break;
   6359 			}
   6360 		}
   6361 	}
   6362 
   6363 #if 0
   6364 	if (itd->itd_num == 0) {
   6365 		u_int32_t *fdp;
   6366 		u_int32_t adr;
   6367 		int i = 0;
   6368 
   6369 		fdp = (u_int32_t *)itd->itd_desc;
   6370 		adr = (u_int32_t)itd->itd_desc_phys; /* XXX */
   6371 
   6372 		printf("fwohci_itd_construct: audit DMA desc chain. %d\n",
   6373 		    itd->itd_maxpacket);
   6374 		for (i = 0; i < itd->itd_maxpacket*12 + 4; ++i) {
   6375 			if (i % 4 == 0) {
   6376 				printf("\n%x:", adr + 4*i);
   6377 			}
   6378 			printf(" %08x", fdp[i]);
   6379 		}
   6380 		printf("\n");
   6381 
   6382 	}
   6383 #endif
   6384 	/* last branch should be 0 */
   6385 	--fd;
   6386 	fd->fd_branch = 0;
   6387 
   6388 	printf("%s: pkt %d %d maxdesc %p\n",
   6389 	    xname, npkt, itd->itd_maxpacket, descend);
   6390 
   6391 	return 0;
   6392 }
   6393 
   6394 void
   6395 fwohci_itd_destruct(struct fwohci_it_dmabuf *itd)
   6396 {
   6397 	const char *xname = itd->itd_ctx->itc_sc->sc_sc1394.sc1394_dev.dv_xname;
   6398 
   6399 	printf("%s: fwohci_itd_destruct %d\n", xname, itd->itd_num);
   6400 
   6401 	fwohci_itd_dmabuf_free(itd);
   6402 }
   6403 
   6404 
   6405 /*
   6406  * static int fwohci_itd_dmabuf_alloc(struct fwohci_it_dmabuf *itd)
   6407  *
   6408  *	This function allocates DMA memory for fwohci_it_dmabuf.  This
   6409  *	function will return 0 when it succeeds and return non-zero
   6410  *	value when it fails.
   6411  */
   6412 static int
   6413 fwohci_itd_dmabuf_alloc(struct fwohci_it_dmabuf *itd)
   6414 {
   6415 	const char *xname = itd->itd_ctx->itc_sc->sc_sc1394.sc1394_dev.dv_xname;
   6416 	bus_dma_tag_t dmat = itd->itd_ctx->itc_sc->sc_dmat;
   6417 
   6418 	int dmasize = itd->itd_maxsize * itd->itd_maxpacket;
   6419 	int error;
   6420 
   6421 	DPRINTF(("%s: fwohci_itd_dmabuf_alloc[%d] dmasize %d maxpkt %d\n",
   6422 	    xname, itd->itd_num, dmasize, itd->itd_maxpacket));
   6423 
   6424 	if ((error = bus_dmamem_alloc(dmat, dmasize, PAGE_SIZE, 0,
   6425 	    itd->itd_seg, FWOHCI_MAX_ITDATASEG, &itd->itd_nsegs, 0)) != 0) {
   6426 		printf("%s: unable to allocate data buffer, error = %d\n",
   6427 		    xname, error);
   6428 		goto fail_0;
   6429 	}
   6430 
   6431 	/* checking memory range */
   6432 #ifdef FW_DEBUG
   6433 	{
   6434 		int loop;
   6435 
   6436 		for (loop = 0; loop < itd->itd_nsegs; ++loop) {
   6437 			DPRINTF(("\t%.2d: 0x%lx - 0x%lx\n", loop,
   6438 			    (long)itd->itd_seg[loop].ds_addr,
   6439 			    (long)itd->itd_seg[loop].ds_addr
   6440 			    + (long)itd->itd_seg[loop].ds_len - 1));
   6441 		}
   6442 	}
   6443 #endif
   6444 
   6445 	if ((error = bus_dmamem_map(dmat, itd->itd_seg, itd->itd_nsegs,
   6446 	    dmasize, (caddr_t *)&itd->itd_buf,
   6447 	    BUS_DMA_COHERENT | BUS_DMA_WAITOK)) != 0) {
   6448 		printf("%s: unable to map data buffer, error = %d\n",
   6449 		    xname, error);
   6450 		goto fail_1;
   6451 	}
   6452 
   6453 	DPRINTF(("fwohci_it_data_alloc[%d]: bus_dmamem_map addr %p\n",
   6454 	    itd->itd_num, itd->itd_buf));
   6455 
   6456 	if ((error = bus_dmamap_create(dmat, /*chunklen*/dmasize,
   6457 	    itd->itd_nsegs, dmasize, 0, BUS_DMA_WAITOK,
   6458 	    &itd->itd_dmamap)) != 0) {
   6459 		printf("%s: unable to create data buffer DMA map, "
   6460 		    "error = %d\n", xname, error);
   6461 		goto fail_2;
   6462 	}
   6463 
   6464 	DPRINTF(("fwohci_it_data_alloc: bus_dmamem_create\n"));
   6465 
   6466 	if ((error = bus_dmamap_load(dmat, itd->itd_dmamap,
   6467 	    itd->itd_buf, dmasize, NULL, BUS_DMA_WAITOK)) != 0) {
   6468 		printf("%s: unable to load data buffer DMA map, error = %d\n",
   6469 		    xname, error);
   6470 		goto fail_3;
   6471 	}
   6472 
   6473 	DPRINTF(("fwohci_itd_dmabuf_alloc: load DMA memory vm %p\n",
   6474 	    itd->itd_buf));
   6475 	DPRINTF(("\tmapsize %ld nsegs %d\n",
   6476 	    (long)itd->itd_dmamap->dm_mapsize, itd->itd_dmamap->dm_nsegs));
   6477 
   6478 #ifdef FW_DEBUG
   6479 	{
   6480 		int loop;
   6481 
   6482 		for (loop = 0; loop < itd->itd_dmamap->dm_nsegs; ++loop) {
   6483 			DPRINTF(("\t%.2d: 0x%lx - 0x%lx\n", loop,
   6484 			    (long)itd->itd_dmamap->dm_segs[loop].ds_addr,
   6485 			    (long)itd->itd_dmamap->dm_segs[loop].ds_addr +
   6486 			    (long)itd->itd_dmamap->dm_segs[loop].ds_len - 1));
   6487 		}
   6488 	}
   6489 #endif
   6490 
   6491 	return 0;
   6492 
   6493   fail_3:
   6494 	bus_dmamap_destroy(dmat, itd->itd_dmamap);
   6495   fail_2:
   6496 	bus_dmamem_unmap(dmat, (caddr_t)itd->itd_buf, dmasize);
   6497   fail_1:
   6498 	bus_dmamem_free(dmat, itd->itd_seg, itd->itd_nsegs);
   6499   fail_0:
   6500 	itd->itd_nsegs = 0;
   6501 	itd->itd_maxpacket = 0;
   6502 	return error;
   6503 }
   6504 
   6505 /*
   6506  * static void fwohci_itd_dmabuf_free(struct fwohci_it_dmabuf *itd)
   6507  *
   6508  *	This function will release memory resource allocated by
   6509  *	fwohci_itd_dmabuf_alloc().
   6510  */
   6511 static void
   6512 fwohci_itd_dmabuf_free(struct fwohci_it_dmabuf *itd)
   6513 {
   6514 	bus_dma_tag_t dmat = itd->itd_ctx->itc_sc->sc_dmat;
   6515 	int dmasize = itd->itd_maxsize * itd->itd_maxpacket;
   6516 
   6517 	bus_dmamap_destroy(dmat, itd->itd_dmamap);
   6518 	bus_dmamem_unmap(dmat, (caddr_t)itd->itd_buf, dmasize);
   6519 	bus_dmamem_free(dmat, itd->itd_seg, itd->itd_nsegs);
   6520 
   6521 	itd->itd_nsegs = 0;
   6522 	itd->itd_maxpacket = 0;
   6523 }
   6524 
   6525 
   6526 
   6527 /*
   6528  * int fwohci_itd_link(struct fwohci_it_dmabuf *itd,
   6529  *		struct fwohci_it_dmabuf *itdc)
   6530  *
   6531  *	This function will concatinate two descriptor chains in dmabuf
   6532  *	itd and itdc.  The descriptor link in itdc follows one in itd.
   6533  *	This function will move interrrupt packet from the end of itd
   6534  *	to the top of itdc.
   6535  *
   6536  *	This function will return 0 whel this funcion suceeds.  If an
   6537  *	error happens, return a negative value.
   6538  */
   6539 int
   6540 fwohci_itd_link(struct fwohci_it_dmabuf *itd, struct fwohci_it_dmabuf *itdc)
   6541 {
   6542 	struct fwohci_desc *fd1, *fdc;
   6543 
   6544 	if (itdc->itd_lastdesc == itdc->itd_desc) {
   6545 		/* no valid data */
   6546 		printf("fwohci_itd_link: no data\n");
   6547 		return -1;
   6548 	}
   6549 
   6550 	if (itdc->itd_flags & ITD_FLAGS_LOCK) {
   6551 		/* used already */
   6552 		printf("fwohci_itd_link: link locked\n");
   6553 		return -1;
   6554 	}
   6555 	itdc->itd_flags |= ITD_FLAGS_LOCK;
   6556 	/* for the first one */
   6557 	itd->itd_flags |= ITD_FLAGS_LOCK;
   6558 
   6559 	DPRINTF(("linking %d after %d: add %d pkts\n",
   6560 	    itdc->itd_num, itd->itd_num, itdc->itd_npacket));
   6561 
   6562 	/* XXX: should sync cache */
   6563 
   6564 	fd1 = itd->itd_lastdesc;
   6565 	fdc = itdc->itd_desc + 3; /* OUTPUT_LAST in the first descriptor */
   6566 
   6567 	/* sanity check */
   6568 #define OUTPUT_LAST_DESC (OHCI_DESC_OUTPUT | OHCI_DESC_LAST | OHCI_DESC_BRANCH)
   6569 	if ((fd1->fd_flags & OUTPUT_LAST_DESC) != OUTPUT_LAST_DESC) {
   6570 		printf("funny! not OUTPUT_LAST descriptor %p\n", fd1);
   6571 	}
   6572 	if (itd->itd_lastdesc - itd->itd_desc != 3 * itd->itd_npacket) {
   6573 		printf("funny! packet number inconsistency %d <=> %d\n",
   6574 		    itd->itd_lastdesc - itd->itd_desc, 3*itd->itd_npacket);
   6575 	}
   6576 
   6577 	fd1->fd_flags &= ~OHCI_DESC_INTR_ALWAYS;
   6578 	fdc->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   6579 	fd1->fd_branch = itdc->itd_desc_phys | 4;
   6580 
   6581 	itdc->itd_lastdesc->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   6582 	/* save branch addr of lastdesc and substitute 0 to it */
   6583 	itdc->itd_savedbranch = itdc->itd_lastdesc->fd_branch;
   6584 	itdc->itd_lastdesc->fd_branch = 0;
   6585 
   6586 	DPRINTF(("%s: link (%d %d), add pkt %d/%d branch 0x%x next saved 0x%x\n",
   6587 	    itd->itd_ctx->itc_sc->sc_sc1394.sc1394_dev.dv_xname,
   6588 	    itd->itd_num, itdc->itd_num,
   6589 	    itdc->itd_npacket, itdc->itd_maxpacket,
   6590 	    (int)fd1->fd_branch, (int)itdc->itd_savedbranch));
   6591 
   6592 	/* XXX: should sync cache */
   6593 
   6594 	return 0;
   6595 }
   6596 
   6597 
   6598 /*
   6599  * int fwohci_itd_unlink(struct fwohci_it_dmabuf *itd)
   6600  *
   6601  *	This function will unlink the descriptor chain from valid link
   6602  *	of descriptors.  The target descriptor is specified by the
   6603  *	arguent.
   6604  */
   6605 int
   6606 fwohci_itd_unlink(struct fwohci_it_dmabuf *itd)
   6607 {
   6608 	struct fwohci_desc *fd;
   6609 
   6610 	/* XXX: should sync cache */
   6611 
   6612 	fd = itd->itd_lastdesc;
   6613 
   6614 	fd->fd_branch = itd->itd_savedbranch;
   6615 	DPRINTF(("%s: unlink buf %d branch restored 0x%x\n",
   6616 	    itd->itd_ctx->itc_sc->sc_sc1394.sc1394_dev.dv_xname,
   6617 	    itd->itd_num, (int)fd->fd_branch));
   6618 
   6619 	fd->fd_flags &= ~OHCI_DESC_INTR_ALWAYS;
   6620 	itd->itd_lastdesc = itd->itd_desc;
   6621 
   6622 	fd = itd->itd_desc + 3;	/* 1st OUTPUT_LAST */
   6623 	fd->fd_flags &= ~OHCI_DESC_INTR_ALWAYS;
   6624 
   6625 	/* XXX: should sync cache */
   6626 
   6627 	itd->itd_npacket = 0;
   6628 	itd->itd_lastdesc = itd->itd_desc;
   6629 	itd->itd_flags &= ~ITD_FLAGS_LOCK;
   6630 
   6631 	return 0;
   6632 }
   6633 
   6634 
   6635 /*
   6636  * static int fwohci_itd_writedata(struct fwohci_it_dmabuf *, int ndata,
   6637  *			struct ieee1394_it_datalist *);
   6638  *
   6639  *	This function will return the number of written data, or
   6640  *	negative value if an error happens
   6641  */
   6642 int
   6643 fwohci_itd_writedata(struct fwohci_it_dmabuf *itd, int ndata,
   6644     struct ieee1394_it_datalist *itdata)
   6645 {
   6646 	int writepkt;
   6647 	int i;
   6648 	u_int8_t *p;
   6649 	struct fwohci_desc *fd;
   6650 	u_int32_t *fd_idata;
   6651 	const int dspace =
   6652 	    itd->itd_maxpacket - itd->itd_npacket < ndata ?
   6653 	    itd->itd_maxpacket - itd->itd_npacket : ndata;
   6654 
   6655 	if (itd->itd_flags & ITD_FLAGS_LOCK || dspace == 0) {
   6656 		/* it is locked: cannot write anything */
   6657 		if (itd->itd_flags & ITD_FLAGS_LOCK) {
   6658 			DPRINTF(("fwohci_itd_writedata: buf %d lock flag %s,"
   6659 			    " dspace %d\n",
   6660 			    itd->itd_num,
   6661 			    itd->itd_flags & ITD_FLAGS_LOCK ? "ON" : "OFF",
   6662 			    dspace));
   6663 			return 0;	/* not an error */
   6664 		}
   6665 	}
   6666 
   6667 	/* sanity check */
   6668 	if (itd->itd_maxpacket < itd->itd_npacket) {
   6669 		printf("fwohci_itd_writedata: funny! # pkt > maxpkt"
   6670 			"%d %d\n", itd->itd_npacket, itd->itd_maxpacket);
   6671 	}
   6672 
   6673 	p = itd->itd_buf + itd->itd_maxsize * itd->itd_npacket;
   6674 	fd = itd->itd_lastdesc;
   6675 
   6676 	DPRINTF(("fwohci_itd_writedata(%d[%p], %d, 0x%p) invoked:\n",
   6677 	    itd->itd_num, itd, ndata, itdata));
   6678 
   6679 	for (writepkt = 0; writepkt < dspace; ++writepkt) {
   6680 		u_int8_t *p1 = p;
   6681 		int cpysize;
   6682 		int totalsize = 0;
   6683 
   6684 		DPRINTF(("writing %d ", writepkt));
   6685 
   6686 		for (i = 0; i < 4; ++i) {
   6687 			switch (itdata->it_cmd[i]&IEEE1394_IT_CMD_MASK) {
   6688 			case IEEE1394_IT_CMD_IMMED:
   6689 				memcpy(p1, &itdata->it_u[i].id_data, 8);
   6690 				p1 += 8;
   6691 				totalsize += 8;
   6692 				break;
   6693 			case IEEE1394_IT_CMD_PTR:
   6694 				cpysize = itdata->it_cmd[i]&IEEE1394_IT_CMD_SIZE;
   6695 				DPRINTF(("fwohci_itd_writedata: cpy %d %p\n",
   6696 				    cpysize, itdata->it_u[i].id_addr));
   6697 				if (totalsize + cpysize > itd->itd_maxsize) {
   6698 					/* error: too big size */
   6699 					break;
   6700 				}
   6701 				memcpy(p1, itdata->it_u[i].id_addr, cpysize);
   6702 				totalsize += cpysize;
   6703 				break;
   6704 			case IEEE1394_IT_CMD_NOP:
   6705 				break;
   6706 			default:
   6707 				/* unknown command */
   6708 				break;
   6709 			}
   6710 		}
   6711 
   6712 		/* only for DV test */
   6713 		if (totalsize != 488) {
   6714 			printf("error: totalsize %d at %d\n",
   6715 			    totalsize, writepkt);
   6716 		}
   6717 
   6718 		DPRINTF(("totalsize %d ", totalsize));
   6719 
   6720 		/* fill iso command in OUTPUT_IMMED descriptor */
   6721 
   6722 		/* XXX: sync cache */
   6723 		fd += 2;	/* next to first descriptor */
   6724 		fd_idata = (u_int32_t *)fd;
   6725 
   6726 		/*
   6727 		 * Umm, should tag, channel and tcode be written
   6728 		 * previously in itd_construct?
   6729 		 */
   6730 #if 0
   6731 		*fd_idata = OHCI_ITHEADER_VAL(TAG, tag) |
   6732 		    OHCI_ITHEADER_VAL(CHAN, ch) |
   6733 		    OHCI_ITHEADER_VAL(TCODE, IEEE1394_TCODE_STREAM_DATA);
   6734 #endif
   6735 		*++fd_idata = totalsize << 16;
   6736 
   6737 		/* fill data in OUTPUT_LAST descriptor */
   6738 		++fd;
   6739 		/* intr check... */
   6740 		if (fd->fd_flags & OHCI_DESC_INTR_ALWAYS) {
   6741 			printf("uncleared INTR flag in desc %d\n",
   6742 			    (fd - itd->itd_desc - 1)/3);
   6743 		}
   6744 		fd->fd_flags &= ~OHCI_DESC_INTR_ALWAYS;
   6745 
   6746 		if ((fd - itd->itd_desc - 1)/3 != itd->itd_maxpacket - 1) {
   6747 			u_int32_t bcal;
   6748 
   6749 			bcal = (fd - itd->itd_desc + 1)*sizeof(struct fwohci_desc) + (u_int32_t)itd->itd_desc_phys;
   6750 			if (bcal != (fd->fd_branch & 0xfffffff0)) {
   6751 
   6752 				printf("uum, branch differ at %d, %x %x %d/%d\n",
   6753 				    itd->itd_num,
   6754 				    bcal,
   6755 				    fd->fd_branch,
   6756 				    (fd - itd->itd_desc - 1)/3,
   6757 				    itd->itd_maxpacket);
   6758 			}
   6759 		} else {
   6760 			/* the last pcaket */
   6761 			if (fd->fd_branch != 0) {
   6762 				printf("uum, branch differ at %d, %x %x %d/%d\n",
   6763 				    itd->itd_num,
   6764 				    0,
   6765 				    fd->fd_branch,
   6766 				    (fd - itd->itd_desc - 1)/3,
   6767 				    itd->itd_maxpacket);
   6768 			}
   6769 		}
   6770 
   6771 		/* sanity check */
   6772 		if (fd->fd_flags != OUTPUT_LAST_DESC) {
   6773 			printf("fwohci_itd_writedata: dmabuf %d desc inconsistent %d\n",
   6774 			    itd->itd_num, writepkt + itd->itd_npacket);
   6775 			break;
   6776 		}
   6777 		fd->fd_reqcount = totalsize;
   6778 		/* XXX: sync cache */
   6779 
   6780 		++itdata;
   6781 		p += itd->itd_maxsize;
   6782 	}
   6783 
   6784 	DPRINTF(("loop start %d, %d times %d\n",
   6785 	    itd->itd_npacket, dspace, writepkt));
   6786 
   6787 	itd->itd_npacket += writepkt;
   6788 	itd->itd_lastdesc = fd;
   6789 
   6790 	return writepkt;
   6791 }
   6792 
   6793 
   6794 
   6795 
   6796 
   6797 int
   6798 fwohci_itd_isfilled(struct fwohci_it_dmabuf *itd)
   6799 {
   6800 
   6801 	return itd->itd_npacket*2 > itd->itd_maxpacket ? 1 : 0;
   6802 }
   6803