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