sg_err.cc
上传用户:weiliju62
上传日期:2007-01-06
资源大小:619k
文件大小:25k
源码类别:

SCSI/ASPI

开发平台:

MultiPlatform

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