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