1#ifndef SCSI_UTILS_H
2#define SCSI_UTILS_H
3
4#ifdef CONFIG_LINUX
5#include <scsi/sg.h>
6#endif
7
8#define SCSI_CMD_BUF_SIZE 16
9#define SCSI_SENSE_LEN 18
10#define SCSI_SENSE_LEN_SCANNER 32
11#define SCSI_INQUIRY_LEN 36
12
13enum SCSIXferMode {
14 SCSI_XFER_NONE,
15 SCSI_XFER_FROM_DEV,
16 SCSI_XFER_TO_DEV,
17};
18
19typedef struct SCSICommand {
20 uint8_t buf[SCSI_CMD_BUF_SIZE];
21 int len;
22 size_t xfer;
23 uint64_t lba;
24 enum SCSIXferMode mode;
25} SCSICommand;
26
27typedef struct SCSISense {
28 uint8_t key;
29 uint8_t asc;
30 uint8_t ascq;
31} SCSISense;
32
33int scsi_build_sense(uint8_t *buf, SCSISense sense);
34SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len);
35int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense,
36 bool fixed_sense);
37
38
39
40
41
42
43extern const struct SCSISense sense_code_NO_SENSE;
44
45extern const struct SCSISense sense_code_LUN_NOT_READY;
46
47extern const struct SCSISense sense_code_NO_MEDIUM;
48
49extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
50
51extern const struct SCSISense sense_code_TARGET_FAILURE;
52
53extern const struct SCSISense sense_code_INVALID_OPCODE;
54
55extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
56
57extern const struct SCSISense sense_code_INVALID_FIELD;
58
59extern const struct SCSISense sense_code_INVALID_PARAM;
60
61extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
62
63extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
64
65extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
66
67extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
68
69extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
70
71extern const struct SCSISense sense_code_INVALID_TAG;
72
73extern const struct SCSISense sense_code_IO_ERROR;
74
75extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
76
77extern const struct SCSISense sense_code_LUN_FAILURE;
78
79extern const struct SCSISense sense_code_LUN_COMM_FAILURE;
80
81extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
82
83extern const struct SCSISense sense_code_READ_ERROR;
84
85extern const struct SCSISense sense_code_NOT_READY;
86
87extern const struct SCSISense sense_code_CAPACITY_CHANGED;
88
89extern const struct SCSISense sense_code_SCSI_BUS_RESET;
90
91extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
92
93extern const struct SCSISense sense_code_RESET;
94
95extern const struct SCSISense sense_code_MEDIUM_CHANGED;
96
97extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
98
99extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
100
101extern const struct SCSISense sense_code_WRITE_PROTECTED;
102
103extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
104
105#define SENSE_CODE(x) sense_code_ ## x
106
107int scsi_sense_to_errno(int key, int asc, int ascq);
108int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size);
109bool scsi_sense_buf_is_guest_recoverable(const uint8_t *sense, size_t sense_size);
110
111int scsi_convert_sense(uint8_t *in_buf, int in_len,
112 uint8_t *buf, int len, bool fixed);
113const char *scsi_command_name(uint8_t cmd);
114
115uint64_t scsi_cmd_lba(SCSICommand *cmd);
116uint32_t scsi_data_cdb_xfer(uint8_t *buf);
117uint32_t scsi_cdb_xfer(uint8_t *buf);
118int scsi_cdb_length(uint8_t *buf);
119
120
121#ifdef CONFIG_LINUX
122#define SG_ERR_DRIVER_TIMEOUT 0x06
123#define SG_ERR_DRIVER_SENSE 0x08
124
125#define SG_ERR_DID_OK 0x00
126#define SG_ERR_DID_NO_CONNECT 0x01
127#define SG_ERR_DID_BUS_BUSY 0x02
128#define SG_ERR_DID_TIME_OUT 0x03
129
130#define SG_ERR_DRIVER_SENSE 0x08
131
132int sg_io_sense_from_errno(int errno_value, struct sg_io_hdr *io_hdr,
133 SCSISense *sense);
134#endif
135
136#endif
137