1 #ifndef QEMU_HW_SCSI_H 2 #define QEMU_HW_SCSI_H 3 4 #include "block/aio.h" 5 #include "hw/block/block.h" 6 #include "hw/qdev-core.h" 7 #include "scsi/utils.h" 8 #include "qemu/notify.h" 9 #include "qom/object.h" 10 11 #define MAX_SCSI_DEVS 255 12 13 typedef struct SCSIBus SCSIBus; 14 typedef struct SCSIBusInfo SCSIBusInfo; 15 typedef struct SCSIDevice SCSIDevice; 16 typedef struct SCSIRequest SCSIRequest; 17 typedef struct SCSIReqOps SCSIReqOps; 18 19 #define SCSI_SENSE_BUF_SIZE_OLD 96 20 #define SCSI_SENSE_BUF_SIZE 252 21 #define DEFAULT_IO_TIMEOUT 30 22 23 struct SCSIRequest { 24 SCSIBus *bus; 25 SCSIDevice *dev; 26 const SCSIReqOps *ops; 27 AioContext *ctx; 28 uint32_t refcount; 29 uint32_t tag; 30 uint32_t lun; 31 int16_t status; 32 int16_t host_status; 33 void *hba_private; 34 uint64_t residual; 35 SCSICommand cmd; 36 NotifierList cancel_notifiers; 37 38 /* Note: 39 * - fields before sense are initialized by scsi_req_alloc; 40 * - sense[] is uninitialized; 41 * - fields after sense are memset to 0 by scsi_req_alloc. 42 * */ 43 44 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 45 uint32_t sense_len; 46 bool enqueued; 47 bool io_canceled; 48 bool retry; 49 bool dma_started; 50 BlockAIOCB *aiocb; 51 QEMUSGList *sg; 52 QTAILQ_ENTRY(SCSIRequest) next; 53 }; 54 55 #define TYPE_SCSI_DEVICE "scsi-device" 56 OBJECT_DECLARE_TYPE(SCSIDevice, SCSIDeviceClass, SCSI_DEVICE) 57 58 struct SCSIDeviceClass { 59 DeviceClass parent_class; 60 void (*realize)(SCSIDevice *dev, Error **errp); 61 void (*unrealize)(SCSIDevice *dev); 62 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 63 size_t buf_len, void *hba_private); 64 SCSIRequest *(*alloc_req)(SCSIDevice *s, uint32_t tag, uint32_t lun, 65 uint8_t *buf, void *hba_private); 66 void (*unit_attention_reported)(SCSIDevice *s); 67 }; 68 69 struct SCSIDevice 70 { 71 DeviceState qdev; 72 VMChangeStateEntry *vmsentry; 73 uint32_t id; 74 BlockConf conf; 75 SCSISense unit_attention; 76 bool sense_is_ua; 77 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 78 uint32_t sense_len; 79 80 /* 81 * The requests list is only accessed from the AioContext that executes 82 * requests or from the main loop when IOThread processing is stopped. 83 */ 84 QTAILQ_HEAD(, SCSIRequest) requests; 85 86 uint32_t channel; 87 uint32_t lun; 88 int blocksize; 89 int type; 90 uint64_t max_lba; 91 uint64_t wwn; 92 uint64_t port_wwn; 93 int scsi_version; 94 int default_scsi_version; 95 uint32_t io_timeout; 96 bool needs_vpd_bl_emulation; 97 bool hba_supports_iothread; 98 }; 99 100 extern const VMStateDescription vmstate_scsi_device; 101 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), \ 108 } 109 110 /* cdrom.c */ 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); 113 114 /* scsi-bus.c */ 115 struct SCSIReqOps { 116 size_t size; 117 void (*init_req)(SCSIRequest *req); 118 void (*free_req)(SCSIRequest *req); 119 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf); 120 void (*read_data)(SCSIRequest *req); 121 void (*write_data)(SCSIRequest *req); 122 uint8_t *(*get_buf)(SCSIRequest *req); 123 124 void (*save_request)(QEMUFile *f, SCSIRequest *req); 125 void (*load_request)(QEMUFile *f, SCSIRequest *req); 126 }; 127 128 struct SCSIBusInfo { 129 int tcq; 130 int max_channel, max_target, max_lun; 131 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 132 size_t buf_len, void *hba_private); 133 void (*transfer_data)(SCSIRequest *req, uint32_t arg); 134 void (*fail)(SCSIRequest *req); 135 void (*complete)(SCSIRequest *req, size_t residual); 136 void (*cancel)(SCSIRequest *req); 137 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense); 138 QEMUSGList *(*get_sg_list)(SCSIRequest *req); 139 140 void (*save_request)(QEMUFile *f, SCSIRequest *req); 141 void *(*load_request)(QEMUFile *f, SCSIRequest *req); 142 void (*free_request)(SCSIBus *bus, void *priv); 143 144 /* 145 * Temporarily stop submitting new requests between drained_begin() and 146 * drained_end(). Called from the main loop thread with the BQL held. 147 * 148 * Implement these callbacks if request processing is triggered by a file 149 * descriptor like an EventNotifier. Otherwise set them to NULL. 150 */ 151 void (*drained_begin)(SCSIBus *bus); 152 void (*drained_end)(SCSIBus *bus); 153 }; 154 155 #define TYPE_SCSI_BUS "SCSI" 156 OBJECT_DECLARE_SIMPLE_TYPE(SCSIBus, SCSI_BUS) 157 158 struct SCSIBus { 159 BusState qbus; 160 int busnr; 161 162 SCSISense unit_attention; 163 const SCSIBusInfo *info; 164 165 int drain_count; /* protected by BQL */ 166 }; 167 168 /** 169 * scsi_bus_init_named: Initialize a SCSI bus with the specified name 170 * @bus: SCSIBus object to initialize 171 * @bus_size: size of @bus object 172 * @host: Device which owns the bus (generally the SCSI controller) 173 * @info: structure defining callbacks etc for the controller 174 * @bus_name: Name to use for this bus 175 * 176 * This in-place initializes @bus as a new SCSI bus with a name 177 * provided by the caller. It is the caller's responsibility to make 178 * sure that name does not clash with the name of any other bus in the 179 * system. Unless you need the new bus to have a specific name, you 180 * should use scsi_bus_init() instead. 181 */ 182 void scsi_bus_init_named(SCSIBus *bus, size_t bus_size, DeviceState *host, 183 const SCSIBusInfo *info, const char *bus_name); 184 185 /** 186 * scsi_bus_init: Initialize a SCSI bus 187 * 188 * This in-place-initializes @bus as a new SCSI bus and gives it 189 * an automatically generated unique name. 190 */ 191 static inline void scsi_bus_init(SCSIBus *bus, size_t bus_size, 192 DeviceState *host, const SCSIBusInfo *info) 193 { 194 scsi_bus_init_named(bus, bus_size, host, info, NULL); 195 } 196 197 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d) 198 { 199 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); 200 } 201 202 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, 203 int unit, bool removable, BlockConf *conf, 204 const char *serial, Error **errp); 205 void scsi_bus_set_ua(SCSIBus *bus, SCSISense sense); 206 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus); 207 208 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, 209 uint32_t tag, uint32_t lun, void *hba_private); 210 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, 211 uint8_t *buf, size_t buf_len, void *hba_private); 212 int32_t scsi_req_enqueue(SCSIRequest *req); 213 SCSIRequest *scsi_req_ref(SCSIRequest *req); 214 void scsi_req_unref(SCSIRequest *req); 215 216 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 217 size_t buf_len, void *hba_private); 218 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 219 size_t buf_len); 220 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense); 221 void scsi_req_print(SCSIRequest *req); 222 void scsi_req_continue(SCSIRequest *req); 223 void scsi_req_data(SCSIRequest *req, int len); 224 void scsi_req_complete(SCSIRequest *req, int status); 225 void scsi_req_complete_failed(SCSIRequest *req, int host_status); 226 uint8_t *scsi_req_get_buf(SCSIRequest *req); 227 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len); 228 void scsi_req_cancel_complete(SCSIRequest *req); 229 void scsi_req_cancel(SCSIRequest *req); 230 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier); 231 void scsi_req_retry(SCSIRequest *req); 232 void scsi_device_drained_begin(SCSIDevice *sdev); 233 void scsi_device_drained_end(SCSIDevice *sdev); 234 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense); 235 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense); 236 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense); 237 void scsi_device_unit_attention_reported(SCSIDevice *dev); 238 void scsi_generic_read_device_inquiry(SCSIDevice *dev); 239 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed); 240 int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size, 241 uint8_t *buf, uint8_t buf_size, uint32_t timeout); 242 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun); 243 SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun); 244 245 /* scsi-generic.c. */ 246 extern const SCSIReqOps scsi_generic_req_ops; 247 248 /* scsi-disk.c */ 249 #define SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR 0 250 #define SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD 1 251 #define SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE 2 252 #define SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED 3 253 254 #endif 255