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