Home | History | Annotate | Line # | Download | only in i2o
iop.c revision 1.7
      1  1.7       ad /*	$NetBSD: iop.c,v 1.7 2000/12/03 15:51:36 ad Exp $	*/
      2  1.1       ad 
      3  1.1       ad /*-
      4  1.1       ad  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1       ad  * All rights reserved.
      6  1.1       ad  *
      7  1.1       ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1       ad  * by Andrew Doran.
      9  1.1       ad  *
     10  1.1       ad  * Redistribution and use in source and binary forms, with or without
     11  1.1       ad  * modification, are permitted provided that the following conditions
     12  1.1       ad  * are met:
     13  1.1       ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1       ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1       ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       ad  *    documentation and/or other materials provided with the distribution.
     18  1.1       ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1       ad  *    must display the following acknowledgement:
     20  1.1       ad  *        This product includes software developed by the NetBSD
     21  1.1       ad  *        Foundation, Inc. and its contributors.
     22  1.1       ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1       ad  *    contributors may be used to endorse or promote products derived
     24  1.1       ad  *    from this software without specific prior written permission.
     25  1.1       ad  *
     26  1.1       ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1       ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1       ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1       ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1       ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1       ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1       ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1       ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1       ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1       ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1       ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1       ad  */
     38  1.1       ad 
     39  1.1       ad /*
     40  1.1       ad  * Support for I2O IOPs (intelligent I/O processors).
     41  1.1       ad  */
     42  1.1       ad 
     43  1.1       ad #include "opt_i2o.h"
     44  1.5       ad #include "iop.h"
     45  1.1       ad 
     46  1.1       ad #include <sys/param.h>
     47  1.1       ad #include <sys/systm.h>
     48  1.1       ad #include <sys/kernel.h>
     49  1.1       ad #include <sys/device.h>
     50  1.1       ad #include <sys/queue.h>
     51  1.1       ad #include <sys/proc.h>
     52  1.1       ad #include <sys/malloc.h>
     53  1.1       ad #include <sys/ioctl.h>
     54  1.1       ad #include <sys/endian.h>
     55  1.1       ad #include <sys/pool.h>
     56  1.5       ad #include <sys/conf.h>
     57  1.5       ad #include <sys/kthread.h>
     58  1.1       ad 
     59  1.4  thorpej #include <uvm/uvm_extern.h>
     60  1.4  thorpej 
     61  1.1       ad #include <machine/bus.h>
     62  1.1       ad 
     63  1.1       ad #include <dev/i2o/i2o.h>
     64  1.1       ad #include <dev/i2o/iopreg.h>
     65  1.1       ad #include <dev/i2o/iopvar.h>
     66  1.1       ad 
     67  1.1       ad #define POLL(ms, cond)				\
     68  1.1       ad do {						\
     69  1.1       ad 	int i;					\
     70  1.1       ad 	for (i = (ms) * 10; i; i--) {		\
     71  1.1       ad 		if (cond)			\
     72  1.1       ad 			break;			\
     73  1.1       ad 		DELAY(100);			\
     74  1.1       ad 	}					\
     75  1.1       ad } while (/* CONSTCOND */0);
     76  1.1       ad 
     77  1.1       ad #ifdef I2ODEBUG
     78  1.1       ad #define DPRINTF(x)	printf x
     79  1.1       ad #else
     80  1.1       ad #define	DPRINTF(x)
     81  1.1       ad #endif
     82  1.1       ad 
     83  1.1       ad #ifdef I2OVERBOSE
     84  1.5       ad #define IFVERBOSE(x)	x
     85  1.1       ad #else
     86  1.1       ad #define	IFVERBOSE(x)
     87  1.1       ad #endif
     88  1.1       ad 
     89  1.5       ad #define	COMMENT(x)	""
     90  1.1       ad 
     91  1.5       ad #define IOP_ICTXHASH_NBUCKETS	16
     92  1.5       ad #define	IOP_ICTXHASH(ictx)	(&iop_ictxhashtbl[(ictx) & iop_ictxhash])
     93  1.5       ad #define IOP_TCTXHASH_NBUCKETS	64
     94  1.5       ad #define	IOP_TCTXHASH(tctx)	(&iop_tctxhashtbl[(tctx) & iop_tctxhash])
     95  1.5       ad 
     96  1.5       ad static LIST_HEAD(, iop_initiator) *iop_ictxhashtbl;
     97  1.5       ad static u_long	iop_ictxhash;
     98  1.5       ad static TAILQ_HEAD(, iop_msg) *iop_tctxhashtbl;
     99  1.5       ad static u_long	iop_tctxhash;
    100  1.1       ad static void	*iop_sdh;
    101  1.1       ad static struct	pool *iop_msgpool;
    102  1.5       ad static struct	i2o_systab *iop_systab;
    103  1.5       ad static int	iop_systab_size;
    104  1.1       ad 
    105  1.1       ad extern struct cfdriver iop_cd;
    106  1.1       ad 
    107  1.5       ad #define	IC_CONFIGURE	0x01
    108  1.1       ad 
    109  1.1       ad struct iop_class {
    110  1.5       ad 	u_short	ic_class;
    111  1.5       ad 	u_short	ic_flags;
    112  1.1       ad 	const char	*ic_caption;
    113  1.1       ad } static const iop_class[] = {
    114  1.1       ad 	{
    115  1.1       ad 		I2O_CLASS_EXECUTIVE,
    116  1.1       ad 		0,
    117  1.5       ad 		COMMENT("executive")
    118  1.1       ad 	},
    119  1.1       ad 	{
    120  1.1       ad 		I2O_CLASS_DDM,
    121  1.1       ad 		0,
    122  1.5       ad 		COMMENT("device driver module")
    123  1.1       ad 	},
    124  1.1       ad 	{
    125  1.1       ad 		I2O_CLASS_RANDOM_BLOCK_STORAGE,
    126  1.5       ad 		IC_CONFIGURE,
    127  1.1       ad 		IFVERBOSE("random block storage")
    128  1.1       ad 	},
    129  1.1       ad 	{
    130  1.1       ad 		I2O_CLASS_SEQUENTIAL_STORAGE,
    131  1.5       ad 		IC_CONFIGURE,
    132  1.1       ad 		IFVERBOSE("sequential storage")
    133  1.1       ad 	},
    134  1.1       ad 	{
    135  1.1       ad 		I2O_CLASS_LAN,
    136  1.1       ad 		IC_CONFIGURE,
    137  1.1       ad 		IFVERBOSE("LAN port")
    138  1.1       ad 	},
    139  1.1       ad 	{
    140  1.1       ad 		I2O_CLASS_WAN,
    141  1.1       ad 		IC_CONFIGURE,
    142  1.1       ad 		IFVERBOSE("WAN port")
    143  1.1       ad 	},
    144  1.1       ad 	{
    145  1.1       ad 		I2O_CLASS_FIBRE_CHANNEL_PORT,
    146  1.1       ad 		IC_CONFIGURE,
    147  1.1       ad 		IFVERBOSE("fibrechannel port")
    148  1.1       ad 	},
    149  1.1       ad 	{
    150  1.1       ad 		I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL,
    151  1.1       ad 		0,
    152  1.5       ad 		COMMENT("fibrechannel peripheral")
    153  1.1       ad 	},
    154  1.1       ad  	{
    155  1.1       ad  		I2O_CLASS_SCSI_PERIPHERAL,
    156  1.1       ad  		0,
    157  1.5       ad  		COMMENT("SCSI peripheral")
    158  1.1       ad  	},
    159  1.1       ad 	{
    160  1.1       ad 		I2O_CLASS_ATE_PORT,
    161  1.1       ad 		IC_CONFIGURE,
    162  1.1       ad 		IFVERBOSE("ATE port")
    163  1.1       ad 	},
    164  1.1       ad 	{
    165  1.1       ad 		I2O_CLASS_ATE_PERIPHERAL,
    166  1.1       ad 		0,
    167  1.5       ad 		COMMENT("ATE peripheral")
    168  1.1       ad 	},
    169  1.1       ad 	{
    170  1.1       ad 		I2O_CLASS_FLOPPY_CONTROLLER,
    171  1.1       ad 		IC_CONFIGURE,
    172  1.1       ad 		IFVERBOSE("floppy controller")
    173  1.1       ad 	},
    174  1.1       ad 	{
    175  1.1       ad 		I2O_CLASS_FLOPPY_DEVICE,
    176  1.1       ad 		0,
    177  1.5       ad 		COMMENT("floppy device")
    178  1.1       ad 	},
    179  1.1       ad 	{
    180  1.1       ad 		I2O_CLASS_BUS_ADAPTER_PORT,
    181  1.1       ad 		IC_CONFIGURE,
    182  1.1       ad 		IFVERBOSE("bus adapter port" )
    183  1.1       ad 	},
    184  1.1       ad };
    185  1.1       ad 
    186  1.1       ad #if defined(I2ODEBUG) && defined(I2OVERBOSE)
    187  1.1       ad static const char *iop_status[] = {
    188  1.1       ad 	"success",
    189  1.1       ad 	"abort (dirty)",
    190  1.1       ad 	"abort (no data transfer)",
    191  1.1       ad 	"abort (partial transfer)",
    192  1.1       ad 	"error (dirty)",
    193  1.1       ad 	"error (no data transfer)",
    194  1.1       ad 	"error (partial transfer)",
    195  1.1       ad 	"undefined error code",
    196  1.1       ad 	"process abort (dirty)",
    197  1.1       ad 	"process abort (no data transfer)",
    198  1.1       ad 	"process abort (partial transfer)",
    199  1.1       ad 	"transaction error",
    200  1.1       ad };
    201  1.1       ad #endif
    202  1.1       ad 
    203  1.5       ad static inline u_int32_t	iop_inl(struct iop_softc *, int);
    204  1.5       ad static inline void	iop_outl(struct iop_softc *, int, u_int32_t);
    205  1.5       ad 
    206  1.1       ad static void	iop_config_interrupts(struct device *);
    207  1.5       ad static void	iop_configure_devices(struct iop_softc *);
    208  1.1       ad static void	iop_devinfo(int, char *);
    209  1.1       ad static int	iop_print(void *, const char *);
    210  1.5       ad static int	iop_reconfigure(struct iop_softc *, u_int32_t);
    211  1.1       ad static void	iop_shutdown(void *);
    212  1.1       ad static int	iop_submatch(struct device *, struct cfdata *, void *);
    213  1.5       ad #ifdef notyet
    214  1.1       ad static int	iop_vendor_print(void *, const char *);
    215  1.5       ad #endif
    216  1.1       ad 
    217  1.5       ad static void	iop_intr_event(struct device *, struct iop_msg *, void *);
    218  1.1       ad static int	iop_hrt_get(struct iop_softc *);
    219  1.1       ad static int	iop_hrt_get0(struct iop_softc *, struct i2o_hrt *, int);
    220  1.5       ad static int	iop_lct_get0(struct iop_softc *, struct i2o_lct *, int,
    221  1.5       ad 			     u_int32_t);
    222  1.5       ad static int	iop_msg_wait(struct iop_softc *, struct iop_msg *, int);
    223  1.1       ad static int	iop_ofifo_init(struct iop_softc *);
    224  1.5       ad static int	iop_handle_reply(struct iop_softc *, u_int32_t);
    225  1.5       ad static void	iop_reconfigure_proc(void *);
    226  1.1       ad static void	iop_release_mfa(struct iop_softc *, u_int32_t);
    227  1.1       ad static int	iop_reset(struct iop_softc *);
    228  1.1       ad static int	iop_status_get(struct iop_softc *);
    229  1.1       ad static int	iop_systab_set(struct iop_softc *);
    230  1.1       ad 
    231  1.1       ad #ifdef I2ODEBUG
    232  1.1       ad static void	iop_reply_print(struct iop_softc *, struct iop_msg *,
    233  1.1       ad 				struct i2o_reply *);
    234  1.1       ad #endif
    235  1.1       ad 
    236  1.5       ad cdev_decl(iop);
    237  1.5       ad 
    238  1.5       ad static inline u_int32_t
    239  1.5       ad iop_inl(struct iop_softc *sc, int off)
    240  1.5       ad {
    241  1.5       ad 
    242  1.5       ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    243  1.5       ad 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    244  1.5       ad 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
    245  1.5       ad }
    246  1.5       ad 
    247  1.5       ad static inline void
    248  1.5       ad iop_outl(struct iop_softc *sc, int off, u_int32_t val)
    249  1.5       ad {
    250  1.5       ad 
    251  1.5       ad 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    252  1.5       ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    253  1.5       ad 	    BUS_SPACE_BARRIER_WRITE);
    254  1.5       ad }
    255  1.5       ad 
    256  1.1       ad /*
    257  1.1       ad  * Initialise the adapter.
    258  1.1       ad  */
    259  1.5       ad void
    260  1.1       ad iop_init(struct iop_softc *sc, const char *intrstr)
    261  1.1       ad {
    262  1.1       ad 	int rv;
    263  1.1       ad 	u_int32_t mask;
    264  1.1       ad 	static int again;
    265  1.1       ad 	char ident[64];
    266  1.1       ad 
    267  1.1       ad 	if (again == 0) {
    268  1.5       ad 		/* Create the shared message wrapper pool and hashes. */
    269  1.1       ad 		iop_msgpool = pool_create(sizeof(struct iop_msg), 0, 0, 0,
    270  1.1       ad 		    "ioppl", 0, NULL, NULL, M_DEVBUF);
    271  1.5       ad 		iop_ictxhashtbl = hashinit(IOP_ICTXHASH_NBUCKETS, HASH_LIST,
    272  1.5       ad 		    M_DEVBUF, M_NOWAIT, &iop_ictxhash);
    273  1.5       ad 		iop_tctxhashtbl = hashinit(IOP_TCTXHASH_NBUCKETS, HASH_TAILQ,
    274  1.5       ad 		    M_DEVBUF, M_NOWAIT, &iop_tctxhash);
    275  1.1       ad 		again = 1;
    276  1.1       ad 	}
    277  1.1       ad 
    278  1.5       ad 	/* Reset the IOP and request status. */
    279  1.1       ad 	printf("I2O adapter");
    280  1.5       ad 
    281  1.5       ad 	if ((rv = iop_reset(sc)) != 0) {
    282  1.5       ad 		printf("%s: not responding (reset)\n", sc->sc_dv.dv_xname);
    283  1.5       ad 		return;
    284  1.5       ad 	}
    285  1.1       ad 	if ((rv = iop_status_get(sc)) != 0) {
    286  1.5       ad 		printf("%s: not responding (get status)\n", sc->sc_dv.dv_xname);
    287  1.5       ad 		return;
    288  1.1       ad 	}
    289  1.5       ad 	DPRINTF((" (state=%d)",
    290  1.5       ad 	    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff));
    291  1.5       ad 	sc->sc_flags |= IOP_HAVESTATUS;
    292  1.1       ad 
    293  1.5       ad 	iop_strvis(sc, sc->sc_status.productid, sizeof(sc->sc_status.productid),
    294  1.1       ad 	    ident, sizeof(ident));
    295  1.5       ad 	printf(" <%s>\n", ident);
    296  1.5       ad 
    297  1.5       ad #ifdef I2ODEBUG
    298  1.5       ad 	printf("%s: orgid=0x%04x version=%d\n", sc->sc_dv.dv_xname,
    299  1.5       ad 	    le16toh(sc->sc_status.orgid),
    300  1.5       ad 	    (le32toh(sc->sc_status.segnumber) >> 12) & 15);
    301  1.5       ad 	printf("%s: type want have cbase\n", sc->sc_dv.dv_xname);
    302  1.5       ad 	printf("%s: mem  %04x %04x %08x\n", sc->sc_dv.dv_xname,
    303  1.5       ad 	    le32toh(sc->sc_status.desiredprivmemsize),
    304  1.5       ad 	    le32toh(sc->sc_status.currentprivmemsize),
    305  1.5       ad 	    le32toh(sc->sc_status.currentprivmembase));
    306  1.5       ad 	printf("%s: i/o  %04x %04x %08x\n", sc->sc_dv.dv_xname,
    307  1.5       ad 	    le32toh(sc->sc_status.desiredpriviosize),
    308  1.5       ad 	    le32toh(sc->sc_status.currentpriviosize),
    309  1.5       ad 	    le32toh(sc->sc_status.currentpriviobase));
    310  1.5       ad #endif
    311  1.1       ad 
    312  1.1       ad 	sc->sc_maxreplycnt = le32toh(sc->sc_status.maxoutboundmframes);
    313  1.1       ad 	if (sc->sc_maxreplycnt > IOP_MAX_HW_REPLYCNT)
    314  1.1       ad 		sc->sc_maxreplycnt = IOP_MAX_HW_REPLYCNT;
    315  1.5       ad 	sc->sc_maxqueuecnt = le32toh(sc->sc_status.maxinboundmframes);
    316  1.5       ad 	if (sc->sc_maxqueuecnt > IOP_MAX_HW_QUEUECNT)
    317  1.5       ad 		sc->sc_maxqueuecnt = IOP_MAX_HW_QUEUECNT;
    318  1.1       ad 
    319  1.5       ad 	if (iop_ofifo_init(sc) != 0) {
    320  1.5       ad 		printf("%s: unable to init oubound FIFO\n", sc->sc_dv.dv_xname);
    321  1.5       ad 		return;
    322  1.5       ad 	}
    323  1.1       ad 
    324  1.5       ad 	/*
    325  1.5       ad  	 * Defer further configuration until (a) interrupts are working and
    326  1.5       ad  	 * (b) we have enough information to build the system table.
    327  1.5       ad  	 */
    328  1.1       ad 	config_interrupts((struct device *)sc, iop_config_interrupts);
    329  1.1       ad 
    330  1.5       ad 	/* Configure shutdown hook before we start any device activity. */
    331  1.1       ad 	if (iop_sdh == NULL)
    332  1.1       ad 		iop_sdh = shutdownhook_establish(iop_shutdown, NULL);
    333  1.1       ad 
    334  1.1       ad 	/* Ensure interrupts are enabled at the IOP. */
    335  1.5       ad 	mask = iop_inl(sc, IOP_REG_INTR_MASK);
    336  1.5       ad 	iop_outl(sc, IOP_REG_INTR_MASK, mask & ~IOP_INTR_OFIFO);
    337  1.1       ad 
    338  1.1       ad 	if (intrstr != NULL)
    339  1.1       ad 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    340  1.1       ad 		    intrstr);
    341  1.1       ad 
    342  1.1       ad #ifdef I2ODEBUG
    343  1.1       ad 	printf("%s: queue depths: inbound %d/%d, outbound %d/%d\n",
    344  1.1       ad 	    sc->sc_dv.dv_xname,
    345  1.1       ad 	    sc->sc_maxqueuecnt, le32toh(sc->sc_status.maxinboundmframes),
    346  1.1       ad 	    sc->sc_maxreplycnt, le32toh(sc->sc_status.maxoutboundmframes));
    347  1.1       ad #endif
    348  1.1       ad 
    349  1.5       ad 	lockinit(&sc->sc_conflock, PRIBIO, "iopconf", hz * 30, 0);
    350  1.1       ad 	SIMPLEQ_INIT(&sc->sc_queue);
    351  1.1       ad }
    352  1.1       ad 
    353  1.1       ad /*
    354  1.5       ad  * Perform autoconfiguration tasks.
    355  1.1       ad  */
    356  1.1       ad static void
    357  1.1       ad iop_config_interrupts(struct device *self)
    358  1.1       ad {
    359  1.5       ad 	struct iop_softc *sc, *iop;
    360  1.5       ad 	struct i2o_systab_entry *ste;
    361  1.5       ad 	int rv, i, niop;
    362  1.1       ad 
    363  1.1       ad 	sc = (struct iop_softc *)self;
    364  1.5       ad 	LIST_INIT(&sc->sc_iilist);
    365  1.5       ad 
    366  1.5       ad 	printf("%s: configuring...\n", sc->sc_dv.dv_xname);
    367  1.1       ad 
    368  1.5       ad 	if (iop_hrt_get(sc) != 0) {
    369  1.5       ad 		printf("%s: unable to retrieve HRT\n", sc->sc_dv.dv_xname);
    370  1.5       ad 		return;
    371  1.5       ad 	}
    372  1.1       ad 
    373  1.5       ad 	/*
    374  1.5       ad  	 * Build the system table.
    375  1.5       ad  	 */
    376  1.5       ad 	if (iop_systab == NULL) {
    377  1.5       ad 		for (i = 0, niop = 0; i < iop_cd.cd_ndevs; i++) {
    378  1.5       ad 			if ((iop = device_lookup(&iop_cd, i)) == NULL)
    379  1.5       ad 				continue;
    380  1.5       ad 			if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
    381  1.5       ad 				continue;
    382  1.5       ad 			if (iop_status_get(iop) != 0) {
    383  1.5       ad 				printf("%s: unable to retrieve status\n",
    384  1.5       ad 				    sc->sc_dv.dv_xname);
    385  1.5       ad 				iop->sc_flags &= ~IOP_HAVESTATUS;
    386  1.5       ad 				continue;
    387  1.5       ad 			}
    388  1.5       ad 			niop++;
    389  1.5       ad 		}
    390  1.5       ad 		if (niop == 0)
    391  1.5       ad 			return;
    392  1.5       ad 
    393  1.5       ad 		i = sizeof(struct i2o_systab_entry) * (niop - 1) +
    394  1.5       ad 		    sizeof(struct i2o_systab);
    395  1.5       ad 		iop_systab_size = i;
    396  1.5       ad 		iop_systab = malloc(i, M_DEVBUF, M_NOWAIT);
    397  1.5       ad 
    398  1.5       ad 		memset(iop_systab, 0, i);
    399  1.5       ad 		iop_systab->numentries = niop;
    400  1.5       ad 		iop_systab->version = I2O_VERSION_11;
    401  1.5       ad 
    402  1.5       ad 		for (i = 0, ste = iop_systab->entry; i < iop_cd.cd_ndevs; i++) {
    403  1.5       ad 			if ((iop = device_lookup(&iop_cd, i)) == NULL)
    404  1.5       ad 				continue;
    405  1.5       ad 			if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
    406  1.5       ad 				continue;
    407  1.5       ad 
    408  1.5       ad 			ste->orgid = iop->sc_status.orgid;
    409  1.5       ad 			ste->iopid = iop->sc_dv.dv_unit + 2;
    410  1.5       ad 			ste->segnumber =
    411  1.5       ad 			    htole32(le32toh(iop->sc_status.segnumber) & ~4095);
    412  1.5       ad 			ste->iopcaps = iop->sc_status.iopcaps;
    413  1.5       ad 			ste->inboundmsgframesize =
    414  1.5       ad 			    iop->sc_status.inboundmframesize;
    415  1.5       ad 			ste->inboundmsgportaddresslow =
    416  1.5       ad 			    htole32(iop->sc_memaddr + IOP_REG_IFIFO);
    417  1.5       ad 			ste++;
    418  1.5       ad 		}
    419  1.5       ad 	}
    420  1.5       ad 
    421  1.5       ad 	if (iop_systab_set(sc) != 0) {
    422  1.5       ad 		printf("%s: unable to set system table\n", sc->sc_dv.dv_xname);
    423  1.5       ad 		return;
    424  1.5       ad 	}
    425  1.5       ad 	if (iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_ENABLE, IOP_ICTX, 1,
    426  1.5       ad 	    5000) != 0) {
    427  1.5       ad 		printf("%s: unable to enable system\n", sc->sc_dv.dv_xname);
    428  1.5       ad 		return;
    429  1.5       ad 	}
    430  1.5       ad 
    431  1.5       ad 	/*
    432  1.5       ad 	 * Set up an event handler for this IOP.
    433  1.5       ad 	 */
    434  1.5       ad 	sc->sc_eventii.ii_dv = self;
    435  1.5       ad 	sc->sc_eventii.ii_intr = iop_intr_event;
    436  1.5       ad 	sc->sc_eventii.ii_flags = II_DISCARD | II_UTILITY;
    437  1.5       ad 	sc->sc_eventii.ii_tid = I2O_TID_IOP;
    438  1.5       ad 	if (iop_initiator_register(sc, &sc->sc_eventii) != 0) {
    439  1.5       ad 		printf("%s: unable to register initiator", sc->sc_dv.dv_xname);
    440  1.5       ad 		return;
    441  1.5       ad 	}
    442  1.5       ad 	if (iop_util_eventreg(sc, &sc->sc_eventii, 0xffffffff)) {
    443  1.5       ad 		printf("%s: unable to register for events", sc->sc_dv.dv_xname);
    444  1.5       ad 		return;
    445  1.5       ad 	}
    446  1.5       ad 
    447  1.5       ad #ifdef notyet
    448  1.1       ad 	/* Attempt to match and attach a product-specific extension. */
    449  1.1       ad 	ia.ia_class = I2O_CLASS_ANY;
    450  1.1       ad 	ia.ia_tid = I2O_TID_IOP;
    451  1.1       ad 	config_found_sm(self, &ia, iop_vendor_print, iop_submatch);
    452  1.5       ad #endif
    453  1.5       ad 
    454  1.5       ad 	if ((rv = iop_reconfigure(sc, 0)) != 0) {
    455  1.5       ad 		printf("%s: configure failed (%d)\n", sc->sc_dv.dv_xname, rv);
    456  1.5       ad 		return;
    457  1.5       ad 	}
    458  1.5       ad 
    459  1.5       ad 	sc->sc_flags |= IOP_ONLINE;
    460  1.5       ad 
    461  1.5       ad 	rv = kthread_create1(iop_reconfigure_proc, sc, &sc->sc_reconf_proc,
    462  1.5       ad 	    "%s", sc->sc_dv.dv_xname);
    463  1.5       ad 	if (rv != 0) {
    464  1.5       ad 		printf("%s: unable to create thread (%d)",
    465  1.5       ad 		    sc->sc_dv.dv_xname, rv);
    466  1.5       ad 		return;
    467  1.5       ad 	}
    468  1.5       ad }
    469  1.5       ad 
    470  1.5       ad /*
    471  1.5       ad  * Reconfiguration thread; listens for LCT change notification, and
    472  1.5       ad  * initiates re-configuration if recieved.
    473  1.5       ad  */
    474  1.5       ad static void
    475  1.5       ad iop_reconfigure_proc(void *cookie)
    476  1.5       ad {
    477  1.5       ad 	struct iop_softc *sc;
    478  1.5       ad 	struct i2o_lct lct;
    479  1.5       ad 	u_int32_t chgind;
    480  1.5       ad 
    481  1.5       ad 	sc = cookie;
    482  1.5       ad 
    483  1.5       ad 	for (;;) {
    484  1.5       ad 		chgind = le32toh(sc->sc_chgindicator) + 1;
    485  1.5       ad 
    486  1.5       ad 		if (iop_lct_get0(sc, &lct, sizeof(lct), chgind) == 0) {
    487  1.5       ad 			DPRINTF(("%s: async reconfiguration (0x%08x)\n",
    488  1.5       ad 			    sc->sc_dv.dv_xname, le32toh(lct.changeindicator)));
    489  1.5       ad 			iop_reconfigure(sc, lct.changeindicator);
    490  1.5       ad 		}
    491  1.5       ad 
    492  1.5       ad 		tsleep(iop_reconfigure_proc, PWAIT, "iopzzz", hz * 5);
    493  1.5       ad 	}
    494  1.5       ad }
    495  1.5       ad 
    496  1.5       ad /*
    497  1.5       ad  * Reconfigure: find new and removed devices.
    498  1.5       ad  */
    499  1.5       ad static int
    500  1.5       ad iop_reconfigure(struct iop_softc *sc, u_int32_t chgind)
    501  1.5       ad {
    502  1.5       ad 	struct iop_msg *im;
    503  1.5       ad 	struct i2o_hba_bus_scan *mb;
    504  1.5       ad 	struct i2o_lct_entry *le;
    505  1.5       ad 	struct iop_initiator *ii, *nextii;
    506  1.5       ad 	int rv, tid, i;
    507  1.5       ad 
    508  1.5       ad 	rv = lockmgr(&sc->sc_conflock, LK_EXCLUSIVE | LK_RECURSEFAIL, NULL);
    509  1.5       ad 	if (rv != 0) {
    510  1.5       ad 		DPRINTF(("iop_reconfigure: unable to acquire lock\n"));
    511  1.5       ad 		return (rv);
    512  1.5       ad 	}
    513  1.1       ad 
    514  1.1       ad 	/*
    515  1.5       ad 	 * If the reconfiguration request isn't the result of LCT change
    516  1.5       ad 	 * notification, then be more thorough: ask all bus ports to scan
    517  1.5       ad 	 * their busses.  Wait up to 5 minutes for each bus port to complete
    518  1.5       ad 	 * the request.
    519  1.1       ad 	 */
    520  1.5       ad 	if (chgind == 0) {
    521  1.5       ad 		if ((rv = iop_lct_get(sc)) != 0) {
    522  1.5       ad 			DPRINTF(("iop_reconfigure: unable to read LCT\n"));
    523  1.5       ad 			goto done;
    524  1.5       ad 		}
    525  1.5       ad 
    526  1.5       ad 		le = sc->sc_lct->entry;
    527  1.5       ad 		for (i = 0; i < sc->sc_nlctent; i++, le++) {
    528  1.5       ad 			if ((le16toh(le->classid) & 4095) !=
    529  1.5       ad 			    I2O_CLASS_BUS_ADAPTER_PORT)
    530  1.5       ad 				continue;
    531  1.5       ad 			tid = le32toh(le->localtid) & 4095;
    532  1.5       ad 
    533  1.5       ad 			rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR);
    534  1.5       ad 			if (rv != 0) {
    535  1.5       ad 				DPRINTF(("iop_reconfigure: alloc msg\n"));
    536  1.5       ad 				goto done;
    537  1.5       ad 			}
    538  1.5       ad 
    539  1.5       ad 			mb = (struct i2o_hba_bus_scan *)im->im_msg;
    540  1.5       ad 			mb->msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    541  1.5       ad 			mb->msgfunc = I2O_MSGFUNC(tid, I2O_HBA_BUS_SCAN);
    542  1.5       ad 			mb->msgictx = IOP_ICTX;
    543  1.5       ad 			mb->msgtctx = im->im_tctx;
    544  1.5       ad 
    545  1.5       ad 			DPRINTF(("%s: scanning bus %d\n", sc->sc_dv.dv_xname,
    546  1.5       ad 			    tid));
    547  1.5       ad 
    548  1.5       ad 			rv = iop_msg_enqueue(sc, im, 5*60*1000);
    549  1.5       ad 			iop_msg_free(sc, NULL, im);
    550  1.5       ad 			if (rv != 0) {
    551  1.5       ad 				DPRINTF(("iop_reconfigure: scan failed\n"));
    552  1.5       ad 				goto done;
    553  1.5       ad 			}
    554  1.5       ad 		}
    555  1.5       ad 	} else if (chgind == sc->sc_chgindicator) {
    556  1.5       ad 		DPRINTF(("%s: LCT unchanged (async)\n", sc->sc_dv.dv_xname));
    557  1.5       ad 		goto done;
    558  1.5       ad 	}
    559  1.5       ad 
    560  1.5       ad 	/* Re-read the LCT and determine if it has changed. */
    561  1.5       ad 	if ((rv = iop_lct_get(sc)) != 0) {
    562  1.5       ad 		DPRINTF(("iop_reconfigure: unable to re-read LCT\n"));
    563  1.5       ad 		goto done;
    564  1.5       ad 	}
    565  1.5       ad 	DPRINTF(("%s: %d LCT entries\n", sc->sc_dv.dv_xname, sc->sc_nlctent));
    566  1.5       ad 
    567  1.5       ad 	if (sc->sc_lct->changeindicator == sc->sc_chgindicator) {
    568  1.5       ad 		DPRINTF(("%s: LCT unchanged\n", sc->sc_dv.dv_xname));
    569  1.5       ad 		/* Nothing to do. */
    570  1.5       ad 		rv = 0;
    571  1.5       ad 		goto done;
    572  1.5       ad 	}
    573  1.5       ad 	DPRINTF(("%s: LCT changed\n", sc->sc_dv.dv_xname));
    574  1.5       ad 	sc->sc_chgindicator = sc->sc_lct->changeindicator;
    575  1.5       ad 
    576  1.5       ad 	if (sc->sc_tidmap != NULL)
    577  1.5       ad 		free(sc->sc_tidmap, M_DEVBUF);
    578  1.5       ad 	sc->sc_tidmap = malloc(sc->sc_nlctent * sizeof(struct iop_tidmap),
    579  1.5       ad 	    M_DEVBUF, M_NOWAIT);
    580  1.5       ad 	memset(sc->sc_tidmap, 0, sc->sc_nlctent * sizeof(struct iop_tidmap));
    581  1.5       ad 
    582  1.5       ad 	/* Match and attach child devices. */
    583  1.5       ad 	iop_configure_devices(sc);
    584  1.5       ad 
    585  1.5       ad 	for (ii = LIST_FIRST(&sc->sc_iilist); ii != NULL; ii = nextii) {
    586  1.5       ad 		nextii = LIST_NEXT(ii, ii_list);
    587  1.5       ad 		if ((ii->ii_flags & II_UTILITY) != 0)
    588  1.5       ad 			continue;
    589  1.5       ad 		if ((ii->ii_flags & II_CONFIGURED) == 0) {
    590  1.5       ad 			ii->ii_flags |= II_CONFIGURED;
    591  1.5       ad 			continue;
    592  1.5       ad 		}
    593  1.5       ad 
    594  1.5       ad 		/* Detach devices that were configured, but are now gone. */
    595  1.5       ad 		for (i = 0; i < sc->sc_nlctent; i++)
    596  1.5       ad 			if (ii->ii_tid == sc->sc_tidmap[i].it_tid)
    597  1.5       ad 				break;
    598  1.5       ad 		if (i == sc->sc_nlctent ||
    599  1.5       ad 		    (sc->sc_tidmap[i].it_flags & IT_CONFIGURED) == 0)
    600  1.5       ad 			config_detach(ii->ii_dv, DETACH_FORCE);
    601  1.5       ad 
    602  1.5       ad 		/*
    603  1.5       ad 		 * Tell initiators that existed before the re-configuration
    604  1.5       ad 		 * to re-configure.
    605  1.5       ad 		 */
    606  1.5       ad 		if (ii->ii_reconfig == NULL)
    607  1.5       ad 			continue;
    608  1.5       ad 		if ((rv = (*ii->ii_reconfig)(ii->ii_dv)) != 0)
    609  1.5       ad 			printf("%s: %s failed reconfigure (%d)\n",
    610  1.5       ad 			    sc->sc_dv.dv_xname, ii->ii_dv->dv_xname, rv);
    611  1.5       ad 	}
    612  1.5       ad 	rv = 0;
    613  1.5       ad 
    614  1.5       ad done:
    615  1.5       ad 	lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
    616  1.5       ad 	return (rv);
    617  1.1       ad }
    618  1.1       ad 
    619  1.1       ad /*
    620  1.5       ad  * Configure I2O devices into the system.
    621  1.1       ad  */
    622  1.1       ad static void
    623  1.5       ad iop_configure_devices(struct iop_softc *sc)
    624  1.1       ad {
    625  1.1       ad 	struct iop_attach_args ia;
    626  1.5       ad 	struct iop_initiator *ii;
    627  1.1       ad 	const struct i2o_lct_entry *le;
    628  1.5       ad 	int i, nent;
    629  1.1       ad 
    630  1.1       ad 	nent = sc->sc_nlctent;
    631  1.1       ad 	for (i = 0, le = sc->sc_lct->entry; i < nent; i++, le++) {
    632  1.5       ad 		/*
    633  1.5       ad 		 * Ignore the device if it's in use.
    634  1.5       ad 		 */
    635  1.5       ad 		if ((le32toh(le->usertid) & 4095) != 4095)
    636  1.1       ad 			continue;
    637  1.1       ad 
    638  1.1       ad 		ia.ia_class = le16toh(le->classid) & 4095;
    639  1.5       ad 		ia.ia_tid = le32toh(le->localtid) & 4095;
    640  1.1       ad 
    641  1.1       ad 		/*
    642  1.5       ad 		 * Try to configure the device only if it's not already
    643  1.5       ad 		 * configured.
    644  1.1       ad  		 */
    645  1.7       ad  		LIST_FOREACH(ii, &sc->sc_iilist, ii_list) {
    646  1.7       ad  			if ((ii->ii_flags & II_UTILITY) != 0)
    647  1.7       ad  				continue;
    648  1.5       ad  			if (ia.ia_tid == ii->ii_tid)
    649  1.5       ad 				break;
    650  1.7       ad 		}
    651  1.5       ad 		if (ii != NULL)
    652  1.5       ad 			continue;
    653  1.5       ad 
    654  1.5       ad 		if (config_found_sm(&sc->sc_dv, &ia, iop_print, iop_submatch))
    655  1.5       ad 			sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
    656  1.1       ad 	}
    657  1.1       ad }
    658  1.1       ad 
    659  1.1       ad static void
    660  1.1       ad iop_devinfo(int class, char *devinfo)
    661  1.1       ad {
    662  1.1       ad #ifdef I2OVERBOSE
    663  1.1       ad 	int i;
    664  1.1       ad 
    665  1.1       ad 	for (i = 0; i < sizeof(iop_class) / sizeof(iop_class[0]); i++)
    666  1.1       ad 		if (class == iop_class[i].ic_class)
    667  1.1       ad 			break;
    668  1.1       ad 
    669  1.1       ad 	if (i == sizeof(iop_class) / sizeof(iop_class[0]))
    670  1.1       ad 		sprintf(devinfo, "device (class 0x%x)", class);
    671  1.1       ad 	else
    672  1.1       ad 		strcpy(devinfo, iop_class[i].ic_caption);
    673  1.1       ad #else
    674  1.1       ad 
    675  1.1       ad 	sprintf(devinfo, "device (class 0x%x)", class);
    676  1.1       ad #endif
    677  1.1       ad }
    678  1.1       ad 
    679  1.1       ad static int
    680  1.1       ad iop_print(void *aux, const char *pnp)
    681  1.1       ad {
    682  1.1       ad 	struct iop_attach_args *ia;
    683  1.1       ad 	char devinfo[256];
    684  1.1       ad 
    685  1.1       ad 	ia = aux;
    686  1.1       ad 
    687  1.1       ad 	if (pnp != NULL) {
    688  1.1       ad 		iop_devinfo(ia->ia_class, devinfo);
    689  1.1       ad 		printf("%s at %s", devinfo, pnp);
    690  1.1       ad 	}
    691  1.1       ad 	printf(" tid %d", ia->ia_tid);
    692  1.1       ad 	return (UNCONF);
    693  1.1       ad }
    694  1.1       ad 
    695  1.5       ad #ifdef notyet
    696  1.1       ad static int
    697  1.1       ad iop_vendor_print(void *aux, const char *pnp)
    698  1.1       ad {
    699  1.1       ad 
    700  1.1       ad 	if (pnp != NULL)
    701  1.1       ad 		printf("vendor specific extension at %s", pnp);
    702  1.1       ad 	return (UNCONF);
    703  1.1       ad }
    704  1.5       ad #endif
    705  1.1       ad 
    706  1.1       ad static int
    707  1.1       ad iop_submatch(struct device *parent, struct cfdata *cf, void *aux)
    708  1.1       ad {
    709  1.1       ad 	struct iop_attach_args *ia;
    710  1.1       ad 
    711  1.1       ad 	ia = aux;
    712  1.1       ad 
    713  1.1       ad 	if (cf->iopcf_tid != IOPCF_TID_DEFAULT && cf->iopcf_tid != ia->ia_tid)
    714  1.1       ad 		return (0);
    715  1.1       ad 
    716  1.1       ad 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    717  1.1       ad }
    718  1.1       ad 
    719  1.1       ad /*
    720  1.1       ad  * Shut down all configured IOPs.
    721  1.1       ad  */
    722  1.1       ad static void
    723  1.1       ad iop_shutdown(void *junk)
    724  1.1       ad {
    725  1.1       ad 	struct iop_softc *sc;
    726  1.1       ad 	int i;
    727  1.1       ad 
    728  1.1       ad 	printf("shutting down iop devices... ");
    729  1.1       ad 
    730  1.1       ad 	for (i = 0; i < iop_cd.cd_ndevs; i++) {
    731  1.1       ad 		if ((sc = device_lookup(&iop_cd, i)) == NULL)
    732  1.1       ad 			continue;
    733  1.5       ad 		if ((sc->sc_flags & IOP_ONLINE) == 0)
    734  1.5       ad 			continue;
    735  1.5       ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_QUIESCE, IOP_ICTX,
    736  1.5       ad 		    0, 5000);
    737  1.5       ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_IOP_CLEAR, IOP_ICTX,
    738  1.5       ad 		    0, 5000);
    739  1.1       ad 	}
    740  1.1       ad 
    741  1.1       ad 	/* Wait.  Some boards could still be flushing, stupidly enough. */
    742  1.1       ad 	delay(5000*1000);
    743  1.1       ad 	printf(" done\n");
    744  1.1       ad }
    745  1.1       ad 
    746  1.1       ad /*
    747  1.1       ad  * Retrieve adapter status.
    748  1.1       ad  */
    749  1.1       ad static int
    750  1.1       ad iop_status_get(struct iop_softc *sc)
    751  1.1       ad {
    752  1.1       ad 	struct iop_msg *im;
    753  1.1       ad 	struct i2o_exec_status_get *mb;
    754  1.1       ad 	int rv, s;
    755  1.1       ad 
    756  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    757  1.1       ad 		return (rv);
    758  1.1       ad 
    759  1.1       ad 	mb = (struct i2o_exec_status_get *)im->im_msg;
    760  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_status_get);
    761  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_STATUS_GET);
    762  1.1       ad 	mb->reserved[0] = 0;
    763  1.1       ad 	mb->reserved[1] = 0;
    764  1.1       ad 	mb->reserved[2] = 0;
    765  1.1       ad 	mb->reserved[3] = 0;
    766  1.1       ad 	mb->addrlow = kvtop((caddr_t)&sc->sc_status);	/* XXX */
    767  1.1       ad 	mb->addrhigh = 0;
    768  1.1       ad 	mb->length = sizeof(sc->sc_status);
    769  1.1       ad 
    770  1.1       ad 	s = splbio();
    771  1.5       ad 	memset(&sc->sc_status, 0, sizeof(sc->sc_status));
    772  1.1       ad 
    773  1.1       ad 	if ((rv = iop_msg_send(sc, im, 0)) != 0) {
    774  1.1       ad 		splx(s);
    775  1.1       ad 		iop_msg_free(sc, NULL, im);
    776  1.1       ad 		return (rv);
    777  1.1       ad 	}
    778  1.1       ad 
    779  1.1       ad 	/* XXX */
    780  1.1       ad 	POLL(2500, *((volatile u_char *)&sc->sc_status.syncbyte) == 0xff);
    781  1.1       ad 
    782  1.1       ad 	splx(s);
    783  1.1       ad 	iop_msg_free(sc, NULL, im);
    784  1.1       ad 	return (*((volatile u_char *)&sc->sc_status.syncbyte) != 0xff);
    785  1.1       ad }
    786  1.1       ad 
    787  1.1       ad /*
    788  1.1       ad  * Initalize and populate the adapter's outbound FIFO.
    789  1.1       ad  */
    790  1.1       ad static int
    791  1.1       ad iop_ofifo_init(struct iop_softc *sc)
    792  1.1       ad {
    793  1.1       ad 	struct iop_msg *im;
    794  1.1       ad 	volatile u_int32_t status;
    795  1.1       ad 	bus_addr_t addr;
    796  1.5       ad 	bus_dma_segment_t seg;
    797  1.1       ad 	struct i2o_exec_outbound_init *mb;
    798  1.5       ad 	int i, rseg, rv;
    799  1.1       ad 
    800  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    801  1.1       ad 		return (rv);
    802  1.1       ad 
    803  1.1       ad 	mb = (struct i2o_exec_outbound_init *)im->im_msg;
    804  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_outbound_init);
    805  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_OUTBOUND_INIT);
    806  1.1       ad 	mb->msgictx = IOP_ICTX;
    807  1.1       ad 	mb->msgtctx = im->im_tctx;
    808  1.4  thorpej 	mb->pagesize = PAGE_SIZE;
    809  1.5       ad 	mb->flags = 0x80 | ((IOP_MAX_REPLY_SIZE >> 2) << 16);	/* XXX */
    810  1.1       ad 
    811  1.1       ad 	status = 0;
    812  1.5       ad 
    813  1.5       ad 	/*
    814  1.5       ad 	 * The I2O spec says that there are two SGLs: one for the status
    815  1.5       ad 	 * word, and one for a list of discarded MFAs.  It continues to say
    816  1.5       ad 	 * that if you don't want to get the list of MFAs, an IGNORE SGL is
    817  1.5       ad 	 * necessary; this isn't the case (and in fact appears to be a bad
    818  1.5       ad 	 * thing).
    819  1.5       ad 	 */
    820  1.1       ad 	iop_msg_map(sc, im, (void *)&status, sizeof(status), 0);
    821  1.1       ad 	if ((rv = iop_msg_send(sc, im, 0)) != 0) {
    822  1.1       ad 		iop_msg_free(sc, NULL, im);
    823  1.1       ad 		return (rv);
    824  1.1       ad 	}
    825  1.1       ad 	iop_msg_unmap(sc, im);
    826  1.1       ad 	iop_msg_free(sc, NULL, im);
    827  1.1       ad 
    828  1.5       ad 	/* XXX */
    829  1.5       ad 	POLL(5000, status == I2O_EXEC_OUTBOUND_INIT_COMPLETE);
    830  1.5       ad 	if (status != I2O_EXEC_OUTBOUND_INIT_COMPLETE) {
    831  1.5       ad 		printf("%s: outbound FIFO init failed\n", sc->sc_dv.dv_xname);
    832  1.5       ad 		return (EIO);
    833  1.1       ad 	}
    834  1.1       ad 
    835  1.1       ad 	/* If we need to allocate DMA safe memory, do it now. */
    836  1.1       ad 	if (sc->sc_rep_phys == 0) {
    837  1.5       ad 		sc->sc_rep_size = sc->sc_maxreplycnt * IOP_MAX_REPLY_SIZE;
    838  1.5       ad 
    839  1.5       ad 		rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_rep_size, PAGE_SIZE,
    840  1.5       ad 		    0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
    841  1.5       ad 		if (rv != 0) {
    842  1.5       ad 			printf("%s: dma alloc = %d\n", sc->sc_dv.dv_xname,
    843  1.5       ad 			   rv);
    844  1.5       ad 			return (rv);
    845  1.5       ad 		}
    846  1.5       ad 
    847  1.5       ad 		rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sc->sc_rep_size,
    848  1.5       ad 		    &sc->sc_rep, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    849  1.5       ad 		if (rv != 0) {
    850  1.5       ad 			printf("%s: dma map = %d\n", sc->sc_dv.dv_xname, rv);
    851  1.5       ad 			return (rv);
    852  1.5       ad 		}
    853  1.5       ad 
    854  1.5       ad 		rv = bus_dmamap_create(sc->sc_dmat, sc->sc_rep_size, 1,
    855  1.5       ad 		    sc->sc_rep_size, 0, BUS_DMA_NOWAIT, &sc->sc_rep_dmamap);
    856  1.5       ad 		if (rv != 0) {
    857  1.5       ad 			printf("%s: dma create = %d\n", sc->sc_dv.dv_xname, rv);
    858  1.5       ad 			return (rv);
    859  1.5       ad 		}
    860  1.5       ad 
    861  1.5       ad 		rv = bus_dmamap_load(sc->sc_dmat, sc->sc_rep_dmamap, sc->sc_rep,
    862  1.5       ad 		    sc->sc_rep_size, NULL, BUS_DMA_NOWAIT);
    863  1.5       ad 		if (rv != 0) {
    864  1.5       ad 			printf("%s: dma load = %d\n", sc->sc_dv.dv_xname, rv);
    865  1.5       ad 			return (rv);
    866  1.5       ad 		}
    867  1.5       ad 
    868  1.5       ad 		sc->sc_rep_phys = sc->sc_rep_dmamap->dm_segs[0].ds_addr;
    869  1.1       ad 	}
    870  1.1       ad 
    871  1.1       ad 	/* Populate the outbound FIFO. */
    872  1.5       ad 	for (i = sc->sc_maxreplycnt, addr = sc->sc_rep_phys; i != 0; i--) {
    873  1.5       ad 		iop_outl(sc, IOP_REG_OFIFO, (u_int32_t)addr);
    874  1.5       ad 		addr += IOP_MAX_REPLY_SIZE;
    875  1.1       ad 	}
    876  1.1       ad 
    877  1.1       ad 	return (0);
    878  1.1       ad }
    879  1.1       ad 
    880  1.1       ad /*
    881  1.1       ad  * Read the specified number of bytes from the IOP's hardware resource table.
    882  1.1       ad  */
    883  1.1       ad static int
    884  1.1       ad iop_hrt_get0(struct iop_softc *sc, struct i2o_hrt *hrt, int size)
    885  1.1       ad {
    886  1.1       ad 	struct iop_msg *im;
    887  1.1       ad 	int rv;
    888  1.1       ad 	struct i2o_exec_hrt_get *mb;
    889  1.1       ad 
    890  1.5       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
    891  1.1       ad 		return (rv);
    892  1.1       ad 
    893  1.1       ad 	mb = (struct i2o_exec_hrt_get *)im->im_msg;
    894  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_hrt_get);
    895  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_HRT_GET);
    896  1.1       ad 	mb->msgictx = IOP_ICTX;
    897  1.1       ad 	mb->msgtctx = im->im_tctx;
    898  1.1       ad 
    899  1.1       ad 	iop_msg_map(sc, im, hrt, size, 0);
    900  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
    901  1.1       ad 	iop_msg_unmap(sc, im);
    902  1.1       ad 	iop_msg_free(sc, NULL, im);
    903  1.1       ad 	return (rv);
    904  1.1       ad }
    905  1.1       ad 
    906  1.1       ad /*
    907  1.5       ad  * Read the IOP's hardware resource table.
    908  1.1       ad  */
    909  1.1       ad static int
    910  1.1       ad iop_hrt_get(struct iop_softc *sc)
    911  1.1       ad {
    912  1.1       ad 	struct i2o_hrt hrthdr, *hrt;
    913  1.1       ad 	int size, rv;
    914  1.1       ad 
    915  1.1       ad 	if ((rv = iop_hrt_get0(sc, &hrthdr, sizeof(hrthdr))) != 0)
    916  1.1       ad 		return (rv);
    917  1.1       ad 
    918  1.5       ad 	DPRINTF(("%s: %d hrt entries\n", sc->sc_dv.dv_xname,
    919  1.5       ad 	    le16toh(hrthdr.numentries)));
    920  1.5       ad 
    921  1.5       ad 	size = sizeof(struct i2o_hrt) +
    922  1.5       ad 	    (htole32(hrthdr.numentries) - 1) * sizeof(struct i2o_hrt_entry);
    923  1.1       ad 	hrt = (struct i2o_hrt *)malloc(size, M_DEVBUF, M_NOWAIT);
    924  1.1       ad 
    925  1.1       ad 	if ((rv = iop_hrt_get0(sc, hrt, size)) != 0) {
    926  1.1       ad 		free(hrt, M_DEVBUF);
    927  1.1       ad 		return (rv);
    928  1.1       ad 	}
    929  1.1       ad 
    930  1.1       ad 	if (sc->sc_hrt != NULL)
    931  1.1       ad 		free(sc->sc_hrt, M_DEVBUF);
    932  1.1       ad 	sc->sc_hrt = hrt;
    933  1.1       ad 	return (0);
    934  1.1       ad }
    935  1.1       ad 
    936  1.1       ad /*
    937  1.1       ad  * Request the specified number of bytes from the IOP's logical
    938  1.5       ad  * configuration table.  If a change indicator is specified, this
    939  1.5       ad  * is an verbatim notification request, so the caller is prepared
    940  1.5       ad  * to wait indefinitely.
    941  1.1       ad  */
    942  1.1       ad static int
    943  1.5       ad iop_lct_get0(struct iop_softc *sc, struct i2o_lct *lct, int size,
    944  1.5       ad 	     u_int32_t chgind)
    945  1.1       ad {
    946  1.1       ad 	struct iop_msg *im;
    947  1.1       ad 	struct i2o_exec_lct_notify *mb;
    948  1.1       ad 	int rv;
    949  1.1       ad 
    950  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
    951  1.1       ad 		return (rv);
    952  1.5       ad 
    953  1.1       ad 	memset(lct, 0, size);
    954  1.5       ad 	memset(im->im_msg, 0, sizeof(im->im_msg));
    955  1.1       ad 
    956  1.1       ad 	mb = (struct i2o_exec_lct_notify *)im->im_msg;
    957  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_lct_notify);
    958  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_LCT_NOTIFY);
    959  1.1       ad 	mb->msgictx = IOP_ICTX;
    960  1.1       ad 	mb->msgtctx = im->im_tctx;
    961  1.1       ad 	mb->classid = I2O_CLASS_ANY;
    962  1.5       ad 	mb->changeindicator = chgind;
    963  1.5       ad 
    964  1.5       ad 	DPRINTF(("iop_lct_get0: reading LCT\n"));
    965  1.1       ad 
    966  1.1       ad 	iop_msg_map(sc, im, lct, size, 0);
    967  1.5       ad 	rv = iop_msg_enqueue(sc, im, (chgind == 0 ? 120*1000 : 0));
    968  1.1       ad 	iop_msg_unmap(sc, im);
    969  1.1       ad 	iop_msg_free(sc, NULL, im);
    970  1.1       ad 	return (rv);
    971  1.1       ad }
    972  1.1       ad 
    973  1.1       ad /*
    974  1.6       ad  * Read the IOP's logical configuration table.
    975  1.1       ad  */
    976  1.1       ad int
    977  1.1       ad iop_lct_get(struct iop_softc *sc)
    978  1.1       ad {
    979  1.5       ad 	int esize, size, rv;
    980  1.5       ad 	struct i2o_lct *lct;
    981  1.1       ad 
    982  1.5       ad 	esize = le32toh(sc->sc_status.expectedlctsize);
    983  1.5       ad 	lct = (struct i2o_lct *)malloc(esize, M_DEVBUF, M_WAITOK);
    984  1.5       ad 	if (lct == NULL)
    985  1.1       ad 		return (ENOMEM);
    986  1.1       ad 
    987  1.5       ad 	if ((rv = iop_lct_get0(sc, lct, esize, 0)) != 0) {
    988  1.1       ad 		free(lct, M_DEVBUF);
    989  1.1       ad 		return (rv);
    990  1.1       ad 	}
    991  1.1       ad 
    992  1.5       ad 	size = le16toh(lct->tablesize) << 2;
    993  1.5       ad 	if (esize != size) {
    994  1.1       ad 		free(lct, M_DEVBUF);
    995  1.5       ad 		lct = (struct i2o_lct *)malloc(size, M_DEVBUF, M_WAITOK);
    996  1.5       ad 		if (lct == NULL)
    997  1.5       ad 			return (ENOMEM);
    998  1.5       ad 
    999  1.5       ad 		if ((rv = iop_lct_get0(sc, lct, size, 0)) != 0) {
   1000  1.5       ad 			free(lct, M_DEVBUF);
   1001  1.5       ad 			return (rv);
   1002  1.5       ad 		}
   1003  1.1       ad 	}
   1004  1.5       ad 
   1005  1.5       ad 	/* Swap in the new LCT. */
   1006  1.1       ad 	if (sc->sc_lct != NULL)
   1007  1.1       ad 		free(sc->sc_lct, M_DEVBUF);
   1008  1.1       ad 	sc->sc_lct = lct;
   1009  1.1       ad 	sc->sc_nlctent = ((le16toh(sc->sc_lct->tablesize) << 2) -
   1010  1.1       ad 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
   1011  1.1       ad 	    sizeof(struct i2o_lct_entry);
   1012  1.1       ad 	return (0);
   1013  1.1       ad }
   1014  1.1       ad 
   1015  1.1       ad /*
   1016  1.5       ad  * Request the specified parameter group from the target.
   1017  1.1       ad  */
   1018  1.1       ad int
   1019  1.5       ad iop_param_op(struct iop_softc *sc, int tid, int write, int group, void *buf,
   1020  1.5       ad 	     int size)
   1021  1.1       ad {
   1022  1.1       ad 	struct iop_msg *im;
   1023  1.5       ad 	struct i2o_util_params_op *mb;
   1024  1.5       ad 	int rv, func, op;
   1025  1.1       ad 	struct {
   1026  1.1       ad 		struct	i2o_param_op_list_header olh;
   1027  1.1       ad 		struct	i2o_param_op_all_template oat;
   1028  1.1       ad 	} req;
   1029  1.1       ad 
   1030  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
   1031  1.1       ad 		return (rv);
   1032  1.1       ad 
   1033  1.5       ad 	if (write) {
   1034  1.5       ad 		func = I2O_UTIL_PARAMS_SET;
   1035  1.5       ad 		op = I2O_PARAMS_OP_FIELD_SET;
   1036  1.5       ad 	} else {
   1037  1.5       ad 		func = I2O_UTIL_PARAMS_GET;
   1038  1.5       ad 		op = I2O_PARAMS_OP_FIELD_GET;
   1039  1.5       ad 	}
   1040  1.5       ad 
   1041  1.5       ad 	mb = (struct i2o_util_params_op *)im->im_msg;
   1042  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1043  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(tid, func);
   1044  1.1       ad 	mb->msgictx = IOP_ICTX;
   1045  1.1       ad 	mb->msgtctx = im->im_tctx;
   1046  1.1       ad 	mb->flags = 0;
   1047  1.1       ad 
   1048  1.1       ad 	req.olh.count = htole16(1);
   1049  1.1       ad 	req.olh.reserved = htole16(0);
   1050  1.5       ad 	req.oat.operation = htole16(op);
   1051  1.1       ad 	req.oat.fieldcount = htole16(0xffff);
   1052  1.1       ad 	req.oat.group = htole16(group);
   1053  1.1       ad 
   1054  1.5       ad 	memset(buf, 0, size);
   1055  1.1       ad 	iop_msg_map(sc, im, &req, sizeof(req), 1);
   1056  1.5       ad 	iop_msg_map(sc, im, buf, size, write);
   1057  1.1       ad 
   1058  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1059  1.1       ad 	iop_msg_unmap(sc, im);
   1060  1.1       ad 	iop_msg_free(sc, NULL, im);
   1061  1.1       ad 	return (rv);
   1062  1.1       ad }
   1063  1.1       ad 
   1064  1.1       ad /*
   1065  1.5       ad  * Execute a simple command (no parameters).
   1066  1.1       ad  */
   1067  1.1       ad int
   1068  1.5       ad iop_simple_cmd(struct iop_softc *sc, int tid, int function, int ictx,
   1069  1.5       ad 	       int async, int timo)
   1070  1.1       ad {
   1071  1.1       ad 	struct iop_msg *im;
   1072  1.1       ad 	struct i2o_msg *mb;
   1073  1.5       ad 	int rv, fl;
   1074  1.1       ad 
   1075  1.5       ad 	fl = (async != 0 ? IM_NOWAIT : 0);
   1076  1.5       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, fl | IM_NOINTR)) != 0)
   1077  1.1       ad 		return (rv);
   1078  1.1       ad 
   1079  1.1       ad 	mb = (struct i2o_msg *)im->im_msg;
   1080  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_msg);
   1081  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(tid, function);
   1082  1.1       ad 	mb->msgictx = ictx;
   1083  1.1       ad 	mb->msgtctx = im->im_tctx;
   1084  1.1       ad 
   1085  1.5       ad 	if (async)
   1086  1.5       ad 		rv = iop_msg_enqueue(sc, im, timo);
   1087  1.5       ad 	else
   1088  1.5       ad 		rv = iop_msg_send(sc, im, timo);
   1089  1.1       ad 	iop_msg_free(sc, NULL, im);
   1090  1.1       ad 	return (rv);
   1091  1.1       ad }
   1092  1.1       ad 
   1093  1.1       ad /*
   1094  1.5       ad  * Post the system table to the IOP.
   1095  1.1       ad  */
   1096  1.1       ad static int
   1097  1.1       ad iop_systab_set(struct iop_softc *sc)
   1098  1.1       ad {
   1099  1.5       ad 	struct i2o_exec_sys_tab_set *mb;
   1100  1.1       ad 	struct iop_msg *im;
   1101  1.1       ad 	u_int32_t mema[2], ioa[2];
   1102  1.1       ad 	int rv;
   1103  1.1       ad 
   1104  1.5       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
   1105  1.1       ad 		return (rv);
   1106  1.1       ad 
   1107  1.1       ad 	mb = (struct i2o_exec_sys_tab_set *)im->im_msg;
   1108  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_sys_tab_set);
   1109  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_TAB_SET);
   1110  1.1       ad 	mb->msgictx = IOP_ICTX;
   1111  1.1       ad 	mb->msgtctx = im->im_tctx;
   1112  1.5       ad 	mb->iopid = (sc->sc_dv.dv_unit + 2) << 12;
   1113  1.5       ad 	mb->segnumber = 0;
   1114  1.5       ad 
   1115  1.5       ad 	/* XXX This is questionable, but better than nothing... */
   1116  1.5       ad 	mema[0] = le32toh(sc->sc_status.currentprivmembase);
   1117  1.5       ad 	mema[1] = le32toh(sc->sc_status.currentprivmemsize);
   1118  1.5       ad 	ioa[0] = le32toh(sc->sc_status.currentpriviobase);
   1119  1.5       ad 	ioa[1] = le32toh(sc->sc_status.currentpriviosize);
   1120  1.1       ad 
   1121  1.5       ad 	iop_msg_map(sc, im, iop_systab, iop_systab_size, 1);
   1122  1.1       ad 	iop_msg_map(sc, im, mema, sizeof(mema), 1);
   1123  1.1       ad 	iop_msg_map(sc, im, ioa, sizeof(ioa), 1);
   1124  1.1       ad 
   1125  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1126  1.1       ad 	iop_msg_unmap(sc, im);
   1127  1.1       ad 	iop_msg_free(sc, NULL, im);
   1128  1.1       ad 	return (rv);
   1129  1.1       ad }
   1130  1.1       ad 
   1131  1.1       ad /*
   1132  1.1       ad  * Reset the adapter.  Must be called with interrupts disabled.
   1133  1.1       ad  */
   1134  1.1       ad static int
   1135  1.1       ad iop_reset(struct iop_softc *sc)
   1136  1.1       ad {
   1137  1.1       ad 	struct iop_msg *im;
   1138  1.1       ad 	volatile u_int32_t sw;
   1139  1.1       ad 	u_int32_t mfa;
   1140  1.1       ad 	struct i2o_exec_iop_reset *mb;
   1141  1.1       ad 	int rv;
   1142  1.1       ad 
   1143  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
   1144  1.1       ad 		return (rv);
   1145  1.1       ad 
   1146  1.1       ad 	sw = 0;
   1147  1.1       ad 
   1148  1.1       ad 	mb = (struct i2o_exec_iop_reset *)im->im_msg;
   1149  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_iop_reset);
   1150  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_IOP_RESET);
   1151  1.1       ad 	mb->reserved[0] = 0;
   1152  1.1       ad 	mb->reserved[1] = 0;
   1153  1.1       ad 	mb->reserved[2] = 0;
   1154  1.1       ad 	mb->reserved[3] = 0;
   1155  1.1       ad 	mb->statuslow = kvtop((caddr_t)&sw);		/* XXX */
   1156  1.1       ad 	mb->statushigh = 0;
   1157  1.1       ad 
   1158  1.1       ad 	if ((rv = iop_msg_send(sc, im, 0)))
   1159  1.1       ad 		return (rv);
   1160  1.1       ad 	iop_msg_free(sc, NULL, im);
   1161  1.1       ad 
   1162  1.1       ad 	POLL(2500, sw != 0);					/* XXX */
   1163  1.1       ad 	if (sw != I2O_RESET_IN_PROGRESS) {
   1164  1.1       ad 		printf("%s: reset rejected\n", sc->sc_dv.dv_xname);
   1165  1.1       ad 		return (EIO);
   1166  1.1       ad 	}
   1167  1.1       ad 
   1168  1.1       ad 	/*
   1169  1.5       ad 	 * IOP is now in the INIT state.  Wait no more than 10 seconds for
   1170  1.1       ad 	 * the inbound queue to become responsive.
   1171  1.1       ad 	 */
   1172  1.5       ad 	POLL(10000, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1173  1.1       ad 	if (mfa == IOP_MFA_EMPTY) {
   1174  1.1       ad 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
   1175  1.1       ad 		return (EIO);
   1176  1.1       ad 	}
   1177  1.1       ad 
   1178  1.5       ad 	if (sw == I2O_RESET_REJECTED)
   1179  1.5       ad 		printf("%s: reset rejected?\n", sc->sc_dv.dv_xname);
   1180  1.5       ad 
   1181  1.1       ad 	iop_release_mfa(sc, mfa);
   1182  1.1       ad 	return (0);
   1183  1.1       ad }
   1184  1.1       ad 
   1185  1.1       ad /*
   1186  1.1       ad  * Register a new initiator.
   1187  1.1       ad  */
   1188  1.1       ad int
   1189  1.1       ad iop_initiator_register(struct iop_softc *sc, struct iop_initiator *ii)
   1190  1.1       ad {
   1191  1.5       ad 	static int ictx;
   1192  1.5       ad 	static int stctx;
   1193  1.5       ad 
   1194  1.5       ad 	/* 0 is reserved for system messages. */
   1195  1.5       ad 	ii->ii_ictx = ++ictx;
   1196  1.5       ad 	ii->ii_stctx = ++stctx | 0x80000000;
   1197  1.1       ad 
   1198  1.5       ad 	LIST_INSERT_HEAD(&sc->sc_iilist, ii, ii_list);
   1199  1.5       ad 	LIST_INSERT_HEAD(IOP_ICTXHASH(ii->ii_ictx), ii, ii_hash);
   1200  1.1       ad 
   1201  1.1       ad 	return (0);
   1202  1.1       ad }
   1203  1.1       ad 
   1204  1.1       ad /*
   1205  1.1       ad  * Unregister an initiator.
   1206  1.1       ad  */
   1207  1.1       ad void
   1208  1.1       ad iop_initiator_unregister(struct iop_softc *sc, struct iop_initiator *ii)
   1209  1.1       ad {
   1210  1.1       ad 
   1211  1.5       ad 	LIST_REMOVE(ii, ii_list);
   1212  1.5       ad 	LIST_REMOVE(ii, ii_hash);
   1213  1.1       ad }
   1214  1.1       ad 
   1215  1.1       ad /*
   1216  1.5       ad  * Handle a reply frame from the adapter.
   1217  1.1       ad  */
   1218  1.1       ad static int
   1219  1.5       ad iop_handle_reply(struct iop_softc *sc, u_int32_t rmfa)
   1220  1.1       ad {
   1221  1.1       ad 	struct iop_msg *im;
   1222  1.1       ad 	struct i2o_reply *rb;
   1223  1.1       ad 	struct iop_initiator *ii;
   1224  1.5       ad 	u_int off, ictx, tctx, status, size;
   1225  1.1       ad 
   1226  1.1       ad 	off = (int)(rmfa - sc->sc_rep_phys);
   1227  1.1       ad 	rb = (struct i2o_reply *)(sc->sc_rep + off);
   1228  1.1       ad 
   1229  1.5       ad 	/* Perform reply queue DMA synchronisation... */
   1230  1.1       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, off, IOP_MAX_MSG_SIZE,
   1231  1.1       ad 	    BUS_DMASYNC_POSTREAD);
   1232  1.1       ad 	if (--sc->sc_stat.is_cur_hwqueue != 0)
   1233  1.1       ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap,
   1234  1.1       ad 		    0, sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1235  1.1       ad 
   1236  1.1       ad #ifdef I2ODEBUG
   1237  1.1       ad 	if ((le32toh(rb->msgflags) & I2O_MSGFLAGS_64BIT) != 0)
   1238  1.5       ad 		panic("iop_handle_reply: 64-bit reply");
   1239  1.1       ad #endif
   1240  1.1       ad 	/*
   1241  1.1       ad 	 * Find the initiator.
   1242  1.1       ad 	 */
   1243  1.1       ad 	ictx = le32toh(rb->msgictx);
   1244  1.1       ad 	if (ictx == IOP_ICTX)
   1245  1.1       ad 		ii = NULL;
   1246  1.1       ad 	else {
   1247  1.5       ad 		ii = LIST_FIRST(IOP_ICTXHASH(ictx));
   1248  1.5       ad 		for (; ii != NULL; ii = LIST_NEXT(ii, ii_hash))
   1249  1.5       ad 			if (ii->ii_ictx == ictx)
   1250  1.5       ad 				break;
   1251  1.5       ad 		if (ii == NULL) {
   1252  1.1       ad #ifdef I2ODEBUG
   1253  1.5       ad 			iop_reply_print(sc, NULL, rb);
   1254  1.1       ad #endif
   1255  1.5       ad 			printf("%s: WARNING: bad ictx returned (%x)",
   1256  1.5       ad 			    sc->sc_dv.dv_xname, ictx);
   1257  1.5       ad 
   1258  1.5       ad 			/* Return the reply frame to the IOP's outbound FIFO. */
   1259  1.5       ad 			iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1260  1.5       ad 			return (-1);
   1261  1.5       ad 		}
   1262  1.1       ad 	}
   1263  1.1       ad 
   1264  1.1       ad 	status = rb->reqstatus;
   1265  1.1       ad 
   1266  1.1       ad 	if (ii == NULL || (ii->ii_flags & II_DISCARD) == 0) {
   1267  1.1       ad 		/*
   1268  1.1       ad 		 * This initiator tracks state using message wrappers.
   1269  1.1       ad 		 *
   1270  1.1       ad 		 * Find the originating message wrapper, and if requested
   1271  1.1       ad 		 * notify the initiator.
   1272  1.1       ad 		 */
   1273  1.1       ad 		tctx = le32toh(rb->msgtctx);
   1274  1.5       ad 		im = TAILQ_FIRST(IOP_TCTXHASH(tctx));
   1275  1.1       ad 		for (; im != NULL; im = TAILQ_NEXT(im, im_hash))
   1276  1.1       ad 			if (im->im_tctx == tctx)
   1277  1.1       ad 				break;
   1278  1.5       ad 		if (im == NULL || (im->im_flags & IM_ALLOCED) == 0) {
   1279  1.5       ad #ifdef I2ODEBUG
   1280  1.5       ad 			iop_reply_print(sc, NULL, rb);
   1281  1.5       ad #endif
   1282  1.5       ad 			printf("%s: WARNING: bad tctx returned (%x, %p)",
   1283  1.1       ad 			    sc->sc_dv.dv_xname, tctx, im);
   1284  1.5       ad 
   1285  1.5       ad 			/* Return the reply frame to the IOP's outbound FIFO. */
   1286  1.5       ad 			iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1287  1.5       ad 			return (-1);
   1288  1.5       ad 		}
   1289  1.1       ad #ifdef I2ODEBUG
   1290  1.1       ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1291  1.5       ad 			panic("%s: dup reply", sc->sc_dv.dv_xname);
   1292  1.1       ad #endif
   1293  1.1       ad 
   1294  1.1       ad 		im->im_flags |= IM_REPLIED;
   1295  1.1       ad 
   1296  1.1       ad #ifdef I2ODEBUG
   1297  1.1       ad 		if (rb->reqstatus != 0)
   1298  1.1       ad 			iop_reply_print(sc, im, rb);
   1299  1.1       ad #endif
   1300  1.1       ad 		/* Notify the initiator. */
   1301  1.5       ad 		if ((im->im_flags & IM_WAITING) != 0) {
   1302  1.5       ad 			size = (le32toh(rb->msgflags) >> 14) & ~3;
   1303  1.5       ad 			if (size > IOP_MAX_REPLY_SIZE)
   1304  1.5       ad 				size = IOP_MAX_REPLY_SIZE;
   1305  1.5       ad 			memcpy(im->im_msg, rb, size);
   1306  1.1       ad 			wakeup(im);
   1307  1.5       ad 		} else if ((im->im_flags & IM_NOINTR) == 0)
   1308  1.1       ad 			(*ii->ii_intr)(ii->ii_dv, im, rb);
   1309  1.1       ad 	} else {
   1310  1.1       ad 		/*
   1311  1.1       ad 		 * This initiator discards message wrappers.
   1312  1.1       ad 		 *
   1313  1.1       ad 		 * Simply pass the reply frame to the initiator.
   1314  1.1       ad 		 */
   1315  1.1       ad 		(*ii->ii_intr)(ii->ii_dv, NULL, rb);
   1316  1.1       ad 	}
   1317  1.1       ad 
   1318  1.1       ad 	/* Return the reply frame to the IOP's outbound FIFO. */
   1319  1.5       ad 	iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1320  1.1       ad 
   1321  1.1       ad 	/* Run the queue. */
   1322  1.1       ad 	if ((im = SIMPLEQ_FIRST(&sc->sc_queue)) != NULL)
   1323  1.5       ad 		iop_msg_enqueue(sc, im, 0);
   1324  1.1       ad 
   1325  1.1       ad 	return (status);
   1326  1.1       ad }
   1327  1.1       ad 
   1328  1.1       ad /*
   1329  1.1       ad  * Handle an interrupt from the adapter.
   1330  1.1       ad  */
   1331  1.1       ad int
   1332  1.1       ad iop_intr(void *arg)
   1333  1.1       ad {
   1334  1.1       ad 	struct iop_softc *sc;
   1335  1.5       ad 	u_int32_t rmfa;
   1336  1.1       ad 
   1337  1.1       ad 	sc = arg;
   1338  1.1       ad 
   1339  1.5       ad 	if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) == 0)
   1340  1.5       ad 		return (0);
   1341  1.5       ad 
   1342  1.5       ad 	for (;;) {
   1343  1.5       ad 		/* Double read to account for IOP bug. */
   1344  1.5       ad 		if ((rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY &&
   1345  1.5       ad 		    (rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY)
   1346  1.5       ad 			break;
   1347  1.5       ad 		iop_handle_reply(sc, rmfa);
   1348  1.1       ad 	}
   1349  1.1       ad 
   1350  1.5       ad 	return (1);
   1351  1.5       ad }
   1352  1.5       ad 
   1353  1.5       ad /*
   1354  1.5       ad  * Handle an event signalled by the executive.
   1355  1.5       ad  */
   1356  1.5       ad static void
   1357  1.5       ad iop_intr_event(struct device *dv, struct iop_msg *im, void *reply)
   1358  1.5       ad {
   1359  1.5       ad 	struct i2o_util_event_register_reply *rb;
   1360  1.5       ad 	struct iop_softc *sc;
   1361  1.5       ad 	u_int event;
   1362  1.5       ad 
   1363  1.5       ad 	sc = (struct iop_softc *)dv;
   1364  1.5       ad 	rb = reply;
   1365  1.5       ad 	event = le32toh(rb->event);
   1366  1.5       ad 
   1367  1.5       ad #ifndef I2ODEBUG
   1368  1.5       ad 	if (event == I2O_EVENT_GEN_EVENT_MASK_MODIFIED)
   1369  1.5       ad 		return;
   1370  1.1       ad #endif
   1371  1.5       ad 
   1372  1.5       ad 	printf("%s: event 0x%08x received\n", dv->dv_xname, event);
   1373  1.1       ad }
   1374  1.1       ad 
   1375  1.1       ad /*
   1376  1.1       ad  * Allocate a message wrapper.
   1377  1.1       ad  */
   1378  1.1       ad int
   1379  1.1       ad iop_msg_alloc(struct iop_softc *sc, struct iop_initiator *ii,
   1380  1.1       ad 	      struct iop_msg **imp, int flags)
   1381  1.1       ad {
   1382  1.1       ad 	struct iop_msg *im;
   1383  1.5       ad 	static int tctxgen = 666;
   1384  1.5       ad 	int s, rv, i, tctx;
   1385  1.1       ad 
   1386  1.1       ad #ifdef I2ODEBUG
   1387  1.1       ad 	if ((flags & IM_SYSMASK) != 0)
   1388  1.1       ad 		panic("iop_msg_alloc: system flags specified");
   1389  1.1       ad #endif
   1390  1.1       ad 
   1391  1.1       ad 	s = splbio();	/* XXX */
   1392  1.1       ad 
   1393  1.5       ad 	if (ii != NULL && (ii->ii_flags & II_DISCARD) != 0) {
   1394  1.5       ad 		flags |= IM_DISCARD;
   1395  1.5       ad 		tctx = ii->ii_stctx;
   1396  1.5       ad 	} else
   1397  1.5       ad 		tctx = tctxgen++ & 0x7fffffff;
   1398  1.1       ad 
   1399  1.1       ad 	im = (struct iop_msg *)pool_get(iop_msgpool,
   1400  1.1       ad 	    (flags & IM_NOWAIT) == 0 ? PR_WAITOK : 0);
   1401  1.1       ad 	if (im == NULL) {
   1402  1.1       ad 		splx(s);
   1403  1.1       ad 		return (ENOMEM);
   1404  1.1       ad 	}
   1405  1.1       ad 
   1406  1.1       ad 	/* XXX */
   1407  1.1       ad 	rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER, IOP_MAX_SGL_ENTRIES,
   1408  1.1       ad 	    IOP_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1409  1.1       ad 	    &im->im_xfer[0].ix_map);
   1410  1.1       ad 	if (rv != 0) {
   1411  1.1       ad 		pool_put(iop_msgpool, im);
   1412  1.1       ad 		splx(s);
   1413  1.1       ad 		return (rv);
   1414  1.1       ad 	}
   1415  1.1       ad 
   1416  1.1       ad 	if ((flags & (IM_DISCARD | IM_NOICTX)) == 0)
   1417  1.5       ad 		TAILQ_INSERT_TAIL(IOP_TCTXHASH(tctx), im, im_hash);
   1418  1.1       ad 
   1419  1.1       ad 	splx(s);
   1420  1.1       ad 
   1421  1.5       ad 	im->im_tctx = tctx;
   1422  1.1       ad 	im->im_flags = flags | IM_ALLOCED;
   1423  1.1       ad 	for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1424  1.1       ad 		im->im_xfer[i].ix_size = 0;
   1425  1.1       ad 	*imp = im;
   1426  1.1       ad 
   1427  1.1       ad 	return (0);
   1428  1.1       ad }
   1429  1.1       ad 
   1430  1.1       ad /*
   1431  1.1       ad  * Free a message wrapper.
   1432  1.1       ad  */
   1433  1.1       ad void
   1434  1.1       ad iop_msg_free(struct iop_softc *sc, struct iop_initiator *ii, struct iop_msg *im)
   1435  1.1       ad {
   1436  1.1       ad 	int s;
   1437  1.1       ad 
   1438  1.1       ad #ifdef I2ODEBUG
   1439  1.1       ad 	if ((im->im_flags & IM_ALLOCED) == 0)
   1440  1.1       ad 		panic("iop_msg_free: wrapper not allocated");
   1441  1.1       ad #endif
   1442  1.1       ad 
   1443  1.1       ad 	/* XXX */
   1444  1.1       ad 	bus_dmamap_destroy(sc->sc_dmat, im->im_xfer[0].ix_map);
   1445  1.1       ad 
   1446  1.1       ad 	s = splbio();	/* XXX */
   1447  1.1       ad 
   1448  1.1       ad 	if ((im->im_flags & (IM_DISCARD | IM_NOICTX)) == 0)
   1449  1.5       ad 		TAILQ_REMOVE(IOP_TCTXHASH(im->im_tctx), im, im_hash);
   1450  1.1       ad 
   1451  1.1       ad 	im->im_flags = 0;
   1452  1.1       ad 	pool_put(iop_msgpool, im);
   1453  1.1       ad 	splx(s);
   1454  1.1       ad }
   1455  1.1       ad 
   1456  1.1       ad /*
   1457  1.5       ad  * Map a data transfer.  Write a scatter-gather list into the message frame.
   1458  1.1       ad  */
   1459  1.1       ad int
   1460  1.1       ad iop_msg_map(struct iop_softc *sc, struct iop_msg *im, void *xferaddr,
   1461  1.1       ad 	    int xfersize, int out)
   1462  1.1       ad {
   1463  1.1       ad 	struct iop_xfer *ix;
   1464  1.1       ad 	u_int32_t *mb;
   1465  1.5       ad 	int rv, seg, i;
   1466  1.5       ad 
   1467  1.1       ad 	for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++)
   1468  1.1       ad 		if (ix->ix_size == 0)
   1469  1.1       ad 			break;
   1470  1.1       ad #ifdef I2ODEBUG
   1471  1.1       ad 	if (i == IOP_MAX_MSG_XFERS)
   1472  1.1       ad 		panic("iop_msg_map: too many xfers");
   1473  1.1       ad #endif
   1474  1.1       ad 
   1475  1.1       ad 	/* Only the first DMA map is static. */
   1476  1.1       ad 	if (i != 0) {
   1477  1.1       ad 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
   1478  1.1       ad 		    IOP_MAX_SGL_ENTRIES, IOP_MAX_XFER, 0,
   1479  1.1       ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ix->ix_map);
   1480  1.1       ad 		if (rv != 0)
   1481  1.1       ad 			return (rv);
   1482  1.1       ad 	}
   1483  1.1       ad 
   1484  1.5       ad 	ix->ix_flags = (out ? IX_OUT : IX_IN);
   1485  1.1       ad 	ix->ix_size = xfersize;
   1486  1.1       ad 
   1487  1.1       ad 	rv = bus_dmamap_load(sc->sc_dmat, ix->ix_map, xferaddr, xfersize,
   1488  1.1       ad 	    NULL, 0);
   1489  1.1       ad 	if (rv != 0)
   1490  1.1       ad 		return (rv);
   1491  1.1       ad 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   1492  1.1       ad 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   1493  1.1       ad 
   1494  1.1       ad 	mb = im->im_msg + (im->im_msg[0] >> 16);
   1495  1.1       ad 	if (out)
   1496  1.1       ad 		out = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   1497  1.1       ad 	else
   1498  1.1       ad 		out = I2O_SGL_SIMPLE;
   1499  1.1       ad 
   1500  1.1       ad 	for (seg = 0; seg < ix->ix_map->dm_nsegs; seg++) {
   1501  1.1       ad #ifdef I2ODEBUG
   1502  1.1       ad 		if ((seg << 1) + (im->im_msg[0] >> 16) >=
   1503  1.1       ad 		    (IOP_MAX_MSG_SIZE >> 2))
   1504  1.1       ad 			panic("iop_map_xfer: message frame too large");
   1505  1.1       ad #endif
   1506  1.1       ad 		if (seg == ix->ix_map->dm_nsegs - 1)
   1507  1.1       ad 			out |= I2O_SGL_END_BUFFER;
   1508  1.1       ad 		*mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_len | out;
   1509  1.1       ad 		*mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_addr;
   1510  1.1       ad 	}
   1511  1.1       ad 
   1512  1.1       ad 	/*
   1513  1.1       ad 	 * If this is the first xfer we've mapped for this message, adjust
   1514  1.1       ad 	 * the SGL offset field in the message header.
   1515  1.1       ad 	 */
   1516  1.2       ad 	if ((im->im_flags & IM_SGLOFFADJ) == 0) {
   1517  1.1       ad 		im->im_msg[0] += ((im->im_msg[0] >> 16) + seg * 2) << 4;
   1518  1.2       ad 		im->im_flags |= IM_SGLOFFADJ;
   1519  1.2       ad 	}
   1520  1.1       ad 	im->im_msg[0] += (seg << 17);
   1521  1.1       ad 	return (0);
   1522  1.1       ad }
   1523  1.1       ad 
   1524  1.1       ad /*
   1525  1.1       ad  * Unmap all data transfers associated with a message wrapper.
   1526  1.1       ad  */
   1527  1.1       ad void
   1528  1.1       ad iop_msg_unmap(struct iop_softc *sc, struct iop_msg *im)
   1529  1.1       ad {
   1530  1.1       ad 	struct iop_xfer *ix;
   1531  1.1       ad 	int i;
   1532  1.1       ad 
   1533  1.1       ad 	for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++) {
   1534  1.1       ad 		if (ix->ix_size == 0)
   1535  1.2       ad 			break;
   1536  1.1       ad 		bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, ix->ix_size,
   1537  1.1       ad 		    ix->ix_flags & IX_OUT ? BUS_DMASYNC_POSTWRITE :
   1538  1.1       ad 		    BUS_DMASYNC_POSTREAD);
   1539  1.1       ad 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   1540  1.1       ad 
   1541  1.1       ad 		/* Only the first DMA map is static. */
   1542  1.1       ad 		if (i != 0)
   1543  1.1       ad 			bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   1544  1.1       ad 
   1545  1.1       ad 		ix->ix_size = 0;
   1546  1.1       ad 	}
   1547  1.1       ad }
   1548  1.1       ad 
   1549  1.1       ad /*
   1550  1.1       ad  * Send a message to the IOP.  Optionally, poll on completion.  Return
   1551  1.1       ad  * non-zero if failure status is returned and IM_NOINTR is set.
   1552  1.1       ad  */
   1553  1.1       ad int
   1554  1.1       ad iop_msg_send(struct iop_softc *sc, struct iop_msg *im, int timo)
   1555  1.1       ad {
   1556  1.5       ad 	u_int32_t mfa, rmfa;
   1557  1.2       ad 	int rv, status, i, s;
   1558  1.1       ad 
   1559  1.1       ad #ifdef I2ODEBUG
   1560  1.1       ad 	if ((im->im_flags & IM_NOICTX) == 0)
   1561  1.1       ad 		if (im->im_msg[3] == IOP_ICTX &&
   1562  1.1       ad 		    (im->im_flags & IM_NOINTR) == 0)
   1563  1.1       ad 			panic("iop_msg_send: IOP_ICTX and !IM_NOINTR");
   1564  1.1       ad 	if ((im->im_flags & IM_DISCARD) != 0)
   1565  1.1       ad 		panic("iop_msg_send: IM_DISCARD");
   1566  1.1       ad #endif
   1567  1.1       ad 
   1568  1.2       ad 	s = splbio();	/* XXX */
   1569  1.1       ad 
   1570  1.1       ad 	/* Wait up to 250ms for an MFA. */
   1571  1.5       ad 	POLL(250, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1572  1.1       ad 	if (mfa == IOP_MFA_EMPTY) {
   1573  1.1       ad 		DPRINTF(("%s: mfa not forthcoming\n", sc->sc_dv.dv_xname));
   1574  1.2       ad 		splx(s);
   1575  1.1       ad 		return (EBUSY);
   1576  1.1       ad 	}
   1577  1.1       ad 
   1578  1.1       ad 	/* Perform reply queue DMA synchronisation and update counters. */
   1579  1.1       ad 	if ((im->im_flags & IM_NOICTX) == 0) {
   1580  1.1       ad 		if (sc->sc_stat.is_cur_hwqueue == 0)
   1581  1.1       ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   1582  1.1       ad 			    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1583  1.1       ad 		for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1584  1.1       ad 			sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
   1585  1.1       ad 		sc->sc_stat.is_requests++;
   1586  1.1       ad 		if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
   1587  1.1       ad 			sc->sc_stat.is_peak_hwqueue =
   1588  1.1       ad 			    sc->sc_stat.is_cur_hwqueue;
   1589  1.1       ad 	}
   1590  1.1       ad 
   1591  1.1       ad 	/* Terminate scatter/gather lists. */
   1592  1.1       ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1593  1.1       ad 		im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
   1594  1.1       ad 
   1595  1.1       ad 	/* Post the message frame. */
   1596  1.1       ad 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
   1597  1.1       ad 	    im->im_msg, im->im_msg[0] >> 16);
   1598  1.5       ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa,
   1599  1.5       ad 	    (im->im_msg[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
   1600  1.1       ad 
   1601  1.1       ad 	/* Post the MFA back to the IOP, thus starting the command. */
   1602  1.5       ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   1603  1.1       ad 
   1604  1.1       ad 	if (timo == 0) {
   1605  1.2       ad 		splx(s);
   1606  1.1       ad 		return (0);
   1607  1.1       ad 	}
   1608  1.1       ad 
   1609  1.1       ad 	/* Wait for completion. */
   1610  1.1       ad 	for (timo *= 10; timo != 0; timo--) {
   1611  1.5       ad 		if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0) {
   1612  1.5       ad 			/* Double read to account for IOP bug. */
   1613  1.5       ad 			rmfa = iop_inl(sc, IOP_REG_OFIFO);
   1614  1.5       ad 			if (rmfa == IOP_MFA_EMPTY)
   1615  1.5       ad 				rmfa = iop_inl(sc, IOP_REG_OFIFO);
   1616  1.5       ad 			if (rmfa != IOP_MFA_EMPTY)
   1617  1.5       ad 				status = iop_handle_reply(sc, rmfa);
   1618  1.5       ad 		}
   1619  1.1       ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1620  1.1       ad 			break;
   1621  1.1       ad 		DELAY(100);
   1622  1.1       ad 	}
   1623  1.1       ad 
   1624  1.2       ad 	splx(s);
   1625  1.1       ad 
   1626  1.1       ad 	if (timo == 0) {
   1627  1.5       ad #ifdef I2ODEBUG
   1628  1.5       ad 		printf("%s: poll - no reply\n", sc->sc_dv.dv_xname);
   1629  1.5       ad 		if (iop_status_get(sc) != 0)
   1630  1.5       ad 			printf("iop_msg_send: unable to retrieve status\n");
   1631  1.5       ad 		else
   1632  1.5       ad 			printf("iop_msg_send: IOP state = %d\n",
   1633  1.5       ad 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   1634  1.5       ad #endif
   1635  1.1       ad 		rv = EBUSY;
   1636  1.1       ad 	} else if ((im->im_flags & IM_NOINTR) != 0)
   1637  1.1       ad 		rv = (status != I2O_STATUS_SUCCESS ? EIO : 0);
   1638  1.1       ad 
   1639  1.1       ad 	return (rv);
   1640  1.1       ad }
   1641  1.1       ad 
   1642  1.1       ad /*
   1643  1.1       ad  * Try to post a message to the adapter; if that's not possible, enqueue it
   1644  1.5       ad  * with us.  If a timeout is specified, wait for the message to complete.
   1645  1.1       ad  */
   1646  1.1       ad int
   1647  1.5       ad iop_msg_enqueue(struct iop_softc *sc, struct iop_msg *im, int timo)
   1648  1.1       ad {
   1649  1.1       ad 	u_int mfa;
   1650  1.5       ad 	int s, fromqueue, i, rv;
   1651  1.1       ad 
   1652  1.1       ad #ifdef I2ODEBUG
   1653  1.1       ad 	if (im == NULL)
   1654  1.1       ad 		panic("iop_msg_enqueue: im == NULL");
   1655  1.1       ad 	if (sc == NULL)
   1656  1.1       ad 		panic("iop_msg_enqueue: sc == NULL");
   1657  1.1       ad 	if ((im->im_flags & IM_NOICTX) != 0)
   1658  1.1       ad 		panic("iop_msg_enqueue: IM_NOICTX");
   1659  1.1       ad 	if (im->im_msg[3] == IOP_ICTX && (im->im_flags & IM_NOINTR) == 0)
   1660  1.5       ad 		panic("iop_msg_enqueue: IOP_ICTX and no IM_NOINTR");
   1661  1.5       ad 	if ((im->im_flags & IM_DISCARD) != 0 && timo != 0)
   1662  1.5       ad 		panic("iop_msg_enqueue: IM_DISCARD && timo != 0");
   1663  1.5       ad 	if ((im->im_flags & IM_NOINTR) == 0 && timo != 0)
   1664  1.5       ad 		panic("iop_msg_enqueue: !IM_NOINTR && timo != 0");
   1665  1.1       ad #endif
   1666  1.1       ad 
   1667  1.1       ad 	s = splbio();	/* XXX */
   1668  1.1       ad 	fromqueue = (im == SIMPLEQ_FIRST(&sc->sc_queue));
   1669  1.1       ad 
   1670  1.1       ad 	if (sc->sc_stat.is_cur_hwqueue >= sc->sc_maxqueuecnt) {
   1671  1.1       ad 		/*
   1672  1.1       ad 		 * While the IOP may be able to accept more inbound message
   1673  1.1       ad 		 * frames than it advertises, don't push harder than it
   1674  1.1       ad 		 * wants to go lest we starve it.
   1675  1.1       ad 		 *
   1676  1.1       ad 		 * XXX We should be handling IOP resource shortages.
   1677  1.1       ad 		 */
   1678  1.5       ad 		mfa = IOP_MFA_EMPTY;
   1679  1.5       ad 		DPRINTF(("iop_msg_enqueue: exceeded max queue count\n"));
   1680  1.1       ad 	} else {
   1681  1.1       ad 		/* Double read to account for IOP bug. */
   1682  1.5       ad 		if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY)
   1683  1.5       ad 			mfa = iop_inl(sc, IOP_REG_IFIFO);
   1684  1.1       ad 	}
   1685  1.1       ad 
   1686  1.1       ad 	if (mfa == IOP_MFA_EMPTY) {
   1687  1.5       ad 		DPRINTF(("iop_msg_enqueue: no mfa\n"));
   1688  1.1       ad 		/* Can't transfer to h/w queue - queue with us. */
   1689  1.1       ad 		if (!fromqueue) {
   1690  1.1       ad 			SIMPLEQ_INSERT_TAIL(&sc->sc_queue, im, im_queue);
   1691  1.1       ad 			if (++sc->sc_stat.is_cur_swqueue >
   1692  1.1       ad 			    sc->sc_stat.is_peak_swqueue)
   1693  1.1       ad 				sc->sc_stat.is_peak_swqueue =
   1694  1.1       ad 				    sc->sc_stat.is_cur_swqueue;
   1695  1.1       ad 		}
   1696  1.1       ad 		splx(s);
   1697  1.5       ad 		if ((im->im_flags & IM_NOINTR) != 0)
   1698  1.5       ad 			rv = iop_msg_wait(sc, im, timo);
   1699  1.5       ad 		else
   1700  1.5       ad 			rv = 0;
   1701  1.5       ad 		return (rv);
   1702  1.1       ad 	} else if (fromqueue) {
   1703  1.1       ad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, im, im_queue);
   1704  1.1       ad 		sc->sc_stat.is_cur_swqueue--;
   1705  1.1       ad 	}
   1706  1.1       ad 
   1707  1.5       ad 	if ((im->im_flags & IM_NOINTR) != 0)
   1708  1.5       ad 		im->im_flags |= IM_WAITING;
   1709  1.5       ad 
   1710  1.1       ad 	/* Perform reply queue DMA synchronisation and update counters. */
   1711  1.1       ad 	if (sc->sc_stat.is_cur_hwqueue == 0)
   1712  1.1       ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   1713  1.1       ad 		    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1714  1.1       ad 
   1715  1.1       ad 	for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1716  1.1       ad 		sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
   1717  1.1       ad 	sc->sc_stat.is_requests++;
   1718  1.1       ad 	if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
   1719  1.1       ad 		sc->sc_stat.is_peak_hwqueue = sc->sc_stat.is_cur_hwqueue;
   1720  1.1       ad 
   1721  1.1       ad 	/* Terminate the scatter/gather list. */
   1722  1.1       ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1723  1.1       ad 		im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
   1724  1.1       ad 
   1725  1.1       ad 	/* Post the message frame. */
   1726  1.1       ad 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
   1727  1.1       ad 	    im->im_msg, im->im_msg[0] >> 16);
   1728  1.5       ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa,
   1729  1.5       ad 	    (im->im_msg[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
   1730  1.1       ad 
   1731  1.1       ad 	/* Post the MFA back to the IOP, thus starting the command. */
   1732  1.5       ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   1733  1.1       ad 
   1734  1.1       ad 	/* If this is a discardable message wrapper, free it. */
   1735  1.1       ad 	if ((im->im_flags & IM_DISCARD) != 0)
   1736  1.1       ad 		iop_msg_free(sc, NULL, im);
   1737  1.1       ad 	splx(s);
   1738  1.5       ad 
   1739  1.5       ad 	if ((im->im_flags & IM_NOINTR) != 0)
   1740  1.5       ad 		rv = iop_msg_wait(sc, im, timo);
   1741  1.5       ad 	else
   1742  1.5       ad 		rv = 0;
   1743  1.5       ad 	return (rv);
   1744  1.1       ad }
   1745  1.1       ad 
   1746  1.1       ad /*
   1747  1.5       ad  * Wait for the specified message to complete.
   1748  1.1       ad  */
   1749  1.5       ad static int
   1750  1.1       ad iop_msg_wait(struct iop_softc *sc, struct iop_msg *im, int timo)
   1751  1.1       ad {
   1752  1.5       ad 	struct i2o_reply *rb;
   1753  1.5       ad 	int rv, s;
   1754  1.1       ad 
   1755  1.5       ad 	s = splbio();
   1756  1.5       ad 	if ((im->im_flags & IM_REPLIED) != 0) {
   1757  1.5       ad 		splx(s);
   1758  1.1       ad 		return (0);
   1759  1.5       ad 	}
   1760  1.5       ad 	rv = tsleep(im, PRIBIO, "iopmsg", timo * hz / 1000);
   1761  1.5       ad 	splx(s);
   1762  1.5       ad #ifdef I2ODEBUG
   1763  1.5       ad 	if (rv != 0) {
   1764  1.5       ad 		printf("iop_msg_wait: tsleep() == %d\n", rv);
   1765  1.5       ad 		if (iop_status_get(sc) != 0)
   1766  1.5       ad 			printf("iop_msg_wait: unable to retrieve status\n");
   1767  1.5       ad 		else
   1768  1.5       ad 			printf("iop_msg_wait: IOP state = %d\n",
   1769  1.5       ad 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   1770  1.5       ad 	}
   1771  1.5       ad #endif
   1772  1.5       ad 	if ((im->im_flags & (IM_REPLIED | IM_NOSTATUS)) == IM_REPLIED) {
   1773  1.5       ad 		rb = (struct i2o_reply *)im->im_msg;
   1774  1.5       ad 		rv = (rb->reqstatus != I2O_STATUS_SUCCESS ? EIO : 0);
   1775  1.5       ad 	}
   1776  1.1       ad 	return (rv);
   1777  1.1       ad }
   1778  1.1       ad 
   1779  1.1       ad /*
   1780  1.1       ad  * Release an unused message frame back to the IOP's inbound fifo.
   1781  1.1       ad  */
   1782  1.1       ad static void
   1783  1.1       ad iop_release_mfa(struct iop_softc *sc, u_int32_t mfa)
   1784  1.1       ad {
   1785  1.1       ad 
   1786  1.1       ad 	/* Use the frame to issue a no-op. */
   1787  1.5       ad 	iop_outl(sc, mfa, I2O_VERSION_11 | (4 << 16));
   1788  1.5       ad 	iop_outl(sc, mfa + 4, I2O_MSGFUNC(I2O_TID_IOP, I2O_UTIL_NOP));
   1789  1.5       ad 	iop_outl(sc, mfa + 8, 0);
   1790  1.5       ad 	iop_outl(sc, mfa + 12, 0);
   1791  1.1       ad 
   1792  1.5       ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   1793  1.1       ad }
   1794  1.1       ad 
   1795  1.1       ad #ifdef I2ODEBUG
   1796  1.1       ad /*
   1797  1.1       ad  * Print status information from a failure reply frame.
   1798  1.1       ad  */
   1799  1.1       ad static void
   1800  1.1       ad iop_reply_print(struct iop_softc *sc, struct iop_msg *im,
   1801  1.1       ad 		struct i2o_reply *rb)
   1802  1.1       ad {
   1803  1.5       ad 	u_int function, detail;
   1804  1.1       ad #ifdef I2OVERBOSE
   1805  1.1       ad 	const char *statusstr;
   1806  1.1       ad #endif
   1807  1.1       ad 
   1808  1.5       ad 	if (im != NULL && (im->im_flags & IM_REPLIED) == 0)
   1809  1.1       ad 		panic("iop_msg_print_status: %p not replied to", im);
   1810  1.1       ad 
   1811  1.5       ad 	function = (le32toh(rb->msgfunc) >> 24) & 0xff;
   1812  1.1       ad 	detail = le16toh(rb->detail);
   1813  1.1       ad 
   1814  1.5       ad 	printf("%s: reply:\n", sc->sc_dv.dv_xname);
   1815  1.5       ad 
   1816  1.1       ad #ifdef I2OVERBOSE
   1817  1.1       ad 	if (rb->reqstatus < sizeof(iop_status) / sizeof(iop_status[0]))
   1818  1.1       ad 		statusstr = iop_status[rb->reqstatus];
   1819  1.1       ad 	else
   1820  1.1       ad 		statusstr = "undefined error code";
   1821  1.1       ad 
   1822  1.5       ad 	printf("%s:   function=0x%02x status=0x%02x (%s)\n",
   1823  1.5       ad 	    sc->sc_dv.dv_xname, function, rb->reqstatus, statusstr);
   1824  1.1       ad #else
   1825  1.5       ad 	printf("%s:   function=0x%02x status=0x%02x\n",
   1826  1.5       ad 	    sc->sc_dv.dv_xname, function, rb->reqstatus);
   1827  1.1       ad #endif
   1828  1.5       ad 	printf("%s:   detail=0x%04x ictx=0x%08x tctx=0x%08x\n",
   1829  1.5       ad 	    sc->sc_dv.dv_xname, detail, le32toh(rb->msgictx),
   1830  1.5       ad 	    le32toh(rb->msgtctx));
   1831  1.5       ad 	printf("%s:   tidi=%d tidt=%d flags=0x%02x\n", sc->sc_dv.dv_xname,
   1832  1.5       ad 	    (le32toh(rb->msgfunc) >> 12) & 4095, le32toh(rb->msgfunc) & 4095,
   1833  1.5       ad 	    (le32toh(rb->msgflags) >> 8) & 0xff);
   1834  1.1       ad }
   1835  1.1       ad #endif
   1836  1.1       ad 
   1837  1.1       ad /*
   1838  1.5       ad  * Translate an I2O ASCII field into a C string.
   1839  1.1       ad  */
   1840  1.1       ad void
   1841  1.5       ad iop_strvis(struct iop_softc *sc, const char *src, int slen, char *dst, int dlen)
   1842  1.1       ad {
   1843  1.5       ad 	int hc, lc, i, nit;
   1844  1.1       ad 
   1845  1.1       ad 	dlen--;
   1846  1.1       ad 	lc = 0;
   1847  1.1       ad 	hc = 0;
   1848  1.1       ad 	i = 0;
   1849  1.5       ad 
   1850  1.5       ad 	/*
   1851  1.5       ad 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
   1852  1.5       ad 	 * spec has nothing to say about it.  Since AMI fields are usually
   1853  1.5       ad 	 * filled with junk after the terminator, ...
   1854  1.5       ad 	 */
   1855  1.5       ad 	nit = (le16toh(sc->sc_status.orgid) != I2O_ORG_DPT);
   1856  1.5       ad 
   1857  1.5       ad 	while (slen-- != 0 && dlen-- != 0) {
   1858  1.5       ad 		if (nit && *src == '\0')
   1859  1.5       ad 			break;
   1860  1.5       ad 		else if (*src <= 0x20 || *src >= 0x7f) {
   1861  1.1       ad 			if (hc)
   1862  1.1       ad 				dst[i++] = ' ';
   1863  1.1       ad 		} else {
   1864  1.1       ad 			hc = 1;
   1865  1.1       ad 			dst[i++] = *src;
   1866  1.1       ad 			lc = i;
   1867  1.1       ad 		}
   1868  1.1       ad 		src++;
   1869  1.1       ad 	}
   1870  1.1       ad 
   1871  1.1       ad 	dst[lc] = '\0';
   1872  1.1       ad }
   1873  1.1       ad 
   1874  1.1       ad /*
   1875  1.5       ad  * Claim or unclaim the specified TID.
   1876  1.1       ad  */
   1877  1.1       ad int
   1878  1.5       ad iop_util_claim(struct iop_softc *sc, struct iop_initiator *ii, int release,
   1879  1.5       ad 	      int flags)
   1880  1.1       ad {
   1881  1.5       ad 	struct iop_msg *im;
   1882  1.5       ad 	struct i2o_util_claim *mb;
   1883  1.5       ad 	int rv, func;
   1884  1.5       ad 
   1885  1.5       ad 	func = release ? I2O_UTIL_CLAIM_RELEASE : I2O_UTIL_CLAIM;
   1886  1.5       ad 
   1887  1.5       ad 	if ((rv = iop_msg_alloc(sc, ii, &im, IM_NOINTR)) != 0)
   1888  1.5       ad 		return (rv);
   1889  1.5       ad 
   1890  1.5       ad 	/* We can use the same structure, as both are identical. */
   1891  1.5       ad 	mb = (struct i2o_util_claim *)im->im_msg;
   1892  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_claim);
   1893  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, func);
   1894  1.5       ad 	mb->msgictx = ii->ii_ictx;
   1895  1.5       ad 	mb->msgtctx = im->im_tctx;
   1896  1.5       ad 	mb->flags = flags;
   1897  1.5       ad 
   1898  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1899  1.5       ad 	iop_msg_free(sc, ii, im);
   1900  1.5       ad 	return (rv);
   1901  1.5       ad }
   1902  1.5       ad 
   1903  1.5       ad /*
   1904  1.5       ad  * Perform an abort.
   1905  1.5       ad  */
   1906  1.5       ad int iop_util_abort(struct iop_softc *sc, struct iop_initiator *ii, int func,
   1907  1.5       ad 		  int tctxabort, int flags)
   1908  1.5       ad {
   1909  1.5       ad 	struct iop_msg *im;
   1910  1.5       ad 	struct i2o_util_abort *mb;
   1911  1.5       ad 	int rv;
   1912  1.5       ad 
   1913  1.5       ad 	if ((rv = iop_msg_alloc(sc, ii, &im, IM_NOINTR)) != 0)
   1914  1.5       ad 		return (rv);
   1915  1.1       ad 
   1916  1.5       ad 	mb = (struct i2o_util_abort *)im->im_msg;
   1917  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_abort);
   1918  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_ABORT);
   1919  1.5       ad 	mb->msgictx = ii->ii_ictx;
   1920  1.5       ad 	mb->msgtctx = im->im_tctx;
   1921  1.5       ad 	mb->flags = (func << 24) | flags;
   1922  1.5       ad 	mb->tctxabort = tctxabort;
   1923  1.1       ad 
   1924  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1925  1.5       ad 	iop_msg_free(sc, ii, im);
   1926  1.5       ad 	return (rv);
   1927  1.1       ad }
   1928  1.1       ad 
   1929  1.1       ad /*
   1930  1.5       ad  * Enable or disable event types for the specified device.
   1931  1.1       ad  */
   1932  1.5       ad int iop_util_eventreg(struct iop_softc *sc, struct iop_initiator *ii, int mask)
   1933  1.5       ad {
   1934  1.5       ad 	struct iop_msg *im;
   1935  1.5       ad 	struct i2o_util_event_register *mb;
   1936  1.5       ad 	int rv;
   1937  1.5       ad 
   1938  1.5       ad 	if ((rv = iop_msg_alloc(sc, ii, &im, 0)) != 0)
   1939  1.5       ad 		return (rv);
   1940  1.5       ad 
   1941  1.5       ad 	mb = (struct i2o_util_event_register *)im->im_msg;
   1942  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_event_register);
   1943  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_EVENT_REGISTER);
   1944  1.5       ad 	mb->msgictx = ii->ii_ictx;
   1945  1.5       ad 	mb->msgtctx = im->im_tctx;
   1946  1.5       ad 	mb->eventmask = mask;
   1947  1.5       ad 
   1948  1.5       ad 	return (iop_msg_enqueue(sc, im, 0));
   1949  1.5       ad }
   1950  1.5       ad 
   1951  1.1       ad int
   1952  1.5       ad iopopen(dev_t dev, int flag, int mode, struct proc *p)
   1953  1.1       ad {
   1954  1.5       ad 	struct iop_softc *sc;
   1955  1.5       ad 	int unit, error;
   1956  1.5       ad 
   1957  1.5       ad 	unit = minor(dev);
   1958  1.5       ad 
   1959  1.5       ad 	sc = device_lookup(&iop_cd, minor(dev));
   1960  1.5       ad 	if ((sc = iop_cd.cd_devs[unit]) == NULL)
   1961  1.1       ad 		return (ENXIO);
   1962  1.5       ad 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1963  1.5       ad 		return (error);
   1964  1.5       ad 
   1965  1.5       ad 	if ((sc->sc_flags & IOP_OPEN) != 0)
   1966  1.5       ad 		return (EBUSY);
   1967  1.5       ad 	if ((sc->sc_flags & IOP_ONLINE) == 0)
   1968  1.5       ad 		return (EIO);
   1969  1.5       ad 	sc->sc_flags |= IOP_OPEN;
   1970  1.5       ad 
   1971  1.5       ad 	return (0);
   1972  1.1       ad }
   1973  1.1       ad 
   1974  1.5       ad int
   1975  1.5       ad iopclose(dev_t dev, int flag, int mode, struct proc *p)
   1976  1.1       ad {
   1977  1.5       ad 	struct iop_softc *sc;
   1978  1.1       ad 
   1979  1.5       ad 	sc = device_lookup(&iop_cd, minor(dev));
   1980  1.5       ad 	sc->sc_flags &= ~IOP_OPEN;
   1981  1.5       ad 	return (0);
   1982  1.1       ad }
   1983  1.1       ad 
   1984  1.1       ad int
   1985  1.5       ad iopioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   1986  1.1       ad {
   1987  1.5       ad 	struct iop_softc *sc;
   1988  1.5       ad 	struct iovec *iov;
   1989  1.5       ad 	struct ioppt *pt;
   1990  1.1       ad 	struct iop_msg *im;
   1991  1.5       ad 	struct i2o_msg *mb;
   1992  1.5       ad 	struct i2o_reply *rb;
   1993  1.5       ad 	int rv, i;
   1994  1.5       ad 
   1995  1.5       ad 	if (securelevel >= 2)
   1996  1.5       ad 		return (EPERM);
   1997  1.5       ad 
   1998  1.5       ad 	sc = device_lookup(&iop_cd, minor(dev));
   1999  1.5       ad 
   2000  1.5       ad 	switch (cmd) {
   2001  1.5       ad 	case IOPIOCPT:
   2002  1.5       ad 		pt = (struct ioppt *)data;
   2003  1.5       ad 
   2004  1.5       ad 		if (pt->pt_msglen > IOP_MAX_MSG_SIZE ||
   2005  1.5       ad 		    pt->pt_msglen < sizeof(struct i2o_msg) ||
   2006  1.5       ad 		    pt->pt_nbufs > IOP_MAX_MSG_XFERS ||
   2007  1.5       ad 		    pt->pt_nbufs < 0 ||
   2008  1.5       ad 		    pt->pt_replylen < 0 ||
   2009  1.5       ad 		    pt->pt_timo < 1000 ||
   2010  1.5       ad 		    pt->pt_timo > 5*60*1000) {
   2011  1.5       ad 		    	rv = EINVAL;
   2012  1.5       ad 		    	break;
   2013  1.5       ad 		}
   2014  1.5       ad 
   2015  1.5       ad 		rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR | IM_NOSTATUS);
   2016  1.5       ad 		if (rv != 0)
   2017  1.5       ad 			break;
   2018  1.5       ad 
   2019  1.5       ad 		if ((rv = copyin(pt->pt_msg, im->im_msg, pt->pt_msglen)) != 0) {
   2020  1.5       ad 			iop_msg_free(sc, NULL, im);
   2021  1.5       ad 			break;
   2022  1.5       ad 		}
   2023  1.5       ad 
   2024  1.5       ad 		mb = (struct i2o_msg *)im->im_msg;
   2025  1.5       ad 		mb->msgictx = IOP_ICTX;
   2026  1.5       ad 		mb->msgtctx = im->im_tctx;
   2027  1.5       ad 
   2028  1.5       ad 		for (i = 0; i < pt->pt_nbufs; i++) {
   2029  1.5       ad 			rv = iop_msg_map(sc, im, pt->pt_bufs[i].ptb_data,
   2030  1.5       ad 			    pt->pt_bufs[i].ptb_datalen,
   2031  1.5       ad 			    pt->pt_bufs[i].ptb_out != 0);
   2032  1.5       ad 			if (rv != 0) {
   2033  1.5       ad 				iop_msg_free(sc, NULL, im);
   2034  1.5       ad 				return (rv);
   2035  1.5       ad 			}
   2036  1.5       ad 		}
   2037  1.5       ad 
   2038  1.5       ad 		if ((rv = iop_msg_enqueue(sc, im, pt->pt_timo)) == 0) {
   2039  1.5       ad 			rb = (struct i2o_reply *)im->im_msg;
   2040  1.5       ad 			i = (le32toh(rb->msgflags) >> 14) & ~3;	/* XXX */
   2041  1.5       ad 			if (i > IOP_MAX_REPLY_SIZE)
   2042  1.5       ad 				i = IOP_MAX_REPLY_SIZE;
   2043  1.5       ad 			if (i > pt->pt_replylen)
   2044  1.5       ad 				i = pt->pt_replylen;
   2045  1.5       ad 			rv = copyout(rb, pt->pt_reply, i);
   2046  1.5       ad 		}
   2047  1.5       ad 
   2048  1.5       ad 		iop_msg_free(sc, NULL, im);
   2049  1.5       ad 		break;
   2050  1.1       ad 
   2051  1.5       ad 	case IOPIOCGLCT:
   2052  1.5       ad 		iov = (struct iovec *)data;
   2053  1.5       ad 		rv = lockmgr(&sc->sc_conflock, LK_EXCLUSIVE, NULL);
   2054  1.5       ad 		if (rv == 0) {
   2055  1.5       ad 			i = le16toh(sc->sc_lct->tablesize) << 2;
   2056  1.5       ad 			if (i > iov->iov_len)
   2057  1.5       ad 				i = iov->iov_len;
   2058  1.5       ad 			else
   2059  1.5       ad 				iov->iov_len = i;
   2060  1.5       ad 			rv = copyout(sc->sc_lct, iov->iov_base, i);
   2061  1.5       ad 			lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
   2062  1.5       ad 		}
   2063  1.5       ad 		break;
   2064  1.1       ad 
   2065  1.5       ad 	case IOPIOCGSTATUS:
   2066  1.5       ad 		iov = (struct iovec *)data;
   2067  1.5       ad 		i = sizeof(struct i2o_status);
   2068  1.5       ad 		if (i > iov->iov_len)
   2069  1.5       ad 			i = iov->iov_len;
   2070  1.5       ad 		else
   2071  1.5       ad 			iov->iov_len = i;
   2072  1.5       ad 		if ((rv = iop_status_get(sc)) == 0)
   2073  1.5       ad 			rv = copyout(&sc->sc_status, iov->iov_base, i);
   2074  1.5       ad 		break;
   2075  1.5       ad 
   2076  1.5       ad 	case IOPIOCRECONFIG:
   2077  1.5       ad 		rv = iop_reconfigure(sc, 0);
   2078  1.5       ad 		break;
   2079  1.5       ad 
   2080  1.5       ad 	default:
   2081  1.5       ad #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
   2082  1.5       ad 		printf("%s: unknown ioctl %lx\n", sc->sc_dv.dv_xname, cmd);
   2083  1.5       ad #endif
   2084  1.5       ad 		rv = ENOTTY;
   2085  1.5       ad 		break;
   2086  1.5       ad 	}
   2087  1.1       ad 
   2088  1.1       ad 	return (rv);
   2089  1.5       ad }
   2090