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 21 #define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common" 22 #define VIRTIO_SCSI_COMMON(obj) \ 23 OBJECT_CHECK(VirtIOSCSICommon, (obj), TYPE_VIRTIO_SCSI_COMMON) 24 25 #define TYPE_VIRTIO_SCSI "virtio-scsi-device" 26 #define VIRTIO_SCSI(obj) \ 27 OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI) 28 29 30 /* The ID for virtio_scsi */ 31 #define VIRTIO_ID_SCSI 8 32 33 /* Feature Bits */ 34 #define VIRTIO_SCSI_F_INOUT 0 35 #define VIRTIO_SCSI_F_HOTPLUG 1 36 #define VIRTIO_SCSI_F_CHANGE 2 37 38 #define VIRTIO_SCSI_VQ_SIZE 128 39 #define VIRTIO_SCSI_CDB_SIZE 32 40 #define VIRTIO_SCSI_SENSE_SIZE 96 41 #define VIRTIO_SCSI_MAX_CHANNEL 0 42 #define VIRTIO_SCSI_MAX_TARGET 255 43 #define VIRTIO_SCSI_MAX_LUN 16383 44 45 /* Response codes */ 46 #define VIRTIO_SCSI_S_OK 0 47 #define VIRTIO_SCSI_S_OVERRUN 1 48 #define VIRTIO_SCSI_S_ABORTED 2 49 #define VIRTIO_SCSI_S_BAD_TARGET 3 50 #define VIRTIO_SCSI_S_RESET 4 51 #define VIRTIO_SCSI_S_BUSY 5 52 #define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6 53 #define VIRTIO_SCSI_S_TARGET_FAILURE 7 54 #define VIRTIO_SCSI_S_NEXUS_FAILURE 8 55 #define VIRTIO_SCSI_S_FAILURE 9 56 #define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10 57 #define VIRTIO_SCSI_S_FUNCTION_REJECTED 11 58 #define VIRTIO_SCSI_S_INCORRECT_LUN 12 59 60 /* Controlq type codes. */ 61 #define VIRTIO_SCSI_T_TMF 0 62 #define VIRTIO_SCSI_T_AN_QUERY 1 63 #define VIRTIO_SCSI_T_AN_SUBSCRIBE 2 64 65 /* Valid TMF subtypes. */ 66 #define VIRTIO_SCSI_T_TMF_ABORT_TASK 0 67 #define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1 68 #define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2 69 #define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3 70 #define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4 71 #define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5 72 #define VIRTIO_SCSI_T_TMF_QUERY_TASK 6 73 #define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7 74 75 /* Events. */ 76 #define VIRTIO_SCSI_T_EVENTS_MISSED 0x80000000 77 #define VIRTIO_SCSI_T_NO_EVENT 0 78 #define VIRTIO_SCSI_T_TRANSPORT_RESET 1 79 #define VIRTIO_SCSI_T_ASYNC_NOTIFY 2 80 #define VIRTIO_SCSI_T_PARAM_CHANGE 3 81 82 /* Reasons for transport reset event */ 83 #define VIRTIO_SCSI_EVT_RESET_HARD 0 84 #define VIRTIO_SCSI_EVT_RESET_RESCAN 1 85 #define VIRTIO_SCSI_EVT_RESET_REMOVED 2 86 87 /* SCSI command request, followed by CDB and data-out */ 88 typedef struct { 89 uint8_t lun[8]; /* Logical Unit Number */ 90 uint64_t tag; /* Command identifier */ 91 uint8_t task_attr; /* Task attribute */ 92 uint8_t prio; 93 uint8_t crn; 94 } QEMU_PACKED VirtIOSCSICmdReq; 95 96 /* Response, followed by sense data and data-in */ 97 typedef struct { 98 uint32_t sense_len; /* Sense data length */ 99 uint32_t resid; /* Residual bytes in data buffer */ 100 uint16_t status_qualifier; /* Status qualifier */ 101 uint8_t status; /* Command completion status */ 102 uint8_t response; /* Response values */ 103 } QEMU_PACKED VirtIOSCSICmdResp; 104 105 /* Task Management Request */ 106 typedef struct { 107 uint32_t type; 108 uint32_t subtype; 109 uint8_t lun[8]; 110 uint64_t tag; 111 } QEMU_PACKED VirtIOSCSICtrlTMFReq; 112 113 typedef struct { 114 uint8_t response; 115 } QEMU_PACKED VirtIOSCSICtrlTMFResp; 116 117 /* Asynchronous notification query/subscription */ 118 typedef struct { 119 uint32_t type; 120 uint8_t lun[8]; 121 uint32_t event_requested; 122 } QEMU_PACKED VirtIOSCSICtrlANReq; 123 124 typedef struct { 125 uint32_t event_actual; 126 uint8_t response; 127 } QEMU_PACKED VirtIOSCSICtrlANResp; 128 129 typedef struct { 130 uint32_t event; 131 uint8_t lun[8]; 132 uint32_t reason; 133 } QEMU_PACKED VirtIOSCSIEvent; 134 135 typedef struct { 136 uint32_t num_queues; 137 uint32_t seg_max; 138 uint32_t max_sectors; 139 uint32_t cmd_per_lun; 140 uint32_t event_info_size; 141 uint32_t sense_size; 142 uint32_t cdb_size; 143 uint16_t max_channel; 144 uint16_t max_target; 145 uint32_t max_lun; 146 } QEMU_PACKED VirtIOSCSIConfig; 147 148 struct VirtIOSCSIConf { 149 uint32_t num_queues; 150 uint32_t max_sectors; 151 uint32_t cmd_per_lun; 152 char *vhostfd; 153 char *wwpn; 154 }; 155 156 typedef struct VirtIOSCSICommon { 157 VirtIODevice parent_obj; 158 VirtIOSCSIConf conf; 159 160 uint32_t sense_size; 161 uint32_t cdb_size; 162 VirtQueue *ctrl_vq; 163 VirtQueue *event_vq; 164 VirtQueue **cmd_vqs; 165 } VirtIOSCSICommon; 166 167 typedef struct { 168 VirtIOSCSICommon parent_obj; 169 170 SCSIBus bus; 171 int resetting; 172 bool events_dropped; 173 } VirtIOSCSI; 174 175 #define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _conf_field) \ 176 DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1), \ 177 DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF),\ 178 DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128) 179 180 #define DEFINE_VIRTIO_SCSI_FEATURES(_state, _feature_field) \ 181 DEFINE_VIRTIO_COMMON_FEATURES(_state, _feature_field), \ 182 DEFINE_PROP_BIT("hotplug", _state, _feature_field, VIRTIO_SCSI_F_HOTPLUG, \ 183 true), \ 184 DEFINE_PROP_BIT("param_change", _state, _feature_field, \ 185 VIRTIO_SCSI_F_CHANGE, true) 186 187 typedef void (*HandleOutput)(VirtIODevice *, VirtQueue *); 188 189 void virtio_scsi_common_realize(DeviceState *dev, Error **errp, 190 HandleOutput ctrl, HandleOutput evt, 191 HandleOutput cmd); 192 193 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); 194 195 #endif /* _QEMU_VIRTIO_SCSI_H */ 196