1 /* $NetBSD: atapi_base.c,v 1.30 2019/02/03 03:19:28 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 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; 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: atapi_base.c,v 1.30 2019/02/03 03:19:28 mrg Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/buf.h>
40 #include <sys/uio.h>
41 #include <sys/malloc.h>
42 #include <sys/errno.h>
43 #include <sys/device.h>
44 #include <sys/proc.h>
45
46 #include <dev/scsipi/scsipi_all.h>
47 #include <dev/scsipi/scsipiconf.h>
48 #include <dev/scsipi/atapiconf.h>
49 #include <dev/scsipi/scsipi_base.h>
50
51 /*
52 * Look at the returned sense and act on the error, determining
53 * the unix error number to pass back. (0 = report no error)
54 *
55 * THIS IS THE DEFAULT ERROR HANDLER
56 */
57 int
atapi_interpret_sense(struct scsipi_xfer * xs)58 atapi_interpret_sense(struct scsipi_xfer *xs)
59 {
60 struct scsipi_periph *periph = xs->xs_periph;
61 int key, error;
62 const char *msg = NULL;
63
64 /*
65 * If the device has its own error handler, call it first.
66 * If it returns a legit error value, return that, otherwise
67 * it wants us to continue with normal error processing.
68 */
69 if (periph->periph_switch->psw_error != NULL) {
70 SC_DEBUG(periph, SCSIPI_DB2,
71 ("calling private err_handler()\n"));
72 error = (*periph->periph_switch->psw_error)(xs);
73 if (error != EJUSTRETURN)
74 return (error);
75 }
76 /*
77 * otherwise use the default, call the generic sense handler if we have
78 * more than the sense key
79 */
80 if (xs->error == XS_SENSE)
81 return (scsipi_interpret_sense(xs));
82
83 key = (xs->sense.atapi_sense & 0xf0) >> 4;
84 switch (key) {
85 case SKEY_RECOVERED_ERROR:
86 msg = "soft error (corrected)";
87 /* FALLTHROUGH */
88 case SKEY_NO_SENSE:
89 if (xs->resid == xs->datalen)
90 xs->resid = 0; /* not short read */
91 error = 0;
92 break;
93 case SKEY_NOT_READY:
94 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
95 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
96 if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
97 return (0);
98 if ((xs->xs_control & XS_CTL_SILENT) != 0)
99 return (EIO);
100 msg = "not ready";
101 error = EIO;
102 break;
103 case SKEY_MEDIUM_ERROR: /* MEDIUM ERROR */
104 msg = "medium error";
105 error = EIO;
106 break;
107 case SKEY_HARDWARE_ERROR:
108 msg = "non-media hardware failure";
109 error = EIO;
110 break;
111 case SKEY_ILLEGAL_REQUEST:
112 if ((xs->xs_control &
113 XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
114 return (0);
115 if ((xs->xs_control & XS_CTL_SILENT) != 0)
116 return (EIO);
117 msg = "illegal request";
118 error = EINVAL;
119 break;
120 case SKEY_UNIT_ATTENTION:
121 if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
122 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
123 if ((xs->xs_control &
124 XS_CTL_IGNORE_MEDIA_CHANGE) != 0 ||
125 /* XXX Should reupload any transient state. */
126 (periph->periph_flags & PERIPH_REMOVABLE) == 0)
127 return (ERESTART);
128 if ((xs->xs_control & XS_CTL_SILENT) != 0)
129 return (EIO);
130 msg = "unit attention";
131 error = EIO;
132 break;
133 case SKEY_DATA_PROTECT:
134 msg = "readonly device";
135 error = EROFS;
136 break;
137 case SKEY_ABORTED_COMMAND:
138 msg = "command aborted";
139 if (xs->xs_retries != 0) {
140 xs->xs_retries--;
141 error = ERESTART;
142 } else
143 error = EIO;
144 break;
145 default:
146 error = EIO;
147 break;
148 }
149
150 if (!key) {
151 if (xs->sense.atapi_sense & 0x01) {
152 /* Illegal length indication */
153 msg = "ATA illegal length indication";
154 error = EIO;
155 }
156 if (xs->sense.atapi_sense & 0x02) { /* vol overflow */
157 msg = "ATA volume overflow";
158 error = ENOSPC;
159 }
160 if (xs->sense.atapi_sense & 0x04) { /* Aborted command */
161 msg = "ATA command aborted";
162 if (xs->xs_retries != 0) {
163 xs->xs_retries--;
164 error = ERESTART;
165 } else
166 error = EIO;
167 }
168 }
169 if (msg) {
170 scsipi_printaddr(periph);
171 printf("%s\n", msg);
172 } else {
173 if (error) {
174 scsipi_printaddr(periph);
175 printf("unknown error code %d\n",
176 xs->sense.atapi_sense);
177 }
178 }
179
180 return (error);
181 }
182
183 /*
184 * Utility routines often used in SCSI stuff
185 */
186
187
188 /*
189 * Print out the scsi_link structure's address info.
190 */
191 void
atapi_print_addr(struct scsipi_periph * periph)192 atapi_print_addr(struct scsipi_periph *periph)
193 {
194 struct scsipi_channel *chan = periph->periph_channel;
195 struct scsipi_adapter *adapt = chan->chan_adapter;
196
197 printf("%s(%s:%d:%d): ", periph->periph_dev != NULL ?
198 device_xname(periph->periph_dev) : "probe",
199 device_xname(adapt->adapt_dev),
200 chan->chan_channel, periph->periph_target);
201 }
202
203 /*
204 * ask the atapi driver to perform a command for us.
205 * tell it where to read/write the data, and how
206 * long the data is supposed to be. If we have a buf
207 * to associate with the transfer, we need that too.
208 */
209 void
atapi_scsipi_cmd(struct scsipi_xfer * xs)210 atapi_scsipi_cmd(struct scsipi_xfer *xs)
211 {
212 struct scsipi_periph *periph = xs->xs_periph;
213
214 SC_DEBUG(periph, SCSIPI_DB2, ("atapi_cmd\n"));
215
216 xs->cmdlen = (periph->periph_cap & PERIPH_CAP_CMD16) ? 16 : 12;
217 }
218