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