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