Home | History | Annotate | Line # | Download | only in i2o
iopsp.c revision 1.2
      1  1.2  ad /*	$NetBSD: iopsp.c,v 1.2 2000/11/09 12:51:36 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  ad  * by Andrew Doran.
      9  1.1  ad  *
     10  1.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1  ad  * modification, are permitted provided that the following conditions
     12  1.1  ad  * are met:
     13  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1  ad  *    must display the following acknowledgement:
     20  1.1  ad  *        This product includes software developed by the NetBSD
     21  1.1  ad  *        Foundation, Inc. and its contributors.
     22  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1  ad  *    from this software without specific prior written permission.
     25  1.1  ad  *
     26  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  ad  */
     38  1.1  ad 
     39  1.1  ad /*
     40  1.1  ad  * Raw SCSI/FC-AL device support for I2O.  I2O presents SCSI devices
     41  1.1  ad  * individually; we group them by controller port.
     42  1.1  ad  */
     43  1.1  ad 
     44  1.1  ad #include "opt_i2o.h"
     45  1.1  ad 
     46  1.1  ad #include <sys/param.h>
     47  1.1  ad #include <sys/systm.h>
     48  1.1  ad #include <sys/kernel.h>
     49  1.1  ad #include <sys/device.h>
     50  1.1  ad #include <sys/queue.h>
     51  1.1  ad #include <sys/proc.h>
     52  1.1  ad #include <sys/buf.h>
     53  1.1  ad #include <sys/endian.h>
     54  1.1  ad #include <sys/malloc.h>
     55  1.1  ad #include <sys/scsiio.h>
     56  1.1  ad 
     57  1.1  ad #include <machine/bswap.h>
     58  1.1  ad #include <machine/bus.h>
     59  1.1  ad 
     60  1.1  ad #include <dev/scsipi/scsi_all.h>
     61  1.1  ad #include <dev/scsipi/scsi_disk.h>
     62  1.1  ad #include <dev/scsipi/scsipi_all.h>
     63  1.1  ad #include <dev/scsipi/scsiconf.h>
     64  1.1  ad 
     65  1.1  ad #include <dev/i2o/i2o.h>
     66  1.1  ad #include <dev/i2o/iopreg.h>
     67  1.1  ad #include <dev/i2o/iopvar.h>
     68  1.1  ad #include <dev/i2o/iopspvar.h>
     69  1.1  ad 
     70  1.1  ad static void	iopsp_attach(struct device *, struct device *, void *);
     71  1.1  ad static void	iopsp_intr(struct device *, struct iop_msg *, void *);
     72  1.1  ad static int	iopsp_ioctl(struct scsipi_link *, u_long, caddr_t, int,
     73  1.1  ad 			    struct proc *);
     74  1.1  ad static int	iopsp_match(struct device *, struct cfdata *, void *);
     75  1.1  ad static void	iopsp_minphys(struct buf *);
     76  1.1  ad static int	iopsp_rescan(struct iopsp_softc *);
     77  1.1  ad static void	iopsp_scan(struct iopsp_softc *);
     78  1.1  ad static int	iopsp_scsi_abort(struct iopsp_softc *, struct iop_msg *);
     79  1.1  ad static int	iopsp_scsi_cmd(struct scsipi_xfer *);
     80  1.1  ad 
     81  1.1  ad static struct scsipi_device iopsp_dev = {
     82  1.1  ad 	NULL,			/* Use default error handler */
     83  1.1  ad 	NULL,			/* have a queue, served by this */
     84  1.1  ad 	NULL,			/* have no async handler */
     85  1.1  ad 	NULL,			/* Use default 'done' routine */
     86  1.1  ad };
     87  1.1  ad 
     88  1.1  ad struct cfattach iopsp_ca = {
     89  1.1  ad 	sizeof(struct iopsp_softc), iopsp_match, iopsp_attach
     90  1.1  ad };
     91  1.1  ad 
     92  1.1  ad /*
     93  1.1  ad  * Match SCSI and fibre channel ports.
     94  1.1  ad  */
     95  1.1  ad static int
     96  1.1  ad iopsp_match(struct device *parent, struct cfdata *match, void *aux)
     97  1.1  ad {
     98  1.1  ad 	struct iop_attach_args *ia;
     99  1.1  ad 	struct {
    100  1.1  ad 		struct	i2o_param_op_results pr;
    101  1.1  ad 		struct	i2o_param_read_results prr;
    102  1.1  ad 		struct	i2o_param_hba_ctlr_info ci;
    103  1.1  ad 	} __attribute__ ((__packed__)) param;
    104  1.1  ad 
    105  1.1  ad 	ia = aux;
    106  1.1  ad 
    107  1.1  ad 	if (ia->ia_class != I2O_CLASS_BUS_ADAPTER_PORT)
    108  1.1  ad 		return (0);
    109  1.1  ad 
    110  1.1  ad 	if (iop_params_get((struct iop_softc *)parent, ia->ia_tid,
    111  1.1  ad 	    I2O_PARAM_HBA_CTLR_INFO, &param, sizeof(param)) != 0)
    112  1.1  ad 		return (0);
    113  1.1  ad 
    114  1.1  ad 	/* XXX */
    115  1.1  ad 	return (param.ci.bustype == I2O_HBA_BUS_SCSI ||
    116  1.1  ad 	    param.ci.bustype == I2O_HBA_BUS_FCA);
    117  1.1  ad }
    118  1.1  ad 
    119  1.1  ad /*
    120  1.1  ad  * Attach a supported device.
    121  1.1  ad  */
    122  1.1  ad static void
    123  1.1  ad iopsp_attach(struct device *parent, struct device *self, void *aux)
    124  1.1  ad {
    125  1.1  ad 	struct iop_attach_args *ia;
    126  1.1  ad 	struct iopsp_softc *sc;
    127  1.1  ad 	struct scsipi_link *sc_link;
    128  1.1  ad 	struct iop_softc *iop;
    129  1.1  ad 	struct {
    130  1.1  ad 		struct	i2o_param_op_results pr;
    131  1.1  ad 		struct	i2o_param_read_results prr;
    132  1.1  ad 		union {
    133  1.1  ad 			struct	i2o_param_device_identity di;
    134  1.1  ad 			struct	i2o_param_hba_ctlr_info ci;
    135  1.1  ad 			struct	i2o_param_hba_scsi_ctlr_info sci;
    136  1.1  ad 		} p;
    137  1.1  ad 	} __attribute__ ((__packed__)) param;
    138  1.1  ad 	char ident[64];
    139  1.1  ad 	int fcal;
    140  1.1  ad #ifdef I2OVERBOSE
    141  1.1  ad 	int size;
    142  1.1  ad #endif
    143  1.1  ad 
    144  1.1  ad 	ia = (struct iop_attach_args *)aux;
    145  1.1  ad 	sc = (struct iopsp_softc *)self;
    146  1.1  ad 	iop = (struct iop_softc *)parent;
    147  1.1  ad 	sc->sc_tid = ia->ia_tid;
    148  1.1  ad 
    149  1.1  ad 	/* Register us as an initiator. */
    150  1.1  ad 	sc->sc_ii.ii_dv = self;
    151  1.1  ad 	sc->sc_ii.ii_intr = iopsp_intr;
    152  1.1  ad 	sc->sc_ii.ii_flags = 0;
    153  1.1  ad 	if (iop_initiator_register(iop, &sc->sc_ii) != 0) {
    154  1.1  ad 		printf("%s: unable to register as an initiator",
    155  1.1  ad 		    sc->sc_dv.dv_xname);
    156  1.1  ad 		return;
    157  1.1  ad 	}
    158  1.1  ad 
    159  1.1  ad 	if (iop_params_get(iop, ia->ia_tid, I2O_PARAM_HBA_CTLR_INFO, &param,
    160  1.1  ad 	    sizeof(param)) != 0) {
    161  1.1  ad 	    	printf("%s: unable to retrieve parameters\n",
    162  1.1  ad 	    	    sc->sc_dv.dv_xname);
    163  1.1  ad 		goto bad;
    164  1.1  ad 	}
    165  1.1  ad 
    166  1.1  ad 	fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA);		/* XXX */
    167  1.1  ad 
    168  1.1  ad 	/*
    169  1.1  ad 	 * Say what the device is.  If we can find out what the controling
    170  1.1  ad 	 * device is, say what that is too.
    171  1.1  ad 	 */
    172  1.1  ad 	printf(": %s port", fcal ? "FC-AL" : "SCSI");
    173  1.1  ad 	if (iop_params_get(iop, ia->ia_tid, I2O_PARAM_DEVICE_IDENTITY, &param,
    174  1.1  ad 	    sizeof(param)) == 0) {
    175  1.1  ad 		iop_strvis(param.p.di.vendorinfo,
    176  1.1  ad 		    sizeof(param.p.di.vendorinfo), ident, sizeof(ident));
    177  1.1  ad 		printf(" <%s, ", ident);
    178  1.1  ad 		iop_strvis(param.p.di.productinfo,
    179  1.1  ad 		    sizeof(param.p.di.productinfo), ident, sizeof(ident));
    180  1.1  ad 		printf("%s, ", ident);
    181  1.1  ad 		iop_strvis(param.p.di.revlevel,
    182  1.1  ad 		    sizeof(param.p.di.revlevel), ident, sizeof(ident));
    183  1.1  ad 		printf("%s> ", ident);
    184  1.1  ad 	}
    185  1.1  ad 	printf("\n");
    186  1.1  ad 
    187  1.1  ad 	if (iop_params_get(iop, ia->ia_tid, I2O_PARAM_HBA_SCSI_CTLR_INFO,
    188  1.1  ad 	    &param, sizeof(param)) != 0) {
    189  1.1  ad 		printf("%s: unable to retrieve controller parameters\n",
    190  1.1  ad 		    sc->sc_dv.dv_xname);
    191  1.1  ad 		goto bad;
    192  1.1  ad 	}
    193  1.1  ad 
    194  1.1  ad #ifdef I2OVERBOSE
    195  1.1  ad 	printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
    196  1.1  ad 	    sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
    197  1.1  ad 	    (u_int32_t)le64toh(param.p.sci.maxsyncrate) / 1000,
    198  1.1  ad 	    le32toh(param.p.sci.initiatorid));
    199  1.1  ad #endif
    200  1.1  ad 
    201  1.1  ad 	sc->sc_adapter.scsipi_cmd = iopsp_scsi_cmd;
    202  1.1  ad 	sc->sc_adapter.scsipi_minphys = iopsp_minphys;
    203  1.1  ad 	sc->sc_adapter.scsipi_ioctl = iopsp_ioctl;
    204  1.1  ad 
    205  1.1  ad 	sc_link = &sc->sc_link;
    206  1.1  ad 	sc_link->type = BUS_SCSI;
    207  1.1  ad 	sc_link->device = &iopsp_dev;
    208  1.1  ad 	sc_link->adapter = &sc->sc_adapter;
    209  1.1  ad 	sc_link->adapter_softc = sc;
    210  1.1  ad 	sc_link->scsipi_scsi.channel = 0;
    211  1.1  ad 	sc_link->scsipi_scsi.adapter_target = le32toh(param.p.sci.initiatorid);
    212  1.1  ad 	sc_link->scsipi_scsi.max_target =
    213  1.1  ad 	    fcal ? IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth - 1;
    214  1.1  ad 	sc_link->scsipi_scsi.max_lun = IOPSP_MAX_LUN;
    215  1.1  ad 	sc_link->openings = iop->sc_maxqueuecnt;		/* XXX */
    216  1.1  ad 
    217  1.1  ad #ifdef I2OVERBOSE
    218  1.1  ad 	/*
    219  1.1  ad 	 * Allocate the target map.  Currently used for informational
    220  1.1  ad 	 * purposes only.
    221  1.1  ad 	 */
    222  1.1  ad 	size = (sc_link->scsipi_scsi.max_target + 1) *
    223  1.1  ad 	    sizeof(struct iopsp_target);
    224  1.1  ad 	sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT);
    225  1.1  ad 	memset(sc->sc_targetmap, 0, size);
    226  1.1  ad #endif
    227  1.1  ad 
    228  1.1  ad 	/* Build the two maps, and attach to scsipi. */
    229  1.1  ad 	iopsp_scan(sc);
    230  1.1  ad 	config_found(self, sc_link, scsiprint);
    231  1.1  ad 	return;
    232  1.1  ad 
    233  1.1  ad bad:
    234  1.1  ad 	iop_initiator_unregister(iop, &sc->sc_ii);
    235  1.1  ad }
    236  1.1  ad 
    237  1.1  ad /*
    238  1.1  ad  * Determine which devices we control, and enter them into the maps.
    239  1.1  ad  */
    240  1.1  ad static void
    241  1.1  ad iopsp_scan(struct iopsp_softc *sc)
    242  1.1  ad {
    243  1.1  ad 	struct iop_softc *iop;
    244  1.1  ad 	struct i2o_lct_entry *le;
    245  1.1  ad 	struct scsipi_link *sc_link;
    246  1.1  ad 	struct {
    247  1.1  ad 		struct	i2o_param_op_results pr;
    248  1.1  ad 		struct	i2o_param_read_results prr;
    249  1.1  ad 		struct	i2o_param_scsi_device_info sdi;
    250  1.1  ad 	} __attribute__ ((__packed__)) param;
    251  1.1  ad 	int tid, nent, i, targ, lun, size;
    252  1.1  ad 	u_short *tidmap;
    253  1.1  ad #ifdef I2OVERBOSE
    254  1.1  ad 	struct iopsp_target *it;
    255  1.1  ad 	int syncrate;
    256  1.1  ad #endif
    257  1.1  ad 
    258  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    259  1.1  ad 	sc_link = &sc->sc_link;
    260  1.1  ad 
    261  1.1  ad 	/*
    262  1.1  ad 	 * Allocate memory for the target/LUN -> TID map.  Use zero to
    263  1.1  ad 	 * denote absent targets (zero is the TID of the I2O executive,
    264  1.1  ad 	 * and we never address that here).
    265  1.1  ad 	 */
    266  1.1  ad 	size = (sc_link->scsipi_scsi.max_target + 1) *
    267  1.1  ad 	    (IOPSP_MAX_LUN + 1) * sizeof(u_short);
    268  1.1  ad 	tidmap = malloc(size, M_DEVBUF, M_NOWAIT);
    269  1.1  ad 	memset(tidmap, 0, size);	/* XXX */
    270  1.1  ad 
    271  1.1  ad #ifdef I2OVERBOSE
    272  1.1  ad 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    273  1.1  ad 		sc->sc_targetmap[i].it_flags &= ~IT_PRESENT;
    274  1.1  ad #endif
    275  1.1  ad 
    276  1.1  ad 	if (iop_lct_lock(iop) != 0)
    277  1.1  ad 		return;
    278  1.1  ad 
    279  1.1  ad 	nent = iop->sc_nlctent;
    280  1.1  ad 	for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
    281  1.1  ad 		switch (le16toh(le->classid) & 4095) {
    282  1.1  ad 		case I2O_CLASS_SCSI_PERIPHERAL:
    283  1.1  ad 		case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
    284  1.1  ad 			break;
    285  1.1  ad 		default:
    286  1.1  ad 			continue;
    287  1.1  ad 		}
    288  1.1  ad 		if (((le32toh(le->usertid) >> 12) & 4095) != sc->sc_tid)
    289  1.1  ad 			continue;
    290  1.1  ad 
    291  1.1  ad 		tid = le32toh(le->localtid) & 4095;
    292  1.1  ad 
    293  1.1  ad 		if (iop_params_get(iop, tid, I2O_PARAM_SCSI_DEVICE_INFO, &param,
    294  1.1  ad 		    sizeof(param)) != 0) {
    295  1.1  ad 			printf("%s: unable to retrieve device parameters\n",
    296  1.1  ad 			    sc->sc_dv.dv_xname);
    297  1.1  ad 			continue;
    298  1.1  ad 		}
    299  1.1  ad 		targ = le32toh(param.sdi.identifier);
    300  1.1  ad 		lun = param.sdi.luninfo[1];
    301  1.1  ad 
    302  1.1  ad 		/* If the device is in use by another module, ignore it. */
    303  1.1  ad 		if (!iop_tid_inuse(iop, tid)) {
    304  1.1  ad #ifdef I2OVERBOSE
    305  1.1  ad 			if (sc->sc_tidmap == NULL ||
    306  1.1  ad 			    IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
    307  1.1  ad 			    IOPSP_TID_INUSE)
    308  1.1  ad 				printf("%s: target %d,%d (tid %d): in use by"
    309  1.1  ad 				    " another module\n", sc->sc_dv.dv_xname,
    310  1.1  ad 				    targ, lun, tid);
    311  1.1  ad #endif
    312  1.1  ad 			IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
    313  1.1  ad 			continue;
    314  1.1  ad 		}
    315  1.1  ad 		IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
    316  1.1  ad 
    317  1.1  ad #ifdef I2OVERBOSE
    318  1.1  ad 		/*
    319  1.1  ad 		 * If we've already described this target, and nothing has
    320  1.1  ad 		 * changed, then don't describe it again.
    321  1.1  ad 		 */
    322  1.1  ad 		it = &sc->sc_targetmap[targ];
    323  1.1  ad 		it->it_flags |= IT_PRESENT;
    324  1.1  ad 		syncrate = ((int)le64toh(param.sdi.negsyncrate) + 500) / 1000;
    325  1.1  ad 		if (it->it_width == param.sdi.negdatawidth &&
    326  1.1  ad 		    it->it_offset == param.sdi.negoffset &&
    327  1.1  ad 		    it->it_syncrate == syncrate)
    328  1.1  ad 			continue;
    329  1.1  ad 
    330  1.1  ad 		it->it_width = param.sdi.negdatawidth;
    331  1.1  ad 		it->it_offset = param.sdi.negoffset;
    332  1.1  ad 		it->it_syncrate = syncrate;
    333  1.1  ad 
    334  1.1  ad 		printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
    335  1.1  ad 		    targ, tid, it->it_width);
    336  1.1  ad 		if (it->it_syncrate == 0)
    337  1.1  ad 			printf("asynchronous\n");
    338  1.1  ad 		else
    339  1.1  ad 			printf("synchronous at %dMHz, offset 0x%x\n",
    340  1.1  ad 			    it->it_syncrate, it->it_offset);
    341  1.1  ad #endif
    342  1.1  ad 	}
    343  1.1  ad 
    344  1.1  ad 	iop_lct_unlock(iop);
    345  1.1  ad 
    346  1.1  ad #ifdef I2OVERBOSE
    347  1.1  ad 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    348  1.1  ad 		if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
    349  1.1  ad 			sc->sc_targetmap[i].it_width = 0;
    350  1.1  ad #endif
    351  1.1  ad 
    352  1.1  ad 	/* Swap in the new map and return. */
    353  1.1  ad 	if (sc->sc_tidmap != NULL)
    354  1.1  ad 		free(sc->sc_tidmap, M_DEVBUF);
    355  1.1  ad 	sc->sc_tidmap = tidmap;
    356  1.1  ad 	sc->sc_chgindicator = iop->sc_lct->changeindicator;
    357  1.1  ad }
    358  1.1  ad 
    359  1.1  ad /*
    360  1.1  ad  * Adjust the size an I/O request.
    361  1.1  ad  */
    362  1.1  ad static void
    363  1.1  ad iopsp_minphys(struct buf *bp)
    364  1.1  ad {
    365  1.1  ad 
    366  1.1  ad 	if (bp->b_bcount > IOP_MAX_XFER)
    367  1.1  ad 		bp->b_bcount = IOP_MAX_XFER;
    368  1.1  ad 	minphys(bp);
    369  1.1  ad }
    370  1.1  ad 
    371  1.1  ad /*
    372  1.1  ad  * Ask the bus port to perform a rescan.  XXX It's unclear whether or not
    373  1.1  ad  * TIDs for established targets will change.
    374  1.1  ad  */
    375  1.1  ad static int
    376  1.1  ad iopsp_rescan(struct iopsp_softc *sc)
    377  1.1  ad {
    378  1.1  ad 	struct iop_softc *iop;
    379  1.1  ad 	struct iop_msg *im;
    380  1.1  ad 	struct i2o_hba_bus_scan *mb;
    381  1.1  ad 	int rv;
    382  1.1  ad 
    383  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    384  1.1  ad 
    385  1.1  ad 	if (iop_msg_alloc(iop, &sc->sc_ii, &im, IM_NOINTR) != 0)
    386  1.1  ad 		return (-1);
    387  1.1  ad 
    388  1.1  ad 	mb = (struct i2o_hba_bus_scan *)im->im_msg;
    389  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    390  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(sc->sc_tid, I2O_HBA_BUS_SCAN);
    391  1.1  ad 	mb->msgictx = sc->sc_ii.ii_ictx;
    392  1.1  ad 	mb->msgtctx = im->im_tctx;
    393  1.1  ad 
    394  1.1  ad 	if (iop_msg_enqueue(iop, im) != 0)
    395  1.1  ad 		rv = -1;
    396  1.1  ad 	else if(iop_msg_wait(iop, im, 1000) != 0)
    397  1.1  ad 		rv = -1;
    398  1.1  ad 	else
    399  1.1  ad 		rv = 0;
    400  1.1  ad 	iop_msg_free(iop, &sc->sc_ii, im);
    401  1.1  ad 	if (rv != 0)
    402  1.1  ad 		return (rv);
    403  1.1  ad 
    404  1.1  ad 	/*
    405  1.1  ad          * Re-read the LCT and determine if it has changed.  XXX This should
    406  1.1  ad          * probably be accomplished by noticing a CHANGED_LCT event from the
    407  1.1  ad          * executive.
    408  1.1  ad 	 */
    409  1.1  ad 	if (iop_lct_get(iop))
    410  1.1  ad 		return (-1);
    411  1.1  ad 	if (iop->sc_lct->changeindicator == sc->sc_chgindicator)
    412  1.1  ad 		return (0);
    413  1.1  ad 
    414  1.1  ad 	/* Rebuild the target/LUN -> TID map, and return. */
    415  1.1  ad 	iopsp_scan(sc);
    416  1.1  ad 	return (0);
    417  1.1  ad }
    418  1.1  ad 
    419  1.1  ad /*
    420  1.1  ad  * Start a SCSI command.
    421  1.1  ad  */
    422  1.1  ad static int
    423  1.1  ad iopsp_scsi_cmd(struct scsipi_xfer *xs)
    424  1.1  ad {
    425  1.1  ad 	struct scsipi_link *sc_link;
    426  1.1  ad 	struct iopsp_softc *sc;
    427  1.1  ad 	struct iop_msg *im;
    428  1.1  ad 	struct iop_softc *iop;
    429  1.1  ad 	struct i2o_scsi_scb_exec *mb;
    430  1.1  ad 	int error, flags, tid;
    431  1.1  ad 
    432  1.1  ad 	sc_link = xs->sc_link;
    433  1.1  ad 	flags = xs->xs_control;
    434  1.1  ad 	sc = sc_link->adapter_softc;
    435  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    436  1.1  ad 
    437  1.1  ad 	tid = IOPSP_TIDMAP(sc->sc_tidmap, sc_link->scsipi_scsi.target,
    438  1.1  ad 	    sc_link->scsipi_scsi.lun);
    439  1.1  ad 	if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
    440  1.1  ad 		xs->error = XS_SELTIMEOUT;
    441  1.1  ad 		return (COMPLETE);
    442  1.1  ad 	}
    443  1.1  ad 
    444  1.1  ad 	SC_DEBUG(sc_link, SDEV_DB2, ("iopsp_scsi_cmd\n"));
    445  1.1  ad 
    446  1.1  ad 	/* Need to reset the target? */
    447  1.1  ad 	if ((flags & XS_CTL_RESET) != 0) {
    448  1.1  ad 		if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
    449  1.1  ad 		    sc->sc_ii.ii_ictx) != 0) {
    450  1.1  ad #ifdef I2ODEBUG
    451  1.1  ad 			printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    452  1.1  ad #endif
    453  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    454  1.1  ad 		}
    455  1.1  ad 		return (COMPLETE);
    456  1.1  ad 	}
    457  1.1  ad 
    458  1.1  ad #if defined(I2ODEBUG) || defined(SCSIDEBUG)
    459  1.1  ad 	if (xs->cmdlen > 16) {
    460  1.1  ad 		printf("%s: CDB too large\n", sc->sc_dv.dv_xname);
    461  1.1  ad 		xs->error = XS_DRIVER_STUFFUP;
    462  1.1  ad 		return (COMPLETE);
    463  1.1  ad 	}
    464  1.1  ad #endif
    465  1.1  ad 
    466  1.1  ad 	if (iop_msg_alloc(iop, &sc->sc_ii, &im,
    467  1.1  ad 	    (flags & (XS_CTL_POLL | XS_CTL_NOSLEEP)) != 0 ? IM_NOWAIT : 0)) {
    468  1.1  ad 		xs->error = XS_DRIVER_STUFFUP;
    469  1.1  ad 		return (TRY_AGAIN_LATER);
    470  1.1  ad 	}
    471  1.1  ad 	im->im_dvcontext = xs;
    472  1.1  ad 
    473  1.1  ad 	mb = (struct i2o_scsi_scb_exec *)im->im_msg;
    474  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
    475  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
    476  1.1  ad 	mb->msgictx = sc->sc_ii.ii_ictx;
    477  1.1  ad 	mb->msgtctx = im->im_tctx;
    478  1.1  ad 	mb->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
    479  1.1  ad 	    I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
    480  1.1  ad 	memset(mb->cdb, 0, sizeof(mb->cdb));
    481  1.1  ad 	memcpy(mb->cdb, xs->cmd, xs->cmdlen);
    482  1.1  ad 	mb->datalen = xs->datalen;
    483  1.1  ad 
    484  1.1  ad 	/* XXX */
    485  1.1  ad 	if ((xs->sc_link->quirks & SDEV_NOTAG) == 0 &&
    486  1.1  ad 	    xs->cmd->opcode != INQUIRY &&
    487  1.1  ad 	    xs->cmd->opcode != TEST_UNIT_READY &&
    488  1.1  ad 	    xs->cmd->opcode != REQUEST_SENSE) {
    489  1.1  ad 		if (xs->bp != NULL && (xs->bp->b_flags & B_ASYNC) != 0)
    490  1.1  ad 			mb->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
    491  1.1  ad 		else
    492  1.1  ad 			mb->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
    493  1.1  ad 	}
    494  1.1  ad 
    495  1.1  ad 	if (xs->datalen != 0) {
    496  1.1  ad 		error = iop_msg_map(iop, im, xs->data, xs->datalen,
    497  1.1  ad 		    (flags & XS_CTL_DATA_OUT) == 0);
    498  1.1  ad 		if (error) {
    499  1.1  ad #ifdef I2ODEBUG
    500  1.1  ad 			printf("%s: error %d mapping xfer\n",
    501  1.1  ad 			    sc->sc_dv.dv_xname, error);
    502  1.1  ad #endif
    503  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    504  1.1  ad 			iop_msg_free(iop, &sc->sc_ii, im);
    505  1.1  ad 			return (COMPLETE);
    506  1.1  ad 		}
    507  1.1  ad 		if ((flags & XS_CTL_DATA_IN) == 0)
    508  1.1  ad 			mb->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
    509  1.1  ad 		else
    510  1.1  ad 			mb->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
    511  1.1  ad 	}
    512  1.1  ad 
    513  1.1  ad 	/*
    514  1.1  ad 	 * If the command is allowed to execute asynchronously, enqueue it
    515  1.1  ad 	 * with the IOP.  XXX Time out async commands?
    516  1.1  ad 	 */
    517  1.1  ad 	if ((flags & XS_CTL_POLL) == 0) {
    518  1.1  ad 		if (iop_msg_enqueue(iop, im)) {
    519  1.1  ad 			iop_msg_unmap(iop, im);
    520  1.1  ad 			iop_msg_free(iop, &sc->sc_ii, im);
    521  1.1  ad #ifdef I2ODEBUG
    522  1.1  ad 			printf("%s: can't enqueue msg\n", sc->sc_dv.dv_xname);
    523  1.1  ad #endif
    524  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    525  1.1  ad 			return (COMPLETE);
    526  1.1  ad 		}
    527  1.1  ad 		return (SUCCESSFULLY_QUEUED);
    528  1.1  ad 	}
    529  1.1  ad 
    530  1.1  ad 	if (iop_msg_send(iop, im, xs->timeout)) {
    531  1.1  ad 		scsi_print_addr(xs->sc_link);
    532  1.1  ad 		printf("timeout; aborting command\n");
    533  1.1  ad 		if (iopsp_scsi_abort(sc, im)) {
    534  1.1  ad 			scsi_print_addr(xs->sc_link);
    535  1.1  ad 			printf("abort failed\n");
    536  1.1  ad 		}
    537  1.1  ad 		xs->error = XS_DRIVER_STUFFUP;
    538  1.1  ad 	}
    539  1.1  ad 	return (COMPLETE);
    540  1.1  ad }
    541  1.1  ad 
    542  1.1  ad /*
    543  1.1  ad  * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
    544  1.1  ad  */
    545  1.1  ad static int
    546  1.1  ad iopsp_scsi_abort(struct iopsp_softc *sc, struct iop_msg *aim)
    547  1.1  ad {
    548  1.1  ad 	struct iop_msg *im;
    549  1.1  ad 	struct i2o_scsi_scb_abort *mb;
    550  1.1  ad 	struct iop_softc *iop;
    551  1.1  ad 	int rv;
    552  1.1  ad 
    553  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    554  1.1  ad 
    555  1.1  ad 	rv = iop_msg_alloc(iop, &sc->sc_ii, &im, IM_NOWAIT | IM_NOINTR);
    556  1.1  ad 	if (rv != 0)
    557  1.1  ad 		return (rv);
    558  1.1  ad 
    559  1.1  ad 	mb = (struct i2o_scsi_scb_abort *)im->im_msg;
    560  1.1  ad 	mb->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
    561  1.1  ad 	mb->msgfunc = I2O_MSGFUNC(aim->im_tid, I2O_SCSI_SCB_ABORT);
    562  1.1  ad 	mb->msgictx = sc->sc_ii.ii_ictx;
    563  1.1  ad 	mb->msgtctx = im->im_tctx;
    564  1.1  ad 	mb->tctxabort = aim->im_tctx;
    565  1.1  ad 
    566  1.1  ad 	rv = iop_msg_send(iop, im, 1000);
    567  1.1  ad 	iop_msg_free(iop, &sc->sc_ii, im);
    568  1.1  ad 	return (rv);
    569  1.1  ad }
    570  1.1  ad 
    571  1.1  ad /*
    572  1.1  ad  * We have a message which has been processed and replied to by the IOP -
    573  1.1  ad  * deal with it.
    574  1.1  ad  */
    575  1.1  ad static void
    576  1.1  ad iopsp_intr(struct device *dv, struct iop_msg *im, void *reply)
    577  1.1  ad {
    578  1.1  ad 	struct scsipi_xfer *xs;
    579  1.1  ad 	struct iopsp_softc *sc;
    580  1.1  ad 	struct i2o_scsi_reply *rb;
    581  1.1  ad 	struct iop_softc *iop;
    582  1.1  ad 	u_int hba_status, scsi_status, detail;
    583  1.1  ad 	int sl;
    584  1.2  ad 
    585  1.1  ad 	sc = (struct iopsp_softc *)dv;
    586  1.1  ad 	xs = (struct scsipi_xfer *)im->im_dvcontext;
    587  1.1  ad 	iop = (struct iop_softc *)dv->dv_parent;
    588  1.1  ad 
    589  1.1  ad 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_intr\n"));
    590  1.1  ad 
    591  1.1  ad 	if (xs->error == XS_NOERROR) {
    592  1.1  ad 		rb = reply;
    593  1.1  ad 		detail = le16toh(rb->detail);
    594  1.1  ad 		hba_status = (detail >> 8) & 0xff;
    595  1.1  ad 		scsi_status = detail & 0xff;
    596  1.1  ad 
    597  1.1  ad 		if (hba_status != I2O_SCSI_DSC_SUCCESS) {
    598  1.1  ad 			switch (hba_status) {
    599  1.1  ad 			case I2O_SCSI_DSC_ADAPTER_BUSY:
    600  1.1  ad 			case I2O_SCSI_DSC_SCSI_BUS_RESET:
    601  1.1  ad 			case I2O_SCSI_DSC_BUS_BUSY:
    602  1.1  ad 				xs->error = XS_BUSY;
    603  1.1  ad 				break;
    604  1.1  ad 			case I2O_SCSI_DSC_SELECTION_TIMEOUT:
    605  1.1  ad 				xs->error = XS_SELTIMEOUT;
    606  1.1  ad 				break;
    607  1.1  ad 			case I2O_SCSI_DSC_COMMAND_TIMEOUT:
    608  1.1  ad 			case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
    609  1.1  ad 			case I2O_SCSI_DSC_LUN_INVALID:
    610  1.1  ad 			case I2O_SCSI_DSC_SCSI_TID_INVALID:
    611  1.1  ad 				xs->error = XS_TIMEOUT;
    612  1.1  ad 				break;
    613  1.1  ad 			default:
    614  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    615  1.1  ad 				break;
    616  1.1  ad 			}
    617  1.1  ad #ifdef I2ODEBUG
    618  1.1  ad 			printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
    619  1.1  ad 			    hba_status);
    620  1.1  ad #endif
    621  1.1  ad 		} else if (scsi_status != SCSI_OK) {
    622  1.1  ad 			switch (scsi_status) {
    623  1.1  ad 			case SCSI_CHECK:
    624  1.1  ad 				xs->error = XS_SENSE;
    625  1.1  ad 				sl = le32toh(rb->senselen);
    626  1.1  ad 				if (xs->req_sense_length != 0 &&
    627  1.1  ad 				    xs->req_sense_length < sl)
    628  1.1  ad 					sl = xs->req_sense_length;
    629  1.1  ad 				if (sl > sizeof(xs->sense.scsi_sense))
    630  1.1  ad 					sl = le32toh(rb->senselen);
    631  1.1  ad 				memcpy(&xs->sense.scsi_sense, rb->sense, sl);
    632  1.1  ad 				break;
    633  1.1  ad 			case SCSI_BUSY:
    634  1.1  ad 				xs->error = XS_BUSY;
    635  1.1  ad 				break;
    636  1.1  ad 			default:
    637  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    638  1.1  ad 				break;
    639  1.1  ad 			}
    640  1.1  ad 		} else
    641  1.1  ad 			xs->error = XS_NOERROR;
    642  1.1  ad 
    643  1.1  ad 		xs->resid = le32toh(rb->datalen) - xs->datalen;
    644  1.1  ad 		xs->status = scsi_status;
    645  1.1  ad 	}
    646  1.1  ad 
    647  1.1  ad 	/* Free the message wrapper and pass the news to scsipi. */
    648  1.1  ad 	iop_msg_unmap(iop, im);
    649  1.1  ad 	iop_msg_free(iop, &sc->sc_ii, im);
    650  1.1  ad 	xs->xs_status |= XS_STS_DONE;
    651  1.1  ad 	scsipi_done(xs);
    652  1.1  ad }
    653  1.1  ad 
    654  1.1  ad /*
    655  1.1  ad  * ioctl hook; used here only to initiate low-level rescans.
    656  1.1  ad  */
    657  1.1  ad static int
    658  1.1  ad iopsp_ioctl(struct scsipi_link *sc_link, u_long cmd, caddr_t data, int flag,
    659  1.1  ad 	    struct proc *p)
    660  1.1  ad {
    661  1.1  ad 	int rv;
    662  1.1  ad 
    663  1.1  ad 	switch (cmd) {
    664  1.1  ad 	case SCBUSIOLLSCAN:
    665  1.1  ad 		rv = iopsp_rescan(sc_link->adapter_softc);
    666  1.1  ad 		break;
    667  1.1  ad 	default:
    668  1.1  ad 		rv = ENXIO;
    669  1.1  ad 		break;
    670  1.1  ad 	}
    671  1.1  ad 
    672  1.1  ad 	return (rv);
    673  1.1  ad }
    674