Home | History | Annotate | Line # | Download | only in scsictl
      1  1.11   andvar /*	$NetBSD: scsi_sense.c,v 1.11 2023/12/28 19:58:11 andvar Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4   1.1  thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7   1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  thorpej  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
      9   1.1  thorpej  * Simulation Facility, NASA Ames Research Center.
     10   1.1  thorpej  *
     11   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.1  thorpej  * modification, are permitted provided that the following conditions
     13   1.1  thorpej  * are met:
     14   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.1  thorpej  *
     20   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  thorpej  */
     32   1.1  thorpej 
     33   1.1  thorpej /*
     34   1.1  thorpej  * SCSI sense interpretation.  Lifted from src/sys/dev/scsipi/scsi_verbose.c,
     35   1.1  thorpej  * and modified for userland.
     36   1.1  thorpej  *
     37   1.1  thorpej  * XXX THESE SHOULD BE IN A LIBRARY!
     38   1.1  thorpej  */
     39   1.5      agc #include <sys/cdefs.h>
     40   1.5      agc 
     41   1.5      agc #ifndef lint
     42  1.11   andvar __RCSID("$NetBSD: scsi_sense.c,v 1.11 2023/12/28 19:58:11 andvar Exp $");
     43   1.5      agc #endif
     44   1.5      agc 
     45   1.1  thorpej 
     46   1.1  thorpej #include <sys/param.h>
     47   1.1  thorpej #include <sys/scsiio.h>
     48   1.1  thorpej #include <stdio.h>
     49   1.1  thorpej #include <string.h>
     50   1.1  thorpej 
     51   1.1  thorpej #include <dev/scsipi/scsipi_all.h>
     52   1.1  thorpej #include <dev/scsipi/scsi_all.h>
     53   1.1  thorpej #include <dev/scsipi/scsipiconf.h>
     54   1.1  thorpej 
     55   1.1  thorpej #include "extern.h"
     56   1.1  thorpej 
     57   1.1  thorpej static const char *sense_keys[16] = {
     58   1.1  thorpej 	"No Additional Sense",
     59   1.1  thorpej 	"Recovered Error",
     60   1.1  thorpej 	"Not Ready",
     61   1.1  thorpej 	"Media Error",
     62   1.1  thorpej 	"Hardware Error",
     63   1.1  thorpej 	"Illegal Request",
     64   1.1  thorpej 	"Unit Attention",
     65   1.1  thorpej 	"Write Protected",
     66   1.1  thorpej 	"Blank Check",
     67   1.1  thorpej 	"Vendor Unique",
     68   1.1  thorpej 	"Copy Aborted",
     69   1.1  thorpej 	"Aborted Command",
     70   1.1  thorpej 	"Equal Error",
     71   1.1  thorpej 	"Volume Overflow",
     72   1.1  thorpej 	"Miscompare Error",
     73   1.1  thorpej 	"Reserved"
     74   1.1  thorpej };
     75   1.1  thorpej 
     76   1.1  thorpej static const struct {
     77   1.1  thorpej 	unsigned char asc;
     78   1.1  thorpej 	unsigned char ascq;
     79   1.1  thorpej 	const char *description;
     80   1.1  thorpej } adesc[] = {
     81   1.1  thorpej { 0x00, 0x00, "No Additional Sense Information" },
     82   1.1  thorpej { 0x00, 0x01, "Filemark Detected" },
     83   1.1  thorpej { 0x00, 0x02, "End-Of-Partition/Medium Detected" },
     84   1.1  thorpej { 0x00, 0x03, "Setmark Detected" },
     85   1.1  thorpej { 0x00, 0x04, "Beginning-Of-Partition/Medium Detected" },
     86   1.1  thorpej { 0x00, 0x05, "End-Of-Data Detected" },
     87   1.1  thorpej { 0x00, 0x06, "I/O Process Terminated" },
     88   1.1  thorpej { 0x00, 0x11, "Audio Play Operation In Progress" },
     89   1.1  thorpej { 0x00, 0x12, "Audio Play Operation Paused" },
     90   1.1  thorpej { 0x00, 0x13, "Audio Play Operation Successfully Completed" },
     91   1.1  thorpej { 0x00, 0x14, "Audio Play Operation Stopped Due to Error" },
     92   1.1  thorpej { 0x00, 0x15, "No Current Audio Status To Return" },
     93   1.1  thorpej { 0x01, 0x00, "No Index/Sector Signal" },
     94   1.1  thorpej { 0x02, 0x00, "No Seek Complete" },
     95   1.1  thorpej { 0x03, 0x00, "Peripheral Device Write Fault" },
     96   1.1  thorpej { 0x03, 0x01, "No Write Current" },
     97   1.1  thorpej { 0x03, 0x02, "Excessive Write Errors" },
     98   1.1  thorpej { 0x04, 0x00, "Logical Unit Not Ready, Cause Not Reportable" },
     99   1.1  thorpej { 0x04, 0x01, "Logical Unit Is in Process Of Becoming Ready" },
    100   1.1  thorpej { 0x04, 0x02, "Logical Unit Not Ready, Initialization Command Required" },
    101   1.1  thorpej { 0x04, 0x03, "Logical Unit Not Ready, Manual Intervention Required" },
    102   1.1  thorpej { 0x04, 0x04, "Logical Unit Not Ready, Format In Progress" },
    103   1.1  thorpej { 0x05, 0x00, "Logical Unit Does Not Respond To Selection" },
    104   1.1  thorpej { 0x06, 0x00, "No Reference Position Found" },
    105   1.1  thorpej { 0x07, 0x00, "Multiple Peripheral Devices Selected" },
    106   1.1  thorpej { 0x08, 0x00, "Logical Unit Communication Failure" },
    107   1.1  thorpej { 0x08, 0x01, "Logical Unit Communication Timeout" },
    108   1.1  thorpej { 0x08, 0x02, "Logical Unit Communication Parity Error" },
    109   1.1  thorpej { 0x09, 0x00, "Track Following Error" },
    110   1.1  thorpej { 0x09, 0x01, "Tracking Servo Failure" },
    111   1.1  thorpej { 0x09, 0x02, "Focus Servo Failure" },
    112   1.1  thorpej { 0x09, 0x03, "Spindle Servo Failure" },
    113   1.1  thorpej { 0x0A, 0x00, "Error Log Overflow" },
    114   1.1  thorpej { 0x0C, 0x00, "Write Error" },
    115   1.1  thorpej { 0x0C, 0x01, "Write Error Recovered with Auto Reallocation" },
    116   1.1  thorpej { 0x0C, 0x02, "Write Error - Auto Reallocate Failed" },
    117   1.1  thorpej { 0x10, 0x00, "ID CRC Or ECC Error" },
    118   1.1  thorpej { 0x11, 0x00, "Unrecovered Read Error" },
    119   1.1  thorpej { 0x11, 0x01, "Read Retried Exhausted" },
    120   1.1  thorpej { 0x11, 0x02, "Error Too Long To Correct" },
    121   1.1  thorpej { 0x11, 0x03, "Multiple Read Errors" },
    122   1.1  thorpej { 0x11, 0x04, "Unrecovered Read Error - Auto Reallocate Failed" },
    123   1.1  thorpej { 0x11, 0x05, "L-EC Uncorrectable Error" },
    124   1.1  thorpej { 0x11, 0x06, "CIRC Unrecovered Error" },
    125   1.1  thorpej { 0x11, 0x07, "Data Resynchronization Error" },
    126   1.1  thorpej { 0x11, 0x08, "Incomplete Block Found" },
    127   1.1  thorpej { 0x11, 0x09, "No Gap Found" },
    128   1.1  thorpej { 0x11, 0x0A, "Miscorrected Error" },
    129   1.1  thorpej { 0x11, 0x0B, "Uncorrected Read Error - Recommend Reassignment" },
    130   1.1  thorpej { 0x11, 0x0C, "Uncorrected Read Error - Recommend Rewrite the Data" },
    131   1.1  thorpej { 0x12, 0x00, "Address Mark Not Found for ID Field" },
    132   1.1  thorpej { 0x13, 0x00, "Address Mark Not Found for Data Field" },
    133   1.1  thorpej { 0x14, 0x00, "Recorded Entity Not Found" },
    134   1.1  thorpej { 0x14, 0x01, "Record Not Found" },
    135   1.1  thorpej { 0x14, 0x02, "Filemark or Setmark Not Found" },
    136   1.1  thorpej { 0x14, 0x03, "End-Of-Data Not Found" },
    137   1.1  thorpej { 0x14, 0x04, "Block Sequence Error" },
    138   1.1  thorpej { 0x15, 0x00, "Random Positioning Error" },
    139   1.1  thorpej { 0x15, 0x01, "Mechanical Positioning Error" },
    140   1.1  thorpej { 0x15, 0x02, "Positioning Error Detected By Read of Medium" },
    141   1.1  thorpej { 0x16, 0x00, "Data Synchronization Mark Error" },
    142   1.1  thorpej { 0x17, 0x00, "Recovered Data With No Error Correction Applied" },
    143   1.1  thorpej { 0x17, 0x01, "Recovered Data With Retries" },
    144   1.1  thorpej { 0x17, 0x02, "Recovered Data With Positive Head Offset" },
    145   1.1  thorpej { 0x17, 0x03, "Recovered Data With Negative Head Offset" },
    146   1.1  thorpej { 0x17, 0x04, "Recovered Data With Retries and/or CIRC Applied" },
    147   1.1  thorpej { 0x17, 0x05, "Recovered Data Using Previous Sector ID" },
    148   1.1  thorpej { 0x17, 0x06, "Recovered Data Without ECC - Data Auto-Reallocated" },
    149   1.1  thorpej { 0x17, 0x07, "Recovered Data Without ECC - Recommend Reassignment" },
    150   1.1  thorpej { 0x17, 0x08, "Recovered Data Without ECC - Recommend Rewrite" },
    151   1.1  thorpej { 0x18, 0x00, "Recovered Data With Error Correction Applied" },
    152   1.1  thorpej { 0x18, 0x01, "Recovered Data With Error Correction & Retries Applied" },
    153   1.1  thorpej { 0x18, 0x02, "Recovered Data - Data Auto-Reallocated" },
    154   1.1  thorpej { 0x18, 0x03, "Recovered Data With CIRC" },
    155   1.1  thorpej { 0x18, 0x04, "Recovered Data With LEC" },
    156   1.1  thorpej { 0x18, 0x05, "Recovered Data - Recommend Reassignment" },
    157   1.1  thorpej { 0x18, 0x06, "Recovered Data - Recommend Rewrite" },
    158   1.1  thorpej { 0x19, 0x00, "Defect List Error" },
    159   1.1  thorpej { 0x19, 0x01, "Defect List Not Available" },
    160   1.1  thorpej { 0x19, 0x02, "Defect List Error in Primary List" },
    161   1.1  thorpej { 0x19, 0x03, "Defect List Error in Grown List" },
    162   1.1  thorpej { 0x1A, 0x00, "Parameter List Length Error" },
    163   1.1  thorpej { 0x1B, 0x00, "Synchronous Data Transfer Error" },
    164   1.1  thorpej { 0x1C, 0x00, "Defect List Not Found" },
    165   1.1  thorpej { 0x1C, 0x01, "Primary Defect List Not Found" },
    166   1.1  thorpej { 0x1C, 0x02, "Grown Defect List Not Found" },
    167   1.1  thorpej { 0x1D, 0x00, "Miscompare During Verify Operation" },
    168   1.1  thorpej { 0x1E, 0x00, "Recovered ID with ECC" },
    169   1.1  thorpej { 0x20, 0x00, "Invalid Command Operation Code" },
    170   1.1  thorpej { 0x21, 0x00, "Logical Block Address Out of Range" },
    171   1.1  thorpej { 0x21, 0x01, "Invalid Element Address" },
    172   1.1  thorpej { 0x22, 0x00, "Illegal Function (Should 20 00, 24 00, or 26 00)" },
    173   1.1  thorpej { 0x24, 0x00, "Illegal Field in CDB" },
    174   1.1  thorpej { 0x25, 0x00, "Logical Unit Not Supported" },
    175   1.1  thorpej { 0x26, 0x00, "Invalid Field In Parameter List" },
    176   1.1  thorpej { 0x26, 0x01, "Parameter Not Supported" },
    177   1.1  thorpej { 0x26, 0x02, "Parameter Value Invalid" },
    178   1.1  thorpej { 0x26, 0x03, "Threshold Parameters Not Supported" },
    179   1.1  thorpej { 0x27, 0x00, "Write Protected" },
    180   1.1  thorpej { 0x28, 0x00, "Not Ready To Ready Transition (Medium May Have Changed)" },
    181   1.1  thorpej { 0x28, 0x01, "Import Or Export Element Accessed" },
    182   1.1  thorpej { 0x29, 0x00, "Power On, Reset, or Bus Device Reset Occurred" },
    183   1.1  thorpej { 0x2A, 0x00, "Parameters Changed" },
    184   1.1  thorpej { 0x2A, 0x01, "Mode Parameters Changed" },
    185   1.1  thorpej { 0x2A, 0x02, "Log Parameters Changed" },
    186   1.1  thorpej { 0x2B, 0x00, "Copy Cannot Execute Since Host Cannot Disconnect" },
    187   1.1  thorpej { 0x2C, 0x00, "Command Sequence Error" },
    188   1.1  thorpej { 0x2C, 0x01, "Too Many Windows Specified" },
    189   1.1  thorpej { 0x2C, 0x02, "Invalid Combination of Windows Specified" },
    190   1.1  thorpej { 0x2D, 0x00, "Overwrite Error On Update In Place" },
    191   1.1  thorpej { 0x2F, 0x00, "Commands Cleared By Another Initiator" },
    192   1.1  thorpej { 0x30, 0x00, "Incompatible Medium Installed" },
    193   1.1  thorpej { 0x30, 0x01, "Cannot Read Medium - Unknown Format" },
    194   1.1  thorpej { 0x30, 0x02, "Cannot Read Medium - Incompatible Format" },
    195   1.1  thorpej { 0x30, 0x03, "Cleaning Cartridge Installed" },
    196   1.1  thorpej { 0x31, 0x00, "Medium Format Corrupted" },
    197   1.1  thorpej { 0x31, 0x01, "Format Command Failed" },
    198   1.1  thorpej { 0x32, 0x00, "No Defect Spare Location Available" },
    199   1.1  thorpej { 0x32, 0x01, "Defect List Update Failure" },
    200   1.1  thorpej { 0x33, 0x00, "Tape Length Error" },
    201   1.1  thorpej { 0x36, 0x00, "Ribbon, Ink, or Toner Failure" },
    202   1.1  thorpej { 0x37, 0x00, "Rounded Parameter" },
    203   1.1  thorpej { 0x39, 0x00, "Saving Parameters Not Supported" },
    204   1.1  thorpej { 0x3A, 0x00, "Medium Not Present" },
    205   1.1  thorpej { 0x3B, 0x00, "Positioning Error" },
    206   1.1  thorpej { 0x3B, 0x01, "Tape Position Error At Beginning-of-Medium" },
    207   1.1  thorpej { 0x3B, 0x02, "Tape Position Error At End-of-Medium" },
    208   1.1  thorpej { 0x3B, 0x03, "Tape or Electronic Vertical Forms Unit Not Ready" },
    209   1.1  thorpej { 0x3B, 0x04, "Slew Failure" },
    210   1.1  thorpej { 0x3B, 0x05, "Paper Jam" },
    211   1.1  thorpej { 0x3B, 0x06, "Failed To Sense Top-Of-Form" },
    212   1.1  thorpej { 0x3B, 0x07, "Failed To Sense Bottom-Of-Form" },
    213   1.1  thorpej { 0x3B, 0x08, "Reposition Error" },
    214   1.1  thorpej { 0x3B, 0x09, "Read Past End Of Medium" },
    215  1.10   andvar { 0x3B, 0x0A, "Read Past Beginning Of Medium" },
    216   1.1  thorpej { 0x3B, 0x0B, "Position Past End Of Medium" },
    217   1.1  thorpej { 0x3B, 0x0C, "Position Past Beginning Of Medium" },
    218   1.1  thorpej { 0x3B, 0x0D, "Medium Destination Element Full" },
    219   1.1  thorpej { 0x3B, 0x0E, "Medium Source Element Empty" },
    220   1.2    jwise { 0x3D, 0x00, "Invalid Bits In IDENTIFY Message" },
    221   1.1  thorpej { 0x3E, 0x00, "Logical Unit Has Not Self-Configured Yet" },
    222   1.1  thorpej { 0x3F, 0x00, "Target Operating Conditions Have Changed" },
    223   1.1  thorpej { 0x3F, 0x01, "Microcode Has Changed" },
    224   1.1  thorpej { 0x3F, 0x02, "Changed Operating Definition" },
    225   1.1  thorpej { 0x3F, 0x03, "INQUIRY Data Has Changed" },
    226   1.1  thorpej { 0x40, 0x00, "RAM FAILURE (Should Use 40 NN)" },
    227   1.1  thorpej { 0x41, 0x00, "Data Path FAILURE (Should Use 40 NN)" },
    228   1.1  thorpej { 0x42, 0x00, "Power-On or Self-Test FAILURE (Should Use 40 NN)" },
    229   1.1  thorpej { 0x43, 0x00, "Message Error" },
    230   1.1  thorpej { 0x44, 0x00, "Internal Target Failure" },
    231   1.1  thorpej { 0x45, 0x00, "Select Or Reselect Failure" },
    232   1.1  thorpej { 0x46, 0x00, "Unsuccessful Soft Reset" },
    233   1.1  thorpej { 0x47, 0x00, "SCSI Parity Error" },
    234   1.1  thorpej { 0x48, 0x00, "INITIATOR DETECTED ERROR Message Received" },
    235   1.1  thorpej { 0x49, 0x00, "Invalid Message Error" },
    236   1.1  thorpej { 0x4A, 0x00, "Command Phase Error" },
    237   1.1  thorpej { 0x4B, 0x00, "Data Phase Error" },
    238   1.1  thorpej { 0x4C, 0x00, "Logical Unit Failed Self-Configuration" },
    239   1.1  thorpej { 0x4E, 0x00, "Overlapped Commands Attempted" },
    240   1.1  thorpej { 0x50, 0x00, "Write Append Error" },
    241   1.1  thorpej { 0x50, 0x01, "Write Append Position Error" },
    242   1.1  thorpej { 0x50, 0x01, "Write Append Position Error" },
    243   1.1  thorpej { 0x50, 0x02, "Position Error Related To Timing" },
    244   1.1  thorpej { 0x51, 0x00, "Erase Failure" },
    245   1.1  thorpej { 0x52, 0x00, "Cartridge Fault" },
    246   1.1  thorpej { 0x53, 0x00, "Media Load or Eject Failed" },
    247   1.1  thorpej { 0x53, 0x01, "Unload Tape Failure" },
    248   1.1  thorpej { 0x53, 0x02, "Medium Removal Prevented" },
    249   1.1  thorpej { 0x54, 0x00, "SCSI To Host System Interface Failure" },
    250   1.1  thorpej { 0x55, 0x00, "System Resource Failure" },
    251   1.1  thorpej { 0x57, 0x00, "Unable To Recover Table-Of-Contents" },
    252   1.1  thorpej { 0x58, 0x00, "Generation Does Not Exist" },
    253   1.1  thorpej { 0x59, 0x00, "Updated Block Read" },
    254   1.1  thorpej { 0x5A, 0x00, "Operator Request or State Change Input (Unspecified)" },
    255   1.1  thorpej { 0x5A, 0x01, "Operator Medium Removal Requested" },
    256   1.1  thorpej { 0x5A, 0x02, "Operator Selected Write Protect" },
    257   1.1  thorpej { 0x5A, 0x03, "Operator Selected Write Permit" },
    258   1.1  thorpej { 0x5B, 0x00, "Log Exception" },
    259   1.1  thorpej { 0x5B, 0x01, "Threshold Condition Met" },
    260   1.1  thorpej { 0x5B, 0x02, "Log Counter At Maximum" },
    261   1.1  thorpej { 0x5B, 0x03, "Log List Codes Exhausted" },
    262   1.1  thorpej { 0x5C, 0x00, "RPL Status Change" },
    263   1.1  thorpej { 0x5C, 0x01, "Spindles Synchronized" },
    264   1.1  thorpej { 0x5C, 0x02, "Spindles Not Synchronized" },
    265   1.1  thorpej { 0x60, 0x00, "Lamp Failure" },
    266   1.1  thorpej { 0x61, 0x00, "Video Acquisition Error" },
    267   1.1  thorpej { 0x61, 0x01, "Unable To Acquire Video" },
    268   1.1  thorpej { 0x61, 0x02, "Out Of Focus" },
    269   1.1  thorpej { 0x62, 0x00, "Scan Head Positioning Error" },
    270   1.1  thorpej { 0x63, 0x00, "End Of User Area Encountered On This Track" },
    271   1.1  thorpej { 0x64, 0x00, "Illegal Mode For This Track" },
    272   1.1  thorpej { 0x00, 0x00, NULL }
    273   1.1  thorpej };
    274   1.1  thorpej 
    275   1.6  xtraeme static void asc2ascii(unsigned char, unsigned char, char *, size_t);
    276   1.1  thorpej 
    277   1.1  thorpej static void
    278   1.6  xtraeme asc2ascii(unsigned char asc, unsigned char ascq, char *result, size_t reslen)
    279   1.1  thorpej {
    280   1.1  thorpej 	int i = 0;
    281   1.1  thorpej 
    282   1.1  thorpej 	while (adesc[i].description != NULL) {
    283   1.1  thorpej 		if (adesc[i].asc == asc && adesc[i].ascq == ascq)
    284   1.1  thorpej 			break;
    285   1.1  thorpej 		i++;
    286   1.1  thorpej 	}
    287   1.1  thorpej 	if (adesc[i].description == NULL) {
    288   1.1  thorpej 		if (asc == 0x40 && ascq != 0)
    289   1.1  thorpej 			(void) snprintf(result, reslen,
    290   1.1  thorpej 			    "Diagnostic Failure on Component 0x%02x",
    291   1.1  thorpej 			    ascq & 0xff);
    292   1.1  thorpej 		else
    293   1.1  thorpej 			(void) snprintf(result, reslen,
    294   1.1  thorpej 			    "ASC 0x%02x ASCQ 0x%02x",
    295   1.1  thorpej 			    asc & 0xff, ascq & 0xff);
    296   1.4   itojun 	} else
    297   1.4   itojun 		(void) strlcpy(result, adesc[i].description, reslen);
    298   1.1  thorpej }
    299   1.1  thorpej 
    300   1.1  thorpej void
    301   1.6  xtraeme scsi_print_sense_data(const unsigned char *s, int slen, int verbosity)
    302   1.1  thorpej {
    303   1.1  thorpej 	int32_t info;
    304   1.1  thorpej 	int i, j, k;
    305   1.1  thorpej 	char sbs[132], *cp;
    306   1.1  thorpej 
    307   1.1  thorpej 	/*
    308   1.1  thorpej 	 * Basics - print out SENSE KEY
    309   1.1  thorpej 	 */
    310   1.1  thorpej 	printf("    SENSE KEY: %s", scsi_decode_sense(s, 0, sbs, sizeof(sbs)));
    311   1.1  thorpej 
    312   1.1  thorpej 	/*
    313   1.1  thorpej 	 * Print out, unqualified but aligned, FMK, EOM and ILI status.
    314   1.1  thorpej 	 */
    315   1.1  thorpej 	if (s[2] & 0xe0) {
    316   1.1  thorpej 		char pad;
    317   1.1  thorpej 	        printf("\n              ");
    318   1.1  thorpej 		pad = ' ';
    319   1.1  thorpej 		if (s[2] & SSD_FILEMARK) {
    320   1.1  thorpej 			printf("%c Filemark Detected", pad);
    321   1.1  thorpej 			pad = ',';
    322   1.1  thorpej 		}
    323   1.1  thorpej 		if (s[2] & SSD_EOM) {
    324   1.1  thorpej 			printf("%c EOM Detected", pad);
    325   1.1  thorpej 			pad = ',';
    326   1.1  thorpej 		}
    327   1.1  thorpej 		if (s[2] & SSD_ILI)
    328   1.1  thorpej 			printf("%c Incorrect Length Indicator Set", pad);
    329   1.1  thorpej 	}
    330   1.1  thorpej 
    331   1.1  thorpej 	/*
    332   1.1  thorpej 	 * Now we should figure out, based upon device type, how
    333   1.1  thorpej 	 * to format the information field. Unfortunately, that's
    334   1.1  thorpej 	 * not convenient here, so we'll print it as a signed
    335   1.1  thorpej 	 * 32 bit integer.
    336   1.1  thorpej 	 */
    337   1.1  thorpej 	info = _4btol(&s[3]);
    338   1.1  thorpej 	if (info)
    339   1.3  thorpej 		printf("\n   INFO FIELD: %d", info);
    340   1.1  thorpej 
    341   1.1  thorpej 	/*
    342   1.1  thorpej 	 * Now we check additional length to see whether there is
    343   1.1  thorpej 	 * more information to extract.
    344   1.1  thorpej 	 */
    345   1.1  thorpej 
    346   1.1  thorpej 	/* enough for command specific information? */
    347   1.1  thorpej 	if (((unsigned int) s[7]) < 4) {
    348   1.1  thorpej 		printf("\n");
    349   1.1  thorpej 		return;
    350   1.1  thorpej 	}
    351   1.1  thorpej 	info = _4btol(&s[8]);
    352   1.1  thorpej 	if (info)
    353   1.3  thorpej 		printf("\n COMMAND INFO: %d (0x%x)", info, info);
    354   1.1  thorpej 
    355   1.1  thorpej 	/*
    356   1.1  thorpej 	 * Decode ASC && ASCQ info, plus FRU, plus the rest...
    357   1.1  thorpej 	 */
    358   1.1  thorpej 
    359   1.1  thorpej 	cp = scsi_decode_sense(s, 1, sbs, sizeof(sbs));
    360   1.1  thorpej 	if (cp)
    361   1.3  thorpej 		printf("\n     ASC/ASCQ: %s", cp);
    362   1.1  thorpej 	if (s[14] != 0)
    363   1.3  thorpej 		printf("\n     FRU CODE: 0x%x", s[14] & 0xff);
    364   1.1  thorpej 	cp = scsi_decode_sense(s, 3, sbs, sizeof(sbs));
    365   1.1  thorpej 	if (cp)
    366   1.3  thorpej 		printf("\n         SKSV: %s", cp);
    367   1.1  thorpej 	printf("\n");
    368   1.1  thorpej 	if (verbosity == 0) {
    369   1.1  thorpej 		printf("\n");
    370   1.1  thorpej 		return;
    371   1.1  thorpej 	}
    372   1.1  thorpej 
    373   1.1  thorpej 	/*
    374  1.11   andvar 	 * Now figure whether we should print any additional information.
    375   1.1  thorpej 	 *
    376   1.1  thorpej 	 * Where should we start from? If we had SKSV data,
    377   1.1  thorpej 	 * start from offset 18, else from offset 15.
    378   1.1  thorpej 	 *
    379   1.1  thorpej 	 * From that point until the end of the buffer, check for any
    380   1.1  thorpej 	 * nonzero data. If we have some, go back and print the lot,
    381   1.1  thorpej 	 * otherwise we're done.
    382   1.1  thorpej 	 */
    383   1.9      mrg 	if (cp)
    384   1.1  thorpej 		i = 18;
    385   1.1  thorpej 	else
    386   1.1  thorpej 		i = 15;
    387   1.1  thorpej 	for (j = i; j < slen; j++)
    388   1.1  thorpej 		if (s[j])
    389   1.1  thorpej 			break;
    390   1.1  thorpej 	if (j == slen)
    391   1.1  thorpej 		return;
    392   1.1  thorpej 
    393   1.1  thorpej 	printf("\n Additional Sense Information (byte %d out...):\n", i);
    394   1.1  thorpej 	if (i == 15) {
    395   1.1  thorpej 		printf("\n\t%2d:", i);
    396   1.1  thorpej 		k = 7;
    397   1.1  thorpej 	} else {
    398   1.1  thorpej 		printf("\n\t%2d:", i);
    399   1.1  thorpej 		k = 2;
    400   1.1  thorpej 		j -= 2;
    401   1.1  thorpej 	}
    402   1.1  thorpej 	while (j > 0) {
    403   1.1  thorpej 		if (i >= slen)
    404   1.1  thorpej 			break;
    405   1.1  thorpej 		if (k == 8) {
    406   1.1  thorpej 			k = 0;
    407   1.1  thorpej 			printf("\n\t%2d:", i);
    408   1.1  thorpej 		}
    409   1.1  thorpej 		printf(" 0x%02x", s[i] & 0xff);
    410   1.1  thorpej 		k++;
    411   1.1  thorpej 		j--;
    412   1.1  thorpej 		i++;
    413   1.1  thorpej 	}
    414   1.1  thorpej 	printf("\n\n");
    415   1.1  thorpej }
    416   1.1  thorpej 
    417   1.1  thorpej char *
    418   1.6  xtraeme scsi_decode_sense(const unsigned char *snsbuf, int flag, char *rqsbuf,
    419   1.6  xtraeme 	size_t rqsbuflen)
    420   1.1  thorpej {
    421   1.1  thorpej 	unsigned char skey;
    422   1.1  thorpej 	char localbuf[64];
    423   1.1  thorpej 
    424   1.1  thorpej 	skey = 0;
    425   1.1  thorpej 
    426   1.1  thorpej 	if (flag == 0 || flag == 2 || flag == 3)
    427   1.1  thorpej 		skey = snsbuf[2] & 0xf;
    428   1.1  thorpej 	if (flag == 0) {			/* Sense Key Only */
    429   1.4   itojun 		(void) strlcpy(rqsbuf, sense_keys[skey], rqsbuflen);
    430   1.1  thorpej 		return (rqsbuf);
    431   1.1  thorpej 	} else if (flag == 1) {			/* ASC/ASCQ Only */
    432   1.1  thorpej 		asc2ascii(snsbuf[12], snsbuf[13], rqsbuf, rqsbuflen);
    433   1.1  thorpej 		return (rqsbuf);
    434   1.1  thorpej 	} else  if (flag == 2) {		/* Sense Key && ASC/ASCQ */
    435   1.1  thorpej 		asc2ascii(snsbuf[12], snsbuf[13], localbuf, sizeof(localbuf));
    436   1.1  thorpej 		(void) snprintf(rqsbuf, rqsbuflen, "%s, %s", sense_keys[skey],
    437   1.1  thorpej 		    localbuf);
    438   1.1  thorpej 		return (rqsbuf);
    439   1.1  thorpej 	} else if (flag == 3 && snsbuf[7] >= 9 && (snsbuf[15] & 0x80)) {
    440   1.1  thorpej 		/*
    441   1.1  thorpej 		 * SKSV Data
    442   1.1  thorpej 		 */
    443   1.1  thorpej 		switch (skey) {
    444   1.1  thorpej 		case SKEY_ILLEGAL_REQUEST:
    445   1.1  thorpej 			if (snsbuf[15] & 0x8)
    446   1.1  thorpej 				(void) snprintf(rqsbuf, rqsbuflen,
    447   1.1  thorpej 				    "Error in %s, Offset %d, bit %d",
    448   1.1  thorpej 				    (snsbuf[15] & 0x40)? "CDB" : "Parameters",
    449   1.1  thorpej 				    (snsbuf[16] & 0xff) << 8 |
    450   1.1  thorpej 				    (snsbuf[17] & 0xff), snsbuf[15] & 0x7);
    451   1.1  thorpej 			else
    452   1.1  thorpej 				(void) snprintf(rqsbuf, rqsbuflen,
    453   1.1  thorpej 				    "Error in %s, Offset %d",
    454   1.1  thorpej 				    (snsbuf[15] & 0x40)? "CDB" : "Parameters",
    455   1.1  thorpej 				    (snsbuf[16] & 0xff) << 8 |
    456   1.1  thorpej 				    (snsbuf[17] & 0xff));
    457   1.1  thorpej 			return (rqsbuf);
    458   1.1  thorpej 
    459   1.1  thorpej 		case SKEY_RECOVERED_ERROR:
    460   1.1  thorpej 		case SKEY_MEDIUM_ERROR:
    461   1.1  thorpej 		case SKEY_HARDWARE_ERROR:
    462   1.1  thorpej 			(void) snprintf(rqsbuf, rqsbuflen,
    463   1.1  thorpej 			    "Actual Retry Count: %d",
    464   1.1  thorpej 			    (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
    465   1.1  thorpej 			return (rqsbuf);
    466   1.1  thorpej 
    467   1.1  thorpej 		case SKEY_NOT_READY:
    468   1.1  thorpej 			(void) snprintf(rqsbuf, rqsbuflen,
    469   1.1  thorpej 			    "Progress Indicator: %d",
    470   1.1  thorpej 			    (snsbuf[16] & 0xff) << 8 | (snsbuf[17] & 0xff));
    471   1.1  thorpej 			return (rqsbuf);
    472   1.1  thorpej 
    473   1.1  thorpej 		default:
    474   1.1  thorpej 			break;
    475   1.1  thorpej 		}
    476   1.1  thorpej 	}
    477   1.1  thorpej 	return (NULL);
    478   1.1  thorpej }
    479   1.1  thorpej 
    480   1.1  thorpej void
    481   1.6  xtraeme scsi_print_sense(const char *name, const scsireq_t *req, int verbosity)
    482   1.1  thorpej {
    483   1.1  thorpej 	int i;
    484   1.1  thorpej 
    485   1.1  thorpej  	printf("%s: Check Condition on CDB:", name);
    486   1.1  thorpej  	for (i = 0; i < req->cmdlen; i++)
    487   1.1  thorpej  		printf(" %02x", req->cmd[i]);
    488   1.1  thorpej  	printf("\n");
    489   1.1  thorpej 	scsi_print_sense_data(req->sense, req->senselen_used, verbosity);
    490   1.1  thorpej }
    491