Home | History | Annotate | Line # | Download | only in i2o
iop.c revision 1.67
      1  1.67        ad /*	$NetBSD: iop.c,v 1.67 2007/10/19 11:59:43 ad Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*-
      4  1.63        ad  * Copyright (c) 2000, 2001, 2002, 2007 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.20     lukem 
     43  1.20     lukem #include <sys/cdefs.h>
     44  1.67        ad __KERNEL_RCSID(0, "$NetBSD: iop.c,v 1.67 2007/10/19 11:59:43 ad Exp $");
     45   1.1        ad 
     46   1.5        ad #include "iop.h"
     47   1.1        ad 
     48   1.1        ad #include <sys/param.h>
     49   1.1        ad #include <sys/systm.h>
     50   1.1        ad #include <sys/kernel.h>
     51   1.1        ad #include <sys/device.h>
     52   1.1        ad #include <sys/queue.h>
     53   1.1        ad #include <sys/proc.h>
     54   1.1        ad #include <sys/malloc.h>
     55   1.1        ad #include <sys/ioctl.h>
     56   1.1        ad #include <sys/endian.h>
     57   1.5        ad #include <sys/conf.h>
     58   1.5        ad #include <sys/kthread.h>
     59  1.60      elad #include <sys/kauth.h>
     60  1.67        ad #include <sys/bus.h>
     61   1.1        ad 
     62   1.4   thorpej #include <uvm/uvm_extern.h>
     63   1.4   thorpej 
     64   1.1        ad #include <dev/i2o/i2o.h>
     65  1.11        ad #include <dev/i2o/iopio.h>
     66   1.1        ad #include <dev/i2o/iopreg.h>
     67   1.1        ad #include <dev/i2o/iopvar.h>
     68   1.1        ad 
     69  1.44  drochner #include "locators.h"
     70  1.44  drochner 
     71   1.1        ad #define POLL(ms, cond)				\
     72   1.1        ad do {						\
     73  1.48  christos 	int xi;					\
     74  1.48  christos 	for (xi = (ms) * 10; xi; xi--) {	\
     75   1.1        ad 		if (cond)			\
     76   1.1        ad 			break;			\
     77   1.1        ad 		DELAY(100);			\
     78   1.1        ad 	}					\
     79   1.1        ad } while (/* CONSTCOND */0);
     80   1.1        ad 
     81   1.1        ad #ifdef I2ODEBUG
     82   1.1        ad #define DPRINTF(x)	printf x
     83   1.1        ad #else
     84   1.1        ad #define	DPRINTF(x)
     85   1.1        ad #endif
     86   1.1        ad 
     87   1.5        ad #define IOP_ICTXHASH_NBUCKETS	16
     88   1.5        ad #define	IOP_ICTXHASH(ictx)	(&iop_ictxhashtbl[(ictx) & iop_ictxhash])
     89  1.11        ad 
     90  1.11        ad #define	IOP_MAX_SEGS	(((IOP_MAX_XFER + PAGE_SIZE - 1) / PAGE_SIZE) + 1)
     91  1.11        ad 
     92  1.11        ad #define	IOP_TCTX_SHIFT	12
     93  1.11        ad #define	IOP_TCTX_MASK	((1 << IOP_TCTX_SHIFT) - 1)
     94   1.5        ad 
     95   1.5        ad static LIST_HEAD(, iop_initiator) *iop_ictxhashtbl;
     96   1.5        ad static u_long	iop_ictxhash;
     97   1.1        ad static void	*iop_sdh;
     98   1.5        ad static struct	i2o_systab *iop_systab;
     99   1.5        ad static int	iop_systab_size;
    100   1.1        ad 
    101   1.1        ad extern struct cfdriver iop_cd;
    102   1.1        ad 
    103  1.25   gehenna dev_type_open(iopopen);
    104  1.25   gehenna dev_type_close(iopclose);
    105  1.25   gehenna dev_type_ioctl(iopioctl);
    106  1.25   gehenna 
    107  1.25   gehenna const struct cdevsw iop_cdevsw = {
    108  1.25   gehenna 	iopopen, iopclose, noread, nowrite, iopioctl,
    109  1.57  christos 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
    110  1.25   gehenna };
    111  1.25   gehenna 
    112   1.5        ad #define	IC_CONFIGURE	0x01
    113  1.11        ad #define	IC_PRIORITY	0x02
    114   1.1        ad 
    115  1.57  christos static struct iop_class {
    116   1.5        ad 	u_short	ic_class;
    117   1.5        ad 	u_short	ic_flags;
    118  1.65        ad 	const char *ic_caption;
    119  1.57  christos } const iop_class[] = {
    120  1.47     perry 	{
    121   1.1        ad 		I2O_CLASS_EXECUTIVE,
    122   1.1        ad 		0,
    123  1.65        ad 		"executive"
    124   1.1        ad 	},
    125   1.1        ad 	{
    126   1.1        ad 		I2O_CLASS_DDM,
    127   1.1        ad 		0,
    128  1.65        ad 		"device driver module"
    129   1.1        ad 	},
    130   1.1        ad 	{
    131   1.1        ad 		I2O_CLASS_RANDOM_BLOCK_STORAGE,
    132  1.11        ad 		IC_CONFIGURE | IC_PRIORITY,
    133  1.65        ad 		"random block storage"
    134   1.1        ad 	},
    135   1.1        ad 	{
    136   1.1        ad 		I2O_CLASS_SEQUENTIAL_STORAGE,
    137  1.11        ad 		IC_CONFIGURE | IC_PRIORITY,
    138  1.65        ad 		"sequential storage"
    139   1.1        ad 	},
    140   1.1        ad 	{
    141   1.1        ad 		I2O_CLASS_LAN,
    142  1.11        ad 		IC_CONFIGURE | IC_PRIORITY,
    143  1.65        ad 		"LAN port"
    144   1.1        ad 	},
    145   1.1        ad 	{
    146   1.1        ad 		I2O_CLASS_WAN,
    147  1.11        ad 		IC_CONFIGURE | IC_PRIORITY,
    148  1.65        ad 		"WAN port"
    149   1.1        ad 	},
    150   1.1        ad 	{
    151   1.1        ad 		I2O_CLASS_FIBRE_CHANNEL_PORT,
    152   1.1        ad 		IC_CONFIGURE,
    153  1.65        ad 		"fibrechannel port"
    154   1.1        ad 	},
    155   1.1        ad 	{
    156   1.1        ad 		I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL,
    157   1.1        ad 		0,
    158  1.65        ad 		"fibrechannel peripheral"
    159   1.1        ad 	},
    160   1.1        ad  	{
    161   1.1        ad  		I2O_CLASS_SCSI_PERIPHERAL,
    162   1.1        ad  		0,
    163  1.65        ad  		"SCSI peripheral"
    164   1.1        ad  	},
    165   1.1        ad 	{
    166   1.1        ad 		I2O_CLASS_ATE_PORT,
    167   1.1        ad 		IC_CONFIGURE,
    168  1.65        ad 		"ATE port"
    169   1.1        ad 	},
    170  1.47     perry 	{
    171   1.1        ad 		I2O_CLASS_ATE_PERIPHERAL,
    172   1.1        ad 		0,
    173  1.65        ad 		"ATE peripheral"
    174   1.1        ad 	},
    175  1.47     perry 	{
    176   1.1        ad 		I2O_CLASS_FLOPPY_CONTROLLER,
    177   1.1        ad 		IC_CONFIGURE,
    178  1.65        ad 		"floppy controller"
    179   1.1        ad 	},
    180   1.1        ad 	{
    181   1.1        ad 		I2O_CLASS_FLOPPY_DEVICE,
    182   1.1        ad 		0,
    183  1.65        ad 		"floppy device"
    184   1.1        ad 	},
    185   1.1        ad 	{
    186   1.1        ad 		I2O_CLASS_BUS_ADAPTER_PORT,
    187   1.1        ad 		IC_CONFIGURE,
    188  1.65        ad 		"bus adapter port"
    189   1.1        ad 	},
    190   1.1        ad };
    191   1.1        ad 
    192  1.11        ad static const char * const iop_status[] = {
    193   1.1        ad 	"success",
    194   1.1        ad 	"abort (dirty)",
    195   1.1        ad 	"abort (no data transfer)",
    196   1.1        ad 	"abort (partial transfer)",
    197   1.1        ad 	"error (dirty)",
    198   1.1        ad 	"error (no data transfer)",
    199   1.1        ad 	"error (partial transfer)",
    200   1.1        ad 	"undefined error code",
    201   1.1        ad 	"process abort (dirty)",
    202   1.1        ad 	"process abort (no data transfer)",
    203   1.1        ad 	"process abort (partial transfer)",
    204   1.1        ad 	"transaction error",
    205   1.1        ad };
    206   1.1        ad 
    207   1.5        ad static inline u_int32_t	iop_inl(struct iop_softc *, int);
    208   1.5        ad static inline void	iop_outl(struct iop_softc *, int, u_int32_t);
    209   1.5        ad 
    210  1.29   msaitoh static inline u_int32_t	iop_inl_msg(struct iop_softc *, int);
    211  1.30        ad static inline void	iop_outl_msg(struct iop_softc *, int, u_int32_t);
    212  1.29   msaitoh 
    213   1.1        ad static void	iop_config_interrupts(struct device *);
    214  1.11        ad static void	iop_configure_devices(struct iop_softc *, int, int);
    215  1.43    itojun static void	iop_devinfo(int, char *, size_t);
    216   1.1        ad static int	iop_print(void *, const char *);
    217   1.1        ad static void	iop_shutdown(void *);
    218   1.1        ad 
    219  1.11        ad static void	iop_adjqparam(struct iop_softc *, int);
    220  1.11        ad static int	iop_handle_reply(struct iop_softc *, u_int32_t);
    221   1.1        ad static int	iop_hrt_get(struct iop_softc *);
    222   1.1        ad static int	iop_hrt_get0(struct iop_softc *, struct i2o_hrt *, int);
    223  1.11        ad static void	iop_intr_event(struct device *, struct iop_msg *, void *);
    224   1.5        ad static int	iop_lct_get0(struct iop_softc *, struct i2o_lct *, int,
    225   1.5        ad 			     u_int32_t);
    226  1.11        ad static void	iop_msg_poll(struct iop_softc *, struct iop_msg *, int);
    227  1.11        ad static void	iop_msg_wait(struct iop_softc *, struct iop_msg *, int);
    228   1.1        ad static int	iop_ofifo_init(struct iop_softc *);
    229  1.15        ad static int	iop_passthrough(struct iop_softc *, struct ioppt *,
    230  1.15        ad 				struct proc *);
    231   1.9        ad static void	iop_reconf_thread(void *);
    232   1.1        ad static void	iop_release_mfa(struct iop_softc *, u_int32_t);
    233   1.1        ad static int	iop_reset(struct iop_softc *);
    234  1.42        ad static int	iop_sys_enable(struct iop_softc *);
    235   1.1        ad static int	iop_systab_set(struct iop_softc *);
    236  1.11        ad static void	iop_tfn_print(struct iop_softc *, struct i2o_fault_notify *);
    237   1.1        ad 
    238   1.1        ad #ifdef I2ODEBUG
    239  1.11        ad static void	iop_reply_print(struct iop_softc *, struct i2o_reply *);
    240   1.1        ad #endif
    241   1.5        ad 
    242   1.5        ad static inline u_int32_t
    243   1.5        ad iop_inl(struct iop_softc *sc, int off)
    244   1.5        ad {
    245   1.5        ad 
    246   1.5        ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    247   1.5        ad 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    248   1.5        ad 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, off));
    249   1.5        ad }
    250   1.5        ad 
    251   1.5        ad static inline void
    252   1.5        ad iop_outl(struct iop_softc *sc, int off, u_int32_t val)
    253   1.5        ad {
    254   1.5        ad 
    255   1.5        ad 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    256   1.5        ad 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, off, 4,
    257   1.5        ad 	    BUS_SPACE_BARRIER_WRITE);
    258   1.5        ad }
    259   1.5        ad 
    260  1.29   msaitoh static inline u_int32_t
    261  1.29   msaitoh iop_inl_msg(struct iop_softc *sc, int off)
    262  1.29   msaitoh {
    263  1.29   msaitoh 
    264  1.32        ad 	bus_space_barrier(sc->sc_msg_iot, sc->sc_msg_ioh, off, 4,
    265  1.29   msaitoh 	    BUS_SPACE_BARRIER_WRITE | BUS_SPACE_BARRIER_READ);
    266  1.32        ad 	return (bus_space_read_4(sc->sc_msg_iot, sc->sc_msg_ioh, off));
    267  1.29   msaitoh }
    268  1.29   msaitoh 
    269  1.29   msaitoh static inline void
    270  1.30        ad iop_outl_msg(struct iop_softc *sc, int off, u_int32_t val)
    271  1.29   msaitoh {
    272  1.29   msaitoh 
    273  1.30        ad 	bus_space_write_4(sc->sc_msg_iot, sc->sc_msg_ioh, off, val);
    274  1.30        ad 	bus_space_barrier(sc->sc_msg_iot, sc->sc_msg_ioh, off, 4,
    275  1.29   msaitoh 	    BUS_SPACE_BARRIER_WRITE);
    276  1.29   msaitoh }
    277  1.29   msaitoh 
    278   1.1        ad /*
    279  1.11        ad  * Initialise the IOP and our interface.
    280   1.1        ad  */
    281   1.5        ad void
    282   1.1        ad iop_init(struct iop_softc *sc, const char *intrstr)
    283   1.1        ad {
    284  1.11        ad 	struct iop_msg *im;
    285  1.40   mycroft 	int rv, i, j, state, nsegs;
    286   1.1        ad 	u_int32_t mask;
    287   1.1        ad 	char ident[64];
    288   1.1        ad 
    289  1.15        ad 	state = 0;
    290  1.15        ad 
    291  1.15        ad 	printf("I2O adapter");
    292  1.15        ad 
    293  1.65        ad 	mutex_init(&sc->sc_intrlock, MUTEX_DRIVER, IPL_VM);
    294  1.65        ad 	mutex_init(&sc->sc_conflock, MUTEX_DRIVER, IPL_NONE);
    295  1.65        ad 	cv_init(&sc->sc_confcv, "iopconf");
    296  1.65        ad 
    297  1.11        ad 	if (iop_ictxhashtbl == NULL)
    298   1.5        ad 		iop_ictxhashtbl = hashinit(IOP_ICTXHASH_NBUCKETS, HASH_LIST,
    299   1.5        ad 		    M_DEVBUF, M_NOWAIT, &iop_ictxhash);
    300   1.1        ad 
    301  1.15        ad 	/* Disable interrupts at the IOP. */
    302  1.15        ad 	mask = iop_inl(sc, IOP_REG_INTR_MASK);
    303  1.15        ad 	iop_outl(sc, IOP_REG_INTR_MASK, mask | IOP_INTR_OFIFO);
    304   1.5        ad 
    305  1.15        ad 	/* Allocate a scratch DMA map for small miscellaneous shared data. */
    306  1.15        ad 	if (bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
    307  1.15        ad 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->sc_scr_dmamap) != 0) {
    308  1.15        ad 		printf("%s: cannot create scratch dmamap\n",
    309  1.15        ad 		    sc->sc_dv.dv_xname);
    310   1.5        ad 		return;
    311   1.1        ad 	}
    312  1.15        ad 
    313  1.15        ad 	if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    314  1.15        ad 	    sc->sc_scr_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
    315  1.15        ad 		printf("%s: cannot alloc scratch dmamem\n",
    316  1.15        ad 		    sc->sc_dv.dv_xname);
    317  1.15        ad 		goto bail_out;
    318  1.15        ad 	}
    319  1.15        ad 	state++;
    320  1.15        ad 
    321  1.15        ad 	if (bus_dmamem_map(sc->sc_dmat, sc->sc_scr_seg, nsegs, PAGE_SIZE,
    322  1.15        ad 	    &sc->sc_scr, 0)) {
    323  1.15        ad 		printf("%s: cannot map scratch dmamem\n", sc->sc_dv.dv_xname);
    324  1.15        ad 		goto bail_out;
    325  1.15        ad 	}
    326  1.15        ad 	state++;
    327  1.15        ad 
    328  1.15        ad 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_scr_dmamap, sc->sc_scr,
    329  1.15        ad 	    PAGE_SIZE, NULL, BUS_DMA_NOWAIT)) {
    330  1.15        ad 		printf("%s: cannot load scratch dmamap\n", sc->sc_dv.dv_xname);
    331  1.15        ad 		goto bail_out;
    332  1.15        ad 	}
    333  1.15        ad 	state++;
    334  1.15        ad 
    335  1.21        ad #ifdef I2ODEBUG
    336  1.21        ad 	/* So that our debug checks don't choke. */
    337  1.21        ad 	sc->sc_framesize = 128;
    338  1.21        ad #endif
    339  1.21        ad 
    340  1.65        ad 	/* Avoid syncing the reply map until it's set up. */
    341  1.65        ad 	sc->sc_curib = 0x123;
    342  1.65        ad 
    343  1.15        ad 	/* Reset the adapter and request status. */
    344  1.15        ad  	if ((rv = iop_reset(sc)) != 0) {
    345  1.15        ad  		printf("%s: not responding (reset)\n", sc->sc_dv.dv_xname);
    346  1.15        ad 		goto bail_out;
    347  1.15        ad  	}
    348  1.15        ad 
    349  1.15        ad  	if ((rv = iop_status_get(sc, 1)) != 0) {
    350  1.15        ad 		printf("%s: not responding (get status)\n",
    351  1.15        ad 		    sc->sc_dv.dv_xname);
    352  1.15        ad 		goto bail_out;
    353  1.15        ad  	}
    354  1.15        ad 
    355   1.5        ad 	sc->sc_flags |= IOP_HAVESTATUS;
    356   1.5        ad 	iop_strvis(sc, sc->sc_status.productid, sizeof(sc->sc_status.productid),
    357   1.1        ad 	    ident, sizeof(ident));
    358   1.5        ad 	printf(" <%s>\n", ident);
    359   1.5        ad 
    360   1.5        ad #ifdef I2ODEBUG
    361   1.5        ad 	printf("%s: orgid=0x%04x version=%d\n", sc->sc_dv.dv_xname,
    362   1.5        ad 	    le16toh(sc->sc_status.orgid),
    363   1.5        ad 	    (le32toh(sc->sc_status.segnumber) >> 12) & 15);
    364   1.5        ad 	printf("%s: type want have cbase\n", sc->sc_dv.dv_xname);
    365   1.5        ad 	printf("%s: mem  %04x %04x %08x\n", sc->sc_dv.dv_xname,
    366   1.5        ad 	    le32toh(sc->sc_status.desiredprivmemsize),
    367   1.5        ad 	    le32toh(sc->sc_status.currentprivmemsize),
    368   1.5        ad 	    le32toh(sc->sc_status.currentprivmembase));
    369   1.5        ad 	printf("%s: i/o  %04x %04x %08x\n", sc->sc_dv.dv_xname,
    370   1.5        ad 	    le32toh(sc->sc_status.desiredpriviosize),
    371   1.5        ad 	    le32toh(sc->sc_status.currentpriviosize),
    372   1.5        ad 	    le32toh(sc->sc_status.currentpriviobase));
    373   1.5        ad #endif
    374   1.1        ad 
    375  1.11        ad 	sc->sc_maxob = le32toh(sc->sc_status.maxoutboundmframes);
    376  1.11        ad 	if (sc->sc_maxob > IOP_MAX_OUTBOUND)
    377  1.11        ad 		sc->sc_maxob = IOP_MAX_OUTBOUND;
    378  1.11        ad 	sc->sc_maxib = le32toh(sc->sc_status.maxinboundmframes);
    379  1.11        ad 	if (sc->sc_maxib > IOP_MAX_INBOUND)
    380  1.11        ad 		sc->sc_maxib = IOP_MAX_INBOUND;
    381  1.19        ad 	sc->sc_framesize = le16toh(sc->sc_status.inboundmframesize) << 2;
    382  1.19        ad 	if (sc->sc_framesize > IOP_MAX_MSG_SIZE)
    383  1.19        ad 		sc->sc_framesize = IOP_MAX_MSG_SIZE;
    384  1.19        ad 
    385  1.19        ad #if defined(I2ODEBUG) || defined(DIAGNOSTIC)
    386  1.19        ad 	if (sc->sc_framesize < IOP_MIN_MSG_SIZE) {
    387  1.19        ad 		printf("%s: frame size too small (%d)\n",
    388  1.19        ad 		    sc->sc_dv.dv_xname, sc->sc_framesize);
    389  1.23        ad 		goto bail_out;
    390  1.19        ad 	}
    391  1.19        ad #endif
    392  1.11        ad 
    393  1.11        ad 	/* Allocate message wrappers. */
    394  1.22   tsutsui 	im = malloc(sizeof(*im) * sc->sc_maxib, M_DEVBUF, M_NOWAIT|M_ZERO);
    395  1.23        ad 	if (im == NULL) {
    396  1.23        ad 		printf("%s: memory allocation failure\n", sc->sc_dv.dv_xname);
    397  1.23        ad 		goto bail_out;
    398  1.23        ad 	}
    399  1.23        ad 	state++;
    400  1.11        ad 	sc->sc_ims = im;
    401  1.11        ad 	SLIST_INIT(&sc->sc_im_freelist);
    402  1.11        ad 
    403  1.40   mycroft 	for (i = 0; i < sc->sc_maxib; i++, im++) {
    404  1.11        ad 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
    405  1.11        ad 		    IOP_MAX_SEGS, IOP_MAX_XFER, 0,
    406  1.11        ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    407  1.11        ad 		    &im->im_xfer[0].ix_map);
    408  1.11        ad 		if (rv != 0) {
    409  1.11        ad 			printf("%s: couldn't create dmamap (%d)",
    410  1.11        ad 			    sc->sc_dv.dv_xname, rv);
    411  1.40   mycroft 			goto bail_out3;
    412  1.11        ad 		}
    413  1.11        ad 
    414  1.11        ad 		im->im_tctx = i;
    415  1.11        ad 		SLIST_INSERT_HEAD(&sc->sc_im_freelist, im, im_chain);
    416  1.65        ad 		cv_init(&im->im_cv, "iopmsg");
    417  1.11        ad 	}
    418   1.1        ad 
    419  1.17       wiz 	/* Initialise the IOP's outbound FIFO. */
    420   1.5        ad 	if (iop_ofifo_init(sc) != 0) {
    421  1.15        ad 		printf("%s: unable to init oubound FIFO\n",
    422  1.15        ad 		    sc->sc_dv.dv_xname);
    423  1.40   mycroft 		goto bail_out3;
    424   1.5        ad 	}
    425   1.1        ad 
    426   1.5        ad 	/*
    427   1.5        ad  	 * Defer further configuration until (a) interrupts are working and
    428   1.5        ad  	 * (b) we have enough information to build the system table.
    429   1.5        ad  	 */
    430   1.1        ad 	config_interrupts((struct device *)sc, iop_config_interrupts);
    431   1.1        ad 
    432   1.5        ad 	/* Configure shutdown hook before we start any device activity. */
    433   1.1        ad 	if (iop_sdh == NULL)
    434   1.1        ad 		iop_sdh = shutdownhook_establish(iop_shutdown, NULL);
    435   1.1        ad 
    436   1.1        ad 	/* Ensure interrupts are enabled at the IOP. */
    437   1.5        ad 	mask = iop_inl(sc, IOP_REG_INTR_MASK);
    438   1.5        ad 	iop_outl(sc, IOP_REG_INTR_MASK, mask & ~IOP_INTR_OFIFO);
    439   1.1        ad 
    440   1.1        ad 	if (intrstr != NULL)
    441   1.1        ad 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    442   1.1        ad 		    intrstr);
    443   1.1        ad 
    444   1.1        ad #ifdef I2ODEBUG
    445   1.1        ad 	printf("%s: queue depths: inbound %d/%d, outbound %d/%d\n",
    446  1.11        ad 	    sc->sc_dv.dv_xname, sc->sc_maxib,
    447  1.11        ad 	    le32toh(sc->sc_status.maxinboundmframes),
    448  1.11        ad 	    sc->sc_maxob, le32toh(sc->sc_status.maxoutboundmframes));
    449   1.1        ad #endif
    450   1.1        ad 
    451  1.15        ad 	return;
    452  1.15        ad 
    453  1.40   mycroft  bail_out3:
    454  1.15        ad  	if (state > 3) {
    455  1.15        ad 		for (j = 0; j < i; j++)
    456  1.15        ad 			bus_dmamap_destroy(sc->sc_dmat,
    457  1.15        ad 			    sc->sc_ims[j].im_xfer[0].ix_map);
    458  1.15        ad 		free(sc->sc_ims, M_DEVBUF);
    459  1.15        ad 	}
    460  1.40   mycroft  bail_out:
    461  1.15        ad 	if (state > 2)
    462  1.15        ad 		bus_dmamap_unload(sc->sc_dmat, sc->sc_scr_dmamap);
    463  1.15        ad 	if (state > 1)
    464  1.15        ad 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_scr, PAGE_SIZE);
    465  1.15        ad 	if (state > 0)
    466  1.15        ad 		bus_dmamem_free(sc->sc_dmat, sc->sc_scr_seg, nsegs);
    467  1.15        ad 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_scr_dmamap);
    468   1.1        ad }
    469   1.1        ad 
    470   1.1        ad /*
    471   1.5        ad  * Perform autoconfiguration tasks.
    472   1.1        ad  */
    473   1.1        ad static void
    474   1.1        ad iop_config_interrupts(struct device *self)
    475   1.1        ad {
    476  1.18        ad 	struct iop_attach_args ia;
    477   1.5        ad 	struct iop_softc *sc, *iop;
    478   1.5        ad 	struct i2o_systab_entry *ste;
    479   1.5        ad 	int rv, i, niop;
    480  1.49  drochner 	int locs[IOPCF_NLOCS];
    481   1.1        ad 
    482  1.54   thorpej 	sc = device_private(self);
    483  1.65        ad 	mutex_enter(&sc->sc_conflock);
    484  1.65        ad 
    485   1.5        ad 	LIST_INIT(&sc->sc_iilist);
    486   1.5        ad 
    487   1.5        ad 	printf("%s: configuring...\n", sc->sc_dv.dv_xname);
    488   1.1        ad 
    489   1.5        ad 	if (iop_hrt_get(sc) != 0) {
    490   1.5        ad 		printf("%s: unable to retrieve HRT\n", sc->sc_dv.dv_xname);
    491  1.65        ad 		mutex_exit(&sc->sc_conflock);
    492   1.5        ad 		return;
    493   1.5        ad 	}
    494   1.1        ad 
    495   1.5        ad 	/*
    496   1.5        ad  	 * Build the system table.
    497   1.5        ad  	 */
    498   1.5        ad 	if (iop_systab == NULL) {
    499   1.5        ad 		for (i = 0, niop = 0; i < iop_cd.cd_ndevs; i++) {
    500   1.5        ad 			if ((iop = device_lookup(&iop_cd, i)) == NULL)
    501   1.5        ad 				continue;
    502   1.5        ad 			if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
    503   1.5        ad 				continue;
    504  1.11        ad 			if (iop_status_get(iop, 1) != 0) {
    505   1.5        ad 				printf("%s: unable to retrieve status\n",
    506   1.5        ad 				    sc->sc_dv.dv_xname);
    507   1.5        ad 				iop->sc_flags &= ~IOP_HAVESTATUS;
    508   1.5        ad 				continue;
    509   1.5        ad 			}
    510   1.5        ad 			niop++;
    511   1.5        ad 		}
    512  1.65        ad 		if (niop == 0) {
    513  1.65        ad 			mutex_exit(&sc->sc_conflock);
    514   1.5        ad 			return;
    515  1.65        ad 		}
    516   1.5        ad 
    517   1.5        ad 		i = sizeof(struct i2o_systab_entry) * (niop - 1) +
    518   1.5        ad 		    sizeof(struct i2o_systab);
    519   1.5        ad 		iop_systab_size = i;
    520  1.22   tsutsui 		iop_systab = malloc(i, M_DEVBUF, M_NOWAIT|M_ZERO);
    521   1.5        ad 
    522   1.5        ad 		iop_systab->numentries = niop;
    523   1.5        ad 		iop_systab->version = I2O_VERSION_11;
    524   1.5        ad 
    525   1.5        ad 		for (i = 0, ste = iop_systab->entry; i < iop_cd.cd_ndevs; i++) {
    526   1.5        ad 			if ((iop = device_lookup(&iop_cd, i)) == NULL)
    527   1.5        ad 				continue;
    528   1.5        ad 			if ((iop->sc_flags & IOP_HAVESTATUS) == 0)
    529   1.5        ad 				continue;
    530   1.5        ad 
    531   1.5        ad 			ste->orgid = iop->sc_status.orgid;
    532  1.53   thorpej 			ste->iopid = device_unit(&iop->sc_dv) + 2;
    533   1.5        ad 			ste->segnumber =
    534   1.5        ad 			    htole32(le32toh(iop->sc_status.segnumber) & ~4095);
    535   1.5        ad 			ste->iopcaps = iop->sc_status.iopcaps;
    536   1.5        ad 			ste->inboundmsgframesize =
    537   1.5        ad 			    iop->sc_status.inboundmframesize;
    538   1.5        ad 			ste->inboundmsgportaddresslow =
    539   1.5        ad 			    htole32(iop->sc_memaddr + IOP_REG_IFIFO);
    540   1.5        ad 			ste++;
    541   1.5        ad 		}
    542   1.5        ad 	}
    543   1.5        ad 
    544  1.11        ad 	/*
    545  1.11        ad 	 * Post the system table to the IOP and bring it to the OPERATIONAL
    546  1.11        ad 	 * state.
    547  1.11        ad 	 */
    548   1.5        ad 	if (iop_systab_set(sc) != 0) {
    549   1.5        ad 		printf("%s: unable to set system table\n", sc->sc_dv.dv_xname);
    550  1.65        ad 		mutex_exit(&sc->sc_conflock);
    551   1.5        ad 		return;
    552   1.5        ad 	}
    553  1.42        ad 	if (iop_sys_enable(sc) != 0) {
    554   1.5        ad 		printf("%s: unable to enable system\n", sc->sc_dv.dv_xname);
    555  1.65        ad 		mutex_exit(&sc->sc_conflock);
    556   1.5        ad 		return;
    557   1.5        ad 	}
    558   1.5        ad 
    559   1.5        ad 	/*
    560   1.5        ad 	 * Set up an event handler for this IOP.
    561   1.5        ad 	 */
    562   1.5        ad 	sc->sc_eventii.ii_dv = self;
    563   1.5        ad 	sc->sc_eventii.ii_intr = iop_intr_event;
    564  1.15        ad 	sc->sc_eventii.ii_flags = II_NOTCTX | II_UTILITY;
    565   1.5        ad 	sc->sc_eventii.ii_tid = I2O_TID_IOP;
    566  1.11        ad 	iop_initiator_register(sc, &sc->sc_eventii);
    567  1.11        ad 
    568  1.11        ad 	rv = iop_util_eventreg(sc, &sc->sc_eventii,
    569  1.11        ad 	    I2O_EVENT_EXEC_RESOURCE_LIMITS |
    570  1.11        ad 	    I2O_EVENT_EXEC_CONNECTION_FAIL |
    571  1.11        ad 	    I2O_EVENT_EXEC_ADAPTER_FAULT |
    572  1.11        ad 	    I2O_EVENT_EXEC_POWER_FAIL |
    573  1.11        ad 	    I2O_EVENT_EXEC_RESET_PENDING |
    574  1.11        ad 	    I2O_EVENT_EXEC_RESET_IMMINENT |
    575  1.11        ad 	    I2O_EVENT_EXEC_HARDWARE_FAIL |
    576  1.11        ad 	    I2O_EVENT_EXEC_XCT_CHANGE |
    577  1.11        ad 	    I2O_EVENT_EXEC_DDM_AVAILIBILITY |
    578  1.11        ad 	    I2O_EVENT_GEN_DEVICE_RESET |
    579  1.11        ad 	    I2O_EVENT_GEN_STATE_CHANGE |
    580  1.11        ad 	    I2O_EVENT_GEN_GENERAL_WARNING);
    581  1.11        ad 	if (rv != 0) {
    582   1.5        ad 		printf("%s: unable to register for events", sc->sc_dv.dv_xname);
    583  1.65        ad 		mutex_exit(&sc->sc_conflock);
    584   1.5        ad 		return;
    585   1.5        ad 	}
    586   1.5        ad 
    587  1.18        ad 	/*
    588  1.18        ad 	 * Attempt to match and attach a product-specific extension.
    589  1.18        ad 	 */
    590   1.1        ad 	ia.ia_class = I2O_CLASS_ANY;
    591   1.1        ad 	ia.ia_tid = I2O_TID_IOP;
    592  1.49  drochner 	locs[IOPCF_TID] = I2O_TID_IOP;
    593  1.50  drochner 	config_found_sm_loc(self, "iop", locs, &ia, iop_print,
    594  1.50  drochner 		config_stdsubmatch);
    595   1.5        ad 
    596  1.18        ad 	/*
    597  1.18        ad 	 * Start device configuration.
    598  1.18        ad 	 */
    599  1.63        ad 	if ((rv = iop_reconfigure(sc, 0)) == -1)
    600   1.5        ad 		printf("%s: configure failed (%d)\n", sc->sc_dv.dv_xname, rv);
    601  1.65        ad 
    602   1.9        ad 
    603  1.11        ad 	sc->sc_flags |= IOP_ONLINE;
    604  1.66        ad 	rv = kthread_create(PRI_NONE, 0, NULL, iop_reconf_thread, sc,
    605  1.66        ad 	    &sc->sc_reconf_thread, "%s", sc->sc_dv.dv_xname);
    606  1.65        ad 	mutex_exit(&sc->sc_conflock);
    607  1.11        ad  	if (rv != 0) {
    608   1.9        ad 		printf("%s: unable to create reconfiguration thread (%d)",
    609  1.11        ad  		    sc->sc_dv.dv_xname, rv);
    610  1.11        ad  		return;
    611  1.11        ad  	}
    612   1.5        ad }
    613   1.5        ad 
    614   1.5        ad /*
    615   1.5        ad  * Reconfiguration thread; listens for LCT change notification, and
    616  1.14       wiz  * initiates re-configuration if received.
    617   1.5        ad  */
    618   1.5        ad static void
    619   1.9        ad iop_reconf_thread(void *cookie)
    620   1.5        ad {
    621   1.5        ad 	struct iop_softc *sc;
    622  1.34   thorpej 	struct lwp *l;
    623   1.5        ad 	struct i2o_lct lct;
    624   1.5        ad 	u_int32_t chgind;
    625  1.11        ad 	int rv;
    626   1.5        ad 
    627   1.5        ad 	sc = cookie;
    628  1.11        ad 	chgind = sc->sc_chgind + 1;
    629  1.34   thorpej 	l = curlwp;
    630   1.5        ad 
    631   1.5        ad 	for (;;) {
    632  1.11        ad 		DPRINTF(("%s: async reconfig: requested 0x%08x\n",
    633  1.11        ad 		    sc->sc_dv.dv_xname, chgind));
    634   1.5        ad 
    635  1.11        ad 		rv = iop_lct_get0(sc, &lct, sizeof(lct), chgind);
    636  1.11        ad 
    637  1.11        ad 		DPRINTF(("%s: async reconfig: notified (0x%08x, %d)\n",
    638  1.11        ad 		    sc->sc_dv.dv_xname, le32toh(lct.changeindicator), rv));
    639  1.11        ad 
    640  1.65        ad 		mutex_enter(&sc->sc_conflock);
    641  1.63        ad 		if (rv == 0) {
    642  1.11        ad 			iop_reconfigure(sc, le32toh(lct.changeindicator));
    643  1.11        ad 			chgind = sc->sc_chgind + 1;
    644   1.5        ad 		}
    645  1.65        ad 		(void)cv_timedwait(&sc->sc_confcv, &sc->sc_conflock, hz * 5);
    646  1.65        ad 		mutex_exit(&sc->sc_conflock);
    647   1.5        ad 	}
    648   1.5        ad }
    649   1.5        ad 
    650   1.5        ad /*
    651   1.5        ad  * Reconfigure: find new and removed devices.
    652   1.5        ad  */
    653  1.18        ad int
    654  1.11        ad iop_reconfigure(struct iop_softc *sc, u_int chgind)
    655   1.5        ad {
    656   1.5        ad 	struct iop_msg *im;
    657  1.11        ad 	struct i2o_hba_bus_scan mf;
    658   1.5        ad 	struct i2o_lct_entry *le;
    659   1.5        ad 	struct iop_initiator *ii, *nextii;
    660   1.5        ad 	int rv, tid, i;
    661   1.5        ad 
    662  1.65        ad 	KASSERT(mutex_owned(&sc->sc_conflock));
    663  1.65        ad 
    664   1.1        ad 	/*
    665   1.5        ad 	 * If the reconfiguration request isn't the result of LCT change
    666   1.5        ad 	 * notification, then be more thorough: ask all bus ports to scan
    667   1.5        ad 	 * their busses.  Wait up to 5 minutes for each bus port to complete
    668   1.5        ad 	 * the request.
    669   1.1        ad 	 */
    670   1.5        ad 	if (chgind == 0) {
    671   1.5        ad 		if ((rv = iop_lct_get(sc)) != 0) {
    672   1.5        ad 			DPRINTF(("iop_reconfigure: unable to read LCT\n"));
    673  1.11        ad 			return (rv);
    674   1.5        ad 		}
    675   1.5        ad 
    676   1.5        ad 		le = sc->sc_lct->entry;
    677   1.5        ad 		for (i = 0; i < sc->sc_nlctent; i++, le++) {
    678   1.5        ad 			if ((le16toh(le->classid) & 4095) !=
    679   1.5        ad 			    I2O_CLASS_BUS_ADAPTER_PORT)
    680   1.5        ad 				continue;
    681  1.15        ad 			tid = le16toh(le->localtid) & 4095;
    682   1.5        ad 
    683  1.15        ad 			im = iop_msg_alloc(sc, IM_WAIT);
    684   1.5        ad 
    685  1.11        ad 			mf.msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    686  1.11        ad 			mf.msgfunc = I2O_MSGFUNC(tid, I2O_HBA_BUS_SCAN);
    687  1.11        ad 			mf.msgictx = IOP_ICTX;
    688  1.11        ad 			mf.msgtctx = im->im_tctx;
    689   1.5        ad 
    690   1.5        ad 			DPRINTF(("%s: scanning bus %d\n", sc->sc_dv.dv_xname,
    691   1.5        ad 			    tid));
    692   1.5        ad 
    693  1.11        ad 			rv = iop_msg_post(sc, im, &mf, 5*60*1000);
    694  1.11        ad 			iop_msg_free(sc, im);
    695  1.11        ad #ifdef I2ODEBUG
    696  1.11        ad 			if (rv != 0)
    697  1.11        ad 				printf("%s: bus scan failed\n",
    698  1.11        ad 				    sc->sc_dv.dv_xname);
    699  1.11        ad #endif
    700   1.5        ad 		}
    701  1.11        ad 	} else if (chgind <= sc->sc_chgind) {
    702   1.5        ad 		DPRINTF(("%s: LCT unchanged (async)\n", sc->sc_dv.dv_xname));
    703  1.11        ad 		return (0);
    704   1.5        ad 	}
    705   1.5        ad 
    706   1.5        ad 	/* Re-read the LCT and determine if it has changed. */
    707   1.5        ad 	if ((rv = iop_lct_get(sc)) != 0) {
    708   1.5        ad 		DPRINTF(("iop_reconfigure: unable to re-read LCT\n"));
    709  1.11        ad 		return (rv);
    710   1.5        ad 	}
    711   1.5        ad 	DPRINTF(("%s: %d LCT entries\n", sc->sc_dv.dv_xname, sc->sc_nlctent));
    712   1.5        ad 
    713  1.11        ad 	chgind = le32toh(sc->sc_lct->changeindicator);
    714  1.11        ad 	if (chgind == sc->sc_chgind) {
    715   1.5        ad 		DPRINTF(("%s: LCT unchanged\n", sc->sc_dv.dv_xname));
    716  1.11        ad 		return (0);
    717   1.5        ad 	}
    718   1.5        ad 	DPRINTF(("%s: LCT changed\n", sc->sc_dv.dv_xname));
    719  1.11        ad 	sc->sc_chgind = chgind;
    720   1.5        ad 
    721   1.5        ad 	if (sc->sc_tidmap != NULL)
    722   1.5        ad 		free(sc->sc_tidmap, M_DEVBUF);
    723   1.5        ad 	sc->sc_tidmap = malloc(sc->sc_nlctent * sizeof(struct iop_tidmap),
    724  1.22   tsutsui 	    M_DEVBUF, M_NOWAIT|M_ZERO);
    725   1.5        ad 
    726  1.11        ad 	/* Allow 1 queued command per device while we're configuring. */
    727  1.11        ad 	iop_adjqparam(sc, 1);
    728  1.11        ad 
    729  1.11        ad 	/*
    730  1.11        ad 	 * Match and attach child devices.  We configure high-level devices
    731  1.11        ad 	 * first so that any claims will propagate throughout the LCT,
    732  1.11        ad 	 * hopefully masking off aliased devices as a result.
    733  1.11        ad 	 *
    734  1.11        ad 	 * Re-reading the LCT at this point is a little dangerous, but we'll
    735  1.11        ad 	 * trust the IOP (and the operator) to behave itself...
    736  1.11        ad 	 */
    737  1.11        ad 	iop_configure_devices(sc, IC_CONFIGURE | IC_PRIORITY,
    738  1.11        ad 	    IC_CONFIGURE | IC_PRIORITY);
    739  1.58  christos 	if ((rv = iop_lct_get(sc)) != 0) {
    740  1.11        ad 		DPRINTF(("iop_reconfigure: unable to re-read LCT\n"));
    741  1.58  christos 	}
    742  1.11        ad 	iop_configure_devices(sc, IC_CONFIGURE | IC_PRIORITY,
    743  1.11        ad 	    IC_CONFIGURE);
    744   1.5        ad 
    745   1.5        ad 	for (ii = LIST_FIRST(&sc->sc_iilist); ii != NULL; ii = nextii) {
    746  1.11        ad 		nextii = LIST_NEXT(ii, ii_list);
    747   1.5        ad 
    748   1.5        ad 		/* Detach devices that were configured, but are now gone. */
    749   1.5        ad 		for (i = 0; i < sc->sc_nlctent; i++)
    750   1.5        ad 			if (ii->ii_tid == sc->sc_tidmap[i].it_tid)
    751   1.5        ad 				break;
    752   1.5        ad 		if (i == sc->sc_nlctent ||
    753  1.52    bouyer 		    (sc->sc_tidmap[i].it_flags & IT_CONFIGURED) == 0) {
    754   1.5        ad 			config_detach(ii->ii_dv, DETACH_FORCE);
    755  1.52    bouyer 			continue;
    756  1.52    bouyer 		}
    757   1.5        ad 
    758   1.5        ad 		/*
    759   1.5        ad 		 * Tell initiators that existed before the re-configuration
    760   1.5        ad 		 * to re-configure.
    761   1.5        ad 		 */
    762   1.5        ad 		if (ii->ii_reconfig == NULL)
    763   1.5        ad 			continue;
    764   1.5        ad 		if ((rv = (*ii->ii_reconfig)(ii->ii_dv)) != 0)
    765   1.5        ad 			printf("%s: %s failed reconfigure (%d)\n",
    766   1.5        ad 			    sc->sc_dv.dv_xname, ii->ii_dv->dv_xname, rv);
    767   1.5        ad 	}
    768   1.5        ad 
    769  1.11        ad 	/* Re-adjust queue parameters and return. */
    770  1.11        ad 	if (sc->sc_nii != 0)
    771  1.11        ad 		iop_adjqparam(sc, (sc->sc_maxib - sc->sc_nuii - IOP_MF_RESERVE)
    772  1.11        ad 		    / sc->sc_nii);
    773  1.11        ad 
    774  1.11        ad 	return (0);
    775   1.1        ad }
    776   1.1        ad 
    777   1.1        ad /*
    778   1.5        ad  * Configure I2O devices into the system.
    779   1.1        ad  */
    780   1.1        ad static void
    781  1.11        ad iop_configure_devices(struct iop_softc *sc, int mask, int maskval)
    782   1.1        ad {
    783   1.1        ad 	struct iop_attach_args ia;
    784   1.5        ad 	struct iop_initiator *ii;
    785   1.1        ad 	const struct i2o_lct_entry *le;
    786   1.9        ad 	struct device *dv;
    787   1.8        ad 	int i, j, nent;
    788  1.11        ad 	u_int usertid;
    789  1.49  drochner 	int locs[IOPCF_NLOCS];
    790   1.1        ad 
    791   1.1        ad 	nent = sc->sc_nlctent;
    792   1.1        ad 	for (i = 0, le = sc->sc_lct->entry; i < nent; i++, le++) {
    793  1.15        ad 		sc->sc_tidmap[i].it_tid = le16toh(le->localtid) & 4095;
    794   1.9        ad 
    795  1.11        ad 		/* Ignore the device if it's in use. */
    796  1.11        ad 		usertid = le32toh(le->usertid) & 4095;
    797  1.11        ad 		if (usertid != I2O_TID_NONE && usertid != I2O_TID_HOST)
    798   1.1        ad 			continue;
    799   1.1        ad 
    800   1.1        ad 		ia.ia_class = le16toh(le->classid) & 4095;
    801   1.9        ad 		ia.ia_tid = sc->sc_tidmap[i].it_tid;
    802   1.8        ad 
    803   1.8        ad 		/* Ignore uninteresting devices. */
    804   1.8        ad 		for (j = 0; j < sizeof(iop_class) / sizeof(iop_class[0]); j++)
    805   1.8        ad 			if (iop_class[j].ic_class == ia.ia_class)
    806   1.8        ad 				break;
    807   1.8        ad 		if (j < sizeof(iop_class) / sizeof(iop_class[0]) &&
    808  1.11        ad 		    (iop_class[j].ic_flags & mask) != maskval)
    809   1.8        ad 			continue;
    810   1.1        ad 
    811   1.1        ad 		/*
    812   1.5        ad 		 * Try to configure the device only if it's not already
    813   1.5        ad 		 * configured.
    814   1.1        ad  		 */
    815   1.7        ad  		LIST_FOREACH(ii, &sc->sc_iilist, ii_list) {
    816   1.9        ad  			if (ia.ia_tid == ii->ii_tid) {
    817   1.9        ad 				sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
    818   1.9        ad 				strcpy(sc->sc_tidmap[i].it_dvname,
    819   1.9        ad 				    ii->ii_dv->dv_xname);
    820  1.11        ad  				break;
    821   1.9        ad 			}
    822   1.7        ad 		}
    823   1.5        ad 		if (ii != NULL)
    824   1.5        ad 			continue;
    825   1.5        ad 
    826  1.49  drochner 		locs[IOPCF_TID] = ia.ia_tid;
    827  1.44  drochner 
    828  1.49  drochner 		dv = config_found_sm_loc(&sc->sc_dv, "iop", locs, &ia,
    829  1.50  drochner 					 iop_print, config_stdsubmatch);
    830   1.9        ad 		if (dv != NULL) {
    831  1.11        ad  			sc->sc_tidmap[i].it_flags |= IT_CONFIGURED;
    832   1.9        ad 			strcpy(sc->sc_tidmap[i].it_dvname, dv->dv_xname);
    833   1.9        ad 		}
    834   1.1        ad 	}
    835   1.1        ad }
    836   1.1        ad 
    837  1.11        ad /*
    838  1.11        ad  * Adjust queue parameters for all child devices.
    839  1.11        ad  */
    840  1.11        ad static void
    841  1.11        ad iop_adjqparam(struct iop_softc *sc, int mpi)
    842  1.11        ad {
    843  1.11        ad 	struct iop_initiator *ii;
    844  1.11        ad 
    845  1.11        ad 	LIST_FOREACH(ii, &sc->sc_iilist, ii_list)
    846  1.11        ad 		if (ii->ii_adjqparam != NULL)
    847  1.11        ad 			(*ii->ii_adjqparam)(ii->ii_dv, mpi);
    848  1.11        ad }
    849  1.11        ad 
    850   1.1        ad static void
    851  1.43    itojun iop_devinfo(int class, char *devinfo, size_t l)
    852   1.1        ad {
    853   1.1        ad 	int i;
    854   1.1        ad 
    855   1.1        ad 	for (i = 0; i < sizeof(iop_class) / sizeof(iop_class[0]); i++)
    856   1.1        ad 		if (class == iop_class[i].ic_class)
    857   1.1        ad 			break;
    858  1.47     perry 
    859   1.1        ad 	if (i == sizeof(iop_class) / sizeof(iop_class[0]))
    860  1.43    itojun 		snprintf(devinfo, l, "device (class 0x%x)", class);
    861   1.1        ad 	else
    862  1.43    itojun 		strlcpy(devinfo, iop_class[i].ic_caption, l);
    863   1.1        ad }
    864   1.1        ad 
    865   1.1        ad static int
    866   1.1        ad iop_print(void *aux, const char *pnp)
    867   1.1        ad {
    868   1.1        ad 	struct iop_attach_args *ia;
    869   1.1        ad 	char devinfo[256];
    870   1.1        ad 
    871   1.1        ad 	ia = aux;
    872   1.1        ad 
    873   1.1        ad 	if (pnp != NULL) {
    874  1.43    itojun 		iop_devinfo(ia->ia_class, devinfo, sizeof(devinfo));
    875  1.33   thorpej 		aprint_normal("%s at %s", devinfo, pnp);
    876   1.1        ad 	}
    877  1.33   thorpej 	aprint_normal(" tid %d", ia->ia_tid);
    878   1.1        ad 	return (UNCONF);
    879   1.1        ad }
    880   1.1        ad 
    881   1.1        ad /*
    882   1.1        ad  * Shut down all configured IOPs.
    883  1.47     perry  */
    884   1.1        ad static void
    885  1.61  christos iop_shutdown(void *junk)
    886   1.1        ad {
    887   1.1        ad 	struct iop_softc *sc;
    888   1.1        ad 	int i;
    889   1.1        ad 
    890  1.11        ad 	printf("shutting down iop devices...");
    891   1.1        ad 
    892   1.1        ad 	for (i = 0; i < iop_cd.cd_ndevs; i++) {
    893   1.1        ad 		if ((sc = device_lookup(&iop_cd, i)) == NULL)
    894   1.1        ad 			continue;
    895   1.5        ad 		if ((sc->sc_flags & IOP_ONLINE) == 0)
    896   1.5        ad 			continue;
    897  1.27        ad 
    898   1.5        ad 		iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_SYS_QUIESCE, IOP_ICTX,
    899  1.12        ad 		    0, 5000);
    900  1.27        ad 
    901  1.27        ad 		if (le16toh(sc->sc_status.orgid) != I2O_ORG_AMI) {
    902  1.27        ad 			/*
    903  1.27        ad 			 * Some AMI firmware revisions will go to sleep and
    904  1.27        ad 			 * never come back after this.
    905  1.27        ad 			 */
    906  1.27        ad 			iop_simple_cmd(sc, I2O_TID_IOP, I2O_EXEC_IOP_CLEAR,
    907  1.27        ad 			    IOP_ICTX, 0, 1000);
    908  1.27        ad 		}
    909   1.1        ad 	}
    910   1.1        ad 
    911   1.1        ad 	/* Wait.  Some boards could still be flushing, stupidly enough. */
    912   1.1        ad 	delay(5000*1000);
    913  1.18        ad 	printf(" done\n");
    914   1.1        ad }
    915   1.1        ad 
    916   1.1        ad /*
    917  1.11        ad  * Retrieve IOP status.
    918   1.1        ad  */
    919  1.18        ad int
    920  1.11        ad iop_status_get(struct iop_softc *sc, int nosleep)
    921   1.1        ad {
    922  1.11        ad 	struct i2o_exec_status_get mf;
    923  1.15        ad 	struct i2o_status *st;
    924  1.15        ad 	paddr_t pa;
    925  1.11        ad 	int rv, i;
    926   1.1        ad 
    927  1.15        ad 	pa = sc->sc_scr_seg->ds_addr;
    928  1.15        ad 	st = (struct i2o_status *)sc->sc_scr;
    929  1.15        ad 
    930  1.11        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_exec_status_get);
    931  1.11        ad 	mf.msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_STATUS_GET);
    932  1.11        ad 	mf.reserved[0] = 0;
    933  1.11        ad 	mf.reserved[1] = 0;
    934  1.11        ad 	mf.reserved[2] = 0;
    935  1.11        ad 	mf.reserved[3] = 0;
    936  1.15        ad 	mf.addrlow = (u_int32_t)pa;
    937  1.15        ad 	mf.addrhigh = (u_int32_t)((u_int64_t)pa >> 32);
    938  1.11        ad 	mf.length = sizeof(sc->sc_status);
    939   1.1        ad 
    940  1.15        ad 	memset(st, 0, sizeof(*st));
    941  1.15        ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*st),
    942  1.15        ad 	    BUS_DMASYNC_PREREAD);
    943   1.1        ad 
    944  1.11        ad 	if ((rv = iop_post(sc, (u_int32_t *)&mf)) != 0)
    945   1.1        ad 		return (rv);
    946   1.1        ad 
    947  1.11        ad 	for (i = 25; i != 0; i--) {
    948  1.15        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0,
    949  1.15        ad 		    sizeof(*st), BUS_DMASYNC_POSTREAD);
    950  1.15        ad 		if (st->syncbyte == 0xff)
    951  1.11        ad 			break;
    952  1.11        ad 		if (nosleep)
    953  1.11        ad 			DELAY(100*1000);
    954  1.11        ad 		else
    955  1.65        ad 			kpause("iopstat", false, hz / 10, NULL);
    956  1.11        ad 	}
    957   1.1        ad 
    958  1.21        ad 	if (st->syncbyte != 0xff) {
    959  1.21        ad 		printf("%s: STATUS_GET timed out\n", sc->sc_dv.dv_xname);
    960  1.11        ad 		rv = EIO;
    961  1.21        ad 	} else {
    962  1.15        ad 		memcpy(&sc->sc_status, st, sizeof(sc->sc_status));
    963  1.11        ad 		rv = 0;
    964  1.15        ad 	}
    965  1.15        ad 
    966  1.11        ad 	return (rv);
    967   1.1        ad }
    968   1.1        ad 
    969   1.1        ad /*
    970  1.17       wiz  * Initialize and populate the IOP's outbound FIFO.
    971   1.1        ad  */
    972   1.1        ad static int
    973   1.1        ad iop_ofifo_init(struct iop_softc *sc)
    974   1.1        ad {
    975   1.1        ad 	bus_addr_t addr;
    976   1.5        ad 	bus_dma_segment_t seg;
    977  1.11        ad 	struct i2o_exec_outbound_init *mf;
    978   1.5        ad 	int i, rseg, rv;
    979  1.15        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)], *sw;
    980   1.1        ad 
    981  1.15        ad 	sw = (u_int32_t *)sc->sc_scr;
    982   1.1        ad 
    983  1.11        ad 	mf = (struct i2o_exec_outbound_init *)mb;
    984  1.11        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_outbound_init);
    985  1.11        ad 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_OUTBOUND_INIT);
    986  1.11        ad 	mf->msgictx = IOP_ICTX;
    987  1.15        ad 	mf->msgtctx = 0;
    988  1.11        ad 	mf->pagesize = PAGE_SIZE;
    989  1.19        ad 	mf->flags = IOP_INIT_CODE | ((sc->sc_framesize >> 2) << 16);
    990   1.1        ad 
    991   1.5        ad 	/*
    992   1.5        ad 	 * The I2O spec says that there are two SGLs: one for the status
    993   1.5        ad 	 * word, and one for a list of discarded MFAs.  It continues to say
    994   1.5        ad 	 * that if you don't want to get the list of MFAs, an IGNORE SGL is
    995  1.11        ad 	 * necessary; this isn't the case (and is in fact a bad thing).
    996   1.5        ad 	 */
    997  1.15        ad 	mb[sizeof(*mf) / sizeof(u_int32_t) + 0] = sizeof(*sw) |
    998  1.15        ad 	    I2O_SGL_SIMPLE | I2O_SGL_END_BUFFER | I2O_SGL_END;
    999  1.15        ad 	mb[sizeof(*mf) / sizeof(u_int32_t) + 1] =
   1000  1.15        ad 	    (u_int32_t)sc->sc_scr_seg->ds_addr;
   1001  1.15        ad 	mb[0] += 2 << 16;
   1002  1.15        ad 
   1003  1.15        ad 	*sw = 0;
   1004  1.15        ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
   1005  1.15        ad 	    BUS_DMASYNC_PREREAD);
   1006  1.15        ad 
   1007  1.15        ad 	if ((rv = iop_post(sc, mb)) != 0)
   1008   1.1        ad 		return (rv);
   1009   1.1        ad 
   1010  1.15        ad 	POLL(5000,
   1011  1.15        ad 	    (bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
   1012  1.15        ad 	    BUS_DMASYNC_POSTREAD),
   1013  1.15        ad 	    *sw == htole32(I2O_EXEC_OUTBOUND_INIT_COMPLETE)));
   1014  1.15        ad 
   1015  1.15        ad 	if (*sw != htole32(I2O_EXEC_OUTBOUND_INIT_COMPLETE)) {
   1016  1.15        ad 		printf("%s: outbound FIFO init failed (%d)\n",
   1017  1.15        ad 		    sc->sc_dv.dv_xname, le32toh(*sw));
   1018   1.5        ad 		return (EIO);
   1019   1.1        ad 	}
   1020   1.1        ad 
   1021  1.11        ad 	/* Allocate DMA safe memory for the reply frames. */
   1022   1.1        ad 	if (sc->sc_rep_phys == 0) {
   1023  1.19        ad 		sc->sc_rep_size = sc->sc_maxob * sc->sc_framesize;
   1024   1.5        ad 
   1025   1.5        ad 		rv = bus_dmamem_alloc(sc->sc_dmat, sc->sc_rep_size, PAGE_SIZE,
   1026   1.5        ad 		    0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
   1027   1.5        ad 		if (rv != 0) {
   1028  1.36       wiz 			printf("%s: DMA alloc = %d\n", sc->sc_dv.dv_xname,
   1029   1.5        ad 			   rv);
   1030   1.5        ad 			return (rv);
   1031   1.5        ad 		}
   1032   1.5        ad 
   1033   1.5        ad 		rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sc->sc_rep_size,
   1034   1.5        ad 		    &sc->sc_rep, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
   1035   1.5        ad 		if (rv != 0) {
   1036  1.36       wiz 			printf("%s: DMA map = %d\n", sc->sc_dv.dv_xname, rv);
   1037   1.5        ad 			return (rv);
   1038   1.5        ad 		}
   1039   1.5        ad 
   1040   1.5        ad 		rv = bus_dmamap_create(sc->sc_dmat, sc->sc_rep_size, 1,
   1041   1.5        ad 		    sc->sc_rep_size, 0, BUS_DMA_NOWAIT, &sc->sc_rep_dmamap);
   1042   1.5        ad 		if (rv != 0) {
   1043  1.36       wiz 			printf("%s: DMA create = %d\n", sc->sc_dv.dv_xname,
   1044  1.15        ad 			    rv);
   1045   1.5        ad 			return (rv);
   1046   1.5        ad 		}
   1047   1.5        ad 
   1048  1.15        ad 		rv = bus_dmamap_load(sc->sc_dmat, sc->sc_rep_dmamap,
   1049  1.15        ad 		    sc->sc_rep, sc->sc_rep_size, NULL, BUS_DMA_NOWAIT);
   1050   1.5        ad 		if (rv != 0) {
   1051  1.36       wiz 			printf("%s: DMA load = %d\n", sc->sc_dv.dv_xname, rv);
   1052   1.5        ad 			return (rv);
   1053   1.5        ad 		}
   1054   1.5        ad 
   1055   1.5        ad 		sc->sc_rep_phys = sc->sc_rep_dmamap->dm_segs[0].ds_addr;
   1056  1.65        ad 
   1057  1.65        ad 		/* Now safe to sync the reply map. */
   1058  1.65        ad 		sc->sc_curib = 0;
   1059   1.1        ad 	}
   1060   1.1        ad 
   1061   1.1        ad 	/* Populate the outbound FIFO. */
   1062  1.11        ad 	for (i = sc->sc_maxob, addr = sc->sc_rep_phys; i != 0; i--) {
   1063   1.5        ad 		iop_outl(sc, IOP_REG_OFIFO, (u_int32_t)addr);
   1064  1.19        ad 		addr += sc->sc_framesize;
   1065   1.1        ad 	}
   1066   1.1        ad 
   1067   1.1        ad 	return (0);
   1068   1.1        ad }
   1069   1.1        ad 
   1070   1.1        ad /*
   1071   1.1        ad  * Read the specified number of bytes from the IOP's hardware resource table.
   1072   1.1        ad  */
   1073   1.1        ad static int
   1074   1.1        ad iop_hrt_get0(struct iop_softc *sc, struct i2o_hrt *hrt, int size)
   1075   1.1        ad {
   1076   1.1        ad 	struct iop_msg *im;
   1077   1.1        ad 	int rv;
   1078  1.11        ad 	struct i2o_exec_hrt_get *mf;
   1079  1.11        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1080   1.1        ad 
   1081  1.15        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   1082  1.11        ad 	mf = (struct i2o_exec_hrt_get *)mb;
   1083  1.11        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_hrt_get);
   1084  1.11        ad 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_HRT_GET);
   1085  1.11        ad 	mf->msgictx = IOP_ICTX;
   1086  1.11        ad 	mf->msgtctx = im->im_tctx;
   1087   1.1        ad 
   1088  1.15        ad 	iop_msg_map(sc, im, mb, hrt, size, 0, NULL);
   1089  1.11        ad 	rv = iop_msg_post(sc, im, mb, 30000);
   1090   1.1        ad 	iop_msg_unmap(sc, im);
   1091  1.11        ad 	iop_msg_free(sc, im);
   1092   1.1        ad 	return (rv);
   1093   1.1        ad }
   1094   1.1        ad 
   1095   1.1        ad /*
   1096   1.5        ad  * Read the IOP's hardware resource table.
   1097   1.1        ad  */
   1098   1.1        ad static int
   1099   1.1        ad iop_hrt_get(struct iop_softc *sc)
   1100   1.1        ad {
   1101   1.1        ad 	struct i2o_hrt hrthdr, *hrt;
   1102   1.1        ad 	int size, rv;
   1103   1.1        ad 
   1104  1.66        ad 	uvm_lwp_hold(curlwp);
   1105  1.11        ad 	rv = iop_hrt_get0(sc, &hrthdr, sizeof(hrthdr));
   1106  1.66        ad 	uvm_lwp_rele(curlwp);
   1107  1.11        ad 	if (rv != 0)
   1108   1.1        ad 		return (rv);
   1109   1.1        ad 
   1110   1.5        ad 	DPRINTF(("%s: %d hrt entries\n", sc->sc_dv.dv_xname,
   1111   1.5        ad 	    le16toh(hrthdr.numentries)));
   1112   1.5        ad 
   1113  1.47     perry 	size = sizeof(struct i2o_hrt) +
   1114  1.15        ad 	    (le16toh(hrthdr.numentries) - 1) * sizeof(struct i2o_hrt_entry);
   1115   1.1        ad 	hrt = (struct i2o_hrt *)malloc(size, M_DEVBUF, M_NOWAIT);
   1116   1.1        ad 
   1117   1.1        ad 	if ((rv = iop_hrt_get0(sc, hrt, size)) != 0) {
   1118   1.1        ad 		free(hrt, M_DEVBUF);
   1119   1.1        ad 		return (rv);
   1120   1.1        ad 	}
   1121   1.1        ad 
   1122   1.1        ad 	if (sc->sc_hrt != NULL)
   1123   1.1        ad 		free(sc->sc_hrt, M_DEVBUF);
   1124   1.1        ad 	sc->sc_hrt = hrt;
   1125   1.1        ad 	return (0);
   1126   1.1        ad }
   1127   1.1        ad 
   1128   1.1        ad /*
   1129   1.1        ad  * Request the specified number of bytes from the IOP's logical
   1130   1.5        ad  * configuration table.  If a change indicator is specified, this
   1131  1.11        ad  * is a verbatim notification request, so the caller is prepared
   1132   1.5        ad  * to wait indefinitely.
   1133   1.1        ad  */
   1134   1.1        ad static int
   1135   1.5        ad iop_lct_get0(struct iop_softc *sc, struct i2o_lct *lct, int size,
   1136   1.5        ad 	     u_int32_t chgind)
   1137   1.1        ad {
   1138   1.1        ad 	struct iop_msg *im;
   1139  1.11        ad 	struct i2o_exec_lct_notify *mf;
   1140   1.1        ad 	int rv;
   1141  1.11        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1142   1.1        ad 
   1143  1.15        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   1144   1.1        ad 	memset(lct, 0, size);
   1145   1.1        ad 
   1146  1.11        ad 	mf = (struct i2o_exec_lct_notify *)mb;
   1147  1.11        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_lct_notify);
   1148  1.11        ad 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_LCT_NOTIFY);
   1149  1.11        ad 	mf->msgictx = IOP_ICTX;
   1150  1.11        ad 	mf->msgtctx = im->im_tctx;
   1151  1.11        ad 	mf->classid = I2O_CLASS_ANY;
   1152  1.11        ad 	mf->changeindicator = chgind;
   1153   1.5        ad 
   1154   1.9        ad #ifdef I2ODEBUG
   1155   1.9        ad 	printf("iop_lct_get0: reading LCT");
   1156   1.9        ad 	if (chgind != 0)
   1157   1.9        ad 		printf(" (async)");
   1158   1.9        ad 	printf("\n");
   1159   1.9        ad #endif
   1160   1.1        ad 
   1161  1.15        ad 	iop_msg_map(sc, im, mb, lct, size, 0, NULL);
   1162  1.11        ad 	rv = iop_msg_post(sc, im, mb, (chgind == 0 ? 120*1000 : 0));
   1163   1.1        ad 	iop_msg_unmap(sc, im);
   1164  1.11        ad 	iop_msg_free(sc, im);
   1165   1.1        ad 	return (rv);
   1166   1.1        ad }
   1167   1.1        ad 
   1168   1.1        ad /*
   1169   1.6        ad  * Read the IOP's logical configuration table.
   1170   1.1        ad  */
   1171   1.1        ad int
   1172   1.1        ad iop_lct_get(struct iop_softc *sc)
   1173   1.1        ad {
   1174   1.5        ad 	int esize, size, rv;
   1175   1.5        ad 	struct i2o_lct *lct;
   1176   1.1        ad 
   1177   1.5        ad 	esize = le32toh(sc->sc_status.expectedlctsize);
   1178   1.5        ad 	lct = (struct i2o_lct *)malloc(esize, M_DEVBUF, M_WAITOK);
   1179   1.5        ad 	if (lct == NULL)
   1180   1.1        ad 		return (ENOMEM);
   1181   1.1        ad 
   1182   1.5        ad 	if ((rv = iop_lct_get0(sc, lct, esize, 0)) != 0) {
   1183   1.1        ad 		free(lct, M_DEVBUF);
   1184   1.1        ad 		return (rv);
   1185   1.1        ad 	}
   1186   1.1        ad 
   1187   1.5        ad 	size = le16toh(lct->tablesize) << 2;
   1188   1.5        ad 	if (esize != size) {
   1189   1.1        ad 		free(lct, M_DEVBUF);
   1190   1.5        ad 		lct = (struct i2o_lct *)malloc(size, M_DEVBUF, M_WAITOK);
   1191   1.5        ad 		if (lct == NULL)
   1192   1.5        ad 			return (ENOMEM);
   1193   1.5        ad 
   1194   1.5        ad 		if ((rv = iop_lct_get0(sc, lct, size, 0)) != 0) {
   1195   1.5        ad 			free(lct, M_DEVBUF);
   1196   1.5        ad 			return (rv);
   1197   1.5        ad 		}
   1198   1.1        ad 	}
   1199   1.5        ad 
   1200   1.5        ad 	/* Swap in the new LCT. */
   1201   1.1        ad 	if (sc->sc_lct != NULL)
   1202   1.1        ad 		free(sc->sc_lct, M_DEVBUF);
   1203   1.1        ad 	sc->sc_lct = lct;
   1204   1.1        ad 	sc->sc_nlctent = ((le16toh(sc->sc_lct->tablesize) << 2) -
   1205   1.1        ad 	    sizeof(struct i2o_lct) + sizeof(struct i2o_lct_entry)) /
   1206   1.1        ad 	    sizeof(struct i2o_lct_entry);
   1207   1.1        ad 	return (0);
   1208   1.1        ad }
   1209   1.1        ad 
   1210   1.1        ad /*
   1211  1.42        ad  * Post a SYS_ENABLE message to the adapter.
   1212  1.42        ad  */
   1213  1.42        ad int
   1214  1.42        ad iop_sys_enable(struct iop_softc *sc)
   1215  1.42        ad {
   1216  1.42        ad 	struct iop_msg *im;
   1217  1.42        ad 	struct i2o_msg mf;
   1218  1.42        ad 	int rv;
   1219  1.42        ad 
   1220  1.42        ad 	im = iop_msg_alloc(sc, IM_WAIT | IM_NOSTATUS);
   1221  1.42        ad 
   1222  1.42        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_msg);
   1223  1.42        ad 	mf.msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_ENABLE);
   1224  1.42        ad 	mf.msgictx = IOP_ICTX;
   1225  1.42        ad 	mf.msgtctx = im->im_tctx;
   1226  1.42        ad 
   1227  1.42        ad 	rv = iop_msg_post(sc, im, &mf, 30000);
   1228  1.42        ad 	if (rv == 0) {
   1229  1.42        ad 		if ((im->im_flags & IM_FAIL) != 0)
   1230  1.42        ad 			rv = ENXIO;
   1231  1.42        ad 		else if (im->im_reqstatus == I2O_STATUS_SUCCESS ||
   1232  1.42        ad 		    (im->im_reqstatus == I2O_STATUS_ERROR_NO_DATA_XFER &&
   1233  1.42        ad 		    im->im_detstatus == I2O_DSC_INVALID_REQUEST))
   1234  1.42        ad 			rv = 0;
   1235  1.42        ad 		else
   1236  1.42        ad 			rv = EIO;
   1237  1.42        ad 	}
   1238  1.42        ad 
   1239  1.42        ad 	iop_msg_free(sc, im);
   1240  1.42        ad 	return (rv);
   1241  1.42        ad }
   1242  1.42        ad 
   1243  1.42        ad /*
   1244  1.11        ad  * Request the specified parameter group from the target.  If an initiator
   1245  1.11        ad  * is specified (a) don't wait for the operation to complete, but instead
   1246  1.11        ad  * let the initiator's interrupt handler deal with the reply and (b) place a
   1247  1.11        ad  * pointer to the parameter group op in the wrapper's `im_dvcontext' field.
   1248   1.1        ad  */
   1249   1.1        ad int
   1250  1.16        ad iop_field_get_all(struct iop_softc *sc, int tid, int group, void *buf,
   1251  1.16        ad 		  int size, struct iop_initiator *ii)
   1252   1.1        ad {
   1253   1.1        ad 	struct iop_msg *im;
   1254  1.11        ad 	struct i2o_util_params_op *mf;
   1255  1.16        ad 	int rv;
   1256  1.11        ad 	struct iop_pgop *pgop;
   1257  1.11        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1258   1.1        ad 
   1259  1.15        ad 	im = iop_msg_alloc(sc, (ii == NULL ? IM_WAIT : 0) | IM_NOSTATUS);
   1260  1.11        ad 	if ((pgop = malloc(sizeof(*pgop), M_DEVBUF, M_WAITOK)) == NULL) {
   1261  1.11        ad 		iop_msg_free(sc, im);
   1262  1.11        ad 		return (ENOMEM);
   1263  1.11        ad 	}
   1264  1.11        ad 	im->im_dvcontext = pgop;
   1265   1.1        ad 
   1266  1.11        ad 	mf = (struct i2o_util_params_op *)mb;
   1267  1.11        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1268  1.16        ad 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_GET);
   1269  1.11        ad 	mf->msgictx = IOP_ICTX;
   1270  1.11        ad 	mf->msgtctx = im->im_tctx;
   1271  1.11        ad 	mf->flags = 0;
   1272  1.11        ad 
   1273  1.11        ad 	pgop->olh.count = htole16(1);
   1274  1.11        ad 	pgop->olh.reserved = htole16(0);
   1275  1.16        ad 	pgop->oat.operation = htole16(I2O_PARAMS_OP_FIELD_GET);
   1276  1.11        ad 	pgop->oat.fieldcount = htole16(0xffff);
   1277  1.11        ad 	pgop->oat.group = htole16(group);
   1278  1.11        ad 
   1279  1.11        ad 	if (ii == NULL)
   1280  1.66        ad 		uvm_lwp_hold(curlwp);
   1281   1.1        ad 
   1282   1.5        ad 	memset(buf, 0, size);
   1283  1.15        ad 	iop_msg_map(sc, im, mb, pgop, sizeof(*pgop), 1, NULL);
   1284  1.16        ad 	iop_msg_map(sc, im, mb, buf, size, 0, NULL);
   1285  1.11        ad 	rv = iop_msg_post(sc, im, mb, (ii == NULL ? 30000 : 0));
   1286  1.11        ad 
   1287  1.11        ad 	if (ii == NULL)
   1288  1.66        ad 		uvm_lwp_rele(curlwp);
   1289  1.11        ad 
   1290  1.11        ad 	/* Detect errors; let partial transfers to count as success. */
   1291  1.11        ad 	if (ii == NULL && rv == 0) {
   1292  1.42        ad 		if (im->im_reqstatus == I2O_STATUS_ERROR_PARTIAL_XFER &&
   1293  1.42        ad 		    im->im_detstatus == I2O_DSC_UNKNOWN_ERROR)
   1294  1.11        ad 			rv = 0;
   1295  1.11        ad 		else
   1296  1.42        ad 			rv = (im->im_reqstatus != 0 ? EIO : 0);
   1297  1.16        ad 
   1298  1.16        ad 		if (rv != 0)
   1299  1.16        ad 			printf("%s: FIELD_GET failed for tid %d group %d\n",
   1300  1.16        ad 			    sc->sc_dv.dv_xname, tid, group);
   1301  1.11        ad 	}
   1302  1.11        ad 
   1303  1.11        ad 	if (ii == NULL || rv != 0) {
   1304  1.11        ad 		iop_msg_unmap(sc, im);
   1305  1.11        ad 		iop_msg_free(sc, im);
   1306  1.11        ad 		free(pgop, M_DEVBUF);
   1307  1.11        ad 	}
   1308   1.1        ad 
   1309   1.1        ad 	return (rv);
   1310  1.11        ad }
   1311   1.1        ad 
   1312   1.1        ad /*
   1313  1.16        ad  * Set a single field in a scalar parameter group.
   1314  1.16        ad  */
   1315  1.16        ad int
   1316  1.16        ad iop_field_set(struct iop_softc *sc, int tid, int group, void *buf,
   1317  1.16        ad 	      int size, int field)
   1318  1.16        ad {
   1319  1.16        ad 	struct iop_msg *im;
   1320  1.16        ad 	struct i2o_util_params_op *mf;
   1321  1.16        ad 	struct iop_pgop *pgop;
   1322  1.16        ad 	int rv, totsize;
   1323  1.16        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1324  1.16        ad 
   1325  1.16        ad 	totsize = sizeof(*pgop) + size;
   1326  1.16        ad 
   1327  1.16        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   1328  1.16        ad 	if ((pgop = malloc(totsize, M_DEVBUF, M_WAITOK)) == NULL) {
   1329  1.16        ad 		iop_msg_free(sc, im);
   1330  1.16        ad 		return (ENOMEM);
   1331  1.16        ad 	}
   1332  1.16        ad 
   1333  1.16        ad 	mf = (struct i2o_util_params_op *)mb;
   1334  1.16        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1335  1.16        ad 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_SET);
   1336  1.16        ad 	mf->msgictx = IOP_ICTX;
   1337  1.16        ad 	mf->msgtctx = im->im_tctx;
   1338  1.16        ad 	mf->flags = 0;
   1339  1.16        ad 
   1340  1.16        ad 	pgop->olh.count = htole16(1);
   1341  1.16        ad 	pgop->olh.reserved = htole16(0);
   1342  1.16        ad 	pgop->oat.operation = htole16(I2O_PARAMS_OP_FIELD_SET);
   1343  1.16        ad 	pgop->oat.fieldcount = htole16(1);
   1344  1.16        ad 	pgop->oat.group = htole16(group);
   1345  1.16        ad 	pgop->oat.fields[0] = htole16(field);
   1346  1.16        ad 	memcpy(pgop + 1, buf, size);
   1347  1.16        ad 
   1348  1.16        ad 	iop_msg_map(sc, im, mb, pgop, totsize, 1, NULL);
   1349  1.16        ad 	rv = iop_msg_post(sc, im, mb, 30000);
   1350  1.16        ad 	if (rv != 0)
   1351  1.16        ad 		printf("%s: FIELD_SET failed for tid %d group %d\n",
   1352  1.16        ad 		    sc->sc_dv.dv_xname, tid, group);
   1353  1.16        ad 
   1354  1.16        ad 	iop_msg_unmap(sc, im);
   1355  1.16        ad 	iop_msg_free(sc, im);
   1356  1.16        ad 	free(pgop, M_DEVBUF);
   1357  1.16        ad 	return (rv);
   1358  1.16        ad }
   1359  1.16        ad 
   1360  1.16        ad /*
   1361  1.16        ad  * Delete all rows in a tablular parameter group.
   1362  1.16        ad  */
   1363  1.16        ad int
   1364  1.16        ad iop_table_clear(struct iop_softc *sc, int tid, int group)
   1365  1.16        ad {
   1366  1.16        ad 	struct iop_msg *im;
   1367  1.16        ad 	struct i2o_util_params_op *mf;
   1368  1.16        ad 	struct iop_pgop pgop;
   1369  1.16        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1370  1.16        ad 	int rv;
   1371  1.16        ad 
   1372  1.16        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   1373  1.16        ad 
   1374  1.16        ad 	mf = (struct i2o_util_params_op *)mb;
   1375  1.16        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1376  1.16        ad 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_SET);
   1377  1.16        ad 	mf->msgictx = IOP_ICTX;
   1378  1.16        ad 	mf->msgtctx = im->im_tctx;
   1379  1.16        ad 	mf->flags = 0;
   1380  1.16        ad 
   1381  1.16        ad 	pgop.olh.count = htole16(1);
   1382  1.16        ad 	pgop.olh.reserved = htole16(0);
   1383  1.16        ad 	pgop.oat.operation = htole16(I2O_PARAMS_OP_TABLE_CLEAR);
   1384  1.16        ad 	pgop.oat.fieldcount = htole16(0);
   1385  1.16        ad 	pgop.oat.group = htole16(group);
   1386  1.16        ad 	pgop.oat.fields[0] = htole16(0);
   1387  1.16        ad 
   1388  1.66        ad 	uvm_lwp_hold(curlwp);
   1389  1.16        ad 	iop_msg_map(sc, im, mb, &pgop, sizeof(pgop), 1, NULL);
   1390  1.16        ad 	rv = iop_msg_post(sc, im, mb, 30000);
   1391  1.16        ad 	if (rv != 0)
   1392  1.16        ad 		printf("%s: TABLE_CLEAR failed for tid %d group %d\n",
   1393  1.16        ad 		    sc->sc_dv.dv_xname, tid, group);
   1394  1.16        ad 
   1395  1.16        ad 	iop_msg_unmap(sc, im);
   1396  1.66        ad 	uvm_lwp_rele(curlwp);
   1397  1.16        ad 	iop_msg_free(sc, im);
   1398  1.16        ad 	return (rv);
   1399  1.16        ad }
   1400  1.16        ad 
   1401  1.16        ad /*
   1402  1.16        ad  * Add a single row to a tabular parameter group.  The row can have only one
   1403  1.16        ad  * field.
   1404  1.16        ad  */
   1405  1.16        ad int
   1406  1.16        ad iop_table_add_row(struct iop_softc *sc, int tid, int group, void *buf,
   1407  1.16        ad 		  int size, int row)
   1408  1.16        ad {
   1409  1.16        ad 	struct iop_msg *im;
   1410  1.16        ad 	struct i2o_util_params_op *mf;
   1411  1.16        ad 	struct iop_pgop *pgop;
   1412  1.16        ad 	int rv, totsize;
   1413  1.16        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1414  1.16        ad 
   1415  1.16        ad 	totsize = sizeof(*pgop) + sizeof(u_int16_t) * 2 + size;
   1416  1.16        ad 
   1417  1.16        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   1418  1.16        ad 	if ((pgop = malloc(totsize, M_DEVBUF, M_WAITOK)) == NULL) {
   1419  1.16        ad 		iop_msg_free(sc, im);
   1420  1.16        ad 		return (ENOMEM);
   1421  1.16        ad 	}
   1422  1.16        ad 
   1423  1.16        ad 	mf = (struct i2o_util_params_op *)mb;
   1424  1.16        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_util_params_op);
   1425  1.16        ad 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_UTIL_PARAMS_SET);
   1426  1.16        ad 	mf->msgictx = IOP_ICTX;
   1427  1.16        ad 	mf->msgtctx = im->im_tctx;
   1428  1.16        ad 	mf->flags = 0;
   1429  1.16        ad 
   1430  1.16        ad 	pgop->olh.count = htole16(1);
   1431  1.16        ad 	pgop->olh.reserved = htole16(0);
   1432  1.16        ad 	pgop->oat.operation = htole16(I2O_PARAMS_OP_ROW_ADD);
   1433  1.16        ad 	pgop->oat.fieldcount = htole16(1);
   1434  1.16        ad 	pgop->oat.group = htole16(group);
   1435  1.16        ad 	pgop->oat.fields[0] = htole16(0);	/* FieldIdx */
   1436  1.16        ad 	pgop->oat.fields[1] = htole16(1);	/* RowCount */
   1437  1.16        ad 	pgop->oat.fields[2] = htole16(row);	/* KeyValue */
   1438  1.16        ad 	memcpy(&pgop->oat.fields[3], buf, size);
   1439  1.16        ad 
   1440  1.16        ad 	iop_msg_map(sc, im, mb, pgop, totsize, 1, NULL);
   1441  1.16        ad 	rv = iop_msg_post(sc, im, mb, 30000);
   1442  1.16        ad 	if (rv != 0)
   1443  1.16        ad 		printf("%s: ADD_ROW failed for tid %d group %d row %d\n",
   1444  1.16        ad 		    sc->sc_dv.dv_xname, tid, group, row);
   1445  1.16        ad 
   1446  1.16        ad 	iop_msg_unmap(sc, im);
   1447  1.16        ad 	iop_msg_free(sc, im);
   1448  1.16        ad 	free(pgop, M_DEVBUF);
   1449  1.16        ad 	return (rv);
   1450  1.16        ad }
   1451  1.16        ad 
   1452  1.16        ad /*
   1453   1.5        ad  * Execute a simple command (no parameters).
   1454   1.1        ad  */
   1455   1.1        ad int
   1456   1.5        ad iop_simple_cmd(struct iop_softc *sc, int tid, int function, int ictx,
   1457   1.5        ad 	       int async, int timo)
   1458   1.1        ad {
   1459   1.1        ad 	struct iop_msg *im;
   1460  1.11        ad 	struct i2o_msg mf;
   1461   1.5        ad 	int rv, fl;
   1462   1.1        ad 
   1463  1.11        ad 	fl = (async != 0 ? IM_WAIT : IM_POLL);
   1464  1.15        ad 	im = iop_msg_alloc(sc, fl);
   1465   1.1        ad 
   1466  1.11        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_msg);
   1467  1.11        ad 	mf.msgfunc = I2O_MSGFUNC(tid, function);
   1468  1.11        ad 	mf.msgictx = ictx;
   1469  1.11        ad 	mf.msgtctx = im->im_tctx;
   1470   1.1        ad 
   1471  1.11        ad 	rv = iop_msg_post(sc, im, &mf, timo);
   1472  1.11        ad 	iop_msg_free(sc, im);
   1473   1.1        ad 	return (rv);
   1474   1.1        ad }
   1475   1.1        ad 
   1476   1.1        ad /*
   1477   1.5        ad  * Post the system table to the IOP.
   1478   1.1        ad  */
   1479   1.1        ad static int
   1480   1.1        ad iop_systab_set(struct iop_softc *sc)
   1481   1.1        ad {
   1482  1.11        ad 	struct i2o_exec_sys_tab_set *mf;
   1483   1.1        ad 	struct iop_msg *im;
   1484  1.13        ad 	bus_space_handle_t bsh;
   1485  1.13        ad 	bus_addr_t boo;
   1486   1.1        ad 	u_int32_t mema[2], ioa[2];
   1487   1.1        ad 	int rv;
   1488  1.11        ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
   1489   1.1        ad 
   1490  1.15        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   1491   1.1        ad 
   1492  1.11        ad 	mf = (struct i2o_exec_sys_tab_set *)mb;
   1493  1.11        ad 	mf->msgflags = I2O_MSGFLAGS(i2o_exec_sys_tab_set);
   1494  1.11        ad 	mf->msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_SYS_TAB_SET);
   1495  1.11        ad 	mf->msgictx = IOP_ICTX;
   1496  1.11        ad 	mf->msgtctx = im->im_tctx;
   1497  1.53   thorpej 	mf->iopid = (device_unit(&sc->sc_dv) + 2) << 12;
   1498  1.11        ad 	mf->segnumber = 0;
   1499   1.5        ad 
   1500  1.13        ad 	mema[1] = sc->sc_status.desiredprivmemsize;
   1501  1.13        ad 	ioa[1] = sc->sc_status.desiredpriviosize;
   1502  1.13        ad 
   1503  1.13        ad 	if (mema[1] != 0) {
   1504  1.13        ad 		rv = bus_space_alloc(sc->sc_bus_memt, 0, 0xffffffff,
   1505  1.13        ad 		    le32toh(mema[1]), PAGE_SIZE, 0, 0, &boo, &bsh);
   1506  1.13        ad 		mema[0] = htole32(boo);
   1507  1.13        ad 		if (rv != 0) {
   1508  1.13        ad 			printf("%s: can't alloc priv mem space, err = %d\n",
   1509  1.13        ad 			    sc->sc_dv.dv_xname, rv);
   1510  1.13        ad 			mema[0] = 0;
   1511  1.13        ad 			mema[1] = 0;
   1512  1.13        ad 		}
   1513  1.13        ad 	}
   1514  1.13        ad 
   1515  1.13        ad 	if (ioa[1] != 0) {
   1516  1.13        ad 		rv = bus_space_alloc(sc->sc_bus_iot, 0, 0xffff,
   1517  1.13        ad 		    le32toh(ioa[1]), 0, 0, 0, &boo, &bsh);
   1518  1.13        ad 		ioa[0] = htole32(boo);
   1519  1.13        ad 		if (rv != 0) {
   1520  1.13        ad 			printf("%s: can't alloc priv i/o space, err = %d\n",
   1521  1.13        ad 			    sc->sc_dv.dv_xname, rv);
   1522  1.13        ad 			ioa[0] = 0;
   1523  1.13        ad 			ioa[1] = 0;
   1524  1.13        ad 		}
   1525  1.13        ad 	}
   1526   1.1        ad 
   1527  1.66        ad 	uvm_lwp_hold(curlwp);
   1528  1.15        ad 	iop_msg_map(sc, im, mb, iop_systab, iop_systab_size, 1, NULL);
   1529  1.15        ad 	iop_msg_map(sc, im, mb, mema, sizeof(mema), 1, NULL);
   1530  1.15        ad 	iop_msg_map(sc, im, mb, ioa, sizeof(ioa), 1, NULL);
   1531  1.11        ad 	rv = iop_msg_post(sc, im, mb, 5000);
   1532   1.1        ad 	iop_msg_unmap(sc, im);
   1533  1.11        ad 	iop_msg_free(sc, im);
   1534  1.66        ad 	uvm_lwp_rele(curlwp);
   1535   1.1        ad 	return (rv);
   1536   1.1        ad }
   1537   1.1        ad 
   1538   1.1        ad /*
   1539  1.11        ad  * Reset the IOP.  Must be called with interrupts disabled.
   1540   1.1        ad  */
   1541   1.1        ad static int
   1542   1.1        ad iop_reset(struct iop_softc *sc)
   1543   1.1        ad {
   1544  1.15        ad 	u_int32_t mfa, *sw;
   1545  1.11        ad 	struct i2o_exec_iop_reset mf;
   1546   1.1        ad 	int rv;
   1547  1.15        ad 	paddr_t pa;
   1548   1.1        ad 
   1549  1.15        ad 	sw = (u_int32_t *)sc->sc_scr;
   1550  1.15        ad 	pa = sc->sc_scr_seg->ds_addr;
   1551   1.1        ad 
   1552  1.11        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_exec_iop_reset);
   1553  1.11        ad 	mf.msgfunc = I2O_MSGFUNC(I2O_TID_IOP, I2O_EXEC_IOP_RESET);
   1554  1.11        ad 	mf.reserved[0] = 0;
   1555  1.11        ad 	mf.reserved[1] = 0;
   1556  1.11        ad 	mf.reserved[2] = 0;
   1557  1.11        ad 	mf.reserved[3] = 0;
   1558  1.15        ad 	mf.statuslow = (u_int32_t)pa;
   1559  1.15        ad 	mf.statushigh = (u_int32_t)((u_int64_t)pa >> 32);
   1560  1.15        ad 
   1561  1.15        ad 	*sw = htole32(0);
   1562  1.15        ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
   1563  1.15        ad 	    BUS_DMASYNC_PREREAD);
   1564   1.1        ad 
   1565  1.11        ad 	if ((rv = iop_post(sc, (u_int32_t *)&mf)))
   1566   1.1        ad 		return (rv);
   1567   1.1        ad 
   1568  1.15        ad 	POLL(2500,
   1569  1.15        ad 	    (bus_dmamap_sync(sc->sc_dmat, sc->sc_scr_dmamap, 0, sizeof(*sw),
   1570  1.15        ad 	    BUS_DMASYNC_POSTREAD), *sw != 0));
   1571  1.15        ad 	if (*sw != htole32(I2O_RESET_IN_PROGRESS)) {
   1572  1.15        ad 		printf("%s: reset rejected, status 0x%x\n",
   1573  1.15        ad 		    sc->sc_dv.dv_xname, le32toh(*sw));
   1574   1.1        ad 		return (EIO);
   1575   1.1        ad 	}
   1576   1.1        ad 
   1577  1.47     perry 	/*
   1578   1.5        ad 	 * IOP is now in the INIT state.  Wait no more than 10 seconds for
   1579   1.1        ad 	 * the inbound queue to become responsive.
   1580   1.1        ad 	 */
   1581   1.5        ad 	POLL(10000, (mfa = iop_inl(sc, IOP_REG_IFIFO)) != IOP_MFA_EMPTY);
   1582   1.1        ad 	if (mfa == IOP_MFA_EMPTY) {
   1583   1.1        ad 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
   1584   1.1        ad 		return (EIO);
   1585   1.1        ad 	}
   1586   1.1        ad 
   1587   1.1        ad 	iop_release_mfa(sc, mfa);
   1588   1.1        ad 	return (0);
   1589   1.1        ad }
   1590   1.1        ad 
   1591   1.1        ad /*
   1592  1.11        ad  * Register a new initiator.  Must be called with the configuration lock
   1593  1.11        ad  * held.
   1594   1.1        ad  */
   1595  1.11        ad void
   1596   1.1        ad iop_initiator_register(struct iop_softc *sc, struct iop_initiator *ii)
   1597   1.1        ad {
   1598  1.11        ad 	static int ictxgen;
   1599   1.5        ad 
   1600  1.11        ad 	/* 0 is reserved (by us) for system messages. */
   1601  1.11        ad 	ii->ii_ictx = ++ictxgen;
   1602   1.1        ad 
   1603  1.11        ad 	/*
   1604  1.11        ad 	 * `Utility initiators' don't make it onto the per-IOP initiator list
   1605  1.11        ad 	 * (which is used only for configuration), but do get one slot on
   1606  1.11        ad 	 * the inbound queue.
   1607  1.11        ad 	 */
   1608  1.11        ad 	if ((ii->ii_flags & II_UTILITY) == 0) {
   1609  1.11        ad 		LIST_INSERT_HEAD(&sc->sc_iilist, ii, ii_list);
   1610  1.11        ad 		sc->sc_nii++;
   1611  1.11        ad 	} else
   1612  1.11        ad 		sc->sc_nuii++;
   1613  1.11        ad 
   1614  1.65        ad 	cv_init(&ii->ii_cv, "iopevt");
   1615  1.65        ad 
   1616  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   1617   1.5        ad 	LIST_INSERT_HEAD(IOP_ICTXHASH(ii->ii_ictx), ii, ii_hash);
   1618  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   1619   1.1        ad }
   1620   1.1        ad 
   1621   1.1        ad /*
   1622  1.11        ad  * Unregister an initiator.  Must be called with the configuration lock
   1623  1.11        ad  * held.
   1624   1.1        ad  */
   1625   1.1        ad void
   1626   1.1        ad iop_initiator_unregister(struct iop_softc *sc, struct iop_initiator *ii)
   1627   1.1        ad {
   1628  1.11        ad 
   1629  1.11        ad 	if ((ii->ii_flags & II_UTILITY) == 0) {
   1630  1.11        ad 		LIST_REMOVE(ii, ii_list);
   1631  1.11        ad 		sc->sc_nii--;
   1632  1.11        ad 	} else
   1633  1.11        ad 		sc->sc_nuii--;
   1634   1.1        ad 
   1635  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   1636   1.5        ad 	LIST_REMOVE(ii, ii_hash);
   1637  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   1638  1.65        ad 
   1639  1.65        ad 	cv_destroy(&ii->ii_cv);
   1640   1.1        ad }
   1641   1.1        ad 
   1642   1.1        ad /*
   1643  1.11        ad  * Handle a reply frame from the IOP.
   1644   1.1        ad  */
   1645   1.1        ad static int
   1646   1.5        ad iop_handle_reply(struct iop_softc *sc, u_int32_t rmfa)
   1647   1.1        ad {
   1648   1.1        ad 	struct iop_msg *im;
   1649   1.1        ad 	struct i2o_reply *rb;
   1650  1.11        ad 	struct i2o_fault_notify *fn;
   1651   1.1        ad 	struct iop_initiator *ii;
   1652   1.5        ad 	u_int off, ictx, tctx, status, size;
   1653   1.1        ad 
   1654  1.65        ad 	KASSERT(mutex_owned(&sc->sc_intrlock));
   1655  1.65        ad 
   1656   1.1        ad 	off = (int)(rmfa - sc->sc_rep_phys);
   1657  1.64  christos 	rb = (struct i2o_reply *)((char *)sc->sc_rep + off);
   1658   1.1        ad 
   1659  1.15        ad 	/* Perform reply queue DMA synchronisation. */
   1660  1.11        ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, off,
   1661  1.19        ad 	    sc->sc_framesize, BUS_DMASYNC_POSTREAD);
   1662  1.11        ad 	if (--sc->sc_curib != 0)
   1663   1.1        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap,
   1664   1.1        ad 		    0, sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   1665   1.1        ad 
   1666   1.1        ad #ifdef I2ODEBUG
   1667   1.1        ad 	if ((le32toh(rb->msgflags) & I2O_MSGFLAGS_64BIT) != 0)
   1668   1.5        ad 		panic("iop_handle_reply: 64-bit reply");
   1669   1.1        ad #endif
   1670  1.47     perry 	/*
   1671   1.1        ad 	 * Find the initiator.
   1672   1.1        ad 	 */
   1673   1.1        ad 	ictx = le32toh(rb->msgictx);
   1674   1.1        ad 	if (ictx == IOP_ICTX)
   1675   1.1        ad 		ii = NULL;
   1676   1.1        ad 	else {
   1677   1.5        ad 		ii = LIST_FIRST(IOP_ICTXHASH(ictx));
   1678   1.5        ad 		for (; ii != NULL; ii = LIST_NEXT(ii, ii_hash))
   1679   1.5        ad 			if (ii->ii_ictx == ictx)
   1680   1.5        ad 				break;
   1681   1.5        ad 		if (ii == NULL) {
   1682   1.1        ad #ifdef I2ODEBUG
   1683  1.11        ad 			iop_reply_print(sc, rb);
   1684   1.1        ad #endif
   1685  1.11        ad 			printf("%s: WARNING: bad ictx returned (%x)\n",
   1686   1.5        ad 			    sc->sc_dv.dv_xname, ictx);
   1687   1.5        ad 			return (-1);
   1688   1.5        ad 		}
   1689   1.1        ad 	}
   1690   1.1        ad 
   1691  1.11        ad 	/*
   1692  1.14       wiz 	 * If we received a transport failure notice, we've got to dig the
   1693  1.11        ad 	 * transaction context (if any) out of the original message frame,
   1694  1.11        ad 	 * and then release the original MFA back to the inbound FIFO.
   1695  1.11        ad 	 */
   1696  1.11        ad 	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) {
   1697  1.11        ad 		status = I2O_STATUS_SUCCESS;
   1698  1.11        ad 
   1699  1.11        ad 		fn = (struct i2o_fault_notify *)rb;
   1700  1.29   msaitoh 		tctx = iop_inl_msg(sc, fn->lowmfa + 12);
   1701  1.11        ad 		iop_release_mfa(sc, fn->lowmfa);
   1702  1.11        ad 		iop_tfn_print(sc, fn);
   1703  1.11        ad 	} else {
   1704  1.11        ad 		status = rb->reqstatus;
   1705  1.11        ad 		tctx = le32toh(rb->msgtctx);
   1706  1.11        ad 	}
   1707   1.1        ad 
   1708  1.15        ad 	if (ii == NULL || (ii->ii_flags & II_NOTCTX) == 0) {
   1709   1.1        ad 		/*
   1710   1.1        ad 		 * This initiator tracks state using message wrappers.
   1711   1.1        ad 		 *
   1712   1.1        ad 		 * Find the originating message wrapper, and if requested
   1713   1.1        ad 		 * notify the initiator.
   1714   1.1        ad 		 */
   1715  1.11        ad 		im = sc->sc_ims + (tctx & IOP_TCTX_MASK);
   1716  1.11        ad 		if ((tctx & IOP_TCTX_MASK) > sc->sc_maxib ||
   1717  1.11        ad 		    (im->im_flags & IM_ALLOCED) == 0 ||
   1718  1.11        ad 		    tctx != im->im_tctx) {
   1719  1.11        ad 			printf("%s: WARNING: bad tctx returned (0x%08x, %p)\n",
   1720  1.11        ad 			    sc->sc_dv.dv_xname, tctx, im);
   1721  1.11        ad 			if (im != NULL)
   1722  1.11        ad 				printf("%s: flags=0x%08x tctx=0x%08x\n",
   1723  1.11        ad 				    sc->sc_dv.dv_xname, im->im_flags,
   1724  1.11        ad 				    im->im_tctx);
   1725   1.5        ad #ifdef I2ODEBUG
   1726  1.11        ad 			if ((rb->msgflags & I2O_MSGFLAGS_FAIL) == 0)
   1727  1.11        ad 				iop_reply_print(sc, rb);
   1728   1.5        ad #endif
   1729   1.5        ad 			return (-1);
   1730   1.5        ad 		}
   1731  1.11        ad 
   1732  1.11        ad 		if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0)
   1733  1.11        ad 			im->im_flags |= IM_FAIL;
   1734  1.11        ad 
   1735   1.1        ad #ifdef I2ODEBUG
   1736   1.1        ad 		if ((im->im_flags & IM_REPLIED) != 0)
   1737   1.5        ad 			panic("%s: dup reply", sc->sc_dv.dv_xname);
   1738   1.1        ad #endif
   1739  1.11        ad 		im->im_flags |= IM_REPLIED;
   1740   1.1        ad 
   1741  1.11        ad #ifdef I2ODEBUG
   1742  1.11        ad 		if (status != I2O_STATUS_SUCCESS)
   1743  1.11        ad 			iop_reply_print(sc, rb);
   1744  1.11        ad #endif
   1745  1.11        ad 		im->im_reqstatus = status;
   1746  1.42        ad 		im->im_detstatus = le16toh(rb->detail);
   1747   1.1        ad 
   1748  1.11        ad 		/* Copy the reply frame, if requested. */
   1749  1.11        ad 		if (im->im_rb != NULL) {
   1750  1.11        ad 			size = (le32toh(rb->msgflags) >> 14) & ~3;
   1751   1.1        ad #ifdef I2ODEBUG
   1752  1.19        ad 			if (size > sc->sc_framesize)
   1753  1.11        ad 				panic("iop_handle_reply: reply too large");
   1754   1.1        ad #endif
   1755  1.11        ad 			memcpy(im->im_rb, rb, size);
   1756  1.11        ad 		}
   1757  1.11        ad 
   1758   1.1        ad 		/* Notify the initiator. */
   1759  1.11        ad 		if ((im->im_flags & IM_WAIT) != 0)
   1760  1.65        ad 			cv_broadcast(&im->im_cv);
   1761  1.55  christos 		else if ((im->im_flags & (IM_POLL | IM_POLL_INTR)) != IM_POLL) {
   1762  1.65        ad 			if (ii != NULL) {
   1763  1.65        ad 				mutex_spin_exit(&sc->sc_intrlock);
   1764  1.55  christos 				(*ii->ii_intr)(ii->ii_dv, im, rb);
   1765  1.65        ad 				mutex_spin_enter(&sc->sc_intrlock);
   1766  1.65        ad 			}
   1767  1.55  christos 		}
   1768   1.1        ad 	} else {
   1769   1.1        ad 		/*
   1770   1.1        ad 		 * This initiator discards message wrappers.
   1771   1.1        ad 		 *
   1772   1.1        ad 		 * Simply pass the reply frame to the initiator.
   1773   1.1        ad 		 */
   1774  1.65        ad 		if (ii != NULL) {
   1775  1.65        ad 			mutex_spin_exit(&sc->sc_intrlock);
   1776  1.55  christos 			(*ii->ii_intr)(ii->ii_dv, NULL, rb);
   1777  1.65        ad 			mutex_spin_enter(&sc->sc_intrlock);
   1778  1.65        ad 		}
   1779   1.1        ad 	}
   1780   1.1        ad 
   1781   1.1        ad 	return (status);
   1782   1.1        ad }
   1783   1.1        ad 
   1784   1.1        ad /*
   1785  1.11        ad  * Handle an interrupt from the IOP.
   1786   1.1        ad  */
   1787   1.1        ad int
   1788   1.1        ad iop_intr(void *arg)
   1789   1.1        ad {
   1790   1.1        ad 	struct iop_softc *sc;
   1791   1.5        ad 	u_int32_t rmfa;
   1792   1.1        ad 
   1793   1.1        ad 	sc = arg;
   1794   1.1        ad 
   1795  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   1796  1.65        ad 
   1797  1.65        ad 	if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) == 0) {
   1798  1.65        ad 		mutex_spin_exit(&sc->sc_intrlock);
   1799   1.5        ad 		return (0);
   1800  1.65        ad 	}
   1801   1.5        ad 
   1802   1.5        ad 	for (;;) {
   1803   1.5        ad 		/* Double read to account for IOP bug. */
   1804  1.11        ad 		if ((rmfa = iop_inl(sc, IOP_REG_OFIFO)) == IOP_MFA_EMPTY) {
   1805  1.11        ad 			rmfa = iop_inl(sc, IOP_REG_OFIFO);
   1806  1.11        ad 			if (rmfa == IOP_MFA_EMPTY)
   1807  1.11        ad 				break;
   1808  1.11        ad 		}
   1809   1.5        ad 		iop_handle_reply(sc, rmfa);
   1810  1.11        ad 		iop_outl(sc, IOP_REG_OFIFO, rmfa);
   1811   1.1        ad 	}
   1812   1.1        ad 
   1813  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   1814   1.5        ad 	return (1);
   1815   1.5        ad }
   1816   1.5        ad 
   1817   1.5        ad /*
   1818   1.5        ad  * Handle an event signalled by the executive.
   1819   1.5        ad  */
   1820   1.5        ad static void
   1821  1.61  christos iop_intr_event(struct device *dv, struct iop_msg *im, void *reply)
   1822   1.5        ad {
   1823   1.5        ad 	struct i2o_util_event_register_reply *rb;
   1824   1.5        ad 	u_int event;
   1825   1.5        ad 
   1826   1.5        ad 	rb = reply;
   1827   1.5        ad 
   1828  1.11        ad 	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0)
   1829   1.5        ad 		return;
   1830   1.5        ad 
   1831  1.11        ad 	event = le32toh(rb->event);
   1832   1.5        ad 	printf("%s: event 0x%08x received\n", dv->dv_xname, event);
   1833   1.1        ad }
   1834   1.1        ad 
   1835  1.47     perry /*
   1836   1.1        ad  * Allocate a message wrapper.
   1837   1.1        ad  */
   1838  1.11        ad struct iop_msg *
   1839  1.15        ad iop_msg_alloc(struct iop_softc *sc, int flags)
   1840   1.1        ad {
   1841   1.1        ad 	struct iop_msg *im;
   1842  1.11        ad 	static u_int tctxgen;
   1843  1.65        ad 	int i;
   1844   1.1        ad 
   1845   1.1        ad #ifdef I2ODEBUG
   1846   1.1        ad 	if ((flags & IM_SYSMASK) != 0)
   1847   1.1        ad 		panic("iop_msg_alloc: system flags specified");
   1848   1.1        ad #endif
   1849   1.1        ad 
   1850  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   1851  1.11        ad 	im = SLIST_FIRST(&sc->sc_im_freelist);
   1852  1.11        ad #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
   1853  1.11        ad 	if (im == NULL)
   1854  1.11        ad 		panic("iop_msg_alloc: no free wrappers");
   1855  1.11        ad #endif
   1856  1.11        ad 	SLIST_REMOVE_HEAD(&sc->sc_im_freelist, im_chain);
   1857  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   1858   1.1        ad 
   1859  1.11        ad 	im->im_tctx = (im->im_tctx & IOP_TCTX_MASK) | tctxgen;
   1860  1.11        ad 	tctxgen += (1 << IOP_TCTX_SHIFT);
   1861   1.1        ad 	im->im_flags = flags | IM_ALLOCED;
   1862  1.11        ad 	im->im_rb = NULL;
   1863  1.11        ad 	i = 0;
   1864  1.11        ad 	do {
   1865  1.11        ad 		im->im_xfer[i++].ix_size = 0;
   1866  1.11        ad 	} while (i < IOP_MAX_MSG_XFERS);
   1867   1.1        ad 
   1868  1.11        ad 	return (im);
   1869   1.1        ad }
   1870   1.1        ad 
   1871  1.47     perry /*
   1872   1.1        ad  * Free a message wrapper.
   1873   1.1        ad  */
   1874   1.1        ad void
   1875  1.11        ad iop_msg_free(struct iop_softc *sc, struct iop_msg *im)
   1876   1.1        ad {
   1877   1.1        ad 
   1878   1.1        ad #ifdef I2ODEBUG
   1879   1.1        ad 	if ((im->im_flags & IM_ALLOCED) == 0)
   1880   1.1        ad 		panic("iop_msg_free: wrapper not allocated");
   1881   1.1        ad #endif
   1882   1.1        ad 
   1883   1.1        ad 	im->im_flags = 0;
   1884  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   1885  1.11        ad 	SLIST_INSERT_HEAD(&sc->sc_im_freelist, im, im_chain);
   1886  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   1887   1.1        ad }
   1888   1.1        ad 
   1889   1.1        ad /*
   1890  1.47     perry  * Map a data transfer.  Write a scatter-gather list into the message frame.
   1891   1.1        ad  */
   1892   1.1        ad int
   1893  1.11        ad iop_msg_map(struct iop_softc *sc, struct iop_msg *im, u_int32_t *mb,
   1894  1.15        ad 	    void *xferaddr, int xfersize, int out, struct proc *up)
   1895   1.1        ad {
   1896  1.11        ad 	bus_dmamap_t dm;
   1897  1.11        ad 	bus_dma_segment_t *ds;
   1898   1.1        ad 	struct iop_xfer *ix;
   1899  1.11        ad 	u_int rv, i, nsegs, flg, off, xn;
   1900  1.11        ad 	u_int32_t *p;
   1901   1.5        ad 
   1902  1.11        ad 	for (xn = 0, ix = im->im_xfer; xn < IOP_MAX_MSG_XFERS; xn++, ix++)
   1903   1.1        ad 		if (ix->ix_size == 0)
   1904   1.1        ad 			break;
   1905  1.11        ad 
   1906   1.1        ad #ifdef I2ODEBUG
   1907  1.11        ad 	if (xfersize == 0)
   1908  1.11        ad 		panic("iop_msg_map: null transfer");
   1909  1.11        ad 	if (xfersize > IOP_MAX_XFER)
   1910  1.11        ad 		panic("iop_msg_map: transfer too large");
   1911  1.11        ad 	if (xn == IOP_MAX_MSG_XFERS)
   1912   1.1        ad 		panic("iop_msg_map: too many xfers");
   1913   1.1        ad #endif
   1914   1.1        ad 
   1915  1.11        ad 	/*
   1916  1.11        ad 	 * Only the first DMA map is static.
   1917  1.11        ad 	 */
   1918  1.11        ad 	if (xn != 0) {
   1919   1.1        ad 		rv = bus_dmamap_create(sc->sc_dmat, IOP_MAX_XFER,
   1920  1.11        ad 		    IOP_MAX_SEGS, IOP_MAX_XFER, 0,
   1921   1.1        ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ix->ix_map);
   1922   1.1        ad 		if (rv != 0)
   1923   1.1        ad 			return (rv);
   1924   1.1        ad 	}
   1925   1.1        ad 
   1926  1.11        ad 	dm = ix->ix_map;
   1927  1.15        ad 	rv = bus_dmamap_load(sc->sc_dmat, dm, xferaddr, xfersize, up,
   1928  1.15        ad 	    (up == NULL ? BUS_DMA_NOWAIT : 0));
   1929  1.11        ad 	if (rv != 0)
   1930  1.11        ad 		goto bad;
   1931  1.11        ad 
   1932  1.11        ad 	/*
   1933  1.11        ad 	 * How many SIMPLE SG elements can we fit in this message?
   1934  1.11        ad 	 */
   1935  1.11        ad 	off = mb[0] >> 16;
   1936  1.11        ad 	p = mb + off;
   1937  1.19        ad 	nsegs = ((sc->sc_framesize >> 2) - off) >> 1;
   1938  1.11        ad 
   1939  1.11        ad 	if (dm->dm_nsegs > nsegs) {
   1940  1.11        ad 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   1941  1.11        ad 		rv = EFBIG;
   1942  1.11        ad 		DPRINTF(("iop_msg_map: too many segs\n"));
   1943  1.11        ad 		goto bad;
   1944  1.11        ad 	}
   1945   1.1        ad 
   1946  1.11        ad 	nsegs = dm->dm_nsegs;
   1947  1.11        ad 	xfersize = 0;
   1948   1.1        ad 
   1949  1.11        ad 	/*
   1950  1.11        ad 	 * Write out the SG list.
   1951  1.11        ad 	 */
   1952   1.1        ad 	if (out)
   1953  1.11        ad 		flg = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   1954   1.1        ad 	else
   1955  1.11        ad 		flg = I2O_SGL_SIMPLE;
   1956   1.1        ad 
   1957  1.11        ad 	for (i = nsegs, ds = dm->dm_segs; i > 1; i--, p += 2, ds++) {
   1958  1.11        ad 		p[0] = (u_int32_t)ds->ds_len | flg;
   1959  1.11        ad 		p[1] = (u_int32_t)ds->ds_addr;
   1960  1.11        ad 		xfersize += ds->ds_len;
   1961   1.1        ad 	}
   1962   1.1        ad 
   1963  1.11        ad 	p[0] = (u_int32_t)ds->ds_len | flg | I2O_SGL_END_BUFFER;
   1964  1.11        ad 	p[1] = (u_int32_t)ds->ds_addr;
   1965  1.11        ad 	xfersize += ds->ds_len;
   1966  1.11        ad 
   1967  1.11        ad 	/* Fix up the transfer record, and sync the map. */
   1968  1.11        ad 	ix->ix_flags = (out ? IX_OUT : IX_IN);
   1969  1.11        ad 	ix->ix_size = xfersize;
   1970  1.11        ad 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   1971  1.11        ad 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   1972  1.11        ad 
   1973   1.1        ad 	/*
   1974   1.1        ad 	 * If this is the first xfer we've mapped for this message, adjust
   1975   1.1        ad 	 * the SGL offset field in the message header.
   1976   1.1        ad 	 */
   1977   1.2        ad 	if ((im->im_flags & IM_SGLOFFADJ) == 0) {
   1978  1.11        ad 		mb[0] += (mb[0] >> 12) & 0xf0;
   1979   1.2        ad 		im->im_flags |= IM_SGLOFFADJ;
   1980   1.2        ad 	}
   1981  1.11        ad 	mb[0] += (nsegs << 17);
   1982  1.11        ad 	return (0);
   1983  1.11        ad 
   1984  1.11        ad  bad:
   1985  1.11        ad  	if (xn != 0)
   1986  1.11        ad 		bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   1987  1.11        ad 	return (rv);
   1988  1.11        ad }
   1989  1.11        ad 
   1990  1.11        ad /*
   1991  1.11        ad  * Map a block I/O data transfer (different in that there's only one per
   1992  1.11        ad  * message maximum, and PAGE addressing may be used).  Write a scatter
   1993  1.11        ad  * gather list into the message frame.
   1994  1.11        ad  */
   1995  1.11        ad int
   1996  1.11        ad iop_msg_map_bio(struct iop_softc *sc, struct iop_msg *im, u_int32_t *mb,
   1997  1.11        ad 		void *xferaddr, int xfersize, int out)
   1998  1.11        ad {
   1999  1.11        ad 	bus_dma_segment_t *ds;
   2000  1.11        ad 	bus_dmamap_t dm;
   2001  1.11        ad 	struct iop_xfer *ix;
   2002  1.11        ad 	u_int rv, i, nsegs, off, slen, tlen, flg;
   2003  1.11        ad 	paddr_t saddr, eaddr;
   2004  1.11        ad 	u_int32_t *p;
   2005  1.11        ad 
   2006  1.11        ad #ifdef I2ODEBUG
   2007  1.11        ad 	if (xfersize == 0)
   2008  1.11        ad 		panic("iop_msg_map_bio: null transfer");
   2009  1.11        ad 	if (xfersize > IOP_MAX_XFER)
   2010  1.11        ad 		panic("iop_msg_map_bio: transfer too large");
   2011  1.11        ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   2012  1.11        ad 		panic("iop_msg_map_bio: SGLOFFADJ");
   2013  1.11        ad #endif
   2014  1.11        ad 
   2015  1.11        ad 	ix = im->im_xfer;
   2016  1.11        ad 	dm = ix->ix_map;
   2017  1.15        ad 	rv = bus_dmamap_load(sc->sc_dmat, dm, xferaddr, xfersize, NULL,
   2018  1.15        ad 	    BUS_DMA_NOWAIT | BUS_DMA_STREAMING);
   2019  1.11        ad 	if (rv != 0)
   2020  1.11        ad 		return (rv);
   2021  1.11        ad 
   2022  1.11        ad 	off = mb[0] >> 16;
   2023  1.19        ad 	nsegs = ((sc->sc_framesize >> 2) - off) >> 1;
   2024  1.11        ad 
   2025  1.11        ad 	/*
   2026  1.11        ad 	 * If the transfer is highly fragmented and won't fit using SIMPLE
   2027  1.11        ad 	 * elements, use PAGE_LIST elements instead.  SIMPLE elements are
   2028  1.11        ad 	 * potentially more efficient, both for us and the IOP.
   2029  1.11        ad 	 */
   2030  1.11        ad 	if (dm->dm_nsegs > nsegs) {
   2031  1.11        ad 		nsegs = 1;
   2032  1.11        ad 		p = mb + off + 1;
   2033  1.11        ad 
   2034  1.11        ad 		/* XXX This should be done with a bus_space flag. */
   2035  1.11        ad 		for (i = dm->dm_nsegs, ds = dm->dm_segs; i > 0; i--, ds++) {
   2036  1.11        ad 			slen = ds->ds_len;
   2037  1.11        ad 			saddr = ds->ds_addr;
   2038  1.11        ad 
   2039  1.11        ad 			while (slen > 0) {
   2040  1.11        ad 				eaddr = (saddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
   2041  1.11        ad 				tlen = min(eaddr - saddr, slen);
   2042  1.11        ad 				slen -= tlen;
   2043  1.11        ad 				*p++ = le32toh(saddr);
   2044  1.11        ad 				saddr = eaddr;
   2045  1.11        ad 				nsegs++;
   2046  1.11        ad 			}
   2047  1.11        ad 		}
   2048  1.11        ad 
   2049  1.11        ad 		mb[off] = xfersize | I2O_SGL_PAGE_LIST | I2O_SGL_END_BUFFER |
   2050  1.11        ad 		    I2O_SGL_END;
   2051  1.11        ad 		if (out)
   2052  1.11        ad 			mb[off] |= I2O_SGL_DATA_OUT;
   2053  1.11        ad 	} else {
   2054  1.11        ad 		p = mb + off;
   2055  1.13        ad 		nsegs = dm->dm_nsegs;
   2056  1.11        ad 
   2057  1.11        ad 		if (out)
   2058  1.11        ad 			flg = I2O_SGL_SIMPLE | I2O_SGL_DATA_OUT;
   2059  1.11        ad 		else
   2060  1.11        ad 			flg = I2O_SGL_SIMPLE;
   2061  1.11        ad 
   2062  1.11        ad 		for (i = nsegs, ds = dm->dm_segs; i > 1; i--, p += 2, ds++) {
   2063  1.11        ad 			p[0] = (u_int32_t)ds->ds_len | flg;
   2064  1.11        ad 			p[1] = (u_int32_t)ds->ds_addr;
   2065  1.11        ad 		}
   2066  1.11        ad 
   2067  1.11        ad 		p[0] = (u_int32_t)ds->ds_len | flg | I2O_SGL_END_BUFFER |
   2068  1.11        ad 		    I2O_SGL_END;
   2069  1.11        ad 		p[1] = (u_int32_t)ds->ds_addr;
   2070  1.11        ad 		nsegs <<= 1;
   2071  1.11        ad 	}
   2072  1.11        ad 
   2073  1.11        ad 	/* Fix up the transfer record, and sync the map. */
   2074  1.11        ad 	ix->ix_flags = (out ? IX_OUT : IX_IN);
   2075  1.11        ad 	ix->ix_size = xfersize;
   2076  1.11        ad 	bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, xfersize,
   2077  1.11        ad 	    out ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_POSTREAD);
   2078  1.11        ad 
   2079  1.11        ad 	/*
   2080  1.11        ad 	 * Adjust the SGL offset and total message size fields.  We don't
   2081  1.11        ad 	 * set IM_SGLOFFADJ, since it's used only for SIMPLE elements.
   2082  1.11        ad 	 */
   2083  1.11        ad 	mb[0] += ((off << 4) + (nsegs << 16));
   2084   1.1        ad 	return (0);
   2085   1.1        ad }
   2086   1.1        ad 
   2087   1.1        ad /*
   2088   1.1        ad  * Unmap all data transfers associated with a message wrapper.
   2089   1.1        ad  */
   2090   1.1        ad void
   2091   1.1        ad iop_msg_unmap(struct iop_softc *sc, struct iop_msg *im)
   2092   1.1        ad {
   2093   1.1        ad 	struct iop_xfer *ix;
   2094   1.1        ad 	int i;
   2095  1.11        ad 
   2096  1.47     perry #ifdef I2ODEBUG
   2097  1.11        ad 	if (im->im_xfer[0].ix_size == 0)
   2098  1.11        ad 		panic("iop_msg_unmap: no transfers mapped");
   2099  1.11        ad #endif
   2100  1.11        ad 
   2101  1.11        ad 	for (ix = im->im_xfer, i = 0;;) {
   2102   1.1        ad 		bus_dmamap_sync(sc->sc_dmat, ix->ix_map, 0, ix->ix_size,
   2103   1.1        ad 		    ix->ix_flags & IX_OUT ? BUS_DMASYNC_POSTWRITE :
   2104   1.1        ad 		    BUS_DMASYNC_POSTREAD);
   2105   1.1        ad 		bus_dmamap_unload(sc->sc_dmat, ix->ix_map);
   2106   1.1        ad 
   2107   1.1        ad 		/* Only the first DMA map is static. */
   2108   1.1        ad 		if (i != 0)
   2109   1.1        ad 			bus_dmamap_destroy(sc->sc_dmat, ix->ix_map);
   2110  1.47     perry 		if ((++ix)->ix_size == 0)
   2111  1.11        ad 			break;
   2112  1.11        ad 		if (++i >= IOP_MAX_MSG_XFERS)
   2113  1.11        ad 			break;
   2114   1.1        ad 	}
   2115   1.1        ad }
   2116   1.1        ad 
   2117  1.11        ad /*
   2118  1.11        ad  * Post a message frame to the IOP's inbound queue.
   2119   1.1        ad  */
   2120   1.1        ad int
   2121  1.11        ad iop_post(struct iop_softc *sc, u_int32_t *mb)
   2122   1.1        ad {
   2123  1.11        ad 	u_int32_t mfa;
   2124  1.11        ad 
   2125  1.15        ad #ifdef I2ODEBUG
   2126  1.19        ad 	if ((mb[0] >> 16) > (sc->sc_framesize >> 2))
   2127  1.13        ad 		panic("iop_post: frame too large");
   2128  1.15        ad #endif
   2129  1.13        ad 
   2130  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   2131  1.11        ad 
   2132  1.11        ad 	/* Allocate a slot with the IOP. */
   2133  1.11        ad 	if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY)
   2134  1.11        ad 		if ((mfa = iop_inl(sc, IOP_REG_IFIFO)) == IOP_MFA_EMPTY) {
   2135  1.65        ad 			mutex_spin_exit(&sc->sc_intrlock);
   2136  1.11        ad 			printf("%s: mfa not forthcoming\n",
   2137  1.11        ad 			    sc->sc_dv.dv_xname);
   2138  1.11        ad 			return (EAGAIN);
   2139  1.11        ad 		}
   2140  1.11        ad 
   2141  1.15        ad 	/* Perform reply buffer DMA synchronisation. */
   2142  1.11        ad 	if (sc->sc_curib++ == 0)
   2143  1.11        ad 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rep_dmamap, 0,
   2144  1.11        ad 		    sc->sc_rep_size, BUS_DMASYNC_PREREAD);
   2145   1.1        ad 
   2146  1.11        ad 	/* Copy out the message frame. */
   2147  1.30        ad 	bus_space_write_region_4(sc->sc_msg_iot, sc->sc_msg_ioh, mfa, mb,
   2148  1.29   msaitoh 	    mb[0] >> 16);
   2149  1.30        ad 	bus_space_barrier(sc->sc_msg_iot, sc->sc_msg_ioh, mfa,
   2150  1.29   msaitoh 	    (mb[0] >> 14) & ~3, BUS_SPACE_BARRIER_WRITE);
   2151  1.11        ad 
   2152  1.11        ad 	/* Post the MFA back to the IOP. */
   2153  1.11        ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   2154   1.1        ad 
   2155  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   2156  1.11        ad 	return (0);
   2157  1.11        ad }
   2158   1.1        ad 
   2159  1.11        ad /*
   2160  1.11        ad  * Post a message to the IOP and deal with completion.
   2161  1.11        ad  */
   2162  1.11        ad int
   2163  1.11        ad iop_msg_post(struct iop_softc *sc, struct iop_msg *im, void *xmb, int timo)
   2164  1.11        ad {
   2165  1.11        ad 	u_int32_t *mb;
   2166  1.65        ad 	int rv;
   2167   1.1        ad 
   2168  1.11        ad 	mb = xmb;
   2169   1.1        ad 
   2170  1.11        ad 	/* Terminate the scatter/gather list chain. */
   2171   1.1        ad 	if ((im->im_flags & IM_SGLOFFADJ) != 0)
   2172  1.11        ad 		mb[(mb[0] >> 16) - 2] |= I2O_SGL_END;
   2173   1.1        ad 
   2174  1.11        ad 	if ((rv = iop_post(sc, mb)) != 0)
   2175  1.11        ad 		return (rv);
   2176   1.1        ad 
   2177  1.15        ad 	if ((im->im_flags & (IM_POLL | IM_WAIT)) != 0) {
   2178  1.11        ad 		if ((im->im_flags & IM_POLL) != 0)
   2179  1.11        ad 			iop_msg_poll(sc, im, timo);
   2180  1.11        ad 		else
   2181  1.11        ad 			iop_msg_wait(sc, im, timo);
   2182   1.1        ad 
   2183  1.65        ad 		mutex_spin_enter(&sc->sc_intrlock);
   2184  1.11        ad 		if ((im->im_flags & IM_REPLIED) != 0) {
   2185  1.11        ad 			if ((im->im_flags & IM_NOSTATUS) != 0)
   2186  1.11        ad 				rv = 0;
   2187  1.11        ad 			else if ((im->im_flags & IM_FAIL) != 0)
   2188  1.11        ad 				rv = ENXIO;
   2189  1.11        ad 			else if (im->im_reqstatus != I2O_STATUS_SUCCESS)
   2190  1.11        ad 				rv = EIO;
   2191  1.11        ad 			else
   2192  1.11        ad 				rv = 0;
   2193  1.11        ad 		} else
   2194  1.11        ad 			rv = EBUSY;
   2195  1.65        ad 		mutex_spin_exit(&sc->sc_intrlock);
   2196  1.11        ad 	} else
   2197  1.11        ad 		rv = 0;
   2198  1.11        ad 
   2199  1.11        ad 	return (rv);
   2200  1.11        ad }
   2201  1.11        ad 
   2202  1.47     perry /*
   2203  1.11        ad  * Spin until the specified message is replied to.
   2204  1.11        ad  */
   2205  1.11        ad static void
   2206  1.11        ad iop_msg_poll(struct iop_softc *sc, struct iop_msg *im, int timo)
   2207  1.11        ad {
   2208  1.11        ad 	u_int32_t rmfa;
   2209  1.11        ad 
   2210  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   2211   1.1        ad 
   2212   1.1        ad 	for (timo *= 10; timo != 0; timo--) {
   2213   1.5        ad 		if ((iop_inl(sc, IOP_REG_INTR_STATUS) & IOP_INTR_OFIFO) != 0) {
   2214   1.5        ad 			/* Double read to account for IOP bug. */
   2215   1.5        ad 			rmfa = iop_inl(sc, IOP_REG_OFIFO);
   2216   1.5        ad 			if (rmfa == IOP_MFA_EMPTY)
   2217   1.5        ad 				rmfa = iop_inl(sc, IOP_REG_OFIFO);
   2218  1.11        ad 			if (rmfa != IOP_MFA_EMPTY) {
   2219  1.35    simonb 				iop_handle_reply(sc, rmfa);
   2220  1.11        ad 
   2221  1.11        ad 				/*
   2222  1.11        ad 				 * Return the reply frame to the IOP's
   2223  1.11        ad 				 * outbound FIFO.
   2224  1.11        ad 				 */
   2225  1.11        ad 				iop_outl(sc, IOP_REG_OFIFO, rmfa);
   2226  1.11        ad 			}
   2227   1.5        ad 		}
   2228   1.1        ad 		if ((im->im_flags & IM_REPLIED) != 0)
   2229   1.1        ad 			break;
   2230  1.65        ad 		mutex_spin_exit(&sc->sc_intrlock);
   2231   1.1        ad 		DELAY(100);
   2232  1.65        ad 		mutex_spin_enter(&sc->sc_intrlock);
   2233   1.1        ad 	}
   2234   1.1        ad 
   2235   1.1        ad 	if (timo == 0) {
   2236   1.5        ad #ifdef I2ODEBUG
   2237   1.5        ad 		printf("%s: poll - no reply\n", sc->sc_dv.dv_xname);
   2238  1.11        ad 		if (iop_status_get(sc, 1) != 0)
   2239  1.11        ad 			printf("iop_msg_poll: unable to retrieve status\n");
   2240   1.5        ad 		else
   2241  1.11        ad 			printf("iop_msg_poll: IOP state = %d\n",
   2242  1.47     perry 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   2243   1.5        ad #endif
   2244   1.1        ad 	}
   2245   1.1        ad 
   2246  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   2247   1.1        ad }
   2248   1.1        ad 
   2249   1.1        ad /*
   2250  1.11        ad  * Sleep until the specified message is replied to.
   2251   1.1        ad  */
   2252  1.11        ad static void
   2253  1.61  christos iop_msg_wait(struct iop_softc *sc, struct iop_msg *im, int timo)
   2254   1.1        ad {
   2255  1.65        ad 	int rv;
   2256   1.1        ad 
   2257  1.65        ad 	mutex_spin_enter(&sc->sc_intrlock);
   2258   1.5        ad 	if ((im->im_flags & IM_REPLIED) != 0) {
   2259  1.65        ad 		mutex_spin_exit(&sc->sc_intrlock);
   2260  1.11        ad 		return;
   2261   1.5        ad 	}
   2262  1.65        ad 	rv = cv_timedwait(&im->im_cv, &sc->sc_intrlock, mstohz(timo));
   2263  1.65        ad 	mutex_spin_exit(&sc->sc_intrlock);
   2264  1.11        ad 
   2265   1.5        ad #ifdef I2ODEBUG
   2266   1.5        ad 	if (rv != 0) {
   2267   1.5        ad 		printf("iop_msg_wait: tsleep() == %d\n", rv);
   2268  1.11        ad 		if (iop_status_get(sc, 0) != 0)
   2269   1.5        ad 			printf("iop_msg_wait: unable to retrieve status\n");
   2270   1.5        ad 		else
   2271   1.5        ad 			printf("iop_msg_wait: IOP state = %d\n",
   2272  1.47     perry 			    (le32toh(sc->sc_status.segnumber) >> 16) & 0xff);
   2273   1.5        ad 	}
   2274   1.5        ad #endif
   2275   1.1        ad }
   2276   1.1        ad 
   2277   1.1        ad /*
   2278   1.1        ad  * Release an unused message frame back to the IOP's inbound fifo.
   2279   1.1        ad  */
   2280   1.1        ad static void
   2281   1.1        ad iop_release_mfa(struct iop_softc *sc, u_int32_t mfa)
   2282   1.1        ad {
   2283   1.1        ad 
   2284   1.1        ad 	/* Use the frame to issue a no-op. */
   2285  1.30        ad 	iop_outl_msg(sc, mfa, I2O_VERSION_11 | (4 << 16));
   2286  1.30        ad 	iop_outl_msg(sc, mfa + 4, I2O_MSGFUNC(I2O_TID_IOP, I2O_UTIL_NOP));
   2287  1.30        ad 	iop_outl_msg(sc, mfa + 8, 0);
   2288  1.30        ad 	iop_outl_msg(sc, mfa + 12, 0);
   2289   1.1        ad 
   2290   1.5        ad 	iop_outl(sc, IOP_REG_IFIFO, mfa);
   2291   1.1        ad }
   2292   1.1        ad 
   2293   1.1        ad #ifdef I2ODEBUG
   2294   1.1        ad /*
   2295  1.11        ad  * Dump a reply frame header.
   2296   1.1        ad  */
   2297   1.1        ad static void
   2298  1.11        ad iop_reply_print(struct iop_softc *sc, struct i2o_reply *rb)
   2299   1.1        ad {
   2300   1.5        ad 	u_int function, detail;
   2301   1.1        ad 	const char *statusstr;
   2302   1.1        ad 
   2303   1.5        ad 	function = (le32toh(rb->msgfunc) >> 24) & 0xff;
   2304   1.1        ad 	detail = le16toh(rb->detail);
   2305   1.1        ad 
   2306   1.5        ad 	printf("%s: reply:\n", sc->sc_dv.dv_xname);
   2307   1.5        ad 
   2308   1.1        ad 	if (rb->reqstatus < sizeof(iop_status) / sizeof(iop_status[0]))
   2309   1.1        ad 		statusstr = iop_status[rb->reqstatus];
   2310   1.1        ad 	else
   2311   1.1        ad 		statusstr = "undefined error code";
   2312   1.1        ad 
   2313  1.47     perry 	printf("%s:   function=0x%02x status=0x%02x (%s)\n",
   2314   1.5        ad 	    sc->sc_dv.dv_xname, function, rb->reqstatus, statusstr);
   2315   1.5        ad 	printf("%s:   detail=0x%04x ictx=0x%08x tctx=0x%08x\n",
   2316   1.5        ad 	    sc->sc_dv.dv_xname, detail, le32toh(rb->msgictx),
   2317   1.5        ad 	    le32toh(rb->msgtctx));
   2318   1.5        ad 	printf("%s:   tidi=%d tidt=%d flags=0x%02x\n", sc->sc_dv.dv_xname,
   2319   1.5        ad 	    (le32toh(rb->msgfunc) >> 12) & 4095, le32toh(rb->msgfunc) & 4095,
   2320   1.5        ad 	    (le32toh(rb->msgflags) >> 8) & 0xff);
   2321   1.1        ad }
   2322   1.1        ad #endif
   2323   1.1        ad 
   2324   1.1        ad /*
   2325  1.11        ad  * Dump a transport failure reply.
   2326  1.11        ad  */
   2327  1.11        ad static void
   2328  1.11        ad iop_tfn_print(struct iop_softc *sc, struct i2o_fault_notify *fn)
   2329  1.11        ad {
   2330  1.11        ad 
   2331  1.11        ad 	printf("%s: WARNING: transport failure:\n", sc->sc_dv.dv_xname);
   2332  1.11        ad 
   2333  1.19        ad 	printf("%s:  ictx=0x%08x tctx=0x%08x\n", sc->sc_dv.dv_xname,
   2334  1.11        ad 	    le32toh(fn->msgictx), le32toh(fn->msgtctx));
   2335  1.11        ad 	printf("%s:  failurecode=0x%02x severity=0x%02x\n",
   2336  1.11        ad 	    sc->sc_dv.dv_xname, fn->failurecode, fn->severity);
   2337  1.11        ad 	printf("%s:  highestver=0x%02x lowestver=0x%02x\n",
   2338  1.11        ad 	    sc->sc_dv.dv_xname, fn->highestver, fn->lowestver);
   2339  1.11        ad }
   2340  1.11        ad 
   2341  1.11        ad /*
   2342   1.5        ad  * Translate an I2O ASCII field into a C string.
   2343   1.1        ad  */
   2344   1.1        ad void
   2345   1.5        ad iop_strvis(struct iop_softc *sc, const char *src, int slen, char *dst, int dlen)
   2346   1.1        ad {
   2347   1.5        ad 	int hc, lc, i, nit;
   2348   1.1        ad 
   2349   1.1        ad 	dlen--;
   2350   1.1        ad 	lc = 0;
   2351   1.1        ad 	hc = 0;
   2352   1.1        ad 	i = 0;
   2353   1.5        ad 
   2354   1.5        ad 	/*
   2355   1.5        ad 	 * DPT use NUL as a space, whereas AMI use it as a terminator.  The
   2356   1.5        ad 	 * spec has nothing to say about it.  Since AMI fields are usually
   2357   1.5        ad 	 * filled with junk after the terminator, ...
   2358   1.5        ad 	 */
   2359   1.5        ad 	nit = (le16toh(sc->sc_status.orgid) != I2O_ORG_DPT);
   2360   1.5        ad 
   2361   1.5        ad 	while (slen-- != 0 && dlen-- != 0) {
   2362   1.5        ad 		if (nit && *src == '\0')
   2363   1.5        ad 			break;
   2364   1.5        ad 		else if (*src <= 0x20 || *src >= 0x7f) {
   2365   1.1        ad 			if (hc)
   2366   1.1        ad 				dst[i++] = ' ';
   2367   1.1        ad 		} else {
   2368   1.1        ad 			hc = 1;
   2369   1.1        ad 			dst[i++] = *src;
   2370   1.1        ad 			lc = i;
   2371   1.1        ad 		}
   2372   1.1        ad 		src++;
   2373   1.1        ad 	}
   2374  1.47     perry 
   2375   1.1        ad 	dst[lc] = '\0';
   2376   1.1        ad }
   2377   1.1        ad 
   2378   1.1        ad /*
   2379  1.11        ad  * Retrieve the DEVICE_IDENTITY parameter group from the target and dump it.
   2380  1.11        ad  */
   2381  1.11        ad int
   2382  1.11        ad iop_print_ident(struct iop_softc *sc, int tid)
   2383  1.11        ad {
   2384  1.11        ad 	struct {
   2385  1.11        ad 		struct	i2o_param_op_results pr;
   2386  1.11        ad 		struct	i2o_param_read_results prr;
   2387  1.11        ad 		struct	i2o_param_device_identity di;
   2388  1.11        ad 	} __attribute__ ((__packed__)) p;
   2389  1.11        ad 	char buf[32];
   2390  1.11        ad 	int rv;
   2391  1.11        ad 
   2392  1.16        ad 	rv = iop_field_get_all(sc, tid, I2O_PARAM_DEVICE_IDENTITY, &p,
   2393  1.16        ad 	    sizeof(p), NULL);
   2394  1.11        ad 	if (rv != 0)
   2395  1.11        ad 		return (rv);
   2396  1.11        ad 
   2397  1.11        ad 	iop_strvis(sc, p.di.vendorinfo, sizeof(p.di.vendorinfo), buf,
   2398  1.11        ad 	    sizeof(buf));
   2399  1.11        ad 	printf(" <%s, ", buf);
   2400  1.11        ad 	iop_strvis(sc, p.di.productinfo, sizeof(p.di.productinfo), buf,
   2401  1.11        ad 	    sizeof(buf));
   2402  1.11        ad 	printf("%s, ", buf);
   2403  1.11        ad 	iop_strvis(sc, p.di.revlevel, sizeof(p.di.revlevel), buf, sizeof(buf));
   2404  1.11        ad 	printf("%s>", buf);
   2405  1.11        ad 
   2406  1.11        ad 	return (0);
   2407  1.11        ad }
   2408  1.11        ad 
   2409  1.11        ad /*
   2410   1.5        ad  * Claim or unclaim the specified TID.
   2411   1.1        ad  */
   2412   1.1        ad int
   2413   1.5        ad iop_util_claim(struct iop_softc *sc, struct iop_initiator *ii, int release,
   2414  1.15        ad 	       int flags)
   2415   1.1        ad {
   2416   1.5        ad 	struct iop_msg *im;
   2417  1.11        ad 	struct i2o_util_claim mf;
   2418   1.5        ad 	int rv, func;
   2419   1.5        ad 
   2420   1.5        ad 	func = release ? I2O_UTIL_CLAIM_RELEASE : I2O_UTIL_CLAIM;
   2421  1.15        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   2422   1.5        ad 
   2423  1.11        ad 	/* We can use the same structure, as they're identical. */
   2424  1.11        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_util_claim);
   2425  1.11        ad 	mf.msgfunc = I2O_MSGFUNC(ii->ii_tid, func);
   2426  1.11        ad 	mf.msgictx = ii->ii_ictx;
   2427  1.11        ad 	mf.msgtctx = im->im_tctx;
   2428  1.11        ad 	mf.flags = flags;
   2429   1.5        ad 
   2430  1.11        ad 	rv = iop_msg_post(sc, im, &mf, 5000);
   2431  1.11        ad 	iop_msg_free(sc, im);
   2432   1.5        ad 	return (rv);
   2433  1.47     perry }
   2434   1.5        ad 
   2435   1.5        ad /*
   2436   1.5        ad  * Perform an abort.
   2437   1.5        ad  */
   2438   1.5        ad int iop_util_abort(struct iop_softc *sc, struct iop_initiator *ii, int func,
   2439  1.15        ad 		   int tctxabort, int flags)
   2440   1.5        ad {
   2441   1.5        ad 	struct iop_msg *im;
   2442  1.11        ad 	struct i2o_util_abort mf;
   2443   1.5        ad 	int rv;
   2444   1.5        ad 
   2445  1.15        ad 	im = iop_msg_alloc(sc, IM_WAIT);
   2446   1.1        ad 
   2447  1.11        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_util_abort);
   2448  1.11        ad 	mf.msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_ABORT);
   2449  1.11        ad 	mf.msgictx = ii->ii_ictx;
   2450  1.11        ad 	mf.msgtctx = im->im_tctx;
   2451  1.11        ad 	mf.flags = (func << 24) | flags;
   2452  1.11        ad 	mf.tctxabort = tctxabort;
   2453   1.1        ad 
   2454  1.11        ad 	rv = iop_msg_post(sc, im, &mf, 5000);
   2455  1.11        ad 	iop_msg_free(sc, im);
   2456   1.5        ad 	return (rv);
   2457   1.1        ad }
   2458   1.1        ad 
   2459   1.1        ad /*
   2460  1.11        ad  * Enable or disable reception of events for the specified device.
   2461   1.1        ad  */
   2462   1.5        ad int iop_util_eventreg(struct iop_softc *sc, struct iop_initiator *ii, int mask)
   2463   1.5        ad {
   2464  1.11        ad 	struct i2o_util_event_register mf;
   2465   1.5        ad 
   2466  1.11        ad 	mf.msgflags = I2O_MSGFLAGS(i2o_util_event_register);
   2467  1.11        ad 	mf.msgfunc = I2O_MSGFUNC(ii->ii_tid, I2O_UTIL_EVENT_REGISTER);
   2468  1.11        ad 	mf.msgictx = ii->ii_ictx;
   2469  1.15        ad 	mf.msgtctx = 0;
   2470  1.11        ad 	mf.eventmask = mask;
   2471   1.5        ad 
   2472  1.11        ad 	/* This message is replied to only when events are signalled. */
   2473  1.15        ad 	return (iop_post(sc, (u_int32_t *)&mf));
   2474   1.5        ad }
   2475   1.5        ad 
   2476   1.1        ad int
   2477  1.61  christos iopopen(dev_t dev, int flag, int mode, struct lwp *l)
   2478   1.1        ad {
   2479   1.5        ad 	struct iop_softc *sc;
   2480   1.5        ad 
   2481  1.11        ad 	if ((sc = device_lookup(&iop_cd, minor(dev))) == NULL)
   2482  1.11        ad 		return (ENXIO);
   2483  1.11        ad 	if ((sc->sc_flags & IOP_ONLINE) == 0)
   2484   1.1        ad 		return (ENXIO);
   2485   1.5        ad 	if ((sc->sc_flags & IOP_OPEN) != 0)
   2486   1.5        ad 		return (EBUSY);
   2487   1.5        ad 	sc->sc_flags |= IOP_OPEN;
   2488   1.5        ad 
   2489   1.5        ad 	return (0);
   2490   1.1        ad }
   2491   1.1        ad 
   2492   1.5        ad int
   2493  1.61  christos iopclose(dev_t dev, int flag, int mode,
   2494  1.61  christos     struct lwp *l)
   2495   1.1        ad {
   2496   1.5        ad 	struct iop_softc *sc;
   2497   1.1        ad 
   2498   1.5        ad 	sc = device_lookup(&iop_cd, minor(dev));
   2499  1.11        ad 	sc->sc_flags &= ~IOP_OPEN;
   2500  1.15        ad 
   2501   1.5        ad 	return (0);
   2502   1.1        ad }
   2503   1.1        ad 
   2504   1.1        ad int
   2505  1.64  christos iopioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   2506   1.1        ad {
   2507   1.5        ad 	struct iop_softc *sc;
   2508   1.5        ad 	struct iovec *iov;
   2509   1.5        ad 	int rv, i;
   2510   1.5        ad 
   2511   1.5        ad 	sc = device_lookup(&iop_cd, minor(dev));
   2512  1.63        ad 	rv = 0;
   2513   1.5        ad 
   2514   1.5        ad 	switch (cmd) {
   2515   1.5        ad 	case IOPIOCPT:
   2516  1.62      elad 		rv = kauth_authorize_device_passthru(l->l_cred, dev,
   2517  1.62      elad 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
   2518  1.60      elad 		if (rv)
   2519  1.60      elad 			return (rv);
   2520  1.56  christos 
   2521  1.51  christos 		return (iop_passthrough(sc, (struct ioppt *)data, l->l_proc));
   2522   1.5        ad 
   2523  1.11        ad 	case IOPIOCGSTATUS:
   2524  1.11        ad 		iov = (struct iovec *)data;
   2525  1.11        ad 		i = sizeof(struct i2o_status);
   2526  1.11        ad 		if (i > iov->iov_len)
   2527  1.11        ad 			i = iov->iov_len;
   2528  1.11        ad 		else
   2529  1.11        ad 			iov->iov_len = i;
   2530  1.11        ad 		if ((rv = iop_status_get(sc, 0)) == 0)
   2531  1.11        ad 			rv = copyout(&sc->sc_status, iov->iov_base, i);
   2532  1.11        ad 		return (rv);
   2533   1.5        ad 
   2534  1.11        ad 	case IOPIOCGLCT:
   2535  1.11        ad 	case IOPIOCGTIDMAP:
   2536  1.11        ad 	case IOPIOCRECONFIG:
   2537  1.11        ad 		break;
   2538   1.5        ad 
   2539  1.11        ad 	default:
   2540  1.11        ad #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
   2541  1.11        ad 		printf("%s: unknown ioctl %lx\n", sc->sc_dv.dv_xname, cmd);
   2542  1.11        ad #endif
   2543  1.11        ad 		return (ENOTTY);
   2544  1.11        ad 	}
   2545   1.5        ad 
   2546  1.63        ad 	mutex_enter(&sc->sc_conflock);
   2547   1.1        ad 
   2548  1.11        ad 	switch (cmd) {
   2549   1.5        ad 	case IOPIOCGLCT:
   2550   1.5        ad 		iov = (struct iovec *)data;
   2551  1.11        ad 		i = le16toh(sc->sc_lct->tablesize) << 2;
   2552   1.5        ad 		if (i > iov->iov_len)
   2553   1.5        ad 			i = iov->iov_len;
   2554   1.5        ad 		else
   2555   1.5        ad 			iov->iov_len = i;
   2556  1.11        ad 		rv = copyout(sc->sc_lct, iov->iov_base, i);
   2557   1.5        ad 		break;
   2558   1.5        ad 
   2559   1.5        ad 	case IOPIOCRECONFIG:
   2560  1.63        ad 		rv = iop_reconfigure(sc, 0);
   2561   1.9        ad 		break;
   2562   1.9        ad 
   2563   1.9        ad 	case IOPIOCGTIDMAP:
   2564   1.9        ad 		iov = (struct iovec *)data;
   2565  1.11        ad 		i = sizeof(struct iop_tidmap) * sc->sc_nlctent;
   2566  1.11        ad 		if (i > iov->iov_len)
   2567  1.11        ad 			i = iov->iov_len;
   2568  1.11        ad 		else
   2569  1.11        ad 			iov->iov_len = i;
   2570  1.11        ad 		rv = copyout(sc->sc_tidmap, iov->iov_base, i);
   2571  1.11        ad 		break;
   2572  1.11        ad 	}
   2573  1.11        ad 
   2574  1.63        ad 	mutex_exit(&sc->sc_conflock);
   2575  1.11        ad 	return (rv);
   2576  1.11        ad }
   2577  1.11        ad 
   2578  1.11        ad static int
   2579  1.15        ad iop_passthrough(struct iop_softc *sc, struct ioppt *pt, struct proc *p)
   2580  1.11        ad {
   2581  1.11        ad 	struct iop_msg *im;
   2582  1.11        ad 	struct i2o_msg *mf;
   2583  1.11        ad 	struct ioppt_buf *ptb;
   2584  1.11        ad 	int rv, i, mapped;
   2585  1.11        ad 
   2586  1.11        ad 	mf = NULL;
   2587  1.11        ad 	im = NULL;
   2588  1.11        ad 	mapped = 1;
   2589  1.11        ad 
   2590  1.19        ad 	if (pt->pt_msglen > sc->sc_framesize ||
   2591  1.11        ad 	    pt->pt_msglen < sizeof(struct i2o_msg) ||
   2592  1.11        ad 	    pt->pt_nbufs > IOP_MAX_MSG_XFERS ||
   2593  1.57  christos 	    pt->pt_nbufs < 0 ||
   2594  1.57  christos #if 0
   2595  1.57  christos 	    pt->pt_replylen < 0 ||
   2596  1.57  christos #endif
   2597  1.11        ad             pt->pt_timo < 1000 || pt->pt_timo > 5*60*1000)
   2598  1.11        ad 		return (EINVAL);
   2599  1.11        ad 
   2600  1.11        ad 	for (i = 0; i < pt->pt_nbufs; i++)
   2601  1.11        ad 		if (pt->pt_bufs[i].ptb_datalen > IOP_MAX_XFER) {
   2602  1.11        ad 			rv = ENOMEM;
   2603  1.11        ad 			goto bad;
   2604  1.11        ad 		}
   2605  1.11        ad 
   2606  1.19        ad 	mf = malloc(sc->sc_framesize, M_DEVBUF, M_WAITOK);
   2607  1.11        ad 	if (mf == NULL)
   2608  1.11        ad 		return (ENOMEM);
   2609  1.11        ad 
   2610  1.11        ad 	if ((rv = copyin(pt->pt_msg, mf, pt->pt_msglen)) != 0)
   2611  1.11        ad 		goto bad;
   2612  1.11        ad 
   2613  1.15        ad 	im = iop_msg_alloc(sc, IM_WAIT | IM_NOSTATUS);
   2614  1.11        ad 	im->im_rb = (struct i2o_reply *)mf;
   2615  1.11        ad 	mf->msgictx = IOP_ICTX;
   2616  1.11        ad 	mf->msgtctx = im->im_tctx;
   2617  1.11        ad 
   2618  1.11        ad 	for (i = 0; i < pt->pt_nbufs; i++) {
   2619  1.11        ad 		ptb = &pt->pt_bufs[i];
   2620  1.15        ad 		rv = iop_msg_map(sc, im, (u_int32_t *)mf, ptb->ptb_data,
   2621  1.15        ad 		    ptb->ptb_datalen, ptb->ptb_out != 0, p);
   2622  1.11        ad 		if (rv != 0)
   2623  1.11        ad 			goto bad;
   2624  1.11        ad 		mapped = 1;
   2625  1.11        ad 	}
   2626  1.11        ad 
   2627  1.11        ad 	if ((rv = iop_msg_post(sc, im, mf, pt->pt_timo)) != 0)
   2628  1.11        ad 		goto bad;
   2629  1.11        ad 
   2630  1.11        ad 	i = (le32toh(im->im_rb->msgflags) >> 14) & ~3;
   2631  1.19        ad 	if (i > sc->sc_framesize)
   2632  1.19        ad 		i = sc->sc_framesize;
   2633  1.11        ad 	if (i > pt->pt_replylen)
   2634  1.11        ad 		i = pt->pt_replylen;
   2635  1.15        ad 	rv = copyout(im->im_rb, pt->pt_reply, i);
   2636   1.9        ad 
   2637  1.11        ad  bad:
   2638  1.11        ad 	if (mapped != 0)
   2639  1.11        ad 		iop_msg_unmap(sc, im);
   2640  1.11        ad 	if (im != NULL)
   2641  1.11        ad 		iop_msg_free(sc, im);
   2642  1.11        ad 	if (mf != NULL)
   2643  1.11        ad 		free(mf, M_DEVBUF);
   2644   1.1        ad 	return (rv);
   2645   1.5        ad }
   2646