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