Home | History | Annotate | Line # | Download | only in scsipi
scsi_base.c revision 1.73.6.3
      1 /*	$NetBSD: scsi_base.c,v 1.73.6.3 2001/11/14 19:16:02 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: scsi_base.c,v 1.73.6.3 2001/11/14 19:16:02 nathanw Exp $");
     41 
     42 #include <sys/types.h>
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/buf.h>
     47 #include <sys/uio.h>
     48 #include <sys/malloc.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 #include <sys/proc.h>
     52 
     53 #include <dev/scsipi/scsipi_all.h>
     54 #include <dev/scsipi/scsi_all.h>
     55 #include <dev/scsipi/scsi_disk.h>
     56 #include <dev/scsipi/scsiconf.h>
     57 #include <dev/scsipi/scsipi_base.h>
     58 
     59 /*
     60  * Do a scsi operation, asking a device to run as SCSI-II if it can.
     61  */
     62 int
     63 scsi_change_def(periph, flags)
     64 	struct scsipi_periph *periph;
     65 	int flags;
     66 {
     67 	struct scsi_changedef scsipi_cmd;
     68 
     69 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
     70 	scsipi_cmd.opcode = SCSI_CHANGE_DEFINITION;
     71 	scsipi_cmd.how = SC_SCSI_2;
     72 
     73 	return (scsipi_command(periph,
     74 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
     75 	    0, 0, SCSIPIRETRIES, 100000, NULL, flags));
     76 }
     77 
     78 /*
     79  * ask the scsi driver to perform a command for us.
     80  * tell it where to read/write the data, and how
     81  * long the data is supposed to be. If we have  a buf
     82  * to associate with the transfer, we need that too.
     83  */
     84 int
     85 scsi_scsipi_cmd(periph, scsipi_cmd, cmdlen, data, datalen,
     86 	retries, timeout, bp, flags)
     87 	struct scsipi_periph *periph;
     88 	struct scsipi_generic *scsipi_cmd;
     89 	int cmdlen;
     90 	void *data;
     91 	size_t datalen;
     92 	int retries;
     93 	int timeout;
     94 	struct buf *bp;
     95 	int flags;
     96 {
     97 	struct scsipi_xfer *xs;
     98 	int error, s;
     99 
    100 	SC_DEBUG(periph, SCSIPI_DB2, ("scsi_scsipi_cmd\n"));
    101 
    102 #ifdef DIAGNOSTIC
    103 	if (bp != NULL && (flags & XS_CTL_ASYNC) == 0)
    104 		panic("scsi_scsipi_cmd: buffer without async");
    105 #endif
    106 
    107 	if ((xs = scsipi_make_xs(periph, scsipi_cmd, cmdlen, data,
    108 	    datalen, retries, timeout, bp, flags)) == NULL) {
    109 		if (bp != NULL) {
    110 			s = splbio();
    111 			bp->b_flags |= B_ERROR;
    112 			bp->b_error = ENOMEM;
    113 			biodone(bp);
    114 			splx(s);
    115 		}
    116 		return (ENOMEM);
    117 	}
    118 
    119 	/*
    120 	 * Set the LUN in the CDB if we have an older device.  We also
    121 	 * set it for more modern SCSI-2 devices "just in case".
    122 	 */
    123 	if (periph->periph_version <= 2)
    124 		xs->cmd->bytes[0] |=
    125 		    ((periph->periph_lun << SCSI_CMD_LUN_SHIFT) &
    126 			SCSI_CMD_LUN_MASK);
    127 
    128 	if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
    129 		return (0);
    130 	return (error);
    131 }
    132 
    133 /*
    134  * Utility routines often used in SCSI stuff
    135  */
    136 
    137 /*
    138  * Print out the periph's address info.
    139  */
    140 void
    141 scsi_print_addr(periph)
    142 	struct scsipi_periph *periph;
    143 {
    144 	struct scsipi_channel *chan = periph->periph_channel;
    145 	struct scsipi_adapter *adapt = chan->chan_adapter;
    146 
    147 	printf("%s(%s:%d:%d:%d): ", periph->periph_dev != NULL ?
    148 	    periph->periph_dev->dv_xname : "probe",
    149 	    adapt->adapt_dev->dv_xname,
    150 	    chan->chan_channel, periph->periph_target,
    151 	    periph->periph_lun);
    152 }
    153 
    154 /*
    155  * Kill off all pending xfers for a periph.
    156  *
    157  * Must be called at splbio().
    158  */
    159 void
    160 scsi_kill_pending(periph)
    161 	struct scsipi_periph *periph;
    162 {
    163 	struct scsipi_xfer *xs;
    164 
    165 	while ((xs = TAILQ_FIRST(&periph->periph_xferq)) != NULL) {
    166 		xs->error = XS_DRIVER_STUFFUP;
    167 		scsipi_done(xs);
    168 	}
    169 }
    170