]>
Commit | Line | Data |
---|---|---|
1e37607b GH |
1 | #ifndef QEMU_HW_SCSI_H |
2 | #define QEMU_HW_SCSI_H | |
43b443b6 | 3 | |
83c9f4ca | 4 | #include "hw/qdev.h" |
4be74634 | 5 | #include "qemu/typedefs.h" |
0d09e41a | 6 | #include "hw/block/block.h" |
9c17d615 | 7 | #include "sysemu/sysemu.h" |
8e0a9320 | 8 | #include "qemu/notify.h" |
43b443b6 | 9 | |
27d6bf40 MA |
10 | #define MAX_SCSI_DEVS 255 |
11 | ||
29362ebe | 12 | #define SCSI_CMD_BUF_SIZE 16 |
84642435 AH |
13 | #define SCSI_SENSE_LEN 18 |
14 | #define SCSI_INQUIRY_LEN 36 | |
29362ebe | 15 | |
43b443b6 | 16 | typedef struct SCSIBus SCSIBus; |
afd4030c | 17 | typedef struct SCSIBusInfo SCSIBusInfo; |
2599aece | 18 | typedef struct SCSICommand SCSICommand; |
43b443b6 | 19 | typedef struct SCSIDevice SCSIDevice; |
5c6c0e51 | 20 | typedef struct SCSIRequest SCSIRequest; |
8dbd4574 | 21 | typedef struct SCSIReqOps SCSIReqOps; |
43b443b6 | 22 | |
97a06435 GH |
23 | enum SCSIXferMode { |
24 | SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */ | |
25 | SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */ | |
26 | SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */ | |
27 | }; | |
28 | ||
a1f0cce2 HR |
29 | typedef struct SCSISense { |
30 | uint8_t key; | |
31 | uint8_t asc; | |
32 | uint8_t ascq; | |
33 | } SCSISense; | |
34 | ||
2e323f03 | 35 | #define SCSI_SENSE_BUF_SIZE_OLD 96 |
c5f52875 | 36 | #define SCSI_SENSE_BUF_SIZE 252 |
b45ef674 | 37 | |
2599aece PB |
38 | struct SCSICommand { |
39 | uint8_t buf[SCSI_CMD_BUF_SIZE]; | |
40 | int len; | |
41 | size_t xfer; | |
42 | uint64_t lba; | |
43 | enum SCSIXferMode mode; | |
44 | }; | |
45 | ||
5c6c0e51 | 46 | struct SCSIRequest { |
4c41d2ef GH |
47 | SCSIBus *bus; |
48 | SCSIDevice *dev; | |
adcf2754 | 49 | const SCSIReqOps *ops; |
ad2d30f7 | 50 | uint32_t refcount; |
4c41d2ef | 51 | uint32_t tag; |
89b08ae1 | 52 | uint32_t lun; |
ed3a34a3 | 53 | uint32_t status; |
61e68b3f | 54 | void *hba_private; |
01e95455 | 55 | size_t resid; |
2599aece | 56 | SCSICommand cmd; |
8e0a9320 | 57 | NotifierList cancel_notifiers; |
61e68b3f FZ |
58 | |
59 | /* Note: | |
60 | * - fields before sense are initialized by scsi_req_alloc; | |
61 | * - sense[] is uninitialized; | |
62 | * - fields after sense are memset to 0 by scsi_req_alloc. | |
63 | * */ | |
64 | ||
65 | uint8_t sense[SCSI_SENSE_BUF_SIZE]; | |
66 | uint32_t sense_len; | |
67 | bool enqueued; | |
68 | bool io_canceled; | |
69 | bool retry; | |
70 | bool dma_started; | |
7c84b1b8 | 71 | BlockAIOCB *aiocb; |
3d5aba97 | 72 | QEMUSGList *sg; |
9af99d98 | 73 | QTAILQ_ENTRY(SCSIRequest) next; |
5c6c0e51 | 74 | }; |
4c41d2ef | 75 | |
b9eea3e6 AL |
76 | #define TYPE_SCSI_DEVICE "scsi-device" |
77 | #define SCSI_DEVICE(obj) \ | |
78 | OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE) | |
79 | #define SCSI_DEVICE_CLASS(klass) \ | |
80 | OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE) | |
81 | #define SCSI_DEVICE_GET_CLASS(obj) \ | |
82 | OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE) | |
83 | ||
84 | typedef struct SCSIDeviceClass { | |
85 | DeviceClass parent_class; | |
a818a4b6 FZ |
86 | void (*realize)(SCSIDevice *dev, Error **errp); |
87 | void (*unrealize)(SCSIDevice *dev, Error **errp); | |
ff34c32c PB |
88 | int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, |
89 | void *hba_private); | |
b9eea3e6 AL |
90 | SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun, |
91 | uint8_t *buf, void *hba_private); | |
92 | void (*unit_attention_reported)(SCSIDevice *s); | |
93 | } SCSIDeviceClass; | |
94 | ||
43b443b6 GH |
95 | struct SCSIDevice |
96 | { | |
97 | DeviceState qdev; | |
71544d30 PB |
98 | VMChangeStateEntry *vmsentry; |
99 | QEMUBH *bh; | |
43b443b6 | 100 | uint32_t id; |
428c149b | 101 | BlockConf conf; |
6dc06f08 | 102 | SCSISense unit_attention; |
3653d8c4 | 103 | bool sense_is_ua; |
b45ef674 PB |
104 | uint8_t sense[SCSI_SENSE_BUF_SIZE]; |
105 | uint32_t sense_len; | |
9af99d98 | 106 | QTAILQ_HEAD(, SCSIRequest) requests; |
0d3545e7 | 107 | uint32_t channel; |
87dcd1b2 | 108 | uint32_t lun; |
b07995e3 | 109 | int blocksize; |
91376656 | 110 | int type; |
7877903a | 111 | uint64_t max_lba; |
43b443b6 GH |
112 | }; |
113 | ||
63f740dd PB |
114 | extern const VMStateDescription vmstate_scsi_device; |
115 | ||
116 | #define VMSTATE_SCSI_DEVICE(_field, _state) { \ | |
117 | .name = (stringify(_field)), \ | |
118 | .size = sizeof(SCSIDevice), \ | |
119 | .vmsd = &vmstate_scsi_device, \ | |
120 | .flags = VMS_STRUCT, \ | |
121 | .offset = vmstate_offset_value(_state, _field, SCSIDevice), \ | |
122 | } | |
123 | ||
43b443b6 GH |
124 | /* cdrom.c */ |
125 | int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track); | |
126 | int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num); | |
127 | ||
128 | /* scsi-bus.c */ | |
8dbd4574 PB |
129 | struct SCSIReqOps { |
130 | size_t size; | |
12010e7b PB |
131 | void (*free_req)(SCSIRequest *req); |
132 | int32_t (*send_command)(SCSIRequest *req, uint8_t *buf); | |
133 | void (*read_data)(SCSIRequest *req); | |
134 | void (*write_data)(SCSIRequest *req); | |
12010e7b | 135 | uint8_t *(*get_buf)(SCSIRequest *req); |
63f740dd PB |
136 | |
137 | void (*save_request)(QEMUFile *f, SCSIRequest *req); | |
138 | void (*load_request)(QEMUFile *f, SCSIRequest *req); | |
8dbd4574 PB |
139 | }; |
140 | ||
afd4030c | 141 | struct SCSIBusInfo { |
7e0380b9 | 142 | int tcq; |
0d3545e7 | 143 | int max_channel, max_target, max_lun; |
ff34c32c PB |
144 | int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, |
145 | void *hba_private); | |
c6df7102 | 146 | void (*transfer_data)(SCSIRequest *req, uint32_t arg); |
01e95455 | 147 | void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid); |
94d3f98a | 148 | void (*cancel)(SCSIRequest *req); |
53200fad | 149 | void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense); |
3d5aba97 | 150 | QEMUSGList *(*get_sg_list)(SCSIRequest *req); |
63f740dd PB |
151 | |
152 | void (*save_request)(QEMUFile *f, SCSIRequest *req); | |
153 | void *(*load_request)(QEMUFile *f, SCSIRequest *req); | |
8e86b93c | 154 | void (*free_request)(SCSIBus *bus, void *priv); |
cfdc1bb0 PB |
155 | }; |
156 | ||
0d936928 AL |
157 | #define TYPE_SCSI_BUS "SCSI" |
158 | #define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS) | |
159 | ||
43b443b6 GH |
160 | struct SCSIBus { |
161 | BusState qbus; | |
162 | int busnr; | |
163 | ||
6dc06f08 | 164 | SCSISense unit_attention; |
afd4030c | 165 | const SCSIBusInfo *info; |
43b443b6 GH |
166 | }; |
167 | ||
b1187b51 AF |
168 | void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host, |
169 | const SCSIBusInfo *info, const char *bus_name); | |
43b443b6 GH |
170 | |
171 | static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d) | |
172 | { | |
173 | return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); | |
174 | } | |
175 | ||
4be74634 | 176 | SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, |
76534da7 | 177 | int unit, bool removable, int bootindex, |
caad4eb3 AF |
178 | const char *serial, Error **errp); |
179 | void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp); | |
43b443b6 | 180 | |
a1f0cce2 HR |
181 | /* |
182 | * Predefined sense codes | |
183 | */ | |
184 | ||
185 | /* No sense data available */ | |
186 | extern const struct SCSISense sense_code_NO_SENSE; | |
187 | /* LUN not ready, Manual intervention required */ | |
188 | extern const struct SCSISense sense_code_LUN_NOT_READY; | |
189 | /* LUN not ready, Medium not present */ | |
190 | extern const struct SCSISense sense_code_NO_MEDIUM; | |
68bb01f3 MA |
191 | /* LUN not ready, medium removal prevented */ |
192 | extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED; | |
a1f0cce2 HR |
193 | /* Hardware error, internal target failure */ |
194 | extern const struct SCSISense sense_code_TARGET_FAILURE; | |
195 | /* Illegal request, invalid command operation code */ | |
196 | extern const struct SCSISense sense_code_INVALID_OPCODE; | |
197 | /* Illegal request, LBA out of range */ | |
198 | extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE; | |
199 | /* Illegal request, Invalid field in CDB */ | |
200 | extern const struct SCSISense sense_code_INVALID_FIELD; | |
380feaff PB |
201 | /* Illegal request, Invalid field in parameter list */ |
202 | extern const struct SCSISense sense_code_INVALID_PARAM; | |
203 | /* Illegal request, Parameter list length error */ | |
204 | extern const struct SCSISense sense_code_INVALID_PARAM_LEN; | |
a1f0cce2 HR |
205 | /* Illegal request, LUN not supported */ |
206 | extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED; | |
a872a304 PB |
207 | /* Illegal request, Saving parameters not supported */ |
208 | extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED; | |
209 | /* Illegal request, Incompatible format */ | |
210 | extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT; | |
68bb01f3 MA |
211 | /* Illegal request, medium removal prevented */ |
212 | extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED; | |
9ec557bd HG |
213 | /* Illegal request, Invalid Transfer Tag */ |
214 | extern const struct SCSISense sense_code_INVALID_TAG; | |
a1f0cce2 HR |
215 | /* Command aborted, I/O process terminated */ |
216 | extern const struct SCSISense sense_code_IO_ERROR; | |
217 | /* Command aborted, I_T Nexus loss occurred */ | |
218 | extern const struct SCSISense sense_code_I_T_NEXUS_LOSS; | |
219 | /* Command aborted, Logical Unit failure */ | |
220 | extern const struct SCSISense sense_code_LUN_FAILURE; | |
9ec557bd HG |
221 | /* Command aborted, Overlapped Commands Attempted */ |
222 | extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS; | |
aaebacef PB |
223 | /* LUN not ready, Capacity data has changed */ |
224 | extern const struct SCSISense sense_code_CAPACITY_CHANGED; | |
8a9c16f6 PB |
225 | /* LUN not ready, Medium not present */ |
226 | extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM; | |
a872a304 PB |
227 | /* Unit attention, Power on, reset or bus device reset occurred */ |
228 | extern const struct SCSISense sense_code_RESET; | |
229 | /* Unit attention, Medium may have changed*/ | |
230 | extern const struct SCSISense sense_code_MEDIUM_CHANGED; | |
231 | /* Unit attention, Reported LUNs data has changed */ | |
232 | extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED; | |
233 | /* Unit attention, Device internal reset */ | |
234 | extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET; | |
6a8a685c RS |
235 | /* Data Protection, Write Protected */ |
236 | extern const struct SCSISense sense_code_WRITE_PROTECTED; | |
703dd81a PB |
237 | /* Data Protection, Space Allocation Failed Write Protect */ |
238 | extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED; | |
a1f0cce2 HR |
239 | |
240 | #define SENSE_CODE(x) sense_code_ ## x | |
241 | ||
1894df02 HR |
242 | uint32_t scsi_data_cdb_xfer(uint8_t *buf); |
243 | uint32_t scsi_cdb_xfer(uint8_t *buf); | |
244 | int scsi_cdb_length(uint8_t *buf); | |
a1f0cce2 | 245 | int scsi_sense_valid(SCSISense sense); |
f3b338ef PB |
246 | int scsi_build_sense(uint8_t *in_buf, int in_len, |
247 | uint8_t *buf, int len, bool fixed); | |
a1f0cce2 | 248 | |
adcf2754 PB |
249 | SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, |
250 | uint32_t tag, uint32_t lun, void *hba_private); | |
c5bf71a9 | 251 | SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, |
c39ce112 PB |
252 | uint8_t *buf, void *hba_private); |
253 | int32_t scsi_req_enqueue(SCSIRequest *req); | |
89b08ae1 | 254 | void scsi_req_free(SCSIRequest *req); |
ad2d30f7 PB |
255 | SCSIRequest *scsi_req_ref(SCSIRequest *req); |
256 | void scsi_req_unref(SCSIRequest *req); | |
37659e51 | 257 | |
ff34c32c PB |
258 | int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, |
259 | void *hba_private); | |
3e7e180a | 260 | int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf); |
b45ef674 | 261 | void scsi_req_build_sense(SCSIRequest *req, SCSISense sense); |
ec766865 | 262 | void scsi_req_print(SCSIRequest *req); |
ad3376cc | 263 | void scsi_req_continue(SCSIRequest *req); |
ab9adc88 | 264 | void scsi_req_data(SCSIRequest *req, int len); |
682a9b21 | 265 | void scsi_req_complete(SCSIRequest *req, int status); |
0c34459b | 266 | uint8_t *scsi_req_get_buf(SCSIRequest *req); |
74382217 | 267 | int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len); |
d5776465 | 268 | void scsi_req_cancel_complete(SCSIRequest *req); |
94d3f98a | 269 | void scsi_req_cancel(SCSIRequest *req); |
8e0a9320 | 270 | void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier); |
71544d30 | 271 | void scsi_req_retry(SCSIRequest *req); |
c7b48872 | 272 | void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense); |
e48e84ea | 273 | void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense); |
53200fad | 274 | void scsi_device_report_change(SCSIDevice *dev, SCSISense sense); |
b45ef674 | 275 | int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed); |
0d3545e7 | 276 | SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun); |
89b08ae1 | 277 | |
765d1525 PB |
278 | /* scsi-generic.c. */ |
279 | extern const SCSIReqOps scsi_generic_req_ops; | |
280 | ||
43b443b6 | 281 | #endif |