5 #include "block/block.h"
6 #include "hw/block-common.h"
7 #include "sysemu/sysemu.h"
9 #define MAX_SCSI_DEVS 255
11 #define SCSI_CMD_BUF_SIZE 16
13 typedef struct SCSIBus SCSIBus
;
14 typedef struct SCSIBusInfo SCSIBusInfo
;
15 typedef struct SCSICommand SCSICommand
;
16 typedef struct SCSIDevice SCSIDevice
;
17 typedef struct SCSIRequest SCSIRequest
;
18 typedef struct SCSIReqOps SCSIReqOps
;
21 SCSI_XFER_NONE
, /* TEST_UNIT_READY, ... */
22 SCSI_XFER_FROM_DEV
, /* READ, INQUIRY, MODE_SENSE, ... */
23 SCSI_XFER_TO_DEV
, /* WRITE, MODE_SELECT, ... */
26 typedef struct SCSISense
{
32 #define SCSI_SENSE_BUF_SIZE 96
35 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
39 enum SCSIXferMode mode
;
45 const SCSIReqOps
*ops
;
52 BlockDriverAIOCB
*aiocb
;
55 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
61 QTAILQ_ENTRY(SCSIRequest
) next
;
64 #define TYPE_SCSI_DEVICE "scsi-device"
65 #define SCSI_DEVICE(obj) \
66 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE)
67 #define SCSI_DEVICE_CLASS(klass) \
68 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE)
69 #define SCSI_DEVICE_GET_CLASS(obj) \
70 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE)
72 typedef struct SCSIDeviceClass
{
73 DeviceClass parent_class
;
74 int (*init
)(SCSIDevice
*dev
);
75 void (*destroy
)(SCSIDevice
*s
);
76 SCSIRequest
*(*alloc_req
)(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
77 uint8_t *buf
, void *hba_private
);
78 void (*unit_attention_reported
)(SCSIDevice
*s
);
84 VMChangeStateEntry
*vmsentry
;
88 SCSISense unit_attention
;
90 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
92 QTAILQ_HEAD(, SCSIRequest
) requests
;
100 extern const VMStateDescription vmstate_scsi_device
;
102 #define VMSTATE_SCSI_DEVICE(_field, _state) { \
103 .name = (stringify(_field)), \
104 .size = sizeof(SCSIDevice), \
105 .vmsd = &vmstate_scsi_device, \
106 .flags = VMS_STRUCT, \
107 .offset = vmstate_offset_value(_state, _field, SCSIDevice), \
111 int cdrom_read_toc(int nb_sectors
, uint8_t *buf
, int msf
, int start_track
);
112 int cdrom_read_toc_raw(int nb_sectors
, uint8_t *buf
, int msf
, int session_num
);
117 void (*free_req
)(SCSIRequest
*req
);
118 int32_t (*send_command
)(SCSIRequest
*req
, uint8_t *buf
);
119 void (*read_data
)(SCSIRequest
*req
);
120 void (*write_data
)(SCSIRequest
*req
);
121 void (*cancel_io
)(SCSIRequest
*req
);
122 uint8_t *(*get_buf
)(SCSIRequest
*req
);
124 void (*save_request
)(QEMUFile
*f
, SCSIRequest
*req
);
125 void (*load_request
)(QEMUFile
*f
, SCSIRequest
*req
);
130 int max_channel
, max_target
, max_lun
;
131 void (*transfer_data
)(SCSIRequest
*req
, uint32_t arg
);
132 void (*complete
)(SCSIRequest
*req
, uint32_t arg
, size_t resid
);
133 void (*cancel
)(SCSIRequest
*req
);
134 void (*hotplug
)(SCSIBus
*bus
, SCSIDevice
*dev
);
135 void (*hot_unplug
)(SCSIBus
*bus
, SCSIDevice
*dev
);
136 void (*change
)(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
);
137 QEMUSGList
*(*get_sg_list
)(SCSIRequest
*req
);
139 void (*save_request
)(QEMUFile
*f
, SCSIRequest
*req
);
140 void *(*load_request
)(QEMUFile
*f
, SCSIRequest
*req
);
141 void (*free_request
)(SCSIBus
*bus
, void *priv
);
144 #define TYPE_SCSI_BUS "SCSI"
145 #define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS)
151 SCSISense unit_attention
;
152 const SCSIBusInfo
*info
;
155 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
);
157 static inline SCSIBus
*scsi_bus_from_device(SCSIDevice
*d
)
159 return DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
162 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
163 int unit
, bool removable
, int bootindex
);
164 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
);
167 * Predefined sense codes
170 /* No sense data available */
171 extern const struct SCSISense sense_code_NO_SENSE
;
172 /* LUN not ready, Manual intervention required */
173 extern const struct SCSISense sense_code_LUN_NOT_READY
;
174 /* LUN not ready, Medium not present */
175 extern const struct SCSISense sense_code_NO_MEDIUM
;
176 /* LUN not ready, medium removal prevented */
177 extern const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
;
178 /* Hardware error, internal target failure */
179 extern const struct SCSISense sense_code_TARGET_FAILURE
;
180 /* Illegal request, invalid command operation code */
181 extern const struct SCSISense sense_code_INVALID_OPCODE
;
182 /* Illegal request, LBA out of range */
183 extern const struct SCSISense sense_code_LBA_OUT_OF_RANGE
;
184 /* Illegal request, Invalid field in CDB */
185 extern const struct SCSISense sense_code_INVALID_FIELD
;
186 /* Illegal request, Invalid field in parameter list */
187 extern const struct SCSISense sense_code_INVALID_PARAM
;
188 /* Illegal request, Parameter list length error */
189 extern const struct SCSISense sense_code_INVALID_PARAM_LEN
;
190 /* Illegal request, LUN not supported */
191 extern const struct SCSISense sense_code_LUN_NOT_SUPPORTED
;
192 /* Illegal request, Saving parameters not supported */
193 extern const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
;
194 /* Illegal request, Incompatible format */
195 extern const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
;
196 /* Illegal request, medium removal prevented */
197 extern const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
;
198 /* Command aborted, I/O process terminated */
199 extern const struct SCSISense sense_code_IO_ERROR
;
200 /* Command aborted, I_T Nexus loss occurred */
201 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS
;
202 /* Command aborted, Logical Unit failure */
203 extern const struct SCSISense sense_code_LUN_FAILURE
;
204 /* LUN not ready, Capacity data has changed */
205 extern const struct SCSISense sense_code_CAPACITY_CHANGED
;
206 /* LUN not ready, Medium not present */
207 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
;
208 /* Unit attention, Power on, reset or bus device reset occurred */
209 extern const struct SCSISense sense_code_RESET
;
210 /* Unit attention, Medium may have changed*/
211 extern const struct SCSISense sense_code_MEDIUM_CHANGED
;
212 /* Unit attention, Reported LUNs data has changed */
213 extern const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
;
214 /* Unit attention, Device internal reset */
215 extern const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
;
216 /* Data Protection, Write Protected */
217 extern const struct SCSISense sense_code_WRITE_PROTECTED
;
219 #define SENSE_CODE(x) sense_code_ ## x
221 uint32_t scsi_data_cdb_length(uint8_t *buf
);
222 uint32_t scsi_cdb_length(uint8_t *buf
);
223 int scsi_sense_valid(SCSISense sense
);
224 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
225 uint8_t *buf
, int len
, bool fixed
);
227 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
228 uint32_t tag
, uint32_t lun
, void *hba_private
);
229 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
230 uint8_t *buf
, void *hba_private
);
231 int32_t scsi_req_enqueue(SCSIRequest
*req
);
232 void scsi_req_free(SCSIRequest
*req
);
233 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
);
234 void scsi_req_unref(SCSIRequest
*req
);
236 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
);
237 void scsi_req_print(SCSIRequest
*req
);
238 void scsi_req_continue(SCSIRequest
*req
);
239 void scsi_req_data(SCSIRequest
*req
, int len
);
240 void scsi_req_complete(SCSIRequest
*req
, int status
);
241 uint8_t *scsi_req_get_buf(SCSIRequest
*req
);
242 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
);
243 void scsi_req_abort(SCSIRequest
*req
, int status
);
244 void scsi_req_cancel(SCSIRequest
*req
);
245 void scsi_req_retry(SCSIRequest
*req
);
246 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
);
247 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
);
248 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
);
249 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
);
250 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int target
, int lun
);
252 /* scsi-generic.c. */
253 extern const SCSIReqOps scsi_generic_req_ops
;