Home | History | Annotate | Line # | Download | only in i2o
iop.c revision 1.8
      1  1.8       ad /*	$NetBSD: iop.c,v 1.8 2001/01/01 19:03:30 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.8       ad 	int i, j, 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.8       ad 
    641  1.8       ad 		/* Ignore uninteresting devices. */
    642  1.8       ad 		for (j = 0; j < sizeof(iop_class) / sizeof(iop_class[0]); j++)
    643  1.8       ad 			if (iop_class[j].ic_class == ia.ia_class)
    644  1.8       ad 				break;
    645  1.8       ad 		if (j < sizeof(iop_class) / sizeof(iop_class[0]) &&
    646  1.8       ad 		    (iop_class[j].ic_flags & IC_CONFIGURE) == 0)
    647  1.8       ad 			continue;
    648  1.1       ad 
    649  1.1       ad 		/*
    650  1.5       ad 		 * Try to configure the device only if it's not already
    651  1.5       ad 		 * configured.
    652  1.1       ad  		 */
    653  1.7       ad  		LIST_FOREACH(ii, &sc->sc_iilist, ii_list) {
    654  1.7       ad  			if ((ii->ii_flags & II_UTILITY) != 0)
    655  1.7       ad  				continue;
    656  1.5       ad  			if (ia.ia_tid == ii->ii_tid)
    657  1.5       ad 				break;
    658  1.7       ad 		}
    659  1.5       ad 		if (ii != NULL)
    660  1.5       ad 			continue;
    661  1.5       ad 
    662  1.5       ad 		if (config_found_sm(&sc->sc_dv, &ia, iop_print, iop_submatch))
    663  1.5       ad 			sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
    664  1.1       ad 	}
    665  1.1       ad }
    666  1.1       ad 
    667  1.1       ad static void
    668  1.1       ad iop_devinfo(int class, char *devinfo)
    669  1.1       ad {
    670  1.1       ad #ifdef I2OVERBOSE
    671  1.1       ad 	int i;
    672  1.1       ad 
    673  1.1       ad 	for (i = 0; i < sizeof(iop_class) / sizeof(iop_class[0]); i++)
    674  1.1       ad 		if (class == iop_class[i].ic_class)
    675  1.1       ad 			break;
    676  1.1       ad 
    677  1.1       ad 	if (i == sizeof(iop_class) / sizeof(iop_class[0]))
    678  1.1       ad 		sprintf(devinfo, "device (class 0x%x)", class);
    679  1.1       ad 	else
    680  1.1       ad 		strcpy(devinfo, iop_class[i].ic_caption);
    681  1.1       ad #else
    682  1.1       ad 
    683  1.1       ad 	sprintf(devinfo, "device (class 0x%x)", class);
    684  1.1       ad #endif
    685  1.1       ad }
    686  1.1       ad 
    687  1.1       ad static int
    688  1.1       ad iop_print(void *aux, const char *pnp)
    689  1.1       ad {
    690  1.1       ad 	struct iop_attach_args *ia;
    691  1.1       ad 	char devinfo[256];
    692  1.1       ad 
    693  1.1       ad 	ia = aux;
    694  1.1       ad 
    695  1.1       ad 	if (pnp != NULL) {
    696  1.1       ad 		iop_devinfo(ia->ia_class, devinfo);
    697  1.1       ad 		printf("%s at %s", devinfo, pnp);
    698  1.1       ad 	}
    699  1.1       ad 	printf(" tid %d", ia->ia_tid);
    700  1.1       ad 	return (UNCONF);
    701  1.1       ad }
    702  1.1       ad 
    703  1.5       ad #ifdef notyet
    704  1.1       ad static int
    705  1.1       ad iop_vendor_print(void *aux, const char *pnp)
    706  1.1       ad {
    707  1.1       ad 
    708  1.1       ad 	if (pnp != NULL)
    709  1.1       ad 		printf("vendor specific extension at %s", pnp);
    710  1.1       ad 	return (UNCONF);
    711  1.1       ad }
    712  1.5       ad #endif
    713  1.1       ad 
    714  1.1       ad static int
    715  1.1       ad iop_submatch(struct device *parent, struct cfdata *cf, void *aux)
    716  1.1       ad {
    717  1.1       ad 	struct iop_attach_args *ia;
    718  1.1       ad 
    719  1.1       ad 	ia = aux;
    720  1.1       ad 
    721  1.1       ad 	if (cf->iopcf_tid != IOPCF_TID_DEFAULT && cf->iopcf_tid != ia->ia_tid)
    722  1.1       ad 		return (0);
    723  1.1       ad 
    724  1.1       ad 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    725  1.1       ad }
    726  1.1       ad 
    727  1.1       ad /*
    728  1.1       ad  * Shut down all configured IOPs.
    729  1.1       ad  */
    730  1.1       ad static void
    731  1.1       ad iop_shutdown(void *junk)
    732  1.1       ad {
    733  1.1       ad 	struct iop_softc *sc;
    734  1.1       ad 	int i;
    735  1.1       ad 
    736  1.1       ad 	printf("shutting down iop devices... ");
    737  1.1       ad 
    738  1.1       ad 	for (i = 0; i < iop_cd.cd_ndevs; i++) {
    739  1.1       ad 		if ((sc = device_lookup(&iop_cd, i)) == NULL)
    740  1.1       ad 			continue;
    741  1.5       ad 		if ((sc->sc_flags & IOP_ONLINE) == 0)
    742  1.5       ad 			continue;
    743  1.5       ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_QUIESCE, IOP_ICTX,
    744  1.5       ad 		    0, 5000);
    745  1.5       ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_IOP_CLEAR, IOP_ICTX,
    746  1.5       ad 		    0, 5000);
    747  1.1       ad 	}
    748  1.1       ad 
    749  1.1       ad 	/* Wait.  Some boards could still be flushing, stupidly enough. */
    750  1.1       ad 	delay(5000*1000);
    751  1.1       ad 	printf(" done\n");
    752  1.1       ad }
    753  1.1       ad 
    754  1.1       ad /*
    755  1.1       ad  * Retrieve adapter status.
    756  1.1       ad  */
    757  1.1       ad static int
    758  1.1       ad iop_status_get(struct iop_softc *sc)
    759  1.1       ad {
    760  1.1       ad 	struct iop_msg *im;
    761  1.1       ad 	struct i2o_exec_status_get *mb;
    762  1.1       ad 	int rv, s;
    763  1.1       ad 
    764  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    765  1.1       ad 		return (rv);
    766  1.1       ad 
    767  1.1       ad 	mb = (struct i2o_exec_status_get *)im->im_msg;
    768  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_status_get);
    769  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_STATUS_GET);
    770  1.1       ad 	mb->reserved[0] = 0;
    771  1.1       ad 	mb->reserved[1] = 0;
    772  1.1       ad 	mb->reserved[2] = 0;
    773  1.1       ad 	mb->reserved[3] = 0;
    774  1.1       ad 	mb->addrlow = kvtop((caddr_t)&sc->sc_status);	/* XXX */
    775  1.1       ad 	mb->addrhigh = 0;
    776  1.1       ad 	mb->length = sizeof(sc->sc_status);
    777  1.1       ad 
    778  1.1       ad 	s = splbio();
    779  1.5       ad 	memset(&sc->sc_status, 0, sizeof(sc->sc_status));
    780  1.1       ad 
    781  1.1       ad 	if ((rv = iop_msg_send(sc, im, 0)) != 0) {
    782  1.1       ad 		splx(s);
    783  1.1       ad 		iop_msg_free(sc, NULL, im);
    784  1.1       ad 		return (rv);
    785  1.1       ad 	}
    786  1.1       ad 
    787  1.1       ad 	/* XXX */
    788  1.1       ad 	POLL(2500, *((volatile u_char *)&sc->sc_status.syncbyte) == 0xff);
    789  1.1       ad 
    790  1.1       ad 	splx(s);
    791  1.1       ad 	iop_msg_free(sc, NULL, im);
    792  1.1       ad 	return (*((volatile u_char *)&sc->sc_status.syncbyte) != 0xff);
    793  1.1       ad }
    794  1.1       ad 
    795  1.1       ad /*
    796  1.1       ad  * Initalize and populate the adapter's outbound FIFO.
    797  1.1       ad  */
    798  1.1       ad static int
    799  1.1       ad iop_ofifo_init(struct iop_softc *sc)
    800  1.1       ad {
    801  1.1       ad 	struct iop_msg *im;
    802  1.1       ad 	volatile u_int32_t status;
    803  1.1       ad 	bus_addr_t addr;
    804  1.5       ad 	bus_dma_segment_t seg;
    805  1.1       ad 	struct i2o_exec_outbound_init *mb;
    806  1.5       ad 	int i, rseg, rv;
    807  1.1       ad 
    808  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    809  1.1       ad 		return (rv);
    810  1.1       ad 
    811  1.1       ad 	mb = (struct i2o_exec_outbound_init *)im->im_msg;
    812  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_outbound_init);
    813  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_OUTBOUND_INIT);
    814  1.1       ad 	mb->msgictx = IOP_ICTX;
    815  1.1       ad 	mb->msgtctx = im->im_tctx;
    816  1.4  thorpej 	mb->pagesize = PAGE_SIZE;
    817  1.5       ad 	mb->flags = 0x80 | ((IOP_MAX_REPLY_SIZE >> 2) << 16);	/* XXX */
    818  1.1       ad 
    819  1.1       ad 	status = 0;
    820  1.5       ad 
    821  1.5       ad 	/*
    822  1.5       ad 	 * The I2O spec says that there are two SGLs: one for the status
    823  1.5       ad 	 * word, and one for a list of discarded MFAs.  It continues to say
    824  1.5       ad 	 * that if you don't want to get the list of MFAs, an IGNORE SGL is
    825  1.5       ad 	 * necessary; this isn't the case (and in fact appears to be a bad
    826  1.5       ad 	 * thing).
    827  1.5       ad 	 */
    828  1.1       ad 	iop_msg_map(sc, im, (void *)&status, sizeof(status), 0);
    829  1.1       ad 	if ((rv = iop_msg_send(sc, im, 0)) != 0) {
    830  1.1       ad 		iop_msg_free(sc, NULL, im);
    831  1.1       ad 		return (rv);
    832  1.1       ad 	}
    833  1.1       ad 	iop_msg_unmap(sc, im);
    834  1.1       ad 	iop_msg_free(sc, NULL, im);
    835  1.1       ad 
    836  1.5       ad 	/* XXX */
    837  1.5       ad 	POLL(5000, status == I2O_EXEC_OUTBOUND_INIT_COMPLETE);
    838  1.5       ad 	if (status != I2O_EXEC_OUTBOUND_INIT_COMPLETE) {
    839  1.5       ad 		printf("%s: outbound FIFO init failed\n", sc->sc_dv.dv_xname);
    840  1.5       ad 		return (EIO);
    841  1.1       ad 	}
    842  1.1       ad 
    843  1.1       ad 	/* If we need to allocate DMA safe memory, do it now. */
    844  1.1       ad 	if (sc->sc_rep_phys == 0) {
    845  1.5       ad 		sc->sc_rep_size = sc->sc_maxreplycnt * IOP_MAX_REPLY_SIZE;
    846  1.5       ad 
    847  1.5       ad 		rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_rep_size, PAGE_SIZE,
    848  1.5       ad 		    0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
    849  1.5       ad 		if (rv != 0) {
    850  1.5       ad 			printf("%s: dma alloc = %d\n", sc->sc_dv.dv_xname,
    851  1.5       ad 			   rv);
    852  1.5       ad 			return (rv);
    853  1.5       ad 		}
    854  1.5       ad 
    855  1.5       ad 		rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sc->sc_rep_size,
    856  1.5       ad 		    &sc->sc_rep, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    857  1.5       ad 		if (rv != 0) {
    858  1.5       ad 			printf("%s: dma map = %d\n", sc->sc_dv.dv_xname, rv);
    859  1.5       ad 			return (rv);
    860  1.5       ad 		}
    861  1.5       ad 
    862  1.5       ad 		rv = bus_dmamap_create(sc->sc_dmat, sc->sc_rep_size, 1,
    863  1.5       ad 		    sc->sc_rep_size, 0, BUS_DMA_NOWAIT, &sc->sc_rep_dmamap);
    864  1.5       ad 		if (rv != 0) {
    865  1.5       ad 			printf("%s: dma create = %d\n", sc->sc_dv.dv_xname, rv);
    866  1.5       ad 			return (rv);
    867  1.5       ad 		}
    868  1.5       ad 
    869  1.5       ad 		rv = bus_dmamap_load(sc->sc_dmat, sc->sc_rep_dmamap, sc->sc_rep,
    870  1.5       ad 		    sc->sc_rep_size, NULL, BUS_DMA_NOWAIT);
    871  1.5       ad 		if (rv != 0) {
    872  1.5       ad 			printf("%s: dma load = %d\n", sc->sc_dv.dv_xname, rv);
    873  1.5       ad 			return (rv);
    874  1.5       ad 		}
    875  1.5       ad 
    876  1.5       ad 		sc->sc_rep_phys = sc->sc_rep_dmamap->dm_segs[0].ds_addr;
    877  1.1       ad 	}
    878  1.1       ad 
    879  1.1       ad 	/* Populate the outbound FIFO. */
    880  1.5       ad 	for (i = sc->sc_maxreplycnt, addr = sc->sc_rep_phys; i != 0; i--) {
    881  1.5       ad 		iop_outl(sc, IOP_REG_OFIFO, (u_int32_t)addr);
    882  1.5       ad 		addr += IOP_MAX_REPLY_SIZE;
    883  1.1       ad 	}
    884  1.1       ad 
    885  1.1       ad 	return (0);
    886  1.1       ad }
    887  1.1       ad 
    888  1.1       ad /*
    889  1.1       ad  * Read the specified number of bytes from the IOP's hardware resource table.
    890  1.1       ad  */
    891  1.1       ad static int
    892  1.1       ad iop_hrt_get0(struct iop_softc *sc, struct i2o_hrt *hrt, int size)
    893  1.1       ad {
    894  1.1       ad 	struct iop_msg *im;
    895  1.1       ad 	int rv;
    896  1.1       ad 	struct i2o_exec_hrt_get *mb;
    897  1.1       ad 
    898  1.5       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
    899  1.1       ad 		return (rv);
    900  1.1       ad 
    901  1.1       ad 	mb = (struct i2o_exec_hrt_get *)im->im_msg;
    902  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_hrt_get);
    903  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_HRT_GET);
    904  1.1       ad 	mb->msgictx = IOP_ICTX;
    905  1.1       ad 	mb->msgtctx = im->im_tctx;
    906  1.1       ad 
    907  1.1       ad 	iop_msg_map(sc, im, hrt, size, 0);
    908  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
    909  1.1       ad 	iop_msg_unmap(sc, im);
    910  1.1       ad 	iop_msg_free(sc, NULL, im);
    911  1.1       ad 	return (rv);
    912  1.1       ad }
    913  1.1       ad 
    914  1.1       ad /*
    915  1.5       ad  * Read the IOP's hardware resource table.
    916  1.1       ad  */
    917  1.1       ad static int
    918  1.1       ad iop_hrt_get(struct iop_softc *sc)
    919  1.1       ad {
    920  1.1       ad 	struct i2o_hrt hrthdr, *hrt;
    921  1.1       ad 	int size, rv;
    922  1.1       ad 
    923  1.1       ad 	if ((rv = iop_hrt_get0(sc, &hrthdr, sizeof(hrthdr))) != 0)
    924  1.1       ad 		return (rv);
    925  1.1       ad 
    926  1.5       ad 	DPRINTF(("%s: %d hrt entries\n", sc->sc_dv.dv_xname,
    927  1.5       ad 	    le16toh(hrthdr.numentries)));
    928  1.5       ad 
    929  1.5       ad 	size = sizeof(struct i2o_hrt) +
    930  1.5       ad 	    (htole32(hrthdr.numentries) - 1) * sizeof(struct i2o_hrt_entry);
    931  1.1       ad 	hrt = (struct i2o_hrt *)malloc(size, M_DEVBUF, M_NOWAIT);
    932  1.1       ad 
    933  1.1       ad 	if ((rv = iop_hrt_get0(sc, hrt, size)) != 0) {
    934  1.1       ad 		free(hrt, M_DEVBUF);
    935  1.1       ad 		return (rv);
    936  1.1       ad 	}
    937  1.1       ad 
    938  1.1       ad 	if (sc->sc_hrt != NULL)
    939  1.1       ad 		free(sc->sc_hrt, M_DEVBUF);
    940  1.1       ad 	sc->sc_hrt = hrt;
    941  1.1       ad 	return (0);
    942  1.1       ad }
    943  1.1       ad 
    944  1.1       ad /*
    945  1.1       ad  * Request the specified number of bytes from the IOP's logical
    946  1.5       ad  * configuration table.  If a change indicator is specified, this
    947  1.5       ad  * is an verbatim notification request, so the caller is prepared
    948  1.5       ad  * to wait indefinitely.
    949  1.1       ad  */
    950  1.1       ad static int
    951  1.5       ad iop_lct_get0(struct iop_softc *sc, struct i2o_lct *lct, int size,
    952  1.5       ad 	     u_int32_t chgind)
    953  1.1       ad {
    954  1.1       ad 	struct iop_msg *im;
    955  1.1       ad 	struct i2o_exec_lct_notify *mb;
    956  1.1       ad 	int rv;
    957  1.1       ad 
    958  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
    959  1.1       ad 		return (rv);
    960  1.5       ad 
    961  1.1       ad 	memset(lct, 0, size);
    962  1.5       ad 	memset(im->im_msg, 0, sizeof(im->im_msg));
    963  1.1       ad 
    964  1.1       ad 	mb = (struct i2o_exec_lct_notify *)im->im_msg;
    965  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_lct_notify);
    966  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_LCT_NOTIFY);
    967  1.1       ad 	mb->msgictx = IOP_ICTX;
    968  1.1       ad 	mb->msgtctx = im->im_tctx;
    969  1.1       ad 	mb->classid = I2O_CLASS_ANY;
    970  1.5       ad 	mb->changeindicator = chgind;
    971  1.5       ad 
    972  1.5       ad 	DPRINTF(("iop_lct_get0: reading LCT\n"));
    973  1.1       ad 
    974  1.1       ad 	iop_msg_map(sc, im, lct, size, 0);
    975  1.5       ad 	rv = iop_msg_enqueue(sc, im, (chgind == 0 ? 120*1000 : 0));
    976  1.1       ad 	iop_msg_unmap(sc, im);
    977  1.1       ad 	iop_msg_free(sc, NULL, im);
    978  1.1       ad 	return (rv);
    979  1.1       ad }
    980  1.1       ad 
    981  1.1       ad /*
    982  1.6       ad  * Read the IOP's logical configuration table.
    983  1.1       ad  */
    984  1.1       ad int
    985  1.1       ad iop_lct_get(struct iop_softc *sc)
    986  1.1       ad {
    987  1.5       ad 	int esize, size, rv;
    988  1.5       ad 	struct i2o_lct *lct;
    989  1.1       ad 
    990  1.5       ad 	esize = le32toh(sc->sc_status.expectedlctsize);
    991  1.5       ad 	lct = (struct i2o_lct *)malloc(esize, M_DEVBUF, M_WAITOK);
    992  1.5       ad 	if (lct == NULL)
    993  1.1       ad 		return (ENOMEM);
    994  1.1       ad 
    995  1.5       ad 	if ((rv = iop_lct_get0(sc, lct, esize, 0)) != 0) {
    996  1.1       ad 		free(lct, M_DEVBUF);
    997  1.1       ad 		return (rv);
    998  1.1       ad 	}
    999  1.1       ad 
   1000  1.5       ad 	size = le16toh(lct->tablesize) << 2;
   1001  1.5       ad 	if (esize != size) {
   1002  1.1       ad 		free(lct, M_DEVBUF);
   1003  1.5       ad 		lct = (struct i2o_lct *)malloc(size, M_DEVBUF, M_WAITOK);
   1004  1.5       ad 		if (lct == NULL)
   1005  1.5       ad 			return (ENOMEM);
   1006  1.5       ad 
   1007  1.5       ad 		if ((rv = iop_lct_get0(sc, lct, size, 0)) != 0) {
   1008  1.5       ad 			free(lct, M_DEVBUF);
   1009  1.5       ad 			return (rv);
   1010  1.5       ad 		}
   1011  1.1       ad 	}
   1012  1.5       ad 
   1013  1.5       ad 	/* Swap in the new LCT. */
   1014  1.1       ad 	if (sc->sc_lct != NULL)
   1015  1.1       ad 		free(sc->sc_lct, M_DEVBUF);
   1016  1.1       ad 	sc->sc_lct = lct;
   1017  1.1       ad 	sc->sc_nlctent = ((le16toh(sc->sc_lct->tablesize) << 2) -
   1018  1.1       ad 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
   1019  1.1       ad 	    sizeof(struct i2o_lct_entry);
   1020  1.1       ad 	return (0);
   1021  1.1       ad }
   1022  1.1       ad 
   1023  1.1       ad /*
   1024  1.5       ad  * Request the specified parameter group from the target.
   1025  1.1       ad  */
   1026  1.1       ad int
   1027  1.5       ad iop_param_op(struct iop_softc *sc, int tid, int write, int group, void *buf,
   1028  1.5       ad 	     int size)
   1029  1.1       ad {
   1030  1.1       ad 	struct iop_msg *im;
   1031  1.5       ad 	struct i2o_util_params_op *mb;
   1032  1.5       ad 	int rv, func, op;
   1033  1.1       ad 	struct {
   1034  1.1       ad 		struct	i2o_param_op_list_header olh;
   1035  1.1       ad 		struct	i2o_param_op_all_template oat;
   1036  1.1       ad 	} req;
   1037  1.1       ad 
   1038  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
   1039  1.1       ad 		return (rv);
   1040  1.1       ad 
   1041  1.5       ad 	if (write) {
   1042  1.5       ad 		func = I2O_UTIL_PARAMS_SET;
   1043  1.5       ad 		op = I2O_PARAMS_OP_FIELD_SET;
   1044  1.5       ad 	} else {
   1045  1.5       ad 		func = I2O_UTIL_PARAMS_GET;
   1046  1.5       ad 		op = I2O_PARAMS_OP_FIELD_GET;
   1047  1.5       ad 	}
   1048  1.5       ad 
   1049  1.5       ad 	mb = (struct i2o_util_params_op *)im->im_msg;
   1050  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1051  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(tid, func);
   1052  1.1       ad 	mb->msgictx = IOP_ICTX;
   1053  1.1       ad 	mb->msgtctx = im->im_tctx;
   1054  1.1       ad 	mb->flags = 0;
   1055  1.1       ad 
   1056  1.1       ad 	req.olh.count = htole16(1);
   1057  1.1       ad 	req.olh.reserved = htole16(0);
   1058  1.5       ad 	req.oat.operation = htole16(op);
   1059  1.1       ad 	req.oat.fieldcount = htole16(0xffff);
   1060  1.1       ad 	req.oat.group = htole16(group);
   1061  1.1       ad 
   1062  1.5       ad 	memset(buf, 0, size);
   1063  1.1       ad 	iop_msg_map(sc, im, &req, sizeof(req), 1);
   1064  1.5       ad 	iop_msg_map(sc, im, buf, size, write);
   1065  1.1       ad 
   1066  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1067  1.1       ad 	iop_msg_unmap(sc, im);
   1068  1.1       ad 	iop_msg_free(sc, NULL, im);
   1069  1.1       ad 	return (rv);
   1070  1.1       ad }
   1071  1.1       ad 
   1072  1.1       ad /*
   1073  1.5       ad  * Execute a simple command (no parameters).
   1074  1.1       ad  */
   1075  1.1       ad int
   1076  1.5       ad iop_simple_cmd(struct iop_softc *sc, int tid, int function, int ictx,
   1077  1.5       ad 	       int async, int timo)
   1078  1.1       ad {
   1079  1.1       ad 	struct iop_msg *im;
   1080  1.1       ad 	struct i2o_msg *mb;
   1081  1.5       ad 	int rv, fl;
   1082  1.1       ad 
   1083  1.5       ad 	fl = (async != 0 ? IM_NOWAIT : 0);
   1084  1.5       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, fl | IM_NOINTR)) != 0)
   1085  1.1       ad 		return (rv);
   1086  1.1       ad 
   1087  1.1       ad 	mb = (struct i2o_msg *)im->im_msg;
   1088  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_msg);
   1089  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(tid, function);
   1090  1.1       ad 	mb->msgictx = ictx;
   1091  1.1       ad 	mb->msgtctx = im->im_tctx;
   1092  1.1       ad 
   1093  1.5       ad 	if (async)
   1094  1.5       ad 		rv = iop_msg_enqueue(sc, im, timo);
   1095  1.5       ad 	else
   1096  1.5       ad 		rv = iop_msg_send(sc, im, timo);
   1097  1.1       ad 	iop_msg_free(sc, NULL, im);
   1098  1.1       ad 	return (rv);
   1099  1.1       ad }
   1100  1.1       ad 
   1101  1.1       ad /*
   1102  1.5       ad  * Post the system table to the IOP.
   1103  1.1       ad  */
   1104  1.1       ad static int
   1105  1.1       ad iop_systab_set(struct iop_softc *sc)
   1106  1.1       ad {
   1107  1.5       ad 	struct i2o_exec_sys_tab_set *mb;
   1108  1.1       ad 	struct iop_msg *im;
   1109  1.1       ad 	u_int32_t mema[2], ioa[2];
   1110  1.1       ad 	int rv;
   1111  1.1       ad 
   1112  1.5       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
   1113  1.1       ad 		return (rv);
   1114  1.1       ad 
   1115  1.1       ad 	mb = (struct i2o_exec_sys_tab_set *)im->im_msg;
   1116  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_sys_tab_set);
   1117  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_TAB_SET);
   1118  1.1       ad 	mb->msgictx = IOP_ICTX;
   1119  1.1       ad 	mb->msgtctx = im->im_tctx;
   1120  1.5       ad 	mb->iopid = (sc->sc_dv.dv_unit + 2) << 12;
   1121  1.5       ad 	mb->segnumber = 0;
   1122  1.5       ad 
   1123  1.5       ad 	/* XXX This is questionable, but better than nothing... */
   1124  1.5       ad 	mema[0] = le32toh(sc->sc_status.currentprivmembase);
   1125  1.5       ad 	mema[1] = le32toh(sc->sc_status.currentprivmemsize);
   1126  1.5       ad 	ioa[0] = le32toh(sc->sc_status.currentpriviobase);
   1127  1.5       ad 	ioa[1] = le32toh(sc->sc_status.currentpriviosize);
   1128  1.1       ad 
   1129  1.5       ad 	iop_msg_map(sc, im, iop_systab, iop_systab_size, 1);
   1130  1.1       ad 	iop_msg_map(sc, im, mema, sizeof(mema), 1);
   1131  1.1       ad 	iop_msg_map(sc, im, ioa, sizeof(ioa), 1);
   1132  1.1       ad 
   1133  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1134  1.1       ad 	iop_msg_unmap(sc, im);
   1135  1.1       ad 	iop_msg_free(sc, NULL, im);
   1136  1.1       ad 	return (rv);
   1137  1.1       ad }
   1138  1.1       ad 
   1139  1.1       ad /*
   1140  1.1       ad  * Reset the adapter.  Must be called with interrupts disabled.
   1141  1.1       ad  */
   1142  1.1       ad static int
   1143  1.1       ad iop_reset(struct iop_softc *sc)
   1144  1.1       ad {
   1145  1.1       ad 	struct iop_msg *im;
   1146  1.1       ad 	volatile u_int32_t sw;
   1147  1.1       ad 	u_int32_t mfa;
   1148  1.1       ad 	struct i2o_exec_iop_reset *mb;
   1149  1.1       ad 	int rv;
   1150  1.1       ad 
   1151  1.1       ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
   1152  1.1       ad 		return (rv);
   1153  1.1       ad 
   1154  1.1       ad 	sw = 0;
   1155  1.1       ad 
   1156  1.1       ad 	mb = (struct i2o_exec_iop_reset *)im->im_msg;
   1157  1.1       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_iop_reset);
   1158  1.1       ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_IOP_RESET);
   1159  1.1       ad 	mb->reserved[0] = 0;
   1160  1.1       ad 	mb->reserved[1] = 0;
   1161  1.1       ad 	mb->reserved[2] = 0;
   1162  1.1       ad 	mb->reserved[3] = 0;
   1163  1.1       ad 	mb->statuslow = kvtop((caddr_t)&sw);		/* XXX */
   1164  1.1       ad 	mb->statushigh = 0;
   1165  1.1       ad 
   1166  1.1       ad 	if ((rv = iop_msg_send(sc, im, 0)))
   1167  1.1       ad 		return (rv);
   1168  1.1       ad 	iop_msg_free(sc, NULL, im);
   1169  1.1       ad 
   1170  1.1       ad 	POLL(2500, sw != 0);					/* XXX */
   1171  1.1       ad 	if (sw != I2O_RESET_IN_PROGRESS) {
   1172  1.1       ad 		printf("%s: reset rejected\n", sc->sc_dv.dv_xname);
   1173  1.1       ad 		return (EIO);
   1174  1.1       ad 	}
   1175  1.1       ad 
   1176  1.1       ad 	/*
   1177  1.5       ad 	 * IOP is now in the INIT state.  Wait no more than 10 seconds for
   1178  1.1       ad 	 * the inbound queue to become responsive.
   1179  1.1       ad 	 */
   1180  1.5       ad 	POLL(10000, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1181  1.1       ad 	if (mfa == IOP_MFA_EMPTY) {
   1182  1.1       ad 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
   1183  1.1       ad 		return (EIO);
   1184  1.1       ad 	}
   1185  1.1       ad 
   1186  1.5       ad 	if (sw == I2O_RESET_REJECTED)
   1187  1.5       ad 		printf("%s: reset rejected?\n", sc->sc_dv.dv_xname);
   1188  1.5       ad 
   1189  1.1       ad 	iop_release_mfa(sc, mfa);
   1190  1.1       ad 	return (0);
   1191  1.1       ad }
   1192  1.1       ad 
   1193  1.1       ad /*
   1194  1.1       ad  * Register a new initiator.
   1195  1.1       ad  */
   1196  1.1       ad int
   1197  1.1       ad iop_initiator_register(struct iop_softc *sc, struct iop_initiator *ii)
   1198  1.1       ad {
   1199  1.5       ad 	static int ictx;
   1200  1.5       ad 	static int stctx;
   1201  1.5       ad 
   1202  1.5       ad 	/* 0 is reserved for system messages. */
   1203  1.5       ad 	ii->ii_ictx = ++ictx;
   1204  1.5       ad 	ii->ii_stctx = ++stctx | 0x80000000;
   1205  1.1       ad 
   1206  1.5       ad 	LIST_INSERT_HEAD(&sc->sc_iilist, ii, ii_list);
   1207  1.5       ad 	LIST_INSERT_HEAD(IOP_ICTXHASH(ii->ii_ictx), ii, ii_hash);
   1208  1.1       ad 
   1209  1.1       ad 	return (0);
   1210  1.1       ad }
   1211  1.1       ad 
   1212  1.1       ad /*
   1213  1.1       ad  * Unregister an initiator.
   1214  1.1       ad  */
   1215  1.1       ad void
   1216  1.1       ad iop_initiator_unregister(struct iop_softc *sc, struct iop_initiator *ii)
   1217  1.1       ad {
   1218  1.1       ad 
   1219  1.5       ad 	LIST_REMOVE(ii, ii_list);
   1220  1.5       ad 	LIST_REMOVE(ii, ii_hash);
   1221  1.1       ad }
   1222  1.1       ad 
   1223  1.1       ad /*
   1224  1.5       ad  * Handle a reply frame from the adapter.
   1225  1.1       ad  */
   1226  1.1       ad static int
   1227  1.5       ad iop_handle_reply(struct iop_softc *sc, u_int32_t rmfa)
   1228  1.1       ad {
   1229  1.1       ad 	struct iop_msg *im;
   1230  1.1       ad 	struct i2o_reply *rb;
   1231  1.1       ad 	struct iop_initiator *ii;
   1232  1.5       ad 	u_int off, ictx, tctx, status, size;
   1233  1.1       ad 
   1234  1.1       ad 	off = (int)(rmfa - sc->sc_rep_phys);
   1235  1.1       ad 	rb = (struct i2o_reply *)(sc->sc_rep + off);
   1236  1.1       ad 
   1237  1.5       ad 	/* Perform reply queue DMA synchronisation... */
   1238  1.1       ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, off, IOP_MAX_MSG_SIZE,
   1239  1.1       ad 	    BUS_DMASYNC_POSTREAD);
   1240  1.1       ad 	if (--sc->sc_stat.is_cur_hwqueue != 0)
   1241  1.1       ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap,
   1242  1.1       ad 		    0, sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1243  1.1       ad 
   1244  1.1       ad #ifdef I2ODEBUG
   1245  1.1       ad 	if ((le32toh(rb->msgflags) & I2O_MSGFLAGS_64BIT) != 0)
   1246  1.5       ad 		panic("iop_handle_reply: 64-bit reply");
   1247  1.1       ad #endif
   1248  1.1       ad 	/*
   1249  1.1       ad 	 * Find the initiator.
   1250  1.1       ad 	 */
   1251  1.1       ad 	ictx = le32toh(rb->msgictx);
   1252  1.1       ad 	if (ictx == IOP_ICTX)
   1253  1.1       ad 		ii = NULL;
   1254  1.1       ad 	else {
   1255  1.5       ad 		ii = LIST_FIRST(IOP_ICTXHASH(ictx));
   1256  1.5       ad 		for (; ii != NULL; ii = LIST_NEXT(ii, ii_hash))
   1257  1.5       ad 			if (ii->ii_ictx == ictx)
   1258  1.5       ad 				break;
   1259  1.5       ad 		if (ii == NULL) {
   1260  1.1       ad #ifdef I2ODEBUG
   1261  1.5       ad 			iop_reply_print(sc, NULL, rb);
   1262  1.1       ad #endif
   1263  1.5       ad 			printf("%s: WARNING: bad ictx returned (%x)",
   1264  1.5       ad 			    sc->sc_dv.dv_xname, ictx);
   1265  1.5       ad 
   1266  1.5       ad 			/* Return the reply frame to the IOP's outbound FIFO. */
   1267  1.5       ad 			iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1268  1.5       ad 			return (-1);
   1269  1.5       ad 		}
   1270  1.1       ad 	}
   1271  1.1       ad 
   1272  1.1       ad 	status = rb->reqstatus;
   1273  1.1       ad 
   1274  1.1       ad 	if (ii == NULL || (ii->ii_flags & II_DISCARD) == 0) {
   1275  1.1       ad 		/*
   1276  1.1       ad 		 * This initiator tracks state using message wrappers.
   1277  1.1       ad 		 *
   1278  1.1       ad 		 * Find the originating message wrapper, and if requested
   1279  1.1       ad 		 * notify the initiator.
   1280  1.1       ad 		 */
   1281  1.1       ad 		tctx = le32toh(rb->msgtctx);
   1282  1.5       ad 		im = TAILQ_FIRST(IOP_TCTXHASH(tctx));
   1283  1.1       ad 		for (; im != NULL; im = TAILQ_NEXT(im, im_hash))
   1284  1.1       ad 			if (im->im_tctx == tctx)
   1285  1.1       ad 				break;
   1286  1.5       ad 		if (im == NULL || (im->im_flags & IM_ALLOCED) == 0) {
   1287  1.5       ad #ifdef I2ODEBUG
   1288  1.5       ad 			iop_reply_print(sc, NULL, rb);
   1289  1.5       ad #endif
   1290  1.5       ad 			printf("%s: WARNING: bad tctx returned (%x, %p)",
   1291  1.1       ad 			    sc->sc_dv.dv_xname, tctx, im);
   1292  1.5       ad 
   1293  1.5       ad 			/* Return the reply frame to the IOP's outbound FIFO. */
   1294  1.5       ad 			iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1295  1.5       ad 			return (-1);
   1296  1.5       ad 		}
   1297  1.1       ad #ifdef I2ODEBUG
   1298  1.1       ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1299  1.5       ad 			panic("%s: dup reply", sc->sc_dv.dv_xname);
   1300  1.1       ad #endif
   1301  1.1       ad 
   1302  1.1       ad 		im->im_flags |= IM_REPLIED;
   1303  1.1       ad 
   1304  1.1       ad #ifdef I2ODEBUG
   1305  1.1       ad 		if (rb->reqstatus != 0)
   1306  1.1       ad 			iop_reply_print(sc, im, rb);
   1307  1.1       ad #endif
   1308  1.1       ad 		/* Notify the initiator. */
   1309  1.5       ad 		if ((im->im_flags & IM_WAITING) != 0) {
   1310  1.5       ad 			size = (le32toh(rb->msgflags) >> 14) & ~3;
   1311  1.5       ad 			if (size > IOP_MAX_REPLY_SIZE)
   1312  1.5       ad 				size = IOP_MAX_REPLY_SIZE;
   1313  1.5       ad 			memcpy(im->im_msg, rb, size);
   1314  1.1       ad 			wakeup(im);
   1315  1.5       ad 		} else if ((im->im_flags & IM_NOINTR) == 0)
   1316  1.1       ad 			(*ii->ii_intr)(ii->ii_dv, im, rb);
   1317  1.1       ad 	} else {
   1318  1.1       ad 		/*
   1319  1.1       ad 		 * This initiator discards message wrappers.
   1320  1.1       ad 		 *
   1321  1.1       ad 		 * Simply pass the reply frame to the initiator.
   1322  1.1       ad 		 */
   1323  1.1       ad 		(*ii->ii_intr)(ii->ii_dv, NULL, rb);
   1324  1.1       ad 	}
   1325  1.1       ad 
   1326  1.1       ad 	/* Return the reply frame to the IOP's outbound FIFO. */
   1327  1.5       ad 	iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1328  1.1       ad 
   1329  1.1       ad 	/* Run the queue. */
   1330  1.1       ad 	if ((im = SIMPLEQ_FIRST(&sc->sc_queue)) != NULL)
   1331  1.5       ad 		iop_msg_enqueue(sc, im, 0);
   1332  1.1       ad 
   1333  1.1       ad 	return (status);
   1334  1.1       ad }
   1335  1.1       ad 
   1336  1.1       ad /*
   1337  1.1       ad  * Handle an interrupt from the adapter.
   1338  1.1       ad  */
   1339  1.1       ad int
   1340  1.1       ad iop_intr(void *arg)
   1341  1.1       ad {
   1342  1.1       ad 	struct iop_softc *sc;
   1343  1.5       ad 	u_int32_t rmfa;
   1344  1.1       ad 
   1345  1.1       ad 	sc = arg;
   1346  1.1       ad 
   1347  1.5       ad 	if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) == 0)
   1348  1.5       ad 		return (0);
   1349  1.5       ad 
   1350  1.5       ad 	for (;;) {
   1351  1.5       ad 		/* Double read to account for IOP bug. */
   1352  1.5       ad 		if ((rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY &&
   1353  1.5       ad 		    (rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY)
   1354  1.5       ad 			break;
   1355  1.5       ad 		iop_handle_reply(sc, rmfa);
   1356  1.1       ad 	}
   1357  1.1       ad 
   1358  1.5       ad 	return (1);
   1359  1.5       ad }
   1360  1.5       ad 
   1361  1.5       ad /*
   1362  1.5       ad  * Handle an event signalled by the executive.
   1363  1.5       ad  */
   1364  1.5       ad static void
   1365  1.5       ad iop_intr_event(struct device *dv, struct iop_msg *im, void *reply)
   1366  1.5       ad {
   1367  1.5       ad 	struct i2o_util_event_register_reply *rb;
   1368  1.5       ad 	struct iop_softc *sc;
   1369  1.5       ad 	u_int event;
   1370  1.5       ad 
   1371  1.5       ad 	sc = (struct iop_softc *)dv;
   1372  1.5       ad 	rb = reply;
   1373  1.5       ad 	event = le32toh(rb->event);
   1374  1.5       ad 
   1375  1.5       ad #ifndef I2ODEBUG
   1376  1.5       ad 	if (event == I2O_EVENT_GEN_EVENT_MASK_MODIFIED)
   1377  1.5       ad 		return;
   1378  1.1       ad #endif
   1379  1.5       ad 
   1380  1.5       ad 	printf("%s: event 0x%08x received\n", dv->dv_xname, event);
   1381  1.1       ad }
   1382  1.1       ad 
   1383  1.1       ad /*
   1384  1.1       ad  * Allocate a message wrapper.
   1385  1.1       ad  */
   1386  1.1       ad int
   1387  1.1       ad iop_msg_alloc(struct iop_softc *sc, struct iop_initiator *ii,
   1388  1.1       ad 	      struct iop_msg **imp, int flags)
   1389  1.1       ad {
   1390  1.1       ad 	struct iop_msg *im;
   1391  1.5       ad 	static int tctxgen = 666;
   1392  1.5       ad 	int s, rv, i, tctx;
   1393  1.1       ad 
   1394  1.1       ad #ifdef I2ODEBUG
   1395  1.1       ad 	if ((flags & IM_SYSMASK) != 0)
   1396  1.1       ad 		panic("iop_msg_alloc: system flags specified");
   1397  1.1       ad #endif
   1398  1.1       ad 
   1399  1.1       ad 	s = splbio();	/* XXX */
   1400  1.1       ad 
   1401  1.5       ad 	if (ii != NULL && (ii->ii_flags & II_DISCARD) != 0) {
   1402  1.5       ad 		flags |= IM_DISCARD;
   1403  1.5       ad 		tctx = ii->ii_stctx;
   1404  1.5       ad 	} else
   1405  1.5       ad 		tctx = tctxgen++ & 0x7fffffff;
   1406  1.1       ad 
   1407  1.1       ad 	im = (struct iop_msg *)pool_get(iop_msgpool,
   1408  1.1       ad 	    (flags & IM_NOWAIT) == 0 ? PR_WAITOK : 0);
   1409  1.1       ad 	if (im == NULL) {
   1410  1.1       ad 		splx(s);
   1411  1.1       ad 		return (ENOMEM);
   1412  1.1       ad 	}
   1413  1.1       ad 
   1414  1.1       ad 	/* XXX */
   1415  1.1       ad 	rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER, IOP_MAX_SGL_ENTRIES,
   1416  1.1       ad 	    IOP_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1417  1.1       ad 	    &im->im_xfer[0].ix_map);
   1418  1.1       ad 	if (rv != 0) {
   1419  1.1       ad 		pool_put(iop_msgpool, im);
   1420  1.1       ad 		splx(s);
   1421  1.1       ad 		return (rv);
   1422  1.1       ad 	}
   1423  1.1       ad 
   1424  1.1       ad 	if ((flags & (IM_DISCARD | IM_NOICTX)) == 0)
   1425  1.5       ad 		TAILQ_INSERT_TAIL(IOP_TCTXHASH(tctx), im, im_hash);
   1426  1.1       ad 
   1427  1.1       ad 	splx(s);
   1428  1.1       ad 
   1429  1.5       ad 	im->im_tctx = tctx;
   1430  1.1       ad 	im->im_flags = flags | IM_ALLOCED;
   1431  1.1       ad 	for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1432  1.1       ad 		im->im_xfer[i].ix_size = 0;
   1433  1.1       ad 	*imp = im;
   1434  1.1       ad 
   1435  1.1       ad 	return (0);
   1436  1.1       ad }
   1437  1.1       ad 
   1438  1.1       ad /*
   1439  1.1       ad  * Free a message wrapper.
   1440  1.1       ad  */
   1441  1.1       ad void
   1442  1.1       ad iop_msg_free(struct iop_softc *sc, struct iop_initiator *ii, struct iop_msg *im)
   1443  1.1       ad {
   1444  1.1       ad 	int s;
   1445  1.1       ad 
   1446  1.1       ad #ifdef I2ODEBUG
   1447  1.1       ad 	if ((im->im_flags & IM_ALLOCED) == 0)
   1448  1.1       ad 		panic("iop_msg_free: wrapper not allocated");
   1449  1.1       ad #endif
   1450  1.1       ad 
   1451  1.1       ad 	/* XXX */
   1452  1.1       ad 	bus_dmamap_destroy(sc->sc_dmat, im->im_xfer[0].ix_map);
   1453  1.1       ad 
   1454  1.1       ad 	s = splbio();	/* XXX */
   1455  1.1       ad 
   1456  1.1       ad 	if ((im->im_flags & (IM_DISCARD | IM_NOICTX)) == 0)
   1457  1.5       ad 		TAILQ_REMOVE(IOP_TCTXHASH(im->im_tctx), im, im_hash);
   1458  1.1       ad 
   1459  1.1       ad 	im->im_flags = 0;
   1460  1.1       ad 	pool_put(iop_msgpool, im);
   1461  1.1       ad 	splx(s);
   1462  1.1       ad }
   1463  1.1       ad 
   1464  1.1       ad /*
   1465  1.5       ad  * Map a data transfer.  Write a scatter-gather list into the message frame.
   1466  1.1       ad  */
   1467  1.1       ad int
   1468  1.1       ad iop_msg_map(struct iop_softc *sc, struct iop_msg *im, void *xferaddr,
   1469  1.1       ad 	    int xfersize, int out)
   1470  1.1       ad {
   1471  1.1       ad 	struct iop_xfer *ix;
   1472  1.1       ad 	u_int32_t *mb;
   1473  1.5       ad 	int rv, seg, i;
   1474  1.5       ad 
   1475  1.1       ad 	for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++)
   1476  1.1       ad 		if (ix->ix_size == 0)
   1477  1.1       ad 			break;
   1478  1.1       ad #ifdef I2ODEBUG
   1479  1.1       ad 	if (i == IOP_MAX_MSG_XFERS)
   1480  1.1       ad 		panic("iop_msg_map: too many xfers");
   1481  1.1       ad #endif
   1482  1.1       ad 
   1483  1.1       ad 	/* Only the first DMA map is static. */
   1484  1.1       ad 	if (i != 0) {
   1485  1.1       ad 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
   1486  1.1       ad 		    IOP_MAX_SGL_ENTRIES, IOP_MAX_XFER, 0,
   1487  1.1       ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ix->ix_map);
   1488  1.1       ad 		if (rv != 0)
   1489  1.1       ad 			return (rv);
   1490  1.1       ad 	}
   1491  1.1       ad 
   1492  1.5       ad 	ix->ix_flags = (out ? IX_OUT : IX_IN);
   1493  1.1       ad 	ix->ix_size = xfersize;
   1494  1.1       ad 
   1495  1.1       ad 	rv = bus_dmamap_load(sc->sc_dmat, ix->ix_map, xferaddr, xfersize,
   1496  1.1       ad 	    NULL, 0);
   1497  1.1       ad 	if (rv != 0)
   1498  1.1       ad 		return (rv);
   1499  1.1       ad 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   1500  1.1       ad 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   1501  1.1       ad 
   1502  1.1       ad 	mb = im->im_msg + (im->im_msg[0] >> 16);
   1503  1.1       ad 	if (out)
   1504  1.1       ad 		out = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   1505  1.1       ad 	else
   1506  1.1       ad 		out = I2O_SGL_SIMPLE;
   1507  1.1       ad 
   1508  1.1       ad 	for (seg = 0; seg < ix->ix_map->dm_nsegs; seg++) {
   1509  1.1       ad #ifdef I2ODEBUG
   1510  1.1       ad 		if ((seg << 1) + (im->im_msg[0] >> 16) >=
   1511  1.1       ad 		    (IOP_MAX_MSG_SIZE >> 2))
   1512  1.1       ad 			panic("iop_map_xfer: message frame too large");
   1513  1.1       ad #endif
   1514  1.1       ad 		if (seg == ix->ix_map->dm_nsegs - 1)
   1515  1.1       ad 			out |= I2O_SGL_END_BUFFER;
   1516  1.1       ad 		*mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_len | out;
   1517  1.1       ad 		*mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_addr;
   1518  1.1       ad 	}
   1519  1.1       ad 
   1520  1.1       ad 	/*
   1521  1.1       ad 	 * If this is the first xfer we've mapped for this message, adjust
   1522  1.1       ad 	 * the SGL offset field in the message header.
   1523  1.1       ad 	 */
   1524  1.2       ad 	if ((im->im_flags & IM_SGLOFFADJ) == 0) {
   1525  1.1       ad 		im->im_msg[0] += ((im->im_msg[0] >> 16) + seg * 2) << 4;
   1526  1.2       ad 		im->im_flags |= IM_SGLOFFADJ;
   1527  1.2       ad 	}
   1528  1.1       ad 	im->im_msg[0] += (seg << 17);
   1529  1.1       ad 	return (0);
   1530  1.1       ad }
   1531  1.1       ad 
   1532  1.1       ad /*
   1533  1.1       ad  * Unmap all data transfers associated with a message wrapper.
   1534  1.1       ad  */
   1535  1.1       ad void
   1536  1.1       ad iop_msg_unmap(struct iop_softc *sc, struct iop_msg *im)
   1537  1.1       ad {
   1538  1.1       ad 	struct iop_xfer *ix;
   1539  1.1       ad 	int i;
   1540  1.1       ad 
   1541  1.1       ad 	for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++) {
   1542  1.1       ad 		if (ix->ix_size == 0)
   1543  1.2       ad 			break;
   1544  1.1       ad 		bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, ix->ix_size,
   1545  1.1       ad 		    ix->ix_flags & IX_OUT ? BUS_DMASYNC_POSTWRITE :
   1546  1.1       ad 		    BUS_DMASYNC_POSTREAD);
   1547  1.1       ad 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   1548  1.1       ad 
   1549  1.1       ad 		/* Only the first DMA map is static. */
   1550  1.1       ad 		if (i != 0)
   1551  1.1       ad 			bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   1552  1.1       ad 
   1553  1.1       ad 		ix->ix_size = 0;
   1554  1.1       ad 	}
   1555  1.1       ad }
   1556  1.1       ad 
   1557  1.1       ad /*
   1558  1.1       ad  * Send a message to the IOP.  Optionally, poll on completion.  Return
   1559  1.1       ad  * non-zero if failure status is returned and IM_NOINTR is set.
   1560  1.1       ad  */
   1561  1.1       ad int
   1562  1.1       ad iop_msg_send(struct iop_softc *sc, struct iop_msg *im, int timo)
   1563  1.1       ad {
   1564  1.5       ad 	u_int32_t mfa, rmfa;
   1565  1.2       ad 	int rv, status, i, s;
   1566  1.1       ad 
   1567  1.1       ad #ifdef I2ODEBUG
   1568  1.1       ad 	if ((im->im_flags & IM_NOICTX) == 0)
   1569  1.1       ad 		if (im->im_msg[3] == IOP_ICTX &&
   1570  1.1       ad 		    (im->im_flags & IM_NOINTR) == 0)
   1571  1.1       ad 			panic("iop_msg_send: IOP_ICTX and !IM_NOINTR");
   1572  1.1       ad 	if ((im->im_flags & IM_DISCARD) != 0)
   1573  1.1       ad 		panic("iop_msg_send: IM_DISCARD");
   1574  1.1       ad #endif
   1575  1.1       ad 
   1576  1.2       ad 	s = splbio();	/* XXX */
   1577  1.1       ad 
   1578  1.1       ad 	/* Wait up to 250ms for an MFA. */
   1579  1.5       ad 	POLL(250, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1580  1.1       ad 	if (mfa == IOP_MFA_EMPTY) {
   1581  1.1       ad 		DPRINTF(("%s: mfa not forthcoming\n", sc->sc_dv.dv_xname));
   1582  1.2       ad 		splx(s);
   1583  1.1       ad 		return (EBUSY);
   1584  1.1       ad 	}
   1585  1.1       ad 
   1586  1.1       ad 	/* Perform reply queue DMA synchronisation and update counters. */
   1587  1.1       ad 	if ((im->im_flags & IM_NOICTX) == 0) {
   1588  1.1       ad 		if (sc->sc_stat.is_cur_hwqueue == 0)
   1589  1.1       ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   1590  1.1       ad 			    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1591  1.1       ad 		for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1592  1.1       ad 			sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
   1593  1.1       ad 		sc->sc_stat.is_requests++;
   1594  1.1       ad 		if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
   1595  1.1       ad 			sc->sc_stat.is_peak_hwqueue =
   1596  1.1       ad 			    sc->sc_stat.is_cur_hwqueue;
   1597  1.1       ad 	}
   1598  1.1       ad 
   1599  1.1       ad 	/* Terminate scatter/gather lists. */
   1600  1.1       ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1601  1.1       ad 		im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
   1602  1.1       ad 
   1603  1.1       ad 	/* Post the message frame. */
   1604  1.1       ad 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
   1605  1.1       ad 	    im->im_msg, im->im_msg[0] >> 16);
   1606  1.5       ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa,
   1607  1.5       ad 	    (im->im_msg[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
   1608  1.1       ad 
   1609  1.1       ad 	/* Post the MFA back to the IOP, thus starting the command. */
   1610  1.5       ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   1611  1.1       ad 
   1612  1.1       ad 	if (timo == 0) {
   1613  1.2       ad 		splx(s);
   1614  1.1       ad 		return (0);
   1615  1.1       ad 	}
   1616  1.1       ad 
   1617  1.1       ad 	/* Wait for completion. */
   1618  1.1       ad 	for (timo *= 10; timo != 0; timo--) {
   1619  1.5       ad 		if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0) {
   1620  1.5       ad 			/* Double read to account for IOP bug. */
   1621  1.5       ad 			rmfa = iop_inl(sc, IOP_REG_OFIFO);
   1622  1.5       ad 			if (rmfa == IOP_MFA_EMPTY)
   1623  1.5       ad 				rmfa = iop_inl(sc, IOP_REG_OFIFO);
   1624  1.5       ad 			if (rmfa != IOP_MFA_EMPTY)
   1625  1.5       ad 				status = iop_handle_reply(sc, rmfa);
   1626  1.5       ad 		}
   1627  1.1       ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1628  1.1       ad 			break;
   1629  1.1       ad 		DELAY(100);
   1630  1.1       ad 	}
   1631  1.1       ad 
   1632  1.2       ad 	splx(s);
   1633  1.1       ad 
   1634  1.1       ad 	if (timo == 0) {
   1635  1.5       ad #ifdef I2ODEBUG
   1636  1.5       ad 		printf("%s: poll - no reply\n", sc->sc_dv.dv_xname);
   1637  1.5       ad 		if (iop_status_get(sc) != 0)
   1638  1.5       ad 			printf("iop_msg_send: unable to retrieve status\n");
   1639  1.5       ad 		else
   1640  1.5       ad 			printf("iop_msg_send: IOP state = %d\n",
   1641  1.5       ad 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   1642  1.5       ad #endif
   1643  1.1       ad 		rv = EBUSY;
   1644  1.1       ad 	} else if ((im->im_flags & IM_NOINTR) != 0)
   1645  1.1       ad 		rv = (status != I2O_STATUS_SUCCESS ? EIO : 0);
   1646  1.1       ad 
   1647  1.1       ad 	return (rv);
   1648  1.1       ad }
   1649  1.1       ad 
   1650  1.1       ad /*
   1651  1.1       ad  * Try to post a message to the adapter; if that's not possible, enqueue it
   1652  1.5       ad  * with us.  If a timeout is specified, wait for the message to complete.
   1653  1.1       ad  */
   1654  1.1       ad int
   1655  1.5       ad iop_msg_enqueue(struct iop_softc *sc, struct iop_msg *im, int timo)
   1656  1.1       ad {
   1657  1.1       ad 	u_int mfa;
   1658  1.5       ad 	int s, fromqueue, i, rv;
   1659  1.1       ad 
   1660  1.1       ad #ifdef I2ODEBUG
   1661  1.1       ad 	if (im == NULL)
   1662  1.1       ad 		panic("iop_msg_enqueue: im == NULL");
   1663  1.1       ad 	if (sc == NULL)
   1664  1.1       ad 		panic("iop_msg_enqueue: sc == NULL");
   1665  1.1       ad 	if ((im->im_flags & IM_NOICTX) != 0)
   1666  1.1       ad 		panic("iop_msg_enqueue: IM_NOICTX");
   1667  1.1       ad 	if (im->im_msg[3] == IOP_ICTX && (im->im_flags & IM_NOINTR) == 0)
   1668  1.5       ad 		panic("iop_msg_enqueue: IOP_ICTX and no IM_NOINTR");
   1669  1.5       ad 	if ((im->im_flags & IM_DISCARD) != 0 && timo != 0)
   1670  1.5       ad 		panic("iop_msg_enqueue: IM_DISCARD && timo != 0");
   1671  1.5       ad 	if ((im->im_flags & IM_NOINTR) == 0 && timo != 0)
   1672  1.5       ad 		panic("iop_msg_enqueue: !IM_NOINTR && timo != 0");
   1673  1.1       ad #endif
   1674  1.1       ad 
   1675  1.1       ad 	s = splbio();	/* XXX */
   1676  1.1       ad 	fromqueue = (im == SIMPLEQ_FIRST(&sc->sc_queue));
   1677  1.1       ad 
   1678  1.1       ad 	if (sc->sc_stat.is_cur_hwqueue >= sc->sc_maxqueuecnt) {
   1679  1.1       ad 		/*
   1680  1.1       ad 		 * While the IOP may be able to accept more inbound message
   1681  1.1       ad 		 * frames than it advertises, don't push harder than it
   1682  1.1       ad 		 * wants to go lest we starve it.
   1683  1.1       ad 		 *
   1684  1.1       ad 		 * XXX We should be handling IOP resource shortages.
   1685  1.1       ad 		 */
   1686  1.5       ad 		mfa = IOP_MFA_EMPTY;
   1687  1.5       ad 		DPRINTF(("iop_msg_enqueue: exceeded max queue count\n"));
   1688  1.1       ad 	} else {
   1689  1.1       ad 		/* Double read to account for IOP bug. */
   1690  1.5       ad 		if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY)
   1691  1.5       ad 			mfa = iop_inl(sc, IOP_REG_IFIFO);
   1692  1.1       ad 	}
   1693  1.1       ad 
   1694  1.1       ad 	if (mfa == IOP_MFA_EMPTY) {
   1695  1.5       ad 		DPRINTF(("iop_msg_enqueue: no mfa\n"));
   1696  1.1       ad 		/* Can't transfer to h/w queue - queue with us. */
   1697  1.1       ad 		if (!fromqueue) {
   1698  1.1       ad 			SIMPLEQ_INSERT_TAIL(&sc->sc_queue, im, im_queue);
   1699  1.1       ad 			if (++sc->sc_stat.is_cur_swqueue >
   1700  1.1       ad 			    sc->sc_stat.is_peak_swqueue)
   1701  1.1       ad 				sc->sc_stat.is_peak_swqueue =
   1702  1.1       ad 				    sc->sc_stat.is_cur_swqueue;
   1703  1.1       ad 		}
   1704  1.1       ad 		splx(s);
   1705  1.5       ad 		if ((im->im_flags & IM_NOINTR) != 0)
   1706  1.5       ad 			rv = iop_msg_wait(sc, im, timo);
   1707  1.5       ad 		else
   1708  1.5       ad 			rv = 0;
   1709  1.5       ad 		return (rv);
   1710  1.1       ad 	} else if (fromqueue) {
   1711  1.1       ad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, im, im_queue);
   1712  1.1       ad 		sc->sc_stat.is_cur_swqueue--;
   1713  1.1       ad 	}
   1714  1.1       ad 
   1715  1.5       ad 	if ((im->im_flags & IM_NOINTR) != 0)
   1716  1.5       ad 		im->im_flags |= IM_WAITING;
   1717  1.5       ad 
   1718  1.1       ad 	/* Perform reply queue DMA synchronisation and update counters. */
   1719  1.1       ad 	if (sc->sc_stat.is_cur_hwqueue == 0)
   1720  1.1       ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   1721  1.1       ad 		    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1722  1.1       ad 
   1723  1.1       ad 	for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1724  1.1       ad 		sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
   1725  1.1       ad 	sc->sc_stat.is_requests++;
   1726  1.1       ad 	if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
   1727  1.1       ad 		sc->sc_stat.is_peak_hwqueue = sc->sc_stat.is_cur_hwqueue;
   1728  1.1       ad 
   1729  1.1       ad 	/* Terminate the scatter/gather list. */
   1730  1.1       ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1731  1.1       ad 		im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
   1732  1.1       ad 
   1733  1.1       ad 	/* Post the message frame. */
   1734  1.1       ad 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
   1735  1.1       ad 	    im->im_msg, im->im_msg[0] >> 16);
   1736  1.5       ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, mfa,
   1737  1.5       ad 	    (im->im_msg[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
   1738  1.1       ad 
   1739  1.1       ad 	/* Post the MFA back to the IOP, thus starting the command. */
   1740  1.5       ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   1741  1.1       ad 
   1742  1.1       ad 	/* If this is a discardable message wrapper, free it. */
   1743  1.1       ad 	if ((im->im_flags & IM_DISCARD) != 0)
   1744  1.1       ad 		iop_msg_free(sc, NULL, im);
   1745  1.1       ad 	splx(s);
   1746  1.5       ad 
   1747  1.5       ad 	if ((im->im_flags & IM_NOINTR) != 0)
   1748  1.5       ad 		rv = iop_msg_wait(sc, im, timo);
   1749  1.5       ad 	else
   1750  1.5       ad 		rv = 0;
   1751  1.5       ad 	return (rv);
   1752  1.1       ad }
   1753  1.1       ad 
   1754  1.1       ad /*
   1755  1.5       ad  * Wait for the specified message to complete.
   1756  1.1       ad  */
   1757  1.5       ad static int
   1758  1.1       ad iop_msg_wait(struct iop_softc *sc, struct iop_msg *im, int timo)
   1759  1.1       ad {
   1760  1.5       ad 	struct i2o_reply *rb;
   1761  1.5       ad 	int rv, s;
   1762  1.1       ad 
   1763  1.5       ad 	s = splbio();
   1764  1.5       ad 	if ((im->im_flags & IM_REPLIED) != 0) {
   1765  1.5       ad 		splx(s);
   1766  1.1       ad 		return (0);
   1767  1.5       ad 	}
   1768  1.5       ad 	rv = tsleep(im, PRIBIO, "iopmsg", timo * hz / 1000);
   1769  1.5       ad 	splx(s);
   1770  1.5       ad #ifdef I2ODEBUG
   1771  1.5       ad 	if (rv != 0) {
   1772  1.5       ad 		printf("iop_msg_wait: tsleep() == %d\n", rv);
   1773  1.5       ad 		if (iop_status_get(sc) != 0)
   1774  1.5       ad 			printf("iop_msg_wait: unable to retrieve status\n");
   1775  1.5       ad 		else
   1776  1.5       ad 			printf("iop_msg_wait: IOP state = %d\n",
   1777  1.5       ad 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   1778  1.5       ad 	}
   1779  1.5       ad #endif
   1780  1.5       ad 	if ((im->im_flags & (IM_REPLIED | IM_NOSTATUS)) == IM_REPLIED) {
   1781  1.5       ad 		rb = (struct i2o_reply *)im->im_msg;
   1782  1.5       ad 		rv = (rb->reqstatus != I2O_STATUS_SUCCESS ? EIO : 0);
   1783  1.5       ad 	}
   1784  1.1       ad 	return (rv);
   1785  1.1       ad }
   1786  1.1       ad 
   1787  1.1       ad /*
   1788  1.1       ad  * Release an unused message frame back to the IOP's inbound fifo.
   1789  1.1       ad  */
   1790  1.1       ad static void
   1791  1.1       ad iop_release_mfa(struct iop_softc *sc, u_int32_t mfa)
   1792  1.1       ad {
   1793  1.1       ad 
   1794  1.1       ad 	/* Use the frame to issue a no-op. */
   1795  1.5       ad 	iop_outl(sc, mfa, I2O_VERSION_11 | (4 << 16));
   1796  1.5       ad 	iop_outl(sc, mfa + 4, I2O_MSGFUNC(I2O_TID_IOP, I2O_UTIL_NOP));
   1797  1.5       ad 	iop_outl(sc, mfa + 8, 0);
   1798  1.5       ad 	iop_outl(sc, mfa + 12, 0);
   1799  1.1       ad 
   1800  1.5       ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   1801  1.1       ad }
   1802  1.1       ad 
   1803  1.1       ad #ifdef I2ODEBUG
   1804  1.1       ad /*
   1805  1.1       ad  * Print status information from a failure reply frame.
   1806  1.1       ad  */
   1807  1.1       ad static void
   1808  1.1       ad iop_reply_print(struct iop_softc *sc, struct iop_msg *im,
   1809  1.1       ad 		struct i2o_reply *rb)
   1810  1.1       ad {
   1811  1.5       ad 	u_int function, detail;
   1812  1.1       ad #ifdef I2OVERBOSE
   1813  1.1       ad 	const char *statusstr;
   1814  1.1       ad #endif
   1815  1.1       ad 
   1816  1.5       ad 	if (im != NULL && (im->im_flags & IM_REPLIED) == 0)
   1817  1.1       ad 		panic("iop_msg_print_status: %p not replied to", im);
   1818  1.1       ad 
   1819  1.5       ad 	function = (le32toh(rb->msgfunc) >> 24) & 0xff;
   1820  1.1       ad 	detail = le16toh(rb->detail);
   1821  1.1       ad 
   1822  1.5       ad 	printf("%s: reply:\n", sc->sc_dv.dv_xname);
   1823  1.5       ad 
   1824  1.1       ad #ifdef I2OVERBOSE
   1825  1.1       ad 	if (rb->reqstatus < sizeof(iop_status) / sizeof(iop_status[0]))
   1826  1.1       ad 		statusstr = iop_status[rb->reqstatus];
   1827  1.1       ad 	else
   1828  1.1       ad 		statusstr = "undefined error code";
   1829  1.1       ad 
   1830  1.5       ad 	printf("%s:   function=0x%02x status=0x%02x (%s)\n",
   1831  1.5       ad 	    sc->sc_dv.dv_xname, function, rb->reqstatus, statusstr);
   1832  1.1       ad #else
   1833  1.5       ad 	printf("%s:   function=0x%02x status=0x%02x\n",
   1834  1.5       ad 	    sc->sc_dv.dv_xname, function, rb->reqstatus);
   1835  1.1       ad #endif
   1836  1.5       ad 	printf("%s:   detail=0x%04x ictx=0x%08x tctx=0x%08x\n",
   1837  1.5       ad 	    sc->sc_dv.dv_xname, detail, le32toh(rb->msgictx),
   1838  1.5       ad 	    le32toh(rb->msgtctx));
   1839  1.5       ad 	printf("%s:   tidi=%d tidt=%d flags=0x%02x\n", sc->sc_dv.dv_xname,
   1840  1.5       ad 	    (le32toh(rb->msgfunc) >> 12) & 4095, le32toh(rb->msgfunc) & 4095,
   1841  1.5       ad 	    (le32toh(rb->msgflags) >> 8) & 0xff);
   1842  1.1       ad }
   1843  1.1       ad #endif
   1844  1.1       ad 
   1845  1.1       ad /*
   1846  1.5       ad  * Translate an I2O ASCII field into a C string.
   1847  1.1       ad  */
   1848  1.1       ad void
   1849  1.5       ad iop_strvis(struct iop_softc *sc, const char *src, int slen, char *dst, int dlen)
   1850  1.1       ad {
   1851  1.5       ad 	int hc, lc, i, nit;
   1852  1.1       ad 
   1853  1.1       ad 	dlen--;
   1854  1.1       ad 	lc = 0;
   1855  1.1       ad 	hc = 0;
   1856  1.1       ad 	i = 0;
   1857  1.5       ad 
   1858  1.5       ad 	/*
   1859  1.5       ad 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
   1860  1.5       ad 	 * spec has nothing to say about it.  Since AMI fields are usually
   1861  1.5       ad 	 * filled with junk after the terminator, ...
   1862  1.5       ad 	 */
   1863  1.5       ad 	nit = (le16toh(sc->sc_status.orgid) != I2O_ORG_DPT);
   1864  1.5       ad 
   1865  1.5       ad 	while (slen-- != 0 && dlen-- != 0) {
   1866  1.5       ad 		if (nit && *src == '\0')
   1867  1.5       ad 			break;
   1868  1.5       ad 		else if (*src <= 0x20 || *src >= 0x7f) {
   1869  1.1       ad 			if (hc)
   1870  1.1       ad 				dst[i++] = ' ';
   1871  1.1       ad 		} else {
   1872  1.1       ad 			hc = 1;
   1873  1.1       ad 			dst[i++] = *src;
   1874  1.1       ad 			lc = i;
   1875  1.1       ad 		}
   1876  1.1       ad 		src++;
   1877  1.1       ad 	}
   1878  1.1       ad 
   1879  1.1       ad 	dst[lc] = '\0';
   1880  1.1       ad }
   1881  1.1       ad 
   1882  1.1       ad /*
   1883  1.5       ad  * Claim or unclaim the specified TID.
   1884  1.1       ad  */
   1885  1.1       ad int
   1886  1.5       ad iop_util_claim(struct iop_softc *sc, struct iop_initiator *ii, int release,
   1887  1.5       ad 	      int flags)
   1888  1.1       ad {
   1889  1.5       ad 	struct iop_msg *im;
   1890  1.5       ad 	struct i2o_util_claim *mb;
   1891  1.5       ad 	int rv, func;
   1892  1.5       ad 
   1893  1.5       ad 	func = release ? I2O_UTIL_CLAIM_RELEASE : I2O_UTIL_CLAIM;
   1894  1.5       ad 
   1895  1.5       ad 	if ((rv = iop_msg_alloc(sc, ii, &im, IM_NOINTR)) != 0)
   1896  1.5       ad 		return (rv);
   1897  1.5       ad 
   1898  1.5       ad 	/* We can use the same structure, as both are identical. */
   1899  1.5       ad 	mb = (struct i2o_util_claim *)im->im_msg;
   1900  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_claim);
   1901  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, func);
   1902  1.5       ad 	mb->msgictx = ii->ii_ictx;
   1903  1.5       ad 	mb->msgtctx = im->im_tctx;
   1904  1.5       ad 	mb->flags = flags;
   1905  1.5       ad 
   1906  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1907  1.5       ad 	iop_msg_free(sc, ii, im);
   1908  1.5       ad 	return (rv);
   1909  1.5       ad }
   1910  1.5       ad 
   1911  1.5       ad /*
   1912  1.5       ad  * Perform an abort.
   1913  1.5       ad  */
   1914  1.5       ad int iop_util_abort(struct iop_softc *sc, struct iop_initiator *ii, int func,
   1915  1.5       ad 		  int tctxabort, int flags)
   1916  1.5       ad {
   1917  1.5       ad 	struct iop_msg *im;
   1918  1.5       ad 	struct i2o_util_abort *mb;
   1919  1.5       ad 	int rv;
   1920  1.5       ad 
   1921  1.5       ad 	if ((rv = iop_msg_alloc(sc, ii, &im, IM_NOINTR)) != 0)
   1922  1.5       ad 		return (rv);
   1923  1.1       ad 
   1924  1.5       ad 	mb = (struct i2o_util_abort *)im->im_msg;
   1925  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_abort);
   1926  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_ABORT);
   1927  1.5       ad 	mb->msgictx = ii->ii_ictx;
   1928  1.5       ad 	mb->msgtctx = im->im_tctx;
   1929  1.5       ad 	mb->flags = (func << 24) | flags;
   1930  1.5       ad 	mb->tctxabort = tctxabort;
   1931  1.1       ad 
   1932  1.5       ad 	rv = iop_msg_enqueue(sc, im, 5000);
   1933  1.5       ad 	iop_msg_free(sc, ii, im);
   1934  1.5       ad 	return (rv);
   1935  1.1       ad }
   1936  1.1       ad 
   1937  1.1       ad /*
   1938  1.5       ad  * Enable or disable event types for the specified device.
   1939  1.1       ad  */
   1940  1.5       ad int iop_util_eventreg(struct iop_softc *sc, struct iop_initiator *ii, int mask)
   1941  1.5       ad {
   1942  1.5       ad 	struct iop_msg *im;
   1943  1.5       ad 	struct i2o_util_event_register *mb;
   1944  1.5       ad 	int rv;
   1945  1.5       ad 
   1946  1.5       ad 	if ((rv = iop_msg_alloc(sc, ii, &im, 0)) != 0)
   1947  1.5       ad 		return (rv);
   1948  1.5       ad 
   1949  1.5       ad 	mb = (struct i2o_util_event_register *)im->im_msg;
   1950  1.5       ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_event_register);
   1951  1.5       ad 	mb->msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_EVENT_REGISTER);
   1952  1.5       ad 	mb->msgictx = ii->ii_ictx;
   1953  1.5       ad 	mb->msgtctx = im->im_tctx;
   1954  1.5       ad 	mb->eventmask = mask;
   1955  1.5       ad 
   1956  1.5       ad 	return (iop_msg_enqueue(sc, im, 0));
   1957  1.5       ad }
   1958  1.5       ad 
   1959  1.1       ad int
   1960  1.5       ad iopopen(dev_t dev, int flag, int mode, struct proc *p)
   1961  1.1       ad {
   1962  1.5       ad 	struct iop_softc *sc;
   1963  1.5       ad 	int unit, error;
   1964  1.5       ad 
   1965  1.5       ad 	unit = minor(dev);
   1966  1.5       ad 
   1967  1.5       ad 	sc = device_lookup(&iop_cd, minor(dev));
   1968  1.5       ad 	if ((sc = iop_cd.cd_devs[unit]) == NULL)
   1969  1.1       ad 		return (ENXIO);
   1970  1.5       ad 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1971  1.5       ad 		return (error);
   1972  1.5       ad 
   1973  1.5       ad 	if ((sc->sc_flags & IOP_OPEN) != 0)
   1974  1.5       ad 		return (EBUSY);
   1975  1.5       ad 	if ((sc->sc_flags & IOP_ONLINE) == 0)
   1976  1.5       ad 		return (EIO);
   1977  1.5       ad 	sc->sc_flags |= IOP_OPEN;
   1978  1.5       ad 
   1979  1.5       ad 	return (0);
   1980  1.1       ad }
   1981  1.1       ad 
   1982  1.5       ad int
   1983  1.5       ad iopclose(dev_t dev, int flag, int mode, struct proc *p)
   1984  1.1       ad {
   1985  1.5       ad 	struct iop_softc *sc;
   1986  1.1       ad 
   1987  1.5       ad 	sc = device_lookup(&iop_cd, minor(dev));
   1988  1.5       ad 	sc->sc_flags &= ~IOP_OPEN;
   1989  1.5       ad 	return (0);
   1990  1.1       ad }
   1991  1.1       ad 
   1992  1.1       ad int
   1993  1.5       ad iopioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
   1994  1.1       ad {
   1995  1.5       ad 	struct iop_softc *sc;
   1996  1.5       ad 	struct iovec *iov;
   1997  1.5       ad 	struct ioppt *pt;
   1998  1.1       ad 	struct iop_msg *im;
   1999  1.5       ad 	struct i2o_msg *mb;
   2000  1.5       ad 	struct i2o_reply *rb;
   2001  1.5       ad 	int rv, i;
   2002  1.5       ad 
   2003  1.5       ad 	if (securelevel >= 2)
   2004  1.5       ad 		return (EPERM);
   2005  1.5       ad 
   2006  1.5       ad 	sc = device_lookup(&iop_cd, minor(dev));
   2007  1.5       ad 
   2008  1.5       ad 	switch (cmd) {
   2009  1.5       ad 	case IOPIOCPT:
   2010  1.5       ad 		pt = (struct ioppt *)data;
   2011  1.5       ad 
   2012  1.5       ad 		if (pt->pt_msglen > IOP_MAX_MSG_SIZE ||
   2013  1.5       ad 		    pt->pt_msglen < sizeof(struct i2o_msg) ||
   2014  1.5       ad 		    pt->pt_nbufs > IOP_MAX_MSG_XFERS ||
   2015  1.5       ad 		    pt->pt_nbufs < 0 ||
   2016  1.5       ad 		    pt->pt_replylen < 0 ||
   2017  1.5       ad 		    pt->pt_timo < 1000 ||
   2018  1.5       ad 		    pt->pt_timo > 5*60*1000) {
   2019  1.5       ad 		    	rv = EINVAL;
   2020  1.5       ad 		    	break;
   2021  1.5       ad 		}
   2022  1.5       ad 
   2023  1.5       ad 		rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR | IM_NOSTATUS);
   2024  1.5       ad 		if (rv != 0)
   2025  1.5       ad 			break;
   2026  1.5       ad 
   2027  1.5       ad 		if ((rv = copyin(pt->pt_msg, im->im_msg, pt->pt_msglen)) != 0) {
   2028  1.5       ad 			iop_msg_free(sc, NULL, im);
   2029  1.5       ad 			break;
   2030  1.5       ad 		}
   2031  1.5       ad 
   2032  1.5       ad 		mb = (struct i2o_msg *)im->im_msg;
   2033  1.5       ad 		mb->msgictx = IOP_ICTX;
   2034  1.5       ad 		mb->msgtctx = im->im_tctx;
   2035  1.5       ad 
   2036  1.5       ad 		for (i = 0; i < pt->pt_nbufs; i++) {
   2037  1.5       ad 			rv = iop_msg_map(sc, im, pt->pt_bufs[i].ptb_data,
   2038  1.5       ad 			    pt->pt_bufs[i].ptb_datalen,
   2039  1.5       ad 			    pt->pt_bufs[i].ptb_out != 0);
   2040  1.5       ad 			if (rv != 0) {
   2041  1.5       ad 				iop_msg_free(sc, NULL, im);
   2042  1.5       ad 				return (rv);
   2043  1.5       ad 			}
   2044  1.5       ad 		}
   2045  1.5       ad 
   2046  1.5       ad 		if ((rv = iop_msg_enqueue(sc, im, pt->pt_timo)) == 0) {
   2047  1.5       ad 			rb = (struct i2o_reply *)im->im_msg;
   2048  1.5       ad 			i = (le32toh(rb->msgflags) >> 14) & ~3;	/* XXX */
   2049  1.5       ad 			if (i > IOP_MAX_REPLY_SIZE)
   2050  1.5       ad 				i = IOP_MAX_REPLY_SIZE;
   2051  1.5       ad 			if (i > pt->pt_replylen)
   2052  1.5       ad 				i = pt->pt_replylen;
   2053  1.5       ad 			rv = copyout(rb, pt->pt_reply, i);
   2054  1.5       ad 		}
   2055  1.5       ad 
   2056  1.5       ad 		iop_msg_free(sc, NULL, im);
   2057  1.5       ad 		break;
   2058  1.1       ad 
   2059  1.5       ad 	case IOPIOCGLCT:
   2060  1.5       ad 		iov = (struct iovec *)data;
   2061  1.5       ad 		rv = lockmgr(&sc->sc_conflock, LK_EXCLUSIVE, NULL);
   2062  1.5       ad 		if (rv == 0) {
   2063  1.5       ad 			i = le16toh(sc->sc_lct->tablesize) << 2;
   2064  1.5       ad 			if (i > iov->iov_len)
   2065  1.5       ad 				i = iov->iov_len;
   2066  1.5       ad 			else
   2067  1.5       ad 				iov->iov_len = i;
   2068  1.5       ad 			rv = copyout(sc->sc_lct, iov->iov_base, i);
   2069  1.5       ad 			lockmgr(&sc->sc_conflock, LK_RELEASE, NULL);
   2070  1.5       ad 		}
   2071  1.5       ad 		break;
   2072  1.1       ad 
   2073  1.5       ad 	case IOPIOCGSTATUS:
   2074  1.5       ad 		iov = (struct iovec *)data;
   2075  1.5       ad 		i = sizeof(struct i2o_status);
   2076  1.5       ad 		if (i > iov->iov_len)
   2077  1.5       ad 			i = iov->iov_len;
   2078  1.5       ad 		else
   2079  1.5       ad 			iov->iov_len = i;
   2080  1.5       ad 		if ((rv = iop_status_get(sc)) == 0)
   2081  1.5       ad 			rv = copyout(&sc->sc_status, iov->iov_base, i);
   2082  1.5       ad 		break;
   2083  1.5       ad 
   2084  1.5       ad 	case IOPIOCRECONFIG:
   2085  1.5       ad 		rv = iop_reconfigure(sc, 0);
   2086  1.5       ad 		break;
   2087  1.5       ad 
   2088  1.5       ad 	default:
   2089  1.5       ad #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
   2090  1.5       ad 		printf("%s: unknown ioctl %lx\n", sc->sc_dv.dv_xname, cmd);
   2091  1.5       ad #endif
   2092  1.5       ad 		rv = ENOTTY;
   2093  1.5       ad 		break;
   2094  1.5       ad 	}
   2095  1.1       ad 
   2096  1.1       ad 	return (rv);
   2097  1.5       ad }
   2098