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