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