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