2
0

utils.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef SCSI_UTILS_H
  2. #define SCSI_UTILS_H
  3. #ifdef CONFIG_LINUX
  4. #include <scsi/sg.h>
  5. #endif
  6. #define SCSI_CMD_BUF_SIZE 16
  7. #define SCSI_SENSE_LEN 18
  8. #define SCSI_SENSE_LEN_SCANNER 32
  9. #define SCSI_INQUIRY_LEN 36
  10. enum SCSIXferMode {
  11. SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
  12. SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
  13. SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
  14. };
  15. enum SCSIHostStatus {
  16. SCSI_HOST_OK,
  17. SCSI_HOST_NO_LUN,
  18. SCSI_HOST_BUSY,
  19. SCSI_HOST_TIME_OUT,
  20. SCSI_HOST_BAD_RESPONSE,
  21. SCSI_HOST_ABORTED,
  22. SCSI_HOST_ERROR = 0x07,
  23. SCSI_HOST_RESET = 0x08,
  24. SCSI_HOST_TRANSPORT_DISRUPTED = 0xe,
  25. SCSI_HOST_TARGET_FAILURE = 0x10,
  26. SCSI_HOST_RESERVATION_ERROR = 0x11,
  27. SCSI_HOST_ALLOCATION_FAILURE = 0x12,
  28. SCSI_HOST_MEDIUM_ERROR = 0x13,
  29. };
  30. typedef struct SCSICommand {
  31. uint8_t buf[SCSI_CMD_BUF_SIZE];
  32. int len;
  33. size_t xfer;
  34. uint64_t lba;
  35. enum SCSIXferMode mode;
  36. } SCSICommand;
  37. typedef struct SCSISense {
  38. uint8_t key;
  39. uint8_t asc;
  40. uint8_t ascq;
  41. } SCSISense;
  42. int scsi_build_sense(uint8_t *buf, SCSISense sense);
  43. SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len);
  44. int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense,
  45. bool fixed_sense);
  46. /*
  47. * Predefined sense codes
  48. */
  49. /* No sense data available */
  50. extern const struct SCSISense sense_code_NO_SENSE;
  51. /* LUN not ready, Manual intervention required */
  52. extern const struct SCSISense sense_code_LUN_NOT_READY;
  53. /* LUN not ready, Medium not present */
  54. extern const struct SCSISense sense_code_NO_MEDIUM;
  55. /* LUN not ready, medium removal prevented */
  56. extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
  57. /* Hardware error, internal target failure */
  58. extern const struct SCSISense sense_code_TARGET_FAILURE;
  59. /* Illegal request, invalid command operation code */
  60. extern const struct SCSISense sense_code_INVALID_OPCODE;
  61. /* Illegal request, LBA out of range */
  62. extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
  63. /* Illegal request, Invalid field in CDB */
  64. extern const struct SCSISense sense_code_INVALID_FIELD;
  65. /* Illegal request, Invalid field in parameter list */
  66. extern const struct SCSISense sense_code_INVALID_PARAM;
  67. /* Illegal request, Invalid value in parameter list */
  68. extern const struct SCSISense sense_code_INVALID_PARAM_VALUE;
  69. /* Illegal request, Parameter list length error */
  70. extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
  71. /* Illegal request, LUN not supported */
  72. extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
  73. /* Illegal request, Saving parameters not supported */
  74. extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
  75. /* Illegal request, Incompatible format */
  76. extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
  77. /* Illegal request, medium removal prevented */
  78. extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
  79. /* Illegal request, Invalid Transfer Tag */
  80. extern const struct SCSISense sense_code_INVALID_TAG;
  81. /* Command aborted, I/O process terminated */
  82. extern const struct SCSISense sense_code_IO_ERROR;
  83. /* Command aborted, I_T Nexus loss occurred */
  84. extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
  85. /* Command aborted, Logical Unit failure */
  86. extern const struct SCSISense sense_code_LUN_FAILURE;
  87. /* Command aborted, LUN Communication failure */
  88. extern const struct SCSISense sense_code_LUN_COMM_FAILURE;
  89. /* Command aborted, Overlapped Commands Attempted */
  90. extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
  91. /* Medium error, Unrecovered read error */
  92. extern const struct SCSISense sense_code_READ_ERROR;
  93. /* LUN not ready, Cause not reportable */
  94. extern const struct SCSISense sense_code_NOT_READY;
  95. /* Unit attention, Capacity data has changed */
  96. extern const struct SCSISense sense_code_CAPACITY_CHANGED;
  97. /* Unit attention, SCSI bus reset */
  98. extern const struct SCSISense sense_code_SCSI_BUS_RESET;
  99. /* LUN not ready, Medium not present */
  100. extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
  101. /* Unit attention, Power on, reset or bus device reset occurred */
  102. extern const struct SCSISense sense_code_RESET;
  103. /* Unit attention, Medium may have changed*/
  104. extern const struct SCSISense sense_code_MEDIUM_CHANGED;
  105. /* Unit attention, Reported LUNs data has changed */
  106. extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
  107. /* Unit attention, Device internal reset */
  108. extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
  109. /* Data Protection, Write Protected */
  110. extern const struct SCSISense sense_code_WRITE_PROTECTED;
  111. /* Data Protection, Space Allocation Failed Write Protect */
  112. extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
  113. #define SENSE_CODE(x) sense_code_ ## x
  114. int scsi_sense_to_errno(int key, int asc, int ascq);
  115. int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size);
  116. bool scsi_sense_buf_is_guest_recoverable(const uint8_t *sense, size_t sense_size);
  117. int scsi_convert_sense(uint8_t *in_buf, int in_len,
  118. uint8_t *buf, int len, bool fixed);
  119. const char *scsi_command_name(uint8_t cmd);
  120. uint64_t scsi_cmd_lba(SCSICommand *cmd);
  121. uint32_t scsi_data_cdb_xfer(uint8_t *buf);
  122. uint32_t scsi_cdb_xfer(uint8_t *buf);
  123. int scsi_cdb_length(uint8_t *buf);
  124. /* Linux SG_IO interface. */
  125. #ifdef CONFIG_LINUX
  126. #define SG_ERR_DRIVER_TIMEOUT 0x06
  127. #define SG_ERR_DRIVER_SENSE 0x08
  128. #endif
  129. int scsi_sense_from_errno(int errno_value, SCSISense *sense);
  130. int scsi_sense_from_host_status(uint8_t host_status, SCSISense *sense);
  131. #endif