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