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 #include "hw/virtio/virtio.h" 18 #include "hw/pci/pci.h" 19 #include "hw/scsi/scsi.h" 20 #include "sysemu/iothread.h" 21 #include "hw/virtio/dataplane/vring.h" 22 23 #define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common" 24 #define VIRTIO_SCSI_COMMON(obj) \ 25 OBJECT_CHECK(VirtIOSCSICommon, (obj), TYPE_VIRTIO_SCSI_COMMON) 26 27 #define TYPE_VIRTIO_SCSI "virtio-scsi-device" 28 #define VIRTIO_SCSI(obj) \ 29 OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI) 30 31 32 /* The ID for virtio_scsi */ 33 #define VIRTIO_ID_SCSI 8 34 35 /* Feature Bits */ 36 #define VIRTIO_SCSI_F_INOUT 0 37 #define VIRTIO_SCSI_F_HOTPLUG 1 38 #define VIRTIO_SCSI_F_CHANGE 2 39 40 #define VIRTIO_SCSI_VQ_SIZE 128 41 #define VIRTIO_SCSI_CDB_SIZE 32 42 #define VIRTIO_SCSI_SENSE_SIZE 96 43 #define VIRTIO_SCSI_MAX_CHANNEL 0 44 #define VIRTIO_SCSI_MAX_TARGET 255 45 #define VIRTIO_SCSI_MAX_LUN 16383 46 47 /* Response codes */ 48 #define VIRTIO_SCSI_S_OK 0 49 #define VIRTIO_SCSI_S_OVERRUN 1 50 #define VIRTIO_SCSI_S_ABORTED 2 51 #define VIRTIO_SCSI_S_BAD_TARGET 3 52 #define VIRTIO_SCSI_S_RESET 4 53 #define VIRTIO_SCSI_S_BUSY 5 54 #define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6 55 #define VIRTIO_SCSI_S_TARGET_FAILURE 7 56 #define VIRTIO_SCSI_S_NEXUS_FAILURE 8 57 #define VIRTIO_SCSI_S_FAILURE 9 58 #define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10 59 #define VIRTIO_SCSI_S_FUNCTION_REJECTED 11 60 #define VIRTIO_SCSI_S_INCORRECT_LUN 12 61 62 /* Controlq type codes. */ 63 #define VIRTIO_SCSI_T_TMF 0 64 #define VIRTIO_SCSI_T_AN_QUERY 1 65 #define VIRTIO_SCSI_T_AN_SUBSCRIBE 2 66 67 /* Valid TMF subtypes. */ 68 #define VIRTIO_SCSI_T_TMF_ABORT_TASK 0 69 #define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1 70 #define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2 71 #define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3 72 #define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4 73 #define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5 74 #define VIRTIO_SCSI_T_TMF_QUERY_TASK 6 75 #define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7 76 77 /* Events. */ 78 #define VIRTIO_SCSI_T_EVENTS_MISSED 0x80000000 79 #define VIRTIO_SCSI_T_NO_EVENT 0 80 #define VIRTIO_SCSI_T_TRANSPORT_RESET 1 81 #define VIRTIO_SCSI_T_ASYNC_NOTIFY 2 82 #define VIRTIO_SCSI_T_PARAM_CHANGE 3 83 84 /* Reasons for transport reset event */ 85 #define VIRTIO_SCSI_EVT_RESET_HARD 0 86 #define VIRTIO_SCSI_EVT_RESET_RESCAN 1 87 #define VIRTIO_SCSI_EVT_RESET_REMOVED 2 88 89 /* SCSI command request, followed by CDB and data-out */ 90 typedef struct { 91 uint8_t lun[8]; /* Logical Unit Number */ 92 uint64_t tag; /* Command identifier */ 93 uint8_t task_attr; /* Task attribute */ 94 uint8_t prio; 95 uint8_t crn; 96 } QEMU_PACKED VirtIOSCSICmdReq; 97 98 /* Response, followed by sense data and data-in */ 99 typedef struct { 100 uint32_t sense_len; /* Sense data length */ 101 uint32_t resid; /* Residual bytes in data buffer */ 102 uint16_t status_qualifier; /* Status qualifier */ 103 uint8_t status; /* Command completion status */ 104 uint8_t response; /* Response values */ 105 } QEMU_PACKED VirtIOSCSICmdResp; 106 107 /* Task Management Request */ 108 typedef struct { 109 uint32_t type; 110 uint32_t subtype; 111 uint8_t lun[8]; 112 uint64_t tag; 113 } QEMU_PACKED VirtIOSCSICtrlTMFReq; 114 115 typedef struct { 116 uint8_t response; 117 } QEMU_PACKED VirtIOSCSICtrlTMFResp; 118 119 /* Asynchronous notification query/subscription */ 120 typedef struct { 121 uint32_t type; 122 uint8_t lun[8]; 123 uint32_t event_requested; 124 } QEMU_PACKED VirtIOSCSICtrlANReq; 125 126 typedef struct { 127 uint32_t event_actual; 128 uint8_t response; 129 } QEMU_PACKED VirtIOSCSICtrlANResp; 130 131 typedef struct { 132 uint32_t event; 133 uint8_t lun[8]; 134 uint32_t reason; 135 } QEMU_PACKED VirtIOSCSIEvent; 136 137 typedef struct { 138 uint32_t num_queues; 139 uint32_t seg_max; 140 uint32_t max_sectors; 141 uint32_t cmd_per_lun; 142 uint32_t event_info_size; 143 uint32_t sense_size; 144 uint32_t cdb_size; 145 uint16_t max_channel; 146 uint16_t max_target; 147 uint32_t max_lun; 148 } QEMU_PACKED VirtIOSCSIConfig; 149 150 struct VirtIOSCSIConf { 151 uint32_t num_queues; 152 uint32_t max_sectors; 153 uint32_t cmd_per_lun; 154 char *vhostfd; 155 char *wwpn; 156 IOThread *iothread; 157 }; 158 159 struct VirtIOSCSI; 160 161 typedef struct { 162 struct VirtIOSCSI *parent; 163 Vring vring; 164 EventNotifier host_notifier; 165 EventNotifier guest_notifier; 166 } VirtIOSCSIVring; 167 168 typedef struct VirtIOSCSICommon { 169 VirtIODevice parent_obj; 170 VirtIOSCSIConf conf; 171 172 uint32_t sense_size; 173 uint32_t cdb_size; 174 VirtQueue *ctrl_vq; 175 VirtQueue *event_vq; 176 VirtQueue **cmd_vqs; 177 } VirtIOSCSICommon; 178 179 typedef struct VirtIOSCSI { 180 VirtIOSCSICommon parent_obj; 181 182 SCSIBus bus; 183 int resetting; 184 bool events_dropped; 185 186 /* Fields for dataplane below */ 187 AioContext *ctx; /* one iothread per virtio-scsi-pci for now */ 188 189 /* Vring is used instead of vq in dataplane code, because of the underlying 190 * memory layer thread safety */ 191 VirtIOSCSIVring *ctrl_vring; 192 VirtIOSCSIVring *event_vring; 193 VirtIOSCSIVring **cmd_vrings; 194 bool dataplane_started; 195 bool dataplane_starting; 196 bool dataplane_stopping; 197 bool dataplane_disabled; 198 bool dataplane_fenced; 199 Error *blocker; 200 Notifier migration_state_notifier; 201 } VirtIOSCSI; 202 203 typedef struct VirtIOSCSIReq { 204 VirtIOSCSI *dev; 205 VirtQueue *vq; 206 QEMUSGList qsgl; 207 QEMUIOVector resp_iov; 208 209 /* Note: 210 * - fields before elem are initialized by virtio_scsi_init_req; 211 * - elem is uninitialized at the time of allocation. 212 * - fields after elem are zeroed by virtio_scsi_init_req. 213 * */ 214 215 VirtQueueElement elem; 216 /* Set by dataplane code. */ 217 VirtIOSCSIVring *vring; 218 219 union { 220 /* Used for two-stage request submission */ 221 QTAILQ_ENTRY(VirtIOSCSIReq) next; 222 223 /* Used for cancellation of request during TMFs */ 224 int remaining; 225 }; 226 227 SCSIRequest *sreq; 228 size_t resp_size; 229 enum SCSIXferMode mode; 230 union { 231 VirtIOSCSICmdResp cmd; 232 VirtIOSCSICtrlTMFResp tmf; 233 VirtIOSCSICtrlANResp an; 234 VirtIOSCSIEvent event; 235 } resp; 236 union { 237 struct { 238 VirtIOSCSICmdReq cmd; 239 uint8_t cdb[]; 240 } QEMU_PACKED; 241 VirtIOSCSICtrlTMFReq tmf; 242 VirtIOSCSICtrlANReq an; 243 } req; 244 } VirtIOSCSIReq; 245 246 QEMU_BUILD_BUG_ON(offsetof(VirtIOSCSIReq, req.cdb) != 247 offsetof(VirtIOSCSIReq, req.cmd) + sizeof(VirtIOSCSICmdReq)); 248 249 #define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _conf_field) \ 250 DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1), \ 251 DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF),\ 252 DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128) 253 254 #define DEFINE_VIRTIO_SCSI_FEATURES(_state, _feature_field) \ 255 DEFINE_PROP_BIT("any_layout", _state, _feature_field, \ 256 VIRTIO_F_ANY_LAYOUT, true), \ 257 DEFINE_PROP_BIT("hotplug", _state, _feature_field, VIRTIO_SCSI_F_HOTPLUG, \ 258 true), \ 259 DEFINE_PROP_BIT("param_change", _state, _feature_field, \ 260 VIRTIO_SCSI_F_CHANGE, true) 261 262 typedef void (*HandleOutput)(VirtIODevice *, VirtQueue *); 263 264 void virtio_scsi_common_realize(DeviceState *dev, Error **errp, 265 HandleOutput ctrl, HandleOutput evt, 266 HandleOutput cmd); 267 268 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); 269 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req); 270 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req); 271 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req); 272 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq); 273 void virtio_scsi_free_req(VirtIOSCSIReq *req); 274 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 275 uint32_t event, uint32_t reason); 276 277 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread); 278 void virtio_scsi_dataplane_start(VirtIOSCSI *s); 279 void virtio_scsi_dataplane_stop(VirtIOSCSI *s); 280 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req); 281 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s, 282 VirtIOSCSIVring *vring); 283 284 #endif /* _QEMU_VIRTIO_SCSI_H */ 285