scsi_base.c revision 1.69 1 /* $NetBSD: scsi_base.c,v 1.69 1999/09/30 22:57:53 thorpej 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/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/buf.h>
44 #include <sys/uio.h>
45 #include <sys/malloc.h>
46 #include <sys/errno.h>
47 #include <sys/device.h>
48 #include <sys/proc.h>
49
50 #include <dev/scsipi/scsipi_all.h>
51 #include <dev/scsipi/scsi_all.h>
52 #include <dev/scsipi/scsi_disk.h>
53 #include <dev/scsipi/scsiconf.h>
54 #include <dev/scsipi/scsipi_base.h>
55
56 /*
57 * Do a scsi operation, asking a device to run as SCSI-II if it can.
58 */
59 int
60 scsi_change_def(sc_link, flags)
61 struct scsipi_link *sc_link;
62 int flags;
63 {
64 struct scsi_changedef scsipi_cmd;
65
66 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
67 scsipi_cmd.opcode = SCSI_CHANGE_DEFINITION;
68 scsipi_cmd.how = SC_SCSI_2;
69
70 return (scsipi_command(sc_link,
71 (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
72 0, 0, 2, 100000, NULL, flags));
73 }
74
75 /*
76 * ask the scsi driver to perform a command for us.
77 * tell it where to read/write the data, and how
78 * long the data is supposed to be. If we have a buf
79 * to associate with the transfer, we need that too.
80 */
81 int
82 scsi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
83 retries, timeout, bp, flags)
84 struct scsipi_link *sc_link;
85 struct scsipi_generic *scsipi_cmd;
86 int cmdlen;
87 u_char *data_addr;
88 int datalen;
89 int retries;
90 int timeout;
91 struct buf *bp;
92 int flags;
93 {
94 struct scsipi_xfer *xs;
95 int error, s;
96
97 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_scsipi_cmd\n"));
98
99 #ifdef DIAGNOSTIC
100 if (bp != NULL && (flags & XS_CTL_ASYNC) == 0)
101 panic("scsi_scsipi_cmd: buffer without async");
102 #endif
103
104 if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr,
105 datalen, retries, timeout, bp, flags)) == NULL) {
106 if (bp != NULL) {
107 s = splbio();
108 bp->b_flags |= B_ERROR;
109 bp->b_error = ENOMEM;
110 biodone(bp);
111 splx(s);
112 }
113 return (ENOMEM);
114 }
115
116 /*
117 * Set the LUN in the CDB if we have an older device. We also
118 * set it for more modern SCSI-II devices "just in case".
119 */
120 if ((sc_link->scsipi_scsi.scsi_version & SID_ANSII) <= 2)
121 xs->cmd->bytes[0] |=
122 ((sc_link->scsipi_scsi.lun << SCSI_CMD_LUN_SHIFT) &
123 SCSI_CMD_LUN_MASK);
124
125 if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
126 return (0);
127
128 /*
129 * we have finished with the xfer stuct, free it and
130 * check if anyone else needs to be started up.
131 */
132 s = splbio();
133 scsipi_free_xs(xs, flags);
134 splx(s);
135 return (error);
136 }
137
138 /*
139 * Utility routines often used in SCSI stuff
140 */
141
142
143 /*
144 * Print out the scsi_link structure's address info.
145 */
146 void
147 scsi_print_addr(sc_link)
148 struct scsipi_link *sc_link;
149 {
150
151 printf("%s(%s:%d:%d): ",
152 sc_link->device_softc ?
153 ((struct device *)sc_link->device_softc)->dv_xname : "probe",
154 ((struct device *)sc_link->adapter_softc)->dv_xname,
155 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun);
156 }
157