1 /* 2 * Virtio SCSI HBA 3 * 4 * Copyright IBM, Corp. 2010 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 #ifndef _QEMU_VIRTIO_SCSI_H 15 #define _QEMU_VIRTIO_SCSI_H 16 17 /* Override CDB/sense data size: they are dynamic (guest controlled) in QEMU */ 18 #define VIRTIO_SCSI_CDB_SIZE 0 19 #define VIRTIO_SCSI_SENSE_SIZE 0 20 #include "standard-headers/linux/virtio_scsi.h" 21 #include "hw/virtio/virtio.h" 22 #include "hw/pci/pci.h" 23 #include "hw/scsi/scsi.h" 24 #include "sysemu/iothread.h" 25 #include "hw/virtio/dataplane/vring.h" 26 27 #define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common" 28 #define VIRTIO_SCSI_COMMON(obj) \ 29 OBJECT_CHECK(VirtIOSCSICommon, (obj), TYPE_VIRTIO_SCSI_COMMON) 30 31 #define TYPE_VIRTIO_SCSI "virtio-scsi-device" 32 #define VIRTIO_SCSI(obj) \ 33 OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI) 34 35 #define VIRTIO_SCSI_VQ_SIZE 128 36 #define VIRTIO_SCSI_MAX_CHANNEL 0 37 #define VIRTIO_SCSI_MAX_TARGET 255 38 #define VIRTIO_SCSI_MAX_LUN 16383 39 40 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq; 41 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp; 42 typedef struct virtio_scsi_ctrl_tmf_req VirtIOSCSICtrlTMFReq; 43 typedef struct virtio_scsi_ctrl_tmf_resp VirtIOSCSICtrlTMFResp; 44 typedef struct virtio_scsi_ctrl_an_req VirtIOSCSICtrlANReq; 45 typedef struct virtio_scsi_ctrl_an_resp VirtIOSCSICtrlANResp; 46 typedef struct virtio_scsi_event VirtIOSCSIEvent; 47 typedef struct virtio_scsi_config VirtIOSCSIConfig; 48 49 struct VirtIOSCSIConf { 50 uint32_t num_queues; 51 uint32_t max_sectors; 52 uint32_t cmd_per_lun; 53 char *vhostfd; 54 char *wwpn; 55 uint32_t boot_tpgt; 56 IOThread *iothread; 57 }; 58 59 struct VirtIOSCSI; 60 61 typedef struct { 62 struct VirtIOSCSI *parent; 63 Vring vring; 64 EventNotifier host_notifier; 65 EventNotifier guest_notifier; 66 } VirtIOSCSIVring; 67 68 typedef struct VirtIOSCSICommon { 69 VirtIODevice parent_obj; 70 VirtIOSCSIConf conf; 71 72 uint32_t sense_size; 73 uint32_t cdb_size; 74 VirtQueue *ctrl_vq; 75 VirtQueue *event_vq; 76 VirtQueue **cmd_vqs; 77 } VirtIOSCSICommon; 78 79 typedef struct VirtIOSCSI { 80 VirtIOSCSICommon parent_obj; 81 82 SCSIBus bus; 83 int resetting; 84 bool events_dropped; 85 86 /* Fields for dataplane below */ 87 AioContext *ctx; /* one iothread per virtio-scsi-pci for now */ 88 89 /* Vring is used instead of vq in dataplane code, because of the underlying 90 * memory layer thread safety */ 91 VirtIOSCSIVring *ctrl_vring; 92 VirtIOSCSIVring *event_vring; 93 VirtIOSCSIVring **cmd_vrings; 94 bool dataplane_started; 95 bool dataplane_starting; 96 bool dataplane_stopping; 97 bool dataplane_disabled; 98 bool dataplane_fenced; 99 Error *blocker; 100 Notifier migration_state_notifier; 101 } VirtIOSCSI; 102 103 typedef struct VirtIOSCSIReq { 104 VirtIOSCSI *dev; 105 VirtQueue *vq; 106 QEMUSGList qsgl; 107 QEMUIOVector resp_iov; 108 109 /* Note: 110 * - fields before elem are initialized by virtio_scsi_init_req; 111 * - elem is uninitialized at the time of allocation. 112 * - fields after elem are zeroed by virtio_scsi_init_req. 113 * */ 114 115 VirtQueueElement elem; 116 /* Set by dataplane code. */ 117 VirtIOSCSIVring *vring; 118 119 union { 120 /* Used for two-stage request submission */ 121 QTAILQ_ENTRY(VirtIOSCSIReq) next; 122 123 /* Used for cancellation of request during TMFs */ 124 int remaining; 125 }; 126 127 SCSIRequest *sreq; 128 size_t resp_size; 129 enum SCSIXferMode mode; 130 union { 131 VirtIOSCSICmdResp cmd; 132 VirtIOSCSICtrlTMFResp tmf; 133 VirtIOSCSICtrlANResp an; 134 VirtIOSCSIEvent event; 135 } resp; 136 union { 137 VirtIOSCSICmdReq cmd; 138 VirtIOSCSICtrlTMFReq tmf; 139 VirtIOSCSICtrlANReq an; 140 } req; 141 } VirtIOSCSIReq; 142 143 #define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _conf_field) \ 144 DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1), \ 145 DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF),\ 146 DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128) 147 148 #define DEFINE_VIRTIO_SCSI_FEATURES(_state, _feature_field) \ 149 DEFINE_PROP_BIT("any_layout", _state, _feature_field, \ 150 VIRTIO_F_ANY_LAYOUT, true), \ 151 DEFINE_PROP_BIT("hotplug", _state, _feature_field, VIRTIO_SCSI_F_HOTPLUG, \ 152 true), \ 153 DEFINE_PROP_BIT("param_change", _state, _feature_field, \ 154 VIRTIO_SCSI_F_CHANGE, true) 155 156 typedef void (*HandleOutput)(VirtIODevice *, VirtQueue *); 157 158 void virtio_scsi_common_realize(DeviceState *dev, Error **errp, 159 HandleOutput ctrl, HandleOutput evt, 160 HandleOutput cmd); 161 162 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); 163 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req); 164 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req); 165 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req); 166 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq); 167 void virtio_scsi_free_req(VirtIOSCSIReq *req); 168 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 169 uint32_t event, uint32_t reason); 170 171 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread); 172 void virtio_scsi_dataplane_start(VirtIOSCSI *s); 173 void virtio_scsi_dataplane_stop(VirtIOSCSI *s); 174 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req); 175 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s, 176 VirtIOSCSIVring *vring); 177 178 #endif /* _QEMU_VIRTIO_SCSI_H */ 179