Home | History | Annotate | Line # | Download | only in i2o
iopsp.c revision 1.5
      1  1.5  ad /*	$NetBSD: iopsp.c,v 1.5 2001/03/20 13:01:49 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.5  ad  * Copyright (c) 2000, 2001 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.5  ad  * Raw SCSI device support for I2O.  IOPs present SCSI devices individually;
     41  1.5  ad  * we group them by controlling 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.3  ad #include <sys/lock.h>
     57  1.1  ad 
     58  1.1  ad #include <machine/bswap.h>
     59  1.1  ad #include <machine/bus.h>
     60  1.1  ad 
     61  1.1  ad #include <dev/scsipi/scsi_all.h>
     62  1.1  ad #include <dev/scsipi/scsi_disk.h>
     63  1.1  ad #include <dev/scsipi/scsipi_all.h>
     64  1.1  ad #include <dev/scsipi/scsiconf.h>
     65  1.1  ad 
     66  1.1  ad #include <dev/i2o/i2o.h>
     67  1.5  ad #include <dev/i2o/iopio.h>
     68  1.1  ad #include <dev/i2o/iopvar.h>
     69  1.1  ad #include <dev/i2o/iopspvar.h>
     70  1.1  ad 
     71  1.5  ad static void	iopsp_adjqparam(struct device *, int);
     72  1.1  ad static void	iopsp_attach(struct device *, struct device *, void *);
     73  1.1  ad static void	iopsp_intr(struct device *, struct iop_msg *, void *);
     74  1.1  ad static int	iopsp_ioctl(struct scsipi_link *, u_long, caddr_t, int,
     75  1.1  ad 			    struct proc *);
     76  1.1  ad static int	iopsp_match(struct device *, struct cfdata *, void *);
     77  1.1  ad static void	iopsp_minphys(struct buf *);
     78  1.1  ad static int	iopsp_rescan(struct iopsp_softc *);
     79  1.3  ad static int	iopsp_reconfig(struct device *);
     80  1.3  ad static int	iopsp_scsi_abort(struct iopsp_softc *, int, struct iop_msg *);
     81  1.1  ad static int	iopsp_scsi_cmd(struct scsipi_xfer *);
     82  1.1  ad 
     83  1.1  ad static struct scsipi_device iopsp_dev = {
     84  1.1  ad 	NULL,			/* Use default error handler */
     85  1.1  ad 	NULL,			/* have a queue, served by this */
     86  1.1  ad 	NULL,			/* have no async handler */
     87  1.1  ad 	NULL,			/* Use default 'done' routine */
     88  1.1  ad };
     89  1.1  ad 
     90  1.1  ad struct cfattach iopsp_ca = {
     91  1.1  ad 	sizeof(struct iopsp_softc), iopsp_match, iopsp_attach
     92  1.1  ad };
     93  1.1  ad 
     94  1.1  ad /*
     95  1.5  ad  * Match a supported device.
     96  1.1  ad  */
     97  1.1  ad static int
     98  1.1  ad iopsp_match(struct device *parent, struct cfdata *match, void *aux)
     99  1.1  ad {
    100  1.1  ad 	struct iop_attach_args *ia;
    101  1.1  ad 	struct {
    102  1.1  ad 		struct	i2o_param_op_results pr;
    103  1.1  ad 		struct	i2o_param_read_results prr;
    104  1.1  ad 		struct	i2o_param_hba_ctlr_info ci;
    105  1.1  ad 	} __attribute__ ((__packed__)) param;
    106  1.1  ad 
    107  1.1  ad 	ia = aux;
    108  1.1  ad 
    109  1.1  ad 	if (ia->ia_class != I2O_CLASS_BUS_ADAPTER_PORT)
    110  1.1  ad 		return (0);
    111  1.1  ad 
    112  1.5  ad 	if (iop_param_op((struct iop_softc *)parent, ia->ia_tid, NULL, 0,
    113  1.1  ad 	    I2O_PARAM_HBA_CTLR_INFO, &param, sizeof(param)) != 0)
    114  1.1  ad 		return (0);
    115  1.1  ad 
    116  1.1  ad 	return (param.ci.bustype == I2O_HBA_BUS_SCSI ||
    117  1.1  ad 	    param.ci.bustype == I2O_HBA_BUS_FCA);
    118  1.1  ad }
    119  1.1  ad 
    120  1.1  ad /*
    121  1.1  ad  * Attach a supported device.
    122  1.1  ad  */
    123  1.1  ad static void
    124  1.1  ad iopsp_attach(struct device *parent, struct device *self, void *aux)
    125  1.1  ad {
    126  1.1  ad 	struct iop_attach_args *ia;
    127  1.1  ad 	struct iopsp_softc *sc;
    128  1.1  ad 	struct scsipi_link *sc_link;
    129  1.1  ad 	struct iop_softc *iop;
    130  1.1  ad 	struct {
    131  1.1  ad 		struct	i2o_param_op_results pr;
    132  1.1  ad 		struct	i2o_param_read_results prr;
    133  1.1  ad 		union {
    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.3  ad 			struct	i2o_param_hba_scsi_port_info spi;
    137  1.1  ad 		} p;
    138  1.1  ad 	} __attribute__ ((__packed__)) param;
    139  1.3  ad 	int fcal, rv;
    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 
    148  1.1  ad 	/* Register us as an initiator. */
    149  1.1  ad 	sc->sc_ii.ii_dv = self;
    150  1.1  ad 	sc->sc_ii.ii_intr = iopsp_intr;
    151  1.1  ad 	sc->sc_ii.ii_flags = 0;
    152  1.3  ad 	sc->sc_ii.ii_tid = ia->ia_tid;
    153  1.3  ad 	sc->sc_ii.ii_reconfig = iopsp_reconfig;
    154  1.5  ad 	sc->sc_ii.ii_adjqparam = iopsp_adjqparam;
    155  1.5  ad 	iop_initiator_register(iop, &sc->sc_ii);
    156  1.1  ad 
    157  1.5  ad 	rv = iop_param_op(iop, ia->ia_tid, NULL, 0, I2O_PARAM_HBA_CTLR_INFO,
    158  1.5  ad 	    &param, sizeof(param));
    159  1.3  ad 	if (rv != 0) {
    160  1.3  ad 		printf("%s: unable to get parameters (0x%04x; %d)\n",
    161  1.3  ad 	    	    sc->sc_dv.dv_xname, I2O_PARAM_HBA_CTLR_INFO, rv);
    162  1.1  ad 		goto bad;
    163  1.1  ad 	}
    164  1.1  ad 
    165  1.1  ad 	fcal = (param.p.ci.bustype == I2O_HBA_BUS_FCA);		/* XXX */
    166  1.1  ad 
    167  1.1  ad 	/*
    168  1.1  ad 	 * Say what the device is.  If we can find out what the controling
    169  1.1  ad 	 * device is, say what that is too.
    170  1.1  ad 	 */
    171  1.5  ad 	printf(": SCSI port");
    172  1.5  ad 	iop_print_ident(iop, ia->ia_tid);
    173  1.1  ad 	printf("\n");
    174  1.1  ad 
    175  1.5  ad 	rv = iop_param_op(iop, ia->ia_tid, NULL, 0,
    176  1.5  ad 	    I2O_PARAM_HBA_SCSI_CTLR_INFO, &param, sizeof(param));
    177  1.3  ad 	if (rv != 0) {
    178  1.3  ad 		printf("%s: unable to get parameters (0x%04x; %d)\n",
    179  1.3  ad 		    sc->sc_dv.dv_xname, I2O_PARAM_HBA_SCSI_CTLR_INFO, rv);
    180  1.1  ad 		goto bad;
    181  1.1  ad 	}
    182  1.1  ad 
    183  1.1  ad #ifdef I2OVERBOSE
    184  1.1  ad 	printf("%s: %d-bit, max sync rate %dMHz, initiator ID %d\n",
    185  1.1  ad 	    sc->sc_dv.dv_xname, param.p.sci.maxdatawidth,
    186  1.1  ad 	    (u_int32_t)le64toh(param.p.sci.maxsyncrate) / 1000,
    187  1.1  ad 	    le32toh(param.p.sci.initiatorid));
    188  1.1  ad #endif
    189  1.1  ad 
    190  1.1  ad 	sc->sc_adapter.scsipi_cmd = iopsp_scsi_cmd;
    191  1.1  ad 	sc->sc_adapter.scsipi_minphys = iopsp_minphys;
    192  1.1  ad 	sc->sc_adapter.scsipi_ioctl = iopsp_ioctl;
    193  1.1  ad 
    194  1.1  ad 	sc_link = &sc->sc_link;
    195  1.1  ad 	sc_link->type = BUS_SCSI;
    196  1.1  ad 	sc_link->device = &iopsp_dev;
    197  1.1  ad 	sc_link->adapter = &sc->sc_adapter;
    198  1.1  ad 	sc_link->adapter_softc = sc;
    199  1.1  ad 	sc_link->scsipi_scsi.channel = 0;
    200  1.1  ad 	sc_link->scsipi_scsi.adapter_target = le32toh(param.p.sci.initiatorid);
    201  1.1  ad 	sc_link->scsipi_scsi.max_target =
    202  1.1  ad 	    fcal ? IOPSP_MAX_FCAL_TARGET : param.p.sci.maxdatawidth - 1;
    203  1.1  ad 	sc_link->scsipi_scsi.max_lun = IOPSP_MAX_LUN;
    204  1.5  ad 	sc_link->openings = 1;
    205  1.1  ad 
    206  1.1  ad #ifdef I2OVERBOSE
    207  1.1  ad 	/*
    208  1.1  ad 	 * Allocate the target map.  Currently used for informational
    209  1.1  ad 	 * purposes only.
    210  1.1  ad 	 */
    211  1.1  ad 	size = (sc_link->scsipi_scsi.max_target + 1) *
    212  1.1  ad 	    sizeof(struct iopsp_target);
    213  1.1  ad 	sc->sc_targetmap = malloc(size, M_DEVBUF, M_NOWAIT);
    214  1.1  ad 	memset(sc->sc_targetmap, 0, size);
    215  1.1  ad #endif
    216  1.1  ad 
    217  1.3  ad  	/* Build the two maps, and attach to scsipi. */
    218  1.3  ad 	if (iopsp_reconfig(self) != 0) {
    219  1.5  ad 		printf("%s: configure failed\n", sc->sc_dv.dv_xname);
    220  1.3  ad 		goto bad;
    221  1.3  ad 	}
    222  1.1  ad 	config_found(self, sc_link, scsiprint);
    223  1.1  ad 	return;
    224  1.1  ad 
    225  1.5  ad  bad:
    226  1.1  ad 	iop_initiator_unregister(iop, &sc->sc_ii);
    227  1.1  ad }
    228  1.1  ad 
    229  1.1  ad /*
    230  1.3  ad  * Scan the LCT to determine which devices we control, and enter them into
    231  1.3  ad  * the maps.
    232  1.1  ad  */
    233  1.3  ad static int
    234  1.3  ad iopsp_reconfig(struct device *dv)
    235  1.1  ad {
    236  1.3  ad 	struct iopsp_softc *sc;
    237  1.1  ad 	struct iop_softc *iop;
    238  1.1  ad 	struct i2o_lct_entry *le;
    239  1.1  ad 	struct scsipi_link *sc_link;
    240  1.1  ad 	struct {
    241  1.1  ad 		struct	i2o_param_op_results pr;
    242  1.1  ad 		struct	i2o_param_read_results prr;
    243  1.1  ad 		struct	i2o_param_scsi_device_info sdi;
    244  1.1  ad 	} __attribute__ ((__packed__)) param;
    245  1.5  ad 	u_int tid, nent, i, targ, lun, size, s, rv, bptid;
    246  1.1  ad 	u_short *tidmap;
    247  1.1  ad #ifdef I2OVERBOSE
    248  1.1  ad 	struct iopsp_target *it;
    249  1.1  ad 	int syncrate;
    250  1.1  ad #endif
    251  1.1  ad 
    252  1.3  ad 	sc = (struct iopsp_softc *)dv;
    253  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    254  1.1  ad 	sc_link = &sc->sc_link;
    255  1.1  ad 
    256  1.3  ad 	/* Anything to do? */
    257  1.5  ad 	if (iop->sc_chgind == sc->sc_chgind)
    258  1.3  ad 		return (0);
    259  1.3  ad 
    260  1.1  ad 	/*
    261  1.1  ad 	 * Allocate memory for the target/LUN -> TID map.  Use zero to
    262  1.1  ad 	 * denote absent targets (zero is the TID of the I2O executive,
    263  1.1  ad 	 * and we never address that here).
    264  1.1  ad 	 */
    265  1.1  ad 	size = (sc_link->scsipi_scsi.max_target + 1) *
    266  1.1  ad 	    (IOPSP_MAX_LUN + 1) * sizeof(u_short);
    267  1.3  ad 	if ((tidmap = malloc(size, M_DEVBUF, M_WAITOK)) == NULL)
    268  1.3  ad 		return (ENOMEM);
    269  1.3  ad 	memset(tidmap, 0, size);
    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.5  ad 	/*
    277  1.5  ad 	 * A quick hack to handle Intel's stacked bus port arrangement.
    278  1.5  ad 	 */
    279  1.5  ad 	bptid = sc->sc_ii.ii_tid;
    280  1.5  ad 	nent = iop->sc_nlctent;
    281  1.5  ad 	for (le = iop->sc_lct->entry; nent != 0; nent--, le++)
    282  1.5  ad 		if ((le16toh(le->classid) & 4095) ==
    283  1.5  ad 		    I2O_CLASS_BUS_ADAPTER_PORT &&
    284  1.5  ad 		    (le32toh(le->usertid) & 4095) == bptid) {
    285  1.5  ad 			bptid = le32toh(le->localtid) & 4095;
    286  1.5  ad 			break;
    287  1.5  ad 		}
    288  1.5  ad 
    289  1.1  ad 	nent = iop->sc_nlctent;
    290  1.1  ad 	for (i = 0, le = iop->sc_lct->entry; i < nent; i++, le++) {
    291  1.5  ad 		if ((le16toh(le->classid) & 4095) != I2O_CLASS_SCSI_PERIPHERAL)
    292  1.1  ad 			continue;
    293  1.5  ad 		if (((le32toh(le->usertid) >> 12) & 4095) != bptid)
    294  1.1  ad 			continue;
    295  1.1  ad 		tid = le32toh(le->localtid) & 4095;
    296  1.1  ad 
    297  1.5  ad 		rv = iop_param_op(iop, tid, NULL, 0, I2O_PARAM_SCSI_DEVICE_INFO,
    298  1.3  ad 		    &param, sizeof(param));
    299  1.3  ad 		if (rv != 0) {
    300  1.3  ad 			printf("%s: unable to get parameters (0x%04x; %d)\n",
    301  1.3  ad 			    sc->sc_dv.dv_xname, I2O_PARAM_SCSI_DEVICE_INFO,
    302  1.3  ad 			    rv);
    303  1.1  ad 			continue;
    304  1.1  ad 		}
    305  1.1  ad 		targ = le32toh(param.sdi.identifier);
    306  1.1  ad 		lun = param.sdi.luninfo[1];
    307  1.5  ad #if defined(DIAGNOSTIC) || defined(I2ODEBUG)
    308  1.5  ad 		if (targ > sc_link->scsipi_scsi.max_target ||
    309  1.5  ad 		    lun > sc_link->scsipi_scsi.max_lun) {
    310  1.5  ad 			printf("%s: target %d,%d (tid %d): bad target/LUN\n",
    311  1.5  ad 			    sc->sc_dv.dv_xname, targ, lun, tid);
    312  1.1  ad 			continue;
    313  1.1  ad 		}
    314  1.5  ad #endif
    315  1.1  ad 
    316  1.1  ad #ifdef I2OVERBOSE
    317  1.1  ad 		/*
    318  1.1  ad 		 * If we've already described this target, and nothing has
    319  1.1  ad 		 * changed, then don't describe it again.
    320  1.1  ad 		 */
    321  1.1  ad 		it = &sc->sc_targetmap[targ];
    322  1.1  ad 		it->it_flags |= IT_PRESENT;
    323  1.1  ad 		syncrate = ((int)le64toh(param.sdi.negsyncrate) + 500) / 1000;
    324  1.1  ad 		if (it->it_width == param.sdi.negdatawidth &&
    325  1.1  ad 		    it->it_offset == param.sdi.negoffset &&
    326  1.1  ad 		    it->it_syncrate == syncrate)
    327  1.1  ad 			continue;
    328  1.1  ad 
    329  1.1  ad 		it->it_width = param.sdi.negdatawidth;
    330  1.1  ad 		it->it_offset = param.sdi.negoffset;
    331  1.1  ad 		it->it_syncrate = syncrate;
    332  1.1  ad 
    333  1.1  ad 		printf("%s: target %d (tid %d): %d-bit, ", sc->sc_dv.dv_xname,
    334  1.1  ad 		    targ, tid, it->it_width);
    335  1.1  ad 		if (it->it_syncrate == 0)
    336  1.1  ad 			printf("asynchronous\n");
    337  1.1  ad 		else
    338  1.1  ad 			printf("synchronous at %dMHz, offset 0x%x\n",
    339  1.1  ad 			    it->it_syncrate, it->it_offset);
    340  1.1  ad #endif
    341  1.5  ad 
    342  1.5  ad 		/* Ignore the device if it's in use by somebody else. */
    343  1.5  ad 		if ((le32toh(le->usertid) & 4095) != I2O_TID_NONE) {
    344  1.5  ad #ifdef I2OVERBOSE
    345  1.5  ad 			if (sc->sc_tidmap == NULL ||
    346  1.5  ad 			    IOPSP_TIDMAP(sc->sc_tidmap, targ, lun) !=
    347  1.5  ad 			    IOPSP_TID_INUSE)
    348  1.5  ad 				printf("%s: target %d,%d (tid %d): in use by"
    349  1.5  ad 				    " tid %d\n", sc->sc_dv.dv_xname,
    350  1.5  ad 				    targ, lun, tid,
    351  1.5  ad 				    le32toh(le->usertid) & 4095);
    352  1.5  ad #endif
    353  1.5  ad 			IOPSP_TIDMAP(tidmap, targ, lun) = IOPSP_TID_INUSE;
    354  1.5  ad 		} else
    355  1.5  ad 			IOPSP_TIDMAP(tidmap, targ, lun) = (u_short)tid;
    356  1.1  ad 	}
    357  1.1  ad 
    358  1.1  ad #ifdef I2OVERBOSE
    359  1.1  ad 	for (i = 0; i <= sc_link->scsipi_scsi.max_target; i++)
    360  1.1  ad 		if ((sc->sc_targetmap[i].it_flags & IT_PRESENT) == 0)
    361  1.1  ad 			sc->sc_targetmap[i].it_width = 0;
    362  1.1  ad #endif
    363  1.1  ad 
    364  1.1  ad 	/* Swap in the new map and return. */
    365  1.3  ad 	s = splbio();
    366  1.1  ad 	if (sc->sc_tidmap != NULL)
    367  1.1  ad 		free(sc->sc_tidmap, M_DEVBUF);
    368  1.1  ad 	sc->sc_tidmap = tidmap;
    369  1.3  ad 	splx(s);
    370  1.5  ad 	sc->sc_chgind = iop->sc_chgind;
    371  1.3  ad 	return (0);
    372  1.1  ad }
    373  1.1  ad 
    374  1.1  ad /*
    375  1.3  ad  * Adjust the size of an I/O request.
    376  1.1  ad  */
    377  1.1  ad static void
    378  1.1  ad iopsp_minphys(struct buf *bp)
    379  1.1  ad {
    380  1.1  ad 
    381  1.1  ad 	if (bp->b_bcount > IOP_MAX_XFER)
    382  1.1  ad 		bp->b_bcount = IOP_MAX_XFER;
    383  1.1  ad 	minphys(bp);
    384  1.1  ad }
    385  1.1  ad 
    386  1.1  ad /*
    387  1.3  ad  * Re-scan the bus; to be called from a higher level (e.g. scsipi).
    388  1.1  ad  */
    389  1.1  ad static int
    390  1.1  ad iopsp_rescan(struct iopsp_softc *sc)
    391  1.1  ad {
    392  1.1  ad 	struct iop_softc *iop;
    393  1.1  ad 	struct iop_msg *im;
    394  1.5  ad 	struct i2o_hba_bus_scan mf;
    395  1.1  ad 	int rv;
    396  1.1  ad 
    397  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    398  1.1  ad 
    399  1.5  ad 	rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL);
    400  1.3  ad 	if (rv != 0) {
    401  1.3  ad #ifdef I2ODEBUG
    402  1.3  ad 		printf("iopsp_rescan: unable to acquire lock\n");
    403  1.3  ad #endif
    404  1.1  ad 		return (rv);
    405  1.3  ad 	}
    406  1.1  ad 
    407  1.5  ad 	im = iop_msg_alloc(iop, &sc->sc_ii, IM_WAIT);
    408  1.5  ad 
    409  1.5  ad 	mf.msgflags = I2O_MSGFLAGS(i2o_hba_bus_scan);
    410  1.5  ad 	mf.msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_HBA_BUS_SCAN);
    411  1.5  ad 	mf.msgictx = sc->sc_ii.ii_ictx;
    412  1.5  ad 	mf.msgtctx = im->im_tctx;
    413  1.3  ad 
    414  1.5  ad 	rv = iop_msg_post(iop, im, &mf, 5*60*1000);
    415  1.5  ad 	iop_msg_free(iop, im);
    416  1.5  ad 	if (rv != 0)
    417  1.5  ad 		printf("%s: bus rescan failed (error %d)\n",
    418  1.5  ad 		    sc->sc_dv.dv_xname, rv);
    419  1.5  ad 
    420  1.5  ad 	if ((rv = iop_lct_get(iop)) != 0)
    421  1.5  ad 		goto done;
    422  1.3  ad 
    423  1.3  ad 	/* Rebuild the target/LUN -> TID map, release lock, and return. */
    424  1.3  ad 	rv = iopsp_reconfig(&sc->sc_dv);
    425  1.5  ad  done:
    426  1.3  ad 	lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
    427  1.3  ad 	return (rv);
    428  1.1  ad }
    429  1.1  ad 
    430  1.1  ad /*
    431  1.1  ad  * Start a SCSI command.
    432  1.1  ad  */
    433  1.1  ad static int
    434  1.1  ad iopsp_scsi_cmd(struct scsipi_xfer *xs)
    435  1.1  ad {
    436  1.1  ad 	struct scsipi_link *sc_link;
    437  1.1  ad 	struct iopsp_softc *sc;
    438  1.1  ad 	struct iop_msg *im;
    439  1.1  ad 	struct iop_softc *iop;
    440  1.5  ad 	struct i2o_scsi_scb_exec *mf;
    441  1.5  ad 	int error, flags, tid, imf;
    442  1.5  ad 	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    443  1.1  ad 
    444  1.1  ad 	sc_link = xs->sc_link;
    445  1.1  ad 	flags = xs->xs_control;
    446  1.1  ad 	sc = sc_link->adapter_softc;
    447  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    448  1.1  ad 
    449  1.1  ad 	tid = IOPSP_TIDMAP(sc->sc_tidmap, sc_link->scsipi_scsi.target,
    450  1.1  ad 	    sc_link->scsipi_scsi.lun);
    451  1.1  ad 	if (tid == IOPSP_TID_ABSENT || tid == IOPSP_TID_INUSE) {
    452  1.1  ad 		xs->error = XS_SELTIMEOUT;
    453  1.1  ad 		return (COMPLETE);
    454  1.1  ad 	}
    455  1.1  ad 
    456  1.1  ad 	SC_DEBUG(sc_link, SDEV_DB2, ("iopsp_scsi_cmd\n"));
    457  1.1  ad 
    458  1.1  ad 	/* Need to reset the target? */
    459  1.1  ad 	if ((flags & XS_CTL_RESET) != 0) {
    460  1.1  ad 		if (iop_simple_cmd(iop, tid, I2O_SCSI_DEVICE_RESET,
    461  1.3  ad 		    sc->sc_ii.ii_ictx, 1, 10*1000) != 0) {
    462  1.1  ad #ifdef I2ODEBUG
    463  1.1  ad 			printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    464  1.1  ad #endif
    465  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    466  1.1  ad 		}
    467  1.1  ad 		return (COMPLETE);
    468  1.1  ad 	}
    469  1.1  ad 
    470  1.1  ad #if defined(I2ODEBUG) || defined(SCSIDEBUG)
    471  1.5  ad 	if (xs->cmdlen > sizeof(mf->cdb))
    472  1.3  ad 		panic("%s: CDB too large\n", sc->sc_dv.dv_xname);
    473  1.1  ad #endif
    474  1.1  ad 
    475  1.5  ad 	imf = (flags & (XS_CTL_POLL | XS_CTL_NOSLEEP)) != 0 ? IM_POLL : 0;
    476  1.5  ad 	im = iop_msg_alloc(iop, &sc->sc_ii, imf);
    477  1.1  ad 	im->im_dvcontext = xs;
    478  1.1  ad 
    479  1.5  ad 	mf = (struct i2o_scsi_scb_exec *)mb;
    480  1.5  ad 	mf->msgflags = I2O_MSGFLAGS(i2o_scsi_scb_exec);
    481  1.5  ad 	mf->msgfunc = I2O_MSGFUNC(tid, I2O_SCSI_SCB_EXEC);
    482  1.5  ad 	mf->msgictx = sc->sc_ii.ii_ictx;
    483  1.5  ad 	mf->msgtctx = im->im_tctx;
    484  1.5  ad 	mf->flags = xs->cmdlen | I2O_SCB_FLAG_ENABLE_DISCONNECT |
    485  1.1  ad 	    I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
    486  1.5  ad 	mf->datalen = xs->datalen;
    487  1.5  ad 	memcpy(mf->cdb, xs->cmd, xs->cmdlen);
    488  1.1  ad 
    489  1.1  ad 	if ((xs->sc_link->quirks & SDEV_NOTAG) == 0 &&
    490  1.3  ad 	    (xs->xs_control & XS_CTL_POLL) != 0) {
    491  1.1  ad 		if (xs->bp != NULL && (xs->bp->b_flags & B_ASYNC) != 0)
    492  1.5  ad 			mf->flags |= I2O_SCB_FLAG_ORDERED_QUEUE_TAG;
    493  1.1  ad 		else
    494  1.5  ad 			mf->flags |= I2O_SCB_FLAG_SIMPLE_QUEUE_TAG;
    495  1.1  ad 	}
    496  1.1  ad 
    497  1.1  ad 	if (xs->datalen != 0) {
    498  1.5  ad 		error = iop_msg_map_bio(iop, im, mb, xs->data, xs->datalen,
    499  1.5  ad 		    (flags & XS_CTL_DATA_OUT) != 0);
    500  1.1  ad 		if (error) {
    501  1.1  ad #ifdef I2ODEBUG
    502  1.1  ad 			printf("%s: error %d mapping xfer\n",
    503  1.1  ad 			    sc->sc_dv.dv_xname, error);
    504  1.1  ad #endif
    505  1.1  ad 			xs->error = XS_DRIVER_STUFFUP;
    506  1.5  ad 			iop_msg_free(iop, im);
    507  1.1  ad 			return (COMPLETE);
    508  1.1  ad 		}
    509  1.1  ad 		if ((flags & XS_CTL_DATA_IN) == 0)
    510  1.5  ad 			mf->flags |= I2O_SCB_FLAG_XFER_TO_DEVICE;
    511  1.1  ad 		else
    512  1.5  ad 			mf->flags |= I2O_SCB_FLAG_XFER_FROM_DEVICE;
    513  1.1  ad 	}
    514  1.1  ad 
    515  1.1  ad 	if ((flags & XS_CTL_POLL) == 0) {
    516  1.5  ad 		if (iop_msg_post(iop, im, mb, 0)) {
    517  1.5  ad 			if (xs->datalen != 0)
    518  1.5  ad 				iop_msg_unmap(iop, im);
    519  1.5  ad 			iop_msg_free(iop, im);
    520  1.5  ad 			xs->error = XS_DRIVER_STUFFUP;
    521  1.5  ad 			return (COMPLETE);
    522  1.5  ad 		}
    523  1.1  ad 		return (SUCCESSFULLY_QUEUED);
    524  1.1  ad 	}
    525  1.1  ad 
    526  1.5  ad 	if (iop_msg_post(iop, im, mb, xs->timeout)) {
    527  1.1  ad 		scsi_print_addr(xs->sc_link);
    528  1.1  ad 		printf("timeout; aborting command\n");
    529  1.3  ad 		if (iopsp_scsi_abort(sc, tid, im)) {
    530  1.1  ad 			scsi_print_addr(xs->sc_link);
    531  1.1  ad 			printf("abort failed\n");
    532  1.1  ad 		}
    533  1.1  ad 		xs->error = XS_DRIVER_STUFFUP;
    534  1.1  ad 	}
    535  1.5  ad 
    536  1.1  ad 	return (COMPLETE);
    537  1.1  ad }
    538  1.1  ad 
    539  1.1  ad /*
    540  1.1  ad  * Abort the specified I2O_SCSI_SCB_EXEC message and its associated SCB.
    541  1.1  ad  */
    542  1.1  ad static int
    543  1.3  ad iopsp_scsi_abort(struct iopsp_softc *sc, int atid, struct iop_msg *aim)
    544  1.1  ad {
    545  1.1  ad 	struct iop_msg *im;
    546  1.5  ad 	struct i2o_scsi_scb_abort mf;
    547  1.1  ad 	struct iop_softc *iop;
    548  1.5  ad 	int rv, s;
    549  1.1  ad 
    550  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    551  1.5  ad 	im = iop_msg_alloc(iop, &sc->sc_ii, IM_POLL);
    552  1.1  ad 
    553  1.5  ad 	mf.msgflags = I2O_MSGFLAGS(i2o_scsi_scb_abort);
    554  1.5  ad 	mf.msgfunc = I2O_MSGFUNC(atid, I2O_SCSI_SCB_ABORT);
    555  1.5  ad 	mf.msgictx = sc->sc_ii.ii_ictx;
    556  1.5  ad 	mf.msgtctx = im->im_tctx;
    557  1.5  ad 	mf.tctxabort = aim->im_tctx;
    558  1.1  ad 
    559  1.5  ad 	s = splbio();
    560  1.5  ad 	rv = iop_msg_post(iop, im, &mf, 30000);
    561  1.5  ad 	splx(s);
    562  1.5  ad 	iop_msg_free(iop, im);
    563  1.1  ad 	return (rv);
    564  1.1  ad }
    565  1.1  ad 
    566  1.1  ad /*
    567  1.1  ad  * We have a message which has been processed and replied to by the IOP -
    568  1.1  ad  * deal with it.
    569  1.1  ad  */
    570  1.1  ad static void
    571  1.1  ad iopsp_intr(struct device *dv, struct iop_msg *im, void *reply)
    572  1.1  ad {
    573  1.1  ad 	struct scsipi_xfer *xs;
    574  1.1  ad 	struct iopsp_softc *sc;
    575  1.1  ad 	struct i2o_scsi_reply *rb;
    576  1.5  ad  	struct iop_softc *iop;
    577  1.5  ad 	u_int sl;
    578  1.2  ad 
    579  1.1  ad 	sc = (struct iopsp_softc *)dv;
    580  1.1  ad 	xs = (struct scsipi_xfer *)im->im_dvcontext;
    581  1.1  ad 	iop = (struct iop_softc *)dv->dv_parent;
    582  1.1  ad 
    583  1.1  ad 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("iopsp_intr\n"));
    584  1.1  ad 
    585  1.5  ad 	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0) {
    586  1.5  ad 		xs->error = XS_DRIVER_STUFFUP;
    587  1.5  ad 		xs->resid = xs->datalen;
    588  1.5  ad 	} else {
    589  1.1  ad 		rb = reply;
    590  1.1  ad 
    591  1.5  ad 		if (rb->hbastatus != I2O_SCSI_DSC_SUCCESS) {
    592  1.5  ad 			switch (rb->hbastatus) {
    593  1.1  ad 			case I2O_SCSI_DSC_ADAPTER_BUSY:
    594  1.1  ad 			case I2O_SCSI_DSC_SCSI_BUS_RESET:
    595  1.1  ad 			case I2O_SCSI_DSC_BUS_BUSY:
    596  1.1  ad 				xs->error = XS_BUSY;
    597  1.1  ad 				break;
    598  1.1  ad 			case I2O_SCSI_DSC_SELECTION_TIMEOUT:
    599  1.1  ad 				xs->error = XS_SELTIMEOUT;
    600  1.1  ad 				break;
    601  1.1  ad 			case I2O_SCSI_DSC_COMMAND_TIMEOUT:
    602  1.1  ad 			case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
    603  1.1  ad 			case I2O_SCSI_DSC_LUN_INVALID:
    604  1.1  ad 			case I2O_SCSI_DSC_SCSI_TID_INVALID:
    605  1.1  ad 				xs->error = XS_TIMEOUT;
    606  1.1  ad 				break;
    607  1.1  ad 			default:
    608  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    609  1.1  ad 				break;
    610  1.1  ad 			}
    611  1.1  ad 			printf("%s: HBA status 0x%02x\n", sc->sc_dv.dv_xname,
    612  1.5  ad 			   rb->hbastatus);
    613  1.5  ad 		} else if (rb->scsistatus != SCSI_OK) {
    614  1.5  ad 			switch (rb->scsistatus) {
    615  1.1  ad 			case SCSI_CHECK:
    616  1.1  ad 				xs->error = XS_SENSE;
    617  1.1  ad 				sl = le32toh(rb->senselen);
    618  1.1  ad 				if (sl > sizeof(xs->sense.scsi_sense))
    619  1.5  ad 					sl = sizeof(xs->sense.scsi_sense);
    620  1.1  ad 				memcpy(&xs->sense.scsi_sense, rb->sense, sl);
    621  1.1  ad 				break;
    622  1.1  ad 			case SCSI_BUSY:
    623  1.1  ad 				xs->error = XS_BUSY;
    624  1.1  ad 				break;
    625  1.1  ad 			default:
    626  1.1  ad 				xs->error = XS_DRIVER_STUFFUP;
    627  1.1  ad 				break;
    628  1.1  ad 			}
    629  1.1  ad 		} else
    630  1.1  ad 			xs->error = XS_NOERROR;
    631  1.1  ad 
    632  1.5  ad 		xs->resid = xs->datalen - le32toh(rb->datalen);
    633  1.5  ad 		xs->status = rb->scsistatus;
    634  1.1  ad 	}
    635  1.1  ad 
    636  1.1  ad 	/* Free the message wrapper and pass the news to scsipi. */
    637  1.5  ad 	if (xs->datalen != 0)
    638  1.5  ad 		iop_msg_unmap(iop, im);
    639  1.5  ad 	iop_msg_free(iop, im);
    640  1.1  ad 	xs->xs_status |= XS_STS_DONE;
    641  1.1  ad 	scsipi_done(xs);
    642  1.1  ad }
    643  1.1  ad 
    644  1.1  ad /*
    645  1.1  ad  * ioctl hook; used here only to initiate low-level rescans.
    646  1.1  ad  */
    647  1.1  ad static int
    648  1.1  ad iopsp_ioctl(struct scsipi_link *sc_link, u_long cmd, caddr_t data, int flag,
    649  1.1  ad 	    struct proc *p)
    650  1.1  ad {
    651  1.1  ad 	int rv;
    652  1.1  ad 
    653  1.1  ad 	switch (cmd) {
    654  1.1  ad 	case SCBUSIOLLSCAN:
    655  1.5  ad 		/*
    656  1.5  ad 		 * If it's boot time, the bus will have been scanned and the
    657  1.5  ad 		 * maps built.  Locking would stop re-configuration, but we
    658  1.5  ad 		 * want to fake success.
    659  1.5  ad 		 */
    660  1.5  ad 		if (p != &proc0)
    661  1.5  ad 			rv = iopsp_rescan(sc_link->adapter_softc);
    662  1.5  ad 		else
    663  1.5  ad 			rv = 0;
    664  1.1  ad 		break;
    665  1.5  ad 
    666  1.1  ad 	default:
    667  1.5  ad 		rv = ENOTTY;
    668  1.1  ad 		break;
    669  1.1  ad 	}
    670  1.1  ad 
    671  1.1  ad 	return (rv);
    672  1.5  ad }
    673  1.5  ad 
    674  1.5  ad /*
    675  1.5  ad  * The number of openings available to us has changed, so inform scsipi.
    676  1.5  ad  */
    677  1.5  ad static void
    678  1.5  ad iopsp_adjqparam(struct device *dv, int mpi)
    679  1.5  ad {
    680  1.5  ad 	int s;
    681  1.5  ad 
    682  1.5  ad 	/* XXX */
    683  1.5  ad 	s = splbio();
    684  1.5  ad 	((struct iopsp_softc *)dv)->sc_link.openings = mpi;
    685  1.5  ad 	splx(s);
    686  1.1  ad }
    687