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