Home | History | Annotate | Line # | Download | only in i2o
iop.c revision 1.2
      1  1.2  ad /*	$NetBSD: iop.c,v 1.2 2000/11/09 12:51:36 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  ad  * by Andrew Doran.
      9  1.1  ad  *
     10  1.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1  ad  * modification, are permitted provided that the following conditions
     12  1.1  ad  * are met:
     13  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1  ad  *    must display the following acknowledgement:
     20  1.1  ad  *        This product includes software developed by the NetBSD
     21  1.1  ad  *        Foundation, Inc. and its contributors.
     22  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1  ad  *    from this software without specific prior written permission.
     25  1.1  ad  *
     26  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  ad  */
     38  1.1  ad 
     39  1.1  ad /*
     40  1.1  ad  * Support for I2O IOPs (intelligent I/O processors).
     41  1.1  ad  */
     42  1.1  ad 
     43  1.1  ad #include "opt_i2o.h"
     44  1.1  ad 
     45  1.1  ad #include <sys/param.h>
     46  1.1  ad #include <sys/systm.h>
     47  1.1  ad #include <sys/kernel.h>
     48  1.1  ad #include <sys/device.h>
     49  1.1  ad #include <sys/queue.h>
     50  1.1  ad #include <sys/proc.h>
     51  1.1  ad #include <sys/malloc.h>
     52  1.1  ad #include <sys/ioctl.h>
     53  1.1  ad #include <sys/endian.h>
     54  1.1  ad #include <sys/pool.h>
     55  1.1  ad 
     56  1.1  ad #include <machine/bus.h>
     57  1.1  ad 
     58  1.1  ad #include <dev/i2o/i2o.h>
     59  1.1  ad #include <dev/i2o/iopreg.h>
     60  1.1  ad #include <dev/i2o/iopvar.h>
     61  1.1  ad 
     62  1.1  ad #define IOP_INL(x, o)		\
     63  1.1  ad     bus_space_read_4((x)->sc_iot, (x)->sc_ioh, (o))
     64  1.1  ad #define IOP_OUTL(x, o, d)	\
     65  1.1  ad     bus_space_write_4((x)->sc_iot, (x)->sc_ioh, (o), (d))
     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.1  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.1  ad #define IOP_MSGHASH_NBUCKETS	64
     90  1.1  ad #define	IOP_MSGHASH(tctx)	(&iop_msghashtbl[(tctx) & iop_msghash])
     91  1.1  ad 
     92  1.1  ad static TAILQ_HEAD(iop_msghashhead, iop_msg) *iop_msghashtbl;
     93  1.1  ad static u_long	iop_msghash;
     94  1.1  ad static void	*iop_sdh;
     95  1.1  ad static struct	pool *iop_msgpool;
     96  1.1  ad 
     97  1.1  ad extern struct cfdriver iop_cd;
     98  1.1  ad 
     99  1.1  ad #define	IC_CONFIGURE	0x01	/* Try to configure devices of this class */
    100  1.1  ad #define	IC_HIGHLEVEL	0x02	/* This is a `high level' device class */
    101  1.1  ad 
    102  1.1  ad struct iop_class {
    103  1.1  ad 	int	ic_class;
    104  1.1  ad 	int	ic_flags;
    105  1.1  ad #ifdef I2OVERBOSE
    106  1.1  ad 	const char	*ic_caption;
    107  1.1  ad #endif
    108  1.1  ad } static const iop_class[] = {
    109  1.1  ad 	{
    110  1.1  ad 		I2O_CLASS_EXECUTIVE,
    111  1.1  ad 		0,
    112  1.1  ad 		IFVERBOSE("executive")
    113  1.1  ad 	},
    114  1.1  ad 	{
    115  1.1  ad 		I2O_CLASS_DDM,
    116  1.1  ad 		0,
    117  1.1  ad 		IFVERBOSE("device driver module")
    118  1.1  ad 	},
    119  1.1  ad 	{
    120  1.1  ad 		I2O_CLASS_RANDOM_BLOCK_STORAGE,
    121  1.1  ad 		IC_CONFIGURE | IC_HIGHLEVEL,
    122  1.1  ad 		IFVERBOSE("random block storage")
    123  1.1  ad 	},
    124  1.1  ad 	{
    125  1.1  ad 		I2O_CLASS_SEQUENTIAL_STORAGE,
    126  1.1  ad 		IC_CONFIGURE | IC_HIGHLEVEL,
    127  1.1  ad 		IFVERBOSE("sequential storage")
    128  1.1  ad 	},
    129  1.1  ad 	{
    130  1.1  ad 		I2O_CLASS_LAN,
    131  1.1  ad 		IC_CONFIGURE,
    132  1.1  ad 		IFVERBOSE("LAN port")
    133  1.1  ad 	},
    134  1.1  ad 	{
    135  1.1  ad 		I2O_CLASS_WAN,
    136  1.1  ad 		IC_CONFIGURE,
    137  1.1  ad 		IFVERBOSE("WAN port")
    138  1.1  ad 	},
    139  1.1  ad 	{
    140  1.1  ad 		I2O_CLASS_FIBRE_CHANNEL_PORT,
    141  1.1  ad 		IC_CONFIGURE,
    142  1.1  ad 		IFVERBOSE("fibrechannel port")
    143  1.1  ad 	},
    144  1.1  ad 	{
    145  1.1  ad 		I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL,
    146  1.1  ad 		0,
    147  1.1  ad 		IFVERBOSE("fibrechannel peripheral")
    148  1.1  ad 	},
    149  1.1  ad  	{
    150  1.1  ad  		I2O_CLASS_SCSI_PERIPHERAL,
    151  1.1  ad  		0,
    152  1.1  ad  		IFVERBOSE("SCSI peripheral")
    153  1.1  ad  	},
    154  1.1  ad 	{
    155  1.1  ad 		I2O_CLASS_ATE_PORT,
    156  1.1  ad 		IC_CONFIGURE,
    157  1.1  ad 		IFVERBOSE("ATE port")
    158  1.1  ad 	},
    159  1.1  ad 	{
    160  1.1  ad 		I2O_CLASS_ATE_PERIPHERAL,
    161  1.1  ad 		0,
    162  1.1  ad 		IFVERBOSE("ATE peripheral")
    163  1.1  ad 	},
    164  1.1  ad 	{
    165  1.1  ad 		I2O_CLASS_FLOPPY_CONTROLLER,
    166  1.1  ad 		IC_CONFIGURE,
    167  1.1  ad 		IFVERBOSE("floppy controller")
    168  1.1  ad 	},
    169  1.1  ad 	{
    170  1.1  ad 		I2O_CLASS_FLOPPY_DEVICE,
    171  1.1  ad 		0,
    172  1.1  ad 		IFVERBOSE("floppy device")
    173  1.1  ad 	},
    174  1.1  ad 	{
    175  1.1  ad 		I2O_CLASS_BUS_ADAPTER_PORT,
    176  1.1  ad 		IC_CONFIGURE,
    177  1.1  ad 		IFVERBOSE("bus adapter port" )
    178  1.1  ad 	},
    179  1.1  ad };
    180  1.1  ad 
    181  1.1  ad #if defined(I2ODEBUG) && defined(I2OVERBOSE)
    182  1.1  ad static const char *iop_status[] = {
    183  1.1  ad 	"success",
    184  1.1  ad 	"abort (dirty)",
    185  1.1  ad 	"abort (no data transfer)",
    186  1.1  ad 	"abort (partial transfer)",
    187  1.1  ad 	"error (dirty)",
    188  1.1  ad 	"error (no data transfer)",
    189  1.1  ad 	"error (partial transfer)",
    190  1.1  ad 	"undefined error code",
    191  1.1  ad 	"process abort (dirty)",
    192  1.1  ad 	"process abort (no data transfer)",
    193  1.1  ad 	"process abort (partial transfer)",
    194  1.1  ad 	"transaction error",
    195  1.1  ad };
    196  1.1  ad #endif
    197  1.1  ad 
    198  1.1  ad static void	iop_config_interrupts(struct device *);
    199  1.1  ad static void	iop_config_interrupts0(struct iop_softc *, int, int);
    200  1.1  ad static void	iop_devinfo(int, char *);
    201  1.1  ad static int	iop_print(void *, const char *);
    202  1.1  ad static void	iop_shutdown(void *);
    203  1.1  ad static int	iop_submatch(struct device *, struct cfdata *, void *);
    204  1.1  ad static int	iop_vendor_print(void *, const char *);
    205  1.1  ad 
    206  1.1  ad static int	iop_alloc_dmamem(struct iop_softc *, int, bus_dmamap_t *,
    207  1.1  ad 				 caddr_t *, bus_addr_t *);
    208  1.1  ad static int	iop_hrt_get(struct iop_softc *);
    209  1.1  ad static int	iop_hrt_get0(struct iop_softc *, struct i2o_hrt *, int);
    210  1.1  ad static int	iop_lct_get0(struct iop_softc *, struct i2o_lct *, int);
    211  1.1  ad static int	iop_ofifo_init(struct iop_softc *);
    212  1.1  ad static int	iop_poll(struct iop_softc *);
    213  1.1  ad static void	iop_release_mfa(struct iop_softc *, u_int32_t);
    214  1.1  ad static int	iop_reset(struct iop_softc *);
    215  1.1  ad static int	iop_status_get(struct iop_softc *);
    216  1.1  ad static int	iop_systab_set(struct iop_softc *);
    217  1.1  ad 
    218  1.1  ad #ifdef I2ODEBUG
    219  1.1  ad static void	iop_reply_print(struct iop_softc *, struct iop_msg *,
    220  1.1  ad 				struct i2o_reply *);
    221  1.1  ad #endif
    222  1.1  ad 
    223  1.1  ad /*
    224  1.1  ad  * Initialise the adapter.
    225  1.1  ad  */
    226  1.1  ad int
    227  1.1  ad iop_init(struct iop_softc *sc, const char *intrstr)
    228  1.1  ad {
    229  1.1  ad 	int rv;
    230  1.1  ad 	u_int32_t mask;
    231  1.1  ad 	static int again;
    232  1.1  ad 	char ident[64];
    233  1.1  ad 
    234  1.1  ad 	if (again == 0) {
    235  1.1  ad 		/* Create the shared message wrapper pool and hash. */
    236  1.1  ad 		iop_msgpool = pool_create(sizeof(struct iop_msg), 0, 0, 0,
    237  1.1  ad 		    "ioppl", 0, NULL, NULL, M_DEVBUF);
    238  1.1  ad 		iop_msghashtbl = hashinit(IOP_MSGHASH_NBUCKETS, HASH_TAILQ,
    239  1.1  ad 		    M_DEVBUF, M_NOWAIT, &iop_msghash);
    240  1.1  ad 		again = 1;
    241  1.1  ad 	}
    242  1.1  ad 
    243  1.1  ad 	/*
    244  1.1  ad 	 * Reset the IOP and request status.
    245  1.1  ad 	 */
    246  1.1  ad 	printf("I2O adapter");
    247  1.1  ad 	if ((rv = iop_reset(sc)) != 0)
    248  1.1  ad 		return (rv);
    249  1.1  ad 	if ((rv = iop_status_get(sc)) != 0) {
    250  1.1  ad 		printf("%s: not responding\n", sc->sc_dv.dv_xname);
    251  1.1  ad 		return (rv);
    252  1.1  ad 	}
    253  1.1  ad 
    254  1.1  ad 	iop_strvis(sc->sc_status.productid, sizeof(sc->sc_status.productid),
    255  1.1  ad 	    ident, sizeof(ident));
    256  1.1  ad 	printf(" <%s>", ident);
    257  1.1  ad 
    258  1.1  ad 	/* Allocate reply frames and initialise the IOP's outbound FIFO. */
    259  1.1  ad 	sc->sc_maxreplycnt = le32toh(sc->sc_status.maxoutboundmframes);
    260  1.1  ad 	if (sc->sc_maxreplycnt > IOP_MAX_HW_REPLYCNT)
    261  1.1  ad 		sc->sc_maxreplycnt = IOP_MAX_HW_REPLYCNT;
    262  1.1  ad 	if ((rv = iop_ofifo_init(sc)) != 0)
    263  1.1  ad 		return (rv);
    264  1.1  ad 
    265  1.1  ad 	/* Bring the IOP online. */
    266  1.1  ad 	if ((rv = iop_hrt_get(sc)) != 0)
    267  1.1  ad 		return (rv);
    268  1.1  ad 	if ((rv = iop_systab_set(sc)) != 0)
    269  1.1  ad 		return (rv);
    270  1.1  ad 	if ((rv = iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_ENABLE,
    271  1.1  ad 	    IOP_ICTX)) != 0)
    272  1.1  ad 		return (rv);
    273  1.1  ad 
    274  1.1  ad 	/* Defer configuration of children until interrupts are working. */
    275  1.1  ad 	config_interrupts((struct device *)sc, iop_config_interrupts);
    276  1.1  ad 
    277  1.1  ad 	/* Configure shutdownhook. */
    278  1.1  ad 	if (iop_sdh == NULL)
    279  1.1  ad 		iop_sdh = shutdownhook_establish(iop_shutdown, NULL);
    280  1.1  ad 
    281  1.1  ad 	/* Ensure interrupts are enabled at the IOP. */
    282  1.1  ad 	mask = IOP_INL(sc, IOP_REG_INTR_MASK);
    283  1.1  ad 	IOP_OUTL(sc, IOP_REG_INTR_MASK, mask & ~IOP_INTR_OFIFO);
    284  1.1  ad 
    285  1.1  ad 	printf("\n");
    286  1.1  ad 	if (intrstr != NULL)
    287  1.1  ad 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    288  1.1  ad 		    intrstr);
    289  1.1  ad 
    290  1.1  ad 	sc->sc_maxqueuecnt = le32toh(sc->sc_status.maxinboundmframes);
    291  1.1  ad 	if (sc->sc_maxqueuecnt > IOP_MAX_HW_QUEUECNT)
    292  1.1  ad 		sc->sc_maxqueuecnt = IOP_MAX_HW_QUEUECNT;
    293  1.1  ad 
    294  1.1  ad #ifdef I2ODEBUG
    295  1.1  ad 	printf("%s: queue depths: inbound %d/%d, outbound %d/%d\n",
    296  1.1  ad 	    sc->sc_dv.dv_xname,
    297  1.1  ad 	    sc->sc_maxqueuecnt, le32toh(sc->sc_status.maxinboundmframes),
    298  1.1  ad 	    sc->sc_maxreplycnt, le32toh(sc->sc_status.maxoutboundmframes));
    299  1.1  ad #endif
    300  1.1  ad 
    301  1.1  ad 	SIMPLEQ_INIT(&sc->sc_queue);
    302  1.1  ad 	return (0);
    303  1.1  ad }
    304  1.1  ad 
    305  1.1  ad /*
    306  1.1  ad  * Attempt to match and attach child devices.
    307  1.1  ad  */
    308  1.1  ad static void
    309  1.1  ad iop_config_interrupts(struct device *self)
    310  1.1  ad {
    311  1.1  ad 	struct iop_attach_args ia;
    312  1.1  ad 	struct iop_softc *sc;
    313  1.1  ad 	int rv;
    314  1.1  ad 
    315  1.1  ad 	sc = (struct iop_softc *)self;
    316  1.1  ad 
    317  1.1  ad 	/* Read the LCT. */
    318  1.1  ad 	if ((rv = iop_lct_get(sc)) != 0)
    319  1.1  ad 		printf("%s: failed to read LCT (%d)\n", sc->sc_dv.dv_xname, rv);
    320  1.1  ad 
    321  1.1  ad 	/* Attempt to match and attach a product-specific extension. */
    322  1.1  ad 	ia.ia_class = I2O_CLASS_ANY;
    323  1.1  ad 	ia.ia_tid = I2O_TID_IOP;
    324  1.1  ad 	config_found_sm(self, &ia, iop_vendor_print, iop_submatch);
    325  1.1  ad 
    326  1.1  ad 	/*
    327  1.1  ad 	 * Match and attach child devices.  We do two runs: the first to
    328  1.1  ad 	 * match "high level" devices, and the second to match "low level"
    329  1.1  ad 	 * devices (low level devices may be parents of high level devices).
    330  1.1  ad 	 *
    331  1.1  ad 	 * XXX sc_lctmap shouldn't be allocated here.
    332  1.1  ad 	 */
    333  1.1  ad 	sc->sc_lctmap = malloc(sc->sc_nlctent * sizeof(int8_t), M_DEVBUF,
    334  1.1  ad 	    M_NOWAIT);
    335  1.1  ad 	memset(sc->sc_lctmap, 0, sc->sc_nlctent * sizeof(int8_t));
    336  1.1  ad 	iop_config_interrupts0(sc, IC_CONFIGURE | IC_HIGHLEVEL,
    337  1.1  ad 	    IC_CONFIGURE | IC_HIGHLEVEL);
    338  1.1  ad 	iop_config_interrupts0(sc, IC_CONFIGURE, IC_CONFIGURE | IC_HIGHLEVEL);
    339  1.1  ad }
    340  1.1  ad 
    341  1.1  ad /*
    342  1.1  ad  * Attempt to match and attach device classes with the specified flag
    343  1.1  ad  * pattern.
    344  1.1  ad  */
    345  1.1  ad static void
    346  1.1  ad iop_config_interrupts0(struct iop_softc *sc, int pat, int mask)
    347  1.1  ad {
    348  1.1  ad 	struct iop_attach_args ia;
    349  1.1  ad 	const struct i2o_lct_entry *le;
    350  1.1  ad 	int i, j, nent, doit;
    351  1.1  ad 
    352  1.1  ad 	nent = sc->sc_nlctent;
    353  1.1  ad 	for (i = 0, le = sc->sc_lct->entry; i < nent; i++, le++) {
    354  1.1  ad 		if ((sc->sc_lctmap[i] & IOP_LCTMAP_INUSE) != 0)
    355  1.1  ad 			continue;
    356  1.1  ad 
    357  1.1  ad 		ia.ia_class = le16toh(le->classid) & 4095;
    358  1.1  ad 		doit = 0;
    359  1.1  ad 
    360  1.1  ad 		for (j = 0; j < sizeof(iop_class) / sizeof(iop_class[0]); j++)
    361  1.1  ad 			if (ia.ia_class == iop_class[j].ic_class)
    362  1.1  ad 				if ((iop_class[j].ic_flags & mask) == pat) {
    363  1.1  ad 					doit = 1;
    364  1.1  ad 					break;
    365  1.1  ad 				}
    366  1.1  ad 
    367  1.1  ad 		/*
    368  1.1  ad 		 * Try to configure the device if the pattern matches.  If
    369  1.1  ad 		 * the device is matched, mark it as being in use.
    370  1.1  ad  		 */
    371  1.1  ad 		if (doit) {
    372  1.1  ad 			ia.ia_tid = le32toh(le->localtid) & 4095;
    373  1.1  ad 			if (config_found_sm(&sc->sc_dv, &ia, iop_print,
    374  1.1  ad 			    iop_submatch))
    375  1.1  ad 				sc->sc_lctmap[i] |= IOP_LCTMAP_INUSE;
    376  1.1  ad 		}
    377  1.1  ad 	}
    378  1.1  ad }
    379  1.1  ad 
    380  1.1  ad static void
    381  1.1  ad iop_devinfo(int class, char *devinfo)
    382  1.1  ad {
    383  1.1  ad #ifdef I2OVERBOSE
    384  1.1  ad 	int i;
    385  1.1  ad 
    386  1.1  ad 	for (i = 0; i < sizeof(iop_class) / sizeof(iop_class[0]); i++)
    387  1.1  ad 		if (class == iop_class[i].ic_class)
    388  1.1  ad 			break;
    389  1.1  ad 
    390  1.1  ad 	if (i == sizeof(iop_class) / sizeof(iop_class[0]))
    391  1.1  ad 		sprintf(devinfo, "device (class 0x%x)", class);
    392  1.1  ad 	else
    393  1.1  ad 		strcpy(devinfo, iop_class[i].ic_caption);
    394  1.1  ad #else
    395  1.1  ad 
    396  1.1  ad 	sprintf(devinfo, "device (class 0x%x)", class);
    397  1.1  ad #endif
    398  1.1  ad }
    399  1.1  ad 
    400  1.1  ad static int
    401  1.1  ad iop_print(void *aux, const char *pnp)
    402  1.1  ad {
    403  1.1  ad 	struct iop_attach_args *ia;
    404  1.1  ad 	char devinfo[256];
    405  1.1  ad 
    406  1.1  ad 	ia = aux;
    407  1.1  ad 
    408  1.1  ad 	if (pnp != NULL) {
    409  1.1  ad 		iop_devinfo(ia->ia_class, devinfo);
    410  1.1  ad 		printf("%s at %s", devinfo, pnp);
    411  1.1  ad 	}
    412  1.1  ad 	printf(" tid %d", ia->ia_tid);
    413  1.1  ad 	return (UNCONF);
    414  1.1  ad }
    415  1.1  ad 
    416  1.1  ad static int
    417  1.1  ad iop_vendor_print(void *aux, const char *pnp)
    418  1.1  ad {
    419  1.1  ad 
    420  1.1  ad 	if (pnp != NULL)
    421  1.1  ad 		printf("vendor specific extension at %s", pnp);
    422  1.1  ad 	return (UNCONF);
    423  1.1  ad }
    424  1.1  ad 
    425  1.1  ad static int
    426  1.1  ad iop_submatch(struct device *parent, struct cfdata *cf, void *aux)
    427  1.1  ad {
    428  1.1  ad 	struct iop_attach_args *ia;
    429  1.1  ad 
    430  1.1  ad 	ia = aux;
    431  1.1  ad 
    432  1.1  ad 	if (cf->iopcf_tid != IOPCF_TID_DEFAULT && cf->iopcf_tid != ia->ia_tid)
    433  1.1  ad 		return (0);
    434  1.1  ad 
    435  1.1  ad 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    436  1.1  ad }
    437  1.1  ad 
    438  1.1  ad /*
    439  1.1  ad  * Shut down all configured IOPs.
    440  1.1  ad  */
    441  1.1  ad static void
    442  1.1  ad iop_shutdown(void *junk)
    443  1.1  ad {
    444  1.1  ad 	struct iop_softc *sc;
    445  1.1  ad 	int i;
    446  1.1  ad 
    447  1.1  ad 	printf("shutting down iop devices... ");
    448  1.1  ad 
    449  1.1  ad 	for (i = 0; i < iop_cd.cd_ndevs; i++) {
    450  1.1  ad 		if ((sc = device_lookup(&iop_cd, i)) == NULL)
    451  1.1  ad 			continue;
    452  1.1  ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_QUIESCE, IOP_ICTX);
    453  1.1  ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_IOP_CLEAR, IOP_ICTX);
    454  1.1  ad 	}
    455  1.1  ad 
    456  1.1  ad 	/* Wait.  Some boards could still be flushing, stupidly enough. */
    457  1.1  ad 	delay(5000*1000);
    458  1.1  ad 	printf(" done\n");
    459  1.1  ad }
    460  1.1  ad 
    461  1.1  ad /*
    462  1.1  ad  * Retrieve adapter status.
    463  1.1  ad  */
    464  1.1  ad static int
    465  1.1  ad iop_status_get(struct iop_softc *sc)
    466  1.1  ad {
    467  1.1  ad 	struct iop_msg *im;
    468  1.1  ad 	struct i2o_exec_status_get *mb;
    469  1.1  ad 	int rv, s;
    470  1.1  ad 
    471  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    472  1.1  ad 		return (rv);
    473  1.1  ad 
    474  1.1  ad 	mb = (struct i2o_exec_status_get *)im->im_msg;
    475  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_status_get);
    476  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_STATUS_GET);
    477  1.1  ad 	mb->reserved[0] = 0;
    478  1.1  ad 	mb->reserved[1] = 0;
    479  1.1  ad 	mb->reserved[2] = 0;
    480  1.1  ad 	mb->reserved[3] = 0;
    481  1.1  ad 	mb->addrlow = kvtop((caddr_t)&sc->sc_status);	/* XXX */
    482  1.1  ad 	mb->addrhigh = 0;
    483  1.1  ad 	mb->length = sizeof(sc->sc_status);
    484  1.1  ad 
    485  1.1  ad 	s = splbio();
    486  1.1  ad 
    487  1.1  ad 	if ((rv = iop_msg_send(sc, im, 0)) != 0) {
    488  1.1  ad 		splx(s);
    489  1.1  ad 		iop_msg_free(sc, NULL, im);
    490  1.1  ad 		return (rv);
    491  1.1  ad 	}
    492  1.1  ad 
    493  1.1  ad 	/* XXX */
    494  1.1  ad 	POLL(2500, *((volatile u_char *)&sc->sc_status.syncbyte) == 0xff);
    495  1.1  ad 
    496  1.1  ad 	splx(s);
    497  1.1  ad 	iop_msg_free(sc, NULL, im);
    498  1.1  ad 	return (*((volatile u_char *)&sc->sc_status.syncbyte) != 0xff);
    499  1.1  ad }
    500  1.1  ad 
    501  1.1  ad /*
    502  1.1  ad  * Allocate DMA safe memory.
    503  1.1  ad  */
    504  1.1  ad static int
    505  1.1  ad iop_alloc_dmamem(struct iop_softc *sc, int size, bus_dmamap_t *dmamap,
    506  1.1  ad 		 caddr_t *kva, bus_addr_t *paddr)
    507  1.1  ad {
    508  1.1  ad 	int rseg, rv;
    509  1.1  ad 	bus_dma_segment_t seg;
    510  1.1  ad 
    511  1.1  ad 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0,
    512  1.1  ad 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    513  1.1  ad 		printf("%s: dmamem_alloc = %d\n", sc->sc_dv.dv_xname, rv);
    514  1.1  ad 		return (rv);
    515  1.1  ad 	}
    516  1.1  ad 
    517  1.1  ad 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size, kva,
    518  1.1  ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    519  1.1  ad 		printf("%s: dmamem_map = %d\n", sc->sc_dv.dv_xname, rv);
    520  1.1  ad 		return (rv);
    521  1.1  ad 	}
    522  1.1  ad 
    523  1.1  ad 	if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
    524  1.1  ad 	    BUS_DMA_NOWAIT, dmamap)) != 0) {
    525  1.1  ad 		printf("%s: dmamap_create = %d\n", sc->sc_dv.dv_xname, rv);
    526  1.1  ad 		return (rv);
    527  1.1  ad 	}
    528  1.1  ad 
    529  1.1  ad 	if ((rv = bus_dmamap_load(sc->sc_dmat, *dmamap, *kva, size,
    530  1.1  ad 	    NULL, BUS_DMA_NOWAIT)) != 0) {
    531  1.1  ad 		printf("%s: dmamap_load = %d\n", sc->sc_dv.dv_xname, rv);
    532  1.1  ad 		return (rv);
    533  1.1  ad 	}
    534  1.1  ad 
    535  1.1  ad 	*paddr = sc->sc_rep_dmamap->dm_segs[0].ds_addr;
    536  1.1  ad 	return (0);
    537  1.1  ad }
    538  1.1  ad 
    539  1.1  ad /*
    540  1.1  ad  * Initalize and populate the adapter's outbound FIFO.
    541  1.1  ad  */
    542  1.1  ad static int
    543  1.1  ad iop_ofifo_init(struct iop_softc *sc)
    544  1.1  ad {
    545  1.1  ad 	struct iop_msg *im;
    546  1.1  ad 	volatile u_int32_t status;
    547  1.1  ad 	bus_addr_t addr;
    548  1.1  ad 	struct i2o_exec_outbound_init *mb;
    549  1.1  ad 	int i, rv;
    550  1.1  ad 
    551  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    552  1.1  ad 		return (rv);
    553  1.1  ad 
    554  1.1  ad 	mb = (struct i2o_exec_outbound_init *)im->im_msg;
    555  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_outbound_init);
    556  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_OUTBOUND_INIT);
    557  1.1  ad 	mb->msgictx = IOP_ICTX;
    558  1.1  ad 	mb->msgtctx = im->im_tctx;
    559  1.1  ad 	mb->pagesize = NBPG;
    560  1.1  ad 	mb->flags = 0x80 | ((IOP_MAX_MSG_SIZE >> 2) << 16);
    561  1.1  ad 
    562  1.1  ad 	status = 0;
    563  1.1  ad 	iop_msg_map(sc, im, (void *)&status, sizeof(status), 0);
    564  1.1  ad 
    565  1.1  ad 	if ((rv = iop_msg_send(sc, im, 0)) != 0) {
    566  1.1  ad 		iop_msg_free(sc, NULL, im);
    567  1.1  ad 		return (rv);
    568  1.1  ad 	}
    569  1.1  ad 
    570  1.1  ad 	DELAY(500000);	/* XXX */
    571  1.1  ad 
    572  1.1  ad 	iop_msg_unmap(sc, im);
    573  1.1  ad 	iop_msg_free(sc, NULL, im);
    574  1.1  ad 
    575  1.1  ad 	if (status != I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS &&
    576  1.1  ad 	    status != I2O_EXEC_OUTBOUND_INIT_COMPLETE) {
    577  1.1  ad 		printf("%s: outbound queue failed to initialize (%08x)\n",
    578  1.1  ad 		    sc->sc_dv.dv_xname, status);
    579  1.1  ad 		return (ENXIO);
    580  1.1  ad 	}
    581  1.1  ad 
    582  1.1  ad #ifdef I2ODEBUG
    583  1.1  ad 	if (status != I2O_EXEC_OUTBOUND_INIT_COMPLETE)
    584  1.1  ad 		printf("%s: outbound FIFO init not complete yet\n",
    585  1.1  ad 		    sc->sc_dv.dv_xname);
    586  1.1  ad #endif
    587  1.1  ad 
    588  1.1  ad 	/* If we need to allocate DMA safe memory, do it now. */
    589  1.1  ad 	if (sc->sc_rep_phys == 0) {
    590  1.1  ad 		sc->sc_rep_size = sc->sc_maxreplycnt * IOP_MAX_MSG_SIZE;
    591  1.1  ad 		iop_alloc_dmamem(sc, sc->sc_rep_size, &sc->sc_rep_dmamap,
    592  1.1  ad 		    &sc->sc_rep, &sc->sc_rep_phys);
    593  1.1  ad 	}
    594  1.1  ad 
    595  1.1  ad 	/* Populate the outbound FIFO. */
    596  1.1  ad 	for (i = sc->sc_maxreplycnt, addr = sc->sc_rep_phys; i; i--) {
    597  1.1  ad 		IOP_OUTL(sc, IOP_REG_OFIFO, (u_int32_t)addr);
    598  1.1  ad 		DELAY(10);
    599  1.1  ad 		addr += IOP_MAX_MSG_SIZE;
    600  1.1  ad 	}
    601  1.1  ad 
    602  1.1  ad 	return (0);
    603  1.1  ad }
    604  1.1  ad 
    605  1.1  ad /*
    606  1.1  ad  * Read the specified number of bytes from the IOP's hardware resource table.
    607  1.1  ad  */
    608  1.1  ad static int
    609  1.1  ad iop_hrt_get0(struct iop_softc *sc, struct i2o_hrt *hrt, int size)
    610  1.1  ad {
    611  1.1  ad 	struct iop_msg *im;
    612  1.1  ad 	int rv;
    613  1.1  ad 	struct i2o_exec_hrt_get *mb;
    614  1.1  ad 
    615  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOINTR)) != 0)
    616  1.1  ad 		return (rv);
    617  1.1  ad 
    618  1.1  ad 	mb = (struct i2o_exec_hrt_get *)im->im_msg;
    619  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_hrt_get);
    620  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_HRT_GET);
    621  1.1  ad 	mb->msgictx = IOP_ICTX;
    622  1.1  ad 	mb->msgtctx = im->im_tctx;
    623  1.1  ad 
    624  1.1  ad 	iop_msg_map(sc, im, hrt, size, 0);
    625  1.1  ad 	rv = iop_msg_send(sc, im, 5000);
    626  1.1  ad 	iop_msg_unmap(sc, im);
    627  1.1  ad 	iop_msg_free(sc, NULL, im);
    628  1.1  ad 	return (rv);
    629  1.1  ad }
    630  1.1  ad 
    631  1.1  ad /*
    632  1.1  ad  * Read the IOP's hardware resource table.  Once read, not much is done with
    633  1.1  ad  * the HRT; it's stored for later retrieval by a user-space program.  Reading
    634  1.1  ad  * the HRT is a required part of the IOP initalization sequence.
    635  1.1  ad  */
    636  1.1  ad static int
    637  1.1  ad iop_hrt_get(struct iop_softc *sc)
    638  1.1  ad {
    639  1.1  ad 	struct i2o_hrt hrthdr, *hrt;
    640  1.1  ad 	int size, rv;
    641  1.1  ad 
    642  1.1  ad 	if ((rv = iop_hrt_get0(sc, &hrthdr, sizeof(hrthdr))) != 0)
    643  1.1  ad 		return (rv);
    644  1.1  ad 
    645  1.1  ad 	size = (htole32(hrthdr.nentries) - 1) * sizeof(struct i2o_hrt_entry) +
    646  1.1  ad 	    sizeof(struct i2o_hrt);
    647  1.1  ad 	hrt = (struct i2o_hrt *)malloc(size, M_DEVBUF, M_NOWAIT);
    648  1.1  ad 
    649  1.1  ad 	if ((rv = iop_hrt_get0(sc, hrt, size)) != 0) {
    650  1.1  ad 		free(hrt, M_DEVBUF);
    651  1.1  ad 		return (rv);
    652  1.1  ad 	}
    653  1.1  ad 
    654  1.1  ad 	if (sc->sc_hrt != NULL)
    655  1.1  ad 		free(sc->sc_hrt, M_DEVBUF);
    656  1.1  ad 	sc->sc_hrt = hrt;
    657  1.1  ad 	return (0);
    658  1.1  ad }
    659  1.1  ad 
    660  1.1  ad /*
    661  1.1  ad  * Request the specified number of bytes from the IOP's logical
    662  1.1  ad  * configuration table.  Must be called with interrupts enabled.
    663  1.1  ad  */
    664  1.1  ad static int
    665  1.1  ad iop_lct_get0(struct iop_softc *sc, struct i2o_lct *lct, int size)
    666  1.1  ad {
    667  1.1  ad 	struct iop_msg *im;
    668  1.1  ad 	struct i2o_exec_lct_notify *mb;
    669  1.1  ad 	int rv;
    670  1.1  ad 
    671  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
    672  1.1  ad 		return (rv);
    673  1.1  ad 
    674  1.1  ad 	memset(lct, 0, size);
    675  1.1  ad 
    676  1.1  ad 	mb = (struct i2o_exec_lct_notify *)im->im_msg;
    677  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_lct_notify);
    678  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_LCT_NOTIFY);
    679  1.1  ad 	mb->msgictx = IOP_ICTX;
    680  1.1  ad 	mb->msgtctx = im->im_tctx;
    681  1.1  ad 	mb->classid = I2O_CLASS_ANY;
    682  1.1  ad 	mb->changeindicator = 0;
    683  1.1  ad 
    684  1.1  ad 	iop_msg_map(sc, im, lct, size, 0);
    685  1.1  ad 	if ((rv = iop_msg_enqueue(sc, im)) == 0)
    686  1.1  ad 		rv = iop_msg_wait(sc, im, 1000);
    687  1.1  ad 	iop_msg_unmap(sc, im);
    688  1.1  ad 	iop_msg_free(sc, NULL, im);
    689  1.1  ad 	return (rv);
    690  1.1  ad }
    691  1.1  ad 
    692  1.1  ad /*
    693  1.1  ad  * Read the IOP's logical configuration table.  Must be called with
    694  1.1  ad  * interrupts enabled.
    695  1.1  ad  */
    696  1.1  ad int
    697  1.1  ad iop_lct_get(struct iop_softc *sc)
    698  1.1  ad {
    699  1.1  ad 	int size, rv;
    700  1.1  ad 	struct i2o_lct lcthdr, *lct;
    701  1.1  ad 
    702  1.1  ad 	/* Determine LCT size. */
    703  1.1  ad 	if ((rv = iop_lct_get0(sc, &lcthdr, sizeof(lcthdr))) != 0)
    704  1.1  ad 		return (rv);
    705  1.1  ad 
    706  1.1  ad 	size = le16toh(lcthdr.tablesize) << 2;
    707  1.1  ad 	if ((lct = (struct i2o_lct *)malloc(size, M_DEVBUF, M_WAITOK)) == NULL)
    708  1.1  ad 		return (ENOMEM);
    709  1.1  ad 
    710  1.1  ad 	/* Request the entire LCT. */
    711  1.1  ad 	if ((rv = iop_lct_get0(sc, lct, size)) != 0) {
    712  1.1  ad 		free(lct, M_DEVBUF);
    713  1.1  ad 		return (rv);
    714  1.1  ad 	}
    715  1.1  ad 
    716  1.1  ad 	/* Swap in the new LCT. */
    717  1.1  ad 	if ((rv = iop_lct_lock(sc)) != 0) {
    718  1.1  ad 		free(lct, M_DEVBUF);
    719  1.1  ad 		return (rv);
    720  1.1  ad 	}
    721  1.1  ad 	if (sc->sc_lct != NULL)
    722  1.1  ad 		free(sc->sc_lct, M_DEVBUF);
    723  1.1  ad 	sc->sc_lct = lct;
    724  1.1  ad 	sc->sc_nlctent = ((le16toh(sc->sc_lct->tablesize) << 2) -
    725  1.1  ad 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
    726  1.1  ad 	    sizeof(struct i2o_lct_entry);
    727  1.1  ad 	iop_lct_unlock(sc);
    728  1.1  ad 	return (0);
    729  1.1  ad }
    730  1.1  ad 
    731  1.1  ad /*
    732  1.1  ad  * Request the specified parameter group from the target.  Must be called
    733  1.1  ad  * with interrupts enabled.
    734  1.1  ad  */
    735  1.1  ad int
    736  1.1  ad iop_params_get(struct iop_softc *sc, int tid, int group, void *buf, int size)
    737  1.1  ad {
    738  1.1  ad 	struct iop_msg *im;
    739  1.1  ad 	struct i2o_util_params_get *mb;
    740  1.1  ad 	int rv;
    741  1.1  ad 	struct {
    742  1.1  ad 		struct	i2o_param_op_list_header olh;
    743  1.1  ad 		struct	i2o_param_op_all_template oat;
    744  1.1  ad 	} req;
    745  1.1  ad 
    746  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
    747  1.1  ad 		return (rv);
    748  1.1  ad 
    749  1.1  ad 	mb = (struct i2o_util_params_get *)im->im_msg;
    750  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_params_get);
    751  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
    752  1.1  ad 	mb->msgictx = IOP_ICTX;
    753  1.1  ad 	mb->msgtctx = im->im_tctx;
    754  1.1  ad 	mb->flags = 0;
    755  1.1  ad 
    756  1.1  ad 	req.olh.count = htole16(1);
    757  1.1  ad 	req.olh.reserved = htole16(0);
    758  1.1  ad 	req.oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
    759  1.1  ad 	req.oat.fieldcount = htole16(0xffff);
    760  1.1  ad 	req.oat.group = htole16(group);
    761  1.1  ad 
    762  1.1  ad 	iop_msg_map(sc, im, &req, sizeof(req), 1);
    763  1.1  ad 	iop_msg_map(sc, im, buf, size, 0);
    764  1.1  ad 
    765  1.1  ad 	if ((rv = iop_msg_enqueue(sc, im)) == 0)
    766  1.1  ad 		rv = iop_msg_wait(sc, im, 1000);
    767  1.1  ad 	iop_msg_unmap(sc, im);
    768  1.1  ad 	iop_msg_free(sc, NULL, im);
    769  1.1  ad 	return (rv);
    770  1.1  ad }
    771  1.1  ad 
    772  1.1  ad /*
    773  1.1  ad  * Execute a simple command (no parameters) and poll on completion.
    774  1.1  ad  */
    775  1.1  ad int
    776  1.1  ad iop_simple_cmd(struct iop_softc *sc, int tid, int function, int ictx)
    777  1.1  ad {
    778  1.1  ad 	struct iop_msg *im;
    779  1.1  ad 	struct i2o_msg *mb;
    780  1.1  ad 	int rv;
    781  1.1  ad 
    782  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOINTR)) != 0)
    783  1.1  ad 		return (rv);
    784  1.1  ad 
    785  1.1  ad 	mb = (struct i2o_msg *)im->im_msg;
    786  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_msg);
    787  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(tid, function);
    788  1.1  ad 	mb->msgictx = ictx;
    789  1.1  ad 	mb->msgtctx = im->im_tctx;
    790  1.1  ad 
    791  1.1  ad 	rv = iop_msg_send(sc, im, 5000);
    792  1.1  ad 	iop_msg_free(sc, NULL, im);
    793  1.1  ad 	return (rv);
    794  1.1  ad }
    795  1.1  ad 
    796  1.1  ad /*
    797  1.1  ad  * Post the system table to the IOP.  We don't maintain a full system table
    798  1.1  ad  * as we should - it describes only "this" IOP and is built on the stack
    799  1.1  ad  * here for the one time that we will use it: posting to the IOP.
    800  1.1  ad  */
    801  1.1  ad static int
    802  1.1  ad iop_systab_set(struct iop_softc *sc)
    803  1.1  ad {
    804  1.1  ad 	struct i2o_iop_entry systab;
    805  1.1  ad 	struct iop_msg *im;
    806  1.1  ad 	u_int32_t mema[2], ioa[2];
    807  1.1  ad 	struct i2o_exec_sys_tab_set *mb;
    808  1.1  ad 	int rv;
    809  1.1  ad 
    810  1.1  ad 	memset(&systab, 0, sizeof(systab));
    811  1.1  ad 	systab.orgid = sc->sc_status.orgid;
    812  1.1  ad 	systab.iopid = htole32(le32toh(sc->sc_status.iopid) & 4095);
    813  1.1  ad 	systab.segnumber = sc->sc_status.segnumber;
    814  1.1  ad 	systab.iopcaps = sc->sc_status.iopcaps;
    815  1.1  ad 	systab.inboundmsgframesize = sc->sc_status.inboundmframesize;
    816  1.1  ad 	systab.inboundmsgportaddresslow =
    817  1.1  ad 	    htole32(sc->sc_memaddr + IOP_REG_IFIFO);
    818  1.1  ad 
    819  1.1  ad 	/* Record private memory and I/O spaces. */
    820  1.1  ad 	mema[0] = htole32(sc->sc_memaddr);
    821  1.1  ad 	mema[1] = htole32(sc->sc_memsize);
    822  1.1  ad 	ioa[0] = htole32(sc->sc_ioaddr);
    823  1.1  ad 	ioa[1] = htole32(sc->sc_iosize);
    824  1.1  ad 
    825  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOINTR)) != 0)
    826  1.1  ad 		return (rv);
    827  1.1  ad 
    828  1.1  ad 	mb = (struct i2o_exec_sys_tab_set *)im->im_msg;
    829  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_sys_tab_set);
    830  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_TAB_SET);
    831  1.1  ad 	mb->msgictx = IOP_ICTX;
    832  1.1  ad 	mb->msgtctx = im->im_tctx;
    833  1.1  ad 	mb->iopid = (2 + sc->sc_dv.dv_unit) & 4095;
    834  1.1  ad 	mb->segnumber = le32toh(sc->sc_status.segnumber) & 4095;
    835  1.1  ad 
    836  1.1  ad 	iop_msg_map(sc, im, &systab, sizeof(systab), 1);
    837  1.1  ad 	iop_msg_map(sc, im, mema, sizeof(mema), 1);
    838  1.1  ad 	iop_msg_map(sc, im, ioa, sizeof(ioa), 1);
    839  1.1  ad 
    840  1.1  ad 	rv = iop_msg_send(sc, im, 5000);
    841  1.1  ad 	iop_msg_unmap(sc, im);
    842  1.1  ad 	iop_msg_free(sc, NULL, im);
    843  1.1  ad 	return (rv);
    844  1.1  ad }
    845  1.1  ad 
    846  1.1  ad /*
    847  1.1  ad  * Reset the adapter.  Must be called with interrupts disabled.
    848  1.1  ad  */
    849  1.1  ad static int
    850  1.1  ad iop_reset(struct iop_softc *sc)
    851  1.1  ad {
    852  1.1  ad 	struct iop_msg *im;
    853  1.1  ad 	volatile u_int32_t sw;
    854  1.1  ad 	u_int32_t mfa;
    855  1.1  ad 	struct i2o_exec_iop_reset *mb;
    856  1.1  ad 	int rv;
    857  1.1  ad 
    858  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOWAIT | IM_NOICTX)) != 0)
    859  1.1  ad 		return (rv);
    860  1.1  ad 
    861  1.1  ad 	sw = 0;
    862  1.1  ad 
    863  1.1  ad 	mb = (struct i2o_exec_iop_reset *)im->im_msg;
    864  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_exec_iop_reset);
    865  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_IOP_RESET);
    866  1.1  ad 	mb->reserved[0] = 0;
    867  1.1  ad 	mb->reserved[1] = 0;
    868  1.1  ad 	mb->reserved[2] = 0;
    869  1.1  ad 	mb->reserved[3] = 0;
    870  1.1  ad 	mb->statuslow = kvtop((caddr_t)&sw);		/* XXX */
    871  1.1  ad 	mb->statushigh = 0;
    872  1.1  ad 
    873  1.1  ad 	if ((rv = iop_msg_send(sc, im, 0)))
    874  1.1  ad 		return (rv);
    875  1.1  ad 	iop_msg_free(sc, NULL, im);
    876  1.1  ad 
    877  1.1  ad 	POLL(2500, sw != 0);					/* XXX */
    878  1.1  ad 	if (sw != I2O_RESET_IN_PROGRESS) {
    879  1.1  ad 		printf("%s: reset rejected\n", sc->sc_dv.dv_xname);
    880  1.1  ad 		return (EIO);
    881  1.1  ad 	}
    882  1.1  ad 
    883  1.1  ad 	/*
    884  1.1  ad 	 * IOP is now in the INIT state.  Wait no more than 5 seconds for
    885  1.1  ad 	 * the inbound queue to become responsive.
    886  1.1  ad 	 */
    887  1.1  ad 	DELAY(1000);
    888  1.1  ad 	POLL(5000, (mfa = IOP_INL(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
    889  1.1  ad 	if (mfa == IOP_MFA_EMPTY) {
    890  1.1  ad 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    891  1.1  ad 		return (EIO);
    892  1.1  ad 	}
    893  1.1  ad 
    894  1.1  ad 	iop_release_mfa(sc, mfa);
    895  1.1  ad 	return (0);
    896  1.1  ad }
    897  1.1  ad 
    898  1.1  ad /*
    899  1.1  ad  * Register a new initiator.
    900  1.1  ad  */
    901  1.1  ad int
    902  1.1  ad iop_initiator_register(struct iop_softc *sc, struct iop_initiator *ii)
    903  1.1  ad {
    904  1.1  ad 	int i;
    905  1.1  ad 
    906  1.1  ad 	/* Find a free slot.  If no slots are free, puke. */
    907  1.1  ad 	for (i = 0; i < IOP_MAX_INITIATORS; i++)
    908  1.1  ad 		if (sc->sc_itab[i] == NULL)
    909  1.1  ad 			break;
    910  1.1  ad 	if (i == IOP_MAX_INITIATORS)
    911  1.1  ad 		return (ENOMEM);
    912  1.1  ad 
    913  1.1  ad #ifdef notyet
    914  1.1  ad 	ii->ii_maxqueuecnt = IOP_MAX_PI_QUEUECNT;
    915  1.1  ad 	ii->ii_queuecnt = 0;
    916  1.1  ad #endif
    917  1.1  ad 	ii->ii_ictx = i + 1;
    918  1.1  ad 	sc->sc_itab[i] = ii;
    919  1.1  ad 	return (0);
    920  1.1  ad }
    921  1.1  ad 
    922  1.1  ad /*
    923  1.1  ad  * Unregister an initiator.
    924  1.1  ad  */
    925  1.1  ad void
    926  1.1  ad iop_initiator_unregister(struct iop_softc *sc, struct iop_initiator *ii)
    927  1.1  ad {
    928  1.1  ad 
    929  1.1  ad #ifdef notyet
    930  1.1  ad #ifdef I2ODEBUG
    931  1.1  ad 	if (ii->ii_queuecnt != 0)
    932  1.1  ad 		panic("iop_inititator_unregister: busy");
    933  1.1  ad #endif
    934  1.1  ad #endif
    935  1.1  ad 	sc->sc_itab[ii->ii_ictx - 1] = NULL;
    936  1.1  ad }
    937  1.1  ad 
    938  1.1  ad /*
    939  1.1  ad  * Attempt to read a reply frame from the adapter.  If we get one, deal with
    940  1.1  ad  * it.
    941  1.1  ad  */
    942  1.1  ad static int
    943  1.1  ad iop_poll(struct iop_softc *sc)
    944  1.1  ad {
    945  1.1  ad 	struct iop_msg *im;
    946  1.1  ad 	struct i2o_reply *rb;
    947  1.1  ad 	struct iop_initiator *ii;
    948  1.1  ad 	u_int32_t rmfa;
    949  1.1  ad 	u_int off, ictx, tctx, status;
    950  1.1  ad 
    951  1.1  ad 	/* Double read to account for IOP bug. */
    952  1.1  ad 	if ((rmfa = IOP_INL(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY &&
    953  1.1  ad 	    (rmfa = IOP_INL(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY)
    954  1.1  ad 		return (-1);
    955  1.1  ad 
    956  1.1  ad 	off = (int)(rmfa - sc->sc_rep_phys);
    957  1.1  ad 	rb = (struct i2o_reply *)(sc->sc_rep + off);
    958  1.1  ad 
    959  1.1  ad 	/*
    960  1.1  ad 	 * Perform reply queue DMA synchronisation.
    961  1.1  ad 	 */
    962  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, off, IOP_MAX_MSG_SIZE,
    963  1.1  ad 	    BUS_DMASYNC_POSTREAD);
    964  1.1  ad 	if (--sc->sc_stat.is_cur_hwqueue != 0)
    965  1.1  ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap,
    966  1.1  ad 		    0, sc->sc_rep_size, BUS_DMASYNC_PREREAD);
    967  1.1  ad 
    968  1.1  ad #ifdef I2ODEBUG
    969  1.1  ad 	if ((le32toh(rb->msgflags) & I2O_MSGFLAGS_64BIT) != 0)
    970  1.1  ad 		panic("iop_poll: 64-bit reply");
    971  1.1  ad #endif
    972  1.1  ad 	/*
    973  1.1  ad 	 * Find the initiator.
    974  1.1  ad 	 */
    975  1.1  ad 	ictx = le32toh(rb->msgictx);
    976  1.1  ad 	if (ictx > IOP_MAX_INITIATORS)
    977  1.1  ad 		panic("%s: bad ictx returned", sc->sc_dv.dv_xname);
    978  1.1  ad 	if (ictx == IOP_ICTX)
    979  1.1  ad 		ii = NULL;
    980  1.1  ad 	else {
    981  1.1  ad #ifdef I2ODEBUG
    982  1.1  ad 		if (sc->sc_itab == NULL)
    983  1.1  ad 			panic("iop_poll: itab == NULL; ictx %d", ictx);
    984  1.1  ad #endif
    985  1.1  ad 		if ((ii = sc->sc_itab[ictx - 1]) == NULL)
    986  1.1  ad 			panic("%s: bad ictx returned", sc->sc_dv.dv_xname);
    987  1.1  ad 	}
    988  1.1  ad 
    989  1.1  ad 	status = rb->reqstatus;
    990  1.1  ad 
    991  1.1  ad 	if (ii == NULL || (ii->ii_flags & II_DISCARD) == 0) {
    992  1.1  ad 		/*
    993  1.1  ad 		 * This initiator tracks state using message wrappers.
    994  1.1  ad 		 *
    995  1.1  ad 		 * Find the originating message wrapper, and if requested
    996  1.1  ad 		 * notify the initiator.
    997  1.1  ad 		 */
    998  1.1  ad 		tctx = le32toh(rb->msgtctx);
    999  1.1  ad 		im = TAILQ_FIRST(IOP_MSGHASH(tctx));
   1000  1.1  ad 		for (; im != NULL; im = TAILQ_NEXT(im, im_hash))
   1001  1.1  ad 			if (im->im_tctx == tctx)
   1002  1.1  ad 				break;
   1003  1.1  ad 		if (im == NULL || (im->im_flags & IM_ALLOCED) == 0)
   1004  1.1  ad 			panic("%s: bad tctx returned (%x, %p)",
   1005  1.1  ad 			    sc->sc_dv.dv_xname, tctx, im);
   1006  1.1  ad #ifdef I2ODEBUG
   1007  1.1  ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1008  1.1  ad 			panic("%s: duplicate reply", sc->sc_dv.dv_xname);
   1009  1.1  ad #endif
   1010  1.1  ad 
   1011  1.1  ad 		im->im_flags |= IM_REPLIED;
   1012  1.1  ad 
   1013  1.1  ad #ifdef I2ODEBUG
   1014  1.1  ad 		if (rb->reqstatus != 0)
   1015  1.1  ad 			iop_reply_print(sc, im, rb);
   1016  1.1  ad #endif
   1017  1.1  ad 		/* Notify the initiator. */
   1018  1.1  ad 		if ((im->im_flags & IM_WAITING) != 0)
   1019  1.1  ad 			wakeup(im);
   1020  1.1  ad 		if ((im->im_flags & IM_NOINTR) == 0)
   1021  1.1  ad 			(*ii->ii_intr)(ii->ii_dv, im, rb);
   1022  1.1  ad 	} else {
   1023  1.1  ad 		/*
   1024  1.1  ad 		 * This initiator discards message wrappers.
   1025  1.1  ad 		 *
   1026  1.1  ad 		 * Simply pass the reply frame to the initiator.
   1027  1.1  ad 		 */
   1028  1.1  ad 		(*ii->ii_intr)(ii->ii_dv, NULL, rb);
   1029  1.1  ad 	}
   1030  1.1  ad 
   1031  1.1  ad 	/* Return the reply frame to the IOP's outbound FIFO. */
   1032  1.1  ad 	IOP_OUTL(sc, IOP_REG_OFIFO, rmfa);
   1033  1.1  ad 
   1034  1.1  ad 	/* Run the queue. */
   1035  1.1  ad 	if ((im = SIMPLEQ_FIRST(&sc->sc_queue)) != NULL)
   1036  1.1  ad 		iop_msg_enqueue(sc, im);
   1037  1.1  ad 
   1038  1.1  ad 	return (status);
   1039  1.1  ad }
   1040  1.1  ad 
   1041  1.1  ad /*
   1042  1.1  ad  * Handle an interrupt from the adapter.
   1043  1.1  ad  */
   1044  1.1  ad int
   1045  1.1  ad iop_intr(void *arg)
   1046  1.1  ad {
   1047  1.1  ad 	struct iop_softc *sc;
   1048  1.1  ad 	int forus;
   1049  1.1  ad 
   1050  1.1  ad 	sc = arg;
   1051  1.1  ad 	forus = 0;
   1052  1.1  ad 
   1053  1.1  ad 	/* Handle replies and dispatch enqueued messages. */
   1054  1.1  ad 	while ((IOP_INL(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0) {
   1055  1.1  ad 		iop_poll(sc);
   1056  1.1  ad 		forus = 1;
   1057  1.1  ad 	}
   1058  1.1  ad 
   1059  1.1  ad #ifdef I2ODEBUG
   1060  1.1  ad 	if (!forus)
   1061  1.1  ad 		printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
   1062  1.1  ad #endif
   1063  1.1  ad 	return (forus);
   1064  1.1  ad }
   1065  1.1  ad 
   1066  1.1  ad /*
   1067  1.1  ad  * Allocate a message wrapper.
   1068  1.1  ad  */
   1069  1.1  ad int
   1070  1.1  ad iop_msg_alloc(struct iop_softc *sc, struct iop_initiator *ii,
   1071  1.1  ad 	      struct iop_msg **imp, int flags)
   1072  1.1  ad {
   1073  1.1  ad 	struct iop_msg *im;
   1074  1.1  ad 	static int tctx = 666;
   1075  1.1  ad 	int s, rv, i;
   1076  1.1  ad 
   1077  1.1  ad #ifdef I2ODEBUG
   1078  1.1  ad 	if ((flags & IM_SYSMASK) != 0)
   1079  1.1  ad 		panic("iop_msg_alloc: system flags specified");
   1080  1.1  ad #endif
   1081  1.1  ad 
   1082  1.1  ad 	s = splbio();	/* XXX */
   1083  1.1  ad 
   1084  1.1  ad 	if (ii != NULL) {
   1085  1.1  ad #ifdef notyet
   1086  1.1  ad 		/*
   1087  1.1  ad 		 * If this initiator has exceeded it's maximum allowed queue
   1088  1.1  ad 		 * depth, sleep until one of its currently queued commands
   1089  1.1  ad 		 * has completed.
   1090  1.1  ad 		 */
   1091  1.1  ad 		if (ii->ii_queuecnt >= ii->ii_maxqueuecnt) {
   1092  1.1  ad 			if ((flags & IM_NOWAIT) != 0) {
   1093  1.1  ad 				splx(s);
   1094  1.1  ad 				return (EAGAIN);
   1095  1.1  ad 			}
   1096  1.1  ad 			ii->ii_waitcnt++;
   1097  1.1  ad 			tsleep(ii, PRIBIO, "iopmsg", 0);
   1098  1.1  ad 		}
   1099  1.1  ad 		ii->ii_queuecnt++;
   1100  1.1  ad #endif
   1101  1.1  ad 		if ((ii->ii_flags & II_DISCARD) != 0)
   1102  1.1  ad 			flags |= IM_DISCARD;
   1103  1.1  ad 	}
   1104  1.1  ad 
   1105  1.1  ad 	im = (struct iop_msg *)pool_get(iop_msgpool,
   1106  1.1  ad 	    (flags & IM_NOWAIT) == 0 ? PR_WAITOK : 0);
   1107  1.1  ad 	if (im == NULL) {
   1108  1.1  ad 		splx(s);
   1109  1.1  ad 		return (ENOMEM);
   1110  1.1  ad 	}
   1111  1.1  ad 
   1112  1.1  ad 	/* XXX */
   1113  1.1  ad 	rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER, IOP_MAX_SGL_ENTRIES,
   1114  1.1  ad 	    IOP_MAX_XFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1115  1.1  ad 	    &im->im_xfer[0].ix_map);
   1116  1.1  ad 	if (rv != 0) {
   1117  1.1  ad 		pool_put(iop_msgpool, im);
   1118  1.1  ad 		splx(s);
   1119  1.1  ad 		return (rv);
   1120  1.1  ad 	}
   1121  1.1  ad 
   1122  1.1  ad 	if ((flags & (IM_DISCARD | IM_NOICTX)) == 0)
   1123  1.1  ad 		TAILQ_INSERT_TAIL(IOP_MSGHASH(tctx), im, im_hash);
   1124  1.1  ad 
   1125  1.1  ad 	splx(s);
   1126  1.1  ad 
   1127  1.1  ad 	im->im_tctx = tctx++;
   1128  1.1  ad 	im->im_flags = flags | IM_ALLOCED;
   1129  1.1  ad 	for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1130  1.1  ad 		im->im_xfer[i].ix_size = 0;
   1131  1.1  ad 	*imp = im;
   1132  1.1  ad 
   1133  1.1  ad 	return (0);
   1134  1.1  ad }
   1135  1.1  ad 
   1136  1.1  ad /*
   1137  1.1  ad  * Free a message wrapper.
   1138  1.1  ad  */
   1139  1.1  ad void
   1140  1.1  ad iop_msg_free(struct iop_softc *sc, struct iop_initiator *ii, struct iop_msg *im)
   1141  1.1  ad {
   1142  1.1  ad 	int s;
   1143  1.1  ad 
   1144  1.1  ad #ifdef I2ODEBUG
   1145  1.1  ad 	if ((im->im_flags & IM_ALLOCED) == 0)
   1146  1.1  ad 		panic("iop_msg_free: wrapper not allocated");
   1147  1.1  ad #endif
   1148  1.1  ad 
   1149  1.1  ad 	/* XXX */
   1150  1.1  ad 	bus_dmamap_destroy(sc->sc_dmat, im->im_xfer[0].ix_map);
   1151  1.1  ad 
   1152  1.1  ad 	s = splbio();	/* XXX */
   1153  1.1  ad 
   1154  1.1  ad 	if ((im->im_flags & (IM_DISCARD | IM_NOICTX)) == 0)
   1155  1.1  ad 		TAILQ_REMOVE(IOP_MSGHASH(im->im_tctx), im, im_hash);
   1156  1.1  ad 
   1157  1.1  ad 	im->im_flags = 0;
   1158  1.1  ad 	pool_put(iop_msgpool, im);
   1159  1.1  ad 
   1160  1.1  ad #ifdef notyet
   1161  1.1  ad 	if (ii != NULL) {
   1162  1.1  ad 		ii->ii_queuecnt--;
   1163  1.1  ad 		if (ii->ii_waitcnt != 0) {
   1164  1.1  ad 			wakeup_one(ii);
   1165  1.1  ad 			ii->ii_waitcnt--;
   1166  1.1  ad 		}
   1167  1.1  ad 	}
   1168  1.1  ad #endif
   1169  1.1  ad 
   1170  1.1  ad 	splx(s);
   1171  1.1  ad }
   1172  1.1  ad 
   1173  1.1  ad /*
   1174  1.1  ad  * Map a data transfer.  Write a scatter gather list into the message frame.
   1175  1.1  ad  */
   1176  1.1  ad int
   1177  1.1  ad iop_msg_map(struct iop_softc *sc, struct iop_msg *im, void *xferaddr,
   1178  1.1  ad 	    int xfersize, int out)
   1179  1.1  ad {
   1180  1.1  ad 	struct iop_xfer *ix;
   1181  1.1  ad 	u_int32_t *mb;
   1182  1.1  ad 	int rv, seg, flg, i;
   1183  1.1  ad 
   1184  1.1  ad 	for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++)
   1185  1.1  ad 		if (ix->ix_size == 0)
   1186  1.1  ad 			break;
   1187  1.1  ad #ifdef I2ODEBUG
   1188  1.1  ad 	if (i == IOP_MAX_MSG_XFERS)
   1189  1.1  ad 		panic("iop_msg_map: too many xfers");
   1190  1.1  ad #endif
   1191  1.1  ad 
   1192  1.1  ad 	/* Only the first DMA map is static. */
   1193  1.1  ad 	if (i != 0) {
   1194  1.1  ad 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
   1195  1.1  ad 		    IOP_MAX_SGL_ENTRIES, IOP_MAX_XFER, 0,
   1196  1.1  ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ix->ix_map);
   1197  1.1  ad 		if (rv != 0)
   1198  1.1  ad 			return (rv);
   1199  1.1  ad 	}
   1200  1.1  ad 
   1201  1.1  ad 	flg = (out ? IX_OUT : IX_IN);
   1202  1.1  ad 	ix->ix_size = xfersize;
   1203  1.1  ad 
   1204  1.1  ad 	rv = bus_dmamap_load(sc->sc_dmat, ix->ix_map, xferaddr, xfersize,
   1205  1.1  ad 	    NULL, 0);
   1206  1.1  ad 	if (rv != 0)
   1207  1.1  ad 		return (rv);
   1208  1.1  ad 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   1209  1.1  ad 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   1210  1.1  ad 
   1211  1.1  ad 	mb = im->im_msg + (im->im_msg[0] >> 16);
   1212  1.1  ad 	if (out)
   1213  1.1  ad 		out = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   1214  1.1  ad 	else
   1215  1.1  ad 		out = I2O_SGL_SIMPLE;
   1216  1.1  ad 
   1217  1.1  ad 	for (seg = 0; seg < ix->ix_map->dm_nsegs; seg++) {
   1218  1.1  ad #ifdef I2ODEBUG
   1219  1.1  ad 		if ((seg << 1) + (im->im_msg[0] >> 16) >=
   1220  1.1  ad 		    (IOP_MAX_MSG_SIZE >> 2))
   1221  1.1  ad 			panic("iop_map_xfer: message frame too large");
   1222  1.1  ad #endif
   1223  1.1  ad 		if (seg == ix->ix_map->dm_nsegs - 1)
   1224  1.1  ad 			out |= I2O_SGL_END_BUFFER;
   1225  1.1  ad 		*mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_len | out;
   1226  1.1  ad 		*mb++ = (u_int32_t)ix->ix_map->dm_segs[seg].ds_addr;
   1227  1.1  ad 	}
   1228  1.1  ad 
   1229  1.1  ad 	/*
   1230  1.1  ad 	 * If this is the first xfer we've mapped for this message, adjust
   1231  1.1  ad 	 * the SGL offset field in the message header.
   1232  1.1  ad 	 */
   1233  1.2  ad 	if ((im->im_flags & IM_SGLOFFADJ) == 0) {
   1234  1.1  ad 		im->im_msg[0] += ((im->im_msg[0] >> 16) + seg * 2) << 4;
   1235  1.2  ad 		im->im_flags |= IM_SGLOFFADJ;
   1236  1.2  ad 	}
   1237  1.1  ad 	im->im_msg[0] += (seg << 17);
   1238  1.1  ad 	return (0);
   1239  1.1  ad }
   1240  1.1  ad 
   1241  1.1  ad /*
   1242  1.1  ad  * Unmap all data transfers associated with a message wrapper.
   1243  1.1  ad  */
   1244  1.1  ad void
   1245  1.1  ad iop_msg_unmap(struct iop_softc *sc, struct iop_msg *im)
   1246  1.1  ad {
   1247  1.1  ad 	struct iop_xfer *ix;
   1248  1.1  ad 	int i;
   1249  1.1  ad 
   1250  1.1  ad 	for (i = 0, ix = im->im_xfer; i < IOP_MAX_MSG_XFERS; i++, ix++) {
   1251  1.1  ad 		if (ix->ix_size == 0)
   1252  1.2  ad 			break;
   1253  1.1  ad 		bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, ix->ix_size,
   1254  1.1  ad 		    ix->ix_flags & IX_OUT ? BUS_DMASYNC_POSTWRITE :
   1255  1.1  ad 		    BUS_DMASYNC_POSTREAD);
   1256  1.1  ad 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   1257  1.1  ad 
   1258  1.1  ad 		/* Only the first DMA map is static. */
   1259  1.1  ad 		if (i != 0)
   1260  1.1  ad 			bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   1261  1.1  ad 
   1262  1.1  ad 		ix->ix_size = 0;
   1263  1.1  ad 	}
   1264  1.1  ad }
   1265  1.1  ad 
   1266  1.1  ad /*
   1267  1.1  ad  * Send a message to the IOP.  Optionally, poll on completion.  Return
   1268  1.1  ad  * non-zero if failure status is returned and IM_NOINTR is set.
   1269  1.1  ad  */
   1270  1.1  ad int
   1271  1.1  ad iop_msg_send(struct iop_softc *sc, struct iop_msg *im, int timo)
   1272  1.1  ad {
   1273  1.1  ad 	u_int32_t mfa, mask;
   1274  1.2  ad 	int rv, status, i, s;
   1275  1.1  ad 
   1276  1.1  ad #ifdef I2ODEBUG
   1277  1.1  ad 	if ((im->im_flags & IM_NOICTX) == 0)
   1278  1.1  ad 		if (im->im_msg[3] == IOP_ICTX &&
   1279  1.1  ad 		    (im->im_flags & IM_NOINTR) == 0)
   1280  1.1  ad 			panic("iop_msg_send: IOP_ICTX and !IM_NOINTR");
   1281  1.1  ad 	if ((im->im_flags & IM_DISCARD) != 0)
   1282  1.1  ad 		panic("iop_msg_send: IM_DISCARD");
   1283  1.1  ad #endif
   1284  1.1  ad 
   1285  1.1  ad 	im->im_tid = im->im_msg[1] & 4095;	/* XXX */
   1286  1.1  ad 
   1287  1.2  ad 	s = splbio();	/* XXX */
   1288  1.1  ad 
   1289  1.1  ad 	/* Wait up to 250ms for an MFA. */
   1290  1.1  ad 	POLL(250, (mfa = IOP_INL(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1291  1.1  ad 	if (mfa == IOP_MFA_EMPTY) {
   1292  1.1  ad 		DPRINTF(("%s: mfa not forthcoming\n", sc->sc_dv.dv_xname));
   1293  1.2  ad 		splx(s);
   1294  1.1  ad 		return (EBUSY);
   1295  1.1  ad 	}
   1296  1.1  ad 
   1297  1.1  ad 	/* Perform reply queue DMA synchronisation and update counters. */
   1298  1.1  ad 	if ((im->im_flags & IM_NOICTX) == 0) {
   1299  1.1  ad 		if (sc->sc_stat.is_cur_hwqueue == 0)
   1300  1.1  ad 			bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   1301  1.1  ad 			    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1302  1.1  ad 		for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1303  1.1  ad 			sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
   1304  1.1  ad 		sc->sc_stat.is_requests++;
   1305  1.1  ad 		if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
   1306  1.1  ad 			sc->sc_stat.is_peak_hwqueue =
   1307  1.1  ad 			    sc->sc_stat.is_cur_hwqueue;
   1308  1.1  ad 	}
   1309  1.1  ad 
   1310  1.1  ad 	/* Terminate scatter/gather lists. */
   1311  1.1  ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1312  1.1  ad 		im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
   1313  1.1  ad 
   1314  1.1  ad 	/* Post the message frame. */
   1315  1.1  ad 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
   1316  1.1  ad 	    im->im_msg, im->im_msg[0] >> 16);
   1317  1.1  ad 
   1318  1.1  ad 	/* Post the MFA back to the IOP, thus starting the command. */
   1319  1.1  ad 	IOP_OUTL(sc, IOP_REG_IFIFO, mfa);
   1320  1.1  ad 
   1321  1.1  ad 	if (timo == 0) {
   1322  1.2  ad 		splx(s);
   1323  1.1  ad 		return (0);
   1324  1.1  ad 	}
   1325  1.1  ad 
   1326  1.1  ad 	/* Wait for completion. */
   1327  1.1  ad 	for (timo *= 10; timo != 0; timo--) {
   1328  1.1  ad 		if ((IOP_INL(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0)
   1329  1.1  ad 			status = iop_poll(sc);
   1330  1.1  ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1331  1.1  ad 			break;
   1332  1.1  ad 		DELAY(100);
   1333  1.1  ad 	}
   1334  1.1  ad 
   1335  1.2  ad 	splx(s);
   1336  1.1  ad 
   1337  1.1  ad 	if (timo == 0) {
   1338  1.1  ad 		DPRINTF(("%s: poll - no reply\n", sc->sc_dv.dv_xname));
   1339  1.1  ad 		rv = EBUSY;
   1340  1.1  ad 	} else if ((im->im_flags & IM_NOINTR) != 0)
   1341  1.1  ad 		rv = (status != I2O_STATUS_SUCCESS ? EIO : 0);
   1342  1.1  ad 
   1343  1.1  ad 	return (rv);
   1344  1.1  ad }
   1345  1.1  ad 
   1346  1.1  ad /*
   1347  1.1  ad  * Try to post a message to the adapter; if that's not possible, enqueue it
   1348  1.1  ad  * with us.
   1349  1.1  ad  */
   1350  1.1  ad int
   1351  1.1  ad iop_msg_enqueue(struct iop_softc *sc, struct iop_msg *im)
   1352  1.1  ad {
   1353  1.1  ad 	u_int mfa;
   1354  1.1  ad 	int s, fromqueue, i;
   1355  1.1  ad 
   1356  1.1  ad #ifdef I2ODEBUG
   1357  1.1  ad 	if (im == NULL)
   1358  1.1  ad 		panic("iop_msg_enqueue: im == NULL");
   1359  1.1  ad 	if (sc == NULL)
   1360  1.1  ad 		panic("iop_msg_enqueue: sc == NULL");
   1361  1.1  ad 	if ((im->im_flags & IM_NOICTX) != 0)
   1362  1.1  ad 		panic("iop_msg_enqueue: IM_NOICTX");
   1363  1.1  ad 	if (im->im_msg[3] == IOP_ICTX && (im->im_flags & IM_NOINTR) == 0)
   1364  1.1  ad 		panic("iop_msg_send: IOP_ICTX and no IM_NOINTR");
   1365  1.1  ad #endif
   1366  1.1  ad 
   1367  1.1  ad 	im->im_tid = im->im_msg[1] & 4095;	/* XXX */
   1368  1.1  ad 
   1369  1.1  ad 	s = splbio();	/* XXX */
   1370  1.1  ad 	fromqueue = (im == SIMPLEQ_FIRST(&sc->sc_queue));
   1371  1.1  ad 
   1372  1.1  ad 	if (sc->sc_stat.is_cur_hwqueue >= sc->sc_maxqueuecnt) {
   1373  1.1  ad 		/*
   1374  1.1  ad 		 * While the IOP may be able to accept more inbound message
   1375  1.1  ad 		 * frames than it advertises, don't push harder than it
   1376  1.1  ad 		 * wants to go lest we starve it.
   1377  1.1  ad 		 *
   1378  1.1  ad 		 * XXX We should be handling IOP resource shortages.
   1379  1.1  ad 		 */
   1380  1.1  ad 		 mfa = IOP_MFA_EMPTY;
   1381  1.1  ad 	} else {
   1382  1.1  ad 		/* Double read to account for IOP bug. */
   1383  1.1  ad 		if ((mfa = IOP_INL(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY)
   1384  1.1  ad 			mfa = IOP_INL(sc, IOP_REG_IFIFO);
   1385  1.1  ad 	}
   1386  1.1  ad 
   1387  1.1  ad 	if (mfa == IOP_MFA_EMPTY) {
   1388  1.1  ad 		/* Can't transfer to h/w queue - queue with us. */
   1389  1.1  ad 		if (!fromqueue) {
   1390  1.1  ad 			SIMPLEQ_INSERT_TAIL(&sc->sc_queue, im, im_queue);
   1391  1.1  ad 			if (++sc->sc_stat.is_cur_swqueue >
   1392  1.1  ad 			    sc->sc_stat.is_peak_swqueue)
   1393  1.1  ad 				sc->sc_stat.is_peak_swqueue =
   1394  1.1  ad 				    sc->sc_stat.is_cur_swqueue;
   1395  1.1  ad 		}
   1396  1.1  ad 		splx(s);
   1397  1.1  ad 		return (0);
   1398  1.1  ad 	} else if (fromqueue) {
   1399  1.1  ad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, im, im_queue);
   1400  1.1  ad 		sc->sc_stat.is_cur_swqueue--;
   1401  1.1  ad 	}
   1402  1.1  ad 
   1403  1.1  ad 	/* Perform reply queue DMA synchronisation and update counters. */
   1404  1.1  ad 	if (sc->sc_stat.is_cur_hwqueue == 0)
   1405  1.1  ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   1406  1.1  ad 		    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1407  1.1  ad 
   1408  1.1  ad 	for (i = 0; i < IOP_MAX_MSG_XFERS; i++)
   1409  1.1  ad 		sc->sc_stat.is_bytes += im->im_xfer[i].ix_size;
   1410  1.1  ad 	sc->sc_stat.is_requests++;
   1411  1.1  ad 	if (++sc->sc_stat.is_cur_hwqueue > sc->sc_stat.is_peak_hwqueue)
   1412  1.1  ad 		sc->sc_stat.is_peak_hwqueue = sc->sc_stat.is_cur_hwqueue;
   1413  1.1  ad 
   1414  1.1  ad 	/* Terminate the scatter/gather list. */
   1415  1.1  ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   1416  1.1  ad 		im->im_msg[(im->im_msg[0] >> 16) - 2] |= I2O_SGL_END;
   1417  1.1  ad 
   1418  1.1  ad 	/* Post the message frame. */
   1419  1.1  ad 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, mfa,
   1420  1.1  ad 	    im->im_msg, im->im_msg[0] >> 16);
   1421  1.1  ad 
   1422  1.1  ad 	/* Post the MFA back to the IOP, thus starting the command. */
   1423  1.1  ad 	IOP_OUTL(sc, IOP_REG_IFIFO, mfa);
   1424  1.1  ad 
   1425  1.1  ad 	/* If this is a discardable message wrapper, free it. */
   1426  1.1  ad 	if ((im->im_flags & IM_DISCARD) != 0)
   1427  1.1  ad 		iop_msg_free(sc, NULL, im);
   1428  1.1  ad 	splx(s);
   1429  1.1  ad 	return (0);
   1430  1.1  ad }
   1431  1.1  ad 
   1432  1.1  ad /*
   1433  1.1  ad  * Wait for the specified message to complete.  Must be called with
   1434  1.1  ad  * interrupts enabled.
   1435  1.1  ad  */
   1436  1.1  ad int
   1437  1.1  ad iop_msg_wait(struct iop_softc *sc, struct iop_msg *im, int timo)
   1438  1.1  ad {
   1439  1.1  ad 	int rv;
   1440  1.1  ad 
   1441  1.1  ad 	im->im_flags |= IM_WAITING;
   1442  1.1  ad 	if ((im->im_flags & IM_REPLIED) != 0)
   1443  1.1  ad 		return (0);
   1444  1.1  ad 	rv = tsleep(im, PRIBIO, "iopmsg", timo);
   1445  1.1  ad 	if ((im->im_flags & IM_REPLIED) != 0)
   1446  1.1  ad 		return (0);
   1447  1.1  ad 	return (rv);
   1448  1.1  ad }
   1449  1.1  ad 
   1450  1.1  ad /*
   1451  1.1  ad  * Release an unused message frame back to the IOP's inbound fifo.
   1452  1.1  ad  */
   1453  1.1  ad static void
   1454  1.1  ad iop_release_mfa(struct iop_softc *sc, u_int32_t mfa)
   1455  1.1  ad {
   1456  1.1  ad 
   1457  1.1  ad 	/* Use the frame to issue a no-op. */
   1458  1.1  ad 	IOP_OUTL(sc, mfa, I2O_VERSION_11 | (4 << 16));
   1459  1.1  ad 	IOP_OUTL(sc, mfa + 4, I2O_MSGFUNC(I2O_TID_IOP, I2O_UTIL_NOP));
   1460  1.1  ad 	IOP_OUTL(sc, mfa + 8, 0);
   1461  1.1  ad 	IOP_OUTL(sc, mfa + 12, 0);
   1462  1.1  ad 
   1463  1.1  ad 	IOP_OUTL(sc, IOP_REG_IFIFO, mfa);
   1464  1.1  ad }
   1465  1.1  ad 
   1466  1.1  ad #ifdef I2ODEBUG
   1467  1.1  ad /*
   1468  1.1  ad  * Print status information from a failure reply frame.
   1469  1.1  ad  */
   1470  1.1  ad static void
   1471  1.1  ad iop_reply_print(struct iop_softc *sc, struct iop_msg *im,
   1472  1.1  ad 		struct i2o_reply *rb)
   1473  1.1  ad {
   1474  1.1  ad 	u_int cmd, detail;
   1475  1.1  ad #ifdef I2OVERBOSE
   1476  1.1  ad 	const char *statusstr;
   1477  1.1  ad #endif
   1478  1.1  ad 
   1479  1.1  ad #ifdef I2ODEBUG
   1480  1.1  ad 	if ((im->im_flags & IM_REPLIED) == 0)
   1481  1.1  ad 		panic("iop_msg_print_status: %p not replied to", im);
   1482  1.1  ad #endif
   1483  1.1  ad 
   1484  1.1  ad 	cmd = le32toh(rb->msgflags) >> 24;
   1485  1.1  ad 	detail = le16toh(rb->detail);
   1486  1.1  ad 
   1487  1.1  ad #ifdef I2OVERBOSE
   1488  1.1  ad 	if (rb->reqstatus < sizeof(iop_status) / sizeof(iop_status[0]))
   1489  1.1  ad 		statusstr = iop_status[rb->reqstatus];
   1490  1.1  ad 	else
   1491  1.1  ad 		statusstr = "undefined error code";
   1492  1.1  ad 
   1493  1.1  ad 	printf("%s: tid=%d cmd=0x%02x: status=0x%02x (%s) detail=0x%04x\n",
   1494  1.1  ad 	    sc->sc_dv.dv_xname, im->im_tid, cmd, rb->reqstatus, statusstr,
   1495  1.1  ad 	    detail);
   1496  1.1  ad #else
   1497  1.1  ad 	printf("%s: tid=%d cmd=0x%02x: status=0x%02x detail=0x%04x\n",
   1498  1.1  ad 	    sc->sc_dv.dv_xname, im->im_tid, cmd, rb->reqstatus, detail);
   1499  1.1  ad #endif
   1500  1.1  ad }
   1501  1.1  ad #endif
   1502  1.1  ad 
   1503  1.1  ad /*
   1504  1.1  ad  * Wait for an exclusive lock on the LCT.
   1505  1.1  ad  */
   1506  1.1  ad int
   1507  1.1  ad iop_lct_lock(struct iop_softc *sc)
   1508  1.1  ad {
   1509  1.1  ad 	int rv;
   1510  1.1  ad 
   1511  1.1  ad 	while ((sc->sc_flags & IOP_LCTLKHELD) != 0)
   1512  1.1  ad 		if ((rv = tsleep(sc, PRIBIO | PCATCH, "ioplct", 0)) != 0)
   1513  1.1  ad 			return (rv);
   1514  1.1  ad 	sc->sc_flags |= IOP_LCTLKHELD;
   1515  1.1  ad 	return (0);
   1516  1.1  ad }
   1517  1.1  ad 
   1518  1.1  ad /*
   1519  1.1  ad  * Unlock and wake up any waiters.
   1520  1.1  ad  */
   1521  1.1  ad void
   1522  1.1  ad iop_lct_unlock(struct iop_softc *sc)
   1523  1.1  ad {
   1524  1.1  ad 
   1525  1.1  ad 	sc->sc_flags &= ~IOP_LCTLKHELD;
   1526  1.1  ad 	wakeup_one(sc);
   1527  1.1  ad }
   1528  1.1  ad 
   1529  1.1  ad /*
   1530  1.1  ad  * Translate an I2O ASCII string into a C string.
   1531  1.1  ad  *
   1532  1.1  ad  * XXX Doesn't belong here.
   1533  1.1  ad  */
   1534  1.1  ad void
   1535  1.1  ad iop_strvis(const char *src, int slen, char *dst, int dlen)
   1536  1.1  ad {
   1537  1.1  ad 	int hc, lc, i;
   1538  1.1  ad 
   1539  1.1  ad 	dlen--;
   1540  1.1  ad 	lc = 0;
   1541  1.1  ad 	hc = 0;
   1542  1.1  ad 	i = 0;
   1543  1.1  ad 
   1544  1.1  ad 	while (slen-- && dlen--) {
   1545  1.1  ad 		if (*src <= 0x20 || *src >= 0x7f) {
   1546  1.1  ad 			if (hc)
   1547  1.1  ad 				dst[i++] = ' ';
   1548  1.1  ad 		} else {
   1549  1.1  ad 			hc = 1;
   1550  1.1  ad 			dst[i++] = *src;
   1551  1.1  ad 			lc = i;
   1552  1.1  ad 		}
   1553  1.1  ad 		src++;
   1554  1.1  ad 	}
   1555  1.1  ad 
   1556  1.1  ad 	dst[lc] = '\0';
   1557  1.1  ad }
   1558  1.1  ad 
   1559  1.1  ad /*
   1560  1.1  ad  * Return the index of the LCT entry matching the specified TID.
   1561  1.1  ad  */
   1562  1.1  ad int
   1563  1.1  ad iop_tid_lct_index(struct iop_softc *sc, int tid)
   1564  1.1  ad {
   1565  1.1  ad 	const struct i2o_lct_entry *le;
   1566  1.1  ad 	int i;
   1567  1.1  ad 
   1568  1.1  ad 	for (i = 0, le = sc->sc_lct->entry; i < sc->sc_nlctent; i++, le++)
   1569  1.1  ad 		if ((le32toh(le->localtid) & 4095) == tid)
   1570  1.1  ad 			return (i);
   1571  1.1  ad 
   1572  1.1  ad 	return (-1);
   1573  1.1  ad }
   1574  1.1  ad 
   1575  1.1  ad /*
   1576  1.1  ad  * Determine whether the specified target is in use by an OSM (or in turn,
   1577  1.1  ad  * by a DDM).  Return a positive non-zero value on error, zero if the TID is
   1578  1.1  ad  * in use and a negative non-zero value if the TID is not in use.
   1579  1.1  ad  */
   1580  1.1  ad int
   1581  1.1  ad iop_tid_inuse(struct iop_softc *sc, int tid)
   1582  1.1  ad {
   1583  1.1  ad 	int i;
   1584  1.1  ad 
   1585  1.1  ad 	if ((i = iop_tid_lct_index(sc, tid)) < 0)
   1586  1.1  ad 		return (ENXIO);
   1587  1.1  ad 	return (-((sc->sc_lctmap[i] & IOP_LCTMAP_INUSE) == 0));
   1588  1.1  ad }
   1589  1.1  ad 
   1590  1.1  ad /*
   1591  1.1  ad  * Mark all targets used by the specified target as in use.
   1592  1.1  ad  */
   1593  1.1  ad void
   1594  1.1  ad iop_tid_markallused(struct iop_softc *sc, int tid)
   1595  1.1  ad {
   1596  1.1  ad 	const struct i2o_lct_entry *le;
   1597  1.1  ad 	int i;
   1598  1.1  ad 
   1599  1.1  ad 	for (i = 0, le = sc->sc_lct->entry; i < sc->sc_nlctent; i++, le++)
   1600  1.1  ad 		if ((le32toh(le->usertid) & 4095) == tid) {
   1601  1.1  ad #ifdef I2ODEBUG
   1602  1.1  ad 			if ((sc->sc_lctmap[i] & IOP_LCTMAP_INUSE) != 0)
   1603  1.1  ad 				panic("iop_tid_markallused: multiple use\n");
   1604  1.1  ad #endif
   1605  1.1  ad 			sc->sc_lctmap[i] |= IOP_LCTMAP_INUSE;
   1606  1.1  ad 		}
   1607  1.1  ad }
   1608  1.1  ad 
   1609  1.1  ad /*
   1610  1.1  ad  * Claim the specified TID.  Must be called with interrupts enabled.
   1611  1.1  ad  */
   1612  1.1  ad int
   1613  1.1  ad iop_tid_claim(struct iop_softc *sc, int tid, int ictx, int flags)
   1614  1.1  ad {
   1615  1.1  ad 	struct iop_msg *im;
   1616  1.1  ad 	struct i2o_util_claim *mb;
   1617  1.1  ad 	int rv;
   1618  1.1  ad 
   1619  1.1  ad 	if ((rv = iop_msg_alloc(sc, NULL, &im, IM_NOINTR)) != 0)
   1620  1.1  ad 		return (rv);
   1621  1.1  ad 
   1622  1.1  ad 	mb = (struct i2o_util_claim *)im->im_msg;
   1623  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_util_claim);
   1624  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_CLAIM);
   1625  1.1  ad 	mb->msgictx = ictx;
   1626  1.1  ad 	mb->msgtctx = im->im_tctx;
   1627  1.1  ad 	mb->flags = flags;
   1628  1.1  ad 
   1629  1.1  ad 	if ((rv = iop_msg_enqueue(sc, im)) == 0)
   1630  1.1  ad 		rv = iop_msg_wait(sc, im, 1000);
   1631  1.1  ad 	iop_msg_free(sc, NULL, im);
   1632  1.1  ad 	return (rv);
   1633  1.1  ad }
   1634