1 /*	$OpenBSD: scsi_base.c,v 1.85 2005/06/23 00:31:44 krw Exp $	*/
2 /*	$NetBSD: scsi_base.c,v 1.43 1997/04/02 02:29:36 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Charles M. Hannum.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Originally written by Julian Elischer (julian@dialix.oz.au)
35  * Detailed SCSI error printing Copyright 1997 by Matthew Jacob.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/buf.h>
43 #include <sys/uio.h>
44 #include <sys/malloc.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
47 #include <sys/proc.h>
48 #include <sys/pool.h>
49 
50 #include <scsi/scsi_all.h>
51 #include <scsi/scsi_disk.h>
52 #include <scsi/scsiconf.h>
53 
54 static __inline struct scsi_xfer *scsi_make_xs(struct scsi_link *,
55     struct scsi_generic *, int cmdlen, u_char *data_addr,
56     int datalen, int retries, int timeout, struct buf *, int flags);
57 static __inline void asc2ascii(u_int8_t, u_int8_t ascq, char *result,
58     size_t len);
59 int	sc_err1(struct scsi_xfer *);
60 int	scsi_interpret_sense(struct scsi_xfer *);
61 char   *scsi_decode_sense(struct scsi_sense_data *, int);
62 
63 /* Values for flag parameter to scsi_decode_sense. */
64 #define	DECODE_SENSE_KEY	1
65 #define	DECODE_ASC_ASCQ		2
66 #define DECODE_SKSV		3
67 
68 struct pool scsi_xfer_pool;
69 
70 /*
71  * Called when a scsibus is attached to initialize global data.
72  */
73 void
scsi_init()74 scsi_init()
75 {
76 	static int scsi_init_done;
77 
78 	if (scsi_init_done)
79 		return;
80 	scsi_init_done = 1;
81 
82 	/* Initialize the scsi_xfer pool. */
83 	pool_init(&scsi_xfer_pool, sizeof(struct scsi_xfer), 0,
84 	    0, 0, "scxspl", NULL);
85 }
86 
87 /*
88  * Get a scsi transfer structure for the caller. Charge the structure
89  * to the device that is referenced by the sc_link structure. If the
90  * sc_link structure has no 'credits' then the device already has the
91  * maximum number or outstanding operations under way. In this stage,
92  * wait on the structure so that when one is freed, we are awoken again
93  * If the SCSI_NOSLEEP flag is set, then do not wait, but rather, return
94  * a NULL pointer, signifying that no slots were available
95  * Note in the link structure, that we are waiting on it.
96  */
97 
98 struct scsi_xfer *
scsi_get_xs(sc_link,flags)99 scsi_get_xs(sc_link, flags)
100 	struct scsi_link *sc_link;	/* who to charge the xs to */
101 	int flags;			/* if this call can sleep */
102 {
103 	struct scsi_xfer *xs;
104 	int s;
105 
106 	SC_DEBUG(sc_link, SDEV_DB3, ("scsi_get_xs\n"));
107 
108 	s = splbio();
109 	while (sc_link->openings == 0) {
110 		SC_DEBUG(sc_link, SDEV_DB3, ("sleeping\n"));
111 		if ((flags & SCSI_NOSLEEP) != 0) {
112 			splx(s);
113 			return (NULL);
114 		}
115 		sc_link->flags |= SDEV_WAITING;
116 		(void) tsleep(sc_link, PRIBIO, "getxs", 0);
117 	}
118 	SC_DEBUG(sc_link, SDEV_DB3, ("calling pool_get\n"));
119 	xs = pool_get(&scsi_xfer_pool,
120 	    ((flags & SCSI_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
121 	if (xs != NULL) {
122 		bzero(xs, sizeof *xs);
123 		sc_link->openings--;
124 		xs->flags = flags;
125 	} else {
126 		sc_print_addr(sc_link);
127 		printf("cannot allocate scsi xs\n");
128 	}
129 	splx(s);
130 
131 	SC_DEBUG(sc_link, SDEV_DB3, ("returning\n"));
132 
133 	return (xs);
134 }
135 
136 /*
137  * Given a scsi_xfer struct, and a device (referenced through sc_link)
138  * return the struct to the free pool and credit the device with it
139  * If another process is waiting for an xs, do a wakeup, let it proceed
140  */
141 void
scsi_free_xs(xs)142 scsi_free_xs(xs)
143 	struct scsi_xfer *xs;
144 {
145 	struct scsi_link *sc_link = xs->sc_link;
146 
147 	splassert(IPL_BIO);
148 
149 	SC_DEBUG(sc_link, SDEV_DB3, ("scsi_free_xs\n"));
150 
151 	pool_put(&scsi_xfer_pool, xs);
152 
153 	/* if was 0 and someone waits, wake them up */
154 	sc_link->openings++;
155 	if ((sc_link->flags & SDEV_WAITING) != 0) {
156 		sc_link->flags &= ~SDEV_WAITING;
157 		wakeup(sc_link);
158 	} else {
159 		if (sc_link->device->start) {
160 			SC_DEBUG(sc_link, SDEV_DB2,
161 			    ("calling private start()\n"));
162 			(*(sc_link->device->start)) (sc_link->device_softc);
163 		}
164 	}
165 }
166 
167 /*
168  * Make a scsi_xfer, and return a pointer to it.
169  */
170 static __inline struct scsi_xfer *
scsi_make_xs(sc_link,scsi_cmd,cmdlen,data_addr,datalen,retries,timeout,bp,flags)171 scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
172     retries, timeout, bp, flags)
173 	struct scsi_link *sc_link;
174 	struct scsi_generic *scsi_cmd;
175 	int cmdlen;
176 	u_char *data_addr;
177 	int datalen;
178 	int retries;
179 	int timeout;
180 	struct buf *bp;
181 	int flags;
182 {
183 	struct scsi_xfer *xs;
184 
185 	if ((xs = scsi_get_xs(sc_link, flags)) == NULL)
186 		return NULL;
187 
188 	/*
189 	 * Fill out the scsi_xfer structure.  We don't know whose context
190 	 * the cmd is in, so copy it.
191 	 */
192 	xs->sc_link = sc_link;
193 	bcopy(scsi_cmd, &xs->cmdstore, cmdlen);
194 	xs->cmd = &xs->cmdstore;
195 	xs->cmdlen = cmdlen;
196 	xs->data = data_addr;
197 	xs->datalen = datalen;
198 	xs->retries = retries;
199 	xs->timeout = timeout;
200 	xs->bp = bp;
201 
202 	/*
203 	 * Set the LUN in the CDB.  This may only be needed if we have an
204 	 * older device.  However, we also set it for more modern SCSI
205 	 * devices "just in case".  The old code assumed everything newer
206 	 * than SCSI-2 would not need it, but why risk it?  This was the
207 	 * old conditional:
208 	 *
209 	 * if ((sc_link->scsi_version & SID_ANSII) <= 2)
210 	 */
211 	xs->cmd->bytes[0] &= ~SCSI_CMD_LUN_MASK;
212 	xs->cmd->bytes[0] |=
213 	    ((sc_link->lun << SCSI_CMD_LUN_SHIFT) & SCSI_CMD_LUN_MASK);
214 
215 	return xs;
216 }
217 
218 /*
219  * Find out from the device what its capacity is.
220  */
221 u_long
scsi_size(sc_link,flags,blksize)222 scsi_size(sc_link, flags, blksize)
223 	struct scsi_link *sc_link;
224 	int flags;
225 	u_int32_t *blksize;
226 {
227 	struct scsi_read_capacity scsi_cmd;
228 	struct scsi_read_cap_data rdcap;
229 	u_long max_addr;
230 
231 	/*
232 	 * make up a scsi command and ask the scsi driver to do
233 	 * it for you.
234 	 */
235 	bzero(&scsi_cmd, sizeof(scsi_cmd));
236 	scsi_cmd.opcode = READ_CAPACITY;
237 
238 	/*
239 	 * If the command works, interpret the result as a 4 byte
240 	 * number of blocks
241 	 */
242 	if (scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
243 			  sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap),
244 			  2, 20000, NULL, flags | SCSI_DATA_IN) != 0) {
245 		sc_print_addr(sc_link);
246 		printf("could not get size\n");
247 		return (0);
248 	}
249 
250 	max_addr = _4btol(rdcap.addr);
251 	if (blksize)
252 		*blksize = _4btol(rdcap.length);
253 
254 	if (max_addr == 0xffffffffUL) {
255 		/*
256 		 * The device is reporting it has more than 2^32-1 sectors. The
257 		 * 16-byte READ CAPACITY command must be issued to get full
258 		 * capacity.
259 		 */
260 		sc_print_addr(sc_link);
261 		printf("only the first 4,294,967,295 sectors will be used.\n");
262 		return (0xffffffffUL);
263 	}
264 
265 	return (max_addr + 1);
266 }
267 
268 /*
269  * Get scsi driver to send a "are you ready?" command
270  */
271 int
scsi_test_unit_ready(sc_link,retries,flags)272 scsi_test_unit_ready(sc_link, retries, flags)
273 	struct scsi_link *sc_link;
274 	int retries;
275 	int flags;
276 {
277 	struct scsi_test_unit_ready scsi_cmd;
278 
279 	if (sc_link->quirks & ADEV_NOTUR)
280 		return 0;
281 
282 	bzero(&scsi_cmd, sizeof(scsi_cmd));
283 	scsi_cmd.opcode = TEST_UNIT_READY;
284 
285 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
286 	    sizeof(scsi_cmd), 0, 0, retries, 10000, NULL, flags);
287 }
288 
289 /*
290  * Do a scsi operation asking a device what it is.
291  * Use the scsi_cmd routine in the switch table.
292  */
293 int
scsi_inquire(sc_link,inqbuf,flags)294 scsi_inquire(sc_link, inqbuf, flags)
295 	struct scsi_link *sc_link;
296 	struct scsi_inquiry_data *inqbuf;
297 	int flags;
298 {
299 	struct scsi_inquiry scsi_cmd;
300 	int error;
301 
302 	bzero(&scsi_cmd, sizeof scsi_cmd);
303 	scsi_cmd.opcode = INQUIRY;
304 
305 	bzero(inqbuf, sizeof *inqbuf);
306 
307 	memset(&inqbuf->vendor, ' ', sizeof inqbuf->vendor);
308 	memset(&inqbuf->product, ' ', sizeof inqbuf->product);
309 	memset(&inqbuf->revision, ' ', sizeof inqbuf->revision);
310 	memset(&inqbuf->extra, ' ', sizeof inqbuf->extra);
311 
312 	/*
313 	 * Ask for only the basic 36 bytes of SCSI2 inquiry information. This
314 	 * avoids problems with devices that choke trying to supply more.
315 	 */
316 	scsi_cmd.length = SID_INQUIRY_HDR + SID_SCSI2_ALEN;
317 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
318 	    sizeof(scsi_cmd), (u_char *)inqbuf, scsi_cmd.length, 2, 10000, NULL,
319 	    SCSI_DATA_IN | flags);
320 
321 	return (error);
322 }
323 
324 /*
325  * Prevent or allow the user to remove the media
326  */
327 int
scsi_prevent(sc_link,type,flags)328 scsi_prevent(sc_link, type, flags)
329 	struct scsi_link *sc_link;
330 	int type, flags;
331 {
332 	struct scsi_prevent scsi_cmd;
333 
334 	if (sc_link->quirks & ADEV_NODOORLOCK)
335 		return 0;
336 
337 	bzero(&scsi_cmd, sizeof(scsi_cmd));
338 	scsi_cmd.opcode = PREVENT_ALLOW;
339 	scsi_cmd.how = type;
340 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
341 	    sizeof(scsi_cmd), 0, 0, 2, 5000, NULL, flags);
342 }
343 
344 /*
345  * Get scsi driver to send a "start up" command
346  */
347 int
scsi_start(sc_link,type,flags)348 scsi_start(sc_link, type, flags)
349 	struct scsi_link *sc_link;
350 	int type, flags;
351 {
352 	struct scsi_start_stop scsi_cmd;
353 
354 	bzero(&scsi_cmd, sizeof(scsi_cmd));
355 	scsi_cmd.opcode = START_STOP;
356 	scsi_cmd.byte2 = 0x00;
357 	scsi_cmd.how = type;
358 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
359 	    sizeof(scsi_cmd), 0, 0, 2,
360 	    type == SSS_START ? 30000 : 10000, NULL, flags);
361 }
362 
363 int
scsi_mode_sense(sc_link,byte2,page,data,len,flags,timeout)364 scsi_mode_sense(sc_link, byte2, page, data, len, flags, timeout)
365 	struct scsi_link *sc_link;
366 	int byte2, page, flags, timeout;
367 	size_t len;
368 	struct scsi_mode_header *data;
369 {
370 	struct scsi_mode_sense scsi_cmd;
371 	int error;
372 
373 	/*
374 	 * Make sure the sense buffer is clean before we do the mode sense, so
375 	 * that checks for bogus values of 0 will work in case the mode sense
376 	 * fails.
377 	 */
378 	bzero(data, len);
379 
380 	bzero(&scsi_cmd, sizeof(scsi_cmd));
381 	scsi_cmd.opcode = MODE_SENSE;
382 	scsi_cmd.byte2 = byte2;
383 	scsi_cmd.page = page;
384 	scsi_cmd.length = len & 0xff;
385 
386 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
387 	    sizeof(scsi_cmd), (u_char *)data, len, 4, timeout, NULL,
388 	    flags | SCSI_DATA_IN);
389 
390 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_mode_sense: page %#x, error = %d\n",
391 	    page, error));
392 
393 	return (error);
394 }
395 
396 int
scsi_mode_sense_big(sc_link,byte2,page,data,len,flags,timeout)397 scsi_mode_sense_big(sc_link, byte2, page, data, len, flags, timeout)
398 	struct scsi_link *sc_link;
399 	int byte2, page, flags, timeout;
400 	size_t len;
401 	struct scsi_mode_header_big *data;
402 {
403 	struct scsi_mode_sense_big scsi_cmd;
404 	int error;
405 
406 	/*
407 	 * Make sure the sense buffer is clean before we do the mode sense, so
408 	 * that checks for bogus values of 0 will work in case the mode sense
409 	 * fails.
410 	 */
411 	bzero(data, len);
412 
413 	bzero(&scsi_cmd, sizeof(scsi_cmd));
414 	scsi_cmd.opcode = MODE_SENSE_BIG;
415 	scsi_cmd.byte2 = byte2;
416 	scsi_cmd.page = page;
417 	_lto2b(len, scsi_cmd.length);
418 
419 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
420 	    sizeof(scsi_cmd), (u_char *)data, len, 4, timeout, NULL,
421 	    flags | SCSI_DATA_IN);
422 
423 	SC_DEBUG(sc_link, SDEV_DB2,
424 	    ("scsi_mode_sense_big: page %#x, error = %d\n", page, error));
425 
426 	return (error);
427 }
428 
429 void *
scsi_mode_sense_page(hdr,page_len)430 scsi_mode_sense_page(hdr, page_len)
431 	struct scsi_mode_header *hdr;
432 	const int page_len;
433 {
434 	int total_length, header_length;
435 
436 	total_length = hdr->data_length + sizeof(hdr->data_length);
437 	header_length = sizeof(*hdr) + hdr->blk_desc_len;
438 
439 	if ((total_length - header_length) < page_len)
440 		return (NULL);
441 
442 	return ((u_char *)hdr + header_length);
443 }
444 
445 void *
scsi_mode_sense_big_page(hdr,page_len)446 scsi_mode_sense_big_page(hdr, page_len)
447 	struct scsi_mode_header_big *hdr;
448 	const int page_len;
449 {
450 	int total_length, header_length;
451 
452 	total_length = _2btol(hdr->data_length) + sizeof(hdr->data_length);
453 	header_length = sizeof(*hdr) + _2btol(hdr->blk_desc_len);
454 
455 	if ((total_length - header_length) < page_len)
456 		return (NULL);
457 
458 	return ((u_char *)hdr + header_length);
459 }
460 
461 int
scsi_do_mode_sense(sc_link,page,buf,page_data,density,block_count,block_size,page_len,flags,big)462 scsi_do_mode_sense(sc_link, page, buf, page_data, density, block_count,
463     block_size, page_len, flags, big)
464 	struct scsi_link *sc_link;
465 	struct scsi_mode_sense_buf *buf;
466 	int page, page_len, flags, *big;
467 	u_int32_t *density, *block_size;
468 	u_int64_t *block_count;
469 	void **page_data;
470 {
471 	struct scsi_direct_blk_desc *direct;
472 	struct scsi_blk_desc *general;
473 	int error, blk_desc_len, offset;
474 
475 	*page_data = NULL;
476 
477 	if (density)
478 		*density = 0;
479 	if (block_count)
480 		*block_count = 0;
481 	if (block_size)
482 		*block_size = 0;
483 	if (big)
484 		*big = 0;
485 
486 	if ((sc_link->flags & SDEV_ATAPI) == 0) {
487 		/*
488 		 * Try 6 byte mode sense request first. Some devices don't
489 		 * distinguish between 6 and 10 byte MODE SENSE commands,
490 		 * returning 6 byte data for 10 byte requests. Don't bother
491 		 * with SMS_DBD. Check returned data length to ensure that
492 		 * at least a header (3 additional bytes) is returned.
493 		 */
494 		error = scsi_mode_sense(sc_link, 0, page, &buf->headers.hdr,
495 		    sizeof(*buf), flags, 20000);
496 		if (error == 0 && buf->headers.hdr.data_length > 2) {
497 			*page_data = scsi_mode_sense_page(&buf->headers.hdr,
498 			    page_len);
499 			offset = sizeof(struct scsi_mode_header);
500 			blk_desc_len = buf->headers.hdr.blk_desc_len;
501 			goto blk_desc;
502 		}
503 	}
504 
505 	/*
506 	 * Try 10 byte mode sense request. Don't bother with SMS_DBD or
507 	 * SMS_LLBAA. Bail out if the returned information is less than
508 	 * a big header in size (6 additional bytes).
509 	 */
510 	error = scsi_mode_sense_big(sc_link, 0, page, &buf->headers.hdr_big,
511 	    sizeof(*buf), flags, 20000);
512 	if (error != 0)
513 		return (error);
514 	if (_2btol(buf->headers.hdr_big.data_length) < 6)
515 		return (EIO);
516 
517 	if (big)
518 		*big = 1;
519 	offset = sizeof(struct scsi_mode_header_big);
520 	*page_data = scsi_mode_sense_big_page(&buf->headers.hdr_big, page_len);
521 	blk_desc_len = _2btol(buf->headers.hdr_big.blk_desc_len);
522 
523 blk_desc:
524 	/* Both scsi_blk_desc and scsi_direct_blk_desc are 8 bytes. */
525 	if (blk_desc_len == 0 || (blk_desc_len % 8 != 0))
526 		return (0);
527 
528 	switch (sc_link->inqdata.device & SID_TYPE) {
529 	case T_SEQUENTIAL:
530 		/*
531 		 * XXX What other device types return general block descriptors?
532 		 */
533 		general = (struct scsi_blk_desc *)&buf->headers.buf[offset];
534 		if (density)
535 			*density = general->density;
536 		if (block_size)
537 			*block_size = _3btol(general->blklen);
538 		if (block_count)
539 			*block_count = (u_int64_t)_3btol(general->nblocks);
540 		break;
541 
542 	default:
543 		direct = (struct scsi_direct_blk_desc *)&buf->
544 		    headers.buf[offset];
545 		if (density)
546 			*density = direct->density;
547 		if (block_size)
548 			*block_size = _3btol(direct->blklen);
549 		if (block_count)
550 			*block_count = (u_int64_t)_4btol(direct->nblocks);
551 		break;
552 	}
553 
554 	return (0);
555 }
556 
557 int
scsi_mode_select(sc_link,byte2,data,flags,timeout)558 scsi_mode_select(sc_link, byte2, data, flags, timeout)
559 	struct scsi_link *sc_link;
560 	int byte2, flags, timeout;
561 	struct scsi_mode_header *data;
562 {
563 	struct scsi_mode_select scsi_cmd;
564 	int error;
565 
566 	bzero(&scsi_cmd, sizeof(scsi_cmd));
567 	scsi_cmd.opcode = MODE_SELECT;
568 	scsi_cmd.byte2 = byte2;
569 	scsi_cmd.length = data->data_length + 1; /* 1 == sizeof(data_length) */
570 
571 	/* Length is reserved when doing mode select so zero it. */
572 	data->data_length = 0;
573 
574 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
575 	    sizeof(scsi_cmd), (u_char *)data, scsi_cmd.length, 4, timeout, NULL,
576 	    flags | SCSI_DATA_OUT);
577 
578 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_mode_select: error = %d\n", error));
579 
580 	return (error);
581 }
582 
583 int
scsi_mode_select_big(sc_link,byte2,data,flags,timeout)584 scsi_mode_select_big(sc_link, byte2, data, flags, timeout)
585 	struct scsi_link *sc_link;
586 	int byte2, flags, timeout;
587 	struct scsi_mode_header_big *data;
588 {
589 	struct scsi_mode_select_big scsi_cmd;
590 	u_int32_t len;
591 	int error;
592 
593 	len = _2btol(data->data_length) + 2; /* 2 == sizeof data->data_length */
594 
595 	bzero(&scsi_cmd, sizeof(scsi_cmd));
596 	scsi_cmd.opcode = MODE_SELECT_BIG;
597 	scsi_cmd.byte2 = byte2;
598 	_lto2b(len, scsi_cmd.length);
599 
600 	/* Length is reserved when doing mode select so zero it. */
601 	_lto2b(0, data->data_length);
602 
603 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)&scsi_cmd,
604 	    sizeof(scsi_cmd), (u_char *)data, len, 4, timeout, NULL,
605 	    flags | SCSI_DATA_OUT);
606 
607 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_mode_select_big: error = %d\n",
608 	    error));
609 
610 	return (error);
611 }
612 
613 /*
614  * This routine is called by the scsi interrupt when the transfer is complete.
615  */
616 void
scsi_done(xs)617 scsi_done(xs)
618 	struct scsi_xfer *xs;
619 {
620 	struct scsi_link *sc_link = xs->sc_link;
621 	struct buf *bp;
622 	int error;
623 
624 	splassert(IPL_BIO);
625 
626 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_done\n"));
627 #ifdef	SCSIDEBUG
628 	if ((sc_link->flags & SDEV_DB1) != 0)
629 		show_scsi_cmd(xs);
630 #endif /* SCSIDEBUG */
631 
632 	/*
633  	 * If it's a user level request, bypass all usual completion processing,
634  	 * let the user work it out.. We take reponsibility for freeing the
635  	 * xs when the user returns. (and restarting the device's queue).
636  	 */
637 	if ((xs->flags & SCSI_USER) != 0) {
638 		SC_DEBUG(sc_link, SDEV_DB3, ("calling user done()\n"));
639 		scsi_user_done(xs); /* to take a copy of the sense etc. */
640 		SC_DEBUG(sc_link, SDEV_DB3, ("returned from user done()\n"));
641 
642 		scsi_free_xs(xs); /* restarts queue too */
643 		SC_DEBUG(sc_link, SDEV_DB3, ("returning to adapter\n"));
644 		return;
645 	}
646 
647 	if (!((xs->flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)) {
648 		/*
649 		 * if it's a normal upper level request, then ask
650 		 * the upper level code to handle error checking
651 		 * rather than doing it here at interrupt time
652 		 */
653 		wakeup(xs);
654 		return;
655 	}
656 
657 	/*
658 	 * Go and handle errors now.
659 	 * If it returns ERESTART then we should RETRY
660 	 */
661 retry:
662 	error = sc_err1(xs);
663 	if (error == ERESTART) {
664 		switch ((*(sc_link->adapter->scsi_cmd)) (xs)) {
665 		case SUCCESSFULLY_QUEUED:
666 			return;
667 
668 		case TRY_AGAIN_LATER:
669 			xs->error = XS_BUSY;
670 			/* FALLTHROUGH */
671 		case COMPLETE:
672 			goto retry;
673 		}
674 	}
675 
676 	bp = xs->bp;
677 	if (bp) {
678 		if (error) {
679 			bp->b_error = error;
680 			bp->b_flags |= B_ERROR;
681 			bp->b_resid = bp->b_bcount;
682 		} else {
683 			bp->b_error = 0;
684 			bp->b_resid = xs->resid;
685 		}
686 	}
687 	if (sc_link->device->done) {
688 		/*
689 		 * Tell the device the operation is actually complete.
690 		 * No more will happen with this xfer.  This for
691 		 * notification of the upper-level driver only; they
692 		 * won't be returning any meaningful information to us.
693 		 */
694 		(*sc_link->device->done)(xs);
695 	}
696 	scsi_free_xs(xs);
697 	if (bp)
698 		biodone(bp);
699 }
700 
701 int
scsi_execute_xs(xs)702 scsi_execute_xs(xs)
703 	struct scsi_xfer *xs;
704 {
705 	int error;
706 	int s;
707 	int flags;
708 
709 	xs->flags &= ~ITSDONE;
710 	xs->error = XS_NOERROR;
711 	xs->resid = xs->datalen;
712 	xs->status = 0;
713 
714 	/*
715 	 * Do the transfer. If we are polling we will return:
716 	 * COMPLETE,  Was poll, and scsi_done has been called
717 	 * TRY_AGAIN_LATER, Adapter short resources, try again
718 	 *
719 	 * if under full steam (interrupts) it will return:
720 	 * SUCCESSFULLY_QUEUED, will do a wakeup when complete
721 	 * TRY_AGAIN_LATER, (as for polling)
722 	 * After the wakeup, we must still check if it succeeded
723 	 *
724 	 * If we have a SCSI_NOSLEEP (typically because we have a buf)
725 	 * we just return.  All the error processing and the buffer
726 	 * code both expect us to return straight to them, so as soon
727 	 * as the command is queued, return.
728 	 */
729 
730 	/*
731 	 * We save the flags here because the xs structure may already
732 	 * be freed by scsi_done by the time adapter->scsi_cmd returns.
733 	 *
734 	 * scsi_done is responsible for freeing the xs if either
735 	 * (flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP
736 	 * -or-
737 	 * (flags & SCSI_USER) != 0
738 	 *
739 	 * Note: SCSI_USER must always be called with SCSI_NOSLEEP
740 	 * and never with SCSI_POLL, so the second expression should be
741 	 * is equivalent to the first.
742 	 */
743 
744 	flags = xs->flags;
745 #ifdef DIAGNOSTIC
746 	if ((flags & (SCSI_USER | SCSI_NOSLEEP)) == SCSI_USER)
747 		panic("scsi_execute_xs: USER without NOSLEEP");
748 	if ((flags & (SCSI_USER | SCSI_POLL)) == (SCSI_USER | SCSI_POLL))
749 		panic("scsi_execute_xs: USER with POLL");
750 #endif
751 retry:
752 	switch ((*(xs->sc_link->adapter->scsi_cmd)) (xs)) {
753 	case SUCCESSFULLY_QUEUED:
754 		if ((flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
755 			return EJUSTRETURN;
756 #ifdef DIAGNOSTIC
757 		if (flags & SCSI_NOSLEEP)
758 			panic("scsi_execute_xs: NOSLEEP and POLL");
759 #endif
760 		s = splbio();
761 		while ((xs->flags & ITSDONE) == 0)
762 			tsleep(xs, PRIBIO + 1, "scsi_scsi_cmd", 0);
763 		splx(s);
764 		/* FALLTHROUGH */
765 	case COMPLETE:		/* Polling command completed ok */
766 		if ((flags & (SCSI_NOSLEEP | SCSI_POLL)) == SCSI_NOSLEEP)
767 			return EJUSTRETURN;
768 		if (xs->bp)
769 			return EJUSTRETURN;
770 	doit:
771 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("back in cmd()\n"));
772 		if ((error = sc_err1(xs)) != ERESTART)
773 			return error;
774 		goto retry;
775 
776 	case TRY_AGAIN_LATER:	/* adapter resource shortage */
777 		xs->error = XS_BUSY;
778 		goto doit;
779 
780 	default:
781 		panic("scsi_execute_xs: invalid return code");
782 	}
783 
784 #ifdef DIAGNOSTIC
785 	panic("scsi_execute_xs: impossible");
786 #endif
787 	return EINVAL;
788 }
789 
790 /*
791  * ask the scsi driver to perform a command for us.
792  * tell it where to read/write the data, and how
793  * long the data is supposed to be. If we have  a buf
794  * to associate with the transfer, we need that too.
795  */
796 int
scsi_scsi_cmd(sc_link,scsi_cmd,cmdlen,data_addr,datalen,retries,timeout,bp,flags)797 scsi_scsi_cmd(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
798     retries, timeout, bp, flags)
799 	struct scsi_link *sc_link;
800 	struct scsi_generic *scsi_cmd;
801 	int cmdlen;
802 	u_char *data_addr;
803 	int datalen;
804 	int retries;
805 	int timeout;
806 	struct buf *bp;
807 	int flags;
808 {
809 	struct scsi_xfer *xs;
810 	int error;
811 	int s;
812 
813 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_cmd\n"));
814 
815 #ifdef DIAGNOSTIC
816 	if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
817 		panic("scsi_scsi_cmd: buffer without nosleep");
818 #endif
819 
820 	if ((xs = scsi_make_xs(sc_link, scsi_cmd, cmdlen, data_addr, datalen,
821 	    retries, timeout, bp, flags)) == NULL)
822 		return ENOMEM;
823 
824 	if ((error = scsi_execute_xs(xs)) == EJUSTRETURN)
825 		return 0;
826 
827 	s = splbio();
828 	/*
829 	 * we have finished with the xfer stuct, free it and
830 	 * check if anyone else needs to be started up.
831 	 */
832 	scsi_free_xs(xs);
833 	splx(s);
834 	return error;
835 }
836 
837 int
sc_err1(xs)838 sc_err1(xs)
839 	struct scsi_xfer *xs;
840 {
841 	int error;
842 
843 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("sc_err1,err = 0x%x\n", xs->error));
844 
845 	/*
846 	 * If it has a buf, we might be working with
847 	 * a request from the buffer cache or some other
848 	 * piece of code that requires us to process
849 	 * errors at interrupt time. We have probably
850 	 * been called by scsi_done()
851 	 */
852 	switch (xs->error) {
853 	case XS_NOERROR:	/* nearly always hit this one */
854 		error = 0;
855 		break;
856 
857 	case XS_SENSE:
858 	case XS_SHORTSENSE:
859 		if ((error = scsi_interpret_sense(xs)) == ERESTART) {
860 			if (xs->error == XS_BUSY) {
861 				xs->error = XS_SENSE;
862 				goto sense_retry;
863 			}
864 			goto retry;
865 		}
866 		SC_DEBUG(xs->sc_link, SDEV_DB3,
867 		    ("scsi_interpret_sense returned %d\n", error));
868 		break;
869 
870 	case XS_BUSY:
871 	sense_retry:
872 		if (xs->retries) {
873 			if ((xs->flags & SCSI_POLL) != 0)
874 				delay(1000000);
875 			else if ((xs->flags & SCSI_NOSLEEP) == 0) {
876 				tsleep(&lbolt, PRIBIO, "scbusy", 0);
877 			} else
878 				goto lose;
879 		}
880 		/* FALLTHROUGH */
881 	case XS_TIMEOUT:
882 	retry:
883 		if (xs->retries--) {
884 			xs->error = XS_NOERROR;
885 			xs->flags &= ~ITSDONE;
886 			return ERESTART;
887 		}
888 		/* FALLTHROUGH */
889 	case XS_DRIVER_STUFFUP:
890 	lose:
891 		error = EIO;
892 		break;
893 
894 	case XS_SELTIMEOUT:
895 		/* XXX Disable device? */
896 		error = EIO;
897 		break;
898 
899 	case XS_RESET:
900 		if (xs->retries) {
901 			SC_DEBUG(xs->sc_link, SDEV_DB3,
902 			    ("restarting command destroyed by reset\n"));
903 			goto retry;
904 		}
905 		error = EIO;
906 		break;
907 
908 	default:
909 		sc_print_addr(xs->sc_link);
910 		printf("unknown error category (0x%x) from scsi driver\n",
911 		    xs->error);
912 		error = EIO;
913 		break;
914 	}
915 
916 	return error;
917 }
918 
919 /*
920  * Look at the returned sense and act on the error, determining
921  * the unix error number to pass back.  (0 = report no error)
922  *
923  * THIS IS THE DEFAULT ERROR HANDLER
924  */
925 int
scsi_interpret_sense(xs)926 scsi_interpret_sense(xs)
927 	struct scsi_xfer *xs;
928 {
929 	struct scsi_sense_data *sense = &xs->sense;
930 	struct scsi_link *sc_link = xs->sc_link;
931 	u_int8_t serr, skey;
932 	int error;
933 
934 	SC_DEBUG(sc_link, SDEV_DB1,
935 	    ("code:%#x valid:%d key:%#x ili:%d eom:%d fmark:%d extra:%d\n",
936 	    sense->error_code & SSD_ERRCODE,
937 	    sense->error_code & SSD_ERRCODE_VALID ? 1 : 0,
938 	    sense->flags & SSD_KEY,
939 	    sense->flags & SSD_ILI ? 1 : 0,
940 	    sense->flags & SSD_EOM ? 1 : 0,
941 	    sense->flags & SSD_FILEMARK ? 1 : 0,
942 	    sense->extra_len));
943 #ifdef	SCSIDEBUG
944 	if ((sc_link->flags & SDEV_DB1) != 0)
945 		show_mem((u_char *)&xs->sense, sizeof xs->sense);
946 #endif	/* SCSIDEBUG */
947 
948 	/*
949 	 * If the device has it's own error handler, call it first.
950 	 * If it returns a legit error value, return that, otherwise
951 	 * it wants us to continue with normal error processing.
952 	 */
953 	if (sc_link->device->err_handler) {
954 		SC_DEBUG(sc_link, SDEV_DB2, ("calling private err_handler()\n"));
955 		error = (*sc_link->device->err_handler) (xs);
956 		if (error != EJUSTRETURN)
957 			return error;		/* error >= 0  better ? */
958 	}
959 
960 	/* Default sense interpretation. */
961 	serr = sense->error_code & SSD_ERRCODE;
962 	if (serr != 0x70 && serr != 0x71)
963 		skey = 0xff;	/* Invalid value, since key is 4 bit value. */
964 	else
965 		skey = sense->flags & SSD_KEY;
966 
967 	/*
968 	 * Interpret the key/asc/ascq information where appropriate.
969 	 */
970 	error = 0;
971 	switch (skey) {
972 	case SKEY_NO_SENSE:
973 	case SKEY_RECOVERED_ERROR:
974 		if (xs->resid == xs->datalen)
975 			xs->resid = 0;	/* not short read */
976 		break;
977 	case SKEY_BLANK_CHECK:
978 	case SKEY_EQUAL:
979 		break;
980 	case SKEY_NOT_READY:
981 		if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
982 			return (0);
983 		error = EIO;
984 		if (xs->retries) {
985 			switch (sense->add_sense_code) {
986 			case 0x04:	/* LUN not ready */
987 				switch (sense->add_sense_code_qual) {
988 				case 0x01: /* Becoming Ready */
989 				case 0x04: /* Format In Progress */
990 				case 0x05: /* Rebuild In Progress */
991 				case 0x06: /* Recalculation In Progress */
992 				case 0x07: /* Operation In Progress */
993 				case 0x08: /* Long Write In Progress */
994 				case 0x09: /* Self-Test In Progress */
995 					xs->error = XS_BUSY; /* wait & retry */
996 					return (ERESTART);
997 				}
998 				break;
999 			case 0x3a:	/* Medium not present */
1000 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
1001 				error = ENODEV;
1002 				break;
1003 			}
1004 		}
1005 		break;
1006 	case SKEY_ILLEGAL_REQUEST:
1007 		if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
1008 			return 0;
1009 		error = EINVAL;
1010 		break;
1011 	case SKEY_UNIT_ATTENTION:
1012 		if (sense->add_sense_code == 0x29)
1013 			return (ERESTART); /* device or bus reset */
1014 		if ((sc_link->flags & SDEV_REMOVABLE) != 0)
1015 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
1016 		if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
1017 		    /* XXX Should reupload any transient state. */
1018 		    (sc_link->flags & SDEV_REMOVABLE) == 0)
1019 			return ERESTART;
1020 		error = EIO;
1021 		break;
1022 	case SKEY_WRITE_PROTECT:
1023 		error = EROFS;
1024 		break;
1025 	case SKEY_ABORTED_COMMAND:
1026 		error = ERESTART;
1027 		break;
1028 	case SKEY_VOLUME_OVERFLOW:
1029 		error = ENOSPC;
1030 		break;
1031 	default:
1032 		error = EIO;
1033 		break;
1034 	}
1035 
1036 	if (skey && (xs->flags & SCSI_SILENT) == 0)
1037 		scsi_print_sense(xs);
1038 
1039 	return error;
1040 }
1041 
1042 /*
1043  * Utility routines often used in SCSI stuff
1044  */
1045 
1046 
1047 /*
1048  * Print out the scsi_link structure's address info.
1049  */
1050 void
sc_print_addr(sc_link)1051 sc_print_addr(sc_link)
1052 	struct scsi_link *sc_link;
1053 {
1054 
1055 	printf("%s(%s:%d:%d): ",
1056 	    sc_link->device_softc ?
1057 	    ((struct device *)sc_link->device_softc)->dv_xname : "probe",
1058 	    ((struct device *)sc_link->adapter_softc)->dv_xname,
1059 	    sc_link->target, sc_link->lun);
1060 }
1061 
1062 static const char *sense_keys[16] = {
1063 	"No Additional Sense",
1064 	"Soft Error",
1065 	"Not Ready",
1066 	"Media Error",
1067 	"Hardware Error",
1068 	"Illegal Request",
1069 	"Unit Attention",
1070 	"Write Protected",
1071 	"Blank Check",
1072 	"Vendor Unique",
1073 	"Copy Aborted",
1074 	"Aborted Command",
1075 	"Equal Error",
1076 	"Volume Overflow",
1077 	"Miscompare Error",
1078 	"Reserved"
1079 };
1080 
1081 #ifdef SCSITERSE
1082 static __inline void
asc2ascii(asc,ascq,result,len)1083 asc2ascii(asc, ascq, result, len)
1084 	u_int8_t asc, ascq;
1085 	char *result;
1086 	size_t len;
1087 {
1088 	snprintf(result, len, "ASC 0x%02x ASCQ 0x%02x", asc, ascq);
1089 }
1090 #else
1091 static const struct {
1092 	u_int8_t asc, ascq;
1093 	char *description;
1094 } adesc[] = {
1095 	{ 0x00, 0x00, "No Additional Sense Information" },
1096 	{ 0x00, 0x01, "Filemark Detected" },
1097 	{ 0x00, 0x02, "End-Of-Partition/Medium Detected" },
1098 	{ 0x00, 0x03, "Setmark Detected" },
1099 	{ 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
1100 	{ 0x00, 0x05, "End-Of-Data Detected" },
1101 	{ 0x00, 0x06, "I/O Process Terminated" },
1102 	{ 0x00, 0x11, "Audio Play Operation In Progress" },
1103 	{ 0x00, 0x12, "Audio Play Operation Paused" },
1104 	{ 0x00, 0x13, "Audio Play Operation Successfully Completed" },
1105 	{ 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
1106 	{ 0x00, 0x15, "No Current Audio Status To Return" },
1107 	{ 0x00, 0x16, "Operation In Progress" },
1108 	{ 0x00, 0x17, "Cleaning Requested" },
1109 	{ 0x00, 0x18, "Erase Operation In Progress" },
1110 	{ 0x00, 0x19, "Locate Operation In Progress" },
1111 	{ 0x00, 0x1A, "Rewind Operation In Progress" },
1112 	{ 0x00, 0x1B, "Set Capacity Operation In Progress" },
1113 	{ 0x00, 0x1C, "Verify Operation In Progress" },
1114 	{ 0x01, 0x00, "No Index/Sector Signal" },
1115 	{ 0x02, 0x00, "No Seek Complete" },
1116 	{ 0x03, 0x00, "Peripheral Device Write Fault" },
1117 	{ 0x03, 0x01, "No Write Current" },
1118 	{ 0x03, 0x02, "Excessive Write Errors" },
1119 	{ 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
1120 	{ 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
1121 	{ 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
1122 	{ 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
1123 	{ 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
1124 	{ 0x04, 0x05, "Logical Unit Not Ready, Rebuild In Progress" },
1125 	{ 0x04, 0x06, "Logical Unit Not Ready, Recalculation In Progress" },
1126 	{ 0x04, 0x07, "Logical Unit Not Ready, Operation In Progress" },
1127 	{ 0x04, 0x08, "Logical Unit Not Ready, Long Write In Progress" },
1128 	{ 0x04, 0x09, "Logical Unit Not Ready, Self-Test In Progress" },
1129 	{ 0x04, 0x0A, "Logical Unit Not Accessible, Asymmetric Access State Transition" },
1130 	{ 0x04, 0x0B, "Logical Unit Not Accessible, Target Port In Standby State" },
1131 	{ 0x04, 0x0C, "Logical Unit Not Accessible, Target Port In Unavailable State" },
1132 	{ 0x04, 0x10, "Logical Unit Not Ready, Auxiliary Memory Not Accessible" },
1133 	{ 0x04, 0x11, "Logical Unit Not Ready, Notify (Enable Spinup) Required" },
1134 	{ 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
1135 	{ 0x06, 0x00, "No Reference Position Found" },
1136 	{ 0x07, 0x00, "Multiple Peripheral Devices Selected" },
1137 	{ 0x08, 0x00, "Logical Unit Communication Failure" },
1138 	{ 0x08, 0x01, "Logical Unit Communication Timeout" },
1139 	{ 0x08, 0x02, "Logical Unit Communication Parity Error" },
1140 	{ 0x08, 0x03, "Logical Unit Communication CRC Error (ULTRA-DMA/32)" },
1141 	{ 0x08, 0x04, "Unreachable Copy Target" },
1142 	{ 0x09, 0x00, "Track Following Error" },
1143 	{ 0x09, 0x01, "Tracking Servo Failure" },
1144 	{ 0x09, 0x02, "Focus Servo Failure" },
1145 	{ 0x09, 0x03, "Spindle Servo Failure" },
1146 	{ 0x09, 0x04, "Head Select Fault" },
1147 	{ 0x0A, 0x00, "Error Log Overflow" },
1148 	{ 0x0B, 0x00, "Warning" },
1149 	{ 0x0B, 0x01, "Warning - Specified Temperature Exceeded" },
1150 	{ 0x0B, 0x02, "Warning - Enclosure Degraded" },
1151 	{ 0x0C, 0x00, "Write Error" },
1152 	{ 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
1153 	{ 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
1154 	{ 0x0C, 0x03, "Write Error - Recommend Reassignment" },
1155 	{ 0x0C, 0x04, "Compression Check Miscompare Error" },
1156 	{ 0x0C, 0x05, "Data Expansion Occurred During Compression" },
1157 	{ 0x0C, 0x06, "Block Not Compressible" },
1158 	{ 0x0C, 0x07, "Write Error - Recovery Needed" },
1159 	{ 0x0C, 0x08, "Write Error - Recovery Failed" },
1160 	{ 0x0C, 0x09, "Write Error - Loss Of Streaming" },
1161 	{ 0x0C, 0x0A, "Write Error - Padding Blocks Added" },
1162 	{ 0x0C, 0x0B, "Auxiliary Memory Write Error" },
1163 	{ 0x0C, 0x0C, "Write Error - Unexpected Unsolicited Data" },
1164 	{ 0x0C, 0x0D, "Write Error - Not Enough Unsolicited Data" },
1165 	{ 0x0D, 0x00, "Error Detected By Third Party Temporary Initiator" },
1166 	{ 0x0D, 0x01, "Third Party Device Failure" },
1167 	{ 0x0D, 0x02, "Copy Target Device Not Reachable" },
1168 	{ 0x0D, 0x03, "Incorrect Copy Target Device Type" },
1169 	{ 0x0D, 0x04, "Copy Target Device Data Underrun" },
1170 	{ 0x0D, 0x05, "Copy Target Device Data Overrun" },
1171 	{ 0x0E, 0x00, "Invalid Information Unit" },
1172 	{ 0x0E, 0x01, "Information Unit Too Short" },
1173 	{ 0x0E, 0x02, "Information Unit Too Long" },
1174 	{ 0x10, 0x00, "ID CRC Or ECC Error" },
1175 	{ 0x11, 0x00, "Unrecovered Read Error" },
1176 	{ 0x11, 0x01, "Read Retries Exhausted" },
1177 	{ 0x11, 0x02, "Error Too Long To Correct" },
1178 	{ 0x11, 0x03, "Multiple Read Errors" },
1179 	{ 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
1180 	{ 0x11, 0x05, "L-EC Uncorrectable Error" },
1181 	{ 0x11, 0x06, "CIRC Unrecovered Error" },
1182 	{ 0x11, 0x07, "Data Resynchronization Error" },
1183 	{ 0x11, 0x08, "Incomplete Block Read" },
1184 	{ 0x11, 0x09, "No Gap Found" },
1185 	{ 0x11, 0x0A, "Miscorrected Error" },
1186 	{ 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
1187 	{ 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite The Data" },
1188 	{ 0x11, 0x0D, "De-Compression CRC Error" },
1189 	{ 0x11, 0x0E, "Cannot Decompress Using Declared Algorithm" },
1190 	{ 0x11, 0x0F, "Error Reading UPC/EAN Number" },
1191 	{ 0x11, 0x10, "Error Reading ISRC Number" },
1192 	{ 0x11, 0x11, "Read Error - Loss Of Streaming" },
1193 	{ 0x11, 0x12, "Auxiliary Memory Read Error" },
1194 	{ 0x11, 0x13, "Read Error - Failed Retransmission Request" },
1195 	{ 0x12, 0x00, "Address Mark Not Found for ID Field" },
1196 	{ 0x13, 0x00, "Address Mark Not Found for Data Field" },
1197 	{ 0x14, 0x00, "Recorded Entity Not Found" },
1198 	{ 0x14, 0x01, "Record Not Found" },
1199 	{ 0x14, 0x02, "Filemark or Setmark Not Found" },
1200 	{ 0x14, 0x03, "End-Of-Data Not Found" },
1201 	{ 0x14, 0x04, "Block Sequence Error" },
1202 	{ 0x14, 0x05, "Record Not Found - Recommend Reassignment" },
1203 	{ 0x14, 0x06, "Record Not Found - Data Auto-Reallocated" },
1204 	{ 0x14, 0x07, "Locate Operation Failure" },
1205 	{ 0x15, 0x00, "Random Positioning Error" },
1206 	{ 0x15, 0x01, "Mechanical Positioning Error" },
1207 	{ 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
1208 	{ 0x16, 0x00, "Data Synchronization Mark Error" },
1209 	{ 0x16, 0x01, "Data Sync Error - Data Rewritten" },
1210 	{ 0x16, 0x02, "Data Sync Error - Recommend Rewrite" },
1211 	{ 0x16, 0x03, "Data Sync Error - Data Auto-Reallocated" },
1212 	{ 0x16, 0x04, "Data Sync Error - Recommend Reassignment" },
1213 	{ 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
1214 	{ 0x17, 0x01, "Recovered Data With Retries" },
1215 	{ 0x17, 0x02, "Recovered Data With Positive Head Offset" },
1216 	{ 0x17, 0x03, "Recovered Data With Negative Head Offset" },
1217 	{ 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
1218 	{ 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
1219 	{ 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
1220 	{ 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
1221 	{ 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
1222 	{ 0x17, 0x09, "Recovered Data Without ECC - Data Rewritten" },
1223 	{ 0x18, 0x00, "Recovered Data With Error Correction Applied" },
1224 	{ 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
1225 	{ 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
1226 	{ 0x18, 0x03, "Recovered Data With CIRC" },
1227 	{ 0x18, 0x04, "Recovered Data With L-EC" },
1228 	{ 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
1229 	{ 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
1230 	{ 0x18, 0x07, "Recovered Data With ECC - Data Rewritten" },
1231 	{ 0x18, 0x08, "Recovered Data With Linking" },
1232 	{ 0x19, 0x00, "Defect List Error" },
1233 	{ 0x19, 0x01, "Defect List Not Available" },
1234 	{ 0x19, 0x02, "Defect List Error in Primary List" },
1235 	{ 0x19, 0x03, "Defect List Error in Grown List" },
1236 	{ 0x1A, 0x00, "Parameter List Length Error" },
1237 	{ 0x1B, 0x00, "Synchronous Data Transfer Error" },
1238 	{ 0x1C, 0x00, "Defect List Not Found" },
1239 	{ 0x1C, 0x01, "Primary Defect List Not Found" },
1240 	{ 0x1C, 0x02, "Grown Defect List Not Found" },
1241 	{ 0x1D, 0x00, "Miscompare During Verify Operation" },
1242 	{ 0x1E, 0x00, "Recovered ID with ECC" },
1243 	{ 0x1F, 0x00, "Partial Defect List Transfer" },
1244 	{ 0x20, 0x00, "Invalid Command Operation Code" },
1245 	{ 0x20, 0x01, "Access Denied - Initiator Pending-Enrolled" },
1246 	{ 0x20, 0x02, "Access Denied - No Access rights" },
1247 	{ 0x20, 0x03, "Access Denied - Invalid Mgmt ID Key" },
1248 	{ 0x20, 0x04, "Illegal Command While In Write Capable State" },
1249 	{ 0x20, 0x05, "Obsolete" },
1250 	{ 0x20, 0x06, "Illegal Command While In Explicit Address Mode" },
1251 	{ 0x20, 0x07, "Illegal Command While In Implicit Address Mode" },
1252 	{ 0x20, 0x08, "Access Denied - Enrollment Conflict" },
1253 	{ 0x20, 0x09, "Access Denied - Invalid LU Identifier" },
1254 	{ 0x20, 0x0A, "Access Denied - Invalid Proxy Token" },
1255 	{ 0x20, 0x0B, "Access Denied - ACL LUN Conflict" },
1256 	{ 0x21, 0x00, "Logical Block Address Out of Range" },
1257 	{ 0x21, 0x01, "Invalid Element Address" },
1258 	{ 0x21, 0x02, "Invalid Address For Write" },
1259 	{ 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
1260 	{ 0x24, 0x00, "Illegal Field in CDB" },
1261 	{ 0x24, 0x01, "CDB Decryption Error" },
1262 	{ 0x24, 0x02, "Obsolete" },
1263 	{ 0x24, 0x03, "Obsolete" },
1264 	{ 0x24, 0x04, "Security Audit Value Frozen" },
1265 	{ 0x24, 0x05, "Security Working Key Frozen" },
1266 	{ 0x24, 0x06, "Nonce Not Unique" },
1267 	{ 0x24, 0x07, "Nonce Timestamp Out Of Range" },
1268 	{ 0x25, 0x00, "Logical Unit Not Supported" },
1269 	{ 0x26, 0x00, "Invalid Field In Parameter List" },
1270 	{ 0x26, 0x01, "Parameter Not Supported" },
1271 	{ 0x26, 0x02, "Parameter Value Invalid" },
1272 	{ 0x26, 0x03, "Threshold Parameters Not Supported" },
1273 	{ 0x26, 0x04, "Invalid Release Of Persistent Reservation" },
1274 	{ 0x26, 0x05, "Data Decryption Error" },
1275 	{ 0x26, 0x06, "Too Many Target Descriptors" },
1276 	{ 0x26, 0x07, "Unsupported Target Descriptor Type Code" },
1277 	{ 0x26, 0x08, "Too Many Segment Descriptors" },
1278 	{ 0x26, 0x09, "Unsupported Segment Descriptor Type Code" },
1279 	{ 0x26, 0x0A, "Unexpected Inexact Segment" },
1280 	{ 0x26, 0x0B, "Inline Data Length Exceeded" },
1281 	{ 0x26, 0x0C, "Invalid Operation For Copy Source Or Destination" },
1282 	{ 0x26, 0x0D, "Copy Segment Granularity Violation" },
1283 	{ 0x26, 0x0E, "Invalid Parameter While Port Is Enabled" },
1284 	{ 0x27, 0x00, "Write Protected" },
1285 	{ 0x27, 0x01, "Hardware Write Protected" },
1286 	{ 0x27, 0x02, "Logical Unit Software Write Protected" },
1287 	{ 0x27, 0x03, "Associated Write Protect" },
1288 	{ 0x27, 0x04, "Persistent Write Protect" },
1289 	{ 0x27, 0x05, "Permanent Write Protect" },
1290 	{ 0x27, 0x06, "Conditional Write Protect" },
1291 	{ 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
1292 	{ 0x28, 0x01, "Import Or Export Element Accessed" },
1293 	{ 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
1294 	{ 0x29, 0x01, "Power On Occurred" },
1295 	{ 0x29, 0x02, "SCSI Bus Reset Occurred" },
1296 	{ 0x29, 0x03, "Bus Device Reset Function Occurred" },
1297 	{ 0x29, 0x04, "Device Internal Reset" },
1298 	{ 0x29, 0x05, "Transceiver Mode Changed to Single Ended" },
1299 	{ 0x29, 0x06, "Transceiver Mode Changed to LVD" },
1300 	{ 0x29, 0x07, "I_T Nexus Loss Occurred" },
1301 	{ 0x2A, 0x00, "Parameters Changed" },
1302 	{ 0x2A, 0x01, "Mode Parameters Changed" },
1303 	{ 0x2A, 0x02, "Log Parameters Changed" },
1304 	{ 0x2A, 0x03, "Reservations Preempted" },
1305 	{ 0x2A, 0x04, "Reservations Released" },
1306 	{ 0x2A, 0x05, "Registrations Preempted" },
1307 	{ 0x2A, 0x06, "Asymmetric Access State Changed" },
1308 	{ 0x2A, 0x07, "Implicit Asymmetric Access State Transition Failed" },
1309 	{ 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
1310 	{ 0x2C, 0x00, "Command Sequence Error" },
1311 	{ 0x2C, 0x01, "Too Many Windows Specified" },
1312 	{ 0x2C, 0x02, "Invalid Combination of Windows Specified" },
1313 	{ 0x2C, 0x03, "Current Program Area Is Not Empty" },
1314 	{ 0x2C, 0x04, "Current Program Area Is Empty" },
1315 	{ 0x2C, 0x05, "Illegal Power Condition Request" },
1316 	{ 0x2C, 0x06, "Persistent Prevent Conflict" },
1317 	{ 0x2C, 0x07, "Previous Busy Status" },
1318 	{ 0x2C, 0x08, "Previous Task Set Full Status" },
1319 	{ 0x2C, 0x09, "Previous Reservation Conflict Status" },
1320 	{ 0x2C, 0x0A, "Partition Or Collection Contains User Objects" },
1321 	{ 0x2D, 0x00, "Overwrite Error On Update In Place" },
1322 	{ 0x2E, 0x00, "Insufficient Time For Operation" },
1323 	{ 0x2F, 0x00, "Commands Cleared By Another Initiator" },
1324 	{ 0x30, 0x00, "Incompatible Medium Installed" },
1325 	{ 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
1326 	{ 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
1327 	{ 0x30, 0x03, "Cleaning Cartridge Installed" },
1328 	{ 0x30, 0x04, "Cannot Write Medium - Unknown Format" },
1329 	{ 0x30, 0x05, "Cannot Write Medium - Incompatible Format" },
1330 	{ 0x30, 0x06, "Cannot Format Medium - Incompatible Medium" },
1331 	{ 0x30, 0x07, "Cleaning Failure" },
1332 	{ 0x30, 0x08, "Cannot Write - Application Code Mismatch" },
1333 	{ 0x30, 0x09, "Current Session Not Fixated For Append" },
1334 	{ 0x30, 0x0A, "Cleaning Request Rejected" },
1335 	{ 0x30, 0x10, "Medium Not Formatted" },
1336 	{ 0x31, 0x00, "Medium Format Corrupted" },
1337 	{ 0x31, 0x01, "Format Command Failed" },
1338 	{ 0x32, 0x00, "No Defect Spare Location Available" },
1339 	{ 0x32, 0x01, "Defect List Update Failure" },
1340 	{ 0x33, 0x00, "Tape Length Error" },
1341 	{ 0x34, 0x00, "Enclosure Failure" },
1342 	{ 0x35, 0x00, "Enclosure Services Failure" },
1343 	{ 0x35, 0x01, "Unsupported Enclosure Function" },
1344 	{ 0x35, 0x02, "Enclosure Services Unavailable" },
1345 	{ 0x35, 0x03, "Enclosure Services Transfer Failure" },
1346 	{ 0x35, 0x04, "Enclosure Services Transfer Refused" },
1347 	{ 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
1348 	{ 0x37, 0x00, "Rounded Parameter" },
1349 	{ 0x38, 0x00, "Event Status Notification" },
1350 	{ 0x38, 0x02, "ESN - Power Management Class Event" },
1351 	{ 0x38, 0x04, "ESN - Media Class Event" },
1352 	{ 0x38, 0x06, "ESN - Device Busy Class Event" },
1353 	{ 0x39, 0x00, "Saving Parameters Not Supported" },
1354 	{ 0x3A, 0x00, "Medium Not Present" },
1355 	{ 0x3A, 0x01, "Medium Not Present - Tray Closed" },
1356 	{ 0x3A, 0x02, "Medium Not Present - Tray Open" },
1357 	{ 0x3A, 0x03, "Medium Not Present - Loadable" },
1358 	{ 0x3A, 0x04, "Medium Not Present - Medium Auxiliary Memory Accessible" },
1359 	{ 0x3B, 0x00, "Sequential Positioning Error" },
1360 	{ 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
1361 	{ 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
1362 	{ 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
1363 	{ 0x3B, 0x04, "Slew Failure" },
1364 	{ 0x3B, 0x05, "Paper Jam" },
1365 	{ 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
1366 	{ 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
1367 	{ 0x3B, 0x08, "Reposition Error" },
1368 	{ 0x3B, 0x09, "Read Past End Of Medium" },
1369 	{ 0x3B, 0x0A, "Read Past Beginning Of Medium" },
1370 	{ 0x3B, 0x0B, "Position Past End Of Medium" },
1371 	{ 0x3B, 0x0C, "Position Past Beginning Of Medium" },
1372 	{ 0x3B, 0x0D, "Medium Destination Element Full" },
1373 	{ 0x3B, 0x0E, "Medium Source Element Empty" },
1374 	{ 0x3B, 0x0F, "End Of Medium Reached" },
1375 	{ 0x3B, 0x11, "Medium Magazine Not Accessible" },
1376 	{ 0x3B, 0x12, "Medium Magazine Removed" },
1377 	{ 0x3B, 0x13, "Medium Magazine Inserted" },
1378 	{ 0x3B, 0x14, "Medium Magazine Locked" },
1379 	{ 0x3B, 0x15, "Medium Magazine Unlocked" },
1380 	{ 0x3B, 0x16, "Mechanical Positioning Or Changer Error" },
1381 	{ 0x3D, 0x00, "Invalid Bits In IDENTIFY Message" },
1382 	{ 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
1383 	{ 0x3E, 0x01, "Logical Unit Failure" },
1384 	{ 0x3E, 0x02, "Timeout On Logical Unit" },
1385 	{ 0x3E, 0x03, "Logical Unit Failed Self-Test" },
1386 	{ 0x3E, 0x04, "Logical Unit Unable To Update Self-Test Log" },
1387 	{ 0x3F, 0x00, "Target Operating Conditions Have Changed" },
1388 	{ 0x3F, 0x01, "Microcode Has Changed" },
1389 	{ 0x3F, 0x02, "Changed Operating Definition" },
1390 	{ 0x3F, 0x03, "INQUIRY Data Has Changed" },
1391 	{ 0x3F, 0x04, "component Device Attached" },
1392 	{ 0x3F, 0x05, "Device Identifier Changed" },
1393 	{ 0x3F, 0x06, "Redundancy Group Created Or Modified" },
1394 	{ 0x3F, 0x07, "Redundancy Group Deleted" },
1395 	{ 0x3F, 0x08, "Spare Created Or Modified" },
1396 	{ 0x3F, 0x09, "Spare Deleted" },
1397 	{ 0x3F, 0x0A, "Volume Set Created Or Modified" },
1398 	{ 0x3F, 0x0B, "Volume Set Deleted" },
1399 	{ 0x3F, 0x0C, "Volume Set Deassigned" },
1400 	{ 0x3F, 0x0D, "Volume Set Reassigned" },
1401 	{ 0x3F, 0x0E, "Reported LUNs Data Has Changed" },
1402 	{ 0x3F, 0x0F, "Echo Buffer Overwritten" },
1403 	{ 0x3F, 0x10, "Medium Loadable" },
1404 	{ 0x3F, 0x11, "Medium Auxiliary Memory Accessible" },
1405 	{ 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
1406 	/*
1407 	 * ASC 0x40 also has an ASCQ range from 0x80 to 0xFF.
1408 	 * 0x40 0xNN DIAGNOSTIC FAILURE ON COMPONENT NN
1409 	 */
1410 	{ 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
1411 	{ 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
1412 	{ 0x43, 0x00, "Message Error" },
1413 	{ 0x44, 0x00, "Internal Target Failure" },
1414 	{ 0x45, 0x00, "Select Or Reselect Failure" },
1415 	{ 0x46, 0x00, "Unsuccessful Soft Reset" },
1416 	{ 0x47, 0x00, "SCSI Parity Error" },
1417 	{ 0x47, 0x01, "Data Phase CRC Error Detected" },
1418 	{ 0x47, 0x02, "SCSI Parity Error Detected During ST Data Phase" },
1419 	{ 0x47, 0x03, "Information Unit iuCRC Error Detected" },
1420 	{ 0x47, 0x04, "Asynchronous Information Protection Error Detected" },
1421 	{ 0x47, 0x05, "Protocol Service CRC Error" },
1422 	{ 0x47, 0x7F, "Some Commands Cleared By iSCSI Protocol Event" },
1423 	{ 0x48, 0x00, "Initiator Detected Error Message Received" },
1424 	{ 0x49, 0x00, "Invalid Message Error" },
1425 	{ 0x4A, 0x00, "Command Phase Error" },
1426 	{ 0x4B, 0x00, "Data Phase Error" },
1427 	{ 0x4B, 0x01, "Invalid Target Port Transfer Tag Received" },
1428 	{ 0x4B, 0x02, "Too Much Write Data" },
1429 	{ 0x4B, 0x03, "ACK/NAK Timeout" },
1430 	{ 0x4B, 0x04, "NAK Received" },
1431 	{ 0x4B, 0x05, "Data Offset Error" },
1432 	{ 0x4B, 0x06, "Initiator Response Timeout" },
1433 	{ 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
1434 	/*
1435 	 * ASC 0x4D has an ASCQ range from 0x00 to 0xFF.
1436 	 * 0x4D 0xNN TAGGED OVERLAPPED COMMANDS (NN = TASK TAG)
1437 	 */
1438 	{ 0x4E, 0x00, "Overlapped Commands Attempted" },
1439 	{ 0x50, 0x00, "Write Append Error" },
1440 	{ 0x50, 0x01, "Write Append Position Error" },
1441 	{ 0x50, 0x02, "Position Error Related To Timing" },
1442 	{ 0x51, 0x00, "Erase Failure" },
1443 	{ 0x51, 0x01, "Erase Failure - Incomplete Erase Operation Detected" },
1444 	{ 0x52, 0x00, "Cartridge Fault" },
1445 	{ 0x53, 0x00, "Media Load or Eject Failed" },
1446 	{ 0x53, 0x01, "Unload Tape Failure" },
1447 	{ 0x53, 0x02, "Medium Removal Prevented" },
1448 	{ 0x54, 0x00, "SCSI To Host System Interface Failure" },
1449 	{ 0x55, 0x00, "System Resource Failure" },
1450 	{ 0x55, 0x01, "System Buffer Full" },
1451 	{ 0x55, 0x02, "Insufficient Reservation Resources" },
1452 	{ 0x55, 0x03, "Insufficient Resources" },
1453 	{ 0x55, 0x04, "Insufficient Registration Resources" },
1454 	{ 0x55, 0x05, "Insufficient Access Control Resources" },
1455 	{ 0x55, 0x06, "Auxiliary Memory Out Of Space" },
1456 	{ 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
1457 	{ 0x58, 0x00, "Generation Does Not Exist" },
1458 	{ 0x59, 0x00, "Updated Block Read" },
1459 	{ 0x5A, 0x00, "Operator Request or State Change Input" },
1460 	{ 0x5A, 0x01, "Operator Medium Removal Requested" },
1461 	{ 0x5A, 0x02, "Operator Selected Write Protect" },
1462 	{ 0x5A, 0x03, "Operator Selected Write Permit" },
1463 	{ 0x5B, 0x00, "Log Exception" },
1464 	{ 0x5B, 0x01, "Threshold Condition Met" },
1465 	{ 0x5B, 0x02, "Log Counter At Maximum" },
1466 	{ 0x5B, 0x03, "Log List Codes Exhausted" },
1467 	{ 0x5C, 0x00, "RPL Status Change" },
1468 	{ 0x5C, 0x01, "Spindles Synchronized" },
1469 	{ 0x5C, 0x02, "Spindles Not Synchronized" },
1470 	{ 0x5D, 0x00, "Failure Prediction Threshold Exceeded" },
1471 	{ 0x5D, 0x01, "Media Failure Prediction Threshold Exceeded" },
1472 	{ 0x5D, 0x02, "Logical Unit Failure Prediction Threshold Exceeded" },
1473 	{ 0x5D, 0x03, "Spare Area Exhaustion Prediction Threshold Exceeded" },
1474 	{ 0x5D, 0x10, "Hardware Impending Failure General Hard Drive Failure" },
1475 	{ 0x5D, 0x11, "Hardware Impending Failure Drive Error Rate Too High" },
1476 	{ 0x5D, 0x12, "Hardware Impending Failure Data Error Rate Too High" },
1477 	{ 0x5D, 0x13, "Hardware Impending Failure Seek Error Rate Too High" },
1478 	{ 0x5D, 0x14, "Hardware Impending Failure Too Many Block Reassigns" },
1479 	{ 0x5D, 0x15, "Hardware Impending Failure Access Times Too High" },
1480 	{ 0x5D, 0x16, "Hardware Impending Failure Start Unit Times Too High" },
1481 	{ 0x5D, 0x17, "Hardware Impending Failure Channel Parametrics" },
1482 	{ 0x5D, 0x18, "Hardware Impending Failure Controller Detected" },
1483 	{ 0x5D, 0x19, "Hardware Impending Failure Throughput Performance" },
1484 	{ 0x5D, 0x1A, "Hardware Impending Failure Seek Time Performance" },
1485 	{ 0x5D, 0x1B, "Hardware Impending Failure Spin-Up Retry Count" },
1486 	{ 0x5D, 0x1C, "Hardware Impending Failure Drive Calibration Retry Count" },
1487 	{ 0x5D, 0x20, "Controller Impending Failure General Hard Drive Failure" },
1488 	{ 0x5D, 0x21, "Controller Impending Failure Drive Error Rate Too High" },
1489 	{ 0x5D, 0x22, "Controller Impending Failure Data Error Rate Too High" },
1490 	{ 0x5D, 0x23, "Controller Impending Failure Seek Error Rate Too High" },
1491 	{ 0x5D, 0x24, "Controller Impending Failure Too Many Block Reassigns" },
1492 	{ 0x5D, 0x25, "Controller Impending Failure Access Times Too High" },
1493 	{ 0x5D, 0x26, "Controller Impending Failure Start Unit Times Too High" },
1494 	{ 0x5D, 0x27, "Controller Impending Failure Channel Parametrics" },
1495 	{ 0x5D, 0x28, "Controller Impending Failure Controller Detected" },
1496 	{ 0x5D, 0x29, "Controller Impending Failure Throughput Performance" },
1497 	{ 0x5D, 0x2A, "Controller Impending Failure Seek Time Performance" },
1498 	{ 0x5D, 0x2B, "Controller Impending Failure Spin-Up Retry Count" },
1499 	{ 0x5D, 0x2C, "Controller Impending Failure Drive Calibration Retry Count" },
1500 	{ 0x5D, 0x30, "Data Channel Impending Failure General Hard Drive Failure" },
1501 	{ 0x5D, 0x31, "Data Channel Impending Failure Drive Error Rate Too High" },
1502 	{ 0x5D, 0x32, "Data Channel Impending Failure Data Error Rate Too High" },
1503 	{ 0x5D, 0x33, "Data Channel Impending Failure Seek Error Rate Too High" },
1504 	{ 0x5D, 0x34, "Data Channel Impending Failure Too Many Block Reassigns" },
1505 	{ 0x5D, 0x35, "Data Channel Impending Failure Access Times Too High" },
1506 	{ 0x5D, 0x36, "Data Channel Impending Failure Start Unit Times Too High" },
1507 	{ 0x5D, 0x37, "Data Channel Impending Failure Channel Parametrics" },
1508 	{ 0x5D, 0x38, "Data Channel Impending Failure Controller Detected" },
1509 	{ 0x5D, 0x39, "Data Channel Impending Failure Throughput Performance" },
1510 	{ 0x5D, 0x3A, "Data Channel Impending Failure Seek Time Performance" },
1511 	{ 0x5D, 0x3B, "Data Channel Impending Failure Spin-Up Retry Count" },
1512 	{ 0x5D, 0x3C, "Data Channel Impending Failure Drive Calibration Retry Count" },
1513 	{ 0x5D, 0x40, "Servo Impending Failure General Hard Drive Failure" },
1514 	{ 0x5D, 0x41, "Servo Impending Failure Drive Error Rate Too High" },
1515 	{ 0x5D, 0x42, "Servo Impending Failure Data Error Rate Too High" },
1516 	{ 0x5D, 0x43, "Servo Impending Failure Seek Error Rate Too High" },
1517 	{ 0x5D, 0x44, "Servo Impending Failure Too Many Block Reassigns" },
1518 	{ 0x5D, 0x45, "Servo Impending Failure Access Times Too High" },
1519 	{ 0x5D, 0x46, "Servo Impending Failure Start Unit Times Too High" },
1520 	{ 0x5D, 0x47, "Servo Impending Failure Channel Parametrics" },
1521 	{ 0x5D, 0x48, "Servo Impending Failure Controller Detected" },
1522 	{ 0x5D, 0x49, "Servo Impending Failure Throughput Performance" },
1523 	{ 0x5D, 0x4A, "Servo Impending Failure Seek Time Performance" },
1524 	{ 0x5D, 0x4B, "Servo Impending Failure Spin-Up Retry Count" },
1525 	{ 0x5D, 0x4C, "Servo Impending Failure Drive Calibration Retry Count" },
1526 	{ 0x5D, 0x50, "Spindle Impending Failure General Hard Drive Failure" },
1527 	{ 0x5D, 0x51, "Spindle Impending Failure Drive Error Rate Too High" },
1528 	{ 0x5D, 0x52, "Spindle Impending Failure Data Error Rate Too High" },
1529 	{ 0x5D, 0x53, "Spindle Impending Failure Seek Error Rate Too High" },
1530 	{ 0x5D, 0x54, "Spindle Impending Failure Too Many Block Reassigns" },
1531 	{ 0x5D, 0x55, "Spindle Impending Failure Access Times Too High" },
1532 	{ 0x5D, 0x56, "Spindle Impending Failure Start Unit Times Too High" },
1533 	{ 0x5D, 0x57, "Spindle Impending Failure Channel Parametrics" },
1534 	{ 0x5D, 0x58, "Spindle Impending Failure Controller Detected" },
1535 	{ 0x5D, 0x59, "Spindle Impending Failure Throughput Performance" },
1536 	{ 0x5D, 0x5A, "Spindle Impending Failure Seek Time Performance" },
1537 	{ 0x5D, 0x5B, "Spindle Impending Failure Spin-Up Retry Count" },
1538 	{ 0x5D, 0x5C, "Spindle Impending Failure Drive Calibration Retry Count" },
1539 	{ 0x5D, 0x60, "Firmware Impending Failure General Hard Drive Failure" },
1540 	{ 0x5D, 0x61, "Firmware Impending Failure Drive Error Rate Too High" },
1541 	{ 0x5D, 0x62, "Firmware Impending Failure Data Error Rate Too High" },
1542 	{ 0x5D, 0x63, "Firmware Impending Failure Seek Error Rate Too High" },
1543 	{ 0x5D, 0x64, "Firmware Impending Failure Too Many Block Reassigns" },
1544 	{ 0x5D, 0x65, "Firmware Impending Failure Access Times Too High" },
1545 	{ 0x5D, 0x66, "Firmware Impending Failure Start Unit Times Too High" },
1546 	{ 0x5D, 0x67, "Firmware Impending Failure Channel Parametrics" },
1547 	{ 0x5D, 0x68, "Firmware Impending Failure Controller Detected" },
1548 	{ 0x5D, 0x69, "Firmware Impending Failure Throughput Performance" },
1549 	{ 0x5D, 0x6A, "Firmware Impending Failure Seek Time Performance" },
1550 	{ 0x5D, 0x6B, "Firmware Impending Failure Spin-Up Retry Count" },
1551 	{ 0x5D, 0x6C, "Firmware Impending Failure Drive Calibration Retry Count" },
1552 	{ 0x5D, 0xFF, "Failure Prediction Threshold Exceeded (false)" },
1553 	{ 0x5E, 0x00, "Low Power Condition On" },
1554 	{ 0x5E, 0x01, "Idle Condition Activated By Timer" },
1555 	{ 0x5E, 0x02, "Standby Condition Activated By Timer" },
1556 	{ 0x5E, 0x03, "Idle Condition Activated By Command" },
1557 	{ 0x5E, 0x04, "Standby Condition Activated By Command" },
1558 	{ 0x5E, 0x41, "Power State Change To Active" },
1559 	{ 0x5E, 0x42, "Power State Change To Idle" },
1560 	{ 0x5E, 0x43, "Power State Change To Standby" },
1561 	{ 0x5E, 0x45, "Power State Change To Sleep" },
1562 	{ 0x5E, 0x47, "Power State Change To Device Control" },
1563 	{ 0x60, 0x00, "Lamp Failure" },
1564 	{ 0x61, 0x00, "Video Acquisition Error" },
1565 	{ 0x61, 0x01, "Unable To Acquire Video" },
1566 	{ 0x61, 0x02, "Out Of Focus" },
1567 	{ 0x62, 0x00, "Scan Head Positioning Error" },
1568 	{ 0x63, 0x00, "End Of User Area Encountered On This Track" },
1569 	{ 0x63, 0x01, "Packet Does Not Fit In Available Space" },
1570 	{ 0x64, 0x00, "Illegal Mode For This Track" },
1571 	{ 0x64, 0x01, "Invalid Packet Size" },
1572 	{ 0x65, 0x00, "Voltage Fault" },
1573 	{ 0x66, 0x00, "Automatic Document Feeder Cover Up" },
1574 	{ 0x66, 0x01, "Automatic Document Feeder Lift Up" },
1575 	{ 0x66, 0x02, "Document Jam In Automatic Document Feeder" },
1576 	{ 0x66, 0x03, "Document Miss Feed Automatic In Document Feeder" },
1577 	{ 0x67, 0x00, "Configuration Failure" },
1578 	{ 0x67, 0x01, "Configuration Of Incapable Logical Units Failed" },
1579 	{ 0x67, 0x02, "Add Logical Unit Failed" },
1580 	{ 0x67, 0x03, "Modification Of Logical Unit Failed" },
1581 	{ 0x67, 0x04, "Exchange Of Logical Unit Failed" },
1582 	{ 0x67, 0x05, "Remove Of Logical Unit Failed" },
1583 	{ 0x67, 0x06, "Attachment Of Logical Unit Failed" },
1584 	{ 0x67, 0x07, "Creation Of Logical Unit Failed" },
1585 	{ 0x67, 0x08, "Assign Failure Occurred" },
1586 	{ 0x67, 0x09, "Multiply Assigned Logical Unit" },
1587 	{ 0x67, 0x0A, "Set Target Port Groups Command Failed" },
1588 	{ 0x68, 0x00, "Logical Unit Not Configured" },
1589 	{ 0x69, 0x00, "Data Loss On Logical Unit" },
1590 	{ 0x69, 0x01, "Multiple Logical Unit Failures" },
1591 	{ 0x69, 0x02, "Parity/Data Mismatch" },
1592 	{ 0x6A, 0x00, "Informational, Refer To Log" },
1593 	{ 0x6B, 0x00, "State Change Has Occurred" },
1594 	{ 0x6B, 0x01, "Redundancy Level Got Better" },
1595 	{ 0x6B, 0x02, "Redundancy Level Got Worse" },
1596 	{ 0x6C, 0x00, "Rebuild Failure Occurred" },
1597 	{ 0x6D, 0x00, "Recalculate Failure Occurred" },
1598 	{ 0x6E, 0x00, "Command To Logical Unit Failed" },
1599 	{ 0x6F, 0x00, "Copy Protection Key Exchange Failure - Authentication Failure" },
1600 	{ 0x6F, 0x01, "Copy Protection Key Exchange Failure - Key Not Present" },
1601 	{ 0x6F, 0x02, "Copy Protection Key Exchange Failure - Key Not Established" },
1602 	{ 0x6F, 0x03, "Read Of Scrambled Sector Without Authentication" },
1603 	{ 0x6F, 0x04, "Media Region Code Is Mismatched To Logical Unit Region" },
1604 	{ 0x6F, 0x05, "Drive Region Must Be Permanent/Region Reset Count Error" },
1605 	/*
1606 	 * ASC 0x70 has an ASCQ range from 0x00 to 0xFF.
1607 	 * 0x70 0xNN DECOMPRESSION EXCEPTION SHORT ALGORITHM ID Of NN
1608 	 */
1609 	{ 0x71, 0x00, "Decompression Exception Long Algorithm ID" },
1610 	{ 0x72, 0x00, "Session Fixation Error" },
1611 	{ 0x72, 0x01, "Session Fixation Error Writing Lead-In" },
1612 	{ 0x72, 0x02, "Session Fixation Error Writing Lead-Out" },
1613 	{ 0x72, 0x03, "Session Fixation Error - Incomplete Track In Session" },
1614 	{ 0x72, 0x04, "Empty Or Partially Written Reserved Track" },
1615 	{ 0x72, 0x05, "No More Track Reservations Allowed" },
1616 	{ 0x73, 0x00, "CD Control Error" },
1617 	{ 0x73, 0x01, "Power Calibration Area Almost Full" },
1618 	{ 0x73, 0x02, "Power Calibration Area Is Full" },
1619 	{ 0x73, 0x03, "Power Calibration Area Error" },
1620 	{ 0x73, 0x04, "Program Memory Area Update Failure" },
1621 	{ 0x73, 0x05, "Program Memory Area Is Full" },
1622 	{ 0x73, 0x06, "RMA/PMA Is Almost Full" },
1623 	{ 0x00, 0x00, NULL }
1624 };
1625 
1626 static __inline void
asc2ascii(asc,ascq,result,len)1627 asc2ascii(asc, ascq, result, len)
1628 	u_int8_t asc, ascq;
1629 	char *result;
1630 	size_t len;
1631 {
1632 	int i;
1633 
1634 	/* Check for a dynamically built description. */
1635 	switch (asc) {
1636 	case 0x40:
1637 		if (ascq >= 0x80) {
1638 			snprintf(result, len,
1639 		            "Diagnostic Failure on Component 0x%02x", ascq);
1640 			return;
1641 		}
1642 		break;
1643 	case 0x4d:
1644 		snprintf(result, len,
1645 	 	    "Tagged Overlapped Commands (0x%02x = TASK TAG)", ascq);
1646 		return;
1647 	case 0x70:
1648 		snprintf(result, len,
1649 		    "Decompression Exception Short Algorithm ID OF 0x%02x",
1650 		    ascq);
1651 		return;
1652 	default:
1653 		break;
1654 	}
1655 
1656 	/* Check for a fixed description. */
1657 	for (i = 0; adesc[i].description != NULL; i++)
1658 		if (adesc[i].asc == asc && adesc[i].ascq == ascq) {
1659 			strlcpy(result, adesc[i].description, len);
1660 			return;
1661 		}
1662 
1663 	/* Just print out the ASC and ASCQ values as a description. */
1664 	snprintf(result, len, "ASC 0x%02x ASCQ 0x%02x", asc, ascq);
1665 }
1666 #endif /* SCSITERSE */
1667 
1668 void
scsi_print_sense(xs)1669 scsi_print_sense(xs)
1670 	struct scsi_xfer *xs;
1671 {
1672 	struct scsi_sense_data *sense = &xs->sense;
1673 	u_int8_t serr = sense->error_code & SSD_ERRCODE;
1674 	int32_t info;
1675 	char *sbs;
1676 
1677 	sc_print_addr(xs->sc_link);
1678 
1679 	/* XXX For error 0x71, current opcode is not the relevant one. */
1680 	printf("%sCheck Condition (error %#x) on opcode 0x%x\n",
1681 	    (serr == 0x71) ? "DEFERRED " : "", serr, xs->cmd->opcode);
1682 
1683 	if (serr != 0x70 && serr != 0x71) {
1684 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
1685 			struct scsi_sense_data_unextended *usense =
1686 			    (struct scsi_sense_data_unextended *)sense;
1687 			printf("   AT BLOCK #: %d (decimal)",
1688 			    _3btol(usense->block));
1689 		}
1690 		return;
1691 	}
1692 
1693 	printf("    SENSE KEY: %s\n", scsi_decode_sense(sense,
1694 	    DECODE_SENSE_KEY));
1695 
1696 	if (sense->flags & (SSD_FILEMARK | SSD_EOM | SSD_ILI)) {
1697 		char pad = ' ';
1698 
1699 		printf("             ");
1700 		if (sense->flags & SSD_FILEMARK) {
1701 			printf("%c Filemark Detected", pad);
1702 			pad = ',';
1703 		}
1704 		if (sense->flags & SSD_EOM) {
1705 			printf("%c EOM Detected", pad);
1706 			pad = ',';
1707 		}
1708 		if (sense->flags & SSD_ILI)
1709 			printf("%c Incorrect Length Indicator Set", pad);
1710 		printf("\n");
1711 	}
1712 
1713 	/*
1714 	 * It is inconvenient to use device type to figure out how to
1715 	 * format the info fields. So print them as 32 bit integers.
1716 	 */
1717 	info = _4btol(&sense->info[0]);
1718 	if (info)
1719 		printf("         INFO: 0x%x (VALID flag %s)\n", info,
1720 		    sense->error_code & SSD_ERRCODE_VALID ? "on" : "off");
1721 
1722 	if (sense->extra_len < 4)
1723 		return;
1724 
1725 	info = _4btol(&sense->cmd_spec_info[0]);
1726 	if (info)
1727 		printf(" COMMAND INFO: 0x%x\n", info);
1728 	sbs = scsi_decode_sense(sense, DECODE_ASC_ASCQ);
1729 	if (strlen(sbs) > 0)
1730 		printf("     ASC/ASCQ: %s\n", sbs);
1731 	if (sense->fru != 0)
1732 		printf("     FRU CODE: 0x%x\n", sense->fru);
1733 	sbs = scsi_decode_sense(sense, DECODE_SKSV);
1734 	if (strlen(sbs) > 0)
1735 		printf("         SKSV: %s\n", sbs);
1736 }
1737 
1738 char *
scsi_decode_sense(sense,flag)1739 scsi_decode_sense(sense, flag)
1740 	struct scsi_sense_data *sense;
1741 	int flag;
1742 {
1743 	static char rqsbuf[132];
1744 	u_int16_t count;
1745 	u_int8_t skey, spec_1;
1746 	int len;
1747 
1748 	bzero(rqsbuf, sizeof rqsbuf);
1749 
1750 	skey = sense->flags & SSD_KEY;
1751 	spec_1 = sense->sense_key_spec_1;
1752 	count = _2btol(&sense->sense_key_spec_2);
1753 
1754 	switch (flag) {
1755 	case DECODE_SENSE_KEY:
1756 		strlcpy(rqsbuf, sense_keys[skey], sizeof rqsbuf);
1757 		break;
1758 	case DECODE_ASC_ASCQ:
1759 		asc2ascii(sense->add_sense_code, sense->add_sense_code_qual,
1760 		    rqsbuf, sizeof rqsbuf);
1761 		break;
1762 	case DECODE_SKSV:
1763 		if (sense->extra_len < 9 || ((spec_1 & SSD_SCS_VALID) == 0))
1764 			break;
1765 		switch (skey) {
1766 		case SKEY_ILLEGAL_REQUEST:
1767 			len = snprintf(rqsbuf, sizeof rqsbuf,
1768 			    "Error in %s, Offset %d",
1769 			    (spec_1 & SSD_SCS_CDB_ERROR) ? "CDB" : "Parameters",
1770 			    count);
1771 			if ((len != -1 && len < sizeof rqsbuf) &&
1772 			    (spec_1 & SSD_SCS_VALID_BIT_INDEX))
1773 				snprintf(rqsbuf+len, sizeof rqsbuf - len,
1774 				    ", bit %d", spec_1 & SSD_SCS_BIT_INDEX);
1775 			break;
1776 		case SKEY_RECOVERED_ERROR:
1777 		case SKEY_MEDIUM_ERROR:
1778 		case SKEY_HARDWARE_ERROR:
1779 			snprintf(rqsbuf, sizeof rqsbuf,
1780 			    "Actual Retry Count: %d", count);
1781 			break;
1782 		case SKEY_NOT_READY:
1783 			snprintf(rqsbuf, sizeof rqsbuf,
1784 			    "Progress Indicator: %d", count);
1785 			break;
1786 		default:
1787 			break;
1788 		}
1789 		break;
1790 	default:
1791 		break;
1792 	}
1793 
1794 	return (rqsbuf);
1795 }
1796 
1797 #ifdef	SCSIDEBUG
1798 /*
1799  * Given a scsi_xfer, dump the request, in all it's glory
1800  */
1801 void
show_scsi_xs(xs)1802 show_scsi_xs(xs)
1803 	struct scsi_xfer *xs;
1804 {
1805 	printf("xs(%p): ", xs);
1806 	printf("flg(0x%x)", xs->flags);
1807 	printf("sc_link(%p)", xs->sc_link);
1808 	printf("retr(0x%x)", xs->retries);
1809 	printf("timo(0x%x)", xs->timeout);
1810 	printf("cmd(%p)", xs->cmd);
1811 	printf("len(0x%x)", xs->cmdlen);
1812 	printf("data(%p)", xs->data);
1813 	printf("len(0x%x)", xs->datalen);
1814 	printf("res(0x%lx)", xs->resid);
1815 	printf("err(0x%x)", xs->error);
1816 	printf("bp(%p)", xs->bp);
1817 	show_scsi_cmd(xs);
1818 }
1819 
1820 void
show_scsi_cmd(xs)1821 show_scsi_cmd(xs)
1822 	struct scsi_xfer *xs;
1823 {
1824 	u_char *b = (u_char *) xs->cmd;
1825 	int	i = 0;
1826 
1827 	sc_print_addr(xs->sc_link);
1828 	printf("command: ");
1829 
1830 	if ((xs->flags & SCSI_RESET) == 0) {
1831 		while (i < xs->cmdlen) {
1832 			if (i)
1833 				printf(",");
1834 			printf("%x", b[i++]);
1835 		}
1836 		printf("-[%d bytes]\n", xs->datalen);
1837 		if (xs->datalen)
1838 			show_mem(xs->data, min(64, xs->datalen));
1839 	} else
1840 		printf("-RESET-\n");
1841 }
1842 
1843 void
show_mem(address,num)1844 show_mem(address, num)
1845 	u_char *address;
1846 	int num;
1847 {
1848 	int x;
1849 
1850 	printf("------------------------------");
1851 	for (x = 0; x < num; x++) {
1852 		if ((x % 16) == 0)
1853 			printf("\n%03d: ", x);
1854 		printf("%02x ", *address++);
1855 	}
1856 	printf("\n------------------------------\n");
1857 }
1858 #endif /* SCSIDEBUG */
1859