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