]> git.proxmox.com Git - qemu.git/blame - hw/virtio-scsi.c
virtio-scsi: Add basic request processing infrastructure
[qemu.git] / hw / virtio-scsi.c
CommitLineData
973abc7f
SH
1/*
2 * Virtio SCSI HBA
3 *
4 * Copyright IBM, Corp. 2010
5 * Copyright Red Hat, Inc. 2011
6 *
7 * Authors:
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15
16#include "virtio-scsi.h"
17#include <hw/scsi.h>
18#include <hw/scsi-defs.h>
19
20#define VIRTIO_SCSI_VQ_SIZE 128
21#define VIRTIO_SCSI_CDB_SIZE 32
22#define VIRTIO_SCSI_SENSE_SIZE 96
23#define VIRTIO_SCSI_MAX_CHANNEL 0
24#define VIRTIO_SCSI_MAX_TARGET 255
25#define VIRTIO_SCSI_MAX_LUN 16383
26
27/* Response codes */
28#define VIRTIO_SCSI_S_OK 0
29#define VIRTIO_SCSI_S_OVERRUN 1
30#define VIRTIO_SCSI_S_ABORTED 2
31#define VIRTIO_SCSI_S_BAD_TARGET 3
32#define VIRTIO_SCSI_S_RESET 4
33#define VIRTIO_SCSI_S_BUSY 5
34#define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6
35#define VIRTIO_SCSI_S_TARGET_FAILURE 7
36#define VIRTIO_SCSI_S_NEXUS_FAILURE 8
37#define VIRTIO_SCSI_S_FAILURE 9
38#define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10
39#define VIRTIO_SCSI_S_FUNCTION_REJECTED 11
40#define VIRTIO_SCSI_S_INCORRECT_LUN 12
41
42/* Controlq type codes. */
43#define VIRTIO_SCSI_T_TMF 0
44#define VIRTIO_SCSI_T_AN_QUERY 1
45#define VIRTIO_SCSI_T_AN_SUBSCRIBE 2
46
47/* Valid TMF subtypes. */
48#define VIRTIO_SCSI_T_TMF_ABORT_TASK 0
49#define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1
50#define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2
51#define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3
52#define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4
53#define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5
54#define VIRTIO_SCSI_T_TMF_QUERY_TASK 6
55#define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7
56
57/* Events. */
58#define VIRTIO_SCSI_T_EVENTS_MISSED 0x80000000
59#define VIRTIO_SCSI_T_NO_EVENT 0
60#define VIRTIO_SCSI_T_TRANSPORT_RESET 1
61#define VIRTIO_SCSI_T_ASYNC_NOTIFY 2
62
63/* SCSI command request, followed by data-out */
64typedef struct {
65 uint8_t lun[8]; /* Logical Unit Number */
66 uint64_t tag; /* Command identifier */
67 uint8_t task_attr; /* Task attribute */
68 uint8_t prio;
69 uint8_t crn;
70 uint8_t cdb[];
71} QEMU_PACKED VirtIOSCSICmdReq;
72
73/* Response, followed by sense data and data-in */
74typedef struct {
75 uint32_t sense_len; /* Sense data length */
76 uint32_t resid; /* Residual bytes in data buffer */
77 uint16_t status_qualifier; /* Status qualifier */
78 uint8_t status; /* Command completion status */
79 uint8_t response; /* Response values */
80 uint8_t sense[];
81} QEMU_PACKED VirtIOSCSICmdResp;
82
83/* Task Management Request */
84typedef struct {
85 uint32_t type;
86 uint32_t subtype;
87 uint8_t lun[8];
88 uint64_t tag;
89} QEMU_PACKED VirtIOSCSICtrlTMFReq;
90
91typedef struct {
92 uint8_t response;
93} QEMU_PACKED VirtIOSCSICtrlTMFResp;
94
95/* Asynchronous notification query/subscription */
96typedef struct {
97 uint32_t type;
98 uint8_t lun[8];
99 uint32_t event_requested;
100} QEMU_PACKED VirtIOSCSICtrlANReq;
101
102typedef struct {
103 uint32_t event_actual;
104 uint8_t response;
105} QEMU_PACKED VirtIOSCSICtrlANResp;
106
107typedef struct {
108 uint32_t event;
109 uint8_t lun[8];
110 uint32_t reason;
111} QEMU_PACKED VirtIOSCSIEvent;
112
113typedef struct {
114 uint32_t num_queues;
115 uint32_t seg_max;
116 uint32_t max_sectors;
117 uint32_t cmd_per_lun;
118 uint32_t event_info_size;
119 uint32_t sense_size;
120 uint32_t cdb_size;
121 uint16_t max_channel;
122 uint16_t max_target;
123 uint32_t max_lun;
124} QEMU_PACKED VirtIOSCSIConfig;
125
126typedef struct {
127 VirtIODevice vdev;
128 DeviceState *qdev;
129 VirtIOSCSIConf *conf;
130
131 VirtQueue *ctrl_vq;
132 VirtQueue *event_vq;
133 VirtQueue *cmd_vq;
134 uint32_t sense_size;
135 uint32_t cdb_size;
136} VirtIOSCSI;
137
326799c0
SH
138typedef struct VirtIOSCSIReq {
139 VirtIOSCSI *dev;
140 VirtQueue *vq;
141 VirtQueueElement elem;
142 QEMUSGList qsgl;
143 SCSIRequest *sreq;
144 union {
145 char *buf;
146 VirtIOSCSICmdReq *cmd;
147 VirtIOSCSICtrlTMFReq *tmf;
148 VirtIOSCSICtrlANReq *an;
149 } req;
150 union {
151 char *buf;
152 VirtIOSCSICmdResp *cmd;
153 VirtIOSCSICtrlTMFResp *tmf;
154 VirtIOSCSICtrlANResp *an;
155 VirtIOSCSIEvent *event;
156 } resp;
157} VirtIOSCSIReq;
158
159static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
160{
161 VirtIOSCSI *s = req->dev;
162 VirtQueue *vq = req->vq;
163 virtqueue_push(vq, &req->elem, req->qsgl.size + req->elem.in_sg[0].iov_len);
164 qemu_sglist_destroy(&req->qsgl);
165 if (req->sreq) {
166 req->sreq->hba_private = NULL;
167 scsi_req_unref(req->sreq);
168 }
169 g_free(req);
170 virtio_notify(&s->vdev, vq);
171}
172
173static void virtio_scsi_bad_req(void)
174{
175 error_report("wrong size for virtio-scsi headers");
176 exit(1);
177}
178
179static void qemu_sgl_init_external(QEMUSGList *qsgl, struct iovec *sg,
180 target_phys_addr_t *addr, int num)
181{
182 memset(qsgl, 0, sizeof(*qsgl));
183 while (num--) {
184 qemu_sglist_add(qsgl, *(addr++), (sg++)->iov_len);
185 }
186}
187
188static void virtio_scsi_parse_req(VirtIOSCSI *s, VirtQueue *vq,
189 VirtIOSCSIReq *req)
190{
191 assert(req->elem.out_num && req->elem.in_num);
192 req->vq = vq;
193 req->dev = s;
194 req->sreq = NULL;
195 req->req.buf = req->elem.out_sg[0].iov_base;
196 req->resp.buf = req->elem.in_sg[0].iov_base;
197
198 if (req->elem.out_num > 1) {
199 qemu_sgl_init_external(&req->qsgl, &req->elem.out_sg[1],
200 &req->elem.out_addr[1],
201 req->elem.out_num - 1);
202 } else {
203 qemu_sgl_init_external(&req->qsgl, &req->elem.in_sg[1],
204 &req->elem.in_addr[1],
205 req->elem.in_num - 1);
206 }
207}
208
209static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
210{
211 VirtIOSCSIReq *req;
212 req = g_malloc(sizeof(*req));
213 if (!virtqueue_pop(vq, &req->elem)) {
214 g_free(req);
215 return NULL;
216 }
217
218 virtio_scsi_parse_req(s, vq, req);
219 return req;
220}
221
222static void virtio_scsi_fail_ctrl_req(VirtIOSCSIReq *req)
223{
224 if (req->req.tmf->type == VIRTIO_SCSI_T_TMF) {
225 req->resp.tmf->response = VIRTIO_SCSI_S_FAILURE;
226 } else {
227 req->resp.an->response = VIRTIO_SCSI_S_FAILURE;
228 }
229
230 virtio_scsi_complete_req(req);
231}
232
973abc7f
SH
233static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
234{
326799c0
SH
235 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
236 VirtIOSCSIReq *req;
237
238 while ((req = virtio_scsi_pop_req(s, vq))) {
239 virtio_scsi_fail_ctrl_req(req);
240 }
241}
242
243static void virtio_scsi_fail_cmd_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
244{
245 req->resp.cmd->response = VIRTIO_SCSI_S_FAILURE;
246 virtio_scsi_complete_req(req);
973abc7f
SH
247}
248
249static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
250{
326799c0
SH
251 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
252 VirtIOSCSIReq *req;
253
254 while ((req = virtio_scsi_pop_req(s, vq))) {
255 int out_size, in_size;
256 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
257 virtio_scsi_bad_req();
258 }
259
260 out_size = req->elem.out_sg[0].iov_len;
261 in_size = req->elem.in_sg[0].iov_len;
262 if (out_size < sizeof(VirtIOSCSICmdReq) + s->cdb_size ||
263 in_size < sizeof(VirtIOSCSICmdResp) + s->sense_size) {
264 virtio_scsi_bad_req();
265 }
266
267 if (req->elem.out_num > 1 && req->elem.in_num > 1) {
268 virtio_scsi_fail_cmd_req(s, req);
269 continue;
270 }
271
272 req->resp.cmd->resid = 0;
273 req->resp.cmd->status_qualifier = 0;
274 req->resp.cmd->status = CHECK_CONDITION;
275 req->resp.cmd->sense_len = 4;
276 req->resp.cmd->sense[0] = 0xf0; /* Fixed format current sense */
277 req->resp.cmd->sense[1] = ILLEGAL_REQUEST;
278 req->resp.cmd->sense[2] = 0x20;
279 req->resp.cmd->sense[3] = 0x00;
280 req->resp.cmd->response = VIRTIO_SCSI_S_OK;
281
282 virtio_scsi_complete_req(req);
283 }
973abc7f
SH
284}
285
286static void virtio_scsi_get_config(VirtIODevice *vdev,
287 uint8_t *config)
288{
289 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
290 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
291
292 stl_raw(&scsiconf->num_queues, s->conf->num_queues);
293 stl_raw(&scsiconf->seg_max, 128 - 2);
294 stl_raw(&scsiconf->max_sectors, s->conf->max_sectors);
295 stl_raw(&scsiconf->cmd_per_lun, s->conf->cmd_per_lun);
296 stl_raw(&scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
297 stl_raw(&scsiconf->sense_size, s->sense_size);
298 stl_raw(&scsiconf->cdb_size, s->cdb_size);
299 stl_raw(&scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
300 stl_raw(&scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
301 stl_raw(&scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
302}
303
304static void virtio_scsi_set_config(VirtIODevice *vdev,
305 const uint8_t *config)
306{
307 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
308 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
309
310 if ((uint32_t) ldl_raw(&scsiconf->sense_size) >= 65536 ||
311 (uint32_t) ldl_raw(&scsiconf->cdb_size) >= 256) {
312 error_report("bad data written to virtio-scsi configuration space");
313 exit(1);
314 }
315
316 s->sense_size = ldl_raw(&scsiconf->sense_size);
317 s->cdb_size = ldl_raw(&scsiconf->cdb_size);
318}
319
320static uint32_t virtio_scsi_get_features(VirtIODevice *vdev,
321 uint32_t requested_features)
322{
323 return requested_features;
324}
325
326static void virtio_scsi_reset(VirtIODevice *vdev)
327{
328 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
329
330 s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
331 s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
332}
333
334VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
335{
336 VirtIOSCSI *s;
337
338 s = (VirtIOSCSI *)virtio_common_init("virtio-scsi", VIRTIO_ID_SCSI,
339 sizeof(VirtIOSCSIConfig),
340 sizeof(VirtIOSCSI));
341
342 s->qdev = dev;
343 s->conf = proxyconf;
344
345 /* TODO set up vdev function pointers */
346 s->vdev.get_config = virtio_scsi_get_config;
347 s->vdev.set_config = virtio_scsi_set_config;
348 s->vdev.get_features = virtio_scsi_get_features;
349 s->vdev.reset = virtio_scsi_reset;
350
351 s->ctrl_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
352 virtio_scsi_handle_ctrl);
353 s->event_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
354 NULL);
355 s->cmd_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
356 virtio_scsi_handle_cmd);
357
358 /* TODO savevm */
359
360 return &s->vdev;
361}
362
363void virtio_scsi_exit(VirtIODevice *vdev)
364{
365 virtio_cleanup(vdev);
366}