atapi_base.c revision 1.14 1 1.14 thorpej /* $NetBSD: atapi_base.c,v 1.14 1999/09/30 22:57:52 thorpej Exp $ */
2 1.2 bouyer
3 1.9 mycroft /*-
4 1.9 mycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.9 mycroft * All rights reserved.
6 1.9 mycroft *
7 1.9 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.9 mycroft * by Charles M. Hannum.
9 1.2 bouyer *
10 1.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.2 bouyer * modification, are permitted provided that the following conditions
12 1.2 bouyer * are met:
13 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.2 bouyer * must display the following acknowledgement:
20 1.9 mycroft * This product includes software developed by the NetBSD
21 1.9 mycroft * Foundation, Inc. and its contributors.
22 1.9 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.9 mycroft * contributors may be used to endorse or promote products derived
24 1.9 mycroft * from this software without specific prior written permission.
25 1.2 bouyer *
26 1.9 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.9 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.9 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.9 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.9 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.9 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.9 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.9 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.9 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.9 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.9 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.2 bouyer */
38 1.2 bouyer
39 1.2 bouyer #include <sys/types.h>
40 1.2 bouyer #include <sys/param.h>
41 1.2 bouyer #include <sys/systm.h>
42 1.2 bouyer #include <sys/kernel.h>
43 1.2 bouyer #include <sys/buf.h>
44 1.2 bouyer #include <sys/uio.h>
45 1.2 bouyer #include <sys/malloc.h>
46 1.2 bouyer #include <sys/errno.h>
47 1.2 bouyer #include <sys/device.h>
48 1.2 bouyer #include <sys/proc.h>
49 1.2 bouyer
50 1.2 bouyer #include <dev/scsipi/scsipi_all.h>
51 1.5 cgd #include <dev/scsipi/atapi_all.h>
52 1.5 cgd #include <dev/scsipi/scsipiconf.h>
53 1.2 bouyer #include <dev/scsipi/atapiconf.h>
54 1.2 bouyer #include <dev/scsipi/scsipi_base.h>
55 1.2 bouyer
56 1.2 bouyer /*
57 1.2 bouyer * Look at the returned sense and act on the error, determining
58 1.2 bouyer * the unix error number to pass back. (0 = report no error)
59 1.2 bouyer *
60 1.2 bouyer * THIS IS THE DEFAULT ERROR HANDLER
61 1.2 bouyer */
62 1.3 enami int
63 1.2 bouyer atapi_interpret_sense(xs)
64 1.2 bouyer struct scsipi_xfer *xs;
65 1.2 bouyer {
66 1.2 bouyer int key, error;
67 1.2 bouyer struct scsipi_link *sc_link = xs->sc_link;
68 1.2 bouyer char *msg = NULL;
69 1.2 bouyer
70 1.2 bouyer /*
71 1.2 bouyer * If the device has it's own error handler, call it first.
72 1.2 bouyer * If it returns a legit error value, return that, otherwise
73 1.2 bouyer * it wants us to continue with normal error processing.
74 1.2 bouyer */
75 1.2 bouyer if (sc_link->device->err_handler) {
76 1.2 bouyer SC_DEBUG(sc_link, SDEV_DB2,
77 1.2 bouyer ("calling private err_handler()\n"));
78 1.2 bouyer error = (*sc_link->device->err_handler) (xs);
79 1.8 mjacob if (error != SCSIRET_CONTINUE)
80 1.3 enami return (error); /* error >= 0 better ? */
81 1.2 bouyer }
82 1.11 bouyer /*
83 1.11 bouyer * otherwise use the default, call the generic sense handler if we have
84 1.11 bouyer * more than the sense key
85 1.11 bouyer */
86 1.11 bouyer if (xs->error == XS_SENSE)
87 1.11 bouyer return (scsipi_interpret_sense(xs));
88 1.11 bouyer
89 1.11 bouyer key = (xs->sense.atapi_sense & 0xf0) >> 4;
90 1.2 bouyer switch (key) {
91 1.7 mjacob case SKEY_RECOVERED_ERROR:
92 1.2 bouyer msg = "soft error (corrected)";
93 1.6 mjacob case SKEY_NO_SENSE:
94 1.2 bouyer if (xs->resid == xs->datalen)
95 1.2 bouyer xs->resid = 0; /* not short read */
96 1.6 mjacob error = 0;
97 1.6 mjacob break;
98 1.6 mjacob case SKEY_NOT_READY:
99 1.2 bouyer if ((sc_link->flags & SDEV_REMOVABLE) != 0)
100 1.2 bouyer sc_link->flags &= ~SDEV_MEDIA_LOADED;
101 1.14 thorpej if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
102 1.3 enami return (0);
103 1.14 thorpej if ((xs->xs_control & XS_CTL_SILENT) != 0)
104 1.3 enami return (EIO);
105 1.2 bouyer msg = "not ready";
106 1.2 bouyer error = EIO;
107 1.2 bouyer break;
108 1.6 mjacob case SKEY_MEDIUM_ERROR: /* MEDIUM ERROR */
109 1.2 bouyer msg = "medium error";
110 1.2 bouyer error = EIO;
111 1.2 bouyer break;
112 1.6 mjacob case SKEY_HARDWARE_ERROR:
113 1.2 bouyer msg = "non-media hardware failure";
114 1.2 bouyer error = EIO;
115 1.2 bouyer break;
116 1.6 mjacob case SKEY_ILLEGAL_REQUEST:
117 1.14 thorpej if ((xs->xs_control &
118 1.14 thorpej XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
119 1.3 enami return (0);
120 1.14 thorpej if ((xs->xs_control & XS_CTL_SILENT) != 0)
121 1.3 enami return (EIO);
122 1.2 bouyer msg = "illegal request";
123 1.2 bouyer error = EINVAL;
124 1.2 bouyer break;
125 1.6 mjacob case SKEY_UNIT_ATTENTION:
126 1.2 bouyer if ((sc_link->flags & SDEV_REMOVABLE) != 0)
127 1.2 bouyer sc_link->flags &= ~SDEV_MEDIA_LOADED;
128 1.14 thorpej if ((xs->xs_control &
129 1.14 thorpej XS_CTL_IGNORE_MEDIA_CHANGE) != 0 ||
130 1.2 bouyer /* XXX Should reupload any transient state. */
131 1.2 bouyer (sc_link->flags & SDEV_REMOVABLE) == 0)
132 1.3 enami return (ERESTART);
133 1.14 thorpej if ((xs->xs_control & XS_CTL_SILENT) != 0)
134 1.3 enami return (EIO);
135 1.2 bouyer msg = "unit attention";
136 1.2 bouyer error = EIO;
137 1.2 bouyer break;
138 1.6 mjacob case SKEY_WRITE_PROTECT:
139 1.2 bouyer msg = "readonly device";
140 1.4 is error = EROFS;
141 1.2 bouyer break;
142 1.6 mjacob case SKEY_ABORTED_COMMAND:
143 1.2 bouyer msg = "command aborted";
144 1.2 bouyer error = ERESTART;
145 1.2 bouyer break;
146 1.2 bouyer default:
147 1.2 bouyer error = EIO;
148 1.2 bouyer break;
149 1.2 bouyer }
150 1.2 bouyer
151 1.2 bouyer if (!key) {
152 1.2 bouyer if (xs->sense.atapi_sense & 0x01) {
153 1.2 bouyer /* Illegal length indication */
154 1.2 bouyer msg = "ATA illegal length indication";
155 1.2 bouyer error = EIO;
156 1.2 bouyer }
157 1.2 bouyer if (xs->sense.atapi_sense & 0x02) { /* vol overflow */
158 1.2 bouyer msg = "ATA volume overflow";
159 1.2 bouyer error = ENOSPC;
160 1.2 bouyer }
161 1.2 bouyer if (xs->sense.atapi_sense & 0x04) { /* Aborted command */
162 1.2 bouyer msg = "ATA command aborted";
163 1.2 bouyer error = ERESTART;
164 1.2 bouyer }
165 1.2 bouyer }
166 1.2 bouyer if (msg) {
167 1.2 bouyer sc_link->sc_print_addr(sc_link);
168 1.2 bouyer printf("%s\n", msg);
169 1.2 bouyer } else {
170 1.2 bouyer if (error) {
171 1.2 bouyer sc_link->sc_print_addr(sc_link);
172 1.2 bouyer printf("unknown error code %d\n",
173 1.2 bouyer xs->sense.atapi_sense);
174 1.2 bouyer }
175 1.2 bouyer }
176 1.2 bouyer
177 1.3 enami return (error);
178 1.2 bouyer
179 1.2 bouyer }
180 1.2 bouyer
181 1.2 bouyer /*
182 1.2 bouyer * Utility routines often used in SCSI stuff
183 1.2 bouyer */
184 1.2 bouyer
185 1.2 bouyer
186 1.2 bouyer /*
187 1.2 bouyer * Print out the scsi_link structure's address info.
188 1.2 bouyer */
189 1.2 bouyer void
190 1.2 bouyer atapi_print_addr(sc_link)
191 1.2 bouyer struct scsipi_link *sc_link;
192 1.2 bouyer {
193 1.2 bouyer
194 1.10 bouyer printf("%s(%s:%d:%d): ",
195 1.2 bouyer sc_link->device_softc ?
196 1.2 bouyer ((struct device *)sc_link->device_softc)->dv_xname : "probe",
197 1.2 bouyer ((struct device *)sc_link->adapter_softc)->dv_xname,
198 1.10 bouyer sc_link->scsipi_atapi.channel, sc_link->scsipi_atapi.drive);
199 1.2 bouyer }
200 1.2 bouyer
201 1.2 bouyer /*
202 1.2 bouyer * ask the atapi driver to perform a command for us.
203 1.2 bouyer * tell it where to read/write the data, and how
204 1.2 bouyer * long the data is supposed to be. If we have a buf
205 1.2 bouyer * to associate with the transfer, we need that too.
206 1.2 bouyer */
207 1.3 enami int
208 1.2 bouyer atapi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
209 1.2 bouyer retries, timeout, bp, flags)
210 1.2 bouyer struct scsipi_link *sc_link;
211 1.2 bouyer struct scsipi_generic *scsipi_cmd;
212 1.2 bouyer int cmdlen;
213 1.2 bouyer u_char *data_addr;
214 1.2 bouyer int datalen;
215 1.2 bouyer int retries;
216 1.2 bouyer int timeout;
217 1.2 bouyer struct buf *bp;
218 1.2 bouyer int flags;
219 1.2 bouyer {
220 1.2 bouyer struct scsipi_xfer *xs;
221 1.12 thorpej int error, s;
222 1.2 bouyer
223 1.2 bouyer SC_DEBUG(sc_link, SDEV_DB2, ("atapi_cmd\n"));
224 1.2 bouyer
225 1.2 bouyer #ifdef DIAGNOSTIC
226 1.14 thorpej if (bp != NULL && (flags & XS_CTL_ASYNC) == 0)
227 1.14 thorpej panic("atapi_scsipi_cmd: buffer without async");
228 1.2 bouyer #endif
229 1.2 bouyer
230 1.2 bouyer if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr,
231 1.13 thorpej datalen, retries, timeout, bp, flags)) == NULL) {
232 1.13 thorpej if (bp != NULL) {
233 1.13 thorpej s = splbio();
234 1.13 thorpej bp->b_flags |= B_ERROR;
235 1.13 thorpej bp->b_error = ENOMEM;
236 1.13 thorpej biodone(bp);
237 1.13 thorpej splx(s);
238 1.13 thorpej }
239 1.3 enami return (ENOMEM);
240 1.13 thorpej }
241 1.2 bouyer
242 1.2 bouyer xs->cmdlen = (sc_link->scsipi_atapi.cap & ACAP_LEN) ? 16 : 12;
243 1.2 bouyer
244 1.2 bouyer if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
245 1.3 enami return (0);
246 1.2 bouyer
247 1.2 bouyer /*
248 1.2 bouyer * we have finished with the xfer stuct, free it and
249 1.2 bouyer * check if anyone else needs to be started up.
250 1.2 bouyer */
251 1.12 thorpej s = splbio();
252 1.2 bouyer scsipi_free_xs(xs, flags);
253 1.12 thorpej splx(s);
254 1.5 cgd return (error);
255 1.5 cgd }
256 1.5 cgd
257 1.5 cgd int
258 1.5 cgd atapi_mode_select(l, data, len, flags, retries, timeout)
259 1.5 cgd struct scsipi_link *l;
260 1.5 cgd struct atapi_mode_header *data;
261 1.5 cgd int len, flags, retries, timeout;
262 1.5 cgd {
263 1.5 cgd struct atapi_mode_select scsipi_cmd;
264 1.5 cgd int error;
265 1.5 cgd
266 1.5 cgd bzero(&scsipi_cmd, sizeof(scsipi_cmd));
267 1.5 cgd scsipi_cmd.opcode = ATAPI_MODE_SELECT;
268 1.5 cgd scsipi_cmd.byte2 = AMS_PF;
269 1.5 cgd _lto2b(len, scsipi_cmd.length);
270 1.5 cgd
271 1.5 cgd /* length is reserved when doing mode select; zero it */
272 1.5 cgd _lto2l(0, data->length);
273 1.5 cgd
274 1.5 cgd error = scsipi_command(l, (struct scsipi_generic *)&scsipi_cmd,
275 1.5 cgd sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL,
276 1.14 thorpej flags | XS_CTL_DATA_OUT);
277 1.5 cgd SC_DEBUG(l, SDEV_DB2, ("atapi_mode_select: error=%d\n", error));
278 1.5 cgd return (error);
279 1.5 cgd }
280 1.5 cgd
281 1.5 cgd int
282 1.5 cgd atapi_mode_sense(l, page, data, len, flags, retries, timeout)
283 1.5 cgd struct scsipi_link *l;
284 1.5 cgd int page, len, flags, retries, timeout;
285 1.5 cgd struct atapi_mode_header *data;
286 1.5 cgd {
287 1.5 cgd struct atapi_mode_sense scsipi_cmd;
288 1.5 cgd int error;
289 1.5 cgd
290 1.5 cgd bzero(&scsipi_cmd, sizeof(scsipi_cmd));
291 1.5 cgd scsipi_cmd.opcode = ATAPI_MODE_SENSE;
292 1.5 cgd scsipi_cmd.page = page;
293 1.5 cgd _lto2b(len, scsipi_cmd.length);
294 1.5 cgd
295 1.5 cgd error = scsipi_command(l, (struct scsipi_generic *)&scsipi_cmd,
296 1.5 cgd sizeof(scsipi_cmd), (void *)data, len, retries, timeout, NULL,
297 1.14 thorpej flags | XS_CTL_DATA_IN);
298 1.5 cgd SC_DEBUG(l, SDEV_DB2, ("atapi_mode_sense: error=%d\n", error));
299 1.3 enami return (error);
300 1.2 bouyer }
301