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