xref: /openbmc/qemu/include/hw/virtio/virtio-scsi.h (revision 81b07353c5e7ae9ae9360c357b7b4732b1cb03b4)
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 #include "standard-headers/linux/virtio_scsi.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/pci/pci.h"
20 #include "hw/scsi/scsi.h"
21 #include "sysemu/iothread.h"
22 #include "hw/virtio/dataplane/vring.h"
23 
24 #define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common"
25 #define VIRTIO_SCSI_COMMON(obj) \
26         OBJECT_CHECK(VirtIOSCSICommon, (obj), TYPE_VIRTIO_SCSI_COMMON)
27 
28 #define TYPE_VIRTIO_SCSI "virtio-scsi-device"
29 #define VIRTIO_SCSI(obj) \
30         OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI)
31 
32 #define VIRTIO_SCSI_VQ_SIZE     128
33 #define VIRTIO_SCSI_CDB_SIZE    32
34 #define VIRTIO_SCSI_SENSE_SIZE  96
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     char *vhostfd;
53     char *wwpn;
54     uint32_t boot_tpgt;
55     IOThread *iothread;
56 };
57 
58 struct VirtIOSCSI;
59 
60 typedef struct {
61     struct VirtIOSCSI *parent;
62     Vring vring;
63     EventNotifier host_notifier;
64     EventNotifier guest_notifier;
65 } VirtIOSCSIVring;
66 
67 typedef 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 } VirtIOSCSICommon;
77 
78 typedef 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     /* Vring is used instead of vq in dataplane code, because of the underlying
89      * memory layer thread safety */
90     VirtIOSCSIVring *ctrl_vring;
91     VirtIOSCSIVring *event_vring;
92     VirtIOSCSIVring **cmd_vrings;
93     bool dataplane_started;
94     bool dataplane_starting;
95     bool dataplane_stopping;
96     bool dataplane_disabled;
97     bool dataplane_fenced;
98     Error *blocker;
99     Notifier migration_state_notifier;
100 } VirtIOSCSI;
101 
102 typedef struct VirtIOSCSIReq {
103     VirtIOSCSI *dev;
104     VirtQueue *vq;
105     QEMUSGList qsgl;
106     QEMUIOVector resp_iov;
107 
108     /* Note:
109      * - fields before elem are initialized by virtio_scsi_init_req;
110      * - elem is uninitialized at the time of allocation.
111      * - fields after elem (except the ending cdb[]) are zeroed by
112      *   virtio_scsi_init_req.
113      * */
114 
115     VirtQueueElement elem;
116     /* Set by dataplane code. */
117     VirtIOSCSIVring *vring;
118 
119     union {
120         /* Used for two-stage request submission */
121         QTAILQ_ENTRY(VirtIOSCSIReq) next;
122 
123         /* Used for cancellation of request during TMFs */
124         int remaining;
125     };
126 
127     SCSIRequest *sreq;
128     size_t resp_size;
129     enum SCSIXferMode mode;
130     union {
131         VirtIOSCSICmdResp     cmd;
132         VirtIOSCSICtrlTMFResp tmf;
133         VirtIOSCSICtrlANResp  an;
134         VirtIOSCSIEvent       event;
135     } resp;
136     union {
137         struct {
138             VirtIOSCSICmdReq  cmd;
139             uint8_t           cdb[];
140         } QEMU_PACKED;
141         VirtIOSCSICtrlTMFReq  tmf;
142         VirtIOSCSICtrlANReq   an;
143     } req;
144 } VirtIOSCSIReq;
145 
146 QEMU_BUILD_BUG_ON(offsetof(VirtIOSCSIReq, req.cdb) !=
147                   offsetof(VirtIOSCSIReq, req.cmd) + sizeof(VirtIOSCSICmdReq));
148 
149 #define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _conf_field)                     \
150     DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1),       \
151     DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF),\
152     DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128)
153 
154 #define DEFINE_VIRTIO_SCSI_FEATURES(_state, _feature_field)                    \
155     DEFINE_PROP_BIT("any_layout", _state, _feature_field,                      \
156                     VIRTIO_F_ANY_LAYOUT, true),                                \
157     DEFINE_PROP_BIT("hotplug", _state, _feature_field, VIRTIO_SCSI_F_HOTPLUG,  \
158                                                        true),                  \
159     DEFINE_PROP_BIT("param_change", _state, _feature_field,                    \
160                                             VIRTIO_SCSI_F_CHANGE, true)
161 
162 typedef void (*HandleOutput)(VirtIODevice *, VirtQueue *);
163 
164 void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
165                                 HandleOutput ctrl, HandleOutput evt,
166                                 HandleOutput cmd);
167 
168 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp);
169 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req);
170 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req);
171 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req);
172 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq);
173 void virtio_scsi_free_req(VirtIOSCSIReq *req);
174 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
175                             uint32_t event, uint32_t reason);
176 
177 void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread);
178 void virtio_scsi_dataplane_start(VirtIOSCSI *s);
179 void virtio_scsi_dataplane_stop(VirtIOSCSI *s);
180 void virtio_scsi_vring_push_notify(VirtIOSCSIReq *req);
181 VirtIOSCSIReq *virtio_scsi_pop_req_vring(VirtIOSCSI *s,
182                                          VirtIOSCSIVring *vring);
183 
184 #endif /* _QEMU_VIRTIO_SCSI_H */
185