Home | History | Annotate | Line # | Download | only in i2o
iop.c revision 1.19.4.2
      1  1.19.4.2  he /*	$NetBSD: iop.c,v 1.19.4.2 2001/10/25 18:00:39 he Exp $	*/
      2  1.19.4.2  he 
      3  1.19.4.2  he /*-
      4  1.19.4.2  he  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
      5  1.19.4.2  he  * All rights reserved.
      6  1.19.4.2  he  *
      7  1.19.4.2  he  * This code is derived from software contributed to The NetBSD Foundation
      8  1.19.4.2  he  * by Andrew Doran.
      9  1.19.4.2  he  *
     10  1.19.4.2  he  * Redistribution and use in source and binary forms, with or without
     11  1.19.4.2  he  * modification, are permitted provided that the following conditions
     12  1.19.4.2  he  * are met:
     13  1.19.4.2  he  * 1. Redistributions of source code must retain the above copyright
     14  1.19.4.2  he  *    notice, this list of conditions and the following disclaimer.
     15  1.19.4.2  he  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.19.4.2  he  *    notice, this list of conditions and the following disclaimer in the
     17  1.19.4.2  he  *    documentation and/or other materials provided with the distribution.
     18  1.19.4.2  he  * 3. All advertising materials mentioning features or use of this software
     19  1.19.4.2  he  *    must display the following acknowledgement:
     20  1.19.4.2  he  *        This product includes software developed by the NetBSD
     21  1.19.4.2  he  *        Foundation, Inc. and its contributors.
     22  1.19.4.2  he  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.19.4.2  he  *    contributors may be used to endorse or promote products derived
     24  1.19.4.2  he  *    from this software without specific prior written permission.
     25  1.19.4.2  he  *
     26  1.19.4.2  he  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.19.4.2  he  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.19.4.2  he  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.19.4.2  he  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.19.4.2  he  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.19.4.2  he  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.19.4.2  he  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.19.4.2  he  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.19.4.2  he  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.19.4.2  he  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.19.4.2  he  * POSSIBILITY OF SUCH DAMAGE.
     37  1.19.4.2  he  */
     38  1.19.4.2  he 
     39  1.19.4.2  he /*
     40  1.19.4.2  he  * Support for I2O IOPs (intelligent I/O processors).
     41  1.19.4.2  he  */
     42  1.19.4.2  he 
     43  1.19.4.2  he #include "opt_i2o.h"
     44  1.19.4.2  he #include "iop.h"
     45  1.19.4.2  he 
     46  1.19.4.2  he #include <sys/param.h>
     47  1.19.4.2  he #include <sys/systm.h>
     48  1.19.4.2  he #include <sys/kernel.h>
     49  1.19.4.2  he #include <sys/device.h>
     50  1.19.4.2  he #include <sys/queue.h>
     51  1.19.4.2  he #include <sys/proc.h>
     52  1.19.4.2  he #include <sys/malloc.h>
     53  1.19.4.2  he #include <sys/ioctl.h>
     54  1.19.4.2  he #include <sys/endian.h>
     55  1.19.4.2  he #include <sys/conf.h>
     56  1.19.4.2  he #include <sys/kthread.h>
     57  1.19.4.2  he 
     58  1.19.4.2  he #include <machine/vmparam.h>
     59  1.19.4.2  he #include <machine/bus.h>
     60  1.19.4.2  he 
     61  1.19.4.2  he #include <vm/vm.h>
     62  1.19.4.2  he 
     63  1.19.4.2  he #include <dev/i2o/i2o.h>
     64  1.19.4.2  he #include <dev/i2o/iopio.h>
     65  1.19.4.2  he #include <dev/i2o/iopreg.h>
     66  1.19.4.2  he #include <dev/i2o/iopvar.h>
     67  1.19.4.2  he 
     68  1.19.4.2  he #define POLL(ms, cond)				\
     69  1.19.4.2  he do {						\
     70  1.19.4.2  he 	int i;					\
     71  1.19.4.2  he 	for (i = (ms) * 10; i; i--) {		\
     72  1.19.4.2  he 		if (cond)			\
     73  1.19.4.2  he 			break;			\
     74  1.19.4.2  he 		DELAY(100);			\
     75  1.19.4.2  he 	}					\
     76  1.19.4.2  he } while (/* CONSTCOND */0);
     77  1.19.4.2  he 
     78  1.19.4.2  he #ifdef I2ODEBUG
     79  1.19.4.2  he #define DPRINTF(x)	printf x
     80  1.19.4.2  he #else
     81  1.19.4.2  he #define	DPRINTF(x)
     82  1.19.4.2  he #endif
     83  1.19.4.2  he 
     84  1.19.4.2  he #ifdef I2OVERBOSE
     85  1.19.4.2  he #define IFVERBOSE(x)	x
     86  1.19.4.2  he #define	COMMENT(x)	NULL
     87  1.19.4.2  he #else
     88  1.19.4.2  he #define	IFVERBOSE(x)
     89  1.19.4.2  he #define	COMMENT(x)
     90  1.19.4.2  he #endif
     91  1.19.4.2  he 
     92  1.19.4.2  he #define IOP_ICTXHASH_NBUCKETS	16
     93  1.19.4.2  he #define	IOP_ICTXHASH(ictx)	(&iop_ictxhashtbl[(ictx) & iop_ictxhash])
     94  1.19.4.2  he 
     95  1.19.4.2  he #define	IOP_MAX_SEGS	(((IOP_MAX_XFER + NBPG - 1) / NBPG) + 1)
     96  1.19.4.2  he 
     97  1.19.4.2  he #define	IOP_TCTX_SHIFT	12
     98  1.19.4.2  he #define	IOP_TCTX_MASK	((1 << IOP_TCTX_SHIFT) - 1)
     99  1.19.4.2  he 
    100  1.19.4.2  he static LIST_HEAD(, iop_initiator) *iop_ictxhashtbl;
    101  1.19.4.2  he static u_long	iop_ictxhash;
    102  1.19.4.2  he static void	*iop_sdh;
    103  1.19.4.2  he static struct	i2o_systab *iop_systab;
    104  1.19.4.2  he static int	iop_systab_size;
    105  1.19.4.2  he 
    106  1.19.4.2  he extern struct cfdriver iop_cd;
    107  1.19.4.2  he 
    108  1.19.4.2  he #define	IC_CONFIGURE	0x01
    109  1.19.4.2  he #define	IC_PRIORITY	0x02
    110  1.19.4.2  he 
    111  1.19.4.2  he struct iop_class {
    112  1.19.4.2  he 	u_short	ic_class;
    113  1.19.4.2  he 	u_short	ic_flags;
    114  1.19.4.2  he #ifdef I2OVERBOSE
    115  1.19.4.2  he 	const char	*ic_caption;
    116  1.19.4.2  he #endif
    117  1.19.4.2  he } static const iop_class[] = {
    118  1.19.4.2  he 	{
    119  1.19.4.2  he 		I2O_CLASS_EXECUTIVE,
    120  1.19.4.2  he 		0,
    121  1.19.4.2  he 		COMMENT("executive")
    122  1.19.4.2  he 	},
    123  1.19.4.2  he 	{
    124  1.19.4.2  he 		I2O_CLASS_DDM,
    125  1.19.4.2  he 		0,
    126  1.19.4.2  he 		COMMENT("device driver module")
    127  1.19.4.2  he 	},
    128  1.19.4.2  he 	{
    129  1.19.4.2  he 		I2O_CLASS_RANDOM_BLOCK_STORAGE,
    130  1.19.4.2  he 		IC_CONFIGURE | IC_PRIORITY,
    131  1.19.4.2  he 		IFVERBOSE("random block storage")
    132  1.19.4.2  he 	},
    133  1.19.4.2  he 	{
    134  1.19.4.2  he 		I2O_CLASS_SEQUENTIAL_STORAGE,
    135  1.19.4.2  he 		IC_CONFIGURE | IC_PRIORITY,
    136  1.19.4.2  he 		IFVERBOSE("sequential storage")
    137  1.19.4.2  he 	},
    138  1.19.4.2  he 	{
    139  1.19.4.2  he 		I2O_CLASS_LAN,
    140  1.19.4.2  he 		IC_CONFIGURE | IC_PRIORITY,
    141  1.19.4.2  he 		IFVERBOSE("LAN port")
    142  1.19.4.2  he 	},
    143  1.19.4.2  he 	{
    144  1.19.4.2  he 		I2O_CLASS_WAN,
    145  1.19.4.2  he 		IC_CONFIGURE | IC_PRIORITY,
    146  1.19.4.2  he 		IFVERBOSE("WAN port")
    147  1.19.4.2  he 	},
    148  1.19.4.2  he 	{
    149  1.19.4.2  he 		I2O_CLASS_FIBRE_CHANNEL_PORT,
    150  1.19.4.2  he 		IC_CONFIGURE,
    151  1.19.4.2  he 		IFVERBOSE("fibrechannel port")
    152  1.19.4.2  he 	},
    153  1.19.4.2  he 	{
    154  1.19.4.2  he 		I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL,
    155  1.19.4.2  he 		0,
    156  1.19.4.2  he 		COMMENT("fibrechannel peripheral")
    157  1.19.4.2  he 	},
    158  1.19.4.2  he  	{
    159  1.19.4.2  he  		I2O_CLASS_SCSI_PERIPHERAL,
    160  1.19.4.2  he  		0,
    161  1.19.4.2  he  		COMMENT("SCSI peripheral")
    162  1.19.4.2  he  	},
    163  1.19.4.2  he 	{
    164  1.19.4.2  he 		I2O_CLASS_ATE_PORT,
    165  1.19.4.2  he 		IC_CONFIGURE,
    166  1.19.4.2  he 		IFVERBOSE("ATE port")
    167  1.19.4.2  he 	},
    168  1.19.4.2  he 	{
    169  1.19.4.2  he 		I2O_CLASS_ATE_PERIPHERAL,
    170  1.19.4.2  he 		0,
    171  1.19.4.2  he 		COMMENT("ATE peripheral")
    172  1.19.4.2  he 	},
    173  1.19.4.2  he 	{
    174  1.19.4.2  he 		I2O_CLASS_FLOPPY_CONTROLLER,
    175  1.19.4.2  he 		IC_CONFIGURE,
    176  1.19.4.2  he 		IFVERBOSE("floppy controller")
    177  1.19.4.2  he 	},
    178  1.19.4.2  he 	{
    179  1.19.4.2  he 		I2O_CLASS_FLOPPY_DEVICE,
    180  1.19.4.2  he 		0,
    181  1.19.4.2  he 		COMMENT("floppy device")
    182  1.19.4.2  he 	},
    183  1.19.4.2  he 	{
    184  1.19.4.2  he 		I2O_CLASS_BUS_ADAPTER_PORT,
    185  1.19.4.2  he 		IC_CONFIGURE,
    186  1.19.4.2  he 		IFVERBOSE("bus adapter port" )
    187  1.19.4.2  he 	},
    188  1.19.4.2  he };
    189  1.19.4.2  he 
    190  1.19.4.2  he #if defined(I2ODEBUG) && defined(I2OVERBOSE)
    191  1.19.4.2  he static const char * const iop_status[] = {
    192  1.19.4.2  he 	"success",
    193  1.19.4.2  he 	"abort (dirty)",
    194  1.19.4.2  he 	"abort (no data transfer)",
    195  1.19.4.2  he 	"abort (partial transfer)",
    196  1.19.4.2  he 	"error (dirty)",
    197  1.19.4.2  he 	"error (no data transfer)",
    198  1.19.4.2  he 	"error (partial transfer)",
    199  1.19.4.2  he 	"undefined error code",
    200  1.19.4.2  he 	"process abort (dirty)",
    201  1.19.4.2  he 	"process abort (no data transfer)",
    202  1.19.4.2  he 	"process abort (partial transfer)",
    203  1.19.4.2  he 	"transaction error",
    204  1.19.4.2  he };
    205  1.19.4.2  he #endif
    206  1.19.4.2  he 
    207  1.19.4.2  he static inline u_int32_t	iop_inl(struct iop_softc *, int);
    208  1.19.4.2  he static inline void	iop_outl(struct iop_softc *, int, u_int32_t);
    209  1.19.4.2  he 
    210  1.19.4.2  he static void	iop_config_interrupts(struct device *);
    211  1.19.4.2  he static void	iop_configure_devices(struct iop_softc *, int, int);
    212  1.19.4.2  he static void	iop_devinfo(int, char *);
    213  1.19.4.2  he static int	iop_print(void *, const char *);
    214  1.19.4.2  he static int	iop_reconfigure(struct iop_softc *, u_int);
    215  1.19.4.2  he static void	iop_shutdown(void *);
    216  1.19.4.2  he static int	iop_submatch(struct device *, struct cfdata *, void *);
    217  1.19.4.2  he #ifdef notyet
    218  1.19.4.2  he static int	iop_vendor_print(void *, const char *);
    219  1.19.4.2  he #endif
    220  1.19.4.2  he 
    221  1.19.4.2  he static void	iop_adjqparam(struct iop_softc *, int);
    222  1.19.4.2  he static void	iop_create_reconf_thread(void *);
    223  1.19.4.2  he static int	iop_handle_reply(struct iop_softc *, u_int32_t);
    224  1.19.4.2  he static int	iop_hrt_get(struct iop_softc *);
    225  1.19.4.2  he static int	iop_hrt_get0(struct iop_softc *, struct i2o_hrt *, int);
    226  1.19.4.2  he static void	iop_intr_event(struct device *, struct iop_msg *, void *);
    227  1.19.4.2  he static int	iop_lct_get0(struct iop_softc *, struct i2o_lct *, int,
    228  1.19.4.2  he 			     u_int32_t);
    229  1.19.4.2  he static void	iop_msg_poll(struct iop_softc *, struct iop_msg *, int);
    230  1.19.4.2  he static void	iop_msg_wait(struct iop_softc *, struct iop_msg *, int);
    231  1.19.4.2  he static int	iop_ofifo_init(struct iop_softc *);
    232  1.19.4.2  he static int	iop_passthrough(struct iop_softc *, struct ioppt *,
    233  1.19.4.2  he 				struct proc *);
    234  1.19.4.2  he static void	iop_reconf_thread(void *);
    235  1.19.4.2  he static void	iop_release_mfa(struct iop_softc *, u_int32_t);
    236  1.19.4.2  he static int	iop_reset(struct iop_softc *);
    237  1.19.4.2  he static int	iop_status_get(struct iop_softc *, int);
    238  1.19.4.2  he static int	iop_systab_set(struct iop_softc *);
    239  1.19.4.2  he static void	iop_tfn_print(struct iop_softc *, struct i2o_fault_notify *);
    240  1.19.4.2  he 
    241  1.19.4.2  he #ifdef I2ODEBUG
    242  1.19.4.2  he static void	iop_reply_print(struct iop_softc *, struct i2o_reply *);
    243  1.19.4.2  he #endif
    244  1.19.4.2  he 
    245  1.19.4.2  he cdev_decl(iop);
    246  1.19.4.2  he 
    247  1.19.4.2  he static inline u_int32_t
    248  1.19.4.2  he iop_inl(struct iop_softc *sc, int off)
    249  1.19.4.2  he {
    250  1.19.4.2  he 
    251  1.19.4.2  he 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    252  1.19.4.2  he 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    253  1.19.4.2  he 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
    254  1.19.4.2  he }
    255  1.19.4.2  he 
    256  1.19.4.2  he static inline void
    257  1.19.4.2  he iop_outl(struct iop_softc *sc, int off, u_int32_t val)
    258  1.19.4.2  he {
    259  1.19.4.2  he 
    260  1.19.4.2  he 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    261  1.19.4.2  he 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    262  1.19.4.2  he 	    BUS_SPACE_BARRIER_WRITE);
    263  1.19.4.2  he }
    264  1.19.4.2  he 
    265  1.19.4.2  he /*
    266  1.19.4.2  he  * Initialise the IOP and our interface.
    267  1.19.4.2  he  */
    268  1.19.4.2  he void
    269  1.19.4.2  he iop_init(struct iop_softc *sc, const char *intrstr)
    270  1.19.4.2  he {
    271  1.19.4.2  he 	struct iop_msg *im;
    272  1.19.4.2  he 	int rv, i, j, state, nsegs;
    273  1.19.4.2  he 	u_int32_t mask;
    274  1.19.4.2  he 	char ident[64];
    275  1.19.4.2  he 
    276  1.19.4.2  he 	state = 0;
    277  1.19.4.2  he 
    278  1.19.4.2  he 	printf("I2O adapter");
    279  1.19.4.2  he 
    280  1.19.4.2  he 	if (iop_ictxhashtbl == NULL)
    281  1.19.4.2  he 		iop_ictxhashtbl = hashinit(IOP_ICTXHASH_NBUCKETS,
    282  1.19.4.2  he 		    M_DEVBUF, M_NOWAIT, &iop_ictxhash);
    283  1.19.4.2  he 
    284  1.19.4.2  he 	/* Disable interrupts at the IOP. */
    285  1.19.4.2  he 	mask = iop_inl(sc, IOP_REG_INTR_MASK);
    286  1.19.4.2  he 	iop_outl(sc, IOP_REG_INTR_MASK, mask | IOP_INTR_OFIFO);
    287  1.19.4.2  he 
    288  1.19.4.2  he 	/* Allocate a scratch DMA map for small miscellaneous shared data. */
    289  1.19.4.2  he 	if (bus_dmamap_create(sc->sc_dmat, NBPG, 1, NBPG, 0,
    290  1.19.4.2  he 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->sc_scr_dmamap) != 0) {
    291  1.19.4.2  he 		printf("%s: cannot create scratch dmamap\n",
    292  1.19.4.2  he 		    sc->sc_dv.dv_xname);
    293  1.19.4.2  he 		return;
    294  1.19.4.2  he 	}
    295  1.19.4.2  he 	state++;
    296  1.19.4.2  he 
    297  1.19.4.2  he 	if (bus_dmamem_alloc(sc->sc_dmat, NBPG, NBPG, 0,
    298  1.19.4.2  he 	    sc->sc_scr_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
    299  1.19.4.2  he 		printf("%s: cannot alloc scratch dmamem\n",
    300  1.19.4.2  he 		    sc->sc_dv.dv_xname);
    301  1.19.4.2  he 		goto bail_out;
    302  1.19.4.2  he 	}
    303  1.19.4.2  he 	state++;
    304  1.19.4.2  he 
    305  1.19.4.2  he 	if (bus_dmamem_map(sc->sc_dmat, sc->sc_scr_seg, nsegs, NBPG,
    306  1.19.4.2  he 	    &sc->sc_scr, 0)) {
    307  1.19.4.2  he 		printf("%s: cannot map scratch dmamem\n", sc->sc_dv.dv_xname);
    308  1.19.4.2  he 		goto bail_out;
    309  1.19.4.2  he 	}
    310  1.19.4.2  he 	state++;
    311  1.19.4.2  he 
    312  1.19.4.2  he 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_scr_dmamap, sc->sc_scr,
    313  1.19.4.2  he 	    NBPG, NULL, BUS_DMA_NOWAIT)) {
    314  1.19.4.2  he 		printf("%s: cannot load scratch dmamap\n", sc->sc_dv.dv_xname);
    315  1.19.4.2  he 		goto bail_out;
    316  1.19.4.2  he 	}
    317  1.19.4.2  he 	state++;
    318  1.19.4.2  he 
    319  1.19.4.2  he 	/* Reset the adapter and request status. */
    320  1.19.4.2  he  	if ((rv = iop_reset(sc)) != 0) {
    321  1.19.4.2  he  		printf("%s: not responding (reset)\n", sc->sc_dv.dv_xname);
    322  1.19.4.2  he 		goto bail_out;
    323  1.19.4.2  he  	}
    324  1.19.4.2  he 
    325  1.19.4.2  he  	if ((rv = iop_status_get(sc, 1)) != 0) {
    326  1.19.4.2  he 		printf("%s: not responding (get status)\n",
    327  1.19.4.2  he 		    sc->sc_dv.dv_xname);
    328  1.19.4.2  he 		goto bail_out;
    329  1.19.4.2  he  	}
    330  1.19.4.2  he 
    331  1.19.4.2  he 	sc->sc_flags |= IOP_HAVESTATUS;
    332  1.19.4.2  he 	iop_strvis(sc, sc->sc_status.productid, sizeof(sc->sc_status.productid),
    333  1.19.4.2  he 	    ident, sizeof(ident));
    334  1.19.4.2  he 	printf(" <%s>\n", ident);
    335  1.19.4.2  he 
    336  1.19.4.2  he #ifdef I2ODEBUG
    337  1.19.4.2  he 	printf("%s: orgid=0x%04x version=%d\n", sc->sc_dv.dv_xname,
    338  1.19.4.2  he 	    le16toh(sc->sc_status.orgid),
    339  1.19.4.2  he 	    (le32toh(sc->sc_status.segnumber) >> 12) & 15);
    340  1.19.4.2  he 	printf("%s: type want have cbase\n", sc->sc_dv.dv_xname);
    341  1.19.4.2  he 	printf("%s: mem  %04x %04x %08x\n", sc->sc_dv.dv_xname,
    342  1.19.4.2  he 	    le32toh(sc->sc_status.desiredprivmemsize),
    343  1.19.4.2  he 	    le32toh(sc->sc_status.currentprivmemsize),
    344  1.19.4.2  he 	    le32toh(sc->sc_status.currentprivmembase));
    345  1.19.4.2  he 	printf("%s: i/o  %04x %04x %08x\n", sc->sc_dv.dv_xname,
    346  1.19.4.2  he 	    le32toh(sc->sc_status.desiredpriviosize),
    347  1.19.4.2  he 	    le32toh(sc->sc_status.currentpriviosize),
    348  1.19.4.2  he 	    le32toh(sc->sc_status.currentpriviobase));
    349  1.19.4.2  he #endif
    350  1.19.4.2  he 
    351  1.19.4.2  he 	sc->sc_maxob = le32toh(sc->sc_status.maxoutboundmframes);
    352  1.19.4.2  he 	if (sc->sc_maxob > IOP_MAX_OUTBOUND)
    353  1.19.4.2  he 		sc->sc_maxob = IOP_MAX_OUTBOUND;
    354  1.19.4.2  he 	sc->sc_maxib = le32toh(sc->sc_status.maxinboundmframes);
    355  1.19.4.2  he 	if (sc->sc_maxib > IOP_MAX_INBOUND)
    356  1.19.4.2  he 		sc->sc_maxib = IOP_MAX_INBOUND;
    357  1.19.4.2  he 
    358  1.19.4.2  he 	/* Allocate message wrappers. */
    359  1.19.4.2  he 	im = malloc(sizeof(*im) * sc->sc_maxib, M_DEVBUF, M_NOWAIT);
    360  1.19.4.2  he 	memset(im, 0, sizeof(*im) * sc->sc_maxib);
    361  1.19.4.2  he 	sc->sc_ims = im;
    362  1.19.4.2  he 	SLIST_INIT(&sc->sc_im_freelist);
    363  1.19.4.2  he 
    364  1.19.4.2  he 	for (i = 0, state++; i < sc->sc_maxib; i++, im++) {
    365  1.19.4.2  he 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
    366  1.19.4.2  he 		    IOP_MAX_SEGS, IOP_MAX_XFER, 0,
    367  1.19.4.2  he 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    368  1.19.4.2  he 		    &im->im_xfer[0].ix_map);
    369  1.19.4.2  he 		if (rv != 0) {
    370  1.19.4.2  he 			printf("%s: couldn't create dmamap (%d)",
    371  1.19.4.2  he 			    sc->sc_dv.dv_xname, rv);
    372  1.19.4.2  he 			goto bail_out;
    373  1.19.4.2  he 		}
    374  1.19.4.2  he 
    375  1.19.4.2  he 		im->im_tctx = i;
    376  1.19.4.2  he 		SLIST_INSERT_HEAD(&sc->sc_im_freelist, im, im_chain);
    377  1.19.4.2  he 	}
    378  1.19.4.2  he 
    379  1.19.4.2  he 	/* Initalise the IOP's outbound FIFO. */
    380  1.19.4.2  he 	if (iop_ofifo_init(sc) != 0) {
    381  1.19.4.2  he 		printf("%s: unable to init oubound FIFO\n",
    382  1.19.4.2  he 		    sc->sc_dv.dv_xname);
    383  1.19.4.2  he 		goto bail_out;
    384  1.19.4.2  he 	}
    385  1.19.4.2  he 
    386  1.19.4.2  he 	/*
    387  1.19.4.2  he  	 * Defer further configuration until (a) interrupts are working and
    388  1.19.4.2  he  	 * (b) we have enough information to build the system table.
    389  1.19.4.2  he  	 */
    390  1.19.4.2  he 	config_interrupts((struct device *)sc, iop_config_interrupts);
    391  1.19.4.2  he 
    392  1.19.4.2  he 	/* Configure shutdown hook before we start any device activity. */
    393  1.19.4.2  he 	if (iop_sdh == NULL)
    394  1.19.4.2  he 		iop_sdh = shutdownhook_establish(iop_shutdown, NULL);
    395  1.19.4.2  he 
    396  1.19.4.2  he 	/* Ensure interrupts are enabled at the IOP. */
    397  1.19.4.2  he 	mask = iop_inl(sc, IOP_REG_INTR_MASK);
    398  1.19.4.2  he 	iop_outl(sc, IOP_REG_INTR_MASK, mask & ~IOP_INTR_OFIFO);
    399  1.19.4.2  he 
    400  1.19.4.2  he 	if (intrstr != NULL)
    401  1.19.4.2  he 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    402  1.19.4.2  he 		    intrstr);
    403  1.19.4.2  he 
    404  1.19.4.2  he #ifdef I2ODEBUG
    405  1.19.4.2  he 	printf("%s: queue depths: inbound %d/%d, outbound %d/%d\n",
    406  1.19.4.2  he 	    sc->sc_dv.dv_xname, sc->sc_maxib,
    407  1.19.4.2  he 	    le32toh(sc->sc_status.maxinboundmframes),
    408  1.19.4.2  he 	    sc->sc_maxob, le32toh(sc->sc_status.maxoutboundmframes));
    409  1.19.4.2  he #endif
    410  1.19.4.2  he 
    411  1.19.4.2  he 	lockinit(&sc->sc_conflock, PRIBIO, "iopconf", hz * 30, 0);
    412  1.19.4.2  he 	return;
    413  1.19.4.2  he 
    414  1.19.4.2  he  bail_out:
    415  1.19.4.2  he  	if (state > 3) {
    416  1.19.4.2  he 		for (j = 0; j < i; j++)
    417  1.19.4.2  he 			bus_dmamap_destroy(sc->sc_dmat,
    418  1.19.4.2  he 			    sc->sc_ims[j].im_xfer[0].ix_map);
    419  1.19.4.2  he 		free(sc->sc_ims, M_DEVBUF);
    420  1.19.4.2  he 	}
    421  1.19.4.2  he 	if (state > 2)
    422  1.19.4.2  he 		bus_dmamap_unload(sc->sc_dmat, sc->sc_scr_dmamap);
    423  1.19.4.2  he 	if (state > 1)
    424  1.19.4.2  he 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_scr, NBPG);
    425  1.19.4.2  he 	if (state > 0)
    426  1.19.4.2  he 		bus_dmamem_free(sc->sc_dmat, sc->sc_scr_seg, nsegs);
    427  1.19.4.2  he 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_scr_dmamap);
    428  1.19.4.2  he 
    429  1.19.4.2  he }
    430  1.19.4.2  he 
    431  1.19.4.2  he /*
    432  1.19.4.2  he  * Perform autoconfiguration tasks.
    433  1.19.4.2  he  */
    434  1.19.4.2  he static void
    435  1.19.4.2  he iop_config_interrupts(struct device *self)
    436  1.19.4.2  he {
    437  1.19.4.2  he 	struct iop_softc *sc, *iop;
    438  1.19.4.2  he 	struct i2o_systab_entry *ste;
    439  1.19.4.2  he 	int rv, i, niop;
    440  1.19.4.2  he 
    441  1.19.4.2  he 	sc = (struct iop_softc *)self;
    442  1.19.4.2  he 	LIST_INIT(&sc->sc_iilist);
    443  1.19.4.2  he 
    444  1.19.4.2  he 	printf("%s: configuring...\n", sc->sc_dv.dv_xname);
    445  1.19.4.2  he 
    446  1.19.4.2  he 	if (iop_hrt_get(sc) != 0) {
    447  1.19.4.2  he 		printf("%s: unable to retrieve HRT\n", sc->sc_dv.dv_xname);
    448  1.19.4.2  he 		return;
    449  1.19.4.2  he 	}
    450  1.19.4.2  he 
    451  1.19.4.2  he 	/*
    452  1.19.4.2  he  	 * Build the system table.
    453  1.19.4.2  he  	 */
    454  1.19.4.2  he 	if (iop_systab == NULL) {
    455  1.19.4.2  he 		for (i = 0, niop = 0; i < iop_cd.cd_ndevs; i++) {
    456  1.19.4.2  he 			if ((iop = device_lookup(&iop_cd, i)) == NULL)
    457  1.19.4.2  he 				continue;
    458  1.19.4.2  he 			if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
    459  1.19.4.2  he 				continue;
    460  1.19.4.2  he 			if (iop_status_get(iop, 1) != 0) {
    461  1.19.4.2  he 				printf("%s: unable to retrieve status\n",
    462  1.19.4.2  he 				    sc->sc_dv.dv_xname);
    463  1.19.4.2  he 				iop->sc_flags &= ~IOP_HAVESTATUS;
    464  1.19.4.2  he 				continue;
    465  1.19.4.2  he 			}
    466  1.19.4.2  he 			niop++;
    467  1.19.4.2  he 		}
    468  1.19.4.2  he 		if (niop == 0)
    469  1.19.4.2  he 			return;
    470  1.19.4.2  he 
    471  1.19.4.2  he 		i = sizeof(struct i2o_systab_entry) * (niop - 1) +
    472  1.19.4.2  he 		    sizeof(struct i2o_systab);
    473  1.19.4.2  he 		iop_systab_size = i;
    474  1.19.4.2  he 		iop_systab = malloc(i, M_DEVBUF, M_NOWAIT);
    475  1.19.4.2  he 
    476  1.19.4.2  he 		memset(iop_systab, 0, i);
    477  1.19.4.2  he 		iop_systab->numentries = niop;
    478  1.19.4.2  he 		iop_systab->version = I2O_VERSION_11;
    479  1.19.4.2  he 
    480  1.19.4.2  he 		for (i = 0, ste = iop_systab->entry; i < iop_cd.cd_ndevs; i++) {
    481  1.19.4.2  he 			if ((iop = device_lookup(&iop_cd, i)) == NULL)
    482  1.19.4.2  he 				continue;
    483  1.19.4.2  he 			if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
    484  1.19.4.2  he 				continue;
    485  1.19.4.2  he 
    486  1.19.4.2  he 			ste->orgid = iop->sc_status.orgid;
    487  1.19.4.2  he 			ste->iopid = iop->sc_dv.dv_unit + 2;
    488  1.19.4.2  he 			ste->segnumber =
    489  1.19.4.2  he 			    htole32(le32toh(iop->sc_status.segnumber) & ~4095);
    490  1.19.4.2  he 			ste->iopcaps = iop->sc_status.iopcaps;
    491  1.19.4.2  he 			ste->inboundmsgframesize =
    492  1.19.4.2  he 			    iop->sc_status.inboundmframesize;
    493  1.19.4.2  he 			ste->inboundmsgportaddresslow =
    494  1.19.4.2  he 			    htole32(iop->sc_memaddr + IOP_REG_IFIFO);
    495  1.19.4.2  he 			ste++;
    496  1.19.4.2  he 		}
    497  1.19.4.2  he 	}
    498  1.19.4.2  he 
    499  1.19.4.2  he 	/*
    500  1.19.4.2  he 	 * Post the system table to the IOP and bring it to the OPERATIONAL
    501  1.19.4.2  he 	 * state.
    502  1.19.4.2  he 	 */
    503  1.19.4.2  he 	if (iop_systab_set(sc) != 0) {
    504  1.19.4.2  he 		printf("%s: unable to set system table\n", sc->sc_dv.dv_xname);
    505  1.19.4.2  he 		return;
    506  1.19.4.2  he 	}
    507  1.19.4.2  he 	if (iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_ENABLE, IOP_ICTX, 1,
    508  1.19.4.2  he 	    30000) != 0) {
    509  1.19.4.2  he 		printf("%s: unable to enable system\n", sc->sc_dv.dv_xname);
    510  1.19.4.2  he 		return;
    511  1.19.4.2  he 	}
    512  1.19.4.2  he 
    513  1.19.4.2  he 	/*
    514  1.19.4.2  he 	 * Set up an event handler for this IOP.
    515  1.19.4.2  he 	 */
    516  1.19.4.2  he 	sc->sc_eventii.ii_dv = self;
    517  1.19.4.2  he 	sc->sc_eventii.ii_intr = iop_intr_event;
    518  1.19.4.2  he 	sc->sc_eventii.ii_flags = II_NOTCTX | II_UTILITY;
    519  1.19.4.2  he 	sc->sc_eventii.ii_tid = I2O_TID_IOP;
    520  1.19.4.2  he 	iop_initiator_register(sc, &sc->sc_eventii);
    521  1.19.4.2  he 
    522  1.19.4.2  he 	rv = iop_util_eventreg(sc, &sc->sc_eventii,
    523  1.19.4.2  he 	    I2O_EVENT_EXEC_RESOURCE_LIMITS |
    524  1.19.4.2  he 	    I2O_EVENT_EXEC_CONNECTION_FAIL |
    525  1.19.4.2  he 	    I2O_EVENT_EXEC_ADAPTER_FAULT |
    526  1.19.4.2  he 	    I2O_EVENT_EXEC_POWER_FAIL |
    527  1.19.4.2  he 	    I2O_EVENT_EXEC_RESET_PENDING |
    528  1.19.4.2  he 	    I2O_EVENT_EXEC_RESET_IMMINENT |
    529  1.19.4.2  he 	    I2O_EVENT_EXEC_HARDWARE_FAIL |
    530  1.19.4.2  he 	    I2O_EVENT_EXEC_XCT_CHANGE |
    531  1.19.4.2  he 	    I2O_EVENT_EXEC_DDM_AVAILIBILITY |
    532  1.19.4.2  he 	    I2O_EVENT_GEN_DEVICE_RESET |
    533  1.19.4.2  he 	    I2O_EVENT_GEN_STATE_CHANGE |
    534  1.19.4.2  he 	    I2O_EVENT_GEN_GENERAL_WARNING);
    535  1.19.4.2  he 	if (rv != 0) {
    536  1.19.4.2  he 		printf("%s: unable to register for events", sc->sc_dv.dv_xname);
    537  1.19.4.2  he 		return;
    538  1.19.4.2  he 	}
    539  1.19.4.2  he 
    540  1.19.4.2  he #ifdef notyet
    541  1.19.4.2  he 	/* Attempt to match and attach a product-specific extension. */
    542  1.19.4.2  he 	ia.ia_class = I2O_CLASS_ANY;
    543  1.19.4.2  he 	ia.ia_tid = I2O_TID_IOP;
    544  1.19.4.2  he 	config_found_sm(self, &ia, iop_vendor_print, iop_submatch);
    545  1.19.4.2  he #endif
    546  1.19.4.2  he 
    547  1.19.4.2  he 	lockmgr(&sc->sc_conflock, LK_EXCLUSIVE, NULL);
    548  1.19.4.2  he 	if ((rv = iop_reconfigure(sc, 0)) == -1) {
    549  1.19.4.2  he 		printf("%s: configure failed (%d)\n", sc->sc_dv.dv_xname, rv);
    550  1.19.4.2  he 		return;
    551  1.19.4.2  he 	}
    552  1.19.4.2  he 	lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
    553  1.19.4.2  he 
    554  1.19.4.2  he 	kthread_create(iop_create_reconf_thread, sc);
    555  1.19.4.2  he }
    556  1.19.4.2  he 
    557  1.19.4.2  he /*
    558  1.19.4.2  he  * Create the reconfiguration thread.  Called after the standard kernel
    559  1.19.4.2  he  * threads have been created.
    560  1.19.4.2  he  */
    561  1.19.4.2  he static void
    562  1.19.4.2  he iop_create_reconf_thread(void *cookie)
    563  1.19.4.2  he {
    564  1.19.4.2  he 	struct iop_softc *sc;
    565  1.19.4.2  he 	int rv;
    566  1.19.4.2  he 
    567  1.19.4.2  he 	sc = cookie;
    568  1.19.4.2  he 	sc->sc_flags |= IOP_ONLINE;
    569  1.19.4.2  he 
    570  1.19.4.2  he 	rv = kthread_create1(iop_reconf_thread, sc, &sc->sc_reconf_proc,
    571  1.19.4.2  he  	    "%s", sc->sc_dv.dv_xname);
    572  1.19.4.2  he  	if (rv != 0) {
    573  1.19.4.2  he 		printf("%s: unable to create reconfiguration thread (%d)",
    574  1.19.4.2  he  		    sc->sc_dv.dv_xname, rv);
    575  1.19.4.2  he  		return;
    576  1.19.4.2  he  	}
    577  1.19.4.2  he }
    578  1.19.4.2  he 
    579  1.19.4.2  he /*
    580  1.19.4.2  he  * Reconfiguration thread; listens for LCT change notification, and
    581  1.19.4.2  he  * initiates re-configuration if received.
    582  1.19.4.2  he  */
    583  1.19.4.2  he static void
    584  1.19.4.2  he iop_reconf_thread(void *cookie)
    585  1.19.4.2  he {
    586  1.19.4.2  he 	struct iop_softc *sc;
    587  1.19.4.2  he 	struct i2o_lct lct;
    588  1.19.4.2  he 	u_int32_t chgind;
    589  1.19.4.2  he 	int rv;
    590  1.19.4.2  he 
    591  1.19.4.2  he 	sc = cookie;
    592  1.19.4.2  he 	chgind = sc->sc_chgind + 1;
    593  1.19.4.2  he 
    594  1.19.4.2  he 	for (;;) {
    595  1.19.4.2  he 		DPRINTF(("%s: async reconfig: requested 0x%08x\n",
    596  1.19.4.2  he 		    sc->sc_dv.dv_xname, chgind));
    597  1.19.4.2  he 
    598  1.19.4.2  he 		PHOLD(sc->sc_reconf_proc);
    599  1.19.4.2  he 		rv = iop_lct_get0(sc, &lct, sizeof(lct), chgind);
    600  1.19.4.2  he 		PRELE(sc->sc_reconf_proc);
    601  1.19.4.2  he 
    602  1.19.4.2  he 		DPRINTF(("%s: async reconfig: notified (0x%08x, %d)\n",
    603  1.19.4.2  he 		    sc->sc_dv.dv_xname, le32toh(lct.changeindicator), rv));
    604  1.19.4.2  he 
    605  1.19.4.2  he 		if (rv == 0 &&
    606  1.19.4.2  he 		    lockmgr(&sc->sc_conflock, LK_EXCLUSIVE, NULL) == 0) {
    607  1.19.4.2  he 			iop_reconfigure(sc, le32toh(lct.changeindicator));
    608  1.19.4.2  he 			chgind = sc->sc_chgind + 1;
    609  1.19.4.2  he 			lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
    610  1.19.4.2  he 		}
    611  1.19.4.2  he 
    612  1.19.4.2  he 		tsleep(iop_reconf_thread, PWAIT, "iopzzz", hz * 5);
    613  1.19.4.2  he 	}
    614  1.19.4.2  he }
    615  1.19.4.2  he 
    616  1.19.4.2  he /*
    617  1.19.4.2  he  * Reconfigure: find new and removed devices.
    618  1.19.4.2  he  */
    619  1.19.4.2  he static int
    620  1.19.4.2  he iop_reconfigure(struct iop_softc *sc, u_int chgind)
    621  1.19.4.2  he {
    622  1.19.4.2  he 	struct iop_msg *im;
    623  1.19.4.2  he 	struct i2o_hba_bus_scan mf;
    624  1.19.4.2  he 	struct i2o_lct_entry *le;
    625  1.19.4.2  he 	struct iop_initiator *ii, *nextii;
    626  1.19.4.2  he 	int rv, tid, i;
    627  1.19.4.2  he 
    628  1.19.4.2  he 	/*
    629  1.19.4.2  he 	 * If the reconfiguration request isn't the result of LCT change
    630  1.19.4.2  he 	 * notification, then be more thorough: ask all bus ports to scan
    631  1.19.4.2  he 	 * their busses.  Wait up to 5 minutes for each bus port to complete
    632  1.19.4.2  he 	 * the request.
    633  1.19.4.2  he 	 */
    634  1.19.4.2  he 	if (chgind == 0) {
    635  1.19.4.2  he 		if ((rv = iop_lct_get(sc)) != 0) {
    636  1.19.4.2  he 			DPRINTF(("iop_reconfigure: unable to read LCT\n"));
    637  1.19.4.2  he 			return (rv);
    638  1.19.4.2  he 		}
    639  1.19.4.2  he 
    640  1.19.4.2  he 		le = sc->sc_lct->entry;
    641  1.19.4.2  he 		for (i = 0; i < sc->sc_nlctent; i++, le++) {
    642  1.19.4.2  he 			if ((le16toh(le->classid) & 4095) !=
    643  1.19.4.2  he 			    I2O_CLASS_BUS_ADAPTER_PORT)
    644  1.19.4.2  he 				continue;
    645  1.19.4.2  he 			tid = le16toh(le->localtid) & 4095;
    646  1.19.4.2  he 
    647  1.19.4.2  he 			im = iop_msg_alloc(sc, IM_WAIT);
    648  1.19.4.2  he 
    649  1.19.4.2  he 			mf.msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    650  1.19.4.2  he 			mf.msgfunc = I2O_MSGFUNC(tid, I2O_HBA_BUS_SCAN);
    651  1.19.4.2  he 			mf.msgictx = IOP_ICTX;
    652  1.19.4.2  he 			mf.msgtctx = im->im_tctx;
    653  1.19.4.2  he 
    654  1.19.4.2  he 			DPRINTF(("%s: scanning bus %d\n", sc->sc_dv.dv_xname,
    655  1.19.4.2  he 			    tid));
    656  1.19.4.2  he 
    657  1.19.4.2  he 			rv = iop_msg_post(sc, im, &mf, 5*60*1000);
    658  1.19.4.2  he 			iop_msg_free(sc, im);
    659  1.19.4.2  he #ifdef I2ODEBUG
    660  1.19.4.2  he 			if (rv != 0)
    661  1.19.4.2  he 				printf("%s: bus scan failed\n",
    662  1.19.4.2  he 				    sc->sc_dv.dv_xname);
    663  1.19.4.2  he #endif
    664  1.19.4.2  he 		}
    665  1.19.4.2  he 	} else if (chgind <= sc->sc_chgind) {
    666  1.19.4.2  he 		DPRINTF(("%s: LCT unchanged (async)\n", sc->sc_dv.dv_xname));
    667  1.19.4.2  he 		return (0);
    668  1.19.4.2  he 	}
    669  1.19.4.2  he 
    670  1.19.4.2  he 	/* Re-read the LCT and determine if it has changed. */
    671  1.19.4.2  he 	if ((rv = iop_lct_get(sc)) != 0) {
    672  1.19.4.2  he 		DPRINTF(("iop_reconfigure: unable to re-read LCT\n"));
    673  1.19.4.2  he 		return (rv);
    674  1.19.4.2  he 	}
    675  1.19.4.2  he 	DPRINTF(("%s: %d LCT entries\n", sc->sc_dv.dv_xname, sc->sc_nlctent));
    676  1.19.4.2  he 
    677  1.19.4.2  he 	chgind = le32toh(sc->sc_lct->changeindicator);
    678  1.19.4.2  he 	if (chgind == sc->sc_chgind) {
    679  1.19.4.2  he 		DPRINTF(("%s: LCT unchanged\n", sc->sc_dv.dv_xname));
    680  1.19.4.2  he 		return (0);
    681  1.19.4.2  he 	}
    682  1.19.4.2  he 	DPRINTF(("%s: LCT changed\n", sc->sc_dv.dv_xname));
    683  1.19.4.2  he 	sc->sc_chgind = chgind;
    684  1.19.4.2  he 
    685  1.19.4.2  he 	if (sc->sc_tidmap != NULL)
    686  1.19.4.2  he 		free(sc->sc_tidmap, M_DEVBUF);
    687  1.19.4.2  he 	sc->sc_tidmap = malloc(sc->sc_nlctent * sizeof(struct iop_tidmap),
    688  1.19.4.2  he 	    M_DEVBUF, M_NOWAIT);
    689  1.19.4.2  he 	memset(sc->sc_tidmap, 0, sizeof(sc->sc_tidmap));
    690  1.19.4.2  he 
    691  1.19.4.2  he 	/* Allow 1 queued command per device while we're configuring. */
    692  1.19.4.2  he 	iop_adjqparam(sc, 1);
    693  1.19.4.2  he 
    694  1.19.4.2  he 	/*
    695  1.19.4.2  he 	 * Match and attach child devices.  We configure high-level devices
    696  1.19.4.2  he 	 * first so that any claims will propagate throughout the LCT,
    697  1.19.4.2  he 	 * hopefully masking off aliased devices as a result.
    698  1.19.4.2  he 	 *
    699  1.19.4.2  he 	 * Re-reading the LCT at this point is a little dangerous, but we'll
    700  1.19.4.2  he 	 * trust the IOP (and the operator) to behave itself...
    701  1.19.4.2  he 	 */
    702  1.19.4.2  he 	iop_configure_devices(sc, IC_CONFIGURE | IC_PRIORITY,
    703  1.19.4.2  he 	    IC_CONFIGURE | IC_PRIORITY);
    704  1.19.4.2  he 	if ((rv = iop_lct_get(sc)) != 0)
    705  1.19.4.2  he 		DPRINTF(("iop_reconfigure: unable to re-read LCT\n"));
    706  1.19.4.2  he 	iop_configure_devices(sc, IC_CONFIGURE | IC_PRIORITY,
    707  1.19.4.2  he 	    IC_CONFIGURE);
    708  1.19.4.2  he 
    709  1.19.4.2  he 	for (ii = LIST_FIRST(&sc->sc_iilist); ii != NULL; ii = nextii) {
    710  1.19.4.2  he 		nextii = LIST_NEXT(ii, ii_list);
    711  1.19.4.2  he 
    712  1.19.4.2  he 		/* Detach devices that were configured, but are now gone. */
    713  1.19.4.2  he 		for (i = 0; i < sc->sc_nlctent; i++)
    714  1.19.4.2  he 			if (ii->ii_tid == sc->sc_tidmap[i].it_tid)
    715  1.19.4.2  he 				break;
    716  1.19.4.2  he 		if (i == sc->sc_nlctent ||
    717  1.19.4.2  he 		    (sc->sc_tidmap[i].it_flags & IT_CONFIGURED) == 0)
    718  1.19.4.2  he 			config_detach(ii->ii_dv, DETACH_FORCE);
    719  1.19.4.2  he 
    720  1.19.4.2  he 		/*
    721  1.19.4.2  he 		 * Tell initiators that existed before the re-configuration
    722  1.19.4.2  he 		 * to re-configure.
    723  1.19.4.2  he 		 */
    724  1.19.4.2  he 		if (ii->ii_reconfig == NULL)
    725  1.19.4.2  he 			continue;
    726  1.19.4.2  he 		if ((rv = (*ii->ii_reconfig)(ii->ii_dv)) != 0)
    727  1.19.4.2  he 			printf("%s: %s failed reconfigure (%d)\n",
    728  1.19.4.2  he 			    sc->sc_dv.dv_xname, ii->ii_dv->dv_xname, rv);
    729  1.19.4.2  he 	}
    730  1.19.4.2  he 
    731  1.19.4.2  he 	/* Re-adjust queue parameters and return. */
    732  1.19.4.2  he 	if (sc->sc_nii != 0)
    733  1.19.4.2  he 		iop_adjqparam(sc, (sc->sc_maxib - sc->sc_nuii - IOP_MF_RESERVE)
    734  1.19.4.2  he 		    / sc->sc_nii);
    735  1.19.4.2  he 
    736  1.19.4.2  he 	return (0);
    737  1.19.4.2  he }
    738  1.19.4.2  he 
    739  1.19.4.2  he /*
    740  1.19.4.2  he  * Configure I2O devices into the system.
    741  1.19.4.2  he  */
    742  1.19.4.2  he static void
    743  1.19.4.2  he iop_configure_devices(struct iop_softc *sc, int mask, int maskval)
    744  1.19.4.2  he {
    745  1.19.4.2  he 	struct iop_attach_args ia;
    746  1.19.4.2  he 	struct iop_initiator *ii;
    747  1.19.4.2  he 	const struct i2o_lct_entry *le;
    748  1.19.4.2  he 	struct device *dv;
    749  1.19.4.2  he 	int i, j, nent;
    750  1.19.4.2  he 	u_int usertid;
    751  1.19.4.2  he 
    752  1.19.4.2  he 	nent = sc->sc_nlctent;
    753  1.19.4.2  he 	for (i = 0, le = sc->sc_lct->entry; i < nent; i++, le++) {
    754  1.19.4.2  he 		sc->sc_tidmap[i].it_tid = le16toh(le->localtid) & 4095;
    755  1.19.4.2  he 
    756  1.19.4.2  he 		/* Ignore the device if it's in use. */
    757  1.19.4.2  he 		usertid = le32toh(le->usertid) & 4095;
    758  1.19.4.2  he 		if (usertid != I2O_TID_NONE && usertid != I2O_TID_HOST)
    759  1.19.4.2  he 			continue;
    760  1.19.4.2  he 
    761  1.19.4.2  he 		ia.ia_class = le16toh(le->classid) & 4095;
    762  1.19.4.2  he 		ia.ia_tid = sc->sc_tidmap[i].it_tid;
    763  1.19.4.2  he 
    764  1.19.4.2  he 		/* Ignore uninteresting devices. */
    765  1.19.4.2  he 		for (j = 0; j < sizeof(iop_class) / sizeof(iop_class[0]); j++)
    766  1.19.4.2  he 			if (iop_class[j].ic_class == ia.ia_class)
    767  1.19.4.2  he 				break;
    768  1.19.4.2  he 		if (j < sizeof(iop_class) / sizeof(iop_class[0]) &&
    769  1.19.4.2  he 		    (iop_class[j].ic_flags & mask) != maskval)
    770  1.19.4.2  he 			continue;
    771  1.19.4.2  he 
    772  1.19.4.2  he 		/*
    773  1.19.4.2  he 		 * Try to configure the device only if it's not already
    774  1.19.4.2  he 		 * configured.
    775  1.19.4.2  he  		 */
    776  1.19.4.2  he  		LIST_FOREACH(ii, &sc->sc_iilist, ii_list) {
    777  1.19.4.2  he  			if (ia.ia_tid == ii->ii_tid) {
    778  1.19.4.2  he 				sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
    779  1.19.4.2  he 				strcpy(sc->sc_tidmap[i].it_dvname,
    780  1.19.4.2  he 				    ii->ii_dv->dv_xname);
    781  1.19.4.2  he  				break;
    782  1.19.4.2  he 			}
    783  1.19.4.2  he 		}
    784  1.19.4.2  he 		if (ii != NULL)
    785  1.19.4.2  he 			continue;
    786  1.19.4.2  he 
    787  1.19.4.2  he 		dv = config_found_sm(&sc->sc_dv, &ia, iop_print, iop_submatch);
    788  1.19.4.2  he 		if (dv != NULL) {
    789  1.19.4.2  he  			sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
    790  1.19.4.2  he 			strcpy(sc->sc_tidmap[i].it_dvname, dv->dv_xname);
    791  1.19.4.2  he 		}
    792  1.19.4.2  he 	}
    793  1.19.4.2  he }
    794  1.19.4.2  he 
    795  1.19.4.2  he /*
    796  1.19.4.2  he  * Adjust queue parameters for all child devices.
    797  1.19.4.2  he  */
    798  1.19.4.2  he static void
    799  1.19.4.2  he iop_adjqparam(struct iop_softc *sc, int mpi)
    800  1.19.4.2  he {
    801  1.19.4.2  he 	struct iop_initiator *ii;
    802  1.19.4.2  he 
    803  1.19.4.2  he 	LIST_FOREACH(ii, &sc->sc_iilist, ii_list)
    804  1.19.4.2  he 		if (ii->ii_adjqparam != NULL)
    805  1.19.4.2  he 			(*ii->ii_adjqparam)(ii->ii_dv, mpi);
    806  1.19.4.2  he }
    807  1.19.4.2  he 
    808  1.19.4.2  he static void
    809  1.19.4.2  he iop_devinfo(int class, char *devinfo)
    810  1.19.4.2  he {
    811  1.19.4.2  he #ifdef I2OVERBOSE
    812  1.19.4.2  he 	int i;
    813  1.19.4.2  he 
    814  1.19.4.2  he 	for (i = 0; i < sizeof(iop_class) / sizeof(iop_class[0]); i++)
    815  1.19.4.2  he 		if (class == iop_class[i].ic_class)
    816  1.19.4.2  he 			break;
    817  1.19.4.2  he 
    818  1.19.4.2  he 	if (i == sizeof(iop_class) / sizeof(iop_class[0]))
    819  1.19.4.2  he 		sprintf(devinfo, "device (class 0x%x)", class);
    820  1.19.4.2  he 	else
    821  1.19.4.2  he 		strcpy(devinfo, iop_class[i].ic_caption);
    822  1.19.4.2  he #else
    823  1.19.4.2  he 
    824  1.19.4.2  he 	sprintf(devinfo, "device (class 0x%x)", class);
    825  1.19.4.2  he #endif
    826  1.19.4.2  he }
    827  1.19.4.2  he 
    828  1.19.4.2  he static int
    829  1.19.4.2  he iop_print(void *aux, const char *pnp)
    830  1.19.4.2  he {
    831  1.19.4.2  he 	struct iop_attach_args *ia;
    832  1.19.4.2  he 	char devinfo[256];
    833  1.19.4.2  he 
    834  1.19.4.2  he 	ia = aux;
    835  1.19.4.2  he 
    836  1.19.4.2  he 	if (pnp != NULL) {
    837  1.19.4.2  he 		iop_devinfo(ia->ia_class, devinfo);
    838  1.19.4.2  he 		printf("%s at %s", devinfo, pnp);
    839  1.19.4.2  he 	}
    840  1.19.4.2  he 	printf(" tid %d", ia->ia_tid);
    841  1.19.4.2  he 	return (UNCONF);
    842  1.19.4.2  he }
    843  1.19.4.2  he 
    844  1.19.4.2  he #ifdef notyet
    845  1.19.4.2  he static int
    846  1.19.4.2  he iop_vendor_print(void *aux, const char *pnp)
    847  1.19.4.2  he {
    848  1.19.4.2  he 
    849  1.19.4.2  he 	if (pnp != NULL)
    850  1.19.4.2  he 		printf("vendor specific extension at %s", pnp);
    851  1.19.4.2  he 	return (UNCONF);
    852  1.19.4.2  he }
    853  1.19.4.2  he #endif
    854  1.19.4.2  he 
    855  1.19.4.2  he static int
    856  1.19.4.2  he iop_submatch(struct device *parent, struct cfdata *cf, void *aux)
    857  1.19.4.2  he {
    858  1.19.4.2  he 	struct iop_attach_args *ia;
    859  1.19.4.2  he 
    860  1.19.4.2  he 	ia = aux;
    861  1.19.4.2  he 
    862  1.19.4.2  he 	if (cf->iopcf_tid != IOPCF_TID_DEFAULT && cf->iopcf_tid != ia->ia_tid)
    863  1.19.4.2  he 		return (0);
    864  1.19.4.2  he 
    865  1.19.4.2  he 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    866  1.19.4.2  he }
    867  1.19.4.2  he 
    868  1.19.4.2  he /*
    869  1.19.4.2  he  * Shut down all configured IOPs.
    870  1.19.4.2  he  */
    871  1.19.4.2  he static void
    872  1.19.4.2  he iop_shutdown(void *junk)
    873  1.19.4.2  he {
    874  1.19.4.2  he 	struct iop_softc *sc;
    875  1.19.4.2  he 	int i;
    876  1.19.4.2  he 
    877  1.19.4.2  he 	printf("shutting down iop devices...");
    878  1.19.4.2  he 
    879  1.19.4.2  he 	for (i = 0; i < iop_cd.cd_ndevs; i++) {
    880  1.19.4.2  he 		if ((sc = device_lookup(&iop_cd, i)) == NULL)
    881  1.19.4.2  he 			continue;
    882  1.19.4.2  he 		if ((sc->sc_flags & IOP_ONLINE) == 0)
    883  1.19.4.2  he 			continue;
    884  1.19.4.2  he 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_QUIESCE, IOP_ICTX,
    885  1.19.4.2  he 		    0, 5000);
    886  1.19.4.2  he 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_IOP_CLEAR, IOP_ICTX,
    887  1.19.4.2  he 		    0, 1000);
    888  1.19.4.2  he 	}
    889  1.19.4.2  he 
    890  1.19.4.2  he 	/* Wait.  Some boards could still be flushing, stupidly enough. */
    891  1.19.4.2  he 	delay(5000*1000);
    892  1.19.4.2  he 	printf(" done.\n");
    893  1.19.4.2  he }
    894  1.19.4.2  he 
    895  1.19.4.2  he /*
    896  1.19.4.2  he  * Retrieve IOP status.
    897  1.19.4.2  he  */
    898  1.19.4.2  he static int
    899  1.19.4.2  he iop_status_get(struct iop_softc *sc, int nosleep)
    900  1.19.4.2  he {
    901  1.19.4.2  he 	struct i2o_exec_status_get mf;
    902  1.19.4.2  he 	struct i2o_status *st;
    903  1.19.4.2  he 	paddr_t pa;
    904  1.19.4.2  he 	int rv, i;
    905  1.19.4.2  he 
    906  1.19.4.2  he 	pa = sc->sc_scr_seg->ds_addr;
    907  1.19.4.2  he 	st = (struct i2o_status *)sc->sc_scr;
    908  1.19.4.2  he 
    909  1.19.4.2  he 	mf.msgflags = I2O_MSGFLAGS(i2o_exec_status_get);
    910  1.19.4.2  he 	mf.msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_STATUS_GET);
    911  1.19.4.2  he 	mf.reserved[0] = 0;
    912  1.19.4.2  he 	mf.reserved[1] = 0;
    913  1.19.4.2  he 	mf.reserved[2] = 0;
    914  1.19.4.2  he 	mf.reserved[3] = 0;
    915  1.19.4.2  he 	mf.addrlow = (u_int32_t)pa;
    916  1.19.4.2  he 	mf.addrhigh = (u_int32_t)((u_int64_t)pa >> 32);
    917  1.19.4.2  he 	mf.length = sizeof(sc->sc_status);
    918  1.19.4.2  he 
    919  1.19.4.2  he 	memset(st, 0, sizeof(*st));
    920  1.19.4.2  he 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*st),
    921  1.19.4.2  he 	    BUS_DMASYNC_PREREAD);
    922  1.19.4.2  he 
    923  1.19.4.2  he 	if ((rv = iop_post(sc, (u_int32_t *)&mf)) != 0)
    924  1.19.4.2  he 		return (rv);
    925  1.19.4.2  he 
    926  1.19.4.2  he 	for (i = 25; i != 0; i--) {
    927  1.19.4.2  he 		bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0,
    928  1.19.4.2  he 		    sizeof(*st), BUS_DMASYNC_POSTREAD);
    929  1.19.4.2  he 		if (st->syncbyte == 0xff)
    930  1.19.4.2  he 			break;
    931  1.19.4.2  he 		if (nosleep)
    932  1.19.4.2  he 			DELAY(100*1000);
    933  1.19.4.2  he 		else
    934  1.19.4.2  he 			tsleep(iop_status_get, PWAIT, "iopstat", hz / 10);
    935  1.19.4.2  he 	}
    936  1.19.4.2  he 
    937  1.19.4.2  he 	if (st->syncbyte != 0xff)
    938  1.19.4.2  he 		rv = EIO;
    939  1.19.4.2  he 	else {
    940  1.19.4.2  he 		memcpy(&sc->sc_status, st, sizeof(sc->sc_status));
    941  1.19.4.2  he 		rv = 0;
    942  1.19.4.2  he 	}
    943  1.19.4.2  he 
    944  1.19.4.2  he 	return (rv);
    945  1.19.4.2  he }
    946  1.19.4.2  he 
    947  1.19.4.2  he /*
    948  1.19.4.2  he  * Initalize and populate the IOP's outbound FIFO.
    949  1.19.4.2  he  */
    950  1.19.4.2  he static int
    951  1.19.4.2  he iop_ofifo_init(struct iop_softc *sc)
    952  1.19.4.2  he {
    953  1.19.4.2  he 	bus_addr_t addr;
    954  1.19.4.2  he 	bus_dma_segment_t seg;
    955  1.19.4.2  he 	struct i2o_exec_outbound_init *mf;
    956  1.19.4.2  he 	int i, rseg, rv;
    957  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)], *sw;
    958  1.19.4.2  he 
    959  1.19.4.2  he 	sw = (u_int32_t *)sc->sc_scr;
    960  1.19.4.2  he 
    961  1.19.4.2  he 	mf = (struct i2o_exec_outbound_init *)mb;
    962  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_outbound_init);
    963  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_OUTBOUND_INIT);
    964  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
    965  1.19.4.2  he 	mf->msgtctx = 0;
    966  1.19.4.2  he 	mf->pagesize = NBPG;
    967  1.19.4.2  he 	mf->flags = IOP_INIT_CODE | ((IOP_MAX_MSG_SIZE >> 2) << 16);
    968  1.19.4.2  he 
    969  1.19.4.2  he 	/*
    970  1.19.4.2  he 	 * The I2O spec says that there are two SGLs: one for the status
    971  1.19.4.2  he 	 * word, and one for a list of discarded MFAs.  It continues to say
    972  1.19.4.2  he 	 * that if you don't want to get the list of MFAs, an IGNORE SGL is
    973  1.19.4.2  he 	 * necessary; this isn't the case (and is in fact a bad thing).
    974  1.19.4.2  he 	 */
    975  1.19.4.2  he 	mb[sizeof(*mf) / sizeof(u_int32_t) + 0] = sizeof(*sw) |
    976  1.19.4.2  he 	    I2O_SGL_SIMPLE | I2O_SGL_END_BUFFER | I2O_SGL_END;
    977  1.19.4.2  he 	mb[sizeof(*mf) / sizeof(u_int32_t) + 1] =
    978  1.19.4.2  he 	    (u_int32_t)sc->sc_scr_seg->ds_addr;
    979  1.19.4.2  he 	mb[0] += 2 << 16;
    980  1.19.4.2  he 
    981  1.19.4.2  he 	*sw = 0;
    982  1.19.4.2  he 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
    983  1.19.4.2  he 	    BUS_DMASYNC_PREREAD);
    984  1.19.4.2  he 
    985  1.19.4.2  he 	if ((rv = iop_post(sc, mb)) != 0)
    986  1.19.4.2  he 		return (rv);
    987  1.19.4.2  he 
    988  1.19.4.2  he 	POLL(5000,
    989  1.19.4.2  he 	    (bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
    990  1.19.4.2  he 	    BUS_DMASYNC_POSTREAD),
    991  1.19.4.2  he 	    *sw == htole32(I2O_EXEC_OUTBOUND_INIT_COMPLETE)));
    992  1.19.4.2  he 
    993  1.19.4.2  he 	if (*sw != htole32(I2O_EXEC_OUTBOUND_INIT_COMPLETE)) {
    994  1.19.4.2  he 		printf("%s: outbound FIFO init failed (%d)\n",
    995  1.19.4.2  he 		    sc->sc_dv.dv_xname, le32toh(*sw));
    996  1.19.4.2  he 		return (EIO);
    997  1.19.4.2  he 	}
    998  1.19.4.2  he 
    999  1.19.4.2  he 	/* Allocate DMA safe memory for the reply frames. */
   1000  1.19.4.2  he 	if (sc->sc_rep_phys == 0) {
   1001  1.19.4.2  he 		sc->sc_rep_size = sc->sc_maxob * IOP_MAX_MSG_SIZE;
   1002  1.19.4.2  he 
   1003  1.19.4.2  he 		rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_rep_size, NBPG,
   1004  1.19.4.2  he 		    0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
   1005  1.19.4.2  he 		if (rv != 0) {
   1006  1.19.4.2  he 			printf("%s: dma alloc = %d\n", sc->sc_dv.dv_xname,
   1007  1.19.4.2  he 			   rv);
   1008  1.19.4.2  he 			return (rv);
   1009  1.19.4.2  he 		}
   1010  1.19.4.2  he 
   1011  1.19.4.2  he 		rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sc->sc_rep_size,
   1012  1.19.4.2  he 		    &sc->sc_rep, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
   1013  1.19.4.2  he 		if (rv != 0) {
   1014  1.19.4.2  he 			printf("%s: dma map = %d\n", sc->sc_dv.dv_xname, rv);
   1015  1.19.4.2  he 			return (rv);
   1016  1.19.4.2  he 		}
   1017  1.19.4.2  he 
   1018  1.19.4.2  he 		rv = bus_dmamap_create(sc->sc_dmat, sc->sc_rep_size, 1,
   1019  1.19.4.2  he 		    sc->sc_rep_size, 0, BUS_DMA_NOWAIT, &sc->sc_rep_dmamap);
   1020  1.19.4.2  he 		if (rv != 0) {
   1021  1.19.4.2  he 			printf("%s: dma create = %d\n", sc->sc_dv.dv_xname,
   1022  1.19.4.2  he 			    rv);
   1023  1.19.4.2  he 			return (rv);
   1024  1.19.4.2  he 		}
   1025  1.19.4.2  he 
   1026  1.19.4.2  he 		rv = bus_dmamap_load(sc->sc_dmat, sc->sc_rep_dmamap,
   1027  1.19.4.2  he 		    sc->sc_rep, sc->sc_rep_size, NULL, BUS_DMA_NOWAIT);
   1028  1.19.4.2  he 		if (rv != 0) {
   1029  1.19.4.2  he 			printf("%s: dma load = %d\n", sc->sc_dv.dv_xname, rv);
   1030  1.19.4.2  he 			return (rv);
   1031  1.19.4.2  he 		}
   1032  1.19.4.2  he 
   1033  1.19.4.2  he 		sc->sc_rep_phys = sc->sc_rep_dmamap->dm_segs[0].ds_addr;
   1034  1.19.4.2  he 	}
   1035  1.19.4.2  he 
   1036  1.19.4.2  he 	/* Populate the outbound FIFO. */
   1037  1.19.4.2  he 	for (i = sc->sc_maxob, addr = sc->sc_rep_phys; i != 0; i--) {
   1038  1.19.4.2  he 		iop_outl(sc, IOP_REG_OFIFO, (u_int32_t)addr);
   1039  1.19.4.2  he 		addr += IOP_MAX_MSG_SIZE;
   1040  1.19.4.2  he 	}
   1041  1.19.4.2  he 
   1042  1.19.4.2  he 	return (0);
   1043  1.19.4.2  he }
   1044  1.19.4.2  he 
   1045  1.19.4.2  he /*
   1046  1.19.4.2  he  * Read the specified number of bytes from the IOP's hardware resource table.
   1047  1.19.4.2  he  */
   1048  1.19.4.2  he static int
   1049  1.19.4.2  he iop_hrt_get0(struct iop_softc *sc, struct i2o_hrt *hrt, int size)
   1050  1.19.4.2  he {
   1051  1.19.4.2  he 	struct iop_msg *im;
   1052  1.19.4.2  he 	int rv;
   1053  1.19.4.2  he 	struct i2o_exec_hrt_get *mf;
   1054  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1055  1.19.4.2  he 
   1056  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   1057  1.19.4.2  he 	mf = (struct i2o_exec_hrt_get *)mb;
   1058  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_hrt_get);
   1059  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_HRT_GET);
   1060  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1061  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1062  1.19.4.2  he 
   1063  1.19.4.2  he 	iop_msg_map(sc, im, mb, hrt, size, 0, NULL);
   1064  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, 30000);
   1065  1.19.4.2  he 	iop_msg_unmap(sc, im);
   1066  1.19.4.2  he 	iop_msg_free(sc, im);
   1067  1.19.4.2  he 	return (rv);
   1068  1.19.4.2  he }
   1069  1.19.4.2  he 
   1070  1.19.4.2  he /*
   1071  1.19.4.2  he  * Read the IOP's hardware resource table.
   1072  1.19.4.2  he  */
   1073  1.19.4.2  he static int
   1074  1.19.4.2  he iop_hrt_get(struct iop_softc *sc)
   1075  1.19.4.2  he {
   1076  1.19.4.2  he 	struct i2o_hrt hrthdr, *hrt;
   1077  1.19.4.2  he 	int size, rv;
   1078  1.19.4.2  he 
   1079  1.19.4.2  he 	PHOLD(curproc);
   1080  1.19.4.2  he 	rv = iop_hrt_get0(sc, &hrthdr, sizeof(hrthdr));
   1081  1.19.4.2  he 	PRELE(curproc);
   1082  1.19.4.2  he 	if (rv != 0)
   1083  1.19.4.2  he 		return (rv);
   1084  1.19.4.2  he 
   1085  1.19.4.2  he 	DPRINTF(("%s: %d hrt entries\n", sc->sc_dv.dv_xname,
   1086  1.19.4.2  he 	    le16toh(hrthdr.numentries)));
   1087  1.19.4.2  he 
   1088  1.19.4.2  he 	size = sizeof(struct i2o_hrt) +
   1089  1.19.4.2  he 	    (le16toh(hrthdr.numentries) - 1) * sizeof(struct i2o_hrt_entry);
   1090  1.19.4.2  he 	hrt = (struct i2o_hrt *)malloc(size, M_DEVBUF, M_NOWAIT);
   1091  1.19.4.2  he 
   1092  1.19.4.2  he 	if ((rv = iop_hrt_get0(sc, hrt, size)) != 0) {
   1093  1.19.4.2  he 		free(hrt, M_DEVBUF);
   1094  1.19.4.2  he 		return (rv);
   1095  1.19.4.2  he 	}
   1096  1.19.4.2  he 
   1097  1.19.4.2  he 	if (sc->sc_hrt != NULL)
   1098  1.19.4.2  he 		free(sc->sc_hrt, M_DEVBUF);
   1099  1.19.4.2  he 	sc->sc_hrt = hrt;
   1100  1.19.4.2  he 	return (0);
   1101  1.19.4.2  he }
   1102  1.19.4.2  he 
   1103  1.19.4.2  he /*
   1104  1.19.4.2  he  * Request the specified number of bytes from the IOP's logical
   1105  1.19.4.2  he  * configuration table.  If a change indicator is specified, this
   1106  1.19.4.2  he  * is a verbatim notification request, so the caller is prepared
   1107  1.19.4.2  he  * to wait indefinitely.
   1108  1.19.4.2  he  */
   1109  1.19.4.2  he static int
   1110  1.19.4.2  he iop_lct_get0(struct iop_softc *sc, struct i2o_lct *lct, int size,
   1111  1.19.4.2  he 	     u_int32_t chgind)
   1112  1.19.4.2  he {
   1113  1.19.4.2  he 	struct iop_msg *im;
   1114  1.19.4.2  he 	struct i2o_exec_lct_notify *mf;
   1115  1.19.4.2  he 	int rv;
   1116  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1117  1.19.4.2  he 
   1118  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   1119  1.19.4.2  he 	memset(lct, 0, size);
   1120  1.19.4.2  he 
   1121  1.19.4.2  he 	mf = (struct i2o_exec_lct_notify *)mb;
   1122  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_lct_notify);
   1123  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_LCT_NOTIFY);
   1124  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1125  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1126  1.19.4.2  he 	mf->classid = I2O_CLASS_ANY;
   1127  1.19.4.2  he 	mf->changeindicator = chgind;
   1128  1.19.4.2  he 
   1129  1.19.4.2  he #ifdef I2ODEBUG
   1130  1.19.4.2  he 	printf("iop_lct_get0: reading LCT");
   1131  1.19.4.2  he 	if (chgind != 0)
   1132  1.19.4.2  he 		printf(" (async)");
   1133  1.19.4.2  he 	printf("\n");
   1134  1.19.4.2  he #endif
   1135  1.19.4.2  he 
   1136  1.19.4.2  he 	iop_msg_map(sc, im, mb, lct, size, 0, NULL);
   1137  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, (chgind == 0 ? 120*1000 : 0));
   1138  1.19.4.2  he 	iop_msg_unmap(sc, im);
   1139  1.19.4.2  he 	iop_msg_free(sc, im);
   1140  1.19.4.2  he 	return (rv);
   1141  1.19.4.2  he }
   1142  1.19.4.2  he 
   1143  1.19.4.2  he /*
   1144  1.19.4.2  he  * Read the IOP's logical configuration table.
   1145  1.19.4.2  he  */
   1146  1.19.4.2  he int
   1147  1.19.4.2  he iop_lct_get(struct iop_softc *sc)
   1148  1.19.4.2  he {
   1149  1.19.4.2  he 	int esize, size, rv;
   1150  1.19.4.2  he 	struct i2o_lct *lct;
   1151  1.19.4.2  he 
   1152  1.19.4.2  he 	esize = le32toh(sc->sc_status.expectedlctsize);
   1153  1.19.4.2  he 	lct = (struct i2o_lct *)malloc(esize, M_DEVBUF, M_WAITOK);
   1154  1.19.4.2  he 	if (lct == NULL)
   1155  1.19.4.2  he 		return (ENOMEM);
   1156  1.19.4.2  he 
   1157  1.19.4.2  he 	if ((rv = iop_lct_get0(sc, lct, esize, 0)) != 0) {
   1158  1.19.4.2  he 		free(lct, M_DEVBUF);
   1159  1.19.4.2  he 		return (rv);
   1160  1.19.4.2  he 	}
   1161  1.19.4.2  he 
   1162  1.19.4.2  he 	size = le16toh(lct->tablesize) << 2;
   1163  1.19.4.2  he 	if (esize != size) {
   1164  1.19.4.2  he 		free(lct, M_DEVBUF);
   1165  1.19.4.2  he 		lct = (struct i2o_lct *)malloc(size, M_DEVBUF, M_WAITOK);
   1166  1.19.4.2  he 		if (lct == NULL)
   1167  1.19.4.2  he 			return (ENOMEM);
   1168  1.19.4.2  he 
   1169  1.19.4.2  he 		if ((rv = iop_lct_get0(sc, lct, size, 0)) != 0) {
   1170  1.19.4.2  he 			free(lct, M_DEVBUF);
   1171  1.19.4.2  he 			return (rv);
   1172  1.19.4.2  he 		}
   1173  1.19.4.2  he 	}
   1174  1.19.4.2  he 
   1175  1.19.4.2  he 	/* Swap in the new LCT. */
   1176  1.19.4.2  he 	if (sc->sc_lct != NULL)
   1177  1.19.4.2  he 		free(sc->sc_lct, M_DEVBUF);
   1178  1.19.4.2  he 	sc->sc_lct = lct;
   1179  1.19.4.2  he 	sc->sc_nlctent = ((le16toh(sc->sc_lct->tablesize) << 2) -
   1180  1.19.4.2  he 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
   1181  1.19.4.2  he 	    sizeof(struct i2o_lct_entry);
   1182  1.19.4.2  he 	return (0);
   1183  1.19.4.2  he }
   1184  1.19.4.2  he 
   1185  1.19.4.2  he /*
   1186  1.19.4.2  he  * Request the specified parameter group from the target.  If an initiator
   1187  1.19.4.2  he  * is specified (a) don't wait for the operation to complete, but instead
   1188  1.19.4.2  he  * let the initiator's interrupt handler deal with the reply and (b) place a
   1189  1.19.4.2  he  * pointer to the parameter group op in the wrapper's `im_dvcontext' field.
   1190  1.19.4.2  he  */
   1191  1.19.4.2  he int
   1192  1.19.4.2  he iop_field_get_all(struct iop_softc *sc, int tid, int group, void *buf,
   1193  1.19.4.2  he 		  int size, struct iop_initiator *ii)
   1194  1.19.4.2  he {
   1195  1.19.4.2  he 	struct iop_msg *im;
   1196  1.19.4.2  he 	struct i2o_util_params_op *mf;
   1197  1.19.4.2  he 	struct i2o_reply *rf;
   1198  1.19.4.2  he 	int rv;
   1199  1.19.4.2  he 	struct iop_pgop *pgop;
   1200  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1201  1.19.4.2  he 
   1202  1.19.4.2  he 	im = iop_msg_alloc(sc, (ii == NULL ? IM_WAIT : 0) | IM_NOSTATUS);
   1203  1.19.4.2  he 	if ((pgop = malloc(sizeof(*pgop), M_DEVBUF, M_WAITOK)) == NULL) {
   1204  1.19.4.2  he 		iop_msg_free(sc, im);
   1205  1.19.4.2  he 		return (ENOMEM);
   1206  1.19.4.2  he 	}
   1207  1.19.4.2  he 	if ((rf = malloc(sizeof(*rf), M_DEVBUF, M_WAITOK)) == NULL) {
   1208  1.19.4.2  he 		iop_msg_free(sc, im);
   1209  1.19.4.2  he 		free(pgop, M_DEVBUF);
   1210  1.19.4.2  he 		return (ENOMEM);
   1211  1.19.4.2  he 	}
   1212  1.19.4.2  he 	im->im_dvcontext = pgop;
   1213  1.19.4.2  he 	im->im_rb = rf;
   1214  1.19.4.2  he 
   1215  1.19.4.2  he 	mf = (struct i2o_util_params_op *)mb;
   1216  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1217  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
   1218  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1219  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1220  1.19.4.2  he 	mf->flags = 0;
   1221  1.19.4.2  he 
   1222  1.19.4.2  he 	pgop->olh.count = htole16(1);
   1223  1.19.4.2  he 	pgop->olh.reserved = htole16(0);
   1224  1.19.4.2  he 	pgop->oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
   1225  1.19.4.2  he 	pgop->oat.fieldcount = htole16(0xffff);
   1226  1.19.4.2  he 	pgop->oat.group = htole16(group);
   1227  1.19.4.2  he 
   1228  1.19.4.2  he 	if (ii == NULL)
   1229  1.19.4.2  he 		PHOLD(curproc);
   1230  1.19.4.2  he 
   1231  1.19.4.2  he 	memset(buf, 0, size);
   1232  1.19.4.2  he 	iop_msg_map(sc, im, mb, pgop, sizeof(*pgop), 1, NULL);
   1233  1.19.4.2  he 	iop_msg_map(sc, im, mb, buf, size, 0, NULL);
   1234  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, (ii == NULL ? 30000 : 0));
   1235  1.19.4.2  he 
   1236  1.19.4.2  he 	if (ii == NULL)
   1237  1.19.4.2  he 		PRELE(curproc);
   1238  1.19.4.2  he 
   1239  1.19.4.2  he 	/* Detect errors; let partial transfers to count as success. */
   1240  1.19.4.2  he 	if (ii == NULL && rv == 0) {
   1241  1.19.4.2  he 		if (rf->reqstatus == I2O_STATUS_ERROR_PARTIAL_XFER &&
   1242  1.19.4.2  he 		    le16toh(rf->detail) == I2O_DSC_UNKNOWN_ERROR)
   1243  1.19.4.2  he 			rv = 0;
   1244  1.19.4.2  he 		else
   1245  1.19.4.2  he 			rv = (rf->reqstatus != 0 ? EIO : 0);
   1246  1.19.4.2  he 
   1247  1.19.4.2  he 		if (rv != 0)
   1248  1.19.4.2  he 			printf("%s: FIELD_GET failed for tid %d group %d\n",
   1249  1.19.4.2  he 			    sc->sc_dv.dv_xname, tid, group);
   1250  1.19.4.2  he 	}
   1251  1.19.4.2  he 
   1252  1.19.4.2  he 	if (ii == NULL || rv != 0) {
   1253  1.19.4.2  he 		iop_msg_unmap(sc, im);
   1254  1.19.4.2  he 		iop_msg_free(sc, im);
   1255  1.19.4.2  he 		free(pgop, M_DEVBUF);
   1256  1.19.4.2  he 		free(rf, M_DEVBUF);
   1257  1.19.4.2  he 	}
   1258  1.19.4.2  he 
   1259  1.19.4.2  he 	return (rv);
   1260  1.19.4.2  he }
   1261  1.19.4.2  he 
   1262  1.19.4.2  he /*
   1263  1.19.4.2  he  * Set a single field in a scalar parameter group.
   1264  1.19.4.2  he  */
   1265  1.19.4.2  he int
   1266  1.19.4.2  he iop_field_set(struct iop_softc *sc, int tid, int group, void *buf,
   1267  1.19.4.2  he 	      int size, int field)
   1268  1.19.4.2  he {
   1269  1.19.4.2  he 	struct iop_msg *im;
   1270  1.19.4.2  he 	struct i2o_util_params_op *mf;
   1271  1.19.4.2  he 	struct iop_pgop *pgop;
   1272  1.19.4.2  he 	int rv, totsize;
   1273  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1274  1.19.4.2  he 
   1275  1.19.4.2  he 	totsize = sizeof(*pgop) + size;
   1276  1.19.4.2  he 
   1277  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   1278  1.19.4.2  he 	if ((pgop = malloc(totsize, M_DEVBUF, M_WAITOK)) == NULL) {
   1279  1.19.4.2  he 		iop_msg_free(sc, im);
   1280  1.19.4.2  he 		return (ENOMEM);
   1281  1.19.4.2  he 	}
   1282  1.19.4.2  he 
   1283  1.19.4.2  he 	mf = (struct i2o_util_params_op *)mb;
   1284  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1285  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_SET);
   1286  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1287  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1288  1.19.4.2  he 	mf->flags = 0;
   1289  1.19.4.2  he 
   1290  1.19.4.2  he 	pgop->olh.count = htole16(1);
   1291  1.19.4.2  he 	pgop->olh.reserved = htole16(0);
   1292  1.19.4.2  he 	pgop->oat.operation = htole16(I2O_PARAMS_OP_FIELD_SET);
   1293  1.19.4.2  he 	pgop->oat.fieldcount = htole16(1);
   1294  1.19.4.2  he 	pgop->oat.group = htole16(group);
   1295  1.19.4.2  he 	pgop->oat.fields[0] = htole16(field);
   1296  1.19.4.2  he 	memcpy(pgop + 1, buf, size);
   1297  1.19.4.2  he 
   1298  1.19.4.2  he 	iop_msg_map(sc, im, mb, pgop, totsize, 1, NULL);
   1299  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, 30000);
   1300  1.19.4.2  he 	if (rv != 0)
   1301  1.19.4.2  he 		printf("%s: FIELD_SET failed for tid %d group %d\n",
   1302  1.19.4.2  he 		    sc->sc_dv.dv_xname, tid, group);
   1303  1.19.4.2  he 
   1304  1.19.4.2  he 	iop_msg_unmap(sc, im);
   1305  1.19.4.2  he 	iop_msg_free(sc, im);
   1306  1.19.4.2  he 	free(pgop, M_DEVBUF);
   1307  1.19.4.2  he 	return (rv);
   1308  1.19.4.2  he }
   1309  1.19.4.2  he 
   1310  1.19.4.2  he /*
   1311  1.19.4.2  he  * Delete all rows in a tablular parameter group.
   1312  1.19.4.2  he  */
   1313  1.19.4.2  he int
   1314  1.19.4.2  he iop_table_clear(struct iop_softc *sc, int tid, int group)
   1315  1.19.4.2  he {
   1316  1.19.4.2  he 	struct iop_msg *im;
   1317  1.19.4.2  he 	struct i2o_util_params_op *mf;
   1318  1.19.4.2  he 	struct iop_pgop pgop;
   1319  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1320  1.19.4.2  he 	int rv;
   1321  1.19.4.2  he 
   1322  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   1323  1.19.4.2  he 
   1324  1.19.4.2  he 	mf = (struct i2o_util_params_op *)mb;
   1325  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1326  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_SET);
   1327  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1328  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1329  1.19.4.2  he 	mf->flags = 0;
   1330  1.19.4.2  he 
   1331  1.19.4.2  he 	pgop.olh.count = htole16(1);
   1332  1.19.4.2  he 	pgop.olh.reserved = htole16(0);
   1333  1.19.4.2  he 	pgop.oat.operation = htole16(I2O_PARAMS_OP_TABLE_CLEAR);
   1334  1.19.4.2  he 	pgop.oat.fieldcount = htole16(0);
   1335  1.19.4.2  he 	pgop.oat.group = htole16(group);
   1336  1.19.4.2  he 	pgop.oat.fields[0] = htole16(0);
   1337  1.19.4.2  he 
   1338  1.19.4.2  he 	PHOLD(curproc);
   1339  1.19.4.2  he 	iop_msg_map(sc, im, mb, &pgop, sizeof(pgop), 1, NULL);
   1340  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, 30000);
   1341  1.19.4.2  he 	if (rv != 0)
   1342  1.19.4.2  he 		printf("%s: TABLE_CLEAR failed for tid %d group %d\n",
   1343  1.19.4.2  he 		    sc->sc_dv.dv_xname, tid, group);
   1344  1.19.4.2  he 
   1345  1.19.4.2  he 	iop_msg_unmap(sc, im);
   1346  1.19.4.2  he 	PRELE(curproc);
   1347  1.19.4.2  he 	iop_msg_free(sc, im);
   1348  1.19.4.2  he 	return (rv);
   1349  1.19.4.2  he }
   1350  1.19.4.2  he 
   1351  1.19.4.2  he /*
   1352  1.19.4.2  he  * Add a single row to a tabular parameter group.  The row can have only one
   1353  1.19.4.2  he  * field.
   1354  1.19.4.2  he  */
   1355  1.19.4.2  he int
   1356  1.19.4.2  he iop_table_add_row(struct iop_softc *sc, int tid, int group, void *buf,
   1357  1.19.4.2  he 		  int size, int row)
   1358  1.19.4.2  he {
   1359  1.19.4.2  he 	struct iop_msg *im;
   1360  1.19.4.2  he 	struct i2o_util_params_op *mf;
   1361  1.19.4.2  he 	struct iop_pgop *pgop;
   1362  1.19.4.2  he 	int rv, totsize;
   1363  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1364  1.19.4.2  he 
   1365  1.19.4.2  he 	totsize = sizeof(*pgop) + sizeof(u_int16_t) * 2 + size;
   1366  1.19.4.2  he 
   1367  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   1368  1.19.4.2  he 	if ((pgop = malloc(totsize, M_DEVBUF, M_WAITOK)) == NULL) {
   1369  1.19.4.2  he 		iop_msg_free(sc, im);
   1370  1.19.4.2  he 		return (ENOMEM);
   1371  1.19.4.2  he 	}
   1372  1.19.4.2  he 
   1373  1.19.4.2  he 	mf = (struct i2o_util_params_op *)mb;
   1374  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1375  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_SET);
   1376  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1377  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1378  1.19.4.2  he 	mf->flags = 0;
   1379  1.19.4.2  he 
   1380  1.19.4.2  he 	pgop->olh.count = htole16(1);
   1381  1.19.4.2  he 	pgop->olh.reserved = htole16(0);
   1382  1.19.4.2  he 	pgop->oat.operation = htole16(I2O_PARAMS_OP_ROW_ADD);
   1383  1.19.4.2  he 	pgop->oat.fieldcount = htole16(1);
   1384  1.19.4.2  he 	pgop->oat.group = htole16(group);
   1385  1.19.4.2  he 	pgop->oat.fields[0] = htole16(0);	/* FieldIdx */
   1386  1.19.4.2  he 	pgop->oat.fields[1] = htole16(1);	/* RowCount */
   1387  1.19.4.2  he 	pgop->oat.fields[2] = htole16(row);	/* KeyValue */
   1388  1.19.4.2  he 	memcpy(&pgop->oat.fields[3], buf, size);
   1389  1.19.4.2  he 
   1390  1.19.4.2  he 	iop_msg_map(sc, im, mb, pgop, totsize, 1, NULL);
   1391  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, 30000);
   1392  1.19.4.2  he 	if (rv != 0)
   1393  1.19.4.2  he 		printf("%s: ADD_ROW failed for tid %d group %d row %d\n",
   1394  1.19.4.2  he 		    sc->sc_dv.dv_xname, tid, group, row);
   1395  1.19.4.2  he 
   1396  1.19.4.2  he 	iop_msg_unmap(sc, im);
   1397  1.19.4.2  he 	iop_msg_free(sc, im);
   1398  1.19.4.2  he 	free(pgop, M_DEVBUF);
   1399  1.19.4.2  he 	return (rv);
   1400  1.19.4.2  he }
   1401  1.19.4.2  he 
   1402  1.19.4.2  he /*
   1403  1.19.4.2  he  * Execute a simple command (no parameters).
   1404  1.19.4.2  he  */
   1405  1.19.4.2  he int
   1406  1.19.4.2  he iop_simple_cmd(struct iop_softc *sc, int tid, int function, int ictx,
   1407  1.19.4.2  he 	       int async, int timo)
   1408  1.19.4.2  he {
   1409  1.19.4.2  he 	struct iop_msg *im;
   1410  1.19.4.2  he 	struct i2o_msg mf;
   1411  1.19.4.2  he 	int rv, fl;
   1412  1.19.4.2  he 
   1413  1.19.4.2  he 	fl = (async != 0 ? IM_WAIT : IM_POLL);
   1414  1.19.4.2  he 	im = iop_msg_alloc(sc, fl);
   1415  1.19.4.2  he 
   1416  1.19.4.2  he 	mf.msgflags = I2O_MSGFLAGS(i2o_msg);
   1417  1.19.4.2  he 	mf.msgfunc = I2O_MSGFUNC(tid, function);
   1418  1.19.4.2  he 	mf.msgictx = ictx;
   1419  1.19.4.2  he 	mf.msgtctx = im->im_tctx;
   1420  1.19.4.2  he 
   1421  1.19.4.2  he 	rv = iop_msg_post(sc, im, &mf, timo);
   1422  1.19.4.2  he 	iop_msg_free(sc, im);
   1423  1.19.4.2  he 	return (rv);
   1424  1.19.4.2  he }
   1425  1.19.4.2  he 
   1426  1.19.4.2  he /*
   1427  1.19.4.2  he  * Post the system table to the IOP.
   1428  1.19.4.2  he  */
   1429  1.19.4.2  he static int
   1430  1.19.4.2  he iop_systab_set(struct iop_softc *sc)
   1431  1.19.4.2  he {
   1432  1.19.4.2  he 	struct i2o_exec_sys_tab_set *mf;
   1433  1.19.4.2  he 	struct iop_msg *im;
   1434  1.19.4.2  he 	bus_space_handle_t bsh;
   1435  1.19.4.2  he 	bus_addr_t boo;
   1436  1.19.4.2  he 	u_int32_t mema[2], ioa[2];
   1437  1.19.4.2  he 	int rv;
   1438  1.19.4.2  he 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1439  1.19.4.2  he 
   1440  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   1441  1.19.4.2  he 
   1442  1.19.4.2  he 	mf = (struct i2o_exec_sys_tab_set *)mb;
   1443  1.19.4.2  he 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_sys_tab_set);
   1444  1.19.4.2  he 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_TAB_SET);
   1445  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   1446  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   1447  1.19.4.2  he 	mf->iopid = (sc->sc_dv.dv_unit + 2) << 12;
   1448  1.19.4.2  he 	mf->segnumber = 0;
   1449  1.19.4.2  he 
   1450  1.19.4.2  he 	mema[1] = sc->sc_status.desiredprivmemsize;
   1451  1.19.4.2  he 	ioa[1] = sc->sc_status.desiredpriviosize;
   1452  1.19.4.2  he 
   1453  1.19.4.2  he 	if (mema[1] != 0) {
   1454  1.19.4.2  he 		rv = bus_space_alloc(sc->sc_bus_memt, 0, 0xffffffff,
   1455  1.19.4.2  he 		    le32toh(mema[1]), NBPG, 0, 0, &boo, &bsh);
   1456  1.19.4.2  he 		mema[0] = htole32(boo);
   1457  1.19.4.2  he 		if (rv != 0) {
   1458  1.19.4.2  he 			printf("%s: can't alloc priv mem space, err = %d\n",
   1459  1.19.4.2  he 			    sc->sc_dv.dv_xname, rv);
   1460  1.19.4.2  he 			mema[0] = 0;
   1461  1.19.4.2  he 			mema[1] = 0;
   1462  1.19.4.2  he 		}
   1463  1.19.4.2  he 	}
   1464  1.19.4.2  he 
   1465  1.19.4.2  he 	if (ioa[1] != 0) {
   1466  1.19.4.2  he 		rv = bus_space_alloc(sc->sc_bus_iot, 0, 0xffff,
   1467  1.19.4.2  he 		    le32toh(ioa[1]), 0, 0, 0, &boo, &bsh);
   1468  1.19.4.2  he 		ioa[0] = htole32(boo);
   1469  1.19.4.2  he 		if (rv != 0) {
   1470  1.19.4.2  he 			printf("%s: can't alloc priv i/o space, err = %d\n",
   1471  1.19.4.2  he 			    sc->sc_dv.dv_xname, rv);
   1472  1.19.4.2  he 			ioa[0] = 0;
   1473  1.19.4.2  he 			ioa[1] = 0;
   1474  1.19.4.2  he 		}
   1475  1.19.4.2  he 	}
   1476  1.19.4.2  he 
   1477  1.19.4.2  he 	PHOLD(curproc);
   1478  1.19.4.2  he 	iop_msg_map(sc, im, mb, iop_systab, iop_systab_size, 1, NULL);
   1479  1.19.4.2  he 	iop_msg_map(sc, im, mb, mema, sizeof(mema), 1, NULL);
   1480  1.19.4.2  he 	iop_msg_map(sc, im, mb, ioa, sizeof(ioa), 1, NULL);
   1481  1.19.4.2  he 	rv = iop_msg_post(sc, im, mb, 5000);
   1482  1.19.4.2  he 	iop_msg_unmap(sc, im);
   1483  1.19.4.2  he 	iop_msg_free(sc, im);
   1484  1.19.4.2  he 	PRELE(curproc);
   1485  1.19.4.2  he 	return (rv);
   1486  1.19.4.2  he }
   1487  1.19.4.2  he 
   1488  1.19.4.2  he /*
   1489  1.19.4.2  he  * Reset the IOP.  Must be called with interrupts disabled.
   1490  1.19.4.2  he  */
   1491  1.19.4.2  he static int
   1492  1.19.4.2  he iop_reset(struct iop_softc *sc)
   1493  1.19.4.2  he {
   1494  1.19.4.2  he 	u_int32_t mfa, *sw;
   1495  1.19.4.2  he 	struct i2o_exec_iop_reset mf;
   1496  1.19.4.2  he 	int rv;
   1497  1.19.4.2  he 	paddr_t pa;
   1498  1.19.4.2  he 
   1499  1.19.4.2  he 	sw = (u_int32_t *)sc->sc_scr;
   1500  1.19.4.2  he 	pa = sc->sc_scr_seg->ds_addr;
   1501  1.19.4.2  he 
   1502  1.19.4.2  he 	mf.msgflags = I2O_MSGFLAGS(i2o_exec_iop_reset);
   1503  1.19.4.2  he 	mf.msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_IOP_RESET);
   1504  1.19.4.2  he 	mf.reserved[0] = 0;
   1505  1.19.4.2  he 	mf.reserved[1] = 0;
   1506  1.19.4.2  he 	mf.reserved[2] = 0;
   1507  1.19.4.2  he 	mf.reserved[3] = 0;
   1508  1.19.4.2  he 	mf.statuslow = (u_int32_t)pa;
   1509  1.19.4.2  he 	mf.statushigh = (u_int32_t)((u_int64_t)pa >> 32);
   1510  1.19.4.2  he 
   1511  1.19.4.2  he 	*sw = htole32(0);
   1512  1.19.4.2  he 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
   1513  1.19.4.2  he 	    BUS_DMASYNC_PREREAD);
   1514  1.19.4.2  he 
   1515  1.19.4.2  he 	if ((rv = iop_post(sc, (u_int32_t *)&mf)))
   1516  1.19.4.2  he 		return (rv);
   1517  1.19.4.2  he 
   1518  1.19.4.2  he 	POLL(2500,
   1519  1.19.4.2  he 	    (bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
   1520  1.19.4.2  he 	    BUS_DMASYNC_POSTREAD), *sw != 0));
   1521  1.19.4.2  he 	if (*sw != htole32(I2O_RESET_IN_PROGRESS)) {
   1522  1.19.4.2  he 		printf("%s: reset rejected, status 0x%x\n",
   1523  1.19.4.2  he 		    sc->sc_dv.dv_xname, le32toh(*sw));
   1524  1.19.4.2  he 		return (EIO);
   1525  1.19.4.2  he 	}
   1526  1.19.4.2  he 
   1527  1.19.4.2  he 	/*
   1528  1.19.4.2  he 	 * IOP is now in the INIT state.  Wait no more than 10 seconds for
   1529  1.19.4.2  he 	 * the inbound queue to become responsive.
   1530  1.19.4.2  he 	 */
   1531  1.19.4.2  he 	POLL(10000, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1532  1.19.4.2  he 	if (mfa == IOP_MFA_EMPTY) {
   1533  1.19.4.2  he 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
   1534  1.19.4.2  he 		return (EIO);
   1535  1.19.4.2  he 	}
   1536  1.19.4.2  he 
   1537  1.19.4.2  he 	iop_release_mfa(sc, mfa);
   1538  1.19.4.2  he 	return (0);
   1539  1.19.4.2  he }
   1540  1.19.4.2  he 
   1541  1.19.4.2  he /*
   1542  1.19.4.2  he  * Register a new initiator.  Must be called with the configuration lock
   1543  1.19.4.2  he  * held.
   1544  1.19.4.2  he  */
   1545  1.19.4.2  he void
   1546  1.19.4.2  he iop_initiator_register(struct iop_softc *sc, struct iop_initiator *ii)
   1547  1.19.4.2  he {
   1548  1.19.4.2  he 	static int ictxgen;
   1549  1.19.4.2  he 	int s;
   1550  1.19.4.2  he 
   1551  1.19.4.2  he 	/* 0 is reserved (by us) for system messages. */
   1552  1.19.4.2  he 	ii->ii_ictx = ++ictxgen;
   1553  1.19.4.2  he 
   1554  1.19.4.2  he 	/*
   1555  1.19.4.2  he 	 * `Utility initiators' don't make it onto the per-IOP initiator list
   1556  1.19.4.2  he 	 * (which is used only for configuration), but do get one slot on
   1557  1.19.4.2  he 	 * the inbound queue.
   1558  1.19.4.2  he 	 */
   1559  1.19.4.2  he 	if ((ii->ii_flags & II_UTILITY) == 0) {
   1560  1.19.4.2  he 		LIST_INSERT_HEAD(&sc->sc_iilist, ii, ii_list);
   1561  1.19.4.2  he 		sc->sc_nii++;
   1562  1.19.4.2  he 	} else
   1563  1.19.4.2  he 		sc->sc_nuii++;
   1564  1.19.4.2  he 
   1565  1.19.4.2  he 	s = splbio();
   1566  1.19.4.2  he 	LIST_INSERT_HEAD(IOP_ICTXHASH(ii->ii_ictx), ii, ii_hash);
   1567  1.19.4.2  he 	splx(s);
   1568  1.19.4.2  he }
   1569  1.19.4.2  he 
   1570  1.19.4.2  he /*
   1571  1.19.4.2  he  * Unregister an initiator.  Must be called with the configuration lock
   1572  1.19.4.2  he  * held.
   1573  1.19.4.2  he  */
   1574  1.19.4.2  he void
   1575  1.19.4.2  he iop_initiator_unregister(struct iop_softc *sc, struct iop_initiator *ii)
   1576  1.19.4.2  he {
   1577  1.19.4.2  he 	int s;
   1578  1.19.4.2  he 
   1579  1.19.4.2  he 	if ((ii->ii_flags & II_UTILITY) == 0) {
   1580  1.19.4.2  he 		LIST_REMOVE(ii, ii_list);
   1581  1.19.4.2  he 		sc->sc_nii--;
   1582  1.19.4.2  he 	} else
   1583  1.19.4.2  he 		sc->sc_nuii--;
   1584  1.19.4.2  he 
   1585  1.19.4.2  he 	s = splbio();
   1586  1.19.4.2  he 	LIST_REMOVE(ii, ii_hash);
   1587  1.19.4.2  he 	splx(s);
   1588  1.19.4.2  he }
   1589  1.19.4.2  he 
   1590  1.19.4.2  he /*
   1591  1.19.4.2  he  * Handle a reply frame from the IOP.
   1592  1.19.4.2  he  */
   1593  1.19.4.2  he static int
   1594  1.19.4.2  he iop_handle_reply(struct iop_softc *sc, u_int32_t rmfa)
   1595  1.19.4.2  he {
   1596  1.19.4.2  he 	struct iop_msg *im;
   1597  1.19.4.2  he 	struct i2o_reply *rb;
   1598  1.19.4.2  he 	struct i2o_fault_notify *fn;
   1599  1.19.4.2  he 	struct iop_initiator *ii;
   1600  1.19.4.2  he 	u_int off, ictx, tctx, status, size;
   1601  1.19.4.2  he 
   1602  1.19.4.2  he 	off = (int)(rmfa - sc->sc_rep_phys);
   1603  1.19.4.2  he 	rb = (struct i2o_reply *)(sc->sc_rep + off);
   1604  1.19.4.2  he 
   1605  1.19.4.2  he 	/* Perform reply queue DMA synchronisation. */
   1606  1.19.4.2  he 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, off,
   1607  1.19.4.2  he 	    IOP_MAX_MSG_SIZE, BUS_DMASYNC_POSTREAD);
   1608  1.19.4.2  he 	if (--sc->sc_curib != 0)
   1609  1.19.4.2  he 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap,
   1610  1.19.4.2  he 		    0, sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1611  1.19.4.2  he 
   1612  1.19.4.2  he #ifdef I2ODEBUG
   1613  1.19.4.2  he 	if ((le32toh(rb->msgflags) & I2O_MSGFLAGS_64BIT) != 0)
   1614  1.19.4.2  he 		panic("iop_handle_reply: 64-bit reply");
   1615  1.19.4.2  he #endif
   1616  1.19.4.2  he 	/*
   1617  1.19.4.2  he 	 * Find the initiator.
   1618  1.19.4.2  he 	 */
   1619  1.19.4.2  he 	ictx = le32toh(rb->msgictx);
   1620  1.19.4.2  he 	if (ictx == IOP_ICTX)
   1621  1.19.4.2  he 		ii = NULL;
   1622  1.19.4.2  he 	else {
   1623  1.19.4.2  he 		ii = LIST_FIRST(IOP_ICTXHASH(ictx));
   1624  1.19.4.2  he 		for (; ii != NULL; ii = LIST_NEXT(ii, ii_hash))
   1625  1.19.4.2  he 			if (ii->ii_ictx == ictx)
   1626  1.19.4.2  he 				break;
   1627  1.19.4.2  he 		if (ii == NULL) {
   1628  1.19.4.2  he #ifdef I2ODEBUG
   1629  1.19.4.2  he 			iop_reply_print(sc, rb);
   1630  1.19.4.2  he #endif
   1631  1.19.4.2  he 			printf("%s: WARNING: bad ictx returned (%x)\n",
   1632  1.19.4.2  he 			    sc->sc_dv.dv_xname, ictx);
   1633  1.19.4.2  he 			return (-1);
   1634  1.19.4.2  he 		}
   1635  1.19.4.2  he 	}
   1636  1.19.4.2  he 
   1637  1.19.4.2  he 	/*
   1638  1.19.4.2  he 	 * If we received a transport failure notice, we've got to dig the
   1639  1.19.4.2  he 	 * transaction context (if any) out of the original message frame,
   1640  1.19.4.2  he 	 * and then release the original MFA back to the inbound FIFO.
   1641  1.19.4.2  he 	 */
   1642  1.19.4.2  he 	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) {
   1643  1.19.4.2  he 		status = I2O_STATUS_SUCCESS;
   1644  1.19.4.2  he 
   1645  1.19.4.2  he 		fn = (struct i2o_fault_notify *)rb;
   1646  1.19.4.2  he 		tctx = iop_inl(sc, fn->lowmfa + 12);
   1647  1.19.4.2  he 		iop_release_mfa(sc, fn->lowmfa);
   1648  1.19.4.2  he 		iop_tfn_print(sc, fn);
   1649  1.19.4.2  he 	} else {
   1650  1.19.4.2  he 		status = rb->reqstatus;
   1651  1.19.4.2  he 		tctx = le32toh(rb->msgtctx);
   1652  1.19.4.2  he 	}
   1653  1.19.4.2  he 
   1654  1.19.4.2  he 	if (ii == NULL || (ii->ii_flags & II_NOTCTX) == 0) {
   1655  1.19.4.2  he 		/*
   1656  1.19.4.2  he 		 * This initiator tracks state using message wrappers.
   1657  1.19.4.2  he 		 *
   1658  1.19.4.2  he 		 * Find the originating message wrapper, and if requested
   1659  1.19.4.2  he 		 * notify the initiator.
   1660  1.19.4.2  he 		 */
   1661  1.19.4.2  he 		im = sc->sc_ims + (tctx & IOP_TCTX_MASK);
   1662  1.19.4.2  he 		if ((tctx & IOP_TCTX_MASK) > sc->sc_maxib ||
   1663  1.19.4.2  he 		    (im->im_flags & IM_ALLOCED) == 0 ||
   1664  1.19.4.2  he 		    tctx != im->im_tctx) {
   1665  1.19.4.2  he 			printf("%s: WARNING: bad tctx returned (0x%08x, %p)\n",
   1666  1.19.4.2  he 			    sc->sc_dv.dv_xname, tctx, im);
   1667  1.19.4.2  he 			if (im != NULL)
   1668  1.19.4.2  he 				printf("%s: flags=0x%08x tctx=0x%08x\n",
   1669  1.19.4.2  he 				    sc->sc_dv.dv_xname, im->im_flags,
   1670  1.19.4.2  he 				    im->im_tctx);
   1671  1.19.4.2  he #ifdef I2ODEBUG
   1672  1.19.4.2  he 			if ((rb->msgflags & I2O_MSGFLAGS_FAIL) == 0)
   1673  1.19.4.2  he 				iop_reply_print(sc, rb);
   1674  1.19.4.2  he #endif
   1675  1.19.4.2  he 			return (-1);
   1676  1.19.4.2  he 		}
   1677  1.19.4.2  he 
   1678  1.19.4.2  he 		if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0)
   1679  1.19.4.2  he 			im->im_flags |= IM_FAIL;
   1680  1.19.4.2  he 
   1681  1.19.4.2  he #ifdef I2ODEBUG
   1682  1.19.4.2  he 		if ((im->im_flags & IM_REPLIED) != 0)
   1683  1.19.4.2  he 			panic("%s: dup reply", sc->sc_dv.dv_xname);
   1684  1.19.4.2  he #endif
   1685  1.19.4.2  he 		im->im_flags |= IM_REPLIED;
   1686  1.19.4.2  he 
   1687  1.19.4.2  he #ifdef I2ODEBUG
   1688  1.19.4.2  he 		if (status != I2O_STATUS_SUCCESS)
   1689  1.19.4.2  he 			iop_reply_print(sc, rb);
   1690  1.19.4.2  he #endif
   1691  1.19.4.2  he 		im->im_reqstatus = status;
   1692  1.19.4.2  he 
   1693  1.19.4.2  he 		/* Copy the reply frame, if requested. */
   1694  1.19.4.2  he 		if (im->im_rb != NULL) {
   1695  1.19.4.2  he 			size = (le32toh(rb->msgflags) >> 14) & ~3;
   1696  1.19.4.2  he #ifdef I2ODEBUG
   1697  1.19.4.2  he 			if (size > IOP_MAX_MSG_SIZE)
   1698  1.19.4.2  he 				panic("iop_handle_reply: reply too large");
   1699  1.19.4.2  he #endif
   1700  1.19.4.2  he 			memcpy(im->im_rb, rb, size);
   1701  1.19.4.2  he 		}
   1702  1.19.4.2  he 
   1703  1.19.4.2  he 		/* Notify the initiator. */
   1704  1.19.4.2  he 		if ((im->im_flags & IM_WAIT) != 0)
   1705  1.19.4.2  he 			wakeup(im);
   1706  1.19.4.2  he 		else if ((im->im_flags & (IM_POLL | IM_POLL_INTR)) != IM_POLL)
   1707  1.19.4.2  he 			(*ii->ii_intr)(ii->ii_dv, im, rb);
   1708  1.19.4.2  he 	} else {
   1709  1.19.4.2  he 		/*
   1710  1.19.4.2  he 		 * This initiator discards message wrappers.
   1711  1.19.4.2  he 		 *
   1712  1.19.4.2  he 		 * Simply pass the reply frame to the initiator.
   1713  1.19.4.2  he 		 */
   1714  1.19.4.2  he 		(*ii->ii_intr)(ii->ii_dv, NULL, rb);
   1715  1.19.4.2  he 	}
   1716  1.19.4.2  he 
   1717  1.19.4.2  he 	return (status);
   1718  1.19.4.2  he }
   1719  1.19.4.2  he 
   1720  1.19.4.2  he /*
   1721  1.19.4.2  he  * Handle an interrupt from the IOP.
   1722  1.19.4.2  he  */
   1723  1.19.4.2  he int
   1724  1.19.4.2  he iop_intr(void *arg)
   1725  1.19.4.2  he {
   1726  1.19.4.2  he 	struct iop_softc *sc;
   1727  1.19.4.2  he 	u_int32_t rmfa;
   1728  1.19.4.2  he 
   1729  1.19.4.2  he 	sc = arg;
   1730  1.19.4.2  he 
   1731  1.19.4.2  he 	if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) == 0)
   1732  1.19.4.2  he 		return (0);
   1733  1.19.4.2  he 
   1734  1.19.4.2  he 	for (;;) {
   1735  1.19.4.2  he 		/* Double read to account for IOP bug. */
   1736  1.19.4.2  he 		if ((rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY) {
   1737  1.19.4.2  he 			rmfa = iop_inl(sc, IOP_REG_OFIFO);
   1738  1.19.4.2  he 			if (rmfa == IOP_MFA_EMPTY)
   1739  1.19.4.2  he 				break;
   1740  1.19.4.2  he 		}
   1741  1.19.4.2  he 		iop_handle_reply(sc, rmfa);
   1742  1.19.4.2  he 		iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1743  1.19.4.2  he 	}
   1744  1.19.4.2  he 
   1745  1.19.4.2  he 	return (1);
   1746  1.19.4.2  he }
   1747  1.19.4.2  he 
   1748  1.19.4.2  he /*
   1749  1.19.4.2  he  * Handle an event signalled by the executive.
   1750  1.19.4.2  he  */
   1751  1.19.4.2  he static void
   1752  1.19.4.2  he iop_intr_event(struct device *dv, struct iop_msg *im, void *reply)
   1753  1.19.4.2  he {
   1754  1.19.4.2  he 	struct i2o_util_event_register_reply *rb;
   1755  1.19.4.2  he 	struct iop_softc *sc;
   1756  1.19.4.2  he 	u_int event;
   1757  1.19.4.2  he 
   1758  1.19.4.2  he 	sc = (struct iop_softc *)dv;
   1759  1.19.4.2  he 	rb = reply;
   1760  1.19.4.2  he 
   1761  1.19.4.2  he 	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0)
   1762  1.19.4.2  he 		return;
   1763  1.19.4.2  he 
   1764  1.19.4.2  he 	event = le32toh(rb->event);
   1765  1.19.4.2  he 	printf("%s: event 0x%08x received\n", dv->dv_xname, event);
   1766  1.19.4.2  he }
   1767  1.19.4.2  he 
   1768  1.19.4.2  he /*
   1769  1.19.4.2  he  * Allocate a message wrapper.
   1770  1.19.4.2  he  */
   1771  1.19.4.2  he struct iop_msg *
   1772  1.19.4.2  he iop_msg_alloc(struct iop_softc *sc, int flags)
   1773  1.19.4.2  he {
   1774  1.19.4.2  he 	struct iop_msg *im;
   1775  1.19.4.2  he 	static u_int tctxgen;
   1776  1.19.4.2  he 	int s, i;
   1777  1.19.4.2  he 
   1778  1.19.4.2  he #ifdef I2ODEBUG
   1779  1.19.4.2  he 	if ((flags & IM_SYSMASK) != 0)
   1780  1.19.4.2  he 		panic("iop_msg_alloc: system flags specified");
   1781  1.19.4.2  he #endif
   1782  1.19.4.2  he 
   1783  1.19.4.2  he 	s = splbio();
   1784  1.19.4.2  he 	im = SLIST_FIRST(&sc->sc_im_freelist);
   1785  1.19.4.2  he #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
   1786  1.19.4.2  he 	if (im == NULL)
   1787  1.19.4.2  he 		panic("iop_msg_alloc: no free wrappers");
   1788  1.19.4.2  he #endif
   1789  1.19.4.2  he 	SLIST_REMOVE_HEAD(&sc->sc_im_freelist, im_chain);
   1790  1.19.4.2  he 	splx(s);
   1791  1.19.4.2  he 
   1792  1.19.4.2  he 	im->im_tctx = (im->im_tctx & IOP_TCTX_MASK) | tctxgen;
   1793  1.19.4.2  he 	tctxgen += (1 << IOP_TCTX_SHIFT);
   1794  1.19.4.2  he 	im->im_flags = flags | IM_ALLOCED;
   1795  1.19.4.2  he 	im->im_rb = NULL;
   1796  1.19.4.2  he 	i = 0;
   1797  1.19.4.2  he 	do {
   1798  1.19.4.2  he 		im->im_xfer[i++].ix_size = 0;
   1799  1.19.4.2  he 	} while (i < IOP_MAX_MSG_XFERS);
   1800  1.19.4.2  he 
   1801  1.19.4.2  he 	return (im);
   1802  1.19.4.2  he }
   1803  1.19.4.2  he 
   1804  1.19.4.2  he /*
   1805  1.19.4.2  he  * Free a message wrapper.
   1806  1.19.4.2  he  */
   1807  1.19.4.2  he void
   1808  1.19.4.2  he iop_msg_free(struct iop_softc *sc, struct iop_msg *im)
   1809  1.19.4.2  he {
   1810  1.19.4.2  he 	int s;
   1811  1.19.4.2  he 
   1812  1.19.4.2  he #ifdef I2ODEBUG
   1813  1.19.4.2  he 	if ((im->im_flags & IM_ALLOCED) == 0)
   1814  1.19.4.2  he 		panic("iop_msg_free: wrapper not allocated");
   1815  1.19.4.2  he #endif
   1816  1.19.4.2  he 
   1817  1.19.4.2  he 	im->im_flags = 0;
   1818  1.19.4.2  he 	s = splbio();
   1819  1.19.4.2  he 	SLIST_INSERT_HEAD(&sc->sc_im_freelist, im, im_chain);
   1820  1.19.4.2  he 	splx(s);
   1821  1.19.4.2  he }
   1822  1.19.4.2  he 
   1823  1.19.4.2  he /*
   1824  1.19.4.2  he  * Map a data transfer.  Write a scatter-gather list into the message frame.
   1825  1.19.4.2  he  */
   1826  1.19.4.2  he int
   1827  1.19.4.2  he iop_msg_map(struct iop_softc *sc, struct iop_msg *im, u_int32_t *mb,
   1828  1.19.4.2  he 	    void *xferaddr, int xfersize, int out, struct proc *up)
   1829  1.19.4.2  he {
   1830  1.19.4.2  he 	bus_dmamap_t dm;
   1831  1.19.4.2  he 	bus_dma_segment_t *ds;
   1832  1.19.4.2  he 	struct iop_xfer *ix;
   1833  1.19.4.2  he 	u_int rv, i, nsegs, flg, off, xn;
   1834  1.19.4.2  he 	u_int32_t *p;
   1835  1.19.4.2  he 
   1836  1.19.4.2  he 	for (xn = 0, ix = im->im_xfer; xn < IOP_MAX_MSG_XFERS; xn++, ix++)
   1837  1.19.4.2  he 		if (ix->ix_size == 0)
   1838  1.19.4.2  he 			break;
   1839  1.19.4.2  he 
   1840  1.19.4.2  he #ifdef I2ODEBUG
   1841  1.19.4.2  he 	if (xfersize == 0)
   1842  1.19.4.2  he 		panic("iop_msg_map: null transfer");
   1843  1.19.4.2  he 	if (xfersize > IOP_MAX_XFER)
   1844  1.19.4.2  he 		panic("iop_msg_map: transfer too large");
   1845  1.19.4.2  he 	if (xn == IOP_MAX_MSG_XFERS)
   1846  1.19.4.2  he 		panic("iop_msg_map: too many xfers");
   1847  1.19.4.2  he #endif
   1848  1.19.4.2  he 
   1849  1.19.4.2  he 	/*
   1850  1.19.4.2  he 	 * Only the first DMA map is static.
   1851  1.19.4.2  he 	 */
   1852  1.19.4.2  he 	if (xn != 0) {
   1853  1.19.4.2  he 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
   1854  1.19.4.2  he 		    IOP_MAX_SEGS, IOP_MAX_XFER, 0,
   1855  1.19.4.2  he 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ix->ix_map);
   1856  1.19.4.2  he 		if (rv != 0)
   1857  1.19.4.2  he 			return (rv);
   1858  1.19.4.2  he 	}
   1859  1.19.4.2  he 
   1860  1.19.4.2  he 	dm = ix->ix_map;
   1861  1.19.4.2  he 	rv = bus_dmamap_load(sc->sc_dmat, dm, xferaddr, xfersize, up,
   1862  1.19.4.2  he 	    (up == NULL ? BUS_DMA_NOWAIT : 0));
   1863  1.19.4.2  he 	if (rv != 0)
   1864  1.19.4.2  he 		goto bad;
   1865  1.19.4.2  he 
   1866  1.19.4.2  he 	/*
   1867  1.19.4.2  he 	 * How many SIMPLE SG elements can we fit in this message?
   1868  1.19.4.2  he 	 */
   1869  1.19.4.2  he 	off = mb[0] >> 16;
   1870  1.19.4.2  he 	p = mb + off;
   1871  1.19.4.2  he 	nsegs = ((IOP_MAX_MSG_SIZE / 4) - off) >> 1;
   1872  1.19.4.2  he 
   1873  1.19.4.2  he 	if (dm->dm_nsegs > nsegs) {
   1874  1.19.4.2  he 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   1875  1.19.4.2  he 		rv = EFBIG;
   1876  1.19.4.2  he 		DPRINTF(("iop_msg_map: too many segs\n"));
   1877  1.19.4.2  he 		goto bad;
   1878  1.19.4.2  he 	}
   1879  1.19.4.2  he 
   1880  1.19.4.2  he 	nsegs = dm->dm_nsegs;
   1881  1.19.4.2  he 	xfersize = 0;
   1882  1.19.4.2  he 
   1883  1.19.4.2  he 	/*
   1884  1.19.4.2  he 	 * Write out the SG list.
   1885  1.19.4.2  he 	 */
   1886  1.19.4.2  he 	if (out)
   1887  1.19.4.2  he 		flg = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   1888  1.19.4.2  he 	else
   1889  1.19.4.2  he 		flg = I2O_SGL_SIMPLE;
   1890  1.19.4.2  he 
   1891  1.19.4.2  he 	for (i = nsegs, ds = dm->dm_segs; i > 1; i--, p += 2, ds++) {
   1892  1.19.4.2  he 		p[0] = (u_int32_t)ds->ds_len | flg;
   1893  1.19.4.2  he 		p[1] = (u_int32_t)ds->ds_addr;
   1894  1.19.4.2  he 		xfersize += ds->ds_len;
   1895  1.19.4.2  he 	}
   1896  1.19.4.2  he 
   1897  1.19.4.2  he 	p[0] = (u_int32_t)ds->ds_len | flg | I2O_SGL_END_BUFFER;
   1898  1.19.4.2  he 	p[1] = (u_int32_t)ds->ds_addr;
   1899  1.19.4.2  he 	xfersize += ds->ds_len;
   1900  1.19.4.2  he 
   1901  1.19.4.2  he 	/* Fix up the transfer record, and sync the map. */
   1902  1.19.4.2  he 	ix->ix_flags = (out ? IX_OUT : IX_IN);
   1903  1.19.4.2  he 	ix->ix_size = xfersize;
   1904  1.19.4.2  he 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   1905  1.19.4.2  he 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   1906  1.19.4.2  he 
   1907  1.19.4.2  he 	/*
   1908  1.19.4.2  he 	 * If this is the first xfer we've mapped for this message, adjust
   1909  1.19.4.2  he 	 * the SGL offset field in the message header.
   1910  1.19.4.2  he 	 */
   1911  1.19.4.2  he 	if ((im->im_flags & IM_SGLOFFADJ) == 0) {
   1912  1.19.4.2  he 		mb[0] += (mb[0] >> 12) & 0xf0;
   1913  1.19.4.2  he 		im->im_flags |= IM_SGLOFFADJ;
   1914  1.19.4.2  he 	}
   1915  1.19.4.2  he 	mb[0] += (nsegs << 17);
   1916  1.19.4.2  he 	return (0);
   1917  1.19.4.2  he 
   1918  1.19.4.2  he  bad:
   1919  1.19.4.2  he  	if (xn != 0)
   1920  1.19.4.2  he 		bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   1921  1.19.4.2  he 	return (rv);
   1922  1.19.4.2  he }
   1923  1.19.4.2  he 
   1924  1.19.4.2  he /*
   1925  1.19.4.2  he  * Map a block I/O data transfer (different in that there's only one per
   1926  1.19.4.2  he  * message maximum, and PAGE addressing may be used).  Write a scatter
   1927  1.19.4.2  he  * gather list into the message frame.
   1928  1.19.4.2  he  */
   1929  1.19.4.2  he int
   1930  1.19.4.2  he iop_msg_map_bio(struct iop_softc *sc, struct iop_msg *im, u_int32_t *mb,
   1931  1.19.4.2  he 		void *xferaddr, int xfersize, int out)
   1932  1.19.4.2  he {
   1933  1.19.4.2  he 	bus_dma_segment_t *ds;
   1934  1.19.4.2  he 	bus_dmamap_t dm;
   1935  1.19.4.2  he 	struct iop_xfer *ix;
   1936  1.19.4.2  he 	u_int rv, i, nsegs, off, slen, tlen, flg;
   1937  1.19.4.2  he 	paddr_t saddr, eaddr;
   1938  1.19.4.2  he 	u_int32_t *p;
   1939  1.19.4.2  he 
   1940  1.19.4.2  he #ifdef I2ODEBUG
   1941  1.19.4.2  he 	if (xfersize == 0)
   1942  1.19.4.2  he 		panic("iop_msg_map_bio: null transfer");
   1943  1.19.4.2  he 	if (xfersize > IOP_MAX_XFER)
   1944  1.19.4.2  he 		panic("iop_msg_map_bio: transfer too large");
   1945  1.19.4.2  he 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1946  1.19.4.2  he 		panic("iop_msg_map_bio: SGLOFFADJ");
   1947  1.19.4.2  he #endif
   1948  1.19.4.2  he 
   1949  1.19.4.2  he 	ix = im->im_xfer;
   1950  1.19.4.2  he 	dm = ix->ix_map;
   1951  1.19.4.2  he 	rv = bus_dmamap_load(sc->sc_dmat, dm, xferaddr, xfersize, NULL,
   1952  1.19.4.2  he 	    BUS_DMA_NOWAIT);
   1953  1.19.4.2  he 	if (rv != 0)
   1954  1.19.4.2  he 		return (rv);
   1955  1.19.4.2  he 
   1956  1.19.4.2  he 	off = mb[0] >> 16;
   1957  1.19.4.2  he 	nsegs = ((IOP_MAX_MSG_SIZE / 4) - off) >> 1;
   1958  1.19.4.2  he 
   1959  1.19.4.2  he 	/*
   1960  1.19.4.2  he 	 * If the transfer is highly fragmented and won't fit using SIMPLE
   1961  1.19.4.2  he 	 * elements, use PAGE_LIST elements instead.  SIMPLE elements are
   1962  1.19.4.2  he 	 * potentially more efficient, both for us and the IOP.
   1963  1.19.4.2  he 	 */
   1964  1.19.4.2  he 	if (dm->dm_nsegs > nsegs) {
   1965  1.19.4.2  he 		nsegs = 1;
   1966  1.19.4.2  he 		p = mb + off + 1;
   1967  1.19.4.2  he 
   1968  1.19.4.2  he 		/* XXX This should be done with a bus_space flag. */
   1969  1.19.4.2  he 		for (i = dm->dm_nsegs, ds = dm->dm_segs; i > 0; i--, ds++) {
   1970  1.19.4.2  he 			slen = ds->ds_len;
   1971  1.19.4.2  he 			saddr = ds->ds_addr;
   1972  1.19.4.2  he 
   1973  1.19.4.2  he 			while (slen > 0) {
   1974  1.19.4.2  he 				eaddr = (saddr + NBPG) & ~(NBPG - 1);
   1975  1.19.4.2  he 				tlen = min(eaddr - saddr, slen);
   1976  1.19.4.2  he 				slen -= tlen;
   1977  1.19.4.2  he 				*p++ = le32toh(saddr);
   1978  1.19.4.2  he 				saddr = eaddr;
   1979  1.19.4.2  he 				nsegs++;
   1980  1.19.4.2  he 			}
   1981  1.19.4.2  he 		}
   1982  1.19.4.2  he 
   1983  1.19.4.2  he 		mb[off] = xfersize | I2O_SGL_PAGE_LIST | I2O_SGL_END_BUFFER |
   1984  1.19.4.2  he 		    I2O_SGL_END;
   1985  1.19.4.2  he 		if (out)
   1986  1.19.4.2  he 			mb[off] |= I2O_SGL_DATA_OUT;
   1987  1.19.4.2  he 	} else {
   1988  1.19.4.2  he 		p = mb + off;
   1989  1.19.4.2  he 		nsegs = dm->dm_nsegs;
   1990  1.19.4.2  he 
   1991  1.19.4.2  he 		if (out)
   1992  1.19.4.2  he 			flg = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   1993  1.19.4.2  he 		else
   1994  1.19.4.2  he 			flg = I2O_SGL_SIMPLE;
   1995  1.19.4.2  he 
   1996  1.19.4.2  he 		for (i = nsegs, ds = dm->dm_segs; i > 1; i--, p += 2, ds++) {
   1997  1.19.4.2  he 			p[0] = (u_int32_t)ds->ds_len | flg;
   1998  1.19.4.2  he 			p[1] = (u_int32_t)ds->ds_addr;
   1999  1.19.4.2  he 		}
   2000  1.19.4.2  he 
   2001  1.19.4.2  he 		p[0] = (u_int32_t)ds->ds_len | flg | I2O_SGL_END_BUFFER |
   2002  1.19.4.2  he 		    I2O_SGL_END;
   2003  1.19.4.2  he 		p[1] = (u_int32_t)ds->ds_addr;
   2004  1.19.4.2  he 		nsegs <<= 1;
   2005  1.19.4.2  he 	}
   2006  1.19.4.2  he 
   2007  1.19.4.2  he 	/* Fix up the transfer record, and sync the map. */
   2008  1.19.4.2  he 	ix->ix_flags = (out ? IX_OUT : IX_IN);
   2009  1.19.4.2  he 	ix->ix_size = xfersize;
   2010  1.19.4.2  he 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   2011  1.19.4.2  he 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   2012  1.19.4.2  he 
   2013  1.19.4.2  he 	/*
   2014  1.19.4.2  he 	 * Adjust the SGL offset and total message size fields.  We don't
   2015  1.19.4.2  he 	 * set IM_SGLOFFADJ, since it's used only for SIMPLE elements.
   2016  1.19.4.2  he 	 */
   2017  1.19.4.2  he 	mb[0] += ((off << 4) + (nsegs << 16));
   2018  1.19.4.2  he 	return (0);
   2019  1.19.4.2  he }
   2020  1.19.4.2  he 
   2021  1.19.4.2  he /*
   2022  1.19.4.2  he  * Unmap all data transfers associated with a message wrapper.
   2023  1.19.4.2  he  */
   2024  1.19.4.2  he void
   2025  1.19.4.2  he iop_msg_unmap(struct iop_softc *sc, struct iop_msg *im)
   2026  1.19.4.2  he {
   2027  1.19.4.2  he 	struct iop_xfer *ix;
   2028  1.19.4.2  he 	int i;
   2029  1.19.4.2  he 
   2030  1.19.4.2  he #ifdef I2ODEBUG
   2031  1.19.4.2  he 	if (im->im_xfer[0].ix_size == 0)
   2032  1.19.4.2  he 		panic("iop_msg_unmap: no transfers mapped");
   2033  1.19.4.2  he #endif
   2034  1.19.4.2  he 
   2035  1.19.4.2  he 	for (ix = im->im_xfer, i = 0;;) {
   2036  1.19.4.2  he 		bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, ix->ix_size,
   2037  1.19.4.2  he 		    ix->ix_flags & IX_OUT ? BUS_DMASYNC_POSTWRITE :
   2038  1.19.4.2  he 		    BUS_DMASYNC_POSTREAD);
   2039  1.19.4.2  he 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   2040  1.19.4.2  he 
   2041  1.19.4.2  he 		/* Only the first DMA map is static. */
   2042  1.19.4.2  he 		if (i != 0)
   2043  1.19.4.2  he 			bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   2044  1.19.4.2  he 		if ((++ix)->ix_size == 0)
   2045  1.19.4.2  he 			break;
   2046  1.19.4.2  he 		if (++i >= IOP_MAX_MSG_XFERS)
   2047  1.19.4.2  he 			break;
   2048  1.19.4.2  he 	}
   2049  1.19.4.2  he }
   2050  1.19.4.2  he 
   2051  1.19.4.2  he /*
   2052  1.19.4.2  he  * Post a message frame to the IOP's inbound queue.
   2053  1.19.4.2  he  */
   2054  1.19.4.2  he int
   2055  1.19.4.2  he iop_post(struct iop_softc *sc, u_int32_t *mb)
   2056  1.19.4.2  he {
   2057  1.19.4.2  he 	u_int32_t mfa;
   2058  1.19.4.2  he 	int s;
   2059  1.19.4.2  he 
   2060  1.19.4.2  he #ifdef I2ODEBUG
   2061  1.19.4.2  he 	if ((mb[0] >> 16) > IOP_MAX_MSG_SIZE / 4)
   2062  1.19.4.2  he 		panic("iop_post: frame too large");
   2063  1.19.4.2  he #endif
   2064  1.19.4.2  he 
   2065  1.19.4.2  he 	s = splbio();
   2066  1.19.4.2  he 
   2067  1.19.4.2  he 	/* Allocate a slot with the IOP. */
   2068  1.19.4.2  he 	if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY)
   2069  1.19.4.2  he 		if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY) {
   2070  1.19.4.2  he 			splx(s);
   2071  1.19.4.2  he 			printf("%s: mfa not forthcoming\n",
   2072  1.19.4.2  he 			    sc->sc_dv.dv_xname);
   2073  1.19.4.2  he 			return (EAGAIN);
   2074  1.19.4.2  he 		}
   2075  1.19.4.2  he 
   2076  1.19.4.2  he 	/* Perform reply buffer DMA synchronisation. */
   2077  1.19.4.2  he 	if (sc->sc_curib++ == 0)
   2078  1.19.4.2  he 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   2079  1.19.4.2  he 		    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   2080  1.19.4.2  he 
   2081  1.19.4.2  he 	/* Copy out the message frame. */
   2082  1.19.4.2  he 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa, mb, mb[0] >> 16);
   2083  1.19.4.2  he 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa, (mb[0] >> 14) & ~3,
   2084  1.19.4.2  he 	    BUS_SPACE_BARRIER_WRITE);
   2085  1.19.4.2  he 
   2086  1.19.4.2  he 	/* Post the MFA back to the IOP. */
   2087  1.19.4.2  he 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   2088  1.19.4.2  he 
   2089  1.19.4.2  he 	splx(s);
   2090  1.19.4.2  he 	return (0);
   2091  1.19.4.2  he }
   2092  1.19.4.2  he 
   2093  1.19.4.2  he /*
   2094  1.19.4.2  he  * Post a message to the IOP and deal with completion.
   2095  1.19.4.2  he  */
   2096  1.19.4.2  he int
   2097  1.19.4.2  he iop_msg_post(struct iop_softc *sc, struct iop_msg *im, void *xmb, int timo)
   2098  1.19.4.2  he {
   2099  1.19.4.2  he 	u_int32_t *mb;
   2100  1.19.4.2  he 	int rv, s;
   2101  1.19.4.2  he 
   2102  1.19.4.2  he 	mb = xmb;
   2103  1.19.4.2  he 
   2104  1.19.4.2  he 	/* Terminate the scatter/gather list chain. */
   2105  1.19.4.2  he 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   2106  1.19.4.2  he 		mb[(mb[0] >> 16) - 2] |= I2O_SGL_END;
   2107  1.19.4.2  he 
   2108  1.19.4.2  he 	if ((rv = iop_post(sc, mb)) != 0)
   2109  1.19.4.2  he 		return (rv);
   2110  1.19.4.2  he 
   2111  1.19.4.2  he 	if ((im->im_flags & (IM_POLL | IM_WAIT)) != 0) {
   2112  1.19.4.2  he 		if ((im->im_flags & IM_POLL) != 0)
   2113  1.19.4.2  he 			iop_msg_poll(sc, im, timo);
   2114  1.19.4.2  he 		else
   2115  1.19.4.2  he 			iop_msg_wait(sc, im, timo);
   2116  1.19.4.2  he 
   2117  1.19.4.2  he 		s = splbio();
   2118  1.19.4.2  he 		if ((im->im_flags & IM_REPLIED) != 0) {
   2119  1.19.4.2  he 			if ((im->im_flags & IM_NOSTATUS) != 0)
   2120  1.19.4.2  he 				rv = 0;
   2121  1.19.4.2  he 			else if ((im->im_flags & IM_FAIL) != 0)
   2122  1.19.4.2  he 				rv = ENXIO;
   2123  1.19.4.2  he 			else if (im->im_reqstatus != I2O_STATUS_SUCCESS)
   2124  1.19.4.2  he 				rv = EIO;
   2125  1.19.4.2  he 			else
   2126  1.19.4.2  he 				rv = 0;
   2127  1.19.4.2  he 		} else
   2128  1.19.4.2  he 			rv = EBUSY;
   2129  1.19.4.2  he 		splx(s);
   2130  1.19.4.2  he 	} else
   2131  1.19.4.2  he 		rv = 0;
   2132  1.19.4.2  he 
   2133  1.19.4.2  he 	return (rv);
   2134  1.19.4.2  he }
   2135  1.19.4.2  he 
   2136  1.19.4.2  he /*
   2137  1.19.4.2  he  * Spin until the specified message is replied to.
   2138  1.19.4.2  he  */
   2139  1.19.4.2  he static void
   2140  1.19.4.2  he iop_msg_poll(struct iop_softc *sc, struct iop_msg *im, int timo)
   2141  1.19.4.2  he {
   2142  1.19.4.2  he 	u_int32_t rmfa;
   2143  1.19.4.2  he 	int s, status;
   2144  1.19.4.2  he 
   2145  1.19.4.2  he 	s = splbio();
   2146  1.19.4.2  he 
   2147  1.19.4.2  he 	/* Wait for completion. */
   2148  1.19.4.2  he 	for (timo *= 10; timo != 0; timo--) {
   2149  1.19.4.2  he 		if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0) {
   2150  1.19.4.2  he 			/* Double read to account for IOP bug. */
   2151  1.19.4.2  he 			rmfa = iop_inl(sc, IOP_REG_OFIFO);
   2152  1.19.4.2  he 			if (rmfa == IOP_MFA_EMPTY)
   2153  1.19.4.2  he 				rmfa = iop_inl(sc, IOP_REG_OFIFO);
   2154  1.19.4.2  he 			if (rmfa != IOP_MFA_EMPTY) {
   2155  1.19.4.2  he 				status = iop_handle_reply(sc, rmfa);
   2156  1.19.4.2  he 
   2157  1.19.4.2  he 				/*
   2158  1.19.4.2  he 				 * Return the reply frame to the IOP's
   2159  1.19.4.2  he 				 * outbound FIFO.
   2160  1.19.4.2  he 				 */
   2161  1.19.4.2  he 				iop_outl(sc, IOP_REG_OFIFO, rmfa);
   2162  1.19.4.2  he 			}
   2163  1.19.4.2  he 		}
   2164  1.19.4.2  he 		if ((im->im_flags & IM_REPLIED) != 0)
   2165  1.19.4.2  he 			break;
   2166  1.19.4.2  he 		DELAY(100);
   2167  1.19.4.2  he 	}
   2168  1.19.4.2  he 
   2169  1.19.4.2  he 	if (timo == 0) {
   2170  1.19.4.2  he #ifdef I2ODEBUG
   2171  1.19.4.2  he 		printf("%s: poll - no reply\n", sc->sc_dv.dv_xname);
   2172  1.19.4.2  he 		if (iop_status_get(sc, 1) != 0)
   2173  1.19.4.2  he 			printf("iop_msg_poll: unable to retrieve status\n");
   2174  1.19.4.2  he 		else
   2175  1.19.4.2  he 			printf("iop_msg_poll: IOP state = %d\n",
   2176  1.19.4.2  he 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   2177  1.19.4.2  he #endif
   2178  1.19.4.2  he 	}
   2179  1.19.4.2  he 
   2180  1.19.4.2  he 	splx(s);
   2181  1.19.4.2  he }
   2182  1.19.4.2  he 
   2183  1.19.4.2  he /*
   2184  1.19.4.2  he  * Sleep until the specified message is replied to.
   2185  1.19.4.2  he  */
   2186  1.19.4.2  he static void
   2187  1.19.4.2  he iop_msg_wait(struct iop_softc *sc, struct iop_msg *im, int timo)
   2188  1.19.4.2  he {
   2189  1.19.4.2  he 	int s, rv;
   2190  1.19.4.2  he 
   2191  1.19.4.2  he 	s = splbio();
   2192  1.19.4.2  he 	if ((im->im_flags & IM_REPLIED) != 0) {
   2193  1.19.4.2  he 		splx(s);
   2194  1.19.4.2  he 		return;
   2195  1.19.4.2  he 	}
   2196  1.19.4.2  he 	rv = tsleep(im, PRIBIO, "iopmsg", timo * hz / 1000);
   2197  1.19.4.2  he 	splx(s);
   2198  1.19.4.2  he 
   2199  1.19.4.2  he #ifdef I2ODEBUG
   2200  1.19.4.2  he 	if (rv != 0) {
   2201  1.19.4.2  he 		printf("iop_msg_wait: tsleep() == %d\n", rv);
   2202  1.19.4.2  he 		if (iop_status_get(sc, 0) != 0)
   2203  1.19.4.2  he 			printf("iop_msg_wait: unable to retrieve status\n");
   2204  1.19.4.2  he 		else
   2205  1.19.4.2  he 			printf("iop_msg_wait: IOP state = %d\n",
   2206  1.19.4.2  he 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   2207  1.19.4.2  he 	}
   2208  1.19.4.2  he #endif
   2209  1.19.4.2  he }
   2210  1.19.4.2  he 
   2211  1.19.4.2  he /*
   2212  1.19.4.2  he  * Release an unused message frame back to the IOP's inbound fifo.
   2213  1.19.4.2  he  */
   2214  1.19.4.2  he static void
   2215  1.19.4.2  he iop_release_mfa(struct iop_softc *sc, u_int32_t mfa)
   2216  1.19.4.2  he {
   2217  1.19.4.2  he 
   2218  1.19.4.2  he 	/* Use the frame to issue a no-op. */
   2219  1.19.4.2  he 	iop_outl(sc, mfa, I2O_VERSION_11 | (4 << 16));
   2220  1.19.4.2  he 	iop_outl(sc, mfa + 4, I2O_MSGFUNC(I2O_TID_IOP, I2O_UTIL_NOP));
   2221  1.19.4.2  he 	iop_outl(sc, mfa + 8, 0);
   2222  1.19.4.2  he 	iop_outl(sc, mfa + 12, 0);
   2223  1.19.4.2  he 
   2224  1.19.4.2  he 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   2225  1.19.4.2  he }
   2226  1.19.4.2  he 
   2227  1.19.4.2  he #ifdef I2ODEBUG
   2228  1.19.4.2  he /*
   2229  1.19.4.2  he  * Dump a reply frame header.
   2230  1.19.4.2  he  */
   2231  1.19.4.2  he static void
   2232  1.19.4.2  he iop_reply_print(struct iop_softc *sc, struct i2o_reply *rb)
   2233  1.19.4.2  he {
   2234  1.19.4.2  he 	u_int function, detail;
   2235  1.19.4.2  he #ifdef I2OVERBOSE
   2236  1.19.4.2  he 	const char *statusstr;
   2237  1.19.4.2  he #endif
   2238  1.19.4.2  he 
   2239  1.19.4.2  he 	function = (le32toh(rb->msgfunc) >> 24) & 0xff;
   2240  1.19.4.2  he 	detail = le16toh(rb->detail);
   2241  1.19.4.2  he 
   2242  1.19.4.2  he 	printf("%s: reply:\n", sc->sc_dv.dv_xname);
   2243  1.19.4.2  he 
   2244  1.19.4.2  he #ifdef I2OVERBOSE
   2245  1.19.4.2  he 	if (rb->reqstatus < sizeof(iop_status) / sizeof(iop_status[0]))
   2246  1.19.4.2  he 		statusstr = iop_status[rb->reqstatus];
   2247  1.19.4.2  he 	else
   2248  1.19.4.2  he 		statusstr = "undefined error code";
   2249  1.19.4.2  he 
   2250  1.19.4.2  he 	printf("%s:   function=0x%02x status=0x%02x (%s)\n",
   2251  1.19.4.2  he 	    sc->sc_dv.dv_xname, function, rb->reqstatus, statusstr);
   2252  1.19.4.2  he #else
   2253  1.19.4.2  he 	printf("%s:   function=0x%02x status=0x%02x\n",
   2254  1.19.4.2  he 	    sc->sc_dv.dv_xname, function, rb->reqstatus);
   2255  1.19.4.2  he #endif
   2256  1.19.4.2  he 	printf("%s:   detail=0x%04x ictx=0x%08x tctx=0x%08x\n",
   2257  1.19.4.2  he 	    sc->sc_dv.dv_xname, detail, le32toh(rb->msgictx),
   2258  1.19.4.2  he 	    le32toh(rb->msgtctx));
   2259  1.19.4.2  he 	printf("%s:   tidi=%d tidt=%d flags=0x%02x\n", sc->sc_dv.dv_xname,
   2260  1.19.4.2  he 	    (le32toh(rb->msgfunc) >> 12) & 4095, le32toh(rb->msgfunc) & 4095,
   2261  1.19.4.2  he 	    (le32toh(rb->msgflags) >> 8) & 0xff);
   2262  1.19.4.2  he }
   2263  1.19.4.2  he #endif
   2264  1.19.4.2  he 
   2265  1.19.4.2  he /*
   2266  1.19.4.2  he  * Dump a transport failure reply.
   2267  1.19.4.2  he  */
   2268  1.19.4.2  he static void
   2269  1.19.4.2  he iop_tfn_print(struct iop_softc *sc, struct i2o_fault_notify *fn)
   2270  1.19.4.2  he {
   2271  1.19.4.2  he 
   2272  1.19.4.2  he 	printf("%s: WARNING: transport failure:\n", sc->sc_dv.dv_xname);
   2273  1.19.4.2  he 
   2274  1.19.4.2  he 	printf("%s:   ictx=0x%08x tctx=0x%08x\n", sc->sc_dv.dv_xname,
   2275  1.19.4.2  he 	    le32toh(fn->msgictx), le32toh(fn->msgtctx));
   2276  1.19.4.2  he 	printf("%s:  failurecode=0x%02x severity=0x%02x\n",
   2277  1.19.4.2  he 	    sc->sc_dv.dv_xname, fn->failurecode, fn->severity);
   2278  1.19.4.2  he 	printf("%s:  highestver=0x%02x lowestver=0x%02x\n",
   2279  1.19.4.2  he 	    sc->sc_dv.dv_xname, fn->highestver, fn->lowestver);
   2280  1.19.4.2  he }
   2281  1.19.4.2  he 
   2282  1.19.4.2  he /*
   2283  1.19.4.2  he  * Translate an I2O ASCII field into a C string.
   2284  1.19.4.2  he  */
   2285  1.19.4.2  he void
   2286  1.19.4.2  he iop_strvis(struct iop_softc *sc, const char *src, int slen, char *dst, int dlen)
   2287  1.19.4.2  he {
   2288  1.19.4.2  he 	int hc, lc, i, nit;
   2289  1.19.4.2  he 
   2290  1.19.4.2  he 	dlen--;
   2291  1.19.4.2  he 	lc = 0;
   2292  1.19.4.2  he 	hc = 0;
   2293  1.19.4.2  he 	i = 0;
   2294  1.19.4.2  he 
   2295  1.19.4.2  he 	/*
   2296  1.19.4.2  he 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
   2297  1.19.4.2  he 	 * spec has nothing to say about it.  Since AMI fields are usually
   2298  1.19.4.2  he 	 * filled with junk after the terminator, ...
   2299  1.19.4.2  he 	 */
   2300  1.19.4.2  he 	nit = (le16toh(sc->sc_status.orgid) != I2O_ORG_DPT);
   2301  1.19.4.2  he 
   2302  1.19.4.2  he 	while (slen-- != 0 && dlen-- != 0) {
   2303  1.19.4.2  he 		if (nit && *src == '\0')
   2304  1.19.4.2  he 			break;
   2305  1.19.4.2  he 		else if (*src <= 0x20 || *src >= 0x7f) {
   2306  1.19.4.2  he 			if (hc)
   2307  1.19.4.2  he 				dst[i++] = ' ';
   2308  1.19.4.2  he 		} else {
   2309  1.19.4.2  he 			hc = 1;
   2310  1.19.4.2  he 			dst[i++] = *src;
   2311  1.19.4.2  he 			lc = i;
   2312  1.19.4.2  he 		}
   2313  1.19.4.2  he 		src++;
   2314  1.19.4.2  he 	}
   2315  1.19.4.2  he 
   2316  1.19.4.2  he 	dst[lc] = '\0';
   2317  1.19.4.2  he }
   2318  1.19.4.2  he 
   2319  1.19.4.2  he /*
   2320  1.19.4.2  he  * Retrieve the DEVICE_IDENTITY parameter group from the target and dump it.
   2321  1.19.4.2  he  */
   2322  1.19.4.2  he int
   2323  1.19.4.2  he iop_print_ident(struct iop_softc *sc, int tid)
   2324  1.19.4.2  he {
   2325  1.19.4.2  he 	struct {
   2326  1.19.4.2  he 		struct	i2o_param_op_results pr;
   2327  1.19.4.2  he 		struct	i2o_param_read_results prr;
   2328  1.19.4.2  he 		struct	i2o_param_device_identity di;
   2329  1.19.4.2  he 	} __attribute__ ((__packed__)) p;
   2330  1.19.4.2  he 	char buf[32];
   2331  1.19.4.2  he 	int rv;
   2332  1.19.4.2  he 
   2333  1.19.4.2  he 	rv = iop_field_get_all(sc, tid, I2O_PARAM_DEVICE_IDENTITY, &p,
   2334  1.19.4.2  he 	    sizeof(p), NULL);
   2335  1.19.4.2  he 	if (rv != 0)
   2336  1.19.4.2  he 		return (rv);
   2337  1.19.4.2  he 
   2338  1.19.4.2  he 	iop_strvis(sc, p.di.vendorinfo, sizeof(p.di.vendorinfo), buf,
   2339  1.19.4.2  he 	    sizeof(buf));
   2340  1.19.4.2  he 	printf(" <%s, ", buf);
   2341  1.19.4.2  he 	iop_strvis(sc, p.di.productinfo, sizeof(p.di.productinfo), buf,
   2342  1.19.4.2  he 	    sizeof(buf));
   2343  1.19.4.2  he 	printf("%s, ", buf);
   2344  1.19.4.2  he 	iop_strvis(sc, p.di.revlevel, sizeof(p.di.revlevel), buf, sizeof(buf));
   2345  1.19.4.2  he 	printf("%s>", buf);
   2346  1.19.4.2  he 
   2347  1.19.4.2  he 	return (0);
   2348  1.19.4.2  he }
   2349  1.19.4.2  he 
   2350  1.19.4.2  he /*
   2351  1.19.4.2  he  * Claim or unclaim the specified TID.
   2352  1.19.4.2  he  */
   2353  1.19.4.2  he int
   2354  1.19.4.2  he iop_util_claim(struct iop_softc *sc, struct iop_initiator *ii, int release,
   2355  1.19.4.2  he 	       int flags)
   2356  1.19.4.2  he {
   2357  1.19.4.2  he 	struct iop_msg *im;
   2358  1.19.4.2  he 	struct i2o_util_claim mf;
   2359  1.19.4.2  he 	int rv, func;
   2360  1.19.4.2  he 
   2361  1.19.4.2  he 	func = release ? I2O_UTIL_CLAIM_RELEASE : I2O_UTIL_CLAIM;
   2362  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   2363  1.19.4.2  he 
   2364  1.19.4.2  he 	/* We can use the same structure, as they're identical. */
   2365  1.19.4.2  he 	mf.msgflags = I2O_MSGFLAGS(i2o_util_claim);
   2366  1.19.4.2  he 	mf.msgfunc = I2O_MSGFUNC(ii->ii_tid, func);
   2367  1.19.4.2  he 	mf.msgictx = ii->ii_ictx;
   2368  1.19.4.2  he 	mf.msgtctx = im->im_tctx;
   2369  1.19.4.2  he 	mf.flags = flags;
   2370  1.19.4.2  he 
   2371  1.19.4.2  he 	rv = iop_msg_post(sc, im, &mf, 5000);
   2372  1.19.4.2  he 	iop_msg_free(sc, im);
   2373  1.19.4.2  he 	return (rv);
   2374  1.19.4.2  he }
   2375  1.19.4.2  he 
   2376  1.19.4.2  he /*
   2377  1.19.4.2  he  * Perform an abort.
   2378  1.19.4.2  he  */
   2379  1.19.4.2  he int iop_util_abort(struct iop_softc *sc, struct iop_initiator *ii, int func,
   2380  1.19.4.2  he 		   int tctxabort, int flags)
   2381  1.19.4.2  he {
   2382  1.19.4.2  he 	struct iop_msg *im;
   2383  1.19.4.2  he 	struct i2o_util_abort mf;
   2384  1.19.4.2  he 	int rv;
   2385  1.19.4.2  he 
   2386  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT);
   2387  1.19.4.2  he 
   2388  1.19.4.2  he 	mf.msgflags = I2O_MSGFLAGS(i2o_util_abort);
   2389  1.19.4.2  he 	mf.msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_ABORT);
   2390  1.19.4.2  he 	mf.msgictx = ii->ii_ictx;
   2391  1.19.4.2  he 	mf.msgtctx = im->im_tctx;
   2392  1.19.4.2  he 	mf.flags = (func << 24) | flags;
   2393  1.19.4.2  he 	mf.tctxabort = tctxabort;
   2394  1.19.4.2  he 
   2395  1.19.4.2  he 	rv = iop_msg_post(sc, im, &mf, 5000);
   2396  1.19.4.2  he 	iop_msg_free(sc, im);
   2397  1.19.4.2  he 	return (rv);
   2398  1.19.4.2  he }
   2399  1.19.4.2  he 
   2400  1.19.4.2  he /*
   2401  1.19.4.2  he  * Enable or disable reception of events for the specified device.
   2402  1.19.4.2  he  */
   2403  1.19.4.2  he int iop_util_eventreg(struct iop_softc *sc, struct iop_initiator *ii, int mask)
   2404  1.19.4.2  he {
   2405  1.19.4.2  he 	struct i2o_util_event_register mf;
   2406  1.19.4.2  he 
   2407  1.19.4.2  he 	mf.msgflags = I2O_MSGFLAGS(i2o_util_event_register);
   2408  1.19.4.2  he 	mf.msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_EVENT_REGISTER);
   2409  1.19.4.2  he 	mf.msgictx = ii->ii_ictx;
   2410  1.19.4.2  he 	mf.msgtctx = 0;
   2411  1.19.4.2  he 	mf.eventmask = mask;
   2412  1.19.4.2  he 
   2413  1.19.4.2  he 	/* This message is replied to only when events are signalled. */
   2414  1.19.4.2  he 	return (iop_post(sc, (u_int32_t *)&mf));
   2415  1.19.4.2  he }
   2416  1.19.4.2  he 
   2417  1.19.4.2  he int
   2418  1.19.4.2  he iopopen(dev_t dev, int flag, int mode, struct proc *p)
   2419  1.19.4.2  he {
   2420  1.19.4.2  he 	struct iop_softc *sc;
   2421  1.19.4.2  he 
   2422  1.19.4.2  he 	if ((sc = device_lookup(&iop_cd, minor(dev))) == NULL)
   2423  1.19.4.2  he 		return (ENXIO);
   2424  1.19.4.2  he 	if ((sc->sc_flags & IOP_ONLINE) == 0)
   2425  1.19.4.2  he 		return (ENXIO);
   2426  1.19.4.2  he 	if ((sc->sc_flags & IOP_OPEN) != 0)
   2427  1.19.4.2  he 		return (EBUSY);
   2428  1.19.4.2  he 	sc->sc_flags |= IOP_OPEN;
   2429  1.19.4.2  he 
   2430  1.19.4.2  he 	return (0);
   2431  1.19.4.2  he }
   2432  1.19.4.2  he 
   2433  1.19.4.2  he int
   2434  1.19.4.2  he iopclose(dev_t dev, int flag, int mode, struct proc *p)
   2435  1.19.4.2  he {
   2436  1.19.4.2  he 	struct iop_softc *sc;
   2437  1.19.4.2  he 
   2438  1.19.4.2  he 	sc = device_lookup(&iop_cd, minor(dev));
   2439  1.19.4.2  he 	sc->sc_flags &= ~IOP_OPEN;
   2440  1.19.4.2  he 
   2441  1.19.4.2  he 	return (0);
   2442  1.19.4.2  he }
   2443  1.19.4.2  he 
   2444  1.19.4.2  he int
   2445  1.19.4.2  he iopioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   2446  1.19.4.2  he {
   2447  1.19.4.2  he 	struct iop_softc *sc;
   2448  1.19.4.2  he 	struct iovec *iov;
   2449  1.19.4.2  he 	int rv, i;
   2450  1.19.4.2  he 
   2451  1.19.4.2  he 	if (securelevel >= 2)
   2452  1.19.4.2  he 		return (EPERM);
   2453  1.19.4.2  he 
   2454  1.19.4.2  he 	sc = device_lookup(&iop_cd, minor(dev));
   2455  1.19.4.2  he 
   2456  1.19.4.2  he 	switch (cmd) {
   2457  1.19.4.2  he 	case IOPIOCPT:
   2458  1.19.4.2  he 		return (iop_passthrough(sc, (struct ioppt *)data, p));
   2459  1.19.4.2  he 
   2460  1.19.4.2  he 	case IOPIOCGSTATUS:
   2461  1.19.4.2  he 		iov = (struct iovec *)data;
   2462  1.19.4.2  he 		i = sizeof(struct i2o_status);
   2463  1.19.4.2  he 		if (i > iov->iov_len)
   2464  1.19.4.2  he 			i = iov->iov_len;
   2465  1.19.4.2  he 		else
   2466  1.19.4.2  he 			iov->iov_len = i;
   2467  1.19.4.2  he 		if ((rv = iop_status_get(sc, 0)) == 0)
   2468  1.19.4.2  he 			rv = copyout(&sc->sc_status, iov->iov_base, i);
   2469  1.19.4.2  he 		return (rv);
   2470  1.19.4.2  he 
   2471  1.19.4.2  he 	case IOPIOCGLCT:
   2472  1.19.4.2  he 	case IOPIOCGTIDMAP:
   2473  1.19.4.2  he 	case IOPIOCRECONFIG:
   2474  1.19.4.2  he 		break;
   2475  1.19.4.2  he 
   2476  1.19.4.2  he 	default:
   2477  1.19.4.2  he #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
   2478  1.19.4.2  he 		printf("%s: unknown ioctl %lx\n", sc->sc_dv.dv_xname, cmd);
   2479  1.19.4.2  he #endif
   2480  1.19.4.2  he 		return (ENOTTY);
   2481  1.19.4.2  he 	}
   2482  1.19.4.2  he 
   2483  1.19.4.2  he 	if ((rv = lockmgr(&sc->sc_conflock, LK_SHARED, NULL)) != 0)
   2484  1.19.4.2  he 		return (rv);
   2485  1.19.4.2  he 
   2486  1.19.4.2  he 	switch (cmd) {
   2487  1.19.4.2  he 	case IOPIOCGLCT:
   2488  1.19.4.2  he 		iov = (struct iovec *)data;
   2489  1.19.4.2  he 		i = le16toh(sc->sc_lct->tablesize) << 2;
   2490  1.19.4.2  he 		if (i > iov->iov_len)
   2491  1.19.4.2  he 			i = iov->iov_len;
   2492  1.19.4.2  he 		else
   2493  1.19.4.2  he 			iov->iov_len = i;
   2494  1.19.4.2  he 		rv = copyout(sc->sc_lct, iov->iov_base, i);
   2495  1.19.4.2  he 		break;
   2496  1.19.4.2  he 
   2497  1.19.4.2  he 	case IOPIOCRECONFIG:
   2498  1.19.4.2  he 		rv = iop_reconfigure(sc, 0);
   2499  1.19.4.2  he 		break;
   2500  1.19.4.2  he 
   2501  1.19.4.2  he 	case IOPIOCGTIDMAP:
   2502  1.19.4.2  he 		iov = (struct iovec *)data;
   2503  1.19.4.2  he 		i = sizeof(struct iop_tidmap) * sc->sc_nlctent;
   2504  1.19.4.2  he 		if (i > iov->iov_len)
   2505  1.19.4.2  he 			i = iov->iov_len;
   2506  1.19.4.2  he 		else
   2507  1.19.4.2  he 			iov->iov_len = i;
   2508  1.19.4.2  he 		rv = copyout(sc->sc_tidmap, iov->iov_base, i);
   2509  1.19.4.2  he 		break;
   2510  1.19.4.2  he 	}
   2511  1.19.4.2  he 
   2512  1.19.4.2  he 	lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
   2513  1.19.4.2  he 	return (rv);
   2514  1.19.4.2  he }
   2515  1.19.4.2  he 
   2516  1.19.4.2  he static int
   2517  1.19.4.2  he iop_passthrough(struct iop_softc *sc, struct ioppt *pt, struct proc *p)
   2518  1.19.4.2  he {
   2519  1.19.4.2  he 	struct iop_msg *im;
   2520  1.19.4.2  he 	struct i2o_msg *mf;
   2521  1.19.4.2  he 	struct ioppt_buf *ptb;
   2522  1.19.4.2  he 	int rv, i, mapped;
   2523  1.19.4.2  he 
   2524  1.19.4.2  he 	mf = NULL;
   2525  1.19.4.2  he 	im = NULL;
   2526  1.19.4.2  he 	mapped = 1;
   2527  1.19.4.2  he 
   2528  1.19.4.2  he 	if (pt->pt_msglen > IOP_MAX_MSG_SIZE ||
   2529  1.19.4.2  he 	    pt->pt_msglen > (le16toh(sc->sc_status.inboundmframesize) << 2) ||
   2530  1.19.4.2  he 	    pt->pt_msglen < sizeof(struct i2o_msg) ||
   2531  1.19.4.2  he 	    pt->pt_nbufs > IOP_MAX_MSG_XFERS ||
   2532  1.19.4.2  he 	    pt->pt_nbufs < 0 || pt->pt_replylen < 0 ||
   2533  1.19.4.2  he             pt->pt_timo < 1000 || pt->pt_timo > 5*60*1000)
   2534  1.19.4.2  he 		return (EINVAL);
   2535  1.19.4.2  he 
   2536  1.19.4.2  he 	for (i = 0; i < pt->pt_nbufs; i++)
   2537  1.19.4.2  he 		if (pt->pt_bufs[i].ptb_datalen > IOP_MAX_XFER) {
   2538  1.19.4.2  he 			rv = ENOMEM;
   2539  1.19.4.2  he 			goto bad;
   2540  1.19.4.2  he 		}
   2541  1.19.4.2  he 
   2542  1.19.4.2  he 	mf = malloc(IOP_MAX_MSG_SIZE, M_DEVBUF, M_WAITOK);
   2543  1.19.4.2  he 	if (mf == NULL)
   2544  1.19.4.2  he 		return (ENOMEM);
   2545  1.19.4.2  he 
   2546  1.19.4.2  he 	if ((rv = copyin(pt->pt_msg, mf, pt->pt_msglen)) != 0)
   2547  1.19.4.2  he 		goto bad;
   2548  1.19.4.2  he 
   2549  1.19.4.2  he 	im = iop_msg_alloc(sc, IM_WAIT | IM_NOSTATUS);
   2550  1.19.4.2  he 	im->im_rb = (struct i2o_reply *)mf;
   2551  1.19.4.2  he 	mf->msgictx = IOP_ICTX;
   2552  1.19.4.2  he 	mf->msgtctx = im->im_tctx;
   2553  1.19.4.2  he 
   2554  1.19.4.2  he 	for (i = 0; i < pt->pt_nbufs; i++) {
   2555  1.19.4.2  he 		ptb = &pt->pt_bufs[i];
   2556  1.19.4.2  he 		rv = iop_msg_map(sc, im, (u_int32_t *)mf, ptb->ptb_data,
   2557  1.19.4.2  he 		    ptb->ptb_datalen, ptb->ptb_out != 0, p);
   2558  1.19.4.2  he 		if (rv != 0)
   2559  1.19.4.2  he 			goto bad;
   2560  1.19.4.2  he 		mapped = 1;
   2561  1.19.4.2  he 	}
   2562  1.19.4.2  he 
   2563  1.19.4.2  he 	if ((rv = iop_msg_post(sc, im, mf, pt->pt_timo)) != 0)
   2564  1.19.4.2  he 		goto bad;
   2565  1.19.4.2  he 
   2566  1.19.4.2  he 	i = (le32toh(im->im_rb->msgflags) >> 14) & ~3;
   2567  1.19.4.2  he 	if (i > IOP_MAX_MSG_SIZE)
   2568  1.19.4.2  he 		i = IOP_MAX_MSG_SIZE;
   2569  1.19.4.2  he 	if (i > pt->pt_replylen)
   2570  1.19.4.2  he 		i = pt->pt_replylen;
   2571  1.19.4.2  he 	rv = copyout(im->im_rb, pt->pt_reply, i);
   2572  1.19.4.2  he 
   2573  1.19.4.2  he  bad:
   2574  1.19.4.2  he 	if (mapped != 0)
   2575  1.19.4.2  he 		iop_msg_unmap(sc, im);
   2576  1.19.4.2  he 	if (im != NULL)
   2577  1.19.4.2  he 		iop_msg_free(sc, im);
   2578  1.19.4.2  he 	if (mf != NULL)
   2579  1.19.4.2  he 		free(mf, M_DEVBUF);
   2580  1.19.4.2  he 	return (rv);
   2581  1.19.4.2  he }
   2582