xref: /netbsd-src/sys/dev/scsipi/scsi_base.c (revision 422da90206af4aa93240398e2e9a776a68390dba)
1 /*	$NetBSD: scsi_base.c,v 1.93 2019/05/03 16:06:56 mlelstv 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.93 2019/05/03 16:06:56 mlelstv 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 with channel lock held
115  */
116 void
scsi_kill_pending(struct scsipi_periph * periph)117 scsi_kill_pending(struct scsipi_periph *periph)
118 {
119 	struct scsipi_xfer *xs;
120 
121 	TAILQ_FOREACH(xs, &periph->periph_xferq, device_q) {
122 		callout_stop(&xs->xs_callout);
123 		scsi_print_addr(periph);
124 		printf("killed ");
125 		scsipi_print_cdb(xs->cmd);
126 		xs->error = XS_DRIVER_STUFFUP;
127 		scsipi_done(xs);
128 	}
129 }
130 
131 /*
132  * scsi_print_xfer_mode:
133  *
134  *	Print a parallel SCSI periph's capabilities.
135  */
136 static void
scsi_print_xfer_mode(struct scsipi_periph * periph)137 scsi_print_xfer_mode(struct scsipi_periph *periph)
138 {
139 	struct scsipi_channel *chan = periph->periph_channel;
140 	struct scsipi_adapter *adapt = chan->chan_adapter;
141 	int period, freq, speed, mbs;
142 
143 	if (periph->periph_dev)
144 		aprint_normal_dev(periph->periph_dev, "");
145 	else
146 		aprint_normal("probe(%s:%d:%d:%d): ",
147 			device_xname(adapt->adapt_dev),
148 			chan->chan_channel, periph->periph_target,
149 			periph->periph_lun);
150 	if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
151 		period = scsipi_sync_factor_to_period(periph->periph_period);
152 		aprint_normal("sync (%d.%02dns offset %d)",
153 		    period / 100, period % 100, periph->periph_offset);
154 	} else
155 		aprint_normal("async");
156 
157 	if (periph->periph_mode & PERIPH_CAP_WIDE32)
158 		aprint_normal(", 32-bit");
159 	else if (periph->periph_mode & (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
160 		aprint_normal(", 16-bit");
161 	else
162 		aprint_normal(", 8-bit");
163 
164 	if (periph->periph_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT)) {
165 		freq = scsipi_sync_factor_to_freq(periph->periph_period);
166 		speed = freq;
167 		if (periph->periph_mode & PERIPH_CAP_WIDE32)
168 			speed *= 4;
169 		else if (periph->periph_mode &
170 		    (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
171 			speed *= 2;
172 		mbs = speed / 1000;
173 		if (mbs > 0) {
174 			aprint_normal(" (%d.%03dMB/s)", mbs,
175 			    speed % 1000);
176 		} else
177 			aprint_normal(" (%dKB/s)", speed % 1000);
178 	}
179 
180 	aprint_normal(" transfers");
181 
182 	if (periph->periph_mode & PERIPH_CAP_TQING)
183 		aprint_normal(", tagged queueing");
184 
185 	aprint_normal("\n");
186 }
187 
188 /*
189  * scsi_async_event_xfer_mode:
190  *
191  *	Update the xfer mode for all parallel SCSI periphs sharing the
192  *	specified I_T Nexus.
193  */
194 void
scsi_async_event_xfer_mode(struct scsipi_channel * chan,void * arg)195 scsi_async_event_xfer_mode(struct scsipi_channel *chan, void *arg)
196 {
197 	struct scsipi_xfer_mode *xm = arg;
198 	struct scsipi_periph *periph;
199 	int lun, announce, mode, period, offset;
200 
201 	for (lun = 0; lun < chan->chan_nluns; lun++) {
202 		periph = scsipi_lookup_periph_locked(chan, xm->xm_target, lun);
203 		if (periph == NULL)
204 			continue;
205 		announce = 0;
206 
207 		/*
208 		 * Clamp the xfer mode down to this periph's capabilities.
209 		 */
210 		mode = xm->xm_mode & periph->periph_cap;
211 		if (mode & PERIPH_CAP_SYNC) {
212 			period = xm->xm_period;
213 			offset = xm->xm_offset;
214 		} else {
215 			period = 0;
216 			offset = 0;
217 		}
218 
219 		/*
220 		 * If we do not have a valid xfer mode yet, or the parameters
221 		 * are different, announce them.
222 		 */
223 		if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
224 		    periph->periph_mode != mode ||
225 		    periph->periph_period != period ||
226 		    periph->periph_offset != offset)
227 			announce = 1;
228 
229 		periph->periph_mode = mode;
230 		periph->periph_period = period;
231 		periph->periph_offset = offset;
232 		periph->periph_flags |= PERIPH_MODE_VALID;
233 
234 		if (announce)
235 			scsi_print_xfer_mode(periph);
236 	}
237 }
238 
239 /*
240  * scsipi_async_event_xfer_mode:
241  *
242  *	Update the xfer mode for all SAS/FC periphs sharing the
243  *	specified I_T Nexus.
244  */
245 void
scsi_fc_sas_async_event_xfer_mode(struct scsipi_channel * chan,void * arg)246 scsi_fc_sas_async_event_xfer_mode(struct scsipi_channel *chan, void *arg)
247 {
248 	struct scsipi_xfer_mode *xm = arg;
249 	struct scsipi_periph *periph;
250 	int lun, announce, mode;
251 
252 	for (lun = 0; lun < chan->chan_nluns; lun++) {
253 		periph = scsipi_lookup_periph_locked(chan, xm->xm_target, lun);
254 		if (periph == NULL)
255 			continue;
256 		announce = 0;
257 
258 		/*
259 		 * Clamp the xfer mode down to this periph's capabilities.
260 		 */
261 		mode = xm->xm_mode & periph->periph_cap;
262 		/*
263 		 * If we do not have a valid xfer mode yet, or the parameters
264 		 * are different, announce them.
265 		 */
266 		if ((periph->periph_flags & PERIPH_MODE_VALID) == 0 ||
267 		    periph->periph_mode != mode)
268 			announce = 1;
269 
270 		periph->periph_mode = mode;
271 		periph->periph_flags |= PERIPH_MODE_VALID;
272 
273 		if (announce &&
274 		    (periph->periph_mode & PERIPH_CAP_TQING) != 0) {
275 			aprint_normal_dev(periph->periph_dev,
276 			    "tagged queueing\n");
277 		}
278 	}
279 }
280