scsi_base.c revision 1.70 1 1.70 enami /* $NetBSD: scsi_base.c,v 1.70 1999/10/20 15:22:27 enami 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.49 enami
39 1.1 mycroft #include <sys/types.h>
40 1.1 mycroft #include <sys/param.h>
41 1.30 thorpej #include <sys/systm.h>
42 1.11 mycroft #include <sys/kernel.h>
43 1.1 mycroft #include <sys/buf.h>
44 1.1 mycroft #include <sys/uio.h>
45 1.1 mycroft #include <sys/malloc.h>
46 1.1 mycroft #include <sys/errno.h>
47 1.1 mycroft #include <sys/device.h>
48 1.33 christos #include <sys/proc.h>
49 1.2 mycroft
50 1.47 bouyer #include <dev/scsipi/scsipi_all.h>
51 1.47 bouyer #include <dev/scsipi/scsi_all.h>
52 1.47 bouyer #include <dev/scsipi/scsi_disk.h>
53 1.47 bouyer #include <dev/scsipi/scsiconf.h>
54 1.47 bouyer #include <dev/scsipi/scsipi_base.h>
55 1.47 bouyer
56 1.1 mycroft /*
57 1.1 mycroft * Do a scsi operation, asking a device to run as SCSI-II if it can.
58 1.1 mycroft */
59 1.50 enami int
60 1.1 mycroft scsi_change_def(sc_link, flags)
61 1.47 bouyer struct scsipi_link *sc_link;
62 1.11 mycroft int flags;
63 1.1 mycroft {
64 1.47 bouyer struct scsi_changedef scsipi_cmd;
65 1.1 mycroft
66 1.47 bouyer bzero(&scsipi_cmd, 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.55 thorpej return (scsipi_command(sc_link,
71 1.50 enami (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
72 1.50 enami 0, 0, 2, 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.47 bouyer scsi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
83 1.47 bouyer retries, timeout, bp, flags)
84 1.47 bouyer struct scsipi_link *sc_link;
85 1.47 bouyer struct scsipi_generic *scsipi_cmd;
86 1.22 mycroft int cmdlen;
87 1.22 mycroft u_char *data_addr;
88 1.22 mycroft int datalen;
89 1.22 mycroft int retries;
90 1.22 mycroft int timeout;
91 1.22 mycroft struct buf *bp;
92 1.22 mycroft int flags;
93 1.22 mycroft {
94 1.47 bouyer struct scsipi_xfer *xs;
95 1.67 pk int error, s;
96 1.22 mycroft
97 1.47 bouyer SC_DEBUG(sc_link, SDEV_DB2, ("scsi_scsipi_cmd\n"));
98 1.22 mycroft
99 1.25 mycroft #ifdef DIAGNOSTIC
100 1.69 thorpej if (bp != NULL && (flags & XS_CTL_ASYNC) == 0)
101 1.69 thorpej panic("scsi_scsipi_cmd: buffer without async");
102 1.25 mycroft #endif
103 1.25 mycroft
104 1.50 enami if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr,
105 1.68 thorpej datalen, retries, timeout, bp, flags)) == NULL) {
106 1.68 thorpej if (bp != NULL) {
107 1.68 thorpej s = splbio();
108 1.68 thorpej bp->b_flags |= B_ERROR;
109 1.68 thorpej bp->b_error = ENOMEM;
110 1.68 thorpej biodone(bp);
111 1.68 thorpej splx(s);
112 1.68 thorpej }
113 1.50 enami return (ENOMEM);
114 1.68 thorpej }
115 1.58 cgd
116 1.58 cgd /*
117 1.58 cgd * Set the LUN in the CDB if we have an older device. We also
118 1.58 cgd * set it for more modern SCSI-II devices "just in case".
119 1.58 cgd */
120 1.58 cgd if ((sc_link->scsipi_scsi.scsi_version & SID_ANSII) <= 2)
121 1.58 cgd xs->cmd->bytes[0] |=
122 1.58 cgd ((sc_link->scsipi_scsi.lun << SCSI_CMD_LUN_SHIFT) &
123 1.58 cgd SCSI_CMD_LUN_MASK);
124 1.22 mycroft
125 1.47 bouyer if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
126 1.50 enami return (0);
127 1.22 mycroft
128 1.1 mycroft /*
129 1.1 mycroft * we have finished with the xfer stuct, free it and
130 1.1 mycroft * check if anyone else needs to be started up.
131 1.1 mycroft */
132 1.67 pk s = splbio();
133 1.47 bouyer scsipi_free_xs(xs, flags);
134 1.67 pk splx(s);
135 1.50 enami return (error);
136 1.1 mycroft }
137 1.1 mycroft
138 1.1 mycroft /*
139 1.1 mycroft * Utility routines often used in SCSI stuff
140 1.1 mycroft */
141 1.1 mycroft
142 1.1 mycroft
143 1.1 mycroft /*
144 1.1 mycroft * Print out the scsi_link structure's address info.
145 1.1 mycroft */
146 1.1 mycroft void
147 1.47 bouyer scsi_print_addr(sc_link)
148 1.47 bouyer struct scsipi_link *sc_link;
149 1.1 mycroft {
150 1.1 mycroft
151 1.40 christos printf("%s(%s:%d:%d): ",
152 1.50 enami sc_link->device_softc ?
153 1.50 enami ((struct device *)sc_link->device_softc)->dv_xname : "probe",
154 1.50 enami ((struct device *)sc_link->adapter_softc)->dv_xname,
155 1.50 enami sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun);
156 1.70 enami }
157 1.70 enami
158 1.70 enami /*
159 1.70 enami * Kill off all pending xfers for a scsipi_link.
160 1.70 enami *
161 1.70 enami * Must be called at splbio().
162 1.70 enami */
163 1.70 enami void
164 1.70 enami scsi_kill_pending(sc_link)
165 1.70 enami struct scsipi_link *sc_link;
166 1.70 enami {
167 1.70 enami struct scsipi_xfer *xs;
168 1.70 enami
169 1.70 enami while ((xs = TAILQ_FIRST(&sc_link->pending_xfers)) != NULL) {
170 1.70 enami xs->xs_status |= XS_STS_DONE;
171 1.70 enami xs->error = XS_DRIVER_STUFFUP;
172 1.70 enami scsipi_done(xs);
173 1.70 enami }
174 1.1 mycroft }
175