add a config file for git-buildpackage
[debian/mtx] / sg_err.c
1
2 /* This file is a huge cut, paste and hack from linux/drivers/scsi/constant.c
3 *  which I guess was written by:
4 *         Copyright (C) 1993, 1994, 1995 Eric Youngdale
5
6 * The rest of this is:
7 *  Copyright (C) 1999 - 2001 D. Gilbert
8 *
9 *  This program is free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2, or (at your option)
12 *  any later version.
13 *
14 *  ASCII values for a number of symbolic constants, printing functions, etc.
15 *
16 *  Some of the tables have been updated for SCSI 2.
17 *
18 *  Version 0.84 (20010115)
19 *      Change output from stdout to stderr
20 */
21
22 #define OUTP stderr
23
24 static const unsigned char scsi_command_size[8] = { 6, 10, 10, 12,
25                                                    12, 12, 10, 10 };
26
27 #define COMMAND_SIZE(opcode) scsi_command_size[((opcode) >> 5) & 7]
28
29 static const char unknown[] = "UNKNOWN";
30
31 static const char * group_0_commands[] = {
32 /* 00-03 */ "Test Unit Ready", "Rezero Unit", unknown, "Request Sense",
33 /* 04-07 */ "Format Unit", "Read Block Limits", unknown, "Reasssign Blocks",
34 /* 08-0d */ "Read (6)", unknown, "Write (6)", "Seek (6)", unknown, unknown,
35 /* 0e-12 */ unknown, "Read Reverse", "Write Filemarks", "Space", "Inquiry",
36 /* 13-16 */ "Verify", "Recover Buffered Data", "Mode Select", "Reserve",
37 /* 17-1b */ "Release", "Copy", "Erase", "Mode Sense", "Start/Stop Unit",
38 /* 1c-1d */ "Receive Diagnostic", "Send Diagnostic",
39 /* 1e-1f */ "Prevent/Allow Medium Removal", unknown,
40 };
41
42
43 static const char *group_1_commands[] = {
44 /* 20-22 */  unknown, unknown, unknown,
45 /* 23-28 */ unknown, "Define window parameters", "Read Capacity",
46             unknown, unknown, "Read (10)",
47 /* 29-2d */ "Read Generation", "Write (10)", "Seek (10)", "Erase",
48             "Read updated block",
49 /* 2e-31 */ "Write Verify","Verify", "Search High", "Search Equal",
50 /* 32-34 */ "Search Low", "Set Limits", "Prefetch or Read Position",
51 /* 35-37 */ "Synchronize Cache","Lock/Unlock Cache", "Read Defect Data",
52 /* 38-3c */ "Medium Scan", "Compare", "Copy Verify", "Write Buffer",
53             "Read Buffer",
54 /* 3d-3f */ "Update Block", "Read Long",  "Write Long",
55 };
56
57 static const char *group_2_commands[] = {
58 /* 40-41 */ "Change Definition", "Write Same",
59 /* 42-48 */ "Read sub-channel", "Read TOC", "Read header",
60             "Play audio (10)", unknown, "Play audio msf",
61             "Play audio track/index",
62 /* 49-4f */ "Play track relative (10)", unknown, "Pause/resume",
63             "Log Select", "Log Sense", unknown, unknown,
64 /* 50-55 */ unknown, unknown, unknown, unknown, unknown, "Mode Select (10)",
65 /* 56-5b */ unknown, unknown, unknown, unknown, "Mode Sense (10)", unknown,
66 /* 5c-5f */ unknown, unknown, unknown,
67 };
68
69
70 /* The following are 12 byte commands in group 5 */
71 static const char *group_5_commands[] = {
72 /* a0-a5 */ unknown, unknown, unknown, unknown, unknown,
73             "Move medium/play audio(12)",
74 /* a6-a9 */ "Exchange medium", unknown, "Read(12)", "Play track relative(12)",
75 /* aa-ae */ "Write(12)", unknown, "Erase(12)", unknown,
76             "Write and verify(12)",
77 /* af-b1 */ "Verify(12)", "Search data high(12)", "Search data equal(12)",
78 /* b2-b4 */ "Search data low(12)", "Set limits(12)", unknown,
79 /* b5-b6 */ "Request volume element address", "Send volume tag",
80 /* b7-b9 */ "Read defect data(12)", "Read element status", unknown,
81 /* ba-bf */ unknown, unknown, unknown, unknown, unknown, unknown,
82 };
83
84
85
86
87 #define group(opcode) (((opcode) >> 5) & 7)
88
89 #define RESERVED_GROUP  0
90 #define VENDOR_GROUP    1
91
92 static const char **commands[] = {
93     group_0_commands, group_1_commands, group_2_commands,
94     (const char **) RESERVED_GROUP, (const char **) RESERVED_GROUP,
95     group_5_commands, (const char **) VENDOR_GROUP,
96     (const char **) VENDOR_GROUP
97 };
98
99 static const char reserved[] = "RESERVED";
100 static const char vendor[] = "VENDOR SPECIFIC";
101
102 static void print_opcode(int opcode) {
103     const char **table = commands[ group(opcode) ];
104     switch ((unsigned long) table) {
105     case RESERVED_GROUP:
106         fprintf(OUTP, "%s(0x%02x) ", reserved, opcode);
107         break;
108     case VENDOR_GROUP:
109         fprintf(OUTP, "%s(0x%02x) ", vendor, opcode);
110         break;
111     default:
112         if (table[opcode & 0x1f] != unknown)
113             fprintf(OUTP, "%s ",table[opcode & 0x1f]);
114         else
115             fprintf(OUTP, "%s(0x%02x) ", unknown, opcode);
116         break;
117     }
118 }
119
120 void sg_print_command (const unsigned char * command) {
121     int i,s;
122     print_opcode(command[0]);
123     for ( i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
124         fprintf(OUTP, "%02x ", command[i]);
125     fprintf(OUTP, "\n");
126 }
127
128 static const char * statuses[] = {
129 /* 0-4 */ "Good", "Check Condition", "Condition Met", unknown, "Busy",
130 /* 5-9 */ unknown, unknown, unknown, "Intermediate", unknown,
131 /* a-c */ "Intermediate-Condition Met", unknown, "Reservation Conflict",
132 /* d-10 */ unknown, unknown, unknown, unknown,
133 /* 11-14 */ "Command Terminated", unknown, unknown, "Queue Full",
134 /* 15-1a */ unknown, unknown, unknown,  unknown, unknown, unknown,
135 /* 1b-1f */ unknown, unknown, unknown,  unknown, unknown,
136 };
137
138 void sg_print_status (int masked_status) {
139     /* status = (status >> 1) & 0xf; */ /* already done */
140     fprintf(OUTP, "%s ",statuses[masked_status]);
141 }
142
143 #define D 0x001  /* DIRECT ACCESS DEVICE (disk) */
144 #define T 0x002  /* SEQUENTIAL ACCESS DEVICE (tape) */
145 #define L 0x004  /* PRINTER DEVICE */
146 #define P 0x008  /* PROCESSOR DEVICE */
147 #define W 0x010  /* WRITE ONCE READ MULTIPLE DEVICE */
148 #define R 0x020  /* READ ONLY (CD-ROM) DEVICE */
149 #define S 0x040  /* SCANNER DEVICE */
150 #define O 0x080  /* OPTICAL MEMORY DEVICE */
151 #define M 0x100  /* MEDIA CHANGER DEVICE */
152 #define C 0x200  /* COMMUNICATION DEVICE */
153
154 struct error_info{
155     unsigned char code1, code2;
156     unsigned short int devices;
157     const char * text;
158 };
159
160 struct error_info2{
161     unsigned char code1, code2_min, code2_max;
162     unsigned short int devices;
163     const char * text;
164 };
165
166 static struct error_info2 additional2[] =
167 {
168   {0x40,0x00,0x7f,D,"Ram failure (%x)"},
169   {0x40,0x80,0xff,D|T|L|P|W|R|S|O|M|C,"Diagnostic failure on component (%x)"},
170   {0x41,0x00,0xff,D,"Data path failure (%x)"},
171   {0x42,0x00,0xff,D,"Power-on or self-test failure (%x)"},
172   {0, 0, 0, 0, NULL}
173 };
174
175 static struct error_info additional[] =
176 {
177   {0x00,0x01,T,"Filemark detected"},
178   {0x00,0x02,T|S,"End-of-partition/medium detected"},
179   {0x00,0x03,T,"Setmark detected"},
180   {0x00,0x04,T|S,"Beginning-of-partition/medium detected"},
181   {0x00,0x05,T|S,"End-of-data detected"},
182   {0x00,0x06,D|T|L|P|W|R|S|O|M|C,"I/O process terminated"},
183   {0x00,0x11,R,"Audio play operation in progress"},
184   {0x00,0x12,R,"Audio play operation paused"},
185   {0x00,0x13,R,"Audio play operation successfully completed"},
186   {0x00,0x14,R,"Audio play operation stopped due to error"},
187   {0x00,0x15,R,"No current audio status to return"},
188   {0x01,0x00,D|W|O,"No index/sector signal"},
189   {0x02,0x00,D|W|R|O|M,"No seek complete"},
190   {0x03,0x00,D|T|L|W|S|O,"Peripheral device write fault"},
191   {0x03,0x01,T,"No write current"},
192   {0x03,0x02,T,"Excessive write errors"},
193   {0x04,0x00,D|T|L|P|W|R|S|O|M|C,
194      "Logical unit not ready, cause not reportable"},
195   {0x04,0x01,D|T|L|P|W|R|S|O|M|C,
196      "Logical unit is in process of becoming ready"},
197   {0x04,0x02,D|T|L|P|W|R|S|O|M|C,
198      "Logical unit not ready, initializing command required"},
199   {0x04,0x03,D|T|L|P|W|R|S|O|M|C,
200      "Logical unit not ready, manual intervention required"},
201   {0x04,0x04,D|T|L|O,"Logical unit not ready, format in progress"},
202   {0x05,0x00,D|T|L|W|R|S|O|M|C,"Logical unit does not respond to selection"},
203   {0x06,0x00,D|W|R|O|M,"No reference position found"},
204   {0x07,0x00,D|T|L|W|R|S|O|M,"Multiple peripheral devices selected"},
205   {0x08,0x00,D|T|L|W|R|S|O|M|C,"Logical unit communication failure"},
206   {0x08,0x01,D|T|L|W|R|S|O|M|C,"Logical unit communication time-out"},
207   {0x08,0x02,D|T|L|W|R|S|O|M|C,"Logical unit communication parity error"},
208   {0x09,0x00,D|T|W|R|O,"Track following error"},
209   {0x09,0x01,W|R|O,"Tracking servo failure"},
210   {0x09,0x02,W|R|O,"Focus servo failure"},
211   {0x09,0x03,W|R|O,"Spindle servo failure"},
212   {0x0A,0x00,D|T|L|P|W|R|S|O|M|C,"Error log overflow"},
213   {0x0C,0x00,T|S,"Write error"},
214   {0x0C,0x01,D|W|O,"Write error recovered with auto reallocation"},
215   {0x0C,0x02,D|W|O,"Write error - auto reallocation failed"},
216   {0x10,0x00,D|W|O,"Id crc or ecc error"},
217   {0x11,0x00,D|T|W|R|S|O,"Unrecovered read error"},
218   {0x11,0x01,D|T|W|S|O,"Read retries exhausted"},
219   {0x11,0x02,D|T|W|S|O,"Error too long to correct"},
220   {0x11,0x03,D|T|W|S|O,"Multiple read errors"},
221   {0x11,0x04,D|W|O,"Unrecovered read error - auto reallocate failed"},
222   {0x11,0x05,W|R|O,"L-ec uncorrectable error"},
223   {0x11,0x06,W|R|O,"Circ unrecovered error"},
224   {0x11,0x07,W|O,"Data resynchronization error"},
225   {0x11,0x08,T,"Incomplete block read"},
226   {0x11,0x09,T,"No gap found"},
227   {0x11,0x0A,D|T|O,"Miscorrected error"},
228   {0x11,0x0B,D|W|O,"Unrecovered read error - recommend reassignment"},
229   {0x11,0x0C,D|W|O,"Unrecovered read error - recommend rewrite the data"},
230   {0x12,0x00,D|W|O,"Address mark not found for id field"},
231   {0x13,0x00,D|W|O,"Address mark not found for data field"},
232   {0x14,0x00,D|T|L|W|R|S|O,"Recorded entity not found"},
233   {0x14,0x01,D|T|W|R|O,"Record not found"},
234   {0x14,0x02,T,"Filemark or setmark not found"},
235   {0x14,0x03,T,"End-of-data not found"},
236   {0x14,0x04,T,"Block sequence error"},
237   {0x15,0x00,D|T|L|W|R|S|O|M,"Random positioning error"},
238   {0x15,0x01,D|T|L|W|R|S|O|M,"Mechanical positioning error"},
239   {0x15,0x02,D|T|W|R|O,"Positioning error detected by read of medium"},
240   {0x16,0x00,D|W|O,"Data synchronization mark error"},
241   {0x17,0x00,D|T|W|R|S|O,"Recovered data with no error correction applied"},
242   {0x17,0x01,D|T|W|R|S|O,"Recovered data with retries"},
243   {0x17,0x02,D|T|W|R|O,"Recovered data with positive head offset"},
244   {0x17,0x03,D|T|W|R|O,"Recovered data with negative head offset"},
245   {0x17,0x04,W|R|O,"Recovered data with retries and/or circ applied"},
246   {0x17,0x05,D|W|R|O,"Recovered data using previous sector id"},
247   {0x17,0x06,D|W|O,"Recovered data without ecc - data auto-reallocated"},
248   {0x17,0x07,D|W|O,"Recovered data without ecc - recommend reassignment"},
249   {0x18,0x00,D|T|W|R|O,"Recovered data with error correction applied"},
250   {0x18,0x01,D|W|R|O,"Recovered data with error correction and retries applied"},
251   {0x18,0x02,D|W|R|O,"Recovered data - data auto-reallocated"},
252   {0x18,0x03,R,"Recovered data with circ"},
253   {0x18,0x04,R,"Recovered data with lec"},
254   {0x18,0x05,D|W|R|O,"Recovered data - recommend reassignment"},
255   {0x19,0x00,D|O,"Defect list error"},
256   {0x19,0x01,D|O,"Defect list not available"},
257   {0x19,0x02,D|O,"Defect list error in primary list"},
258   {0x19,0x03,D|O,"Defect list error in grown list"},
259   {0x1A,0x00,D|T|L|P|W|R|S|O|M|C,"Parameter list length error"},
260   {0x1B,0x00,D|T|L|P|W|R|S|O|M|C,"Synchronous data transfer error"},
261   {0x1C,0x00,D|O,"Defect list not found"},
262   {0x1C,0x01,D|O,"Primary defect list not found"},
263   {0x1C,0x02,D|O,"Grown defect list not found"},
264   {0x1D,0x00,D|W|O,"Miscompare during verify operation"},
265   {0x1E,0x00,D|W|O,"Recovered id with ecc correction"},
266   {0x20,0x00,D|T|L|P|W|R|S|O|M|C,"Invalid command operation code"},
267   {0x21,0x00,D|T|W|R|O|M,"Logical block address out of range"},
268   {0x21,0x01,M,"Invalid element address"},
269   {0x22,0x00,D,"Illegal function (should use 20 00, 24 00, or 26 00)"},
270   {0x24,0x00,D|T|L|P|W|R|S|O|M|C,"Invalid field in cdb"},
271   {0x25,0x00,D|T|L|P|W|R|S|O|M|C,"Logical unit not supported"},
272   {0x26,0x00,D|T|L|P|W|R|S|O|M|C,"Invalid field in parameter list"},
273   {0x26,0x01,D|T|L|P|W|R|S|O|M|C,"Parameter not supported"},
274   {0x26,0x02,D|T|L|P|W|R|S|O|M|C,"Parameter value invalid"},
275   {0x26,0x03,D|T|L|P|W|R|S|O|M|C,"Threshold parameters not supported"},
276   {0x27,0x00,D|T|W|O,"Write protected"},
277   {0x28,0x00,D|T|L|P|W|R|S|O|M|C,"Not ready to ready transition (medium may have changed)"},
278   {0x28,0x01,M,"Import or export element accessed"},
279   {0x29,0x00,D|T|L|P|W|R|S|O|M|C,"Power on, reset, or bus device reset occurred"},
280   {0x2A,0x00,D|T|L|W|R|S|O|M|C,"Parameters changed"},
281   {0x2A,0x01,D|T|L|W|R|S|O|M|C,"Mode parameters changed"},
282   {0x2A,0x02,D|T|L|W|R|S|O|M|C,"Log parameters changed"},
283   {0x2B,0x00,D|T|L|P|W|R|S|O|C,"Copy cannot execute since host cannot disconnect"},
284   {0x2C,0x00,D|T|L|P|W|R|S|O|M|C,"Command sequence error"},
285   {0x2C,0x01,S,"Too many windows specified"},
286   {0x2C,0x02,S,"Invalid combination of windows specified"},
287   {0x2D,0x00,T,"Overwrite error on update in place"},
288   {0x2F,0x00,D|T|L|P|W|R|S|O|M|C,"Commands cleared by another initiator"},
289   {0x30,0x00,D|T|W|R|O|M,"Incompatible medium installed"},
290   {0x30,0x01,D|T|W|R|O,"Cannot read medium - unknown format"},
291   {0x30,0x02,D|T|W|R|O,"Cannot read medium - incompatible format"},
292   {0x30,0x03,D|T,"Cleaning cartridge installed"},
293   {0x31,0x00,D|T|W|O,"Medium format corrupted"},
294   {0x31,0x01,D|L|O,"Format command failed"},
295   {0x32,0x00,D|W|O,"No defect spare location available"},
296   {0x32,0x01,D|W|O,"Defect list update failure"},
297   {0x33,0x00,T,"Tape length error"},
298   {0x36,0x00,L,"Ribbon, ink, or toner failure"},
299   {0x37,0x00,D|T|L|W|R|S|O|M|C,"Rounded parameter"},
300   {0x39,0x00,D|T|L|W|R|S|O|M|C,"Saving parameters not supported"},
301   {0x3A,0x00,D|T|L|W|R|S|O|M,"Medium not present"},
302   {0x3B,0x00,T|L,"Sequential positioning error"},
303   {0x3B,0x01,T,"Tape position error at beginning-of-medium"},
304   {0x3B,0x02,T,"Tape position error at end-of-medium"},
305   {0x3B,0x03,L,"Tape or electronic vertical forms unit not ready"},
306   {0x3B,0x04,L,"Slew failure"},
307   {0x3B,0x05,L,"Paper jam"},
308   {0x3B,0x06,L,"Failed to sense top-of-form"},
309   {0x3B,0x07,L,"Failed to sense bottom-of-form"},
310   {0x3B,0x08,T,"Reposition error"},
311   {0x3B,0x09,S,"Read past end of medium"},
312   {0x3B,0x0A,S,"Read past beginning of medium"},
313   {0x3B,0x0B,S,"Position past end of medium"},
314   {0x3B,0x0C,S,"Position past beginning of medium"},
315   {0x3B,0x0D,M,"Medium destination element full"},
316   {0x3B,0x0E,M,"Medium source element empty"},
317   {0x3D,0x00,D|T|L|P|W|R|S|O|M|C,"Invalid bits in identify message"},
318   {0x3E,0x00,D|T|L|P|W|R|S|O|M|C,"Logical unit has not self-configured yet"},
319   {0x3F,0x00,D|T|L|P|W|R|S|O|M|C,"Target operating conditions have changed"},
320   {0x3F,0x01,D|T|L|P|W|R|S|O|M|C,"Microcode has been changed"},
321   {0x3F,0x02,D|T|L|P|W|R|S|O|M|C,"Changed operating definition"},
322   {0x3F,0x03,D|T|L|P|W|R|S|O|M|C,"Inquiry data has changed"},
323   {0x43,0x00,D|T|L|P|W|R|S|O|M|C,"Message error"},
324   {0x44,0x00,D|T|L|P|W|R|S|O|M|C,"Internal target failure"},
325   {0x45,0x00,D|T|L|P|W|R|S|O|M|C,"Select or reselect failure"},
326   {0x46,0x00,D|T|L|P|W|R|S|O|M|C,"Unsuccessful soft reset"},
327   {0x47,0x00,D|T|L|P|W|R|S|O|M|C,"Scsi parity error"},
328   {0x48,0x00,D|T|L|P|W|R|S|O|M|C,"Initiator detected error message received"},
329   {0x49,0x00,D|T|L|P|W|R|S|O|M|C,"Invalid message error"},
330   {0x4A,0x00,D|T|L|P|W|R|S|O|M|C,"Command phase error"},
331   {0x4B,0x00,D|T|L|P|W|R|S|O|M|C,"Data phase error"},
332   {0x4C,0x00,D|T|L|P|W|R|S|O|M|C,"Logical unit failed self-configuration"},
333   {0x4E,0x00,D|T|L|P|W|R|S|O|M|C,"Overlapped commands attempted"},
334   {0x50,0x00,T,"Write append error"},
335   {0x50,0x01,T,"Write append position error"},
336   {0x50,0x02,T,"Position error related to timing"},
337   {0x51,0x00,T|O,"Erase failure"},
338   {0x52,0x00,T,"Cartridge fault"},
339   {0x53,0x00,D|T|L|W|R|S|O|M,"Media load or eject failed"},
340   {0x53,0x01,T,"Unload tape failure"},
341   {0x53,0x02,D|T|W|R|O|M,"Medium removal prevented"},
342   {0x54,0x00,P,"Scsi to host system interface failure"},
343   {0x55,0x00,P,"System resource failure"},
344   {0x57,0x00,R,"Unable to recover table-of-contents"},
345   {0x58,0x00,O,"Generation does not exist"},
346   {0x59,0x00,O,"Updated block read"},
347   {0x5A,0x00,D|T|L|P|W|R|S|O|M,"Operator request or state change input (unspecified)"},
348   {0x5A,0x01,D|T|W|R|O|M,"Operator medium removal request"},
349   {0x5A,0x02,D|T|W|O,"Operator selected write protect"},
350   {0x5A,0x03,D|T|W|O,"Operator selected write permit"},
351   {0x5B,0x00,D|T|L|P|W|R|S|O|M,"Log exception"},
352   {0x5B,0x01,D|T|L|P|W|R|S|O|M,"Threshold condition met"},
353   {0x5B,0x02,D|T|L|P|W|R|S|O|M,"Log counter at maximum"},
354   {0x5B,0x03,D|T|L|P|W|R|S|O|M,"Log list codes exhausted"},
355   {0x5C,0x00,D|O,"Rpl status change"},
356   {0x5C,0x01,D|O,"Spindles synchronized"},
357   {0x5C,0x02,D|O,"Spindles not synchronized"},
358   {0x60,0x00,S,"Lamp failure"},
359   {0x61,0x00,S,"Video acquisition error"},
360   {0x61,0x01,S,"Unable to acquire video"},
361   {0x61,0x02,S,"Out of focus"},
362   {0x62,0x00,S,"Scan head positioning error"},
363   {0x63,0x00,R,"End of user area encountered on this track"},
364   {0x64,0x00,R,"Illegal mode for this track"},
365   {0, 0, 0, NULL}
366 };
367
368 static const char *snstext[] = {
369     "None",                     /* There is no sense information */
370     "Recovered Error",          /* The last command completed successfully
371                                    but used error correction */
372     "Not Ready",                /* The addressed target is not ready */
373     "Medium Error",             /* Data error detected on the medium */
374     "Hardware Error",           /* Controller or device failure */
375     "Illegal Request",
376     "Unit Attention",           /* Removable medium was changed, or
377                                    the target has been reset */
378     "Data Protect",             /* Access to the data is blocked */
379     "Blank Check",              /* Reached unexpected written or unwritten
380                                    region of the medium */
381     "Key=9",                    /* Vendor specific */
382     "Copy Aborted",             /* COPY or COMPARE was aborted */
383     "Aborted Command",          /* The target aborted the command */
384     "Equal",                    /* A SEARCH DATA command found data equal */
385     "Volume Overflow",          /* Medium full with still data to be written */
386     "Miscompare",               /* Source data and data on the medium
387                                    do not agree */
388     "Key=15"                    /* Reserved */
389 };
390
391 /* Print sense information */
392 void sg_print_sense(const char * leadin, const unsigned char * sense_buffer,
393                     int sb_len)
394 {
395     int i, s;
396     int sense_class, valid, code;
397     const char * error = NULL;
398
399     sense_class = (sense_buffer[0] >> 4) & 0x07;
400     code = sense_buffer[0] & 0xf;
401     valid = sense_buffer[0] & 0x80;
402
403     if (sense_class == 7) {     /* extended sense data */
404         s = sense_buffer[7] + 8;
405         if(s > sb_len)
406            s = sb_len;
407
408         if (!valid)
409             fprintf(OUTP, "[valid=0] ");
410         fprintf(OUTP, "Info fld=0x%x, ", (int)((sense_buffer[3] << 24) |
411                 (sense_buffer[4] << 16) | (sense_buffer[5] << 8) |
412                 sense_buffer[6]));
413
414         if (sense_buffer[2] & 0x80)
415            fprintf(OUTP, "FMK ");     /* current command has read a filemark */
416         if (sense_buffer[2] & 0x40)
417            fprintf(OUTP, "EOM ");     /* end-of-medium condition exists */
418         if (sense_buffer[2] & 0x20)
419            fprintf(OUTP, "ILI ");     /* incorrect block length requested */
420
421         switch (code) {
422         case 0x0:
423             error = "Current";  /* error concerns current command */
424             break;
425         case 0x1:
426             error = "Deferred"; /* error concerns some earlier command */
427                 /* e.g., an earlier write to disk cache succeeded, but
428                    now the disk discovers that it cannot write the data */
429             break;
430         default:
431             error = "Invalid";
432         }
433
434         fprintf(OUTP, "%s ", error);
435
436         if (leadin)
437             fprintf(OUTP, "%s: ", leadin);
438         fprintf(OUTP, "sense key: %s\n", snstext[sense_buffer[2] & 0x0f]);
439
440         /* Check to see if additional sense information is available */
441         if(sense_buffer[7] + 7 < 13 ||
442            (sense_buffer[12] == 0  && sense_buffer[13] ==  0)) goto done;
443
444         for(i=0; additional[i].text; i++)
445             if(additional[i].code1 == sense_buffer[12] &&
446                additional[i].code2 == sense_buffer[13])
447                 fprintf(OUTP, "Additional sense indicates: %s\n",
448                         additional[i].text);
449
450         for(i=0; additional2[i].text; i++)
451             if(additional2[i].code1 == sense_buffer[12] &&
452                additional2[i].code2_min >= sense_buffer[13]  &&
453                additional2[i].code2_max <= sense_buffer[13]) {
454                 fprintf(OUTP, "Additional sense indicates: ");
455                 fprintf(OUTP, additional2[i].text, sense_buffer[13]);
456                 fprintf(OUTP, "\n");
457             };
458     } else {    /* non-extended sense data */
459
460          /*
461           * Standard says:
462           *    sense_buffer[0] & 0200 : address valid
463           *    sense_buffer[0] & 0177 : vendor-specific error code
464           *    sense_buffer[1] & 0340 : vendor-specific
465           *    sense_buffer[1..3] : 21-bit logical block address
466           */
467
468         if (leadin)
469             fprintf(OUTP, "%s: ", leadin);
470         if (sense_buffer[0] < 15)
471             fprintf(OUTP, 
472                     "old sense: key %s\n", snstext[sense_buffer[0] & 0x0f]);
473         else
474             fprintf(OUTP, "sns = %2x %2x\n", sense_buffer[0], sense_buffer[2]);
475
476         fprintf(OUTP, "Non-extended sense class %d code 0x%0x ", 
477                 sense_class, code);
478         s = 4;
479     }
480
481  done:
482     fprintf(OUTP, "Raw sense data (in hex):\n  ");
483     for (i = 0; i < s; ++i) {
484         if ((i > 0) && (0 == (i % 24)))
485             fprintf(OUTP, "\n  ");
486         fprintf(OUTP, "%02x ", sense_buffer[i]);
487     }
488     fprintf(OUTP, "\n");
489     return;
490 }
491
492 static const char * hostbyte_table[]={
493 "DID_OK", "DID_NO_CONNECT", "DID_BUS_BUSY", "DID_TIME_OUT", "DID_BAD_TARGET",
494 "DID_ABORT", "DID_PARITY", "DID_ERROR", "DID_RESET", "DID_BAD_INTR",
495 "DID_PASSTHROUGH", "DID_SOFT_ERROR", NULL};
496
497 void sg_print_host_status(int host_status)
498 {   static int maxcode=0;
499     int i;
500
501     if(! maxcode) {
502         for(i = 0; hostbyte_table[i]; i++) ;
503         maxcode = i-1;
504     }
505     fprintf(OUTP, "Host_status=0x%02x", host_status);
506     if(host_status > maxcode) {
507         fprintf(OUTP, "is invalid ");
508         return;
509     }
510     fprintf(OUTP, "(%s) ",hostbyte_table[host_status]);
511 }
512
513 static const char * driverbyte_table[]={
514 "DRIVER_OK", "DRIVER_BUSY", "DRIVER_SOFT",  "DRIVER_MEDIA", "DRIVER_ERROR",
515 "DRIVER_INVALID", "DRIVER_TIMEOUT", "DRIVER_HARD", "DRIVER_SENSE", NULL};
516
517 static const char * driversuggest_table[]={"SUGGEST_OK",
518 "SUGGEST_RETRY", "SUGGEST_ABORT", "SUGGEST_REMAP", "SUGGEST_DIE",
519 unknown,unknown,unknown, "SUGGEST_SENSE",NULL};
520
521
522 void sg_print_driver_status(int driver_status)
523 {
524     static int driver_max =0 , suggest_max=0;
525     int i;
526     int dr = driver_status & SG_ERR_DRIVER_MASK;
527     int su = (driver_status & SG_ERR_SUGGEST_MASK) >> 4;
528
529     if(! driver_max) {
530         for(i = 0; driverbyte_table[i]; i++) ;
531         driver_max = i;
532         for(i = 0; driversuggest_table[i]; i++) ;
533         suggest_max = i;
534     }
535     fprintf(OUTP, "Driver_status=0x%02x",driver_status);
536     fprintf(OUTP, " (%s,%s) ",
537             dr < driver_max  ? driverbyte_table[dr]:"invalid",
538             su < suggest_max ? driversuggest_table[su]:"invalid");
539 }
540
541 #ifdef SG_IO
542 int sg_chk_n_print3(const char * leadin, struct sg_io_hdr * hp)
543 {
544     return sg_chk_n_print(leadin, hp->masked_status, hp->host_status,
545                           hp->driver_status, hp->sbp, hp->sb_len_wr);
546 }
547 #endif
548
549 int sg_chk_n_print(const char * leadin, int masked_status,
550                    int host_status, int driver_status,
551                    const unsigned char * sense_buffer, int sb_len)
552 {
553     int done_leadin = 0;
554     int done_sense = 0;
555
556     if ((0 == masked_status) && (0 == host_status) &&
557         (0 == driver_status))
558         return 1;       /* No problems */
559     if (0 != masked_status) {
560         if (leadin)
561             fprintf(OUTP, "%s: ", leadin);
562         done_leadin = 1;
563         sg_print_status(masked_status);
564         fprintf(OUTP, "\n");
565         if (sense_buffer && ((masked_status == CHECK_CONDITION) ||
566                              (masked_status == COMMAND_TERMINATED))) {
567             sg_print_sense(0, sense_buffer, sb_len);
568             done_sense = 1;
569         }
570     }
571     if (0 != host_status) {
572         if (leadin && (! done_leadin))
573             fprintf(OUTP, "%s: ", leadin);
574         if (done_leadin)
575             fprintf(OUTP, "plus...: ");
576         else
577             done_leadin = 1;
578         sg_print_host_status(host_status);
579         fprintf(OUTP, "\n");
580     }
581     if (0 != driver_status) {
582         if (leadin && (! done_leadin))
583             fprintf(OUTP, "%s: ", leadin);
584         if (done_leadin)
585             fprintf(OUTP, "plus...: ");
586         else
587             done_leadin = 1;
588         sg_print_driver_status(driver_status);
589         fprintf(OUTP, "\n");
590         if (sense_buffer && (! done_sense) &&
591             (SG_ERR_DRIVER_SENSE & driver_status))
592             sg_print_sense(0, sense_buffer, sb_len);
593     }
594     return 0;
595 }
596
597 #ifdef SG_IO
598 int sg_err_category3(struct sg_io_hdr * hp)
599 {
600     return sg_err_category(hp->masked_status, hp->host_status,
601                            hp->driver_status, hp->sbp, hp->sb_len_wr);
602 }
603 #endif
604
605 int sg_err_category(int masked_status, int host_status,
606                     int driver_status, const unsigned char * sense_buffer,
607                     int sb_len)
608 {
609     if ((0 == masked_status) && (0 == host_status) &&
610         (0 == driver_status))
611         return SG_ERR_CAT_CLEAN;
612     if ((CHECK_CONDITION == masked_status) ||
613         (COMMAND_TERMINATED == masked_status) ||
614         (SG_ERR_DRIVER_SENSE & driver_status)) {
615         if (sense_buffer && (sb_len > 2)) {
616             if(RECOVERED_ERROR == sense_buffer[2])
617                 return SG_ERR_CAT_RECOVERED;
618             else if ((UNIT_ATTENTION == (0x0f & sense_buffer[2])) &&
619                      (sb_len > 12)) {
620                 if (0x28 == sense_buffer[12])
621                     return SG_ERR_CAT_MEDIA_CHANGED;
622                 if (0x29 == sense_buffer[12])
623                     return SG_ERR_CAT_RESET;
624             }
625         }
626         return SG_ERR_CAT_SENSE;
627     }
628     if (0 != host_status) {
629         if ((SG_ERR_DID_NO_CONNECT == host_status) ||
630             (SG_ERR_DID_BUS_BUSY == host_status) ||
631             (SG_ERR_DID_TIME_OUT == host_status))
632             return SG_ERR_CAT_TIMEOUT;
633     }
634     if (0 != driver_status) {
635         if (SG_ERR_DRIVER_TIMEOUT == driver_status)
636             return SG_ERR_CAT_TIMEOUT;
637     }
638     return SG_ERR_CAT_OTHER;
639 }
640
641 int sg_get_command_size(unsigned char opcode)
642 {
643     return COMMAND_SIZE(opcode);
644 }