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