xref: /openbsd-src/sys/scsi/scsi_base.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: scsi_base.c,v 1.277 2020/10/14 23:40: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			 error;
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 
853 #ifdef SCSIDEBUG
854 again:
855 #endif /* SCSIDEBUG */
856 	xs = scsi_xs_get(link, flags);
857 	if (xs == NULL)
858 		return EBUSY;
859 
860 	scsi_init_inquiry(xs, 0, 0, inqbuf, bytes);
861 
862 	bzero(inqbuf, sizeof(*inqbuf));
863 	memset(&inqbuf->vendor, ' ', sizeof inqbuf->vendor);
864 	memset(&inqbuf->product, ' ', sizeof inqbuf->product);
865 	memset(&inqbuf->revision, ' ', sizeof inqbuf->revision);
866 	memset(&inqbuf->extra, ' ', sizeof inqbuf->extra);
867 
868 	error = scsi_xs_sync(xs);
869 
870 	scsi_xs_put(xs);
871 
872 #ifdef SCSIDEBUG
873 	sc_print_addr(link);
874 	if (bytes > SID_SCSI2_HDRLEN + inqbuf->additional_length)
875 		bytes = SID_SCSI2_HDRLEN + inqbuf->additional_length;
876 	printf("got %zu of %u bytes of inquiry data:\n",
877 	    bytes, SID_SCSI2_HDRLEN + inqbuf->additional_length);
878 	scsi_show_mem((u_char *)inqbuf, bytes);
879 	if (bytes == SID_SCSI2_HDRLEN + SID_SCSI2_ALEN && bytes <
880 	    SID_SCSI2_HDRLEN + inqbuf->additional_length) {
881 		bytes = SID_SCSI2_HDRLEN + inqbuf->additional_length;
882 		if (bytes > sizeof(*inqbuf))
883 			bytes = sizeof(*inqbuf);
884 		goto again;
885 	}
886 #endif /* SCSIDEBUG */
887 	return error;
888 }
889 
890 /*
891  * Query a VPD inquiry page
892  */
893 int
894 scsi_inquire_vpd(struct scsi_link *link, void *buf, u_int buflen,
895     u_int8_t page, int flags)
896 {
897 	struct scsi_xfer	*xs;
898 	int			 error;
899 #ifdef SCSIDEBUG
900 	u_int32_t		 bytes;
901 #endif /* SCSIDEBUG */
902 
903 	if (ISSET(link->flags, SDEV_UMASS))
904 		return EJUSTRETURN;
905 
906 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
907 	if (xs == NULL)
908 		return ENOMEM;
909 
910 	xs->retries = 2;
911 	xs->timeout = 10000;
912 
913 	scsi_init_inquiry(xs, SI_EVPD, page, buf, buflen);
914 
915 	error = scsi_xs_sync(xs);
916 
917 	scsi_xs_put(xs);
918 #ifdef SCSIDEBUG
919 	sc_print_addr(link);
920 	if (error == 0) {
921 		bytes = sizeof(struct scsi_vpd_hdr) +
922 		    _2btol(((struct scsi_vpd_hdr *)buf)->page_length);
923 		if (bytes < buflen)
924 			buflen = bytes;
925 		printf("got %u of %u bytes of VPD inquiry page %u data:\n",
926 		    buflen, bytes, page);
927 		scsi_show_mem(buf, buflen);
928 	} else {
929 		printf("VPD inquiry page %u not available\n", page);
930 	}
931 #endif /* SCSIDEBUG */
932 	return error;
933 }
934 
935 int
936 scsi_read_cap_10(struct scsi_link *link, struct scsi_read_cap_data *rdcap,
937     int flags)
938 {
939 	struct scsi_read_capacity	  cdb;
940 	struct scsi_xfer		 *xs;
941 	int				  rv;
942 
943 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
944 	if (xs == NULL)
945 		return ENOMEM;
946 
947 	memset(&cdb, 0, sizeof(cdb));
948 	cdb.opcode = READ_CAPACITY;
949 
950 	memcpy(&xs->cmd, &cdb, sizeof(cdb));
951 	xs->cmdlen = sizeof(cdb);
952 	xs->data = (void *)rdcap;
953 	xs->datalen = sizeof(*rdcap);
954 	xs->timeout = 20000;
955 
956 	rv = scsi_xs_sync(xs);
957 	scsi_xs_put(xs);
958 
959 #ifdef SCSIDEBUG
960 	if (rv == 0) {
961 		sc_print_addr(link);
962 		printf("read capacity 10 data:\n");
963 		scsi_show_mem((u_char *)rdcap, sizeof(*rdcap));
964 	}
965 #endif /* SCSIDEBUG */
966 
967 	return rv;
968 }
969 
970 int
971 scsi_read_cap_16(struct scsi_link *link, struct scsi_read_cap_data_16 *rdcap,
972     int flags)
973 {
974 	struct scsi_read_capacity_16	 cdb;
975 	struct scsi_xfer		*xs;
976 	int				 rv;
977 
978 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN | SCSI_SILENT);
979 	if (xs == NULL)
980 		return ENOMEM;
981 
982 	memset(&cdb, 0, sizeof(cdb));
983 	cdb.opcode = READ_CAPACITY_16;
984 	cdb.byte2 = SRC16_SERVICE_ACTION;
985 	_lto4b(sizeof(*rdcap), cdb.length);
986 
987 	memcpy(&xs->cmd, &cdb, sizeof(cdb));
988 	xs->cmdlen = sizeof(cdb);
989 	xs->data = (void *)rdcap;
990 	xs->datalen = sizeof(*rdcap);
991 	xs->timeout = 20000;
992 
993 	rv = scsi_xs_sync(xs);
994 	scsi_xs_put(xs);
995 
996 #ifdef SCSIDEBUG
997 	if (rv == 0) {
998 		sc_print_addr(link);
999 		printf("read capacity 16 data:\n");
1000 		scsi_show_mem((u_char *)rdcap, sizeof(*rdcap));
1001 	}
1002 #endif /* SCSIDEBUG */
1003 
1004 	return rv;
1005 }
1006 
1007 /*
1008  * Prevent or allow the user to remove the media
1009  */
1010 int
1011 scsi_prevent(struct scsi_link *link, int type, int flags)
1012 {
1013 	struct scsi_prevent	*cmd;
1014 	struct scsi_xfer	*xs;
1015 	int			 error;
1016 
1017 	if (ISSET(link->quirks, ADEV_NODOORLOCK))
1018 		return 0;
1019 
1020 	xs = scsi_xs_get(link, flags);
1021 	if (xs == NULL)
1022 		return ENOMEM;
1023 	xs->cmdlen = sizeof(*cmd);
1024 	xs->retries = 2;
1025 	xs->timeout = 5000;
1026 
1027 	cmd = (struct scsi_prevent *)&xs->cmd;
1028 	cmd->opcode = PREVENT_ALLOW;
1029 	cmd->how = type;
1030 
1031 	error = scsi_xs_sync(xs);
1032 	scsi_xs_put(xs);
1033 
1034 	return error;
1035 }
1036 
1037 /*
1038  * Get scsi driver to send a "start up" command
1039  */
1040 int
1041 scsi_start(struct scsi_link *link, int type, int flags)
1042 {
1043 	struct scsi_start_stop	*cmd;
1044 	struct scsi_xfer	*xs;
1045 	int			 error;
1046 
1047 	xs = scsi_xs_get(link, flags);
1048 	if (xs == NULL)
1049 		return ENOMEM;
1050 	xs->cmdlen = sizeof(*cmd);
1051 	xs->retries = 2;
1052 	xs->timeout = (type == SSS_START) ? 30000 : 10000;
1053 
1054 	cmd = (struct scsi_start_stop *)&xs->cmd;
1055 	cmd->opcode = START_STOP;
1056 	cmd->how = type;
1057 
1058 	error = scsi_xs_sync(xs);
1059 	scsi_xs_put(xs);
1060 
1061 	return error;
1062 }
1063 
1064 int
1065 scsi_mode_sense(struct scsi_link *link, int pg_code,
1066     union scsi_mode_sense_buf *data, int flags)
1067 {
1068 	struct scsi_mode_sense	*cmd;
1069 	struct scsi_xfer	*xs;
1070 	size_t			 len;
1071 	int			 error;
1072 #ifdef SCSIDEBUG
1073 	size_t			 bytes;
1074 #endif /* SCSIDEBUG */
1075 
1076 	len = sizeof(*data);
1077 
1078 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN);
1079 	if (xs == NULL)
1080 		return ENOMEM;
1081 	xs->cmdlen = sizeof(*cmd);
1082 	xs->data = (void *)data;
1083 	xs->datalen = len;
1084 	xs->timeout = 20000;
1085 
1086 	/*
1087 	 * Make sure the sense buffer is clean before we do the mode sense, so
1088 	 * that checks for bogus values of 0 will work in case the mode sense
1089 	 * fails.
1090 	 */
1091 	memset(data, 0, len);
1092 
1093 	cmd = (struct scsi_mode_sense *)&xs->cmd;
1094 	cmd->opcode = MODE_SENSE;
1095 	cmd->page = pg_code;
1096 
1097 	if (len > 0xff)
1098 		len = 0xff;
1099 	cmd->length = len;
1100 
1101 	error = scsi_xs_sync(xs);
1102 	scsi_xs_put(xs);
1103 
1104 	if (error == 0 && !VALID_MODE_HDR(&data->hdr))
1105 		error = EIO;
1106 
1107 #ifdef SCSIDEBUG
1108 	sc_print_addr(link);
1109 	if (error == 0) {
1110 		bytes = sizeof(data->hdr.data_length) + data->hdr.data_length;
1111 		if (bytes < len)
1112 			len = bytes;
1113 		printf("got %zu of %zu bytes of mode sense (6) page %d data:\n",
1114 		    len, bytes, pg_code);
1115 		scsi_show_mem((u_char *)data, len);
1116 	} else
1117 		printf("mode sense (6) page %d not available\n", pg_code);
1118 #endif /* SCSIDEBUG */
1119 
1120 	return error;
1121 }
1122 
1123 int
1124 scsi_mode_sense_big(struct scsi_link *link, int pg_code,
1125     union scsi_mode_sense_buf *data, int flags)
1126 {
1127 	struct scsi_mode_sense_big	*cmd;
1128 	struct scsi_xfer		*xs;
1129 	size_t				 len;
1130 	int				 error;
1131 #ifdef SCSIDEBUG
1132 	size_t				 bytes;
1133 #endif /* SCSIDEBUG */
1134 
1135 	len = sizeof(*data);
1136 
1137 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN);
1138 	if (xs == NULL)
1139 		return ENOMEM;
1140 	xs->cmdlen = sizeof(*cmd);
1141 	xs->data = (void *)data;
1142 	xs->datalen = len;
1143 	xs->timeout = 20000;
1144 
1145 	/*
1146 	 * Make sure the sense buffer is clean before we do the mode sense, so
1147 	 * that checks for bogus values of 0 will work in case the mode sense
1148 	 * fails.
1149 	 */
1150 	memset(data, 0, len);
1151 
1152 	cmd = (struct scsi_mode_sense_big *)&xs->cmd;
1153 	cmd->opcode = MODE_SENSE_BIG;
1154 	cmd->page = pg_code;
1155 
1156 	if (len > 0xffff)
1157 		len = 0xffff;
1158 	_lto2b(len, cmd->length);
1159 
1160 	error = scsi_xs_sync(xs);
1161 	scsi_xs_put(xs);
1162 
1163 	if (error == 0 && !VALID_MODE_HDR_BIG(&data->hdr_big))
1164 		error = EIO;
1165 
1166 #ifdef SCSIDEBUG
1167 	sc_print_addr(link);
1168 	if (error == 0) {
1169 		bytes = sizeof(data->hdr_big.data_length) +
1170 		    _2btol(data->hdr_big.data_length);
1171 		if (bytes < len)
1172 			len = bytes;
1173 		printf("got %zu bytes of %zu bytes of mode sense (10) page %d "
1174 		    "data:\n", len, bytes, pg_code);
1175 		scsi_show_mem((u_char *)data, len);
1176 	} else
1177 		printf("mode sense (10) page %d not available\n", pg_code);
1178 #endif /* SCSIDEBUG */
1179 
1180 	return error;
1181 }
1182 
1183 void *
1184 scsi_mode_sense_page(struct scsi_mode_header *hdr, int pg_code, int pg_length)
1185 {
1186 	u_int8_t	*page;
1187 	int		 total_length, header_length;
1188 
1189 	total_length = hdr->data_length + sizeof(hdr->data_length);
1190 	header_length = sizeof(*hdr) + hdr->blk_desc_len;
1191 	page = (u_int8_t *)hdr + header_length;
1192 
1193 	if ((total_length - header_length) < pg_length)
1194 		return NULL;
1195 
1196 	if ((*page & SMS_PAGE_CODE) != pg_code)
1197 		return NULL;
1198 
1199 	return page;
1200 }
1201 
1202 void *
1203 scsi_mode_sense_big_page(struct scsi_mode_header_big *hdr, int pg_code,
1204     int pg_length)
1205 {
1206 	u_int8_t	*page;
1207 	int		 total_length, header_length;
1208 
1209 	total_length = _2btol(hdr->data_length) + sizeof(hdr->data_length);
1210 	header_length = sizeof(*hdr) + _2btol(hdr->blk_desc_len);
1211 	page = (u_int8_t *)hdr + header_length;
1212 
1213 	if ((total_length - header_length) < pg_length)
1214 		return NULL;
1215 
1216 	if ((*page & SMS_PAGE_CODE) != pg_code)
1217 		return NULL;
1218 
1219 	return page;
1220 }
1221 
1222 void
1223 scsi_parse_blkdesc(struct scsi_link *link, union scsi_mode_sense_buf *buf,
1224     int big, u_int32_t *density, u_int64_t *block_count, u_int32_t *block_size)
1225 {
1226 	struct scsi_direct_blk_desc	*direct;
1227 	struct scsi_blk_desc		*general;
1228 	size_t				 offset;
1229 	unsigned int			 blk_desc_len;
1230 
1231 	if (big == 0) {
1232 		offset = sizeof(struct scsi_mode_header);
1233 		blk_desc_len = buf->hdr.blk_desc_len;
1234 	} else {
1235 		offset = sizeof(struct scsi_mode_header_big);
1236 		blk_desc_len = _2btol(buf->hdr_big.blk_desc_len);
1237 	}
1238 
1239 	/* Both scsi_blk_desc and scsi_direct_blk_desc are 8 bytes. */
1240 	if (blk_desc_len == 0 || (blk_desc_len % 8 != 0))
1241 		return;
1242 
1243 	switch (link->inqdata.device & SID_TYPE) {
1244 	case T_SEQUENTIAL:
1245 		/*
1246 		 * XXX What other device types return general block descriptors?
1247 		 */
1248 		general = (struct scsi_blk_desc *)&buf->buf[offset];
1249 		if (density != NULL)
1250 			*density = general->density;
1251 		if (block_size != NULL)
1252 			*block_size = _3btol(general->blklen);
1253 		if (block_count != NULL)
1254 			*block_count = (u_int64_t)_3btol(general->nblocks);
1255 		break;
1256 
1257 	default:
1258 		direct = (struct scsi_direct_blk_desc *)&buf->buf[offset];
1259 		if (density != NULL)
1260 			*density = direct->density;
1261 		if (block_size != NULL)
1262 			*block_size = _3btol(direct->blklen);
1263 		if (block_count != NULL)
1264 			*block_count = (u_int64_t)_4btol(direct->nblocks);
1265 		break;
1266 	}
1267 }
1268 
1269 int
1270 scsi_do_mode_sense(struct scsi_link *link, int pg_code,
1271     union scsi_mode_sense_buf *buf, void **page_data,
1272     int pg_length, int flags, int *big)
1273 {
1274 	int error = 0;
1275 
1276 	*page_data = NULL;
1277 	*big = 0;
1278 
1279 	if (!ISSET(link->flags, SDEV_ATAPI) ||
1280 	    (link->inqdata.device & SID_TYPE) == T_SEQUENTIAL) {
1281 		/*
1282 		 * Try 6 byte mode sense request first. Some devices don't
1283 		 * distinguish between 6 and 10 byte MODE SENSE commands,
1284 		 * returning 6 byte data for 10 byte requests. ATAPI tape
1285 		 * drives use MODE SENSE (6) even though ATAPI uses 10 byte
1286 		 * everything else. Don't bother with SMS_DBD. Check returned
1287 		 * data length to ensure that at least a header (3 additional
1288 		 * bytes) is returned.
1289 		 */
1290 		error = scsi_mode_sense(link, pg_code, buf, flags);
1291 		if (error == 0) {
1292 			/*
1293 			 * Page data may be invalid (e.g. all zeros) but we
1294 			 * accept the device's word that this is the best it can
1295 			 * do. Some devices will freak out if their word is not
1296 			 * accepted and MODE_SENSE_BIG is attempted.
1297 			 */
1298 			*page_data = scsi_mode_sense_page(&buf->hdr, pg_code,
1299 			    pg_length);
1300 			return 0;
1301 		}
1302 	}
1303 
1304 	/*
1305 	 * non-ATAPI, non-USB devices that don't support SCSI-2 commands
1306 	 * (i.e. MODE SENSE (10)) are done.
1307 	 */
1308 	if (!ISSET(link->flags, (SDEV_ATAPI | SDEV_UMASS)) &&
1309 	    SID_ANSII_REV(&link->inqdata) < SCSI_REV_2)
1310 		return error;
1311 
1312 	/*
1313 	 * Try 10 byte mode sense request.
1314 	 */
1315 	error = scsi_mode_sense_big(link, pg_code, buf, flags);
1316 	if (error != 0)
1317 		return error;
1318 
1319 	*big = 1;
1320 	*page_data = scsi_mode_sense_big_page(&buf->hdr_big, pg_code,
1321 	    pg_length);
1322 
1323 	return 0;
1324 }
1325 
1326 int
1327 scsi_mode_select(struct scsi_link *link, int byte2,
1328     struct scsi_mode_header *data, int flags, int timeout)
1329 {
1330 	struct scsi_mode_select		*cmd;
1331 	struct scsi_xfer		*xs;
1332 	int				 error;
1333 	u_int32_t			 len;
1334 
1335 	len = data->data_length + 1; /* 1 == sizeof(data_length) */
1336 
1337 	xs = scsi_xs_get(link, flags | SCSI_DATA_OUT);
1338 	if (xs == NULL)
1339 		return ENOMEM;
1340 	xs->cmdlen = sizeof(*cmd);
1341 	xs->data = (void *)data;
1342 	xs->datalen = len;
1343 	xs->timeout = timeout;
1344 
1345 	cmd = (struct scsi_mode_select *)&xs->cmd;
1346 	cmd->opcode = MODE_SELECT;
1347 	cmd->byte2 = byte2;
1348 	cmd->length = len;
1349 
1350 	/* Length is reserved when doing mode select so zero it. */
1351 	data->data_length = 0;
1352 
1353 	error = scsi_xs_sync(xs);
1354 	scsi_xs_put(xs);
1355 
1356 	SC_DEBUG(link, SDEV_DB2, ("scsi_mode_select: error = %d\n", error));
1357 
1358 	return error;
1359 }
1360 
1361 int
1362 scsi_mode_select_big(struct scsi_link *link, int byte2,
1363     struct scsi_mode_header_big *data, int flags, int timeout)
1364 {
1365 	struct scsi_mode_select_big	*cmd;
1366 	struct scsi_xfer		*xs;
1367 	int				 error;
1368 	u_int32_t			 len;
1369 
1370 	len = _2btol(data->data_length) + 2; /* 2 == sizeof data_length */
1371 
1372 	xs = scsi_xs_get(link, flags | SCSI_DATA_OUT);
1373 	if (xs == NULL)
1374 		return ENOMEM;
1375 	xs->cmdlen = sizeof(*cmd);
1376 	xs->data = (void *)data;
1377 	xs->datalen = len;
1378 	xs->timeout = timeout;
1379 
1380 	cmd = (struct scsi_mode_select_big *)&xs->cmd;
1381 	cmd->opcode = MODE_SELECT_BIG;
1382 	cmd->byte2 = byte2;
1383 	_lto2b(len, cmd->length);
1384 
1385 	/* Length is reserved when doing mode select so zero it. */
1386 	_lto2b(0, data->data_length);
1387 
1388 	error = scsi_xs_sync(xs);
1389 	scsi_xs_put(xs);
1390 
1391 	SC_DEBUG(link, SDEV_DB2, ("scsi_mode_select_big: error = %d\n",
1392 	    error));
1393 
1394 	return error;
1395 }
1396 
1397 int
1398 scsi_report_luns(struct scsi_link *link, int selectreport,
1399     struct scsi_report_luns_data *data, u_int32_t datalen, int flags,
1400     int timeout)
1401 {
1402 	struct scsi_report_luns		*cmd;
1403 	struct scsi_xfer		*xs;
1404 	int				 error;
1405 
1406 	xs = scsi_xs_get(link, flags | SCSI_DATA_IN);
1407 	if (xs == NULL)
1408 		return ENOMEM;
1409 	xs->cmdlen = sizeof(*cmd);
1410 	xs->data = (void *)data;
1411 	xs->datalen = datalen;
1412 	xs->timeout = timeout;
1413 
1414 	bzero(data, datalen);
1415 
1416 	cmd = (struct scsi_report_luns *)&xs->cmd;
1417 	cmd->opcode = REPORT_LUNS;
1418 	cmd->selectreport = selectreport;
1419 	_lto4b(datalen, cmd->length);
1420 
1421 	error = scsi_xs_sync(xs);
1422 	scsi_xs_put(xs);
1423 
1424 	SC_DEBUG(link, SDEV_DB2, ("scsi_report_luns: error = %d\n", error));
1425 
1426 	return error;
1427 }
1428 
1429 void
1430 scsi_xs_exec(struct scsi_xfer *xs)
1431 {
1432 	xs->error = XS_NOERROR;
1433 	xs->resid = xs->datalen;
1434 	xs->status = 0;
1435 	CLR(xs->flags, ITSDONE);
1436 
1437 #ifdef SCSIDEBUG
1438 	scsi_show_xs(xs);
1439 #endif /* SCSIDEBUG */
1440 
1441 	/* The adapter's scsi_cmd() is responsible for calling scsi_done(). */
1442 	KERNEL_LOCK();
1443 	xs->sc_link->bus->sb_adapter->scsi_cmd(xs);
1444 	KERNEL_UNLOCK();
1445 }
1446 
1447 /*
1448  * Used by device drivers that fake various scsi commands.
1449  */
1450 void
1451 scsi_copy_internal_data(struct scsi_xfer *xs, void *data, size_t datalen)
1452 {
1453 	size_t copy_cnt;
1454 
1455 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("scsi_copy_internal_data\n"));
1456 
1457 	if (xs->datalen == 0) {
1458 		sc_print_addr(xs->sc_link);
1459 		printf("uio internal data copy not supported\n");
1460 	} else {
1461 		copy_cnt = MIN(datalen, xs->datalen);
1462 		memcpy(xs->data, data, copy_cnt);
1463 		xs->resid = xs->datalen - copy_cnt;
1464 	}
1465 }
1466 
1467 /*
1468  * This routine is called by the adapter when its xs handling is done.
1469  */
1470 void
1471 scsi_done(struct scsi_xfer *xs)
1472 {
1473 #ifdef SCSIDEBUG
1474 	if (ISSET(xs->sc_link->flags, SDEV_DB1)) {
1475 		if (xs->datalen && ISSET(xs->flags, SCSI_DATA_IN))
1476 			scsi_show_mem(xs->data, min(64, xs->datalen));
1477 	}
1478 #endif /* SCSIDEBUG */
1479 
1480 	SET(xs->flags, ITSDONE);
1481 	KERNEL_LOCK();
1482 	xs->done(xs);
1483 	KERNEL_UNLOCK();
1484 }
1485 
1486 int
1487 scsi_xs_sync(struct scsi_xfer *xs)
1488 {
1489 	struct mutex	cookie = MUTEX_INITIALIZER(IPL_BIO);
1490 	int		error;
1491 
1492 #ifdef DIAGNOSTIC
1493 	if (xs->cookie != NULL)
1494 		panic("xs->cookie != NULL in scsi_xs_sync");
1495 	if (xs->done != NULL)
1496 		panic("xs->done != NULL in scsi_xs_sync");
1497 #endif /* DIAGNOSTIC */
1498 
1499 	/*
1500 	 * If we cant sleep while waiting for completion, get the adapter to
1501 	 * complete it for us.
1502 	 */
1503 	if (ISSET(xs->flags, SCSI_NOSLEEP))
1504 		SET(xs->flags, SCSI_POLL);
1505 
1506 	xs->done = scsi_xs_sync_done;
1507 
1508 	do {
1509 		xs->cookie = &cookie;
1510 
1511 		scsi_xs_exec(xs);
1512 
1513 		mtx_enter(&cookie);
1514 		while (xs->cookie != NULL)
1515 			msleep_nsec(xs, &cookie, PRIBIO, "syncxs", INFSLP);
1516 		mtx_leave(&cookie);
1517 
1518 		error = scsi_xs_error(xs);
1519 	} while (error == ERESTART);
1520 
1521 	return error;
1522 }
1523 
1524 void
1525 scsi_xs_sync_done(struct scsi_xfer *xs)
1526 {
1527 	struct mutex *cookie = xs->cookie;
1528 
1529 	if (cookie == NULL)
1530 		panic("scsi_done called twice on xs(%p)", xs);
1531 
1532 	mtx_enter(cookie);
1533 	xs->cookie = NULL;
1534 	if (!ISSET(xs->flags, SCSI_NOSLEEP))
1535 		wakeup_one(xs);
1536 	mtx_leave(cookie);
1537 }
1538 
1539 int
1540 scsi_xs_error(struct scsi_xfer *xs)
1541 {
1542 	int error = EIO;
1543 
1544 	SC_DEBUG(xs->sc_link, SDEV_DB3, ("scsi_xs_error,err = 0x%x\n",
1545 	    xs->error));
1546 
1547 	if (ISSET(xs->sc_link->state, SDEV_S_DYING))
1548 		return ENXIO;
1549 
1550 	switch (xs->error) {
1551 	case XS_NOERROR:	/* nearly always hit this one */
1552 		error = 0;
1553 		break;
1554 
1555 	case XS_SENSE:
1556 	case XS_SHORTSENSE:
1557 		SC_DEBUG_SENSE(xs);
1558 		error = xs->sc_link->interpret_sense(xs);
1559 		SC_DEBUG(xs->sc_link, SDEV_DB3,
1560 		    ("scsi_interpret_sense returned %#x\n", error));
1561 		break;
1562 
1563 	case XS_BUSY:
1564 		error = scsi_delay(xs, 1);
1565 		break;
1566 
1567 	case XS_TIMEOUT:
1568 	case XS_RESET:
1569 		error = ERESTART;
1570 		break;
1571 
1572 	case XS_DRIVER_STUFFUP:
1573 	case XS_SELTIMEOUT:
1574 		break;
1575 
1576 	default:
1577 		sc_print_addr(xs->sc_link);
1578 		printf("unknown error category (0x%x) from scsi driver\n",
1579 		    xs->error);
1580 		break;
1581 	}
1582 
1583 	if (error == ERESTART && xs->retries-- < 1)
1584 		return EIO;
1585 	else
1586 		return error;
1587 }
1588 
1589 int
1590 scsi_delay(struct scsi_xfer *xs, int seconds)
1591 {
1592 	int ret;
1593 
1594 	switch (xs->flags & (SCSI_POLL | SCSI_NOSLEEP)) {
1595 	case SCSI_POLL:
1596 		delay(1000000 * seconds);
1597 		return ERESTART;
1598 	case SCSI_NOSLEEP:
1599 		/* Retry the command immediately since we can't delay. */
1600 		return ERESTART;
1601 	case (SCSI_POLL | SCSI_NOSLEEP):
1602 		/* Invalid combination! */
1603 		return EIO;
1604 	}
1605 
1606 	ret = tsleep_nsec(&ret, PRIBIO|PCATCH, "scbusy", SEC_TO_NSEC(seconds));
1607 
1608 	/* Signal == abort xs. */
1609 	if (ret == ERESTART || ret == EINTR)
1610 		return EIO;
1611 
1612 	return ERESTART;
1613 }
1614 
1615 /*
1616  * Look at the returned sense and act on the error, determining
1617  * the unix error number to pass back.  (0 = report no error)
1618  *
1619  * THIS IS THE DEFAULT ERROR HANDLER
1620  */
1621 int
1622 scsi_interpret_sense(struct scsi_xfer *xs)
1623 {
1624 	struct scsi_sense_data			*sense = &xs->sense;
1625 	struct scsi_link			*link = xs->sc_link;
1626 	u_int8_t				serr, skey;
1627 	int					error;
1628 
1629 	/* Default sense interpretation. */
1630 	serr = sense->error_code & SSD_ERRCODE;
1631 	if (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)
1632 		skey = 0xff;	/* Invalid value, since key is 4 bit value. */
1633 	else
1634 		skey = sense->flags & SSD_KEY;
1635 
1636 	/*
1637 	 * Interpret the key/asc/ascq information where appropriate.
1638 	 */
1639 	error = 0;
1640 	switch (skey) {
1641 	case SKEY_NO_SENSE:
1642 	case SKEY_RECOVERED_ERROR:
1643 		if (xs->resid == xs->datalen)
1644 			xs->resid = 0;	/* not short read */
1645 		break;
1646 	case SKEY_BLANK_CHECK:
1647 	case SKEY_EQUAL:
1648 		break;
1649 	case SKEY_NOT_READY:
1650 		if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY))
1651 			return 0;
1652 		error = EIO;
1653 		if (xs->retries) {
1654 			switch (ASC_ASCQ(sense)) {
1655 			case SENSE_NOT_READY_BECOMING_READY:
1656 			case SENSE_NOT_READY_FORMAT:
1657 			case SENSE_NOT_READY_REBUILD:
1658 			case SENSE_NOT_READY_RECALC:
1659 			case SENSE_NOT_READY_INPROGRESS:
1660 			case SENSE_NOT_READY_LONGWRITE:
1661 			case SENSE_NOT_READY_SELFTEST:
1662 			case SENSE_NOT_READY_INIT_REQUIRED:
1663 				SC_DEBUG(link, SDEV_DB1,
1664 				    ("not ready (ASC_ASCQ == %#x)\n",
1665 				    ASC_ASCQ(sense)));
1666 				return scsi_delay(xs, 1);
1667 			case SENSE_NOMEDIUM:
1668 			case SENSE_NOMEDIUM_TCLOSED:
1669 			case SENSE_NOMEDIUM_TOPEN:
1670 			case SENSE_NOMEDIUM_LOADABLE:
1671 			case SENSE_NOMEDIUM_AUXMEM:
1672 				CLR(link->flags, SDEV_MEDIA_LOADED);
1673 				error = ENOMEDIUM;
1674 				break;
1675 			default:
1676 				break;
1677 			}
1678 		}
1679 		break;
1680 	case SKEY_MEDIUM_ERROR:
1681 		switch (ASC_ASCQ(sense)) {
1682 		case SENSE_NOMEDIUM:
1683 		case SENSE_NOMEDIUM_TCLOSED:
1684 		case SENSE_NOMEDIUM_TOPEN:
1685 		case SENSE_NOMEDIUM_LOADABLE:
1686 		case SENSE_NOMEDIUM_AUXMEM:
1687 			CLR(link->flags, SDEV_MEDIA_LOADED);
1688 			error = ENOMEDIUM;
1689 			break;
1690 		case SENSE_BAD_MEDIUM:
1691 		case SENSE_NR_MEDIUM_UNKNOWN_FORMAT:
1692 		case SENSE_NR_MEDIUM_INCOMPATIBLE_FORMAT:
1693 		case SENSE_NW_MEDIUM_UNKNOWN_FORMAT:
1694 		case SENSE_NW_MEDIUM_INCOMPATIBLE_FORMAT:
1695 		case SENSE_NF_MEDIUM_INCOMPATIBLE_FORMAT:
1696 		case SENSE_NW_MEDIUM_AC_MISMATCH:
1697 			error = EMEDIUMTYPE;
1698 			break;
1699 		default:
1700 			error = EIO;
1701 			break;
1702 		}
1703 		break;
1704 	case SKEY_ILLEGAL_REQUEST:
1705 		if (ISSET(xs->flags, SCSI_IGNORE_ILLEGAL_REQUEST))
1706 			return 0;
1707 		if (ASC_ASCQ(sense) == SENSE_MEDIUM_REMOVAL_PREVENTED)
1708 			return EBUSY;
1709 		error = EINVAL;
1710 		break;
1711 	case SKEY_UNIT_ATTENTION:
1712 		switch (ASC_ASCQ(sense)) {
1713 		case SENSE_POWER_RESET_OR_BUS:
1714 		case SENSE_POWER_ON:
1715 		case SENSE_BUS_RESET:
1716 		case SENSE_BUS_DEVICE_RESET:
1717 		case SENSE_DEVICE_INTERNAL_RESET:
1718 		case SENSE_TSC_CHANGE_SE:
1719 		case SENSE_TSC_CHANGE_LVD:
1720 		case SENSE_IT_NEXUS_LOSS:
1721 			return scsi_delay(xs, 1);
1722 		default:
1723 			break;
1724 		}
1725 		if (ISSET(link->flags, SDEV_REMOVABLE))
1726 			CLR(link->flags, SDEV_MEDIA_LOADED);
1727 		if (ISSET(xs->flags, SCSI_IGNORE_MEDIA_CHANGE) ||
1728 		    /* XXX Should reupload any transient state. */
1729 		    !ISSET(link->flags, SDEV_REMOVABLE)) {
1730 			return scsi_delay(xs, 1);
1731 		}
1732 		error = EIO;
1733 		break;
1734 	case SKEY_WRITE_PROTECT:
1735 		error = EROFS;
1736 		break;
1737 	case SKEY_ABORTED_COMMAND:
1738 		error = ERESTART;
1739 		break;
1740 	case SKEY_VOLUME_OVERFLOW:
1741 		error = ENOSPC;
1742 		break;
1743 	case SKEY_HARDWARE_ERROR:
1744 		if (ASC_ASCQ(sense) == SENSE_CARTRIDGE_FAULT)
1745 			return EMEDIUMTYPE;
1746 		error = EIO;
1747 		break;
1748 	default:
1749 		error = EIO;
1750 		break;
1751 	}
1752 
1753 #ifndef SCSIDEBUG
1754 	/* SCSIDEBUG would mean it has already been printed. */
1755 	if (skey && !ISSET(xs->flags, SCSI_SILENT))
1756 		scsi_print_sense(xs);
1757 #endif /* ~SCSIDEBUG */
1758 
1759 	return error;
1760 }
1761 
1762 /*
1763  * Utility routines often used in SCSI stuff
1764  */
1765 
1766 
1767 /*
1768  * Print out the scsi_link structure's address info.
1769  */
1770 void
1771 sc_print_addr(struct scsi_link *link)
1772 {
1773 	struct device *adapter_device = link->bus->sc_dev.dv_parent;
1774 
1775 	printf("%s(%s:%d:%d): ",
1776 	    link->device_softc ?
1777 	    ((struct device *)link->device_softc)->dv_xname : "probe",
1778 	    adapter_device->dv_xname,
1779 	    link->target, link->lun);
1780 }
1781 
1782 static const char *sense_keys[16] = {
1783 	"No Additional Sense",
1784 	"Soft Error",
1785 	"Not Ready",
1786 	"Media Error",
1787 	"Hardware Error",
1788 	"Illegal Request",
1789 	"Unit Attention",
1790 	"Write Protected",
1791 	"Blank Check",
1792 	"Vendor Unique",
1793 	"Copy Aborted",
1794 	"Aborted Command",
1795 	"Equal Error",
1796 	"Volume Overflow",
1797 	"Miscompare Error",
1798 	"Reserved"
1799 };
1800 
1801 #ifdef SCSITERSE
1802 static __inline void
1803 asc2ascii(u_int8_t asc, u_int8_t ascq, char *result, size_t len)
1804 {
1805 	snprintf(result, len, "ASC 0x%02x ASCQ 0x%02x", asc, ascq);
1806 }
1807 #else
1808 static const struct {
1809 	u_int8_t	 asc, ascq;
1810 	char		*description;
1811 } adesc[] = {
1812 	/* www.t10.org/lists/asc-num.txt as of 11/15/10. */
1813 	{ 0x00, 0x00, "No Additional Sense Information" },
1814 	{ 0x00, 0x01, "Filemark Detected" },
1815 	{ 0x00, 0x02, "End-Of-Partition/Medium Detected" },
1816 	{ 0x00, 0x03, "Setmark Detected" },
1817 	{ 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
1818 	{ 0x00, 0x05, "End-Of-Data Detected" },
1819 	{ 0x00, 0x06, "I/O Process Terminated" },
1820 	{ 0x00, 0x11, "Audio Play Operation In Progress" },
1821 	{ 0x00, 0x12, "Audio Play Operation Paused" },
1822 	{ 0x00, 0x13, "Audio Play Operation Successfully Completed" },
1823 	{ 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
1824 	{ 0x00, 0x15, "No Current Audio Status To Return" },
1825 	{ 0x00, 0x16, "Operation In Progress" },
1826 	{ 0x00, 0x17, "Cleaning Requested" },
1827 	{ 0x00, 0x18, "Erase Operation In Progress" },
1828 	{ 0x00, 0x19, "Locate Operation In Progress" },
1829 	{ 0x00, 0x1A, "Rewind Operation In Progress" },
1830 	{ 0x00, 0x1B, "Set Capacity Operation In Progress" },
1831 	{ 0x00, 0x1C, "Verify Operation In Progress" },
1832 	{ 0x01, 0x00, "No Index/Sector Signal" },
1833 	{ 0x02, 0x00, "No Seek Complete" },
1834 	{ 0x03, 0x00, "Peripheral Device Write Fault" },
1835 	{ 0x03, 0x01, "No Write Current" },
1836 	{ 0x03, 0x02, "Excessive Write Errors" },
1837 	{ 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
1838 	{ 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
1839 	{ 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
1840 	{ 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
1841 	{ 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
1842 	{ 0x04, 0x05, "Logical Unit Not Ready, Rebuild In Progress" },
1843 	{ 0x04, 0x06, "Logical Unit Not Ready, Recalculation In Progress" },
1844 	{ 0x04, 0x07, "Logical Unit Not Ready, Operation In Progress" },
1845 	{ 0x04, 0x08, "Logical Unit Not Ready, Long Write In Progress" },
1846 	{ 0x04, 0x09, "Logical Unit Not Ready, Self-Test In Progress" },
1847 	{ 0x04, 0x0A, "Logical Unit Not Accessible, Asymmetric Access State Transition" },
1848 	{ 0x04, 0x0B, "Logical Unit Not Accessible, Target Port In Standby State" },
1849 	{ 0x04, 0x0C, "Logical Unit Not Accessible, Target Port In Unavailable State" },
1850 	{ 0x04, 0x0D, "Logical Unit Not Ready, Structure Check Required" },
1851 	{ 0x04, 0x10, "Logical Unit Not Ready, Auxiliary Memory Not Accessible" },
1852 	{ 0x04, 0x11, "Logical Unit Not Ready, Notify (Enable Spinup) Required" },
1853 	{ 0x04, 0x12, "Logical Unit Not Ready, Offline" },
1854 	{ 0x04, 0x13, "Logical Unit Not Ready, SA Creation In Progress" },
1855 	{ 0x04, 0x14, "Logical Unit Not Ready, Space Allocation In Progress" },
1856 	{ 0x04, 0x15, "Logical Unit Not Ready, Robotics Disabled" },
1857 	{ 0x04, 0x16, "Logical Unit Not Ready, Configuration Required" },
1858 	{ 0x04, 0x17, "Logical Unit Not Ready, Calibration Required" },
1859 	{ 0x04, 0x18, "Logical Unit Not Ready, A Door Is Open" },
1860 	{ 0x04, 0x19, "Logical Unit Not Ready, Operating In Sequential Mode" },
1861 	{ 0x04, 0x1A, "Logical Unit Not Ready, Start Stop Unit Command In Progress" },
1862 	{ 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
1863 	{ 0x06, 0x00, "No Reference Position Found" },
1864 	{ 0x07, 0x00, "Multiple Peripheral Devices Selected" },
1865 	{ 0x08, 0x00, "Logical Unit Communication Failure" },
1866 	{ 0x08, 0x01, "Logical Unit Communication Timeout" },
1867 	{ 0x08, 0x02, "Logical Unit Communication Parity Error" },
1868 	{ 0x08, 0x03, "Logical Unit Communication CRC Error (ULTRA-DMA/32)" },
1869 	{ 0x08, 0x04, "Unreachable Copy Target" },
1870 	{ 0x09, 0x00, "Track Following Error" },
1871 	{ 0x09, 0x01, "Tracking Servo Failure" },
1872 	{ 0x09, 0x02, "Focus Servo Failure" },
1873 	{ 0x09, 0x03, "Spindle Servo Failure" },
1874 	{ 0x09, 0x04, "Head Select Fault" },
1875 	{ 0x0A, 0x00, "Error Log Overflow" },
1876 	{ 0x0B, 0x00, "Warning" },
1877 	{ 0x0B, 0x01, "Warning - Specified Temperature Exceeded" },
1878 	{ 0x0B, 0x02, "Warning - Enclosure Degraded" },
1879 	{ 0x0B, 0x03, "Warning - Background Self-Test Failed" },
1880 	{ 0x0B, 0x04, "Warning - Background Pre-Scan Detected Medium Error" },
1881 	{ 0x0B, 0x05, "Warning - Background Medium Scan Detected Medium Error" },
1882 	{ 0x0B, 0x06, "Warning - Non-Volatile Cache Now Volatile" },
1883 	{ 0x0B, 0x07, "Warning - Degraded Power To Non-Volatile Cache" },
1884 	{ 0x0B, 0x08, "Warning - Power Loss Expected" },
1885 	{ 0x0C, 0x00, "Write Error" },
1886 	{ 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
1887 	{ 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
1888 	{ 0x0C, 0x03, "Write Error - Recommend Reassignment" },
1889 	{ 0x0C, 0x04, "Compression Check Miscompare Error" },
1890 	{ 0x0C, 0x05, "Data Expansion Occurred During Compression" },
1891 	{ 0x0C, 0x06, "Block Not Compressible" },
1892 	{ 0x0C, 0x07, "Write Error - Recovery Needed" },
1893 	{ 0x0C, 0x08, "Write Error - Recovery Failed" },
1894 	{ 0x0C, 0x09, "Write Error - Loss Of Streaming" },
1895 	{ 0x0C, 0x0A, "Write Error - Padding Blocks Added" },
1896 	{ 0x0C, 0x0B, "Auxiliary Memory Write Error" },
1897 	{ 0x0C, 0x0C, "Write Error - Unexpected Unsolicited Data" },
1898 	{ 0x0C, 0x0D, "Write Error - Not Enough Unsolicited Data" },
1899 	{ 0x0C, 0x0F, "Defects In Error Window" },
1900 	{ 0x0D, 0x00, "Error Detected By Third Party Temporary Initiator" },
1901 	{ 0x0D, 0x01, "Third Party Device Failure" },
1902 	{ 0x0D, 0x02, "Copy Target Device Not Reachable" },
1903 	{ 0x0D, 0x03, "Incorrect Copy Target Device Type" },
1904 	{ 0x0D, 0x04, "Copy Target Device Data Underrun" },
1905 	{ 0x0D, 0x05, "Copy Target Device Data Overrun" },
1906 	{ 0x0E, 0x00, "Invalid Information Unit" },
1907 	{ 0x0E, 0x01, "Information Unit Too Short" },
1908 	{ 0x0E, 0x02, "Information Unit Too Long" },
1909 	{ 0x10, 0x00, "ID CRC Or ECC Error" },
1910 	{ 0x10, 0x01, "Logical Block Guard Check Failed" },
1911 	{ 0x10, 0x02, "Logical Block Application Tag Check Failed" },
1912 	{ 0x10, 0x03, "Logical Block Reference Tag Check Failed" },
1913 	{ 0x10, 0x04, "Logical Block Protection Error On Recover Buffered Data" },
1914 	{ 0x10, 0x05, "Logical Block Protection Method Error" },
1915 	{ 0x11, 0x00, "Unrecovered Read Error" },
1916 	{ 0x11, 0x01, "Read Retries Exhausted" },
1917 	{ 0x11, 0x02, "Error Too Long To Correct" },
1918 	{ 0x11, 0x03, "Multiple Read Errors" },
1919 	{ 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
1920 	{ 0x11, 0x05, "L-EC Uncorrectable Error" },
1921 	{ 0x11, 0x06, "CIRC Unrecovered Error" },
1922 	{ 0x11, 0x07, "Data Resynchronization Error" },
1923 	{ 0x11, 0x08, "Incomplete Block Read" },
1924 	{ 0x11, 0x09, "No Gap Found" },
1925 	{ 0x11, 0x0A, "Miscorrected Error" },
1926 	{ 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
1927 	{ 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite The Data" },
1928 	{ 0x11, 0x0D, "De-Compression CRC Error" },
1929 	{ 0x11, 0x0E, "Cannot Decompress Using Declared Algorithm" },
1930 	{ 0x11, 0x0F, "Error Reading UPC/EAN Number" },
1931 	{ 0x11, 0x10, "Error Reading ISRC Number" },
1932 	{ 0x11, 0x11, "Read Error - Loss Of Streaming" },
1933 	{ 0x11, 0x12, "Auxiliary Memory Read Error" },
1934 	{ 0x11, 0x13, "Read Error - Failed Retransmission Request" },
1935 	{ 0x11, 0x14, "Read Error - LBA Marked Bad By Application Client" },
1936 	{ 0x12, 0x00, "Address Mark Not Found for ID Field" },
1937 	{ 0x13, 0x00, "Address Mark Not Found for Data Field" },
1938 	{ 0x14, 0x00, "Recorded Entity Not Found" },
1939 	{ 0x14, 0x01, "Record Not Found" },
1940 	{ 0x14, 0x02, "Filemark or Setmark Not Found" },
1941 	{ 0x14, 0x03, "End-Of-Data Not Found" },
1942 	{ 0x14, 0x04, "Block Sequence Error" },
1943 	{ 0x14, 0x05, "Record Not Found - Recommend Reassignment" },
1944 	{ 0x14, 0x06, "Record Not Found - Data Auto-Reallocated" },
1945 	{ 0x14, 0x07, "Locate Operation Failure" },
1946 	{ 0x15, 0x00, "Random Positioning Error" },
1947 	{ 0x15, 0x01, "Mechanical Positioning Error" },
1948 	{ 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
1949 	{ 0x16, 0x00, "Data Synchronization Mark Error" },
1950 	{ 0x16, 0x01, "Data Sync Error - Data Rewritten" },
1951 	{ 0x16, 0x02, "Data Sync Error - Recommend Rewrite" },
1952 	{ 0x16, 0x03, "Data Sync Error - Data Auto-Reallocated" },
1953 	{ 0x16, 0x04, "Data Sync Error - Recommend Reassignment" },
1954 	{ 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
1955 	{ 0x17, 0x01, "Recovered Data With Retries" },
1956 	{ 0x17, 0x02, "Recovered Data With Positive Head Offset" },
1957 	{ 0x17, 0x03, "Recovered Data With Negative Head Offset" },
1958 	{ 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
1959 	{ 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
1960 	{ 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
1961 	{ 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
1962 	{ 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
1963 	{ 0x17, 0x09, "Recovered Data Without ECC - Data Rewritten" },
1964 	{ 0x18, 0x00, "Recovered Data With Error Correction Applied" },
1965 	{ 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
1966 	{ 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
1967 	{ 0x18, 0x03, "Recovered Data With CIRC" },
1968 	{ 0x18, 0x04, "Recovered Data With L-EC" },
1969 	{ 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
1970 	{ 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
1971 	{ 0x18, 0x07, "Recovered Data With ECC - Data Rewritten" },
1972 	{ 0x18, 0x08, "Recovered Data With Linking" },
1973 	{ 0x19, 0x00, "Defect List Error" },
1974 	{ 0x19, 0x01, "Defect List Not Available" },
1975 	{ 0x19, 0x02, "Defect List Error in Primary List" },
1976 	{ 0x19, 0x03, "Defect List Error in Grown List" },
1977 	{ 0x1A, 0x00, "Parameter List Length Error" },
1978 	{ 0x1B, 0x00, "Synchronous Data Transfer Error" },
1979 	{ 0x1C, 0x00, "Defect List Not Found" },
1980 	{ 0x1C, 0x01, "Primary Defect List Not Found" },
1981 	{ 0x1C, 0x02, "Grown Defect List Not Found" },
1982 	{ 0x1D, 0x00, "Miscompare During Verify Operation" },
1983 	{ 0x1D, 0x01, "Miscompare Verify Of Unmapped Lba" },
1984 	{ 0x1E, 0x00, "Recovered ID with ECC" },
1985 	{ 0x1F, 0x00, "Partial Defect List Transfer" },
1986 	{ 0x20, 0x00, "Invalid Command Operation Code" },
1987 	{ 0x20, 0x01, "Access Denied - Initiator Pending-Enrolled" },
1988 	{ 0x20, 0x02, "Access Denied - No Access rights" },
1989 	{ 0x20, 0x03, "Access Denied - Invalid Mgmt ID Key" },
1990 	{ 0x20, 0x04, "Illegal Command While In Write Capable State" },
1991 	{ 0x20, 0x05, "Obsolete" },
1992 	{ 0x20, 0x06, "Illegal Command While In Explicit Address Mode" },
1993 	{ 0x20, 0x07, "Illegal Command While In Implicit Address Mode" },
1994 	{ 0x20, 0x08, "Access Denied - Enrollment Conflict" },
1995 	{ 0x20, 0x09, "Access Denied - Invalid LU Identifier" },
1996 	{ 0x20, 0x0A, "Access Denied - Invalid Proxy Token" },
1997 	{ 0x20, 0x0B, "Access Denied - ACL LUN Conflict" },
1998 	{ 0x20, 0x0C, "Illegal Command When Not In Append-Only Mode" },
1999 	{ 0x21, 0x00, "Logical Block Address Out of Range" },
2000 	{ 0x21, 0x01, "Invalid Element Address" },
2001 	{ 0x21, 0x02, "Invalid Address For Write" },
2002 	{ 0x21, 0x03, "Invalid Write Crossing Layer Jump" },
2003 	{ 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
2004 	{ 0x24, 0x00, "Illegal Field in CDB" },
2005 	{ 0x24, 0x01, "CDB Decryption Error" },
2006 	{ 0x24, 0x02, "Obsolete" },
2007 	{ 0x24, 0x03, "Obsolete" },
2008 	{ 0x24, 0x04, "Security Audit Value Frozen" },
2009 	{ 0x24, 0x05, "Security Working Key Frozen" },
2010 	{ 0x24, 0x06, "Nonce Not Unique" },
2011 	{ 0x24, 0x07, "Nonce Timestamp Out Of Range" },
2012 	{ 0x24, 0x08, "Invalid XCDB" },
2013 	{ 0x25, 0x00, "Logical Unit Not Supported" },
2014 	{ 0x26, 0x00, "Invalid Field In Parameter List" },
2015 	{ 0x26, 0x01, "Parameter Not Supported" },
2016 	{ 0x26, 0x02, "Parameter Value Invalid" },
2017 	{ 0x26, 0x03, "Threshold Parameters Not Supported" },
2018 	{ 0x26, 0x04, "Invalid Release Of Persistent Reservation" },
2019 	{ 0x26, 0x05, "Data Decryption Error" },
2020 	{ 0x26, 0x06, "Too Many Target Descriptors" },
2021 	{ 0x26, 0x07, "Unsupported Target Descriptor Type Code" },
2022 	{ 0x26, 0x08, "Too Many Segment Descriptors" },
2023 	{ 0x26, 0x09, "Unsupported Segment Descriptor Type Code" },
2024 	{ 0x26, 0x0A, "Unexpected Inexact Segment" },
2025 	{ 0x26, 0x0B, "Inline Data Length Exceeded" },
2026 	{ 0x26, 0x0C, "Invalid Operation For Copy Source Or Destination" },
2027 	{ 0x26, 0x0D, "Copy Segment Granularity Violation" },
2028 	{ 0x26, 0x0E, "Invalid Parameter While Port Is Enabled" },
2029 	{ 0x26, 0x0F, "Invalid Data-Out Buffer Integrity Check Value" },
2030 	{ 0x26, 0x10, "Data Decryption Key Fail Limit Reached" },
2031 	{ 0x26, 0x11, "Incomplete Key-Associated Data Set" },
2032 	{ 0x26, 0x12, "Vendor Specific Key Reference Not Found" },
2033 	{ 0x27, 0x00, "Write Protected" },
2034 	{ 0x27, 0x01, "Hardware Write Protected" },
2035 	{ 0x27, 0x02, "Logical Unit Software Write Protected" },
2036 	{ 0x27, 0x03, "Associated Write Protect" },
2037 	{ 0x27, 0x04, "Persistent Write Protect" },
2038 	{ 0x27, 0x05, "Permanent Write Protect" },
2039 	{ 0x27, 0x06, "Conditional Write Protect" },
2040 	{ 0x27, 0x07, "Space Allocation Failed Write Protect" },
2041 	{ 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
2042 	{ 0x28, 0x01, "Import Or Export Element Accessed" },
2043 	{ 0x28, 0x02, "Format-Layer May Have Changed" },
2044 	{ 0x28, 0x03, "Import/Export Element Accessed, Medium Changed" },
2045 	{ 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
2046 	{ 0x29, 0x01, "Power On Occurred" },
2047 	{ 0x29, 0x02, "SCSI Bus Reset Occurred" },
2048 	{ 0x29, 0x03, "Bus Device Reset Function Occurred" },
2049 	{ 0x29, 0x04, "Device Internal Reset" },
2050 	{ 0x29, 0x05, "Transceiver Mode Changed to Single Ended" },
2051 	{ 0x29, 0x06, "Transceiver Mode Changed to LVD" },
2052 	{ 0x29, 0x07, "I_T Nexus Loss Occurred" },
2053 	{ 0x2A, 0x00, "Parameters Changed" },
2054 	{ 0x2A, 0x01, "Mode Parameters Changed" },
2055 	{ 0x2A, 0x02, "Log Parameters Changed" },
2056 	{ 0x2A, 0x03, "Reservations Preempted" },
2057 	{ 0x2A, 0x04, "Reservations Released" },
2058 	{ 0x2A, 0x05, "Registrations Preempted" },
2059 	{ 0x2A, 0x06, "Asymmetric Access State Changed" },
2060 	{ 0x2A, 0x07, "Implicit Asymmetric Access State Transition Failed" },
2061 	{ 0x2A, 0x08, "Priority Changed" },
2062 	{ 0x2A, 0x09, "Capacity Data Has Changed" },
2063 	{ 0x2A, 0x0A, "Error History I_T Nexus Cleared" },
2064 	{ 0x2A, 0x0B, "Error History Snapshot Released" },
2065 	{ 0x2A, 0x0C, "Error Recovery Attributes Have Changed" },
2066 	{ 0x2A, 0x0D, "Data Encryption Capabilities Changed" },
2067 	{ 0x2A, 0x10, "Timestamp Changed" },
2068 	{ 0x2A, 0x11, "Data Encryption Parameters Changed By Another I_T Nexus" },
2069 	{ 0x2A, 0x12, "Data Encryption Parameters Changed By Vendor Specific Event" },
2070 	{ 0x2A, 0x13, "Data Encryption Key Instance Counter Has Changed" },
2071 	{ 0x2A, 0x14, "SA Creation Capabilities Data Has Changed" },
2072 	{ 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
2073 	{ 0x2C, 0x00, "Command Sequence Error" },
2074 	{ 0x2C, 0x01, "Too Many Windows Specified" },
2075 	{ 0x2C, 0x02, "Invalid Combination of Windows Specified" },
2076 	{ 0x2C, 0x03, "Current Program Area Is Not Empty" },
2077 	{ 0x2C, 0x04, "Current Program Area Is Empty" },
2078 	{ 0x2C, 0x05, "Illegal Power Condition Request" },
2079 	{ 0x2C, 0x06, "Persistent Prevent Conflict" },
2080 	{ 0x2C, 0x07, "Previous Busy Status" },
2081 	{ 0x2C, 0x08, "Previous Task Set Full Status" },
2082 	{ 0x2C, 0x09, "Previous Reservation Conflict Status" },
2083 	{ 0x2C, 0x0A, "Partition Or Collection Contains User Objects" },
2084 	{ 0x2C, 0x0B, "Not Reserved" },
2085 	{ 0x2C, 0x0C, "ORWrite Generation Does Not Match" },
2086 	{ 0x2D, 0x00, "Overwrite Error On Update In Place" },
2087 	{ 0x2E, 0x00, "Insufficient Time For Operation" },
2088 	{ 0x2F, 0x00, "Commands Cleared By Another Initiator" },
2089 	{ 0x2F, 0x01, "Commands Cleared By Power Loss Notification" },
2090 	{ 0x2F, 0x02, "Commands Cleared By Device Server" },
2091 	{ 0x30, 0x00, "Incompatible Medium Installed" },
2092 	{ 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
2093 	{ 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
2094 	{ 0x30, 0x03, "Cleaning Cartridge Installed" },
2095 	{ 0x30, 0x04, "Cannot Write Medium - Unknown Format" },
2096 	{ 0x30, 0x05, "Cannot Write Medium - Incompatible Format" },
2097 	{ 0x30, 0x06, "Cannot Format Medium - Incompatible Medium" },
2098 	{ 0x30, 0x07, "Cleaning Failure" },
2099 	{ 0x30, 0x08, "Cannot Write - Application Code Mismatch" },
2100 	{ 0x30, 0x09, "Current Session Not Fixated For Append" },
2101 	{ 0x30, 0x0A, "Cleaning Request Rejected" },
2102 	{ 0x30, 0x10, "Medium Not Formatted" },
2103 	{ 0x30, 0x11, "Incompatible Volume Type" },
2104 	{ 0x30, 0x12, "Incompatible Volume Qualifier" },
2105 	{ 0x30, 0x13, "Cleaning Volume Expired" },
2106 	{ 0x31, 0x00, "Medium Format Corrupted" },
2107 	{ 0x31, 0x01, "Format Command Failed" },
2108 	{ 0x31, 0x02, "Zoned Formatting Failed Due To Spare Linking" },
2109 	{ 0x32, 0x00, "No Defect Spare Location Available" },
2110 	{ 0x32, 0x01, "Defect List Update Failure" },
2111 	{ 0x33, 0x00, "Tape Length Error" },
2112 	{ 0x34, 0x00, "Enclosure Failure" },
2113 	{ 0x35, 0x00, "Enclosure Services Failure" },
2114 	{ 0x35, 0x01, "Unsupported Enclosure Function" },
2115 	{ 0x35, 0x02, "Enclosure Services Unavailable" },
2116 	{ 0x35, 0x03, "Enclosure Services Transfer Failure" },
2117 	{ 0x35, 0x04, "Enclosure Services Transfer Refused" },
2118 	{ 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
2119 	{ 0x37, 0x00, "Rounded Parameter" },
2120 	{ 0x38, 0x00, "Event Status Notification" },
2121 	{ 0x38, 0x02, "ESN - Power Management Class Event" },
2122 	{ 0x38, 0x04, "ESN - Media Class Event" },
2123 	{ 0x38, 0x06, "ESN - Device Busy Class Event" },
2124 	{ 0x39, 0x00, "Saving Parameters Not Supported" },
2125 	{ 0x3A, 0x00, "Medium Not Present" },
2126 	{ 0x3A, 0x01, "Medium Not Present - Tray Closed" },
2127 	{ 0x3A, 0x02, "Medium Not Present - Tray Open" },
2128 	{ 0x3A, 0x03, "Medium Not Present - Loadable" },
2129 	{ 0x3A, 0x04, "Medium Not Present - Medium Auxiliary Memory Accessible" },
2130 	{ 0x3B, 0x00, "Sequential Positioning Error" },
2131 	{ 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
2132 	{ 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
2133 	{ 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
2134 	{ 0x3B, 0x04, "Slew Failure" },
2135 	{ 0x3B, 0x05, "Paper Jam" },
2136 	{ 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
2137 	{ 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
2138 	{ 0x3B, 0x08, "Reposition Error" },
2139 	{ 0x3B, 0x09, "Read Past End Of Medium" },
2140 	{ 0x3B, 0x0A, "Read Past Beginning Of Medium" },
2141 	{ 0x3B, 0x0B, "Position Past End Of Medium" },
2142 	{ 0x3B, 0x0C, "Position Past Beginning Of Medium" },
2143 	{ 0x3B, 0x0D, "Medium Destination Element Full" },
2144 	{ 0x3B, 0x0E, "Medium Source Element Empty" },
2145 	{ 0x3B, 0x0F, "End Of Medium Reached" },
2146 	{ 0x3B, 0x11, "Medium Magazine Not Accessible" },
2147 	{ 0x3B, 0x12, "Medium Magazine Removed" },
2148 	{ 0x3B, 0x13, "Medium Magazine Inserted" },
2149 	{ 0x3B, 0x14, "Medium Magazine Locked" },
2150 	{ 0x3B, 0x15, "Medium Magazine Unlocked" },
2151 	{ 0x3B, 0x16, "Mechanical Positioning Or Changer Error" },
2152 	{ 0x3B, 0x17, "Read Past End Of User Object" },
2153 	{ 0x3B, 0x18, "Element Disabled" },
2154 	{ 0x3B, 0x19, "Element Enabled" },
2155 	{ 0x3B, 0x1A, "Data Transfer Device Removed" },
2156 	{ 0x3B, 0x1B, "Data Transfer Device Inserted" },
2157 	{ 0x3D, 0x00, "Invalid Bits In IDENTIFY Message" },
2158 	{ 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
2159 	{ 0x3E, 0x01, "Logical Unit Failure" },
2160 	{ 0x3E, 0x02, "Timeout On Logical Unit" },
2161 	{ 0x3E, 0x03, "Logical Unit Failed Self-Test" },
2162 	{ 0x3E, 0x04, "Logical Unit Unable To Update Self-Test Log" },
2163 	{ 0x3F, 0x00, "Target Operating Conditions Have Changed" },
2164 	{ 0x3F, 0x01, "Microcode Has Changed" },
2165 	{ 0x3F, 0x02, "Changed Operating Definition" },
2166 	{ 0x3F, 0x03, "INQUIRY Data Has Changed" },
2167 	{ 0x3F, 0x04, "component Device Attached" },
2168 	{ 0x3F, 0x05, "Device Identifier Changed" },
2169 	{ 0x3F, 0x06, "Redundancy Group Created Or Modified" },
2170 	{ 0x3F, 0x07, "Redundancy Group Deleted" },
2171 	{ 0x3F, 0x08, "Spare Created Or Modified" },
2172 	{ 0x3F, 0x09, "Spare Deleted" },
2173 	{ 0x3F, 0x0A, "Volume Set Created Or Modified" },
2174 	{ 0x3F, 0x0B, "Volume Set Deleted" },
2175 	{ 0x3F, 0x0C, "Volume Set Deassigned" },
2176 	{ 0x3F, 0x0D, "Volume Set Reassigned" },
2177 	{ 0x3F, 0x0E, "Reported LUNs Data Has Changed" },
2178 	{ 0x3F, 0x0F, "Echo Buffer Overwritten" },
2179 	{ 0x3F, 0x10, "Medium Loadable" },
2180 	{ 0x3F, 0x11, "Medium Auxiliary Memory Accessible" },
2181 	{ 0x3F, 0x12, "iSCSI IP Address Added" },
2182 	{ 0x3F, 0x13, "iSCSI IP Address Removed" },
2183 	{ 0x3F, 0x14, "iSCSI IP Address Changed" },
2184 	{ 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
2185 	/*
2186 	 * ASC 0x40 also has an ASCQ range from 0x80 to 0xFF.
2187 	 * 0x40 0xNN DIAGNOSTIC FAILURE ON COMPONENT NN
2188 	 */
2189 	{ 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
2190 	{ 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
2191 	{ 0x43, 0x00, "Message Error" },
2192 	{ 0x44, 0x00, "Internal Target Failure" },
2193 	{ 0x44, 0x71, "ATA Device Failed Set Features" },
2194 	{ 0x45, 0x00, "Select Or Reselect Failure" },
2195 	{ 0x46, 0x00, "Unsuccessful Soft Reset" },
2196 	{ 0x47, 0x00, "SCSI Parity Error" },
2197 	{ 0x47, 0x01, "Data Phase CRC Error Detected" },
2198 	{ 0x47, 0x02, "SCSI Parity Error Detected During ST Data Phase" },
2199 	{ 0x47, 0x03, "Information Unit iuCRC Error Detected" },
2200 	{ 0x47, 0x04, "Asynchronous Information Protection Error Detected" },
2201 	{ 0x47, 0x05, "Protocol Service CRC Error" },
2202 	{ 0x47, 0x06, "PHY Test Function In Progress" },
2203 	{ 0x47, 0x7F, "Some Commands Cleared By iSCSI Protocol Event" },
2204 	{ 0x48, 0x00, "Initiator Detected Error Message Received" },
2205 	{ 0x49, 0x00, "Invalid Message Error" },
2206 	{ 0x4A, 0x00, "Command Phase Error" },
2207 	{ 0x4B, 0x00, "Data Phase Error" },
2208 	{ 0x4B, 0x01, "Invalid Target Port Transfer Tag Received" },
2209 	{ 0x4B, 0x02, "Too Much Write Data" },
2210 	{ 0x4B, 0x03, "ACK/NAK Timeout" },
2211 	{ 0x4B, 0x04, "NAK Received" },
2212 	{ 0x4B, 0x05, "Data Offset Error" },
2213 	{ 0x4B, 0x06, "Initiator Response Timeout" },
2214 	{ 0x4B, 0x07, "Connection Lost" },
2215 	{ 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
2216 	/*
2217 	 * ASC 0x4D has an ASCQ range from 0x00 to 0xFF.
2218 	 * 0x4D 0xNN TAGGED OVERLAPPED COMMANDS (NN = TASK TAG)
2219 	 */
2220 	{ 0x4E, 0x00, "Overlapped Commands Attempted" },
2221 	{ 0x50, 0x00, "Write Append Error" },
2222 	{ 0x50, 0x01, "Write Append Position Error" },
2223 	{ 0x50, 0x02, "Position Error Related To Timing" },
2224 	{ 0x51, 0x00, "Erase Failure" },
2225 	{ 0x51, 0x01, "Erase Failure - Incomplete Erase Operation Detected" },
2226 	{ 0x52, 0x00, "Cartridge Fault" },
2227 	{ 0x53, 0x00, "Media Load or Eject Failed" },
2228 	{ 0x53, 0x01, "Unload Tape Failure" },
2229 	{ 0x53, 0x02, "Medium Removal Prevented" },
2230 	{ 0x53, 0x03, "Medium Removal Prevented By Data Transfer Element" },
2231 	{ 0x53, 0x04, "Medium Thread Or Unthread Failure" },
2232 	{ 0x53, 0x05, "Volume Identifier Invalid" },
2233 	{ 0x53, 0x06, "Volume Identifier Missing" },
2234 	{ 0x53, 0x07, "Duplicate Volume Identifier" },
2235 	{ 0x53, 0x08, "Element Status Unknown" },
2236 	{ 0x54, 0x00, "SCSI To Host System Interface Failure" },
2237 	{ 0x55, 0x00, "System Resource Failure" },
2238 	{ 0x55, 0x01, "System Buffer Full" },
2239 	{ 0x55, 0x02, "Insufficient Reservation Resources" },
2240 	{ 0x55, 0x03, "Insufficient Resources" },
2241 	{ 0x55, 0x04, "Insufficient Registration Resources" },
2242 	{ 0x55, 0x05, "Insufficient Access Control Resources" },
2243 	{ 0x55, 0x06, "Auxiliary Memory Out Of Space" },
2244 	{ 0x55, 0x07, "Quota Error" },
2245 	{ 0x55, 0x08, "Maximum Number Of Supplemental Decryption Keys Exceeded" },
2246 	{ 0x55, 0x09, "Medium Auxiliary Memory Not Accessible" },
2247 	{ 0x55, 0x0A, "Data Currently Unavailable" },
2248 	{ 0x55, 0x0B, "Insufficient Power For Operation" },
2249 	{ 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
2250 	{ 0x58, 0x00, "Generation Does Not Exist" },
2251 	{ 0x59, 0x00, "Updated Block Read" },
2252 	{ 0x5A, 0x00, "Operator Request or State Change Input" },
2253 	{ 0x5A, 0x01, "Operator Medium Removal Requested" },
2254 	{ 0x5A, 0x02, "Operator Selected Write Protect" },
2255 	{ 0x5A, 0x03, "Operator Selected Write Permit" },
2256 	{ 0x5B, 0x00, "Log Exception" },
2257 	{ 0x5B, 0x01, "Threshold Condition Met" },
2258 	{ 0x5B, 0x02, "Log Counter At Maximum" },
2259 	{ 0x5B, 0x03, "Log List Codes Exhausted" },
2260 	{ 0x5C, 0x00, "RPL Status Change" },
2261 	{ 0x5C, 0x01, "Spindles Synchronized" },
2262 	{ 0x5C, 0x02, "Spindles Not Synchronized" },
2263 	{ 0x5D, 0x00, "Failure Prediction Threshold Exceeded" },
2264 	{ 0x5D, 0x01, "Media Failure Prediction Threshold Exceeded" },
2265 	{ 0x5D, 0x02, "Logical Unit Failure Prediction Threshold Exceeded" },
2266 	{ 0x5D, 0x03, "Spare Area Exhaustion Prediction Threshold Exceeded" },
2267 	{ 0x5D, 0x10, "Hardware Impending Failure General Hard Drive Failure" },
2268 	{ 0x5D, 0x11, "Hardware Impending Failure Drive Error Rate Too High" },
2269 	{ 0x5D, 0x12, "Hardware Impending Failure Data Error Rate Too High" },
2270 	{ 0x5D, 0x13, "Hardware Impending Failure Seek Error Rate Too High" },
2271 	{ 0x5D, 0x14, "Hardware Impending Failure Too Many Block Reassigns" },
2272 	{ 0x5D, 0x15, "Hardware Impending Failure Access Times Too High" },
2273 	{ 0x5D, 0x16, "Hardware Impending Failure Start Unit Times Too High" },
2274 	{ 0x5D, 0x17, "Hardware Impending Failure Channel Parametrics" },
2275 	{ 0x5D, 0x18, "Hardware Impending Failure Controller Detected" },
2276 	{ 0x5D, 0x19, "Hardware Impending Failure Throughput Performance" },
2277 	{ 0x5D, 0x1A, "Hardware Impending Failure Seek Time Performance" },
2278 	{ 0x5D, 0x1B, "Hardware Impending Failure Spin-Up Retry Count" },
2279 	{ 0x5D, 0x1C, "Hardware Impending Failure Drive Calibration Retry Count" },
2280 	{ 0x5D, 0x20, "Controller Impending Failure General Hard Drive Failure" },
2281 	{ 0x5D, 0x21, "Controller Impending Failure Drive Error Rate Too High" },
2282 	{ 0x5D, 0x22, "Controller Impending Failure Data Error Rate Too High" },
2283 	{ 0x5D, 0x23, "Controller Impending Failure Seek Error Rate Too High" },
2284 	{ 0x5D, 0x24, "Controller Impending Failure Too Many Block Reassigns" },
2285 	{ 0x5D, 0x25, "Controller Impending Failure Access Times Too High" },
2286 	{ 0x5D, 0x26, "Controller Impending Failure Start Unit Times Too High" },
2287 	{ 0x5D, 0x27, "Controller Impending Failure Channel Parametrics" },
2288 	{ 0x5D, 0x28, "Controller Impending Failure Controller Detected" },
2289 	{ 0x5D, 0x29, "Controller Impending Failure Throughput Performance" },
2290 	{ 0x5D, 0x2A, "Controller Impending Failure Seek Time Performance" },
2291 	{ 0x5D, 0x2B, "Controller Impending Failure Spin-Up Retry Count" },
2292 	{ 0x5D, 0x2C, "Controller Impending Failure Drive Calibration Retry Count" },
2293 	{ 0x5D, 0x30, "Data Channel Impending Failure General Hard Drive Failure" },
2294 	{ 0x5D, 0x31, "Data Channel Impending Failure Drive Error Rate Too High" },
2295 	{ 0x5D, 0x32, "Data Channel Impending Failure Data Error Rate Too High" },
2296 	{ 0x5D, 0x33, "Data Channel Impending Failure Seek Error Rate Too High" },
2297 	{ 0x5D, 0x34, "Data Channel Impending Failure Too Many Block Reassigns" },
2298 	{ 0x5D, 0x35, "Data Channel Impending Failure Access Times Too High" },
2299 	{ 0x5D, 0x36, "Data Channel Impending Failure Start Unit Times Too High" },
2300 	{ 0x5D, 0x37, "Data Channel Impending Failure Channel Parametrics" },
2301 	{ 0x5D, 0x38, "Data Channel Impending Failure Controller Detected" },
2302 	{ 0x5D, 0x39, "Data Channel Impending Failure Throughput Performance" },
2303 	{ 0x5D, 0x3A, "Data Channel Impending Failure Seek Time Performance" },
2304 	{ 0x5D, 0x3B, "Data Channel Impending Failure Spin-Up Retry Count" },
2305 	{ 0x5D, 0x3C, "Data Channel Impending Failure Drive Calibration Retry Count" },
2306 	{ 0x5D, 0x40, "Servo Impending Failure General Hard Drive Failure" },
2307 	{ 0x5D, 0x41, "Servo Impending Failure Drive Error Rate Too High" },
2308 	{ 0x5D, 0x42, "Servo Impending Failure Data Error Rate Too High" },
2309 	{ 0x5D, 0x43, "Servo Impending Failure Seek Error Rate Too High" },
2310 	{ 0x5D, 0x44, "Servo Impending Failure Too Many Block Reassigns" },
2311 	{ 0x5D, 0x45, "Servo Impending Failure Access Times Too High" },
2312 	{ 0x5D, 0x46, "Servo Impending Failure Start Unit Times Too High" },
2313 	{ 0x5D, 0x47, "Servo Impending Failure Channel Parametrics" },
2314 	{ 0x5D, 0x48, "Servo Impending Failure Controller Detected" },
2315 	{ 0x5D, 0x49, "Servo Impending Failure Throughput Performance" },
2316 	{ 0x5D, 0x4A, "Servo Impending Failure Seek Time Performance" },
2317 	{ 0x5D, 0x4B, "Servo Impending Failure Spin-Up Retry Count" },
2318 	{ 0x5D, 0x4C, "Servo Impending Failure Drive Calibration Retry Count" },
2319 	{ 0x5D, 0x50, "Spindle Impending Failure General Hard Drive Failure" },
2320 	{ 0x5D, 0x51, "Spindle Impending Failure Drive Error Rate Too High" },
2321 	{ 0x5D, 0x52, "Spindle Impending Failure Data Error Rate Too High" },
2322 	{ 0x5D, 0x53, "Spindle Impending Failure Seek Error Rate Too High" },
2323 	{ 0x5D, 0x54, "Spindle Impending Failure Too Many Block Reassigns" },
2324 	{ 0x5D, 0x55, "Spindle Impending Failure Access Times Too High" },
2325 	{ 0x5D, 0x56, "Spindle Impending Failure Start Unit Times Too High" },
2326 	{ 0x5D, 0x57, "Spindle Impending Failure Channel Parametrics" },
2327 	{ 0x5D, 0x58, "Spindle Impending Failure Controller Detected" },
2328 	{ 0x5D, 0x59, "Spindle Impending Failure Throughput Performance" },
2329 	{ 0x5D, 0x5A, "Spindle Impending Failure Seek Time Performance" },
2330 	{ 0x5D, 0x5B, "Spindle Impending Failure Spin-Up Retry Count" },
2331 	{ 0x5D, 0x5C, "Spindle Impending Failure Drive Calibration Retry Count" },
2332 	{ 0x5D, 0x60, "Firmware Impending Failure General Hard Drive Failure" },
2333 	{ 0x5D, 0x61, "Firmware Impending Failure Drive Error Rate Too High" },
2334 	{ 0x5D, 0x62, "Firmware Impending Failure Data Error Rate Too High" },
2335 	{ 0x5D, 0x63, "Firmware Impending Failure Seek Error Rate Too High" },
2336 	{ 0x5D, 0x64, "Firmware Impending Failure Too Many Block Reassigns" },
2337 	{ 0x5D, 0x65, "Firmware Impending Failure Access Times Too High" },
2338 	{ 0x5D, 0x66, "Firmware Impending Failure Start Unit Times Too High" },
2339 	{ 0x5D, 0x67, "Firmware Impending Failure Channel Parametrics" },
2340 	{ 0x5D, 0x68, "Firmware Impending Failure Controller Detected" },
2341 	{ 0x5D, 0x69, "Firmware Impending Failure Throughput Performance" },
2342 	{ 0x5D, 0x6A, "Firmware Impending Failure Seek Time Performance" },
2343 	{ 0x5D, 0x6B, "Firmware Impending Failure Spin-Up Retry Count" },
2344 	{ 0x5D, 0x6C, "Firmware Impending Failure Drive Calibration Retry Count" },
2345 	{ 0x5D, 0xFF, "Failure Prediction Threshold Exceeded (false)" },
2346 	{ 0x5E, 0x00, "Low Power Condition On" },
2347 	{ 0x5E, 0x01, "Idle Condition Activated By Timer" },
2348 	{ 0x5E, 0x02, "Standby Condition Activated By Timer" },
2349 	{ 0x5E, 0x03, "Idle Condition Activated By Command" },
2350 	{ 0x5E, 0x04, "Standby Condition Activated By Command" },
2351 	{ 0x5E, 0x05, "IDLE_B Condition Activated By Timer" },
2352 	{ 0x5E, 0x06, "IDLE_B Condition Activated By Command" },
2353 	{ 0x5E, 0x07, "IDLE_C Condition Activated By Timer" },
2354 	{ 0x5E, 0x08, "IDLE_C Condition Activated By Command" },
2355 	{ 0x5E, 0x09, "STANDBY_Y Condition Activated By Timer" },
2356 	{ 0x5E, 0x0A, "STANDBY_Y Condition Activated By Command" },
2357 	{ 0x5E, 0x41, "Power State Change To Active" },
2358 	{ 0x5E, 0x42, "Power State Change To Idle" },
2359 	{ 0x5E, 0x43, "Power State Change To Standby" },
2360 	{ 0x5E, 0x45, "Power State Change To Sleep" },
2361 	{ 0x5E, 0x47, "Power State Change To Device Control" },
2362 	{ 0x60, 0x00, "Lamp Failure" },
2363 	{ 0x61, 0x00, "Video Acquisition Error" },
2364 	{ 0x61, 0x01, "Unable To Acquire Video" },
2365 	{ 0x61, 0x02, "Out Of Focus" },
2366 	{ 0x62, 0x00, "Scan Head Positioning Error" },
2367 	{ 0x63, 0x00, "End Of User Area Encountered On This Track" },
2368 	{ 0x63, 0x01, "Packet Does Not Fit In Available Space" },
2369 	{ 0x64, 0x00, "Illegal Mode For This Track" },
2370 	{ 0x64, 0x01, "Invalid Packet Size" },
2371 	{ 0x65, 0x00, "Voltage Fault" },
2372 	{ 0x66, 0x00, "Automatic Document Feeder Cover Up" },
2373 	{ 0x66, 0x01, "Automatic Document Feeder Lift Up" },
2374 	{ 0x66, 0x02, "Document Jam In Automatic Document Feeder" },
2375 	{ 0x66, 0x03, "Document Miss Feed Automatic In Document Feeder" },
2376 	{ 0x67, 0x00, "Configuration Failure" },
2377 	{ 0x67, 0x01, "Configuration Of Incapable Logical Units Failed" },
2378 	{ 0x67, 0x02, "Add Logical Unit Failed" },
2379 	{ 0x67, 0x03, "Modification Of Logical Unit Failed" },
2380 	{ 0x67, 0x04, "Exchange Of Logical Unit Failed" },
2381 	{ 0x67, 0x05, "Remove Of Logical Unit Failed" },
2382 	{ 0x67, 0x06, "Attachment Of Logical Unit Failed" },
2383 	{ 0x67, 0x07, "Creation Of Logical Unit Failed" },
2384 	{ 0x67, 0x08, "Assign Failure Occurred" },
2385 	{ 0x67, 0x09, "Multiply Assigned Logical Unit" },
2386 	{ 0x67, 0x0A, "Set Target Port Groups Command Failed" },
2387 	{ 0x67, 0x0B, "ATA Device Feature Not Enabled" },
2388 	{ 0x68, 0x00, "Logical Unit Not Configured" },
2389 	{ 0x69, 0x00, "Data Loss On Logical Unit" },
2390 	{ 0x69, 0x01, "Multiple Logical Unit Failures" },
2391 	{ 0x69, 0x02, "Parity/Data Mismatch" },
2392 	{ 0x6A, 0x00, "Informational, Refer To Log" },
2393 	{ 0x6B, 0x00, "State Change Has Occurred" },
2394 	{ 0x6B, 0x01, "Redundancy Level Got Better" },
2395 	{ 0x6B, 0x02, "Redundancy Level Got Worse" },
2396 	{ 0x6C, 0x00, "Rebuild Failure Occurred" },
2397 	{ 0x6D, 0x00, "Recalculate Failure Occurred" },
2398 	{ 0x6E, 0x00, "Command To Logical Unit Failed" },
2399 	{ 0x6F, 0x00, "Copy Protection Key Exchange Failure - Authentication Failure" },
2400 	{ 0x6F, 0x01, "Copy Protection Key Exchange Failure - Key Not Present" },
2401 	{ 0x6F, 0x02, "Copy Protection Key Exchange Failure - Key Not Established" },
2402 	{ 0x6F, 0x03, "Read Of Scrambled Sector Without Authentication" },
2403 	{ 0x6F, 0x04, "Media Region Code Is Mismatched To Logical Unit Region" },
2404 	{ 0x6F, 0x05, "Drive Region Must Be Permanent/Region Reset Count Error" },
2405 	/*
2406 	 * ASC 0x70 has an ASCQ range from 0x00 to 0xFF.
2407 	 * 0x70 0xNN DECOMPRESSION EXCEPTION SHORT ALGORITHM ID Of NN
2408 	 */
2409 	{ 0x71, 0x00, "Decompression Exception Long Algorithm ID" },
2410 	{ 0x72, 0x00, "Session Fixation Error" },
2411 	{ 0x72, 0x01, "Session Fixation Error Writing Lead-In" },
2412 	{ 0x72, 0x02, "Session Fixation Error Writing Lead-Out" },
2413 	{ 0x72, 0x03, "Session Fixation Error - Incomplete Track In Session" },
2414 	{ 0x72, 0x04, "Empty Or Partially Written Reserved Track" },
2415 	{ 0x72, 0x05, "No More Track Reservations Allowed" },
2416 	{ 0x72, 0x06, "RMZ Extension Is Not Allowed" },
2417 	{ 0x72, 0x07, "No More Test Zone Extensions Are Allowed" },
2418 	{ 0x73, 0x00, "CD Control Error" },
2419 	{ 0x73, 0x01, "Power Calibration Area Almost Full" },
2420 	{ 0x73, 0x02, "Power Calibration Area Is Full" },
2421 	{ 0x73, 0x03, "Power Calibration Area Error" },
2422 	{ 0x73, 0x04, "Program Memory Area Update Failure" },
2423 	{ 0x73, 0x05, "Program Memory Area Is Full" },
2424 	{ 0x73, 0x06, "RMA/PMA Is Almost Full" },
2425 	{ 0x73, 0x10, "Current Power Calibration Area Almost Full" },
2426 	{ 0x73, 0x11, "Current Power Calibration Area Is Full" },
2427 	{ 0x73, 0x17, "RDZ Is Full" },
2428 	{ 0x74, 0x00, "Security Error" },
2429 	{ 0x74, 0x01, "Unable To Decrypt Data" },
2430 	{ 0x74, 0x02, "Unencrypted Data Encountered While Decrypting" },
2431 	{ 0x74, 0x03, "Incorrect Data Encryption Key" },
2432 	{ 0x74, 0x04, "Cryptographic Integrity Validation Failed" },
2433 	{ 0x74, 0x05, "Error Decrypting Data" },
2434 	{ 0x74, 0x06, "Unknown Signature Verification Key" },
2435 	{ 0x74, 0x07, "Encryption Parameters Not Useable" },
2436 	{ 0x74, 0x08, "Digital Signature Validation Failure" },
2437 	{ 0x74, 0x09, "Encryption Mode Mismatch On Read" },
2438 	{ 0x74, 0x0A, "Encrypted Block Not Raw Read Enabled" },
2439 	{ 0x74, 0x0B, "Incorrect Encryption Parameters" },
2440 	{ 0x74, 0x0C, "Unable To Decrypt Parameter List" },
2441 	{ 0x74, 0x0D, "Encryption Algorithm Disabled" },
2442 	{ 0x74, 0x10, "SA Creation Parameter Value Invalid" },
2443 	{ 0x74, 0x11, "SA Creation Parameter Value Rejected" },
2444 	{ 0x74, 0x12, "Invalid SA Usage" },
2445 	{ 0x74, 0x21, "Data Encryption Configuration Prevented" },
2446 	{ 0x74, 0x30, "SA Creation Parameter Not Supported" },
2447 	{ 0x74, 0x40, "Authentication Failed" },
2448 	{ 0x74, 0x61, "External Data Encryption Key Manager Access Error" },
2449 	{ 0x74, 0x62, "External Data Encryption Key Manager Error" },
2450 	{ 0x74, 0x63, "External Data Encryption Key Not Found" },
2451 	{ 0x74, 0x64, "External Data Encryption Request Not Authorized" },
2452 	{ 0x74, 0x6E, "External Data Encryption Control Timeout" },
2453 	{ 0x74, 0x6F, "External Data Encryption Control Error" },
2454 	{ 0x74, 0x71, "Logical Unit Access Not Authorized" },
2455 	{ 0x74, 0x79, "Security Conflict In Translated Device" },
2456 	{ 0x00, 0x00, NULL }
2457 };
2458 
2459 static __inline void
2460 asc2ascii(u_int8_t asc, u_int8_t ascq, char *result, size_t len)
2461 {
2462 	int i;
2463 
2464 	/* Check for a dynamically built description. */
2465 	switch (asc) {
2466 	case 0x40:
2467 		if (ascq >= 0x80) {
2468 			snprintf(result, len,
2469 		            "Diagnostic Failure on Component 0x%02x", ascq);
2470 			return;
2471 		}
2472 		break;
2473 	case 0x4d:
2474 		snprintf(result, len,
2475 		    "Tagged Overlapped Commands (0x%02x = TASK TAG)", ascq);
2476 		return;
2477 	case 0x70:
2478 		snprintf(result, len,
2479 		    "Decompression Exception Short Algorithm ID OF 0x%02x",
2480 		    ascq);
2481 		return;
2482 	default:
2483 		break;
2484 	}
2485 
2486 	/* Check for a fixed description. */
2487 	for (i = 0; adesc[i].description != NULL; i++) {
2488 		if (adesc[i].asc == asc && adesc[i].ascq == ascq) {
2489 			strlcpy(result, adesc[i].description, len);
2490 			return;
2491 		}
2492 	}
2493 
2494 	/* Just print out the ASC and ASCQ values as a description. */
2495 	snprintf(result, len, "ASC 0x%02x ASCQ 0x%02x", asc, ascq);
2496 }
2497 #endif /* SCSITERSE */
2498 
2499 void
2500 scsi_print_sense(struct scsi_xfer *xs)
2501 {
2502 	struct scsi_sense_data		*sense = &xs->sense;
2503 	char				*sbs;
2504 	int32_t				 info;
2505 	u_int8_t			 serr = sense->error_code & SSD_ERRCODE;
2506 
2507 	sc_print_addr(xs->sc_link);
2508 
2509 	/* XXX For error 0x71, current opcode is not the relevant one. */
2510 	printf("%sCheck Condition (error %#x) on opcode 0x%x\n",
2511 	    (serr == SSD_ERRCODE_DEFERRED) ? "DEFERRED " : "", serr,
2512 	    xs->cmd.opcode);
2513 
2514 	if (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED) {
2515 		if (ISSET(sense->error_code, SSD_ERRCODE_VALID)) {
2516 			struct scsi_sense_data_unextended *usense =
2517 			    (struct scsi_sense_data_unextended *)sense;
2518 			printf("   AT BLOCK #: %d (decimal)",
2519 			    _3btol(usense->block));
2520 		}
2521 		return;
2522 	}
2523 
2524 	printf("    SENSE KEY: %s\n", scsi_decode_sense(sense,
2525 	    DECODE_SENSE_KEY));
2526 
2527 	if (sense->flags & (SSD_FILEMARK | SSD_EOM | SSD_ILI)) {
2528 		char pad = ' ';
2529 
2530 		printf("             ");
2531 		if (ISSET(sense->flags, SSD_FILEMARK)) {
2532 			printf("%c Filemark Detected", pad);
2533 			pad = ',';
2534 		}
2535 		if (ISSET(sense->flags, SSD_EOM)) {
2536 			printf("%c EOM Detected", pad);
2537 			pad = ',';
2538 		}
2539 		if (ISSET(sense->flags, SSD_ILI))
2540 			printf("%c Incorrect Length Indicator Set", pad);
2541 		printf("\n");
2542 	}
2543 
2544 	/*
2545 	 * It is inconvenient to use device type to figure out how to
2546 	 * format the info fields. So print them as 32 bit integers.
2547 	 */
2548 	info = _4btol(&sense->info[0]);
2549 	if (info)
2550 		printf("         INFO: 0x%x (VALID flag %s)\n", info,
2551 		    ISSET(sense->error_code, SSD_ERRCODE_VALID) ? "on" : "off");
2552 
2553 	if (sense->extra_len < 4)
2554 		return;
2555 
2556 	info = _4btol(&sense->cmd_spec_info[0]);
2557 	if (info)
2558 		printf(" COMMAND INFO: 0x%x\n", info);
2559 	sbs = scsi_decode_sense(sense, DECODE_ASC_ASCQ);
2560 	if (strlen(sbs) > 0)
2561 		printf("     ASC/ASCQ: %s\n", sbs);
2562 	if (sense->fru != 0)
2563 		printf("     FRU CODE: 0x%x\n", sense->fru);
2564 	sbs = scsi_decode_sense(sense, DECODE_SKSV);
2565 	if (strlen(sbs) > 0)
2566 		printf("         SKSV: %s\n", sbs);
2567 }
2568 
2569 char *
2570 scsi_decode_sense(struct scsi_sense_data *sense, int flag)
2571 {
2572 	static char				rqsbuf[132];
2573 	u_int16_t				count;
2574 	u_int8_t				skey, spec_1;
2575 	int					len;
2576 
2577 	bzero(rqsbuf, sizeof(rqsbuf));
2578 
2579 	skey = sense->flags & SSD_KEY;
2580 	spec_1 = sense->sense_key_spec_1;
2581 	count = _2btol(&sense->sense_key_spec_2);
2582 
2583 	switch (flag) {
2584 	case DECODE_SENSE_KEY:
2585 		strlcpy(rqsbuf, sense_keys[skey], sizeof(rqsbuf));
2586 		break;
2587 	case DECODE_ASC_ASCQ:
2588 		asc2ascii(sense->add_sense_code, sense->add_sense_code_qual,
2589 		    rqsbuf, sizeof(rqsbuf));
2590 		break;
2591 	case DECODE_SKSV:
2592 		if (sense->extra_len < 9 || !ISSET(spec_1, SSD_SCS_VALID))
2593 			break;
2594 		switch (skey) {
2595 		case SKEY_ILLEGAL_REQUEST:
2596 			len = snprintf(rqsbuf, sizeof rqsbuf,
2597 			    "Error in %s, Offset %d",
2598 			    ISSET(spec_1, SSD_SCS_CDB_ERROR) ? "CDB" :
2599 			    "Parameters", count);
2600 			if ((len != -1 && len < sizeof rqsbuf) &&
2601 			    ISSET(spec_1, SSD_SCS_VALID_BIT_INDEX))
2602 				snprintf(rqsbuf+len, sizeof rqsbuf - len,
2603 				    ", bit %d", spec_1 & SSD_SCS_BIT_INDEX);
2604 			break;
2605 		case SKEY_RECOVERED_ERROR:
2606 		case SKEY_MEDIUM_ERROR:
2607 		case SKEY_HARDWARE_ERROR:
2608 			snprintf(rqsbuf, sizeof rqsbuf,
2609 			    "Actual Retry Count: %d", count);
2610 			break;
2611 		case SKEY_NOT_READY:
2612 			snprintf(rqsbuf, sizeof rqsbuf,
2613 			    "Progress Indicator: %d", count);
2614 			break;
2615 		default:
2616 			break;
2617 		}
2618 		break;
2619 	default:
2620 		break;
2621 	}
2622 
2623 	return rqsbuf;
2624 }
2625 
2626 void
2627 scsi_cmd_rw_decode(struct scsi_generic *cmd, u_int64_t *blkno,
2628     u_int32_t *nblks)
2629 {
2630 	switch (cmd->opcode) {
2631 	case READ_COMMAND:
2632 	case WRITE_COMMAND: {
2633 		struct scsi_rw *rw = (struct scsi_rw *)cmd;
2634 		*blkno = _3btol(rw->addr) & (SRW_TOPADDR << 16 | 0xffff);
2635 		*nblks = rw->length ? rw->length : 0x100;
2636 		break;
2637 	}
2638 	case READ_10:
2639 	case WRITE_10: {
2640 		struct scsi_rw_10 *rw10 = (struct scsi_rw_10 *)cmd;
2641 		*blkno = _4btol(rw10->addr);
2642 		*nblks = _2btol(rw10->length);
2643 		break;
2644 	}
2645 	case READ_12:
2646 	case WRITE_12: {
2647 		struct scsi_rw_12 *rw12 = (struct scsi_rw_12 *)cmd;
2648 		*blkno = _4btol(rw12->addr);
2649 		*nblks = _4btol(rw12->length);
2650 		break;
2651 	}
2652 	case READ_16:
2653 	case WRITE_16: {
2654 		struct scsi_rw_16 *rw16 = (struct scsi_rw_16 *)cmd;
2655 		*blkno = _8btol(rw16->addr);
2656 		*nblks = _4btol(rw16->length);
2657 		break;
2658 	}
2659 	default:
2660 		panic("scsi_cmd_rw_decode: bad opcode 0x%02x", cmd->opcode);
2661 	}
2662 }
2663 
2664 #ifdef SCSIDEBUG
2665 u_int32_t scsidebug_buses = SCSIDEBUG_BUSES;
2666 u_int32_t scsidebug_targets = SCSIDEBUG_TARGETS;
2667 u_int32_t scsidebug_luns = SCSIDEBUG_LUNS;
2668 int scsidebug_level = SCSIDEBUG_LEVEL;
2669 
2670 const char *flagnames[] = {
2671 	"REMOVABLE",
2672 	"MEDIA LOADED",
2673 	"READONLY",
2674 	"OPEN",
2675 	"DB1",
2676 	"DB2",
2677 	"DB3",
2678 	"DB4",
2679 	"EJECTING",
2680 	"ATAPI",
2681 	"UMASS",
2682 	"VIRTUAL",
2683 	"OWN_IOPL",
2684 	NULL
2685 };
2686 
2687 const char *quirknames[] = {
2688 	"AUTOSAVE",
2689 	"NOSYNC",
2690 	"NOWIDE",
2691 	"NOTAGS",
2692 	"NOSYNCCACHE",
2693 	"NOSENSE",
2694 	"LITTLETOC",
2695 	"NOCAPACITY",
2696 	"NODOORLOCK",
2697 	NULL
2698 };
2699 
2700 const char *devicetypenames[32] = {
2701 	"T_DIRECT",
2702 	"T_SEQUENTIAL",
2703 	"T_PRINTER",
2704 	"T_PROCESSOR",
2705 	"T_WORM",
2706 	"T_CDROM",
2707 	"T_SCANNER",
2708 	"T_OPTICAL",
2709 	"T_CHANGER",
2710 	"T_COMM",
2711 	"T_ASC0",
2712 	"T_ASC1",
2713 	"T_STROARRAY",
2714 	"T_ENCLOSURE",
2715 	"T_RDIRECT",
2716 	"T_OCRW",
2717 	"T_BCC",
2718 	"T_OSD",
2719 	"T_ADC",
2720 	"T_RESERVED",
2721 	"T_RESERVED",
2722 	"T_RESERVED",
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_WELL_KNOWN_LU",
2732 	"T_NODEVICE"
2733 };
2734 
2735 /*
2736  * Print out sense data details.
2737  */
2738 void
2739 scsi_show_sense(struct scsi_xfer *xs)
2740 {
2741 	struct scsi_sense_data	*sense = &xs->sense;
2742 	struct scsi_link	*link = xs->sc_link;
2743 
2744 	SC_DEBUG(link, SDEV_DB1,
2745 	    ("code:%#x valid:%d key:%#x ili:%d eom:%d fmark:%d extra:%d\n",
2746 	    sense->error_code & SSD_ERRCODE,
2747 	    sense->error_code & SSD_ERRCODE_VALID ? 1 : 0,
2748 	    sense->flags & SSD_KEY,
2749 	    sense->flags & SSD_ILI ? 1 : 0,
2750 	    sense->flags & SSD_EOM ? 1 : 0,
2751 	    sense->flags & SSD_FILEMARK ? 1 : 0,
2752 	    sense->extra_len));
2753 
2754 	if (ISSET(xs->sc_link->flags, SDEV_DB1))
2755 		scsi_show_mem((u_char *)&xs->sense, sizeof(xs->sense));
2756 
2757 	scsi_print_sense(xs);
2758 }
2759 
2760 /*
2761  * Given a scsi_xfer, dump the request, in all its glory
2762  */
2763 void
2764 scsi_show_xs(struct scsi_xfer *xs)
2765 {
2766 	u_char		*b = (u_char *)&xs->cmd;
2767 	int		 i = 0;
2768 
2769 	if (!ISSET(xs->sc_link->flags, SDEV_DB1))
2770 		return;
2771 
2772 	sc_print_addr(xs->sc_link);
2773 	printf("xs  (%p): ", xs);
2774 
2775 	printf("flg(0x%x)", xs->flags);
2776 	printf("link(%p)", xs->sc_link);
2777 	printf("retr(0x%x)", xs->retries);
2778 	printf("timo(0x%x)", xs->timeout);
2779 	printf("data(%p)", xs->data);
2780 	printf("res(0x%zx)", xs->resid);
2781 	printf("err(0x%x)", xs->error);
2782 	printf("bp(%p)\n", xs->bp);
2783 
2784 	sc_print_addr(xs->sc_link);
2785 	printf("cmd (%p): ", &xs->cmd);
2786 
2787 	if (!ISSET(xs->flags, SCSI_RESET)) {
2788 		while (i < xs->cmdlen) {
2789 			if (i)
2790 				printf(",");
2791 			printf("%x", b[i++]);
2792 		}
2793 		printf("-[%d bytes]\n", xs->datalen);
2794 	} else
2795 		printf("-RESET-\n");
2796 
2797 	if (xs->datalen && ISSET(xs->flags, SCSI_DATA_OUT))
2798 		scsi_show_mem(xs->data, min(64, xs->datalen));
2799 }
2800 
2801 void
2802 scsi_show_mem(u_char *address, int num)
2803 {
2804 	int x;
2805 
2806 	printf("------------------------------");
2807 	for (x = 0; x < num; x++) {
2808 		if ((x % 16) == 0)
2809 			printf("\n%03d: ", x);
2810 		printf("%02x ", *address++);
2811 	}
2812 	printf("\n------------------------------\n");
2813 }
2814 
2815 void
2816 scsi_show_flags(u_int32_t flags, const char **names)
2817 {
2818 	int		i, first, exhausted;
2819 	u_int32_t	unnamed;
2820 
2821 	printf("<");
2822 	for (first = 1, exhausted = 0, unnamed = 0, i = 0; i < 32; i++) {
2823 		if (!ISSET(flags, 1 << i))
2824 			continue;
2825 		if (exhausted == 0 && names[i] == NULL)
2826 			exhausted = 1;
2827 		if (exhausted || strlen(names[i]) == 0) {
2828 			SET(unnamed, 1 << i);
2829 			continue;
2830 		}
2831 		if (first == 0)
2832 			printf(", ");
2833 		else
2834 			first = 0;
2835 		printf("%s", names[i]);
2836 	}
2837 	if (unnamed != 0)
2838 		printf("%s0x%08x", first ? "" : ", ", unnamed);
2839 	printf(">");
2840 }
2841 #endif /* SCSIDEBUG */
2842