1 /* $NetBSD: scsi_base.c,v 1.90 2012/04/20 20:23:21 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2004 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: scsi_base.c,v 1.90 2012/04/20 20:23:21 bouyer Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/buf.h>
39 #include <sys/uio.h>
40 #include <sys/malloc.h>
41 #include <sys/errno.h>
42 #include <sys/device.h>
43 #include <sys/proc.h>
44
45 #include <dev/scsipi/scsipi_all.h>
46 #include <dev/scsipi/scsi_all.h>
47 #include <dev/scsipi/scsi_disk.h>
48 #include <dev/scsipi/scsiconf.h>
49 #include <dev/scsipi/scsipi_base.h>
50
51 static void scsi_print_xfer_mode(struct scsipi_periph *);
52 /*
53 * Do a scsi operation, asking a device to run as SCSI-II if it can.
54 */
55 int
scsi_change_def(struct scsipi_periph * periph,int flags)56 scsi_change_def(struct scsipi_periph *periph, int flags)
57 {
58 struct scsi_changedef cmd;
59
60 memset(&cmd, 0, sizeof(cmd));
61 cmd.opcode = SCSI_CHANGE_DEFINITION;
62 cmd.how = SC_SCSI_2;
63
64 return (scsipi_command(periph, (void *)&cmd, sizeof(cmd), 0, 0,
65 SCSIPIRETRIES, 100000, NULL, flags));
66 }
67
68 /*
69 * ask the scsi driver to perform a command for us.
70 * tell it where to read/write the data, and how
71 * long the data is supposed to be. If we have a buf
72 * to associate with the transfer, we need that too.
73 */
74 void
scsi_scsipi_cmd(struct scsipi_xfer * xs)75 scsi_scsipi_cmd(struct scsipi_xfer *xs)
76 {
77 struct scsipi_periph *periph = xs->xs_periph;
78
79 SC_DEBUG(periph, SCSIPI_DB2, ("scsi_scsipi_cmd\n"));
80
81 /*
82 * Set the LUN in the CDB if we have an older device. We also
83 * set it for more modern SCSI-2 devices "just in case".
84 */
85 if (periph->periph_version <= 2)
86 xs->cmd->bytes[0] |=
87 ((periph->periph_lun << SCSI_CMD_LUN_SHIFT) &
88 SCSI_CMD_LUN_MASK);
89 }
90
91 /*
92 * Utility routines often used in SCSI stuff
93 */
94
95 /*
96 * Print out the periph's address info.
97 */
98 void
scsi_print_addr(struct scsipi_periph * periph)99 scsi_print_addr(struct scsipi_periph *periph)
100 {
101 struct scsipi_channel *chan = periph->periph_channel;
102 struct scsipi_adapter *adapt = chan->chan_adapter;
103
104 printf("%s(%s:%d:%d:%d): ", periph->periph_dev != NULL ?
105 device_xname(periph->periph_dev) : "probe",
106 device_xname(adapt->adapt_dev),
107 chan->chan_channel, periph->periph_target,
108 periph->periph_lun);
109 }
110
111 /*
112 * Kill off all pending xfers for a periph.
113 *
114 * Must be called at splbio().
115 */
116 void
scsi_kill_pending(struct scsipi_periph * periph)117 scsi_kill_pending(struct scsipi_periph *periph)
118 {
119 }
120
121 /*
122 * scsi_print_xfer_mode:
123 *
124 * Print a parallel SCSI periph's capabilities.
125 */
126 static void
scsi_print_xfer_mode(struct scsipi_periph * periph)127 scsi_print_xfer_mode(struct scsipi_periph *periph)
128 {
129 int period, freq, speed, mbs;
130
131 aprint_normal_dev(periph->periph_dev, "");
132 if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
133 period = scsipi_sync_factor_to_period(periph->periph_period);
134 aprint_normal("sync (%d.%02dns offset %d)",
135 period / 100, period % 100, periph->periph_offset);
136 } else
137 aprint_normal("async");
138
139 if (periph->periph_mode & PERIPH_CAP_WIDE32)
140 aprint_normal(", 32-bit");
141 else if (periph->periph_mode & (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
142 aprint_normal(", 16-bit");
143 else
144 aprint_normal(", 8-bit");
145
146 if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
147 freq = scsipi_sync_factor_to_freq(periph->periph_period);
148 speed = freq;
149 if (periph->periph_mode & PERIPH_CAP_WIDE32)
150 speed *= 4;
151 else if (periph->periph_mode &
152 (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
153 speed *= 2;
154 mbs = speed / 1000;
155 if (mbs > 0) {
156 aprint_normal(" (%d.%03dMB/s)", mbs,
157 speed % 1000);
158 } else
159 aprint_normal(" (%dKB/s)", speed % 1000);
160 }
161
162 aprint_normal(" transfers");
163
164 if (periph->periph_mode & PERIPH_CAP_TQING)
165 aprint_normal(", tagged queueing");
166
167 aprint_normal("\n");
168 }
169
170 /*
171 * scsi_async_event_xfer_mode:
172 *
173 * Update the xfer mode for all parallel SCSI periphs sharing the
174 * specified I_T Nexus.
175 */
176 void
scsi_async_event_xfer_mode(struct scsipi_channel * chan,void * arg)177 scsi_async_event_xfer_mode(struct scsipi_channel *chan, void *arg)
178 {
179 struct scsipi_xfer_mode *xm = arg;
180 struct scsipi_periph *periph;
181 int lun, announce, mode, period, offset;
182
183 for (lun = 0; lun < chan->chan_nluns; lun++) {
184 periph = scsipi_lookup_periph(chan, xm->xm_target, lun);
185 if (periph == NULL)
186 continue;
187 announce = 0;
188
189 /*
190 * Clamp the xfer mode down to this periph's capabilities.
191 */
192 mode = xm->xm_mode & periph->periph_cap;
193 if (mode & PERIPH_CAP_SYNC) {
194 period = xm->xm_period;
195 offset = xm->xm_offset;
196 } else {
197 period = 0;
198 offset = 0;
199 }
200
201 /*
202 * If we do not have a valid xfer mode yet, or the parameters
203 * are different, announce them.
204 */
205 if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
206 periph->periph_mode != mode ||
207 periph->periph_period != period ||
208 periph->periph_offset != offset)
209 announce = 1;
210
211 periph->periph_mode = mode;
212 periph->periph_period = period;
213 periph->periph_offset = offset;
214 periph->periph_flags |= PERIPH_MODE_VALID;
215
216 if (announce)
217 scsi_print_xfer_mode(periph);
218 }
219 }
220
221 /*
222 * scsipi_async_event_xfer_mode:
223 *
224 * Update the xfer mode for all SAS/FC periphs sharing the
225 * specified I_T Nexus.
226 */
227 void
scsi_fc_sas_async_event_xfer_mode(struct scsipi_channel * chan,void * arg)228 scsi_fc_sas_async_event_xfer_mode(struct scsipi_channel *chan, void *arg)
229 {
230 struct scsipi_xfer_mode *xm = arg;
231 struct scsipi_periph *periph;
232 int lun, announce, mode;
233
234 for (lun = 0; lun < chan->chan_nluns; lun++) {
235 periph = scsipi_lookup_periph(chan, xm->xm_target, lun);
236 if (periph == NULL)
237 continue;
238 announce = 0;
239
240 /*
241 * Clamp the xfer mode down to this periph's capabilities.
242 */
243 mode = xm->xm_mode & periph->periph_cap;
244 /*
245 * If we do not have a valid xfer mode yet, or the parameters
246 * are different, announce them.
247 */
248 if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
249 periph->periph_mode != mode)
250 announce = 1;
251
252 periph->periph_mode = mode;
253 periph->periph_flags |= PERIPH_MODE_VALID;
254
255 if (announce &&
256 (periph->periph_mode & PERIPH_CAP_TQING) != 0) {
257 aprint_normal_dev(periph->periph_dev,
258 "tagged queueing\n");
259 }
260 }
261 }
262