5 #include "hw/block/block.h"
6 #include "sysemu/sysemu.h"
7 #include "qemu/notify.h"
9 #define MAX_SCSI_DEVS 255
11 #define SCSI_CMD_BUF_SIZE 16
12 #define SCSI_SENSE_LEN 18
13 #define SCSI_INQUIRY_LEN 36
15 typedef struct SCSIBus SCSIBus;
16 typedef struct SCSIBusInfo SCSIBusInfo;
17 typedef struct SCSICommand SCSICommand;
18 typedef struct SCSIDevice SCSIDevice;
19 typedef struct SCSIRequest SCSIRequest;
20 typedef struct SCSIReqOps SCSIReqOps;
23 SCSI_XFER_NONE, /* TEST_UNIT_READY, ... */
24 SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
25 SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
28 typedef struct SCSISense {
34 #define SCSI_SENSE_BUF_SIZE_OLD 96
35 #define SCSI_SENSE_BUF_SIZE 252
38 uint8_t buf[SCSI_CMD_BUF_SIZE];
42 enum SCSIXferMode mode;
48 const SCSIReqOps *ops;
56 NotifierList cancel_notifiers;
59 * - fields before sense are initialized by scsi_req_alloc;
60 * - sense[] is uninitialized;
61 * - fields after sense are memset to 0 by scsi_req_alloc.
64 uint8_t sense[SCSI_SENSE_BUF_SIZE];
72 QTAILQ_ENTRY(SCSIRequest) next;
75 #define TYPE_SCSI_DEVICE "scsi-device"
76 #define SCSI_DEVICE(obj) \
77 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
78 #define SCSI_DEVICE_CLASS(klass) \
79 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
80 #define SCSI_DEVICE_GET_CLASS(obj) \
81 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
83 typedef struct SCSIDeviceClass {
84 DeviceClass parent_class;
85 void (*realize)(SCSIDevice *dev, Error **errp);
86 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
88 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
89 uint8_t *buf, void *hba_private);
90 void (*unit_attention_reported)(SCSIDevice *s);
96 VMChangeStateEntry *vmsentry;
100 SCSISense unit_attention;
102 uint8_t sense[SCSI_SENSE_BUF_SIZE];
104 QTAILQ_HEAD(, SCSIRequest) requests;
114 extern const VMStateDescription vmstate_scsi_device;
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), \
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);
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);
135 uint8_t *(*get_buf)(SCSIRequest *req);
137 void (*save_request)(QEMUFile *f, SCSIRequest *req);
138 void (*load_request)(QEMUFile *f, SCSIRequest *req);
143 int max_channel, max_target, max_lun;
144 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
146 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
147 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
148 void (*cancel)(SCSIRequest *req);
149 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
150 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
152 void (*save_request)(QEMUFile *f, SCSIRequest *req);
153 void *(*load_request)(QEMUFile *f, SCSIRequest *req);
154 void (*free_request)(SCSIBus *bus, void *priv);
157 #define TYPE_SCSI_BUS "SCSI"
158 #define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
164 SCSISense unit_attention;
165 const SCSIBusInfo *info;
168 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
169 const SCSIBusInfo *info, const char *bus_name);
171 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
173 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
176 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
177 int unit, bool removable, int bootindex,
178 const char *serial, Error **errp);
179 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp);
182 * Predefined sense codes
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;
191 /* LUN not ready, medium removal prevented */
192 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
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;
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;
205 /* Illegal request, LUN not supported */
206 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
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;
211 /* Illegal request, medium removal prevented */
212 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
213 /* Illegal request, Invalid Transfer Tag */
214 extern const struct SCSISense sense_code_INVALID_TAG;
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;
221 /* Command aborted, Overlapped Commands Attempted */
222 extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
223 /* LUN not ready, Capacity data has changed */
224 extern const struct SCSISense sense_code_CAPACITY_CHANGED;
225 /* LUN not ready, Medium not present */
226 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
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;
235 /* Data Protection, Write Protected */
236 extern const struct SCSISense sense_code_WRITE_PROTECTED;
237 /* Data Protection, Space Allocation Failed Write Protect */
238 extern const struct SCSISense sense_code_SPACE_ALLOC_FAILED;
240 #define SENSE_CODE(x) sense_code_ ## x
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);
245 int scsi_sense_valid(SCSISense sense);
246 int scsi_build_sense(uint8_t *in_buf, int in_len,
247 uint8_t *buf, int len, bool fixed);
249 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
250 uint32_t tag, uint32_t lun, void *hba_private);
251 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
252 uint8_t *buf, void *hba_private);
253 int32_t scsi_req_enqueue(SCSIRequest *req);
254 SCSIRequest *scsi_req_ref(SCSIRequest *req);
255 void scsi_req_unref(SCSIRequest *req);
257 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
259 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf);
260 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
261 void scsi_req_print(SCSIRequest *req);
262 void scsi_req_continue(SCSIRequest *req);
263 void scsi_req_data(SCSIRequest *req, int len);
264 void scsi_req_complete(SCSIRequest *req, int status);
265 uint8_t *scsi_req_get_buf(SCSIRequest *req);
266 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
267 void scsi_req_cancel_complete(SCSIRequest *req);
268 void scsi_req_cancel(SCSIRequest *req);
269 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier);
270 void scsi_req_retry(SCSIRequest *req);
271 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
272 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
273 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
274 void scsi_device_unit_attention_reported(SCSIDevice *dev);
275 void scsi_generic_read_device_identification(SCSIDevice *dev);
276 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
277 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
279 /* scsi-generic.c. */
280 extern const SCSIReqOps scsi_generic_req_ops;