scsi_base.c revision 1.42 1 /* $NetBSD: scsi_base.c,v 1.42 1997/03/20 07:13:07 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
274 bzero(&scsi_cmd, sizeof(scsi_cmd));
275 scsi_cmd.opcode = INQUIRY;
276 scsi_cmd.length = sizeof(struct scsi_inquiry_data);
277
278 return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
279 sizeof(scsi_cmd), (u_char *) inqbuf,
280 sizeof(struct scsi_inquiry_data), 2, 10000, NULL,
281 SCSI_DATA_IN | flags);
282 }
283
284 /*
285 * Prevent or allow the user to remove the media
286 */
287 int
288 scsi_prevent(sc_link, type, flags)
289 struct scsi_link *sc_link;
290 int type, flags;
291 {
292 struct scsi_prevent scsi_cmd;
293
294 bzero(&scsi_cmd, sizeof(scsi_cmd));
295 scsi_cmd.opcode = PREVENT_ALLOW;
296 scsi_cmd.how = type;
297 return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
298 sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
299 }
300
301 /*
302 * Get scsi driver to send a "start up" command
303 */
304 int
305 scsi_start(sc_link, type, flags)
306 struct scsi_link *sc_link;
307 int type, flags;
308 {
309 struct scsi_start_stop scsi_cmd;
310
311 bzero(&scsi_cmd, sizeof(scsi_cmd));
312 scsi_cmd.opcode = START_STOP;
313 scsi_cmd.byte2 = 0x00;
314 scsi_cmd.how = type;
315 return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
316 sizeof(scsi_cmd), 0, 0, 2,
317 type == SSS_START ? 30000 : 10000, NULL, flags);
318 }
319
320 /*
321 * This routine is called by the scsi interrupt when the transfer is complete.
322 */
323 void
324 scsi_done(xs)
325 struct scsi_xfer *xs;
326 {
327 struct scsi_link *sc_link = xs->sc_link;
328 int error;
329
330 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
331 #ifdef SCSIDEBUG
332 if ((sc_link->flags & SDEV_DB1) != 0)
333 show_scsi_cmd(xs);
334 #endif /* SCSIDEBUG */
335
336 /*
337 * If it's a user level request, bypass all usual completion processing,
338 * let the user work it out.. We take reponsibility for freeing the
339 * xs when the user returns. (and restarting the device's queue).
340 */
341 if ((xs->flags & SCSI_USER) != 0) {
342 SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
343 scsi_user_done(xs); /* to take a copy of the sense etc. */
344 SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n "));
345
346 scsi_free_xs(xs, SCSI_NOSLEEP); /* restarts queue too */
347 SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
348 return;
349 }
350
351 /*
352 * If the device has it's own done routine, call it first.
353 * If it returns a legit error value, return that, otherwise
354 * it wants us to continue with normal processing.
355 *
356 * Make sure the upper-level driver knows that this might not
357 * actually be the last time they hear from us. We need to get
358 * status back.
359 */
360 if (sc_link->device->done) {
361 SC_DEBUG(sc_link, SDEV_DB2, ("calling private done()\n"));
362 error = (*sc_link->device->done)(xs, 0);
363 if (error == EJUSTRETURN)
364 goto done;
365 SC_DEBUG(sc_link, SDEV_DB3, ("continuing with generic done()\n"));
366 }
367 if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
368 /*
369 * if it's a normal upper level request, then ask
370 * the upper level code to handle error checking
371 * rather than doing it here at interrupt time
372 */
373 wakeup(xs);
374 return;
375 }
376 /*
377 * Go and handle errors now.
378 * If it returns ERESTART then we should RETRY
379 */
380 retry:
381 if (sc_err1(xs, 1) == ERESTART) {
382 switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
383 case SUCCESSFULLY_QUEUED:
384 return;
385
386 case TRY_AGAIN_LATER:
387 xs->error = XS_BUSY;
388 case COMPLETE:
389 goto retry;
390 }
391 }
392 done:
393 if (sc_link->device->done) {
394 /*
395 * Tell the device the operation is actually complete.
396 * No more will happen with this xfer. This for
397 * notification of the upper-level driver only; they
398 * won't be returning any meaningful information to us.
399 */
400 (void)(*sc_link->device->done)(xs, 1);
401 }
402 scsi_free_xs(xs, SCSI_NOSLEEP);
403 }
404
405 int
406 scsi_execute_xs(xs)
407 struct scsi_xfer *xs;
408 {
409 int error;
410 int s;
411
412 xs->flags &= ~ITSDONE;
413 xs->error = XS_NOERROR;
414 xs->resid = xs->datalen;
415
416 retry:
417 /*
418 * Do the transfer. If we are polling we will return:
419 * COMPLETE, Was poll, and scsi_done has been called
420 * TRY_AGAIN_LATER, Adapter short resources, try again
421 *
422 * if under full steam (interrupts) it will return:
423 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
424 * TRY_AGAIN_LATER, (as for polling)
425 * After the wakeup, we must still check if it succeeded
426 *
427 * If we have a SCSI_NOSLEEP (typically because we have a buf)
428 * we just return. All the error proccessing and the buffer
429 * code both expect us to return straight to them, so as soon
430 * as the command is queued, return.
431 */
432 switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
433 case SUCCESSFULLY_QUEUED:
434 if ((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
435 return EJUSTRETURN;
436 #ifdef DIAGNOSTIC
437 if (xs->flags & SCSI_NOSLEEP)
438 panic("scsi_execute_xs: NOSLEEP and POLL");
439 #endif
440 s = splbio();
441 while ((xs->flags & ITSDONE) == 0)
442 tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
443 splx(s);
444 case COMPLETE: /* Polling command completed ok */
445 if (xs->bp)
446 return EJUSTRETURN;
447 doit:
448 SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
449 if ((error = sc_err1(xs, 0)) != ERESTART)
450 return error;
451 goto retry;
452
453 case TRY_AGAIN_LATER: /* adapter resource shortage */
454 xs->error = XS_BUSY;
455 goto doit;
456
457 default:
458 panic("scsi_execute_xs: invalid return code");
459 }
460
461 #ifdef DIAGNOSTIC
462 panic("scsi_execute_xs: impossible");
463 #endif
464 return EINVAL;
465 }
466
467 /*
468 * ask the scsi driver to perform a command for us.
469 * tell it where to read/write the data, and how
470 * long the data is supposed to be. If we have a buf
471 * to associate with the transfer, we need that too.
472 */
473 int
474 scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
475 retries, timeout, bp, flags)
476 struct scsi_link *sc_link;
477 struct scsi_generic *scsi_cmd;
478 int cmdlen;
479 u_char *data_addr;
480 int datalen;
481 int retries;
482 int timeout;
483 struct buf *bp;
484 int flags;
485 {
486 struct scsi_xfer *xs;
487 int error;
488
489 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
490
491 #ifdef DIAGNOSTIC
492 if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
493 panic("scsi_scsi_cmd: buffer without nosleep");
494 #endif
495
496 if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
497 retries, timeout, bp, flags)) == NULL)
498 return ENOMEM;
499
500 if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
501 return 0;
502
503 /*
504 * we have finished with the xfer stuct, free it and
505 * check if anyone else needs to be started up.
506 */
507 scsi_free_xs(xs, flags);
508 return error;
509 }
510
511 int
512 sc_err1(xs, async)
513 struct scsi_xfer *xs;
514 int async;
515 {
516 int error;
517
518 SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
519
520 /*
521 * If it has a buf, we might be working with
522 * a request from the buffer cache or some other
523 * piece of code that requires us to process
524 * errors at inetrrupt time. We have probably
525 * been called by scsi_done()
526 */
527 switch (xs->error) {
528 case XS_NOERROR: /* nearly always hit this one */
529 error = 0;
530 break;
531
532 case XS_SENSE:
533 if ((error = scsi_interpret_sense(xs)) == ERESTART)
534 goto retry;
535 SC_DEBUG(xs->sc_link, SDEV_DB3,
536 ("scsi_interpret_sense returned %d\n", error));
537 break;
538
539 case XS_BUSY:
540 if (xs->retries) {
541 if ((xs->flags & SCSI_POLL) != 0)
542 delay(1000000);
543 else if ((xs->flags & SCSI_NOSLEEP) == 0)
544 tsleep(&lbolt, PRIBIO, "scbusy", 0);
545 else
546 #if 0
547 timeout(scsi_requeue, xs, hz);
548 #else
549 goto lose;
550 #endif
551 }
552 case XS_TIMEOUT:
553 retry:
554 if (xs->retries--) {
555 xs->error = XS_NOERROR;
556 xs->flags &= ~ITSDONE;
557 return ERESTART;
558 }
559 case XS_DRIVER_STUFFUP:
560 lose:
561 error = EIO;
562 break;
563
564 case XS_SELTIMEOUT:
565 /* XXX Disable device? */
566 error = EIO;
567 break;
568
569 default:
570 sc_print_addr(xs->sc_link);
571 printf("unknown error category from scsi driver\n");
572 error = EIO;
573 break;
574 }
575
576 scsi_error(xs, error);
577 return error;
578 }
579
580 void
581 scsi_error(xs, error)
582 struct scsi_xfer *xs;
583 int error;
584 {
585 struct buf *bp = xs->bp;
586
587 if (bp) {
588 if (error) {
589 bp->b_error = error;
590 bp->b_flags |= B_ERROR;
591 bp->b_resid = bp->b_bcount;
592 } else {
593 bp->b_error = 0;
594 bp->b_resid = xs->resid;
595 }
596 biodone(bp);
597 }
598 }
599
600 /*
601 * Look at the returned sense and act on the error, determining
602 * the unix error number to pass back. (0 = report no error)
603 *
604 * THIS IS THE DEFAULT ERROR HANDLER
605 */
606 int
607 scsi_interpret_sense(xs)
608 struct scsi_xfer *xs;
609 {
610 struct scsi_sense_data *sense;
611 struct scsi_link *sc_link = xs->sc_link;
612 u_int8_t key;
613 u_int32_t info;
614 int error;
615
616 static char *error_mes[] = {
617 "soft error (corrected)",
618 "not ready", "medium error",
619 "non-media hardware failure", "illegal request",
620 "unit attention", "readonly device",
621 "no data found", "vendor unique",
622 "copy aborted", "command aborted",
623 "search returned equal", "volume overflow",
624 "verify miscompare", "unknown error key"
625 };
626
627 sense = &xs->sense;
628 #ifdef SCSIDEBUG
629 if ((sc_link->flags & SDEV_DB1) != 0) {
630 int count;
631 printf("code%x valid%x ",
632 sense->error_code & SSD_ERRCODE,
633 sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
634 printf("seg%x key%x ili%x eom%x fmark%x\n",
635 sense->segment,
636 sense->flags & SSD_KEY,
637 sense->flags & SSD_ILI ? 1 : 0,
638 sense->flags & SSD_EOM ? 1 : 0,
639 sense->flags & SSD_FILEMARK ? 1 : 0);
640 printf("info: %x %x %x %x followed by %d extra bytes\n",
641 sense->info[0],
642 sense->info[1],
643 sense->info[2],
644 sense->info[3],
645 sense->extra_len);
646 printf("extra: ");
647 for (count = 0; count < sense->extra_len; count++)
648 printf("%x ", sense->extra_bytes[count]);
649 printf("\n");
650 }
651 #endif /*SCSIDEBUG */
652 /*
653 * If the device has it's own error handler, call it first.
654 * If it returns a legit error value, return that, otherwise
655 * it wants us to continue with normal error processing.
656 */
657 if (sc_link->device->err_handler) {
658 SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
659 error = (*sc_link->device->err_handler) (xs);
660 if (error != -1)
661 return error; /* error >= 0 better ? */
662 }
663 /* otherwise use the default */
664 switch (sense->error_code & SSD_ERRCODE) {
665 /*
666 * If it's code 70, use the extended stuff and interpret the key
667 */
668 case 0x71: /* delayed error */
669 sc_print_addr(sc_link);
670 key = sense->flags & SSD_KEY;
671 printf(" DELAYED ERROR, key = 0x%x\n", key);
672 case 0x70:
673 if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
674 info = _4btol(sense->info);
675 else
676 info = 0;
677 key = sense->flags & SSD_KEY;
678
679 switch (key) {
680 case 0x0: /* NO SENSE */
681 case 0x1: /* RECOVERED ERROR */
682 if (xs->resid == xs->datalen)
683 xs->resid = 0; /* not short read */
684 case 0xc: /* EQUAL */
685 error = 0;
686 break;
687 case 0x2: /* NOT READY */
688 if ((sc_link->flags & SDEV_REMOVABLE) != 0)
689 sc_link->flags &= ~SDEV_MEDIA_LOADED;
690 if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
691 return 0;
692 if ((xs->flags & SCSI_SILENT) != 0)
693 return EIO;
694 error = EIO;
695 break;
696 case 0x5: /* ILLEGAL REQUEST */
697 if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
698 return 0;
699 if ((xs->flags & SCSI_SILENT) != 0)
700 return EIO;
701 error = EINVAL;
702 break;
703 case 0x6: /* UNIT ATTENTION */
704 if ((sc_link->flags & SDEV_REMOVABLE) != 0)
705 sc_link->flags &= ~SDEV_MEDIA_LOADED;
706 if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
707 /* XXX Should reupload any transient state. */
708 (sc_link->flags & SDEV_REMOVABLE) == 0)
709 return ERESTART;
710 if ((xs->flags & SCSI_SILENT) != 0)
711 return EIO;
712 error = EIO;
713 break;
714 case 0x7: /* DATA PROTECT */
715 error = EACCES;
716 break;
717 case 0x8: /* BLANK CHECK */
718 error = 0;
719 break;
720 case 0xb: /* COMMAND ABORTED */
721 error = ERESTART;
722 break;
723 case 0xd: /* VOLUME OVERFLOW */
724 error = ENOSPC;
725 break;
726 default:
727 error = EIO;
728 break;
729 }
730
731 if (key) {
732 sc_print_addr(sc_link);
733 printf("%s", error_mes[key - 1]);
734 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
735 switch (key) {
736 case 0x2: /* NOT READY */
737 case 0x5: /* ILLEGAL REQUEST */
738 case 0x6: /* UNIT ATTENTION */
739 case 0x7: /* DATA PROTECT */
740 break;
741 case 0x8: /* BLANK CHECK */
742 printf(", requested size: %d (decimal)",
743 info);
744 break;
745 case 0xb:
746 if (xs->retries)
747 printf(", retrying");
748 printf(", cmd 0x%x, info 0x%x",
749 xs->cmd->opcode, info);
750 break;
751 default:
752 printf(", info = %d (decimal)", info);
753 }
754 }
755 if (sense->extra_len != 0) {
756 int n;
757 printf(", data =");
758 for (n = 0; n < sense->extra_len; n++)
759 printf(" %02x", sense->extra_bytes[n]);
760 }
761 printf("\n");
762 }
763 return error;
764
765 /*
766 * Not code 70, just report it
767 */
768 default:
769 sc_print_addr(sc_link);
770 printf("error code %d",
771 sense->error_code & SSD_ERRCODE);
772 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
773 struct scsi_sense_data_unextended *usense =
774 (struct scsi_sense_data_unextended *)sense;
775 printf(" at block no. %d (decimal)",
776 _3btol(usense->block));
777 }
778 printf("\n");
779 return EIO;
780 }
781 }
782
783 /*
784 * Utility routines often used in SCSI stuff
785 */
786
787
788 /*
789 * Print out the scsi_link structure's address info.
790 */
791 void
792 sc_print_addr(sc_link)
793 struct scsi_link *sc_link;
794 {
795
796 printf("%s(%s:%d:%d): ",
797 sc_link->device_softc ?
798 ((struct device *)sc_link->device_softc)->dv_xname : "probe",
799 ((struct device *)sc_link->adapter_softc)->dv_xname,
800 sc_link->target, sc_link->lun);
801 }
802
803 #ifdef SCSIDEBUG
804 /*
805 * Given a scsi_xfer, dump the request, in all it's glory
806 */
807 void
808 show_scsi_xs(xs)
809 struct scsi_xfer *xs;
810 {
811 printf("xs(%p): ", xs);
812 printf("flg(0x%x)", xs->flags);
813 printf("sc_link(%p)", xs->sc_link);
814 printf("retr(0x%x)", xs->retries);
815 printf("timo(0x%x)", xs->timeout);
816 printf("cmd(%p)", xs->cmd);
817 printf("len(0x%x)", xs->cmdlen);
818 printf("data(%p)", xs->data);
819 printf("len(0x%x)", xs->datalen);
820 printf("res(0x%x)", xs->resid);
821 printf("err(0x%x)", xs->error);
822 printf("bp(%p)", xs->bp);
823 show_scsi_cmd(xs);
824 }
825
826 void
827 show_scsi_cmd(xs)
828 struct scsi_xfer *xs;
829 {
830 u_char *b = (u_char *) xs->cmd;
831 int i = 0;
832
833 sc_print_addr(xs->sc_link);
834 printf("command: ");
835
836 if ((xs->flags & SCSI_RESET) == 0) {
837 while (i < xs->cmdlen) {
838 if (i)
839 printf(",");
840 printf("%x", b[i++]);
841 }
842 printf("-[%d bytes]\n", xs->datalen);
843 if (xs->datalen)
844 show_mem(xs->data, min(64, xs->datalen));
845 } else
846 printf("-RESET-\n");
847 }
848
849 void
850 show_mem(address, num)
851 u_char *address;
852 int num;
853 {
854 int x;
855
856 printf("------------------------------");
857 for (x = 0; x < num; x++) {
858 if ((x % 16) == 0)
859 printf("\n%03d: ", x);
860 printf("%02x ", *address++);
861 }
862 printf("\n------------------------------\n");
863 }
864 #endif /*SCSIDEBUG */
865