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