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