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