scsi_base.c revision 1.40 1 /* $NetBSD: scsi_base.c,v 1.40 1996/10/12 23:23:15 christos 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->bp == NULL) {
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 bp however, all the error proccessing
428 * and the buffer code both expect us to return straight
429 * to them, so as soon as the command is queued, return
430 */
431 switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
432 case SUCCESSFULLY_QUEUED:
433 if (xs->bp)
434 return EJUSTRETURN;
435 s = splbio();
436 while ((xs->flags & ITSDONE) == 0)
437 tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
438 splx(s);
439 case COMPLETE: /* Polling command completed ok */
440 if (xs->bp)
441 return EJUSTRETURN;
442 doit:
443 SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
444 if ((error = sc_err1(xs, 0)) != ERESTART)
445 return error;
446 goto retry;
447
448 case TRY_AGAIN_LATER: /* adapter resource shortage */
449 xs->error = XS_BUSY;
450 goto doit;
451
452 default:
453 panic("scsi_execute_xs: invalid return code");
454 }
455
456 #ifdef DIAGNOSTIC
457 panic("scsi_execute_xs: impossible");
458 #endif
459 return EINVAL;
460 }
461
462 /*
463 * ask the scsi driver to perform a command for us.
464 * tell it where to read/write the data, and how
465 * long the data is supposed to be. If we have a buf
466 * to associate with the transfer, we need that too.
467 */
468 int
469 scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
470 retries, timeout, bp, flags)
471 struct scsi_link *sc_link;
472 struct scsi_generic *scsi_cmd;
473 int cmdlen;
474 u_char *data_addr;
475 int datalen;
476 int retries;
477 int timeout;
478 struct buf *bp;
479 int flags;
480 {
481 struct scsi_xfer *xs;
482 int error;
483
484 SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
485
486 #ifdef DIAGNOSTIC
487 if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
488 panic("scsi_scsi_cmd: buffer without nosleep");
489 #endif
490
491 if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
492 retries, timeout, bp, flags)) == NULL)
493 return ENOMEM;
494
495 if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
496 return 0;
497
498 /*
499 * we have finished with the xfer stuct, free it and
500 * check if anyone else needs to be started up.
501 */
502 scsi_free_xs(xs, flags);
503 return error;
504 }
505
506 int
507 sc_err1(xs, async)
508 struct scsi_xfer *xs;
509 int async;
510 {
511 int error;
512
513 SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x \n", xs->error));
514
515 /*
516 * If it has a buf, we might be working with
517 * a request from the buffer cache or some other
518 * piece of code that requires us to process
519 * errors at inetrrupt time. We have probably
520 * been called by scsi_done()
521 */
522 switch (xs->error) {
523 case XS_NOERROR: /* nearly always hit this one */
524 error = 0;
525 break;
526
527 case XS_SENSE:
528 if ((error = scsi_interpret_sense(xs)) == ERESTART)
529 goto retry;
530 SC_DEBUG(xs->sc_link, SDEV_DB3,
531 ("scsi_interpret_sense returned %d\n", error));
532 break;
533
534 case XS_BUSY:
535 if (xs->retries) {
536 if ((xs->flags & SCSI_POLL) != 0)
537 delay(1000000);
538 else if ((xs->flags & SCSI_NOSLEEP) == 0)
539 tsleep(&lbolt, PRIBIO, "scbusy", 0);
540 else
541 #if 0
542 timeout(scsi_requeue, xs, hz);
543 #else
544 goto lose;
545 #endif
546 }
547 case XS_TIMEOUT:
548 retry:
549 if (xs->retries--) {
550 xs->error = XS_NOERROR;
551 xs->flags &= ~ITSDONE;
552 return ERESTART;
553 }
554 case XS_DRIVER_STUFFUP:
555 lose:
556 error = EIO;
557 break;
558
559 case XS_SELTIMEOUT:
560 /* XXX Disable device? */
561 error = EIO;
562 break;
563
564 default:
565 sc_print_addr(xs->sc_link);
566 printf("unknown error category from scsi driver\n");
567 error = EIO;
568 break;
569 }
570
571 scsi_error(xs, error);
572 return error;
573 }
574
575 void
576 scsi_error(xs, error)
577 struct scsi_xfer *xs;
578 int error;
579 {
580 struct buf *bp = xs->bp;
581
582 if (bp) {
583 if (error) {
584 bp->b_error = error;
585 bp->b_flags |= B_ERROR;
586 bp->b_resid = bp->b_bcount;
587 } else {
588 bp->b_error = 0;
589 bp->b_resid = xs->resid;
590 }
591 biodone(bp);
592 }
593 }
594
595 /*
596 * Look at the returned sense and act on the error, determining
597 * the unix error number to pass back. (0 = report no error)
598 *
599 * THIS IS THE DEFAULT ERROR HANDLER
600 */
601 int
602 scsi_interpret_sense(xs)
603 struct scsi_xfer *xs;
604 {
605 struct scsi_sense_data *sense;
606 struct scsi_link *sc_link = xs->sc_link;
607 u_int8_t key;
608 u_int32_t info;
609 int error;
610
611 static char *error_mes[] = {
612 "soft error (corrected)",
613 "not ready", "medium error",
614 "non-media hardware failure", "illegal request",
615 "unit attention", "readonly device",
616 "no data found", "vendor unique",
617 "copy aborted", "command aborted",
618 "search returned equal", "volume overflow",
619 "verify miscompare", "unknown error key"
620 };
621
622 sense = &xs->sense;
623 #ifdef SCSIDEBUG
624 if ((sc_link->flags & SDEV_DB1) != 0) {
625 int count;
626 printf("code%x valid%x ",
627 sense->error_code & SSD_ERRCODE,
628 sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
629 printf("seg%x key%x ili%x eom%x fmark%x\n",
630 sense->segment,
631 sense->flags & SSD_KEY,
632 sense->flags & SSD_ILI ? 1 : 0,
633 sense->flags & SSD_EOM ? 1 : 0,
634 sense->flags & SSD_FILEMARK ? 1 : 0);
635 printf("info: %x %x %x %x followed by %d extra bytes\n",
636 sense->info[0],
637 sense->info[1],
638 sense->info[2],
639 sense->info[3],
640 sense->extra_len);
641 printf("extra: ");
642 for (count = 0; count < sense->extra_len; count++)
643 printf("%x ", sense->extra_bytes[count]);
644 printf("\n");
645 }
646 #endif /*SCSIDEBUG */
647 /*
648 * If the device has it's own error handler, call it first.
649 * If it returns a legit error value, return that, otherwise
650 * it wants us to continue with normal error processing.
651 */
652 if (sc_link->device->err_handler) {
653 SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
654 error = (*sc_link->device->err_handler) (xs);
655 if (error != -1)
656 return error; /* error >= 0 better ? */
657 }
658 /* otherwise use the default */
659 switch (sense->error_code & SSD_ERRCODE) {
660 /*
661 * If it's code 70, use the extended stuff and interpret the key
662 */
663 case 0x71: /* delayed error */
664 sc_print_addr(sc_link);
665 key = sense->flags & SSD_KEY;
666 printf(" DELAYED ERROR, key = 0x%x\n", key);
667 case 0x70:
668 if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
669 info = _4btol(sense->info);
670 else
671 info = 0;
672 key = sense->flags & SSD_KEY;
673
674 switch (key) {
675 case 0x0: /* NO SENSE */
676 case 0x1: /* RECOVERED ERROR */
677 if (xs->resid == xs->datalen)
678 xs->resid = 0; /* not short read */
679 case 0xc: /* EQUAL */
680 error = 0;
681 break;
682 case 0x2: /* NOT READY */
683 if ((sc_link->flags & SDEV_REMOVABLE) != 0)
684 sc_link->flags &= ~SDEV_MEDIA_LOADED;
685 if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
686 return 0;
687 if ((xs->flags & SCSI_SILENT) != 0)
688 return EIO;
689 error = EIO;
690 break;
691 case 0x5: /* ILLEGAL REQUEST */
692 if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
693 return 0;
694 if ((xs->flags & SCSI_SILENT) != 0)
695 return EIO;
696 error = EINVAL;
697 break;
698 case 0x6: /* UNIT ATTENTION */
699 if ((sc_link->flags & SDEV_REMOVABLE) != 0)
700 sc_link->flags &= ~SDEV_MEDIA_LOADED;
701 if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
702 /* XXX Should reupload any transient state. */
703 (sc_link->flags & SDEV_REMOVABLE) == 0)
704 return ERESTART;
705 if ((xs->flags & SCSI_SILENT) != 0)
706 return EIO;
707 error = EIO;
708 break;
709 case 0x7: /* DATA PROTECT */
710 error = EACCES;
711 break;
712 case 0x8: /* BLANK CHECK */
713 error = 0;
714 break;
715 case 0xb: /* COMMAND ABORTED */
716 error = ERESTART;
717 break;
718 case 0xd: /* VOLUME OVERFLOW */
719 error = ENOSPC;
720 break;
721 default:
722 error = EIO;
723 break;
724 }
725
726 if (key) {
727 sc_print_addr(sc_link);
728 printf("%s", error_mes[key - 1]);
729 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
730 switch (key) {
731 case 0x2: /* NOT READY */
732 case 0x5: /* ILLEGAL REQUEST */
733 case 0x6: /* UNIT ATTENTION */
734 case 0x7: /* DATA PROTECT */
735 break;
736 case 0x8: /* BLANK CHECK */
737 printf(", requested size: %d (decimal)",
738 info);
739 break;
740 case 0xb:
741 if (xs->retries)
742 printf(", retrying");
743 printf(", cmd 0x%x, info 0x%x",
744 xs->cmd->opcode, info);
745 break;
746 default:
747 printf(", info = %d (decimal)", info);
748 }
749 }
750 if (sense->extra_len != 0) {
751 int n;
752 printf(", data =");
753 for (n = 0; n < sense->extra_len; n++)
754 printf(" %02x", sense->extra_bytes[n]);
755 }
756 printf("\n");
757 }
758 return error;
759
760 /*
761 * Not code 70, just report it
762 */
763 default:
764 sc_print_addr(sc_link);
765 printf("error code %d",
766 sense->error_code & SSD_ERRCODE);
767 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
768 struct scsi_sense_data_unextended *usense =
769 (struct scsi_sense_data_unextended *)sense;
770 printf(" at block no. %d (decimal)",
771 _3btol(usense->block));
772 }
773 printf("\n");
774 return EIO;
775 }
776 }
777
778 /*
779 * Utility routines often used in SCSI stuff
780 */
781
782
783 /*
784 * Print out the scsi_link structure's address info.
785 */
786 void
787 sc_print_addr(sc_link)
788 struct scsi_link *sc_link;
789 {
790
791 printf("%s(%s:%d:%d): ",
792 sc_link->device_softc ?
793 ((struct device *)sc_link->device_softc)->dv_xname : "probe",
794 ((struct device *)sc_link->adapter_softc)->dv_xname,
795 sc_link->target, sc_link->lun);
796 }
797
798 #ifdef SCSIDEBUG
799 /*
800 * Given a scsi_xfer, dump the request, in all it's glory
801 */
802 void
803 show_scsi_xs(xs)
804 struct scsi_xfer *xs;
805 {
806 printf("xs(%p): ", xs);
807 printf("flg(0x%x)", xs->flags);
808 printf("sc_link(%p)", xs->sc_link);
809 printf("retr(0x%x)", xs->retries);
810 printf("timo(0x%x)", xs->timeout);
811 printf("cmd(%p)", xs->cmd);
812 printf("len(0x%x)", xs->cmdlen);
813 printf("data(%p)", xs->data);
814 printf("len(0x%x)", xs->datalen);
815 printf("res(0x%x)", xs->resid);
816 printf("err(0x%x)", xs->error);
817 printf("bp(%p)", xs->bp);
818 show_scsi_cmd(xs);
819 }
820
821 void
822 show_scsi_cmd(xs)
823 struct scsi_xfer *xs;
824 {
825 u_char *b = (u_char *) xs->cmd;
826 int i = 0;
827
828 sc_print_addr(xs->sc_link);
829 printf("command: ");
830
831 if ((xs->flags & SCSI_RESET) == 0) {
832 while (i < xs->cmdlen) {
833 if (i)
834 printf(",");
835 printf("%x", b[i++]);
836 }
837 printf("-[%d bytes]\n", xs->datalen);
838 if (xs->datalen)
839 show_mem(xs->data, min(64, xs->datalen));
840 } else
841 printf("-RESET-\n");
842 }
843
844 void
845 show_mem(address, num)
846 u_char *address;
847 int num;
848 {
849 int x;
850
851 printf("------------------------------");
852 for (x = 0; x < num; x++) {
853 if ((x % 16) == 0)
854 printf("\n%03d: ", x);
855 printf("%02x ", *address++);
856 }
857 printf("\n------------------------------\n");
858 }
859 #endif /*SCSIDEBUG */
860