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