Home | History | Annotate | Line # | Download | only in scsipi
scsi_base.c revision 1.59
      1 /*	$NetBSD: scsi_base.c,v 1.59 1998/02/05 07:59:44 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     34  */
     35 
     36 #include "opt_scsiverbose.h"
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/buf.h>
     43 #include <sys/uio.h>
     44 #include <sys/malloc.h>
     45 #include <sys/errno.h>
     46 #include <sys/device.h>
     47 #include <sys/proc.h>
     48 
     49 #include <dev/scsipi/scsipi_all.h>
     50 #include <dev/scsipi/scsi_all.h>
     51 #include <dev/scsipi/scsi_disk.h>
     52 #include <dev/scsipi/scsiconf.h>
     53 #include <dev/scsipi/scsipi_base.h>
     54 
     55 #ifdef SCSIVERBOSE
     56 static	void asc2ascii __P((unsigned char, unsigned char, char *));
     57 char	*scsi_decode_sense __P((void *, int));
     58 #endif
     59 
     60 /*
     61  * Do a scsi operation, asking a device to run as SCSI-II if it can.
     62  */
     63 int
     64 scsi_change_def(sc_link, flags)
     65 	struct scsipi_link *sc_link;
     66 	int flags;
     67 {
     68 	struct scsi_changedef scsipi_cmd;
     69 
     70 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
     71 	scsipi_cmd.opcode = SCSI_CHANGE_DEFINITION;
     72 	scsipi_cmd.how = SC_SCSI_2;
     73 
     74 	return (scsipi_command(sc_link,
     75 	    (struct scsipi_generic *) &scsipi_cmd, sizeof(scsipi_cmd),
     76 	    0, 0, 2, 100000, NULL, flags));
     77 }
     78 
     79 /*
     80  * ask the scsi driver to perform a command for us.
     81  * tell it where to read/write the data, and how
     82  * long the data is supposed to be. If we have  a buf
     83  * to associate with the transfer, we need that too.
     84  */
     85 int
     86 scsi_scsipi_cmd(sc_link, scsipi_cmd, cmdlen, data_addr, datalen,
     87 	retries, timeout, bp, flags)
     88 	struct scsipi_link *sc_link;
     89 	struct scsipi_generic *scsipi_cmd;
     90 	int cmdlen;
     91 	u_char *data_addr;
     92 	int datalen;
     93 	int retries;
     94 	int timeout;
     95 	struct buf *bp;
     96 	int flags;
     97 {
     98 	struct scsipi_xfer *xs;
     99 	int error;
    100 
    101 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_scsipi_cmd\n"));
    102 
    103 #ifdef DIAGNOSTIC
    104 	if (bp != 0 && (flags & SCSI_NOSLEEP) == 0)
    105 		panic("scsi_scsipi_cmd: buffer without nosleep");
    106 #endif
    107 
    108 	if ((xs = scsipi_make_xs(sc_link, scsipi_cmd, cmdlen, data_addr,
    109 	    datalen, retries, timeout, bp, flags)) == NULL)
    110 		return (ENOMEM);
    111 
    112 	/*
    113 	 * Set the LUN in the CDB if we have an older device.  We also
    114 	 * set it for more modern SCSI-II devices "just in case".
    115 	 */
    116 	if ((sc_link->scsipi_scsi.scsi_version & SID_ANSII) <= 2)
    117 		xs->cmd->bytes[0] |=
    118 		    ((sc_link->scsipi_scsi.lun << SCSI_CMD_LUN_SHIFT) &
    119 			SCSI_CMD_LUN_MASK);
    120 
    121 	if ((error = scsipi_execute_xs(xs)) == EJUSTRETURN)
    122 		return (0);
    123 
    124 	/*
    125 	 * we have finished with the xfer stuct, free it and
    126 	 * check if anyone else needs to be started up.
    127 	 */
    128 	scsipi_free_xs(xs, flags);
    129 	return (error);
    130 }
    131 
    132 /*
    133  * Look at the returned sense and act on the error, determining
    134  * the unix error number to pass back.  (0 = report no error)
    135  *
    136  * THIS IS THE DEFAULT ERROR HANDLER FOR SCSI DEVICES
    137  */
    138 int
    139 scsi_interpret_sense(xs)
    140 	struct scsipi_xfer *xs;
    141 {
    142 	struct scsipi_sense_data *sense;
    143 	struct scsipi_link *sc_link = xs->sc_link;
    144 	u_int8_t key;
    145 	u_int32_t info;
    146 	int error;
    147 #ifndef	SCSIVERBOSE
    148 	static char *error_mes[] = {
    149 		"soft error (corrected)",
    150 		"not ready", "medium error",
    151 		"non-media hardware failure", "illegal request",
    152 		"unit attention", "readonly device",
    153 		"no data found", "vendor unique",
    154 		"copy aborted", "command aborted",
    155 		"search returned equal", "volume overflow",
    156 		"verify miscompare", "unknown error key"
    157 	};
    158 #endif
    159 
    160 	sense = &xs->sense.scsi_sense;
    161 #ifdef	SCSIDEBUG
    162 	if ((sc_link->flags & SDEV_DB1) != 0) {
    163 		int count;
    164 		printf("code 0x%x valid 0x%x ",
    165 			sense->error_code & SSD_ERRCODE,
    166 			sense->error_code & SSD_ERRCODE_VALID ? 1 : 0);
    167 		printf("seg 0x%x key 0x%x ili 0x%x eom 0x%x fmark 0x%x\n",
    168 			sense->segment,
    169 			sense->flags & SSD_KEY,
    170 			sense->flags & SSD_ILI ? 1 : 0,
    171 			sense->flags & SSD_EOM ? 1 : 0,
    172 			sense->flags & SSD_FILEMARK ? 1 : 0);
    173 		printf("info: 0x%x 0x%x 0x%x 0x%x followed by %d extra bytes\n",
    174 			sense->info[0],
    175 			sense->info[1],
    176 			sense->info[2],
    177 			sense->info[3],
    178 			sense->extra_len);
    179 		printf("extra: ");
    180 		for (count = 0; count < ADD_BYTES_LIM(sense); count++)
    181 			printf("0x%x ", sense->cmd_spec_info[count]);
    182 		printf("\n");
    183 	}
    184 #endif	/* SCSIDEBUG */
    185 	/*
    186 	 * If the device has it's own error handler, call it first.
    187 	 * If it returns a legit error value, return that, otherwise
    188 	 * it wants us to continue with normal error processing.
    189 	 */
    190 	if (sc_link->device->err_handler) {
    191 		SC_DEBUG(sc_link, SDEV_DB2,
    192 		    ("calling private err_handler()\n"));
    193 		error = (*sc_link->device->err_handler)(xs);
    194 		if (error != -1)
    195 			return (error);		/* error >= 0  better ? */
    196 	}
    197 	/* otherwise use the default */
    198 	switch (sense->error_code & SSD_ERRCODE) {
    199 		/*
    200 		 * If it's code 70, use the extended stuff and
    201 		 * interpret the key
    202 		 */
    203 	case 0x71:		/* delayed error */
    204 		sc_link->sc_print_addr(sc_link);
    205 		key = sense->flags & SSD_KEY;
    206 		printf(" DEFERRED ERROR, key = 0x%x\n", key);
    207 		/* FALLTHROUGH */
    208 	case 0x70:
    209 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0)
    210 			info = _4btol(sense->info);
    211 		else
    212 			info = 0;
    213 		key = sense->flags & SSD_KEY;
    214 
    215 		switch (key) {
    216 		case 0x0:	/* NO SENSE */
    217 		case 0x1:	/* RECOVERED ERROR */
    218 			if (xs->resid == xs->datalen)
    219 				xs->resid = 0;	/* not short read */
    220 		case 0xc:	/* EQUAL */
    221 			error = 0;
    222 			break;
    223 		case 0x2:	/* NOT READY */
    224 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    225 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    226 			if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
    227 				return (0);
    228 			if ((xs->flags & SCSI_SILENT) != 0)
    229 				return (EIO);
    230 			error = EIO;
    231 			break;
    232 		case 0x5:	/* ILLEGAL REQUEST */
    233 			if ((xs->flags & SCSI_IGNORE_ILLEGAL_REQUEST) != 0)
    234 				return (0);
    235 			if ((xs->flags & SCSI_SILENT) != 0)
    236 				return (EIO);
    237 			error = EINVAL;
    238 			break;
    239 		case 0x6:	/* UNIT ATTENTION */
    240 			if ((sc_link->flags & SDEV_REMOVABLE) != 0)
    241 				sc_link->flags &= ~SDEV_MEDIA_LOADED;
    242 			if ((xs->flags & SCSI_IGNORE_MEDIA_CHANGE) != 0 ||
    243 				/* XXX Should reupload any transient state. */
    244 				(sc_link->flags & SDEV_REMOVABLE) == 0)
    245 				return (ERESTART);
    246 			if ((xs->flags & SCSI_SILENT) != 0)
    247 				return (EIO);
    248 			error = EIO;
    249 			break;
    250 		case 0x7:	/* DATA PROTECT */
    251 			error = EROFS;
    252 			break;
    253 		case 0x8:	/* BLANK CHECK */
    254 			error = 0;
    255 			break;
    256 		case 0xb:	/* COMMAND ABORTED */
    257 			error = ERESTART;
    258 			break;
    259 		case 0xd:	/* VOLUME OVERFLOW */
    260 			error = ENOSPC;
    261 			break;
    262 		default:
    263 			error = EIO;
    264 			break;
    265 		}
    266 
    267 #ifdef SCSIVERBOSE
    268 		if ((xs->flags & SCSI_SILENT) == 0)
    269 			scsi_print_sense(xs, 0);
    270 #else
    271 		if (key) {
    272 			sc_link->sc_print_addr(sc_link);
    273 			printf("%s", error_mes[key - 1]);
    274 			if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    275 				switch (key) {
    276 				case 0x2:	/* NOT READY */
    277 				case 0x5:	/* ILLEGAL REQUEST */
    278 				case 0x6:	/* UNIT ATTENTION */
    279 				case 0x7:	/* DATA PROTECT */
    280 					break;
    281 				case 0x8:	/* BLANK CHECK */
    282 					printf(", requested size: %d (decimal)",
    283 					    info);
    284 					break;
    285 				case 0xb:
    286 					if (xs->retries)
    287 						printf(", retrying");
    288 					printf(", cmd 0x%x, info 0x%x",
    289 					    xs->cmd->opcode, info);
    290 					break;
    291 				default:
    292 					printf(", info = %d (decimal)", info);
    293 				}
    294 			}
    295 			if (sense->extra_len != 0) {
    296 				int n;
    297 				printf(", data =");
    298 				for (n = 0; n < sense->extra_len; n++)
    299 					printf(" %02x",
    300 					    sense->cmd_spec_info[n]);
    301 			}
    302 			printf("\n");
    303 		}
    304 #endif
    305 		return (error);
    306 
    307 	/*
    308 	 * Not code 70, just report it
    309 	 */
    310 	default:
    311 		sc_link->sc_print_addr(sc_link);
    312 		printf("error code %d", sense->error_code & SSD_ERRCODE);
    313 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
    314 			struct scsipi_sense_data_unextended *usense =
    315 			    (struct scsipi_sense_data_unextended *)sense;
    316 			printf(" at block no. %d (decimal)",
    317 			    _3btol(usense->block));
    318 		}
    319 		printf("\n");
    320 		return (EIO);
    321 	}
    322 }
    323 
    324 /*
    325  * Utility routines often used in SCSI stuff
    326  */
    327 
    328 
    329 /*
    330  * Print out the scsi_link structure's address info.
    331  */
    332 void
    333 scsi_print_addr(sc_link)
    334 	struct scsipi_link *sc_link;
    335 {
    336 
    337 	printf("%s(%s:%d:%d): ",
    338 	    sc_link->device_softc ?
    339 	    ((struct device *)sc_link->device_softc)->dv_xname : "probe",
    340 	    ((struct device *)sc_link->adapter_softc)->dv_xname,
    341 	    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun);
    342 }
    343 
    344 #ifdef SCSIVERBOSE
    345 static const char *sense_keys[16] = {
    346 	"No Additional Sense",
    347 	"Soft Error",
    348 	"Not Ready",
    349 	"Media Error",
    350 	"Hardware Error",
    351 	"Illegal Request",
    352 	"Unit Attention",
    353 	"Write Protected",
    354 	"Blank Check",
    355 	"Vendor Unique",
    356 	"Copy Aborted",
    357 	"Aborted Command",
    358 	"Equal Error",
    359 	"Volume Overflow",
    360 	"Miscompare Error",
    361 	"Reserved"
    362 };
    363 static const struct {
    364 	unsigned char asc;
    365 	unsigned char ascq;
    366 	char *description;
    367 } adesc[] = {
    368 { 0x00, 0x00, "No Additional Sense Information" },
    369 { 0x00, 0x01, "Filemark Detected" },
    370 { 0x00, 0x02, "End-Of-Partition/Medium Detected" },
    371 { 0x00, 0x03, "Setmark Detected" },
    372 { 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
    373 { 0x00, 0x05, "End-Of-Data Detected" },
    374 { 0x00, 0x06, "I/O Process Terminated" },
    375 { 0x00, 0x11, "Audio Play Operation In Progress" },
    376 { 0x00, 0x12, "Audio Play Operation Paused" },
    377 { 0x00, 0x13, "Audio Play Operation Successfully Completed" },
    378 { 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
    379 { 0x00, 0x15, "No Current Audio Status To Return" },
    380 { 0x01, 0x00, "No Index/Sector Signal" },
    381 { 0x02, 0x00, "No Seek Complete" },
    382 { 0x03, 0x00, "Peripheral Device Write Fault" },
    383 { 0x03, 0x01, "No Write Current" },
    384 { 0x03, 0x02, "Excessive Write Errors" },
    385 { 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
    386 { 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
    387 { 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
    388 { 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
    389 { 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
    390 { 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
    391 { 0x06, 0x00, "No Reference Position Found" },
    392 { 0x07, 0x00, "Multiple Peripheral Devices Selected" },
    393 { 0x08, 0x00, "Logical Unit Communication Failure" },
    394 { 0x08, 0x01, "Logical Unit Communication Timeout" },
    395 { 0x08, 0x02, "Logical Unit Communication Parity Error" },
    396 { 0x09, 0x00, "Track Following Error" },
    397 { 0x09, 0x01, "Tracking Servo Failure" },
    398 { 0x09, 0x02, "Focus Servo Failure" },
    399 { 0x09, 0x03, "Spindle Servo Failure" },
    400 { 0x0A, 0x00, "Error Log Overflow" },
    401 { 0x0C, 0x00, "Write Error" },
    402 { 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
    403 { 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
    404 { 0x10, 0x00, "ID CRC Or ECC Error" },
    405 { 0x11, 0x00, "Unrecovered Read Error" },
    406 { 0x11, 0x01, "Read Retried Exhausted" },
    407 { 0x11, 0x02, "Error Too Long To Correct" },
    408 { 0x11, 0x03, "Multiple Read Errors" },
    409 { 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
    410 { 0x11, 0x05, "L-EC Uncorrectable Error" },
    411 { 0x11, 0x06, "CIRC Unrecovered Error" },
    412 { 0x11, 0x07, "Data Resynchronization Error" },
    413 { 0x11, 0x08, "Incomplete Block Found" },
    414 { 0x11, 0x09, "No Gap Found" },
    415 { 0x11, 0x0A, "Miscorrected Error" },
    416 { 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
    417 { 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite the Data" },
    418 { 0x12, 0x00, "Address Mark Not Found for ID Field" },
    419 { 0x13, 0x00, "Address Mark Not Found for Data Field" },
    420 { 0x14, 0x00, "Recorded Entity Not Found" },
    421 { 0x14, 0x01, "Record Not Found" },
    422 { 0x14, 0x02, "Filemark or Setmark Not Found" },
    423 { 0x14, 0x03, "End-Of-Data Not Found" },
    424 { 0x14, 0x04, "Block Sequence Error" },
    425 { 0x15, 0x00, "Random Positioning Error" },
    426 { 0x15, 0x01, "Mechanical Positioning Error" },
    427 { 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
    428 { 0x16, 0x00, "Data Synchronization Mark Error" },
    429 { 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
    430 { 0x17, 0x01, "Recovered Data With Retries" },
    431 { 0x17, 0x02, "Recovered Data With Positive Head Offset" },
    432 { 0x17, 0x03, "Recovered Data With Negative Head Offset" },
    433 { 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
    434 { 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
    435 { 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
    436 { 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
    437 { 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
    438 { 0x18, 0x00, "Recovered Data With Error Correction Applied" },
    439 { 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
    440 { 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
    441 { 0x18, 0x03, "Recovered Data With CIRC" },
    442 { 0x18, 0x04, "Recovered Data With LEC" },
    443 { 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
    444 { 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
    445 { 0x19, 0x00, "Defect List Error" },
    446 { 0x19, 0x01, "Defect List Not Available" },
    447 { 0x19, 0x02, "Defect List Error in Primary List" },
    448 { 0x19, 0x03, "Defect List Error in Grown List" },
    449 { 0x1A, 0x00, "Parameter List Length Error" },
    450 { 0x1B, 0x00, "Synchronous Data Transfer Error" },
    451 { 0x1C, 0x00, "Defect List Not Found" },
    452 { 0x1C, 0x01, "Primary Defect List Not Found" },
    453 { 0x1C, 0x02, "Grown Defect List Not Found" },
    454 { 0x1D, 0x00, "Miscompare During Verify Operation" },
    455 { 0x1E, 0x00, "Recovered ID with ECC" },
    456 { 0x20, 0x00, "Invalid Command Operation Code" },
    457 { 0x21, 0x00, "Logical Block Address Out of Range" },
    458 { 0x21, 0x01, "Invalid Element Address" },
    459 { 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
    460 { 0x24, 0x00, "Illegal Field in CDB" },
    461 { 0x25, 0x00, "Logical Unit Not Supported" },
    462 { 0x26, 0x00, "Invalid Field In Parameter List" },
    463 { 0x26, 0x01, "Parameter Not Supported" },
    464 { 0x26, 0x02, "Parameter Value Invalid" },
    465 { 0x26, 0x03, "Threshold Parameters Not Supported" },
    466 { 0x27, 0x00, "Write Protected" },
    467 { 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
    468 { 0x28, 0x01, "Import Or Export Element Accessed" },
    469 { 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
    470 { 0x2A, 0x00, "Parameters Changed" },
    471 { 0x2A, 0x01, "Mode Parameters Changed" },
    472 { 0x2A, 0x02, "Log Parameters Changed" },
    473 { 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
    474 { 0x2C, 0x00, "Command Sequence Error" },
    475 { 0x2C, 0x01, "Too Many Windows Specified" },
    476 { 0x2C, 0x02, "Invalid Combination of Windows Specified" },
    477 { 0x2D, 0x00, "Overwrite Error On Update In Place" },
    478 { 0x2F, 0x00, "Commands Cleared By Another Initiator" },
    479 { 0x30, 0x00, "Incompatible Medium Installed" },
    480 { 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
    481 { 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
    482 { 0x30, 0x03, "Cleaning Cartridge Installed" },
    483 { 0x31, 0x00, "Medium Format Corrupted" },
    484 { 0x31, 0x01, "Format Command Failed" },
    485 { 0x32, 0x00, "No Defect Spare Location Available" },
    486 { 0x32, 0x01, "Defect List Update Failure" },
    487 { 0x33, 0x00, "Tape Length Error" },
    488 { 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
    489 { 0x37, 0x00, "Rounded Parameter" },
    490 { 0x39, 0x00, "Saving Parameters Not Supported" },
    491 { 0x3A, 0x00, "Medium Not Present" },
    492 { 0x3B, 0x00, "Positioning Error" },
    493 { 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
    494 { 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
    495 { 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
    496 { 0x3B, 0x04, "Slew Failure" },
    497 { 0x3B, 0x05, "Paper Jam" },
    498 { 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
    499 { 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
    500 { 0x3B, 0x08, "Reposition Error" },
    501 { 0x3B, 0x09, "Read Past End Of Medium" },
    502 { 0x3B, 0x0A, "Read Past Begining Of Medium" },
    503 { 0x3B, 0x0B, "Position Past End Of Medium" },
    504 { 0x3B, 0x0C, "Position Past Beginning Of Medium" },
    505 { 0x3B, 0x0D, "Medium Destination Element Full" },
    506 { 0x3B, 0x0E, "Medium Source Element Empty" },
    507 { 0x3D, 0x00, "Invalid Bits In IDENTFY Message" },
    508 { 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
    509 { 0x3F, 0x00, "Target Operating Conditions Have Changed" },
    510 { 0x3F, 0x01, "Microcode Has Changed" },
    511 { 0x3F, 0x02, "Changed Operating Definition" },
    512 { 0x3F, 0x03, "INQUIRY Data Has Changed" },
    513 { 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
    514 { 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
    515 { 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
    516 { 0x43, 0x00, "Message Error" },
    517 { 0x44, 0x00, "Internal Target Failure" },
    518 { 0x45, 0x00, "Select Or Reselect Failure" },
    519 { 0x46, 0x00, "Unsuccessful Soft Reset" },
    520 { 0x47, 0x00, "SCSI Parity Error" },
    521 { 0x48, 0x00, "INITIATOR DETECTED ERROR Message Received" },
    522 { 0x49, 0x00, "Invalid Message Error" },
    523 { 0x4A, 0x00, "Command Phase Error" },
    524 { 0x4B, 0x00, "Data Phase Error" },
    525 { 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
    526 { 0x4E, 0x00, "Overlapped Commands Attempted" },
    527 { 0x50, 0x00, "Write Append Error" },
    528 { 0x50, 0x01, "Write Append Position Error" },
    529 { 0x50, 0x01, "Write Append Position Error" },
    530 { 0x50, 0x02, "Position Error Related To Timing" },
    531 { 0x51, 0x00, "Erase Failure" },
    532 { 0x52, 0x00, "Cartridge Fault" },
    533 { 0x53, 0x00, "Media Load or Eject Failed" },
    534 { 0x53, 0x01, "Unload Tape Failure" },
    535 { 0x53, 0x02, "Medium Removal Prevented" },
    536 { 0x54, 0x00, "SCSI To Host System Interface Failure" },
    537 { 0x55, 0x00, "System Resource Failure" },
    538 { 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
    539 { 0x58, 0x00, "Generation Does Not Exist" },
    540 { 0x59, 0x00, "Updated Block Read" },
    541 { 0x5A, 0x00, "Operator Request or State Change Input (Unspecified)" },
    542 { 0x5A, 0x01, "Operator Medium Removal Requested" },
    543 { 0x5A, 0x02, "Operator Selected Write Protect" },
    544 { 0x5A, 0x03, "Operator Selected Write Permit" },
    545 { 0x5B, 0x00, "Log Exception" },
    546 { 0x5B, 0x01, "Threshold Condition Met" },
    547 { 0x5B, 0x02, "Log Counter At Maximum" },
    548 { 0x5B, 0x03, "Log List Codes Exhausted" },
    549 { 0x5C, 0x00, "RPL Status Change" },
    550 { 0x5C, 0x01, "Spindles Synchronized" },
    551 { 0x5C, 0x02, "Spindles Not Synchronized" },
    552 { 0x60, 0x00, "Lamp Failure" },
    553 { 0x61, 0x00, "Video Acquisition Error" },
    554 { 0x61, 0x01, "Unable To Acquire Video" },
    555 { 0x61, 0x02, "Out Of Focus" },
    556 { 0x62, 0x00, "Scan Head Positioning Error" },
    557 { 0x63, 0x00, "End Of User Area Encountered On This Track" },
    558 { 0x64, 0x00, "Illegal Mode For This Track" },
    559 { 0x00, 0x00, NULL }
    560 };
    561 
    562 static inline void
    563 asc2ascii(asc, ascq, result)
    564 	unsigned char asc, ascq;
    565 	char *result;
    566 {
    567 	register int i = 0;
    568 
    569 	while (adesc[i].description != NULL) {
    570 		if (adesc[i].asc == asc && adesc[i].ascq == ascq)
    571 			break;
    572 		i++;
    573 	}
    574 	if (adesc[i].description == NULL) {
    575 		if (asc == 0x40 && ascq != 0)
    576 			(void)sprintf(result,
    577 			    "Diagnostic Failure on Component 0x%02x",
    578 			    ascq & 0xff);
    579 		else
    580 			(void)sprintf(result, "ASC 0x%02x ASCQ 0x%02x",
    581 			    asc & 0xff, ascq & 0xff);
    582 	} else
    583 		(void)strcpy(result, adesc[i].description);
    584 }
    585 
    586 void
    587 scsi_print_sense(xs, verbosity)
    588 	struct scsipi_xfer *xs;
    589 	int verbosity;
    590 {
    591 	int32_t info;
    592 	register int i, j, k;
    593 	char *sbs, *s;
    594 
    595 	xs->sc_link->sc_print_addr(xs->sc_link);
    596 	s = (char *) &xs->sense.scsi_sense;
    597 	printf(" Check Condition on opcode 0x%x\n", xs->cmd->opcode);
    598 
    599 	/*
    600 	 * Basics- print out SENSE KEY
    601 	 */
    602 	printf("	SENSE KEY:  %s", scsi_decode_sense(s, 0));
    603 
    604 	/*
    605 	 * Print out, unqualified but aligned, FMK, EOM and ILI status.
    606 	 */
    607 	if (s[2] & 0xe0) {
    608 		char pad;
    609 		printf("\n			  ");
    610 		pad = ' ';
    611 		if (s[2] & SSD_FILEMARK) {
    612 			printf("%c Filemark Detected", pad);
    613 			pad = ',';
    614 		}
    615 		if (s[2] & SSD_EOM) {
    616 			printf("%c EOM Detected", pad);
    617 			pad = ',';
    618 		}
    619 		if (s[2] & SSD_ILI)
    620 			printf("%c Incorrect Length Indicator Set", pad);
    621 	}
    622 
    623 	/*
    624 	 * Now we should figure out, based upon device type, how
    625 	 * to format the information field. Unfortunately, that's
    626 	 * not convenient here, so we'll print it as a signed
    627 	 * 32 bit integer.
    628 	 */
    629 	info = _4btol(&s[3]);
    630 	if (info)
    631 		printf("\n   INFO FIELD:  %d", info);
    632 
    633 	/*
    634 	 * Now we check additional length to see whether there is
    635 	 * more information to extract.
    636 	 */
    637 
    638 	/* enough for command specific information? */
    639 	if (s[7] < 4) {
    640 		printf("\n");
    641 		return;
    642 	}
    643 	info = _4btol(&s[8]);
    644 	if (info)
    645 		printf("\n COMMAND INFO:  %d (0x%x)", info, info);
    646 
    647 	/*
    648 	 * Decode ASC && ASCQ info, plus FRU, plus the rest...
    649 	 */
    650 
    651 	sbs = scsi_decode_sense(s, 1);
    652 	if (sbs)
    653 		printf("\n     ASC/ASCQ:  %s", sbs);
    654 	if (s[14] != 0)
    655 		printf("\n     FRU CODE:  0x%x", s[14] & 0xff);
    656 	sbs = scsi_decode_sense(s, 3);
    657 	if (sbs)
    658 		printf("\n         SKSV:  %s", sbs);
    659 	printf("\n");
    660 	if (verbosity == 0) {
    661 		printf("\n");
    662 		return;
    663 	}
    664 
    665 	/*
    666 	 * Now figure whether we should print any additional informtion.
    667 	 *
    668 	 * Where should we start from? If we had SKSV data,
    669 	 * start from offset 18, else from offset 15.
    670 	 *
    671 	 * From that point until the end of the buffer, check for any
    672 	 * nonzero data. If we have some, go back and print the lot,
    673 	 * otherwise we're done.
    674 	 */
    675 	if (sbs)
    676 		i = 18;
    677 	else
    678 		i = 15;
    679 	for (j = i; j < sizeof (xs->sense); j++)
    680 		if (s[j])
    681 			break;
    682 	if (j == sizeof (xs->sense))
    683 		return;
    684 
    685 	printf("\n Additional Sense Information (byte %d out...):\n", i);
    686 	if (i == 15) {
    687 		printf("\n\t%2d:", i);
    688 		k = 7;
    689 	} else {
    690 		printf("\n\t%2d:", i);
    691 		k = 2;
    692 		j -= 2;
    693 	}
    694 	while (j > 0) {
    695 		if (i >= sizeof (xs->sense))
    696 			break;
    697 		if (k == 8) {
    698 			k = 0;
    699 			printf("\n\t%2d:", i);
    700 		}
    701 		printf(" 0x%02x", s[i] & 0xff);
    702 		k++;
    703 		j--;
    704 		i++;
    705 	}
    706 	printf("\n\n");
    707 }
    708 
    709 char *
    710 scsi_decode_sense(sinfo, flag)
    711 	void *sinfo;
    712 	int flag;
    713 {
    714 	unsigned char *snsbuf;
    715 	unsigned char skey;
    716 	static char rqsbuf[132];
    717 
    718 	skey = 0;
    719 
    720 	snsbuf = (unsigned char *) sinfo;
    721 	if (flag == 0 || flag == 2 || flag == 3)
    722 		skey = snsbuf[2] & 0xf;
    723 	if (flag == 0) {			/* Sense Key Only */
    724 		(void) strcpy(rqsbuf, sense_keys[skey]);
    725 		return (rqsbuf);
    726 	} else if (flag == 1) {			/* ASC/ASCQ Only */
    727 		asc2ascii(snsbuf[12], snsbuf[13], rqsbuf);
    728 		return (rqsbuf);
    729 	} else  if (flag == 2) {		/* Sense Key && ASC/ASCQ */
    730 		auto char localbuf[64];
    731 		asc2ascii(snsbuf[12], snsbuf[13], localbuf);
    732 		(void) sprintf(rqsbuf, "%s, %s", sense_keys[skey], localbuf);
    733 		return (rqsbuf);
    734 	} else if (flag == 3 && snsbuf[7] >= 9 && (snsbuf[15] & 0x80)) {
    735 		/*
    736 		 * SKSV Data
    737 		 */
    738 		switch (skey) {
    739 		case 0x5:			/* Illegal Request */
    740 			if (snsbuf[15] & 0x8)
    741 				(void)sprintf(rqsbuf,
    742 				    "Error in %s, Offset %d, bit %d",
    743 				    (snsbuf[15] & 0x40)? "CDB" : "Parameters",
    744 				    (snsbuf[16] & 0xff) << 8 |
    745 				    (snsbuf[17] & 0xff), snsbuf[15] & 0x7);
    746 			else
    747 				(void)sprintf(rqsbuf,
    748 				    "Error in %s, Offset %d",
    749 				    (snsbuf[15] & 0x40)? "CDB" : "Parameters",
    750 				    (snsbuf[16] & 0xff) << 8 |
    751 				    (snsbuf[17] & 0xff));
    752 			return (rqsbuf);
    753 		case 0x1:
    754 		case 0x3:
    755 		case 0x4:
    756 			(void)sprintf(rqsbuf, "Actual Retry Count: %d",
    757 			    (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
    758 			return (rqsbuf);
    759 		case 0x2:
    760 			(void)sprintf(rqsbuf, "Progress Indicator: %d",
    761 			    (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
    762 			return (rqsbuf);
    763 		default:
    764 			break;
    765 		}
    766 	}
    767 	return (NULL);
    768 }
    769 #endif
    770