5 #include "block/block.h"
6 #include "hw/block/block.h"
7 #include "sysemu/sysemu.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 96
37 uint8_t buf[SCSI_CMD_BUF_SIZE];
41 enum SCSIXferMode mode;
47 const SCSIReqOps *ops;
54 BlockDriverAIOCB *aiocb;
57 uint8_t sense[SCSI_SENSE_BUF_SIZE];
63 QTAILQ_ENTRY(SCSIRequest) next;
66 #define TYPE_SCSI_DEVICE "scsi-device"
67 #define SCSI_DEVICE(obj) \
68 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
69 #define SCSI_DEVICE_CLASS(klass) \
70 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
71 #define SCSI_DEVICE_GET_CLASS(obj) \
72 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
74 typedef struct SCSIDeviceClass {
75 DeviceClass parent_class;
76 int (*init)(SCSIDevice *dev);
77 void (*destroy)(SCSIDevice *s);
78 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun,
79 uint8_t *buf, void *hba_private);
80 void (*unit_attention_reported)(SCSIDevice *s);
86 VMChangeStateEntry *vmsentry;
90 SCSISense unit_attention;
92 uint8_t sense[SCSI_SENSE_BUF_SIZE];
94 QTAILQ_HEAD(, SCSIRequest) requests;
102 extern const VMStateDescription vmstate_scsi_device;
104 #define VMSTATE_SCSI_DEVICE(_field, _state) { \
105 .name = (stringify(_field)), \
106 .size = sizeof(SCSIDevice), \
107 .vmsd = &vmstate_scsi_device, \
108 .flags = VMS_STRUCT, \
109 .offset = vmstate_offset_value(_state, _field, SCSIDevice), \
113 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track);
114 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num);
119 void (*free_req)(SCSIRequest *req);
120 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf);
121 void (*read_data)(SCSIRequest *req);
122 void (*write_data)(SCSIRequest *req);
123 void (*cancel_io)(SCSIRequest *req);
124 uint8_t *(*get_buf)(SCSIRequest *req);
126 void (*save_request)(QEMUFile *f, SCSIRequest *req);
127 void (*load_request)(QEMUFile *f, SCSIRequest *req);
132 int max_channel, max_target, max_lun;
133 void (*transfer_data)(SCSIRequest *req, uint32_t arg);
134 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid);
135 void (*cancel)(SCSIRequest *req);
136 void (*hotplug)(SCSIBus *bus, SCSIDevice *dev);
137 void (*hot_unplug)(SCSIBus *bus, SCSIDevice *dev);
138 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense);
139 QEMUSGList *(*get_sg_list)(SCSIRequest *req);
141 void (*save_request)(QEMUFile *f, SCSIRequest *req);
142 void *(*load_request)(QEMUFile *f, SCSIRequest *req);
143 void (*free_request)(SCSIBus *bus, void *priv);
146 #define TYPE_SCSI_BUS "SCSI"
147 #define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
153 SCSISense unit_attention;
154 const SCSIBusInfo *info;
157 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
158 const SCSIBusInfo *info, const char *bus_name);
160 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d)
162 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
165 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
166 int unit, bool removable, int bootindex,
167 const char *serial, Error **errp);
168 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp);
171 * Predefined sense codes
174 /* No sense data available */
175 extern const struct SCSISense sense_code_NO_SENSE;
176 /* LUN not ready, Manual intervention required */
177 extern const struct SCSISense sense_code_LUN_NOT_READY;
178 /* LUN not ready, Medium not present */
179 extern const struct SCSISense sense_code_NO_MEDIUM;
180 /* LUN not ready, medium removal prevented */
181 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED;
182 /* Hardware error, internal target failure */
183 extern const struct SCSISense sense_code_TARGET_FAILURE;
184 /* Illegal request, invalid command operation code */
185 extern const struct SCSISense sense_code_INVALID_OPCODE;
186 /* Illegal request, LBA out of range */
187 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE;
188 /* Illegal request, Invalid field in CDB */
189 extern const struct SCSISense sense_code_INVALID_FIELD;
190 /* Illegal request, Invalid field in parameter list */
191 extern const struct SCSISense sense_code_INVALID_PARAM;
192 /* Illegal request, Parameter list length error */
193 extern const struct SCSISense sense_code_INVALID_PARAM_LEN;
194 /* Illegal request, LUN not supported */
195 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED;
196 /* Illegal request, Saving parameters not supported */
197 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED;
198 /* Illegal request, Incompatible format */
199 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT;
200 /* Illegal request, medium removal prevented */
201 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED;
202 /* Illegal request, Invalid Transfer Tag */
203 extern const struct SCSISense sense_code_INVALID_TAG;
204 /* Command aborted, I/O process terminated */
205 extern const struct SCSISense sense_code_IO_ERROR;
206 /* Command aborted, I_T Nexus loss occurred */
207 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
208 /* Command aborted, Logical Unit failure */
209 extern const struct SCSISense sense_code_LUN_FAILURE;
210 /* Command aborted, Overlapped Commands Attempted */
211 extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
212 /* LUN not ready, Capacity data has changed */
213 extern const struct SCSISense sense_code_CAPACITY_CHANGED;
214 /* LUN not ready, Medium not present */
215 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
216 /* Unit attention, Power on, reset or bus device reset occurred */
217 extern const struct SCSISense sense_code_RESET;
218 /* Unit attention, Medium may have changed*/
219 extern const struct SCSISense sense_code_MEDIUM_CHANGED;
220 /* Unit attention, Reported LUNs data has changed */
221 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED;
222 /* Unit attention, Device internal reset */
223 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET;
224 /* Data Protection, Write Protected */
225 extern const struct SCSISense sense_code_WRITE_PROTECTED;
227 #define SENSE_CODE(x) sense_code_ ## x
229 uint32_t scsi_data_cdb_length(uint8_t *buf);
230 uint32_t scsi_cdb_length(uint8_t *buf);
231 int scsi_sense_valid(SCSISense sense);
232 int scsi_build_sense(uint8_t *in_buf, int in_len,
233 uint8_t *buf, int len, bool fixed);
235 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
236 uint32_t tag, uint32_t lun, void *hba_private);
237 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
238 uint8_t *buf, void *hba_private);
239 int32_t scsi_req_enqueue(SCSIRequest *req);
240 void scsi_req_free(SCSIRequest *req);
241 SCSIRequest *scsi_req_ref(SCSIRequest *req);
242 void scsi_req_unref(SCSIRequest *req);
244 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
245 void scsi_req_print(SCSIRequest *req);
246 void scsi_req_continue(SCSIRequest *req);
247 void scsi_req_data(SCSIRequest *req, int len);
248 void scsi_req_complete(SCSIRequest *req, int status);
249 uint8_t *scsi_req_get_buf(SCSIRequest *req);
250 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len);
251 void scsi_req_abort(SCSIRequest *req, int status);
252 void scsi_req_cancel(SCSIRequest *req);
253 void scsi_req_retry(SCSIRequest *req);
254 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense);
255 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense);
256 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense);
257 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed);
258 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun);
260 /* scsi-generic.c. */
261 extern const SCSIReqOps scsi_generic_req_ops;