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