Home | History | Annotate | Line # | Download | only in ieee1394
fwohci.c revision 1.24
      1  1.24       jmc /*	$NetBSD: fwohci.c,v 1.24 2001/05/01 04:48:11 jmc Exp $	*/
      2  1.14     enami 
      3   1.1      matt /*-
      4   1.1      matt  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.1      matt  * All rights reserved.
      6   1.1      matt  *
      7   1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9   1.1      matt  *
     10   1.1      matt  * Redistribution and use in source and binary forms, with or without
     11   1.1      matt  * modification, are permitted provided that the following conditions
     12   1.1      matt  * are met:
     13   1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14   1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15   1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      matt  *    documentation and/or other materials provided with the distribution.
     18   1.1      matt  * 3. All advertising materials mentioning features or use of this software
     19   1.1      matt  *    must display the following acknowledgement:
     20   1.1      matt  *        This product includes software developed by the NetBSD
     21   1.1      matt  *        Foundation, Inc. and its contributors.
     22   1.1      matt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      matt  *    contributors may be used to endorse or promote products derived
     24   1.1      matt  *    from this software without specific prior written permission.
     25   1.1      matt  *
     26   1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      matt  */
     38   1.1      matt 
     39   1.3      onoe /*
     40   1.3      onoe  * IEEE1394 Open Host Controller Interface
     41   1.3      onoe  *	based on OHCI Specification 1.1 (January 6, 2000)
     42   1.3      onoe  * The first version to support network interface part is wrtten by
     43   1.3      onoe  * Atsushi Onoe <onoe (at) netbsd.org>.
     44   1.3      onoe  */
     45   1.3      onoe 
     46   1.3      onoe #include "opt_inet.h"
     47   1.3      onoe 
     48   1.1      matt #include <sys/param.h>
     49   1.2  augustss #include <sys/systm.h>
     50  1.24       jmc #include <sys/kthread.h>
     51   1.1      matt #include <sys/types.h>
     52   1.1      matt #include <sys/socket.h>
     53   1.7      onoe #include <sys/callout.h>
     54   1.1      matt #include <sys/device.h>
     55   1.7      onoe #include <sys/kernel.h>
     56   1.3      onoe #include <sys/malloc.h>
     57   1.3      onoe #include <sys/mbuf.h>
     58   1.1      matt 
     59   1.7      onoe #if __NetBSD_Version__ >= 105010000
     60   1.7      onoe #include <uvm/uvm_extern.h>
     61   1.7      onoe #else
     62   1.7      onoe #include <vm/vm.h>
     63   1.7      onoe #endif
     64   1.7      onoe 
     65   1.1      matt #include <machine/bus.h>
     66  1.24       jmc #include <machine/intr.h>
     67   1.1      matt 
     68   1.1      matt #include <dev/ieee1394/ieee1394reg.h>
     69   1.1      matt #include <dev/ieee1394/fwohcireg.h>
     70   1.1      matt 
     71   1.1      matt #include <dev/ieee1394/ieee1394var.h>
     72   1.1      matt #include <dev/ieee1394/fwohcivar.h>
     73   1.1      matt 
     74   1.1      matt static const char * const ieee1394_speeds[] = { IEEE1394_SPD_STRINGS };
     75   1.1      matt 
     76   1.5      matt #if 0
     77   1.5      matt static int fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
     78   1.5      matt 			       bus_dmamap_t *mapp, caddr_t *kvap, int flags);
     79   1.5      matt #endif
     80  1.24       jmc static void fwohci_create_event_thread(void *);
     81  1.24       jmc static void fwohci_thread_init(void *);
     82  1.24       jmc 
     83  1.24       jmc static void fwohci_event_thread(struct fwohci_softc *);
     84   1.7      onoe static void fwohci_hw_init(struct fwohci_softc *);
     85   1.7      onoe static void fwohci_power(int, void *);
     86   1.7      onoe static void fwohci_shutdown(void *);
     87   1.5      matt 
     88   1.3      onoe static int  fwohci_desc_alloc(struct fwohci_softc *);
     89   1.9      onoe static struct fwohci_desc *fwohci_desc_get(struct fwohci_softc *, int);
     90   1.9      onoe static void fwohci_desc_put(struct fwohci_softc *, struct fwohci_desc *, int);
     91   1.3      onoe 
     92   1.3      onoe static int  fwohci_ctx_alloc(struct fwohci_softc *, struct fwohci_ctx **,
     93   1.3      onoe 		int, int);
     94   1.9      onoe static void fwohci_ctx_free(struct fwohci_softc *, struct fwohci_ctx *);
     95   1.3      onoe static void fwohci_ctx_init(struct fwohci_softc *, struct fwohci_ctx *);
     96   1.3      onoe 
     97   1.3      onoe static int  fwohci_buf_alloc(struct fwohci_softc *, struct fwohci_buf *);
     98   1.3      onoe static void fwohci_buf_free(struct fwohci_softc *, struct fwohci_buf *);
     99   1.3      onoe static void fwohci_buf_init(struct fwohci_softc *);
    100   1.7      onoe static void fwohci_buf_start(struct fwohci_softc *);
    101   1.7      onoe static void fwohci_buf_stop(struct fwohci_softc *);
    102   1.3      onoe static void fwohci_buf_next(struct fwohci_softc *, struct fwohci_ctx *);
    103   1.3      onoe static int  fwohci_buf_pktget(struct fwohci_softc *, struct fwohci_ctx *,
    104   1.3      onoe 		caddr_t *, int);
    105   1.3      onoe static int  fwohci_buf_input(struct fwohci_softc *, struct fwohci_ctx *,
    106   1.3      onoe 		struct fwohci_pkt *);
    107   1.3      onoe 
    108   1.7      onoe static u_int8_t fwohci_phy_read(struct fwohci_softc *, u_int8_t);
    109   1.7      onoe static void fwohci_phy_write(struct fwohci_softc *, u_int8_t, u_int8_t);
    110   1.3      onoe static void fwohci_phy_busreset(struct fwohci_softc *);
    111   1.7      onoe static void fwohci_phy_input(struct fwohci_softc *, struct fwohci_pkt *);
    112   1.3      onoe 
    113   1.3      onoe static int  fwohci_handler_set(struct fwohci_softc *, int, u_int32_t, u_int32_t,
    114   1.3      onoe 		int (*)(struct fwohci_softc *, void *, struct fwohci_pkt *),
    115   1.3      onoe 		void *);
    116   1.3      onoe 
    117   1.3      onoe static void fwohci_arrq_input(struct fwohci_softc *, struct fwohci_ctx *);
    118   1.3      onoe static void fwohci_arrs_input(struct fwohci_softc *, struct fwohci_ctx *);
    119   1.3      onoe static void fwohci_ir_input(struct fwohci_softc *, struct fwohci_ctx *);
    120   1.3      onoe 
    121   1.3      onoe static int  fwohci_at_output(struct fwohci_softc *, struct fwohci_ctx *,
    122   1.3      onoe 		struct fwohci_pkt *);
    123   1.9      onoe static void fwohci_at_done(struct fwohci_softc *, struct fwohci_ctx *, int);
    124   1.3      onoe static void fwohci_atrs_output(struct fwohci_softc *, int, struct fwohci_pkt *,
    125   1.3      onoe 		struct fwohci_pkt *);
    126   1.3      onoe 
    127  1.16      onoe static int  fwohci_guidrom_init(struct fwohci_softc *);
    128   1.3      onoe static void fwohci_configrom_init(struct fwohci_softc *);
    129  1.24       jmc static int  fwohci_configrom_input(struct fwohci_softc *, void *,
    130  1.24       jmc                 struct fwohci_pkt *);
    131   1.3      onoe static void fwohci_selfid_init(struct fwohci_softc *);
    132   1.7      onoe static int  fwohci_selfid_input(struct fwohci_softc *);
    133   1.3      onoe 
    134   1.3      onoe static void fwohci_csr_init(struct fwohci_softc *);
    135   1.3      onoe static int  fwohci_csr_input(struct fwohci_softc *, void *,
    136   1.3      onoe 		struct fwohci_pkt *);
    137   1.3      onoe 
    138   1.3      onoe static void fwohci_uid_collect(struct fwohci_softc *);
    139   1.3      onoe static int  fwohci_uid_input(struct fwohci_softc *, void *,
    140   1.3      onoe 		struct fwohci_pkt *);
    141   1.8      onoe static int  fwohci_uid_lookup(struct fwohci_softc *, const u_int8_t *);
    142  1.24       jmc static void fwohci_check_nodes(struct fwohci_softc *);
    143   1.3      onoe 
    144   1.3      onoe static int  fwohci_if_inreg(struct device *, u_int32_t, u_int32_t,
    145   1.3      onoe 		void (*)(struct device *, struct mbuf *));
    146   1.3      onoe static int  fwohci_if_input(struct fwohci_softc *, void *, struct fwohci_pkt *);
    147   1.3      onoe static int  fwohci_if_output(struct device *, struct mbuf *,
    148   1.3      onoe 		void (*)(struct device *, struct mbuf *));
    149  1.24       jmc static int  fwohci_input(struct ieee1394_abuf *);
    150  1.24       jmc static int  fwohci_output(struct ieee1394_abuf *);
    151  1.24       jmc static int  fwohci_extract_resp(struct fwohci_softc *, void *,
    152  1.24       jmc                 struct fwohci_pkt *);
    153  1.24       jmc static int  fwohci_multi_resp(struct fwohci_softc *, void *,
    154  1.24       jmc                 struct fwohci_pkt *);
    155  1.24       jmc static int  fwohci_inreg(struct ieee1394_abuf *, int);
    156  1.24       jmc static int  fwohci_parse_input(struct fwohci_softc *, void *,
    157  1.24       jmc                 struct fwohci_pkt *);
    158   1.3      onoe 
    159   1.8      onoe #ifdef FW_DEBUG
    160  1.17      onoe int fw_verbose = 1;
    161  1.24       jmc int fw_dump = 1;
    162   1.8      onoe #endif
    163   1.8      onoe 
    164   1.1      matt int
    165   1.5      matt fwohci_init(struct fwohci_softc *sc, const struct evcnt *ev)
    166   1.1      matt {
    167   1.3      onoe 	int i;
    168   1.1      matt 	u_int32_t val;
    169   1.5      matt #if 0
    170   1.5      matt 	int error;
    171   1.5      matt #endif
    172   1.5      matt 
    173   1.5      matt 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ev,
    174   1.5      matt 	    sc->sc_sc1394.sc1394_dev.dv_xname, "intr");
    175   1.1      matt 
    176   1.3      onoe 	/*
    177   1.3      onoe 	 * Wait for reset completion
    178   1.3      onoe 	 */
    179   1.3      onoe 	for (i = 0; i < OHCI_LOOP; i++) {
    180   1.3      onoe 		val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
    181   1.3      onoe 		if ((val & OHCI_HCControl_SoftReset) == 0)
    182   1.3      onoe 			break;
    183   1.3      onoe 	}
    184   1.3      onoe 
    185   1.1      matt 	/* What dialect of OHCI is this device?
    186   1.1      matt 	 */
    187   1.1      matt 	val = OHCI_CSR_READ(sc, OHCI_REG_Version);
    188   1.1      matt 	printf("%s: OHCI %u.%u", sc->sc_sc1394.sc1394_dev.dv_xname,
    189   1.1      matt 	    OHCI_Version_GET_Version(val), OHCI_Version_GET_Revision(val));
    190   1.1      matt 
    191  1.24       jmc 	LIST_INIT(&sc->sc_nodelist);
    192  1.24       jmc 
    193  1.16      onoe 	if (fwohci_guidrom_init(sc) != 0) {
    194  1.16      onoe 		printf("\n%s: fatal: no global UID ROM\n",
    195  1.16      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname);
    196   1.1      matt 		return -1;
    197   1.1      matt 	}
    198   1.1      matt 
    199   1.1      matt 	printf(", %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    200   1.1      matt 	    sc->sc_sc1394.sc1394_guid[0], sc->sc_sc1394.sc1394_guid[1],
    201   1.1      matt 	    sc->sc_sc1394.sc1394_guid[2], sc->sc_sc1394.sc1394_guid[3],
    202   1.1      matt 	    sc->sc_sc1394.sc1394_guid[4], sc->sc_sc1394.sc1394_guid[5],
    203   1.1      matt 	    sc->sc_sc1394.sc1394_guid[6], sc->sc_sc1394.sc1394_guid[7]);
    204   1.1      matt 
    205   1.1      matt 	/* Get the maximum link speed and receive size
    206   1.1      matt 	 */
    207   1.1      matt 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    208   1.1      matt 	sc->sc_sc1394.sc1394_link_speed =
    209  1.18      onoe 	    OHCI_BITVAL(val, OHCI_BusOptions_LinkSpd);
    210   1.1      matt 	if (sc->sc_sc1394.sc1394_link_speed < IEEE1394_SPD_MAX) {
    211   1.1      matt 		printf(", %s", ieee1394_speeds[sc->sc_sc1394.sc1394_link_speed]);
    212   1.1      matt 	} else {
    213   1.1      matt 		printf(", unknown speed %u", sc->sc_sc1394.sc1394_link_speed);
    214   1.1      matt 	}
    215   1.1      matt 
    216   1.1      matt 	/* MaxRec is encoded as log2(max_rec_octets)-1
    217   1.1      matt 	 */
    218   1.1      matt 	sc->sc_sc1394.sc1394_max_receive =
    219  1.18      onoe 	    1 << (OHCI_BITVAL(val, OHCI_BusOptions_MaxRec) + 1);
    220   1.3      onoe 	printf(", %u max_rec", sc->sc_sc1394.sc1394_max_receive);
    221   1.3      onoe 
    222   1.3      onoe 	/*
    223   1.3      onoe 	 * Count how many isochronous ctx we have.
    224   1.3      onoe 	 */
    225   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
    226   1.3      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntMaskClear);
    227   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskClear, ~0);
    228   1.3      onoe 	for (i = 0; val != 0; val >>= 1) {
    229   1.3      onoe 		if (val & 0x1)
    230   1.3      onoe 			i++;
    231   1.3      onoe 	}
    232   1.3      onoe 	sc->sc_isoctx = i;
    233   1.3      onoe 	printf(", %d iso_ctx", sc->sc_isoctx);
    234   1.1      matt 
    235   1.1      matt 	printf("\n");
    236   1.3      onoe 
    237   1.5      matt #if 0
    238   1.5      matt 	error = fwohci_dnamem_alloc(sc, OHCI_CONFIG_SIZE, OHCI_CONFIG_ALIGNMENT,
    239   1.5      matt 				    &sc->sc_configrom_map,
    240   1.5      matt 				    (caddr_t *) &sc->sc_configrom,
    241   1.5      matt 				    BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    242   1.5      matt 	return error;
    243   1.5      matt #endif
    244   1.5      matt 
    245  1.24       jmc 	sc->sc_dying = 0;
    246   1.3      onoe 
    247  1.24       jmc         kthread_create(fwohci_create_event_thread, sc);
    248   1.1      matt 	return 0;
    249   1.1      matt }
    250   1.1      matt 
    251   1.1      matt int
    252   1.1      matt fwohci_intr(void *arg)
    253   1.1      matt {
    254   1.1      matt 	struct fwohci_softc * const sc = arg;
    255   1.1      matt 	int progress = 0;
    256   1.3      onoe 	u_int32_t intmask, iso;
    257   1.1      matt 
    258   1.1      matt 	for (;;) {
    259   1.3      onoe 		intmask = OHCI_CSR_READ(sc, OHCI_REG_IntEventClear);
    260  1.24       jmc 
    261  1.24       jmc                 /*
    262  1.24       jmc                  * On a bus reset, everything except bus reset gets cleared. That
    263  1.24       jmc                  * can't get cleared until the selfid phase completes (which
    264  1.24       jmc                  * happens outside the interrupt routines). So if just a bus
    265  1.24       jmc                  * reset is left in the mask and it's already in the sc_intmask,
    266  1.24       jmc                  * just return.
    267  1.24       jmc                  */
    268  1.24       jmc 
    269  1.24       jmc                 if ((intmask == 0) ||
    270  1.24       jmc                     (progress && (intmask == OHCI_Int_BusReset) &&
    271  1.24       jmc                      (sc->sc_intmask & OHCI_Int_BusReset))) {
    272  1.24       jmc                     if (progress)
    273  1.24       jmc                         wakeup(fwohci_event_thread);
    274  1.24       jmc                     return progress;
    275  1.24       jmc                 }
    276   1.7      onoe 		OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    277   1.7      onoe 		    intmask & ~OHCI_Int_BusReset);
    278   1.3      onoe #ifdef FW_DEBUG
    279  1.17      onoe 		if (fw_verbose > 1) {
    280   1.8      onoe 			printf("%s: intmask=0x%08x:",
    281   1.8      onoe 			    sc->sc_sc1394.sc1394_dev.dv_xname, intmask);
    282   1.8      onoe 			if (intmask & OHCI_Int_CycleTooLong)
    283   1.8      onoe 				printf(" CycleTooLong");
    284   1.8      onoe 			if (intmask & OHCI_Int_UnrecoverableError)
    285   1.8      onoe 				printf(" UnrecoverableError");
    286   1.8      onoe 			if (intmask & OHCI_Int_CycleInconsistent)
    287   1.8      onoe 				printf(" CycleInconsistent");
    288   1.8      onoe 			if (intmask & OHCI_Int_BusReset)
    289   1.8      onoe 				printf(" BusReset");
    290   1.8      onoe 			if (intmask & OHCI_Int_SelfIDComplete)
    291   1.8      onoe 				printf(" SelfIDComplete");
    292   1.8      onoe 			if (intmask & OHCI_Int_LockRespErr)
    293   1.8      onoe 				printf(" LockRespErr");
    294   1.8      onoe 			if (intmask & OHCI_Int_PostedWriteErr)
    295   1.8      onoe 				printf(" PostedWriteErr");
    296   1.8      onoe 			if (intmask & OHCI_Int_ReqTxComplete)
    297   1.8      onoe 				printf(" ReqTxComplete(0x%04x)",
    298   1.8      onoe 				    OHCI_ASYNC_DMA_READ(sc,
    299   1.8      onoe 				    OHCI_CTX_ASYNC_TX_REQUEST,
    300   1.8      onoe 				    OHCI_SUBREG_ContextControlClear));
    301   1.8      onoe 			if (intmask & OHCI_Int_RespTxComplete)
    302   1.8      onoe 				printf(" RespTxComplete(0x%04x)",
    303   1.8      onoe 				    OHCI_ASYNC_DMA_READ(sc,
    304   1.8      onoe 				    OHCI_CTX_ASYNC_TX_RESPONSE,
    305   1.8      onoe 				    OHCI_SUBREG_ContextControlClear));
    306   1.8      onoe 			if (intmask & OHCI_Int_ARRS)
    307   1.8      onoe 				printf(" ARRS(0x%04x)",
    308   1.8      onoe 				    OHCI_ASYNC_DMA_READ(sc,
    309   1.8      onoe 				    OHCI_CTX_ASYNC_RX_RESPONSE,
    310   1.8      onoe 				    OHCI_SUBREG_ContextControlClear));
    311   1.8      onoe 			if (intmask & OHCI_Int_ARRQ)
    312   1.8      onoe 				printf(" ARRQ(0x%04x)",
    313   1.8      onoe 				    OHCI_ASYNC_DMA_READ(sc,
    314   1.8      onoe 				    OHCI_CTX_ASYNC_RX_REQUEST,
    315   1.8      onoe 				    OHCI_SUBREG_ContextControlClear));
    316   1.8      onoe 			if (intmask & OHCI_Int_IsochRx)
    317   1.8      onoe 				printf(" IsochRx(0x%08x)",
    318   1.8      onoe 				    OHCI_CSR_READ(sc,
    319   1.8      onoe 				    OHCI_REG_IsoRecvIntEventClear));
    320   1.8      onoe 			if (intmask & OHCI_Int_IsochTx)
    321   1.8      onoe 				printf(" IsochTx(0x%08x)",
    322   1.8      onoe 				    OHCI_CSR_READ(sc,
    323   1.8      onoe 				    OHCI_REG_IsoXmitIntEventClear));
    324   1.8      onoe 			if (intmask & OHCI_Int_RQPkt)
    325   1.8      onoe 				printf(" RQPkt(0x%04x)",
    326   1.8      onoe 				    OHCI_ASYNC_DMA_READ(sc,
    327   1.8      onoe 				    OHCI_CTX_ASYNC_RX_REQUEST,
    328   1.8      onoe 				    OHCI_SUBREG_ContextControlClear));
    329   1.8      onoe 			if (intmask & OHCI_Int_RSPkt)
    330   1.8      onoe 				printf(" RSPkt(0x%04x)",
    331   1.8      onoe 				    OHCI_ASYNC_DMA_READ(sc,
    332   1.8      onoe 				    OHCI_CTX_ASYNC_RX_RESPONSE,
    333   1.8      onoe 				    OHCI_SUBREG_ContextControlClear));
    334   1.8      onoe 			printf("\n");
    335   1.8      onoe 		}
    336   1.3      onoe #endif /* FW_DEBUG */
    337   1.3      onoe 		if (intmask & OHCI_Int_BusReset) {
    338   1.7      onoe 			/*
    339   1.7      onoe 			 * According to OHCI spec 6.1.1 "busReset",
    340   1.7      onoe 			 * All asynchronous transmit must be stopped before
    341   1.7      onoe 			 * clearing BusReset.  Moreover, the BusReset
    342   1.7      onoe 			 * interrupt bit should not be cleared during the
    343   1.7      onoe 			 * SelfID phase.  Thus we turned off interrupt mask
    344   1.7      onoe 			 * bit of BusReset instead until SelfID completion
    345   1.7      onoe 			 * or SelfID timeout.
    346   1.7      onoe 			 */
    347   1.9      onoe 			intmask &= OHCI_Int_SelfIDComplete;
    348  1.24       jmc                         OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear,
    349  1.24       jmc                                        OHCI_Int_BusReset);
    350  1.24       jmc                         sc->sc_intmask = intmask;
    351  1.24       jmc                         sc->sc_intmask |= OHCI_Int_BusReset;
    352  1.24       jmc 
    353   1.9      onoe 		}
    354   1.9      onoe 
    355  1.24       jmc 		if (intmask & OHCI_Int_SelfIDComplete)
    356  1.24       jmc                     sc->sc_intmask |= OHCI_Int_SelfIDComplete;
    357  1.24       jmc 
    358   1.3      onoe 		if (intmask & OHCI_Int_ReqTxComplete)
    359  1.24       jmc                     sc->sc_intmask |= OHCI_Int_ReqTxComplete;
    360   1.3      onoe 		if (intmask & OHCI_Int_RespTxComplete)
    361  1.24       jmc                     sc->sc_intmask |= OHCI_Int_RespTxComplete;
    362   1.3      onoe 		if (intmask & OHCI_Int_RQPkt)
    363  1.24       jmc                     sc->sc_intmask |= OHCI_Int_RQPkt;
    364   1.3      onoe 		if (intmask & OHCI_Int_RSPkt)
    365  1.24       jmc                     sc->sc_intmask |= OHCI_Int_RSPkt;
    366   1.3      onoe 		if (intmask & OHCI_Int_IsochTx) {
    367  1.24       jmc                     iso = OHCI_CSR_READ(sc, OHCI_REG_IsoXmitIntEventClear);
    368  1.24       jmc                     OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntEventClear, iso);
    369  1.24       jmc                     sc->sc_intmask |= OHCI_Int_IsochTx;
    370  1.24       jmc                 }
    371   1.3      onoe 		if (intmask & OHCI_Int_IsochRx) {
    372  1.24       jmc                     iso = OHCI_CSR_READ(sc, OHCI_REG_IsoRecvIntEventClear);
    373  1.24       jmc                     OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntEventClear, iso);
    374  1.24       jmc                     sc->sc_iso |= iso;
    375  1.24       jmc                     sc->sc_intmask |= OHCI_Int_IsochRx;
    376  1.24       jmc                 }
    377   1.3      onoe 
    378   1.5      matt 		if (!progress) {
    379   1.5      matt 			sc->sc_intrcnt.ev_count++;
    380   1.5      matt 			progress = 1;
    381   1.5      matt 		}
    382   1.1      matt 	}
    383   1.3      onoe }
    384   1.3      onoe 
    385  1.24       jmc static void
    386  1.24       jmc fwohci_create_event_thread(void *arg)
    387  1.24       jmc {
    388  1.24       jmc     struct fwohci_softc  *sc = arg;
    389  1.24       jmc 
    390  1.24       jmc     if (kthread_create1(fwohci_thread_init, sc, &sc->sc_event_thread, "%s",
    391  1.24       jmc                         sc->sc_sc1394.sc1394_dev.dv_xname)) {
    392  1.24       jmc         printf("%s: unable to create event thread\n",
    393  1.24       jmc                sc->sc_sc1394.sc1394_dev.dv_xname);
    394  1.24       jmc         panic("fwohci_create_event_thread");
    395  1.24       jmc     }
    396  1.24       jmc }
    397  1.24       jmc 
    398  1.24       jmc static void
    399  1.24       jmc fwohci_thread_init(void *arg)
    400  1.24       jmc {
    401  1.24       jmc         struct fwohci_softc *sc = arg;
    402  1.24       jmc         int i;
    403  1.24       jmc 
    404  1.24       jmc         /*
    405  1.24       jmc 	 * Allocate descriptors
    406  1.24       jmc 	 */
    407  1.24       jmc         if (fwohci_desc_alloc(sc)) {
    408  1.24       jmc             printf("%s: not enabling interrupts\n",
    409  1.24       jmc                    sc->sc_sc1394.sc1394_dev.dv_xname);
    410  1.24       jmc             kthread_exit(1);
    411  1.24       jmc         }
    412  1.24       jmc 
    413  1.24       jmc 	/*
    414  1.24       jmc 	 * Enable Link Power
    415  1.24       jmc 	 */
    416  1.24       jmc 
    417  1.24       jmc 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
    418  1.24       jmc 
    419  1.24       jmc 	/*
    420  1.24       jmc 	 * Allocate DMA Context
    421  1.24       jmc 	 */
    422  1.24       jmc 	fwohci_ctx_alloc(sc, &sc->sc_ctx_arrq, OHCI_BUF_ARRQ_CNT,
    423  1.24       jmc 	    OHCI_CTX_ASYNC_RX_REQUEST);
    424  1.24       jmc 	fwohci_ctx_alloc(sc, &sc->sc_ctx_arrs, OHCI_BUF_ARRS_CNT,
    425  1.24       jmc 	    OHCI_CTX_ASYNC_RX_RESPONSE);
    426  1.24       jmc 	fwohci_ctx_alloc(sc, &sc->sc_ctx_atrq, 0, OHCI_CTX_ASYNC_TX_REQUEST);
    427  1.24       jmc 	fwohci_ctx_alloc(sc, &sc->sc_ctx_atrs, 0, OHCI_CTX_ASYNC_TX_RESPONSE);
    428  1.24       jmc 	sc->sc_ctx_ir = malloc(sizeof(sc->sc_ctx_ir[0]) * sc->sc_isoctx,
    429  1.24       jmc 	    M_DEVBUF, M_WAITOK);
    430  1.24       jmc 	for (i = 0; i < sc->sc_isoctx; i++)
    431  1.24       jmc 		sc->sc_ctx_ir[i] = NULL;
    432  1.24       jmc 
    433  1.24       jmc 	/*
    434  1.24       jmc 	 * Allocate buffer for configuration ROM and SelfID buffer
    435  1.24       jmc 	 */
    436  1.24       jmc 	fwohci_buf_alloc(sc, &sc->sc_buf_cnfrom);
    437  1.24       jmc 	fwohci_buf_alloc(sc, &sc->sc_buf_selfid);
    438  1.24       jmc 
    439  1.24       jmc         callout_init(&sc->sc_selfid_callout);
    440  1.24       jmc 
    441  1.24       jmc 	sc->sc_sc1394.sc1394_ifinreg = fwohci_if_inreg;
    442  1.24       jmc 	sc->sc_sc1394.sc1394_ifoutput = fwohci_if_output;
    443  1.24       jmc 
    444  1.24       jmc 	/*
    445  1.24       jmc 	 * establish hooks for shutdown and suspend/resume
    446  1.24       jmc 	 */
    447  1.24       jmc 	sc->sc_shutdownhook = shutdownhook_establish(fwohci_shutdown, sc);
    448  1.24       jmc 	sc->sc_powerhook = powerhook_establish(fwohci_power, sc);
    449  1.24       jmc 
    450  1.24       jmc         sc->sc_sc1394.sc1394_if = config_found(&sc->sc_sc1394.sc1394_dev, "fw",
    451  1.24       jmc             fwohci_print);
    452  1.24       jmc 
    453  1.24       jmc         /* Main loop. It's not coming back normally. */
    454  1.24       jmc 
    455  1.24       jmc         fwohci_event_thread(sc);
    456  1.24       jmc 
    457  1.24       jmc         kthread_exit(0);
    458  1.24       jmc }
    459  1.24       jmc 
    460  1.24       jmc static void
    461  1.24       jmc fwohci_event_thread(struct fwohci_softc *sc)
    462  1.24       jmc {
    463  1.24       jmc     int i, s;
    464  1.24       jmc     u_int32_t intmask, iso;
    465  1.24       jmc 
    466  1.24       jmc     s=splbio();
    467  1.24       jmc 
    468  1.24       jmc     /*
    469  1.24       jmc      * Initialize hardware registers.
    470  1.24       jmc      */
    471  1.24       jmc 
    472  1.24       jmc     fwohci_hw_init(sc);
    473  1.24       jmc 
    474  1.24       jmc     /* Initial Bus Reset */
    475  1.24       jmc     fwohci_phy_busreset(sc);
    476  1.24       jmc     tsleep(fwohci_event_thread, PZERO, "fwohci_event", 0);
    477  1.24       jmc     splx(s);
    478  1.24       jmc 
    479  1.24       jmc     while (!sc->sc_dying) {
    480  1.24       jmc         while (1) {
    481  1.24       jmc             s=splbio();
    482  1.24       jmc             intmask = sc->sc_intmask;
    483  1.24       jmc             if (intmask) {
    484  1.24       jmc                 splx(s);
    485  1.24       jmc                 if (intmask & OHCI_Int_BusReset) {
    486  1.24       jmc                     s=splbio();
    487  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_BusReset;
    488  1.24       jmc                     splx(s);
    489  1.24       jmc                     fwohci_buf_stop(sc);
    490  1.24       jmc                     fwohci_buf_init(sc);
    491  1.24       jmc                     if (sc->sc_uidtbl != NULL) {
    492  1.24       jmc                         free(sc->sc_uidtbl, M_DEVBUF);
    493  1.24       jmc                         sc->sc_uidtbl = NULL;
    494  1.24       jmc                     }
    495  1.24       jmc 
    496  1.24       jmc                     callout_reset(&sc->sc_selfid_callout, OHCI_SELFID_TIMEOUT,
    497  1.24       jmc                                   (void (*)(void *))fwohci_phy_busreset, sc);
    498  1.24       jmc                     sc->sc_nodeid = 0xffff;		/* indicate invalid */
    499  1.24       jmc                     sc->sc_rootid = 0;
    500  1.24       jmc                     sc->sc_irmid = IEEE1394_BCAST_PHY_ID;
    501  1.24       jmc                 }
    502  1.24       jmc                 if (intmask & OHCI_Int_SelfIDComplete) {
    503  1.24       jmc                     s=splbio();
    504  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_SelfIDComplete;
    505  1.24       jmc                     OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    506  1.24       jmc                                    OHCI_Int_BusReset);
    507  1.24       jmc                     OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet,
    508  1.24       jmc                                    OHCI_Int_BusReset);
    509  1.24       jmc                     splx(s);
    510  1.24       jmc                     callout_stop(&sc->sc_selfid_callout);
    511  1.24       jmc                     if (fwohci_selfid_input(sc) == 0) {
    512  1.24       jmc                         fwohci_buf_start(sc);
    513  1.24       jmc                         fwohci_uid_collect(sc);
    514  1.24       jmc                     }
    515  1.24       jmc                 }
    516  1.24       jmc                 if (intmask & OHCI_Int_ReqTxComplete) {
    517  1.24       jmc                     s=splbio();
    518  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_ReqTxComplete;
    519  1.24       jmc                     splx(s);
    520  1.24       jmc                     fwohci_at_done(sc, sc->sc_ctx_atrq, 0);
    521  1.24       jmc                 }
    522  1.24       jmc                 if (intmask & OHCI_Int_RespTxComplete) {
    523  1.24       jmc                     s=splbio();
    524  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_RespTxComplete;
    525  1.24       jmc                     splx(s);
    526  1.24       jmc                     fwohci_at_done(sc, sc->sc_ctx_atrs, 0);
    527  1.24       jmc                 }
    528  1.24       jmc                 if (intmask & OHCI_Int_RQPkt) {
    529  1.24       jmc                     s=splbio();
    530  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_RQPkt;
    531  1.24       jmc                     splx(s);
    532  1.24       jmc                     fwohci_arrq_input(sc, sc->sc_ctx_arrq);
    533  1.24       jmc                 }
    534  1.24       jmc                 if (intmask & OHCI_Int_RSPkt) {
    535  1.24       jmc                     s=splbio();
    536  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_RSPkt;
    537  1.24       jmc                     splx(s);
    538  1.24       jmc                     fwohci_arrs_input(sc, sc->sc_ctx_arrs);
    539  1.24       jmc                 }
    540  1.24       jmc                 if (intmask & OHCI_Int_IsochTx) {
    541  1.24       jmc                     s=splbio();
    542  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_IsochTx;
    543  1.24       jmc                     splx(s);
    544  1.24       jmc                 }
    545  1.24       jmc                 if (intmask & OHCI_Int_IsochRx) {
    546  1.24       jmc                     s=splbio();
    547  1.24       jmc                     sc->sc_intmask &= ~OHCI_Int_IsochRx;
    548  1.24       jmc                     iso = sc->sc_iso;
    549  1.24       jmc                     sc->sc_iso = 0;
    550  1.24       jmc                     splx(s);
    551  1.24       jmc                     for (i = 0; i < sc->sc_isoctx; i++) {
    552  1.24       jmc                         if ((iso & (1<<i)) && sc->sc_ctx_ir[i] != NULL)
    553  1.24       jmc                             fwohci_ir_input(sc, sc->sc_ctx_ir[i]);
    554  1.24       jmc 
    555  1.24       jmc                     }
    556  1.24       jmc                 }
    557  1.24       jmc             } else
    558  1.24       jmc                 break;
    559  1.24       jmc         }
    560  1.24       jmc         tsleep(fwohci_event_thread, PZERO, "fwohci_event", 0);
    561  1.24       jmc         splx(s);
    562  1.24       jmc     }
    563  1.24       jmc }
    564  1.24       jmc 
    565   1.5      matt #if 0
    566   1.5      matt static int
    567   1.5      matt fwohci_dnamem_alloc(struct fwohci_softc *sc, int size, int alignment,
    568   1.5      matt 		    bus_dmamap_t *mapp, caddr_t *kvap, int flags)
    569   1.5      matt {
    570   1.5      matt 	bus_dma_segment_t segs[1];
    571   1.5      matt 	int error, nsegs, steps;
    572   1.5      matt 
    573   1.5      matt 	steps = 0;
    574   1.5      matt 	error = bus_dmamem_alloc(sc->sc_dmat, size, alignment, alignment,
    575   1.5      matt 				 segs, 1, &nsegs, flags);
    576   1.5      matt 	if (error)
    577   1.5      matt 		goto cleanup;
    578   1.5      matt 
    579   1.5      matt 	steps = 1;
    580   1.5      matt 	error = bus_dmamem_map(sc->sc_dmat, segs, nsegs, segs[0].ds_len,
    581   1.5      matt 			       kvap, flags);
    582   1.5      matt 	if (error)
    583   1.5      matt 		goto cleanup;
    584   1.5      matt 
    585   1.5      matt 	if (error == 0)
    586   1.5      matt 		error = bus_dmamap_create(sc->sc_dmat, size, 1, alignment,
    587   1.5      matt 					  size, flags, mapp);
    588   1.5      matt 	if (error)
    589   1.5      matt 		goto cleanup;
    590   1.5      matt 	if (error == 0)
    591   1.5      matt 		error = bus_dmamap_load(sc->sc_dmat, *mapp, *kvap, size, NULL, flags);
    592   1.5      matt 	if (error)
    593   1.5      matt 		goto cleanup;
    594   1.5      matt 
    595   1.5      matt cleanup:
    596   1.5      matt 	switch (steps) {
    597   1.5      matt 	case 1:
    598   1.5      matt 		bus_dmamem_free(sc->sc_dmat, segs, nsegs);
    599   1.5      matt 	}
    600   1.5      matt 
    601   1.5      matt 	return error;
    602   1.5      matt }
    603   1.5      matt #endif
    604   1.5      matt 
    605   1.3      onoe int
    606   1.3      onoe fwohci_print(void *aux, const char *pnp)
    607   1.3      onoe {
    608   1.3      onoe 	char *name = aux;
    609   1.3      onoe 
    610   1.3      onoe 	if (pnp)
    611   1.3      onoe 		printf("%s at %s", name, pnp);
    612   1.3      onoe 
    613   1.3      onoe 	return UNCONF;
    614   1.3      onoe }
    615   1.3      onoe 
    616   1.7      onoe static void
    617   1.7      onoe fwohci_hw_init(struct fwohci_softc *sc)
    618   1.7      onoe {
    619   1.7      onoe 	int i;
    620   1.7      onoe 	u_int32_t val;
    621   1.7      onoe 
    622   1.7      onoe 	/*
    623   1.7      onoe 	 * Software Reset.
    624   1.7      onoe 	 */
    625   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
    626   1.7      onoe 	for (i = 0; i < OHCI_LOOP; i++) {
    627   1.7      onoe 		val = OHCI_CSR_READ(sc, OHCI_REG_HCControlClear);
    628   1.7      onoe 		if ((val & OHCI_HCControl_SoftReset) == 0)
    629   1.7      onoe 			break;
    630   1.7      onoe 	}
    631   1.7      onoe 
    632   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LPS);
    633   1.7      onoe 
    634   1.7      onoe 	/*
    635   1.7      onoe 	 * First, initilize CSRs with undefined value to default settings.
    636   1.7      onoe 	 */
    637   1.7      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    638   1.7      onoe 	val |= OHCI_BusOptions_ISC | OHCI_BusOptions_CMC;
    639   1.7      onoe #if 0
    640   1.7      onoe 	val |= OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC;
    641   1.7      onoe #else
    642   1.7      onoe 	val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_IRMC);
    643   1.7      onoe #endif
    644   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
    645   1.7      onoe 	for (i = 0; i < sc->sc_isoctx; i++) {
    646   1.7      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, i, OHCI_SUBREG_ContextControlClear,
    647   1.7      onoe 		    ~0);
    648   1.7      onoe 	}
    649   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear, ~0);
    650   1.7      onoe 
    651   1.7      onoe 	fwohci_configrom_init(sc);
    652   1.7      onoe 	fwohci_selfid_init(sc);
    653   1.7      onoe 	fwohci_buf_init(sc);
    654   1.7      onoe 	fwohci_csr_init(sc);
    655   1.7      onoe 
    656   1.7      onoe 	/*
    657   1.7      onoe 	 * Final CSR settings.
    658   1.7      onoe 	 */
    659   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
    660   1.7      onoe 	    OHCI_LinkControl_CycleTimerEnable |
    661   1.7      onoe 	    OHCI_LinkControl_RcvSelfID | OHCI_LinkControl_RcvPhyPkt);
    662   1.7      onoe 
    663   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_ATRetries, 0x00000888);	/*XXX*/
    664   1.7      onoe 
    665   1.7      onoe 	/* clear receive filter */
    666   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskHiClear, ~0);
    667   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IRMultiChanMaskLoClear, ~0);
    668   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_AsynchronousRequestFilterHiSet, 0x80000000);
    669   1.7      onoe 
    670   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear,
    671   1.7      onoe 	    OHCI_HCControl_NoByteSwapData | OHCI_HCControl_APhyEnhanceEnable);
    672  1.22     enami #if BYTE_ORDER == BIG_ENDIAN
    673  1.22     enami 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet,
    674  1.22     enami 	    OHCI_HCControl_NoByteSwapData);
    675  1.22     enami #endif
    676   1.7      onoe 
    677   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, ~0);
    678   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset |
    679   1.7      onoe 	    OHCI_Int_SelfIDComplete | OHCI_Int_IsochRx | OHCI_Int_IsochTx |
    680   1.7      onoe 	    OHCI_Int_RSPkt | OHCI_Int_RQPkt | OHCI_Int_ARRS | OHCI_Int_ARRQ |
    681   1.7      onoe 	    OHCI_Int_RespTxComplete | OHCI_Int_ReqTxComplete);
    682   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_CycleTooLong |
    683   1.7      onoe 	    OHCI_Int_UnrecoverableError | OHCI_Int_CycleInconsistent |
    684   1.7      onoe 	    OHCI_Int_LockRespErr | OHCI_Int_PostedWriteErr);
    685   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoXmitIntMaskSet, ~0);
    686   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IsoRecvIntMaskSet, ~0);
    687   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_MasterEnable);
    688   1.7      onoe 
    689   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_LinkEnable);
    690   1.7      onoe 
    691   1.7      onoe 	/*
    692   1.7      onoe 	 * Start the receivers
    693   1.7      onoe 	 */
    694   1.7      onoe 	fwohci_buf_start(sc);
    695   1.7      onoe }
    696   1.7      onoe 
    697   1.7      onoe static void
    698   1.7      onoe fwohci_power(int why, void *arg)
    699   1.7      onoe {
    700   1.7      onoe 	struct fwohci_softc *sc = arg;
    701   1.7      onoe 	int s;
    702   1.7      onoe 
    703  1.24       jmc 	s = splbio();
    704  1.10  takemura 	switch (why) {
    705  1.10  takemura 	case PWR_SUSPEND:
    706  1.10  takemura 	case PWR_STANDBY:
    707  1.10  takemura 		fwohci_shutdown(sc);
    708  1.10  takemura 		break;
    709  1.10  takemura 	case PWR_RESUME:
    710   1.7      onoe 		fwohci_hw_init(sc);
    711   1.7      onoe 		fwohci_phy_busreset(sc);
    712  1.10  takemura 		break;
    713  1.10  takemura 	case PWR_SOFTSUSPEND:
    714  1.10  takemura 	case PWR_SOFTSTANDBY:
    715  1.10  takemura 	case PWR_SOFTRESUME:
    716  1.10  takemura 		break;
    717   1.7      onoe 	}
    718   1.7      onoe 	splx(s);
    719   1.7      onoe }
    720   1.7      onoe 
    721   1.7      onoe static void
    722   1.7      onoe fwohci_shutdown(void *arg)
    723   1.7      onoe {
    724   1.7      onoe 	struct fwohci_softc *sc = arg;
    725   1.7      onoe 	u_int32_t val;
    726   1.7      onoe 
    727   1.7      onoe 	callout_stop(&sc->sc_selfid_callout);
    728   1.7      onoe 	/* disable all interrupt */
    729   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskClear, OHCI_Int_MasterEnable);
    730   1.7      onoe 	fwohci_buf_stop(sc);
    731   1.7      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_BusOptions);
    732   1.7      onoe 	val &= ~(OHCI_BusOptions_BMC | OHCI_BusOptions_ISC |
    733   1.7      onoe 		OHCI_BusOptions_CMC | OHCI_BusOptions_IRMC);
    734   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_BusOptions, val);
    735   1.7      onoe 	fwohci_phy_busreset(sc);
    736   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlClear, OHCI_HCControl_LPS);
    737   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_SoftReset);
    738   1.7      onoe }
    739   1.7      onoe 
    740   1.3      onoe /*
    741   1.3      onoe  * COMMON FUNCTIONS
    742   1.3      onoe  */
    743   1.3      onoe 
    744   1.3      onoe /*
    745   1.7      onoe  * read the PHY Register.
    746   1.3      onoe  */
    747   1.7      onoe static u_int8_t
    748   1.7      onoe fwohci_phy_read(struct fwohci_softc *sc, u_int8_t reg)
    749   1.3      onoe {
    750   1.3      onoe 	int i;
    751   1.3      onoe 	u_int32_t val;
    752   1.3      onoe 
    753   1.3      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl,
    754   1.3      onoe 	    OHCI_PhyControl_RdReg | (reg << OHCI_PhyControl_RegAddr_BITPOS));
    755   1.3      onoe 	for (i = 0; i < OHCI_LOOP; i++) {
    756   1.3      onoe 		if (OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
    757   1.3      onoe 		    OHCI_PhyControl_RdDone)
    758   1.3      onoe 			break;
    759   1.3      onoe 	}
    760   1.3      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_PhyControl);
    761   1.7      onoe 	return (val & OHCI_PhyControl_RdData) >> OHCI_PhyControl_RdData_BITPOS;
    762   1.7      onoe }
    763   1.7      onoe 
    764   1.7      onoe /*
    765   1.7      onoe  * write the PHY Register.
    766   1.7      onoe  */
    767   1.7      onoe static void
    768   1.7      onoe fwohci_phy_write(struct fwohci_softc *sc, u_int8_t reg, u_int8_t val)
    769   1.7      onoe {
    770   1.7      onoe 	int i;
    771   1.7      onoe 
    772   1.3      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_PhyControl, OHCI_PhyControl_WrReg |
    773   1.3      onoe 	    (reg << OHCI_PhyControl_RegAddr_BITPOS) |
    774   1.3      onoe 	    (val << OHCI_PhyControl_WrData_BITPOS));
    775   1.3      onoe 	for (i = 0; i < OHCI_LOOP; i++) {
    776   1.3      onoe 		if (!(OHCI_CSR_READ(sc, OHCI_REG_PhyControl) &
    777   1.3      onoe 		    OHCI_PhyControl_WrReg))
    778   1.3      onoe 			break;
    779   1.3      onoe 	}
    780   1.3      onoe }
    781   1.3      onoe 
    782   1.3      onoe /*
    783   1.7      onoe  * Initiate Bus Reset
    784   1.7      onoe  */
    785   1.7      onoe static void
    786   1.7      onoe fwohci_phy_busreset(struct fwohci_softc *sc)
    787   1.7      onoe {
    788   1.7      onoe 	int s;
    789   1.7      onoe 	u_int8_t val;
    790   1.7      onoe 
    791  1.24       jmc 	s = splbio();
    792  1.24       jmc         OHCI_CSR_WRITE(sc, OHCI_REG_IntEventClear,
    793   1.7      onoe 	    OHCI_Int_BusReset | OHCI_Int_SelfIDComplete);
    794   1.7      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_IntMaskSet, OHCI_Int_BusReset);
    795   1.7      onoe 	callout_stop(&sc->sc_selfid_callout);
    796   1.7      onoe 	val = fwohci_phy_read(sc, 1);
    797   1.7      onoe 	val = (val & 0x80) |			/* preserve RHB (force root) */
    798   1.7      onoe 	    0x40 |				/* Initiate Bus Reset */
    799   1.7      onoe 	    0x3f;				/* default GAP count */
    800   1.7      onoe 	fwohci_phy_write(sc, 1, val);
    801   1.7      onoe 	splx(s);
    802   1.7      onoe }
    803   1.7      onoe 
    804   1.7      onoe /*
    805   1.7      onoe  * PHY Packet
    806   1.7      onoe  */
    807   1.7      onoe static void
    808   1.7      onoe fwohci_phy_input(struct fwohci_softc *sc, struct fwohci_pkt *pkt)
    809   1.7      onoe {
    810   1.7      onoe 	u_int32_t val;
    811   1.7      onoe 	u_int8_t key, phyid;
    812   1.7      onoe 
    813   1.7      onoe 	val = pkt->fp_hdr[1];
    814   1.7      onoe 	if (val != ~pkt->fp_hdr[2]) {
    815   1.7      onoe 		if (val == 0 && ((*pkt->fp_trail & 0x001f0000) >> 16) ==
    816   1.7      onoe 		    OHCI_CTXCTL_EVENT_BUS_RESET) {
    817   1.7      onoe #ifdef FW_DEBUG
    818  1.17      onoe 			if (fw_verbose > 1)
    819   1.8      onoe 				printf("fwohci_phy_input: BusReset: 0x%08x\n",
    820   1.8      onoe 				    pkt->fp_hdr[2]);
    821   1.7      onoe #endif
    822   1.7      onoe 		} else {
    823   1.7      onoe 			printf("%s: phy packet corrupted (0x%08x, 0x%08x)\n",
    824   1.7      onoe 			    sc->sc_sc1394.sc1394_dev.dv_xname, val,
    825   1.7      onoe 			    pkt->fp_hdr[2]);
    826   1.7      onoe 		}
    827   1.7      onoe 		return;
    828   1.7      onoe 	}
    829   1.7      onoe 	key = (val & 0xc0000000) >> 30;
    830   1.7      onoe 	phyid = (val & 0x3f000000) >> 24;
    831   1.7      onoe 	switch (key) {
    832   1.7      onoe 	case 0:
    833   1.7      onoe #ifdef FW_DEBUG
    834  1.17      onoe 		if (fw_verbose > 1) {
    835   1.8      onoe 			printf("fwohci_phy_input: PHY Config from %d:", phyid);
    836   1.8      onoe 			if (val & 0x00800000)
    837   1.8      onoe 				printf(" ForceRoot");
    838   1.8      onoe 			if (val & 0x00400000)
    839   1.8      onoe 				printf(" Gap=%x", (val & 0x003f0000) >> 16);
    840   1.8      onoe 			printf("\n");
    841   1.8      onoe 		}
    842   1.7      onoe #endif
    843   1.7      onoe 		break;
    844   1.7      onoe 	case 1:
    845   1.7      onoe #ifdef FW_DEBUG
    846  1.17      onoe 		if (fw_verbose > 1)
    847   1.8      onoe 			printf("fwohci_phy_input: Link-on from %d\n", phyid);
    848   1.7      onoe #endif
    849   1.7      onoe 		break;
    850   1.7      onoe 	case 2:
    851   1.7      onoe #ifdef FW_DEBUG
    852  1.17      onoe 		if (fw_verbose > 1) {
    853   1.8      onoe 			printf("fwohci_phy_input: SelfID from %d:", phyid);
    854   1.8      onoe 			if (val & 0x00800000) {
    855   1.8      onoe 				printf(" #%d", (val & 0x00700000) >> 20);
    856   1.8      onoe 			} else {
    857   1.8      onoe 				if (val & 0x00400000)
    858   1.8      onoe 					printf(" LinkActive");
    859   1.8      onoe 				printf(" Gap=%x", (val & 0x003f0000) >> 16);
    860   1.8      onoe 				printf(" Spd=S%d",
    861   1.8      onoe 				    100 << ((val & 0x0000c000) >> 14));
    862   1.8      onoe 				if (val & 0x00000800)
    863   1.8      onoe 					printf(" Cont");
    864   1.8      onoe 				if (val & 0x00000002)
    865   1.8      onoe 					printf(" InitiateBusReset");
    866   1.8      onoe 			}
    867   1.8      onoe 			if (val & 0x00000001)
    868   1.8      onoe 				printf(" +");
    869   1.8      onoe 			printf("\n");
    870   1.7      onoe 		}
    871   1.7      onoe #endif
    872   1.7      onoe 		break;
    873   1.7      onoe 	default:
    874   1.8      onoe 		printf("%s: unknown PHY packet: 0x%08x\n",
    875   1.8      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, val);
    876   1.7      onoe 		break;
    877   1.7      onoe 	}
    878   1.7      onoe }
    879   1.7      onoe 
    880   1.7      onoe /*
    881   1.3      onoe  * Descriptor for context DMA.
    882   1.3      onoe  */
    883   1.3      onoe static int
    884   1.3      onoe fwohci_desc_alloc(struct fwohci_softc *sc)
    885   1.3      onoe {
    886   1.9      onoe 	int error, mapsize, dsize;
    887   1.3      onoe 
    888   1.3      onoe 	/*
    889   1.3      onoe 	 * allocate descriptor buffer
    890   1.3      onoe 	 */
    891   1.3      onoe 
    892   1.9      onoe 	sc->sc_descsize = OHCI_BUF_ARRQ_CNT + OHCI_BUF_ARRS_CNT +
    893   1.3      onoe 	    OHCI_BUF_ATRQ_CNT + OHCI_BUF_ATRS_CNT +
    894   1.9      onoe 	    OHCI_BUF_IR_CNT * sc->sc_isoctx + 2;
    895   1.9      onoe 	dsize = sizeof(struct fwohci_desc) * sc->sc_descsize;
    896   1.9      onoe 	mapsize = howmany(sc->sc_descsize, NBBY);
    897   1.9      onoe 	sc->sc_descmap = malloc(mapsize, M_DEVBUF, M_WAITOK);
    898   1.9      onoe 	memset(sc->sc_descmap, 0, mapsize);
    899   1.3      onoe 
    900   1.9      onoe 	if ((error = bus_dmamem_alloc(sc->sc_dmat, dsize, PAGE_SIZE, 0,
    901   1.9      onoe 	    &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
    902   1.3      onoe 		printf("%s: unable to allocate descriptor buffer, error = %d\n",
    903   1.3      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    904   1.3      onoe 		goto fail_0;
    905   1.3      onoe 	}
    906   1.3      onoe 
    907   1.3      onoe 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
    908   1.9      onoe 	    dsize, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT | BUS_DMA_WAITOK))
    909   1.9      onoe 	    != 0) {
    910   1.3      onoe 		printf("%s: unable to map descriptor buffer, error = %d\n",
    911   1.3      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
    912   1.3      onoe 		goto fail_1;
    913   1.3      onoe 	}
    914   1.3      onoe 
    915   1.9      onoe 	if ((error = bus_dmamap_create(sc->sc_dmat, dsize, sc->sc_dnseg,
    916  1.11     enami 	    dsize, 0, BUS_DMA_WAITOK, &sc->sc_ddmamap)) != 0) {
    917   1.3      onoe 		printf("%s: unable to create descriptor buffer DMA map, "
    918   1.3      onoe 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
    919   1.3      onoe 		goto fail_2;
    920   1.3      onoe 	}
    921   1.3      onoe 
    922   1.3      onoe 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
    923   1.9      onoe 	    dsize, NULL, BUS_DMA_WAITOK)) != 0) {
    924   1.3      onoe 		printf("%s: unable to load descriptor buffer DMA map, "
    925   1.3      onoe 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname, error);
    926   1.3      onoe 		goto fail_3;
    927   1.3      onoe 	}
    928   1.3      onoe 
    929   1.3      onoe 	return 0;
    930   1.3      onoe 
    931   1.3      onoe   fail_3:
    932   1.3      onoe 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
    933   1.3      onoe   fail_2:
    934   1.9      onoe 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, dsize);
    935   1.3      onoe   fail_1:
    936   1.3      onoe 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
    937   1.3      onoe   fail_0:
    938   1.3      onoe 	return error;
    939   1.3      onoe }
    940   1.3      onoe 
    941   1.9      onoe static struct fwohci_desc *
    942   1.9      onoe fwohci_desc_get(struct fwohci_softc *sc, int ndesc)
    943   1.9      onoe {
    944   1.9      onoe 	int i, n;
    945   1.9      onoe 
    946   1.9      onoe 	for (n = 0; n <= sc->sc_descsize - ndesc; n++) {
    947   1.9      onoe 		for (i = 0; ; i++) {
    948   1.9      onoe 			if (i == ndesc) {
    949   1.9      onoe 				for (i = 0; i < ndesc; i++)
    950   1.9      onoe 					setbit(sc->sc_descmap, n + i);
    951   1.9      onoe 				return sc->sc_desc + n;
    952   1.9      onoe 			}
    953   1.9      onoe 			if (isset(sc->sc_descmap, n + i))
    954   1.9      onoe 				break;
    955   1.9      onoe 		}
    956   1.9      onoe 	}
    957   1.9      onoe 	return NULL;
    958   1.9      onoe }
    959   1.9      onoe 
    960   1.9      onoe static void
    961   1.9      onoe fwohci_desc_put(struct fwohci_softc *sc, struct fwohci_desc *fd, int ndesc)
    962   1.9      onoe {
    963   1.9      onoe 	int i, n;
    964   1.9      onoe 
    965   1.9      onoe 	n = fd - sc->sc_desc;
    966   1.9      onoe 	for (i = 0; i < ndesc; i++, n++) {
    967   1.9      onoe #ifdef DIAGNOSTICS
    968   1.9      onoe 		if (isclr(sc->sc_descmap, n))
    969   1.9      onoe 			panic("fwohci_desc_put: duplicated free");
    970   1.9      onoe #endif
    971   1.9      onoe 		clrbit(sc->sc_descmap, n);
    972   1.9      onoe 	}
    973   1.9      onoe }
    974   1.9      onoe 
    975   1.3      onoe /*
    976   1.3      onoe  * Asyncronous/Isochronous Transmit/Receive Context
    977   1.3      onoe  */
    978   1.3      onoe static int
    979   1.3      onoe fwohci_ctx_alloc(struct fwohci_softc *sc, struct fwohci_ctx **fcp,
    980   1.3      onoe     int bufcnt, int ctx)
    981   1.3      onoe {
    982   1.3      onoe 	int i, error;
    983   1.3      onoe 	struct fwohci_ctx *fc;
    984   1.3      onoe 	struct fwohci_buf *fb;
    985   1.3      onoe 	struct fwohci_desc *fd;
    986   1.3      onoe 
    987   1.3      onoe 	fc = malloc(sizeof(*fc) + sizeof(*fb) * bufcnt, M_DEVBUF, M_WAITOK);
    988   1.3      onoe 	memset(fc, 0, sizeof(*fc) + sizeof(*fb) * bufcnt);
    989   1.3      onoe 	LIST_INIT(&fc->fc_handler);
    990   1.3      onoe 	TAILQ_INIT(&fc->fc_buf);
    991   1.3      onoe 	fc->fc_ctx = ctx;
    992   1.3      onoe 	fc->fc_bufcnt = bufcnt;
    993   1.3      onoe 	fb = (struct fwohci_buf *)&fc[1];
    994   1.3      onoe 	for (i = 0; i < bufcnt; i++, fb++) {
    995   1.3      onoe 		if ((error = fwohci_buf_alloc(sc, fb)) != 0)
    996   1.3      onoe 			goto fail;
    997   1.9      onoe 		if ((fd = fwohci_desc_get(sc, 1)) == NULL) {
    998   1.9      onoe 			error = ENOBUFS;
    999   1.9      onoe 			goto fail;
   1000   1.9      onoe 		}
   1001   1.3      onoe 		fb->fb_desc = fd;
   1002   1.3      onoe 		fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
   1003   1.7      onoe 		    ((caddr_t)fd - (caddr_t)sc->sc_desc);
   1004   1.3      onoe 		fd->fd_flags = OHCI_DESC_INPUT | OHCI_DESC_STATUS |
   1005   1.3      onoe 		    OHCI_DESC_INTR_ALWAYS | OHCI_DESC_BRANCH;
   1006   1.3      onoe 		fd->fd_reqcount = fb->fb_dmamap->dm_segs[0].ds_len;
   1007   1.3      onoe 		fd->fd_data = fb->fb_dmamap->dm_segs[0].ds_addr;
   1008   1.3      onoe 		TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1009   1.3      onoe 	}
   1010   1.3      onoe 	*fcp = fc;
   1011   1.3      onoe 	return 0;
   1012   1.3      onoe 
   1013   1.3      onoe   fail:
   1014   1.3      onoe 	while (i-- > 0)
   1015   1.3      onoe 		fwohci_buf_free(sc, --fb);
   1016   1.3      onoe 	free(fc, M_DEVBUF);
   1017   1.3      onoe 	return error;
   1018   1.3      onoe }
   1019   1.3      onoe 
   1020   1.3      onoe static void
   1021   1.9      onoe fwohci_ctx_free(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1022   1.9      onoe {
   1023   1.9      onoe 	struct fwohci_buf *fb;
   1024   1.9      onoe 	struct fwohci_handler *fh;
   1025   1.9      onoe 
   1026   1.9      onoe 	while ((fh = LIST_FIRST(&fc->fc_handler)) != NULL)
   1027   1.9      onoe 		fwohci_handler_set(sc, fh->fh_tcode, fh->fh_key1, fh->fh_key2,
   1028   1.9      onoe 		    NULL, NULL);
   1029   1.9      onoe 	while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   1030   1.9      onoe 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1031   1.9      onoe 		fwohci_buf_free(sc, fb);
   1032   1.9      onoe 	}
   1033   1.9      onoe 	free(fc, M_DEVBUF);
   1034   1.9      onoe }
   1035   1.9      onoe 
   1036   1.9      onoe static void
   1037   1.3      onoe fwohci_ctx_init(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1038   1.3      onoe {
   1039   1.3      onoe 	struct fwohci_buf *fb, *nfb;
   1040   1.3      onoe 	struct fwohci_desc *fd;
   1041  1.19      onoe 	struct fwohci_handler *fh;
   1042   1.9      onoe 	int n;
   1043   1.3      onoe 
   1044   1.3      onoe 	for (fb = TAILQ_FIRST(&fc->fc_buf); fb != NULL; fb = nfb) {
   1045   1.3      onoe 		nfb = TAILQ_NEXT(fb, fb_list);
   1046   1.3      onoe 		fb->fb_off = 0;
   1047   1.3      onoe 		fd = fb->fb_desc;
   1048   1.3      onoe 		fd->fd_branch = (nfb != NULL) ? (nfb->fb_daddr | 1) : 0;
   1049   1.3      onoe 		fd->fd_rescount = fd->fd_reqcount;
   1050   1.3      onoe 	}
   1051   1.9      onoe 
   1052   1.9      onoe 	n = fc->fc_ctx;
   1053   1.9      onoe 	fb = TAILQ_FIRST(&fc->fc_buf);
   1054   1.9      onoe 	if (fc->fc_isoch) {
   1055   1.9      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_CommandPtr,
   1056   1.9      onoe 		    fb->fb_daddr | 1);
   1057   1.9      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlClear,
   1058   1.9      onoe 		    OHCI_CTXCTL_RX_BUFFER_FILL |
   1059   1.9      onoe 		    OHCI_CTXCTL_RX_CYCLE_MATCH_ENABLE |
   1060   1.9      onoe 		    OHCI_CTXCTL_RX_MULTI_CHAN_MODE |
   1061   1.9      onoe 		    OHCI_CTXCTL_RX_DUAL_BUFFER_MODE);
   1062   1.9      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextControlSet,
   1063   1.9      onoe 		    OHCI_CTXCTL_RX_ISOCH_HEADER);
   1064  1.19      onoe 		fh = LIST_FIRST(&fc->fc_handler);
   1065  1.19      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, n, OHCI_SUBREG_ContextMatch,
   1066  1.19      onoe 		    (OHCI_CTXMATCH_TAG0 << fh->fh_key2) | fh->fh_key1);
   1067   1.9      onoe 	} else {
   1068   1.9      onoe 		OHCI_ASYNC_DMA_WRITE(sc, n, OHCI_SUBREG_CommandPtr,
   1069   1.9      onoe 		    fb->fb_daddr | 1);
   1070   1.9      onoe 	}
   1071   1.3      onoe }
   1072   1.3      onoe 
   1073   1.3      onoe /*
   1074   1.3      onoe  * DMA data buffer
   1075   1.3      onoe  */
   1076   1.3      onoe static int
   1077   1.3      onoe fwohci_buf_alloc(struct fwohci_softc *sc, struct fwohci_buf *fb)
   1078   1.3      onoe {
   1079   1.3      onoe 	int error;
   1080   1.3      onoe 
   1081   1.7      onoe 	if ((error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
   1082   1.7      onoe 	    PAGE_SIZE, &fb->fb_seg, 1, &fb->fb_nseg, BUS_DMA_WAITOK)) != 0) {
   1083   1.3      onoe 		printf("%s: unable to allocate buffer, error = %d\n",
   1084   1.3      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1085   1.3      onoe 		goto fail_0;
   1086   1.3      onoe 	}
   1087   1.3      onoe 
   1088   1.3      onoe 	if ((error = bus_dmamem_map(sc->sc_dmat, &fb->fb_seg,
   1089   1.7      onoe 	    fb->fb_nseg, PAGE_SIZE, &fb->fb_buf, BUS_DMA_WAITOK)) != 0) {
   1090   1.3      onoe 		printf("%s: unable to map buffer, error = %d\n",
   1091   1.3      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, error);
   1092   1.3      onoe 		goto fail_1;
   1093   1.3      onoe 	}
   1094   1.3      onoe 
   1095   1.7      onoe 	if ((error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, fb->fb_nseg,
   1096   1.7      onoe 	    PAGE_SIZE, 0, BUS_DMA_WAITOK, &fb->fb_dmamap)) != 0) {
   1097   1.3      onoe 		printf("%s: unable to create buffer DMA map, "
   1098   1.3      onoe 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
   1099   1.3      onoe 		    error);
   1100   1.3      onoe 		goto fail_2;
   1101   1.3      onoe 	}
   1102   1.3      onoe 
   1103   1.3      onoe 	if ((error = bus_dmamap_load(sc->sc_dmat, fb->fb_dmamap,
   1104   1.7      onoe 	    fb->fb_buf, PAGE_SIZE, NULL, BUS_DMA_WAITOK)) != 0) {
   1105   1.3      onoe 		printf("%s: unable to load buffer DMA map, "
   1106   1.3      onoe 		    "error = %d\n", sc->sc_sc1394.sc1394_dev.dv_xname,
   1107   1.3      onoe 		    error);
   1108   1.3      onoe 		goto fail_3;
   1109   1.3      onoe 	}
   1110   1.3      onoe 
   1111   1.3      onoe 	return 0;
   1112   1.3      onoe 
   1113   1.3      onoe 	bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
   1114   1.3      onoe   fail_3:
   1115   1.3      onoe 	bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   1116   1.3      onoe   fail_2:
   1117   1.7      onoe 	bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
   1118   1.3      onoe   fail_1:
   1119   1.3      onoe 	bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
   1120   1.3      onoe   fail_0:
   1121   1.3      onoe 	return error;
   1122   1.3      onoe }
   1123   1.3      onoe 
   1124   1.3      onoe static void
   1125   1.3      onoe fwohci_buf_free(struct fwohci_softc *sc, struct fwohci_buf *fb)
   1126   1.3      onoe {
   1127   1.3      onoe 
   1128   1.3      onoe 	bus_dmamap_unload(sc->sc_dmat, fb->fb_dmamap);
   1129   1.3      onoe 	bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   1130   1.7      onoe 	bus_dmamem_unmap(sc->sc_dmat, fb->fb_buf, PAGE_SIZE);
   1131   1.3      onoe 	bus_dmamem_free(sc->sc_dmat, &fb->fb_seg, fb->fb_nseg);
   1132   1.3      onoe }
   1133   1.3      onoe 
   1134   1.3      onoe static void
   1135   1.3      onoe fwohci_buf_init(struct fwohci_softc *sc)
   1136   1.3      onoe {
   1137   1.3      onoe 	int i;
   1138   1.3      onoe 
   1139   1.3      onoe 	/*
   1140   1.9      onoe 	 * Initialize for Asynchronous Transmit Queue.
   1141   1.3      onoe 	 */
   1142   1.9      onoe 	fwohci_at_done(sc, sc->sc_ctx_atrq, 1);
   1143   1.9      onoe 	fwohci_at_done(sc, sc->sc_ctx_atrs, 1);
   1144   1.3      onoe 
   1145   1.3      onoe 	/*
   1146   1.9      onoe 	 * Initialize for Asynchronous Receive Queue.
   1147   1.3      onoe 	 */
   1148   1.3      onoe 	fwohci_ctx_init(sc, sc->sc_ctx_arrq);
   1149   1.3      onoe 	fwohci_ctx_init(sc, sc->sc_ctx_arrs);
   1150   1.3      onoe 
   1151   1.3      onoe 	/*
   1152   1.9      onoe 	 * Initialize for Isochronous Receive Queue.
   1153   1.3      onoe 	 */
   1154   1.3      onoe 	for (i = 0; i < sc->sc_isoctx; i++) {
   1155   1.9      onoe 		if (sc->sc_ctx_ir[i] != NULL)
   1156   1.9      onoe 			fwohci_ctx_init(sc, sc->sc_ctx_ir[i]);
   1157   1.7      onoe 	}
   1158   1.7      onoe }
   1159   1.7      onoe 
   1160   1.7      onoe static void
   1161   1.7      onoe fwohci_buf_start(struct fwohci_softc *sc)
   1162   1.7      onoe {
   1163   1.7      onoe 	int i;
   1164   1.7      onoe 
   1165   1.7      onoe 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   1166   1.7      onoe 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1167   1.7      onoe 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   1168   1.7      onoe 	    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1169   1.7      onoe 	for (i = 0; i < sc->sc_isoctx; i++) {
   1170   1.9      onoe 		if (sc->sc_ctx_ir[i] != NULL &&
   1171   1.9      onoe 		    LIST_FIRST(&sc->sc_ctx_ir[i]->fc_handler) != NULL) {
   1172   1.3      onoe 			OHCI_SYNC_RX_DMA_WRITE(sc, i,
   1173   1.3      onoe 			    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1174   1.3      onoe 		}
   1175   1.3      onoe 	}
   1176   1.3      onoe }
   1177   1.3      onoe 
   1178   1.3      onoe static void
   1179   1.7      onoe fwohci_buf_stop(struct fwohci_softc *sc)
   1180   1.7      onoe {
   1181   1.7      onoe 	int i, j;
   1182   1.7      onoe 
   1183   1.7      onoe 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   1184   1.7      onoe 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1185   1.7      onoe 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   1186   1.7      onoe 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1187   1.7      onoe 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_REQUEST,
   1188   1.7      onoe 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1189   1.7      onoe 	OHCI_ASYNC_DMA_WRITE(sc, OHCI_CTX_ASYNC_RX_RESPONSE,
   1190   1.7      onoe 	    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1191   1.7      onoe 	for (i = 0; i < sc->sc_isoctx; i++) {
   1192   1.7      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, i,
   1193   1.7      onoe 		    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1194   1.7      onoe 	}
   1195   1.7      onoe 
   1196   1.7      onoe 	/*
   1197   1.7      onoe 	 * Make sure the transmitter is stopped.
   1198   1.7      onoe 	 */
   1199   1.7      onoe 	for (j = 0; j < OHCI_LOOP; j++) {
   1200   1.7      onoe 		if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_REQUEST,
   1201   1.7      onoe 		    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
   1202   1.7      onoe 			continue;
   1203   1.7      onoe 		if (OHCI_ASYNC_DMA_READ(sc, OHCI_CTX_ASYNC_TX_RESPONSE,
   1204   1.7      onoe 		    OHCI_SUBREG_ContextControlClear) & OHCI_CTXCTL_ACTIVE)
   1205   1.7      onoe 			continue;
   1206   1.7      onoe 		break;
   1207   1.7      onoe 	}
   1208   1.7      onoe }
   1209   1.7      onoe 
   1210   1.7      onoe static void
   1211   1.3      onoe fwohci_buf_next(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1212   1.3      onoe {
   1213   1.3      onoe 	struct fwohci_buf *fb, *tfb;
   1214   1.3      onoe 
   1215   1.3      onoe 	while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   1216   1.3      onoe 		if (fb->fb_off != fb->fb_desc->fd_reqcount ||
   1217   1.3      onoe 		    fb->fb_desc->fd_rescount != 0)
   1218   1.3      onoe 			break;
   1219   1.3      onoe 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1220   1.3      onoe 		fb->fb_desc->fd_rescount = fb->fb_desc->fd_reqcount;
   1221   1.3      onoe 		fb->fb_off = 0;
   1222   1.3      onoe 		fb->fb_desc->fd_branch = 0;
   1223   1.3      onoe 		tfb = TAILQ_LAST(&fc->fc_buf, fwohci_buf_s);
   1224   1.3      onoe 		tfb->fb_desc->fd_branch = fb->fb_daddr | 1;
   1225   1.3      onoe 		TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1226   1.3      onoe 	}
   1227   1.3      onoe }
   1228   1.3      onoe 
   1229   1.3      onoe static int
   1230   1.3      onoe fwohci_buf_pktget(struct fwohci_softc *sc, struct fwohci_ctx *fc, caddr_t *pp,
   1231   1.3      onoe     int len)
   1232   1.3      onoe {
   1233   1.3      onoe 	struct fwohci_buf *fb;
   1234   1.3      onoe 	struct fwohci_desc *fd;
   1235   1.3      onoe 	int bufend;
   1236   1.3      onoe 
   1237   1.3      onoe 	fb = TAILQ_FIRST(&fc->fc_buf);
   1238   1.3      onoe   again:
   1239   1.3      onoe 	fd = fb->fb_desc;
   1240   1.3      onoe #ifdef FW_DEBUG
   1241  1.17      onoe 	if (fw_verbose > 1)
   1242  1.12     enami 		printf("fwohci_buf_pktget: desc %ld, off %d, req %d, res %d,"
   1243   1.9      onoe 		    " len %d, avail %d\n",
   1244  1.12     enami 		    (long)(fd - sc->sc_desc), fb->fb_off, fd->fd_reqcount,
   1245   1.9      onoe 		    fd->fd_rescount, len,
   1246   1.9      onoe 		    fd->fd_reqcount - fd->fd_rescount - fb->fb_off);
   1247   1.3      onoe #endif
   1248   1.3      onoe 	bufend = fd->fd_reqcount - fd->fd_rescount;
   1249   1.3      onoe 	if (fb->fb_off >= bufend) {
   1250   1.9      onoe 		if (fc->fc_isoch && fb->fb_off > 0) {
   1251   1.3      onoe 			fb->fb_off = fd->fd_reqcount;
   1252   1.3      onoe 			fd->fd_rescount = 0;
   1253   1.3      onoe 		}
   1254   1.3      onoe 		if (fd->fd_rescount == 0) {
   1255   1.3      onoe 			if ((fb = TAILQ_NEXT(fb, fb_list)) != NULL)
   1256   1.3      onoe 				goto again;
   1257   1.3      onoe 		}
   1258   1.3      onoe 		return 0;
   1259   1.3      onoe 	}
   1260   1.3      onoe 	if (fb->fb_off + len > bufend)
   1261   1.3      onoe 		len = bufend - fb->fb_off;
   1262   1.7      onoe 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, fb->fb_off, len,
   1263   1.7      onoe 	    BUS_DMASYNC_POSTREAD);
   1264   1.3      onoe 	*pp = fb->fb_buf + fb->fb_off;
   1265   1.3      onoe 	fb->fb_off += roundup(len, 4);
   1266   1.3      onoe 	return len;
   1267   1.3      onoe }
   1268   1.3      onoe 
   1269   1.3      onoe static int
   1270   1.3      onoe fwohci_buf_input(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   1271   1.3      onoe     struct fwohci_pkt *pkt)
   1272   1.3      onoe {
   1273   1.3      onoe 	caddr_t p;
   1274   1.3      onoe 	int len, count, i;
   1275   1.3      onoe 
   1276   1.9      onoe 	memset(pkt, 0, sizeof(*pkt));
   1277   1.9      onoe 	pkt->fp_uio.uio_iov = pkt->fp_iov;
   1278   1.9      onoe 	pkt->fp_uio.uio_rw = UIO_WRITE;
   1279   1.9      onoe 	pkt->fp_uio.uio_segflg = UIO_SYSSPACE;
   1280   1.9      onoe 
   1281   1.3      onoe 	/* get first quadlet */
   1282   1.3      onoe 	count = 4;
   1283   1.9      onoe 	if (fc->fc_isoch) {
   1284   1.3      onoe 		/*
   1285   1.3      onoe 		 * get trailer first, may be bogus data unless status update
   1286   1.3      onoe 		 * in descriptor is set.
   1287   1.3      onoe 		 */
   1288   1.3      onoe 		len = fwohci_buf_pktget(sc, fc, (caddr_t *)&pkt->fp_trail,
   1289  1.13     enami 		    sizeof(*pkt->fp_trail));
   1290   1.7      onoe 		if (len <= 0) {
   1291   1.7      onoe #ifdef FW_DEBUG
   1292  1.17      onoe 			if (fw_verbose > 1)
   1293   1.9      onoe 				printf("fwohci_buf_input: no input for is#%d\n",
   1294   1.8      onoe 				    fc->fc_ctx);
   1295   1.7      onoe #endif
   1296   1.3      onoe 			return 0;
   1297   1.7      onoe 		}
   1298   1.8      onoe 		*pkt->fp_trail = (*pkt->fp_trail & 0xffff) |
   1299   1.8      onoe 			(TAILQ_FIRST(&fc->fc_buf)->fb_desc->fd_status << 16);
   1300   1.3      onoe 	}
   1301   1.3      onoe 	len = fwohci_buf_pktget(sc, fc, &p, count);
   1302   1.3      onoe 	if (len <= 0) {
   1303   1.3      onoe #ifdef FW_DEBUG
   1304  1.17      onoe 		if (fw_verbose > 1)
   1305   1.8      onoe 			printf("fwohci_buf_input: no input for %d\n",
   1306   1.8      onoe 			    fc->fc_ctx);
   1307   1.3      onoe #endif
   1308   1.3      onoe 		return 0;
   1309   1.3      onoe 	}
   1310   1.3      onoe 	pkt->fp_hdr[0] = *(u_int32_t *)p;
   1311   1.3      onoe 	pkt->fp_tcode = (pkt->fp_hdr[0] & 0x000000f0) >> 4;
   1312   1.3      onoe 	switch (pkt->fp_tcode) {
   1313   1.3      onoe 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1314   1.3      onoe 	case IEEE1394_TCODE_READ_RESP_QUAD:
   1315   1.3      onoe 		pkt->fp_hlen = 12;
   1316   1.3      onoe 		pkt->fp_dlen = 4;
   1317   1.3      onoe 		break;
   1318  1.24       jmc 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   1319  1.24       jmc 		pkt->fp_hlen = 16;
   1320  1.24       jmc                 pkt->fp_dlen = 0;
   1321  1.24       jmc                 break;
   1322  1.24       jmc         case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1323  1.24       jmc         case IEEE1394_TCODE_READ_RESP_BLOCK:
   1324   1.3      onoe 	case IEEE1394_TCODE_LOCK_REQ:
   1325   1.3      onoe 	case IEEE1394_TCODE_LOCK_RESP:
   1326   1.3      onoe 		pkt->fp_hlen = 16;
   1327   1.3      onoe 		break;
   1328   1.3      onoe 	case IEEE1394_TCODE_STREAM_DATA:
   1329   1.3      onoe 		pkt->fp_hlen = 4;
   1330   1.3      onoe 		pkt->fp_dlen = pkt->fp_hdr[0] >> 16;
   1331   1.3      onoe 		break;
   1332   1.3      onoe 	default:
   1333   1.3      onoe 		pkt->fp_hlen = 12;
   1334   1.3      onoe 		pkt->fp_dlen = 0;
   1335   1.3      onoe 		break;
   1336   1.3      onoe 	}
   1337   1.3      onoe 
   1338   1.3      onoe 	/* get header */
   1339   1.3      onoe 	while (count < pkt->fp_hlen) {
   1340   1.3      onoe 		len = fwohci_buf_pktget(sc, fc, &p, pkt->fp_hlen - count);
   1341   1.3      onoe 		if (len == 0) {
   1342   1.3      onoe 			printf("fwohci_buf_input: malformed input 1: %d\n",
   1343   1.3      onoe 			    pkt->fp_hlen - count);
   1344   1.3      onoe 			return 0;
   1345   1.3      onoe 		}
   1346   1.3      onoe 		memcpy((caddr_t)pkt->fp_hdr + count, p, len);
   1347   1.3      onoe 		count += len;
   1348   1.3      onoe 	}
   1349  1.24       jmc 	if ((pkt->fp_hlen == 16) &&
   1350  1.24       jmc             pkt->fp_tcode != IEEE1394_TCODE_READ_REQ_BLOCK)
   1351  1.24       jmc                 pkt->fp_dlen = pkt->fp_hdr[3] >> 16;
   1352   1.3      onoe #ifdef FW_DEBUG
   1353  1.17      onoe 	if (fw_verbose > 1)
   1354   1.8      onoe 		printf("fwohci_buf_input: tcode=0x%x, hlen=%d, dlen=%d\n",
   1355   1.8      onoe 		    pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen);
   1356   1.3      onoe #endif
   1357   1.3      onoe 
   1358   1.3      onoe 	/* get data */
   1359   1.3      onoe 	count = 0;
   1360   1.3      onoe 	i = 0;
   1361   1.3      onoe 	while (count < pkt->fp_dlen) {
   1362   1.3      onoe 		len = fwohci_buf_pktget(sc, fc,
   1363   1.3      onoe 		    (caddr_t *)&pkt->fp_iov[i].iov_base,
   1364   1.3      onoe 		    pkt->fp_dlen - count);
   1365   1.3      onoe 		if (len == 0) {
   1366   1.3      onoe 			printf("fwohci_buf_input: malformed input 2: %d\n",
   1367   1.3      onoe 			    pkt->fp_hlen - count);
   1368   1.3      onoe 			return 0;
   1369   1.3      onoe 		}
   1370   1.3      onoe 		pkt->fp_iov[i++].iov_len = len;
   1371   1.3      onoe 		count += len;
   1372   1.3      onoe 	}
   1373   1.9      onoe 	pkt->fp_uio.uio_iovcnt = i;
   1374   1.9      onoe 	pkt->fp_uio.uio_resid = count;
   1375   1.3      onoe 
   1376   1.9      onoe 	if (!fc->fc_isoch) {
   1377   1.3      onoe 		/* get trailer */
   1378   1.3      onoe 		len = fwohci_buf_pktget(sc, fc, (caddr_t *)&pkt->fp_trail,
   1379  1.13     enami 		    sizeof(*pkt->fp_trail));
   1380   1.3      onoe 		if (len <= 0) {
   1381   1.3      onoe 			printf("fwohci_buf_input: malformed input 3: %d\n",
   1382   1.3      onoe 			    pkt->fp_hlen - count);
   1383   1.3      onoe 			return 0;
   1384   1.3      onoe 		}
   1385   1.3      onoe 	}
   1386   1.3      onoe 	return 1;
   1387   1.3      onoe }
   1388   1.3      onoe 
   1389   1.3      onoe static int
   1390   1.3      onoe fwohci_handler_set(struct fwohci_softc *sc,
   1391   1.3      onoe     int tcode, u_int32_t key1, u_int32_t key2,
   1392   1.3      onoe     int (*handler)(struct fwohci_softc *, void *, struct fwohci_pkt *),
   1393   1.3      onoe     void *arg)
   1394   1.3      onoe {
   1395   1.3      onoe 	struct fwohci_ctx *fc;
   1396   1.3      onoe 	struct fwohci_handler *fh;
   1397   1.9      onoe 	int i, j;
   1398   1.3      onoe 
   1399  1.24       jmc         if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1400  1.24       jmc                 j = sc->sc_isoctx;
   1401   1.9      onoe 		fh = NULL;
   1402   1.9      onoe 		for (i = 0; i < sc->sc_isoctx; i++) {
   1403   1.9      onoe 			if ((fc = sc->sc_ctx_ir[i]) == NULL) {
   1404   1.9      onoe 				if (j == sc->sc_isoctx)
   1405   1.9      onoe 					j = i;
   1406   1.9      onoe 				continue;
   1407   1.3      onoe 			}
   1408   1.3      onoe 			fh = LIST_FIRST(&fc->fc_handler);
   1409   1.9      onoe 			if (fh == NULL) {
   1410   1.9      onoe 				j = i;
   1411   1.3      onoe 				break;
   1412   1.9      onoe 			}
   1413   1.9      onoe 			if (fh->fh_tcode == tcode &&
   1414   1.9      onoe 			    fh->fh_key1 == key1 && fh->fh_key2 == key2)
   1415   1.3      onoe 				break;
   1416   1.9      onoe 			fh = NULL;
   1417   1.9      onoe 		}
   1418   1.9      onoe 		if (fh == NULL) {
   1419   1.9      onoe 			if (handler == NULL)
   1420   1.9      onoe 				return 0;
   1421   1.9      onoe 			if (j == sc->sc_isoctx) {
   1422   1.9      onoe #ifdef FW_DEBUG
   1423   1.9      onoe 				if (fw_verbose)
   1424   1.9      onoe 					printf("fwohci_handler_set: "
   1425   1.9      onoe 					    "no more free context\n");
   1426   1.9      onoe #endif
   1427   1.9      onoe 				return ENOMEM;
   1428   1.9      onoe 			}
   1429   1.9      onoe 			if ((fc = sc->sc_ctx_ir[j]) == NULL) {
   1430   1.9      onoe 				fwohci_ctx_alloc(sc, &fc, OHCI_BUF_IR_CNT, j);
   1431   1.9      onoe 				fc->fc_isoch = 1;
   1432   1.9      onoe 				sc->sc_ctx_ir[j] = fc;
   1433   1.9      onoe 			}
   1434   1.3      onoe 		}
   1435   1.3      onoe 	} else {
   1436   1.3      onoe 		switch (tcode) {
   1437   1.3      onoe 		case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1438   1.3      onoe 		case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1439   1.3      onoe 		case IEEE1394_TCODE_READ_REQ_QUAD:
   1440   1.3      onoe 		case IEEE1394_TCODE_READ_REQ_BLOCK:
   1441   1.3      onoe 		case IEEE1394_TCODE_LOCK_REQ:
   1442   1.3      onoe 			fc = sc->sc_ctx_arrq;
   1443   1.3      onoe 			break;
   1444   1.3      onoe 		case IEEE1394_TCODE_WRITE_RESP:
   1445   1.3      onoe 		case IEEE1394_TCODE_READ_RESP_QUAD:
   1446   1.3      onoe 		case IEEE1394_TCODE_READ_RESP_BLOCK:
   1447   1.3      onoe 		case IEEE1394_TCODE_LOCK_RESP:
   1448   1.3      onoe 			fc = sc->sc_ctx_arrs;
   1449   1.3      onoe 			break;
   1450   1.3      onoe 		default:
   1451   1.3      onoe 			return EIO;
   1452   1.3      onoe 		}
   1453   1.3      onoe 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1454   1.3      onoe 		    fh = LIST_NEXT(fh, fh_list)) {
   1455   1.9      onoe 			if (fh->fh_tcode == tcode &&
   1456   1.9      onoe 			    fh->fh_key1 == key1 && fh->fh_key2 == key2)
   1457   1.3      onoe 				break;
   1458   1.3      onoe 		}
   1459   1.3      onoe 	}
   1460   1.3      onoe 	if (handler == NULL) {
   1461   1.9      onoe 		if (fh != NULL) {
   1462  1.24       jmc                         LIST_REMOVE(fh, fh_list);
   1463  1.24       jmc                         free(fh, M_DEVBUF);
   1464   1.9      onoe 		}
   1465   1.9      onoe 		if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1466  1.24       jmc                         sc->sc_ctx_ir[fc->fc_ctx] = NULL;
   1467   1.9      onoe 			fwohci_ctx_free(sc, fc);
   1468   1.9      onoe 		}
   1469   1.3      onoe 		return 0;
   1470   1.3      onoe 	}
   1471   1.3      onoe 	if (fh == NULL) {
   1472  1.24       jmc 		fh = malloc(sizeof(*fh), M_DEVBUF, M_WAITOK);
   1473  1.24       jmc                 LIST_INSERT_HEAD(&fc->fc_handler, fh, fh_list);
   1474   1.3      onoe 	}
   1475  1.24       jmc         fh->fh_tcode = tcode;
   1476   1.3      onoe 	fh->fh_key1 = key1;
   1477   1.3      onoe 	fh->fh_key2 = key2;
   1478   1.3      onoe 	fh->fh_handler = handler;
   1479   1.3      onoe 	fh->fh_handarg = arg;
   1480   1.7      onoe #ifdef FW_DEBUG
   1481  1.17      onoe 	if (fw_verbose > 1)
   1482   1.8      onoe 		printf("fwohci_handler_set: ctx %d, tcode %x, key 0x%x, 0x%x\n",
   1483   1.8      onoe 		    fc->fc_ctx, tcode, key1, key2);
   1484   1.7      onoe #endif
   1485   1.3      onoe 
   1486   1.3      onoe 	if (tcode == IEEE1394_TCODE_STREAM_DATA) {
   1487   1.7      onoe 		fwohci_ctx_init(sc, fc);
   1488   1.7      onoe #ifdef FW_DEBUG
   1489  1.17      onoe 		if (fw_verbose > 1)
   1490  1.12     enami 			printf("fwohci_handler_set: SYNC desc %ld\n",
   1491  1.12     enami 			    (long)(TAILQ_FIRST(&fc->fc_buf)->fb_desc -
   1492  1.12     enami 			    sc->sc_desc));
   1493   1.7      onoe #endif
   1494   1.7      onoe 		OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx,
   1495   1.7      onoe 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1496   1.3      onoe 	}
   1497   1.3      onoe 	return 0;
   1498   1.3      onoe }
   1499   1.3      onoe 
   1500   1.3      onoe /*
   1501   1.3      onoe  * Asyncronous Receive Requests input frontend.
   1502   1.3      onoe  */
   1503   1.3      onoe static void
   1504   1.3      onoe fwohci_arrq_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1505   1.3      onoe {
   1506   1.3      onoe 	int rcode;
   1507   1.3      onoe 	u_int32_t key1, key2;
   1508   1.3      onoe 	struct fwohci_handler *fh;
   1509   1.3      onoe 	struct fwohci_pkt pkt, res;
   1510   1.3      onoe 
   1511  1.24       jmc         while (fwohci_buf_input(sc, fc, &pkt)) {
   1512  1.24       jmc             if (pkt.fp_tcode == OHCI_TCODE_PHY) {
   1513  1.24       jmc                 fwohci_phy_input(sc, &pkt);
   1514  1.24       jmc                 return;
   1515  1.24       jmc             }
   1516  1.24       jmc             key1 = pkt.fp_hdr[1] & 0xffff;
   1517  1.24       jmc             key2 = pkt.fp_hdr[2];
   1518  1.24       jmc             memset(&res, 0, sizeof(res));
   1519  1.24       jmc             res.fp_uio.uio_rw = UIO_WRITE;
   1520  1.24       jmc             res.fp_uio.uio_segflg = UIO_SYSSPACE;
   1521  1.24       jmc             for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1522  1.24       jmc                  fh = LIST_NEXT(fh, fh_list)) {
   1523  1.24       jmc                 if (pkt.fp_tcode == fh->fh_tcode &&
   1524  1.24       jmc                     key1 == fh->fh_key1 &&
   1525  1.24       jmc                     key2 == fh->fh_key2) {
   1526  1.24       jmc                     rcode = (*fh->fh_handler)(sc, fh->fh_handarg, &pkt);
   1527  1.24       jmc                     break;
   1528  1.24       jmc                 }
   1529  1.24       jmc             }
   1530  1.24       jmc             if (fh == NULL) {
   1531  1.24       jmc                 rcode = IEEE1394_RCODE_ADDRESS_ERROR;
   1532  1.24       jmc #ifdef FW_DEBUG
   1533  1.24       jmc                 if (fw_verbose > 1)
   1534  1.24       jmc                     printf("fwohci_arrq_input: no listener:"
   1535  1.24       jmc                            " tcode 0x%x, addr=0x%04x %08x\n",
   1536  1.24       jmc                            pkt.fp_tcode, key1, key2);
   1537  1.24       jmc #endif
   1538  1.24       jmc             }
   1539  1.24       jmc             if (((*pkt.fp_trail & 0x001f0000) >> 16) !=
   1540  1.24       jmc                 OHCI_CTXCTL_EVENT_ACK_PENDING)
   1541  1.24       jmc                 return;
   1542  1.24       jmc             if (rcode != -1)
   1543  1.24       jmc                 fwohci_atrs_output(sc, rcode, &pkt, &res);
   1544  1.24       jmc         }
   1545  1.24       jmc         fwohci_buf_next(sc, fc);
   1546  1.24       jmc         OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1547  1.24       jmc            OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   1548   1.3      onoe }
   1549   1.3      onoe 
   1550  1.24       jmc 
   1551   1.3      onoe /*
   1552   1.3      onoe  * Asynchronous Receive Response input frontend.
   1553   1.3      onoe  */
   1554   1.3      onoe static void
   1555   1.3      onoe fwohci_arrs_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1556   1.3      onoe {
   1557  1.24       jmc         struct fwohci_pkt pkt;
   1558  1.24       jmc         struct fwohci_handler *fh;
   1559   1.3      onoe 	u_int16_t srcid;
   1560   1.3      onoe 	int rcode, tlabel;
   1561   1.3      onoe 
   1562  1.24       jmc         while (fwohci_buf_input(sc, fc, &pkt)) {
   1563  1.24       jmc             srcid = pkt.fp_hdr[1] >> 16;
   1564  1.24       jmc             rcode = (pkt.fp_hdr[1] & 0x0000f000) >> 12;
   1565  1.24       jmc             tlabel = (pkt.fp_hdr[0] & 0x0000fc00) >> 10;
   1566  1.24       jmc #ifdef FW_DEBUG
   1567  1.24       jmc             if (fw_verbose > 1)
   1568  1.24       jmc                 printf("fwohci_arrs_input: tcode 0x%x, from 0x%04x,"
   1569  1.24       jmc                        " tlabel 0x%x, rcode 0x%x, hlen %d, dlen %d\n",
   1570  1.24       jmc                        pkt.fp_tcode, srcid, tlabel, rcode, pkt.fp_hlen,
   1571  1.24       jmc                        pkt.fp_dlen);
   1572  1.24       jmc #endif
   1573  1.24       jmc             for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1574  1.24       jmc                  fh = LIST_NEXT(fh, fh_list)) {
   1575  1.24       jmc                 if (pkt.fp_tcode == fh->fh_tcode &&
   1576  1.24       jmc                     (srcid & OHCI_NodeId_NodeNumber) == fh->fh_key1 &&
   1577  1.24       jmc                     tlabel == fh->fh_key2) {
   1578  1.24       jmc                     (*fh->fh_handler)(sc, fh->fh_handarg, &pkt);
   1579  1.24       jmc                     LIST_REMOVE(fh, fh_list);
   1580  1.24       jmc                     free(fh, M_DEVBUF);
   1581  1.24       jmc                     break;
   1582  1.24       jmc                 }
   1583  1.24       jmc             }
   1584  1.24       jmc #ifdef FW_DEBUG
   1585  1.24       jmc             if (fw_verbose > 1)
   1586  1.24       jmc                 if (fh == NULL) {
   1587  1.24       jmc                     printf("fwohci_arrs_input: no listner\n");
   1588  1.24       jmc                     printf("src: %d, rcode: %d, tlabel: %d, tcode: "
   1589  1.24       jmc                            "%d hdr[3]: 0x%08x, data: 0x%08lx\n", srcid,
   1590  1.24       jmc                            rcode, tlabel, pkt.fp_tcode, pkt.fp_hdr[3],
   1591  1.24       jmc                            (unsigned long)(*((int *)pkt.fp_iov[0].iov_base)));
   1592  1.24       jmc                 }
   1593  1.24       jmc #endif
   1594  1.24       jmc         }
   1595  1.24       jmc         fwohci_buf_next(sc, fc);
   1596  1.24       jmc         OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1597  1.24       jmc             OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   1598   1.3      onoe }
   1599   1.3      onoe 
   1600   1.3      onoe /*
   1601   1.3      onoe  * Isochronous Receive input frontend.
   1602   1.3      onoe  */
   1603   1.3      onoe static void
   1604   1.3      onoe fwohci_ir_input(struct fwohci_softc *sc, struct fwohci_ctx *fc)
   1605   1.3      onoe {
   1606   1.3      onoe 	int rcode, chan, tag;
   1607   1.3      onoe 	struct iovec *iov;
   1608   1.3      onoe 	struct fwohci_handler *fh;
   1609   1.3      onoe 	struct fwohci_pkt pkt;
   1610   1.3      onoe 
   1611   1.3      onoe 	while (fwohci_buf_input(sc, fc, &pkt)) {
   1612   1.3      onoe 		chan = (pkt.fp_hdr[0] & 0x00003f00) >> 8;
   1613   1.3      onoe 		tag  = (pkt.fp_hdr[0] & 0x0000c000) >> 14;
   1614   1.3      onoe #ifdef FW_DEBUG
   1615  1.17      onoe 		if (fw_verbose > 1)
   1616   1.8      onoe 			printf("fwohci_ir_input: hdr 0x%08x, tcode %d,"
   1617   1.8      onoe 			    " hlen %d, dlen %d\n", pkt.fp_hdr[0],
   1618   1.8      onoe 			    pkt.fp_tcode, pkt.fp_hlen, pkt.fp_dlen);
   1619   1.3      onoe #endif
   1620   1.3      onoe 		if (tag == IEEE1394_TAG_GASP) {
   1621   1.3      onoe 			/*
   1622   1.3      onoe 			 * The pkt with tag=3 is GASP format.
   1623   1.3      onoe 			 * Move GASP header to header part.
   1624   1.3      onoe 			 */
   1625   1.3      onoe 			if (pkt.fp_dlen < 8)
   1626   1.3      onoe 				continue;
   1627   1.3      onoe 			iov = pkt.fp_iov;
   1628   1.3      onoe 			/* assuming pkt per buffer mode */
   1629   1.9      onoe 			pkt.fp_hdr[1] = ntohl(((u_int32_t *)iov->iov_base)[0]);
   1630   1.9      onoe 			pkt.fp_hdr[2] = ntohl(((u_int32_t *)iov->iov_base)[1]);
   1631   1.3      onoe 			iov->iov_base = (caddr_t)iov->iov_base + 8;
   1632   1.3      onoe 			iov->iov_len -= 8;
   1633   1.3      onoe 			pkt.fp_hlen += 8;
   1634   1.3      onoe 			pkt.fp_dlen -= 8;
   1635   1.3      onoe 		}
   1636   1.3      onoe 		for (fh = LIST_FIRST(&fc->fc_handler); fh != NULL;
   1637   1.3      onoe 		    fh = LIST_NEXT(fh, fh_list)) {
   1638   1.3      onoe 			if (pkt.fp_tcode == fh->fh_tcode &&
   1639   1.3      onoe 			    chan == fh->fh_key1 && tag == fh->fh_key2) {
   1640   1.3      onoe 				rcode = (*fh->fh_handler)(sc, fh->fh_handarg,
   1641   1.3      onoe 				    &pkt);
   1642   1.3      onoe 				break;
   1643   1.3      onoe 			}
   1644   1.3      onoe 		}
   1645   1.3      onoe #ifdef FW_DEBUG
   1646  1.17      onoe 		if (fw_verbose > 1) {
   1647   1.8      onoe 			if (fh == NULL)
   1648   1.8      onoe 				printf("fwohci_ir_input: no handler\n");
   1649   1.8      onoe 			else
   1650   1.8      onoe 				printf("fwohci_ir_input: rcode %d\n", rcode);
   1651   1.8      onoe 		}
   1652   1.3      onoe #endif
   1653   1.3      onoe 	}
   1654   1.3      onoe 	fwohci_buf_next(sc, fc);
   1655   1.3      onoe 	OHCI_SYNC_RX_DMA_WRITE(sc, fc->fc_ctx, OHCI_SUBREG_ContextControlSet,
   1656   1.3      onoe 	    OHCI_CTXCTL_WAKE);
   1657   1.3      onoe }
   1658   1.3      onoe 
   1659   1.3      onoe /*
   1660   1.3      onoe  * Asynchronous Transmit common routine.
   1661   1.3      onoe  */
   1662   1.3      onoe static int
   1663   1.3      onoe fwohci_at_output(struct fwohci_softc *sc, struct fwohci_ctx *fc,
   1664   1.3      onoe     struct fwohci_pkt *pkt)
   1665   1.3      onoe {
   1666   1.9      onoe 	struct fwohci_buf *fb;
   1667   1.3      onoe 	struct fwohci_desc *fd;
   1668  1.24       jmc         struct mbuf *m, *m0;
   1669   1.9      onoe 	int i, ndesc, error, off, len;
   1670   1.3      onoe 	u_int32_t val;
   1671   1.3      onoe 
   1672  1.24       jmc         if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) > sc->sc_rootid)
   1673   1.9      onoe 		/* We can't send anything during selfid duration */
   1674  1.24       jmc             return EAGAIN;
   1675  1.24       jmc 
   1676   1.3      onoe #ifdef FW_DEBUG
   1677  1.17      onoe 	if (fw_verbose > 1) {
   1678   1.9      onoe 		struct iovec *iov;
   1679   1.8      onoe 		printf("fwohci_at_output: tcode 0x%x, hlen %d, dlen %d",
   1680   1.8      onoe 		    pkt->fp_tcode, pkt->fp_hlen, pkt->fp_dlen);
   1681   1.9      onoe 		if (fw_dump) {
   1682   1.9      onoe 			for (i = 0; i < pkt->fp_hlen/4; i++)
   1683   1.9      onoe 				printf("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]);
   1684   1.8      onoe 			printf("$");
   1685   1.9      onoe 			for (ndesc = 0, iov = pkt->fp_iov;
   1686   1.9      onoe 			    ndesc < pkt->fp_uio.uio_iovcnt; ndesc++, iov++) {
   1687   1.9      onoe 				for (i = 0; i < iov->iov_len; i++)
   1688   1.9      onoe 					printf("%s%02x",
   1689   1.9      onoe 					    (i%32)?((i%4)?"":" "):"\n\t",
   1690   1.9      onoe 					    ((u_int8_t *)iov->iov_base)[i]);
   1691   1.9      onoe 				printf("$");
   1692   1.9      onoe 			}
   1693   1.8      onoe 		}
   1694   1.8      onoe 		printf("\n");
   1695   1.3      onoe 	}
   1696   1.3      onoe #endif
   1697   1.3      onoe 
   1698   1.9      onoe 	if ((m = pkt->fp_m) != NULL) {
   1699   1.9      onoe 		for (ndesc = 2; m != NULL; m = m->m_next)
   1700   1.9      onoe 			ndesc++;
   1701   1.9      onoe 		if (ndesc > OHCI_DESC_MAX) {
   1702   1.9      onoe 			m0 = NULL;
   1703   1.9      onoe 			ndesc = 2;
   1704   1.9      onoe 			for (off = 0; off < pkt->fp_dlen; off += len) {
   1705   1.9      onoe 				if (m0 == NULL) {
   1706   1.9      onoe 					MGETHDR(m0, M_DONTWAIT, MT_DATA);
   1707   1.9      onoe 					if (m0 != NULL)
   1708   1.9      onoe 						M_COPY_PKTHDR(m0, pkt->fp_m);
   1709   1.9      onoe 					m = m0;
   1710   1.9      onoe 				} else {
   1711   1.9      onoe 					MGET(m->m_next, M_DONTWAIT, MT_DATA);
   1712   1.9      onoe 					m = m->m_next;
   1713   1.9      onoe 				}
   1714   1.9      onoe 				if (m != NULL)
   1715   1.9      onoe 					MCLGET(m, M_DONTWAIT);
   1716   1.9      onoe 				if (m == NULL || (m->m_flags & M_EXT) == 0) {
   1717   1.9      onoe 					m_freem(m0);
   1718   1.9      onoe 					return ENOMEM;
   1719   1.9      onoe 				}
   1720   1.9      onoe 				len = pkt->fp_dlen - off;
   1721   1.9      onoe 				if (len > m->m_ext.ext_size)
   1722   1.9      onoe 					len = m->m_ext.ext_size;
   1723   1.9      onoe 				m_copydata(pkt->fp_m, off, len,
   1724   1.9      onoe 				    mtod(m, caddr_t));
   1725  1.15      onoe 				m->m_len = len;
   1726   1.9      onoe 				ndesc++;
   1727   1.9      onoe 			}
   1728   1.9      onoe 			m_freem(pkt->fp_m);
   1729   1.9      onoe 			pkt->fp_m = m0;
   1730   1.9      onoe 		}
   1731   1.9      onoe 	} else
   1732   1.9      onoe 		ndesc = 2 + pkt->fp_uio.uio_iovcnt;
   1733   1.9      onoe 
   1734   1.9      onoe 	if (ndesc > OHCI_DESC_MAX)
   1735   1.3      onoe 		return ENOBUFS;
   1736   1.3      onoe 
   1737   1.9      onoe 	if (fc->fc_bufcnt > 50)			/*XXX*/
   1738   1.9      onoe 		return ENOBUFS;
   1739  1.24       jmc 	fb = malloc(sizeof(*fb), M_DEVBUF, M_WAITOK);
   1740  1.24       jmc         fb->fb_nseg = ndesc;
   1741   1.9      onoe 	fb->fb_desc = fwohci_desc_get(sc, ndesc);
   1742   1.9      onoe 	if (fb->fb_desc == NULL) {
   1743   1.9      onoe 		free(fb, M_DEVBUF);
   1744   1.3      onoe 		return ENOBUFS;
   1745   1.9      onoe 	}
   1746   1.9      onoe 	fb->fb_daddr = sc->sc_ddmamap->dm_segs[0].ds_addr +
   1747   1.9      onoe 	    ((caddr_t)fb->fb_desc - (caddr_t)sc->sc_desc);
   1748   1.9      onoe 	fb->fb_m = pkt->fp_m;
   1749   1.9      onoe 	fb->fb_callback = pkt->fp_callback;
   1750   1.9      onoe 
   1751   1.9      onoe 	if (ndesc > 2) {
   1752   1.9      onoe 		if ((error = bus_dmamap_create(sc->sc_dmat, pkt->fp_dlen, ndesc,
   1753  1.24       jmc 		    PAGE_SIZE, 0, BUS_DMA_WAITOK, &fb->fb_dmamap)) != 0) {
   1754   1.9      onoe 			fwohci_desc_put(sc, fb->fb_desc, ndesc);
   1755   1.9      onoe 			free(fb, M_DEVBUF);
   1756   1.9      onoe 			return error;
   1757   1.9      onoe 		}
   1758   1.9      onoe 
   1759   1.9      onoe 		if (pkt->fp_m != NULL)
   1760   1.9      onoe 			error = bus_dmamap_load_mbuf(sc->sc_dmat, fb->fb_dmamap,
   1761  1.24       jmc 			    pkt->fp_m, BUS_DMA_WAITOK);
   1762   1.9      onoe 		else
   1763   1.9      onoe 			error = bus_dmamap_load_uio(sc->sc_dmat, fb->fb_dmamap,
   1764  1.24       jmc 			    &pkt->fp_uio, BUS_DMA_WAITOK);
   1765   1.9      onoe 		if (error != 0) {
   1766   1.9      onoe 			bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   1767   1.9      onoe 			fwohci_desc_put(sc, fb->fb_desc, ndesc);
   1768   1.9      onoe 			free(fb, M_DEVBUF);
   1769   1.9      onoe 			return error;
   1770   1.3      onoe 		}
   1771   1.9      onoe 		bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0, pkt->fp_dlen,
   1772   1.9      onoe 		    BUS_DMASYNC_PREWRITE);
   1773   1.3      onoe 	}
   1774   1.3      onoe 
   1775   1.3      onoe 	fd = fb->fb_desc;
   1776   1.3      onoe 	fd->fd_flags = OHCI_DESC_IMMED;
   1777   1.3      onoe 	fd->fd_reqcount = pkt->fp_hlen;
   1778   1.3      onoe 	fd->fd_data = 0;
   1779   1.3      onoe 	fd->fd_branch = 0;
   1780   1.3      onoe 	fd->fd_status = 0;
   1781   1.3      onoe 	if (fc->fc_ctx == OHCI_CTX_ASYNC_TX_RESPONSE) {
   1782   1.3      onoe 		i = 3;				/* XXX: 3 sec */
   1783   1.3      onoe 		val = OHCI_CSR_READ(sc, OHCI_REG_IsochronousCycleTimer);
   1784   1.3      onoe 		fd->fd_timestamp = ((val >> 12) & 0x1fff) |
   1785   1.3      onoe 		    ((((val >> 25) + i) & 0x7) << 13);
   1786   1.3      onoe 	} else
   1787   1.3      onoe 		fd->fd_timestamp = 0;
   1788   1.9      onoe 	memcpy(fd + 1, pkt->fp_hdr, pkt->fp_hlen);
   1789   1.9      onoe 	for (i = 0; i < ndesc - 2; i++) {
   1790   1.9      onoe 		fd = fb->fb_desc + 2 + i;
   1791   1.3      onoe 		fd->fd_flags = 0;
   1792   1.9      onoe 		fd->fd_reqcount = fb->fb_dmamap->dm_segs[i].ds_len;
   1793   1.9      onoe 		fd->fd_data = fb->fb_dmamap->dm_segs[i].ds_addr;
   1794   1.3      onoe 		fd->fd_branch = 0;
   1795   1.3      onoe 		fd->fd_status = 0;
   1796   1.3      onoe 		fd->fd_timestamp = 0;
   1797   1.3      onoe 	}
   1798   1.3      onoe 	fd->fd_flags |= OHCI_DESC_LAST | OHCI_DESC_BRANCH;
   1799   1.3      onoe 	fd->fd_flags |= OHCI_DESC_INTR_ALWAYS;
   1800   1.3      onoe 
   1801   1.3      onoe #ifdef FW_DEBUG
   1802  1.17      onoe 	if (fw_verbose > 1) {
   1803  1.12     enami 		printf("fwohci_at_output: desc %ld",
   1804  1.12     enami 		    (long)(fb->fb_desc - sc->sc_desc));
   1805   1.8      onoe 		for (i = 0; i < ndesc * 4; i++)
   1806   1.8      onoe 			printf("%s%08x", i&7?" ":"\n\t",
   1807   1.8      onoe 			    ((u_int32_t *)fb->fb_desc)[i]);
   1808   1.8      onoe 		printf("\n");
   1809   1.8      onoe 	}
   1810   1.3      onoe #endif
   1811   1.3      onoe 
   1812   1.3      onoe 	val = OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
   1813   1.3      onoe 	    OHCI_SUBREG_ContextControlClear);
   1814   1.3      onoe 
   1815   1.3      onoe 	if (val & OHCI_CTXCTL_RUN) {
   1816   1.3      onoe 		if (fc->fc_branch == NULL) {
   1817   1.3      onoe 			OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1818   1.3      onoe 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1819   1.3      onoe 			goto run;
   1820   1.3      onoe 		}
   1821   1.3      onoe 		*fc->fc_branch = fb->fb_daddr | ndesc;
   1822   1.9      onoe 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1823   1.9      onoe 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_WAKE);
   1824   1.3      onoe 	} else {
   1825   1.3      onoe   run:
   1826   1.3      onoe 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1827   1.3      onoe 		    OHCI_SUBREG_CommandPtr, fb->fb_daddr | ndesc);
   1828   1.3      onoe 		OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1829   1.3      onoe 		    OHCI_SUBREG_ContextControlSet, OHCI_CTXCTL_RUN);
   1830   1.3      onoe 	}
   1831   1.3      onoe 	fc->fc_branch = &fd->fd_branch;
   1832   1.3      onoe 
   1833   1.9      onoe 	fc->fc_bufcnt++;
   1834   1.9      onoe 	TAILQ_INSERT_TAIL(&fc->fc_buf, fb, fb_list);
   1835  1.15      onoe 	pkt->fp_m = NULL;
   1836   1.3      onoe 	return 0;
   1837   1.3      onoe }
   1838   1.3      onoe 
   1839   1.3      onoe static void
   1840   1.9      onoe fwohci_at_done(struct fwohci_softc *sc, struct fwohci_ctx *fc, int force)
   1841   1.3      onoe {
   1842   1.9      onoe 	struct fwohci_buf *fb;
   1843   1.9      onoe 	struct fwohci_desc *fd;
   1844   1.9      onoe 	int i;
   1845   1.3      onoe 
   1846   1.9      onoe 	while ((fb = TAILQ_FIRST(&fc->fc_buf)) != NULL) {
   1847   1.9      onoe 		fd = fb->fb_desc;
   1848   1.3      onoe #ifdef FW_DEBUG
   1849  1.17      onoe 		if (fw_verbose > 1) {
   1850  1.12     enami 			printf("fwohci_at_done: %sdesc %ld (%d)",
   1851   1.9      onoe 			    force ? "force " : "",
   1852  1.12     enami 			    (long)(fd - sc->sc_desc), fb->fb_nseg);
   1853   1.9      onoe 			for (i = 0; i < fb->fb_nseg * 4; i++)
   1854   1.9      onoe 				printf("%s%08x", i&7?" ":"\n    ",
   1855   1.9      onoe 				    ((u_int32_t *)fd)[i]);
   1856   1.9      onoe 			printf("\n");
   1857   1.9      onoe 		}
   1858   1.3      onoe #endif
   1859   1.9      onoe 		if (fb->fb_nseg > 2)
   1860   1.9      onoe 			fd += fb->fb_nseg - 1;
   1861   1.9      onoe 		if (!force && !(fd->fd_status & OHCI_CTXCTL_ACTIVE))
   1862   1.3      onoe 			break;
   1863   1.9      onoe 		TAILQ_REMOVE(&fc->fc_buf, fb, fb_list);
   1864   1.9      onoe 		if (fc->fc_branch == &fd->fd_branch) {
   1865   1.9      onoe 			OHCI_ASYNC_DMA_WRITE(sc, fc->fc_ctx,
   1866   1.9      onoe 			    OHCI_SUBREG_ContextControlClear, OHCI_CTXCTL_RUN);
   1867   1.9      onoe 			fc->fc_branch = NULL;
   1868   1.9      onoe 			for (i = 0; i < OHCI_LOOP; i++) {
   1869   1.9      onoe 				if (!(OHCI_ASYNC_DMA_READ(sc, fc->fc_ctx,
   1870   1.9      onoe 				    OHCI_SUBREG_ContextControlClear) &
   1871   1.9      onoe 				    OHCI_CTXCTL_ACTIVE))
   1872   1.9      onoe 					break;
   1873   1.9      onoe 			}
   1874   1.3      onoe 		}
   1875   1.9      onoe 		fwohci_desc_put(sc, fb->fb_desc, fb->fb_nseg);
   1876   1.9      onoe 		if (fb->fb_nseg > 2)
   1877   1.9      onoe 			bus_dmamap_destroy(sc->sc_dmat, fb->fb_dmamap);
   1878   1.9      onoe 		fc->fc_bufcnt--;
   1879   1.9      onoe 		if (fb->fb_callback != NULL) {
   1880   1.9      onoe 			(*fb->fb_callback)(sc->sc_sc1394.sc1394_if, fb->fb_m);
   1881   1.9      onoe 			fb->fb_callback = NULL;
   1882   1.9      onoe 		} else if (fb->fb_m != NULL)
   1883   1.9      onoe 			m_freem(fb->fb_m);
   1884   1.9      onoe 		free(fb, M_DEVBUF);
   1885   1.3      onoe 	}
   1886   1.3      onoe }
   1887   1.3      onoe 
   1888   1.3      onoe /*
   1889   1.3      onoe  * Asynchronous Transmit Reponse -- in response of request packet.
   1890   1.3      onoe  */
   1891   1.3      onoe static void
   1892   1.3      onoe fwohci_atrs_output(struct fwohci_softc *sc, int rcode, struct fwohci_pkt *req,
   1893   1.3      onoe     struct fwohci_pkt *res)
   1894   1.3      onoe {
   1895   1.3      onoe 
   1896  1.24       jmc         if (((*req->fp_trail & 0x001f0000) >> 16) !=
   1897  1.24       jmc             OHCI_CTXCTL_EVENT_ACK_PENDING)
   1898  1.24       jmc             return;
   1899  1.24       jmc 
   1900   1.3      onoe 	res->fp_hdr[0] = (req->fp_hdr[0] & 0x0000fc00) | 0x00000100;
   1901   1.3      onoe 	res->fp_hdr[1] = (req->fp_hdr[1] & 0xffff0000) | (rcode << 12);
   1902   1.3      onoe 	switch (req->fp_tcode) {
   1903   1.3      onoe 	case IEEE1394_TCODE_WRITE_REQ_QUAD:
   1904   1.3      onoe 	case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   1905   1.3      onoe 		res->fp_tcode = IEEE1394_TCODE_WRITE_RESP;
   1906   1.3      onoe 		res->fp_hlen = 12;
   1907   1.3      onoe 		break;
   1908   1.3      onoe 	case IEEE1394_TCODE_READ_REQ_QUAD:
   1909   1.3      onoe 		res->fp_tcode = IEEE1394_TCODE_READ_RESP_QUAD;
   1910   1.3      onoe 		res->fp_hlen = 16;
   1911   1.3      onoe 		res->fp_dlen = 0;
   1912   1.9      onoe 		if (res->fp_uio.uio_iovcnt == 1 && res->fp_iov[0].iov_len == 4)
   1913   1.3      onoe 			res->fp_hdr[3] =
   1914   1.3      onoe 			    *(u_int32_t *)res->fp_iov[0].iov_base;
   1915   1.9      onoe 		res->fp_uio.uio_iovcnt = 0;
   1916   1.3      onoe 		break;
   1917   1.3      onoe 	case IEEE1394_TCODE_READ_REQ_BLOCK:
   1918   1.3      onoe 	case IEEE1394_TCODE_LOCK_REQ:
   1919   1.3      onoe 		if (req->fp_tcode == IEEE1394_TCODE_LOCK_REQ)
   1920   1.3      onoe 			res->fp_tcode = IEEE1394_TCODE_LOCK_RESP;
   1921   1.3      onoe 		else
   1922   1.3      onoe 			res->fp_tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
   1923   1.3      onoe 		res->fp_hlen = 16;
   1924   1.9      onoe 		res->fp_dlen = res->fp_uio.uio_resid;
   1925   1.3      onoe 		res->fp_hdr[3] = res->fp_dlen << 16;
   1926   1.3      onoe 		break;
   1927   1.3      onoe 	}
   1928   1.3      onoe 	res->fp_hdr[0] |= (res->fp_tcode << 4);
   1929  1.24       jmc         fwohci_at_output(sc, sc->sc_ctx_atrs, res);
   1930   1.3      onoe }
   1931   1.3      onoe 
   1932   1.3      onoe /*
   1933   1.3      onoe  * APPLICATION LAYER SERVICES
   1934   1.3      onoe  */
   1935  1.16      onoe 
   1936  1.16      onoe /*
   1937  1.16      onoe  * Retrieve Global UID from GUID ROM
   1938  1.16      onoe  */
   1939  1.16      onoe static int
   1940  1.16      onoe fwohci_guidrom_init(struct fwohci_softc *sc)
   1941  1.16      onoe {
   1942  1.16      onoe 	int i, n, off;
   1943  1.16      onoe 	u_int32_t val1, val2;
   1944  1.16      onoe 
   1945  1.16      onoe 	/* Extract the Global UID
   1946  1.16      onoe 	 */
   1947  1.16      onoe 	val1 = OHCI_CSR_READ(sc, OHCI_REG_GUIDHi);
   1948  1.16      onoe 	val2 = OHCI_CSR_READ(sc, OHCI_REG_GUIDLo);
   1949  1.24       jmc 
   1950  1.16      onoe 	if (val1 != 0 || val2 != 0) {
   1951  1.16      onoe 		sc->sc_sc1394.sc1394_guid[0] = (val1 >> 24) & 0xff;
   1952  1.16      onoe 		sc->sc_sc1394.sc1394_guid[1] = (val1 >> 16) & 0xff;
   1953  1.16      onoe 		sc->sc_sc1394.sc1394_guid[2] = (val1 >>  8) & 0xff;
   1954  1.16      onoe 		sc->sc_sc1394.sc1394_guid[3] = (val1 >>  0) & 0xff;
   1955  1.16      onoe 		sc->sc_sc1394.sc1394_guid[4] = (val2 >> 24) & 0xff;
   1956  1.16      onoe 		sc->sc_sc1394.sc1394_guid[5] = (val2 >> 16) & 0xff;
   1957  1.16      onoe 		sc->sc_sc1394.sc1394_guid[6] = (val2 >>  8) & 0xff;
   1958  1.16      onoe 		sc->sc_sc1394.sc1394_guid[7] = (val2 >>  0) & 0xff;
   1959  1.16      onoe 	} else {
   1960  1.16      onoe 		val1 = OHCI_CSR_READ(sc, OHCI_REG_Version);
   1961  1.16      onoe 		if ((val1 & OHCI_Version_GUID_ROM) == 0)
   1962  1.16      onoe 			return -1;
   1963  1.16      onoe 		OHCI_CSR_WRITE(sc, OHCI_REG_Guid_Rom, OHCI_Guid_AddrReset);
   1964  1.16      onoe 		for (i = 0; i < OHCI_LOOP; i++) {
   1965  1.16      onoe 			val1 = OHCI_CSR_READ(sc, OHCI_REG_Guid_Rom);
   1966  1.16      onoe 			if (!(val1 & OHCI_Guid_AddrReset))
   1967  1.16      onoe 				break;
   1968  1.16      onoe 		}
   1969  1.18      onoe 		off = OHCI_BITVAL(val1, OHCI_Guid_MiniROM) + 4;
   1970  1.16      onoe 		val2 = 0;
   1971  1.16      onoe 		for (n = 0; n < off + sizeof(sc->sc_sc1394.sc1394_guid); n++) {
   1972  1.16      onoe 			OHCI_CSR_WRITE(sc, OHCI_REG_Guid_Rom,
   1973  1.16      onoe 			    OHCI_Guid_RdStart);
   1974  1.16      onoe 			for (i = 0; i < OHCI_LOOP; i++) {
   1975  1.16      onoe 				val1 = OHCI_CSR_READ(sc, OHCI_REG_Guid_Rom);
   1976  1.16      onoe 				if (!(val1 & OHCI_Guid_RdStart))
   1977  1.16      onoe 					break;
   1978  1.16      onoe 			}
   1979  1.16      onoe 			if (n < off)
   1980  1.16      onoe 				continue;
   1981  1.18      onoe 			val1 = OHCI_BITVAL(val1, OHCI_Guid_RdData);
   1982  1.16      onoe 			sc->sc_sc1394.sc1394_guid[n - off] = val1;
   1983  1.16      onoe 			val2 |= val1;
   1984  1.16      onoe 		}
   1985  1.16      onoe 		if (val2 == 0)
   1986  1.16      onoe 			return -1;
   1987  1.16      onoe 	}
   1988  1.16      onoe 	return 0;
   1989  1.16      onoe }
   1990   1.3      onoe 
   1991   1.3      onoe /*
   1992   1.3      onoe  * Initialization for Configuration ROM (no DMA context)
   1993   1.3      onoe  */
   1994   1.3      onoe 
   1995   1.3      onoe #define	CFR_MAXUNIT		20
   1996   1.3      onoe 
   1997   1.3      onoe struct configromctx {
   1998   1.3      onoe 	u_int32_t	*ptr;
   1999   1.3      onoe 	int		curunit;
   2000   1.3      onoe 	struct {
   2001   1.3      onoe 		u_int32_t	*start;
   2002   1.3      onoe 		int		length;
   2003   1.3      onoe 		u_int32_t	*refer;
   2004   1.3      onoe 		int		refunit;
   2005   1.3      onoe 	} unit[CFR_MAXUNIT];
   2006   1.3      onoe };
   2007   1.3      onoe 
   2008   1.3      onoe #define	CFR_PUT_DATA4(cfr, d1, d2, d3, d4)				\
   2009   1.3      onoe 	(*(cfr)->ptr++ = (((d1)<<24) | ((d2)<<16) | ((d3)<<8) | (d4)))
   2010   1.3      onoe 
   2011   1.3      onoe #define	CFR_PUT_DATA1(cfr, d)	(*(cfr)->ptr++ = (d))
   2012   1.3      onoe 
   2013   1.3      onoe #define	CFR_PUT_VALUE(cfr, key, d)	(*(cfr)->ptr++ = ((key)<<24) | (d))
   2014   1.3      onoe 
   2015   1.3      onoe #define	CFR_PUT_CRC(cfr, n)						\
   2016   1.3      onoe 	(*(cfr)->unit[n].start = ((cfr)->unit[n].length << 16) |	\
   2017   1.3      onoe 	    fwohci_crc16((cfr)->unit[n].start + 1, (cfr)->unit[n].length))
   2018   1.3      onoe 
   2019   1.3      onoe #define	CFR_START_UNIT(cfr, n)						\
   2020   1.3      onoe do {									\
   2021   1.3      onoe 	if ((cfr)->unit[n].refer != NULL) {				\
   2022   1.3      onoe 		*(cfr)->unit[n].refer |=				\
   2023   1.3      onoe 		    (cfr)->ptr - (cfr)->unit[n].refer;			\
   2024   1.3      onoe 		CFR_PUT_CRC(cfr, (cfr)->unit[n].refunit);		\
   2025   1.3      onoe 	}								\
   2026   1.3      onoe 	(cfr)->curunit = (n);						\
   2027   1.3      onoe 	(cfr)->unit[n].start = (cfr)->ptr++;				\
   2028   1.3      onoe } while (0 /* CONSTCOND */)
   2029   1.3      onoe 
   2030   1.3      onoe #define	CFR_PUT_REFER(cfr, key, n)					\
   2031   1.3      onoe do {									\
   2032   1.3      onoe 	(cfr)->unit[n].refer = (cfr)->ptr;				\
   2033   1.3      onoe 	(cfr)->unit[n].refunit = (cfr)->curunit;			\
   2034   1.3      onoe 	*(cfr)->ptr++ = (key) << 24;					\
   2035   1.3      onoe } while (0 /* CONSTCOND */)
   2036   1.3      onoe 
   2037   1.3      onoe #define	CFR_END_UNIT(cfr)						\
   2038   1.3      onoe do {									\
   2039   1.3      onoe 	(cfr)->unit[(cfr)->curunit].length = (cfr)->ptr -		\
   2040   1.3      onoe 	    ((cfr)->unit[(cfr)->curunit].start + 1);			\
   2041   1.3      onoe 	CFR_PUT_CRC(cfr, (cfr)->curunit);				\
   2042   1.3      onoe } while (0 /* CONSTCOND */)
   2043   1.3      onoe 
   2044   1.3      onoe static u_int16_t
   2045   1.3      onoe fwohci_crc16(u_int32_t *ptr, int len)
   2046   1.3      onoe {
   2047   1.3      onoe 	int shift;
   2048   1.3      onoe 	u_int32_t crc, sum, data;
   2049   1.3      onoe 
   2050   1.3      onoe 	crc = 0;
   2051   1.3      onoe 	while (len-- > 0) {
   2052   1.3      onoe 		data = *ptr++;
   2053   1.3      onoe 		for (shift = 28; shift >= 0; shift -= 4) {
   2054   1.3      onoe 			sum = ((crc >> 12) ^ (data >> shift)) & 0x000f;
   2055   1.3      onoe 			crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
   2056   1.3      onoe 		}
   2057   1.3      onoe 		crc &= 0xffff;
   2058   1.3      onoe 	}
   2059   1.3      onoe 	return crc;
   2060   1.3      onoe }
   2061   1.3      onoe 
   2062   1.3      onoe static void
   2063   1.3      onoe fwohci_configrom_init(struct fwohci_softc *sc)
   2064   1.3      onoe {
   2065   1.3      onoe 	int i;
   2066   1.3      onoe 	struct fwohci_buf *fb;
   2067   1.3      onoe 	u_int32_t *hdr;
   2068   1.3      onoe 	struct configromctx cfr;
   2069   1.3      onoe 
   2070   1.3      onoe 	fb = &sc->sc_buf_cnfrom;
   2071   1.3      onoe 	memset(&cfr, 0, sizeof(cfr));
   2072   1.3      onoe 	cfr.ptr = hdr = (u_int32_t *)fb->fb_buf;
   2073   1.3      onoe 
   2074   1.3      onoe 	/* headers */
   2075   1.3      onoe 	CFR_START_UNIT(&cfr, 0);
   2076   1.3      onoe 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusId));
   2077   1.3      onoe 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_BusOptions));
   2078   1.3      onoe 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDHi));
   2079   1.3      onoe 	CFR_PUT_DATA1(&cfr, OHCI_CSR_READ(sc, OHCI_REG_GUIDLo));
   2080   1.3      onoe 	CFR_END_UNIT(&cfr);
   2081   1.3      onoe 	/* copy info_length from crc_length */
   2082   1.3      onoe 	*hdr |= (*hdr & 0x00ff0000) << 8;
   2083   1.3      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMhdr, *hdr);
   2084   1.3      onoe 
   2085   1.3      onoe 	/* root directory */
   2086   1.3      onoe 	CFR_START_UNIT(&cfr, 1);
   2087   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0x03, 0x00005e);	/* vendor id */
   2088   1.3      onoe 	CFR_PUT_REFER(&cfr, 0x81, 2);		/* textual descriptor offset */
   2089   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0x0c, 0x0083c0);	/* node capability */
   2090   1.3      onoe 						/* spt,64,fix,lst,drq */
   2091   1.3      onoe #ifdef INET
   2092   1.3      onoe 	CFR_PUT_REFER(&cfr, 0xd1, 3);		/* IPv4 unit directory */
   2093   1.3      onoe #endif /* INET */
   2094   1.3      onoe #ifdef INET6
   2095   1.3      onoe 	CFR_PUT_REFER(&cfr, 0xd1, 4);		/* IPv6 unit directory */
   2096   1.3      onoe #endif /* INET6 */
   2097   1.3      onoe 	CFR_END_UNIT(&cfr);
   2098   1.3      onoe 
   2099   1.3      onoe 	CFR_START_UNIT(&cfr, 2);
   2100   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2101   1.3      onoe 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2102   1.3      onoe 	CFR_PUT_DATA4(&cfr, 'N', 'e', 't', 'B');
   2103   1.3      onoe 	CFR_PUT_DATA4(&cfr, 'S', 'D', 0x00, 0x00);
   2104   1.3      onoe 	CFR_END_UNIT(&cfr);
   2105   1.3      onoe 
   2106   1.3      onoe #ifdef INET
   2107   1.3      onoe 	/* IPv4 unit directory */
   2108   1.3      onoe 	CFR_START_UNIT(&cfr, 3);
   2109   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0x12, 0x00005e);	/* unit spec id */
   2110   1.3      onoe 	CFR_PUT_REFER(&cfr, 0x81, 6);		/* textual descriptor offset */
   2111   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0x13, 0x000001);	/* unit sw version */
   2112   1.3      onoe 	CFR_PUT_REFER(&cfr, 0x81, 7);		/* textual descriptor offset */
   2113   1.3      onoe 	CFR_END_UNIT(&cfr);
   2114   1.3      onoe 
   2115   1.3      onoe 	CFR_START_UNIT(&cfr, 6);
   2116   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2117   1.3      onoe 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2118   1.3      onoe 	CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
   2119   1.3      onoe 	CFR_END_UNIT(&cfr);
   2120   1.3      onoe 
   2121   1.3      onoe 	CFR_START_UNIT(&cfr, 7);
   2122   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2123   1.3      onoe 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2124   1.3      onoe 	CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '4');
   2125   1.3      onoe 	CFR_END_UNIT(&cfr);
   2126   1.3      onoe #endif /* INET */
   2127   1.3      onoe 
   2128   1.3      onoe #ifdef INET6
   2129   1.3      onoe 	/* IPv6 unit directory */
   2130   1.3      onoe 	CFR_START_UNIT(&cfr, 4);
   2131   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0x12, 0x00005e);	/* unit spec id */
   2132   1.3      onoe 	CFR_PUT_REFER(&cfr, 0x81, 8);		/* textual descriptor offset */
   2133   1.8      onoe 	CFR_PUT_VALUE(&cfr, 0x13, 0x000002);	/* unit sw version */
   2134   1.8      onoe 						/* XXX: TBA by IANA */
   2135   1.3      onoe 	CFR_PUT_REFER(&cfr, 0x81, 9);		/* textual descriptor offset */
   2136   1.3      onoe 	CFR_END_UNIT(&cfr);
   2137   1.3      onoe 
   2138   1.3      onoe 	CFR_START_UNIT(&cfr, 8);
   2139   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2140   1.3      onoe 	CFR_PUT_DATA1(&cfr, 0);			/* minimal ASCII */
   2141   1.3      onoe 	CFR_PUT_DATA4(&cfr, 'I', 'A', 'N', 'A');
   2142   1.3      onoe 	CFR_END_UNIT(&cfr);
   2143   1.3      onoe 
   2144   1.3      onoe 	CFR_START_UNIT(&cfr, 9);
   2145   1.3      onoe 	CFR_PUT_VALUE(&cfr, 0, 0);		/* textual descriptor */
   2146   1.3      onoe 	CFR_PUT_DATA1(&cfr, 0);
   2147   1.3      onoe 	CFR_PUT_DATA4(&cfr, 'I', 'P', 'v', '6');
   2148   1.3      onoe 	CFR_END_UNIT(&cfr);
   2149   1.3      onoe #endif /* INET6 */
   2150   1.3      onoe 
   2151  1.24       jmc 	fb->fb_off = cfr.ptr - hdr;
   2152   1.3      onoe #ifdef FW_DEBUG
   2153   1.8      onoe 	if (fw_dump) {
   2154  1.24       jmc                 printf("%s: Config ROM:", sc->sc_sc1394.sc1394_dev.dv_xname);
   2155  1.24       jmc 		for (i = 0; i < fb->fb_off; i++)
   2156   1.8      onoe 			printf("%s%08x", i&7?" ":"\n    ", hdr[i]);
   2157   1.8      onoe 		printf("\n");
   2158   1.8      onoe 	}
   2159   1.3      onoe #endif /* FW_DEBUG */
   2160   1.3      onoe 
   2161   1.3      onoe 	/*
   2162   1.3      onoe 	 * Make network byte order for DMA
   2163   1.3      onoe 	 */
   2164  1.24       jmc 	for (i = 0; i < fb->fb_off; i++)
   2165   1.8      onoe 		HTONL(hdr[i]);
   2166  1.24       jmc         bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
   2167   1.3      onoe 	    (caddr_t)cfr.ptr - fb->fb_buf, BUS_DMASYNC_PREWRITE);
   2168   1.3      onoe 
   2169   1.3      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_ConfigROMmap,
   2170   1.3      onoe 	    fb->fb_dmamap->dm_segs[0].ds_addr);
   2171   1.3      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_HCControlSet, OHCI_HCControl_BIBImageValid);
   2172  1.24       jmc 
   2173  1.24       jmc 	/* Just allow quad reads of the rom. */
   2174  1.24       jmc         for (i = 0; i < fb->fb_off; i++)
   2175  1.24       jmc                 fwohci_handler_set(sc, IEEE1394_TCODE_READ_REQ_QUAD,
   2176  1.24       jmc 		    CSR_BASE_HI, CSR_BASE_LO + CSR_CONFIG_ROM + (i * 4),
   2177  1.24       jmc                     fwohci_configrom_input, NULL);
   2178  1.24       jmc }
   2179  1.24       jmc 
   2180  1.24       jmc static int
   2181  1.24       jmc fwohci_configrom_input(struct fwohci_softc *sc, void *arg,
   2182  1.24       jmc     struct fwohci_pkt *pkt)
   2183  1.24       jmc {
   2184  1.24       jmc 	struct fwohci_pkt res;
   2185  1.24       jmc 	u_int32_t loc, *rom;
   2186  1.24       jmc 
   2187  1.24       jmc 	/* This will be used as an array index so size accordingly. */
   2188  1.24       jmc         loc = pkt->fp_hdr[2] - (CSR_BASE_LO + CSR_CONFIG_ROM);
   2189  1.24       jmc         if ((loc & 0x03) != 0) {
   2190  1.24       jmc 		/* alignment error */
   2191  1.24       jmc 		return IEEE1394_RCODE_ADDRESS_ERROR;
   2192  1.24       jmc 	}
   2193  1.24       jmc         else
   2194  1.24       jmc             loc /= 4;
   2195  1.24       jmc         rom = (u_int32_t *)sc->sc_buf_cnfrom.fb_buf;
   2196  1.24       jmc 
   2197  1.24       jmc #ifdef FW_DEBUG
   2198  1.24       jmc 	if (fw_verbose > 1)
   2199  1.24       jmc 		printf("fwohci_configrom_input: ConfigRom[0x%04x]: 0x%08x\n",
   2200  1.24       jmc 		    loc, ntohl(rom[loc]));
   2201  1.24       jmc #endif
   2202  1.24       jmc 
   2203  1.24       jmc         memset(&res, 0, sizeof(res));
   2204  1.24       jmc         res.fp_hdr[3] = rom[loc];
   2205  1.24       jmc         fwohci_atrs_output(sc, IEEE1394_RCODE_COMPLETE, pkt, &res);
   2206  1.24       jmc         return -1;
   2207   1.3      onoe }
   2208   1.3      onoe 
   2209   1.3      onoe /*
   2210   1.3      onoe  * SelfID buffer (no DMA context)
   2211   1.3      onoe  */
   2212   1.3      onoe static void
   2213   1.3      onoe fwohci_selfid_init(struct fwohci_softc *sc)
   2214   1.3      onoe {
   2215   1.3      onoe 	struct fwohci_buf *fb;
   2216   1.3      onoe 
   2217   1.3      onoe 	fb = &sc->sc_buf_selfid;
   2218   1.7      onoe #ifdef DIAGNOSTICS
   2219   1.7      onoe 	if ((fb->fb_dmamap->dm_segs[0].ds_addr & 0x7ff) != 0)
   2220   1.7      onoe 		panic("fwohci_selfid_init: not aligned: %p (%ld) %p",
   2221   1.7      onoe 		    (caddr_t)fb->fb_dmamap->dm_segs[0].ds_addr,
   2222   1.7      onoe 		    fb->fb_dmamap->dm_segs[0].ds_len, fb->fb_buf);
   2223   1.7      onoe #endif
   2224   1.9      onoe 	memset(fb->fb_buf, 0, fb->fb_dmamap->dm_segs[0].ds_len);
   2225   1.7      onoe 	bus_dmamap_sync(sc->sc_dmat, fb->fb_dmamap, 0,
   2226   1.7      onoe 	    fb->fb_dmamap->dm_segs[0].ds_len, BUS_DMASYNC_PREREAD);
   2227   1.3      onoe 
   2228   1.3      onoe 	OHCI_CSR_WRITE(sc, OHCI_REG_SelfIDBuffer,
   2229   1.3      onoe 	    fb->fb_dmamap->dm_segs[0].ds_addr);
   2230   1.3      onoe }
   2231   1.3      onoe 
   2232   1.7      onoe static int
   2233   1.3      onoe fwohci_selfid_input(struct fwohci_softc *sc)
   2234   1.3      onoe {
   2235   1.3      onoe 	int i;
   2236   1.7      onoe 	u_int32_t count, val, gen;
   2237   1.3      onoe 	u_int32_t *buf;
   2238   1.3      onoe 
   2239  1.20      onoe 	buf = (u_int32_t *)sc->sc_buf_selfid.fb_buf;
   2240   1.3      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
   2241  1.20      onoe   again:
   2242   1.3      onoe 	if (val & OHCI_SelfID_Error) {
   2243   1.3      onoe 		printf("%s: SelfID Error\n", sc->sc_sc1394.sc1394_dev.dv_xname);
   2244   1.7      onoe 		return -1;
   2245   1.3      onoe 	}
   2246  1.18      onoe 	count = OHCI_BITVAL(val, OHCI_SelfID_Size);
   2247   1.3      onoe 
   2248   1.3      onoe 	bus_dmamap_sync(sc->sc_dmat, sc->sc_buf_selfid.fb_dmamap,
   2249   1.3      onoe 	    0, count << 2, BUS_DMASYNC_POSTREAD);
   2250  1.20      onoe 	gen = OHCI_BITVAL(buf[0], OHCI_SelfID_Gen);
   2251   1.3      onoe 
   2252   1.3      onoe #ifdef FW_DEBUG
   2253  1.17      onoe 	if (fw_verbose > 1) {
   2254   1.8      onoe 		printf("%s: SelfID: 0x%08x", sc->sc_sc1394.sc1394_dev.dv_xname,
   2255   1.8      onoe 		    val);
   2256   1.8      onoe 		for (i = 0; i < count; i++)
   2257   1.8      onoe 			printf("%s%08x", i&7?" ":"\n    ", buf[i]);
   2258   1.8      onoe 		printf("\n");
   2259   1.8      onoe 	}
   2260   1.3      onoe #endif /* FW_DEBUG */
   2261   1.3      onoe 
   2262  1.20      onoe 	for (i = 1; i < count; i += 2) {
   2263  1.20      onoe 		if (buf[i] != ~buf[i + 1])
   2264  1.20      onoe 			break;
   2265  1.20      onoe 		if (buf[i] & 0x00000001)
   2266  1.20      onoe 			continue;	/* more pkt */
   2267  1.20      onoe 		if (buf[i] & 0x00800000)
   2268  1.20      onoe 			continue;	/* external id */
   2269  1.20      onoe 		sc->sc_rootid = (buf[i] & 0x3f000000) >> 24;
   2270  1.20      onoe 		if ((buf[i] & 0x00400800) == 0x00400800)
   2271  1.20      onoe 			sc->sc_irmid = sc->sc_rootid;
   2272  1.20      onoe 	}
   2273  1.20      onoe 
   2274  1.20      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_SelfIDCount);
   2275  1.20      onoe 	if (OHCI_BITVAL(val, OHCI_SelfID_Gen) != gen) {
   2276  1.20      onoe 		if (OHCI_BITVAL(val, OHCI_SelfID_Gen) !=
   2277  1.20      onoe 		    OHCI_BITVAL(buf[0], OHCI_SelfID_Gen))
   2278  1.20      onoe 			goto again;
   2279  1.21      onoe #ifdef FW_DEBUG
   2280  1.20      onoe 		if (fw_verbose)
   2281  1.20      onoe 			printf("%s: SelfID Gen mismatch (%d, %d)\n",
   2282  1.20      onoe 			    sc->sc_sc1394.sc1394_dev.dv_xname, gen,
   2283  1.20      onoe 			    OHCI_BITVAL(val, OHCI_SelfID_Gen));
   2284  1.21      onoe #endif
   2285  1.20      onoe 		return -1;
   2286  1.20      onoe 	}
   2287  1.20      onoe 	if (i != count) {
   2288  1.20      onoe 		printf("%s: SelfID corrupted (%d, 0x%08x, 0x%08x)\n",
   2289  1.20      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, i, buf[i], buf[i + 1]);
   2290  1.20      onoe #if 1
   2291  1.20      onoe 		if (i == 1 && buf[i] == 0 && buf[i + 1] == 0) {
   2292  1.20      onoe 			/*
   2293  1.20      onoe 			 * XXX: CXD3222 sometimes fails to DMA
   2294  1.20      onoe 			 * selfid packet??
   2295  1.20      onoe 			 */
   2296  1.20      onoe 			sc->sc_rootid = (count - 1) / 2 - 1;
   2297  1.20      onoe 			sc->sc_irmid = sc->sc_rootid;
   2298  1.20      onoe 		} else
   2299  1.20      onoe #endif
   2300  1.20      onoe 		return -1;
   2301  1.20      onoe 	}
   2302  1.20      onoe 
   2303   1.7      onoe 	val = OHCI_CSR_READ(sc, OHCI_REG_NodeId);
   2304   1.7      onoe 	if ((val & OHCI_NodeId_IDValid) == 0) {
   2305   1.9      onoe 		sc->sc_nodeid = 0xffff;		/* invalid */
   2306   1.7      onoe 		printf("%s: nodeid is invalid\n",
   2307   1.7      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname);
   2308   1.7      onoe 		return -1;
   2309   1.7      onoe 	}
   2310   1.7      onoe 	sc->sc_nodeid = val & 0xffff;
   2311   1.7      onoe 
   2312   1.3      onoe #ifdef FW_DEBUG
   2313   1.8      onoe 	if (fw_verbose)
   2314   1.8      onoe 		printf("%s: nodeid=0x%04x(%d), rootid=%d, irmid=%d\n",
   2315   1.8      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname,
   2316   1.8      onoe 		    sc->sc_nodeid, sc->sc_nodeid & OHCI_NodeId_NodeNumber,
   2317   1.8      onoe 		    sc->sc_rootid, sc->sc_irmid);
   2318   1.3      onoe #endif
   2319   1.3      onoe 
   2320   1.3      onoe 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) > sc->sc_rootid)
   2321   1.7      onoe 		return -1;
   2322   1.3      onoe 
   2323   1.3      onoe 	if ((sc->sc_nodeid & OHCI_NodeId_NodeNumber) == sc->sc_rootid)
   2324   1.3      onoe 		OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlSet,
   2325   1.3      onoe 		    OHCI_LinkControl_CycleMaster);
   2326   1.3      onoe 	else
   2327   1.3      onoe 		OHCI_CSR_WRITE(sc, OHCI_REG_LinkControlClear,
   2328   1.3      onoe 		    OHCI_LinkControl_CycleMaster);
   2329   1.7      onoe 	return 0;
   2330   1.3      onoe }
   2331   1.3      onoe 
   2332   1.3      onoe /*
   2333   1.3      onoe  * some CSRs are handled by driver.
   2334   1.3      onoe  */
   2335   1.3      onoe static void
   2336   1.3      onoe fwohci_csr_init(struct fwohci_softc *sc)
   2337   1.3      onoe {
   2338   1.3      onoe 	int i;
   2339   1.3      onoe 	static u_int32_t csr[] = {
   2340   1.3      onoe 	    CSR_STATE_CLEAR, CSR_STATE_SET, CSR_SB_CYCLE_TIME,
   2341   1.3      onoe 	    CSR_SB_BUS_TIME, CSR_SB_BUSY_TIMEOUT, CSR_SB_BUS_MANAGER_ID,
   2342   1.3      onoe 	    CSR_SB_CHANNEL_AVAILABLE_HI, CSR_SB_CHANNEL_AVAILABLE_LO,
   2343   1.3      onoe 	    CSR_SB_BROADCAST_CHANNEL
   2344   1.3      onoe 	};
   2345   1.3      onoe 
   2346   1.3      onoe 	for (i = 0; i < sizeof(csr) / sizeof(csr[0]); i++) {
   2347   1.3      onoe 		fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_QUAD,
   2348   1.3      onoe 		    CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
   2349   1.3      onoe 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_REQ_QUAD,
   2350   1.3      onoe 		    CSR_BASE_HI, CSR_BASE_LO + csr[i], fwohci_csr_input, NULL);
   2351   1.3      onoe 	}
   2352   1.3      onoe 	sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] = 31;	/*XXX*/
   2353   1.3      onoe }
   2354   1.3      onoe 
   2355   1.3      onoe static int
   2356   1.3      onoe fwohci_csr_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   2357   1.3      onoe {
   2358   1.3      onoe 	struct fwohci_pkt res;
   2359   1.3      onoe 	u_int32_t reg;
   2360   1.3      onoe 
   2361   1.3      onoe 	/*
   2362   1.3      onoe 	 * XXX need to do special functionality other than just r/w...
   2363   1.3      onoe 	 */
   2364   1.3      onoe 	reg = pkt->fp_hdr[2] - CSR_BASE_LO;
   2365   1.3      onoe 
   2366   1.3      onoe 	if ((reg & 0x03) != 0) {
   2367   1.3      onoe 		/* alignment error */
   2368   1.3      onoe 		return IEEE1394_RCODE_ADDRESS_ERROR;
   2369   1.3      onoe 	}
   2370   1.8      onoe #ifdef FW_DEBUG
   2371  1.17      onoe 	if (fw_verbose > 1)
   2372   1.8      onoe 		printf("fwohci_csr_input: CSR[0x%04x]: 0x%08x",
   2373   1.8      onoe 		    reg, *(u_int32_t *)(&sc->sc_csr[reg]));
   2374   1.8      onoe #endif
   2375   1.3      onoe 	if (pkt->fp_tcode == IEEE1394_TCODE_WRITE_REQ_QUAD) {
   2376   1.3      onoe #ifdef FW_DEBUG
   2377  1.17      onoe 		if (fw_verbose > 1)
   2378   1.8      onoe 			printf(" -> 0x%08x\n",
   2379   1.8      onoe 			    ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base));
   2380   1.3      onoe #endif
   2381   1.3      onoe 		*(u_int32_t *)&sc->sc_csr[reg] =
   2382   1.3      onoe 		    ntohl(*(u_int32_t *)pkt->fp_iov[0].iov_base);
   2383   1.3      onoe 	} else {
   2384   1.3      onoe #ifdef FW_DEBUG
   2385  1.17      onoe 		if (fw_verbose > 1)
   2386   1.8      onoe 			printf("\n");
   2387   1.3      onoe #endif
   2388   1.3      onoe 		res.fp_hdr[3] = htonl(*(u_int32_t *)&sc->sc_csr[reg]);
   2389   1.3      onoe 		res.fp_iov[0].iov_base = &res.fp_hdr[3];
   2390   1.3      onoe 		res.fp_iov[0].iov_len = 4;
   2391   1.9      onoe 		res.fp_uio.uio_resid = 4;
   2392   1.9      onoe 		res.fp_uio.uio_iovcnt = 1;
   2393   1.3      onoe 		fwohci_atrs_output(sc, IEEE1394_RCODE_COMPLETE, pkt, &res);
   2394   1.3      onoe 		return -1;
   2395   1.3      onoe 	}
   2396   1.3      onoe 	return IEEE1394_RCODE_COMPLETE;
   2397   1.3      onoe }
   2398   1.3      onoe 
   2399   1.3      onoe /*
   2400   1.3      onoe  * Mapping between nodeid and unique ID (EUI-64).
   2401  1.24       jmc  *
   2402  1.24       jmc  * Track old mappings and simply update their devices with the new id's when
   2403  1.24       jmc  * they match an existing EUI. This allows proper renumeration of the bus.
   2404   1.3      onoe  */
   2405   1.3      onoe static void
   2406   1.3      onoe fwohci_uid_collect(struct fwohci_softc *sc)
   2407   1.3      onoe {
   2408   1.3      onoe 	int i;
   2409   1.3      onoe 	struct fwohci_uidtbl *fu;
   2410   1.3      onoe 	struct fwohci_pkt pkt;
   2411  1.24       jmc 	struct ieee1394_softc *iea;
   2412  1.24       jmc 
   2413  1.24       jmc 	LIST_FOREACH(iea, &sc->sc_nodelist, sc1394_node)
   2414  1.24       jmc 		iea->sc1394_node_id = 0xffff;
   2415   1.3      onoe 
   2416   1.3      onoe 	if (sc->sc_uidtbl != NULL)
   2417   1.3      onoe 		free(sc->sc_uidtbl, M_DEVBUF);
   2418  1.24       jmc         sc->sc_uidtbl = malloc(sizeof(*fu) * (sc->sc_rootid + 1), M_DEVBUF,
   2419  1.24       jmc             M_WAITOK);
   2420   1.3      onoe 	memset(sc->sc_uidtbl, 0, sizeof(*fu) * (sc->sc_rootid + 1));
   2421   1.3      onoe 
   2422   1.3      onoe 	memset(&pkt, 0, sizeof(pkt));
   2423   1.3      onoe 	for (i = 0, fu = sc->sc_uidtbl; i <= sc->sc_rootid; i++, fu++) {
   2424   1.3      onoe 		if (i == (sc->sc_nodeid & OHCI_NodeId_NodeNumber)) {
   2425   1.8      onoe 			memcpy(fu->fu_uid, sc->sc_sc1394.sc1394_guid, 8);
   2426   1.8      onoe 			fu->fu_valid = 3;
   2427  1.24       jmc 
   2428  1.24       jmc 		        iea = (struct ieee1394_softc *)sc->sc_sc1394.sc1394_if;
   2429  1.24       jmc                         if (iea) {
   2430  1.24       jmc                             iea->sc1394_node_id = i;
   2431  1.24       jmc #ifdef FW_DEBUG
   2432  1.24       jmc                             if (fw_verbose)
   2433  1.24       jmc                                 printf("%s: Updating nodeid to %d\n",
   2434  1.24       jmc                                        iea->sc1394_dev.dv_xname,
   2435  1.24       jmc                                        iea->sc1394_node_id);
   2436  1.24       jmc #endif
   2437  1.24       jmc                         }
   2438  1.24       jmc                         continue;
   2439   1.3      onoe 		}
   2440   1.8      onoe 		fu->fu_valid = 0;
   2441   1.3      onoe 		pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   2442   1.3      onoe 		pkt.fp_hlen = 12;
   2443   1.3      onoe 		pkt.fp_dlen = 0;
   2444  1.24       jmc 		pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   2445  1.24       jmc                     (pkt.fp_tcode << 4);
   2446   1.3      onoe 		pkt.fp_hdr[1] = ((0xffc0 | i) << 16) | CSR_BASE_HI;
   2447   1.3      onoe 		pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 12;
   2448   1.3      onoe 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, i,
   2449   1.8      onoe 		    sc->sc_tlabel, fwohci_uid_input, (void *)0);
   2450   1.3      onoe 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   2451   1.3      onoe 		fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   2452   1.3      onoe 
   2453  1.24       jmc 		pkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   2454  1.24       jmc                     (pkt.fp_tcode << 4);
   2455   1.3      onoe 		pkt.fp_hdr[2] = CSR_BASE_LO + CSR_CONFIG_ROM + 16;
   2456   1.3      onoe 		fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD, i,
   2457   1.8      onoe 		    sc->sc_tlabel, fwohci_uid_input, (void *)1);
   2458   1.3      onoe 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   2459   1.3      onoe 		fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   2460  1.24       jmc 
   2461   1.3      onoe 	}
   2462  1.24       jmc         if (sc->sc_rootid == 0)
   2463  1.24       jmc             fwohci_check_nodes(sc);
   2464  1.24       jmc 
   2465   1.3      onoe }
   2466   1.3      onoe 
   2467   1.3      onoe static int
   2468   1.3      onoe fwohci_uid_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *res)
   2469   1.3      onoe {
   2470   1.8      onoe 	struct fwohci_uidtbl *fu;
   2471  1.24       jmc 	struct ieee1394_softc *iea;
   2472  1.24       jmc         struct ieee1394_attach_args fwa;
   2473  1.24       jmc         int i, n, done, rcode, found;
   2474  1.24       jmc 
   2475  1.24       jmc         found = 0;
   2476  1.24       jmc 
   2477  1.24       jmc         n = (res->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
   2478   1.8      onoe 	rcode = (res->fp_hdr[1] & 0x0000f000) >> 12;
   2479   1.8      onoe 	if (rcode != IEEE1394_RCODE_COMPLETE ||
   2480   1.8      onoe 	    sc->sc_uidtbl == NULL ||
   2481   1.8      onoe 	    n > sc->sc_rootid)
   2482   1.8      onoe 		return 0;
   2483   1.8      onoe 	fu = &sc->sc_uidtbl[n];
   2484   1.8      onoe 	if (arg == 0) {
   2485   1.8      onoe 		memcpy(fu->fu_uid, res->fp_iov[0].iov_base, 4);
   2486   1.8      onoe 		fu->fu_valid |= 0x1;
   2487   1.8      onoe 	} else {
   2488   1.8      onoe 		memcpy(fu->fu_uid + 4, res->fp_iov[0].iov_base, 4);
   2489   1.8      onoe 		fu->fu_valid |= 0x2;
   2490   1.8      onoe 	}
   2491   1.3      onoe #ifdef FW_DEBUG
   2492  1.24       jmc 	if ((fw_verbose > 1) && fu->fu_valid == 0x3)
   2493   1.8      onoe 		printf("fwohci_uid_input: "
   2494   1.8      onoe 		    "Node %d, UID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", n,
   2495   1.8      onoe 		    fu->fu_uid[0], fu->fu_uid[1], fu->fu_uid[2], fu->fu_uid[3],
   2496   1.8      onoe 		    fu->fu_uid[4], fu->fu_uid[5], fu->fu_uid[6], fu->fu_uid[7]);
   2497   1.3      onoe #endif
   2498  1.24       jmc 	if (fu->fu_valid == 0x3) {
   2499  1.24       jmc                 LIST_FOREACH(iea, &sc->sc_nodelist, sc1394_node)
   2500  1.24       jmc 			if (memcmp (iea->sc1394_guid, fu->fu_uid, 8) == 0) {
   2501  1.24       jmc 				found = 1;
   2502  1.24       jmc 				iea->sc1394_node_id = n;
   2503  1.24       jmc #ifdef FW_DEBUG
   2504  1.24       jmc 				if (fw_verbose)
   2505  1.24       jmc 					printf("%s: Updating nodeid to %d\n",
   2506  1.24       jmc 					    iea->sc1394_dev.dv_xname,
   2507  1.24       jmc 					    iea->sc1394_node_id);
   2508  1.24       jmc #endif
   2509  1.24       jmc 				break;
   2510  1.24       jmc 			}
   2511  1.24       jmc 		if (!found) {
   2512  1.24       jmc 			strcpy (fwa.name, "fwnode");
   2513  1.24       jmc 			memcpy (fwa.uid, fu->fu_uid, 8);
   2514  1.24       jmc 			fwa.nodeid = n;
   2515  1.24       jmc                         fwa.input = fwohci_input;
   2516  1.24       jmc                         fwa.output = fwohci_output;
   2517  1.24       jmc                         fwa.inreg = fwohci_inreg;
   2518  1.24       jmc                         iea = (struct ieee1394_softc *)
   2519  1.24       jmc 			    config_found(&sc->sc_sc1394.sc1394_dev, &fwa,
   2520  1.24       jmc 			        fwohci_print);
   2521  1.24       jmc                         LIST_INSERT_HEAD(&sc->sc_nodelist, iea, sc1394_node);
   2522  1.24       jmc 		}
   2523  1.24       jmc 	}
   2524  1.24       jmc         done = 1;
   2525  1.24       jmc 
   2526  1.24       jmc         for (i = 0; i < sc->sc_rootid + 1; i++) {
   2527  1.24       jmc             fu = &sc->sc_uidtbl[i];
   2528  1.24       jmc             if (fu->fu_valid != 0x3) {
   2529  1.24       jmc                 done = 0;
   2530  1.24       jmc                 break;
   2531  1.24       jmc             }
   2532  1.24       jmc         }
   2533  1.24       jmc         if (done)
   2534  1.24       jmc             fwohci_check_nodes(sc);
   2535  1.24       jmc 
   2536  1.24       jmc         return 0;
   2537  1.24       jmc }
   2538  1.24       jmc 
   2539  1.24       jmc static void
   2540  1.24       jmc fwohci_check_nodes(struct fwohci_softc *sc)
   2541  1.24       jmc {
   2542  1.24       jmc     struct device *detach = NULL;
   2543  1.24       jmc     struct ieee1394_softc *iea;
   2544  1.24       jmc 
   2545  1.24       jmc     LIST_FOREACH(iea, &sc->sc_nodelist, sc1394_node) {
   2546  1.24       jmc         /*
   2547  1.24       jmc          * Have to defer detachment until the next
   2548  1.24       jmc          * loop iteration since config_detach
   2549  1.24       jmc          * free's the softc and the loop iterator
   2550  1.24       jmc          * needs data from the softc to move
   2551  1.24       jmc          * forward.
   2552  1.24       jmc          */
   2553  1.24       jmc 
   2554  1.24       jmc         if (detach) {
   2555  1.24       jmc             config_detach (detach, 0);
   2556  1.24       jmc             detach = NULL;
   2557  1.24       jmc         }
   2558  1.24       jmc         if (iea->sc1394_node_id == 0xffff) {
   2559  1.24       jmc             detach = (struct device *)iea;
   2560  1.24       jmc             LIST_REMOVE(iea, sc1394_node);
   2561  1.24       jmc         }
   2562  1.24       jmc     }
   2563  1.24       jmc     if (detach)
   2564  1.24       jmc         config_detach (detach, 0);
   2565   1.3      onoe }
   2566   1.3      onoe 
   2567   1.3      onoe static int
   2568   1.8      onoe fwohci_uid_lookup(struct fwohci_softc *sc, const u_int8_t *uid)
   2569   1.3      onoe {
   2570   1.3      onoe 	struct fwohci_uidtbl *fu;
   2571   1.3      onoe 	int n;
   2572   1.3      onoe 	static const u_int8_t bcast[] =
   2573   1.3      onoe 	    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
   2574   1.3      onoe 
   2575  1.24       jmc         fu = sc->sc_uidtbl;
   2576   1.3      onoe 	if (fu == NULL) {
   2577   1.8      onoe   notfound:
   2578   1.8      onoe 		if (memcmp(uid, bcast, sizeof(bcast)) == 0)
   2579   1.8      onoe 			return IEEE1394_BCAST_PHY_ID;
   2580   1.3      onoe 		fwohci_uid_collect(sc); /* try to get */
   2581   1.3      onoe 		return -1;
   2582   1.3      onoe 	}
   2583   1.8      onoe 	for (n = 0; ; n++, fu++) {
   2584   1.8      onoe 		if (n > sc->sc_rootid)
   2585   1.8      onoe 			goto notfound;
   2586   1.8      onoe 		if (fu->fu_valid == 0x3 && memcmp(fu->fu_uid, uid, 8) == 0)
   2587   1.3      onoe 			break;
   2588   1.3      onoe 	}
   2589  1.24       jmc         return n;
   2590   1.3      onoe }
   2591   1.3      onoe 
   2592   1.3      onoe /*
   2593   1.3      onoe  * functions to support network interface
   2594   1.3      onoe  */
   2595   1.3      onoe static int
   2596   1.3      onoe fwohci_if_inreg(struct device *self, u_int32_t offhi, u_int32_t offlo,
   2597   1.3      onoe     void (*handler)(struct device *, struct mbuf *))
   2598   1.3      onoe {
   2599   1.3      onoe 	struct fwohci_softc *sc = (struct fwohci_softc *)self;
   2600  1.24       jmc 
   2601  1.24       jmc         fwohci_handler_set(sc, IEEE1394_TCODE_WRITE_REQ_BLOCK, offhi, offlo,
   2602   1.3      onoe 	    fwohci_if_input, handler);
   2603  1.24       jmc         fwohci_handler_set(sc, IEEE1394_TCODE_STREAM_DATA,
   2604   1.3      onoe 	    sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] & OHCI_NodeId_NodeNumber,
   2605   1.3      onoe 	    IEEE1394_TAG_GASP, fwohci_if_input, handler);
   2606   1.3      onoe 	return 0;
   2607   1.3      onoe }
   2608   1.3      onoe 
   2609   1.3      onoe static int
   2610   1.3      onoe fwohci_if_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   2611   1.3      onoe {
   2612   1.4  jdolecek 	int n, len;
   2613   1.3      onoe 	struct mbuf *m;
   2614   1.3      onoe 	struct iovec *iov;
   2615   1.3      onoe 	void (*handler)(struct device *, struct mbuf *) = arg;
   2616   1.3      onoe 
   2617   1.3      onoe #ifdef FW_DEBUG
   2618  1.17      onoe 	if (fw_verbose > 1) {
   2619   1.8      onoe 		int i;
   2620   1.8      onoe 		printf("fwohci_if_input: tcode=0x%x, dlen=%d",
   2621   1.8      onoe 		    pkt->fp_tcode, pkt->fp_dlen);
   2622   1.9      onoe 		if (fw_dump) {
   2623   1.9      onoe 			for (i = 0; i < pkt->fp_hlen/4; i++)
   2624   1.9      onoe 				printf("%s%08x", i?" ":"\n\t", pkt->fp_hdr[i]);
   2625   1.8      onoe 			printf("$");
   2626   1.9      onoe 			for (n = 0, len = pkt->fp_dlen; len > 0; len -= i, n++){
   2627   1.9      onoe 				iov = &pkt->fp_iov[n];
   2628   1.9      onoe 				for (i = 0; i < iov->iov_len; i++)
   2629   1.9      onoe 					printf("%s%02x",
   2630   1.9      onoe 					    (i%32)?((i%4)?"":" "):"\n\t",
   2631   1.9      onoe 					    ((u_int8_t *)iov->iov_base)[i]);
   2632   1.9      onoe 				printf("$");
   2633   1.9      onoe 			}
   2634   1.8      onoe 		}
   2635   1.8      onoe 		printf("\n");
   2636   1.5      matt 	}
   2637   1.3      onoe #endif /* FW_DEBUG */
   2638   1.3      onoe 	len = pkt->fp_dlen;
   2639   1.3      onoe 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2640   1.3      onoe 	if (m == NULL)
   2641   1.3      onoe 		return IEEE1394_RCODE_COMPLETE;
   2642  1.15      onoe 	m->m_len = 16;
   2643   1.8      onoe 	if (len + m->m_len > MHLEN) {
   2644   1.3      onoe 		MCLGET(m, M_DONTWAIT);
   2645   1.3      onoe 		if ((m->m_flags & M_EXT) == 0) {
   2646   1.3      onoe 			m_freem(m);
   2647   1.3      onoe 			return IEEE1394_RCODE_COMPLETE;
   2648   1.3      onoe 		}
   2649   1.3      onoe 	}
   2650   1.8      onoe 	n = (pkt->fp_hdr[1] >> 16) & OHCI_NodeId_NodeNumber;
   2651  1.24       jmc         if (sc->sc_uidtbl == NULL || n > sc->sc_rootid ||
   2652   1.8      onoe 	    sc->sc_uidtbl[n].fu_valid != 0x3) {
   2653   1.8      onoe 		printf("%s: packet from unknown node: phy id %d\n",
   2654   1.8      onoe 		    sc->sc_sc1394.sc1394_dev.dv_xname, n);
   2655  1.24       jmc                 m_freem(m);
   2656   1.8      onoe 		return IEEE1394_RCODE_COMPLETE;
   2657   1.8      onoe 	}
   2658   1.8      onoe 	memcpy(mtod(m, caddr_t), sc->sc_uidtbl[n].fu_uid, 8);
   2659   1.8      onoe 	if (pkt->fp_tcode == IEEE1394_TCODE_STREAM_DATA) {
   2660   1.8      onoe 		m->m_flags |= M_BCAST;
   2661   1.8      onoe 		mtod(m, u_int32_t *)[2] = mtod(m, u_int32_t *)[3] = 0;
   2662   1.8      onoe 	} else {
   2663   1.8      onoe 		mtod(m, u_int32_t *)[2] = htonl(pkt->fp_hdr[1]);
   2664   1.8      onoe 		mtod(m, u_int32_t *)[3] = htonl(pkt->fp_hdr[2]);
   2665   1.8      onoe 	}
   2666   1.8      onoe 	mtod(m, u_int8_t *)[8] = n;	/*XXX: node id for debug */
   2667   1.8      onoe 	mtod(m, u_int8_t *)[9] =
   2668   1.8      onoe 	    (*pkt->fp_trail >> (16 + OHCI_CTXCTL_SPD_BITPOS)) &
   2669   1.8      onoe 	    ((1 << OHCI_CTXCTL_SPD_BITLEN) - 1);
   2670   1.8      onoe 
   2671   1.8      onoe 	m->m_pkthdr.rcvif = NULL;	/* set in child */
   2672   1.8      onoe 	m->m_pkthdr.len = len + m->m_len;
   2673   1.3      onoe 	/*
   2674   1.3      onoe 	 * We may use receive buffer by external mbuf instead of copy here.
   2675   1.3      onoe 	 * But asynchronous receive buffer must be operate in buffer fill
   2676   1.3      onoe 	 * mode, so that each receive buffer will shared by multiple mbufs.
   2677   1.3      onoe 	 * If upper layer doesn't free mbuf soon, e.g. application program
   2678   1.3      onoe 	 * is suspended, buffer must be reallocated.
   2679   1.3      onoe 	 * Isochronous buffer must be operate in packet buffer mode, and
   2680   1.3      onoe 	 * it is easy to map receive buffer to external mbuf.  But it is
   2681   1.3      onoe 	 * used for broadcast/multicast only, and is expected not so
   2682   1.3      onoe 	 * performance sensitive for now.
   2683   1.3      onoe 	 * XXX: The performance may be important for multicast case,
   2684   1.3      onoe 	 * so we should revisit here later.
   2685   1.3      onoe 	 *						-- onoe
   2686   1.3      onoe 	 */
   2687   1.3      onoe 	n = 0;
   2688   1.9      onoe 	iov = pkt->fp_uio.uio_iov;
   2689   1.3      onoe 	while (len > 0) {
   2690   1.3      onoe 		memcpy(mtod(m, caddr_t) + m->m_len, iov->iov_base,
   2691   1.3      onoe 		    iov->iov_len);
   2692   1.3      onoe 	        m->m_len += iov->iov_len;
   2693   1.3      onoe 	        len -= iov->iov_len;
   2694   1.3      onoe 		iov++;
   2695   1.3      onoe 	}
   2696   1.3      onoe 	(*handler)(sc->sc_sc1394.sc1394_if, m);
   2697   1.3      onoe 	return IEEE1394_RCODE_COMPLETE;
   2698   1.3      onoe }
   2699   1.3      onoe 
   2700   1.3      onoe static int
   2701   1.3      onoe fwohci_if_output(struct device *self, struct mbuf *m0,
   2702   1.3      onoe     void (*callback)(struct device *, struct mbuf *))
   2703   1.3      onoe {
   2704  1.24       jmc         struct fwohci_softc *sc = (struct fwohci_softc *)self;
   2705   1.3      onoe 	struct fwohci_pkt pkt;
   2706   1.3      onoe 	u_int8_t *p;
   2707  1.24       jmc 	int n, error, spd, hdrlen, maxrec;
   2708   1.8      onoe 
   2709   1.8      onoe 	p = mtod(m0, u_int8_t *);
   2710   1.9      onoe 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
   2711   1.8      onoe 		spd = IEEE1394_SPD_S100;	/*XXX*/
   2712   1.8      onoe 		maxrec = 512;			/*XXX*/
   2713   1.8      onoe 		hdrlen = 8;
   2714   1.8      onoe 	} else {
   2715   1.8      onoe 		n = fwohci_uid_lookup(sc, p);
   2716   1.8      onoe 		if (n < 0) {
   2717   1.8      onoe 			printf("%s: nodeid unknown:"
   2718   1.8      onoe 			    " %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
   2719   1.8      onoe 			    sc->sc_sc1394.sc1394_dev.dv_xname,
   2720   1.8      onoe 			    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
   2721   1.8      onoe 			error = EHOSTUNREACH;
   2722   1.8      onoe 			goto end;
   2723   1.8      onoe 		}
   2724   1.8      onoe 		if (n == IEEE1394_BCAST_PHY_ID) {
   2725  1.24       jmc                         printf("%s: broadcast with !M_MCAST\n",
   2726   1.8      onoe 			    sc->sc_sc1394.sc1394_dev.dv_xname);
   2727   1.8      onoe #ifdef FW_DEBUG
   2728   1.8      onoe 			if (fw_dump) {
   2729   1.9      onoe 				struct mbuf *m;
   2730   1.8      onoe 				printf("packet:");
   2731   1.8      onoe 				for (m = m0; m != NULL; m = m->m_next) {
   2732   1.8      onoe 					for (n = 0; n < m->m_len; n++)
   2733   1.8      onoe 						printf("%s%02x", (n%32)?
   2734   1.8      onoe 						    ((n%4)?"":" "):"\n\t",
   2735   1.8      onoe 						    mtod(m, u_int8_t *)[n]);
   2736   1.8      onoe 					printf("$");
   2737   1.8      onoe 				}
   2738   1.8      onoe 				printf("\n");
   2739   1.8      onoe 			}
   2740   1.8      onoe #endif
   2741   1.8      onoe 			error = EHOSTUNREACH;
   2742   1.8      onoe 			goto end;
   2743   1.8      onoe 		}
   2744   1.8      onoe 		maxrec = 2 << p[8];
   2745   1.8      onoe 		spd = p[9];
   2746   1.8      onoe 		hdrlen = 0;
   2747   1.8      onoe 	}
   2748  1.24       jmc         if (spd > sc->sc_sc1394.sc1394_link_speed) {
   2749   1.8      onoe #ifdef FW_DEBUG
   2750   1.8      onoe 		if (fw_verbose)
   2751   1.8      onoe 			printf("fwohci_if_output: spd (%d) is faster than %d\n",
   2752   1.8      onoe 			    spd, sc->sc_sc1394.sc1394_link_speed);
   2753   1.8      onoe #endif
   2754   1.8      onoe 		spd = sc->sc_sc1394.sc1394_link_speed;
   2755   1.8      onoe 	}
   2756  1.24       jmc         if (maxrec > (512 << spd)) {
   2757   1.8      onoe #ifdef FW_DEBUG
   2758   1.8      onoe 		if (fw_verbose)
   2759   1.8      onoe 			printf("fwohci_if_output: maxrec (%d) is larger for"
   2760   1.8      onoe 			" spd (%d)\n", maxrec, spd);
   2761   1.8      onoe #endif
   2762   1.8      onoe 		maxrec = 512 << spd;
   2763   1.8      onoe 	}
   2764   1.8      onoe 	while (maxrec > sc->sc_sc1394.sc1394_max_receive) {
   2765   1.8      onoe #ifdef FW_DEBUG
   2766   1.8      onoe 		if (fw_verbose)
   2767   1.8      onoe 			printf("fwohci_if_output: maxrec (%d) is larger than"
   2768   1.8      onoe 			    " %d\n", maxrec, sc->sc_sc1394.sc1394_max_receive);
   2769   1.8      onoe #endif
   2770   1.8      onoe 		maxrec >>= 1;
   2771   1.8      onoe 	}
   2772   1.8      onoe 	if (maxrec < 512) {
   2773   1.8      onoe #ifdef FW_DEBUG
   2774   1.8      onoe 		if (fw_verbose)
   2775   1.8      onoe 			printf("fwohci_if_output: maxrec (%d) is smaller"
   2776   1.8      onoe 			    " than minimum\n", maxrec);
   2777   1.8      onoe #endif
   2778   1.8      onoe 		maxrec = 512;
   2779   1.8      onoe 	}
   2780   1.8      onoe 
   2781   1.8      onoe 	m_adj(m0, 16 - hdrlen);
   2782   1.8      onoe 	if (m0->m_pkthdr.len > maxrec) {
   2783   1.8      onoe #ifdef FW_DEBUG
   2784   1.8      onoe 		if (fw_verbose)
   2785   1.8      onoe 			printf("fwohci_if_output: packet too big:"
   2786   1.8      onoe 			    " hdr %d, pktlen %d, maxrec %d\n",
   2787   1.8      onoe 			    hdrlen, m0->m_pkthdr.len, maxrec);
   2788   1.8      onoe #endif
   2789   1.8      onoe 		error = E2BIG;	/*XXX*/
   2790   1.8      onoe 		goto end;
   2791   1.8      onoe 	}
   2792   1.3      onoe 
   2793   1.3      onoe 	memset(&pkt, 0, sizeof(pkt));
   2794   1.9      onoe 	pkt.fp_uio.uio_iov = pkt.fp_iov;
   2795   1.9      onoe 	pkt.fp_uio.uio_segflg = UIO_SYSSPACE;
   2796   1.9      onoe 	pkt.fp_uio.uio_rw = UIO_WRITE;
   2797   1.9      onoe 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
   2798   1.3      onoe 		/* construct GASP header */
   2799   1.3      onoe 		p = mtod(m0, u_int8_t *);
   2800   1.3      onoe 		p[0] = sc->sc_nodeid >> 8;
   2801   1.3      onoe 		p[1] = sc->sc_nodeid & 0xff;
   2802   1.3      onoe 		p[2] = 0x00; p[3] = 0x00; p[4] = 0x5e;
   2803   1.3      onoe 		p[5] = 0x00; p[6] = 0x00; p[7] = 0x01;
   2804   1.3      onoe 		pkt.fp_tcode = IEEE1394_TCODE_STREAM_DATA;
   2805   1.3      onoe 		pkt.fp_hlen = 8;
   2806   1.8      onoe 		pkt.fp_hdr[0] = (spd << 16) | (IEEE1394_TAG_GASP << 14) |
   2807   1.3      onoe 		    ((sc->sc_csr[CSR_SB_BROADCAST_CHANNEL] &
   2808   1.3      onoe 		    OHCI_NodeId_NodeNumber) << 8);
   2809   1.3      onoe 		pkt.fp_hdr[1] = m0->m_pkthdr.len << 16;
   2810   1.3      onoe 	} else {
   2811   1.3      onoe 		pkt.fp_tcode = IEEE1394_TCODE_WRITE_REQ_BLOCK;
   2812   1.3      onoe 		pkt.fp_hlen = 16;
   2813   1.3      onoe 		pkt.fp_hdr[0] = 0x00800100 | (sc->sc_tlabel << 10) |
   2814   1.8      onoe 		    (spd << 16);
   2815   1.3      onoe 		pkt.fp_hdr[1] =
   2816   1.3      onoe 		    (((sc->sc_nodeid & OHCI_NodeId_BusNumber) | n) << 16) |
   2817   1.3      onoe 		    (p[10] << 8) | p[11];
   2818   1.3      onoe 		pkt.fp_hdr[2] = (p[12]<<24) | (p[13]<<16) | (p[14]<<8) | p[15];
   2819   1.3      onoe 		pkt.fp_hdr[3] = m0->m_pkthdr.len << 16;
   2820   1.3      onoe 		sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   2821   1.3      onoe 	}
   2822   1.3      onoe 	pkt.fp_hdr[0] |= (pkt.fp_tcode << 4);
   2823   1.3      onoe 	pkt.fp_dlen = m0->m_pkthdr.len;
   2824   1.3      onoe 	pkt.fp_m = m0;
   2825   1.3      onoe 	pkt.fp_callback = callback;
   2826   1.3      onoe 	error = fwohci_at_output(sc, sc->sc_ctx_atrq, &pkt);
   2827   1.9      onoe 	m0 = pkt.fp_m;
   2828   1.3      onoe   end:
   2829  1.15      onoe 	if (m0 != NULL) {
   2830   1.3      onoe 		if (callback)
   2831   1.3      onoe 			(*callback)(sc->sc_sc1394.sc1394_if, m0);
   2832   1.3      onoe 		else
   2833   1.3      onoe 			m_freem(m0);
   2834   1.3      onoe 	}
   2835   1.3      onoe 	return error;
   2836  1.24       jmc }
   2837  1.24       jmc 
   2838  1.24       jmc /*
   2839  1.24       jmc  * High level routines to provide abstraction to attaching layers to
   2840  1.24       jmc  * send/receive data.
   2841  1.24       jmc  */
   2842  1.24       jmc 
   2843  1.24       jmc static int
   2844  1.24       jmc fwohci_input(struct ieee1394_abuf *ab)
   2845  1.24       jmc {
   2846  1.24       jmc         struct fwohci_pkt pkt;
   2847  1.24       jmc         struct ieee1394_softc *sc = ab->ab_node;
   2848  1.24       jmc         struct fwohci_softc *psc =
   2849  1.24       jmc             (struct fwohci_softc *)sc->sc1394_dev.dv_parent;
   2850  1.24       jmc         u_int32_t high, lo;
   2851  1.24       jmc         int rv, tcode;
   2852  1.24       jmc 
   2853  1.24       jmc         high = ((ab->ab_csr & 0x0000ffff00000000) >> 32);
   2854  1.24       jmc         lo = (ab->ab_csr & 0x00000000ffffffff);
   2855  1.24       jmc 
   2856  1.24       jmc 	memset(&pkt, 0, sizeof(pkt));
   2857  1.24       jmc         pkt.fp_hdr[1] = ((0xffc0 | ab->ab_node->sc1394_node_id) << 16) | high;
   2858  1.24       jmc         pkt.fp_hdr[2] = lo;
   2859  1.24       jmc         pkt.fp_dlen = 0;
   2860  1.24       jmc 
   2861  1.24       jmc         if (ab->ab_length == 4) {
   2862  1.24       jmc             pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   2863  1.24       jmc             tcode = IEEE1394_TCODE_READ_RESP_QUAD;
   2864  1.24       jmc             pkt.fp_hlen = 12;
   2865  1.24       jmc         } else {
   2866  1.24       jmc             pkt.fp_tcode = IEEE1394_TCODE_READ_REQ_BLOCK;
   2867  1.24       jmc             pkt.fp_hlen = 16;
   2868  1.24       jmc             tcode = IEEE1394_TCODE_READ_RESP_BLOCK;
   2869  1.24       jmc             pkt.fp_hdr[3] = (ab->ab_length << 16);
   2870  1.24       jmc         }
   2871  1.24       jmc         pkt.fp_hdr[0] = 0x00000100 | (sc->sc1394_link_speed << 16) |
   2872  1.24       jmc             (psc->sc_tlabel << 10) | (pkt.fp_tcode << 4);
   2873  1.24       jmc 
   2874  1.24       jmc         rv = fwohci_handler_set(psc, tcode, ab->ab_node->sc1394_node_id,
   2875  1.24       jmc             psc->sc_tlabel, fwohci_extract_resp, ab);
   2876  1.24       jmc         if (rv)
   2877  1.24       jmc             return rv;
   2878  1.24       jmc         psc->sc_tlabel = (psc->sc_tlabel + 1) & 0x3f;
   2879  1.24       jmc         rv = fwohci_at_output(psc, psc->sc_ctx_atrq, &pkt);
   2880  1.24       jmc         return rv;
   2881  1.24       jmc }
   2882  1.24       jmc 
   2883  1.24       jmc static int
   2884  1.24       jmc fwohci_output(struct ieee1394_abuf *ab)
   2885  1.24       jmc {
   2886  1.24       jmc         struct fwohci_pkt pkt;
   2887  1.24       jmc         struct ieee1394_softc *sc = ab->ab_node;
   2888  1.24       jmc         struct fwohci_softc *psc =
   2889  1.24       jmc             (struct fwohci_softc *)sc->sc1394_dev.dv_parent;
   2890  1.24       jmc         u_int32_t high, lo;
   2891  1.24       jmc         int rv;
   2892  1.24       jmc 
   2893  1.24       jmc         if (ab->ab_length > sc->sc1394_max_receive) {
   2894  1.24       jmc #ifdef FW_DEBUG
   2895  1.24       jmc             if (fw_verbose)
   2896  1.24       jmc                 printf("Packet too large: %d\n", ab->ab_length);
   2897  1.24       jmc #endif
   2898  1.24       jmc             return E2BIG;
   2899  1.24       jmc         }
   2900  1.24       jmc 
   2901  1.24       jmc         memset(&pkt, 0, sizeof(pkt));
   2902  1.24       jmc 
   2903  1.24       jmc         pkt.fp_tcode = ab->ab_tcode;
   2904  1.24       jmc         pkt.fp_uio.uio_iov = pkt.fp_iov;
   2905  1.24       jmc 	pkt.fp_uio.uio_segflg = UIO_SYSSPACE;
   2906  1.24       jmc 	pkt.fp_uio.uio_rw = UIO_WRITE;
   2907  1.24       jmc 
   2908  1.24       jmc 	switch (ab->ab_tcode) {
   2909  1.24       jmc         case IEEE1394_TCODE_WRITE_RESP:
   2910  1.24       jmc             pkt.fp_hlen = 12;
   2911  1.24       jmc         case IEEE1394_TCODE_READ_RESP_QUAD:
   2912  1.24       jmc         case IEEE1394_TCODE_READ_RESP_BLOCK:
   2913  1.24       jmc             if (!pkt.fp_hlen)
   2914  1.24       jmc                 pkt.fp_hlen = 16;
   2915  1.24       jmc             high = ab->ab_retlen;
   2916  1.24       jmc             ab->ab_retlen = 0;
   2917  1.24       jmc             lo = 0;
   2918  1.24       jmc             pkt.fp_hdr[0] = 0x00000100 | (sc->sc1394_link_speed << 16) |
   2919  1.24       jmc                 (ab->ab_tlabel << 10) | (pkt.fp_tcode << 4);
   2920  1.24       jmc             break;
   2921  1.24       jmc         default:
   2922  1.24       jmc             pkt.fp_hlen = 16;
   2923  1.24       jmc             high = ((ab->ab_csr & 0x0000ffff00000000) >> 32);
   2924  1.24       jmc             lo = (ab->ab_csr & 0x00000000ffffffff);
   2925  1.24       jmc             pkt.fp_hdr[0] = 0x00000100 | (sc->sc1394_link_speed << 16) |
   2926  1.24       jmc                 (psc->sc_tlabel << 10) | (pkt.fp_tcode << 4);
   2927  1.24       jmc             break;
   2928  1.24       jmc         }
   2929  1.24       jmc 
   2930  1.24       jmc         pkt.fp_hdr[1] = ((0xffc0 | ab->ab_node->sc1394_node_id) << 16) | high;
   2931  1.24       jmc         pkt.fp_hdr[2] = lo;
   2932  1.24       jmc         if (pkt.fp_hlen == 16) {
   2933  1.24       jmc             if (ab->ab_length == 4) {
   2934  1.24       jmc                 pkt.fp_hdr[3] = ab->ab_data[0];
   2935  1.24       jmc                 pkt.fp_dlen = 0;
   2936  1.24       jmc             }  else {
   2937  1.24       jmc                 pkt.fp_hdr[3] = (ab->ab_length << 16);
   2938  1.24       jmc                 pkt.fp_dlen = ab->ab_length;
   2939  1.24       jmc                 pkt.fp_uio.uio_iovcnt = 1;
   2940  1.24       jmc                 pkt.fp_uio.uio_resid = ab->ab_length;
   2941  1.24       jmc                 pkt.fp_iov[0].iov_base = ab->ab_data;
   2942  1.24       jmc                 pkt.fp_iov[0].iov_len = ab->ab_length;
   2943  1.24       jmc             }
   2944  1.24       jmc         }
   2945  1.24       jmc         switch (ab->ab_tcode) {
   2946  1.24       jmc         case IEEE1394_TCODE_WRITE_RESP:
   2947  1.24       jmc         case IEEE1394_TCODE_READ_RESP_QUAD:
   2948  1.24       jmc         case IEEE1394_TCODE_READ_RESP_BLOCK:
   2949  1.24       jmc             rv = fwohci_at_output(psc, psc->sc_ctx_atrs, &pkt);
   2950  1.24       jmc             break;
   2951  1.24       jmc         default:
   2952  1.24       jmc             rv = fwohci_handler_set(psc, IEEE1394_TCODE_WRITE_RESP,
   2953  1.24       jmc                 ab->ab_node->sc1394_node_id, psc->sc_tlabel, fwohci_extract_resp,
   2954  1.24       jmc                 ab);
   2955  1.24       jmc             if (rv)
   2956  1.24       jmc                 return rv;
   2957  1.24       jmc             psc->sc_tlabel = (psc->sc_tlabel + 1) & 0x3f;
   2958  1.24       jmc             rv = fwohci_at_output(psc, psc->sc_ctx_atrq, &pkt);
   2959  1.24       jmc             break;
   2960  1.24       jmc         }
   2961  1.24       jmc         return rv;
   2962  1.24       jmc }
   2963  1.24       jmc 
   2964  1.24       jmc static int
   2965  1.24       jmc fwohci_extract_resp(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   2966  1.24       jmc {
   2967  1.24       jmc         struct ieee1394_abuf *ab = (struct ieee1394_abuf *)arg;
   2968  1.24       jmc         struct fwohci_pkt newpkt;
   2969  1.24       jmc         u_int32_t *cur, high, lo;
   2970  1.24       jmc         int i, rcode, rv;
   2971  1.24       jmc 
   2972  1.24       jmc 	/* No callback just means we want to have something clean up the abuf. */
   2973  1.24       jmc         if (!ab->ab_cb) {
   2974  1.24       jmc             if (ab->ab_data)
   2975  1.24       jmc                 free (ab->ab_data, M_1394DATA);
   2976  1.24       jmc             if (ab)
   2977  1.24       jmc                 free (ab, M_1394DATA);
   2978  1.24       jmc             return 0;
   2979  1.24       jmc         }
   2980  1.24       jmc 
   2981  1.24       jmc         rcode = (pkt->fp_hdr[1] & 0x0000f000) >> 12;
   2982  1.24       jmc 
   2983  1.24       jmc         /* Some area's (like the config rom want to be read as quadlets only. */
   2984  1.24       jmc 
   2985  1.24       jmc         if (((rcode == IEEE1394_RCODE_TYPE_ERROR) ||
   2986  1.24       jmc 	     (rcode == IEEE1394_RCODE_ADDRESS_ERROR)) &&
   2987  1.24       jmc             (pkt->fp_tcode == IEEE1394_TCODE_READ_RESP_BLOCK)) {
   2988  1.24       jmc 
   2989  1.24       jmc             /* Read the area in quadlet chunks (internally track this). */
   2990  1.24       jmc 
   2991  1.24       jmc             memset(&newpkt, 0, sizeof(newpkt));
   2992  1.24       jmc 
   2993  1.24       jmc             high = ((ab->ab_csr & 0x0000ffff00000000) >> 32);
   2994  1.24       jmc             lo = (ab->ab_csr & 0x00000000ffffffff);
   2995  1.24       jmc 
   2996  1.24       jmc             newpkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   2997  1.24       jmc             newpkt.fp_hlen = 12;
   2998  1.24       jmc             newpkt.fp_dlen = 0;
   2999  1.24       jmc             newpkt.fp_hdr[1] = ((0xffc0 | ab->ab_node->sc1394_node_id) << 16) |
   3000  1.24       jmc                 high;
   3001  1.24       jmc             newpkt.fp_hdr[2] = lo;
   3002  1.24       jmc             newpkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   3003  1.24       jmc                 (newpkt.fp_tcode << 4);
   3004  1.24       jmc 
   3005  1.24       jmc             rv = fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD,
   3006  1.24       jmc                 ab->ab_node->sc1394_node_id, sc->sc_tlabel,
   3007  1.24       jmc                 fwohci_multi_resp, ab);
   3008  1.24       jmc             if (rv)
   3009  1.24       jmc                 return rv;
   3010  1.24       jmc             sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   3011  1.24       jmc             printf("Calling at_output\n");
   3012  1.24       jmc             fwohci_at_output(sc, sc->sc_ctx_atrq, &newpkt);
   3013  1.24       jmc         } else {
   3014  1.24       jmc 
   3015  1.24       jmc             /* Recombine all the iov data into 1 chunk for higher level code. */
   3016  1.24       jmc 
   3017  1.24       jmc             cur = ab->ab_data;
   3018  1.24       jmc             for (i = 0; i < pkt->fp_uio.uio_iovcnt; i++) {
   3019  1.24       jmc                 /* Make sure and don't exceed the buffer allocated for return. */
   3020  1.24       jmc                 if ((ab->ab_retlen + pkt->fp_iov[i].iov_len) > ab->ab_length) {
   3021  1.24       jmc                     memcpy(cur, pkt->fp_iov[i].iov_base,
   3022  1.24       jmc                         (ab->ab_length - ab->ab_retlen));
   3023  1.24       jmc                     ab->ab_retlen = ab->ab_length;
   3024  1.24       jmc                     break;
   3025  1.24       jmc                 }
   3026  1.24       jmc                 memcpy (cur, pkt->fp_iov[i].iov_base, pkt->fp_iov[i].iov_len);
   3027  1.24       jmc                 cur += pkt->fp_iov[i].iov_len;
   3028  1.24       jmc                 ab->ab_retlen += pkt->fp_iov[i].iov_len;
   3029  1.24       jmc             }
   3030  1.24       jmc             (*ab->ab_cb)(ab, rcode);
   3031  1.24       jmc         }
   3032  1.24       jmc 	return IEEE1394_RCODE_COMPLETE;
   3033  1.24       jmc }
   3034  1.24       jmc 
   3035  1.24       jmc static int
   3036  1.24       jmc fwohci_multi_resp(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   3037  1.24       jmc {
   3038  1.24       jmc         struct ieee1394_abuf *ab = (struct ieee1394_abuf *)arg;
   3039  1.24       jmc         struct fwohci_pkt newpkt;
   3040  1.24       jmc         u_int32_t high, lo;
   3041  1.24       jmc         int rcode, rv;
   3042  1.24       jmc 
   3043  1.24       jmc         /*
   3044  1.24       jmc          * Bad return codes from the wire, just return what's already in the
   3045  1.24       jmc          * buf.
   3046  1.24       jmc          */
   3047  1.24       jmc 
   3048  1.24       jmc         rcode = (pkt->fp_hdr[1] & 0x0000f000) >> 12;
   3049  1.24       jmc 
   3050  1.24       jmc         if (rcode) {
   3051  1.24       jmc             (*ab->ab_cb)(ab, rcode);
   3052  1.24       jmc             return rcode;
   3053  1.24       jmc         }
   3054  1.24       jmc 
   3055  1.24       jmc         if ((ab->ab_retlen + pkt->fp_iov[0].iov_len) > ab->ab_length) {
   3056  1.24       jmc             memcpy(((char *)ab->ab_data + ab->ab_retlen), pkt->fp_iov[0].iov_base,
   3057  1.24       jmc                (ab->ab_length - ab->ab_retlen));
   3058  1.24       jmc             ab->ab_retlen = ab->ab_length;
   3059  1.24       jmc         } else {
   3060  1.24       jmc             memcpy(((char *)ab->ab_data + ab->ab_retlen), pkt->fp_iov[0].iov_base,
   3061  1.24       jmc                 4);
   3062  1.24       jmc             ab->ab_retlen += 4;
   3063  1.24       jmc         }
   3064  1.24       jmc         /* Still more, loop and read 4 more bytes. */
   3065  1.24       jmc         if (ab->ab_retlen < ab->ab_length) {
   3066  1.24       jmc             memset(&newpkt, 0, sizeof(newpkt));
   3067  1.24       jmc 
   3068  1.24       jmc             high = ((ab->ab_csr & 0x0000ffff00000000) >> 32);
   3069  1.24       jmc             lo = (ab->ab_csr & 0x00000000ffffffff) + ab->ab_retlen;
   3070  1.24       jmc 
   3071  1.24       jmc 
   3072  1.24       jmc             newpkt.fp_tcode = IEEE1394_TCODE_READ_REQ_QUAD;
   3073  1.24       jmc             newpkt.fp_hlen = 12;
   3074  1.24       jmc             newpkt.fp_dlen = 0;
   3075  1.24       jmc             newpkt.fp_hdr[1] = ((0xffc0 | ab->ab_node->sc1394_node_id) << 16) |
   3076  1.24       jmc                 high;
   3077  1.24       jmc             newpkt.fp_hdr[2] = lo;
   3078  1.24       jmc             newpkt.fp_hdr[0] = 0x00000100 | (sc->sc_tlabel << 10) |
   3079  1.24       jmc                 (newpkt.fp_tcode << 4);
   3080  1.24       jmc 
   3081  1.24       jmc             /* Bad return code..Just give up and return what's come in now. */
   3082  1.24       jmc             rv = fwohci_handler_set(sc, IEEE1394_TCODE_READ_RESP_QUAD,
   3083  1.24       jmc                 ab->ab_node->sc1394_node_id, sc->sc_tlabel,
   3084  1.24       jmc                 fwohci_multi_resp, ab);
   3085  1.24       jmc             if (rv) {
   3086  1.24       jmc                 (*ab->ab_cb)(ab, rcode);
   3087  1.24       jmc                 return IEEE1394_RCODE_DATA_ERROR;
   3088  1.24       jmc             }
   3089  1.24       jmc             sc->sc_tlabel = (sc->sc_tlabel + 1) & 0x3f;
   3090  1.24       jmc             rv = fwohci_at_output(sc, sc->sc_ctx_atrq, &newpkt);
   3091  1.24       jmc             if (rv) {
   3092  1.24       jmc                 (*ab->ab_cb)(ab, rcode);
   3093  1.24       jmc                 return IEEE1394_RCODE_DATA_ERROR;
   3094  1.24       jmc             }
   3095  1.24       jmc         } else
   3096  1.24       jmc             (*ab->ab_cb)(ab, rcode);
   3097  1.24       jmc         return IEEE1394_RCODE_COMPLETE;
   3098  1.24       jmc }
   3099  1.24       jmc 
   3100  1.24       jmc static int
   3101  1.24       jmc fwohci_inreg(struct ieee1394_abuf *ab, int allow)
   3102  1.24       jmc {
   3103  1.24       jmc     struct ieee1394_softc *sc = ab->ab_node;
   3104  1.24       jmc     struct fwohci_softc *psc =
   3105  1.24       jmc         (struct fwohci_softc *)sc->sc1394_dev.dv_parent;
   3106  1.24       jmc     u_int32_t high, lo;
   3107  1.24       jmc     int i, rv;
   3108  1.24       jmc 
   3109  1.24       jmc     high = ((ab->ab_csr & 0x0000ffff00000000) >> 32);
   3110  1.24       jmc     lo = (ab->ab_csr & 0x00000000ffffffff);
   3111  1.24       jmc 
   3112  1.24       jmc     switch (ab->ab_tcode) {
   3113  1.24       jmc     case IEEE1394_TCODE_READ_REQ_QUAD:
   3114  1.24       jmc     case IEEE1394_TCODE_WRITE_REQ_QUAD:
   3115  1.24       jmc         rv = fwohci_handler_set(psc, ab->ab_tcode, high, lo, fwohci_parse_input,
   3116  1.24       jmc             ab);
   3117  1.24       jmc         break;
   3118  1.24       jmc     case IEEE1394_TCODE_READ_REQ_BLOCK:
   3119  1.24       jmc     case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   3120  1.24       jmc         if (allow) {
   3121  1.24       jmc             for (i = 0; i < (ab->ab_length / 4); i++) {
   3122  1.24       jmc                 rv = fwohci_handler_set(psc, ab->ab_tcode, high, lo + (i * 4),
   3123  1.24       jmc                     fwohci_parse_input, ab);
   3124  1.24       jmc                 if (rv)
   3125  1.24       jmc                     return rv;
   3126  1.24       jmc             }
   3127  1.24       jmc             ab->ab_data = (void *)1;
   3128  1.24       jmc         } else
   3129  1.24       jmc             rv = fwohci_handler_set(psc, ab->ab_tcode, high, lo,
   3130  1.24       jmc                 fwohci_parse_input, ab);
   3131  1.24       jmc         break;
   3132  1.24       jmc     default:
   3133  1.24       jmc #ifdef FW_DEBUG
   3134  1.24       jmc         if (fw_verbose)
   3135  1.24       jmc             printf("Invalid registration tcode: %d\n", ab->ab_tcode);
   3136  1.24       jmc #endif
   3137  1.24       jmc         return -1;
   3138  1.24       jmc         break;
   3139  1.24       jmc     }
   3140  1.24       jmc     return rv;
   3141  1.24       jmc }
   3142  1.24       jmc 
   3143  1.24       jmc static int
   3144  1.24       jmc fwohci_parse_input(struct fwohci_softc *sc, void *arg, struct fwohci_pkt *pkt)
   3145  1.24       jmc {
   3146  1.24       jmc     struct ieee1394_abuf *ab = (struct ieee1394_abuf *)arg;
   3147  1.24       jmc     u_int64_t csr;
   3148  1.24       jmc     u_int32_t *cur;
   3149  1.24       jmc     int i, count;
   3150  1.24       jmc 
   3151  1.24       jmc     ab->ab_tcode = (pkt->fp_hdr[0] >> 4) & 0xf;
   3152  1.24       jmc     ab->ab_tlabel = (pkt->fp_hdr[0] >> 10) & 0x3f;
   3153  1.24       jmc     csr = (((u_int64_t)(pkt->fp_hdr[1] & 0xffff) << 32) | pkt->fp_hdr[2]);
   3154  1.24       jmc 
   3155  1.24       jmc     switch (ab->ab_tcode) {
   3156  1.24       jmc     case IEEE1394_TCODE_READ_REQ_QUAD:
   3157  1.24       jmc         ab->ab_retlen = 4;
   3158  1.24       jmc         break;
   3159  1.24       jmc     case IEEE1394_TCODE_READ_REQ_BLOCK:
   3160  1.24       jmc         ab->ab_retlen = (pkt->fp_hdr[3] >> 16) & 0xffff;
   3161  1.24       jmc         printf("csr: 0x%016qx retlen: %d\n", csr, ab->ab_retlen);
   3162  1.24       jmc         if (ab->ab_data) {
   3163  1.24       jmc             if ((csr + ab->ab_retlen) > (ab->ab_csr + ab->ab_length))
   3164  1.24       jmc                 return IEEE1394_RCODE_ADDRESS_ERROR;
   3165  1.24       jmc             ab->ab_data = NULL;
   3166  1.24       jmc         } else
   3167  1.24       jmc             if (ab->ab_retlen != ab->ab_length)
   3168  1.24       jmc                 return IEEE1394_RCODE_ADDRESS_ERROR;
   3169  1.24       jmc         break;
   3170  1.24       jmc     case IEEE1394_TCODE_WRITE_REQ_QUAD:
   3171  1.24       jmc         ab->ab_retlen = 4;
   3172  1.24       jmc     case IEEE1394_TCODE_WRITE_REQ_BLOCK:
   3173  1.24       jmc         if (!ab->ab_retlen)
   3174  1.24       jmc             ab->ab_retlen = (pkt->fp_hdr[3] >> 16) & 0xffff;
   3175  1.24       jmc         if (ab->ab_data) {
   3176  1.24       jmc             if ((csr + ab->ab_retlen) > (ab->ab_csr + ab->ab_length))
   3177  1.24       jmc                 return IEEE1394_RCODE_ADDRESS_ERROR;
   3178  1.24       jmc             ab->ab_data = NULL;
   3179  1.24       jmc         } else
   3180  1.24       jmc             if (ab->ab_retlen != ab->ab_length)
   3181  1.24       jmc                 return IEEE1394_RCODE_ADDRESS_ERROR;
   3182  1.24       jmc 
   3183  1.24       jmc         ab->ab_data = malloc(ab->ab_retlen, M_1394DATA, M_WAITOK);
   3184  1.24       jmc         if (ab->ab_tcode == IEEE1394_TCODE_WRITE_REQ_QUAD)
   3185  1.24       jmc             ab->ab_data[0] = pkt->fp_hdr[3];
   3186  1.24       jmc         else {
   3187  1.24       jmc             count = 0;
   3188  1.24       jmc             cur = ab->ab_data;
   3189  1.24       jmc             for (i = 0; i < pkt->fp_uio.uio_iovcnt; i++) {
   3190  1.24       jmc                 memcpy (cur, pkt->fp_iov[i].iov_base, pkt->fp_iov[i].iov_len);
   3191  1.24       jmc                 cur += pkt->fp_iov[i].iov_len;
   3192  1.24       jmc                 count += pkt->fp_iov[i].iov_len;
   3193  1.24       jmc             }
   3194  1.24       jmc             if (ab->ab_retlen != count)
   3195  1.24       jmc                 panic ("Packet claims %d length but only %d bytes returned\n",
   3196  1.24       jmc                        ab->ab_retlen, count);
   3197  1.24       jmc         }
   3198  1.24       jmc         break;
   3199  1.24       jmc     default:
   3200  1.24       jmc         panic("Got a callback for a tcode that wasn't requested: %d\n",
   3201  1.24       jmc               ab->ab_tcode);
   3202  1.24       jmc         break;
   3203  1.24       jmc     }
   3204  1.24       jmc     ab->ab_csr = csr;
   3205  1.24       jmc     ab->ab_cb(ab, IEEE1394_RCODE_COMPLETE);
   3206  1.24       jmc     return -1;
   3207   1.1      matt }
   3208