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