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 26 #define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common" 27 #define VIRTIO_SCSI_COMMON(obj) \ 28 OBJECT_CHECK(VirtIOSCSICommon, (obj), TYPE_VIRTIO_SCSI_COMMON) 29 30 #define TYPE_VIRTIO_SCSI "virtio-scsi-device" 31 #define VIRTIO_SCSI(obj) \ 32 OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI) 33 34 #define VIRTIO_SCSI_VQ_SIZE 128 35 #define VIRTIO_SCSI_MAX_CHANNEL 0 36 #define VIRTIO_SCSI_MAX_TARGET 255 37 #define VIRTIO_SCSI_MAX_LUN 16383 38 39 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq; 40 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp; 41 typedef struct virtio_scsi_ctrl_tmf_req VirtIOSCSICtrlTMFReq; 42 typedef struct virtio_scsi_ctrl_tmf_resp VirtIOSCSICtrlTMFResp; 43 typedef struct virtio_scsi_ctrl_an_req VirtIOSCSICtrlANReq; 44 typedef struct virtio_scsi_ctrl_an_resp VirtIOSCSICtrlANResp; 45 typedef struct virtio_scsi_event VirtIOSCSIEvent; 46 typedef struct virtio_scsi_config VirtIOSCSIConfig; 47 48 struct VirtIOSCSIConf { 49 uint32_t num_queues; 50 uint32_t max_sectors; 51 uint32_t cmd_per_lun; 52 #ifdef CONFIG_VHOST_SCSI 53 char *vhostfd; 54 char *wwpn; 55 #endif 56 uint32_t boot_tpgt; 57 IOThread *iothread; 58 }; 59 60 struct VirtIOSCSI; 61 62 typedef struct VirtIOSCSICommon { 63 VirtIODevice parent_obj; 64 VirtIOSCSIConf conf; 65 66 uint32_t sense_size; 67 uint32_t cdb_size; 68 VirtQueue *ctrl_vq; 69 VirtQueue *event_vq; 70 VirtQueue **cmd_vqs; 71 } VirtIOSCSICommon; 72 73 typedef struct VirtIOSCSI { 74 VirtIOSCSICommon parent_obj; 75 76 SCSIBus bus; 77 int resetting; 78 bool events_dropped; 79 80 /* Fields for dataplane below */ 81 AioContext *ctx; /* one iothread per virtio-scsi-pci for now */ 82 83 bool dataplane_started; 84 bool dataplane_starting; 85 bool dataplane_stopping; 86 bool dataplane_fenced; 87 uint32_t host_features; 88 } VirtIOSCSI; 89 90 typedef struct VirtIOSCSIReq { 91 /* Note: 92 * - fields up to resp_iov are initialized by virtio_scsi_init_req; 93 * - fields starting at vring are zeroed by virtio_scsi_init_req. 94 * */ 95 VirtQueueElement elem; 96 97 VirtIOSCSI *dev; 98 VirtQueue *vq; 99 QEMUSGList qsgl; 100 QEMUIOVector resp_iov; 101 102 union { 103 /* Used for two-stage request submission */ 104 QTAILQ_ENTRY(VirtIOSCSIReq) next; 105 106 /* Used for cancellation of request during TMFs */ 107 int remaining; 108 }; 109 110 SCSIRequest *sreq; 111 size_t resp_size; 112 enum SCSIXferMode mode; 113 union { 114 VirtIOSCSICmdResp cmd; 115 VirtIOSCSICtrlTMFResp tmf; 116 VirtIOSCSICtrlANResp an; 117 VirtIOSCSIEvent event; 118 } resp; 119 union { 120 VirtIOSCSICmdReq cmd; 121 VirtIOSCSICtrlTMFReq tmf; 122 VirtIOSCSICtrlANReq an; 123 } req; 124 } VirtIOSCSIReq; 125 126 static inline void virtio_scsi_acquire(VirtIOSCSI *s) 127 { 128 if (s->ctx) { 129 aio_context_acquire(s->ctx); 130 } 131 } 132 133 static inline void virtio_scsi_release(VirtIOSCSI *s) 134 { 135 if (s->ctx) { 136 aio_context_release(s->ctx); 137 } 138 } 139 140 void virtio_scsi_common_realize(DeviceState *dev, 141 VirtIOHandleOutput ctrl, 142 VirtIOHandleOutput evt, 143 VirtIOHandleOutput cmd, 144 Error **errp); 145 146 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp); 147 bool virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq); 148 bool virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq); 149 bool virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq); 150 void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req); 151 void virtio_scsi_free_req(VirtIOSCSIReq *req); 152 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 153 uint32_t event, uint32_t reason); 154 155 void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp); 156 int virtio_scsi_dataplane_start(VirtIODevice *s); 157 void virtio_scsi_dataplane_stop(VirtIODevice *s); 158 159 #endif /* QEMU_VIRTIO_SCSI_H */ 160