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