Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 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 | |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 17 | #include "hw/virtio/virtio.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 18 | #include "hw/pci/pci.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 19 | #include "hw/scsi/scsi.h" |
Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 20 | |
Paolo Bonzini | 292c8e5 | 2013-03-29 01:08:15 +0000 | [diff] [blame] | 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 | |
KONRAD Frederic | f574fa8 | 2013-04-09 12:04:48 +0200 | [diff] [blame] | 25 | #define TYPE_VIRTIO_SCSI "virtio-scsi-device" |
KONRAD Frederic | 3ab1dfd | 2013-03-21 15:15:14 +0100 | [diff] [blame] | 26 | #define VIRTIO_SCSI(obj) \ |
| 27 | OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI) |
| 28 | |
| 29 | |
Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 30 | /* The ID for virtio_scsi */ |
| 31 | #define VIRTIO_ID_SCSI 8 |
| 32 | |
Paolo Bonzini | 07a5298 | 2012-08-20 15:23:28 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 292c8e5 | 2013-03-29 01:08:15 +0000 | [diff] [blame] | 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 | |
Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 150 | struct VirtIOSCSIConf { |
| 151 | uint32_t num_queues; |
| 152 | uint32_t max_sectors; |
| 153 | uint32_t cmd_per_lun; |
Nicholas Bellinger | 5e9be92 | 2013-03-29 01:08:16 +0000 | [diff] [blame] | 154 | char *vhostfd; |
| 155 | char *wwpn; |
Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
Paolo Bonzini | 292c8e5 | 2013-03-29 01:08:15 +0000 | [diff] [blame] | 158 | typedef struct VirtIOSCSICommon { |
KONRAD Frederic | 0ac8e13 | 2013-03-21 15:15:18 +0100 | [diff] [blame] | 159 | VirtIODevice parent_obj; |
KONRAD Frederic | 394e2e4 | 2013-03-21 15:15:11 +0100 | [diff] [blame] | 160 | VirtIOSCSIConf conf; |
KONRAD Frederic | f1b24e8 | 2013-03-18 17:37:18 +0100 | [diff] [blame] | 161 | |
KONRAD Frederic | f1b24e8 | 2013-03-18 17:37:18 +0100 | [diff] [blame] | 162 | uint32_t sense_size; |
| 163 | uint32_t cdb_size; |
KONRAD Frederic | f1b24e8 | 2013-03-18 17:37:18 +0100 | [diff] [blame] | 164 | VirtQueue *ctrl_vq; |
| 165 | VirtQueue *event_vq; |
KONRAD Frederic | 2221952 | 2013-03-21 15:15:12 +0100 | [diff] [blame] | 166 | VirtQueue **cmd_vqs; |
Paolo Bonzini | 292c8e5 | 2013-03-29 01:08:15 +0000 | [diff] [blame] | 167 | } VirtIOSCSICommon; |
| 168 | |
| 169 | typedef struct { |
| 170 | VirtIOSCSICommon parent_obj; |
| 171 | |
| 172 | SCSIBus bus; |
| 173 | int resetting; |
| 174 | bool events_dropped; |
KONRAD Frederic | f1b24e8 | 2013-03-18 17:37:18 +0100 | [diff] [blame] | 175 | } VirtIOSCSI; |
| 176 | |
KONRAD Frederic | 4bfeb18 | 2013-03-21 15:15:13 +0100 | [diff] [blame] | 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) |
Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 188 | |
Andreas Färber | 71a6520 | 2013-07-30 03:19:55 +0200 | [diff] [blame] | 189 | void virtio_scsi_common_realize(DeviceState *dev, Error **errp); |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 190 | void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); |
Paolo Bonzini | 292c8e5 | 2013-03-29 01:08:15 +0000 | [diff] [blame] | 191 | |
Stefan Hajnoczi | 973abc7 | 2011-02-11 08:40:59 +0000 | [diff] [blame] | 192 | #endif /* _QEMU_VIRTIO_SCSI_H */ |