1 #ifndef QEMU_HW_SCSI_H 2 #define QEMU_HW_SCSI_H 3 4 #include "hw/qdev.h" 5 #include "hw/block/block.h" 6 #include "sysemu/sysemu.h" 7 #include "scsi/utils.h" 8 #include "qemu/notify.h" 9 10 #define MAX_SCSI_DEVS 255 11 12 typedef struct SCSIBus SCSIBus; 13 typedef struct SCSIBusInfo SCSIBusInfo; 14 typedef struct SCSIDevice SCSIDevice; 15 typedef struct SCSIRequest SCSIRequest; 16 typedef struct SCSIReqOps SCSIReqOps; 17 18 #define SCSI_SENSE_BUF_SIZE_OLD 96 19 #define SCSI_SENSE_BUF_SIZE 252 20 21 struct SCSIRequest { 22 SCSIBus *bus; 23 SCSIDevice *dev; 24 const SCSIReqOps *ops; 25 uint32_t refcount; 26 uint32_t tag; 27 uint32_t lun; 28 uint32_t status; 29 void *hba_private; 30 size_t resid; 31 SCSICommand cmd; 32 NotifierList cancel_notifiers; 33 34 /* Note: 35 * - fields before sense are initialized by scsi_req_alloc; 36 * - sense[] is uninitialized; 37 * - fields after sense are memset to 0 by scsi_req_alloc. 38 * */ 39 40 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 41 uint32_t sense_len; 42 bool enqueued; 43 bool io_canceled; 44 bool retry; 45 bool dma_started; 46 BlockAIOCB *aiocb; 47 QEMUSGList *sg; 48 QTAILQ_ENTRY(SCSIRequest) next; 49 }; 50 51 #define TYPE_SCSI_DEVICE "scsi-device" 52 #define SCSI_DEVICE(obj) \ 53 OBJECT_CHECK(SCSIDevice, (obj), TYPE_SCSI_DEVICE) 54 #define SCSI_DEVICE_CLASS(klass) \ 55 OBJECT_CLASS_CHECK(SCSIDeviceClass, (klass), TYPE_SCSI_DEVICE) 56 #define SCSI_DEVICE_GET_CLASS(obj) \ 57 OBJECT_GET_CLASS(SCSIDeviceClass, (obj), TYPE_SCSI_DEVICE) 58 59 typedef struct SCSIDeviceClass { 60 DeviceClass parent_class; 61 void (*realize)(SCSIDevice *dev, Error **errp); 62 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 63 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 } SCSIDeviceClass; 68 69 struct SCSIDevice 70 { 71 DeviceState qdev; 72 VMChangeStateEntry *vmsentry; 73 QEMUBH *bh; 74 uint32_t id; 75 BlockConf conf; 76 SCSISense unit_attention; 77 bool sense_is_ua; 78 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 79 uint32_t sense_len; 80 QTAILQ_HEAD(, SCSIRequest) requests; 81 uint32_t channel; 82 uint32_t lun; 83 int blocksize; 84 int type; 85 uint64_t max_lba; 86 uint64_t wwn; 87 uint64_t port_wwn; 88 int scsi_version; 89 int default_scsi_version; 90 }; 91 92 extern const VMStateDescription vmstate_scsi_device; 93 94 #define VMSTATE_SCSI_DEVICE(_field, _state) { \ 95 .name = (stringify(_field)), \ 96 .size = sizeof(SCSIDevice), \ 97 .vmsd = &vmstate_scsi_device, \ 98 .flags = VMS_STRUCT, \ 99 .offset = vmstate_offset_value(_state, _field, SCSIDevice), \ 100 } 101 102 /* cdrom.c */ 103 int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track); 104 int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num); 105 106 /* scsi-bus.c */ 107 struct SCSIReqOps { 108 size_t size; 109 void (*free_req)(SCSIRequest *req); 110 int32_t (*send_command)(SCSIRequest *req, uint8_t *buf); 111 void (*read_data)(SCSIRequest *req); 112 void (*write_data)(SCSIRequest *req); 113 uint8_t *(*get_buf)(SCSIRequest *req); 114 115 void (*save_request)(QEMUFile *f, SCSIRequest *req); 116 void (*load_request)(QEMUFile *f, SCSIRequest *req); 117 }; 118 119 struct SCSIBusInfo { 120 int tcq; 121 int max_channel, max_target, max_lun; 122 int (*parse_cdb)(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 123 void *hba_private); 124 void (*transfer_data)(SCSIRequest *req, uint32_t arg); 125 void (*complete)(SCSIRequest *req, uint32_t arg, size_t resid); 126 void (*cancel)(SCSIRequest *req); 127 void (*change)(SCSIBus *bus, SCSIDevice *dev, SCSISense sense); 128 QEMUSGList *(*get_sg_list)(SCSIRequest *req); 129 130 void (*save_request)(QEMUFile *f, SCSIRequest *req); 131 void *(*load_request)(QEMUFile *f, SCSIRequest *req); 132 void (*free_request)(SCSIBus *bus, void *priv); 133 }; 134 135 #define TYPE_SCSI_BUS "SCSI" 136 #define SCSI_BUS(obj) OBJECT_CHECK(SCSIBus, (obj), TYPE_SCSI_BUS) 137 138 struct SCSIBus { 139 BusState qbus; 140 int busnr; 141 142 SCSISense unit_attention; 143 const SCSIBusInfo *info; 144 }; 145 146 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host, 147 const SCSIBusInfo *info, const char *bus_name); 148 149 static inline SCSIBus *scsi_bus_from_device(SCSIDevice *d) 150 { 151 return DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); 152 } 153 154 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, 155 int unit, bool removable, int bootindex, 156 bool share_rw, 157 const char *serial, Error **errp); 158 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus); 159 void scsi_legacy_handle_cmdline(void); 160 161 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, 162 uint32_t tag, uint32_t lun, void *hba_private); 163 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, 164 uint8_t *buf, void *hba_private); 165 int32_t scsi_req_enqueue(SCSIRequest *req); 166 SCSIRequest *scsi_req_ref(SCSIRequest *req); 167 void scsi_req_unref(SCSIRequest *req); 168 169 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, 170 void *hba_private); 171 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf); 172 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense); 173 void scsi_req_print(SCSIRequest *req); 174 void scsi_req_continue(SCSIRequest *req); 175 void scsi_req_data(SCSIRequest *req, int len); 176 void scsi_req_complete(SCSIRequest *req, int status); 177 uint8_t *scsi_req_get_buf(SCSIRequest *req); 178 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len); 179 void scsi_req_cancel_complete(SCSIRequest *req); 180 void scsi_req_cancel(SCSIRequest *req); 181 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier); 182 void scsi_req_retry(SCSIRequest *req); 183 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense); 184 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense); 185 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense); 186 void scsi_device_unit_attention_reported(SCSIDevice *dev); 187 void scsi_generic_read_device_identification(SCSIDevice *dev); 188 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed); 189 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lun); 190 191 /* scsi-generic.c. */ 192 extern const SCSIReqOps scsi_generic_req_ops; 193 194 #endif 195