scsi_base.c revision 1.38 1 1.38 thorpej /* $NetBSD: scsi_base.c,v 1.38 1996/09/03 18:20:33 thorpej Exp $ */
2 1.14 cgd
3 1.1 mycroft /*
4 1.28 mycroft * Copyright (c) 1994, 1995 Charles Hannum. All rights reserved.
5 1.9 mycroft *
6 1.9 mycroft * Redistribution and use in source and binary forms, with or without
7 1.9 mycroft * modification, are permitted provided that the following conditions
8 1.9 mycroft * are met:
9 1.9 mycroft * 1. Redistributions of source code must retain the above copyright
10 1.9 mycroft * notice, this list of conditions and the following disclaimer.
11 1.9 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.9 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.9 mycroft * documentation and/or other materials provided with the distribution.
14 1.9 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.9 mycroft * must display the following acknowledgement:
16 1.9 mycroft * This product includes software developed by Charles Hannum.
17 1.9 mycroft * 4. The name of the author may not be used to endorse or promote products
18 1.9 mycroft * derived from this software without specific prior written permission.
19 1.9 mycroft *
20 1.9 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.9 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.9 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.9 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.9 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.9 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.9 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.9 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.9 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.9 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.9 mycroft */
31 1.9 mycroft
32 1.9 mycroft /*
33 1.9 mycroft * Originally written by Julian Elischer (julian (at) dialix.oz.au)
34 1.1 mycroft */
35 1.1 mycroft
36 1.1 mycroft #include <sys/types.h>
37 1.1 mycroft #include <sys/param.h>
38 1.30 thorpej #include <sys/systm.h>
39 1.11 mycroft #include <sys/kernel.h>
40 1.1 mycroft #include <sys/buf.h>
41 1.1 mycroft #include <sys/uio.h>
42 1.1 mycroft #include <sys/malloc.h>
43 1.1 mycroft #include <sys/errno.h>
44 1.1 mycroft #include <sys/device.h>
45 1.33 christos #include <sys/proc.h>
46 1.2 mycroft
47 1.1 mycroft #include <scsi/scsi_all.h>
48 1.1 mycroft #include <scsi/scsi_disk.h>
49 1.1 mycroft #include <scsi/scsiconf.h>
50 1.1 mycroft
51 1.22 mycroft void scsi_error __P((struct scsi_xfer *, int));
52 1.22 mycroft
53 1.12 mycroft LIST_HEAD(xs_free_list, scsi_xfer) xs_free_list;
54 1.1 mycroft
55 1.33 christos static __inline struct scsi_xfer *scsi_make_xs __P((struct scsi_link *,
56 1.33 christos struct scsi_generic *,
57 1.33 christos int cmdlen,
58 1.33 christos u_char *data_addr,
59 1.33 christos int datalen,
60 1.33 christos int retries,
61 1.33 christos int timeout,
62 1.33 christos struct buf *,
63 1.33 christos int flags));
64 1.33 christos
65 1.33 christos int sc_err1 __P((struct scsi_xfer *, int));
66 1.33 christos int scsi_interpret_sense __P((struct scsi_xfer *));
67 1.33 christos
68 1.1 mycroft /*
69 1.1 mycroft * Get a scsi transfer structure for the caller. Charge the structure
70 1.1 mycroft * to the device that is referenced by the sc_link structure. If the
71 1.1 mycroft * sc_link structure has no 'credits' then the device already has the
72 1.1 mycroft * maximum number or outstanding operations under way. In this stage,
73 1.1 mycroft * wait on the structure so that when one is freed, we are awoken again
74 1.1 mycroft * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
75 1.1 mycroft * a NULL pointer, signifying that no slots were available
76 1.1 mycroft * Note in the link structure, that we are waiting on it.
77 1.1 mycroft */
78 1.1 mycroft
79 1.1 mycroft struct scsi_xfer *
80 1.22 mycroft scsi_get_xs(sc_link, flags)
81 1.2 mycroft struct scsi_link *sc_link; /* who to charge the xs to */
82 1.11 mycroft int flags; /* if this call can sleep */
83 1.1 mycroft {
84 1.2 mycroft struct scsi_xfer *xs;
85 1.11 mycroft int s;
86 1.1 mycroft
87 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("scsi_get_xs\n"));
88 1.1 mycroft s = splbio();
89 1.22 mycroft while (sc_link->openings <= 0) {
90 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
91 1.22 mycroft if ((flags & SCSI_NOSLEEP) != 0) {
92 1.1 mycroft splx(s);
93 1.1 mycroft return 0;
94 1.1 mycroft }
95 1.1 mycroft sc_link->flags |= SDEV_WAITING;
96 1.12 mycroft (void) tsleep(sc_link, PRIBIO, "getxs", 0);
97 1.1 mycroft }
98 1.22 mycroft sc_link->openings--;
99 1.33 christos if ((xs = xs_free_list.lh_first) != NULL) {
100 1.12 mycroft LIST_REMOVE(xs, free_list);
101 1.1 mycroft splx(s);
102 1.1 mycroft } else {
103 1.1 mycroft splx(s);
104 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("making\n"));
105 1.12 mycroft xs = malloc(sizeof(*xs), M_DEVBUF,
106 1.22 mycroft ((flags & SCSI_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
107 1.12 mycroft if (!xs) {
108 1.1 mycroft sc_print_addr(sc_link);
109 1.1 mycroft printf("cannot allocate scsi xs\n");
110 1.12 mycroft return 0;
111 1.1 mycroft }
112 1.1 mycroft }
113 1.22 mycroft
114 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
115 1.22 mycroft xs->flags = INUSE | flags;
116 1.11 mycroft return xs;
117 1.1 mycroft }
118 1.1 mycroft
119 1.1 mycroft /*
120 1.1 mycroft * Given a scsi_xfer struct, and a device (referenced through sc_link)
121 1.1 mycroft * return the struct to the free pool and credit the device with it
122 1.1 mycroft * If another process is waiting for an xs, do a wakeup, let it proceed
123 1.1 mycroft */
124 1.1 mycroft void
125 1.22 mycroft scsi_free_xs(xs, flags)
126 1.1 mycroft struct scsi_xfer *xs;
127 1.11 mycroft int flags;
128 1.1 mycroft {
129 1.22 mycroft struct scsi_link *sc_link = xs->sc_link;
130 1.22 mycroft
131 1.22 mycroft xs->flags &= ~INUSE;
132 1.12 mycroft LIST_INSERT_HEAD(&xs_free_list, xs, free_list);
133 1.1 mycroft
134 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("scsi_free_xs\n"));
135 1.1 mycroft /* if was 0 and someone waits, wake them up */
136 1.22 mycroft sc_link->openings++;
137 1.22 mycroft if ((sc_link->flags & SDEV_WAITING) != 0) {
138 1.3 mycroft sc_link->flags &= ~SDEV_WAITING;
139 1.22 mycroft wakeup(sc_link);
140 1.3 mycroft } else {
141 1.1 mycroft if (sc_link->device->start) {
142 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private start()\n"));
143 1.17 mycroft (*(sc_link->device->start)) (sc_link->device_softc);
144 1.1 mycroft }
145 1.3 mycroft }
146 1.1 mycroft }
147 1.1 mycroft
148 1.1 mycroft /*
149 1.22 mycroft * Make a scsi_xfer, and return a pointer to it.
150 1.22 mycroft */
151 1.22 mycroft static __inline struct scsi_xfer *
152 1.22 mycroft scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
153 1.33 christos retries, timeout, bp, flags)
154 1.22 mycroft struct scsi_link *sc_link;
155 1.22 mycroft struct scsi_generic *scsi_cmd;
156 1.22 mycroft int cmdlen;
157 1.22 mycroft u_char *data_addr;
158 1.22 mycroft int datalen;
159 1.22 mycroft int retries;
160 1.22 mycroft int timeout;
161 1.22 mycroft struct buf *bp;
162 1.22 mycroft int flags;
163 1.22 mycroft {
164 1.22 mycroft struct scsi_xfer *xs;
165 1.22 mycroft
166 1.22 mycroft if ((xs = scsi_get_xs(sc_link, flags)) == NULL)
167 1.22 mycroft return NULL;
168 1.22 mycroft
169 1.22 mycroft /*
170 1.22 mycroft * Fill out the scsi_xfer structure. We don't know whose context
171 1.22 mycroft * the cmd is in, so copy it.
172 1.22 mycroft */
173 1.22 mycroft xs->sc_link = sc_link;
174 1.22 mycroft bcopy(scsi_cmd, &xs->cmdstore, cmdlen);
175 1.22 mycroft xs->cmd = &xs->cmdstore;
176 1.22 mycroft xs->cmdlen = cmdlen;
177 1.22 mycroft xs->data = data_addr;
178 1.22 mycroft xs->datalen = datalen;
179 1.22 mycroft xs->retries = retries;
180 1.22 mycroft xs->timeout = timeout;
181 1.22 mycroft xs->bp = bp;
182 1.38 thorpej
183 1.38 thorpej /*
184 1.38 thorpej * Set the LUN in the CDB if we have an older device. We also
185 1.38 thorpej * set it for more modern SCSI-II devices "just in case".
186 1.38 thorpej */
187 1.38 thorpej if ((sc_link->scsi_version & SID_ANSII) <= 2)
188 1.38 thorpej xs->cmd->bytes[0] |=
189 1.38 thorpej ((sc_link->lun << SCSI_CMD_LUN_SHIFT) & SCSI_CMD_LUN_MASK);
190 1.22 mycroft
191 1.22 mycroft return xs;
192 1.22 mycroft }
193 1.22 mycroft
194 1.22 mycroft /*
195 1.1 mycroft * Find out from the device what its capacity is.
196 1.1 mycroft */
197 1.22 mycroft u_long
198 1.1 mycroft scsi_size(sc_link, flags)
199 1.1 mycroft struct scsi_link *sc_link;
200 1.11 mycroft int flags;
201 1.1 mycroft {
202 1.1 mycroft struct scsi_read_cap_data rdcap;
203 1.1 mycroft struct scsi_read_capacity scsi_cmd;
204 1.1 mycroft
205 1.1 mycroft /*
206 1.1 mycroft * make up a scsi command and ask the scsi driver to do
207 1.1 mycroft * it for you.
208 1.1 mycroft */
209 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
210 1.22 mycroft scsi_cmd.opcode = READ_CAPACITY;
211 1.1 mycroft
212 1.1 mycroft /*
213 1.1 mycroft * If the command works, interpret the result as a 4 byte
214 1.1 mycroft * number of blocks
215 1.1 mycroft */
216 1.22 mycroft if (scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
217 1.22 mycroft sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap),
218 1.2 mycroft 2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
219 1.1 mycroft sc_print_addr(sc_link);
220 1.1 mycroft printf("could not get size\n");
221 1.11 mycroft return 0;
222 1.1 mycroft }
223 1.34 mycroft
224 1.34 mycroft return _4btol(rdcap.addr) + 1;
225 1.1 mycroft }
226 1.1 mycroft
227 1.1 mycroft /*
228 1.1 mycroft * Get scsi driver to send a "are you ready?" command
229 1.1 mycroft */
230 1.2 mycroft int
231 1.1 mycroft scsi_test_unit_ready(sc_link, flags)
232 1.1 mycroft struct scsi_link *sc_link;
233 1.11 mycroft int flags;
234 1.1 mycroft {
235 1.1 mycroft struct scsi_test_unit_ready scsi_cmd;
236 1.1 mycroft
237 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
238 1.22 mycroft scsi_cmd.opcode = TEST_UNIT_READY;
239 1.1 mycroft
240 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
241 1.22 mycroft sizeof(scsi_cmd), 0, 0, 2, 10000, NULL, flags);
242 1.1 mycroft }
243 1.1 mycroft
244 1.1 mycroft /*
245 1.1 mycroft * Do a scsi operation, asking a device to run as SCSI-II if it can.
246 1.1 mycroft */
247 1.2 mycroft int
248 1.1 mycroft scsi_change_def(sc_link, flags)
249 1.1 mycroft struct scsi_link *sc_link;
250 1.11 mycroft int flags;
251 1.1 mycroft {
252 1.1 mycroft struct scsi_changedef scsi_cmd;
253 1.1 mycroft
254 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
255 1.22 mycroft scsi_cmd.opcode = CHANGE_DEFINITION;
256 1.1 mycroft scsi_cmd.how = SC_SCSI_2;
257 1.1 mycroft
258 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
259 1.2 mycroft sizeof(scsi_cmd), 0, 0, 2, 100000, NULL, flags);
260 1.1 mycroft }
261 1.1 mycroft
262 1.1 mycroft /*
263 1.1 mycroft * Do a scsi operation asking a device what it is
264 1.1 mycroft * Use the scsi_cmd routine in the switch table.
265 1.1 mycroft */
266 1.2 mycroft int
267 1.1 mycroft scsi_inquire(sc_link, inqbuf, flags)
268 1.1 mycroft struct scsi_link *sc_link;
269 1.1 mycroft struct scsi_inquiry_data *inqbuf;
270 1.11 mycroft int flags;
271 1.1 mycroft {
272 1.1 mycroft struct scsi_inquiry scsi_cmd;
273 1.1 mycroft
274 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
275 1.22 mycroft scsi_cmd.opcode = INQUIRY;
276 1.1 mycroft scsi_cmd.length = sizeof(struct scsi_inquiry_data);
277 1.1 mycroft
278 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
279 1.2 mycroft sizeof(scsi_cmd), (u_char *) inqbuf,
280 1.22 mycroft sizeof(struct scsi_inquiry_data), 2, 10000, NULL,
281 1.2 mycroft SCSI_DATA_IN | flags);
282 1.1 mycroft }
283 1.1 mycroft
284 1.1 mycroft /*
285 1.1 mycroft * Prevent or allow the user to remove the media
286 1.1 mycroft */
287 1.2 mycroft int
288 1.1 mycroft scsi_prevent(sc_link, type, flags)
289 1.1 mycroft struct scsi_link *sc_link;
290 1.11 mycroft int type, flags;
291 1.1 mycroft {
292 1.1 mycroft struct scsi_prevent scsi_cmd;
293 1.1 mycroft
294 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
295 1.22 mycroft scsi_cmd.opcode = PREVENT_ALLOW;
296 1.1 mycroft scsi_cmd.how = type;
297 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
298 1.2 mycroft sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
299 1.1 mycroft }
300 1.1 mycroft
301 1.1 mycroft /*
302 1.1 mycroft * Get scsi driver to send a "start up" command
303 1.1 mycroft */
304 1.2 mycroft int
305 1.10 mycroft scsi_start(sc_link, type, flags)
306 1.1 mycroft struct scsi_link *sc_link;
307 1.11 mycroft int type, flags;
308 1.1 mycroft {
309 1.1 mycroft struct scsi_start_stop scsi_cmd;
310 1.1 mycroft
311 1.1 mycroft bzero(&scsi_cmd, sizeof(scsi_cmd));
312 1.22 mycroft scsi_cmd.opcode = START_STOP;
313 1.22 mycroft scsi_cmd.byte2 = 0x00;
314 1.10 mycroft scsi_cmd.how = type;
315 1.2 mycroft return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
316 1.10 mycroft sizeof(scsi_cmd), 0, 0, 2,
317 1.22 mycroft type == SSS_START ? 30000 : 10000, NULL, flags);
318 1.1 mycroft }
319 1.1 mycroft
320 1.1 mycroft /*
321 1.1 mycroft * This routine is called by the scsi interrupt when the transfer is complete.
322 1.1 mycroft */
323 1.1 mycroft void
324 1.1 mycroft scsi_done(xs)
325 1.1 mycroft struct scsi_xfer *xs;
326 1.1 mycroft {
327 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
328 1.11 mycroft int error;
329 1.1 mycroft
330 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
331 1.1 mycroft #ifdef SCSIDEBUG
332 1.22 mycroft if ((sc_link->flags & SDEV_DB1) != 0)
333 1.1 mycroft show_scsi_cmd(xs);
334 1.22 mycroft #endif /* SCSIDEBUG */
335 1.22 mycroft
336 1.1 mycroft /*
337 1.1 mycroft * If it's a user level request, bypass all usual completion processing,
338 1.1 mycroft * let the user work it out.. We take reponsibility for freeing the
339 1.1 mycroft * xs when the user returns. (and restarting the device's queue).
340 1.1 mycroft */
341 1.22 mycroft if ((xs->flags & SCSI_USER) != 0) {
342 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
343 1.1 mycroft scsi_user_done(xs); /* to take a copy of the sense etc. */
344 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
345 1.19 mycroft
346 1.22 mycroft scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
347 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
348 1.1 mycroft return;
349 1.1 mycroft }
350 1.22 mycroft
351 1.1 mycroft /*
352 1.1 mycroft * If the device has it's own done routine, call it first.
353 1.1 mycroft * If it returns a legit error value, return that, otherwise
354 1.1 mycroft * it wants us to continue with normal processing.
355 1.31 thorpej *
356 1.31 thorpej * Make sure the upper-level driver knows that this might not
357 1.31 thorpej * actually be the last time they hear from us. We need to get
358 1.31 thorpej * status back.
359 1.1 mycroft */
360 1.1 mycroft if (sc_link->device->done) {
361 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private done()\n"));
362 1.31 thorpej error = (*sc_link->device->done)(xs, 0);
363 1.22 mycroft if (error == EJUSTRETURN)
364 1.22 mycroft goto done;
365 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB3, ("continuing with generic done()\n"));
366 1.1 mycroft }
367 1.22 mycroft if (xs->bp == NULL) {
368 1.1 mycroft /*
369 1.1 mycroft * if it's a normal upper level request, then ask
370 1.1 mycroft * the upper level code to handle error checking
371 1.1 mycroft * rather than doing it here at interrupt time
372 1.1 mycroft */
373 1.1 mycroft wakeup(xs);
374 1.1 mycroft return;
375 1.1 mycroft }
376 1.1 mycroft /*
377 1.1 mycroft * Go and handle errors now.
378 1.22 mycroft * If it returns ERESTART then we should RETRY
379 1.1 mycroft */
380 1.22 mycroft retry:
381 1.22 mycroft if (sc_err1(xs, 1) == ERESTART) {
382 1.22 mycroft switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
383 1.22 mycroft case SUCCESSFULLY_QUEUED:
384 1.1 mycroft return;
385 1.22 mycroft
386 1.22 mycroft case TRY_AGAIN_LATER:
387 1.22 mycroft xs->error = XS_BUSY;
388 1.22 mycroft case COMPLETE:
389 1.22 mycroft goto retry;
390 1.22 mycroft }
391 1.1 mycroft }
392 1.22 mycroft done:
393 1.31 thorpej if (sc_link->device->done) {
394 1.31 thorpej /*
395 1.31 thorpej * Tell the device the operation is actually complete.
396 1.31 thorpej * No more will happen with this xfer. This for
397 1.31 thorpej * notification of the upper-level driver only; they
398 1.31 thorpej * won't be returning any meaningful information to us.
399 1.31 thorpej */
400 1.31 thorpej (void)(*sc_link->device->done)(xs, 1);
401 1.31 thorpej }
402 1.22 mycroft scsi_free_xs(xs, SCSI_NOSLEEP);
403 1.1 mycroft }
404 1.1 mycroft
405 1.22 mycroft int
406 1.22 mycroft scsi_execute_xs(xs)
407 1.22 mycroft struct scsi_xfer *xs;
408 1.1 mycroft {
409 1.11 mycroft int error;
410 1.11 mycroft int s;
411 1.1 mycroft
412 1.22 mycroft xs->flags &= ~ITSDONE;
413 1.22 mycroft xs->error = XS_NOERROR;
414 1.22 mycroft xs->resid = xs->datalen;
415 1.1 mycroft
416 1.1 mycroft retry:
417 1.1 mycroft /*
418 1.1 mycroft * Do the transfer. If we are polling we will return:
419 1.1 mycroft * COMPLETE, Was poll, and scsi_done has been called
420 1.1 mycroft * TRY_AGAIN_LATER, Adapter short resources, try again
421 1.1 mycroft *
422 1.1 mycroft * if under full steam (interrupts) it will return:
423 1.1 mycroft * SUCCESSFULLY_QUEUED, will do a wakeup when complete
424 1.1 mycroft * TRY_AGAIN_LATER, (as for polling)
425 1.1 mycroft * After the wakeup, we must still check if it succeeded
426 1.1 mycroft *
427 1.1 mycroft * If we have a bp however, all the error proccessing
428 1.1 mycroft * and the buffer code both expect us to return straight
429 1.1 mycroft * to them, so as soon as the command is queued, return
430 1.1 mycroft */
431 1.22 mycroft switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
432 1.1 mycroft case SUCCESSFULLY_QUEUED:
433 1.22 mycroft if (xs->bp)
434 1.22 mycroft return EJUSTRETURN;
435 1.1 mycroft s = splbio();
436 1.22 mycroft while ((xs->flags & ITSDONE) == 0)
437 1.4 mycroft tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
438 1.1 mycroft splx(s);
439 1.1 mycroft case COMPLETE: /* Polling command completed ok */
440 1.22 mycroft if (xs->bp)
441 1.22 mycroft return EJUSTRETURN;
442 1.22 mycroft doit:
443 1.24 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
444 1.22 mycroft if ((error = sc_err1(xs, 0)) != ERESTART)
445 1.22 mycroft return error;
446 1.22 mycroft goto retry;
447 1.1 mycroft
448 1.1 mycroft case TRY_AGAIN_LATER: /* adapter resource shortage */
449 1.22 mycroft xs->error = XS_BUSY;
450 1.22 mycroft goto doit;
451 1.22 mycroft
452 1.1 mycroft default:
453 1.22 mycroft panic("scsi_execute_xs: invalid return code");
454 1.1 mycroft }
455 1.22 mycroft
456 1.22 mycroft #ifdef DIAGNOSTIC
457 1.22 mycroft panic("scsi_execute_xs: impossible");
458 1.22 mycroft #endif
459 1.33 christos return EINVAL;
460 1.22 mycroft }
461 1.22 mycroft
462 1.22 mycroft /*
463 1.22 mycroft * ask the scsi driver to perform a command for us.
464 1.22 mycroft * tell it where to read/write the data, and how
465 1.22 mycroft * long the data is supposed to be. If we have a buf
466 1.22 mycroft * to associate with the transfer, we need that too.
467 1.22 mycroft */
468 1.22 mycroft int
469 1.22 mycroft scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
470 1.22 mycroft retries, timeout, bp, flags)
471 1.22 mycroft struct scsi_link *sc_link;
472 1.22 mycroft struct scsi_generic *scsi_cmd;
473 1.22 mycroft int cmdlen;
474 1.22 mycroft u_char *data_addr;
475 1.22 mycroft int datalen;
476 1.22 mycroft int retries;
477 1.22 mycroft int timeout;
478 1.22 mycroft struct buf *bp;
479 1.22 mycroft int flags;
480 1.22 mycroft {
481 1.22 mycroft struct scsi_xfer *xs;
482 1.22 mycroft int error;
483 1.22 mycroft
484 1.22 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
485 1.22 mycroft
486 1.25 mycroft #ifdef DIAGNOSTIC
487 1.25 mycroft if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
488 1.25 mycroft panic("scsi_scsi_cmd: buffer without nosleep");
489 1.25 mycroft #endif
490 1.25 mycroft
491 1.22 mycroft if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
492 1.22 mycroft retries, timeout, bp, flags)) == NULL)
493 1.22 mycroft return ENOMEM;
494 1.22 mycroft
495 1.22 mycroft if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
496 1.22 mycroft return 0;
497 1.22 mycroft
498 1.1 mycroft /*
499 1.1 mycroft * we have finished with the xfer stuct, free it and
500 1.1 mycroft * check if anyone else needs to be started up.
501 1.1 mycroft */
502 1.22 mycroft scsi_free_xs(xs, flags);
503 1.11 mycroft return error;
504 1.1 mycroft }
505 1.1 mycroft
506 1.2 mycroft int
507 1.22 mycroft sc_err1(xs, async)
508 1.1 mycroft struct scsi_xfer *xs;
509 1.22 mycroft int async;
510 1.1 mycroft {
511 1.11 mycroft int error;
512 1.1 mycroft
513 1.1 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
514 1.22 mycroft
515 1.1 mycroft /*
516 1.1 mycroft * If it has a buf, we might be working with
517 1.1 mycroft * a request from the buffer cache or some other
518 1.1 mycroft * piece of code that requires us to process
519 1.1 mycroft * errors at inetrrupt time. We have probably
520 1.1 mycroft * been called by scsi_done()
521 1.1 mycroft */
522 1.1 mycroft switch (xs->error) {
523 1.1 mycroft case XS_NOERROR: /* nearly always hit this one */
524 1.11 mycroft error = 0;
525 1.1 mycroft break;
526 1.1 mycroft
527 1.1 mycroft case XS_SENSE:
528 1.22 mycroft if ((error = scsi_interpret_sense(xs)) == ERESTART)
529 1.22 mycroft goto retry;
530 1.21 mycroft SC_DEBUG(xs->sc_link, SDEV_DB3,
531 1.21 mycroft ("scsi_interpret_sense returned %d\n", error));
532 1.1 mycroft break;
533 1.1 mycroft
534 1.1 mycroft case XS_BUSY:
535 1.22 mycroft if (xs->retries) {
536 1.22 mycroft if ((xs->flags & SCSI_POLL) != 0)
537 1.22 mycroft delay(1000000);
538 1.22 mycroft else if ((xs->flags & SCSI_NOSLEEP) == 0)
539 1.22 mycroft tsleep(&lbolt, PRIBIO, "scbusy", 0);
540 1.22 mycroft else
541 1.22 mycroft #if 0
542 1.22 mycroft timeout(scsi_requeue, xs, hz);
543 1.22 mycroft #else
544 1.22 mycroft goto lose;
545 1.22 mycroft #endif
546 1.22 mycroft }
547 1.1 mycroft case XS_TIMEOUT:
548 1.22 mycroft retry:
549 1.1 mycroft if (xs->retries--) {
550 1.1 mycroft xs->error = XS_NOERROR;
551 1.1 mycroft xs->flags &= ~ITSDONE;
552 1.22 mycroft return ERESTART;
553 1.1 mycroft }
554 1.1 mycroft case XS_DRIVER_STUFFUP:
555 1.22 mycroft lose:
556 1.22 mycroft error = EIO;
557 1.22 mycroft break;
558 1.22 mycroft
559 1.22 mycroft case XS_SELTIMEOUT:
560 1.22 mycroft /* XXX Disable device? */
561 1.11 mycroft error = EIO;
562 1.1 mycroft break;
563 1.21 mycroft
564 1.1 mycroft default:
565 1.1 mycroft sc_print_addr(xs->sc_link);
566 1.1 mycroft printf("unknown error category from scsi driver\n");
567 1.21 mycroft error = EIO;
568 1.21 mycroft break;
569 1.21 mycroft }
570 1.21 mycroft
571 1.22 mycroft scsi_error(xs, error);
572 1.22 mycroft return error;
573 1.22 mycroft }
574 1.22 mycroft
575 1.22 mycroft void
576 1.22 mycroft scsi_error(xs, error)
577 1.22 mycroft struct scsi_xfer *xs;
578 1.22 mycroft int error;
579 1.22 mycroft {
580 1.22 mycroft struct buf *bp = xs->bp;
581 1.22 mycroft
582 1.21 mycroft if (bp) {
583 1.21 mycroft if (error) {
584 1.22 mycroft bp->b_error = error;
585 1.21 mycroft bp->b_flags |= B_ERROR;
586 1.21 mycroft bp->b_resid = bp->b_bcount;
587 1.21 mycroft } else {
588 1.21 mycroft bp->b_error = 0;
589 1.22 mycroft bp->b_resid = xs->resid;
590 1.21 mycroft }
591 1.22 mycroft biodone(bp);
592 1.1 mycroft }
593 1.1 mycroft }
594 1.1 mycroft
595 1.1 mycroft /*
596 1.1 mycroft * Look at the returned sense and act on the error, determining
597 1.1 mycroft * the unix error number to pass back. (0 = report no error)
598 1.1 mycroft *
599 1.1 mycroft * THIS IS THE DEFAULT ERROR HANDLER
600 1.1 mycroft */
601 1.2 mycroft int
602 1.1 mycroft scsi_interpret_sense(xs)
603 1.1 mycroft struct scsi_xfer *xs;
604 1.1 mycroft {
605 1.1 mycroft struct scsi_sense_data *sense;
606 1.1 mycroft struct scsi_link *sc_link = xs->sc_link;
607 1.22 mycroft u_int8_t key;
608 1.22 mycroft u_int32_t info;
609 1.2 mycroft int error;
610 1.1 mycroft
611 1.22 mycroft static char *error_mes[] = {
612 1.22 mycroft "soft error (corrected)",
613 1.22 mycroft "not ready", "medium error",
614 1.22 mycroft "non-media hardware failure", "illegal request",
615 1.22 mycroft "unit attention", "readonly device",
616 1.22 mycroft "no data found", "vendor unique",
617 1.22 mycroft "copy aborted", "command aborted",
618 1.22 mycroft "search returned equal", "volume overflow",
619 1.22 mycroft "verify miscompare", "unknown error key"
620 1.1 mycroft };
621 1.1 mycroft
622 1.11 mycroft sense = &xs->sense;
623 1.1 mycroft #ifdef SCSIDEBUG
624 1.22 mycroft if ((sc_link->flags & SDEV_DB1) != 0) {
625 1.22 mycroft int count;
626 1.1 mycroft printf("code%x valid%x ",
627 1.1 mycroft sense->error_code & SSD_ERRCODE,
628 1.1 mycroft sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
629 1.1 mycroft printf("seg%x key%x ili%x eom%x fmark%x\n",
630 1.34 mycroft sense->segment,
631 1.34 mycroft sense->flags & SSD_KEY,
632 1.34 mycroft sense->flags & SSD_ILI ? 1 : 0,
633 1.34 mycroft sense->flags & SSD_EOM ? 1 : 0,
634 1.34 mycroft sense->flags & SSD_FILEMARK ? 1 : 0);
635 1.1 mycroft printf("info: %x %x %x %x followed by %d extra bytes\n",
636 1.34 mycroft sense->info[0],
637 1.34 mycroft sense->info[1],
638 1.34 mycroft sense->info[2],
639 1.34 mycroft sense->info[3],
640 1.34 mycroft sense->extra_len);
641 1.1 mycroft printf("extra: ");
642 1.34 mycroft for (count = 0; count < sense->extra_len; count++)
643 1.34 mycroft printf("%x ", sense->extra_bytes[count]);
644 1.1 mycroft printf("\n");
645 1.1 mycroft }
646 1.1 mycroft #endif /*SCSIDEBUG */
647 1.1 mycroft /*
648 1.1 mycroft * If the device has it's own error handler, call it first.
649 1.1 mycroft * If it returns a legit error value, return that, otherwise
650 1.1 mycroft * it wants us to continue with normal error processing.
651 1.1 mycroft */
652 1.1 mycroft if (sc_link->device->err_handler) {
653 1.1 mycroft SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
654 1.2 mycroft error = (*sc_link->device->err_handler) (xs);
655 1.2 mycroft if (error != -1)
656 1.2 mycroft return error; /* error >= 0 better ? */
657 1.1 mycroft }
658 1.1 mycroft /* otherwise use the default */
659 1.1 mycroft switch (sense->error_code & SSD_ERRCODE) {
660 1.1 mycroft /*
661 1.1 mycroft * If it's code 70, use the extended stuff and interpret the key
662 1.1 mycroft */
663 1.1 mycroft case 0x71: /* delayed error */
664 1.1 mycroft sc_print_addr(sc_link);
665 1.34 mycroft key = sense->flags & SSD_KEY;
666 1.1 mycroft printf(" DELAYED ERROR, key = 0x%x\n", key);
667 1.1 mycroft case 0x70:
668 1.34 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
669 1.34 mycroft info = _4btol(sense->info);
670 1.34 mycroft else
671 1.1 mycroft info = 0;
672 1.34 mycroft key = sense->flags & SSD_KEY;
673 1.1 mycroft
674 1.22 mycroft switch (key) {
675 1.23 mycroft case 0x0: /* NO SENSE */
676 1.22 mycroft case 0x1: /* RECOVERED ERROR */
677 1.22 mycroft if (xs->resid == xs->datalen)
678 1.22 mycroft xs->resid = 0; /* not short read */
679 1.22 mycroft case 0xc: /* EQUAL */
680 1.22 mycroft error = 0;
681 1.22 mycroft break;
682 1.22 mycroft case 0x2: /* NOT READY */
683 1.22 mycroft if ((sc_link->flags & SDEV_REMOVABLE) != 0)
684 1.22 mycroft sc_link->flags &= ~SDEV_MEDIA_LOADED;
685 1.22 mycroft if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
686 1.22 mycroft return 0;
687 1.22 mycroft if ((xs->flags & SCSI_SILENT) != 0)
688 1.22 mycroft return EIO;
689 1.22 mycroft error = EIO;
690 1.22 mycroft break;
691 1.22 mycroft case 0x5: /* ILLEGAL REQUEST */
692 1.22 mycroft if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
693 1.22 mycroft return 0;
694 1.37 christos if ((xs->flags & SCSI_SILENT) != 0)
695 1.37 christos return EIO;
696 1.22 mycroft error = EINVAL;
697 1.22 mycroft break;
698 1.22 mycroft case 0x6: /* UNIT ATTENTION */
699 1.22 mycroft if ((sc_link->flags & SDEV_REMOVABLE) != 0)
700 1.22 mycroft sc_link->flags &= ~SDEV_MEDIA_LOADED;
701 1.22 mycroft if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
702 1.22 mycroft /* XXX Should reupload any transient state. */
703 1.22 mycroft (sc_link->flags & SDEV_REMOVABLE) == 0)
704 1.22 mycroft return ERESTART;
705 1.22 mycroft if ((xs->flags & SCSI_SILENT) != 0)
706 1.22 mycroft return EIO;
707 1.22 mycroft error = EIO;
708 1.22 mycroft break;
709 1.22 mycroft case 0x7: /* DATA PROTECT */
710 1.22 mycroft error = EACCES;
711 1.22 mycroft break;
712 1.22 mycroft case 0x8: /* BLANK CHECK */
713 1.22 mycroft error = 0;
714 1.22 mycroft break;
715 1.32 briggs case 0xb: /* COMMAND ABORTED */
716 1.32 briggs error = ERESTART;
717 1.32 briggs break;
718 1.22 mycroft case 0xd: /* VOLUME OVERFLOW */
719 1.22 mycroft error = ENOSPC;
720 1.22 mycroft break;
721 1.22 mycroft default:
722 1.22 mycroft error = EIO;
723 1.22 mycroft break;
724 1.22 mycroft }
725 1.22 mycroft
726 1.22 mycroft if (key) {
727 1.1 mycroft sc_print_addr(sc_link);
728 1.1 mycroft printf("%s", error_mes[key - 1]);
729 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
730 1.1 mycroft switch (key) {
731 1.23 mycroft case 0x2: /* NOT READY */
732 1.23 mycroft case 0x5: /* ILLEGAL REQUEST */
733 1.23 mycroft case 0x6: /* UNIT ATTENTION */
734 1.1 mycroft case 0x7: /* DATA PROTECT */
735 1.1 mycroft break;
736 1.1 mycroft case 0x8: /* BLANK CHECK */
737 1.1 mycroft printf(", requested size: %d (decimal)",
738 1.1 mycroft info);
739 1.32 briggs break;
740 1.32 briggs case 0xb:
741 1.32 briggs if (xs->retries)
742 1.32 briggs printf(", retrying");
743 1.32 briggs printf(", cmd 0x%x, info 0x%x",
744 1.32 briggs xs->cmd->opcode, info);
745 1.1 mycroft break;
746 1.1 mycroft default:
747 1.1 mycroft printf(", info = %d (decimal)", info);
748 1.1 mycroft }
749 1.25 mycroft }
750 1.34 mycroft if (sense->extra_len != 0) {
751 1.25 mycroft int n;
752 1.25 mycroft printf(", data =");
753 1.34 mycroft for (n = 0; n < sense->extra_len; n++)
754 1.34 mycroft printf(" %02x", sense->extra_bytes[n]);
755 1.1 mycroft }
756 1.1 mycroft printf("\n");
757 1.1 mycroft }
758 1.22 mycroft return error;
759 1.22 mycroft
760 1.1 mycroft /*
761 1.1 mycroft * Not code 70, just report it
762 1.1 mycroft */
763 1.1 mycroft default:
764 1.22 mycroft sc_print_addr(sc_link);
765 1.22 mycroft printf("error code %d",
766 1.22 mycroft sense->error_code & SSD_ERRCODE);
767 1.22 mycroft if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
768 1.34 mycroft struct scsi_sense_data_unextended *usense =
769 1.34 mycroft (struct scsi_sense_data_unextended *)sense;
770 1.22 mycroft printf(" at block no. %d (decimal)",
771 1.34 mycroft _3btol(usense->block));
772 1.1 mycroft }
773 1.22 mycroft printf("\n");
774 1.11 mycroft return EIO;
775 1.1 mycroft }
776 1.1 mycroft }
777 1.1 mycroft
778 1.1 mycroft /*
779 1.1 mycroft * Utility routines often used in SCSI stuff
780 1.1 mycroft */
781 1.1 mycroft
782 1.1 mycroft
783 1.1 mycroft /*
784 1.1 mycroft * Print out the scsi_link structure's address info.
785 1.1 mycroft */
786 1.1 mycroft void
787 1.1 mycroft sc_print_addr(sc_link)
788 1.22 mycroft struct scsi_link *sc_link;
789 1.1 mycroft {
790 1.1 mycroft
791 1.17 mycroft printf("%s(%s:%d:%d): ",
792 1.22 mycroft sc_link->device_softc ?
793 1.22 mycroft ((struct device *)sc_link->device_softc)->dv_xname : "probe",
794 1.22 mycroft ((struct device *)sc_link->adapter_softc)->dv_xname,
795 1.22 mycroft sc_link->target, sc_link->lun);
796 1.1 mycroft }
797 1.1 mycroft
798 1.1 mycroft #ifdef SCSIDEBUG
799 1.1 mycroft /*
800 1.1 mycroft * Given a scsi_xfer, dump the request, in all it's glory
801 1.1 mycroft */
802 1.1 mycroft void
803 1.1 mycroft show_scsi_xs(xs)
804 1.1 mycroft struct scsi_xfer *xs;
805 1.1 mycroft {
806 1.36 christos printf("xs(%p): ", xs);
807 1.1 mycroft printf("flg(0x%x)", xs->flags);
808 1.36 christos printf("sc_link(%p)", xs->sc_link);
809 1.1 mycroft printf("retr(0x%x)", xs->retries);
810 1.1 mycroft printf("timo(0x%x)", xs->timeout);
811 1.36 christos printf("cmd(%p)", xs->cmd);
812 1.1 mycroft printf("len(0x%x)", xs->cmdlen);
813 1.36 christos printf("data(%p)", xs->data);
814 1.1 mycroft printf("len(0x%x)", xs->datalen);
815 1.1 mycroft printf("res(0x%x)", xs->resid);
816 1.1 mycroft printf("err(0x%x)", xs->error);
817 1.36 christos printf("bp(%p)", xs->bp);
818 1.1 mycroft show_scsi_cmd(xs);
819 1.1 mycroft }
820 1.1 mycroft
821 1.1 mycroft void
822 1.15 deraadt show_scsi_cmd(xs)
823 1.15 deraadt struct scsi_xfer *xs;
824 1.1 mycroft {
825 1.1 mycroft u_char *b = (u_char *) xs->cmd;
826 1.1 mycroft int i = 0;
827 1.1 mycroft
828 1.1 mycroft sc_print_addr(xs->sc_link);
829 1.1 mycroft printf("command: ");
830 1.1 mycroft
831 1.22 mycroft if ((xs->flags & SCSI_RESET) == 0) {
832 1.1 mycroft while (i < xs->cmdlen) {
833 1.1 mycroft if (i)
834 1.1 mycroft printf(",");
835 1.1 mycroft printf("%x", b[i++]);
836 1.1 mycroft }
837 1.1 mycroft printf("-[%d bytes]\n", xs->datalen);
838 1.1 mycroft if (xs->datalen)
839 1.1 mycroft show_mem(xs->data, min(64, xs->datalen));
840 1.2 mycroft } else
841 1.1 mycroft printf("-RESET-\n");
842 1.1 mycroft }
843 1.1 mycroft
844 1.1 mycroft void
845 1.1 mycroft show_mem(address, num)
846 1.24 mycroft u_char *address;
847 1.24 mycroft int num;
848 1.1 mycroft {
849 1.24 mycroft int x;
850 1.24 mycroft
851 1.1 mycroft printf("------------------------------");
852 1.24 mycroft for (x = 0; x < num; x++) {
853 1.24 mycroft if ((x % 16) == 0)
854 1.24 mycroft printf("\n%03d: ", x);
855 1.1 mycroft printf("%02x ", *address++);
856 1.1 mycroft }
857 1.1 mycroft printf("\n------------------------------\n");
858 1.1 mycroft }
859 1.1 mycroft #endif /*SCSIDEBUG */
860