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