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