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