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 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 uint8_t cdb[]; 95 } QEMU_PACKED VirtIOSCSICmdReq; 96 97 /* Response, followed by sense data and data-in */ 98 typedef struct { 99 uint32_t sense_len; /* Sense data length */ 100 uint32_t resid; /* Residual bytes in data buffer */ 101 uint16_t status_qualifier; /* Status qualifier */ 102 uint8_t status; /* Command completion status */ 103 uint8_t response; /* Response values */ 104 uint8_t sense[]; 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 }; 157 158 typedef struct VirtIOSCSICommon { 159 VirtIODevice parent_obj; 160 VirtIOSCSIConf conf; 161 162 uint32_t sense_size; 163 uint32_t cdb_size; 164 VirtQueue *ctrl_vq; 165 VirtQueue *event_vq; 166 VirtQueue **cmd_vqs; 167 } VirtIOSCSICommon; 168 169 typedef struct { 170 VirtIOSCSICommon parent_obj; 171 172 SCSIBus bus; 173 int resetting; 174 bool events_dropped; 175 } VirtIOSCSI; 176 177 #define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _conf_field) \ 178 DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1), \ 179 DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF),\ 180 DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128) 181 182 #define DEFINE_VIRTIO_SCSI_FEATURES(_state, _feature_field) \ 183 DEFINE_VIRTIO_COMMON_FEATURES(_state, _feature_field), \ 184 DEFINE_PROP_BIT("hotplug", _state, _feature_field, VIRTIO_SCSI_F_HOTPLUG, \ 185 true), \ 186 DEFINE_PROP_BIT("param_change", _state, _feature_field, \ 187 VIRTIO_SCSI_F_CHANGE, true) 188 189 void virtio_scsi_common_realize(DeviceState *dev, Error **errp); 190 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); 191 192 #endif /* _QEMU_VIRTIO_SCSI_H */ 193