atapi_base.c revision 1.16 1 /* $NetBSD: atapi_base.c,v 1.16 2001/05/14 20:35:27 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 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; by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/buf.h>
45 #include <sys/uio.h>
46 #include <sys/malloc.h>
47 #include <sys/errno.h>
48 #include <sys/device.h>
49 #include <sys/proc.h>
50
51 #include <dev/scsipi/scsipi_all.h>
52 #include <dev/scsipi/scsipiconf.h>
53 #include <dev/scsipi/atapiconf.h>
54 #include <dev/scsipi/scsipi_base.h>
55
56 /*
57 * Look at the returned sense and act on the error, determining
58 * the unix error number to pass back. (0 = report no error)
59 *
60 * THIS IS THE DEFAULT ERROR HANDLER
61 */
62 int
63 atapi_interpret_sense(xs)
64 struct scsipi_xfer *xs;
65 {
66 struct scsipi_periph *periph = xs->xs_periph;
67 int key, error;
68 char *msg = NULL;
69
70 /*
71 * If the device has it's own error handler, call it first.
72 * If it returns a legit error value, return that, otherwise
73 * it wants us to continue with normal error processing.
74 */
75 if (periph->periph_switch->psw_error != NULL) {
76 SC_DEBUG(periph, SCSIPI_DB2,
77 ("calling private err_handler()\n"));
78 error = (*periph->periph_switch->psw_error)(xs);
79 if (error != EJUSTRETURN)
80 return (error);
81 }
82 /*
83 * otherwise use the default, call the generic sense handler if we have
84 * more than the sense key
85 */
86 if (xs->error == XS_SENSE)
87 return (scsipi_interpret_sense(xs));
88
89 key = (xs->sense.atapi_sense & 0xf0) >> 4;
90 switch (key) {
91 case SKEY_RECOVERED_ERROR:
92 msg = "soft error (corrected)";
93 case SKEY_NO_SENSE:
94 if (xs->resid == xs->datalen)
95 xs->resid = 0; /* not short read */
96 error = 0;
97 break;
98 case SKEY_NOT_READY:
99 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
100 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
101 if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
102 return (0);
103 if ((xs->xs_control & XS_CTL_SILENT) != 0)
104 return (EIO);
105 msg = "not ready";
106 error = EIO;
107 break;
108 case SKEY_MEDIUM_ERROR: /* MEDIUM ERROR */
109 msg = "medium error";
110 error = EIO;
111 break;
112 case SKEY_HARDWARE_ERROR:
113 msg = "non-media hardware failure";
114 error = EIO;
115 break;
116 case SKEY_ILLEGAL_REQUEST:
117 if ((xs->xs_control &
118 XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
119 return (0);
120 if ((xs->xs_control & XS_CTL_SILENT) != 0)
121 return (EIO);
122 msg = "illegal request";
123 error = EINVAL;
124 break;
125 case SKEY_UNIT_ATTENTION:
126 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
127 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
128 if ((xs->xs_control &
129 XS_CTL_IGNORE_MEDIA_CHANGE) != 0 ||
130 /* XXX Should reupload any transient state. */
131 (periph->periph_flags & PERIPH_REMOVABLE) == 0)
132 return (ERESTART);
133 if ((xs->xs_control & XS_CTL_SILENT) != 0)
134 return (EIO);
135 msg = "unit attention";
136 error = EIO;
137 break;
138 case SKEY_WRITE_PROTECT:
139 msg = "readonly device";
140 error = EROFS;
141 break;
142 case SKEY_ABORTED_COMMAND:
143 msg = "command aborted";
144 error = ERESTART;
145 break;
146 default:
147 error = EIO;
148 break;
149 }
150
151 if (!key) {
152 if (xs->sense.atapi_sense & 0x01) {
153 /* Illegal length indication */
154 msg = "ATA illegal length indication";
155 error = EIO;
156 }
157 if (xs->sense.atapi_sense & 0x02) { /* vol overflow */
158 msg = "ATA volume overflow";
159 error = ENOSPC;
160 }
161 if (xs->sense.atapi_sense & 0x04) { /* Aborted command */
162 msg = "ATA command aborted";
163 error = ERESTART;
164 }
165 }
166 if (msg) {
167 scsipi_printaddr(periph);
168 printf("%s\n", msg);
169 } else {
170 if (error) {
171 scsipi_printaddr(periph);
172 printf("unknown error code %d\n",
173 xs->sense.atapi_sense);
174 }
175 }
176
177 return (error);
178 }
179
180 /*
181 * Utility routines often used in SCSI stuff
182 */
183
184
185 /*
186 * Print out the scsi_link structure's address info.
187 */
188 void
189 atapi_print_addr(periph)
190 struct scsipi_periph *periph;
191 {
192 struct scsipi_channel *chan = periph->periph_channel;
193 struct scsipi_adapter *adapt = chan->chan_adapter;
194
195 printf("%s(%s:%d:%d): ", periph->periph_dev != NULL ?
196 periph->periph_dev->dv_xname : "probe",
197 adapt->adapt_dev->dv_xname,
198 chan->chan_channel, periph->periph_target);
199 }
200
201 /*
202 * ask the atapi driver to perform a command for us.
203 * tell it where to read/write the data, and how
204 * long the data is supposed to be. If we have a buf
205 * to associate with the transfer, we need that too.
206 */
207 int
208 atapi_scsipi_cmd(periph, scsipi_cmd, cmdlen, data, datalen,
209 retries, timeout, bp, flags)
210 struct scsipi_periph *periph;
211 struct scsipi_generic *scsipi_cmd;
212 int cmdlen;
213 void *data;
214 size_t datalen;
215 int retries;
216 int timeout;
217 struct buf *bp;
218 int flags;
219 {
220 struct scsipi_xfer *xs;
221 int error, s;
222
223 SC_DEBUG(periph, SCSIPI_DB2, ("atapi_cmd\n"));
224
225 #ifdef DIAGNOSTIC
226 if (bp != NULL && (flags & XS_CTL_ASYNC) == 0)
227 panic("atapi_scsipi_cmd: buffer without async");
228 #endif
229
230 if ((xs = scsipi_make_xs(periph, scsipi_cmd, cmdlen, data,
231 datalen, retries, timeout, bp, flags)) == NULL) {
232 if (bp != NULL) {
233 s = splbio();
234 bp->b_flags |= B_ERROR;
235 bp->b_error = ENOMEM;
236 biodone(bp);
237 splx(s);
238 }
239 return (ENOMEM);
240 }
241
242 xs->cmdlen = (periph->periph_cap & PERIPH_CAP_CMD16) ? 16 : 12;
243
244 if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
245 return (0);
246 return (error);
247 }
248