xref: /openbsd/sys/scsi/scsi_base.c (revision 73471bf0)
1 /*	$OpenBSD: scsi_base.c,v 1.279 2021/05/13 02:22:33 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/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/uio.h>
42 #include <sys/errno.h>
43 #include <sys/device.h>
44 #include <sys/pool.h>
45 #include <sys/task.h>
46 
47 #include <scsi/scsi_all.h>
48 #include <scsi/scsi_debug.h>
49 #include <scsi/scsi_disk.h>
50 #include <scsi/scsiconf.h>
51 
52 static __inline void asc2ascii(u_int8_t, u_int8_t ascq, char *result,
53     size_t len);
54 int	scsi_xs_error(struct scsi_xfer *);
55 char   *scsi_decode_sense(struct scsi_sense_data *, int);
56 
57 void	scsi_xs_sync_done(struct scsi_xfer *);
58 
59 /* Values for flag parameter to scsi_decode_sense. */
60 #define	DECODE_SENSE_KEY	1
61 #define	DECODE_ASC_ASCQ		2
62 #define DECODE_SKSV		3
63 
64 struct pool		scsi_xfer_pool;
65 struct pool		scsi_plug_pool;
66 
67 struct scsi_plug {
68 	struct task		task;
69 	struct scsibus_softc	*sb;
70 	int			target;
71 	int			lun;
72 	int			how;
73 };
74 
75 void	scsi_plug_probe(void *);
76 void	scsi_plug_detach(void *);
77 
78 struct scsi_xfer *	scsi_xs_io(struct scsi_link *, void *, int);
79 
80 int			scsi_ioh_pending(struct scsi_iopool *);
81 struct scsi_iohandler *	scsi_ioh_deq(struct scsi_iopool *);
82 
83 void			scsi_xsh_runqueue(struct scsi_link *);
84 void			scsi_xsh_ioh(void *, void *);
85 
86 int			scsi_link_open(struct scsi_link *);
87 void			scsi_link_close(struct scsi_link *);
88 
89 void *			scsi_iopool_get(struct scsi_iopool *);
90 void			scsi_iopool_put(struct scsi_iopool *, void *);
91 
92 /* Various helper functions for scsi_do_mode_sense() */
93 int			scsi_mode_sense(struct scsi_link *, int,
94 			    union scsi_mode_sense_buf *, int);
95 int			scsi_mode_sense_big(struct scsi_link *, int,
96 			    union scsi_mode_sense_buf *, int);
97 void *			scsi_mode_sense_page(struct scsi_mode_header *, int,
98 			    int);
99 void *			scsi_mode_sense_big_page(struct scsi_mode_header_big *,
100 			    int, int);
101 
102 /* ioh/xsh queue state */
103 #define RUNQ_IDLE	0
104 #define RUNQ_LINKQ	1
105 #define RUNQ_POOLQ	2
106 
107 /* synchronous api for allocating an io. */
108 struct scsi_io_mover {
109 	struct mutex mtx;
110 	void *io;
111 	u_int done;
112 };
113 #define SCSI_IO_MOVER_INITIALIZER { MUTEX_INITIALIZER(IPL_BIO), NULL, 0 }
114 
115 void scsi_move(struct scsi_io_mover *);
116 void scsi_move_done(void *, void *);
117 
118 void scsi_io_get_done(void *, void *);
119 void scsi_xs_get_done(void *, void *);
120 
121 /*
122  * Called when a scsibus is attached to initialize global data.
123  */
124 void
125 scsi_init(void)
126 {
127 	static int scsi_init_done;
128 
129 	if (scsi_init_done)
130 		return;
131 	scsi_init_done = 1;
132 
133 #if defined(SCSI_DELAY) && SCSI_DELAY > 0
134 	/* Historical. Older buses may need a moment to stabilize. */
135 	delay(1000000 * SCSI_DELAY);
136 #endif /* SCSI_DELAY && SCSI_DELAY > 0 */
137 
138 	/* Initialize the scsi_xfer pool. */
139 	pool_init(&scsi_xfer_pool, sizeof(struct scsi_xfer), 0, IPL_BIO, 0,
140 	    "scxspl", NULL);
141 	pool_init(&scsi_plug_pool, sizeof(struct scsi_plug), 0, IPL_BIO, 0,
142 	    "scsiplug", NULL);
143 }
144 
145 int
146 scsi_req_probe(struct scsibus_softc *sb, int target, int lun)
147 {
148 	struct scsi_plug *p;
149 
150 	p = pool_get(&scsi_plug_pool, PR_NOWAIT);
151 	if (p == NULL)
152 		return ENOMEM;
153 
154 	task_set(&p->task, scsi_plug_probe, p);
155 	p->sb = sb;
156 	p->target = target;
157 	p->lun = lun;
158 
159 	task_add(systq, &p->task);
160 
161 	return 0;
162 }
163 
164 int
165 scsi_req_detach(struct scsibus_softc *sb, int target, int lun, int how)
166 {
167 	struct scsi_plug *p;
168 
169 	p = pool_get(&scsi_plug_pool, PR_NOWAIT);
170 	if (p == NULL)
171 		return ENOMEM;
172 
173 	task_set(&p->task, scsi_plug_detach, p);
174 	p->sb = sb;
175 	p->target = target;
176 	p->lun = lun;
177 	p->how = how;
178 
179 	task_add(systq, &p->task);
180 
181 	return 0;
182 }
183 
184 void
185 scsi_plug_probe(void *xp)
186 {
187 	struct scsi_plug	*p = xp;
188 	struct scsibus_softc	*sb = p->sb;
189 	int			 target = p->target, lun = p->lun;
190 
191 	pool_put(&scsi_plug_pool, p);
192 
193 	scsi_probe(sb, target, lun);
194 }
195 
196 void
197 scsi_plug_detach(void *xp)
198 {
199 	struct scsi_plug	*p = xp;
200 	struct scsibus_softc	*sb = p->sb;
201 	int			 target = p->target, lun = p->lun;
202 	int			 how = p->how;
203 
204 	pool_put(&scsi_plug_pool, p);
205 
206 	scsi_detach(sb, target, lun, how);
207 }
208 
209 int
210 scsi_pending_start(struct mutex *mtx, u_int *running)
211 {
212 	int rv = 1;
213 
214 	mtx_enter(mtx);
215 	(*running)++;
216 	if ((*running) > 1)
217 		rv = 0;
218 	mtx_leave(mtx);
219 
220 	return rv;
221 }
222 
223 int
224 scsi_pending_finish(struct mutex *mtx, u_int *running)
225 {
226 	int rv = 1;
227 
228 	mtx_enter(mtx);
229 	(*running)--;
230 	if ((*running) > 0) {
231 		(*running) = 1;
232 		rv = 0;
233 	}
234 	mtx_leave(mtx);
235 
236 	return rv;
237 }
238 
239 void
240 scsi_iopool_init(struct scsi_iopool *iopl, void *iocookie,
241     void *(*io_get)(void *), void (*io_put)(void *, void *))
242 {
243 	iopl->iocookie = iocookie;
244 	iopl->io_get = io_get;
245 	iopl->io_put = io_put;
246 
247 	TAILQ_INIT(&iopl->queue);
248 	iopl->running = 0;
249 	mtx_init(&iopl->mtx, IPL_BIO);
250 }
251 
252 void *
253 scsi_iopool_get(struct scsi_iopool *iopl)
254 {
255 	void *io;
256 
257 	KERNEL_LOCK();
258 	io = iopl->io_get(iopl->iocookie);
259 	KERNEL_UNLOCK();
260 
261 	return io;
262 }
263 
264 void
265 scsi_iopool_put(struct scsi_iopool *iopl, void *io)
266 {
267 	KERNEL_LOCK();
268 	iopl->io_put(iopl->iocookie, io);
269 	KERNEL_UNLOCK();
270 }
271 
272 void
273 scsi_iopool_destroy(struct scsi_iopool *iopl)
274 {
275 	struct scsi_runq	 sleepers = TAILQ_HEAD_INITIALIZER(sleepers);
276 	struct scsi_iohandler	*ioh = NULL;
277 
278 	mtx_enter(&iopl->mtx);
279 	while ((ioh = TAILQ_FIRST(&iopl->queue)) != NULL) {
280 		TAILQ_REMOVE(&iopl->queue, ioh, q_entry);
281 		ioh->q_state = RUNQ_IDLE;
282 
283 		if (ioh->handler == scsi_io_get_done)
284 			TAILQ_INSERT_TAIL(&sleepers, ioh, q_entry);
285 #ifdef DIAGNOSTIC
286 		else
287 			panic("scsi_iopool_destroy: scsi_iohandler on pool");
288 #endif /* DIAGNOSTIC */
289 	}
290 	mtx_leave(&iopl->mtx);
291 
292 	while ((ioh = TAILQ_FIRST(&sleepers)) != NULL) {
293 		TAILQ_REMOVE(&sleepers, ioh, q_entry);
294 		ioh->handler(ioh->cookie, NULL);
295 	}
296 }
297 
298 void *
299 scsi_default_get(void *iocookie)
300 {
301 	return SCSI_IOPOOL_POISON;
302 }
303 
304 void
305 scsi_default_put(void *iocookie, void *io)
306 {
307 #ifdef DIAGNOSTIC
308 	if (io != SCSI_IOPOOL_POISON)
309 		panic("unexpected opening returned");
310 #endif /* DIAGNOSTIC */
311 }
312 
313 /*
314  * public interface to the ioh api.
315  */
316 
317 void
318 scsi_ioh_set(struct scsi_iohandler *ioh, struct scsi_iopool *iopl,
319     void (*handler)(void *, void *), void *cookie)
320 {
321 	ioh->q_state = RUNQ_IDLE;
322 	ioh->pool = iopl;
323 	ioh->handler = handler;
324 	ioh->cookie = cookie;
325 }
326 
327 int
328 scsi_ioh_add(struct scsi_iohandler *ioh)
329 {
330 	struct scsi_iopool	*iopl = ioh->pool;
331 	int			 rv = 0;
332 
333 	mtx_enter(&iopl->mtx);
334 	switch (ioh->q_state) {
335 	case RUNQ_IDLE:
336 		TAILQ_INSERT_TAIL(&iopl->queue, ioh, q_entry);
337 		ioh->q_state = RUNQ_POOLQ;
338 		rv = 1;
339 		break;
340 #ifdef DIAGNOSTIC
341 	case RUNQ_POOLQ:
342 		break;
343 	default:
344 		panic("scsi_ioh_add: unexpected state %u", ioh->q_state);
345 #endif /* DIAGNOSTIC */
346 	}
347 	mtx_leave(&iopl->mtx);
348 
349 	/* lets get some io up in the air */
350 	scsi_iopool_run(iopl);
351 
352 	return rv;
353 }
354 
355 int
356 scsi_ioh_del(struct scsi_iohandler *ioh)
357 {
358 	struct scsi_iopool	*iopl = ioh->pool;
359 	int			 rv = 0;
360 
361 	mtx_enter(&iopl->mtx);
362 	switch (ioh->q_state) {
363 	case RUNQ_POOLQ:
364 		TAILQ_REMOVE(&iopl->queue, ioh, q_entry);
365 		ioh->q_state = RUNQ_IDLE;
366 		rv = 1;
367 		break;
368 #ifdef DIAGNOSTIC
369 	case RUNQ_IDLE:
370 		break;
371 	default:
372 		panic("scsi_ioh_del: unexpected state %u", ioh->q_state);
373 #endif /* DIAGNOSTIC */
374 	}
375 	mtx_leave(&iopl->mtx);
376 
377 	return rv;
378 }
379 
380 /*
381  * internal iopool runqueue handling.
382  */
383 
384 struct scsi_iohandler *
385 scsi_ioh_deq(struct scsi_iopool *iopl)
386 {
387 	struct scsi_iohandler *ioh = NULL;
388 
389 	mtx_enter(&iopl->mtx);
390 	ioh = TAILQ_FIRST(&iopl->queue);
391 	if (ioh != NULL) {
392 		TAILQ_REMOVE(&iopl->queue, ioh, q_entry);
393 		ioh->q_state = RUNQ_IDLE;
394 	}
395 	mtx_leave(&iopl->mtx);
396 
397 	return ioh;
398 }
399 
400 int
401 scsi_ioh_pending(struct scsi_iopool *iopl)
402 {
403 	int rv;
404 
405 	mtx_enter(&iopl->mtx);
406 	rv = !TAILQ_EMPTY(&iopl->queue);
407 	mtx_leave(&iopl->mtx);
408 
409 	return rv;
410 }
411 
412 void
413 scsi_iopool_run(struct scsi_iopool *iopl)
414 {
415 	struct scsi_iohandler	*ioh;
416 	void			*io;
417 
418 	if (!scsi_pending_start(&iopl->mtx, &iopl->running))
419 		return;
420 	do {
421 		while (scsi_ioh_pending(iopl)) {
422 			io = scsi_iopool_get(iopl);
423 			if (io == NULL)
424 				break;
425 
426 			ioh = scsi_ioh_deq(iopl);
427 			if (ioh == NULL) {
428 				scsi_iopool_put(iopl, io);
429 				break;
430 			}
431 
432 			ioh->handler(ioh->cookie, io);
433 		}
434 	} while (!scsi_pending_finish(&iopl->mtx, &iopl->running));
435 }
436 
437 /*
438  * move an io from a runq to a proc thats waiting for an io.
439  */
440 
441 void
442 scsi_move(struct scsi_io_mover *m)
443 {
444 	mtx_enter(&m->mtx);
445 	while (!m->done)
446 		msleep_nsec(m, &m->mtx, PRIBIO, "scsiiomv", INFSLP);
447 	mtx_leave(&m->mtx);
448 }
449 
450 void
451 scsi_move_done(void *cookie, void *io)
452 {
453 	struct scsi_io_mover *m = cookie;
454 
455 	mtx_enter(&m->mtx);
456 	m->io = io;
457 	m->done = 1;
458 	wakeup_one(m);
459 	mtx_leave(&m->mtx);
460 }
461 
462 /*
463  * synchronous api for allocating an io.
464  */
465 
466 void *
467 scsi_io_get(struct scsi_iopool *iopl, int flags)
468 {
469 	struct scsi_io_mover	 m = SCSI_IO_MOVER_INITIALIZER;
470 	struct scsi_iohandler	 ioh;
471 	void			*io;
472 
473 	/* try and sneak an io off the backend immediately */
474 	io = scsi_iopool_get(iopl);
475 	if (io != NULL)
476 		return io;
477 	else if (ISSET(flags, SCSI_NOSLEEP))
478 		return NULL;
479 
480 	/* otherwise sleep until we get one */
481 	scsi_ioh_set(&ioh, iopl, scsi_io_get_done, &m);
482 	scsi_ioh_add(&ioh);
483 	scsi_move(&m);
484 
485 	return m.io;
486 }
487 
488 void
489 scsi_io_get_done(void *cookie, void *io)
490 {
491 	scsi_move_done(cookie, io);
492 }
493 
494 void
495 scsi_io_put(struct scsi_iopool *iopl, void *io)
496 {
497 	scsi_iopool_put(iopl, io);
498 	scsi_iopool_run(iopl);
499 }
500 
501 /*
502  * public interface to the xsh api.
503  */
504 
505 void
506 scsi_xsh_set(struct scsi_xshandler *xsh, struct scsi_link *link,
507     void (*handler)(struct scsi_xfer *))
508 {
509 	scsi_ioh_set(&xsh->ioh, link->pool, scsi_xsh_ioh, xsh);
510 
511 	xsh->link = link;
512 	xsh->handler = handler;
513 }
514 
515 int
516 scsi_xsh_add(struct scsi_xshandler *xsh)
517 {
518 	struct scsi_link	*link = xsh->link;
519 	int			 rv = 0;
520 
521 	if (ISSET(link->state, SDEV_S_DYING))
522 		return 0;
523 
524 	mtx_enter(&link->pool->mtx);
525 	if (xsh->ioh.q_state == RUNQ_IDLE) {
526 		TAILQ_INSERT_TAIL(&link->queue, &xsh->ioh, q_entry);
527 		xsh->ioh.q_state = RUNQ_LINKQ;
528 		rv = 1;
529 	}
530 	mtx_leave(&link->pool->mtx);
531 
532 	/* lets get some io up in the air */
533 	scsi_xsh_runqueue(link);
534 
535 	return rv;
536 }
537 
538 int
539 scsi_xsh_del(struct scsi_xshandler *xsh)
540 {
541 	struct scsi_link	*link = xsh->link;
542 	int			 rv = 1;
543 
544 	mtx_enter(&link->pool->mtx);
545 	switch (xsh->ioh.q_state) {
546 	case RUNQ_IDLE:
547 		rv = 0;
548 		break;
549 	case RUNQ_LINKQ:
550 		TAILQ_REMOVE(&link->queue, &xsh->ioh, q_entry);
551 		break;
552 	case RUNQ_POOLQ:
553 		TAILQ_REMOVE(&link->pool->queue, &xsh->ioh, q_entry);
554 		link->pending--;
555 		if (ISSET(link->state, SDEV_S_DYING) && link->pending == 0)
556 			wakeup_one(&link->pending);
557 		break;
558 	default:
559 		panic("unexpected xsh state %u", xsh->ioh.q_state);
560 	}
561 	xsh->ioh.q_state = RUNQ_IDLE;
562 	mtx_leave(&link->pool->mtx);
563 
564 	return rv;
565 }
566 
567 /*
568  * internal xs runqueue handling.
569  */
570 
571 void
572 scsi_xsh_runqueue(struct scsi_link *link)
573 {
574 	struct scsi_iohandler	*ioh;
575 	int			 runq;
576 
577 	if (!scsi_pending_start(&link->pool->mtx, &link->running))
578 		return;
579 	do {
580 		runq = 0;
581 
582 		mtx_enter(&link->pool->mtx);
583 		while (!ISSET(link->state, SDEV_S_DYING) &&
584 		    link->pending < link->openings &&
585 		    ((ioh = TAILQ_FIRST(&link->queue)) != NULL)) {
586 			link->pending++;
587 
588 			TAILQ_REMOVE(&link->queue, ioh, q_entry);
589 			TAILQ_INSERT_TAIL(&link->pool->queue, ioh, q_entry);
590 			ioh->q_state = RUNQ_POOLQ;
591 
592 			runq = 1;
593 		}
594 		mtx_leave(&link->pool->mtx);
595 
596 		if (runq)
597 			scsi_iopool_run(link->pool);
598 	} while (!scsi_pending_finish(&link->pool->mtx, &link->running));
599 }
600 
601 void
602 scsi_xsh_ioh(void *cookie, void *io)
603 {
604 	struct scsi_xshandler	*xsh = cookie;
605 	struct scsi_xfer	*xs;
606 
607 	xs = scsi_xs_io(xsh->link, io, SCSI_NOSLEEP);
608 	if (xs == NULL) {
609 		/*
610 		 * in this situation we should queue things waiting for an
611 		 * xs and then give them xses when they were supposed be to
612 		 * returned to the pool.
613 		 */
614 
615 		printf("scsi_xfer pool exhausted!\n");
616 		scsi_xsh_add(xsh);
617 		return;
618 	}
619 
620 	xsh->handler(xs);
621 }
622 
623 /*
624  * Get a scsi transfer structure for the caller.
625  * Go to the iopool backend for an "opening" and then attach an xs to it.
626  */
627 
628 struct scsi_xfer *
629 scsi_xs_get(struct scsi_link *link, int flags)
630 {
631 	struct scsi_xshandler	 xsh;
632 	struct scsi_io_mover	 m = SCSI_IO_MOVER_INITIALIZER;
633 	struct scsi_iopool	*iopl = link->pool;
634 	void			*io;
635 
636 	if (ISSET(link->state, SDEV_S_DYING))
637 		return NULL;
638 
639 	/* really custom xs handler to avoid scsi_xsh_ioh */
640 	scsi_ioh_set(&xsh.ioh, iopl, scsi_xs_get_done, &m);
641 	xsh.link = link;
642 
643 	if (!scsi_link_open(link)) {
644 		if (ISSET(flags, SCSI_NOSLEEP))
645 			return NULL;
646 
647 		scsi_xsh_add(&xsh);
648 		scsi_move(&m);
649 		if (m.io == NULL)
650 			return NULL;
651 
652 		io = m.io;
653 	} else if ((io = scsi_iopool_get(iopl)) == NULL) {
654 		if (ISSET(flags, SCSI_NOSLEEP)) {
655 			scsi_link_close(link);
656 			return NULL;
657 		}
658 
659 		scsi_ioh_add(&xsh.ioh);
660 		scsi_move(&m);
661 		if (m.io == NULL)
662 			return NULL;
663 
664 		io = m.io;
665 	}
666 
667 	return scsi_xs_io(link, io, flags);
668 }
669 
670 void
671 scsi_xs_get_done(void *cookie, void *io)
672 {
673 	scsi_move_done(cookie, io);
674 }
675 
676 void
677 scsi_link_shutdown(struct scsi_link *link)
678 {
679 	struct scsi_runq	 sleepers = TAILQ_HEAD_INITIALIZER(sleepers);
680 	struct scsi_iopool	*iopl = link->pool;
681 	struct scsi_iohandler	*ioh;
682 	struct scsi_xshandler	*xsh;
683 
684 	mtx_enter(&iopl->mtx);
685 	while ((ioh = TAILQ_FIRST(&link->queue)) != NULL) {
686 		TAILQ_REMOVE(&link->queue, ioh, q_entry);
687 		ioh->q_state = RUNQ_IDLE;
688 
689 		if (ioh->handler == scsi_xs_get_done)
690 			TAILQ_INSERT_TAIL(&sleepers, ioh, q_entry);
691 #ifdef DIAGNOSTIC
692 		else
693 			panic("scsi_link_shutdown: scsi_xshandler on link");
694 #endif /* DIAGNOSTIC */
695 	}
696 
697 	ioh = TAILQ_FIRST(&iopl->queue);
698 	while (ioh != NULL) {
699 		xsh = (struct scsi_xshandler *)ioh;
700 		ioh = TAILQ_NEXT(ioh, q_entry);
701 
702 #ifdef DIAGNOSTIC
703 		if (xsh->ioh.handler == scsi_xsh_ioh &&
704 		    xsh->link == link)
705 			panic("scsi_link_shutdown: scsi_xshandler on pool");
706 #endif /* DIAGNOSTIC */
707 
708 		if (xsh->ioh.handler == scsi_xs_get_done &&
709 		    xsh->link == link) {
710 			TAILQ_REMOVE(&iopl->queue, &xsh->ioh, q_entry);
711 			xsh->ioh.q_state = RUNQ_IDLE;
712 			link->pending--;
713 
714 			TAILQ_INSERT_TAIL(&sleepers, &xsh->ioh, q_entry);
715 		}
716 	}
717 
718 	while (link->pending > 0)
719 		msleep_nsec(&link->pending, &iopl->mtx, PRIBIO, "pendxs",
720 		    INFSLP);
721 	mtx_leave(&iopl->mtx);
722 
723 	while ((ioh = TAILQ_FIRST(&sleepers)) != NULL) {
724 		TAILQ_REMOVE(&sleepers, ioh, q_entry);
725 		ioh->handler(ioh->cookie, NULL);
726 	}
727 }
728 
729 int
730 scsi_link_open(struct scsi_link *link)
731 {
732 	int open = 0;
733 
734 	mtx_enter(&link->pool->mtx);
735 	if (link->pending < link->openings) {
736 		link->pending++;
737 		open = 1;
738 	}
739 	mtx_leave(&link->pool->mtx);
740 
741 	return open;
742 }
743 
744 void
745 scsi_link_close(struct scsi_link *link)
746 {
747 	mtx_enter(&link->pool->mtx);
748 	link->pending--;
749 	if (ISSET(link->state, SDEV_S_DYING) && link->pending == 0)
750 		wakeup_one(&link->pending);
751 	mtx_leave(&link->pool->mtx);
752 
753 	scsi_xsh_runqueue(link);
754 }
755 
756 struct scsi_xfer *
757 scsi_xs_io(struct scsi_link *link, void *io, int flags)
758 {
759 	struct scsi_xfer *xs;
760 
761 	xs = pool_get(&scsi_xfer_pool, PR_ZERO |
762 	    (ISSET(flags, SCSI_NOSLEEP) ? PR_NOWAIT : PR_WAITOK));
763 	if (xs == NULL) {
764 		scsi_io_put(link->pool, io);
765 		scsi_link_close(link);
766 	} else {
767 		xs->flags = flags;
768 		xs->sc_link = link;
769 		xs->retries = SCSI_RETRIES;
770 		xs->timeout = 10000;
771 		xs->io = io;
772 	}
773 
774 	return xs;
775 }
776 
777 void
778 scsi_xs_put(struct scsi_xfer *xs)
779 {
780 	struct scsi_link	*link = xs->sc_link;
781 	void			*io = xs->io;
782 
783 	pool_put(&scsi_xfer_pool, xs);
784 
785 	scsi_io_put(link->pool, io);
786 	scsi_link_close(link);
787 }
788 
789 /*
790  * Get scsi driver to send a "are you ready?" command
791  */
792 int
793 scsi_test_unit_ready(struct scsi_link *link, int retries, int flags)
794 {
795 	struct scsi_test_unit_ready	*cmd;
796 	struct scsi_xfer		*xs;
797 	int				 error;
798 
799 	xs = scsi_xs_get(link, flags);
800 	if (xs == NULL)
801 		return ENOMEM;
802 	xs->cmdlen = sizeof(*cmd);
803 	xs->retries = retries;
804 	xs->timeout = 10000;
805 
806 	cmd = (struct scsi_test_unit_ready *)&xs->cmd;
807 	cmd->opcode = TEST_UNIT_READY;
808 
809 	error = scsi_xs_sync(xs);
810 	scsi_xs_put(xs);
811 
812 	return error;
813 }
814 
815 void
816 scsi_init_inquiry(struct scsi_xfer *xs, u_int8_t flags, u_int8_t pagecode,
817     void *data, size_t len)
818 {
819 	struct scsi_inquiry *cmd;
820 
821 	cmd = (struct scsi_inquiry *)&xs->cmd;
822 	cmd->opcode = INQUIRY;
823 	cmd->flags = flags;
824 	cmd->pagecode = pagecode;
825 	_lto2b(len, cmd->length);
826 
827 	xs->cmdlen = sizeof(*cmd);
828 
829 	SET(xs->flags, SCSI_DATA_IN);
830 	xs->data = data;
831 	xs->datalen = len;
832 }
833 
834 /*
835  * Do a scsi operation asking a device what it is.
836  * Use the scsi_cmd routine in the switch table.
837  */
838 int
839 scsi_inquire(struct scsi_link *link, struct scsi_inquiry_data *inqbuf,
840     int flags)
841 {
842 	struct scsi_xfer	*xs;
843 	size_t			 bytes;
844 	int			 avail, retries, error, received;
845 
846 	/*
847 	 * Start by asking for only the basic 36 bytes of SCSI2 inquiry
848 	 * information. This avoids problems with devices that choke trying to
849 	 * supply more.
850 	 */
851 	bytes = SID_SCSI2_HDRLEN + SID_SCSI2_ALEN;
852 	retries = 0;
853 
854 again:
855 	xs = scsi_xs_get(link, flags);
856 	if (xs == NULL)
857 		return EBUSY;
858 
859 	if (bytes > sizeof(*inqbuf))
860 		bytes = sizeof(*inqbuf);
861 	scsi_init_inquiry(xs, 0, 0, inqbuf, bytes);
862 
863 	error = scsi_xs_sync(xs);
864 	received = xs->datalen - xs->resid;
865 	scsi_xs_put(xs);
866 
867 	if (error != 0)
868 		return error;
869 	if (received < SID_SCSI2_HDRLEN)
870 		return EINVAL;
871 
872 	avail = SID_SCSI2_HDRLEN + inqbuf->additional_length;
873 
874 	if (received < avail && retries == 0) {
875 		retries++;
876 		bytes = avail;
877 		goto again;
878 	}
879 
880 #ifdef SCSIDEBUG
881 	sc_print_addr(link);
882 	printf("got %d of %d bytes of inquiry data:\n", received,
883 	    avail);
884 	scsi_show_mem((u_char *)inqbuf, received);
885 #endif /* SCSIDEBUG */
886 
887 	if (avail > received)
888 		inqbuf->additional_length = received - SID_SCSI2_HDRLEN;
889 
890 	return 0;
891 }
892 
893 /*
894  * Query a VPD inquiry page
895  */
896 int
897 scsi_inquire_vpd(struct scsi_link *link, void *buf, u_int buflen,
898     u_int8_t page, int flags)
899 {
900 	struct scsi_xfer	*xs;
901 	int			 error;
902 #ifdef SCSIDEBUG
903 	u_int32_t		 bytes;
904 #endif /* SCSIDEBUG */
905 
906 	if (ISSET(link->flags, SDEV_UMASS))
907 		return EJUSTRETURN;
908 
909 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
910 	if (xs == NULL)
911 		return ENOMEM;
912 
913 	xs->retries = 2;
914 	xs->timeout = 10000;
915 
916 	scsi_init_inquiry(xs, SI_EVPD, page, buf, buflen);
917 
918 	error = scsi_xs_sync(xs);
919 
920 	scsi_xs_put(xs);
921 #ifdef SCSIDEBUG
922 	sc_print_addr(link);
923 	if (error == 0) {
924 		bytes = sizeof(struct scsi_vpd_hdr) +
925 		    _2btol(((struct scsi_vpd_hdr *)buf)->page_length);
926 		if (bytes < buflen)
927 			buflen = bytes;
928 		printf("got %u of %u bytes of VPD inquiry page %u data:\n",
929 		    buflen, bytes, page);
930 		scsi_show_mem(buf, buflen);
931 	} else {
932 		printf("VPD inquiry page %u not available\n", page);
933 	}
934 #endif /* SCSIDEBUG */
935 	return error;
936 }
937 
938 int
939 scsi_read_cap_10(struct scsi_link *link, struct scsi_read_cap_data *rdcap,
940     int flags)
941 {
942 	struct scsi_read_capacity	  cdb;
943 	struct scsi_xfer		 *xs;
944 	int				  rv;
945 
946 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
947 	if (xs == NULL)
948 		return ENOMEM;
949 
950 	memset(&cdb, 0, sizeof(cdb));
951 	cdb.opcode = READ_CAPACITY;
952 
953 	memcpy(&xs->cmd, &cdb, sizeof(cdb));
954 	xs->cmdlen = sizeof(cdb);
955 	xs->data = (void *)rdcap;
956 	xs->datalen = sizeof(*rdcap);
957 	xs->timeout = 20000;
958 
959 	rv = scsi_xs_sync(xs);
960 	scsi_xs_put(xs);
961 
962 #ifdef SCSIDEBUG
963 	if (rv == 0) {
964 		sc_print_addr(link);
965 		printf("read capacity 10 data:\n");
966 		scsi_show_mem((u_char *)rdcap, sizeof(*rdcap));
967 	}
968 #endif /* SCSIDEBUG */
969 
970 	return rv;
971 }
972 
973 int
974 scsi_read_cap_16(struct scsi_link *link, struct scsi_read_cap_data_16 *rdcap,
975     int flags)
976 {
977 	struct scsi_read_capacity_16	 cdb;
978 	struct scsi_xfer		*xs;
979 	int				 rv;
980 
981 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
982 	if (xs == NULL)
983 		return ENOMEM;
984 
985 	memset(&cdb, 0, sizeof(cdb));
986 	cdb.opcode = READ_CAPACITY_16;
987 	cdb.byte2 = SRC16_SERVICE_ACTION;
988 	_lto4b(sizeof(*rdcap), cdb.length);
989 
990 	memcpy(&xs->cmd, &cdb, sizeof(cdb));
991 	xs->cmdlen = sizeof(cdb);
992 	xs->data = (void *)rdcap;
993 	xs->datalen = sizeof(*rdcap);
994 	xs->timeout = 20000;
995 
996 	rv = scsi_xs_sync(xs);
997 	scsi_xs_put(xs);
998 
999 #ifdef SCSIDEBUG
1000 	if (rv == 0) {
1001 		sc_print_addr(link);
1002 		printf("read capacity 16 data:\n");
1003 		scsi_show_mem((u_char *)rdcap, sizeof(*rdcap));
1004 	}
1005 #endif /* SCSIDEBUG */
1006 
1007 	return rv;
1008 }
1009 
1010 /*
1011  * Prevent or allow the user to remove the media
1012  */
1013 int
1014 scsi_prevent(struct scsi_link *link, int type, int flags)
1015 {
1016 	struct scsi_prevent	*cmd;
1017 	struct scsi_xfer	*xs;
1018 	int			 error;
1019 
1020 	if (ISSET(link->quirks, ADEV_NODOORLOCK))
1021 		return 0;
1022 
1023 	xs = scsi_xs_get(link, flags);
1024 	if (xs == NULL)
1025 		return ENOMEM;
1026 	xs->cmdlen = sizeof(*cmd);
1027 	xs->retries = 2;
1028 	xs->timeout = 5000;
1029 
1030 	cmd = (struct scsi_prevent *)&xs->cmd;
1031 	cmd->opcode = PREVENT_ALLOW;
1032 	cmd->how = type;
1033 
1034 	error = scsi_xs_sync(xs);
1035 	scsi_xs_put(xs);
1036 
1037 	return error;
1038 }
1039 
1040 /*
1041  * Get scsi driver to send a "start up" command
1042  */
1043 int
1044 scsi_start(struct scsi_link *link, int type, int flags)
1045 {
1046 	struct scsi_start_stop	*cmd;
1047 	struct scsi_xfer	*xs;
1048 	int			 error;
1049 
1050 	xs = scsi_xs_get(link, flags);
1051 	if (xs == NULL)
1052 		return ENOMEM;
1053 	xs->cmdlen = sizeof(*cmd);
1054 	xs->retries = 2;
1055 	xs->timeout = (type == SSS_START) ? 30000 : 10000;
1056 
1057 	cmd = (struct scsi_start_stop *)&xs->cmd;
1058 	cmd->opcode = START_STOP;
1059 	cmd->how = type;
1060 
1061 	error = scsi_xs_sync(xs);
1062 	scsi_xs_put(xs);
1063 
1064 	return error;
1065 }
1066 
1067 int
1068 scsi_mode_sense(struct scsi_link *link, int pg_code,
1069     union scsi_mode_sense_buf *data, int flags)
1070 {
1071 	struct scsi_mode_sense	*cmd;
1072 	struct scsi_xfer	*xs;
1073 	size_t			 len;
1074 	int			 error;
1075 #ifdef SCSIDEBUG
1076 	size_t			 bytes;
1077 #endif /* SCSIDEBUG */
1078 
1079 	len = sizeof(*data);
1080 
1081 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN);
1082 	if (xs == NULL)
1083 		return ENOMEM;
1084 	xs->cmdlen = sizeof(*cmd);
1085 	xs->data = (void *)data;
1086 	xs->datalen = len;
1087 	xs->timeout = 20000;
1088 
1089 	/*
1090 	 * Make sure the sense buffer is clean before we do the mode sense, so
1091 	 * that checks for bogus values of 0 will work in case the mode sense
1092 	 * fails.
1093 	 */
1094 	memset(data, 0, len);
1095 
1096 	cmd = (struct scsi_mode_sense *)&xs->cmd;
1097 	cmd->opcode = MODE_SENSE;
1098 	cmd->page = pg_code;
1099 
1100 	if (len > 0xff)
1101 		len = 0xff;
1102 	cmd->length = len;
1103 
1104 	error = scsi_xs_sync(xs);
1105 	scsi_xs_put(xs);
1106 
1107 	if (error == 0 && !VALID_MODE_HDR(&data->hdr))
1108 		error = EIO;
1109 
1110 #ifdef SCSIDEBUG
1111 	sc_print_addr(link);
1112 	if (error == 0) {
1113 		bytes = sizeof(data->hdr.data_length) + data->hdr.data_length;
1114 		if (bytes < len)
1115 			len = bytes;
1116 		printf("got %zu of %zu bytes of mode sense (6) page %d data:\n",
1117 		    len, bytes, pg_code);
1118 		scsi_show_mem((u_char *)data, len);
1119 	} else
1120 		printf("mode sense (6) page %d not available\n", pg_code);
1121 #endif /* SCSIDEBUG */
1122 
1123 	return error;
1124 }
1125 
1126 int
1127 scsi_mode_sense_big(struct scsi_link *link, int pg_code,
1128     union scsi_mode_sense_buf *data, int flags)
1129 {
1130 	struct scsi_mode_sense_big	*cmd;
1131 	struct scsi_xfer		*xs;
1132 	size_t				 len;
1133 	int				 error;
1134 #ifdef SCSIDEBUG
1135 	size_t				 bytes;
1136 #endif /* SCSIDEBUG */
1137 
1138 	len = sizeof(*data);
1139 
1140 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN);
1141 	if (xs == NULL)
1142 		return ENOMEM;
1143 	xs->cmdlen = sizeof(*cmd);
1144 	xs->data = (void *)data;
1145 	xs->datalen = len;
1146 	xs->timeout = 20000;
1147 
1148 	/*
1149 	 * Make sure the sense buffer is clean before we do the mode sense, so
1150 	 * that checks for bogus values of 0 will work in case the mode sense
1151 	 * fails.
1152 	 */
1153 	memset(data, 0, len);
1154 
1155 	cmd = (struct scsi_mode_sense_big *)&xs->cmd;
1156 	cmd->opcode = MODE_SENSE_BIG;
1157 	cmd->page = pg_code;
1158 
1159 	if (len > 0xffff)
1160 		len = 0xffff;
1161 	_lto2b(len, cmd->length);
1162 
1163 	error = scsi_xs_sync(xs);
1164 	scsi_xs_put(xs);
1165 
1166 	if (error == 0 && !VALID_MODE_HDR_BIG(&data->hdr_big))
1167 		error = EIO;
1168 
1169 #ifdef SCSIDEBUG
1170 	sc_print_addr(link);
1171 	if (error == 0) {
1172 		bytes = sizeof(data->hdr_big.data_length) +
1173 		    _2btol(data->hdr_big.data_length);
1174 		if (bytes < len)
1175 			len = bytes;
1176 		printf("got %zu bytes of %zu bytes of mode sense (10) page %d "
1177 		    "data:\n", len, bytes, pg_code);
1178 		scsi_show_mem((u_char *)data, len);
1179 	} else
1180 		printf("mode sense (10) page %d not available\n", pg_code);
1181 #endif /* SCSIDEBUG */
1182 
1183 	return error;
1184 }
1185 
1186 void *
1187 scsi_mode_sense_page(struct scsi_mode_header *hdr, int pg_code, int pg_length)
1188 {
1189 	u_int8_t	*page;
1190 	int		 total_length, header_length;
1191 
1192 	total_length = hdr->data_length + sizeof(hdr->data_length);
1193 	header_length = sizeof(*hdr) + hdr->blk_desc_len;
1194 	page = (u_int8_t *)hdr + header_length;
1195 
1196 	if ((total_length - header_length) < pg_length)
1197 		return NULL;
1198 
1199 	if ((*page & SMS_PAGE_CODE) != pg_code)
1200 		return NULL;
1201 
1202 	return page;
1203 }
1204 
1205 void *
1206 scsi_mode_sense_big_page(struct scsi_mode_header_big *hdr, int pg_code,
1207     int pg_length)
1208 {
1209 	u_int8_t	*page;
1210 	int		 total_length, header_length;
1211 
1212 	total_length = _2btol(hdr->data_length) + sizeof(hdr->data_length);
1213 	header_length = sizeof(*hdr) + _2btol(hdr->blk_desc_len);
1214 	page = (u_int8_t *)hdr + header_length;
1215 
1216 	if ((total_length - header_length) < pg_length)
1217 		return NULL;
1218 
1219 	if ((*page & SMS_PAGE_CODE) != pg_code)
1220 		return NULL;
1221 
1222 	return page;
1223 }
1224 
1225 void
1226 scsi_parse_blkdesc(struct scsi_link *link, union scsi_mode_sense_buf *buf,
1227     int big, u_int32_t *density, u_int64_t *block_count, u_int32_t *block_size)
1228 {
1229 	struct scsi_direct_blk_desc	*direct;
1230 	struct scsi_blk_desc		*general;
1231 	size_t				 offset;
1232 	unsigned int			 blk_desc_len;
1233 
1234 	if (big == 0) {
1235 		offset = sizeof(struct scsi_mode_header);
1236 		blk_desc_len = buf->hdr.blk_desc_len;
1237 	} else {
1238 		offset = sizeof(struct scsi_mode_header_big);
1239 		blk_desc_len = _2btol(buf->hdr_big.blk_desc_len);
1240 	}
1241 
1242 	/* Both scsi_blk_desc and scsi_direct_blk_desc are 8 bytes. */
1243 	if (blk_desc_len == 0 || (blk_desc_len % 8 != 0))
1244 		return;
1245 
1246 	switch (link->inqdata.device & SID_TYPE) {
1247 	case T_SEQUENTIAL:
1248 		/*
1249 		 * XXX What other device types return general block descriptors?
1250 		 */
1251 		general = (struct scsi_blk_desc *)&buf->buf[offset];
1252 		if (density != NULL)
1253 			*density = general->density;
1254 		if (block_size != NULL)
1255 			*block_size = _3btol(general->blklen);
1256 		if (block_count != NULL)
1257 			*block_count = (u_int64_t)_3btol(general->nblocks);
1258 		break;
1259 
1260 	default:
1261 		direct = (struct scsi_direct_blk_desc *)&buf->buf[offset];
1262 		if (density != NULL)
1263 			*density = direct->density;
1264 		if (block_size != NULL)
1265 			*block_size = _3btol(direct->blklen);
1266 		if (block_count != NULL)
1267 			*block_count = (u_int64_t)_4btol(direct->nblocks);
1268 		break;
1269 	}
1270 }
1271 
1272 int
1273 scsi_do_mode_sense(struct scsi_link *link, int pg_code,
1274     union scsi_mode_sense_buf *buf, void **page_data,
1275     int pg_length, int flags, int *big)
1276 {
1277 	int error = 0;
1278 
1279 	*page_data = NULL;
1280 	*big = 0;
1281 
1282 	if (!ISSET(link->flags, SDEV_ATAPI) ||
1283 	    (link->inqdata.device & SID_TYPE) == T_SEQUENTIAL) {
1284 		/*
1285 		 * Try 6 byte mode sense request first. Some devices don't
1286 		 * distinguish between 6 and 10 byte MODE SENSE commands,
1287 		 * returning 6 byte data for 10 byte requests. ATAPI tape
1288 		 * drives use MODE SENSE (6) even though ATAPI uses 10 byte
1289 		 * everything else. Don't bother with SMS_DBD. Check returned
1290 		 * data length to ensure that at least a header (3 additional
1291 		 * bytes) is returned.
1292 		 */
1293 		error = scsi_mode_sense(link, pg_code, buf, flags);
1294 		if (error == 0) {
1295 			/*
1296 			 * Page data may be invalid (e.g. all zeros) but we
1297 			 * accept the device's word that this is the best it can
1298 			 * do. Some devices will freak out if their word is not
1299 			 * accepted and MODE_SENSE_BIG is attempted.
1300 			 */
1301 			*page_data = scsi_mode_sense_page(&buf->hdr, pg_code,
1302 			    pg_length);
1303 			return 0;
1304 		}
1305 	}
1306 
1307 	/*
1308 	 * non-ATAPI, non-USB devices that don't support SCSI-2 commands
1309 	 * (i.e. MODE SENSE (10)) are done.
1310 	 */
1311 	if (!ISSET(link->flags, (SDEV_ATAPI | SDEV_UMASS)) &&
1312 	    SID_ANSII_REV(&link->inqdata) < SCSI_REV_2)
1313 		return error;
1314 
1315 	/*
1316 	 * Try 10 byte mode sense request.
1317 	 */
1318 	error = scsi_mode_sense_big(link, pg_code, buf, flags);
1319 	if (error != 0)
1320 		return error;
1321 
1322 	*big = 1;
1323 	*page_data = scsi_mode_sense_big_page(&buf->hdr_big, pg_code,
1324 	    pg_length);
1325 
1326 	return 0;
1327 }
1328 
1329 int
1330 scsi_mode_select(struct scsi_link *link, int byte2,
1331     struct scsi_mode_header *data, int flags, int timeout)
1332 {
1333 	struct scsi_mode_select		*cmd;
1334 	struct scsi_xfer		*xs;
1335 	int				 error;
1336 	u_int32_t			 len;
1337 
1338 	len = data->data_length + 1; /* 1 == sizeof(data_length) */
1339 
1340 	xs = scsi_xs_get(link, flags | SCSI_DATA_OUT);
1341 	if (xs == NULL)
1342 		return ENOMEM;
1343 	xs->cmdlen = sizeof(*cmd);
1344 	xs->data = (void *)data;
1345 	xs->datalen = len;
1346 	xs->timeout = timeout;
1347 
1348 	cmd = (struct scsi_mode_select *)&xs->cmd;
1349 	cmd->opcode = MODE_SELECT;
1350 	cmd->byte2 = byte2;
1351 	cmd->length = len;
1352 
1353 	/* Length is reserved when doing mode select so zero it. */
1354 	data->data_length = 0;
1355 
1356 	error = scsi_xs_sync(xs);
1357 	scsi_xs_put(xs);
1358 
1359 	SC_DEBUG(link, SDEV_DB2, ("scsi_mode_select: error = %d\n", error));
1360 
1361 	return error;
1362 }
1363 
1364 int
1365 scsi_mode_select_big(struct scsi_link *link, int byte2,
1366     struct scsi_mode_header_big *data, int flags, int timeout)
1367 {
1368 	struct scsi_mode_select_big	*cmd;
1369 	struct scsi_xfer		*xs;
1370 	int				 error;
1371 	u_int32_t			 len;
1372 
1373 	len = _2btol(data->data_length) + 2; /* 2 == sizeof data_length */
1374 
1375 	xs = scsi_xs_get(link, flags | SCSI_DATA_OUT);
1376 	if (xs == NULL)
1377 		return ENOMEM;
1378 	xs->cmdlen = sizeof(*cmd);
1379 	xs->data = (void *)data;
1380 	xs->datalen = len;
1381 	xs->timeout = timeout;
1382 
1383 	cmd = (struct scsi_mode_select_big *)&xs->cmd;
1384 	cmd->opcode = MODE_SELECT_BIG;
1385 	cmd->byte2 = byte2;
1386 	_lto2b(len, cmd->length);
1387 
1388 	/* Length is reserved when doing mode select so zero it. */
1389 	_lto2b(0, data->data_length);
1390 
1391 	error = scsi_xs_sync(xs);
1392 	scsi_xs_put(xs);
1393 
1394 	SC_DEBUG(link, SDEV_DB2, ("scsi_mode_select_big: error = %d\n",
1395 	    error));
1396 
1397 	return error;
1398 }
1399 
1400 int
1401 scsi_report_luns(struct scsi_link *link, int selectreport,
1402     struct scsi_report_luns_data *data, u_int32_t datalen, int flags,
1403     int timeout)
1404 {
1405 	struct scsi_report_luns		*cmd;
1406 	struct scsi_xfer		*xs;
1407 	int				 error;
1408 
1409 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN);
1410 	if (xs == NULL)
1411 		return ENOMEM;
1412 	xs->cmdlen = sizeof(*cmd);
1413 	xs->data = (void *)data;
1414 	xs->datalen = datalen;
1415 	xs->timeout = timeout;
1416 
1417 	bzero(data, datalen);
1418 
1419 	cmd = (struct scsi_report_luns *)&xs->cmd;
1420 	cmd->opcode = REPORT_LUNS;
1421 	cmd->selectreport = selectreport;
1422 	_lto4b(datalen, cmd->length);
1423 
1424 	error = scsi_xs_sync(xs);
1425 	scsi_xs_put(xs);
1426 
1427 	SC_DEBUG(link, SDEV_DB2, ("scsi_report_luns: error = %d\n", error));
1428 
1429 	return error;
1430 }
1431 
1432 void
1433 scsi_xs_exec(struct scsi_xfer *xs)
1434 {
1435 	xs->error = XS_NOERROR;
1436 	xs->resid = xs->datalen;
1437 	xs->status = 0;
1438 	CLR(xs->flags, ITSDONE);
1439 
1440 #ifdef SCSIDEBUG
1441 	scsi_show_xs(xs);
1442 #endif /* SCSIDEBUG */
1443 
1444 	/* The adapter's scsi_cmd() is responsible for calling scsi_done(). */
1445 	KERNEL_LOCK();
1446 	xs->sc_link->bus->sb_adapter->scsi_cmd(xs);
1447 	KERNEL_UNLOCK();
1448 }
1449 
1450 /*
1451  * Used by device drivers that fake various scsi commands.
1452  */
1453 void
1454 scsi_copy_internal_data(struct scsi_xfer *xs, void *data, size_t datalen)
1455 {
1456 	size_t copy_cnt;
1457 
1458 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("scsi_copy_internal_data\n"));
1459 
1460 	if (xs->datalen == 0) {
1461 		sc_print_addr(xs->sc_link);
1462 		printf("uio internal data copy not supported\n");
1463 	} else {
1464 		copy_cnt = MIN(datalen, xs->datalen);
1465 		memcpy(xs->data, data, copy_cnt);
1466 		xs->resid = xs->datalen - copy_cnt;
1467 	}
1468 }
1469 
1470 /*
1471  * This routine is called by the adapter when its xs handling is done.
1472  */
1473 void
1474 scsi_done(struct scsi_xfer *xs)
1475 {
1476 #ifdef SCSIDEBUG
1477 	if (ISSET(xs->sc_link->flags, SDEV_DB1)) {
1478 		if (xs->datalen && ISSET(xs->flags, SCSI_DATA_IN))
1479 			scsi_show_mem(xs->data, min(64, xs->datalen));
1480 	}
1481 #endif /* SCSIDEBUG */
1482 
1483 	SET(xs->flags, ITSDONE);
1484 	KERNEL_LOCK();
1485 	xs->done(xs);
1486 	KERNEL_UNLOCK();
1487 }
1488 
1489 int
1490 scsi_xs_sync(struct scsi_xfer *xs)
1491 {
1492 	struct mutex	cookie = MUTEX_INITIALIZER(IPL_BIO);
1493 	int		error;
1494 
1495 #ifdef DIAGNOSTIC
1496 	if (xs->cookie != NULL)
1497 		panic("xs->cookie != NULL in scsi_xs_sync");
1498 	if (xs->done != NULL)
1499 		panic("xs->done != NULL in scsi_xs_sync");
1500 #endif /* DIAGNOSTIC */
1501 
1502 	/*
1503 	 * If we cant sleep while waiting for completion, get the adapter to
1504 	 * complete it for us.
1505 	 */
1506 	if (ISSET(xs->flags, SCSI_NOSLEEP))
1507 		SET(xs->flags, SCSI_POLL);
1508 
1509 	xs->done = scsi_xs_sync_done;
1510 
1511 	do {
1512 		xs->cookie = &cookie;
1513 
1514 		scsi_xs_exec(xs);
1515 
1516 		mtx_enter(&cookie);
1517 		while (xs->cookie != NULL)
1518 			msleep_nsec(xs, &cookie, PRIBIO, "syncxs", INFSLP);
1519 		mtx_leave(&cookie);
1520 
1521 		error = scsi_xs_error(xs);
1522 	} while (error == ERESTART);
1523 
1524 	return error;
1525 }
1526 
1527 void
1528 scsi_xs_sync_done(struct scsi_xfer *xs)
1529 {
1530 	struct mutex *cookie = xs->cookie;
1531 
1532 	if (cookie == NULL)
1533 		panic("scsi_done called twice on xs(%p)", xs);
1534 
1535 	mtx_enter(cookie);
1536 	xs->cookie = NULL;
1537 	if (!ISSET(xs->flags, SCSI_NOSLEEP))
1538 		wakeup_one(xs);
1539 	mtx_leave(cookie);
1540 }
1541 
1542 int
1543 scsi_xs_error(struct scsi_xfer *xs)
1544 {
1545 	int error = EIO;
1546 
1547 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("scsi_xs_error,err = 0x%x\n",
1548 	    xs->error));
1549 
1550 	if (ISSET(xs->sc_link->state, SDEV_S_DYING))
1551 		return ENXIO;
1552 
1553 	switch (xs->error) {
1554 	case XS_NOERROR:	/* nearly always hit this one */
1555 		error = 0;
1556 		break;
1557 
1558 	case XS_SENSE:
1559 	case XS_SHORTSENSE:
1560 		SC_DEBUG_SENSE(xs);
1561 		error = xs->sc_link->interpret_sense(xs);
1562 		SC_DEBUG(xs->sc_link, SDEV_DB3,
1563 		    ("scsi_interpret_sense returned %#x\n", error));
1564 		break;
1565 
1566 	case XS_BUSY:
1567 		error = scsi_delay(xs, 1);
1568 		break;
1569 
1570 	case XS_TIMEOUT:
1571 	case XS_RESET:
1572 		error = ERESTART;
1573 		break;
1574 
1575 	case XS_DRIVER_STUFFUP:
1576 	case XS_SELTIMEOUT:
1577 		break;
1578 
1579 	default:
1580 		sc_print_addr(xs->sc_link);
1581 		printf("unknown error category (0x%x) from scsi driver\n",
1582 		    xs->error);
1583 		break;
1584 	}
1585 
1586 	if (error == ERESTART && xs->retries-- < 1)
1587 		return EIO;
1588 	else
1589 		return error;
1590 }
1591 
1592 int
1593 scsi_delay(struct scsi_xfer *xs, int seconds)
1594 {
1595 	int ret;
1596 
1597 	switch (xs->flags & (SCSI_POLL | SCSI_NOSLEEP)) {
1598 	case SCSI_POLL:
1599 		delay(1000000 * seconds);
1600 		return ERESTART;
1601 	case SCSI_NOSLEEP:
1602 		/* Retry the command immediately since we can't delay. */
1603 		return ERESTART;
1604 	case (SCSI_POLL | SCSI_NOSLEEP):
1605 		/* Invalid combination! */
1606 		return EIO;
1607 	}
1608 
1609 	ret = tsleep_nsec(&ret, PRIBIO|PCATCH, "scbusy", SEC_TO_NSEC(seconds));
1610 
1611 	/* Signal == abort xs. */
1612 	if (ret == ERESTART || ret == EINTR)
1613 		return EIO;
1614 
1615 	return ERESTART;
1616 }
1617 
1618 /*
1619  * Look at the returned sense and act on the error, determining
1620  * the unix error number to pass back.  (0 = report no error)
1621  *
1622  * THIS IS THE DEFAULT ERROR HANDLER
1623  */
1624 int
1625 scsi_interpret_sense(struct scsi_xfer *xs)
1626 {
1627 	struct scsi_sense_data			*sense = &xs->sense;
1628 	struct scsi_link			*link = xs->sc_link;
1629 	u_int8_t				serr, skey;
1630 	int					error;
1631 
1632 	/* Default sense interpretation. */
1633 	serr = sense->error_code & SSD_ERRCODE;
1634 	if (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)
1635 		skey = 0xff;	/* Invalid value, since key is 4 bit value. */
1636 	else
1637 		skey = sense->flags & SSD_KEY;
1638 
1639 	/*
1640 	 * Interpret the key/asc/ascq information where appropriate.
1641 	 */
1642 	error = 0;
1643 	switch (skey) {
1644 	case SKEY_NO_SENSE:
1645 	case SKEY_RECOVERED_ERROR:
1646 		if (xs->resid == xs->datalen)
1647 			xs->resid = 0;	/* not short read */
1648 		break;
1649 	case SKEY_BLANK_CHECK:
1650 	case SKEY_EQUAL:
1651 		break;
1652 	case SKEY_NOT_READY:
1653 		if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY))
1654 			return 0;
1655 		error = EIO;
1656 		if (xs->retries) {
1657 			switch (ASC_ASCQ(sense)) {
1658 			case SENSE_NOT_READY_BECOMING_READY:
1659 			case SENSE_NOT_READY_FORMAT:
1660 			case SENSE_NOT_READY_REBUILD:
1661 			case SENSE_NOT_READY_RECALC:
1662 			case SENSE_NOT_READY_INPROGRESS:
1663 			case SENSE_NOT_READY_LONGWRITE:
1664 			case SENSE_NOT_READY_SELFTEST:
1665 			case SENSE_NOT_READY_INIT_REQUIRED:
1666 				SC_DEBUG(link, SDEV_DB1,
1667 				    ("not ready (ASC_ASCQ == %#x)\n",
1668 				    ASC_ASCQ(sense)));
1669 				return scsi_delay(xs, 1);
1670 			case SENSE_NOMEDIUM:
1671 			case SENSE_NOMEDIUM_TCLOSED:
1672 			case SENSE_NOMEDIUM_TOPEN:
1673 			case SENSE_NOMEDIUM_LOADABLE:
1674 			case SENSE_NOMEDIUM_AUXMEM:
1675 				CLR(link->flags, SDEV_MEDIA_LOADED);
1676 				error = ENOMEDIUM;
1677 				break;
1678 			default:
1679 				break;
1680 			}
1681 		}
1682 		break;
1683 	case SKEY_MEDIUM_ERROR:
1684 		switch (ASC_ASCQ(sense)) {
1685 		case SENSE_NOMEDIUM:
1686 		case SENSE_NOMEDIUM_TCLOSED:
1687 		case SENSE_NOMEDIUM_TOPEN:
1688 		case SENSE_NOMEDIUM_LOADABLE:
1689 		case SENSE_NOMEDIUM_AUXMEM:
1690 			CLR(link->flags, SDEV_MEDIA_LOADED);
1691 			error = ENOMEDIUM;
1692 			break;
1693 		case SENSE_BAD_MEDIUM:
1694 		case SENSE_NR_MEDIUM_UNKNOWN_FORMAT:
1695 		case SENSE_NR_MEDIUM_INCOMPATIBLE_FORMAT:
1696 		case SENSE_NW_MEDIUM_UNKNOWN_FORMAT:
1697 		case SENSE_NW_MEDIUM_INCOMPATIBLE_FORMAT:
1698 		case SENSE_NF_MEDIUM_INCOMPATIBLE_FORMAT:
1699 		case SENSE_NW_MEDIUM_AC_MISMATCH:
1700 			error = EMEDIUMTYPE;
1701 			break;
1702 		default:
1703 			error = EIO;
1704 			break;
1705 		}
1706 		break;
1707 	case SKEY_ILLEGAL_REQUEST:
1708 		if (ISSET(xs->flags, SCSI_IGNORE_ILLEGAL_REQUEST))
1709 			return 0;
1710 		if (ASC_ASCQ(sense) == SENSE_MEDIUM_REMOVAL_PREVENTED)
1711 			return EBUSY;
1712 		error = EINVAL;
1713 		break;
1714 	case SKEY_UNIT_ATTENTION:
1715 		switch (ASC_ASCQ(sense)) {
1716 		case SENSE_POWER_RESET_OR_BUS:
1717 		case SENSE_POWER_ON:
1718 		case SENSE_BUS_RESET:
1719 		case SENSE_BUS_DEVICE_RESET:
1720 		case SENSE_DEVICE_INTERNAL_RESET:
1721 		case SENSE_TSC_CHANGE_SE:
1722 		case SENSE_TSC_CHANGE_LVD:
1723 		case SENSE_IT_NEXUS_LOSS:
1724 			return scsi_delay(xs, 1);
1725 		default:
1726 			break;
1727 		}
1728 		if (ISSET(link->flags, SDEV_REMOVABLE))
1729 			CLR(link->flags, SDEV_MEDIA_LOADED);
1730 		if (ISSET(xs->flags, SCSI_IGNORE_MEDIA_CHANGE) ||
1731 		    /* XXX Should reupload any transient state. */
1732 		    !ISSET(link->flags, SDEV_REMOVABLE)) {
1733 			return scsi_delay(xs, 1);
1734 		}
1735 		error = EIO;
1736 		break;
1737 	case SKEY_WRITE_PROTECT:
1738 		error = EROFS;
1739 		break;
1740 	case SKEY_ABORTED_COMMAND:
1741 		error = ERESTART;
1742 		break;
1743 	case SKEY_VOLUME_OVERFLOW:
1744 		error = ENOSPC;
1745 		break;
1746 	case SKEY_HARDWARE_ERROR:
1747 		if (ASC_ASCQ(sense) == SENSE_CARTRIDGE_FAULT)
1748 			return EMEDIUMTYPE;
1749 		error = EIO;
1750 		break;
1751 	default:
1752 		error = EIO;
1753 		break;
1754 	}
1755 
1756 #ifndef SCSIDEBUG
1757 	/* SCSIDEBUG would mean it has already been printed. */
1758 	if (skey && !ISSET(xs->flags, SCSI_SILENT))
1759 		scsi_print_sense(xs);
1760 #endif /* ~SCSIDEBUG */
1761 
1762 	return error;
1763 }
1764 
1765 /*
1766  * Utility routines often used in SCSI stuff
1767  */
1768 
1769 
1770 /*
1771  * Print out the scsi_link structure's address info.
1772  */
1773 void
1774 sc_print_addr(struct scsi_link *link)
1775 {
1776 	struct device *adapter_device = link->bus->sc_dev.dv_parent;
1777 
1778 	printf("%s(%s:%d:%d): ",
1779 	    link->device_softc ?
1780 	    ((struct device *)link->device_softc)->dv_xname : "probe",
1781 	    adapter_device->dv_xname,
1782 	    link->target, link->lun);
1783 }
1784 
1785 static const char *sense_keys[16] = {
1786 	"No Additional Sense",
1787 	"Soft Error",
1788 	"Not Ready",
1789 	"Media Error",
1790 	"Hardware Error",
1791 	"Illegal Request",
1792 	"Unit Attention",
1793 	"Write Protected",
1794 	"Blank Check",
1795 	"Vendor Unique",
1796 	"Copy Aborted",
1797 	"Aborted Command",
1798 	"Equal Error",
1799 	"Volume Overflow",
1800 	"Miscompare Error",
1801 	"Reserved"
1802 };
1803 
1804 #ifdef SCSITERSE
1805 static __inline void
1806 asc2ascii(u_int8_t asc, u_int8_t ascq, char *result, size_t len)
1807 {
1808 	snprintf(result, len, "ASC 0x%02x ASCQ 0x%02x", asc, ascq);
1809 }
1810 #else
1811 static const struct {
1812 	u_int8_t	 asc, ascq;
1813 	char		*description;
1814 } adesc[] = {
1815 	/* www.t10.org/lists/asc-num.txt as of 11/15/10. */
1816 	{ 0x00, 0x00, "No Additional Sense Information" },
1817 	{ 0x00, 0x01, "Filemark Detected" },
1818 	{ 0x00, 0x02, "End-Of-Partition/Medium Detected" },
1819 	{ 0x00, 0x03, "Setmark Detected" },
1820 	{ 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
1821 	{ 0x00, 0x05, "End-Of-Data Detected" },
1822 	{ 0x00, 0x06, "I/O Process Terminated" },
1823 	{ 0x00, 0x11, "Audio Play Operation In Progress" },
1824 	{ 0x00, 0x12, "Audio Play Operation Paused" },
1825 	{ 0x00, 0x13, "Audio Play Operation Successfully Completed" },
1826 	{ 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
1827 	{ 0x00, 0x15, "No Current Audio Status To Return" },
1828 	{ 0x00, 0x16, "Operation In Progress" },
1829 	{ 0x00, 0x17, "Cleaning Requested" },
1830 	{ 0x00, 0x18, "Erase Operation In Progress" },
1831 	{ 0x00, 0x19, "Locate Operation In Progress" },
1832 	{ 0x00, 0x1A, "Rewind Operation In Progress" },
1833 	{ 0x00, 0x1B, "Set Capacity Operation In Progress" },
1834 	{ 0x00, 0x1C, "Verify Operation In Progress" },
1835 	{ 0x01, 0x00, "No Index/Sector Signal" },
1836 	{ 0x02, 0x00, "No Seek Complete" },
1837 	{ 0x03, 0x00, "Peripheral Device Write Fault" },
1838 	{ 0x03, 0x01, "No Write Current" },
1839 	{ 0x03, 0x02, "Excessive Write Errors" },
1840 	{ 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
1841 	{ 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
1842 	{ 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
1843 	{ 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
1844 	{ 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
1845 	{ 0x04, 0x05, "Logical Unit Not Ready, Rebuild In Progress" },
1846 	{ 0x04, 0x06, "Logical Unit Not Ready, Recalculation In Progress" },
1847 	{ 0x04, 0x07, "Logical Unit Not Ready, Operation In Progress" },
1848 	{ 0x04, 0x08, "Logical Unit Not Ready, Long Write In Progress" },
1849 	{ 0x04, 0x09, "Logical Unit Not Ready, Self-Test In Progress" },
1850 	{ 0x04, 0x0A, "Logical Unit Not Accessible, Asymmetric Access State Transition" },
1851 	{ 0x04, 0x0B, "Logical Unit Not Accessible, Target Port In Standby State" },
1852 	{ 0x04, 0x0C, "Logical Unit Not Accessible, Target Port In Unavailable State" },
1853 	{ 0x04, 0x0D, "Logical Unit Not Ready, Structure Check Required" },
1854 	{ 0x04, 0x10, "Logical Unit Not Ready, Auxiliary Memory Not Accessible" },
1855 	{ 0x04, 0x11, "Logical Unit Not Ready, Notify (Enable Spinup) Required" },
1856 	{ 0x04, 0x12, "Logical Unit Not Ready, Offline" },
1857 	{ 0x04, 0x13, "Logical Unit Not Ready, SA Creation In Progress" },
1858 	{ 0x04, 0x14, "Logical Unit Not Ready, Space Allocation In Progress" },
1859 	{ 0x04, 0x15, "Logical Unit Not Ready, Robotics Disabled" },
1860 	{ 0x04, 0x16, "Logical Unit Not Ready, Configuration Required" },
1861 	{ 0x04, 0x17, "Logical Unit Not Ready, Calibration Required" },
1862 	{ 0x04, 0x18, "Logical Unit Not Ready, A Door Is Open" },
1863 	{ 0x04, 0x19, "Logical Unit Not Ready, Operating In Sequential Mode" },
1864 	{ 0x04, 0x1A, "Logical Unit Not Ready, Start Stop Unit Command In Progress" },
1865 	{ 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
1866 	{ 0x06, 0x00, "No Reference Position Found" },
1867 	{ 0x07, 0x00, "Multiple Peripheral Devices Selected" },
1868 	{ 0x08, 0x00, "Logical Unit Communication Failure" },
1869 	{ 0x08, 0x01, "Logical Unit Communication Timeout" },
1870 	{ 0x08, 0x02, "Logical Unit Communication Parity Error" },
1871 	{ 0x08, 0x03, "Logical Unit Communication CRC Error (ULTRA-DMA/32)" },
1872 	{ 0x08, 0x04, "Unreachable Copy Target" },
1873 	{ 0x09, 0x00, "Track Following Error" },
1874 	{ 0x09, 0x01, "Tracking Servo Failure" },
1875 	{ 0x09, 0x02, "Focus Servo Failure" },
1876 	{ 0x09, 0x03, "Spindle Servo Failure" },
1877 	{ 0x09, 0x04, "Head Select Fault" },
1878 	{ 0x0A, 0x00, "Error Log Overflow" },
1879 	{ 0x0B, 0x00, "Warning" },
1880 	{ 0x0B, 0x01, "Warning - Specified Temperature Exceeded" },
1881 	{ 0x0B, 0x02, "Warning - Enclosure Degraded" },
1882 	{ 0x0B, 0x03, "Warning - Background Self-Test Failed" },
1883 	{ 0x0B, 0x04, "Warning - Background Pre-Scan Detected Medium Error" },
1884 	{ 0x0B, 0x05, "Warning - Background Medium Scan Detected Medium Error" },
1885 	{ 0x0B, 0x06, "Warning - Non-Volatile Cache Now Volatile" },
1886 	{ 0x0B, 0x07, "Warning - Degraded Power To Non-Volatile Cache" },
1887 	{ 0x0B, 0x08, "Warning - Power Loss Expected" },
1888 	{ 0x0C, 0x00, "Write Error" },
1889 	{ 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
1890 	{ 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
1891 	{ 0x0C, 0x03, "Write Error - Recommend Reassignment" },
1892 	{ 0x0C, 0x04, "Compression Check Miscompare Error" },
1893 	{ 0x0C, 0x05, "Data Expansion Occurred During Compression" },
1894 	{ 0x0C, 0x06, "Block Not Compressible" },
1895 	{ 0x0C, 0x07, "Write Error - Recovery Needed" },
1896 	{ 0x0C, 0x08, "Write Error - Recovery Failed" },
1897 	{ 0x0C, 0x09, "Write Error - Loss Of Streaming" },
1898 	{ 0x0C, 0x0A, "Write Error - Padding Blocks Added" },
1899 	{ 0x0C, 0x0B, "Auxiliary Memory Write Error" },
1900 	{ 0x0C, 0x0C, "Write Error - Unexpected Unsolicited Data" },
1901 	{ 0x0C, 0x0D, "Write Error - Not Enough Unsolicited Data" },
1902 	{ 0x0C, 0x0F, "Defects In Error Window" },
1903 	{ 0x0D, 0x00, "Error Detected By Third Party Temporary Initiator" },
1904 	{ 0x0D, 0x01, "Third Party Device Failure" },
1905 	{ 0x0D, 0x02, "Copy Target Device Not Reachable" },
1906 	{ 0x0D, 0x03, "Incorrect Copy Target Device Type" },
1907 	{ 0x0D, 0x04, "Copy Target Device Data Underrun" },
1908 	{ 0x0D, 0x05, "Copy Target Device Data Overrun" },
1909 	{ 0x0E, 0x00, "Invalid Information Unit" },
1910 	{ 0x0E, 0x01, "Information Unit Too Short" },
1911 	{ 0x0E, 0x02, "Information Unit Too Long" },
1912 	{ 0x10, 0x00, "ID CRC Or ECC Error" },
1913 	{ 0x10, 0x01, "Logical Block Guard Check Failed" },
1914 	{ 0x10, 0x02, "Logical Block Application Tag Check Failed" },
1915 	{ 0x10, 0x03, "Logical Block Reference Tag Check Failed" },
1916 	{ 0x10, 0x04, "Logical Block Protection Error On Recover Buffered Data" },
1917 	{ 0x10, 0x05, "Logical Block Protection Method Error" },
1918 	{ 0x11, 0x00, "Unrecovered Read Error" },
1919 	{ 0x11, 0x01, "Read Retries Exhausted" },
1920 	{ 0x11, 0x02, "Error Too Long To Correct" },
1921 	{ 0x11, 0x03, "Multiple Read Errors" },
1922 	{ 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
1923 	{ 0x11, 0x05, "L-EC Uncorrectable Error" },
1924 	{ 0x11, 0x06, "CIRC Unrecovered Error" },
1925 	{ 0x11, 0x07, "Data Resynchronization Error" },
1926 	{ 0x11, 0x08, "Incomplete Block Read" },
1927 	{ 0x11, 0x09, "No Gap Found" },
1928 	{ 0x11, 0x0A, "Miscorrected Error" },
1929 	{ 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
1930 	{ 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite The Data" },
1931 	{ 0x11, 0x0D, "De-Compression CRC Error" },
1932 	{ 0x11, 0x0E, "Cannot Decompress Using Declared Algorithm" },
1933 	{ 0x11, 0x0F, "Error Reading UPC/EAN Number" },
1934 	{ 0x11, 0x10, "Error Reading ISRC Number" },
1935 	{ 0x11, 0x11, "Read Error - Loss Of Streaming" },
1936 	{ 0x11, 0x12, "Auxiliary Memory Read Error" },
1937 	{ 0x11, 0x13, "Read Error - Failed Retransmission Request" },
1938 	{ 0x11, 0x14, "Read Error - LBA Marked Bad By Application Client" },
1939 	{ 0x12, 0x00, "Address Mark Not Found for ID Field" },
1940 	{ 0x13, 0x00, "Address Mark Not Found for Data Field" },
1941 	{ 0x14, 0x00, "Recorded Entity Not Found" },
1942 	{ 0x14, 0x01, "Record Not Found" },
1943 	{ 0x14, 0x02, "Filemark or Setmark Not Found" },
1944 	{ 0x14, 0x03, "End-Of-Data Not Found" },
1945 	{ 0x14, 0x04, "Block Sequence Error" },
1946 	{ 0x14, 0x05, "Record Not Found - Recommend Reassignment" },
1947 	{ 0x14, 0x06, "Record Not Found - Data Auto-Reallocated" },
1948 	{ 0x14, 0x07, "Locate Operation Failure" },
1949 	{ 0x15, 0x00, "Random Positioning Error" },
1950 	{ 0x15, 0x01, "Mechanical Positioning Error" },
1951 	{ 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
1952 	{ 0x16, 0x00, "Data Synchronization Mark Error" },
1953 	{ 0x16, 0x01, "Data Sync Error - Data Rewritten" },
1954 	{ 0x16, 0x02, "Data Sync Error - Recommend Rewrite" },
1955 	{ 0x16, 0x03, "Data Sync Error - Data Auto-Reallocated" },
1956 	{ 0x16, 0x04, "Data Sync Error - Recommend Reassignment" },
1957 	{ 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
1958 	{ 0x17, 0x01, "Recovered Data With Retries" },
1959 	{ 0x17, 0x02, "Recovered Data With Positive Head Offset" },
1960 	{ 0x17, 0x03, "Recovered Data With Negative Head Offset" },
1961 	{ 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
1962 	{ 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
1963 	{ 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
1964 	{ 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
1965 	{ 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
1966 	{ 0x17, 0x09, "Recovered Data Without ECC - Data Rewritten" },
1967 	{ 0x18, 0x00, "Recovered Data With Error Correction Applied" },
1968 	{ 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
1969 	{ 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
1970 	{ 0x18, 0x03, "Recovered Data With CIRC" },
1971 	{ 0x18, 0x04, "Recovered Data With L-EC" },
1972 	{ 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
1973 	{ 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
1974 	{ 0x18, 0x07, "Recovered Data With ECC - Data Rewritten" },
1975 	{ 0x18, 0x08, "Recovered Data With Linking" },
1976 	{ 0x19, 0x00, "Defect List Error" },
1977 	{ 0x19, 0x01, "Defect List Not Available" },
1978 	{ 0x19, 0x02, "Defect List Error in Primary List" },
1979 	{ 0x19, 0x03, "Defect List Error in Grown List" },
1980 	{ 0x1A, 0x00, "Parameter List Length Error" },
1981 	{ 0x1B, 0x00, "Synchronous Data Transfer Error" },
1982 	{ 0x1C, 0x00, "Defect List Not Found" },
1983 	{ 0x1C, 0x01, "Primary Defect List Not Found" },
1984 	{ 0x1C, 0x02, "Grown Defect List Not Found" },
1985 	{ 0x1D, 0x00, "Miscompare During Verify Operation" },
1986 	{ 0x1D, 0x01, "Miscompare Verify Of Unmapped Lba" },
1987 	{ 0x1E, 0x00, "Recovered ID with ECC" },
1988 	{ 0x1F, 0x00, "Partial Defect List Transfer" },
1989 	{ 0x20, 0x00, "Invalid Command Operation Code" },
1990 	{ 0x20, 0x01, "Access Denied - Initiator Pending-Enrolled" },
1991 	{ 0x20, 0x02, "Access Denied - No Access rights" },
1992 	{ 0x20, 0x03, "Access Denied - Invalid Mgmt ID Key" },
1993 	{ 0x20, 0x04, "Illegal Command While In Write Capable State" },
1994 	{ 0x20, 0x05, "Obsolete" },
1995 	{ 0x20, 0x06, "Illegal Command While In Explicit Address Mode" },
1996 	{ 0x20, 0x07, "Illegal Command While In Implicit Address Mode" },
1997 	{ 0x20, 0x08, "Access Denied - Enrollment Conflict" },
1998 	{ 0x20, 0x09, "Access Denied - Invalid LU Identifier" },
1999 	{ 0x20, 0x0A, "Access Denied - Invalid Proxy Token" },
2000 	{ 0x20, 0x0B, "Access Denied - ACL LUN Conflict" },
2001 	{ 0x20, 0x0C, "Illegal Command When Not In Append-Only Mode" },
2002 	{ 0x21, 0x00, "Logical Block Address Out of Range" },
2003 	{ 0x21, 0x01, "Invalid Element Address" },
2004 	{ 0x21, 0x02, "Invalid Address For Write" },
2005 	{ 0x21, 0x03, "Invalid Write Crossing Layer Jump" },
2006 	{ 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
2007 	{ 0x24, 0x00, "Illegal Field in CDB" },
2008 	{ 0x24, 0x01, "CDB Decryption Error" },
2009 	{ 0x24, 0x02, "Obsolete" },
2010 	{ 0x24, 0x03, "Obsolete" },
2011 	{ 0x24, 0x04, "Security Audit Value Frozen" },
2012 	{ 0x24, 0x05, "Security Working Key Frozen" },
2013 	{ 0x24, 0x06, "Nonce Not Unique" },
2014 	{ 0x24, 0x07, "Nonce Timestamp Out Of Range" },
2015 	{ 0x24, 0x08, "Invalid XCDB" },
2016 	{ 0x25, 0x00, "Logical Unit Not Supported" },
2017 	{ 0x26, 0x00, "Invalid Field In Parameter List" },
2018 	{ 0x26, 0x01, "Parameter Not Supported" },
2019 	{ 0x26, 0x02, "Parameter Value Invalid" },
2020 	{ 0x26, 0x03, "Threshold Parameters Not Supported" },
2021 	{ 0x26, 0x04, "Invalid Release Of Persistent Reservation" },
2022 	{ 0x26, 0x05, "Data Decryption Error" },
2023 	{ 0x26, 0x06, "Too Many Target Descriptors" },
2024 	{ 0x26, 0x07, "Unsupported Target Descriptor Type Code" },
2025 	{ 0x26, 0x08, "Too Many Segment Descriptors" },
2026 	{ 0x26, 0x09, "Unsupported Segment Descriptor Type Code" },
2027 	{ 0x26, 0x0A, "Unexpected Inexact Segment" },
2028 	{ 0x26, 0x0B, "Inline Data Length Exceeded" },
2029 	{ 0x26, 0x0C, "Invalid Operation For Copy Source Or Destination" },
2030 	{ 0x26, 0x0D, "Copy Segment Granularity Violation" },
2031 	{ 0x26, 0x0E, "Invalid Parameter While Port Is Enabled" },
2032 	{ 0x26, 0x0F, "Invalid Data-Out Buffer Integrity Check Value" },
2033 	{ 0x26, 0x10, "Data Decryption Key Fail Limit Reached" },
2034 	{ 0x26, 0x11, "Incomplete Key-Associated Data Set" },
2035 	{ 0x26, 0x12, "Vendor Specific Key Reference Not Found" },
2036 	{ 0x27, 0x00, "Write Protected" },
2037 	{ 0x27, 0x01, "Hardware Write Protected" },
2038 	{ 0x27, 0x02, "Logical Unit Software Write Protected" },
2039 	{ 0x27, 0x03, "Associated Write Protect" },
2040 	{ 0x27, 0x04, "Persistent Write Protect" },
2041 	{ 0x27, 0x05, "Permanent Write Protect" },
2042 	{ 0x27, 0x06, "Conditional Write Protect" },
2043 	{ 0x27, 0x07, "Space Allocation Failed Write Protect" },
2044 	{ 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
2045 	{ 0x28, 0x01, "Import Or Export Element Accessed" },
2046 	{ 0x28, 0x02, "Format-Layer May Have Changed" },
2047 	{ 0x28, 0x03, "Import/Export Element Accessed, Medium Changed" },
2048 	{ 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
2049 	{ 0x29, 0x01, "Power On Occurred" },
2050 	{ 0x29, 0x02, "SCSI Bus Reset Occurred" },
2051 	{ 0x29, 0x03, "Bus Device Reset Function Occurred" },
2052 	{ 0x29, 0x04, "Device Internal Reset" },
2053 	{ 0x29, 0x05, "Transceiver Mode Changed to Single Ended" },
2054 	{ 0x29, 0x06, "Transceiver Mode Changed to LVD" },
2055 	{ 0x29, 0x07, "I_T Nexus Loss Occurred" },
2056 	{ 0x2A, 0x00, "Parameters Changed" },
2057 	{ 0x2A, 0x01, "Mode Parameters Changed" },
2058 	{ 0x2A, 0x02, "Log Parameters Changed" },
2059 	{ 0x2A, 0x03, "Reservations Preempted" },
2060 	{ 0x2A, 0x04, "Reservations Released" },
2061 	{ 0x2A, 0x05, "Registrations Preempted" },
2062 	{ 0x2A, 0x06, "Asymmetric Access State Changed" },
2063 	{ 0x2A, 0x07, "Implicit Asymmetric Access State Transition Failed" },
2064 	{ 0x2A, 0x08, "Priority Changed" },
2065 	{ 0x2A, 0x09, "Capacity Data Has Changed" },
2066 	{ 0x2A, 0x0A, "Error History I_T Nexus Cleared" },
2067 	{ 0x2A, 0x0B, "Error History Snapshot Released" },
2068 	{ 0x2A, 0x0C, "Error Recovery Attributes Have Changed" },
2069 	{ 0x2A, 0x0D, "Data Encryption Capabilities Changed" },
2070 	{ 0x2A, 0x10, "Timestamp Changed" },
2071 	{ 0x2A, 0x11, "Data Encryption Parameters Changed By Another I_T Nexus" },
2072 	{ 0x2A, 0x12, "Data Encryption Parameters Changed By Vendor Specific Event" },
2073 	{ 0x2A, 0x13, "Data Encryption Key Instance Counter Has Changed" },
2074 	{ 0x2A, 0x14, "SA Creation Capabilities Data Has Changed" },
2075 	{ 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
2076 	{ 0x2C, 0x00, "Command Sequence Error" },
2077 	{ 0x2C, 0x01, "Too Many Windows Specified" },
2078 	{ 0x2C, 0x02, "Invalid Combination of Windows Specified" },
2079 	{ 0x2C, 0x03, "Current Program Area Is Not Empty" },
2080 	{ 0x2C, 0x04, "Current Program Area Is Empty" },
2081 	{ 0x2C, 0x05, "Illegal Power Condition Request" },
2082 	{ 0x2C, 0x06, "Persistent Prevent Conflict" },
2083 	{ 0x2C, 0x07, "Previous Busy Status" },
2084 	{ 0x2C, 0x08, "Previous Task Set Full Status" },
2085 	{ 0x2C, 0x09, "Previous Reservation Conflict Status" },
2086 	{ 0x2C, 0x0A, "Partition Or Collection Contains User Objects" },
2087 	{ 0x2C, 0x0B, "Not Reserved" },
2088 	{ 0x2C, 0x0C, "ORWrite Generation Does Not Match" },
2089 	{ 0x2D, 0x00, "Overwrite Error On Update In Place" },
2090 	{ 0x2E, 0x00, "Insufficient Time For Operation" },
2091 	{ 0x2F, 0x00, "Commands Cleared By Another Initiator" },
2092 	{ 0x2F, 0x01, "Commands Cleared By Power Loss Notification" },
2093 	{ 0x2F, 0x02, "Commands Cleared By Device Server" },
2094 	{ 0x30, 0x00, "Incompatible Medium Installed" },
2095 	{ 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
2096 	{ 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
2097 	{ 0x30, 0x03, "Cleaning Cartridge Installed" },
2098 	{ 0x30, 0x04, "Cannot Write Medium - Unknown Format" },
2099 	{ 0x30, 0x05, "Cannot Write Medium - Incompatible Format" },
2100 	{ 0x30, 0x06, "Cannot Format Medium - Incompatible Medium" },
2101 	{ 0x30, 0x07, "Cleaning Failure" },
2102 	{ 0x30, 0x08, "Cannot Write - Application Code Mismatch" },
2103 	{ 0x30, 0x09, "Current Session Not Fixated For Append" },
2104 	{ 0x30, 0x0A, "Cleaning Request Rejected" },
2105 	{ 0x30, 0x10, "Medium Not Formatted" },
2106 	{ 0x30, 0x11, "Incompatible Volume Type" },
2107 	{ 0x30, 0x12, "Incompatible Volume Qualifier" },
2108 	{ 0x30, 0x13, "Cleaning Volume Expired" },
2109 	{ 0x31, 0x00, "Medium Format Corrupted" },
2110 	{ 0x31, 0x01, "Format Command Failed" },
2111 	{ 0x31, 0x02, "Zoned Formatting Failed Due To Spare Linking" },
2112 	{ 0x32, 0x00, "No Defect Spare Location Available" },
2113 	{ 0x32, 0x01, "Defect List Update Failure" },
2114 	{ 0x33, 0x00, "Tape Length Error" },
2115 	{ 0x34, 0x00, "Enclosure Failure" },
2116 	{ 0x35, 0x00, "Enclosure Services Failure" },
2117 	{ 0x35, 0x01, "Unsupported Enclosure Function" },
2118 	{ 0x35, 0x02, "Enclosure Services Unavailable" },
2119 	{ 0x35, 0x03, "Enclosure Services Transfer Failure" },
2120 	{ 0x35, 0x04, "Enclosure Services Transfer Refused" },
2121 	{ 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
2122 	{ 0x37, 0x00, "Rounded Parameter" },
2123 	{ 0x38, 0x00, "Event Status Notification" },
2124 	{ 0x38, 0x02, "ESN - Power Management Class Event" },
2125 	{ 0x38, 0x04, "ESN - Media Class Event" },
2126 	{ 0x38, 0x06, "ESN - Device Busy Class Event" },
2127 	{ 0x39, 0x00, "Saving Parameters Not Supported" },
2128 	{ 0x3A, 0x00, "Medium Not Present" },
2129 	{ 0x3A, 0x01, "Medium Not Present - Tray Closed" },
2130 	{ 0x3A, 0x02, "Medium Not Present - Tray Open" },
2131 	{ 0x3A, 0x03, "Medium Not Present - Loadable" },
2132 	{ 0x3A, 0x04, "Medium Not Present - Medium Auxiliary Memory Accessible" },
2133 	{ 0x3B, 0x00, "Sequential Positioning Error" },
2134 	{ 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
2135 	{ 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
2136 	{ 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
2137 	{ 0x3B, 0x04, "Slew Failure" },
2138 	{ 0x3B, 0x05, "Paper Jam" },
2139 	{ 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
2140 	{ 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
2141 	{ 0x3B, 0x08, "Reposition Error" },
2142 	{ 0x3B, 0x09, "Read Past End Of Medium" },
2143 	{ 0x3B, 0x0A, "Read Past Beginning Of Medium" },
2144 	{ 0x3B, 0x0B, "Position Past End Of Medium" },
2145 	{ 0x3B, 0x0C, "Position Past Beginning Of Medium" },
2146 	{ 0x3B, 0x0D, "Medium Destination Element Full" },
2147 	{ 0x3B, 0x0E, "Medium Source Element Empty" },
2148 	{ 0x3B, 0x0F, "End Of Medium Reached" },
2149 	{ 0x3B, 0x11, "Medium Magazine Not Accessible" },
2150 	{ 0x3B, 0x12, "Medium Magazine Removed" },
2151 	{ 0x3B, 0x13, "Medium Magazine Inserted" },
2152 	{ 0x3B, 0x14, "Medium Magazine Locked" },
2153 	{ 0x3B, 0x15, "Medium Magazine Unlocked" },
2154 	{ 0x3B, 0x16, "Mechanical Positioning Or Changer Error" },
2155 	{ 0x3B, 0x17, "Read Past End Of User Object" },
2156 	{ 0x3B, 0x18, "Element Disabled" },
2157 	{ 0x3B, 0x19, "Element Enabled" },
2158 	{ 0x3B, 0x1A, "Data Transfer Device Removed" },
2159 	{ 0x3B, 0x1B, "Data Transfer Device Inserted" },
2160 	{ 0x3D, 0x00, "Invalid Bits In IDENTIFY Message" },
2161 	{ 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
2162 	{ 0x3E, 0x01, "Logical Unit Failure" },
2163 	{ 0x3E, 0x02, "Timeout On Logical Unit" },
2164 	{ 0x3E, 0x03, "Logical Unit Failed Self-Test" },
2165 	{ 0x3E, 0x04, "Logical Unit Unable To Update Self-Test Log" },
2166 	{ 0x3F, 0x00, "Target Operating Conditions Have Changed" },
2167 	{ 0x3F, 0x01, "Microcode Has Changed" },
2168 	{ 0x3F, 0x02, "Changed Operating Definition" },
2169 	{ 0x3F, 0x03, "INQUIRY Data Has Changed" },
2170 	{ 0x3F, 0x04, "component Device Attached" },
2171 	{ 0x3F, 0x05, "Device Identifier Changed" },
2172 	{ 0x3F, 0x06, "Redundancy Group Created Or Modified" },
2173 	{ 0x3F, 0x07, "Redundancy Group Deleted" },
2174 	{ 0x3F, 0x08, "Spare Created Or Modified" },
2175 	{ 0x3F, 0x09, "Spare Deleted" },
2176 	{ 0x3F, 0x0A, "Volume Set Created Or Modified" },
2177 	{ 0x3F, 0x0B, "Volume Set Deleted" },
2178 	{ 0x3F, 0x0C, "Volume Set Deassigned" },
2179 	{ 0x3F, 0x0D, "Volume Set Reassigned" },
2180 	{ 0x3F, 0x0E, "Reported LUNs Data Has Changed" },
2181 	{ 0x3F, 0x0F, "Echo Buffer Overwritten" },
2182 	{ 0x3F, 0x10, "Medium Loadable" },
2183 	{ 0x3F, 0x11, "Medium Auxiliary Memory Accessible" },
2184 	{ 0x3F, 0x12, "iSCSI IP Address Added" },
2185 	{ 0x3F, 0x13, "iSCSI IP Address Removed" },
2186 	{ 0x3F, 0x14, "iSCSI IP Address Changed" },
2187 	{ 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
2188 	/*
2189 	 * ASC 0x40 also has an ASCQ range from 0x80 to 0xFF.
2190 	 * 0x40 0xNN DIAGNOSTIC FAILURE ON COMPONENT NN
2191 	 */
2192 	{ 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
2193 	{ 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
2194 	{ 0x43, 0x00, "Message Error" },
2195 	{ 0x44, 0x00, "Internal Target Failure" },
2196 	{ 0x44, 0x71, "ATA Device Failed Set Features" },
2197 	{ 0x45, 0x00, "Select Or Reselect Failure" },
2198 	{ 0x46, 0x00, "Unsuccessful Soft Reset" },
2199 	{ 0x47, 0x00, "SCSI Parity Error" },
2200 	{ 0x47, 0x01, "Data Phase CRC Error Detected" },
2201 	{ 0x47, 0x02, "SCSI Parity Error Detected During ST Data Phase" },
2202 	{ 0x47, 0x03, "Information Unit iuCRC Error Detected" },
2203 	{ 0x47, 0x04, "Asynchronous Information Protection Error Detected" },
2204 	{ 0x47, 0x05, "Protocol Service CRC Error" },
2205 	{ 0x47, 0x06, "PHY Test Function In Progress" },
2206 	{ 0x47, 0x7F, "Some Commands Cleared By iSCSI Protocol Event" },
2207 	{ 0x48, 0x00, "Initiator Detected Error Message Received" },
2208 	{ 0x49, 0x00, "Invalid Message Error" },
2209 	{ 0x4A, 0x00, "Command Phase Error" },
2210 	{ 0x4B, 0x00, "Data Phase Error" },
2211 	{ 0x4B, 0x01, "Invalid Target Port Transfer Tag Received" },
2212 	{ 0x4B, 0x02, "Too Much Write Data" },
2213 	{ 0x4B, 0x03, "ACK/NAK Timeout" },
2214 	{ 0x4B, 0x04, "NAK Received" },
2215 	{ 0x4B, 0x05, "Data Offset Error" },
2216 	{ 0x4B, 0x06, "Initiator Response Timeout" },
2217 	{ 0x4B, 0x07, "Connection Lost" },
2218 	{ 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
2219 	/*
2220 	 * ASC 0x4D has an ASCQ range from 0x00 to 0xFF.
2221 	 * 0x4D 0xNN TAGGED OVERLAPPED COMMANDS (NN = TASK TAG)
2222 	 */
2223 	{ 0x4E, 0x00, "Overlapped Commands Attempted" },
2224 	{ 0x50, 0x00, "Write Append Error" },
2225 	{ 0x50, 0x01, "Write Append Position Error" },
2226 	{ 0x50, 0x02, "Position Error Related To Timing" },
2227 	{ 0x51, 0x00, "Erase Failure" },
2228 	{ 0x51, 0x01, "Erase Failure - Incomplete Erase Operation Detected" },
2229 	{ 0x52, 0x00, "Cartridge Fault" },
2230 	{ 0x53, 0x00, "Media Load or Eject Failed" },
2231 	{ 0x53, 0x01, "Unload Tape Failure" },
2232 	{ 0x53, 0x02, "Medium Removal Prevented" },
2233 	{ 0x53, 0x03, "Medium Removal Prevented By Data Transfer Element" },
2234 	{ 0x53, 0x04, "Medium Thread Or Unthread Failure" },
2235 	{ 0x53, 0x05, "Volume Identifier Invalid" },
2236 	{ 0x53, 0x06, "Volume Identifier Missing" },
2237 	{ 0x53, 0x07, "Duplicate Volume Identifier" },
2238 	{ 0x53, 0x08, "Element Status Unknown" },
2239 	{ 0x54, 0x00, "SCSI To Host System Interface Failure" },
2240 	{ 0x55, 0x00, "System Resource Failure" },
2241 	{ 0x55, 0x01, "System Buffer Full" },
2242 	{ 0x55, 0x02, "Insufficient Reservation Resources" },
2243 	{ 0x55, 0x03, "Insufficient Resources" },
2244 	{ 0x55, 0x04, "Insufficient Registration Resources" },
2245 	{ 0x55, 0x05, "Insufficient Access Control Resources" },
2246 	{ 0x55, 0x06, "Auxiliary Memory Out Of Space" },
2247 	{ 0x55, 0x07, "Quota Error" },
2248 	{ 0x55, 0x08, "Maximum Number Of Supplemental Decryption Keys Exceeded" },
2249 	{ 0x55, 0x09, "Medium Auxiliary Memory Not Accessible" },
2250 	{ 0x55, 0x0A, "Data Currently Unavailable" },
2251 	{ 0x55, 0x0B, "Insufficient Power For Operation" },
2252 	{ 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
2253 	{ 0x58, 0x00, "Generation Does Not Exist" },
2254 	{ 0x59, 0x00, "Updated Block Read" },
2255 	{ 0x5A, 0x00, "Operator Request or State Change Input" },
2256 	{ 0x5A, 0x01, "Operator Medium Removal Requested" },
2257 	{ 0x5A, 0x02, "Operator Selected Write Protect" },
2258 	{ 0x5A, 0x03, "Operator Selected Write Permit" },
2259 	{ 0x5B, 0x00, "Log Exception" },
2260 	{ 0x5B, 0x01, "Threshold Condition Met" },
2261 	{ 0x5B, 0x02, "Log Counter At Maximum" },
2262 	{ 0x5B, 0x03, "Log List Codes Exhausted" },
2263 	{ 0x5C, 0x00, "RPL Status Change" },
2264 	{ 0x5C, 0x01, "Spindles Synchronized" },
2265 	{ 0x5C, 0x02, "Spindles Not Synchronized" },
2266 	{ 0x5D, 0x00, "Failure Prediction Threshold Exceeded" },
2267 	{ 0x5D, 0x01, "Media Failure Prediction Threshold Exceeded" },
2268 	{ 0x5D, 0x02, "Logical Unit Failure Prediction Threshold Exceeded" },
2269 	{ 0x5D, 0x03, "Spare Area Exhaustion Prediction Threshold Exceeded" },
2270 	{ 0x5D, 0x10, "Hardware Impending Failure General Hard Drive Failure" },
2271 	{ 0x5D, 0x11, "Hardware Impending Failure Drive Error Rate Too High" },
2272 	{ 0x5D, 0x12, "Hardware Impending Failure Data Error Rate Too High" },
2273 	{ 0x5D, 0x13, "Hardware Impending Failure Seek Error Rate Too High" },
2274 	{ 0x5D, 0x14, "Hardware Impending Failure Too Many Block Reassigns" },
2275 	{ 0x5D, 0x15, "Hardware Impending Failure Access Times Too High" },
2276 	{ 0x5D, 0x16, "Hardware Impending Failure Start Unit Times Too High" },
2277 	{ 0x5D, 0x17, "Hardware Impending Failure Channel Parametrics" },
2278 	{ 0x5D, 0x18, "Hardware Impending Failure Controller Detected" },
2279 	{ 0x5D, 0x19, "Hardware Impending Failure Throughput Performance" },
2280 	{ 0x5D, 0x1A, "Hardware Impending Failure Seek Time Performance" },
2281 	{ 0x5D, 0x1B, "Hardware Impending Failure Spin-Up Retry Count" },
2282 	{ 0x5D, 0x1C, "Hardware Impending Failure Drive Calibration Retry Count" },
2283 	{ 0x5D, 0x20, "Controller Impending Failure General Hard Drive Failure" },
2284 	{ 0x5D, 0x21, "Controller Impending Failure Drive Error Rate Too High" },
2285 	{ 0x5D, 0x22, "Controller Impending Failure Data Error Rate Too High" },
2286 	{ 0x5D, 0x23, "Controller Impending Failure Seek Error Rate Too High" },
2287 	{ 0x5D, 0x24, "Controller Impending Failure Too Many Block Reassigns" },
2288 	{ 0x5D, 0x25, "Controller Impending Failure Access Times Too High" },
2289 	{ 0x5D, 0x26, "Controller Impending Failure Start Unit Times Too High" },
2290 	{ 0x5D, 0x27, "Controller Impending Failure Channel Parametrics" },
2291 	{ 0x5D, 0x28, "Controller Impending Failure Controller Detected" },
2292 	{ 0x5D, 0x29, "Controller Impending Failure Throughput Performance" },
2293 	{ 0x5D, 0x2A, "Controller Impending Failure Seek Time Performance" },
2294 	{ 0x5D, 0x2B, "Controller Impending Failure Spin-Up Retry Count" },
2295 	{ 0x5D, 0x2C, "Controller Impending Failure Drive Calibration Retry Count" },
2296 	{ 0x5D, 0x30, "Data Channel Impending Failure General Hard Drive Failure" },
2297 	{ 0x5D, 0x31, "Data Channel Impending Failure Drive Error Rate Too High" },
2298 	{ 0x5D, 0x32, "Data Channel Impending Failure Data Error Rate Too High" },
2299 	{ 0x5D, 0x33, "Data Channel Impending Failure Seek Error Rate Too High" },
2300 	{ 0x5D, 0x34, "Data Channel Impending Failure Too Many Block Reassigns" },
2301 	{ 0x5D, 0x35, "Data Channel Impending Failure Access Times Too High" },
2302 	{ 0x5D, 0x36, "Data Channel Impending Failure Start Unit Times Too High" },
2303 	{ 0x5D, 0x37, "Data Channel Impending Failure Channel Parametrics" },
2304 	{ 0x5D, 0x38, "Data Channel Impending Failure Controller Detected" },
2305 	{ 0x5D, 0x39, "Data Channel Impending Failure Throughput Performance" },
2306 	{ 0x5D, 0x3A, "Data Channel Impending Failure Seek Time Performance" },
2307 	{ 0x5D, 0x3B, "Data Channel Impending Failure Spin-Up Retry Count" },
2308 	{ 0x5D, 0x3C, "Data Channel Impending Failure Drive Calibration Retry Count" },
2309 	{ 0x5D, 0x40, "Servo Impending Failure General Hard Drive Failure" },
2310 	{ 0x5D, 0x41, "Servo Impending Failure Drive Error Rate Too High" },
2311 	{ 0x5D, 0x42, "Servo Impending Failure Data Error Rate Too High" },
2312 	{ 0x5D, 0x43, "Servo Impending Failure Seek Error Rate Too High" },
2313 	{ 0x5D, 0x44, "Servo Impending Failure Too Many Block Reassigns" },
2314 	{ 0x5D, 0x45, "Servo Impending Failure Access Times Too High" },
2315 	{ 0x5D, 0x46, "Servo Impending Failure Start Unit Times Too High" },
2316 	{ 0x5D, 0x47, "Servo Impending Failure Channel Parametrics" },
2317 	{ 0x5D, 0x48, "Servo Impending Failure Controller Detected" },
2318 	{ 0x5D, 0x49, "Servo Impending Failure Throughput Performance" },
2319 	{ 0x5D, 0x4A, "Servo Impending Failure Seek Time Performance" },
2320 	{ 0x5D, 0x4B, "Servo Impending Failure Spin-Up Retry Count" },
2321 	{ 0x5D, 0x4C, "Servo Impending Failure Drive Calibration Retry Count" },
2322 	{ 0x5D, 0x50, "Spindle Impending Failure General Hard Drive Failure" },
2323 	{ 0x5D, 0x51, "Spindle Impending Failure Drive Error Rate Too High" },
2324 	{ 0x5D, 0x52, "Spindle Impending Failure Data Error Rate Too High" },
2325 	{ 0x5D, 0x53, "Spindle Impending Failure Seek Error Rate Too High" },
2326 	{ 0x5D, 0x54, "Spindle Impending Failure Too Many Block Reassigns" },
2327 	{ 0x5D, 0x55, "Spindle Impending Failure Access Times Too High" },
2328 	{ 0x5D, 0x56, "Spindle Impending Failure Start Unit Times Too High" },
2329 	{ 0x5D, 0x57, "Spindle Impending Failure Channel Parametrics" },
2330 	{ 0x5D, 0x58, "Spindle Impending Failure Controller Detected" },
2331 	{ 0x5D, 0x59, "Spindle Impending Failure Throughput Performance" },
2332 	{ 0x5D, 0x5A, "Spindle Impending Failure Seek Time Performance" },
2333 	{ 0x5D, 0x5B, "Spindle Impending Failure Spin-Up Retry Count" },
2334 	{ 0x5D, 0x5C, "Spindle Impending Failure Drive Calibration Retry Count" },
2335 	{ 0x5D, 0x60, "Firmware Impending Failure General Hard Drive Failure" },
2336 	{ 0x5D, 0x61, "Firmware Impending Failure Drive Error Rate Too High" },
2337 	{ 0x5D, 0x62, "Firmware Impending Failure Data Error Rate Too High" },
2338 	{ 0x5D, 0x63, "Firmware Impending Failure Seek Error Rate Too High" },
2339 	{ 0x5D, 0x64, "Firmware Impending Failure Too Many Block Reassigns" },
2340 	{ 0x5D, 0x65, "Firmware Impending Failure Access Times Too High" },
2341 	{ 0x5D, 0x66, "Firmware Impending Failure Start Unit Times Too High" },
2342 	{ 0x5D, 0x67, "Firmware Impending Failure Channel Parametrics" },
2343 	{ 0x5D, 0x68, "Firmware Impending Failure Controller Detected" },
2344 	{ 0x5D, 0x69, "Firmware Impending Failure Throughput Performance" },
2345 	{ 0x5D, 0x6A, "Firmware Impending Failure Seek Time Performance" },
2346 	{ 0x5D, 0x6B, "Firmware Impending Failure Spin-Up Retry Count" },
2347 	{ 0x5D, 0x6C, "Firmware Impending Failure Drive Calibration Retry Count" },
2348 	{ 0x5D, 0xFF, "Failure Prediction Threshold Exceeded (false)" },
2349 	{ 0x5E, 0x00, "Low Power Condition On" },
2350 	{ 0x5E, 0x01, "Idle Condition Activated By Timer" },
2351 	{ 0x5E, 0x02, "Standby Condition Activated By Timer" },
2352 	{ 0x5E, 0x03, "Idle Condition Activated By Command" },
2353 	{ 0x5E, 0x04, "Standby Condition Activated By Command" },
2354 	{ 0x5E, 0x05, "IDLE_B Condition Activated By Timer" },
2355 	{ 0x5E, 0x06, "IDLE_B Condition Activated By Command" },
2356 	{ 0x5E, 0x07, "IDLE_C Condition Activated By Timer" },
2357 	{ 0x5E, 0x08, "IDLE_C Condition Activated By Command" },
2358 	{ 0x5E, 0x09, "STANDBY_Y Condition Activated By Timer" },
2359 	{ 0x5E, 0x0A, "STANDBY_Y Condition Activated By Command" },
2360 	{ 0x5E, 0x41, "Power State Change To Active" },
2361 	{ 0x5E, 0x42, "Power State Change To Idle" },
2362 	{ 0x5E, 0x43, "Power State Change To Standby" },
2363 	{ 0x5E, 0x45, "Power State Change To Sleep" },
2364 	{ 0x5E, 0x47, "Power State Change To Device Control" },
2365 	{ 0x60, 0x00, "Lamp Failure" },
2366 	{ 0x61, 0x00, "Video Acquisition Error" },
2367 	{ 0x61, 0x01, "Unable To Acquire Video" },
2368 	{ 0x61, 0x02, "Out Of Focus" },
2369 	{ 0x62, 0x00, "Scan Head Positioning Error" },
2370 	{ 0x63, 0x00, "End Of User Area Encountered On This Track" },
2371 	{ 0x63, 0x01, "Packet Does Not Fit In Available Space" },
2372 	{ 0x64, 0x00, "Illegal Mode For This Track" },
2373 	{ 0x64, 0x01, "Invalid Packet Size" },
2374 	{ 0x65, 0x00, "Voltage Fault" },
2375 	{ 0x66, 0x00, "Automatic Document Feeder Cover Up" },
2376 	{ 0x66, 0x01, "Automatic Document Feeder Lift Up" },
2377 	{ 0x66, 0x02, "Document Jam In Automatic Document Feeder" },
2378 	{ 0x66, 0x03, "Document Miss Feed Automatic In Document Feeder" },
2379 	{ 0x67, 0x00, "Configuration Failure" },
2380 	{ 0x67, 0x01, "Configuration Of Incapable Logical Units Failed" },
2381 	{ 0x67, 0x02, "Add Logical Unit Failed" },
2382 	{ 0x67, 0x03, "Modification Of Logical Unit Failed" },
2383 	{ 0x67, 0x04, "Exchange Of Logical Unit Failed" },
2384 	{ 0x67, 0x05, "Remove Of Logical Unit Failed" },
2385 	{ 0x67, 0x06, "Attachment Of Logical Unit Failed" },
2386 	{ 0x67, 0x07, "Creation Of Logical Unit Failed" },
2387 	{ 0x67, 0x08, "Assign Failure Occurred" },
2388 	{ 0x67, 0x09, "Multiply Assigned Logical Unit" },
2389 	{ 0x67, 0x0A, "Set Target Port Groups Command Failed" },
2390 	{ 0x67, 0x0B, "ATA Device Feature Not Enabled" },
2391 	{ 0x68, 0x00, "Logical Unit Not Configured" },
2392 	{ 0x69, 0x00, "Data Loss On Logical Unit" },
2393 	{ 0x69, 0x01, "Multiple Logical Unit Failures" },
2394 	{ 0x69, 0x02, "Parity/Data Mismatch" },
2395 	{ 0x6A, 0x00, "Informational, Refer To Log" },
2396 	{ 0x6B, 0x00, "State Change Has Occurred" },
2397 	{ 0x6B, 0x01, "Redundancy Level Got Better" },
2398 	{ 0x6B, 0x02, "Redundancy Level Got Worse" },
2399 	{ 0x6C, 0x00, "Rebuild Failure Occurred" },
2400 	{ 0x6D, 0x00, "Recalculate Failure Occurred" },
2401 	{ 0x6E, 0x00, "Command To Logical Unit Failed" },
2402 	{ 0x6F, 0x00, "Copy Protection Key Exchange Failure - Authentication Failure" },
2403 	{ 0x6F, 0x01, "Copy Protection Key Exchange Failure - Key Not Present" },
2404 	{ 0x6F, 0x02, "Copy Protection Key Exchange Failure - Key Not Established" },
2405 	{ 0x6F, 0x03, "Read Of Scrambled Sector Without Authentication" },
2406 	{ 0x6F, 0x04, "Media Region Code Is Mismatched To Logical Unit Region" },
2407 	{ 0x6F, 0x05, "Drive Region Must Be Permanent/Region Reset Count Error" },
2408 	/*
2409 	 * ASC 0x70 has an ASCQ range from 0x00 to 0xFF.
2410 	 * 0x70 0xNN DECOMPRESSION EXCEPTION SHORT ALGORITHM ID Of NN
2411 	 */
2412 	{ 0x71, 0x00, "Decompression Exception Long Algorithm ID" },
2413 	{ 0x72, 0x00, "Session Fixation Error" },
2414 	{ 0x72, 0x01, "Session Fixation Error Writing Lead-In" },
2415 	{ 0x72, 0x02, "Session Fixation Error Writing Lead-Out" },
2416 	{ 0x72, 0x03, "Session Fixation Error - Incomplete Track In Session" },
2417 	{ 0x72, 0x04, "Empty Or Partially Written Reserved Track" },
2418 	{ 0x72, 0x05, "No More Track Reservations Allowed" },
2419 	{ 0x72, 0x06, "RMZ Extension Is Not Allowed" },
2420 	{ 0x72, 0x07, "No More Test Zone Extensions Are Allowed" },
2421 	{ 0x73, 0x00, "CD Control Error" },
2422 	{ 0x73, 0x01, "Power Calibration Area Almost Full" },
2423 	{ 0x73, 0x02, "Power Calibration Area Is Full" },
2424 	{ 0x73, 0x03, "Power Calibration Area Error" },
2425 	{ 0x73, 0x04, "Program Memory Area Update Failure" },
2426 	{ 0x73, 0x05, "Program Memory Area Is Full" },
2427 	{ 0x73, 0x06, "RMA/PMA Is Almost Full" },
2428 	{ 0x73, 0x10, "Current Power Calibration Area Almost Full" },
2429 	{ 0x73, 0x11, "Current Power Calibration Area Is Full" },
2430 	{ 0x73, 0x17, "RDZ Is Full" },
2431 	{ 0x74, 0x00, "Security Error" },
2432 	{ 0x74, 0x01, "Unable To Decrypt Data" },
2433 	{ 0x74, 0x02, "Unencrypted Data Encountered While Decrypting" },
2434 	{ 0x74, 0x03, "Incorrect Data Encryption Key" },
2435 	{ 0x74, 0x04, "Cryptographic Integrity Validation Failed" },
2436 	{ 0x74, 0x05, "Error Decrypting Data" },
2437 	{ 0x74, 0x06, "Unknown Signature Verification Key" },
2438 	{ 0x74, 0x07, "Encryption Parameters Not Useable" },
2439 	{ 0x74, 0x08, "Digital Signature Validation Failure" },
2440 	{ 0x74, 0x09, "Encryption Mode Mismatch On Read" },
2441 	{ 0x74, 0x0A, "Encrypted Block Not Raw Read Enabled" },
2442 	{ 0x74, 0x0B, "Incorrect Encryption Parameters" },
2443 	{ 0x74, 0x0C, "Unable To Decrypt Parameter List" },
2444 	{ 0x74, 0x0D, "Encryption Algorithm Disabled" },
2445 	{ 0x74, 0x10, "SA Creation Parameter Value Invalid" },
2446 	{ 0x74, 0x11, "SA Creation Parameter Value Rejected" },
2447 	{ 0x74, 0x12, "Invalid SA Usage" },
2448 	{ 0x74, 0x21, "Data Encryption Configuration Prevented" },
2449 	{ 0x74, 0x30, "SA Creation Parameter Not Supported" },
2450 	{ 0x74, 0x40, "Authentication Failed" },
2451 	{ 0x74, 0x61, "External Data Encryption Key Manager Access Error" },
2452 	{ 0x74, 0x62, "External Data Encryption Key Manager Error" },
2453 	{ 0x74, 0x63, "External Data Encryption Key Not Found" },
2454 	{ 0x74, 0x64, "External Data Encryption Request Not Authorized" },
2455 	{ 0x74, 0x6E, "External Data Encryption Control Timeout" },
2456 	{ 0x74, 0x6F, "External Data Encryption Control Error" },
2457 	{ 0x74, 0x71, "Logical Unit Access Not Authorized" },
2458 	{ 0x74, 0x79, "Security Conflict In Translated Device" },
2459 	{ 0x00, 0x00, NULL }
2460 };
2461 
2462 static __inline void
2463 asc2ascii(u_int8_t asc, u_int8_t ascq, char *result, size_t len)
2464 {
2465 	int i;
2466 
2467 	/* Check for a dynamically built description. */
2468 	switch (asc) {
2469 	case 0x40:
2470 		if (ascq >= 0x80) {
2471 			snprintf(result, len,
2472 		            "Diagnostic Failure on Component 0x%02x", ascq);
2473 			return;
2474 		}
2475 		break;
2476 	case 0x4d:
2477 		snprintf(result, len,
2478 		    "Tagged Overlapped Commands (0x%02x = TASK TAG)", ascq);
2479 		return;
2480 	case 0x70:
2481 		snprintf(result, len,
2482 		    "Decompression Exception Short Algorithm ID OF 0x%02x",
2483 		    ascq);
2484 		return;
2485 	default:
2486 		break;
2487 	}
2488 
2489 	/* Check for a fixed description. */
2490 	for (i = 0; adesc[i].description != NULL; i++) {
2491 		if (adesc[i].asc == asc && adesc[i].ascq == ascq) {
2492 			strlcpy(result, adesc[i].description, len);
2493 			return;
2494 		}
2495 	}
2496 
2497 	/* Just print out the ASC and ASCQ values as a description. */
2498 	snprintf(result, len, "ASC 0x%02x ASCQ 0x%02x", asc, ascq);
2499 }
2500 #endif /* SCSITERSE */
2501 
2502 void
2503 scsi_print_sense(struct scsi_xfer *xs)
2504 {
2505 	struct scsi_sense_data		*sense = &xs->sense;
2506 	char				*sbs;
2507 	int32_t				 info;
2508 	u_int8_t			 serr = sense->error_code & SSD_ERRCODE;
2509 
2510 	sc_print_addr(xs->sc_link);
2511 
2512 	/* XXX For error 0x71, current opcode is not the relevant one. */
2513 	printf("%sCheck Condition (error %#x) on opcode 0x%x\n",
2514 	    (serr == SSD_ERRCODE_DEFERRED) ? "DEFERRED " : "", serr,
2515 	    xs->cmd.opcode);
2516 
2517 	if (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED) {
2518 		if (ISSET(sense->error_code, SSD_ERRCODE_VALID)) {
2519 			struct scsi_sense_data_unextended *usense =
2520 			    (struct scsi_sense_data_unextended *)sense;
2521 			printf("   AT BLOCK #: %d (decimal)",
2522 			    _3btol(usense->block));
2523 		}
2524 		return;
2525 	}
2526 
2527 	printf("    SENSE KEY: %s\n", scsi_decode_sense(sense,
2528 	    DECODE_SENSE_KEY));
2529 
2530 	if (sense->flags & (SSD_FILEMARK | SSD_EOM | SSD_ILI)) {
2531 		char pad = ' ';
2532 
2533 		printf("             ");
2534 		if (ISSET(sense->flags, SSD_FILEMARK)) {
2535 			printf("%c Filemark Detected", pad);
2536 			pad = ',';
2537 		}
2538 		if (ISSET(sense->flags, SSD_EOM)) {
2539 			printf("%c EOM Detected", pad);
2540 			pad = ',';
2541 		}
2542 		if (ISSET(sense->flags, SSD_ILI))
2543 			printf("%c Incorrect Length Indicator Set", pad);
2544 		printf("\n");
2545 	}
2546 
2547 	/*
2548 	 * It is inconvenient to use device type to figure out how to
2549 	 * format the info fields. So print them as 32 bit integers.
2550 	 */
2551 	info = _4btol(&sense->info[0]);
2552 	if (info)
2553 		printf("         INFO: 0x%x (VALID flag %s)\n", info,
2554 		    ISSET(sense->error_code, SSD_ERRCODE_VALID) ? "on" : "off");
2555 
2556 	if (sense->extra_len < 4)
2557 		return;
2558 
2559 	info = _4btol(&sense->cmd_spec_info[0]);
2560 	if (info)
2561 		printf(" COMMAND INFO: 0x%x\n", info);
2562 	sbs = scsi_decode_sense(sense, DECODE_ASC_ASCQ);
2563 	if (strlen(sbs) > 0)
2564 		printf("     ASC/ASCQ: %s\n", sbs);
2565 	if (sense->fru != 0)
2566 		printf("     FRU CODE: 0x%x\n", sense->fru);
2567 	sbs = scsi_decode_sense(sense, DECODE_SKSV);
2568 	if (strlen(sbs) > 0)
2569 		printf("         SKSV: %s\n", sbs);
2570 }
2571 
2572 char *
2573 scsi_decode_sense(struct scsi_sense_data *sense, int flag)
2574 {
2575 	static char				rqsbuf[132];
2576 	u_int16_t				count;
2577 	u_int8_t				skey, spec_1;
2578 	int					len;
2579 
2580 	bzero(rqsbuf, sizeof(rqsbuf));
2581 
2582 	skey = sense->flags & SSD_KEY;
2583 	spec_1 = sense->sense_key_spec_1;
2584 	count = _2btol(&sense->sense_key_spec_2);
2585 
2586 	switch (flag) {
2587 	case DECODE_SENSE_KEY:
2588 		strlcpy(rqsbuf, sense_keys[skey], sizeof(rqsbuf));
2589 		break;
2590 	case DECODE_ASC_ASCQ:
2591 		asc2ascii(sense->add_sense_code, sense->add_sense_code_qual,
2592 		    rqsbuf, sizeof(rqsbuf));
2593 		break;
2594 	case DECODE_SKSV:
2595 		if (sense->extra_len < 9 || !ISSET(spec_1, SSD_SCS_VALID))
2596 			break;
2597 		switch (skey) {
2598 		case SKEY_ILLEGAL_REQUEST:
2599 			len = snprintf(rqsbuf, sizeof rqsbuf,
2600 			    "Error in %s, Offset %d",
2601 			    ISSET(spec_1, SSD_SCS_CDB_ERROR) ? "CDB" :
2602 			    "Parameters", count);
2603 			if ((len != -1 && len < sizeof rqsbuf) &&
2604 			    ISSET(spec_1, SSD_SCS_VALID_BIT_INDEX))
2605 				snprintf(rqsbuf+len, sizeof rqsbuf - len,
2606 				    ", bit %d", spec_1 & SSD_SCS_BIT_INDEX);
2607 			break;
2608 		case SKEY_RECOVERED_ERROR:
2609 		case SKEY_MEDIUM_ERROR:
2610 		case SKEY_HARDWARE_ERROR:
2611 			snprintf(rqsbuf, sizeof rqsbuf,
2612 			    "Actual Retry Count: %d", count);
2613 			break;
2614 		case SKEY_NOT_READY:
2615 			snprintf(rqsbuf, sizeof rqsbuf,
2616 			    "Progress Indicator: %d", count);
2617 			break;
2618 		default:
2619 			break;
2620 		}
2621 		break;
2622 	default:
2623 		break;
2624 	}
2625 
2626 	return rqsbuf;
2627 }
2628 
2629 void
2630 scsi_cmd_rw_decode(struct scsi_generic *cmd, u_int64_t *blkno,
2631     u_int32_t *nblks)
2632 {
2633 	switch (cmd->opcode) {
2634 	case READ_COMMAND:
2635 	case WRITE_COMMAND: {
2636 		struct scsi_rw *rw = (struct scsi_rw *)cmd;
2637 		*blkno = _3btol(rw->addr) & (SRW_TOPADDR << 16 | 0xffff);
2638 		*nblks = rw->length ? rw->length : 0x100;
2639 		break;
2640 	}
2641 	case READ_10:
2642 	case WRITE_10: {
2643 		struct scsi_rw_10 *rw10 = (struct scsi_rw_10 *)cmd;
2644 		*blkno = _4btol(rw10->addr);
2645 		*nblks = _2btol(rw10->length);
2646 		break;
2647 	}
2648 	case READ_12:
2649 	case WRITE_12: {
2650 		struct scsi_rw_12 *rw12 = (struct scsi_rw_12 *)cmd;
2651 		*blkno = _4btol(rw12->addr);
2652 		*nblks = _4btol(rw12->length);
2653 		break;
2654 	}
2655 	case READ_16:
2656 	case WRITE_16: {
2657 		struct scsi_rw_16 *rw16 = (struct scsi_rw_16 *)cmd;
2658 		*blkno = _8btol(rw16->addr);
2659 		*nblks = _4btol(rw16->length);
2660 		break;
2661 	}
2662 	default:
2663 		panic("scsi_cmd_rw_decode: bad opcode 0x%02x", cmd->opcode);
2664 	}
2665 }
2666 
2667 #ifdef SCSIDEBUG
2668 u_int32_t scsidebug_buses = SCSIDEBUG_BUSES;
2669 u_int32_t scsidebug_targets = SCSIDEBUG_TARGETS;
2670 u_int32_t scsidebug_luns = SCSIDEBUG_LUNS;
2671 int scsidebug_level = SCSIDEBUG_LEVEL;
2672 
2673 const char *flagnames[] = {
2674 	"REMOVABLE",
2675 	"MEDIA LOADED",
2676 	"READONLY",
2677 	"OPEN",
2678 	"DB1",
2679 	"DB2",
2680 	"DB3",
2681 	"DB4",
2682 	"EJECTING",
2683 	"ATAPI",
2684 	"UMASS",
2685 	"VIRTUAL",
2686 	"OWN_IOPL",
2687 	NULL
2688 };
2689 
2690 const char *quirknames[] = {
2691 	"AUTOSAVE",
2692 	"NOSYNC",
2693 	"NOWIDE",
2694 	"NOTAGS",
2695 	"NOSYNCCACHE",
2696 	"NOSENSE",
2697 	"LITTLETOC",
2698 	"NOCAPACITY",
2699 	"NODOORLOCK",
2700 	NULL
2701 };
2702 
2703 const char *devicetypenames[32] = {
2704 	"T_DIRECT",
2705 	"T_SEQUENTIAL",
2706 	"T_PRINTER",
2707 	"T_PROCESSOR",
2708 	"T_WORM",
2709 	"T_CDROM",
2710 	"T_SCANNER",
2711 	"T_OPTICAL",
2712 	"T_CHANGER",
2713 	"T_COMM",
2714 	"T_ASC0",
2715 	"T_ASC1",
2716 	"T_STROARRAY",
2717 	"T_ENCLOSURE",
2718 	"T_RDIRECT",
2719 	"T_OCRW",
2720 	"T_BCC",
2721 	"T_OSD",
2722 	"T_ADC",
2723 	"T_RESERVED",
2724 	"T_RESERVED",
2725 	"T_RESERVED",
2726 	"T_RESERVED",
2727 	"T_RESERVED",
2728 	"T_RESERVED",
2729 	"T_RESERVED",
2730 	"T_RESERVED",
2731 	"T_RESERVED",
2732 	"T_RESERVED",
2733 	"T_RESERVED",
2734 	"T_WELL_KNOWN_LU",
2735 	"T_NODEVICE"
2736 };
2737 
2738 /*
2739  * Print out sense data details.
2740  */
2741 void
2742 scsi_show_sense(struct scsi_xfer *xs)
2743 {
2744 	struct scsi_sense_data	*sense = &xs->sense;
2745 	struct scsi_link	*link = xs->sc_link;
2746 
2747 	SC_DEBUG(link, SDEV_DB1,
2748 	    ("code:%#x valid:%d key:%#x ili:%d eom:%d fmark:%d extra:%d\n",
2749 	    sense->error_code & SSD_ERRCODE,
2750 	    sense->error_code & SSD_ERRCODE_VALID ? 1 : 0,
2751 	    sense->flags & SSD_KEY,
2752 	    sense->flags & SSD_ILI ? 1 : 0,
2753 	    sense->flags & SSD_EOM ? 1 : 0,
2754 	    sense->flags & SSD_FILEMARK ? 1 : 0,
2755 	    sense->extra_len));
2756 
2757 	if (ISSET(xs->sc_link->flags, SDEV_DB1))
2758 		scsi_show_mem((u_char *)&xs->sense, sizeof(xs->sense));
2759 
2760 	scsi_print_sense(xs);
2761 }
2762 
2763 /*
2764  * Given a scsi_xfer, dump the request, in all its glory
2765  */
2766 void
2767 scsi_show_xs(struct scsi_xfer *xs)
2768 {
2769 	u_char		*b = (u_char *)&xs->cmd;
2770 	int		 i = 0;
2771 
2772 	if (!ISSET(xs->sc_link->flags, SDEV_DB1))
2773 		return;
2774 
2775 	sc_print_addr(xs->sc_link);
2776 	printf("xs  (%p): ", xs);
2777 
2778 	printf("flg(0x%x)", xs->flags);
2779 	printf("link(%p)", xs->sc_link);
2780 	printf("retr(0x%x)", xs->retries);
2781 	printf("timo(0x%x)", xs->timeout);
2782 	printf("data(%p)", xs->data);
2783 	printf("res(0x%zx)", xs->resid);
2784 	printf("err(0x%x)", xs->error);
2785 	printf("bp(%p)\n", xs->bp);
2786 
2787 	sc_print_addr(xs->sc_link);
2788 	printf("cmd (%p): ", &xs->cmd);
2789 
2790 	if (!ISSET(xs->flags, SCSI_RESET)) {
2791 		while (i < xs->cmdlen) {
2792 			if (i)
2793 				printf(",");
2794 			printf("%x", b[i++]);
2795 		}
2796 		printf("-[%d bytes]\n", xs->datalen);
2797 	} else
2798 		printf("-RESET-\n");
2799 
2800 	if (xs->datalen && ISSET(xs->flags, SCSI_DATA_OUT))
2801 		scsi_show_mem(xs->data, min(64, xs->datalen));
2802 }
2803 
2804 void
2805 scsi_show_mem(u_char *address, int num)
2806 {
2807 	int x;
2808 
2809 	printf("------------------------------");
2810 	for (x = 0; x < num; x++) {
2811 		if ((x % 16) == 0)
2812 			printf("\n%03d: ", x);
2813 		printf("%02x ", *address++);
2814 	}
2815 	printf("\n------------------------------\n");
2816 }
2817 
2818 void
2819 scsi_show_flags(u_int32_t flags, const char **names)
2820 {
2821 	int		i, first, exhausted;
2822 	u_int32_t	unnamed;
2823 
2824 	printf("<");
2825 	for (first = 1, exhausted = 0, unnamed = 0, i = 0; i < 32; i++) {
2826 		if (!ISSET(flags, 1 << i))
2827 			continue;
2828 		if (exhausted == 0 && names[i] == NULL)
2829 			exhausted = 1;
2830 		if (exhausted || strlen(names[i]) == 0) {
2831 			SET(unnamed, 1 << i);
2832 			continue;
2833 		}
2834 		if (first == 0)
2835 			printf(", ");
2836 		else
2837 			first = 0;
2838 		printf("%s", names[i]);
2839 	}
2840 	if (unnamed != 0)
2841 		printf("%s0x%08x", first ? "" : ", ", unnamed);
2842 	printf(">");
2843 }
2844 #endif /* SCSIDEBUG */
2845